• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Driver for Marvell Xenon SDHC as a platform device
4  *
5  * Copyright (C) 2016 Marvell, All Rights Reserved.
6  *
7  * Author:	Hu Ziji <huziji@marvell.com>
8  * Date:	2016-8-24
9  *
10  * Inspired by Jisheng Zhang <jszhang@marvell.com>
11  * Special thanks to Video BG4 project team.
12  */
13 
14 #include <linux/delay.h>
15 #include <linux/ktime.h>
16 #include <linux/module.h>
17 #include <linux/of.h>
18 #include <linux/pm.h>
19 #include <linux/pm_runtime.h>
20 
21 #include "sdhci-pltfm.h"
22 #include "sdhci-xenon.h"
23 
xenon_enable_internal_clk(struct sdhci_host * host)24 static int xenon_enable_internal_clk(struct sdhci_host *host)
25 {
26 	u32 reg;
27 	ktime_t timeout;
28 
29 	reg = sdhci_readl(host, SDHCI_CLOCK_CONTROL);
30 	reg |= SDHCI_CLOCK_INT_EN;
31 	sdhci_writel(host, reg, SDHCI_CLOCK_CONTROL);
32 	/* Wait max 20 ms */
33 	timeout = ktime_add_ms(ktime_get(), 20);
34 	while (1) {
35 		bool timedout = ktime_after(ktime_get(), timeout);
36 
37 		reg = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
38 		if (reg & SDHCI_CLOCK_INT_STABLE)
39 			break;
40 		if (timedout) {
41 			dev_err(mmc_dev(host->mmc), "Internal clock never stabilised.\n");
42 			return -ETIMEDOUT;
43 		}
44 		usleep_range(900, 1100);
45 	}
46 
47 	return 0;
48 }
49 
50 /* Set SDCLK-off-while-idle */
xenon_set_sdclk_off_idle(struct sdhci_host * host,unsigned char sdhc_id,bool enable)51 static void xenon_set_sdclk_off_idle(struct sdhci_host *host,
52 				     unsigned char sdhc_id, bool enable)
53 {
54 	u32 reg;
55 	u32 mask;
56 
57 	reg = sdhci_readl(host, XENON_SYS_OP_CTRL);
58 	/* Get the bit shift basing on the SDHC index */
59 	mask = (0x1 << (XENON_SDCLK_IDLEOFF_ENABLE_SHIFT + sdhc_id));
60 	if (enable)
61 		reg |= mask;
62 	else
63 		reg &= ~mask;
64 
65 	sdhci_writel(host, reg, XENON_SYS_OP_CTRL);
66 }
67 
68 /* Enable/Disable the Auto Clock Gating function */
xenon_set_acg(struct sdhci_host * host,bool enable)69 static void xenon_set_acg(struct sdhci_host *host, bool enable)
70 {
71 	u32 reg;
72 
73 	reg = sdhci_readl(host, XENON_SYS_OP_CTRL);
74 	if (enable)
75 		reg &= ~XENON_AUTO_CLKGATE_DISABLE_MASK;
76 	else
77 		reg |= XENON_AUTO_CLKGATE_DISABLE_MASK;
78 	sdhci_writel(host, reg, XENON_SYS_OP_CTRL);
79 }
80 
81 /* Enable this SDHC */
xenon_enable_sdhc(struct sdhci_host * host,unsigned char sdhc_id)82 static void xenon_enable_sdhc(struct sdhci_host *host,
83 			      unsigned char sdhc_id)
84 {
85 	u32 reg;
86 
87 	reg = sdhci_readl(host, XENON_SYS_OP_CTRL);
88 	reg |= (BIT(sdhc_id) << XENON_SLOT_ENABLE_SHIFT);
89 	sdhci_writel(host, reg, XENON_SYS_OP_CTRL);
90 
91 	host->mmc->caps |= MMC_CAP_WAIT_WHILE_BUSY;
92 	/*
93 	 * Force to clear BUS_TEST to
94 	 * skip bus_test_pre and bus_test_post
95 	 */
96 	host->mmc->caps &= ~MMC_CAP_BUS_WIDTH_TEST;
97 }
98 
99 /* Disable this SDHC */
xenon_disable_sdhc(struct sdhci_host * host,unsigned char sdhc_id)100 static void xenon_disable_sdhc(struct sdhci_host *host,
101 			       unsigned char sdhc_id)
102 {
103 	u32 reg;
104 
105 	reg = sdhci_readl(host, XENON_SYS_OP_CTRL);
106 	reg &= ~(BIT(sdhc_id) << XENON_SLOT_ENABLE_SHIFT);
107 	sdhci_writel(host, reg, XENON_SYS_OP_CTRL);
108 }
109 
110 /* Enable Parallel Transfer Mode */
xenon_enable_sdhc_parallel_tran(struct sdhci_host * host,unsigned char sdhc_id)111 static void xenon_enable_sdhc_parallel_tran(struct sdhci_host *host,
112 					    unsigned char sdhc_id)
113 {
114 	u32 reg;
115 
116 	reg = sdhci_readl(host, XENON_SYS_EXT_OP_CTRL);
117 	reg |= BIT(sdhc_id);
118 	sdhci_writel(host, reg, XENON_SYS_EXT_OP_CTRL);
119 }
120 
121 /* Mask command conflict error */
xenon_mask_cmd_conflict_err(struct sdhci_host * host)122 static void xenon_mask_cmd_conflict_err(struct sdhci_host *host)
123 {
124 	u32  reg;
125 
126 	reg = sdhci_readl(host, XENON_SYS_EXT_OP_CTRL);
127 	reg |= XENON_MASK_CMD_CONFLICT_ERR;
128 	sdhci_writel(host, reg, XENON_SYS_EXT_OP_CTRL);
129 }
130 
xenon_retune_setup(struct sdhci_host * host)131 static void xenon_retune_setup(struct sdhci_host *host)
132 {
133 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
134 	struct xenon_priv *priv = sdhci_pltfm_priv(pltfm_host);
135 	u32 reg;
136 
137 	/* Disable the Re-Tuning Request functionality */
138 	reg = sdhci_readl(host, XENON_SLOT_RETUNING_REQ_CTRL);
139 	reg &= ~XENON_RETUNING_COMPATIBLE;
140 	sdhci_writel(host, reg, XENON_SLOT_RETUNING_REQ_CTRL);
141 
142 	/* Disable the Re-tuning Interrupt */
143 	reg = sdhci_readl(host, SDHCI_SIGNAL_ENABLE);
144 	reg &= ~SDHCI_INT_RETUNE;
145 	sdhci_writel(host, reg, SDHCI_SIGNAL_ENABLE);
146 	reg = sdhci_readl(host, SDHCI_INT_ENABLE);
147 	reg &= ~SDHCI_INT_RETUNE;
148 	sdhci_writel(host, reg, SDHCI_INT_ENABLE);
149 
150 	/* Force to use Tuning Mode 1 */
151 	host->tuning_mode = SDHCI_TUNING_MODE_1;
152 	/* Set re-tuning period */
153 	host->tuning_count = 1 << (priv->tuning_count - 1);
154 }
155 
156 /*
157  * Operations inside struct sdhci_ops
158  */
159 /* Recover the Register Setting cleared during SOFTWARE_RESET_ALL */
xenon_reset_exit(struct sdhci_host * host,unsigned char sdhc_id,u8 mask)160 static void xenon_reset_exit(struct sdhci_host *host,
161 			     unsigned char sdhc_id, u8 mask)
162 {
163 	/* Only SOFTWARE RESET ALL will clear the register setting */
164 	if (!(mask & SDHCI_RESET_ALL))
165 		return;
166 
167 	/* Disable tuning request and auto-retuning again */
168 	xenon_retune_setup(host);
169 
170 	/*
171 	 * The ACG should be turned off at the early init time, in order
172 	 * to solve a possible issues with the 1.8V regulator stabilization.
173 	 * The feature is enabled in later stage.
174 	 */
175 	xenon_set_acg(host, false);
176 
177 	xenon_set_sdclk_off_idle(host, sdhc_id, false);
178 
179 	xenon_mask_cmd_conflict_err(host);
180 }
181 
xenon_reset(struct sdhci_host * host,u8 mask)182 static void xenon_reset(struct sdhci_host *host, u8 mask)
183 {
184 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
185 	struct xenon_priv *priv = sdhci_pltfm_priv(pltfm_host);
186 
187 	sdhci_reset(host, mask);
188 	xenon_reset_exit(host, priv->sdhc_id, mask);
189 }
190 
191 /*
192  * Xenon defines different values for HS200 and HS400
193  * in Host_Control_2
194  */
xenon_set_uhs_signaling(struct sdhci_host * host,unsigned int timing)195 static void xenon_set_uhs_signaling(struct sdhci_host *host,
196 				    unsigned int timing)
197 {
198 	u16 ctrl_2;
199 
200 	ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
201 	/* Select Bus Speed Mode for host */
202 	ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
203 	if (timing == MMC_TIMING_MMC_HS200)
204 		ctrl_2 |= XENON_CTRL_HS200;
205 	else if (timing == MMC_TIMING_UHS_SDR104)
206 		ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
207 	else if (timing == MMC_TIMING_UHS_SDR12)
208 		ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
209 	else if (timing == MMC_TIMING_UHS_SDR25)
210 		ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
211 	else if (timing == MMC_TIMING_UHS_SDR50)
212 		ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
213 	else if ((timing == MMC_TIMING_UHS_DDR50) ||
214 		 (timing == MMC_TIMING_MMC_DDR52))
215 		ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
216 	else if (timing == MMC_TIMING_MMC_HS400)
217 		ctrl_2 |= XENON_CTRL_HS400;
218 	sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
219 }
220 
xenon_set_power(struct sdhci_host * host,unsigned char mode,unsigned short vdd)221 static void xenon_set_power(struct sdhci_host *host, unsigned char mode,
222 		unsigned short vdd)
223 {
224 	struct mmc_host *mmc = host->mmc;
225 	u8 pwr = host->pwr;
226 
227 	sdhci_set_power_noreg(host, mode, vdd);
228 
229 	if (host->pwr == pwr)
230 		return;
231 
232 	if (host->pwr == 0)
233 		vdd = 0;
234 
235 	if (!IS_ERR(mmc->supply.vmmc))
236 		mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, vdd);
237 }
238 
xenon_voltage_switch(struct sdhci_host * host)239 static void xenon_voltage_switch(struct sdhci_host *host)
240 {
241 	/* Wait for 5ms after set 1.8V signal enable bit */
242 	usleep_range(5000, 5500);
243 
244 	/*
245 	 * For some reason the controller's Host Control2 register reports
246 	 * the bit representing 1.8V signaling as 0 when read after it was
247 	 * written as 1. Subsequent read reports 1.
248 	 *
249 	 * Since this may cause some issues, do an empty read of the Host
250 	 * Control2 register here to circumvent this.
251 	 */
252 	sdhci_readw(host, SDHCI_HOST_CONTROL2);
253 }
254 
255 static const struct sdhci_ops sdhci_xenon_ops = {
256 	.voltage_switch		= xenon_voltage_switch,
257 	.set_clock		= sdhci_set_clock,
258 	.set_power		= xenon_set_power,
259 	.set_bus_width		= sdhci_set_bus_width,
260 	.reset			= xenon_reset,
261 	.set_uhs_signaling	= xenon_set_uhs_signaling,
262 	.get_max_clock		= sdhci_pltfm_clk_get_max_clock,
263 };
264 
265 static const struct sdhci_pltfm_data sdhci_xenon_pdata = {
266 	.ops = &sdhci_xenon_ops,
267 	.quirks = SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC |
268 		  SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER |
269 		  SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN,
270 };
271 
272 /*
273  * Xenon Specific Operations in mmc_host_ops
274  */
xenon_set_ios(struct mmc_host * mmc,struct mmc_ios * ios)275 static void xenon_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
276 {
277 	struct sdhci_host *host = mmc_priv(mmc);
278 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
279 	struct xenon_priv *priv = sdhci_pltfm_priv(pltfm_host);
280 	u32 reg;
281 
282 	/*
283 	 * HS400/HS200/eMMC HS doesn't have Preset Value register.
284 	 * However, sdhci_set_ios will read HS400/HS200 Preset register.
285 	 * Disable Preset Value register for HS400/HS200.
286 	 * eMMC HS with preset_enabled set will trigger a bug in
287 	 * get_preset_value().
288 	 */
289 	if ((ios->timing == MMC_TIMING_MMC_HS400) ||
290 	    (ios->timing == MMC_TIMING_MMC_HS200) ||
291 	    (ios->timing == MMC_TIMING_MMC_HS)) {
292 		host->preset_enabled = false;
293 		host->quirks2 |= SDHCI_QUIRK2_PRESET_VALUE_BROKEN;
294 		host->flags &= ~SDHCI_PV_ENABLED;
295 
296 		reg = sdhci_readw(host, SDHCI_HOST_CONTROL2);
297 		reg &= ~SDHCI_CTRL_PRESET_VAL_ENABLE;
298 		sdhci_writew(host, reg, SDHCI_HOST_CONTROL2);
299 	} else {
300 		host->quirks2 &= ~SDHCI_QUIRK2_PRESET_VALUE_BROKEN;
301 	}
302 
303 	sdhci_set_ios(mmc, ios);
304 	xenon_phy_adj(host, ios);
305 
306 	if (host->clock > XENON_DEFAULT_SDCLK_FREQ)
307 		xenon_set_sdclk_off_idle(host, priv->sdhc_id, true);
308 }
309 
xenon_start_signal_voltage_switch(struct mmc_host * mmc,struct mmc_ios * ios)310 static int xenon_start_signal_voltage_switch(struct mmc_host *mmc,
311 					     struct mmc_ios *ios)
312 {
313 	struct sdhci_host *host = mmc_priv(mmc);
314 
315 	/*
316 	 * Before SD/SDIO set signal voltage, SD bus clock should be
317 	 * disabled. However, sdhci_set_clock will also disable the Internal
318 	 * clock in mmc_set_signal_voltage().
319 	 * If Internal clock is disabled, the 3.3V/1.8V bit can not be updated.
320 	 * Thus here manually enable internal clock.
321 	 *
322 	 * After switch completes, it is unnecessary to disable internal clock,
323 	 * since keeping internal clock active obeys SD spec.
324 	 */
325 	xenon_enable_internal_clk(host);
326 
327 	xenon_soc_pad_ctrl(host, ios->signal_voltage);
328 
329 	/*
330 	 * If Vqmmc is fixed on platform, vqmmc regulator should be unavailable.
331 	 * Thus SDHCI_CTRL_VDD_180 bit might not work then.
332 	 * Skip the standard voltage switch to avoid any issue.
333 	 */
334 	if (PTR_ERR(mmc->supply.vqmmc) == -ENODEV)
335 		return 0;
336 
337 	return sdhci_start_signal_voltage_switch(mmc, ios);
338 }
339 
340 /*
341  * Update card type.
342  * priv->init_card_type will be used in PHY timing adjustment.
343  */
xenon_init_card(struct mmc_host * mmc,struct mmc_card * card)344 static void xenon_init_card(struct mmc_host *mmc, struct mmc_card *card)
345 {
346 	struct sdhci_host *host = mmc_priv(mmc);
347 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
348 	struct xenon_priv *priv = sdhci_pltfm_priv(pltfm_host);
349 
350 	/* Update card type*/
351 	priv->init_card_type = card->type;
352 }
353 
xenon_execute_tuning(struct mmc_host * mmc,u32 opcode)354 static int xenon_execute_tuning(struct mmc_host *mmc, u32 opcode)
355 {
356 	struct sdhci_host *host = mmc_priv(mmc);
357 
358 	if (host->timing == MMC_TIMING_UHS_DDR50 ||
359 		host->timing == MMC_TIMING_MMC_DDR52)
360 		return 0;
361 
362 	/*
363 	 * Currently force Xenon driver back to support mode 1 only,
364 	 * even though Xenon might claim to support mode 2 or mode 3.
365 	 * It requires more time to test mode 2/mode 3 on more platforms.
366 	 */
367 	if (host->tuning_mode != SDHCI_TUNING_MODE_1)
368 		xenon_retune_setup(host);
369 
370 	return sdhci_execute_tuning(mmc, opcode);
371 }
372 
xenon_enable_sdio_irq(struct mmc_host * mmc,int enable)373 static void xenon_enable_sdio_irq(struct mmc_host *mmc, int enable)
374 {
375 	struct sdhci_host *host = mmc_priv(mmc);
376 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
377 	struct xenon_priv *priv = sdhci_pltfm_priv(pltfm_host);
378 	u32 reg;
379 	u8 sdhc_id = priv->sdhc_id;
380 
381 	sdhci_enable_sdio_irq(mmc, enable);
382 
383 	if (enable) {
384 		/*
385 		 * Set SDIO Card Inserted indication
386 		 * to enable detecting SDIO async irq.
387 		 */
388 		reg = sdhci_readl(host, XENON_SYS_CFG_INFO);
389 		reg |= (1 << (sdhc_id + XENON_SLOT_TYPE_SDIO_SHIFT));
390 		sdhci_writel(host, reg, XENON_SYS_CFG_INFO);
391 	} else {
392 		/* Clear SDIO Card Inserted indication */
393 		reg = sdhci_readl(host, XENON_SYS_CFG_INFO);
394 		reg &= ~(1 << (sdhc_id + XENON_SLOT_TYPE_SDIO_SHIFT));
395 		sdhci_writel(host, reg, XENON_SYS_CFG_INFO);
396 	}
397 }
398 
xenon_replace_mmc_host_ops(struct sdhci_host * host)399 static void xenon_replace_mmc_host_ops(struct sdhci_host *host)
400 {
401 	host->mmc_host_ops.set_ios = xenon_set_ios;
402 	host->mmc_host_ops.start_signal_voltage_switch =
403 			xenon_start_signal_voltage_switch;
404 	host->mmc_host_ops.init_card = xenon_init_card;
405 	host->mmc_host_ops.execute_tuning = xenon_execute_tuning;
406 	host->mmc_host_ops.enable_sdio_irq = xenon_enable_sdio_irq;
407 }
408 
409 /*
410  * Parse Xenon specific DT properties:
411  * sdhc-id: the index of current SDHC.
412  *	    Refer to XENON_SYS_CFG_INFO register
413  * tun-count: the interval between re-tuning
414  */
xenon_probe_dt(struct platform_device * pdev)415 static int xenon_probe_dt(struct platform_device *pdev)
416 {
417 	struct device_node *np = pdev->dev.of_node;
418 	struct sdhci_host *host = platform_get_drvdata(pdev);
419 	struct mmc_host *mmc = host->mmc;
420 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
421 	struct xenon_priv *priv = sdhci_pltfm_priv(pltfm_host);
422 	u32 sdhc_id, nr_sdhc;
423 	u32 tuning_count;
424 
425 	/* Disable HS200 on Armada AP806 */
426 	if (of_device_is_compatible(np, "marvell,armada-ap806-sdhci"))
427 		host->quirks2 |= SDHCI_QUIRK2_BROKEN_HS200;
428 
429 	sdhc_id = 0x0;
430 	if (!of_property_read_u32(np, "marvell,xenon-sdhc-id", &sdhc_id)) {
431 		nr_sdhc = sdhci_readl(host, XENON_SYS_CFG_INFO);
432 		nr_sdhc &= XENON_NR_SUPPORTED_SLOT_MASK;
433 		if (unlikely(sdhc_id > nr_sdhc)) {
434 			dev_err(mmc_dev(mmc), "SDHC Index %d exceeds Number of SDHCs %d\n",
435 				sdhc_id, nr_sdhc);
436 			return -EINVAL;
437 		}
438 	}
439 	priv->sdhc_id = sdhc_id;
440 
441 	tuning_count = XENON_DEF_TUNING_COUNT;
442 	if (!of_property_read_u32(np, "marvell,xenon-tun-count",
443 				  &tuning_count)) {
444 		if (unlikely(tuning_count >= XENON_TMR_RETUN_NO_PRESENT)) {
445 			dev_err(mmc_dev(mmc), "Wrong Re-tuning Count. Set default value %d\n",
446 				XENON_DEF_TUNING_COUNT);
447 			tuning_count = XENON_DEF_TUNING_COUNT;
448 		}
449 	}
450 	priv->tuning_count = tuning_count;
451 
452 	return xenon_phy_parse_dt(np, host);
453 }
454 
xenon_sdhc_prepare(struct sdhci_host * host)455 static int xenon_sdhc_prepare(struct sdhci_host *host)
456 {
457 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
458 	struct xenon_priv *priv = sdhci_pltfm_priv(pltfm_host);
459 	u8 sdhc_id = priv->sdhc_id;
460 
461 	/* Enable SDHC */
462 	xenon_enable_sdhc(host, sdhc_id);
463 
464 	/* Enable ACG */
465 	xenon_set_acg(host, true);
466 
467 	/* Enable Parallel Transfer Mode */
468 	xenon_enable_sdhc_parallel_tran(host, sdhc_id);
469 
470 	/* Disable SDCLK-Off-While-Idle before card init */
471 	xenon_set_sdclk_off_idle(host, sdhc_id, false);
472 
473 	xenon_mask_cmd_conflict_err(host);
474 
475 	return 0;
476 }
477 
xenon_sdhc_unprepare(struct sdhci_host * host)478 static void xenon_sdhc_unprepare(struct sdhci_host *host)
479 {
480 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
481 	struct xenon_priv *priv = sdhci_pltfm_priv(pltfm_host);
482 	u8 sdhc_id = priv->sdhc_id;
483 
484 	/* disable SDHC */
485 	xenon_disable_sdhc(host, sdhc_id);
486 }
487 
xenon_probe(struct platform_device * pdev)488 static int xenon_probe(struct platform_device *pdev)
489 {
490 	struct sdhci_pltfm_host *pltfm_host;
491 	struct sdhci_host *host;
492 	struct xenon_priv *priv;
493 	int err;
494 
495 	host = sdhci_pltfm_init(pdev, &sdhci_xenon_pdata,
496 				sizeof(struct xenon_priv));
497 	if (IS_ERR(host))
498 		return PTR_ERR(host);
499 
500 	pltfm_host = sdhci_priv(host);
501 	priv = sdhci_pltfm_priv(pltfm_host);
502 
503 	/*
504 	 * Link Xenon specific mmc_host_ops function,
505 	 * to replace standard ones in sdhci_ops.
506 	 */
507 	xenon_replace_mmc_host_ops(host);
508 
509 	pltfm_host->clk = devm_clk_get(&pdev->dev, "core");
510 	if (IS_ERR(pltfm_host->clk)) {
511 		err = PTR_ERR(pltfm_host->clk);
512 		dev_err(&pdev->dev, "Failed to setup input clk: %d\n", err);
513 		goto free_pltfm;
514 	}
515 	err = clk_prepare_enable(pltfm_host->clk);
516 	if (err)
517 		goto free_pltfm;
518 
519 	priv->axi_clk = devm_clk_get(&pdev->dev, "axi");
520 	if (IS_ERR(priv->axi_clk)) {
521 		err = PTR_ERR(priv->axi_clk);
522 		if (err == -EPROBE_DEFER)
523 			goto err_clk;
524 	} else {
525 		err = clk_prepare_enable(priv->axi_clk);
526 		if (err)
527 			goto err_clk;
528 	}
529 
530 	err = mmc_of_parse(host->mmc);
531 	if (err)
532 		goto err_clk_axi;
533 
534 	sdhci_get_of_property(pdev);
535 
536 	xenon_set_acg(host, false);
537 
538 	/* Xenon specific dt parse */
539 	err = xenon_probe_dt(pdev);
540 	if (err)
541 		goto err_clk_axi;
542 
543 	err = xenon_sdhc_prepare(host);
544 	if (err)
545 		goto err_clk_axi;
546 
547 	pm_runtime_get_noresume(&pdev->dev);
548 	pm_runtime_set_active(&pdev->dev);
549 	pm_runtime_set_autosuspend_delay(&pdev->dev, 50);
550 	pm_runtime_use_autosuspend(&pdev->dev);
551 	pm_runtime_enable(&pdev->dev);
552 	pm_suspend_ignore_children(&pdev->dev, 1);
553 
554 	err = sdhci_add_host(host);
555 	if (err)
556 		goto remove_sdhc;
557 
558 	pm_runtime_put_autosuspend(&pdev->dev);
559 
560 	return 0;
561 
562 remove_sdhc:
563 	pm_runtime_disable(&pdev->dev);
564 	pm_runtime_put_noidle(&pdev->dev);
565 	xenon_sdhc_unprepare(host);
566 err_clk_axi:
567 	clk_disable_unprepare(priv->axi_clk);
568 err_clk:
569 	clk_disable_unprepare(pltfm_host->clk);
570 free_pltfm:
571 	sdhci_pltfm_free(pdev);
572 	return err;
573 }
574 
xenon_remove(struct platform_device * pdev)575 static int xenon_remove(struct platform_device *pdev)
576 {
577 	struct sdhci_host *host = platform_get_drvdata(pdev);
578 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
579 	struct xenon_priv *priv = sdhci_pltfm_priv(pltfm_host);
580 
581 	pm_runtime_get_sync(&pdev->dev);
582 	pm_runtime_disable(&pdev->dev);
583 	pm_runtime_put_noidle(&pdev->dev);
584 
585 	sdhci_remove_host(host, 0);
586 
587 	xenon_sdhc_unprepare(host);
588 	clk_disable_unprepare(priv->axi_clk);
589 	clk_disable_unprepare(pltfm_host->clk);
590 
591 	sdhci_pltfm_free(pdev);
592 
593 	return 0;
594 }
595 
596 #ifdef CONFIG_PM_SLEEP
xenon_suspend(struct device * dev)597 static int xenon_suspend(struct device *dev)
598 {
599 	struct sdhci_host *host = dev_get_drvdata(dev);
600 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
601 	struct xenon_priv *priv = sdhci_pltfm_priv(pltfm_host);
602 	int ret;
603 
604 	ret = pm_runtime_force_suspend(dev);
605 
606 	priv->restore_needed = true;
607 	return ret;
608 }
609 #endif
610 
611 #ifdef CONFIG_PM
xenon_runtime_suspend(struct device * dev)612 static int xenon_runtime_suspend(struct device *dev)
613 {
614 	struct sdhci_host *host = dev_get_drvdata(dev);
615 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
616 	struct xenon_priv *priv = sdhci_pltfm_priv(pltfm_host);
617 	int ret;
618 
619 	ret = sdhci_runtime_suspend_host(host);
620 	if (ret)
621 		return ret;
622 
623 	if (host->tuning_mode != SDHCI_TUNING_MODE_3)
624 		mmc_retune_needed(host->mmc);
625 
626 	clk_disable_unprepare(pltfm_host->clk);
627 	/*
628 	 * Need to update the priv->clock here, or when runtime resume
629 	 * back, phy don't aware the clock change and won't adjust phy
630 	 * which will cause cmd err
631 	 */
632 	priv->clock = 0;
633 	return 0;
634 }
635 
xenon_runtime_resume(struct device * dev)636 static int xenon_runtime_resume(struct device *dev)
637 {
638 	struct sdhci_host *host = dev_get_drvdata(dev);
639 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
640 	struct xenon_priv *priv = sdhci_pltfm_priv(pltfm_host);
641 	int ret;
642 
643 	ret = clk_prepare_enable(pltfm_host->clk);
644 	if (ret) {
645 		dev_err(dev, "can't enable mainck\n");
646 		return ret;
647 	}
648 
649 	if (priv->restore_needed) {
650 		ret = xenon_sdhc_prepare(host);
651 		if (ret)
652 			goto out;
653 		priv->restore_needed = false;
654 	}
655 
656 	ret = sdhci_runtime_resume_host(host, 0);
657 	if (ret)
658 		goto out;
659 	return 0;
660 out:
661 	clk_disable_unprepare(pltfm_host->clk);
662 	return ret;
663 }
664 #endif /* CONFIG_PM */
665 
666 static const struct dev_pm_ops sdhci_xenon_dev_pm_ops = {
667 	SET_SYSTEM_SLEEP_PM_OPS(xenon_suspend,
668 				pm_runtime_force_resume)
669 	SET_RUNTIME_PM_OPS(xenon_runtime_suspend,
670 			   xenon_runtime_resume,
671 			   NULL)
672 };
673 
674 static const struct of_device_id sdhci_xenon_dt_ids[] = {
675 	{ .compatible = "marvell,armada-ap806-sdhci",},
676 	{ .compatible = "marvell,armada-cp110-sdhci",},
677 	{ .compatible = "marvell,armada-3700-sdhci",},
678 	{}
679 };
680 MODULE_DEVICE_TABLE(of, sdhci_xenon_dt_ids);
681 
682 static struct platform_driver sdhci_xenon_driver = {
683 	.driver	= {
684 		.name	= "xenon-sdhci",
685 		.probe_type = PROBE_PREFER_ASYNCHRONOUS,
686 		.of_match_table = sdhci_xenon_dt_ids,
687 		.pm = &sdhci_xenon_dev_pm_ops,
688 	},
689 	.probe	= xenon_probe,
690 	.remove	= xenon_remove,
691 };
692 
693 module_platform_driver(sdhci_xenon_driver);
694 
695 MODULE_DESCRIPTION("SDHCI platform driver for Marvell Xenon SDHC");
696 MODULE_AUTHOR("Hu Ziji <huziji@marvell.com>");
697 MODULE_LICENSE("GPL v2");
698