1 // SPDX-License-Identifier: GPL-2.0
3 * Amlogic GXL DWC3 Glue layer
5 * Copyright (C) 2019 BayLibre, SAS
6 * Author: Neil Armstrong <narmstrong@baylibre.com>
11 #include <asm-generic/io.h>
13 #include <dm/device-internal.h>
15 #include <dwc3-uboot.h>
16 #include <generic-phy.h>
17 #include <linux/usb/ch9.h>
18 #include <linux/usb/gadget.h>
26 #include <power/regulator.h>
27 #include <linux/bitfield.h>
28 #include <linux/bitops.h>
29 #include <linux/compat.h>
30 #include <asm/arch/usb-gx.h>
32 /* USB Glue Control Registers */
35 #define USB_R0_P30_FSEL_MASK GENMASK(5, 0)
36 #define USB_R0_P30_PHY_RESET BIT(6)
37 #define USB_R0_P30_TEST_POWERDOWN_HSP BIT(7)
38 #define USB_R0_P30_TEST_POWERDOWN_SSP BIT(8)
39 #define USB_R0_P30_ACJT_LEVEL_MASK GENMASK(13, 9)
40 #define USB_R0_P30_TX_BOOST_LEVEL_MASK GENMASK(16, 14)
41 #define USB_R0_P30_LANE0_TX2RX_LOOPBACK BIT(17)
42 #define USB_R0_P30_LANE0_EXT_PCLK_REQ BIT(18)
43 #define USB_R0_P30_PCS_RX_LOS_MASK_VAL_MASK GENMASK(28, 19)
44 #define USB_R0_U2D_SS_SCALEDOWN_MODE_MASK GENMASK(30, 29)
45 #define USB_R0_U2D_ACT BIT(31)
48 #define USB_R1_U3H_BIGENDIAN_GS BIT(0)
49 #define USB_R1_U3H_PME_ENABLE BIT(1)
50 #define USB_R1_U3H_HUB_PORT_OVERCURRENT_MASK GENMASK(6, 2)
51 #define USB_R1_U3H_HUB_PORT_PERM_ATTACH_MASK GENMASK(11, 7)
52 #define USB_R1_U3H_HOST_U2_PORT_DISABLE_MASK GENMASK(15, 12)
53 #define USB_R1_U3H_HOST_U3_PORT_DISABLE BIT(16)
54 #define USB_R1_U3H_HOST_PORT_POWER_CONTROL_PRESENT BIT(17)
55 #define USB_R1_U3H_HOST_MSI_ENABLE BIT(18)
56 #define USB_R1_U3H_FLADJ_30MHZ_REG_MASK GENMASK(24, 19)
57 #define USB_R1_P30_PCS_TX_SWING_FULL_MASK GENMASK(31, 25)
60 #define USB_R2_P30_CR_DATA_IN_MASK GENMASK(15, 0)
61 #define USB_R2_P30_CR_READ BIT(16)
62 #define USB_R2_P30_CR_WRITE BIT(17)
63 #define USB_R2_P30_CR_CAP_ADDR BIT(18)
64 #define USB_R2_P30_CR_CAP_DATA BIT(19)
65 #define USB_R2_P30_PCS_TX_DEEMPH_3P5DB_MASK GENMASK(25, 20)
66 #define USB_R2_P30_PCS_TX_DEEMPH_6DB_MASK GENMASK(31, 26)
69 #define USB_R3_P30_SSC_ENABLE BIT(0)
70 #define USB_R3_P30_SSC_RANGE_MASK GENMASK(3, 1)
71 #define USB_R3_P30_SSC_REF_CLK_SEL_MASK GENMASK(12, 4)
72 #define USB_R3_P30_REF_SSP_EN BIT(13)
73 #define USB_R3_P30_LOS_BIAS_MASK GENMASK(18, 16)
74 #define USB_R3_P30_LOS_LEVEL_MASK GENMASK(23, 19)
75 #define USB_R3_P30_MPLL_MULTIPLIER_MASK GENMASK(30, 24)
78 #define USB_R4_P21_PORT_RESET_0 BIT(0)
79 #define USB_R4_P21_SLEEP_M0 BIT(1)
80 #define USB_R4_MEM_PD_MASK GENMASK(3, 2)
81 #define USB_R4_P21_ONLY BIT(4)
84 #define USB_R5_ID_DIG_SYNC BIT(0)
85 #define USB_R5_ID_DIG_REG BIT(1)
86 #define USB_R5_ID_DIG_CFG_MASK GENMASK(3, 2)
87 #define USB_R5_ID_DIG_EN_0 BIT(4)
88 #define USB_R5_ID_DIG_EN_1 BIT(5)
89 #define USB_R5_ID_DIG_CURR BIT(6)
90 #define USB_R5_ID_DIG_IRQ BIT(7)
91 #define USB_R5_ID_DIG_TH_MASK GENMASK(15, 8)
92 #define USB_R5_ID_DIG_CNT_MASK GENMASK(23, 16)
94 /* read-only register */
96 #define USB_R6_P30_CR_DATA_OUT_MASK GENMASK(15, 0)
97 #define USB_R6_P30_CR_ACK BIT(16)
106 static const char *phy_names[PHY_COUNT] = {
107 "usb2-phy0", "usb2-phy1", "usb2-phy2",
110 struct dwc3_meson_gxl {
112 struct regmap *regmap;
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 #if CONFIG_IS_ENABLED(DM_REGULATOR)
120 struct udevice *vbus_supply;
124 #define U2P_REG_SIZE 0x20
125 #define USB_REG_OFFSET 0x80
127 #define USB2_OTG_PHY USB2_OTG_PHY1
129 static void dwc3_meson_gxl_usb2_set_mode(struct dwc3_meson_gxl *priv, enum usb_dr_mode 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, USB_R1,
136 USB_R1_U3H_HOST_U2_PORT_DISABLE_MASK, 0);
137 regmap_update_bits(priv->regmap, USB_R0,
139 regmap_update_bits(priv->regmap, USB_R4,
140 USB_R4_P21_SLEEP_M0, 0);
143 case USB_DR_MODE_PERIPHERAL:
144 regmap_update_bits(priv->regmap, USB_R0,
145 USB_R0_U2D_ACT, USB_R0_U2D_ACT);
146 regmap_update_bits(priv->regmap, USB_R0,
147 USB_R0_U2D_SS_SCALEDOWN_MODE_MASK, 0);
148 regmap_update_bits(priv->regmap, USB_R4,
149 USB_R4_P21_SLEEP_M0, USB_R4_P21_SLEEP_M0);
154 static int dwc3_meson_gxl_usb2_init(struct dwc3_meson_gxl *priv)
158 for (i = 0; i < PHY_COUNT; ++i) {
159 if (!priv->phys[i].dev)
162 phy_meson_gxl_usb2_set_mode(&priv->phys[i],
163 (i == USB2_OTG_PHY) ? USB_DR_MODE_PERIPHERAL
170 static int dwc3_meson_gxl_usb_init(struct dwc3_meson_gxl *priv)
174 ret = dwc3_meson_gxl_usb2_init(priv);
178 regmap_update_bits(priv->regmap, USB_R1,
179 USB_R1_U3H_FLADJ_30MHZ_REG_MASK,
180 FIELD_PREP(USB_R1_U3H_FLADJ_30MHZ_REG_MASK, 0x20));
182 regmap_update_bits(priv->regmap, USB_R5,
185 regmap_update_bits(priv->regmap, USB_R5,
188 regmap_update_bits(priv->regmap, USB_R5,
189 USB_R5_ID_DIG_TH_MASK,
190 FIELD_PREP(USB_R5_ID_DIG_TH_MASK, 0xff));
192 dwc3_meson_gxl_usb2_set_mode(priv, priv->otg_phy_mode);
197 int dwc3_meson_gxl_force_mode(struct udevice *dev, enum usb_dr_mode mode)
199 struct dwc3_meson_gxl *priv = dev_get_plat(dev);
204 if (mode != USB_DR_MODE_HOST && mode != USB_DR_MODE_PERIPHERAL)
207 if (!priv->phys[USB2_OTG_PHY].dev)
210 if (mode == priv->otg_phy_mode)
213 if (mode == USB_DR_MODE_HOST)
214 debug("%s: switching to Host Mode\n", __func__);
216 debug("%s: switching to Device Mode\n", __func__);
218 #if CONFIG_IS_ENABLED(DM_REGULATOR)
219 if (priv->vbus_supply) {
220 int ret = regulator_set_enable(priv->vbus_supply,
221 (mode == USB_DR_MODE_PERIPHERAL));
226 priv->otg_phy_mode = mode;
228 phy_meson_gxl_usb2_set_mode(&priv->phys[USB2_OTG_PHY], mode);
230 dwc3_meson_gxl_usb2_set_mode(priv, mode);
235 static int dwc3_meson_gxl_get_phys(struct dwc3_meson_gxl *priv)
239 for (i = 0 ; i < PHY_COUNT ; ++i) {
240 ret = generic_phy_get_by_name(priv->dev, phy_names[i],
242 if (ret == -ENOENT || ret == -ENODATA) {
243 priv->phys[i].dev = NULL;
253 debug("%s: usb2 ports: %d\n", __func__, priv->usb2_ports);
258 static int dwc3_meson_gxl_reset_init(struct dwc3_meson_gxl *priv)
262 ret = reset_get_by_index(priv->dev, 0, &priv->reset);
266 ret = reset_assert(&priv->reset);
268 ret |= reset_deassert(&priv->reset);
270 reset_free(&priv->reset);
277 static int dwc3_meson_gxl_clk_init(struct dwc3_meson_gxl *priv)
281 ret = clk_get_by_index(priv->dev, 0, &priv->clk);
285 #if CONFIG_IS_ENABLED(CLK)
286 ret = clk_enable(&priv->clk);
288 clk_free(&priv->clk);
296 static int dwc3_meson_gxl_probe(struct udevice *dev)
298 struct dwc3_meson_gxl *priv = dev_get_plat(dev);
303 ret = regmap_init_mem(dev_ofnode(dev), &priv->regmap);
307 ret = dwc3_meson_gxl_clk_init(priv);
311 ret = dwc3_meson_gxl_reset_init(priv);
315 ret = dwc3_meson_gxl_get_phys(priv);
319 #if CONFIG_IS_ENABLED(DM_REGULATOR)
320 ret = device_get_supply_regulator(dev, "vbus-supply",
322 if (ret && ret != -ENOENT) {
323 pr_err("Failed to get PHY regulator\n");
327 if (priv->vbus_supply) {
328 ret = regulator_set_enable(priv->vbus_supply, true);
334 /* On GXL PHY must be started in device mode for DWC2 init */
335 priv->otg_mode = USB_DR_MODE_PERIPHERAL;
337 ret = dwc3_meson_gxl_usb_init(priv);
341 priv->otg_mode = usb_get_dr_mode(dev->node);
343 if (priv->otg_mode == USB_DR_MODE_PERIPHERAL)
344 priv->otg_phy_mode = USB_DR_MODE_PERIPHERAL;
346 priv->otg_phy_mode = USB_DR_MODE_HOST;
348 for (i = 0 ; i < PHY_COUNT ; ++i) {
349 if (!priv->phys[i].dev)
352 ret = generic_phy_init(&priv->phys[i]);
357 for (i = 0; i < PHY_COUNT; ++i) {
358 if (!priv->phys[i].dev)
361 ret = generic_phy_power_on(&priv->phys[i]);
366 if (priv->phys[USB2_OTG_PHY].dev)
367 phy_meson_gxl_usb2_set_mode(&priv->phys[USB2_OTG_PHY],
370 dwc3_meson_gxl_usb2_set_mode(priv, priv->otg_phy_mode);
375 for (i = 0 ; i < PHY_COUNT ; ++i) {
376 if (!priv->phys[i].dev)
379 generic_phy_exit(&priv->phys[i]);
385 static int dwc3_meson_gxl_remove(struct udevice *dev)
387 struct dwc3_meson_gxl *priv = dev_get_plat(dev);
390 reset_release_all(&priv->reset, 1);
392 clk_release_all(&priv->clk, 1);
394 for (i = 0; i < PHY_COUNT; ++i) {
395 if (!priv->phys[i].dev)
398 generic_phy_power_off(&priv->phys[i]);
401 for (i = 0 ; i < PHY_COUNT ; ++i) {
402 if (!priv->phys[i].dev)
405 generic_phy_exit(&priv->phys[i]);
408 return dm_scan_fdt_dev(dev);
411 static const struct udevice_id dwc3_meson_gxl_ids[] = {
412 { .compatible = "amlogic,meson-gxl-usb-ctrl" },
413 { .compatible = "amlogic,meson-gxm-usb-ctrl" },
417 U_BOOT_DRIVER(dwc3_generic_wrapper) = {
418 .name = "dwc3-meson-gxl",
419 .id = UCLASS_SIMPLE_BUS,
420 .of_match = dwc3_meson_gxl_ids,
421 .probe = dwc3_meson_gxl_probe,
422 .remove = dwc3_meson_gxl_remove,
423 .plat_auto = sizeof(struct dwc3_meson_gxl),