blob: 744e5201aa9c785b88951457290857e2fd74360e [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 Lu473b095a2012-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>
Asutosh Dasb58499d2013-07-30 19:07:29 +053033#include <linux/mmc/sdio.h>
Pierre Ossmand129bce2006-03-24 03:18:17 -080034
Venkat Gopalakrishnana71fbae2014-06-09 14:00:31 -070035#include <trace/events/mmc.h>
36
Pierre Ossmand129bce2006-03-24 03:18:17 -080037#include "sdhci.h"
Asutosh Das3621b372014-10-17 16:36:47 +053038#include "cmdq_hci.h"
Pierre Ossmand129bce2006-03-24 03:18:17 -080039
40#define DRIVER_NAME "sdhci"
Pierre Ossmand129bce2006-03-24 03:18:17 -080041
Pierre Ossmand129bce2006-03-24 03:18:17 -080042#define DBG(f, x...) \
Russell Kingc6563172006-03-29 09:30:20 +010043 pr_debug(DRIVER_NAME " [%s()]: " f, __func__,## x)
Pierre Ossmand129bce2006-03-24 03:18:17 -080044
Arindam Nathb513ea22011-05-05 12:19:04 +053045#define MAX_TUNING_LOOP 40
46
Sahitya Tummala4c196de2014-10-31 14:00:12 +053047#define SDHCI_DBG_DUMP_RS_INTERVAL (10 * HZ)
48#define SDHCI_DBG_DUMP_RS_BURST 2
49
Pierre Ossmandf673b22006-06-30 02:22:31 -070050static unsigned int debug_quirks = 0;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +030051static unsigned int debug_quirks2;
Pierre Ossman67435272006-06-30 02:22:31 -070052
Pierre Ossmand129bce2006-03-24 03:18:17 -080053static void sdhci_finish_data(struct sdhci_host *);
54
Sahitya Tummalaea4e3aa2013-05-24 14:08:10 +053055static bool sdhci_check_state(struct sdhci_host *);
56
Ritesh Harjanice4b8f82015-11-25 10:37:21 +053057static void sdhci_enable_sdio_irq_nolock(struct sdhci_host *host, int enable);
58
Kevin Liu52983382013-01-31 11:31:37 +080059static void sdhci_enable_preset_value(struct sdhci_host *host, bool enable);
Pierre Ossmand129bce2006-03-24 03:18:17 -080060
Asutosh Das494003b2013-03-20 22:53:40 +053061static void sdhci_dump_state(struct sdhci_host *host)
62{
63 struct mmc_host *mmc = host->mmc;
64
65 #ifdef CONFIG_MMC_CLKGATE
Ritesh Harjanice4b8f82015-11-25 10:37:21 +053066 pr_info("%s: clk: %d clk-gated: %d claimer: %s pwr: %d host->irq = %d\n",
Asutosh Das494003b2013-03-20 22:53:40 +053067 mmc_hostname(mmc), host->clock, mmc->clk_gated,
Ritesh Harjanice4b8f82015-11-25 10:37:21 +053068 mmc->claimer->comm, host->pwr,
69 (host->flags & SDHCI_HOST_IRQ_STATUS));
Asutosh Das494003b2013-03-20 22:53:40 +053070 #else
71 pr_info("%s: clk: %d claimer: %s pwr: %d\n",
72 mmc_hostname(mmc), host->clock,
73 mmc->claimer->comm, host->pwr);
74 #endif
75 pr_info("%s: rpmstatus[pltfm](runtime-suspend:usage_count:disable_depth)(%d:%d:%d)\n",
76 mmc_hostname(mmc), mmc->parent->power.runtime_status,
77 atomic_read(&mmc->parent->power.usage_count),
78 mmc->parent->power.disable_depth);
79}
80
Pierre Ossmand129bce2006-03-24 03:18:17 -080081static void sdhci_dumpregs(struct sdhci_host *host)
82{
Sayali Lokhandebff771e2016-11-30 11:35:22 +053083 MMC_TRACE(host->mmc,
84 "%s: 0x04=0x%08x 0x06=0x%08x 0x0E=0x%08x 0x30=0x%08x 0x34=0x%08x 0x38=0x%08x\n",
85 __func__,
86 sdhci_readw(host, SDHCI_BLOCK_SIZE),
87 sdhci_readw(host, SDHCI_BLOCK_COUNT),
88 sdhci_readw(host, SDHCI_COMMAND),
89 sdhci_readl(host, SDHCI_INT_STATUS),
90 sdhci_readl(host, SDHCI_INT_ENABLE),
91 sdhci_readl(host, SDHCI_SIGNAL_ENABLE));
92 mmc_stop_tracing(host->mmc);
93
Asutosh Das494003b2013-03-20 22:53:40 +053094 pr_info(DRIVER_NAME ": =========== REGISTER DUMP (%s)===========\n",
95 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -080096
Asutosh Das494003b2013-03-20 22:53:40 +053097 pr_info(DRIVER_NAME ": Sys addr: 0x%08x | Version: 0x%08x\n",
Chuanxiao Donga7c53672016-06-22 14:40:01 +030098 sdhci_readl(host, SDHCI_DMA_ADDRESS),
99 sdhci_readw(host, SDHCI_HOST_VERSION));
Asutosh Das494003b2013-03-20 22:53:40 +0530100 pr_info(DRIVER_NAME ": Blk size: 0x%08x | Blk cnt: 0x%08x\n",
Chuanxiao Donga7c53672016-06-22 14:40:01 +0300101 sdhci_readw(host, SDHCI_BLOCK_SIZE),
102 sdhci_readw(host, SDHCI_BLOCK_COUNT));
Asutosh Das494003b2013-03-20 22:53:40 +0530103 pr_info(DRIVER_NAME ": Argument: 0x%08x | Trn mode: 0x%08x\n",
Chuanxiao Donga7c53672016-06-22 14:40:01 +0300104 sdhci_readl(host, SDHCI_ARGUMENT),
105 sdhci_readw(host, SDHCI_TRANSFER_MODE));
Asutosh Das494003b2013-03-20 22:53:40 +0530106 pr_info(DRIVER_NAME ": Present: 0x%08x | Host ctl: 0x%08x\n",
Chuanxiao Donga7c53672016-06-22 14:40:01 +0300107 sdhci_readl(host, SDHCI_PRESENT_STATE),
108 sdhci_readb(host, SDHCI_HOST_CONTROL));
Asutosh Das494003b2013-03-20 22:53:40 +0530109 pr_info(DRIVER_NAME ": Power: 0x%08x | Blk gap: 0x%08x\n",
Chuanxiao Donga7c53672016-06-22 14:40:01 +0300110 sdhci_readb(host, SDHCI_POWER_CONTROL),
111 sdhci_readb(host, SDHCI_BLOCK_GAP_CONTROL));
Asutosh Das494003b2013-03-20 22:53:40 +0530112 pr_info(DRIVER_NAME ": Wake-up: 0x%08x | Clock: 0x%08x\n",
Chuanxiao Donga7c53672016-06-22 14:40:01 +0300113 sdhci_readb(host, SDHCI_WAKE_UP_CONTROL),
114 sdhci_readw(host, SDHCI_CLOCK_CONTROL));
Asutosh Das494003b2013-03-20 22:53:40 +0530115 pr_info(DRIVER_NAME ": Timeout: 0x%08x | Int stat: 0x%08x\n",
Chuanxiao Donga7c53672016-06-22 14:40:01 +0300116 sdhci_readb(host, SDHCI_TIMEOUT_CONTROL),
117 sdhci_readl(host, SDHCI_INT_STATUS));
Asutosh Das494003b2013-03-20 22:53:40 +0530118 pr_info(DRIVER_NAME ": Int enab: 0x%08x | Sig enab: 0x%08x\n",
Chuanxiao Donga7c53672016-06-22 14:40:01 +0300119 sdhci_readl(host, SDHCI_INT_ENABLE),
120 sdhci_readl(host, SDHCI_SIGNAL_ENABLE));
Asutosh Das494003b2013-03-20 22:53:40 +0530121 pr_info(DRIVER_NAME ": AC12 err: 0x%08x | Slot int: 0x%08x\n",
Sahitya Tummala9e7fadb2013-08-07 18:40:29 +0530122 host->auto_cmd_err_sts,
Chuanxiao Donga7c53672016-06-22 14:40:01 +0300123 sdhci_readw(host, SDHCI_SLOT_INT_STATUS));
Asutosh Das494003b2013-03-20 22:53:40 +0530124 pr_info(DRIVER_NAME ": Caps: 0x%08x | Caps_1: 0x%08x\n",
Chuanxiao Donga7c53672016-06-22 14:40:01 +0300125 sdhci_readl(host, SDHCI_CAPABILITIES),
126 sdhci_readl(host, SDHCI_CAPABILITIES_1));
Asutosh Das494003b2013-03-20 22:53:40 +0530127 pr_info(DRIVER_NAME ": Cmd: 0x%08x | Max curr: 0x%08x\n",
Chuanxiao Donga7c53672016-06-22 14:40:01 +0300128 sdhci_readw(host, SDHCI_COMMAND),
129 sdhci_readl(host, SDHCI_MAX_CURRENT));
Asutosh Das09f36d02013-07-23 16:20:34 +0530130 pr_info(DRIVER_NAME ": Resp 1: 0x%08x | Resp 0: 0x%08x\n",
131 sdhci_readl(host, SDHCI_RESPONSE + 0x4),
132 sdhci_readl(host, SDHCI_RESPONSE));
133 pr_info(DRIVER_NAME ": Resp 3: 0x%08x | Resp 2: 0x%08x\n",
134 sdhci_readl(host, SDHCI_RESPONSE + 0xC),
135 sdhci_readl(host, SDHCI_RESPONSE + 0x8));
Asutosh Das494003b2013-03-20 22:53:40 +0530136 pr_info(DRIVER_NAME ": Host ctl2: 0x%08x\n",
Chuanxiao Donga7c53672016-06-22 14:40:01 +0300137 sdhci_readw(host, SDHCI_HOST_CONTROL2));
Pierre Ossmand129bce2006-03-24 03:18:17 -0800138
Adrian Huntere57a5f62014-11-04 12:42:46 +0200139 if (host->flags & SDHCI_USE_ADMA) {
140 if (host->flags & SDHCI_USE_64_BIT_DMA)
Asutosh Das494003b2013-03-20 22:53:40 +0530141 pr_info(DRIVER_NAME ": ADMA Err: 0x%08x | ADMA Ptr: 0x%08x%08x\n",
Chuanxiao Donga7c53672016-06-22 14:40:01 +0300142 readl(host->ioaddr + SDHCI_ADMA_ERROR),
143 readl(host->ioaddr + SDHCI_ADMA_ADDRESS_HI),
144 readl(host->ioaddr + SDHCI_ADMA_ADDRESS));
Adrian Huntere57a5f62014-11-04 12:42:46 +0200145 else
Asutosh Das494003b2013-03-20 22:53:40 +0530146 pr_info(DRIVER_NAME ": ADMA Err: 0x%08x | ADMA Ptr: 0x%08x\n",
Chuanxiao Donga7c53672016-06-22 14:40:01 +0300147 readl(host->ioaddr + SDHCI_ADMA_ERROR),
148 readl(host->ioaddr + SDHCI_ADMA_ADDRESS));
Adrian Huntere57a5f62014-11-04 12:42:46 +0200149 }
Ben Dooksbe3f4ae2009-06-08 23:33:52 +0100150
Sahitya Tummala91d315e2013-08-02 09:17:54 +0530151 if (host->ops->dump_vendor_regs)
152 host->ops->dump_vendor_regs(host);
Asutosh Das494003b2013-03-20 22:53:40 +0530153 sdhci_dump_state(host);
154 pr_info(DRIVER_NAME ": ===========================================\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800155}
156
157/*****************************************************************************\
158 * *
159 * Low level functions *
160 * *
161\*****************************************************************************/
162
Adrian Hunter56a590d2016-06-29 16:24:32 +0300163static inline bool sdhci_data_line_cmd(struct mmc_command *cmd)
164{
165 return cmd->data || cmd->flags & MMC_RSP_BUSY;
166}
167
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300168static void sdhci_set_card_detection(struct sdhci_host *host, bool enable)
169{
Russell King5b4f1f62014-04-25 12:57:02 +0100170 u32 present;
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300171
Adrian Hunterc79396c2011-12-27 15:48:42 +0200172 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) ||
Jaehoon Chung860951c2016-06-21 10:13:26 +0900173 !mmc_card_is_removable(host->mmc))
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300174 return;
175
Russell King5b4f1f62014-04-25 12:57:02 +0100176 if (enable) {
177 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
178 SDHCI_CARD_PRESENT;
Shawn Guod25928d2011-06-21 22:41:48 +0800179
Russell King5b4f1f62014-04-25 12:57:02 +0100180 host->ier |= present ? SDHCI_INT_CARD_REMOVE :
181 SDHCI_INT_CARD_INSERT;
182 } else {
183 host->ier &= ~(SDHCI_INT_CARD_REMOVE | SDHCI_INT_CARD_INSERT);
184 }
Russell Kingb537f942014-04-25 12:56:01 +0100185
186 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
187 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300188}
189
190static void sdhci_enable_card_detection(struct sdhci_host *host)
191{
192 sdhci_set_card_detection(host, true);
193}
194
195static void sdhci_disable_card_detection(struct sdhci_host *host)
196{
197 sdhci_set_card_detection(host, false);
198}
199
Ulf Hansson02d0b682016-04-11 15:32:41 +0200200static void sdhci_runtime_pm_bus_on(struct sdhci_host *host)
201{
202 if (host->bus_on)
203 return;
204 host->bus_on = true;
205 pm_runtime_get_noresume(host->mmc->parent);
206}
207
208static void sdhci_runtime_pm_bus_off(struct sdhci_host *host)
209{
210 if (!host->bus_on)
211 return;
212 host->bus_on = false;
213 pm_runtime_put_noidle(host->mmc->parent);
214}
215
Russell King03231f92014-04-25 12:57:12 +0100216void sdhci_reset(struct sdhci_host *host, u8 mask)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800217{
Pierre Ossmane16514d82006-06-30 02:22:24 -0700218 unsigned long timeout;
Philip Rakity393c1a32011-01-21 11:26:40 -0800219
Pavan Anamulae1ec2a72015-08-25 15:00:25 +0530220retry_reset:
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300221 sdhci_writeb(host, mask, SDHCI_SOFTWARE_RESET);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800222
Adrian Hunterf0710a52013-05-06 12:17:32 +0300223 if (mask & SDHCI_RESET_ALL) {
Pierre Ossmand129bce2006-03-24 03:18:17 -0800224 host->clock = 0;
Adrian Hunterf0710a52013-05-06 12:17:32 +0300225 /* Reset-all turns off SD Bus Power */
226 if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
227 sdhci_runtime_pm_bus_off(host);
228 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800229
Pierre Ossmane16514d82006-06-30 02:22:24 -0700230 /* Wait max 100 ms */
Venkat Gopalakrishnanc90fa962014-04-09 10:54:29 -0700231 timeout = 100000;
Pierre Ossmane16514d82006-06-30 02:22:24 -0700232
Sahitya Tummala63d235d2013-02-21 10:09:49 +0530233 if (host->ops->check_power_status && host->pwr &&
234 (mask & SDHCI_RESET_ALL))
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +0530235 host->ops->check_power_status(host, REQ_BUS_OFF);
Sahitya Tummala63d235d2013-02-21 10:09:49 +0530236
Sujit Reddy Thumma23dd7f82014-02-14 08:37:47 +0530237 /* clear pending normal/error interrupt status */
238 sdhci_writel(host, sdhci_readl(host, SDHCI_INT_STATUS),
239 SDHCI_INT_STATUS);
240
Pierre Ossmane16514d82006-06-30 02:22:24 -0700241 /* hw clears the bit when it's done */
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300242 while (sdhci_readb(host, SDHCI_SOFTWARE_RESET) & mask) {
Pierre Ossmane16514d82006-06-30 02:22:24 -0700243 if (timeout == 0) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530244 pr_err("%s: Reset 0x%x never completed.\n",
Pierre Ossmane16514d82006-06-30 02:22:24 -0700245 mmc_hostname(host->mmc), (int)mask);
Pavan Anamulae1ec2a72015-08-25 15:00:25 +0530246 if ((host->quirks2 & SDHCI_QUIRK2_USE_RESET_WORKAROUND)
247 && host->ops->reset_workaround) {
248 if (!host->reset_wa_applied) {
249 /*
250 * apply the workaround and issue
251 * reset again.
252 */
253 host->ops->reset_workaround(host, 1);
254 host->reset_wa_applied = 1;
255 host->reset_wa_cnt++;
256 goto retry_reset;
257 } else {
258 pr_err("%s: Reset 0x%x failed with workaround\n",
259 mmc_hostname(host->mmc),
260 (int)mask);
261 /* clear the workaround */
262 host->ops->reset_workaround(host, 0);
263 host->reset_wa_applied = 0;
264 }
265 }
266
Pierre Ossmane16514d82006-06-30 02:22:24 -0700267 sdhci_dumpregs(host);
268 return;
269 }
270 timeout--;
Ritesh Harjani16cf7882015-12-23 13:21:51 +0530271 udelay(1);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800272 }
Pavan Anamulae1ec2a72015-08-25 15:00:25 +0530273
274 if ((host->quirks2 & SDHCI_QUIRK2_USE_RESET_WORKAROUND) &&
275 host->ops->reset_workaround && host->reset_wa_applied) {
276 pr_info("%s: Reset 0x%x successful with workaround\n",
277 mmc_hostname(host->mmc), (int)mask);
278 /* clear the workaround */
279 host->ops->reset_workaround(host, 0);
280 host->reset_wa_applied = 0;
281 }
282
Russell King03231f92014-04-25 12:57:12 +0100283}
284EXPORT_SYMBOL_GPL(sdhci_reset);
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300285
Russell King03231f92014-04-25 12:57:12 +0100286static void sdhci_do_reset(struct sdhci_host *host, u8 mask)
287{
288 if (host->quirks & SDHCI_QUIRK_NO_CARD_NO_RESET) {
Adrian Hunterd3940f22016-06-29 16:24:14 +0300289 struct mmc_host *mmc = host->mmc;
290
291 if (!mmc->ops->get_cd(mmc))
Russell King03231f92014-04-25 12:57:12 +0100292 return;
293 }
294
295 host->ops->reset(host, mask);
Philip Rakity393c1a32011-01-21 11:26:40 -0800296
Russell Kingda91a8f2014-04-25 13:00:12 +0100297 if (mask & SDHCI_RESET_ALL) {
298 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
299 if (host->ops->enable_dma)
300 host->ops->enable_dma(host);
301 }
302
303 /* Resetting the controller clears many */
304 host->preset_enabled = false;
Shaohui Xie3abc1e802011-12-29 16:33:00 +0800305 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800306}
307
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800308static void sdhci_init(struct sdhci_host *host, int soft)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800309{
Adrian Hunterd3940f22016-06-29 16:24:14 +0300310 struct mmc_host *mmc = host->mmc;
311
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800312 if (soft)
Russell King03231f92014-04-25 12:57:12 +0100313 sdhci_do_reset(host, SDHCI_RESET_CMD|SDHCI_RESET_DATA);
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800314 else
Russell King03231f92014-04-25 12:57:12 +0100315 sdhci_do_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800316
Russell Kingb537f942014-04-25 12:56:01 +0100317 host->ier = SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT |
318 SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT |
319 SDHCI_INT_INDEX | SDHCI_INT_END_BIT | SDHCI_INT_CRC |
320 SDHCI_INT_TIMEOUT | SDHCI_INT_DATA_END |
Asutosh Das09f36d02013-07-23 16:20:34 +0530321 SDHCI_INT_RESPONSE | SDHCI_INT_AUTO_CMD_ERR;
Russell Kingb537f942014-04-25 12:56:01 +0100322
Dong Aishengf37b20e2016-07-12 15:46:17 +0800323 if (host->tuning_mode == SDHCI_TUNING_MODE_2 ||
324 host->tuning_mode == SDHCI_TUNING_MODE_3)
325 host->ier |= SDHCI_INT_RETUNE;
326
Russell Kingb537f942014-04-25 12:56:01 +0100327 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
328 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800329
330 if (soft) {
331 /* force clock reconfiguration */
332 host->clock = 0;
Adrian Hunterd3940f22016-06-29 16:24:14 +0300333 mmc->ops->set_ios(mmc, &mmc->ios);
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800334 }
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300335}
Pierre Ossmand129bce2006-03-24 03:18:17 -0800336
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300337static void sdhci_reinit(struct sdhci_host *host)
338{
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800339 sdhci_init(host, 0);
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300340 sdhci_enable_card_detection(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800341}
342
Adrian Hunter061d17a2016-04-12 14:25:09 +0300343static void __sdhci_led_activate(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800344{
345 u8 ctrl;
346
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300347 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800348 ctrl |= SDHCI_CTRL_LED;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300349 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800350}
351
Adrian Hunter061d17a2016-04-12 14:25:09 +0300352static void __sdhci_led_deactivate(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800353{
354 u8 ctrl;
355
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300356 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800357 ctrl &= ~SDHCI_CTRL_LED;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300358 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800359}
360
Masahiro Yamada4f782302016-04-14 13:19:39 +0900361#if IS_REACHABLE(CONFIG_LEDS_CLASS)
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100362static void sdhci_led_control(struct led_classdev *led,
Adrian Hunter061d17a2016-04-12 14:25:09 +0300363 enum led_brightness brightness)
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100364{
365 struct sdhci_host *host = container_of(led, struct sdhci_host, led);
366 unsigned long flags;
367
Sahitya Tummalae8b0de92014-04-07 10:33:11 +0530368 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
369 return;
370
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100371 spin_lock_irqsave(&host->lock, flags);
372
Sahitya Tummalaea4e3aa2013-05-24 14:08:10 +0530373 if (host->runtime_suspended || sdhci_check_state(host))
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300374 goto out;
375
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100376 if (brightness == LED_OFF)
Adrian Hunter061d17a2016-04-12 14:25:09 +0300377 __sdhci_led_deactivate(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100378 else
Adrian Hunter061d17a2016-04-12 14:25:09 +0300379 __sdhci_led_activate(host);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300380out:
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100381 spin_unlock_irqrestore(&host->lock, flags);
382}
Adrian Hunter061d17a2016-04-12 14:25:09 +0300383
384static int sdhci_led_register(struct sdhci_host *host)
385{
386 struct mmc_host *mmc = host->mmc;
387
388 snprintf(host->led_name, sizeof(host->led_name),
389 "%s::", mmc_hostname(mmc));
390
391 host->led.name = host->led_name;
392 host->led.brightness = LED_OFF;
393 host->led.default_trigger = mmc_hostname(mmc);
394 host->led.brightness_set = sdhci_led_control;
395
396 return led_classdev_register(mmc_dev(mmc), &host->led);
397}
398
399static void sdhci_led_unregister(struct sdhci_host *host)
400{
401 led_classdev_unregister(&host->led);
402}
403
404static inline void sdhci_led_activate(struct sdhci_host *host)
405{
406}
407
408static inline void sdhci_led_deactivate(struct sdhci_host *host)
409{
410}
411
412#else
413
414static inline int sdhci_led_register(struct sdhci_host *host)
415{
416 return 0;
417}
418
419static inline void sdhci_led_unregister(struct sdhci_host *host)
420{
421}
422
423static inline void sdhci_led_activate(struct sdhci_host *host)
424{
425 __sdhci_led_activate(host);
426}
427
428static inline void sdhci_led_deactivate(struct sdhci_host *host)
429{
430 __sdhci_led_deactivate(host);
431}
432
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100433#endif
434
Pierre Ossmand129bce2006-03-24 03:18:17 -0800435/*****************************************************************************\
436 * *
437 * Core functions *
438 * *
439\*****************************************************************************/
440
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100441static void sdhci_read_block_pio(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800442{
Pierre Ossman76591502008-07-21 00:32:11 +0200443 unsigned long flags;
444 size_t blksize, len, chunk;
Steven Noonan7244b852008-10-01 01:50:25 -0700445 u32 uninitialized_var(scratch);
Pierre Ossman76591502008-07-21 00:32:11 +0200446 u8 *buf;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800447
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100448 DBG("PIO reading\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800449
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100450 blksize = host->data->blksz;
Pierre Ossman76591502008-07-21 00:32:11 +0200451 chunk = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800452
Pierre Ossman76591502008-07-21 00:32:11 +0200453 local_irq_save(flags);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800454
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100455 while (blksize) {
Fabio Estevambf3a35a2015-05-09 18:44:51 -0300456 BUG_ON(!sg_miter_next(&host->sg_miter));
Pierre Ossmand129bce2006-03-24 03:18:17 -0800457
Pierre Ossman76591502008-07-21 00:32:11 +0200458 len = min(host->sg_miter.length, blksize);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800459
Pierre Ossman76591502008-07-21 00:32:11 +0200460 blksize -= len;
461 host->sg_miter.consumed = len;
Alex Dubov14d836e2007-04-13 19:04:38 +0200462
Pierre Ossman76591502008-07-21 00:32:11 +0200463 buf = host->sg_miter.addr;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800464
Pierre Ossman76591502008-07-21 00:32:11 +0200465 while (len) {
466 if (chunk == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300467 scratch = sdhci_readl(host, SDHCI_BUFFER);
Pierre Ossman76591502008-07-21 00:32:11 +0200468 chunk = 4;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800469 }
Pierre Ossman76591502008-07-21 00:32:11 +0200470
471 *buf = scratch & 0xFF;
472
473 buf++;
474 scratch >>= 8;
475 chunk--;
476 len--;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800477 }
478 }
Pierre Ossman76591502008-07-21 00:32:11 +0200479
480 sg_miter_stop(&host->sg_miter);
481
482 local_irq_restore(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100483}
Pierre Ossmand129bce2006-03-24 03:18:17 -0800484
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100485static void sdhci_write_block_pio(struct sdhci_host *host)
486{
Pierre Ossman76591502008-07-21 00:32:11 +0200487 unsigned long flags;
488 size_t blksize, len, chunk;
489 u32 scratch;
490 u8 *buf;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100491
492 DBG("PIO writing\n");
493
494 blksize = host->data->blksz;
Pierre Ossman76591502008-07-21 00:32:11 +0200495 chunk = 0;
496 scratch = 0;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100497
Pierre Ossman76591502008-07-21 00:32:11 +0200498 local_irq_save(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100499
500 while (blksize) {
Fabio Estevambf3a35a2015-05-09 18:44:51 -0300501 BUG_ON(!sg_miter_next(&host->sg_miter));
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100502
Pierre Ossman76591502008-07-21 00:32:11 +0200503 len = min(host->sg_miter.length, blksize);
Alex Dubov14d836e2007-04-13 19:04:38 +0200504
Pierre Ossman76591502008-07-21 00:32:11 +0200505 blksize -= len;
506 host->sg_miter.consumed = len;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100507
Pierre Ossman76591502008-07-21 00:32:11 +0200508 buf = host->sg_miter.addr;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100509
Pierre Ossman76591502008-07-21 00:32:11 +0200510 while (len) {
511 scratch |= (u32)*buf << (chunk * 8);
512
513 buf++;
514 chunk++;
515 len--;
516
517 if ((chunk == 4) || ((len == 0) && (blksize == 0))) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300518 sdhci_writel(host, scratch, SDHCI_BUFFER);
Pierre Ossman76591502008-07-21 00:32:11 +0200519 chunk = 0;
520 scratch = 0;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100521 }
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100522 }
523 }
Pierre Ossman76591502008-07-21 00:32:11 +0200524
525 sg_miter_stop(&host->sg_miter);
526
527 local_irq_restore(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100528}
529
530static void sdhci_transfer_pio(struct sdhci_host *host)
531{
532 u32 mask;
533
Pierre Ossman76591502008-07-21 00:32:11 +0200534 if (host->blocks == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100535 return;
536
537 if (host->data->flags & MMC_DATA_READ)
538 mask = SDHCI_DATA_AVAILABLE;
539 else
540 mask = SDHCI_SPACE_AVAILABLE;
541
Pierre Ossman4a3cba32008-07-29 00:11:16 +0200542 /*
543 * Some controllers (JMicron JMB38x) mess up the buffer bits
544 * for transfers < 4 bytes. As long as it is just one block,
545 * we can ignore the bits.
546 */
547 if ((host->quirks & SDHCI_QUIRK_BROKEN_SMALL_PIO) &&
548 (host->data->blocks == 1))
549 mask = ~0;
550
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300551 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Anton Vorontsov3e3bf202009-03-17 00:14:00 +0300552 if (host->quirks & SDHCI_QUIRK_PIO_NEEDS_DELAY)
553 udelay(100);
554
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100555 if (host->data->flags & MMC_DATA_READ)
556 sdhci_read_block_pio(host);
557 else
558 sdhci_write_block_pio(host);
559
Pierre Ossman76591502008-07-21 00:32:11 +0200560 host->blocks--;
561 if (host->blocks == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100562 break;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100563 }
564
565 DBG("PIO transfer complete.\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800566}
567
Russell King48857d92016-01-26 13:40:16 +0000568static int sdhci_pre_dma_transfer(struct sdhci_host *host,
Russell Kingc0999b72016-01-26 13:40:27 +0000569 struct mmc_data *data, int cookie)
Russell King48857d92016-01-26 13:40:16 +0000570{
571 int sg_count;
572
Russell King94538e52016-01-26 13:40:37 +0000573 /*
574 * If the data buffers are already mapped, return the previous
575 * dma_map_sg() result.
576 */
577 if (data->host_cookie == COOKIE_PRE_MAPPED)
Russell King48857d92016-01-26 13:40:16 +0000578 return data->sg_count;
Russell King48857d92016-01-26 13:40:16 +0000579
580 sg_count = dma_map_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
581 data->flags & MMC_DATA_WRITE ?
582 DMA_TO_DEVICE : DMA_FROM_DEVICE);
583
584 if (sg_count == 0)
585 return -ENOSPC;
586
587 data->sg_count = sg_count;
Russell Kingc0999b72016-01-26 13:40:27 +0000588 data->host_cookie = cookie;
Russell King48857d92016-01-26 13:40:16 +0000589
590 return sg_count;
591}
592
Pierre Ossman2134a922008-06-28 18:28:51 +0200593static char *sdhci_kmap_atomic(struct scatterlist *sg, unsigned long *flags)
594{
595 local_irq_save(*flags);
Cong Wang482fce92011-11-27 13:27:00 +0800596 return kmap_atomic(sg_page(sg)) + sg->offset;
Pierre Ossman2134a922008-06-28 18:28:51 +0200597}
598
599static void sdhci_kunmap_atomic(void *buffer, unsigned long *flags)
600{
Cong Wang482fce92011-11-27 13:27:00 +0800601 kunmap_atomic(buffer);
Pierre Ossman2134a922008-06-28 18:28:51 +0200602 local_irq_restore(*flags);
603}
604
Adrian Huntere57a5f62014-11-04 12:42:46 +0200605static void sdhci_adma_write_desc(struct sdhci_host *host, void *desc,
606 dma_addr_t addr, int len, unsigned cmd)
Ben Dooks118cd172010-03-05 13:43:26 -0800607{
Adrian Huntere57a5f62014-11-04 12:42:46 +0200608 struct sdhci_adma2_64_desc *dma_desc = desc;
Ben Dooks118cd172010-03-05 13:43:26 -0800609
Adrian Huntere57a5f62014-11-04 12:42:46 +0200610 /* 32-bit and 64-bit descriptors have these members in same position */
Adrian Hunter05452302014-11-04 12:42:45 +0200611 dma_desc->cmd = cpu_to_le16(cmd);
612 dma_desc->len = cpu_to_le16(len);
Adrian Huntere57a5f62014-11-04 12:42:46 +0200613 dma_desc->addr_lo = cpu_to_le32((u32)addr);
614
615 if (host->flags & SDHCI_USE_64_BIT_DMA)
616 dma_desc->addr_hi = cpu_to_le32((u64)addr >> 32);
Ben Dooks118cd172010-03-05 13:43:26 -0800617}
618
Adrian Hunterb5ffa672014-11-04 12:42:40 +0200619static void sdhci_adma_mark_end(void *desc)
620{
Adrian Huntere57a5f62014-11-04 12:42:46 +0200621 struct sdhci_adma2_64_desc *dma_desc = desc;
Adrian Hunterb5ffa672014-11-04 12:42:40 +0200622
Adrian Huntere57a5f62014-11-04 12:42:46 +0200623 /* 32-bit and 64-bit descriptors have 'cmd' in same position */
Adrian Hunter05452302014-11-04 12:42:45 +0200624 dma_desc->cmd |= cpu_to_le16(ADMA2_END);
Adrian Hunterb5ffa672014-11-04 12:42:40 +0200625}
626
Russell King60c64762016-01-26 13:40:22 +0000627static void sdhci_adma_table_pre(struct sdhci_host *host,
628 struct mmc_data *data, int sg_count)
Pierre Ossman2134a922008-06-28 18:28:51 +0200629{
Pierre Ossman2134a922008-06-28 18:28:51 +0200630 struct scatterlist *sg;
Pierre Ossman2134a922008-06-28 18:28:51 +0200631 unsigned long flags;
Russell Kingacc3ad12016-01-26 13:40:00 +0000632 dma_addr_t addr, align_addr;
633 void *desc, *align;
634 char *buffer;
635 int len, offset, i;
Pierre Ossman2134a922008-06-28 18:28:51 +0200636
637 /*
638 * The spec does not specify endianness of descriptor table.
639 * We currently guess that it is LE.
640 */
641
Russell King60c64762016-01-26 13:40:22 +0000642 host->sg_count = sg_count;
Pierre Ossman2134a922008-06-28 18:28:51 +0200643
Adrian Hunter4efaa6f2014-11-04 12:42:39 +0200644 desc = host->adma_table;
Pierre Ossman2134a922008-06-28 18:28:51 +0200645 align = host->align_buffer;
646
647 align_addr = host->align_addr;
648
649 for_each_sg(data->sg, sg, host->sg_count, i) {
650 addr = sg_dma_address(sg);
651 len = sg_dma_len(sg);
652
653 /*
Russell Kingacc3ad12016-01-26 13:40:00 +0000654 * The SDHCI specification states that ADMA addresses must
655 * be 32-bit aligned. If they aren't, then we use a bounce
656 * buffer for the (up to three) bytes that screw up the
Pierre Ossman2134a922008-06-28 18:28:51 +0200657 * alignment.
658 */
Adrian Hunter04a5ae62015-11-26 14:00:49 +0200659 offset = (SDHCI_ADMA2_ALIGN - (addr & SDHCI_ADMA2_MASK)) &
660 SDHCI_ADMA2_MASK;
Pierre Ossman2134a922008-06-28 18:28:51 +0200661 if (offset) {
662 if (data->flags & MMC_DATA_WRITE) {
663 buffer = sdhci_kmap_atomic(sg, &flags);
664 memcpy(align, buffer, offset);
665 sdhci_kunmap_atomic(buffer, &flags);
666 }
667
Ben Dooks118cd172010-03-05 13:43:26 -0800668 /* tran, valid */
Adrian Huntere57a5f62014-11-04 12:42:46 +0200669 sdhci_adma_write_desc(host, desc, align_addr, offset,
Adrian Hunter739d46d2014-11-04 12:42:44 +0200670 ADMA2_TRAN_VALID);
Pierre Ossman2134a922008-06-28 18:28:51 +0200671
672 BUG_ON(offset > 65536);
673
Adrian Hunter04a5ae62015-11-26 14:00:49 +0200674 align += SDHCI_ADMA2_ALIGN;
675 align_addr += SDHCI_ADMA2_ALIGN;
Pierre Ossman2134a922008-06-28 18:28:51 +0200676
Adrian Hunter76fe3792014-11-04 12:42:42 +0200677 desc += host->desc_sz;
Pierre Ossman2134a922008-06-28 18:28:51 +0200678
679 addr += offset;
680 len -= offset;
681 }
682
Pierre Ossman2134a922008-06-28 18:28:51 +0200683 BUG_ON(len > 65536);
684
Adrian Hunter347ea322015-11-26 14:00:48 +0200685 if (len) {
686 /* tran, valid */
687 sdhci_adma_write_desc(host, desc, addr, len,
688 ADMA2_TRAN_VALID);
689 desc += host->desc_sz;
690 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200691
692 /*
693 * If this triggers then we have a calculation bug
694 * somewhere. :/
695 */
Adrian Hunter76fe3792014-11-04 12:42:42 +0200696 WARN_ON((desc - host->adma_table) >= host->adma_table_sz);
Pierre Ossman2134a922008-06-28 18:28:51 +0200697 }
698
Thomas Abraham70764a92010-05-26 14:42:04 -0700699 if (host->quirks & SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC) {
Russell Kingacc3ad12016-01-26 13:40:00 +0000700 /* Mark the last descriptor as the terminating descriptor */
Adrian Hunter4efaa6f2014-11-04 12:42:39 +0200701 if (desc != host->adma_table) {
Adrian Hunter76fe3792014-11-04 12:42:42 +0200702 desc -= host->desc_sz;
Adrian Hunterb5ffa672014-11-04 12:42:40 +0200703 sdhci_adma_mark_end(desc);
Thomas Abraham70764a92010-05-26 14:42:04 -0700704 }
705 } else {
Russell Kingacc3ad12016-01-26 13:40:00 +0000706 /* Add a terminating entry - nop, end, valid */
Adrian Huntere57a5f62014-11-04 12:42:46 +0200707 sdhci_adma_write_desc(host, desc, 0, 0, ADMA2_NOP_END_VALID);
Thomas Abraham70764a92010-05-26 14:42:04 -0700708 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200709}
710
711static void sdhci_adma_table_post(struct sdhci_host *host,
712 struct mmc_data *data)
713{
Pierre Ossman2134a922008-06-28 18:28:51 +0200714 struct scatterlist *sg;
715 int i, size;
Adrian Hunter1c3d5f62014-11-04 12:42:41 +0200716 void *align;
Pierre Ossman2134a922008-06-28 18:28:51 +0200717 char *buffer;
718 unsigned long flags;
Venkat Gopalakrishnana71fbae2014-06-09 14:00:31 -0700719 u32 command = SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND));
720
721 trace_mmc_adma_table_post(command, data->sg_len);
Pierre Ossman2134a922008-06-28 18:28:51 +0200722
Russell King47fa9612016-01-26 13:40:06 +0000723 if (data->flags & MMC_DATA_READ) {
724 bool has_unaligned = false;
Russell Kingde0b65a2014-04-25 12:58:29 +0100725
Russell King47fa9612016-01-26 13:40:06 +0000726 /* Do a quick scan of the SG list for any unaligned mappings */
727 for_each_sg(data->sg, sg, host->sg_count, i)
Adrian Hunter04a5ae62015-11-26 14:00:49 +0200728 if (sg_dma_address(sg) & SDHCI_ADMA2_MASK) {
Russell King47fa9612016-01-26 13:40:06 +0000729 has_unaligned = true;
730 break;
731 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200732
Russell King47fa9612016-01-26 13:40:06 +0000733 if (has_unaligned) {
734 dma_sync_sg_for_cpu(mmc_dev(host->mmc), data->sg,
Russell Kingf55c98f2016-01-26 13:40:11 +0000735 data->sg_len, DMA_FROM_DEVICE);
Pierre Ossman2134a922008-06-28 18:28:51 +0200736
Russell King47fa9612016-01-26 13:40:06 +0000737 align = host->align_buffer;
738
739 for_each_sg(data->sg, sg, host->sg_count, i) {
740 if (sg_dma_address(sg) & SDHCI_ADMA2_MASK) {
741 size = SDHCI_ADMA2_ALIGN -
742 (sg_dma_address(sg) & SDHCI_ADMA2_MASK);
743
744 buffer = sdhci_kmap_atomic(sg, &flags);
745 memcpy(buffer, align, size);
746 sdhci_kunmap_atomic(buffer, &flags);
747
748 align += SDHCI_ADMA2_ALIGN;
749 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200750 }
751 }
752 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200753}
754
Andrei Warkentina3c77782011-04-11 16:13:42 -0500755static u8 sdhci_calc_timeout(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800756{
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700757 u8 count;
Andrei Warkentina3c77782011-04-11 16:13:42 -0500758 struct mmc_data *data = cmd->data;
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700759 unsigned target_timeout, current_timeout;
Sahitya Tummalaa5733ab52013-06-10 16:32:51 +0530760 u32 curr_clk = 0; /* In KHz */
Pierre Ossmand129bce2006-03-24 03:18:17 -0800761
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200762 /*
763 * If the host controller provides us with an incorrect timeout
764 * value, just skip the check and use 0xE. The hardware may take
765 * longer to time out, but that's much better than having a too-short
766 * timeout value.
767 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +0200768 if (host->quirks & SDHCI_QUIRK_BROKEN_TIMEOUT_VAL)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200769 return 0xE;
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200770
Andrei Warkentina3c77782011-04-11 16:13:42 -0500771 /* Unspecified timeout, assume max */
Ulf Hansson1d4d7742014-01-08 15:06:08 +0100772 if (!data && !cmd->busy_timeout)
Andrei Warkentina3c77782011-04-11 16:13:42 -0500773 return 0xE;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800774
Andrei Warkentina3c77782011-04-11 16:13:42 -0500775 /* timeout in us */
776 if (!data)
Ulf Hansson1d4d7742014-01-08 15:06:08 +0100777 target_timeout = cmd->busy_timeout * 1000;
Andy Shevchenko78a2ca22011-08-03 18:35:59 +0300778 else {
Russell Kingfafcfda2016-01-26 13:40:58 +0000779 target_timeout = DIV_ROUND_UP(data->timeout_ns, 1000);
Russell King7f055382016-01-26 13:41:04 +0000780 if (host->clock && data->timeout_clks) {
781 unsigned long long val;
782
783 /*
784 * data->timeout_clks is in units of clock cycles.
785 * host->clock is in Hz. target_timeout is in us.
786 * Hence, us = 1000000 * cycles / Hz. Round up.
787 */
Haibo Chen02265cd62016-10-17 10:18:37 +0200788 val = 1000000ULL * data->timeout_clks;
Russell King7f055382016-01-26 13:41:04 +0000789 if (do_div(val, host->clock))
790 target_timeout++;
791 target_timeout += val;
792 }
Andy Shevchenko78a2ca22011-08-03 18:35:59 +0300793 }
Anton Vorontsov81b39802009-09-22 16:45:13 -0700794
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700795 /*
796 * Figure out needed cycles.
797 * We do this in steps in order to fit inside a 32 bit int.
798 * The first step is the minimum timeout, which will have a
799 * minimum resolution of 6 bits:
800 * (1) 2^13*1000 > 2^22,
801 * (2) host->timeout_clk < 2^16
802 * =>
803 * (1) / (2) > 2^6
804 */
805 count = 0;
Sahitya Tummalaa5733ab52013-06-10 16:32:51 +0530806 if (host->quirks2 & SDHCI_QUIRK2_ALWAYS_USE_BASE_CLOCK) {
807 curr_clk = host->clock / 1000;
808 if (host->quirks2 & SDHCI_QUIRK2_DIVIDE_TOUT_BY_4)
809 curr_clk /= 4;
810 current_timeout = (1 << 13) * 1000 / curr_clk;
811 } else {
812 current_timeout = (1 << 13) * 1000 / host->timeout_clk;
813 }
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700814 while (current_timeout < target_timeout) {
815 count++;
816 current_timeout <<= 1;
817 if (count >= 0xF)
818 break;
819 }
820
Sahitya Tummala7c9780d2013-04-12 11:59:25 +0530821 if (!(host->quirks2 & SDHCI_QUIRK2_USE_RESERVED_MAX_TIMEOUT)) {
822 if (count >= 0xF) {
823 DBG("%s: Too large timeout 0x%x requested for CMD%d!\n",
824 mmc_hostname(host->mmc), count, cmd->opcode);
825 count = 0xE;
826 }
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700827 }
828
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200829 return count;
830}
831
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300832static void sdhci_set_transfer_irqs(struct sdhci_host *host)
833{
834 u32 pio_irqs = SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL;
835 u32 dma_irqs = SDHCI_INT_DMA_END | SDHCI_INT_ADMA_ERROR;
836
837 if (host->flags & SDHCI_REQ_USE_DMA)
Russell Kingb537f942014-04-25 12:56:01 +0100838 host->ier = (host->ier & ~pio_irqs) | dma_irqs;
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300839 else
Russell Kingb537f942014-04-25 12:56:01 +0100840 host->ier = (host->ier & ~dma_irqs) | pio_irqs;
841
842 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
843 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300844}
845
Aisheng Dongb45e6682014-08-27 15:26:29 +0800846static void sdhci_set_timeout(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200847{
848 u8 count;
Aisheng Dongb45e6682014-08-27 15:26:29 +0800849
850 if (host->ops->set_timeout) {
851 host->ops->set_timeout(host, cmd);
852 } else {
853 count = sdhci_calc_timeout(host, cmd);
854 sdhci_writeb(host, count, SDHCI_TIMEOUT_CONTROL);
855 }
856}
857
Subhash Jadavani3ed97712014-09-02 17:55:54 -0700858static void sdhci_set_blk_size_reg(struct sdhci_host *host, unsigned int blksz,
859 unsigned int sdma_boundary)
860{
861 if (host->flags & SDHCI_USE_ADMA)
862 sdhci_writew(host, SDHCI_MAKE_BLKSZ(0, blksz),
863 SDHCI_BLOCK_SIZE);
864 else
865 sdhci_writew(host, SDHCI_MAKE_BLKSZ(sdma_boundary, blksz),
866 SDHCI_BLOCK_SIZE);
867}
868
Aisheng Dongb45e6682014-08-27 15:26:29 +0800869static void sdhci_prepare_data(struct sdhci_host *host, struct mmc_command *cmd)
870{
Pierre Ossman2134a922008-06-28 18:28:51 +0200871 u8 ctrl;
Andrei Warkentina3c77782011-04-11 16:13:42 -0500872 struct mmc_data *data = cmd->data;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200873
Adrian Hunter56a590d2016-06-29 16:24:32 +0300874 if (sdhci_data_line_cmd(cmd))
Aisheng Dongb45e6682014-08-27 15:26:29 +0800875 sdhci_set_timeout(host, cmd);
Andrei Warkentina3c77782011-04-11 16:13:42 -0500876
877 if (!data)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200878 return;
879
Adrian Hunter43dea092016-06-29 16:24:26 +0300880 WARN_ON(host->data);
881
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200882 /* Sanity checks */
Asutosh Dasaafcad42013-01-10 21:05:49 +0530883 BUG_ON(data->blksz * data->blocks > host->mmc->max_req_size);
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200884 BUG_ON(data->blksz > host->mmc->max_blk_size);
885 BUG_ON(data->blocks > 65535);
886
887 host->data = data;
888 host->data_early = 0;
Mikko Vinnif6a03cb2011-04-12 09:36:18 -0400889 host->data->bytes_xfered = 0;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200890
Russell Kingfce14422016-01-26 13:41:20 +0000891 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossman2134a922008-06-28 18:28:51 +0200892 struct scatterlist *sg;
Russell Kingdf953922016-01-26 13:41:14 +0000893 unsigned int length_mask, offset_mask;
Russell Kinga0eaf0f2016-01-26 13:41:09 +0000894 int i;
Pierre Ossman2134a922008-06-28 18:28:51 +0200895
Russell Kingfce14422016-01-26 13:41:20 +0000896 host->flags |= SDHCI_REQ_USE_DMA;
897
Sahitya Tummala01b9a922016-04-05 15:27:36 +0530898 if ((host->quirks2 & SDHCI_QUIRK2_USE_PIO_FOR_EMMC_TUNING) &&
899 cmd->opcode == MMC_SEND_TUNING_BLOCK_HS200)
900 host->flags &= ~SDHCI_REQ_USE_DMA;
901
Russell Kingfce14422016-01-26 13:41:20 +0000902 /*
903 * FIXME: This doesn't account for merging when mapping the
904 * scatterlist.
905 *
906 * The assumption here being that alignment and lengths are
907 * the same after DMA mapping to device address space.
908 */
Russell Kinga0eaf0f2016-01-26 13:41:09 +0000909 length_mask = 0;
Russell Kingdf953922016-01-26 13:41:14 +0000910 offset_mask = 0;
Pierre Ossman2134a922008-06-28 18:28:51 +0200911 if (host->flags & SDHCI_USE_ADMA) {
Russell Kingdf953922016-01-26 13:41:14 +0000912 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE) {
Russell Kinga0eaf0f2016-01-26 13:41:09 +0000913 length_mask = 3;
Russell Kingdf953922016-01-26 13:41:14 +0000914 /*
915 * As we use up to 3 byte chunks to work
916 * around alignment problems, we need to
917 * check the offset as well.
918 */
919 offset_mask = 3;
920 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200921 } else {
922 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE)
Russell Kinga0eaf0f2016-01-26 13:41:09 +0000923 length_mask = 3;
Russell Kingdf953922016-01-26 13:41:14 +0000924 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR)
925 offset_mask = 3;
Pierre Ossman2134a922008-06-28 18:28:51 +0200926 }
927
Russell Kingdf953922016-01-26 13:41:14 +0000928 if (unlikely(length_mask | offset_mask)) {
Pierre Ossman2134a922008-06-28 18:28:51 +0200929 for_each_sg(data->sg, sg, data->sg_len, i) {
Russell Kinga0eaf0f2016-01-26 13:41:09 +0000930 if (sg->length & length_mask) {
Marek Vasut2e4456f2015-11-18 10:47:02 +0100931 DBG("Reverting to PIO because of transfer size (%d)\n",
Russell Kinga0eaf0f2016-01-26 13:41:09 +0000932 sg->length);
Pierre Ossman2134a922008-06-28 18:28:51 +0200933 host->flags &= ~SDHCI_REQ_USE_DMA;
934 break;
935 }
Russell Kinga0eaf0f2016-01-26 13:41:09 +0000936 if (sg->offset & offset_mask) {
Marek Vasut2e4456f2015-11-18 10:47:02 +0100937 DBG("Reverting to PIO because of bad alignment\n");
Pierre Ossman2134a922008-06-28 18:28:51 +0200938 host->flags &= ~SDHCI_REQ_USE_DMA;
939 break;
940 }
941 }
942 }
943 }
944
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200945 if (host->flags & SDHCI_REQ_USE_DMA) {
Russell Kingc0999b72016-01-26 13:40:27 +0000946 int sg_cnt = sdhci_pre_dma_transfer(host, data, COOKIE_MAPPED);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200947
Russell King60c64762016-01-26 13:40:22 +0000948 if (sg_cnt <= 0) {
949 /*
950 * This only happens when someone fed
951 * us an invalid request.
952 */
953 WARN_ON(1);
954 host->flags &= ~SDHCI_REQ_USE_DMA;
955 } else if (host->flags & SDHCI_USE_ADMA) {
Venkat Gopalakrishnana71fbae2014-06-09 14:00:31 -0700956 trace_mmc_adma_table_pre(cmd->opcode, data->sg_len);
Russell King60c64762016-01-26 13:40:22 +0000957 sdhci_adma_table_pre(host, data, sg_cnt);
958
959 sdhci_writel(host, host->adma_addr, SDHCI_ADMA_ADDRESS);
960 if (host->flags & SDHCI_USE_64_BIT_DMA)
961 sdhci_writel(host,
962 (u64)host->adma_addr >> 32,
963 SDHCI_ADMA_ADDRESS_HI);
964 } else {
965 WARN_ON(sg_cnt != 1);
966 sdhci_writel(host, sg_dma_address(data->sg),
967 SDHCI_DMA_ADDRESS);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200968 }
969 }
970
Pierre Ossman2134a922008-06-28 18:28:51 +0200971 /*
972 * Always adjust the DMA selection as some controllers
973 * (e.g. JMicron) can't do PIO properly when the selection
974 * is ADMA.
975 */
976 if (host->version >= SDHCI_SPEC_200) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300977 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossman2134a922008-06-28 18:28:51 +0200978 ctrl &= ~SDHCI_CTRL_DMA_MASK;
979 if ((host->flags & SDHCI_REQ_USE_DMA) &&
Adrian Huntere57a5f62014-11-04 12:42:46 +0200980 (host->flags & SDHCI_USE_ADMA)) {
981 if (host->flags & SDHCI_USE_64_BIT_DMA)
982 ctrl |= SDHCI_CTRL_ADMA64;
983 else
984 ctrl |= SDHCI_CTRL_ADMA32;
985 } else {
Pierre Ossman2134a922008-06-28 18:28:51 +0200986 ctrl |= SDHCI_CTRL_SDMA;
Adrian Huntere57a5f62014-11-04 12:42:46 +0200987 }
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300988 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100989 }
990
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200991 if (!(host->flags & SDHCI_REQ_USE_DMA)) {
Sebastian Andrzej Siewiorda60a912009-06-18 09:33:32 +0200992 int flags;
993
994 flags = SG_MITER_ATOMIC;
995 if (host->data->flags & MMC_DATA_READ)
996 flags |= SG_MITER_TO_SG;
997 else
998 flags |= SG_MITER_FROM_SG;
999 sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
Pierre Ossman76591502008-07-21 00:32:11 +02001000 host->blocks = data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001001 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -07001002
Anton Vorontsov6aa943a2009-03-17 00:13:50 +03001003 sdhci_set_transfer_irqs(host);
1004
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04001005 /* Set the DMA boundary value and block size */
Subhash Jadavani3ed97712014-09-02 17:55:54 -07001006 sdhci_set_blk_size_reg(host, data->blksz, SDHCI_DEFAULT_BOUNDARY_ARG);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001007 sdhci_writew(host, data->blocks, SDHCI_BLOCK_COUNT);
Sayali Lokhandebff771e2016-11-30 11:35:22 +05301008 MMC_TRACE(host->mmc,
1009 "%s: 0x28=0x%08x 0x3E=0x%08x 0x06=0x%08x\n", __func__,
1010 sdhci_readb(host, SDHCI_HOST_CONTROL),
1011 sdhci_readw(host, SDHCI_HOST_CONTROL2),
1012 sdhci_readw(host, SDHCI_BLOCK_COUNT));
Pierre Ossmanc7fa9962006-06-30 02:22:25 -07001013}
1014
Adrian Hunter0293d502016-06-29 16:24:35 +03001015static inline bool sdhci_auto_cmd12(struct sdhci_host *host,
1016 struct mmc_request *mrq)
1017{
Adrian Hunter20845be2016-08-16 13:44:13 +03001018 return !mrq->sbc && (host->flags & SDHCI_AUTO_CMD12) &&
1019 !mrq->cap_cmd_during_tfr;
Adrian Hunter0293d502016-06-29 16:24:35 +03001020}
1021
Pierre Ossmanc7fa9962006-06-30 02:22:25 -07001022static void sdhci_set_transfer_mode(struct sdhci_host *host,
Andrei Warkentine89d4562011-05-23 15:06:37 -05001023 struct mmc_command *cmd)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -07001024{
Vincent Yangd3fc5d72015-01-20 16:05:17 +08001025 u16 mode = 0;
Andrei Warkentine89d4562011-05-23 15:06:37 -05001026 struct mmc_data *data = cmd->data;
Pierre Ossmanc7fa9962006-06-30 02:22:25 -07001027
Dong Aisheng2b558c12013-10-30 22:09:48 +08001028 if (data == NULL) {
Vincent Wan9b8ffea2014-11-05 14:09:00 +08001029 if (host->quirks2 &
1030 SDHCI_QUIRK2_CLEAR_TRANSFERMODE_REG_BEFORE_CMD) {
1031 sdhci_writew(host, 0x0, SDHCI_TRANSFER_MODE);
1032 } else {
Dong Aisheng2b558c12013-10-30 22:09:48 +08001033 /* clear Auto CMD settings for no data CMDs */
Vincent Wan9b8ffea2014-11-05 14:09:00 +08001034 mode = sdhci_readw(host, SDHCI_TRANSFER_MODE);
1035 sdhci_writew(host, mode & ~(SDHCI_TRNS_AUTO_CMD12 |
Dong Aisheng2b558c12013-10-30 22:09:48 +08001036 SDHCI_TRNS_AUTO_CMD23), SDHCI_TRANSFER_MODE);
Vincent Wan9b8ffea2014-11-05 14:09:00 +08001037 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -07001038 return;
Dong Aisheng2b558c12013-10-30 22:09:48 +08001039 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -07001040
Pierre Ossmane538fbe2007-08-12 16:46:32 +02001041 WARN_ON(!host->data);
1042
Vincent Yangd3fc5d72015-01-20 16:05:17 +08001043 if (!(host->quirks2 & SDHCI_QUIRK2_SUPPORT_SINGLE))
1044 mode = SDHCI_TRNS_BLK_CNT_EN;
1045
Andrei Warkentine89d4562011-05-23 15:06:37 -05001046 if (mmc_op_multi(cmd->opcode) || data->blocks > 1) {
Vincent Yangd3fc5d72015-01-20 16:05:17 +08001047 mode = SDHCI_TRNS_BLK_CNT_EN | SDHCI_TRNS_MULTI;
Andrei Warkentine89d4562011-05-23 15:06:37 -05001048 /*
1049 * If we are sending CMD23, CMD12 never gets sent
1050 * on successful completion (so no Auto-CMD12).
1051 */
Adrian Hunter0293d502016-06-29 16:24:35 +03001052 if (sdhci_auto_cmd12(host, cmd->mrq) &&
Corneliu Doban85cc1c32015-02-09 16:06:29 -08001053 (cmd->opcode != SD_IO_RW_EXTENDED))
Andrei Warkentine89d4562011-05-23 15:06:37 -05001054 mode |= SDHCI_TRNS_AUTO_CMD12;
Adrian Huntera4c73ab2016-06-29 16:24:25 +03001055 else if (cmd->mrq->sbc && (host->flags & SDHCI_AUTO_CMD23)) {
Andrei Warkentin8edf63712011-05-23 15:06:39 -05001056 mode |= SDHCI_TRNS_AUTO_CMD23;
Adrian Huntera4c73ab2016-06-29 16:24:25 +03001057 sdhci_writel(host, cmd->mrq->sbc->arg, SDHCI_ARGUMENT2);
Andrei Warkentin8edf63712011-05-23 15:06:39 -05001058 }
Jerry Huangc4512f72010-08-10 18:01:59 -07001059 }
Andrei Warkentin8edf63712011-05-23 15:06:39 -05001060
Sahitya Tummala9dca7d72013-02-25 15:45:32 +05301061 if (data->flags & MMC_DATA_READ) {
Pierre Ossmanc7fa9962006-06-30 02:22:25 -07001062 mode |= SDHCI_TRNS_READ;
Ritesh Harjani210c2302014-11-14 11:06:40 +05301063 if (host->ops->toggle_cdr) {
1064 if ((cmd->opcode == MMC_SEND_TUNING_BLOCK_HS200) ||
1065 (cmd->opcode == MMC_SEND_TUNING_BLOCK_HS400) ||
1066 (cmd->opcode == MMC_SEND_TUNING_BLOCK))
1067 host->ops->toggle_cdr(host, false);
1068 else
1069 host->ops->toggle_cdr(host, true);
1070 }
Sahitya Tummala9dca7d72013-02-25 15:45:32 +05301071 }
1072 if (host->ops->toggle_cdr && (data->flags & MMC_DATA_WRITE))
1073 host->ops->toggle_cdr(host, false);
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +01001074 if (host->flags & SDHCI_REQ_USE_DMA)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -07001075 mode |= SDHCI_TRNS_DMA;
1076
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001077 sdhci_writew(host, mode, SDHCI_TRANSFER_MODE);
Sayali Lokhandebff771e2016-11-30 11:35:22 +05301078 MMC_TRACE(host->mmc, "%s: 0x00=0x%08x 0x0C=0x%08x\n", __func__,
1079 sdhci_readw(host, SDHCI_ARGUMENT2),
1080 sdhci_readw(host, SDHCI_TRANSFER_MODE));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001081}
1082
Adrian Hunter0cc563c2016-06-29 16:24:28 +03001083static bool sdhci_needs_reset(struct sdhci_host *host, struct mmc_request *mrq)
1084{
1085 return (!(host->flags & SDHCI_DEVICE_DEAD) &&
1086 ((mrq->cmd && mrq->cmd->error) ||
1087 (mrq->sbc && mrq->sbc->error) ||
1088 (mrq->data && ((mrq->data->error && !mrq->data->stop) ||
1089 (mrq->data->stop && mrq->data->stop->error))) ||
1090 (host->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST)));
1091}
1092
Adrian Hunter4e9f8fe2016-06-29 16:24:34 +03001093static void __sdhci_finish_mrq(struct sdhci_host *host, struct mmc_request *mrq)
1094{
1095 int i;
1096
1097 for (i = 0; i < SDHCI_MAX_MRQS; i++) {
1098 if (host->mrqs_done[i] == mrq) {
1099 WARN_ON(1);
1100 return;
1101 }
1102 }
1103
1104 for (i = 0; i < SDHCI_MAX_MRQS; i++) {
1105 if (!host->mrqs_done[i]) {
1106 host->mrqs_done[i] = mrq;
1107 break;
1108 }
1109 }
1110
1111 WARN_ON(i >= SDHCI_MAX_MRQS);
1112
1113 tasklet_schedule(&host->finish_tasklet);
1114}
1115
Adrian Huntera6d3bdd2016-06-29 16:24:27 +03001116static void sdhci_finish_mrq(struct sdhci_host *host, struct mmc_request *mrq)
1117{
Adrian Hunter5a8a3fe2016-06-29 16:24:30 +03001118 if (host->cmd && host->cmd->mrq == mrq)
1119 host->cmd = NULL;
1120
1121 if (host->data_cmd && host->data_cmd->mrq == mrq)
1122 host->data_cmd = NULL;
1123
1124 if (host->data && host->data->mrq == mrq)
1125 host->data = NULL;
1126
Adrian Huntered1563d2016-06-29 16:24:29 +03001127 if (sdhci_needs_reset(host, mrq))
1128 host->pending_reset = true;
1129
Adrian Hunter4e9f8fe2016-06-29 16:24:34 +03001130 __sdhci_finish_mrq(host, mrq);
Adrian Huntera6d3bdd2016-06-29 16:24:27 +03001131}
1132
Pierre Ossmand129bce2006-03-24 03:18:17 -08001133static void sdhci_finish_data(struct sdhci_host *host)
1134{
Adrian Hunter33a57ad2016-06-29 16:24:36 +03001135 struct mmc_command *data_cmd = host->data_cmd;
1136 struct mmc_data *data = host->data;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001137
Pierre Ossmand129bce2006-03-24 03:18:17 -08001138 host->data = NULL;
Adrian Hunter7c89a3d2016-06-29 16:24:23 +03001139 host->data_cmd = NULL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001140
Sayali Lokhandebff771e2016-11-30 11:35:22 +05301141 MMC_TRACE(host->mmc, "%s: 0x24=0x%08x\n", __func__,
1142 sdhci_readl(host, SDHCI_PRESENT_STATE));
Russell Kingadd89132016-01-26 13:40:42 +00001143 if ((host->flags & (SDHCI_REQ_USE_DMA | SDHCI_USE_ADMA)) ==
1144 (SDHCI_REQ_USE_DMA | SDHCI_USE_ADMA))
1145 sdhci_adma_table_post(host, data);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001146
1147 /*
Pierre Ossmanc9b74c52008-04-18 20:41:49 +02001148 * The specification states that the block count register must
1149 * be updated, but it does not specify at what point in the
1150 * data flow. That makes the register entirely useless to read
1151 * back so we have to assume that nothing made it to the card
1152 * in the event of an error.
Pierre Ossmand129bce2006-03-24 03:18:17 -08001153 */
Pierre Ossmanc9b74c52008-04-18 20:41:49 +02001154 if (data->error)
1155 data->bytes_xfered = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001156 else
Pierre Ossmanc9b74c52008-04-18 20:41:49 +02001157 data->bytes_xfered = data->blksz * data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001158
Andrei Warkentine89d4562011-05-23 15:06:37 -05001159 /*
1160 * Need to send CMD12 if -
1161 * a) open-ended multiblock transfer (no CMD23)
1162 * b) error in multiblock transfer
1163 */
1164 if (data->stop &&
1165 (data->error ||
Adrian Huntera4c73ab2016-06-29 16:24:25 +03001166 !data->mrq->sbc)) {
Andrei Warkentine89d4562011-05-23 15:06:37 -05001167
Pierre Ossmand129bce2006-03-24 03:18:17 -08001168 /*
1169 * The controller needs a reset of internal state machines
1170 * upon error conditions.
1171 */
Pierre Ossman17b04292007-07-22 22:18:46 +02001172 if (data->error) {
Adrian Hunter33a57ad2016-06-29 16:24:36 +03001173 if (!host->cmd || host->cmd == data_cmd)
1174 sdhci_do_reset(host, SDHCI_RESET_CMD);
Russell King03231f92014-04-25 12:57:12 +01001175 sdhci_do_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001176 }
1177
Adrian Hunter20845be2016-08-16 13:44:13 +03001178 /*
1179 * 'cap_cmd_during_tfr' request must not use the command line
1180 * after mmc_command_done() has been called. It is upper layer's
1181 * responsibility to send the stop command if required.
1182 */
1183 if (data->mrq->cap_cmd_during_tfr) {
1184 sdhci_finish_mrq(host, data->mrq);
1185 } else {
1186 /* Avoid triggering warning in sdhci_send_command() */
1187 host->cmd = NULL;
1188 sdhci_send_command(host, data->stop);
1189 }
Adrian Huntera6d3bdd2016-06-29 16:24:27 +03001190 } else {
1191 sdhci_finish_mrq(host, data->mrq);
1192 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001193}
1194
Adrian Hunterd7422fb2016-06-29 16:24:33 +03001195static void sdhci_mod_timer(struct sdhci_host *host, struct mmc_request *mrq,
1196 unsigned long timeout)
1197{
1198 if (sdhci_data_line_cmd(mrq->cmd))
1199 mod_timer(&host->data_timer, timeout);
1200 else
1201 mod_timer(&host->timer, timeout);
1202}
1203
1204static void sdhci_del_timer(struct sdhci_host *host, struct mmc_request *mrq)
1205{
1206 if (sdhci_data_line_cmd(mrq->cmd))
1207 del_timer(&host->data_timer);
1208 else
1209 del_timer(&host->timer);
1210}
1211
Dong Aishengc0e551292013-09-13 19:11:31 +08001212void sdhci_send_command(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001213{
1214 int flags;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -07001215 u32 mask;
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001216 unsigned long timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001217
1218 WARN_ON(host->cmd);
1219
Russell King96776202016-01-26 13:39:34 +00001220 /* Initially, a command has no error */
1221 cmd->error = 0;
1222
Adrian Hunterfc605f12016-10-05 12:11:21 +03001223 if ((host->quirks2 & SDHCI_QUIRK2_STOP_WITH_TC) &&
1224 cmd->opcode == MMC_STOP_TRANSMISSION)
1225 cmd->flags |= MMC_RSP_BUSY;
1226
Pierre Ossmand129bce2006-03-24 03:18:17 -08001227 /* Wait max 10 ms */
Venkat Gopalakrishnanc90fa962014-04-09 10:54:29 -07001228 timeout = 10000;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -07001229
1230 mask = SDHCI_CMD_INHIBIT;
Adrian Hunter56a590d2016-06-29 16:24:32 +03001231 if (sdhci_data_line_cmd(cmd))
Pierre Ossmanfd2208d2006-06-30 02:22:28 -07001232 mask |= SDHCI_DATA_INHIBIT;
1233
1234 /* We shouldn't wait for data inihibit for stop commands, even
1235 though they might use busy signaling */
Adrian Huntera4c73ab2016-06-29 16:24:25 +03001236 if (cmd->mrq->data && (cmd == cmd->mrq->data->stop))
Pierre Ossmanfd2208d2006-06-30 02:22:28 -07001237 mask &= ~SDHCI_DATA_INHIBIT;
1238
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001239 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001240 if (timeout == 0) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01001241 pr_err("%s: Controller never released inhibit bit(s).\n",
1242 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001243 sdhci_dumpregs(host);
Pierre Ossman17b04292007-07-22 22:18:46 +02001244 cmd->error = -EIO;
Adrian Huntera6d3bdd2016-06-29 16:24:27 +03001245 sdhci_finish_mrq(host, cmd->mrq);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001246 return;
1247 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001248 timeout--;
Venkat Gopalakrishnanc90fa962014-04-09 10:54:29 -07001249 udelay(1);
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001250 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001251
Adrian Hunter3e1a6892013-11-14 10:16:20 +02001252 timeout = jiffies;
Ulf Hansson1d4d7742014-01-08 15:06:08 +01001253 if (!cmd->data && cmd->busy_timeout > 9000)
1254 timeout += DIV_ROUND_UP(cmd->busy_timeout, 1000) * HZ + HZ;
Adrian Hunter3e1a6892013-11-14 10:16:20 +02001255 else
1256 timeout += 10 * HZ;
Adrian Hunterd7422fb2016-06-29 16:24:33 +03001257 sdhci_mod_timer(host, cmd->mrq, timeout);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001258
1259 host->cmd = cmd;
Adrian Hunter56a590d2016-06-29 16:24:32 +03001260 if (sdhci_data_line_cmd(cmd)) {
Adrian Hunter7c89a3d2016-06-29 16:24:23 +03001261 WARN_ON(host->data_cmd);
1262 host->data_cmd = cmd;
1263 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001264
Andrei Warkentina3c77782011-04-11 16:13:42 -05001265 sdhci_prepare_data(host, cmd);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001266
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001267 sdhci_writel(host, cmd->arg, SDHCI_ARGUMENT);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001268
Andrei Warkentine89d4562011-05-23 15:06:37 -05001269 sdhci_set_transfer_mode(host, cmd);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -07001270
Pierre Ossmand129bce2006-03-24 03:18:17 -08001271 if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301272 pr_err("%s: Unsupported response type!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -08001273 mmc_hostname(host->mmc));
Pierre Ossman17b04292007-07-22 22:18:46 +02001274 cmd->error = -EINVAL;
Adrian Huntera6d3bdd2016-06-29 16:24:27 +03001275 sdhci_finish_mrq(host, cmd->mrq);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001276 return;
1277 }
1278
1279 if (!(cmd->flags & MMC_RSP_PRESENT))
1280 flags = SDHCI_CMD_RESP_NONE;
1281 else if (cmd->flags & MMC_RSP_136)
1282 flags = SDHCI_CMD_RESP_LONG;
1283 else if (cmd->flags & MMC_RSP_BUSY)
1284 flags = SDHCI_CMD_RESP_SHORT_BUSY;
1285 else
1286 flags = SDHCI_CMD_RESP_SHORT;
1287
1288 if (cmd->flags & MMC_RSP_CRC)
1289 flags |= SDHCI_CMD_CRC;
1290 if (cmd->flags & MMC_RSP_OPCODE)
1291 flags |= SDHCI_CMD_INDEX;
Arindam Nathb513ea22011-05-05 12:19:04 +05301292
1293 /* CMD19 is special in that the Data Present Select should be set */
Girish K S069c9f12012-01-06 09:56:39 +05301294 if (cmd->data || cmd->opcode == MMC_SEND_TUNING_BLOCK ||
1295 cmd->opcode == MMC_SEND_TUNING_BLOCK_HS200)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001296 flags |= SDHCI_CMD_DATA;
1297
Sahitya Tummala16dabee2013-04-08 12:53:44 +05301298 if (cmd->data)
1299 host->data_start_time = ktime_get();
Venkat Gopalakrishnana71fbae2014-06-09 14:00:31 -07001300 trace_mmc_cmd_rw_start(cmd->opcode, cmd->arg, cmd->flags);
Sayali Lokhandebff771e2016-11-30 11:35:22 +05301301 MMC_TRACE(host->mmc,
1302 "%s: updated 0x8=0x%08x 0xC=0x%08x 0xE=0x%08x\n", __func__,
1303 sdhci_readl(host, SDHCI_ARGUMENT),
1304 sdhci_readw(host, SDHCI_TRANSFER_MODE),
1305 sdhci_readw(host, SDHCI_COMMAND));
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001306 sdhci_writew(host, SDHCI_MAKE_CMD(cmd->opcode, flags), SDHCI_COMMAND);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001307}
Dong Aishengc0e551292013-09-13 19:11:31 +08001308EXPORT_SYMBOL_GPL(sdhci_send_command);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001309
1310static void sdhci_finish_command(struct sdhci_host *host)
1311{
Adrian Huntere0a56402016-06-29 16:24:22 +03001312 struct mmc_command *cmd = host->cmd;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001313 int i;
1314
Adrian Huntere0a56402016-06-29 16:24:22 +03001315 host->cmd = NULL;
1316
1317 if (cmd->flags & MMC_RSP_PRESENT) {
1318 if (cmd->flags & MMC_RSP_136) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08001319 /* CRC is stripped so we need to do some shifting. */
1320 for (i = 0;i < 4;i++) {
Adrian Huntere0a56402016-06-29 16:24:22 +03001321 cmd->resp[i] = sdhci_readl(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001322 SDHCI_RESPONSE + (3-i)*4) << 8;
1323 if (i != 3)
Adrian Huntere0a56402016-06-29 16:24:22 +03001324 cmd->resp[i] |=
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001325 sdhci_readb(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001326 SDHCI_RESPONSE + (3-i)*4-1);
1327 }
Sayali Lokhandebff771e2016-11-30 11:35:22 +05301328 MMC_TRACE(host->mmc,
1329 "%s: resp 0: 0x%08x resp 1: 0x%08x resp 2: 0x%08x resp 3: 0x%08x\n",
1330 __func__, cmd->resp[0], cmd->resp[1],
1331 cmd->resp[2], cmd->resp[3]);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001332 } else {
Adrian Huntere0a56402016-06-29 16:24:22 +03001333 cmd->resp[0] = sdhci_readl(host, SDHCI_RESPONSE);
Sayali Lokhandebff771e2016-11-30 11:35:22 +05301334 MMC_TRACE(host->mmc, "%s: resp 0: 0x%08x\n",
1335 __func__, cmd->resp[0]);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001336 }
1337 }
1338
Adrian Hunter20845be2016-08-16 13:44:13 +03001339 if (cmd->mrq->cap_cmd_during_tfr && cmd == cmd->mrq->cmd)
1340 mmc_command_done(host->mmc, cmd->mrq);
1341
Adrian Hunter6bde8682016-06-29 16:24:20 +03001342 /*
1343 * The host can send and interrupt when the busy state has
1344 * ended, allowing us to wait without wasting CPU cycles.
1345 * The busy signal uses DAT0 so this is similar to waiting
1346 * for data to complete.
1347 *
1348 * Note: The 1.0 specification is a bit ambiguous about this
1349 * feature so there might be some problems with older
1350 * controllers.
1351 */
Adrian Huntere0a56402016-06-29 16:24:22 +03001352 if (cmd->flags & MMC_RSP_BUSY) {
1353 if (cmd->data) {
Adrian Hunter6bde8682016-06-29 16:24:20 +03001354 DBG("Cannot wait for busy signal when also doing a data transfer");
1355 } else if (!(host->quirks & SDHCI_QUIRK_NO_BUSY_IRQ) &&
Adrian Hunterea968022016-06-29 16:24:24 +03001356 cmd == host->data_cmd) {
1357 /* Command complete before busy is ended */
Adrian Hunter6bde8682016-06-29 16:24:20 +03001358 return;
1359 }
1360 }
1361
Andrei Warkentine89d4562011-05-23 15:06:37 -05001362 /* Finished CMD23, now send actual command. */
Adrian Huntera4c73ab2016-06-29 16:24:25 +03001363 if (cmd == cmd->mrq->sbc) {
1364 sdhci_send_command(host, cmd->mrq->cmd);
Andrei Warkentine89d4562011-05-23 15:06:37 -05001365 } else {
Pierre Ossmane538fbe2007-08-12 16:46:32 +02001366
Andrei Warkentine89d4562011-05-23 15:06:37 -05001367 /* Processed actual command. */
1368 if (host->data && host->data_early)
1369 sdhci_finish_data(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001370
Adrian Huntere0a56402016-06-29 16:24:22 +03001371 if (!cmd->data)
Adrian Huntera6d3bdd2016-06-29 16:24:27 +03001372 sdhci_finish_mrq(host, cmd->mrq);
Andrei Warkentine89d4562011-05-23 15:06:37 -05001373 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001374}
1375
Kevin Liu52983382013-01-31 11:31:37 +08001376static u16 sdhci_get_preset_value(struct sdhci_host *host)
1377{
Russell Kingd975f122014-04-25 12:59:31 +01001378 u16 preset = 0;
Kevin Liu52983382013-01-31 11:31:37 +08001379
Russell Kingd975f122014-04-25 12:59:31 +01001380 switch (host->timing) {
1381 case MMC_TIMING_UHS_SDR12:
Kevin Liu52983382013-01-31 11:31:37 +08001382 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR12);
1383 break;
Russell Kingd975f122014-04-25 12:59:31 +01001384 case MMC_TIMING_UHS_SDR25:
Kevin Liu52983382013-01-31 11:31:37 +08001385 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR25);
1386 break;
Russell Kingd975f122014-04-25 12:59:31 +01001387 case MMC_TIMING_UHS_SDR50:
Kevin Liu52983382013-01-31 11:31:37 +08001388 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR50);
1389 break;
Russell Kingd975f122014-04-25 12:59:31 +01001390 case MMC_TIMING_UHS_SDR104:
1391 case MMC_TIMING_MMC_HS200:
Kevin Liu52983382013-01-31 11:31:37 +08001392 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR104);
1393 break;
Russell Kingd975f122014-04-25 12:59:31 +01001394 case MMC_TIMING_UHS_DDR50:
Jisheng Zhang0dafa602015-08-18 16:21:39 +08001395 case MMC_TIMING_MMC_DDR52:
Kevin Liu52983382013-01-31 11:31:37 +08001396 preset = sdhci_readw(host, SDHCI_PRESET_FOR_DDR50);
1397 break;
Adrian Huntere9fb05d2014-11-06 15:19:06 +02001398 case MMC_TIMING_MMC_HS400:
1399 preset = sdhci_readw(host, SDHCI_PRESET_FOR_HS400);
1400 break;
Kevin Liu52983382013-01-31 11:31:37 +08001401 default:
1402 pr_warn("%s: Invalid UHS-I mode selected\n",
1403 mmc_hostname(host->mmc));
1404 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR12);
1405 break;
1406 }
1407 return preset;
1408}
1409
Ludovic Desrochesfb9ee042016-04-07 11:13:08 +02001410u16 sdhci_calc_clk(struct sdhci_host *host, unsigned int clock,
1411 unsigned int *actual_clock)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001412{
Arindam Nathc3ed3872011-05-05 12:19:06 +05301413 int div = 0; /* Initialized for compiler warning */
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001414 int real_div = div, clk_mul = 1;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301415 u16 clk = 0;
ludovic.desroches@atmel.com54971592015-07-29 16:22:46 +02001416 bool switch_base_clk = false;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001417
Zhangfei Gao85105c52010-08-06 07:10:01 +08001418 if (host->version >= SDHCI_SPEC_300) {
Russell Kingda91a8f2014-04-25 13:00:12 +01001419 if (host->preset_enabled) {
Kevin Liu52983382013-01-31 11:31:37 +08001420 u16 pre_val;
1421
1422 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1423 pre_val = sdhci_get_preset_value(host);
1424 div = (pre_val & SDHCI_PRESET_SDCLK_FREQ_MASK)
1425 >> SDHCI_PRESET_SDCLK_FREQ_SHIFT;
1426 if (host->clk_mul &&
1427 (pre_val & SDHCI_PRESET_CLKGEN_SEL_MASK)) {
1428 clk = SDHCI_PROG_CLOCK_MODE;
1429 real_div = div + 1;
1430 clk_mul = host->clk_mul;
1431 } else {
1432 real_div = max_t(int, 1, div << 1);
1433 }
1434 goto clock_set;
1435 }
1436
Arindam Nathc3ed3872011-05-05 12:19:06 +05301437 /*
1438 * Check if the Host Controller supports Programmable Clock
1439 * Mode.
1440 */
1441 if (host->clk_mul) {
Kevin Liu52983382013-01-31 11:31:37 +08001442 for (div = 1; div <= 1024; div++) {
1443 if ((host->max_clk * host->clk_mul / div)
1444 <= clock)
1445 break;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001446 }
ludovic.desroches@atmel.com54971592015-07-29 16:22:46 +02001447 if ((host->max_clk * host->clk_mul / div) <= clock) {
1448 /*
1449 * Set Programmable Clock Mode in the Clock
1450 * Control register.
1451 */
1452 clk = SDHCI_PROG_CLOCK_MODE;
1453 real_div = div;
1454 clk_mul = host->clk_mul;
1455 div--;
1456 } else {
1457 /*
1458 * Divisor can be too small to reach clock
1459 * speed requirement. Then use the base clock.
1460 */
1461 switch_base_clk = true;
1462 }
1463 }
1464
1465 if (!host->clk_mul || switch_base_clk) {
Arindam Nathc3ed3872011-05-05 12:19:06 +05301466 /* Version 3.00 divisors must be a multiple of 2. */
1467 if (host->max_clk <= clock)
1468 div = 1;
1469 else {
1470 for (div = 2; div < SDHCI_MAX_DIV_SPEC_300;
1471 div += 2) {
1472 if ((host->max_clk / div) <= clock)
1473 break;
1474 }
1475 }
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001476 real_div = div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301477 div >>= 1;
Suneel Garapatid1955c32015-06-09 13:01:50 +05301478 if ((host->quirks2 & SDHCI_QUIRK2_CLOCK_DIV_ZERO_BROKEN)
1479 && !div && host->max_clk <= 25000000)
1480 div = 1;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001481 }
1482 } else {
1483 /* Version 2.00 divisors must be a power of 2. */
Zhangfei Gao03975262010-09-20 15:15:18 -04001484 for (div = 1; div < SDHCI_MAX_DIV_SPEC_200; div *= 2) {
Zhangfei Gao85105c52010-08-06 07:10:01 +08001485 if ((host->max_clk / div) <= clock)
1486 break;
1487 }
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001488 real_div = div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301489 div >>= 1;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001490 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001491
Kevin Liu52983382013-01-31 11:31:37 +08001492clock_set:
Aisheng Dong03d6f5f2014-08-27 15:26:32 +08001493 if (real_div)
Ludovic Desrochesfb9ee042016-04-07 11:13:08 +02001494 *actual_clock = (host->max_clk * clk_mul) / real_div;
Sahitya Tummala22dd3362013-02-28 19:50:51 +05301495
1496 if (host->quirks2 & SDHCI_QUIRK2_ALWAYS_USE_BASE_CLOCK)
1497 div = 0;
1498
Arindam Nathc3ed3872011-05-05 12:19:06 +05301499 clk |= (div & SDHCI_DIV_MASK) << SDHCI_DIVIDER_SHIFT;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001500 clk |= ((div & SDHCI_DIV_HI_MASK) >> SDHCI_DIV_MASK_LEN)
1501 << SDHCI_DIVIDER_HI_SHIFT;
Ludovic Desrochesfb9ee042016-04-07 11:13:08 +02001502
1503 return clk;
1504}
1505EXPORT_SYMBOL_GPL(sdhci_calc_clk);
1506
1507void sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
1508{
1509 u16 clk;
1510 unsigned long timeout;
1511
1512 host->mmc->actual_clock = 0;
1513
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05301514 if (host->clock)
1515 sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
Ludovic Desrochesfb9ee042016-04-07 11:13:08 +02001516
1517 if (clock == 0)
1518 return;
1519
1520 clk = sdhci_calc_clk(host, clock, &host->mmc->actual_clock);
1521
Pierre Ossmand129bce2006-03-24 03:18:17 -08001522 clk |= SDHCI_CLOCK_INT_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001523 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001524
Chris Ball27f6cb12009-09-22 16:45:31 -07001525 /* Wait max 20 ms */
Subhash Jadavaniac6de6d2017-04-11 19:12:39 -07001526 timeout = 20;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001527 while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001528 & SDHCI_CLOCK_INT_STABLE)) {
1529 if (timeout == 0) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01001530 pr_err("%s: Internal clock never stabilised.\n",
1531 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001532 sdhci_dumpregs(host);
1533 return;
1534 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001535 timeout--;
Adrian Hunterb43ba212017-03-20 19:50:29 +02001536 usleep_range(900, 1100);
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001537 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001538
1539 clk |= SDHCI_CLOCK_CARD_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001540 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001541}
Russell King17710592014-04-25 12:58:55 +01001542EXPORT_SYMBOL_GPL(sdhci_set_clock);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001543
Adrian Hunter1dceb042016-03-29 12:45:43 +03001544static void sdhci_set_power_reg(struct sdhci_host *host, unsigned char mode,
1545 unsigned short vdd)
Pierre Ossman146ad662006-06-30 02:22:23 -07001546{
Tim Kryger3a48edc2014-06-13 10:13:56 -07001547 struct mmc_host *mmc = host->mmc;
Adrian Hunter1dceb042016-03-29 12:45:43 +03001548
1549 spin_unlock_irq(&host->lock);
1550 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, vdd);
1551 spin_lock_irq(&host->lock);
1552
1553 if (mode != MMC_POWER_OFF)
1554 sdhci_writeb(host, SDHCI_POWER_ON, SDHCI_POWER_CONTROL);
1555 else
1556 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
1557}
1558
Adrian Hunter606d3132016-10-05 12:11:22 +03001559void sdhci_set_power_noreg(struct sdhci_host *host, unsigned char mode,
1560 unsigned short vdd)
Adrian Hunter1dceb042016-03-29 12:45:43 +03001561{
Giuseppe Cavallaro83642482010-09-28 10:41:28 +02001562 u8 pwr = 0;
Pierre Ossman146ad662006-06-30 02:22:23 -07001563
Russell King24fbb3c2014-04-25 13:00:06 +01001564 if (mode != MMC_POWER_OFF) {
1565 switch (1 << vdd) {
Pierre Ossmanae628902009-05-03 20:45:03 +02001566 case MMC_VDD_165_195:
1567 pwr = SDHCI_POWER_180;
1568 break;
1569 case MMC_VDD_29_30:
1570 case MMC_VDD_30_31:
1571 pwr = SDHCI_POWER_300;
1572 break;
1573 case MMC_VDD_32_33:
1574 case MMC_VDD_33_34:
1575 pwr = SDHCI_POWER_330;
1576 break;
1577 default:
Adrian Hunter9d5de932015-11-26 14:00:46 +02001578 WARN(1, "%s: Invalid vdd %#x\n",
1579 mmc_hostname(host->mmc), vdd);
1580 break;
Pierre Ossmanae628902009-05-03 20:45:03 +02001581 }
1582 }
1583
1584 if (host->pwr == pwr)
Russell Kinge921a8b2014-04-25 13:00:01 +01001585 return;
Pierre Ossman146ad662006-06-30 02:22:23 -07001586
Pierre Ossmanae628902009-05-03 20:45:03 +02001587 host->pwr = pwr;
1588
1589 if (pwr == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001590 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Sahitya Tummala63d235d2013-02-21 10:09:49 +05301591 if (host->ops->check_power_status)
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05301592 host->ops->check_power_status(host, REQ_BUS_OFF);
Adrian Hunterf0710a52013-05-06 12:17:32 +03001593 if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
1594 sdhci_runtime_pm_bus_off(host);
Russell Kinge921a8b2014-04-25 13:00:01 +01001595 } else {
1596 /*
1597 * Spec says that we should clear the power reg before setting
1598 * a new value. Some controllers don't seem to like this though.
1599 */
Sahitya Tummala63d235d2013-02-21 10:09:49 +05301600 if (!(host->quirks & SDHCI_QUIRK_SINGLE_POWER_WRITE)) {
Russell Kinge921a8b2014-04-25 13:00:01 +01001601 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Sahitya Tummala63d235d2013-02-21 10:09:49 +05301602 if (host->ops->check_power_status)
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05301603 host->ops->check_power_status(host, REQ_BUS_OFF);
Sahitya Tummala63d235d2013-02-21 10:09:49 +05301604 }
Russell Kinge921a8b2014-04-25 13:00:01 +01001605 /*
1606 * At least the Marvell CaFe chip gets confused if we set the
1607 * voltage and set turn on power at the same time, so set the
1608 * voltage first.
1609 */
Sahitya Tummala63d235d2013-02-21 10:09:49 +05301610 if (host->quirks & SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER) {
Russell Kinge921a8b2014-04-25 13:00:01 +01001611 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
Sahitya Tummala63d235d2013-02-21 10:09:49 +05301612 if (host->ops->check_power_status)
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05301613 host->ops->check_power_status(host, REQ_BUS_ON);
Sahitya Tummala63d235d2013-02-21 10:09:49 +05301614 }
Pierre Ossman146ad662006-06-30 02:22:23 -07001615
Russell Kinge921a8b2014-04-25 13:00:01 +01001616 pwr |= SDHCI_POWER_ON;
1617
Pierre Ossmanae628902009-05-03 20:45:03 +02001618 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
Sahitya Tummala63d235d2013-02-21 10:09:49 +05301619 if (host->ops->check_power_status)
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05301620 host->ops->check_power_status(host, REQ_BUS_ON);
Pierre Ossmanae628902009-05-03 20:45:03 +02001621
Russell Kinge921a8b2014-04-25 13:00:01 +01001622 if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
1623 sdhci_runtime_pm_bus_on(host);
Andres Salomone08c1692008-07-04 10:00:03 -07001624
Russell Kinge921a8b2014-04-25 13:00:01 +01001625 /*
1626 * Some controllers need an extra 10ms delay of 10ms before
1627 * they can apply clock after applying power
1628 */
1629 if (host->quirks & SDHCI_QUIRK_DELAY_AFTER_POWER)
1630 mdelay(10);
1631 }
Adrian Hunter1dceb042016-03-29 12:45:43 +03001632}
Adrian Hunter606d3132016-10-05 12:11:22 +03001633EXPORT_SYMBOL_GPL(sdhci_set_power_noreg);
Jisheng Zhang918f4cb2015-12-11 21:36:29 +08001634
Adrian Hunter606d3132016-10-05 12:11:22 +03001635void sdhci_set_power(struct sdhci_host *host, unsigned char mode,
1636 unsigned short vdd)
Adrian Hunter1dceb042016-03-29 12:45:43 +03001637{
Adrian Hunter606d3132016-10-05 12:11:22 +03001638 if (IS_ERR(host->mmc->supply.vmmc))
1639 sdhci_set_power_noreg(host, mode, vdd);
Adrian Hunter1dceb042016-03-29 12:45:43 +03001640 else
Adrian Hunter606d3132016-10-05 12:11:22 +03001641 sdhci_set_power_reg(host, mode, vdd);
Pierre Ossman146ad662006-06-30 02:22:23 -07001642}
Adrian Hunter606d3132016-10-05 12:11:22 +03001643EXPORT_SYMBOL_GPL(sdhci_set_power);
Pierre Ossman146ad662006-06-30 02:22:23 -07001644
Pierre Ossmand129bce2006-03-24 03:18:17 -08001645/*****************************************************************************\
1646 * *
1647 * MMC callbacks *
1648 * *
1649\*****************************************************************************/
1650
Sahitya Tummalac6f48d42013-03-10 07:03:17 +05301651static int sdhci_enable(struct mmc_host *mmc)
1652{
1653 struct sdhci_host *host = mmc_priv(mmc);
1654
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301655 if (host->ops->platform_bus_voting)
1656 host->ops->platform_bus_voting(host, 1);
1657
Sahitya Tummalac6f48d42013-03-10 07:03:17 +05301658 return 0;
1659}
1660
1661static int sdhci_disable(struct mmc_host *mmc)
1662{
1663 struct sdhci_host *host = mmc_priv(mmc);
1664
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301665 if (host->ops->platform_bus_voting)
1666 host->ops->platform_bus_voting(host, 0);
1667
Sahitya Tummalac6f48d42013-03-10 07:03:17 +05301668 return 0;
1669}
1670
Talel Shenhare68741c2015-06-25 09:33:24 +03001671static void sdhci_notify_halt(struct mmc_host *mmc, bool halt)
1672{
1673 struct sdhci_host *host = mmc_priv(mmc);
1674
1675 pr_debug("%s: halt notification was sent, halt=%d\n",
1676 mmc_hostname(mmc), halt);
Subhash Jadavani2147ba92016-05-11 16:35:47 -07001677 if (host->flags & SDHCI_USE_64_BIT_DMA) {
Talel Shenhare68741c2015-06-25 09:33:24 +03001678 if (halt)
Subhash Jadavanice552942016-05-11 17:01:06 -07001679 host->desc_sz = 16;
Talel Shenhare68741c2015-06-25 09:33:24 +03001680 else
Subhash Jadavanice552942016-05-11 17:01:06 -07001681 host->desc_sz = SDHCI_ADMA2_64_DESC_SZ;
Talel Shenhare68741c2015-06-25 09:33:24 +03001682 }
1683}
1684
Sujit Reddy Thumma360bbf42013-06-19 20:15:37 +05301685static inline void sdhci_update_power_policy(struct sdhci_host *host,
1686 enum sdhci_power_policy policy)
1687{
1688 host->power_policy = policy;
1689}
1690
1691static int sdhci_notify_load(struct mmc_host *mmc, enum mmc_load state)
1692{
1693 int err = 0;
1694 struct sdhci_host *host = mmc_priv(mmc);
1695
1696 switch (state) {
1697 case MMC_LOAD_HIGH:
1698 sdhci_update_power_policy(host, SDHCI_PERFORMANCE_MODE);
1699 break;
1700 case MMC_LOAD_LOW:
1701 sdhci_update_power_policy(host, SDHCI_POWER_SAVE_MODE);
1702 break;
1703 default:
1704 err = -EINVAL;
1705 break;
1706 }
1707
Sahitya Tummalad3004702015-08-06 13:58:47 +05301708 if (host->ops->notify_load)
1709 err = host->ops->notify_load(host, state);
1710
Sujit Reddy Thumma360bbf42013-06-19 20:15:37 +05301711 return err;
1712}
1713
Asutosh Das494003b2013-03-20 22:53:40 +05301714static bool sdhci_check_state(struct sdhci_host *host)
1715{
1716 if (!host->clock || !host->pwr)
1717 return true;
1718 else
1719 return false;
1720}
1721
Asutosh Dasb58499d2013-07-30 19:07:29 +05301722static bool sdhci_check_auto_tuning(struct sdhci_host *host,
1723 struct mmc_command *cmd)
1724{
1725 if (((cmd->opcode != MMC_READ_SINGLE_BLOCK) &&
1726 (cmd->opcode != MMC_READ_MULTIPLE_BLOCK) &&
1727 (cmd->opcode != SD_IO_RW_EXTENDED)) || (host->clock < 100000000))
1728 return false;
1729 else if (host->mmc->ios.timing == MMC_TIMING_MMC_HS200 ||
1730 host->mmc->ios.timing == MMC_TIMING_UHS_SDR104)
1731 return true;
1732 else
1733 return false;
1734}
1735
1736static int sdhci_get_tuning_cmd(struct sdhci_host *host)
1737{
1738 if (!host->mmc || !host->mmc->card)
1739 return 0;
1740 /*
1741 * If we are here, all conditions have already been true
1742 * and the card can either be an eMMC or SD/SDIO
1743 */
1744 if (mmc_card_mmc(host->mmc->card))
1745 return MMC_SEND_TUNING_BLOCK_HS200;
1746 else
1747 return MMC_SEND_TUNING_BLOCK;
1748}
1749
Pierre Ossmand129bce2006-03-24 03:18:17 -08001750static void sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1751{
1752 struct sdhci_host *host;
Shawn Guo505a8682012-12-11 15:23:42 +08001753 int present;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001754 unsigned long flags;
1755
1756 host = mmc_priv(mmc);
1757
Asutosh Das494003b2013-03-20 22:53:40 +05301758 if (sdhci_check_state(host)) {
1759 sdhci_dump_state(host);
1760 WARN(1, "sdhci in bad state");
1761 mrq->cmd->error = -EIO;
1762 if (mrq->data)
1763 mrq->data->error = -EIO;
Venkat Gopalakrishnan987fc412015-09-01 15:52:29 -07001764 host->mrq = NULL;
1765 sdhci_dumpregs(host);
Asutosh Das06d9f322014-02-21 11:28:36 +05301766 mmc_request_done(host->mmc, mrq);
Asutosh Das494003b2013-03-20 22:53:40 +05301767 return;
1768 }
1769
Sahitya Tummala68adcae2013-04-16 18:06:06 +05301770 /*
1771 * Firstly check card presence from cd-gpio. The return could
1772 * be one of the following possibilities:
1773 * negative: cd-gpio is not available
1774 * zero: cd-gpio is used, and card is removed
1775 * one: cd-gpio is used, and card is present
1776 */
Adrian Hunter8d28b7a2016-02-09 16:12:36 +02001777 present = mmc->ops->get_cd(mmc);
Sahitya Tummala68adcae2013-04-16 18:06:06 +05301778 if (present < 0) {
1779 /* If polling, assume that the card is always present. */
1780 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
1781 present = 1;
1782 else
1783 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
1784 SDHCI_CARD_PRESENT;
1785 }
Krzysztof Kozlowski28367662015-01-05 10:50:15 +01001786
Pierre Ossmand129bce2006-03-24 03:18:17 -08001787 spin_lock_irqsave(&host->lock, flags);
1788
Pavan Anamula5b761502015-07-23 18:45:37 +05301789 if (!(host->quirks2 & SDHCI_QUIRK2_BROKEN_LED_CONTROL))
1790 sdhci_led_activate(host);
Andrei Warkentine89d4562011-05-23 15:06:37 -05001791
1792 /*
1793 * Ensure we don't send the STOP for non-SET_BLOCK_COUNTED
1794 * requests if Auto-CMD12 is enabled.
1795 */
Adrian Hunter0293d502016-06-29 16:24:35 +03001796 if (sdhci_auto_cmd12(host, mrq)) {
Jerry Huangc4512f72010-08-10 18:01:59 -07001797 if (mrq->stop) {
1798 mrq->data->stop = NULL;
1799 mrq->stop = NULL;
1800 }
1801 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001802
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001803 if (!present || host->flags & SDHCI_DEVICE_DEAD) {
Adrian Huntera4c73ab2016-06-29 16:24:25 +03001804 mrq->cmd->error = -ENOMEDIUM;
Adrian Huntera6d3bdd2016-06-29 16:24:27 +03001805 sdhci_finish_mrq(host, mrq);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301806 } else {
Asutosh Dasb58499d2013-07-30 19:07:29 +05301807 if (host->ops->config_auto_tuning_cmd) {
1808 if (sdhci_check_auto_tuning(host, mrq->cmd))
1809 host->ops->config_auto_tuning_cmd(host, true,
1810 sdhci_get_tuning_cmd(host));
1811 else
1812 host->ops->config_auto_tuning_cmd(host, false,
1813 sdhci_get_tuning_cmd(host));
1814 }
1815
Andrei Warkentin8edf63712011-05-23 15:06:39 -05001816 if (mrq->sbc && !(host->flags & SDHCI_AUTO_CMD23))
Andrei Warkentine89d4562011-05-23 15:06:37 -05001817 sdhci_send_command(host, mrq->sbc);
1818 else
1819 sdhci_send_command(host, mrq->cmd);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301820 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001821
Pierre Ossman5f25a662006-10-04 02:15:39 -07001822 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001823 spin_unlock_irqrestore(&host->lock, flags);
1824}
1825
Russell King2317f562014-04-25 12:57:07 +01001826void sdhci_set_bus_width(struct sdhci_host *host, int width)
1827{
1828 u8 ctrl;
1829
1830 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
1831 if (width == MMC_BUS_WIDTH_8) {
1832 ctrl &= ~SDHCI_CTRL_4BITBUS;
1833 if (host->version >= SDHCI_SPEC_300)
1834 ctrl |= SDHCI_CTRL_8BITBUS;
1835 } else {
1836 if (host->version >= SDHCI_SPEC_300)
1837 ctrl &= ~SDHCI_CTRL_8BITBUS;
1838 if (width == MMC_BUS_WIDTH_4)
1839 ctrl |= SDHCI_CTRL_4BITBUS;
1840 else
1841 ctrl &= ~SDHCI_CTRL_4BITBUS;
1842 }
1843 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1844}
1845EXPORT_SYMBOL_GPL(sdhci_set_bus_width);
1846
Russell King96d7b782014-04-25 12:59:26 +01001847void sdhci_set_uhs_signaling(struct sdhci_host *host, unsigned timing)
1848{
1849 u16 ctrl_2;
1850
1851 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1852 /* Select Bus Speed Mode for host */
1853 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
1854 if ((timing == MMC_TIMING_MMC_HS200) ||
1855 (timing == MMC_TIMING_UHS_SDR104))
1856 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
1857 else if (timing == MMC_TIMING_UHS_SDR12)
1858 ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
1859 else if (timing == MMC_TIMING_UHS_SDR25)
1860 ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
1861 else if (timing == MMC_TIMING_UHS_SDR50)
1862 ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
1863 else if ((timing == MMC_TIMING_UHS_DDR50) ||
1864 (timing == MMC_TIMING_MMC_DDR52))
1865 ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
Adrian Huntere9fb05d2014-11-06 15:19:06 +02001866 else if (timing == MMC_TIMING_MMC_HS400)
1867 ctrl_2 |= SDHCI_CTRL_HS400; /* Non-standard */
Russell King96d7b782014-04-25 12:59:26 +01001868 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
1869}
1870EXPORT_SYMBOL_GPL(sdhci_set_uhs_signaling);
1871
Ritesh Harjanice4b8f82015-11-25 10:37:21 +05301872void sdhci_cfg_irq(struct sdhci_host *host, bool enable, bool sync)
1873{
1874 if (enable && !(host->flags & SDHCI_HOST_IRQ_STATUS)) {
1875 enable_irq(host->irq);
1876 host->flags |= SDHCI_HOST_IRQ_STATUS;
1877 } else if (!enable && (host->flags & SDHCI_HOST_IRQ_STATUS)) {
1878 if (sync)
1879 disable_irq(host->irq);
1880 else
1881 disable_irq_nosync(host->irq);
1882 host->flags &= ~SDHCI_HOST_IRQ_STATUS;
1883 }
1884}
1885EXPORT_SYMBOL(sdhci_cfg_irq);
1886
Dong Aishengded97e02016-04-16 01:29:25 +08001887static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001888{
Dong Aishengded97e02016-04-16 01:29:25 +08001889 struct sdhci_host *host = mmc_priv(mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001890 unsigned long flags;
1891 u8 ctrl;
Asutosh Das30ec5992013-11-08 12:31:48 +05301892 int ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001893
Adrian Hunterceb61432011-12-27 15:48:41 +02001894 if (host->flags & SDHCI_DEVICE_DEAD) {
Tim Kryger3a48edc2014-06-13 10:13:56 -07001895 if (!IS_ERR(mmc->supply.vmmc) &&
1896 ios->power_mode == MMC_POWER_OFF)
Markus Mayer4e743f12014-07-03 13:27:42 -07001897 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0);
Adrian Hunterceb61432011-12-27 15:48:41 +02001898 return;
1899 }
Pierre Ossman1e728592008-04-16 19:13:13 +02001900
Kevin Liu52983382013-01-31 11:31:37 +08001901 if (host->version >= SDHCI_SPEC_300 &&
Dong Aisheng372c4632013-10-18 19:48:50 +08001902 (ios->power_mode == MMC_POWER_UP) &&
1903 !(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN))
Kevin Liu52983382013-01-31 11:31:37 +08001904 sdhci_enable_preset_value(host, false);
1905
Venkat Gopalakrishnan0fac7312015-01-23 12:58:00 -08001906 spin_lock_irqsave(&host->lock, flags);
Ritesh Harjanice4b8f82015-11-25 10:37:21 +05301907 if (host->mmc && host->mmc->card &&
1908 mmc_card_sdio(host->mmc->card))
1909 sdhci_cfg_irq(host, false, false);
1910
Venkat Gopalakrishnanf36166f2015-03-02 13:22:40 -08001911 if (ios->clock &&
1912 ((ios->clock != host->clock) || (ios->timing != host->timing))) {
Venkat Gopalakrishnan0fac7312015-01-23 12:58:00 -08001913 spin_unlock_irqrestore(&host->lock, flags);
1914 host->ops->set_clock(host, ios->clock);
1915 spin_lock_irqsave(&host->lock, flags);
1916 host->clock = ios->clock;
1917
1918 if (host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK &&
1919 host->clock) {
1920 host->timeout_clk = host->mmc->actual_clock ?
1921 host->mmc->actual_clock / 1000 :
1922 host->clock / 1000;
1923 host->mmc->max_busy_timeout =
1924 host->ops->get_max_timeout_count ?
1925 host->ops->get_max_timeout_count(host) :
1926 1 << 27;
1927 host->mmc->max_busy_timeout /= host->timeout_clk;
1928 }
1929 }
Ritesh Harjanice4b8f82015-11-25 10:37:21 +05301930 if (ios->clock && host->sdio_irq_async_status)
1931 sdhci_enable_sdio_irq_nolock(host, false);
Venkat Gopalakrishnan0fac7312015-01-23 12:58:00 -08001932 spin_unlock_irqrestore(&host->lock, flags);
1933
Asutosh Das30ec5992013-11-08 12:31:48 +05301934 /*
1935 * The controller clocks may be off during power-up and we may end up
1936 * enabling card clock before giving power to the card. Hence, during
1937 * MMC_POWER_UP enable the controller clock and turn-on the regulators.
1938 * The mmc_power_up would provide the necessary delay before turning on
1939 * the clocks to the card.
1940 */
1941 if (ios->power_mode & MMC_POWER_UP) {
1942 if (host->ops->enable_controller_clock) {
1943 ret = host->ops->enable_controller_clock(host);
1944 if (ret) {
1945 pr_err("%s: enabling controller clock: failed: %d\n",
1946 mmc_hostname(host->mmc), ret);
1947 } else {
1948 sdhci_set_power(host, ios->power_mode, ios->vdd);
1949 }
1950 }
1951 }
1952
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05301953 spin_lock_irqsave(&host->lock, flags);
Venkat Gopalakrishnan0fac7312015-01-23 12:58:00 -08001954 if (!host->clock) {
Ritesh Harjanice4b8f82015-11-25 10:37:21 +05301955 if (host->mmc && host->mmc->card &&
1956 mmc_card_sdio(host->mmc->card))
1957 sdhci_cfg_irq(host, true, false);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05301958 spin_unlock_irqrestore(&host->lock, flags);
Venkat Gopalakrishnan0fac7312015-01-23 12:58:00 -08001959 return;
Russell King373073e2014-04-25 12:58:45 +01001960 }
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05301961 spin_unlock_irqrestore(&host->lock, flags);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001962
Adrian Hunter606d3132016-10-05 12:11:22 +03001963 if (host->ops->set_power)
1964 host->ops->set_power(host, ios->power_mode, ios->vdd);
1965 else
Asutosh Das30ec5992013-11-08 12:31:48 +05301966 if (!host->ops->enable_controller_clock && (ios->power_mode &
1967 (MMC_POWER_UP |
1968 MMC_POWER_ON)))
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05301969 sdhci_set_power(host, ios->power_mode, ios->vdd);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001970
Sahitya Tummala63d235d2013-02-21 10:09:49 +05301971 spin_lock_irqsave(&host->lock, flags);
1972
Philip Rakity643a81f2010-09-23 08:24:32 -07001973 if (host->ops->platform_send_init_74_clocks)
1974 host->ops->platform_send_init_74_clocks(host, ios->power_mode);
1975
Russell King2317f562014-04-25 12:57:07 +01001976 host->ops->set_bus_width(host, ios->bus_width);
Philip Rakity15ec4462010-11-19 16:48:39 -05001977
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001978 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001979
Philip Rakity3ab9c8d2010-10-06 11:57:23 -07001980 if ((ios->timing == MMC_TIMING_SD_HS ||
1981 ios->timing == MMC_TIMING_MMC_HS)
1982 && !(host->quirks & SDHCI_QUIRK_NO_HISPD_BIT))
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001983 ctrl |= SDHCI_CTRL_HISPD;
1984 else
1985 ctrl &= ~SDHCI_CTRL_HISPD;
1986
Arindam Nathd6d50a12011-05-05 12:18:59 +05301987 if (host->version >= SDHCI_SPEC_300) {
Arindam Nath49c468f2011-05-05 12:19:01 +05301988 u16 clk, ctrl_2;
Arindam Nath49c468f2011-05-05 12:19:01 +05301989
1990 /* In case of UHS-I modes, set High Speed Enable */
Adrian Huntere9fb05d2014-11-06 15:19:06 +02001991 if ((ios->timing == MMC_TIMING_MMC_HS400) ||
1992 (ios->timing == MMC_TIMING_MMC_HS200) ||
Seungwon Jeonbb8175a2014-03-14 21:12:48 +09001993 (ios->timing == MMC_TIMING_MMC_DDR52) ||
Girish K S069c9f12012-01-06 09:56:39 +05301994 (ios->timing == MMC_TIMING_UHS_SDR50) ||
Arindam Nath49c468f2011-05-05 12:19:01 +05301995 (ios->timing == MMC_TIMING_UHS_SDR104) ||
1996 (ios->timing == MMC_TIMING_UHS_DDR50) ||
Alexander Elbsdd8df172012-01-03 23:26:53 -05001997 (ios->timing == MMC_TIMING_UHS_SDR25))
Arindam Nath49c468f2011-05-05 12:19:01 +05301998 ctrl |= SDHCI_CTRL_HISPD;
Arindam Nathd6d50a12011-05-05 12:18:59 +05301999
Russell Kingda91a8f2014-04-25 13:00:12 +01002000 if (!host->preset_enabled) {
Arindam Nath758535c2011-05-05 12:19:00 +05302001 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05302002 /*
2003 * We only need to set Driver Strength if the
2004 * preset value enable is not set.
2005 */
Russell Kingda91a8f2014-04-25 13:00:12 +01002006 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
Arindam Nathd6d50a12011-05-05 12:18:59 +05302007 ctrl_2 &= ~SDHCI_CTRL_DRV_TYPE_MASK;
2008 if (ios->drv_type == MMC_SET_DRIVER_TYPE_A)
2009 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_A;
Petri Gynther43e943a2015-05-20 14:35:00 -07002010 else if (ios->drv_type == MMC_SET_DRIVER_TYPE_B)
2011 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_B;
Arindam Nathd6d50a12011-05-05 12:18:59 +05302012 else if (ios->drv_type == MMC_SET_DRIVER_TYPE_C)
2013 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_C;
Petri Gynther43e943a2015-05-20 14:35:00 -07002014 else if (ios->drv_type == MMC_SET_DRIVER_TYPE_D)
2015 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_D;
2016 else {
Marek Vasut2e4456f2015-11-18 10:47:02 +01002017 pr_warn("%s: invalid driver type, default to driver type B\n",
2018 mmc_hostname(mmc));
Petri Gynther43e943a2015-05-20 14:35:00 -07002019 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_B;
2020 }
Arindam Nathd6d50a12011-05-05 12:18:59 +05302021
2022 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
Arindam Nath758535c2011-05-05 12:19:00 +05302023 } else {
2024 /*
2025 * According to SDHC Spec v3.00, if the Preset Value
2026 * Enable in the Host Control 2 register is set, we
2027 * need to reset SD Clock Enable before changing High
2028 * Speed Enable to avoid generating clock gliches.
2029 */
Arindam Nath758535c2011-05-05 12:19:00 +05302030
2031 /* Reset SD Clock Enable */
2032 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
2033 clk &= ~SDHCI_CLOCK_CARD_EN;
2034 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
2035
2036 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
2037
2038 /* Re-enable SD Clock */
Venkat Gopalakrishnan745734f2015-01-12 19:52:35 -08002039 if (ios->clock) {
2040 spin_unlock_irqrestore(&host->lock, flags);
Sujit Reddy Thummafbe7d862014-01-21 17:22:05 +05302041 host->ops->set_clock(host, host->clock);
Venkat Gopalakrishnan745734f2015-01-12 19:52:35 -08002042 spin_lock_irqsave(&host->lock, flags);
2043 }
Arindam Nathd6d50a12011-05-05 12:18:59 +05302044 }
Arindam Nath49c468f2011-05-05 12:19:01 +05302045
Arindam Nath49c468f2011-05-05 12:19:01 +05302046 /* Reset SD Clock Enable */
2047 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
2048 clk &= ~SDHCI_CLOCK_CARD_EN;
2049 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
2050
Russell King96d7b782014-04-25 12:59:26 +01002051 host->ops->set_uhs_signaling(host, ios->timing);
Russell Kingd975f122014-04-25 12:59:31 +01002052 host->timing = ios->timing;
Arindam Nath49c468f2011-05-05 12:19:01 +05302053
Kevin Liu52983382013-01-31 11:31:37 +08002054 if (!(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN) &&
2055 ((ios->timing == MMC_TIMING_UHS_SDR12) ||
2056 (ios->timing == MMC_TIMING_UHS_SDR25) ||
2057 (ios->timing == MMC_TIMING_UHS_SDR50) ||
2058 (ios->timing == MMC_TIMING_UHS_SDR104) ||
Jisheng Zhang0dafa602015-08-18 16:21:39 +08002059 (ios->timing == MMC_TIMING_UHS_DDR50) ||
2060 (ios->timing == MMC_TIMING_MMC_DDR52))) {
Kevin Liu52983382013-01-31 11:31:37 +08002061 u16 preset;
2062
2063 sdhci_enable_preset_value(host, true);
2064 preset = sdhci_get_preset_value(host);
2065 ios->drv_type = (preset & SDHCI_PRESET_DRV_MASK)
2066 >> SDHCI_PRESET_DRV_SHIFT;
2067 }
2068
Arindam Nath49c468f2011-05-05 12:19:01 +05302069 /* Re-enable SD Clock */
Venkat Gopalakrishnan745734f2015-01-12 19:52:35 -08002070 if (ios->clock) {
2071 spin_unlock_irqrestore(&host->lock, flags);
Sujit Reddy Thummafbe7d862014-01-21 17:22:05 +05302072 host->ops->set_clock(host, host->clock);
Venkat Gopalakrishnan745734f2015-01-12 19:52:35 -08002073 spin_lock_irqsave(&host->lock, flags);
2074 }
Arindam Nath758535c2011-05-05 12:19:00 +05302075 } else
2076 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05302077
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302078 spin_unlock_irqrestore(&host->lock, flags);
Leandro Dorileob8352262007-07-25 23:47:04 +02002079 /*
2080 * Some (ENE) controllers go apeshit on some ios operation,
2081 * signalling timeout and CRC errors even on CMD0. Resetting
2082 * it on each ios seems to solve the problem.
2083 */
Mohammad Jamalc63705e2015-01-13 20:47:24 +05302084 if (host->quirks & SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS)
Russell King03231f92014-04-25 12:57:12 +01002085 sdhci_do_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
Leandro Dorileob8352262007-07-25 23:47:04 +02002086
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302087 /*
2088 * Reset the chip on each power off.
2089 * Should clear out any weird states.
2090 */
2091 if (ios->power_mode == MMC_POWER_OFF) {
2092 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
2093 sdhci_reinit(host);
2094 sdhci_set_power(host, ios->power_mode, ios->vdd);
2095 }
2096 if (!ios->clock)
Venkat Gopalakrishnan766b7452015-03-10 15:51:23 -07002097 host->ops->set_clock(host, ios->clock);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302098
Ritesh Harjanice4b8f82015-11-25 10:37:21 +05302099 spin_lock_irqsave(&host->lock, flags);
2100 if (host->mmc && host->mmc->card &&
2101 mmc_card_sdio(host->mmc->card))
2102 sdhci_cfg_irq(host, true, false);
2103 spin_unlock_irqrestore(&host->lock, flags);
2104
Pierre Ossman5f25a662006-10-04 02:15:39 -07002105 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002106}
2107
Dong Aishengded97e02016-04-16 01:29:25 +08002108static int sdhci_get_cd(struct mmc_host *mmc)
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002109{
2110 struct sdhci_host *host = mmc_priv(mmc);
Dong Aishengded97e02016-04-16 01:29:25 +08002111 int gpio_cd = mmc_gpio_get_cd(mmc);
Kevin Liu94144a42013-02-28 17:35:53 +08002112
2113 if (host->flags & SDHCI_DEVICE_DEAD)
2114 return 0;
2115
Ivan T. Ivanov88af5652015-07-06 15:16:19 +03002116 /* If nonremovable, assume that the card is always present. */
Jaehoon Chung860951c2016-06-21 10:13:26 +09002117 if (!mmc_card_is_removable(host->mmc))
Kevin Liu94144a42013-02-28 17:35:53 +08002118 return 1;
2119
Ivan T. Ivanov88af5652015-07-06 15:16:19 +03002120 /*
2121 * Try slot gpio detect, if defined it take precedence
2122 * over build in controller functionality
2123 */
Arnd Bergmann287980e2016-05-27 23:23:25 +02002124 if (gpio_cd >= 0)
Kevin Liu94144a42013-02-28 17:35:53 +08002125 return !!gpio_cd;
2126
Ivan T. Ivanov88af5652015-07-06 15:16:19 +03002127 /* If polling, assume that the card is always present. */
2128 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
2129 return 1;
2130
Kevin Liu94144a42013-02-28 17:35:53 +08002131 /* Host native card detect */
2132 return !!(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT);
2133}
2134
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002135static int sdhci_check_ro(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002136{
Pierre Ossmand129bce2006-03-24 03:18:17 -08002137 unsigned long flags;
Wolfram Sang2dfb5792010-10-15 12:21:01 +02002138 int is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002139
Pierre Ossmand129bce2006-03-24 03:18:17 -08002140 spin_lock_irqsave(&host->lock, flags);
2141
Pierre Ossman1e728592008-04-16 19:13:13 +02002142 if (host->flags & SDHCI_DEVICE_DEAD)
Wolfram Sang2dfb5792010-10-15 12:21:01 +02002143 is_readonly = 0;
2144 else if (host->ops->get_ro)
2145 is_readonly = host->ops->get_ro(host);
Pierre Ossman1e728592008-04-16 19:13:13 +02002146 else
Wolfram Sang2dfb5792010-10-15 12:21:01 +02002147 is_readonly = !(sdhci_readl(host, SDHCI_PRESENT_STATE)
2148 & SDHCI_WRITE_PROTECT);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002149
2150 spin_unlock_irqrestore(&host->lock, flags);
2151
Wolfram Sang2dfb5792010-10-15 12:21:01 +02002152 /* This quirk needs to be replaced by a callback-function later */
2153 return host->quirks & SDHCI_QUIRK_INVERTED_WRITE_PROTECT ?
2154 !is_readonly : is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002155}
2156
Takashi Iwai82b0e232011-04-21 20:26:38 +02002157#define SAMPLE_COUNT 5
2158
Dong Aishengded97e02016-04-16 01:29:25 +08002159static int sdhci_get_ro(struct mmc_host *mmc)
Takashi Iwai82b0e232011-04-21 20:26:38 +02002160{
Dong Aishengded97e02016-04-16 01:29:25 +08002161 struct sdhci_host *host = mmc_priv(mmc);
Takashi Iwai82b0e232011-04-21 20:26:38 +02002162 int i, ro_count;
2163
Takashi Iwai82b0e232011-04-21 20:26:38 +02002164 if (!(host->quirks & SDHCI_QUIRK_UNSTABLE_RO_DETECT))
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002165 return sdhci_check_ro(host);
Takashi Iwai82b0e232011-04-21 20:26:38 +02002166
2167 ro_count = 0;
2168 for (i = 0; i < SAMPLE_COUNT; i++) {
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002169 if (sdhci_check_ro(host)) {
Takashi Iwai82b0e232011-04-21 20:26:38 +02002170 if (++ro_count > SAMPLE_COUNT / 2)
2171 return 1;
2172 }
2173 msleep(30);
2174 }
2175 return 0;
2176}
2177
Adrian Hunter20758b62011-08-29 16:42:12 +03002178static void sdhci_hw_reset(struct mmc_host *mmc)
2179{
2180 struct sdhci_host *host = mmc_priv(mmc);
2181
2182 if (host->ops && host->ops->hw_reset)
2183 host->ops->hw_reset(host);
2184}
2185
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002186static void sdhci_enable_sdio_irq_nolock(struct sdhci_host *host, int enable)
2187{
Ritesh Harjanice4b8f82015-11-25 10:37:21 +05302188 u16 ctrl = 0;
Russell Kingb537f942014-04-25 12:56:01 +01002189
Ritesh Harjanice4b8f82015-11-25 10:37:21 +05302190 if (host->flags & SDHCI_DEVICE_DEAD)
2191 return;
2192
2193 if (mmc_card_and_host_support_async_int(host->mmc)) {
2194 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2195 if (enable)
2196 ctrl |= SDHCI_CTRL_ASYNC_INT_ENABLE;
2197 else
2198 ctrl &= ~SDHCI_CTRL_ASYNC_INT_ENABLE;
2199 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Russell Kingef104332014-04-25 12:55:41 +01002200 }
Ritesh Harjanice4b8f82015-11-25 10:37:21 +05302201
2202 if (enable)
2203 host->ier |= SDHCI_INT_CARD_INT;
2204 else
2205 host->ier &= ~SDHCI_INT_CARD_INT;
2206
2207 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
2208 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
2209 mmiowb();
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002210}
Pierre Ossmanf75979b2007-09-04 07:59:18 +02002211
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002212static void sdhci_enable_sdio_irq(struct mmc_host *mmc, int enable)
2213{
2214 struct sdhci_host *host = mmc_priv(mmc);
2215 unsigned long flags;
2216
Hans de Goedefa3b4f42017-03-26 13:14:45 +02002217 if (enable)
2218 pm_runtime_get_noresume(host->mmc->parent);
2219
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002220 spin_lock_irqsave(&host->lock, flags);
Russell Kingef104332014-04-25 12:55:41 +01002221 if (enable)
2222 host->flags |= SDHCI_SDIO_IRQ_ENABLED;
2223 else
2224 host->flags &= ~SDHCI_SDIO_IRQ_ENABLED;
2225
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002226 sdhci_enable_sdio_irq_nolock(host, enable);
Pierre Ossmanf75979b2007-09-04 07:59:18 +02002227 spin_unlock_irqrestore(&host->lock, flags);
Hans de Goedefa3b4f42017-03-26 13:14:45 +02002228
2229 if (!enable)
2230 pm_runtime_put_noidle(host->mmc->parent);
Pierre Ossmanf75979b2007-09-04 07:59:18 +02002231}
2232
Dong Aishengded97e02016-04-16 01:29:25 +08002233static int sdhci_start_signal_voltage_switch(struct mmc_host *mmc,
2234 struct mmc_ios *ios)
Philip Rakity6231f3d2012-07-23 15:56:23 -07002235{
Dong Aishengded97e02016-04-16 01:29:25 +08002236 struct sdhci_host *host = mmc_priv(mmc);
Philip Rakity6231f3d2012-07-23 15:56:23 -07002237 u16 ctrl;
Kevin Liu20b92a32012-12-17 19:29:26 +08002238 int ret;
Philip Rakity6231f3d2012-07-23 15:56:23 -07002239
2240 /*
2241 * Signal Voltage Switching is only applicable for Host Controllers
2242 * v3.00 and above.
2243 */
2244 if (host->version < SDHCI_SPEC_300)
2245 return 0;
2246
Philip Rakity6231f3d2012-07-23 15:56:23 -07002247 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
Kevin Liu20b92a32012-12-17 19:29:26 +08002248
Fabio Estevam21f59982013-02-14 10:35:03 -02002249 switch (ios->signal_voltage) {
Kevin Liu20b92a32012-12-17 19:29:26 +08002250 case MMC_SIGNAL_VOLTAGE_330:
Adrian Hunter8cb851a2016-06-29 16:24:16 +03002251 if (!(host->flags & SDHCI_SIGNALING_330))
2252 return -EINVAL;
Kevin Liu20b92a32012-12-17 19:29:26 +08002253 /* Set 1.8V Signal Enable in the Host Control2 register to 0 */
2254 ctrl &= ~SDHCI_CTRL_VDD_180;
2255 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Sahitya Tummala63d235d2013-02-21 10:09:49 +05302256 if (host->ops->check_power_status)
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302257 host->ops->check_power_status(host, REQ_IO_HIGH);
Kevin Liu20b92a32012-12-17 19:29:26 +08002258
Tim Kryger3a48edc2014-06-13 10:13:56 -07002259 if (!IS_ERR(mmc->supply.vqmmc)) {
Dong Aisheng761daa32016-07-12 15:46:10 +08002260 ret = mmc_regulator_set_vqmmc(mmc, ios);
Kevin Liu20b92a32012-12-17 19:29:26 +08002261 if (ret) {
Joe Perches66061102014-09-12 14:56:56 -07002262 pr_warn("%s: Switching to 3.3V signalling voltage failed\n",
2263 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08002264 return -EIO;
2265 }
2266 }
2267 /* Wait for 5ms */
2268 usleep_range(5000, 5500);
2269
2270 /* 3.3V regulator output should be stable within 5 ms */
2271 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2272 if (!(ctrl & SDHCI_CTRL_VDD_180))
2273 return 0;
2274
Joe Perches66061102014-09-12 14:56:56 -07002275 pr_warn("%s: 3.3V regulator output did not became stable\n",
2276 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08002277
2278 return -EAGAIN;
2279 case MMC_SIGNAL_VOLTAGE_180:
Adrian Hunter8cb851a2016-06-29 16:24:16 +03002280 if (!(host->flags & SDHCI_SIGNALING_180))
2281 return -EINVAL;
Tim Kryger3a48edc2014-06-13 10:13:56 -07002282 if (!IS_ERR(mmc->supply.vqmmc)) {
Dong Aisheng761daa32016-07-12 15:46:10 +08002283 ret = mmc_regulator_set_vqmmc(mmc, ios);
Kevin Liu20b92a32012-12-17 19:29:26 +08002284 if (ret) {
Joe Perches66061102014-09-12 14:56:56 -07002285 pr_warn("%s: Switching to 1.8V signalling voltage failed\n",
2286 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08002287 return -EIO;
2288 }
2289 }
2290
2291 /*
2292 * Enable 1.8V Signal Enable in the Host Control2
2293 * register
2294 */
2295 ctrl |= SDHCI_CTRL_VDD_180;
2296 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Sahitya Tummala63d235d2013-02-21 10:09:49 +05302297 if (host->ops->check_power_status)
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302298 host->ops->check_power_status(host, REQ_IO_LOW);
Kevin Liu20b92a32012-12-17 19:29:26 +08002299
Vincent Yang9d967a62015-01-20 16:05:15 +08002300 /* Some controller need to do more when switching */
2301 if (host->ops->voltage_switch)
2302 host->ops->voltage_switch(host);
2303
Kevin Liu20b92a32012-12-17 19:29:26 +08002304 /* 1.8V regulator output should be stable within 5 ms */
2305 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2306 if (ctrl & SDHCI_CTRL_VDD_180)
2307 return 0;
2308
Joe Perches66061102014-09-12 14:56:56 -07002309 pr_warn("%s: 1.8V regulator output did not became stable\n",
2310 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08002311
2312 return -EAGAIN;
2313 case MMC_SIGNAL_VOLTAGE_120:
Adrian Hunter8cb851a2016-06-29 16:24:16 +03002314 if (!(host->flags & SDHCI_SIGNALING_120))
2315 return -EINVAL;
Tim Kryger3a48edc2014-06-13 10:13:56 -07002316 if (!IS_ERR(mmc->supply.vqmmc)) {
Dong Aisheng761daa32016-07-12 15:46:10 +08002317 ret = mmc_regulator_set_vqmmc(mmc, ios);
Kevin Liu20b92a32012-12-17 19:29:26 +08002318 if (ret) {
Joe Perches66061102014-09-12 14:56:56 -07002319 pr_warn("%s: Switching to 1.2V signalling voltage failed\n",
2320 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08002321 return -EIO;
2322 }
2323 }
2324 return 0;
2325 default:
Arindam Nathf2119df2011-05-05 12:18:57 +05302326 /* No signal voltage switch required */
2327 return 0;
Kevin Liu20b92a32012-12-17 19:29:26 +08002328 }
Arindam Nathf2119df2011-05-05 12:18:57 +05302329}
2330
Kevin Liu20b92a32012-12-17 19:29:26 +08002331static int sdhci_card_busy(struct mmc_host *mmc)
2332{
2333 struct sdhci_host *host = mmc_priv(mmc);
2334 u32 present_state;
2335
Adrian Huntere613cc42016-06-23 14:00:58 +03002336 /* Check whether DAT[0] is 0 */
Kevin Liu20b92a32012-12-17 19:29:26 +08002337 present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
Kevin Liu20b92a32012-12-17 19:29:26 +08002338
Adrian Huntere613cc42016-06-23 14:00:58 +03002339 return !(present_state & SDHCI_DATA_0_LVL_MASK);
Kevin Liu20b92a32012-12-17 19:29:26 +08002340}
2341
Adrian Hunterb5540ce2014-12-05 19:25:31 +02002342static int sdhci_prepare_hs400_tuning(struct mmc_host *mmc, struct mmc_ios *ios)
2343{
2344 struct sdhci_host *host = mmc_priv(mmc);
2345 unsigned long flags;
2346
2347 spin_lock_irqsave(&host->lock, flags);
2348 host->flags |= SDHCI_HS400_TUNING;
2349 spin_unlock_irqrestore(&host->lock, flags);
2350
2351 return 0;
2352}
2353
Ritesh Harjani9ce9ecf2015-05-27 15:32:40 +05302354static int sdhci_enhanced_strobe(struct mmc_host *mmc)
2355{
2356 struct sdhci_host *host = mmc_priv(mmc);
2357 int err = 0;
2358
Ritesh Harjani9ce9ecf2015-05-27 15:32:40 +05302359 if (host->ops->enhanced_strobe)
2360 err = host->ops->enhanced_strobe(host);
Ritesh Harjani9ce9ecf2015-05-27 15:32:40 +05302361
2362 return err;
2363}
2364
Girish K S069c9f12012-01-06 09:56:39 +05302365static int sdhci_execute_tuning(struct mmc_host *mmc, u32 opcode)
Arindam Nathb513ea22011-05-05 12:19:04 +05302366{
Russell King4b6f37d2014-04-25 12:59:36 +01002367 struct sdhci_host *host = mmc_priv(mmc);
Arindam Nathb513ea22011-05-05 12:19:04 +05302368 u16 ctrl;
Arindam Nathb513ea22011-05-05 12:19:04 +05302369 int tuning_loop_counter = MAX_TUNING_LOOP;
Arindam Nathb513ea22011-05-05 12:19:04 +05302370 int err = 0;
Aisheng Dong2b35bd82013-12-23 19:13:04 +08002371 unsigned long flags;
Adrian Hunter38e40bf2014-12-05 19:25:30 +02002372 unsigned int tuning_count = 0;
Adrian Hunterb5540ce2014-12-05 19:25:31 +02002373 bool hs400_tuning;
Arindam Nathb513ea22011-05-05 12:19:04 +05302374
Aisheng Dong2b35bd82013-12-23 19:13:04 +08002375 spin_lock_irqsave(&host->lock, flags);
Arindam Nathb513ea22011-05-05 12:19:04 +05302376
Adrian Hunterb5540ce2014-12-05 19:25:31 +02002377 hs400_tuning = host->flags & SDHCI_HS400_TUNING;
2378 host->flags &= ~SDHCI_HS400_TUNING;
2379
Adrian Hunter38e40bf2014-12-05 19:25:30 +02002380 if (host->tuning_mode == SDHCI_TUNING_MODE_1)
2381 tuning_count = host->tuning_count;
2382
Arindam Nathb513ea22011-05-05 12:19:04 +05302383 /*
Weijun Yang9faac7b2015-10-04 12:04:12 +00002384 * The Host Controller needs tuning in case of SDR104 and DDR50
2385 * mode, and for SDR50 mode when Use Tuning for SDR50 is set in
2386 * the Capabilities register.
Girish K S069c9f12012-01-06 09:56:39 +05302387 * If the Host Controller supports the HS200 mode then the
2388 * tuning function has to be executed.
Arindam Nathb513ea22011-05-05 12:19:04 +05302389 */
Russell King4b6f37d2014-04-25 12:59:36 +01002390 switch (host->timing) {
Adrian Hunterb5540ce2014-12-05 19:25:31 +02002391 /* HS400 tuning is done in HS200 mode */
Adrian Huntere9fb05d2014-11-06 15:19:06 +02002392 case MMC_TIMING_MMC_HS400:
Sayali Lokhande27d29e22016-07-21 05:58:43 -07002393 if (!(mmc->caps2 & MMC_CAP2_HS400_POST_TUNING)) {
2394 err = -EINVAL;
2395 goto out_unlock;
2396 }
Russell King4b6f37d2014-04-25 12:59:36 +01002397 case MMC_TIMING_MMC_HS200:
Adrian Hunterb5540ce2014-12-05 19:25:31 +02002398 /*
2399 * Periodic re-tuning for HS400 is not expected to be needed, so
2400 * disable it here.
2401 */
2402 if (hs400_tuning)
2403 tuning_count = 0;
2404 break;
2405
Russell King4b6f37d2014-04-25 12:59:36 +01002406 case MMC_TIMING_UHS_SDR104:
Weijun Yang9faac7b2015-10-04 12:04:12 +00002407 case MMC_TIMING_UHS_DDR50:
Russell King4b6f37d2014-04-25 12:59:36 +01002408 break;
Girish K S069c9f12012-01-06 09:56:39 +05302409
Russell King4b6f37d2014-04-25 12:59:36 +01002410 case MMC_TIMING_UHS_SDR50:
Adrian Hunter4228b212016-04-20 09:24:03 +03002411 if (host->flags & SDHCI_SDR50_NEEDS_TUNING)
Russell King4b6f37d2014-04-25 12:59:36 +01002412 break;
2413 /* FALLTHROUGH */
2414
2415 default:
Adrian Hunterd519c862014-12-05 19:25:29 +02002416 goto out_unlock;
Arindam Nathb513ea22011-05-05 12:19:04 +05302417 }
2418
Dong Aisheng45251812013-09-13 19:11:30 +08002419 if (host->ops->platform_execute_tuning) {
Aisheng Dong2b35bd82013-12-23 19:13:04 +08002420 spin_unlock_irqrestore(&host->lock, flags);
Dong Aisheng45251812013-09-13 19:11:30 +08002421 err = host->ops->platform_execute_tuning(host, opcode);
Dong Aisheng45251812013-09-13 19:11:30 +08002422 return err;
2423 }
2424
Russell King4b6f37d2014-04-25 12:59:36 +01002425 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2426 ctrl |= SDHCI_CTRL_EXEC_TUNING;
Vincent Yang67d0d042015-01-20 16:05:16 +08002427 if (host->quirks2 & SDHCI_QUIRK2_TUNING_WORK_AROUND)
2428 ctrl |= SDHCI_CTRL_TUNED_CLK;
Arindam Nathb513ea22011-05-05 12:19:04 +05302429 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
2430
2431 /*
2432 * As per the Host Controller spec v3.00, tuning command
2433 * generates Buffer Read Ready interrupt, so enable that.
2434 *
2435 * Note: The spec clearly says that when tuning sequence
2436 * is being performed, the controller does not generate
2437 * interrupts other than Buffer Read Ready interrupt. But
2438 * to make sure we don't hit a controller bug, we _only_
2439 * enable Buffer Read Ready interrupt here.
2440 */
Russell Kingb537f942014-04-25 12:56:01 +01002441 sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_INT_ENABLE);
2442 sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_SIGNAL_ENABLE);
Arindam Nathb513ea22011-05-05 12:19:04 +05302443
2444 /*
2445 * Issue CMD19 repeatedly till Execute Tuning is set to 0 or the number
Simon Horman1473bdd2016-05-13 13:24:31 +09002446 * of loops reaches 40 times.
Arindam Nathb513ea22011-05-05 12:19:04 +05302447 */
Arindam Nathb513ea22011-05-05 12:19:04 +05302448 do {
2449 struct mmc_command cmd = {0};
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002450 struct mmc_request mrq = {NULL};
Arindam Nathb513ea22011-05-05 12:19:04 +05302451
Girish K S069c9f12012-01-06 09:56:39 +05302452 cmd.opcode = opcode;
Arindam Nathb513ea22011-05-05 12:19:04 +05302453 cmd.arg = 0;
2454 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
2455 cmd.retries = 0;
2456 cmd.data = NULL;
Adrian Hunter4e9f8fe2016-06-29 16:24:34 +03002457 cmd.mrq = &mrq;
Arindam Nathb513ea22011-05-05 12:19:04 +05302458 cmd.error = 0;
2459
Al Cooper7ce45e92014-05-09 11:34:07 -04002460 if (tuning_loop_counter-- == 0)
2461 break;
2462
Arindam Nathb513ea22011-05-05 12:19:04 +05302463 mrq.cmd = &cmd;
Arindam Nathb513ea22011-05-05 12:19:04 +05302464
2465 /*
2466 * In response to CMD19, the card sends 64 bytes of tuning
2467 * block to the Host Controller. So we set the block size
2468 * to 64 here.
2469 */
Girish K S069c9f12012-01-06 09:56:39 +05302470 if (cmd.opcode == MMC_SEND_TUNING_BLOCK_HS200) {
2471 if (mmc->ios.bus_width == MMC_BUS_WIDTH_8)
Subhash Jadavani3ed97712014-09-02 17:55:54 -07002472 sdhci_set_blk_size_reg(host, 128, 7);
Girish K S069c9f12012-01-06 09:56:39 +05302473 else if (mmc->ios.bus_width == MMC_BUS_WIDTH_4)
Subhash Jadavani3ed97712014-09-02 17:55:54 -07002474 sdhci_set_blk_size_reg(host, 64, 7);
Girish K S069c9f12012-01-06 09:56:39 +05302475 } else {
Subhash Jadavani3ed97712014-09-02 17:55:54 -07002476 sdhci_set_blk_size_reg(host, 64, 7);
Girish K S069c9f12012-01-06 09:56:39 +05302477 }
Arindam Nathb513ea22011-05-05 12:19:04 +05302478
2479 /*
2480 * The tuning block is sent by the card to the host controller.
2481 * So we set the TRNS_READ bit in the Transfer Mode register.
2482 * This also takes care of setting DMA Enable and Multi Block
2483 * Select in the same register to 0.
2484 */
2485 sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE);
2486
2487 sdhci_send_command(host, &cmd);
2488
2489 host->cmd = NULL;
Adrian Hunter07c161b2016-06-29 16:24:38 +03002490 sdhci_del_timer(host, &mrq);
Arindam Nathb513ea22011-05-05 12:19:04 +05302491
Aisheng Dong2b35bd82013-12-23 19:13:04 +08002492 spin_unlock_irqrestore(&host->lock, flags);
Arindam Nathb513ea22011-05-05 12:19:04 +05302493 /* Wait for Buffer Read Ready interrupt */
Christopher Freeman622b5f32016-08-17 13:34:27 -04002494 wait_event_timeout(host->buf_ready_int,
Arindam Nathb513ea22011-05-05 12:19:04 +05302495 (host->tuning_done == 1),
2496 msecs_to_jiffies(50));
Aisheng Dong2b35bd82013-12-23 19:13:04 +08002497 spin_lock_irqsave(&host->lock, flags);
Arindam Nathb513ea22011-05-05 12:19:04 +05302498
2499 if (!host->tuning_done) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01002500 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 +05302501 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2502 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
2503 ctrl &= ~SDHCI_CTRL_EXEC_TUNING;
2504 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
2505
Adrian Huntercee93582016-12-02 15:14:20 +02002506 sdhci_do_reset(host, SDHCI_RESET_CMD);
2507 sdhci_do_reset(host, SDHCI_RESET_DATA);
2508
Arindam Nathb513ea22011-05-05 12:19:04 +05302509 err = -EIO;
Adrian Huntercee93582016-12-02 15:14:20 +02002510
2511 if (cmd.opcode != MMC_SEND_TUNING_BLOCK_HS200)
2512 goto out;
2513
2514 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
2515 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
2516
2517 spin_unlock_irqrestore(&host->lock, flags);
2518
2519 memset(&cmd, 0, sizeof(cmd));
2520 cmd.opcode = MMC_STOP_TRANSMISSION;
2521 cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
2522 cmd.busy_timeout = 50;
2523 mmc_wait_for_cmd(mmc, &cmd, 0);
2524
2525 spin_lock_irqsave(&host->lock, flags);
2526
Arindam Nathb513ea22011-05-05 12:19:04 +05302527 goto out;
2528 }
2529
2530 host->tuning_done = 0;
2531
2532 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
Nick Sanders197160d2014-05-06 18:52:38 -07002533
2534 /* eMMC spec does not require a delay between tuning cycles */
2535 if (opcode == MMC_SEND_TUNING_BLOCK)
2536 mdelay(1);
Arindam Nathb513ea22011-05-05 12:19:04 +05302537 } while (ctrl & SDHCI_CTRL_EXEC_TUNING);
2538
2539 /*
2540 * The Host Driver has exhausted the maximum number of loops allowed,
2541 * so use fixed sampling frequency.
2542 */
Al Cooper7ce45e92014-05-09 11:34:07 -04002543 if (tuning_loop_counter < 0) {
Arindam Nathb513ea22011-05-05 12:19:04 +05302544 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
2545 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Al Cooper7ce45e92014-05-09 11:34:07 -04002546 }
2547 if (!(ctrl & SDHCI_CTRL_TUNED_CLK)) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01002548 pr_info(DRIVER_NAME ": Tuning procedure failed, falling back to fixed sampling clock\n");
Dong Aisheng114f2bf2013-10-18 19:48:45 +08002549 err = -EIO;
Arindam Nathb513ea22011-05-05 12:19:04 +05302550 }
2551
2552out:
Adrian Hunter38e40bf2014-12-05 19:25:30 +02002553 if (tuning_count) {
Adrian Hunter66c39dfc2015-05-07 13:10:21 +03002554 /*
2555 * In case tuning fails, host controllers which support
2556 * re-tuning can try tuning again at a later time, when the
2557 * re-tuning timer expires. So for these controllers, we
2558 * return 0. Since there might be other controllers who do not
2559 * have this capability, we return error for them.
2560 */
2561 err = 0;
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302562 }
2563
Adrian Hunter66c39dfc2015-05-07 13:10:21 +03002564 host->mmc->retune_period = err ? 0 : tuning_count;
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302565
Russell Kingb537f942014-04-25 12:56:01 +01002566 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
2567 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Adrian Hunterd519c862014-12-05 19:25:29 +02002568out_unlock:
Aisheng Dong2b35bd82013-12-23 19:13:04 +08002569 spin_unlock_irqrestore(&host->lock, flags);
Arindam Nathb513ea22011-05-05 12:19:04 +05302570 return err;
2571}
2572
Adrian Huntercb849642015-02-06 14:12:59 +02002573static int sdhci_select_drive_strength(struct mmc_card *card,
2574 unsigned int max_dtr, int host_drv,
2575 int card_drv, int *drv_type)
2576{
2577 struct sdhci_host *host = mmc_priv(card->host);
2578
2579 if (!host->ops->select_drive_strength)
2580 return 0;
2581
2582 return host->ops->select_drive_strength(host, card, max_dtr, host_drv,
2583 card_drv, drv_type);
2584}
Kevin Liu52983382013-01-31 11:31:37 +08002585
2586static void sdhci_enable_preset_value(struct sdhci_host *host, bool enable)
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302587{
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302588 /* Host Controller v3.00 defines preset value registers */
2589 if (host->version < SDHCI_SPEC_300)
2590 return;
2591
Sahitya Tummala314162c2013-04-12 12:11:20 +05302592 if (host->quirks2 & SDHCI_QUIRK2_BROKEN_PRESET_VALUE)
2593 return;
2594
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302595 /*
2596 * We only enable or disable Preset Value if they are not already
2597 * enabled or disabled respectively. Otherwise, we bail out.
2598 */
Russell Kingda91a8f2014-04-25 13:00:12 +01002599 if (host->preset_enabled != enable) {
2600 u16 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2601
2602 if (enable)
2603 ctrl |= SDHCI_CTRL_PRESET_VAL_ENABLE;
2604 else
2605 ctrl &= ~SDHCI_CTRL_PRESET_VAL_ENABLE;
2606
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302607 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Russell Kingda91a8f2014-04-25 13:00:12 +01002608
2609 if (enable)
2610 host->flags |= SDHCI_PV_ENABLED;
2611 else
2612 host->flags &= ~SDHCI_PV_ENABLED;
2613
2614 host->preset_enabled = enable;
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302615 }
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002616}
2617
Haibo Chen348487c2014-12-09 17:04:05 +08002618static void sdhci_post_req(struct mmc_host *mmc, struct mmc_request *mrq,
2619 int err)
2620{
2621 struct sdhci_host *host = mmc_priv(mmc);
2622 struct mmc_data *data = mrq->data;
2623
Russell Kingf48f0392016-01-26 13:40:32 +00002624 if (data->host_cookie != COOKIE_UNMAPPED)
Russell King771a3dc2016-01-26 13:40:53 +00002625 dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
2626 data->flags & MMC_DATA_WRITE ?
2627 DMA_TO_DEVICE : DMA_FROM_DEVICE);
2628
2629 data->host_cookie = COOKIE_UNMAPPED;
Gilad Broner07d92eb2015-09-29 16:57:21 +03002630
2631 if (host->ops->pre_req)
2632 host->ops->pre_req(host, mrq);
Haibo Chen348487c2014-12-09 17:04:05 +08002633}
2634
Haibo Chen348487c2014-12-09 17:04:05 +08002635static void sdhci_pre_req(struct mmc_host *mmc, struct mmc_request *mrq,
2636 bool is_first_req)
2637{
2638 struct sdhci_host *host = mmc_priv(mmc);
2639
Haibo Chend31911b2015-08-25 10:02:11 +08002640 mrq->data->host_cookie = COOKIE_UNMAPPED;
Haibo Chen348487c2014-12-09 17:04:05 +08002641
2642 if (host->flags & SDHCI_REQ_USE_DMA)
Russell King94538e52016-01-26 13:40:37 +00002643 sdhci_pre_dma_transfer(host, mrq->data, COOKIE_PRE_MAPPED);
Haibo Chen348487c2014-12-09 17:04:05 +08002644}
2645
Adrian Hunter5d0d11c2016-06-29 16:24:31 +03002646static inline bool sdhci_has_requests(struct sdhci_host *host)
2647{
2648 return host->cmd || host->data_cmd;
2649}
2650
2651static void sdhci_error_out_mrqs(struct sdhci_host *host, int err)
2652{
2653 if (host->data_cmd) {
2654 host->data_cmd->error = err;
2655 sdhci_finish_mrq(host, host->data_cmd->mrq);
2656 }
2657
2658 if (host->cmd) {
2659 host->cmd->error = err;
2660 sdhci_finish_mrq(host, host->cmd->mrq);
2661 }
2662}
2663
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002664static void sdhci_card_event(struct mmc_host *mmc)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002665{
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002666 struct sdhci_host *host = mmc_priv(mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002667 unsigned long flags;
Krzysztof Kozlowski28367662015-01-05 10:50:15 +01002668 int present;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002669
Christian Daudt722e1282013-06-20 14:26:36 -07002670 /* First check if client has provided their own card event */
2671 if (host->ops->card_event)
2672 host->ops->card_event(host);
2673
Adrian Hunterd3940f22016-06-29 16:24:14 +03002674 present = mmc->ops->get_cd(mmc);
Krzysztof Kozlowski28367662015-01-05 10:50:15 +01002675
Pierre Ossmand129bce2006-03-24 03:18:17 -08002676 spin_lock_irqsave(&host->lock, flags);
2677
Adrian Hunter5d0d11c2016-06-29 16:24:31 +03002678 /* Check sdhci_has_requests() first in case we are runtime suspended */
2679 if (sdhci_has_requests(host) && !present) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302680 pr_err("%s: Card removed during transfer!\n",
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002681 mmc_hostname(host->mmc));
Girish K Sa3c76eb2011-10-11 11:44:09 +05302682 pr_err("%s: Resetting controller.\n",
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002683 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08002684
Russell King03231f92014-04-25 12:57:12 +01002685 sdhci_do_reset(host, SDHCI_RESET_CMD);
2686 sdhci_do_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002687
Adrian Hunter5d0d11c2016-06-29 16:24:31 +03002688 sdhci_error_out_mrqs(host, -ENOMEDIUM);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002689 }
2690
2691 spin_unlock_irqrestore(&host->lock, flags);
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002692}
2693
Gilad Broner07d92eb2015-09-29 16:57:21 +03002694static int sdhci_late_init(struct mmc_host *mmc)
2695{
2696 struct sdhci_host *host = mmc_priv(mmc);
2697
2698 if (host->ops->init)
2699 host->ops->init(host);
2700
2701 return 0;
2702}
Venkat Gopalakrishnan28cfcee2015-09-15 15:57:35 -07002703
2704static void sdhci_force_err_irq(struct mmc_host *mmc, u64 errmask)
2705{
2706 struct sdhci_host *host = mmc_priv(mmc);
2707 u16 mask = errmask & 0xFFFF;
2708
2709 pr_err("%s: Force raise error mask:0x%04x\n", __func__, mask);
Venkat Gopalakrishnan28cfcee2015-09-15 15:57:35 -07002710 sdhci_writew(host, mask, SDHCI_SET_INT_ERROR);
Venkat Gopalakrishnan28cfcee2015-09-15 15:57:35 -07002711}
2712
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002713static const struct mmc_host_ops sdhci_ops = {
Gilad Broner07d92eb2015-09-29 16:57:21 +03002714 .init = sdhci_late_init,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002715 .request = sdhci_request,
Haibo Chen348487c2014-12-09 17:04:05 +08002716 .post_req = sdhci_post_req,
2717 .pre_req = sdhci_pre_req,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002718 .set_ios = sdhci_set_ios,
Kevin Liu94144a42013-02-28 17:35:53 +08002719 .get_cd = sdhci_get_cd,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002720 .get_ro = sdhci_get_ro,
2721 .hw_reset = sdhci_hw_reset,
2722 .enable_sdio_irq = sdhci_enable_sdio_irq,
2723 .start_signal_voltage_switch = sdhci_start_signal_voltage_switch,
Adrian Hunterb5540ce2014-12-05 19:25:31 +02002724 .prepare_hs400_tuning = sdhci_prepare_hs400_tuning,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002725 .execute_tuning = sdhci_execute_tuning,
Ritesh Harjani9ce9ecf2015-05-27 15:32:40 +05302726 .enhanced_strobe = sdhci_enhanced_strobe,
Adrian Huntercb849642015-02-06 14:12:59 +02002727 .select_drive_strength = sdhci_select_drive_strength,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002728 .card_event = sdhci_card_event,
Kevin Liu20b92a32012-12-17 19:29:26 +08002729 .card_busy = sdhci_card_busy,
Sahitya Tummalac6f48d42013-03-10 07:03:17 +05302730 .enable = sdhci_enable,
2731 .disable = sdhci_disable,
Sujit Reddy Thumma360bbf42013-06-19 20:15:37 +05302732 .notify_load = sdhci_notify_load,
Talel Shenhare68741c2015-06-25 09:33:24 +03002733 .notify_halt = sdhci_notify_halt,
Venkat Gopalakrishnan28cfcee2015-09-15 15:57:35 -07002734 .force_err_irq = sdhci_force_err_irq,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002735};
2736
2737/*****************************************************************************\
2738 * *
2739 * Tasklets *
2740 * *
2741\*****************************************************************************/
2742
Adrian Hunter4e9f8fe2016-06-29 16:24:34 +03002743static bool sdhci_request_done(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002744{
Pierre Ossmand129bce2006-03-24 03:18:17 -08002745 unsigned long flags;
2746 struct mmc_request *mrq;
Adrian Hunter4e9f8fe2016-06-29 16:24:34 +03002747 int i;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002748
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002749 spin_lock_irqsave(&host->lock, flags);
2750
Adrian Hunter4e9f8fe2016-06-29 16:24:34 +03002751 for (i = 0; i < SDHCI_MAX_MRQS; i++) {
2752 mrq = host->mrqs_done[i];
Adrian Hunter6ebebea2016-11-02 15:49:08 +02002753 if (mrq)
Adrian Hunter4e9f8fe2016-06-29 16:24:34 +03002754 break;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002755 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002756
Adrian Hunter4e9f8fe2016-06-29 16:24:34 +03002757 if (!mrq) {
2758 spin_unlock_irqrestore(&host->lock, flags);
2759 return true;
2760 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002761
Adrian Hunterd7422fb2016-06-29 16:24:33 +03002762 sdhci_del_timer(host, mrq);
2763
Pierre Ossmand129bce2006-03-24 03:18:17 -08002764 /*
Russell King054cedf2016-01-26 13:40:42 +00002765 * Always unmap the data buffers if they were mapped by
2766 * sdhci_prepare_data() whenever we finish with a request.
2767 * This avoids leaking DMA mappings on error.
2768 */
2769 if (host->flags & SDHCI_REQ_USE_DMA) {
2770 struct mmc_data *data = mrq->data;
2771
2772 if (data && data->host_cookie == COOKIE_MAPPED) {
2773 dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
2774 (data->flags & MMC_DATA_READ) ?
2775 DMA_FROM_DEVICE : DMA_TO_DEVICE);
2776 data->host_cookie = COOKIE_UNMAPPED;
2777 }
2778 }
2779
2780 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08002781 * The controller needs a reset of internal state machines
2782 * upon error conditions.
2783 */
Adrian Hunter0cc563c2016-06-29 16:24:28 +03002784 if (sdhci_needs_reset(host, mrq)) {
Adrian Hunter6ebebea2016-11-02 15:49:08 +02002785 /*
2786 * Do not finish until command and data lines are available for
2787 * reset. Note there can only be one other mrq, so it cannot
2788 * also be in mrqs_done, otherwise host->cmd and host->data_cmd
2789 * would both be null.
2790 */
2791 if (host->cmd || host->data_cmd) {
2792 spin_unlock_irqrestore(&host->lock, flags);
2793 return true;
2794 }
2795
Pierre Ossman645289d2006-06-30 02:22:33 -07002796 /* Some controllers need this kick or reset won't work here */
Andy Shevchenko8213af32013-01-07 16:31:08 +02002797 if (host->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET)
Pierre Ossman645289d2006-06-30 02:22:33 -07002798 /* This is to force an update */
Russell King17710592014-04-25 12:58:55 +01002799 host->ops->set_clock(host, host->clock);
Pierre Ossman645289d2006-06-30 02:22:33 -07002800
2801 /* Spec says we should do both at the same time, but Ricoh
2802 controllers do not like that. */
Adrian Hunter6ebebea2016-11-02 15:49:08 +02002803 sdhci_do_reset(host, SDHCI_RESET_CMD);
2804 sdhci_do_reset(host, SDHCI_RESET_DATA);
Adrian Huntered1563d2016-06-29 16:24:29 +03002805
2806 host->pending_reset = false;
Venkat Gopalakrishnana58f91f2012-09-17 16:00:15 -07002807 } else {
2808 if (host->quirks2 & SDHCI_QUIRK2_RDWR_TX_ACTIVE_EOT)
2809 sdhci_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002810 }
2811
Adrian Hunter4e9f8fe2016-06-29 16:24:34 +03002812 if (!sdhci_has_requests(host))
Pavan Anamula5b761502015-07-23 18:45:37 +05302813 if (!(host->quirks2 & SDHCI_QUIRK2_BROKEN_LED_CONTROL))
2814 sdhci_led_deactivate(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002815
Adrian Hunter6ebebea2016-11-02 15:49:08 +02002816 host->mrqs_done[i] = NULL;
Sahitya Tummala9e7fadb2013-08-07 18:40:29 +05302817 host->auto_cmd_err_sts = 0;
Adrian Hunter6ebebea2016-11-02 15:49:08 +02002818
Pierre Ossman5f25a662006-10-04 02:15:39 -07002819 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002820 spin_unlock_irqrestore(&host->lock, flags);
2821
2822 mmc_request_done(host->mmc, mrq);
Adrian Hunter4e9f8fe2016-06-29 16:24:34 +03002823
2824 return false;
2825}
2826
2827static void sdhci_tasklet_finish(unsigned long param)
2828{
2829 struct sdhci_host *host = (struct sdhci_host *)param;
2830
2831 while (!sdhci_request_done(host))
2832 ;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002833}
2834
2835static void sdhci_timeout_timer(unsigned long data)
2836{
2837 struct sdhci_host *host;
2838 unsigned long flags;
2839
2840 host = (struct sdhci_host*)data;
2841
2842 spin_lock_irqsave(&host->lock, flags);
2843
Adrian Hunterd7422fb2016-06-29 16:24:33 +03002844 if (host->cmd && !sdhci_data_line_cmd(host->cmd)) {
2845 pr_err("%s: Timeout waiting for hardware cmd interrupt.\n",
2846 mmc_hostname(host->mmc));
2847 sdhci_dumpregs(host);
2848
2849 host->cmd->error = -ETIMEDOUT;
2850 sdhci_finish_mrq(host, host->cmd->mrq);
2851 }
2852
2853 mmiowb();
2854 spin_unlock_irqrestore(&host->lock, flags);
2855}
2856
2857static void sdhci_timeout_data_timer(unsigned long data)
2858{
2859 struct sdhci_host *host;
2860 unsigned long flags;
2861
2862 host = (struct sdhci_host *)data;
2863
2864 spin_lock_irqsave(&host->lock, flags);
2865
2866 if (host->data || host->data_cmd ||
2867 (host->cmd && sdhci_data_line_cmd(host->cmd))) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01002868 pr_err("%s: Timeout waiting for hardware interrupt.\n",
2869 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08002870 sdhci_dumpregs(host);
2871
2872 if (host->data) {
Asutosh Das494003b2013-03-20 22:53:40 +05302873 pr_info("%s: bytes to transfer: %d transferred: %d\n",
2874 mmc_hostname(host->mmc),
2875 (host->data->blksz * host->data->blocks),
2876 (sdhci_readw(host, SDHCI_BLOCK_SIZE) & 0xFFF) *
2877 sdhci_readw(host, SDHCI_BLOCK_COUNT));
Pierre Ossman17b04292007-07-22 22:18:46 +02002878 host->data->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002879 sdhci_finish_data(host);
Adrian Hunterd7422fb2016-06-29 16:24:33 +03002880 } else if (host->data_cmd) {
2881 host->data_cmd->error = -ETIMEDOUT;
2882 sdhci_finish_mrq(host, host->data_cmd->mrq);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002883 } else {
Adrian Hunterd7422fb2016-06-29 16:24:33 +03002884 host->cmd->error = -ETIMEDOUT;
2885 sdhci_finish_mrq(host, host->cmd->mrq);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002886 }
2887 }
2888
Pierre Ossman5f25a662006-10-04 02:15:39 -07002889 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002890 spin_unlock_irqrestore(&host->lock, flags);
2891}
2892
2893/*****************************************************************************\
2894 * *
2895 * Interrupt handling *
2896 * *
2897\*****************************************************************************/
2898
Adrian Hunterfc605f12016-10-05 12:11:21 +03002899static void sdhci_cmd_irq(struct sdhci_host *host, u32 intmask)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002900{
Asutosh Das09f36d02013-07-23 16:20:34 +05302901 u16 auto_cmd_status;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002902 if (!host->cmd) {
Adrian Huntered1563d2016-06-29 16:24:29 +03002903 /*
2904 * SDHCI recovers from errors by resetting the cmd and data
2905 * circuits. Until that is done, there very well might be more
2906 * interrupts, so ignore them in that case.
2907 */
2908 if (host->pending_reset)
2909 return;
Marek Vasut2e4456f2015-11-18 10:47:02 +01002910 pr_err("%s: Got command interrupt 0x%08x even though no command operation was in progress.\n",
2911 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002912 sdhci_dumpregs(host);
2913 return;
2914 }
2915
Venkat Gopalakrishnana71fbae2014-06-09 14:00:31 -07002916 trace_mmc_cmd_rw_end(host->cmd->opcode, intmask,
2917 sdhci_readl(host, SDHCI_RESPONSE));
2918
Russell Kingec014cb2016-01-26 13:39:39 +00002919 if (intmask & (SDHCI_INT_TIMEOUT | SDHCI_INT_CRC |
Asutosh Das09f36d02013-07-23 16:20:34 +05302920 SDHCI_INT_END_BIT | SDHCI_INT_INDEX |
2921 SDHCI_INT_AUTO_CMD_ERR)) {
Russell Kingec014cb2016-01-26 13:39:39 +00002922 if (intmask & SDHCI_INT_TIMEOUT)
2923 host->cmd->error = -ETIMEDOUT;
2924 else
2925 host->cmd->error = -EILSEQ;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002926
Asutosh Das09f36d02013-07-23 16:20:34 +05302927 if (intmask & SDHCI_INT_AUTO_CMD_ERR) {
Sahitya Tummala9e7fadb2013-08-07 18:40:29 +05302928 auto_cmd_status = host->auto_cmd_err_sts;
Konstantin Dorfman4b0bcd32015-06-02 17:41:53 +03002929 pr_err_ratelimited("%s: %s: AUTO CMD err sts 0x%08x\n",
Sahitya Tummala9e7fadb2013-08-07 18:40:29 +05302930 mmc_hostname(host->mmc), __func__, auto_cmd_status);
Asutosh Das09f36d02013-07-23 16:20:34 +05302931 if (auto_cmd_status & (SDHCI_AUTO_CMD12_NOT_EXEC |
2932 SDHCI_AUTO_CMD_INDEX_ERR |
2933 SDHCI_AUTO_CMD_ENDBIT_ERR))
2934 host->cmd->error = -EIO;
2935 else if (auto_cmd_status & SDHCI_AUTO_CMD_TIMEOUT_ERR)
2936 host->cmd->error = -ETIMEDOUT;
2937 else if (auto_cmd_status & SDHCI_AUTO_CMD_CRC_ERR)
2938 host->cmd->error = -EILSEQ;
2939 }
2940
Russell King71fcbda2016-01-26 13:39:45 +00002941 /*
2942 * If this command initiates a data phase and a response
2943 * CRC error is signalled, the card can start transferring
2944 * data - the card may have received the command without
2945 * error. We must not terminate the mmc_request early.
2946 *
2947 * If the card did not receive the command or returned an
2948 * error which prevented it sending data, the data phase
2949 * will time out.
Ritesh Harjani8c0c9372016-05-19 14:04:34 +05302950 *
2951 * Even in case of cmd INDEX OR ENDBIT error we
2952 * handle it the same way.
Russell King71fcbda2016-01-26 13:39:45 +00002953 */
2954 if (host->cmd->data &&
Ritesh Harjani8c0c9372016-05-19 14:04:34 +05302955 (((intmask & (SDHCI_INT_CRC | SDHCI_INT_TIMEOUT)) ==
2956 SDHCI_INT_CRC) || (host->cmd->error == -EILSEQ))) {
Russell King71fcbda2016-01-26 13:39:45 +00002957 host->cmd = NULL;
2958 return;
2959 }
2960
Adrian Huntera6d3bdd2016-06-29 16:24:27 +03002961 sdhci_finish_mrq(host, host->cmd->mrq);
Pierre Ossmane8095172008-07-25 01:09:08 +02002962 return;
2963 }
2964
Pierre Ossmane8095172008-07-25 01:09:08 +02002965 if (intmask & SDHCI_INT_RESPONSE)
Pierre Ossman43b58b32007-07-25 23:15:27 +02002966 sdhci_finish_command(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002967}
2968
George G. Davis0957c332010-02-18 12:32:12 -05002969#ifdef CONFIG_MMC_DEBUG
Adrian Hunter08621b12014-11-04 12:42:38 +02002970static void sdhci_adma_show_error(struct sdhci_host *host)
Ben Dooks6882a8c2009-06-14 13:52:38 +01002971{
2972 const char *name = mmc_hostname(host->mmc);
Adrian Hunter1c3d5f62014-11-04 12:42:41 +02002973 void *desc = host->adma_table;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002974
2975 sdhci_dumpregs(host);
2976
2977 while (true) {
Adrian Huntere57a5f62014-11-04 12:42:46 +02002978 struct sdhci_adma2_64_desc *dma_desc = desc;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002979
Adrian Huntere57a5f62014-11-04 12:42:46 +02002980 if (host->flags & SDHCI_USE_64_BIT_DMA)
2981 DBG("%s: %p: DMA 0x%08x%08x, LEN 0x%04x, Attr=0x%02x\n",
2982 name, desc, le32_to_cpu(dma_desc->addr_hi),
2983 le32_to_cpu(dma_desc->addr_lo),
2984 le16_to_cpu(dma_desc->len),
2985 le16_to_cpu(dma_desc->cmd));
2986 else
2987 DBG("%s: %p: DMA 0x%08x, LEN 0x%04x, Attr=0x%02x\n",
2988 name, desc, le32_to_cpu(dma_desc->addr_lo),
2989 le16_to_cpu(dma_desc->len),
2990 le16_to_cpu(dma_desc->cmd));
Ben Dooks6882a8c2009-06-14 13:52:38 +01002991
Adrian Hunter76fe3792014-11-04 12:42:42 +02002992 desc += host->desc_sz;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002993
Adrian Hunter05452302014-11-04 12:42:45 +02002994 if (dma_desc->cmd & cpu_to_le16(ADMA2_END))
Ben Dooks6882a8c2009-06-14 13:52:38 +01002995 break;
2996 }
2997}
2998#else
Adrian Hunter08621b12014-11-04 12:42:38 +02002999static void sdhci_adma_show_error(struct sdhci_host *host) { }
Ben Dooks6882a8c2009-06-14 13:52:38 +01003000#endif
3001
Pierre Ossmand129bce2006-03-24 03:18:17 -08003002static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
3003{
Girish K S069c9f12012-01-06 09:56:39 +05303004 u32 command;
Asutosh Das494003b2013-03-20 22:53:40 +05303005 bool pr_msg = false;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003006
Venkat Gopalakrishnana71fbae2014-06-09 14:00:31 -07003007 command = SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND));
3008 trace_mmc_data_rw_end(command, intmask);
3009
Arindam Nathb513ea22011-05-05 12:19:04 +05303010 /* CMD19 generates _only_ Buffer Read Ready interrupt */
3011 if (intmask & SDHCI_INT_DATA_AVAIL) {
Sahitya Tummala05709df2016-04-01 14:29:22 +05303012 if (!(host->quirks2 & SDHCI_QUIRK2_NON_STANDARD_TUNING) &&
3013 (command == MMC_SEND_TUNING_BLOCK ||
3014 command == MMC_SEND_TUNING_BLOCK_HS200)) {
Arindam Nathb513ea22011-05-05 12:19:04 +05303015 host->tuning_done = 1;
3016 wake_up(&host->buf_ready_int);
3017 return;
3018 }
3019 }
3020
Pierre Ossmand129bce2006-03-24 03:18:17 -08003021 if (!host->data) {
Adrian Hunter7c89a3d2016-06-29 16:24:23 +03003022 struct mmc_command *data_cmd = host->data_cmd;
3023
Pierre Ossmand129bce2006-03-24 03:18:17 -08003024 /*
Pierre Ossmane8095172008-07-25 01:09:08 +02003025 * The "data complete" interrupt is also used to
3026 * indicate that a busy state has ended. See comment
3027 * above in sdhci_cmd_irq().
Pierre Ossmand129bce2006-03-24 03:18:17 -08003028 */
Adrian Hunter7c89a3d2016-06-29 16:24:23 +03003029 if (data_cmd && (data_cmd->flags & MMC_RSP_BUSY)) {
Matthieu CASTETc5abd5e2014-08-14 16:03:17 +02003030 if (intmask & SDHCI_INT_DATA_TIMEOUT) {
Adrian Hunter69b962a2016-11-02 15:49:09 +02003031 host->data_cmd = NULL;
Adrian Hunter7c89a3d2016-06-29 16:24:23 +03003032 data_cmd->error = -ETIMEDOUT;
Adrian Huntera6d3bdd2016-06-29 16:24:27 +03003033 sdhci_finish_mrq(host, data_cmd->mrq);
Matthieu CASTETc5abd5e2014-08-14 16:03:17 +02003034 return;
3035 }
Pierre Ossmane8095172008-07-25 01:09:08 +02003036 if (intmask & SDHCI_INT_DATA_END) {
Adrian Hunter69b962a2016-11-02 15:49:09 +02003037 host->data_cmd = NULL;
Chanho Mine99783a2014-08-30 12:40:40 +09003038 /*
3039 * Some cards handle busy-end interrupt
3040 * before the command completed, so make
3041 * sure we do things in the proper order.
3042 */
Adrian Hunterea968022016-06-29 16:24:24 +03003043 if (host->cmd == data_cmd)
3044 return;
3045
Adrian Huntera6d3bdd2016-06-29 16:24:27 +03003046 sdhci_finish_mrq(host, data_cmd->mrq);
Pierre Ossmane8095172008-07-25 01:09:08 +02003047 return;
3048 }
Sahitya Tummala87d43942013-04-12 11:49:11 +05303049 if (host->quirks2 &
3050 SDHCI_QUIRK2_IGNORE_DATATOUT_FOR_R1BCMD)
3051 return;
Pierre Ossmane8095172008-07-25 01:09:08 +02003052 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003053
Adrian Huntered1563d2016-06-29 16:24:29 +03003054 /*
3055 * SDHCI recovers from errors by resetting the cmd and data
3056 * circuits. Until that is done, there very well might be more
3057 * interrupts, so ignore them in that case.
3058 */
3059 if (host->pending_reset)
3060 return;
3061
Marek Vasut2e4456f2015-11-18 10:47:02 +01003062 pr_err("%s: Got data interrupt 0x%08x even though no data operation was in progress.\n",
3063 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003064 sdhci_dumpregs(host);
3065
3066 return;
3067 }
3068
3069 if (intmask & SDHCI_INT_DATA_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02003070 host->data->error = -ETIMEDOUT;
Aries Lee22113ef2010-12-15 08:14:24 +01003071 else if (intmask & SDHCI_INT_DATA_END_BIT)
3072 host->data->error = -EILSEQ;
3073 else if ((intmask & SDHCI_INT_DATA_CRC) &&
Venkat Gopalakrishnana71fbae2014-06-09 14:00:31 -07003074 (command != MMC_BUS_TEST_R))
Pierre Ossman17b04292007-07-22 22:18:46 +02003075 host->data->error = -EILSEQ;
Ben Dooks6882a8c2009-06-14 13:52:38 +01003076 else if (intmask & SDHCI_INT_ADMA_ERROR) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05303077 pr_err("%s: ADMA error\n", mmc_hostname(host->mmc));
Adrian Hunter08621b12014-11-04 12:42:38 +02003078 sdhci_adma_show_error(host);
Pierre Ossman2134a922008-06-28 18:28:51 +02003079 host->data->error = -EIO;
Haijun Zhanga4071fb2012-12-04 10:41:28 +08003080 if (host->ops->adma_workaround)
3081 host->ops->adma_workaround(host, intmask);
Ben Dooks6882a8c2009-06-14 13:52:38 +01003082 }
Asutosh Das494003b2013-03-20 22:53:40 +05303083 if (host->data->error) {
Pavan Anamula716d8c22016-04-29 11:37:23 +05303084 if (intmask & (SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT
3085 | SDHCI_INT_DATA_END_BIT)) {
Asutosh Das494003b2013-03-20 22:53:40 +05303086 command = SDHCI_GET_CMD(sdhci_readw(host,
3087 SDHCI_COMMAND));
3088 if ((command != MMC_SEND_TUNING_BLOCK_HS200) &&
3089 (command != MMC_SEND_TUNING_BLOCK))
3090 pr_msg = true;
3091 } else {
3092 pr_msg = true;
3093 }
Sahitya Tummala4c196de2014-10-31 14:00:12 +05303094 if (pr_msg && __ratelimit(&host->dbg_dump_rs)) {
Sahitya Tummala16dabee2013-04-08 12:53:44 +05303095 pr_err("%s: data txfr (0x%08x) error: %d after %lld ms\n",
Asutosh Das494003b2013-03-20 22:53:40 +05303096 mmc_hostname(host->mmc), intmask,
Sahitya Tummala16dabee2013-04-08 12:53:44 +05303097 host->data->error, ktime_to_ms(ktime_sub(
3098 ktime_get(), host->data_start_time)));
Asutosh Das494003b2013-03-20 22:53:40 +05303099 sdhci_dumpregs(host);
3100 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003101 sdhci_finish_data(host);
Asutosh Das494003b2013-03-20 22:53:40 +05303102 } else {
Pierre Ossmana406f5a2006-07-02 16:50:59 +01003103 if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
Pierre Ossmand129bce2006-03-24 03:18:17 -08003104 sdhci_transfer_pio(host);
3105
Pierre Ossman6ba736a2007-05-13 22:39:23 +02003106 /*
3107 * We currently don't do anything fancy with DMA
3108 * boundaries, but as we can't disable the feature
3109 * we need to at least restart the transfer.
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04003110 *
3111 * According to the spec sdhci_readl(host, SDHCI_DMA_ADDRESS)
3112 * should return a valid address to continue from, but as
3113 * some controllers are faulty, don't trust them.
Pierre Ossman6ba736a2007-05-13 22:39:23 +02003114 */
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04003115 if (intmask & SDHCI_INT_DMA_END) {
3116 u32 dmastart, dmanow;
3117 dmastart = sg_dma_address(host->data->sg);
3118 dmanow = dmastart + host->data->bytes_xfered;
3119 /*
3120 * Force update to the next DMA block boundary.
3121 */
3122 dmanow = (dmanow &
3123 ~(SDHCI_DEFAULT_BOUNDARY_SIZE - 1)) +
3124 SDHCI_DEFAULT_BOUNDARY_SIZE;
3125 host->data->bytes_xfered = dmanow - dmastart;
3126 DBG("%s: DMA base 0x%08x, transferred 0x%06x bytes,"
3127 " next 0x%08x\n",
3128 mmc_hostname(host->mmc), dmastart,
3129 host->data->bytes_xfered, dmanow);
3130 sdhci_writel(host, dmanow, SDHCI_DMA_ADDRESS);
3131 }
Pierre Ossman6ba736a2007-05-13 22:39:23 +02003132
Pierre Ossmane538fbe2007-08-12 16:46:32 +02003133 if (intmask & SDHCI_INT_DATA_END) {
Adrian Hunter7c89a3d2016-06-29 16:24:23 +03003134 if (host->cmd == host->data_cmd) {
Pierre Ossmane538fbe2007-08-12 16:46:32 +02003135 /*
3136 * Data managed to finish before the
3137 * command completed. Make sure we do
3138 * things in the proper order.
3139 */
3140 host->data_early = 1;
3141 } else {
3142 sdhci_finish_data(host);
3143 }
3144 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003145 }
3146}
3147
Asutosh Das3621b372014-10-17 16:36:47 +05303148#ifdef CONFIG_MMC_CQ_HCI
Asutosh Dasf67a83f2015-02-27 00:02:02 +05303149static int sdhci_get_cmd_err(u32 intmask)
3150{
3151 if (intmask & SDHCI_INT_TIMEOUT)
3152 return -ETIMEDOUT;
3153 else if (intmask & (SDHCI_INT_CRC | SDHCI_INT_END_BIT |
3154 SDHCI_INT_INDEX))
3155 return -EILSEQ;
3156 return 0;
3157}
3158
3159static int sdhci_get_data_err(u32 intmask)
3160{
3161 if (intmask & SDHCI_INT_DATA_TIMEOUT)
3162 return -ETIMEDOUT;
3163 else if (intmask & (SDHCI_INT_DATA_END_BIT | SDHCI_INT_DATA_CRC))
3164 return -EILSEQ;
3165 else if (intmask & SDHCI_INT_ADMA_ERROR)
3166 return -EIO;
3167 return 0;
3168}
3169
Venkat Gopalakrishnan7c6a56c12015-09-15 12:33:13 -07003170static irqreturn_t sdhci_cmdq_irq(struct sdhci_host *host, u32 intmask)
Asutosh Das3621b372014-10-17 16:36:47 +05303171{
Asutosh Dasf67a83f2015-02-27 00:02:02 +05303172 int err = 0;
Venkat Gopalakrishnan7c6a56c12015-09-15 12:33:13 -07003173 u32 mask = 0;
Ritesh Harjaniaf18da92015-10-01 11:41:29 +05303174 irqreturn_t ret;
Asutosh Dasf67a83f2015-02-27 00:02:02 +05303175
3176 if (intmask & SDHCI_INT_CMD_MASK)
3177 err = sdhci_get_cmd_err(intmask);
3178 else if (intmask & SDHCI_INT_DATA_MASK)
3179 err = sdhci_get_data_err(intmask);
3180
Ritesh Harjaniaf18da92015-10-01 11:41:29 +05303181 ret = cmdq_irq(host->mmc, err);
Venkat Gopalakrishnan7c6a56c12015-09-15 12:33:13 -07003182 if (err) {
3183 /* Clear the error interrupts */
3184 mask = intmask & SDHCI_INT_ERROR_MASK;
3185 sdhci_writel(host, mask, SDHCI_INT_STATUS);
3186 }
Ritesh Harjaniaf18da92015-10-01 11:41:29 +05303187 return ret;
Venkat Gopalakrishnan7c6a56c12015-09-15 12:33:13 -07003188
Asutosh Das3621b372014-10-17 16:36:47 +05303189}
3190
3191#else
Venkat Gopalakrishnan7c6a56c12015-09-15 12:33:13 -07003192static irqreturn_t sdhci_cmdq_irq(struct sdhci_host *host, u32 intmask)
Asutosh Das3621b372014-10-17 16:36:47 +05303193{
Venkat Gopalakrishnan7c6a56c12015-09-15 12:33:13 -07003194 pr_err("%s: Received cmdq-irq when disabled !!!!\n",
3195 mmc_hostname(host->mmc));
Asutosh Das3621b372014-10-17 16:36:47 +05303196 return IRQ_NONE;
3197}
3198#endif
3199
David Howells7d12e782006-10-05 14:55:46 +01003200static irqreturn_t sdhci_irq(int irq, void *dev_id)
Pierre Ossmand129bce2006-03-24 03:18:17 -08003201{
Russell King781e9892014-04-25 12:55:46 +01003202 irqreturn_t result = IRQ_NONE;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003203 struct sdhci_host *host = dev_id;
Russell King41005002014-04-25 12:55:36 +01003204 u32 intmask, mask, unexpected = 0;
Russell King781e9892014-04-25 12:55:46 +01003205 int max_loops = 16;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003206
3207 spin_lock(&host->lock);
3208
Russell Kingbe138552014-04-25 12:55:56 +01003209 if (host->runtime_suspended && !sdhci_sdio_irq_enabled(host)) {
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003210 spin_unlock(&host->lock);
Adrian Hunter655bca72014-03-11 10:09:36 +02003211 return IRQ_NONE;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003212 }
3213
Ritesh Harjanice4b8f82015-11-25 10:37:21 +05303214 if (!host->clock && host->mmc->card &&
3215 mmc_card_sdio(host->mmc->card)) {
Pavan Anamulaf2dda062016-03-30 22:07:56 +05303216 if (!mmc_card_and_host_support_async_int(host->mmc)) {
3217 spin_unlock(&host->lock);
Ritesh Harjanice4b8f82015-11-25 10:37:21 +05303218 return IRQ_NONE;
Pavan Anamulaf2dda062016-03-30 22:07:56 +05303219 }
Ritesh Harjanice4b8f82015-11-25 10:37:21 +05303220 /*
3221 * async card interrupt is level sensitive and received
3222 * when clocks are off.
3223 * If sdio card has asserted async interrupt, in that
3224 * case we need to disable host->irq.
3225 * Later we can disable card interrupt and re-enable
3226 * host->irq.
3227 */
3228
3229 pr_debug("%s: %s: sdio_async intr. received\n",
3230 mmc_hostname(host->mmc), __func__);
3231 sdhci_cfg_irq(host, false, false);
3232 host->sdio_irq_async_status = true;
3233 host->thread_isr |= SDHCI_INT_CARD_INT;
3234 result = IRQ_WAKE_THREAD;
3235 spin_unlock(&host->lock);
3236 return result;
3237 }
3238
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03003239 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
Mark Lord62df67a52007-03-06 13:30:13 +01003240 if (!intmask || intmask == 0xffffffff) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08003241 result = IRQ_NONE;
3242 goto out;
3243 }
3244
Russell King41005002014-04-25 12:55:36 +01003245 do {
Asutosh Das3621b372014-10-17 16:36:47 +05303246 if (host->mmc->card && mmc_card_cmdq(host->mmc->card) &&
Sahitya Tummala94f45082016-04-26 16:31:03 +05303247 !mmc_host_halt(host->mmc) && !mmc_host_cq_disable(host->mmc)) {
Asutosh Das3621b372014-10-17 16:36:47 +05303248 pr_debug("*** %s: cmdq intr: 0x%08x\n",
3249 mmc_hostname(host->mmc),
3250 intmask);
Venkat Gopalakrishnan7c6a56c12015-09-15 12:33:13 -07003251 result = sdhci_cmdq_irq(host, intmask);
Asutosh Dasf67a83f2015-02-27 00:02:02 +05303252 if (result == IRQ_HANDLED)
3253 goto out;
Asutosh Das3621b372014-10-17 16:36:47 +05303254 }
3255
Sayali Lokhandebff771e2016-11-30 11:35:22 +05303256 MMC_TRACE(host->mmc,
3257 "%s: intmask: 0x%x\n", __func__, intmask);
3258
Sahitya Tummala9e7fadb2013-08-07 18:40:29 +05303259 if (intmask & SDHCI_INT_AUTO_CMD_ERR)
3260 host->auto_cmd_err_sts = sdhci_readw(host,
3261 SDHCI_AUTO_CMD_ERR);
Russell King41005002014-04-25 12:55:36 +01003262 /* Clear selected interrupts. */
3263 mask = intmask & (SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK |
3264 SDHCI_INT_BUS_POWER);
3265 sdhci_writel(host, mask, SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003266
Russell King41005002014-04-25 12:55:36 +01003267 DBG("*** %s got interrupt: 0x%08x\n",
3268 mmc_hostname(host->mmc), intmask);
3269
3270 if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
3271 u32 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
3272 SDHCI_CARD_PRESENT;
3273
3274 /*
3275 * There is a observation on i.mx esdhc. INSERT
3276 * bit will be immediately set again when it gets
3277 * cleared, if a card is inserted. We have to mask
3278 * the irq to prevent interrupt storm which will
3279 * freeze the system. And the REMOVE gets the
3280 * same situation.
3281 *
3282 * More testing are needed here to ensure it works
3283 * for other platforms though.
3284 */
Russell Kingb537f942014-04-25 12:56:01 +01003285 host->ier &= ~(SDHCI_INT_CARD_INSERT |
3286 SDHCI_INT_CARD_REMOVE);
3287 host->ier |= present ? SDHCI_INT_CARD_REMOVE :
3288 SDHCI_INT_CARD_INSERT;
3289 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
3290 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Russell King41005002014-04-25 12:55:36 +01003291
3292 sdhci_writel(host, intmask & (SDHCI_INT_CARD_INSERT |
3293 SDHCI_INT_CARD_REMOVE), SDHCI_INT_STATUS);
Russell King3560db82014-04-25 12:55:51 +01003294
3295 host->thread_isr |= intmask & (SDHCI_INT_CARD_INSERT |
3296 SDHCI_INT_CARD_REMOVE);
3297 result = IRQ_WAKE_THREAD;
Russell King41005002014-04-25 12:55:36 +01003298 }
3299
Venkat Gopalakrishnana58f91f2012-09-17 16:00:15 -07003300 if (intmask & SDHCI_INT_CMD_MASK) {
3301 if ((host->quirks2 & SDHCI_QUIRK2_SLOW_INT_CLR) &&
3302 (host->clock <= 400000))
3303 udelay(40);
Adrian Hunterfc605f12016-10-05 12:11:21 +03003304 sdhci_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK);
Venkat Gopalakrishnana58f91f2012-09-17 16:00:15 -07003305 }
Russell King41005002014-04-25 12:55:36 +01003306
Venkat Gopalakrishnana58f91f2012-09-17 16:00:15 -07003307 if (intmask & SDHCI_INT_DATA_MASK) {
3308 if ((host->quirks2 & SDHCI_QUIRK2_SLOW_INT_CLR) &&
3309 (host->clock <= 400000))
3310 udelay(40);
Russell King41005002014-04-25 12:55:36 +01003311 sdhci_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
Venkat Gopalakrishnana58f91f2012-09-17 16:00:15 -07003312 }
Russell King41005002014-04-25 12:55:36 +01003313
3314 if (intmask & SDHCI_INT_BUS_POWER)
3315 pr_err("%s: Card is consuming too much power!\n",
3316 mmc_hostname(host->mmc));
3317
Dong Aishengf37b20e2016-07-12 15:46:17 +08003318 if (intmask & SDHCI_INT_RETUNE)
3319 mmc_retune_needed(host->mmc);
3320
Gabriel Krisman Bertazi04eb7db2017-01-16 12:23:42 -02003321 if ((intmask & SDHCI_INT_CARD_INT) &&
3322 (host->ier & SDHCI_INT_CARD_INT)) {
Russell King781e9892014-04-25 12:55:46 +01003323 sdhci_enable_sdio_irq_nolock(host, false);
3324 host->thread_isr |= SDHCI_INT_CARD_INT;
3325 result = IRQ_WAKE_THREAD;
3326 }
Russell King41005002014-04-25 12:55:36 +01003327
3328 intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE |
3329 SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK |
3330 SDHCI_INT_ERROR | SDHCI_INT_BUS_POWER |
Dong Aishengf37b20e2016-07-12 15:46:17 +08003331 SDHCI_INT_RETUNE | SDHCI_INT_CARD_INT);
Russell King41005002014-04-25 12:55:36 +01003332
3333 if (intmask) {
3334 unexpected |= intmask;
3335 sdhci_writel(host, intmask, SDHCI_INT_STATUS);
3336 }
3337
Russell King781e9892014-04-25 12:55:46 +01003338 if (result == IRQ_NONE)
3339 result = IRQ_HANDLED;
Russell King41005002014-04-25 12:55:36 +01003340
3341 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
Russell King41005002014-04-25 12:55:36 +01003342 } while (intmask && --max_loops);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003343out:
3344 spin_unlock(&host->lock);
3345
Alexander Stein6379b232012-03-14 09:52:10 +01003346 if (unexpected) {
3347 pr_err("%s: Unexpected interrupt 0x%08x.\n",
3348 mmc_hostname(host->mmc), unexpected);
3349 sdhci_dumpregs(host);
3350 }
Pierre Ossmanf75979b2007-09-04 07:59:18 +02003351
Pierre Ossmand129bce2006-03-24 03:18:17 -08003352 return result;
3353}
3354
Russell King781e9892014-04-25 12:55:46 +01003355static irqreturn_t sdhci_thread_irq(int irq, void *dev_id)
3356{
3357 struct sdhci_host *host = dev_id;
3358 unsigned long flags;
3359 u32 isr;
3360
3361 spin_lock_irqsave(&host->lock, flags);
3362 isr = host->thread_isr;
3363 host->thread_isr = 0;
3364 spin_unlock_irqrestore(&host->lock, flags);
3365
Russell King3560db82014-04-25 12:55:51 +01003366 if (isr & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
Adrian Hunterd3940f22016-06-29 16:24:14 +03003367 struct mmc_host *mmc = host->mmc;
3368
3369 mmc->ops->card_event(mmc);
3370 mmc_detect_change(mmc, msecs_to_jiffies(200));
Russell King3560db82014-04-25 12:55:51 +01003371 }
3372
Russell King781e9892014-04-25 12:55:46 +01003373 if (isr & SDHCI_INT_CARD_INT) {
3374 sdio_run_irqs(host->mmc);
3375
3376 spin_lock_irqsave(&host->lock, flags);
Ritesh Harjanice4b8f82015-11-25 10:37:21 +05303377 if (host->flags & SDHCI_SDIO_IRQ_ENABLED) {
3378 if (host->sdio_irq_async_status)
3379 host->sdio_irq_async_status = false;
Russell King781e9892014-04-25 12:55:46 +01003380 sdhci_enable_sdio_irq_nolock(host, true);
Ritesh Harjanice4b8f82015-11-25 10:37:21 +05303381 }
Russell King781e9892014-04-25 12:55:46 +01003382 spin_unlock_irqrestore(&host->lock, flags);
3383 }
3384
3385 return isr ? IRQ_HANDLED : IRQ_NONE;
3386}
3387
Pierre Ossmand129bce2006-03-24 03:18:17 -08003388/*****************************************************************************\
3389 * *
3390 * Suspend/resume *
3391 * *
3392\*****************************************************************************/
3393
3394#ifdef CONFIG_PM
Ludovic Desroches84d62602016-05-13 15:16:02 +02003395/*
3396 * To enable wakeup events, the corresponding events have to be enabled in
3397 * the Interrupt Status Enable register too. See 'Table 1-6: Wakeup Signal
3398 * Table' in the SD Host Controller Standard Specification.
3399 * It is useless to restore SDHCI_INT_ENABLE state in
3400 * sdhci_disable_irq_wakeups() since it will be set by
3401 * sdhci_enable_card_detection() or sdhci_init().
3402 */
Kevin Liuad080d72013-01-05 17:21:33 +08003403void sdhci_enable_irq_wakeups(struct sdhci_host *host)
3404{
3405 u8 val;
3406 u8 mask = SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE
3407 | SDHCI_WAKE_ON_INT;
Ludovic Desroches84d62602016-05-13 15:16:02 +02003408 u32 irq_val = SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE |
3409 SDHCI_INT_CARD_INT;
Kevin Liuad080d72013-01-05 17:21:33 +08003410
3411 val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
3412 val |= mask ;
3413 /* Avoid fake wake up */
Ludovic Desroches84d62602016-05-13 15:16:02 +02003414 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) {
Kevin Liuad080d72013-01-05 17:21:33 +08003415 val &= ~(SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE);
Ludovic Desroches84d62602016-05-13 15:16:02 +02003416 irq_val &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE);
3417 }
Kevin Liuad080d72013-01-05 17:21:33 +08003418 sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
Ludovic Desroches84d62602016-05-13 15:16:02 +02003419 sdhci_writel(host, irq_val, SDHCI_INT_ENABLE);
Kevin Liuad080d72013-01-05 17:21:33 +08003420}
3421EXPORT_SYMBOL_GPL(sdhci_enable_irq_wakeups);
3422
Fabio Estevam0b10f472014-08-30 14:53:13 -03003423static void sdhci_disable_irq_wakeups(struct sdhci_host *host)
Kevin Liuad080d72013-01-05 17:21:33 +08003424{
3425 u8 val;
3426 u8 mask = SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE
3427 | SDHCI_WAKE_ON_INT;
3428
3429 val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
3430 val &= ~mask;
3431 sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
3432}
Pierre Ossmand129bce2006-03-24 03:18:17 -08003433
Manuel Lauss29495aa2011-11-03 11:09:45 +01003434int sdhci_suspend_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08003435{
Anton Vorontsov7260cf52009-03-17 00:13:48 +03003436 sdhci_disable_card_detection(host);
3437
Adrian Hunter66c39dfc2015-05-07 13:10:21 +03003438 mmc_retune_timer_stop(host->mmc);
Dong Aishengf37b20e2016-07-12 15:46:17 +08003439 if (host->tuning_mode != SDHCI_TUNING_MODE_3)
3440 mmc_retune_needed(host->mmc);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303441
Kevin Liuad080d72013-01-05 17:21:33 +08003442 if (!device_may_wakeup(mmc_dev(host->mmc))) {
Russell Kingb537f942014-04-25 12:56:01 +01003443 host->ier = 0;
3444 sdhci_writel(host, 0, SDHCI_INT_ENABLE);
3445 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Kevin Liuad080d72013-01-05 17:21:33 +08003446 free_irq(host->irq, host);
3447 } else {
3448 sdhci_enable_irq_wakeups(host);
3449 enable_irq_wake(host->irq);
3450 }
Ulf Hansson4ee14ec2013-09-25 14:15:24 +02003451 return 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003452}
3453
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003454EXPORT_SYMBOL_GPL(sdhci_suspend_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003455
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003456int sdhci_resume_host(struct sdhci_host *host)
3457{
Adrian Hunterd3940f22016-06-29 16:24:14 +03003458 struct mmc_host *mmc = host->mmc;
Ulf Hansson4ee14ec2013-09-25 14:15:24 +02003459 int ret = 0;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003460
Richard Röjforsa13abc72009-09-22 16:45:30 -07003461 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003462 if (host->ops->enable_dma)
3463 host->ops->enable_dma(host);
3464 }
3465
Adrian Hunter6308d292012-02-07 14:48:54 +02003466 if ((host->mmc->pm_flags & MMC_PM_KEEP_POWER) &&
3467 (host->quirks2 & SDHCI_QUIRK2_HOST_OFF_CARD_ON)) {
3468 /* Card keeps power but host controller does not */
3469 sdhci_init(host, 0);
3470 host->pwr = 0;
3471 host->clock = 0;
Adrian Hunterd3940f22016-06-29 16:24:14 +03003472 mmc->ops->set_ios(mmc, &mmc->ios);
Adrian Hunter6308d292012-02-07 14:48:54 +02003473 } else {
3474 sdhci_init(host, (host->mmc->pm_flags & MMC_PM_KEEP_POWER));
3475 mmiowb();
3476 }
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003477
Haibo Chen14a7b41642015-09-15 18:32:58 +08003478 if (!device_may_wakeup(mmc_dev(host->mmc))) {
3479 ret = request_threaded_irq(host->irq, sdhci_irq,
3480 sdhci_thread_irq, IRQF_SHARED,
3481 mmc_hostname(host->mmc), host);
3482 if (ret)
3483 return ret;
3484 } else {
3485 sdhci_disable_irq_wakeups(host);
3486 disable_irq_wake(host->irq);
3487 }
3488
Anton Vorontsov7260cf52009-03-17 00:13:48 +03003489 sdhci_enable_card_detection(host);
3490
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08003491 return ret;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003492}
3493
3494EXPORT_SYMBOL_GPL(sdhci_resume_host);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003495
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003496int sdhci_runtime_suspend_host(struct sdhci_host *host)
3497{
3498 unsigned long flags;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003499
Adrian Hunter66c39dfc2015-05-07 13:10:21 +03003500 mmc_retune_timer_stop(host->mmc);
Dong Aishengf37b20e2016-07-12 15:46:17 +08003501 if (host->tuning_mode != SDHCI_TUNING_MODE_3)
3502 mmc_retune_needed(host->mmc);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003503
3504 spin_lock_irqsave(&host->lock, flags);
Russell Kingb537f942014-04-25 12:56:01 +01003505 host->ier &= SDHCI_INT_CARD_INT;
3506 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
3507 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003508 spin_unlock_irqrestore(&host->lock, flags);
3509
Russell King781e9892014-04-25 12:55:46 +01003510 synchronize_hardirq(host->irq);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003511
3512 spin_lock_irqsave(&host->lock, flags);
3513 host->runtime_suspended = true;
3514 spin_unlock_irqrestore(&host->lock, flags);
3515
Markus Pargmann8a125ba2014-06-04 15:24:29 +02003516 return 0;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003517}
3518EXPORT_SYMBOL_GPL(sdhci_runtime_suspend_host);
3519
3520int sdhci_runtime_resume_host(struct sdhci_host *host)
3521{
Adrian Hunterd3940f22016-06-29 16:24:14 +03003522 struct mmc_host *mmc = host->mmc;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003523 unsigned long flags;
Markus Pargmann8a125ba2014-06-04 15:24:29 +02003524 int host_flags = host->flags;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003525
3526 if (host_flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
3527 if (host->ops->enable_dma)
3528 host->ops->enable_dma(host);
3529 }
3530
3531 sdhci_init(host, 0);
3532
3533 /* Force clock and power re-program */
3534 host->pwr = 0;
3535 host->clock = 0;
Adrian Hunterd3940f22016-06-29 16:24:14 +03003536 mmc->ops->start_signal_voltage_switch(mmc, &mmc->ios);
3537 mmc->ops->set_ios(mmc, &mmc->ios);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003538
Kevin Liu52983382013-01-31 11:31:37 +08003539 if ((host_flags & SDHCI_PV_ENABLED) &&
3540 !(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN)) {
3541 spin_lock_irqsave(&host->lock, flags);
3542 sdhci_enable_preset_value(host, true);
3543 spin_unlock_irqrestore(&host->lock, flags);
3544 }
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003545
Adrian Hunter086b0dd2016-11-02 15:49:11 +02003546 if ((mmc->caps2 & MMC_CAP2_HS400_ES) &&
3547 mmc->ops->hs400_enhanced_strobe)
3548 mmc->ops->hs400_enhanced_strobe(mmc, &mmc->ios);
3549
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003550 spin_lock_irqsave(&host->lock, flags);
3551
3552 host->runtime_suspended = false;
3553
3554 /* Enable SDIO IRQ */
Russell Kingef104332014-04-25 12:55:41 +01003555 if (host->flags & SDHCI_SDIO_IRQ_ENABLED)
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003556 sdhci_enable_sdio_irq_nolock(host, true);
3557
3558 /* Enable Card Detection */
3559 sdhci_enable_card_detection(host);
3560
3561 spin_unlock_irqrestore(&host->lock, flags);
3562
Markus Pargmann8a125ba2014-06-04 15:24:29 +02003563 return 0;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003564}
3565EXPORT_SYMBOL_GPL(sdhci_runtime_resume_host);
3566
Rafael J. Wysocki162d6f92014-12-05 03:05:33 +01003567#endif /* CONFIG_PM */
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003568
Pierre Ossmand129bce2006-03-24 03:18:17 -08003569/*****************************************************************************\
3570 * *
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003571 * Device allocation/registration *
Pierre Ossmand129bce2006-03-24 03:18:17 -08003572 * *
3573\*****************************************************************************/
3574
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003575struct sdhci_host *sdhci_alloc_host(struct device *dev,
3576 size_t priv_size)
Pierre Ossmand129bce2006-03-24 03:18:17 -08003577{
Pierre Ossmand129bce2006-03-24 03:18:17 -08003578 struct mmc_host *mmc;
3579 struct sdhci_host *host;
3580
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003581 WARN_ON(dev == NULL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003582
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003583 mmc = mmc_alloc_host(sizeof(struct sdhci_host) + priv_size, dev);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003584 if (!mmc)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003585 return ERR_PTR(-ENOMEM);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003586
3587 host = mmc_priv(mmc);
3588 host->mmc = mmc;
Adrian Hunterbf60e592016-02-09 16:12:35 +02003589 host->mmc_host_ops = sdhci_ops;
3590 mmc->ops = &host->mmc_host_ops;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003591
Adrian Hunter8cb851a2016-06-29 16:24:16 +03003592 host->flags = SDHCI_SIGNALING_330;
3593
Sahitya Tummalaef4de6c2013-05-24 08:47:26 +05303594 spin_lock_init(&host->lock);
Sahitya Tummala4c196de2014-10-31 14:00:12 +05303595 ratelimit_state_init(&host->dbg_dump_rs, SDHCI_DBG_DUMP_RS_INTERVAL,
3596 SDHCI_DBG_DUMP_RS_BURST);
Sahitya Tummalaef4de6c2013-05-24 08:47:26 +05303597
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003598 return host;
3599}
Pierre Ossman8a4da142006-10-04 02:15:40 -07003600
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003601EXPORT_SYMBOL_GPL(sdhci_alloc_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003602
Asutosh Das3621b372014-10-17 16:36:47 +05303603#ifdef CONFIG_MMC_CQ_HCI
Sahitya Tummala738b74c2016-04-12 13:25:41 +05303604static void sdhci_cmdq_set_transfer_params(struct mmc_host *mmc)
3605{
3606 struct sdhci_host *host = mmc_priv(mmc);
3607 u8 ctrl;
3608
3609 if (host->version >= SDHCI_SPEC_200) {
3610 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
3611 ctrl &= ~SDHCI_CTRL_DMA_MASK;
Sahitya Tummala05709df2016-04-01 14:29:22 +05303612 if (host->flags & SDHCI_USE_64_BIT_DMA)
Sahitya Tummala738b74c2016-04-12 13:25:41 +05303613 ctrl |= SDHCI_CTRL_ADMA64;
3614 else
3615 ctrl |= SDHCI_CTRL_ADMA32;
3616 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
3617 }
Sahitya Tummala7e709302016-04-25 13:21:42 +05303618 if (host->ops->toggle_cdr)
3619 host->ops->toggle_cdr(host, false);
Sahitya Tummala738b74c2016-04-12 13:25:41 +05303620}
3621
Asutosh Das3621b372014-10-17 16:36:47 +05303622static void sdhci_cmdq_clear_set_irqs(struct mmc_host *mmc, bool clear)
3623{
3624 struct sdhci_host *host = mmc_priv(mmc);
3625 u32 ier = 0;
3626
3627 ier &= ~SDHCI_INT_ALL_MASK;
3628
3629 if (clear) {
3630 ier = SDHCI_INT_CMDQ_EN | SDHCI_INT_ERROR_MASK;
3631 sdhci_writel(host, ier, SDHCI_INT_ENABLE);
3632 sdhci_writel(host, ier, SDHCI_SIGNAL_ENABLE);
3633 } else {
3634 ier = SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT |
3635 SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT |
3636 SDHCI_INT_INDEX | SDHCI_INT_END_BIT |
3637 SDHCI_INT_CRC | SDHCI_INT_TIMEOUT |
3638 SDHCI_INT_DATA_END | SDHCI_INT_RESPONSE |
3639 SDHCI_INT_AUTO_CMD_ERR;
3640 sdhci_writel(host, ier, SDHCI_INT_ENABLE);
3641 sdhci_writel(host, ier, SDHCI_SIGNAL_ENABLE);
3642 }
3643}
3644
3645static void sdhci_cmdq_set_data_timeout(struct mmc_host *mmc, u32 val)
3646{
3647 struct sdhci_host *host = mmc_priv(mmc);
3648
3649 sdhci_writeb(host, val, SDHCI_TIMEOUT_CONTROL);
3650}
3651
3652static void sdhci_cmdq_dump_vendor_regs(struct mmc_host *mmc)
3653{
3654 struct sdhci_host *host = mmc_priv(mmc);
3655
3656 sdhci_dumpregs(host);
3657}
3658
3659static int sdhci_cmdq_init(struct sdhci_host *host, struct mmc_host *mmc,
3660 bool dma64)
3661{
3662 return cmdq_init(host->cq_host, mmc, dma64);
3663}
3664
3665static void sdhci_cmdq_set_block_size(struct mmc_host *mmc)
3666{
3667 struct sdhci_host *host = mmc_priv(mmc);
3668
3669 sdhci_set_blk_size_reg(host, 512, 0);
3670}
3671
Ritesh Harjani1f6d7622015-07-15 13:31:06 +05303672static void sdhci_enhanced_strobe_mask(struct mmc_host *mmc, bool set)
3673{
3674 struct sdhci_host *host = mmc_priv(mmc);
3675
3676 if (host->ops->enhanced_strobe_mask)
3677 host->ops->enhanced_strobe_mask(host, set);
3678}
3679
Asutosh Das3621b372014-10-17 16:36:47 +05303680static void sdhci_cmdq_clear_set_dumpregs(struct mmc_host *mmc, bool set)
3681{
3682 struct sdhci_host *host = mmc_priv(mmc);
3683
3684 if (host->ops->clear_set_dumpregs)
3685 host->ops->clear_set_dumpregs(host, set);
3686}
Konstantin Dorfman26a6d9b2015-05-31 10:08:29 +03003687
3688static void sdhci_cmdq_post_cqe_halt(struct mmc_host *mmc)
3689{
3690 struct sdhci_host *host = mmc_priv(mmc);
3691
3692 sdhci_writel(host, sdhci_readl(host, SDHCI_INT_ENABLE) |
3693 SDHCI_INT_RESPONSE, SDHCI_INT_ENABLE);
3694 sdhci_writel(host, SDHCI_INT_RESPONSE, SDHCI_INT_STATUS);
3695}
Asutosh Das3621b372014-10-17 16:36:47 +05303696#else
Sahitya Tummala738b74c2016-04-12 13:25:41 +05303697static void sdhci_cmdq_set_transfer_params(struct mmc_host *mmc)
3698{
3699
3700}
Asutosh Das3621b372014-10-17 16:36:47 +05303701static void sdhci_cmdq_clear_set_irqs(struct mmc_host *mmc, bool clear)
3702{
3703
3704}
3705
3706static void sdhci_cmdq_set_data_timeout(struct mmc_host *mmc, u32 val)
3707{
3708
3709}
3710
3711static void sdhci_cmdq_dump_vendor_regs(struct mmc_host *mmc)
3712{
3713
3714}
3715
3716static int sdhci_cmdq_init(struct sdhci_host *host, struct mmc_host *mmc,
3717 bool dma64)
3718{
3719 return -ENOSYS;
3720}
3721
3722static void sdhci_cmdq_set_block_size(struct mmc_host *mmc)
3723{
3724
3725}
3726
Ritesh Harjani1f6d7622015-07-15 13:31:06 +05303727static void sdhci_enhanced_strobe_mask(struct mmc_host *mmc, bool set)
3728{
3729
3730}
3731
Asutosh Das3621b372014-10-17 16:36:47 +05303732static void sdhci_cmdq_clear_set_dumpregs(struct mmc_host *mmc, bool set)
3733{
3734
3735}
3736
Konstantin Dorfman26a6d9b2015-05-31 10:08:29 +03003737static void sdhci_cmdq_post_cqe_halt(struct mmc_host *mmc)
3738{
3739}
Asutosh Das3621b372014-10-17 16:36:47 +05303740#endif
3741
3742static const struct cmdq_host_ops sdhci_cmdq_ops = {
3743 .clear_set_irqs = sdhci_cmdq_clear_set_irqs,
3744 .set_data_timeout = sdhci_cmdq_set_data_timeout,
3745 .dump_vendor_regs = sdhci_cmdq_dump_vendor_regs,
3746 .set_block_size = sdhci_cmdq_set_block_size,
3747 .clear_set_dumpregs = sdhci_cmdq_clear_set_dumpregs,
Ritesh Harjani1f6d7622015-07-15 13:31:06 +05303748 .enhanced_strobe_mask = sdhci_enhanced_strobe_mask,
Konstantin Dorfman26a6d9b2015-05-31 10:08:29 +03003749 .post_cqe_halt = sdhci_cmdq_post_cqe_halt,
Sahitya Tummala738b74c2016-04-12 13:25:41 +05303750 .set_transfer_params = sdhci_cmdq_set_transfer_params,
Asutosh Das3621b372014-10-17 16:36:47 +05303751};
3752
Subhash Jadavani7a9b0072016-05-11 16:47:20 -07003753#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
3754static int sdhci_is_adma2_64bit(struct sdhci_host *host)
3755{
3756 u32 caps;
3757
3758 caps = (host->quirks & SDHCI_QUIRK_MISSING_CAPS) ? host->caps :
3759 sdhci_readl(host, SDHCI_CAPABILITIES);
3760
3761 if (caps & SDHCI_CAN_64BIT)
3762 return 1;
3763 return 0;
3764}
3765#else
3766static int sdhci_is_adma2_64bit(struct sdhci_host *host)
3767{
3768 return 0;
3769}
3770#endif
3771
Alexandre Courbot7b913692016-03-07 11:07:55 +09003772static int sdhci_set_dma_mask(struct sdhci_host *host)
3773{
3774 struct mmc_host *mmc = host->mmc;
3775 struct device *dev = mmc_dev(mmc);
3776 int ret = -EINVAL;
3777
3778 if (host->quirks2 & SDHCI_QUIRK2_BROKEN_64_BIT_DMA)
3779 host->flags &= ~SDHCI_USE_64_BIT_DMA;
3780
3781 /* Try 64-bit mask if hardware is capable of it */
3782 if (host->flags & SDHCI_USE_64_BIT_DMA) {
3783 ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64));
3784 if (ret) {
3785 pr_warn("%s: Failed to set 64-bit DMA mask.\n",
3786 mmc_hostname(mmc));
3787 host->flags &= ~SDHCI_USE_64_BIT_DMA;
3788 }
3789 }
3790
3791 /* 32-bit mask as default & fallback */
3792 if (ret) {
3793 ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
3794 if (ret)
3795 pr_warn("%s: Failed to set 32-bit DMA mask.\n",
3796 mmc_hostname(mmc));
3797 }
3798
3799 return ret;
3800}
3801
Adrian Hunter6132a3b2016-06-29 16:24:18 +03003802void __sdhci_read_caps(struct sdhci_host *host, u16 *ver, u32 *caps, u32 *caps1)
3803{
3804 u16 v;
3805
3806 if (host->read_caps)
3807 return;
3808
3809 host->read_caps = true;
3810
3811 if (debug_quirks)
3812 host->quirks = debug_quirks;
3813
3814 if (debug_quirks2)
3815 host->quirks2 = debug_quirks2;
3816
3817 sdhci_do_reset(host, SDHCI_RESET_ALL);
3818
3819 v = ver ? *ver : sdhci_readw(host, SDHCI_HOST_VERSION);
3820 host->version = (v & SDHCI_SPEC_VER_MASK) >> SDHCI_SPEC_VER_SHIFT;
3821
3822 if (host->quirks & SDHCI_QUIRK_MISSING_CAPS)
3823 return;
3824
3825 host->caps = caps ? *caps : sdhci_readl(host, SDHCI_CAPABILITIES);
3826
3827 if (host->version < SDHCI_SPEC_300)
3828 return;
3829
3830 host->caps1 = caps1 ? *caps1 : sdhci_readl(host, SDHCI_CAPABILITIES_1);
3831}
3832EXPORT_SYMBOL_GPL(__sdhci_read_caps);
3833
Adrian Hunter52f53362016-06-29 16:24:15 +03003834int sdhci_setup_host(struct sdhci_host *host)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003835{
3836 struct mmc_host *mmc;
Ritesh Harjanice4b8f82015-11-25 10:37:21 +05303837 u32 caps[2] = {0, 0};
Arindam Nathf2119df2011-05-05 12:18:57 +05303838 u32 max_current_caps;
3839 unsigned int ocr_avail;
Adrian Hunterf5fa92e2014-09-24 10:27:32 +03003840 unsigned int override_timeout_clk;
Dong Aisheng59241752015-07-22 20:53:07 +08003841 u32 max_clk;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003842 int ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003843
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003844 WARN_ON(host == NULL);
3845 if (host == NULL)
3846 return -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003847
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003848 mmc = host->mmc;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003849
Jon Hunterefba1422016-07-12 14:53:36 +01003850 /*
3851 * If there are external regulators, get them. Note this must be done
3852 * early before resetting the host and reading the capabilities so that
3853 * the host can take the appropriate action if regulators are not
3854 * available.
3855 */
3856 ret = mmc_regulator_get_supply(mmc);
3857 if (ret == -EPROBE_DEFER)
3858 return ret;
3859
Adrian Hunter6132a3b2016-06-29 16:24:18 +03003860 sdhci_read_caps(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003861
Ritesh Harjanice4b8f82015-11-25 10:37:21 +05303862 caps[0] = host->caps;
3863
Adrian Hunterf5fa92e2014-09-24 10:27:32 +03003864 override_timeout_clk = host->timeout_clk;
3865
Zhangfei Gao85105c52010-08-06 07:10:01 +08003866 if (host->version > SDHCI_SPEC_300) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01003867 pr_err("%s: Unknown controller version (%d). You may experience problems.\n",
3868 mmc_hostname(mmc), host->version);
Pierre Ossman4a965502006-06-30 02:22:29 -07003869 }
3870
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003871 if (host->quirks & SDHCI_QUIRK_FORCE_DMA)
Richard Röjforsa13abc72009-09-22 16:45:30 -07003872 host->flags |= SDHCI_USE_SDMA;
Adrian Hunter28da3582016-06-29 16:24:17 +03003873 else if (!(host->caps & SDHCI_CAN_DO_SDMA))
Richard Röjforsa13abc72009-09-22 16:45:30 -07003874 DBG("Controller doesn't have SDMA capability\n");
Pierre Ossman67435272006-06-30 02:22:31 -07003875 else
Richard Röjforsa13abc72009-09-22 16:45:30 -07003876 host->flags |= SDHCI_USE_SDMA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003877
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003878 if ((host->quirks & SDHCI_QUIRK_BROKEN_DMA) &&
Richard Röjforsa13abc72009-09-22 16:45:30 -07003879 (host->flags & SDHCI_USE_SDMA)) {
Rolf Eike Beercee687c2007-11-02 15:22:30 +01003880 DBG("Disabling DMA as it is marked broken\n");
Richard Röjforsa13abc72009-09-22 16:45:30 -07003881 host->flags &= ~SDHCI_USE_SDMA;
Feng Tang7c168e32007-09-30 12:44:18 +02003882 }
3883
Arindam Nathf2119df2011-05-05 12:18:57 +05303884 if ((host->version >= SDHCI_SPEC_200) &&
Adrian Hunter28da3582016-06-29 16:24:17 +03003885 (host->caps & SDHCI_CAN_DO_ADMA2))
Richard Röjforsa13abc72009-09-22 16:45:30 -07003886 host->flags |= SDHCI_USE_ADMA;
Pierre Ossman2134a922008-06-28 18:28:51 +02003887
3888 if ((host->quirks & SDHCI_QUIRK_BROKEN_ADMA) &&
3889 (host->flags & SDHCI_USE_ADMA)) {
3890 DBG("Disabling ADMA as it is marked broken\n");
3891 host->flags &= ~SDHCI_USE_ADMA;
3892 }
3893
Adrian Huntere57a5f62014-11-04 12:42:46 +02003894 /*
3895 * It is assumed that a 64-bit capable device has set a 64-bit DMA mask
3896 * and *must* do 64-bit DMA. A driver has the opportunity to change
3897 * that during the first call to ->enable_dma(). Similarly
3898 * SDHCI_QUIRK2_BROKEN_64_BIT_DMA must be left to the drivers to
3899 * implement.
3900 */
Subhash Jadavani7a9b0072016-05-11 16:47:20 -07003901 if (sdhci_is_adma2_64bit(host))
Adrian Huntere57a5f62014-11-04 12:42:46 +02003902 host->flags |= SDHCI_USE_64_BIT_DMA;
3903
Richard Röjforsa13abc72009-09-22 16:45:30 -07003904 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Alexandre Courbot7b913692016-03-07 11:07:55 +09003905 ret = sdhci_set_dma_mask(host);
3906
3907 if (!ret && host->ops->enable_dma)
3908 ret = host->ops->enable_dma(host);
3909
3910 if (ret) {
3911 pr_warn("%s: No suitable DMA available - falling back to PIO\n",
3912 mmc_hostname(mmc));
3913 host->flags &= ~(SDHCI_USE_SDMA | SDHCI_USE_ADMA);
3914
3915 ret = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003916 }
3917 }
3918
Adrian Huntere57a5f62014-11-04 12:42:46 +02003919 /* SDMA does not support 64-bit DMA */
3920 if (host->flags & SDHCI_USE_64_BIT_DMA)
3921 host->flags &= ~SDHCI_USE_SDMA;
3922
Pierre Ossman2134a922008-06-28 18:28:51 +02003923 if (host->flags & SDHCI_USE_ADMA) {
Russell Kinge66e61c2016-01-26 13:39:55 +00003924 dma_addr_t dma;
3925 void *buf;
3926
Pierre Ossman2134a922008-06-28 18:28:51 +02003927 /*
Adrian Hunter76fe3792014-11-04 12:42:42 +02003928 * The DMA descriptor table size is calculated as the maximum
3929 * number of segments times 2, to allow for an alignment
3930 * descriptor for each segment, plus 1 for a nop end descriptor,
3931 * all multipled by the descriptor size.
Pierre Ossman2134a922008-06-28 18:28:51 +02003932 */
Adrian Huntere57a5f62014-11-04 12:42:46 +02003933 if (host->flags & SDHCI_USE_64_BIT_DMA) {
3934 host->adma_table_sz = (SDHCI_MAX_SEGS * 2 + 1) *
3935 SDHCI_ADMA2_64_DESC_SZ;
Adrian Huntere57a5f62014-11-04 12:42:46 +02003936 host->desc_sz = SDHCI_ADMA2_64_DESC_SZ;
Adrian Huntere57a5f62014-11-04 12:42:46 +02003937 } else {
3938 host->adma_table_sz = (SDHCI_MAX_SEGS * 2 + 1) *
3939 SDHCI_ADMA2_32_DESC_SZ;
Adrian Huntere57a5f62014-11-04 12:42:46 +02003940 host->desc_sz = SDHCI_ADMA2_32_DESC_SZ;
Adrian Huntere57a5f62014-11-04 12:42:46 +02003941 }
Russell Kinge66e61c2016-01-26 13:39:55 +00003942
Adrian Hunter04a5ae62015-11-26 14:00:49 +02003943 host->align_buffer_sz = SDHCI_MAX_SEGS * SDHCI_ADMA2_ALIGN;
Russell Kinge66e61c2016-01-26 13:39:55 +00003944 buf = dma_alloc_coherent(mmc_dev(mmc), host->align_buffer_sz +
3945 host->adma_table_sz, &dma, GFP_KERNEL);
3946 if (!buf) {
Joe Perches66061102014-09-12 14:56:56 -07003947 pr_warn("%s: Unable to allocate ADMA buffers - falling back to standard DMA\n",
Pierre Ossman2134a922008-06-28 18:28:51 +02003948 mmc_hostname(mmc));
3949 host->flags &= ~SDHCI_USE_ADMA;
Russell Kinge66e61c2016-01-26 13:39:55 +00003950 } else if ((dma + host->align_buffer_sz) &
3951 (SDHCI_ADMA2_DESC_ALIGN - 1)) {
Joe Perches66061102014-09-12 14:56:56 -07003952 pr_warn("%s: unable to allocate aligned ADMA descriptor\n",
3953 mmc_hostname(mmc));
Russell Kingd1e49f72014-04-25 12:58:34 +01003954 host->flags &= ~SDHCI_USE_ADMA;
Russell Kinge66e61c2016-01-26 13:39:55 +00003955 dma_free_coherent(mmc_dev(mmc), host->align_buffer_sz +
3956 host->adma_table_sz, buf, dma);
3957 } else {
3958 host->align_buffer = buf;
3959 host->align_addr = dma;
Russell Kingedd63fc2016-01-26 13:39:50 +00003960
Russell Kinge66e61c2016-01-26 13:39:55 +00003961 host->adma_table = buf + host->align_buffer_sz;
3962 host->adma_addr = dma + host->align_buffer_sz;
3963 }
Pierre Ossman2134a922008-06-28 18:28:51 +02003964 }
3965
Pierre Ossman76591502008-07-21 00:32:11 +02003966 /*
3967 * If we use DMA, then it's up to the caller to set the DMA
3968 * mask, but PIO does not need the hw shim so we set a new
3969 * mask here in that case.
3970 */
Richard Röjforsa13abc72009-09-22 16:45:30 -07003971 if (!(host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))) {
Pierre Ossman76591502008-07-21 00:32:11 +02003972 host->dma_mask = DMA_BIT_MASK(64);
Markus Mayer4e743f12014-07-03 13:27:42 -07003973 mmc_dev(mmc)->dma_mask = &host->dma_mask;
Pierre Ossman76591502008-07-21 00:32:11 +02003974 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003975
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04003976 if (host->version >= SDHCI_SPEC_300)
Adrian Hunter28da3582016-06-29 16:24:17 +03003977 host->max_clk = (host->caps & SDHCI_CLOCK_V3_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04003978 >> SDHCI_CLOCK_BASE_SHIFT;
3979 else
Adrian Hunter28da3582016-06-29 16:24:17 +03003980 host->max_clk = (host->caps & SDHCI_CLOCK_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04003981 >> SDHCI_CLOCK_BASE_SHIFT;
3982
Pierre Ossmand129bce2006-03-24 03:18:17 -08003983 host->max_clk *= 1000000;
Anton Vorontsovf27f47e2010-05-26 14:41:53 -07003984 if (host->max_clk == 0 || host->quirks &
3985 SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN) {
Ben Dooks4240ff02009-03-17 00:13:57 +03003986 if (!host->ops->get_max_clock) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01003987 pr_err("%s: Hardware doesn't specify base clock frequency.\n",
3988 mmc_hostname(mmc));
Adrian Huntereb5c20d2016-04-12 14:25:08 +03003989 ret = -ENODEV;
3990 goto undma;
Ben Dooks4240ff02009-03-17 00:13:57 +03003991 }
3992 host->max_clk = host->ops->get_max_clock(host);
3993 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003994
3995 /*
Arindam Nathc3ed3872011-05-05 12:19:06 +05303996 * In case of Host Controller v3.00, find out whether clock
3997 * multiplier is supported.
3998 */
Adrian Hunter28da3582016-06-29 16:24:17 +03003999 host->clk_mul = (host->caps1 & SDHCI_CLOCK_MUL_MASK) >>
Arindam Nathc3ed3872011-05-05 12:19:06 +05304000 SDHCI_CLOCK_MUL_SHIFT;
4001
4002 /*
4003 * In case the value in Clock Multiplier is 0, then programmable
4004 * clock mode is not supported, otherwise the actual clock
4005 * multiplier is one more than the value of Clock Multiplier
4006 * in the Capabilities Register.
4007 */
4008 if (host->clk_mul)
4009 host->clk_mul += 1;
4010
4011 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08004012 * Set host parameters.
4013 */
Dong Aisheng59241752015-07-22 20:53:07 +08004014 max_clk = host->max_clk;
4015
Marek Szyprowskice5f0362010-08-10 18:01:56 -07004016 if (host->ops->get_min_clock)
Anton Vorontsova9e58f22009-07-29 15:04:16 -07004017 mmc->f_min = host->ops->get_min_clock(host);
Arindam Nathc3ed3872011-05-05 12:19:06 +05304018 else if (host->version >= SDHCI_SPEC_300) {
4019 if (host->clk_mul) {
4020 mmc->f_min = (host->max_clk * host->clk_mul) / 1024;
Dong Aisheng59241752015-07-22 20:53:07 +08004021 max_clk = host->max_clk * host->clk_mul;
Arindam Nathc3ed3872011-05-05 12:19:06 +05304022 } else
4023 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_300;
4024 } else
Zhangfei Gao03975262010-09-20 15:15:18 -04004025 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_200;
Philip Rakity15ec4462010-11-19 16:48:39 -05004026
Adrian Hunterd310ae42016-04-12 14:25:07 +03004027 if (!mmc->f_max || mmc->f_max > max_clk)
Dong Aisheng59241752015-07-22 20:53:07 +08004028 mmc->f_max = max_clk;
4029
Aisheng Dong28aab052014-08-27 15:26:31 +08004030 if (!(host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)) {
Adrian Hunter28da3582016-06-29 16:24:17 +03004031 host->timeout_clk = (host->caps & SDHCI_TIMEOUT_CLK_MASK) >>
Aisheng Dong28aab052014-08-27 15:26:31 +08004032 SDHCI_TIMEOUT_CLK_SHIFT;
4033 if (host->timeout_clk == 0) {
4034 if (host->ops->get_timeout_clock) {
4035 host->timeout_clk =
4036 host->ops->get_timeout_clock(host);
4037 } else {
4038 pr_err("%s: Hardware doesn't specify timeout clock frequency.\n",
4039 mmc_hostname(mmc));
Adrian Huntereb5c20d2016-04-12 14:25:08 +03004040 ret = -ENODEV;
4041 goto undma;
Aisheng Dong28aab052014-08-27 15:26:31 +08004042 }
Andy Shevchenko272308c2011-08-03 18:36:00 +03004043 }
Andy Shevchenko272308c2011-08-03 18:36:00 +03004044
Adrian Hunter28da3582016-06-29 16:24:17 +03004045 if (host->caps & SDHCI_TIMEOUT_CLK_UNIT)
Aisheng Dong28aab052014-08-27 15:26:31 +08004046 host->timeout_clk *= 1000;
Andy Shevchenko272308c2011-08-03 18:36:00 +03004047
Adrian Hunter99513622016-03-07 13:33:55 +02004048 if (override_timeout_clk)
4049 host->timeout_clk = override_timeout_clk;
4050
Aisheng Dong28aab052014-08-27 15:26:31 +08004051 mmc->max_busy_timeout = host->ops->get_max_timeout_count ?
Aisheng Donga6ff5ae2014-08-27 15:26:27 +08004052 host->ops->get_max_timeout_count(host) : 1 << 27;
Aisheng Dong28aab052014-08-27 15:26:31 +08004053 mmc->max_busy_timeout /= host->timeout_clk;
4054 }
Adrian Hunter58d12462011-06-28 17:16:03 +03004055
Andrei Warkentine89d4562011-05-23 15:06:37 -05004056 mmc->caps |= MMC_CAP_SDIO_IRQ | MMC_CAP_ERASE | MMC_CAP_CMD23;
Russell King781e9892014-04-25 12:55:46 +01004057 mmc->caps2 |= MMC_CAP2_SDIO_IRQ_NOTHREAD;
Andrei Warkentine89d4562011-05-23 15:06:37 -05004058
Ritesh Harjanice4b8f82015-11-25 10:37:21 +05304059 if (caps[0] & SDHCI_CAN_ASYNC_INT)
4060 mmc->caps2 |= MMC_CAP2_ASYNC_SDIO_IRQ_4BIT_MODE;
4061
Andrei Warkentine89d4562011-05-23 15:06:37 -05004062 if (host->quirks & SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12)
4063 host->flags |= SDHCI_AUTO_CMD12;
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04004064
Andrei Warkentin8edf63712011-05-23 15:06:39 -05004065 /* Auto-CMD23 stuff only works in ADMA or PIO. */
Andrei Warkentin4f3d3e92011-05-25 10:42:50 -04004066 if ((host->version >= SDHCI_SPEC_300) &&
Andrei Warkentin8edf63712011-05-23 15:06:39 -05004067 ((host->flags & SDHCI_USE_ADMA) ||
Scott Branden3bfa6f02015-02-09 16:06:28 -08004068 !(host->flags & SDHCI_USE_SDMA)) &&
4069 !(host->quirks2 & SDHCI_QUIRK2_ACMD23_BROKEN)) {
Andrei Warkentin8edf63712011-05-23 15:06:39 -05004070 host->flags |= SDHCI_AUTO_CMD23;
4071 DBG("%s: Auto-CMD23 available\n", mmc_hostname(mmc));
4072 } else {
4073 DBG("%s: Auto-CMD23 unavailable\n", mmc_hostname(mmc));
4074 }
4075
Philip Rakity15ec4462010-11-19 16:48:39 -05004076 /*
4077 * A controller may support 8-bit width, but the board itself
4078 * might not have the pins brought out. Boards that support
4079 * 8-bit width must set "mmc->caps |= MMC_CAP_8_BIT_DATA;" in
4080 * their platform code before calling sdhci_add_host(), and we
4081 * won't assume 8-bit width for hosts without that CAP.
4082 */
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04004083 if (!(host->quirks & SDHCI_QUIRK_FORCE_1_BIT_DATA))
Philip Rakity15ec4462010-11-19 16:48:39 -05004084 mmc->caps |= MMC_CAP_4_BIT_DATA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08004085
Jerry Huang63ef5d82012-10-25 13:47:19 +08004086 if (host->quirks2 & SDHCI_QUIRK2_HOST_NO_CMD23)
4087 mmc->caps &= ~MMC_CAP_CMD23;
4088
Adrian Hunter28da3582016-06-29 16:24:17 +03004089 if (host->caps & SDHCI_CAN_DO_HISPD)
Zhangfei Gaoa29e7e12010-08-16 21:15:32 -04004090 mmc->caps |= MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED;
Pierre Ossmancd9277c2007-02-18 12:07:47 +01004091
Jaehoon Chung176d1ed2010-09-27 09:42:20 +01004092 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) &&
Jaehoon Chung860951c2016-06-21 10:13:26 +09004093 mmc_card_is_removable(mmc) &&
Guoping Yue3b17cf2014-08-20 16:42:55 +08004094 mmc_gpio_get_cd(host->mmc) < 0 &&
4095 !(mmc->caps2 & MMC_CAP2_NONHOTPLUG))
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03004096 mmc->caps |= MMC_CAP_NEEDS_POLL;
4097
Philip Rakity6231f3d2012-07-23 15:56:23 -07004098 /* If vqmmc regulator and no 1.8V signalling, then there's no UHS */
Tim Kryger3a48edc2014-06-13 10:13:56 -07004099 if (!IS_ERR(mmc->supply.vqmmc)) {
4100 ret = regulator_enable(mmc->supply.vqmmc);
4101 if (!regulator_is_supported_voltage(mmc->supply.vqmmc, 1700000,
4102 1950000))
Adrian Hunter28da3582016-06-29 16:24:17 +03004103 host->caps1 &= ~(SDHCI_SUPPORT_SDR104 |
4104 SDHCI_SUPPORT_SDR50 |
4105 SDHCI_SUPPORT_DDR50);
Chris Balla3361ab2013-03-11 17:51:53 -04004106 if (ret) {
4107 pr_warn("%s: Failed to enable vqmmc regulator: %d\n",
4108 mmc_hostname(mmc), ret);
Adrian Hunter4bb74312014-11-06 15:19:04 +02004109 mmc->supply.vqmmc = ERR_PTR(-EINVAL);
Chris Balla3361ab2013-03-11 17:51:53 -04004110 }
Kevin Liu8363c372012-11-17 17:55:51 -05004111 }
Philip Rakity6231f3d2012-07-23 15:56:23 -07004112
Adrian Hunter28da3582016-06-29 16:24:17 +03004113 if (host->quirks2 & SDHCI_QUIRK2_NO_1_8_V) {
4114 host->caps1 &= ~(SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 |
4115 SDHCI_SUPPORT_DDR50);
4116 }
Daniel Drake6a661802012-11-25 13:01:19 -05004117
Al Cooper4188bba2012-03-16 15:54:17 -04004118 /* Any UHS-I mode in caps implies SDR12 and SDR25 support. */
Adrian Hunter28da3582016-06-29 16:24:17 +03004119 if (host->caps1 & (SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 |
4120 SDHCI_SUPPORT_DDR50))
Arindam Nathf2119df2011-05-05 12:18:57 +05304121 mmc->caps |= MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25;
4122
4123 /* SDR104 supports also implies SDR50 support */
Adrian Hunter28da3582016-06-29 16:24:17 +03004124 if (host->caps1 & SDHCI_SUPPORT_SDR104) {
Arindam Nathf2119df2011-05-05 12:18:57 +05304125 mmc->caps |= MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_SDR50;
Giuseppe CAVALLARO156e14b2013-06-12 08:16:38 +02004126 /* SD3.0: SDR104 is supported so (for eMMC) the caps2
4127 * field can be promoted to support HS200.
4128 */
Adrian Hunter549c0b12014-11-06 15:19:05 +02004129 if (!(host->quirks2 & SDHCI_QUIRK2_BROKEN_HS200))
David Cohen13868bf2013-10-29 10:58:26 -07004130 mmc->caps2 |= MMC_CAP2_HS200;
Adrian Hunter28da3582016-06-29 16:24:17 +03004131 } else if (host->caps1 & SDHCI_SUPPORT_SDR50) {
Arindam Nathf2119df2011-05-05 12:18:57 +05304132 mmc->caps |= MMC_CAP_UHS_SDR50;
Adrian Hunter28da3582016-06-29 16:24:17 +03004133 }
Arindam Nathf2119df2011-05-05 12:18:57 +05304134
Adrian Huntere9fb05d2014-11-06 15:19:06 +02004135 if (host->quirks2 & SDHCI_QUIRK2_CAPS_BIT63_FOR_HS400 &&
Adrian Hunter28da3582016-06-29 16:24:17 +03004136 (host->caps1 & SDHCI_SUPPORT_HS400))
Adrian Huntere9fb05d2014-11-06 15:19:06 +02004137 mmc->caps2 |= MMC_CAP2_HS400;
4138
Adrian Hunter549c0b12014-11-06 15:19:05 +02004139 if ((mmc->caps2 & MMC_CAP2_HSX00_1_2V) &&
4140 (IS_ERR(mmc->supply.vqmmc) ||
4141 !regulator_is_supported_voltage(mmc->supply.vqmmc, 1100000,
4142 1300000)))
4143 mmc->caps2 &= ~MMC_CAP2_HSX00_1_2V;
4144
Adrian Hunter28da3582016-06-29 16:24:17 +03004145 if ((host->caps1 & SDHCI_SUPPORT_DDR50) &&
4146 !(host->quirks2 & SDHCI_QUIRK2_BROKEN_DDR50))
Arindam Nathf2119df2011-05-05 12:18:57 +05304147 mmc->caps |= MMC_CAP_UHS_DDR50;
4148
Girish K S069c9f12012-01-06 09:56:39 +05304149 /* Does the host need tuning for SDR50? */
Adrian Hunter28da3582016-06-29 16:24:17 +03004150 if (host->caps1 & SDHCI_USE_SDR50_TUNING)
Arindam Nathb513ea22011-05-05 12:19:04 +05304151 host->flags |= SDHCI_SDR50_NEEDS_TUNING;
4152
Arindam Nathd6d50a12011-05-05 12:18:59 +05304153 /* Driver Type(s) (A, C, D) supported by the host */
Adrian Hunter28da3582016-06-29 16:24:17 +03004154 if (host->caps1 & SDHCI_DRIVER_TYPE_A)
Arindam Nathd6d50a12011-05-05 12:18:59 +05304155 mmc->caps |= MMC_CAP_DRIVER_TYPE_A;
Adrian Hunter28da3582016-06-29 16:24:17 +03004156 if (host->caps1 & SDHCI_DRIVER_TYPE_C)
Arindam Nathd6d50a12011-05-05 12:18:59 +05304157 mmc->caps |= MMC_CAP_DRIVER_TYPE_C;
Adrian Hunter28da3582016-06-29 16:24:17 +03004158 if (host->caps1 & SDHCI_DRIVER_TYPE_D)
Arindam Nathd6d50a12011-05-05 12:18:59 +05304159 mmc->caps |= MMC_CAP_DRIVER_TYPE_D;
4160
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05304161 /* Initial value for re-tuning timer count */
Adrian Hunter28da3582016-06-29 16:24:17 +03004162 host->tuning_count = (host->caps1 & SDHCI_RETUNING_TIMER_COUNT_MASK) >>
4163 SDHCI_RETUNING_TIMER_COUNT_SHIFT;
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05304164
4165 /*
4166 * In case Re-tuning Timer is not disabled, the actual value of
4167 * re-tuning timer will be 2 ^ (n - 1).
4168 */
4169 if (host->tuning_count)
4170 host->tuning_count = 1 << (host->tuning_count - 1);
4171
4172 /* Re-tuning mode supported by the Host Controller */
Adrian Hunter28da3582016-06-29 16:24:17 +03004173 host->tuning_mode = (host->caps1 & SDHCI_RETUNING_MODE_MASK) >>
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05304174 SDHCI_RETUNING_MODE_SHIFT;
4175
Takashi Iwai8f230f42010-12-08 10:04:30 +01004176 ocr_avail = 0;
Philip Rakitybad37e12012-05-27 18:36:44 -07004177
Arindam Nathf2119df2011-05-05 12:18:57 +05304178 /*
4179 * According to SD Host Controller spec v3.00, if the Host System
4180 * can afford more than 150mA, Host Driver should set XPC to 1. Also
4181 * the value is meaningful only if Voltage Support in the Capabilities
4182 * register is set. The actual current value is 4 times the register
4183 * value.
4184 */
4185 max_current_caps = sdhci_readl(host, SDHCI_MAX_CURRENT);
Sahitya Tummalacba7e832014-09-19 15:43:37 +05304186 if (!max_current_caps) {
4187 u32 curr = 0;
Philip Rakitybad37e12012-05-27 18:36:44 -07004188
Sahitya Tummalacba7e832014-09-19 15:43:37 +05304189 if (!IS_ERR(mmc->supply.vmmc))
4190 curr = regulator_get_current_limit(mmc->supply.vmmc);
4191 else if (host->ops->get_current_limit)
4192 curr = host->ops->get_current_limit(host);
4193
4194 if (curr > 0) {
Philip Rakitybad37e12012-05-27 18:36:44 -07004195 /* convert to SDHCI_MAX_CURRENT format */
4196 curr = curr/1000; /* convert to mA */
4197 curr = curr/SDHCI_MAX_CURRENT_MULTIPLIER;
4198
4199 curr = min_t(u32, curr, SDHCI_MAX_CURRENT_LIMIT);
4200 max_current_caps =
4201 (curr << SDHCI_MAX_CURRENT_330_SHIFT) |
4202 (curr << SDHCI_MAX_CURRENT_300_SHIFT) |
4203 (curr << SDHCI_MAX_CURRENT_180_SHIFT);
4204 }
4205 }
Arindam Nathf2119df2011-05-05 12:18:57 +05304206
Adrian Hunter28da3582016-06-29 16:24:17 +03004207 if (host->caps & SDHCI_CAN_VDD_330) {
Takashi Iwai8f230f42010-12-08 10:04:30 +01004208 ocr_avail |= MMC_VDD_32_33 | MMC_VDD_33_34;
Arindam Nathf2119df2011-05-05 12:18:57 +05304209
Aaron Lu55c46652012-07-04 13:31:48 +08004210 mmc->max_current_330 = ((max_current_caps &
Arindam Nathf2119df2011-05-05 12:18:57 +05304211 SDHCI_MAX_CURRENT_330_MASK) >>
4212 SDHCI_MAX_CURRENT_330_SHIFT) *
4213 SDHCI_MAX_CURRENT_MULTIPLIER;
Arindam Nathf2119df2011-05-05 12:18:57 +05304214 }
Adrian Hunter28da3582016-06-29 16:24:17 +03004215 if (host->caps & SDHCI_CAN_VDD_300) {
Takashi Iwai8f230f42010-12-08 10:04:30 +01004216 ocr_avail |= MMC_VDD_29_30 | MMC_VDD_30_31;
Arindam Nathf2119df2011-05-05 12:18:57 +05304217
Aaron Lu55c46652012-07-04 13:31:48 +08004218 mmc->max_current_300 = ((max_current_caps &
Arindam Nathf2119df2011-05-05 12:18:57 +05304219 SDHCI_MAX_CURRENT_300_MASK) >>
4220 SDHCI_MAX_CURRENT_300_SHIFT) *
4221 SDHCI_MAX_CURRENT_MULTIPLIER;
Arindam Nathf2119df2011-05-05 12:18:57 +05304222 }
Adrian Hunter28da3582016-06-29 16:24:17 +03004223 if (host->caps & SDHCI_CAN_VDD_180) {
Takashi Iwai8f230f42010-12-08 10:04:30 +01004224 ocr_avail |= MMC_VDD_165_195;
4225
Aaron Lu55c46652012-07-04 13:31:48 +08004226 mmc->max_current_180 = ((max_current_caps &
Arindam Nathf2119df2011-05-05 12:18:57 +05304227 SDHCI_MAX_CURRENT_180_MASK) >>
4228 SDHCI_MAX_CURRENT_180_SHIFT) *
4229 SDHCI_MAX_CURRENT_MULTIPLIER;
Arindam Nathf2119df2011-05-05 12:18:57 +05304230 }
4231
Ulf Hansson5fd26c72015-06-05 11:40:08 +02004232 /* If OCR set by host, use it instead. */
4233 if (host->ocr_mask)
4234 ocr_avail = host->ocr_mask;
4235
4236 /* If OCR set by external regulators, give it highest prio. */
Tim Kryger3a48edc2014-06-13 10:13:56 -07004237 if (mmc->ocr_avail)
Tim Kryger52221612014-06-25 00:25:34 -07004238 ocr_avail = mmc->ocr_avail;
Tim Kryger3a48edc2014-06-13 10:13:56 -07004239
Takashi Iwai8f230f42010-12-08 10:04:30 +01004240 mmc->ocr_avail = ocr_avail;
4241 mmc->ocr_avail_sdio = ocr_avail;
4242 if (host->ocr_avail_sdio)
4243 mmc->ocr_avail_sdio &= host->ocr_avail_sdio;
4244 mmc->ocr_avail_sd = ocr_avail;
4245 if (host->ocr_avail_sd)
4246 mmc->ocr_avail_sd &= host->ocr_avail_sd;
4247 else /* normal SD controllers don't support 1.8V */
4248 mmc->ocr_avail_sd &= ~MMC_VDD_165_195;
4249 mmc->ocr_avail_mmc = ocr_avail;
4250 if (host->ocr_avail_mmc)
4251 mmc->ocr_avail_mmc &= host->ocr_avail_mmc;
Pierre Ossman146ad662006-06-30 02:22:23 -07004252
4253 if (mmc->ocr_avail == 0) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01004254 pr_err("%s: Hardware doesn't report any support voltages.\n",
4255 mmc_hostname(mmc));
Adrian Huntereb5c20d2016-04-12 14:25:08 +03004256 ret = -ENODEV;
4257 goto unreg;
Pierre Ossman146ad662006-06-30 02:22:23 -07004258 }
4259
Adrian Hunter8cb851a2016-06-29 16:24:16 +03004260 if ((mmc->caps & (MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 |
4261 MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR104 |
4262 MMC_CAP_UHS_DDR50 | MMC_CAP_1_8V_DDR)) ||
4263 (mmc->caps2 & (MMC_CAP2_HS200_1_8V_SDR | MMC_CAP2_HS400_1_8V)))
4264 host->flags |= SDHCI_SIGNALING_180;
4265
4266 if (mmc->caps2 & MMC_CAP2_HSX00_1_2V)
4267 host->flags |= SDHCI_SIGNALING_120;
4268
Pierre Ossmand129bce2006-03-24 03:18:17 -08004269 /*
Pierre Ossman2134a922008-06-28 18:28:51 +02004270 * Maximum number of segments. Depends on if the hardware
4271 * can do scatter/gather or not.
Pierre Ossmand129bce2006-03-24 03:18:17 -08004272 */
Pierre Ossman2134a922008-06-28 18:28:51 +02004273 if (host->flags & SDHCI_USE_ADMA)
Adrian Hunter4fb213f2014-11-04 12:42:43 +02004274 mmc->max_segs = SDHCI_MAX_SEGS;
Richard Röjforsa13abc72009-09-22 16:45:30 -07004275 else if (host->flags & SDHCI_USE_SDMA)
Martin K. Petersena36274e2010-09-10 01:33:59 -04004276 mmc->max_segs = 1;
Pierre Ossman2134a922008-06-28 18:28:51 +02004277 else /* PIO */
Adrian Hunter4fb213f2014-11-04 12:42:43 +02004278 mmc->max_segs = SDHCI_MAX_SEGS;
Pierre Ossmand129bce2006-03-24 03:18:17 -08004279
4280 /*
Adrian Hunterac005312014-12-05 19:25:28 +02004281 * Maximum number of sectors in one transfer. Limited by SDMA boundary
4282 * size (512KiB). Note some tuning modes impose a 4MiB limit, but this
4283 * is less anyway.
Pierre Ossmand129bce2006-03-24 03:18:17 -08004284 */
Pierre Ossman55db8902006-11-21 17:55:45 +01004285 mmc->max_req_size = 524288;
Pierre Ossmand129bce2006-03-24 03:18:17 -08004286
4287 /*
4288 * Maximum segment size. Could be one segment with the maximum number
Pierre Ossman2134a922008-06-28 18:28:51 +02004289 * of bytes. When doing hardware scatter/gather, each entry cannot
4290 * be larger than 64 KiB though.
Pierre Ossmand129bce2006-03-24 03:18:17 -08004291 */
Olof Johansson30652aa2011-01-01 18:37:32 -06004292 if (host->flags & SDHCI_USE_ADMA) {
4293 if (host->quirks & SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC)
4294 mmc->max_seg_size = 65535;
4295 else
4296 mmc->max_seg_size = 65536;
4297 } else {
Pierre Ossman2134a922008-06-28 18:28:51 +02004298 mmc->max_seg_size = mmc->max_req_size;
Olof Johansson30652aa2011-01-01 18:37:32 -06004299 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08004300
4301 /*
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01004302 * Maximum block size. This varies from controller to controller and
4303 * is specified in the capabilities register.
4304 */
Anton Vorontsov0633f652009-03-17 00:14:03 +03004305 if (host->quirks & SDHCI_QUIRK_FORCE_BLK_SZ_2048) {
4306 mmc->max_blk_size = 2;
4307 } else {
Adrian Hunter28da3582016-06-29 16:24:17 +03004308 mmc->max_blk_size = (host->caps & SDHCI_MAX_BLOCK_MASK) >>
Anton Vorontsov0633f652009-03-17 00:14:03 +03004309 SDHCI_MAX_BLOCK_SHIFT;
4310 if (mmc->max_blk_size >= 3) {
Joe Perches66061102014-09-12 14:56:56 -07004311 pr_warn("%s: Invalid maximum block size, assuming 512 bytes\n",
4312 mmc_hostname(mmc));
Anton Vorontsov0633f652009-03-17 00:14:03 +03004313 mmc->max_blk_size = 0;
4314 }
4315 }
4316
4317 mmc->max_blk_size = 512 << mmc->max_blk_size;
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01004318
4319 /*
Pierre Ossman55db8902006-11-21 17:55:45 +01004320 * Maximum block count.
4321 */
Ben Dooks1388eef2009-06-14 12:40:53 +01004322 mmc->max_blk_count = (host->quirks & SDHCI_QUIRK_NO_MULTIBLOCK) ? 1 : 65535;
Pierre Ossman55db8902006-11-21 17:55:45 +01004323
Adrian Hunter52f53362016-06-29 16:24:15 +03004324 return 0;
4325
4326unreg:
4327 if (!IS_ERR(mmc->supply.vqmmc))
4328 regulator_disable(mmc->supply.vqmmc);
4329undma:
4330 if (host->align_buffer)
4331 dma_free_coherent(mmc_dev(mmc), host->align_buffer_sz +
4332 host->adma_table_sz, host->align_buffer,
4333 host->align_addr);
4334 host->adma_table = NULL;
4335 host->align_buffer = NULL;
4336
4337 return ret;
4338}
4339EXPORT_SYMBOL_GPL(sdhci_setup_host);
4340
4341int __sdhci_add_host(struct sdhci_host *host)
4342{
4343 struct mmc_host *mmc = host->mmc;
4344 int ret;
4345
Pierre Ossman55db8902006-11-21 17:55:45 +01004346 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08004347 * Init tasklets.
4348 */
Pierre Ossmand129bce2006-03-24 03:18:17 -08004349 tasklet_init(&host->finish_tasklet,
4350 sdhci_tasklet_finish, (unsigned long)host);
4351
Al Viroe4cad1b2006-10-10 22:47:07 +01004352 setup_timer(&host->timer, sdhci_timeout_timer, (unsigned long)host);
Adrian Hunterd7422fb2016-06-29 16:24:33 +03004353 setup_timer(&host->data_timer, sdhci_timeout_data_timer,
4354 (unsigned long)host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08004355
Adrian Hunter250fb7b42014-12-05 19:41:10 +02004356 init_waitqueue_head(&host->buf_ready_int);
Arindam Nathb513ea22011-05-05 12:19:04 +05304357
Ritesh Harjanice4b8f82015-11-25 10:37:21 +05304358 host->flags |= SDHCI_HOST_IRQ_STATUS;
4359
Shawn Guo2af502c2013-07-05 14:38:55 +08004360 sdhci_init(host, 0);
4361
Russell King781e9892014-04-25 12:55:46 +01004362 ret = request_threaded_irq(host->irq, sdhci_irq, sdhci_thread_irq,
4363 IRQF_SHARED, mmc_hostname(mmc), host);
Mark Brown0fc81ee2012-07-02 14:26:15 +01004364 if (ret) {
4365 pr_err("%s: Failed to request IRQ %d: %d\n",
4366 mmc_hostname(mmc), host->irq, ret);
Pierre Ossman8ef1a142006-06-30 02:22:21 -07004367 goto untasklet;
Mark Brown0fc81ee2012-07-02 14:26:15 +01004368 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08004369
Pierre Ossmand129bce2006-03-24 03:18:17 -08004370#ifdef CONFIG_MMC_DEBUG
4371 sdhci_dumpregs(host);
4372#endif
4373
Pavan Anamula5b761502015-07-23 18:45:37 +05304374 if (!(host->quirks2 & SDHCI_QUIRK2_BROKEN_LED_CONTROL)) {
4375 ret = sdhci_led_register(host);
4376 if (ret) {
4377 pr_err("%s: Failed to register LED device: %d\n",
4378 mmc_hostname(mmc), ret);
4379 goto unirq;
4380 }
Mark Brown0fc81ee2012-07-02 14:26:15 +01004381 }
Pierre Ossman2f730fe2008-03-17 10:29:38 +01004382
Pierre Ossman5f25a662006-10-04 02:15:39 -07004383 mmiowb();
4384
Asutosh Das214b9662013-06-13 14:27:42 +05304385 if (host->quirks2 & SDHCI_QUIRK2_IGN_DATA_END_BIT_ERROR) {
4386 host->ier = (host->ier & ~SDHCI_INT_DATA_END_BIT);
4387 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
4388 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
4389 }
4390
Asutosh Das3621b372014-10-17 16:36:47 +05304391 if (mmc->caps2 & MMC_CAP2_CMD_QUEUE) {
Subhash Jadavani2147ba92016-05-11 16:35:47 -07004392 bool dma64 = (host->flags & SDHCI_USE_64_BIT_DMA) ?
Asutosh Das3621b372014-10-17 16:36:47 +05304393 true : false;
4394 ret = sdhci_cmdq_init(host, mmc, dma64);
4395 if (ret)
4396 pr_err("%s: CMDQ init: failed (%d)\n",
4397 mmc_hostname(host->mmc), ret);
4398 else
4399 host->cq_host->ops = &sdhci_cmdq_ops;
4400 }
4401
4402 pr_info("%s: SDHCI controller on %s [%s] using %s in %s mode\n",
4403 mmc_hostname(mmc), host->hw_name, dev_name(mmc_dev(mmc)),
Adrian Huntere57a5f62014-11-04 12:42:46 +02004404 (host->flags & SDHCI_USE_ADMA) ?
Subhash Jadavani2147ba92016-05-11 16:35:47 -07004405 ((host->flags & SDHCI_USE_64_BIT_DMA) ?
Asutosh Das3621b372014-10-17 16:36:47 +05304406 "64-bit ADMA" : "32-bit ADMA") :
4407 ((host->flags & SDHCI_USE_SDMA) ? "DMA" : "PIO"),
4408 ((mmc->caps2 & MMC_CAP2_CMD_QUEUE) && !ret) ?
4409 "CMDQ" : "legacy");
Pierre Ossmand129bce2006-03-24 03:18:17 -08004410
Anton Vorontsov7260cf52009-03-17 00:13:48 +03004411 sdhci_enable_card_detection(host);
4412
Venkat Gopalakrishnan0c3a9e42014-12-15 17:45:03 -08004413 ret = mmc_add_host(mmc);
4414 if (ret)
4415 goto unled;
4416
Pierre Ossmand129bce2006-03-24 03:18:17 -08004417 return 0;
4418
Adrian Huntereb5c20d2016-04-12 14:25:08 +03004419unled:
Pavan Anamula5b761502015-07-23 18:45:37 +05304420 if (!(host->quirks2 & SDHCI_QUIRK2_BROKEN_LED_CONTROL))
4421 sdhci_led_unregister(host);
Adrian Huntereb5c20d2016-04-12 14:25:08 +03004422unirq:
Russell King03231f92014-04-25 12:57:12 +01004423 sdhci_do_reset(host, SDHCI_RESET_ALL);
Russell Kingb537f942014-04-25 12:56:01 +01004424 sdhci_writel(host, 0, SDHCI_INT_ENABLE);
4425 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01004426 free_irq(host->irq, host);
Pierre Ossman8ef1a142006-06-30 02:22:21 -07004427untasklet:
Pierre Ossmand129bce2006-03-24 03:18:17 -08004428 tasklet_kill(&host->finish_tasklet);
Adrian Hunter52f53362016-06-29 16:24:15 +03004429
Adrian Huntereb5c20d2016-04-12 14:25:08 +03004430 if (!IS_ERR(mmc->supply.vqmmc))
4431 regulator_disable(mmc->supply.vqmmc);
Adrian Hunter52f53362016-06-29 16:24:15 +03004432
Adrian Huntereb5c20d2016-04-12 14:25:08 +03004433 if (host->align_buffer)
4434 dma_free_coherent(mmc_dev(mmc), host->align_buffer_sz +
4435 host->adma_table_sz, host->align_buffer,
4436 host->align_addr);
4437 host->adma_table = NULL;
4438 host->align_buffer = NULL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08004439
4440 return ret;
4441}
Adrian Hunter52f53362016-06-29 16:24:15 +03004442EXPORT_SYMBOL_GPL(__sdhci_add_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08004443
Adrian Hunter52f53362016-06-29 16:24:15 +03004444int sdhci_add_host(struct sdhci_host *host)
4445{
4446 int ret;
4447
4448 ret = sdhci_setup_host(host);
4449 if (ret)
4450 return ret;
4451
4452 return __sdhci_add_host(host);
4453}
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01004454EXPORT_SYMBOL_GPL(sdhci_add_host);
4455
Pierre Ossman1e728592008-04-16 19:13:13 +02004456void sdhci_remove_host(struct sdhci_host *host, int dead)
Pierre Ossmand129bce2006-03-24 03:18:17 -08004457{
Tim Kryger3a48edc2014-06-13 10:13:56 -07004458 struct mmc_host *mmc = host->mmc;
Pierre Ossman1e728592008-04-16 19:13:13 +02004459 unsigned long flags;
4460
4461 if (dead) {
4462 spin_lock_irqsave(&host->lock, flags);
4463
4464 host->flags |= SDHCI_DEVICE_DEAD;
4465
Adrian Hunter5d0d11c2016-06-29 16:24:31 +03004466 if (sdhci_has_requests(host)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05304467 pr_err("%s: Controller removed during "
Markus Mayer4e743f12014-07-03 13:27:42 -07004468 " transfer!\n", mmc_hostname(mmc));
Adrian Hunter5d0d11c2016-06-29 16:24:31 +03004469 sdhci_error_out_mrqs(host, -ENOMEDIUM);
Pierre Ossman1e728592008-04-16 19:13:13 +02004470 }
4471
4472 spin_unlock_irqrestore(&host->lock, flags);
4473 }
4474
Anton Vorontsov7260cf52009-03-17 00:13:48 +03004475 sdhci_disable_card_detection(host);
4476
Sahitya Tummalac6f48d42013-03-10 07:03:17 +05304477 mmc_remove_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08004478
Pavan Anamula5b761502015-07-23 18:45:37 +05304479 if (!(host->quirks2 & SDHCI_QUIRK2_BROKEN_LED_CONTROL))
4480 sdhci_led_unregister(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01004481
Pierre Ossman1e728592008-04-16 19:13:13 +02004482 if (!dead)
Russell King03231f92014-04-25 12:57:12 +01004483 sdhci_do_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08004484
Russell Kingb537f942014-04-25 12:56:01 +01004485 sdhci_writel(host, 0, SDHCI_INT_ENABLE);
4486 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08004487 free_irq(host->irq, host);
4488
4489 del_timer_sync(&host->timer);
Adrian Hunterd7422fb2016-06-29 16:24:33 +03004490 del_timer_sync(&host->data_timer);
Pierre Ossmand129bce2006-03-24 03:18:17 -08004491
Pierre Ossmand129bce2006-03-24 03:18:17 -08004492 tasklet_kill(&host->finish_tasklet);
Pierre Ossman2134a922008-06-28 18:28:51 +02004493
Tim Kryger3a48edc2014-06-13 10:13:56 -07004494 if (!IS_ERR(mmc->supply.vqmmc))
4495 regulator_disable(mmc->supply.vqmmc);
Philip Rakity6231f3d2012-07-23 15:56:23 -07004496
Russell Kingedd63fc2016-01-26 13:39:50 +00004497 if (host->align_buffer)
Russell Kinge66e61c2016-01-26 13:39:55 +00004498 dma_free_coherent(mmc_dev(mmc), host->align_buffer_sz +
4499 host->adma_table_sz, host->align_buffer,
4500 host->align_addr);
Pierre Ossman2134a922008-06-28 18:28:51 +02004501
Adrian Hunter4efaa6f2014-11-04 12:42:39 +02004502 host->adma_table = NULL;
Pierre Ossman2134a922008-06-28 18:28:51 +02004503 host->align_buffer = NULL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08004504}
4505
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01004506EXPORT_SYMBOL_GPL(sdhci_remove_host);
4507
4508void sdhci_free_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08004509{
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01004510 mmc_free_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08004511}
4512
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01004513EXPORT_SYMBOL_GPL(sdhci_free_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08004514
4515/*****************************************************************************\
4516 * *
4517 * Driver init/exit *
4518 * *
4519\*****************************************************************************/
4520
4521static int __init sdhci_drv_init(void)
4522{
Girish K Sa3c76eb2011-10-11 11:44:09 +05304523 pr_info(DRIVER_NAME
Pierre Ossman52fbf9c2007-02-09 08:23:41 +01004524 ": Secure Digital Host Controller Interface driver\n");
Girish K Sa3c76eb2011-10-11 11:44:09 +05304525 pr_info(DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -08004526
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01004527 return 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08004528}
4529
4530static void __exit sdhci_drv_exit(void)
4531{
Pierre Ossmand129bce2006-03-24 03:18:17 -08004532}
4533
4534module_init(sdhci_drv_init);
4535module_exit(sdhci_drv_exit);
4536
Pierre Ossmandf673b22006-06-30 02:22:31 -07004537module_param(debug_quirks, uint, 0444);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03004538module_param(debug_quirks2, uint, 0444);
Pierre Ossman67435272006-06-30 02:22:31 -07004539
Pierre Ossman32710e82009-04-08 20:14:54 +02004540MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01004541MODULE_DESCRIPTION("Secure Digital Host Controller Interface core driver");
Pierre Ossmand129bce2006-03-24 03:18:17 -08004542MODULE_LICENSE("GPL");
Pierre Ossman67435272006-06-30 02:22:31 -07004543
Pierre Ossmandf673b22006-06-30 02:22:31 -07004544MODULE_PARM_DESC(debug_quirks, "Force certain quirks.");
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03004545MODULE_PARM_DESC(debug_quirks2, "Force certain other quirks.");