xref: /linux/drivers/mmc/host/sdhci-of-dwcmshc.c (revision 7255fcc80d4b525cc10cfaaf7f485830d4ed2000)
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/bitfield.h>
12 #include <linux/clk.h>
13 #include <linux/dma-mapping.h>
14 #include <linux/iopoll.h>
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/of.h>
18 #include <linux/platform_device.h>
19 #include <linux/pm_runtime.h>
20 #include <linux/reset.h>
21 #include <linux/sizes.h>
22 
23 #include "sdhci-pltfm.h"
24 
25 #define SDHCI_DWCMSHC_ARG2_STUFF	GENMASK(31, 16)
26 
27 /* DWCMSHC specific Mode Select value */
28 #define DWCMSHC_CTRL_HS400		0x7
29 
30 /* DWC IP vendor area 1 pointer */
31 #define DWCMSHC_P_VENDOR_AREA1		0xe8
32 #define DWCMSHC_AREA1_MASK		GENMASK(11, 0)
33 /* Offset inside the  vendor area 1 */
34 #define DWCMSHC_HOST_CTRL3		0x8
35 #define DWCMSHC_EMMC_CONTROL		0x2c
36 #define DWCMSHC_CARD_IS_EMMC		BIT(0)
37 #define DWCMSHC_ENHANCED_STROBE		BIT(8)
38 #define DWCMSHC_EMMC_ATCTRL		0x40
39 /* Tuning and auto-tuning fields in AT_CTRL_R control register */
40 #define AT_CTRL_AT_EN			BIT(0) /* autotuning is enabled */
41 #define AT_CTRL_CI_SEL			BIT(1) /* interval to drive center phase select */
42 #define AT_CTRL_SWIN_TH_EN		BIT(2) /* sampling window threshold enable */
43 #define AT_CTRL_RPT_TUNE_ERR		BIT(3) /* enable reporting framing errors */
44 #define AT_CTRL_SW_TUNE_EN		BIT(4) /* enable software managed tuning */
45 #define AT_CTRL_WIN_EDGE_SEL_MASK	GENMASK(11, 8) /* bits [11:8] */
46 #define AT_CTRL_WIN_EDGE_SEL		0xf /* sampling window edge select */
47 #define AT_CTRL_TUNE_CLK_STOP_EN	BIT(16) /* clocks stopped during phase code change */
48 #define AT_CTRL_PRE_CHANGE_DLY_MASK	GENMASK(18, 17) /* bits [18:17] */
49 #define AT_CTRL_PRE_CHANGE_DLY		0x1  /* 2-cycle latency */
50 #define AT_CTRL_POST_CHANGE_DLY_MASK	GENMASK(20, 19) /* bits [20:19] */
51 #define AT_CTRL_POST_CHANGE_DLY		0x3  /* 4-cycle latency */
52 #define AT_CTRL_SWIN_TH_VAL_MASK	GENMASK(31, 24) /* bits [31:24] */
53 #define AT_CTRL_SWIN_TH_VAL		0x9  /* sampling window threshold */
54 
55 /* Sophgo CV18XX specific Registers */
56 #define CV18XX_SDHCI_MSHC_CTRL			0x00
57 #define  CV18XX_EMMC_FUNC_EN			BIT(0)
58 #define  CV18XX_LATANCY_1T			BIT(1)
59 #define CV18XX_SDHCI_PHY_TX_RX_DLY		0x40
60 #define  CV18XX_PHY_TX_DLY_MSK			GENMASK(6, 0)
61 #define  CV18XX_PHY_TX_SRC_MSK			GENMASK(9, 8)
62 #define  CV18XX_PHY_TX_SRC_INVERT_CLK_TX	0x1
63 #define  CV18XX_PHY_RX_DLY_MSK			GENMASK(22, 16)
64 #define  CV18XX_PHY_RX_SRC_MSK			GENMASK(25, 24)
65 #define  CV18XX_PHY_RX_SRC_INVERT_RX_CLK	0x1
66 #define CV18XX_SDHCI_PHY_CONFIG			0x4c
67 #define  CV18XX_PHY_TX_BPS			BIT(0)
68 
69 /* Rockchip specific Registers */
70 #define DWCMSHC_EMMC_DLL_CTRL		0x800
71 #define DWCMSHC_EMMC_DLL_RXCLK		0x804
72 #define DWCMSHC_EMMC_DLL_TXCLK		0x808
73 #define DWCMSHC_EMMC_DLL_STRBIN		0x80c
74 #define DECMSHC_EMMC_DLL_CMDOUT		0x810
75 #define DWCMSHC_EMMC_DLL_STATUS0	0x840
76 #define DWCMSHC_EMMC_DLL_START		BIT(0)
77 #define DWCMSHC_EMMC_DLL_LOCKED		BIT(8)
78 #define DWCMSHC_EMMC_DLL_TIMEOUT	BIT(9)
79 #define DWCMSHC_EMMC_DLL_RXCLK_SRCSEL	29
80 #define DWCMSHC_EMMC_DLL_START_POINT	16
81 #define DWCMSHC_EMMC_DLL_INC		8
82 #define DWCMSHC_EMMC_DLL_BYPASS		BIT(24)
83 #define DWCMSHC_EMMC_DLL_DLYENA		BIT(27)
84 #define DLL_TXCLK_TAPNUM_DEFAULT	0x10
85 #define DLL_TXCLK_TAPNUM_90_DEGREES	0xA
86 #define DLL_TXCLK_TAPNUM_FROM_SW	BIT(24)
87 #define DLL_STRBIN_TAPNUM_DEFAULT	0x8
88 #define DLL_STRBIN_TAPNUM_FROM_SW	BIT(24)
89 #define DLL_STRBIN_DELAY_NUM_SEL	BIT(26)
90 #define DLL_STRBIN_DELAY_NUM_OFFSET	16
91 #define DLL_STRBIN_DELAY_NUM_DEFAULT	0x16
92 #define DLL_RXCLK_NO_INVERTER		1
93 #define DLL_RXCLK_INVERTER		0
94 #define DLL_CMDOUT_TAPNUM_90_DEGREES	0x8
95 #define DLL_RXCLK_ORI_GATE		BIT(31)
96 #define DLL_CMDOUT_TAPNUM_FROM_SW	BIT(24)
97 #define DLL_CMDOUT_SRC_CLK_NEG		BIT(28)
98 #define DLL_CMDOUT_EN_SRC_CLK_NEG	BIT(29)
99 
100 #define DLL_LOCK_WO_TMOUT(x) \
101 	((((x) & DWCMSHC_EMMC_DLL_LOCKED) == DWCMSHC_EMMC_DLL_LOCKED) && \
102 	(((x) & DWCMSHC_EMMC_DLL_TIMEOUT) == 0))
103 #define RK35xx_MAX_CLKS 3
104 
105 /* PHY register area pointer */
106 #define DWC_MSHC_PTR_PHY_R	0x300
107 
108 /* PHY general configuration */
109 #define PHY_CNFG_R		(DWC_MSHC_PTR_PHY_R + 0x00)
110 #define PHY_CNFG_RSTN_DEASSERT	0x1  /* Deassert PHY reset */
111 #define PHY_CNFG_PAD_SP_MASK	GENMASK(19, 16) /* bits [19:16] */
112 #define PHY_CNFG_PAD_SP		0x0c /* PMOS TX drive strength */
113 #define PHY_CNFG_PAD_SN_MASK	GENMASK(23, 20) /* bits [23:20] */
114 #define PHY_CNFG_PAD_SN		0x0c /* NMOS TX drive strength */
115 
116 /* PHY command/response pad settings */
117 #define PHY_CMDPAD_CNFG_R	(DWC_MSHC_PTR_PHY_R + 0x04)
118 
119 /* PHY data pad settings */
120 #define PHY_DATAPAD_CNFG_R	(DWC_MSHC_PTR_PHY_R + 0x06)
121 
122 /* PHY clock pad settings */
123 #define PHY_CLKPAD_CNFG_R	(DWC_MSHC_PTR_PHY_R + 0x08)
124 
125 /* PHY strobe pad settings */
126 #define PHY_STBPAD_CNFG_R	(DWC_MSHC_PTR_PHY_R + 0x0a)
127 
128 /* PHY reset pad settings */
129 #define PHY_RSTNPAD_CNFG_R	(DWC_MSHC_PTR_PHY_R + 0x0c)
130 
131 /* Bitfields are common for all pad settings */
132 #define PHY_PAD_RXSEL_1V8		0x1 /* Receiver type select for 1.8V */
133 #define PHY_PAD_RXSEL_3V3		0x2 /* Receiver type select for 3.3V */
134 
135 #define PHY_PAD_WEAKPULL_MASK		GENMASK(4, 3) /* bits [4:3] */
136 #define PHY_PAD_WEAKPULL_PULLUP		0x1 /* Weak pull up enabled */
137 #define PHY_PAD_WEAKPULL_PULLDOWN	0x2 /* Weak pull down enabled */
138 
139 #define PHY_PAD_TXSLEW_CTRL_P_MASK	GENMASK(8, 5) /* bits [8:5] */
140 #define PHY_PAD_TXSLEW_CTRL_P		0x3 /* Slew control for P-Type pad TX */
141 #define PHY_PAD_TXSLEW_CTRL_N_MASK	GENMASK(12, 9) /* bits [12:9] */
142 #define PHY_PAD_TXSLEW_CTRL_N		0x3 /* Slew control for N-Type pad TX */
143 
144 /* PHY CLK delay line settings */
145 #define PHY_SDCLKDL_CNFG_R		(DWC_MSHC_PTR_PHY_R + 0x1d)
146 #define PHY_SDCLKDL_CNFG_UPDATE	BIT(4) /* set before writing to SDCLKDL_DC */
147 
148 /* PHY CLK delay line delay code */
149 #define PHY_SDCLKDL_DC_R		(DWC_MSHC_PTR_PHY_R + 0x1e)
150 #define PHY_SDCLKDL_DC_INITIAL		0x40 /* initial delay code */
151 #define PHY_SDCLKDL_DC_DEFAULT		0x32 /* default delay code */
152 #define PHY_SDCLKDL_DC_HS400		0x18 /* delay code for HS400 mode */
153 
154 /* PHY drift_cclk_rx delay line configuration setting */
155 #define PHY_ATDL_CNFG_R			(DWC_MSHC_PTR_PHY_R + 0x21)
156 #define PHY_ATDL_CNFG_INPSEL_MASK	GENMASK(3, 2) /* bits [3:2] */
157 #define PHY_ATDL_CNFG_INPSEL		0x3 /* delay line input source */
158 
159 /* PHY DLL control settings */
160 #define PHY_DLL_CTRL_R			(DWC_MSHC_PTR_PHY_R + 0x24)
161 #define PHY_DLL_CTRL_DISABLE		0x0 /* PHY DLL is enabled */
162 #define PHY_DLL_CTRL_ENABLE		0x1 /* PHY DLL is disabled */
163 
164 /* PHY DLL  configuration register 1 */
165 #define PHY_DLL_CNFG1_R			(DWC_MSHC_PTR_PHY_R + 0x25)
166 #define PHY_DLL_CNFG1_SLVDLY_MASK	GENMASK(5, 4) /* bits [5:4] */
167 #define PHY_DLL_CNFG1_SLVDLY		0x2 /* DLL slave update delay input */
168 #define PHY_DLL_CNFG1_WAITCYCLE		0x5 /* DLL wait cycle input */
169 
170 /* PHY DLL configuration register 2 */
171 #define PHY_DLL_CNFG2_R			(DWC_MSHC_PTR_PHY_R + 0x26)
172 #define PHY_DLL_CNFG2_JUMPSTEP		0xa /* DLL jump step input */
173 
174 /* PHY DLL master and slave delay line configuration settings */
175 #define PHY_DLLDL_CNFG_R		(DWC_MSHC_PTR_PHY_R + 0x28)
176 #define PHY_DLLDL_CNFG_SLV_INPSEL_MASK	GENMASK(6, 5) /* bits [6:5] */
177 #define PHY_DLLDL_CNFG_SLV_INPSEL	0x3 /* clock source select for slave DL */
178 
179 #define FLAG_IO_FIXED_1V8	BIT(0)
180 
181 #define BOUNDARY_OK(addr, len) \
182 	((addr | (SZ_128M - 1)) == ((addr + len - 1) | (SZ_128M - 1)))
183 
184 enum dwcmshc_rk_type {
185 	DWCMSHC_RK3568,
186 	DWCMSHC_RK3588,
187 };
188 
189 struct rk35xx_priv {
190 	/* Rockchip specified optional clocks */
191 	struct clk_bulk_data rockchip_clks[RK35xx_MAX_CLKS];
192 	struct reset_control *reset;
193 	enum dwcmshc_rk_type devtype;
194 	u8 txclk_tapnum;
195 };
196 
197 struct dwcmshc_priv {
198 	struct clk	*bus_clk;
199 	int vendor_specific_area1; /* P_VENDOR_SPECIFIC_AREA reg */
200 	void *priv; /* pointer to SoC private stuff */
201 	u16 delay_line;
202 	u16 flags;
203 };
204 
205 /*
206  * If DMA addr spans 128MB boundary, we split the DMA transfer into two
207  * so that each DMA transfer doesn't exceed the boundary.
208  */
209 static void dwcmshc_adma_write_desc(struct sdhci_host *host, void **desc,
210 				    dma_addr_t addr, int len, unsigned int cmd)
211 {
212 	int tmplen, offset;
213 
214 	if (likely(!len || BOUNDARY_OK(addr, len))) {
215 		sdhci_adma_write_desc(host, desc, addr, len, cmd);
216 		return;
217 	}
218 
219 	offset = addr & (SZ_128M - 1);
220 	tmplen = SZ_128M - offset;
221 	sdhci_adma_write_desc(host, desc, addr, tmplen, cmd);
222 
223 	addr += tmplen;
224 	len -= tmplen;
225 	sdhci_adma_write_desc(host, desc, addr, len, cmd);
226 }
227 
228 static unsigned int dwcmshc_get_max_clock(struct sdhci_host *host)
229 {
230 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
231 
232 	if (pltfm_host->clk)
233 		return sdhci_pltfm_clk_get_max_clock(host);
234 	else
235 		return pltfm_host->clock;
236 }
237 
238 static unsigned int rk35xx_get_max_clock(struct sdhci_host *host)
239 {
240 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
241 
242 	return clk_round_rate(pltfm_host->clk, ULONG_MAX);
243 }
244 
245 static void dwcmshc_check_auto_cmd23(struct mmc_host *mmc,
246 				     struct mmc_request *mrq)
247 {
248 	struct sdhci_host *host = mmc_priv(mmc);
249 
250 	/*
251 	 * No matter V4 is enabled or not, ARGUMENT2 register is 32-bit
252 	 * block count register which doesn't support stuff bits of
253 	 * CMD23 argument on dwcmsch host controller.
254 	 */
255 	if (mrq->sbc && (mrq->sbc->arg & SDHCI_DWCMSHC_ARG2_STUFF))
256 		host->flags &= ~SDHCI_AUTO_CMD23;
257 	else
258 		host->flags |= SDHCI_AUTO_CMD23;
259 }
260 
261 static void dwcmshc_request(struct mmc_host *mmc, struct mmc_request *mrq)
262 {
263 	dwcmshc_check_auto_cmd23(mmc, mrq);
264 
265 	sdhci_request(mmc, mrq);
266 }
267 
268 static void dwcmshc_phy_1_8v_init(struct sdhci_host *host)
269 {
270 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
271 	struct dwcmshc_priv *priv = sdhci_pltfm_priv(pltfm_host);
272 	u32 val;
273 
274 	/* deassert phy reset & set tx drive strength */
275 	val = PHY_CNFG_RSTN_DEASSERT;
276 	val |= FIELD_PREP(PHY_CNFG_PAD_SP_MASK, PHY_CNFG_PAD_SP);
277 	val |= FIELD_PREP(PHY_CNFG_PAD_SN_MASK, PHY_CNFG_PAD_SN);
278 	sdhci_writel(host, val, PHY_CNFG_R);
279 
280 	/* disable delay line */
281 	sdhci_writeb(host, PHY_SDCLKDL_CNFG_UPDATE, PHY_SDCLKDL_CNFG_R);
282 
283 	/* set delay line */
284 	sdhci_writeb(host, priv->delay_line, PHY_SDCLKDL_DC_R);
285 	sdhci_writeb(host, PHY_DLL_CNFG2_JUMPSTEP, PHY_DLL_CNFG2_R);
286 
287 	/* enable delay lane */
288 	val = sdhci_readb(host, PHY_SDCLKDL_CNFG_R);
289 	val &= ~(PHY_SDCLKDL_CNFG_UPDATE);
290 	sdhci_writeb(host, val, PHY_SDCLKDL_CNFG_R);
291 
292 	/* configure phy pads */
293 	val = PHY_PAD_RXSEL_1V8;
294 	val |= FIELD_PREP(PHY_PAD_WEAKPULL_MASK, PHY_PAD_WEAKPULL_PULLUP);
295 	val |= FIELD_PREP(PHY_PAD_TXSLEW_CTRL_P_MASK, PHY_PAD_TXSLEW_CTRL_P);
296 	val |= FIELD_PREP(PHY_PAD_TXSLEW_CTRL_N_MASK, PHY_PAD_TXSLEW_CTRL_N);
297 	sdhci_writew(host, val, PHY_CMDPAD_CNFG_R);
298 	sdhci_writew(host, val, PHY_DATAPAD_CNFG_R);
299 	sdhci_writew(host, val, PHY_RSTNPAD_CNFG_R);
300 
301 	val = FIELD_PREP(PHY_PAD_TXSLEW_CTRL_P_MASK, PHY_PAD_TXSLEW_CTRL_P);
302 	val |= FIELD_PREP(PHY_PAD_TXSLEW_CTRL_N_MASK, PHY_PAD_TXSLEW_CTRL_N);
303 	sdhci_writew(host, val, PHY_CLKPAD_CNFG_R);
304 
305 	val = PHY_PAD_RXSEL_1V8;
306 	val |= FIELD_PREP(PHY_PAD_WEAKPULL_MASK, PHY_PAD_WEAKPULL_PULLDOWN);
307 	val |= FIELD_PREP(PHY_PAD_TXSLEW_CTRL_P_MASK, PHY_PAD_TXSLEW_CTRL_P);
308 	val |= FIELD_PREP(PHY_PAD_TXSLEW_CTRL_N_MASK, PHY_PAD_TXSLEW_CTRL_N);
309 	sdhci_writew(host, val, PHY_STBPAD_CNFG_R);
310 
311 	/* enable data strobe mode */
312 	sdhci_writeb(host, FIELD_PREP(PHY_DLLDL_CNFG_SLV_INPSEL_MASK, PHY_DLLDL_CNFG_SLV_INPSEL),
313 		     PHY_DLLDL_CNFG_R);
314 
315 	/* enable phy dll */
316 	sdhci_writeb(host, PHY_DLL_CTRL_ENABLE, PHY_DLL_CTRL_R);
317 }
318 
319 static void dwcmshc_phy_3_3v_init(struct sdhci_host *host)
320 {
321 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
322 	struct dwcmshc_priv *priv = sdhci_pltfm_priv(pltfm_host);
323 	u32 val;
324 
325 	/* deassert phy reset & set tx drive strength */
326 	val = PHY_CNFG_RSTN_DEASSERT;
327 	val |= FIELD_PREP(PHY_CNFG_PAD_SP_MASK, PHY_CNFG_PAD_SP);
328 	val |= FIELD_PREP(PHY_CNFG_PAD_SN_MASK, PHY_CNFG_PAD_SN);
329 	sdhci_writel(host, val, PHY_CNFG_R);
330 
331 	/* disable delay line */
332 	sdhci_writeb(host, PHY_SDCLKDL_CNFG_UPDATE, PHY_SDCLKDL_CNFG_R);
333 
334 	/* set delay line */
335 	sdhci_writeb(host, priv->delay_line, PHY_SDCLKDL_DC_R);
336 	sdhci_writeb(host, PHY_DLL_CNFG2_JUMPSTEP, PHY_DLL_CNFG2_R);
337 
338 	/* enable delay lane */
339 	val = sdhci_readb(host, PHY_SDCLKDL_CNFG_R);
340 	val &= ~(PHY_SDCLKDL_CNFG_UPDATE);
341 	sdhci_writeb(host, val, PHY_SDCLKDL_CNFG_R);
342 
343 	/* configure phy pads */
344 	val = PHY_PAD_RXSEL_3V3;
345 	val |= FIELD_PREP(PHY_PAD_WEAKPULL_MASK, PHY_PAD_WEAKPULL_PULLUP);
346 	val |= FIELD_PREP(PHY_PAD_TXSLEW_CTRL_P_MASK, PHY_PAD_TXSLEW_CTRL_P);
347 	val |= FIELD_PREP(PHY_PAD_TXSLEW_CTRL_N_MASK, PHY_PAD_TXSLEW_CTRL_N);
348 	sdhci_writew(host, val, PHY_CMDPAD_CNFG_R);
349 	sdhci_writew(host, val, PHY_DATAPAD_CNFG_R);
350 	sdhci_writew(host, val, PHY_RSTNPAD_CNFG_R);
351 
352 	val = FIELD_PREP(PHY_PAD_TXSLEW_CTRL_P_MASK, PHY_PAD_TXSLEW_CTRL_P);
353 	val |= FIELD_PREP(PHY_PAD_TXSLEW_CTRL_N_MASK, PHY_PAD_TXSLEW_CTRL_N);
354 	sdhci_writew(host, val, PHY_CLKPAD_CNFG_R);
355 
356 	val = PHY_PAD_RXSEL_3V3;
357 	val |= FIELD_PREP(PHY_PAD_WEAKPULL_MASK, PHY_PAD_WEAKPULL_PULLDOWN);
358 	val |= FIELD_PREP(PHY_PAD_TXSLEW_CTRL_P_MASK, PHY_PAD_TXSLEW_CTRL_P);
359 	val |= FIELD_PREP(PHY_PAD_TXSLEW_CTRL_N_MASK, PHY_PAD_TXSLEW_CTRL_N);
360 	sdhci_writew(host, val, PHY_STBPAD_CNFG_R);
361 
362 	/* enable phy dll */
363 	sdhci_writeb(host, PHY_DLL_CTRL_ENABLE, PHY_DLL_CTRL_R);
364 }
365 
366 static void th1520_sdhci_set_phy(struct sdhci_host *host)
367 {
368 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
369 	struct dwcmshc_priv *priv = sdhci_pltfm_priv(pltfm_host);
370 	u32 emmc_caps = MMC_CAP2_NO_SD | MMC_CAP2_NO_SDIO;
371 	u16 emmc_ctrl;
372 
373 	/* Before power on, set PHY configs */
374 	if (priv->flags & FLAG_IO_FIXED_1V8)
375 		dwcmshc_phy_1_8v_init(host);
376 	else
377 		dwcmshc_phy_3_3v_init(host);
378 
379 	if ((host->mmc->caps2 & emmc_caps) == emmc_caps) {
380 		emmc_ctrl = sdhci_readw(host, priv->vendor_specific_area1 + DWCMSHC_EMMC_CONTROL);
381 		emmc_ctrl |= DWCMSHC_CARD_IS_EMMC;
382 		sdhci_writew(host, emmc_ctrl, priv->vendor_specific_area1 + DWCMSHC_EMMC_CONTROL);
383 	}
384 
385 	sdhci_writeb(host, FIELD_PREP(PHY_DLL_CNFG1_SLVDLY_MASK, PHY_DLL_CNFG1_SLVDLY) |
386 		     PHY_DLL_CNFG1_WAITCYCLE, PHY_DLL_CNFG1_R);
387 }
388 
389 static void dwcmshc_set_uhs_signaling(struct sdhci_host *host,
390 				      unsigned int timing)
391 {
392 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
393 	struct dwcmshc_priv *priv = sdhci_pltfm_priv(pltfm_host);
394 	u16 ctrl, ctrl_2;
395 
396 	ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
397 	/* Select Bus Speed Mode for host */
398 	ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
399 	if ((timing == MMC_TIMING_MMC_HS200) ||
400 	    (timing == MMC_TIMING_UHS_SDR104))
401 		ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
402 	else if (timing == MMC_TIMING_UHS_SDR12)
403 		ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
404 	else if ((timing == MMC_TIMING_UHS_SDR25) ||
405 		 (timing == MMC_TIMING_MMC_HS))
406 		ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
407 	else if (timing == MMC_TIMING_UHS_SDR50)
408 		ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
409 	else if ((timing == MMC_TIMING_UHS_DDR50) ||
410 		 (timing == MMC_TIMING_MMC_DDR52))
411 		ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
412 	else if (timing == MMC_TIMING_MMC_HS400) {
413 		/* set CARD_IS_EMMC bit to enable Data Strobe for HS400 */
414 		ctrl = sdhci_readw(host, priv->vendor_specific_area1 + DWCMSHC_EMMC_CONTROL);
415 		ctrl |= DWCMSHC_CARD_IS_EMMC;
416 		sdhci_writew(host, ctrl, priv->vendor_specific_area1 + DWCMSHC_EMMC_CONTROL);
417 
418 		ctrl_2 |= DWCMSHC_CTRL_HS400;
419 	}
420 
421 	if (priv->flags & FLAG_IO_FIXED_1V8)
422 		ctrl_2 |= SDHCI_CTRL_VDD_180;
423 	sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
424 }
425 
426 static void th1520_set_uhs_signaling(struct sdhci_host *host,
427 				     unsigned int timing)
428 {
429 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
430 	struct dwcmshc_priv *priv = sdhci_pltfm_priv(pltfm_host);
431 
432 	dwcmshc_set_uhs_signaling(host, timing);
433 	if (timing == MMC_TIMING_MMC_HS400)
434 		priv->delay_line = PHY_SDCLKDL_DC_HS400;
435 	else
436 		sdhci_writeb(host, 0, PHY_DLLDL_CNFG_R);
437 	th1520_sdhci_set_phy(host);
438 }
439 
440 static void dwcmshc_hs400_enhanced_strobe(struct mmc_host *mmc,
441 					  struct mmc_ios *ios)
442 {
443 	u32 vendor;
444 	struct sdhci_host *host = mmc_priv(mmc);
445 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
446 	struct dwcmshc_priv *priv = sdhci_pltfm_priv(pltfm_host);
447 	int reg = priv->vendor_specific_area1 + DWCMSHC_EMMC_CONTROL;
448 
449 	vendor = sdhci_readl(host, reg);
450 	if (ios->enhanced_strobe)
451 		vendor |= DWCMSHC_ENHANCED_STROBE;
452 	else
453 		vendor &= ~DWCMSHC_ENHANCED_STROBE;
454 
455 	sdhci_writel(host, vendor, reg);
456 }
457 
458 static void dwcmshc_rk3568_set_clock(struct sdhci_host *host, unsigned int clock)
459 {
460 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
461 	struct dwcmshc_priv *dwc_priv = sdhci_pltfm_priv(pltfm_host);
462 	struct rk35xx_priv *priv = dwc_priv->priv;
463 	u8 txclk_tapnum = DLL_TXCLK_TAPNUM_DEFAULT;
464 	u32 extra, reg;
465 	int err;
466 
467 	host->mmc->actual_clock = 0;
468 
469 	if (clock == 0) {
470 		/* Disable interface clock at initial state. */
471 		sdhci_set_clock(host, clock);
472 		return;
473 	}
474 
475 	/* Rockchip platform only support 375KHz for identify mode */
476 	if (clock <= 400000)
477 		clock = 375000;
478 
479 	err = clk_set_rate(pltfm_host->clk, clock);
480 	if (err)
481 		dev_err(mmc_dev(host->mmc), "fail to set clock %d", clock);
482 
483 	sdhci_set_clock(host, clock);
484 
485 	/* Disable cmd conflict check */
486 	reg = dwc_priv->vendor_specific_area1 + DWCMSHC_HOST_CTRL3;
487 	extra = sdhci_readl(host, reg);
488 	extra &= ~BIT(0);
489 	sdhci_writel(host, extra, reg);
490 
491 	if (clock <= 52000000) {
492 		/*
493 		 * Disable DLL and reset both of sample and drive clock.
494 		 * The bypass bit and start bit need to be set if DLL is not locked.
495 		 */
496 		sdhci_writel(host, DWCMSHC_EMMC_DLL_BYPASS | DWCMSHC_EMMC_DLL_START, DWCMSHC_EMMC_DLL_CTRL);
497 		sdhci_writel(host, DLL_RXCLK_ORI_GATE, DWCMSHC_EMMC_DLL_RXCLK);
498 		sdhci_writel(host, 0, DWCMSHC_EMMC_DLL_TXCLK);
499 		sdhci_writel(host, 0, DECMSHC_EMMC_DLL_CMDOUT);
500 		/*
501 		 * Before switching to hs400es mode, the driver will enable
502 		 * enhanced strobe first. PHY needs to configure the parameters
503 		 * of enhanced strobe first.
504 		 */
505 		extra = DWCMSHC_EMMC_DLL_DLYENA |
506 			DLL_STRBIN_DELAY_NUM_SEL |
507 			DLL_STRBIN_DELAY_NUM_DEFAULT << DLL_STRBIN_DELAY_NUM_OFFSET;
508 		sdhci_writel(host, extra, DWCMSHC_EMMC_DLL_STRBIN);
509 		return;
510 	}
511 
512 	/* Reset DLL */
513 	sdhci_writel(host, BIT(1), DWCMSHC_EMMC_DLL_CTRL);
514 	udelay(1);
515 	sdhci_writel(host, 0x0, DWCMSHC_EMMC_DLL_CTRL);
516 
517 	/*
518 	 * We shouldn't set DLL_RXCLK_NO_INVERTER for identify mode but
519 	 * we must set it in higher speed mode.
520 	 */
521 	extra = DWCMSHC_EMMC_DLL_DLYENA;
522 	if (priv->devtype == DWCMSHC_RK3568)
523 		extra |= DLL_RXCLK_NO_INVERTER << DWCMSHC_EMMC_DLL_RXCLK_SRCSEL;
524 	sdhci_writel(host, extra, DWCMSHC_EMMC_DLL_RXCLK);
525 
526 	/* Init DLL settings */
527 	extra = 0x5 << DWCMSHC_EMMC_DLL_START_POINT |
528 		0x2 << DWCMSHC_EMMC_DLL_INC |
529 		DWCMSHC_EMMC_DLL_START;
530 	sdhci_writel(host, extra, DWCMSHC_EMMC_DLL_CTRL);
531 	err = readl_poll_timeout(host->ioaddr + DWCMSHC_EMMC_DLL_STATUS0,
532 				 extra, DLL_LOCK_WO_TMOUT(extra), 1,
533 				 500 * USEC_PER_MSEC);
534 	if (err) {
535 		dev_err(mmc_dev(host->mmc), "DLL lock timeout!\n");
536 		return;
537 	}
538 
539 	extra = 0x1 << 16 | /* tune clock stop en */
540 		0x3 << 17 | /* pre-change delay */
541 		0x3 << 19;  /* post-change delay */
542 	sdhci_writel(host, extra, dwc_priv->vendor_specific_area1 + DWCMSHC_EMMC_ATCTRL);
543 
544 	if (host->mmc->ios.timing == MMC_TIMING_MMC_HS200 ||
545 	    host->mmc->ios.timing == MMC_TIMING_MMC_HS400)
546 		txclk_tapnum = priv->txclk_tapnum;
547 
548 	if ((priv->devtype == DWCMSHC_RK3588) && host->mmc->ios.timing == MMC_TIMING_MMC_HS400) {
549 		txclk_tapnum = DLL_TXCLK_TAPNUM_90_DEGREES;
550 
551 		extra = DLL_CMDOUT_SRC_CLK_NEG |
552 			DLL_CMDOUT_EN_SRC_CLK_NEG |
553 			DWCMSHC_EMMC_DLL_DLYENA |
554 			DLL_CMDOUT_TAPNUM_90_DEGREES |
555 			DLL_CMDOUT_TAPNUM_FROM_SW;
556 		sdhci_writel(host, extra, DECMSHC_EMMC_DLL_CMDOUT);
557 	}
558 
559 	extra = DWCMSHC_EMMC_DLL_DLYENA |
560 		DLL_TXCLK_TAPNUM_FROM_SW |
561 		DLL_RXCLK_NO_INVERTER << DWCMSHC_EMMC_DLL_RXCLK_SRCSEL |
562 		txclk_tapnum;
563 	sdhci_writel(host, extra, DWCMSHC_EMMC_DLL_TXCLK);
564 
565 	extra = DWCMSHC_EMMC_DLL_DLYENA |
566 		DLL_STRBIN_TAPNUM_DEFAULT |
567 		DLL_STRBIN_TAPNUM_FROM_SW;
568 	sdhci_writel(host, extra, DWCMSHC_EMMC_DLL_STRBIN);
569 }
570 
571 static void rk35xx_sdhci_reset(struct sdhci_host *host, u8 mask)
572 {
573 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
574 	struct dwcmshc_priv *dwc_priv = sdhci_pltfm_priv(pltfm_host);
575 	struct rk35xx_priv *priv = dwc_priv->priv;
576 
577 	if (mask & SDHCI_RESET_ALL && priv->reset) {
578 		reset_control_assert(priv->reset);
579 		udelay(1);
580 		reset_control_deassert(priv->reset);
581 	}
582 
583 	sdhci_reset(host, mask);
584 }
585 
586 static int th1520_execute_tuning(struct sdhci_host *host, u32 opcode)
587 {
588 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
589 	struct dwcmshc_priv *priv = sdhci_pltfm_priv(pltfm_host);
590 	u32 val = 0;
591 
592 	if (host->flags & SDHCI_HS400_TUNING)
593 		return 0;
594 
595 	sdhci_writeb(host, FIELD_PREP(PHY_ATDL_CNFG_INPSEL_MASK, PHY_ATDL_CNFG_INPSEL),
596 		     PHY_ATDL_CNFG_R);
597 	val = sdhci_readl(host, priv->vendor_specific_area1 + DWCMSHC_EMMC_ATCTRL);
598 
599 	/*
600 	 * configure tuning settings:
601 	 *  - center phase select code driven in block gap interval
602 	 *  - disable reporting of framing errors
603 	 *  - disable software managed tuning
604 	 *  - disable user selection of sampling window edges,
605 	 *    instead tuning calculated edges are used
606 	 */
607 	val &= ~(AT_CTRL_CI_SEL | AT_CTRL_RPT_TUNE_ERR | AT_CTRL_SW_TUNE_EN |
608 		 FIELD_PREP(AT_CTRL_WIN_EDGE_SEL_MASK, AT_CTRL_WIN_EDGE_SEL));
609 
610 	/*
611 	 * configure tuning settings:
612 	 *  - enable auto-tuning
613 	 *  - enable sampling window threshold
614 	 *  - stop clocks during phase code change
615 	 *  - set max latency in cycles between tx and rx clocks
616 	 *  - set max latency in cycles to switch output phase
617 	 *  - set max sampling window threshold value
618 	 */
619 	val |= AT_CTRL_AT_EN | AT_CTRL_SWIN_TH_EN | AT_CTRL_TUNE_CLK_STOP_EN;
620 	val |= FIELD_PREP(AT_CTRL_PRE_CHANGE_DLY_MASK, AT_CTRL_PRE_CHANGE_DLY);
621 	val |= FIELD_PREP(AT_CTRL_POST_CHANGE_DLY_MASK, AT_CTRL_POST_CHANGE_DLY);
622 	val |= FIELD_PREP(AT_CTRL_SWIN_TH_VAL_MASK, AT_CTRL_SWIN_TH_VAL);
623 
624 	sdhci_writel(host, val, priv->vendor_specific_area1 + DWCMSHC_EMMC_ATCTRL);
625 	val = sdhci_readl(host, priv->vendor_specific_area1 + DWCMSHC_EMMC_ATCTRL);
626 
627 	/* perform tuning */
628 	sdhci_start_tuning(host);
629 	host->tuning_err = __sdhci_execute_tuning(host, opcode);
630 	if (host->tuning_err) {
631 		/* disable auto-tuning upon tuning error */
632 		val &= ~AT_CTRL_AT_EN;
633 		sdhci_writel(host, val, priv->vendor_specific_area1 + DWCMSHC_EMMC_ATCTRL);
634 		dev_err(mmc_dev(host->mmc), "tuning failed: %d\n", host->tuning_err);
635 		return -EIO;
636 	}
637 	sdhci_end_tuning(host);
638 
639 	return 0;
640 }
641 
642 static void th1520_sdhci_reset(struct sdhci_host *host, u8 mask)
643 {
644 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
645 	struct dwcmshc_priv *priv = sdhci_pltfm_priv(pltfm_host);
646 	u16 ctrl_2;
647 
648 	sdhci_reset(host, mask);
649 
650 	if (priv->flags & FLAG_IO_FIXED_1V8) {
651 		ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
652 		if (!(ctrl_2 & SDHCI_CTRL_VDD_180)) {
653 			ctrl_2 |= SDHCI_CTRL_VDD_180;
654 			sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
655 		}
656 	}
657 }
658 
659 static void cv18xx_sdhci_reset(struct sdhci_host *host, u8 mask)
660 {
661 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
662 	struct dwcmshc_priv *priv = sdhci_pltfm_priv(pltfm_host);
663 	u32 val, emmc_caps = MMC_CAP2_NO_SD | MMC_CAP2_NO_SDIO;
664 
665 	sdhci_reset(host, mask);
666 
667 	if ((host->mmc->caps2 & emmc_caps) == emmc_caps) {
668 		val = sdhci_readl(host, priv->vendor_specific_area1 + CV18XX_SDHCI_MSHC_CTRL);
669 		val |= CV18XX_EMMC_FUNC_EN;
670 		sdhci_writel(host, val, priv->vendor_specific_area1 + CV18XX_SDHCI_MSHC_CTRL);
671 	}
672 
673 	val = sdhci_readl(host, priv->vendor_specific_area1 + CV18XX_SDHCI_MSHC_CTRL);
674 	val |= CV18XX_LATANCY_1T;
675 	sdhci_writel(host, val, priv->vendor_specific_area1 + CV18XX_SDHCI_MSHC_CTRL);
676 
677 	val = sdhci_readl(host, priv->vendor_specific_area1 + CV18XX_SDHCI_PHY_CONFIG);
678 	val |= CV18XX_PHY_TX_BPS;
679 	sdhci_writel(host, val, priv->vendor_specific_area1 + CV18XX_SDHCI_PHY_CONFIG);
680 
681 	val =  (FIELD_PREP(CV18XX_PHY_TX_DLY_MSK, 0) |
682 		FIELD_PREP(CV18XX_PHY_TX_SRC_MSK, CV18XX_PHY_TX_SRC_INVERT_CLK_TX) |
683 		FIELD_PREP(CV18XX_PHY_RX_DLY_MSK, 0) |
684 		FIELD_PREP(CV18XX_PHY_RX_SRC_MSK, CV18XX_PHY_RX_SRC_INVERT_RX_CLK));
685 	sdhci_writel(host, val, priv->vendor_specific_area1 + CV18XX_SDHCI_PHY_TX_RX_DLY);
686 }
687 
688 static const struct sdhci_ops sdhci_dwcmshc_ops = {
689 	.set_clock		= sdhci_set_clock,
690 	.set_bus_width		= sdhci_set_bus_width,
691 	.set_uhs_signaling	= dwcmshc_set_uhs_signaling,
692 	.get_max_clock		= dwcmshc_get_max_clock,
693 	.reset			= sdhci_reset,
694 	.adma_write_desc	= dwcmshc_adma_write_desc,
695 };
696 
697 static const struct sdhci_ops sdhci_dwcmshc_rk35xx_ops = {
698 	.set_clock		= dwcmshc_rk3568_set_clock,
699 	.set_bus_width		= sdhci_set_bus_width,
700 	.set_uhs_signaling	= dwcmshc_set_uhs_signaling,
701 	.get_max_clock		= rk35xx_get_max_clock,
702 	.reset			= rk35xx_sdhci_reset,
703 	.adma_write_desc	= dwcmshc_adma_write_desc,
704 };
705 
706 static const struct sdhci_ops sdhci_dwcmshc_th1520_ops = {
707 	.set_clock		= sdhci_set_clock,
708 	.set_bus_width		= sdhci_set_bus_width,
709 	.set_uhs_signaling	= th1520_set_uhs_signaling,
710 	.get_max_clock		= dwcmshc_get_max_clock,
711 	.reset			= th1520_sdhci_reset,
712 	.adma_write_desc	= dwcmshc_adma_write_desc,
713 	.voltage_switch		= dwcmshc_phy_1_8v_init,
714 	.platform_execute_tuning = &th1520_execute_tuning,
715 };
716 
717 static const struct sdhci_ops sdhci_dwcmshc_cv18xx_ops = {
718 	.set_clock		= sdhci_set_clock,
719 	.set_bus_width		= sdhci_set_bus_width,
720 	.set_uhs_signaling	= dwcmshc_set_uhs_signaling,
721 	.get_max_clock		= dwcmshc_get_max_clock,
722 	.reset			= cv18xx_sdhci_reset,
723 	.adma_write_desc	= dwcmshc_adma_write_desc,
724 };
725 
726 static const struct sdhci_pltfm_data sdhci_dwcmshc_pdata = {
727 	.ops = &sdhci_dwcmshc_ops,
728 	.quirks = SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN,
729 	.quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN,
730 };
731 
732 #ifdef CONFIG_ACPI
733 static const struct sdhci_pltfm_data sdhci_dwcmshc_bf3_pdata = {
734 	.ops = &sdhci_dwcmshc_ops,
735 	.quirks = SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN,
736 	.quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN |
737 		   SDHCI_QUIRK2_ACMD23_BROKEN,
738 };
739 #endif
740 
741 static const struct sdhci_pltfm_data sdhci_dwcmshc_rk35xx_pdata = {
742 	.ops = &sdhci_dwcmshc_rk35xx_ops,
743 	.quirks = SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN |
744 		  SDHCI_QUIRK_BROKEN_TIMEOUT_VAL,
745 	.quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN |
746 		   SDHCI_QUIRK2_CLOCK_DIV_ZERO_BROKEN,
747 };
748 
749 static const struct sdhci_pltfm_data sdhci_dwcmshc_th1520_pdata = {
750 	.ops = &sdhci_dwcmshc_th1520_ops,
751 	.quirks = SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN,
752 	.quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN,
753 };
754 
755 static const struct sdhci_pltfm_data sdhci_dwcmshc_cv18xx_pdata = {
756 	.ops = &sdhci_dwcmshc_cv18xx_ops,
757 	.quirks = SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN,
758 	.quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN,
759 };
760 
761 static int dwcmshc_rk35xx_init(struct sdhci_host *host, struct dwcmshc_priv *dwc_priv)
762 {
763 	int err;
764 	struct rk35xx_priv *priv = dwc_priv->priv;
765 
766 	priv->reset = devm_reset_control_array_get_optional_exclusive(mmc_dev(host->mmc));
767 	if (IS_ERR(priv->reset)) {
768 		err = PTR_ERR(priv->reset);
769 		dev_err(mmc_dev(host->mmc), "failed to get reset control %d\n", err);
770 		return err;
771 	}
772 
773 	priv->rockchip_clks[0].id = "axi";
774 	priv->rockchip_clks[1].id = "block";
775 	priv->rockchip_clks[2].id = "timer";
776 	err = devm_clk_bulk_get_optional(mmc_dev(host->mmc), RK35xx_MAX_CLKS,
777 					 priv->rockchip_clks);
778 	if (err) {
779 		dev_err(mmc_dev(host->mmc), "failed to get clocks %d\n", err);
780 		return err;
781 	}
782 
783 	err = clk_bulk_prepare_enable(RK35xx_MAX_CLKS, priv->rockchip_clks);
784 	if (err) {
785 		dev_err(mmc_dev(host->mmc), "failed to enable clocks %d\n", err);
786 		return err;
787 	}
788 
789 	if (of_property_read_u8(mmc_dev(host->mmc)->of_node, "rockchip,txclk-tapnum",
790 				&priv->txclk_tapnum))
791 		priv->txclk_tapnum = DLL_TXCLK_TAPNUM_DEFAULT;
792 
793 	/* Disable cmd conflict check */
794 	sdhci_writel(host, 0x0, dwc_priv->vendor_specific_area1 + DWCMSHC_HOST_CTRL3);
795 	/* Reset previous settings */
796 	sdhci_writel(host, 0, DWCMSHC_EMMC_DLL_TXCLK);
797 	sdhci_writel(host, 0, DWCMSHC_EMMC_DLL_STRBIN);
798 
799 	return 0;
800 }
801 
802 static void dwcmshc_rk35xx_postinit(struct sdhci_host *host, struct dwcmshc_priv *dwc_priv)
803 {
804 	/*
805 	 * Don't support highspeed bus mode with low clk speed as we
806 	 * cannot use DLL for this condition.
807 	 */
808 	if (host->mmc->f_max <= 52000000) {
809 		dev_info(mmc_dev(host->mmc), "Disabling HS200/HS400, frequency too low (%d)\n",
810 			 host->mmc->f_max);
811 		host->mmc->caps2 &= ~(MMC_CAP2_HS200 | MMC_CAP2_HS400);
812 		host->mmc->caps &= ~(MMC_CAP_3_3V_DDR | MMC_CAP_1_8V_DDR);
813 	}
814 }
815 
816 static const struct of_device_id sdhci_dwcmshc_dt_ids[] = {
817 	{
818 		.compatible = "rockchip,rk3588-dwcmshc",
819 		.data = &sdhci_dwcmshc_rk35xx_pdata,
820 	},
821 	{
822 		.compatible = "rockchip,rk3568-dwcmshc",
823 		.data = &sdhci_dwcmshc_rk35xx_pdata,
824 	},
825 	{
826 		.compatible = "snps,dwcmshc-sdhci",
827 		.data = &sdhci_dwcmshc_pdata,
828 	},
829 	{
830 		.compatible = "sophgo,cv1800b-dwcmshc",
831 		.data = &sdhci_dwcmshc_cv18xx_pdata,
832 	},
833 	{
834 		.compatible = "sophgo,sg2002-dwcmshc",
835 		.data = &sdhci_dwcmshc_cv18xx_pdata,
836 	},
837 	{
838 		.compatible = "thead,th1520-dwcmshc",
839 		.data = &sdhci_dwcmshc_th1520_pdata,
840 	},
841 	{},
842 };
843 MODULE_DEVICE_TABLE(of, sdhci_dwcmshc_dt_ids);
844 
845 #ifdef CONFIG_ACPI
846 static const struct acpi_device_id sdhci_dwcmshc_acpi_ids[] = {
847 	{
848 		.id = "MLNXBF30",
849 		.driver_data = (kernel_ulong_t)&sdhci_dwcmshc_bf3_pdata,
850 	},
851 	{}
852 };
853 MODULE_DEVICE_TABLE(acpi, sdhci_dwcmshc_acpi_ids);
854 #endif
855 
856 static int dwcmshc_probe(struct platform_device *pdev)
857 {
858 	struct device *dev = &pdev->dev;
859 	struct sdhci_pltfm_host *pltfm_host;
860 	struct sdhci_host *host;
861 	struct dwcmshc_priv *priv;
862 	struct rk35xx_priv *rk_priv = NULL;
863 	const struct sdhci_pltfm_data *pltfm_data;
864 	int err;
865 	u32 extra;
866 
867 	pltfm_data = device_get_match_data(&pdev->dev);
868 	if (!pltfm_data) {
869 		dev_err(&pdev->dev, "Error: No device match data found\n");
870 		return -ENODEV;
871 	}
872 
873 	host = sdhci_pltfm_init(pdev, pltfm_data,
874 				sizeof(struct dwcmshc_priv));
875 	if (IS_ERR(host))
876 		return PTR_ERR(host);
877 
878 	/*
879 	 * extra adma table cnt for cross 128M boundary handling.
880 	 */
881 	extra = DIV_ROUND_UP_ULL(dma_get_required_mask(dev), SZ_128M);
882 	if (extra > SDHCI_MAX_SEGS)
883 		extra = SDHCI_MAX_SEGS;
884 	host->adma_table_cnt += extra;
885 
886 	pltfm_host = sdhci_priv(host);
887 	priv = sdhci_pltfm_priv(pltfm_host);
888 
889 	if (dev->of_node) {
890 		pltfm_host->clk = devm_clk_get(dev, "core");
891 		if (IS_ERR(pltfm_host->clk)) {
892 			err = PTR_ERR(pltfm_host->clk);
893 			dev_err(dev, "failed to get core clk: %d\n", err);
894 			goto free_pltfm;
895 		}
896 		err = clk_prepare_enable(pltfm_host->clk);
897 		if (err)
898 			goto free_pltfm;
899 
900 		priv->bus_clk = devm_clk_get(dev, "bus");
901 		if (!IS_ERR(priv->bus_clk))
902 			clk_prepare_enable(priv->bus_clk);
903 	}
904 
905 	err = mmc_of_parse(host->mmc);
906 	if (err)
907 		goto err_clk;
908 
909 	sdhci_get_of_property(pdev);
910 
911 	priv->vendor_specific_area1 =
912 		sdhci_readl(host, DWCMSHC_P_VENDOR_AREA1) & DWCMSHC_AREA1_MASK;
913 
914 	host->mmc_host_ops.request = dwcmshc_request;
915 	host->mmc_host_ops.hs400_enhanced_strobe = dwcmshc_hs400_enhanced_strobe;
916 
917 	if (pltfm_data == &sdhci_dwcmshc_rk35xx_pdata) {
918 		rk_priv = devm_kzalloc(&pdev->dev, sizeof(struct rk35xx_priv), GFP_KERNEL);
919 		if (!rk_priv) {
920 			err = -ENOMEM;
921 			goto err_clk;
922 		}
923 
924 		if (of_device_is_compatible(pdev->dev.of_node, "rockchip,rk3588-dwcmshc"))
925 			rk_priv->devtype = DWCMSHC_RK3588;
926 		else
927 			rk_priv->devtype = DWCMSHC_RK3568;
928 
929 		priv->priv = rk_priv;
930 
931 		err = dwcmshc_rk35xx_init(host, priv);
932 		if (err)
933 			goto err_clk;
934 	}
935 
936 	if (pltfm_data == &sdhci_dwcmshc_th1520_pdata) {
937 		priv->delay_line = PHY_SDCLKDL_DC_DEFAULT;
938 
939 		if (device_property_read_bool(dev, "mmc-ddr-1_8v") ||
940 		    device_property_read_bool(dev, "mmc-hs200-1_8v") ||
941 		    device_property_read_bool(dev, "mmc-hs400-1_8v"))
942 			priv->flags |= FLAG_IO_FIXED_1V8;
943 		else
944 			priv->flags &= ~FLAG_IO_FIXED_1V8;
945 
946 		/*
947 		 * start_signal_voltage_switch() will try 3.3V first
948 		 * then 1.8V. Use SDHCI_SIGNALING_180 rather than
949 		 * SDHCI_SIGNALING_330 to avoid setting voltage to 3.3V
950 		 * in sdhci_start_signal_voltage_switch().
951 		 */
952 		if (priv->flags & FLAG_IO_FIXED_1V8) {
953 			host->flags &= ~SDHCI_SIGNALING_330;
954 			host->flags |=  SDHCI_SIGNALING_180;
955 		}
956 
957 		sdhci_enable_v4_mode(host);
958 	}
959 
960 #ifdef CONFIG_ACPI
961 	if (pltfm_data == &sdhci_dwcmshc_bf3_pdata)
962 		sdhci_enable_v4_mode(host);
963 #endif
964 
965 	host->mmc->caps |= MMC_CAP_WAIT_WHILE_BUSY;
966 
967 	pm_runtime_get_noresume(dev);
968 	pm_runtime_set_active(dev);
969 	pm_runtime_enable(dev);
970 
971 	err = sdhci_setup_host(host);
972 	if (err)
973 		goto err_rpm;
974 
975 	if (rk_priv)
976 		dwcmshc_rk35xx_postinit(host, priv);
977 
978 	err = __sdhci_add_host(host);
979 	if (err)
980 		goto err_setup_host;
981 
982 	pm_runtime_put(dev);
983 
984 	return 0;
985 
986 err_setup_host:
987 	sdhci_cleanup_host(host);
988 err_rpm:
989 	pm_runtime_disable(dev);
990 	pm_runtime_put_noidle(dev);
991 err_clk:
992 	clk_disable_unprepare(pltfm_host->clk);
993 	clk_disable_unprepare(priv->bus_clk);
994 	if (rk_priv)
995 		clk_bulk_disable_unprepare(RK35xx_MAX_CLKS,
996 					   rk_priv->rockchip_clks);
997 free_pltfm:
998 	sdhci_pltfm_free(pdev);
999 	return err;
1000 }
1001 
1002 static void dwcmshc_disable_card_clk(struct sdhci_host *host)
1003 {
1004 	u16 ctrl;
1005 
1006 	ctrl = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1007 	if (ctrl & SDHCI_CLOCK_CARD_EN) {
1008 		ctrl &= ~SDHCI_CLOCK_CARD_EN;
1009 		sdhci_writew(host, ctrl, SDHCI_CLOCK_CONTROL);
1010 	}
1011 }
1012 
1013 static void dwcmshc_remove(struct platform_device *pdev)
1014 {
1015 	struct sdhci_host *host = platform_get_drvdata(pdev);
1016 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1017 	struct dwcmshc_priv *priv = sdhci_pltfm_priv(pltfm_host);
1018 	struct rk35xx_priv *rk_priv = priv->priv;
1019 
1020 	pm_runtime_get_sync(&pdev->dev);
1021 	pm_runtime_disable(&pdev->dev);
1022 	pm_runtime_put_noidle(&pdev->dev);
1023 
1024 	sdhci_remove_host(host, 0);
1025 
1026 	dwcmshc_disable_card_clk(host);
1027 
1028 	clk_disable_unprepare(pltfm_host->clk);
1029 	clk_disable_unprepare(priv->bus_clk);
1030 	if (rk_priv)
1031 		clk_bulk_disable_unprepare(RK35xx_MAX_CLKS,
1032 					   rk_priv->rockchip_clks);
1033 	sdhci_pltfm_free(pdev);
1034 }
1035 
1036 #ifdef CONFIG_PM_SLEEP
1037 static int dwcmshc_suspend(struct device *dev)
1038 {
1039 	struct sdhci_host *host = dev_get_drvdata(dev);
1040 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1041 	struct dwcmshc_priv *priv = sdhci_pltfm_priv(pltfm_host);
1042 	struct rk35xx_priv *rk_priv = priv->priv;
1043 	int ret;
1044 
1045 	pm_runtime_resume(dev);
1046 
1047 	ret = sdhci_suspend_host(host);
1048 	if (ret)
1049 		return ret;
1050 
1051 	clk_disable_unprepare(pltfm_host->clk);
1052 	if (!IS_ERR(priv->bus_clk))
1053 		clk_disable_unprepare(priv->bus_clk);
1054 
1055 	if (rk_priv)
1056 		clk_bulk_disable_unprepare(RK35xx_MAX_CLKS,
1057 					   rk_priv->rockchip_clks);
1058 
1059 	return ret;
1060 }
1061 
1062 static int dwcmshc_resume(struct device *dev)
1063 {
1064 	struct sdhci_host *host = dev_get_drvdata(dev);
1065 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1066 	struct dwcmshc_priv *priv = sdhci_pltfm_priv(pltfm_host);
1067 	struct rk35xx_priv *rk_priv = priv->priv;
1068 	int ret;
1069 
1070 	ret = clk_prepare_enable(pltfm_host->clk);
1071 	if (ret)
1072 		return ret;
1073 
1074 	if (!IS_ERR(priv->bus_clk)) {
1075 		ret = clk_prepare_enable(priv->bus_clk);
1076 		if (ret)
1077 			goto disable_clk;
1078 	}
1079 
1080 	if (rk_priv) {
1081 		ret = clk_bulk_prepare_enable(RK35xx_MAX_CLKS,
1082 					      rk_priv->rockchip_clks);
1083 		if (ret)
1084 			goto disable_bus_clk;
1085 	}
1086 
1087 	ret = sdhci_resume_host(host);
1088 	if (ret)
1089 		goto disable_rockchip_clks;
1090 
1091 	return 0;
1092 
1093 disable_rockchip_clks:
1094 	if (rk_priv)
1095 		clk_bulk_disable_unprepare(RK35xx_MAX_CLKS,
1096 					   rk_priv->rockchip_clks);
1097 disable_bus_clk:
1098 	if (!IS_ERR(priv->bus_clk))
1099 		clk_disable_unprepare(priv->bus_clk);
1100 disable_clk:
1101 	clk_disable_unprepare(pltfm_host->clk);
1102 	return ret;
1103 }
1104 #endif
1105 
1106 #ifdef CONFIG_PM
1107 
1108 static void dwcmshc_enable_card_clk(struct sdhci_host *host)
1109 {
1110 	u16 ctrl;
1111 
1112 	ctrl = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1113 	if ((ctrl & SDHCI_CLOCK_INT_EN) && !(ctrl & SDHCI_CLOCK_CARD_EN)) {
1114 		ctrl |= SDHCI_CLOCK_CARD_EN;
1115 		sdhci_writew(host, ctrl, SDHCI_CLOCK_CONTROL);
1116 	}
1117 }
1118 
1119 static int dwcmshc_runtime_suspend(struct device *dev)
1120 {
1121 	struct sdhci_host *host = dev_get_drvdata(dev);
1122 
1123 	dwcmshc_disable_card_clk(host);
1124 
1125 	return 0;
1126 }
1127 
1128 static int dwcmshc_runtime_resume(struct device *dev)
1129 {
1130 	struct sdhci_host *host = dev_get_drvdata(dev);
1131 
1132 	dwcmshc_enable_card_clk(host);
1133 
1134 	return 0;
1135 }
1136 
1137 #endif
1138 
1139 static const struct dev_pm_ops dwcmshc_pmops = {
1140 	SET_SYSTEM_SLEEP_PM_OPS(dwcmshc_suspend, dwcmshc_resume)
1141 	SET_RUNTIME_PM_OPS(dwcmshc_runtime_suspend,
1142 			   dwcmshc_runtime_resume, NULL)
1143 };
1144 
1145 static struct platform_driver sdhci_dwcmshc_driver = {
1146 	.driver	= {
1147 		.name	= "sdhci-dwcmshc",
1148 		.probe_type = PROBE_PREFER_ASYNCHRONOUS,
1149 		.of_match_table = sdhci_dwcmshc_dt_ids,
1150 		.acpi_match_table = ACPI_PTR(sdhci_dwcmshc_acpi_ids),
1151 		.pm = &dwcmshc_pmops,
1152 	},
1153 	.probe	= dwcmshc_probe,
1154 	.remove_new = dwcmshc_remove,
1155 };
1156 module_platform_driver(sdhci_dwcmshc_driver);
1157 
1158 MODULE_DESCRIPTION("SDHCI platform driver for Synopsys DWC MSHC");
1159 MODULE_AUTHOR("Jisheng Zhang <jszhang@kernel.org>");
1160 MODULE_LICENSE("GPL v2");
1161