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