1 // SPDX-License-Identifier: GPL-2.0-only
3 * Rockchip emmc PHY driver
5 * Copyright (C) 2016 Shawn Lin <shawn.lin@rock-chips.com>
6 * Copyright (C) 2016 ROCKCHIP, Inc.
10 #include <linux/delay.h>
11 #include <linux/mfd/syscon.h>
12 #include <linux/module.h>
14 #include <linux/of_address.h>
15 #include <linux/phy/phy.h>
16 #include <linux/platform_device.h>
17 #include <linux/regmap.h>
20 * The higher 16-bit of this register is used for write protection
21 * only if BIT(x + 16) set to 1 the BIT(x) can be written.
23 #define HIWORD_UPDATE(val, mask, shift) \
24 ((val) << (shift) | (mask) << ((shift) + 16))
26 /* Register definition */
27 #define GRF_EMMCPHY_CON0 0x0
28 #define GRF_EMMCPHY_CON1 0x4
29 #define GRF_EMMCPHY_CON2 0x8
30 #define GRF_EMMCPHY_CON3 0xc
31 #define GRF_EMMCPHY_CON4 0x10
32 #define GRF_EMMCPHY_CON5 0x14
33 #define GRF_EMMCPHY_CON6 0x18
34 #define GRF_EMMCPHY_STATUS 0x20
36 #define PHYCTRL_PDB_MASK 0x1
37 #define PHYCTRL_PDB_SHIFT 0x0
38 #define PHYCTRL_PDB_PWR_ON 0x1
39 #define PHYCTRL_PDB_PWR_OFF 0x0
40 #define PHYCTRL_ENDLL_MASK 0x1
41 #define PHYCTRL_ENDLL_SHIFT 0x1
42 #define PHYCTRL_ENDLL_ENABLE 0x1
43 #define PHYCTRL_ENDLL_DISABLE 0x0
44 #define PHYCTRL_CALDONE_MASK 0x1
45 #define PHYCTRL_CALDONE_SHIFT 0x6
46 #define PHYCTRL_CALDONE_DONE 0x1
47 #define PHYCTRL_CALDONE_GOING 0x0
48 #define PHYCTRL_DLLRDY_MASK 0x1
49 #define PHYCTRL_DLLRDY_SHIFT 0x5
50 #define PHYCTRL_DLLRDY_DONE 0x1
51 #define PHYCTRL_DLLRDY_GOING 0x0
52 #define PHYCTRL_FREQSEL_200M 0x0
53 #define PHYCTRL_FREQSEL_50M 0x1
54 #define PHYCTRL_FREQSEL_100M 0x2
55 #define PHYCTRL_FREQSEL_150M 0x3
56 #define PHYCTRL_FREQSEL_MASK 0x3
57 #define PHYCTRL_FREQSEL_SHIFT 0xc
58 #define PHYCTRL_DR_MASK 0x7
59 #define PHYCTRL_DR_SHIFT 0x4
60 #define PHYCTRL_DR_50OHM 0x0
61 #define PHYCTRL_DR_33OHM 0x1
62 #define PHYCTRL_DR_66OHM 0x2
63 #define PHYCTRL_DR_100OHM 0x3
64 #define PHYCTRL_DR_40OHM 0x4
65 #define PHYCTRL_OTAPDLYENA 0x1
66 #define PHYCTRL_OTAPDLYENA_MASK 0x1
67 #define PHYCTRL_OTAPDLYENA_SHIFT 0xb
68 #define PHYCTRL_OTAPDLYSEL_DEFAULT 0x4
69 #define PHYCTRL_OTAPDLYSEL_MAXVALUE 0xf
70 #define PHYCTRL_OTAPDLYSEL_MASK 0xf
71 #define PHYCTRL_OTAPDLYSEL_SHIFT 0x7
72 #define PHYCTRL_REN_STRB_DISABLE 0x0
73 #define PHYCTRL_REN_STRB_ENABLE 0x1
74 #define PHYCTRL_REN_STRB_MASK 0x1
75 #define PHYCTRL_REN_STRB_SHIFT 0x9
77 #define PHYCTRL_IS_CALDONE(x) \
78 ((((x) >> PHYCTRL_CALDONE_SHIFT) & \
79 PHYCTRL_CALDONE_MASK) == PHYCTRL_CALDONE_DONE)
80 #define PHYCTRL_IS_DLLRDY(x) \
81 ((((x) >> PHYCTRL_DLLRDY_SHIFT) & \
82 PHYCTRL_DLLRDY_MASK) == PHYCTRL_DLLRDY_DONE)
84 struct rockchip_emmc_phy
{
85 unsigned int reg_offset
;
86 struct regmap
*reg_base
;
88 unsigned int drive_impedance
;
89 unsigned int enable_strobe_pulldown
;
90 unsigned int output_tapdelay_select
;
93 static int rockchip_emmc_phy_power(struct phy
*phy
, bool on_off
)
95 struct rockchip_emmc_phy
*rk_phy
= phy_get_drvdata(phy
);
98 unsigned int freqsel
= PHYCTRL_FREQSEL_200M
;
103 * Keep phyctrl_pdb and phyctrl_endll low to allow
104 * initialization of CALIO state M/C DFFs
106 regmap_write(rk_phy
->reg_base
,
107 rk_phy
->reg_offset
+ GRF_EMMCPHY_CON6
,
108 HIWORD_UPDATE(PHYCTRL_PDB_PWR_OFF
,
111 regmap_write(rk_phy
->reg_base
,
112 rk_phy
->reg_offset
+ GRF_EMMCPHY_CON6
,
113 HIWORD_UPDATE(PHYCTRL_ENDLL_DISABLE
,
115 PHYCTRL_ENDLL_SHIFT
));
117 /* Already finish power_off above */
118 if (on_off
== PHYCTRL_PDB_PWR_OFF
)
121 rate
= clk_get_rate(rk_phy
->emmcclk
);
124 unsigned long ideal_rate
;
129 ideal_rate
= 50000000;
130 freqsel
= PHYCTRL_FREQSEL_50M
;
132 case 75000000 ... 124999999:
133 ideal_rate
= 100000000;
134 freqsel
= PHYCTRL_FREQSEL_100M
;
136 case 125000000 ... 174999999:
137 ideal_rate
= 150000000;
138 freqsel
= PHYCTRL_FREQSEL_150M
;
141 ideal_rate
= 200000000;
145 diff
= (rate
> ideal_rate
) ?
146 rate
- ideal_rate
: ideal_rate
- rate
;
149 * In order for tuning delays to be accurate we need to be
150 * pretty spot on for the DLL range, so warn if we're too
151 * far off. Also warn if we're above the 200 MHz max. Don't
152 * warn for really slow rates since we won't be tuning then.
154 if ((rate
> 50000000 && diff
> 15000000) || (rate
> 200000000))
155 dev_warn(&phy
->dev
, "Unsupported rate: %lu\n", rate
);
159 * According to the user manual, calpad calibration
160 * cycle takes more than 2us without the minimal recommended
161 * value, so we may need a little margin here
164 regmap_write(rk_phy
->reg_base
,
165 rk_phy
->reg_offset
+ GRF_EMMCPHY_CON6
,
166 HIWORD_UPDATE(PHYCTRL_PDB_PWR_ON
,
171 * According to the user manual, it asks driver to wait 5us for
172 * calpad busy trimming. However it is documented that this value is
173 * PVT(A.K.A process,voltage and temperature) relevant, so some
174 * failure cases are found which indicates we should be more tolerant
175 * to calpad busy trimming.
177 ret
= regmap_read_poll_timeout(rk_phy
->reg_base
,
178 rk_phy
->reg_offset
+ GRF_EMMCPHY_STATUS
,
179 caldone
, PHYCTRL_IS_CALDONE(caldone
),
182 pr_err("%s: caldone failed, ret=%d\n", __func__
, ret
);
186 /* Set the frequency of the DLL operation */
187 regmap_write(rk_phy
->reg_base
,
188 rk_phy
->reg_offset
+ GRF_EMMCPHY_CON0
,
189 HIWORD_UPDATE(freqsel
, PHYCTRL_FREQSEL_MASK
,
190 PHYCTRL_FREQSEL_SHIFT
));
192 /* Turn on the DLL */
193 regmap_write(rk_phy
->reg_base
,
194 rk_phy
->reg_offset
+ GRF_EMMCPHY_CON6
,
195 HIWORD_UPDATE(PHYCTRL_ENDLL_ENABLE
,
197 PHYCTRL_ENDLL_SHIFT
));
200 * We turned on the DLL even though the rate was 0 because we the
201 * clock might be turned on later. ...but we can't wait for the DLL
202 * to lock when the rate is 0 because it will never lock with no
205 * Technically we should be checking the lock later when the clock
206 * is turned on, but for now we won't.
212 * After enabling analog DLL circuits docs say that we need 10.2 us if
213 * our source clock is at 50 MHz and that lock time scales linearly
214 * with clock speed. If we are powering on the PHY and the card clock
215 * is super slow (like 100 kHZ) this could take as long as 5.1 ms as
216 * per the math: 10.2 us * (50000000 Hz / 100000 Hz) => 5.1 ms
217 * Hopefully we won't be running at 100 kHz, but we should still make
218 * sure we wait long enough.
220 * NOTE: There appear to be corner cases where the DLL seems to take
221 * extra long to lock for reasons that aren't understood. In some
222 * extreme cases we've seen it take up to over 10ms (!). We'll be
223 * generous and give it 50ms.
225 ret
= regmap_read_poll_timeout(rk_phy
->reg_base
,
226 rk_phy
->reg_offset
+ GRF_EMMCPHY_STATUS
,
227 dllrdy
, PHYCTRL_IS_DLLRDY(dllrdy
),
228 0, 50 * USEC_PER_MSEC
);
230 pr_err("%s: dllrdy failed. ret=%d\n", __func__
, ret
);
237 static int rockchip_emmc_phy_init(struct phy
*phy
)
239 struct rockchip_emmc_phy
*rk_phy
= phy_get_drvdata(phy
);
243 * We purposely get the clock here and not in probe to avoid the
244 * circular dependency problem. We expect:
245 * - PHY driver to probe
246 * - SDHCI driver to start probe
247 * - SDHCI driver to register it's clock
248 * - SDHCI driver to get the PHY
249 * - SDHCI driver to init the PHY
251 * The clock is optional, so upon any error we just set to NULL.
253 * NOTE: we don't do anything special for EPROBE_DEFER here. Given the
254 * above expected use case, EPROBE_DEFER isn't sensible to expect, so
255 * it's just like any other error.
257 rk_phy
->emmcclk
= clk_get(&phy
->dev
, "emmcclk");
258 if (IS_ERR(rk_phy
->emmcclk
)) {
259 dev_dbg(&phy
->dev
, "Error getting emmcclk: %d\n", ret
);
260 rk_phy
->emmcclk
= NULL
;
266 static int rockchip_emmc_phy_exit(struct phy
*phy
)
268 struct rockchip_emmc_phy
*rk_phy
= phy_get_drvdata(phy
);
270 clk_put(rk_phy
->emmcclk
);
275 static int rockchip_emmc_phy_power_off(struct phy
*phy
)
277 /* Power down emmc phy analog blocks */
278 return rockchip_emmc_phy_power(phy
, PHYCTRL_PDB_PWR_OFF
);
281 static int rockchip_emmc_phy_power_on(struct phy
*phy
)
283 struct rockchip_emmc_phy
*rk_phy
= phy_get_drvdata(phy
);
285 /* Drive impedance: from DTS */
286 regmap_write(rk_phy
->reg_base
,
287 rk_phy
->reg_offset
+ GRF_EMMCPHY_CON6
,
288 HIWORD_UPDATE(rk_phy
->drive_impedance
,
292 /* Output tap delay: enable */
293 regmap_write(rk_phy
->reg_base
,
294 rk_phy
->reg_offset
+ GRF_EMMCPHY_CON0
,
295 HIWORD_UPDATE(PHYCTRL_OTAPDLYENA
,
296 PHYCTRL_OTAPDLYENA_MASK
,
297 PHYCTRL_OTAPDLYENA_SHIFT
));
299 /* Output tap delay */
300 regmap_write(rk_phy
->reg_base
,
301 rk_phy
->reg_offset
+ GRF_EMMCPHY_CON0
,
302 HIWORD_UPDATE(rk_phy
->output_tapdelay_select
,
303 PHYCTRL_OTAPDLYSEL_MASK
,
304 PHYCTRL_OTAPDLYSEL_SHIFT
));
306 /* Internal pull-down for strobe line */
307 regmap_write(rk_phy
->reg_base
,
308 rk_phy
->reg_offset
+ GRF_EMMCPHY_CON2
,
309 HIWORD_UPDATE(rk_phy
->enable_strobe_pulldown
,
310 PHYCTRL_REN_STRB_MASK
,
311 PHYCTRL_REN_STRB_SHIFT
));
313 /* Power up emmc phy analog blocks */
314 return rockchip_emmc_phy_power(phy
, PHYCTRL_PDB_PWR_ON
);
317 static const struct phy_ops ops
= {
318 .init
= rockchip_emmc_phy_init
,
319 .exit
= rockchip_emmc_phy_exit
,
320 .power_on
= rockchip_emmc_phy_power_on
,
321 .power_off
= rockchip_emmc_phy_power_off
,
322 .owner
= THIS_MODULE
,
325 static u32
convert_drive_impedance_ohm(struct platform_device
*pdev
, u32 dr_ohm
)
329 return PHYCTRL_DR_100OHM
;
331 return PHYCTRL_DR_66OHM
;
333 return PHYCTRL_DR_50OHM
;
335 return PHYCTRL_DR_40OHM
;
337 return PHYCTRL_DR_33OHM
;
340 dev_warn(&pdev
->dev
, "Invalid value %u for drive-impedance-ohm.\n",
342 return PHYCTRL_DR_50OHM
;
345 static int rockchip_emmc_phy_probe(struct platform_device
*pdev
)
347 struct device
*dev
= &pdev
->dev
;
348 struct rockchip_emmc_phy
*rk_phy
;
349 struct phy
*generic_phy
;
350 struct phy_provider
*phy_provider
;
352 unsigned int reg_offset
;
355 if (!dev
->parent
|| !dev
->parent
->of_node
)
358 grf
= syscon_node_to_regmap(dev
->parent
->of_node
);
360 dev_err(dev
, "Missing rockchip,grf property\n");
364 rk_phy
= devm_kzalloc(dev
, sizeof(*rk_phy
), GFP_KERNEL
);
368 if (of_property_read_u32(dev
->of_node
, "reg", ®_offset
)) {
369 dev_err(dev
, "missing reg property in node %pOFn\n",
374 rk_phy
->reg_offset
= reg_offset
;
375 rk_phy
->reg_base
= grf
;
376 rk_phy
->drive_impedance
= PHYCTRL_DR_50OHM
;
377 rk_phy
->enable_strobe_pulldown
= PHYCTRL_REN_STRB_DISABLE
;
378 rk_phy
->output_tapdelay_select
= PHYCTRL_OTAPDLYSEL_DEFAULT
;
380 if (!of_property_read_u32(dev
->of_node
, "drive-impedance-ohm", &val
))
381 rk_phy
->drive_impedance
= convert_drive_impedance_ohm(pdev
, val
);
383 if (of_property_read_bool(dev
->of_node
, "enable-strobe-pulldown"))
384 rk_phy
->enable_strobe_pulldown
= PHYCTRL_REN_STRB_ENABLE
;
386 if (!of_property_read_u32(dev
->of_node
, "output-tapdelay-select", &val
)) {
387 if (val
<= PHYCTRL_OTAPDLYSEL_MAXVALUE
)
388 rk_phy
->output_tapdelay_select
= val
;
390 dev_err(dev
, "output-tapdelay-select exceeds limit, apply default\n");
393 generic_phy
= devm_phy_create(dev
, dev
->of_node
, &ops
);
394 if (IS_ERR(generic_phy
)) {
395 dev_err(dev
, "failed to create PHY\n");
396 return PTR_ERR(generic_phy
);
399 phy_set_drvdata(generic_phy
, rk_phy
);
400 phy_provider
= devm_of_phy_provider_register(dev
, of_phy_simple_xlate
);
402 return PTR_ERR_OR_ZERO(phy_provider
);
405 static const struct of_device_id rockchip_emmc_phy_dt_ids
[] = {
406 { .compatible
= "rockchip,rk3399-emmc-phy" },
410 MODULE_DEVICE_TABLE(of
, rockchip_emmc_phy_dt_ids
);
412 static struct platform_driver rockchip_emmc_driver
= {
413 .probe
= rockchip_emmc_phy_probe
,
415 .name
= "rockchip-emmc-phy",
416 .of_match_table
= rockchip_emmc_phy_dt_ids
,
420 module_platform_driver(rockchip_emmc_driver
);
422 MODULE_AUTHOR("Shawn Lin <shawn.lin@rock-chips.com>");
423 MODULE_DESCRIPTION("Rockchip EMMC PHY driver");
424 MODULE_LICENSE("GPL v2");