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