1 // SPDX-License-Identifier: GPL-2.0+
3 * Meson G12A USB3+PCIE Combo PHY driver
5 * Copyright (C) 2018 Martin Blumenstingl <martin.blumenstingl@googlemail.com>
6 * Copyright (C) 2019 BayLibre, SAS
7 * Author: Neil Armstrong <narmstron@baylibre.com>
19 #include <generic-phy.h>
21 #include <linux/bitops.h>
22 #include <linux/compat.h>
23 #include <linux/bitfield.h>
26 #define PHY_R0_PCIE_POWER_STATE GENMASK(4, 0)
27 #define PHY_R0_PCIE_USB3_SWITCH GENMASK(6, 5)
30 #define PHY_R1_PHY_TX1_TERM_OFFSET GENMASK(4, 0)
31 #define PHY_R1_PHY_TX0_TERM_OFFSET GENMASK(9, 5)
32 #define PHY_R1_PHY_RX1_EQ GENMASK(12, 10)
33 #define PHY_R1_PHY_RX0_EQ GENMASK(15, 13)
34 #define PHY_R1_PHY_LOS_LEVEL GENMASK(20, 16)
35 #define PHY_R1_PHY_LOS_BIAS GENMASK(23, 21)
36 #define PHY_R1_PHY_REF_CLKDIV2 BIT(24)
37 #define PHY_R1_PHY_MPLL_MULTIPLIER GENMASK(31, 25)
40 #define PHY_R2_PCS_TX_DEEMPH_GEN2_6DB GENMASK(5, 0)
41 #define PHY_R2_PCS_TX_DEEMPH_GEN2_3P5DB GENMASK(11, 6)
42 #define PHY_R2_PCS_TX_DEEMPH_GEN1 GENMASK(17, 12)
43 #define PHY_R2_PHY_TX_VBOOST_LVL GENMASK(20, 18)
46 #define PHY_R4_PHY_CR_WRITE BIT(0)
47 #define PHY_R4_PHY_CR_READ BIT(1)
48 #define PHY_R4_PHY_CR_DATA_IN GENMASK(17, 2)
49 #define PHY_R4_PHY_CR_CAP_DATA BIT(18)
50 #define PHY_R4_PHY_CR_CAP_ADDR BIT(19)
53 #define PHY_R5_PHY_CR_DATA_OUT GENMASK(15, 0)
54 #define PHY_R5_PHY_CR_ACK BIT(16)
55 #define PHY_R5_PHY_BS_OUT BIT(17)
57 struct phy_g12a_usb3_pcie_priv {
58 struct regmap *regmap;
59 #if CONFIG_IS_ENABLED(CLK)
62 struct reset_ctl_bulk resets;
65 static int phy_g12a_usb3_pcie_cr_bus_addr(struct phy_g12a_usb3_pcie_priv *priv,
68 unsigned int val, reg;
71 reg = FIELD_PREP(PHY_R4_PHY_CR_DATA_IN, addr);
73 regmap_write(priv->regmap, PHY_R4, reg);
74 regmap_write(priv->regmap, PHY_R4, reg);
76 regmap_write(priv->regmap, PHY_R4, reg | PHY_R4_PHY_CR_CAP_ADDR);
78 ret = regmap_read_poll_timeout(priv->regmap, PHY_R5, val,
79 (val & PHY_R5_PHY_CR_ACK),
84 regmap_write(priv->regmap, PHY_R4, reg);
86 ret = regmap_read_poll_timeout(priv->regmap, PHY_R5, val,
87 !(val & PHY_R5_PHY_CR_ACK),
96 phy_g12a_usb3_pcie_cr_bus_read(struct phy_g12a_usb3_pcie_priv *priv,
97 unsigned int addr, unsigned int *data)
102 ret = phy_g12a_usb3_pcie_cr_bus_addr(priv, addr);
106 regmap_write(priv->regmap, PHY_R4, 0);
107 regmap_write(priv->regmap, PHY_R4, PHY_R4_PHY_CR_READ);
109 ret = regmap_read_poll_timeout(priv->regmap, PHY_R5, val,
110 (val & PHY_R5_PHY_CR_ACK),
115 *data = FIELD_GET(PHY_R5_PHY_CR_DATA_OUT, val);
117 regmap_write(priv->regmap, PHY_R4, 0);
119 ret = regmap_read_poll_timeout(priv->regmap, PHY_R5, val,
120 !(val & PHY_R5_PHY_CR_ACK),
129 phy_g12a_usb3_pcie_cr_bus_write(struct phy_g12a_usb3_pcie_priv *priv,
130 unsigned int addr, unsigned int data)
132 unsigned int val, reg;
135 ret = phy_g12a_usb3_pcie_cr_bus_addr(priv, addr);
139 reg = FIELD_PREP(PHY_R4_PHY_CR_DATA_IN, data);
141 regmap_write(priv->regmap, PHY_R4, reg);
142 regmap_write(priv->regmap, PHY_R4, reg);
144 regmap_write(priv->regmap, PHY_R4, reg | PHY_R4_PHY_CR_CAP_DATA);
146 ret = regmap_read_poll_timeout(priv->regmap, PHY_R5, val,
147 (val & PHY_R5_PHY_CR_ACK),
152 regmap_write(priv->regmap, PHY_R4, reg);
154 ret = regmap_read_poll_timeout(priv->regmap, PHY_R5, val,
155 (val & PHY_R5_PHY_CR_ACK) == 0,
160 regmap_write(priv->regmap, PHY_R4, reg);
162 regmap_write(priv->regmap, PHY_R4, reg | PHY_R4_PHY_CR_WRITE);
164 ret = regmap_read_poll_timeout(priv->regmap, PHY_R5, val,
165 (val & PHY_R5_PHY_CR_ACK),
170 regmap_write(priv->regmap, PHY_R4, reg);
172 ret = regmap_read_poll_timeout(priv->regmap, PHY_R5, val,
173 (val & PHY_R5_PHY_CR_ACK) == 0,
182 phy_g12a_usb3_pcie_cr_bus_update_bits(struct phy_g12a_usb3_pcie_priv *priv,
183 uint offset, uint mask, uint val)
188 ret = phy_g12a_usb3_pcie_cr_bus_read(priv, offset, ®);
194 return phy_g12a_usb3_pcie_cr_bus_write(priv, offset, reg | val);
197 static int phy_meson_g12a_usb3_init(struct phy *phy)
199 struct udevice *dev = phy->dev;
200 struct phy_g12a_usb3_pcie_priv *priv = dev_get_priv(dev);
204 /* TOFIX Handle PCIE mode */
206 ret = reset_assert_bulk(&priv->resets);
208 ret |= reset_deassert_bulk(&priv->resets);
212 /* Switch PHY to USB3 */
213 regmap_update_bits(priv->regmap, PHY_R0,
214 PHY_R0_PCIE_USB3_SWITCH,
215 PHY_R0_PCIE_USB3_SWITCH);
218 * WORKAROUND: There is SSPHY suspend bug due to
219 * which USB enumerates
220 * in HS mode instead of SS mode. Workaround it by asserting
221 * LANE0.TX_ALT_BLOCK.EN_ALT_BUS to enable TX to use alt bus
224 ret = phy_g12a_usb3_pcie_cr_bus_update_bits(priv, 0x102d,
229 ret = phy_g12a_usb3_pcie_cr_bus_update_bits(priv, 0x1010, 0xff0, 20);
234 * Fix RX Equalization setting as follows
235 * LANE0.RX_OVRD_IN_HI. RX_EQ_EN set to 0
236 * LANE0.RX_OVRD_IN_HI.RX_EQ_EN_OVRD set to 1
237 * LANE0.RX_OVRD_IN_HI.RX_EQ set to 3
238 * LANE0.RX_OVRD_IN_HI.RX_EQ_OVRD set to 1
240 ret = phy_g12a_usb3_pcie_cr_bus_read(priv, 0x1006, &data);
249 ret = phy_g12a_usb3_pcie_cr_bus_write(priv, 0x1006, data);
254 * Set EQ and TX launch amplitudes as follows
255 * LANE0.TX_OVRD_DRV_LO.PREEMPH set to 22
256 * LANE0.TX_OVRD_DRV_LO.AMPLITUDE set to 127
257 * LANE0.TX_OVRD_DRV_LO.EN set to 1.
259 ret = phy_g12a_usb3_pcie_cr_bus_read(priv, 0x1002, &data);
266 data |= (0x7f | BIT(14));
267 ret = phy_g12a_usb3_pcie_cr_bus_write(priv, 0x1002, data);
272 * MPLL_LOOP_CTL.PROP_CNTRL = 8
274 ret = phy_g12a_usb3_pcie_cr_bus_update_bits(priv, 0x30,
279 regmap_update_bits(priv->regmap, PHY_R2,
280 PHY_R2_PHY_TX_VBOOST_LVL,
281 FIELD_PREP(PHY_R2_PHY_TX_VBOOST_LVL, 0x4));
283 regmap_update_bits(priv->regmap, PHY_R1,
284 PHY_R1_PHY_LOS_BIAS | PHY_R1_PHY_LOS_LEVEL,
285 FIELD_PREP(PHY_R1_PHY_LOS_BIAS, 4) |
286 FIELD_PREP(PHY_R1_PHY_LOS_LEVEL, 9));
291 static int phy_meson_g12a_usb3_exit(struct phy *phy)
293 struct phy_g12a_usb3_pcie_priv *priv = dev_get_priv(phy->dev);
295 return reset_assert_bulk(&priv->resets);
298 struct phy_ops meson_g12a_usb3_pcie_phy_ops = {
299 .init = phy_meson_g12a_usb3_init,
300 .exit = phy_meson_g12a_usb3_exit,
303 int meson_g12a_usb3_pcie_phy_probe(struct udevice *dev)
305 struct phy_g12a_usb3_pcie_priv *priv = dev_get_priv(dev);
308 ret = regmap_init_mem(dev_ofnode(dev), &priv->regmap);
312 ret = reset_get_bulk(dev, &priv->resets);
313 if (ret == -ENOTSUPP)
318 #if CONFIG_IS_ENABLED(CLK)
319 ret = clk_get_by_index(dev, 0, &priv->clk);
323 ret = clk_enable(&priv->clk);
324 if (ret && ret != -ENOENT && ret != -ENOTSUPP) {
325 pr_err("failed to enable PHY clock\n");
326 clk_free(&priv->clk);
334 static const struct udevice_id meson_g12a_usb3_pcie_phy_ids[] = {
335 { .compatible = "amlogic,g12a-usb3-pcie-phy" },
339 U_BOOT_DRIVER(meson_g12a_usb3_pcie_phy) = {
340 .name = "meson_g12a_usb3_pcie_phy",
342 .of_match = meson_g12a_usb3_pcie_phy_ids,
343 .probe = meson_g12a_usb3_pcie_phy_probe,
344 .ops = &meson_g12a_usb3_pcie_phy_ops,
345 .priv_auto_alloc_size = sizeof(struct phy_g12a_usb3_pcie_priv),