1 // SPDX-License-Identifier: GPL-2.0-only
3 * PHY support for Xenon SDHC
5 * Copyright (C) 2016 Marvell, All Rights Reserved.
7 * Author: Hu Ziji <huziji@marvell.com>
11 #include <linux/slab.h>
12 #include <linux/delay.h>
13 #include <linux/ktime.h>
14 #include <linux/iopoll.h>
15 #include <linux/of_address.h>
17 #include "sdhci-pltfm.h"
18 #include "sdhci-xenon.h"
20 /* Register base for eMMC PHY 5.0 Version */
21 #define XENON_EMMC_5_0_PHY_REG_BASE 0x0160
22 /* Register base for eMMC PHY 5.1 Version */
23 #define XENON_EMMC_PHY_REG_BASE 0x0170
25 #define XENON_EMMC_PHY_TIMING_ADJUST XENON_EMMC_PHY_REG_BASE
26 #define XENON_EMMC_5_0_PHY_TIMING_ADJUST XENON_EMMC_5_0_PHY_REG_BASE
27 #define XENON_TIMING_ADJUST_SLOW_MODE BIT(29)
28 #define XENON_TIMING_ADJUST_SDIO_MODE BIT(28)
29 #define XENON_SAMPL_INV_QSP_PHASE_SELECT BIT(18)
30 #define XENON_SAMPL_INV_QSP_PHASE_SELECT_SHIFT 18
31 #define XENON_PHY_INITIALIZAION BIT(31)
32 #define XENON_WAIT_CYCLE_BEFORE_USING_MASK 0xF
33 #define XENON_WAIT_CYCLE_BEFORE_USING_SHIFT 12
34 #define XENON_FC_SYNC_EN_DURATION_MASK 0xF
35 #define XENON_FC_SYNC_EN_DURATION_SHIFT 8
36 #define XENON_FC_SYNC_RST_EN_DURATION_MASK 0xF
37 #define XENON_FC_SYNC_RST_EN_DURATION_SHIFT 4
38 #define XENON_FC_SYNC_RST_DURATION_MASK 0xF
39 #define XENON_FC_SYNC_RST_DURATION_SHIFT 0
41 #define XENON_EMMC_PHY_FUNC_CONTROL (XENON_EMMC_PHY_REG_BASE + 0x4)
42 #define XENON_EMMC_5_0_PHY_FUNC_CONTROL \
43 (XENON_EMMC_5_0_PHY_REG_BASE + 0x4)
44 #define XENON_ASYNC_DDRMODE_MASK BIT(23)
45 #define XENON_ASYNC_DDRMODE_SHIFT 23
46 #define XENON_CMD_DDR_MODE BIT(16)
47 #define XENON_DQ_DDR_MODE_SHIFT 8
48 #define XENON_DQ_DDR_MODE_MASK 0xFF
49 #define XENON_DQ_ASYNC_MODE BIT(4)
51 #define XENON_EMMC_PHY_PAD_CONTROL (XENON_EMMC_PHY_REG_BASE + 0x8)
52 #define XENON_EMMC_5_0_PHY_PAD_CONTROL \
53 (XENON_EMMC_5_0_PHY_REG_BASE + 0x8)
54 #define XENON_REC_EN_SHIFT 24
55 #define XENON_REC_EN_MASK 0xF
56 #define XENON_FC_DQ_RECEN BIT(24)
57 #define XENON_FC_CMD_RECEN BIT(25)
58 #define XENON_FC_QSP_RECEN BIT(26)
59 #define XENON_FC_QSN_RECEN BIT(27)
60 #define XENON_OEN_QSN BIT(28)
61 #define XENON_AUTO_RECEN_CTRL BIT(30)
62 #define XENON_FC_ALL_CMOS_RECEIVER 0xF000
64 #define XENON_EMMC5_FC_QSP_PD BIT(18)
65 #define XENON_EMMC5_FC_QSP_PU BIT(22)
66 #define XENON_EMMC5_FC_CMD_PD BIT(17)
67 #define XENON_EMMC5_FC_CMD_PU BIT(21)
68 #define XENON_EMMC5_FC_DQ_PD BIT(16)
69 #define XENON_EMMC5_FC_DQ_PU BIT(20)
71 #define XENON_EMMC_PHY_PAD_CONTROL1 (XENON_EMMC_PHY_REG_BASE + 0xC)
72 #define XENON_EMMC5_1_FC_QSP_PD BIT(9)
73 #define XENON_EMMC5_1_FC_QSP_PU BIT(25)
74 #define XENON_EMMC5_1_FC_CMD_PD BIT(8)
75 #define XENON_EMMC5_1_FC_CMD_PU BIT(24)
76 #define XENON_EMMC5_1_FC_DQ_PD 0xFF
77 #define XENON_EMMC5_1_FC_DQ_PU (0xFF << 16)
79 #define XENON_EMMC_PHY_PAD_CONTROL2 (XENON_EMMC_PHY_REG_BASE + 0x10)
80 #define XENON_EMMC_5_0_PHY_PAD_CONTROL2 \
81 (XENON_EMMC_5_0_PHY_REG_BASE + 0xC)
82 #define XENON_ZNR_MASK 0x1F
83 #define XENON_ZNR_SHIFT 8
84 #define XENON_ZPR_MASK 0x1F
85 /* Preferred ZNR and ZPR value vary between different boards.
86 * The specific ZNR and ZPR value should be defined here
87 * according to board actual timing.
89 #define XENON_ZNR_DEF_VALUE 0xF
90 #define XENON_ZPR_DEF_VALUE 0xF
92 #define XENON_EMMC_PHY_DLL_CONTROL (XENON_EMMC_PHY_REG_BASE + 0x14)
93 #define XENON_EMMC_5_0_PHY_DLL_CONTROL \
94 (XENON_EMMC_5_0_PHY_REG_BASE + 0x10)
95 #define XENON_DLL_ENABLE BIT(31)
96 #define XENON_DLL_UPDATE_STROBE_5_0 BIT(30)
97 #define XENON_DLL_REFCLK_SEL BIT(30)
98 #define XENON_DLL_UPDATE BIT(23)
99 #define XENON_DLL_PHSEL1_SHIFT 24
100 #define XENON_DLL_PHSEL0_SHIFT 16
101 #define XENON_DLL_PHASE_MASK 0x3F
102 #define XENON_DLL_PHASE_90_DEGREE 0x1F
103 #define XENON_DLL_FAST_LOCK BIT(5)
104 #define XENON_DLL_GAIN2X BIT(3)
105 #define XENON_DLL_BYPASS_EN BIT(0)
107 #define XENON_EMMC_5_0_PHY_LOGIC_TIMING_ADJUST \
108 (XENON_EMMC_5_0_PHY_REG_BASE + 0x14)
109 #define XENON_EMMC_5_0_PHY_LOGIC_TIMING_VALUE 0x5A54
110 #define XENON_EMMC_PHY_LOGIC_TIMING_ADJUST (XENON_EMMC_PHY_REG_BASE + 0x18)
111 #define XENON_LOGIC_TIMING_VALUE 0x00AA8977
113 #define XENON_MAX_PHY_TIMEOUT_LOOPS 100
116 * List offset of PHY registers and some special register values
117 * in eMMC PHY 5.0 or eMMC PHY 5.1
119 struct xenon_emmc_phy_regs
{
120 /* Offset of Timing Adjust register */
122 /* Offset of Func Control register */
124 /* Offset of Pad Control register */
126 /* Offset of Pad Control register 2 */
128 /* Offset of DLL Control register */
130 /* Offset of Logic Timing Adjust register */
131 u16 logic_timing_adj
;
132 /* DLL Update Enable bit */
134 /* value in Logic Timing Adjustment register */
135 u32 logic_timing_val
;
138 static const char * const phy_types
[] = {
143 enum xenon_phy_type_enum
{
149 enum soc_pad_ctrl_type
{
154 struct soc_pad_ctrl
{
155 /* Register address of SoC PHY PAD ctrl */
157 /* SoC PHY PAD ctrl type */
158 enum soc_pad_ctrl_type pad_type
;
159 /* SoC specific operation to set SoC PHY PAD */
160 void (*set_soc_pad
)(struct sdhci_host
*host
,
161 unsigned char signal_voltage
);
164 static struct xenon_emmc_phy_regs xenon_emmc_5_0_phy_regs
= {
165 .timing_adj
= XENON_EMMC_5_0_PHY_TIMING_ADJUST
,
166 .func_ctrl
= XENON_EMMC_5_0_PHY_FUNC_CONTROL
,
167 .pad_ctrl
= XENON_EMMC_5_0_PHY_PAD_CONTROL
,
168 .pad_ctrl2
= XENON_EMMC_5_0_PHY_PAD_CONTROL2
,
169 .dll_ctrl
= XENON_EMMC_5_0_PHY_DLL_CONTROL
,
170 .logic_timing_adj
= XENON_EMMC_5_0_PHY_LOGIC_TIMING_ADJUST
,
171 .dll_update
= XENON_DLL_UPDATE_STROBE_5_0
,
172 .logic_timing_val
= XENON_EMMC_5_0_PHY_LOGIC_TIMING_VALUE
,
175 static struct xenon_emmc_phy_regs xenon_emmc_5_1_phy_regs
= {
176 .timing_adj
= XENON_EMMC_PHY_TIMING_ADJUST
,
177 .func_ctrl
= XENON_EMMC_PHY_FUNC_CONTROL
,
178 .pad_ctrl
= XENON_EMMC_PHY_PAD_CONTROL
,
179 .pad_ctrl2
= XENON_EMMC_PHY_PAD_CONTROL2
,
180 .dll_ctrl
= XENON_EMMC_PHY_DLL_CONTROL
,
181 .logic_timing_adj
= XENON_EMMC_PHY_LOGIC_TIMING_ADJUST
,
182 .dll_update
= XENON_DLL_UPDATE
,
183 .logic_timing_val
= XENON_LOGIC_TIMING_VALUE
,
187 * eMMC PHY configuration and operations
189 struct xenon_emmc_phy_params
{
195 /* Nr of consecutive Sampling Points of a Valid Sampling Window */
197 /* Divider for calculating Tuning Step */
200 struct soc_pad_ctrl pad_ctrl
;
203 static int xenon_alloc_emmc_phy(struct sdhci_host
*host
)
205 struct sdhci_pltfm_host
*pltfm_host
= sdhci_priv(host
);
206 struct xenon_priv
*priv
= sdhci_pltfm_priv(pltfm_host
);
207 struct xenon_emmc_phy_params
*params
;
209 params
= devm_kzalloc(mmc_dev(host
->mmc
), sizeof(*params
), GFP_KERNEL
);
213 priv
->phy_params
= params
;
214 if (priv
->phy_type
== EMMC_5_0_PHY
)
215 priv
->emmc_phy_regs
= &xenon_emmc_5_0_phy_regs
;
217 priv
->emmc_phy_regs
= &xenon_emmc_5_1_phy_regs
;
222 static int xenon_check_stability_internal_clk(struct sdhci_host
*host
)
227 err
= read_poll_timeout(sdhci_readw
, reg
, reg
& SDHCI_CLOCK_INT_STABLE
,
228 1100, 20000, false, host
, SDHCI_CLOCK_CONTROL
);
230 dev_err(mmc_dev(host
->mmc
), "phy_init: Internal clock never stabilized.\n");
236 * eMMC 5.0/5.1 PHY init/re-init.
237 * eMMC PHY init should be executed after:
238 * 1. SDCLK frequency changes.
239 * 2. SDCLK is stopped and re-enabled.
240 * 3. config in emmc_phy_regs->timing_adj and emmc_phy_regs->func_ctrl
243 static int xenon_emmc_phy_init(struct sdhci_host
*host
)
247 struct sdhci_pltfm_host
*pltfm_host
= sdhci_priv(host
);
248 struct xenon_priv
*priv
= sdhci_pltfm_priv(pltfm_host
);
249 struct xenon_emmc_phy_regs
*phy_regs
= priv
->emmc_phy_regs
;
251 int ret
= xenon_check_stability_internal_clk(host
);
256 reg
= sdhci_readl(host
, phy_regs
->timing_adj
);
257 reg
|= XENON_PHY_INITIALIZAION
;
258 sdhci_writel(host
, reg
, phy_regs
->timing_adj
);
260 /* Add duration of FC_SYNC_RST */
261 wait
= ((reg
>> XENON_FC_SYNC_RST_DURATION_SHIFT
) &
262 XENON_FC_SYNC_RST_DURATION_MASK
);
263 /* Add interval between FC_SYNC_EN and FC_SYNC_RST */
264 wait
+= ((reg
>> XENON_FC_SYNC_RST_EN_DURATION_SHIFT
) &
265 XENON_FC_SYNC_RST_EN_DURATION_MASK
);
266 /* Add duration of asserting FC_SYNC_EN */
267 wait
+= ((reg
>> XENON_FC_SYNC_EN_DURATION_SHIFT
) &
268 XENON_FC_SYNC_EN_DURATION_MASK
);
269 /* Add duration of waiting for PHY */
270 wait
+= ((reg
>> XENON_WAIT_CYCLE_BEFORE_USING_SHIFT
) &
271 XENON_WAIT_CYCLE_BEFORE_USING_MASK
);
272 /* 4 additional bus clock and 4 AXI bus clock are required */
278 /* Use the possibly slowest bus frequency value */
279 clock
= XENON_LOWEST_SDCLK_FREQ
;
280 /* get the wait time */
285 * AC5X spec says bit must be polled until zero.
286 * We see cases in which timeout can take longer
287 * than the standard calculation on AC5X, which is
288 * expected following the spec comment above.
289 * According to the spec, we must wait as long as
290 * it takes for that bit to toggle on AC5X.
291 * Cap that with 100 delay loops so we won't get
292 * stuck here forever:
295 ret
= read_poll_timeout(sdhci_readl
, reg
,
296 !(reg
& XENON_PHY_INITIALIZAION
),
297 wait
, XENON_MAX_PHY_TIMEOUT_LOOPS
* wait
,
298 false, host
, phy_regs
->timing_adj
);
300 dev_err(mmc_dev(host
->mmc
), "eMMC PHY init cannot complete after %d us\n",
301 wait
* XENON_MAX_PHY_TIMEOUT_LOOPS
);
306 #define ARMADA_3700_SOC_PAD_1_8V 0x1
307 #define ARMADA_3700_SOC_PAD_3_3V 0x0
309 static void armada_3700_soc_pad_voltage_set(struct sdhci_host
*host
,
310 unsigned char signal_voltage
)
312 struct sdhci_pltfm_host
*pltfm_host
= sdhci_priv(host
);
313 struct xenon_priv
*priv
= sdhci_pltfm_priv(pltfm_host
);
314 struct xenon_emmc_phy_params
*params
= priv
->phy_params
;
316 if (params
->pad_ctrl
.pad_type
== SOC_PAD_FIXED_1_8V
) {
317 writel(ARMADA_3700_SOC_PAD_1_8V
, params
->pad_ctrl
.reg
);
318 } else if (params
->pad_ctrl
.pad_type
== SOC_PAD_SD
) {
319 if (signal_voltage
== MMC_SIGNAL_VOLTAGE_180
)
320 writel(ARMADA_3700_SOC_PAD_1_8V
, params
->pad_ctrl
.reg
);
321 else if (signal_voltage
== MMC_SIGNAL_VOLTAGE_330
)
322 writel(ARMADA_3700_SOC_PAD_3_3V
, params
->pad_ctrl
.reg
);
327 * Set SoC PHY voltage PAD control register,
328 * according to the operation voltage on PAD.
329 * The detailed operation depends on SoC implementation.
331 static void xenon_emmc_phy_set_soc_pad(struct sdhci_host
*host
,
332 unsigned char signal_voltage
)
334 struct sdhci_pltfm_host
*pltfm_host
= sdhci_priv(host
);
335 struct xenon_priv
*priv
= sdhci_pltfm_priv(pltfm_host
);
336 struct xenon_emmc_phy_params
*params
= priv
->phy_params
;
338 if (!params
->pad_ctrl
.reg
)
341 if (params
->pad_ctrl
.set_soc_pad
)
342 params
->pad_ctrl
.set_soc_pad(host
, signal_voltage
);
346 * Enable eMMC PHY HW DLL
347 * DLL should be enabled and stable before HS200/SDR104 tuning,
348 * and before HS400 data strobe setting.
350 static int xenon_emmc_phy_enable_dll(struct sdhci_host
*host
)
353 struct sdhci_pltfm_host
*pltfm_host
= sdhci_priv(host
);
354 struct xenon_priv
*priv
= sdhci_pltfm_priv(pltfm_host
);
355 struct xenon_emmc_phy_regs
*phy_regs
= priv
->emmc_phy_regs
;
358 if (WARN_ON(host
->clock
<= MMC_HIGH_52_MAX_DTR
))
361 reg
= sdhci_readl(host
, phy_regs
->dll_ctrl
);
362 if (reg
& XENON_DLL_ENABLE
)
366 reg
= sdhci_readl(host
, phy_regs
->dll_ctrl
);
367 reg
|= (XENON_DLL_ENABLE
| XENON_DLL_FAST_LOCK
);
370 * Set Phase as 90 degree, which is most common value.
371 * Might set another value if necessary.
372 * The granularity is 1 degree.
374 reg
&= ~((XENON_DLL_PHASE_MASK
<< XENON_DLL_PHSEL0_SHIFT
) |
375 (XENON_DLL_PHASE_MASK
<< XENON_DLL_PHSEL1_SHIFT
));
376 reg
|= ((XENON_DLL_PHASE_90_DEGREE
<< XENON_DLL_PHSEL0_SHIFT
) |
377 (XENON_DLL_PHASE_90_DEGREE
<< XENON_DLL_PHSEL1_SHIFT
));
379 reg
&= ~XENON_DLL_BYPASS_EN
;
380 reg
|= phy_regs
->dll_update
;
381 if (priv
->phy_type
== EMMC_5_1_PHY
)
382 reg
&= ~XENON_DLL_REFCLK_SEL
;
383 sdhci_writel(host
, reg
, phy_regs
->dll_ctrl
);
386 timeout
= ktime_add_ms(ktime_get(), 32);
388 bool timedout
= ktime_after(ktime_get(), timeout
);
390 if (sdhci_readw(host
, XENON_SLOT_EXT_PRESENT_STATE
) &
391 XENON_DLL_LOCK_STATE
)
394 dev_err(mmc_dev(host
->mmc
), "Wait for DLL Lock time-out\n");
403 * Config to eMMC PHY to prepare for tuning.
404 * Enable HW DLL and set the TUNING_STEP
406 static int xenon_emmc_phy_config_tuning(struct sdhci_host
*host
)
408 struct sdhci_pltfm_host
*pltfm_host
= sdhci_priv(host
);
409 struct xenon_priv
*priv
= sdhci_pltfm_priv(pltfm_host
);
410 struct xenon_emmc_phy_params
*params
= priv
->phy_params
;
411 u32 reg
, tuning_step
;
414 if (host
->clock
<= MMC_HIGH_52_MAX_DTR
)
417 ret
= xenon_emmc_phy_enable_dll(host
);
421 /* Achieve TUNING_STEP with HW DLL help */
422 reg
= sdhci_readl(host
, XENON_SLOT_DLL_CUR_DLY_VAL
);
423 tuning_step
= reg
/ params
->tun_step_divider
;
424 if (unlikely(tuning_step
> XENON_TUNING_STEP_MASK
)) {
425 dev_warn(mmc_dev(host
->mmc
),
426 "HS200 TUNING_STEP %d is larger than MAX value\n",
428 tuning_step
= XENON_TUNING_STEP_MASK
;
431 /* Set TUNING_STEP for later tuning */
432 reg
= sdhci_readl(host
, XENON_SLOT_OP_STATUS_CTRL
);
433 reg
&= ~(XENON_TUN_CONSECUTIVE_TIMES_MASK
<<
434 XENON_TUN_CONSECUTIVE_TIMES_SHIFT
);
435 reg
|= (params
->nr_tun_times
<< XENON_TUN_CONSECUTIVE_TIMES_SHIFT
);
436 reg
&= ~(XENON_TUNING_STEP_MASK
<< XENON_TUNING_STEP_SHIFT
);
437 reg
|= (tuning_step
<< XENON_TUNING_STEP_SHIFT
);
438 sdhci_writel(host
, reg
, XENON_SLOT_OP_STATUS_CTRL
);
443 static void xenon_emmc_phy_disable_strobe(struct sdhci_host
*host
)
445 struct sdhci_pltfm_host
*pltfm_host
= sdhci_priv(host
);
446 struct xenon_priv
*priv
= sdhci_pltfm_priv(pltfm_host
);
449 /* Disable both SDHC Data Strobe and Enhanced Strobe */
450 reg
= sdhci_readl(host
, XENON_SLOT_EMMC_CTRL
);
451 reg
&= ~(XENON_ENABLE_DATA_STROBE
| XENON_ENABLE_RESP_STROBE
);
452 sdhci_writel(host
, reg
, XENON_SLOT_EMMC_CTRL
);
454 /* Clear Strobe line Pull down or Pull up */
455 if (priv
->phy_type
== EMMC_5_0_PHY
) {
456 reg
= sdhci_readl(host
, XENON_EMMC_5_0_PHY_PAD_CONTROL
);
457 reg
&= ~(XENON_EMMC5_FC_QSP_PD
| XENON_EMMC5_FC_QSP_PU
);
458 sdhci_writel(host
, reg
, XENON_EMMC_5_0_PHY_PAD_CONTROL
);
460 reg
= sdhci_readl(host
, XENON_EMMC_PHY_PAD_CONTROL1
);
461 reg
&= ~(XENON_EMMC5_1_FC_QSP_PD
| XENON_EMMC5_1_FC_QSP_PU
);
462 sdhci_writel(host
, reg
, XENON_EMMC_PHY_PAD_CONTROL1
);
466 /* Set HS400 Data Strobe and Enhanced Strobe */
467 static void xenon_emmc_phy_strobe_delay_adj(struct sdhci_host
*host
)
469 struct sdhci_pltfm_host
*pltfm_host
= sdhci_priv(host
);
470 struct xenon_priv
*priv
= sdhci_pltfm_priv(pltfm_host
);
473 if (WARN_ON(host
->timing
!= MMC_TIMING_MMC_HS400
))
476 if (host
->clock
<= MMC_HIGH_52_MAX_DTR
)
479 dev_dbg(mmc_dev(host
->mmc
), "starts HS400 strobe delay adjustment\n");
481 xenon_emmc_phy_enable_dll(host
);
483 /* Enable SDHC Data Strobe */
484 reg
= sdhci_readl(host
, XENON_SLOT_EMMC_CTRL
);
485 reg
|= XENON_ENABLE_DATA_STROBE
;
487 * Enable SDHC Enhanced Strobe if supported
488 * Xenon Enhanced Strobe should be enabled only when
489 * 1. card is in HS400 mode and
490 * 2. SDCLK is higher than 52MHz
493 if (host
->mmc
->ios
.enhanced_strobe
)
494 reg
|= XENON_ENABLE_RESP_STROBE
;
495 sdhci_writel(host
, reg
, XENON_SLOT_EMMC_CTRL
);
497 /* Set Data Strobe Pull down */
498 if (priv
->phy_type
== EMMC_5_0_PHY
) {
499 reg
= sdhci_readl(host
, XENON_EMMC_5_0_PHY_PAD_CONTROL
);
500 reg
|= XENON_EMMC5_FC_QSP_PD
;
501 reg
&= ~XENON_EMMC5_FC_QSP_PU
;
502 sdhci_writel(host
, reg
, XENON_EMMC_5_0_PHY_PAD_CONTROL
);
504 reg
= sdhci_readl(host
, XENON_EMMC_PHY_PAD_CONTROL1
);
505 reg
|= XENON_EMMC5_1_FC_QSP_PD
;
506 reg
&= ~XENON_EMMC5_1_FC_QSP_PU
;
507 sdhci_writel(host
, reg
, XENON_EMMC_PHY_PAD_CONTROL1
);
512 * If eMMC PHY Slow Mode is required in lower speed mode (SDCLK < 55MHz)
513 * in SDR mode, enable Slow Mode to bypass eMMC PHY.
514 * SDIO slower SDR mode also requires Slow Mode.
516 * If Slow Mode is enabled, return true.
517 * Otherwise, return false.
519 static bool xenon_emmc_phy_slow_mode(struct sdhci_host
*host
,
520 unsigned char timing
)
522 struct sdhci_pltfm_host
*pltfm_host
= sdhci_priv(host
);
523 struct xenon_priv
*priv
= sdhci_pltfm_priv(pltfm_host
);
524 struct xenon_emmc_phy_params
*params
= priv
->phy_params
;
525 struct xenon_emmc_phy_regs
*phy_regs
= priv
->emmc_phy_regs
;
529 if (host
->clock
> MMC_HIGH_52_MAX_DTR
)
532 reg
= sdhci_readl(host
, phy_regs
->timing_adj
);
533 /* When in slower SDR mode, enable Slow Mode for SDIO
534 * or when Slow Mode flag is set
537 case MMC_TIMING_LEGACY
:
539 * If Slow Mode is required, enable Slow Mode by default
540 * in early init phase to avoid any potential issue.
542 if (params
->slow_mode
) {
543 reg
|= XENON_TIMING_ADJUST_SLOW_MODE
;
546 reg
&= ~XENON_TIMING_ADJUST_SLOW_MODE
;
550 case MMC_TIMING_UHS_SDR25
:
551 case MMC_TIMING_UHS_SDR12
:
552 case MMC_TIMING_SD_HS
:
553 case MMC_TIMING_MMC_HS
:
554 if ((priv
->init_card_type
== MMC_TYPE_SDIO
) ||
556 reg
|= XENON_TIMING_ADJUST_SLOW_MODE
;
562 reg
&= ~XENON_TIMING_ADJUST_SLOW_MODE
;
566 sdhci_writel(host
, reg
, phy_regs
->timing_adj
);
571 * Set-up eMMC 5.0/5.1 PHY.
572 * Specific configuration depends on the current speed mode in use.
574 static void xenon_emmc_phy_set(struct sdhci_host
*host
,
575 unsigned char timing
)
578 struct sdhci_pltfm_host
*pltfm_host
= sdhci_priv(host
);
579 struct xenon_priv
*priv
= sdhci_pltfm_priv(pltfm_host
);
580 struct xenon_emmc_phy_params
*params
= priv
->phy_params
;
581 struct xenon_emmc_phy_regs
*phy_regs
= priv
->emmc_phy_regs
;
583 dev_dbg(mmc_dev(host
->mmc
), "eMMC PHY setting starts\n");
585 /* Setup pad, set bit[28] and bits[26:24] */
586 reg
= sdhci_readl(host
, phy_regs
->pad_ctrl
);
587 reg
|= (XENON_FC_DQ_RECEN
| XENON_FC_CMD_RECEN
|
588 XENON_FC_QSP_RECEN
| XENON_OEN_QSN
);
589 /* All FC_XX_RECEIVCE should be set as CMOS Type */
590 reg
|= XENON_FC_ALL_CMOS_RECEIVER
;
591 sdhci_writel(host
, reg
, phy_regs
->pad_ctrl
);
593 /* Set CMD and DQ Pull Up */
594 if (priv
->phy_type
== EMMC_5_0_PHY
) {
595 reg
= sdhci_readl(host
, XENON_EMMC_5_0_PHY_PAD_CONTROL
);
596 reg
|= (XENON_EMMC5_FC_CMD_PU
| XENON_EMMC5_FC_DQ_PU
);
597 reg
&= ~(XENON_EMMC5_FC_CMD_PD
| XENON_EMMC5_FC_DQ_PD
);
598 sdhci_writel(host
, reg
, XENON_EMMC_5_0_PHY_PAD_CONTROL
);
600 reg
= sdhci_readl(host
, XENON_EMMC_PHY_PAD_CONTROL1
);
601 reg
|= (XENON_EMMC5_1_FC_CMD_PU
| XENON_EMMC5_1_FC_DQ_PU
);
602 reg
&= ~(XENON_EMMC5_1_FC_CMD_PD
| XENON_EMMC5_1_FC_DQ_PD
);
603 sdhci_writel(host
, reg
, XENON_EMMC_PHY_PAD_CONTROL1
);
606 if (timing
== MMC_TIMING_LEGACY
) {
607 xenon_emmc_phy_slow_mode(host
, timing
);
612 * If SDIO card, set SDIO Mode
613 * Otherwise, clear SDIO Mode
615 reg
= sdhci_readl(host
, phy_regs
->timing_adj
);
616 if (priv
->init_card_type
== MMC_TYPE_SDIO
)
617 reg
|= XENON_TIMING_ADJUST_SDIO_MODE
;
619 reg
&= ~XENON_TIMING_ADJUST_SDIO_MODE
;
620 sdhci_writel(host
, reg
, phy_regs
->timing_adj
);
622 if (xenon_emmc_phy_slow_mode(host
, timing
))
626 * Set preferred ZNR and ZPR value
627 * The ZNR and ZPR value vary between different boards.
628 * Define them both in sdhci-xenon-emmc-phy.h.
630 reg
= sdhci_readl(host
, phy_regs
->pad_ctrl2
);
631 reg
&= ~((XENON_ZNR_MASK
<< XENON_ZNR_SHIFT
) | XENON_ZPR_MASK
);
632 reg
|= ((params
->znr
<< XENON_ZNR_SHIFT
) | params
->zpr
);
633 sdhci_writel(host
, reg
, phy_regs
->pad_ctrl2
);
636 * When setting EMMC_PHY_FUNC_CONTROL register,
637 * SD clock should be disabled
639 reg
= sdhci_readl(host
, SDHCI_CLOCK_CONTROL
);
640 reg
&= ~SDHCI_CLOCK_CARD_EN
;
641 sdhci_writew(host
, reg
, SDHCI_CLOCK_CONTROL
);
643 reg
= sdhci_readl(host
, phy_regs
->func_ctrl
);
645 case MMC_TIMING_MMC_HS400
:
646 reg
|= (XENON_DQ_DDR_MODE_MASK
<< XENON_DQ_DDR_MODE_SHIFT
) |
648 reg
&= ~XENON_DQ_ASYNC_MODE
;
650 case MMC_TIMING_UHS_DDR50
:
651 case MMC_TIMING_MMC_DDR52
:
652 reg
|= (XENON_DQ_DDR_MODE_MASK
<< XENON_DQ_DDR_MODE_SHIFT
) |
653 XENON_CMD_DDR_MODE
| XENON_DQ_ASYNC_MODE
;
656 reg
&= ~((XENON_DQ_DDR_MODE_MASK
<< XENON_DQ_DDR_MODE_SHIFT
) |
658 reg
|= XENON_DQ_ASYNC_MODE
;
660 sdhci_writel(host
, reg
, phy_regs
->func_ctrl
);
662 /* Enable bus clock */
663 reg
= sdhci_readl(host
, SDHCI_CLOCK_CONTROL
);
664 reg
|= SDHCI_CLOCK_CARD_EN
;
665 sdhci_writew(host
, reg
, SDHCI_CLOCK_CONTROL
);
667 if (timing
== MMC_TIMING_MMC_HS400
)
668 /* Hardware team recommend a value for HS400 */
669 sdhci_writel(host
, phy_regs
->logic_timing_val
,
670 phy_regs
->logic_timing_adj
);
672 xenon_emmc_phy_disable_strobe(host
);
675 xenon_emmc_phy_init(host
);
677 dev_dbg(mmc_dev(host
->mmc
), "eMMC PHY setting completes\n");
680 static int get_dt_pad_ctrl_data(struct sdhci_host
*host
,
681 struct device_node
*np
,
682 struct xenon_emmc_phy_params
*params
)
684 struct sdhci_pltfm_host
*pltfm_host
= sdhci_priv(host
);
685 struct xenon_priv
*priv
= sdhci_pltfm_priv(pltfm_host
);
688 struct resource iomem
;
690 if (priv
->hw_version
== XENON_A3700
)
691 params
->pad_ctrl
.set_soc_pad
= armada_3700_soc_pad_voltage_set
;
695 if (of_address_to_resource(np
, 1, &iomem
)) {
696 dev_err(mmc_dev(host
->mmc
), "Unable to find SoC PAD ctrl register address for %pOFn\n",
701 params
->pad_ctrl
.reg
= devm_ioremap_resource(mmc_dev(host
->mmc
),
703 if (IS_ERR(params
->pad_ctrl
.reg
))
704 return PTR_ERR(params
->pad_ctrl
.reg
);
706 ret
= of_property_read_string(np
, "marvell,pad-type", &name
);
708 dev_err(mmc_dev(host
->mmc
), "Unable to determine SoC PHY PAD ctrl type\n");
711 if (!strcmp(name
, "sd")) {
712 params
->pad_ctrl
.pad_type
= SOC_PAD_SD
;
713 } else if (!strcmp(name
, "fixed-1-8v")) {
714 params
->pad_ctrl
.pad_type
= SOC_PAD_FIXED_1_8V
;
716 dev_err(mmc_dev(host
->mmc
), "Unsupported SoC PHY PAD ctrl type %s\n",
724 static int xenon_emmc_phy_parse_params(struct sdhci_host
*host
,
726 struct xenon_emmc_phy_params
*params
)
730 params
->slow_mode
= false;
731 if (device_property_read_bool(dev
, "marvell,xenon-phy-slow-mode"))
732 params
->slow_mode
= true;
734 params
->znr
= XENON_ZNR_DEF_VALUE
;
735 if (!device_property_read_u32(dev
, "marvell,xenon-phy-znr", &value
))
736 params
->znr
= value
& XENON_ZNR_MASK
;
738 params
->zpr
= XENON_ZPR_DEF_VALUE
;
739 if (!device_property_read_u32(dev
, "marvell,xenon-phy-zpr", &value
))
740 params
->zpr
= value
& XENON_ZPR_MASK
;
742 params
->nr_tun_times
= XENON_TUN_CONSECUTIVE_TIMES
;
743 if (!device_property_read_u32(dev
, "marvell,xenon-phy-nr-success-tun",
745 params
->nr_tun_times
= value
& XENON_TUN_CONSECUTIVE_TIMES_MASK
;
747 params
->tun_step_divider
= XENON_TUNING_STEP_DIVIDER
;
748 if (!device_property_read_u32(dev
, "marvell,xenon-phy-tun-step-divider",
750 params
->tun_step_divider
= value
& 0xFF;
753 return get_dt_pad_ctrl_data(host
, dev
->of_node
, params
);
757 /* Set SoC PHY Voltage PAD */
758 void xenon_soc_pad_ctrl(struct sdhci_host
*host
,
759 unsigned char signal_voltage
)
761 xenon_emmc_phy_set_soc_pad(host
, signal_voltage
);
765 * Setting PHY when card is working in High Speed Mode.
766 * HS400 set Data Strobe and Enhanced Strobe if it is supported.
767 * HS200/SDR104 set tuning config to prepare for tuning.
769 static int xenon_hs_delay_adj(struct sdhci_host
*host
)
773 if (WARN_ON(host
->clock
<= XENON_DEFAULT_SDCLK_FREQ
))
776 switch (host
->timing
) {
777 case MMC_TIMING_MMC_HS400
:
778 xenon_emmc_phy_strobe_delay_adj(host
);
780 case MMC_TIMING_MMC_HS200
:
781 case MMC_TIMING_UHS_SDR104
:
782 return xenon_emmc_phy_config_tuning(host
);
783 case MMC_TIMING_MMC_DDR52
:
784 case MMC_TIMING_UHS_DDR50
:
786 * DDR Mode requires driver to scan Sampling Fixed Delay Line,
787 * to find out a perfect operation sampling point.
788 * It is hard to implement such a scan in host driver
789 * since initiating commands by host driver is not safe.
790 * Thus so far just keep PHY Sampling Fixed Delay in
791 * default value of DDR mode.
793 * If any timing issue occurs in DDR mode on Marvell products,
794 * please contact maintainer for internal support in Marvell.
796 dev_warn_once(mmc_dev(host
->mmc
), "Timing issue might occur in DDR mode\n");
804 * Adjust PHY setting.
805 * PHY setting should be adjusted when SDCLK frequency, Bus Width
806 * or Speed Mode is changed.
807 * Additional config are required when card is working in High Speed mode,
808 * after leaving Legacy Mode.
810 int xenon_phy_adj(struct sdhci_host
*host
, struct mmc_ios
*ios
)
812 struct sdhci_pltfm_host
*pltfm_host
= sdhci_priv(host
);
813 struct xenon_priv
*priv
= sdhci_pltfm_priv(pltfm_host
);
822 * The timing, frequency or bus width is changed,
823 * better to set eMMC PHY based on current setting
824 * and adjust Xenon SDHC delay.
826 if ((host
->clock
== priv
->clock
) &&
827 (ios
->bus_width
== priv
->bus_width
) &&
828 (ios
->timing
== priv
->timing
))
831 xenon_emmc_phy_set(host
, ios
->timing
);
833 /* Update the record */
834 priv
->bus_width
= ios
->bus_width
;
836 priv
->timing
= ios
->timing
;
837 priv
->clock
= host
->clock
;
839 /* Legacy mode is a special case */
840 if (ios
->timing
== MMC_TIMING_LEGACY
)
843 if (host
->clock
> XENON_DEFAULT_SDCLK_FREQ
)
844 ret
= xenon_hs_delay_adj(host
);
848 static int xenon_add_phy(struct device
*dev
, struct sdhci_host
*host
,
849 const char *phy_name
)
851 struct sdhci_pltfm_host
*pltfm_host
= sdhci_priv(host
);
852 struct xenon_priv
*priv
= sdhci_pltfm_priv(pltfm_host
);
855 priv
->phy_type
= match_string(phy_types
, NR_PHY_TYPES
, phy_name
);
856 if (priv
->phy_type
< 0) {
857 dev_err(mmc_dev(host
->mmc
),
858 "Unable to determine PHY name %s. Use default eMMC 5.1 PHY\n",
860 priv
->phy_type
= EMMC_5_1_PHY
;
863 ret
= xenon_alloc_emmc_phy(host
);
867 return xenon_emmc_phy_parse_params(host
, dev
, priv
->phy_params
);
870 int xenon_phy_parse_params(struct device
*dev
, struct sdhci_host
*host
)
872 const char *phy_type
= NULL
;
874 if (!device_property_read_string(dev
, "marvell,xenon-phy-type", &phy_type
))
875 return xenon_add_phy(dev
, host
, phy_type
);
877 return xenon_add_phy(dev
, host
, "emmc 5.1 phy");