1*7a099760SInochi Amaoto // SPDX-License-Identifier: GPL-2.0-only
2*7a099760SInochi Amaoto
3*7a099760SInochi Amaoto #include <linux/bitfield.h>
4*7a099760SInochi Amaoto #include <linux/cleanup.h>
5*7a099760SInochi Amaoto #include <linux/io.h>
6*7a099760SInochi Amaoto #include <linux/iopoll.h>
7*7a099760SInochi Amaoto #include <linux/module.h>
8*7a099760SInochi Amaoto #include <linux/regmap.h>
9*7a099760SInochi Amaoto #include <linux/usb.h>
10*7a099760SInochi Amaoto
11*7a099760SInochi Amaoto #include <dt-bindings/phy/phy.h>
12*7a099760SInochi Amaoto
13*7a099760SInochi Amaoto #include "phy-k3-common.h"
14*7a099760SInochi Amaoto
15*7a099760SInochi Amaoto /* PHY Registers */
16*7a099760SInochi Amaoto #define PHY_VERSION 0x0
17*7a099760SInochi Amaoto
18*7a099760SInochi Amaoto #define PHY_RESET_CFG 0x04
19*7a099760SInochi Amaoto
20*7a099760SInochi Amaoto #define PHY_RESET_RXBUF_RST BIT(0)
21*7a099760SInochi Amaoto #define PHY_RESET_SOFT_RST_PCS BIT(1)
22*7a099760SInochi Amaoto #define PHY_RESET_SOFT_RST_AHB BIT(2)
23*7a099760SInochi Amaoto #define PHY_RESET_EN_SD_AFTER_LOCK BIT(6)
24*7a099760SInochi Amaoto
25*7a099760SInochi Amaoto #define PHY_CLK_CFG 0x08
26*7a099760SInochi Amaoto
27*7a099760SInochi Amaoto #define PHY_CLK_PLL_READY BIT(0)
28*7a099760SInochi Amaoto #define PHY_CLK_TXCLK_INV BIT(2)
29*7a099760SInochi Amaoto #define PHY_CLK_RXCLK_EN BIT(3)
30*7a099760SInochi Amaoto #define PHY_CLK_TXCLK_EN BIT(4)
31*7a099760SInochi Amaoto #define PHY_CLK_PCLK_EN BIT(5)
32*7a099760SInochi Amaoto #define PHY_CLK_PIPE_PCLK_EN BIT(6)
33*7a099760SInochi Amaoto #define PHY_CLK_REFCLK_FREQ GENMASK(10, 7)
34*7a099760SInochi Amaoto #define PHY_CLK_REFCLK_24M 2
35*7a099760SInochi Amaoto #define PHY_CLK_SW_INIT_DONE BIT(11)
36*7a099760SInochi Amaoto #define PHY_CLK_PU_SSC_OUT BIT(23)
37*7a099760SInochi Amaoto
38*7a099760SInochi Amaoto #define PHY_MODE_CFG 0x0C
39*7a099760SInochi Amaoto
40*7a099760SInochi Amaoto #define PHY_MODE_PCIE_INT_EN BIT(0)
41*7a099760SInochi Amaoto #define PHY_MODE_LFPS_TPERIOD GENMASK(9, 8)
42*7a099760SInochi Amaoto #define PHY_MODE_LFPS_TPERIOD_USB 3
43*7a099760SInochi Amaoto
44*7a099760SInochi Amaoto #define PHY_PU_SEL 0x40
45*7a099760SInochi Amaoto
46*7a099760SInochi Amaoto #define PHY_PU_CFG_STATUS BIT(9)
47*7a099760SInochi Amaoto #define PHY_PU_OVRD_STATUS BIT(10)
48*7a099760SInochi Amaoto
49*7a099760SInochi Amaoto #define PHY_PU_CK_REG 0x54
50*7a099760SInochi Amaoto
51*7a099760SInochi Amaoto #define PHY_PU_REFCLK_100 BIT(25)
52*7a099760SInochi Amaoto
53*7a099760SInochi Amaoto #define PHY_PLL_REG1 0x58
54*7a099760SInochi Amaoto
55*7a099760SInochi Amaoto #define PHY_PLL_FREF_SEL GENMASK(15, 13)
56*7a099760SInochi Amaoto #define PHY_PLL_FREF_24M 0x1
57*7a099760SInochi Amaoto #define PHY_PLL_SSC_DEP_SEL GENMASK(27, 24)
58*7a099760SInochi Amaoto #define PHY_PLL_SSC_5000PPM 0xa
59*7a099760SInochi Amaoto #define PHY_PLL_SSC_MODE GENMASK(29, 28)
60*7a099760SInochi Amaoto #define PHY_PLL_SSC_MODE_CENTER_SPREAD 0
61*7a099760SInochi Amaoto #define PHY_PLL_SSC_MODE_UP_SPREAD 1
62*7a099760SInochi Amaoto #define PHY_PLL_SSC_MODE_DOWN_SPREAD 2
63*7a099760SInochi Amaoto #define PHY_PLL_SSC_MODE_DOWN_SPREAD1 3
64*7a099760SInochi Amaoto
65*7a099760SInochi Amaoto #define PHY_PLL_REG2 0x5c
66*7a099760SInochi Amaoto
67*7a099760SInochi Amaoto #define PHY_PLL_SEL_REF100 BIT(21)
68*7a099760SInochi Amaoto
69*7a099760SInochi Amaoto /* PHY RX Register Definitions */
70*7a099760SInochi Amaoto #define PHY_RX_REG_A 0x60
71*7a099760SInochi Amaoto
72*7a099760SInochi Amaoto #define PHY_RX_REG0_MASK GENMASK(7, 0)
73*7a099760SInochi Amaoto #define PHY_RX_REG1_MASK GENMASK(15, 8)
74*7a099760SInochi Amaoto #define PHY_RX_REG2_MASK GENMASK(23, 16)
75*7a099760SInochi Amaoto #define PHY_RX_REG3_MASK GENMASK(31, 24)
76*7a099760SInochi Amaoto
77*7a099760SInochi Amaoto #define PHY_RX_REG0_RLOAD BIT(4)
78*7a099760SInochi Amaoto #define PHY_RX_REG1_RTERM GENMASK(11, 8)
79*7a099760SInochi Amaoto #define PHY_RX_REG1_RC_CALI GENMASK(15, 12)
80*7a099760SInochi Amaoto #define PHY_RX_REG2_CSEL GENMASK(19, 16)
81*7a099760SInochi Amaoto #define PHY_RX_REG2_FORCE_CSEL BIT(20)
82*7a099760SInochi Amaoto #define PHY_RX_REG2_PSEL GENMASK(23, 21)
83*7a099760SInochi Amaoto #define PHY_RX_REG3_I_LOAD GENMASK(26, 24)
84*7a099760SInochi Amaoto #define PHY_RX_REG3_SEL_CBOOST_CODE BIT(27)
85*7a099760SInochi Amaoto #define PHY_RX_REG3_ADJ_BIAS GENMASK(29, 28)
86*7a099760SInochi Amaoto #define PHY_RX_REG3_RDEG1 GENMASK(31, 30)
87*7a099760SInochi Amaoto
88*7a099760SInochi Amaoto #define PHY_RX_REG_B 0x64
89*7a099760SInochi Amaoto
90*7a099760SInochi Amaoto #define PHY_RX_REG4_MASK GENMASK(7, 0)
91*7a099760SInochi Amaoto #define PHY_RX_REG5_MASK GENMASK(15, 8)
92*7a099760SInochi Amaoto #define PHY_RX_REG6_MASK GENMASK(23, 16)
93*7a099760SInochi Amaoto
94*7a099760SInochi Amaoto #define PHY_RX_REGB_MASK GENMASK(23, 0)
95*7a099760SInochi Amaoto
96*7a099760SInochi Amaoto #define PHY_RX_REG4_RDEG2 GENMASK(2, 1)
97*7a099760SInochi Amaoto #define PHY_RX_REG4_ENVOS BIT(4)
98*7a099760SInochi Amaoto #define PHY_RX_REG4_RTERM_SEL BIT(5)
99*7a099760SInochi Amaoto #define PHY_RX_REG4_MANUAL_CFG BIT(7)
100*7a099760SInochi Amaoto #define PHY_RX_REG5_RCELL_VCM GENMASK(11, 8)
101*7a099760SInochi Amaoto #define PHY_RX_REG5_RCELL_BIAS GENMASK(15, 12)
102*7a099760SInochi Amaoto #define PHY_RX_REG6_H1_REG GENMASK(19, 16)
103*7a099760SInochi Amaoto #define PHY_RX_REG6_ADAPT_GAIN GENMASK(21, 20)
104*7a099760SInochi Amaoto #define PHY_RX_REG6_BYPASS_ADPT BIT(22)
105*7a099760SInochi Amaoto
106*7a099760SInochi Amaoto #define PHY_ADPT_CFG0 0x140
107*7a099760SInochi Amaoto #define PHY_ADPT_AFE_RST_OVRD_EN BIT(1)
108*7a099760SInochi Amaoto #define PHY_ADPT_AFE_RST_OVRD_VAL BIT(4)
109*7a099760SInochi Amaoto
110*7a099760SInochi Amaoto #define PHY_RXEQ_TIME 0xb4
111*7a099760SInochi Amaoto #define PHY_RXEQ_TIME_OVRD_POST_C_SOC BIT(21)
112*7a099760SInochi Amaoto #define PHY_RXEQ_TIME_CFG_AMP_SOC GENMASK(23, 22)
113*7a099760SInochi Amaoto #define PHY_RXEQ_TIME_AMP_SOC_650M 0
114*7a099760SInochi Amaoto #define PHY_RXEQ_TIME_AMP_SOC_800M 1
115*7a099760SInochi Amaoto #define PHY_RXEQ_TIME_AMP_SOC_870M 2
116*7a099760SInochi Amaoto #define PHY_RXEQ_TIME_AMP_SOC_900M 3
117*7a099760SInochi Amaoto #define PHY_RXEQ_TIME_OVRD_AMP_SOC BIT(24)
118*7a099760SInochi Amaoto
119*7a099760SInochi Amaoto #define PCIE_PU_ADDR_CLK_CFG 0x0008
120*7a099760SInochi Amaoto #define PHY_CLK_PLL_READY BIT(0)
121*7a099760SInochi Amaoto #define PCIE_INITAL_TIMER GENMASK(6, 3)
122*7a099760SInochi Amaoto #define CFG_INTERNAL_TIMER_ADJ GENMASK(10, 7)
123*7a099760SInochi Amaoto #define CFG_SW_PHY_INIT_DONE BIT(11)
124*7a099760SInochi Amaoto
125*7a099760SInochi Amaoto /* Lane RX/TX configuration (per‑lane, at lane_base) */
126*7a099760SInochi Amaoto #define PCIE_RX_REG1 0x050
127*7a099760SInochi Amaoto #define PCIE_RX_REFCLK_MODE GENMASK(1, 0)
128*7a099760SInochi Amaoto #define PCIE_RX_REFCLK_MODE_DRIVER 1
129*7a099760SInochi Amaoto #define PCIE_RX_SEL_TRI_CODE BIT(2)
130*7a099760SInochi Amaoto #define PCIE_RX_LEGACY GENMASK(15, 8)
131*7a099760SInochi Amaoto #define PCIE_RX_LEGACY_DEFAULT 0x65
132*7a099760SInochi Amaoto
133*7a099760SInochi Amaoto #define PCIE_TX_REG1 0x064
134*7a099760SInochi Amaoto
135*7a099760SInochi Amaoto #define PCIE_PLL_TIMEOUT 500000
136*7a099760SInochi Amaoto #define PCIE_POLL_DELAY 500
137*7a099760SInochi Amaoto
k3_usb3phy_init_single(struct k3_lane_group * lg,void __iomem * base)138*7a099760SInochi Amaoto static int k3_usb3phy_init_single(struct k3_lane_group *lg, void __iomem *base)
139*7a099760SInochi Amaoto {
140*7a099760SInochi Amaoto struct phy *phy = lg->phy;
141*7a099760SInochi Amaoto u32 val, tmp;
142*7a099760SInochi Amaoto int ret;
143*7a099760SInochi Amaoto
144*7a099760SInochi Amaoto /* Do not wait CDR lock before sampling data */
145*7a099760SInochi Amaoto val = readl(base + PHY_RESET_CFG);
146*7a099760SInochi Amaoto val = u32_replace_bits(val, 0, PHY_RESET_EN_SD_AFTER_LOCK);
147*7a099760SInochi Amaoto writel(val, base + PHY_RESET_CFG);
148*7a099760SInochi Amaoto
149*7a099760SInochi Amaoto /* Power down 100MHz refclk buffer */
150*7a099760SInochi Amaoto val = readl(base + PHY_PU_CK_REG);
151*7a099760SInochi Amaoto val = u32_replace_bits(val, 0, PHY_PU_REFCLK_100);
152*7a099760SInochi Amaoto writel(val, base + PHY_PU_CK_REG);
153*7a099760SInochi Amaoto
154*7a099760SInochi Amaoto /* Program PLL REG1 configure the SSC */
155*7a099760SInochi Amaoto val = FIELD_PREP(PHY_PLL_SSC_MODE, PHY_PLL_SSC_MODE_DOWN_SPREAD1) |
156*7a099760SInochi Amaoto FIELD_PREP(PHY_PLL_SSC_DEP_SEL, PHY_PLL_SSC_5000PPM) |
157*7a099760SInochi Amaoto FIELD_PREP(PHY_PLL_FREF_SEL, PHY_PLL_FREF_24M);
158*7a099760SInochi Amaoto writel(val, base + PHY_PLL_REG1);
159*7a099760SInochi Amaoto
160*7a099760SInochi Amaoto /* Un-select 100MHz PLL reference */
161*7a099760SInochi Amaoto val = readl(base + PHY_PLL_REG2);
162*7a099760SInochi Amaoto val = u32_replace_bits(val, 0, PHY_PLL_SEL_REF100);
163*7a099760SInochi Amaoto writel(val, base + PHY_PLL_REG2);
164*7a099760SInochi Amaoto
165*7a099760SInochi Amaoto /* USB LFPS period configuration */
166*7a099760SInochi Amaoto val = readl(base + PHY_MODE_CFG);
167*7a099760SInochi Amaoto val = u32_replace_bits(val, PHY_MODE_LFPS_TPERIOD_USB, PHY_MODE_LFPS_TPERIOD);
168*7a099760SInochi Amaoto writel(val, base + PHY_MODE_CFG);
169*7a099760SInochi Amaoto
170*7a099760SInochi Amaoto /* Force AFE adaptation reset */
171*7a099760SInochi Amaoto val = readl(base + PHY_ADPT_CFG0);
172*7a099760SInochi Amaoto val |= PHY_ADPT_AFE_RST_OVRD_EN | PHY_ADPT_AFE_RST_OVRD_VAL;
173*7a099760SInochi Amaoto writel(val, base + PHY_ADPT_CFG0);
174*7a099760SInochi Amaoto
175*7a099760SInochi Amaoto /* Override driver amplitude value to 900m */
176*7a099760SInochi Amaoto val = readl(base + PHY_RXEQ_TIME);
177*7a099760SInochi Amaoto val |= PHY_RXEQ_TIME_OVRD_AMP_SOC;
178*7a099760SInochi Amaoto val = u32_replace_bits(val, PHY_RXEQ_TIME_AMP_SOC_900M, PHY_RXEQ_TIME_CFG_AMP_SOC);
179*7a099760SInochi Amaoto writel(val, base + PHY_RXEQ_TIME);
180*7a099760SInochi Amaoto
181*7a099760SInochi Amaoto /* Configure RX parameters */
182*7a099760SInochi Amaoto val = PHY_RX_REG0_RLOAD |
183*7a099760SInochi Amaoto FIELD_PREP(PHY_RX_REG1_RTERM, 0x8) |
184*7a099760SInochi Amaoto FIELD_PREP(PHY_RX_REG1_RC_CALI, 0x7) |
185*7a099760SInochi Amaoto FIELD_PREP(PHY_RX_REG2_CSEL, 0x8) |
186*7a099760SInochi Amaoto PHY_RX_REG2_FORCE_CSEL |
187*7a099760SInochi Amaoto FIELD_PREP(PHY_RX_REG2_PSEL, 0x4) |
188*7a099760SInochi Amaoto FIELD_PREP(PHY_RX_REG3_I_LOAD, 0x7) |
189*7a099760SInochi Amaoto PHY_RX_REG3_SEL_CBOOST_CODE |
190*7a099760SInochi Amaoto FIELD_PREP(PHY_RX_REG3_ADJ_BIAS, 0x1) |
191*7a099760SInochi Amaoto FIELD_PREP(PHY_RX_REG3_RDEG1, 0x3);
192*7a099760SInochi Amaoto writel(val, base + PHY_RX_REG_A);
193*7a099760SInochi Amaoto
194*7a099760SInochi Amaoto val = readl(base + PHY_RX_REG_B);
195*7a099760SInochi Amaoto tmp = FIELD_PREP(PHY_RX_REG4_RDEG2, 0x2) |
196*7a099760SInochi Amaoto PHY_RX_REG4_ENVOS | PHY_RX_REG4_RTERM_SEL | PHY_RX_REG4_MANUAL_CFG |
197*7a099760SInochi Amaoto FIELD_PREP(PHY_RX_REG5_RCELL_VCM, 0x8) |
198*7a099760SInochi Amaoto FIELD_PREP(PHY_RX_REG5_RCELL_BIAS, 0x8) |
199*7a099760SInochi Amaoto FIELD_PREP(PHY_RX_REG6_H1_REG, 0x8) |
200*7a099760SInochi Amaoto FIELD_PREP(PHY_RX_REG6_ADAPT_GAIN, 0x2);
201*7a099760SInochi Amaoto val = u32_replace_bits(val, tmp, PHY_RX_REGB_MASK);
202*7a099760SInochi Amaoto writel(val, base + PHY_RX_REG_B);
203*7a099760SInochi Amaoto
204*7a099760SInochi Amaoto /*
205*7a099760SInochi Amaoto * Inform PHY that all PLL-related configuration is done.
206*7a099760SInochi Amaoto * PLL will not start locking until PHY_CLK_SW_INIT_DONE is set.
207*7a099760SInochi Amaoto */
208*7a099760SInochi Amaoto val = PHY_CLK_SW_INIT_DONE | PHY_CLK_PU_SSC_OUT |
209*7a099760SInochi Amaoto FIELD_PREP(PHY_CLK_REFCLK_FREQ, PHY_CLK_REFCLK_24M) |
210*7a099760SInochi Amaoto PHY_CLK_RXCLK_EN | PHY_CLK_TXCLK_EN |
211*7a099760SInochi Amaoto PHY_CLK_PCLK_EN | PHY_CLK_PIPE_PCLK_EN;
212*7a099760SInochi Amaoto writel(val, base + PHY_CLK_CFG);
213*7a099760SInochi Amaoto
214*7a099760SInochi Amaoto ret = readl_poll_timeout(base + PHY_CLK_CFG, val,
215*7a099760SInochi Amaoto (val & PHY_CLK_PLL_READY),
216*7a099760SInochi Amaoto PCIE_POLL_DELAY, PCIE_PLL_TIMEOUT);
217*7a099760SInochi Amaoto if (ret) {
218*7a099760SInochi Amaoto dev_err(&phy->dev, "PHY PLL polling timeout\n");
219*7a099760SInochi Amaoto return ret;
220*7a099760SInochi Amaoto }
221*7a099760SInochi Amaoto
222*7a099760SInochi Amaoto return 0;
223*7a099760SInochi Amaoto }
224*7a099760SInochi Amaoto
k3_usb3phy_init(struct phy * phy)225*7a099760SInochi Amaoto static int k3_usb3phy_init(struct phy *phy)
226*7a099760SInochi Amaoto {
227*7a099760SInochi Amaoto struct k3_lane_group *lg = phy_get_drvdata(phy);
228*7a099760SInochi Amaoto int ret, i;
229*7a099760SInochi Amaoto
230*7a099760SInochi Amaoto for (i = 0; i < lg->data->lanes; i++) {
231*7a099760SInochi Amaoto ret = k3_usb3phy_init_single(lg, lg->base + lg->data->offsets[i]);
232*7a099760SInochi Amaoto if (ret < 0)
233*7a099760SInochi Amaoto return ret;
234*7a099760SInochi Amaoto }
235*7a099760SInochi Amaoto
236*7a099760SInochi Amaoto return 0;
237*7a099760SInochi Amaoto }
238*7a099760SInochi Amaoto
239*7a099760SInochi Amaoto const struct phy_ops k3_usb3_phy_ops = {
240*7a099760SInochi Amaoto .init = k3_usb3phy_init,
241*7a099760SInochi Amaoto .owner = THIS_MODULE,
242*7a099760SInochi Amaoto };
243*7a099760SInochi Amaoto EXPORT_SYMBOL_GPL(k3_usb3_phy_ops);
244*7a099760SInochi Amaoto
k3_pcie_phy_init(struct phy * phy)245*7a099760SInochi Amaoto static int k3_pcie_phy_init(struct phy *phy)
246*7a099760SInochi Amaoto {
247*7a099760SInochi Amaoto struct k3_lane_group *lg = phy_get_drvdata(phy);
248*7a099760SInochi Amaoto void __iomem *phy_base = lg->base + lg->data->offsets[0];
249*7a099760SInochi Amaoto u32 val;
250*7a099760SInochi Amaoto int ret;
251*7a099760SInochi Amaoto int i;
252*7a099760SInochi Amaoto
253*7a099760SInochi Amaoto val = readl(phy_base + PHY_PLL_REG1);
254*7a099760SInochi Amaoto val = u32_replace_bits(val, 0x2, GENMASK(15, 12));
255*7a099760SInochi Amaoto writel(val, phy_base + PHY_PLL_REG1);
256*7a099760SInochi Amaoto
257*7a099760SInochi Amaoto val = readl(phy_base + PHY_PLL_REG2);
258*7a099760SInochi Amaoto val = u32_replace_bits(val, 0, BIT(21));
259*7a099760SInochi Amaoto writel(val, phy_base + PHY_PLL_REG2);
260*7a099760SInochi Amaoto
261*7a099760SInochi Amaoto for (i = 0; i < lg->data->lanes; i++) {
262*7a099760SInochi Amaoto void __iomem *lane_base = lg->base + lg->data->offsets[i];
263*7a099760SInochi Amaoto
264*7a099760SInochi Amaoto val = readl(lane_base + PCIE_RX_REG1);
265*7a099760SInochi Amaoto val = u32_replace_bits(val, 0, 0x3);
266*7a099760SInochi Amaoto writel(val, lane_base + PCIE_RX_REG1);
267*7a099760SInochi Amaoto }
268*7a099760SInochi Amaoto
269*7a099760SInochi Amaoto val = readl(phy_base + PHY_PLL_REG2);
270*7a099760SInochi Amaoto val |= BIT(20);
271*7a099760SInochi Amaoto writel(val, phy_base + PHY_PLL_REG2);
272*7a099760SInochi Amaoto
273*7a099760SInochi Amaoto /* The write is needed as clock requires renegotiation */
274*7a099760SInochi Amaoto val = FIELD_PREP(PCIE_RX_REFCLK_MODE, PCIE_RX_REFCLK_MODE_DRIVER) |
275*7a099760SInochi Amaoto PCIE_RX_SEL_TRI_CODE |
276*7a099760SInochi Amaoto FIELD_PREP(PCIE_RX_LEGACY, PCIE_RX_LEGACY_DEFAULT);
277*7a099760SInochi Amaoto writel(val, phy_base + PCIE_RX_REG1);
278*7a099760SInochi Amaoto
279*7a099760SInochi Amaoto /* pll_reg1 of lane0, disable SSC: pll[27:24] = 0 */
280*7a099760SInochi Amaoto val = readl(phy_base + PHY_PLL_REG1);
281*7a099760SInochi Amaoto val = u32_replace_bits(val, 0, GENMASK(27, 24));
282*7a099760SInochi Amaoto writel(val, phy_base + PHY_PLL_REG1);
283*7a099760SInochi Amaoto
284*7a099760SInochi Amaoto for (i = 0; i < lg->data->lanes; i++) {
285*7a099760SInochi Amaoto void __iomem *lane_base = lg->base + lg->data->offsets[i];
286*7a099760SInochi Amaoto
287*7a099760SInochi Amaoto /* set cfg_tx_send_dummy_data to be 1'b1 for disable dash data */
288*7a099760SInochi Amaoto val = readl(lane_base + PHY_PU_SEL);
289*7a099760SInochi Amaoto val = u32_replace_bits(val, 1, BIT(13));
290*7a099760SInochi Amaoto writel(val, lane_base + PHY_PU_SEL);
291*7a099760SInochi Amaoto
292*7a099760SInochi Amaoto /* disable en_sample_data_after_cdr_locked */
293*7a099760SInochi Amaoto val = readl(lane_base + PHY_RESET_CFG);
294*7a099760SInochi Amaoto val = u32_replace_bits(val, 0, BIT(6));
295*7a099760SInochi Amaoto writel(val, lane_base + PHY_RESET_CFG);
296*7a099760SInochi Amaoto
297*7a099760SInochi Amaoto /* Dynamic Lock */
298*7a099760SInochi Amaoto val = readl(lane_base + PHY_MODE_CFG);
299*7a099760SInochi Amaoto val = u32_replace_bits(val, 1, BIT(2));
300*7a099760SInochi Amaoto writel(val, lane_base + PHY_MODE_CFG);
301*7a099760SInochi Amaoto
302*7a099760SInochi Amaoto val = FIELD_PREP(PHY_RX_REG0_MASK, 0x10) |
303*7a099760SInochi Amaoto FIELD_PREP(PHY_RX_REG1_MASK, 0x78) |
304*7a099760SInochi Amaoto FIELD_PREP(PHY_RX_REG2_MASK, 0x98) |
305*7a099760SInochi Amaoto FIELD_PREP(PHY_RX_REG3_MASK, 0xdf);
306*7a099760SInochi Amaoto writel(val, lane_base + PHY_RX_REG_A);
307*7a099760SInochi Amaoto
308*7a099760SInochi Amaoto val = readl(lane_base + PHY_RX_REG_B);
309*7a099760SInochi Amaoto val &= ~PHY_RX_REGB_MASK;
310*7a099760SInochi Amaoto val |= FIELD_PREP(PHY_RX_REG4_MASK, 0xb4) |
311*7a099760SInochi Amaoto FIELD_PREP(PHY_RX_REG5_MASK, 0x88) |
312*7a099760SInochi Amaoto FIELD_PREP(PHY_RX_REG6_MASK, 0x28);
313*7a099760SInochi Amaoto writel(val, lane_base + PHY_RX_REG_B);
314*7a099760SInochi Amaoto
315*7a099760SInochi Amaoto /* Set init done */
316*7a099760SInochi Amaoto val = readl(lane_base + PCIE_PU_ADDR_CLK_CFG);
317*7a099760SInochi Amaoto val = u32_replace_bits(val, 1, CFG_SW_PHY_INIT_DONE);
318*7a099760SInochi Amaoto writel(val, lane_base + PCIE_PU_ADDR_CLK_CFG);
319*7a099760SInochi Amaoto }
320*7a099760SInochi Amaoto
321*7a099760SInochi Amaoto ret = readl_poll_timeout(phy_base + PCIE_PU_ADDR_CLK_CFG, val,
322*7a099760SInochi Amaoto (val & PHY_CLK_PLL_READY), PCIE_POLL_DELAY,
323*7a099760SInochi Amaoto PCIE_PLL_TIMEOUT);
324*7a099760SInochi Amaoto if (ret) {
325*7a099760SInochi Amaoto dev_err(&lg->phy->dev, "PHY PLL lock timeout\n");
326*7a099760SInochi Amaoto return ret;
327*7a099760SInochi Amaoto }
328*7a099760SInochi Amaoto
329*7a099760SInochi Amaoto return 0;
330*7a099760SInochi Amaoto }
331*7a099760SInochi Amaoto
332*7a099760SInochi Amaoto const struct phy_ops k3_pcie_phy_ops = {
333*7a099760SInochi Amaoto .init = k3_pcie_phy_init,
334*7a099760SInochi Amaoto .owner = THIS_MODULE,
335*7a099760SInochi Amaoto };
336*7a099760SInochi Amaoto EXPORT_SYMBOL_GPL(k3_pcie_phy_ops);
337*7a099760SInochi Amaoto
338*7a099760SInochi Amaoto /* PHY rcal init requires APB_SPARE regmap access */
339*7a099760SInochi Amaoto
340*7a099760SInochi Amaoto #define APB_SPARE_PU_CAL 0x178
341*7a099760SInochi Amaoto #define PU_CAL BIT(17)
342*7a099760SInochi Amaoto
343*7a099760SInochi Amaoto #define APB_SPARE_RCAL_HSIO 0x17c
344*7a099760SInochi Amaoto #define APB_SPARE_PU_CAL_DONE BIT(8)
345*7a099760SInochi Amaoto #define RCAL_OVRD_PTRIM GENMASK(23, 20)
346*7a099760SInochi Amaoto #define RCAL_OVRD_NTRIM GENMASK(27, 24)
347*7a099760SInochi Amaoto #define RCAL_OVRD_PTRIM_EN BIT(28)
348*7a099760SInochi Amaoto #define RCAL_OVRD_NTRIM_EN BIT(29)
349*7a099760SInochi Amaoto #define RCAL_OVRD_STABLE_VAL BIT(30)
350*7a099760SInochi Amaoto #define RCAL_OVRD_STABLE_EN BIT(31)
351*7a099760SInochi Amaoto
352*7a099760SInochi Amaoto #define RCAL_OVRD_TRIM_EN (RCAL_OVRD_NTRIM_EN | RCAL_OVRD_PTRIM_EN)
353*7a099760SInochi Amaoto #define RCAL_OVRD_TRIM_MASK (RCAL_OVRD_NTRIM | RCAL_OVRD_PTRIM)
354*7a099760SInochi Amaoto
355*7a099760SInochi Amaoto #define PU_CAL_TIMEOUT 2000000
356*7a099760SInochi Amaoto
357*7a099760SInochi Amaoto static DEFINE_MUTEX(calibrate_lock);
358*7a099760SInochi Amaoto
k3_phy_calibrate(struct regmap * apb_spare)359*7a099760SInochi Amaoto int k3_phy_calibrate(struct regmap *apb_spare)
360*7a099760SInochi Amaoto {
361*7a099760SInochi Amaoto unsigned int val = 0;
362*7a099760SInochi Amaoto int ret;
363*7a099760SInochi Amaoto
364*7a099760SInochi Amaoto guard(mutex)(&calibrate_lock);
365*7a099760SInochi Amaoto
366*7a099760SInochi Amaoto regmap_read(apb_spare, APB_SPARE_RCAL_HSIO, &val);
367*7a099760SInochi Amaoto if (val & APB_SPARE_PU_CAL_DONE)
368*7a099760SInochi Amaoto return 0;
369*7a099760SInochi Amaoto
370*7a099760SInochi Amaoto regmap_update_bits(apb_spare, APB_SPARE_PU_CAL, PU_CAL,
371*7a099760SInochi Amaoto PU_CAL);
372*7a099760SInochi Amaoto
373*7a099760SInochi Amaoto ret = regmap_read_poll_timeout(apb_spare, APB_SPARE_RCAL_HSIO,
374*7a099760SInochi Amaoto val, (val & APB_SPARE_PU_CAL_DONE), PCIE_POLL_DELAY,
375*7a099760SInochi Amaoto PU_CAL_TIMEOUT);
376*7a099760SInochi Amaoto
377*7a099760SInochi Amaoto if (ret)
378*7a099760SInochi Amaoto regmap_update_bits(apb_spare, APB_SPARE_RCAL_HSIO,
379*7a099760SInochi Amaoto RCAL_OVRD_TRIM_EN | RCAL_OVRD_STABLE_VAL |
380*7a099760SInochi Amaoto RCAL_OVRD_TRIM_MASK | RCAL_OVRD_STABLE_EN,
381*7a099760SInochi Amaoto RCAL_OVRD_TRIM_EN | RCAL_OVRD_STABLE_VAL |
382*7a099760SInochi Amaoto FIELD_PREP(RCAL_OVRD_NTRIM, 0x6) |
383*7a099760SInochi Amaoto FIELD_PREP(RCAL_OVRD_PTRIM, 0xa) |
384*7a099760SInochi Amaoto RCAL_OVRD_STABLE_EN);
385*7a099760SInochi Amaoto
386*7a099760SInochi Amaoto return 0;
387*7a099760SInochi Amaoto }
388*7a099760SInochi Amaoto EXPORT_SYMBOL_GPL(k3_phy_calibrate);
389*7a099760SInochi Amaoto
390*7a099760SInochi Amaoto MODULE_DESCRIPTION("SpacemiT K3 PHY common ops");
391*7a099760SInochi Amaoto MODULE_LICENSE("GPL");
392