1 // SPDX-License-Identifier: GPL-2.0+
3 * StarFive JH7110 USB 2.0 PHY driver
5 * Copyright (C) 2023 StarFive Technology Co., Ltd.
6 * Author: Minda Chen <minda.chen@starfivetech.com>
9 #include <linux/bits.h>
10 #include <linux/clk.h>
11 #include <linux/err.h>
13 #include <linux/mfd/syscon.h>
14 #include <linux/module.h>
15 #include <linux/phy/phy.h>
16 #include <linux/platform_device.h>
17 #include <linux/regmap.h>
18 #include <linux/usb/of.h>
20 #define USB_125M_CLK_RATE 125000000
21 #define USB_LS_KEEPALIVE_OFF 0x4
22 #define USB_LS_KEEPALIVE_ENABLE BIT(4)
24 #define USB_PDRSTN_SPLIT BIT(17)
25 #define SYSCON_USB_SPLIT_OFFSET 0x18
27 struct jh7110_usb2_phy
{
30 struct regmap
*sys_syscon
;
31 struct clk
*usb_125m_clk
;
36 static void usb2_set_ls_keepalive(struct jh7110_usb2_phy
*phy
, bool set
)
40 /* Host mode enable the LS speed keep-alive signal */
41 val
= readl(phy
->regs
+ USB_LS_KEEPALIVE_OFF
);
43 val
|= USB_LS_KEEPALIVE_ENABLE
;
45 val
&= ~USB_LS_KEEPALIVE_ENABLE
;
47 writel(val
, phy
->regs
+ USB_LS_KEEPALIVE_OFF
);
50 static int usb2_phy_set_mode(struct phy
*_phy
,
51 enum phy_mode mode
, int submode
)
53 struct jh7110_usb2_phy
*phy
= phy_get_drvdata(_phy
);
56 case PHY_MODE_USB_HOST
:
57 case PHY_MODE_USB_DEVICE
:
58 case PHY_MODE_USB_OTG
:
64 if (mode
!= phy
->mode
) {
65 dev_dbg(&_phy
->dev
, "Changing phy to %d\n", mode
);
67 usb2_set_ls_keepalive(phy
, (mode
!= PHY_MODE_USB_DEVICE
));
70 /* Connect usb 2.0 phy mode */
71 regmap_update_bits(phy
->sys_syscon
, SYSCON_USB_SPLIT_OFFSET
,
72 USB_PDRSTN_SPLIT
, USB_PDRSTN_SPLIT
);
77 static int jh7110_usb2_phy_init(struct phy
*_phy
)
79 struct jh7110_usb2_phy
*phy
= phy_get_drvdata(_phy
);
82 ret
= clk_set_rate(phy
->usb_125m_clk
, USB_125M_CLK_RATE
);
86 ret
= clk_prepare_enable(phy
->app_125m
);
93 static int jh7110_usb2_phy_exit(struct phy
*_phy
)
95 struct jh7110_usb2_phy
*phy
= phy_get_drvdata(_phy
);
97 clk_disable_unprepare(phy
->app_125m
);
102 static const struct phy_ops jh7110_usb2_phy_ops
= {
103 .init
= jh7110_usb2_phy_init
,
104 .exit
= jh7110_usb2_phy_exit
,
105 .set_mode
= usb2_phy_set_mode
,
106 .owner
= THIS_MODULE
,
109 static int jh7110_usb_phy_probe(struct platform_device
*pdev
)
111 struct jh7110_usb2_phy
*phy
;
112 struct device
*dev
= &pdev
->dev
;
113 struct phy_provider
*phy_provider
;
115 phy
= devm_kzalloc(dev
, sizeof(*phy
), GFP_KERNEL
);
119 phy
->usb_125m_clk
= devm_clk_get(dev
, "125m");
120 if (IS_ERR(phy
->usb_125m_clk
))
121 return dev_err_probe(dev
, PTR_ERR(phy
->usb_125m_clk
),
122 "Failed to get 125m clock\n");
124 phy
->app_125m
= devm_clk_get(dev
, "app_125m");
125 if (IS_ERR(phy
->app_125m
))
126 return dev_err_probe(dev
, PTR_ERR(phy
->app_125m
),
127 "Failed to get app 125m clock\n");
129 phy
->regs
= devm_platform_ioremap_resource(pdev
, 0);
130 if (IS_ERR(phy
->regs
))
131 return dev_err_probe(dev
, PTR_ERR(phy
->regs
),
132 "Failed to map phy base\n");
134 phy
->phy
= devm_phy_create(dev
, NULL
, &jh7110_usb2_phy_ops
);
135 if (IS_ERR(phy
->phy
))
136 return dev_err_probe(dev
, PTR_ERR(phy
->phy
),
137 "Failed to create phy\n");
139 phy_set_drvdata(phy
->phy
, phy
);
140 phy_provider
= devm_of_phy_provider_register(dev
, of_phy_simple_xlate
);
143 syscon_regmap_lookup_by_compatible("starfive,jh7110-sys-syscon");
144 if (IS_ERR(phy
->sys_syscon
))
145 return dev_err_probe(dev
, PTR_ERR(phy
->sys_syscon
),
146 "Failed to get sys-syscon\n");
148 return PTR_ERR_OR_ZERO(phy_provider
);
151 static const struct of_device_id jh7110_usb_phy_of_match
[] = {
152 { .compatible
= "starfive,jh7110-usb-phy" },
155 MODULE_DEVICE_TABLE(of
, jh7110_usb_phy_of_match
);
157 static struct platform_driver jh7110_usb_phy_driver
= {
158 .probe
= jh7110_usb_phy_probe
,
160 .of_match_table
= jh7110_usb_phy_of_match
,
161 .name
= "jh7110-usb-phy",
164 module_platform_driver(jh7110_usb_phy_driver
);
166 MODULE_DESCRIPTION("StarFive JH7110 USB 2.0 PHY driver");
167 MODULE_AUTHOR("Minda Chen <minda.chen@starfivetech.com>");
168 MODULE_LICENSE("GPL");