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>
17 #include <generic-phy.h>
19 #include <linux/delay.h>
20 #include <power/regulator.h>
24 #include <linux/bitops.h>
25 #include <linux/compat.h>
27 #define PHY_CTRL_R0 0x0
28 #define PHY_CTRL_R1 0x4
29 #define PHY_CTRL_R2 0x8
30 #define PHY_CTRL_R3 0xc
31 #define PHY_CTRL_R4 0x10
32 #define PHY_CTRL_R5 0x14
33 #define PHY_CTRL_R6 0x18
34 #define PHY_CTRL_R7 0x1c
35 #define PHY_CTRL_R8 0x20
36 #define PHY_CTRL_R9 0x24
37 #define PHY_CTRL_R10 0x28
38 #define PHY_CTRL_R11 0x2c
39 #define PHY_CTRL_R12 0x30
40 #define PHY_CTRL_R13 0x34
41 #define PHY_CTRL_R14 0x38
42 #define PHY_CTRL_R15 0x3c
43 #define PHY_CTRL_R16 0x40
44 #define PHY_CTRL_R17 0x44
45 #define PHY_CTRL_R18 0x48
46 #define PHY_CTRL_R19 0x4c
47 #define PHY_CTRL_R20 0x50
48 #define PHY_CTRL_R21 0x54
49 #define PHY_CTRL_R22 0x58
50 #define PHY_CTRL_R23 0x5c
52 #define RESET_COMPLETE_TIME 1000
53 #define PLL_RESET_COMPLETE_TIME 100
55 struct phy_meson_g12a_usb2_priv {
56 struct regmap *regmap;
57 #if CONFIG_IS_ENABLED(DM_REGULATOR)
58 struct udevice *phy_supply;
60 #if CONFIG_IS_ENABLED(CLK)
63 struct reset_ctl reset;
67 static int phy_meson_g12a_usb2_power_on(struct phy *phy)
69 #if CONFIG_IS_ENABLED(DM_REGULATOR)
70 struct udevice *dev = phy->dev;
71 struct phy_meson_g12a_usb2_priv *priv = dev_get_priv(dev);
73 if (priv->phy_supply) {
74 int ret = regulator_set_enable(priv->phy_supply, true);
83 static int phy_meson_g12a_usb2_power_off(struct phy *phy)
85 #if CONFIG_IS_ENABLED(DM_REGULATOR)
86 struct udevice *dev = phy->dev;
87 struct phy_meson_g12a_usb2_priv *priv = dev_get_priv(dev);
89 if (priv->phy_supply) {
90 int ret = regulator_set_enable(priv->phy_supply, false);
92 pr_err("Error disabling PHY supply\n");
101 static int phy_meson_g12a_usb2_init(struct phy *phy)
103 struct udevice *dev = phy->dev;
104 struct phy_meson_g12a_usb2_priv *priv = dev_get_priv(dev);
107 ret = reset_assert(&priv->reset);
109 ret |= reset_deassert(&priv->reset);
113 udelay(RESET_COMPLETE_TIME);
115 /* usb2_otg_aca_en == 0 */
116 regmap_update_bits(priv->regmap, PHY_CTRL_R21, BIT(2), 0);
118 /* PLL Setup : 24MHz * 20 / 1 = 480MHz */
119 regmap_write(priv->regmap, PHY_CTRL_R16, 0x39400414);
120 regmap_write(priv->regmap, PHY_CTRL_R17, 0x927e0000);
121 regmap_write(priv->regmap, PHY_CTRL_R18, 0xac5f49e5);
123 udelay(PLL_RESET_COMPLETE_TIME);
126 regmap_write(priv->regmap, PHY_CTRL_R16, 0x19400414);
129 regmap_write(priv->regmap, PHY_CTRL_R20, 0xfe18);
130 regmap_write(priv->regmap, PHY_CTRL_R4, 0x8000fff);
132 /* Tuning Disconnect Threshold */
133 regmap_write(priv->regmap, PHY_CTRL_R3, 0x34);
135 /* Analog Settings */
136 regmap_write(priv->regmap, PHY_CTRL_R14, 0);
137 regmap_write(priv->regmap, PHY_CTRL_R13, 0x78000);
142 static int phy_meson_g12a_usb2_exit(struct phy *phy)
144 struct udevice *dev = phy->dev;
145 struct phy_meson_g12a_usb2_priv *priv = dev_get_priv(dev);
148 ret = reset_assert(&priv->reset);
155 struct phy_ops meson_g12a_usb2_phy_ops = {
156 .init = phy_meson_g12a_usb2_init,
157 .exit = phy_meson_g12a_usb2_exit,
158 .power_on = phy_meson_g12a_usb2_power_on,
159 .power_off = phy_meson_g12a_usb2_power_off,
162 int meson_g12a_usb2_phy_probe(struct udevice *dev)
164 struct phy_meson_g12a_usb2_priv *priv = dev_get_priv(dev);
167 ret = regmap_init_mem(dev_ofnode(dev), &priv->regmap);
171 ret = reset_get_by_index(dev, 0, &priv->reset);
172 if (ret == -ENOTSUPP)
177 ret = reset_deassert(&priv->reset);
179 reset_release_all(&priv->reset, 1);
183 #if CONFIG_IS_ENABLED(CLK)
184 ret = clk_get_by_index(dev, 0, &priv->clk);
188 ret = clk_enable(&priv->clk);
189 if (ret && ret != -ENOSYS && ret != -ENOTSUPP) {
190 pr_err("failed to enable PHY clock\n");
191 clk_free(&priv->clk);
196 #if CONFIG_IS_ENABLED(DM_REGULATOR)
197 ret = device_get_supply_regulator(dev, "phy-supply", &priv->phy_supply);
198 if (ret && ret != -ENOENT) {
199 pr_err("Failed to get PHY regulator\n");
207 static const struct udevice_id meson_g12a_usb2_phy_ids[] = {
208 { .compatible = "amlogic,g12a-usb2-phy" },
212 U_BOOT_DRIVER(meson_g12a_usb2_phy) = {
213 .name = "meson_g12a_usb2_phy",
215 .of_match = meson_g12a_usb2_phy_ids,
216 .probe = meson_g12a_usb2_phy_probe,
217 .ops = &meson_g12a_usb2_phy_ops,
218 .priv_auto = sizeof(struct phy_meson_g12a_usb2_priv),