1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * StarFive JH7110 USB 2.0 PHY driver
4  *
5  * Copyright (C) 2023 StarFive Technology Co., Ltd.
6  * Author: Minda Chen <minda.chen@starfivetech.com>
7  */
8 
9 #include <linux/bits.h>
10 #include <linux/clk.h>
11 #include <linux/err.h>
12 #include <linux/io.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>
19 
20 #define USB_125M_CLK_RATE		125000000
21 #define USB_LS_KEEPALIVE_OFF		0x4
22 #define USB_LS_KEEPALIVE_ENABLE		BIT(4)
23 
24 #define USB_PDRSTN_SPLIT		BIT(17)
25 #define SYSCON_USB_SPLIT_OFFSET		0x18
26 
27 struct jh7110_usb2_phy {
28 	struct phy *phy;
29 	void __iomem *regs;
30 	struct regmap *sys_syscon;
31 	struct clk *usb_125m_clk;
32 	struct clk *app_125m;
33 	enum phy_mode mode;
34 };
35 
usb2_set_ls_keepalive(struct jh7110_usb2_phy * phy,bool set)36 static void usb2_set_ls_keepalive(struct jh7110_usb2_phy *phy, bool set)
37 {
38 	unsigned int val;
39 
40 	/* Host mode enable the LS speed keep-alive signal */
41 	val = readl(phy->regs + USB_LS_KEEPALIVE_OFF);
42 	if (set)
43 		val |= USB_LS_KEEPALIVE_ENABLE;
44 	else
45 		val &= ~USB_LS_KEEPALIVE_ENABLE;
46 
47 	writel(val, phy->regs + USB_LS_KEEPALIVE_OFF);
48 }
49 
usb2_phy_set_mode(struct phy * _phy,enum phy_mode mode,int submode)50 static int usb2_phy_set_mode(struct phy *_phy,
51 			     enum phy_mode mode, int submode)
52 {
53 	struct jh7110_usb2_phy *phy = phy_get_drvdata(_phy);
54 
55 	switch (mode) {
56 	case PHY_MODE_USB_HOST:
57 	case PHY_MODE_USB_DEVICE:
58 	case PHY_MODE_USB_OTG:
59 		break;
60 	default:
61 		return -EINVAL;
62 	}
63 
64 	if (mode != phy->mode) {
65 		dev_dbg(&_phy->dev, "Changing phy to %d\n", mode);
66 		phy->mode = mode;
67 		usb2_set_ls_keepalive(phy, (mode != PHY_MODE_USB_DEVICE));
68 	}
69 
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);
73 
74 	return 0;
75 }
76 
jh7110_usb2_phy_init(struct phy * _phy)77 static int jh7110_usb2_phy_init(struct phy *_phy)
78 {
79 	struct jh7110_usb2_phy *phy = phy_get_drvdata(_phy);
80 	int ret;
81 
82 	ret = clk_set_rate(phy->usb_125m_clk, USB_125M_CLK_RATE);
83 	if (ret)
84 		return ret;
85 
86 	ret = clk_prepare_enable(phy->app_125m);
87 	if (ret)
88 		return ret;
89 
90 	return 0;
91 }
92 
jh7110_usb2_phy_exit(struct phy * _phy)93 static int jh7110_usb2_phy_exit(struct phy *_phy)
94 {
95 	struct jh7110_usb2_phy *phy = phy_get_drvdata(_phy);
96 
97 	clk_disable_unprepare(phy->app_125m);
98 
99 	return 0;
100 }
101 
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,
107 };
108 
jh7110_usb_phy_probe(struct platform_device * pdev)109 static int jh7110_usb_phy_probe(struct platform_device *pdev)
110 {
111 	struct jh7110_usb2_phy *phy;
112 	struct device *dev = &pdev->dev;
113 	struct phy_provider *phy_provider;
114 
115 	phy = devm_kzalloc(dev, sizeof(*phy), GFP_KERNEL);
116 	if (!phy)
117 		return -ENOMEM;
118 
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");
123 
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");
128 
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");
133 
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");
138 
139 	phy_set_drvdata(phy->phy, phy);
140 	phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
141 
142 	phy->sys_syscon =
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");
147 
148 	return PTR_ERR_OR_ZERO(phy_provider);
149 }
150 
151 static const struct of_device_id jh7110_usb_phy_of_match[] = {
152 	{ .compatible = "starfive,jh7110-usb-phy" },
153 	{ /* sentinel */ },
154 };
155 MODULE_DEVICE_TABLE(of, jh7110_usb_phy_of_match);
156 
157 static struct platform_driver jh7110_usb_phy_driver = {
158 	.probe	= jh7110_usb_phy_probe,
159 	.driver = {
160 		.of_match_table	= jh7110_usb_phy_of_match,
161 		.name  = "jh7110-usb-phy",
162 	}
163 };
164 module_platform_driver(jh7110_usb_phy_driver);
165 
166 MODULE_DESCRIPTION("StarFive JH7110 USB 2.0 PHY driver");
167 MODULE_AUTHOR("Minda Chen <minda.chen@starfivetech.com>");
168 MODULE_LICENSE("GPL");
169