1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2019 Stefan Roese <sr@denx.de>
5 * Derived from linux/drivers/phy/ralink/phy-ralink-usb.c
6 * Copyright (C) 2017 John Crispin <john@phrozen.org>
11 #include <generic-phy.h>
13 #include <reset-uclass.h>
17 #define RT_SYSC_REG_SYSCFG1 0x014
18 #define RT_SYSC_REG_CLKCFG1 0x030
19 #define RT_SYSC_REG_USB_PHY_CFG 0x05c
21 #define OFS_U2_PHY_AC0 0x800
22 #define OFS_U2_PHY_AC1 0x804
23 #define OFS_U2_PHY_AC2 0x808
24 #define OFS_U2_PHY_ACR0 0x810
25 #define OFS_U2_PHY_ACR1 0x814
26 #define OFS_U2_PHY_ACR2 0x818
27 #define OFS_U2_PHY_ACR3 0x81C
28 #define OFS_U2_PHY_ACR4 0x820
29 #define OFS_U2_PHY_AMON0 0x824
30 #define OFS_U2_PHY_DCR0 0x860
31 #define OFS_U2_PHY_DCR1 0x864
32 #define OFS_U2_PHY_DTM0 0x868
33 #define OFS_U2_PHY_DTM1 0x86C
35 #define RT_RSTCTRL_UDEV BIT(25)
36 #define RT_RSTCTRL_UHST BIT(22)
37 #define RT_SYSCFG1_USB0_HOST_MODE BIT(10)
39 #define MT7620_CLKCFG1_UPHY0_CLK_EN BIT(25)
40 #define MT7620_CLKCFG1_UPHY1_CLK_EN BIT(22)
41 #define RT_CLKCFG1_UPHY1_CLK_EN BIT(20)
42 #define RT_CLKCFG1_UPHY0_CLK_EN BIT(18)
44 #define USB_PHY_UTMI_8B60M BIT(1)
45 #define UDEV_WAKEUP BIT(0)
47 struct mt76x8_usb_phy {
50 struct regmap *sysctl;
53 static void u2_phy_w32(struct mt76x8_usb_phy *phy, u32 val, u32 reg)
55 writel(val, phy->base + reg);
58 static u32 u2_phy_r32(struct mt76x8_usb_phy *phy, u32 reg)
60 return readl(phy->base + reg);
63 static void mt76x8_usb_phy_init(struct mt76x8_usb_phy *phy)
65 u2_phy_r32(phy, OFS_U2_PHY_AC2);
66 u2_phy_r32(phy, OFS_U2_PHY_ACR0);
67 u2_phy_r32(phy, OFS_U2_PHY_DCR0);
69 u2_phy_w32(phy, 0x00ffff02, OFS_U2_PHY_DCR0);
70 u2_phy_r32(phy, OFS_U2_PHY_DCR0);
71 u2_phy_w32(phy, 0x00555502, OFS_U2_PHY_DCR0);
72 u2_phy_r32(phy, OFS_U2_PHY_DCR0);
73 u2_phy_w32(phy, 0x00aaaa02, OFS_U2_PHY_DCR0);
74 u2_phy_r32(phy, OFS_U2_PHY_DCR0);
75 u2_phy_w32(phy, 0x00000402, OFS_U2_PHY_DCR0);
76 u2_phy_r32(phy, OFS_U2_PHY_DCR0);
77 u2_phy_w32(phy, 0x0048086a, OFS_U2_PHY_AC0);
78 u2_phy_w32(phy, 0x4400001c, OFS_U2_PHY_AC1);
79 u2_phy_w32(phy, 0xc0200000, OFS_U2_PHY_ACR3);
80 u2_phy_w32(phy, 0x02000000, OFS_U2_PHY_DTM0);
83 static int mt76x8_usb_phy_power_on(struct phy *_phy)
85 struct mt76x8_usb_phy *phy = dev_get_priv(_phy->dev);
89 regmap_update_bits(phy->sysctl, RT_SYSC_REG_CLKCFG1,
93 regmap_update_bits(phy->sysctl, RT_SYSC_REG_SYSCFG1,
94 RT_SYSCFG1_USB0_HOST_MODE,
95 RT_SYSCFG1_USB0_HOST_MODE);
98 * The SDK kernel had a delay of 100ms. however on device
99 * testing showed that 10ms is enough
104 mt76x8_usb_phy_init(phy);
106 /* print some status info */
107 regmap_read(phy->sysctl, RT_SYSC_REG_USB_PHY_CFG, &t);
108 printf("remote usb device wakeup %s\n",
109 (t & UDEV_WAKEUP) ? "enabled" : "disabled");
110 if (t & USB_PHY_UTMI_8B60M)
111 printf("UTMI 8bit 60MHz\n");
113 printf("UTMI 16bit 30MHz\n");
118 static int mt76x8_usb_phy_power_off(struct phy *_phy)
120 struct mt76x8_usb_phy *phy = dev_get_priv(_phy->dev);
122 /* disable the phy */
123 regmap_update_bits(phy->sysctl, RT_SYSC_REG_CLKCFG1,
129 static int mt76x8_usb_phy_probe(struct udevice *dev)
131 struct mt76x8_usb_phy *phy = dev_get_priv(dev);
133 phy->sysctl = syscon_regmap_lookup_by_phandle(dev, "ralink,sysctl");
134 if (IS_ERR(phy->sysctl))
135 return PTR_ERR(phy->sysctl);
137 phy->base = dev_read_addr_ptr(dev);
144 static struct phy_ops mt76x8_usb_phy_ops = {
145 .power_on = mt76x8_usb_phy_power_on,
146 .power_off = mt76x8_usb_phy_power_off,
149 static const struct udevice_id mt76x8_usb_phy_ids[] = {
150 { .compatible = "mediatek,mt7628-usbphy" },
154 U_BOOT_DRIVER(mt76x8_usb_phy) = {
155 .name = "mt76x8_usb_phy",
157 .of_match = mt76x8_usb_phy_ids,
158 .ops = &mt76x8_usb_phy_ops,
159 .probe = mt76x8_usb_phy_probe,
160 .priv_auto_alloc_size = sizeof(struct mt76x8_usb_phy),