1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Arasan Secure Digital Host Controller Interface.
4  * Copyright (C) 2011 - 2012 Michal Simek <monstr@monstr.eu>
5  * Copyright (c) 2012 Wind River Systems, Inc.
6  * Copyright (C) 2013 Pengutronix e.K.
7  * Copyright (C) 2013 Xilinx Inc.
8  *
9  * Based on sdhci-of-esdhc.c
10  *
11  * Copyright (c) 2007 Freescale Semiconductor, Inc.
12  * Copyright (c) 2009 MontaVista Software, Inc.
13  *
14  * Authors: Xiaobo Xie <X.Xie@freescale.com>
15  *	    Anton Vorontsov <avorontsov@ru.mvista.com>
16  */
17 
18 #include <linux/clk-provider.h>
19 #include <linux/mfd/syscon.h>
20 #include <linux/module.h>
21 #include <linux/of_device.h>
22 #include <linux/phy/phy.h>
23 #include <linux/regmap.h>
24 #include <linux/of.h>
25 #include <linux/firmware/xlnx-zynqmp.h>
26 
27 #include "cqhci.h"
28 #include "sdhci-cqhci.h"
29 #include "sdhci-pltfm.h"
30 
31 #define SDHCI_ARASAN_VENDOR_REGISTER	0x78
32 
33 #define SDHCI_ARASAN_ITAPDLY_REGISTER	0xF0F8
34 #define SDHCI_ARASAN_ITAPDLY_SEL_MASK	0xFF
35 
36 #define SDHCI_ARASAN_OTAPDLY_REGISTER	0xF0FC
37 #define SDHCI_ARASAN_OTAPDLY_SEL_MASK	0x3F
38 
39 #define SDHCI_ARASAN_CQE_BASE_ADDR	0x200
40 #define VENDOR_ENHANCED_STROBE		BIT(0)
41 
42 #define PHY_CLK_TOO_SLOW_HZ		400000
43 
44 #define SDHCI_ITAPDLY_CHGWIN		0x200
45 #define SDHCI_ITAPDLY_ENABLE		0x100
46 #define SDHCI_OTAPDLY_ENABLE		0x40
47 
48 /* Default settings for ZynqMP Clock Phases */
49 #define ZYNQMP_ICLK_PHASE {0, 63, 63, 0, 63,  0,   0, 183, 54,  0, 0}
50 #define ZYNQMP_OCLK_PHASE {0, 72, 60, 0, 60, 72, 135, 48, 72, 135, 0}
51 
52 #define VERSAL_ICLK_PHASE {0, 132, 132, 0, 132, 0, 0, 162, 90, 0, 0}
53 #define VERSAL_OCLK_PHASE {0,  60, 48, 0, 48, 72, 90, 36, 60, 90, 0}
54 
55 /*
56  * On some SoCs the syscon area has a feature where the upper 16-bits of
57  * each 32-bit register act as a write mask for the lower 16-bits.  This allows
58  * atomic updates of the register without locking.  This macro is used on SoCs
59  * that have that feature.
60  */
61 #define HIWORD_UPDATE(val, mask, shift) \
62 		((val) << (shift) | (mask) << ((shift) + 16))
63 
64 /**
65  * struct sdhci_arasan_soc_ctl_field - Field used in sdhci_arasan_soc_ctl_map
66  *
67  * @reg:	Offset within the syscon of the register containing this field
68  * @width:	Number of bits for this field
69  * @shift:	Bit offset within @reg of this field (or -1 if not avail)
70  */
71 struct sdhci_arasan_soc_ctl_field {
72 	u32 reg;
73 	u16 width;
74 	s16 shift;
75 };
76 
77 /**
78  * struct sdhci_arasan_soc_ctl_map - Map in syscon to corecfg registers
79  *
80  * @baseclkfreq:	Where to find corecfg_baseclkfreq
81  * @clockmultiplier:	Where to find corecfg_clockmultiplier
82  * @support64b:		Where to find SUPPORT64B bit
83  * @hiword_update:	If true, use HIWORD_UPDATE to access the syscon
84  *
85  * It's up to the licensee of the Arsan IP block to make these available
86  * somewhere if needed.  Presumably these will be scattered somewhere that's
87  * accessible via the syscon API.
88  */
89 struct sdhci_arasan_soc_ctl_map {
90 	struct sdhci_arasan_soc_ctl_field	baseclkfreq;
91 	struct sdhci_arasan_soc_ctl_field	clockmultiplier;
92 	struct sdhci_arasan_soc_ctl_field	support64b;
93 	bool					hiword_update;
94 };
95 
96 /**
97  * struct sdhci_arasan_clk_ops - Clock Operations for Arasan SD controller
98  *
99  * @sdcardclk_ops:	The output clock related operations
100  * @sampleclk_ops:	The sample clock related operations
101  */
102 struct sdhci_arasan_clk_ops {
103 	const struct clk_ops *sdcardclk_ops;
104 	const struct clk_ops *sampleclk_ops;
105 };
106 
107 /**
108  * struct sdhci_arasan_clk_data - Arasan Controller Clock Data.
109  *
110  * @sdcardclk_hw:	Struct for the clock we might provide to a PHY.
111  * @sdcardclk:		Pointer to normal 'struct clock' for sdcardclk_hw.
112  * @sampleclk_hw:	Struct for the clock we might provide to a PHY.
113  * @sampleclk:		Pointer to normal 'struct clock' for sampleclk_hw.
114  * @clk_phase_in:	Array of Input Clock Phase Delays for all speed modes
115  * @clk_phase_out:	Array of Output Clock Phase Delays for all speed modes
116  * @set_clk_delays:	Function pointer for setting Clock Delays
117  * @clk_of_data:	Platform specific runtime clock data storage pointer
118  */
119 struct sdhci_arasan_clk_data {
120 	struct clk_hw	sdcardclk_hw;
121 	struct clk      *sdcardclk;
122 	struct clk_hw	sampleclk_hw;
123 	struct clk      *sampleclk;
124 	int		clk_phase_in[MMC_TIMING_MMC_HS400 + 1];
125 	int		clk_phase_out[MMC_TIMING_MMC_HS400 + 1];
126 	void		(*set_clk_delays)(struct sdhci_host *host);
127 	void		*clk_of_data;
128 };
129 
130 /**
131  * struct sdhci_arasan_data - Arasan Controller Data
132  *
133  * @host:		Pointer to the main SDHCI host structure.
134  * @clk_ahb:		Pointer to the AHB clock
135  * @phy:		Pointer to the generic phy
136  * @is_phy_on:		True if the PHY is on; false if not.
137  * @has_cqe:		True if controller has command queuing engine.
138  * @clk_data:		Struct for the Arasan Controller Clock Data.
139  * @clk_ops:		Struct for the Arasan Controller Clock Operations.
140  * @soc_ctl_base:	Pointer to regmap for syscon for soc_ctl registers.
141  * @soc_ctl_map:	Map to get offsets into soc_ctl registers.
142  * @quirks:		Arasan deviations from spec.
143  */
144 struct sdhci_arasan_data {
145 	struct sdhci_host *host;
146 	struct clk	*clk_ahb;
147 	struct phy	*phy;
148 	bool		is_phy_on;
149 
150 	bool		has_cqe;
151 	struct sdhci_arasan_clk_data clk_data;
152 	const struct sdhci_arasan_clk_ops *clk_ops;
153 
154 	struct regmap	*soc_ctl_base;
155 	const struct sdhci_arasan_soc_ctl_map *soc_ctl_map;
156 	unsigned int	quirks;
157 
158 /* Controller does not have CD wired and will not function normally without */
159 #define SDHCI_ARASAN_QUIRK_FORCE_CDTEST	BIT(0)
160 /* Controller immediately reports SDHCI_CLOCK_INT_STABLE after enabling the
161  * internal clock even when the clock isn't stable */
162 #define SDHCI_ARASAN_QUIRK_CLOCK_UNSTABLE BIT(1)
163 /*
164  * Some of the Arasan variations might not have timing requirements
165  * met at 25MHz for Default Speed mode, those controllers work at
166  * 19MHz instead
167  */
168 #define SDHCI_ARASAN_QUIRK_CLOCK_25_BROKEN BIT(2)
169 };
170 
171 struct sdhci_arasan_of_data {
172 	const struct sdhci_arasan_soc_ctl_map *soc_ctl_map;
173 	const struct sdhci_pltfm_data *pdata;
174 	const struct sdhci_arasan_clk_ops *clk_ops;
175 };
176 
177 static const struct sdhci_arasan_soc_ctl_map rk3399_soc_ctl_map = {
178 	.baseclkfreq = { .reg = 0xf000, .width = 8, .shift = 8 },
179 	.clockmultiplier = { .reg = 0xf02c, .width = 8, .shift = 0},
180 	.hiword_update = true,
181 };
182 
183 static const struct sdhci_arasan_soc_ctl_map intel_lgm_emmc_soc_ctl_map = {
184 	.baseclkfreq = { .reg = 0xa0, .width = 8, .shift = 2 },
185 	.clockmultiplier = { .reg = 0, .width = -1, .shift = -1 },
186 	.hiword_update = false,
187 };
188 
189 static const struct sdhci_arasan_soc_ctl_map intel_lgm_sdxc_soc_ctl_map = {
190 	.baseclkfreq = { .reg = 0x80, .width = 8, .shift = 2 },
191 	.clockmultiplier = { .reg = 0, .width = -1, .shift = -1 },
192 	.hiword_update = false,
193 };
194 
195 static const struct sdhci_arasan_soc_ctl_map thunderbay_soc_ctl_map = {
196 	.baseclkfreq = { .reg = 0x0, .width = 8, .shift = 14 },
197 	.clockmultiplier = { .reg = 0x4, .width = 8, .shift = 14 },
198 	.support64b = { .reg = 0x4, .width = 1, .shift = 24 },
199 	.hiword_update = false,
200 };
201 
202 static const struct sdhci_arasan_soc_ctl_map intel_keembay_soc_ctl_map = {
203 	.baseclkfreq = { .reg = 0x0, .width = 8, .shift = 14 },
204 	.clockmultiplier = { .reg = 0x4, .width = 8, .shift = 14 },
205 	.support64b = { .reg = 0x4, .width = 1, .shift = 24 },
206 	.hiword_update = false,
207 };
208 
209 /**
210  * sdhci_arasan_syscon_write - Write to a field in soc_ctl registers
211  *
212  * @host:	The sdhci_host
213  * @fld:	The field to write to
214  * @val:	The value to write
215  *
216  * This function allows writing to fields in sdhci_arasan_soc_ctl_map.
217  * Note that if a field is specified as not available (shift < 0) then
218  * this function will silently return an error code.  It will be noisy
219  * and print errors for any other (unexpected) errors.
220  *
221  * Return: 0 on success and error value on error
222  */
223 static int sdhci_arasan_syscon_write(struct sdhci_host *host,
224 				   const struct sdhci_arasan_soc_ctl_field *fld,
225 				   u32 val)
226 {
227 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
228 	struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
229 	struct regmap *soc_ctl_base = sdhci_arasan->soc_ctl_base;
230 	u32 reg = fld->reg;
231 	u16 width = fld->width;
232 	s16 shift = fld->shift;
233 	int ret;
234 
235 	/*
236 	 * Silently return errors for shift < 0 so caller doesn't have
237 	 * to check for fields which are optional.  For fields that
238 	 * are required then caller needs to do something special
239 	 * anyway.
240 	 */
241 	if (shift < 0)
242 		return -EINVAL;
243 
244 	if (sdhci_arasan->soc_ctl_map->hiword_update)
245 		ret = regmap_write(soc_ctl_base, reg,
246 				   HIWORD_UPDATE(val, GENMASK(width, 0),
247 						 shift));
248 	else
249 		ret = regmap_update_bits(soc_ctl_base, reg,
250 					 GENMASK(shift + width, shift),
251 					 val << shift);
252 
253 	/* Yell about (unexpected) regmap errors */
254 	if (ret)
255 		pr_warn("%s: Regmap write fail: %d\n",
256 			 mmc_hostname(host->mmc), ret);
257 
258 	return ret;
259 }
260 
261 static void sdhci_arasan_set_clock(struct sdhci_host *host, unsigned int clock)
262 {
263 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
264 	struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
265 	struct sdhci_arasan_clk_data *clk_data = &sdhci_arasan->clk_data;
266 	bool ctrl_phy = false;
267 
268 	if (!IS_ERR(sdhci_arasan->phy)) {
269 		if (!sdhci_arasan->is_phy_on && clock <= PHY_CLK_TOO_SLOW_HZ) {
270 			/*
271 			 * If PHY off, set clock to max speed and power PHY on.
272 			 *
273 			 * Although PHY docs apparently suggest power cycling
274 			 * when changing the clock the PHY doesn't like to be
275 			 * powered on while at low speeds like those used in ID
276 			 * mode.  Even worse is powering the PHY on while the
277 			 * clock is off.
278 			 *
279 			 * To workaround the PHY limitations, the best we can
280 			 * do is to power it on at a faster speed and then slam
281 			 * through low speeds without power cycling.
282 			 */
283 			sdhci_set_clock(host, host->max_clk);
284 			if (phy_power_on(sdhci_arasan->phy)) {
285 				pr_err("%s: Cannot power on phy.\n",
286 				       mmc_hostname(host->mmc));
287 				return;
288 			}
289 
290 			sdhci_arasan->is_phy_on = true;
291 
292 			/*
293 			 * We'll now fall through to the below case with
294 			 * ctrl_phy = false (so we won't turn off/on).  The
295 			 * sdhci_set_clock() will set the real clock.
296 			 */
297 		} else if (clock > PHY_CLK_TOO_SLOW_HZ) {
298 			/*
299 			 * At higher clock speeds the PHY is fine being power
300 			 * cycled and docs say you _should_ power cycle when
301 			 * changing clock speeds.
302 			 */
303 			ctrl_phy = true;
304 		}
305 	}
306 
307 	if (ctrl_phy && sdhci_arasan->is_phy_on) {
308 		phy_power_off(sdhci_arasan->phy);
309 		sdhci_arasan->is_phy_on = false;
310 	}
311 
312 	if (sdhci_arasan->quirks & SDHCI_ARASAN_QUIRK_CLOCK_25_BROKEN) {
313 		/*
314 		 * Some of the Arasan variations might not have timing
315 		 * requirements met at 25MHz for Default Speed mode,
316 		 * those controllers work at 19MHz instead.
317 		 */
318 		if (clock == DEFAULT_SPEED_MAX_DTR)
319 			clock = (DEFAULT_SPEED_MAX_DTR * 19) / 25;
320 	}
321 
322 	/* Set the Input and Output Clock Phase Delays */
323 	if (clk_data->set_clk_delays)
324 		clk_data->set_clk_delays(host);
325 
326 	sdhci_set_clock(host, clock);
327 
328 	if (sdhci_arasan->quirks & SDHCI_ARASAN_QUIRK_CLOCK_UNSTABLE)
329 		/*
330 		 * Some controllers immediately report SDHCI_CLOCK_INT_STABLE
331 		 * after enabling the clock even though the clock is not
332 		 * stable. Trying to use a clock without waiting here results
333 		 * in EILSEQ while detecting some older/slower cards. The
334 		 * chosen delay is the maximum delay from sdhci_set_clock.
335 		 */
336 		msleep(20);
337 
338 	if (ctrl_phy) {
339 		if (phy_power_on(sdhci_arasan->phy)) {
340 			pr_err("%s: Cannot power on phy.\n",
341 			       mmc_hostname(host->mmc));
342 			return;
343 		}
344 
345 		sdhci_arasan->is_phy_on = true;
346 	}
347 }
348 
349 static void sdhci_arasan_hs400_enhanced_strobe(struct mmc_host *mmc,
350 					struct mmc_ios *ios)
351 {
352 	u32 vendor;
353 	struct sdhci_host *host = mmc_priv(mmc);
354 
355 	vendor = sdhci_readl(host, SDHCI_ARASAN_VENDOR_REGISTER);
356 	if (ios->enhanced_strobe)
357 		vendor |= VENDOR_ENHANCED_STROBE;
358 	else
359 		vendor &= ~VENDOR_ENHANCED_STROBE;
360 
361 	sdhci_writel(host, vendor, SDHCI_ARASAN_VENDOR_REGISTER);
362 }
363 
364 static void sdhci_arasan_reset(struct sdhci_host *host, u8 mask)
365 {
366 	u8 ctrl;
367 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
368 	struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
369 
370 	sdhci_and_cqhci_reset(host, mask);
371 
372 	if (sdhci_arasan->quirks & SDHCI_ARASAN_QUIRK_FORCE_CDTEST) {
373 		ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
374 		ctrl |= SDHCI_CTRL_CDTEST_INS | SDHCI_CTRL_CDTEST_EN;
375 		sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
376 	}
377 }
378 
379 static int sdhci_arasan_voltage_switch(struct mmc_host *mmc,
380 				       struct mmc_ios *ios)
381 {
382 	switch (ios->signal_voltage) {
383 	case MMC_SIGNAL_VOLTAGE_180:
384 		/*
385 		 * Plese don't switch to 1V8 as arasan,5.1 doesn't
386 		 * actually refer to this setting to indicate the
387 		 * signal voltage and the state machine will be broken
388 		 * actually if we force to enable 1V8. That's something
389 		 * like broken quirk but we could work around here.
390 		 */
391 		return 0;
392 	case MMC_SIGNAL_VOLTAGE_330:
393 	case MMC_SIGNAL_VOLTAGE_120:
394 		/* We don't support 3V3 and 1V2 */
395 		break;
396 	}
397 
398 	return -EINVAL;
399 }
400 
401 static const struct sdhci_ops sdhci_arasan_ops = {
402 	.set_clock = sdhci_arasan_set_clock,
403 	.get_max_clock = sdhci_pltfm_clk_get_max_clock,
404 	.get_timeout_clock = sdhci_pltfm_clk_get_max_clock,
405 	.set_bus_width = sdhci_set_bus_width,
406 	.reset = sdhci_arasan_reset,
407 	.set_uhs_signaling = sdhci_set_uhs_signaling,
408 	.set_power = sdhci_set_power_and_bus_voltage,
409 };
410 
411 static u32 sdhci_arasan_cqhci_irq(struct sdhci_host *host, u32 intmask)
412 {
413 	int cmd_error = 0;
414 	int data_error = 0;
415 
416 	if (!sdhci_cqe_irq(host, intmask, &cmd_error, &data_error))
417 		return intmask;
418 
419 	cqhci_irq(host->mmc, intmask, cmd_error, data_error);
420 
421 	return 0;
422 }
423 
424 static void sdhci_arasan_dumpregs(struct mmc_host *mmc)
425 {
426 	sdhci_dumpregs(mmc_priv(mmc));
427 }
428 
429 static void sdhci_arasan_cqe_enable(struct mmc_host *mmc)
430 {
431 	struct sdhci_host *host = mmc_priv(mmc);
432 	u32 reg;
433 
434 	reg = sdhci_readl(host, SDHCI_PRESENT_STATE);
435 	while (reg & SDHCI_DATA_AVAILABLE) {
436 		sdhci_readl(host, SDHCI_BUFFER);
437 		reg = sdhci_readl(host, SDHCI_PRESENT_STATE);
438 	}
439 
440 	sdhci_cqe_enable(mmc);
441 }
442 
443 static const struct cqhci_host_ops sdhci_arasan_cqhci_ops = {
444 	.enable         = sdhci_arasan_cqe_enable,
445 	.disable        = sdhci_cqe_disable,
446 	.dumpregs       = sdhci_arasan_dumpregs,
447 };
448 
449 static const struct sdhci_ops sdhci_arasan_cqe_ops = {
450 	.set_clock = sdhci_arasan_set_clock,
451 	.get_max_clock = sdhci_pltfm_clk_get_max_clock,
452 	.get_timeout_clock = sdhci_pltfm_clk_get_max_clock,
453 	.set_bus_width = sdhci_set_bus_width,
454 	.reset = sdhci_arasan_reset,
455 	.set_uhs_signaling = sdhci_set_uhs_signaling,
456 	.set_power = sdhci_set_power_and_bus_voltage,
457 	.irq = sdhci_arasan_cqhci_irq,
458 };
459 
460 static const struct sdhci_pltfm_data sdhci_arasan_cqe_pdata = {
461 	.ops = &sdhci_arasan_cqe_ops,
462 	.quirks = SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN,
463 	.quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN |
464 			SDHCI_QUIRK2_CLOCK_DIV_ZERO_BROKEN,
465 };
466 
467 static const struct sdhci_pltfm_data sdhci_arasan_thunderbay_pdata = {
468 	.ops = &sdhci_arasan_cqe_ops,
469 	.quirks = SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN | SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC,
470 	.quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN |
471 		SDHCI_QUIRK2_CLOCK_DIV_ZERO_BROKEN |
472 		SDHCI_QUIRK2_STOP_WITH_TC |
473 		SDHCI_QUIRK2_CAPS_BIT63_FOR_HS400,
474 };
475 
476 #ifdef CONFIG_PM_SLEEP
477 /**
478  * sdhci_arasan_suspend - Suspend method for the driver
479  * @dev:	Address of the device structure
480  *
481  * Put the device in a low power state.
482  *
483  * Return: 0 on success and error value on error
484  */
485 static int sdhci_arasan_suspend(struct device *dev)
486 {
487 	struct sdhci_host *host = dev_get_drvdata(dev);
488 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
489 	struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
490 	int ret;
491 
492 	if (host->tuning_mode != SDHCI_TUNING_MODE_3)
493 		mmc_retune_needed(host->mmc);
494 
495 	if (sdhci_arasan->has_cqe) {
496 		ret = cqhci_suspend(host->mmc);
497 		if (ret)
498 			return ret;
499 	}
500 
501 	ret = sdhci_suspend_host(host);
502 	if (ret)
503 		return ret;
504 
505 	if (!IS_ERR(sdhci_arasan->phy) && sdhci_arasan->is_phy_on) {
506 		ret = phy_power_off(sdhci_arasan->phy);
507 		if (ret) {
508 			dev_err(dev, "Cannot power off phy.\n");
509 			if (sdhci_resume_host(host))
510 				dev_err(dev, "Cannot resume host.\n");
511 
512 			return ret;
513 		}
514 		sdhci_arasan->is_phy_on = false;
515 	}
516 
517 	clk_disable(pltfm_host->clk);
518 	clk_disable(sdhci_arasan->clk_ahb);
519 
520 	return 0;
521 }
522 
523 /**
524  * sdhci_arasan_resume - Resume method for the driver
525  * @dev:	Address of the device structure
526  *
527  * Resume operation after suspend
528  *
529  * Return: 0 on success and error value on error
530  */
531 static int sdhci_arasan_resume(struct device *dev)
532 {
533 	struct sdhci_host *host = dev_get_drvdata(dev);
534 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
535 	struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
536 	int ret;
537 
538 	ret = clk_enable(sdhci_arasan->clk_ahb);
539 	if (ret) {
540 		dev_err(dev, "Cannot enable AHB clock.\n");
541 		return ret;
542 	}
543 
544 	ret = clk_enable(pltfm_host->clk);
545 	if (ret) {
546 		dev_err(dev, "Cannot enable SD clock.\n");
547 		return ret;
548 	}
549 
550 	if (!IS_ERR(sdhci_arasan->phy) && host->mmc->actual_clock) {
551 		ret = phy_power_on(sdhci_arasan->phy);
552 		if (ret) {
553 			dev_err(dev, "Cannot power on phy.\n");
554 			return ret;
555 		}
556 		sdhci_arasan->is_phy_on = true;
557 	}
558 
559 	ret = sdhci_resume_host(host);
560 	if (ret) {
561 		dev_err(dev, "Cannot resume host.\n");
562 		return ret;
563 	}
564 
565 	if (sdhci_arasan->has_cqe)
566 		return cqhci_resume(host->mmc);
567 
568 	return 0;
569 }
570 #endif /* ! CONFIG_PM_SLEEP */
571 
572 static SIMPLE_DEV_PM_OPS(sdhci_arasan_dev_pm_ops, sdhci_arasan_suspend,
573 			 sdhci_arasan_resume);
574 
575 /**
576  * sdhci_arasan_sdcardclk_recalc_rate - Return the card clock rate
577  *
578  * @hw:			Pointer to the hardware clock structure.
579  * @parent_rate:		The parent rate (should be rate of clk_xin).
580  *
581  * Return the current actual rate of the SD card clock.  This can be used
582  * to communicate with out PHY.
583  *
584  * Return: The card clock rate.
585  */
586 static unsigned long sdhci_arasan_sdcardclk_recalc_rate(struct clk_hw *hw,
587 						      unsigned long parent_rate)
588 {
589 	struct sdhci_arasan_clk_data *clk_data =
590 		container_of(hw, struct sdhci_arasan_clk_data, sdcardclk_hw);
591 	struct sdhci_arasan_data *sdhci_arasan =
592 		container_of(clk_data, struct sdhci_arasan_data, clk_data);
593 	struct sdhci_host *host = sdhci_arasan->host;
594 
595 	return host->mmc->actual_clock;
596 }
597 
598 static const struct clk_ops arasan_sdcardclk_ops = {
599 	.recalc_rate = sdhci_arasan_sdcardclk_recalc_rate,
600 };
601 
602 /**
603  * sdhci_arasan_sampleclk_recalc_rate - Return the sampling clock rate
604  *
605  * @hw:			Pointer to the hardware clock structure.
606  * @parent_rate:		The parent rate (should be rate of clk_xin).
607  *
608  * Return the current actual rate of the sampling clock.  This can be used
609  * to communicate with out PHY.
610  *
611  * Return: The sample clock rate.
612  */
613 static unsigned long sdhci_arasan_sampleclk_recalc_rate(struct clk_hw *hw,
614 						      unsigned long parent_rate)
615 {
616 	struct sdhci_arasan_clk_data *clk_data =
617 		container_of(hw, struct sdhci_arasan_clk_data, sampleclk_hw);
618 	struct sdhci_arasan_data *sdhci_arasan =
619 		container_of(clk_data, struct sdhci_arasan_data, clk_data);
620 	struct sdhci_host *host = sdhci_arasan->host;
621 
622 	return host->mmc->actual_clock;
623 }
624 
625 static const struct clk_ops arasan_sampleclk_ops = {
626 	.recalc_rate = sdhci_arasan_sampleclk_recalc_rate,
627 };
628 
629 /**
630  * sdhci_zynqmp_sdcardclk_set_phase - Set the SD Output Clock Tap Delays
631  *
632  * @hw:			Pointer to the hardware clock structure.
633  * @degrees:		The clock phase shift between 0 - 359.
634  *
635  * Set the SD Output Clock Tap Delays for Output path
636  *
637  * Return: 0 on success and error value on error
638  */
639 static int sdhci_zynqmp_sdcardclk_set_phase(struct clk_hw *hw, int degrees)
640 {
641 	struct sdhci_arasan_clk_data *clk_data =
642 		container_of(hw, struct sdhci_arasan_clk_data, sdcardclk_hw);
643 	struct sdhci_arasan_data *sdhci_arasan =
644 		container_of(clk_data, struct sdhci_arasan_data, clk_data);
645 	struct sdhci_host *host = sdhci_arasan->host;
646 	const char *clk_name = clk_hw_get_name(hw);
647 	u32 node_id = !strcmp(clk_name, "clk_out_sd0") ? NODE_SD_0 : NODE_SD_1;
648 	u8 tap_delay, tap_max = 0;
649 	int ret;
650 
651 	/* This is applicable for SDHCI_SPEC_300 and above */
652 	if (host->version < SDHCI_SPEC_300)
653 		return 0;
654 
655 	switch (host->timing) {
656 	case MMC_TIMING_MMC_HS:
657 	case MMC_TIMING_SD_HS:
658 	case MMC_TIMING_UHS_SDR25:
659 	case MMC_TIMING_UHS_DDR50:
660 	case MMC_TIMING_MMC_DDR52:
661 		/* For 50MHz clock, 30 Taps are available */
662 		tap_max = 30;
663 		break;
664 	case MMC_TIMING_UHS_SDR50:
665 		/* For 100MHz clock, 15 Taps are available */
666 		tap_max = 15;
667 		break;
668 	case MMC_TIMING_UHS_SDR104:
669 	case MMC_TIMING_MMC_HS200:
670 		/* For 200MHz clock, 8 Taps are available */
671 		tap_max = 8;
672 		break;
673 	default:
674 		break;
675 	}
676 
677 	tap_delay = (degrees * tap_max) / 360;
678 
679 	/* Set the Clock Phase */
680 	ret = zynqmp_pm_set_sd_tapdelay(node_id, PM_TAPDELAY_OUTPUT, tap_delay);
681 	if (ret)
682 		pr_err("Error setting Output Tap Delay\n");
683 
684 	/* Release DLL Reset */
685 	zynqmp_pm_sd_dll_reset(node_id, PM_DLL_RESET_RELEASE);
686 
687 	return ret;
688 }
689 
690 static const struct clk_ops zynqmp_sdcardclk_ops = {
691 	.recalc_rate = sdhci_arasan_sdcardclk_recalc_rate,
692 	.set_phase = sdhci_zynqmp_sdcardclk_set_phase,
693 };
694 
695 /**
696  * sdhci_zynqmp_sampleclk_set_phase - Set the SD Input Clock Tap Delays
697  *
698  * @hw:			Pointer to the hardware clock structure.
699  * @degrees:		The clock phase shift between 0 - 359.
700  *
701  * Set the SD Input Clock Tap Delays for Input path
702  *
703  * Return: 0 on success and error value on error
704  */
705 static int sdhci_zynqmp_sampleclk_set_phase(struct clk_hw *hw, int degrees)
706 {
707 	struct sdhci_arasan_clk_data *clk_data =
708 		container_of(hw, struct sdhci_arasan_clk_data, sampleclk_hw);
709 	struct sdhci_arasan_data *sdhci_arasan =
710 		container_of(clk_data, struct sdhci_arasan_data, clk_data);
711 	struct sdhci_host *host = sdhci_arasan->host;
712 	const char *clk_name = clk_hw_get_name(hw);
713 	u32 node_id = !strcmp(clk_name, "clk_in_sd0") ? NODE_SD_0 : NODE_SD_1;
714 	u8 tap_delay, tap_max = 0;
715 	int ret;
716 
717 	/* This is applicable for SDHCI_SPEC_300 and above */
718 	if (host->version < SDHCI_SPEC_300)
719 		return 0;
720 
721 	/* Assert DLL Reset */
722 	zynqmp_pm_sd_dll_reset(node_id, PM_DLL_RESET_ASSERT);
723 
724 	switch (host->timing) {
725 	case MMC_TIMING_MMC_HS:
726 	case MMC_TIMING_SD_HS:
727 	case MMC_TIMING_UHS_SDR25:
728 	case MMC_TIMING_UHS_DDR50:
729 	case MMC_TIMING_MMC_DDR52:
730 		/* For 50MHz clock, 120 Taps are available */
731 		tap_max = 120;
732 		break;
733 	case MMC_TIMING_UHS_SDR50:
734 		/* For 100MHz clock, 60 Taps are available */
735 		tap_max = 60;
736 		break;
737 	case MMC_TIMING_UHS_SDR104:
738 	case MMC_TIMING_MMC_HS200:
739 		/* For 200MHz clock, 30 Taps are available */
740 		tap_max = 30;
741 		break;
742 	default:
743 		break;
744 	}
745 
746 	tap_delay = (degrees * tap_max) / 360;
747 
748 	/* Set the Clock Phase */
749 	ret = zynqmp_pm_set_sd_tapdelay(node_id, PM_TAPDELAY_INPUT, tap_delay);
750 	if (ret)
751 		pr_err("Error setting Input Tap Delay\n");
752 
753 	return ret;
754 }
755 
756 static const struct clk_ops zynqmp_sampleclk_ops = {
757 	.recalc_rate = sdhci_arasan_sampleclk_recalc_rate,
758 	.set_phase = sdhci_zynqmp_sampleclk_set_phase,
759 };
760 
761 /**
762  * sdhci_versal_sdcardclk_set_phase - Set the SD Output Clock Tap Delays
763  *
764  * @hw:			Pointer to the hardware clock structure.
765  * @degrees:		The clock phase shift between 0 - 359.
766  *
767  * Set the SD Output Clock Tap Delays for Output path
768  *
769  * Return: 0 on success and error value on error
770  */
771 static int sdhci_versal_sdcardclk_set_phase(struct clk_hw *hw, int degrees)
772 {
773 	struct sdhci_arasan_clk_data *clk_data =
774 		container_of(hw, struct sdhci_arasan_clk_data, sdcardclk_hw);
775 	struct sdhci_arasan_data *sdhci_arasan =
776 		container_of(clk_data, struct sdhci_arasan_data, clk_data);
777 	struct sdhci_host *host = sdhci_arasan->host;
778 	u8 tap_delay, tap_max = 0;
779 
780 	/* This is applicable for SDHCI_SPEC_300 and above */
781 	if (host->version < SDHCI_SPEC_300)
782 		return 0;
783 
784 	switch (host->timing) {
785 	case MMC_TIMING_MMC_HS:
786 	case MMC_TIMING_SD_HS:
787 	case MMC_TIMING_UHS_SDR25:
788 	case MMC_TIMING_UHS_DDR50:
789 	case MMC_TIMING_MMC_DDR52:
790 		/* For 50MHz clock, 30 Taps are available */
791 		tap_max = 30;
792 		break;
793 	case MMC_TIMING_UHS_SDR50:
794 		/* For 100MHz clock, 15 Taps are available */
795 		tap_max = 15;
796 		break;
797 	case MMC_TIMING_UHS_SDR104:
798 	case MMC_TIMING_MMC_HS200:
799 		/* For 200MHz clock, 8 Taps are available */
800 		tap_max = 8;
801 		break;
802 	default:
803 		break;
804 	}
805 
806 	tap_delay = (degrees * tap_max) / 360;
807 
808 	/* Set the Clock Phase */
809 	if (tap_delay) {
810 		u32 regval;
811 
812 		regval = sdhci_readl(host, SDHCI_ARASAN_OTAPDLY_REGISTER);
813 		regval |= SDHCI_OTAPDLY_ENABLE;
814 		sdhci_writel(host, regval, SDHCI_ARASAN_OTAPDLY_REGISTER);
815 		regval &= ~SDHCI_ARASAN_OTAPDLY_SEL_MASK;
816 		regval |= tap_delay;
817 		sdhci_writel(host, regval, SDHCI_ARASAN_OTAPDLY_REGISTER);
818 	}
819 
820 	return 0;
821 }
822 
823 static const struct clk_ops versal_sdcardclk_ops = {
824 	.recalc_rate = sdhci_arasan_sdcardclk_recalc_rate,
825 	.set_phase = sdhci_versal_sdcardclk_set_phase,
826 };
827 
828 /**
829  * sdhci_versal_sampleclk_set_phase - Set the SD Input Clock Tap Delays
830  *
831  * @hw:			Pointer to the hardware clock structure.
832  * @degrees:		The clock phase shift between 0 - 359.
833  *
834  * Set the SD Input Clock Tap Delays for Input path
835  *
836  * Return: 0 on success and error value on error
837  */
838 static int sdhci_versal_sampleclk_set_phase(struct clk_hw *hw, int degrees)
839 {
840 	struct sdhci_arasan_clk_data *clk_data =
841 		container_of(hw, struct sdhci_arasan_clk_data, sampleclk_hw);
842 	struct sdhci_arasan_data *sdhci_arasan =
843 		container_of(clk_data, struct sdhci_arasan_data, clk_data);
844 	struct sdhci_host *host = sdhci_arasan->host;
845 	u8 tap_delay, tap_max = 0;
846 
847 	/* This is applicable for SDHCI_SPEC_300 and above */
848 	if (host->version < SDHCI_SPEC_300)
849 		return 0;
850 
851 	switch (host->timing) {
852 	case MMC_TIMING_MMC_HS:
853 	case MMC_TIMING_SD_HS:
854 	case MMC_TIMING_UHS_SDR25:
855 	case MMC_TIMING_UHS_DDR50:
856 	case MMC_TIMING_MMC_DDR52:
857 		/* For 50MHz clock, 120 Taps are available */
858 		tap_max = 120;
859 		break;
860 	case MMC_TIMING_UHS_SDR50:
861 		/* For 100MHz clock, 60 Taps are available */
862 		tap_max = 60;
863 		break;
864 	case MMC_TIMING_UHS_SDR104:
865 	case MMC_TIMING_MMC_HS200:
866 		/* For 200MHz clock, 30 Taps are available */
867 		tap_max = 30;
868 		break;
869 	default:
870 		break;
871 	}
872 
873 	tap_delay = (degrees * tap_max) / 360;
874 
875 	/* Set the Clock Phase */
876 	if (tap_delay) {
877 		u32 regval;
878 
879 		regval = sdhci_readl(host, SDHCI_ARASAN_ITAPDLY_REGISTER);
880 		regval |= SDHCI_ITAPDLY_CHGWIN;
881 		sdhci_writel(host, regval, SDHCI_ARASAN_ITAPDLY_REGISTER);
882 		regval |= SDHCI_ITAPDLY_ENABLE;
883 		sdhci_writel(host, regval, SDHCI_ARASAN_ITAPDLY_REGISTER);
884 		regval &= ~SDHCI_ARASAN_ITAPDLY_SEL_MASK;
885 		regval |= tap_delay;
886 		sdhci_writel(host, regval, SDHCI_ARASAN_ITAPDLY_REGISTER);
887 		regval &= ~SDHCI_ITAPDLY_CHGWIN;
888 		sdhci_writel(host, regval, SDHCI_ARASAN_ITAPDLY_REGISTER);
889 	}
890 
891 	return 0;
892 }
893 
894 static const struct clk_ops versal_sampleclk_ops = {
895 	.recalc_rate = sdhci_arasan_sampleclk_recalc_rate,
896 	.set_phase = sdhci_versal_sampleclk_set_phase,
897 };
898 
899 static void arasan_zynqmp_dll_reset(struct sdhci_host *host, u32 deviceid)
900 {
901 	u16 clk;
902 
903 	clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
904 	clk &= ~(SDHCI_CLOCK_CARD_EN | SDHCI_CLOCK_INT_EN);
905 	sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
906 
907 	/* Issue DLL Reset */
908 	zynqmp_pm_sd_dll_reset(deviceid, PM_DLL_RESET_PULSE);
909 
910 	clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
911 
912 	sdhci_enable_clk(host, clk);
913 }
914 
915 static int arasan_zynqmp_execute_tuning(struct mmc_host *mmc, u32 opcode)
916 {
917 	struct sdhci_host *host = mmc_priv(mmc);
918 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
919 	struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
920 	struct clk_hw *hw = &sdhci_arasan->clk_data.sdcardclk_hw;
921 	const char *clk_name = clk_hw_get_name(hw);
922 	u32 device_id = !strcmp(clk_name, "clk_out_sd0") ? NODE_SD_0 :
923 							   NODE_SD_1;
924 	int err;
925 
926 	/* ZynqMP SD controller does not perform auto tuning in DDR50 mode */
927 	if (mmc->ios.timing == MMC_TIMING_UHS_DDR50)
928 		return 0;
929 
930 	arasan_zynqmp_dll_reset(host, device_id);
931 
932 	err = sdhci_execute_tuning(mmc, opcode);
933 	if (err)
934 		return err;
935 
936 	arasan_zynqmp_dll_reset(host, device_id);
937 
938 	return 0;
939 }
940 
941 /**
942  * sdhci_arasan_update_clockmultiplier - Set corecfg_clockmultiplier
943  *
944  * @host:		The sdhci_host
945  * @value:		The value to write
946  *
947  * The corecfg_clockmultiplier is supposed to contain clock multiplier
948  * value of programmable clock generator.
949  *
950  * NOTES:
951  * - Many existing devices don't seem to do this and work fine.  To keep
952  *   compatibility for old hardware where the device tree doesn't provide a
953  *   register map, this function is a noop if a soc_ctl_map hasn't been provided
954  *   for this platform.
955  * - The value of corecfg_clockmultiplier should sync with that of corresponding
956  *   value reading from sdhci_capability_register. So this function is called
957  *   once at probe time and never called again.
958  */
959 static void sdhci_arasan_update_clockmultiplier(struct sdhci_host *host,
960 						u32 value)
961 {
962 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
963 	struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
964 	const struct sdhci_arasan_soc_ctl_map *soc_ctl_map =
965 		sdhci_arasan->soc_ctl_map;
966 
967 	/* Having a map is optional */
968 	if (!soc_ctl_map)
969 		return;
970 
971 	/* If we have a map, we expect to have a syscon */
972 	if (!sdhci_arasan->soc_ctl_base) {
973 		pr_warn("%s: Have regmap, but no soc-ctl-syscon\n",
974 			mmc_hostname(host->mmc));
975 		return;
976 	}
977 
978 	sdhci_arasan_syscon_write(host, &soc_ctl_map->clockmultiplier, value);
979 }
980 
981 /**
982  * sdhci_arasan_update_baseclkfreq - Set corecfg_baseclkfreq
983  *
984  * @host:		The sdhci_host
985  *
986  * The corecfg_baseclkfreq is supposed to contain the MHz of clk_xin.  This
987  * function can be used to make that happen.
988  *
989  * NOTES:
990  * - Many existing devices don't seem to do this and work fine.  To keep
991  *   compatibility for old hardware where the device tree doesn't provide a
992  *   register map, this function is a noop if a soc_ctl_map hasn't been provided
993  *   for this platform.
994  * - It's assumed that clk_xin is not dynamic and that we use the SDHCI divider
995  *   to achieve lower clock rates.  That means that this function is called once
996  *   at probe time and never called again.
997  */
998 static void sdhci_arasan_update_baseclkfreq(struct sdhci_host *host)
999 {
1000 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1001 	struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
1002 	const struct sdhci_arasan_soc_ctl_map *soc_ctl_map =
1003 		sdhci_arasan->soc_ctl_map;
1004 	u32 mhz = DIV_ROUND_CLOSEST_ULL(clk_get_rate(pltfm_host->clk), 1000000);
1005 
1006 	/* Having a map is optional */
1007 	if (!soc_ctl_map)
1008 		return;
1009 
1010 	/* If we have a map, we expect to have a syscon */
1011 	if (!sdhci_arasan->soc_ctl_base) {
1012 		pr_warn("%s: Have regmap, but no soc-ctl-syscon\n",
1013 			mmc_hostname(host->mmc));
1014 		return;
1015 	}
1016 
1017 	sdhci_arasan_syscon_write(host, &soc_ctl_map->baseclkfreq, mhz);
1018 }
1019 
1020 static void sdhci_arasan_set_clk_delays(struct sdhci_host *host)
1021 {
1022 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1023 	struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
1024 	struct sdhci_arasan_clk_data *clk_data = &sdhci_arasan->clk_data;
1025 
1026 	clk_set_phase(clk_data->sampleclk,
1027 		      clk_data->clk_phase_in[host->timing]);
1028 	clk_set_phase(clk_data->sdcardclk,
1029 		      clk_data->clk_phase_out[host->timing]);
1030 }
1031 
1032 static void arasan_dt_read_clk_phase(struct device *dev,
1033 				     struct sdhci_arasan_clk_data *clk_data,
1034 				     unsigned int timing, const char *prop)
1035 {
1036 	struct device_node *np = dev->of_node;
1037 
1038 	u32 clk_phase[2] = {0};
1039 	int ret;
1040 
1041 	/*
1042 	 * Read Tap Delay values from DT, if the DT does not contain the
1043 	 * Tap Values then use the pre-defined values.
1044 	 */
1045 	ret = of_property_read_variable_u32_array(np, prop, &clk_phase[0],
1046 						  2, 0);
1047 	if (ret < 0) {
1048 		dev_dbg(dev, "Using predefined clock phase for %s = %d %d\n",
1049 			prop, clk_data->clk_phase_in[timing],
1050 			clk_data->clk_phase_out[timing]);
1051 		return;
1052 	}
1053 
1054 	/* The values read are Input and Output Clock Delays in order */
1055 	clk_data->clk_phase_in[timing] = clk_phase[0];
1056 	clk_data->clk_phase_out[timing] = clk_phase[1];
1057 }
1058 
1059 /**
1060  * arasan_dt_parse_clk_phases - Read Clock Delay values from DT
1061  *
1062  * @dev:		Pointer to our struct device.
1063  * @clk_data:		Pointer to the Clock Data structure
1064  *
1065  * Called at initialization to parse the values of Clock Delays.
1066  */
1067 static void arasan_dt_parse_clk_phases(struct device *dev,
1068 				       struct sdhci_arasan_clk_data *clk_data)
1069 {
1070 	u32 mio_bank = 0;
1071 	int i;
1072 
1073 	/*
1074 	 * This has been kept as a pointer and is assigned a function here.
1075 	 * So that different controller variants can assign their own handling
1076 	 * function.
1077 	 */
1078 	clk_data->set_clk_delays = sdhci_arasan_set_clk_delays;
1079 
1080 	if (of_device_is_compatible(dev->of_node, "xlnx,zynqmp-8.9a")) {
1081 		u32 zynqmp_iclk_phase[MMC_TIMING_MMC_HS400 + 1] =
1082 			ZYNQMP_ICLK_PHASE;
1083 		u32 zynqmp_oclk_phase[MMC_TIMING_MMC_HS400 + 1] =
1084 			ZYNQMP_OCLK_PHASE;
1085 
1086 		of_property_read_u32(dev->of_node, "xlnx,mio-bank", &mio_bank);
1087 		if (mio_bank == 2) {
1088 			zynqmp_oclk_phase[MMC_TIMING_UHS_SDR104] = 90;
1089 			zynqmp_oclk_phase[MMC_TIMING_MMC_HS200] = 90;
1090 		}
1091 
1092 		for (i = 0; i <= MMC_TIMING_MMC_HS400; i++) {
1093 			clk_data->clk_phase_in[i] = zynqmp_iclk_phase[i];
1094 			clk_data->clk_phase_out[i] = zynqmp_oclk_phase[i];
1095 		}
1096 	}
1097 
1098 	if (of_device_is_compatible(dev->of_node, "xlnx,versal-8.9a")) {
1099 		u32 versal_iclk_phase[MMC_TIMING_MMC_HS400 + 1] =
1100 			VERSAL_ICLK_PHASE;
1101 		u32 versal_oclk_phase[MMC_TIMING_MMC_HS400 + 1] =
1102 			VERSAL_OCLK_PHASE;
1103 
1104 		for (i = 0; i <= MMC_TIMING_MMC_HS400; i++) {
1105 			clk_data->clk_phase_in[i] = versal_iclk_phase[i];
1106 			clk_data->clk_phase_out[i] = versal_oclk_phase[i];
1107 		}
1108 	}
1109 
1110 	arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_LEGACY,
1111 				 "clk-phase-legacy");
1112 	arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_MMC_HS,
1113 				 "clk-phase-mmc-hs");
1114 	arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_SD_HS,
1115 				 "clk-phase-sd-hs");
1116 	arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_UHS_SDR12,
1117 				 "clk-phase-uhs-sdr12");
1118 	arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_UHS_SDR25,
1119 				 "clk-phase-uhs-sdr25");
1120 	arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_UHS_SDR50,
1121 				 "clk-phase-uhs-sdr50");
1122 	arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_UHS_SDR104,
1123 				 "clk-phase-uhs-sdr104");
1124 	arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_UHS_DDR50,
1125 				 "clk-phase-uhs-ddr50");
1126 	arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_MMC_DDR52,
1127 				 "clk-phase-mmc-ddr52");
1128 	arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_MMC_HS200,
1129 				 "clk-phase-mmc-hs200");
1130 	arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_MMC_HS400,
1131 				 "clk-phase-mmc-hs400");
1132 }
1133 
1134 static const struct sdhci_pltfm_data sdhci_arasan_pdata = {
1135 	.ops = &sdhci_arasan_ops,
1136 	.quirks = SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN,
1137 	.quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN |
1138 			SDHCI_QUIRK2_CLOCK_DIV_ZERO_BROKEN |
1139 			SDHCI_QUIRK2_STOP_WITH_TC,
1140 };
1141 
1142 static const struct sdhci_arasan_clk_ops arasan_clk_ops = {
1143 	.sdcardclk_ops = &arasan_sdcardclk_ops,
1144 	.sampleclk_ops = &arasan_sampleclk_ops,
1145 };
1146 
1147 static struct sdhci_arasan_of_data sdhci_arasan_generic_data = {
1148 	.pdata = &sdhci_arasan_pdata,
1149 	.clk_ops = &arasan_clk_ops,
1150 };
1151 
1152 static const struct sdhci_arasan_of_data sdhci_arasan_thunderbay_data = {
1153 	.soc_ctl_map = &thunderbay_soc_ctl_map,
1154 	.pdata = &sdhci_arasan_thunderbay_pdata,
1155 	.clk_ops = &arasan_clk_ops,
1156 };
1157 
1158 static const struct sdhci_pltfm_data sdhci_keembay_emmc_pdata = {
1159 	.ops = &sdhci_arasan_cqe_ops,
1160 	.quirks = SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN |
1161 		SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC |
1162 		SDHCI_QUIRK_NO_LED |
1163 		SDHCI_QUIRK_32BIT_DMA_ADDR |
1164 		SDHCI_QUIRK_32BIT_DMA_SIZE |
1165 		SDHCI_QUIRK_32BIT_ADMA_SIZE,
1166 	.quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN |
1167 		SDHCI_QUIRK2_CLOCK_DIV_ZERO_BROKEN |
1168 		SDHCI_QUIRK2_CAPS_BIT63_FOR_HS400 |
1169 		SDHCI_QUIRK2_STOP_WITH_TC |
1170 		SDHCI_QUIRK2_BROKEN_64_BIT_DMA,
1171 };
1172 
1173 static const struct sdhci_pltfm_data sdhci_keembay_sd_pdata = {
1174 	.ops = &sdhci_arasan_ops,
1175 	.quirks = SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN |
1176 		SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC |
1177 		SDHCI_QUIRK_NO_LED |
1178 		SDHCI_QUIRK_32BIT_DMA_ADDR |
1179 		SDHCI_QUIRK_32BIT_DMA_SIZE |
1180 		SDHCI_QUIRK_32BIT_ADMA_SIZE,
1181 	.quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN |
1182 		SDHCI_QUIRK2_CLOCK_DIV_ZERO_BROKEN |
1183 		SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON |
1184 		SDHCI_QUIRK2_STOP_WITH_TC |
1185 		SDHCI_QUIRK2_BROKEN_64_BIT_DMA,
1186 };
1187 
1188 static const struct sdhci_pltfm_data sdhci_keembay_sdio_pdata = {
1189 	.ops = &sdhci_arasan_ops,
1190 	.quirks = SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN |
1191 		SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC |
1192 		SDHCI_QUIRK_NO_LED |
1193 		SDHCI_QUIRK_32BIT_DMA_ADDR |
1194 		SDHCI_QUIRK_32BIT_DMA_SIZE |
1195 		SDHCI_QUIRK_32BIT_ADMA_SIZE,
1196 	.quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN |
1197 		SDHCI_QUIRK2_CLOCK_DIV_ZERO_BROKEN |
1198 		SDHCI_QUIRK2_HOST_OFF_CARD_ON |
1199 		SDHCI_QUIRK2_BROKEN_64_BIT_DMA,
1200 };
1201 
1202 static struct sdhci_arasan_of_data sdhci_arasan_rk3399_data = {
1203 	.soc_ctl_map = &rk3399_soc_ctl_map,
1204 	.pdata = &sdhci_arasan_cqe_pdata,
1205 	.clk_ops = &arasan_clk_ops,
1206 };
1207 
1208 static struct sdhci_arasan_of_data intel_lgm_emmc_data = {
1209 	.soc_ctl_map = &intel_lgm_emmc_soc_ctl_map,
1210 	.pdata = &sdhci_arasan_cqe_pdata,
1211 	.clk_ops = &arasan_clk_ops,
1212 };
1213 
1214 static struct sdhci_arasan_of_data intel_lgm_sdxc_data = {
1215 	.soc_ctl_map = &intel_lgm_sdxc_soc_ctl_map,
1216 	.pdata = &sdhci_arasan_cqe_pdata,
1217 	.clk_ops = &arasan_clk_ops,
1218 };
1219 
1220 static const struct sdhci_pltfm_data sdhci_arasan_zynqmp_pdata = {
1221 	.ops = &sdhci_arasan_ops,
1222 	.quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN |
1223 			SDHCI_QUIRK2_CLOCK_DIV_ZERO_BROKEN |
1224 			SDHCI_QUIRK2_STOP_WITH_TC,
1225 };
1226 
1227 static const struct sdhci_arasan_clk_ops zynqmp_clk_ops = {
1228 	.sdcardclk_ops = &zynqmp_sdcardclk_ops,
1229 	.sampleclk_ops = &zynqmp_sampleclk_ops,
1230 };
1231 
1232 static struct sdhci_arasan_of_data sdhci_arasan_zynqmp_data = {
1233 	.pdata = &sdhci_arasan_zynqmp_pdata,
1234 	.clk_ops = &zynqmp_clk_ops,
1235 };
1236 
1237 static const struct sdhci_arasan_clk_ops versal_clk_ops = {
1238 	.sdcardclk_ops = &versal_sdcardclk_ops,
1239 	.sampleclk_ops = &versal_sampleclk_ops,
1240 };
1241 
1242 static struct sdhci_arasan_of_data sdhci_arasan_versal_data = {
1243 	.pdata = &sdhci_arasan_zynqmp_pdata,
1244 	.clk_ops = &versal_clk_ops,
1245 };
1246 
1247 static struct sdhci_arasan_of_data intel_keembay_emmc_data = {
1248 	.soc_ctl_map = &intel_keembay_soc_ctl_map,
1249 	.pdata = &sdhci_keembay_emmc_pdata,
1250 	.clk_ops = &arasan_clk_ops,
1251 };
1252 
1253 static struct sdhci_arasan_of_data intel_keembay_sd_data = {
1254 	.soc_ctl_map = &intel_keembay_soc_ctl_map,
1255 	.pdata = &sdhci_keembay_sd_pdata,
1256 	.clk_ops = &arasan_clk_ops,
1257 };
1258 
1259 static struct sdhci_arasan_of_data intel_keembay_sdio_data = {
1260 	.soc_ctl_map = &intel_keembay_soc_ctl_map,
1261 	.pdata = &sdhci_keembay_sdio_pdata,
1262 	.clk_ops = &arasan_clk_ops,
1263 };
1264 
1265 static const struct of_device_id sdhci_arasan_of_match[] = {
1266 	/* SoC-specific compatible strings w/ soc_ctl_map */
1267 	{
1268 		.compatible = "rockchip,rk3399-sdhci-5.1",
1269 		.data = &sdhci_arasan_rk3399_data,
1270 	},
1271 	{
1272 		.compatible = "intel,lgm-sdhci-5.1-emmc",
1273 		.data = &intel_lgm_emmc_data,
1274 	},
1275 	{
1276 		.compatible = "intel,lgm-sdhci-5.1-sdxc",
1277 		.data = &intel_lgm_sdxc_data,
1278 	},
1279 	{
1280 		.compatible = "intel,keembay-sdhci-5.1-emmc",
1281 		.data = &intel_keembay_emmc_data,
1282 	},
1283 	{
1284 		.compatible = "intel,keembay-sdhci-5.1-sd",
1285 		.data = &intel_keembay_sd_data,
1286 	},
1287 	{
1288 		.compatible = "intel,keembay-sdhci-5.1-sdio",
1289 		.data = &intel_keembay_sdio_data,
1290 	},
1291 	{
1292 		.compatible = "intel,thunderbay-sdhci-5.1",
1293 		.data = &sdhci_arasan_thunderbay_data,
1294 	},
1295 	/* Generic compatible below here */
1296 	{
1297 		.compatible = "arasan,sdhci-8.9a",
1298 		.data = &sdhci_arasan_generic_data,
1299 	},
1300 	{
1301 		.compatible = "arasan,sdhci-5.1",
1302 		.data = &sdhci_arasan_generic_data,
1303 	},
1304 	{
1305 		.compatible = "arasan,sdhci-4.9a",
1306 		.data = &sdhci_arasan_generic_data,
1307 	},
1308 	{
1309 		.compatible = "xlnx,zynqmp-8.9a",
1310 		.data = &sdhci_arasan_zynqmp_data,
1311 	},
1312 	{
1313 		.compatible = "xlnx,versal-8.9a",
1314 		.data = &sdhci_arasan_versal_data,
1315 	},
1316 	{ /* sentinel */ }
1317 };
1318 MODULE_DEVICE_TABLE(of, sdhci_arasan_of_match);
1319 
1320 /**
1321  * sdhci_arasan_register_sdcardclk - Register the sdcardclk for a PHY to use
1322  *
1323  * @sdhci_arasan:	Our private data structure.
1324  * @clk_xin:		Pointer to the functional clock
1325  * @dev:		Pointer to our struct device.
1326  *
1327  * Some PHY devices need to know what the actual card clock is.  In order for
1328  * them to find out, we'll provide a clock through the common clock framework
1329  * for them to query.
1330  *
1331  * Return: 0 on success and error value on error
1332  */
1333 static int
1334 sdhci_arasan_register_sdcardclk(struct sdhci_arasan_data *sdhci_arasan,
1335 				struct clk *clk_xin,
1336 				struct device *dev)
1337 {
1338 	struct sdhci_arasan_clk_data *clk_data = &sdhci_arasan->clk_data;
1339 	struct device_node *np = dev->of_node;
1340 	struct clk_init_data sdcardclk_init;
1341 	const char *parent_clk_name;
1342 	int ret;
1343 
1344 	ret = of_property_read_string_index(np, "clock-output-names", 0,
1345 					    &sdcardclk_init.name);
1346 	if (ret) {
1347 		dev_err(dev, "DT has #clock-cells but no clock-output-names\n");
1348 		return ret;
1349 	}
1350 
1351 	parent_clk_name = __clk_get_name(clk_xin);
1352 	sdcardclk_init.parent_names = &parent_clk_name;
1353 	sdcardclk_init.num_parents = 1;
1354 	sdcardclk_init.flags = CLK_GET_RATE_NOCACHE;
1355 	sdcardclk_init.ops = sdhci_arasan->clk_ops->sdcardclk_ops;
1356 
1357 	clk_data->sdcardclk_hw.init = &sdcardclk_init;
1358 	clk_data->sdcardclk =
1359 		devm_clk_register(dev, &clk_data->sdcardclk_hw);
1360 	if (IS_ERR(clk_data->sdcardclk))
1361 		return PTR_ERR(clk_data->sdcardclk);
1362 	clk_data->sdcardclk_hw.init = NULL;
1363 
1364 	ret = of_clk_add_provider(np, of_clk_src_simple_get,
1365 				  clk_data->sdcardclk);
1366 	if (ret)
1367 		dev_err(dev, "Failed to add sdcard clock provider\n");
1368 
1369 	return ret;
1370 }
1371 
1372 /**
1373  * sdhci_arasan_register_sampleclk - Register the sampleclk for a PHY to use
1374  *
1375  * @sdhci_arasan:	Our private data structure.
1376  * @clk_xin:		Pointer to the functional clock
1377  * @dev:		Pointer to our struct device.
1378  *
1379  * Some PHY devices need to know what the actual card clock is.  In order for
1380  * them to find out, we'll provide a clock through the common clock framework
1381  * for them to query.
1382  *
1383  * Return: 0 on success and error value on error
1384  */
1385 static int
1386 sdhci_arasan_register_sampleclk(struct sdhci_arasan_data *sdhci_arasan,
1387 				struct clk *clk_xin,
1388 				struct device *dev)
1389 {
1390 	struct sdhci_arasan_clk_data *clk_data = &sdhci_arasan->clk_data;
1391 	struct device_node *np = dev->of_node;
1392 	struct clk_init_data sampleclk_init;
1393 	const char *parent_clk_name;
1394 	int ret;
1395 
1396 	ret = of_property_read_string_index(np, "clock-output-names", 1,
1397 					    &sampleclk_init.name);
1398 	if (ret) {
1399 		dev_err(dev, "DT has #clock-cells but no clock-output-names\n");
1400 		return ret;
1401 	}
1402 
1403 	parent_clk_name = __clk_get_name(clk_xin);
1404 	sampleclk_init.parent_names = &parent_clk_name;
1405 	sampleclk_init.num_parents = 1;
1406 	sampleclk_init.flags = CLK_GET_RATE_NOCACHE;
1407 	sampleclk_init.ops = sdhci_arasan->clk_ops->sampleclk_ops;
1408 
1409 	clk_data->sampleclk_hw.init = &sampleclk_init;
1410 	clk_data->sampleclk =
1411 		devm_clk_register(dev, &clk_data->sampleclk_hw);
1412 	if (IS_ERR(clk_data->sampleclk))
1413 		return PTR_ERR(clk_data->sampleclk);
1414 	clk_data->sampleclk_hw.init = NULL;
1415 
1416 	ret = of_clk_add_provider(np, of_clk_src_simple_get,
1417 				  clk_data->sampleclk);
1418 	if (ret)
1419 		dev_err(dev, "Failed to add sample clock provider\n");
1420 
1421 	return ret;
1422 }
1423 
1424 /**
1425  * sdhci_arasan_unregister_sdclk - Undoes sdhci_arasan_register_sdclk()
1426  *
1427  * @dev:		Pointer to our struct device.
1428  *
1429  * Should be called any time we're exiting and sdhci_arasan_register_sdclk()
1430  * returned success.
1431  */
1432 static void sdhci_arasan_unregister_sdclk(struct device *dev)
1433 {
1434 	struct device_node *np = dev->of_node;
1435 
1436 	if (!of_find_property(np, "#clock-cells", NULL))
1437 		return;
1438 
1439 	of_clk_del_provider(dev->of_node);
1440 }
1441 
1442 /**
1443  * sdhci_arasan_update_support64b - Set SUPPORT_64B (64-bit System Bus Support)
1444  * @host:		The sdhci_host
1445  * @value:		The value to write
1446  *
1447  * This should be set based on the System Address Bus.
1448  * 0: the Core supports only 32-bit System Address Bus.
1449  * 1: the Core supports 64-bit System Address Bus.
1450  *
1451  * NOTE:
1452  * For Keem Bay, it is required to clear this bit. Its default value is 1'b1.
1453  * Keem Bay does not support 64-bit access.
1454  */
1455 static void sdhci_arasan_update_support64b(struct sdhci_host *host, u32 value)
1456 {
1457 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1458 	struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
1459 	const struct sdhci_arasan_soc_ctl_map *soc_ctl_map;
1460 
1461 	/* Having a map is optional */
1462 	soc_ctl_map = sdhci_arasan->soc_ctl_map;
1463 	if (!soc_ctl_map)
1464 		return;
1465 
1466 	/* If we have a map, we expect to have a syscon */
1467 	if (!sdhci_arasan->soc_ctl_base) {
1468 		pr_warn("%s: Have regmap, but no soc-ctl-syscon\n",
1469 			mmc_hostname(host->mmc));
1470 		return;
1471 	}
1472 
1473 	sdhci_arasan_syscon_write(host, &soc_ctl_map->support64b, value);
1474 }
1475 
1476 /**
1477  * sdhci_arasan_register_sdclk - Register the sdcardclk for a PHY to use
1478  *
1479  * @sdhci_arasan:	Our private data structure.
1480  * @clk_xin:		Pointer to the functional clock
1481  * @dev:		Pointer to our struct device.
1482  *
1483  * Some PHY devices need to know what the actual card clock is.  In order for
1484  * them to find out, we'll provide a clock through the common clock framework
1485  * for them to query.
1486  *
1487  * Note: without seriously re-architecting SDHCI's clock code and testing on
1488  * all platforms, there's no way to create a totally beautiful clock here
1489  * with all clock ops implemented.  Instead, we'll just create a clock that can
1490  * be queried and set the CLK_GET_RATE_NOCACHE attribute to tell common clock
1491  * framework that we're doing things behind its back.  This should be sufficient
1492  * to create nice clean device tree bindings and later (if needed) we can try
1493  * re-architecting SDHCI if we see some benefit to it.
1494  *
1495  * Return: 0 on success and error value on error
1496  */
1497 static int sdhci_arasan_register_sdclk(struct sdhci_arasan_data *sdhci_arasan,
1498 				       struct clk *clk_xin,
1499 				       struct device *dev)
1500 {
1501 	struct device_node *np = dev->of_node;
1502 	u32 num_clks = 0;
1503 	int ret;
1504 
1505 	/* Providing a clock to the PHY is optional; no error if missing */
1506 	if (of_property_read_u32(np, "#clock-cells", &num_clks) < 0)
1507 		return 0;
1508 
1509 	ret = sdhci_arasan_register_sdcardclk(sdhci_arasan, clk_xin, dev);
1510 	if (ret)
1511 		return ret;
1512 
1513 	if (num_clks) {
1514 		ret = sdhci_arasan_register_sampleclk(sdhci_arasan, clk_xin,
1515 						      dev);
1516 		if (ret) {
1517 			sdhci_arasan_unregister_sdclk(dev);
1518 			return ret;
1519 		}
1520 	}
1521 
1522 	return 0;
1523 }
1524 
1525 static int sdhci_arasan_add_host(struct sdhci_arasan_data *sdhci_arasan)
1526 {
1527 	struct sdhci_host *host = sdhci_arasan->host;
1528 	struct cqhci_host *cq_host;
1529 	bool dma64;
1530 	int ret;
1531 
1532 	if (!sdhci_arasan->has_cqe)
1533 		return sdhci_add_host(host);
1534 
1535 	ret = sdhci_setup_host(host);
1536 	if (ret)
1537 		return ret;
1538 
1539 	cq_host = devm_kzalloc(host->mmc->parent,
1540 			       sizeof(*cq_host), GFP_KERNEL);
1541 	if (!cq_host) {
1542 		ret = -ENOMEM;
1543 		goto cleanup;
1544 	}
1545 
1546 	cq_host->mmio = host->ioaddr + SDHCI_ARASAN_CQE_BASE_ADDR;
1547 	cq_host->ops = &sdhci_arasan_cqhci_ops;
1548 
1549 	dma64 = host->flags & SDHCI_USE_64_BIT_DMA;
1550 	if (dma64)
1551 		cq_host->caps |= CQHCI_TASK_DESC_SZ_128;
1552 
1553 	ret = cqhci_init(cq_host, host->mmc, dma64);
1554 	if (ret)
1555 		goto cleanup;
1556 
1557 	ret = __sdhci_add_host(host);
1558 	if (ret)
1559 		goto cleanup;
1560 
1561 	return 0;
1562 
1563 cleanup:
1564 	sdhci_cleanup_host(host);
1565 	return ret;
1566 }
1567 
1568 static int sdhci_arasan_probe(struct platform_device *pdev)
1569 {
1570 	int ret;
1571 	struct device_node *node;
1572 	struct clk *clk_xin;
1573 	struct sdhci_host *host;
1574 	struct sdhci_pltfm_host *pltfm_host;
1575 	struct device *dev = &pdev->dev;
1576 	struct device_node *np = dev->of_node;
1577 	struct sdhci_arasan_data *sdhci_arasan;
1578 	const struct sdhci_arasan_of_data *data;
1579 
1580 	data = of_device_get_match_data(dev);
1581 	if (!data)
1582 		return -EINVAL;
1583 
1584 	host = sdhci_pltfm_init(pdev, data->pdata, sizeof(*sdhci_arasan));
1585 
1586 	if (IS_ERR(host))
1587 		return PTR_ERR(host);
1588 
1589 	pltfm_host = sdhci_priv(host);
1590 	sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
1591 	sdhci_arasan->host = host;
1592 
1593 	sdhci_arasan->soc_ctl_map = data->soc_ctl_map;
1594 	sdhci_arasan->clk_ops = data->clk_ops;
1595 
1596 	node = of_parse_phandle(np, "arasan,soc-ctl-syscon", 0);
1597 	if (node) {
1598 		sdhci_arasan->soc_ctl_base = syscon_node_to_regmap(node);
1599 		of_node_put(node);
1600 
1601 		if (IS_ERR(sdhci_arasan->soc_ctl_base)) {
1602 			ret = dev_err_probe(dev,
1603 					    PTR_ERR(sdhci_arasan->soc_ctl_base),
1604 					    "Can't get syscon\n");
1605 			goto err_pltfm_free;
1606 		}
1607 	}
1608 
1609 	sdhci_get_of_property(pdev);
1610 
1611 	sdhci_arasan->clk_ahb = devm_clk_get(dev, "clk_ahb");
1612 	if (IS_ERR(sdhci_arasan->clk_ahb)) {
1613 		ret = dev_err_probe(dev, PTR_ERR(sdhci_arasan->clk_ahb),
1614 				    "clk_ahb clock not found.\n");
1615 		goto err_pltfm_free;
1616 	}
1617 
1618 	clk_xin = devm_clk_get(dev, "clk_xin");
1619 	if (IS_ERR(clk_xin)) {
1620 		ret = dev_err_probe(dev, PTR_ERR(clk_xin), "clk_xin clock not found.\n");
1621 		goto err_pltfm_free;
1622 	}
1623 
1624 	ret = clk_prepare_enable(sdhci_arasan->clk_ahb);
1625 	if (ret) {
1626 		dev_err(dev, "Unable to enable AHB clock.\n");
1627 		goto err_pltfm_free;
1628 	}
1629 
1630 	/* If clock-frequency property is set, use the provided value */
1631 	if (pltfm_host->clock &&
1632 	    pltfm_host->clock != clk_get_rate(clk_xin)) {
1633 		ret = clk_set_rate(clk_xin, pltfm_host->clock);
1634 		if (ret) {
1635 			dev_err(&pdev->dev, "Failed to set SD clock rate\n");
1636 			goto clk_dis_ahb;
1637 		}
1638 	}
1639 
1640 	ret = clk_prepare_enable(clk_xin);
1641 	if (ret) {
1642 		dev_err(dev, "Unable to enable SD clock.\n");
1643 		goto clk_dis_ahb;
1644 	}
1645 
1646 	if (of_property_read_bool(np, "xlnx,fails-without-test-cd"))
1647 		sdhci_arasan->quirks |= SDHCI_ARASAN_QUIRK_FORCE_CDTEST;
1648 
1649 	if (of_property_read_bool(np, "xlnx,int-clock-stable-broken"))
1650 		sdhci_arasan->quirks |= SDHCI_ARASAN_QUIRK_CLOCK_UNSTABLE;
1651 
1652 	pltfm_host->clk = clk_xin;
1653 
1654 	if (of_device_is_compatible(np, "rockchip,rk3399-sdhci-5.1"))
1655 		sdhci_arasan_update_clockmultiplier(host, 0x0);
1656 
1657 	if (of_device_is_compatible(np, "intel,keembay-sdhci-5.1-emmc") ||
1658 	    of_device_is_compatible(np, "intel,keembay-sdhci-5.1-sd") ||
1659 	    of_device_is_compatible(np, "intel,keembay-sdhci-5.1-sdio") ||
1660 	    of_device_is_compatible(np, "intel,thunderbay-sdhci-5.1")) {
1661 		sdhci_arasan_update_clockmultiplier(host, 0x0);
1662 		sdhci_arasan_update_support64b(host, 0x0);
1663 
1664 		host->mmc->caps |= MMC_CAP_WAIT_WHILE_BUSY;
1665 	}
1666 
1667 	sdhci_arasan_update_baseclkfreq(host);
1668 
1669 	ret = sdhci_arasan_register_sdclk(sdhci_arasan, clk_xin, dev);
1670 	if (ret)
1671 		goto clk_disable_all;
1672 
1673 	if (of_device_is_compatible(np, "xlnx,zynqmp-8.9a")) {
1674 		host->mmc_host_ops.execute_tuning =
1675 			arasan_zynqmp_execute_tuning;
1676 
1677 		sdhci_arasan->quirks |= SDHCI_ARASAN_QUIRK_CLOCK_25_BROKEN;
1678 		host->quirks |= SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12;
1679 	}
1680 
1681 	arasan_dt_parse_clk_phases(dev, &sdhci_arasan->clk_data);
1682 
1683 	ret = mmc_of_parse(host->mmc);
1684 	if (ret) {
1685 		ret = dev_err_probe(dev, ret, "parsing dt failed.\n");
1686 		goto unreg_clk;
1687 	}
1688 
1689 	sdhci_arasan->phy = ERR_PTR(-ENODEV);
1690 	if (of_device_is_compatible(np, "arasan,sdhci-5.1")) {
1691 		sdhci_arasan->phy = devm_phy_get(dev, "phy_arasan");
1692 		if (IS_ERR(sdhci_arasan->phy)) {
1693 			ret = dev_err_probe(dev, PTR_ERR(sdhci_arasan->phy),
1694 					    "No phy for arasan,sdhci-5.1.\n");
1695 			goto unreg_clk;
1696 		}
1697 
1698 		ret = phy_init(sdhci_arasan->phy);
1699 		if (ret < 0) {
1700 			dev_err(dev, "phy_init err.\n");
1701 			goto unreg_clk;
1702 		}
1703 
1704 		host->mmc_host_ops.hs400_enhanced_strobe =
1705 					sdhci_arasan_hs400_enhanced_strobe;
1706 		host->mmc_host_ops.start_signal_voltage_switch =
1707 					sdhci_arasan_voltage_switch;
1708 		sdhci_arasan->has_cqe = true;
1709 		host->mmc->caps2 |= MMC_CAP2_CQE;
1710 
1711 		if (!of_property_read_bool(np, "disable-cqe-dcmd"))
1712 			host->mmc->caps2 |= MMC_CAP2_CQE_DCMD;
1713 	}
1714 
1715 	ret = sdhci_arasan_add_host(sdhci_arasan);
1716 	if (ret)
1717 		goto err_add_host;
1718 
1719 	return 0;
1720 
1721 err_add_host:
1722 	if (!IS_ERR(sdhci_arasan->phy))
1723 		phy_exit(sdhci_arasan->phy);
1724 unreg_clk:
1725 	sdhci_arasan_unregister_sdclk(dev);
1726 clk_disable_all:
1727 	clk_disable_unprepare(clk_xin);
1728 clk_dis_ahb:
1729 	clk_disable_unprepare(sdhci_arasan->clk_ahb);
1730 err_pltfm_free:
1731 	sdhci_pltfm_free(pdev);
1732 	return ret;
1733 }
1734 
1735 static int sdhci_arasan_remove(struct platform_device *pdev)
1736 {
1737 	struct sdhci_host *host = platform_get_drvdata(pdev);
1738 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1739 	struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
1740 	struct clk *clk_ahb = sdhci_arasan->clk_ahb;
1741 
1742 	if (!IS_ERR(sdhci_arasan->phy)) {
1743 		if (sdhci_arasan->is_phy_on)
1744 			phy_power_off(sdhci_arasan->phy);
1745 		phy_exit(sdhci_arasan->phy);
1746 	}
1747 
1748 	sdhci_arasan_unregister_sdclk(&pdev->dev);
1749 
1750 	sdhci_pltfm_unregister(pdev);
1751 
1752 	clk_disable_unprepare(clk_ahb);
1753 
1754 	return 0;
1755 }
1756 
1757 static struct platform_driver sdhci_arasan_driver = {
1758 	.driver = {
1759 		.name = "sdhci-arasan",
1760 		.probe_type = PROBE_PREFER_ASYNCHRONOUS,
1761 		.of_match_table = sdhci_arasan_of_match,
1762 		.pm = &sdhci_arasan_dev_pm_ops,
1763 	},
1764 	.probe = sdhci_arasan_probe,
1765 	.remove = sdhci_arasan_remove,
1766 };
1767 
1768 module_platform_driver(sdhci_arasan_driver);
1769 
1770 MODULE_DESCRIPTION("Driver for the Arasan SDHCI Controller");
1771 MODULE_AUTHOR("Soeren Brinkmann <soren.brinkmann@xilinx.com>");
1772 MODULE_LICENSE("GPL");
1773