blob: 03fbb36580f7f3d696a3f2c9f5b69e48128af4ea [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
Pierre Ossmanf9134312008-12-21 17:01:48 +010041#if defined(CONFIG_LEDS_CLASS) || (defined(CONFIG_LEDS_CLASS_MODULE) && \
42 defined(CONFIG_MMC_SDHCI_MODULE))
43#define SDHCI_USE_LEDS_CLASS
44#endif
45
Arindam Nathb513ea22011-05-05 12:19:04 +053046#define MAX_TUNING_LOOP 40
47
Pierre Ossmandf673b22006-06-30 02:22:31 -070048static unsigned int debug_quirks = 0;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +030049static unsigned int debug_quirks2;
Pierre Ossman67435272006-06-30 02:22:31 -070050
Pierre Ossmand129bce2006-03-24 03:18:17 -080051static void sdhci_finish_data(struct sdhci_host *);
52
Pierre Ossmand129bce2006-03-24 03:18:17 -080053static void sdhci_finish_command(struct sdhci_host *);
Girish K S069c9f12012-01-06 09:56:39 +053054static int sdhci_execute_tuning(struct mmc_host *mmc, u32 opcode);
Kevin Liu52983382013-01-31 11:31:37 +080055static void sdhci_enable_preset_value(struct sdhci_host *host, bool enable);
Scott Branden04e079cf2015-03-10 11:35:10 -070056static int sdhci_do_get_cd(struct sdhci_host *host);
Pierre Ossmand129bce2006-03-24 03:18:17 -080057
Rafael J. Wysocki162d6f92014-12-05 03:05:33 +010058#ifdef CONFIG_PM
Adrian Hunter66fd8ad2011-10-03 15:33:34 +030059static int sdhci_runtime_pm_get(struct sdhci_host *host);
60static int sdhci_runtime_pm_put(struct sdhci_host *host);
Adrian Hunterf0710a52013-05-06 12:17:32 +030061static void sdhci_runtime_pm_bus_on(struct sdhci_host *host);
62static void sdhci_runtime_pm_bus_off(struct sdhci_host *host);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +030063#else
64static inline int sdhci_runtime_pm_get(struct sdhci_host *host)
65{
66 return 0;
67}
68static inline int sdhci_runtime_pm_put(struct sdhci_host *host)
69{
70 return 0;
71}
Adrian Hunterf0710a52013-05-06 12:17:32 +030072static void sdhci_runtime_pm_bus_on(struct sdhci_host *host)
73{
74}
75static void sdhci_runtime_pm_bus_off(struct sdhci_host *host)
76{
77}
Adrian Hunter66fd8ad2011-10-03 15:33:34 +030078#endif
79
Pierre Ossmand129bce2006-03-24 03:18:17 -080080static void sdhci_dumpregs(struct sdhci_host *host)
81{
Girish K Sa3c76eb2011-10-11 11:44:09 +053082 pr_debug(DRIVER_NAME ": =========== REGISTER DUMP (%s)===========\n",
Philip Rakity412ab652010-09-22 15:25:13 -070083 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -080084
Girish K Sa3c76eb2011-10-11 11:44:09 +053085 pr_debug(DRIVER_NAME ": Sys addr: 0x%08x | Version: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030086 sdhci_readl(host, SDHCI_DMA_ADDRESS),
87 sdhci_readw(host, SDHCI_HOST_VERSION));
Girish K Sa3c76eb2011-10-11 11:44:09 +053088 pr_debug(DRIVER_NAME ": Blk size: 0x%08x | Blk cnt: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030089 sdhci_readw(host, SDHCI_BLOCK_SIZE),
90 sdhci_readw(host, SDHCI_BLOCK_COUNT));
Girish K Sa3c76eb2011-10-11 11:44:09 +053091 pr_debug(DRIVER_NAME ": Argument: 0x%08x | Trn mode: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030092 sdhci_readl(host, SDHCI_ARGUMENT),
93 sdhci_readw(host, SDHCI_TRANSFER_MODE));
Girish K Sa3c76eb2011-10-11 11:44:09 +053094 pr_debug(DRIVER_NAME ": Present: 0x%08x | Host ctl: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030095 sdhci_readl(host, SDHCI_PRESENT_STATE),
96 sdhci_readb(host, SDHCI_HOST_CONTROL));
Girish K Sa3c76eb2011-10-11 11:44:09 +053097 pr_debug(DRIVER_NAME ": Power: 0x%08x | Blk gap: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030098 sdhci_readb(host, SDHCI_POWER_CONTROL),
99 sdhci_readb(host, SDHCI_BLOCK_GAP_CONTROL));
Girish K Sa3c76eb2011-10-11 11:44:09 +0530100 pr_debug(DRIVER_NAME ": Wake-up: 0x%08x | Clock: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300101 sdhci_readb(host, SDHCI_WAKE_UP_CONTROL),
102 sdhci_readw(host, SDHCI_CLOCK_CONTROL));
Girish K Sa3c76eb2011-10-11 11:44:09 +0530103 pr_debug(DRIVER_NAME ": Timeout: 0x%08x | Int stat: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300104 sdhci_readb(host, SDHCI_TIMEOUT_CONTROL),
105 sdhci_readl(host, SDHCI_INT_STATUS));
Girish K Sa3c76eb2011-10-11 11:44:09 +0530106 pr_debug(DRIVER_NAME ": Int enab: 0x%08x | Sig enab: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300107 sdhci_readl(host, SDHCI_INT_ENABLE),
108 sdhci_readl(host, SDHCI_SIGNAL_ENABLE));
Girish K Sa3c76eb2011-10-11 11:44:09 +0530109 pr_debug(DRIVER_NAME ": AC12 err: 0x%08x | Slot int: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300110 sdhci_readw(host, SDHCI_ACMD12_ERR),
111 sdhci_readw(host, SDHCI_SLOT_INT_STATUS));
Girish K Sa3c76eb2011-10-11 11:44:09 +0530112 pr_debug(DRIVER_NAME ": Caps: 0x%08x | Caps_1: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300113 sdhci_readl(host, SDHCI_CAPABILITIES),
Philip Rakitye8120ad2010-11-30 00:55:23 -0500114 sdhci_readl(host, SDHCI_CAPABILITIES_1));
Girish K Sa3c76eb2011-10-11 11:44:09 +0530115 pr_debug(DRIVER_NAME ": Cmd: 0x%08x | Max curr: 0x%08x\n",
Philip Rakitye8120ad2010-11-30 00:55:23 -0500116 sdhci_readw(host, SDHCI_COMMAND),
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300117 sdhci_readl(host, SDHCI_MAX_CURRENT));
Girish K Sa3c76eb2011-10-11 11:44:09 +0530118 pr_debug(DRIVER_NAME ": Host ctl2: 0x%08x\n",
Arindam Nathf2119df2011-05-05 12:18:57 +0530119 sdhci_readw(host, SDHCI_HOST_CONTROL2));
Pierre Ossmand129bce2006-03-24 03:18:17 -0800120
Adrian Huntere57a5f62014-11-04 12:42:46 +0200121 if (host->flags & SDHCI_USE_ADMA) {
122 if (host->flags & SDHCI_USE_64_BIT_DMA)
123 pr_debug(DRIVER_NAME ": ADMA Err: 0x%08x | ADMA Ptr: 0x%08x%08x\n",
124 readl(host->ioaddr + SDHCI_ADMA_ERROR),
125 readl(host->ioaddr + SDHCI_ADMA_ADDRESS_HI),
126 readl(host->ioaddr + SDHCI_ADMA_ADDRESS));
127 else
128 pr_debug(DRIVER_NAME ": ADMA Err: 0x%08x | ADMA Ptr: 0x%08x\n",
129 readl(host->ioaddr + SDHCI_ADMA_ERROR),
130 readl(host->ioaddr + SDHCI_ADMA_ADDRESS));
131 }
Ben Dooksbe3f4ae2009-06-08 23:33:52 +0100132
Girish K Sa3c76eb2011-10-11 11:44:09 +0530133 pr_debug(DRIVER_NAME ": ===========================================\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800134}
135
136/*****************************************************************************\
137 * *
138 * Low level functions *
139 * *
140\*****************************************************************************/
141
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300142static void sdhci_set_card_detection(struct sdhci_host *host, bool enable)
143{
Russell King5b4f1f62014-04-25 12:57:02 +0100144 u32 present;
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300145
Adrian Hunterc79396c2011-12-27 15:48:42 +0200146 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) ||
Daniel Drake87b87a32012-04-10 00:14:20 +0100147 (host->mmc->caps & MMC_CAP_NONREMOVABLE))
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300148 return;
149
Russell King5b4f1f62014-04-25 12:57:02 +0100150 if (enable) {
151 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
152 SDHCI_CARD_PRESENT;
Shawn Guod25928d2011-06-21 22:41:48 +0800153
Russell King5b4f1f62014-04-25 12:57:02 +0100154 host->ier |= present ? SDHCI_INT_CARD_REMOVE :
155 SDHCI_INT_CARD_INSERT;
156 } else {
157 host->ier &= ~(SDHCI_INT_CARD_REMOVE | SDHCI_INT_CARD_INSERT);
158 }
Russell Kingb537f942014-04-25 12:56:01 +0100159
160 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
161 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300162}
163
164static void sdhci_enable_card_detection(struct sdhci_host *host)
165{
166 sdhci_set_card_detection(host, true);
167}
168
169static void sdhci_disable_card_detection(struct sdhci_host *host)
170{
171 sdhci_set_card_detection(host, false);
172}
173
Russell King03231f92014-04-25 12:57:12 +0100174void sdhci_reset(struct sdhci_host *host, u8 mask)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800175{
Pierre Ossmane16514d82006-06-30 02:22:24 -0700176 unsigned long timeout;
Philip Rakity393c1a32011-01-21 11:26:40 -0800177
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300178 sdhci_writeb(host, mask, SDHCI_SOFTWARE_RESET);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800179
Adrian Hunterf0710a52013-05-06 12:17:32 +0300180 if (mask & SDHCI_RESET_ALL) {
Pierre Ossmand129bce2006-03-24 03:18:17 -0800181 host->clock = 0;
Adrian Hunterf0710a52013-05-06 12:17:32 +0300182 /* Reset-all turns off SD Bus Power */
183 if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
184 sdhci_runtime_pm_bus_off(host);
185 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800186
Pierre Ossmane16514d82006-06-30 02:22:24 -0700187 /* Wait max 100 ms */
188 timeout = 100;
189
190 /* hw clears the bit when it's done */
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300191 while (sdhci_readb(host, SDHCI_SOFTWARE_RESET) & mask) {
Pierre Ossmane16514d82006-06-30 02:22:24 -0700192 if (timeout == 0) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530193 pr_err("%s: Reset 0x%x never completed.\n",
Pierre Ossmane16514d82006-06-30 02:22:24 -0700194 mmc_hostname(host->mmc), (int)mask);
195 sdhci_dumpregs(host);
196 return;
197 }
198 timeout--;
199 mdelay(1);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800200 }
Russell King03231f92014-04-25 12:57:12 +0100201}
202EXPORT_SYMBOL_GPL(sdhci_reset);
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300203
Russell King03231f92014-04-25 12:57:12 +0100204static void sdhci_do_reset(struct sdhci_host *host, u8 mask)
205{
206 if (host->quirks & SDHCI_QUIRK_NO_CARD_NO_RESET) {
Ivan T. Ivanov135b0a22015-07-06 15:16:21 +0300207 if (!sdhci_do_get_cd(host))
Russell King03231f92014-04-25 12:57:12 +0100208 return;
209 }
210
211 host->ops->reset(host, mask);
Philip Rakity393c1a32011-01-21 11:26:40 -0800212
Russell Kingda91a8f2014-04-25 13:00:12 +0100213 if (mask & SDHCI_RESET_ALL) {
214 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
215 if (host->ops->enable_dma)
216 host->ops->enable_dma(host);
217 }
218
219 /* Resetting the controller clears many */
220 host->preset_enabled = false;
Shaohui Xie3abc1e802011-12-29 16:33:00 +0800221 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800222}
223
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800224static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios);
225
226static void sdhci_init(struct sdhci_host *host, int soft)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800227{
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800228 if (soft)
Russell King03231f92014-04-25 12:57:12 +0100229 sdhci_do_reset(host, SDHCI_RESET_CMD|SDHCI_RESET_DATA);
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800230 else
Russell King03231f92014-04-25 12:57:12 +0100231 sdhci_do_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800232
Russell Kingb537f942014-04-25 12:56:01 +0100233 host->ier = SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT |
234 SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT |
235 SDHCI_INT_INDEX | SDHCI_INT_END_BIT | SDHCI_INT_CRC |
236 SDHCI_INT_TIMEOUT | SDHCI_INT_DATA_END |
237 SDHCI_INT_RESPONSE;
238
239 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
240 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800241
242 if (soft) {
243 /* force clock reconfiguration */
244 host->clock = 0;
245 sdhci_set_ios(host->mmc, &host->mmc->ios);
246 }
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300247}
Pierre Ossmand129bce2006-03-24 03:18:17 -0800248
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300249static void sdhci_reinit(struct sdhci_host *host)
250{
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800251 sdhci_init(host, 0);
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300252 sdhci_enable_card_detection(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800253}
254
255static void sdhci_activate_led(struct sdhci_host *host)
256{
257 u8 ctrl;
258
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300259 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800260 ctrl |= SDHCI_CTRL_LED;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300261 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800262}
263
264static void sdhci_deactivate_led(struct sdhci_host *host)
265{
266 u8 ctrl;
267
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300268 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800269 ctrl &= ~SDHCI_CTRL_LED;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300270 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800271}
272
Pierre Ossmanf9134312008-12-21 17:01:48 +0100273#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100274static void sdhci_led_control(struct led_classdev *led,
275 enum led_brightness brightness)
276{
277 struct sdhci_host *host = container_of(led, struct sdhci_host, led);
278 unsigned long flags;
279
280 spin_lock_irqsave(&host->lock, flags);
281
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300282 if (host->runtime_suspended)
283 goto out;
284
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100285 if (brightness == LED_OFF)
286 sdhci_deactivate_led(host);
287 else
288 sdhci_activate_led(host);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300289out:
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100290 spin_unlock_irqrestore(&host->lock, flags);
291}
292#endif
293
Pierre Ossmand129bce2006-03-24 03:18:17 -0800294/*****************************************************************************\
295 * *
296 * Core functions *
297 * *
298\*****************************************************************************/
299
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100300static void sdhci_read_block_pio(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800301{
Pierre Ossman76591502008-07-21 00:32:11 +0200302 unsigned long flags;
303 size_t blksize, len, chunk;
Steven Noonan7244b852008-10-01 01:50:25 -0700304 u32 uninitialized_var(scratch);
Pierre Ossman76591502008-07-21 00:32:11 +0200305 u8 *buf;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800306
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100307 DBG("PIO reading\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800308
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100309 blksize = host->data->blksz;
Pierre Ossman76591502008-07-21 00:32:11 +0200310 chunk = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800311
Pierre Ossman76591502008-07-21 00:32:11 +0200312 local_irq_save(flags);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800313
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100314 while (blksize) {
Fabio Estevambf3a35a2015-05-09 18:44:51 -0300315 BUG_ON(!sg_miter_next(&host->sg_miter));
Pierre Ossmand129bce2006-03-24 03:18:17 -0800316
Pierre Ossman76591502008-07-21 00:32:11 +0200317 len = min(host->sg_miter.length, blksize);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800318
Pierre Ossman76591502008-07-21 00:32:11 +0200319 blksize -= len;
320 host->sg_miter.consumed = len;
Alex Dubov14d836e2007-04-13 19:04:38 +0200321
Pierre Ossman76591502008-07-21 00:32:11 +0200322 buf = host->sg_miter.addr;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800323
Pierre Ossman76591502008-07-21 00:32:11 +0200324 while (len) {
325 if (chunk == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300326 scratch = sdhci_readl(host, SDHCI_BUFFER);
Pierre Ossman76591502008-07-21 00:32:11 +0200327 chunk = 4;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800328 }
Pierre Ossman76591502008-07-21 00:32:11 +0200329
330 *buf = scratch & 0xFF;
331
332 buf++;
333 scratch >>= 8;
334 chunk--;
335 len--;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800336 }
337 }
Pierre Ossman76591502008-07-21 00:32:11 +0200338
339 sg_miter_stop(&host->sg_miter);
340
341 local_irq_restore(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100342}
Pierre Ossmand129bce2006-03-24 03:18:17 -0800343
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100344static void sdhci_write_block_pio(struct sdhci_host *host)
345{
Pierre Ossman76591502008-07-21 00:32:11 +0200346 unsigned long flags;
347 size_t blksize, len, chunk;
348 u32 scratch;
349 u8 *buf;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100350
351 DBG("PIO writing\n");
352
353 blksize = host->data->blksz;
Pierre Ossman76591502008-07-21 00:32:11 +0200354 chunk = 0;
355 scratch = 0;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100356
Pierre Ossman76591502008-07-21 00:32:11 +0200357 local_irq_save(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100358
359 while (blksize) {
Fabio Estevambf3a35a2015-05-09 18:44:51 -0300360 BUG_ON(!sg_miter_next(&host->sg_miter));
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100361
Pierre Ossman76591502008-07-21 00:32:11 +0200362 len = min(host->sg_miter.length, blksize);
Alex Dubov14d836e2007-04-13 19:04:38 +0200363
Pierre Ossman76591502008-07-21 00:32:11 +0200364 blksize -= len;
365 host->sg_miter.consumed = len;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100366
Pierre Ossman76591502008-07-21 00:32:11 +0200367 buf = host->sg_miter.addr;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100368
Pierre Ossman76591502008-07-21 00:32:11 +0200369 while (len) {
370 scratch |= (u32)*buf << (chunk * 8);
371
372 buf++;
373 chunk++;
374 len--;
375
376 if ((chunk == 4) || ((len == 0) && (blksize == 0))) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300377 sdhci_writel(host, scratch, SDHCI_BUFFER);
Pierre Ossman76591502008-07-21 00:32:11 +0200378 chunk = 0;
379 scratch = 0;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100380 }
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100381 }
382 }
Pierre Ossman76591502008-07-21 00:32:11 +0200383
384 sg_miter_stop(&host->sg_miter);
385
386 local_irq_restore(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100387}
388
389static void sdhci_transfer_pio(struct sdhci_host *host)
390{
391 u32 mask;
392
393 BUG_ON(!host->data);
394
Pierre Ossman76591502008-07-21 00:32:11 +0200395 if (host->blocks == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100396 return;
397
398 if (host->data->flags & MMC_DATA_READ)
399 mask = SDHCI_DATA_AVAILABLE;
400 else
401 mask = SDHCI_SPACE_AVAILABLE;
402
Pierre Ossman4a3cba32008-07-29 00:11:16 +0200403 /*
404 * Some controllers (JMicron JMB38x) mess up the buffer bits
405 * for transfers < 4 bytes. As long as it is just one block,
406 * we can ignore the bits.
407 */
408 if ((host->quirks & SDHCI_QUIRK_BROKEN_SMALL_PIO) &&
409 (host->data->blocks == 1))
410 mask = ~0;
411
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300412 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Anton Vorontsov3e3bf202009-03-17 00:14:00 +0300413 if (host->quirks & SDHCI_QUIRK_PIO_NEEDS_DELAY)
414 udelay(100);
415
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100416 if (host->data->flags & MMC_DATA_READ)
417 sdhci_read_block_pio(host);
418 else
419 sdhci_write_block_pio(host);
420
Pierre Ossman76591502008-07-21 00:32:11 +0200421 host->blocks--;
422 if (host->blocks == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100423 break;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100424 }
425
426 DBG("PIO transfer complete.\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800427}
428
Russell King48857d92016-01-26 13:40:16 +0000429static int sdhci_pre_dma_transfer(struct sdhci_host *host,
Russell Kingc0999b72016-01-26 13:40:27 +0000430 struct mmc_data *data, int cookie)
Russell King48857d92016-01-26 13:40:16 +0000431{
432 int sg_count;
433
Russell King94538e52016-01-26 13:40:37 +0000434 /*
435 * If the data buffers are already mapped, return the previous
436 * dma_map_sg() result.
437 */
438 if (data->host_cookie == COOKIE_PRE_MAPPED)
Russell King48857d92016-01-26 13:40:16 +0000439 return data->sg_count;
Russell King48857d92016-01-26 13:40:16 +0000440
441 sg_count = dma_map_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
442 data->flags & MMC_DATA_WRITE ?
443 DMA_TO_DEVICE : DMA_FROM_DEVICE);
444
445 if (sg_count == 0)
446 return -ENOSPC;
447
448 data->sg_count = sg_count;
Russell Kingc0999b72016-01-26 13:40:27 +0000449 data->host_cookie = cookie;
Russell King48857d92016-01-26 13:40:16 +0000450
451 return sg_count;
452}
453
Pierre Ossman2134a922008-06-28 18:28:51 +0200454static char *sdhci_kmap_atomic(struct scatterlist *sg, unsigned long *flags)
455{
456 local_irq_save(*flags);
Cong Wang482fce92011-11-27 13:27:00 +0800457 return kmap_atomic(sg_page(sg)) + sg->offset;
Pierre Ossman2134a922008-06-28 18:28:51 +0200458}
459
460static void sdhci_kunmap_atomic(void *buffer, unsigned long *flags)
461{
Cong Wang482fce92011-11-27 13:27:00 +0800462 kunmap_atomic(buffer);
Pierre Ossman2134a922008-06-28 18:28:51 +0200463 local_irq_restore(*flags);
464}
465
Adrian Huntere57a5f62014-11-04 12:42:46 +0200466static void sdhci_adma_write_desc(struct sdhci_host *host, void *desc,
467 dma_addr_t addr, int len, unsigned cmd)
Ben Dooks118cd172010-03-05 13:43:26 -0800468{
Adrian Huntere57a5f62014-11-04 12:42:46 +0200469 struct sdhci_adma2_64_desc *dma_desc = desc;
Ben Dooks118cd172010-03-05 13:43:26 -0800470
Adrian Huntere57a5f62014-11-04 12:42:46 +0200471 /* 32-bit and 64-bit descriptors have these members in same position */
Adrian Hunter05452302014-11-04 12:42:45 +0200472 dma_desc->cmd = cpu_to_le16(cmd);
473 dma_desc->len = cpu_to_le16(len);
Adrian Huntere57a5f62014-11-04 12:42:46 +0200474 dma_desc->addr_lo = cpu_to_le32((u32)addr);
475
476 if (host->flags & SDHCI_USE_64_BIT_DMA)
477 dma_desc->addr_hi = cpu_to_le32((u64)addr >> 32);
Ben Dooks118cd172010-03-05 13:43:26 -0800478}
479
Adrian Hunterb5ffa672014-11-04 12:42:40 +0200480static void sdhci_adma_mark_end(void *desc)
481{
Adrian Huntere57a5f62014-11-04 12:42:46 +0200482 struct sdhci_adma2_64_desc *dma_desc = desc;
Adrian Hunterb5ffa672014-11-04 12:42:40 +0200483
Adrian Huntere57a5f62014-11-04 12:42:46 +0200484 /* 32-bit and 64-bit descriptors have 'cmd' in same position */
Adrian Hunter05452302014-11-04 12:42:45 +0200485 dma_desc->cmd |= cpu_to_le16(ADMA2_END);
Adrian Hunterb5ffa672014-11-04 12:42:40 +0200486}
487
Russell King60c64762016-01-26 13:40:22 +0000488static void sdhci_adma_table_pre(struct sdhci_host *host,
489 struct mmc_data *data, int sg_count)
Pierre Ossman2134a922008-06-28 18:28:51 +0200490{
Pierre Ossman2134a922008-06-28 18:28:51 +0200491 struct scatterlist *sg;
Pierre Ossman2134a922008-06-28 18:28:51 +0200492 unsigned long flags;
Russell Kingacc3ad12016-01-26 13:40:00 +0000493 dma_addr_t addr, align_addr;
494 void *desc, *align;
495 char *buffer;
496 int len, offset, i;
Pierre Ossman2134a922008-06-28 18:28:51 +0200497
498 /*
499 * The spec does not specify endianness of descriptor table.
500 * We currently guess that it is LE.
501 */
502
Russell King60c64762016-01-26 13:40:22 +0000503 host->sg_count = sg_count;
Pierre Ossman2134a922008-06-28 18:28:51 +0200504
Adrian Hunter4efaa6f2014-11-04 12:42:39 +0200505 desc = host->adma_table;
Pierre Ossman2134a922008-06-28 18:28:51 +0200506 align = host->align_buffer;
507
508 align_addr = host->align_addr;
509
510 for_each_sg(data->sg, sg, host->sg_count, i) {
511 addr = sg_dma_address(sg);
512 len = sg_dma_len(sg);
513
514 /*
Russell Kingacc3ad12016-01-26 13:40:00 +0000515 * The SDHCI specification states that ADMA addresses must
516 * be 32-bit aligned. If they aren't, then we use a bounce
517 * buffer for the (up to three) bytes that screw up the
Pierre Ossman2134a922008-06-28 18:28:51 +0200518 * alignment.
519 */
Adrian Hunter04a5ae62015-11-26 14:00:49 +0200520 offset = (SDHCI_ADMA2_ALIGN - (addr & SDHCI_ADMA2_MASK)) &
521 SDHCI_ADMA2_MASK;
Pierre Ossman2134a922008-06-28 18:28:51 +0200522 if (offset) {
523 if (data->flags & MMC_DATA_WRITE) {
524 buffer = sdhci_kmap_atomic(sg, &flags);
525 memcpy(align, buffer, offset);
526 sdhci_kunmap_atomic(buffer, &flags);
527 }
528
Ben Dooks118cd172010-03-05 13:43:26 -0800529 /* tran, valid */
Adrian Huntere57a5f62014-11-04 12:42:46 +0200530 sdhci_adma_write_desc(host, desc, align_addr, offset,
Adrian Hunter739d46d2014-11-04 12:42:44 +0200531 ADMA2_TRAN_VALID);
Pierre Ossman2134a922008-06-28 18:28:51 +0200532
533 BUG_ON(offset > 65536);
534
Adrian Hunter04a5ae62015-11-26 14:00:49 +0200535 align += SDHCI_ADMA2_ALIGN;
536 align_addr += SDHCI_ADMA2_ALIGN;
Pierre Ossman2134a922008-06-28 18:28:51 +0200537
Adrian Hunter76fe3792014-11-04 12:42:42 +0200538 desc += host->desc_sz;
Pierre Ossman2134a922008-06-28 18:28:51 +0200539
540 addr += offset;
541 len -= offset;
542 }
543
Pierre Ossman2134a922008-06-28 18:28:51 +0200544 BUG_ON(len > 65536);
545
Adrian Hunter347ea322015-11-26 14:00:48 +0200546 if (len) {
547 /* tran, valid */
548 sdhci_adma_write_desc(host, desc, addr, len,
549 ADMA2_TRAN_VALID);
550 desc += host->desc_sz;
551 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200552
553 /*
554 * If this triggers then we have a calculation bug
555 * somewhere. :/
556 */
Adrian Hunter76fe3792014-11-04 12:42:42 +0200557 WARN_ON((desc - host->adma_table) >= host->adma_table_sz);
Pierre Ossman2134a922008-06-28 18:28:51 +0200558 }
559
Thomas Abraham70764a92010-05-26 14:42:04 -0700560 if (host->quirks & SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC) {
Russell Kingacc3ad12016-01-26 13:40:00 +0000561 /* Mark the last descriptor as the terminating descriptor */
Adrian Hunter4efaa6f2014-11-04 12:42:39 +0200562 if (desc != host->adma_table) {
Adrian Hunter76fe3792014-11-04 12:42:42 +0200563 desc -= host->desc_sz;
Adrian Hunterb5ffa672014-11-04 12:42:40 +0200564 sdhci_adma_mark_end(desc);
Thomas Abraham70764a92010-05-26 14:42:04 -0700565 }
566 } else {
Russell Kingacc3ad12016-01-26 13:40:00 +0000567 /* Add a terminating entry - nop, end, valid */
Adrian Huntere57a5f62014-11-04 12:42:46 +0200568 sdhci_adma_write_desc(host, desc, 0, 0, ADMA2_NOP_END_VALID);
Thomas Abraham70764a92010-05-26 14:42:04 -0700569 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200570}
571
572static void sdhci_adma_table_post(struct sdhci_host *host,
573 struct mmc_data *data)
574{
Pierre Ossman2134a922008-06-28 18:28:51 +0200575 struct scatterlist *sg;
576 int i, size;
Adrian Hunter1c3d5f62014-11-04 12:42:41 +0200577 void *align;
Pierre Ossman2134a922008-06-28 18:28:51 +0200578 char *buffer;
579 unsigned long flags;
580
Russell King47fa9612016-01-26 13:40:06 +0000581 if (data->flags & MMC_DATA_READ) {
582 bool has_unaligned = false;
Russell Kingde0b65a2014-04-25 12:58:29 +0100583
Russell King47fa9612016-01-26 13:40:06 +0000584 /* Do a quick scan of the SG list for any unaligned mappings */
585 for_each_sg(data->sg, sg, host->sg_count, i)
Adrian Hunter04a5ae62015-11-26 14:00:49 +0200586 if (sg_dma_address(sg) & SDHCI_ADMA2_MASK) {
Russell King47fa9612016-01-26 13:40:06 +0000587 has_unaligned = true;
588 break;
589 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200590
Russell King47fa9612016-01-26 13:40:06 +0000591 if (has_unaligned) {
592 dma_sync_sg_for_cpu(mmc_dev(host->mmc), data->sg,
Russell Kingf55c98f2016-01-26 13:40:11 +0000593 data->sg_len, DMA_FROM_DEVICE);
Pierre Ossman2134a922008-06-28 18:28:51 +0200594
Russell King47fa9612016-01-26 13:40:06 +0000595 align = host->align_buffer;
596
597 for_each_sg(data->sg, sg, host->sg_count, i) {
598 if (sg_dma_address(sg) & SDHCI_ADMA2_MASK) {
599 size = SDHCI_ADMA2_ALIGN -
600 (sg_dma_address(sg) & SDHCI_ADMA2_MASK);
601
602 buffer = sdhci_kmap_atomic(sg, &flags);
603 memcpy(buffer, align, size);
604 sdhci_kunmap_atomic(buffer, &flags);
605
606 align += SDHCI_ADMA2_ALIGN;
607 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200608 }
609 }
610 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200611}
612
Andrei Warkentina3c77782011-04-11 16:13:42 -0500613static u8 sdhci_calc_timeout(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800614{
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700615 u8 count;
Andrei Warkentina3c77782011-04-11 16:13:42 -0500616 struct mmc_data *data = cmd->data;
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700617 unsigned target_timeout, current_timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800618
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200619 /*
620 * If the host controller provides us with an incorrect timeout
621 * value, just skip the check and use 0xE. The hardware may take
622 * longer to time out, but that's much better than having a too-short
623 * timeout value.
624 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +0200625 if (host->quirks & SDHCI_QUIRK_BROKEN_TIMEOUT_VAL)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200626 return 0xE;
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200627
Andrei Warkentina3c77782011-04-11 16:13:42 -0500628 /* Unspecified timeout, assume max */
Ulf Hansson1d4d7742014-01-08 15:06:08 +0100629 if (!data && !cmd->busy_timeout)
Andrei Warkentina3c77782011-04-11 16:13:42 -0500630 return 0xE;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800631
Andrei Warkentina3c77782011-04-11 16:13:42 -0500632 /* timeout in us */
633 if (!data)
Ulf Hansson1d4d7742014-01-08 15:06:08 +0100634 target_timeout = cmd->busy_timeout * 1000;
Andy Shevchenko78a2ca22011-08-03 18:35:59 +0300635 else {
Russell Kingfafcfda2016-01-26 13:40:58 +0000636 target_timeout = DIV_ROUND_UP(data->timeout_ns, 1000);
Russell King7f055382016-01-26 13:41:04 +0000637 if (host->clock && data->timeout_clks) {
638 unsigned long long val;
639
640 /*
641 * data->timeout_clks is in units of clock cycles.
642 * host->clock is in Hz. target_timeout is in us.
643 * Hence, us = 1000000 * cycles / Hz. Round up.
644 */
645 val = 1000000 * data->timeout_clks;
646 if (do_div(val, host->clock))
647 target_timeout++;
648 target_timeout += val;
649 }
Andy Shevchenko78a2ca22011-08-03 18:35:59 +0300650 }
Anton Vorontsov81b39802009-09-22 16:45:13 -0700651
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700652 /*
653 * Figure out needed cycles.
654 * We do this in steps in order to fit inside a 32 bit int.
655 * The first step is the minimum timeout, which will have a
656 * minimum resolution of 6 bits:
657 * (1) 2^13*1000 > 2^22,
658 * (2) host->timeout_clk < 2^16
659 * =>
660 * (1) / (2) > 2^6
661 */
662 count = 0;
663 current_timeout = (1 << 13) * 1000 / host->timeout_clk;
664 while (current_timeout < target_timeout) {
665 count++;
666 current_timeout <<= 1;
667 if (count >= 0xF)
668 break;
669 }
670
671 if (count >= 0xF) {
Chris Ball09eeff52012-06-01 10:39:45 -0400672 DBG("%s: Too large timeout 0x%x requested for CMD%d!\n",
673 mmc_hostname(host->mmc), count, cmd->opcode);
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700674 count = 0xE;
675 }
676
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200677 return count;
678}
679
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300680static void sdhci_set_transfer_irqs(struct sdhci_host *host)
681{
682 u32 pio_irqs = SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL;
683 u32 dma_irqs = SDHCI_INT_DMA_END | SDHCI_INT_ADMA_ERROR;
684
685 if (host->flags & SDHCI_REQ_USE_DMA)
Russell Kingb537f942014-04-25 12:56:01 +0100686 host->ier = (host->ier & ~pio_irqs) | dma_irqs;
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300687 else
Russell Kingb537f942014-04-25 12:56:01 +0100688 host->ier = (host->ier & ~dma_irqs) | pio_irqs;
689
690 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
691 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300692}
693
Aisheng Dongb45e6682014-08-27 15:26:29 +0800694static void sdhci_set_timeout(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200695{
696 u8 count;
Aisheng Dongb45e6682014-08-27 15:26:29 +0800697
698 if (host->ops->set_timeout) {
699 host->ops->set_timeout(host, cmd);
700 } else {
701 count = sdhci_calc_timeout(host, cmd);
702 sdhci_writeb(host, count, SDHCI_TIMEOUT_CONTROL);
703 }
704}
705
706static void sdhci_prepare_data(struct sdhci_host *host, struct mmc_command *cmd)
707{
Pierre Ossman2134a922008-06-28 18:28:51 +0200708 u8 ctrl;
Andrei Warkentina3c77782011-04-11 16:13:42 -0500709 struct mmc_data *data = cmd->data;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200710
711 WARN_ON(host->data);
712
Aisheng Dongb45e6682014-08-27 15:26:29 +0800713 if (data || (cmd->flags & MMC_RSP_BUSY))
714 sdhci_set_timeout(host, cmd);
Andrei Warkentina3c77782011-04-11 16:13:42 -0500715
716 if (!data)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200717 return;
718
719 /* Sanity checks */
720 BUG_ON(data->blksz * data->blocks > 524288);
721 BUG_ON(data->blksz > host->mmc->max_blk_size);
722 BUG_ON(data->blocks > 65535);
723
724 host->data = data;
725 host->data_early = 0;
Mikko Vinnif6a03cb2011-04-12 09:36:18 -0400726 host->data->bytes_xfered = 0;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200727
Russell Kingfce14422016-01-26 13:41:20 +0000728 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossman2134a922008-06-28 18:28:51 +0200729 struct scatterlist *sg;
Russell Kingdf953922016-01-26 13:41:14 +0000730 unsigned int length_mask, offset_mask;
Russell Kinga0eaf0f2016-01-26 13:41:09 +0000731 int i;
Pierre Ossman2134a922008-06-28 18:28:51 +0200732
Russell Kingfce14422016-01-26 13:41:20 +0000733 host->flags |= SDHCI_REQ_USE_DMA;
734
735 /*
736 * FIXME: This doesn't account for merging when mapping the
737 * scatterlist.
738 *
739 * The assumption here being that alignment and lengths are
740 * the same after DMA mapping to device address space.
741 */
Russell Kinga0eaf0f2016-01-26 13:41:09 +0000742 length_mask = 0;
Russell Kingdf953922016-01-26 13:41:14 +0000743 offset_mask = 0;
Pierre Ossman2134a922008-06-28 18:28:51 +0200744 if (host->flags & SDHCI_USE_ADMA) {
Russell Kingdf953922016-01-26 13:41:14 +0000745 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE) {
Russell Kinga0eaf0f2016-01-26 13:41:09 +0000746 length_mask = 3;
Russell Kingdf953922016-01-26 13:41:14 +0000747 /*
748 * As we use up to 3 byte chunks to work
749 * around alignment problems, we need to
750 * check the offset as well.
751 */
752 offset_mask = 3;
753 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200754 } else {
755 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE)
Russell Kinga0eaf0f2016-01-26 13:41:09 +0000756 length_mask = 3;
Russell Kingdf953922016-01-26 13:41:14 +0000757 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR)
758 offset_mask = 3;
Pierre Ossman2134a922008-06-28 18:28:51 +0200759 }
760
Russell Kingdf953922016-01-26 13:41:14 +0000761 if (unlikely(length_mask | offset_mask)) {
Pierre Ossman2134a922008-06-28 18:28:51 +0200762 for_each_sg(data->sg, sg, data->sg_len, i) {
Russell Kinga0eaf0f2016-01-26 13:41:09 +0000763 if (sg->length & length_mask) {
Marek Vasut2e4456f2015-11-18 10:47:02 +0100764 DBG("Reverting to PIO because of transfer size (%d)\n",
Russell Kinga0eaf0f2016-01-26 13:41:09 +0000765 sg->length);
Pierre Ossman2134a922008-06-28 18:28:51 +0200766 host->flags &= ~SDHCI_REQ_USE_DMA;
767 break;
768 }
Russell Kinga0eaf0f2016-01-26 13:41:09 +0000769 if (sg->offset & offset_mask) {
Marek Vasut2e4456f2015-11-18 10:47:02 +0100770 DBG("Reverting to PIO because of bad alignment\n");
Pierre Ossman2134a922008-06-28 18:28:51 +0200771 host->flags &= ~SDHCI_REQ_USE_DMA;
772 break;
773 }
774 }
775 }
776 }
777
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200778 if (host->flags & SDHCI_REQ_USE_DMA) {
Russell Kingc0999b72016-01-26 13:40:27 +0000779 int sg_cnt = sdhci_pre_dma_transfer(host, data, COOKIE_MAPPED);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200780
Russell King60c64762016-01-26 13:40:22 +0000781 if (sg_cnt <= 0) {
782 /*
783 * This only happens when someone fed
784 * us an invalid request.
785 */
786 WARN_ON(1);
787 host->flags &= ~SDHCI_REQ_USE_DMA;
788 } else if (host->flags & SDHCI_USE_ADMA) {
789 sdhci_adma_table_pre(host, data, sg_cnt);
790
791 sdhci_writel(host, host->adma_addr, SDHCI_ADMA_ADDRESS);
792 if (host->flags & SDHCI_USE_64_BIT_DMA)
793 sdhci_writel(host,
794 (u64)host->adma_addr >> 32,
795 SDHCI_ADMA_ADDRESS_HI);
796 } else {
797 WARN_ON(sg_cnt != 1);
798 sdhci_writel(host, sg_dma_address(data->sg),
799 SDHCI_DMA_ADDRESS);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200800 }
801 }
802
Pierre Ossman2134a922008-06-28 18:28:51 +0200803 /*
804 * Always adjust the DMA selection as some controllers
805 * (e.g. JMicron) can't do PIO properly when the selection
806 * is ADMA.
807 */
808 if (host->version >= SDHCI_SPEC_200) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300809 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossman2134a922008-06-28 18:28:51 +0200810 ctrl &= ~SDHCI_CTRL_DMA_MASK;
811 if ((host->flags & SDHCI_REQ_USE_DMA) &&
Adrian Huntere57a5f62014-11-04 12:42:46 +0200812 (host->flags & SDHCI_USE_ADMA)) {
813 if (host->flags & SDHCI_USE_64_BIT_DMA)
814 ctrl |= SDHCI_CTRL_ADMA64;
815 else
816 ctrl |= SDHCI_CTRL_ADMA32;
817 } else {
Pierre Ossman2134a922008-06-28 18:28:51 +0200818 ctrl |= SDHCI_CTRL_SDMA;
Adrian Huntere57a5f62014-11-04 12:42:46 +0200819 }
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300820 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100821 }
822
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200823 if (!(host->flags & SDHCI_REQ_USE_DMA)) {
Sebastian Andrzej Siewiorda60a912009-06-18 09:33:32 +0200824 int flags;
825
826 flags = SG_MITER_ATOMIC;
827 if (host->data->flags & MMC_DATA_READ)
828 flags |= SG_MITER_TO_SG;
829 else
830 flags |= SG_MITER_FROM_SG;
831 sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
Pierre Ossman76591502008-07-21 00:32:11 +0200832 host->blocks = data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800833 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700834
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300835 sdhci_set_transfer_irqs(host);
836
Mikko Vinnif6a03cb2011-04-12 09:36:18 -0400837 /* Set the DMA boundary value and block size */
838 sdhci_writew(host, SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG,
839 data->blksz), SDHCI_BLOCK_SIZE);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300840 sdhci_writew(host, data->blocks, SDHCI_BLOCK_COUNT);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700841}
842
843static void sdhci_set_transfer_mode(struct sdhci_host *host,
Andrei Warkentine89d4562011-05-23 15:06:37 -0500844 struct mmc_command *cmd)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700845{
Vincent Yangd3fc5d72015-01-20 16:05:17 +0800846 u16 mode = 0;
Andrei Warkentine89d4562011-05-23 15:06:37 -0500847 struct mmc_data *data = cmd->data;
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700848
Dong Aisheng2b558c12013-10-30 22:09:48 +0800849 if (data == NULL) {
Vincent Wan9b8ffea2014-11-05 14:09:00 +0800850 if (host->quirks2 &
851 SDHCI_QUIRK2_CLEAR_TRANSFERMODE_REG_BEFORE_CMD) {
852 sdhci_writew(host, 0x0, SDHCI_TRANSFER_MODE);
853 } else {
Dong Aisheng2b558c12013-10-30 22:09:48 +0800854 /* clear Auto CMD settings for no data CMDs */
Vincent Wan9b8ffea2014-11-05 14:09:00 +0800855 mode = sdhci_readw(host, SDHCI_TRANSFER_MODE);
856 sdhci_writew(host, mode & ~(SDHCI_TRNS_AUTO_CMD12 |
Dong Aisheng2b558c12013-10-30 22:09:48 +0800857 SDHCI_TRNS_AUTO_CMD23), SDHCI_TRANSFER_MODE);
Vincent Wan9b8ffea2014-11-05 14:09:00 +0800858 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700859 return;
Dong Aisheng2b558c12013-10-30 22:09:48 +0800860 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700861
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200862 WARN_ON(!host->data);
863
Vincent Yangd3fc5d72015-01-20 16:05:17 +0800864 if (!(host->quirks2 & SDHCI_QUIRK2_SUPPORT_SINGLE))
865 mode = SDHCI_TRNS_BLK_CNT_EN;
866
Andrei Warkentine89d4562011-05-23 15:06:37 -0500867 if (mmc_op_multi(cmd->opcode) || data->blocks > 1) {
Vincent Yangd3fc5d72015-01-20 16:05:17 +0800868 mode = SDHCI_TRNS_BLK_CNT_EN | SDHCI_TRNS_MULTI;
Andrei Warkentine89d4562011-05-23 15:06:37 -0500869 /*
870 * If we are sending CMD23, CMD12 never gets sent
871 * on successful completion (so no Auto-CMD12).
872 */
Corneliu Doban85cc1c32015-02-09 16:06:29 -0800873 if (!host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD12) &&
874 (cmd->opcode != SD_IO_RW_EXTENDED))
Andrei Warkentine89d4562011-05-23 15:06:37 -0500875 mode |= SDHCI_TRNS_AUTO_CMD12;
Andrei Warkentin8edf63712011-05-23 15:06:39 -0500876 else if (host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD23)) {
877 mode |= SDHCI_TRNS_AUTO_CMD23;
878 sdhci_writel(host, host->mrq->sbc->arg, SDHCI_ARGUMENT2);
879 }
Jerry Huangc4512f72010-08-10 18:01:59 -0700880 }
Andrei Warkentin8edf63712011-05-23 15:06:39 -0500881
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700882 if (data->flags & MMC_DATA_READ)
883 mode |= SDHCI_TRNS_READ;
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100884 if (host->flags & SDHCI_REQ_USE_DMA)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700885 mode |= SDHCI_TRNS_DMA;
886
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300887 sdhci_writew(host, mode, SDHCI_TRANSFER_MODE);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800888}
889
890static void sdhci_finish_data(struct sdhci_host *host)
891{
892 struct mmc_data *data;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800893
894 BUG_ON(!host->data);
895
896 data = host->data;
897 host->data = NULL;
898
Russell Kingadd89132016-01-26 13:40:42 +0000899 if ((host->flags & (SDHCI_REQ_USE_DMA | SDHCI_USE_ADMA)) ==
900 (SDHCI_REQ_USE_DMA | SDHCI_USE_ADMA))
901 sdhci_adma_table_post(host, data);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800902
903 /*
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200904 * The specification states that the block count register must
905 * be updated, but it does not specify at what point in the
906 * data flow. That makes the register entirely useless to read
907 * back so we have to assume that nothing made it to the card
908 * in the event of an error.
Pierre Ossmand129bce2006-03-24 03:18:17 -0800909 */
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200910 if (data->error)
911 data->bytes_xfered = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800912 else
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200913 data->bytes_xfered = data->blksz * data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800914
Andrei Warkentine89d4562011-05-23 15:06:37 -0500915 /*
916 * Need to send CMD12 if -
917 * a) open-ended multiblock transfer (no CMD23)
918 * b) error in multiblock transfer
919 */
920 if (data->stop &&
921 (data->error ||
922 !host->mrq->sbc)) {
923
Pierre Ossmand129bce2006-03-24 03:18:17 -0800924 /*
925 * The controller needs a reset of internal state machines
926 * upon error conditions.
927 */
Pierre Ossman17b04292007-07-22 22:18:46 +0200928 if (data->error) {
Russell King03231f92014-04-25 12:57:12 +0100929 sdhci_do_reset(host, SDHCI_RESET_CMD);
930 sdhci_do_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800931 }
932
933 sdhci_send_command(host, data->stop);
934 } else
935 tasklet_schedule(&host->finish_tasklet);
936}
937
Dong Aishengc0e551292013-09-13 19:11:31 +0800938void sdhci_send_command(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800939{
940 int flags;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -0700941 u32 mask;
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700942 unsigned long timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800943
944 WARN_ON(host->cmd);
945
Russell King96776202016-01-26 13:39:34 +0000946 /* Initially, a command has no error */
947 cmd->error = 0;
948
Pierre Ossmand129bce2006-03-24 03:18:17 -0800949 /* Wait max 10 ms */
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700950 timeout = 10;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -0700951
952 mask = SDHCI_CMD_INHIBIT;
953 if ((cmd->data != NULL) || (cmd->flags & MMC_RSP_BUSY))
954 mask |= SDHCI_DATA_INHIBIT;
955
956 /* We shouldn't wait for data inihibit for stop commands, even
957 though they might use busy signaling */
958 if (host->mrq->data && (cmd == host->mrq->data->stop))
959 mask &= ~SDHCI_DATA_INHIBIT;
960
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300961 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700962 if (timeout == 0) {
Marek Vasut2e4456f2015-11-18 10:47:02 +0100963 pr_err("%s: Controller never released inhibit bit(s).\n",
964 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -0800965 sdhci_dumpregs(host);
Pierre Ossman17b04292007-07-22 22:18:46 +0200966 cmd->error = -EIO;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800967 tasklet_schedule(&host->finish_tasklet);
968 return;
969 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700970 timeout--;
971 mdelay(1);
972 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800973
Adrian Hunter3e1a6892013-11-14 10:16:20 +0200974 timeout = jiffies;
Ulf Hansson1d4d7742014-01-08 15:06:08 +0100975 if (!cmd->data && cmd->busy_timeout > 9000)
976 timeout += DIV_ROUND_UP(cmd->busy_timeout, 1000) * HZ + HZ;
Adrian Hunter3e1a6892013-11-14 10:16:20 +0200977 else
978 timeout += 10 * HZ;
979 mod_timer(&host->timer, timeout);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800980
981 host->cmd = cmd;
Chanho Mine99783a2014-08-30 12:40:40 +0900982 host->busy_handle = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800983
Andrei Warkentina3c77782011-04-11 16:13:42 -0500984 sdhci_prepare_data(host, cmd);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800985
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300986 sdhci_writel(host, cmd->arg, SDHCI_ARGUMENT);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800987
Andrei Warkentine89d4562011-05-23 15:06:37 -0500988 sdhci_set_transfer_mode(host, cmd);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700989
Pierre Ossmand129bce2006-03-24 03:18:17 -0800990 if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530991 pr_err("%s: Unsupported response type!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -0800992 mmc_hostname(host->mmc));
Pierre Ossman17b04292007-07-22 22:18:46 +0200993 cmd->error = -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800994 tasklet_schedule(&host->finish_tasklet);
995 return;
996 }
997
998 if (!(cmd->flags & MMC_RSP_PRESENT))
999 flags = SDHCI_CMD_RESP_NONE;
1000 else if (cmd->flags & MMC_RSP_136)
1001 flags = SDHCI_CMD_RESP_LONG;
1002 else if (cmd->flags & MMC_RSP_BUSY)
1003 flags = SDHCI_CMD_RESP_SHORT_BUSY;
1004 else
1005 flags = SDHCI_CMD_RESP_SHORT;
1006
1007 if (cmd->flags & MMC_RSP_CRC)
1008 flags |= SDHCI_CMD_CRC;
1009 if (cmd->flags & MMC_RSP_OPCODE)
1010 flags |= SDHCI_CMD_INDEX;
Arindam Nathb513ea22011-05-05 12:19:04 +05301011
1012 /* CMD19 is special in that the Data Present Select should be set */
Girish K S069c9f12012-01-06 09:56:39 +05301013 if (cmd->data || cmd->opcode == MMC_SEND_TUNING_BLOCK ||
1014 cmd->opcode == MMC_SEND_TUNING_BLOCK_HS200)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001015 flags |= SDHCI_CMD_DATA;
1016
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001017 sdhci_writew(host, SDHCI_MAKE_CMD(cmd->opcode, flags), SDHCI_COMMAND);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001018}
Dong Aishengc0e551292013-09-13 19:11:31 +08001019EXPORT_SYMBOL_GPL(sdhci_send_command);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001020
1021static void sdhci_finish_command(struct sdhci_host *host)
1022{
1023 int i;
1024
1025 BUG_ON(host->cmd == NULL);
1026
1027 if (host->cmd->flags & MMC_RSP_PRESENT) {
1028 if (host->cmd->flags & MMC_RSP_136) {
1029 /* CRC is stripped so we need to do some shifting. */
1030 for (i = 0;i < 4;i++) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001031 host->cmd->resp[i] = sdhci_readl(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001032 SDHCI_RESPONSE + (3-i)*4) << 8;
1033 if (i != 3)
1034 host->cmd->resp[i] |=
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001035 sdhci_readb(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001036 SDHCI_RESPONSE + (3-i)*4-1);
1037 }
1038 } else {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001039 host->cmd->resp[0] = sdhci_readl(host, SDHCI_RESPONSE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001040 }
1041 }
1042
Andrei Warkentine89d4562011-05-23 15:06:37 -05001043 /* Finished CMD23, now send actual command. */
1044 if (host->cmd == host->mrq->sbc) {
1045 host->cmd = NULL;
1046 sdhci_send_command(host, host->mrq->cmd);
1047 } else {
Pierre Ossmane538fbe2007-08-12 16:46:32 +02001048
Andrei Warkentine89d4562011-05-23 15:06:37 -05001049 /* Processed actual command. */
1050 if (host->data && host->data_early)
1051 sdhci_finish_data(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001052
Andrei Warkentine89d4562011-05-23 15:06:37 -05001053 if (!host->cmd->data)
1054 tasklet_schedule(&host->finish_tasklet);
1055
1056 host->cmd = NULL;
1057 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001058}
1059
Kevin Liu52983382013-01-31 11:31:37 +08001060static u16 sdhci_get_preset_value(struct sdhci_host *host)
1061{
Russell Kingd975f122014-04-25 12:59:31 +01001062 u16 preset = 0;
Kevin Liu52983382013-01-31 11:31:37 +08001063
Russell Kingd975f122014-04-25 12:59:31 +01001064 switch (host->timing) {
1065 case MMC_TIMING_UHS_SDR12:
Kevin Liu52983382013-01-31 11:31:37 +08001066 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR12);
1067 break;
Russell Kingd975f122014-04-25 12:59:31 +01001068 case MMC_TIMING_UHS_SDR25:
Kevin Liu52983382013-01-31 11:31:37 +08001069 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR25);
1070 break;
Russell Kingd975f122014-04-25 12:59:31 +01001071 case MMC_TIMING_UHS_SDR50:
Kevin Liu52983382013-01-31 11:31:37 +08001072 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR50);
1073 break;
Russell Kingd975f122014-04-25 12:59:31 +01001074 case MMC_TIMING_UHS_SDR104:
1075 case MMC_TIMING_MMC_HS200:
Kevin Liu52983382013-01-31 11:31:37 +08001076 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR104);
1077 break;
Russell Kingd975f122014-04-25 12:59:31 +01001078 case MMC_TIMING_UHS_DDR50:
Jisheng Zhang0dafa602015-08-18 16:21:39 +08001079 case MMC_TIMING_MMC_DDR52:
Kevin Liu52983382013-01-31 11:31:37 +08001080 preset = sdhci_readw(host, SDHCI_PRESET_FOR_DDR50);
1081 break;
Adrian Huntere9fb05d2014-11-06 15:19:06 +02001082 case MMC_TIMING_MMC_HS400:
1083 preset = sdhci_readw(host, SDHCI_PRESET_FOR_HS400);
1084 break;
Kevin Liu52983382013-01-31 11:31:37 +08001085 default:
1086 pr_warn("%s: Invalid UHS-I mode selected\n",
1087 mmc_hostname(host->mmc));
1088 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR12);
1089 break;
1090 }
1091 return preset;
1092}
1093
Russell King17710592014-04-25 12:58:55 +01001094void sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001095{
Arindam Nathc3ed3872011-05-05 12:19:06 +05301096 int div = 0; /* Initialized for compiler warning */
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001097 int real_div = div, clk_mul = 1;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301098 u16 clk = 0;
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001099 unsigned long timeout;
ludovic.desroches@atmel.com54971592015-07-29 16:22:46 +02001100 bool switch_base_clk = false;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001101
Russell King1650d0c2014-04-25 12:58:50 +01001102 host->mmc->actual_clock = 0;
1103
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001104 sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
ludovic.desroches@atmel.comaf951762015-09-17 10:16:19 +02001105 if (host->quirks2 & SDHCI_QUIRK2_NEED_DELAY_AFTER_INT_CLK_RST)
1106 mdelay(1);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001107
1108 if (clock == 0)
Russell King373073e2014-04-25 12:58:45 +01001109 return;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001110
Zhangfei Gao85105c52010-08-06 07:10:01 +08001111 if (host->version >= SDHCI_SPEC_300) {
Russell Kingda91a8f2014-04-25 13:00:12 +01001112 if (host->preset_enabled) {
Kevin Liu52983382013-01-31 11:31:37 +08001113 u16 pre_val;
1114
1115 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1116 pre_val = sdhci_get_preset_value(host);
1117 div = (pre_val & SDHCI_PRESET_SDCLK_FREQ_MASK)
1118 >> SDHCI_PRESET_SDCLK_FREQ_SHIFT;
1119 if (host->clk_mul &&
1120 (pre_val & SDHCI_PRESET_CLKGEN_SEL_MASK)) {
1121 clk = SDHCI_PROG_CLOCK_MODE;
1122 real_div = div + 1;
1123 clk_mul = host->clk_mul;
1124 } else {
1125 real_div = max_t(int, 1, div << 1);
1126 }
1127 goto clock_set;
1128 }
1129
Arindam Nathc3ed3872011-05-05 12:19:06 +05301130 /*
1131 * Check if the Host Controller supports Programmable Clock
1132 * Mode.
1133 */
1134 if (host->clk_mul) {
Kevin Liu52983382013-01-31 11:31:37 +08001135 for (div = 1; div <= 1024; div++) {
1136 if ((host->max_clk * host->clk_mul / div)
1137 <= clock)
1138 break;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001139 }
ludovic.desroches@atmel.com54971592015-07-29 16:22:46 +02001140 if ((host->max_clk * host->clk_mul / div) <= clock) {
1141 /*
1142 * Set Programmable Clock Mode in the Clock
1143 * Control register.
1144 */
1145 clk = SDHCI_PROG_CLOCK_MODE;
1146 real_div = div;
1147 clk_mul = host->clk_mul;
1148 div--;
1149 } else {
1150 /*
1151 * Divisor can be too small to reach clock
1152 * speed requirement. Then use the base clock.
1153 */
1154 switch_base_clk = true;
1155 }
1156 }
1157
1158 if (!host->clk_mul || switch_base_clk) {
Arindam Nathc3ed3872011-05-05 12:19:06 +05301159 /* Version 3.00 divisors must be a multiple of 2. */
1160 if (host->max_clk <= clock)
1161 div = 1;
1162 else {
1163 for (div = 2; div < SDHCI_MAX_DIV_SPEC_300;
1164 div += 2) {
1165 if ((host->max_clk / div) <= clock)
1166 break;
1167 }
1168 }
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001169 real_div = div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301170 div >>= 1;
Suneel Garapatid1955c32015-06-09 13:01:50 +05301171 if ((host->quirks2 & SDHCI_QUIRK2_CLOCK_DIV_ZERO_BROKEN)
1172 && !div && host->max_clk <= 25000000)
1173 div = 1;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001174 }
1175 } else {
1176 /* Version 2.00 divisors must be a power of 2. */
Zhangfei Gao03975262010-09-20 15:15:18 -04001177 for (div = 1; div < SDHCI_MAX_DIV_SPEC_200; div *= 2) {
Zhangfei Gao85105c52010-08-06 07:10:01 +08001178 if ((host->max_clk / div) <= clock)
1179 break;
1180 }
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001181 real_div = div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301182 div >>= 1;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001183 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001184
Kevin Liu52983382013-01-31 11:31:37 +08001185clock_set:
Aisheng Dong03d6f5f2014-08-27 15:26:32 +08001186 if (real_div)
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001187 host->mmc->actual_clock = (host->max_clk * clk_mul) / real_div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301188 clk |= (div & SDHCI_DIV_MASK) << SDHCI_DIVIDER_SHIFT;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001189 clk |= ((div & SDHCI_DIV_HI_MASK) >> SDHCI_DIV_MASK_LEN)
1190 << SDHCI_DIVIDER_HI_SHIFT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001191 clk |= SDHCI_CLOCK_INT_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001192 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001193
Chris Ball27f6cb12009-09-22 16:45:31 -07001194 /* Wait max 20 ms */
1195 timeout = 20;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001196 while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001197 & SDHCI_CLOCK_INT_STABLE)) {
1198 if (timeout == 0) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01001199 pr_err("%s: Internal clock never stabilised.\n",
1200 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001201 sdhci_dumpregs(host);
1202 return;
1203 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001204 timeout--;
1205 mdelay(1);
1206 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001207
1208 clk |= SDHCI_CLOCK_CARD_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001209 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001210}
Russell King17710592014-04-25 12:58:55 +01001211EXPORT_SYMBOL_GPL(sdhci_set_clock);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001212
Russell King24fbb3c2014-04-25 13:00:06 +01001213static void sdhci_set_power(struct sdhci_host *host, unsigned char mode,
1214 unsigned short vdd)
Pierre Ossman146ad662006-06-30 02:22:23 -07001215{
Tim Kryger3a48edc2014-06-13 10:13:56 -07001216 struct mmc_host *mmc = host->mmc;
Giuseppe Cavallaro83642482010-09-28 10:41:28 +02001217 u8 pwr = 0;
Pierre Ossman146ad662006-06-30 02:22:23 -07001218
Russell King24fbb3c2014-04-25 13:00:06 +01001219 if (mode != MMC_POWER_OFF) {
1220 switch (1 << vdd) {
Pierre Ossmanae628902009-05-03 20:45:03 +02001221 case MMC_VDD_165_195:
1222 pwr = SDHCI_POWER_180;
1223 break;
1224 case MMC_VDD_29_30:
1225 case MMC_VDD_30_31:
1226 pwr = SDHCI_POWER_300;
1227 break;
1228 case MMC_VDD_32_33:
1229 case MMC_VDD_33_34:
1230 pwr = SDHCI_POWER_330;
1231 break;
1232 default:
Adrian Hunter9d5de932015-11-26 14:00:46 +02001233 WARN(1, "%s: Invalid vdd %#x\n",
1234 mmc_hostname(host->mmc), vdd);
1235 break;
Pierre Ossmanae628902009-05-03 20:45:03 +02001236 }
1237 }
1238
1239 if (host->pwr == pwr)
Russell Kinge921a8b2014-04-25 13:00:01 +01001240 return;
Pierre Ossman146ad662006-06-30 02:22:23 -07001241
Pierre Ossmanae628902009-05-03 20:45:03 +02001242 host->pwr = pwr;
1243
1244 if (pwr == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001245 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Adrian Hunterf0710a52013-05-06 12:17:32 +03001246 if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
1247 sdhci_runtime_pm_bus_off(host);
Russell King24fbb3c2014-04-25 13:00:06 +01001248 vdd = 0;
Russell Kinge921a8b2014-04-25 13:00:01 +01001249 } else {
1250 /*
1251 * Spec says that we should clear the power reg before setting
1252 * a new value. Some controllers don't seem to like this though.
1253 */
1254 if (!(host->quirks & SDHCI_QUIRK_SINGLE_POWER_WRITE))
1255 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Darren Salt9e9dc5f2007-01-27 15:32:31 +01001256
Russell Kinge921a8b2014-04-25 13:00:01 +01001257 /*
1258 * At least the Marvell CaFe chip gets confused if we set the
1259 * voltage and set turn on power at the same time, so set the
1260 * voltage first.
1261 */
1262 if (host->quirks & SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER)
1263 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
Pierre Ossman146ad662006-06-30 02:22:23 -07001264
Russell Kinge921a8b2014-04-25 13:00:01 +01001265 pwr |= SDHCI_POWER_ON;
1266
Pierre Ossmanae628902009-05-03 20:45:03 +02001267 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
1268
Russell Kinge921a8b2014-04-25 13:00:01 +01001269 if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
1270 sdhci_runtime_pm_bus_on(host);
Andres Salomone08c1692008-07-04 10:00:03 -07001271
Russell Kinge921a8b2014-04-25 13:00:01 +01001272 /*
1273 * Some controllers need an extra 10ms delay of 10ms before
1274 * they can apply clock after applying power
1275 */
1276 if (host->quirks & SDHCI_QUIRK_DELAY_AFTER_POWER)
1277 mdelay(10);
1278 }
Jisheng Zhang918f4cb2015-12-11 21:36:29 +08001279
1280 if (!IS_ERR(mmc->supply.vmmc)) {
1281 spin_unlock_irq(&host->lock);
1282 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, vdd);
1283 spin_lock_irq(&host->lock);
1284 }
Pierre Ossman146ad662006-06-30 02:22:23 -07001285}
1286
Pierre Ossmand129bce2006-03-24 03:18:17 -08001287/*****************************************************************************\
1288 * *
1289 * MMC callbacks *
1290 * *
1291\*****************************************************************************/
1292
1293static void sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1294{
1295 struct sdhci_host *host;
Shawn Guo505a8682012-12-11 15:23:42 +08001296 int present;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001297 unsigned long flags;
1298
1299 host = mmc_priv(mmc);
1300
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001301 sdhci_runtime_pm_get(host);
1302
Scott Branden04e079cf2015-03-10 11:35:10 -07001303 /* Firstly check card presence */
Adrian Hunter8d28b7a2016-02-09 16:12:36 +02001304 present = mmc->ops->get_cd(mmc);
Krzysztof Kozlowski28367662015-01-05 10:50:15 +01001305
Pierre Ossmand129bce2006-03-24 03:18:17 -08001306 spin_lock_irqsave(&host->lock, flags);
1307
1308 WARN_ON(host->mrq != NULL);
1309
Pierre Ossmanf9134312008-12-21 17:01:48 +01001310#ifndef SDHCI_USE_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08001311 sdhci_activate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01001312#endif
Andrei Warkentine89d4562011-05-23 15:06:37 -05001313
1314 /*
1315 * Ensure we don't send the STOP for non-SET_BLOCK_COUNTED
1316 * requests if Auto-CMD12 is enabled.
1317 */
1318 if (!mrq->sbc && (host->flags & SDHCI_AUTO_CMD12)) {
Jerry Huangc4512f72010-08-10 18:01:59 -07001319 if (mrq->stop) {
1320 mrq->data->stop = NULL;
1321 mrq->stop = NULL;
1322 }
1323 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001324
1325 host->mrq = mrq;
1326
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001327 if (!present || host->flags & SDHCI_DEVICE_DEAD) {
Pierre Ossman17b04292007-07-22 22:18:46 +02001328 host->mrq->cmd->error = -ENOMEDIUM;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001329 tasklet_schedule(&host->finish_tasklet);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301330 } else {
Andrei Warkentin8edf63712011-05-23 15:06:39 -05001331 if (mrq->sbc && !(host->flags & SDHCI_AUTO_CMD23))
Andrei Warkentine89d4562011-05-23 15:06:37 -05001332 sdhci_send_command(host, mrq->sbc);
1333 else
1334 sdhci_send_command(host, mrq->cmd);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301335 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001336
Pierre Ossman5f25a662006-10-04 02:15:39 -07001337 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001338 spin_unlock_irqrestore(&host->lock, flags);
1339}
1340
Russell King2317f562014-04-25 12:57:07 +01001341void sdhci_set_bus_width(struct sdhci_host *host, int width)
1342{
1343 u8 ctrl;
1344
1345 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
1346 if (width == MMC_BUS_WIDTH_8) {
1347 ctrl &= ~SDHCI_CTRL_4BITBUS;
1348 if (host->version >= SDHCI_SPEC_300)
1349 ctrl |= SDHCI_CTRL_8BITBUS;
1350 } else {
1351 if (host->version >= SDHCI_SPEC_300)
1352 ctrl &= ~SDHCI_CTRL_8BITBUS;
1353 if (width == MMC_BUS_WIDTH_4)
1354 ctrl |= SDHCI_CTRL_4BITBUS;
1355 else
1356 ctrl &= ~SDHCI_CTRL_4BITBUS;
1357 }
1358 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1359}
1360EXPORT_SYMBOL_GPL(sdhci_set_bus_width);
1361
Russell King96d7b782014-04-25 12:59:26 +01001362void sdhci_set_uhs_signaling(struct sdhci_host *host, unsigned timing)
1363{
1364 u16 ctrl_2;
1365
1366 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1367 /* Select Bus Speed Mode for host */
1368 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
1369 if ((timing == MMC_TIMING_MMC_HS200) ||
1370 (timing == MMC_TIMING_UHS_SDR104))
1371 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
1372 else if (timing == MMC_TIMING_UHS_SDR12)
1373 ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
1374 else if (timing == MMC_TIMING_UHS_SDR25)
1375 ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
1376 else if (timing == MMC_TIMING_UHS_SDR50)
1377 ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
1378 else if ((timing == MMC_TIMING_UHS_DDR50) ||
1379 (timing == MMC_TIMING_MMC_DDR52))
1380 ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
Adrian Huntere9fb05d2014-11-06 15:19:06 +02001381 else if (timing == MMC_TIMING_MMC_HS400)
1382 ctrl_2 |= SDHCI_CTRL_HS400; /* Non-standard */
Russell King96d7b782014-04-25 12:59:26 +01001383 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
1384}
1385EXPORT_SYMBOL_GPL(sdhci_set_uhs_signaling);
1386
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001387static void sdhci_do_set_ios(struct sdhci_host *host, struct mmc_ios *ios)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001388{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001389 unsigned long flags;
1390 u8 ctrl;
Tim Kryger3a48edc2014-06-13 10:13:56 -07001391 struct mmc_host *mmc = host->mmc;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001392
Pierre Ossmand129bce2006-03-24 03:18:17 -08001393 spin_lock_irqsave(&host->lock, flags);
1394
Adrian Hunterceb61432011-12-27 15:48:41 +02001395 if (host->flags & SDHCI_DEVICE_DEAD) {
1396 spin_unlock_irqrestore(&host->lock, flags);
Tim Kryger3a48edc2014-06-13 10:13:56 -07001397 if (!IS_ERR(mmc->supply.vmmc) &&
1398 ios->power_mode == MMC_POWER_OFF)
Markus Mayer4e743f12014-07-03 13:27:42 -07001399 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0);
Adrian Hunterceb61432011-12-27 15:48:41 +02001400 return;
1401 }
Pierre Ossman1e728592008-04-16 19:13:13 +02001402
Pierre Ossmand129bce2006-03-24 03:18:17 -08001403 /*
1404 * Reset the chip on each power off.
1405 * Should clear out any weird states.
1406 */
1407 if (ios->power_mode == MMC_POWER_OFF) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001408 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001409 sdhci_reinit(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001410 }
1411
Kevin Liu52983382013-01-31 11:31:37 +08001412 if (host->version >= SDHCI_SPEC_300 &&
Dong Aisheng372c4632013-10-18 19:48:50 +08001413 (ios->power_mode == MMC_POWER_UP) &&
1414 !(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN))
Kevin Liu52983382013-01-31 11:31:37 +08001415 sdhci_enable_preset_value(host, false);
1416
Russell King373073e2014-04-25 12:58:45 +01001417 if (!ios->clock || ios->clock != host->clock) {
Russell King17710592014-04-25 12:58:55 +01001418 host->ops->set_clock(host, ios->clock);
Russell King373073e2014-04-25 12:58:45 +01001419 host->clock = ios->clock;
Aisheng Dong03d6f5f2014-08-27 15:26:32 +08001420
1421 if (host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK &&
1422 host->clock) {
1423 host->timeout_clk = host->mmc->actual_clock ?
1424 host->mmc->actual_clock / 1000 :
1425 host->clock / 1000;
1426 host->mmc->max_busy_timeout =
1427 host->ops->get_max_timeout_count ?
1428 host->ops->get_max_timeout_count(host) :
1429 1 << 27;
1430 host->mmc->max_busy_timeout /= host->timeout_clk;
1431 }
Russell King373073e2014-04-25 12:58:45 +01001432 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001433
Russell King24fbb3c2014-04-25 13:00:06 +01001434 sdhci_set_power(host, ios->power_mode, ios->vdd);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001435
Philip Rakity643a81f2010-09-23 08:24:32 -07001436 if (host->ops->platform_send_init_74_clocks)
1437 host->ops->platform_send_init_74_clocks(host, ios->power_mode);
1438
Russell King2317f562014-04-25 12:57:07 +01001439 host->ops->set_bus_width(host, ios->bus_width);
Philip Rakity15ec4462010-11-19 16:48:39 -05001440
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001441 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001442
Philip Rakity3ab9c8d2010-10-06 11:57:23 -07001443 if ((ios->timing == MMC_TIMING_SD_HS ||
1444 ios->timing == MMC_TIMING_MMC_HS)
1445 && !(host->quirks & SDHCI_QUIRK_NO_HISPD_BIT))
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001446 ctrl |= SDHCI_CTRL_HISPD;
1447 else
1448 ctrl &= ~SDHCI_CTRL_HISPD;
1449
Arindam Nathd6d50a12011-05-05 12:18:59 +05301450 if (host->version >= SDHCI_SPEC_300) {
Arindam Nath49c468f2011-05-05 12:19:01 +05301451 u16 clk, ctrl_2;
Arindam Nath49c468f2011-05-05 12:19:01 +05301452
1453 /* In case of UHS-I modes, set High Speed Enable */
Adrian Huntere9fb05d2014-11-06 15:19:06 +02001454 if ((ios->timing == MMC_TIMING_MMC_HS400) ||
1455 (ios->timing == MMC_TIMING_MMC_HS200) ||
Seungwon Jeonbb8175a2014-03-14 21:12:48 +09001456 (ios->timing == MMC_TIMING_MMC_DDR52) ||
Girish K S069c9f12012-01-06 09:56:39 +05301457 (ios->timing == MMC_TIMING_UHS_SDR50) ||
Arindam Nath49c468f2011-05-05 12:19:01 +05301458 (ios->timing == MMC_TIMING_UHS_SDR104) ||
1459 (ios->timing == MMC_TIMING_UHS_DDR50) ||
Alexander Elbsdd8df172012-01-03 23:26:53 -05001460 (ios->timing == MMC_TIMING_UHS_SDR25))
Arindam Nath49c468f2011-05-05 12:19:01 +05301461 ctrl |= SDHCI_CTRL_HISPD;
Arindam Nathd6d50a12011-05-05 12:18:59 +05301462
Russell Kingda91a8f2014-04-25 13:00:12 +01001463 if (!host->preset_enabled) {
Arindam Nath758535c2011-05-05 12:19:00 +05301464 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301465 /*
1466 * We only need to set Driver Strength if the
1467 * preset value enable is not set.
1468 */
Russell Kingda91a8f2014-04-25 13:00:12 +01001469 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301470 ctrl_2 &= ~SDHCI_CTRL_DRV_TYPE_MASK;
1471 if (ios->drv_type == MMC_SET_DRIVER_TYPE_A)
1472 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_A;
Petri Gynther43e943a2015-05-20 14:35:00 -07001473 else if (ios->drv_type == MMC_SET_DRIVER_TYPE_B)
1474 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_B;
Arindam Nathd6d50a12011-05-05 12:18:59 +05301475 else if (ios->drv_type == MMC_SET_DRIVER_TYPE_C)
1476 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_C;
Petri Gynther43e943a2015-05-20 14:35:00 -07001477 else if (ios->drv_type == MMC_SET_DRIVER_TYPE_D)
1478 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_D;
1479 else {
Marek Vasut2e4456f2015-11-18 10:47:02 +01001480 pr_warn("%s: invalid driver type, default to driver type B\n",
1481 mmc_hostname(mmc));
Petri Gynther43e943a2015-05-20 14:35:00 -07001482 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_B;
1483 }
Arindam Nathd6d50a12011-05-05 12:18:59 +05301484
1485 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
Arindam Nath758535c2011-05-05 12:19:00 +05301486 } else {
1487 /*
1488 * According to SDHC Spec v3.00, if the Preset Value
1489 * Enable in the Host Control 2 register is set, we
1490 * need to reset SD Clock Enable before changing High
1491 * Speed Enable to avoid generating clock gliches.
1492 */
Arindam Nath758535c2011-05-05 12:19:00 +05301493
1494 /* Reset SD Clock Enable */
1495 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1496 clk &= ~SDHCI_CLOCK_CARD_EN;
1497 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1498
1499 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1500
1501 /* Re-enable SD Clock */
Russell King17710592014-04-25 12:58:55 +01001502 host->ops->set_clock(host, host->clock);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301503 }
Arindam Nath49c468f2011-05-05 12:19:01 +05301504
Arindam Nath49c468f2011-05-05 12:19:01 +05301505 /* Reset SD Clock Enable */
1506 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1507 clk &= ~SDHCI_CLOCK_CARD_EN;
1508 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1509
Russell King96d7b782014-04-25 12:59:26 +01001510 host->ops->set_uhs_signaling(host, ios->timing);
Russell Kingd975f122014-04-25 12:59:31 +01001511 host->timing = ios->timing;
Arindam Nath49c468f2011-05-05 12:19:01 +05301512
Kevin Liu52983382013-01-31 11:31:37 +08001513 if (!(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN) &&
1514 ((ios->timing == MMC_TIMING_UHS_SDR12) ||
1515 (ios->timing == MMC_TIMING_UHS_SDR25) ||
1516 (ios->timing == MMC_TIMING_UHS_SDR50) ||
1517 (ios->timing == MMC_TIMING_UHS_SDR104) ||
Jisheng Zhang0dafa602015-08-18 16:21:39 +08001518 (ios->timing == MMC_TIMING_UHS_DDR50) ||
1519 (ios->timing == MMC_TIMING_MMC_DDR52))) {
Kevin Liu52983382013-01-31 11:31:37 +08001520 u16 preset;
1521
1522 sdhci_enable_preset_value(host, true);
1523 preset = sdhci_get_preset_value(host);
1524 ios->drv_type = (preset & SDHCI_PRESET_DRV_MASK)
1525 >> SDHCI_PRESET_DRV_SHIFT;
1526 }
1527
Arindam Nath49c468f2011-05-05 12:19:01 +05301528 /* Re-enable SD Clock */
Russell King17710592014-04-25 12:58:55 +01001529 host->ops->set_clock(host, host->clock);
Arindam Nath758535c2011-05-05 12:19:00 +05301530 } else
1531 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301532
Leandro Dorileob8352262007-07-25 23:47:04 +02001533 /*
1534 * Some (ENE) controllers go apeshit on some ios operation,
1535 * signalling timeout and CRC errors even on CMD0. Resetting
1536 * it on each ios seems to solve the problem.
1537 */
Mohammad Jamalc63705e2015-01-13 20:47:24 +05301538 if (host->quirks & SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS)
Russell King03231f92014-04-25 12:57:12 +01001539 sdhci_do_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
Leandro Dorileob8352262007-07-25 23:47:04 +02001540
Pierre Ossman5f25a662006-10-04 02:15:39 -07001541 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001542 spin_unlock_irqrestore(&host->lock, flags);
1543}
1544
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001545static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1546{
1547 struct sdhci_host *host = mmc_priv(mmc);
1548
1549 sdhci_runtime_pm_get(host);
1550 sdhci_do_set_ios(host, ios);
1551 sdhci_runtime_pm_put(host);
1552}
1553
Kevin Liu94144a42013-02-28 17:35:53 +08001554static int sdhci_do_get_cd(struct sdhci_host *host)
1555{
1556 int gpio_cd = mmc_gpio_get_cd(host->mmc);
1557
1558 if (host->flags & SDHCI_DEVICE_DEAD)
1559 return 0;
1560
Ivan T. Ivanov88af5652015-07-06 15:16:19 +03001561 /* If nonremovable, assume that the card is always present. */
1562 if (host->mmc->caps & MMC_CAP_NONREMOVABLE)
Kevin Liu94144a42013-02-28 17:35:53 +08001563 return 1;
1564
Ivan T. Ivanov88af5652015-07-06 15:16:19 +03001565 /*
1566 * Try slot gpio detect, if defined it take precedence
1567 * over build in controller functionality
1568 */
Kevin Liu94144a42013-02-28 17:35:53 +08001569 if (!IS_ERR_VALUE(gpio_cd))
1570 return !!gpio_cd;
1571
Ivan T. Ivanov88af5652015-07-06 15:16:19 +03001572 /* If polling, assume that the card is always present. */
1573 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
1574 return 1;
1575
Kevin Liu94144a42013-02-28 17:35:53 +08001576 /* Host native card detect */
1577 return !!(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT);
1578}
1579
1580static int sdhci_get_cd(struct mmc_host *mmc)
1581{
1582 struct sdhci_host *host = mmc_priv(mmc);
1583 int ret;
1584
1585 sdhci_runtime_pm_get(host);
1586 ret = sdhci_do_get_cd(host);
1587 sdhci_runtime_pm_put(host);
1588 return ret;
1589}
1590
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001591static int sdhci_check_ro(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001592{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001593 unsigned long flags;
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001594 int is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001595
Pierre Ossmand129bce2006-03-24 03:18:17 -08001596 spin_lock_irqsave(&host->lock, flags);
1597
Pierre Ossman1e728592008-04-16 19:13:13 +02001598 if (host->flags & SDHCI_DEVICE_DEAD)
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001599 is_readonly = 0;
1600 else if (host->ops->get_ro)
1601 is_readonly = host->ops->get_ro(host);
Pierre Ossman1e728592008-04-16 19:13:13 +02001602 else
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001603 is_readonly = !(sdhci_readl(host, SDHCI_PRESENT_STATE)
1604 & SDHCI_WRITE_PROTECT);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001605
1606 spin_unlock_irqrestore(&host->lock, flags);
1607
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001608 /* This quirk needs to be replaced by a callback-function later */
1609 return host->quirks & SDHCI_QUIRK_INVERTED_WRITE_PROTECT ?
1610 !is_readonly : is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001611}
1612
Takashi Iwai82b0e232011-04-21 20:26:38 +02001613#define SAMPLE_COUNT 5
1614
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001615static int sdhci_do_get_ro(struct sdhci_host *host)
Takashi Iwai82b0e232011-04-21 20:26:38 +02001616{
Takashi Iwai82b0e232011-04-21 20:26:38 +02001617 int i, ro_count;
1618
Takashi Iwai82b0e232011-04-21 20:26:38 +02001619 if (!(host->quirks & SDHCI_QUIRK_UNSTABLE_RO_DETECT))
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001620 return sdhci_check_ro(host);
Takashi Iwai82b0e232011-04-21 20:26:38 +02001621
1622 ro_count = 0;
1623 for (i = 0; i < SAMPLE_COUNT; i++) {
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001624 if (sdhci_check_ro(host)) {
Takashi Iwai82b0e232011-04-21 20:26:38 +02001625 if (++ro_count > SAMPLE_COUNT / 2)
1626 return 1;
1627 }
1628 msleep(30);
1629 }
1630 return 0;
1631}
1632
Adrian Hunter20758b62011-08-29 16:42:12 +03001633static void sdhci_hw_reset(struct mmc_host *mmc)
1634{
1635 struct sdhci_host *host = mmc_priv(mmc);
1636
1637 if (host->ops && host->ops->hw_reset)
1638 host->ops->hw_reset(host);
1639}
1640
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001641static int sdhci_get_ro(struct mmc_host *mmc)
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001642{
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001643 struct sdhci_host *host = mmc_priv(mmc);
1644 int ret;
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001645
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001646 sdhci_runtime_pm_get(host);
1647 ret = sdhci_do_get_ro(host);
1648 sdhci_runtime_pm_put(host);
1649 return ret;
1650}
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001651
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001652static void sdhci_enable_sdio_irq_nolock(struct sdhci_host *host, int enable)
1653{
Russell Kingbe138552014-04-25 12:55:56 +01001654 if (!(host->flags & SDHCI_DEVICE_DEAD)) {
Russell Kingef104332014-04-25 12:55:41 +01001655 if (enable)
Russell Kingb537f942014-04-25 12:56:01 +01001656 host->ier |= SDHCI_INT_CARD_INT;
Russell Kingef104332014-04-25 12:55:41 +01001657 else
Russell Kingb537f942014-04-25 12:56:01 +01001658 host->ier &= ~SDHCI_INT_CARD_INT;
1659
1660 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
1661 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Russell Kingef104332014-04-25 12:55:41 +01001662 mmiowb();
1663 }
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001664}
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001665
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001666static void sdhci_enable_sdio_irq(struct mmc_host *mmc, int enable)
1667{
1668 struct sdhci_host *host = mmc_priv(mmc);
1669 unsigned long flags;
1670
Russell Kingef104332014-04-25 12:55:41 +01001671 sdhci_runtime_pm_get(host);
1672
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001673 spin_lock_irqsave(&host->lock, flags);
Russell Kingef104332014-04-25 12:55:41 +01001674 if (enable)
1675 host->flags |= SDHCI_SDIO_IRQ_ENABLED;
1676 else
1677 host->flags &= ~SDHCI_SDIO_IRQ_ENABLED;
1678
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001679 sdhci_enable_sdio_irq_nolock(host, enable);
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001680 spin_unlock_irqrestore(&host->lock, flags);
Russell Kingef104332014-04-25 12:55:41 +01001681
1682 sdhci_runtime_pm_put(host);
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001683}
1684
Philip Rakity6231f3d2012-07-23 15:56:23 -07001685static int sdhci_do_start_signal_voltage_switch(struct sdhci_host *host,
Fabio Estevam21f59982013-02-14 10:35:03 -02001686 struct mmc_ios *ios)
Philip Rakity6231f3d2012-07-23 15:56:23 -07001687{
Tim Kryger3a48edc2014-06-13 10:13:56 -07001688 struct mmc_host *mmc = host->mmc;
Philip Rakity6231f3d2012-07-23 15:56:23 -07001689 u16 ctrl;
Kevin Liu20b92a32012-12-17 19:29:26 +08001690 int ret;
Philip Rakity6231f3d2012-07-23 15:56:23 -07001691
1692 /*
1693 * Signal Voltage Switching is only applicable for Host Controllers
1694 * v3.00 and above.
1695 */
1696 if (host->version < SDHCI_SPEC_300)
1697 return 0;
1698
Philip Rakity6231f3d2012-07-23 15:56:23 -07001699 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
Kevin Liu20b92a32012-12-17 19:29:26 +08001700
Fabio Estevam21f59982013-02-14 10:35:03 -02001701 switch (ios->signal_voltage) {
Kevin Liu20b92a32012-12-17 19:29:26 +08001702 case MMC_SIGNAL_VOLTAGE_330:
1703 /* Set 1.8V Signal Enable in the Host Control2 register to 0 */
1704 ctrl &= ~SDHCI_CTRL_VDD_180;
1705 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1706
Tim Kryger3a48edc2014-06-13 10:13:56 -07001707 if (!IS_ERR(mmc->supply.vqmmc)) {
1708 ret = regulator_set_voltage(mmc->supply.vqmmc, 2700000,
1709 3600000);
Kevin Liu20b92a32012-12-17 19:29:26 +08001710 if (ret) {
Joe Perches66061102014-09-12 14:56:56 -07001711 pr_warn("%s: Switching to 3.3V signalling voltage failed\n",
1712 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001713 return -EIO;
1714 }
1715 }
1716 /* Wait for 5ms */
1717 usleep_range(5000, 5500);
1718
1719 /* 3.3V regulator output should be stable within 5 ms */
1720 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1721 if (!(ctrl & SDHCI_CTRL_VDD_180))
1722 return 0;
1723
Joe Perches66061102014-09-12 14:56:56 -07001724 pr_warn("%s: 3.3V regulator output did not became stable\n",
1725 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001726
1727 return -EAGAIN;
1728 case MMC_SIGNAL_VOLTAGE_180:
Tim Kryger3a48edc2014-06-13 10:13:56 -07001729 if (!IS_ERR(mmc->supply.vqmmc)) {
1730 ret = regulator_set_voltage(mmc->supply.vqmmc,
Kevin Liu20b92a32012-12-17 19:29:26 +08001731 1700000, 1950000);
1732 if (ret) {
Joe Perches66061102014-09-12 14:56:56 -07001733 pr_warn("%s: Switching to 1.8V signalling voltage failed\n",
1734 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001735 return -EIO;
1736 }
1737 }
1738
1739 /*
1740 * Enable 1.8V Signal Enable in the Host Control2
1741 * register
1742 */
1743 ctrl |= SDHCI_CTRL_VDD_180;
1744 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1745
Vincent Yang9d967a62015-01-20 16:05:15 +08001746 /* Some controller need to do more when switching */
1747 if (host->ops->voltage_switch)
1748 host->ops->voltage_switch(host);
1749
Kevin Liu20b92a32012-12-17 19:29:26 +08001750 /* 1.8V regulator output should be stable within 5 ms */
1751 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1752 if (ctrl & SDHCI_CTRL_VDD_180)
1753 return 0;
1754
Joe Perches66061102014-09-12 14:56:56 -07001755 pr_warn("%s: 1.8V regulator output did not became stable\n",
1756 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001757
1758 return -EAGAIN;
1759 case MMC_SIGNAL_VOLTAGE_120:
Tim Kryger3a48edc2014-06-13 10:13:56 -07001760 if (!IS_ERR(mmc->supply.vqmmc)) {
1761 ret = regulator_set_voltage(mmc->supply.vqmmc, 1100000,
1762 1300000);
Kevin Liu20b92a32012-12-17 19:29:26 +08001763 if (ret) {
Joe Perches66061102014-09-12 14:56:56 -07001764 pr_warn("%s: Switching to 1.2V signalling voltage failed\n",
1765 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001766 return -EIO;
1767 }
1768 }
1769 return 0;
1770 default:
Arindam Nathf2119df2011-05-05 12:18:57 +05301771 /* No signal voltage switch required */
1772 return 0;
Kevin Liu20b92a32012-12-17 19:29:26 +08001773 }
Arindam Nathf2119df2011-05-05 12:18:57 +05301774}
1775
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001776static int sdhci_start_signal_voltage_switch(struct mmc_host *mmc,
Fabio Estevam21f59982013-02-14 10:35:03 -02001777 struct mmc_ios *ios)
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001778{
1779 struct sdhci_host *host = mmc_priv(mmc);
1780 int err;
1781
1782 if (host->version < SDHCI_SPEC_300)
1783 return 0;
1784 sdhci_runtime_pm_get(host);
Fabio Estevam21f59982013-02-14 10:35:03 -02001785 err = sdhci_do_start_signal_voltage_switch(host, ios);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001786 sdhci_runtime_pm_put(host);
1787 return err;
1788}
1789
Kevin Liu20b92a32012-12-17 19:29:26 +08001790static int sdhci_card_busy(struct mmc_host *mmc)
1791{
1792 struct sdhci_host *host = mmc_priv(mmc);
1793 u32 present_state;
1794
1795 sdhci_runtime_pm_get(host);
1796 /* Check whether DAT[3:0] is 0000 */
1797 present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
1798 sdhci_runtime_pm_put(host);
1799
1800 return !(present_state & SDHCI_DATA_LVL_MASK);
1801}
1802
Adrian Hunterb5540ce2014-12-05 19:25:31 +02001803static int sdhci_prepare_hs400_tuning(struct mmc_host *mmc, struct mmc_ios *ios)
1804{
1805 struct sdhci_host *host = mmc_priv(mmc);
1806 unsigned long flags;
1807
1808 spin_lock_irqsave(&host->lock, flags);
1809 host->flags |= SDHCI_HS400_TUNING;
1810 spin_unlock_irqrestore(&host->lock, flags);
1811
1812 return 0;
1813}
1814
Girish K S069c9f12012-01-06 09:56:39 +05301815static int sdhci_execute_tuning(struct mmc_host *mmc, u32 opcode)
Arindam Nathb513ea22011-05-05 12:19:04 +05301816{
Russell King4b6f37d2014-04-25 12:59:36 +01001817 struct sdhci_host *host = mmc_priv(mmc);
Arindam Nathb513ea22011-05-05 12:19:04 +05301818 u16 ctrl;
Arindam Nathb513ea22011-05-05 12:19:04 +05301819 int tuning_loop_counter = MAX_TUNING_LOOP;
Arindam Nathb513ea22011-05-05 12:19:04 +05301820 int err = 0;
Aisheng Dong2b35bd82013-12-23 19:13:04 +08001821 unsigned long flags;
Adrian Hunter38e40bf2014-12-05 19:25:30 +02001822 unsigned int tuning_count = 0;
Adrian Hunterb5540ce2014-12-05 19:25:31 +02001823 bool hs400_tuning;
Arindam Nathb513ea22011-05-05 12:19:04 +05301824
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001825 sdhci_runtime_pm_get(host);
Aisheng Dong2b35bd82013-12-23 19:13:04 +08001826 spin_lock_irqsave(&host->lock, flags);
Arindam Nathb513ea22011-05-05 12:19:04 +05301827
Adrian Hunterb5540ce2014-12-05 19:25:31 +02001828 hs400_tuning = host->flags & SDHCI_HS400_TUNING;
1829 host->flags &= ~SDHCI_HS400_TUNING;
1830
Adrian Hunter38e40bf2014-12-05 19:25:30 +02001831 if (host->tuning_mode == SDHCI_TUNING_MODE_1)
1832 tuning_count = host->tuning_count;
1833
Arindam Nathb513ea22011-05-05 12:19:04 +05301834 /*
Weijun Yang9faac7b2015-10-04 12:04:12 +00001835 * The Host Controller needs tuning in case of SDR104 and DDR50
1836 * mode, and for SDR50 mode when Use Tuning for SDR50 is set in
1837 * the Capabilities register.
Girish K S069c9f12012-01-06 09:56:39 +05301838 * If the Host Controller supports the HS200 mode then the
1839 * tuning function has to be executed.
Arindam Nathb513ea22011-05-05 12:19:04 +05301840 */
Russell King4b6f37d2014-04-25 12:59:36 +01001841 switch (host->timing) {
Adrian Hunterb5540ce2014-12-05 19:25:31 +02001842 /* HS400 tuning is done in HS200 mode */
Adrian Huntere9fb05d2014-11-06 15:19:06 +02001843 case MMC_TIMING_MMC_HS400:
Adrian Hunterb5540ce2014-12-05 19:25:31 +02001844 err = -EINVAL;
1845 goto out_unlock;
1846
Russell King4b6f37d2014-04-25 12:59:36 +01001847 case MMC_TIMING_MMC_HS200:
Adrian Hunterb5540ce2014-12-05 19:25:31 +02001848 /*
1849 * Periodic re-tuning for HS400 is not expected to be needed, so
1850 * disable it here.
1851 */
1852 if (hs400_tuning)
1853 tuning_count = 0;
1854 break;
1855
Russell King4b6f37d2014-04-25 12:59:36 +01001856 case MMC_TIMING_UHS_SDR104:
Weijun Yang9faac7b2015-10-04 12:04:12 +00001857 case MMC_TIMING_UHS_DDR50:
Russell King4b6f37d2014-04-25 12:59:36 +01001858 break;
Girish K S069c9f12012-01-06 09:56:39 +05301859
Russell King4b6f37d2014-04-25 12:59:36 +01001860 case MMC_TIMING_UHS_SDR50:
1861 if (host->flags & SDHCI_SDR50_NEEDS_TUNING ||
1862 host->flags & SDHCI_SDR104_NEEDS_TUNING)
1863 break;
1864 /* FALLTHROUGH */
1865
1866 default:
Adrian Hunterd519c862014-12-05 19:25:29 +02001867 goto out_unlock;
Arindam Nathb513ea22011-05-05 12:19:04 +05301868 }
1869
Dong Aisheng45251812013-09-13 19:11:30 +08001870 if (host->ops->platform_execute_tuning) {
Aisheng Dong2b35bd82013-12-23 19:13:04 +08001871 spin_unlock_irqrestore(&host->lock, flags);
Dong Aisheng45251812013-09-13 19:11:30 +08001872 err = host->ops->platform_execute_tuning(host, opcode);
1873 sdhci_runtime_pm_put(host);
1874 return err;
1875 }
1876
Russell King4b6f37d2014-04-25 12:59:36 +01001877 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1878 ctrl |= SDHCI_CTRL_EXEC_TUNING;
Vincent Yang67d0d042015-01-20 16:05:16 +08001879 if (host->quirks2 & SDHCI_QUIRK2_TUNING_WORK_AROUND)
1880 ctrl |= SDHCI_CTRL_TUNED_CLK;
Arindam Nathb513ea22011-05-05 12:19:04 +05301881 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1882
1883 /*
1884 * As per the Host Controller spec v3.00, tuning command
1885 * generates Buffer Read Ready interrupt, so enable that.
1886 *
1887 * Note: The spec clearly says that when tuning sequence
1888 * is being performed, the controller does not generate
1889 * interrupts other than Buffer Read Ready interrupt. But
1890 * to make sure we don't hit a controller bug, we _only_
1891 * enable Buffer Read Ready interrupt here.
1892 */
Russell Kingb537f942014-04-25 12:56:01 +01001893 sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_INT_ENABLE);
1894 sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_SIGNAL_ENABLE);
Arindam Nathb513ea22011-05-05 12:19:04 +05301895
1896 /*
1897 * Issue CMD19 repeatedly till Execute Tuning is set to 0 or the number
1898 * of loops reaches 40 times or a timeout of 150ms occurs.
1899 */
Arindam Nathb513ea22011-05-05 12:19:04 +05301900 do {
1901 struct mmc_command cmd = {0};
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001902 struct mmc_request mrq = {NULL};
Arindam Nathb513ea22011-05-05 12:19:04 +05301903
Girish K S069c9f12012-01-06 09:56:39 +05301904 cmd.opcode = opcode;
Arindam Nathb513ea22011-05-05 12:19:04 +05301905 cmd.arg = 0;
1906 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1907 cmd.retries = 0;
1908 cmd.data = NULL;
1909 cmd.error = 0;
1910
Al Cooper7ce45e92014-05-09 11:34:07 -04001911 if (tuning_loop_counter-- == 0)
1912 break;
1913
Arindam Nathb513ea22011-05-05 12:19:04 +05301914 mrq.cmd = &cmd;
1915 host->mrq = &mrq;
1916
1917 /*
1918 * In response to CMD19, the card sends 64 bytes of tuning
1919 * block to the Host Controller. So we set the block size
1920 * to 64 here.
1921 */
Girish K S069c9f12012-01-06 09:56:39 +05301922 if (cmd.opcode == MMC_SEND_TUNING_BLOCK_HS200) {
1923 if (mmc->ios.bus_width == MMC_BUS_WIDTH_8)
1924 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 128),
1925 SDHCI_BLOCK_SIZE);
1926 else if (mmc->ios.bus_width == MMC_BUS_WIDTH_4)
1927 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64),
1928 SDHCI_BLOCK_SIZE);
1929 } else {
1930 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64),
1931 SDHCI_BLOCK_SIZE);
1932 }
Arindam Nathb513ea22011-05-05 12:19:04 +05301933
1934 /*
1935 * The tuning block is sent by the card to the host controller.
1936 * So we set the TRNS_READ bit in the Transfer Mode register.
1937 * This also takes care of setting DMA Enable and Multi Block
1938 * Select in the same register to 0.
1939 */
1940 sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE);
1941
1942 sdhci_send_command(host, &cmd);
1943
1944 host->cmd = NULL;
1945 host->mrq = NULL;
1946
Aisheng Dong2b35bd82013-12-23 19:13:04 +08001947 spin_unlock_irqrestore(&host->lock, flags);
Arindam Nathb513ea22011-05-05 12:19:04 +05301948 /* Wait for Buffer Read Ready interrupt */
1949 wait_event_interruptible_timeout(host->buf_ready_int,
1950 (host->tuning_done == 1),
1951 msecs_to_jiffies(50));
Aisheng Dong2b35bd82013-12-23 19:13:04 +08001952 spin_lock_irqsave(&host->lock, flags);
Arindam Nathb513ea22011-05-05 12:19:04 +05301953
1954 if (!host->tuning_done) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01001955 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 +05301956 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1957 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
1958 ctrl &= ~SDHCI_CTRL_EXEC_TUNING;
1959 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1960
1961 err = -EIO;
1962 goto out;
1963 }
1964
1965 host->tuning_done = 0;
1966
1967 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
Nick Sanders197160d2014-05-06 18:52:38 -07001968
1969 /* eMMC spec does not require a delay between tuning cycles */
1970 if (opcode == MMC_SEND_TUNING_BLOCK)
1971 mdelay(1);
Arindam Nathb513ea22011-05-05 12:19:04 +05301972 } while (ctrl & SDHCI_CTRL_EXEC_TUNING);
1973
1974 /*
1975 * The Host Driver has exhausted the maximum number of loops allowed,
1976 * so use fixed sampling frequency.
1977 */
Al Cooper7ce45e92014-05-09 11:34:07 -04001978 if (tuning_loop_counter < 0) {
Arindam Nathb513ea22011-05-05 12:19:04 +05301979 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
1980 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Al Cooper7ce45e92014-05-09 11:34:07 -04001981 }
1982 if (!(ctrl & SDHCI_CTRL_TUNED_CLK)) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01001983 pr_info(DRIVER_NAME ": Tuning procedure failed, falling back to fixed sampling clock\n");
Dong Aisheng114f2bf2013-10-18 19:48:45 +08001984 err = -EIO;
Arindam Nathb513ea22011-05-05 12:19:04 +05301985 }
1986
1987out:
Adrian Hunter38e40bf2014-12-05 19:25:30 +02001988 if (tuning_count) {
Adrian Hunter66c39dfc2015-05-07 13:10:21 +03001989 /*
1990 * In case tuning fails, host controllers which support
1991 * re-tuning can try tuning again at a later time, when the
1992 * re-tuning timer expires. So for these controllers, we
1993 * return 0. Since there might be other controllers who do not
1994 * have this capability, we return error for them.
1995 */
1996 err = 0;
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301997 }
1998
Adrian Hunter66c39dfc2015-05-07 13:10:21 +03001999 host->mmc->retune_period = err ? 0 : tuning_count;
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302000
Russell Kingb537f942014-04-25 12:56:01 +01002001 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
2002 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Adrian Hunterd519c862014-12-05 19:25:29 +02002003out_unlock:
Aisheng Dong2b35bd82013-12-23 19:13:04 +08002004 spin_unlock_irqrestore(&host->lock, flags);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002005 sdhci_runtime_pm_put(host);
Arindam Nathb513ea22011-05-05 12:19:04 +05302006
2007 return err;
2008}
2009
Adrian Huntercb849642015-02-06 14:12:59 +02002010static int sdhci_select_drive_strength(struct mmc_card *card,
2011 unsigned int max_dtr, int host_drv,
2012 int card_drv, int *drv_type)
2013{
2014 struct sdhci_host *host = mmc_priv(card->host);
2015
2016 if (!host->ops->select_drive_strength)
2017 return 0;
2018
2019 return host->ops->select_drive_strength(host, card, max_dtr, host_drv,
2020 card_drv, drv_type);
2021}
Kevin Liu52983382013-01-31 11:31:37 +08002022
2023static void sdhci_enable_preset_value(struct sdhci_host *host, bool enable)
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302024{
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302025 /* Host Controller v3.00 defines preset value registers */
2026 if (host->version < SDHCI_SPEC_300)
2027 return;
2028
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302029 /*
2030 * We only enable or disable Preset Value if they are not already
2031 * enabled or disabled respectively. Otherwise, we bail out.
2032 */
Russell Kingda91a8f2014-04-25 13:00:12 +01002033 if (host->preset_enabled != enable) {
2034 u16 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2035
2036 if (enable)
2037 ctrl |= SDHCI_CTRL_PRESET_VAL_ENABLE;
2038 else
2039 ctrl &= ~SDHCI_CTRL_PRESET_VAL_ENABLE;
2040
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302041 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Russell Kingda91a8f2014-04-25 13:00:12 +01002042
2043 if (enable)
2044 host->flags |= SDHCI_PV_ENABLED;
2045 else
2046 host->flags &= ~SDHCI_PV_ENABLED;
2047
2048 host->preset_enabled = enable;
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302049 }
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002050}
2051
Haibo Chen348487c2014-12-09 17:04:05 +08002052static void sdhci_post_req(struct mmc_host *mmc, struct mmc_request *mrq,
2053 int err)
2054{
2055 struct sdhci_host *host = mmc_priv(mmc);
2056 struct mmc_data *data = mrq->data;
2057
Russell Kingf48f0392016-01-26 13:40:32 +00002058 if (data->host_cookie != COOKIE_UNMAPPED)
Russell King771a3dc2016-01-26 13:40:53 +00002059 dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
2060 data->flags & MMC_DATA_WRITE ?
2061 DMA_TO_DEVICE : DMA_FROM_DEVICE);
2062
2063 data->host_cookie = COOKIE_UNMAPPED;
Haibo Chen348487c2014-12-09 17:04:05 +08002064}
2065
Haibo Chen348487c2014-12-09 17:04:05 +08002066static void sdhci_pre_req(struct mmc_host *mmc, struct mmc_request *mrq,
2067 bool is_first_req)
2068{
2069 struct sdhci_host *host = mmc_priv(mmc);
2070
Haibo Chend31911b2015-08-25 10:02:11 +08002071 mrq->data->host_cookie = COOKIE_UNMAPPED;
Haibo Chen348487c2014-12-09 17:04:05 +08002072
2073 if (host->flags & SDHCI_REQ_USE_DMA)
Russell King94538e52016-01-26 13:40:37 +00002074 sdhci_pre_dma_transfer(host, mrq->data, COOKIE_PRE_MAPPED);
Haibo Chen348487c2014-12-09 17:04:05 +08002075}
2076
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002077static void sdhci_card_event(struct mmc_host *mmc)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002078{
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002079 struct sdhci_host *host = mmc_priv(mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002080 unsigned long flags;
Krzysztof Kozlowski28367662015-01-05 10:50:15 +01002081 int present;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002082
Christian Daudt722e1282013-06-20 14:26:36 -07002083 /* First check if client has provided their own card event */
2084 if (host->ops->card_event)
2085 host->ops->card_event(host);
2086
Krzysztof Kozlowski28367662015-01-05 10:50:15 +01002087 present = sdhci_do_get_cd(host);
2088
Pierre Ossmand129bce2006-03-24 03:18:17 -08002089 spin_lock_irqsave(&host->lock, flags);
2090
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002091 /* Check host->mrq first in case we are runtime suspended */
Krzysztof Kozlowski28367662015-01-05 10:50:15 +01002092 if (host->mrq && !present) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302093 pr_err("%s: Card removed during transfer!\n",
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002094 mmc_hostname(host->mmc));
Girish K Sa3c76eb2011-10-11 11:44:09 +05302095 pr_err("%s: Resetting controller.\n",
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002096 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08002097
Russell King03231f92014-04-25 12:57:12 +01002098 sdhci_do_reset(host, SDHCI_RESET_CMD);
2099 sdhci_do_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002100
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002101 host->mrq->cmd->error = -ENOMEDIUM;
2102 tasklet_schedule(&host->finish_tasklet);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002103 }
2104
2105 spin_unlock_irqrestore(&host->lock, flags);
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002106}
2107
2108static const struct mmc_host_ops sdhci_ops = {
2109 .request = sdhci_request,
Haibo Chen348487c2014-12-09 17:04:05 +08002110 .post_req = sdhci_post_req,
2111 .pre_req = sdhci_pre_req,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002112 .set_ios = sdhci_set_ios,
Kevin Liu94144a42013-02-28 17:35:53 +08002113 .get_cd = sdhci_get_cd,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002114 .get_ro = sdhci_get_ro,
2115 .hw_reset = sdhci_hw_reset,
2116 .enable_sdio_irq = sdhci_enable_sdio_irq,
2117 .start_signal_voltage_switch = sdhci_start_signal_voltage_switch,
Adrian Hunterb5540ce2014-12-05 19:25:31 +02002118 .prepare_hs400_tuning = sdhci_prepare_hs400_tuning,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002119 .execute_tuning = sdhci_execute_tuning,
Adrian Huntercb849642015-02-06 14:12:59 +02002120 .select_drive_strength = sdhci_select_drive_strength,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002121 .card_event = sdhci_card_event,
Kevin Liu20b92a32012-12-17 19:29:26 +08002122 .card_busy = sdhci_card_busy,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002123};
2124
2125/*****************************************************************************\
2126 * *
2127 * Tasklets *
2128 * *
2129\*****************************************************************************/
2130
Pierre Ossmand129bce2006-03-24 03:18:17 -08002131static void sdhci_tasklet_finish(unsigned long param)
2132{
2133 struct sdhci_host *host;
2134 unsigned long flags;
2135 struct mmc_request *mrq;
2136
2137 host = (struct sdhci_host*)param;
2138
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002139 spin_lock_irqsave(&host->lock, flags);
2140
Chris Ball0c9c99a2011-04-27 17:35:31 -04002141 /*
2142 * If this tasklet gets rescheduled while running, it will
2143 * be run again afterwards but without any active request.
2144 */
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002145 if (!host->mrq) {
2146 spin_unlock_irqrestore(&host->lock, flags);
Chris Ball0c9c99a2011-04-27 17:35:31 -04002147 return;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002148 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002149
2150 del_timer(&host->timer);
2151
2152 mrq = host->mrq;
2153
Pierre Ossmand129bce2006-03-24 03:18:17 -08002154 /*
Russell King054cedf2016-01-26 13:40:42 +00002155 * Always unmap the data buffers if they were mapped by
2156 * sdhci_prepare_data() whenever we finish with a request.
2157 * This avoids leaking DMA mappings on error.
2158 */
2159 if (host->flags & SDHCI_REQ_USE_DMA) {
2160 struct mmc_data *data = mrq->data;
2161
2162 if (data && data->host_cookie == COOKIE_MAPPED) {
2163 dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
2164 (data->flags & MMC_DATA_READ) ?
2165 DMA_FROM_DEVICE : DMA_TO_DEVICE);
2166 data->host_cookie = COOKIE_UNMAPPED;
2167 }
2168 }
2169
2170 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08002171 * The controller needs a reset of internal state machines
2172 * upon error conditions.
2173 */
Pierre Ossman1e728592008-04-16 19:13:13 +02002174 if (!(host->flags & SDHCI_DEVICE_DEAD) &&
Ben Dooksb7b4d342011-04-27 14:24:19 +01002175 ((mrq->cmd && mrq->cmd->error) ||
Andrew Gabbasovfce9d332014-10-01 07:14:08 -05002176 (mrq->sbc && mrq->sbc->error) ||
2177 (mrq->data && ((mrq->data->error && !mrq->data->stop) ||
2178 (mrq->data->stop && mrq->data->stop->error))) ||
2179 (host->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST))) {
Pierre Ossman645289d2006-06-30 02:22:33 -07002180
2181 /* Some controllers need this kick or reset won't work here */
Andy Shevchenko8213af32013-01-07 16:31:08 +02002182 if (host->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET)
Pierre Ossman645289d2006-06-30 02:22:33 -07002183 /* This is to force an update */
Russell King17710592014-04-25 12:58:55 +01002184 host->ops->set_clock(host, host->clock);
Pierre Ossman645289d2006-06-30 02:22:33 -07002185
2186 /* Spec says we should do both at the same time, but Ricoh
2187 controllers do not like that. */
Russell King03231f92014-04-25 12:57:12 +01002188 sdhci_do_reset(host, SDHCI_RESET_CMD);
2189 sdhci_do_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002190 }
2191
2192 host->mrq = NULL;
2193 host->cmd = NULL;
2194 host->data = NULL;
2195
Pierre Ossmanf9134312008-12-21 17:01:48 +01002196#ifndef SDHCI_USE_LEDS_CLASS
Pierre Ossmand129bce2006-03-24 03:18:17 -08002197 sdhci_deactivate_led(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01002198#endif
Pierre Ossmand129bce2006-03-24 03:18:17 -08002199
Pierre Ossman5f25a662006-10-04 02:15:39 -07002200 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002201 spin_unlock_irqrestore(&host->lock, flags);
2202
2203 mmc_request_done(host->mmc, mrq);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002204 sdhci_runtime_pm_put(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002205}
2206
2207static void sdhci_timeout_timer(unsigned long data)
2208{
2209 struct sdhci_host *host;
2210 unsigned long flags;
2211
2212 host = (struct sdhci_host*)data;
2213
2214 spin_lock_irqsave(&host->lock, flags);
2215
2216 if (host->mrq) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01002217 pr_err("%s: Timeout waiting for hardware interrupt.\n",
2218 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08002219 sdhci_dumpregs(host);
2220
2221 if (host->data) {
Pierre Ossman17b04292007-07-22 22:18:46 +02002222 host->data->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002223 sdhci_finish_data(host);
2224 } else {
2225 if (host->cmd)
Pierre Ossman17b04292007-07-22 22:18:46 +02002226 host->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002227 else
Pierre Ossman17b04292007-07-22 22:18:46 +02002228 host->mrq->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002229
2230 tasklet_schedule(&host->finish_tasklet);
2231 }
2232 }
2233
Pierre Ossman5f25a662006-10-04 02:15:39 -07002234 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002235 spin_unlock_irqrestore(&host->lock, flags);
2236}
2237
2238/*****************************************************************************\
2239 * *
2240 * Interrupt handling *
2241 * *
2242\*****************************************************************************/
2243
Adrian Hunter61541392014-09-24 10:27:27 +03002244static void sdhci_cmd_irq(struct sdhci_host *host, u32 intmask, u32 *mask)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002245{
2246 BUG_ON(intmask == 0);
2247
2248 if (!host->cmd) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01002249 pr_err("%s: Got command interrupt 0x%08x even though no command operation was in progress.\n",
2250 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002251 sdhci_dumpregs(host);
2252 return;
2253 }
2254
Russell Kingec014cb2016-01-26 13:39:39 +00002255 if (intmask & (SDHCI_INT_TIMEOUT | SDHCI_INT_CRC |
2256 SDHCI_INT_END_BIT | SDHCI_INT_INDEX)) {
2257 if (intmask & SDHCI_INT_TIMEOUT)
2258 host->cmd->error = -ETIMEDOUT;
2259 else
2260 host->cmd->error = -EILSEQ;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002261
Russell King71fcbda2016-01-26 13:39:45 +00002262 /*
2263 * If this command initiates a data phase and a response
2264 * CRC error is signalled, the card can start transferring
2265 * data - the card may have received the command without
2266 * error. We must not terminate the mmc_request early.
2267 *
2268 * If the card did not receive the command or returned an
2269 * error which prevented it sending data, the data phase
2270 * will time out.
2271 */
2272 if (host->cmd->data &&
2273 (intmask & (SDHCI_INT_CRC | SDHCI_INT_TIMEOUT)) ==
2274 SDHCI_INT_CRC) {
2275 host->cmd = NULL;
2276 return;
2277 }
2278
Pierre Ossmand129bce2006-03-24 03:18:17 -08002279 tasklet_schedule(&host->finish_tasklet);
Pierre Ossmane8095172008-07-25 01:09:08 +02002280 return;
2281 }
2282
2283 /*
2284 * The host can send and interrupt when the busy state has
2285 * ended, allowing us to wait without wasting CPU cycles.
2286 * Unfortunately this is overloaded on the "data complete"
2287 * interrupt, so we need to take some care when handling
2288 * it.
2289 *
2290 * Note: The 1.0 specification is a bit ambiguous about this
2291 * feature so there might be some problems with older
2292 * controllers.
2293 */
2294 if (host->cmd->flags & MMC_RSP_BUSY) {
2295 if (host->cmd->data)
Marek Vasut2e4456f2015-11-18 10:47:02 +01002296 DBG("Cannot wait for busy signal when also doing a data transfer");
Chanho Mine99783a2014-08-30 12:40:40 +09002297 else if (!(host->quirks & SDHCI_QUIRK_NO_BUSY_IRQ)
2298 && !host->busy_handle) {
2299 /* Mark that command complete before busy is ended */
2300 host->busy_handle = 1;
Pierre Ossmane8095172008-07-25 01:09:08 +02002301 return;
Chanho Mine99783a2014-08-30 12:40:40 +09002302 }
Ben Dooksf9454052009-02-20 20:33:08 +03002303
2304 /* The controller does not support the end-of-busy IRQ,
2305 * fall through and take the SDHCI_INT_RESPONSE */
Adrian Hunter61541392014-09-24 10:27:27 +03002306 } else if ((host->quirks2 & SDHCI_QUIRK2_STOP_WITH_TC) &&
2307 host->cmd->opcode == MMC_STOP_TRANSMISSION && !host->data) {
2308 *mask &= ~SDHCI_INT_DATA_END;
Pierre Ossmane8095172008-07-25 01:09:08 +02002309 }
2310
2311 if (intmask & SDHCI_INT_RESPONSE)
Pierre Ossman43b58b32007-07-25 23:15:27 +02002312 sdhci_finish_command(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002313}
2314
George G. Davis0957c332010-02-18 12:32:12 -05002315#ifdef CONFIG_MMC_DEBUG
Adrian Hunter08621b12014-11-04 12:42:38 +02002316static void sdhci_adma_show_error(struct sdhci_host *host)
Ben Dooks6882a8c2009-06-14 13:52:38 +01002317{
2318 const char *name = mmc_hostname(host->mmc);
Adrian Hunter1c3d5f62014-11-04 12:42:41 +02002319 void *desc = host->adma_table;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002320
2321 sdhci_dumpregs(host);
2322
2323 while (true) {
Adrian Huntere57a5f62014-11-04 12:42:46 +02002324 struct sdhci_adma2_64_desc *dma_desc = desc;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002325
Adrian Huntere57a5f62014-11-04 12:42:46 +02002326 if (host->flags & SDHCI_USE_64_BIT_DMA)
2327 DBG("%s: %p: DMA 0x%08x%08x, LEN 0x%04x, Attr=0x%02x\n",
2328 name, desc, le32_to_cpu(dma_desc->addr_hi),
2329 le32_to_cpu(dma_desc->addr_lo),
2330 le16_to_cpu(dma_desc->len),
2331 le16_to_cpu(dma_desc->cmd));
2332 else
2333 DBG("%s: %p: DMA 0x%08x, LEN 0x%04x, Attr=0x%02x\n",
2334 name, desc, le32_to_cpu(dma_desc->addr_lo),
2335 le16_to_cpu(dma_desc->len),
2336 le16_to_cpu(dma_desc->cmd));
Ben Dooks6882a8c2009-06-14 13:52:38 +01002337
Adrian Hunter76fe3792014-11-04 12:42:42 +02002338 desc += host->desc_sz;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002339
Adrian Hunter05452302014-11-04 12:42:45 +02002340 if (dma_desc->cmd & cpu_to_le16(ADMA2_END))
Ben Dooks6882a8c2009-06-14 13:52:38 +01002341 break;
2342 }
2343}
2344#else
Adrian Hunter08621b12014-11-04 12:42:38 +02002345static void sdhci_adma_show_error(struct sdhci_host *host) { }
Ben Dooks6882a8c2009-06-14 13:52:38 +01002346#endif
2347
Pierre Ossmand129bce2006-03-24 03:18:17 -08002348static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
2349{
Girish K S069c9f12012-01-06 09:56:39 +05302350 u32 command;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002351 BUG_ON(intmask == 0);
2352
Arindam Nathb513ea22011-05-05 12:19:04 +05302353 /* CMD19 generates _only_ Buffer Read Ready interrupt */
2354 if (intmask & SDHCI_INT_DATA_AVAIL) {
Girish K S069c9f12012-01-06 09:56:39 +05302355 command = SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND));
2356 if (command == MMC_SEND_TUNING_BLOCK ||
2357 command == MMC_SEND_TUNING_BLOCK_HS200) {
Arindam Nathb513ea22011-05-05 12:19:04 +05302358 host->tuning_done = 1;
2359 wake_up(&host->buf_ready_int);
2360 return;
2361 }
2362 }
2363
Pierre Ossmand129bce2006-03-24 03:18:17 -08002364 if (!host->data) {
2365 /*
Pierre Ossmane8095172008-07-25 01:09:08 +02002366 * The "data complete" interrupt is also used to
2367 * indicate that a busy state has ended. See comment
2368 * above in sdhci_cmd_irq().
Pierre Ossmand129bce2006-03-24 03:18:17 -08002369 */
Pierre Ossmane8095172008-07-25 01:09:08 +02002370 if (host->cmd && (host->cmd->flags & MMC_RSP_BUSY)) {
Matthieu CASTETc5abd5e2014-08-14 16:03:17 +02002371 if (intmask & SDHCI_INT_DATA_TIMEOUT) {
2372 host->cmd->error = -ETIMEDOUT;
2373 tasklet_schedule(&host->finish_tasklet);
2374 return;
2375 }
Pierre Ossmane8095172008-07-25 01:09:08 +02002376 if (intmask & SDHCI_INT_DATA_END) {
Chanho Mine99783a2014-08-30 12:40:40 +09002377 /*
2378 * Some cards handle busy-end interrupt
2379 * before the command completed, so make
2380 * sure we do things in the proper order.
2381 */
2382 if (host->busy_handle)
2383 sdhci_finish_command(host);
2384 else
2385 host->busy_handle = 1;
Pierre Ossmane8095172008-07-25 01:09:08 +02002386 return;
2387 }
2388 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002389
Marek Vasut2e4456f2015-11-18 10:47:02 +01002390 pr_err("%s: Got data interrupt 0x%08x even though no data operation was in progress.\n",
2391 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002392 sdhci_dumpregs(host);
2393
2394 return;
2395 }
2396
2397 if (intmask & SDHCI_INT_DATA_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02002398 host->data->error = -ETIMEDOUT;
Aries Lee22113ef2010-12-15 08:14:24 +01002399 else if (intmask & SDHCI_INT_DATA_END_BIT)
2400 host->data->error = -EILSEQ;
2401 else if ((intmask & SDHCI_INT_DATA_CRC) &&
2402 SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND))
2403 != MMC_BUS_TEST_R)
Pierre Ossman17b04292007-07-22 22:18:46 +02002404 host->data->error = -EILSEQ;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002405 else if (intmask & SDHCI_INT_ADMA_ERROR) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302406 pr_err("%s: ADMA error\n", mmc_hostname(host->mmc));
Adrian Hunter08621b12014-11-04 12:42:38 +02002407 sdhci_adma_show_error(host);
Pierre Ossman2134a922008-06-28 18:28:51 +02002408 host->data->error = -EIO;
Haijun Zhanga4071fb2012-12-04 10:41:28 +08002409 if (host->ops->adma_workaround)
2410 host->ops->adma_workaround(host, intmask);
Ben Dooks6882a8c2009-06-14 13:52:38 +01002411 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002412
Pierre Ossman17b04292007-07-22 22:18:46 +02002413 if (host->data->error)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002414 sdhci_finish_data(host);
2415 else {
Pierre Ossmana406f5a2006-07-02 16:50:59 +01002416 if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
Pierre Ossmand129bce2006-03-24 03:18:17 -08002417 sdhci_transfer_pio(host);
2418
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002419 /*
2420 * We currently don't do anything fancy with DMA
2421 * boundaries, but as we can't disable the feature
2422 * we need to at least restart the transfer.
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04002423 *
2424 * According to the spec sdhci_readl(host, SDHCI_DMA_ADDRESS)
2425 * should return a valid address to continue from, but as
2426 * some controllers are faulty, don't trust them.
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002427 */
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04002428 if (intmask & SDHCI_INT_DMA_END) {
2429 u32 dmastart, dmanow;
2430 dmastart = sg_dma_address(host->data->sg);
2431 dmanow = dmastart + host->data->bytes_xfered;
2432 /*
2433 * Force update to the next DMA block boundary.
2434 */
2435 dmanow = (dmanow &
2436 ~(SDHCI_DEFAULT_BOUNDARY_SIZE - 1)) +
2437 SDHCI_DEFAULT_BOUNDARY_SIZE;
2438 host->data->bytes_xfered = dmanow - dmastart;
2439 DBG("%s: DMA base 0x%08x, transferred 0x%06x bytes,"
2440 " next 0x%08x\n",
2441 mmc_hostname(host->mmc), dmastart,
2442 host->data->bytes_xfered, dmanow);
2443 sdhci_writel(host, dmanow, SDHCI_DMA_ADDRESS);
2444 }
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002445
Pierre Ossmane538fbe2007-08-12 16:46:32 +02002446 if (intmask & SDHCI_INT_DATA_END) {
2447 if (host->cmd) {
2448 /*
2449 * Data managed to finish before the
2450 * command completed. Make sure we do
2451 * things in the proper order.
2452 */
2453 host->data_early = 1;
2454 } else {
2455 sdhci_finish_data(host);
2456 }
2457 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002458 }
2459}
2460
David Howells7d12e782006-10-05 14:55:46 +01002461static irqreturn_t sdhci_irq(int irq, void *dev_id)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002462{
Russell King781e9892014-04-25 12:55:46 +01002463 irqreturn_t result = IRQ_NONE;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002464 struct sdhci_host *host = dev_id;
Russell King41005002014-04-25 12:55:36 +01002465 u32 intmask, mask, unexpected = 0;
Russell King781e9892014-04-25 12:55:46 +01002466 int max_loops = 16;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002467
2468 spin_lock(&host->lock);
2469
Russell Kingbe138552014-04-25 12:55:56 +01002470 if (host->runtime_suspended && !sdhci_sdio_irq_enabled(host)) {
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002471 spin_unlock(&host->lock);
Adrian Hunter655bca72014-03-11 10:09:36 +02002472 return IRQ_NONE;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002473 }
2474
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002475 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
Mark Lord62df67a52007-03-06 13:30:13 +01002476 if (!intmask || intmask == 0xffffffff) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08002477 result = IRQ_NONE;
2478 goto out;
2479 }
2480
Russell King41005002014-04-25 12:55:36 +01002481 do {
2482 /* Clear selected interrupts. */
2483 mask = intmask & (SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK |
2484 SDHCI_INT_BUS_POWER);
2485 sdhci_writel(host, mask, SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002486
Russell King41005002014-04-25 12:55:36 +01002487 DBG("*** %s got interrupt: 0x%08x\n",
2488 mmc_hostname(host->mmc), intmask);
2489
2490 if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
2491 u32 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
2492 SDHCI_CARD_PRESENT;
2493
2494 /*
2495 * There is a observation on i.mx esdhc. INSERT
2496 * bit will be immediately set again when it gets
2497 * cleared, if a card is inserted. We have to mask
2498 * the irq to prevent interrupt storm which will
2499 * freeze the system. And the REMOVE gets the
2500 * same situation.
2501 *
2502 * More testing are needed here to ensure it works
2503 * for other platforms though.
2504 */
Russell Kingb537f942014-04-25 12:56:01 +01002505 host->ier &= ~(SDHCI_INT_CARD_INSERT |
2506 SDHCI_INT_CARD_REMOVE);
2507 host->ier |= present ? SDHCI_INT_CARD_REMOVE :
2508 SDHCI_INT_CARD_INSERT;
2509 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
2510 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Russell King41005002014-04-25 12:55:36 +01002511
2512 sdhci_writel(host, intmask & (SDHCI_INT_CARD_INSERT |
2513 SDHCI_INT_CARD_REMOVE), SDHCI_INT_STATUS);
Russell King3560db82014-04-25 12:55:51 +01002514
2515 host->thread_isr |= intmask & (SDHCI_INT_CARD_INSERT |
2516 SDHCI_INT_CARD_REMOVE);
2517 result = IRQ_WAKE_THREAD;
Russell King41005002014-04-25 12:55:36 +01002518 }
2519
2520 if (intmask & SDHCI_INT_CMD_MASK)
Adrian Hunter61541392014-09-24 10:27:27 +03002521 sdhci_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK,
2522 &intmask);
Russell King41005002014-04-25 12:55:36 +01002523
2524 if (intmask & SDHCI_INT_DATA_MASK)
2525 sdhci_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
2526
2527 if (intmask & SDHCI_INT_BUS_POWER)
2528 pr_err("%s: Card is consuming too much power!\n",
2529 mmc_hostname(host->mmc));
2530
Russell King781e9892014-04-25 12:55:46 +01002531 if (intmask & SDHCI_INT_CARD_INT) {
2532 sdhci_enable_sdio_irq_nolock(host, false);
2533 host->thread_isr |= SDHCI_INT_CARD_INT;
2534 result = IRQ_WAKE_THREAD;
2535 }
Russell King41005002014-04-25 12:55:36 +01002536
2537 intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE |
2538 SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK |
2539 SDHCI_INT_ERROR | SDHCI_INT_BUS_POWER |
2540 SDHCI_INT_CARD_INT);
2541
2542 if (intmask) {
2543 unexpected |= intmask;
2544 sdhci_writel(host, intmask, SDHCI_INT_STATUS);
2545 }
2546
Russell King781e9892014-04-25 12:55:46 +01002547 if (result == IRQ_NONE)
2548 result = IRQ_HANDLED;
Russell King41005002014-04-25 12:55:36 +01002549
2550 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
Russell King41005002014-04-25 12:55:36 +01002551 } while (intmask && --max_loops);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002552out:
2553 spin_unlock(&host->lock);
2554
Alexander Stein6379b232012-03-14 09:52:10 +01002555 if (unexpected) {
2556 pr_err("%s: Unexpected interrupt 0x%08x.\n",
2557 mmc_hostname(host->mmc), unexpected);
2558 sdhci_dumpregs(host);
2559 }
Pierre Ossmanf75979b2007-09-04 07:59:18 +02002560
Pierre Ossmand129bce2006-03-24 03:18:17 -08002561 return result;
2562}
2563
Russell King781e9892014-04-25 12:55:46 +01002564static irqreturn_t sdhci_thread_irq(int irq, void *dev_id)
2565{
2566 struct sdhci_host *host = dev_id;
2567 unsigned long flags;
2568 u32 isr;
2569
2570 spin_lock_irqsave(&host->lock, flags);
2571 isr = host->thread_isr;
2572 host->thread_isr = 0;
2573 spin_unlock_irqrestore(&host->lock, flags);
2574
Russell King3560db82014-04-25 12:55:51 +01002575 if (isr & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
2576 sdhci_card_event(host->mmc);
2577 mmc_detect_change(host->mmc, msecs_to_jiffies(200));
2578 }
2579
Russell King781e9892014-04-25 12:55:46 +01002580 if (isr & SDHCI_INT_CARD_INT) {
2581 sdio_run_irqs(host->mmc);
2582
2583 spin_lock_irqsave(&host->lock, flags);
2584 if (host->flags & SDHCI_SDIO_IRQ_ENABLED)
2585 sdhci_enable_sdio_irq_nolock(host, true);
2586 spin_unlock_irqrestore(&host->lock, flags);
2587 }
2588
2589 return isr ? IRQ_HANDLED : IRQ_NONE;
2590}
2591
Pierre Ossmand129bce2006-03-24 03:18:17 -08002592/*****************************************************************************\
2593 * *
2594 * Suspend/resume *
2595 * *
2596\*****************************************************************************/
2597
2598#ifdef CONFIG_PM
Kevin Liuad080d72013-01-05 17:21:33 +08002599void sdhci_enable_irq_wakeups(struct sdhci_host *host)
2600{
2601 u8 val;
2602 u8 mask = SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE
2603 | SDHCI_WAKE_ON_INT;
2604
2605 val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
2606 val |= mask ;
2607 /* Avoid fake wake up */
2608 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
2609 val &= ~(SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE);
2610 sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
2611}
2612EXPORT_SYMBOL_GPL(sdhci_enable_irq_wakeups);
2613
Fabio Estevam0b10f472014-08-30 14:53:13 -03002614static void sdhci_disable_irq_wakeups(struct sdhci_host *host)
Kevin Liuad080d72013-01-05 17:21:33 +08002615{
2616 u8 val;
2617 u8 mask = SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE
2618 | SDHCI_WAKE_ON_INT;
2619
2620 val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
2621 val &= ~mask;
2622 sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
2623}
Pierre Ossmand129bce2006-03-24 03:18:17 -08002624
Manuel Lauss29495aa2011-11-03 11:09:45 +01002625int sdhci_suspend_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002626{
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002627 sdhci_disable_card_detection(host);
2628
Adrian Hunter66c39dfc2015-05-07 13:10:21 +03002629 mmc_retune_timer_stop(host->mmc);
2630 mmc_retune_needed(host->mmc);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302631
Kevin Liuad080d72013-01-05 17:21:33 +08002632 if (!device_may_wakeup(mmc_dev(host->mmc))) {
Russell Kingb537f942014-04-25 12:56:01 +01002633 host->ier = 0;
2634 sdhci_writel(host, 0, SDHCI_INT_ENABLE);
2635 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Kevin Liuad080d72013-01-05 17:21:33 +08002636 free_irq(host->irq, host);
2637 } else {
2638 sdhci_enable_irq_wakeups(host);
2639 enable_irq_wake(host->irq);
2640 }
Ulf Hansson4ee14ec2013-09-25 14:15:24 +02002641 return 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002642}
2643
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002644EXPORT_SYMBOL_GPL(sdhci_suspend_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002645
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002646int sdhci_resume_host(struct sdhci_host *host)
2647{
Ulf Hansson4ee14ec2013-09-25 14:15:24 +02002648 int ret = 0;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002649
Richard Röjforsa13abc72009-09-22 16:45:30 -07002650 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002651 if (host->ops->enable_dma)
2652 host->ops->enable_dma(host);
2653 }
2654
Adrian Hunter6308d292012-02-07 14:48:54 +02002655 if ((host->mmc->pm_flags & MMC_PM_KEEP_POWER) &&
2656 (host->quirks2 & SDHCI_QUIRK2_HOST_OFF_CARD_ON)) {
2657 /* Card keeps power but host controller does not */
2658 sdhci_init(host, 0);
2659 host->pwr = 0;
2660 host->clock = 0;
2661 sdhci_do_set_ios(host, &host->mmc->ios);
2662 } else {
2663 sdhci_init(host, (host->mmc->pm_flags & MMC_PM_KEEP_POWER));
2664 mmiowb();
2665 }
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002666
Haibo Chen14a7b41642015-09-15 18:32:58 +08002667 if (!device_may_wakeup(mmc_dev(host->mmc))) {
2668 ret = request_threaded_irq(host->irq, sdhci_irq,
2669 sdhci_thread_irq, IRQF_SHARED,
2670 mmc_hostname(host->mmc), host);
2671 if (ret)
2672 return ret;
2673 } else {
2674 sdhci_disable_irq_wakeups(host);
2675 disable_irq_wake(host->irq);
2676 }
2677
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002678 sdhci_enable_card_detection(host);
2679
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08002680 return ret;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002681}
2682
2683EXPORT_SYMBOL_GPL(sdhci_resume_host);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002684
2685static int sdhci_runtime_pm_get(struct sdhci_host *host)
2686{
2687 return pm_runtime_get_sync(host->mmc->parent);
2688}
2689
2690static int sdhci_runtime_pm_put(struct sdhci_host *host)
2691{
2692 pm_runtime_mark_last_busy(host->mmc->parent);
2693 return pm_runtime_put_autosuspend(host->mmc->parent);
2694}
2695
Adrian Hunterf0710a52013-05-06 12:17:32 +03002696static void sdhci_runtime_pm_bus_on(struct sdhci_host *host)
2697{
Adrian Hunter5c671c42015-11-26 14:00:50 +02002698 if (host->bus_on)
Adrian Hunterf0710a52013-05-06 12:17:32 +03002699 return;
2700 host->bus_on = true;
2701 pm_runtime_get_noresume(host->mmc->parent);
2702}
2703
2704static void sdhci_runtime_pm_bus_off(struct sdhci_host *host)
2705{
Adrian Hunter5c671c42015-11-26 14:00:50 +02002706 if (!host->bus_on)
Adrian Hunterf0710a52013-05-06 12:17:32 +03002707 return;
2708 host->bus_on = false;
2709 pm_runtime_put_noidle(host->mmc->parent);
2710}
2711
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002712int sdhci_runtime_suspend_host(struct sdhci_host *host)
2713{
2714 unsigned long flags;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002715
Adrian Hunter66c39dfc2015-05-07 13:10:21 +03002716 mmc_retune_timer_stop(host->mmc);
2717 mmc_retune_needed(host->mmc);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002718
2719 spin_lock_irqsave(&host->lock, flags);
Russell Kingb537f942014-04-25 12:56:01 +01002720 host->ier &= SDHCI_INT_CARD_INT;
2721 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
2722 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002723 spin_unlock_irqrestore(&host->lock, flags);
2724
Russell King781e9892014-04-25 12:55:46 +01002725 synchronize_hardirq(host->irq);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002726
2727 spin_lock_irqsave(&host->lock, flags);
2728 host->runtime_suspended = true;
2729 spin_unlock_irqrestore(&host->lock, flags);
2730
Markus Pargmann8a125ba2014-06-04 15:24:29 +02002731 return 0;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002732}
2733EXPORT_SYMBOL_GPL(sdhci_runtime_suspend_host);
2734
2735int sdhci_runtime_resume_host(struct sdhci_host *host)
2736{
2737 unsigned long flags;
Markus Pargmann8a125ba2014-06-04 15:24:29 +02002738 int host_flags = host->flags;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002739
2740 if (host_flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
2741 if (host->ops->enable_dma)
2742 host->ops->enable_dma(host);
2743 }
2744
2745 sdhci_init(host, 0);
2746
2747 /* Force clock and power re-program */
2748 host->pwr = 0;
2749 host->clock = 0;
Jisheng Zhang3396e732015-01-29 17:42:12 +08002750 sdhci_do_start_signal_voltage_switch(host, &host->mmc->ios);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002751 sdhci_do_set_ios(host, &host->mmc->ios);
2752
Kevin Liu52983382013-01-31 11:31:37 +08002753 if ((host_flags & SDHCI_PV_ENABLED) &&
2754 !(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN)) {
2755 spin_lock_irqsave(&host->lock, flags);
2756 sdhci_enable_preset_value(host, true);
2757 spin_unlock_irqrestore(&host->lock, flags);
2758 }
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002759
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002760 spin_lock_irqsave(&host->lock, flags);
2761
2762 host->runtime_suspended = false;
2763
2764 /* Enable SDIO IRQ */
Russell Kingef104332014-04-25 12:55:41 +01002765 if (host->flags & SDHCI_SDIO_IRQ_ENABLED)
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002766 sdhci_enable_sdio_irq_nolock(host, true);
2767
2768 /* Enable Card Detection */
2769 sdhci_enable_card_detection(host);
2770
2771 spin_unlock_irqrestore(&host->lock, flags);
2772
Markus Pargmann8a125ba2014-06-04 15:24:29 +02002773 return 0;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002774}
2775EXPORT_SYMBOL_GPL(sdhci_runtime_resume_host);
2776
Rafael J. Wysocki162d6f92014-12-05 03:05:33 +01002777#endif /* CONFIG_PM */
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002778
Pierre Ossmand129bce2006-03-24 03:18:17 -08002779/*****************************************************************************\
2780 * *
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002781 * Device allocation/registration *
Pierre Ossmand129bce2006-03-24 03:18:17 -08002782 * *
2783\*****************************************************************************/
2784
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002785struct sdhci_host *sdhci_alloc_host(struct device *dev,
2786 size_t priv_size)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002787{
Pierre Ossmand129bce2006-03-24 03:18:17 -08002788 struct mmc_host *mmc;
2789 struct sdhci_host *host;
2790
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002791 WARN_ON(dev == NULL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002792
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002793 mmc = mmc_alloc_host(sizeof(struct sdhci_host) + priv_size, dev);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002794 if (!mmc)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002795 return ERR_PTR(-ENOMEM);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002796
2797 host = mmc_priv(mmc);
2798 host->mmc = mmc;
Adrian Hunterbf60e592016-02-09 16:12:35 +02002799 host->mmc_host_ops = sdhci_ops;
2800 mmc->ops = &host->mmc_host_ops;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002801
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002802 return host;
2803}
Pierre Ossman8a4da142006-10-04 02:15:40 -07002804
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002805EXPORT_SYMBOL_GPL(sdhci_alloc_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002806
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002807int sdhci_add_host(struct sdhci_host *host)
2808{
2809 struct mmc_host *mmc;
Philip Rakitybd6a8c32012-06-27 21:49:27 -07002810 u32 caps[2] = {0, 0};
Arindam Nathf2119df2011-05-05 12:18:57 +05302811 u32 max_current_caps;
2812 unsigned int ocr_avail;
Adrian Hunterf5fa92e2014-09-24 10:27:32 +03002813 unsigned int override_timeout_clk;
Dong Aisheng59241752015-07-22 20:53:07 +08002814 u32 max_clk;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002815 int ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002816
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002817 WARN_ON(host == NULL);
2818 if (host == NULL)
2819 return -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002820
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002821 mmc = host->mmc;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002822
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002823 if (debug_quirks)
2824 host->quirks = debug_quirks;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002825 if (debug_quirks2)
2826 host->quirks2 = debug_quirks2;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002827
Adrian Hunterf5fa92e2014-09-24 10:27:32 +03002828 override_timeout_clk = host->timeout_clk;
2829
Russell King03231f92014-04-25 12:57:12 +01002830 sdhci_do_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand96649e2006-06-30 02:22:30 -07002831
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002832 host->version = sdhci_readw(host, SDHCI_HOST_VERSION);
Pierre Ossman2134a922008-06-28 18:28:51 +02002833 host->version = (host->version & SDHCI_SPEC_VER_MASK)
2834 >> SDHCI_SPEC_VER_SHIFT;
Zhangfei Gao85105c52010-08-06 07:10:01 +08002835 if (host->version > SDHCI_SPEC_300) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01002836 pr_err("%s: Unknown controller version (%d). You may experience problems.\n",
2837 mmc_hostname(mmc), host->version);
Pierre Ossman4a965502006-06-30 02:22:29 -07002838 }
2839
Arindam Nathf2119df2011-05-05 12:18:57 +05302840 caps[0] = (host->quirks & SDHCI_QUIRK_MISSING_CAPS) ? host->caps :
Maxim Levitskyccc92c22010-08-10 18:01:42 -07002841 sdhci_readl(host, SDHCI_CAPABILITIES);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002842
Philip Rakitybd6a8c32012-06-27 21:49:27 -07002843 if (host->version >= SDHCI_SPEC_300)
2844 caps[1] = (host->quirks & SDHCI_QUIRK_MISSING_CAPS) ?
2845 host->caps1 :
2846 sdhci_readl(host, SDHCI_CAPABILITIES_1);
Arindam Nathf2119df2011-05-05 12:18:57 +05302847
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002848 if (host->quirks & SDHCI_QUIRK_FORCE_DMA)
Richard Röjforsa13abc72009-09-22 16:45:30 -07002849 host->flags |= SDHCI_USE_SDMA;
Arindam Nathf2119df2011-05-05 12:18:57 +05302850 else if (!(caps[0] & SDHCI_CAN_DO_SDMA))
Richard Röjforsa13abc72009-09-22 16:45:30 -07002851 DBG("Controller doesn't have SDMA capability\n");
Pierre Ossman67435272006-06-30 02:22:31 -07002852 else
Richard Röjforsa13abc72009-09-22 16:45:30 -07002853 host->flags |= SDHCI_USE_SDMA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002854
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002855 if ((host->quirks & SDHCI_QUIRK_BROKEN_DMA) &&
Richard Röjforsa13abc72009-09-22 16:45:30 -07002856 (host->flags & SDHCI_USE_SDMA)) {
Rolf Eike Beercee687c2007-11-02 15:22:30 +01002857 DBG("Disabling DMA as it is marked broken\n");
Richard Röjforsa13abc72009-09-22 16:45:30 -07002858 host->flags &= ~SDHCI_USE_SDMA;
Feng Tang7c168e32007-09-30 12:44:18 +02002859 }
2860
Arindam Nathf2119df2011-05-05 12:18:57 +05302861 if ((host->version >= SDHCI_SPEC_200) &&
2862 (caps[0] & SDHCI_CAN_DO_ADMA2))
Richard Röjforsa13abc72009-09-22 16:45:30 -07002863 host->flags |= SDHCI_USE_ADMA;
Pierre Ossman2134a922008-06-28 18:28:51 +02002864
2865 if ((host->quirks & SDHCI_QUIRK_BROKEN_ADMA) &&
2866 (host->flags & SDHCI_USE_ADMA)) {
2867 DBG("Disabling ADMA as it is marked broken\n");
2868 host->flags &= ~SDHCI_USE_ADMA;
2869 }
2870
Adrian Huntere57a5f62014-11-04 12:42:46 +02002871 /*
2872 * It is assumed that a 64-bit capable device has set a 64-bit DMA mask
2873 * and *must* do 64-bit DMA. A driver has the opportunity to change
2874 * that during the first call to ->enable_dma(). Similarly
2875 * SDHCI_QUIRK2_BROKEN_64_BIT_DMA must be left to the drivers to
2876 * implement.
2877 */
Al Cooper5eaa7472016-02-10 15:25:39 -05002878 if (caps[0] & SDHCI_CAN_64BIT)
Adrian Huntere57a5f62014-11-04 12:42:46 +02002879 host->flags |= SDHCI_USE_64_BIT_DMA;
2880
Richard Röjforsa13abc72009-09-22 16:45:30 -07002881 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002882 if (host->ops->enable_dma) {
2883 if (host->ops->enable_dma(host)) {
Joe Perches66061102014-09-12 14:56:56 -07002884 pr_warn("%s: No suitable DMA available - falling back to PIO\n",
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002885 mmc_hostname(mmc));
Richard Röjforsa13abc72009-09-22 16:45:30 -07002886 host->flags &=
2887 ~(SDHCI_USE_SDMA | SDHCI_USE_ADMA);
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002888 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002889 }
2890 }
2891
Adrian Huntere57a5f62014-11-04 12:42:46 +02002892 /* SDMA does not support 64-bit DMA */
2893 if (host->flags & SDHCI_USE_64_BIT_DMA)
2894 host->flags &= ~SDHCI_USE_SDMA;
2895
Pierre Ossman2134a922008-06-28 18:28:51 +02002896 if (host->flags & SDHCI_USE_ADMA) {
Russell Kinge66e61c2016-01-26 13:39:55 +00002897 dma_addr_t dma;
2898 void *buf;
2899
Pierre Ossman2134a922008-06-28 18:28:51 +02002900 /*
Adrian Hunter76fe3792014-11-04 12:42:42 +02002901 * The DMA descriptor table size is calculated as the maximum
2902 * number of segments times 2, to allow for an alignment
2903 * descriptor for each segment, plus 1 for a nop end descriptor,
2904 * all multipled by the descriptor size.
Pierre Ossman2134a922008-06-28 18:28:51 +02002905 */
Adrian Huntere57a5f62014-11-04 12:42:46 +02002906 if (host->flags & SDHCI_USE_64_BIT_DMA) {
2907 host->adma_table_sz = (SDHCI_MAX_SEGS * 2 + 1) *
2908 SDHCI_ADMA2_64_DESC_SZ;
Adrian Huntere57a5f62014-11-04 12:42:46 +02002909 host->desc_sz = SDHCI_ADMA2_64_DESC_SZ;
Adrian Huntere57a5f62014-11-04 12:42:46 +02002910 } else {
2911 host->adma_table_sz = (SDHCI_MAX_SEGS * 2 + 1) *
2912 SDHCI_ADMA2_32_DESC_SZ;
Adrian Huntere57a5f62014-11-04 12:42:46 +02002913 host->desc_sz = SDHCI_ADMA2_32_DESC_SZ;
Adrian Huntere57a5f62014-11-04 12:42:46 +02002914 }
Russell Kinge66e61c2016-01-26 13:39:55 +00002915
Adrian Hunter04a5ae62015-11-26 14:00:49 +02002916 host->align_buffer_sz = SDHCI_MAX_SEGS * SDHCI_ADMA2_ALIGN;
Russell Kinge66e61c2016-01-26 13:39:55 +00002917 buf = dma_alloc_coherent(mmc_dev(mmc), host->align_buffer_sz +
2918 host->adma_table_sz, &dma, GFP_KERNEL);
2919 if (!buf) {
Joe Perches66061102014-09-12 14:56:56 -07002920 pr_warn("%s: Unable to allocate ADMA buffers - falling back to standard DMA\n",
Pierre Ossman2134a922008-06-28 18:28:51 +02002921 mmc_hostname(mmc));
2922 host->flags &= ~SDHCI_USE_ADMA;
Russell Kinge66e61c2016-01-26 13:39:55 +00002923 } else if ((dma + host->align_buffer_sz) &
2924 (SDHCI_ADMA2_DESC_ALIGN - 1)) {
Joe Perches66061102014-09-12 14:56:56 -07002925 pr_warn("%s: unable to allocate aligned ADMA descriptor\n",
2926 mmc_hostname(mmc));
Russell Kingd1e49f72014-04-25 12:58:34 +01002927 host->flags &= ~SDHCI_USE_ADMA;
Russell Kinge66e61c2016-01-26 13:39:55 +00002928 dma_free_coherent(mmc_dev(mmc), host->align_buffer_sz +
2929 host->adma_table_sz, buf, dma);
2930 } else {
2931 host->align_buffer = buf;
2932 host->align_addr = dma;
Russell Kingedd63fc2016-01-26 13:39:50 +00002933
Russell Kinge66e61c2016-01-26 13:39:55 +00002934 host->adma_table = buf + host->align_buffer_sz;
2935 host->adma_addr = dma + host->align_buffer_sz;
2936 }
Pierre Ossman2134a922008-06-28 18:28:51 +02002937 }
2938
Pierre Ossman76591502008-07-21 00:32:11 +02002939 /*
2940 * If we use DMA, then it's up to the caller to set the DMA
2941 * mask, but PIO does not need the hw shim so we set a new
2942 * mask here in that case.
2943 */
Richard Röjforsa13abc72009-09-22 16:45:30 -07002944 if (!(host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))) {
Pierre Ossman76591502008-07-21 00:32:11 +02002945 host->dma_mask = DMA_BIT_MASK(64);
Markus Mayer4e743f12014-07-03 13:27:42 -07002946 mmc_dev(mmc)->dma_mask = &host->dma_mask;
Pierre Ossman76591502008-07-21 00:32:11 +02002947 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002948
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04002949 if (host->version >= SDHCI_SPEC_300)
Arindam Nathf2119df2011-05-05 12:18:57 +05302950 host->max_clk = (caps[0] & SDHCI_CLOCK_V3_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04002951 >> SDHCI_CLOCK_BASE_SHIFT;
2952 else
Arindam Nathf2119df2011-05-05 12:18:57 +05302953 host->max_clk = (caps[0] & SDHCI_CLOCK_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04002954 >> SDHCI_CLOCK_BASE_SHIFT;
2955
Pierre Ossmand129bce2006-03-24 03:18:17 -08002956 host->max_clk *= 1000000;
Anton Vorontsovf27f47e2010-05-26 14:41:53 -07002957 if (host->max_clk == 0 || host->quirks &
2958 SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN) {
Ben Dooks4240ff02009-03-17 00:13:57 +03002959 if (!host->ops->get_max_clock) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01002960 pr_err("%s: Hardware doesn't specify base clock frequency.\n",
2961 mmc_hostname(mmc));
Ben Dooks4240ff02009-03-17 00:13:57 +03002962 return -ENODEV;
2963 }
2964 host->max_clk = host->ops->get_max_clock(host);
2965 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002966
2967 /*
Arindam Nathc3ed3872011-05-05 12:19:06 +05302968 * In case of Host Controller v3.00, find out whether clock
2969 * multiplier is supported.
2970 */
2971 host->clk_mul = (caps[1] & SDHCI_CLOCK_MUL_MASK) >>
2972 SDHCI_CLOCK_MUL_SHIFT;
2973
2974 /*
2975 * In case the value in Clock Multiplier is 0, then programmable
2976 * clock mode is not supported, otherwise the actual clock
2977 * multiplier is one more than the value of Clock Multiplier
2978 * in the Capabilities Register.
2979 */
2980 if (host->clk_mul)
2981 host->clk_mul += 1;
2982
2983 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08002984 * Set host parameters.
2985 */
Dong Aisheng59241752015-07-22 20:53:07 +08002986 max_clk = host->max_clk;
2987
Marek Szyprowskice5f0362010-08-10 18:01:56 -07002988 if (host->ops->get_min_clock)
Anton Vorontsova9e58f22009-07-29 15:04:16 -07002989 mmc->f_min = host->ops->get_min_clock(host);
Arindam Nathc3ed3872011-05-05 12:19:06 +05302990 else if (host->version >= SDHCI_SPEC_300) {
2991 if (host->clk_mul) {
2992 mmc->f_min = (host->max_clk * host->clk_mul) / 1024;
Dong Aisheng59241752015-07-22 20:53:07 +08002993 max_clk = host->max_clk * host->clk_mul;
Arindam Nathc3ed3872011-05-05 12:19:06 +05302994 } else
2995 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_300;
2996 } else
Zhangfei Gao03975262010-09-20 15:15:18 -04002997 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_200;
Philip Rakity15ec4462010-11-19 16:48:39 -05002998
Dong Aisheng59241752015-07-22 20:53:07 +08002999 if (!mmc->f_max || (mmc->f_max && (mmc->f_max > max_clk)))
3000 mmc->f_max = max_clk;
3001
Aisheng Dong28aab052014-08-27 15:26:31 +08003002 if (!(host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)) {
3003 host->timeout_clk = (caps[0] & SDHCI_TIMEOUT_CLK_MASK) >>
3004 SDHCI_TIMEOUT_CLK_SHIFT;
3005 if (host->timeout_clk == 0) {
3006 if (host->ops->get_timeout_clock) {
3007 host->timeout_clk =
3008 host->ops->get_timeout_clock(host);
3009 } else {
3010 pr_err("%s: Hardware doesn't specify timeout clock frequency.\n",
3011 mmc_hostname(mmc));
3012 return -ENODEV;
3013 }
Andy Shevchenko272308c2011-08-03 18:36:00 +03003014 }
Andy Shevchenko272308c2011-08-03 18:36:00 +03003015
Aisheng Dong28aab052014-08-27 15:26:31 +08003016 if (caps[0] & SDHCI_TIMEOUT_CLK_UNIT)
3017 host->timeout_clk *= 1000;
Andy Shevchenko272308c2011-08-03 18:36:00 +03003018
Aisheng Dong28aab052014-08-27 15:26:31 +08003019 mmc->max_busy_timeout = host->ops->get_max_timeout_count ?
Aisheng Donga6ff5ae2014-08-27 15:26:27 +08003020 host->ops->get_max_timeout_count(host) : 1 << 27;
Aisheng Dong28aab052014-08-27 15:26:31 +08003021 mmc->max_busy_timeout /= host->timeout_clk;
3022 }
Adrian Hunter58d12462011-06-28 17:16:03 +03003023
Adrian Hunterf5fa92e2014-09-24 10:27:32 +03003024 if (override_timeout_clk)
3025 host->timeout_clk = override_timeout_clk;
3026
Andrei Warkentine89d4562011-05-23 15:06:37 -05003027 mmc->caps |= MMC_CAP_SDIO_IRQ | MMC_CAP_ERASE | MMC_CAP_CMD23;
Russell King781e9892014-04-25 12:55:46 +01003028 mmc->caps2 |= MMC_CAP2_SDIO_IRQ_NOTHREAD;
Andrei Warkentine89d4562011-05-23 15:06:37 -05003029
3030 if (host->quirks & SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12)
3031 host->flags |= SDHCI_AUTO_CMD12;
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04003032
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003033 /* Auto-CMD23 stuff only works in ADMA or PIO. */
Andrei Warkentin4f3d3e92011-05-25 10:42:50 -04003034 if ((host->version >= SDHCI_SPEC_300) &&
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003035 ((host->flags & SDHCI_USE_ADMA) ||
Scott Branden3bfa6f02015-02-09 16:06:28 -08003036 !(host->flags & SDHCI_USE_SDMA)) &&
3037 !(host->quirks2 & SDHCI_QUIRK2_ACMD23_BROKEN)) {
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003038 host->flags |= SDHCI_AUTO_CMD23;
3039 DBG("%s: Auto-CMD23 available\n", mmc_hostname(mmc));
3040 } else {
3041 DBG("%s: Auto-CMD23 unavailable\n", mmc_hostname(mmc));
3042 }
3043
Philip Rakity15ec4462010-11-19 16:48:39 -05003044 /*
3045 * A controller may support 8-bit width, but the board itself
3046 * might not have the pins brought out. Boards that support
3047 * 8-bit width must set "mmc->caps |= MMC_CAP_8_BIT_DATA;" in
3048 * their platform code before calling sdhci_add_host(), and we
3049 * won't assume 8-bit width for hosts without that CAP.
3050 */
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04003051 if (!(host->quirks & SDHCI_QUIRK_FORCE_1_BIT_DATA))
Philip Rakity15ec4462010-11-19 16:48:39 -05003052 mmc->caps |= MMC_CAP_4_BIT_DATA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003053
Jerry Huang63ef5d82012-10-25 13:47:19 +08003054 if (host->quirks2 & SDHCI_QUIRK2_HOST_NO_CMD23)
3055 mmc->caps &= ~MMC_CAP_CMD23;
3056
Arindam Nathf2119df2011-05-05 12:18:57 +05303057 if (caps[0] & SDHCI_CAN_DO_HISPD)
Zhangfei Gaoa29e7e12010-08-16 21:15:32 -04003058 mmc->caps |= MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED;
Pierre Ossmancd9277c2007-02-18 12:07:47 +01003059
Jaehoon Chung176d1ed2010-09-27 09:42:20 +01003060 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) &&
Ivan T. Ivanovc31d22e2015-07-06 15:16:20 +03003061 !(mmc->caps & MMC_CAP_NONREMOVABLE) &&
3062 IS_ERR_VALUE(mmc_gpio_get_cd(host->mmc)))
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03003063 mmc->caps |= MMC_CAP_NEEDS_POLL;
3064
Tim Kryger3a48edc2014-06-13 10:13:56 -07003065 /* If there are external regulators, get them */
3066 if (mmc_regulator_get_supply(mmc) == -EPROBE_DEFER)
3067 return -EPROBE_DEFER;
3068
Philip Rakity6231f3d2012-07-23 15:56:23 -07003069 /* If vqmmc regulator and no 1.8V signalling, then there's no UHS */
Tim Kryger3a48edc2014-06-13 10:13:56 -07003070 if (!IS_ERR(mmc->supply.vqmmc)) {
3071 ret = regulator_enable(mmc->supply.vqmmc);
3072 if (!regulator_is_supported_voltage(mmc->supply.vqmmc, 1700000,
3073 1950000))
Kevin Liu8363c372012-11-17 17:55:51 -05003074 caps[1] &= ~(SDHCI_SUPPORT_SDR104 |
3075 SDHCI_SUPPORT_SDR50 |
3076 SDHCI_SUPPORT_DDR50);
Chris Balla3361ab2013-03-11 17:51:53 -04003077 if (ret) {
3078 pr_warn("%s: Failed to enable vqmmc regulator: %d\n",
3079 mmc_hostname(mmc), ret);
Adrian Hunter4bb74312014-11-06 15:19:04 +02003080 mmc->supply.vqmmc = ERR_PTR(-EINVAL);
Chris Balla3361ab2013-03-11 17:51:53 -04003081 }
Kevin Liu8363c372012-11-17 17:55:51 -05003082 }
Philip Rakity6231f3d2012-07-23 15:56:23 -07003083
Daniel Drake6a661802012-11-25 13:01:19 -05003084 if (host->quirks2 & SDHCI_QUIRK2_NO_1_8_V)
3085 caps[1] &= ~(SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 |
3086 SDHCI_SUPPORT_DDR50);
3087
Al Cooper4188bba2012-03-16 15:54:17 -04003088 /* Any UHS-I mode in caps implies SDR12 and SDR25 support. */
3089 if (caps[1] & (SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 |
3090 SDHCI_SUPPORT_DDR50))
Arindam Nathf2119df2011-05-05 12:18:57 +05303091 mmc->caps |= MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25;
3092
3093 /* SDR104 supports also implies SDR50 support */
Giuseppe CAVALLARO156e14b2013-06-12 08:16:38 +02003094 if (caps[1] & SDHCI_SUPPORT_SDR104) {
Arindam Nathf2119df2011-05-05 12:18:57 +05303095 mmc->caps |= MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_SDR50;
Giuseppe CAVALLARO156e14b2013-06-12 08:16:38 +02003096 /* SD3.0: SDR104 is supported so (for eMMC) the caps2
3097 * field can be promoted to support HS200.
3098 */
Adrian Hunter549c0b12014-11-06 15:19:05 +02003099 if (!(host->quirks2 & SDHCI_QUIRK2_BROKEN_HS200))
David Cohen13868bf2013-10-29 10:58:26 -07003100 mmc->caps2 |= MMC_CAP2_HS200;
Giuseppe CAVALLARO156e14b2013-06-12 08:16:38 +02003101 } else if (caps[1] & SDHCI_SUPPORT_SDR50)
Arindam Nathf2119df2011-05-05 12:18:57 +05303102 mmc->caps |= MMC_CAP_UHS_SDR50;
3103
Adrian Huntere9fb05d2014-11-06 15:19:06 +02003104 if (host->quirks2 & SDHCI_QUIRK2_CAPS_BIT63_FOR_HS400 &&
3105 (caps[1] & SDHCI_SUPPORT_HS400))
3106 mmc->caps2 |= MMC_CAP2_HS400;
3107
Adrian Hunter549c0b12014-11-06 15:19:05 +02003108 if ((mmc->caps2 & MMC_CAP2_HSX00_1_2V) &&
3109 (IS_ERR(mmc->supply.vqmmc) ||
3110 !regulator_is_supported_voltage(mmc->supply.vqmmc, 1100000,
3111 1300000)))
3112 mmc->caps2 &= ~MMC_CAP2_HSX00_1_2V;
3113
Micky Ching9107ebb2014-02-21 18:40:35 +08003114 if ((caps[1] & SDHCI_SUPPORT_DDR50) &&
3115 !(host->quirks2 & SDHCI_QUIRK2_BROKEN_DDR50))
Arindam Nathf2119df2011-05-05 12:18:57 +05303116 mmc->caps |= MMC_CAP_UHS_DDR50;
3117
Girish K S069c9f12012-01-06 09:56:39 +05303118 /* Does the host need tuning for SDR50? */
Arindam Nathb513ea22011-05-05 12:19:04 +05303119 if (caps[1] & SDHCI_USE_SDR50_TUNING)
3120 host->flags |= SDHCI_SDR50_NEEDS_TUNING;
3121
Giuseppe CAVALLARO156e14b2013-06-12 08:16:38 +02003122 /* Does the host need tuning for SDR104 / HS200? */
Girish K S069c9f12012-01-06 09:56:39 +05303123 if (mmc->caps2 & MMC_CAP2_HS200)
Giuseppe CAVALLARO156e14b2013-06-12 08:16:38 +02003124 host->flags |= SDHCI_SDR104_NEEDS_TUNING;
Girish K S069c9f12012-01-06 09:56:39 +05303125
Arindam Nathd6d50a12011-05-05 12:18:59 +05303126 /* Driver Type(s) (A, C, D) supported by the host */
3127 if (caps[1] & SDHCI_DRIVER_TYPE_A)
3128 mmc->caps |= MMC_CAP_DRIVER_TYPE_A;
3129 if (caps[1] & SDHCI_DRIVER_TYPE_C)
3130 mmc->caps |= MMC_CAP_DRIVER_TYPE_C;
3131 if (caps[1] & SDHCI_DRIVER_TYPE_D)
3132 mmc->caps |= MMC_CAP_DRIVER_TYPE_D;
3133
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303134 /* Initial value for re-tuning timer count */
3135 host->tuning_count = (caps[1] & SDHCI_RETUNING_TIMER_COUNT_MASK) >>
3136 SDHCI_RETUNING_TIMER_COUNT_SHIFT;
3137
3138 /*
3139 * In case Re-tuning Timer is not disabled, the actual value of
3140 * re-tuning timer will be 2 ^ (n - 1).
3141 */
3142 if (host->tuning_count)
3143 host->tuning_count = 1 << (host->tuning_count - 1);
3144
3145 /* Re-tuning mode supported by the Host Controller */
3146 host->tuning_mode = (caps[1] & SDHCI_RETUNING_MODE_MASK) >>
3147 SDHCI_RETUNING_MODE_SHIFT;
3148
Takashi Iwai8f230f42010-12-08 10:04:30 +01003149 ocr_avail = 0;
Philip Rakitybad37e12012-05-27 18:36:44 -07003150
Arindam Nathf2119df2011-05-05 12:18:57 +05303151 /*
3152 * According to SD Host Controller spec v3.00, if the Host System
3153 * can afford more than 150mA, Host Driver should set XPC to 1. Also
3154 * the value is meaningful only if Voltage Support in the Capabilities
3155 * register is set. The actual current value is 4 times the register
3156 * value.
3157 */
3158 max_current_caps = sdhci_readl(host, SDHCI_MAX_CURRENT);
Tim Kryger3a48edc2014-06-13 10:13:56 -07003159 if (!max_current_caps && !IS_ERR(mmc->supply.vmmc)) {
Chuanxiao.Dongae906032014-08-01 14:00:13 +08003160 int curr = regulator_get_current_limit(mmc->supply.vmmc);
Philip Rakitybad37e12012-05-27 18:36:44 -07003161 if (curr > 0) {
3162
3163 /* convert to SDHCI_MAX_CURRENT format */
3164 curr = curr/1000; /* convert to mA */
3165 curr = curr/SDHCI_MAX_CURRENT_MULTIPLIER;
3166
3167 curr = min_t(u32, curr, SDHCI_MAX_CURRENT_LIMIT);
3168 max_current_caps =
3169 (curr << SDHCI_MAX_CURRENT_330_SHIFT) |
3170 (curr << SDHCI_MAX_CURRENT_300_SHIFT) |
3171 (curr << SDHCI_MAX_CURRENT_180_SHIFT);
3172 }
3173 }
Arindam Nathf2119df2011-05-05 12:18:57 +05303174
3175 if (caps[0] & SDHCI_CAN_VDD_330) {
Takashi Iwai8f230f42010-12-08 10:04:30 +01003176 ocr_avail |= MMC_VDD_32_33 | MMC_VDD_33_34;
Arindam Nathf2119df2011-05-05 12:18:57 +05303177
Aaron Lu55c46652012-07-04 13:31:48 +08003178 mmc->max_current_330 = ((max_current_caps &
Arindam Nathf2119df2011-05-05 12:18:57 +05303179 SDHCI_MAX_CURRENT_330_MASK) >>
3180 SDHCI_MAX_CURRENT_330_SHIFT) *
3181 SDHCI_MAX_CURRENT_MULTIPLIER;
Arindam Nathf2119df2011-05-05 12:18:57 +05303182 }
3183 if (caps[0] & SDHCI_CAN_VDD_300) {
Takashi Iwai8f230f42010-12-08 10:04:30 +01003184 ocr_avail |= MMC_VDD_29_30 | MMC_VDD_30_31;
Arindam Nathf2119df2011-05-05 12:18:57 +05303185
Aaron Lu55c46652012-07-04 13:31:48 +08003186 mmc->max_current_300 = ((max_current_caps &
Arindam Nathf2119df2011-05-05 12:18:57 +05303187 SDHCI_MAX_CURRENT_300_MASK) >>
3188 SDHCI_MAX_CURRENT_300_SHIFT) *
3189 SDHCI_MAX_CURRENT_MULTIPLIER;
Arindam Nathf2119df2011-05-05 12:18:57 +05303190 }
3191 if (caps[0] & SDHCI_CAN_VDD_180) {
Takashi Iwai8f230f42010-12-08 10:04:30 +01003192 ocr_avail |= MMC_VDD_165_195;
3193
Aaron Lu55c46652012-07-04 13:31:48 +08003194 mmc->max_current_180 = ((max_current_caps &
Arindam Nathf2119df2011-05-05 12:18:57 +05303195 SDHCI_MAX_CURRENT_180_MASK) >>
3196 SDHCI_MAX_CURRENT_180_SHIFT) *
3197 SDHCI_MAX_CURRENT_MULTIPLIER;
Arindam Nathf2119df2011-05-05 12:18:57 +05303198 }
3199
Ulf Hansson5fd26c72015-06-05 11:40:08 +02003200 /* If OCR set by host, use it instead. */
3201 if (host->ocr_mask)
3202 ocr_avail = host->ocr_mask;
3203
3204 /* If OCR set by external regulators, give it highest prio. */
Tim Kryger3a48edc2014-06-13 10:13:56 -07003205 if (mmc->ocr_avail)
Tim Kryger52221612014-06-25 00:25:34 -07003206 ocr_avail = mmc->ocr_avail;
Tim Kryger3a48edc2014-06-13 10:13:56 -07003207
Takashi Iwai8f230f42010-12-08 10:04:30 +01003208 mmc->ocr_avail = ocr_avail;
3209 mmc->ocr_avail_sdio = ocr_avail;
3210 if (host->ocr_avail_sdio)
3211 mmc->ocr_avail_sdio &= host->ocr_avail_sdio;
3212 mmc->ocr_avail_sd = ocr_avail;
3213 if (host->ocr_avail_sd)
3214 mmc->ocr_avail_sd &= host->ocr_avail_sd;
3215 else /* normal SD controllers don't support 1.8V */
3216 mmc->ocr_avail_sd &= ~MMC_VDD_165_195;
3217 mmc->ocr_avail_mmc = ocr_avail;
3218 if (host->ocr_avail_mmc)
3219 mmc->ocr_avail_mmc &= host->ocr_avail_mmc;
Pierre Ossman146ad662006-06-30 02:22:23 -07003220
3221 if (mmc->ocr_avail == 0) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01003222 pr_err("%s: Hardware doesn't report any support voltages.\n",
3223 mmc_hostname(mmc));
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003224 return -ENODEV;
Pierre Ossman146ad662006-06-30 02:22:23 -07003225 }
3226
Pierre Ossmand129bce2006-03-24 03:18:17 -08003227 spin_lock_init(&host->lock);
3228
3229 /*
Pierre Ossman2134a922008-06-28 18:28:51 +02003230 * Maximum number of segments. Depends on if the hardware
3231 * can do scatter/gather or not.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003232 */
Pierre Ossman2134a922008-06-28 18:28:51 +02003233 if (host->flags & SDHCI_USE_ADMA)
Adrian Hunter4fb213f2014-11-04 12:42:43 +02003234 mmc->max_segs = SDHCI_MAX_SEGS;
Richard Röjforsa13abc72009-09-22 16:45:30 -07003235 else if (host->flags & SDHCI_USE_SDMA)
Martin K. Petersena36274e2010-09-10 01:33:59 -04003236 mmc->max_segs = 1;
Pierre Ossman2134a922008-06-28 18:28:51 +02003237 else /* PIO */
Adrian Hunter4fb213f2014-11-04 12:42:43 +02003238 mmc->max_segs = SDHCI_MAX_SEGS;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003239
3240 /*
Adrian Hunterac005312014-12-05 19:25:28 +02003241 * Maximum number of sectors in one transfer. Limited by SDMA boundary
3242 * size (512KiB). Note some tuning modes impose a 4MiB limit, but this
3243 * is less anyway.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003244 */
Pierre Ossman55db8902006-11-21 17:55:45 +01003245 mmc->max_req_size = 524288;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003246
3247 /*
3248 * Maximum segment size. Could be one segment with the maximum number
Pierre Ossman2134a922008-06-28 18:28:51 +02003249 * of bytes. When doing hardware scatter/gather, each entry cannot
3250 * be larger than 64 KiB though.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003251 */
Olof Johansson30652aa2011-01-01 18:37:32 -06003252 if (host->flags & SDHCI_USE_ADMA) {
3253 if (host->quirks & SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC)
3254 mmc->max_seg_size = 65535;
3255 else
3256 mmc->max_seg_size = 65536;
3257 } else {
Pierre Ossman2134a922008-06-28 18:28:51 +02003258 mmc->max_seg_size = mmc->max_req_size;
Olof Johansson30652aa2011-01-01 18:37:32 -06003259 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003260
3261 /*
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01003262 * Maximum block size. This varies from controller to controller and
3263 * is specified in the capabilities register.
3264 */
Anton Vorontsov0633f652009-03-17 00:14:03 +03003265 if (host->quirks & SDHCI_QUIRK_FORCE_BLK_SZ_2048) {
3266 mmc->max_blk_size = 2;
3267 } else {
Arindam Nathf2119df2011-05-05 12:18:57 +05303268 mmc->max_blk_size = (caps[0] & SDHCI_MAX_BLOCK_MASK) >>
Anton Vorontsov0633f652009-03-17 00:14:03 +03003269 SDHCI_MAX_BLOCK_SHIFT;
3270 if (mmc->max_blk_size >= 3) {
Joe Perches66061102014-09-12 14:56:56 -07003271 pr_warn("%s: Invalid maximum block size, assuming 512 bytes\n",
3272 mmc_hostname(mmc));
Anton Vorontsov0633f652009-03-17 00:14:03 +03003273 mmc->max_blk_size = 0;
3274 }
3275 }
3276
3277 mmc->max_blk_size = 512 << mmc->max_blk_size;
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01003278
3279 /*
Pierre Ossman55db8902006-11-21 17:55:45 +01003280 * Maximum block count.
3281 */
Ben Dooks1388eef2009-06-14 12:40:53 +01003282 mmc->max_blk_count = (host->quirks & SDHCI_QUIRK_NO_MULTIBLOCK) ? 1 : 65535;
Pierre Ossman55db8902006-11-21 17:55:45 +01003283
3284 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08003285 * Init tasklets.
3286 */
Pierre Ossmand129bce2006-03-24 03:18:17 -08003287 tasklet_init(&host->finish_tasklet,
3288 sdhci_tasklet_finish, (unsigned long)host);
3289
Al Viroe4cad1b2006-10-10 22:47:07 +01003290 setup_timer(&host->timer, sdhci_timeout_timer, (unsigned long)host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003291
Adrian Hunter250fb7b42014-12-05 19:41:10 +02003292 init_waitqueue_head(&host->buf_ready_int);
Arindam Nathb513ea22011-05-05 12:19:04 +05303293
Shawn Guo2af502c2013-07-05 14:38:55 +08003294 sdhci_init(host, 0);
3295
Russell King781e9892014-04-25 12:55:46 +01003296 ret = request_threaded_irq(host->irq, sdhci_irq, sdhci_thread_irq,
3297 IRQF_SHARED, mmc_hostname(mmc), host);
Mark Brown0fc81ee2012-07-02 14:26:15 +01003298 if (ret) {
3299 pr_err("%s: Failed to request IRQ %d: %d\n",
3300 mmc_hostname(mmc), host->irq, ret);
Pierre Ossman8ef1a142006-06-30 02:22:21 -07003301 goto untasklet;
Mark Brown0fc81ee2012-07-02 14:26:15 +01003302 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003303
Pierre Ossmand129bce2006-03-24 03:18:17 -08003304#ifdef CONFIG_MMC_DEBUG
3305 sdhci_dumpregs(host);
3306#endif
3307
Pierre Ossmanf9134312008-12-21 17:01:48 +01003308#ifdef SDHCI_USE_LEDS_CLASS
Helmut Schaa5dbace02009-02-14 16:22:39 +01003309 snprintf(host->led_name, sizeof(host->led_name),
3310 "%s::", mmc_hostname(mmc));
3311 host->led.name = host->led_name;
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003312 host->led.brightness = LED_OFF;
3313 host->led.default_trigger = mmc_hostname(mmc);
3314 host->led.brightness_set = sdhci_led_control;
3315
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003316 ret = led_classdev_register(mmc_dev(mmc), &host->led);
Mark Brown0fc81ee2012-07-02 14:26:15 +01003317 if (ret) {
3318 pr_err("%s: Failed to register LED device: %d\n",
3319 mmc_hostname(mmc), ret);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003320 goto reset;
Mark Brown0fc81ee2012-07-02 14:26:15 +01003321 }
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003322#endif
3323
Pierre Ossman5f25a662006-10-04 02:15:39 -07003324 mmiowb();
3325
Pierre Ossmand129bce2006-03-24 03:18:17 -08003326 mmc_add_host(mmc);
3327
Girish K Sa3c76eb2011-10-11 11:44:09 +05303328 pr_info("%s: SDHCI controller on %s [%s] using %s\n",
Kay Sieversd1b26862008-11-08 21:37:46 +01003329 mmc_hostname(mmc), host->hw_name, dev_name(mmc_dev(mmc)),
Adrian Huntere57a5f62014-11-04 12:42:46 +02003330 (host->flags & SDHCI_USE_ADMA) ?
3331 (host->flags & SDHCI_USE_64_BIT_DMA) ? "ADMA 64-bit" : "ADMA" :
Richard Röjforsa13abc72009-09-22 16:45:30 -07003332 (host->flags & SDHCI_USE_SDMA) ? "DMA" : "PIO");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003333
Anton Vorontsov7260cf52009-03-17 00:13:48 +03003334 sdhci_enable_card_detection(host);
3335
Pierre Ossmand129bce2006-03-24 03:18:17 -08003336 return 0;
3337
Pierre Ossmanf9134312008-12-21 17:01:48 +01003338#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003339reset:
Russell King03231f92014-04-25 12:57:12 +01003340 sdhci_do_reset(host, SDHCI_RESET_ALL);
Russell Kingb537f942014-04-25 12:56:01 +01003341 sdhci_writel(host, 0, SDHCI_INT_ENABLE);
3342 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003343 free_irq(host->irq, host);
3344#endif
Pierre Ossman8ef1a142006-06-30 02:22:21 -07003345untasklet:
Pierre Ossmand129bce2006-03-24 03:18:17 -08003346 tasklet_kill(&host->finish_tasklet);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003347
3348 return ret;
3349}
3350
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003351EXPORT_SYMBOL_GPL(sdhci_add_host);
3352
Pierre Ossman1e728592008-04-16 19:13:13 +02003353void sdhci_remove_host(struct sdhci_host *host, int dead)
Pierre Ossmand129bce2006-03-24 03:18:17 -08003354{
Tim Kryger3a48edc2014-06-13 10:13:56 -07003355 struct mmc_host *mmc = host->mmc;
Pierre Ossman1e728592008-04-16 19:13:13 +02003356 unsigned long flags;
3357
3358 if (dead) {
3359 spin_lock_irqsave(&host->lock, flags);
3360
3361 host->flags |= SDHCI_DEVICE_DEAD;
3362
3363 if (host->mrq) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05303364 pr_err("%s: Controller removed during "
Markus Mayer4e743f12014-07-03 13:27:42 -07003365 " transfer!\n", mmc_hostname(mmc));
Pierre Ossman1e728592008-04-16 19:13:13 +02003366
3367 host->mrq->cmd->error = -ENOMEDIUM;
3368 tasklet_schedule(&host->finish_tasklet);
3369 }
3370
3371 spin_unlock_irqrestore(&host->lock, flags);
3372 }
3373
Anton Vorontsov7260cf52009-03-17 00:13:48 +03003374 sdhci_disable_card_detection(host);
3375
Markus Mayer4e743f12014-07-03 13:27:42 -07003376 mmc_remove_host(mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003377
Pierre Ossmanf9134312008-12-21 17:01:48 +01003378#ifdef SDHCI_USE_LEDS_CLASS
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003379 led_classdev_unregister(&host->led);
3380#endif
3381
Pierre Ossman1e728592008-04-16 19:13:13 +02003382 if (!dead)
Russell King03231f92014-04-25 12:57:12 +01003383 sdhci_do_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003384
Russell Kingb537f942014-04-25 12:56:01 +01003385 sdhci_writel(host, 0, SDHCI_INT_ENABLE);
3386 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003387 free_irq(host->irq, host);
3388
3389 del_timer_sync(&host->timer);
3390
Pierre Ossmand129bce2006-03-24 03:18:17 -08003391 tasklet_kill(&host->finish_tasklet);
Pierre Ossman2134a922008-06-28 18:28:51 +02003392
Tim Kryger3a48edc2014-06-13 10:13:56 -07003393 if (!IS_ERR(mmc->supply.vqmmc))
3394 regulator_disable(mmc->supply.vqmmc);
Philip Rakity6231f3d2012-07-23 15:56:23 -07003395
Russell Kingedd63fc2016-01-26 13:39:50 +00003396 if (host->align_buffer)
Russell Kinge66e61c2016-01-26 13:39:55 +00003397 dma_free_coherent(mmc_dev(mmc), host->align_buffer_sz +
3398 host->adma_table_sz, host->align_buffer,
3399 host->align_addr);
Pierre Ossman2134a922008-06-28 18:28:51 +02003400
Adrian Hunter4efaa6f2014-11-04 12:42:39 +02003401 host->adma_table = NULL;
Pierre Ossman2134a922008-06-28 18:28:51 +02003402 host->align_buffer = NULL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003403}
3404
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003405EXPORT_SYMBOL_GPL(sdhci_remove_host);
3406
3407void sdhci_free_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08003408{
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003409 mmc_free_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003410}
3411
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003412EXPORT_SYMBOL_GPL(sdhci_free_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003413
3414/*****************************************************************************\
3415 * *
3416 * Driver init/exit *
3417 * *
3418\*****************************************************************************/
3419
3420static int __init sdhci_drv_init(void)
3421{
Girish K Sa3c76eb2011-10-11 11:44:09 +05303422 pr_info(DRIVER_NAME
Pierre Ossman52fbf9c2007-02-09 08:23:41 +01003423 ": Secure Digital Host Controller Interface driver\n");
Girish K Sa3c76eb2011-10-11 11:44:09 +05303424 pr_info(DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003425
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003426 return 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003427}
3428
3429static void __exit sdhci_drv_exit(void)
3430{
Pierre Ossmand129bce2006-03-24 03:18:17 -08003431}
3432
3433module_init(sdhci_drv_init);
3434module_exit(sdhci_drv_exit);
3435
Pierre Ossmandf673b22006-06-30 02:22:31 -07003436module_param(debug_quirks, uint, 0444);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003437module_param(debug_quirks2, uint, 0444);
Pierre Ossman67435272006-06-30 02:22:31 -07003438
Pierre Ossman32710e82009-04-08 20:14:54 +02003439MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003440MODULE_DESCRIPTION("Secure Digital Host Controller Interface core driver");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003441MODULE_LICENSE("GPL");
Pierre Ossman67435272006-06-30 02:22:31 -07003442
Pierre Ossmandf673b22006-06-30 02:22:31 -07003443MODULE_PARM_DESC(debug_quirks, "Force certain quirks.");
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003444MODULE_PARM_DESC(debug_quirks2, "Force certain other quirks.");