blob: c089ad83f77699a1d6647cb41f243999ea7418ec [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
Kevin Liu52983382013-01-31 11:31:37 +080057static void sdhci_enable_preset_value(struct sdhci_host *host, bool enable);
Pierre Ossmand129bce2006-03-24 03:18:17 -080058
Asutosh Das06d9f322014-02-21 11:28:36 +053059#ifdef CONFIG_PM
Ritesh Harjani9ce9ecf2015-05-27 15:32:40 +053060static int sdhci_runtime_pm_get(struct sdhci_host *host);
Asutosh Das06d9f322014-02-21 11:28:36 +053061static int sdhci_runtime_pm_put(struct sdhci_host *host);
62#else
Ritesh Harjani9ce9ecf2015-05-27 15:32:40 +053063static inline int sdhci_runtime_pm_get(struct sdhci_host *host)
64{
65 return 0;
66}
Asutosh Das06d9f322014-02-21 11:28:36 +053067static inline int sdhci_runtime_pm_put(struct sdhci_host *host)
68{
69 return 0;
70}
71#endif
72
Asutosh Das494003b2013-03-20 22:53:40 +053073static void sdhci_dump_state(struct sdhci_host *host)
74{
75 struct mmc_host *mmc = host->mmc;
76
77 #ifdef CONFIG_MMC_CLKGATE
78 pr_info("%s: clk: %d clk-gated: %d claimer: %s pwr: %d\n",
79 mmc_hostname(mmc), host->clock, mmc->clk_gated,
80 mmc->claimer->comm, host->pwr);
81 #else
82 pr_info("%s: clk: %d claimer: %s pwr: %d\n",
83 mmc_hostname(mmc), host->clock,
84 mmc->claimer->comm, host->pwr);
85 #endif
86 pr_info("%s: rpmstatus[pltfm](runtime-suspend:usage_count:disable_depth)(%d:%d:%d)\n",
87 mmc_hostname(mmc), mmc->parent->power.runtime_status,
88 atomic_read(&mmc->parent->power.usage_count),
89 mmc->parent->power.disable_depth);
90}
91
Pierre Ossmand129bce2006-03-24 03:18:17 -080092static void sdhci_dumpregs(struct sdhci_host *host)
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--;
271 mdelay(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
898 /*
899 * FIXME: This doesn't account for merging when mapping the
900 * scatterlist.
901 *
902 * The assumption here being that alignment and lengths are
903 * the same after DMA mapping to device address space.
904 */
Russell Kinga0eaf0f2016-01-26 13:41:09 +0000905 length_mask = 0;
Russell Kingdf953922016-01-26 13:41:14 +0000906 offset_mask = 0;
Pierre Ossman2134a922008-06-28 18:28:51 +0200907 if (host->flags & SDHCI_USE_ADMA) {
Russell Kingdf953922016-01-26 13:41:14 +0000908 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE) {
Russell Kinga0eaf0f2016-01-26 13:41:09 +0000909 length_mask = 3;
Russell Kingdf953922016-01-26 13:41:14 +0000910 /*
911 * As we use up to 3 byte chunks to work
912 * around alignment problems, we need to
913 * check the offset as well.
914 */
915 offset_mask = 3;
916 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200917 } else {
918 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE)
Russell Kinga0eaf0f2016-01-26 13:41:09 +0000919 length_mask = 3;
Russell Kingdf953922016-01-26 13:41:14 +0000920 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR)
921 offset_mask = 3;
Pierre Ossman2134a922008-06-28 18:28:51 +0200922 }
923
Russell Kingdf953922016-01-26 13:41:14 +0000924 if (unlikely(length_mask | offset_mask)) {
Pierre Ossman2134a922008-06-28 18:28:51 +0200925 for_each_sg(data->sg, sg, data->sg_len, i) {
Russell Kinga0eaf0f2016-01-26 13:41:09 +0000926 if (sg->length & length_mask) {
Marek Vasut2e4456f2015-11-18 10:47:02 +0100927 DBG("Reverting to PIO because of transfer size (%d)\n",
Russell Kinga0eaf0f2016-01-26 13:41:09 +0000928 sg->length);
Pierre Ossman2134a922008-06-28 18:28:51 +0200929 host->flags &= ~SDHCI_REQ_USE_DMA;
930 break;
931 }
Russell Kinga0eaf0f2016-01-26 13:41:09 +0000932 if (sg->offset & offset_mask) {
Marek Vasut2e4456f2015-11-18 10:47:02 +0100933 DBG("Reverting to PIO because of bad alignment\n");
Pierre Ossman2134a922008-06-28 18:28:51 +0200934 host->flags &= ~SDHCI_REQ_USE_DMA;
935 break;
936 }
937 }
938 }
939 }
940
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200941 if (host->flags & SDHCI_REQ_USE_DMA) {
Russell Kingc0999b72016-01-26 13:40:27 +0000942 int sg_cnt = sdhci_pre_dma_transfer(host, data, COOKIE_MAPPED);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200943
Russell King60c64762016-01-26 13:40:22 +0000944 if (sg_cnt <= 0) {
945 /*
946 * This only happens when someone fed
947 * us an invalid request.
948 */
949 WARN_ON(1);
950 host->flags &= ~SDHCI_REQ_USE_DMA;
951 } else if (host->flags & SDHCI_USE_ADMA) {
Venkat Gopalakrishnana71fbae2014-06-09 14:00:31 -0700952 trace_mmc_adma_table_pre(cmd->opcode, data->sg_len);
Russell King60c64762016-01-26 13:40:22 +0000953 sdhci_adma_table_pre(host, data, sg_cnt);
954
955 sdhci_writel(host, host->adma_addr, SDHCI_ADMA_ADDRESS);
956 if (host->flags & SDHCI_USE_64_BIT_DMA)
957 sdhci_writel(host,
958 (u64)host->adma_addr >> 32,
959 SDHCI_ADMA_ADDRESS_HI);
960 } else {
961 WARN_ON(sg_cnt != 1);
962 sdhci_writel(host, sg_dma_address(data->sg),
963 SDHCI_DMA_ADDRESS);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200964 }
965 }
966
Pierre Ossman2134a922008-06-28 18:28:51 +0200967 /*
968 * Always adjust the DMA selection as some controllers
969 * (e.g. JMicron) can't do PIO properly when the selection
970 * is ADMA.
971 */
972 if (host->version >= SDHCI_SPEC_200) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300973 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossman2134a922008-06-28 18:28:51 +0200974 ctrl &= ~SDHCI_CTRL_DMA_MASK;
975 if ((host->flags & SDHCI_REQ_USE_DMA) &&
Adrian Huntere57a5f62014-11-04 12:42:46 +0200976 (host->flags & SDHCI_USE_ADMA)) {
977 if (host->flags & SDHCI_USE_64_BIT_DMA)
978 ctrl |= SDHCI_CTRL_ADMA64;
979 else
980 ctrl |= SDHCI_CTRL_ADMA32;
981 } else {
Pierre Ossman2134a922008-06-28 18:28:51 +0200982 ctrl |= SDHCI_CTRL_SDMA;
Adrian Huntere57a5f62014-11-04 12:42:46 +0200983 }
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300984 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100985 }
986
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200987 if (!(host->flags & SDHCI_REQ_USE_DMA)) {
Sebastian Andrzej Siewiorda60a912009-06-18 09:33:32 +0200988 int flags;
989
990 flags = SG_MITER_ATOMIC;
991 if (host->data->flags & MMC_DATA_READ)
992 flags |= SG_MITER_TO_SG;
993 else
994 flags |= SG_MITER_FROM_SG;
995 sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
Pierre Ossman76591502008-07-21 00:32:11 +0200996 host->blocks = data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800997 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700998
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300999 sdhci_set_transfer_irqs(host);
1000
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04001001 /* Set the DMA boundary value and block size */
Subhash Jadavani3ed97712014-09-02 17:55:54 -07001002 sdhci_set_blk_size_reg(host, data->blksz, SDHCI_DEFAULT_BOUNDARY_ARG);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001003 sdhci_writew(host, data->blocks, SDHCI_BLOCK_COUNT);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -07001004}
1005
Adrian Hunter0293d502016-06-29 16:24:35 +03001006static inline bool sdhci_auto_cmd12(struct sdhci_host *host,
1007 struct mmc_request *mrq)
1008{
Adrian Hunter20845be2016-08-16 13:44:13 +03001009 return !mrq->sbc && (host->flags & SDHCI_AUTO_CMD12) &&
1010 !mrq->cap_cmd_during_tfr;
Adrian Hunter0293d502016-06-29 16:24:35 +03001011}
1012
Pierre Ossmanc7fa9962006-06-30 02:22:25 -07001013static void sdhci_set_transfer_mode(struct sdhci_host *host,
Andrei Warkentine89d4562011-05-23 15:06:37 -05001014 struct mmc_command *cmd)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -07001015{
Vincent Yangd3fc5d72015-01-20 16:05:17 +08001016 u16 mode = 0;
Andrei Warkentine89d4562011-05-23 15:06:37 -05001017 struct mmc_data *data = cmd->data;
Pierre Ossmanc7fa9962006-06-30 02:22:25 -07001018
Dong Aisheng2b558c12013-10-30 22:09:48 +08001019 if (data == NULL) {
Vincent Wan9b8ffea2014-11-05 14:09:00 +08001020 if (host->quirks2 &
1021 SDHCI_QUIRK2_CLEAR_TRANSFERMODE_REG_BEFORE_CMD) {
1022 sdhci_writew(host, 0x0, SDHCI_TRANSFER_MODE);
1023 } else {
Dong Aisheng2b558c12013-10-30 22:09:48 +08001024 /* clear Auto CMD settings for no data CMDs */
Vincent Wan9b8ffea2014-11-05 14:09:00 +08001025 mode = sdhci_readw(host, SDHCI_TRANSFER_MODE);
1026 sdhci_writew(host, mode & ~(SDHCI_TRNS_AUTO_CMD12 |
Dong Aisheng2b558c12013-10-30 22:09:48 +08001027 SDHCI_TRNS_AUTO_CMD23), SDHCI_TRANSFER_MODE);
Vincent Wan9b8ffea2014-11-05 14:09:00 +08001028 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -07001029 return;
Dong Aisheng2b558c12013-10-30 22:09:48 +08001030 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -07001031
Pierre Ossmane538fbe2007-08-12 16:46:32 +02001032 WARN_ON(!host->data);
1033
Vincent Yangd3fc5d72015-01-20 16:05:17 +08001034 if (!(host->quirks2 & SDHCI_QUIRK2_SUPPORT_SINGLE))
1035 mode = SDHCI_TRNS_BLK_CNT_EN;
1036
Andrei Warkentine89d4562011-05-23 15:06:37 -05001037 if (mmc_op_multi(cmd->opcode) || data->blocks > 1) {
Vincent Yangd3fc5d72015-01-20 16:05:17 +08001038 mode = SDHCI_TRNS_BLK_CNT_EN | SDHCI_TRNS_MULTI;
Andrei Warkentine89d4562011-05-23 15:06:37 -05001039 /*
1040 * If we are sending CMD23, CMD12 never gets sent
1041 * on successful completion (so no Auto-CMD12).
1042 */
Adrian Hunter0293d502016-06-29 16:24:35 +03001043 if (sdhci_auto_cmd12(host, cmd->mrq) &&
Corneliu Doban85cc1c32015-02-09 16:06:29 -08001044 (cmd->opcode != SD_IO_RW_EXTENDED))
Andrei Warkentine89d4562011-05-23 15:06:37 -05001045 mode |= SDHCI_TRNS_AUTO_CMD12;
Adrian Huntera4c73ab2016-06-29 16:24:25 +03001046 else if (cmd->mrq->sbc && (host->flags & SDHCI_AUTO_CMD23)) {
Andrei Warkentin8edf63712011-05-23 15:06:39 -05001047 mode |= SDHCI_TRNS_AUTO_CMD23;
Adrian Huntera4c73ab2016-06-29 16:24:25 +03001048 sdhci_writel(host, cmd->mrq->sbc->arg, SDHCI_ARGUMENT2);
Andrei Warkentin8edf63712011-05-23 15:06:39 -05001049 }
Jerry Huangc4512f72010-08-10 18:01:59 -07001050 }
Andrei Warkentin8edf63712011-05-23 15:06:39 -05001051
Sahitya Tummala9dca7d72013-02-25 15:45:32 +05301052 if (data->flags & MMC_DATA_READ) {
Pierre Ossmanc7fa9962006-06-30 02:22:25 -07001053 mode |= SDHCI_TRNS_READ;
Ritesh Harjani210c2302014-11-14 11:06:40 +05301054 if (host->ops->toggle_cdr) {
1055 if ((cmd->opcode == MMC_SEND_TUNING_BLOCK_HS200) ||
1056 (cmd->opcode == MMC_SEND_TUNING_BLOCK_HS400) ||
1057 (cmd->opcode == MMC_SEND_TUNING_BLOCK))
1058 host->ops->toggle_cdr(host, false);
1059 else
1060 host->ops->toggle_cdr(host, true);
1061 }
Sahitya Tummala9dca7d72013-02-25 15:45:32 +05301062 }
1063 if (host->ops->toggle_cdr && (data->flags & MMC_DATA_WRITE))
1064 host->ops->toggle_cdr(host, false);
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +01001065 if (host->flags & SDHCI_REQ_USE_DMA)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -07001066 mode |= SDHCI_TRNS_DMA;
1067
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001068 sdhci_writew(host, mode, SDHCI_TRANSFER_MODE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001069}
1070
Adrian Hunter0cc563c2016-06-29 16:24:28 +03001071static bool sdhci_needs_reset(struct sdhci_host *host, struct mmc_request *mrq)
1072{
1073 return (!(host->flags & SDHCI_DEVICE_DEAD) &&
1074 ((mrq->cmd && mrq->cmd->error) ||
1075 (mrq->sbc && mrq->sbc->error) ||
1076 (mrq->data && ((mrq->data->error && !mrq->data->stop) ||
1077 (mrq->data->stop && mrq->data->stop->error))) ||
1078 (host->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST)));
1079}
1080
Adrian Hunter4e9f8fe2016-06-29 16:24:34 +03001081static void __sdhci_finish_mrq(struct sdhci_host *host, struct mmc_request *mrq)
1082{
1083 int i;
1084
1085 for (i = 0; i < SDHCI_MAX_MRQS; i++) {
1086 if (host->mrqs_done[i] == mrq) {
1087 WARN_ON(1);
1088 return;
1089 }
1090 }
1091
1092 for (i = 0; i < SDHCI_MAX_MRQS; i++) {
1093 if (!host->mrqs_done[i]) {
1094 host->mrqs_done[i] = mrq;
1095 break;
1096 }
1097 }
1098
1099 WARN_ON(i >= SDHCI_MAX_MRQS);
1100
1101 tasklet_schedule(&host->finish_tasklet);
1102}
1103
Adrian Huntera6d3bdd2016-06-29 16:24:27 +03001104static void sdhci_finish_mrq(struct sdhci_host *host, struct mmc_request *mrq)
1105{
Adrian Hunter5a8a3fe2016-06-29 16:24:30 +03001106 if (host->cmd && host->cmd->mrq == mrq)
1107 host->cmd = NULL;
1108
1109 if (host->data_cmd && host->data_cmd->mrq == mrq)
1110 host->data_cmd = NULL;
1111
1112 if (host->data && host->data->mrq == mrq)
1113 host->data = NULL;
1114
Adrian Huntered1563d2016-06-29 16:24:29 +03001115 if (sdhci_needs_reset(host, mrq))
1116 host->pending_reset = true;
1117
Adrian Hunter4e9f8fe2016-06-29 16:24:34 +03001118 __sdhci_finish_mrq(host, mrq);
Adrian Huntera6d3bdd2016-06-29 16:24:27 +03001119}
1120
Pierre Ossmand129bce2006-03-24 03:18:17 -08001121static void sdhci_finish_data(struct sdhci_host *host)
1122{
Adrian Hunter33a57ad2016-06-29 16:24:36 +03001123 struct mmc_command *data_cmd = host->data_cmd;
1124 struct mmc_data *data = host->data;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001125
Pierre Ossmand129bce2006-03-24 03:18:17 -08001126 host->data = NULL;
Adrian Hunter7c89a3d2016-06-29 16:24:23 +03001127 host->data_cmd = NULL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001128
Russell Kingadd89132016-01-26 13:40:42 +00001129 if ((host->flags & (SDHCI_REQ_USE_DMA | SDHCI_USE_ADMA)) ==
1130 (SDHCI_REQ_USE_DMA | SDHCI_USE_ADMA))
1131 sdhci_adma_table_post(host, data);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001132
1133 /*
Pierre Ossmanc9b74c52008-04-18 20:41:49 +02001134 * The specification states that the block count register must
1135 * be updated, but it does not specify at what point in the
1136 * data flow. That makes the register entirely useless to read
1137 * back so we have to assume that nothing made it to the card
1138 * in the event of an error.
Pierre Ossmand129bce2006-03-24 03:18:17 -08001139 */
Pierre Ossmanc9b74c52008-04-18 20:41:49 +02001140 if (data->error)
1141 data->bytes_xfered = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001142 else
Pierre Ossmanc9b74c52008-04-18 20:41:49 +02001143 data->bytes_xfered = data->blksz * data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001144
Andrei Warkentine89d4562011-05-23 15:06:37 -05001145 /*
1146 * Need to send CMD12 if -
1147 * a) open-ended multiblock transfer (no CMD23)
1148 * b) error in multiblock transfer
1149 */
1150 if (data->stop &&
1151 (data->error ||
Adrian Huntera4c73ab2016-06-29 16:24:25 +03001152 !data->mrq->sbc)) {
Andrei Warkentine89d4562011-05-23 15:06:37 -05001153
Pierre Ossmand129bce2006-03-24 03:18:17 -08001154 /*
1155 * The controller needs a reset of internal state machines
1156 * upon error conditions.
1157 */
Pierre Ossman17b04292007-07-22 22:18:46 +02001158 if (data->error) {
Adrian Hunter33a57ad2016-06-29 16:24:36 +03001159 if (!host->cmd || host->cmd == data_cmd)
1160 sdhci_do_reset(host, SDHCI_RESET_CMD);
Russell King03231f92014-04-25 12:57:12 +01001161 sdhci_do_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001162 }
1163
Adrian Hunter20845be2016-08-16 13:44:13 +03001164 /*
1165 * 'cap_cmd_during_tfr' request must not use the command line
1166 * after mmc_command_done() has been called. It is upper layer's
1167 * responsibility to send the stop command if required.
1168 */
1169 if (data->mrq->cap_cmd_during_tfr) {
1170 sdhci_finish_mrq(host, data->mrq);
1171 } else {
1172 /* Avoid triggering warning in sdhci_send_command() */
1173 host->cmd = NULL;
1174 sdhci_send_command(host, data->stop);
1175 }
Adrian Huntera6d3bdd2016-06-29 16:24:27 +03001176 } else {
1177 sdhci_finish_mrq(host, data->mrq);
1178 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001179}
1180
Adrian Hunterd7422fb2016-06-29 16:24:33 +03001181static void sdhci_mod_timer(struct sdhci_host *host, struct mmc_request *mrq,
1182 unsigned long timeout)
1183{
1184 if (sdhci_data_line_cmd(mrq->cmd))
1185 mod_timer(&host->data_timer, timeout);
1186 else
1187 mod_timer(&host->timer, timeout);
1188}
1189
1190static void sdhci_del_timer(struct sdhci_host *host, struct mmc_request *mrq)
1191{
1192 if (sdhci_data_line_cmd(mrq->cmd))
1193 del_timer(&host->data_timer);
1194 else
1195 del_timer(&host->timer);
1196}
1197
Dong Aishengc0e551292013-09-13 19:11:31 +08001198void sdhci_send_command(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001199{
1200 int flags;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -07001201 u32 mask;
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001202 unsigned long timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001203
1204 WARN_ON(host->cmd);
1205
Russell King96776202016-01-26 13:39:34 +00001206 /* Initially, a command has no error */
1207 cmd->error = 0;
1208
Adrian Hunterfc605f12016-10-05 12:11:21 +03001209 if ((host->quirks2 & SDHCI_QUIRK2_STOP_WITH_TC) &&
1210 cmd->opcode == MMC_STOP_TRANSMISSION)
1211 cmd->flags |= MMC_RSP_BUSY;
1212
Pierre Ossmand129bce2006-03-24 03:18:17 -08001213 /* Wait max 10 ms */
Venkat Gopalakrishnanc90fa962014-04-09 10:54:29 -07001214 timeout = 10000;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -07001215
1216 mask = SDHCI_CMD_INHIBIT;
Adrian Hunter56a590d2016-06-29 16:24:32 +03001217 if (sdhci_data_line_cmd(cmd))
Pierre Ossmanfd2208d2006-06-30 02:22:28 -07001218 mask |= SDHCI_DATA_INHIBIT;
1219
1220 /* We shouldn't wait for data inihibit for stop commands, even
1221 though they might use busy signaling */
Adrian Huntera4c73ab2016-06-29 16:24:25 +03001222 if (cmd->mrq->data && (cmd == cmd->mrq->data->stop))
Pierre Ossmanfd2208d2006-06-30 02:22:28 -07001223 mask &= ~SDHCI_DATA_INHIBIT;
1224
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001225 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001226 if (timeout == 0) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01001227 pr_err("%s: Controller never released inhibit bit(s).\n",
1228 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001229 sdhci_dumpregs(host);
Pierre Ossman17b04292007-07-22 22:18:46 +02001230 cmd->error = -EIO;
Adrian Huntera6d3bdd2016-06-29 16:24:27 +03001231 sdhci_finish_mrq(host, cmd->mrq);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001232 return;
1233 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001234 timeout--;
Venkat Gopalakrishnanc90fa962014-04-09 10:54:29 -07001235 udelay(1);
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001236 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001237
Adrian Hunter3e1a6892013-11-14 10:16:20 +02001238 timeout = jiffies;
Ulf Hansson1d4d7742014-01-08 15:06:08 +01001239 if (!cmd->data && cmd->busy_timeout > 9000)
1240 timeout += DIV_ROUND_UP(cmd->busy_timeout, 1000) * HZ + HZ;
Adrian Hunter3e1a6892013-11-14 10:16:20 +02001241 else
1242 timeout += 10 * HZ;
Adrian Hunterd7422fb2016-06-29 16:24:33 +03001243 sdhci_mod_timer(host, cmd->mrq, timeout);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001244
1245 host->cmd = cmd;
Adrian Hunter56a590d2016-06-29 16:24:32 +03001246 if (sdhci_data_line_cmd(cmd)) {
Adrian Hunter7c89a3d2016-06-29 16:24:23 +03001247 WARN_ON(host->data_cmd);
1248 host->data_cmd = cmd;
1249 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001250
Andrei Warkentina3c77782011-04-11 16:13:42 -05001251 sdhci_prepare_data(host, cmd);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001252
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001253 sdhci_writel(host, cmd->arg, SDHCI_ARGUMENT);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001254
Andrei Warkentine89d4562011-05-23 15:06:37 -05001255 sdhci_set_transfer_mode(host, cmd);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -07001256
Pierre Ossmand129bce2006-03-24 03:18:17 -08001257 if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301258 pr_err("%s: Unsupported response type!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -08001259 mmc_hostname(host->mmc));
Pierre Ossman17b04292007-07-22 22:18:46 +02001260 cmd->error = -EINVAL;
Adrian Huntera6d3bdd2016-06-29 16:24:27 +03001261 sdhci_finish_mrq(host, cmd->mrq);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001262 return;
1263 }
1264
1265 if (!(cmd->flags & MMC_RSP_PRESENT))
1266 flags = SDHCI_CMD_RESP_NONE;
1267 else if (cmd->flags & MMC_RSP_136)
1268 flags = SDHCI_CMD_RESP_LONG;
1269 else if (cmd->flags & MMC_RSP_BUSY)
1270 flags = SDHCI_CMD_RESP_SHORT_BUSY;
1271 else
1272 flags = SDHCI_CMD_RESP_SHORT;
1273
1274 if (cmd->flags & MMC_RSP_CRC)
1275 flags |= SDHCI_CMD_CRC;
1276 if (cmd->flags & MMC_RSP_OPCODE)
1277 flags |= SDHCI_CMD_INDEX;
Arindam Nathb513ea22011-05-05 12:19:04 +05301278
1279 /* CMD19 is special in that the Data Present Select should be set */
Girish K S069c9f12012-01-06 09:56:39 +05301280 if (cmd->data || cmd->opcode == MMC_SEND_TUNING_BLOCK ||
1281 cmd->opcode == MMC_SEND_TUNING_BLOCK_HS200)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001282 flags |= SDHCI_CMD_DATA;
1283
Sahitya Tummala16dabee2013-04-08 12:53:44 +05301284 if (cmd->data)
1285 host->data_start_time = ktime_get();
Venkat Gopalakrishnana71fbae2014-06-09 14:00:31 -07001286 trace_mmc_cmd_rw_start(cmd->opcode, cmd->arg, cmd->flags);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001287 sdhci_writew(host, SDHCI_MAKE_CMD(cmd->opcode, flags), SDHCI_COMMAND);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001288}
Dong Aishengc0e551292013-09-13 19:11:31 +08001289EXPORT_SYMBOL_GPL(sdhci_send_command);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001290
1291static void sdhci_finish_command(struct sdhci_host *host)
1292{
Adrian Huntere0a56402016-06-29 16:24:22 +03001293 struct mmc_command *cmd = host->cmd;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001294 int i;
1295
Adrian Huntere0a56402016-06-29 16:24:22 +03001296 host->cmd = NULL;
1297
1298 if (cmd->flags & MMC_RSP_PRESENT) {
1299 if (cmd->flags & MMC_RSP_136) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08001300 /* CRC is stripped so we need to do some shifting. */
1301 for (i = 0;i < 4;i++) {
Adrian Huntere0a56402016-06-29 16:24:22 +03001302 cmd->resp[i] = sdhci_readl(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001303 SDHCI_RESPONSE + (3-i)*4) << 8;
1304 if (i != 3)
Adrian Huntere0a56402016-06-29 16:24:22 +03001305 cmd->resp[i] |=
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001306 sdhci_readb(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001307 SDHCI_RESPONSE + (3-i)*4-1);
1308 }
1309 } else {
Adrian Huntere0a56402016-06-29 16:24:22 +03001310 cmd->resp[0] = sdhci_readl(host, SDHCI_RESPONSE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001311 }
1312 }
1313
Adrian Hunter20845be2016-08-16 13:44:13 +03001314 if (cmd->mrq->cap_cmd_during_tfr && cmd == cmd->mrq->cmd)
1315 mmc_command_done(host->mmc, cmd->mrq);
1316
Adrian Hunter6bde8682016-06-29 16:24:20 +03001317 /*
1318 * The host can send and interrupt when the busy state has
1319 * ended, allowing us to wait without wasting CPU cycles.
1320 * The busy signal uses DAT0 so this is similar to waiting
1321 * for data to complete.
1322 *
1323 * Note: The 1.0 specification is a bit ambiguous about this
1324 * feature so there might be some problems with older
1325 * controllers.
1326 */
Adrian Huntere0a56402016-06-29 16:24:22 +03001327 if (cmd->flags & MMC_RSP_BUSY) {
1328 if (cmd->data) {
Adrian Hunter6bde8682016-06-29 16:24:20 +03001329 DBG("Cannot wait for busy signal when also doing a data transfer");
1330 } else if (!(host->quirks & SDHCI_QUIRK_NO_BUSY_IRQ) &&
Adrian Hunterea968022016-06-29 16:24:24 +03001331 cmd == host->data_cmd) {
1332 /* Command complete before busy is ended */
Adrian Hunter6bde8682016-06-29 16:24:20 +03001333 return;
1334 }
1335 }
1336
Andrei Warkentine89d4562011-05-23 15:06:37 -05001337 /* Finished CMD23, now send actual command. */
Adrian Huntera4c73ab2016-06-29 16:24:25 +03001338 if (cmd == cmd->mrq->sbc) {
1339 sdhci_send_command(host, cmd->mrq->cmd);
Andrei Warkentine89d4562011-05-23 15:06:37 -05001340 } else {
Pierre Ossmane538fbe2007-08-12 16:46:32 +02001341
Andrei Warkentine89d4562011-05-23 15:06:37 -05001342 /* Processed actual command. */
1343 if (host->data && host->data_early)
1344 sdhci_finish_data(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001345
Adrian Huntere0a56402016-06-29 16:24:22 +03001346 if (!cmd->data)
Adrian Huntera6d3bdd2016-06-29 16:24:27 +03001347 sdhci_finish_mrq(host, cmd->mrq);
Andrei Warkentine89d4562011-05-23 15:06:37 -05001348 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001349}
1350
Kevin Liu52983382013-01-31 11:31:37 +08001351static u16 sdhci_get_preset_value(struct sdhci_host *host)
1352{
Russell Kingd975f122014-04-25 12:59:31 +01001353 u16 preset = 0;
Kevin Liu52983382013-01-31 11:31:37 +08001354
Russell Kingd975f122014-04-25 12:59:31 +01001355 switch (host->timing) {
1356 case MMC_TIMING_UHS_SDR12:
Kevin Liu52983382013-01-31 11:31:37 +08001357 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR12);
1358 break;
Russell Kingd975f122014-04-25 12:59:31 +01001359 case MMC_TIMING_UHS_SDR25:
Kevin Liu52983382013-01-31 11:31:37 +08001360 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR25);
1361 break;
Russell Kingd975f122014-04-25 12:59:31 +01001362 case MMC_TIMING_UHS_SDR50:
Kevin Liu52983382013-01-31 11:31:37 +08001363 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR50);
1364 break;
Russell Kingd975f122014-04-25 12:59:31 +01001365 case MMC_TIMING_UHS_SDR104:
1366 case MMC_TIMING_MMC_HS200:
Kevin Liu52983382013-01-31 11:31:37 +08001367 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR104);
1368 break;
Russell Kingd975f122014-04-25 12:59:31 +01001369 case MMC_TIMING_UHS_DDR50:
Jisheng Zhang0dafa602015-08-18 16:21:39 +08001370 case MMC_TIMING_MMC_DDR52:
Kevin Liu52983382013-01-31 11:31:37 +08001371 preset = sdhci_readw(host, SDHCI_PRESET_FOR_DDR50);
1372 break;
Adrian Huntere9fb05d2014-11-06 15:19:06 +02001373 case MMC_TIMING_MMC_HS400:
1374 preset = sdhci_readw(host, SDHCI_PRESET_FOR_HS400);
1375 break;
Kevin Liu52983382013-01-31 11:31:37 +08001376 default:
1377 pr_warn("%s: Invalid UHS-I mode selected\n",
1378 mmc_hostname(host->mmc));
1379 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR12);
1380 break;
1381 }
1382 return preset;
1383}
1384
Ludovic Desrochesfb9ee042016-04-07 11:13:08 +02001385u16 sdhci_calc_clk(struct sdhci_host *host, unsigned int clock,
1386 unsigned int *actual_clock)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001387{
Arindam Nathc3ed3872011-05-05 12:19:06 +05301388 int div = 0; /* Initialized for compiler warning */
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001389 int real_div = div, clk_mul = 1;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301390 u16 clk = 0;
ludovic.desroches@atmel.com54971592015-07-29 16:22:46 +02001391 bool switch_base_clk = false;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001392
Zhangfei Gao85105c52010-08-06 07:10:01 +08001393 if (host->version >= SDHCI_SPEC_300) {
Russell Kingda91a8f2014-04-25 13:00:12 +01001394 if (host->preset_enabled) {
Kevin Liu52983382013-01-31 11:31:37 +08001395 u16 pre_val;
1396
1397 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1398 pre_val = sdhci_get_preset_value(host);
1399 div = (pre_val & SDHCI_PRESET_SDCLK_FREQ_MASK)
1400 >> SDHCI_PRESET_SDCLK_FREQ_SHIFT;
1401 if (host->clk_mul &&
1402 (pre_val & SDHCI_PRESET_CLKGEN_SEL_MASK)) {
1403 clk = SDHCI_PROG_CLOCK_MODE;
1404 real_div = div + 1;
1405 clk_mul = host->clk_mul;
1406 } else {
1407 real_div = max_t(int, 1, div << 1);
1408 }
1409 goto clock_set;
1410 }
1411
Arindam Nathc3ed3872011-05-05 12:19:06 +05301412 /*
1413 * Check if the Host Controller supports Programmable Clock
1414 * Mode.
1415 */
1416 if (host->clk_mul) {
Kevin Liu52983382013-01-31 11:31:37 +08001417 for (div = 1; div <= 1024; div++) {
1418 if ((host->max_clk * host->clk_mul / div)
1419 <= clock)
1420 break;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001421 }
ludovic.desroches@atmel.com54971592015-07-29 16:22:46 +02001422 if ((host->max_clk * host->clk_mul / div) <= clock) {
1423 /*
1424 * Set Programmable Clock Mode in the Clock
1425 * Control register.
1426 */
1427 clk = SDHCI_PROG_CLOCK_MODE;
1428 real_div = div;
1429 clk_mul = host->clk_mul;
1430 div--;
1431 } else {
1432 /*
1433 * Divisor can be too small to reach clock
1434 * speed requirement. Then use the base clock.
1435 */
1436 switch_base_clk = true;
1437 }
1438 }
1439
1440 if (!host->clk_mul || switch_base_clk) {
Arindam Nathc3ed3872011-05-05 12:19:06 +05301441 /* Version 3.00 divisors must be a multiple of 2. */
1442 if (host->max_clk <= clock)
1443 div = 1;
1444 else {
1445 for (div = 2; div < SDHCI_MAX_DIV_SPEC_300;
1446 div += 2) {
1447 if ((host->max_clk / div) <= clock)
1448 break;
1449 }
1450 }
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001451 real_div = div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301452 div >>= 1;
Suneel Garapatid1955c32015-06-09 13:01:50 +05301453 if ((host->quirks2 & SDHCI_QUIRK2_CLOCK_DIV_ZERO_BROKEN)
1454 && !div && host->max_clk <= 25000000)
1455 div = 1;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001456 }
1457 } else {
1458 /* Version 2.00 divisors must be a power of 2. */
Zhangfei Gao03975262010-09-20 15:15:18 -04001459 for (div = 1; div < SDHCI_MAX_DIV_SPEC_200; div *= 2) {
Zhangfei Gao85105c52010-08-06 07:10:01 +08001460 if ((host->max_clk / div) <= clock)
1461 break;
1462 }
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001463 real_div = div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301464 div >>= 1;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001465 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001466
Kevin Liu52983382013-01-31 11:31:37 +08001467clock_set:
Aisheng Dong03d6f5f2014-08-27 15:26:32 +08001468 if (real_div)
Ludovic Desrochesfb9ee042016-04-07 11:13:08 +02001469 *actual_clock = (host->max_clk * clk_mul) / real_div;
Sahitya Tummala22dd3362013-02-28 19:50:51 +05301470
1471 if (host->quirks2 & SDHCI_QUIRK2_ALWAYS_USE_BASE_CLOCK)
1472 div = 0;
1473
Arindam Nathc3ed3872011-05-05 12:19:06 +05301474 clk |= (div & SDHCI_DIV_MASK) << SDHCI_DIVIDER_SHIFT;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001475 clk |= ((div & SDHCI_DIV_HI_MASK) >> SDHCI_DIV_MASK_LEN)
1476 << SDHCI_DIVIDER_HI_SHIFT;
Ludovic Desrochesfb9ee042016-04-07 11:13:08 +02001477
1478 return clk;
1479}
1480EXPORT_SYMBOL_GPL(sdhci_calc_clk);
1481
1482void sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
1483{
1484 u16 clk;
1485 unsigned long timeout;
1486
1487 host->mmc->actual_clock = 0;
1488
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05301489 if (host->clock)
1490 sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
Ludovic Desrochesfb9ee042016-04-07 11:13:08 +02001491
1492 if (clock == 0)
1493 return;
1494
1495 clk = sdhci_calc_clk(host, clock, &host->mmc->actual_clock);
1496
Pierre Ossmand129bce2006-03-24 03:18:17 -08001497 clk |= SDHCI_CLOCK_INT_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001498 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001499
Chris Ball27f6cb12009-09-22 16:45:31 -07001500 /* Wait max 20 ms */
Venkat Gopalakrishnanc90fa962014-04-09 10:54:29 -07001501 timeout = 20000;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001502 while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001503 & SDHCI_CLOCK_INT_STABLE)) {
1504 if (timeout == 0) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01001505 pr_err("%s: Internal clock never stabilised.\n",
1506 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001507 sdhci_dumpregs(host);
1508 return;
1509 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001510 timeout--;
Adrian Hunterb43ba212017-03-20 19:50:29 +02001511 spin_unlock_irq(&host->lock);
1512 usleep_range(900, 1100);
1513 spin_lock_irq(&host->lock);
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001514 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001515
1516 clk |= SDHCI_CLOCK_CARD_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001517 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001518}
Russell King17710592014-04-25 12:58:55 +01001519EXPORT_SYMBOL_GPL(sdhci_set_clock);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001520
Adrian Hunter1dceb042016-03-29 12:45:43 +03001521static void sdhci_set_power_reg(struct sdhci_host *host, unsigned char mode,
1522 unsigned short vdd)
Pierre Ossman146ad662006-06-30 02:22:23 -07001523{
Tim Kryger3a48edc2014-06-13 10:13:56 -07001524 struct mmc_host *mmc = host->mmc;
Adrian Hunter1dceb042016-03-29 12:45:43 +03001525
1526 spin_unlock_irq(&host->lock);
1527 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, vdd);
1528 spin_lock_irq(&host->lock);
1529
1530 if (mode != MMC_POWER_OFF)
1531 sdhci_writeb(host, SDHCI_POWER_ON, SDHCI_POWER_CONTROL);
1532 else
1533 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
1534}
1535
Adrian Hunter606d3132016-10-05 12:11:22 +03001536void sdhci_set_power_noreg(struct sdhci_host *host, unsigned char mode,
1537 unsigned short vdd)
Adrian Hunter1dceb042016-03-29 12:45:43 +03001538{
Giuseppe Cavallaro83642482010-09-28 10:41:28 +02001539 u8 pwr = 0;
Pierre Ossman146ad662006-06-30 02:22:23 -07001540
Russell King24fbb3c2014-04-25 13:00:06 +01001541 if (mode != MMC_POWER_OFF) {
1542 switch (1 << vdd) {
Pierre Ossmanae628902009-05-03 20:45:03 +02001543 case MMC_VDD_165_195:
1544 pwr = SDHCI_POWER_180;
1545 break;
1546 case MMC_VDD_29_30:
1547 case MMC_VDD_30_31:
1548 pwr = SDHCI_POWER_300;
1549 break;
1550 case MMC_VDD_32_33:
1551 case MMC_VDD_33_34:
1552 pwr = SDHCI_POWER_330;
1553 break;
1554 default:
Adrian Hunter9d5de932015-11-26 14:00:46 +02001555 WARN(1, "%s: Invalid vdd %#x\n",
1556 mmc_hostname(host->mmc), vdd);
1557 break;
Pierre Ossmanae628902009-05-03 20:45:03 +02001558 }
1559 }
1560
1561 if (host->pwr == pwr)
Russell Kinge921a8b2014-04-25 13:00:01 +01001562 return;
Pierre Ossman146ad662006-06-30 02:22:23 -07001563
Pierre Ossmanae628902009-05-03 20:45:03 +02001564 host->pwr = pwr;
1565
1566 if (pwr == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001567 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Sahitya Tummala63d235d2013-02-21 10:09:49 +05301568 if (host->ops->check_power_status)
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05301569 host->ops->check_power_status(host, REQ_BUS_OFF);
Adrian Hunterf0710a52013-05-06 12:17:32 +03001570 if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
1571 sdhci_runtime_pm_bus_off(host);
Russell Kinge921a8b2014-04-25 13:00:01 +01001572 } else {
1573 /*
1574 * Spec says that we should clear the power reg before setting
1575 * a new value. Some controllers don't seem to like this though.
1576 */
Sahitya Tummala63d235d2013-02-21 10:09:49 +05301577 if (!(host->quirks & SDHCI_QUIRK_SINGLE_POWER_WRITE)) {
Russell Kinge921a8b2014-04-25 13:00:01 +01001578 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Sahitya Tummala63d235d2013-02-21 10:09:49 +05301579 if (host->ops->check_power_status)
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05301580 host->ops->check_power_status(host, REQ_BUS_OFF);
Sahitya Tummala63d235d2013-02-21 10:09:49 +05301581 }
Russell Kinge921a8b2014-04-25 13:00:01 +01001582 /*
1583 * At least the Marvell CaFe chip gets confused if we set the
1584 * voltage and set turn on power at the same time, so set the
1585 * voltage first.
1586 */
Sahitya Tummala63d235d2013-02-21 10:09:49 +05301587 if (host->quirks & SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER) {
Russell Kinge921a8b2014-04-25 13:00:01 +01001588 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
Sahitya Tummala63d235d2013-02-21 10:09:49 +05301589 if (host->ops->check_power_status)
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05301590 host->ops->check_power_status(host, REQ_BUS_ON);
Sahitya Tummala63d235d2013-02-21 10:09:49 +05301591 }
Pierre Ossman146ad662006-06-30 02:22:23 -07001592
Russell Kinge921a8b2014-04-25 13:00:01 +01001593 pwr |= SDHCI_POWER_ON;
1594
Pierre Ossmanae628902009-05-03 20:45:03 +02001595 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
Sahitya Tummala63d235d2013-02-21 10:09:49 +05301596 if (host->ops->check_power_status)
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05301597 host->ops->check_power_status(host, REQ_BUS_ON);
Pierre Ossmanae628902009-05-03 20:45:03 +02001598
Russell Kinge921a8b2014-04-25 13:00:01 +01001599 if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
1600 sdhci_runtime_pm_bus_on(host);
Andres Salomone08c1692008-07-04 10:00:03 -07001601
Russell Kinge921a8b2014-04-25 13:00:01 +01001602 /*
1603 * Some controllers need an extra 10ms delay of 10ms before
1604 * they can apply clock after applying power
1605 */
1606 if (host->quirks & SDHCI_QUIRK_DELAY_AFTER_POWER)
1607 mdelay(10);
1608 }
Adrian Hunter1dceb042016-03-29 12:45:43 +03001609}
Adrian Hunter606d3132016-10-05 12:11:22 +03001610EXPORT_SYMBOL_GPL(sdhci_set_power_noreg);
Jisheng Zhang918f4cb2015-12-11 21:36:29 +08001611
Adrian Hunter606d3132016-10-05 12:11:22 +03001612void sdhci_set_power(struct sdhci_host *host, unsigned char mode,
1613 unsigned short vdd)
Adrian Hunter1dceb042016-03-29 12:45:43 +03001614{
Dov Levenglick673a0192015-07-16 11:58:38 +03001615 /*
1616 * Don't disable/re-enable power to the card when running a
1617 * suspend/resume sequence and the pm_flags are configured to preserve
1618 * card power during suspend.
1619 */
1620 if (mmc_card_keep_power(host->mmc) &&
1621 ((host->mmc->dev_status == DEV_SUSPENDED && mode == MMC_POWER_UP) ||
1622 (host->mmc->dev_status == DEV_SUSPENDING && mode == MMC_POWER_OFF)))
1623 return;
1624
Adrian Hunter606d3132016-10-05 12:11:22 +03001625 if (IS_ERR(host->mmc->supply.vmmc))
1626 sdhci_set_power_noreg(host, mode, vdd);
Adrian Hunter1dceb042016-03-29 12:45:43 +03001627 else
Adrian Hunter606d3132016-10-05 12:11:22 +03001628 sdhci_set_power_reg(host, mode, vdd);
Pierre Ossman146ad662006-06-30 02:22:23 -07001629}
Adrian Hunter606d3132016-10-05 12:11:22 +03001630EXPORT_SYMBOL_GPL(sdhci_set_power);
Pierre Ossman146ad662006-06-30 02:22:23 -07001631
Pierre Ossmand129bce2006-03-24 03:18:17 -08001632/*****************************************************************************\
1633 * *
1634 * MMC callbacks *
1635 * *
1636\*****************************************************************************/
1637
Sahitya Tummalac6f48d42013-03-10 07:03:17 +05301638static int sdhci_enable(struct mmc_host *mmc)
1639{
1640 struct sdhci_host *host = mmc_priv(mmc);
1641
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301642 if (host->ops->platform_bus_voting)
1643 host->ops->platform_bus_voting(host, 1);
1644
Sahitya Tummalac6f48d42013-03-10 07:03:17 +05301645 return 0;
1646}
1647
1648static int sdhci_disable(struct mmc_host *mmc)
1649{
1650 struct sdhci_host *host = mmc_priv(mmc);
1651
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301652 if (host->ops->platform_bus_voting)
1653 host->ops->platform_bus_voting(host, 0);
1654
Sahitya Tummalac6f48d42013-03-10 07:03:17 +05301655 return 0;
1656}
1657
Talel Shenhare68741c2015-06-25 09:33:24 +03001658static void sdhci_notify_halt(struct mmc_host *mmc, bool halt)
1659{
1660 struct sdhci_host *host = mmc_priv(mmc);
1661
1662 pr_debug("%s: halt notification was sent, halt=%d\n",
1663 mmc_hostname(mmc), halt);
1664 if (host->flags & SDHCI_USE_ADMA_64BIT) {
1665 if (halt)
1666 host->adma_desc_line_sz = 16;
1667 else
1668 host->adma_desc_line_sz = 12;
1669 }
1670}
1671
Sujit Reddy Thumma360bbf42013-06-19 20:15:37 +05301672static inline void sdhci_update_power_policy(struct sdhci_host *host,
1673 enum sdhci_power_policy policy)
1674{
1675 host->power_policy = policy;
1676}
1677
1678static int sdhci_notify_load(struct mmc_host *mmc, enum mmc_load state)
1679{
1680 int err = 0;
1681 struct sdhci_host *host = mmc_priv(mmc);
1682
1683 switch (state) {
1684 case MMC_LOAD_HIGH:
1685 sdhci_update_power_policy(host, SDHCI_PERFORMANCE_MODE);
1686 break;
1687 case MMC_LOAD_LOW:
1688 sdhci_update_power_policy(host, SDHCI_POWER_SAVE_MODE);
1689 break;
1690 default:
1691 err = -EINVAL;
1692 break;
1693 }
1694
Sahitya Tummalad3004702015-08-06 13:58:47 +05301695 if (host->ops->notify_load)
1696 err = host->ops->notify_load(host, state);
1697
Sujit Reddy Thumma360bbf42013-06-19 20:15:37 +05301698 return err;
1699}
1700
Asutosh Das494003b2013-03-20 22:53:40 +05301701static bool sdhci_check_state(struct sdhci_host *host)
1702{
1703 if (!host->clock || !host->pwr)
1704 return true;
1705 else
1706 return false;
1707}
1708
Asutosh Dasb58499d2013-07-30 19:07:29 +05301709static bool sdhci_check_auto_tuning(struct sdhci_host *host,
1710 struct mmc_command *cmd)
1711{
1712 if (((cmd->opcode != MMC_READ_SINGLE_BLOCK) &&
1713 (cmd->opcode != MMC_READ_MULTIPLE_BLOCK) &&
1714 (cmd->opcode != SD_IO_RW_EXTENDED)) || (host->clock < 100000000))
1715 return false;
1716 else if (host->mmc->ios.timing == MMC_TIMING_MMC_HS200 ||
1717 host->mmc->ios.timing == MMC_TIMING_UHS_SDR104)
1718 return true;
1719 else
1720 return false;
1721}
1722
1723static int sdhci_get_tuning_cmd(struct sdhci_host *host)
1724{
1725 if (!host->mmc || !host->mmc->card)
1726 return 0;
1727 /*
1728 * If we are here, all conditions have already been true
1729 * and the card can either be an eMMC or SD/SDIO
1730 */
1731 if (mmc_card_mmc(host->mmc->card))
1732 return MMC_SEND_TUNING_BLOCK_HS200;
1733 else
1734 return MMC_SEND_TUNING_BLOCK;
1735}
1736
Pierre Ossmand129bce2006-03-24 03:18:17 -08001737static void sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1738{
1739 struct sdhci_host *host;
Shawn Guo505a8682012-12-11 15:23:42 +08001740 int present;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001741 unsigned long flags;
1742
1743 host = mmc_priv(mmc);
1744
Asutosh Das494003b2013-03-20 22:53:40 +05301745 if (sdhci_check_state(host)) {
1746 sdhci_dump_state(host);
1747 WARN(1, "sdhci in bad state");
1748 mrq->cmd->error = -EIO;
1749 if (mrq->data)
1750 mrq->data->error = -EIO;
Venkat Gopalakrishnan987fc412015-09-01 15:52:29 -07001751 host->mrq = NULL;
1752 sdhci_dumpregs(host);
Asutosh Das06d9f322014-02-21 11:28:36 +05301753 mmc_request_done(host->mmc, mrq);
1754 sdhci_runtime_pm_put(host);
Asutosh Das494003b2013-03-20 22:53:40 +05301755 return;
1756 }
1757
Sahitya Tummala68adcae2013-04-16 18:06:06 +05301758 /*
1759 * Firstly check card presence from cd-gpio. The return could
1760 * be one of the following possibilities:
1761 * negative: cd-gpio is not available
1762 * zero: cd-gpio is used, and card is removed
1763 * one: cd-gpio is used, and card is present
1764 */
Adrian Hunter8d28b7a2016-02-09 16:12:36 +02001765 present = mmc->ops->get_cd(mmc);
Sahitya Tummala68adcae2013-04-16 18:06:06 +05301766 if (present < 0) {
1767 /* If polling, assume that the card is always present. */
1768 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
1769 present = 1;
1770 else
1771 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
1772 SDHCI_CARD_PRESENT;
1773 }
Krzysztof Kozlowski28367662015-01-05 10:50:15 +01001774
Pierre Ossmand129bce2006-03-24 03:18:17 -08001775 spin_lock_irqsave(&host->lock, flags);
1776
Pavan Anamula5b761502015-07-23 18:45:37 +05301777 if (!(host->quirks2 & SDHCI_QUIRK2_BROKEN_LED_CONTROL))
1778 sdhci_led_activate(host);
Andrei Warkentine89d4562011-05-23 15:06:37 -05001779
1780 /*
1781 * Ensure we don't send the STOP for non-SET_BLOCK_COUNTED
1782 * requests if Auto-CMD12 is enabled.
1783 */
Adrian Hunter0293d502016-06-29 16:24:35 +03001784 if (sdhci_auto_cmd12(host, mrq)) {
Jerry Huangc4512f72010-08-10 18:01:59 -07001785 if (mrq->stop) {
1786 mrq->data->stop = NULL;
1787 mrq->stop = NULL;
1788 }
1789 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001790
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001791 if (!present || host->flags & SDHCI_DEVICE_DEAD) {
Adrian Huntera4c73ab2016-06-29 16:24:25 +03001792 mrq->cmd->error = -ENOMEDIUM;
Adrian Huntera6d3bdd2016-06-29 16:24:27 +03001793 sdhci_finish_mrq(host, mrq);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301794 } else {
Asutosh Dasb58499d2013-07-30 19:07:29 +05301795 if (host->ops->config_auto_tuning_cmd) {
1796 if (sdhci_check_auto_tuning(host, mrq->cmd))
1797 host->ops->config_auto_tuning_cmd(host, true,
1798 sdhci_get_tuning_cmd(host));
1799 else
1800 host->ops->config_auto_tuning_cmd(host, false,
1801 sdhci_get_tuning_cmd(host));
1802 }
1803
Andrei Warkentin8edf63712011-05-23 15:06:39 -05001804 if (mrq->sbc && !(host->flags & SDHCI_AUTO_CMD23))
Andrei Warkentine89d4562011-05-23 15:06:37 -05001805 sdhci_send_command(host, mrq->sbc);
1806 else
1807 sdhci_send_command(host, mrq->cmd);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301808 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001809
Pierre Ossman5f25a662006-10-04 02:15:39 -07001810 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001811 spin_unlock_irqrestore(&host->lock, flags);
1812}
1813
Russell King2317f562014-04-25 12:57:07 +01001814void sdhci_set_bus_width(struct sdhci_host *host, int width)
1815{
1816 u8 ctrl;
1817
1818 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
1819 if (width == MMC_BUS_WIDTH_8) {
1820 ctrl &= ~SDHCI_CTRL_4BITBUS;
1821 if (host->version >= SDHCI_SPEC_300)
1822 ctrl |= SDHCI_CTRL_8BITBUS;
1823 } else {
1824 if (host->version >= SDHCI_SPEC_300)
1825 ctrl &= ~SDHCI_CTRL_8BITBUS;
1826 if (width == MMC_BUS_WIDTH_4)
1827 ctrl |= SDHCI_CTRL_4BITBUS;
1828 else
1829 ctrl &= ~SDHCI_CTRL_4BITBUS;
1830 }
1831 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1832}
1833EXPORT_SYMBOL_GPL(sdhci_set_bus_width);
1834
Russell King96d7b782014-04-25 12:59:26 +01001835void sdhci_set_uhs_signaling(struct sdhci_host *host, unsigned timing)
1836{
1837 u16 ctrl_2;
1838
1839 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1840 /* Select Bus Speed Mode for host */
1841 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
1842 if ((timing == MMC_TIMING_MMC_HS200) ||
1843 (timing == MMC_TIMING_UHS_SDR104))
1844 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
1845 else if (timing == MMC_TIMING_UHS_SDR12)
1846 ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
1847 else if (timing == MMC_TIMING_UHS_SDR25)
1848 ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
1849 else if (timing == MMC_TIMING_UHS_SDR50)
1850 ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
1851 else if ((timing == MMC_TIMING_UHS_DDR50) ||
1852 (timing == MMC_TIMING_MMC_DDR52))
1853 ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
Adrian Huntere9fb05d2014-11-06 15:19:06 +02001854 else if (timing == MMC_TIMING_MMC_HS400)
1855 ctrl_2 |= SDHCI_CTRL_HS400; /* Non-standard */
Russell King96d7b782014-04-25 12:59:26 +01001856 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
1857}
1858EXPORT_SYMBOL_GPL(sdhci_set_uhs_signaling);
1859
Dong Aishengded97e02016-04-16 01:29:25 +08001860static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001861{
Dong Aishengded97e02016-04-16 01:29:25 +08001862 struct sdhci_host *host = mmc_priv(mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001863 unsigned long flags;
1864 u8 ctrl;
Asutosh Das30ec5992013-11-08 12:31:48 +05301865 int ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001866
Adrian Hunterceb61432011-12-27 15:48:41 +02001867 if (host->flags & SDHCI_DEVICE_DEAD) {
Tim Kryger3a48edc2014-06-13 10:13:56 -07001868 if (!IS_ERR(mmc->supply.vmmc) &&
1869 ios->power_mode == MMC_POWER_OFF)
Markus Mayer4e743f12014-07-03 13:27:42 -07001870 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0);
Adrian Hunterceb61432011-12-27 15:48:41 +02001871 return;
1872 }
Pierre Ossman1e728592008-04-16 19:13:13 +02001873
Kevin Liu52983382013-01-31 11:31:37 +08001874 if (host->version >= SDHCI_SPEC_300 &&
Dong Aisheng372c4632013-10-18 19:48:50 +08001875 (ios->power_mode == MMC_POWER_UP) &&
1876 !(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN))
Kevin Liu52983382013-01-31 11:31:37 +08001877 sdhci_enable_preset_value(host, false);
1878
Venkat Gopalakrishnan0fac7312015-01-23 12:58:00 -08001879 spin_lock_irqsave(&host->lock, flags);
Venkat Gopalakrishnanf36166f2015-03-02 13:22:40 -08001880 if (ios->clock &&
1881 ((ios->clock != host->clock) || (ios->timing != host->timing))) {
Venkat Gopalakrishnan0fac7312015-01-23 12:58:00 -08001882 spin_unlock_irqrestore(&host->lock, flags);
1883 host->ops->set_clock(host, ios->clock);
1884 spin_lock_irqsave(&host->lock, flags);
1885 host->clock = ios->clock;
1886
1887 if (host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK &&
1888 host->clock) {
1889 host->timeout_clk = host->mmc->actual_clock ?
1890 host->mmc->actual_clock / 1000 :
1891 host->clock / 1000;
1892 host->mmc->max_busy_timeout =
1893 host->ops->get_max_timeout_count ?
1894 host->ops->get_max_timeout_count(host) :
1895 1 << 27;
1896 host->mmc->max_busy_timeout /= host->timeout_clk;
1897 }
1898 }
1899 spin_unlock_irqrestore(&host->lock, flags);
1900
Asutosh Das30ec5992013-11-08 12:31:48 +05301901 /*
1902 * The controller clocks may be off during power-up and we may end up
1903 * enabling card clock before giving power to the card. Hence, during
1904 * MMC_POWER_UP enable the controller clock and turn-on the regulators.
1905 * The mmc_power_up would provide the necessary delay before turning on
1906 * the clocks to the card.
1907 */
1908 if (ios->power_mode & MMC_POWER_UP) {
1909 if (host->ops->enable_controller_clock) {
1910 ret = host->ops->enable_controller_clock(host);
1911 if (ret) {
1912 pr_err("%s: enabling controller clock: failed: %d\n",
1913 mmc_hostname(host->mmc), ret);
1914 } else {
1915 sdhci_set_power(host, ios->power_mode, ios->vdd);
1916 }
1917 }
1918 }
1919
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05301920 spin_lock_irqsave(&host->lock, flags);
Venkat Gopalakrishnan0fac7312015-01-23 12:58:00 -08001921 if (!host->clock) {
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05301922 spin_unlock_irqrestore(&host->lock, flags);
Venkat Gopalakrishnan0fac7312015-01-23 12:58:00 -08001923 return;
Russell King373073e2014-04-25 12:58:45 +01001924 }
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05301925 spin_unlock_irqrestore(&host->lock, flags);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001926
Adrian Hunter606d3132016-10-05 12:11:22 +03001927 if (host->ops->set_power)
1928 host->ops->set_power(host, ios->power_mode, ios->vdd);
1929 else
Asutosh Das30ec5992013-11-08 12:31:48 +05301930 if (!host->ops->enable_controller_clock && (ios->power_mode &
1931 (MMC_POWER_UP |
1932 MMC_POWER_ON)))
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05301933 sdhci_set_power(host, ios->power_mode, ios->vdd);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001934
Sahitya Tummala63d235d2013-02-21 10:09:49 +05301935 spin_lock_irqsave(&host->lock, flags);
1936
Philip Rakity643a81f2010-09-23 08:24:32 -07001937 if (host->ops->platform_send_init_74_clocks)
1938 host->ops->platform_send_init_74_clocks(host, ios->power_mode);
1939
Russell King2317f562014-04-25 12:57:07 +01001940 host->ops->set_bus_width(host, ios->bus_width);
Philip Rakity15ec4462010-11-19 16:48:39 -05001941
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001942 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001943
Philip Rakity3ab9c8d2010-10-06 11:57:23 -07001944 if ((ios->timing == MMC_TIMING_SD_HS ||
1945 ios->timing == MMC_TIMING_MMC_HS)
1946 && !(host->quirks & SDHCI_QUIRK_NO_HISPD_BIT))
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001947 ctrl |= SDHCI_CTRL_HISPD;
1948 else
1949 ctrl &= ~SDHCI_CTRL_HISPD;
1950
Arindam Nathd6d50a12011-05-05 12:18:59 +05301951 if (host->version >= SDHCI_SPEC_300) {
Arindam Nath49c468f2011-05-05 12:19:01 +05301952 u16 clk, ctrl_2;
Arindam Nath49c468f2011-05-05 12:19:01 +05301953
1954 /* In case of UHS-I modes, set High Speed Enable */
Adrian Huntere9fb05d2014-11-06 15:19:06 +02001955 if ((ios->timing == MMC_TIMING_MMC_HS400) ||
1956 (ios->timing == MMC_TIMING_MMC_HS200) ||
Seungwon Jeonbb8175a2014-03-14 21:12:48 +09001957 (ios->timing == MMC_TIMING_MMC_DDR52) ||
Girish K S069c9f12012-01-06 09:56:39 +05301958 (ios->timing == MMC_TIMING_UHS_SDR50) ||
Arindam Nath49c468f2011-05-05 12:19:01 +05301959 (ios->timing == MMC_TIMING_UHS_SDR104) ||
1960 (ios->timing == MMC_TIMING_UHS_DDR50) ||
Alexander Elbsdd8df172012-01-03 23:26:53 -05001961 (ios->timing == MMC_TIMING_UHS_SDR25))
Arindam Nath49c468f2011-05-05 12:19:01 +05301962 ctrl |= SDHCI_CTRL_HISPD;
Arindam Nathd6d50a12011-05-05 12:18:59 +05301963
Russell Kingda91a8f2014-04-25 13:00:12 +01001964 if (!host->preset_enabled) {
Arindam Nath758535c2011-05-05 12:19:00 +05301965 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301966 /*
1967 * We only need to set Driver Strength if the
1968 * preset value enable is not set.
1969 */
Russell Kingda91a8f2014-04-25 13:00:12 +01001970 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301971 ctrl_2 &= ~SDHCI_CTRL_DRV_TYPE_MASK;
1972 if (ios->drv_type == MMC_SET_DRIVER_TYPE_A)
1973 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_A;
Petri Gynther43e943a2015-05-20 14:35:00 -07001974 else if (ios->drv_type == MMC_SET_DRIVER_TYPE_B)
1975 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_B;
Arindam Nathd6d50a12011-05-05 12:18:59 +05301976 else if (ios->drv_type == MMC_SET_DRIVER_TYPE_C)
1977 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_C;
Petri Gynther43e943a2015-05-20 14:35:00 -07001978 else if (ios->drv_type == MMC_SET_DRIVER_TYPE_D)
1979 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_D;
1980 else {
Marek Vasut2e4456f2015-11-18 10:47:02 +01001981 pr_warn("%s: invalid driver type, default to driver type B\n",
1982 mmc_hostname(mmc));
Petri Gynther43e943a2015-05-20 14:35:00 -07001983 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_B;
1984 }
Arindam Nathd6d50a12011-05-05 12:18:59 +05301985
1986 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
Arindam Nath758535c2011-05-05 12:19:00 +05301987 } else {
1988 /*
1989 * According to SDHC Spec v3.00, if the Preset Value
1990 * Enable in the Host Control 2 register is set, we
1991 * need to reset SD Clock Enable before changing High
1992 * Speed Enable to avoid generating clock gliches.
1993 */
Arindam Nath758535c2011-05-05 12:19:00 +05301994
1995 /* Reset SD Clock Enable */
1996 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1997 clk &= ~SDHCI_CLOCK_CARD_EN;
1998 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1999
2000 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
2001
2002 /* Re-enable SD Clock */
Venkat Gopalakrishnan745734f2015-01-12 19:52:35 -08002003 if (ios->clock) {
2004 spin_unlock_irqrestore(&host->lock, flags);
Sujit Reddy Thummafbe7d862014-01-21 17:22:05 +05302005 host->ops->set_clock(host, host->clock);
Venkat Gopalakrishnan745734f2015-01-12 19:52:35 -08002006 spin_lock_irqsave(&host->lock, flags);
2007 }
Arindam Nathd6d50a12011-05-05 12:18:59 +05302008 }
Arindam Nath49c468f2011-05-05 12:19:01 +05302009
Arindam Nath49c468f2011-05-05 12:19:01 +05302010 /* Reset SD Clock Enable */
2011 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
2012 clk &= ~SDHCI_CLOCK_CARD_EN;
2013 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
2014
Russell King96d7b782014-04-25 12:59:26 +01002015 host->ops->set_uhs_signaling(host, ios->timing);
Russell Kingd975f122014-04-25 12:59:31 +01002016 host->timing = ios->timing;
Arindam Nath49c468f2011-05-05 12:19:01 +05302017
Kevin Liu52983382013-01-31 11:31:37 +08002018 if (!(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN) &&
2019 ((ios->timing == MMC_TIMING_UHS_SDR12) ||
2020 (ios->timing == MMC_TIMING_UHS_SDR25) ||
2021 (ios->timing == MMC_TIMING_UHS_SDR50) ||
2022 (ios->timing == MMC_TIMING_UHS_SDR104) ||
Jisheng Zhang0dafa602015-08-18 16:21:39 +08002023 (ios->timing == MMC_TIMING_UHS_DDR50) ||
2024 (ios->timing == MMC_TIMING_MMC_DDR52))) {
Kevin Liu52983382013-01-31 11:31:37 +08002025 u16 preset;
2026
2027 sdhci_enable_preset_value(host, true);
2028 preset = sdhci_get_preset_value(host);
2029 ios->drv_type = (preset & SDHCI_PRESET_DRV_MASK)
2030 >> SDHCI_PRESET_DRV_SHIFT;
2031 }
2032
Arindam Nath49c468f2011-05-05 12:19:01 +05302033 /* Re-enable SD Clock */
Venkat Gopalakrishnan745734f2015-01-12 19:52:35 -08002034 if (ios->clock) {
2035 spin_unlock_irqrestore(&host->lock, flags);
Sujit Reddy Thummafbe7d862014-01-21 17:22:05 +05302036 host->ops->set_clock(host, host->clock);
Venkat Gopalakrishnan745734f2015-01-12 19:52:35 -08002037 spin_lock_irqsave(&host->lock, flags);
2038 }
Arindam Nath758535c2011-05-05 12:19:00 +05302039 } else
2040 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05302041
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302042 spin_unlock_irqrestore(&host->lock, flags);
Leandro Dorileob8352262007-07-25 23:47:04 +02002043 /*
2044 * Some (ENE) controllers go apeshit on some ios operation,
2045 * signalling timeout and CRC errors even on CMD0. Resetting
2046 * it on each ios seems to solve the problem.
2047 */
Mohammad Jamalc63705e2015-01-13 20:47:24 +05302048 if (host->quirks & SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS)
Russell King03231f92014-04-25 12:57:12 +01002049 sdhci_do_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
Leandro Dorileob8352262007-07-25 23:47:04 +02002050
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302051 /*
2052 * Reset the chip on each power off.
2053 * Should clear out any weird states.
2054 */
2055 if (ios->power_mode == MMC_POWER_OFF) {
2056 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
2057 sdhci_reinit(host);
2058 sdhci_set_power(host, ios->power_mode, ios->vdd);
2059 }
2060 if (!ios->clock)
Venkat Gopalakrishnan766b7452015-03-10 15:51:23 -07002061 host->ops->set_clock(host, ios->clock);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302062
Pierre Ossman5f25a662006-10-04 02:15:39 -07002063 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002064}
2065
Dong Aishengded97e02016-04-16 01:29:25 +08002066static int sdhci_get_cd(struct mmc_host *mmc)
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002067{
2068 struct sdhci_host *host = mmc_priv(mmc);
Dong Aishengded97e02016-04-16 01:29:25 +08002069 int gpio_cd = mmc_gpio_get_cd(mmc);
Kevin Liu94144a42013-02-28 17:35:53 +08002070
2071 if (host->flags & SDHCI_DEVICE_DEAD)
2072 return 0;
2073
Ivan T. Ivanov88af5652015-07-06 15:16:19 +03002074 /* If nonremovable, assume that the card is always present. */
Jaehoon Chung860951c2016-06-21 10:13:26 +09002075 if (!mmc_card_is_removable(host->mmc))
Kevin Liu94144a42013-02-28 17:35:53 +08002076 return 1;
2077
Ivan T. Ivanov88af5652015-07-06 15:16:19 +03002078 /*
2079 * Try slot gpio detect, if defined it take precedence
2080 * over build in controller functionality
2081 */
Arnd Bergmann287980e2016-05-27 23:23:25 +02002082 if (gpio_cd >= 0)
Kevin Liu94144a42013-02-28 17:35:53 +08002083 return !!gpio_cd;
2084
Ivan T. Ivanov88af5652015-07-06 15:16:19 +03002085 /* If polling, assume that the card is always present. */
2086 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
2087 return 1;
2088
Kevin Liu94144a42013-02-28 17:35:53 +08002089 /* Host native card detect */
2090 return !!(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT);
2091}
2092
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002093static int sdhci_check_ro(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002094{
Pierre Ossmand129bce2006-03-24 03:18:17 -08002095 unsigned long flags;
Wolfram Sang2dfb5792010-10-15 12:21:01 +02002096 int is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002097
Pierre Ossmand129bce2006-03-24 03:18:17 -08002098 spin_lock_irqsave(&host->lock, flags);
2099
Pierre Ossman1e728592008-04-16 19:13:13 +02002100 if (host->flags & SDHCI_DEVICE_DEAD)
Wolfram Sang2dfb5792010-10-15 12:21:01 +02002101 is_readonly = 0;
2102 else if (host->ops->get_ro)
2103 is_readonly = host->ops->get_ro(host);
Pierre Ossman1e728592008-04-16 19:13:13 +02002104 else
Wolfram Sang2dfb5792010-10-15 12:21:01 +02002105 is_readonly = !(sdhci_readl(host, SDHCI_PRESENT_STATE)
2106 & SDHCI_WRITE_PROTECT);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002107
2108 spin_unlock_irqrestore(&host->lock, flags);
2109
Wolfram Sang2dfb5792010-10-15 12:21:01 +02002110 /* This quirk needs to be replaced by a callback-function later */
2111 return host->quirks & SDHCI_QUIRK_INVERTED_WRITE_PROTECT ?
2112 !is_readonly : is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002113}
2114
Takashi Iwai82b0e232011-04-21 20:26:38 +02002115#define SAMPLE_COUNT 5
2116
Dong Aishengded97e02016-04-16 01:29:25 +08002117static int sdhci_get_ro(struct mmc_host *mmc)
Takashi Iwai82b0e232011-04-21 20:26:38 +02002118{
Dong Aishengded97e02016-04-16 01:29:25 +08002119 struct sdhci_host *host = mmc_priv(mmc);
Takashi Iwai82b0e232011-04-21 20:26:38 +02002120 int i, ro_count;
2121
Takashi Iwai82b0e232011-04-21 20:26:38 +02002122 if (!(host->quirks & SDHCI_QUIRK_UNSTABLE_RO_DETECT))
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002123 return sdhci_check_ro(host);
Takashi Iwai82b0e232011-04-21 20:26:38 +02002124
2125 ro_count = 0;
2126 for (i = 0; i < SAMPLE_COUNT; i++) {
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002127 if (sdhci_check_ro(host)) {
Takashi Iwai82b0e232011-04-21 20:26:38 +02002128 if (++ro_count > SAMPLE_COUNT / 2)
2129 return 1;
2130 }
2131 msleep(30);
2132 }
2133 return 0;
2134}
2135
Adrian Hunter20758b62011-08-29 16:42:12 +03002136static void sdhci_hw_reset(struct mmc_host *mmc)
2137{
2138 struct sdhci_host *host = mmc_priv(mmc);
2139
2140 if (host->ops && host->ops->hw_reset)
2141 host->ops->hw_reset(host);
2142}
2143
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002144static void sdhci_enable_sdio_irq_nolock(struct sdhci_host *host, int enable)
2145{
Russell Kingbe138552014-04-25 12:55:56 +01002146 if (!(host->flags & SDHCI_DEVICE_DEAD)) {
Russell Kingef104332014-04-25 12:55:41 +01002147 if (enable)
Russell Kingb537f942014-04-25 12:56:01 +01002148 host->ier |= SDHCI_INT_CARD_INT;
Russell Kingef104332014-04-25 12:55:41 +01002149 else
Russell Kingb537f942014-04-25 12:56:01 +01002150 host->ier &= ~SDHCI_INT_CARD_INT;
2151
2152 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
2153 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Russell Kingef104332014-04-25 12:55:41 +01002154 mmiowb();
2155 }
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002156}
Pierre Ossmanf75979b2007-09-04 07:59:18 +02002157
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002158static void sdhci_enable_sdio_irq(struct mmc_host *mmc, int enable)
2159{
2160 struct sdhci_host *host = mmc_priv(mmc);
2161 unsigned long flags;
2162
Hans de Goedefa3b4f42017-03-26 13:14:45 +02002163 if (enable)
2164 pm_runtime_get_noresume(host->mmc->parent);
2165
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002166 spin_lock_irqsave(&host->lock, flags);
Russell Kingef104332014-04-25 12:55:41 +01002167 if (enable)
2168 host->flags |= SDHCI_SDIO_IRQ_ENABLED;
2169 else
2170 host->flags &= ~SDHCI_SDIO_IRQ_ENABLED;
2171
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002172 sdhci_enable_sdio_irq_nolock(host, enable);
Pierre Ossmanf75979b2007-09-04 07:59:18 +02002173 spin_unlock_irqrestore(&host->lock, flags);
Hans de Goedefa3b4f42017-03-26 13:14:45 +02002174
2175 if (!enable)
2176 pm_runtime_put_noidle(host->mmc->parent);
Pierre Ossmanf75979b2007-09-04 07:59:18 +02002177}
2178
Dong Aishengded97e02016-04-16 01:29:25 +08002179static int sdhci_start_signal_voltage_switch(struct mmc_host *mmc,
2180 struct mmc_ios *ios)
Philip Rakity6231f3d2012-07-23 15:56:23 -07002181{
Dong Aishengded97e02016-04-16 01:29:25 +08002182 struct sdhci_host *host = mmc_priv(mmc);
Philip Rakity6231f3d2012-07-23 15:56:23 -07002183 u16 ctrl;
Kevin Liu20b92a32012-12-17 19:29:26 +08002184 int ret;
Philip Rakity6231f3d2012-07-23 15:56:23 -07002185
2186 /*
2187 * Signal Voltage Switching is only applicable for Host Controllers
2188 * v3.00 and above.
2189 */
2190 if (host->version < SDHCI_SPEC_300)
2191 return 0;
2192
Philip Rakity6231f3d2012-07-23 15:56:23 -07002193 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
Kevin Liu20b92a32012-12-17 19:29:26 +08002194
Fabio Estevam21f59982013-02-14 10:35:03 -02002195 switch (ios->signal_voltage) {
Kevin Liu20b92a32012-12-17 19:29:26 +08002196 case MMC_SIGNAL_VOLTAGE_330:
Adrian Hunter8cb851a2016-06-29 16:24:16 +03002197 if (!(host->flags & SDHCI_SIGNALING_330))
2198 return -EINVAL;
Kevin Liu20b92a32012-12-17 19:29:26 +08002199 /* Set 1.8V Signal Enable in the Host Control2 register to 0 */
2200 ctrl &= ~SDHCI_CTRL_VDD_180;
2201 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Sahitya Tummala63d235d2013-02-21 10:09:49 +05302202 if (host->ops->check_power_status)
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302203 host->ops->check_power_status(host, REQ_IO_HIGH);
Kevin Liu20b92a32012-12-17 19:29:26 +08002204
Tim Kryger3a48edc2014-06-13 10:13:56 -07002205 if (!IS_ERR(mmc->supply.vqmmc)) {
Dong Aisheng761daa32016-07-12 15:46:10 +08002206 ret = mmc_regulator_set_vqmmc(mmc, ios);
Kevin Liu20b92a32012-12-17 19:29:26 +08002207 if (ret) {
Joe Perches66061102014-09-12 14:56:56 -07002208 pr_warn("%s: Switching to 3.3V signalling voltage failed\n",
2209 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08002210 return -EIO;
2211 }
2212 }
2213 /* Wait for 5ms */
2214 usleep_range(5000, 5500);
2215
2216 /* 3.3V regulator output should be stable within 5 ms */
2217 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2218 if (!(ctrl & SDHCI_CTRL_VDD_180))
2219 return 0;
2220
Joe Perches66061102014-09-12 14:56:56 -07002221 pr_warn("%s: 3.3V regulator output did not became stable\n",
2222 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08002223
2224 return -EAGAIN;
2225 case MMC_SIGNAL_VOLTAGE_180:
Adrian Hunter8cb851a2016-06-29 16:24:16 +03002226 if (!(host->flags & SDHCI_SIGNALING_180))
2227 return -EINVAL;
Tim Kryger3a48edc2014-06-13 10:13:56 -07002228 if (!IS_ERR(mmc->supply.vqmmc)) {
Dong Aisheng761daa32016-07-12 15:46:10 +08002229 ret = mmc_regulator_set_vqmmc(mmc, ios);
Kevin Liu20b92a32012-12-17 19:29:26 +08002230 if (ret) {
Joe Perches66061102014-09-12 14:56:56 -07002231 pr_warn("%s: Switching to 1.8V signalling voltage failed\n",
2232 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08002233 return -EIO;
2234 }
2235 }
2236
2237 /*
2238 * Enable 1.8V Signal Enable in the Host Control2
2239 * register
2240 */
2241 ctrl |= SDHCI_CTRL_VDD_180;
2242 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Sahitya Tummala63d235d2013-02-21 10:09:49 +05302243 if (host->ops->check_power_status)
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302244 host->ops->check_power_status(host, REQ_IO_LOW);
Kevin Liu20b92a32012-12-17 19:29:26 +08002245
Vincent Yang9d967a62015-01-20 16:05:15 +08002246 /* Some controller need to do more when switching */
2247 if (host->ops->voltage_switch)
2248 host->ops->voltage_switch(host);
2249
Kevin Liu20b92a32012-12-17 19:29:26 +08002250 /* 1.8V regulator output should be stable within 5 ms */
2251 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2252 if (ctrl & SDHCI_CTRL_VDD_180)
2253 return 0;
2254
Joe Perches66061102014-09-12 14:56:56 -07002255 pr_warn("%s: 1.8V regulator output did not became stable\n",
2256 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08002257
2258 return -EAGAIN;
2259 case MMC_SIGNAL_VOLTAGE_120:
Adrian Hunter8cb851a2016-06-29 16:24:16 +03002260 if (!(host->flags & SDHCI_SIGNALING_120))
2261 return -EINVAL;
Tim Kryger3a48edc2014-06-13 10:13:56 -07002262 if (!IS_ERR(mmc->supply.vqmmc)) {
Dong Aisheng761daa32016-07-12 15:46:10 +08002263 ret = mmc_regulator_set_vqmmc(mmc, ios);
Kevin Liu20b92a32012-12-17 19:29:26 +08002264 if (ret) {
Joe Perches66061102014-09-12 14:56:56 -07002265 pr_warn("%s: Switching to 1.2V signalling voltage failed\n",
2266 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08002267 return -EIO;
2268 }
2269 }
2270 return 0;
2271 default:
Arindam Nathf2119df2011-05-05 12:18:57 +05302272 /* No signal voltage switch required */
2273 return 0;
Kevin Liu20b92a32012-12-17 19:29:26 +08002274 }
Arindam Nathf2119df2011-05-05 12:18:57 +05302275}
2276
Kevin Liu20b92a32012-12-17 19:29:26 +08002277static int sdhci_card_busy(struct mmc_host *mmc)
2278{
2279 struct sdhci_host *host = mmc_priv(mmc);
2280 u32 present_state;
2281
Adrian Huntere613cc42016-06-23 14:00:58 +03002282 /* Check whether DAT[0] is 0 */
Kevin Liu20b92a32012-12-17 19:29:26 +08002283 present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
Kevin Liu20b92a32012-12-17 19:29:26 +08002284
Adrian Huntere613cc42016-06-23 14:00:58 +03002285 return !(present_state & SDHCI_DATA_0_LVL_MASK);
Kevin Liu20b92a32012-12-17 19:29:26 +08002286}
2287
Adrian Hunterb5540ce2014-12-05 19:25:31 +02002288static int sdhci_prepare_hs400_tuning(struct mmc_host *mmc, struct mmc_ios *ios)
2289{
2290 struct sdhci_host *host = mmc_priv(mmc);
2291 unsigned long flags;
2292
2293 spin_lock_irqsave(&host->lock, flags);
2294 host->flags |= SDHCI_HS400_TUNING;
2295 spin_unlock_irqrestore(&host->lock, flags);
2296
2297 return 0;
2298}
2299
Ritesh Harjani9ce9ecf2015-05-27 15:32:40 +05302300static int sdhci_enhanced_strobe(struct mmc_host *mmc)
2301{
2302 struct sdhci_host *host = mmc_priv(mmc);
2303 int err = 0;
2304
2305 sdhci_runtime_pm_get(host);
2306 if (host->ops->enhanced_strobe)
2307 err = host->ops->enhanced_strobe(host);
2308 sdhci_runtime_pm_put(host);
2309
2310 return err;
2311}
2312
Girish K S069c9f12012-01-06 09:56:39 +05302313static int sdhci_execute_tuning(struct mmc_host *mmc, u32 opcode)
Arindam Nathb513ea22011-05-05 12:19:04 +05302314{
Russell King4b6f37d2014-04-25 12:59:36 +01002315 struct sdhci_host *host = mmc_priv(mmc);
Arindam Nathb513ea22011-05-05 12:19:04 +05302316 u16 ctrl;
Arindam Nathb513ea22011-05-05 12:19:04 +05302317 int tuning_loop_counter = MAX_TUNING_LOOP;
Arindam Nathb513ea22011-05-05 12:19:04 +05302318 int err = 0;
Aisheng Dong2b35bd82013-12-23 19:13:04 +08002319 unsigned long flags;
Adrian Hunter38e40bf2014-12-05 19:25:30 +02002320 unsigned int tuning_count = 0;
Adrian Hunterb5540ce2014-12-05 19:25:31 +02002321 bool hs400_tuning;
Arindam Nathb513ea22011-05-05 12:19:04 +05302322
Aisheng Dong2b35bd82013-12-23 19:13:04 +08002323 spin_lock_irqsave(&host->lock, flags);
Arindam Nathb513ea22011-05-05 12:19:04 +05302324
Adrian Hunterb5540ce2014-12-05 19:25:31 +02002325 hs400_tuning = host->flags & SDHCI_HS400_TUNING;
2326 host->flags &= ~SDHCI_HS400_TUNING;
2327
Adrian Hunter38e40bf2014-12-05 19:25:30 +02002328 if (host->tuning_mode == SDHCI_TUNING_MODE_1)
2329 tuning_count = host->tuning_count;
2330
Arindam Nathb513ea22011-05-05 12:19:04 +05302331 /*
Weijun Yang9faac7b2015-10-04 12:04:12 +00002332 * The Host Controller needs tuning in case of SDR104 and DDR50
2333 * mode, and for SDR50 mode when Use Tuning for SDR50 is set in
2334 * the Capabilities register.
Girish K S069c9f12012-01-06 09:56:39 +05302335 * If the Host Controller supports the HS200 mode then the
2336 * tuning function has to be executed.
Arindam Nathb513ea22011-05-05 12:19:04 +05302337 */
Russell King4b6f37d2014-04-25 12:59:36 +01002338 switch (host->timing) {
Adrian Hunterb5540ce2014-12-05 19:25:31 +02002339 /* HS400 tuning is done in HS200 mode */
Adrian Huntere9fb05d2014-11-06 15:19:06 +02002340 case MMC_TIMING_MMC_HS400:
Adrian Hunterb5540ce2014-12-05 19:25:31 +02002341 err = -EINVAL;
2342 goto out_unlock;
2343
Russell King4b6f37d2014-04-25 12:59:36 +01002344 case MMC_TIMING_MMC_HS200:
Adrian Hunterb5540ce2014-12-05 19:25:31 +02002345 /*
2346 * Periodic re-tuning for HS400 is not expected to be needed, so
2347 * disable it here.
2348 */
2349 if (hs400_tuning)
2350 tuning_count = 0;
2351 break;
2352
Russell King4b6f37d2014-04-25 12:59:36 +01002353 case MMC_TIMING_UHS_SDR104:
Weijun Yang9faac7b2015-10-04 12:04:12 +00002354 case MMC_TIMING_UHS_DDR50:
Russell King4b6f37d2014-04-25 12:59:36 +01002355 break;
Girish K S069c9f12012-01-06 09:56:39 +05302356
Russell King4b6f37d2014-04-25 12:59:36 +01002357 case MMC_TIMING_UHS_SDR50:
Adrian Hunter4228b212016-04-20 09:24:03 +03002358 if (host->flags & SDHCI_SDR50_NEEDS_TUNING)
Russell King4b6f37d2014-04-25 12:59:36 +01002359 break;
2360 /* FALLTHROUGH */
2361
2362 default:
Adrian Hunterd519c862014-12-05 19:25:29 +02002363 goto out_unlock;
Arindam Nathb513ea22011-05-05 12:19:04 +05302364 }
2365
Dong Aisheng45251812013-09-13 19:11:30 +08002366 if (host->ops->platform_execute_tuning) {
Aisheng Dong2b35bd82013-12-23 19:13:04 +08002367 spin_unlock_irqrestore(&host->lock, flags);
Dong Aisheng45251812013-09-13 19:11:30 +08002368 err = host->ops->platform_execute_tuning(host, opcode);
Dong Aisheng45251812013-09-13 19:11:30 +08002369 return err;
2370 }
2371
Russell King4b6f37d2014-04-25 12:59:36 +01002372 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2373 ctrl |= SDHCI_CTRL_EXEC_TUNING;
Vincent Yang67d0d042015-01-20 16:05:16 +08002374 if (host->quirks2 & SDHCI_QUIRK2_TUNING_WORK_AROUND)
2375 ctrl |= SDHCI_CTRL_TUNED_CLK;
Arindam Nathb513ea22011-05-05 12:19:04 +05302376 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
2377
2378 /*
2379 * As per the Host Controller spec v3.00, tuning command
2380 * generates Buffer Read Ready interrupt, so enable that.
2381 *
2382 * Note: The spec clearly says that when tuning sequence
2383 * is being performed, the controller does not generate
2384 * interrupts other than Buffer Read Ready interrupt. But
2385 * to make sure we don't hit a controller bug, we _only_
2386 * enable Buffer Read Ready interrupt here.
2387 */
Russell Kingb537f942014-04-25 12:56:01 +01002388 sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_INT_ENABLE);
2389 sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_SIGNAL_ENABLE);
Arindam Nathb513ea22011-05-05 12:19:04 +05302390
2391 /*
2392 * Issue CMD19 repeatedly till Execute Tuning is set to 0 or the number
Simon Horman1473bdd2016-05-13 13:24:31 +09002393 * of loops reaches 40 times.
Arindam Nathb513ea22011-05-05 12:19:04 +05302394 */
Arindam Nathb513ea22011-05-05 12:19:04 +05302395 do {
2396 struct mmc_command cmd = {0};
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002397 struct mmc_request mrq = {NULL};
Arindam Nathb513ea22011-05-05 12:19:04 +05302398
Girish K S069c9f12012-01-06 09:56:39 +05302399 cmd.opcode = opcode;
Arindam Nathb513ea22011-05-05 12:19:04 +05302400 cmd.arg = 0;
2401 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
2402 cmd.retries = 0;
2403 cmd.data = NULL;
Adrian Hunter4e9f8fe2016-06-29 16:24:34 +03002404 cmd.mrq = &mrq;
Arindam Nathb513ea22011-05-05 12:19:04 +05302405 cmd.error = 0;
2406
Al Cooper7ce45e92014-05-09 11:34:07 -04002407 if (tuning_loop_counter-- == 0)
2408 break;
2409
Arindam Nathb513ea22011-05-05 12:19:04 +05302410 mrq.cmd = &cmd;
Arindam Nathb513ea22011-05-05 12:19:04 +05302411
2412 /*
2413 * In response to CMD19, the card sends 64 bytes of tuning
2414 * block to the Host Controller. So we set the block size
2415 * to 64 here.
2416 */
Girish K S069c9f12012-01-06 09:56:39 +05302417 if (cmd.opcode == MMC_SEND_TUNING_BLOCK_HS200) {
2418 if (mmc->ios.bus_width == MMC_BUS_WIDTH_8)
Subhash Jadavani3ed97712014-09-02 17:55:54 -07002419 sdhci_set_blk_size_reg(host, 128, 7);
Girish K S069c9f12012-01-06 09:56:39 +05302420 else if (mmc->ios.bus_width == MMC_BUS_WIDTH_4)
Subhash Jadavani3ed97712014-09-02 17:55:54 -07002421 sdhci_set_blk_size_reg(host, 64, 7);
Girish K S069c9f12012-01-06 09:56:39 +05302422 } else {
Subhash Jadavani3ed97712014-09-02 17:55:54 -07002423 sdhci_set_blk_size_reg(host, 64, 7);
Girish K S069c9f12012-01-06 09:56:39 +05302424 }
Arindam Nathb513ea22011-05-05 12:19:04 +05302425
2426 /*
2427 * The tuning block is sent by the card to the host controller.
2428 * So we set the TRNS_READ bit in the Transfer Mode register.
2429 * This also takes care of setting DMA Enable and Multi Block
2430 * Select in the same register to 0.
2431 */
2432 sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE);
2433
2434 sdhci_send_command(host, &cmd);
2435
2436 host->cmd = NULL;
Adrian Hunter07c161b2016-06-29 16:24:38 +03002437 sdhci_del_timer(host, &mrq);
Arindam Nathb513ea22011-05-05 12:19:04 +05302438
Aisheng Dong2b35bd82013-12-23 19:13:04 +08002439 spin_unlock_irqrestore(&host->lock, flags);
Arindam Nathb513ea22011-05-05 12:19:04 +05302440 /* Wait for Buffer Read Ready interrupt */
Christopher Freeman622b5f32016-08-17 13:34:27 -04002441 wait_event_timeout(host->buf_ready_int,
Arindam Nathb513ea22011-05-05 12:19:04 +05302442 (host->tuning_done == 1),
2443 msecs_to_jiffies(50));
Aisheng Dong2b35bd82013-12-23 19:13:04 +08002444 spin_lock_irqsave(&host->lock, flags);
Arindam Nathb513ea22011-05-05 12:19:04 +05302445
2446 if (!host->tuning_done) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01002447 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 +05302448 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2449 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
2450 ctrl &= ~SDHCI_CTRL_EXEC_TUNING;
2451 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
2452
Adrian Huntercee93582016-12-02 15:14:20 +02002453 sdhci_do_reset(host, SDHCI_RESET_CMD);
2454 sdhci_do_reset(host, SDHCI_RESET_DATA);
2455
Arindam Nathb513ea22011-05-05 12:19:04 +05302456 err = -EIO;
Adrian Huntercee93582016-12-02 15:14:20 +02002457
2458 if (cmd.opcode != MMC_SEND_TUNING_BLOCK_HS200)
2459 goto out;
2460
2461 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
2462 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
2463
2464 spin_unlock_irqrestore(&host->lock, flags);
2465
2466 memset(&cmd, 0, sizeof(cmd));
2467 cmd.opcode = MMC_STOP_TRANSMISSION;
2468 cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
2469 cmd.busy_timeout = 50;
2470 mmc_wait_for_cmd(mmc, &cmd, 0);
2471
2472 spin_lock_irqsave(&host->lock, flags);
2473
Arindam Nathb513ea22011-05-05 12:19:04 +05302474 goto out;
2475 }
2476
2477 host->tuning_done = 0;
2478
2479 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
Nick Sanders197160d2014-05-06 18:52:38 -07002480
2481 /* eMMC spec does not require a delay between tuning cycles */
2482 if (opcode == MMC_SEND_TUNING_BLOCK)
2483 mdelay(1);
Arindam Nathb513ea22011-05-05 12:19:04 +05302484 } while (ctrl & SDHCI_CTRL_EXEC_TUNING);
2485
2486 /*
2487 * The Host Driver has exhausted the maximum number of loops allowed,
2488 * so use fixed sampling frequency.
2489 */
Al Cooper7ce45e92014-05-09 11:34:07 -04002490 if (tuning_loop_counter < 0) {
Arindam Nathb513ea22011-05-05 12:19:04 +05302491 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
2492 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Al Cooper7ce45e92014-05-09 11:34:07 -04002493 }
2494 if (!(ctrl & SDHCI_CTRL_TUNED_CLK)) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01002495 pr_info(DRIVER_NAME ": Tuning procedure failed, falling back to fixed sampling clock\n");
Dong Aisheng114f2bf2013-10-18 19:48:45 +08002496 err = -EIO;
Arindam Nathb513ea22011-05-05 12:19:04 +05302497 }
2498
2499out:
Adrian Hunter38e40bf2014-12-05 19:25:30 +02002500 if (tuning_count) {
Adrian Hunter66c39dfc2015-05-07 13:10:21 +03002501 /*
2502 * In case tuning fails, host controllers which support
2503 * re-tuning can try tuning again at a later time, when the
2504 * re-tuning timer expires. So for these controllers, we
2505 * return 0. Since there might be other controllers who do not
2506 * have this capability, we return error for them.
2507 */
2508 err = 0;
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302509 }
2510
Adrian Hunter66c39dfc2015-05-07 13:10:21 +03002511 host->mmc->retune_period = err ? 0 : tuning_count;
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302512
Russell Kingb537f942014-04-25 12:56:01 +01002513 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
2514 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Adrian Hunterd519c862014-12-05 19:25:29 +02002515out_unlock:
Aisheng Dong2b35bd82013-12-23 19:13:04 +08002516 spin_unlock_irqrestore(&host->lock, flags);
Arindam Nathb513ea22011-05-05 12:19:04 +05302517 return err;
2518}
2519
Adrian Huntercb849642015-02-06 14:12:59 +02002520static int sdhci_select_drive_strength(struct mmc_card *card,
2521 unsigned int max_dtr, int host_drv,
2522 int card_drv, int *drv_type)
2523{
2524 struct sdhci_host *host = mmc_priv(card->host);
2525
2526 if (!host->ops->select_drive_strength)
2527 return 0;
2528
2529 return host->ops->select_drive_strength(host, card, max_dtr, host_drv,
2530 card_drv, drv_type);
2531}
Kevin Liu52983382013-01-31 11:31:37 +08002532
2533static void sdhci_enable_preset_value(struct sdhci_host *host, bool enable)
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302534{
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302535 /* Host Controller v3.00 defines preset value registers */
2536 if (host->version < SDHCI_SPEC_300)
2537 return;
2538
Sahitya Tummala314162c2013-04-12 12:11:20 +05302539 if (host->quirks2 & SDHCI_QUIRK2_BROKEN_PRESET_VALUE)
2540 return;
2541
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302542 /*
2543 * We only enable or disable Preset Value if they are not already
2544 * enabled or disabled respectively. Otherwise, we bail out.
2545 */
Russell Kingda91a8f2014-04-25 13:00:12 +01002546 if (host->preset_enabled != enable) {
2547 u16 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2548
2549 if (enable)
2550 ctrl |= SDHCI_CTRL_PRESET_VAL_ENABLE;
2551 else
2552 ctrl &= ~SDHCI_CTRL_PRESET_VAL_ENABLE;
2553
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302554 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Russell Kingda91a8f2014-04-25 13:00:12 +01002555
2556 if (enable)
2557 host->flags |= SDHCI_PV_ENABLED;
2558 else
2559 host->flags &= ~SDHCI_PV_ENABLED;
2560
2561 host->preset_enabled = enable;
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302562 }
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002563}
2564
Haibo Chen348487c2014-12-09 17:04:05 +08002565static void sdhci_post_req(struct mmc_host *mmc, struct mmc_request *mrq,
2566 int err)
2567{
2568 struct sdhci_host *host = mmc_priv(mmc);
2569 struct mmc_data *data = mrq->data;
2570
Russell Kingf48f0392016-01-26 13:40:32 +00002571 if (data->host_cookie != COOKIE_UNMAPPED)
Russell King771a3dc2016-01-26 13:40:53 +00002572 dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
2573 data->flags & MMC_DATA_WRITE ?
2574 DMA_TO_DEVICE : DMA_FROM_DEVICE);
2575
2576 data->host_cookie = COOKIE_UNMAPPED;
Gilad Broner07d92eb2015-09-29 16:57:21 +03002577
2578 if (host->ops->pre_req)
2579 host->ops->pre_req(host, mrq);
Haibo Chen348487c2014-12-09 17:04:05 +08002580}
2581
Haibo Chen348487c2014-12-09 17:04:05 +08002582static void sdhci_pre_req(struct mmc_host *mmc, struct mmc_request *mrq,
2583 bool is_first_req)
2584{
2585 struct sdhci_host *host = mmc_priv(mmc);
2586
Haibo Chend31911b2015-08-25 10:02:11 +08002587 mrq->data->host_cookie = COOKIE_UNMAPPED;
Haibo Chen348487c2014-12-09 17:04:05 +08002588
2589 if (host->flags & SDHCI_REQ_USE_DMA)
Russell King94538e52016-01-26 13:40:37 +00002590 sdhci_pre_dma_transfer(host, mrq->data, COOKIE_PRE_MAPPED);
Haibo Chen348487c2014-12-09 17:04:05 +08002591}
2592
Adrian Hunter5d0d11c2016-06-29 16:24:31 +03002593static inline bool sdhci_has_requests(struct sdhci_host *host)
2594{
2595 return host->cmd || host->data_cmd;
2596}
2597
2598static void sdhci_error_out_mrqs(struct sdhci_host *host, int err)
2599{
2600 if (host->data_cmd) {
2601 host->data_cmd->error = err;
2602 sdhci_finish_mrq(host, host->data_cmd->mrq);
2603 }
2604
2605 if (host->cmd) {
2606 host->cmd->error = err;
2607 sdhci_finish_mrq(host, host->cmd->mrq);
2608 }
2609}
2610
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002611static void sdhci_card_event(struct mmc_host *mmc)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002612{
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002613 struct sdhci_host *host = mmc_priv(mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002614 unsigned long flags;
Krzysztof Kozlowski28367662015-01-05 10:50:15 +01002615 int present;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002616
Christian Daudt722e1282013-06-20 14:26:36 -07002617 /* First check if client has provided their own card event */
2618 if (host->ops->card_event)
2619 host->ops->card_event(host);
2620
Adrian Hunterd3940f22016-06-29 16:24:14 +03002621 present = mmc->ops->get_cd(mmc);
Krzysztof Kozlowski28367662015-01-05 10:50:15 +01002622
Pierre Ossmand129bce2006-03-24 03:18:17 -08002623 spin_lock_irqsave(&host->lock, flags);
2624
Adrian Hunter5d0d11c2016-06-29 16:24:31 +03002625 /* Check sdhci_has_requests() first in case we are runtime suspended */
2626 if (sdhci_has_requests(host) && !present) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302627 pr_err("%s: Card removed during transfer!\n",
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002628 mmc_hostname(host->mmc));
Girish K Sa3c76eb2011-10-11 11:44:09 +05302629 pr_err("%s: Resetting controller.\n",
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002630 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08002631
Russell King03231f92014-04-25 12:57:12 +01002632 sdhci_do_reset(host, SDHCI_RESET_CMD);
2633 sdhci_do_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002634
Adrian Hunter5d0d11c2016-06-29 16:24:31 +03002635 sdhci_error_out_mrqs(host, -ENOMEDIUM);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002636 }
2637
2638 spin_unlock_irqrestore(&host->lock, flags);
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002639}
2640
Dov Levenglick1669f8e2015-07-20 16:50:03 +03002641static void sdhci_detect(struct mmc_host *mmc, bool detected)
2642{
2643 struct sdhci_host *host = mmc_priv(mmc);
2644
2645 if (host->ops->detect)
2646 host->ops->detect(host, detected);
2647}
2648
Gilad Broner07d92eb2015-09-29 16:57:21 +03002649static int sdhci_late_init(struct mmc_host *mmc)
2650{
2651 struct sdhci_host *host = mmc_priv(mmc);
2652
2653 if (host->ops->init)
2654 host->ops->init(host);
2655
2656 return 0;
2657}
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002658static const struct mmc_host_ops sdhci_ops = {
Gilad Broner07d92eb2015-09-29 16:57:21 +03002659 .init = sdhci_late_init,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002660 .request = sdhci_request,
Haibo Chen348487c2014-12-09 17:04:05 +08002661 .post_req = sdhci_post_req,
2662 .pre_req = sdhci_pre_req,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002663 .set_ios = sdhci_set_ios,
Kevin Liu94144a42013-02-28 17:35:53 +08002664 .get_cd = sdhci_get_cd,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002665 .get_ro = sdhci_get_ro,
2666 .hw_reset = sdhci_hw_reset,
2667 .enable_sdio_irq = sdhci_enable_sdio_irq,
2668 .start_signal_voltage_switch = sdhci_start_signal_voltage_switch,
Adrian Hunterb5540ce2014-12-05 19:25:31 +02002669 .prepare_hs400_tuning = sdhci_prepare_hs400_tuning,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002670 .execute_tuning = sdhci_execute_tuning,
Ritesh Harjani9ce9ecf2015-05-27 15:32:40 +05302671 .enhanced_strobe = sdhci_enhanced_strobe,
Adrian Huntercb849642015-02-06 14:12:59 +02002672 .select_drive_strength = sdhci_select_drive_strength,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002673 .card_event = sdhci_card_event,
Kevin Liu20b92a32012-12-17 19:29:26 +08002674 .card_busy = sdhci_card_busy,
Sahitya Tummalac6f48d42013-03-10 07:03:17 +05302675 .enable = sdhci_enable,
2676 .disable = sdhci_disable,
Sujit Reddy Thumma360bbf42013-06-19 20:15:37 +05302677 .notify_load = sdhci_notify_load,
Talel Shenhare68741c2015-06-25 09:33:24 +03002678 .notify_halt = sdhci_notify_halt,
Dov Levenglick1669f8e2015-07-20 16:50:03 +03002679 .detect = sdhci_detect,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002680};
2681
2682/*****************************************************************************\
2683 * *
2684 * Tasklets *
2685 * *
2686\*****************************************************************************/
2687
Adrian Hunter4e9f8fe2016-06-29 16:24:34 +03002688static bool sdhci_request_done(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002689{
Pierre Ossmand129bce2006-03-24 03:18:17 -08002690 unsigned long flags;
2691 struct mmc_request *mrq;
Adrian Hunter4e9f8fe2016-06-29 16:24:34 +03002692 int i;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002693
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002694 spin_lock_irqsave(&host->lock, flags);
2695
Adrian Hunter4e9f8fe2016-06-29 16:24:34 +03002696 for (i = 0; i < SDHCI_MAX_MRQS; i++) {
2697 mrq = host->mrqs_done[i];
Adrian Hunter6ebebea2016-11-02 15:49:08 +02002698 if (mrq)
Adrian Hunter4e9f8fe2016-06-29 16:24:34 +03002699 break;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002700 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002701
Adrian Hunter4e9f8fe2016-06-29 16:24:34 +03002702 if (!mrq) {
2703 spin_unlock_irqrestore(&host->lock, flags);
2704 return true;
2705 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002706
Adrian Hunterd7422fb2016-06-29 16:24:33 +03002707 sdhci_del_timer(host, mrq);
2708
Pierre Ossmand129bce2006-03-24 03:18:17 -08002709 /*
Russell King054cedf2016-01-26 13:40:42 +00002710 * Always unmap the data buffers if they were mapped by
2711 * sdhci_prepare_data() whenever we finish with a request.
2712 * This avoids leaking DMA mappings on error.
2713 */
2714 if (host->flags & SDHCI_REQ_USE_DMA) {
2715 struct mmc_data *data = mrq->data;
2716
2717 if (data && data->host_cookie == COOKIE_MAPPED) {
2718 dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
2719 (data->flags & MMC_DATA_READ) ?
2720 DMA_FROM_DEVICE : DMA_TO_DEVICE);
2721 data->host_cookie = COOKIE_UNMAPPED;
2722 }
2723 }
2724
2725 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08002726 * The controller needs a reset of internal state machines
2727 * upon error conditions.
2728 */
Adrian Hunter0cc563c2016-06-29 16:24:28 +03002729 if (sdhci_needs_reset(host, mrq)) {
Adrian Hunter6ebebea2016-11-02 15:49:08 +02002730 /*
2731 * Do not finish until command and data lines are available for
2732 * reset. Note there can only be one other mrq, so it cannot
2733 * also be in mrqs_done, otherwise host->cmd and host->data_cmd
2734 * would both be null.
2735 */
2736 if (host->cmd || host->data_cmd) {
2737 spin_unlock_irqrestore(&host->lock, flags);
2738 return true;
2739 }
2740
Pierre Ossman645289d2006-06-30 02:22:33 -07002741 /* Some controllers need this kick or reset won't work here */
Andy Shevchenko8213af32013-01-07 16:31:08 +02002742 if (host->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET)
Pierre Ossman645289d2006-06-30 02:22:33 -07002743 /* This is to force an update */
Russell King17710592014-04-25 12:58:55 +01002744 host->ops->set_clock(host, host->clock);
Pierre Ossman645289d2006-06-30 02:22:33 -07002745
2746 /* Spec says we should do both at the same time, but Ricoh
2747 controllers do not like that. */
Adrian Hunter6ebebea2016-11-02 15:49:08 +02002748 sdhci_do_reset(host, SDHCI_RESET_CMD);
2749 sdhci_do_reset(host, SDHCI_RESET_DATA);
Adrian Huntered1563d2016-06-29 16:24:29 +03002750
2751 host->pending_reset = false;
Venkat Gopalakrishnana58f91f2012-09-17 16:00:15 -07002752 } else {
2753 if (host->quirks2 & SDHCI_QUIRK2_RDWR_TX_ACTIVE_EOT)
2754 sdhci_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002755 }
2756
Adrian Hunter4e9f8fe2016-06-29 16:24:34 +03002757 if (!sdhci_has_requests(host))
Pavan Anamula5b761502015-07-23 18:45:37 +05302758 if (!(host->quirks2 & SDHCI_QUIRK2_BROKEN_LED_CONTROL))
2759 sdhci_led_deactivate(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002760
Adrian Hunter6ebebea2016-11-02 15:49:08 +02002761 host->mrqs_done[i] = NULL;
Sahitya Tummala9e7fadb2013-08-07 18:40:29 +05302762 host->auto_cmd_err_sts = 0;
Adrian Hunter6ebebea2016-11-02 15:49:08 +02002763
Pierre Ossman5f25a662006-10-04 02:15:39 -07002764 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002765 spin_unlock_irqrestore(&host->lock, flags);
2766
2767 mmc_request_done(host->mmc, mrq);
Adrian Hunter4e9f8fe2016-06-29 16:24:34 +03002768
2769 return false;
2770}
2771
2772static void sdhci_tasklet_finish(unsigned long param)
2773{
2774 struct sdhci_host *host = (struct sdhci_host *)param;
2775
2776 while (!sdhci_request_done(host))
2777 ;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002778}
2779
2780static void sdhci_timeout_timer(unsigned long data)
2781{
2782 struct sdhci_host *host;
2783 unsigned long flags;
2784
2785 host = (struct sdhci_host*)data;
2786
2787 spin_lock_irqsave(&host->lock, flags);
2788
Adrian Hunterd7422fb2016-06-29 16:24:33 +03002789 if (host->cmd && !sdhci_data_line_cmd(host->cmd)) {
2790 pr_err("%s: Timeout waiting for hardware cmd interrupt.\n",
2791 mmc_hostname(host->mmc));
2792 sdhci_dumpregs(host);
2793
2794 host->cmd->error = -ETIMEDOUT;
2795 sdhci_finish_mrq(host, host->cmd->mrq);
2796 }
2797
2798 mmiowb();
2799 spin_unlock_irqrestore(&host->lock, flags);
2800}
2801
2802static void sdhci_timeout_data_timer(unsigned long data)
2803{
2804 struct sdhci_host *host;
2805 unsigned long flags;
2806
2807 host = (struct sdhci_host *)data;
2808
2809 spin_lock_irqsave(&host->lock, flags);
2810
2811 if (host->data || host->data_cmd ||
2812 (host->cmd && sdhci_data_line_cmd(host->cmd))) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01002813 pr_err("%s: Timeout waiting for hardware interrupt.\n",
2814 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08002815 sdhci_dumpregs(host);
2816
2817 if (host->data) {
Asutosh Das494003b2013-03-20 22:53:40 +05302818 pr_info("%s: bytes to transfer: %d transferred: %d\n",
2819 mmc_hostname(host->mmc),
2820 (host->data->blksz * host->data->blocks),
2821 (sdhci_readw(host, SDHCI_BLOCK_SIZE) & 0xFFF) *
2822 sdhci_readw(host, SDHCI_BLOCK_COUNT));
Pierre Ossman17b04292007-07-22 22:18:46 +02002823 host->data->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002824 sdhci_finish_data(host);
Adrian Hunterd7422fb2016-06-29 16:24:33 +03002825 } else if (host->data_cmd) {
2826 host->data_cmd->error = -ETIMEDOUT;
2827 sdhci_finish_mrq(host, host->data_cmd->mrq);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002828 } else {
Adrian Hunterd7422fb2016-06-29 16:24:33 +03002829 host->cmd->error = -ETIMEDOUT;
2830 sdhci_finish_mrq(host, host->cmd->mrq);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002831 }
2832 }
2833
Pierre Ossman5f25a662006-10-04 02:15:39 -07002834 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002835 spin_unlock_irqrestore(&host->lock, flags);
2836}
2837
2838/*****************************************************************************\
2839 * *
2840 * Interrupt handling *
2841 * *
2842\*****************************************************************************/
2843
Adrian Hunterfc605f12016-10-05 12:11:21 +03002844static void sdhci_cmd_irq(struct sdhci_host *host, u32 intmask)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002845{
Asutosh Das09f36d02013-07-23 16:20:34 +05302846 u16 auto_cmd_status;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002847 if (!host->cmd) {
Adrian Huntered1563d2016-06-29 16:24:29 +03002848 /*
2849 * SDHCI recovers from errors by resetting the cmd and data
2850 * circuits. Until that is done, there very well might be more
2851 * interrupts, so ignore them in that case.
2852 */
2853 if (host->pending_reset)
2854 return;
Marek Vasut2e4456f2015-11-18 10:47:02 +01002855 pr_err("%s: Got command interrupt 0x%08x even though no command operation was in progress.\n",
2856 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002857 sdhci_dumpregs(host);
2858 return;
2859 }
2860
Venkat Gopalakrishnana71fbae2014-06-09 14:00:31 -07002861 trace_mmc_cmd_rw_end(host->cmd->opcode, intmask,
2862 sdhci_readl(host, SDHCI_RESPONSE));
2863
Russell Kingec014cb2016-01-26 13:39:39 +00002864 if (intmask & (SDHCI_INT_TIMEOUT | SDHCI_INT_CRC |
Asutosh Das09f36d02013-07-23 16:20:34 +05302865 SDHCI_INT_END_BIT | SDHCI_INT_INDEX |
2866 SDHCI_INT_AUTO_CMD_ERR)) {
Russell Kingec014cb2016-01-26 13:39:39 +00002867 if (intmask & SDHCI_INT_TIMEOUT)
2868 host->cmd->error = -ETIMEDOUT;
2869 else
2870 host->cmd->error = -EILSEQ;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002871
Asutosh Das09f36d02013-07-23 16:20:34 +05302872 if (intmask & SDHCI_INT_AUTO_CMD_ERR) {
Sahitya Tummala9e7fadb2013-08-07 18:40:29 +05302873 auto_cmd_status = host->auto_cmd_err_sts;
Konstantin Dorfman4b0bcd32015-06-02 17:41:53 +03002874 pr_err_ratelimited("%s: %s: AUTO CMD err sts 0x%08x\n",
Sahitya Tummala9e7fadb2013-08-07 18:40:29 +05302875 mmc_hostname(host->mmc), __func__, auto_cmd_status);
Asutosh Das09f36d02013-07-23 16:20:34 +05302876 if (auto_cmd_status & (SDHCI_AUTO_CMD12_NOT_EXEC |
2877 SDHCI_AUTO_CMD_INDEX_ERR |
2878 SDHCI_AUTO_CMD_ENDBIT_ERR))
2879 host->cmd->error = -EIO;
2880 else if (auto_cmd_status & SDHCI_AUTO_CMD_TIMEOUT_ERR)
2881 host->cmd->error = -ETIMEDOUT;
2882 else if (auto_cmd_status & SDHCI_AUTO_CMD_CRC_ERR)
2883 host->cmd->error = -EILSEQ;
2884 }
2885
Russell King71fcbda2016-01-26 13:39:45 +00002886 /*
2887 * If this command initiates a data phase and a response
2888 * CRC error is signalled, the card can start transferring
2889 * data - the card may have received the command without
2890 * error. We must not terminate the mmc_request early.
2891 *
2892 * If the card did not receive the command or returned an
2893 * error which prevented it sending data, the data phase
2894 * will time out.
2895 */
2896 if (host->cmd->data &&
2897 (intmask & (SDHCI_INT_CRC | SDHCI_INT_TIMEOUT)) ==
2898 SDHCI_INT_CRC) {
2899 host->cmd = NULL;
2900 return;
2901 }
2902
Adrian Huntera6d3bdd2016-06-29 16:24:27 +03002903 sdhci_finish_mrq(host, host->cmd->mrq);
Pierre Ossmane8095172008-07-25 01:09:08 +02002904 return;
2905 }
2906
Pierre Ossmane8095172008-07-25 01:09:08 +02002907 if (intmask & SDHCI_INT_RESPONSE)
Pierre Ossman43b58b32007-07-25 23:15:27 +02002908 sdhci_finish_command(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002909}
2910
George G. Davis0957c332010-02-18 12:32:12 -05002911#ifdef CONFIG_MMC_DEBUG
Adrian Hunter08621b12014-11-04 12:42:38 +02002912static void sdhci_adma_show_error(struct sdhci_host *host)
Ben Dooks6882a8c2009-06-14 13:52:38 +01002913{
2914 const char *name = mmc_hostname(host->mmc);
Adrian Hunter1c3d5f62014-11-04 12:42:41 +02002915 void *desc = host->adma_table;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002916
2917 sdhci_dumpregs(host);
2918
2919 while (true) {
Adrian Huntere57a5f62014-11-04 12:42:46 +02002920 struct sdhci_adma2_64_desc *dma_desc = desc;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002921
Adrian Huntere57a5f62014-11-04 12:42:46 +02002922 if (host->flags & SDHCI_USE_64_BIT_DMA)
2923 DBG("%s: %p: DMA 0x%08x%08x, LEN 0x%04x, Attr=0x%02x\n",
2924 name, desc, le32_to_cpu(dma_desc->addr_hi),
2925 le32_to_cpu(dma_desc->addr_lo),
2926 le16_to_cpu(dma_desc->len),
2927 le16_to_cpu(dma_desc->cmd));
2928 else
2929 DBG("%s: %p: DMA 0x%08x, LEN 0x%04x, Attr=0x%02x\n",
2930 name, desc, le32_to_cpu(dma_desc->addr_lo),
2931 le16_to_cpu(dma_desc->len),
2932 le16_to_cpu(dma_desc->cmd));
Ben Dooks6882a8c2009-06-14 13:52:38 +01002933
Adrian Hunter76fe3792014-11-04 12:42:42 +02002934 desc += host->desc_sz;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002935
Adrian Hunter05452302014-11-04 12:42:45 +02002936 if (dma_desc->cmd & cpu_to_le16(ADMA2_END))
Ben Dooks6882a8c2009-06-14 13:52:38 +01002937 break;
2938 }
2939}
2940#else
Adrian Hunter08621b12014-11-04 12:42:38 +02002941static void sdhci_adma_show_error(struct sdhci_host *host) { }
Ben Dooks6882a8c2009-06-14 13:52:38 +01002942#endif
2943
Pierre Ossmand129bce2006-03-24 03:18:17 -08002944static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
2945{
Girish K S069c9f12012-01-06 09:56:39 +05302946 u32 command;
Asutosh Das494003b2013-03-20 22:53:40 +05302947 bool pr_msg = false;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002948
Venkat Gopalakrishnana71fbae2014-06-09 14:00:31 -07002949 command = SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND));
2950 trace_mmc_data_rw_end(command, intmask);
2951
Arindam Nathb513ea22011-05-05 12:19:04 +05302952 /* CMD19 generates _only_ Buffer Read Ready interrupt */
2953 if (intmask & SDHCI_INT_DATA_AVAIL) {
Girish K S069c9f12012-01-06 09:56:39 +05302954 if (command == MMC_SEND_TUNING_BLOCK ||
2955 command == MMC_SEND_TUNING_BLOCK_HS200) {
Arindam Nathb513ea22011-05-05 12:19:04 +05302956 host->tuning_done = 1;
2957 wake_up(&host->buf_ready_int);
2958 return;
2959 }
2960 }
2961
Pierre Ossmand129bce2006-03-24 03:18:17 -08002962 if (!host->data) {
Adrian Hunter7c89a3d2016-06-29 16:24:23 +03002963 struct mmc_command *data_cmd = host->data_cmd;
2964
Pierre Ossmand129bce2006-03-24 03:18:17 -08002965 /*
Pierre Ossmane8095172008-07-25 01:09:08 +02002966 * The "data complete" interrupt is also used to
2967 * indicate that a busy state has ended. See comment
2968 * above in sdhci_cmd_irq().
Pierre Ossmand129bce2006-03-24 03:18:17 -08002969 */
Adrian Hunter7c89a3d2016-06-29 16:24:23 +03002970 if (data_cmd && (data_cmd->flags & MMC_RSP_BUSY)) {
Matthieu CASTETc5abd5e2014-08-14 16:03:17 +02002971 if (intmask & SDHCI_INT_DATA_TIMEOUT) {
Adrian Hunter69b962a2016-11-02 15:49:09 +02002972 host->data_cmd = NULL;
Adrian Hunter7c89a3d2016-06-29 16:24:23 +03002973 data_cmd->error = -ETIMEDOUT;
Adrian Huntera6d3bdd2016-06-29 16:24:27 +03002974 sdhci_finish_mrq(host, data_cmd->mrq);
Matthieu CASTETc5abd5e2014-08-14 16:03:17 +02002975 return;
2976 }
Pierre Ossmane8095172008-07-25 01:09:08 +02002977 if (intmask & SDHCI_INT_DATA_END) {
Adrian Hunter69b962a2016-11-02 15:49:09 +02002978 host->data_cmd = NULL;
Chanho Mine99783a2014-08-30 12:40:40 +09002979 /*
2980 * Some cards handle busy-end interrupt
2981 * before the command completed, so make
2982 * sure we do things in the proper order.
2983 */
Adrian Hunterea968022016-06-29 16:24:24 +03002984 if (host->cmd == data_cmd)
2985 return;
2986
Adrian Huntera6d3bdd2016-06-29 16:24:27 +03002987 sdhci_finish_mrq(host, data_cmd->mrq);
Pierre Ossmane8095172008-07-25 01:09:08 +02002988 return;
2989 }
Sahitya Tummala87d43942013-04-12 11:49:11 +05302990 if (host->quirks2 &
2991 SDHCI_QUIRK2_IGNORE_DATATOUT_FOR_R1BCMD)
2992 return;
Pierre Ossmane8095172008-07-25 01:09:08 +02002993 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002994
Adrian Huntered1563d2016-06-29 16:24:29 +03002995 /*
2996 * SDHCI recovers from errors by resetting the cmd and data
2997 * circuits. Until that is done, there very well might be more
2998 * interrupts, so ignore them in that case.
2999 */
3000 if (host->pending_reset)
3001 return;
3002
Marek Vasut2e4456f2015-11-18 10:47:02 +01003003 pr_err("%s: Got data interrupt 0x%08x even though no data operation was in progress.\n",
3004 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003005 sdhci_dumpregs(host);
3006
3007 return;
3008 }
3009
3010 if (intmask & SDHCI_INT_DATA_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02003011 host->data->error = -ETIMEDOUT;
Aries Lee22113ef2010-12-15 08:14:24 +01003012 else if (intmask & SDHCI_INT_DATA_END_BIT)
3013 host->data->error = -EILSEQ;
3014 else if ((intmask & SDHCI_INT_DATA_CRC) &&
Venkat Gopalakrishnana71fbae2014-06-09 14:00:31 -07003015 (command != MMC_BUS_TEST_R))
Pierre Ossman17b04292007-07-22 22:18:46 +02003016 host->data->error = -EILSEQ;
Ben Dooks6882a8c2009-06-14 13:52:38 +01003017 else if (intmask & SDHCI_INT_ADMA_ERROR) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05303018 pr_err("%s: ADMA error\n", mmc_hostname(host->mmc));
Adrian Hunter08621b12014-11-04 12:42:38 +02003019 sdhci_adma_show_error(host);
Pierre Ossman2134a922008-06-28 18:28:51 +02003020 host->data->error = -EIO;
Haijun Zhanga4071fb2012-12-04 10:41:28 +08003021 if (host->ops->adma_workaround)
3022 host->ops->adma_workaround(host, intmask);
Ben Dooks6882a8c2009-06-14 13:52:38 +01003023 }
Asutosh Das494003b2013-03-20 22:53:40 +05303024 if (host->data->error) {
3025 if (intmask & (SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT)) {
3026 command = SDHCI_GET_CMD(sdhci_readw(host,
3027 SDHCI_COMMAND));
3028 if ((command != MMC_SEND_TUNING_BLOCK_HS200) &&
3029 (command != MMC_SEND_TUNING_BLOCK))
3030 pr_msg = true;
3031 } else {
3032 pr_msg = true;
3033 }
Sahitya Tummala4c196de2014-10-31 14:00:12 +05303034 if (pr_msg && __ratelimit(&host->dbg_dump_rs)) {
Sahitya Tummala16dabee2013-04-08 12:53:44 +05303035 pr_err("%s: data txfr (0x%08x) error: %d after %lld ms\n",
Asutosh Das494003b2013-03-20 22:53:40 +05303036 mmc_hostname(host->mmc), intmask,
Sahitya Tummala16dabee2013-04-08 12:53:44 +05303037 host->data->error, ktime_to_ms(ktime_sub(
3038 ktime_get(), host->data_start_time)));
Asutosh Das494003b2013-03-20 22:53:40 +05303039 sdhci_dumpregs(host);
3040 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003041 sdhci_finish_data(host);
Asutosh Das494003b2013-03-20 22:53:40 +05303042 } else {
Pierre Ossmana406f5a2006-07-02 16:50:59 +01003043 if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
Pierre Ossmand129bce2006-03-24 03:18:17 -08003044 sdhci_transfer_pio(host);
3045
Pierre Ossman6ba736a2007-05-13 22:39:23 +02003046 /*
3047 * We currently don't do anything fancy with DMA
3048 * boundaries, but as we can't disable the feature
3049 * we need to at least restart the transfer.
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04003050 *
3051 * According to the spec sdhci_readl(host, SDHCI_DMA_ADDRESS)
3052 * should return a valid address to continue from, but as
3053 * some controllers are faulty, don't trust them.
Pierre Ossman6ba736a2007-05-13 22:39:23 +02003054 */
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04003055 if (intmask & SDHCI_INT_DMA_END) {
3056 u32 dmastart, dmanow;
3057 dmastart = sg_dma_address(host->data->sg);
3058 dmanow = dmastart + host->data->bytes_xfered;
3059 /*
3060 * Force update to the next DMA block boundary.
3061 */
3062 dmanow = (dmanow &
3063 ~(SDHCI_DEFAULT_BOUNDARY_SIZE - 1)) +
3064 SDHCI_DEFAULT_BOUNDARY_SIZE;
3065 host->data->bytes_xfered = dmanow - dmastart;
3066 DBG("%s: DMA base 0x%08x, transferred 0x%06x bytes,"
3067 " next 0x%08x\n",
3068 mmc_hostname(host->mmc), dmastart,
3069 host->data->bytes_xfered, dmanow);
3070 sdhci_writel(host, dmanow, SDHCI_DMA_ADDRESS);
3071 }
Pierre Ossman6ba736a2007-05-13 22:39:23 +02003072
Pierre Ossmane538fbe2007-08-12 16:46:32 +02003073 if (intmask & SDHCI_INT_DATA_END) {
Adrian Hunter7c89a3d2016-06-29 16:24:23 +03003074 if (host->cmd == host->data_cmd) {
Pierre Ossmane538fbe2007-08-12 16:46:32 +02003075 /*
3076 * Data managed to finish before the
3077 * command completed. Make sure we do
3078 * things in the proper order.
3079 */
3080 host->data_early = 1;
3081 } else {
3082 sdhci_finish_data(host);
3083 }
3084 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003085 }
3086}
3087
Asutosh Das3621b372014-10-17 16:36:47 +05303088#ifdef CONFIG_MMC_CQ_HCI
Asutosh Dasf67a83f2015-02-27 00:02:02 +05303089static int sdhci_get_cmd_err(u32 intmask)
3090{
3091 if (intmask & SDHCI_INT_TIMEOUT)
3092 return -ETIMEDOUT;
3093 else if (intmask & (SDHCI_INT_CRC | SDHCI_INT_END_BIT |
3094 SDHCI_INT_INDEX))
3095 return -EILSEQ;
3096 return 0;
3097}
3098
3099static int sdhci_get_data_err(u32 intmask)
3100{
3101 if (intmask & SDHCI_INT_DATA_TIMEOUT)
3102 return -ETIMEDOUT;
3103 else if (intmask & (SDHCI_INT_DATA_END_BIT | SDHCI_INT_DATA_CRC))
3104 return -EILSEQ;
3105 else if (intmask & SDHCI_INT_ADMA_ERROR)
3106 return -EIO;
3107 return 0;
3108}
3109
Venkat Gopalakrishnan7c6a56c12015-09-15 12:33:13 -07003110static irqreturn_t sdhci_cmdq_irq(struct sdhci_host *host, u32 intmask)
Asutosh Das3621b372014-10-17 16:36:47 +05303111{
Asutosh Dasf67a83f2015-02-27 00:02:02 +05303112 int err = 0;
Venkat Gopalakrishnan7c6a56c12015-09-15 12:33:13 -07003113 u32 mask = 0;
Asutosh Dasf67a83f2015-02-27 00:02:02 +05303114
3115 if (intmask & SDHCI_INT_CMD_MASK)
3116 err = sdhci_get_cmd_err(intmask);
3117 else if (intmask & SDHCI_INT_DATA_MASK)
3118 err = sdhci_get_data_err(intmask);
3119
Venkat Gopalakrishnan7c6a56c12015-09-15 12:33:13 -07003120 if (err) {
3121 /* Clear the error interrupts */
3122 mask = intmask & SDHCI_INT_ERROR_MASK;
3123 sdhci_writel(host, mask, SDHCI_INT_STATUS);
3124 }
3125
3126 return cmdq_irq(host->mmc, err);
Asutosh Das3621b372014-10-17 16:36:47 +05303127}
3128
3129#else
Venkat Gopalakrishnan7c6a56c12015-09-15 12:33:13 -07003130static irqreturn_t sdhci_cmdq_irq(struct sdhci_host *host, u32 intmask)
Asutosh Das3621b372014-10-17 16:36:47 +05303131{
Venkat Gopalakrishnan7c6a56c12015-09-15 12:33:13 -07003132 pr_err("%s: Received cmdq-irq when disabled !!!!\n",
3133 mmc_hostname(host->mmc));
Asutosh Das3621b372014-10-17 16:36:47 +05303134 return IRQ_NONE;
3135}
3136#endif
3137
David Howells7d12e782006-10-05 14:55:46 +01003138static irqreturn_t sdhci_irq(int irq, void *dev_id)
Pierre Ossmand129bce2006-03-24 03:18:17 -08003139{
Russell King781e9892014-04-25 12:55:46 +01003140 irqreturn_t result = IRQ_NONE;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003141 struct sdhci_host *host = dev_id;
Russell King41005002014-04-25 12:55:36 +01003142 u32 intmask, mask, unexpected = 0;
Russell King781e9892014-04-25 12:55:46 +01003143 int max_loops = 16;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003144
3145 spin_lock(&host->lock);
3146
Russell Kingbe138552014-04-25 12:55:56 +01003147 if (host->runtime_suspended && !sdhci_sdio_irq_enabled(host)) {
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003148 spin_unlock(&host->lock);
Adrian Hunter655bca72014-03-11 10:09:36 +02003149 return IRQ_NONE;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003150 }
3151
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03003152 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
Mark Lord62df67a52007-03-06 13:30:13 +01003153 if (!intmask || intmask == 0xffffffff) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08003154 result = IRQ_NONE;
3155 goto out;
3156 }
3157
Russell King41005002014-04-25 12:55:36 +01003158 do {
Asutosh Das3621b372014-10-17 16:36:47 +05303159 if (host->mmc->card && mmc_card_cmdq(host->mmc->card) &&
3160 !mmc_host_halt(host->mmc)) {
3161 pr_debug("*** %s: cmdq intr: 0x%08x\n",
3162 mmc_hostname(host->mmc),
3163 intmask);
Venkat Gopalakrishnan7c6a56c12015-09-15 12:33:13 -07003164 result = sdhci_cmdq_irq(host, intmask);
Asutosh Dasf67a83f2015-02-27 00:02:02 +05303165 if (result == IRQ_HANDLED)
3166 goto out;
Asutosh Das3621b372014-10-17 16:36:47 +05303167 }
3168
Sahitya Tummala9e7fadb2013-08-07 18:40:29 +05303169 if (intmask & SDHCI_INT_AUTO_CMD_ERR)
3170 host->auto_cmd_err_sts = sdhci_readw(host,
3171 SDHCI_AUTO_CMD_ERR);
Russell King41005002014-04-25 12:55:36 +01003172 /* Clear selected interrupts. */
3173 mask = intmask & (SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK |
3174 SDHCI_INT_BUS_POWER);
3175 sdhci_writel(host, mask, SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003176
Russell King41005002014-04-25 12:55:36 +01003177 DBG("*** %s got interrupt: 0x%08x\n",
3178 mmc_hostname(host->mmc), intmask);
3179
3180 if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
3181 u32 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
3182 SDHCI_CARD_PRESENT;
3183
3184 /*
3185 * There is a observation on i.mx esdhc. INSERT
3186 * bit will be immediately set again when it gets
3187 * cleared, if a card is inserted. We have to mask
3188 * the irq to prevent interrupt storm which will
3189 * freeze the system. And the REMOVE gets the
3190 * same situation.
3191 *
3192 * More testing are needed here to ensure it works
3193 * for other platforms though.
3194 */
Russell Kingb537f942014-04-25 12:56:01 +01003195 host->ier &= ~(SDHCI_INT_CARD_INSERT |
3196 SDHCI_INT_CARD_REMOVE);
3197 host->ier |= present ? SDHCI_INT_CARD_REMOVE :
3198 SDHCI_INT_CARD_INSERT;
3199 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
3200 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Russell King41005002014-04-25 12:55:36 +01003201
3202 sdhci_writel(host, intmask & (SDHCI_INT_CARD_INSERT |
3203 SDHCI_INT_CARD_REMOVE), SDHCI_INT_STATUS);
Russell King3560db82014-04-25 12:55:51 +01003204
3205 host->thread_isr |= intmask & (SDHCI_INT_CARD_INSERT |
3206 SDHCI_INT_CARD_REMOVE);
3207 result = IRQ_WAKE_THREAD;
Russell King41005002014-04-25 12:55:36 +01003208 }
3209
Venkat Gopalakrishnana58f91f2012-09-17 16:00:15 -07003210 if (intmask & SDHCI_INT_CMD_MASK) {
3211 if ((host->quirks2 & SDHCI_QUIRK2_SLOW_INT_CLR) &&
3212 (host->clock <= 400000))
3213 udelay(40);
Adrian Hunterfc605f12016-10-05 12:11:21 +03003214 sdhci_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK);
Venkat Gopalakrishnana58f91f2012-09-17 16:00:15 -07003215 }
Russell King41005002014-04-25 12:55:36 +01003216
Venkat Gopalakrishnana58f91f2012-09-17 16:00:15 -07003217 if (intmask & SDHCI_INT_DATA_MASK) {
3218 if ((host->quirks2 & SDHCI_QUIRK2_SLOW_INT_CLR) &&
3219 (host->clock <= 400000))
3220 udelay(40);
Russell King41005002014-04-25 12:55:36 +01003221 sdhci_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
Venkat Gopalakrishnana58f91f2012-09-17 16:00:15 -07003222 }
Russell King41005002014-04-25 12:55:36 +01003223
3224 if (intmask & SDHCI_INT_BUS_POWER)
3225 pr_err("%s: Card is consuming too much power!\n",
3226 mmc_hostname(host->mmc));
3227
Dong Aishengf37b20e2016-07-12 15:46:17 +08003228 if (intmask & SDHCI_INT_RETUNE)
3229 mmc_retune_needed(host->mmc);
3230
Gabriel Krisman Bertazi04eb7db2017-01-16 12:23:42 -02003231 if ((intmask & SDHCI_INT_CARD_INT) &&
3232 (host->ier & SDHCI_INT_CARD_INT)) {
Russell King781e9892014-04-25 12:55:46 +01003233 sdhci_enable_sdio_irq_nolock(host, false);
3234 host->thread_isr |= SDHCI_INT_CARD_INT;
3235 result = IRQ_WAKE_THREAD;
3236 }
Russell King41005002014-04-25 12:55:36 +01003237
3238 intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE |
3239 SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK |
3240 SDHCI_INT_ERROR | SDHCI_INT_BUS_POWER |
Dong Aishengf37b20e2016-07-12 15:46:17 +08003241 SDHCI_INT_RETUNE | SDHCI_INT_CARD_INT);
Russell King41005002014-04-25 12:55:36 +01003242
3243 if (intmask) {
3244 unexpected |= intmask;
3245 sdhci_writel(host, intmask, SDHCI_INT_STATUS);
3246 }
3247
Russell King781e9892014-04-25 12:55:46 +01003248 if (result == IRQ_NONE)
3249 result = IRQ_HANDLED;
Russell King41005002014-04-25 12:55:36 +01003250
3251 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
Russell King41005002014-04-25 12:55:36 +01003252 } while (intmask && --max_loops);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003253out:
3254 spin_unlock(&host->lock);
3255
Alexander Stein6379b232012-03-14 09:52:10 +01003256 if (unexpected) {
3257 pr_err("%s: Unexpected interrupt 0x%08x.\n",
3258 mmc_hostname(host->mmc), unexpected);
3259 sdhci_dumpregs(host);
3260 }
Pierre Ossmanf75979b2007-09-04 07:59:18 +02003261
Pierre Ossmand129bce2006-03-24 03:18:17 -08003262 return result;
3263}
3264
Russell King781e9892014-04-25 12:55:46 +01003265static irqreturn_t sdhci_thread_irq(int irq, void *dev_id)
3266{
3267 struct sdhci_host *host = dev_id;
3268 unsigned long flags;
3269 u32 isr;
3270
3271 spin_lock_irqsave(&host->lock, flags);
3272 isr = host->thread_isr;
3273 host->thread_isr = 0;
3274 spin_unlock_irqrestore(&host->lock, flags);
3275
Russell King3560db82014-04-25 12:55:51 +01003276 if (isr & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
Adrian Hunterd3940f22016-06-29 16:24:14 +03003277 struct mmc_host *mmc = host->mmc;
3278
3279 mmc->ops->card_event(mmc);
3280 mmc_detect_change(mmc, msecs_to_jiffies(200));
Russell King3560db82014-04-25 12:55:51 +01003281 }
3282
Russell King781e9892014-04-25 12:55:46 +01003283 if (isr & SDHCI_INT_CARD_INT) {
3284 sdio_run_irqs(host->mmc);
3285
3286 spin_lock_irqsave(&host->lock, flags);
3287 if (host->flags & SDHCI_SDIO_IRQ_ENABLED)
3288 sdhci_enable_sdio_irq_nolock(host, true);
3289 spin_unlock_irqrestore(&host->lock, flags);
3290 }
3291
3292 return isr ? IRQ_HANDLED : IRQ_NONE;
3293}
3294
Pierre Ossmand129bce2006-03-24 03:18:17 -08003295/*****************************************************************************\
3296 * *
3297 * Suspend/resume *
3298 * *
3299\*****************************************************************************/
3300
3301#ifdef CONFIG_PM
Ludovic Desroches84d62602016-05-13 15:16:02 +02003302/*
3303 * To enable wakeup events, the corresponding events have to be enabled in
3304 * the Interrupt Status Enable register too. See 'Table 1-6: Wakeup Signal
3305 * Table' in the SD Host Controller Standard Specification.
3306 * It is useless to restore SDHCI_INT_ENABLE state in
3307 * sdhci_disable_irq_wakeups() since it will be set by
3308 * sdhci_enable_card_detection() or sdhci_init().
3309 */
Kevin Liuad080d72013-01-05 17:21:33 +08003310void sdhci_enable_irq_wakeups(struct sdhci_host *host)
3311{
3312 u8 val;
3313 u8 mask = SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE
3314 | SDHCI_WAKE_ON_INT;
Ludovic Desroches84d62602016-05-13 15:16:02 +02003315 u32 irq_val = SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE |
3316 SDHCI_INT_CARD_INT;
Kevin Liuad080d72013-01-05 17:21:33 +08003317
3318 val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
3319 val |= mask ;
3320 /* Avoid fake wake up */
Ludovic Desroches84d62602016-05-13 15:16:02 +02003321 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) {
Kevin Liuad080d72013-01-05 17:21:33 +08003322 val &= ~(SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE);
Ludovic Desroches84d62602016-05-13 15:16:02 +02003323 irq_val &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE);
3324 }
Kevin Liuad080d72013-01-05 17:21:33 +08003325 sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
Ludovic Desroches84d62602016-05-13 15:16:02 +02003326 sdhci_writel(host, irq_val, SDHCI_INT_ENABLE);
Kevin Liuad080d72013-01-05 17:21:33 +08003327}
3328EXPORT_SYMBOL_GPL(sdhci_enable_irq_wakeups);
3329
Fabio Estevam0b10f472014-08-30 14:53:13 -03003330static void sdhci_disable_irq_wakeups(struct sdhci_host *host)
Kevin Liuad080d72013-01-05 17:21:33 +08003331{
3332 u8 val;
3333 u8 mask = SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE
3334 | SDHCI_WAKE_ON_INT;
3335
3336 val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
3337 val &= ~mask;
3338 sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
3339}
Pierre Ossmand129bce2006-03-24 03:18:17 -08003340
Manuel Lauss29495aa2011-11-03 11:09:45 +01003341int sdhci_suspend_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08003342{
Anton Vorontsov7260cf52009-03-17 00:13:48 +03003343 sdhci_disable_card_detection(host);
3344
Adrian Hunter66c39dfc2015-05-07 13:10:21 +03003345 mmc_retune_timer_stop(host->mmc);
Dong Aishengf37b20e2016-07-12 15:46:17 +08003346 if (host->tuning_mode != SDHCI_TUNING_MODE_3)
3347 mmc_retune_needed(host->mmc);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303348
Kevin Liuad080d72013-01-05 17:21:33 +08003349 if (!device_may_wakeup(mmc_dev(host->mmc))) {
Russell Kingb537f942014-04-25 12:56:01 +01003350 host->ier = 0;
3351 sdhci_writel(host, 0, SDHCI_INT_ENABLE);
3352 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Kevin Liuad080d72013-01-05 17:21:33 +08003353 free_irq(host->irq, host);
3354 } else {
3355 sdhci_enable_irq_wakeups(host);
3356 enable_irq_wake(host->irq);
3357 }
Ulf Hansson4ee14ec2013-09-25 14:15:24 +02003358 return 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003359}
3360
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003361EXPORT_SYMBOL_GPL(sdhci_suspend_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003362
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003363int sdhci_resume_host(struct sdhci_host *host)
3364{
Adrian Hunterd3940f22016-06-29 16:24:14 +03003365 struct mmc_host *mmc = host->mmc;
Ulf Hansson4ee14ec2013-09-25 14:15:24 +02003366 int ret = 0;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003367
Richard Röjforsa13abc72009-09-22 16:45:30 -07003368 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003369 if (host->ops->enable_dma)
3370 host->ops->enable_dma(host);
3371 }
3372
Adrian Hunter6308d292012-02-07 14:48:54 +02003373 if ((host->mmc->pm_flags & MMC_PM_KEEP_POWER) &&
3374 (host->quirks2 & SDHCI_QUIRK2_HOST_OFF_CARD_ON)) {
3375 /* Card keeps power but host controller does not */
3376 sdhci_init(host, 0);
3377 host->pwr = 0;
3378 host->clock = 0;
Adrian Hunterd3940f22016-06-29 16:24:14 +03003379 mmc->ops->set_ios(mmc, &mmc->ios);
Adrian Hunter6308d292012-02-07 14:48:54 +02003380 } else {
3381 sdhci_init(host, (host->mmc->pm_flags & MMC_PM_KEEP_POWER));
3382 mmiowb();
3383 }
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003384
Haibo Chen14a7b41642015-09-15 18:32:58 +08003385 if (!device_may_wakeup(mmc_dev(host->mmc))) {
3386 ret = request_threaded_irq(host->irq, sdhci_irq,
3387 sdhci_thread_irq, IRQF_SHARED,
3388 mmc_hostname(host->mmc), host);
3389 if (ret)
3390 return ret;
3391 } else {
3392 sdhci_disable_irq_wakeups(host);
3393 disable_irq_wake(host->irq);
3394 }
3395
Anton Vorontsov7260cf52009-03-17 00:13:48 +03003396 sdhci_enable_card_detection(host);
3397
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08003398 return ret;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003399}
3400
3401EXPORT_SYMBOL_GPL(sdhci_resume_host);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003402
Ritesh Harjani9ce9ecf2015-05-27 15:32:40 +05303403static int sdhci_runtime_pm_get(struct sdhci_host *host)
3404{
3405 return pm_runtime_get_sync(host->mmc->parent);
3406}
3407
Asutosh Das06d9f322014-02-21 11:28:36 +05303408static int sdhci_runtime_pm_put(struct sdhci_host *host)
3409{
3410 pm_runtime_mark_last_busy(host->mmc->parent);
3411 return pm_runtime_put_autosuspend(host->mmc->parent);
3412}
3413
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003414int sdhci_runtime_suspend_host(struct sdhci_host *host)
3415{
3416 unsigned long flags;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003417
Adrian Hunter66c39dfc2015-05-07 13:10:21 +03003418 mmc_retune_timer_stop(host->mmc);
Dong Aishengf37b20e2016-07-12 15:46:17 +08003419 if (host->tuning_mode != SDHCI_TUNING_MODE_3)
3420 mmc_retune_needed(host->mmc);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003421
3422 spin_lock_irqsave(&host->lock, flags);
Russell Kingb537f942014-04-25 12:56:01 +01003423 host->ier &= SDHCI_INT_CARD_INT;
3424 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
3425 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003426 spin_unlock_irqrestore(&host->lock, flags);
3427
Russell King781e9892014-04-25 12:55:46 +01003428 synchronize_hardirq(host->irq);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003429
3430 spin_lock_irqsave(&host->lock, flags);
3431 host->runtime_suspended = true;
3432 spin_unlock_irqrestore(&host->lock, flags);
3433
Markus Pargmann8a125ba2014-06-04 15:24:29 +02003434 return 0;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003435}
3436EXPORT_SYMBOL_GPL(sdhci_runtime_suspend_host);
3437
3438int sdhci_runtime_resume_host(struct sdhci_host *host)
3439{
Adrian Hunterd3940f22016-06-29 16:24:14 +03003440 struct mmc_host *mmc = host->mmc;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003441 unsigned long flags;
Markus Pargmann8a125ba2014-06-04 15:24:29 +02003442 int host_flags = host->flags;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003443
3444 if (host_flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
3445 if (host->ops->enable_dma)
3446 host->ops->enable_dma(host);
3447 }
3448
3449 sdhci_init(host, 0);
3450
3451 /* Force clock and power re-program */
3452 host->pwr = 0;
3453 host->clock = 0;
Adrian Hunterd3940f22016-06-29 16:24:14 +03003454 mmc->ops->start_signal_voltage_switch(mmc, &mmc->ios);
3455 mmc->ops->set_ios(mmc, &mmc->ios);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003456
Kevin Liu52983382013-01-31 11:31:37 +08003457 if ((host_flags & SDHCI_PV_ENABLED) &&
3458 !(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN)) {
3459 spin_lock_irqsave(&host->lock, flags);
3460 sdhci_enable_preset_value(host, true);
3461 spin_unlock_irqrestore(&host->lock, flags);
3462 }
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003463
Adrian Hunter086b0dd2016-11-02 15:49:11 +02003464 if ((mmc->caps2 & MMC_CAP2_HS400_ES) &&
3465 mmc->ops->hs400_enhanced_strobe)
3466 mmc->ops->hs400_enhanced_strobe(mmc, &mmc->ios);
3467
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003468 spin_lock_irqsave(&host->lock, flags);
3469
3470 host->runtime_suspended = false;
3471
3472 /* Enable SDIO IRQ */
Russell Kingef104332014-04-25 12:55:41 +01003473 if (host->flags & SDHCI_SDIO_IRQ_ENABLED)
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003474 sdhci_enable_sdio_irq_nolock(host, true);
3475
3476 /* Enable Card Detection */
3477 sdhci_enable_card_detection(host);
3478
3479 spin_unlock_irqrestore(&host->lock, flags);
3480
Markus Pargmann8a125ba2014-06-04 15:24:29 +02003481 return 0;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003482}
3483EXPORT_SYMBOL_GPL(sdhci_runtime_resume_host);
3484
Rafael J. Wysocki162d6f92014-12-05 03:05:33 +01003485#endif /* CONFIG_PM */
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003486
Pierre Ossmand129bce2006-03-24 03:18:17 -08003487/*****************************************************************************\
3488 * *
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003489 * Device allocation/registration *
Pierre Ossmand129bce2006-03-24 03:18:17 -08003490 * *
3491\*****************************************************************************/
3492
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003493struct sdhci_host *sdhci_alloc_host(struct device *dev,
3494 size_t priv_size)
Pierre Ossmand129bce2006-03-24 03:18:17 -08003495{
Pierre Ossmand129bce2006-03-24 03:18:17 -08003496 struct mmc_host *mmc;
3497 struct sdhci_host *host;
3498
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003499 WARN_ON(dev == NULL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003500
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003501 mmc = mmc_alloc_host(sizeof(struct sdhci_host) + priv_size, dev);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003502 if (!mmc)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003503 return ERR_PTR(-ENOMEM);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003504
3505 host = mmc_priv(mmc);
3506 host->mmc = mmc;
Adrian Hunterbf60e592016-02-09 16:12:35 +02003507 host->mmc_host_ops = sdhci_ops;
3508 mmc->ops = &host->mmc_host_ops;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003509
Adrian Hunter8cb851a2016-06-29 16:24:16 +03003510 host->flags = SDHCI_SIGNALING_330;
3511
Sahitya Tummalaef4de6c2013-05-24 08:47:26 +05303512 spin_lock_init(&host->lock);
Sahitya Tummala4c196de2014-10-31 14:00:12 +05303513 ratelimit_state_init(&host->dbg_dump_rs, SDHCI_DBG_DUMP_RS_INTERVAL,
3514 SDHCI_DBG_DUMP_RS_BURST);
Sahitya Tummalaef4de6c2013-05-24 08:47:26 +05303515
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003516 return host;
3517}
Pierre Ossman8a4da142006-10-04 02:15:40 -07003518
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003519EXPORT_SYMBOL_GPL(sdhci_alloc_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003520
Asutosh Das3621b372014-10-17 16:36:47 +05303521#ifdef CONFIG_MMC_CQ_HCI
3522static void sdhci_cmdq_clear_set_irqs(struct mmc_host *mmc, bool clear)
3523{
3524 struct sdhci_host *host = mmc_priv(mmc);
3525 u32 ier = 0;
3526
3527 ier &= ~SDHCI_INT_ALL_MASK;
3528
3529 if (clear) {
3530 ier = SDHCI_INT_CMDQ_EN | SDHCI_INT_ERROR_MASK;
3531 sdhci_writel(host, ier, SDHCI_INT_ENABLE);
3532 sdhci_writel(host, ier, SDHCI_SIGNAL_ENABLE);
3533 } else {
3534 ier = SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT |
3535 SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT |
3536 SDHCI_INT_INDEX | SDHCI_INT_END_BIT |
3537 SDHCI_INT_CRC | SDHCI_INT_TIMEOUT |
3538 SDHCI_INT_DATA_END | SDHCI_INT_RESPONSE |
3539 SDHCI_INT_AUTO_CMD_ERR;
3540 sdhci_writel(host, ier, SDHCI_INT_ENABLE);
3541 sdhci_writel(host, ier, SDHCI_SIGNAL_ENABLE);
3542 }
3543}
3544
3545static void sdhci_cmdq_set_data_timeout(struct mmc_host *mmc, u32 val)
3546{
3547 struct sdhci_host *host = mmc_priv(mmc);
3548
3549 sdhci_writeb(host, val, SDHCI_TIMEOUT_CONTROL);
3550}
3551
3552static void sdhci_cmdq_dump_vendor_regs(struct mmc_host *mmc)
3553{
3554 struct sdhci_host *host = mmc_priv(mmc);
3555
3556 sdhci_dumpregs(host);
3557}
3558
3559static int sdhci_cmdq_init(struct sdhci_host *host, struct mmc_host *mmc,
3560 bool dma64)
3561{
3562 return cmdq_init(host->cq_host, mmc, dma64);
3563}
3564
3565static void sdhci_cmdq_set_block_size(struct mmc_host *mmc)
3566{
3567 struct sdhci_host *host = mmc_priv(mmc);
3568
3569 sdhci_set_blk_size_reg(host, 512, 0);
3570}
3571
Ritesh Harjani1f6d7622015-07-15 13:31:06 +05303572static void sdhci_enhanced_strobe_mask(struct mmc_host *mmc, bool set)
3573{
3574 struct sdhci_host *host = mmc_priv(mmc);
3575
3576 if (host->ops->enhanced_strobe_mask)
3577 host->ops->enhanced_strobe_mask(host, set);
3578}
3579
Asutosh Das3621b372014-10-17 16:36:47 +05303580static void sdhci_cmdq_clear_set_dumpregs(struct mmc_host *mmc, bool set)
3581{
3582 struct sdhci_host *host = mmc_priv(mmc);
3583
3584 if (host->ops->clear_set_dumpregs)
3585 host->ops->clear_set_dumpregs(host, set);
3586}
Konstantin Dorfman26a6d9b2015-05-31 10:08:29 +03003587
3588static void sdhci_cmdq_post_cqe_halt(struct mmc_host *mmc)
3589{
3590 struct sdhci_host *host = mmc_priv(mmc);
3591
3592 sdhci_writel(host, sdhci_readl(host, SDHCI_INT_ENABLE) |
3593 SDHCI_INT_RESPONSE, SDHCI_INT_ENABLE);
3594 sdhci_writel(host, SDHCI_INT_RESPONSE, SDHCI_INT_STATUS);
3595}
Asutosh Das3621b372014-10-17 16:36:47 +05303596#else
3597static void sdhci_cmdq_clear_set_irqs(struct mmc_host *mmc, bool clear)
3598{
3599
3600}
3601
3602static void sdhci_cmdq_set_data_timeout(struct mmc_host *mmc, u32 val)
3603{
3604
3605}
3606
3607static void sdhci_cmdq_dump_vendor_regs(struct mmc_host *mmc)
3608{
3609
3610}
3611
3612static int sdhci_cmdq_init(struct sdhci_host *host, struct mmc_host *mmc,
3613 bool dma64)
3614{
3615 return -ENOSYS;
3616}
3617
3618static void sdhci_cmdq_set_block_size(struct mmc_host *mmc)
3619{
3620
3621}
3622
Ritesh Harjani1f6d7622015-07-15 13:31:06 +05303623static void sdhci_enhanced_strobe_mask(struct mmc_host *mmc, bool set)
3624{
3625
3626}
3627
Asutosh Das3621b372014-10-17 16:36:47 +05303628static void sdhci_cmdq_clear_set_dumpregs(struct mmc_host *mmc, bool set)
3629{
3630
3631}
3632
Konstantin Dorfman26a6d9b2015-05-31 10:08:29 +03003633static void sdhci_cmdq_post_cqe_halt(struct mmc_host *mmc)
3634{
3635}
Asutosh Das3621b372014-10-17 16:36:47 +05303636#endif
3637
3638static const struct cmdq_host_ops sdhci_cmdq_ops = {
3639 .clear_set_irqs = sdhci_cmdq_clear_set_irqs,
3640 .set_data_timeout = sdhci_cmdq_set_data_timeout,
3641 .dump_vendor_regs = sdhci_cmdq_dump_vendor_regs,
3642 .set_block_size = sdhci_cmdq_set_block_size,
3643 .clear_set_dumpregs = sdhci_cmdq_clear_set_dumpregs,
Ritesh Harjani1f6d7622015-07-15 13:31:06 +05303644 .enhanced_strobe_mask = sdhci_enhanced_strobe_mask,
Konstantin Dorfman26a6d9b2015-05-31 10:08:29 +03003645 .post_cqe_halt = sdhci_cmdq_post_cqe_halt,
Asutosh Das3621b372014-10-17 16:36:47 +05303646};
3647
Alexandre Courbot7b913692016-03-07 11:07:55 +09003648static int sdhci_set_dma_mask(struct sdhci_host *host)
3649{
3650 struct mmc_host *mmc = host->mmc;
3651 struct device *dev = mmc_dev(mmc);
3652 int ret = -EINVAL;
3653
3654 if (host->quirks2 & SDHCI_QUIRK2_BROKEN_64_BIT_DMA)
3655 host->flags &= ~SDHCI_USE_64_BIT_DMA;
3656
3657 /* Try 64-bit mask if hardware is capable of it */
3658 if (host->flags & SDHCI_USE_64_BIT_DMA) {
3659 ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64));
3660 if (ret) {
3661 pr_warn("%s: Failed to set 64-bit DMA mask.\n",
3662 mmc_hostname(mmc));
3663 host->flags &= ~SDHCI_USE_64_BIT_DMA;
3664 }
3665 }
3666
3667 /* 32-bit mask as default & fallback */
3668 if (ret) {
3669 ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
3670 if (ret)
3671 pr_warn("%s: Failed to set 32-bit DMA mask.\n",
3672 mmc_hostname(mmc));
3673 }
3674
3675 return ret;
3676}
3677
Adrian Hunter6132a3b2016-06-29 16:24:18 +03003678void __sdhci_read_caps(struct sdhci_host *host, u16 *ver, u32 *caps, u32 *caps1)
3679{
3680 u16 v;
3681
3682 if (host->read_caps)
3683 return;
3684
3685 host->read_caps = true;
3686
3687 if (debug_quirks)
3688 host->quirks = debug_quirks;
3689
3690 if (debug_quirks2)
3691 host->quirks2 = debug_quirks2;
3692
3693 sdhci_do_reset(host, SDHCI_RESET_ALL);
3694
3695 v = ver ? *ver : sdhci_readw(host, SDHCI_HOST_VERSION);
3696 host->version = (v & SDHCI_SPEC_VER_MASK) >> SDHCI_SPEC_VER_SHIFT;
3697
3698 if (host->quirks & SDHCI_QUIRK_MISSING_CAPS)
3699 return;
3700
3701 host->caps = caps ? *caps : sdhci_readl(host, SDHCI_CAPABILITIES);
3702
3703 if (host->version < SDHCI_SPEC_300)
3704 return;
3705
3706 host->caps1 = caps1 ? *caps1 : sdhci_readl(host, SDHCI_CAPABILITIES_1);
3707}
3708EXPORT_SYMBOL_GPL(__sdhci_read_caps);
3709
Adrian Hunter52f53362016-06-29 16:24:15 +03003710int sdhci_setup_host(struct sdhci_host *host)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003711{
3712 struct mmc_host *mmc;
Arindam Nathf2119df2011-05-05 12:18:57 +05303713 u32 max_current_caps;
3714 unsigned int ocr_avail;
Adrian Hunterf5fa92e2014-09-24 10:27:32 +03003715 unsigned int override_timeout_clk;
Dong Aisheng59241752015-07-22 20:53:07 +08003716 u32 max_clk;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003717 int ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003718
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003719 WARN_ON(host == NULL);
3720 if (host == NULL)
3721 return -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003722
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003723 mmc = host->mmc;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003724
Jon Hunterefba1422016-07-12 14:53:36 +01003725 /*
3726 * If there are external regulators, get them. Note this must be done
3727 * early before resetting the host and reading the capabilities so that
3728 * the host can take the appropriate action if regulators are not
3729 * available.
3730 */
3731 ret = mmc_regulator_get_supply(mmc);
3732 if (ret == -EPROBE_DEFER)
3733 return ret;
3734
Adrian Hunter6132a3b2016-06-29 16:24:18 +03003735 sdhci_read_caps(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003736
Adrian Hunterf5fa92e2014-09-24 10:27:32 +03003737 override_timeout_clk = host->timeout_clk;
3738
Zhangfei Gao85105c52010-08-06 07:10:01 +08003739 if (host->version > SDHCI_SPEC_300) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01003740 pr_err("%s: Unknown controller version (%d). You may experience problems.\n",
3741 mmc_hostname(mmc), host->version);
Pierre Ossman4a965502006-06-30 02:22:29 -07003742 }
3743
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003744 if (host->quirks & SDHCI_QUIRK_FORCE_DMA)
Richard Röjforsa13abc72009-09-22 16:45:30 -07003745 host->flags |= SDHCI_USE_SDMA;
Adrian Hunter28da3582016-06-29 16:24:17 +03003746 else if (!(host->caps & SDHCI_CAN_DO_SDMA))
Richard Röjforsa13abc72009-09-22 16:45:30 -07003747 DBG("Controller doesn't have SDMA capability\n");
Pierre Ossman67435272006-06-30 02:22:31 -07003748 else
Richard Röjforsa13abc72009-09-22 16:45:30 -07003749 host->flags |= SDHCI_USE_SDMA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003750
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003751 if ((host->quirks & SDHCI_QUIRK_BROKEN_DMA) &&
Richard Röjforsa13abc72009-09-22 16:45:30 -07003752 (host->flags & SDHCI_USE_SDMA)) {
Rolf Eike Beercee687c2007-11-02 15:22:30 +01003753 DBG("Disabling DMA as it is marked broken\n");
Richard Röjforsa13abc72009-09-22 16:45:30 -07003754 host->flags &= ~SDHCI_USE_SDMA;
Feng Tang7c168e32007-09-30 12:44:18 +02003755 }
3756
Arindam Nathf2119df2011-05-05 12:18:57 +05303757 if ((host->version >= SDHCI_SPEC_200) &&
Adrian Hunter28da3582016-06-29 16:24:17 +03003758 (host->caps & SDHCI_CAN_DO_ADMA2))
Richard Röjforsa13abc72009-09-22 16:45:30 -07003759 host->flags |= SDHCI_USE_ADMA;
Pierre Ossman2134a922008-06-28 18:28:51 +02003760
3761 if ((host->quirks & SDHCI_QUIRK_BROKEN_ADMA) &&
3762 (host->flags & SDHCI_USE_ADMA)) {
3763 DBG("Disabling ADMA as it is marked broken\n");
3764 host->flags &= ~SDHCI_USE_ADMA;
3765 }
3766
Adrian Huntere57a5f62014-11-04 12:42:46 +02003767 /*
3768 * It is assumed that a 64-bit capable device has set a 64-bit DMA mask
3769 * and *must* do 64-bit DMA. A driver has the opportunity to change
3770 * that during the first call to ->enable_dma(). Similarly
3771 * SDHCI_QUIRK2_BROKEN_64_BIT_DMA must be left to the drivers to
3772 * implement.
3773 */
Adrian Hunter28da3582016-06-29 16:24:17 +03003774 if (host->caps & SDHCI_CAN_64BIT)
Adrian Huntere57a5f62014-11-04 12:42:46 +02003775 host->flags |= SDHCI_USE_64_BIT_DMA;
3776
Richard Röjforsa13abc72009-09-22 16:45:30 -07003777 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Alexandre Courbot7b913692016-03-07 11:07:55 +09003778 ret = sdhci_set_dma_mask(host);
3779
3780 if (!ret && host->ops->enable_dma)
3781 ret = host->ops->enable_dma(host);
3782
3783 if (ret) {
3784 pr_warn("%s: No suitable DMA available - falling back to PIO\n",
3785 mmc_hostname(mmc));
3786 host->flags &= ~(SDHCI_USE_SDMA | SDHCI_USE_ADMA);
3787
3788 ret = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003789 }
3790 }
3791
Adrian Huntere57a5f62014-11-04 12:42:46 +02003792 /* SDMA does not support 64-bit DMA */
3793 if (host->flags & SDHCI_USE_64_BIT_DMA)
3794 host->flags &= ~SDHCI_USE_SDMA;
3795
Pierre Ossman2134a922008-06-28 18:28:51 +02003796 if (host->flags & SDHCI_USE_ADMA) {
Russell Kinge66e61c2016-01-26 13:39:55 +00003797 dma_addr_t dma;
3798 void *buf;
3799
Pierre Ossman2134a922008-06-28 18:28:51 +02003800 /*
Adrian Hunter76fe3792014-11-04 12:42:42 +02003801 * The DMA descriptor table size is calculated as the maximum
3802 * number of segments times 2, to allow for an alignment
3803 * descriptor for each segment, plus 1 for a nop end descriptor,
3804 * all multipled by the descriptor size.
Pierre Ossman2134a922008-06-28 18:28:51 +02003805 */
Adrian Huntere57a5f62014-11-04 12:42:46 +02003806 if (host->flags & SDHCI_USE_64_BIT_DMA) {
3807 host->adma_table_sz = (SDHCI_MAX_SEGS * 2 + 1) *
3808 SDHCI_ADMA2_64_DESC_SZ;
Adrian Huntere57a5f62014-11-04 12:42:46 +02003809 host->desc_sz = SDHCI_ADMA2_64_DESC_SZ;
Adrian Huntere57a5f62014-11-04 12:42:46 +02003810 } else {
3811 host->adma_table_sz = (SDHCI_MAX_SEGS * 2 + 1) *
3812 SDHCI_ADMA2_32_DESC_SZ;
Adrian Huntere57a5f62014-11-04 12:42:46 +02003813 host->desc_sz = SDHCI_ADMA2_32_DESC_SZ;
Adrian Huntere57a5f62014-11-04 12:42:46 +02003814 }
Russell Kinge66e61c2016-01-26 13:39:55 +00003815
Adrian Hunter04a5ae62015-11-26 14:00:49 +02003816 host->align_buffer_sz = SDHCI_MAX_SEGS * SDHCI_ADMA2_ALIGN;
Russell Kinge66e61c2016-01-26 13:39:55 +00003817 buf = dma_alloc_coherent(mmc_dev(mmc), host->align_buffer_sz +
3818 host->adma_table_sz, &dma, GFP_KERNEL);
3819 if (!buf) {
Joe Perches66061102014-09-12 14:56:56 -07003820 pr_warn("%s: Unable to allocate ADMA buffers - falling back to standard DMA\n",
Pierre Ossman2134a922008-06-28 18:28:51 +02003821 mmc_hostname(mmc));
3822 host->flags &= ~SDHCI_USE_ADMA;
Russell Kinge66e61c2016-01-26 13:39:55 +00003823 } else if ((dma + host->align_buffer_sz) &
3824 (SDHCI_ADMA2_DESC_ALIGN - 1)) {
Joe Perches66061102014-09-12 14:56:56 -07003825 pr_warn("%s: unable to allocate aligned ADMA descriptor\n",
3826 mmc_hostname(mmc));
Russell Kingd1e49f72014-04-25 12:58:34 +01003827 host->flags &= ~SDHCI_USE_ADMA;
Russell Kinge66e61c2016-01-26 13:39:55 +00003828 dma_free_coherent(mmc_dev(mmc), host->align_buffer_sz +
3829 host->adma_table_sz, buf, dma);
3830 } else {
3831 host->align_buffer = buf;
3832 host->align_addr = dma;
Russell Kingedd63fc2016-01-26 13:39:50 +00003833
Russell Kinge66e61c2016-01-26 13:39:55 +00003834 host->adma_table = buf + host->align_buffer_sz;
3835 host->adma_addr = dma + host->align_buffer_sz;
3836 }
Pierre Ossman2134a922008-06-28 18:28:51 +02003837 }
3838
Pierre Ossman76591502008-07-21 00:32:11 +02003839 /*
3840 * If we use DMA, then it's up to the caller to set the DMA
3841 * mask, but PIO does not need the hw shim so we set a new
3842 * mask here in that case.
3843 */
Richard Röjforsa13abc72009-09-22 16:45:30 -07003844 if (!(host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))) {
Pierre Ossman76591502008-07-21 00:32:11 +02003845 host->dma_mask = DMA_BIT_MASK(64);
Markus Mayer4e743f12014-07-03 13:27:42 -07003846 mmc_dev(mmc)->dma_mask = &host->dma_mask;
Pierre Ossman76591502008-07-21 00:32:11 +02003847 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003848
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04003849 if (host->version >= SDHCI_SPEC_300)
Adrian Hunter28da3582016-06-29 16:24:17 +03003850 host->max_clk = (host->caps & SDHCI_CLOCK_V3_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04003851 >> SDHCI_CLOCK_BASE_SHIFT;
3852 else
Adrian Hunter28da3582016-06-29 16:24:17 +03003853 host->max_clk = (host->caps & SDHCI_CLOCK_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04003854 >> SDHCI_CLOCK_BASE_SHIFT;
3855
Pierre Ossmand129bce2006-03-24 03:18:17 -08003856 host->max_clk *= 1000000;
Anton Vorontsovf27f47e2010-05-26 14:41:53 -07003857 if (host->max_clk == 0 || host->quirks &
3858 SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN) {
Ben Dooks4240ff02009-03-17 00:13:57 +03003859 if (!host->ops->get_max_clock) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01003860 pr_err("%s: Hardware doesn't specify base clock frequency.\n",
3861 mmc_hostname(mmc));
Adrian Huntereb5c20d2016-04-12 14:25:08 +03003862 ret = -ENODEV;
3863 goto undma;
Ben Dooks4240ff02009-03-17 00:13:57 +03003864 }
3865 host->max_clk = host->ops->get_max_clock(host);
3866 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003867
3868 /*
Arindam Nathc3ed3872011-05-05 12:19:06 +05303869 * In case of Host Controller v3.00, find out whether clock
3870 * multiplier is supported.
3871 */
Adrian Hunter28da3582016-06-29 16:24:17 +03003872 host->clk_mul = (host->caps1 & SDHCI_CLOCK_MUL_MASK) >>
Arindam Nathc3ed3872011-05-05 12:19:06 +05303873 SDHCI_CLOCK_MUL_SHIFT;
3874
3875 /*
3876 * In case the value in Clock Multiplier is 0, then programmable
3877 * clock mode is not supported, otherwise the actual clock
3878 * multiplier is one more than the value of Clock Multiplier
3879 * in the Capabilities Register.
3880 */
3881 if (host->clk_mul)
3882 host->clk_mul += 1;
3883
3884 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08003885 * Set host parameters.
3886 */
Dong Aisheng59241752015-07-22 20:53:07 +08003887 max_clk = host->max_clk;
3888
Marek Szyprowskice5f0362010-08-10 18:01:56 -07003889 if (host->ops->get_min_clock)
Anton Vorontsova9e58f22009-07-29 15:04:16 -07003890 mmc->f_min = host->ops->get_min_clock(host);
Arindam Nathc3ed3872011-05-05 12:19:06 +05303891 else if (host->version >= SDHCI_SPEC_300) {
3892 if (host->clk_mul) {
3893 mmc->f_min = (host->max_clk * host->clk_mul) / 1024;
Dong Aisheng59241752015-07-22 20:53:07 +08003894 max_clk = host->max_clk * host->clk_mul;
Arindam Nathc3ed3872011-05-05 12:19:06 +05303895 } else
3896 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_300;
3897 } else
Zhangfei Gao03975262010-09-20 15:15:18 -04003898 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_200;
Philip Rakity15ec4462010-11-19 16:48:39 -05003899
Adrian Hunterd310ae42016-04-12 14:25:07 +03003900 if (!mmc->f_max || mmc->f_max > max_clk)
Dong Aisheng59241752015-07-22 20:53:07 +08003901 mmc->f_max = max_clk;
3902
Aisheng Dong28aab052014-08-27 15:26:31 +08003903 if (!(host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)) {
Adrian Hunter28da3582016-06-29 16:24:17 +03003904 host->timeout_clk = (host->caps & SDHCI_TIMEOUT_CLK_MASK) >>
Aisheng Dong28aab052014-08-27 15:26:31 +08003905 SDHCI_TIMEOUT_CLK_SHIFT;
3906 if (host->timeout_clk == 0) {
3907 if (host->ops->get_timeout_clock) {
3908 host->timeout_clk =
3909 host->ops->get_timeout_clock(host);
3910 } else {
3911 pr_err("%s: Hardware doesn't specify timeout clock frequency.\n",
3912 mmc_hostname(mmc));
Adrian Huntereb5c20d2016-04-12 14:25:08 +03003913 ret = -ENODEV;
3914 goto undma;
Aisheng Dong28aab052014-08-27 15:26:31 +08003915 }
Andy Shevchenko272308c2011-08-03 18:36:00 +03003916 }
Andy Shevchenko272308c2011-08-03 18:36:00 +03003917
Adrian Hunter28da3582016-06-29 16:24:17 +03003918 if (host->caps & SDHCI_TIMEOUT_CLK_UNIT)
Aisheng Dong28aab052014-08-27 15:26:31 +08003919 host->timeout_clk *= 1000;
Andy Shevchenko272308c2011-08-03 18:36:00 +03003920
Adrian Hunter99513622016-03-07 13:33:55 +02003921 if (override_timeout_clk)
3922 host->timeout_clk = override_timeout_clk;
3923
Aisheng Dong28aab052014-08-27 15:26:31 +08003924 mmc->max_busy_timeout = host->ops->get_max_timeout_count ?
Aisheng Donga6ff5ae2014-08-27 15:26:27 +08003925 host->ops->get_max_timeout_count(host) : 1 << 27;
Aisheng Dong28aab052014-08-27 15:26:31 +08003926 mmc->max_busy_timeout /= host->timeout_clk;
3927 }
Adrian Hunter58d12462011-06-28 17:16:03 +03003928
Andrei Warkentine89d4562011-05-23 15:06:37 -05003929 mmc->caps |= MMC_CAP_SDIO_IRQ | MMC_CAP_ERASE | MMC_CAP_CMD23;
Russell King781e9892014-04-25 12:55:46 +01003930 mmc->caps2 |= MMC_CAP2_SDIO_IRQ_NOTHREAD;
Andrei Warkentine89d4562011-05-23 15:06:37 -05003931
3932 if (host->quirks & SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12)
3933 host->flags |= SDHCI_AUTO_CMD12;
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04003934
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003935 /* Auto-CMD23 stuff only works in ADMA or PIO. */
Andrei Warkentin4f3d3e92011-05-25 10:42:50 -04003936 if ((host->version >= SDHCI_SPEC_300) &&
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003937 ((host->flags & SDHCI_USE_ADMA) ||
Scott Branden3bfa6f02015-02-09 16:06:28 -08003938 !(host->flags & SDHCI_USE_SDMA)) &&
3939 !(host->quirks2 & SDHCI_QUIRK2_ACMD23_BROKEN)) {
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003940 host->flags |= SDHCI_AUTO_CMD23;
3941 DBG("%s: Auto-CMD23 available\n", mmc_hostname(mmc));
3942 } else {
3943 DBG("%s: Auto-CMD23 unavailable\n", mmc_hostname(mmc));
3944 }
3945
Philip Rakity15ec4462010-11-19 16:48:39 -05003946 /*
3947 * A controller may support 8-bit width, but the board itself
3948 * might not have the pins brought out. Boards that support
3949 * 8-bit width must set "mmc->caps |= MMC_CAP_8_BIT_DATA;" in
3950 * their platform code before calling sdhci_add_host(), and we
3951 * won't assume 8-bit width for hosts without that CAP.
3952 */
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04003953 if (!(host->quirks & SDHCI_QUIRK_FORCE_1_BIT_DATA))
Philip Rakity15ec4462010-11-19 16:48:39 -05003954 mmc->caps |= MMC_CAP_4_BIT_DATA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003955
Jerry Huang63ef5d82012-10-25 13:47:19 +08003956 if (host->quirks2 & SDHCI_QUIRK2_HOST_NO_CMD23)
3957 mmc->caps &= ~MMC_CAP_CMD23;
3958
Adrian Hunter28da3582016-06-29 16:24:17 +03003959 if (host->caps & SDHCI_CAN_DO_HISPD)
Zhangfei Gaoa29e7e12010-08-16 21:15:32 -04003960 mmc->caps |= MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED;
Pierre Ossmancd9277c2007-02-18 12:07:47 +01003961
Jaehoon Chung176d1ed2010-09-27 09:42:20 +01003962 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) &&
Jaehoon Chung860951c2016-06-21 10:13:26 +09003963 mmc_card_is_removable(mmc) &&
Guoping Yue3b17cf2014-08-20 16:42:55 +08003964 mmc_gpio_get_cd(host->mmc) < 0 &&
3965 !(mmc->caps2 & MMC_CAP2_NONHOTPLUG))
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03003966 mmc->caps |= MMC_CAP_NEEDS_POLL;
3967
Philip Rakity6231f3d2012-07-23 15:56:23 -07003968 /* If vqmmc regulator and no 1.8V signalling, then there's no UHS */
Tim Kryger3a48edc2014-06-13 10:13:56 -07003969 if (!IS_ERR(mmc->supply.vqmmc)) {
3970 ret = regulator_enable(mmc->supply.vqmmc);
3971 if (!regulator_is_supported_voltage(mmc->supply.vqmmc, 1700000,
3972 1950000))
Adrian Hunter28da3582016-06-29 16:24:17 +03003973 host->caps1 &= ~(SDHCI_SUPPORT_SDR104 |
3974 SDHCI_SUPPORT_SDR50 |
3975 SDHCI_SUPPORT_DDR50);
Chris Balla3361ab2013-03-11 17:51:53 -04003976 if (ret) {
3977 pr_warn("%s: Failed to enable vqmmc regulator: %d\n",
3978 mmc_hostname(mmc), ret);
Adrian Hunter4bb74312014-11-06 15:19:04 +02003979 mmc->supply.vqmmc = ERR_PTR(-EINVAL);
Chris Balla3361ab2013-03-11 17:51:53 -04003980 }
Kevin Liu8363c372012-11-17 17:55:51 -05003981 }
Philip Rakity6231f3d2012-07-23 15:56:23 -07003982
Adrian Hunter28da3582016-06-29 16:24:17 +03003983 if (host->quirks2 & SDHCI_QUIRK2_NO_1_8_V) {
3984 host->caps1 &= ~(SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 |
3985 SDHCI_SUPPORT_DDR50);
3986 }
Daniel Drake6a661802012-11-25 13:01:19 -05003987
Al Cooper4188bba2012-03-16 15:54:17 -04003988 /* Any UHS-I mode in caps implies SDR12 and SDR25 support. */
Adrian Hunter28da3582016-06-29 16:24:17 +03003989 if (host->caps1 & (SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 |
3990 SDHCI_SUPPORT_DDR50))
Arindam Nathf2119df2011-05-05 12:18:57 +05303991 mmc->caps |= MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25;
3992
3993 /* SDR104 supports also implies SDR50 support */
Adrian Hunter28da3582016-06-29 16:24:17 +03003994 if (host->caps1 & SDHCI_SUPPORT_SDR104) {
Arindam Nathf2119df2011-05-05 12:18:57 +05303995 mmc->caps |= MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_SDR50;
Giuseppe CAVALLARO156e14b2013-06-12 08:16:38 +02003996 /* SD3.0: SDR104 is supported so (for eMMC) the caps2
3997 * field can be promoted to support HS200.
3998 */
Adrian Hunter549c0b12014-11-06 15:19:05 +02003999 if (!(host->quirks2 & SDHCI_QUIRK2_BROKEN_HS200))
David Cohen13868bf2013-10-29 10:58:26 -07004000 mmc->caps2 |= MMC_CAP2_HS200;
Adrian Hunter28da3582016-06-29 16:24:17 +03004001 } else if (host->caps1 & SDHCI_SUPPORT_SDR50) {
Arindam Nathf2119df2011-05-05 12:18:57 +05304002 mmc->caps |= MMC_CAP_UHS_SDR50;
Adrian Hunter28da3582016-06-29 16:24:17 +03004003 }
Arindam Nathf2119df2011-05-05 12:18:57 +05304004
Adrian Huntere9fb05d2014-11-06 15:19:06 +02004005 if (host->quirks2 & SDHCI_QUIRK2_CAPS_BIT63_FOR_HS400 &&
Adrian Hunter28da3582016-06-29 16:24:17 +03004006 (host->caps1 & SDHCI_SUPPORT_HS400))
Adrian Huntere9fb05d2014-11-06 15:19:06 +02004007 mmc->caps2 |= MMC_CAP2_HS400;
4008
Adrian Hunter549c0b12014-11-06 15:19:05 +02004009 if ((mmc->caps2 & MMC_CAP2_HSX00_1_2V) &&
4010 (IS_ERR(mmc->supply.vqmmc) ||
4011 !regulator_is_supported_voltage(mmc->supply.vqmmc, 1100000,
4012 1300000)))
4013 mmc->caps2 &= ~MMC_CAP2_HSX00_1_2V;
4014
Adrian Hunter28da3582016-06-29 16:24:17 +03004015 if ((host->caps1 & SDHCI_SUPPORT_DDR50) &&
4016 !(host->quirks2 & SDHCI_QUIRK2_BROKEN_DDR50))
Arindam Nathf2119df2011-05-05 12:18:57 +05304017 mmc->caps |= MMC_CAP_UHS_DDR50;
4018
Girish K S069c9f12012-01-06 09:56:39 +05304019 /* Does the host need tuning for SDR50? */
Adrian Hunter28da3582016-06-29 16:24:17 +03004020 if (host->caps1 & SDHCI_USE_SDR50_TUNING)
Arindam Nathb513ea22011-05-05 12:19:04 +05304021 host->flags |= SDHCI_SDR50_NEEDS_TUNING;
4022
Arindam Nathd6d50a12011-05-05 12:18:59 +05304023 /* Driver Type(s) (A, C, D) supported by the host */
Adrian Hunter28da3582016-06-29 16:24:17 +03004024 if (host->caps1 & SDHCI_DRIVER_TYPE_A)
Arindam Nathd6d50a12011-05-05 12:18:59 +05304025 mmc->caps |= MMC_CAP_DRIVER_TYPE_A;
Adrian Hunter28da3582016-06-29 16:24:17 +03004026 if (host->caps1 & SDHCI_DRIVER_TYPE_C)
Arindam Nathd6d50a12011-05-05 12:18:59 +05304027 mmc->caps |= MMC_CAP_DRIVER_TYPE_C;
Adrian Hunter28da3582016-06-29 16:24:17 +03004028 if (host->caps1 & SDHCI_DRIVER_TYPE_D)
Arindam Nathd6d50a12011-05-05 12:18:59 +05304029 mmc->caps |= MMC_CAP_DRIVER_TYPE_D;
4030
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05304031 /* Initial value for re-tuning timer count */
Adrian Hunter28da3582016-06-29 16:24:17 +03004032 host->tuning_count = (host->caps1 & SDHCI_RETUNING_TIMER_COUNT_MASK) >>
4033 SDHCI_RETUNING_TIMER_COUNT_SHIFT;
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05304034
4035 /*
4036 * In case Re-tuning Timer is not disabled, the actual value of
4037 * re-tuning timer will be 2 ^ (n - 1).
4038 */
4039 if (host->tuning_count)
4040 host->tuning_count = 1 << (host->tuning_count - 1);
4041
4042 /* Re-tuning mode supported by the Host Controller */
Adrian Hunter28da3582016-06-29 16:24:17 +03004043 host->tuning_mode = (host->caps1 & SDHCI_RETUNING_MODE_MASK) >>
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05304044 SDHCI_RETUNING_MODE_SHIFT;
4045
Takashi Iwai8f230f42010-12-08 10:04:30 +01004046 ocr_avail = 0;
Philip Rakitybad37e12012-05-27 18:36:44 -07004047
Arindam Nathf2119df2011-05-05 12:18:57 +05304048 /*
4049 * According to SD Host Controller spec v3.00, if the Host System
4050 * can afford more than 150mA, Host Driver should set XPC to 1. Also
4051 * the value is meaningful only if Voltage Support in the Capabilities
4052 * register is set. The actual current value is 4 times the register
4053 * value.
4054 */
4055 max_current_caps = sdhci_readl(host, SDHCI_MAX_CURRENT);
Tim Kryger3a48edc2014-06-13 10:13:56 -07004056 if (!max_current_caps && !IS_ERR(mmc->supply.vmmc)) {
Chuanxiao.Dongae906032014-08-01 14:00:13 +08004057 int curr = regulator_get_current_limit(mmc->supply.vmmc);
Philip Rakitybad37e12012-05-27 18:36:44 -07004058 if (curr > 0) {
4059
4060 /* convert to SDHCI_MAX_CURRENT format */
4061 curr = curr/1000; /* convert to mA */
4062 curr = curr/SDHCI_MAX_CURRENT_MULTIPLIER;
4063
4064 curr = min_t(u32, curr, SDHCI_MAX_CURRENT_LIMIT);
4065 max_current_caps =
4066 (curr << SDHCI_MAX_CURRENT_330_SHIFT) |
4067 (curr << SDHCI_MAX_CURRENT_300_SHIFT) |
4068 (curr << SDHCI_MAX_CURRENT_180_SHIFT);
4069 }
4070 }
Arindam Nathf2119df2011-05-05 12:18:57 +05304071
Adrian Hunter28da3582016-06-29 16:24:17 +03004072 if (host->caps & SDHCI_CAN_VDD_330) {
Takashi Iwai8f230f42010-12-08 10:04:30 +01004073 ocr_avail |= MMC_VDD_32_33 | MMC_VDD_33_34;
Arindam Nathf2119df2011-05-05 12:18:57 +05304074
Aaron Lu55c46652012-07-04 13:31:48 +08004075 mmc->max_current_330 = ((max_current_caps &
Arindam Nathf2119df2011-05-05 12:18:57 +05304076 SDHCI_MAX_CURRENT_330_MASK) >>
4077 SDHCI_MAX_CURRENT_330_SHIFT) *
4078 SDHCI_MAX_CURRENT_MULTIPLIER;
Arindam Nathf2119df2011-05-05 12:18:57 +05304079 }
Adrian Hunter28da3582016-06-29 16:24:17 +03004080 if (host->caps & SDHCI_CAN_VDD_300) {
Takashi Iwai8f230f42010-12-08 10:04:30 +01004081 ocr_avail |= MMC_VDD_29_30 | MMC_VDD_30_31;
Arindam Nathf2119df2011-05-05 12:18:57 +05304082
Aaron Lu55c46652012-07-04 13:31:48 +08004083 mmc->max_current_300 = ((max_current_caps &
Arindam Nathf2119df2011-05-05 12:18:57 +05304084 SDHCI_MAX_CURRENT_300_MASK) >>
4085 SDHCI_MAX_CURRENT_300_SHIFT) *
4086 SDHCI_MAX_CURRENT_MULTIPLIER;
Arindam Nathf2119df2011-05-05 12:18:57 +05304087 }
Adrian Hunter28da3582016-06-29 16:24:17 +03004088 if (host->caps & SDHCI_CAN_VDD_180) {
Takashi Iwai8f230f42010-12-08 10:04:30 +01004089 ocr_avail |= MMC_VDD_165_195;
4090
Aaron Lu55c46652012-07-04 13:31:48 +08004091 mmc->max_current_180 = ((max_current_caps &
Arindam Nathf2119df2011-05-05 12:18:57 +05304092 SDHCI_MAX_CURRENT_180_MASK) >>
4093 SDHCI_MAX_CURRENT_180_SHIFT) *
4094 SDHCI_MAX_CURRENT_MULTIPLIER;
Arindam Nathf2119df2011-05-05 12:18:57 +05304095 }
4096
Ulf Hansson5fd26c72015-06-05 11:40:08 +02004097 /* If OCR set by host, use it instead. */
4098 if (host->ocr_mask)
4099 ocr_avail = host->ocr_mask;
4100
4101 /* If OCR set by external regulators, give it highest prio. */
Tim Kryger3a48edc2014-06-13 10:13:56 -07004102 if (mmc->ocr_avail)
Tim Kryger52221612014-06-25 00:25:34 -07004103 ocr_avail = mmc->ocr_avail;
Tim Kryger3a48edc2014-06-13 10:13:56 -07004104
Takashi Iwai8f230f42010-12-08 10:04:30 +01004105 mmc->ocr_avail = ocr_avail;
4106 mmc->ocr_avail_sdio = ocr_avail;
4107 if (host->ocr_avail_sdio)
4108 mmc->ocr_avail_sdio &= host->ocr_avail_sdio;
4109 mmc->ocr_avail_sd = ocr_avail;
4110 if (host->ocr_avail_sd)
4111 mmc->ocr_avail_sd &= host->ocr_avail_sd;
4112 else /* normal SD controllers don't support 1.8V */
4113 mmc->ocr_avail_sd &= ~MMC_VDD_165_195;
4114 mmc->ocr_avail_mmc = ocr_avail;
4115 if (host->ocr_avail_mmc)
4116 mmc->ocr_avail_mmc &= host->ocr_avail_mmc;
Pierre Ossman146ad662006-06-30 02:22:23 -07004117
4118 if (mmc->ocr_avail == 0) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01004119 pr_err("%s: Hardware doesn't report any support voltages.\n",
4120 mmc_hostname(mmc));
Adrian Huntereb5c20d2016-04-12 14:25:08 +03004121 ret = -ENODEV;
4122 goto unreg;
Pierre Ossman146ad662006-06-30 02:22:23 -07004123 }
4124
Adrian Hunter8cb851a2016-06-29 16:24:16 +03004125 if ((mmc->caps & (MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 |
4126 MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR104 |
4127 MMC_CAP_UHS_DDR50 | MMC_CAP_1_8V_DDR)) ||
4128 (mmc->caps2 & (MMC_CAP2_HS200_1_8V_SDR | MMC_CAP2_HS400_1_8V)))
4129 host->flags |= SDHCI_SIGNALING_180;
4130
4131 if (mmc->caps2 & MMC_CAP2_HSX00_1_2V)
4132 host->flags |= SDHCI_SIGNALING_120;
4133
Pierre Ossmand129bce2006-03-24 03:18:17 -08004134 /*
Pierre Ossman2134a922008-06-28 18:28:51 +02004135 * Maximum number of segments. Depends on if the hardware
4136 * can do scatter/gather or not.
Pierre Ossmand129bce2006-03-24 03:18:17 -08004137 */
Pierre Ossman2134a922008-06-28 18:28:51 +02004138 if (host->flags & SDHCI_USE_ADMA)
Adrian Hunter4fb213f2014-11-04 12:42:43 +02004139 mmc->max_segs = SDHCI_MAX_SEGS;
Richard Röjforsa13abc72009-09-22 16:45:30 -07004140 else if (host->flags & SDHCI_USE_SDMA)
Martin K. Petersena36274e2010-09-10 01:33:59 -04004141 mmc->max_segs = 1;
Pierre Ossman2134a922008-06-28 18:28:51 +02004142 else /* PIO */
Adrian Hunter4fb213f2014-11-04 12:42:43 +02004143 mmc->max_segs = SDHCI_MAX_SEGS;
Pierre Ossmand129bce2006-03-24 03:18:17 -08004144
4145 /*
Adrian Hunterac005312014-12-05 19:25:28 +02004146 * Maximum number of sectors in one transfer. Limited by SDMA boundary
4147 * size (512KiB). Note some tuning modes impose a 4MiB limit, but this
4148 * is less anyway.
Pierre Ossmand129bce2006-03-24 03:18:17 -08004149 */
Pierre Ossman55db8902006-11-21 17:55:45 +01004150 mmc->max_req_size = 524288;
Pierre Ossmand129bce2006-03-24 03:18:17 -08004151
4152 /*
4153 * Maximum segment size. Could be one segment with the maximum number
Pierre Ossman2134a922008-06-28 18:28:51 +02004154 * of bytes. When doing hardware scatter/gather, each entry cannot
4155 * be larger than 64 KiB though.
Pierre Ossmand129bce2006-03-24 03:18:17 -08004156 */
Olof Johansson30652aa2011-01-01 18:37:32 -06004157 if (host->flags & SDHCI_USE_ADMA) {
4158 if (host->quirks & SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC)
4159 mmc->max_seg_size = 65535;
4160 else
4161 mmc->max_seg_size = 65536;
4162 } else {
Pierre Ossman2134a922008-06-28 18:28:51 +02004163 mmc->max_seg_size = mmc->max_req_size;
Olof Johansson30652aa2011-01-01 18:37:32 -06004164 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08004165
4166 /*
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01004167 * Maximum block size. This varies from controller to controller and
4168 * is specified in the capabilities register.
4169 */
Anton Vorontsov0633f652009-03-17 00:14:03 +03004170 if (host->quirks & SDHCI_QUIRK_FORCE_BLK_SZ_2048) {
4171 mmc->max_blk_size = 2;
4172 } else {
Adrian Hunter28da3582016-06-29 16:24:17 +03004173 mmc->max_blk_size = (host->caps & SDHCI_MAX_BLOCK_MASK) >>
Anton Vorontsov0633f652009-03-17 00:14:03 +03004174 SDHCI_MAX_BLOCK_SHIFT;
4175 if (mmc->max_blk_size >= 3) {
Joe Perches66061102014-09-12 14:56:56 -07004176 pr_warn("%s: Invalid maximum block size, assuming 512 bytes\n",
4177 mmc_hostname(mmc));
Anton Vorontsov0633f652009-03-17 00:14:03 +03004178 mmc->max_blk_size = 0;
4179 }
4180 }
4181
4182 mmc->max_blk_size = 512 << mmc->max_blk_size;
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01004183
4184 /*
Pierre Ossman55db8902006-11-21 17:55:45 +01004185 * Maximum block count.
4186 */
Ben Dooks1388eef2009-06-14 12:40:53 +01004187 mmc->max_blk_count = (host->quirks & SDHCI_QUIRK_NO_MULTIBLOCK) ? 1 : 65535;
Pierre Ossman55db8902006-11-21 17:55:45 +01004188
Adrian Hunter52f53362016-06-29 16:24:15 +03004189 return 0;
4190
4191unreg:
4192 if (!IS_ERR(mmc->supply.vqmmc))
4193 regulator_disable(mmc->supply.vqmmc);
4194undma:
4195 if (host->align_buffer)
4196 dma_free_coherent(mmc_dev(mmc), host->align_buffer_sz +
4197 host->adma_table_sz, host->align_buffer,
4198 host->align_addr);
4199 host->adma_table = NULL;
4200 host->align_buffer = NULL;
4201
4202 return ret;
4203}
4204EXPORT_SYMBOL_GPL(sdhci_setup_host);
4205
4206int __sdhci_add_host(struct sdhci_host *host)
4207{
4208 struct mmc_host *mmc = host->mmc;
4209 int ret;
4210
Pierre Ossman55db8902006-11-21 17:55:45 +01004211 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08004212 * Init tasklets.
4213 */
Pierre Ossmand129bce2006-03-24 03:18:17 -08004214 tasklet_init(&host->finish_tasklet,
4215 sdhci_tasklet_finish, (unsigned long)host);
4216
Al Viroe4cad1b2006-10-10 22:47:07 +01004217 setup_timer(&host->timer, sdhci_timeout_timer, (unsigned long)host);
Adrian Hunterd7422fb2016-06-29 16:24:33 +03004218 setup_timer(&host->data_timer, sdhci_timeout_data_timer,
4219 (unsigned long)host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08004220
Adrian Hunter250fb7b42014-12-05 19:41:10 +02004221 init_waitqueue_head(&host->buf_ready_int);
Arindam Nathb513ea22011-05-05 12:19:04 +05304222
Shawn Guo2af502c2013-07-05 14:38:55 +08004223 sdhci_init(host, 0);
4224
Russell King781e9892014-04-25 12:55:46 +01004225 ret = request_threaded_irq(host->irq, sdhci_irq, sdhci_thread_irq,
4226 IRQF_SHARED, mmc_hostname(mmc), host);
Mark Brown0fc81ee2012-07-02 14:26:15 +01004227 if (ret) {
4228 pr_err("%s: Failed to request IRQ %d: %d\n",
4229 mmc_hostname(mmc), host->irq, ret);
Pierre Ossman8ef1a142006-06-30 02:22:21 -07004230 goto untasklet;
Mark Brown0fc81ee2012-07-02 14:26:15 +01004231 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08004232
Pierre Ossmand129bce2006-03-24 03:18:17 -08004233#ifdef CONFIG_MMC_DEBUG
4234 sdhci_dumpregs(host);
4235#endif
4236
Pavan Anamula5b761502015-07-23 18:45:37 +05304237 if (!(host->quirks2 & SDHCI_QUIRK2_BROKEN_LED_CONTROL)) {
4238 ret = sdhci_led_register(host);
4239 if (ret) {
4240 pr_err("%s: Failed to register LED device: %d\n",
4241 mmc_hostname(mmc), ret);
4242 goto unirq;
4243 }
Mark Brown0fc81ee2012-07-02 14:26:15 +01004244 }
Pierre Ossman2f730fe2008-03-17 10:29:38 +01004245
Pierre Ossman5f25a662006-10-04 02:15:39 -07004246 mmiowb();
4247
Asutosh Das214b9662013-06-13 14:27:42 +05304248 if (host->quirks2 & SDHCI_QUIRK2_IGN_DATA_END_BIT_ERROR) {
4249 host->ier = (host->ier & ~SDHCI_INT_DATA_END_BIT);
4250 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
4251 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
4252 }
4253
Asutosh Das3621b372014-10-17 16:36:47 +05304254 if (mmc->caps2 & MMC_CAP2_CMD_QUEUE) {
4255 bool dma64 = (host->flags & SDHCI_USE_ADMA_64BIT) ?
4256 true : false;
4257 ret = sdhci_cmdq_init(host, mmc, dma64);
4258 if (ret)
4259 pr_err("%s: CMDQ init: failed (%d)\n",
4260 mmc_hostname(host->mmc), ret);
4261 else
4262 host->cq_host->ops = &sdhci_cmdq_ops;
4263 }
4264
4265 pr_info("%s: SDHCI controller on %s [%s] using %s in %s mode\n",
4266 mmc_hostname(mmc), host->hw_name, dev_name(mmc_dev(mmc)),
Adrian Huntere57a5f62014-11-04 12:42:46 +02004267 (host->flags & SDHCI_USE_ADMA) ?
Asutosh Das3621b372014-10-17 16:36:47 +05304268 ((host->flags & SDHCI_USE_ADMA_64BIT) ?
4269 "64-bit ADMA" : "32-bit ADMA") :
4270 ((host->flags & SDHCI_USE_SDMA) ? "DMA" : "PIO"),
4271 ((mmc->caps2 & MMC_CAP2_CMD_QUEUE) && !ret) ?
4272 "CMDQ" : "legacy");
Pierre Ossmand129bce2006-03-24 03:18:17 -08004273
Anton Vorontsov7260cf52009-03-17 00:13:48 +03004274 sdhci_enable_card_detection(host);
4275
Venkat Gopalakrishnan0c3a9e42014-12-15 17:45:03 -08004276 ret = mmc_add_host(mmc);
4277 if (ret)
4278 goto unled;
4279
Pierre Ossmand129bce2006-03-24 03:18:17 -08004280 return 0;
4281
Adrian Huntereb5c20d2016-04-12 14:25:08 +03004282unled:
Pavan Anamula5b761502015-07-23 18:45:37 +05304283 if (!(host->quirks2 & SDHCI_QUIRK2_BROKEN_LED_CONTROL))
4284 sdhci_led_unregister(host);
Adrian Huntereb5c20d2016-04-12 14:25:08 +03004285unirq:
Russell King03231f92014-04-25 12:57:12 +01004286 sdhci_do_reset(host, SDHCI_RESET_ALL);
Russell Kingb537f942014-04-25 12:56:01 +01004287 sdhci_writel(host, 0, SDHCI_INT_ENABLE);
4288 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01004289 free_irq(host->irq, host);
Pierre Ossman8ef1a142006-06-30 02:22:21 -07004290untasklet:
Pierre Ossmand129bce2006-03-24 03:18:17 -08004291 tasklet_kill(&host->finish_tasklet);
Adrian Hunter52f53362016-06-29 16:24:15 +03004292
Adrian Huntereb5c20d2016-04-12 14:25:08 +03004293 if (!IS_ERR(mmc->supply.vqmmc))
4294 regulator_disable(mmc->supply.vqmmc);
Adrian Hunter52f53362016-06-29 16:24:15 +03004295
Adrian Huntereb5c20d2016-04-12 14:25:08 +03004296 if (host->align_buffer)
4297 dma_free_coherent(mmc_dev(mmc), host->align_buffer_sz +
4298 host->adma_table_sz, host->align_buffer,
4299 host->align_addr);
4300 host->adma_table = NULL;
4301 host->align_buffer = NULL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08004302
4303 return ret;
4304}
Adrian Hunter52f53362016-06-29 16:24:15 +03004305EXPORT_SYMBOL_GPL(__sdhci_add_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08004306
Adrian Hunter52f53362016-06-29 16:24:15 +03004307int sdhci_add_host(struct sdhci_host *host)
4308{
4309 int ret;
4310
4311 ret = sdhci_setup_host(host);
4312 if (ret)
4313 return ret;
4314
4315 return __sdhci_add_host(host);
4316}
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01004317EXPORT_SYMBOL_GPL(sdhci_add_host);
4318
Pierre Ossman1e728592008-04-16 19:13:13 +02004319void sdhci_remove_host(struct sdhci_host *host, int dead)
Pierre Ossmand129bce2006-03-24 03:18:17 -08004320{
Tim Kryger3a48edc2014-06-13 10:13:56 -07004321 struct mmc_host *mmc = host->mmc;
Pierre Ossman1e728592008-04-16 19:13:13 +02004322 unsigned long flags;
4323
4324 if (dead) {
4325 spin_lock_irqsave(&host->lock, flags);
4326
4327 host->flags |= SDHCI_DEVICE_DEAD;
4328
Adrian Hunter5d0d11c2016-06-29 16:24:31 +03004329 if (sdhci_has_requests(host)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05304330 pr_err("%s: Controller removed during "
Markus Mayer4e743f12014-07-03 13:27:42 -07004331 " transfer!\n", mmc_hostname(mmc));
Adrian Hunter5d0d11c2016-06-29 16:24:31 +03004332 sdhci_error_out_mrqs(host, -ENOMEDIUM);
Pierre Ossman1e728592008-04-16 19:13:13 +02004333 }
4334
4335 spin_unlock_irqrestore(&host->lock, flags);
4336 }
4337
Anton Vorontsov7260cf52009-03-17 00:13:48 +03004338 sdhci_disable_card_detection(host);
4339
Sahitya Tummalac6f48d42013-03-10 07:03:17 +05304340 mmc_remove_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08004341
Pavan Anamula5b761502015-07-23 18:45:37 +05304342 if (!(host->quirks2 & SDHCI_QUIRK2_BROKEN_LED_CONTROL))
4343 sdhci_led_unregister(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01004344
Pierre Ossman1e728592008-04-16 19:13:13 +02004345 if (!dead)
Russell King03231f92014-04-25 12:57:12 +01004346 sdhci_do_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08004347
Russell Kingb537f942014-04-25 12:56:01 +01004348 sdhci_writel(host, 0, SDHCI_INT_ENABLE);
4349 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08004350 free_irq(host->irq, host);
4351
4352 del_timer_sync(&host->timer);
Adrian Hunterd7422fb2016-06-29 16:24:33 +03004353 del_timer_sync(&host->data_timer);
Pierre Ossmand129bce2006-03-24 03:18:17 -08004354
Pierre Ossmand129bce2006-03-24 03:18:17 -08004355 tasklet_kill(&host->finish_tasklet);
Pierre Ossman2134a922008-06-28 18:28:51 +02004356
Tim Kryger3a48edc2014-06-13 10:13:56 -07004357 if (!IS_ERR(mmc->supply.vqmmc))
4358 regulator_disable(mmc->supply.vqmmc);
Philip Rakity6231f3d2012-07-23 15:56:23 -07004359
Russell Kingedd63fc2016-01-26 13:39:50 +00004360 if (host->align_buffer)
Russell Kinge66e61c2016-01-26 13:39:55 +00004361 dma_free_coherent(mmc_dev(mmc), host->align_buffer_sz +
4362 host->adma_table_sz, host->align_buffer,
4363 host->align_addr);
Pierre Ossman2134a922008-06-28 18:28:51 +02004364
Adrian Hunter4efaa6f2014-11-04 12:42:39 +02004365 host->adma_table = NULL;
Pierre Ossman2134a922008-06-28 18:28:51 +02004366 host->align_buffer = NULL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08004367}
4368
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01004369EXPORT_SYMBOL_GPL(sdhci_remove_host);
4370
4371void sdhci_free_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08004372{
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01004373 mmc_free_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08004374}
4375
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01004376EXPORT_SYMBOL_GPL(sdhci_free_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08004377
4378/*****************************************************************************\
4379 * *
4380 * Driver init/exit *
4381 * *
4382\*****************************************************************************/
4383
4384static int __init sdhci_drv_init(void)
4385{
Girish K Sa3c76eb2011-10-11 11:44:09 +05304386 pr_info(DRIVER_NAME
Pierre Ossman52fbf9c2007-02-09 08:23:41 +01004387 ": Secure Digital Host Controller Interface driver\n");
Girish K Sa3c76eb2011-10-11 11:44:09 +05304388 pr_info(DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -08004389
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01004390 return 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08004391}
4392
4393static void __exit sdhci_drv_exit(void)
4394{
Pierre Ossmand129bce2006-03-24 03:18:17 -08004395}
4396
4397module_init(sdhci_drv_init);
4398module_exit(sdhci_drv_exit);
4399
Pierre Ossmandf673b22006-06-30 02:22:31 -07004400module_param(debug_quirks, uint, 0444);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03004401module_param(debug_quirks2, uint, 0444);
Pierre Ossman67435272006-06-30 02:22:31 -07004402
Pierre Ossman32710e82009-04-08 20:14:54 +02004403MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01004404MODULE_DESCRIPTION("Secure Digital Host Controller Interface core driver");
Pierre Ossmand129bce2006-03-24 03:18:17 -08004405MODULE_LICENSE("GPL");
Pierre Ossman67435272006-06-30 02:22:31 -07004406
Pierre Ossmandf673b22006-06-30 02:22:31 -07004407MODULE_PARM_DESC(debug_quirks, "Force certain quirks.");
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03004408MODULE_PARM_DESC(debug_quirks2, "Force certain other quirks.");