xref: /openbmc/linux/drivers/mmc/host/sdhci-pci-gli.c (revision e73ad388)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2019 Genesys Logic, Inc.
4  *
5  * Authors: Ben Chuang <ben.chuang@genesyslogic.com.tw>
6  *
7  * Version: v0.9.0 (2019-08-08)
8  */
9 
10 #include <linux/bitfield.h>
11 #include <linux/bits.h>
12 #include <linux/pci.h>
13 #include <linux/mmc/mmc.h>
14 #include <linux/delay.h>
15 #include "sdhci.h"
16 #include "sdhci-pci.h"
17 #include "cqhci.h"
18 
19 /*  Genesys Logic extra registers */
20 #define SDHCI_GLI_9750_WT         0x800
21 #define   SDHCI_GLI_9750_WT_EN      BIT(0)
22 #define   GLI_9750_WT_EN_ON	    0x1
23 #define   GLI_9750_WT_EN_OFF	    0x0
24 
25 #define SDHCI_GLI_9750_CFG2          0x848
26 #define   SDHCI_GLI_9750_CFG2_L1DLY    GENMASK(28, 24)
27 #define   GLI_9750_CFG2_L1DLY_VALUE    0x1F
28 
29 #define SDHCI_GLI_9750_DRIVING      0x860
30 #define   SDHCI_GLI_9750_DRIVING_1    GENMASK(11, 0)
31 #define   SDHCI_GLI_9750_DRIVING_2    GENMASK(27, 26)
32 #define   GLI_9750_DRIVING_1_VALUE    0xFFF
33 #define   GLI_9750_DRIVING_2_VALUE    0x3
34 #define   SDHCI_GLI_9750_SEL_1        BIT(29)
35 #define   SDHCI_GLI_9750_SEL_2        BIT(31)
36 #define   SDHCI_GLI_9750_ALL_RST      (BIT(24)|BIT(25)|BIT(28)|BIT(30))
37 
38 #define SDHCI_GLI_9750_PLL	      0x864
39 #define   SDHCI_GLI_9750_PLL_LDIV       GENMASK(9, 0)
40 #define   SDHCI_GLI_9750_PLL_PDIV       GENMASK(14, 12)
41 #define   SDHCI_GLI_9750_PLL_DIR        BIT(15)
42 #define   SDHCI_GLI_9750_PLL_TX2_INV    BIT(23)
43 #define   SDHCI_GLI_9750_PLL_TX2_DLY    GENMASK(22, 20)
44 #define   GLI_9750_PLL_TX2_INV_VALUE    0x1
45 #define   GLI_9750_PLL_TX2_DLY_VALUE    0x0
46 #define   SDHCI_GLI_9750_PLLSSC_STEP    GENMASK(28, 24)
47 #define   SDHCI_GLI_9750_PLLSSC_EN      BIT(31)
48 
49 #define SDHCI_GLI_9750_PLLSSC        0x86C
50 #define   SDHCI_GLI_9750_PLLSSC_PPM    GENMASK(31, 16)
51 
52 #define SDHCI_GLI_9750_SW_CTRL      0x874
53 #define   SDHCI_GLI_9750_SW_CTRL_4    GENMASK(7, 6)
54 #define   GLI_9750_SW_CTRL_4_VALUE    0x3
55 
56 #define SDHCI_GLI_9750_MISC            0x878
57 #define   SDHCI_GLI_9750_MISC_TX1_INV    BIT(2)
58 #define   SDHCI_GLI_9750_MISC_RX_INV     BIT(3)
59 #define   SDHCI_GLI_9750_MISC_TX1_DLY    GENMASK(6, 4)
60 #define   GLI_9750_MISC_TX1_INV_VALUE    0x0
61 #define   GLI_9750_MISC_RX_INV_ON        0x1
62 #define   GLI_9750_MISC_RX_INV_OFF       0x0
63 #define   GLI_9750_MISC_RX_INV_VALUE     GLI_9750_MISC_RX_INV_OFF
64 #define   GLI_9750_MISC_TX1_DLY_VALUE    0x5
65 
66 #define SDHCI_GLI_9750_TUNING_CONTROL	          0x540
67 #define   SDHCI_GLI_9750_TUNING_CONTROL_EN          BIT(4)
68 #define   GLI_9750_TUNING_CONTROL_EN_ON             0x1
69 #define   GLI_9750_TUNING_CONTROL_EN_OFF            0x0
70 #define   SDHCI_GLI_9750_TUNING_CONTROL_GLITCH_1    BIT(16)
71 #define   SDHCI_GLI_9750_TUNING_CONTROL_GLITCH_2    GENMASK(20, 19)
72 #define   GLI_9750_TUNING_CONTROL_GLITCH_1_VALUE    0x1
73 #define   GLI_9750_TUNING_CONTROL_GLITCH_2_VALUE    0x2
74 
75 #define SDHCI_GLI_9750_TUNING_PARAMETERS           0x544
76 #define   SDHCI_GLI_9750_TUNING_PARAMETERS_RX_DLY    GENMASK(2, 0)
77 #define   GLI_9750_TUNING_PARAMETERS_RX_DLY_VALUE    0x1
78 
79 #define SDHCI_GLI_9763E_CTRL_HS400  0x7
80 
81 #define SDHCI_GLI_9763E_HS400_ES_REG      0x52C
82 #define   SDHCI_GLI_9763E_HS400_ES_BIT      BIT(8)
83 
84 #define PCIE_GLI_9763E_VHS	 0x884
85 #define   GLI_9763E_VHS_REV	   GENMASK(19, 16)
86 #define   GLI_9763E_VHS_REV_R      0x0
87 #define   GLI_9763E_VHS_REV_M      0x1
88 #define   GLI_9763E_VHS_REV_W      0x2
89 #define PCIE_GLI_9763E_MB	 0x888
90 #define   GLI_9763E_MB_CMDQ_OFF	   BIT(19)
91 #define   GLI_9763E_MB_ERP_ON      BIT(7)
92 #define PCIE_GLI_9763E_SCR	 0x8E0
93 #define   GLI_9763E_SCR_AXI_REQ	   BIT(9)
94 
95 #define PCIE_GLI_9763E_CFG2      0x8A4
96 #define   GLI_9763E_CFG2_L1DLY     GENMASK(28, 19)
97 #define   GLI_9763E_CFG2_L1DLY_MID 0x50
98 
99 #define PCIE_GLI_9763E_MMC_CTRL  0x960
100 #define   GLI_9763E_HS400_SLOW     BIT(3)
101 
102 #define PCIE_GLI_9763E_CLKRXDLY  0x934
103 #define   GLI_9763E_HS400_RXDLY    GENMASK(31, 28)
104 #define   GLI_9763E_HS400_RXDLY_5  0x5
105 
106 #define SDHCI_GLI_9763E_CQE_BASE_ADDR	 0x200
107 #define GLI_9763E_CQE_TRNS_MODE	   (SDHCI_TRNS_MULTI | \
108 				    SDHCI_TRNS_BLK_CNT_EN | \
109 				    SDHCI_TRNS_DMA)
110 
111 #define PCI_GLI_9755_WT       0x800
112 #define   PCI_GLI_9755_WT_EN    BIT(0)
113 #define   GLI_9755_WT_EN_ON     0x1
114 #define   GLI_9755_WT_EN_OFF    0x0
115 
116 #define PCI_GLI_9755_PECONF   0x44
117 #define   PCI_GLI_9755_LFCLK    GENMASK(14, 12)
118 #define   PCI_GLI_9755_DMACLK   BIT(29)
119 
120 #define PCI_GLI_9755_CFG2          0x48
121 #define   PCI_GLI_9755_CFG2_L1DLY    GENMASK(28, 24)
122 #define   GLI_9755_CFG2_L1DLY_VALUE  0x1F
123 
124 #define PCI_GLI_9755_PLL            0x64
125 #define   PCI_GLI_9755_PLL_LDIV       GENMASK(9, 0)
126 #define   PCI_GLI_9755_PLL_PDIV       GENMASK(14, 12)
127 #define   PCI_GLI_9755_PLL_DIR        BIT(15)
128 #define   PCI_GLI_9755_PLLSSC_STEP    GENMASK(28, 24)
129 #define   PCI_GLI_9755_PLLSSC_EN      BIT(31)
130 
131 #define PCI_GLI_9755_PLLSSC        0x68
132 #define   PCI_GLI_9755_PLLSSC_PPM    GENMASK(15, 0)
133 
134 #define PCI_GLI_9755_SerDes  0x70
135 #define PCI_GLI_9755_SCP_DIS   BIT(19)
136 
137 #define GLI_MAX_TUNING_LOOP 40
138 
139 /* Genesys Logic chipset */
140 static inline void gl9750_wt_on(struct sdhci_host *host)
141 {
142 	u32 wt_value;
143 	u32 wt_enable;
144 
145 	wt_value = sdhci_readl(host, SDHCI_GLI_9750_WT);
146 	wt_enable = FIELD_GET(SDHCI_GLI_9750_WT_EN, wt_value);
147 
148 	if (wt_enable == GLI_9750_WT_EN_ON)
149 		return;
150 
151 	wt_value &= ~SDHCI_GLI_9750_WT_EN;
152 	wt_value |= FIELD_PREP(SDHCI_GLI_9750_WT_EN, GLI_9750_WT_EN_ON);
153 
154 	sdhci_writel(host, wt_value, SDHCI_GLI_9750_WT);
155 }
156 
157 static inline void gl9750_wt_off(struct sdhci_host *host)
158 {
159 	u32 wt_value;
160 	u32 wt_enable;
161 
162 	wt_value = sdhci_readl(host, SDHCI_GLI_9750_WT);
163 	wt_enable = FIELD_GET(SDHCI_GLI_9750_WT_EN, wt_value);
164 
165 	if (wt_enable == GLI_9750_WT_EN_OFF)
166 		return;
167 
168 	wt_value &= ~SDHCI_GLI_9750_WT_EN;
169 	wt_value |= FIELD_PREP(SDHCI_GLI_9750_WT_EN, GLI_9750_WT_EN_OFF);
170 
171 	sdhci_writel(host, wt_value, SDHCI_GLI_9750_WT);
172 }
173 
174 static void gli_set_9750(struct sdhci_host *host)
175 {
176 	u32 driving_value;
177 	u32 pll_value;
178 	u32 sw_ctrl_value;
179 	u32 misc_value;
180 	u32 parameter_value;
181 	u32 control_value;
182 	u16 ctrl2;
183 
184 	gl9750_wt_on(host);
185 
186 	driving_value = sdhci_readl(host, SDHCI_GLI_9750_DRIVING);
187 	pll_value = sdhci_readl(host, SDHCI_GLI_9750_PLL);
188 	sw_ctrl_value = sdhci_readl(host, SDHCI_GLI_9750_SW_CTRL);
189 	misc_value = sdhci_readl(host, SDHCI_GLI_9750_MISC);
190 	parameter_value = sdhci_readl(host, SDHCI_GLI_9750_TUNING_PARAMETERS);
191 	control_value = sdhci_readl(host, SDHCI_GLI_9750_TUNING_CONTROL);
192 
193 	driving_value &= ~(SDHCI_GLI_9750_DRIVING_1);
194 	driving_value &= ~(SDHCI_GLI_9750_DRIVING_2);
195 	driving_value |= FIELD_PREP(SDHCI_GLI_9750_DRIVING_1,
196 				    GLI_9750_DRIVING_1_VALUE);
197 	driving_value |= FIELD_PREP(SDHCI_GLI_9750_DRIVING_2,
198 				    GLI_9750_DRIVING_2_VALUE);
199 	driving_value &= ~(SDHCI_GLI_9750_SEL_1|SDHCI_GLI_9750_SEL_2|SDHCI_GLI_9750_ALL_RST);
200 	driving_value |= SDHCI_GLI_9750_SEL_2;
201 	sdhci_writel(host, driving_value, SDHCI_GLI_9750_DRIVING);
202 
203 	sw_ctrl_value &= ~SDHCI_GLI_9750_SW_CTRL_4;
204 	sw_ctrl_value |= FIELD_PREP(SDHCI_GLI_9750_SW_CTRL_4,
205 				    GLI_9750_SW_CTRL_4_VALUE);
206 	sdhci_writel(host, sw_ctrl_value, SDHCI_GLI_9750_SW_CTRL);
207 
208 	/* reset the tuning flow after reinit and before starting tuning */
209 	pll_value &= ~SDHCI_GLI_9750_PLL_TX2_INV;
210 	pll_value &= ~SDHCI_GLI_9750_PLL_TX2_DLY;
211 	pll_value |= FIELD_PREP(SDHCI_GLI_9750_PLL_TX2_INV,
212 				GLI_9750_PLL_TX2_INV_VALUE);
213 	pll_value |= FIELD_PREP(SDHCI_GLI_9750_PLL_TX2_DLY,
214 				GLI_9750_PLL_TX2_DLY_VALUE);
215 
216 	misc_value &= ~SDHCI_GLI_9750_MISC_TX1_INV;
217 	misc_value &= ~SDHCI_GLI_9750_MISC_RX_INV;
218 	misc_value &= ~SDHCI_GLI_9750_MISC_TX1_DLY;
219 	misc_value |= FIELD_PREP(SDHCI_GLI_9750_MISC_TX1_INV,
220 				 GLI_9750_MISC_TX1_INV_VALUE);
221 	misc_value |= FIELD_PREP(SDHCI_GLI_9750_MISC_RX_INV,
222 				 GLI_9750_MISC_RX_INV_VALUE);
223 	misc_value |= FIELD_PREP(SDHCI_GLI_9750_MISC_TX1_DLY,
224 				 GLI_9750_MISC_TX1_DLY_VALUE);
225 
226 	parameter_value &= ~SDHCI_GLI_9750_TUNING_PARAMETERS_RX_DLY;
227 	parameter_value |= FIELD_PREP(SDHCI_GLI_9750_TUNING_PARAMETERS_RX_DLY,
228 				      GLI_9750_TUNING_PARAMETERS_RX_DLY_VALUE);
229 
230 	control_value &= ~SDHCI_GLI_9750_TUNING_CONTROL_GLITCH_1;
231 	control_value &= ~SDHCI_GLI_9750_TUNING_CONTROL_GLITCH_2;
232 	control_value |= FIELD_PREP(SDHCI_GLI_9750_TUNING_CONTROL_GLITCH_1,
233 				    GLI_9750_TUNING_CONTROL_GLITCH_1_VALUE);
234 	control_value |= FIELD_PREP(SDHCI_GLI_9750_TUNING_CONTROL_GLITCH_2,
235 				    GLI_9750_TUNING_CONTROL_GLITCH_2_VALUE);
236 
237 	sdhci_writel(host, pll_value, SDHCI_GLI_9750_PLL);
238 	sdhci_writel(host, misc_value, SDHCI_GLI_9750_MISC);
239 
240 	/* disable tuned clk */
241 	ctrl2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
242 	ctrl2 &= ~SDHCI_CTRL_TUNED_CLK;
243 	sdhci_writew(host, ctrl2, SDHCI_HOST_CONTROL2);
244 
245 	/* enable tuning parameters control */
246 	control_value &= ~SDHCI_GLI_9750_TUNING_CONTROL_EN;
247 	control_value |= FIELD_PREP(SDHCI_GLI_9750_TUNING_CONTROL_EN,
248 				    GLI_9750_TUNING_CONTROL_EN_ON);
249 	sdhci_writel(host, control_value, SDHCI_GLI_9750_TUNING_CONTROL);
250 
251 	/* write tuning parameters */
252 	sdhci_writel(host, parameter_value, SDHCI_GLI_9750_TUNING_PARAMETERS);
253 
254 	/* disable tuning parameters control */
255 	control_value &= ~SDHCI_GLI_9750_TUNING_CONTROL_EN;
256 	control_value |= FIELD_PREP(SDHCI_GLI_9750_TUNING_CONTROL_EN,
257 				    GLI_9750_TUNING_CONTROL_EN_OFF);
258 	sdhci_writel(host, control_value, SDHCI_GLI_9750_TUNING_CONTROL);
259 
260 	/* clear tuned clk */
261 	ctrl2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
262 	ctrl2 &= ~SDHCI_CTRL_TUNED_CLK;
263 	sdhci_writew(host, ctrl2, SDHCI_HOST_CONTROL2);
264 
265 	gl9750_wt_off(host);
266 }
267 
268 static void gli_set_9750_rx_inv(struct sdhci_host *host, bool b)
269 {
270 	u32 misc_value;
271 
272 	gl9750_wt_on(host);
273 
274 	misc_value = sdhci_readl(host, SDHCI_GLI_9750_MISC);
275 	misc_value &= ~SDHCI_GLI_9750_MISC_RX_INV;
276 	if (b) {
277 		misc_value |= FIELD_PREP(SDHCI_GLI_9750_MISC_RX_INV,
278 					 GLI_9750_MISC_RX_INV_ON);
279 	} else {
280 		misc_value |= FIELD_PREP(SDHCI_GLI_9750_MISC_RX_INV,
281 					 GLI_9750_MISC_RX_INV_OFF);
282 	}
283 	sdhci_writel(host, misc_value, SDHCI_GLI_9750_MISC);
284 
285 	gl9750_wt_off(host);
286 }
287 
288 static int __sdhci_execute_tuning_9750(struct sdhci_host *host, u32 opcode)
289 {
290 	int i;
291 	int rx_inv;
292 
293 	for (rx_inv = 0; rx_inv < 2; rx_inv++) {
294 		gli_set_9750_rx_inv(host, !!rx_inv);
295 		sdhci_start_tuning(host);
296 
297 		for (i = 0; i < GLI_MAX_TUNING_LOOP; i++) {
298 			u16 ctrl;
299 
300 			sdhci_send_tuning(host, opcode);
301 
302 			if (!host->tuning_done) {
303 				sdhci_abort_tuning(host, opcode);
304 				break;
305 			}
306 
307 			ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
308 			if (!(ctrl & SDHCI_CTRL_EXEC_TUNING)) {
309 				if (ctrl & SDHCI_CTRL_TUNED_CLK)
310 					return 0; /* Success! */
311 				break;
312 			}
313 		}
314 	}
315 	if (!host->tuning_done) {
316 		pr_info("%s: Tuning timeout, falling back to fixed sampling clock\n",
317 			mmc_hostname(host->mmc));
318 		return -ETIMEDOUT;
319 	}
320 
321 	pr_info("%s: Tuning failed, falling back to fixed sampling clock\n",
322 		mmc_hostname(host->mmc));
323 	sdhci_reset_tuning(host);
324 
325 	return -EAGAIN;
326 }
327 
328 static int gl9750_execute_tuning(struct sdhci_host *host, u32 opcode)
329 {
330 	host->mmc->retune_period = 0;
331 	if (host->tuning_mode == SDHCI_TUNING_MODE_1)
332 		host->mmc->retune_period = host->tuning_count;
333 
334 	gli_set_9750(host);
335 	host->tuning_err = __sdhci_execute_tuning_9750(host, opcode);
336 	sdhci_end_tuning(host);
337 
338 	return 0;
339 }
340 
341 static void gl9750_disable_ssc_pll(struct sdhci_host *host)
342 {
343 	u32 pll;
344 
345 	gl9750_wt_on(host);
346 	pll = sdhci_readl(host, SDHCI_GLI_9750_PLL);
347 	pll &= ~(SDHCI_GLI_9750_PLL_DIR | SDHCI_GLI_9750_PLLSSC_EN);
348 	sdhci_writel(host, pll, SDHCI_GLI_9750_PLL);
349 	gl9750_wt_off(host);
350 }
351 
352 static void gl9750_set_pll(struct sdhci_host *host, u8 dir, u16 ldiv, u8 pdiv)
353 {
354 	u32 pll;
355 
356 	gl9750_wt_on(host);
357 	pll = sdhci_readl(host, SDHCI_GLI_9750_PLL);
358 	pll &= ~(SDHCI_GLI_9750_PLL_LDIV |
359 		 SDHCI_GLI_9750_PLL_PDIV |
360 		 SDHCI_GLI_9750_PLL_DIR);
361 	pll |= FIELD_PREP(SDHCI_GLI_9750_PLL_LDIV, ldiv) |
362 	       FIELD_PREP(SDHCI_GLI_9750_PLL_PDIV, pdiv) |
363 	       FIELD_PREP(SDHCI_GLI_9750_PLL_DIR, dir);
364 	sdhci_writel(host, pll, SDHCI_GLI_9750_PLL);
365 	gl9750_wt_off(host);
366 
367 	/* wait for pll stable */
368 	mdelay(1);
369 }
370 
371 static void gl9750_set_ssc(struct sdhci_host *host, u8 enable, u8 step, u16 ppm)
372 {
373 	u32 pll;
374 	u32 ssc;
375 
376 	gl9750_wt_on(host);
377 	pll = sdhci_readl(host, SDHCI_GLI_9750_PLL);
378 	ssc = sdhci_readl(host, SDHCI_GLI_9750_PLLSSC);
379 	pll &= ~(SDHCI_GLI_9750_PLLSSC_STEP |
380 		 SDHCI_GLI_9750_PLLSSC_EN);
381 	ssc &= ~SDHCI_GLI_9750_PLLSSC_PPM;
382 	pll |= FIELD_PREP(SDHCI_GLI_9750_PLLSSC_STEP, step) |
383 	       FIELD_PREP(SDHCI_GLI_9750_PLLSSC_EN, enable);
384 	ssc |= FIELD_PREP(SDHCI_GLI_9750_PLLSSC_PPM, ppm);
385 	sdhci_writel(host, ssc, SDHCI_GLI_9750_PLLSSC);
386 	sdhci_writel(host, pll, SDHCI_GLI_9750_PLL);
387 	gl9750_wt_off(host);
388 }
389 
390 static void gl9750_set_ssc_pll_205mhz(struct sdhci_host *host)
391 {
392 	/* set pll to 205MHz and enable ssc */
393 	gl9750_set_ssc(host, 0x1, 0x1F, 0xFFE7);
394 	gl9750_set_pll(host, 0x1, 0x246, 0x0);
395 }
396 
397 static void sdhci_gl9750_set_clock(struct sdhci_host *host, unsigned int clock)
398 {
399 	struct mmc_ios *ios = &host->mmc->ios;
400 	u16 clk;
401 
402 	host->mmc->actual_clock = 0;
403 
404 	gl9750_disable_ssc_pll(host);
405 	sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
406 
407 	if (clock == 0)
408 		return;
409 
410 	clk = sdhci_calc_clk(host, clock, &host->mmc->actual_clock);
411 	if (clock == 200000000 && ios->timing == MMC_TIMING_UHS_SDR104) {
412 		host->mmc->actual_clock = 205000000;
413 		gl9750_set_ssc_pll_205mhz(host);
414 	}
415 
416 	sdhci_enable_clk(host, clk);
417 }
418 
419 static void gl9750_hw_setting(struct sdhci_host *host)
420 {
421 	u32 value;
422 
423 	gl9750_wt_on(host);
424 
425 	value = sdhci_readl(host, SDHCI_GLI_9750_CFG2);
426 	value &= ~SDHCI_GLI_9750_CFG2_L1DLY;
427 	/* set ASPM L1 entry delay to 7.9us */
428 	value |= FIELD_PREP(SDHCI_GLI_9750_CFG2_L1DLY,
429 			    GLI_9750_CFG2_L1DLY_VALUE);
430 	sdhci_writel(host, value, SDHCI_GLI_9750_CFG2);
431 
432 	gl9750_wt_off(host);
433 }
434 
435 static void gli_pcie_enable_msi(struct sdhci_pci_slot *slot)
436 {
437 	int ret;
438 
439 	ret = pci_alloc_irq_vectors(slot->chip->pdev, 1, 1,
440 				    PCI_IRQ_MSI | PCI_IRQ_MSIX);
441 	if (ret < 0) {
442 		pr_warn("%s: enable PCI MSI failed, error=%d\n",
443 		       mmc_hostname(slot->host->mmc), ret);
444 		return;
445 	}
446 
447 	slot->host->irq = pci_irq_vector(slot->chip->pdev, 0);
448 }
449 
450 static inline void gl9755_wt_on(struct pci_dev *pdev)
451 {
452 	u32 wt_value;
453 	u32 wt_enable;
454 
455 	pci_read_config_dword(pdev, PCI_GLI_9755_WT, &wt_value);
456 	wt_enable = FIELD_GET(PCI_GLI_9755_WT_EN, wt_value);
457 
458 	if (wt_enable == GLI_9755_WT_EN_ON)
459 		return;
460 
461 	wt_value &= ~PCI_GLI_9755_WT_EN;
462 	wt_value |= FIELD_PREP(PCI_GLI_9755_WT_EN, GLI_9755_WT_EN_ON);
463 
464 	pci_write_config_dword(pdev, PCI_GLI_9755_WT, wt_value);
465 }
466 
467 static inline void gl9755_wt_off(struct pci_dev *pdev)
468 {
469 	u32 wt_value;
470 	u32 wt_enable;
471 
472 	pci_read_config_dword(pdev, PCI_GLI_9755_WT, &wt_value);
473 	wt_enable = FIELD_GET(PCI_GLI_9755_WT_EN, wt_value);
474 
475 	if (wt_enable == GLI_9755_WT_EN_OFF)
476 		return;
477 
478 	wt_value &= ~PCI_GLI_9755_WT_EN;
479 	wt_value |= FIELD_PREP(PCI_GLI_9755_WT_EN, GLI_9755_WT_EN_OFF);
480 
481 	pci_write_config_dword(pdev, PCI_GLI_9755_WT, wt_value);
482 }
483 
484 static void gl9755_disable_ssc_pll(struct pci_dev *pdev)
485 {
486 	u32 pll;
487 
488 	gl9755_wt_on(pdev);
489 	pci_read_config_dword(pdev, PCI_GLI_9755_PLL, &pll);
490 	pll &= ~(PCI_GLI_9755_PLL_DIR | PCI_GLI_9755_PLLSSC_EN);
491 	pci_write_config_dword(pdev, PCI_GLI_9755_PLL, pll);
492 	gl9755_wt_off(pdev);
493 }
494 
495 static void gl9755_set_pll(struct pci_dev *pdev, u8 dir, u16 ldiv, u8 pdiv)
496 {
497 	u32 pll;
498 
499 	gl9755_wt_on(pdev);
500 	pci_read_config_dword(pdev, PCI_GLI_9755_PLL, &pll);
501 	pll &= ~(PCI_GLI_9755_PLL_LDIV |
502 		 PCI_GLI_9755_PLL_PDIV |
503 		 PCI_GLI_9755_PLL_DIR);
504 	pll |= FIELD_PREP(PCI_GLI_9755_PLL_LDIV, ldiv) |
505 	       FIELD_PREP(PCI_GLI_9755_PLL_PDIV, pdiv) |
506 	       FIELD_PREP(PCI_GLI_9755_PLL_DIR, dir);
507 	pci_write_config_dword(pdev, PCI_GLI_9755_PLL, pll);
508 	gl9755_wt_off(pdev);
509 
510 	/* wait for pll stable */
511 	mdelay(1);
512 }
513 
514 static void gl9755_set_ssc(struct pci_dev *pdev, u8 enable, u8 step, u16 ppm)
515 {
516 	u32 pll;
517 	u32 ssc;
518 
519 	gl9755_wt_on(pdev);
520 	pci_read_config_dword(pdev, PCI_GLI_9755_PLL, &pll);
521 	pci_read_config_dword(pdev, PCI_GLI_9755_PLLSSC, &ssc);
522 	pll &= ~(PCI_GLI_9755_PLLSSC_STEP |
523 		 PCI_GLI_9755_PLLSSC_EN);
524 	ssc &= ~PCI_GLI_9755_PLLSSC_PPM;
525 	pll |= FIELD_PREP(PCI_GLI_9755_PLLSSC_STEP, step) |
526 	       FIELD_PREP(PCI_GLI_9755_PLLSSC_EN, enable);
527 	ssc |= FIELD_PREP(PCI_GLI_9755_PLLSSC_PPM, ppm);
528 	pci_write_config_dword(pdev, PCI_GLI_9755_PLLSSC, ssc);
529 	pci_write_config_dword(pdev, PCI_GLI_9755_PLL, pll);
530 	gl9755_wt_off(pdev);
531 }
532 
533 static void gl9755_set_ssc_pll_205mhz(struct pci_dev *pdev)
534 {
535 	/* set pll to 205MHz and enable ssc */
536 	gl9755_set_ssc(pdev, 0x1, 0x1F, 0xFFE7);
537 	gl9755_set_pll(pdev, 0x1, 0x246, 0x0);
538 }
539 
540 static void sdhci_gl9755_set_clock(struct sdhci_host *host, unsigned int clock)
541 {
542 	struct sdhci_pci_slot *slot = sdhci_priv(host);
543 	struct mmc_ios *ios = &host->mmc->ios;
544 	struct pci_dev *pdev;
545 	u16 clk;
546 
547 	pdev = slot->chip->pdev;
548 	host->mmc->actual_clock = 0;
549 
550 	gl9755_disable_ssc_pll(pdev);
551 	sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
552 
553 	if (clock == 0)
554 		return;
555 
556 	clk = sdhci_calc_clk(host, clock, &host->mmc->actual_clock);
557 	if (clock == 200000000 && ios->timing == MMC_TIMING_UHS_SDR104) {
558 		host->mmc->actual_clock = 205000000;
559 		gl9755_set_ssc_pll_205mhz(pdev);
560 	}
561 
562 	sdhci_enable_clk(host, clk);
563 }
564 
565 static void gl9755_hw_setting(struct sdhci_pci_slot *slot)
566 {
567 	struct pci_dev *pdev = slot->chip->pdev;
568 	u32 value;
569 
570 	gl9755_wt_on(pdev);
571 
572 	pci_read_config_dword(pdev, PCI_GLI_9755_PECONF, &value);
573 	value &= ~PCI_GLI_9755_LFCLK;
574 	value &= ~PCI_GLI_9755_DMACLK;
575 	pci_write_config_dword(pdev, PCI_GLI_9755_PECONF, value);
576 
577 	/* enable short circuit protection */
578 	pci_read_config_dword(pdev, PCI_GLI_9755_SerDes, &value);
579 	value &= ~PCI_GLI_9755_SCP_DIS;
580 	pci_write_config_dword(pdev, PCI_GLI_9755_SerDes, value);
581 
582 	pci_read_config_dword(pdev, PCI_GLI_9755_CFG2, &value);
583 	value &= ~PCI_GLI_9755_CFG2_L1DLY;
584 	/* set ASPM L1 entry delay to 7.9us */
585 	value |= FIELD_PREP(PCI_GLI_9755_CFG2_L1DLY,
586 			    GLI_9755_CFG2_L1DLY_VALUE);
587 	pci_write_config_dword(pdev, PCI_GLI_9755_CFG2, value);
588 
589 	gl9755_wt_off(pdev);
590 }
591 
592 static int gli_probe_slot_gl9750(struct sdhci_pci_slot *slot)
593 {
594 	struct sdhci_host *host = slot->host;
595 
596 	gl9750_hw_setting(host);
597 	gli_pcie_enable_msi(slot);
598 	slot->host->mmc->caps2 |= MMC_CAP2_NO_SDIO;
599 	sdhci_enable_v4_mode(host);
600 
601 	return 0;
602 }
603 
604 static int gli_probe_slot_gl9755(struct sdhci_pci_slot *slot)
605 {
606 	struct sdhci_host *host = slot->host;
607 
608 	gl9755_hw_setting(slot);
609 	gli_pcie_enable_msi(slot);
610 	slot->host->mmc->caps2 |= MMC_CAP2_NO_SDIO;
611 	sdhci_enable_v4_mode(host);
612 
613 	return 0;
614 }
615 
616 static void sdhci_gli_voltage_switch(struct sdhci_host *host)
617 {
618 	/*
619 	 * According to Section 3.6.1 signal voltage switch procedure in
620 	 * SD Host Controller Simplified Spec. 4.20, steps 6~8 are as
621 	 * follows:
622 	 * (6) Set 1.8V Signal Enable in the Host Control 2 register.
623 	 * (7) Wait 5ms. 1.8V voltage regulator shall be stable within this
624 	 *     period.
625 	 * (8) If 1.8V Signal Enable is cleared by Host Controller, go to
626 	 *     step (12).
627 	 *
628 	 * Wait 5ms after set 1.8V signal enable in Host Control 2 register
629 	 * to ensure 1.8V signal enable bit is set by GL9750/GL9755.
630 	 */
631 	usleep_range(5000, 5500);
632 }
633 
634 static void sdhci_gl9750_reset(struct sdhci_host *host, u8 mask)
635 {
636 	sdhci_reset(host, mask);
637 	gli_set_9750(host);
638 }
639 
640 static u32 sdhci_gl9750_readl(struct sdhci_host *host, int reg)
641 {
642 	u32 value;
643 
644 	value = readl(host->ioaddr + reg);
645 	if (unlikely(reg == SDHCI_MAX_CURRENT && !(value & 0xff)))
646 		value |= 0xc8;
647 
648 	return value;
649 }
650 
651 #ifdef CONFIG_PM_SLEEP
652 static int sdhci_pci_gli_resume(struct sdhci_pci_chip *chip)
653 {
654 	struct sdhci_pci_slot *slot = chip->slots[0];
655 
656 	pci_free_irq_vectors(slot->chip->pdev);
657 	gli_pcie_enable_msi(slot);
658 
659 	return sdhci_pci_resume_host(chip);
660 }
661 
662 static int sdhci_cqhci_gli_resume(struct sdhci_pci_chip *chip)
663 {
664 	struct sdhci_pci_slot *slot = chip->slots[0];
665 	int ret;
666 
667 	ret = sdhci_pci_gli_resume(chip);
668 	if (ret)
669 		return ret;
670 
671 	return cqhci_resume(slot->host->mmc);
672 }
673 
674 static int sdhci_cqhci_gli_suspend(struct sdhci_pci_chip *chip)
675 {
676 	struct sdhci_pci_slot *slot = chip->slots[0];
677 	int ret;
678 
679 	ret = cqhci_suspend(slot->host->mmc);
680 	if (ret)
681 		return ret;
682 
683 	return sdhci_suspend_host(slot->host);
684 }
685 #endif
686 
687 static void gl9763e_hs400_enhanced_strobe(struct mmc_host *mmc,
688 					  struct mmc_ios *ios)
689 {
690 	struct sdhci_host *host = mmc_priv(mmc);
691 	u32 val;
692 
693 	val = sdhci_readl(host, SDHCI_GLI_9763E_HS400_ES_REG);
694 	if (ios->enhanced_strobe)
695 		val |= SDHCI_GLI_9763E_HS400_ES_BIT;
696 	else
697 		val &= ~SDHCI_GLI_9763E_HS400_ES_BIT;
698 
699 	sdhci_writel(host, val, SDHCI_GLI_9763E_HS400_ES_REG);
700 }
701 
702 static void sdhci_set_gl9763e_signaling(struct sdhci_host *host,
703 					unsigned int timing)
704 {
705 	u16 ctrl_2;
706 
707 	ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
708 	ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
709 	if (timing == MMC_TIMING_MMC_HS200)
710 		ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
711 	else if (timing == MMC_TIMING_MMC_HS)
712 		ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
713 	else if (timing == MMC_TIMING_MMC_DDR52)
714 		ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
715 	else if (timing == MMC_TIMING_MMC_HS400)
716 		ctrl_2 |= SDHCI_GLI_9763E_CTRL_HS400;
717 
718 	sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
719 }
720 
721 static void sdhci_gl9763e_dumpregs(struct mmc_host *mmc)
722 {
723 	sdhci_dumpregs(mmc_priv(mmc));
724 }
725 
726 static void sdhci_gl9763e_cqe_pre_enable(struct mmc_host *mmc)
727 {
728 	struct cqhci_host *cq_host = mmc->cqe_private;
729 	u32 value;
730 
731 	value = cqhci_readl(cq_host, CQHCI_CFG);
732 	value |= CQHCI_ENABLE;
733 	cqhci_writel(cq_host, value, CQHCI_CFG);
734 }
735 
736 static void sdhci_gl9763e_cqe_enable(struct mmc_host *mmc)
737 {
738 	struct sdhci_host *host = mmc_priv(mmc);
739 
740 	sdhci_writew(host, GLI_9763E_CQE_TRNS_MODE, SDHCI_TRANSFER_MODE);
741 	sdhci_cqe_enable(mmc);
742 }
743 
744 static u32 sdhci_gl9763e_cqhci_irq(struct sdhci_host *host, u32 intmask)
745 {
746 	int cmd_error = 0;
747 	int data_error = 0;
748 
749 	if (!sdhci_cqe_irq(host, intmask, &cmd_error, &data_error))
750 		return intmask;
751 
752 	cqhci_irq(host->mmc, intmask, cmd_error, data_error);
753 
754 	return 0;
755 }
756 
757 static void sdhci_gl9763e_cqe_post_disable(struct mmc_host *mmc)
758 {
759 	struct sdhci_host *host = mmc_priv(mmc);
760 	struct cqhci_host *cq_host = mmc->cqe_private;
761 	u32 value;
762 
763 	value = cqhci_readl(cq_host, CQHCI_CFG);
764 	value &= ~CQHCI_ENABLE;
765 	cqhci_writel(cq_host, value, CQHCI_CFG);
766 	sdhci_writew(host, 0x0, SDHCI_TRANSFER_MODE);
767 }
768 
769 static const struct cqhci_host_ops sdhci_gl9763e_cqhci_ops = {
770 	.enable         = sdhci_gl9763e_cqe_enable,
771 	.disable        = sdhci_cqe_disable,
772 	.dumpregs       = sdhci_gl9763e_dumpregs,
773 	.pre_enable     = sdhci_gl9763e_cqe_pre_enable,
774 	.post_disable   = sdhci_gl9763e_cqe_post_disable,
775 };
776 
777 static int gl9763e_add_host(struct sdhci_pci_slot *slot)
778 {
779 	struct device *dev = &slot->chip->pdev->dev;
780 	struct sdhci_host *host = slot->host;
781 	struct cqhci_host *cq_host;
782 	bool dma64;
783 	int ret;
784 
785 	ret = sdhci_setup_host(host);
786 	if (ret)
787 		return ret;
788 
789 	cq_host = devm_kzalloc(dev, sizeof(*cq_host), GFP_KERNEL);
790 	if (!cq_host) {
791 		ret = -ENOMEM;
792 		goto cleanup;
793 	}
794 
795 	cq_host->mmio = host->ioaddr + SDHCI_GLI_9763E_CQE_BASE_ADDR;
796 	cq_host->ops = &sdhci_gl9763e_cqhci_ops;
797 
798 	dma64 = host->flags & SDHCI_USE_64_BIT_DMA;
799 	if (dma64)
800 		cq_host->caps |= CQHCI_TASK_DESC_SZ_128;
801 
802 	ret = cqhci_init(cq_host, host->mmc, dma64);
803 	if (ret)
804 		goto cleanup;
805 
806 	ret = __sdhci_add_host(host);
807 	if (ret)
808 		goto cleanup;
809 
810 	return 0;
811 
812 cleanup:
813 	sdhci_cleanup_host(host);
814 	return ret;
815 }
816 
817 static void sdhci_gl9763e_reset(struct sdhci_host *host, u8 mask)
818 {
819 	if ((host->mmc->caps2 & MMC_CAP2_CQE) && (mask & SDHCI_RESET_ALL) &&
820 	    host->mmc->cqe_private)
821 		cqhci_deactivate(host->mmc);
822 	sdhci_reset(host, mask);
823 }
824 
825 static void gli_set_gl9763e(struct sdhci_pci_slot *slot)
826 {
827 	struct pci_dev *pdev = slot->chip->pdev;
828 	u32 value;
829 
830 	pci_read_config_dword(pdev, PCIE_GLI_9763E_VHS, &value);
831 	value &= ~GLI_9763E_VHS_REV;
832 	value |= FIELD_PREP(GLI_9763E_VHS_REV, GLI_9763E_VHS_REV_W);
833 	pci_write_config_dword(pdev, PCIE_GLI_9763E_VHS, value);
834 
835 	pci_read_config_dword(pdev, PCIE_GLI_9763E_SCR, &value);
836 	value |= GLI_9763E_SCR_AXI_REQ;
837 	pci_write_config_dword(pdev, PCIE_GLI_9763E_SCR, value);
838 
839 	pci_read_config_dword(pdev, PCIE_GLI_9763E_MMC_CTRL, &value);
840 	value &= ~GLI_9763E_HS400_SLOW;
841 	pci_write_config_dword(pdev, PCIE_GLI_9763E_MMC_CTRL, value);
842 
843 	pci_read_config_dword(pdev, PCIE_GLI_9763E_CFG2, &value);
844 	value &= ~GLI_9763E_CFG2_L1DLY;
845 	/* set ASPM L1 entry delay to 20us */
846 	value |= FIELD_PREP(GLI_9763E_CFG2_L1DLY, GLI_9763E_CFG2_L1DLY_MID);
847 	pci_write_config_dword(pdev, PCIE_GLI_9763E_CFG2, value);
848 
849 	pci_read_config_dword(pdev, PCIE_GLI_9763E_CLKRXDLY, &value);
850 	value &= ~GLI_9763E_HS400_RXDLY;
851 	value |= FIELD_PREP(GLI_9763E_HS400_RXDLY, GLI_9763E_HS400_RXDLY_5);
852 	pci_write_config_dword(pdev, PCIE_GLI_9763E_CLKRXDLY, value);
853 
854 	pci_read_config_dword(pdev, PCIE_GLI_9763E_VHS, &value);
855 	value &= ~GLI_9763E_VHS_REV;
856 	value |= FIELD_PREP(GLI_9763E_VHS_REV, GLI_9763E_VHS_REV_R);
857 	pci_write_config_dword(pdev, PCIE_GLI_9763E_VHS, value);
858 }
859 
860 static int gli_probe_slot_gl9763e(struct sdhci_pci_slot *slot)
861 {
862 	struct pci_dev *pdev = slot->chip->pdev;
863 	struct sdhci_host *host = slot->host;
864 	u32 value;
865 
866 	host->mmc->caps |= MMC_CAP_8_BIT_DATA |
867 			   MMC_CAP_1_8V_DDR |
868 			   MMC_CAP_NONREMOVABLE;
869 	host->mmc->caps2 |= MMC_CAP2_HS200_1_8V_SDR |
870 			    MMC_CAP2_HS400_1_8V |
871 			    MMC_CAP2_HS400_ES |
872 			    MMC_CAP2_NO_SDIO |
873 			    MMC_CAP2_NO_SD;
874 
875 	pci_read_config_dword(pdev, PCIE_GLI_9763E_MB, &value);
876 	if (!(value & GLI_9763E_MB_CMDQ_OFF))
877 		if (value & GLI_9763E_MB_ERP_ON)
878 			host->mmc->caps2 |= MMC_CAP2_CQE | MMC_CAP2_CQE_DCMD;
879 
880 	gli_pcie_enable_msi(slot);
881 	host->mmc_host_ops.hs400_enhanced_strobe =
882 					gl9763e_hs400_enhanced_strobe;
883 	gli_set_gl9763e(slot);
884 	sdhci_enable_v4_mode(host);
885 
886 	return 0;
887 }
888 
889 static const struct sdhci_ops sdhci_gl9755_ops = {
890 	.set_clock		= sdhci_gl9755_set_clock,
891 	.enable_dma		= sdhci_pci_enable_dma,
892 	.set_bus_width		= sdhci_set_bus_width,
893 	.reset			= sdhci_reset,
894 	.set_uhs_signaling	= sdhci_set_uhs_signaling,
895 	.voltage_switch		= sdhci_gli_voltage_switch,
896 };
897 
898 const struct sdhci_pci_fixes sdhci_gl9755 = {
899 	.quirks		= SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
900 	.quirks2	= SDHCI_QUIRK2_BROKEN_DDR50,
901 	.probe_slot	= gli_probe_slot_gl9755,
902 	.ops            = &sdhci_gl9755_ops,
903 #ifdef CONFIG_PM_SLEEP
904 	.resume         = sdhci_pci_gli_resume,
905 #endif
906 };
907 
908 static const struct sdhci_ops sdhci_gl9750_ops = {
909 	.read_l                 = sdhci_gl9750_readl,
910 	.set_clock		= sdhci_gl9750_set_clock,
911 	.enable_dma		= sdhci_pci_enable_dma,
912 	.set_bus_width		= sdhci_set_bus_width,
913 	.reset			= sdhci_gl9750_reset,
914 	.set_uhs_signaling	= sdhci_set_uhs_signaling,
915 	.voltage_switch		= sdhci_gli_voltage_switch,
916 	.platform_execute_tuning = gl9750_execute_tuning,
917 };
918 
919 const struct sdhci_pci_fixes sdhci_gl9750 = {
920 	.quirks		= SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
921 	.quirks2	= SDHCI_QUIRK2_BROKEN_DDR50,
922 	.probe_slot	= gli_probe_slot_gl9750,
923 	.ops            = &sdhci_gl9750_ops,
924 #ifdef CONFIG_PM_SLEEP
925 	.resume         = sdhci_pci_gli_resume,
926 #endif
927 };
928 
929 static const struct sdhci_ops sdhci_gl9763e_ops = {
930 	.set_clock		= sdhci_set_clock,
931 	.enable_dma		= sdhci_pci_enable_dma,
932 	.set_bus_width		= sdhci_set_bus_width,
933 	.reset			= sdhci_gl9763e_reset,
934 	.set_uhs_signaling	= sdhci_set_gl9763e_signaling,
935 	.voltage_switch		= sdhci_gli_voltage_switch,
936 	.irq                    = sdhci_gl9763e_cqhci_irq,
937 };
938 
939 const struct sdhci_pci_fixes sdhci_gl9763e = {
940 	.quirks		= SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
941 	.probe_slot	= gli_probe_slot_gl9763e,
942 	.ops            = &sdhci_gl9763e_ops,
943 #ifdef CONFIG_PM_SLEEP
944 	.resume		= sdhci_cqhci_gli_resume,
945 	.suspend	= sdhci_cqhci_gli_suspend,
946 #endif
947 	.add_host       = gl9763e_add_host,
948 };
949