xref: /linux/drivers/mmc/host/sdhci-of-dwcmshc.c (revision 23d4f4316a71e04c34c7c5e03a662cc59f9ccb80)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Driver for Synopsys DesignWare Cores Mobile Storage Host Controller
4  *
5  * Copyright (C) 2018 Synaptics Incorporated
6  *
7  * Author: Jisheng Zhang <jszhang@kernel.org>
8  */
9 
10 #include <linux/acpi.h>
11 #include <linux/arm-smccc.h>
12 #include <linux/bitfield.h>
13 #include <linux/clk.h>
14 #include <linux/clk-provider.h>
15 #include <linux/dma-mapping.h>
16 #include <linux/iopoll.h>
17 #include <linux/kernel.h>
18 #include <linux/module.h>
19 #include <linux/of.h>
20 #include <linux/platform_device.h>
21 #include <linux/pm_domain.h>
22 #include <linux/pm_runtime.h>
23 #include <linux/regmap.h>
24 #include <linux/reset.h>
25 #include <linux/sizes.h>
26 #include <linux/mfd/syscon.h>
27 #include <linux/units.h>
28 
29 #include "sdhci-pltfm.h"
30 #include "cqhci.h"
31 #include "sdhci-cqhci.h"
32 
33 #define SDHCI_DWCMSHC_ARG2_STUFF	GENMASK(31, 16)
34 
35 /* DWCMSHC specific Mode Select value */
36 #define DWCMSHC_CTRL_HS400		0x7
37 
38 /* DWC IP vendor area 1 pointer */
39 #define DWCMSHC_P_VENDOR_AREA1		0xe8
40 #define DWCMSHC_AREA1_MASK		GENMASK(11, 0)
41 /* Offset inside the  vendor area 1 */
42 #define DWCMSHC_HOST_CTRL3		0x8
43 #define DWCMSHC_HOST_CTRL3_CMD_CONFLICT	BIT(0)
44 #define DWCMSHC_EMMC_CONTROL		0x2c
45 /* HPE GSC SoC MSHCCS register */
46 #define HPE_GSC_MSHCCS_SCGSYNCDIS	BIT(18)
47 #define DWCMSHC_CARD_IS_EMMC		BIT(0)
48 #define DWCMSHC_ENHANCED_STROBE		BIT(8)
49 #define DWCMSHC_EMMC_ATCTRL		0x40
50 #define DWCMSHC_AT_STAT			0x44
51 /* Tuning and auto-tuning fields in AT_CTRL_R control register */
52 #define AT_CTRL_AT_EN			BIT(0) /* autotuning is enabled */
53 #define AT_CTRL_CI_SEL			BIT(1) /* interval to drive center phase select */
54 #define AT_CTRL_SWIN_TH_EN		BIT(2) /* sampling window threshold enable */
55 #define AT_CTRL_RPT_TUNE_ERR		BIT(3) /* enable reporting framing errors */
56 #define AT_CTRL_SW_TUNE_EN		BIT(4) /* enable software managed tuning */
57 #define AT_CTRL_WIN_EDGE_SEL_MASK	GENMASK(11, 8) /* bits [11:8] */
58 #define AT_CTRL_WIN_EDGE_SEL		0xf /* sampling window edge select */
59 #define AT_CTRL_TUNE_CLK_STOP_EN	BIT(16) /* clocks stopped during phase code change */
60 #define AT_CTRL_PRE_CHANGE_DLY_MASK	GENMASK(18, 17) /* bits [18:17] */
61 #define AT_CTRL_PRE_CHANGE_DLY		0x1  /* 2-cycle latency */
62 #define AT_CTRL_POST_CHANGE_DLY_MASK	GENMASK(20, 19) /* bits [20:19] */
63 #define AT_CTRL_POST_CHANGE_DLY		0x3  /* 4-cycle latency */
64 #define AT_CTRL_SWIN_TH_VAL_MASK	GENMASK(31, 24) /* bits [31:24] */
65 #define AT_CTRL_SWIN_TH_VAL		0x9  /* sampling window threshold */
66 
67 /* DWC IP vendor area 2 pointer */
68 #define DWCMSHC_P_VENDOR_AREA2		0xea
69 
70 /* Sophgo CV18XX specific Registers */
71 #define CV18XX_SDHCI_MSHC_CTRL			0x00
72 #define  CV18XX_EMMC_FUNC_EN			BIT(0)
73 #define  CV18XX_LATANCY_1T			BIT(1)
74 #define CV18XX_SDHCI_PHY_TX_RX_DLY		0x40
75 #define  CV18XX_PHY_TX_DLY_MSK			GENMASK(6, 0)
76 #define  CV18XX_PHY_TX_SRC_MSK			GENMASK(9, 8)
77 #define  CV18XX_PHY_TX_SRC_INVERT_CLK_TX	0x1
78 #define  CV18XX_PHY_RX_DLY_MSK			GENMASK(22, 16)
79 #define  CV18XX_PHY_RX_SRC_MSK			GENMASK(25, 24)
80 #define  CV18XX_PHY_RX_SRC_INVERT_RX_CLK	0x1
81 #define CV18XX_SDHCI_PHY_CONFIG			0x4c
82 #define  CV18XX_PHY_TX_BPS			BIT(0)
83 
84 #define CV18XX_TUNE_MAX				128
85 #define CV18XX_TUNE_STEP			1
86 #define CV18XX_RETRY_TUNING_MAX			50
87 
88 /* Rockchip specific Registers */
89 #define DWCMSHC_EMMC_DLL_CTRL		0x800
90 #define DWCMSHC_EMMC_DLL_RXCLK		0x804
91 #define DWCMSHC_EMMC_DLL_TXCLK		0x808
92 #define DWCMSHC_EMMC_DLL_STRBIN		0x80c
93 #define DECMSHC_EMMC_DLL_CMDOUT		0x810
94 #define DECMSHC_EMMC_MISC_CON		0x81C
95 #define MISC_INTCLK_EN			BIT(1)
96 #define DWCMSHC_EMMC_DLL_STATUS0	0x840
97 #define DWCMSHC_EMMC_DLL_START		BIT(0)
98 #define DWCMSHC_EMMC_DLL_LOCKED		BIT(8)
99 #define DWCMSHC_EMMC_DLL_TIMEOUT	BIT(9)
100 #define DWCMSHC_EMMC_DLL_RXCLK_SRCSEL	29
101 #define DWCMSHC_EMMC_DLL_START_POINT	16
102 #define DWCMSHC_EMMC_DLL_INC		8
103 #define DWCMSHC_EMMC_DLL_BYPASS		BIT(24)
104 #define DWCMSHC_EMMC_DLL_DLYENA		BIT(27)
105 #define DLL_TXCLK_TAPNUM_DEFAULT	0x10
106 #define DLL_TXCLK_TAPNUM_90_DEGREES	0xA
107 #define DLL_TXCLK_TAPNUM_FROM_SW	BIT(24)
108 #define DLL_STRBIN_TAPNUM_DEFAULT	0x4
109 #define DLL_STRBIN_TAPNUM_FROM_SW	BIT(24)
110 #define DLL_STRBIN_DELAY_NUM_SEL	BIT(26)
111 #define DLL_STRBIN_DELAY_NUM_OFFSET	16
112 #define DLL_STRBIN_DELAY_NUM_DEFAULT	0x16
113 #define DLL_RXCLK_NO_INVERTER		1
114 #define DLL_RXCLK_INVERTER		0
115 #define DLL_CMDOUT_TAPNUM_90_DEGREES	0x8
116 #define DLL_RXCLK_ORI_GATE		BIT(31)
117 #define DLL_CMDOUT_TAPNUM_FROM_SW	BIT(24)
118 #define DLL_CMDOUT_SRC_CLK_NEG		BIT(28)
119 #define DLL_CMDOUT_EN_SRC_CLK_NEG	BIT(29)
120 
121 #define DLL_LOCK_WO_TMOUT(x) \
122 	((((x) & DWCMSHC_EMMC_DLL_LOCKED) == DWCMSHC_EMMC_DLL_LOCKED) && \
123 	(((x) & DWCMSHC_EMMC_DLL_TIMEOUT) == 0))
124 
125 /* PHY register area pointer */
126 #define DWC_MSHC_PTR_PHY_R	0x300
127 
128 /* PHY general configuration */
129 #define PHY_CNFG_R			(DWC_MSHC_PTR_PHY_R + 0x00)
130 #define PHY_CNFG_RSTN_DEASSERT		0x1  /* Deassert PHY reset */
131 #define PHY_CNFG_PHY_PWRGOOD_MASK	BIT_MASK(1) /* bit [1] */
132 #define PHY_CNFG_PAD_SP_MASK		GENMASK(19, 16) /* bits [19:16] */
133 #define PHY_CNFG_PAD_SP			0x0c /* PMOS TX drive strength */
134 #define PHY_CNFG_PAD_SP_k230		0x09 /* PMOS TX drive strength for k230 */
135 #define PHY_CNFG_PAD_SP_SG2042		0x09 /* PMOS TX drive strength for SG2042 */
136 #define PHY_CNFG_PAD_SN_MASK		GENMASK(23, 20) /* bits [23:20] */
137 #define PHY_CNFG_PAD_SN			0x0c /* NMOS TX drive strength */
138 #define PHY_CNFG_PAD_SN_k230		0x08 /* NMOS TX drive strength for k230 */
139 #define PHY_CNFG_PAD_SN_SG2042		0x08 /* NMOS TX drive strength for SG2042 */
140 
141 /* PHY command/response pad settings */
142 #define PHY_CMDPAD_CNFG_R	(DWC_MSHC_PTR_PHY_R + 0x04)
143 
144 /* PHY data pad settings */
145 #define PHY_DATAPAD_CNFG_R	(DWC_MSHC_PTR_PHY_R + 0x06)
146 
147 /* PHY clock pad settings */
148 #define PHY_CLKPAD_CNFG_R	(DWC_MSHC_PTR_PHY_R + 0x08)
149 
150 /* PHY strobe pad settings */
151 #define PHY_STBPAD_CNFG_R	(DWC_MSHC_PTR_PHY_R + 0x0a)
152 
153 /* PHY reset pad settings */
154 #define PHY_RSTNPAD_CNFG_R	(DWC_MSHC_PTR_PHY_R + 0x0c)
155 
156 /* Bitfields are common for all pad settings */
157 #define PHY_PAD_RXSEL_1V8		0x1 /* Receiver type select for 1.8V */
158 #define PHY_PAD_RXSEL_3V3		0x2 /* Receiver type select for 3.3V */
159 
160 #define PHY_PAD_WEAKPULL_MASK		GENMASK(4, 3) /* bits [4:3] */
161 #define PHY_PAD_WEAKPULL_DISABLED	0x0 /* Weak pull up and pull down disabled */
162 #define PHY_PAD_WEAKPULL_PULLUP		0x1 /* Weak pull up enabled */
163 #define PHY_PAD_WEAKPULL_PULLDOWN	0x2 /* Weak pull down enabled */
164 
165 #define PHY_PAD_TXSLEW_CTRL_P_MASK	GENMASK(8, 5) /* bits [8:5] */
166 #define PHY_PAD_TXSLEW_CTRL_P		0x3 /* Slew control for P-Type pad TX */
167 #define PHY_PAD_TXSLEW_CTRL_P_k230	0x2 /* Slew control for P-Type pad TX for k230 */
168 #define PHY_PAD_TXSLEW_CTRL_N_MASK	GENMASK(12, 9) /* bits [12:9] */
169 #define PHY_PAD_TXSLEW_CTRL_N		0x3 /* Slew control for N-Type pad TX */
170 #define PHY_PAD_TXSLEW_CTRL_N_SG2042	0x2 /* Slew control for N-Type pad TX for SG2042 */
171 #define PHY_PAD_TXSLEW_CTRL_N_k230	0x2 /* Slew control for N-Type pad TX for k230 */
172 
173 /* PHY Common DelayLine config settings */
174 #define PHY_COMMDL_CNFG			(DWC_MSHC_PTR_PHY_R + 0x1c)
175 #define PHY_COMMDL_CNFG_DLSTEP_SEL	BIT(0) /* DelayLine outputs on PAD enabled */
176 
177 /* PHY CLK delay line settings */
178 #define PHY_SDCLKDL_CNFG_R		(DWC_MSHC_PTR_PHY_R + 0x1d)
179 #define PHY_SDCLKDL_CNFG_EXTDLY_EN	BIT(0)
180 #define PHY_SDCLKDL_CNFG_UPDATE		BIT(4) /* set before writing to SDCLKDL_DC */
181 
182 /* PHY CLK delay line delay code */
183 #define PHY_SDCLKDL_DC_R		(DWC_MSHC_PTR_PHY_R + 0x1e)
184 #define PHY_SDCLKDL_DC_INITIAL		0x40 /* initial delay code */
185 #define PHY_SDCLKDL_DC_DEFAULT		0x32 /* default delay code */
186 #define PHY_SDCLKDL_DC_HS400		0x18 /* delay code for HS400 mode */
187 
188 #define PHY_SMPLDL_CNFG_R		(DWC_MSHC_PTR_PHY_R + 0x20)
189 #define PHY_SMPLDL_CNFG_EXTDLY_EN	BIT(0)
190 #define PHY_SMPLDL_CNFG_BYPASS_EN	BIT(1)
191 #define PHY_SMPLDL_CNFG_INPSEL_MASK	GENMASK(3, 2) /* bits [3:2] */
192 #define PHY_SMPLDL_CNFG_INPSEL		0x3 /* delay line input source */
193 
194 /* PHY drift_cclk_rx delay line configuration setting */
195 #define PHY_ATDL_CNFG_R			(DWC_MSHC_PTR_PHY_R + 0x21)
196 #define PHY_ATDL_CNFG_INPSEL_MASK	GENMASK(3, 2) /* bits [3:2] */
197 #define PHY_ATDL_CNFG_INPSEL		0x3 /* delay line input source */
198 #define PHY_ATDL_CNFG_INPSEL_SG2042	0x2 /* delay line input source for SG2042 */
199 
200 /* PHY DLL control settings */
201 #define PHY_DLL_CTRL_R			(DWC_MSHC_PTR_PHY_R + 0x24)
202 #define PHY_DLL_CTRL_DISABLE		0x0 /* PHY DLL is enabled */
203 #define PHY_DLL_CTRL_ENABLE		0x1 /* PHY DLL is disabled */
204 
205 /* PHY DLL  configuration register 1 */
206 #define PHY_DLL_CNFG1_R			(DWC_MSHC_PTR_PHY_R + 0x25)
207 #define PHY_DLL_CNFG1_SLVDLY_MASK	GENMASK(5, 4) /* bits [5:4] */
208 #define PHY_DLL_CNFG1_SLVDLY		0x2 /* DLL slave update delay input */
209 #define PHY_DLL_CNFG1_WAITCYCLE		0x5 /* DLL wait cycle input */
210 
211 /* PHY DLL configuration register 2 */
212 #define PHY_DLL_CNFG2_R			(DWC_MSHC_PTR_PHY_R + 0x26)
213 #define PHY_DLL_CNFG2_JUMPSTEP		0xa /* DLL jump step input */
214 
215 /* PHY DLL master and slave delay line configuration settings */
216 #define PHY_DLLDL_CNFG_R		(DWC_MSHC_PTR_PHY_R + 0x28)
217 #define PHY_DLLDL_CNFG_SLV_INPSEL_MASK	GENMASK(6, 5) /* bits [6:5] */
218 #define PHY_DLLDL_CNFG_SLV_INPSEL	0x3 /* clock source select for slave DL */
219 
220 /* PHY DLL offset setting register */
221 #define PHY_DLL_OFFST_R			(DWC_MSHC_PTR_PHY_R + 0x29)
222 /* DLL LBT setting register */
223 #define PHY_DLLBT_CNFG_R		(DWC_MSHC_PTR_PHY_R + 0x2c)
224 /* DLL Status register */
225 #define PHY_DLL_STATUS_R		(DWC_MSHC_PTR_PHY_R + 0x2e)
226 #define DLL_LOCK_STS			BIT(0)/* DLL is locked and ready */
227 /*
228  * Captures the value of DLL's lock error status information. Value is valid
229  * only when LOCK_STS is set.
230  */
231 #define DLL_ERROR_STS			BIT(1)
232 
233 #define FLAG_IO_FIXED_1V8	BIT(0)
234 
235 #define BOUNDARY_OK(addr, len) \
236 	((addr | (SZ_128M - 1)) == ((addr + len - 1) | (SZ_128M - 1)))
237 
238 #define DWCMSHC_SDHCI_CQE_TRNS_MODE	(SDHCI_TRNS_MULTI | \
239 					 SDHCI_TRNS_BLK_CNT_EN | \
240 					 SDHCI_TRNS_DMA)
241 
242 #define to_pltfm_data(priv, name) \
243 	container_of((priv)->dwcmshc_pdata, struct name##_pltfm_data, dwcmshc_pdata)
244 
245 /* SMC call for BlueField-3 eMMC RST_N */
246 #define BLUEFIELD_SMC_SET_EMMC_RST_N	0x82000007
247 
248 /* Canaan specific Registers */
249 #define SD0_CTRL			0x00
250 #define SD0_HOST_REG_VOL_STABLE		BIT(4)
251 #define SD0_CARD_WRITE_PROT		BIT(6)
252 #define SD1_CTRL			0x08
253 #define SD1_HOST_REG_VOL_STABLE		BIT(0)
254 #define SD1_CARD_WRITE_PROT		BIT(2)
255 
256 /* Eswin specific Registers */
257 #define EIC7700_CARD_CLK_STABLE		BIT(28)
258 #define EIC7700_INT_BCLK_STABLE		BIT(16)
259 #define EIC7700_INT_ACLK_STABLE		BIT(8)
260 #define EIC7700_INT_TMCLK_STABLE	BIT(0)
261 #define EIC7700_INT_CLK_STABLE		(EIC7700_CARD_CLK_STABLE | \
262 					 EIC7700_INT_ACLK_STABLE | \
263 					 EIC7700_INT_BCLK_STABLE | \
264 					 EIC7700_INT_TMCLK_STABLE)
265 #define EIC7700_HOST_VAL_STABLE		BIT(0)
266 
267 /* strength definition */
268 #define PHYCTRL_DR_33OHM		0xee
269 #define PHYCTRL_DR_40OHM		0xcc
270 #define PHYCTRL_DR_50OHM		0x88
271 #define PHYCTRL_DR_66OHM		0x44
272 #define PHYCTRL_DR_100OHM		0x00
273 
274 #define MAX_PHASE_CODE			0xff
275 #define TUNING_RANGE_THRESHOLD		40
276 #define PHY_CLK_MAX_DELAY_MASK		0x7f
277 #define PHY_DELAY_CODE_MAX		0x7f
278 #define PHY_DELAY_CODE_EMMC		0x17
279 #define PHY_DELAY_CODE_SD		0x55
280 #define PHY_DELAY_CODE_SDIO		0x29
281 
282 struct rk35xx_priv {
283 	struct reset_control *reset;
284 	u8 txclk_tapnum;
285 };
286 
287 struct eic7700_priv {
288 	struct reset_control *reset;
289 	unsigned int drive_impedance;
290 };
291 
292 struct k230_priv  {
293 	/* Canaan k230 specific */
294 	struct regmap *hi_sys_regmap;
295 };
296 
297 #define DWCMSHC_MAX_OTHER_CLKS 3
298 
299 struct dwcmshc_priv {
300 	struct clk	*bus_clk;
301 	int vendor_specific_area1; /* P_VENDOR_SPECIFIC_AREA1 reg */
302 	int vendor_specific_area2; /* P_VENDOR_SPECIFIC_AREA2 reg */
303 
304 	int num_other_clks;
305 	struct clk_bulk_data other_clks[DWCMSHC_MAX_OTHER_CLKS];
306 
307 	const struct dwcmshc_pltfm_data *dwcmshc_pdata;
308 	void *priv; /* pointer to SoC private stuff */
309 	u16 delay_line;
310 	u16 flags;
311 };
312 
313 struct dwcmshc_pltfm_data {
314 	const struct sdhci_pltfm_data pdata;
315 	const struct cqhci_host_ops *cqhci_host_ops;
316 	int (*init)(struct device *dev, struct sdhci_host *host, struct dwcmshc_priv *dwc_priv);
317 	void (*postinit)(struct sdhci_host *host, struct dwcmshc_priv *dwc_priv);
318 };
319 
320 struct k230_pltfm_data {
321 	struct dwcmshc_pltfm_data dwcmshc_pdata;
322 	bool is_emmc;
323 	u32 ctrl_reg;
324 	u32 vol_stable_bit;
325 	u32 write_prot_bit;
326 };
327 
328 struct rockchip_pltfm_data {
329 	struct dwcmshc_pltfm_data dwcmshc_pdata;
330 	/*
331 	 * The controller hardware has two known revisions documented internally:
332 	 * - Revision 0: Exclusively used by RK3566 and RK3568 SoCs.
333 	 * - Revision 1: Implemented in all other Rockchip SoCs, including RK3576, RK3588, etc.
334 	 */
335 	int revision;
336 };
337 
338 static void dwcmshc_enable_card_clk(struct sdhci_host *host)
339 {
340 	u16 ctrl;
341 
342 	ctrl = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
343 	if ((ctrl & SDHCI_CLOCK_INT_EN) && !(ctrl & SDHCI_CLOCK_CARD_EN)) {
344 		ctrl |= SDHCI_CLOCK_CARD_EN;
345 		sdhci_writew(host, ctrl, SDHCI_CLOCK_CONTROL);
346 	}
347 }
348 
349 static int dwcmshc_get_enable_other_clks(struct device *dev,
350 					 struct dwcmshc_priv *priv,
351 					 int num_clks,
352 					 const char * const clk_ids[])
353 {
354 	int err;
355 
356 	if (num_clks > DWCMSHC_MAX_OTHER_CLKS)
357 		return -EINVAL;
358 
359 	for (int i = 0; i < num_clks; i++)
360 		priv->other_clks[i].id = clk_ids[i];
361 
362 	err = devm_clk_bulk_get_optional(dev, num_clks, priv->other_clks);
363 	if (err) {
364 		dev_err(dev, "failed to get clocks %d\n", err);
365 		return err;
366 	}
367 
368 	err = clk_bulk_prepare_enable(num_clks, priv->other_clks);
369 	if (err)
370 		dev_err(dev, "failed to enable clocks %d\n", err);
371 
372 	priv->num_other_clks = num_clks;
373 
374 	return err;
375 }
376 
377 /*
378  * If DMA addr spans 128MB boundary, we split the DMA transfer into two
379  * so that each DMA transfer doesn't exceed the boundary.
380  */
381 static void dwcmshc_adma_write_desc(struct sdhci_host *host, void **desc,
382 				    dma_addr_t addr, int len, unsigned int cmd)
383 {
384 	int tmplen, offset;
385 
386 	if (likely(!len || BOUNDARY_OK(addr, len))) {
387 		sdhci_adma_write_desc(host, desc, addr, len, cmd);
388 		return;
389 	}
390 
391 	offset = addr & (SZ_128M - 1);
392 	tmplen = SZ_128M - offset;
393 	sdhci_adma_write_desc(host, desc, addr, tmplen, cmd);
394 
395 	addr += tmplen;
396 	len -= tmplen;
397 	sdhci_adma_write_desc(host, desc, addr, len, cmd);
398 }
399 
400 static void dwcmshc_reset(struct sdhci_host *host, u8 mask)
401 {
402 	sdhci_reset(host, mask);
403 
404 	/* The dwcmshc does not comply with the SDHCI specification
405 	 * regarding the "Software Reset for CMD line should clear 'Command
406 	 * Complete' in the Normal Interrupt Status Register." Clear the bit
407 	 * here to compensate for this quirk.
408 	 */
409 	if (mask & SDHCI_RESET_CMD)
410 		sdhci_writel(host, SDHCI_INT_RESPONSE, SDHCI_INT_STATUS);
411 }
412 
413 static unsigned int dwcmshc_get_max_clock(struct sdhci_host *host)
414 {
415 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
416 
417 	if (pltfm_host->clk)
418 		return sdhci_pltfm_clk_get_max_clock(host);
419 	else
420 		return pltfm_host->clock;
421 }
422 
423 static unsigned int rk35xx_get_max_clock(struct sdhci_host *host)
424 {
425 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
426 
427 	return clk_round_rate(pltfm_host->clk, ULONG_MAX);
428 }
429 
430 static void dwcmshc_check_auto_cmd23(struct mmc_host *mmc,
431 				     struct mmc_request *mrq)
432 {
433 	struct sdhci_host *host = mmc_priv(mmc);
434 
435 	/*
436 	 * No matter V4 is enabled or not, ARGUMENT2 register is 32-bit
437 	 * block count register which doesn't support stuff bits of
438 	 * CMD23 argument on dwcmsch host controller.
439 	 */
440 	if (mrq->sbc && (mrq->sbc->arg & SDHCI_DWCMSHC_ARG2_STUFF))
441 		host->flags &= ~SDHCI_AUTO_CMD23;
442 	else
443 		host->flags |= SDHCI_AUTO_CMD23;
444 }
445 
446 static void dwcmshc_request(struct mmc_host *mmc, struct mmc_request *mrq)
447 {
448 	dwcmshc_check_auto_cmd23(mmc, mrq);
449 
450 	sdhci_request(mmc, mrq);
451 }
452 
453 static void dwcmshc_phy_init(struct sdhci_host *host)
454 {
455 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
456 	struct dwcmshc_priv *priv = sdhci_pltfm_priv(pltfm_host);
457 	u32 rxsel = PHY_PAD_RXSEL_3V3;
458 	u32 val;
459 
460 	if (priv->flags & FLAG_IO_FIXED_1V8 ||
461 		host->mmc->ios.timing & MMC_SIGNAL_VOLTAGE_180)
462 		rxsel = PHY_PAD_RXSEL_1V8;
463 
464 	/* deassert phy reset & set tx drive strength */
465 	val = PHY_CNFG_RSTN_DEASSERT;
466 	val |= FIELD_PREP(PHY_CNFG_PAD_SP_MASK, PHY_CNFG_PAD_SP);
467 	val |= FIELD_PREP(PHY_CNFG_PAD_SN_MASK, PHY_CNFG_PAD_SN);
468 	sdhci_writel(host, val, PHY_CNFG_R);
469 
470 	/* disable delay line */
471 	sdhci_writeb(host, PHY_SDCLKDL_CNFG_UPDATE, PHY_SDCLKDL_CNFG_R);
472 
473 	/* set delay line */
474 	sdhci_writeb(host, priv->delay_line, PHY_SDCLKDL_DC_R);
475 	sdhci_writeb(host, PHY_DLL_CNFG2_JUMPSTEP, PHY_DLL_CNFG2_R);
476 
477 	/* enable delay lane */
478 	val = sdhci_readb(host, PHY_SDCLKDL_CNFG_R);
479 	val &= ~(PHY_SDCLKDL_CNFG_UPDATE);
480 	sdhci_writeb(host, val, PHY_SDCLKDL_CNFG_R);
481 
482 	/* configure phy pads */
483 	val = rxsel;
484 	val |= FIELD_PREP(PHY_PAD_WEAKPULL_MASK, PHY_PAD_WEAKPULL_PULLUP);
485 	val |= FIELD_PREP(PHY_PAD_TXSLEW_CTRL_P_MASK, PHY_PAD_TXSLEW_CTRL_P);
486 	val |= FIELD_PREP(PHY_PAD_TXSLEW_CTRL_N_MASK, PHY_PAD_TXSLEW_CTRL_N);
487 	sdhci_writew(host, val, PHY_CMDPAD_CNFG_R);
488 	sdhci_writew(host, val, PHY_DATAPAD_CNFG_R);
489 	sdhci_writew(host, val, PHY_RSTNPAD_CNFG_R);
490 
491 	val = FIELD_PREP(PHY_PAD_TXSLEW_CTRL_P_MASK, PHY_PAD_TXSLEW_CTRL_P);
492 	val |= FIELD_PREP(PHY_PAD_TXSLEW_CTRL_N_MASK, PHY_PAD_TXSLEW_CTRL_N);
493 	sdhci_writew(host, val, PHY_CLKPAD_CNFG_R);
494 
495 	val = rxsel;
496 	val |= FIELD_PREP(PHY_PAD_WEAKPULL_MASK, PHY_PAD_WEAKPULL_PULLDOWN);
497 	val |= FIELD_PREP(PHY_PAD_TXSLEW_CTRL_P_MASK, PHY_PAD_TXSLEW_CTRL_P);
498 	val |= FIELD_PREP(PHY_PAD_TXSLEW_CTRL_N_MASK, PHY_PAD_TXSLEW_CTRL_N);
499 	sdhci_writew(host, val, PHY_STBPAD_CNFG_R);
500 
501 	/* enable data strobe mode */
502 	if (rxsel == PHY_PAD_RXSEL_1V8) {
503 		u8 sel = FIELD_PREP(PHY_DLLDL_CNFG_SLV_INPSEL_MASK, PHY_DLLDL_CNFG_SLV_INPSEL);
504 
505 		sdhci_writeb(host, sel, PHY_DLLDL_CNFG_R);
506 	}
507 
508 	/* enable phy dll */
509 	sdhci_writeb(host, PHY_DLL_CTRL_ENABLE, PHY_DLL_CTRL_R);
510 
511 }
512 
513 static void th1520_sdhci_set_phy(struct sdhci_host *host)
514 {
515 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
516 	struct dwcmshc_priv *priv = sdhci_pltfm_priv(pltfm_host);
517 	u32 emmc_caps = MMC_CAP2_NO_SD | MMC_CAP2_NO_SDIO;
518 	u16 emmc_ctrl;
519 
520 	dwcmshc_phy_init(host);
521 
522 	if ((host->mmc->caps2 & emmc_caps) == emmc_caps) {
523 		emmc_ctrl = sdhci_readw(host, priv->vendor_specific_area1 + DWCMSHC_EMMC_CONTROL);
524 		emmc_ctrl |= DWCMSHC_CARD_IS_EMMC;
525 		sdhci_writew(host, emmc_ctrl, priv->vendor_specific_area1 + DWCMSHC_EMMC_CONTROL);
526 	}
527 
528 	sdhci_writeb(host, FIELD_PREP(PHY_DLL_CNFG1_SLVDLY_MASK, PHY_DLL_CNFG1_SLVDLY) |
529 		     PHY_DLL_CNFG1_WAITCYCLE, PHY_DLL_CNFG1_R);
530 }
531 
532 static void dwcmshc_set_uhs_signaling(struct sdhci_host *host,
533 				      unsigned int timing)
534 {
535 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
536 	struct dwcmshc_priv *priv = sdhci_pltfm_priv(pltfm_host);
537 	u16 ctrl, ctrl_2;
538 
539 	ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
540 	/* Select Bus Speed Mode for host */
541 	ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
542 	if ((timing == MMC_TIMING_MMC_HS200) ||
543 	    (timing == MMC_TIMING_UHS_SDR104))
544 		ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
545 	else if (timing == MMC_TIMING_UHS_SDR12)
546 		ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
547 	else if ((timing == MMC_TIMING_UHS_SDR25) ||
548 		 (timing == MMC_TIMING_MMC_HS))
549 		ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
550 	else if (timing == MMC_TIMING_UHS_SDR50)
551 		ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
552 	else if ((timing == MMC_TIMING_UHS_DDR50) ||
553 		 (timing == MMC_TIMING_MMC_DDR52))
554 		ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
555 	else if (timing == MMC_TIMING_MMC_HS400) {
556 		/* set CARD_IS_EMMC bit to enable Data Strobe for HS400 */
557 		ctrl = sdhci_readw(host, priv->vendor_specific_area1 + DWCMSHC_EMMC_CONTROL);
558 		ctrl |= DWCMSHC_CARD_IS_EMMC;
559 		sdhci_writew(host, ctrl, priv->vendor_specific_area1 + DWCMSHC_EMMC_CONTROL);
560 
561 		ctrl_2 |= DWCMSHC_CTRL_HS400;
562 	}
563 
564 	if (priv->flags & FLAG_IO_FIXED_1V8)
565 		ctrl_2 |= SDHCI_CTRL_VDD_180;
566 	sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
567 }
568 
569 static void th1520_set_uhs_signaling(struct sdhci_host *host,
570 				     unsigned int timing)
571 {
572 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
573 	struct dwcmshc_priv *priv = sdhci_pltfm_priv(pltfm_host);
574 
575 	dwcmshc_set_uhs_signaling(host, timing);
576 	if (timing == MMC_TIMING_MMC_HS400)
577 		priv->delay_line = PHY_SDCLKDL_DC_HS400;
578 	else
579 		sdhci_writeb(host, 0, PHY_DLLDL_CNFG_R);
580 	th1520_sdhci_set_phy(host);
581 }
582 
583 static void dwcmshc_hs400_enhanced_strobe(struct mmc_host *mmc,
584 					  struct mmc_ios *ios)
585 {
586 	u32 vendor;
587 	struct sdhci_host *host = mmc_priv(mmc);
588 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
589 	struct dwcmshc_priv *priv = sdhci_pltfm_priv(pltfm_host);
590 	int reg = priv->vendor_specific_area1 + DWCMSHC_EMMC_CONTROL;
591 
592 	vendor = sdhci_readl(host, reg);
593 	if (ios->enhanced_strobe)
594 		vendor |= DWCMSHC_ENHANCED_STROBE;
595 	else
596 		vendor &= ~DWCMSHC_ENHANCED_STROBE;
597 
598 	sdhci_writel(host, vendor, reg);
599 }
600 
601 static int dwcmshc_execute_tuning(struct mmc_host *mmc, u32 opcode)
602 {
603 	int err = sdhci_execute_tuning(mmc, opcode);
604 	struct sdhci_host *host = mmc_priv(mmc);
605 
606 	if (err)
607 		return err;
608 
609 	/*
610 	 * Tuning can leave the IP in an active state (Buffer Read Enable bit
611 	 * set) which prevents the entry to low power states (i.e. S0i3). Data
612 	 * reset will clear it.
613 	 */
614 	sdhci_reset(host, SDHCI_RESET_DATA);
615 
616 	return 0;
617 }
618 
619 static u32 dwcmshc_cqe_irq_handler(struct sdhci_host *host, u32 intmask)
620 {
621 	int cmd_error = 0;
622 	int data_error = 0;
623 
624 	if (!sdhci_cqe_irq(host, intmask, &cmd_error, &data_error))
625 		return intmask;
626 
627 	cqhci_irq(host->mmc, intmask, cmd_error, data_error);
628 
629 	return 0;
630 }
631 
632 static void dwcmshc_sdhci_cqe_enable(struct mmc_host *mmc)
633 {
634 	struct sdhci_host *host = mmc_priv(mmc);
635 	u8 ctrl;
636 
637 	sdhci_writew(host, DWCMSHC_SDHCI_CQE_TRNS_MODE, SDHCI_TRANSFER_MODE);
638 
639 	sdhci_cqe_enable(mmc);
640 
641 	/*
642 	 * The "DesignWare Cores Mobile Storage Host Controller
643 	 * DWC_mshc / DWC_mshc_lite Databook" says:
644 	 * when Host Version 4 Enable" is 1 in Host Control 2 register,
645 	 * SDHCI_CTRL_ADMA32 bit means ADMA2 is selected.
646 	 * Selection of 32-bit/64-bit System Addressing:
647 	 * either 32-bit or 64-bit system addressing is selected by
648 	 * 64-bit Addressing bit in Host Control 2 register.
649 	 *
650 	 * On the other hand the "DesignWare Cores Mobile Storage Host
651 	 * Controller DWC_mshc / DWC_mshc_lite User Guide" says, that we have to
652 	 * set DMA_SEL to ADMA2 _only_ mode in the Host Control 2 register.
653 	 */
654 	ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
655 	ctrl &= ~SDHCI_CTRL_DMA_MASK;
656 	ctrl |= SDHCI_CTRL_ADMA32;
657 	sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
658 }
659 
660 static void dwcmshc_set_tran_desc(struct cqhci_host *cq_host, u8 **desc,
661 				  dma_addr_t addr, int len, bool end, bool dma64)
662 {
663 	int tmplen, offset;
664 
665 	if (likely(!len || BOUNDARY_OK(addr, len))) {
666 		cqhci_set_tran_desc(*desc, addr, len, end, dma64);
667 		return;
668 	}
669 
670 	offset = addr & (SZ_128M - 1);
671 	tmplen = SZ_128M - offset;
672 	cqhci_set_tran_desc(*desc, addr, tmplen, false, dma64);
673 
674 	addr += tmplen;
675 	len -= tmplen;
676 	*desc += cq_host->trans_desc_len;
677 	cqhci_set_tran_desc(*desc, addr, len, end, dma64);
678 }
679 
680 static void dwcmshc_cqhci_dumpregs(struct mmc_host *mmc)
681 {
682 	sdhci_dumpregs(mmc_priv(mmc));
683 }
684 
685 static void rk35xx_sdhci_cqe_pre_enable(struct mmc_host *mmc)
686 {
687 	struct sdhci_host *host = mmc_priv(mmc);
688 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
689 	struct dwcmshc_priv *dwc_priv = sdhci_pltfm_priv(pltfm_host);
690 	u32 reg;
691 
692 	/* Set Send Status Command Idle Timer to 10.66us (256 * 1 / 24) */
693 	reg = sdhci_readl(host, dwc_priv->vendor_specific_area2 + CQHCI_SSC1);
694 	reg = (reg & ~CQHCI_SSC1_CIT_MASK) | 0x0100;
695 	sdhci_writel(host, reg, dwc_priv->vendor_specific_area2 + CQHCI_SSC1);
696 
697 	reg = sdhci_readl(host, dwc_priv->vendor_specific_area2 + CQHCI_CFG);
698 	reg |= CQHCI_ENABLE;
699 	sdhci_writel(host, reg, dwc_priv->vendor_specific_area2 + CQHCI_CFG);
700 }
701 
702 static void rk35xx_sdhci_cqe_enable(struct mmc_host *mmc)
703 {
704 	struct sdhci_host *host = mmc_priv(mmc);
705 	u32 reg;
706 
707 	reg = sdhci_readl(host, SDHCI_PRESENT_STATE);
708 	while (reg & SDHCI_DATA_AVAILABLE) {
709 		sdhci_readl(host, SDHCI_BUFFER);
710 		reg = sdhci_readl(host, SDHCI_PRESENT_STATE);
711 	}
712 
713 	sdhci_writew(host, DWCMSHC_SDHCI_CQE_TRNS_MODE, SDHCI_TRANSFER_MODE);
714 
715 	sdhci_cqe_enable(mmc);
716 }
717 
718 static void rk35xx_sdhci_cqe_disable(struct mmc_host *mmc, bool recovery)
719 {
720 	struct sdhci_host *host = mmc_priv(mmc);
721 	unsigned long flags;
722 	u32 ctrl;
723 
724 	/*
725 	 * During CQE command transfers, command complete bit gets latched.
726 	 * So s/w should clear command complete interrupt status when CQE is
727 	 * either halted or disabled. Otherwise unexpected SDCHI legacy
728 	 * interrupt gets triggered when CQE is halted/disabled.
729 	 */
730 	spin_lock_irqsave(&host->lock, flags);
731 	ctrl = sdhci_readl(host, SDHCI_INT_ENABLE);
732 	ctrl |= SDHCI_INT_RESPONSE;
733 	sdhci_writel(host,  ctrl, SDHCI_INT_ENABLE);
734 	sdhci_writel(host, SDHCI_INT_RESPONSE, SDHCI_INT_STATUS);
735 	spin_unlock_irqrestore(&host->lock, flags);
736 
737 	sdhci_cqe_disable(mmc, recovery);
738 }
739 
740 static void rk35xx_sdhci_cqe_post_disable(struct mmc_host *mmc)
741 {
742 	struct sdhci_host *host = mmc_priv(mmc);
743 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
744 	struct dwcmshc_priv *dwc_priv = sdhci_pltfm_priv(pltfm_host);
745 	u32 ctrl;
746 
747 	ctrl = sdhci_readl(host, dwc_priv->vendor_specific_area2 + CQHCI_CFG);
748 	ctrl &= ~CQHCI_ENABLE;
749 	sdhci_writel(host, ctrl, dwc_priv->vendor_specific_area2 + CQHCI_CFG);
750 }
751 
752 static void dwcmshc_rk3568_set_clock(struct sdhci_host *host, unsigned int clock)
753 {
754 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
755 	struct dwcmshc_priv *dwc_priv = sdhci_pltfm_priv(pltfm_host);
756 	struct rk35xx_priv *priv = dwc_priv->priv;
757 	const struct rockchip_pltfm_data *rockchip_pdata = to_pltfm_data(dwc_priv, rockchip);
758 	u8 txclk_tapnum = DLL_TXCLK_TAPNUM_DEFAULT;
759 	u32 extra, reg;
760 	int err;
761 
762 	host->mmc->actual_clock = 0;
763 
764 	if (clock == 0) {
765 		/* Disable interface clock at initial state. */
766 		sdhci_set_clock(host, clock);
767 		return;
768 	}
769 
770 	/* Rockchip platform only support 375KHz for identify mode */
771 	if (clock <= 400000)
772 		clock = 375000;
773 
774 	err = clk_set_rate(pltfm_host->clk, clock);
775 	if (err)
776 		dev_err(mmc_dev(host->mmc), "fail to set clock %d", clock);
777 
778 	sdhci_set_clock(host, clock);
779 
780 	/* Disable cmd conflict check and internal clock gate */
781 	reg = dwc_priv->vendor_specific_area1 + DWCMSHC_HOST_CTRL3;
782 	extra = sdhci_readl(host, reg);
783 	extra &= ~BIT(0);
784 	extra |= BIT(4);
785 	sdhci_writel(host, extra, reg);
786 
787 	/* Disable clock while config DLL */
788 	sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
789 
790 	if (clock <= 52000000) {
791 		if (host->mmc->ios.timing == MMC_TIMING_MMC_HS200 ||
792 		    host->mmc->ios.timing == MMC_TIMING_MMC_HS400) {
793 			dev_err(mmc_dev(host->mmc),
794 				"Can't reduce the clock below 52MHz in HS200/HS400 mode");
795 			goto enable_clk;
796 		}
797 
798 		/*
799 		 * Disable DLL and reset both of sample and drive clock.
800 		 * The bypass bit and start bit need to be set if DLL is not locked.
801 		 */
802 		sdhci_writel(host, DWCMSHC_EMMC_DLL_BYPASS | DWCMSHC_EMMC_DLL_START, DWCMSHC_EMMC_DLL_CTRL);
803 		sdhci_writel(host, DLL_RXCLK_ORI_GATE, DWCMSHC_EMMC_DLL_RXCLK);
804 		sdhci_writel(host, 0, DWCMSHC_EMMC_DLL_TXCLK);
805 		sdhci_writel(host, 0, DECMSHC_EMMC_DLL_CMDOUT);
806 		/*
807 		 * Before switching to hs400es mode, the driver will enable
808 		 * enhanced strobe first. PHY needs to configure the parameters
809 		 * of enhanced strobe first.
810 		 */
811 		extra = DWCMSHC_EMMC_DLL_DLYENA |
812 			DLL_STRBIN_DELAY_NUM_SEL |
813 			DLL_STRBIN_DELAY_NUM_DEFAULT << DLL_STRBIN_DELAY_NUM_OFFSET;
814 		sdhci_writel(host, extra, DWCMSHC_EMMC_DLL_STRBIN);
815 		goto enable_clk;
816 	}
817 
818 	/* Reset DLL */
819 	sdhci_writel(host, BIT(1), DWCMSHC_EMMC_DLL_CTRL);
820 	udelay(1);
821 	sdhci_writel(host, 0x0, DWCMSHC_EMMC_DLL_CTRL);
822 
823 	/*
824 	 * We shouldn't set DLL_RXCLK_NO_INVERTER for identify mode but
825 	 * we must set it in higher speed mode.
826 	 */
827 	extra = DWCMSHC_EMMC_DLL_DLYENA;
828 	if (rockchip_pdata->revision == 0)
829 		extra |= DLL_RXCLK_NO_INVERTER << DWCMSHC_EMMC_DLL_RXCLK_SRCSEL;
830 	sdhci_writel(host, extra, DWCMSHC_EMMC_DLL_RXCLK);
831 
832 	/* Init DLL settings */
833 	extra = 0x5 << DWCMSHC_EMMC_DLL_START_POINT |
834 		0x2 << DWCMSHC_EMMC_DLL_INC |
835 		DWCMSHC_EMMC_DLL_START;
836 	sdhci_writel(host, extra, DWCMSHC_EMMC_DLL_CTRL);
837 	err = readl_poll_timeout(host->ioaddr + DWCMSHC_EMMC_DLL_STATUS0,
838 				 extra, DLL_LOCK_WO_TMOUT(extra), 1,
839 				 500 * USEC_PER_MSEC);
840 	if (err) {
841 		dev_err(mmc_dev(host->mmc), "DLL lock timeout!\n");
842 		goto enable_clk;
843 	}
844 
845 	extra = 0x1 << 16 | /* tune clock stop en */
846 		0x3 << 17 | /* pre-change delay */
847 		0x3 << 19;  /* post-change delay */
848 	sdhci_writel(host, extra, dwc_priv->vendor_specific_area1 + DWCMSHC_EMMC_ATCTRL);
849 
850 	if (host->mmc->ios.timing == MMC_TIMING_MMC_HS200 ||
851 	    host->mmc->ios.timing == MMC_TIMING_MMC_HS400)
852 		txclk_tapnum = priv->txclk_tapnum;
853 
854 	if (rockchip_pdata->revision == 1 && host->mmc->ios.timing == MMC_TIMING_MMC_HS400) {
855 		txclk_tapnum = DLL_TXCLK_TAPNUM_90_DEGREES;
856 
857 		extra = DLL_CMDOUT_SRC_CLK_NEG |
858 			DLL_CMDOUT_EN_SRC_CLK_NEG |
859 			DWCMSHC_EMMC_DLL_DLYENA |
860 			DLL_CMDOUT_TAPNUM_90_DEGREES |
861 			DLL_CMDOUT_TAPNUM_FROM_SW;
862 		sdhci_writel(host, extra, DECMSHC_EMMC_DLL_CMDOUT);
863 	}
864 
865 	extra = DWCMSHC_EMMC_DLL_DLYENA |
866 		DLL_TXCLK_TAPNUM_FROM_SW |
867 		DLL_RXCLK_NO_INVERTER << DWCMSHC_EMMC_DLL_RXCLK_SRCSEL |
868 		txclk_tapnum;
869 	sdhci_writel(host, extra, DWCMSHC_EMMC_DLL_TXCLK);
870 
871 	extra = DWCMSHC_EMMC_DLL_DLYENA |
872 		DLL_STRBIN_TAPNUM_DEFAULT |
873 		DLL_STRBIN_TAPNUM_FROM_SW;
874 	sdhci_writel(host, extra, DWCMSHC_EMMC_DLL_STRBIN);
875 
876 enable_clk:
877 	/*
878 	 * The sdclk frequency select bits in SDHCI_CLOCK_CONTROL are not functional
879 	 * on Rockchip's SDHCI implementation. Instead, the clock frequency is fully
880 	 * controlled via external clk provider by calling clk_set_rate(). Consequently,
881 	 * passing 0 to sdhci_enable_clk() only re-enables the already-configured clock,
882 	 * which matches the hardware's actual behavior.
883 	 */
884 	sdhci_enable_clk(host, 0);
885 }
886 
887 static void rk35xx_sdhci_reset(struct sdhci_host *host, u8 mask)
888 {
889 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
890 	struct dwcmshc_priv *dwc_priv = sdhci_pltfm_priv(pltfm_host);
891 	struct rk35xx_priv *priv = dwc_priv->priv;
892 	u32 extra = sdhci_readl(host, DECMSHC_EMMC_MISC_CON);
893 
894 	if ((host->mmc->caps2 & MMC_CAP2_CQE) && (mask & SDHCI_RESET_ALL))
895 		cqhci_deactivate(host->mmc);
896 
897 	if (mask & SDHCI_RESET_ALL && priv->reset) {
898 		reset_control_assert(priv->reset);
899 		udelay(1);
900 		reset_control_deassert(priv->reset);
901 	}
902 
903 	sdhci_reset(host, mask);
904 
905 	/* Enable INTERNAL CLOCK */
906 	sdhci_writel(host, MISC_INTCLK_EN | extra, DECMSHC_EMMC_MISC_CON);
907 }
908 
909 static int dwcmshc_rk35xx_init(struct device *dev, struct sdhci_host *host,
910 			       struct dwcmshc_priv *dwc_priv)
911 {
912 	static const char * const clk_ids[] = {"axi", "block", "timer"};
913 	struct rk35xx_priv *priv;
914 	int err;
915 
916 	priv = devm_kzalloc(dev, sizeof(struct rk35xx_priv), GFP_KERNEL);
917 	if (!priv)
918 		return -ENOMEM;
919 
920 	priv->reset = devm_reset_control_array_get_optional_exclusive(mmc_dev(host->mmc));
921 	if (IS_ERR(priv->reset))
922 		return dev_err_probe(mmc_dev(host->mmc), PTR_ERR(priv->reset),
923 				     "failed to get reset control\n");
924 
925 	err = dwcmshc_get_enable_other_clks(mmc_dev(host->mmc), dwc_priv,
926 					    ARRAY_SIZE(clk_ids), clk_ids);
927 	if (err)
928 		return err;
929 
930 	if (of_property_read_u8(mmc_dev(host->mmc)->of_node, "rockchip,txclk-tapnum",
931 				&priv->txclk_tapnum))
932 		priv->txclk_tapnum = DLL_TXCLK_TAPNUM_DEFAULT;
933 
934 	/* Disable cmd conflict check */
935 	sdhci_writel(host, 0x0, dwc_priv->vendor_specific_area1 + DWCMSHC_HOST_CTRL3);
936 	/* Reset previous settings */
937 	sdhci_writel(host, 0, DWCMSHC_EMMC_DLL_TXCLK);
938 	sdhci_writel(host, 0, DWCMSHC_EMMC_DLL_STRBIN);
939 
940 	dwc_priv->priv = priv;
941 
942 	return 0;
943 }
944 
945 static void dwcmshc_rk35xx_postinit(struct sdhci_host *host, struct dwcmshc_priv *dwc_priv)
946 {
947 	/*
948 	 * Don't support highspeed bus mode with low clk speed as we
949 	 * cannot use DLL for this condition.
950 	 */
951 	if (host->mmc->f_max <= 52000000) {
952 		dev_info(mmc_dev(host->mmc), "Disabling HS200/HS400, frequency too low (%d)\n",
953 			 host->mmc->f_max);
954 		host->mmc->caps2 &= ~(MMC_CAP2_HS200 | MMC_CAP2_HS400);
955 		host->mmc->caps &= ~(MMC_CAP_3_3V_DDR | MMC_CAP_1_8V_DDR);
956 	}
957 }
958 
959 static void dwcmshc_rk3576_postinit(struct sdhci_host *host, struct dwcmshc_priv *dwc_priv)
960 {
961 	struct device *dev = mmc_dev(host->mmc);
962 	int ret;
963 
964 	/*
965 	 * This works around the design of the RK3576's power domains, which
966 	 * makes the PD_NVM power domain, which the sdhci controller on the
967 	 * RK3576 is in, never come back the same way once it's run-time
968 	 * suspended once. This can happen during early kernel boot if no driver
969 	 * is using either PD_NVM or its child power domain PD_SDGMAC for a
970 	 * short moment, leading to it being turned off to save power. By
971 	 * keeping it on, sdhci suspending won't lead to PD_NVM becoming a
972 	 * candidate for getting turned off.
973 	 */
974 	ret = dev_pm_genpd_rpm_always_on(dev, true);
975 	if (ret && ret != -EOPNOTSUPP)
976 		dev_warn(dev, "failed to set PD rpm always on, SoC may hang later: %pe\n",
977 			 ERR_PTR(ret));
978 
979 	dwcmshc_rk35xx_postinit(host, dwc_priv);
980 }
981 
982 static int th1520_execute_tuning(struct sdhci_host *host, u32 opcode)
983 {
984 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
985 	struct dwcmshc_priv *priv = sdhci_pltfm_priv(pltfm_host);
986 	u32 val = 0;
987 
988 	if (host->flags & SDHCI_HS400_TUNING)
989 		return 0;
990 
991 	sdhci_writeb(host, FIELD_PREP(PHY_ATDL_CNFG_INPSEL_MASK, PHY_ATDL_CNFG_INPSEL),
992 		     PHY_ATDL_CNFG_R);
993 	val = sdhci_readl(host, priv->vendor_specific_area1 + DWCMSHC_EMMC_ATCTRL);
994 
995 	/*
996 	 * configure tuning settings:
997 	 *  - center phase select code driven in block gap interval
998 	 *  - disable reporting of framing errors
999 	 *  - disable software managed tuning
1000 	 *  - disable user selection of sampling window edges,
1001 	 *    instead tuning calculated edges are used
1002 	 */
1003 	val &= ~(AT_CTRL_CI_SEL | AT_CTRL_RPT_TUNE_ERR | AT_CTRL_SW_TUNE_EN |
1004 		 FIELD_PREP(AT_CTRL_WIN_EDGE_SEL_MASK, AT_CTRL_WIN_EDGE_SEL));
1005 
1006 	/*
1007 	 * configure tuning settings:
1008 	 *  - enable auto-tuning
1009 	 *  - enable sampling window threshold
1010 	 *  - stop clocks during phase code change
1011 	 *  - set max latency in cycles between tx and rx clocks
1012 	 *  - set max latency in cycles to switch output phase
1013 	 *  - set max sampling window threshold value
1014 	 */
1015 	val |= AT_CTRL_AT_EN | AT_CTRL_SWIN_TH_EN | AT_CTRL_TUNE_CLK_STOP_EN;
1016 	val |= FIELD_PREP(AT_CTRL_PRE_CHANGE_DLY_MASK, AT_CTRL_PRE_CHANGE_DLY);
1017 	val |= FIELD_PREP(AT_CTRL_POST_CHANGE_DLY_MASK, AT_CTRL_POST_CHANGE_DLY);
1018 	val |= FIELD_PREP(AT_CTRL_SWIN_TH_VAL_MASK, AT_CTRL_SWIN_TH_VAL);
1019 
1020 	sdhci_writel(host, val, priv->vendor_specific_area1 + DWCMSHC_EMMC_ATCTRL);
1021 	val = sdhci_readl(host, priv->vendor_specific_area1 + DWCMSHC_EMMC_ATCTRL);
1022 
1023 	/* perform tuning */
1024 	sdhci_start_tuning(host);
1025 	host->tuning_loop_count = 128;
1026 	host->tuning_err = __sdhci_execute_tuning(host, opcode);
1027 	if (host->tuning_err) {
1028 		/* disable auto-tuning upon tuning error */
1029 		val &= ~AT_CTRL_AT_EN;
1030 		sdhci_writel(host, val, priv->vendor_specific_area1 + DWCMSHC_EMMC_ATCTRL);
1031 		dev_err(mmc_dev(host->mmc), "tuning failed: %d\n", host->tuning_err);
1032 		return -EIO;
1033 	}
1034 	sdhci_end_tuning(host);
1035 
1036 	return 0;
1037 }
1038 
1039 static void th1520_sdhci_reset(struct sdhci_host *host, u8 mask)
1040 {
1041 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1042 	struct dwcmshc_priv *priv = sdhci_pltfm_priv(pltfm_host);
1043 	u16 ctrl_2;
1044 
1045 	dwcmshc_reset(host, mask);
1046 
1047 	if (priv->flags & FLAG_IO_FIXED_1V8) {
1048 		ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1049 		if (!(ctrl_2 & SDHCI_CTRL_VDD_180)) {
1050 			ctrl_2 |= SDHCI_CTRL_VDD_180;
1051 			sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
1052 		}
1053 	}
1054 }
1055 
1056 static int th1520_init(struct device *dev,
1057 		       struct sdhci_host *host,
1058 		       struct dwcmshc_priv *dwc_priv)
1059 {
1060 	dwc_priv->delay_line = PHY_SDCLKDL_DC_DEFAULT;
1061 
1062 	if (device_property_read_bool(dev, "mmc-ddr-1_8v") ||
1063 	    device_property_read_bool(dev, "mmc-hs200-1_8v") ||
1064 	    device_property_read_bool(dev, "mmc-hs400-1_8v"))
1065 		dwc_priv->flags |= FLAG_IO_FIXED_1V8;
1066 	else
1067 		dwc_priv->flags &= ~FLAG_IO_FIXED_1V8;
1068 
1069 	/*
1070 	 * start_signal_voltage_switch() will try 3.3V first
1071 	 * then 1.8V. Use SDHCI_SIGNALING_180 rather than
1072 	 * SDHCI_SIGNALING_330 to avoid setting voltage to 3.3V
1073 	 * in sdhci_start_signal_voltage_switch().
1074 	 */
1075 	if (dwc_priv->flags & FLAG_IO_FIXED_1V8) {
1076 		host->flags &= ~SDHCI_SIGNALING_330;
1077 		host->flags |=  SDHCI_SIGNALING_180;
1078 	}
1079 
1080 	sdhci_enable_v4_mode(host);
1081 
1082 	return 0;
1083 }
1084 
1085 static void cv18xx_sdhci_reset(struct sdhci_host *host, u8 mask)
1086 {
1087 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1088 	struct dwcmshc_priv *priv = sdhci_pltfm_priv(pltfm_host);
1089 	u32 val, emmc_caps = MMC_CAP2_NO_SD | MMC_CAP2_NO_SDIO;
1090 
1091 	dwcmshc_reset(host, mask);
1092 
1093 	if ((host->mmc->caps2 & emmc_caps) == emmc_caps) {
1094 		val = sdhci_readl(host, priv->vendor_specific_area1 + CV18XX_SDHCI_MSHC_CTRL);
1095 		val |= CV18XX_EMMC_FUNC_EN;
1096 		sdhci_writel(host, val, priv->vendor_specific_area1 + CV18XX_SDHCI_MSHC_CTRL);
1097 	}
1098 
1099 	val = sdhci_readl(host, priv->vendor_specific_area1 + CV18XX_SDHCI_MSHC_CTRL);
1100 	val |= CV18XX_LATANCY_1T;
1101 	sdhci_writel(host, val, priv->vendor_specific_area1 + CV18XX_SDHCI_MSHC_CTRL);
1102 
1103 	val = sdhci_readl(host, priv->vendor_specific_area1 + CV18XX_SDHCI_PHY_CONFIG);
1104 	val |= CV18XX_PHY_TX_BPS;
1105 	sdhci_writel(host, val, priv->vendor_specific_area1 + CV18XX_SDHCI_PHY_CONFIG);
1106 
1107 	val =  (FIELD_PREP(CV18XX_PHY_TX_DLY_MSK, 0) |
1108 		FIELD_PREP(CV18XX_PHY_TX_SRC_MSK, CV18XX_PHY_TX_SRC_INVERT_CLK_TX) |
1109 		FIELD_PREP(CV18XX_PHY_RX_DLY_MSK, 0) |
1110 		FIELD_PREP(CV18XX_PHY_RX_SRC_MSK, CV18XX_PHY_RX_SRC_INVERT_RX_CLK));
1111 	sdhci_writel(host, val, priv->vendor_specific_area1 + CV18XX_SDHCI_PHY_TX_RX_DLY);
1112 }
1113 
1114 static void cv18xx_sdhci_set_tap(struct sdhci_host *host, int tap)
1115 {
1116 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1117 	struct dwcmshc_priv *priv = sdhci_pltfm_priv(pltfm_host);
1118 	u16 clk;
1119 	u32 val;
1120 
1121 	clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1122 	clk &= ~SDHCI_CLOCK_CARD_EN;
1123 	sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1124 
1125 	val = sdhci_readl(host, priv->vendor_specific_area1 + CV18XX_SDHCI_MSHC_CTRL);
1126 	val &= ~CV18XX_LATANCY_1T;
1127 	sdhci_writel(host, val, priv->vendor_specific_area1 + CV18XX_SDHCI_MSHC_CTRL);
1128 
1129 	val =  (FIELD_PREP(CV18XX_PHY_TX_DLY_MSK, 0) |
1130 		FIELD_PREP(CV18XX_PHY_TX_SRC_MSK, CV18XX_PHY_TX_SRC_INVERT_CLK_TX) |
1131 		FIELD_PREP(CV18XX_PHY_RX_DLY_MSK, tap));
1132 	sdhci_writel(host, val, priv->vendor_specific_area1 + CV18XX_SDHCI_PHY_TX_RX_DLY);
1133 
1134 	sdhci_writel(host, 0, priv->vendor_specific_area1 + CV18XX_SDHCI_PHY_CONFIG);
1135 
1136 	clk |= SDHCI_CLOCK_CARD_EN;
1137 	sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1138 	usleep_range(1000, 2000);
1139 }
1140 
1141 static int cv18xx_retry_tuning(struct mmc_host *mmc, u32 opcode, int *cmd_error)
1142 {
1143 	int ret, retry = 0;
1144 
1145 	while (retry < CV18XX_RETRY_TUNING_MAX) {
1146 		ret = mmc_send_tuning(mmc, opcode, NULL);
1147 		if (ret)
1148 			return ret;
1149 		retry++;
1150 	}
1151 
1152 	return 0;
1153 }
1154 
1155 static void cv18xx_sdhci_post_tuning(struct sdhci_host *host)
1156 {
1157 	u32 val;
1158 
1159 	val = sdhci_readl(host, SDHCI_INT_STATUS);
1160 	val |= SDHCI_INT_DATA_AVAIL;
1161 	sdhci_writel(host, val, SDHCI_INT_STATUS);
1162 
1163 	dwcmshc_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
1164 }
1165 
1166 static int cv18xx_sdhci_execute_tuning(struct sdhci_host *host, u32 opcode)
1167 {
1168 	int min, max, avg, ret;
1169 	int win_length, target_min, target_max, target_win_length;
1170 
1171 	min = max = 0;
1172 	target_win_length = 0;
1173 
1174 	sdhci_reset_tuning(host);
1175 
1176 	while (max < CV18XX_TUNE_MAX) {
1177 		/* find the mininum delay first which can pass tuning */
1178 		while (min < CV18XX_TUNE_MAX) {
1179 			cv18xx_sdhci_set_tap(host, min);
1180 			if (!cv18xx_retry_tuning(host->mmc, opcode, NULL))
1181 				break;
1182 			min += CV18XX_TUNE_STEP;
1183 		}
1184 
1185 		/* find the maxinum delay which can not pass tuning */
1186 		max = min + CV18XX_TUNE_STEP;
1187 		while (max < CV18XX_TUNE_MAX) {
1188 			cv18xx_sdhci_set_tap(host, max);
1189 			if (cv18xx_retry_tuning(host->mmc, opcode, NULL)) {
1190 				max -= CV18XX_TUNE_STEP;
1191 				break;
1192 			}
1193 			max += CV18XX_TUNE_STEP;
1194 		}
1195 
1196 		win_length = max - min + 1;
1197 		/* get the largest pass window */
1198 		if (win_length > target_win_length) {
1199 			target_win_length = win_length;
1200 			target_min = min;
1201 			target_max = max;
1202 		}
1203 
1204 		/* continue to find the next pass window */
1205 		min = max + CV18XX_TUNE_STEP;
1206 	}
1207 
1208 	cv18xx_sdhci_post_tuning(host);
1209 
1210 	/* use average delay to get the best timing */
1211 	avg = (target_min + target_max) / 2;
1212 	cv18xx_sdhci_set_tap(host, avg);
1213 	ret = mmc_send_tuning(host->mmc, opcode, NULL);
1214 
1215 	dev_dbg(mmc_dev(host->mmc), "tuning %s at 0x%x ret %d\n",
1216 		ret ? "failed" : "passed", avg, ret);
1217 
1218 	return ret;
1219 }
1220 
1221 static inline void sg2042_sdhci_phy_init(struct sdhci_host *host)
1222 {
1223 	u32 val;
1224 
1225 	/* Asset phy reset & set tx drive strength */
1226 	val = sdhci_readl(host, PHY_CNFG_R);
1227 	val &= ~PHY_CNFG_RSTN_DEASSERT;
1228 	val |= FIELD_PREP(PHY_CNFG_PHY_PWRGOOD_MASK, 1);
1229 	val |= FIELD_PREP(PHY_CNFG_PAD_SP_MASK, PHY_CNFG_PAD_SP_SG2042);
1230 	val |= FIELD_PREP(PHY_CNFG_PAD_SN_MASK, PHY_CNFG_PAD_SN_SG2042);
1231 	sdhci_writel(host, val, PHY_CNFG_R);
1232 
1233 	/* Configure phy pads */
1234 	val = PHY_PAD_RXSEL_3V3;
1235 	val |= FIELD_PREP(PHY_PAD_WEAKPULL_MASK, PHY_PAD_WEAKPULL_PULLUP);
1236 	val |= FIELD_PREP(PHY_PAD_TXSLEW_CTRL_P_MASK, PHY_PAD_TXSLEW_CTRL_P);
1237 	val |= FIELD_PREP(PHY_PAD_TXSLEW_CTRL_N_MASK, PHY_PAD_TXSLEW_CTRL_N_SG2042);
1238 	sdhci_writew(host, val, PHY_CMDPAD_CNFG_R);
1239 	sdhci_writew(host, val, PHY_DATAPAD_CNFG_R);
1240 	sdhci_writew(host, val, PHY_RSTNPAD_CNFG_R);
1241 
1242 	val = PHY_PAD_RXSEL_3V3;
1243 	val |= FIELD_PREP(PHY_PAD_TXSLEW_CTRL_P_MASK, PHY_PAD_TXSLEW_CTRL_P);
1244 	val |= FIELD_PREP(PHY_PAD_TXSLEW_CTRL_N_MASK, PHY_PAD_TXSLEW_CTRL_N_SG2042);
1245 	sdhci_writew(host, val, PHY_CLKPAD_CNFG_R);
1246 
1247 	val = PHY_PAD_RXSEL_3V3;
1248 	val |= FIELD_PREP(PHY_PAD_WEAKPULL_MASK, PHY_PAD_WEAKPULL_PULLDOWN);
1249 	val |= FIELD_PREP(PHY_PAD_TXSLEW_CTRL_P_MASK, PHY_PAD_TXSLEW_CTRL_P);
1250 	val |= FIELD_PREP(PHY_PAD_TXSLEW_CTRL_N_MASK, PHY_PAD_TXSLEW_CTRL_N_SG2042);
1251 	sdhci_writew(host, val, PHY_STBPAD_CNFG_R);
1252 
1253 	/* Configure delay line */
1254 	/* Enable fixed delay */
1255 	sdhci_writeb(host, PHY_SDCLKDL_CNFG_EXTDLY_EN, PHY_SDCLKDL_CNFG_R);
1256 	/*
1257 	 * Set delay line.
1258 	 * Its recommended that bit UPDATE_DC[4] is 1 when SDCLKDL_DC is being written.
1259 	 * Ensure UPDATE_DC[4] is '0' when not updating code.
1260 	 */
1261 	val = sdhci_readb(host, PHY_SDCLKDL_CNFG_R);
1262 	val |= PHY_SDCLKDL_CNFG_UPDATE;
1263 	sdhci_writeb(host, val, PHY_SDCLKDL_CNFG_R);
1264 	/* Add 10 * 70ps = 0.7ns for output delay */
1265 	sdhci_writeb(host, 10, PHY_SDCLKDL_DC_R);
1266 	val = sdhci_readb(host, PHY_SDCLKDL_CNFG_R);
1267 	val &= ~(PHY_SDCLKDL_CNFG_UPDATE);
1268 	sdhci_writeb(host, val, PHY_SDCLKDL_CNFG_R);
1269 
1270 	/* Set SMPLDL_CNFG, Bypass */
1271 	sdhci_writeb(host, PHY_SMPLDL_CNFG_BYPASS_EN, PHY_SMPLDL_CNFG_R);
1272 
1273 	/* Set ATDL_CNFG, tuning clk not use for init */
1274 	val = FIELD_PREP(PHY_ATDL_CNFG_INPSEL_MASK, PHY_ATDL_CNFG_INPSEL_SG2042);
1275 	sdhci_writeb(host, val, PHY_ATDL_CNFG_R);
1276 
1277 	/* Deasset phy reset */
1278 	val = sdhci_readl(host, PHY_CNFG_R);
1279 	val |= PHY_CNFG_RSTN_DEASSERT;
1280 	sdhci_writel(host, val, PHY_CNFG_R);
1281 }
1282 
1283 static void sg2042_sdhci_reset(struct sdhci_host *host, u8 mask)
1284 {
1285 	sdhci_reset(host, mask);
1286 
1287 	if (mask & SDHCI_RESET_ALL)
1288 		sg2042_sdhci_phy_init(host);
1289 }
1290 
1291 static int sg2042_init(struct device *dev, struct sdhci_host *host,
1292 		       struct dwcmshc_priv *dwc_priv)
1293 {
1294 	static const char * const clk_ids[] = {"timer"};
1295 
1296 	return dwcmshc_get_enable_other_clks(mmc_dev(host->mmc), dwc_priv,
1297 					     ARRAY_SIZE(clk_ids), clk_ids);
1298 }
1299 
1300 /*
1301  * HPE GSC-specific vendor configuration: disable command conflict check
1302  * and program Auto-Tuning Control register.
1303  */
1304 static void dwcmshc_hpe_vendor_specific(struct sdhci_host *host)
1305 {
1306 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1307 	struct dwcmshc_priv *dwc_priv = sdhci_pltfm_priv(pltfm_host);
1308 	u32 atctrl;
1309 	u8 extra;
1310 
1311 	extra = sdhci_readb(host, dwc_priv->vendor_specific_area1 + DWCMSHC_HOST_CTRL3);
1312 	extra &= ~DWCMSHC_HOST_CTRL3_CMD_CONFLICT;
1313 	sdhci_writeb(host, extra, dwc_priv->vendor_specific_area1 + DWCMSHC_HOST_CTRL3);
1314 
1315 	atctrl = AT_CTRL_AT_EN | AT_CTRL_SWIN_TH_EN | AT_CTRL_TUNE_CLK_STOP_EN |
1316 		FIELD_PREP(AT_CTRL_PRE_CHANGE_DLY_MASK, 3) |
1317 		FIELD_PREP(AT_CTRL_POST_CHANGE_DLY_MASK, AT_CTRL_POST_CHANGE_DLY) |
1318 		FIELD_PREP(AT_CTRL_SWIN_TH_VAL_MASK, 2);
1319 	sdhci_writel(host, atctrl, dwc_priv->vendor_specific_area1 + DWCMSHC_EMMC_ATCTRL);
1320 }
1321 
1322 static void dwcmshc_hpe_set_emmc(struct sdhci_host *host)
1323 {
1324 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1325 	struct dwcmshc_priv *dwc_priv = sdhci_pltfm_priv(pltfm_host);
1326 	u16 ctrl;
1327 
1328 	ctrl = sdhci_readw(host, dwc_priv->vendor_specific_area1 + DWCMSHC_EMMC_CONTROL);
1329 	ctrl |= DWCMSHC_CARD_IS_EMMC;
1330 	sdhci_writew(host, ctrl, dwc_priv->vendor_specific_area1 + DWCMSHC_EMMC_CONTROL);
1331 }
1332 
1333 static void dwcmshc_hpe_reset(struct sdhci_host *host, u8 mask)
1334 {
1335 	dwcmshc_reset(host, mask);
1336 	dwcmshc_hpe_vendor_specific(host);
1337 	dwcmshc_hpe_set_emmc(host);
1338 }
1339 
1340 static void dwcmshc_hpe_set_uhs_signaling(struct sdhci_host *host, unsigned int timing)
1341 {
1342 	dwcmshc_set_uhs_signaling(host, timing);
1343 	dwcmshc_hpe_set_emmc(host);
1344 }
1345 
1346 /*
1347  * HPE GSC eMMC controller clock setup.
1348  *
1349  * The GSC SoC wires the freq_sel field of SDHCI_CLOCK_CONTROL directly to a
1350  * clock mux rather than a divider. Force freq_sel = 1 when running at
1351  * 200 MHz (HS200) so the mux selects the correct clock source.
1352  */
1353 static void dwcmshc_hpe_set_clock(struct sdhci_host *host, unsigned int clock)
1354 {
1355 	u16 clk;
1356 
1357 	host->mmc->actual_clock = 0;
1358 
1359 	sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
1360 
1361 	if (clock == 0)
1362 		return;
1363 
1364 	clk = sdhci_calc_clk(host, clock, &host->mmc->actual_clock);
1365 
1366 	if (host->mmc->actual_clock == 200000000)
1367 		clk |= (1 << SDHCI_DIVIDER_SHIFT);
1368 
1369 	sdhci_enable_clk(host, clk);
1370 }
1371 
1372 /*
1373  * HPE GSC eMMC controller init.
1374  *
1375  * The GSC SoC requires configuring MSHCCS.  Bit 18 (SCGSyncDis) disables clock
1376  * synchronisation for phase-select values going to the HS200 RX delay lines,
1377  * allowing the card clock to be stopped while the delay selection settles and
1378  * the phase shift is applied.  This must be used together with the ATCTRL
1379  * settings programmed in dwcmshc_hpe_vendor_specific():
1380  *   AT_CTRL_R.TUNE_CLK_STOP_EN  = 0x1
1381  *   AT_CTRL_R.POST_CHANGE_DLY   = 0x3
1382  *   AT_CTRL_R.PRE_CHANGE_DLY    = 0x3
1383  *
1384  * The DTS node provides a syscon phandle ('hpe,gxp-sysreg') with the
1385  * MSHCCS register offset as an argument.
1386  */
1387 static int dwcmshc_hpe_gsc_init(struct device *dev, struct sdhci_host *host,
1388 				struct dwcmshc_priv *dwc_priv)
1389 {
1390 	unsigned int reg_offset;
1391 	struct regmap *soc_ctrl;
1392 	int ret;
1393 
1394 	/* Disable cmd conflict check and configure auto-tuning */
1395 	dwcmshc_hpe_vendor_specific(host);
1396 
1397 	/* Look up the GXP sysreg syscon and MSHCCS offset */
1398 	soc_ctrl = syscon_regmap_lookup_by_phandle_args(dev->of_node,
1399 							"hpe,gxp-sysreg",
1400 							1, &reg_offset);
1401 	if (IS_ERR(soc_ctrl)) {
1402 		dev_err(dev, "failed to get hpe,gxp-sysreg syscon\n");
1403 		return PTR_ERR(soc_ctrl);
1404 	}
1405 
1406 	/* Set SCGSyncDis (bit 18) to disable sync on HS200 RX delay lines */
1407 	ret = regmap_update_bits(soc_ctrl, reg_offset,
1408 				 HPE_GSC_MSHCCS_SCGSYNCDIS,
1409 				 HPE_GSC_MSHCCS_SCGSYNCDIS);
1410 	if (ret) {
1411 		dev_err(dev, "failed to set SCGSyncDis in MSHCCS\n");
1412 		return ret;
1413 	}
1414 
1415 	sdhci_enable_v4_mode(host);
1416 
1417 	return 0;
1418 }
1419 
1420 static void sdhci_eic7700_set_clock(struct sdhci_host *host, unsigned int clock)
1421 {
1422 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1423 	u16 clk;
1424 
1425 	host->mmc->actual_clock = clock;
1426 
1427 	if (clock == 0) {
1428 		sdhci_set_clock(host, clock);
1429 		return;
1430 	}
1431 
1432 	clk_set_rate(pltfm_host->clk, clock);
1433 
1434 	clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1435 	sdhci_enable_clk(host, clk);
1436 }
1437 
1438 static void sdhci_eic7700_config_phy_delay(struct sdhci_host *host, int delay)
1439 {
1440 	delay &= PHY_CLK_MAX_DELAY_MASK;
1441 
1442 	/* phy clk delay line config */
1443 	sdhci_writeb(host, PHY_SDCLKDL_CNFG_UPDATE, PHY_SDCLKDL_CNFG_R);
1444 	sdhci_writeb(host, delay, PHY_SDCLKDL_DC_R);
1445 	sdhci_writeb(host, 0x0, PHY_SDCLKDL_CNFG_R);
1446 }
1447 
1448 static void sdhci_eic7700_config_phy(struct sdhci_host *host)
1449 {
1450 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1451 	struct dwcmshc_priv *dwc_priv = sdhci_pltfm_priv(pltfm_host);
1452 	u32 emmc_caps = MMC_CAP2_NO_SD | MMC_CAP2_NO_SDIO;
1453 	struct eic7700_priv *priv = dwc_priv->priv;
1454 	unsigned int val, drv;
1455 
1456 	drv = FIELD_PREP(PHY_CNFG_PAD_SP_MASK, priv->drive_impedance & 0xF);
1457 	drv |= FIELD_PREP(PHY_CNFG_PAD_SN_MASK, (priv->drive_impedance >> 4) & 0xF);
1458 
1459 	if ((host->mmc->caps2 & emmc_caps) == emmc_caps) {
1460 		val = sdhci_readw(host, dwc_priv->vendor_specific_area1 + DWCMSHC_EMMC_CONTROL);
1461 		val |= DWCMSHC_CARD_IS_EMMC;
1462 		sdhci_writew(host, val, dwc_priv->vendor_specific_area1 + DWCMSHC_EMMC_CONTROL);
1463 	}
1464 
1465 	/* reset phy, config phy's pad */
1466 	sdhci_writel(host, drv | ~PHY_CNFG_RSTN_DEASSERT, PHY_CNFG_R);
1467 
1468 	/* configure phy pads */
1469 	val = FIELD_PREP(PHY_PAD_TXSLEW_CTRL_P_MASK, PHY_PAD_TXSLEW_CTRL_N_SG2042);
1470 	val |= FIELD_PREP(PHY_PAD_TXSLEW_CTRL_N_MASK, PHY_PAD_TXSLEW_CTRL_N_SG2042);
1471 	val |= FIELD_PREP(PHY_PAD_WEAKPULL_MASK, PHY_PAD_WEAKPULL_PULLUP);
1472 	val |= PHY_PAD_RXSEL_1V8;
1473 	sdhci_writew(host, val, PHY_CMDPAD_CNFG_R);
1474 	sdhci_writew(host, val, PHY_DATAPAD_CNFG_R);
1475 	sdhci_writew(host, val, PHY_RSTNPAD_CNFG_R);
1476 
1477 	/* Clock PAD Setting */
1478 	val = FIELD_PREP(PHY_PAD_TXSLEW_CTRL_P_MASK, PHY_PAD_TXSLEW_CTRL_N_SG2042);
1479 	val |= FIELD_PREP(PHY_PAD_TXSLEW_CTRL_N_MASK, PHY_PAD_TXSLEW_CTRL_N_SG2042);
1480 	sdhci_writew(host, val, PHY_CLKPAD_CNFG_R);
1481 
1482 	/* PHY strobe PAD setting (EMMC only) */
1483 	if ((host->mmc->caps2 & emmc_caps) == emmc_caps) {
1484 		val = FIELD_PREP(PHY_PAD_TXSLEW_CTRL_P_MASK, PHY_PAD_TXSLEW_CTRL_N_SG2042);
1485 		val |= FIELD_PREP(PHY_PAD_TXSLEW_CTRL_N_MASK, PHY_PAD_TXSLEW_CTRL_N_SG2042);
1486 		val |= PHY_PAD_RXSEL_1V8;
1487 		sdhci_writew(host, val, PHY_STBPAD_CNFG_R);
1488 	}
1489 	usleep_range(2000, 3000);
1490 	sdhci_writel(host, drv | PHY_CNFG_RSTN_DEASSERT, PHY_CNFG_R);
1491 	sdhci_eic7700_config_phy_delay(host, dwc_priv->delay_line);
1492 }
1493 
1494 static void sdhci_eic7700_reset(struct sdhci_host *host, u8 mask)
1495 {
1496 	dwcmshc_reset(host, mask);
1497 
1498 	/* after reset all, the phy's config will be clear */
1499 	if (mask == SDHCI_RESET_ALL)
1500 		sdhci_eic7700_config_phy(host);
1501 }
1502 
1503 static int sdhci_eic7700_reset_init(struct device *dev, struct eic7700_priv *priv)
1504 {
1505 	int ret;
1506 
1507 	priv->reset = devm_reset_control_array_get_optional_exclusive(dev);
1508 	if (IS_ERR(priv->reset)) {
1509 		ret = PTR_ERR(priv->reset);
1510 		dev_err(dev, "failed to get reset control %d\n", ret);
1511 		return ret;
1512 	}
1513 
1514 	ret = reset_control_assert(priv->reset);
1515 	if (ret) {
1516 		dev_err(dev, "Failed to assert reset signals: %d\n", ret);
1517 		return ret;
1518 	}
1519 	usleep_range(2000, 2100);
1520 	ret = reset_control_deassert(priv->reset);
1521 	if (ret) {
1522 		dev_err(dev, "Failed to deassert reset signals: %d\n", ret);
1523 		return ret;
1524 	}
1525 
1526 	return ret;
1527 }
1528 
1529 static unsigned int eic7700_convert_drive_impedance_ohm(struct device *dev, unsigned int dr_ohm)
1530 {
1531 	switch (dr_ohm) {
1532 	case 100:
1533 		return PHYCTRL_DR_100OHM;
1534 	case 66:
1535 		return PHYCTRL_DR_66OHM;
1536 	case 50:
1537 		return PHYCTRL_DR_50OHM;
1538 	case 40:
1539 		return PHYCTRL_DR_40OHM;
1540 	case 33:
1541 		return PHYCTRL_DR_33OHM;
1542 	}
1543 
1544 	dev_warn(dev, "Invalid value %u for drive-impedance-ohms.\n", dr_ohm);
1545 	return PHYCTRL_DR_50OHM;
1546 }
1547 
1548 static int sdhci_eic7700_delay_tuning(struct sdhci_host *host, u32 opcode)
1549 {
1550 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1551 	struct dwcmshc_priv *dwc_priv = sdhci_pltfm_priv(pltfm_host);
1552 	int delay_min = -1;
1553 	int delay_max = -1;
1554 	int cmd_error = 0;
1555 	int delay = 0;
1556 	int i = 0;
1557 	int ret;
1558 
1559 	for (i = 0; i <= PHY_DELAY_CODE_MAX; i++) {
1560 		sdhci_eic7700_config_phy_delay(host, i);
1561 		ret = mmc_send_tuning(host->mmc, opcode, &cmd_error);
1562 		if (ret) {
1563 			host->ops->reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
1564 			usleep_range(200, 210);
1565 			if (delay_min != -1 && delay_max != -1)
1566 				break;
1567 		} else {
1568 			if (delay_min == -1) {
1569 				delay_min = i;
1570 				continue;
1571 			} else {
1572 				delay_max = i;
1573 				continue;
1574 			}
1575 		}
1576 	}
1577 	if (delay_min == -1 && delay_max == -1) {
1578 		pr_err("%s: delay code tuning failed!\n", mmc_hostname(host->mmc));
1579 		sdhci_eic7700_config_phy_delay(host, dwc_priv->delay_line);
1580 		return ret;
1581 	}
1582 
1583 	delay = (delay_min + delay_max) / 2;
1584 	sdhci_eic7700_config_phy_delay(host, delay);
1585 
1586 	return 0;
1587 }
1588 
1589 static int sdhci_eic7700_phase_code_tuning(struct sdhci_host *host, u32 opcode)
1590 {
1591 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1592 	struct dwcmshc_priv *priv = sdhci_pltfm_priv(pltfm_host);
1593 	u32 emmc_caps = MMC_CAP2_NO_SD | MMC_CAP2_NO_SDIO;
1594 	int phase_code = -1;
1595 	int code_range = -1;
1596 	int code_min = -1;
1597 	int code_max = -1;
1598 	int cmd_error = 0;
1599 	bool is_emmc;
1600 	int ret = 0;
1601 	int i = 0;
1602 
1603 	is_emmc = (host->mmc->caps2 & emmc_caps) == emmc_caps;
1604 
1605 	for (i = 0; i <= MAX_PHASE_CODE; i++) {
1606 		/* Centered Phase code */
1607 		sdhci_writew(host, i, priv->vendor_specific_area1 + DWCMSHC_AT_STAT);
1608 		ret = mmc_send_tuning(host->mmc, opcode, &cmd_error);
1609 		host->ops->reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
1610 
1611 		if (ret) {
1612 			/* SD/SDIO specific range tracking */
1613 			if (!is_emmc && code_min != -1 && code_max != -1) {
1614 				if (code_max - code_min > code_range) {
1615 					code_range = code_max - code_min;
1616 					phase_code = (code_min + code_max) / 2;
1617 					if (code_range > TUNING_RANGE_THRESHOLD)
1618 						break;
1619 				}
1620 				code_min = -1;
1621 				code_max = -1;
1622 			}
1623 			/* EMMC breaks after first valid range */
1624 			if (is_emmc && code_min != -1 && code_max != -1)
1625 				break;
1626 		} else {
1627 			/* Track valid phase code range */
1628 			if (code_min == -1) {
1629 				code_min = i;
1630 				if (is_emmc)
1631 					continue;
1632 			}
1633 			code_max = i;
1634 			if (!is_emmc && i == MAX_PHASE_CODE) {
1635 				if (code_max - code_min > code_range) {
1636 					code_range = code_max - code_min;
1637 					phase_code = (code_min + code_max) / 2;
1638 				}
1639 			}
1640 		}
1641 	}
1642 
1643 	/* Handle tuning failure case */
1644 	if ((!is_emmc && phase_code == -1) ||
1645 	    (is_emmc && code_min == -1 && code_max == -1)) {
1646 		pr_err("%s: phase code tuning failed!\n", mmc_hostname(host->mmc));
1647 		sdhci_writew(host, 0, priv->vendor_specific_area1 + DWCMSHC_AT_STAT);
1648 		return -EIO;
1649 	}
1650 	if (is_emmc)
1651 		phase_code = (code_min + code_max) / 2;
1652 
1653 	sdhci_writew(host, phase_code, priv->vendor_specific_area1 + DWCMSHC_AT_STAT);
1654 
1655 	/* SD/SDIO specific final verification */
1656 	if (!is_emmc) {
1657 		ret = mmc_send_tuning(host->mmc, opcode, &cmd_error);
1658 		host->ops->reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
1659 		if (ret) {
1660 			pr_err("%s: Final phase code 0x%x verification failed!\n",
1661 			       mmc_hostname(host->mmc), phase_code);
1662 			return ret;
1663 		}
1664 	}
1665 
1666 	return 0;
1667 }
1668 
1669 static int sdhci_eic7700_executing_tuning(struct sdhci_host *host, u32 opcode)
1670 {
1671 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1672 	struct dwcmshc_priv *priv = sdhci_pltfm_priv(pltfm_host);
1673 	u32 emmc_caps = MMC_CAP2_NO_SD | MMC_CAP2_NO_SDIO;
1674 	int ret = 0;
1675 	u16 ctrl;
1676 	u32 val;
1677 
1678 	ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1679 	ctrl &= ~SDHCI_CTRL_TUNED_CLK;
1680 	sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1681 
1682 	val = sdhci_readl(host, priv->vendor_specific_area1 + DWCMSHC_EMMC_ATCTRL);
1683 	val |= AT_CTRL_SW_TUNE_EN;
1684 	sdhci_writew(host, val, priv->vendor_specific_area1 + DWCMSHC_EMMC_ATCTRL);
1685 
1686 	sdhci_writew(host, 0, priv->vendor_specific_area1 + DWCMSHC_AT_STAT);
1687 	sdhci_writew(host, 0x0, SDHCI_CMD_DATA);
1688 
1689 	if ((host->mmc->caps2 & emmc_caps) == emmc_caps) {
1690 		ret = sdhci_eic7700_delay_tuning(host, opcode);
1691 		if (ret)
1692 			return ret;
1693 	}
1694 
1695 	ret = sdhci_eic7700_phase_code_tuning(host, opcode);
1696 	if (ret)
1697 		return ret;
1698 
1699 	return 0;
1700 }
1701 
1702 static void sdhci_eic7700_set_uhs_signaling(struct sdhci_host *host, unsigned int timing)
1703 {
1704 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1705 	struct dwcmshc_priv *priv = sdhci_pltfm_priv(pltfm_host);
1706 	u8 status;
1707 	u32 val;
1708 	int ret;
1709 
1710 	dwcmshc_set_uhs_signaling(host, timing);
1711 
1712 	/* here need make dll locked when in hs400 at 200MHz */
1713 	if (timing == MMC_TIMING_MMC_HS400 && host->clock == 200000000) {
1714 		val = sdhci_readl(host, priv->vendor_specific_area1 + DWCMSHC_EMMC_ATCTRL);
1715 		val &= ~(FIELD_PREP(AT_CTRL_POST_CHANGE_DLY_MASK, AT_CTRL_POST_CHANGE_DLY));
1716 		/* 2-cycle latency */
1717 		val |= FIELD_PREP(AT_CTRL_POST_CHANGE_DLY_MASK, 0x2);
1718 		sdhci_writew(host, val, priv->vendor_specific_area1 + DWCMSHC_EMMC_ATCTRL);
1719 
1720 		sdhci_writeb(host, FIELD_PREP(PHY_DLL_CNFG1_SLVDLY_MASK, PHY_DLL_CNFG1_SLVDLY) |
1721 			     0x3, PHY_DLL_CNFG1_R);/* DLL wait cycle input */
1722 		/* DLL jump step input */
1723 		sdhci_writeb(host, 0x02, PHY_DLL_CNFG2_R);
1724 		sdhci_writeb(host, FIELD_PREP(PHY_DLLDL_CNFG_SLV_INPSEL_MASK,
1725 					      PHY_DLLDL_CNFG_SLV_INPSEL), PHY_DLLDL_CNFG_R);
1726 		/* Sets the value of DLL's offset input */
1727 		sdhci_writeb(host, 0x00, PHY_DLL_OFFST_R);
1728 		/*
1729 		 * Sets the value of DLL's olbt loadval input. Controls the Ibt
1730 		 * timer's timeout value at which DLL runs a revalidation cycle.
1731 		 */
1732 		sdhci_writew(host, 0xffff, PHY_DLLBT_CNFG_R);
1733 		sdhci_writeb(host, PHY_DLL_CTRL_ENABLE, PHY_DLL_CTRL_R);
1734 		usleep_range(100, 110);
1735 
1736 		ret = read_poll_timeout(sdhci_readb, status, status & DLL_LOCK_STS, 100, 1000000,
1737 					false, host, PHY_DLL_STATUS_R);
1738 		if (ret) {
1739 			pr_err("%s: DLL lock timeout! status: 0x%x\n",
1740 			       mmc_hostname(host->mmc), status);
1741 			return;
1742 		}
1743 
1744 		status = sdhci_readb(host, PHY_DLL_STATUS_R);
1745 		if (status & DLL_ERROR_STS) {
1746 			pr_err("%s: DLL lock failed!err_status:0x%x\n",
1747 			       mmc_hostname(host->mmc), status);
1748 		}
1749 	}
1750 }
1751 
1752 static void sdhci_eic7700_set_uhs_wrapper(struct sdhci_host *host, unsigned int timing)
1753 {
1754 	u32 emmc_caps = MMC_CAP2_NO_SD | MMC_CAP2_NO_SDIO;
1755 
1756 	if ((host->mmc->caps2 & emmc_caps) != emmc_caps)
1757 		sdhci_set_uhs_signaling(host, timing);
1758 	else
1759 		sdhci_eic7700_set_uhs_signaling(host, timing);
1760 }
1761 
1762 static int eic7700_init(struct device *dev, struct sdhci_host *host, struct dwcmshc_priv *dwc_priv)
1763 {
1764 	u32 emmc_caps = MMC_CAP2_NO_SD | MMC_CAP2_NO_SDIO;
1765 	u32 sd_caps = MMC_CAP2_NO_MMC | MMC_CAP2_NO_SDIO;
1766 	unsigned int val, hsp_int_status, hsp_pwr_ctrl;
1767 	static const char * const clk_ids[] = {"axi"};
1768 	struct of_phandle_args args;
1769 	struct eic7700_priv *priv;
1770 	struct regmap *hsp_regmap;
1771 	int ret;
1772 
1773 	priv = devm_kzalloc(dev, sizeof(struct eic7700_priv), GFP_KERNEL);
1774 	if (!priv)
1775 		return -ENOMEM;
1776 
1777 	dwc_priv->priv = priv;
1778 
1779 	ret = sdhci_eic7700_reset_init(dev, dwc_priv->priv);
1780 	if (ret)
1781 		return dev_err_probe(dev, ret, "failed to reset\n");
1782 
1783 	ret = dwcmshc_get_enable_other_clks(mmc_dev(host->mmc), dwc_priv,
1784 					    ARRAY_SIZE(clk_ids), clk_ids);
1785 	if (ret)
1786 		return ret;
1787 
1788 	ret = of_parse_phandle_with_fixed_args(dev->of_node, "eswin,hsp-sp-csr", 2, 0, &args);
1789 	if (ret)
1790 		return dev_err_probe(dev, ret, "Fail to parse 'eswin,hsp-sp-csr' phandle\n");
1791 
1792 	hsp_regmap = syscon_node_to_regmap(args.np);
1793 	if (IS_ERR(hsp_regmap)) {
1794 		of_node_put(args.np);
1795 		return dev_err_probe(dev, PTR_ERR(hsp_regmap),
1796 				     "Failed to get regmap for 'eswin,hsp-sp-csr'\n");
1797 	}
1798 	hsp_int_status = args.args[0];
1799 	hsp_pwr_ctrl = args.args[1];
1800 	of_node_put(args.np);
1801 	/*
1802 	 * Assert clock stability: write EIC7700_INT_CLK_STABLE to hsp_int_status.
1803 	 * This signals to the eMMC controller that platform clocks (card, ACLK,
1804 	 * BCLK, TMCLK) are enabled and stable.
1805 	 */
1806 	regmap_write(hsp_regmap, hsp_int_status, EIC7700_INT_CLK_STABLE);
1807 	/*
1808 	 * Assert voltage stability: write EIC7700_HOST_VAL_STABLE to hsp_pwr_ctrl.
1809 	 * This signals that VDD is stable and permits transition to high-speed
1810 	 * modes (e.g., UHS-I).
1811 	 */
1812 	regmap_write(hsp_regmap, hsp_pwr_ctrl, EIC7700_HOST_VAL_STABLE);
1813 
1814 	if ((host->mmc->caps2 & emmc_caps) == emmc_caps)
1815 		dwc_priv->delay_line = PHY_DELAY_CODE_EMMC;
1816 	else if ((host->mmc->caps2 & sd_caps) == sd_caps)
1817 		dwc_priv->delay_line = PHY_DELAY_CODE_SD;
1818 	else
1819 		dwc_priv->delay_line = PHY_DELAY_CODE_SDIO;
1820 
1821 	if (!of_property_read_u32(dev->of_node, "eswin,drive-impedance-ohms", &val))
1822 		priv->drive_impedance = eic7700_convert_drive_impedance_ohm(dev, val);
1823 	return 0;
1824 }
1825 
1826 static void dwcmshc_k230_sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
1827 {
1828 	u16 clk;
1829 
1830 	sdhci_set_clock(host, clock);
1831 
1832 	clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1833 	/*
1834 	 * It is necessary to enable SDHCI_PROG_CLOCK_MODE. This is a
1835 	 * vendor-specific quirk. If this is not done, the eMMC will be
1836 	 * unable to read or write.
1837 	 */
1838 	clk |= SDHCI_PROG_CLOCK_MODE;
1839 	sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1840 }
1841 
1842 static void sdhci_k230_config_phy_delay(struct sdhci_host *host)
1843 {
1844 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1845 	struct dwcmshc_priv *dwc_priv = sdhci_pltfm_priv(pltfm_host);
1846 	u32 val;
1847 
1848 	sdhci_writeb(host, PHY_COMMDL_CNFG_DLSTEP_SEL, PHY_COMMDL_CNFG);
1849 	sdhci_writeb(host, 0x0, PHY_SDCLKDL_CNFG_R);
1850 	sdhci_writeb(host, PHY_SDCLKDL_DC_INITIAL, PHY_SDCLKDL_DC_R);
1851 
1852 	val = PHY_SMPLDL_CNFG_EXTDLY_EN;
1853 	val |= FIELD_PREP(PHY_SMPLDL_CNFG_INPSEL_MASK, PHY_SMPLDL_CNFG_INPSEL);
1854 	sdhci_writeb(host, val, PHY_SMPLDL_CNFG_R);
1855 
1856 	sdhci_writeb(host, FIELD_PREP(PHY_ATDL_CNFG_INPSEL_MASK, PHY_ATDL_CNFG_INPSEL),
1857 		     PHY_ATDL_CNFG_R);
1858 
1859 	val = sdhci_readl(host, dwc_priv->vendor_specific_area1 + DWCMSHC_EMMC_ATCTRL);
1860 	val |= AT_CTRL_TUNE_CLK_STOP_EN;
1861 	val |= FIELD_PREP(AT_CTRL_PRE_CHANGE_DLY_MASK, AT_CTRL_PRE_CHANGE_DLY);
1862 	val |= FIELD_PREP(AT_CTRL_POST_CHANGE_DLY_MASK, AT_CTRL_POST_CHANGE_DLY);
1863 	sdhci_writel(host, val, dwc_priv->vendor_specific_area1 + DWCMSHC_EMMC_ATCTRL);
1864 	sdhci_writel(host, 0x0, dwc_priv->vendor_specific_area1 + DWCMSHC_AT_STAT);
1865 }
1866 
1867 static int dwcmshc_k230_phy_init(struct sdhci_host *host)
1868 {
1869 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1870 	struct dwcmshc_priv *dwc_priv = sdhci_pltfm_priv(pltfm_host);
1871 	u32 rxsel;
1872 	u32 val;
1873 	u32 reg;
1874 	int ret;
1875 
1876 	/* reset phy */
1877 	sdhci_writew(host, 0, PHY_CNFG_R);
1878 
1879 	/* Disable the clock */
1880 	sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
1881 
1882 	rxsel = dwc_priv->flags & FLAG_IO_FIXED_1V8 ? PHY_PAD_RXSEL_1V8 : PHY_PAD_RXSEL_3V3;
1883 
1884 	val = rxsel;
1885 	val |= FIELD_PREP(PHY_PAD_TXSLEW_CTRL_P_MASK, PHY_PAD_TXSLEW_CTRL_P_k230);
1886 	val |= FIELD_PREP(PHY_PAD_TXSLEW_CTRL_N_MASK, PHY_PAD_TXSLEW_CTRL_N_k230);
1887 	val |= FIELD_PREP(PHY_PAD_WEAKPULL_MASK, PHY_PAD_WEAKPULL_PULLUP);
1888 
1889 	sdhci_writew(host, val, PHY_CMDPAD_CNFG_R);
1890 	sdhci_writew(host, val, PHY_DATAPAD_CNFG_R);
1891 	sdhci_writew(host, val, PHY_RSTNPAD_CNFG_R);
1892 
1893 	val = rxsel;
1894 	val |= FIELD_PREP(PHY_PAD_TXSLEW_CTRL_P_MASK, PHY_PAD_TXSLEW_CTRL_P_k230);
1895 	val |= FIELD_PREP(PHY_PAD_TXSLEW_CTRL_N_MASK, PHY_PAD_TXSLEW_CTRL_N_k230);
1896 	sdhci_writew(host, val, PHY_CLKPAD_CNFG_R);
1897 
1898 	val = rxsel;
1899 	val |= FIELD_PREP(PHY_PAD_WEAKPULL_MASK, PHY_PAD_WEAKPULL_PULLDOWN);
1900 	val |= FIELD_PREP(PHY_PAD_TXSLEW_CTRL_P_MASK, PHY_PAD_TXSLEW_CTRL_P_k230);
1901 	val |= FIELD_PREP(PHY_PAD_TXSLEW_CTRL_N_MASK, PHY_PAD_TXSLEW_CTRL_N_k230);
1902 	sdhci_writew(host, val, PHY_STBPAD_CNFG_R);
1903 
1904 	sdhci_k230_config_phy_delay(host);
1905 
1906 	/* Wait max 150 ms */
1907 	ret = read_poll_timeout(sdhci_readl, reg,
1908 				(reg & FIELD_PREP(PHY_CNFG_PHY_PWRGOOD_MASK, 1)),
1909 				10, 150000, false, host, PHY_CNFG_R);
1910 	if (ret) {
1911 		dev_err(mmc_dev(host->mmc), "READ PHY PWRGOOD timeout!\n");
1912 		return -ETIMEDOUT;
1913 	}
1914 
1915 	reg = FIELD_PREP(PHY_CNFG_PAD_SN_MASK, PHY_CNFG_PAD_SN_k230) |
1916 	      FIELD_PREP(PHY_CNFG_PAD_SP_MASK, PHY_CNFG_PAD_SP_k230);
1917 	sdhci_writel(host, reg, PHY_CNFG_R);
1918 
1919 	/* de-assert the phy */
1920 	reg |= PHY_CNFG_RSTN_DEASSERT;
1921 	sdhci_writel(host, reg, PHY_CNFG_R);
1922 
1923 	return 0;
1924 }
1925 
1926 static void dwcmshc_k230_sdhci_reset(struct sdhci_host *host, u8 mask)
1927 {
1928 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1929 	struct dwcmshc_priv *dwc_priv = sdhci_pltfm_priv(pltfm_host);
1930 	const struct k230_pltfm_data *k230_pdata = to_pltfm_data(dwc_priv, k230);
1931 	u8 emmc_ctrl;
1932 
1933 	dwcmshc_reset(host, mask);
1934 
1935 	if (mask != SDHCI_RESET_ALL)
1936 		return;
1937 
1938 	emmc_ctrl = sdhci_readw(host, dwc_priv->vendor_specific_area1 + DWCMSHC_EMMC_CONTROL);
1939 	sdhci_writeb(host, emmc_ctrl, dwc_priv->vendor_specific_area1 + DWCMSHC_EMMC_CONTROL);
1940 
1941 	if (k230_pdata->is_emmc)
1942 		dwcmshc_k230_phy_init(host);
1943 	else
1944 		sdhci_writel(host, 0x0, dwc_priv->vendor_specific_area1 + DWCMSHC_HOST_CTRL3);
1945 }
1946 
1947 static int dwcmshc_k230_init(struct device *dev, struct sdhci_host *host,
1948 			     struct dwcmshc_priv *dwc_priv)
1949 {
1950 	const struct k230_pltfm_data *k230_pdata = to_pltfm_data(dwc_priv, k230);
1951 	static const char * const clk_ids[] = {"block", "timer", "axi"};
1952 	struct device_node *usb_phy_node;
1953 	struct k230_priv *k230_priv;
1954 	u32 data;
1955 	int ret;
1956 
1957 	k230_priv = devm_kzalloc(dev, sizeof(struct k230_priv), GFP_KERNEL);
1958 	if (!k230_priv)
1959 		return -ENOMEM;
1960 
1961 	dwc_priv->priv = k230_priv;
1962 
1963 	usb_phy_node = of_parse_phandle(dev->of_node, "canaan,usb-phy", 0);
1964 	if (!usb_phy_node)
1965 		return dev_err_probe(dev, -ENODEV, "Failed to find canaan,usb-phy phandle\n");
1966 
1967 	k230_priv->hi_sys_regmap = device_node_to_regmap(usb_phy_node);
1968 	of_node_put(usb_phy_node);
1969 
1970 	if (IS_ERR(k230_priv->hi_sys_regmap))
1971 		return dev_err_probe(dev, PTR_ERR(k230_priv->hi_sys_regmap),
1972 				     "Failed to get k230-usb-phy regmap\n");
1973 
1974 	ret = dwcmshc_get_enable_other_clks(mmc_dev(host->mmc), dwc_priv,
1975 					    ARRAY_SIZE(clk_ids), clk_ids);
1976 	if (ret)
1977 		return dev_err_probe(dev, ret, "Failed to get/enable k230 mmc other clocks\n");
1978 
1979 	if (k230_pdata->is_emmc) {
1980 		host->flags &= ~SDHCI_SIGNALING_330;
1981 		dwc_priv->flags |= FLAG_IO_FIXED_1V8;
1982 	} else {
1983 		host->mmc->caps |= MMC_CAP_SD_HIGHSPEED;
1984 		host->quirks2 |= SDHCI_QUIRK2_NO_1_8_V;
1985 	}
1986 
1987 	ret = regmap_read(k230_priv->hi_sys_regmap, k230_pdata->ctrl_reg, &data);
1988 	if (ret)
1989 		return dev_err_probe(dev, ret, "Failed to read control reg 0x%x\n",
1990 				     k230_pdata->ctrl_reg);
1991 
1992 	data |= k230_pdata->write_prot_bit | k230_pdata->vol_stable_bit;
1993 	ret = regmap_write(k230_priv->hi_sys_regmap, k230_pdata->ctrl_reg, data);
1994 	if (ret)
1995 		return dev_err_probe(dev, ret, "Failed to write control reg 0x%x\n",
1996 				     k230_pdata->ctrl_reg);
1997 
1998 	return 0;
1999 }
2000 
2001 static const struct sdhci_ops sdhci_dwcmshc_ops = {
2002 	.set_clock		= sdhci_set_clock,
2003 	.set_bus_width		= sdhci_set_bus_width,
2004 	.set_uhs_signaling	= dwcmshc_set_uhs_signaling,
2005 	.get_max_clock		= dwcmshc_get_max_clock,
2006 	.reset			= dwcmshc_reset,
2007 	.adma_write_desc	= dwcmshc_adma_write_desc,
2008 	.irq			= dwcmshc_cqe_irq_handler,
2009 };
2010 
2011 #ifdef CONFIG_ACPI
2012 static void dwcmshc_bf3_hw_reset(struct sdhci_host *host)
2013 {
2014 	struct arm_smccc_res res = { 0 };
2015 
2016 	arm_smccc_smc(BLUEFIELD_SMC_SET_EMMC_RST_N, 0, 0, 0, 0, 0, 0, 0, &res);
2017 
2018 	if (res.a0)
2019 		pr_err("%s: RST_N failed.\n", mmc_hostname(host->mmc));
2020 }
2021 
2022 static const struct sdhci_ops sdhci_dwcmshc_bf3_ops = {
2023 	.set_clock		= sdhci_set_clock,
2024 	.set_bus_width		= sdhci_set_bus_width,
2025 	.set_uhs_signaling	= dwcmshc_set_uhs_signaling,
2026 	.get_max_clock		= dwcmshc_get_max_clock,
2027 	.reset			= sdhci_reset,
2028 	.adma_write_desc	= dwcmshc_adma_write_desc,
2029 	.irq			= dwcmshc_cqe_irq_handler,
2030 	.hw_reset		= dwcmshc_bf3_hw_reset,
2031 };
2032 #endif
2033 
2034 static const struct sdhci_ops sdhci_dwcmshc_rk35xx_ops = {
2035 	.set_clock		= dwcmshc_rk3568_set_clock,
2036 	.set_bus_width		= sdhci_set_bus_width,
2037 	.set_uhs_signaling	= dwcmshc_set_uhs_signaling,
2038 	.get_max_clock		= rk35xx_get_max_clock,
2039 	.reset			= rk35xx_sdhci_reset,
2040 	.adma_write_desc	= dwcmshc_adma_write_desc,
2041 	.irq			= dwcmshc_cqe_irq_handler,
2042 };
2043 
2044 static const struct sdhci_ops sdhci_dwcmshc_th1520_ops = {
2045 	.set_clock		= sdhci_set_clock,
2046 	.set_bus_width		= sdhci_set_bus_width,
2047 	.set_uhs_signaling	= th1520_set_uhs_signaling,
2048 	.get_max_clock		= dwcmshc_get_max_clock,
2049 	.reset			= th1520_sdhci_reset,
2050 	.adma_write_desc	= dwcmshc_adma_write_desc,
2051 	.voltage_switch		= dwcmshc_phy_init,
2052 	.platform_execute_tuning = th1520_execute_tuning,
2053 };
2054 
2055 static const struct sdhci_ops sdhci_dwcmshc_cv18xx_ops = {
2056 	.set_clock		= sdhci_set_clock,
2057 	.set_bus_width		= sdhci_set_bus_width,
2058 	.set_uhs_signaling	= dwcmshc_set_uhs_signaling,
2059 	.get_max_clock		= dwcmshc_get_max_clock,
2060 	.reset			= cv18xx_sdhci_reset,
2061 	.adma_write_desc	= dwcmshc_adma_write_desc,
2062 	.platform_execute_tuning = cv18xx_sdhci_execute_tuning,
2063 };
2064 
2065 static const struct sdhci_ops sdhci_dwcmshc_sg2042_ops = {
2066 	.set_clock		= sdhci_set_clock,
2067 	.set_bus_width		= sdhci_set_bus_width,
2068 	.set_uhs_signaling	= dwcmshc_set_uhs_signaling,
2069 	.get_max_clock		= dwcmshc_get_max_clock,
2070 	.reset			= sg2042_sdhci_reset,
2071 	.adma_write_desc	= dwcmshc_adma_write_desc,
2072 	.platform_execute_tuning = th1520_execute_tuning,
2073 };
2074 
2075 static const struct sdhci_ops sdhci_dwcmshc_eic7700_ops = {
2076 	.set_clock = sdhci_eic7700_set_clock,
2077 	.get_max_clock = sdhci_pltfm_clk_get_max_clock,
2078 	.get_timeout_clock = sdhci_pltfm_clk_get_max_clock,
2079 	.set_bus_width = sdhci_set_bus_width,
2080 	.reset = sdhci_eic7700_reset,
2081 	.set_uhs_signaling = sdhci_eic7700_set_uhs_wrapper,
2082 	.set_power = sdhci_set_power_and_bus_voltage,
2083 	.irq = dwcmshc_cqe_irq_handler,
2084 	.adma_write_desc = dwcmshc_adma_write_desc,
2085 	.platform_execute_tuning = sdhci_eic7700_executing_tuning,
2086 };
2087 
2088 static const struct sdhci_ops sdhci_dwcmshc_k230_ops = {
2089 	.set_clock = dwcmshc_k230_sdhci_set_clock,
2090 	.set_bus_width = sdhci_set_bus_width,
2091 	.set_uhs_signaling = dwcmshc_set_uhs_signaling,
2092 	.get_max_clock = sdhci_pltfm_clk_get_max_clock,
2093 	.reset = dwcmshc_k230_sdhci_reset,
2094 	.adma_write_desc = dwcmshc_adma_write_desc,
2095 };
2096 
2097 static const struct dwcmshc_pltfm_data sdhci_dwcmshc_pdata = {
2098 	.pdata = {
2099 		.ops = &sdhci_dwcmshc_ops,
2100 		.quirks = SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN,
2101 		.quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN,
2102 	},
2103 };
2104 
2105 #ifdef CONFIG_ACPI
2106 static const struct dwcmshc_pltfm_data sdhci_dwcmshc_bf3_pdata = {
2107 	.pdata = {
2108 		.ops = &sdhci_dwcmshc_bf3_ops,
2109 		.quirks = SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN,
2110 		.quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN |
2111 			   SDHCI_QUIRK2_ACMD23_BROKEN,
2112 	},
2113 };
2114 #endif
2115 
2116 static const struct cqhci_host_ops rk35xx_cqhci_ops = {
2117 	.pre_enable	= rk35xx_sdhci_cqe_pre_enable,
2118 	.enable		= rk35xx_sdhci_cqe_enable,
2119 	.disable	= rk35xx_sdhci_cqe_disable,
2120 	.post_disable	= rk35xx_sdhci_cqe_post_disable,
2121 	.dumpregs	= dwcmshc_cqhci_dumpregs,
2122 	.set_tran_desc	= dwcmshc_set_tran_desc,
2123 };
2124 
2125 static const struct rockchip_pltfm_data sdhci_dwcmshc_rk3568_pdata = {
2126 	.dwcmshc_pdata = {
2127 		.pdata = {
2128 			.ops = &sdhci_dwcmshc_rk35xx_ops,
2129 			.quirks = SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN |
2130 				  SDHCI_QUIRK_BROKEN_TIMEOUT_VAL,
2131 			.quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN |
2132 				   SDHCI_QUIRK2_CLOCK_DIV_ZERO_BROKEN,
2133 		},
2134 		.cqhci_host_ops = &rk35xx_cqhci_ops,
2135 		.init = dwcmshc_rk35xx_init,
2136 		.postinit = dwcmshc_rk35xx_postinit,
2137 	},
2138 	.revision = 0,
2139 };
2140 
2141 static const struct rockchip_pltfm_data sdhci_dwcmshc_rk3576_pdata = {
2142 	.dwcmshc_pdata = {
2143 		.pdata = {
2144 			.ops = &sdhci_dwcmshc_rk35xx_ops,
2145 			.quirks = SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN |
2146 				  SDHCI_QUIRK_BROKEN_TIMEOUT_VAL,
2147 			.quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN |
2148 				   SDHCI_QUIRK2_CLOCK_DIV_ZERO_BROKEN,
2149 		},
2150 		.cqhci_host_ops = &rk35xx_cqhci_ops,
2151 		.init = dwcmshc_rk35xx_init,
2152 		.postinit = dwcmshc_rk3576_postinit,
2153 	},
2154 	.revision = 1,
2155 };
2156 
2157 static const struct rockchip_pltfm_data sdhci_dwcmshc_rk3588_pdata = {
2158 	.dwcmshc_pdata = {
2159 		.pdata = {
2160 			.ops = &sdhci_dwcmshc_rk35xx_ops,
2161 			.quirks = SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN |
2162 				  SDHCI_QUIRK_BROKEN_TIMEOUT_VAL,
2163 			.quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN |
2164 				   SDHCI_QUIRK2_CLOCK_DIV_ZERO_BROKEN,
2165 		},
2166 		.cqhci_host_ops = &rk35xx_cqhci_ops,
2167 		.init = dwcmshc_rk35xx_init,
2168 		.postinit = dwcmshc_rk35xx_postinit,
2169 	},
2170 	.revision = 1,
2171 };
2172 
2173 static const struct dwcmshc_pltfm_data sdhci_dwcmshc_th1520_pdata = {
2174 	.pdata = {
2175 		.ops = &sdhci_dwcmshc_th1520_ops,
2176 		.quirks = SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN,
2177 		.quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN,
2178 	},
2179 	.init = th1520_init,
2180 };
2181 
2182 static const struct dwcmshc_pltfm_data sdhci_dwcmshc_cv18xx_pdata = {
2183 	.pdata = {
2184 		.ops = &sdhci_dwcmshc_cv18xx_ops,
2185 		.quirks = SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN,
2186 		.quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN,
2187 	},
2188 };
2189 
2190 static const struct dwcmshc_pltfm_data sdhci_dwcmshc_sg2042_pdata = {
2191 	.pdata = {
2192 		.ops = &sdhci_dwcmshc_sg2042_ops,
2193 		.quirks = SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN,
2194 		.quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN,
2195 	},
2196 	.init = sg2042_init,
2197 };
2198 
2199 static const struct dwcmshc_pltfm_data sdhci_dwcmshc_eic7700_pdata = {
2200 	.pdata = {
2201 		.ops = &sdhci_dwcmshc_eic7700_ops,
2202 		.quirks = SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN |
2203 			  SDHCI_QUIRK_BROKEN_TIMEOUT_VAL,
2204 		.quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN |
2205 			   SDHCI_QUIRK2_CLOCK_DIV_ZERO_BROKEN,
2206 	},
2207 	.init = eic7700_init,
2208 };
2209 
2210 static const struct k230_pltfm_data k230_emmc_data = {
2211 	.dwcmshc_pdata = {
2212 		.pdata = {
2213 			.ops = &sdhci_dwcmshc_k230_ops,
2214 			.quirks = SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN |
2215 				  SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12,
2216 		},
2217 		.init = dwcmshc_k230_init,
2218 	},
2219 	.is_emmc = true,
2220 	.ctrl_reg = SD0_CTRL,
2221 	.vol_stable_bit = SD0_HOST_REG_VOL_STABLE,
2222 	.write_prot_bit = SD0_CARD_WRITE_PROT,
2223 };
2224 
2225 static const struct k230_pltfm_data k230_sdio_data = {
2226 	.dwcmshc_pdata = {
2227 		.pdata = {
2228 			.ops = &sdhci_dwcmshc_k230_ops,
2229 			.quirks = SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN |
2230 				  SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12,
2231 		},
2232 		.init = dwcmshc_k230_init,
2233 	},
2234 	.is_emmc = false,
2235 	.ctrl_reg = SD1_CTRL,
2236 	.vol_stable_bit = SD1_HOST_REG_VOL_STABLE,
2237 	.write_prot_bit = SD1_CARD_WRITE_PROT,
2238 };
2239 
2240 static const struct sdhci_ops sdhci_dwcmshc_hpe_ops = {
2241 	.set_clock		= dwcmshc_hpe_set_clock,
2242 	.set_bus_width		= sdhci_set_bus_width,
2243 	.set_uhs_signaling	= dwcmshc_hpe_set_uhs_signaling,
2244 	.get_max_clock		= dwcmshc_get_max_clock,
2245 	.reset			= dwcmshc_hpe_reset,
2246 	.adma_write_desc	= dwcmshc_adma_write_desc,
2247 	.irq			= dwcmshc_cqe_irq_handler,
2248 };
2249 
2250 static const struct dwcmshc_pltfm_data sdhci_dwcmshc_hpe_gsc_pdata = {
2251 	.pdata = {
2252 		.ops = &sdhci_dwcmshc_hpe_ops,
2253 		.quirks = SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN,
2254 		.quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN,
2255 	},
2256 	.init = dwcmshc_hpe_gsc_init,
2257 };
2258 
2259 static const struct cqhci_host_ops dwcmshc_cqhci_ops = {
2260 	.enable		= dwcmshc_sdhci_cqe_enable,
2261 	.disable	= sdhci_cqe_disable,
2262 	.dumpregs	= dwcmshc_cqhci_dumpregs,
2263 	.set_tran_desc	= dwcmshc_set_tran_desc,
2264 };
2265 
2266 static void dwcmshc_cqhci_init(struct sdhci_host *host, struct platform_device *pdev,
2267 			       const struct dwcmshc_pltfm_data *pltfm_data)
2268 {
2269 	struct cqhci_host *cq_host;
2270 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2271 	struct dwcmshc_priv *priv = sdhci_pltfm_priv(pltfm_host);
2272 	bool dma64 = false;
2273 	u16 clk;
2274 	int err;
2275 
2276 	host->mmc->caps2 |= MMC_CAP2_CQE | MMC_CAP2_CQE_DCMD;
2277 	cq_host = devm_kzalloc(&pdev->dev, sizeof(*cq_host), GFP_KERNEL);
2278 	if (!cq_host) {
2279 		dev_err(mmc_dev(host->mmc), "Unable to setup CQE: not enough memory\n");
2280 		goto dsbl_cqe_caps;
2281 	}
2282 
2283 	/*
2284 	 * For dwcmshc host controller we have to enable internal clock
2285 	 * before access to some registers from Vendor Specific Area 2.
2286 	 */
2287 	clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
2288 	clk |= SDHCI_CLOCK_INT_EN;
2289 	sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
2290 	clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
2291 	if (!(clk & SDHCI_CLOCK_INT_EN)) {
2292 		dev_err(mmc_dev(host->mmc), "Unable to setup CQE: internal clock enable error\n");
2293 		goto free_cq_host;
2294 	}
2295 
2296 	cq_host->mmio = host->ioaddr + priv->vendor_specific_area2;
2297 	if (pltfm_data->cqhci_host_ops)
2298 		cq_host->ops = pltfm_data->cqhci_host_ops;
2299 	else
2300 		cq_host->ops = &dwcmshc_cqhci_ops;
2301 
2302 	/* Enable using of 128-bit task descriptors */
2303 	dma64 = host->flags & SDHCI_USE_64_BIT_DMA;
2304 	if (dma64) {
2305 		dev_dbg(mmc_dev(host->mmc), "128-bit task descriptors\n");
2306 		cq_host->caps |= CQHCI_TASK_DESC_SZ_128;
2307 	}
2308 	err = cqhci_init(cq_host, host->mmc, dma64);
2309 	if (err) {
2310 		dev_err(mmc_dev(host->mmc), "Unable to setup CQE: error %d\n", err);
2311 		goto int_clock_disable;
2312 	}
2313 
2314 	dev_dbg(mmc_dev(host->mmc), "CQE init done\n");
2315 
2316 	return;
2317 
2318 int_clock_disable:
2319 	clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
2320 	clk &= ~SDHCI_CLOCK_INT_EN;
2321 	sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
2322 
2323 free_cq_host:
2324 	devm_kfree(&pdev->dev, cq_host);
2325 
2326 dsbl_cqe_caps:
2327 	host->mmc->caps2 &= ~(MMC_CAP2_CQE | MMC_CAP2_CQE_DCMD);
2328 }
2329 
2330 static const struct of_device_id sdhci_dwcmshc_dt_ids[] = {
2331 	{
2332 		.compatible = "canaan,k230-emmc",
2333 		.data = &k230_emmc_data.dwcmshc_pdata,
2334 	},
2335 	{
2336 		.compatible = "canaan,k230-sdio",
2337 		.data = &k230_sdio_data.dwcmshc_pdata,
2338 	},
2339 	{
2340 		.compatible = "rockchip,rk3588-dwcmshc",
2341 		.data = &sdhci_dwcmshc_rk3588_pdata,
2342 	},
2343 	{
2344 		.compatible = "rockchip,rk3576-dwcmshc",
2345 		.data = &sdhci_dwcmshc_rk3576_pdata,
2346 	},
2347 	{
2348 		.compatible = "rockchip,rk3568-dwcmshc",
2349 		.data = &sdhci_dwcmshc_rk3568_pdata,
2350 	},
2351 	{
2352 		.compatible = "snps,dwcmshc-sdhci",
2353 		.data = &sdhci_dwcmshc_pdata,
2354 	},
2355 	{
2356 		.compatible = "sophgo,cv1800b-dwcmshc",
2357 		.data = &sdhci_dwcmshc_cv18xx_pdata,
2358 	},
2359 	{
2360 		.compatible = "sophgo,sg2002-dwcmshc",
2361 		.data = &sdhci_dwcmshc_cv18xx_pdata,
2362 	},
2363 	{
2364 		.compatible = "thead,th1520-dwcmshc",
2365 		.data = &sdhci_dwcmshc_th1520_pdata,
2366 	},
2367 	{
2368 		.compatible = "sophgo,sg2042-dwcmshc",
2369 		.data = &sdhci_dwcmshc_sg2042_pdata,
2370 	},
2371 	{
2372 		.compatible = "eswin,eic7700-dwcmshc",
2373 		.data = &sdhci_dwcmshc_eic7700_pdata,
2374 	},
2375 	{
2376 		.compatible = "hpe,gsc-dwcmshc",
2377 		.data = &sdhci_dwcmshc_hpe_gsc_pdata,
2378 	},
2379 	{},
2380 };
2381 MODULE_DEVICE_TABLE(of, sdhci_dwcmshc_dt_ids);
2382 
2383 #ifdef CONFIG_ACPI
2384 static const struct acpi_device_id sdhci_dwcmshc_acpi_ids[] = {
2385 	{
2386 		.id = "MLNXBF30",
2387 		.driver_data = (kernel_ulong_t)&sdhci_dwcmshc_bf3_pdata,
2388 	},
2389 	{}
2390 };
2391 MODULE_DEVICE_TABLE(acpi, sdhci_dwcmshc_acpi_ids);
2392 #endif
2393 
2394 static int dwcmshc_probe(struct platform_device *pdev)
2395 {
2396 	struct device *dev = &pdev->dev;
2397 	struct sdhci_pltfm_host *pltfm_host;
2398 	struct sdhci_host *host;
2399 	struct dwcmshc_priv *priv;
2400 	const struct dwcmshc_pltfm_data *pltfm_data;
2401 	int err;
2402 	u32 extra, caps;
2403 
2404 	pltfm_data = device_get_match_data(&pdev->dev);
2405 	if (!pltfm_data)
2406 		return dev_err_probe(&pdev->dev, -ENODEV, "No device match data found\n");
2407 
2408 	host = sdhci_pltfm_init(pdev, &pltfm_data->pdata,
2409 				sizeof(struct dwcmshc_priv));
2410 	if (IS_ERR(host))
2411 		return PTR_ERR(host);
2412 
2413 	/*
2414 	 * extra adma table cnt for cross 128M boundary handling.
2415 	 */
2416 	extra = DIV_ROUND_UP_ULL(dma_get_required_mask(dev), SZ_128M);
2417 	if (extra > SDHCI_MAX_SEGS)
2418 		extra = SDHCI_MAX_SEGS;
2419 	host->adma_table_cnt += extra;
2420 
2421 	pltfm_host = sdhci_priv(host);
2422 	priv = sdhci_pltfm_priv(pltfm_host);
2423 	priv->dwcmshc_pdata = pltfm_data;
2424 
2425 	if (dev->of_node) {
2426 		pltfm_host->clk = devm_clk_get(dev, "core");
2427 		if (IS_ERR(pltfm_host->clk))
2428 			return dev_err_probe(dev, PTR_ERR(pltfm_host->clk),
2429 					     "failed to get core clk\n");
2430 
2431 		err = clk_prepare_enable(pltfm_host->clk);
2432 		if (err)
2433 			return err;
2434 
2435 		priv->bus_clk = devm_clk_get(dev, "bus");
2436 		if (!IS_ERR(priv->bus_clk))
2437 			clk_prepare_enable(priv->bus_clk);
2438 	}
2439 
2440 	err = mmc_of_parse(host->mmc);
2441 	if (err)
2442 		goto err_clk;
2443 
2444 	sdhci_get_of_property(pdev);
2445 
2446 	priv->vendor_specific_area1 =
2447 		sdhci_readl(host, DWCMSHC_P_VENDOR_AREA1) & DWCMSHC_AREA1_MASK;
2448 
2449 	host->mmc_host_ops.request = dwcmshc_request;
2450 	host->mmc_host_ops.hs400_enhanced_strobe = dwcmshc_hs400_enhanced_strobe;
2451 	host->mmc_host_ops.execute_tuning = dwcmshc_execute_tuning;
2452 
2453 	if (pltfm_data->init) {
2454 		err = pltfm_data->init(&pdev->dev, host, priv);
2455 		if (err)
2456 			goto err_clk;
2457 	}
2458 
2459 #ifdef CONFIG_ACPI
2460 	if (pltfm_data == &sdhci_dwcmshc_bf3_pdata)
2461 		sdhci_enable_v4_mode(host);
2462 #endif
2463 
2464 	caps = sdhci_readl(host, SDHCI_CAPABILITIES);
2465 	if (caps & SDHCI_CAN_64BIT_V4)
2466 		sdhci_enable_v4_mode(host);
2467 
2468 	host->mmc->caps |= MMC_CAP_WAIT_WHILE_BUSY;
2469 
2470 	pm_runtime_get_noresume(dev);
2471 	pm_runtime_set_active(dev);
2472 	pm_runtime_enable(dev);
2473 
2474 	err = sdhci_setup_host(host);
2475 	if (err)
2476 		goto err_rpm;
2477 
2478 	/* Setup Command Queue Engine if enabled */
2479 	if (device_property_read_bool(&pdev->dev, "supports-cqe")) {
2480 		priv->vendor_specific_area2 =
2481 			sdhci_readw(host, DWCMSHC_P_VENDOR_AREA2);
2482 
2483 		dwcmshc_cqhci_init(host, pdev, pltfm_data);
2484 	}
2485 
2486 	if (pltfm_data->postinit)
2487 		pltfm_data->postinit(host, priv);
2488 
2489 	err = __sdhci_add_host(host);
2490 	if (err)
2491 		goto err_setup_host;
2492 
2493 	pm_runtime_put(dev);
2494 
2495 	return 0;
2496 
2497 err_setup_host:
2498 	sdhci_cleanup_host(host);
2499 err_rpm:
2500 	pm_runtime_disable(dev);
2501 	pm_runtime_put_noidle(dev);
2502 err_clk:
2503 	clk_disable_unprepare(pltfm_host->clk);
2504 	clk_disable_unprepare(priv->bus_clk);
2505 	clk_bulk_disable_unprepare(priv->num_other_clks, priv->other_clks);
2506 	return err;
2507 }
2508 
2509 static void dwcmshc_disable_card_clk(struct sdhci_host *host)
2510 {
2511 	u16 ctrl;
2512 
2513 	ctrl = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
2514 	if (ctrl & SDHCI_CLOCK_CARD_EN) {
2515 		ctrl &= ~SDHCI_CLOCK_CARD_EN;
2516 		sdhci_writew(host, ctrl, SDHCI_CLOCK_CONTROL);
2517 	}
2518 }
2519 
2520 static void dwcmshc_remove(struct platform_device *pdev)
2521 {
2522 	struct sdhci_host *host = platform_get_drvdata(pdev);
2523 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2524 	struct dwcmshc_priv *priv = sdhci_pltfm_priv(pltfm_host);
2525 
2526 	pm_runtime_get_sync(&pdev->dev);
2527 	pm_runtime_disable(&pdev->dev);
2528 	pm_runtime_put_noidle(&pdev->dev);
2529 
2530 	sdhci_remove_host(host, 0);
2531 
2532 	dwcmshc_disable_card_clk(host);
2533 
2534 	clk_disable_unprepare(pltfm_host->clk);
2535 	clk_disable_unprepare(priv->bus_clk);
2536 	clk_bulk_disable_unprepare(priv->num_other_clks, priv->other_clks);
2537 }
2538 
2539 static int dwcmshc_suspend(struct device *dev)
2540 {
2541 	struct sdhci_host *host = dev_get_drvdata(dev);
2542 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2543 	struct dwcmshc_priv *priv = sdhci_pltfm_priv(pltfm_host);
2544 	int ret;
2545 
2546 	pm_runtime_resume(dev);
2547 
2548 	if (host->mmc->caps2 & MMC_CAP2_CQE) {
2549 		ret = cqhci_suspend(host->mmc);
2550 		if (ret)
2551 			return ret;
2552 	}
2553 
2554 	ret = sdhci_suspend_host(host);
2555 	if (ret)
2556 		return ret;
2557 
2558 	clk_disable_unprepare(pltfm_host->clk);
2559 	clk_disable_unprepare(priv->bus_clk);
2560 
2561 	clk_bulk_disable_unprepare(priv->num_other_clks, priv->other_clks);
2562 
2563 	return ret;
2564 }
2565 
2566 static int dwcmshc_resume(struct device *dev)
2567 {
2568 	struct sdhci_host *host = dev_get_drvdata(dev);
2569 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2570 	struct dwcmshc_priv *priv = sdhci_pltfm_priv(pltfm_host);
2571 	int ret;
2572 
2573 	ret = clk_prepare_enable(pltfm_host->clk);
2574 	if (ret)
2575 		return ret;
2576 
2577 	if (!IS_ERR(priv->bus_clk)) {
2578 		ret = clk_prepare_enable(priv->bus_clk);
2579 		if (ret)
2580 			goto disable_clk;
2581 	}
2582 
2583 	ret = clk_bulk_prepare_enable(priv->num_other_clks, priv->other_clks);
2584 	if (ret)
2585 		goto disable_bus_clk;
2586 
2587 	ret = sdhci_resume_host(host);
2588 	if (ret)
2589 		goto disable_other_clks;
2590 
2591 	if (host->mmc->caps2 & MMC_CAP2_CQE) {
2592 		ret = cqhci_resume(host->mmc);
2593 		if (ret)
2594 			goto disable_other_clks;
2595 	}
2596 
2597 	return 0;
2598 
2599 disable_other_clks:
2600 	clk_bulk_disable_unprepare(priv->num_other_clks, priv->other_clks);
2601 disable_bus_clk:
2602 	clk_disable_unprepare(priv->bus_clk);
2603 disable_clk:
2604 	clk_disable_unprepare(pltfm_host->clk);
2605 	return ret;
2606 }
2607 
2608 static int dwcmshc_runtime_suspend(struct device *dev)
2609 {
2610 	struct sdhci_host *host = dev_get_drvdata(dev);
2611 
2612 	dwcmshc_disable_card_clk(host);
2613 
2614 	return 0;
2615 }
2616 
2617 static int dwcmshc_runtime_resume(struct device *dev)
2618 {
2619 	struct sdhci_host *host = dev_get_drvdata(dev);
2620 
2621 	dwcmshc_enable_card_clk(host);
2622 
2623 	return 0;
2624 }
2625 
2626 static const struct dev_pm_ops dwcmshc_pmops = {
2627 	SYSTEM_SLEEP_PM_OPS(dwcmshc_suspend, dwcmshc_resume)
2628 	RUNTIME_PM_OPS(dwcmshc_runtime_suspend, dwcmshc_runtime_resume, NULL)
2629 };
2630 
2631 static struct platform_driver sdhci_dwcmshc_driver = {
2632 	.driver	= {
2633 		.name	= "sdhci-dwcmshc",
2634 		.probe_type = PROBE_PREFER_ASYNCHRONOUS,
2635 		.of_match_table = sdhci_dwcmshc_dt_ids,
2636 		.acpi_match_table = ACPI_PTR(sdhci_dwcmshc_acpi_ids),
2637 		.pm = pm_ptr(&dwcmshc_pmops),
2638 	},
2639 	.probe	= dwcmshc_probe,
2640 	.remove = dwcmshc_remove,
2641 };
2642 module_platform_driver(sdhci_dwcmshc_driver);
2643 
2644 MODULE_DESCRIPTION("SDHCI platform driver for Synopsys DWC MSHC");
2645 MODULE_AUTHOR("Jisheng Zhang <jszhang@kernel.org>");
2646 MODULE_LICENSE("GPL v2");
2647