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