blob: a01148db220daeb40750c09ca32380c0c4669f3d [file] [log] [blame]
Jagan Teki70e882f2020-05-26 11:33:44 +08001// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Rockchip USB2.0 PHY with Innosilicon IP block driver
4 *
5 * Copyright (C) 2016 Fuzhou Rockchip Electronics Co., Ltd
6 * Copyright (C) 2020 Amarula Solutions(India)
7 */
8
9#include <common.h>
10#include <clk.h>
11#include <dm.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060012#include <asm/global_data.h>
Jagan Teki70e882f2020-05-26 11:33:44 +080013#include <dm/device_compat.h>
14#include <dm/lists.h>
15#include <generic-phy.h>
16#include <reset.h>
17#include <syscon.h>
18#include <asm/gpio.h>
19#include <asm/io.h>
20#include <linux/iopoll.h>
21#include <asm/arch-rockchip/clock.h>
22
23DECLARE_GLOBAL_DATA_PTR;
24
25#define usleep_range(a, b) udelay((b))
26#define BIT_WRITEABLE_SHIFT 16
27
28enum rockchip_usb2phy_port_id {
29 USB2PHY_PORT_OTG,
30 USB2PHY_PORT_HOST,
31 USB2PHY_NUM_PORTS,
32};
33
34struct usb2phy_reg {
35 unsigned int offset;
36 unsigned int bitend;
37 unsigned int bitstart;
38 unsigned int disable;
39 unsigned int enable;
40};
41
42struct rockchip_usb2phy_port_cfg {
43 struct usb2phy_reg phy_sus;
44 struct usb2phy_reg bvalid_det_en;
45 struct usb2phy_reg bvalid_det_st;
46 struct usb2phy_reg bvalid_det_clr;
47 struct usb2phy_reg ls_det_en;
48 struct usb2phy_reg ls_det_st;
49 struct usb2phy_reg ls_det_clr;
50 struct usb2phy_reg utmi_avalid;
51 struct usb2phy_reg utmi_bvalid;
52 struct usb2phy_reg utmi_ls;
53 struct usb2phy_reg utmi_hstdet;
54};
55
56struct rockchip_usb2phy_cfg {
57 unsigned int reg;
58 const struct rockchip_usb2phy_port_cfg port_cfgs[USB2PHY_NUM_PORTS];
59};
60
61struct rockchip_usb2phy {
62 void *reg_base;
63 struct clk phyclk;
64 const struct rockchip_usb2phy_cfg *phy_cfg;
65};
66
67static inline int property_enable(void *reg_base,
68 const struct usb2phy_reg *reg, bool en)
69{
70 unsigned int val, mask, tmp;
71
72 tmp = en ? reg->enable : reg->disable;
73 mask = GENMASK(reg->bitend, reg->bitstart);
74 val = (tmp << reg->bitstart) | (mask << BIT_WRITEABLE_SHIFT);
75
76 return writel(val, reg_base + reg->offset);
77}
78
79static const
80struct rockchip_usb2phy_port_cfg *us2phy_get_port(struct phy *phy)
81{
82 struct udevice *parent = dev_get_parent(phy->dev);
83 struct rockchip_usb2phy *priv = dev_get_priv(parent);
84 const struct rockchip_usb2phy_cfg *phy_cfg = priv->phy_cfg;
85
86 return &phy_cfg->port_cfgs[phy->id];
87}
88
89static int rockchip_usb2phy_power_on(struct phy *phy)
90{
91 struct udevice *parent = dev_get_parent(phy->dev);
92 struct rockchip_usb2phy *priv = dev_get_priv(parent);
93 const struct rockchip_usb2phy_port_cfg *port_cfg = us2phy_get_port(phy);
94
95 property_enable(priv->reg_base, &port_cfg->phy_sus, false);
96
97 /* waiting for the utmi_clk to become stable */
98 usleep_range(1500, 2000);
99
100 return 0;
101}
102
103static int rockchip_usb2phy_power_off(struct phy *phy)
104{
105 struct udevice *parent = dev_get_parent(phy->dev);
106 struct rockchip_usb2phy *priv = dev_get_priv(parent);
107 const struct rockchip_usb2phy_port_cfg *port_cfg = us2phy_get_port(phy);
108
109 property_enable(priv->reg_base, &port_cfg->phy_sus, true);
110
111 return 0;
112}
113
114static int rockchip_usb2phy_init(struct phy *phy)
115{
116 struct udevice *parent = dev_get_parent(phy->dev);
117 struct rockchip_usb2phy *priv = dev_get_priv(parent);
118 const struct rockchip_usb2phy_port_cfg *port_cfg = us2phy_get_port(phy);
119 int ret;
120
121 ret = clk_enable(&priv->phyclk);
John Keeping86cd8342022-12-06 12:48:55 +0000122 if (ret && ret != -ENOSYS) {
Jagan Teki70e882f2020-05-26 11:33:44 +0800123 dev_err(phy->dev, "failed to enable phyclk (ret=%d)\n", ret);
124 return ret;
125 }
126
127 if (phy->id == USB2PHY_PORT_OTG) {
128 property_enable(priv->reg_base, &port_cfg->bvalid_det_clr, true);
129 property_enable(priv->reg_base, &port_cfg->bvalid_det_en, true);
130 } else if (phy->id == USB2PHY_PORT_HOST) {
131 property_enable(priv->reg_base, &port_cfg->bvalid_det_clr, true);
132 property_enable(priv->reg_base, &port_cfg->bvalid_det_en, true);
133 }
134
135 return 0;
136}
137
138static int rockchip_usb2phy_exit(struct phy *phy)
139{
140 struct udevice *parent = dev_get_parent(phy->dev);
141 struct rockchip_usb2phy *priv = dev_get_priv(parent);
142
143 clk_disable(&priv->phyclk);
144
145 return 0;
146}
147
148static int rockchip_usb2phy_of_xlate(struct phy *phy,
149 struct ofnode_phandle_args *args)
150{
151 const char *name = phy->dev->name;
152
153 if (!strcasecmp(name, "host-port"))
154 phy->id = USB2PHY_PORT_HOST;
155 else if (!strcasecmp(name, "otg-port"))
156 phy->id = USB2PHY_PORT_OTG;
157 else
158 dev_err(phy->dev, "improper %s device\n", name);
159
160 return 0;
161}
162
163static struct phy_ops rockchip_usb2phy_ops = {
164 .init = rockchip_usb2phy_init,
165 .exit = rockchip_usb2phy_exit,
166 .power_on = rockchip_usb2phy_power_on,
167 .power_off = rockchip_usb2phy_power_off,
168 .of_xlate = rockchip_usb2phy_of_xlate,
169};
170
171static int rockchip_usb2phy_probe(struct udevice *dev)
172{
173 struct rockchip_usb2phy *priv = dev_get_priv(dev);
174 const struct rockchip_usb2phy_cfg *phy_cfgs;
175 unsigned int reg;
176 int index, ret;
177
178 priv->reg_base = syscon_get_first_range(ROCKCHIP_SYSCON_GRF);
179 if (IS_ERR(priv->reg_base))
180 return PTR_ERR(priv->reg_base);
181
Jagan Teki1b799512023-02-17 17:28:39 +0530182 ret = ofnode_read_u32_index(dev_ofnode(dev), "reg", 0, &reg);
Jagan Teki70e882f2020-05-26 11:33:44 +0800183 if (ret) {
184 dev_err(dev, "failed to read reg property (ret = %d)\n", ret);
185 return ret;
186 }
187
Jagan Teki1b799512023-02-17 17:28:39 +0530188 /* support address_cells=2 */
189 if (reg == 0) {
190 if (ofnode_read_u32_index(dev_ofnode(dev), "reg", 1, &reg)) {
191 dev_err(dev, "%s must have reg[1]\n",
192 ofnode_get_name(dev_ofnode(dev)));
193 return -EINVAL;
194 }
195 }
196
Jagan Teki70e882f2020-05-26 11:33:44 +0800197 phy_cfgs = (const struct rockchip_usb2phy_cfg *)
198 dev_get_driver_data(dev);
199 if (!phy_cfgs)
200 return -EINVAL;
201
202 /* find out a proper config which can be matched with dt. */
203 index = 0;
204 while (phy_cfgs[index].reg) {
205 if (phy_cfgs[index].reg == reg) {
206 priv->phy_cfg = &phy_cfgs[index];
207 break;
208 }
209
210 ++index;
211 }
212
213 if (!priv->phy_cfg) {
214 dev_err(dev, "failed find proper phy-cfg\n");
215 return -EINVAL;
216 }
217
218 ret = clk_get_by_name(dev, "phyclk", &priv->phyclk);
219 if (ret) {
220 dev_err(dev, "failed to get the phyclk (ret=%d)\n", ret);
221 return ret;
222 }
223
224 return 0;
225}
226
227static int rockchip_usb2phy_bind(struct udevice *dev)
228{
229 struct udevice *usb2phy_dev;
230 ofnode node;
231 const char *name;
232 int ret = 0;
233
234 dev_for_each_subnode(node, dev) {
235 if (!ofnode_valid(node)) {
236 dev_info(dev, "subnode %s not found\n", dev->name);
237 return -ENXIO;
238 }
239
240 name = ofnode_get_name(node);
241 dev_dbg(dev, "subnode %s\n", name);
242
243 ret = device_bind_driver_to_node(dev, "rockchip_usb2phy_port",
244 name, node, &usb2phy_dev);
245 if (ret) {
246 dev_err(dev,
247 "'%s' cannot bind 'rockchip_usb2phy_port'\n", name);
248 return ret;
249 }
250 }
251
252 return ret;
253}
254
255static const struct rockchip_usb2phy_cfg rk3399_usb2phy_cfgs[] = {
256 {
257 .reg = 0xe450,
258 .port_cfgs = {
259 [USB2PHY_PORT_OTG] = {
260 .phy_sus = { 0xe454, 1, 0, 2, 1 },
261 .bvalid_det_en = { 0xe3c0, 3, 3, 0, 1 },
262 .bvalid_det_st = { 0xe3e0, 3, 3, 0, 1 },
263 .bvalid_det_clr = { 0xe3d0, 3, 3, 0, 1 },
264 .utmi_avalid = { 0xe2ac, 7, 7, 0, 1 },
265 .utmi_bvalid = { 0xe2ac, 12, 12, 0, 1 },
266 },
267 [USB2PHY_PORT_HOST] = {
268 .phy_sus = { 0xe458, 1, 0, 0x2, 0x1 },
269 .ls_det_en = { 0xe3c0, 6, 6, 0, 1 },
270 .ls_det_st = { 0xe3e0, 6, 6, 0, 1 },
271 .ls_det_clr = { 0xe3d0, 6, 6, 0, 1 },
272 .utmi_ls = { 0xe2ac, 22, 21, 0, 1 },
273 .utmi_hstdet = { 0xe2ac, 23, 23, 0, 1 }
274 }
275 },
276 },
277 {
278 .reg = 0xe460,
279 .port_cfgs = {
280 [USB2PHY_PORT_OTG] = {
281 .phy_sus = { 0xe464, 1, 0, 2, 1 },
282 .bvalid_det_en = { 0xe3c0, 8, 8, 0, 1 },
283 .bvalid_det_st = { 0xe3e0, 8, 8, 0, 1 },
284 .bvalid_det_clr = { 0xe3d0, 8, 8, 0, 1 },
285 .utmi_avalid = { 0xe2ac, 10, 10, 0, 1 },
286 .utmi_bvalid = { 0xe2ac, 16, 16, 0, 1 },
287 },
288 [USB2PHY_PORT_HOST] = {
289 .phy_sus = { 0xe468, 1, 0, 0x2, 0x1 },
290 .ls_det_en = { 0xe3c0, 11, 11, 0, 1 },
291 .ls_det_st = { 0xe3e0, 11, 11, 0, 1 },
292 .ls_det_clr = { 0xe3d0, 11, 11, 0, 1 },
293 .utmi_ls = { 0xe2ac, 26, 25, 0, 1 },
294 .utmi_hstdet = { 0xe2ac, 27, 27, 0, 1 }
295 }
296 },
297 },
298 { /* sentinel */ }
299};
300
301static const struct udevice_id rockchip_usb2phy_ids[] = {
302 {
303 .compatible = "rockchip,rk3399-usb2phy",
304 .data = (ulong)&rk3399_usb2phy_cfgs,
305 },
306 { /* sentinel */ }
307};
308
309U_BOOT_DRIVER(rockchip_usb2phy_port) = {
310 .name = "rockchip_usb2phy_port",
311 .id = UCLASS_PHY,
312 .ops = &rockchip_usb2phy_ops,
313};
314
315U_BOOT_DRIVER(rockchip_usb2phy) = {
316 .name = "rockchip_usb2phy",
317 .id = UCLASS_PHY,
318 .of_match = rockchip_usb2phy_ids,
319 .probe = rockchip_usb2phy_probe,
320 .bind = rockchip_usb2phy_bind,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700321 .priv_auto = sizeof(struct rockchip_usb2phy),
Jagan Teki70e882f2020-05-26 11:33:44 +0800322};