1*16c595a5SJakub Kicinski // SPDX-License-Identifier: GPL-2.0-only
2*16c595a5SJakub Kicinski /* Copyright (c) 2015-2016, The Linux Foundation. All rights reserved.
3*16c595a5SJakub Kicinski */
4*16c595a5SJakub Kicinski
5*16c595a5SJakub Kicinski /* Qualcomm Technologies, Inc. QDF2400 EMAC SGMII Controller driver.
6*16c595a5SJakub Kicinski */
7*16c595a5SJakub Kicinski
8*16c595a5SJakub Kicinski #include <linux/iopoll.h>
9*16c595a5SJakub Kicinski #include "emac.h"
10*16c595a5SJakub Kicinski
11*16c595a5SJakub Kicinski /* EMAC_SGMII register offsets */
12*16c595a5SJakub Kicinski #define EMAC_SGMII_PHY_TX_PWR_CTRL 0x000C
13*16c595a5SJakub Kicinski #define EMAC_SGMII_PHY_LANE_CTRL1 0x0018
14*16c595a5SJakub Kicinski #define EMAC_SGMII_PHY_CDR_CTRL0 0x0058
15*16c595a5SJakub Kicinski #define EMAC_SGMII_PHY_POW_DWN_CTRL0 0x0080
16*16c595a5SJakub Kicinski #define EMAC_SGMII_PHY_RESET_CTRL 0x00a8
17*16c595a5SJakub Kicinski #define EMAC_SGMII_PHY_INTERRUPT_MASK 0x00b4
18*16c595a5SJakub Kicinski
19*16c595a5SJakub Kicinski /* SGMII digital lane registers */
20*16c595a5SJakub Kicinski #define EMAC_SGMII_LN_DRVR_CTRL0 0x000C
21*16c595a5SJakub Kicinski #define EMAC_SGMII_LN_DRVR_CTRL1 0x0010
22*16c595a5SJakub Kicinski #define EMAC_SGMII_LN_DRVR_TAP_EN 0x0018
23*16c595a5SJakub Kicinski #define EMAC_SGMII_LN_TX_MARGINING 0x001C
24*16c595a5SJakub Kicinski #define EMAC_SGMII_LN_TX_PRE 0x0020
25*16c595a5SJakub Kicinski #define EMAC_SGMII_LN_TX_POST 0x0024
26*16c595a5SJakub Kicinski #define EMAC_SGMII_LN_TX_BAND_MODE 0x0060
27*16c595a5SJakub Kicinski #define EMAC_SGMII_LN_LANE_MODE 0x0064
28*16c595a5SJakub Kicinski #define EMAC_SGMII_LN_PARALLEL_RATE 0x007C
29*16c595a5SJakub Kicinski #define EMAC_SGMII_LN_CML_CTRL_MODE0 0x00C0
30*16c595a5SJakub Kicinski #define EMAC_SGMII_LN_MIXER_CTRL_MODE0 0x00D8
31*16c595a5SJakub Kicinski #define EMAC_SGMII_LN_VGA_INITVAL 0x013C
32*16c595a5SJakub Kicinski #define EMAC_SGMII_LN_UCDR_FO_GAIN_MODE0 0x0184
33*16c595a5SJakub Kicinski #define EMAC_SGMII_LN_UCDR_SO_GAIN_MODE0 0x0190
34*16c595a5SJakub Kicinski #define EMAC_SGMII_LN_UCDR_SO_CONFIG 0x019C
35*16c595a5SJakub Kicinski #define EMAC_SGMII_LN_RX_BAND 0x01A4
36*16c595a5SJakub Kicinski #define EMAC_SGMII_LN_RX_RCVR_PATH1_MODE0 0x01C0
37*16c595a5SJakub Kicinski #define EMAC_SGMII_LN_RSM_CONFIG 0x01F8
38*16c595a5SJakub Kicinski #define EMAC_SGMII_LN_SIGDET_ENABLES 0x0230
39*16c595a5SJakub Kicinski #define EMAC_SGMII_LN_SIGDET_CNTRL 0x0234
40*16c595a5SJakub Kicinski #define EMAC_SGMII_LN_SIGDET_DEGLITCH_CNTRL 0x0238
41*16c595a5SJakub Kicinski #define EMAC_SGMII_LN_RX_EN_SIGNAL 0x02AC
42*16c595a5SJakub Kicinski #define EMAC_SGMII_LN_RX_MISC_CNTRL0 0x02B8
43*16c595a5SJakub Kicinski #define EMAC_SGMII_LN_DRVR_LOGIC_CLKDIV 0x02C8
44*16c595a5SJakub Kicinski #define EMAC_SGMII_LN_RX_RESECODE_OFFSET 0x02CC
45*16c595a5SJakub Kicinski
46*16c595a5SJakub Kicinski /* SGMII digital lane register values */
47*16c595a5SJakub Kicinski #define UCDR_STEP_BY_TWO_MODE0 BIT(7)
48*16c595a5SJakub Kicinski #define UCDR_xO_GAIN_MODE(x) ((x) & 0x7f)
49*16c595a5SJakub Kicinski #define UCDR_ENABLE BIT(6)
50*16c595a5SJakub Kicinski #define UCDR_SO_SATURATION(x) ((x) & 0x3f)
51*16c595a5SJakub Kicinski
52*16c595a5SJakub Kicinski #define SIGDET_LP_BYP_PS4 BIT(7)
53*16c595a5SJakub Kicinski #define SIGDET_EN_PS0_TO_PS2 BIT(6)
54*16c595a5SJakub Kicinski
55*16c595a5SJakub Kicinski #define TXVAL_VALID_INIT BIT(4)
56*16c595a5SJakub Kicinski #define KR_PCIGEN3_MODE BIT(0)
57*16c595a5SJakub Kicinski
58*16c595a5SJakub Kicinski #define MAIN_EN BIT(0)
59*16c595a5SJakub Kicinski
60*16c595a5SJakub Kicinski #define TX_MARGINING_MUX BIT(6)
61*16c595a5SJakub Kicinski #define TX_MARGINING(x) ((x) & 0x3f)
62*16c595a5SJakub Kicinski
63*16c595a5SJakub Kicinski #define TX_PRE_MUX BIT(6)
64*16c595a5SJakub Kicinski
65*16c595a5SJakub Kicinski #define TX_POST_MUX BIT(6)
66*16c595a5SJakub Kicinski
67*16c595a5SJakub Kicinski #define CML_GEAR_MODE(x) (((x) & 7) << 3)
68*16c595a5SJakub Kicinski #define CML2CMOS_IBOOST_MODE(x) ((x) & 7)
69*16c595a5SJakub Kicinski
70*16c595a5SJakub Kicinski #define RESCODE_OFFSET(x) ((x) & 0x1f)
71*16c595a5SJakub Kicinski
72*16c595a5SJakub Kicinski #define MIXER_LOADB_MODE(x) (((x) & 0xf) << 2)
73*16c595a5SJakub Kicinski #define MIXER_DATARATE_MODE(x) ((x) & 3)
74*16c595a5SJakub Kicinski
75*16c595a5SJakub Kicinski #define VGA_THRESH_DFE(x) ((x) & 0x3f)
76*16c595a5SJakub Kicinski
77*16c595a5SJakub Kicinski #define SIGDET_LP_BYP_PS0_TO_PS2 BIT(5)
78*16c595a5SJakub Kicinski #define SIGDET_FLT_BYP BIT(0)
79*16c595a5SJakub Kicinski
80*16c595a5SJakub Kicinski #define SIGDET_LVL(x) (((x) & 0xf) << 4)
81*16c595a5SJakub Kicinski
82*16c595a5SJakub Kicinski #define SIGDET_DEGLITCH_CTRL(x) (((x) & 0xf) << 1)
83*16c595a5SJakub Kicinski
84*16c595a5SJakub Kicinski #define INVERT_PCS_RX_CLK BIT(7)
85*16c595a5SJakub Kicinski
86*16c595a5SJakub Kicinski #define DRVR_LOGIC_CLK_EN BIT(4)
87*16c595a5SJakub Kicinski #define DRVR_LOGIC_CLK_DIV(x) ((x) & 0xf)
88*16c595a5SJakub Kicinski
89*16c595a5SJakub Kicinski #define PARALLEL_RATE_MODE0(x) ((x) & 0x3)
90*16c595a5SJakub Kicinski
91*16c595a5SJakub Kicinski #define BAND_MODE0(x) ((x) & 0x3)
92*16c595a5SJakub Kicinski
93*16c595a5SJakub Kicinski #define LANE_MODE(x) ((x) & 0x1f)
94*16c595a5SJakub Kicinski
95*16c595a5SJakub Kicinski #define CDR_PD_SEL_MODE0(x) (((x) & 0x3) << 5)
96*16c595a5SJakub Kicinski #define EN_DLL_MODE0 BIT(4)
97*16c595a5SJakub Kicinski #define EN_IQ_DCC_MODE0 BIT(3)
98*16c595a5SJakub Kicinski #define EN_IQCAL_MODE0 BIT(2)
99*16c595a5SJakub Kicinski
100*16c595a5SJakub Kicinski #define BYPASS_RSM_SAMP_CAL BIT(1)
101*16c595a5SJakub Kicinski #define BYPASS_RSM_DLL_CAL BIT(0)
102*16c595a5SJakub Kicinski
103*16c595a5SJakub Kicinski #define L0_RX_EQUALIZE_ENABLE BIT(6)
104*16c595a5SJakub Kicinski
105*16c595a5SJakub Kicinski #define PWRDN_B BIT(0)
106*16c595a5SJakub Kicinski
107*16c595a5SJakub Kicinski #define CDR_MAX_CNT(x) ((x) & 0xff)
108*16c595a5SJakub Kicinski
109*16c595a5SJakub Kicinski #define SERDES_START_WAIT_TIMES 100
110*16c595a5SJakub Kicinski
111*16c595a5SJakub Kicinski struct emac_reg_write {
112*16c595a5SJakub Kicinski unsigned int offset;
113*16c595a5SJakub Kicinski u32 val;
114*16c595a5SJakub Kicinski };
115*16c595a5SJakub Kicinski
emac_reg_write_all(void __iomem * base,const struct emac_reg_write * itr,size_t size)116*16c595a5SJakub Kicinski static void emac_reg_write_all(void __iomem *base,
117*16c595a5SJakub Kicinski const struct emac_reg_write *itr, size_t size)
118*16c595a5SJakub Kicinski {
119*16c595a5SJakub Kicinski size_t i;
120*16c595a5SJakub Kicinski
121*16c595a5SJakub Kicinski for (i = 0; i < size; ++itr, ++i)
122*16c595a5SJakub Kicinski writel(itr->val, base + itr->offset);
123*16c595a5SJakub Kicinski }
124*16c595a5SJakub Kicinski
125*16c595a5SJakub Kicinski static const struct emac_reg_write sgmii_laned[] = {
126*16c595a5SJakub Kicinski /* CDR Settings */
127*16c595a5SJakub Kicinski {EMAC_SGMII_LN_UCDR_FO_GAIN_MODE0,
128*16c595a5SJakub Kicinski UCDR_STEP_BY_TWO_MODE0 | UCDR_xO_GAIN_MODE(10)},
129*16c595a5SJakub Kicinski {EMAC_SGMII_LN_UCDR_SO_GAIN_MODE0, UCDR_xO_GAIN_MODE(0)},
130*16c595a5SJakub Kicinski {EMAC_SGMII_LN_UCDR_SO_CONFIG, UCDR_ENABLE | UCDR_SO_SATURATION(12)},
131*16c595a5SJakub Kicinski
132*16c595a5SJakub Kicinski /* TX/RX Settings */
133*16c595a5SJakub Kicinski {EMAC_SGMII_LN_RX_EN_SIGNAL, SIGDET_LP_BYP_PS4 | SIGDET_EN_PS0_TO_PS2},
134*16c595a5SJakub Kicinski
135*16c595a5SJakub Kicinski {EMAC_SGMII_LN_DRVR_CTRL0, TXVAL_VALID_INIT | KR_PCIGEN3_MODE},
136*16c595a5SJakub Kicinski {EMAC_SGMII_LN_DRVR_TAP_EN, MAIN_EN},
137*16c595a5SJakub Kicinski {EMAC_SGMII_LN_TX_MARGINING, TX_MARGINING_MUX | TX_MARGINING(25)},
138*16c595a5SJakub Kicinski {EMAC_SGMII_LN_TX_PRE, TX_PRE_MUX},
139*16c595a5SJakub Kicinski {EMAC_SGMII_LN_TX_POST, TX_POST_MUX},
140*16c595a5SJakub Kicinski
141*16c595a5SJakub Kicinski {EMAC_SGMII_LN_CML_CTRL_MODE0,
142*16c595a5SJakub Kicinski CML_GEAR_MODE(1) | CML2CMOS_IBOOST_MODE(1)},
143*16c595a5SJakub Kicinski {EMAC_SGMII_LN_MIXER_CTRL_MODE0,
144*16c595a5SJakub Kicinski MIXER_LOADB_MODE(12) | MIXER_DATARATE_MODE(1)},
145*16c595a5SJakub Kicinski {EMAC_SGMII_LN_VGA_INITVAL, VGA_THRESH_DFE(31)},
146*16c595a5SJakub Kicinski {EMAC_SGMII_LN_SIGDET_ENABLES,
147*16c595a5SJakub Kicinski SIGDET_LP_BYP_PS0_TO_PS2 | SIGDET_FLT_BYP},
148*16c595a5SJakub Kicinski {EMAC_SGMII_LN_SIGDET_CNTRL, SIGDET_LVL(8)},
149*16c595a5SJakub Kicinski
150*16c595a5SJakub Kicinski {EMAC_SGMII_LN_SIGDET_DEGLITCH_CNTRL, SIGDET_DEGLITCH_CTRL(4)},
151*16c595a5SJakub Kicinski {EMAC_SGMII_LN_RX_MISC_CNTRL0, INVERT_PCS_RX_CLK},
152*16c595a5SJakub Kicinski {EMAC_SGMII_LN_DRVR_LOGIC_CLKDIV,
153*16c595a5SJakub Kicinski DRVR_LOGIC_CLK_EN | DRVR_LOGIC_CLK_DIV(4)},
154*16c595a5SJakub Kicinski
155*16c595a5SJakub Kicinski {EMAC_SGMII_LN_PARALLEL_RATE, PARALLEL_RATE_MODE0(1)},
156*16c595a5SJakub Kicinski {EMAC_SGMII_LN_TX_BAND_MODE, BAND_MODE0(1)},
157*16c595a5SJakub Kicinski {EMAC_SGMII_LN_RX_BAND, BAND_MODE0(2)},
158*16c595a5SJakub Kicinski {EMAC_SGMII_LN_DRVR_CTRL1, RESCODE_OFFSET(7)},
159*16c595a5SJakub Kicinski {EMAC_SGMII_LN_RX_RESECODE_OFFSET, RESCODE_OFFSET(9)},
160*16c595a5SJakub Kicinski {EMAC_SGMII_LN_LANE_MODE, LANE_MODE(26)},
161*16c595a5SJakub Kicinski {EMAC_SGMII_LN_RX_RCVR_PATH1_MODE0, CDR_PD_SEL_MODE0(2) |
162*16c595a5SJakub Kicinski EN_DLL_MODE0 | EN_IQ_DCC_MODE0 | EN_IQCAL_MODE0},
163*16c595a5SJakub Kicinski {EMAC_SGMII_LN_RSM_CONFIG, BYPASS_RSM_SAMP_CAL | BYPASS_RSM_DLL_CAL},
164*16c595a5SJakub Kicinski };
165*16c595a5SJakub Kicinski
166*16c595a5SJakub Kicinski static const struct emac_reg_write physical_coding_sublayer_programming[] = {
167*16c595a5SJakub Kicinski {EMAC_SGMII_PHY_POW_DWN_CTRL0, PWRDN_B},
168*16c595a5SJakub Kicinski {EMAC_SGMII_PHY_CDR_CTRL0, CDR_MAX_CNT(15)},
169*16c595a5SJakub Kicinski {EMAC_SGMII_PHY_TX_PWR_CTRL, 0},
170*16c595a5SJakub Kicinski {EMAC_SGMII_PHY_LANE_CTRL1, L0_RX_EQUALIZE_ENABLE},
171*16c595a5SJakub Kicinski };
172*16c595a5SJakub Kicinski
emac_sgmii_init_qdf2400(struct emac_adapter * adpt)173*16c595a5SJakub Kicinski int emac_sgmii_init_qdf2400(struct emac_adapter *adpt)
174*16c595a5SJakub Kicinski {
175*16c595a5SJakub Kicinski struct emac_sgmii *phy = &adpt->phy;
176*16c595a5SJakub Kicinski void __iomem *phy_regs = phy->base;
177*16c595a5SJakub Kicinski void __iomem *laned = phy->digital;
178*16c595a5SJakub Kicinski unsigned int i;
179*16c595a5SJakub Kicinski u32 lnstatus;
180*16c595a5SJakub Kicinski
181*16c595a5SJakub Kicinski /* PCS lane-x init */
182*16c595a5SJakub Kicinski emac_reg_write_all(phy->base, physical_coding_sublayer_programming,
183*16c595a5SJakub Kicinski ARRAY_SIZE(physical_coding_sublayer_programming));
184*16c595a5SJakub Kicinski
185*16c595a5SJakub Kicinski /* SGMII lane-x init */
186*16c595a5SJakub Kicinski emac_reg_write_all(phy->digital, sgmii_laned, ARRAY_SIZE(sgmii_laned));
187*16c595a5SJakub Kicinski
188*16c595a5SJakub Kicinski /* Power up PCS and start reset lane state machine */
189*16c595a5SJakub Kicinski
190*16c595a5SJakub Kicinski writel(0, phy_regs + EMAC_SGMII_PHY_RESET_CTRL);
191*16c595a5SJakub Kicinski writel(1, laned + SGMII_LN_RSM_START);
192*16c595a5SJakub Kicinski
193*16c595a5SJakub Kicinski /* Wait for c_ready assertion */
194*16c595a5SJakub Kicinski for (i = 0; i < SERDES_START_WAIT_TIMES; i++) {
195*16c595a5SJakub Kicinski lnstatus = readl(phy_regs + SGMII_PHY_LN_LANE_STATUS);
196*16c595a5SJakub Kicinski if (lnstatus & BIT(1))
197*16c595a5SJakub Kicinski break;
198*16c595a5SJakub Kicinski usleep_range(100, 200);
199*16c595a5SJakub Kicinski }
200*16c595a5SJakub Kicinski
201*16c595a5SJakub Kicinski if (i == SERDES_START_WAIT_TIMES) {
202*16c595a5SJakub Kicinski netdev_err(adpt->netdev, "SGMII failed to start\n");
203*16c595a5SJakub Kicinski return -EIO;
204*16c595a5SJakub Kicinski }
205*16c595a5SJakub Kicinski
206*16c595a5SJakub Kicinski /* Disable digital and SERDES loopback */
207*16c595a5SJakub Kicinski writel(0, phy_regs + SGMII_PHY_LN_BIST_GEN0);
208*16c595a5SJakub Kicinski writel(0, phy_regs + SGMII_PHY_LN_BIST_GEN2);
209*16c595a5SJakub Kicinski writel(0, phy_regs + SGMII_PHY_LN_CDR_CTRL1);
210*16c595a5SJakub Kicinski
211*16c595a5SJakub Kicinski /* Mask out all the SGMII Interrupt */
212*16c595a5SJakub Kicinski writel(0, phy_regs + EMAC_SGMII_PHY_INTERRUPT_MASK);
213*16c595a5SJakub Kicinski
214*16c595a5SJakub Kicinski return 0;
215*16c595a5SJakub Kicinski }
216