blob: 8b2432251561bd0b1bc5127ee02a7b227c56f80b [file] [log] [blame]
Neil Armstrong5fe837d2019-02-19 15:17:29 +01001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Meson G12A USB2 PHY driver
4 *
5 * Copyright (C) 2017 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>
Simon Glass0f2af882020-05-10 11:40:05 -060011#include <log.h>
Simon Glass9bc15642020-02-03 07:36:16 -070012#include <malloc.h>
Neil Armstrong5fe837d2019-02-19 15:17:29 +010013#include <asm/io.h>
14#include <bitfield.h>
15#include <dm.h>
16#include <errno.h>
17#include <generic-phy.h>
18#include <regmap.h>
Simon Glassdbd79542020-05-10 11:40:11 -060019#include <linux/delay.h>
Simon Glassbdd5f812023-09-14 18:21:46 -060020#include <linux/printk.h>
Neil Armstrong5fe837d2019-02-19 15:17:29 +010021#include <power/regulator.h>
22#include <reset.h>
23#include <clk.h>
24
25#include <linux/bitops.h>
26#include <linux/compat.h>
27
28#define PHY_CTRL_R0 0x0
29#define PHY_CTRL_R1 0x4
30#define PHY_CTRL_R2 0x8
31#define PHY_CTRL_R3 0xc
32#define PHY_CTRL_R4 0x10
33#define PHY_CTRL_R5 0x14
34#define PHY_CTRL_R6 0x18
35#define PHY_CTRL_R7 0x1c
36#define PHY_CTRL_R8 0x20
37#define PHY_CTRL_R9 0x24
38#define PHY_CTRL_R10 0x28
39#define PHY_CTRL_R11 0x2c
40#define PHY_CTRL_R12 0x30
41#define PHY_CTRL_R13 0x34
42#define PHY_CTRL_R14 0x38
43#define PHY_CTRL_R15 0x3c
44#define PHY_CTRL_R16 0x40
45#define PHY_CTRL_R17 0x44
46#define PHY_CTRL_R18 0x48
47#define PHY_CTRL_R19 0x4c
48#define PHY_CTRL_R20 0x50
49#define PHY_CTRL_R21 0x54
50#define PHY_CTRL_R22 0x58
51#define PHY_CTRL_R23 0x5c
52
53#define RESET_COMPLETE_TIME 1000
54#define PLL_RESET_COMPLETE_TIME 100
55
56struct phy_meson_g12a_usb2_priv {
57 struct regmap *regmap;
Neil Armstrong5fe837d2019-02-19 15:17:29 +010058#if CONFIG_IS_ENABLED(CLK)
59 struct clk clk;
60#endif
61 struct reset_ctl reset;
62};
63
Neil Armstrong5fe837d2019-02-19 15:17:29 +010064static int phy_meson_g12a_usb2_init(struct phy *phy)
65{
66 struct udevice *dev = phy->dev;
67 struct phy_meson_g12a_usb2_priv *priv = dev_get_priv(dev);
68 int ret;
69
70 ret = reset_assert(&priv->reset);
71 udelay(1);
72 ret |= reset_deassert(&priv->reset);
73 if (ret)
74 return ret;
75
76 udelay(RESET_COMPLETE_TIME);
77
78 /* usb2_otg_aca_en == 0 */
79 regmap_update_bits(priv->regmap, PHY_CTRL_R21, BIT(2), 0);
80
81 /* PLL Setup : 24MHz * 20 / 1 = 480MHz */
82 regmap_write(priv->regmap, PHY_CTRL_R16, 0x39400414);
83 regmap_write(priv->regmap, PHY_CTRL_R17, 0x927e0000);
84 regmap_write(priv->regmap, PHY_CTRL_R18, 0xac5f49e5);
85
86 udelay(PLL_RESET_COMPLETE_TIME);
87
88 /* UnReset PLL */
89 regmap_write(priv->regmap, PHY_CTRL_R16, 0x19400414);
90
91 /* PHY Tuning */
92 regmap_write(priv->regmap, PHY_CTRL_R20, 0xfe18);
93 regmap_write(priv->regmap, PHY_CTRL_R4, 0x8000fff);
94
95 /* Tuning Disconnect Threshold */
96 regmap_write(priv->regmap, PHY_CTRL_R3, 0x34);
97
98 /* Analog Settings */
99 regmap_write(priv->regmap, PHY_CTRL_R14, 0);
100 regmap_write(priv->regmap, PHY_CTRL_R13, 0x78000);
101
102 return 0;
103}
104
105static int phy_meson_g12a_usb2_exit(struct phy *phy)
106{
107 struct udevice *dev = phy->dev;
108 struct phy_meson_g12a_usb2_priv *priv = dev_get_priv(dev);
109 int ret;
110
111 ret = reset_assert(&priv->reset);
112 if (ret)
113 return ret;
114
115 return 0;
116}
117
118struct phy_ops meson_g12a_usb2_phy_ops = {
119 .init = phy_meson_g12a_usb2_init,
120 .exit = phy_meson_g12a_usb2_exit,
Neil Armstrong5fe837d2019-02-19 15:17:29 +0100121};
122
123int meson_g12a_usb2_phy_probe(struct udevice *dev)
124{
125 struct phy_meson_g12a_usb2_priv *priv = dev_get_priv(dev);
126 int ret;
127
128 ret = regmap_init_mem(dev_ofnode(dev), &priv->regmap);
129 if (ret)
130 return ret;
131
132 ret = reset_get_by_index(dev, 0, &priv->reset);
133 if (ret == -ENOTSUPP)
134 return 0;
135 else if (ret)
136 return ret;
137
138 ret = reset_deassert(&priv->reset);
139 if (ret) {
140 reset_release_all(&priv->reset, 1);
141 return ret;
142 }
143
144#if CONFIG_IS_ENABLED(CLK)
145 ret = clk_get_by_index(dev, 0, &priv->clk);
146 if (ret < 0)
147 return ret;
148
149 ret = clk_enable(&priv->clk);
150 if (ret && ret != -ENOSYS && ret != -ENOTSUPP) {
151 pr_err("failed to enable PHY clock\n");
152 clk_free(&priv->clk);
153 return ret;
154 }
155#endif
156
Neil Armstrong5fe837d2019-02-19 15:17:29 +0100157 return 0;
158}
159
160static const struct udevice_id meson_g12a_usb2_phy_ids[] = {
161 { .compatible = "amlogic,g12a-usb2-phy" },
162 { }
163};
164
165U_BOOT_DRIVER(meson_g12a_usb2_phy) = {
166 .name = "meson_g12a_usb2_phy",
167 .id = UCLASS_PHY,
168 .of_match = meson_g12a_usb2_phy_ids,
169 .probe = meson_g12a_usb2_phy_probe,
170 .ops = &meson_g12a_usb2_phy_ops,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700171 .priv_auto = sizeof(struct phy_meson_g12a_usb2_priv),
Neil Armstrong5fe837d2019-02-19 15:17:29 +0100172};