blob: 3e693c5ff310ad5e7a513ff1849856117b26cf73 [file] [log] [blame]
Neil Armstrongc1a7e72c2020-09-10 10:48:13 +02001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Amlogic GXL DWC3 Glue layer
4 *
5 * Copyright (C) 2019 BayLibre, SAS
6 * Author: Neil Armstrong <narmstrong@baylibre.com>
7 */
8
9#define DEBUG
Neil Armstrongc1a7e72c2020-09-10 10:48:13 +020010#include <dm.h>
11#include <dm/device-internal.h>
12#include <dm/lists.h>
13#include <dwc3-uboot.h>
14#include <generic-phy.h>
Igor Prusov89606c02023-11-14 14:02:56 +030015#include <linux/io.h>
Neil Armstrongc1a7e72c2020-09-10 10:48:13 +020016#include <linux/usb/ch9.h>
17#include <linux/usb/gadget.h>
18#include <malloc.h>
19#include <regmap.h>
20#include <usb.h>
21#include "core.h"
22#include "gadget.h"
23#include <reset.h>
24#include <clk.h>
25#include <power/regulator.h>
26#include <linux/bitfield.h>
27#include <linux/bitops.h>
28#include <linux/compat.h>
29#include <asm/arch/usb-gx.h>
30
31/* USB Glue Control Registers */
32
33#define USB_R0 0x00
34 #define USB_R0_P30_FSEL_MASK GENMASK(5, 0)
35 #define USB_R0_P30_PHY_RESET BIT(6)
36 #define USB_R0_P30_TEST_POWERDOWN_HSP BIT(7)
37 #define USB_R0_P30_TEST_POWERDOWN_SSP BIT(8)
38 #define USB_R0_P30_ACJT_LEVEL_MASK GENMASK(13, 9)
39 #define USB_R0_P30_TX_BOOST_LEVEL_MASK GENMASK(16, 14)
40 #define USB_R0_P30_LANE0_TX2RX_LOOPBACK BIT(17)
41 #define USB_R0_P30_LANE0_EXT_PCLK_REQ BIT(18)
42 #define USB_R0_P30_PCS_RX_LOS_MASK_VAL_MASK GENMASK(28, 19)
43 #define USB_R0_U2D_SS_SCALEDOWN_MODE_MASK GENMASK(30, 29)
44 #define USB_R0_U2D_ACT BIT(31)
45
46#define USB_R1 0x04
47 #define USB_R1_U3H_BIGENDIAN_GS BIT(0)
48 #define USB_R1_U3H_PME_ENABLE BIT(1)
49 #define USB_R1_U3H_HUB_PORT_OVERCURRENT_MASK GENMASK(6, 2)
50 #define USB_R1_U3H_HUB_PORT_PERM_ATTACH_MASK GENMASK(11, 7)
51 #define USB_R1_U3H_HOST_U2_PORT_DISABLE_MASK GENMASK(15, 12)
52 #define USB_R1_U3H_HOST_U3_PORT_DISABLE BIT(16)
53 #define USB_R1_U3H_HOST_PORT_POWER_CONTROL_PRESENT BIT(17)
54 #define USB_R1_U3H_HOST_MSI_ENABLE BIT(18)
55 #define USB_R1_U3H_FLADJ_30MHZ_REG_MASK GENMASK(24, 19)
56 #define USB_R1_P30_PCS_TX_SWING_FULL_MASK GENMASK(31, 25)
57
58#define USB_R2 0x08
59 #define USB_R2_P30_CR_DATA_IN_MASK GENMASK(15, 0)
60 #define USB_R2_P30_CR_READ BIT(16)
61 #define USB_R2_P30_CR_WRITE BIT(17)
62 #define USB_R2_P30_CR_CAP_ADDR BIT(18)
63 #define USB_R2_P30_CR_CAP_DATA BIT(19)
64 #define USB_R2_P30_PCS_TX_DEEMPH_3P5DB_MASK GENMASK(25, 20)
65 #define USB_R2_P30_PCS_TX_DEEMPH_6DB_MASK GENMASK(31, 26)
66
67#define USB_R3 0x0c
68 #define USB_R3_P30_SSC_ENABLE BIT(0)
69 #define USB_R3_P30_SSC_RANGE_MASK GENMASK(3, 1)
70 #define USB_R3_P30_SSC_REF_CLK_SEL_MASK GENMASK(12, 4)
71 #define USB_R3_P30_REF_SSP_EN BIT(13)
72 #define USB_R3_P30_LOS_BIAS_MASK GENMASK(18, 16)
73 #define USB_R3_P30_LOS_LEVEL_MASK GENMASK(23, 19)
74 #define USB_R3_P30_MPLL_MULTIPLIER_MASK GENMASK(30, 24)
75
76#define USB_R4 0x10
77 #define USB_R4_P21_PORT_RESET_0 BIT(0)
78 #define USB_R4_P21_SLEEP_M0 BIT(1)
79 #define USB_R4_MEM_PD_MASK GENMASK(3, 2)
80 #define USB_R4_P21_ONLY BIT(4)
81
82#define USB_R5 0x14
83 #define USB_R5_ID_DIG_SYNC BIT(0)
84 #define USB_R5_ID_DIG_REG BIT(1)
85 #define USB_R5_ID_DIG_CFG_MASK GENMASK(3, 2)
86 #define USB_R5_ID_DIG_EN_0 BIT(4)
87 #define USB_R5_ID_DIG_EN_1 BIT(5)
88 #define USB_R5_ID_DIG_CURR BIT(6)
89 #define USB_R5_ID_DIG_IRQ BIT(7)
90 #define USB_R5_ID_DIG_TH_MASK GENMASK(15, 8)
91 #define USB_R5_ID_DIG_CNT_MASK GENMASK(23, 16)
92
93/* read-only register */
94#define USB_R6 0x18
95 #define USB_R6_P30_CR_DATA_OUT_MASK GENMASK(15, 0)
96 #define USB_R6_P30_CR_ACK BIT(16)
97
98enum {
99 USB2_HOST_PHY0 = 0,
100 USB2_OTG_PHY1,
101 USB2_HOST_PHY2,
102 PHY_COUNT,
103};
104
105static const char *phy_names[PHY_COUNT] = {
106 "usb2-phy0", "usb2-phy1", "usb2-phy2",
107};
108
109struct dwc3_meson_gxl {
110 struct udevice *dev;
111 struct regmap *regmap;
112 struct clk clk;
113 struct reset_ctl reset;
114 struct phy phys[PHY_COUNT];
115 enum usb_dr_mode otg_mode;
116 enum usb_dr_mode otg_phy_mode;
117 unsigned int usb2_ports;
118#if CONFIG_IS_ENABLED(DM_REGULATOR)
119 struct udevice *vbus_supply;
120#endif
121};
122
123#define U2P_REG_SIZE 0x20
124#define USB_REG_OFFSET 0x80
125
126#define USB2_OTG_PHY USB2_OTG_PHY1
127
128static void dwc3_meson_gxl_usb2_set_mode(struct dwc3_meson_gxl *priv, enum usb_dr_mode mode)
129{
130 switch (mode) {
131 case USB_DR_MODE_HOST:
132 case USB_DR_MODE_OTG:
133 case USB_DR_MODE_UNKNOWN:
134 regmap_update_bits(priv->regmap, USB_R1,
135 USB_R1_U3H_HOST_U2_PORT_DISABLE_MASK, 0);
136 regmap_update_bits(priv->regmap, USB_R0,
137 USB_R0_U2D_ACT, 0);
138 regmap_update_bits(priv->regmap, USB_R4,
139 USB_R4_P21_SLEEP_M0, 0);
140 break;
141
142 case USB_DR_MODE_PERIPHERAL:
143 regmap_update_bits(priv->regmap, USB_R0,
144 USB_R0_U2D_ACT, USB_R0_U2D_ACT);
145 regmap_update_bits(priv->regmap, USB_R0,
146 USB_R0_U2D_SS_SCALEDOWN_MODE_MASK, 0);
147 regmap_update_bits(priv->regmap, USB_R4,
148 USB_R4_P21_SLEEP_M0, USB_R4_P21_SLEEP_M0);
149 break;
150 }
151}
152
153static int dwc3_meson_gxl_usb2_init(struct dwc3_meson_gxl *priv)
154{
155 int i;
156
157 for (i = 0; i < PHY_COUNT; ++i) {
158 if (!priv->phys[i].dev)
159 continue;
160
161 phy_meson_gxl_usb2_set_mode(&priv->phys[i],
162 (i == USB2_OTG_PHY) ? USB_DR_MODE_PERIPHERAL
163 : USB_DR_MODE_HOST);
164 }
165
166 return 0;
167}
168
169static int dwc3_meson_gxl_usb_init(struct dwc3_meson_gxl *priv)
170{
171 int ret;
Wolfgang Denk9d328a62021-09-27 17:42:38 +0200172
Neil Armstrongc1a7e72c2020-09-10 10:48:13 +0200173 ret = dwc3_meson_gxl_usb2_init(priv);
174 if (ret)
175 return ret;
176
177 regmap_update_bits(priv->regmap, USB_R1,
178 USB_R1_U3H_FLADJ_30MHZ_REG_MASK,
179 FIELD_PREP(USB_R1_U3H_FLADJ_30MHZ_REG_MASK, 0x20));
180
181 regmap_update_bits(priv->regmap, USB_R5,
182 USB_R5_ID_DIG_EN_0,
183 USB_R5_ID_DIG_EN_0);
184 regmap_update_bits(priv->regmap, USB_R5,
185 USB_R5_ID_DIG_EN_1,
186 USB_R5_ID_DIG_EN_1);
187 regmap_update_bits(priv->regmap, USB_R5,
188 USB_R5_ID_DIG_TH_MASK,
189 FIELD_PREP(USB_R5_ID_DIG_TH_MASK, 0xff));
190
191 dwc3_meson_gxl_usb2_set_mode(priv, priv->otg_phy_mode);
192
193 return 0;
194}
195
196int dwc3_meson_gxl_force_mode(struct udevice *dev, enum usb_dr_mode mode)
197{
Simon Glassfa20e932020-12-03 16:55:20 -0700198 struct dwc3_meson_gxl *priv = dev_get_plat(dev);
Neil Armstrongc1a7e72c2020-09-10 10:48:13 +0200199
200 if (!priv)
201 return -EINVAL;
202
203 if (mode != USB_DR_MODE_HOST && mode != USB_DR_MODE_PERIPHERAL)
204 return -EINVAL;
205
206 if (!priv->phys[USB2_OTG_PHY].dev)
207 return -EINVAL;
208
209 if (mode == priv->otg_phy_mode)
210 return 0;
211
212 if (mode == USB_DR_MODE_HOST)
213 debug("%s: switching to Host Mode\n", __func__);
214 else
215 debug("%s: switching to Device Mode\n", __func__);
216
217#if CONFIG_IS_ENABLED(DM_REGULATOR)
218 if (priv->vbus_supply) {
219 int ret = regulator_set_enable(priv->vbus_supply,
220 (mode == USB_DR_MODE_PERIPHERAL));
221 if (ret)
222 return ret;
223 }
224#endif
225 priv->otg_phy_mode = mode;
226
227 phy_meson_gxl_usb2_set_mode(&priv->phys[USB2_OTG_PHY], mode);
228
229 dwc3_meson_gxl_usb2_set_mode(priv, mode);
230
231 return 0;
232}
233
234static int dwc3_meson_gxl_get_phys(struct dwc3_meson_gxl *priv)
235{
236 int i, ret;
237
238 for (i = 0 ; i < PHY_COUNT ; ++i) {
239 ret = generic_phy_get_by_name(priv->dev, phy_names[i],
240 &priv->phys[i]);
241 if (ret == -ENOENT || ret == -ENODATA) {
242 priv->phys[i].dev = NULL;
243 continue;
244 }
245
246 if (ret)
247 return ret;
248
249 priv->usb2_ports++;
250 }
251
252 debug("%s: usb2 ports: %d\n", __func__, priv->usb2_ports);
253
254 return 0;
255}
256
257static int dwc3_meson_gxl_reset_init(struct dwc3_meson_gxl *priv)
258{
259 int ret;
260
261 ret = reset_get_by_index(priv->dev, 0, &priv->reset);
262 if (ret)
263 return ret;
264
265 ret = reset_assert(&priv->reset);
266 udelay(1);
267 ret |= reset_deassert(&priv->reset);
268 if (ret) {
269 reset_free(&priv->reset);
270 return ret;
271 }
272
273 return 0;
274}
275
276static int dwc3_meson_gxl_clk_init(struct dwc3_meson_gxl *priv)
277{
278 int ret;
279
280 ret = clk_get_by_index(priv->dev, 0, &priv->clk);
281 if (ret)
282 return ret;
283
284#if CONFIG_IS_ENABLED(CLK)
285 ret = clk_enable(&priv->clk);
Sean Andersond318eb32023-12-16 14:38:42 -0500286 if (ret)
Neil Armstrongc1a7e72c2020-09-10 10:48:13 +0200287 return ret;
Neil Armstrongc1a7e72c2020-09-10 10:48:13 +0200288#endif
289
290 return 0;
291}
292
293static int dwc3_meson_gxl_probe(struct udevice *dev)
294{
Simon Glassfa20e932020-12-03 16:55:20 -0700295 struct dwc3_meson_gxl *priv = dev_get_plat(dev);
Neil Armstrongc1a7e72c2020-09-10 10:48:13 +0200296 int ret, i;
297
298 priv->dev = dev;
299
300 ret = regmap_init_mem(dev_ofnode(dev), &priv->regmap);
301 if (ret)
302 return ret;
303
304 ret = dwc3_meson_gxl_clk_init(priv);
305 if (ret)
306 return ret;
307
308 ret = dwc3_meson_gxl_reset_init(priv);
309 if (ret)
310 return ret;
311
312 ret = dwc3_meson_gxl_get_phys(priv);
313 if (ret)
314 return ret;
315
316#if CONFIG_IS_ENABLED(DM_REGULATOR)
317 ret = device_get_supply_regulator(dev, "vbus-supply",
318 &priv->vbus_supply);
319 if (ret && ret != -ENOENT) {
320 pr_err("Failed to get PHY regulator\n");
321 return ret;
322 }
323
324 if (priv->vbus_supply) {
325 ret = regulator_set_enable(priv->vbus_supply, true);
326 if (ret)
327 return ret;
328 }
329#endif
330
331 /* On GXL PHY must be started in device mode for DWC2 init */
332 priv->otg_mode = USB_DR_MODE_PERIPHERAL;
333
334 ret = dwc3_meson_gxl_usb_init(priv);
335 if (ret)
336 return ret;
337
Simon Glassa7ece582020-12-19 10:40:14 -0700338 priv->otg_mode = usb_get_dr_mode(dev_ofnode(dev));
Neil Armstrongc1a7e72c2020-09-10 10:48:13 +0200339
340 if (priv->otg_mode == USB_DR_MODE_PERIPHERAL)
341 priv->otg_phy_mode = USB_DR_MODE_PERIPHERAL;
342 else
343 priv->otg_phy_mode = USB_DR_MODE_HOST;
344
345 for (i = 0 ; i < PHY_COUNT ; ++i) {
346 if (!priv->phys[i].dev)
347 continue;
348
349 ret = generic_phy_init(&priv->phys[i]);
350 if (ret)
351 goto err_phy_init;
352 }
353
354 for (i = 0; i < PHY_COUNT; ++i) {
355 if (!priv->phys[i].dev)
356 continue;
357
358 ret = generic_phy_power_on(&priv->phys[i]);
359 if (ret)
360 goto err_phy_init;
361 }
362
363 if (priv->phys[USB2_OTG_PHY].dev)
364 phy_meson_gxl_usb2_set_mode(&priv->phys[USB2_OTG_PHY],
365 priv->otg_phy_mode);
366
367 dwc3_meson_gxl_usb2_set_mode(priv, priv->otg_phy_mode);
368
369 return 0;
370
371err_phy_init:
372 for (i = 0 ; i < PHY_COUNT ; ++i) {
373 if (!priv->phys[i].dev)
374 continue;
375
376 generic_phy_exit(&priv->phys[i]);
377 }
378
379 return ret;
380}
381
382static int dwc3_meson_gxl_remove(struct udevice *dev)
383{
Simon Glassfa20e932020-12-03 16:55:20 -0700384 struct dwc3_meson_gxl *priv = dev_get_plat(dev);
Neil Armstrongc1a7e72c2020-09-10 10:48:13 +0200385 int i;
386
387 reset_release_all(&priv->reset, 1);
388
389 clk_release_all(&priv->clk, 1);
390
391 for (i = 0; i < PHY_COUNT; ++i) {
392 if (!priv->phys[i].dev)
393 continue;
394
395 generic_phy_power_off(&priv->phys[i]);
396 }
397
398 for (i = 0 ; i < PHY_COUNT ; ++i) {
399 if (!priv->phys[i].dev)
400 continue;
401
402 generic_phy_exit(&priv->phys[i]);
403 }
404
405 return dm_scan_fdt_dev(dev);
406}
407
Neil Armstrong4a0d7182023-01-17 10:11:45 +0100408static int dwc3_meson_gxl_child_pre_probe(struct udevice *dev)
409{
410 if (ofnode_device_is_compatible(dev_ofnode(dev), "amlogic,meson-g12a-usb"))
411 return dwc3_meson_gxl_force_mode(dev->parent, USB_DR_MODE_PERIPHERAL);
412
413 return 0;
414}
415
416static int dwc3_meson_gxl_child_post_remove(struct udevice *dev)
417{
418 if (ofnode_device_is_compatible(dev_ofnode(dev), "amlogic,meson-g12a-usb"))
419 return dwc3_meson_gxl_force_mode(dev->parent, USB_DR_MODE_HOST);
420
421 return 0;
422}
423
Neil Armstrongc1a7e72c2020-09-10 10:48:13 +0200424static const struct udevice_id dwc3_meson_gxl_ids[] = {
Neil Armstrong2e166c82021-09-17 09:37:01 +0200425 { .compatible = "amlogic,meson-axg-usb-ctrl" },
Neil Armstrongc1a7e72c2020-09-10 10:48:13 +0200426 { .compatible = "amlogic,meson-gxl-usb-ctrl" },
427 { .compatible = "amlogic,meson-gxm-usb-ctrl" },
428 { }
429};
430
431U_BOOT_DRIVER(dwc3_generic_wrapper) = {
432 .name = "dwc3-meson-gxl",
433 .id = UCLASS_SIMPLE_BUS,
434 .of_match = dwc3_meson_gxl_ids,
435 .probe = dwc3_meson_gxl_probe,
436 .remove = dwc3_meson_gxl_remove,
Neil Armstrong4a0d7182023-01-17 10:11:45 +0100437 .child_pre_probe = dwc3_meson_gxl_child_pre_probe,
438 .child_post_remove = dwc3_meson_gxl_child_post_remove,
Simon Glass71fa5b42020-12-03 16:55:18 -0700439 .plat_auto = sizeof(struct dwc3_meson_gxl),
Neil Armstrongc1a7e72c2020-09-10 10:48:13 +0200440
441};