blob: dc5a976f71aa62a530d2309356855f35a211fe06 [file] [log] [blame]
Neil Armstrongadb049a2019-02-19 13:42:01 +01001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Amlogic G12A DWC3 Glue layer
4 *
5 * Copyright (C) 2019 BayLibre, SAS
6 * Author: Neil Armstrong <narmstrong@baylibre.com>
7 */
8
9#include <common.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060010#include <log.h>
Neil Armstrongadb049a2019-02-19 13:42:01 +010011#include <asm-generic/io.h>
12#include <dm.h>
13#include <dm/device-internal.h>
14#include <dm/lists.h>
15#include <dwc3-uboot.h>
16#include <generic-phy.h>
Simon Glassc05ed002020-05-10 11:40:11 -060017#include <linux/delay.h>
Simon Glass1e94b462023-09-14 18:21:46 -060018#include <linux/printk.h>
Neil Armstrongadb049a2019-02-19 13:42:01 +010019#include <linux/usb/ch9.h>
20#include <linux/usb/gadget.h>
21#include <malloc.h>
22#include <regmap.h>
23#include <usb.h>
24#include "core.h"
25#include "gadget.h"
26#include <reset.h>
27#include <clk.h>
28#include <power/regulator.h>
29#include <linux/bitfield.h>
30#include <linux/bitops.h>
31#include <linux/compat.h>
32
33/* USB2 Ports Control Registers */
34
35#define U2P_REG_SIZE 0x20
36
37#define U2P_R0 0x0
38 #define U2P_R0_HOST_DEVICE BIT(0)
39 #define U2P_R0_POWER_OK BIT(1)
40 #define U2P_R0_HAST_MODE BIT(2)
41 #define U2P_R0_POWER_ON_RESET BIT(3)
42 #define U2P_R0_ID_PULLUP BIT(4)
43 #define U2P_R0_DRV_VBUS BIT(5)
44
45#define U2P_R1 0x4
46 #define U2P_R1_PHY_READY BIT(0)
47 #define U2P_R1_ID_DIG BIT(1)
48 #define U2P_R1_OTG_SESSION_VALID BIT(2)
49 #define U2P_R1_VBUS_VALID BIT(3)
50
51/* USB Glue Control Registers */
52
53#define USB_R0 0x80
54 #define USB_R0_P30_LANE0_TX2RX_LOOPBACK BIT(17)
55 #define USB_R0_P30_LANE0_EXT_PCLK_REQ BIT(18)
56 #define USB_R0_P30_PCS_RX_LOS_MASK_VAL_MASK GENMASK(28, 19)
57 #define USB_R0_U2D_SS_SCALEDOWN_MODE_MASK GENMASK(30, 29)
58 #define USB_R0_U2D_ACT BIT(31)
59
60#define USB_R1 0x84
61 #define USB_R1_U3H_BIGENDIAN_GS BIT(0)
62 #define USB_R1_U3H_PME_ENABLE BIT(1)
63 #define USB_R1_U3H_HUB_PORT_OVERCURRENT_MASK GENMASK(4, 2)
64 #define USB_R1_U3H_HUB_PORT_PERM_ATTACH_MASK GENMASK(9, 7)
65 #define USB_R1_U3H_HOST_U2_PORT_DISABLE_MASK GENMASK(13, 12)
66 #define USB_R1_U3H_HOST_U3_PORT_DISABLE BIT(16)
67 #define USB_R1_U3H_HOST_PORT_POWER_CONTROL_PRESENT BIT(17)
68 #define USB_R1_U3H_HOST_MSI_ENABLE BIT(18)
69 #define USB_R1_U3H_FLADJ_30MHZ_REG_MASK GENMASK(24, 19)
70 #define USB_R1_P30_PCS_TX_SWING_FULL_MASK GENMASK(31, 25)
71
72#define USB_R2 0x88
73 #define USB_R2_P30_PCS_TX_DEEMPH_3P5DB_MASK GENMASK(25, 20)
74 #define USB_R2_P30_PCS_TX_DEEMPH_6DB_MASK GENMASK(31, 26)
75
76#define USB_R3 0x8c
77 #define USB_R3_P30_SSC_ENABLE BIT(0)
78 #define USB_R3_P30_SSC_RANGE_MASK GENMASK(3, 1)
79 #define USB_R3_P30_SSC_REF_CLK_SEL_MASK GENMASK(12, 4)
80 #define USB_R3_P30_REF_SSP_EN BIT(13)
81
82#define USB_R4 0x90
83 #define USB_R4_P21_PORT_RESET_0 BIT(0)
84 #define USB_R4_P21_SLEEP_M0 BIT(1)
85 #define USB_R4_MEM_PD_MASK GENMASK(3, 2)
86 #define USB_R4_P21_ONLY BIT(4)
87
88#define USB_R5 0x94
89 #define USB_R5_ID_DIG_SYNC BIT(0)
90 #define USB_R5_ID_DIG_REG BIT(1)
91 #define USB_R5_ID_DIG_CFG_MASK GENMASK(3, 2)
92 #define USB_R5_ID_DIG_EN_0 BIT(4)
93 #define USB_R5_ID_DIG_EN_1 BIT(5)
94 #define USB_R5_ID_DIG_CURR BIT(6)
95 #define USB_R5_ID_DIG_IRQ BIT(7)
96 #define USB_R5_ID_DIG_TH_MASK GENMASK(15, 8)
97 #define USB_R5_ID_DIG_CNT_MASK GENMASK(23, 16)
98
99enum {
100 USB2_HOST_PHY = 0,
101 USB2_OTG_PHY,
102 USB3_HOST_PHY,
103 PHY_COUNT,
104};
105
106static const char *phy_names[PHY_COUNT] = {
107 "usb2-phy0", "usb2-phy1", "usb3-phy0",
108};
109
110struct dwc3_meson_g12a {
111 struct udevice *dev;
112 struct regmap *regmap;
113 struct clk clk;
114 struct reset_ctl reset;
115 struct phy phys[PHY_COUNT];
116 enum usb_dr_mode otg_mode;
117 enum usb_dr_mode otg_phy_mode;
118 unsigned int usb2_ports;
119 unsigned int usb3_ports;
120#if CONFIG_IS_ENABLED(DM_REGULATOR)
121 struct udevice *vbus_supply;
122#endif
123};
124
125#define U2P_REG_SIZE 0x20
126#define USB_REG_OFFSET 0x80
127
128static void dwc3_meson_g12a_usb2_set_mode(struct dwc3_meson_g12a *priv,
129 int i, enum usb_dr_mode mode)
130{
131 switch (mode) {
132 case USB_DR_MODE_HOST:
133 case USB_DR_MODE_OTG:
134 case USB_DR_MODE_UNKNOWN:
135 regmap_update_bits(priv->regmap, U2P_R0 + (U2P_REG_SIZE * i),
136 U2P_R0_HOST_DEVICE,
137 U2P_R0_HOST_DEVICE);
138 break;
139
140 case USB_DR_MODE_PERIPHERAL:
141 regmap_update_bits(priv->regmap, U2P_R0 + (U2P_REG_SIZE * i),
142 U2P_R0_HOST_DEVICE, 0);
143 break;
144 }
145}
146
147static int dwc3_meson_g12a_usb2_init(struct dwc3_meson_g12a *priv)
148{
149 int i;
150
151 if (priv->otg_mode == USB_DR_MODE_PERIPHERAL)
152 priv->otg_phy_mode = USB_DR_MODE_PERIPHERAL;
153 else
154 priv->otg_phy_mode = USB_DR_MODE_HOST;
155
156 for (i = 0 ; i < USB3_HOST_PHY ; ++i) {
157 if (!priv->phys[i].dev)
158 continue;
159
160 regmap_update_bits(priv->regmap, U2P_R0 + (U2P_REG_SIZE * i),
161 U2P_R0_POWER_ON_RESET,
162 U2P_R0_POWER_ON_RESET);
163
164 if (i == USB2_OTG_PHY) {
165 regmap_update_bits(priv->regmap,
166 U2P_R0 + (U2P_REG_SIZE * i),
167 U2P_R0_ID_PULLUP | U2P_R0_DRV_VBUS,
168 U2P_R0_ID_PULLUP | U2P_R0_DRV_VBUS);
169
170 dwc3_meson_g12a_usb2_set_mode(priv, i,
171 priv->otg_phy_mode);
172 } else
173 dwc3_meson_g12a_usb2_set_mode(priv, i,
174 USB_DR_MODE_HOST);
175
176 regmap_update_bits(priv->regmap, U2P_R0 + (U2P_REG_SIZE * i),
177 U2P_R0_POWER_ON_RESET, 0);
178 }
179
180 return 0;
181}
182
183static void dwc3_meson_g12a_usb3_init(struct dwc3_meson_g12a *priv)
184{
185 regmap_update_bits(priv->regmap, USB_R3,
186 USB_R3_P30_SSC_RANGE_MASK |
187 USB_R3_P30_REF_SSP_EN,
188 USB_R3_P30_SSC_ENABLE |
189 FIELD_PREP(USB_R3_P30_SSC_RANGE_MASK, 2) |
190 USB_R3_P30_REF_SSP_EN);
191 udelay(2);
192
193 regmap_update_bits(priv->regmap, USB_R2,
194 USB_R2_P30_PCS_TX_DEEMPH_3P5DB_MASK,
195 FIELD_PREP(USB_R2_P30_PCS_TX_DEEMPH_3P5DB_MASK, 0x15));
196
197 regmap_update_bits(priv->regmap, USB_R2,
198 USB_R2_P30_PCS_TX_DEEMPH_6DB_MASK,
199 FIELD_PREP(USB_R2_P30_PCS_TX_DEEMPH_6DB_MASK, 0x20));
200
201 udelay(2);
202
203 regmap_update_bits(priv->regmap, USB_R1,
204 USB_R1_U3H_HOST_PORT_POWER_CONTROL_PRESENT,
205 USB_R1_U3H_HOST_PORT_POWER_CONTROL_PRESENT);
206
207 regmap_update_bits(priv->regmap, USB_R1,
208 USB_R1_P30_PCS_TX_SWING_FULL_MASK,
209 FIELD_PREP(USB_R1_P30_PCS_TX_SWING_FULL_MASK, 127));
210}
211
212static void dwc3_meson_g12a_usb_init_mode(struct dwc3_meson_g12a *priv)
213{
214 if (priv->otg_phy_mode == USB_DR_MODE_PERIPHERAL) {
215 regmap_update_bits(priv->regmap, USB_R0,
216 USB_R0_U2D_ACT, USB_R0_U2D_ACT);
217 regmap_update_bits(priv->regmap, USB_R0,
218 USB_R0_U2D_SS_SCALEDOWN_MODE_MASK, 0);
219 regmap_update_bits(priv->regmap, USB_R4,
220 USB_R4_P21_SLEEP_M0, USB_R4_P21_SLEEP_M0);
221 } else {
222 regmap_update_bits(priv->regmap, USB_R0,
223 USB_R0_U2D_ACT, 0);
224 regmap_update_bits(priv->regmap, USB_R4,
225 USB_R4_P21_SLEEP_M0, 0);
226 }
227}
228
229static int dwc3_meson_g12a_usb_init(struct dwc3_meson_g12a *priv)
230{
231 int ret;
232
233 ret = dwc3_meson_g12a_usb2_init(priv);
234 if (ret)
235 return ret;
236
237 regmap_update_bits(priv->regmap, USB_R1,
238 USB_R1_U3H_FLADJ_30MHZ_REG_MASK,
239 FIELD_PREP(USB_R1_U3H_FLADJ_30MHZ_REG_MASK, 0x20));
240
241 regmap_update_bits(priv->regmap, USB_R5,
242 USB_R5_ID_DIG_EN_0,
243 USB_R5_ID_DIG_EN_0);
244 regmap_update_bits(priv->regmap, USB_R5,
245 USB_R5_ID_DIG_EN_1,
246 USB_R5_ID_DIG_EN_1);
247 regmap_update_bits(priv->regmap, USB_R5,
248 USB_R5_ID_DIG_TH_MASK,
249 FIELD_PREP(USB_R5_ID_DIG_TH_MASK, 0xff));
250
251 /* If we have an actual SuperSpeed port, initialize it */
252 if (priv->usb3_ports)
253 dwc3_meson_g12a_usb3_init(priv);
254
255 dwc3_meson_g12a_usb_init_mode(priv);
256
257 return 0;
258}
259
260int dwc3_meson_g12a_force_mode(struct udevice *dev, enum usb_dr_mode mode)
261{
Simon Glassc69cda22020-12-03 16:55:20 -0700262 struct dwc3_meson_g12a *priv = dev_get_plat(dev);
Neil Armstrongadb049a2019-02-19 13:42:01 +0100263
264 if (!priv)
265 return -EINVAL;
266
267 if (mode != USB_DR_MODE_HOST && mode != USB_DR_MODE_PERIPHERAL)
268 return -EINVAL;
269
270 if (!priv->phys[USB2_OTG_PHY].dev)
271 return -EINVAL;
272
Neil Armstrongadb049a2019-02-19 13:42:01 +0100273 if (mode == USB_DR_MODE_HOST)
274 debug("%s: switching to Host Mode\n", __func__);
275 else
276 debug("%s: switching to Device Mode\n", __func__);
277
278#if CONFIG_IS_ENABLED(DM_REGULATOR)
279 if (priv->vbus_supply) {
280 int ret = regulator_set_enable(priv->vbus_supply,
281 (mode == USB_DR_MODE_PERIPHERAL));
282 if (ret)
283 return ret;
284 }
285#endif
286 priv->otg_phy_mode = mode;
287
288 dwc3_meson_g12a_usb2_set_mode(priv, USB2_OTG_PHY, mode);
289
290 dwc3_meson_g12a_usb_init_mode(priv);
291
292 return 0;
293}
294
295static int dwc3_meson_g12a_get_phys(struct dwc3_meson_g12a *priv)
296{
297 int i, ret;
298
299 for (i = 0 ; i < PHY_COUNT ; ++i) {
300 ret = generic_phy_get_by_name(priv->dev, phy_names[i],
301 &priv->phys[i]);
Neil Armstrong60e531f2021-05-05 10:38:57 +0200302 if (ret == -ENOENT || ret == -ENODATA)
Neil Armstrongadb049a2019-02-19 13:42:01 +0100303 continue;
304
305 if (ret)
306 return ret;
307
308 if (i == USB3_HOST_PHY)
309 priv->usb3_ports++;
310 else
311 priv->usb2_ports++;
312 }
313
314 debug("%s: usb2 ports: %d\n", __func__, priv->usb2_ports);
315 debug("%s: usb3 ports: %d\n", __func__, priv->usb3_ports);
316
317 return 0;
318}
319
320static int dwc3_meson_g12a_reset_init(struct dwc3_meson_g12a *priv)
321{
322 int ret;
323
324 ret = reset_get_by_index(priv->dev, 0, &priv->reset);
325 if (ret)
326 return ret;
327
328 ret = reset_assert(&priv->reset);
329 udelay(1);
330 ret |= reset_deassert(&priv->reset);
331 if (ret) {
332 reset_free(&priv->reset);
333 return ret;
334 }
335
336 return 0;
337}
338
339static int dwc3_meson_g12a_clk_init(struct dwc3_meson_g12a *priv)
340{
341 int ret;
342
343 ret = clk_get_by_index(priv->dev, 0, &priv->clk);
344 if (ret)
345 return ret;
346
347#if CONFIG_IS_ENABLED(CLK)
348 ret = clk_enable(&priv->clk);
349 if (ret) {
350 clk_free(&priv->clk);
351 return ret;
352 }
353#endif
354
355 return 0;
356}
357
358static int dwc3_meson_g12a_probe(struct udevice *dev)
359{
Simon Glassc69cda22020-12-03 16:55:20 -0700360 struct dwc3_meson_g12a *priv = dev_get_plat(dev);
Neil Armstrongadb049a2019-02-19 13:42:01 +0100361 int ret, i;
362
363 priv->dev = dev;
364
365 ret = regmap_init_mem(dev_ofnode(dev), &priv->regmap);
366 if (ret)
367 return ret;
368
369 ret = dwc3_meson_g12a_clk_init(priv);
370 if (ret)
371 return ret;
372
373 ret = dwc3_meson_g12a_reset_init(priv);
374 if (ret)
375 return ret;
376
377 ret = dwc3_meson_g12a_get_phys(priv);
378 if (ret)
379 return ret;
380
381#if CONFIG_IS_ENABLED(DM_REGULATOR)
382 ret = device_get_supply_regulator(dev, "vbus-supply",
383 &priv->vbus_supply);
384 if (ret && ret != -ENOENT) {
385 pr_err("Failed to get PHY regulator\n");
386 return ret;
387 }
388
389 if (priv->vbus_supply) {
390 ret = regulator_set_enable(priv->vbus_supply, true);
391 if (ret)
392 return ret;
393 }
394#endif
395
Simon Glassf10643c2020-12-19 10:40:14 -0700396 priv->otg_mode = usb_get_dr_mode(dev_ofnode(dev));
Neil Armstrongadb049a2019-02-19 13:42:01 +0100397
398 ret = dwc3_meson_g12a_usb_init(priv);
399 if (ret)
400 return ret;
401
402 for (i = 0 ; i < PHY_COUNT ; ++i) {
403 if (!priv->phys[i].dev)
404 continue;
405
406 ret = generic_phy_init(&priv->phys[i]);
407 if (ret)
408 goto err_phy_init;
409 }
410
Neil Armstrong3dc4f832020-04-21 10:17:42 +0200411 for (i = 0; i < PHY_COUNT; ++i) {
412 if (!priv->phys[i].dev)
413 continue;
414
415 ret = generic_phy_power_on(&priv->phys[i]);
416 if (ret)
417 goto err_phy_init;
418 }
419
Neil Armstrongadb049a2019-02-19 13:42:01 +0100420 return 0;
421
422err_phy_init:
423 for (i = 0 ; i < PHY_COUNT ; ++i) {
424 if (!priv->phys[i].dev)
425 continue;
426
427 generic_phy_exit(&priv->phys[i]);
428 }
429
430 return ret;
431}
432
433static int dwc3_meson_g12a_remove(struct udevice *dev)
434{
Simon Glassc69cda22020-12-03 16:55:20 -0700435 struct dwc3_meson_g12a *priv = dev_get_plat(dev);
Neil Armstrongadb049a2019-02-19 13:42:01 +0100436 int i;
437
438 reset_release_all(&priv->reset, 1);
439
440 clk_release_all(&priv->clk, 1);
441
Neil Armstrong3dc4f832020-04-21 10:17:42 +0200442 for (i = 0; i < PHY_COUNT; ++i) {
443 if (!priv->phys[i].dev)
444 continue;
445
446 generic_phy_power_off(&priv->phys[i]);
447 }
448
Neil Armstrongadb049a2019-02-19 13:42:01 +0100449 for (i = 0 ; i < PHY_COUNT ; ++i) {
450 if (!priv->phys[i].dev)
451 continue;
452
453 generic_phy_exit(&priv->phys[i]);
454 }
455
456 return dm_scan_fdt_dev(dev);
457}
458
Mattijs Korpershoekfd083842022-11-23 16:42:49 +0100459static int dwc3_meson_g12a_child_pre_probe(struct udevice *dev)
460{
461 if (ofnode_device_is_compatible(dev_ofnode(dev), "amlogic,meson-g12a-usb"))
462 return dwc3_meson_g12a_force_mode(dev->parent, USB_DR_MODE_PERIPHERAL);
463
464 return 0;
465}
466
467static int dwc3_meson_g12a_child_post_remove(struct udevice *dev)
468{
469 if (ofnode_device_is_compatible(dev_ofnode(dev), "amlogic,meson-g12a-usb"))
470 return dwc3_meson_g12a_force_mode(dev->parent, USB_DR_MODE_HOST);
471
472 return 0;
473}
474
Neil Armstrongadb049a2019-02-19 13:42:01 +0100475static const struct udevice_id dwc3_meson_g12a_ids[] = {
476 { .compatible = "amlogic,meson-g12a-usb-ctrl" },
477 { }
478};
479
480U_BOOT_DRIVER(dwc3_generic_wrapper) = {
481 .name = "dwc3-meson-g12a",
482 .id = UCLASS_SIMPLE_BUS,
483 .of_match = dwc3_meson_g12a_ids,
484 .probe = dwc3_meson_g12a_probe,
485 .remove = dwc3_meson_g12a_remove,
Mattijs Korpershoekfd083842022-11-23 16:42:49 +0100486 .child_pre_probe = dwc3_meson_g12a_child_pre_probe,
487 .child_post_remove = dwc3_meson_g12a_child_post_remove,
Simon Glasscaa4daa2020-12-03 16:55:18 -0700488 .plat_auto = sizeof(struct dwc3_meson_g12a),
Neil Armstrongadb049a2019-02-19 13:42:01 +0100489
490};