blob: 140e936b47ae8da48579990eb2b216f4d77d1a25 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Neil Armstrong1cbd9ae2018-04-11 17:08:02 +02002/*
3 * Meson GXL and GXM USB2 PHY driver
4 *
5 * Copyright (C) 2017 Martin Blumenstingl <martin.blumenstingl@googlemail.com>
6 * Copyright (C) 2018 BayLibre, SAS
7 * Author: Neil Armstrong <narmstron@baylibre.com>
Neil Armstrong1cbd9ae2018-04-11 17:08:02 +02008 */
9
Simon Glass9bc15642020-02-03 07:36:16 -070010#include <malloc.h>
Neil Armstrong1cbd9ae2018-04-11 17:08:02 +020011#include <asm/io.h>
12#include <bitfield.h>
Simon Glassbdd5f812023-09-14 18:21:46 -060013#include <clk.h>
Neil Armstrong1cbd9ae2018-04-11 17:08:02 +020014#include <dm.h>
15#include <errno.h>
16#include <generic-phy.h>
17#include <regmap.h>
Simon Glassdbd79542020-05-10 11:40:11 -060018#include <linux/delay.h>
Simon Glassbdd5f812023-09-14 18:21:46 -060019#include <linux/printk.h>
Neil Armstrong13b06b42020-03-30 11:27:24 +020020#include <linux/usb/otg.h>
21
Neil Armstrong1cbd9ae2018-04-11 17:08:02 +020022#include <linux/bitops.h>
23#include <linux/compat.h>
24
Neil Armstrong1cbd9ae2018-04-11 17:08:02 +020025/* bits [31:27] are read-only */
26#define U2P_R0 0x0
27 #define U2P_R0_BYPASS_SEL BIT(0)
28 #define U2P_R0_BYPASS_DM_EN BIT(1)
29 #define U2P_R0_BYPASS_DP_EN BIT(2)
30 #define U2P_R0_TXBITSTUFF_ENH BIT(3)
31 #define U2P_R0_TXBITSTUFF_EN BIT(4)
32 #define U2P_R0_DM_PULLDOWN BIT(5)
33 #define U2P_R0_DP_PULLDOWN BIT(6)
34 #define U2P_R0_DP_VBUS_VLD_EXT_SEL BIT(7)
35 #define U2P_R0_DP_VBUS_VLD_EXT BIT(8)
36 #define U2P_R0_ADP_PRB_EN BIT(9)
37 #define U2P_R0_ADP_DISCHARGE BIT(10)
38 #define U2P_R0_ADP_CHARGE BIT(11)
39 #define U2P_R0_DRV_VBUS BIT(12)
40 #define U2P_R0_ID_PULLUP BIT(13)
41 #define U2P_R0_LOOPBACK_EN_B BIT(14)
42 #define U2P_R0_OTG_DISABLE BIT(15)
43 #define U2P_R0_COMMON_ONN BIT(16)
44 #define U2P_R0_FSEL_MASK GENMASK(19, 17)
45 #define U2P_R0_REF_CLK_SEL_MASK GENMASK(21, 20)
46 #define U2P_R0_POWER_ON_RESET BIT(22)
47 #define U2P_R0_V_ATE_TEST_EN_B_MASK GENMASK(24, 23)
48 #define U2P_R0_ID_SET_ID_DQ BIT(25)
49 #define U2P_R0_ATE_RESET BIT(26)
50 #define U2P_R0_FSV_MINUS BIT(27)
51 #define U2P_R0_FSV_PLUS BIT(28)
52 #define U2P_R0_BYPASS_DM_DATA BIT(29)
53 #define U2P_R0_BYPASS_DP_DATA BIT(30)
54
55#define U2P_R1 0x4
56 #define U2P_R1_BURN_IN_TEST BIT(0)
57 #define U2P_R1_ACA_ENABLE BIT(1)
58 #define U2P_R1_DCD_ENABLE BIT(2)
59 #define U2P_R1_VDAT_SRC_EN_B BIT(3)
60 #define U2P_R1_VDAT_DET_EN_B BIT(4)
61 #define U2P_R1_CHARGES_SEL BIT(5)
62 #define U2P_R1_TX_PREEMP_PULSE_TUNE BIT(6)
63 #define U2P_R1_TX_PREEMP_AMP_TUNE_MASK GENMASK(8, 7)
64 #define U2P_R1_TX_RES_TUNE_MASK GENMASK(10, 9)
65 #define U2P_R1_TX_RISE_TUNE_MASK GENMASK(12, 11)
66 #define U2P_R1_TX_VREF_TUNE_MASK GENMASK(16, 13)
67 #define U2P_R1_TX_FSLS_TUNE_MASK GENMASK(20, 17)
68 #define U2P_R1_TX_HSXV_TUNE_MASK GENMASK(22, 21)
69 #define U2P_R1_OTG_TUNE_MASK GENMASK(25, 23)
70 #define U2P_R1_SQRX_TUNE_MASK GENMASK(28, 26)
71 #define U2P_R1_COMP_DIS_TUNE_MASK GENMASK(31, 29)
72
73/* bits [31:14] are read-only */
74#define U2P_R2 0x8
75 #define U2P_R2_TESTDATA_IN_MASK GENMASK(7, 0)
76 #define U2P_R2_TESTADDR_MASK GENMASK(11, 8)
77 #define U2P_R2_TESTDATA_OUT_SEL BIT(12)
78 #define U2P_R2_TESTCLK BIT(13)
79 #define U2P_R2_TESTDATA_OUT_MASK GENMASK(17, 14)
80 #define U2P_R2_ACA_PIN_RANGE_C BIT(18)
81 #define U2P_R2_ACA_PIN_RANGE_B BIT(19)
82 #define U2P_R2_ACA_PIN_RANGE_A BIT(20)
83 #define U2P_R2_ACA_PIN_GND BIT(21)
84 #define U2P_R2_ACA_PIN_FLOAT BIT(22)
85 #define U2P_R2_CHARGE_DETECT BIT(23)
86 #define U2P_R2_DEVICE_SESSION_VALID BIT(24)
87 #define U2P_R2_ADP_PROBE BIT(25)
88 #define U2P_R2_ADP_SENSE BIT(26)
89 #define U2P_R2_SESSION_END BIT(27)
90 #define U2P_R2_VBUS_VALID BIT(28)
91 #define U2P_R2_B_VALID BIT(29)
92 #define U2P_R2_A_VALID BIT(30)
93 #define U2P_R2_ID_DIG BIT(31)
94
95#define U2P_R3 0xc
96
97#define RESET_COMPLETE_TIME 500
98
99struct phy_meson_gxl_usb2_priv {
100 struct regmap *regmap;
Neil Armstrong1cbd9ae2018-04-11 17:08:02 +0200101#if CONFIG_IS_ENABLED(CLK)
102 struct clk clk;
103#endif
104};
105
106static void phy_meson_gxl_usb2_reset(struct phy_meson_gxl_usb2_priv *priv)
107{
108 uint val;
109
110 regmap_read(priv->regmap, U2P_R0, &val);
111
112 /* reset the PHY and wait until settings are stabilized */
113 val |= U2P_R0_POWER_ON_RESET;
114 regmap_write(priv->regmap, U2P_R0, val);
115 udelay(RESET_COMPLETE_TIME);
116
117 val &= ~U2P_R0_POWER_ON_RESET;
118 regmap_write(priv->regmap, U2P_R0, val);
119 udelay(RESET_COMPLETE_TIME);
120}
121
Neil Armstrongfb21e2e2024-06-20 09:42:52 +0200122static int phy_meson_gxl_usb2_set_mode(struct phy *phy, enum phy_mode mode, int submode)
Neil Armstrong1cbd9ae2018-04-11 17:08:02 +0200123{
Neil Armstrong13b06b42020-03-30 11:27:24 +0200124 struct udevice *dev = phy->dev;
125 struct phy_meson_gxl_usb2_priv *priv = dev_get_priv(dev);
Neil Armstrong1cbd9ae2018-04-11 17:08:02 +0200126 uint val;
127
Neil Armstrongfb21e2e2024-06-20 09:42:52 +0200128 if (submode)
129 return -EOPNOTSUPP;
130
Neil Armstrong1cbd9ae2018-04-11 17:08:02 +0200131 regmap_read(priv->regmap, U2P_R0, &val);
Neil Armstrong13b06b42020-03-30 11:27:24 +0200132
133 switch (mode) {
Neil Armstrongfb21e2e2024-06-20 09:42:52 +0200134 case PHY_MODE_USB_DEVICE:
Neil Armstrong13b06b42020-03-30 11:27:24 +0200135 val &= ~U2P_R0_DM_PULLDOWN;
136 val &= ~U2P_R0_DP_PULLDOWN;
137 val |= U2P_R0_ID_PULLUP;
138 break;
Neil Armstrong805989f2024-06-20 09:42:50 +0200139
140 case PHY_MODE_USB_HOST:
141 case PHY_MODE_USB_OTG:
Neil Armstrongfb21e2e2024-06-20 09:42:52 +0200142 val |= U2P_R0_DM_PULLDOWN;
143 val |= U2P_R0_DP_PULLDOWN;
144 val &= ~U2P_R0_ID_PULLUP;
Neil Armstrong805989f2024-06-20 09:42:50 +0200145 break;
146
147 default:
148 return -EINVAL;
149 }
150
Neil Armstrongfb21e2e2024-06-20 09:42:52 +0200151 regmap_write(priv->regmap, U2P_R0, val);
152
153 phy_meson_gxl_usb2_reset(priv);
154
Neil Armstrong805989f2024-06-20 09:42:50 +0200155 return 0;
156}
157
Neil Armstrong1cbd9ae2018-04-11 17:08:02 +0200158static int phy_meson_gxl_usb2_power_on(struct phy *phy)
159{
160 struct udevice *dev = phy->dev;
161 struct phy_meson_gxl_usb2_priv *priv = dev_get_priv(dev);
162 uint val;
163
164 regmap_read(priv->regmap, U2P_R0, &val);
165 /* power on the PHY by taking it out of reset mode */
166 val &= ~U2P_R0_POWER_ON_RESET;
167 regmap_write(priv->regmap, U2P_R0, val);
168
Neil Armstrongfb21e2e2024-06-20 09:42:52 +0200169 phy_meson_gxl_usb2_set_mode(phy, PHY_MODE_USB_HOST, 0);
Neil Armstrong1cbd9ae2018-04-11 17:08:02 +0200170
Neil Armstrong1cbd9ae2018-04-11 17:08:02 +0200171 return 0;
172}
173
174static int phy_meson_gxl_usb2_power_off(struct phy *phy)
175{
176 struct udevice *dev = phy->dev;
177 struct phy_meson_gxl_usb2_priv *priv = dev_get_priv(dev);
178 uint val;
179
180 regmap_read(priv->regmap, U2P_R0, &val);
181 /* power off the PHY by putting it into reset mode */
182 val |= U2P_R0_POWER_ON_RESET;
183 regmap_write(priv->regmap, U2P_R0, val);
184
Neil Armstrong1cbd9ae2018-04-11 17:08:02 +0200185 return 0;
186}
187
188struct phy_ops meson_gxl_usb2_phy_ops = {
189 .power_on = phy_meson_gxl_usb2_power_on,
190 .power_off = phy_meson_gxl_usb2_power_off,
Neil Armstrongfb21e2e2024-06-20 09:42:52 +0200191 .set_mode = phy_meson_gxl_usb2_set_mode,
Neil Armstrong1cbd9ae2018-04-11 17:08:02 +0200192};
193
194int meson_gxl_usb2_phy_probe(struct udevice *dev)
195{
196 struct phy_meson_gxl_usb2_priv *priv = dev_get_priv(dev);
197 int ret;
198
Masahiro Yamadae4873e32018-04-19 12:14:03 +0900199 ret = regmap_init_mem(dev_ofnode(dev), &priv->regmap);
Neil Armstrong1cbd9ae2018-04-11 17:08:02 +0200200 if (ret)
201 return ret;
202
203#if CONFIG_IS_ENABLED(CLK)
204 ret = clk_get_by_index(dev, 0, &priv->clk);
205 if (ret < 0)
206 return ret;
207
208 ret = clk_enable(&priv->clk);
209 if (ret && ret != -ENOSYS && ret != -ENOTSUPP) {
210 pr_err("failed to enable PHY clock\n");
Neil Armstrong1cbd9ae2018-04-11 17:08:02 +0200211 return ret;
212 }
213#endif
214
Neil Armstrong1cbd9ae2018-04-11 17:08:02 +0200215 return 0;
216}
217
218static const struct udevice_id meson_gxl_usb2_phy_ids[] = {
219 { .compatible = "amlogic,meson-gxl-usb2-phy" },
220 { }
221};
222
223U_BOOT_DRIVER(meson_gxl_usb2_phy) = {
224 .name = "meson_gxl_usb2_phy",
225 .id = UCLASS_PHY,
226 .of_match = meson_gxl_usb2_phy_ids,
227 .probe = meson_gxl_usb2_phy_probe,
228 .ops = &meson_gxl_usb2_phy_ops,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700229 .priv_auto = sizeof(struct phy_meson_gxl_usb2_priv),
Neil Armstrong1cbd9ae2018-04-11 17:08:02 +0200230};