• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0
2 //
3 // Secure Digital Host Controller
4 //
5 // Copyright (C) 2018 Spreadtrum, Inc.
6 // Author: Chunyan Zhang <chunyan.zhang@unisoc.com>
7 
8 #include <linux/delay.h>
9 #include <linux/dma-mapping.h>
10 #include <linux/highmem.h>
11 #include <linux/module.h>
12 #include <linux/of.h>
13 #include <linux/of_device.h>
14 #include <linux/of_gpio.h>
15 #include <linux/pinctrl/consumer.h>
16 #include <linux/platform_device.h>
17 #include <linux/pm_runtime.h>
18 #include <linux/regulator/consumer.h>
19 #include <linux/slab.h>
20 
21 #include "sdhci-pltfm.h"
22 #include "mmc_hsq.h"
23 
24 /* SDHCI_ARGUMENT2 register high 16bit */
25 #define SDHCI_SPRD_ARG2_STUFF		GENMASK(31, 16)
26 
27 #define SDHCI_SPRD_REG_32_DLL_CFG	0x200
28 #define  SDHCI_SPRD_DLL_ALL_CPST_EN	(BIT(18) | BIT(24) | BIT(25) | BIT(26) | BIT(27))
29 #define  SDHCI_SPRD_DLL_EN		BIT(21)
30 #define  SDHCI_SPRD_DLL_SEARCH_MODE	BIT(16)
31 #define  SDHCI_SPRD_DLL_INIT_COUNT	0xc00
32 #define  SDHCI_SPRD_DLL_PHASE_INTERNAL	0x3
33 
34 #define SDHCI_SPRD_REG_32_DLL_DLY	0x204
35 
36 #define SDHCI_SPRD_REG_32_DLL_DLY_OFFSET	0x208
37 #define  SDHCIBSPRD_IT_WR_DLY_INV		BIT(5)
38 #define  SDHCI_SPRD_BIT_CMD_DLY_INV		BIT(13)
39 #define  SDHCI_SPRD_BIT_POSRD_DLY_INV		BIT(21)
40 #define  SDHCI_SPRD_BIT_NEGRD_DLY_INV		BIT(29)
41 
42 #define SDHCI_SPRD_REG_32_BUSY_POSI		0x250
43 #define  SDHCI_SPRD_BIT_OUTR_CLK_AUTO_EN	BIT(25)
44 #define  SDHCI_SPRD_BIT_INNR_CLK_AUTO_EN	BIT(24)
45 
46 #define SDHCI_SPRD_REG_DEBOUNCE		0x28C
47 #define  SDHCI_SPRD_BIT_DLL_BAK		BIT(0)
48 #define  SDHCI_SPRD_BIT_DLL_VAL		BIT(1)
49 
50 #define  SDHCI_SPRD_INT_SIGNAL_MASK	0x1B7F410B
51 
52 /* SDHCI_HOST_CONTROL2 */
53 #define  SDHCI_SPRD_CTRL_HS200		0x0005
54 #define  SDHCI_SPRD_CTRL_HS400		0x0006
55 #define  SDHCI_SPRD_CTRL_HS400ES	0x0007
56 
57 /*
58  * According to the standard specification, BIT(3) of SDHCI_SOFTWARE_RESET is
59  * reserved, and only used on Spreadtrum's design, the hardware cannot work
60  * if this bit is cleared.
61  * 1 : normal work
62  * 0 : hardware reset
63  */
64 #define  SDHCI_HW_RESET_CARD		BIT(3)
65 
66 #define SDHCI_SPRD_MAX_CUR		0xFFFFFF
67 #define SDHCI_SPRD_CLK_MAX_DIV		1023
68 
69 #define SDHCI_SPRD_CLK_DEF_RATE		26000000
70 #define SDHCI_SPRD_PHY_DLL_CLK		52000000
71 
72 struct sdhci_sprd_host {
73 	u32 version;
74 	struct clk *clk_sdio;
75 	struct clk *clk_enable;
76 	struct clk *clk_2x_enable;
77 	struct pinctrl *pinctrl;
78 	struct pinctrl_state *pins_uhs;
79 	struct pinctrl_state *pins_default;
80 	u32 base_rate;
81 	int flags; /* backup of host attribute */
82 	u32 phy_delay[MMC_TIMING_MMC_HS400 + 2];
83 };
84 
85 struct sdhci_sprd_phy_cfg {
86 	const char *property;
87 	u8 timing;
88 };
89 
90 static const struct sdhci_sprd_phy_cfg sdhci_sprd_phy_cfgs[] = {
91 	{ "sprd,phy-delay-legacy", MMC_TIMING_LEGACY, },
92 	{ "sprd,phy-delay-sd-highspeed", MMC_TIMING_SD_HS, },
93 	{ "sprd,phy-delay-sd-uhs-sdr50", MMC_TIMING_UHS_SDR50, },
94 	{ "sprd,phy-delay-sd-uhs-sdr104", MMC_TIMING_UHS_SDR104, },
95 	{ "sprd,phy-delay-mmc-highspeed", MMC_TIMING_MMC_HS, },
96 	{ "sprd,phy-delay-mmc-ddr52", MMC_TIMING_MMC_DDR52, },
97 	{ "sprd,phy-delay-mmc-hs200", MMC_TIMING_MMC_HS200, },
98 	{ "sprd,phy-delay-mmc-hs400", MMC_TIMING_MMC_HS400, },
99 	{ "sprd,phy-delay-mmc-hs400es", MMC_TIMING_MMC_HS400 + 1, },
100 };
101 
102 #define TO_SPRD_HOST(host) sdhci_pltfm_priv(sdhci_priv(host))
103 
sdhci_sprd_init_config(struct sdhci_host * host)104 static void sdhci_sprd_init_config(struct sdhci_host *host)
105 {
106 	u16 val;
107 
108 	/* set dll backup mode */
109 	val = sdhci_readl(host, SDHCI_SPRD_REG_DEBOUNCE);
110 	val |= SDHCI_SPRD_BIT_DLL_BAK | SDHCI_SPRD_BIT_DLL_VAL;
111 	sdhci_writel(host, val, SDHCI_SPRD_REG_DEBOUNCE);
112 }
113 
sdhci_sprd_readl(struct sdhci_host * host,int reg)114 static inline u32 sdhci_sprd_readl(struct sdhci_host *host, int reg)
115 {
116 	if (unlikely(reg == SDHCI_MAX_CURRENT))
117 		return SDHCI_SPRD_MAX_CUR;
118 
119 	return readl_relaxed(host->ioaddr + reg);
120 }
121 
sdhci_sprd_writel(struct sdhci_host * host,u32 val,int reg)122 static inline void sdhci_sprd_writel(struct sdhci_host *host, u32 val, int reg)
123 {
124 	/* SDHCI_MAX_CURRENT is reserved on Spreadtrum's platform */
125 	if (unlikely(reg == SDHCI_MAX_CURRENT))
126 		return;
127 
128 	if (unlikely(reg == SDHCI_SIGNAL_ENABLE || reg == SDHCI_INT_ENABLE))
129 		val = val & SDHCI_SPRD_INT_SIGNAL_MASK;
130 
131 	writel_relaxed(val, host->ioaddr + reg);
132 }
133 
sdhci_sprd_writew(struct sdhci_host * host,u16 val,int reg)134 static inline void sdhci_sprd_writew(struct sdhci_host *host, u16 val, int reg)
135 {
136 	/* SDHCI_BLOCK_COUNT is Read Only on Spreadtrum's platform */
137 	if (unlikely(reg == SDHCI_BLOCK_COUNT))
138 		return;
139 
140 	writew_relaxed(val, host->ioaddr + reg);
141 }
142 
sdhci_sprd_writeb(struct sdhci_host * host,u8 val,int reg)143 static inline void sdhci_sprd_writeb(struct sdhci_host *host, u8 val, int reg)
144 {
145 	/*
146 	 * Since BIT(3) of SDHCI_SOFTWARE_RESET is reserved according to the
147 	 * standard specification, sdhci_reset() write this register directly
148 	 * without checking other reserved bits, that will clear BIT(3) which
149 	 * is defined as hardware reset on Spreadtrum's platform and clearing
150 	 * it by mistake will lead the card not work. So here we need to work
151 	 * around it.
152 	 */
153 	if (unlikely(reg == SDHCI_SOFTWARE_RESET)) {
154 		if (readb_relaxed(host->ioaddr + reg) & SDHCI_HW_RESET_CARD)
155 			val |= SDHCI_HW_RESET_CARD;
156 	}
157 
158 	writeb_relaxed(val, host->ioaddr + reg);
159 }
160 
sdhci_sprd_sd_clk_off(struct sdhci_host * host)161 static inline void sdhci_sprd_sd_clk_off(struct sdhci_host *host)
162 {
163 	u16 ctrl = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
164 
165 	ctrl &= ~SDHCI_CLOCK_CARD_EN;
166 	sdhci_writew(host, ctrl, SDHCI_CLOCK_CONTROL);
167 }
168 
sdhci_sprd_sd_clk_on(struct sdhci_host * host)169 static inline void sdhci_sprd_sd_clk_on(struct sdhci_host *host)
170 {
171 	u16 ctrl;
172 
173 	ctrl = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
174 	ctrl |= SDHCI_CLOCK_CARD_EN;
175 	sdhci_writew(host, ctrl, SDHCI_CLOCK_CONTROL);
176 }
177 
178 static inline void
sdhci_sprd_set_dll_invert(struct sdhci_host * host,u32 mask,bool en)179 sdhci_sprd_set_dll_invert(struct sdhci_host *host, u32 mask, bool en)
180 {
181 	u32 dll_dly_offset;
182 
183 	dll_dly_offset = sdhci_readl(host, SDHCI_SPRD_REG_32_DLL_DLY_OFFSET);
184 	if (en)
185 		dll_dly_offset |= mask;
186 	else
187 		dll_dly_offset &= ~mask;
188 	sdhci_writel(host, dll_dly_offset, SDHCI_SPRD_REG_32_DLL_DLY_OFFSET);
189 }
190 
sdhci_sprd_calc_div(u32 base_clk,u32 clk)191 static inline u32 sdhci_sprd_calc_div(u32 base_clk, u32 clk)
192 {
193 	u32 div;
194 
195 	/* select 2x clock source */
196 	if (base_clk <= clk * 2)
197 		return 0;
198 
199 	div = (u32) (base_clk / (clk * 2));
200 
201 	if ((base_clk / div) > (clk * 2))
202 		div++;
203 
204 	if (div > SDHCI_SPRD_CLK_MAX_DIV)
205 		div = SDHCI_SPRD_CLK_MAX_DIV;
206 
207 	if (div % 2)
208 		div = (div + 1) / 2;
209 	else
210 		div = div / 2;
211 
212 	return div;
213 }
214 
_sdhci_sprd_set_clock(struct sdhci_host * host,unsigned int clk)215 static inline void _sdhci_sprd_set_clock(struct sdhci_host *host,
216 					unsigned int clk)
217 {
218 	struct sdhci_sprd_host *sprd_host = TO_SPRD_HOST(host);
219 	u32 div, val, mask;
220 
221 	sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
222 
223 	div = sdhci_sprd_calc_div(sprd_host->base_rate, clk);
224 	div = ((div & 0x300) >> 2) | ((div & 0xFF) << 8);
225 	sdhci_enable_clk(host, div);
226 
227 	/* Enable CLK_AUTO when the clock is greater than 400K. */
228 	if (clk > 400000) {
229 		val = sdhci_readl(host, SDHCI_SPRD_REG_32_BUSY_POSI);
230 		mask = SDHCI_SPRD_BIT_OUTR_CLK_AUTO_EN |
231 			SDHCI_SPRD_BIT_INNR_CLK_AUTO_EN;
232 		if (mask != (val & mask)) {
233 			val |= mask;
234 			sdhci_writel(host, val, SDHCI_SPRD_REG_32_BUSY_POSI);
235 		}
236 	}
237 }
238 
sdhci_sprd_enable_phy_dll(struct sdhci_host * host)239 static void sdhci_sprd_enable_phy_dll(struct sdhci_host *host)
240 {
241 	u32 tmp;
242 
243 	tmp = sdhci_readl(host, SDHCI_SPRD_REG_32_DLL_CFG);
244 	tmp &= ~(SDHCI_SPRD_DLL_EN | SDHCI_SPRD_DLL_ALL_CPST_EN);
245 	sdhci_writel(host, tmp, SDHCI_SPRD_REG_32_DLL_CFG);
246 	/* wait 1ms */
247 	usleep_range(1000, 1250);
248 
249 	tmp = sdhci_readl(host, SDHCI_SPRD_REG_32_DLL_CFG);
250 	tmp |= SDHCI_SPRD_DLL_ALL_CPST_EN | SDHCI_SPRD_DLL_SEARCH_MODE |
251 		SDHCI_SPRD_DLL_INIT_COUNT | SDHCI_SPRD_DLL_PHASE_INTERNAL;
252 	sdhci_writel(host, tmp, SDHCI_SPRD_REG_32_DLL_CFG);
253 	/* wait 1ms */
254 	usleep_range(1000, 1250);
255 
256 	tmp = sdhci_readl(host, SDHCI_SPRD_REG_32_DLL_CFG);
257 	tmp |= SDHCI_SPRD_DLL_EN;
258 	sdhci_writel(host, tmp, SDHCI_SPRD_REG_32_DLL_CFG);
259 	/* wait 1ms */
260 	usleep_range(1000, 1250);
261 }
262 
sdhci_sprd_set_clock(struct sdhci_host * host,unsigned int clock)263 static void sdhci_sprd_set_clock(struct sdhci_host *host, unsigned int clock)
264 {
265 	bool en = false, clk_changed = false;
266 
267 	if (clock == 0) {
268 		sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
269 	} else if (clock != host->clock) {
270 		sdhci_sprd_sd_clk_off(host);
271 		_sdhci_sprd_set_clock(host, clock);
272 
273 		if (clock <= 400000)
274 			en = true;
275 		sdhci_sprd_set_dll_invert(host, SDHCI_SPRD_BIT_CMD_DLY_INV |
276 					  SDHCI_SPRD_BIT_POSRD_DLY_INV, en);
277 		clk_changed = true;
278 	} else {
279 		_sdhci_sprd_set_clock(host, clock);
280 	}
281 
282 	/*
283 	 * According to the Spreadtrum SD host specification, when we changed
284 	 * the clock to be more than 52M, we should enable the PHY DLL which
285 	 * is used to track the clock frequency to make the clock work more
286 	 * stable. Otherwise deviation may occur of the higher clock.
287 	 */
288 	if (clk_changed && clock > SDHCI_SPRD_PHY_DLL_CLK)
289 		sdhci_sprd_enable_phy_dll(host);
290 }
291 
sdhci_sprd_get_max_clock(struct sdhci_host * host)292 static unsigned int sdhci_sprd_get_max_clock(struct sdhci_host *host)
293 {
294 	struct sdhci_sprd_host *sprd_host = TO_SPRD_HOST(host);
295 
296 	return clk_round_rate(sprd_host->clk_sdio, ULONG_MAX);
297 }
298 
sdhci_sprd_get_min_clock(struct sdhci_host * host)299 static unsigned int sdhci_sprd_get_min_clock(struct sdhci_host *host)
300 {
301 	return 100000;
302 }
303 
sdhci_sprd_set_uhs_signaling(struct sdhci_host * host,unsigned int timing)304 static void sdhci_sprd_set_uhs_signaling(struct sdhci_host *host,
305 					 unsigned int timing)
306 {
307 	struct sdhci_sprd_host *sprd_host = TO_SPRD_HOST(host);
308 	struct mmc_host *mmc = host->mmc;
309 	u32 *p = sprd_host->phy_delay;
310 	u16 ctrl_2;
311 
312 	if (timing == host->timing)
313 		return;
314 
315 	ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
316 	/* Select Bus Speed Mode for host */
317 	ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
318 	switch (timing) {
319 	case MMC_TIMING_UHS_SDR12:
320 		ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
321 		break;
322 	case MMC_TIMING_MMC_HS:
323 	case MMC_TIMING_SD_HS:
324 	case MMC_TIMING_UHS_SDR25:
325 		ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
326 		break;
327 	case MMC_TIMING_UHS_SDR50:
328 		ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
329 		break;
330 	case MMC_TIMING_UHS_SDR104:
331 		ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
332 		break;
333 	case MMC_TIMING_UHS_DDR50:
334 	case MMC_TIMING_MMC_DDR52:
335 		ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
336 		break;
337 	case MMC_TIMING_MMC_HS200:
338 		ctrl_2 |= SDHCI_SPRD_CTRL_HS200;
339 		break;
340 	case MMC_TIMING_MMC_HS400:
341 		ctrl_2 |= SDHCI_SPRD_CTRL_HS400;
342 		break;
343 	default:
344 		break;
345 	}
346 
347 	sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
348 
349 	if (!mmc->ios.enhanced_strobe)
350 		sdhci_writel(host, p[timing], SDHCI_SPRD_REG_32_DLL_DLY);
351 }
352 
sdhci_sprd_hw_reset(struct sdhci_host * host)353 static void sdhci_sprd_hw_reset(struct sdhci_host *host)
354 {
355 	int val;
356 
357 	/*
358 	 * Note: don't use sdhci_writeb() API here since it is redirected to
359 	 * sdhci_sprd_writeb() in which we have a workaround for
360 	 * SDHCI_SOFTWARE_RESET which would make bit SDHCI_HW_RESET_CARD can
361 	 * not be cleared.
362 	 */
363 	val = readb_relaxed(host->ioaddr + SDHCI_SOFTWARE_RESET);
364 	val &= ~SDHCI_HW_RESET_CARD;
365 	writeb_relaxed(val, host->ioaddr + SDHCI_SOFTWARE_RESET);
366 	/* wait for 10 us */
367 	usleep_range(10, 20);
368 
369 	val |= SDHCI_HW_RESET_CARD;
370 	writeb_relaxed(val, host->ioaddr + SDHCI_SOFTWARE_RESET);
371 	usleep_range(300, 500);
372 }
373 
sdhci_sprd_get_max_timeout_count(struct sdhci_host * host)374 static unsigned int sdhci_sprd_get_max_timeout_count(struct sdhci_host *host)
375 {
376 	/* The Spredtrum controller actual maximum timeout count is 1 << 31 */
377 	return 1 << 31;
378 }
379 
sdhci_sprd_get_ro(struct sdhci_host * host)380 static unsigned int sdhci_sprd_get_ro(struct sdhci_host *host)
381 {
382 	return 0;
383 }
384 
sdhci_sprd_request_done(struct sdhci_host * host,struct mmc_request * mrq)385 static void sdhci_sprd_request_done(struct sdhci_host *host,
386 				    struct mmc_request *mrq)
387 {
388 	/* Validate if the request was from software queue firstly. */
389 	if (mmc_hsq_finalize_request(host->mmc, mrq))
390 		return;
391 
392 	mmc_request_done(host->mmc, mrq);
393 }
394 
395 static struct sdhci_ops sdhci_sprd_ops = {
396 	.read_l = sdhci_sprd_readl,
397 	.write_l = sdhci_sprd_writel,
398 	.write_w = sdhci_sprd_writew,
399 	.write_b = sdhci_sprd_writeb,
400 	.set_clock = sdhci_sprd_set_clock,
401 	.get_max_clock = sdhci_sprd_get_max_clock,
402 	.get_min_clock = sdhci_sprd_get_min_clock,
403 	.set_bus_width = sdhci_set_bus_width,
404 	.reset = sdhci_reset,
405 	.set_uhs_signaling = sdhci_sprd_set_uhs_signaling,
406 	.hw_reset = sdhci_sprd_hw_reset,
407 	.get_max_timeout_count = sdhci_sprd_get_max_timeout_count,
408 	.get_ro = sdhci_sprd_get_ro,
409 	.request_done = sdhci_sprd_request_done,
410 };
411 
sdhci_sprd_check_auto_cmd23(struct mmc_host * mmc,struct mmc_request * mrq)412 static void sdhci_sprd_check_auto_cmd23(struct mmc_host *mmc,
413 					struct mmc_request *mrq)
414 {
415 	struct sdhci_host *host = mmc_priv(mmc);
416 	struct sdhci_sprd_host *sprd_host = TO_SPRD_HOST(host);
417 
418 	host->flags |= sprd_host->flags & SDHCI_AUTO_CMD23;
419 
420 	/*
421 	 * From version 4.10 onward, ARGUMENT2 register is also as 32-bit
422 	 * block count register which doesn't support stuff bits of
423 	 * CMD23 argument on Spreadtrum's sd host controller.
424 	 */
425 	if (host->version >= SDHCI_SPEC_410 &&
426 	    mrq->sbc && (mrq->sbc->arg & SDHCI_SPRD_ARG2_STUFF) &&
427 	    (host->flags & SDHCI_AUTO_CMD23))
428 		host->flags &= ~SDHCI_AUTO_CMD23;
429 }
430 
sdhci_sprd_request(struct mmc_host * mmc,struct mmc_request * mrq)431 static void sdhci_sprd_request(struct mmc_host *mmc, struct mmc_request *mrq)
432 {
433 	sdhci_sprd_check_auto_cmd23(mmc, mrq);
434 
435 	sdhci_request(mmc, mrq);
436 }
437 
sdhci_sprd_request_atomic(struct mmc_host * mmc,struct mmc_request * mrq)438 static int sdhci_sprd_request_atomic(struct mmc_host *mmc,
439 				     struct mmc_request *mrq)
440 {
441 	sdhci_sprd_check_auto_cmd23(mmc, mrq);
442 
443 	return sdhci_request_atomic(mmc, mrq);
444 }
445 
sdhci_sprd_voltage_switch(struct mmc_host * mmc,struct mmc_ios * ios)446 static int sdhci_sprd_voltage_switch(struct mmc_host *mmc, struct mmc_ios *ios)
447 {
448 	struct sdhci_host *host = mmc_priv(mmc);
449 	struct sdhci_sprd_host *sprd_host = TO_SPRD_HOST(host);
450 	int ret;
451 
452 	if (!IS_ERR(mmc->supply.vqmmc)) {
453 		ret = mmc_regulator_set_vqmmc(mmc, ios);
454 		if (ret < 0) {
455 			pr_err("%s: Switching signalling voltage failed\n",
456 			       mmc_hostname(mmc));
457 			return ret;
458 		}
459 	}
460 
461 	if (IS_ERR(sprd_host->pinctrl))
462 		goto reset;
463 
464 	switch (ios->signal_voltage) {
465 	case MMC_SIGNAL_VOLTAGE_180:
466 		ret = pinctrl_select_state(sprd_host->pinctrl,
467 					   sprd_host->pins_uhs);
468 		if (ret) {
469 			pr_err("%s: failed to select uhs pin state\n",
470 			       mmc_hostname(mmc));
471 			return ret;
472 		}
473 		break;
474 
475 	default:
476 		fallthrough;
477 	case MMC_SIGNAL_VOLTAGE_330:
478 		ret = pinctrl_select_state(sprd_host->pinctrl,
479 					   sprd_host->pins_default);
480 		if (ret) {
481 			pr_err("%s: failed to select default pin state\n",
482 			       mmc_hostname(mmc));
483 			return ret;
484 		}
485 		break;
486 	}
487 
488 	/* Wait for 300 ~ 500 us for pin state stable */
489 	usleep_range(300, 500);
490 
491 reset:
492 	sdhci_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
493 
494 	return 0;
495 }
496 
sdhci_sprd_hs400_enhanced_strobe(struct mmc_host * mmc,struct mmc_ios * ios)497 static void sdhci_sprd_hs400_enhanced_strobe(struct mmc_host *mmc,
498 					     struct mmc_ios *ios)
499 {
500 	struct sdhci_host *host = mmc_priv(mmc);
501 	struct sdhci_sprd_host *sprd_host = TO_SPRD_HOST(host);
502 	u32 *p = sprd_host->phy_delay;
503 	u16 ctrl_2;
504 
505 	if (!ios->enhanced_strobe)
506 		return;
507 
508 	sdhci_sprd_sd_clk_off(host);
509 
510 	/* Set HS400 enhanced strobe mode */
511 	ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
512 	ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
513 	ctrl_2 |= SDHCI_SPRD_CTRL_HS400ES;
514 	sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
515 
516 	sdhci_sprd_sd_clk_on(host);
517 
518 	/* Set the PHY DLL delay value for HS400 enhanced strobe mode */
519 	sdhci_writel(host, p[MMC_TIMING_MMC_HS400 + 1],
520 		     SDHCI_SPRD_REG_32_DLL_DLY);
521 }
522 
sdhci_sprd_phy_param_parse(struct sdhci_sprd_host * sprd_host,struct device_node * np)523 static void sdhci_sprd_phy_param_parse(struct sdhci_sprd_host *sprd_host,
524 				       struct device_node *np)
525 {
526 	u32 *p = sprd_host->phy_delay;
527 	int ret, i, index;
528 	u32 val[4];
529 
530 	for (i = 0; i < ARRAY_SIZE(sdhci_sprd_phy_cfgs); i++) {
531 		ret = of_property_read_u32_array(np,
532 				sdhci_sprd_phy_cfgs[i].property, val, 4);
533 		if (ret)
534 			continue;
535 
536 		index = sdhci_sprd_phy_cfgs[i].timing;
537 		p[index] = val[0] | (val[1] << 8) | (val[2] << 16) | (val[3] << 24);
538 	}
539 }
540 
541 static const struct sdhci_pltfm_data sdhci_sprd_pdata = {
542 	.quirks = SDHCI_QUIRK_BROKEN_CARD_DETECTION |
543 		  SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK |
544 		  SDHCI_QUIRK_MISSING_CAPS,
545 	.quirks2 = SDHCI_QUIRK2_BROKEN_HS200 |
546 		   SDHCI_QUIRK2_USE_32BIT_BLK_CNT |
547 		   SDHCI_QUIRK2_PRESET_VALUE_BROKEN,
548 	.ops = &sdhci_sprd_ops,
549 };
550 
sdhci_sprd_probe(struct platform_device * pdev)551 static int sdhci_sprd_probe(struct platform_device *pdev)
552 {
553 	struct sdhci_host *host;
554 	struct sdhci_sprd_host *sprd_host;
555 	struct mmc_hsq *hsq;
556 	struct clk *clk;
557 	int ret = 0;
558 
559 	host = sdhci_pltfm_init(pdev, &sdhci_sprd_pdata, sizeof(*sprd_host));
560 	if (IS_ERR(host))
561 		return PTR_ERR(host);
562 
563 	host->dma_mask = DMA_BIT_MASK(64);
564 	pdev->dev.dma_mask = &host->dma_mask;
565 	host->mmc_host_ops.request = sdhci_sprd_request;
566 	host->mmc_host_ops.hs400_enhanced_strobe =
567 		sdhci_sprd_hs400_enhanced_strobe;
568 	/*
569 	 * We can not use the standard ops to change and detect the voltage
570 	 * signal for Spreadtrum SD host controller, since our voltage regulator
571 	 * for I/O is fixed in hardware, that means we do not need control
572 	 * the standard SD host controller to change the I/O voltage.
573 	 */
574 	host->mmc_host_ops.start_signal_voltage_switch =
575 		sdhci_sprd_voltage_switch;
576 
577 	host->mmc->caps = MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED |
578 		MMC_CAP_WAIT_WHILE_BUSY;
579 
580 	ret = mmc_of_parse(host->mmc);
581 	if (ret)
582 		goto pltfm_free;
583 
584 	if (!mmc_card_is_removable(host->mmc))
585 		host->mmc_host_ops.request_atomic = sdhci_sprd_request_atomic;
586 	else
587 		host->always_defer_done = true;
588 
589 	sprd_host = TO_SPRD_HOST(host);
590 	sdhci_sprd_phy_param_parse(sprd_host, pdev->dev.of_node);
591 
592 	sprd_host->pinctrl = devm_pinctrl_get(&pdev->dev);
593 	if (!IS_ERR(sprd_host->pinctrl)) {
594 		sprd_host->pins_uhs =
595 			pinctrl_lookup_state(sprd_host->pinctrl, "state_uhs");
596 		if (IS_ERR(sprd_host->pins_uhs)) {
597 			ret = PTR_ERR(sprd_host->pins_uhs);
598 			goto pltfm_free;
599 		}
600 
601 		sprd_host->pins_default =
602 			pinctrl_lookup_state(sprd_host->pinctrl, "default");
603 		if (IS_ERR(sprd_host->pins_default)) {
604 			ret = PTR_ERR(sprd_host->pins_default);
605 			goto pltfm_free;
606 		}
607 	}
608 
609 	clk = devm_clk_get(&pdev->dev, "sdio");
610 	if (IS_ERR(clk)) {
611 		ret = PTR_ERR(clk);
612 		goto pltfm_free;
613 	}
614 	sprd_host->clk_sdio = clk;
615 	sprd_host->base_rate = clk_get_rate(sprd_host->clk_sdio);
616 	if (!sprd_host->base_rate)
617 		sprd_host->base_rate = SDHCI_SPRD_CLK_DEF_RATE;
618 
619 	clk = devm_clk_get(&pdev->dev, "enable");
620 	if (IS_ERR(clk)) {
621 		ret = PTR_ERR(clk);
622 		goto pltfm_free;
623 	}
624 	sprd_host->clk_enable = clk;
625 
626 	clk = devm_clk_get(&pdev->dev, "2x_enable");
627 	if (!IS_ERR(clk))
628 		sprd_host->clk_2x_enable = clk;
629 
630 	ret = clk_prepare_enable(sprd_host->clk_sdio);
631 	if (ret)
632 		goto pltfm_free;
633 
634 	ret = clk_prepare_enable(sprd_host->clk_enable);
635 	if (ret)
636 		goto clk_disable;
637 
638 	ret = clk_prepare_enable(sprd_host->clk_2x_enable);
639 	if (ret)
640 		goto clk_disable2;
641 
642 	sdhci_sprd_init_config(host);
643 	host->version = sdhci_readw(host, SDHCI_HOST_VERSION);
644 	sprd_host->version = ((host->version & SDHCI_VENDOR_VER_MASK) >>
645 			       SDHCI_VENDOR_VER_SHIFT);
646 
647 	pm_runtime_get_noresume(&pdev->dev);
648 	pm_runtime_set_active(&pdev->dev);
649 	pm_runtime_enable(&pdev->dev);
650 	pm_runtime_set_autosuspend_delay(&pdev->dev, 50);
651 	pm_runtime_use_autosuspend(&pdev->dev);
652 	pm_suspend_ignore_children(&pdev->dev, 1);
653 
654 	sdhci_enable_v4_mode(host);
655 
656 	/*
657 	 * Supply the existing CAPS, but clear the UHS-I modes. This
658 	 * will allow these modes to be specified only by device
659 	 * tree properties through mmc_of_parse().
660 	 */
661 	host->caps = sdhci_readl(host, SDHCI_CAPABILITIES);
662 	host->caps1 = sdhci_readl(host, SDHCI_CAPABILITIES_1);
663 	host->caps1 &= ~(SDHCI_SUPPORT_SDR50 | SDHCI_SUPPORT_SDR104 |
664 			 SDHCI_SUPPORT_DDR50);
665 
666 	ret = sdhci_setup_host(host);
667 	if (ret)
668 		goto pm_runtime_disable;
669 
670 	sprd_host->flags = host->flags;
671 
672 	hsq = devm_kzalloc(&pdev->dev, sizeof(*hsq), GFP_KERNEL);
673 	if (!hsq) {
674 		ret = -ENOMEM;
675 		goto err_cleanup_host;
676 	}
677 
678 	ret = mmc_hsq_init(hsq, host->mmc);
679 	if (ret)
680 		goto err_cleanup_host;
681 
682 	ret = __sdhci_add_host(host);
683 	if (ret)
684 		goto err_cleanup_host;
685 
686 	pm_runtime_mark_last_busy(&pdev->dev);
687 	pm_runtime_put_autosuspend(&pdev->dev);
688 
689 	return 0;
690 
691 err_cleanup_host:
692 	sdhci_cleanup_host(host);
693 
694 pm_runtime_disable:
695 	pm_runtime_put_noidle(&pdev->dev);
696 	pm_runtime_disable(&pdev->dev);
697 	pm_runtime_set_suspended(&pdev->dev);
698 
699 	clk_disable_unprepare(sprd_host->clk_2x_enable);
700 
701 clk_disable2:
702 	clk_disable_unprepare(sprd_host->clk_enable);
703 
704 clk_disable:
705 	clk_disable_unprepare(sprd_host->clk_sdio);
706 
707 pltfm_free:
708 	sdhci_pltfm_free(pdev);
709 	return ret;
710 }
711 
sdhci_sprd_remove(struct platform_device * pdev)712 static int sdhci_sprd_remove(struct platform_device *pdev)
713 {
714 	struct sdhci_host *host = platform_get_drvdata(pdev);
715 	struct sdhci_sprd_host *sprd_host = TO_SPRD_HOST(host);
716 
717 	sdhci_remove_host(host, 0);
718 
719 	clk_disable_unprepare(sprd_host->clk_sdio);
720 	clk_disable_unprepare(sprd_host->clk_enable);
721 	clk_disable_unprepare(sprd_host->clk_2x_enable);
722 
723 	sdhci_pltfm_free(pdev);
724 
725 	return 0;
726 }
727 
728 static const struct of_device_id sdhci_sprd_of_match[] = {
729 	{ .compatible = "sprd,sdhci-r11", },
730 	{ }
731 };
732 MODULE_DEVICE_TABLE(of, sdhci_sprd_of_match);
733 
734 #ifdef CONFIG_PM
sdhci_sprd_runtime_suspend(struct device * dev)735 static int sdhci_sprd_runtime_suspend(struct device *dev)
736 {
737 	struct sdhci_host *host = dev_get_drvdata(dev);
738 	struct sdhci_sprd_host *sprd_host = TO_SPRD_HOST(host);
739 
740 	mmc_hsq_suspend(host->mmc);
741 	sdhci_runtime_suspend_host(host);
742 
743 	clk_disable_unprepare(sprd_host->clk_sdio);
744 	clk_disable_unprepare(sprd_host->clk_enable);
745 	clk_disable_unprepare(sprd_host->clk_2x_enable);
746 
747 	return 0;
748 }
749 
sdhci_sprd_runtime_resume(struct device * dev)750 static int sdhci_sprd_runtime_resume(struct device *dev)
751 {
752 	struct sdhci_host *host = dev_get_drvdata(dev);
753 	struct sdhci_sprd_host *sprd_host = TO_SPRD_HOST(host);
754 	int ret;
755 
756 	ret = clk_prepare_enable(sprd_host->clk_2x_enable);
757 	if (ret)
758 		return ret;
759 
760 	ret = clk_prepare_enable(sprd_host->clk_enable);
761 	if (ret)
762 		goto clk_2x_disable;
763 
764 	ret = clk_prepare_enable(sprd_host->clk_sdio);
765 	if (ret)
766 		goto clk_disable;
767 
768 	sdhci_runtime_resume_host(host, 1);
769 	mmc_hsq_resume(host->mmc);
770 
771 	return 0;
772 
773 clk_disable:
774 	clk_disable_unprepare(sprd_host->clk_enable);
775 
776 clk_2x_disable:
777 	clk_disable_unprepare(sprd_host->clk_2x_enable);
778 
779 	return ret;
780 }
781 #endif
782 
783 static const struct dev_pm_ops sdhci_sprd_pm_ops = {
784 	SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
785 				pm_runtime_force_resume)
786 	SET_RUNTIME_PM_OPS(sdhci_sprd_runtime_suspend,
787 			   sdhci_sprd_runtime_resume, NULL)
788 };
789 
790 static struct platform_driver sdhci_sprd_driver = {
791 	.probe = sdhci_sprd_probe,
792 	.remove = sdhci_sprd_remove,
793 	.driver = {
794 		.name = "sdhci_sprd_r11",
795 		.probe_type = PROBE_PREFER_ASYNCHRONOUS,
796 		.of_match_table = of_match_ptr(sdhci_sprd_of_match),
797 		.pm = &sdhci_sprd_pm_ops,
798 	},
799 };
800 module_platform_driver(sdhci_sprd_driver);
801 
802 MODULE_DESCRIPTION("Spreadtrum sdio host controller r11 driver");
803 MODULE_LICENSE("GPL v2");
804 MODULE_ALIAS("platform:sdhci-sprd-r11");
805