blob: 40a5da948dc15481976977b49d2d6f6a89a5dad6 [file] [log] [blame]
Neil Armstrong277d9162019-02-19 15:17:29 +01001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Meson G12A USB3+PCIE Combo PHY driver
4 *
5 * Copyright (C) 2018 Martin Blumenstingl <martin.blumenstingl@googlemail.com>
6 * Copyright (C) 2019 BayLibre, SAS
7 * Author: Neil Armstrong <narmstron@baylibre.com>
8 */
9
10#include <common.h>
11#include <clk.h>
12#include <dm.h>
Simon Glass336d4612020-02-03 07:36:16 -070013#include <malloc.h>
Neil Armstrong277d9162019-02-19 15:17:29 +010014#include <regmap.h>
15#include <errno.h>
16#include <asm/io.h>
17#include <reset.h>
18#include <bitfield.h>
19#include <generic-phy.h>
Simon Glassc05ed002020-05-10 11:40:11 -060020#include <linux/delay.h>
Simon Glass1e94b462023-09-14 18:21:46 -060021#include <linux/printk.h>
Neil Armstrong277d9162019-02-19 15:17:29 +010022
23#include <linux/bitops.h>
24#include <linux/compat.h>
25#include <linux/bitfield.h>
26
Neil Armstrong320160c2021-02-25 17:53:23 +010027#define PHY_TYPE_PCIE 2
28#define PHY_TYPE_USB3 4
29
Neil Armstrong277d9162019-02-19 15:17:29 +010030#define PHY_R0 0x00
31 #define PHY_R0_PCIE_POWER_STATE GENMASK(4, 0)
32 #define PHY_R0_PCIE_USB3_SWITCH GENMASK(6, 5)
33
34#define PHY_R1 0x04
35 #define PHY_R1_PHY_TX1_TERM_OFFSET GENMASK(4, 0)
36 #define PHY_R1_PHY_TX0_TERM_OFFSET GENMASK(9, 5)
37 #define PHY_R1_PHY_RX1_EQ GENMASK(12, 10)
38 #define PHY_R1_PHY_RX0_EQ GENMASK(15, 13)
39 #define PHY_R1_PHY_LOS_LEVEL GENMASK(20, 16)
40 #define PHY_R1_PHY_LOS_BIAS GENMASK(23, 21)
41 #define PHY_R1_PHY_REF_CLKDIV2 BIT(24)
42 #define PHY_R1_PHY_MPLL_MULTIPLIER GENMASK(31, 25)
43
44#define PHY_R2 0x08
45 #define PHY_R2_PCS_TX_DEEMPH_GEN2_6DB GENMASK(5, 0)
46 #define PHY_R2_PCS_TX_DEEMPH_GEN2_3P5DB GENMASK(11, 6)
47 #define PHY_R2_PCS_TX_DEEMPH_GEN1 GENMASK(17, 12)
48 #define PHY_R2_PHY_TX_VBOOST_LVL GENMASK(20, 18)
49
50#define PHY_R4 0x10
51 #define PHY_R4_PHY_CR_WRITE BIT(0)
52 #define PHY_R4_PHY_CR_READ BIT(1)
53 #define PHY_R4_PHY_CR_DATA_IN GENMASK(17, 2)
54 #define PHY_R4_PHY_CR_CAP_DATA BIT(18)
55 #define PHY_R4_PHY_CR_CAP_ADDR BIT(19)
56
57#define PHY_R5 0x14
58 #define PHY_R5_PHY_CR_DATA_OUT GENMASK(15, 0)
59 #define PHY_R5_PHY_CR_ACK BIT(16)
60 #define PHY_R5_PHY_BS_OUT BIT(17)
61
Neil Armstrong320160c2021-02-25 17:53:23 +010062#define PCIE_RESET_DELAY 500
63
Neil Armstrong277d9162019-02-19 15:17:29 +010064struct phy_g12a_usb3_pcie_priv {
65 struct regmap *regmap;
66#if CONFIG_IS_ENABLED(CLK)
67 struct clk clk;
68#endif
69 struct reset_ctl_bulk resets;
70};
71
72static int phy_g12a_usb3_pcie_cr_bus_addr(struct phy_g12a_usb3_pcie_priv *priv,
73 unsigned int addr)
74{
75 unsigned int val, reg;
76 int ret;
77
78 reg = FIELD_PREP(PHY_R4_PHY_CR_DATA_IN, addr);
79
80 regmap_write(priv->regmap, PHY_R4, reg);
81 regmap_write(priv->regmap, PHY_R4, reg);
82
83 regmap_write(priv->regmap, PHY_R4, reg | PHY_R4_PHY_CR_CAP_ADDR);
84
85 ret = regmap_read_poll_timeout(priv->regmap, PHY_R5, val,
86 (val & PHY_R5_PHY_CR_ACK),
87 5, 1000);
88 if (ret)
89 return ret;
90
91 regmap_write(priv->regmap, PHY_R4, reg);
92
93 ret = regmap_read_poll_timeout(priv->regmap, PHY_R5, val,
94 !(val & PHY_R5_PHY_CR_ACK),
95 5, 1000);
96 if (ret)
97 return ret;
98
99 return 0;
100}
101
102static int
103phy_g12a_usb3_pcie_cr_bus_read(struct phy_g12a_usb3_pcie_priv *priv,
104 unsigned int addr, unsigned int *data)
105{
106 unsigned int val;
107 int ret;
108
109 ret = phy_g12a_usb3_pcie_cr_bus_addr(priv, addr);
110 if (ret)
111 return ret;
112
113 regmap_write(priv->regmap, PHY_R4, 0);
114 regmap_write(priv->regmap, PHY_R4, PHY_R4_PHY_CR_READ);
115
116 ret = regmap_read_poll_timeout(priv->regmap, PHY_R5, val,
117 (val & PHY_R5_PHY_CR_ACK),
118 5, 1000);
119 if (ret)
120 return ret;
121
122 *data = FIELD_GET(PHY_R5_PHY_CR_DATA_OUT, val);
123
124 regmap_write(priv->regmap, PHY_R4, 0);
125
126 ret = regmap_read_poll_timeout(priv->regmap, PHY_R5, val,
127 !(val & PHY_R5_PHY_CR_ACK),
128 5, 1000);
129 if (ret)
130 return ret;
131
132 return 0;
133}
134
135static int
136phy_g12a_usb3_pcie_cr_bus_write(struct phy_g12a_usb3_pcie_priv *priv,
137 unsigned int addr, unsigned int data)
138{
139 unsigned int val, reg;
140 int ret;
141
142 ret = phy_g12a_usb3_pcie_cr_bus_addr(priv, addr);
143 if (ret)
144 return ret;
145
146 reg = FIELD_PREP(PHY_R4_PHY_CR_DATA_IN, data);
147
148 regmap_write(priv->regmap, PHY_R4, reg);
149 regmap_write(priv->regmap, PHY_R4, reg);
150
151 regmap_write(priv->regmap, PHY_R4, reg | PHY_R4_PHY_CR_CAP_DATA);
152
153 ret = regmap_read_poll_timeout(priv->regmap, PHY_R5, val,
154 (val & PHY_R5_PHY_CR_ACK),
155 5, 1000);
156 if (ret)
157 return ret;
158
159 regmap_write(priv->regmap, PHY_R4, reg);
160
161 ret = regmap_read_poll_timeout(priv->regmap, PHY_R5, val,
162 (val & PHY_R5_PHY_CR_ACK) == 0,
163 5, 1000);
164 if (ret)
165 return ret;
166
167 regmap_write(priv->regmap, PHY_R4, reg);
168
169 regmap_write(priv->regmap, PHY_R4, reg | PHY_R4_PHY_CR_WRITE);
170
171 ret = regmap_read_poll_timeout(priv->regmap, PHY_R5, val,
172 (val & PHY_R5_PHY_CR_ACK),
173 5, 1000);
174 if (ret)
175 return ret;
176
177 regmap_write(priv->regmap, PHY_R4, reg);
178
179 ret = regmap_read_poll_timeout(priv->regmap, PHY_R5, val,
180 (val & PHY_R5_PHY_CR_ACK) == 0,
181 5, 1000);
182 if (ret)
183 return ret;
184
185 return 0;
186}
187
188static int
189phy_g12a_usb3_pcie_cr_bus_update_bits(struct phy_g12a_usb3_pcie_priv *priv,
190 uint offset, uint mask, uint val)
191{
192 uint reg;
193 int ret;
194
195 ret = phy_g12a_usb3_pcie_cr_bus_read(priv, offset, &reg);
196 if (ret)
197 return ret;
198
199 reg &= ~mask;
200
201 return phy_g12a_usb3_pcie_cr_bus_write(priv, offset, reg | val);
202}
203
204static int phy_meson_g12a_usb3_init(struct phy *phy)
205{
206 struct udevice *dev = phy->dev;
207 struct phy_g12a_usb3_pcie_priv *priv = dev_get_priv(dev);
208 unsigned int data;
209 int ret;
210
Neil Armstrong277d9162019-02-19 15:17:29 +0100211 ret = reset_assert_bulk(&priv->resets);
212 udelay(1);
213 ret |= reset_deassert_bulk(&priv->resets);
214 if (ret)
215 return ret;
216
217 /* Switch PHY to USB3 */
218 regmap_update_bits(priv->regmap, PHY_R0,
219 PHY_R0_PCIE_USB3_SWITCH,
220 PHY_R0_PCIE_USB3_SWITCH);
221
222 /*
223 * WORKAROUND: There is SSPHY suspend bug due to
224 * which USB enumerates
225 * in HS mode instead of SS mode. Workaround it by asserting
226 * LANE0.TX_ALT_BLOCK.EN_ALT_BUS to enable TX to use alt bus
227 * mode
228 */
229 ret = phy_g12a_usb3_pcie_cr_bus_update_bits(priv, 0x102d,
230 BIT(7), BIT(7));
231 if (ret)
232 return ret;
233
234 ret = phy_g12a_usb3_pcie_cr_bus_update_bits(priv, 0x1010, 0xff0, 20);
235 if (ret)
236 return ret;
237
238 /*
239 * Fix RX Equalization setting as follows
240 * LANE0.RX_OVRD_IN_HI. RX_EQ_EN set to 0
241 * LANE0.RX_OVRD_IN_HI.RX_EQ_EN_OVRD set to 1
242 * LANE0.RX_OVRD_IN_HI.RX_EQ set to 3
243 * LANE0.RX_OVRD_IN_HI.RX_EQ_OVRD set to 1
244 */
245 ret = phy_g12a_usb3_pcie_cr_bus_read(priv, 0x1006, &data);
246 if (ret)
247 return ret;
248
249 data &= ~BIT(6);
250 data |= BIT(7);
251 data &= ~(0x7 << 8);
252 data |= (0x3 << 8);
253 data |= (1 << 11);
254 ret = phy_g12a_usb3_pcie_cr_bus_write(priv, 0x1006, data);
255 if (ret)
256 return ret;
257
258 /*
259 * Set EQ and TX launch amplitudes as follows
260 * LANE0.TX_OVRD_DRV_LO.PREEMPH set to 22
261 * LANE0.TX_OVRD_DRV_LO.AMPLITUDE set to 127
262 * LANE0.TX_OVRD_DRV_LO.EN set to 1.
263 */
264 ret = phy_g12a_usb3_pcie_cr_bus_read(priv, 0x1002, &data);
265 if (ret)
266 return ret;
267
268 data &= ~0x3f80;
269 data |= (0x16 << 7);
270 data &= ~0x7f;
271 data |= (0x7f | BIT(14));
272 ret = phy_g12a_usb3_pcie_cr_bus_write(priv, 0x1002, data);
273 if (ret)
274 return ret;
275
276 /*
277 * MPLL_LOOP_CTL.PROP_CNTRL = 8
278 */
279 ret = phy_g12a_usb3_pcie_cr_bus_update_bits(priv, 0x30,
280 0xf << 4, 8 << 4);
281 if (ret)
282 return ret;
283
284 regmap_update_bits(priv->regmap, PHY_R2,
285 PHY_R2_PHY_TX_VBOOST_LVL,
286 FIELD_PREP(PHY_R2_PHY_TX_VBOOST_LVL, 0x4));
287
288 regmap_update_bits(priv->regmap, PHY_R1,
289 PHY_R1_PHY_LOS_BIAS | PHY_R1_PHY_LOS_LEVEL,
290 FIELD_PREP(PHY_R1_PHY_LOS_BIAS, 4) |
291 FIELD_PREP(PHY_R1_PHY_LOS_LEVEL, 9));
292
293 return ret;
294}
295
296static int phy_meson_g12a_usb3_exit(struct phy *phy)
297{
298 struct phy_g12a_usb3_pcie_priv *priv = dev_get_priv(phy->dev);
299
300 return reset_assert_bulk(&priv->resets);
301}
302
Neil Armstrong320160c2021-02-25 17:53:23 +0100303static int phy_meson_g12a_usb3_pcie_init(struct phy *phy)
304{
305 if (phy->id == PHY_TYPE_USB3)
306 return phy_meson_g12a_usb3_init(phy);
307
308 return 0;
309}
310
311static int phy_meson_g12a_usb3_pcie_exit(struct phy *phy)
312{
313 if (phy->id == PHY_TYPE_USB3)
314 return phy_meson_g12a_usb3_exit(phy);
315
316 return 0;
317}
318
319static int phy_meson_g12a_usb3_pcie_power_on(struct phy *phy)
320{
321 struct phy_g12a_usb3_pcie_priv *priv = dev_get_priv(phy->dev);
322
323 if (phy->id == PHY_TYPE_USB3)
324 return 0;
325
326 regmap_update_bits(priv->regmap, PHY_R0,
327 PHY_R0_PCIE_POWER_STATE,
328 FIELD_PREP(PHY_R0_PCIE_POWER_STATE, 0x1c));
329
330 return 0;
331}
332
333static int phy_meson_g12a_usb3_pcie_power_off(struct phy *phy)
334{
335 struct phy_g12a_usb3_pcie_priv *priv = dev_get_priv(phy->dev);
336
337 if (phy->id == PHY_TYPE_USB3)
338 return 0;
339
340 regmap_update_bits(priv->regmap, PHY_R0,
341 PHY_R0_PCIE_POWER_STATE,
342 FIELD_PREP(PHY_R0_PCIE_POWER_STATE, 0x1d));
343
344 return 0;
345}
346
347static int phy_meson_g12a_usb3_pcie_reset(struct phy *phy)
348{
349 struct phy_g12a_usb3_pcie_priv *priv = dev_get_priv(phy->dev);
350 int ret;
351
352 if (phy->id == PHY_TYPE_USB3)
353 return 0;
354
355 ret = reset_assert_bulk(&priv->resets);
356 if (ret)
357 return ret;
358
359 udelay(PCIE_RESET_DELAY);
360
361 ret = reset_deassert_bulk(&priv->resets);
362 if (ret)
363 return ret;
364
365 udelay(PCIE_RESET_DELAY);
366
367 return 0;
368}
369
Neil Armstrong277d9162019-02-19 15:17:29 +0100370struct phy_ops meson_g12a_usb3_pcie_phy_ops = {
Neil Armstrong320160c2021-02-25 17:53:23 +0100371 .init = phy_meson_g12a_usb3_pcie_init,
372 .exit = phy_meson_g12a_usb3_pcie_exit,
373 .power_on = phy_meson_g12a_usb3_pcie_power_on,
374 .power_off = phy_meson_g12a_usb3_pcie_power_off,
375 .reset = phy_meson_g12a_usb3_pcie_reset,
Neil Armstrong277d9162019-02-19 15:17:29 +0100376};
377
378int meson_g12a_usb3_pcie_phy_probe(struct udevice *dev)
379{
380 struct phy_g12a_usb3_pcie_priv *priv = dev_get_priv(dev);
381 int ret;
382
383 ret = regmap_init_mem(dev_ofnode(dev), &priv->regmap);
384 if (ret)
385 return ret;
386
387 ret = reset_get_bulk(dev, &priv->resets);
388 if (ret == -ENOTSUPP)
389 return 0;
390 else if (ret)
391 return ret;
392
393#if CONFIG_IS_ENABLED(CLK)
394 ret = clk_get_by_index(dev, 0, &priv->clk);
395 if (ret < 0)
396 return ret;
397
398 ret = clk_enable(&priv->clk);
399 if (ret && ret != -ENOENT && ret != -ENOTSUPP) {
400 pr_err("failed to enable PHY clock\n");
401 clk_free(&priv->clk);
402 return ret;
403 }
404#endif
405
406 return 0;
407}
408
409static const struct udevice_id meson_g12a_usb3_pcie_phy_ids[] = {
410 { .compatible = "amlogic,g12a-usb3-pcie-phy" },
411 { }
412};
413
414U_BOOT_DRIVER(meson_g12a_usb3_pcie_phy) = {
415 .name = "meson_g12a_usb3_pcie_phy",
416 .id = UCLASS_PHY,
417 .of_match = meson_g12a_usb3_pcie_phy_ids,
418 .probe = meson_g12a_usb3_pcie_phy_probe,
419 .ops = &meson_g12a_usb3_pcie_phy_ops,
Simon Glass41575d82020-12-03 16:55:17 -0700420 .priv_auto = sizeof(struct phy_g12a_usb3_pcie_priv),
Neil Armstrong277d9162019-02-19 15:17:29 +0100421};