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-pltfm.h"
29 
30 #define SDHCI_ARASAN_VENDOR_REGISTER	0x78
31 #define SDHCI_ARASAN_CQE_BASE_ADDR	0x200
32 #define VENDOR_ENHANCED_STROBE		BIT(0)
33 
34 #define PHY_CLK_TOO_SLOW_HZ		400000
35 
36 /* Default settings for ZynqMP Clock Phases */
37 #define ZYNQMP_ICLK_PHASE {0, 63, 63, 0, 63,  0,   0, 183, 54,  0, 0}
38 #define ZYNQMP_OCLK_PHASE {0, 72, 60, 0, 60, 72, 135, 48, 72, 135, 0}
39 
40 /*
41  * On some SoCs the syscon area has a feature where the upper 16-bits of
42  * each 32-bit register act as a write mask for the lower 16-bits.  This allows
43  * atomic updates of the register without locking.  This macro is used on SoCs
44  * that have that feature.
45  */
46 #define HIWORD_UPDATE(val, mask, shift) \
47 		((val) << (shift) | (mask) << ((shift) + 16))
48 
49 /**
50  * struct sdhci_arasan_soc_ctl_field - Field used in sdhci_arasan_soc_ctl_map
51  *
52  * @reg:	Offset within the syscon of the register containing this field
53  * @width:	Number of bits for this field
54  * @shift:	Bit offset within @reg of this field (or -1 if not avail)
55  */
56 struct sdhci_arasan_soc_ctl_field {
57 	u32 reg;
58 	u16 width;
59 	s16 shift;
60 };
61 
62 /**
63  * struct sdhci_arasan_soc_ctl_map - Map in syscon to corecfg registers
64  *
65  * It's up to the licensee of the Arsan IP block to make these available
66  * somewhere if needed.  Presumably these will be scattered somewhere that's
67  * accessible via the syscon API.
68  *
69  * @baseclkfreq:	Where to find corecfg_baseclkfreq
70  * @clockmultiplier:	Where to find corecfg_clockmultiplier
71  * @hiword_update:	If true, use HIWORD_UPDATE to access the syscon
72  */
73 struct sdhci_arasan_soc_ctl_map {
74 	struct sdhci_arasan_soc_ctl_field	baseclkfreq;
75 	struct sdhci_arasan_soc_ctl_field	clockmultiplier;
76 	bool					hiword_update;
77 };
78 
79 /**
80  * struct sdhci_arasan_clk_data
81  * @sdcardclk_hw:	Struct for the clock we might provide to a PHY.
82  * @sdcardclk:		Pointer to normal 'struct clock' for sdcardclk_hw.
83  * @sampleclk_hw:	Struct for the clock we might provide to a PHY.
84  * @sampleclk:		Pointer to normal 'struct clock' for sampleclk_hw.
85  * @clk_phase_in:	Array of Input Clock Phase Delays for all speed modes
86  * @clk_phase_out:	Array of Output Clock Phase Delays for all speed modes
87  * @set_clk_delays:	Function pointer for setting Clock Delays
88  * @clk_of_data:	Platform specific runtime clock data storage pointer
89  */
90 struct sdhci_arasan_clk_data {
91 	struct clk_hw	sdcardclk_hw;
92 	struct clk      *sdcardclk;
93 	struct clk_hw	sampleclk_hw;
94 	struct clk      *sampleclk;
95 	int		clk_phase_in[MMC_TIMING_MMC_HS400 + 1];
96 	int		clk_phase_out[MMC_TIMING_MMC_HS400 + 1];
97 	void		(*set_clk_delays)(struct sdhci_host *host);
98 	void		*clk_of_data;
99 };
100 
101 struct sdhci_arasan_zynqmp_clk_data {
102 	const struct zynqmp_eemi_ops *eemi_ops;
103 };
104 
105 /**
106  * struct sdhci_arasan_data
107  * @host:		Pointer to the main SDHCI host structure.
108  * @clk_ahb:		Pointer to the AHB clock
109  * @phy:		Pointer to the generic phy
110  * @is_phy_on:		True if the PHY is on; false if not.
111  * @clk_data:		Struct for the Arasan Controller Clock Data.
112  * @soc_ctl_base:	Pointer to regmap for syscon for soc_ctl registers.
113  * @soc_ctl_map:	Map to get offsets into soc_ctl registers.
114  */
115 struct sdhci_arasan_data {
116 	struct sdhci_host *host;
117 	struct clk	*clk_ahb;
118 	struct phy	*phy;
119 	bool		is_phy_on;
120 
121 	bool		has_cqe;
122 	struct sdhci_arasan_clk_data clk_data;
123 
124 	struct regmap	*soc_ctl_base;
125 	const struct sdhci_arasan_soc_ctl_map *soc_ctl_map;
126 	unsigned int	quirks; /* Arasan deviations from spec */
127 
128 /* Controller does not have CD wired and will not function normally without */
129 #define SDHCI_ARASAN_QUIRK_FORCE_CDTEST	BIT(0)
130 /* Controller immediately reports SDHCI_CLOCK_INT_STABLE after enabling the
131  * internal clock even when the clock isn't stable */
132 #define SDHCI_ARASAN_QUIRK_CLOCK_UNSTABLE BIT(1)
133 };
134 
135 struct sdhci_arasan_of_data {
136 	const struct sdhci_arasan_soc_ctl_map *soc_ctl_map;
137 	const struct sdhci_pltfm_data *pdata;
138 };
139 
140 static const struct sdhci_arasan_soc_ctl_map rk3399_soc_ctl_map = {
141 	.baseclkfreq = { .reg = 0xf000, .width = 8, .shift = 8 },
142 	.clockmultiplier = { .reg = 0xf02c, .width = 8, .shift = 0},
143 	.hiword_update = true,
144 };
145 
146 static const struct sdhci_arasan_soc_ctl_map intel_lgm_emmc_soc_ctl_map = {
147 	.baseclkfreq = { .reg = 0xa0, .width = 8, .shift = 2 },
148 	.clockmultiplier = { .reg = 0, .width = -1, .shift = -1 },
149 	.hiword_update = false,
150 };
151 
152 static const struct sdhci_arasan_soc_ctl_map intel_lgm_sdxc_soc_ctl_map = {
153 	.baseclkfreq = { .reg = 0x80, .width = 8, .shift = 2 },
154 	.clockmultiplier = { .reg = 0, .width = -1, .shift = -1 },
155 	.hiword_update = false,
156 };
157 
158 /**
159  * sdhci_arasan_syscon_write - Write to a field in soc_ctl registers
160  *
161  * This function allows writing to fields in sdhci_arasan_soc_ctl_map.
162  * Note that if a field is specified as not available (shift < 0) then
163  * this function will silently return an error code.  It will be noisy
164  * and print errors for any other (unexpected) errors.
165  *
166  * @host:	The sdhci_host
167  * @fld:	The field to write to
168  * @val:	The value to write
169  */
170 static int sdhci_arasan_syscon_write(struct sdhci_host *host,
171 				   const struct sdhci_arasan_soc_ctl_field *fld,
172 				   u32 val)
173 {
174 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
175 	struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
176 	struct regmap *soc_ctl_base = sdhci_arasan->soc_ctl_base;
177 	u32 reg = fld->reg;
178 	u16 width = fld->width;
179 	s16 shift = fld->shift;
180 	int ret;
181 
182 	/*
183 	 * Silently return errors for shift < 0 so caller doesn't have
184 	 * to check for fields which are optional.  For fields that
185 	 * are required then caller needs to do something special
186 	 * anyway.
187 	 */
188 	if (shift < 0)
189 		return -EINVAL;
190 
191 	if (sdhci_arasan->soc_ctl_map->hiword_update)
192 		ret = regmap_write(soc_ctl_base, reg,
193 				   HIWORD_UPDATE(val, GENMASK(width, 0),
194 						 shift));
195 	else
196 		ret = regmap_update_bits(soc_ctl_base, reg,
197 					 GENMASK(shift + width, shift),
198 					 val << shift);
199 
200 	/* Yell about (unexpected) regmap errors */
201 	if (ret)
202 		pr_warn("%s: Regmap write fail: %d\n",
203 			 mmc_hostname(host->mmc), ret);
204 
205 	return ret;
206 }
207 
208 static void sdhci_arasan_set_clock(struct sdhci_host *host, unsigned int clock)
209 {
210 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
211 	struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
212 	struct sdhci_arasan_clk_data *clk_data = &sdhci_arasan->clk_data;
213 	bool ctrl_phy = false;
214 
215 	if (!IS_ERR(sdhci_arasan->phy)) {
216 		if (!sdhci_arasan->is_phy_on && clock <= PHY_CLK_TOO_SLOW_HZ) {
217 			/*
218 			 * If PHY off, set clock to max speed and power PHY on.
219 			 *
220 			 * Although PHY docs apparently suggest power cycling
221 			 * when changing the clock the PHY doesn't like to be
222 			 * powered on while at low speeds like those used in ID
223 			 * mode.  Even worse is powering the PHY on while the
224 			 * clock is off.
225 			 *
226 			 * To workaround the PHY limitations, the best we can
227 			 * do is to power it on at a faster speed and then slam
228 			 * through low speeds without power cycling.
229 			 */
230 			sdhci_set_clock(host, host->max_clk);
231 			phy_power_on(sdhci_arasan->phy);
232 			sdhci_arasan->is_phy_on = true;
233 
234 			/*
235 			 * We'll now fall through to the below case with
236 			 * ctrl_phy = false (so we won't turn off/on).  The
237 			 * sdhci_set_clock() will set the real clock.
238 			 */
239 		} else if (clock > PHY_CLK_TOO_SLOW_HZ) {
240 			/*
241 			 * At higher clock speeds the PHY is fine being power
242 			 * cycled and docs say you _should_ power cycle when
243 			 * changing clock speeds.
244 			 */
245 			ctrl_phy = true;
246 		}
247 	}
248 
249 	if (ctrl_phy && sdhci_arasan->is_phy_on) {
250 		phy_power_off(sdhci_arasan->phy);
251 		sdhci_arasan->is_phy_on = false;
252 	}
253 
254 	/* Set the Input and Output Clock Phase Delays */
255 	if (clk_data->set_clk_delays)
256 		clk_data->set_clk_delays(host);
257 
258 	sdhci_set_clock(host, clock);
259 
260 	if (sdhci_arasan->quirks & SDHCI_ARASAN_QUIRK_CLOCK_UNSTABLE)
261 		/*
262 		 * Some controllers immediately report SDHCI_CLOCK_INT_STABLE
263 		 * after enabling the clock even though the clock is not
264 		 * stable. Trying to use a clock without waiting here results
265 		 * in EILSEQ while detecting some older/slower cards. The
266 		 * chosen delay is the maximum delay from sdhci_set_clock.
267 		 */
268 		msleep(20);
269 
270 	if (ctrl_phy) {
271 		phy_power_on(sdhci_arasan->phy);
272 		sdhci_arasan->is_phy_on = true;
273 	}
274 }
275 
276 static void sdhci_arasan_hs400_enhanced_strobe(struct mmc_host *mmc,
277 					struct mmc_ios *ios)
278 {
279 	u32 vendor;
280 	struct sdhci_host *host = mmc_priv(mmc);
281 
282 	vendor = sdhci_readl(host, SDHCI_ARASAN_VENDOR_REGISTER);
283 	if (ios->enhanced_strobe)
284 		vendor |= VENDOR_ENHANCED_STROBE;
285 	else
286 		vendor &= ~VENDOR_ENHANCED_STROBE;
287 
288 	sdhci_writel(host, vendor, SDHCI_ARASAN_VENDOR_REGISTER);
289 }
290 
291 static void sdhci_arasan_reset(struct sdhci_host *host, u8 mask)
292 {
293 	u8 ctrl;
294 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
295 	struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
296 
297 	sdhci_reset(host, mask);
298 
299 	if (sdhci_arasan->quirks & SDHCI_ARASAN_QUIRK_FORCE_CDTEST) {
300 		ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
301 		ctrl |= SDHCI_CTRL_CDTEST_INS | SDHCI_CTRL_CDTEST_EN;
302 		sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
303 	}
304 }
305 
306 static int sdhci_arasan_voltage_switch(struct mmc_host *mmc,
307 				       struct mmc_ios *ios)
308 {
309 	switch (ios->signal_voltage) {
310 	case MMC_SIGNAL_VOLTAGE_180:
311 		/*
312 		 * Plese don't switch to 1V8 as arasan,5.1 doesn't
313 		 * actually refer to this setting to indicate the
314 		 * signal voltage and the state machine will be broken
315 		 * actually if we force to enable 1V8. That's something
316 		 * like broken quirk but we could work around here.
317 		 */
318 		return 0;
319 	case MMC_SIGNAL_VOLTAGE_330:
320 	case MMC_SIGNAL_VOLTAGE_120:
321 		/* We don't support 3V3 and 1V2 */
322 		break;
323 	}
324 
325 	return -EINVAL;
326 }
327 
328 static void sdhci_arasan_set_power(struct sdhci_host *host, unsigned char mode,
329 		     unsigned short vdd)
330 {
331 	if (!IS_ERR(host->mmc->supply.vmmc)) {
332 		struct mmc_host *mmc = host->mmc;
333 
334 		mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, vdd);
335 	}
336 	sdhci_set_power_noreg(host, mode, vdd);
337 }
338 
339 static const struct sdhci_ops sdhci_arasan_ops = {
340 	.set_clock = sdhci_arasan_set_clock,
341 	.get_max_clock = sdhci_pltfm_clk_get_max_clock,
342 	.get_timeout_clock = sdhci_pltfm_clk_get_max_clock,
343 	.set_bus_width = sdhci_set_bus_width,
344 	.reset = sdhci_arasan_reset,
345 	.set_uhs_signaling = sdhci_set_uhs_signaling,
346 	.set_power = sdhci_arasan_set_power,
347 };
348 
349 static const struct sdhci_pltfm_data sdhci_arasan_pdata = {
350 	.ops = &sdhci_arasan_ops,
351 	.quirks = SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN,
352 	.quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN |
353 			SDHCI_QUIRK2_CLOCK_DIV_ZERO_BROKEN |
354 			SDHCI_QUIRK2_STOP_WITH_TC,
355 };
356 
357 static struct sdhci_arasan_of_data sdhci_arasan_data = {
358 	.pdata = &sdhci_arasan_pdata,
359 };
360 
361 static u32 sdhci_arasan_cqhci_irq(struct sdhci_host *host, u32 intmask)
362 {
363 	int cmd_error = 0;
364 	int data_error = 0;
365 
366 	if (!sdhci_cqe_irq(host, intmask, &cmd_error, &data_error))
367 		return intmask;
368 
369 	cqhci_irq(host->mmc, intmask, cmd_error, data_error);
370 
371 	return 0;
372 }
373 
374 static void sdhci_arasan_dumpregs(struct mmc_host *mmc)
375 {
376 	sdhci_dumpregs(mmc_priv(mmc));
377 }
378 
379 static void sdhci_arasan_cqe_enable(struct mmc_host *mmc)
380 {
381 	struct sdhci_host *host = mmc_priv(mmc);
382 	u32 reg;
383 
384 	reg = sdhci_readl(host, SDHCI_PRESENT_STATE);
385 	while (reg & SDHCI_DATA_AVAILABLE) {
386 		sdhci_readl(host, SDHCI_BUFFER);
387 		reg = sdhci_readl(host, SDHCI_PRESENT_STATE);
388 	}
389 
390 	sdhci_cqe_enable(mmc);
391 }
392 
393 static const struct cqhci_host_ops sdhci_arasan_cqhci_ops = {
394 	.enable         = sdhci_arasan_cqe_enable,
395 	.disable        = sdhci_cqe_disable,
396 	.dumpregs       = sdhci_arasan_dumpregs,
397 };
398 
399 static const struct sdhci_ops sdhci_arasan_cqe_ops = {
400 	.set_clock = sdhci_arasan_set_clock,
401 	.get_max_clock = sdhci_pltfm_clk_get_max_clock,
402 	.get_timeout_clock = sdhci_pltfm_clk_get_max_clock,
403 	.set_bus_width = sdhci_set_bus_width,
404 	.reset = sdhci_arasan_reset,
405 	.set_uhs_signaling = sdhci_set_uhs_signaling,
406 	.set_power = sdhci_arasan_set_power,
407 	.irq = sdhci_arasan_cqhci_irq,
408 };
409 
410 static const struct sdhci_pltfm_data sdhci_arasan_cqe_pdata = {
411 	.ops = &sdhci_arasan_cqe_ops,
412 	.quirks = SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN,
413 	.quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN |
414 			SDHCI_QUIRK2_CLOCK_DIV_ZERO_BROKEN,
415 };
416 
417 static struct sdhci_arasan_of_data sdhci_arasan_rk3399_data = {
418 	.soc_ctl_map = &rk3399_soc_ctl_map,
419 	.pdata = &sdhci_arasan_cqe_pdata,
420 };
421 
422 static struct sdhci_arasan_of_data intel_lgm_emmc_data = {
423 	.soc_ctl_map = &intel_lgm_emmc_soc_ctl_map,
424 	.pdata = &sdhci_arasan_cqe_pdata,
425 };
426 
427 static struct sdhci_arasan_of_data intel_lgm_sdxc_data = {
428 	.soc_ctl_map = &intel_lgm_sdxc_soc_ctl_map,
429 	.pdata = &sdhci_arasan_cqe_pdata,
430 };
431 
432 #ifdef CONFIG_PM_SLEEP
433 /**
434  * sdhci_arasan_suspend - Suspend method for the driver
435  * @dev:	Address of the device structure
436  * Returns 0 on success and error value on error
437  *
438  * Put the device in a low power state.
439  */
440 static int sdhci_arasan_suspend(struct device *dev)
441 {
442 	struct sdhci_host *host = dev_get_drvdata(dev);
443 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
444 	struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
445 	int ret;
446 
447 	if (host->tuning_mode != SDHCI_TUNING_MODE_3)
448 		mmc_retune_needed(host->mmc);
449 
450 	if (sdhci_arasan->has_cqe) {
451 		ret = cqhci_suspend(host->mmc);
452 		if (ret)
453 			return ret;
454 	}
455 
456 	ret = sdhci_suspend_host(host);
457 	if (ret)
458 		return ret;
459 
460 	if (!IS_ERR(sdhci_arasan->phy) && sdhci_arasan->is_phy_on) {
461 		ret = phy_power_off(sdhci_arasan->phy);
462 		if (ret) {
463 			dev_err(dev, "Cannot power off phy.\n");
464 			sdhci_resume_host(host);
465 			return ret;
466 		}
467 		sdhci_arasan->is_phy_on = false;
468 	}
469 
470 	clk_disable(pltfm_host->clk);
471 	clk_disable(sdhci_arasan->clk_ahb);
472 
473 	return 0;
474 }
475 
476 /**
477  * sdhci_arasan_resume - Resume method for the driver
478  * @dev:	Address of the device structure
479  * Returns 0 on success and error value on error
480  *
481  * Resume operation after suspend
482  */
483 static int sdhci_arasan_resume(struct device *dev)
484 {
485 	struct sdhci_host *host = dev_get_drvdata(dev);
486 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
487 	struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
488 	int ret;
489 
490 	ret = clk_enable(sdhci_arasan->clk_ahb);
491 	if (ret) {
492 		dev_err(dev, "Cannot enable AHB clock.\n");
493 		return ret;
494 	}
495 
496 	ret = clk_enable(pltfm_host->clk);
497 	if (ret) {
498 		dev_err(dev, "Cannot enable SD clock.\n");
499 		return ret;
500 	}
501 
502 	if (!IS_ERR(sdhci_arasan->phy) && host->mmc->actual_clock) {
503 		ret = phy_power_on(sdhci_arasan->phy);
504 		if (ret) {
505 			dev_err(dev, "Cannot power on phy.\n");
506 			return ret;
507 		}
508 		sdhci_arasan->is_phy_on = true;
509 	}
510 
511 	ret = sdhci_resume_host(host);
512 	if (ret) {
513 		dev_err(dev, "Cannot resume host.\n");
514 		return ret;
515 	}
516 
517 	if (sdhci_arasan->has_cqe)
518 		return cqhci_resume(host->mmc);
519 
520 	return 0;
521 }
522 #endif /* ! CONFIG_PM_SLEEP */
523 
524 static SIMPLE_DEV_PM_OPS(sdhci_arasan_dev_pm_ops, sdhci_arasan_suspend,
525 			 sdhci_arasan_resume);
526 
527 static const struct of_device_id sdhci_arasan_of_match[] = {
528 	/* SoC-specific compatible strings w/ soc_ctl_map */
529 	{
530 		.compatible = "rockchip,rk3399-sdhci-5.1",
531 		.data = &sdhci_arasan_rk3399_data,
532 	},
533 	{
534 		.compatible = "intel,lgm-sdhci-5.1-emmc",
535 		.data = &intel_lgm_emmc_data,
536 	},
537 	{
538 		.compatible = "intel,lgm-sdhci-5.1-sdxc",
539 		.data = &intel_lgm_sdxc_data,
540 	},
541 	/* Generic compatible below here */
542 	{
543 		.compatible = "arasan,sdhci-8.9a",
544 		.data = &sdhci_arasan_data,
545 	},
546 	{
547 		.compatible = "arasan,sdhci-5.1",
548 		.data = &sdhci_arasan_data,
549 	},
550 	{
551 		.compatible = "arasan,sdhci-4.9a",
552 		.data = &sdhci_arasan_data,
553 	},
554 	{
555 		.compatible = "xlnx,zynqmp-8.9a",
556 		.data = &sdhci_arasan_data,
557 	},
558 	{ /* sentinel */ }
559 };
560 MODULE_DEVICE_TABLE(of, sdhci_arasan_of_match);
561 
562 /**
563  * sdhci_arasan_sdcardclk_recalc_rate - Return the card clock rate
564  *
565  * Return the current actual rate of the SD card clock.  This can be used
566  * to communicate with out PHY.
567  *
568  * @hw:			Pointer to the hardware clock structure.
569  * @parent_rate		The parent rate (should be rate of clk_xin).
570  * Returns the card clock rate.
571  */
572 static unsigned long sdhci_arasan_sdcardclk_recalc_rate(struct clk_hw *hw,
573 						      unsigned long parent_rate)
574 
575 {
576 	struct sdhci_arasan_clk_data *clk_data =
577 		container_of(hw, struct sdhci_arasan_clk_data, sdcardclk_hw);
578 	struct sdhci_arasan_data *sdhci_arasan =
579 		container_of(clk_data, struct sdhci_arasan_data, clk_data);
580 	struct sdhci_host *host = sdhci_arasan->host;
581 
582 	return host->mmc->actual_clock;
583 }
584 
585 static const struct clk_ops arasan_sdcardclk_ops = {
586 	.recalc_rate = sdhci_arasan_sdcardclk_recalc_rate,
587 };
588 
589 /**
590  * sdhci_arasan_sampleclk_recalc_rate - Return the sampling clock rate
591  *
592  * Return the current actual rate of the sampling clock.  This can be used
593  * to communicate with out PHY.
594  *
595  * @hw:			Pointer to the hardware clock structure.
596  * @parent_rate		The parent rate (should be rate of clk_xin).
597  * Returns the sample clock rate.
598  */
599 static unsigned long sdhci_arasan_sampleclk_recalc_rate(struct clk_hw *hw,
600 						      unsigned long parent_rate)
601 
602 {
603 	struct sdhci_arasan_clk_data *clk_data =
604 		container_of(hw, struct sdhci_arasan_clk_data, sampleclk_hw);
605 	struct sdhci_arasan_data *sdhci_arasan =
606 		container_of(clk_data, struct sdhci_arasan_data, clk_data);
607 	struct sdhci_host *host = sdhci_arasan->host;
608 
609 	return host->mmc->actual_clock;
610 }
611 
612 static const struct clk_ops arasan_sampleclk_ops = {
613 	.recalc_rate = sdhci_arasan_sampleclk_recalc_rate,
614 };
615 
616 /**
617  * sdhci_zynqmp_sdcardclk_set_phase - Set the SD Output Clock Tap Delays
618  *
619  * Set the SD Output Clock Tap Delays for Output path
620  *
621  * @hw:			Pointer to the hardware clock structure.
622  * @degrees		The clock phase shift between 0 - 359.
623  * Return: 0 on success and error value on error
624  */
625 static int sdhci_zynqmp_sdcardclk_set_phase(struct clk_hw *hw, int degrees)
626 
627 {
628 	struct sdhci_arasan_clk_data *clk_data =
629 		container_of(hw, struct sdhci_arasan_clk_data, sdcardclk_hw);
630 	struct sdhci_arasan_data *sdhci_arasan =
631 		container_of(clk_data, struct sdhci_arasan_data, clk_data);
632 	struct sdhci_host *host = sdhci_arasan->host;
633 	struct sdhci_arasan_zynqmp_clk_data *zynqmp_clk_data =
634 		clk_data->clk_of_data;
635 	const struct zynqmp_eemi_ops *eemi_ops = zynqmp_clk_data->eemi_ops;
636 	const char *clk_name = clk_hw_get_name(hw);
637 	u32 node_id = !strcmp(clk_name, "clk_out_sd0") ? NODE_SD_0 : NODE_SD_1;
638 	u8 tap_delay, tap_max = 0;
639 	int ret;
640 
641 	/*
642 	 * This is applicable for SDHCI_SPEC_300 and above
643 	 * ZynqMP does not set phase for <=25MHz clock.
644 	 * If degrees is zero, no need to do anything.
645 	 */
646 	if (host->version < SDHCI_SPEC_300 ||
647 	    host->timing == MMC_TIMING_LEGACY ||
648 	    host->timing == MMC_TIMING_UHS_SDR12 || !degrees)
649 		return 0;
650 
651 	switch (host->timing) {
652 	case MMC_TIMING_MMC_HS:
653 	case MMC_TIMING_SD_HS:
654 	case MMC_TIMING_UHS_SDR25:
655 	case MMC_TIMING_UHS_DDR50:
656 	case MMC_TIMING_MMC_DDR52:
657 		/* For 50MHz clock, 30 Taps are available */
658 		tap_max = 30;
659 		break;
660 	case MMC_TIMING_UHS_SDR50:
661 		/* For 100MHz clock, 15 Taps are available */
662 		tap_max = 15;
663 		break;
664 	case MMC_TIMING_UHS_SDR104:
665 	case MMC_TIMING_MMC_HS200:
666 		/* For 200MHz clock, 8 Taps are available */
667 		tap_max = 8;
668 	default:
669 		break;
670 	}
671 
672 	tap_delay = (degrees * tap_max) / 360;
673 
674 	/* Set the Clock Phase */
675 	ret = eemi_ops->ioctl(node_id, IOCTL_SET_SD_TAPDELAY,
676 			      PM_TAPDELAY_OUTPUT, tap_delay, NULL);
677 	if (ret)
678 		pr_err("Error setting Output Tap Delay\n");
679 
680 	return ret;
681 }
682 
683 static const struct clk_ops zynqmp_sdcardclk_ops = {
684 	.recalc_rate = sdhci_arasan_sdcardclk_recalc_rate,
685 	.set_phase = sdhci_zynqmp_sdcardclk_set_phase,
686 };
687 
688 /**
689  * sdhci_zynqmp_sampleclk_set_phase - Set the SD Input Clock Tap Delays
690  *
691  * Set the SD Input Clock Tap Delays for Input path
692  *
693  * @hw:			Pointer to the hardware clock structure.
694  * @degrees		The clock phase shift between 0 - 359.
695  * Return: 0 on success and error value on error
696  */
697 static int sdhci_zynqmp_sampleclk_set_phase(struct clk_hw *hw, int degrees)
698 
699 {
700 	struct sdhci_arasan_clk_data *clk_data =
701 		container_of(hw, struct sdhci_arasan_clk_data, sampleclk_hw);
702 	struct sdhci_arasan_data *sdhci_arasan =
703 		container_of(clk_data, struct sdhci_arasan_data, clk_data);
704 	struct sdhci_host *host = sdhci_arasan->host;
705 	struct sdhci_arasan_zynqmp_clk_data *zynqmp_clk_data =
706 		clk_data->clk_of_data;
707 	const struct zynqmp_eemi_ops *eemi_ops = zynqmp_clk_data->eemi_ops;
708 	const char *clk_name = clk_hw_get_name(hw);
709 	u32 node_id = !strcmp(clk_name, "clk_in_sd0") ? NODE_SD_0 : NODE_SD_1;
710 	u8 tap_delay, tap_max = 0;
711 	int ret;
712 
713 	/*
714 	 * This is applicable for SDHCI_SPEC_300 and above
715 	 * ZynqMP does not set phase for <=25MHz clock.
716 	 * If degrees is zero, no need to do anything.
717 	 */
718 	if (host->version < SDHCI_SPEC_300 ||
719 	    host->timing == MMC_TIMING_LEGACY ||
720 	    host->timing == MMC_TIMING_UHS_SDR12 || !degrees)
721 		return 0;
722 
723 	switch (host->timing) {
724 	case MMC_TIMING_MMC_HS:
725 	case MMC_TIMING_SD_HS:
726 	case MMC_TIMING_UHS_SDR25:
727 	case MMC_TIMING_UHS_DDR50:
728 	case MMC_TIMING_MMC_DDR52:
729 		/* For 50MHz clock, 120 Taps are available */
730 		tap_max = 120;
731 		break;
732 	case MMC_TIMING_UHS_SDR50:
733 		/* For 100MHz clock, 60 Taps are available */
734 		tap_max = 60;
735 		break;
736 	case MMC_TIMING_UHS_SDR104:
737 	case MMC_TIMING_MMC_HS200:
738 		/* For 200MHz clock, 30 Taps are available */
739 		tap_max = 30;
740 	default:
741 		break;
742 	}
743 
744 	tap_delay = (degrees * tap_max) / 360;
745 
746 	/* Set the Clock Phase */
747 	ret = eemi_ops->ioctl(node_id, IOCTL_SET_SD_TAPDELAY,
748 			      PM_TAPDELAY_INPUT, tap_delay, NULL);
749 	if (ret)
750 		pr_err("Error setting Input Tap Delay\n");
751 
752 	return ret;
753 }
754 
755 static const struct clk_ops zynqmp_sampleclk_ops = {
756 	.recalc_rate = sdhci_arasan_sampleclk_recalc_rate,
757 	.set_phase = sdhci_zynqmp_sampleclk_set_phase,
758 };
759 
760 /**
761  * sdhci_arasan_update_clockmultiplier - Set corecfg_clockmultiplier
762  *
763  * The corecfg_clockmultiplier is supposed to contain clock multiplier
764  * value of programmable clock generator.
765  *
766  * NOTES:
767  * - Many existing devices don't seem to do this and work fine.  To keep
768  *   compatibility for old hardware where the device tree doesn't provide a
769  *   register map, this function is a noop if a soc_ctl_map hasn't been provided
770  *   for this platform.
771  * - The value of corecfg_clockmultiplier should sync with that of corresponding
772  *   value reading from sdhci_capability_register. So this function is called
773  *   once at probe time and never called again.
774  *
775  * @host:		The sdhci_host
776  */
777 static void sdhci_arasan_update_clockmultiplier(struct sdhci_host *host,
778 						u32 value)
779 {
780 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
781 	struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
782 	const struct sdhci_arasan_soc_ctl_map *soc_ctl_map =
783 		sdhci_arasan->soc_ctl_map;
784 
785 	/* Having a map is optional */
786 	if (!soc_ctl_map)
787 		return;
788 
789 	/* If we have a map, we expect to have a syscon */
790 	if (!sdhci_arasan->soc_ctl_base) {
791 		pr_warn("%s: Have regmap, but no soc-ctl-syscon\n",
792 			mmc_hostname(host->mmc));
793 		return;
794 	}
795 
796 	sdhci_arasan_syscon_write(host, &soc_ctl_map->clockmultiplier, value);
797 }
798 
799 /**
800  * sdhci_arasan_update_baseclkfreq - Set corecfg_baseclkfreq
801  *
802  * The corecfg_baseclkfreq is supposed to contain the MHz of clk_xin.  This
803  * function can be used to make that happen.
804  *
805  * NOTES:
806  * - Many existing devices don't seem to do this and work fine.  To keep
807  *   compatibility for old hardware where the device tree doesn't provide a
808  *   register map, this function is a noop if a soc_ctl_map hasn't been provided
809  *   for this platform.
810  * - It's assumed that clk_xin is not dynamic and that we use the SDHCI divider
811  *   to achieve lower clock rates.  That means that this function is called once
812  *   at probe time and never called again.
813  *
814  * @host:		The sdhci_host
815  */
816 static void sdhci_arasan_update_baseclkfreq(struct sdhci_host *host)
817 {
818 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
819 	struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
820 	const struct sdhci_arasan_soc_ctl_map *soc_ctl_map =
821 		sdhci_arasan->soc_ctl_map;
822 	u32 mhz = DIV_ROUND_CLOSEST(clk_get_rate(pltfm_host->clk), 1000000);
823 
824 	/* Having a map is optional */
825 	if (!soc_ctl_map)
826 		return;
827 
828 	/* If we have a map, we expect to have a syscon */
829 	if (!sdhci_arasan->soc_ctl_base) {
830 		pr_warn("%s: Have regmap, but no soc-ctl-syscon\n",
831 			mmc_hostname(host->mmc));
832 		return;
833 	}
834 
835 	sdhci_arasan_syscon_write(host, &soc_ctl_map->baseclkfreq, mhz);
836 }
837 
838 static void sdhci_arasan_set_clk_delays(struct sdhci_host *host)
839 {
840 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
841 	struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
842 	struct sdhci_arasan_clk_data *clk_data = &sdhci_arasan->clk_data;
843 
844 	clk_set_phase(clk_data->sampleclk,
845 		      clk_data->clk_phase_in[host->timing]);
846 	clk_set_phase(clk_data->sdcardclk,
847 		      clk_data->clk_phase_out[host->timing]);
848 }
849 
850 static void arasan_dt_read_clk_phase(struct device *dev,
851 				     struct sdhci_arasan_clk_data *clk_data,
852 				     unsigned int timing, const char *prop)
853 {
854 	struct device_node *np = dev->of_node;
855 
856 	int clk_phase[2] = {0};
857 
858 	/*
859 	 * Read Tap Delay values from DT, if the DT does not contain the
860 	 * Tap Values then use the pre-defined values.
861 	 */
862 	if (of_property_read_variable_u32_array(np, prop, &clk_phase[0],
863 						2, 0)) {
864 		dev_dbg(dev, "Using predefined clock phase for %s = %d %d\n",
865 			prop, clk_data->clk_phase_in[timing],
866 			clk_data->clk_phase_out[timing]);
867 		return;
868 	}
869 
870 	/* The values read are Input and Output Clock Delays in order */
871 	clk_data->clk_phase_in[timing] = clk_phase[0];
872 	clk_data->clk_phase_out[timing] = clk_phase[1];
873 }
874 
875 /**
876  * arasan_dt_parse_clk_phases - Read Clock Delay values from DT
877  *
878  * Called at initialization to parse the values of Clock Delays.
879  *
880  * @dev:		Pointer to our struct device.
881  * @clk_data:		Pointer to the Clock Data structure
882  */
883 static void arasan_dt_parse_clk_phases(struct device *dev,
884 				       struct sdhci_arasan_clk_data *clk_data)
885 {
886 	int *iclk_phase, *oclk_phase;
887 	u32 mio_bank = 0;
888 	int i;
889 
890 	/*
891 	 * This has been kept as a pointer and is assigned a function here.
892 	 * So that different controller variants can assign their own handling
893 	 * function.
894 	 */
895 	clk_data->set_clk_delays = sdhci_arasan_set_clk_delays;
896 
897 	if (of_device_is_compatible(dev->of_node, "xlnx,zynqmp-8.9a")) {
898 		iclk_phase = (int [MMC_TIMING_MMC_HS400 + 1]) ZYNQMP_ICLK_PHASE;
899 		oclk_phase = (int [MMC_TIMING_MMC_HS400 + 1]) ZYNQMP_OCLK_PHASE;
900 
901 		of_property_read_u32(dev->of_node, "xlnx,mio-bank", &mio_bank);
902 		if (mio_bank == 2) {
903 			oclk_phase[MMC_TIMING_UHS_SDR104] = 90;
904 			oclk_phase[MMC_TIMING_MMC_HS200] = 90;
905 		}
906 
907 		for (i = 0; i <= MMC_TIMING_MMC_HS400; i++) {
908 			clk_data->clk_phase_in[i] = iclk_phase[i];
909 			clk_data->clk_phase_out[i] = oclk_phase[i];
910 		}
911 	}
912 
913 	arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_LEGACY,
914 				 "clk-phase-legacy");
915 	arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_MMC_HS,
916 				 "clk-phase-mmc-hs");
917 	arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_SD_HS,
918 				 "clk-phase-sd-hs");
919 	arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_UHS_SDR12,
920 				 "clk-phase-uhs-sdr12");
921 	arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_UHS_SDR25,
922 				 "clk-phase-uhs-sdr25");
923 	arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_UHS_SDR50,
924 				 "clk-phase-uhs-sdr50");
925 	arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_UHS_SDR104,
926 				 "clk-phase-uhs-sdr104");
927 	arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_UHS_DDR50,
928 				 "clk-phase-uhs-ddr50");
929 	arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_MMC_DDR52,
930 				 "clk-phase-mmc-ddr52");
931 	arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_MMC_HS200,
932 				 "clk-phase-mmc-hs200");
933 	arasan_dt_read_clk_phase(dev, clk_data, MMC_TIMING_MMC_HS400,
934 				 "clk-phase-mmc-hs400");
935 }
936 
937 /**
938  * sdhci_arasan_register_sdcardclk - Register the sdcardclk for a PHY to use
939  *
940  * Some PHY devices need to know what the actual card clock is.  In order for
941  * them to find out, we'll provide a clock through the common clock framework
942  * for them to query.
943  *
944  * @sdhci_arasan:	Our private data structure.
945  * @clk_xin:		Pointer to the functional clock
946  * @dev:		Pointer to our struct device.
947  * Returns 0 on success and error value on error
948  */
949 static int
950 sdhci_arasan_register_sdcardclk(struct sdhci_arasan_data *sdhci_arasan,
951 				struct clk *clk_xin,
952 				struct device *dev)
953 {
954 	struct sdhci_arasan_clk_data *clk_data = &sdhci_arasan->clk_data;
955 	struct device_node *np = dev->of_node;
956 	struct clk_init_data sdcardclk_init;
957 	const char *parent_clk_name;
958 	int ret;
959 
960 	ret = of_property_read_string_index(np, "clock-output-names", 0,
961 					    &sdcardclk_init.name);
962 	if (ret) {
963 		dev_err(dev, "DT has #clock-cells but no clock-output-names\n");
964 		return ret;
965 	}
966 
967 	parent_clk_name = __clk_get_name(clk_xin);
968 	sdcardclk_init.parent_names = &parent_clk_name;
969 	sdcardclk_init.num_parents = 1;
970 	sdcardclk_init.flags = CLK_GET_RATE_NOCACHE;
971 	if (of_device_is_compatible(np, "xlnx,zynqmp-8.9a"))
972 		sdcardclk_init.ops = &zynqmp_sdcardclk_ops;
973 	else
974 		sdcardclk_init.ops = &arasan_sdcardclk_ops;
975 
976 	clk_data->sdcardclk_hw.init = &sdcardclk_init;
977 	clk_data->sdcardclk =
978 		devm_clk_register(dev, &clk_data->sdcardclk_hw);
979 	clk_data->sdcardclk_hw.init = NULL;
980 
981 	ret = of_clk_add_provider(np, of_clk_src_simple_get,
982 				  clk_data->sdcardclk);
983 	if (ret)
984 		dev_err(dev, "Failed to add sdcard clock provider\n");
985 
986 	return ret;
987 }
988 
989 /**
990  * sdhci_arasan_register_sampleclk - Register the sampleclk for a PHY to use
991  *
992  * Some PHY devices need to know what the actual card clock is.  In order for
993  * them to find out, we'll provide a clock through the common clock framework
994  * for them to query.
995  *
996  * @sdhci_arasan:	Our private data structure.
997  * @clk_xin:		Pointer to the functional clock
998  * @dev:		Pointer to our struct device.
999  * Returns 0 on success and error value on error
1000  */
1001 static int
1002 sdhci_arasan_register_sampleclk(struct sdhci_arasan_data *sdhci_arasan,
1003 				struct clk *clk_xin,
1004 				struct device *dev)
1005 {
1006 	struct sdhci_arasan_clk_data *clk_data = &sdhci_arasan->clk_data;
1007 	struct device_node *np = dev->of_node;
1008 	struct clk_init_data sampleclk_init;
1009 	const char *parent_clk_name;
1010 	int ret;
1011 
1012 	ret = of_property_read_string_index(np, "clock-output-names", 1,
1013 					    &sampleclk_init.name);
1014 	if (ret) {
1015 		dev_err(dev, "DT has #clock-cells but no clock-output-names\n");
1016 		return ret;
1017 	}
1018 
1019 	parent_clk_name = __clk_get_name(clk_xin);
1020 	sampleclk_init.parent_names = &parent_clk_name;
1021 	sampleclk_init.num_parents = 1;
1022 	sampleclk_init.flags = CLK_GET_RATE_NOCACHE;
1023 	if (of_device_is_compatible(np, "xlnx,zynqmp-8.9a"))
1024 		sampleclk_init.ops = &zynqmp_sampleclk_ops;
1025 	else
1026 		sampleclk_init.ops = &arasan_sampleclk_ops;
1027 
1028 	clk_data->sampleclk_hw.init = &sampleclk_init;
1029 	clk_data->sampleclk =
1030 		devm_clk_register(dev, &clk_data->sampleclk_hw);
1031 	clk_data->sampleclk_hw.init = NULL;
1032 
1033 	ret = of_clk_add_provider(np, of_clk_src_simple_get,
1034 				  clk_data->sampleclk);
1035 	if (ret)
1036 		dev_err(dev, "Failed to add sample clock provider\n");
1037 
1038 	return ret;
1039 }
1040 
1041 /**
1042  * sdhci_arasan_unregister_sdclk - Undoes sdhci_arasan_register_sdclk()
1043  *
1044  * Should be called any time we're exiting and sdhci_arasan_register_sdclk()
1045  * returned success.
1046  *
1047  * @dev:		Pointer to our struct device.
1048  */
1049 static void sdhci_arasan_unregister_sdclk(struct device *dev)
1050 {
1051 	struct device_node *np = dev->of_node;
1052 
1053 	if (!of_find_property(np, "#clock-cells", NULL))
1054 		return;
1055 
1056 	of_clk_del_provider(dev->of_node);
1057 }
1058 
1059 /**
1060  * sdhci_arasan_register_sdclk - Register the sdcardclk for a PHY to use
1061  *
1062  * Some PHY devices need to know what the actual card clock is.  In order for
1063  * them to find out, we'll provide a clock through the common clock framework
1064  * for them to query.
1065  *
1066  * Note: without seriously re-architecting SDHCI's clock code and testing on
1067  * all platforms, there's no way to create a totally beautiful clock here
1068  * with all clock ops implemented.  Instead, we'll just create a clock that can
1069  * be queried and set the CLK_GET_RATE_NOCACHE attribute to tell common clock
1070  * framework that we're doing things behind its back.  This should be sufficient
1071  * to create nice clean device tree bindings and later (if needed) we can try
1072  * re-architecting SDHCI if we see some benefit to it.
1073  *
1074  * @sdhci_arasan:	Our private data structure.
1075  * @clk_xin:		Pointer to the functional clock
1076  * @dev:		Pointer to our struct device.
1077  * Returns 0 on success and error value on error
1078  */
1079 static int sdhci_arasan_register_sdclk(struct sdhci_arasan_data *sdhci_arasan,
1080 				       struct clk *clk_xin,
1081 				       struct device *dev)
1082 {
1083 	struct device_node *np = dev->of_node;
1084 	u32 num_clks = 0;
1085 	int ret;
1086 
1087 	/* Providing a clock to the PHY is optional; no error if missing */
1088 	if (of_property_read_u32(np, "#clock-cells", &num_clks) < 0)
1089 		return 0;
1090 
1091 	ret = sdhci_arasan_register_sdcardclk(sdhci_arasan, clk_xin, dev);
1092 	if (ret)
1093 		return ret;
1094 
1095 	if (num_clks) {
1096 		ret = sdhci_arasan_register_sampleclk(sdhci_arasan, clk_xin,
1097 						      dev);
1098 		if (ret) {
1099 			sdhci_arasan_unregister_sdclk(dev);
1100 			return ret;
1101 		}
1102 	}
1103 
1104 	return 0;
1105 }
1106 
1107 static int sdhci_arasan_add_host(struct sdhci_arasan_data *sdhci_arasan)
1108 {
1109 	struct sdhci_host *host = sdhci_arasan->host;
1110 	struct cqhci_host *cq_host;
1111 	bool dma64;
1112 	int ret;
1113 
1114 	if (!sdhci_arasan->has_cqe)
1115 		return sdhci_add_host(host);
1116 
1117 	ret = sdhci_setup_host(host);
1118 	if (ret)
1119 		return ret;
1120 
1121 	cq_host = devm_kzalloc(host->mmc->parent,
1122 			       sizeof(*cq_host), GFP_KERNEL);
1123 	if (!cq_host) {
1124 		ret = -ENOMEM;
1125 		goto cleanup;
1126 	}
1127 
1128 	cq_host->mmio = host->ioaddr + SDHCI_ARASAN_CQE_BASE_ADDR;
1129 	cq_host->ops = &sdhci_arasan_cqhci_ops;
1130 
1131 	dma64 = host->flags & SDHCI_USE_64_BIT_DMA;
1132 	if (dma64)
1133 		cq_host->caps |= CQHCI_TASK_DESC_SZ_128;
1134 
1135 	ret = cqhci_init(cq_host, host->mmc, dma64);
1136 	if (ret)
1137 		goto cleanup;
1138 
1139 	ret = __sdhci_add_host(host);
1140 	if (ret)
1141 		goto cleanup;
1142 
1143 	return 0;
1144 
1145 cleanup:
1146 	sdhci_cleanup_host(host);
1147 	return ret;
1148 }
1149 
1150 static int sdhci_arasan_probe(struct platform_device *pdev)
1151 {
1152 	int ret;
1153 	const struct of_device_id *match;
1154 	struct device_node *node;
1155 	struct clk *clk_xin;
1156 	struct sdhci_host *host;
1157 	struct sdhci_pltfm_host *pltfm_host;
1158 	struct sdhci_arasan_data *sdhci_arasan;
1159 	struct device_node *np = pdev->dev.of_node;
1160 	const struct sdhci_arasan_of_data *data;
1161 
1162 	match = of_match_node(sdhci_arasan_of_match, pdev->dev.of_node);
1163 	data = match->data;
1164 	host = sdhci_pltfm_init(pdev, data->pdata, sizeof(*sdhci_arasan));
1165 
1166 	if (IS_ERR(host))
1167 		return PTR_ERR(host);
1168 
1169 	pltfm_host = sdhci_priv(host);
1170 	sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
1171 	sdhci_arasan->host = host;
1172 
1173 	sdhci_arasan->soc_ctl_map = data->soc_ctl_map;
1174 
1175 	node = of_parse_phandle(pdev->dev.of_node, "arasan,soc-ctl-syscon", 0);
1176 	if (node) {
1177 		sdhci_arasan->soc_ctl_base = syscon_node_to_regmap(node);
1178 		of_node_put(node);
1179 
1180 		if (IS_ERR(sdhci_arasan->soc_ctl_base)) {
1181 			ret = PTR_ERR(sdhci_arasan->soc_ctl_base);
1182 			if (ret != -EPROBE_DEFER)
1183 				dev_err(&pdev->dev, "Can't get syscon: %d\n",
1184 					ret);
1185 			goto err_pltfm_free;
1186 		}
1187 	}
1188 
1189 	sdhci_arasan->clk_ahb = devm_clk_get(&pdev->dev, "clk_ahb");
1190 	if (IS_ERR(sdhci_arasan->clk_ahb)) {
1191 		dev_err(&pdev->dev, "clk_ahb clock not found.\n");
1192 		ret = PTR_ERR(sdhci_arasan->clk_ahb);
1193 		goto err_pltfm_free;
1194 	}
1195 
1196 	clk_xin = devm_clk_get(&pdev->dev, "clk_xin");
1197 	if (IS_ERR(clk_xin)) {
1198 		dev_err(&pdev->dev, "clk_xin clock not found.\n");
1199 		ret = PTR_ERR(clk_xin);
1200 		goto err_pltfm_free;
1201 	}
1202 
1203 	ret = clk_prepare_enable(sdhci_arasan->clk_ahb);
1204 	if (ret) {
1205 		dev_err(&pdev->dev, "Unable to enable AHB clock.\n");
1206 		goto err_pltfm_free;
1207 	}
1208 
1209 	ret = clk_prepare_enable(clk_xin);
1210 	if (ret) {
1211 		dev_err(&pdev->dev, "Unable to enable SD clock.\n");
1212 		goto clk_dis_ahb;
1213 	}
1214 
1215 	sdhci_get_of_property(pdev);
1216 
1217 	if (of_property_read_bool(np, "xlnx,fails-without-test-cd"))
1218 		sdhci_arasan->quirks |= SDHCI_ARASAN_QUIRK_FORCE_CDTEST;
1219 
1220 	if (of_property_read_bool(np, "xlnx,int-clock-stable-broken"))
1221 		sdhci_arasan->quirks |= SDHCI_ARASAN_QUIRK_CLOCK_UNSTABLE;
1222 
1223 	pltfm_host->clk = clk_xin;
1224 
1225 	if (of_device_is_compatible(pdev->dev.of_node,
1226 				    "rockchip,rk3399-sdhci-5.1"))
1227 		sdhci_arasan_update_clockmultiplier(host, 0x0);
1228 
1229 	sdhci_arasan_update_baseclkfreq(host);
1230 
1231 	ret = sdhci_arasan_register_sdclk(sdhci_arasan, clk_xin, &pdev->dev);
1232 	if (ret)
1233 		goto clk_disable_all;
1234 
1235 	if (of_device_is_compatible(np, "xlnx,zynqmp-8.9a")) {
1236 		struct sdhci_arasan_zynqmp_clk_data *zynqmp_clk_data;
1237 		const struct zynqmp_eemi_ops *eemi_ops;
1238 
1239 		zynqmp_clk_data = devm_kzalloc(&pdev->dev,
1240 					       sizeof(*zynqmp_clk_data),
1241 					       GFP_KERNEL);
1242 		eemi_ops = zynqmp_pm_get_eemi_ops();
1243 		if (IS_ERR(eemi_ops)) {
1244 			ret = PTR_ERR(eemi_ops);
1245 			goto unreg_clk;
1246 		}
1247 
1248 		zynqmp_clk_data->eemi_ops = eemi_ops;
1249 		sdhci_arasan->clk_data.clk_of_data = zynqmp_clk_data;
1250 	}
1251 
1252 	arasan_dt_parse_clk_phases(&pdev->dev, &sdhci_arasan->clk_data);
1253 
1254 	ret = mmc_of_parse(host->mmc);
1255 	if (ret) {
1256 		if (ret != -EPROBE_DEFER)
1257 			dev_err(&pdev->dev, "parsing dt failed (%d)\n", ret);
1258 		goto unreg_clk;
1259 	}
1260 
1261 	sdhci_arasan->phy = ERR_PTR(-ENODEV);
1262 	if (of_device_is_compatible(pdev->dev.of_node,
1263 				    "arasan,sdhci-5.1")) {
1264 		sdhci_arasan->phy = devm_phy_get(&pdev->dev,
1265 						 "phy_arasan");
1266 		if (IS_ERR(sdhci_arasan->phy)) {
1267 			ret = PTR_ERR(sdhci_arasan->phy);
1268 			dev_err(&pdev->dev, "No phy for arasan,sdhci-5.1.\n");
1269 			goto unreg_clk;
1270 		}
1271 
1272 		ret = phy_init(sdhci_arasan->phy);
1273 		if (ret < 0) {
1274 			dev_err(&pdev->dev, "phy_init err.\n");
1275 			goto unreg_clk;
1276 		}
1277 
1278 		host->mmc_host_ops.hs400_enhanced_strobe =
1279 					sdhci_arasan_hs400_enhanced_strobe;
1280 		host->mmc_host_ops.start_signal_voltage_switch =
1281 					sdhci_arasan_voltage_switch;
1282 		sdhci_arasan->has_cqe = true;
1283 		host->mmc->caps2 |= MMC_CAP2_CQE;
1284 
1285 		if (!of_property_read_bool(np, "disable-cqe-dcmd"))
1286 			host->mmc->caps2 |= MMC_CAP2_CQE_DCMD;
1287 	}
1288 
1289 	ret = sdhci_arasan_add_host(sdhci_arasan);
1290 	if (ret)
1291 		goto err_add_host;
1292 
1293 	return 0;
1294 
1295 err_add_host:
1296 	if (!IS_ERR(sdhci_arasan->phy))
1297 		phy_exit(sdhci_arasan->phy);
1298 unreg_clk:
1299 	sdhci_arasan_unregister_sdclk(&pdev->dev);
1300 clk_disable_all:
1301 	clk_disable_unprepare(clk_xin);
1302 clk_dis_ahb:
1303 	clk_disable_unprepare(sdhci_arasan->clk_ahb);
1304 err_pltfm_free:
1305 	sdhci_pltfm_free(pdev);
1306 	return ret;
1307 }
1308 
1309 static int sdhci_arasan_remove(struct platform_device *pdev)
1310 {
1311 	int ret;
1312 	struct sdhci_host *host = platform_get_drvdata(pdev);
1313 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1314 	struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
1315 	struct clk *clk_ahb = sdhci_arasan->clk_ahb;
1316 
1317 	if (!IS_ERR(sdhci_arasan->phy)) {
1318 		if (sdhci_arasan->is_phy_on)
1319 			phy_power_off(sdhci_arasan->phy);
1320 		phy_exit(sdhci_arasan->phy);
1321 	}
1322 
1323 	sdhci_arasan_unregister_sdclk(&pdev->dev);
1324 
1325 	ret = sdhci_pltfm_unregister(pdev);
1326 
1327 	clk_disable_unprepare(clk_ahb);
1328 
1329 	return ret;
1330 }
1331 
1332 static struct platform_driver sdhci_arasan_driver = {
1333 	.driver = {
1334 		.name = "sdhci-arasan",
1335 		.of_match_table = sdhci_arasan_of_match,
1336 		.pm = &sdhci_arasan_dev_pm_ops,
1337 	},
1338 	.probe = sdhci_arasan_probe,
1339 	.remove = sdhci_arasan_remove,
1340 };
1341 
1342 module_platform_driver(sdhci_arasan_driver);
1343 
1344 MODULE_DESCRIPTION("Driver for the Arasan SDHCI Controller");
1345 MODULE_AUTHOR("Soeren Brinkmann <soren.brinkmann@xilinx.com>");
1346 MODULE_LICENSE("GPL");
1347