blob: 1e7c5f334b1ef548b687ff90fe69dd6047637bab [file] [log] [blame]
Stefan Roesed7d76062019-04-05 13:44:43 +02001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (C) 2019 Stefan Roese <sr@denx.de>
4 *
5 * Derived from linux/drivers/phy/ralink/phy-ralink-usb.c
6 * Copyright (C) 2017 John Crispin <john@phrozen.org>
7 */
8
Weijie Gao4cce5112019-09-25 17:45:31 +08009#include <clk.h>
Stefan Roesed7d76062019-04-05 13:44:43 +020010#include <common.h>
11#include <dm.h>
12#include <generic-phy.h>
Weijie Gao4cce5112019-09-25 17:45:31 +080013#include <reset.h>
Stefan Roesed7d76062019-04-05 13:44:43 +020014#include <asm/io.h>
Weijie Gao4cce5112019-09-25 17:45:31 +080015#include <linux/bitops.h>
Stefan Roesed7d76062019-04-05 13:44:43 +020016
17#define OFS_U2_PHY_AC0 0x800
Weijie Gao4cce5112019-09-25 17:45:31 +080018#define USBPLL_FBDIV_S 16
19#define USBPLL_FBDIV_M GENMASK(22, 16)
20#define BG_TRIM_S 8
21#define BG_TRIM_M GENMASK(11, 8)
22#define BG_RBSEL_S 6
23#define BG_RBSEL_M GENMASK(7, 6)
24#define BG_RASEL_S 4
25#define BG_RASEL_M GENMASK(5, 4)
26#define BGR_DIV_S 2
27#define BGR_DIV_M GENMASK(3, 2)
28#define CHP_EN BIT(1)
29
Stefan Roesed7d76062019-04-05 13:44:43 +020030#define OFS_U2_PHY_AC1 0x804
Weijie Gao4cce5112019-09-25 17:45:31 +080031#define VRT_VREF_SEL_S 28
32#define VRT_VREF_SEL_M GENMASK(30, 28)
33#define TERM_VREF_SEL_S 24
34#define TERM_VREF_SEL_M GENMASK(26, 24)
35#define USBPLL_RSVD BIT(4)
36#define USBPLL_ACCEN BIT(3)
37#define USBPLL_LF BIT(2)
38
Stefan Roesed7d76062019-04-05 13:44:43 +020039#define OFS_U2_PHY_AC2 0x808
Weijie Gao4cce5112019-09-25 17:45:31 +080040
Stefan Roesed7d76062019-04-05 13:44:43 +020041#define OFS_U2_PHY_ACR0 0x810
Weijie Gao4cce5112019-09-25 17:45:31 +080042#define HSTX_SRCAL_EN BIT(23)
43#define HSTX_SRCTRL_S 16
44#define HSTX_SRCTRL_M GENMASK(18, 16)
45
Stefan Roesed7d76062019-04-05 13:44:43 +020046#define OFS_U2_PHY_ACR3 0x81C
Weijie Gao4cce5112019-09-25 17:45:31 +080047#define HSTX_DBIST_S 28
48#define HSTX_DBIST_M GENMASK(31, 28)
49#define HSRX_BIAS_EN_SEL_S 20
50#define HSRX_BIAS_EN_SEL_M GENMASK(21, 20)
51
Stefan Roesed7d76062019-04-05 13:44:43 +020052#define OFS_U2_PHY_DCR0 0x860
Weijie Gao4cce5112019-09-25 17:45:31 +080053#define PHYD_RESERVE_S 8
54#define PHYD_RESERVE_M GENMASK(23, 8)
55#define CDR_FILT_S 0
56#define CDR_FILT_M GENMASK(3, 0)
57
Stefan Roesed7d76062019-04-05 13:44:43 +020058#define OFS_U2_PHY_DTM0 0x868
Weijie Gao4cce5112019-09-25 17:45:31 +080059#define FORCE_USB_CLKEN BIT(25)
Stefan Roesed7d76062019-04-05 13:44:43 +020060
Weijie Gao4cce5112019-09-25 17:45:31 +080061#define OFS_FM_CR0 0xf00
62#define FREQDET_EN BIT(24)
63#define CYCLECNT_S 0
64#define CYCLECNT_M GENMASK(23, 0)
Stefan Roesed7d76062019-04-05 13:44:43 +020065
Weijie Gao4cce5112019-09-25 17:45:31 +080066#define OFS_FM_MONR0 0xf0c
Stefan Roesed7d76062019-04-05 13:44:43 +020067
Weijie Gao4cce5112019-09-25 17:45:31 +080068#define OFS_FM_MONR1 0xf10
69#define FRCK_EN BIT(8)
70
71#define U2_SR_COEF_7628 32
Stefan Roesed7d76062019-04-05 13:44:43 +020072
73struct mt76x8_usb_phy {
Stefan Roesed7d76062019-04-05 13:44:43 +020074 void __iomem *base;
Weijie Gao4cce5112019-09-25 17:45:31 +080075 struct clk cg; /* for clock gating */
76 struct reset_ctl rst_phy;
Stefan Roesed7d76062019-04-05 13:44:43 +020077};
78
Weijie Gao4cce5112019-09-25 17:45:31 +080079static void phy_w32(struct mt76x8_usb_phy *phy, u32 reg, u32 val)
Stefan Roesed7d76062019-04-05 13:44:43 +020080{
81 writel(val, phy->base + reg);
82}
83
Weijie Gao4cce5112019-09-25 17:45:31 +080084static u32 phy_r32(struct mt76x8_usb_phy *phy, u32 reg)
Stefan Roesed7d76062019-04-05 13:44:43 +020085{
86 return readl(phy->base + reg);
87}
88
Weijie Gao4cce5112019-09-25 17:45:31 +080089static void phy_rmw32(struct mt76x8_usb_phy *phy, u32 reg, u32 clr, u32 set)
90{
91 clrsetbits_32(phy->base + reg, clr, set);
92}
93
Stefan Roesed7d76062019-04-05 13:44:43 +020094static void mt76x8_usb_phy_init(struct mt76x8_usb_phy *phy)
95{
Weijie Gao4cce5112019-09-25 17:45:31 +080096 phy_r32(phy, OFS_U2_PHY_AC2);
97 phy_r32(phy, OFS_U2_PHY_ACR0);
98 phy_r32(phy, OFS_U2_PHY_DCR0);
Stefan Roesed7d76062019-04-05 13:44:43 +020099
Weijie Gao4cce5112019-09-25 17:45:31 +0800100 phy_w32(phy, OFS_U2_PHY_DCR0,
101 (0xffff << PHYD_RESERVE_S) | (2 << CDR_FILT_S));
102 phy_r32(phy, OFS_U2_PHY_DCR0);
103
104 phy_w32(phy, OFS_U2_PHY_DCR0,
105 (0x5555 << PHYD_RESERVE_S) | (2 << CDR_FILT_S));
106 phy_r32(phy, OFS_U2_PHY_DCR0);
107
108 phy_w32(phy, OFS_U2_PHY_DCR0,
109 (0xaaaa << PHYD_RESERVE_S) | (2 << CDR_FILT_S));
110 phy_r32(phy, OFS_U2_PHY_DCR0);
111
112 phy_w32(phy, OFS_U2_PHY_DCR0,
113 (4 << PHYD_RESERVE_S) | (2 << CDR_FILT_S));
114 phy_r32(phy, OFS_U2_PHY_DCR0);
115
116 phy_w32(phy, OFS_U2_PHY_AC0,
117 (0x48 << USBPLL_FBDIV_S) | (8 << BG_TRIM_S) |
118 (1 << BG_RBSEL_S) | (2 << BG_RASEL_S) | (2 << BGR_DIV_S) |
119 CHP_EN);
120
121 phy_w32(phy, OFS_U2_PHY_AC1,
122 (4 << VRT_VREF_SEL_S) | (4 << TERM_VREF_SEL_S) | USBPLL_RSVD |
123 USBPLL_ACCEN | USBPLL_LF);
124
125 phy_w32(phy, OFS_U2_PHY_ACR3,
126 (12 << HSTX_DBIST_S) | (2 << HSRX_BIAS_EN_SEL_S));
127
128 phy_w32(phy, OFS_U2_PHY_DTM0, FORCE_USB_CLKEN);
129}
130
131static void mt76x8_usb_phy_sr_calibrate(struct mt76x8_usb_phy *phy)
132{
133 u32 fmout, tmp = 4;
134 int i;
135
136 /* Enable HS TX SR calibration */
137 phy_rmw32(phy, OFS_U2_PHY_ACR0, 0, HSTX_SRCAL_EN);
138 mdelay(1);
139
140 /* Enable free run clock */
141 phy_rmw32(phy, OFS_FM_MONR1, 0, FRCK_EN);
142
143 /* Set cycle count = 0x400 */
144 phy_rmw32(phy, OFS_FM_CR0, CYCLECNT_M, 0x400 << CYCLECNT_S);
145
146 /* Enable frequency meter */
147 phy_rmw32(phy, OFS_FM_CR0, 0, FREQDET_EN);
148
149 /* Wait for FM detection done, set timeout to 10ms */
150 for (i = 0; i < 10; i++) {
151 fmout = phy_r32(phy, OFS_FM_MONR0);
152
153 if (fmout)
154 break;
155
156 mdelay(1);
157 }
158
159 /* Disable frequency meter */
160 phy_rmw32(phy, OFS_FM_CR0, FREQDET_EN, 0);
161
162 /* Disable free run clock */
163 phy_rmw32(phy, OFS_FM_MONR1, FRCK_EN, 0);
164
165 /* Disable HS TX SR calibration */
166 phy_rmw32(phy, OFS_U2_PHY_ACR0, HSTX_SRCAL_EN, 0);
167 mdelay(1);
168
169 if (fmout) {
170 /*
171 * set reg = (1024 / FM_OUT) * 25 * 0.028
172 * (round to the nearest digits)
173 */
174 tmp = (((1024 * 25 * U2_SR_COEF_7628) / fmout) + 500) / 1000;
175 }
176
177 phy_rmw32(phy, OFS_U2_PHY_ACR0, HSTX_SRCTRL_M,
178 (tmp << HSTX_SRCTRL_S) & HSTX_SRCTRL_M);
Stefan Roesed7d76062019-04-05 13:44:43 +0200179}
180
181static int mt76x8_usb_phy_power_on(struct phy *_phy)
182{
183 struct mt76x8_usb_phy *phy = dev_get_priv(_phy->dev);
Stefan Roesed7d76062019-04-05 13:44:43 +0200184
Weijie Gao4cce5112019-09-25 17:45:31 +0800185 clk_enable(&phy->cg);
Stefan Roesed7d76062019-04-05 13:44:43 +0200186
Weijie Gao4cce5112019-09-25 17:45:31 +0800187 reset_deassert(&phy->rst_phy);
Stefan Roesed7d76062019-04-05 13:44:43 +0200188
189 /*
190 * The SDK kernel had a delay of 100ms. however on device
191 * testing showed that 10ms is enough
192 */
193 mdelay(10);
194
Weijie Gao4cce5112019-09-25 17:45:31 +0800195 mt76x8_usb_phy_init(phy);
196 mt76x8_usb_phy_sr_calibrate(phy);
Stefan Roesed7d76062019-04-05 13:44:43 +0200197
198 return 0;
199}
200
201static int mt76x8_usb_phy_power_off(struct phy *_phy)
202{
203 struct mt76x8_usb_phy *phy = dev_get_priv(_phy->dev);
204
Weijie Gao4cce5112019-09-25 17:45:31 +0800205 clk_disable(&phy->cg);
206
207 reset_assert(&phy->rst_phy);
Stefan Roesed7d76062019-04-05 13:44:43 +0200208
209 return 0;
210}
211
212static int mt76x8_usb_phy_probe(struct udevice *dev)
213{
214 struct mt76x8_usb_phy *phy = dev_get_priv(dev);
Weijie Gao4cce5112019-09-25 17:45:31 +0800215 int ret;
Stefan Roesed7d76062019-04-05 13:44:43 +0200216
217 phy->base = dev_read_addr_ptr(dev);
218 if (!phy->base)
219 return -EINVAL;
220
Weijie Gao4cce5112019-09-25 17:45:31 +0800221 /* clock gate */
222 ret = clk_get_by_name(dev, "cg", &phy->cg);
223 if (ret)
224 return ret;
225
226 ret = reset_get_by_name(dev, "phy", &phy->rst_phy);
227 if (ret)
228 return ret;
229
Stefan Roesed7d76062019-04-05 13:44:43 +0200230 return 0;
231}
232
233static struct phy_ops mt76x8_usb_phy_ops = {
234 .power_on = mt76x8_usb_phy_power_on,
235 .power_off = mt76x8_usb_phy_power_off,
236};
237
238static const struct udevice_id mt76x8_usb_phy_ids[] = {
239 { .compatible = "mediatek,mt7628-usbphy" },
240 { }
241};
242
243U_BOOT_DRIVER(mt76x8_usb_phy) = {
244 .name = "mt76x8_usb_phy",
245 .id = UCLASS_PHY,
246 .of_match = mt76x8_usb_phy_ids,
247 .ops = &mt76x8_usb_phy_ops,
248 .probe = mt76x8_usb_phy_probe,
249 .priv_auto_alloc_size = sizeof(struct mt76x8_usb_phy),
250};