1 // SPDX-License-Identifier: GPL-2.0-only
3 * Rockchip usb PHY driver
5 * Copyright (C) 2014 Yunzhi Li <lyz@rock-chips.com>
6 * Copyright (C) 2014 ROCKCHIP, Inc.
10 #include <linux/clk-provider.h>
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/mutex.h>
16 #include <linux/phy/phy.h>
17 #include <linux/platform_device.h>
18 #include <linux/property.h>
19 #include <linux/regulator/consumer.h>
20 #include <linux/reset.h>
21 #include <linux/regmap.h>
22 #include <linux/mfd/syscon.h>
23 #include <linux/delay.h>
25 static int enable_usb_uart
;
27 #define HIWORD_UPDATE(val, mask) \
28 ((val) | (mask) << 16)
31 #define UOC_CON0_SIDDQ BIT(13)
32 #define UOC_CON0_DISABLE BIT(4)
33 #define UOC_CON0_COMMON_ON_N BIT(0)
36 #define UOC_CON2_SOFT_CON_SEL BIT(2)
39 /* bits present on rk3188 and rk3288 phys */
40 #define UOC_CON3_UTMI_TERMSEL_FULLSPEED BIT(5)
41 #define UOC_CON3_UTMI_XCVRSEELCT_FSTRANSC (1 << 3)
42 #define UOC_CON3_UTMI_XCVRSEELCT_MASK (3 << 3)
43 #define UOC_CON3_UTMI_OPMODE_NODRIVING (1 << 1)
44 #define UOC_CON3_UTMI_OPMODE_MASK (3 << 1)
45 #define UOC_CON3_UTMI_SUSPENDN BIT(0)
47 struct rockchip_usb_phys
{
52 struct rockchip_usb_phy_base
;
53 struct rockchip_usb_phy_pdata
{
54 struct rockchip_usb_phys
*phys
;
55 int (*init_usb_uart
)(struct regmap
*grf
,
56 const struct rockchip_usb_phy_pdata
*pdata
);
60 struct rockchip_usb_phy_base
{
62 struct regmap
*reg_base
;
63 const struct rockchip_usb_phy_pdata
*pdata
;
66 struct rockchip_usb_phy
{
67 struct rockchip_usb_phy_base
*base
;
68 struct device_node
*np
;
69 unsigned int reg_offset
;
72 struct clk_hw clk480m_hw
;
75 struct reset_control
*reset
;
76 struct regulator
*vbus
;
79 static int rockchip_usb_phy_power(struct rockchip_usb_phy
*phy
,
82 u32 val
= HIWORD_UPDATE(siddq
? UOC_CON0_SIDDQ
: 0, UOC_CON0_SIDDQ
);
84 return regmap_write(phy
->base
->reg_base
, phy
->reg_offset
, val
);
87 static unsigned long rockchip_usb_phy480m_recalc_rate(struct clk_hw
*hw
,
88 unsigned long parent_rate
)
93 static void rockchip_usb_phy480m_disable(struct clk_hw
*hw
)
95 struct rockchip_usb_phy
*phy
= container_of(hw
,
96 struct rockchip_usb_phy
,
100 regulator_disable(phy
->vbus
);
102 /* Power down usb phy analog blocks by set siddq 1 */
103 rockchip_usb_phy_power(phy
, 1);
106 static int rockchip_usb_phy480m_enable(struct clk_hw
*hw
)
108 struct rockchip_usb_phy
*phy
= container_of(hw
,
109 struct rockchip_usb_phy
,
112 /* Power up usb phy analog blocks by set siddq 0 */
113 return rockchip_usb_phy_power(phy
, 0);
116 static int rockchip_usb_phy480m_is_enabled(struct clk_hw
*hw
)
118 struct rockchip_usb_phy
*phy
= container_of(hw
,
119 struct rockchip_usb_phy
,
124 ret
= regmap_read(phy
->base
->reg_base
, phy
->reg_offset
, &val
);
128 return (val
& UOC_CON0_SIDDQ
) ? 0 : 1;
131 static const struct clk_ops rockchip_usb_phy480m_ops
= {
132 .enable
= rockchip_usb_phy480m_enable
,
133 .disable
= rockchip_usb_phy480m_disable
,
134 .is_enabled
= rockchip_usb_phy480m_is_enabled
,
135 .recalc_rate
= rockchip_usb_phy480m_recalc_rate
,
138 static int rockchip_usb_phy_power_off(struct phy
*_phy
)
140 struct rockchip_usb_phy
*phy
= phy_get_drvdata(_phy
);
142 if (phy
->uart_enabled
)
145 clk_disable_unprepare(phy
->clk480m
);
150 static int rockchip_usb_phy_power_on(struct phy
*_phy
)
152 struct rockchip_usb_phy
*phy
= phy_get_drvdata(_phy
);
154 if (phy
->uart_enabled
)
160 ret
= regulator_enable(phy
->vbus
);
165 return clk_prepare_enable(phy
->clk480m
);
168 static int rockchip_usb_phy_reset(struct phy
*_phy
)
170 struct rockchip_usb_phy
*phy
= phy_get_drvdata(_phy
);
173 reset_control_assert(phy
->reset
);
175 reset_control_deassert(phy
->reset
);
181 static const struct phy_ops ops
= {
182 .power_on
= rockchip_usb_phy_power_on
,
183 .power_off
= rockchip_usb_phy_power_off
,
184 .reset
= rockchip_usb_phy_reset
,
185 .owner
= THIS_MODULE
,
188 static void rockchip_usb_phy_action(void *data
)
190 struct rockchip_usb_phy
*rk_phy
= data
;
192 if (!rk_phy
->uart_enabled
) {
193 of_clk_del_provider(rk_phy
->np
);
194 clk_unregister(rk_phy
->clk480m
);
198 clk_put(rk_phy
->clk
);
201 static int rockchip_usb_phy_init(struct rockchip_usb_phy_base
*base
,
202 struct device_node
*child
)
204 struct rockchip_usb_phy
*rk_phy
;
205 unsigned int reg_offset
;
206 const char *clk_name
;
207 struct clk_init_data init
;
210 rk_phy
= devm_kzalloc(base
->dev
, sizeof(*rk_phy
), GFP_KERNEL
);
217 if (of_property_read_u32(child
, "reg", ®_offset
)) {
218 dev_err(base
->dev
, "missing reg property in node %pOFn\n",
223 rk_phy
->reset
= of_reset_control_get(child
, "phy-reset");
224 if (IS_ERR(rk_phy
->reset
))
225 rk_phy
->reset
= NULL
;
227 rk_phy
->reg_offset
= reg_offset
;
229 rk_phy
->clk
= of_clk_get_by_name(child
, "phyclk");
230 if (IS_ERR(rk_phy
->clk
))
235 while (base
->pdata
->phys
[i
].reg
) {
236 if (base
->pdata
->phys
[i
].reg
== reg_offset
) {
237 init
.name
= base
->pdata
->phys
[i
].pll_name
;
244 dev_err(base
->dev
, "phy data not found\n");
248 if (enable_usb_uart
&& base
->pdata
->usb_uart_phy
== i
) {
249 dev_dbg(base
->dev
, "phy%d used as uart output\n", i
);
250 rk_phy
->uart_enabled
= true;
253 clk_name
= __clk_get_name(rk_phy
->clk
);
255 init
.parent_names
= &clk_name
;
256 init
.num_parents
= 1;
259 init
.parent_names
= NULL
;
260 init
.num_parents
= 0;
263 init
.ops
= &rockchip_usb_phy480m_ops
;
264 rk_phy
->clk480m_hw
.init
= &init
;
266 rk_phy
->clk480m
= clk_register(base
->dev
, &rk_phy
->clk480m_hw
);
267 if (IS_ERR(rk_phy
->clk480m
)) {
268 err
= PTR_ERR(rk_phy
->clk480m
);
272 err
= of_clk_add_provider(child
, of_clk_src_simple_get
,
278 err
= devm_add_action_or_reset(base
->dev
, rockchip_usb_phy_action
,
283 rk_phy
->phy
= devm_phy_create(base
->dev
, child
, &ops
);
284 if (IS_ERR(rk_phy
->phy
)) {
285 dev_err(base
->dev
, "failed to create PHY\n");
286 return PTR_ERR(rk_phy
->phy
);
288 phy_set_drvdata(rk_phy
->phy
, rk_phy
);
290 rk_phy
->vbus
= devm_regulator_get_optional(&rk_phy
->phy
->dev
, "vbus");
291 if (IS_ERR(rk_phy
->vbus
)) {
292 if (PTR_ERR(rk_phy
->vbus
) == -EPROBE_DEFER
)
293 return PTR_ERR(rk_phy
->vbus
);
298 * When acting as uart-pipe, just keep clock on otherwise
299 * only power up usb phy when it use, so disable it when init
301 if (rk_phy
->uart_enabled
)
302 return clk_prepare_enable(rk_phy
->clk
);
304 return rockchip_usb_phy_power(rk_phy
, 1);
307 if (!rk_phy
->uart_enabled
)
308 clk_unregister(rk_phy
->clk480m
);
311 clk_put(rk_phy
->clk
);
315 static const struct rockchip_usb_phy_pdata rk3066a_pdata
= {
316 .phys
= (struct rockchip_usb_phys
[]){
317 { .reg
= 0x17c, .pll_name
= "sclk_otgphy0_480m" },
318 { .reg
= 0x188, .pll_name
= "sclk_otgphy1_480m" },
323 static int __init
rockchip_init_usb_uart_common(struct regmap
*grf
,
324 const struct rockchip_usb_phy_pdata
*pdata
)
326 int regoffs
= pdata
->phys
[pdata
->usb_uart_phy
].reg
;
331 * COMMON_ON and DISABLE settings are described in the TRM,
332 * but were not present in the original code.
333 * Also disable the analog phy components to save power.
335 val
= HIWORD_UPDATE(UOC_CON0_COMMON_ON_N
341 ret
= regmap_write(grf
, regoffs
+ UOC_CON0
, val
);
345 val
= HIWORD_UPDATE(UOC_CON2_SOFT_CON_SEL
,
346 UOC_CON2_SOFT_CON_SEL
);
347 ret
= regmap_write(grf
, regoffs
+ UOC_CON2
, val
);
351 val
= HIWORD_UPDATE(UOC_CON3_UTMI_OPMODE_NODRIVING
352 | UOC_CON3_UTMI_XCVRSEELCT_FSTRANSC
353 | UOC_CON3_UTMI_TERMSEL_FULLSPEED
,
354 UOC_CON3_UTMI_SUSPENDN
355 | UOC_CON3_UTMI_OPMODE_MASK
356 | UOC_CON3_UTMI_XCVRSEELCT_MASK
357 | UOC_CON3_UTMI_TERMSEL_FULLSPEED
);
358 ret
= regmap_write(grf
, UOC_CON3
, val
);
365 #define RK3188_UOC0_CON0 0x10c
366 #define RK3188_UOC0_CON0_BYPASSSEL BIT(9)
367 #define RK3188_UOC0_CON0_BYPASSDMEN BIT(8)
370 * Enable the bypass of uart2 data through the otg usb phy.
371 * See description of rk3288-variant for details.
373 static int __init
rk3188_init_usb_uart(struct regmap
*grf
,
374 const struct rockchip_usb_phy_pdata
*pdata
)
379 ret
= rockchip_init_usb_uart_common(grf
, pdata
);
383 val
= HIWORD_UPDATE(RK3188_UOC0_CON0_BYPASSSEL
384 | RK3188_UOC0_CON0_BYPASSDMEN
,
385 RK3188_UOC0_CON0_BYPASSSEL
386 | RK3188_UOC0_CON0_BYPASSDMEN
);
387 ret
= regmap_write(grf
, RK3188_UOC0_CON0
, val
);
394 static const struct rockchip_usb_phy_pdata rk3188_pdata
= {
395 .phys
= (struct rockchip_usb_phys
[]){
396 { .reg
= 0x10c, .pll_name
= "sclk_otgphy0_480m" },
397 { .reg
= 0x11c, .pll_name
= "sclk_otgphy1_480m" },
400 .init_usb_uart
= rk3188_init_usb_uart
,
404 #define RK3288_UOC0_CON3 0x32c
405 #define RK3288_UOC0_CON3_BYPASSDMEN BIT(6)
406 #define RK3288_UOC0_CON3_BYPASSSEL BIT(7)
409 * Enable the bypass of uart2 data through the otg usb phy.
410 * Original description in the TRM.
411 * 1. Disable the OTG block by setting OTGDISABLE0 to 1’b1.
412 * 2. Disable the pull-up resistance on the D+ line by setting
413 * OPMODE0[1:0] to 2’b01.
414 * 3. To ensure that the XO, Bias, and PLL blocks are powered down in Suspend
415 * mode, set COMMONONN to 1’b1.
416 * 4. Place the USB PHY in Suspend mode by setting SUSPENDM0 to 1’b0.
417 * 5. Set BYPASSSEL0 to 1’b1.
418 * 6. To transmit data, controls BYPASSDMEN0, and BYPASSDMDATA0.
419 * To receive data, monitor FSVPLUS0.
421 * The actual code in the vendor kernel does some things differently.
423 static int __init
rk3288_init_usb_uart(struct regmap
*grf
,
424 const struct rockchip_usb_phy_pdata
*pdata
)
429 ret
= rockchip_init_usb_uart_common(grf
, pdata
);
433 val
= HIWORD_UPDATE(RK3288_UOC0_CON3_BYPASSSEL
434 | RK3288_UOC0_CON3_BYPASSDMEN
,
435 RK3288_UOC0_CON3_BYPASSSEL
436 | RK3288_UOC0_CON3_BYPASSDMEN
);
437 ret
= regmap_write(grf
, RK3288_UOC0_CON3
, val
);
444 static const struct rockchip_usb_phy_pdata rk3288_pdata
= {
445 .phys
= (struct rockchip_usb_phys
[]){
446 { .reg
= 0x320, .pll_name
= "sclk_otgphy0_480m" },
447 { .reg
= 0x334, .pll_name
= "sclk_otgphy1_480m" },
448 { .reg
= 0x348, .pll_name
= "sclk_otgphy2_480m" },
451 .init_usb_uart
= rk3288_init_usb_uart
,
455 static int rockchip_usb_phy_probe(struct platform_device
*pdev
)
457 struct device
*dev
= &pdev
->dev
;
458 struct rockchip_usb_phy_base
*phy_base
;
459 struct phy_provider
*phy_provider
;
460 struct device_node
*child
;
463 phy_base
= devm_kzalloc(dev
, sizeof(*phy_base
), GFP_KERNEL
);
467 phy_base
->pdata
= device_get_match_data(dev
);
468 if (!phy_base
->pdata
) {
469 dev_err(dev
, "missing phy data\n");
474 phy_base
->reg_base
= ERR_PTR(-ENODEV
);
475 if (dev
->parent
&& dev
->parent
->of_node
)
476 phy_base
->reg_base
= syscon_node_to_regmap(
477 dev
->parent
->of_node
);
478 if (IS_ERR(phy_base
->reg_base
))
479 phy_base
->reg_base
= syscon_regmap_lookup_by_phandle(
480 dev
->of_node
, "rockchip,grf");
481 if (IS_ERR(phy_base
->reg_base
)) {
482 dev_err(&pdev
->dev
, "Missing rockchip,grf property\n");
483 return PTR_ERR(phy_base
->reg_base
);
486 for_each_available_child_of_node(dev
->of_node
, child
) {
487 err
= rockchip_usb_phy_init(phy_base
, child
);
494 phy_provider
= devm_of_phy_provider_register(dev
, of_phy_simple_xlate
);
495 return PTR_ERR_OR_ZERO(phy_provider
);
498 static const struct of_device_id rockchip_usb_phy_dt_ids
[] = {
499 { .compatible
= "rockchip,rk3066a-usb-phy", .data
= &rk3066a_pdata
},
500 { .compatible
= "rockchip,rk3188-usb-phy", .data
= &rk3188_pdata
},
501 { .compatible
= "rockchip,rk3288-usb-phy", .data
= &rk3288_pdata
},
505 MODULE_DEVICE_TABLE(of
, rockchip_usb_phy_dt_ids
);
507 static struct platform_driver rockchip_usb_driver
= {
508 .probe
= rockchip_usb_phy_probe
,
510 .name
= "rockchip-usb-phy",
511 .of_match_table
= rockchip_usb_phy_dt_ids
,
515 module_platform_driver(rockchip_usb_driver
);
518 static int __init
rockchip_init_usb_uart(void)
520 const struct of_device_id
*match
;
521 const struct rockchip_usb_phy_pdata
*data
;
522 struct device_node
*np
;
526 if (!enable_usb_uart
)
529 np
= of_find_matching_node_and_match(NULL
, rockchip_usb_phy_dt_ids
,
532 pr_err("%s: failed to find usbphy node\n", __func__
);
536 pr_debug("%s: using settings for %s\n", __func__
, match
->compatible
);
539 if (!data
->init_usb_uart
) {
540 pr_err("%s: usb-uart not available on %s\n",
541 __func__
, match
->compatible
);
545 grf
= ERR_PTR(-ENODEV
);
547 grf
= syscon_node_to_regmap(np
->parent
);
549 grf
= syscon_regmap_lookup_by_phandle(np
, "rockchip,grf");
551 pr_err("%s: Missing rockchip,grf property, %lu\n",
552 __func__
, PTR_ERR(grf
));
556 ret
= data
->init_usb_uart(grf
, data
);
558 pr_err("%s: could not init usb_uart, %d\n", __func__
, ret
);
565 early_initcall(rockchip_init_usb_uart
);
567 static int __init
rockchip_usb_uart(char *buf
)
569 enable_usb_uart
= true;
572 early_param("rockchip.usb_uart", rockchip_usb_uart
);
575 MODULE_AUTHOR("Yunzhi Li <lyz@rock-chips.com>");
576 MODULE_DESCRIPTION("Rockchip USB 2.0 PHY driver");
577 MODULE_LICENSE("GPL v2");