1 /*
2  * Arasan Secure Digital Host Controller Interface.
3  * Copyright (C) 2011 - 2012 Michal Simek <monstr@monstr.eu>
4  * Copyright (c) 2012 Wind River Systems, Inc.
5  * Copyright (C) 2013 Pengutronix e.K.
6  * Copyright (C) 2013 Xilinx Inc.
7  *
8  * Based on sdhci-of-esdhc.c
9  *
10  * Copyright (c) 2007 Freescale Semiconductor, Inc.
11  * Copyright (c) 2009 MontaVista Software, Inc.
12  *
13  * Authors: Xiaobo Xie <X.Xie@freescale.com>
14  *	    Anton Vorontsov <avorontsov@ru.mvista.com>
15  *
16  * This program is free software; you can redistribute it and/or modify
17  * it under the terms of the GNU General Public License as published by
18  * the Free Software Foundation; either version 2 of the License, or (at
19  * your option) any later version.
20  */
21 
22 #include <linux/clk-provider.h>
23 #include <linux/mfd/syscon.h>
24 #include <linux/module.h>
25 #include <linux/of_device.h>
26 #include <linux/phy/phy.h>
27 #include <linux/regmap.h>
28 #include <linux/of.h>
29 
30 #include "cqhci.h"
31 #include "sdhci-pltfm.h"
32 
33 #define SDHCI_ARASAN_VENDOR_REGISTER	0x78
34 #define SDHCI_ARASAN_CQE_BASE_ADDR	0x200
35 #define VENDOR_ENHANCED_STROBE		BIT(0)
36 
37 #define PHY_CLK_TOO_SLOW_HZ		400000
38 
39 /*
40  * On some SoCs the syscon area has a feature where the upper 16-bits of
41  * each 32-bit register act as a write mask for the lower 16-bits.  This allows
42  * atomic updates of the register without locking.  This macro is used on SoCs
43  * that have that feature.
44  */
45 #define HIWORD_UPDATE(val, mask, shift) \
46 		((val) << (shift) | (mask) << ((shift) + 16))
47 
48 /**
49  * struct sdhci_arasan_soc_ctl_field - Field used in sdhci_arasan_soc_ctl_map
50  *
51  * @reg:	Offset within the syscon of the register containing this field
52  * @width:	Number of bits for this field
53  * @shift:	Bit offset within @reg of this field (or -1 if not avail)
54  */
55 struct sdhci_arasan_soc_ctl_field {
56 	u32 reg;
57 	u16 width;
58 	s16 shift;
59 };
60 
61 /**
62  * struct sdhci_arasan_soc_ctl_map - Map in syscon to corecfg registers
63  *
64  * It's up to the licensee of the Arsan IP block to make these available
65  * somewhere if needed.  Presumably these will be scattered somewhere that's
66  * accessible via the syscon API.
67  *
68  * @baseclkfreq:	Where to find corecfg_baseclkfreq
69  * @clockmultiplier:	Where to find corecfg_clockmultiplier
70  * @hiword_update:	If true, use HIWORD_UPDATE to access the syscon
71  */
72 struct sdhci_arasan_soc_ctl_map {
73 	struct sdhci_arasan_soc_ctl_field	baseclkfreq;
74 	struct sdhci_arasan_soc_ctl_field	clockmultiplier;
75 	bool					hiword_update;
76 };
77 
78 /**
79  * struct sdhci_arasan_data
80  * @host:		Pointer to the main SDHCI host structure.
81  * @clk_ahb:		Pointer to the AHB clock
82  * @phy:		Pointer to the generic phy
83  * @is_phy_on:		True if the PHY is on; false if not.
84  * @sdcardclk_hw:	Struct for the clock we might provide to a PHY.
85  * @sdcardclk:		Pointer to normal 'struct clock' for sdcardclk_hw.
86  * @soc_ctl_base:	Pointer to regmap for syscon for soc_ctl registers.
87  * @soc_ctl_map:	Map to get offsets into soc_ctl registers.
88  */
89 struct sdhci_arasan_data {
90 	struct sdhci_host *host;
91 	struct clk	*clk_ahb;
92 	struct phy	*phy;
93 	bool		is_phy_on;
94 
95 	bool		has_cqe;
96 	struct clk_hw	sdcardclk_hw;
97 	struct clk      *sdcardclk;
98 
99 	struct regmap	*soc_ctl_base;
100 	const struct sdhci_arasan_soc_ctl_map *soc_ctl_map;
101 	unsigned int	quirks; /* Arasan deviations from spec */
102 
103 /* Controller does not have CD wired and will not function normally without */
104 #define SDHCI_ARASAN_QUIRK_FORCE_CDTEST	BIT(0)
105 };
106 
107 static const struct sdhci_arasan_soc_ctl_map rk3399_soc_ctl_map = {
108 	.baseclkfreq = { .reg = 0xf000, .width = 8, .shift = 8 },
109 	.clockmultiplier = { .reg = 0xf02c, .width = 8, .shift = 0},
110 	.hiword_update = true,
111 };
112 
113 /**
114  * sdhci_arasan_syscon_write - Write to a field in soc_ctl registers
115  *
116  * This function allows writing to fields in sdhci_arasan_soc_ctl_map.
117  * Note that if a field is specified as not available (shift < 0) then
118  * this function will silently return an error code.  It will be noisy
119  * and print errors for any other (unexpected) errors.
120  *
121  * @host:	The sdhci_host
122  * @fld:	The field to write to
123  * @val:	The value to write
124  */
125 static int sdhci_arasan_syscon_write(struct sdhci_host *host,
126 				   const struct sdhci_arasan_soc_ctl_field *fld,
127 				   u32 val)
128 {
129 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
130 	struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
131 	struct regmap *soc_ctl_base = sdhci_arasan->soc_ctl_base;
132 	u32 reg = fld->reg;
133 	u16 width = fld->width;
134 	s16 shift = fld->shift;
135 	int ret;
136 
137 	/*
138 	 * Silently return errors for shift < 0 so caller doesn't have
139 	 * to check for fields which are optional.  For fields that
140 	 * are required then caller needs to do something special
141 	 * anyway.
142 	 */
143 	if (shift < 0)
144 		return -EINVAL;
145 
146 	if (sdhci_arasan->soc_ctl_map->hiword_update)
147 		ret = regmap_write(soc_ctl_base, reg,
148 				   HIWORD_UPDATE(val, GENMASK(width, 0),
149 						 shift));
150 	else
151 		ret = regmap_update_bits(soc_ctl_base, reg,
152 					 GENMASK(shift + width, shift),
153 					 val << shift);
154 
155 	/* Yell about (unexpected) regmap errors */
156 	if (ret)
157 		pr_warn("%s: Regmap write fail: %d\n",
158 			 mmc_hostname(host->mmc), ret);
159 
160 	return ret;
161 }
162 
163 static void sdhci_arasan_set_clock(struct sdhci_host *host, unsigned int clock)
164 {
165 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
166 	struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
167 	bool ctrl_phy = false;
168 
169 	if (!IS_ERR(sdhci_arasan->phy)) {
170 		if (!sdhci_arasan->is_phy_on && clock <= PHY_CLK_TOO_SLOW_HZ) {
171 			/*
172 			 * If PHY off, set clock to max speed and power PHY on.
173 			 *
174 			 * Although PHY docs apparently suggest power cycling
175 			 * when changing the clock the PHY doesn't like to be
176 			 * powered on while at low speeds like those used in ID
177 			 * mode.  Even worse is powering the PHY on while the
178 			 * clock is off.
179 			 *
180 			 * To workaround the PHY limitations, the best we can
181 			 * do is to power it on at a faster speed and then slam
182 			 * through low speeds without power cycling.
183 			 */
184 			sdhci_set_clock(host, host->max_clk);
185 			phy_power_on(sdhci_arasan->phy);
186 			sdhci_arasan->is_phy_on = true;
187 
188 			/*
189 			 * We'll now fall through to the below case with
190 			 * ctrl_phy = false (so we won't turn off/on).  The
191 			 * sdhci_set_clock() will set the real clock.
192 			 */
193 		} else if (clock > PHY_CLK_TOO_SLOW_HZ) {
194 			/*
195 			 * At higher clock speeds the PHY is fine being power
196 			 * cycled and docs say you _should_ power cycle when
197 			 * changing clock speeds.
198 			 */
199 			ctrl_phy = true;
200 		}
201 	}
202 
203 	if (ctrl_phy && sdhci_arasan->is_phy_on) {
204 		phy_power_off(sdhci_arasan->phy);
205 		sdhci_arasan->is_phy_on = false;
206 	}
207 
208 	sdhci_set_clock(host, clock);
209 
210 	if (ctrl_phy) {
211 		phy_power_on(sdhci_arasan->phy);
212 		sdhci_arasan->is_phy_on = true;
213 	}
214 }
215 
216 static void sdhci_arasan_hs400_enhanced_strobe(struct mmc_host *mmc,
217 					struct mmc_ios *ios)
218 {
219 	u32 vendor;
220 	struct sdhci_host *host = mmc_priv(mmc);
221 
222 	vendor = sdhci_readl(host, SDHCI_ARASAN_VENDOR_REGISTER);
223 	if (ios->enhanced_strobe)
224 		vendor |= VENDOR_ENHANCED_STROBE;
225 	else
226 		vendor &= ~VENDOR_ENHANCED_STROBE;
227 
228 	sdhci_writel(host, vendor, SDHCI_ARASAN_VENDOR_REGISTER);
229 }
230 
231 static void sdhci_arasan_reset(struct sdhci_host *host, u8 mask)
232 {
233 	u8 ctrl;
234 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
235 	struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
236 
237 	sdhci_reset(host, mask);
238 
239 	if (sdhci_arasan->quirks & SDHCI_ARASAN_QUIRK_FORCE_CDTEST) {
240 		ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
241 		ctrl |= SDHCI_CTRL_CDTEST_INS | SDHCI_CTRL_CDTEST_EN;
242 		sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
243 	}
244 }
245 
246 static int sdhci_arasan_voltage_switch(struct mmc_host *mmc,
247 				       struct mmc_ios *ios)
248 {
249 	switch (ios->signal_voltage) {
250 	case MMC_SIGNAL_VOLTAGE_180:
251 		/*
252 		 * Plese don't switch to 1V8 as arasan,5.1 doesn't
253 		 * actually refer to this setting to indicate the
254 		 * signal voltage and the state machine will be broken
255 		 * actually if we force to enable 1V8. That's something
256 		 * like broken quirk but we could work around here.
257 		 */
258 		return 0;
259 	case MMC_SIGNAL_VOLTAGE_330:
260 	case MMC_SIGNAL_VOLTAGE_120:
261 		/* We don't support 3V3 and 1V2 */
262 		break;
263 	}
264 
265 	return -EINVAL;
266 }
267 
268 static void sdhci_arasan_set_power(struct sdhci_host *host, unsigned char mode,
269 		     unsigned short vdd)
270 {
271 	if (!IS_ERR(host->mmc->supply.vmmc)) {
272 		struct mmc_host *mmc = host->mmc;
273 
274 		mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, vdd);
275 	}
276 	sdhci_set_power_noreg(host, mode, vdd);
277 }
278 
279 static const struct sdhci_ops sdhci_arasan_ops = {
280 	.set_clock = sdhci_arasan_set_clock,
281 	.get_max_clock = sdhci_pltfm_clk_get_max_clock,
282 	.get_timeout_clock = sdhci_pltfm_clk_get_max_clock,
283 	.set_bus_width = sdhci_set_bus_width,
284 	.reset = sdhci_arasan_reset,
285 	.set_uhs_signaling = sdhci_set_uhs_signaling,
286 	.set_power = sdhci_arasan_set_power,
287 };
288 
289 static const struct sdhci_pltfm_data sdhci_arasan_pdata = {
290 	.ops = &sdhci_arasan_ops,
291 	.quirks = SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN,
292 	.quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN |
293 			SDHCI_QUIRK2_CLOCK_DIV_ZERO_BROKEN,
294 };
295 
296 static u32 sdhci_arasan_cqhci_irq(struct sdhci_host *host, u32 intmask)
297 {
298 	int cmd_error = 0;
299 	int data_error = 0;
300 
301 	if (!sdhci_cqe_irq(host, intmask, &cmd_error, &data_error))
302 		return intmask;
303 
304 	cqhci_irq(host->mmc, intmask, cmd_error, data_error);
305 
306 	return 0;
307 }
308 
309 static void sdhci_arasan_dumpregs(struct mmc_host *mmc)
310 {
311 	sdhci_dumpregs(mmc_priv(mmc));
312 }
313 
314 static void sdhci_arasan_cqe_enable(struct mmc_host *mmc)
315 {
316 	struct sdhci_host *host = mmc_priv(mmc);
317 	u32 reg;
318 
319 	reg = sdhci_readl(host, SDHCI_PRESENT_STATE);
320 	while (reg & SDHCI_DATA_AVAILABLE) {
321 		sdhci_readl(host, SDHCI_BUFFER);
322 		reg = sdhci_readl(host, SDHCI_PRESENT_STATE);
323 	}
324 
325 	sdhci_cqe_enable(mmc);
326 }
327 
328 static const struct cqhci_host_ops sdhci_arasan_cqhci_ops = {
329 	.enable         = sdhci_arasan_cqe_enable,
330 	.disable        = sdhci_cqe_disable,
331 	.dumpregs       = sdhci_arasan_dumpregs,
332 };
333 
334 static const struct sdhci_ops sdhci_arasan_cqe_ops = {
335 	.set_clock = sdhci_arasan_set_clock,
336 	.get_max_clock = sdhci_pltfm_clk_get_max_clock,
337 	.get_timeout_clock = sdhci_pltfm_clk_get_max_clock,
338 	.set_bus_width = sdhci_set_bus_width,
339 	.reset = sdhci_arasan_reset,
340 	.set_uhs_signaling = sdhci_set_uhs_signaling,
341 	.set_power = sdhci_arasan_set_power,
342 	.irq = sdhci_arasan_cqhci_irq,
343 };
344 
345 static const struct sdhci_pltfm_data sdhci_arasan_cqe_pdata = {
346 	.ops = &sdhci_arasan_cqe_ops,
347 	.quirks = SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN,
348 	.quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN |
349 			SDHCI_QUIRK2_CLOCK_DIV_ZERO_BROKEN,
350 };
351 
352 #ifdef CONFIG_PM_SLEEP
353 /**
354  * sdhci_arasan_suspend - Suspend method for the driver
355  * @dev:	Address of the device structure
356  * Returns 0 on success and error value on error
357  *
358  * Put the device in a low power state.
359  */
360 static int sdhci_arasan_suspend(struct device *dev)
361 {
362 	struct platform_device *pdev = to_platform_device(dev);
363 	struct sdhci_host *host = platform_get_drvdata(pdev);
364 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
365 	struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
366 	int ret;
367 
368 	if (host->tuning_mode != SDHCI_TUNING_MODE_3)
369 		mmc_retune_needed(host->mmc);
370 
371 	if (sdhci_arasan->has_cqe) {
372 		ret = cqhci_suspend(host->mmc);
373 		if (ret)
374 			return ret;
375 	}
376 
377 	ret = sdhci_suspend_host(host);
378 	if (ret)
379 		return ret;
380 
381 	if (!IS_ERR(sdhci_arasan->phy) && sdhci_arasan->is_phy_on) {
382 		ret = phy_power_off(sdhci_arasan->phy);
383 		if (ret) {
384 			dev_err(dev, "Cannot power off phy.\n");
385 			sdhci_resume_host(host);
386 			return ret;
387 		}
388 		sdhci_arasan->is_phy_on = false;
389 	}
390 
391 	clk_disable(pltfm_host->clk);
392 	clk_disable(sdhci_arasan->clk_ahb);
393 
394 	return 0;
395 }
396 
397 /**
398  * sdhci_arasan_resume - Resume method for the driver
399  * @dev:	Address of the device structure
400  * Returns 0 on success and error value on error
401  *
402  * Resume operation after suspend
403  */
404 static int sdhci_arasan_resume(struct device *dev)
405 {
406 	struct platform_device *pdev = to_platform_device(dev);
407 	struct sdhci_host *host = platform_get_drvdata(pdev);
408 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
409 	struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
410 	int ret;
411 
412 	ret = clk_enable(sdhci_arasan->clk_ahb);
413 	if (ret) {
414 		dev_err(dev, "Cannot enable AHB clock.\n");
415 		return ret;
416 	}
417 
418 	ret = clk_enable(pltfm_host->clk);
419 	if (ret) {
420 		dev_err(dev, "Cannot enable SD clock.\n");
421 		return ret;
422 	}
423 
424 	if (!IS_ERR(sdhci_arasan->phy) && host->mmc->actual_clock) {
425 		ret = phy_power_on(sdhci_arasan->phy);
426 		if (ret) {
427 			dev_err(dev, "Cannot power on phy.\n");
428 			return ret;
429 		}
430 		sdhci_arasan->is_phy_on = true;
431 	}
432 
433 	ret = sdhci_resume_host(host);
434 	if (ret) {
435 		dev_err(dev, "Cannot resume host.\n");
436 		return ret;
437 	}
438 
439 	if (sdhci_arasan->has_cqe)
440 		return cqhci_resume(host->mmc);
441 
442 	return 0;
443 }
444 #endif /* ! CONFIG_PM_SLEEP */
445 
446 static SIMPLE_DEV_PM_OPS(sdhci_arasan_dev_pm_ops, sdhci_arasan_suspend,
447 			 sdhci_arasan_resume);
448 
449 static const struct of_device_id sdhci_arasan_of_match[] = {
450 	/* SoC-specific compatible strings w/ soc_ctl_map */
451 	{
452 		.compatible = "rockchip,rk3399-sdhci-5.1",
453 		.data = &rk3399_soc_ctl_map,
454 	},
455 
456 	/* Generic compatible below here */
457 	{ .compatible = "arasan,sdhci-8.9a" },
458 	{ .compatible = "arasan,sdhci-5.1" },
459 	{ .compatible = "arasan,sdhci-4.9a" },
460 
461 	{ /* sentinel */ }
462 };
463 MODULE_DEVICE_TABLE(of, sdhci_arasan_of_match);
464 
465 /**
466  * sdhci_arasan_sdcardclk_recalc_rate - Return the card clock rate
467  *
468  * Return the current actual rate of the SD card clock.  This can be used
469  * to communicate with out PHY.
470  *
471  * @hw:			Pointer to the hardware clock structure.
472  * @parent_rate		The parent rate (should be rate of clk_xin).
473  * Returns the card clock rate.
474  */
475 static unsigned long sdhci_arasan_sdcardclk_recalc_rate(struct clk_hw *hw,
476 						      unsigned long parent_rate)
477 
478 {
479 	struct sdhci_arasan_data *sdhci_arasan =
480 		container_of(hw, struct sdhci_arasan_data, sdcardclk_hw);
481 	struct sdhci_host *host = sdhci_arasan->host;
482 
483 	return host->mmc->actual_clock;
484 }
485 
486 static const struct clk_ops arasan_sdcardclk_ops = {
487 	.recalc_rate = sdhci_arasan_sdcardclk_recalc_rate,
488 };
489 
490 /**
491  * sdhci_arasan_update_clockmultiplier - Set corecfg_clockmultiplier
492  *
493  * The corecfg_clockmultiplier is supposed to contain clock multiplier
494  * value of programmable clock generator.
495  *
496  * NOTES:
497  * - Many existing devices don't seem to do this and work fine.  To keep
498  *   compatibility for old hardware where the device tree doesn't provide a
499  *   register map, this function is a noop if a soc_ctl_map hasn't been provided
500  *   for this platform.
501  * - The value of corecfg_clockmultiplier should sync with that of corresponding
502  *   value reading from sdhci_capability_register. So this function is called
503  *   once at probe time and never called again.
504  *
505  * @host:		The sdhci_host
506  */
507 static void sdhci_arasan_update_clockmultiplier(struct sdhci_host *host,
508 						u32 value)
509 {
510 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
511 	struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
512 	const struct sdhci_arasan_soc_ctl_map *soc_ctl_map =
513 		sdhci_arasan->soc_ctl_map;
514 
515 	/* Having a map is optional */
516 	if (!soc_ctl_map)
517 		return;
518 
519 	/* If we have a map, we expect to have a syscon */
520 	if (!sdhci_arasan->soc_ctl_base) {
521 		pr_warn("%s: Have regmap, but no soc-ctl-syscon\n",
522 			mmc_hostname(host->mmc));
523 		return;
524 	}
525 
526 	sdhci_arasan_syscon_write(host, &soc_ctl_map->clockmultiplier, value);
527 }
528 
529 /**
530  * sdhci_arasan_update_baseclkfreq - Set corecfg_baseclkfreq
531  *
532  * The corecfg_baseclkfreq is supposed to contain the MHz of clk_xin.  This
533  * function can be used to make that happen.
534  *
535  * NOTES:
536  * - Many existing devices don't seem to do this and work fine.  To keep
537  *   compatibility for old hardware where the device tree doesn't provide a
538  *   register map, this function is a noop if a soc_ctl_map hasn't been provided
539  *   for this platform.
540  * - It's assumed that clk_xin is not dynamic and that we use the SDHCI divider
541  *   to achieve lower clock rates.  That means that this function is called once
542  *   at probe time and never called again.
543  *
544  * @host:		The sdhci_host
545  */
546 static void sdhci_arasan_update_baseclkfreq(struct sdhci_host *host)
547 {
548 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
549 	struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
550 	const struct sdhci_arasan_soc_ctl_map *soc_ctl_map =
551 		sdhci_arasan->soc_ctl_map;
552 	u32 mhz = DIV_ROUND_CLOSEST(clk_get_rate(pltfm_host->clk), 1000000);
553 
554 	/* Having a map is optional */
555 	if (!soc_ctl_map)
556 		return;
557 
558 	/* If we have a map, we expect to have a syscon */
559 	if (!sdhci_arasan->soc_ctl_base) {
560 		pr_warn("%s: Have regmap, but no soc-ctl-syscon\n",
561 			mmc_hostname(host->mmc));
562 		return;
563 	}
564 
565 	sdhci_arasan_syscon_write(host, &soc_ctl_map->baseclkfreq, mhz);
566 }
567 
568 /**
569  * sdhci_arasan_register_sdclk - Register the sdclk for a PHY to use
570  *
571  * Some PHY devices need to know what the actual card clock is.  In order for
572  * them to find out, we'll provide a clock through the common clock framework
573  * for them to query.
574  *
575  * Note: without seriously re-architecting SDHCI's clock code and testing on
576  * all platforms, there's no way to create a totally beautiful clock here
577  * with all clock ops implemented.  Instead, we'll just create a clock that can
578  * be queried and set the CLK_GET_RATE_NOCACHE attribute to tell common clock
579  * framework that we're doing things behind its back.  This should be sufficient
580  * to create nice clean device tree bindings and later (if needed) we can try
581  * re-architecting SDHCI if we see some benefit to it.
582  *
583  * @sdhci_arasan:	Our private data structure.
584  * @clk_xin:		Pointer to the functional clock
585  * @dev:		Pointer to our struct device.
586  * Returns 0 on success and error value on error
587  */
588 static int sdhci_arasan_register_sdclk(struct sdhci_arasan_data *sdhci_arasan,
589 				       struct clk *clk_xin,
590 				       struct device *dev)
591 {
592 	struct device_node *np = dev->of_node;
593 	struct clk_init_data sdcardclk_init;
594 	const char *parent_clk_name;
595 	int ret;
596 
597 	/* Providing a clock to the PHY is optional; no error if missing */
598 	if (!of_find_property(np, "#clock-cells", NULL))
599 		return 0;
600 
601 	ret = of_property_read_string_index(np, "clock-output-names", 0,
602 					    &sdcardclk_init.name);
603 	if (ret) {
604 		dev_err(dev, "DT has #clock-cells but no clock-output-names\n");
605 		return ret;
606 	}
607 
608 	parent_clk_name = __clk_get_name(clk_xin);
609 	sdcardclk_init.parent_names = &parent_clk_name;
610 	sdcardclk_init.num_parents = 1;
611 	sdcardclk_init.flags = CLK_GET_RATE_NOCACHE;
612 	sdcardclk_init.ops = &arasan_sdcardclk_ops;
613 
614 	sdhci_arasan->sdcardclk_hw.init = &sdcardclk_init;
615 	sdhci_arasan->sdcardclk =
616 		devm_clk_register(dev, &sdhci_arasan->sdcardclk_hw);
617 	sdhci_arasan->sdcardclk_hw.init = NULL;
618 
619 	ret = of_clk_add_provider(np, of_clk_src_simple_get,
620 				  sdhci_arasan->sdcardclk);
621 	if (ret)
622 		dev_err(dev, "Failed to add clock provider\n");
623 
624 	return ret;
625 }
626 
627 /**
628  * sdhci_arasan_unregister_sdclk - Undoes sdhci_arasan_register_sdclk()
629  *
630  * Should be called any time we're exiting and sdhci_arasan_register_sdclk()
631  * returned success.
632  *
633  * @dev:		Pointer to our struct device.
634  */
635 static void sdhci_arasan_unregister_sdclk(struct device *dev)
636 {
637 	struct device_node *np = dev->of_node;
638 
639 	if (!of_find_property(np, "#clock-cells", NULL))
640 		return;
641 
642 	of_clk_del_provider(dev->of_node);
643 }
644 
645 static int sdhci_arasan_add_host(struct sdhci_arasan_data *sdhci_arasan)
646 {
647 	struct sdhci_host *host = sdhci_arasan->host;
648 	struct cqhci_host *cq_host;
649 	bool dma64;
650 	int ret;
651 
652 	if (!sdhci_arasan->has_cqe)
653 		return sdhci_add_host(host);
654 
655 	ret = sdhci_setup_host(host);
656 	if (ret)
657 		return ret;
658 
659 	cq_host = devm_kzalloc(host->mmc->parent,
660 			       sizeof(*cq_host), GFP_KERNEL);
661 	if (!cq_host) {
662 		ret = -ENOMEM;
663 		goto cleanup;
664 	}
665 
666 	cq_host->mmio = host->ioaddr + SDHCI_ARASAN_CQE_BASE_ADDR;
667 	cq_host->ops = &sdhci_arasan_cqhci_ops;
668 
669 	dma64 = host->flags & SDHCI_USE_64_BIT_DMA;
670 	if (dma64)
671 		cq_host->caps |= CQHCI_TASK_DESC_SZ_128;
672 
673 	ret = cqhci_init(cq_host, host->mmc, dma64);
674 	if (ret)
675 		goto cleanup;
676 
677 	ret = __sdhci_add_host(host);
678 	if (ret)
679 		goto cleanup;
680 
681 	return 0;
682 
683 cleanup:
684 	sdhci_cleanup_host(host);
685 	return ret;
686 }
687 
688 static int sdhci_arasan_probe(struct platform_device *pdev)
689 {
690 	int ret;
691 	const struct of_device_id *match;
692 	struct device_node *node;
693 	struct clk *clk_xin;
694 	struct sdhci_host *host;
695 	struct sdhci_pltfm_host *pltfm_host;
696 	struct sdhci_arasan_data *sdhci_arasan;
697 	struct device_node *np = pdev->dev.of_node;
698 	const struct sdhci_pltfm_data *pdata;
699 
700 	if (of_device_is_compatible(pdev->dev.of_node, "arasan,sdhci-5.1"))
701 		pdata = &sdhci_arasan_cqe_pdata;
702 	else
703 		pdata = &sdhci_arasan_pdata;
704 
705 	host = sdhci_pltfm_init(pdev, pdata, sizeof(*sdhci_arasan));
706 
707 	if (IS_ERR(host))
708 		return PTR_ERR(host);
709 
710 	pltfm_host = sdhci_priv(host);
711 	sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
712 	sdhci_arasan->host = host;
713 
714 	match = of_match_node(sdhci_arasan_of_match, pdev->dev.of_node);
715 	sdhci_arasan->soc_ctl_map = match->data;
716 
717 	node = of_parse_phandle(pdev->dev.of_node, "arasan,soc-ctl-syscon", 0);
718 	if (node) {
719 		sdhci_arasan->soc_ctl_base = syscon_node_to_regmap(node);
720 		of_node_put(node);
721 
722 		if (IS_ERR(sdhci_arasan->soc_ctl_base)) {
723 			ret = PTR_ERR(sdhci_arasan->soc_ctl_base);
724 			if (ret != -EPROBE_DEFER)
725 				dev_err(&pdev->dev, "Can't get syscon: %d\n",
726 					ret);
727 			goto err_pltfm_free;
728 		}
729 	}
730 
731 	sdhci_arasan->clk_ahb = devm_clk_get(&pdev->dev, "clk_ahb");
732 	if (IS_ERR(sdhci_arasan->clk_ahb)) {
733 		dev_err(&pdev->dev, "clk_ahb clock not found.\n");
734 		ret = PTR_ERR(sdhci_arasan->clk_ahb);
735 		goto err_pltfm_free;
736 	}
737 
738 	clk_xin = devm_clk_get(&pdev->dev, "clk_xin");
739 	if (IS_ERR(clk_xin)) {
740 		dev_err(&pdev->dev, "clk_xin clock not found.\n");
741 		ret = PTR_ERR(clk_xin);
742 		goto err_pltfm_free;
743 	}
744 
745 	ret = clk_prepare_enable(sdhci_arasan->clk_ahb);
746 	if (ret) {
747 		dev_err(&pdev->dev, "Unable to enable AHB clock.\n");
748 		goto err_pltfm_free;
749 	}
750 
751 	ret = clk_prepare_enable(clk_xin);
752 	if (ret) {
753 		dev_err(&pdev->dev, "Unable to enable SD clock.\n");
754 		goto clk_dis_ahb;
755 	}
756 
757 	sdhci_get_of_property(pdev);
758 
759 	if (of_property_read_bool(np, "xlnx,fails-without-test-cd"))
760 		sdhci_arasan->quirks |= SDHCI_ARASAN_QUIRK_FORCE_CDTEST;
761 
762 	pltfm_host->clk = clk_xin;
763 
764 	if (of_device_is_compatible(pdev->dev.of_node,
765 				    "rockchip,rk3399-sdhci-5.1"))
766 		sdhci_arasan_update_clockmultiplier(host, 0x0);
767 
768 	sdhci_arasan_update_baseclkfreq(host);
769 
770 	ret = sdhci_arasan_register_sdclk(sdhci_arasan, clk_xin, &pdev->dev);
771 	if (ret)
772 		goto clk_disable_all;
773 
774 	ret = mmc_of_parse(host->mmc);
775 	if (ret) {
776 		dev_err(&pdev->dev, "parsing dt failed (%d)\n", ret);
777 		goto unreg_clk;
778 	}
779 
780 	sdhci_arasan->phy = ERR_PTR(-ENODEV);
781 	if (of_device_is_compatible(pdev->dev.of_node,
782 				    "arasan,sdhci-5.1")) {
783 		sdhci_arasan->phy = devm_phy_get(&pdev->dev,
784 						 "phy_arasan");
785 		if (IS_ERR(sdhci_arasan->phy)) {
786 			ret = PTR_ERR(sdhci_arasan->phy);
787 			dev_err(&pdev->dev, "No phy for arasan,sdhci-5.1.\n");
788 			goto unreg_clk;
789 		}
790 
791 		ret = phy_init(sdhci_arasan->phy);
792 		if (ret < 0) {
793 			dev_err(&pdev->dev, "phy_init err.\n");
794 			goto unreg_clk;
795 		}
796 
797 		host->mmc_host_ops.hs400_enhanced_strobe =
798 					sdhci_arasan_hs400_enhanced_strobe;
799 		host->mmc_host_ops.start_signal_voltage_switch =
800 					sdhci_arasan_voltage_switch;
801 		sdhci_arasan->has_cqe = true;
802 		host->mmc->caps2 |= MMC_CAP2_CQE | MMC_CAP2_CQE_DCMD;
803 	}
804 
805 	ret = sdhci_arasan_add_host(sdhci_arasan);
806 	if (ret)
807 		goto err_add_host;
808 
809 	return 0;
810 
811 err_add_host:
812 	if (!IS_ERR(sdhci_arasan->phy))
813 		phy_exit(sdhci_arasan->phy);
814 unreg_clk:
815 	sdhci_arasan_unregister_sdclk(&pdev->dev);
816 clk_disable_all:
817 	clk_disable_unprepare(clk_xin);
818 clk_dis_ahb:
819 	clk_disable_unprepare(sdhci_arasan->clk_ahb);
820 err_pltfm_free:
821 	sdhci_pltfm_free(pdev);
822 	return ret;
823 }
824 
825 static int sdhci_arasan_remove(struct platform_device *pdev)
826 {
827 	int ret;
828 	struct sdhci_host *host = platform_get_drvdata(pdev);
829 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
830 	struct sdhci_arasan_data *sdhci_arasan = sdhci_pltfm_priv(pltfm_host);
831 	struct clk *clk_ahb = sdhci_arasan->clk_ahb;
832 
833 	if (!IS_ERR(sdhci_arasan->phy)) {
834 		if (sdhci_arasan->is_phy_on)
835 			phy_power_off(sdhci_arasan->phy);
836 		phy_exit(sdhci_arasan->phy);
837 	}
838 
839 	sdhci_arasan_unregister_sdclk(&pdev->dev);
840 
841 	ret = sdhci_pltfm_unregister(pdev);
842 
843 	clk_disable_unprepare(clk_ahb);
844 
845 	return ret;
846 }
847 
848 static struct platform_driver sdhci_arasan_driver = {
849 	.driver = {
850 		.name = "sdhci-arasan",
851 		.of_match_table = sdhci_arasan_of_match,
852 		.pm = &sdhci_arasan_dev_pm_ops,
853 	},
854 	.probe = sdhci_arasan_probe,
855 	.remove = sdhci_arasan_remove,
856 };
857 
858 module_platform_driver(sdhci_arasan_driver);
859 
860 MODULE_DESCRIPTION("Driver for the Arasan SDHCI Controller");
861 MODULE_AUTHOR("Soeren Brinkmann <soren.brinkmann@xilinx.com>");
862 MODULE_LICENSE("GPL");
863