1 // SPDX-License-Identifier: GPL-2.0+
3 * Meson G12A USB2 PHY driver
5 * Copyright (C) 2017 Martin Blumenstingl <martin.blumenstingl@googlemail.com>
6 * Copyright (C) 2019 BayLibre, SAS
7 * Author: Neil Armstrong <narmstron@baylibre.com>
15 #include <generic-phy.h>
17 #include <power/regulator.h>
21 #include <linux/bitops.h>
22 #include <linux/compat.h>
24 #define PHY_CTRL_R0 0x0
25 #define PHY_CTRL_R1 0x4
26 #define PHY_CTRL_R2 0x8
27 #define PHY_CTRL_R3 0xc
28 #define PHY_CTRL_R4 0x10
29 #define PHY_CTRL_R5 0x14
30 #define PHY_CTRL_R6 0x18
31 #define PHY_CTRL_R7 0x1c
32 #define PHY_CTRL_R8 0x20
33 #define PHY_CTRL_R9 0x24
34 #define PHY_CTRL_R10 0x28
35 #define PHY_CTRL_R11 0x2c
36 #define PHY_CTRL_R12 0x30
37 #define PHY_CTRL_R13 0x34
38 #define PHY_CTRL_R14 0x38
39 #define PHY_CTRL_R15 0x3c
40 #define PHY_CTRL_R16 0x40
41 #define PHY_CTRL_R17 0x44
42 #define PHY_CTRL_R18 0x48
43 #define PHY_CTRL_R19 0x4c
44 #define PHY_CTRL_R20 0x50
45 #define PHY_CTRL_R21 0x54
46 #define PHY_CTRL_R22 0x58
47 #define PHY_CTRL_R23 0x5c
49 #define RESET_COMPLETE_TIME 1000
50 #define PLL_RESET_COMPLETE_TIME 100
52 struct phy_meson_g12a_usb2_priv {
53 struct regmap *regmap;
54 #if CONFIG_IS_ENABLED(DM_REGULATOR)
55 struct udevice *phy_supply;
57 #if CONFIG_IS_ENABLED(CLK)
60 struct reset_ctl reset;
64 static int phy_meson_g12a_usb2_power_on(struct phy *phy)
66 struct udevice *dev = phy->dev;
67 struct phy_meson_g12a_usb2_priv *priv = dev_get_priv(dev);
69 #if CONFIG_IS_ENABLED(DM_REGULATOR)
70 if (priv->phy_supply) {
71 int ret = regulator_set_enable(priv->phy_supply, true);
80 static int phy_meson_g12a_usb2_power_off(struct phy *phy)
82 struct udevice *dev = phy->dev;
83 struct phy_meson_g12a_usb2_priv *priv = dev_get_priv(dev);
85 #if CONFIG_IS_ENABLED(DM_REGULATOR)
86 if (priv->phy_supply) {
87 int ret = regulator_set_enable(priv->phy_supply, false);
89 pr_err("Error disabling PHY supply\n");
98 static int phy_meson_g12a_usb2_init(struct phy *phy)
100 struct udevice *dev = phy->dev;
101 struct phy_meson_g12a_usb2_priv *priv = dev_get_priv(dev);
104 ret = reset_assert(&priv->reset);
106 ret |= reset_deassert(&priv->reset);
110 udelay(RESET_COMPLETE_TIME);
112 /* usb2_otg_aca_en == 0 */
113 regmap_update_bits(priv->regmap, PHY_CTRL_R21, BIT(2), 0);
115 /* PLL Setup : 24MHz * 20 / 1 = 480MHz */
116 regmap_write(priv->regmap, PHY_CTRL_R16, 0x39400414);
117 regmap_write(priv->regmap, PHY_CTRL_R17, 0x927e0000);
118 regmap_write(priv->regmap, PHY_CTRL_R18, 0xac5f49e5);
120 udelay(PLL_RESET_COMPLETE_TIME);
123 regmap_write(priv->regmap, PHY_CTRL_R16, 0x19400414);
126 regmap_write(priv->regmap, PHY_CTRL_R20, 0xfe18);
127 regmap_write(priv->regmap, PHY_CTRL_R4, 0x8000fff);
129 /* Tuning Disconnect Threshold */
130 regmap_write(priv->regmap, PHY_CTRL_R3, 0x34);
132 /* Analog Settings */
133 regmap_write(priv->regmap, PHY_CTRL_R14, 0);
134 regmap_write(priv->regmap, PHY_CTRL_R13, 0x78000);
139 static int phy_meson_g12a_usb2_exit(struct phy *phy)
141 struct udevice *dev = phy->dev;
142 struct phy_meson_g12a_usb2_priv *priv = dev_get_priv(dev);
145 ret = reset_assert(&priv->reset);
152 struct phy_ops meson_g12a_usb2_phy_ops = {
153 .init = phy_meson_g12a_usb2_init,
154 .exit = phy_meson_g12a_usb2_exit,
155 .power_on = phy_meson_g12a_usb2_power_on,
156 .power_off = phy_meson_g12a_usb2_power_off,
159 int meson_g12a_usb2_phy_probe(struct udevice *dev)
161 struct phy_meson_g12a_usb2_priv *priv = dev_get_priv(dev);
164 ret = regmap_init_mem(dev_ofnode(dev), &priv->regmap);
168 ret = reset_get_by_index(dev, 0, &priv->reset);
169 if (ret == -ENOTSUPP)
174 ret = reset_deassert(&priv->reset);
176 reset_release_all(&priv->reset, 1);
180 #if CONFIG_IS_ENABLED(CLK)
181 ret = clk_get_by_index(dev, 0, &priv->clk);
185 ret = clk_enable(&priv->clk);
186 if (ret && ret != -ENOSYS && ret != -ENOTSUPP) {
187 pr_err("failed to enable PHY clock\n");
188 clk_free(&priv->clk);
193 #if CONFIG_IS_ENABLED(DM_REGULATOR)
194 ret = device_get_supply_regulator(dev, "phy-supply", &priv->phy_supply);
195 if (ret && ret != -ENOENT) {
196 pr_err("Failed to get PHY regulator\n");
204 static const struct udevice_id meson_g12a_usb2_phy_ids[] = {
205 { .compatible = "amlogic,g12a-usb2-phy" },
209 U_BOOT_DRIVER(meson_g12a_usb2_phy) = {
210 .name = "meson_g12a_usb2_phy",
212 .of_match = meson_g12a_usb2_phy_ids,
213 .probe = meson_g12a_usb2_phy_probe,
214 .ops = &meson_g12a_usb2_phy_ops,
215 .priv_auto_alloc_size = sizeof(struct phy_meson_g12a_usb2_priv),