blob: 44cdb6d035f215d9ebafe49eaaa5c115c2ea5318 [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>
Pierre Ossmand129bce2006-03-24 03:18:17 -080025
Pierre Ossman2f730fe2008-03-17 10:29:38 +010026#include <linux/leds.h>
27
Aries Lee22113ef2010-12-15 08:14:24 +010028#include <linux/mmc/mmc.h>
Pierre Ossmand129bce2006-03-24 03:18:17 -080029#include <linux/mmc/host.h>
Aaron Lu473b0952012-07-03 17:27:49 +080030#include <linux/mmc/card.h>
Corneliu Doban85cc1c32015-02-09 16:06:29 -080031#include <linux/mmc/sdio.h>
Guennadi Liakhovetskibec9d4e2012-09-17 16:45:10 +080032#include <linux/mmc/slot-gpio.h>
Pierre Ossmand129bce2006-03-24 03:18:17 -080033
Pierre Ossmand129bce2006-03-24 03:18:17 -080034#include "sdhci.h"
35
36#define DRIVER_NAME "sdhci"
Pierre Ossmand129bce2006-03-24 03:18:17 -080037
Pierre Ossmand129bce2006-03-24 03:18:17 -080038#define DBG(f, x...) \
Russell Kingc6563172006-03-29 09:30:20 +010039 pr_debug(DRIVER_NAME " [%s()]: " f, __func__,## x)
Pierre Ossmand129bce2006-03-24 03:18:17 -080040
Arindam Nathb513ea22011-05-05 12:19:04 +053041#define MAX_TUNING_LOOP 40
42
Pierre Ossmandf673b22006-06-30 02:22:31 -070043static unsigned int debug_quirks = 0;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +030044static unsigned int debug_quirks2;
Pierre Ossman67435272006-06-30 02:22:31 -070045
Pierre Ossmand129bce2006-03-24 03:18:17 -080046static void sdhci_finish_data(struct sdhci_host *);
47
Pierre Ossmand129bce2006-03-24 03:18:17 -080048static void sdhci_finish_command(struct sdhci_host *);
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
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300113static void sdhci_set_card_detection(struct sdhci_host *host, bool enable)
114{
Russell King5b4f1f62014-04-25 12:57:02 +0100115 u32 present;
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300116
Adrian Hunterc79396c2011-12-27 15:48:42 +0200117 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) ||
Jaehoon Chung860951c2016-06-21 10:13:26 +0900118 !mmc_card_is_removable(host->mmc))
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300119 return;
120
Russell King5b4f1f62014-04-25 12:57:02 +0100121 if (enable) {
122 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
123 SDHCI_CARD_PRESENT;
Shawn Guod25928d2011-06-21 22:41:48 +0800124
Russell King5b4f1f62014-04-25 12:57:02 +0100125 host->ier |= present ? SDHCI_INT_CARD_REMOVE :
126 SDHCI_INT_CARD_INSERT;
127 } else {
128 host->ier &= ~(SDHCI_INT_CARD_REMOVE | SDHCI_INT_CARD_INSERT);
129 }
Russell Kingb537f942014-04-25 12:56:01 +0100130
131 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
132 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300133}
134
135static void sdhci_enable_card_detection(struct sdhci_host *host)
136{
137 sdhci_set_card_detection(host, true);
138}
139
140static void sdhci_disable_card_detection(struct sdhci_host *host)
141{
142 sdhci_set_card_detection(host, false);
143}
144
Ulf Hansson02d0b682016-04-11 15:32:41 +0200145static void sdhci_runtime_pm_bus_on(struct sdhci_host *host)
146{
147 if (host->bus_on)
148 return;
149 host->bus_on = true;
150 pm_runtime_get_noresume(host->mmc->parent);
151}
152
153static void sdhci_runtime_pm_bus_off(struct sdhci_host *host)
154{
155 if (!host->bus_on)
156 return;
157 host->bus_on = false;
158 pm_runtime_put_noidle(host->mmc->parent);
159}
160
Russell King03231f92014-04-25 12:57:12 +0100161void sdhci_reset(struct sdhci_host *host, u8 mask)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800162{
Pierre Ossmane16514d82006-06-30 02:22:24 -0700163 unsigned long timeout;
Philip Rakity393c1a32011-01-21 11:26:40 -0800164
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300165 sdhci_writeb(host, mask, SDHCI_SOFTWARE_RESET);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800166
Adrian Hunterf0710a52013-05-06 12:17:32 +0300167 if (mask & SDHCI_RESET_ALL) {
Pierre Ossmand129bce2006-03-24 03:18:17 -0800168 host->clock = 0;
Adrian Hunterf0710a52013-05-06 12:17:32 +0300169 /* Reset-all turns off SD Bus Power */
170 if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
171 sdhci_runtime_pm_bus_off(host);
172 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800173
Pierre Ossmane16514d82006-06-30 02:22:24 -0700174 /* Wait max 100 ms */
175 timeout = 100;
176
177 /* hw clears the bit when it's done */
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300178 while (sdhci_readb(host, SDHCI_SOFTWARE_RESET) & mask) {
Pierre Ossmane16514d82006-06-30 02:22:24 -0700179 if (timeout == 0) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530180 pr_err("%s: Reset 0x%x never completed.\n",
Pierre Ossmane16514d82006-06-30 02:22:24 -0700181 mmc_hostname(host->mmc), (int)mask);
182 sdhci_dumpregs(host);
183 return;
184 }
185 timeout--;
186 mdelay(1);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800187 }
Russell King03231f92014-04-25 12:57:12 +0100188}
189EXPORT_SYMBOL_GPL(sdhci_reset);
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300190
Russell King03231f92014-04-25 12:57:12 +0100191static void sdhci_do_reset(struct sdhci_host *host, u8 mask)
192{
193 if (host->quirks & SDHCI_QUIRK_NO_CARD_NO_RESET) {
Adrian Hunterd3940f22016-06-29 16:24:14 +0300194 struct mmc_host *mmc = host->mmc;
195
196 if (!mmc->ops->get_cd(mmc))
Russell King03231f92014-04-25 12:57:12 +0100197 return;
198 }
199
200 host->ops->reset(host, mask);
Philip Rakity393c1a32011-01-21 11:26:40 -0800201
Russell Kingda91a8f2014-04-25 13:00:12 +0100202 if (mask & SDHCI_RESET_ALL) {
203 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
204 if (host->ops->enable_dma)
205 host->ops->enable_dma(host);
206 }
207
208 /* Resetting the controller clears many */
209 host->preset_enabled = false;
Shaohui Xie3abc1e802011-12-29 16:33:00 +0800210 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800211}
212
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800213static void sdhci_init(struct sdhci_host *host, int soft)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800214{
Adrian Hunterd3940f22016-06-29 16:24:14 +0300215 struct mmc_host *mmc = host->mmc;
216
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800217 if (soft)
Russell King03231f92014-04-25 12:57:12 +0100218 sdhci_do_reset(host, SDHCI_RESET_CMD|SDHCI_RESET_DATA);
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800219 else
Russell King03231f92014-04-25 12:57:12 +0100220 sdhci_do_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800221
Russell Kingb537f942014-04-25 12:56:01 +0100222 host->ier = SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT |
223 SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT |
224 SDHCI_INT_INDEX | SDHCI_INT_END_BIT | SDHCI_INT_CRC |
225 SDHCI_INT_TIMEOUT | SDHCI_INT_DATA_END |
226 SDHCI_INT_RESPONSE;
227
228 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
229 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800230
231 if (soft) {
232 /* force clock reconfiguration */
233 host->clock = 0;
Adrian Hunterd3940f22016-06-29 16:24:14 +0300234 mmc->ops->set_ios(mmc, &mmc->ios);
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800235 }
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300236}
Pierre Ossmand129bce2006-03-24 03:18:17 -0800237
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300238static void sdhci_reinit(struct sdhci_host *host)
239{
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800240 sdhci_init(host, 0);
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300241 sdhci_enable_card_detection(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800242}
243
Adrian Hunter061d17a2016-04-12 14:25:09 +0300244static void __sdhci_led_activate(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800245{
246 u8 ctrl;
247
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300248 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800249 ctrl |= SDHCI_CTRL_LED;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300250 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800251}
252
Adrian Hunter061d17a2016-04-12 14:25:09 +0300253static void __sdhci_led_deactivate(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
Masahiro Yamada4f782302016-04-14 13:19:39 +0900262#if IS_REACHABLE(CONFIG_LEDS_CLASS)
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100263static void sdhci_led_control(struct led_classdev *led,
Adrian Hunter061d17a2016-04-12 14:25:09 +0300264 enum led_brightness brightness)
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100265{
266 struct sdhci_host *host = container_of(led, struct sdhci_host, led);
267 unsigned long flags;
268
269 spin_lock_irqsave(&host->lock, flags);
270
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300271 if (host->runtime_suspended)
272 goto out;
273
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100274 if (brightness == LED_OFF)
Adrian Hunter061d17a2016-04-12 14:25:09 +0300275 __sdhci_led_deactivate(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100276 else
Adrian Hunter061d17a2016-04-12 14:25:09 +0300277 __sdhci_led_activate(host);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300278out:
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100279 spin_unlock_irqrestore(&host->lock, flags);
280}
Adrian Hunter061d17a2016-04-12 14:25:09 +0300281
282static int sdhci_led_register(struct sdhci_host *host)
283{
284 struct mmc_host *mmc = host->mmc;
285
286 snprintf(host->led_name, sizeof(host->led_name),
287 "%s::", mmc_hostname(mmc));
288
289 host->led.name = host->led_name;
290 host->led.brightness = LED_OFF;
291 host->led.default_trigger = mmc_hostname(mmc);
292 host->led.brightness_set = sdhci_led_control;
293
294 return led_classdev_register(mmc_dev(mmc), &host->led);
295}
296
297static void sdhci_led_unregister(struct sdhci_host *host)
298{
299 led_classdev_unregister(&host->led);
300}
301
302static inline void sdhci_led_activate(struct sdhci_host *host)
303{
304}
305
306static inline void sdhci_led_deactivate(struct sdhci_host *host)
307{
308}
309
310#else
311
312static inline int sdhci_led_register(struct sdhci_host *host)
313{
314 return 0;
315}
316
317static inline void sdhci_led_unregister(struct sdhci_host *host)
318{
319}
320
321static inline void sdhci_led_activate(struct sdhci_host *host)
322{
323 __sdhci_led_activate(host);
324}
325
326static inline void sdhci_led_deactivate(struct sdhci_host *host)
327{
328 __sdhci_led_deactivate(host);
329}
330
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100331#endif
332
Pierre Ossmand129bce2006-03-24 03:18:17 -0800333/*****************************************************************************\
334 * *
335 * Core functions *
336 * *
337\*****************************************************************************/
338
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100339static void sdhci_read_block_pio(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800340{
Pierre Ossman76591502008-07-21 00:32:11 +0200341 unsigned long flags;
342 size_t blksize, len, chunk;
Steven Noonan7244b852008-10-01 01:50:25 -0700343 u32 uninitialized_var(scratch);
Pierre Ossman76591502008-07-21 00:32:11 +0200344 u8 *buf;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800345
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100346 DBG("PIO reading\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800347
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100348 blksize = host->data->blksz;
Pierre Ossman76591502008-07-21 00:32:11 +0200349 chunk = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800350
Pierre Ossman76591502008-07-21 00:32:11 +0200351 local_irq_save(flags);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800352
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100353 while (blksize) {
Fabio Estevambf3a35a2015-05-09 18:44:51 -0300354 BUG_ON(!sg_miter_next(&host->sg_miter));
Pierre Ossmand129bce2006-03-24 03:18:17 -0800355
Pierre Ossman76591502008-07-21 00:32:11 +0200356 len = min(host->sg_miter.length, blksize);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800357
Pierre Ossman76591502008-07-21 00:32:11 +0200358 blksize -= len;
359 host->sg_miter.consumed = len;
Alex Dubov14d836e2007-04-13 19:04:38 +0200360
Pierre Ossman76591502008-07-21 00:32:11 +0200361 buf = host->sg_miter.addr;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800362
Pierre Ossman76591502008-07-21 00:32:11 +0200363 while (len) {
364 if (chunk == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300365 scratch = sdhci_readl(host, SDHCI_BUFFER);
Pierre Ossman76591502008-07-21 00:32:11 +0200366 chunk = 4;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800367 }
Pierre Ossman76591502008-07-21 00:32:11 +0200368
369 *buf = scratch & 0xFF;
370
371 buf++;
372 scratch >>= 8;
373 chunk--;
374 len--;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800375 }
376 }
Pierre Ossman76591502008-07-21 00:32:11 +0200377
378 sg_miter_stop(&host->sg_miter);
379
380 local_irq_restore(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100381}
Pierre Ossmand129bce2006-03-24 03:18:17 -0800382
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100383static void sdhci_write_block_pio(struct sdhci_host *host)
384{
Pierre Ossman76591502008-07-21 00:32:11 +0200385 unsigned long flags;
386 size_t blksize, len, chunk;
387 u32 scratch;
388 u8 *buf;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100389
390 DBG("PIO writing\n");
391
392 blksize = host->data->blksz;
Pierre Ossman76591502008-07-21 00:32:11 +0200393 chunk = 0;
394 scratch = 0;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100395
Pierre Ossman76591502008-07-21 00:32:11 +0200396 local_irq_save(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100397
398 while (blksize) {
Fabio Estevambf3a35a2015-05-09 18:44:51 -0300399 BUG_ON(!sg_miter_next(&host->sg_miter));
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100400
Pierre Ossman76591502008-07-21 00:32:11 +0200401 len = min(host->sg_miter.length, blksize);
Alex Dubov14d836e2007-04-13 19:04:38 +0200402
Pierre Ossman76591502008-07-21 00:32:11 +0200403 blksize -= len;
404 host->sg_miter.consumed = len;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100405
Pierre Ossman76591502008-07-21 00:32:11 +0200406 buf = host->sg_miter.addr;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100407
Pierre Ossman76591502008-07-21 00:32:11 +0200408 while (len) {
409 scratch |= (u32)*buf << (chunk * 8);
410
411 buf++;
412 chunk++;
413 len--;
414
415 if ((chunk == 4) || ((len == 0) && (blksize == 0))) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300416 sdhci_writel(host, scratch, SDHCI_BUFFER);
Pierre Ossman76591502008-07-21 00:32:11 +0200417 chunk = 0;
418 scratch = 0;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100419 }
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100420 }
421 }
Pierre Ossman76591502008-07-21 00:32:11 +0200422
423 sg_miter_stop(&host->sg_miter);
424
425 local_irq_restore(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100426}
427
428static void sdhci_transfer_pio(struct sdhci_host *host)
429{
430 u32 mask;
431
432 BUG_ON(!host->data);
433
Pierre Ossman76591502008-07-21 00:32:11 +0200434 if (host->blocks == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100435 return;
436
437 if (host->data->flags & MMC_DATA_READ)
438 mask = SDHCI_DATA_AVAILABLE;
439 else
440 mask = SDHCI_SPACE_AVAILABLE;
441
Pierre Ossman4a3cba32008-07-29 00:11:16 +0200442 /*
443 * Some controllers (JMicron JMB38x) mess up the buffer bits
444 * for transfers < 4 bytes. As long as it is just one block,
445 * we can ignore the bits.
446 */
447 if ((host->quirks & SDHCI_QUIRK_BROKEN_SMALL_PIO) &&
448 (host->data->blocks == 1))
449 mask = ~0;
450
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300451 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Anton Vorontsov3e3bf202009-03-17 00:14:00 +0300452 if (host->quirks & SDHCI_QUIRK_PIO_NEEDS_DELAY)
453 udelay(100);
454
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100455 if (host->data->flags & MMC_DATA_READ)
456 sdhci_read_block_pio(host);
457 else
458 sdhci_write_block_pio(host);
459
Pierre Ossman76591502008-07-21 00:32:11 +0200460 host->blocks--;
461 if (host->blocks == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100462 break;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100463 }
464
465 DBG("PIO transfer complete.\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800466}
467
Russell King48857d92016-01-26 13:40:16 +0000468static int sdhci_pre_dma_transfer(struct sdhci_host *host,
Russell Kingc0999b72016-01-26 13:40:27 +0000469 struct mmc_data *data, int cookie)
Russell King48857d92016-01-26 13:40:16 +0000470{
471 int sg_count;
472
Russell King94538e52016-01-26 13:40:37 +0000473 /*
474 * If the data buffers are already mapped, return the previous
475 * dma_map_sg() result.
476 */
477 if (data->host_cookie == COOKIE_PRE_MAPPED)
Russell King48857d92016-01-26 13:40:16 +0000478 return data->sg_count;
Russell King48857d92016-01-26 13:40:16 +0000479
480 sg_count = dma_map_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
481 data->flags & MMC_DATA_WRITE ?
482 DMA_TO_DEVICE : DMA_FROM_DEVICE);
483
484 if (sg_count == 0)
485 return -ENOSPC;
486
487 data->sg_count = sg_count;
Russell Kingc0999b72016-01-26 13:40:27 +0000488 data->host_cookie = cookie;
Russell King48857d92016-01-26 13:40:16 +0000489
490 return sg_count;
491}
492
Pierre Ossman2134a922008-06-28 18:28:51 +0200493static char *sdhci_kmap_atomic(struct scatterlist *sg, unsigned long *flags)
494{
495 local_irq_save(*flags);
Cong Wang482fce92011-11-27 13:27:00 +0800496 return kmap_atomic(sg_page(sg)) + sg->offset;
Pierre Ossman2134a922008-06-28 18:28:51 +0200497}
498
499static void sdhci_kunmap_atomic(void *buffer, unsigned long *flags)
500{
Cong Wang482fce92011-11-27 13:27:00 +0800501 kunmap_atomic(buffer);
Pierre Ossman2134a922008-06-28 18:28:51 +0200502 local_irq_restore(*flags);
503}
504
Adrian Huntere57a5f62014-11-04 12:42:46 +0200505static void sdhci_adma_write_desc(struct sdhci_host *host, void *desc,
506 dma_addr_t addr, int len, unsigned cmd)
Ben Dooks118cd172010-03-05 13:43:26 -0800507{
Adrian Huntere57a5f62014-11-04 12:42:46 +0200508 struct sdhci_adma2_64_desc *dma_desc = desc;
Ben Dooks118cd172010-03-05 13:43:26 -0800509
Adrian Huntere57a5f62014-11-04 12:42:46 +0200510 /* 32-bit and 64-bit descriptors have these members in same position */
Adrian Hunter05452302014-11-04 12:42:45 +0200511 dma_desc->cmd = cpu_to_le16(cmd);
512 dma_desc->len = cpu_to_le16(len);
Adrian Huntere57a5f62014-11-04 12:42:46 +0200513 dma_desc->addr_lo = cpu_to_le32((u32)addr);
514
515 if (host->flags & SDHCI_USE_64_BIT_DMA)
516 dma_desc->addr_hi = cpu_to_le32((u64)addr >> 32);
Ben Dooks118cd172010-03-05 13:43:26 -0800517}
518
Adrian Hunterb5ffa672014-11-04 12:42:40 +0200519static void sdhci_adma_mark_end(void *desc)
520{
Adrian Huntere57a5f62014-11-04 12:42:46 +0200521 struct sdhci_adma2_64_desc *dma_desc = desc;
Adrian Hunterb5ffa672014-11-04 12:42:40 +0200522
Adrian Huntere57a5f62014-11-04 12:42:46 +0200523 /* 32-bit and 64-bit descriptors have 'cmd' in same position */
Adrian Hunter05452302014-11-04 12:42:45 +0200524 dma_desc->cmd |= cpu_to_le16(ADMA2_END);
Adrian Hunterb5ffa672014-11-04 12:42:40 +0200525}
526
Russell King60c64762016-01-26 13:40:22 +0000527static void sdhci_adma_table_pre(struct sdhci_host *host,
528 struct mmc_data *data, int sg_count)
Pierre Ossman2134a922008-06-28 18:28:51 +0200529{
Pierre Ossman2134a922008-06-28 18:28:51 +0200530 struct scatterlist *sg;
Pierre Ossman2134a922008-06-28 18:28:51 +0200531 unsigned long flags;
Russell Kingacc3ad12016-01-26 13:40:00 +0000532 dma_addr_t addr, align_addr;
533 void *desc, *align;
534 char *buffer;
535 int len, offset, i;
Pierre Ossman2134a922008-06-28 18:28:51 +0200536
537 /*
538 * The spec does not specify endianness of descriptor table.
539 * We currently guess that it is LE.
540 */
541
Russell King60c64762016-01-26 13:40:22 +0000542 host->sg_count = sg_count;
Pierre Ossman2134a922008-06-28 18:28:51 +0200543
Adrian Hunter4efaa6f2014-11-04 12:42:39 +0200544 desc = host->adma_table;
Pierre Ossman2134a922008-06-28 18:28:51 +0200545 align = host->align_buffer;
546
547 align_addr = host->align_addr;
548
549 for_each_sg(data->sg, sg, host->sg_count, i) {
550 addr = sg_dma_address(sg);
551 len = sg_dma_len(sg);
552
553 /*
Russell Kingacc3ad12016-01-26 13:40:00 +0000554 * The SDHCI specification states that ADMA addresses must
555 * be 32-bit aligned. If they aren't, then we use a bounce
556 * buffer for the (up to three) bytes that screw up the
Pierre Ossman2134a922008-06-28 18:28:51 +0200557 * alignment.
558 */
Adrian Hunter04a5ae62015-11-26 14:00:49 +0200559 offset = (SDHCI_ADMA2_ALIGN - (addr & SDHCI_ADMA2_MASK)) &
560 SDHCI_ADMA2_MASK;
Pierre Ossman2134a922008-06-28 18:28:51 +0200561 if (offset) {
562 if (data->flags & MMC_DATA_WRITE) {
563 buffer = sdhci_kmap_atomic(sg, &flags);
564 memcpy(align, buffer, offset);
565 sdhci_kunmap_atomic(buffer, &flags);
566 }
567
Ben Dooks118cd172010-03-05 13:43:26 -0800568 /* tran, valid */
Adrian Huntere57a5f62014-11-04 12:42:46 +0200569 sdhci_adma_write_desc(host, desc, align_addr, offset,
Adrian Hunter739d46d2014-11-04 12:42:44 +0200570 ADMA2_TRAN_VALID);
Pierre Ossman2134a922008-06-28 18:28:51 +0200571
572 BUG_ON(offset > 65536);
573
Adrian Hunter04a5ae62015-11-26 14:00:49 +0200574 align += SDHCI_ADMA2_ALIGN;
575 align_addr += SDHCI_ADMA2_ALIGN;
Pierre Ossman2134a922008-06-28 18:28:51 +0200576
Adrian Hunter76fe3792014-11-04 12:42:42 +0200577 desc += host->desc_sz;
Pierre Ossman2134a922008-06-28 18:28:51 +0200578
579 addr += offset;
580 len -= offset;
581 }
582
Pierre Ossman2134a922008-06-28 18:28:51 +0200583 BUG_ON(len > 65536);
584
Adrian Hunter347ea322015-11-26 14:00:48 +0200585 if (len) {
586 /* tran, valid */
587 sdhci_adma_write_desc(host, desc, addr, len,
588 ADMA2_TRAN_VALID);
589 desc += host->desc_sz;
590 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200591
592 /*
593 * If this triggers then we have a calculation bug
594 * somewhere. :/
595 */
Adrian Hunter76fe3792014-11-04 12:42:42 +0200596 WARN_ON((desc - host->adma_table) >= host->adma_table_sz);
Pierre Ossman2134a922008-06-28 18:28:51 +0200597 }
598
Thomas Abraham70764a92010-05-26 14:42:04 -0700599 if (host->quirks & SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC) {
Russell Kingacc3ad12016-01-26 13:40:00 +0000600 /* Mark the last descriptor as the terminating descriptor */
Adrian Hunter4efaa6f2014-11-04 12:42:39 +0200601 if (desc != host->adma_table) {
Adrian Hunter76fe3792014-11-04 12:42:42 +0200602 desc -= host->desc_sz;
Adrian Hunterb5ffa672014-11-04 12:42:40 +0200603 sdhci_adma_mark_end(desc);
Thomas Abraham70764a92010-05-26 14:42:04 -0700604 }
605 } else {
Russell Kingacc3ad12016-01-26 13:40:00 +0000606 /* Add a terminating entry - nop, end, valid */
Adrian Huntere57a5f62014-11-04 12:42:46 +0200607 sdhci_adma_write_desc(host, desc, 0, 0, ADMA2_NOP_END_VALID);
Thomas Abraham70764a92010-05-26 14:42:04 -0700608 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200609}
610
611static void sdhci_adma_table_post(struct sdhci_host *host,
612 struct mmc_data *data)
613{
Pierre Ossman2134a922008-06-28 18:28:51 +0200614 struct scatterlist *sg;
615 int i, size;
Adrian Hunter1c3d5f62014-11-04 12:42:41 +0200616 void *align;
Pierre Ossman2134a922008-06-28 18:28:51 +0200617 char *buffer;
618 unsigned long flags;
619
Russell King47fa9612016-01-26 13:40:06 +0000620 if (data->flags & MMC_DATA_READ) {
621 bool has_unaligned = false;
Russell Kingde0b65a2014-04-25 12:58:29 +0100622
Russell King47fa9612016-01-26 13:40:06 +0000623 /* Do a quick scan of the SG list for any unaligned mappings */
624 for_each_sg(data->sg, sg, host->sg_count, i)
Adrian Hunter04a5ae62015-11-26 14:00:49 +0200625 if (sg_dma_address(sg) & SDHCI_ADMA2_MASK) {
Russell King47fa9612016-01-26 13:40:06 +0000626 has_unaligned = true;
627 break;
628 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200629
Russell King47fa9612016-01-26 13:40:06 +0000630 if (has_unaligned) {
631 dma_sync_sg_for_cpu(mmc_dev(host->mmc), data->sg,
Russell Kingf55c98f2016-01-26 13:40:11 +0000632 data->sg_len, DMA_FROM_DEVICE);
Pierre Ossman2134a922008-06-28 18:28:51 +0200633
Russell King47fa9612016-01-26 13:40:06 +0000634 align = host->align_buffer;
635
636 for_each_sg(data->sg, sg, host->sg_count, i) {
637 if (sg_dma_address(sg) & SDHCI_ADMA2_MASK) {
638 size = SDHCI_ADMA2_ALIGN -
639 (sg_dma_address(sg) & SDHCI_ADMA2_MASK);
640
641 buffer = sdhci_kmap_atomic(sg, &flags);
642 memcpy(buffer, align, size);
643 sdhci_kunmap_atomic(buffer, &flags);
644
645 align += SDHCI_ADMA2_ALIGN;
646 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200647 }
648 }
649 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200650}
651
Andrei Warkentina3c77782011-04-11 16:13:42 -0500652static u8 sdhci_calc_timeout(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800653{
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700654 u8 count;
Andrei Warkentina3c77782011-04-11 16:13:42 -0500655 struct mmc_data *data = cmd->data;
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700656 unsigned target_timeout, current_timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800657
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200658 /*
659 * If the host controller provides us with an incorrect timeout
660 * value, just skip the check and use 0xE. The hardware may take
661 * longer to time out, but that's much better than having a too-short
662 * timeout value.
663 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +0200664 if (host->quirks & SDHCI_QUIRK_BROKEN_TIMEOUT_VAL)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200665 return 0xE;
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200666
Andrei Warkentina3c77782011-04-11 16:13:42 -0500667 /* Unspecified timeout, assume max */
Ulf Hansson1d4d7742014-01-08 15:06:08 +0100668 if (!data && !cmd->busy_timeout)
Andrei Warkentina3c77782011-04-11 16:13:42 -0500669 return 0xE;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800670
Andrei Warkentina3c77782011-04-11 16:13:42 -0500671 /* timeout in us */
672 if (!data)
Ulf Hansson1d4d7742014-01-08 15:06:08 +0100673 target_timeout = cmd->busy_timeout * 1000;
Andy Shevchenko78a2ca22011-08-03 18:35:59 +0300674 else {
Russell Kingfafcfda2016-01-26 13:40:58 +0000675 target_timeout = DIV_ROUND_UP(data->timeout_ns, 1000);
Russell King7f055382016-01-26 13:41:04 +0000676 if (host->clock && data->timeout_clks) {
677 unsigned long long val;
678
679 /*
680 * data->timeout_clks is in units of clock cycles.
681 * host->clock is in Hz. target_timeout is in us.
682 * Hence, us = 1000000 * cycles / Hz. Round up.
683 */
684 val = 1000000 * data->timeout_clks;
685 if (do_div(val, host->clock))
686 target_timeout++;
687 target_timeout += val;
688 }
Andy Shevchenko78a2ca22011-08-03 18:35:59 +0300689 }
Anton Vorontsov81b39802009-09-22 16:45:13 -0700690
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700691 /*
692 * Figure out needed cycles.
693 * We do this in steps in order to fit inside a 32 bit int.
694 * The first step is the minimum timeout, which will have a
695 * minimum resolution of 6 bits:
696 * (1) 2^13*1000 > 2^22,
697 * (2) host->timeout_clk < 2^16
698 * =>
699 * (1) / (2) > 2^6
700 */
701 count = 0;
702 current_timeout = (1 << 13) * 1000 / host->timeout_clk;
703 while (current_timeout < target_timeout) {
704 count++;
705 current_timeout <<= 1;
706 if (count >= 0xF)
707 break;
708 }
709
710 if (count >= 0xF) {
Chris Ball09eeff52012-06-01 10:39:45 -0400711 DBG("%s: Too large timeout 0x%x requested for CMD%d!\n",
712 mmc_hostname(host->mmc), count, cmd->opcode);
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700713 count = 0xE;
714 }
715
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200716 return count;
717}
718
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300719static void sdhci_set_transfer_irqs(struct sdhci_host *host)
720{
721 u32 pio_irqs = SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL;
722 u32 dma_irqs = SDHCI_INT_DMA_END | SDHCI_INT_ADMA_ERROR;
723
724 if (host->flags & SDHCI_REQ_USE_DMA)
Russell Kingb537f942014-04-25 12:56:01 +0100725 host->ier = (host->ier & ~pio_irqs) | dma_irqs;
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300726 else
Russell Kingb537f942014-04-25 12:56:01 +0100727 host->ier = (host->ier & ~dma_irqs) | pio_irqs;
728
729 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
730 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300731}
732
Aisheng Dongb45e6682014-08-27 15:26:29 +0800733static void sdhci_set_timeout(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200734{
735 u8 count;
Aisheng Dongb45e6682014-08-27 15:26:29 +0800736
737 if (host->ops->set_timeout) {
738 host->ops->set_timeout(host, cmd);
739 } else {
740 count = sdhci_calc_timeout(host, cmd);
741 sdhci_writeb(host, count, SDHCI_TIMEOUT_CONTROL);
742 }
743}
744
745static void sdhci_prepare_data(struct sdhci_host *host, struct mmc_command *cmd)
746{
Pierre Ossman2134a922008-06-28 18:28:51 +0200747 u8 ctrl;
Andrei Warkentina3c77782011-04-11 16:13:42 -0500748 struct mmc_data *data = cmd->data;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200749
750 WARN_ON(host->data);
751
Aisheng Dongb45e6682014-08-27 15:26:29 +0800752 if (data || (cmd->flags & MMC_RSP_BUSY))
753 sdhci_set_timeout(host, cmd);
Andrei Warkentina3c77782011-04-11 16:13:42 -0500754
755 if (!data)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200756 return;
757
758 /* Sanity checks */
759 BUG_ON(data->blksz * data->blocks > 524288);
760 BUG_ON(data->blksz > host->mmc->max_blk_size);
761 BUG_ON(data->blocks > 65535);
762
763 host->data = data;
764 host->data_early = 0;
Mikko Vinnif6a03cb2011-04-12 09:36:18 -0400765 host->data->bytes_xfered = 0;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200766
Russell Kingfce14422016-01-26 13:41:20 +0000767 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossman2134a922008-06-28 18:28:51 +0200768 struct scatterlist *sg;
Russell Kingdf953922016-01-26 13:41:14 +0000769 unsigned int length_mask, offset_mask;
Russell Kinga0eaf0f2016-01-26 13:41:09 +0000770 int i;
Pierre Ossman2134a922008-06-28 18:28:51 +0200771
Russell Kingfce14422016-01-26 13:41:20 +0000772 host->flags |= SDHCI_REQ_USE_DMA;
773
774 /*
775 * FIXME: This doesn't account for merging when mapping the
776 * scatterlist.
777 *
778 * The assumption here being that alignment and lengths are
779 * the same after DMA mapping to device address space.
780 */
Russell Kinga0eaf0f2016-01-26 13:41:09 +0000781 length_mask = 0;
Russell Kingdf953922016-01-26 13:41:14 +0000782 offset_mask = 0;
Pierre Ossman2134a922008-06-28 18:28:51 +0200783 if (host->flags & SDHCI_USE_ADMA) {
Russell Kingdf953922016-01-26 13:41:14 +0000784 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE) {
Russell Kinga0eaf0f2016-01-26 13:41:09 +0000785 length_mask = 3;
Russell Kingdf953922016-01-26 13:41:14 +0000786 /*
787 * As we use up to 3 byte chunks to work
788 * around alignment problems, we need to
789 * check the offset as well.
790 */
791 offset_mask = 3;
792 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200793 } else {
794 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE)
Russell Kinga0eaf0f2016-01-26 13:41:09 +0000795 length_mask = 3;
Russell Kingdf953922016-01-26 13:41:14 +0000796 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR)
797 offset_mask = 3;
Pierre Ossman2134a922008-06-28 18:28:51 +0200798 }
799
Russell Kingdf953922016-01-26 13:41:14 +0000800 if (unlikely(length_mask | offset_mask)) {
Pierre Ossman2134a922008-06-28 18:28:51 +0200801 for_each_sg(data->sg, sg, data->sg_len, i) {
Russell Kinga0eaf0f2016-01-26 13:41:09 +0000802 if (sg->length & length_mask) {
Marek Vasut2e4456f2015-11-18 10:47:02 +0100803 DBG("Reverting to PIO because of transfer size (%d)\n",
Russell Kinga0eaf0f2016-01-26 13:41:09 +0000804 sg->length);
Pierre Ossman2134a922008-06-28 18:28:51 +0200805 host->flags &= ~SDHCI_REQ_USE_DMA;
806 break;
807 }
Russell Kinga0eaf0f2016-01-26 13:41:09 +0000808 if (sg->offset & offset_mask) {
Marek Vasut2e4456f2015-11-18 10:47:02 +0100809 DBG("Reverting to PIO because of bad alignment\n");
Pierre Ossman2134a922008-06-28 18:28:51 +0200810 host->flags &= ~SDHCI_REQ_USE_DMA;
811 break;
812 }
813 }
814 }
815 }
816
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200817 if (host->flags & SDHCI_REQ_USE_DMA) {
Russell Kingc0999b72016-01-26 13:40:27 +0000818 int sg_cnt = sdhci_pre_dma_transfer(host, data, COOKIE_MAPPED);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200819
Russell King60c64762016-01-26 13:40:22 +0000820 if (sg_cnt <= 0) {
821 /*
822 * This only happens when someone fed
823 * us an invalid request.
824 */
825 WARN_ON(1);
826 host->flags &= ~SDHCI_REQ_USE_DMA;
827 } else if (host->flags & SDHCI_USE_ADMA) {
828 sdhci_adma_table_pre(host, data, sg_cnt);
829
830 sdhci_writel(host, host->adma_addr, SDHCI_ADMA_ADDRESS);
831 if (host->flags & SDHCI_USE_64_BIT_DMA)
832 sdhci_writel(host,
833 (u64)host->adma_addr >> 32,
834 SDHCI_ADMA_ADDRESS_HI);
835 } else {
836 WARN_ON(sg_cnt != 1);
837 sdhci_writel(host, sg_dma_address(data->sg),
838 SDHCI_DMA_ADDRESS);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200839 }
840 }
841
Pierre Ossman2134a922008-06-28 18:28:51 +0200842 /*
843 * Always adjust the DMA selection as some controllers
844 * (e.g. JMicron) can't do PIO properly when the selection
845 * is ADMA.
846 */
847 if (host->version >= SDHCI_SPEC_200) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300848 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossman2134a922008-06-28 18:28:51 +0200849 ctrl &= ~SDHCI_CTRL_DMA_MASK;
850 if ((host->flags & SDHCI_REQ_USE_DMA) &&
Adrian Huntere57a5f62014-11-04 12:42:46 +0200851 (host->flags & SDHCI_USE_ADMA)) {
852 if (host->flags & SDHCI_USE_64_BIT_DMA)
853 ctrl |= SDHCI_CTRL_ADMA64;
854 else
855 ctrl |= SDHCI_CTRL_ADMA32;
856 } else {
Pierre Ossman2134a922008-06-28 18:28:51 +0200857 ctrl |= SDHCI_CTRL_SDMA;
Adrian Huntere57a5f62014-11-04 12:42:46 +0200858 }
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300859 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100860 }
861
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200862 if (!(host->flags & SDHCI_REQ_USE_DMA)) {
Sebastian Andrzej Siewiorda60a912009-06-18 09:33:32 +0200863 int flags;
864
865 flags = SG_MITER_ATOMIC;
866 if (host->data->flags & MMC_DATA_READ)
867 flags |= SG_MITER_TO_SG;
868 else
869 flags |= SG_MITER_FROM_SG;
870 sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
Pierre Ossman76591502008-07-21 00:32:11 +0200871 host->blocks = data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800872 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700873
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300874 sdhci_set_transfer_irqs(host);
875
Mikko Vinnif6a03cb2011-04-12 09:36:18 -0400876 /* Set the DMA boundary value and block size */
877 sdhci_writew(host, SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG,
878 data->blksz), SDHCI_BLOCK_SIZE);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300879 sdhci_writew(host, data->blocks, SDHCI_BLOCK_COUNT);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700880}
881
882static void sdhci_set_transfer_mode(struct sdhci_host *host,
Andrei Warkentine89d4562011-05-23 15:06:37 -0500883 struct mmc_command *cmd)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700884{
Vincent Yangd3fc5d72015-01-20 16:05:17 +0800885 u16 mode = 0;
Andrei Warkentine89d4562011-05-23 15:06:37 -0500886 struct mmc_data *data = cmd->data;
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700887
Dong Aisheng2b558c12013-10-30 22:09:48 +0800888 if (data == NULL) {
Vincent Wan9b8ffea2014-11-05 14:09:00 +0800889 if (host->quirks2 &
890 SDHCI_QUIRK2_CLEAR_TRANSFERMODE_REG_BEFORE_CMD) {
891 sdhci_writew(host, 0x0, SDHCI_TRANSFER_MODE);
892 } else {
Dong Aisheng2b558c12013-10-30 22:09:48 +0800893 /* clear Auto CMD settings for no data CMDs */
Vincent Wan9b8ffea2014-11-05 14:09:00 +0800894 mode = sdhci_readw(host, SDHCI_TRANSFER_MODE);
895 sdhci_writew(host, mode & ~(SDHCI_TRNS_AUTO_CMD12 |
Dong Aisheng2b558c12013-10-30 22:09:48 +0800896 SDHCI_TRNS_AUTO_CMD23), SDHCI_TRANSFER_MODE);
Vincent Wan9b8ffea2014-11-05 14:09:00 +0800897 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700898 return;
Dong Aisheng2b558c12013-10-30 22:09:48 +0800899 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700900
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200901 WARN_ON(!host->data);
902
Vincent Yangd3fc5d72015-01-20 16:05:17 +0800903 if (!(host->quirks2 & SDHCI_QUIRK2_SUPPORT_SINGLE))
904 mode = SDHCI_TRNS_BLK_CNT_EN;
905
Andrei Warkentine89d4562011-05-23 15:06:37 -0500906 if (mmc_op_multi(cmd->opcode) || data->blocks > 1) {
Vincent Yangd3fc5d72015-01-20 16:05:17 +0800907 mode = SDHCI_TRNS_BLK_CNT_EN | SDHCI_TRNS_MULTI;
Andrei Warkentine89d4562011-05-23 15:06:37 -0500908 /*
909 * If we are sending CMD23, CMD12 never gets sent
910 * on successful completion (so no Auto-CMD12).
911 */
Corneliu Doban85cc1c32015-02-09 16:06:29 -0800912 if (!host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD12) &&
913 (cmd->opcode != SD_IO_RW_EXTENDED))
Andrei Warkentine89d4562011-05-23 15:06:37 -0500914 mode |= SDHCI_TRNS_AUTO_CMD12;
Andrei Warkentin8edf63712011-05-23 15:06:39 -0500915 else if (host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD23)) {
916 mode |= SDHCI_TRNS_AUTO_CMD23;
917 sdhci_writel(host, host->mrq->sbc->arg, SDHCI_ARGUMENT2);
918 }
Jerry Huangc4512f72010-08-10 18:01:59 -0700919 }
Andrei Warkentin8edf63712011-05-23 15:06:39 -0500920
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700921 if (data->flags & MMC_DATA_READ)
922 mode |= SDHCI_TRNS_READ;
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100923 if (host->flags & SDHCI_REQ_USE_DMA)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700924 mode |= SDHCI_TRNS_DMA;
925
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300926 sdhci_writew(host, mode, SDHCI_TRANSFER_MODE);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800927}
928
929static void sdhci_finish_data(struct sdhci_host *host)
930{
931 struct mmc_data *data;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800932
933 BUG_ON(!host->data);
934
935 data = host->data;
936 host->data = NULL;
937
Russell Kingadd89132016-01-26 13:40:42 +0000938 if ((host->flags & (SDHCI_REQ_USE_DMA | SDHCI_USE_ADMA)) ==
939 (SDHCI_REQ_USE_DMA | SDHCI_USE_ADMA))
940 sdhci_adma_table_post(host, data);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800941
942 /*
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200943 * The specification states that the block count register must
944 * be updated, but it does not specify at what point in the
945 * data flow. That makes the register entirely useless to read
946 * back so we have to assume that nothing made it to the card
947 * in the event of an error.
Pierre Ossmand129bce2006-03-24 03:18:17 -0800948 */
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200949 if (data->error)
950 data->bytes_xfered = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800951 else
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200952 data->bytes_xfered = data->blksz * data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800953
Andrei Warkentine89d4562011-05-23 15:06:37 -0500954 /*
955 * Need to send CMD12 if -
956 * a) open-ended multiblock transfer (no CMD23)
957 * b) error in multiblock transfer
958 */
959 if (data->stop &&
960 (data->error ||
961 !host->mrq->sbc)) {
962
Pierre Ossmand129bce2006-03-24 03:18:17 -0800963 /*
964 * The controller needs a reset of internal state machines
965 * upon error conditions.
966 */
Pierre Ossman17b04292007-07-22 22:18:46 +0200967 if (data->error) {
Russell King03231f92014-04-25 12:57:12 +0100968 sdhci_do_reset(host, SDHCI_RESET_CMD);
969 sdhci_do_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800970 }
971
972 sdhci_send_command(host, data->stop);
973 } else
974 tasklet_schedule(&host->finish_tasklet);
975}
976
Dong Aishengc0e551292013-09-13 19:11:31 +0800977void sdhci_send_command(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800978{
979 int flags;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -0700980 u32 mask;
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700981 unsigned long timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800982
983 WARN_ON(host->cmd);
984
Russell King96776202016-01-26 13:39:34 +0000985 /* Initially, a command has no error */
986 cmd->error = 0;
987
Pierre Ossmand129bce2006-03-24 03:18:17 -0800988 /* Wait max 10 ms */
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700989 timeout = 10;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -0700990
991 mask = SDHCI_CMD_INHIBIT;
992 if ((cmd->data != NULL) || (cmd->flags & MMC_RSP_BUSY))
993 mask |= SDHCI_DATA_INHIBIT;
994
995 /* We shouldn't wait for data inihibit for stop commands, even
996 though they might use busy signaling */
997 if (host->mrq->data && (cmd == host->mrq->data->stop))
998 mask &= ~SDHCI_DATA_INHIBIT;
999
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001000 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001001 if (timeout == 0) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01001002 pr_err("%s: Controller never released inhibit bit(s).\n",
1003 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001004 sdhci_dumpregs(host);
Pierre Ossman17b04292007-07-22 22:18:46 +02001005 cmd->error = -EIO;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001006 tasklet_schedule(&host->finish_tasklet);
1007 return;
1008 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001009 timeout--;
1010 mdelay(1);
1011 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001012
Adrian Hunter3e1a6892013-11-14 10:16:20 +02001013 timeout = jiffies;
Ulf Hansson1d4d7742014-01-08 15:06:08 +01001014 if (!cmd->data && cmd->busy_timeout > 9000)
1015 timeout += DIV_ROUND_UP(cmd->busy_timeout, 1000) * HZ + HZ;
Adrian Hunter3e1a6892013-11-14 10:16:20 +02001016 else
1017 timeout += 10 * HZ;
1018 mod_timer(&host->timer, timeout);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001019
1020 host->cmd = cmd;
Chanho Mine99783a2014-08-30 12:40:40 +09001021 host->busy_handle = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001022
Andrei Warkentina3c77782011-04-11 16:13:42 -05001023 sdhci_prepare_data(host, cmd);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001024
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001025 sdhci_writel(host, cmd->arg, SDHCI_ARGUMENT);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001026
Andrei Warkentine89d4562011-05-23 15:06:37 -05001027 sdhci_set_transfer_mode(host, cmd);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -07001028
Pierre Ossmand129bce2006-03-24 03:18:17 -08001029 if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301030 pr_err("%s: Unsupported response type!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -08001031 mmc_hostname(host->mmc));
Pierre Ossman17b04292007-07-22 22:18:46 +02001032 cmd->error = -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001033 tasklet_schedule(&host->finish_tasklet);
1034 return;
1035 }
1036
1037 if (!(cmd->flags & MMC_RSP_PRESENT))
1038 flags = SDHCI_CMD_RESP_NONE;
1039 else if (cmd->flags & MMC_RSP_136)
1040 flags = SDHCI_CMD_RESP_LONG;
1041 else if (cmd->flags & MMC_RSP_BUSY)
1042 flags = SDHCI_CMD_RESP_SHORT_BUSY;
1043 else
1044 flags = SDHCI_CMD_RESP_SHORT;
1045
1046 if (cmd->flags & MMC_RSP_CRC)
1047 flags |= SDHCI_CMD_CRC;
1048 if (cmd->flags & MMC_RSP_OPCODE)
1049 flags |= SDHCI_CMD_INDEX;
Arindam Nathb513ea22011-05-05 12:19:04 +05301050
1051 /* CMD19 is special in that the Data Present Select should be set */
Girish K S069c9f12012-01-06 09:56:39 +05301052 if (cmd->data || cmd->opcode == MMC_SEND_TUNING_BLOCK ||
1053 cmd->opcode == MMC_SEND_TUNING_BLOCK_HS200)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001054 flags |= SDHCI_CMD_DATA;
1055
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001056 sdhci_writew(host, SDHCI_MAKE_CMD(cmd->opcode, flags), SDHCI_COMMAND);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001057}
Dong Aishengc0e551292013-09-13 19:11:31 +08001058EXPORT_SYMBOL_GPL(sdhci_send_command);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001059
1060static void sdhci_finish_command(struct sdhci_host *host)
1061{
1062 int i;
1063
1064 BUG_ON(host->cmd == NULL);
1065
1066 if (host->cmd->flags & MMC_RSP_PRESENT) {
1067 if (host->cmd->flags & MMC_RSP_136) {
1068 /* CRC is stripped so we need to do some shifting. */
1069 for (i = 0;i < 4;i++) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001070 host->cmd->resp[i] = sdhci_readl(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001071 SDHCI_RESPONSE + (3-i)*4) << 8;
1072 if (i != 3)
1073 host->cmd->resp[i] |=
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001074 sdhci_readb(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001075 SDHCI_RESPONSE + (3-i)*4-1);
1076 }
1077 } else {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001078 host->cmd->resp[0] = sdhci_readl(host, SDHCI_RESPONSE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001079 }
1080 }
1081
Andrei Warkentine89d4562011-05-23 15:06:37 -05001082 /* Finished CMD23, now send actual command. */
1083 if (host->cmd == host->mrq->sbc) {
1084 host->cmd = NULL;
1085 sdhci_send_command(host, host->mrq->cmd);
1086 } else {
Pierre Ossmane538fbe2007-08-12 16:46:32 +02001087
Andrei Warkentine89d4562011-05-23 15:06:37 -05001088 /* Processed actual command. */
1089 if (host->data && host->data_early)
1090 sdhci_finish_data(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001091
Andrei Warkentine89d4562011-05-23 15:06:37 -05001092 if (!host->cmd->data)
1093 tasklet_schedule(&host->finish_tasklet);
1094
1095 host->cmd = NULL;
1096 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001097}
1098
Kevin Liu52983382013-01-31 11:31:37 +08001099static u16 sdhci_get_preset_value(struct sdhci_host *host)
1100{
Russell Kingd975f122014-04-25 12:59:31 +01001101 u16 preset = 0;
Kevin Liu52983382013-01-31 11:31:37 +08001102
Russell Kingd975f122014-04-25 12:59:31 +01001103 switch (host->timing) {
1104 case MMC_TIMING_UHS_SDR12:
Kevin Liu52983382013-01-31 11:31:37 +08001105 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR12);
1106 break;
Russell Kingd975f122014-04-25 12:59:31 +01001107 case MMC_TIMING_UHS_SDR25:
Kevin Liu52983382013-01-31 11:31:37 +08001108 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR25);
1109 break;
Russell Kingd975f122014-04-25 12:59:31 +01001110 case MMC_TIMING_UHS_SDR50:
Kevin Liu52983382013-01-31 11:31:37 +08001111 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR50);
1112 break;
Russell Kingd975f122014-04-25 12:59:31 +01001113 case MMC_TIMING_UHS_SDR104:
1114 case MMC_TIMING_MMC_HS200:
Kevin Liu52983382013-01-31 11:31:37 +08001115 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR104);
1116 break;
Russell Kingd975f122014-04-25 12:59:31 +01001117 case MMC_TIMING_UHS_DDR50:
Jisheng Zhang0dafa602015-08-18 16:21:39 +08001118 case MMC_TIMING_MMC_DDR52:
Kevin Liu52983382013-01-31 11:31:37 +08001119 preset = sdhci_readw(host, SDHCI_PRESET_FOR_DDR50);
1120 break;
Adrian Huntere9fb05d2014-11-06 15:19:06 +02001121 case MMC_TIMING_MMC_HS400:
1122 preset = sdhci_readw(host, SDHCI_PRESET_FOR_HS400);
1123 break;
Kevin Liu52983382013-01-31 11:31:37 +08001124 default:
1125 pr_warn("%s: Invalid UHS-I mode selected\n",
1126 mmc_hostname(host->mmc));
1127 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR12);
1128 break;
1129 }
1130 return preset;
1131}
1132
Ludovic Desrochesfb9ee042016-04-07 11:13:08 +02001133u16 sdhci_calc_clk(struct sdhci_host *host, unsigned int clock,
1134 unsigned int *actual_clock)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001135{
Arindam Nathc3ed3872011-05-05 12:19:06 +05301136 int div = 0; /* Initialized for compiler warning */
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001137 int real_div = div, clk_mul = 1;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301138 u16 clk = 0;
ludovic.desroches@atmel.com54971592015-07-29 16:22:46 +02001139 bool switch_base_clk = false;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001140
Zhangfei Gao85105c52010-08-06 07:10:01 +08001141 if (host->version >= SDHCI_SPEC_300) {
Russell Kingda91a8f2014-04-25 13:00:12 +01001142 if (host->preset_enabled) {
Kevin Liu52983382013-01-31 11:31:37 +08001143 u16 pre_val;
1144
1145 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1146 pre_val = sdhci_get_preset_value(host);
1147 div = (pre_val & SDHCI_PRESET_SDCLK_FREQ_MASK)
1148 >> SDHCI_PRESET_SDCLK_FREQ_SHIFT;
1149 if (host->clk_mul &&
1150 (pre_val & SDHCI_PRESET_CLKGEN_SEL_MASK)) {
1151 clk = SDHCI_PROG_CLOCK_MODE;
1152 real_div = div + 1;
1153 clk_mul = host->clk_mul;
1154 } else {
1155 real_div = max_t(int, 1, div << 1);
1156 }
1157 goto clock_set;
1158 }
1159
Arindam Nathc3ed3872011-05-05 12:19:06 +05301160 /*
1161 * Check if the Host Controller supports Programmable Clock
1162 * Mode.
1163 */
1164 if (host->clk_mul) {
Kevin Liu52983382013-01-31 11:31:37 +08001165 for (div = 1; div <= 1024; div++) {
1166 if ((host->max_clk * host->clk_mul / div)
1167 <= clock)
1168 break;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001169 }
ludovic.desroches@atmel.com54971592015-07-29 16:22:46 +02001170 if ((host->max_clk * host->clk_mul / div) <= clock) {
1171 /*
1172 * Set Programmable Clock Mode in the Clock
1173 * Control register.
1174 */
1175 clk = SDHCI_PROG_CLOCK_MODE;
1176 real_div = div;
1177 clk_mul = host->clk_mul;
1178 div--;
1179 } else {
1180 /*
1181 * Divisor can be too small to reach clock
1182 * speed requirement. Then use the base clock.
1183 */
1184 switch_base_clk = true;
1185 }
1186 }
1187
1188 if (!host->clk_mul || switch_base_clk) {
Arindam Nathc3ed3872011-05-05 12:19:06 +05301189 /* Version 3.00 divisors must be a multiple of 2. */
1190 if (host->max_clk <= clock)
1191 div = 1;
1192 else {
1193 for (div = 2; div < SDHCI_MAX_DIV_SPEC_300;
1194 div += 2) {
1195 if ((host->max_clk / div) <= clock)
1196 break;
1197 }
1198 }
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001199 real_div = div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301200 div >>= 1;
Suneel Garapatid1955c32015-06-09 13:01:50 +05301201 if ((host->quirks2 & SDHCI_QUIRK2_CLOCK_DIV_ZERO_BROKEN)
1202 && !div && host->max_clk <= 25000000)
1203 div = 1;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001204 }
1205 } else {
1206 /* Version 2.00 divisors must be a power of 2. */
Zhangfei Gao03975262010-09-20 15:15:18 -04001207 for (div = 1; div < SDHCI_MAX_DIV_SPEC_200; div *= 2) {
Zhangfei Gao85105c52010-08-06 07:10:01 +08001208 if ((host->max_clk / div) <= clock)
1209 break;
1210 }
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001211 real_div = div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301212 div >>= 1;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001213 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001214
Kevin Liu52983382013-01-31 11:31:37 +08001215clock_set:
Aisheng Dong03d6f5f2014-08-27 15:26:32 +08001216 if (real_div)
Ludovic Desrochesfb9ee042016-04-07 11:13:08 +02001217 *actual_clock = (host->max_clk * clk_mul) / real_div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301218 clk |= (div & SDHCI_DIV_MASK) << SDHCI_DIVIDER_SHIFT;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001219 clk |= ((div & SDHCI_DIV_HI_MASK) >> SDHCI_DIV_MASK_LEN)
1220 << SDHCI_DIVIDER_HI_SHIFT;
Ludovic Desrochesfb9ee042016-04-07 11:13:08 +02001221
1222 return clk;
1223}
1224EXPORT_SYMBOL_GPL(sdhci_calc_clk);
1225
1226void sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
1227{
1228 u16 clk;
1229 unsigned long timeout;
1230
1231 host->mmc->actual_clock = 0;
1232
1233 sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
Ludovic Desrochesfb9ee042016-04-07 11:13:08 +02001234
1235 if (clock == 0)
1236 return;
1237
1238 clk = sdhci_calc_clk(host, clock, &host->mmc->actual_clock);
1239
Pierre Ossmand129bce2006-03-24 03:18:17 -08001240 clk |= SDHCI_CLOCK_INT_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001241 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001242
Chris Ball27f6cb12009-09-22 16:45:31 -07001243 /* Wait max 20 ms */
1244 timeout = 20;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001245 while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001246 & SDHCI_CLOCK_INT_STABLE)) {
1247 if (timeout == 0) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01001248 pr_err("%s: Internal clock never stabilised.\n",
1249 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001250 sdhci_dumpregs(host);
1251 return;
1252 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001253 timeout--;
1254 mdelay(1);
1255 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001256
1257 clk |= SDHCI_CLOCK_CARD_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001258 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001259}
Russell King17710592014-04-25 12:58:55 +01001260EXPORT_SYMBOL_GPL(sdhci_set_clock);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001261
Adrian Hunter1dceb042016-03-29 12:45:43 +03001262static void sdhci_set_power_reg(struct sdhci_host *host, unsigned char mode,
1263 unsigned short vdd)
Pierre Ossman146ad662006-06-30 02:22:23 -07001264{
Tim Kryger3a48edc2014-06-13 10:13:56 -07001265 struct mmc_host *mmc = host->mmc;
Adrian Hunter1dceb042016-03-29 12:45:43 +03001266
1267 spin_unlock_irq(&host->lock);
1268 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, vdd);
1269 spin_lock_irq(&host->lock);
1270
1271 if (mode != MMC_POWER_OFF)
1272 sdhci_writeb(host, SDHCI_POWER_ON, SDHCI_POWER_CONTROL);
1273 else
1274 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
1275}
1276
1277void sdhci_set_power(struct sdhci_host *host, unsigned char mode,
1278 unsigned short vdd)
1279{
Giuseppe Cavallaro83642482010-09-28 10:41:28 +02001280 u8 pwr = 0;
Pierre Ossman146ad662006-06-30 02:22:23 -07001281
Russell King24fbb3c2014-04-25 13:00:06 +01001282 if (mode != MMC_POWER_OFF) {
1283 switch (1 << vdd) {
Pierre Ossmanae628902009-05-03 20:45:03 +02001284 case MMC_VDD_165_195:
1285 pwr = SDHCI_POWER_180;
1286 break;
1287 case MMC_VDD_29_30:
1288 case MMC_VDD_30_31:
1289 pwr = SDHCI_POWER_300;
1290 break;
1291 case MMC_VDD_32_33:
1292 case MMC_VDD_33_34:
1293 pwr = SDHCI_POWER_330;
1294 break;
1295 default:
Adrian Hunter9d5de932015-11-26 14:00:46 +02001296 WARN(1, "%s: Invalid vdd %#x\n",
1297 mmc_hostname(host->mmc), vdd);
1298 break;
Pierre Ossmanae628902009-05-03 20:45:03 +02001299 }
1300 }
1301
1302 if (host->pwr == pwr)
Russell Kinge921a8b2014-04-25 13:00:01 +01001303 return;
Pierre Ossman146ad662006-06-30 02:22:23 -07001304
Pierre Ossmanae628902009-05-03 20:45:03 +02001305 host->pwr = pwr;
1306
1307 if (pwr == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001308 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Adrian Hunterf0710a52013-05-06 12:17:32 +03001309 if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
1310 sdhci_runtime_pm_bus_off(host);
Russell Kinge921a8b2014-04-25 13:00:01 +01001311 } else {
1312 /*
1313 * Spec says that we should clear the power reg before setting
1314 * a new value. Some controllers don't seem to like this though.
1315 */
1316 if (!(host->quirks & SDHCI_QUIRK_SINGLE_POWER_WRITE))
1317 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Darren Salt9e9dc5f2007-01-27 15:32:31 +01001318
Russell Kinge921a8b2014-04-25 13:00:01 +01001319 /*
1320 * At least the Marvell CaFe chip gets confused if we set the
1321 * voltage and set turn on power at the same time, so set the
1322 * voltage first.
1323 */
1324 if (host->quirks & SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER)
1325 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
Pierre Ossman146ad662006-06-30 02:22:23 -07001326
Russell Kinge921a8b2014-04-25 13:00:01 +01001327 pwr |= SDHCI_POWER_ON;
1328
Pierre Ossmanae628902009-05-03 20:45:03 +02001329 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
1330
Russell Kinge921a8b2014-04-25 13:00:01 +01001331 if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
1332 sdhci_runtime_pm_bus_on(host);
Andres Salomone08c1692008-07-04 10:00:03 -07001333
Russell Kinge921a8b2014-04-25 13:00:01 +01001334 /*
1335 * Some controllers need an extra 10ms delay of 10ms before
1336 * they can apply clock after applying power
1337 */
1338 if (host->quirks & SDHCI_QUIRK_DELAY_AFTER_POWER)
1339 mdelay(10);
1340 }
Adrian Hunter1dceb042016-03-29 12:45:43 +03001341}
1342EXPORT_SYMBOL_GPL(sdhci_set_power);
Jisheng Zhang918f4cb2015-12-11 21:36:29 +08001343
Adrian Hunter1dceb042016-03-29 12:45:43 +03001344static void __sdhci_set_power(struct sdhci_host *host, unsigned char mode,
1345 unsigned short vdd)
1346{
1347 struct mmc_host *mmc = host->mmc;
1348
1349 if (host->ops->set_power)
1350 host->ops->set_power(host, mode, vdd);
1351 else if (!IS_ERR(mmc->supply.vmmc))
1352 sdhci_set_power_reg(host, mode, vdd);
1353 else
1354 sdhci_set_power(host, mode, vdd);
Pierre Ossman146ad662006-06-30 02:22:23 -07001355}
1356
Pierre Ossmand129bce2006-03-24 03:18:17 -08001357/*****************************************************************************\
1358 * *
1359 * MMC callbacks *
1360 * *
1361\*****************************************************************************/
1362
1363static void sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1364{
1365 struct sdhci_host *host;
Shawn Guo505a8682012-12-11 15:23:42 +08001366 int present;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001367 unsigned long flags;
1368
1369 host = mmc_priv(mmc);
1370
Scott Branden04e079c2015-03-10 11:35:10 -07001371 /* Firstly check card presence */
Adrian Hunter8d28b7a2016-02-09 16:12:36 +02001372 present = mmc->ops->get_cd(mmc);
Krzysztof Kozlowski28367662015-01-05 10:50:15 +01001373
Pierre Ossmand129bce2006-03-24 03:18:17 -08001374 spin_lock_irqsave(&host->lock, flags);
1375
1376 WARN_ON(host->mrq != NULL);
1377
Adrian Hunter061d17a2016-04-12 14:25:09 +03001378 sdhci_led_activate(host);
Andrei Warkentine89d4562011-05-23 15:06:37 -05001379
1380 /*
1381 * Ensure we don't send the STOP for non-SET_BLOCK_COUNTED
1382 * requests if Auto-CMD12 is enabled.
1383 */
1384 if (!mrq->sbc && (host->flags & SDHCI_AUTO_CMD12)) {
Jerry Huangc4512f72010-08-10 18:01:59 -07001385 if (mrq->stop) {
1386 mrq->data->stop = NULL;
1387 mrq->stop = NULL;
1388 }
1389 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001390
1391 host->mrq = mrq;
1392
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001393 if (!present || host->flags & SDHCI_DEVICE_DEAD) {
Pierre Ossman17b04292007-07-22 22:18:46 +02001394 host->mrq->cmd->error = -ENOMEDIUM;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001395 tasklet_schedule(&host->finish_tasklet);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301396 } else {
Andrei Warkentin8edf63712011-05-23 15:06:39 -05001397 if (mrq->sbc && !(host->flags & SDHCI_AUTO_CMD23))
Andrei Warkentine89d4562011-05-23 15:06:37 -05001398 sdhci_send_command(host, mrq->sbc);
1399 else
1400 sdhci_send_command(host, mrq->cmd);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301401 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001402
Pierre Ossman5f25a662006-10-04 02:15:39 -07001403 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001404 spin_unlock_irqrestore(&host->lock, flags);
1405}
1406
Russell King2317f562014-04-25 12:57:07 +01001407void sdhci_set_bus_width(struct sdhci_host *host, int width)
1408{
1409 u8 ctrl;
1410
1411 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
1412 if (width == MMC_BUS_WIDTH_8) {
1413 ctrl &= ~SDHCI_CTRL_4BITBUS;
1414 if (host->version >= SDHCI_SPEC_300)
1415 ctrl |= SDHCI_CTRL_8BITBUS;
1416 } else {
1417 if (host->version >= SDHCI_SPEC_300)
1418 ctrl &= ~SDHCI_CTRL_8BITBUS;
1419 if (width == MMC_BUS_WIDTH_4)
1420 ctrl |= SDHCI_CTRL_4BITBUS;
1421 else
1422 ctrl &= ~SDHCI_CTRL_4BITBUS;
1423 }
1424 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1425}
1426EXPORT_SYMBOL_GPL(sdhci_set_bus_width);
1427
Russell King96d7b782014-04-25 12:59:26 +01001428void sdhci_set_uhs_signaling(struct sdhci_host *host, unsigned timing)
1429{
1430 u16 ctrl_2;
1431
1432 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1433 /* Select Bus Speed Mode for host */
1434 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
1435 if ((timing == MMC_TIMING_MMC_HS200) ||
1436 (timing == MMC_TIMING_UHS_SDR104))
1437 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
1438 else if (timing == MMC_TIMING_UHS_SDR12)
1439 ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
1440 else if (timing == MMC_TIMING_UHS_SDR25)
1441 ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
1442 else if (timing == MMC_TIMING_UHS_SDR50)
1443 ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
1444 else if ((timing == MMC_TIMING_UHS_DDR50) ||
1445 (timing == MMC_TIMING_MMC_DDR52))
1446 ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
Adrian Huntere9fb05d2014-11-06 15:19:06 +02001447 else if (timing == MMC_TIMING_MMC_HS400)
1448 ctrl_2 |= SDHCI_CTRL_HS400; /* Non-standard */
Russell King96d7b782014-04-25 12:59:26 +01001449 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
1450}
1451EXPORT_SYMBOL_GPL(sdhci_set_uhs_signaling);
1452
Dong Aishengded97e02016-04-16 01:29:25 +08001453static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001454{
Dong Aishengded97e02016-04-16 01:29:25 +08001455 struct sdhci_host *host = mmc_priv(mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001456 unsigned long flags;
1457 u8 ctrl;
1458
Pierre Ossmand129bce2006-03-24 03:18:17 -08001459 spin_lock_irqsave(&host->lock, flags);
1460
Adrian Hunterceb61432011-12-27 15:48:41 +02001461 if (host->flags & SDHCI_DEVICE_DEAD) {
1462 spin_unlock_irqrestore(&host->lock, flags);
Tim Kryger3a48edc2014-06-13 10:13:56 -07001463 if (!IS_ERR(mmc->supply.vmmc) &&
1464 ios->power_mode == MMC_POWER_OFF)
Markus Mayer4e743f12014-07-03 13:27:42 -07001465 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0);
Adrian Hunterceb61432011-12-27 15:48:41 +02001466 return;
1467 }
Pierre Ossman1e728592008-04-16 19:13:13 +02001468
Pierre Ossmand129bce2006-03-24 03:18:17 -08001469 /*
1470 * Reset the chip on each power off.
1471 * Should clear out any weird states.
1472 */
1473 if (ios->power_mode == MMC_POWER_OFF) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001474 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001475 sdhci_reinit(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001476 }
1477
Kevin Liu52983382013-01-31 11:31:37 +08001478 if (host->version >= SDHCI_SPEC_300 &&
Dong Aisheng372c4632013-10-18 19:48:50 +08001479 (ios->power_mode == MMC_POWER_UP) &&
1480 !(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN))
Kevin Liu52983382013-01-31 11:31:37 +08001481 sdhci_enable_preset_value(host, false);
1482
Russell King373073e2014-04-25 12:58:45 +01001483 if (!ios->clock || ios->clock != host->clock) {
Russell King17710592014-04-25 12:58:55 +01001484 host->ops->set_clock(host, ios->clock);
Russell King373073e2014-04-25 12:58:45 +01001485 host->clock = ios->clock;
Aisheng Dong03d6f5f2014-08-27 15:26:32 +08001486
1487 if (host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK &&
1488 host->clock) {
1489 host->timeout_clk = host->mmc->actual_clock ?
1490 host->mmc->actual_clock / 1000 :
1491 host->clock / 1000;
1492 host->mmc->max_busy_timeout =
1493 host->ops->get_max_timeout_count ?
1494 host->ops->get_max_timeout_count(host) :
1495 1 << 27;
1496 host->mmc->max_busy_timeout /= host->timeout_clk;
1497 }
Russell King373073e2014-04-25 12:58:45 +01001498 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001499
Adrian Hunter1dceb042016-03-29 12:45:43 +03001500 __sdhci_set_power(host, ios->power_mode, ios->vdd);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001501
Philip Rakity643a81f2010-09-23 08:24:32 -07001502 if (host->ops->platform_send_init_74_clocks)
1503 host->ops->platform_send_init_74_clocks(host, ios->power_mode);
1504
Russell King2317f562014-04-25 12:57:07 +01001505 host->ops->set_bus_width(host, ios->bus_width);
Philip Rakity15ec4462010-11-19 16:48:39 -05001506
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001507 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001508
Philip Rakity3ab9c8d2010-10-06 11:57:23 -07001509 if ((ios->timing == MMC_TIMING_SD_HS ||
1510 ios->timing == MMC_TIMING_MMC_HS)
1511 && !(host->quirks & SDHCI_QUIRK_NO_HISPD_BIT))
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001512 ctrl |= SDHCI_CTRL_HISPD;
1513 else
1514 ctrl &= ~SDHCI_CTRL_HISPD;
1515
Arindam Nathd6d50a12011-05-05 12:18:59 +05301516 if (host->version >= SDHCI_SPEC_300) {
Arindam Nath49c468f2011-05-05 12:19:01 +05301517 u16 clk, ctrl_2;
Arindam Nath49c468f2011-05-05 12:19:01 +05301518
1519 /* In case of UHS-I modes, set High Speed Enable */
Adrian Huntere9fb05d2014-11-06 15:19:06 +02001520 if ((ios->timing == MMC_TIMING_MMC_HS400) ||
1521 (ios->timing == MMC_TIMING_MMC_HS200) ||
Seungwon Jeonbb8175a2014-03-14 21:12:48 +09001522 (ios->timing == MMC_TIMING_MMC_DDR52) ||
Girish K S069c9f12012-01-06 09:56:39 +05301523 (ios->timing == MMC_TIMING_UHS_SDR50) ||
Arindam Nath49c468f2011-05-05 12:19:01 +05301524 (ios->timing == MMC_TIMING_UHS_SDR104) ||
1525 (ios->timing == MMC_TIMING_UHS_DDR50) ||
Alexander Elbsdd8df172012-01-03 23:26:53 -05001526 (ios->timing == MMC_TIMING_UHS_SDR25))
Arindam Nath49c468f2011-05-05 12:19:01 +05301527 ctrl |= SDHCI_CTRL_HISPD;
Arindam Nathd6d50a12011-05-05 12:18:59 +05301528
Russell Kingda91a8f2014-04-25 13:00:12 +01001529 if (!host->preset_enabled) {
Arindam Nath758535c2011-05-05 12:19:00 +05301530 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301531 /*
1532 * We only need to set Driver Strength if the
1533 * preset value enable is not set.
1534 */
Russell Kingda91a8f2014-04-25 13:00:12 +01001535 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301536 ctrl_2 &= ~SDHCI_CTRL_DRV_TYPE_MASK;
1537 if (ios->drv_type == MMC_SET_DRIVER_TYPE_A)
1538 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_A;
Petri Gynther43e943a2015-05-20 14:35:00 -07001539 else if (ios->drv_type == MMC_SET_DRIVER_TYPE_B)
1540 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_B;
Arindam Nathd6d50a12011-05-05 12:18:59 +05301541 else if (ios->drv_type == MMC_SET_DRIVER_TYPE_C)
1542 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_C;
Petri Gynther43e943a2015-05-20 14:35:00 -07001543 else if (ios->drv_type == MMC_SET_DRIVER_TYPE_D)
1544 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_D;
1545 else {
Marek Vasut2e4456f2015-11-18 10:47:02 +01001546 pr_warn("%s: invalid driver type, default to driver type B\n",
1547 mmc_hostname(mmc));
Petri Gynther43e943a2015-05-20 14:35:00 -07001548 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_B;
1549 }
Arindam Nathd6d50a12011-05-05 12:18:59 +05301550
1551 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
Arindam Nath758535c2011-05-05 12:19:00 +05301552 } else {
1553 /*
1554 * According to SDHC Spec v3.00, if the Preset Value
1555 * Enable in the Host Control 2 register is set, we
1556 * need to reset SD Clock Enable before changing High
1557 * Speed Enable to avoid generating clock gliches.
1558 */
Arindam Nath758535c2011-05-05 12:19:00 +05301559
1560 /* Reset SD Clock Enable */
1561 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1562 clk &= ~SDHCI_CLOCK_CARD_EN;
1563 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1564
1565 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1566
1567 /* Re-enable SD Clock */
Russell King17710592014-04-25 12:58:55 +01001568 host->ops->set_clock(host, host->clock);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301569 }
Arindam Nath49c468f2011-05-05 12:19:01 +05301570
Arindam Nath49c468f2011-05-05 12:19:01 +05301571 /* Reset SD Clock Enable */
1572 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1573 clk &= ~SDHCI_CLOCK_CARD_EN;
1574 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1575
Russell King96d7b782014-04-25 12:59:26 +01001576 host->ops->set_uhs_signaling(host, ios->timing);
Russell Kingd975f122014-04-25 12:59:31 +01001577 host->timing = ios->timing;
Arindam Nath49c468f2011-05-05 12:19:01 +05301578
Kevin Liu52983382013-01-31 11:31:37 +08001579 if (!(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN) &&
1580 ((ios->timing == MMC_TIMING_UHS_SDR12) ||
1581 (ios->timing == MMC_TIMING_UHS_SDR25) ||
1582 (ios->timing == MMC_TIMING_UHS_SDR50) ||
1583 (ios->timing == MMC_TIMING_UHS_SDR104) ||
Jisheng Zhang0dafa602015-08-18 16:21:39 +08001584 (ios->timing == MMC_TIMING_UHS_DDR50) ||
1585 (ios->timing == MMC_TIMING_MMC_DDR52))) {
Kevin Liu52983382013-01-31 11:31:37 +08001586 u16 preset;
1587
1588 sdhci_enable_preset_value(host, true);
1589 preset = sdhci_get_preset_value(host);
1590 ios->drv_type = (preset & SDHCI_PRESET_DRV_MASK)
1591 >> SDHCI_PRESET_DRV_SHIFT;
1592 }
1593
Arindam Nath49c468f2011-05-05 12:19:01 +05301594 /* Re-enable SD Clock */
Russell King17710592014-04-25 12:58:55 +01001595 host->ops->set_clock(host, host->clock);
Arindam Nath758535c2011-05-05 12:19:00 +05301596 } else
1597 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301598
Leandro Dorileob8352262007-07-25 23:47:04 +02001599 /*
1600 * Some (ENE) controllers go apeshit on some ios operation,
1601 * signalling timeout and CRC errors even on CMD0. Resetting
1602 * it on each ios seems to solve the problem.
1603 */
Mohammad Jamalc63705e2015-01-13 20:47:24 +05301604 if (host->quirks & SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS)
Russell King03231f92014-04-25 12:57:12 +01001605 sdhci_do_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
Leandro Dorileob8352262007-07-25 23:47:04 +02001606
Pierre Ossman5f25a662006-10-04 02:15:39 -07001607 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001608 spin_unlock_irqrestore(&host->lock, flags);
1609}
1610
Dong Aishengded97e02016-04-16 01:29:25 +08001611static int sdhci_get_cd(struct mmc_host *mmc)
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001612{
1613 struct sdhci_host *host = mmc_priv(mmc);
Dong Aishengded97e02016-04-16 01:29:25 +08001614 int gpio_cd = mmc_gpio_get_cd(mmc);
Kevin Liu94144a42013-02-28 17:35:53 +08001615
1616 if (host->flags & SDHCI_DEVICE_DEAD)
1617 return 0;
1618
Ivan T. Ivanov88af5652015-07-06 15:16:19 +03001619 /* If nonremovable, assume that the card is always present. */
Jaehoon Chung860951c2016-06-21 10:13:26 +09001620 if (!mmc_card_is_removable(host->mmc))
Kevin Liu94144a42013-02-28 17:35:53 +08001621 return 1;
1622
Ivan T. Ivanov88af5652015-07-06 15:16:19 +03001623 /*
1624 * Try slot gpio detect, if defined it take precedence
1625 * over build in controller functionality
1626 */
Arnd Bergmann287980e2016-05-27 23:23:25 +02001627 if (gpio_cd >= 0)
Kevin Liu94144a42013-02-28 17:35:53 +08001628 return !!gpio_cd;
1629
Ivan T. Ivanov88af5652015-07-06 15:16:19 +03001630 /* If polling, assume that the card is always present. */
1631 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
1632 return 1;
1633
Kevin Liu94144a42013-02-28 17:35:53 +08001634 /* Host native card detect */
1635 return !!(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT);
1636}
1637
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001638static int sdhci_check_ro(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001639{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001640 unsigned long flags;
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001641 int is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001642
Pierre Ossmand129bce2006-03-24 03:18:17 -08001643 spin_lock_irqsave(&host->lock, flags);
1644
Pierre Ossman1e728592008-04-16 19:13:13 +02001645 if (host->flags & SDHCI_DEVICE_DEAD)
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001646 is_readonly = 0;
1647 else if (host->ops->get_ro)
1648 is_readonly = host->ops->get_ro(host);
Pierre Ossman1e728592008-04-16 19:13:13 +02001649 else
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001650 is_readonly = !(sdhci_readl(host, SDHCI_PRESENT_STATE)
1651 & SDHCI_WRITE_PROTECT);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001652
1653 spin_unlock_irqrestore(&host->lock, flags);
1654
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001655 /* This quirk needs to be replaced by a callback-function later */
1656 return host->quirks & SDHCI_QUIRK_INVERTED_WRITE_PROTECT ?
1657 !is_readonly : is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001658}
1659
Takashi Iwai82b0e232011-04-21 20:26:38 +02001660#define SAMPLE_COUNT 5
1661
Dong Aishengded97e02016-04-16 01:29:25 +08001662static int sdhci_get_ro(struct mmc_host *mmc)
Takashi Iwai82b0e232011-04-21 20:26:38 +02001663{
Dong Aishengded97e02016-04-16 01:29:25 +08001664 struct sdhci_host *host = mmc_priv(mmc);
Takashi Iwai82b0e232011-04-21 20:26:38 +02001665 int i, ro_count;
1666
Takashi Iwai82b0e232011-04-21 20:26:38 +02001667 if (!(host->quirks & SDHCI_QUIRK_UNSTABLE_RO_DETECT))
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001668 return sdhci_check_ro(host);
Takashi Iwai82b0e232011-04-21 20:26:38 +02001669
1670 ro_count = 0;
1671 for (i = 0; i < SAMPLE_COUNT; i++) {
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001672 if (sdhci_check_ro(host)) {
Takashi Iwai82b0e232011-04-21 20:26:38 +02001673 if (++ro_count > SAMPLE_COUNT / 2)
1674 return 1;
1675 }
1676 msleep(30);
1677 }
1678 return 0;
1679}
1680
Adrian Hunter20758b62011-08-29 16:42:12 +03001681static void sdhci_hw_reset(struct mmc_host *mmc)
1682{
1683 struct sdhci_host *host = mmc_priv(mmc);
1684
1685 if (host->ops && host->ops->hw_reset)
1686 host->ops->hw_reset(host);
1687}
1688
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001689static void sdhci_enable_sdio_irq_nolock(struct sdhci_host *host, int enable)
1690{
Russell Kingbe138552014-04-25 12:55:56 +01001691 if (!(host->flags & SDHCI_DEVICE_DEAD)) {
Russell Kingef104332014-04-25 12:55:41 +01001692 if (enable)
Russell Kingb537f942014-04-25 12:56:01 +01001693 host->ier |= SDHCI_INT_CARD_INT;
Russell Kingef104332014-04-25 12:55:41 +01001694 else
Russell Kingb537f942014-04-25 12:56:01 +01001695 host->ier &= ~SDHCI_INT_CARD_INT;
1696
1697 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
1698 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Russell Kingef104332014-04-25 12:55:41 +01001699 mmiowb();
1700 }
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001701}
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001702
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001703static void sdhci_enable_sdio_irq(struct mmc_host *mmc, int enable)
1704{
1705 struct sdhci_host *host = mmc_priv(mmc);
1706 unsigned long flags;
1707
1708 spin_lock_irqsave(&host->lock, flags);
Russell Kingef104332014-04-25 12:55:41 +01001709 if (enable)
1710 host->flags |= SDHCI_SDIO_IRQ_ENABLED;
1711 else
1712 host->flags &= ~SDHCI_SDIO_IRQ_ENABLED;
1713
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001714 sdhci_enable_sdio_irq_nolock(host, enable);
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001715 spin_unlock_irqrestore(&host->lock, flags);
1716}
1717
Dong Aishengded97e02016-04-16 01:29:25 +08001718static int sdhci_start_signal_voltage_switch(struct mmc_host *mmc,
1719 struct mmc_ios *ios)
Philip Rakity6231f3d2012-07-23 15:56:23 -07001720{
Dong Aishengded97e02016-04-16 01:29:25 +08001721 struct sdhci_host *host = mmc_priv(mmc);
Philip Rakity6231f3d2012-07-23 15:56:23 -07001722 u16 ctrl;
Kevin Liu20b92a32012-12-17 19:29:26 +08001723 int ret;
Philip Rakity6231f3d2012-07-23 15:56:23 -07001724
1725 /*
1726 * Signal Voltage Switching is only applicable for Host Controllers
1727 * v3.00 and above.
1728 */
1729 if (host->version < SDHCI_SPEC_300)
1730 return 0;
1731
Philip Rakity6231f3d2012-07-23 15:56:23 -07001732 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
Kevin Liu20b92a32012-12-17 19:29:26 +08001733
Fabio Estevam21f59982013-02-14 10:35:03 -02001734 switch (ios->signal_voltage) {
Kevin Liu20b92a32012-12-17 19:29:26 +08001735 case MMC_SIGNAL_VOLTAGE_330:
Adrian Hunter8cb851a2016-06-29 16:24:16 +03001736 if (!(host->flags & SDHCI_SIGNALING_330))
1737 return -EINVAL;
Kevin Liu20b92a32012-12-17 19:29:26 +08001738 /* Set 1.8V Signal Enable in the Host Control2 register to 0 */
1739 ctrl &= ~SDHCI_CTRL_VDD_180;
1740 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1741
Tim Kryger3a48edc2014-06-13 10:13:56 -07001742 if (!IS_ERR(mmc->supply.vqmmc)) {
1743 ret = regulator_set_voltage(mmc->supply.vqmmc, 2700000,
1744 3600000);
Kevin Liu20b92a32012-12-17 19:29:26 +08001745 if (ret) {
Joe Perches66061102014-09-12 14:56:56 -07001746 pr_warn("%s: Switching to 3.3V signalling voltage failed\n",
1747 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001748 return -EIO;
1749 }
1750 }
1751 /* Wait for 5ms */
1752 usleep_range(5000, 5500);
1753
1754 /* 3.3V regulator output should be stable within 5 ms */
1755 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1756 if (!(ctrl & SDHCI_CTRL_VDD_180))
1757 return 0;
1758
Joe Perches66061102014-09-12 14:56:56 -07001759 pr_warn("%s: 3.3V regulator output did not became stable\n",
1760 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001761
1762 return -EAGAIN;
1763 case MMC_SIGNAL_VOLTAGE_180:
Adrian Hunter8cb851a2016-06-29 16:24:16 +03001764 if (!(host->flags & SDHCI_SIGNALING_180))
1765 return -EINVAL;
Tim Kryger3a48edc2014-06-13 10:13:56 -07001766 if (!IS_ERR(mmc->supply.vqmmc)) {
1767 ret = regulator_set_voltage(mmc->supply.vqmmc,
Kevin Liu20b92a32012-12-17 19:29:26 +08001768 1700000, 1950000);
1769 if (ret) {
Joe Perches66061102014-09-12 14:56:56 -07001770 pr_warn("%s: Switching to 1.8V signalling voltage failed\n",
1771 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001772 return -EIO;
1773 }
1774 }
1775
1776 /*
1777 * Enable 1.8V Signal Enable in the Host Control2
1778 * register
1779 */
1780 ctrl |= SDHCI_CTRL_VDD_180;
1781 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1782
Vincent Yang9d967a62015-01-20 16:05:15 +08001783 /* Some controller need to do more when switching */
1784 if (host->ops->voltage_switch)
1785 host->ops->voltage_switch(host);
1786
Kevin Liu20b92a32012-12-17 19:29:26 +08001787 /* 1.8V regulator output should be stable within 5 ms */
1788 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1789 if (ctrl & SDHCI_CTRL_VDD_180)
1790 return 0;
1791
Joe Perches66061102014-09-12 14:56:56 -07001792 pr_warn("%s: 1.8V regulator output did not became stable\n",
1793 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001794
1795 return -EAGAIN;
1796 case MMC_SIGNAL_VOLTAGE_120:
Adrian Hunter8cb851a2016-06-29 16:24:16 +03001797 if (!(host->flags & SDHCI_SIGNALING_120))
1798 return -EINVAL;
Tim Kryger3a48edc2014-06-13 10:13:56 -07001799 if (!IS_ERR(mmc->supply.vqmmc)) {
1800 ret = regulator_set_voltage(mmc->supply.vqmmc, 1100000,
1801 1300000);
Kevin Liu20b92a32012-12-17 19:29:26 +08001802 if (ret) {
Joe Perches66061102014-09-12 14:56:56 -07001803 pr_warn("%s: Switching to 1.2V signalling voltage failed\n",
1804 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001805 return -EIO;
1806 }
1807 }
1808 return 0;
1809 default:
Arindam Nathf2119df2011-05-05 12:18:57 +05301810 /* No signal voltage switch required */
1811 return 0;
Kevin Liu20b92a32012-12-17 19:29:26 +08001812 }
Arindam Nathf2119df2011-05-05 12:18:57 +05301813}
1814
Kevin Liu20b92a32012-12-17 19:29:26 +08001815static int sdhci_card_busy(struct mmc_host *mmc)
1816{
1817 struct sdhci_host *host = mmc_priv(mmc);
1818 u32 present_state;
1819
Adrian Huntere613cc42016-06-23 14:00:58 +03001820 /* Check whether DAT[0] is 0 */
Kevin Liu20b92a32012-12-17 19:29:26 +08001821 present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
Kevin Liu20b92a32012-12-17 19:29:26 +08001822
Adrian Huntere613cc42016-06-23 14:00:58 +03001823 return !(present_state & SDHCI_DATA_0_LVL_MASK);
Kevin Liu20b92a32012-12-17 19:29:26 +08001824}
1825
Adrian Hunterb5540ce2014-12-05 19:25:31 +02001826static int sdhci_prepare_hs400_tuning(struct mmc_host *mmc, struct mmc_ios *ios)
1827{
1828 struct sdhci_host *host = mmc_priv(mmc);
1829 unsigned long flags;
1830
1831 spin_lock_irqsave(&host->lock, flags);
1832 host->flags |= SDHCI_HS400_TUNING;
1833 spin_unlock_irqrestore(&host->lock, flags);
1834
1835 return 0;
1836}
1837
Girish K S069c9f12012-01-06 09:56:39 +05301838static int sdhci_execute_tuning(struct mmc_host *mmc, u32 opcode)
Arindam Nathb513ea22011-05-05 12:19:04 +05301839{
Russell King4b6f37d2014-04-25 12:59:36 +01001840 struct sdhci_host *host = mmc_priv(mmc);
Arindam Nathb513ea22011-05-05 12:19:04 +05301841 u16 ctrl;
Arindam Nathb513ea22011-05-05 12:19:04 +05301842 int tuning_loop_counter = MAX_TUNING_LOOP;
Arindam Nathb513ea22011-05-05 12:19:04 +05301843 int err = 0;
Aisheng Dong2b35bd82013-12-23 19:13:04 +08001844 unsigned long flags;
Adrian Hunter38e40bf2014-12-05 19:25:30 +02001845 unsigned int tuning_count = 0;
Adrian Hunterb5540ce2014-12-05 19:25:31 +02001846 bool hs400_tuning;
Arindam Nathb513ea22011-05-05 12:19:04 +05301847
Aisheng Dong2b35bd82013-12-23 19:13:04 +08001848 spin_lock_irqsave(&host->lock, flags);
Arindam Nathb513ea22011-05-05 12:19:04 +05301849
Adrian Hunterb5540ce2014-12-05 19:25:31 +02001850 hs400_tuning = host->flags & SDHCI_HS400_TUNING;
1851 host->flags &= ~SDHCI_HS400_TUNING;
1852
Adrian Hunter38e40bf2014-12-05 19:25:30 +02001853 if (host->tuning_mode == SDHCI_TUNING_MODE_1)
1854 tuning_count = host->tuning_count;
1855
Arindam Nathb513ea22011-05-05 12:19:04 +05301856 /*
Weijun Yang9faac7b2015-10-04 12:04:12 +00001857 * The Host Controller needs tuning in case of SDR104 and DDR50
1858 * mode, and for SDR50 mode when Use Tuning for SDR50 is set in
1859 * the Capabilities register.
Girish K S069c9f12012-01-06 09:56:39 +05301860 * If the Host Controller supports the HS200 mode then the
1861 * tuning function has to be executed.
Arindam Nathb513ea22011-05-05 12:19:04 +05301862 */
Russell King4b6f37d2014-04-25 12:59:36 +01001863 switch (host->timing) {
Adrian Hunterb5540ce2014-12-05 19:25:31 +02001864 /* HS400 tuning is done in HS200 mode */
Adrian Huntere9fb05d2014-11-06 15:19:06 +02001865 case MMC_TIMING_MMC_HS400:
Adrian Hunterb5540ce2014-12-05 19:25:31 +02001866 err = -EINVAL;
1867 goto out_unlock;
1868
Russell King4b6f37d2014-04-25 12:59:36 +01001869 case MMC_TIMING_MMC_HS200:
Adrian Hunterb5540ce2014-12-05 19:25:31 +02001870 /*
1871 * Periodic re-tuning for HS400 is not expected to be needed, so
1872 * disable it here.
1873 */
1874 if (hs400_tuning)
1875 tuning_count = 0;
1876 break;
1877
Russell King4b6f37d2014-04-25 12:59:36 +01001878 case MMC_TIMING_UHS_SDR104:
Weijun Yang9faac7b2015-10-04 12:04:12 +00001879 case MMC_TIMING_UHS_DDR50:
Russell King4b6f37d2014-04-25 12:59:36 +01001880 break;
Girish K S069c9f12012-01-06 09:56:39 +05301881
Russell King4b6f37d2014-04-25 12:59:36 +01001882 case MMC_TIMING_UHS_SDR50:
Adrian Hunter4228b212016-04-20 09:24:03 +03001883 if (host->flags & SDHCI_SDR50_NEEDS_TUNING)
Russell King4b6f37d2014-04-25 12:59:36 +01001884 break;
1885 /* FALLTHROUGH */
1886
1887 default:
Adrian Hunterd519c862014-12-05 19:25:29 +02001888 goto out_unlock;
Arindam Nathb513ea22011-05-05 12:19:04 +05301889 }
1890
Dong Aisheng45251812013-09-13 19:11:30 +08001891 if (host->ops->platform_execute_tuning) {
Aisheng Dong2b35bd82013-12-23 19:13:04 +08001892 spin_unlock_irqrestore(&host->lock, flags);
Dong Aisheng45251812013-09-13 19:11:30 +08001893 err = host->ops->platform_execute_tuning(host, opcode);
Dong Aisheng45251812013-09-13 19:11:30 +08001894 return err;
1895 }
1896
Russell King4b6f37d2014-04-25 12:59:36 +01001897 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1898 ctrl |= SDHCI_CTRL_EXEC_TUNING;
Vincent Yang67d0d042015-01-20 16:05:16 +08001899 if (host->quirks2 & SDHCI_QUIRK2_TUNING_WORK_AROUND)
1900 ctrl |= SDHCI_CTRL_TUNED_CLK;
Arindam Nathb513ea22011-05-05 12:19:04 +05301901 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1902
1903 /*
1904 * As per the Host Controller spec v3.00, tuning command
1905 * generates Buffer Read Ready interrupt, so enable that.
1906 *
1907 * Note: The spec clearly says that when tuning sequence
1908 * is being performed, the controller does not generate
1909 * interrupts other than Buffer Read Ready interrupt. But
1910 * to make sure we don't hit a controller bug, we _only_
1911 * enable Buffer Read Ready interrupt here.
1912 */
Russell Kingb537f942014-04-25 12:56:01 +01001913 sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_INT_ENABLE);
1914 sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_SIGNAL_ENABLE);
Arindam Nathb513ea22011-05-05 12:19:04 +05301915
1916 /*
1917 * Issue CMD19 repeatedly till Execute Tuning is set to 0 or the number
Simon Horman1473bdd2016-05-13 13:24:31 +09001918 * of loops reaches 40 times.
Arindam Nathb513ea22011-05-05 12:19:04 +05301919 */
Arindam Nathb513ea22011-05-05 12:19:04 +05301920 do {
1921 struct mmc_command cmd = {0};
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001922 struct mmc_request mrq = {NULL};
Arindam Nathb513ea22011-05-05 12:19:04 +05301923
Girish K S069c9f12012-01-06 09:56:39 +05301924 cmd.opcode = opcode;
Arindam Nathb513ea22011-05-05 12:19:04 +05301925 cmd.arg = 0;
1926 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1927 cmd.retries = 0;
1928 cmd.data = NULL;
1929 cmd.error = 0;
1930
Al Cooper7ce45e92014-05-09 11:34:07 -04001931 if (tuning_loop_counter-- == 0)
1932 break;
1933
Arindam Nathb513ea22011-05-05 12:19:04 +05301934 mrq.cmd = &cmd;
1935 host->mrq = &mrq;
1936
1937 /*
1938 * In response to CMD19, the card sends 64 bytes of tuning
1939 * block to the Host Controller. So we set the block size
1940 * to 64 here.
1941 */
Girish K S069c9f12012-01-06 09:56:39 +05301942 if (cmd.opcode == MMC_SEND_TUNING_BLOCK_HS200) {
1943 if (mmc->ios.bus_width == MMC_BUS_WIDTH_8)
1944 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 128),
1945 SDHCI_BLOCK_SIZE);
1946 else if (mmc->ios.bus_width == MMC_BUS_WIDTH_4)
1947 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64),
1948 SDHCI_BLOCK_SIZE);
1949 } else {
1950 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64),
1951 SDHCI_BLOCK_SIZE);
1952 }
Arindam Nathb513ea22011-05-05 12:19:04 +05301953
1954 /*
1955 * The tuning block is sent by the card to the host controller.
1956 * So we set the TRNS_READ bit in the Transfer Mode register.
1957 * This also takes care of setting DMA Enable and Multi Block
1958 * Select in the same register to 0.
1959 */
1960 sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE);
1961
1962 sdhci_send_command(host, &cmd);
1963
1964 host->cmd = NULL;
1965 host->mrq = NULL;
1966
Aisheng Dong2b35bd82013-12-23 19:13:04 +08001967 spin_unlock_irqrestore(&host->lock, flags);
Arindam Nathb513ea22011-05-05 12:19:04 +05301968 /* Wait for Buffer Read Ready interrupt */
1969 wait_event_interruptible_timeout(host->buf_ready_int,
1970 (host->tuning_done == 1),
1971 msecs_to_jiffies(50));
Aisheng Dong2b35bd82013-12-23 19:13:04 +08001972 spin_lock_irqsave(&host->lock, flags);
Arindam Nathb513ea22011-05-05 12:19:04 +05301973
1974 if (!host->tuning_done) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01001975 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 +05301976 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1977 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
1978 ctrl &= ~SDHCI_CTRL_EXEC_TUNING;
1979 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1980
1981 err = -EIO;
1982 goto out;
1983 }
1984
1985 host->tuning_done = 0;
1986
1987 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
Nick Sanders197160d2014-05-06 18:52:38 -07001988
1989 /* eMMC spec does not require a delay between tuning cycles */
1990 if (opcode == MMC_SEND_TUNING_BLOCK)
1991 mdelay(1);
Arindam Nathb513ea22011-05-05 12:19:04 +05301992 } while (ctrl & SDHCI_CTRL_EXEC_TUNING);
1993
1994 /*
1995 * The Host Driver has exhausted the maximum number of loops allowed,
1996 * so use fixed sampling frequency.
1997 */
Al Cooper7ce45e92014-05-09 11:34:07 -04001998 if (tuning_loop_counter < 0) {
Arindam Nathb513ea22011-05-05 12:19:04 +05301999 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
2000 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Al Cooper7ce45e92014-05-09 11:34:07 -04002001 }
2002 if (!(ctrl & SDHCI_CTRL_TUNED_CLK)) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01002003 pr_info(DRIVER_NAME ": Tuning procedure failed, falling back to fixed sampling clock\n");
Dong Aisheng114f2bf2013-10-18 19:48:45 +08002004 err = -EIO;
Arindam Nathb513ea22011-05-05 12:19:04 +05302005 }
2006
2007out:
Adrian Hunter38e40bf2014-12-05 19:25:30 +02002008 if (tuning_count) {
Adrian Hunter66c39dfc2015-05-07 13:10:21 +03002009 /*
2010 * In case tuning fails, host controllers which support
2011 * re-tuning can try tuning again at a later time, when the
2012 * re-tuning timer expires. So for these controllers, we
2013 * return 0. Since there might be other controllers who do not
2014 * have this capability, we return error for them.
2015 */
2016 err = 0;
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302017 }
2018
Adrian Hunter66c39dfc2015-05-07 13:10:21 +03002019 host->mmc->retune_period = err ? 0 : tuning_count;
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302020
Russell Kingb537f942014-04-25 12:56:01 +01002021 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
2022 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Adrian Hunterd519c862014-12-05 19:25:29 +02002023out_unlock:
Aisheng Dong2b35bd82013-12-23 19:13:04 +08002024 spin_unlock_irqrestore(&host->lock, flags);
Arindam Nathb513ea22011-05-05 12:19:04 +05302025 return err;
2026}
2027
Adrian Huntercb849642015-02-06 14:12:59 +02002028static int sdhci_select_drive_strength(struct mmc_card *card,
2029 unsigned int max_dtr, int host_drv,
2030 int card_drv, int *drv_type)
2031{
2032 struct sdhci_host *host = mmc_priv(card->host);
2033
2034 if (!host->ops->select_drive_strength)
2035 return 0;
2036
2037 return host->ops->select_drive_strength(host, card, max_dtr, host_drv,
2038 card_drv, drv_type);
2039}
Kevin Liu52983382013-01-31 11:31:37 +08002040
2041static void sdhci_enable_preset_value(struct sdhci_host *host, bool enable)
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302042{
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302043 /* Host Controller v3.00 defines preset value registers */
2044 if (host->version < SDHCI_SPEC_300)
2045 return;
2046
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302047 /*
2048 * We only enable or disable Preset Value if they are not already
2049 * enabled or disabled respectively. Otherwise, we bail out.
2050 */
Russell Kingda91a8f2014-04-25 13:00:12 +01002051 if (host->preset_enabled != enable) {
2052 u16 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2053
2054 if (enable)
2055 ctrl |= SDHCI_CTRL_PRESET_VAL_ENABLE;
2056 else
2057 ctrl &= ~SDHCI_CTRL_PRESET_VAL_ENABLE;
2058
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302059 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Russell Kingda91a8f2014-04-25 13:00:12 +01002060
2061 if (enable)
2062 host->flags |= SDHCI_PV_ENABLED;
2063 else
2064 host->flags &= ~SDHCI_PV_ENABLED;
2065
2066 host->preset_enabled = enable;
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302067 }
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002068}
2069
Haibo Chen348487c2014-12-09 17:04:05 +08002070static void sdhci_post_req(struct mmc_host *mmc, struct mmc_request *mrq,
2071 int err)
2072{
2073 struct sdhci_host *host = mmc_priv(mmc);
2074 struct mmc_data *data = mrq->data;
2075
Russell Kingf48f0392016-01-26 13:40:32 +00002076 if (data->host_cookie != COOKIE_UNMAPPED)
Russell King771a3dc2016-01-26 13:40:53 +00002077 dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
2078 data->flags & MMC_DATA_WRITE ?
2079 DMA_TO_DEVICE : DMA_FROM_DEVICE);
2080
2081 data->host_cookie = COOKIE_UNMAPPED;
Haibo Chen348487c2014-12-09 17:04:05 +08002082}
2083
Haibo Chen348487c2014-12-09 17:04:05 +08002084static void sdhci_pre_req(struct mmc_host *mmc, struct mmc_request *mrq,
2085 bool is_first_req)
2086{
2087 struct sdhci_host *host = mmc_priv(mmc);
2088
Haibo Chend31911b2015-08-25 10:02:11 +08002089 mrq->data->host_cookie = COOKIE_UNMAPPED;
Haibo Chen348487c2014-12-09 17:04:05 +08002090
2091 if (host->flags & SDHCI_REQ_USE_DMA)
Russell King94538e52016-01-26 13:40:37 +00002092 sdhci_pre_dma_transfer(host, mrq->data, COOKIE_PRE_MAPPED);
Haibo Chen348487c2014-12-09 17:04:05 +08002093}
2094
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002095static void sdhci_card_event(struct mmc_host *mmc)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002096{
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002097 struct sdhci_host *host = mmc_priv(mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002098 unsigned long flags;
Krzysztof Kozlowski28367662015-01-05 10:50:15 +01002099 int present;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002100
Christian Daudt722e1282013-06-20 14:26:36 -07002101 /* First check if client has provided their own card event */
2102 if (host->ops->card_event)
2103 host->ops->card_event(host);
2104
Adrian Hunterd3940f22016-06-29 16:24:14 +03002105 present = mmc->ops->get_cd(mmc);
Krzysztof Kozlowski28367662015-01-05 10:50:15 +01002106
Pierre Ossmand129bce2006-03-24 03:18:17 -08002107 spin_lock_irqsave(&host->lock, flags);
2108
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002109 /* Check host->mrq first in case we are runtime suspended */
Krzysztof Kozlowski28367662015-01-05 10:50:15 +01002110 if (host->mrq && !present) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302111 pr_err("%s: Card removed during transfer!\n",
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002112 mmc_hostname(host->mmc));
Girish K Sa3c76eb2011-10-11 11:44:09 +05302113 pr_err("%s: Resetting controller.\n",
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002114 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08002115
Russell King03231f92014-04-25 12:57:12 +01002116 sdhci_do_reset(host, SDHCI_RESET_CMD);
2117 sdhci_do_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002118
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002119 host->mrq->cmd->error = -ENOMEDIUM;
2120 tasklet_schedule(&host->finish_tasklet);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002121 }
2122
2123 spin_unlock_irqrestore(&host->lock, flags);
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002124}
2125
2126static const struct mmc_host_ops sdhci_ops = {
2127 .request = sdhci_request,
Haibo Chen348487c2014-12-09 17:04:05 +08002128 .post_req = sdhci_post_req,
2129 .pre_req = sdhci_pre_req,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002130 .set_ios = sdhci_set_ios,
Kevin Liu94144a42013-02-28 17:35:53 +08002131 .get_cd = sdhci_get_cd,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002132 .get_ro = sdhci_get_ro,
2133 .hw_reset = sdhci_hw_reset,
2134 .enable_sdio_irq = sdhci_enable_sdio_irq,
2135 .start_signal_voltage_switch = sdhci_start_signal_voltage_switch,
Adrian Hunterb5540ce2014-12-05 19:25:31 +02002136 .prepare_hs400_tuning = sdhci_prepare_hs400_tuning,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002137 .execute_tuning = sdhci_execute_tuning,
Adrian Huntercb849642015-02-06 14:12:59 +02002138 .select_drive_strength = sdhci_select_drive_strength,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002139 .card_event = sdhci_card_event,
Kevin Liu20b92a32012-12-17 19:29:26 +08002140 .card_busy = sdhci_card_busy,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002141};
2142
2143/*****************************************************************************\
2144 * *
2145 * Tasklets *
2146 * *
2147\*****************************************************************************/
2148
Pierre Ossmand129bce2006-03-24 03:18:17 -08002149static void sdhci_tasklet_finish(unsigned long param)
2150{
2151 struct sdhci_host *host;
2152 unsigned long flags;
2153 struct mmc_request *mrq;
2154
2155 host = (struct sdhci_host*)param;
2156
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002157 spin_lock_irqsave(&host->lock, flags);
2158
Chris Ball0c9c99a2011-04-27 17:35:31 -04002159 /*
2160 * If this tasklet gets rescheduled while running, it will
2161 * be run again afterwards but without any active request.
2162 */
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002163 if (!host->mrq) {
2164 spin_unlock_irqrestore(&host->lock, flags);
Chris Ball0c9c99a2011-04-27 17:35:31 -04002165 return;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002166 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002167
2168 del_timer(&host->timer);
2169
2170 mrq = host->mrq;
2171
Pierre Ossmand129bce2006-03-24 03:18:17 -08002172 /*
Russell King054cedf2016-01-26 13:40:42 +00002173 * Always unmap the data buffers if they were mapped by
2174 * sdhci_prepare_data() whenever we finish with a request.
2175 * This avoids leaking DMA mappings on error.
2176 */
2177 if (host->flags & SDHCI_REQ_USE_DMA) {
2178 struct mmc_data *data = mrq->data;
2179
2180 if (data && data->host_cookie == COOKIE_MAPPED) {
2181 dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
2182 (data->flags & MMC_DATA_READ) ?
2183 DMA_FROM_DEVICE : DMA_TO_DEVICE);
2184 data->host_cookie = COOKIE_UNMAPPED;
2185 }
2186 }
2187
2188 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08002189 * The controller needs a reset of internal state machines
2190 * upon error conditions.
2191 */
Pierre Ossman1e728592008-04-16 19:13:13 +02002192 if (!(host->flags & SDHCI_DEVICE_DEAD) &&
Ben Dooksb7b4d342011-04-27 14:24:19 +01002193 ((mrq->cmd && mrq->cmd->error) ||
Andrew Gabbasovfce9d332014-10-01 07:14:08 -05002194 (mrq->sbc && mrq->sbc->error) ||
2195 (mrq->data && ((mrq->data->error && !mrq->data->stop) ||
2196 (mrq->data->stop && mrq->data->stop->error))) ||
2197 (host->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST))) {
Pierre Ossman645289d2006-06-30 02:22:33 -07002198
2199 /* Some controllers need this kick or reset won't work here */
Andy Shevchenko8213af32013-01-07 16:31:08 +02002200 if (host->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET)
Pierre Ossman645289d2006-06-30 02:22:33 -07002201 /* This is to force an update */
Russell King17710592014-04-25 12:58:55 +01002202 host->ops->set_clock(host, host->clock);
Pierre Ossman645289d2006-06-30 02:22:33 -07002203
2204 /* Spec says we should do both at the same time, but Ricoh
2205 controllers do not like that. */
Russell King03231f92014-04-25 12:57:12 +01002206 sdhci_do_reset(host, SDHCI_RESET_CMD);
2207 sdhci_do_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002208 }
2209
2210 host->mrq = NULL;
2211 host->cmd = NULL;
2212 host->data = NULL;
2213
Adrian Hunter061d17a2016-04-12 14:25:09 +03002214 sdhci_led_deactivate(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002215
Pierre Ossman5f25a662006-10-04 02:15:39 -07002216 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002217 spin_unlock_irqrestore(&host->lock, flags);
2218
2219 mmc_request_done(host->mmc, mrq);
2220}
2221
2222static void sdhci_timeout_timer(unsigned long data)
2223{
2224 struct sdhci_host *host;
2225 unsigned long flags;
2226
2227 host = (struct sdhci_host*)data;
2228
2229 spin_lock_irqsave(&host->lock, flags);
2230
2231 if (host->mrq) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01002232 pr_err("%s: Timeout waiting for hardware interrupt.\n",
2233 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08002234 sdhci_dumpregs(host);
2235
2236 if (host->data) {
Pierre Ossman17b04292007-07-22 22:18:46 +02002237 host->data->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002238 sdhci_finish_data(host);
2239 } else {
2240 if (host->cmd)
Pierre Ossman17b04292007-07-22 22:18:46 +02002241 host->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002242 else
Pierre Ossman17b04292007-07-22 22:18:46 +02002243 host->mrq->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002244
2245 tasklet_schedule(&host->finish_tasklet);
2246 }
2247 }
2248
Pierre Ossman5f25a662006-10-04 02:15:39 -07002249 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002250 spin_unlock_irqrestore(&host->lock, flags);
2251}
2252
2253/*****************************************************************************\
2254 * *
2255 * Interrupt handling *
2256 * *
2257\*****************************************************************************/
2258
Adrian Hunter61541392014-09-24 10:27:27 +03002259static void sdhci_cmd_irq(struct sdhci_host *host, u32 intmask, u32 *mask)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002260{
2261 BUG_ON(intmask == 0);
2262
2263 if (!host->cmd) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01002264 pr_err("%s: Got command interrupt 0x%08x even though no command operation was in progress.\n",
2265 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002266 sdhci_dumpregs(host);
2267 return;
2268 }
2269
Russell Kingec014cb2016-01-26 13:39:39 +00002270 if (intmask & (SDHCI_INT_TIMEOUT | SDHCI_INT_CRC |
2271 SDHCI_INT_END_BIT | SDHCI_INT_INDEX)) {
2272 if (intmask & SDHCI_INT_TIMEOUT)
2273 host->cmd->error = -ETIMEDOUT;
2274 else
2275 host->cmd->error = -EILSEQ;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002276
Russell King71fcbda2016-01-26 13:39:45 +00002277 /*
2278 * If this command initiates a data phase and a response
2279 * CRC error is signalled, the card can start transferring
2280 * data - the card may have received the command without
2281 * error. We must not terminate the mmc_request early.
2282 *
2283 * If the card did not receive the command or returned an
2284 * error which prevented it sending data, the data phase
2285 * will time out.
2286 */
2287 if (host->cmd->data &&
2288 (intmask & (SDHCI_INT_CRC | SDHCI_INT_TIMEOUT)) ==
2289 SDHCI_INT_CRC) {
2290 host->cmd = NULL;
2291 return;
2292 }
2293
Pierre Ossmand129bce2006-03-24 03:18:17 -08002294 tasklet_schedule(&host->finish_tasklet);
Pierre Ossmane8095172008-07-25 01:09:08 +02002295 return;
2296 }
2297
2298 /*
2299 * The host can send and interrupt when the busy state has
2300 * ended, allowing us to wait without wasting CPU cycles.
2301 * Unfortunately this is overloaded on the "data complete"
2302 * interrupt, so we need to take some care when handling
2303 * it.
2304 *
2305 * Note: The 1.0 specification is a bit ambiguous about this
2306 * feature so there might be some problems with older
2307 * controllers.
2308 */
2309 if (host->cmd->flags & MMC_RSP_BUSY) {
2310 if (host->cmd->data)
Marek Vasut2e4456f2015-11-18 10:47:02 +01002311 DBG("Cannot wait for busy signal when also doing a data transfer");
Chanho Mine99783a2014-08-30 12:40:40 +09002312 else if (!(host->quirks & SDHCI_QUIRK_NO_BUSY_IRQ)
2313 && !host->busy_handle) {
2314 /* Mark that command complete before busy is ended */
2315 host->busy_handle = 1;
Pierre Ossmane8095172008-07-25 01:09:08 +02002316 return;
Chanho Mine99783a2014-08-30 12:40:40 +09002317 }
Ben Dooksf9454052009-02-20 20:33:08 +03002318
2319 /* The controller does not support the end-of-busy IRQ,
2320 * fall through and take the SDHCI_INT_RESPONSE */
Adrian Hunter61541392014-09-24 10:27:27 +03002321 } else if ((host->quirks2 & SDHCI_QUIRK2_STOP_WITH_TC) &&
2322 host->cmd->opcode == MMC_STOP_TRANSMISSION && !host->data) {
2323 *mask &= ~SDHCI_INT_DATA_END;
Pierre Ossmane8095172008-07-25 01:09:08 +02002324 }
2325
2326 if (intmask & SDHCI_INT_RESPONSE)
Pierre Ossman43b58b32007-07-25 23:15:27 +02002327 sdhci_finish_command(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002328}
2329
George G. Davis0957c332010-02-18 12:32:12 -05002330#ifdef CONFIG_MMC_DEBUG
Adrian Hunter08621b12014-11-04 12:42:38 +02002331static void sdhci_adma_show_error(struct sdhci_host *host)
Ben Dooks6882a8c2009-06-14 13:52:38 +01002332{
2333 const char *name = mmc_hostname(host->mmc);
Adrian Hunter1c3d5f62014-11-04 12:42:41 +02002334 void *desc = host->adma_table;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002335
2336 sdhci_dumpregs(host);
2337
2338 while (true) {
Adrian Huntere57a5f62014-11-04 12:42:46 +02002339 struct sdhci_adma2_64_desc *dma_desc = desc;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002340
Adrian Huntere57a5f62014-11-04 12:42:46 +02002341 if (host->flags & SDHCI_USE_64_BIT_DMA)
2342 DBG("%s: %p: DMA 0x%08x%08x, LEN 0x%04x, Attr=0x%02x\n",
2343 name, desc, le32_to_cpu(dma_desc->addr_hi),
2344 le32_to_cpu(dma_desc->addr_lo),
2345 le16_to_cpu(dma_desc->len),
2346 le16_to_cpu(dma_desc->cmd));
2347 else
2348 DBG("%s: %p: DMA 0x%08x, LEN 0x%04x, Attr=0x%02x\n",
2349 name, desc, le32_to_cpu(dma_desc->addr_lo),
2350 le16_to_cpu(dma_desc->len),
2351 le16_to_cpu(dma_desc->cmd));
Ben Dooks6882a8c2009-06-14 13:52:38 +01002352
Adrian Hunter76fe3792014-11-04 12:42:42 +02002353 desc += host->desc_sz;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002354
Adrian Hunter05452302014-11-04 12:42:45 +02002355 if (dma_desc->cmd & cpu_to_le16(ADMA2_END))
Ben Dooks6882a8c2009-06-14 13:52:38 +01002356 break;
2357 }
2358}
2359#else
Adrian Hunter08621b12014-11-04 12:42:38 +02002360static void sdhci_adma_show_error(struct sdhci_host *host) { }
Ben Dooks6882a8c2009-06-14 13:52:38 +01002361#endif
2362
Pierre Ossmand129bce2006-03-24 03:18:17 -08002363static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
2364{
Girish K S069c9f12012-01-06 09:56:39 +05302365 u32 command;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002366 BUG_ON(intmask == 0);
2367
Arindam Nathb513ea22011-05-05 12:19:04 +05302368 /* CMD19 generates _only_ Buffer Read Ready interrupt */
2369 if (intmask & SDHCI_INT_DATA_AVAIL) {
Girish K S069c9f12012-01-06 09:56:39 +05302370 command = SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND));
2371 if (command == MMC_SEND_TUNING_BLOCK ||
2372 command == MMC_SEND_TUNING_BLOCK_HS200) {
Arindam Nathb513ea22011-05-05 12:19:04 +05302373 host->tuning_done = 1;
2374 wake_up(&host->buf_ready_int);
2375 return;
2376 }
2377 }
2378
Pierre Ossmand129bce2006-03-24 03:18:17 -08002379 if (!host->data) {
2380 /*
Pierre Ossmane8095172008-07-25 01:09:08 +02002381 * The "data complete" interrupt is also used to
2382 * indicate that a busy state has ended. See comment
2383 * above in sdhci_cmd_irq().
Pierre Ossmand129bce2006-03-24 03:18:17 -08002384 */
Pierre Ossmane8095172008-07-25 01:09:08 +02002385 if (host->cmd && (host->cmd->flags & MMC_RSP_BUSY)) {
Matthieu CASTETc5abd5e2014-08-14 16:03:17 +02002386 if (intmask & SDHCI_INT_DATA_TIMEOUT) {
2387 host->cmd->error = -ETIMEDOUT;
2388 tasklet_schedule(&host->finish_tasklet);
2389 return;
2390 }
Pierre Ossmane8095172008-07-25 01:09:08 +02002391 if (intmask & SDHCI_INT_DATA_END) {
Chanho Mine99783a2014-08-30 12:40:40 +09002392 /*
2393 * Some cards handle busy-end interrupt
2394 * before the command completed, so make
2395 * sure we do things in the proper order.
2396 */
2397 if (host->busy_handle)
2398 sdhci_finish_command(host);
2399 else
2400 host->busy_handle = 1;
Pierre Ossmane8095172008-07-25 01:09:08 +02002401 return;
2402 }
2403 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002404
Marek Vasut2e4456f2015-11-18 10:47:02 +01002405 pr_err("%s: Got data interrupt 0x%08x even though no data operation was in progress.\n",
2406 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002407 sdhci_dumpregs(host);
2408
2409 return;
2410 }
2411
2412 if (intmask & SDHCI_INT_DATA_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02002413 host->data->error = -ETIMEDOUT;
Aries Lee22113ef2010-12-15 08:14:24 +01002414 else if (intmask & SDHCI_INT_DATA_END_BIT)
2415 host->data->error = -EILSEQ;
2416 else if ((intmask & SDHCI_INT_DATA_CRC) &&
2417 SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND))
2418 != MMC_BUS_TEST_R)
Pierre Ossman17b04292007-07-22 22:18:46 +02002419 host->data->error = -EILSEQ;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002420 else if (intmask & SDHCI_INT_ADMA_ERROR) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302421 pr_err("%s: ADMA error\n", mmc_hostname(host->mmc));
Adrian Hunter08621b12014-11-04 12:42:38 +02002422 sdhci_adma_show_error(host);
Pierre Ossman2134a922008-06-28 18:28:51 +02002423 host->data->error = -EIO;
Haijun Zhanga4071fb2012-12-04 10:41:28 +08002424 if (host->ops->adma_workaround)
2425 host->ops->adma_workaround(host, intmask);
Ben Dooks6882a8c2009-06-14 13:52:38 +01002426 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002427
Pierre Ossman17b04292007-07-22 22:18:46 +02002428 if (host->data->error)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002429 sdhci_finish_data(host);
2430 else {
Pierre Ossmana406f5a2006-07-02 16:50:59 +01002431 if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
Pierre Ossmand129bce2006-03-24 03:18:17 -08002432 sdhci_transfer_pio(host);
2433
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002434 /*
2435 * We currently don't do anything fancy with DMA
2436 * boundaries, but as we can't disable the feature
2437 * we need to at least restart the transfer.
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04002438 *
2439 * According to the spec sdhci_readl(host, SDHCI_DMA_ADDRESS)
2440 * should return a valid address to continue from, but as
2441 * some controllers are faulty, don't trust them.
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002442 */
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04002443 if (intmask & SDHCI_INT_DMA_END) {
2444 u32 dmastart, dmanow;
2445 dmastart = sg_dma_address(host->data->sg);
2446 dmanow = dmastart + host->data->bytes_xfered;
2447 /*
2448 * Force update to the next DMA block boundary.
2449 */
2450 dmanow = (dmanow &
2451 ~(SDHCI_DEFAULT_BOUNDARY_SIZE - 1)) +
2452 SDHCI_DEFAULT_BOUNDARY_SIZE;
2453 host->data->bytes_xfered = dmanow - dmastart;
2454 DBG("%s: DMA base 0x%08x, transferred 0x%06x bytes,"
2455 " next 0x%08x\n",
2456 mmc_hostname(host->mmc), dmastart,
2457 host->data->bytes_xfered, dmanow);
2458 sdhci_writel(host, dmanow, SDHCI_DMA_ADDRESS);
2459 }
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002460
Pierre Ossmane538fbe2007-08-12 16:46:32 +02002461 if (intmask & SDHCI_INT_DATA_END) {
2462 if (host->cmd) {
2463 /*
2464 * Data managed to finish before the
2465 * command completed. Make sure we do
2466 * things in the proper order.
2467 */
2468 host->data_early = 1;
2469 } else {
2470 sdhci_finish_data(host);
2471 }
2472 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002473 }
2474}
2475
David Howells7d12e782006-10-05 14:55:46 +01002476static irqreturn_t sdhci_irq(int irq, void *dev_id)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002477{
Russell King781e9892014-04-25 12:55:46 +01002478 irqreturn_t result = IRQ_NONE;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002479 struct sdhci_host *host = dev_id;
Russell King41005002014-04-25 12:55:36 +01002480 u32 intmask, mask, unexpected = 0;
Russell King781e9892014-04-25 12:55:46 +01002481 int max_loops = 16;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002482
2483 spin_lock(&host->lock);
2484
Russell Kingbe138552014-04-25 12:55:56 +01002485 if (host->runtime_suspended && !sdhci_sdio_irq_enabled(host)) {
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002486 spin_unlock(&host->lock);
Adrian Hunter655bca72014-03-11 10:09:36 +02002487 return IRQ_NONE;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002488 }
2489
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002490 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
Mark Lord62df67a52007-03-06 13:30:13 +01002491 if (!intmask || intmask == 0xffffffff) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08002492 result = IRQ_NONE;
2493 goto out;
2494 }
2495
Russell King41005002014-04-25 12:55:36 +01002496 do {
2497 /* Clear selected interrupts. */
2498 mask = intmask & (SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK |
2499 SDHCI_INT_BUS_POWER);
2500 sdhci_writel(host, mask, SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002501
Russell King41005002014-04-25 12:55:36 +01002502 DBG("*** %s got interrupt: 0x%08x\n",
2503 mmc_hostname(host->mmc), intmask);
2504
2505 if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
2506 u32 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
2507 SDHCI_CARD_PRESENT;
2508
2509 /*
2510 * There is a observation on i.mx esdhc. INSERT
2511 * bit will be immediately set again when it gets
2512 * cleared, if a card is inserted. We have to mask
2513 * the irq to prevent interrupt storm which will
2514 * freeze the system. And the REMOVE gets the
2515 * same situation.
2516 *
2517 * More testing are needed here to ensure it works
2518 * for other platforms though.
2519 */
Russell Kingb537f942014-04-25 12:56:01 +01002520 host->ier &= ~(SDHCI_INT_CARD_INSERT |
2521 SDHCI_INT_CARD_REMOVE);
2522 host->ier |= present ? SDHCI_INT_CARD_REMOVE :
2523 SDHCI_INT_CARD_INSERT;
2524 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
2525 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Russell King41005002014-04-25 12:55:36 +01002526
2527 sdhci_writel(host, intmask & (SDHCI_INT_CARD_INSERT |
2528 SDHCI_INT_CARD_REMOVE), SDHCI_INT_STATUS);
Russell King3560db82014-04-25 12:55:51 +01002529
2530 host->thread_isr |= intmask & (SDHCI_INT_CARD_INSERT |
2531 SDHCI_INT_CARD_REMOVE);
2532 result = IRQ_WAKE_THREAD;
Russell King41005002014-04-25 12:55:36 +01002533 }
2534
2535 if (intmask & SDHCI_INT_CMD_MASK)
Adrian Hunter61541392014-09-24 10:27:27 +03002536 sdhci_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK,
2537 &intmask);
Russell King41005002014-04-25 12:55:36 +01002538
2539 if (intmask & SDHCI_INT_DATA_MASK)
2540 sdhci_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
2541
2542 if (intmask & SDHCI_INT_BUS_POWER)
2543 pr_err("%s: Card is consuming too much power!\n",
2544 mmc_hostname(host->mmc));
2545
Russell King781e9892014-04-25 12:55:46 +01002546 if (intmask & SDHCI_INT_CARD_INT) {
2547 sdhci_enable_sdio_irq_nolock(host, false);
2548 host->thread_isr |= SDHCI_INT_CARD_INT;
2549 result = IRQ_WAKE_THREAD;
2550 }
Russell King41005002014-04-25 12:55:36 +01002551
2552 intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE |
2553 SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK |
2554 SDHCI_INT_ERROR | SDHCI_INT_BUS_POWER |
2555 SDHCI_INT_CARD_INT);
2556
2557 if (intmask) {
2558 unexpected |= intmask;
2559 sdhci_writel(host, intmask, SDHCI_INT_STATUS);
2560 }
2561
Russell King781e9892014-04-25 12:55:46 +01002562 if (result == IRQ_NONE)
2563 result = IRQ_HANDLED;
Russell King41005002014-04-25 12:55:36 +01002564
2565 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
Russell King41005002014-04-25 12:55:36 +01002566 } while (intmask && --max_loops);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002567out:
2568 spin_unlock(&host->lock);
2569
Alexander Stein6379b232012-03-14 09:52:10 +01002570 if (unexpected) {
2571 pr_err("%s: Unexpected interrupt 0x%08x.\n",
2572 mmc_hostname(host->mmc), unexpected);
2573 sdhci_dumpregs(host);
2574 }
Pierre Ossmanf75979b2007-09-04 07:59:18 +02002575
Pierre Ossmand129bce2006-03-24 03:18:17 -08002576 return result;
2577}
2578
Russell King781e9892014-04-25 12:55:46 +01002579static irqreturn_t sdhci_thread_irq(int irq, void *dev_id)
2580{
2581 struct sdhci_host *host = dev_id;
2582 unsigned long flags;
2583 u32 isr;
2584
2585 spin_lock_irqsave(&host->lock, flags);
2586 isr = host->thread_isr;
2587 host->thread_isr = 0;
2588 spin_unlock_irqrestore(&host->lock, flags);
2589
Russell King3560db82014-04-25 12:55:51 +01002590 if (isr & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
Adrian Hunterd3940f22016-06-29 16:24:14 +03002591 struct mmc_host *mmc = host->mmc;
2592
2593 mmc->ops->card_event(mmc);
2594 mmc_detect_change(mmc, msecs_to_jiffies(200));
Russell King3560db82014-04-25 12:55:51 +01002595 }
2596
Russell King781e9892014-04-25 12:55:46 +01002597 if (isr & SDHCI_INT_CARD_INT) {
2598 sdio_run_irqs(host->mmc);
2599
2600 spin_lock_irqsave(&host->lock, flags);
2601 if (host->flags & SDHCI_SDIO_IRQ_ENABLED)
2602 sdhci_enable_sdio_irq_nolock(host, true);
2603 spin_unlock_irqrestore(&host->lock, flags);
2604 }
2605
2606 return isr ? IRQ_HANDLED : IRQ_NONE;
2607}
2608
Pierre Ossmand129bce2006-03-24 03:18:17 -08002609/*****************************************************************************\
2610 * *
2611 * Suspend/resume *
2612 * *
2613\*****************************************************************************/
2614
2615#ifdef CONFIG_PM
Ludovic Desroches84d62602016-05-13 15:16:02 +02002616/*
2617 * To enable wakeup events, the corresponding events have to be enabled in
2618 * the Interrupt Status Enable register too. See 'Table 1-6: Wakeup Signal
2619 * Table' in the SD Host Controller Standard Specification.
2620 * It is useless to restore SDHCI_INT_ENABLE state in
2621 * sdhci_disable_irq_wakeups() since it will be set by
2622 * sdhci_enable_card_detection() or sdhci_init().
2623 */
Kevin Liuad080d72013-01-05 17:21:33 +08002624void sdhci_enable_irq_wakeups(struct sdhci_host *host)
2625{
2626 u8 val;
2627 u8 mask = SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE
2628 | SDHCI_WAKE_ON_INT;
Ludovic Desroches84d62602016-05-13 15:16:02 +02002629 u32 irq_val = SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE |
2630 SDHCI_INT_CARD_INT;
Kevin Liuad080d72013-01-05 17:21:33 +08002631
2632 val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
2633 val |= mask ;
2634 /* Avoid fake wake up */
Ludovic Desroches84d62602016-05-13 15:16:02 +02002635 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) {
Kevin Liuad080d72013-01-05 17:21:33 +08002636 val &= ~(SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE);
Ludovic Desroches84d62602016-05-13 15:16:02 +02002637 irq_val &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE);
2638 }
Kevin Liuad080d72013-01-05 17:21:33 +08002639 sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
Ludovic Desroches84d62602016-05-13 15:16:02 +02002640 sdhci_writel(host, irq_val, SDHCI_INT_ENABLE);
Kevin Liuad080d72013-01-05 17:21:33 +08002641}
2642EXPORT_SYMBOL_GPL(sdhci_enable_irq_wakeups);
2643
Fabio Estevam0b10f472014-08-30 14:53:13 -03002644static void sdhci_disable_irq_wakeups(struct sdhci_host *host)
Kevin Liuad080d72013-01-05 17:21:33 +08002645{
2646 u8 val;
2647 u8 mask = SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE
2648 | SDHCI_WAKE_ON_INT;
2649
2650 val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
2651 val &= ~mask;
2652 sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
2653}
Pierre Ossmand129bce2006-03-24 03:18:17 -08002654
Manuel Lauss29495aa2011-11-03 11:09:45 +01002655int sdhci_suspend_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002656{
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002657 sdhci_disable_card_detection(host);
2658
Adrian Hunter66c39dfc2015-05-07 13:10:21 +03002659 mmc_retune_timer_stop(host->mmc);
2660 mmc_retune_needed(host->mmc);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302661
Kevin Liuad080d72013-01-05 17:21:33 +08002662 if (!device_may_wakeup(mmc_dev(host->mmc))) {
Russell Kingb537f942014-04-25 12:56:01 +01002663 host->ier = 0;
2664 sdhci_writel(host, 0, SDHCI_INT_ENABLE);
2665 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Kevin Liuad080d72013-01-05 17:21:33 +08002666 free_irq(host->irq, host);
2667 } else {
2668 sdhci_enable_irq_wakeups(host);
2669 enable_irq_wake(host->irq);
2670 }
Ulf Hansson4ee14ec2013-09-25 14:15:24 +02002671 return 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002672}
2673
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002674EXPORT_SYMBOL_GPL(sdhci_suspend_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002675
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002676int sdhci_resume_host(struct sdhci_host *host)
2677{
Adrian Hunterd3940f22016-06-29 16:24:14 +03002678 struct mmc_host *mmc = host->mmc;
Ulf Hansson4ee14ec2013-09-25 14:15:24 +02002679 int ret = 0;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002680
Richard Röjforsa13abc72009-09-22 16:45:30 -07002681 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002682 if (host->ops->enable_dma)
2683 host->ops->enable_dma(host);
2684 }
2685
Adrian Hunter6308d292012-02-07 14:48:54 +02002686 if ((host->mmc->pm_flags & MMC_PM_KEEP_POWER) &&
2687 (host->quirks2 & SDHCI_QUIRK2_HOST_OFF_CARD_ON)) {
2688 /* Card keeps power but host controller does not */
2689 sdhci_init(host, 0);
2690 host->pwr = 0;
2691 host->clock = 0;
Adrian Hunterd3940f22016-06-29 16:24:14 +03002692 mmc->ops->set_ios(mmc, &mmc->ios);
Adrian Hunter6308d292012-02-07 14:48:54 +02002693 } else {
2694 sdhci_init(host, (host->mmc->pm_flags & MMC_PM_KEEP_POWER));
2695 mmiowb();
2696 }
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002697
Haibo Chen14a7b41642015-09-15 18:32:58 +08002698 if (!device_may_wakeup(mmc_dev(host->mmc))) {
2699 ret = request_threaded_irq(host->irq, sdhci_irq,
2700 sdhci_thread_irq, IRQF_SHARED,
2701 mmc_hostname(host->mmc), host);
2702 if (ret)
2703 return ret;
2704 } else {
2705 sdhci_disable_irq_wakeups(host);
2706 disable_irq_wake(host->irq);
2707 }
2708
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002709 sdhci_enable_card_detection(host);
2710
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08002711 return ret;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002712}
2713
2714EXPORT_SYMBOL_GPL(sdhci_resume_host);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002715
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002716int sdhci_runtime_suspend_host(struct sdhci_host *host)
2717{
2718 unsigned long flags;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002719
Adrian Hunter66c39dfc2015-05-07 13:10:21 +03002720 mmc_retune_timer_stop(host->mmc);
2721 mmc_retune_needed(host->mmc);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002722
2723 spin_lock_irqsave(&host->lock, flags);
Russell Kingb537f942014-04-25 12:56:01 +01002724 host->ier &= SDHCI_INT_CARD_INT;
2725 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
2726 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002727 spin_unlock_irqrestore(&host->lock, flags);
2728
Russell King781e9892014-04-25 12:55:46 +01002729 synchronize_hardirq(host->irq);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002730
2731 spin_lock_irqsave(&host->lock, flags);
2732 host->runtime_suspended = true;
2733 spin_unlock_irqrestore(&host->lock, flags);
2734
Markus Pargmann8a125ba2014-06-04 15:24:29 +02002735 return 0;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002736}
2737EXPORT_SYMBOL_GPL(sdhci_runtime_suspend_host);
2738
2739int sdhci_runtime_resume_host(struct sdhci_host *host)
2740{
Adrian Hunterd3940f22016-06-29 16:24:14 +03002741 struct mmc_host *mmc = host->mmc;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002742 unsigned long flags;
Markus Pargmann8a125ba2014-06-04 15:24:29 +02002743 int host_flags = host->flags;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002744
2745 if (host_flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
2746 if (host->ops->enable_dma)
2747 host->ops->enable_dma(host);
2748 }
2749
2750 sdhci_init(host, 0);
2751
2752 /* Force clock and power re-program */
2753 host->pwr = 0;
2754 host->clock = 0;
Adrian Hunterd3940f22016-06-29 16:24:14 +03002755 mmc->ops->start_signal_voltage_switch(mmc, &mmc->ios);
2756 mmc->ops->set_ios(mmc, &mmc->ios);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002757
Kevin Liu52983382013-01-31 11:31:37 +08002758 if ((host_flags & SDHCI_PV_ENABLED) &&
2759 !(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN)) {
2760 spin_lock_irqsave(&host->lock, flags);
2761 sdhci_enable_preset_value(host, true);
2762 spin_unlock_irqrestore(&host->lock, flags);
2763 }
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002764
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002765 spin_lock_irqsave(&host->lock, flags);
2766
2767 host->runtime_suspended = false;
2768
2769 /* Enable SDIO IRQ */
Russell Kingef104332014-04-25 12:55:41 +01002770 if (host->flags & SDHCI_SDIO_IRQ_ENABLED)
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002771 sdhci_enable_sdio_irq_nolock(host, true);
2772
2773 /* Enable Card Detection */
2774 sdhci_enable_card_detection(host);
2775
2776 spin_unlock_irqrestore(&host->lock, flags);
2777
Markus Pargmann8a125ba2014-06-04 15:24:29 +02002778 return 0;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002779}
2780EXPORT_SYMBOL_GPL(sdhci_runtime_resume_host);
2781
Rafael J. Wysocki162d6f92014-12-05 03:05:33 +01002782#endif /* CONFIG_PM */
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002783
Pierre Ossmand129bce2006-03-24 03:18:17 -08002784/*****************************************************************************\
2785 * *
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002786 * Device allocation/registration *
Pierre Ossmand129bce2006-03-24 03:18:17 -08002787 * *
2788\*****************************************************************************/
2789
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002790struct sdhci_host *sdhci_alloc_host(struct device *dev,
2791 size_t priv_size)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002792{
Pierre Ossmand129bce2006-03-24 03:18:17 -08002793 struct mmc_host *mmc;
2794 struct sdhci_host *host;
2795
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002796 WARN_ON(dev == NULL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002797
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002798 mmc = mmc_alloc_host(sizeof(struct sdhci_host) + priv_size, dev);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002799 if (!mmc)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002800 return ERR_PTR(-ENOMEM);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002801
2802 host = mmc_priv(mmc);
2803 host->mmc = mmc;
Adrian Hunterbf60e592016-02-09 16:12:35 +02002804 host->mmc_host_ops = sdhci_ops;
2805 mmc->ops = &host->mmc_host_ops;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002806
Adrian Hunter8cb851a2016-06-29 16:24:16 +03002807 host->flags = SDHCI_SIGNALING_330;
2808
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002809 return host;
2810}
Pierre Ossman8a4da142006-10-04 02:15:40 -07002811
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002812EXPORT_SYMBOL_GPL(sdhci_alloc_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002813
Alexandre Courbot7b913692016-03-07 11:07:55 +09002814static int sdhci_set_dma_mask(struct sdhci_host *host)
2815{
2816 struct mmc_host *mmc = host->mmc;
2817 struct device *dev = mmc_dev(mmc);
2818 int ret = -EINVAL;
2819
2820 if (host->quirks2 & SDHCI_QUIRK2_BROKEN_64_BIT_DMA)
2821 host->flags &= ~SDHCI_USE_64_BIT_DMA;
2822
2823 /* Try 64-bit mask if hardware is capable of it */
2824 if (host->flags & SDHCI_USE_64_BIT_DMA) {
2825 ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64));
2826 if (ret) {
2827 pr_warn("%s: Failed to set 64-bit DMA mask.\n",
2828 mmc_hostname(mmc));
2829 host->flags &= ~SDHCI_USE_64_BIT_DMA;
2830 }
2831 }
2832
2833 /* 32-bit mask as default & fallback */
2834 if (ret) {
2835 ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
2836 if (ret)
2837 pr_warn("%s: Failed to set 32-bit DMA mask.\n",
2838 mmc_hostname(mmc));
2839 }
2840
2841 return ret;
2842}
2843
Adrian Hunter6132a3b2016-06-29 16:24:18 +03002844void __sdhci_read_caps(struct sdhci_host *host, u16 *ver, u32 *caps, u32 *caps1)
2845{
2846 u16 v;
2847
2848 if (host->read_caps)
2849 return;
2850
2851 host->read_caps = true;
2852
2853 if (debug_quirks)
2854 host->quirks = debug_quirks;
2855
2856 if (debug_quirks2)
2857 host->quirks2 = debug_quirks2;
2858
2859 sdhci_do_reset(host, SDHCI_RESET_ALL);
2860
2861 v = ver ? *ver : sdhci_readw(host, SDHCI_HOST_VERSION);
2862 host->version = (v & SDHCI_SPEC_VER_MASK) >> SDHCI_SPEC_VER_SHIFT;
2863
2864 if (host->quirks & SDHCI_QUIRK_MISSING_CAPS)
2865 return;
2866
2867 host->caps = caps ? *caps : sdhci_readl(host, SDHCI_CAPABILITIES);
2868
2869 if (host->version < SDHCI_SPEC_300)
2870 return;
2871
2872 host->caps1 = caps1 ? *caps1 : sdhci_readl(host, SDHCI_CAPABILITIES_1);
2873}
2874EXPORT_SYMBOL_GPL(__sdhci_read_caps);
2875
Adrian Hunter52f53362016-06-29 16:24:15 +03002876int sdhci_setup_host(struct sdhci_host *host)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002877{
2878 struct mmc_host *mmc;
Arindam Nathf2119df2011-05-05 12:18:57 +05302879 u32 max_current_caps;
2880 unsigned int ocr_avail;
Adrian Hunterf5fa92e2014-09-24 10:27:32 +03002881 unsigned int override_timeout_clk;
Dong Aisheng59241752015-07-22 20:53:07 +08002882 u32 max_clk;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002883 int ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002884
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002885 WARN_ON(host == NULL);
2886 if (host == NULL)
2887 return -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002888
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002889 mmc = host->mmc;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002890
Adrian Hunter6132a3b2016-06-29 16:24:18 +03002891 sdhci_read_caps(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002892
Adrian Hunterf5fa92e2014-09-24 10:27:32 +03002893 override_timeout_clk = host->timeout_clk;
2894
Zhangfei Gao85105c52010-08-06 07:10:01 +08002895 if (host->version > SDHCI_SPEC_300) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01002896 pr_err("%s: Unknown controller version (%d). You may experience problems.\n",
2897 mmc_hostname(mmc), host->version);
Pierre Ossman4a965502006-06-30 02:22:29 -07002898 }
2899
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002900 if (host->quirks & SDHCI_QUIRK_FORCE_DMA)
Richard Röjforsa13abc72009-09-22 16:45:30 -07002901 host->flags |= SDHCI_USE_SDMA;
Adrian Hunter28da3582016-06-29 16:24:17 +03002902 else if (!(host->caps & SDHCI_CAN_DO_SDMA))
Richard Röjforsa13abc72009-09-22 16:45:30 -07002903 DBG("Controller doesn't have SDMA capability\n");
Pierre Ossman67435272006-06-30 02:22:31 -07002904 else
Richard Röjforsa13abc72009-09-22 16:45:30 -07002905 host->flags |= SDHCI_USE_SDMA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002906
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002907 if ((host->quirks & SDHCI_QUIRK_BROKEN_DMA) &&
Richard Röjforsa13abc72009-09-22 16:45:30 -07002908 (host->flags & SDHCI_USE_SDMA)) {
Rolf Eike Beercee687c2007-11-02 15:22:30 +01002909 DBG("Disabling DMA as it is marked broken\n");
Richard Röjforsa13abc72009-09-22 16:45:30 -07002910 host->flags &= ~SDHCI_USE_SDMA;
Feng Tang7c168e32007-09-30 12:44:18 +02002911 }
2912
Arindam Nathf2119df2011-05-05 12:18:57 +05302913 if ((host->version >= SDHCI_SPEC_200) &&
Adrian Hunter28da3582016-06-29 16:24:17 +03002914 (host->caps & SDHCI_CAN_DO_ADMA2))
Richard Röjforsa13abc72009-09-22 16:45:30 -07002915 host->flags |= SDHCI_USE_ADMA;
Pierre Ossman2134a922008-06-28 18:28:51 +02002916
2917 if ((host->quirks & SDHCI_QUIRK_BROKEN_ADMA) &&
2918 (host->flags & SDHCI_USE_ADMA)) {
2919 DBG("Disabling ADMA as it is marked broken\n");
2920 host->flags &= ~SDHCI_USE_ADMA;
2921 }
2922
Adrian Huntere57a5f62014-11-04 12:42:46 +02002923 /*
2924 * It is assumed that a 64-bit capable device has set a 64-bit DMA mask
2925 * and *must* do 64-bit DMA. A driver has the opportunity to change
2926 * that during the first call to ->enable_dma(). Similarly
2927 * SDHCI_QUIRK2_BROKEN_64_BIT_DMA must be left to the drivers to
2928 * implement.
2929 */
Adrian Hunter28da3582016-06-29 16:24:17 +03002930 if (host->caps & SDHCI_CAN_64BIT)
Adrian Huntere57a5f62014-11-04 12:42:46 +02002931 host->flags |= SDHCI_USE_64_BIT_DMA;
2932
Richard Röjforsa13abc72009-09-22 16:45:30 -07002933 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Alexandre Courbot7b913692016-03-07 11:07:55 +09002934 ret = sdhci_set_dma_mask(host);
2935
2936 if (!ret && host->ops->enable_dma)
2937 ret = host->ops->enable_dma(host);
2938
2939 if (ret) {
2940 pr_warn("%s: No suitable DMA available - falling back to PIO\n",
2941 mmc_hostname(mmc));
2942 host->flags &= ~(SDHCI_USE_SDMA | SDHCI_USE_ADMA);
2943
2944 ret = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002945 }
2946 }
2947
Adrian Huntere57a5f62014-11-04 12:42:46 +02002948 /* SDMA does not support 64-bit DMA */
2949 if (host->flags & SDHCI_USE_64_BIT_DMA)
2950 host->flags &= ~SDHCI_USE_SDMA;
2951
Pierre Ossman2134a922008-06-28 18:28:51 +02002952 if (host->flags & SDHCI_USE_ADMA) {
Russell Kinge66e61c2016-01-26 13:39:55 +00002953 dma_addr_t dma;
2954 void *buf;
2955
Pierre Ossman2134a922008-06-28 18:28:51 +02002956 /*
Adrian Hunter76fe3792014-11-04 12:42:42 +02002957 * The DMA descriptor table size is calculated as the maximum
2958 * number of segments times 2, to allow for an alignment
2959 * descriptor for each segment, plus 1 for a nop end descriptor,
2960 * all multipled by the descriptor size.
Pierre Ossman2134a922008-06-28 18:28:51 +02002961 */
Adrian Huntere57a5f62014-11-04 12:42:46 +02002962 if (host->flags & SDHCI_USE_64_BIT_DMA) {
2963 host->adma_table_sz = (SDHCI_MAX_SEGS * 2 + 1) *
2964 SDHCI_ADMA2_64_DESC_SZ;
Adrian Huntere57a5f62014-11-04 12:42:46 +02002965 host->desc_sz = SDHCI_ADMA2_64_DESC_SZ;
Adrian Huntere57a5f62014-11-04 12:42:46 +02002966 } else {
2967 host->adma_table_sz = (SDHCI_MAX_SEGS * 2 + 1) *
2968 SDHCI_ADMA2_32_DESC_SZ;
Adrian Huntere57a5f62014-11-04 12:42:46 +02002969 host->desc_sz = SDHCI_ADMA2_32_DESC_SZ;
Adrian Huntere57a5f62014-11-04 12:42:46 +02002970 }
Russell Kinge66e61c2016-01-26 13:39:55 +00002971
Adrian Hunter04a5ae62015-11-26 14:00:49 +02002972 host->align_buffer_sz = SDHCI_MAX_SEGS * SDHCI_ADMA2_ALIGN;
Russell Kinge66e61c2016-01-26 13:39:55 +00002973 buf = dma_alloc_coherent(mmc_dev(mmc), host->align_buffer_sz +
2974 host->adma_table_sz, &dma, GFP_KERNEL);
2975 if (!buf) {
Joe Perches66061102014-09-12 14:56:56 -07002976 pr_warn("%s: Unable to allocate ADMA buffers - falling back to standard DMA\n",
Pierre Ossman2134a922008-06-28 18:28:51 +02002977 mmc_hostname(mmc));
2978 host->flags &= ~SDHCI_USE_ADMA;
Russell Kinge66e61c2016-01-26 13:39:55 +00002979 } else if ((dma + host->align_buffer_sz) &
2980 (SDHCI_ADMA2_DESC_ALIGN - 1)) {
Joe Perches66061102014-09-12 14:56:56 -07002981 pr_warn("%s: unable to allocate aligned ADMA descriptor\n",
2982 mmc_hostname(mmc));
Russell Kingd1e49f72014-04-25 12:58:34 +01002983 host->flags &= ~SDHCI_USE_ADMA;
Russell Kinge66e61c2016-01-26 13:39:55 +00002984 dma_free_coherent(mmc_dev(mmc), host->align_buffer_sz +
2985 host->adma_table_sz, buf, dma);
2986 } else {
2987 host->align_buffer = buf;
2988 host->align_addr = dma;
Russell Kingedd63fc2016-01-26 13:39:50 +00002989
Russell Kinge66e61c2016-01-26 13:39:55 +00002990 host->adma_table = buf + host->align_buffer_sz;
2991 host->adma_addr = dma + host->align_buffer_sz;
2992 }
Pierre Ossman2134a922008-06-28 18:28:51 +02002993 }
2994
Pierre Ossman76591502008-07-21 00:32:11 +02002995 /*
2996 * If we use DMA, then it's up to the caller to set the DMA
2997 * mask, but PIO does not need the hw shim so we set a new
2998 * mask here in that case.
2999 */
Richard Röjforsa13abc72009-09-22 16:45:30 -07003000 if (!(host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))) {
Pierre Ossman76591502008-07-21 00:32:11 +02003001 host->dma_mask = DMA_BIT_MASK(64);
Markus Mayer4e743f12014-07-03 13:27:42 -07003002 mmc_dev(mmc)->dma_mask = &host->dma_mask;
Pierre Ossman76591502008-07-21 00:32:11 +02003003 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003004
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04003005 if (host->version >= SDHCI_SPEC_300)
Adrian Hunter28da3582016-06-29 16:24:17 +03003006 host->max_clk = (host->caps & SDHCI_CLOCK_V3_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04003007 >> SDHCI_CLOCK_BASE_SHIFT;
3008 else
Adrian Hunter28da3582016-06-29 16:24:17 +03003009 host->max_clk = (host->caps & SDHCI_CLOCK_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04003010 >> SDHCI_CLOCK_BASE_SHIFT;
3011
Pierre Ossmand129bce2006-03-24 03:18:17 -08003012 host->max_clk *= 1000000;
Anton Vorontsovf27f47e2010-05-26 14:41:53 -07003013 if (host->max_clk == 0 || host->quirks &
3014 SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN) {
Ben Dooks4240ff02009-03-17 00:13:57 +03003015 if (!host->ops->get_max_clock) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01003016 pr_err("%s: Hardware doesn't specify base clock frequency.\n",
3017 mmc_hostname(mmc));
Adrian Huntereb5c20d2016-04-12 14:25:08 +03003018 ret = -ENODEV;
3019 goto undma;
Ben Dooks4240ff02009-03-17 00:13:57 +03003020 }
3021 host->max_clk = host->ops->get_max_clock(host);
3022 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003023
3024 /*
Arindam Nathc3ed3872011-05-05 12:19:06 +05303025 * In case of Host Controller v3.00, find out whether clock
3026 * multiplier is supported.
3027 */
Adrian Hunter28da3582016-06-29 16:24:17 +03003028 host->clk_mul = (host->caps1 & SDHCI_CLOCK_MUL_MASK) >>
Arindam Nathc3ed3872011-05-05 12:19:06 +05303029 SDHCI_CLOCK_MUL_SHIFT;
3030
3031 /*
3032 * In case the value in Clock Multiplier is 0, then programmable
3033 * clock mode is not supported, otherwise the actual clock
3034 * multiplier is one more than the value of Clock Multiplier
3035 * in the Capabilities Register.
3036 */
3037 if (host->clk_mul)
3038 host->clk_mul += 1;
3039
3040 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08003041 * Set host parameters.
3042 */
Dong Aisheng59241752015-07-22 20:53:07 +08003043 max_clk = host->max_clk;
3044
Marek Szyprowskice5f0362010-08-10 18:01:56 -07003045 if (host->ops->get_min_clock)
Anton Vorontsova9e58f22009-07-29 15:04:16 -07003046 mmc->f_min = host->ops->get_min_clock(host);
Arindam Nathc3ed3872011-05-05 12:19:06 +05303047 else if (host->version >= SDHCI_SPEC_300) {
3048 if (host->clk_mul) {
3049 mmc->f_min = (host->max_clk * host->clk_mul) / 1024;
Dong Aisheng59241752015-07-22 20:53:07 +08003050 max_clk = host->max_clk * host->clk_mul;
Arindam Nathc3ed3872011-05-05 12:19:06 +05303051 } else
3052 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_300;
3053 } else
Zhangfei Gao03975262010-09-20 15:15:18 -04003054 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_200;
Philip Rakity15ec4462010-11-19 16:48:39 -05003055
Adrian Hunterd310ae42016-04-12 14:25:07 +03003056 if (!mmc->f_max || mmc->f_max > max_clk)
Dong Aisheng59241752015-07-22 20:53:07 +08003057 mmc->f_max = max_clk;
3058
Aisheng Dong28aab052014-08-27 15:26:31 +08003059 if (!(host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)) {
Adrian Hunter28da3582016-06-29 16:24:17 +03003060 host->timeout_clk = (host->caps & SDHCI_TIMEOUT_CLK_MASK) >>
Aisheng Dong28aab052014-08-27 15:26:31 +08003061 SDHCI_TIMEOUT_CLK_SHIFT;
3062 if (host->timeout_clk == 0) {
3063 if (host->ops->get_timeout_clock) {
3064 host->timeout_clk =
3065 host->ops->get_timeout_clock(host);
3066 } else {
3067 pr_err("%s: Hardware doesn't specify timeout clock frequency.\n",
3068 mmc_hostname(mmc));
Adrian Huntereb5c20d2016-04-12 14:25:08 +03003069 ret = -ENODEV;
3070 goto undma;
Aisheng Dong28aab052014-08-27 15:26:31 +08003071 }
Andy Shevchenko272308c2011-08-03 18:36:00 +03003072 }
Andy Shevchenko272308c2011-08-03 18:36:00 +03003073
Adrian Hunter28da3582016-06-29 16:24:17 +03003074 if (host->caps & SDHCI_TIMEOUT_CLK_UNIT)
Aisheng Dong28aab052014-08-27 15:26:31 +08003075 host->timeout_clk *= 1000;
Andy Shevchenko272308c2011-08-03 18:36:00 +03003076
Adrian Hunter99513622016-03-07 13:33:55 +02003077 if (override_timeout_clk)
3078 host->timeout_clk = override_timeout_clk;
3079
Aisheng Dong28aab052014-08-27 15:26:31 +08003080 mmc->max_busy_timeout = host->ops->get_max_timeout_count ?
Aisheng Donga6ff5ae2014-08-27 15:26:27 +08003081 host->ops->get_max_timeout_count(host) : 1 << 27;
Aisheng Dong28aab052014-08-27 15:26:31 +08003082 mmc->max_busy_timeout /= host->timeout_clk;
3083 }
Adrian Hunter58d12462011-06-28 17:16:03 +03003084
Andrei Warkentine89d4562011-05-23 15:06:37 -05003085 mmc->caps |= MMC_CAP_SDIO_IRQ | MMC_CAP_ERASE | MMC_CAP_CMD23;
Russell King781e9892014-04-25 12:55:46 +01003086 mmc->caps2 |= MMC_CAP2_SDIO_IRQ_NOTHREAD;
Andrei Warkentine89d4562011-05-23 15:06:37 -05003087
3088 if (host->quirks & SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12)
3089 host->flags |= SDHCI_AUTO_CMD12;
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04003090
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003091 /* Auto-CMD23 stuff only works in ADMA or PIO. */
Andrei Warkentin4f3d3e92011-05-25 10:42:50 -04003092 if ((host->version >= SDHCI_SPEC_300) &&
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003093 ((host->flags & SDHCI_USE_ADMA) ||
Scott Branden3bfa6f02015-02-09 16:06:28 -08003094 !(host->flags & SDHCI_USE_SDMA)) &&
3095 !(host->quirks2 & SDHCI_QUIRK2_ACMD23_BROKEN)) {
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003096 host->flags |= SDHCI_AUTO_CMD23;
3097 DBG("%s: Auto-CMD23 available\n", mmc_hostname(mmc));
3098 } else {
3099 DBG("%s: Auto-CMD23 unavailable\n", mmc_hostname(mmc));
3100 }
3101
Philip Rakity15ec4462010-11-19 16:48:39 -05003102 /*
3103 * A controller may support 8-bit width, but the board itself
3104 * might not have the pins brought out. Boards that support
3105 * 8-bit width must set "mmc->caps |= MMC_CAP_8_BIT_DATA;" in
3106 * their platform code before calling sdhci_add_host(), and we
3107 * won't assume 8-bit width for hosts without that CAP.
3108 */
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04003109 if (!(host->quirks & SDHCI_QUIRK_FORCE_1_BIT_DATA))
Philip Rakity15ec4462010-11-19 16:48:39 -05003110 mmc->caps |= MMC_CAP_4_BIT_DATA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003111
Jerry Huang63ef5d82012-10-25 13:47:19 +08003112 if (host->quirks2 & SDHCI_QUIRK2_HOST_NO_CMD23)
3113 mmc->caps &= ~MMC_CAP_CMD23;
3114
Adrian Hunter28da3582016-06-29 16:24:17 +03003115 if (host->caps & SDHCI_CAN_DO_HISPD)
Zhangfei Gaoa29e7e12010-08-16 21:15:32 -04003116 mmc->caps |= MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED;
Pierre Ossmancd9277c2007-02-18 12:07:47 +01003117
Jaehoon Chung176d1ed2010-09-27 09:42:20 +01003118 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) &&
Jaehoon Chung860951c2016-06-21 10:13:26 +09003119 mmc_card_is_removable(mmc) &&
Arnd Bergmann287980e2016-05-27 23:23:25 +02003120 mmc_gpio_get_cd(host->mmc) < 0)
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03003121 mmc->caps |= MMC_CAP_NEEDS_POLL;
3122
Tim Kryger3a48edc2014-06-13 10:13:56 -07003123 /* If there are external regulators, get them */
Adrian Huntereb5c20d2016-04-12 14:25:08 +03003124 ret = mmc_regulator_get_supply(mmc);
3125 if (ret == -EPROBE_DEFER)
3126 goto undma;
Tim Kryger3a48edc2014-06-13 10:13:56 -07003127
Philip Rakity6231f3d2012-07-23 15:56:23 -07003128 /* If vqmmc regulator and no 1.8V signalling, then there's no UHS */
Tim Kryger3a48edc2014-06-13 10:13:56 -07003129 if (!IS_ERR(mmc->supply.vqmmc)) {
3130 ret = regulator_enable(mmc->supply.vqmmc);
3131 if (!regulator_is_supported_voltage(mmc->supply.vqmmc, 1700000,
3132 1950000))
Adrian Hunter28da3582016-06-29 16:24:17 +03003133 host->caps1 &= ~(SDHCI_SUPPORT_SDR104 |
3134 SDHCI_SUPPORT_SDR50 |
3135 SDHCI_SUPPORT_DDR50);
Chris Balla3361ab2013-03-11 17:51:53 -04003136 if (ret) {
3137 pr_warn("%s: Failed to enable vqmmc regulator: %d\n",
3138 mmc_hostname(mmc), ret);
Adrian Hunter4bb74312014-11-06 15:19:04 +02003139 mmc->supply.vqmmc = ERR_PTR(-EINVAL);
Chris Balla3361ab2013-03-11 17:51:53 -04003140 }
Kevin Liu8363c372012-11-17 17:55:51 -05003141 }
Philip Rakity6231f3d2012-07-23 15:56:23 -07003142
Adrian Hunter28da3582016-06-29 16:24:17 +03003143 if (host->quirks2 & SDHCI_QUIRK2_NO_1_8_V) {
3144 host->caps1 &= ~(SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 |
3145 SDHCI_SUPPORT_DDR50);
3146 }
Daniel Drake6a661802012-11-25 13:01:19 -05003147
Al Cooper4188bba2012-03-16 15:54:17 -04003148 /* Any UHS-I mode in caps implies SDR12 and SDR25 support. */
Adrian Hunter28da3582016-06-29 16:24:17 +03003149 if (host->caps1 & (SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 |
3150 SDHCI_SUPPORT_DDR50))
Arindam Nathf2119df2011-05-05 12:18:57 +05303151 mmc->caps |= MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25;
3152
3153 /* SDR104 supports also implies SDR50 support */
Adrian Hunter28da3582016-06-29 16:24:17 +03003154 if (host->caps1 & SDHCI_SUPPORT_SDR104) {
Arindam Nathf2119df2011-05-05 12:18:57 +05303155 mmc->caps |= MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_SDR50;
Giuseppe CAVALLARO156e14b2013-06-12 08:16:38 +02003156 /* SD3.0: SDR104 is supported so (for eMMC) the caps2
3157 * field can be promoted to support HS200.
3158 */
Adrian Hunter549c0b12014-11-06 15:19:05 +02003159 if (!(host->quirks2 & SDHCI_QUIRK2_BROKEN_HS200))
David Cohen13868bf2013-10-29 10:58:26 -07003160 mmc->caps2 |= MMC_CAP2_HS200;
Adrian Hunter28da3582016-06-29 16:24:17 +03003161 } else if (host->caps1 & SDHCI_SUPPORT_SDR50) {
Arindam Nathf2119df2011-05-05 12:18:57 +05303162 mmc->caps |= MMC_CAP_UHS_SDR50;
Adrian Hunter28da3582016-06-29 16:24:17 +03003163 }
Arindam Nathf2119df2011-05-05 12:18:57 +05303164
Adrian Huntere9fb05d2014-11-06 15:19:06 +02003165 if (host->quirks2 & SDHCI_QUIRK2_CAPS_BIT63_FOR_HS400 &&
Adrian Hunter28da3582016-06-29 16:24:17 +03003166 (host->caps1 & SDHCI_SUPPORT_HS400))
Adrian Huntere9fb05d2014-11-06 15:19:06 +02003167 mmc->caps2 |= MMC_CAP2_HS400;
3168
Adrian Hunter549c0b12014-11-06 15:19:05 +02003169 if ((mmc->caps2 & MMC_CAP2_HSX00_1_2V) &&
3170 (IS_ERR(mmc->supply.vqmmc) ||
3171 !regulator_is_supported_voltage(mmc->supply.vqmmc, 1100000,
3172 1300000)))
3173 mmc->caps2 &= ~MMC_CAP2_HSX00_1_2V;
3174
Adrian Hunter28da3582016-06-29 16:24:17 +03003175 if ((host->caps1 & SDHCI_SUPPORT_DDR50) &&
3176 !(host->quirks2 & SDHCI_QUIRK2_BROKEN_DDR50))
Arindam Nathf2119df2011-05-05 12:18:57 +05303177 mmc->caps |= MMC_CAP_UHS_DDR50;
3178
Girish K S069c9f12012-01-06 09:56:39 +05303179 /* Does the host need tuning for SDR50? */
Adrian Hunter28da3582016-06-29 16:24:17 +03003180 if (host->caps1 & SDHCI_USE_SDR50_TUNING)
Arindam Nathb513ea22011-05-05 12:19:04 +05303181 host->flags |= SDHCI_SDR50_NEEDS_TUNING;
3182
Arindam Nathd6d50a12011-05-05 12:18:59 +05303183 /* Driver Type(s) (A, C, D) supported by the host */
Adrian Hunter28da3582016-06-29 16:24:17 +03003184 if (host->caps1 & SDHCI_DRIVER_TYPE_A)
Arindam Nathd6d50a12011-05-05 12:18:59 +05303185 mmc->caps |= MMC_CAP_DRIVER_TYPE_A;
Adrian Hunter28da3582016-06-29 16:24:17 +03003186 if (host->caps1 & SDHCI_DRIVER_TYPE_C)
Arindam Nathd6d50a12011-05-05 12:18:59 +05303187 mmc->caps |= MMC_CAP_DRIVER_TYPE_C;
Adrian Hunter28da3582016-06-29 16:24:17 +03003188 if (host->caps1 & SDHCI_DRIVER_TYPE_D)
Arindam Nathd6d50a12011-05-05 12:18:59 +05303189 mmc->caps |= MMC_CAP_DRIVER_TYPE_D;
3190
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303191 /* Initial value for re-tuning timer count */
Adrian Hunter28da3582016-06-29 16:24:17 +03003192 host->tuning_count = (host->caps1 & SDHCI_RETUNING_TIMER_COUNT_MASK) >>
3193 SDHCI_RETUNING_TIMER_COUNT_SHIFT;
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303194
3195 /*
3196 * In case Re-tuning Timer is not disabled, the actual value of
3197 * re-tuning timer will be 2 ^ (n - 1).
3198 */
3199 if (host->tuning_count)
3200 host->tuning_count = 1 << (host->tuning_count - 1);
3201
3202 /* Re-tuning mode supported by the Host Controller */
Adrian Hunter28da3582016-06-29 16:24:17 +03003203 host->tuning_mode = (host->caps1 & SDHCI_RETUNING_MODE_MASK) >>
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303204 SDHCI_RETUNING_MODE_SHIFT;
3205
Takashi Iwai8f230f42010-12-08 10:04:30 +01003206 ocr_avail = 0;
Philip Rakitybad37e12012-05-27 18:36:44 -07003207
Arindam Nathf2119df2011-05-05 12:18:57 +05303208 /*
3209 * According to SD Host Controller spec v3.00, if the Host System
3210 * can afford more than 150mA, Host Driver should set XPC to 1. Also
3211 * the value is meaningful only if Voltage Support in the Capabilities
3212 * register is set. The actual current value is 4 times the register
3213 * value.
3214 */
3215 max_current_caps = sdhci_readl(host, SDHCI_MAX_CURRENT);
Tim Kryger3a48edc2014-06-13 10:13:56 -07003216 if (!max_current_caps && !IS_ERR(mmc->supply.vmmc)) {
Chuanxiao.Dongae906032014-08-01 14:00:13 +08003217 int curr = regulator_get_current_limit(mmc->supply.vmmc);
Philip Rakitybad37e12012-05-27 18:36:44 -07003218 if (curr > 0) {
3219
3220 /* convert to SDHCI_MAX_CURRENT format */
3221 curr = curr/1000; /* convert to mA */
3222 curr = curr/SDHCI_MAX_CURRENT_MULTIPLIER;
3223
3224 curr = min_t(u32, curr, SDHCI_MAX_CURRENT_LIMIT);
3225 max_current_caps =
3226 (curr << SDHCI_MAX_CURRENT_330_SHIFT) |
3227 (curr << SDHCI_MAX_CURRENT_300_SHIFT) |
3228 (curr << SDHCI_MAX_CURRENT_180_SHIFT);
3229 }
3230 }
Arindam Nathf2119df2011-05-05 12:18:57 +05303231
Adrian Hunter28da3582016-06-29 16:24:17 +03003232 if (host->caps & SDHCI_CAN_VDD_330) {
Takashi Iwai8f230f42010-12-08 10:04:30 +01003233 ocr_avail |= MMC_VDD_32_33 | MMC_VDD_33_34;
Arindam Nathf2119df2011-05-05 12:18:57 +05303234
Aaron Lu55c46652012-07-04 13:31:48 +08003235 mmc->max_current_330 = ((max_current_caps &
Arindam Nathf2119df2011-05-05 12:18:57 +05303236 SDHCI_MAX_CURRENT_330_MASK) >>
3237 SDHCI_MAX_CURRENT_330_SHIFT) *
3238 SDHCI_MAX_CURRENT_MULTIPLIER;
Arindam Nathf2119df2011-05-05 12:18:57 +05303239 }
Adrian Hunter28da3582016-06-29 16:24:17 +03003240 if (host->caps & SDHCI_CAN_VDD_300) {
Takashi Iwai8f230f42010-12-08 10:04:30 +01003241 ocr_avail |= MMC_VDD_29_30 | MMC_VDD_30_31;
Arindam Nathf2119df2011-05-05 12:18:57 +05303242
Aaron Lu55c46652012-07-04 13:31:48 +08003243 mmc->max_current_300 = ((max_current_caps &
Arindam Nathf2119df2011-05-05 12:18:57 +05303244 SDHCI_MAX_CURRENT_300_MASK) >>
3245 SDHCI_MAX_CURRENT_300_SHIFT) *
3246 SDHCI_MAX_CURRENT_MULTIPLIER;
Arindam Nathf2119df2011-05-05 12:18:57 +05303247 }
Adrian Hunter28da3582016-06-29 16:24:17 +03003248 if (host->caps & SDHCI_CAN_VDD_180) {
Takashi Iwai8f230f42010-12-08 10:04:30 +01003249 ocr_avail |= MMC_VDD_165_195;
3250
Aaron Lu55c46652012-07-04 13:31:48 +08003251 mmc->max_current_180 = ((max_current_caps &
Arindam Nathf2119df2011-05-05 12:18:57 +05303252 SDHCI_MAX_CURRENT_180_MASK) >>
3253 SDHCI_MAX_CURRENT_180_SHIFT) *
3254 SDHCI_MAX_CURRENT_MULTIPLIER;
Arindam Nathf2119df2011-05-05 12:18:57 +05303255 }
3256
Ulf Hansson5fd26c72015-06-05 11:40:08 +02003257 /* If OCR set by host, use it instead. */
3258 if (host->ocr_mask)
3259 ocr_avail = host->ocr_mask;
3260
3261 /* If OCR set by external regulators, give it highest prio. */
Tim Kryger3a48edc2014-06-13 10:13:56 -07003262 if (mmc->ocr_avail)
Tim Kryger52221612014-06-25 00:25:34 -07003263 ocr_avail = mmc->ocr_avail;
Tim Kryger3a48edc2014-06-13 10:13:56 -07003264
Takashi Iwai8f230f42010-12-08 10:04:30 +01003265 mmc->ocr_avail = ocr_avail;
3266 mmc->ocr_avail_sdio = ocr_avail;
3267 if (host->ocr_avail_sdio)
3268 mmc->ocr_avail_sdio &= host->ocr_avail_sdio;
3269 mmc->ocr_avail_sd = ocr_avail;
3270 if (host->ocr_avail_sd)
3271 mmc->ocr_avail_sd &= host->ocr_avail_sd;
3272 else /* normal SD controllers don't support 1.8V */
3273 mmc->ocr_avail_sd &= ~MMC_VDD_165_195;
3274 mmc->ocr_avail_mmc = ocr_avail;
3275 if (host->ocr_avail_mmc)
3276 mmc->ocr_avail_mmc &= host->ocr_avail_mmc;
Pierre Ossman146ad662006-06-30 02:22:23 -07003277
3278 if (mmc->ocr_avail == 0) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01003279 pr_err("%s: Hardware doesn't report any support voltages.\n",
3280 mmc_hostname(mmc));
Adrian Huntereb5c20d2016-04-12 14:25:08 +03003281 ret = -ENODEV;
3282 goto unreg;
Pierre Ossman146ad662006-06-30 02:22:23 -07003283 }
3284
Adrian Hunter8cb851a2016-06-29 16:24:16 +03003285 if ((mmc->caps & (MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 |
3286 MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR104 |
3287 MMC_CAP_UHS_DDR50 | MMC_CAP_1_8V_DDR)) ||
3288 (mmc->caps2 & (MMC_CAP2_HS200_1_8V_SDR | MMC_CAP2_HS400_1_8V)))
3289 host->flags |= SDHCI_SIGNALING_180;
3290
3291 if (mmc->caps2 & MMC_CAP2_HSX00_1_2V)
3292 host->flags |= SDHCI_SIGNALING_120;
3293
Pierre Ossmand129bce2006-03-24 03:18:17 -08003294 spin_lock_init(&host->lock);
3295
3296 /*
Pierre Ossman2134a922008-06-28 18:28:51 +02003297 * Maximum number of segments. Depends on if the hardware
3298 * can do scatter/gather or not.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003299 */
Pierre Ossman2134a922008-06-28 18:28:51 +02003300 if (host->flags & SDHCI_USE_ADMA)
Adrian Hunter4fb213f2014-11-04 12:42:43 +02003301 mmc->max_segs = SDHCI_MAX_SEGS;
Richard Röjforsa13abc72009-09-22 16:45:30 -07003302 else if (host->flags & SDHCI_USE_SDMA)
Martin K. Petersena36274e2010-09-10 01:33:59 -04003303 mmc->max_segs = 1;
Pierre Ossman2134a922008-06-28 18:28:51 +02003304 else /* PIO */
Adrian Hunter4fb213f2014-11-04 12:42:43 +02003305 mmc->max_segs = SDHCI_MAX_SEGS;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003306
3307 /*
Adrian Hunterac005312014-12-05 19:25:28 +02003308 * Maximum number of sectors in one transfer. Limited by SDMA boundary
3309 * size (512KiB). Note some tuning modes impose a 4MiB limit, but this
3310 * is less anyway.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003311 */
Pierre Ossman55db8902006-11-21 17:55:45 +01003312 mmc->max_req_size = 524288;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003313
3314 /*
3315 * Maximum segment size. Could be one segment with the maximum number
Pierre Ossman2134a922008-06-28 18:28:51 +02003316 * of bytes. When doing hardware scatter/gather, each entry cannot
3317 * be larger than 64 KiB though.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003318 */
Olof Johansson30652aa2011-01-01 18:37:32 -06003319 if (host->flags & SDHCI_USE_ADMA) {
3320 if (host->quirks & SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC)
3321 mmc->max_seg_size = 65535;
3322 else
3323 mmc->max_seg_size = 65536;
3324 } else {
Pierre Ossman2134a922008-06-28 18:28:51 +02003325 mmc->max_seg_size = mmc->max_req_size;
Olof Johansson30652aa2011-01-01 18:37:32 -06003326 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003327
3328 /*
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01003329 * Maximum block size. This varies from controller to controller and
3330 * is specified in the capabilities register.
3331 */
Anton Vorontsov0633f652009-03-17 00:14:03 +03003332 if (host->quirks & SDHCI_QUIRK_FORCE_BLK_SZ_2048) {
3333 mmc->max_blk_size = 2;
3334 } else {
Adrian Hunter28da3582016-06-29 16:24:17 +03003335 mmc->max_blk_size = (host->caps & SDHCI_MAX_BLOCK_MASK) >>
Anton Vorontsov0633f652009-03-17 00:14:03 +03003336 SDHCI_MAX_BLOCK_SHIFT;
3337 if (mmc->max_blk_size >= 3) {
Joe Perches66061102014-09-12 14:56:56 -07003338 pr_warn("%s: Invalid maximum block size, assuming 512 bytes\n",
3339 mmc_hostname(mmc));
Anton Vorontsov0633f652009-03-17 00:14:03 +03003340 mmc->max_blk_size = 0;
3341 }
3342 }
3343
3344 mmc->max_blk_size = 512 << mmc->max_blk_size;
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01003345
3346 /*
Pierre Ossman55db8902006-11-21 17:55:45 +01003347 * Maximum block count.
3348 */
Ben Dooks1388eef2009-06-14 12:40:53 +01003349 mmc->max_blk_count = (host->quirks & SDHCI_QUIRK_NO_MULTIBLOCK) ? 1 : 65535;
Pierre Ossman55db8902006-11-21 17:55:45 +01003350
Adrian Hunter52f53362016-06-29 16:24:15 +03003351 return 0;
3352
3353unreg:
3354 if (!IS_ERR(mmc->supply.vqmmc))
3355 regulator_disable(mmc->supply.vqmmc);
3356undma:
3357 if (host->align_buffer)
3358 dma_free_coherent(mmc_dev(mmc), host->align_buffer_sz +
3359 host->adma_table_sz, host->align_buffer,
3360 host->align_addr);
3361 host->adma_table = NULL;
3362 host->align_buffer = NULL;
3363
3364 return ret;
3365}
3366EXPORT_SYMBOL_GPL(sdhci_setup_host);
3367
3368int __sdhci_add_host(struct sdhci_host *host)
3369{
3370 struct mmc_host *mmc = host->mmc;
3371 int ret;
3372
Pierre Ossman55db8902006-11-21 17:55:45 +01003373 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08003374 * Init tasklets.
3375 */
Pierre Ossmand129bce2006-03-24 03:18:17 -08003376 tasklet_init(&host->finish_tasklet,
3377 sdhci_tasklet_finish, (unsigned long)host);
3378
Al Viroe4cad1b2006-10-10 22:47:07 +01003379 setup_timer(&host->timer, sdhci_timeout_timer, (unsigned long)host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003380
Adrian Hunter250fb7b42014-12-05 19:41:10 +02003381 init_waitqueue_head(&host->buf_ready_int);
Arindam Nathb513ea22011-05-05 12:19:04 +05303382
Shawn Guo2af502c2013-07-05 14:38:55 +08003383 sdhci_init(host, 0);
3384
Russell King781e9892014-04-25 12:55:46 +01003385 ret = request_threaded_irq(host->irq, sdhci_irq, sdhci_thread_irq,
3386 IRQF_SHARED, mmc_hostname(mmc), host);
Mark Brown0fc81ee2012-07-02 14:26:15 +01003387 if (ret) {
3388 pr_err("%s: Failed to request IRQ %d: %d\n",
3389 mmc_hostname(mmc), host->irq, ret);
Pierre Ossman8ef1a142006-06-30 02:22:21 -07003390 goto untasklet;
Mark Brown0fc81ee2012-07-02 14:26:15 +01003391 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003392
Pierre Ossmand129bce2006-03-24 03:18:17 -08003393#ifdef CONFIG_MMC_DEBUG
3394 sdhci_dumpregs(host);
3395#endif
3396
Adrian Hunter061d17a2016-04-12 14:25:09 +03003397 ret = sdhci_led_register(host);
Mark Brown0fc81ee2012-07-02 14:26:15 +01003398 if (ret) {
3399 pr_err("%s: Failed to register LED device: %d\n",
3400 mmc_hostname(mmc), ret);
Adrian Huntereb5c20d2016-04-12 14:25:08 +03003401 goto unirq;
Mark Brown0fc81ee2012-07-02 14:26:15 +01003402 }
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003403
Pierre Ossman5f25a662006-10-04 02:15:39 -07003404 mmiowb();
3405
Adrian Huntereb5c20d2016-04-12 14:25:08 +03003406 ret = mmc_add_host(mmc);
3407 if (ret)
3408 goto unled;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003409
Girish K Sa3c76eb2011-10-11 11:44:09 +05303410 pr_info("%s: SDHCI controller on %s [%s] using %s\n",
Kay Sieversd1b26862008-11-08 21:37:46 +01003411 mmc_hostname(mmc), host->hw_name, dev_name(mmc_dev(mmc)),
Adrian Huntere57a5f62014-11-04 12:42:46 +02003412 (host->flags & SDHCI_USE_ADMA) ?
3413 (host->flags & SDHCI_USE_64_BIT_DMA) ? "ADMA 64-bit" : "ADMA" :
Richard Röjforsa13abc72009-09-22 16:45:30 -07003414 (host->flags & SDHCI_USE_SDMA) ? "DMA" : "PIO");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003415
Anton Vorontsov7260cf52009-03-17 00:13:48 +03003416 sdhci_enable_card_detection(host);
3417
Pierre Ossmand129bce2006-03-24 03:18:17 -08003418 return 0;
3419
Adrian Huntereb5c20d2016-04-12 14:25:08 +03003420unled:
Adrian Hunter061d17a2016-04-12 14:25:09 +03003421 sdhci_led_unregister(host);
Adrian Huntereb5c20d2016-04-12 14:25:08 +03003422unirq:
Russell King03231f92014-04-25 12:57:12 +01003423 sdhci_do_reset(host, SDHCI_RESET_ALL);
Russell Kingb537f942014-04-25 12:56:01 +01003424 sdhci_writel(host, 0, SDHCI_INT_ENABLE);
3425 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003426 free_irq(host->irq, host);
Pierre Ossman8ef1a142006-06-30 02:22:21 -07003427untasklet:
Pierre Ossmand129bce2006-03-24 03:18:17 -08003428 tasklet_kill(&host->finish_tasklet);
Adrian Hunter52f53362016-06-29 16:24:15 +03003429
Adrian Huntereb5c20d2016-04-12 14:25:08 +03003430 if (!IS_ERR(mmc->supply.vqmmc))
3431 regulator_disable(mmc->supply.vqmmc);
Adrian Hunter52f53362016-06-29 16:24:15 +03003432
Adrian Huntereb5c20d2016-04-12 14:25:08 +03003433 if (host->align_buffer)
3434 dma_free_coherent(mmc_dev(mmc), host->align_buffer_sz +
3435 host->adma_table_sz, host->align_buffer,
3436 host->align_addr);
3437 host->adma_table = NULL;
3438 host->align_buffer = NULL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003439
3440 return ret;
3441}
Adrian Hunter52f53362016-06-29 16:24:15 +03003442EXPORT_SYMBOL_GPL(__sdhci_add_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003443
Adrian Hunter52f53362016-06-29 16:24:15 +03003444int sdhci_add_host(struct sdhci_host *host)
3445{
3446 int ret;
3447
3448 ret = sdhci_setup_host(host);
3449 if (ret)
3450 return ret;
3451
3452 return __sdhci_add_host(host);
3453}
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003454EXPORT_SYMBOL_GPL(sdhci_add_host);
3455
Pierre Ossman1e728592008-04-16 19:13:13 +02003456void sdhci_remove_host(struct sdhci_host *host, int dead)
Pierre Ossmand129bce2006-03-24 03:18:17 -08003457{
Tim Kryger3a48edc2014-06-13 10:13:56 -07003458 struct mmc_host *mmc = host->mmc;
Pierre Ossman1e728592008-04-16 19:13:13 +02003459 unsigned long flags;
3460
3461 if (dead) {
3462 spin_lock_irqsave(&host->lock, flags);
3463
3464 host->flags |= SDHCI_DEVICE_DEAD;
3465
3466 if (host->mrq) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05303467 pr_err("%s: Controller removed during "
Markus Mayer4e743f12014-07-03 13:27:42 -07003468 " transfer!\n", mmc_hostname(mmc));
Pierre Ossman1e728592008-04-16 19:13:13 +02003469
3470 host->mrq->cmd->error = -ENOMEDIUM;
3471 tasklet_schedule(&host->finish_tasklet);
3472 }
3473
3474 spin_unlock_irqrestore(&host->lock, flags);
3475 }
3476
Anton Vorontsov7260cf52009-03-17 00:13:48 +03003477 sdhci_disable_card_detection(host);
3478
Markus Mayer4e743f12014-07-03 13:27:42 -07003479 mmc_remove_host(mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003480
Adrian Hunter061d17a2016-04-12 14:25:09 +03003481 sdhci_led_unregister(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003482
Pierre Ossman1e728592008-04-16 19:13:13 +02003483 if (!dead)
Russell King03231f92014-04-25 12:57:12 +01003484 sdhci_do_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003485
Russell Kingb537f942014-04-25 12:56:01 +01003486 sdhci_writel(host, 0, SDHCI_INT_ENABLE);
3487 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003488 free_irq(host->irq, host);
3489
3490 del_timer_sync(&host->timer);
3491
Pierre Ossmand129bce2006-03-24 03:18:17 -08003492 tasklet_kill(&host->finish_tasklet);
Pierre Ossman2134a922008-06-28 18:28:51 +02003493
Tim Kryger3a48edc2014-06-13 10:13:56 -07003494 if (!IS_ERR(mmc->supply.vqmmc))
3495 regulator_disable(mmc->supply.vqmmc);
Philip Rakity6231f3d2012-07-23 15:56:23 -07003496
Russell Kingedd63fc2016-01-26 13:39:50 +00003497 if (host->align_buffer)
Russell Kinge66e61c2016-01-26 13:39:55 +00003498 dma_free_coherent(mmc_dev(mmc), host->align_buffer_sz +
3499 host->adma_table_sz, host->align_buffer,
3500 host->align_addr);
Pierre Ossman2134a922008-06-28 18:28:51 +02003501
Adrian Hunter4efaa6f2014-11-04 12:42:39 +02003502 host->adma_table = NULL;
Pierre Ossman2134a922008-06-28 18:28:51 +02003503 host->align_buffer = NULL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003504}
3505
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003506EXPORT_SYMBOL_GPL(sdhci_remove_host);
3507
3508void sdhci_free_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08003509{
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003510 mmc_free_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003511}
3512
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003513EXPORT_SYMBOL_GPL(sdhci_free_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003514
3515/*****************************************************************************\
3516 * *
3517 * Driver init/exit *
3518 * *
3519\*****************************************************************************/
3520
3521static int __init sdhci_drv_init(void)
3522{
Girish K Sa3c76eb2011-10-11 11:44:09 +05303523 pr_info(DRIVER_NAME
Pierre Ossman52fbf9c2007-02-09 08:23:41 +01003524 ": Secure Digital Host Controller Interface driver\n");
Girish K Sa3c76eb2011-10-11 11:44:09 +05303525 pr_info(DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003526
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003527 return 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003528}
3529
3530static void __exit sdhci_drv_exit(void)
3531{
Pierre Ossmand129bce2006-03-24 03:18:17 -08003532}
3533
3534module_init(sdhci_drv_init);
3535module_exit(sdhci_drv_exit);
3536
Pierre Ossmandf673b22006-06-30 02:22:31 -07003537module_param(debug_quirks, uint, 0444);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003538module_param(debug_quirks2, uint, 0444);
Pierre Ossman67435272006-06-30 02:22:31 -07003539
Pierre Ossman32710e82009-04-08 20:14:54 +02003540MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003541MODULE_DESCRIPTION("Secure Digital Host Controller Interface core driver");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003542MODULE_LICENSE("GPL");
Pierre Ossman67435272006-06-30 02:22:31 -07003543
Pierre Ossmandf673b22006-06-30 02:22:31 -07003544MODULE_PARM_DESC(debug_quirks, "Force certain quirks.");
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003545MODULE_PARM_DESC(debug_quirks2, "Force certain other quirks.");