blob: e051e662243622bb97b3272c3c1a61ea31485ee3 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Neil Armstrong2960e272018-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 Armstrong2960e272018-04-11 17:08:02 +02008 */
9
Simon Glass336d4612020-02-03 07:36:16 -070010#include <malloc.h>
Neil Armstrong2960e272018-04-11 17:08:02 +020011#include <asm/io.h>
12#include <bitfield.h>
Simon Glass1e94b462023-09-14 18:21:46 -060013#include <clk.h>
Neil Armstrong2960e272018-04-11 17:08:02 +020014#include <dm.h>
15#include <errno.h>
16#include <generic-phy.h>
17#include <regmap.h>
Simon Glassc05ed002020-05-10 11:40:11 -060018#include <linux/delay.h>
Simon Glass1e94b462023-09-14 18:21:46 -060019#include <linux/printk.h>
Neil Armstrong838c0af2020-03-30 11:27:24 +020020#include <linux/usb/otg.h>
21
22#include <asm/arch/usb-gx.h>
Neil Armstrong2960e272018-04-11 17:08:02 +020023
24#include <linux/bitops.h>
25#include <linux/compat.h>
26
Neil Armstrong2960e272018-04-11 17:08:02 +020027/* bits [31:27] are read-only */
28#define U2P_R0 0x0
29 #define U2P_R0_BYPASS_SEL BIT(0)
30 #define U2P_R0_BYPASS_DM_EN BIT(1)
31 #define U2P_R0_BYPASS_DP_EN BIT(2)
32 #define U2P_R0_TXBITSTUFF_ENH BIT(3)
33 #define U2P_R0_TXBITSTUFF_EN BIT(4)
34 #define U2P_R0_DM_PULLDOWN BIT(5)
35 #define U2P_R0_DP_PULLDOWN BIT(6)
36 #define U2P_R0_DP_VBUS_VLD_EXT_SEL BIT(7)
37 #define U2P_R0_DP_VBUS_VLD_EXT BIT(8)
38 #define U2P_R0_ADP_PRB_EN BIT(9)
39 #define U2P_R0_ADP_DISCHARGE BIT(10)
40 #define U2P_R0_ADP_CHARGE BIT(11)
41 #define U2P_R0_DRV_VBUS BIT(12)
42 #define U2P_R0_ID_PULLUP BIT(13)
43 #define U2P_R0_LOOPBACK_EN_B BIT(14)
44 #define U2P_R0_OTG_DISABLE BIT(15)
45 #define U2P_R0_COMMON_ONN BIT(16)
46 #define U2P_R0_FSEL_MASK GENMASK(19, 17)
47 #define U2P_R0_REF_CLK_SEL_MASK GENMASK(21, 20)
48 #define U2P_R0_POWER_ON_RESET BIT(22)
49 #define U2P_R0_V_ATE_TEST_EN_B_MASK GENMASK(24, 23)
50 #define U2P_R0_ID_SET_ID_DQ BIT(25)
51 #define U2P_R0_ATE_RESET BIT(26)
52 #define U2P_R0_FSV_MINUS BIT(27)
53 #define U2P_R0_FSV_PLUS BIT(28)
54 #define U2P_R0_BYPASS_DM_DATA BIT(29)
55 #define U2P_R0_BYPASS_DP_DATA BIT(30)
56
57#define U2P_R1 0x4
58 #define U2P_R1_BURN_IN_TEST BIT(0)
59 #define U2P_R1_ACA_ENABLE BIT(1)
60 #define U2P_R1_DCD_ENABLE BIT(2)
61 #define U2P_R1_VDAT_SRC_EN_B BIT(3)
62 #define U2P_R1_VDAT_DET_EN_B BIT(4)
63 #define U2P_R1_CHARGES_SEL BIT(5)
64 #define U2P_R1_TX_PREEMP_PULSE_TUNE BIT(6)
65 #define U2P_R1_TX_PREEMP_AMP_TUNE_MASK GENMASK(8, 7)
66 #define U2P_R1_TX_RES_TUNE_MASK GENMASK(10, 9)
67 #define U2P_R1_TX_RISE_TUNE_MASK GENMASK(12, 11)
68 #define U2P_R1_TX_VREF_TUNE_MASK GENMASK(16, 13)
69 #define U2P_R1_TX_FSLS_TUNE_MASK GENMASK(20, 17)
70 #define U2P_R1_TX_HSXV_TUNE_MASK GENMASK(22, 21)
71 #define U2P_R1_OTG_TUNE_MASK GENMASK(25, 23)
72 #define U2P_R1_SQRX_TUNE_MASK GENMASK(28, 26)
73 #define U2P_R1_COMP_DIS_TUNE_MASK GENMASK(31, 29)
74
75/* bits [31:14] are read-only */
76#define U2P_R2 0x8
77 #define U2P_R2_TESTDATA_IN_MASK GENMASK(7, 0)
78 #define U2P_R2_TESTADDR_MASK GENMASK(11, 8)
79 #define U2P_R2_TESTDATA_OUT_SEL BIT(12)
80 #define U2P_R2_TESTCLK BIT(13)
81 #define U2P_R2_TESTDATA_OUT_MASK GENMASK(17, 14)
82 #define U2P_R2_ACA_PIN_RANGE_C BIT(18)
83 #define U2P_R2_ACA_PIN_RANGE_B BIT(19)
84 #define U2P_R2_ACA_PIN_RANGE_A BIT(20)
85 #define U2P_R2_ACA_PIN_GND BIT(21)
86 #define U2P_R2_ACA_PIN_FLOAT BIT(22)
87 #define U2P_R2_CHARGE_DETECT BIT(23)
88 #define U2P_R2_DEVICE_SESSION_VALID BIT(24)
89 #define U2P_R2_ADP_PROBE BIT(25)
90 #define U2P_R2_ADP_SENSE BIT(26)
91 #define U2P_R2_SESSION_END BIT(27)
92 #define U2P_R2_VBUS_VALID BIT(28)
93 #define U2P_R2_B_VALID BIT(29)
94 #define U2P_R2_A_VALID BIT(30)
95 #define U2P_R2_ID_DIG BIT(31)
96
97#define U2P_R3 0xc
98
99#define RESET_COMPLETE_TIME 500
100
101struct phy_meson_gxl_usb2_priv {
102 struct regmap *regmap;
Neil Armstrong2960e272018-04-11 17:08:02 +0200103#if CONFIG_IS_ENABLED(CLK)
104 struct clk clk;
105#endif
106};
107
108static void phy_meson_gxl_usb2_reset(struct phy_meson_gxl_usb2_priv *priv)
109{
110 uint val;
111
112 regmap_read(priv->regmap, U2P_R0, &val);
113
114 /* reset the PHY and wait until settings are stabilized */
115 val |= U2P_R0_POWER_ON_RESET;
116 regmap_write(priv->regmap, U2P_R0, val);
117 udelay(RESET_COMPLETE_TIME);
118
119 val &= ~U2P_R0_POWER_ON_RESET;
120 regmap_write(priv->regmap, U2P_R0, val);
121 udelay(RESET_COMPLETE_TIME);
122}
123
Neil Armstrong838c0af2020-03-30 11:27:24 +0200124void phy_meson_gxl_usb2_set_mode(struct phy *phy, enum usb_dr_mode mode)
Neil Armstrong2960e272018-04-11 17:08:02 +0200125{
Neil Armstrong838c0af2020-03-30 11:27:24 +0200126 struct udevice *dev = phy->dev;
127 struct phy_meson_gxl_usb2_priv *priv = dev_get_priv(dev);
Neil Armstrong2960e272018-04-11 17:08:02 +0200128 uint val;
129
130 regmap_read(priv->regmap, U2P_R0, &val);
Neil Armstrong838c0af2020-03-30 11:27:24 +0200131
132 switch (mode) {
133 case USB_DR_MODE_UNKNOWN:
134 case USB_DR_MODE_HOST:
135 case USB_DR_MODE_OTG:
136 val |= U2P_R0_DM_PULLDOWN;
137 val |= U2P_R0_DP_PULLDOWN;
138 val &= ~U2P_R0_ID_PULLUP;
139 break;
140
141 case USB_DR_MODE_PERIPHERAL:
142 val &= ~U2P_R0_DM_PULLDOWN;
143 val &= ~U2P_R0_DP_PULLDOWN;
144 val |= U2P_R0_ID_PULLUP;
145 break;
146 }
147
Neil Armstrong2960e272018-04-11 17:08:02 +0200148 regmap_write(priv->regmap, U2P_R0, val);
149
150 phy_meson_gxl_usb2_reset(priv);
151}
152
Neil Armstronge7265f62024-06-20 09:42:50 +0200153static int _phy_meson_gxl_usb2_set_mode(struct phy *phy, enum phy_mode mode, int submode)
154{
155 if (submode)
156 return -EOPNOTSUPP;
157
158 switch (mode) {
159 case PHY_MODE_USB_DEVICE:
160 phy_meson_gxl_usb2_set_mode(phy, USB_DR_MODE_PERIPHERAL);
161 break;
162
163 case PHY_MODE_USB_HOST:
164 case PHY_MODE_USB_OTG:
165 phy_meson_gxl_usb2_set_mode(phy, USB_DR_MODE_HOST);
166 break;
167
168 default:
169 return -EINVAL;
170 }
171
172 return 0;
173}
174
Neil Armstrong2960e272018-04-11 17:08:02 +0200175static int phy_meson_gxl_usb2_power_on(struct phy *phy)
176{
177 struct udevice *dev = phy->dev;
178 struct phy_meson_gxl_usb2_priv *priv = dev_get_priv(dev);
179 uint val;
180
181 regmap_read(priv->regmap, U2P_R0, &val);
182 /* power on the PHY by taking it out of reset mode */
183 val &= ~U2P_R0_POWER_ON_RESET;
184 regmap_write(priv->regmap, U2P_R0, val);
185
Neil Armstronge7265f62024-06-20 09:42:50 +0200186 _phy_meson_gxl_usb2_set_mode(phy, PHY_MODE_USB_HOST, 0);
Neil Armstrong2960e272018-04-11 17:08:02 +0200187
Neil Armstrong2960e272018-04-11 17:08:02 +0200188 return 0;
189}
190
191static int phy_meson_gxl_usb2_power_off(struct phy *phy)
192{
193 struct udevice *dev = phy->dev;
194 struct phy_meson_gxl_usb2_priv *priv = dev_get_priv(dev);
195 uint val;
196
197 regmap_read(priv->regmap, U2P_R0, &val);
198 /* power off the PHY by putting it into reset mode */
199 val |= U2P_R0_POWER_ON_RESET;
200 regmap_write(priv->regmap, U2P_R0, val);
201
Neil Armstrong2960e272018-04-11 17:08:02 +0200202 return 0;
203}
204
205struct phy_ops meson_gxl_usb2_phy_ops = {
206 .power_on = phy_meson_gxl_usb2_power_on,
207 .power_off = phy_meson_gxl_usb2_power_off,
Neil Armstronge7265f62024-06-20 09:42:50 +0200208 .set_mode = _phy_meson_gxl_usb2_set_mode,
Neil Armstrong2960e272018-04-11 17:08:02 +0200209};
210
211int meson_gxl_usb2_phy_probe(struct udevice *dev)
212{
213 struct phy_meson_gxl_usb2_priv *priv = dev_get_priv(dev);
214 int ret;
215
Masahiro Yamadad3581232018-04-19 12:14:03 +0900216 ret = regmap_init_mem(dev_ofnode(dev), &priv->regmap);
Neil Armstrong2960e272018-04-11 17:08:02 +0200217 if (ret)
218 return ret;
219
220#if CONFIG_IS_ENABLED(CLK)
221 ret = clk_get_by_index(dev, 0, &priv->clk);
222 if (ret < 0)
223 return ret;
224
225 ret = clk_enable(&priv->clk);
226 if (ret && ret != -ENOSYS && ret != -ENOTSUPP) {
227 pr_err("failed to enable PHY clock\n");
Neil Armstrong2960e272018-04-11 17:08:02 +0200228 return ret;
229 }
230#endif
231
Neil Armstrong2960e272018-04-11 17:08:02 +0200232 return 0;
233}
234
235static const struct udevice_id meson_gxl_usb2_phy_ids[] = {
236 { .compatible = "amlogic,meson-gxl-usb2-phy" },
237 { }
238};
239
240U_BOOT_DRIVER(meson_gxl_usb2_phy) = {
241 .name = "meson_gxl_usb2_phy",
242 .id = UCLASS_PHY,
243 .of_match = meson_gxl_usb2_phy_ids,
244 .probe = meson_gxl_usb2_phy_probe,
245 .ops = &meson_gxl_usb2_phy_ops,
Simon Glass41575d82020-12-03 16:55:17 -0700246 .priv_auto = sizeof(struct phy_meson_gxl_usb2_priv),
Neil Armstrong2960e272018-04-11 17:08:02 +0200247};