xref: /linux/drivers/net/ethernet/amd/xgbe/xgbe-phy-v1.c (revision e5c86679d5e864947a52fb31e45a425dea3e7fa9)
1 /*
2  * AMD 10Gb Ethernet driver
3  *
4  * This file is available to you under your choice of the following two
5  * licenses:
6  *
7  * License 1: GPLv2
8  *
9  * Copyright (c) 2016 Advanced Micro Devices, Inc.
10  *
11  * This file is free software; you may copy, redistribute and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation, either version 2 of the License, or (at
14  * your option) any later version.
15  *
16  * This file is distributed in the hope that it will be useful, but
17  * WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23  *
24  * This file incorporates work covered by the following copyright and
25  * permission notice:
26  *     The Synopsys DWC ETHER XGMAC Software Driver and documentation
27  *     (hereinafter "Software") is an unsupported proprietary work of Synopsys,
28  *     Inc. unless otherwise expressly agreed to in writing between Synopsys
29  *     and you.
30  *
31  *     The Software IS NOT an item of Licensed Software or Licensed Product
32  *     under any End User Software License Agreement or Agreement for Licensed
33  *     Product with Synopsys or any supplement thereto.  Permission is hereby
34  *     granted, free of charge, to any person obtaining a copy of this software
35  *     annotated with this license and the Software, to deal in the Software
36  *     without restriction, including without limitation the rights to use,
37  *     copy, modify, merge, publish, distribute, sublicense, and/or sell copies
38  *     of the Software, and to permit persons to whom the Software is furnished
39  *     to do so, subject to the following conditions:
40  *
41  *     The above copyright notice and this permission notice shall be included
42  *     in all copies or substantial portions of the Software.
43  *
44  *     THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
45  *     BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
46  *     TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
47  *     PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS
48  *     BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
49  *     CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
50  *     SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
51  *     INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
52  *     CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
53  *     ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
54  *     THE POSSIBILITY OF SUCH DAMAGE.
55  *
56  *
57  * License 2: Modified BSD
58  *
59  * Copyright (c) 2016 Advanced Micro Devices, Inc.
60  * All rights reserved.
61  *
62  * Redistribution and use in source and binary forms, with or without
63  * modification, are permitted provided that the following conditions are met:
64  *     * Redistributions of source code must retain the above copyright
65  *       notice, this list of conditions and the following disclaimer.
66  *     * Redistributions in binary form must reproduce the above copyright
67  *       notice, this list of conditions and the following disclaimer in the
68  *       documentation and/or other materials provided with the distribution.
69  *     * Neither the name of Advanced Micro Devices, Inc. nor the
70  *       names of its contributors may be used to endorse or promote products
71  *       derived from this software without specific prior written permission.
72  *
73  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
74  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
75  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
76  * ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
77  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
78  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
79  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
80  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
81  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
82  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
83  *
84  * This file incorporates work covered by the following copyright and
85  * permission notice:
86  *     The Synopsys DWC ETHER XGMAC Software Driver and documentation
87  *     (hereinafter "Software") is an unsupported proprietary work of Synopsys,
88  *     Inc. unless otherwise expressly agreed to in writing between Synopsys
89  *     and you.
90  *
91  *     The Software IS NOT an item of Licensed Software or Licensed Product
92  *     under any End User Software License Agreement or Agreement for Licensed
93  *     Product with Synopsys or any supplement thereto.  Permission is hereby
94  *     granted, free of charge, to any person obtaining a copy of this software
95  *     annotated with this license and the Software, to deal in the Software
96  *     without restriction, including without limitation the rights to use,
97  *     copy, modify, merge, publish, distribute, sublicense, and/or sell copies
98  *     of the Software, and to permit persons to whom the Software is furnished
99  *     to do so, subject to the following conditions:
100  *
101  *     The above copyright notice and this permission notice shall be included
102  *     in all copies or substantial portions of the Software.
103  *
104  *     THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
105  *     BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
106  *     TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
107  *     PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS
108  *     BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
109  *     CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
110  *     SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
111  *     INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
112  *     CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
113  *     ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
114  *     THE POSSIBILITY OF SUCH DAMAGE.
115  */
116 
117 #include <linux/module.h>
118 #include <linux/kmod.h>
119 #include <linux/device.h>
120 #include <linux/property.h>
121 #include <linux/mdio.h>
122 #include <linux/phy.h>
123 
124 #include "xgbe.h"
125 #include "xgbe-common.h"
126 
127 #define XGBE_BLWC_PROPERTY		"amd,serdes-blwc"
128 #define XGBE_CDR_RATE_PROPERTY		"amd,serdes-cdr-rate"
129 #define XGBE_PQ_SKEW_PROPERTY		"amd,serdes-pq-skew"
130 #define XGBE_TX_AMP_PROPERTY		"amd,serdes-tx-amp"
131 #define XGBE_DFE_CFG_PROPERTY		"amd,serdes-dfe-tap-config"
132 #define XGBE_DFE_ENA_PROPERTY		"amd,serdes-dfe-tap-enable"
133 
134 /* Default SerDes settings */
135 #define XGBE_SPEED_1000_BLWC		1
136 #define XGBE_SPEED_1000_CDR		0x2
137 #define XGBE_SPEED_1000_PLL		0x0
138 #define XGBE_SPEED_1000_PQ		0xa
139 #define XGBE_SPEED_1000_RATE		0x3
140 #define XGBE_SPEED_1000_TXAMP		0xf
141 #define XGBE_SPEED_1000_WORD		0x1
142 #define XGBE_SPEED_1000_DFE_TAP_CONFIG	0x3
143 #define XGBE_SPEED_1000_DFE_TAP_ENABLE	0x0
144 
145 #define XGBE_SPEED_2500_BLWC		1
146 #define XGBE_SPEED_2500_CDR		0x2
147 #define XGBE_SPEED_2500_PLL		0x0
148 #define XGBE_SPEED_2500_PQ		0xa
149 #define XGBE_SPEED_2500_RATE		0x1
150 #define XGBE_SPEED_2500_TXAMP		0xf
151 #define XGBE_SPEED_2500_WORD		0x1
152 #define XGBE_SPEED_2500_DFE_TAP_CONFIG	0x3
153 #define XGBE_SPEED_2500_DFE_TAP_ENABLE	0x0
154 
155 #define XGBE_SPEED_10000_BLWC		0
156 #define XGBE_SPEED_10000_CDR		0x7
157 #define XGBE_SPEED_10000_PLL		0x1
158 #define XGBE_SPEED_10000_PQ		0x12
159 #define XGBE_SPEED_10000_RATE		0x0
160 #define XGBE_SPEED_10000_TXAMP		0xa
161 #define XGBE_SPEED_10000_WORD		0x7
162 #define XGBE_SPEED_10000_DFE_TAP_CONFIG	0x1
163 #define XGBE_SPEED_10000_DFE_TAP_ENABLE	0x7f
164 
165 /* Rate-change complete wait/retry count */
166 #define XGBE_RATECHANGE_COUNT		500
167 
168 static const u32 xgbe_phy_blwc[] = {
169 	XGBE_SPEED_1000_BLWC,
170 	XGBE_SPEED_2500_BLWC,
171 	XGBE_SPEED_10000_BLWC,
172 };
173 
174 static const u32 xgbe_phy_cdr_rate[] = {
175 	XGBE_SPEED_1000_CDR,
176 	XGBE_SPEED_2500_CDR,
177 	XGBE_SPEED_10000_CDR,
178 };
179 
180 static const u32 xgbe_phy_pq_skew[] = {
181 	XGBE_SPEED_1000_PQ,
182 	XGBE_SPEED_2500_PQ,
183 	XGBE_SPEED_10000_PQ,
184 };
185 
186 static const u32 xgbe_phy_tx_amp[] = {
187 	XGBE_SPEED_1000_TXAMP,
188 	XGBE_SPEED_2500_TXAMP,
189 	XGBE_SPEED_10000_TXAMP,
190 };
191 
192 static const u32 xgbe_phy_dfe_tap_cfg[] = {
193 	XGBE_SPEED_1000_DFE_TAP_CONFIG,
194 	XGBE_SPEED_2500_DFE_TAP_CONFIG,
195 	XGBE_SPEED_10000_DFE_TAP_CONFIG,
196 };
197 
198 static const u32 xgbe_phy_dfe_tap_ena[] = {
199 	XGBE_SPEED_1000_DFE_TAP_ENABLE,
200 	XGBE_SPEED_2500_DFE_TAP_ENABLE,
201 	XGBE_SPEED_10000_DFE_TAP_ENABLE,
202 };
203 
204 struct xgbe_phy_data {
205 	/* 1000/10000 vs 2500/10000 indicator */
206 	unsigned int speed_set;
207 
208 	/* SerDes UEFI configurable settings.
209 	 *   Switching between modes/speeds requires new values for some
210 	 *   SerDes settings.  The values can be supplied as device
211 	 *   properties in array format.  The first array entry is for
212 	 *   1GbE, second for 2.5GbE and third for 10GbE
213 	 */
214 	u32 blwc[XGBE_SPEEDS];
215 	u32 cdr_rate[XGBE_SPEEDS];
216 	u32 pq_skew[XGBE_SPEEDS];
217 	u32 tx_amp[XGBE_SPEEDS];
218 	u32 dfe_tap_cfg[XGBE_SPEEDS];
219 	u32 dfe_tap_ena[XGBE_SPEEDS];
220 };
221 
222 static void xgbe_phy_kr_training_pre(struct xgbe_prv_data *pdata)
223 {
224 		XSIR0_IOWRITE_BITS(pdata, SIR0_KR_RT_1, RESET, 1);
225 }
226 
227 static void xgbe_phy_kr_training_post(struct xgbe_prv_data *pdata)
228 {
229 		XSIR0_IOWRITE_BITS(pdata, SIR0_KR_RT_1, RESET, 0);
230 }
231 
232 static enum xgbe_mode xgbe_phy_an_outcome(struct xgbe_prv_data *pdata)
233 {
234 	struct xgbe_phy_data *phy_data = pdata->phy_data;
235 	enum xgbe_mode mode;
236 	unsigned int ad_reg, lp_reg;
237 
238 	pdata->phy.lp_advertising |= ADVERTISED_Autoneg;
239 	pdata->phy.lp_advertising |= ADVERTISED_Backplane;
240 
241 	/* Compare Advertisement and Link Partner register 1 */
242 	ad_reg = XMDIO_READ(pdata, MDIO_MMD_AN, MDIO_AN_ADVERTISE);
243 	lp_reg = XMDIO_READ(pdata, MDIO_MMD_AN, MDIO_AN_LPA);
244 	if (lp_reg & 0x400)
245 		pdata->phy.lp_advertising |= ADVERTISED_Pause;
246 	if (lp_reg & 0x800)
247 		pdata->phy.lp_advertising |= ADVERTISED_Asym_Pause;
248 
249 	if (pdata->phy.pause_autoneg) {
250 		/* Set flow control based on auto-negotiation result */
251 		pdata->phy.tx_pause = 0;
252 		pdata->phy.rx_pause = 0;
253 
254 		if (ad_reg & lp_reg & 0x400) {
255 			pdata->phy.tx_pause = 1;
256 			pdata->phy.rx_pause = 1;
257 		} else if (ad_reg & lp_reg & 0x800) {
258 			if (ad_reg & 0x400)
259 				pdata->phy.rx_pause = 1;
260 			else if (lp_reg & 0x400)
261 				pdata->phy.tx_pause = 1;
262 		}
263 	}
264 
265 	/* Compare Advertisement and Link Partner register 2 */
266 	ad_reg = XMDIO_READ(pdata, MDIO_MMD_AN, MDIO_AN_ADVERTISE + 1);
267 	lp_reg = XMDIO_READ(pdata, MDIO_MMD_AN, MDIO_AN_LPA + 1);
268 	if (lp_reg & 0x80)
269 		pdata->phy.lp_advertising |= ADVERTISED_10000baseKR_Full;
270 	if (lp_reg & 0x20) {
271 		if (phy_data->speed_set == XGBE_SPEEDSET_2500_10000)
272 			pdata->phy.lp_advertising |= ADVERTISED_2500baseX_Full;
273 		else
274 			pdata->phy.lp_advertising |= ADVERTISED_1000baseKX_Full;
275 	}
276 
277 	ad_reg &= lp_reg;
278 	if (ad_reg & 0x80) {
279 		mode = XGBE_MODE_KR;
280 	} else if (ad_reg & 0x20) {
281 		if (phy_data->speed_set == XGBE_SPEEDSET_2500_10000)
282 			mode = XGBE_MODE_KX_2500;
283 		else
284 			mode = XGBE_MODE_KX_1000;
285 	} else {
286 		mode = XGBE_MODE_UNKNOWN;
287 	}
288 
289 	/* Compare Advertisement and Link Partner register 3 */
290 	ad_reg = XMDIO_READ(pdata, MDIO_MMD_AN, MDIO_AN_ADVERTISE + 2);
291 	lp_reg = XMDIO_READ(pdata, MDIO_MMD_AN, MDIO_AN_LPA + 2);
292 	if (lp_reg & 0xc000)
293 		pdata->phy.lp_advertising |= ADVERTISED_10000baseR_FEC;
294 
295 	return mode;
296 }
297 
298 static unsigned int xgbe_phy_an_advertising(struct xgbe_prv_data *pdata)
299 {
300 	return pdata->phy.advertising;
301 }
302 
303 static int xgbe_phy_an_config(struct xgbe_prv_data *pdata)
304 {
305 	/* Nothing uniquely required for an configuration */
306 	return 0;
307 }
308 
309 static enum xgbe_an_mode xgbe_phy_an_mode(struct xgbe_prv_data *pdata)
310 {
311 	return XGBE_AN_MODE_CL73;
312 }
313 
314 static void xgbe_phy_pcs_power_cycle(struct xgbe_prv_data *pdata)
315 {
316 	unsigned int reg;
317 
318 	reg = XMDIO_READ(pdata, MDIO_MMD_PCS, MDIO_CTRL1);
319 
320 	reg |= MDIO_CTRL1_LPOWER;
321 	XMDIO_WRITE(pdata, MDIO_MMD_PCS, MDIO_CTRL1, reg);
322 
323 	usleep_range(75, 100);
324 
325 	reg &= ~MDIO_CTRL1_LPOWER;
326 	XMDIO_WRITE(pdata, MDIO_MMD_PCS, MDIO_CTRL1, reg);
327 }
328 
329 static void xgbe_phy_start_ratechange(struct xgbe_prv_data *pdata)
330 {
331 	/* Assert Rx and Tx ratechange */
332 	XSIR1_IOWRITE_BITS(pdata, SIR1_SPEED, RATECHANGE, 1);
333 }
334 
335 static void xgbe_phy_complete_ratechange(struct xgbe_prv_data *pdata)
336 {
337 	unsigned int wait;
338 	u16 status;
339 
340 	/* Release Rx and Tx ratechange */
341 	XSIR1_IOWRITE_BITS(pdata, SIR1_SPEED, RATECHANGE, 0);
342 
343 	/* Wait for Rx and Tx ready */
344 	wait = XGBE_RATECHANGE_COUNT;
345 	while (wait--) {
346 		usleep_range(50, 75);
347 
348 		status = XSIR0_IOREAD(pdata, SIR0_STATUS);
349 		if (XSIR_GET_BITS(status, SIR0_STATUS, RX_READY) &&
350 		    XSIR_GET_BITS(status, SIR0_STATUS, TX_READY))
351 			goto rx_reset;
352 	}
353 
354 	netif_dbg(pdata, link, pdata->netdev, "SerDes rx/tx not ready (%#hx)\n",
355 		  status);
356 
357 rx_reset:
358 	/* Perform Rx reset for the DFE changes */
359 	XRXTX_IOWRITE_BITS(pdata, RXTX_REG6, RESETB_RXD, 0);
360 	XRXTX_IOWRITE_BITS(pdata, RXTX_REG6, RESETB_RXD, 1);
361 }
362 
363 static void xgbe_phy_kr_mode(struct xgbe_prv_data *pdata)
364 {
365 	struct xgbe_phy_data *phy_data = pdata->phy_data;
366 	unsigned int reg;
367 
368 	/* Set PCS to KR/10G speed */
369 	reg = XMDIO_READ(pdata, MDIO_MMD_PCS, MDIO_CTRL2);
370 	reg &= ~MDIO_PCS_CTRL2_TYPE;
371 	reg |= MDIO_PCS_CTRL2_10GBR;
372 	XMDIO_WRITE(pdata, MDIO_MMD_PCS, MDIO_CTRL2, reg);
373 
374 	reg = XMDIO_READ(pdata, MDIO_MMD_PCS, MDIO_CTRL1);
375 	reg &= ~MDIO_CTRL1_SPEEDSEL;
376 	reg |= MDIO_CTRL1_SPEED10G;
377 	XMDIO_WRITE(pdata, MDIO_MMD_PCS, MDIO_CTRL1, reg);
378 
379 	xgbe_phy_pcs_power_cycle(pdata);
380 
381 	/* Set SerDes to 10G speed */
382 	xgbe_phy_start_ratechange(pdata);
383 
384 	XSIR1_IOWRITE_BITS(pdata, SIR1_SPEED, DATARATE, XGBE_SPEED_10000_RATE);
385 	XSIR1_IOWRITE_BITS(pdata, SIR1_SPEED, WORDMODE, XGBE_SPEED_10000_WORD);
386 	XSIR1_IOWRITE_BITS(pdata, SIR1_SPEED, PLLSEL, XGBE_SPEED_10000_PLL);
387 
388 	XSIR1_IOWRITE_BITS(pdata, SIR1_SPEED, CDR_RATE,
389 			   phy_data->cdr_rate[XGBE_SPEED_10000]);
390 	XSIR1_IOWRITE_BITS(pdata, SIR1_SPEED, TXAMP,
391 			   phy_data->tx_amp[XGBE_SPEED_10000]);
392 	XRXTX_IOWRITE_BITS(pdata, RXTX_REG20, BLWC_ENA,
393 			   phy_data->blwc[XGBE_SPEED_10000]);
394 	XRXTX_IOWRITE_BITS(pdata, RXTX_REG114, PQ_REG,
395 			   phy_data->pq_skew[XGBE_SPEED_10000]);
396 	XRXTX_IOWRITE_BITS(pdata, RXTX_REG129, RXDFE_CONFIG,
397 			   phy_data->dfe_tap_cfg[XGBE_SPEED_10000]);
398 	XRXTX_IOWRITE(pdata, RXTX_REG22,
399 		      phy_data->dfe_tap_ena[XGBE_SPEED_10000]);
400 
401 	xgbe_phy_complete_ratechange(pdata);
402 
403 	netif_dbg(pdata, link, pdata->netdev, "10GbE KR mode set\n");
404 }
405 
406 static void xgbe_phy_kx_2500_mode(struct xgbe_prv_data *pdata)
407 {
408 	struct xgbe_phy_data *phy_data = pdata->phy_data;
409 	unsigned int reg;
410 
411 	/* Set PCS to KX/1G speed */
412 	reg = XMDIO_READ(pdata, MDIO_MMD_PCS, MDIO_CTRL2);
413 	reg &= ~MDIO_PCS_CTRL2_TYPE;
414 	reg |= MDIO_PCS_CTRL2_10GBX;
415 	XMDIO_WRITE(pdata, MDIO_MMD_PCS, MDIO_CTRL2, reg);
416 
417 	reg = XMDIO_READ(pdata, MDIO_MMD_PCS, MDIO_CTRL1);
418 	reg &= ~MDIO_CTRL1_SPEEDSEL;
419 	reg |= MDIO_CTRL1_SPEED1G;
420 	XMDIO_WRITE(pdata, MDIO_MMD_PCS, MDIO_CTRL1, reg);
421 
422 	xgbe_phy_pcs_power_cycle(pdata);
423 
424 	/* Set SerDes to 2.5G speed */
425 	xgbe_phy_start_ratechange(pdata);
426 
427 	XSIR1_IOWRITE_BITS(pdata, SIR1_SPEED, DATARATE, XGBE_SPEED_2500_RATE);
428 	XSIR1_IOWRITE_BITS(pdata, SIR1_SPEED, WORDMODE, XGBE_SPEED_2500_WORD);
429 	XSIR1_IOWRITE_BITS(pdata, SIR1_SPEED, PLLSEL, XGBE_SPEED_2500_PLL);
430 
431 	XSIR1_IOWRITE_BITS(pdata, SIR1_SPEED, CDR_RATE,
432 			   phy_data->cdr_rate[XGBE_SPEED_2500]);
433 	XSIR1_IOWRITE_BITS(pdata, SIR1_SPEED, TXAMP,
434 			   phy_data->tx_amp[XGBE_SPEED_2500]);
435 	XRXTX_IOWRITE_BITS(pdata, RXTX_REG20, BLWC_ENA,
436 			   phy_data->blwc[XGBE_SPEED_2500]);
437 	XRXTX_IOWRITE_BITS(pdata, RXTX_REG114, PQ_REG,
438 			   phy_data->pq_skew[XGBE_SPEED_2500]);
439 	XRXTX_IOWRITE_BITS(pdata, RXTX_REG129, RXDFE_CONFIG,
440 			   phy_data->dfe_tap_cfg[XGBE_SPEED_2500]);
441 	XRXTX_IOWRITE(pdata, RXTX_REG22,
442 		      phy_data->dfe_tap_ena[XGBE_SPEED_2500]);
443 
444 	xgbe_phy_complete_ratechange(pdata);
445 
446 	netif_dbg(pdata, link, pdata->netdev, "2.5GbE KX mode set\n");
447 }
448 
449 static void xgbe_phy_kx_1000_mode(struct xgbe_prv_data *pdata)
450 {
451 	struct xgbe_phy_data *phy_data = pdata->phy_data;
452 	unsigned int reg;
453 
454 	/* Set PCS to KX/1G speed */
455 	reg = XMDIO_READ(pdata, MDIO_MMD_PCS, MDIO_CTRL2);
456 	reg &= ~MDIO_PCS_CTRL2_TYPE;
457 	reg |= MDIO_PCS_CTRL2_10GBX;
458 	XMDIO_WRITE(pdata, MDIO_MMD_PCS, MDIO_CTRL2, reg);
459 
460 	reg = XMDIO_READ(pdata, MDIO_MMD_PCS, MDIO_CTRL1);
461 	reg &= ~MDIO_CTRL1_SPEEDSEL;
462 	reg |= MDIO_CTRL1_SPEED1G;
463 	XMDIO_WRITE(pdata, MDIO_MMD_PCS, MDIO_CTRL1, reg);
464 
465 	xgbe_phy_pcs_power_cycle(pdata);
466 
467 	/* Set SerDes to 1G speed */
468 	xgbe_phy_start_ratechange(pdata);
469 
470 	XSIR1_IOWRITE_BITS(pdata, SIR1_SPEED, DATARATE, XGBE_SPEED_1000_RATE);
471 	XSIR1_IOWRITE_BITS(pdata, SIR1_SPEED, WORDMODE, XGBE_SPEED_1000_WORD);
472 	XSIR1_IOWRITE_BITS(pdata, SIR1_SPEED, PLLSEL, XGBE_SPEED_1000_PLL);
473 
474 	XSIR1_IOWRITE_BITS(pdata, SIR1_SPEED, CDR_RATE,
475 			   phy_data->cdr_rate[XGBE_SPEED_1000]);
476 	XSIR1_IOWRITE_BITS(pdata, SIR1_SPEED, TXAMP,
477 			   phy_data->tx_amp[XGBE_SPEED_1000]);
478 	XRXTX_IOWRITE_BITS(pdata, RXTX_REG20, BLWC_ENA,
479 			   phy_data->blwc[XGBE_SPEED_1000]);
480 	XRXTX_IOWRITE_BITS(pdata, RXTX_REG114, PQ_REG,
481 			   phy_data->pq_skew[XGBE_SPEED_1000]);
482 	XRXTX_IOWRITE_BITS(pdata, RXTX_REG129, RXDFE_CONFIG,
483 			   phy_data->dfe_tap_cfg[XGBE_SPEED_1000]);
484 	XRXTX_IOWRITE(pdata, RXTX_REG22,
485 		      phy_data->dfe_tap_ena[XGBE_SPEED_1000]);
486 
487 	xgbe_phy_complete_ratechange(pdata);
488 
489 	netif_dbg(pdata, link, pdata->netdev, "1GbE KX mode set\n");
490 }
491 
492 static enum xgbe_mode xgbe_phy_cur_mode(struct xgbe_prv_data *pdata)
493 {
494 	struct xgbe_phy_data *phy_data = pdata->phy_data;
495 	enum xgbe_mode mode;
496 	unsigned int reg;
497 
498 	reg = XMDIO_READ(pdata, MDIO_MMD_PCS, MDIO_CTRL2);
499 	reg &= MDIO_PCS_CTRL2_TYPE;
500 
501 	if (reg == MDIO_PCS_CTRL2_10GBR) {
502 		mode = XGBE_MODE_KR;
503 	} else {
504 		if (phy_data->speed_set == XGBE_SPEEDSET_2500_10000)
505 			mode = XGBE_MODE_KX_2500;
506 		else
507 			mode = XGBE_MODE_KX_1000;
508 	}
509 
510 	return mode;
511 }
512 
513 static enum xgbe_mode xgbe_phy_switch_mode(struct xgbe_prv_data *pdata)
514 {
515 	struct xgbe_phy_data *phy_data = pdata->phy_data;
516 	enum xgbe_mode mode;
517 
518 	/* If we are in KR switch to KX, and vice-versa */
519 	if (xgbe_phy_cur_mode(pdata) == XGBE_MODE_KR) {
520 		if (phy_data->speed_set == XGBE_SPEEDSET_2500_10000)
521 			mode = XGBE_MODE_KX_2500;
522 		else
523 			mode = XGBE_MODE_KX_1000;
524 	} else {
525 		mode = XGBE_MODE_KR;
526 	}
527 
528 	return mode;
529 }
530 
531 static enum xgbe_mode xgbe_phy_get_mode(struct xgbe_prv_data *pdata,
532 					int speed)
533 {
534 	struct xgbe_phy_data *phy_data = pdata->phy_data;
535 
536 	switch (speed) {
537 	case SPEED_1000:
538 		return (phy_data->speed_set == XGBE_SPEEDSET_1000_10000)
539 			? XGBE_MODE_KX_1000 : XGBE_MODE_UNKNOWN;
540 	case SPEED_2500:
541 		return (phy_data->speed_set == XGBE_SPEEDSET_2500_10000)
542 			? XGBE_MODE_KX_2500 : XGBE_MODE_UNKNOWN;
543 	case SPEED_10000:
544 		return XGBE_MODE_KR;
545 	default:
546 		return XGBE_MODE_UNKNOWN;
547 	}
548 }
549 
550 static void xgbe_phy_set_mode(struct xgbe_prv_data *pdata, enum xgbe_mode mode)
551 {
552 	switch (mode) {
553 	case XGBE_MODE_KX_1000:
554 		xgbe_phy_kx_1000_mode(pdata);
555 		break;
556 	case XGBE_MODE_KX_2500:
557 		xgbe_phy_kx_2500_mode(pdata);
558 		break;
559 	case XGBE_MODE_KR:
560 		xgbe_phy_kr_mode(pdata);
561 		break;
562 	default:
563 		break;
564 	}
565 }
566 
567 static bool xgbe_phy_check_mode(struct xgbe_prv_data *pdata,
568 				enum xgbe_mode mode, u32 advert)
569 {
570 	if (pdata->phy.autoneg == AUTONEG_ENABLE) {
571 		if (pdata->phy.advertising & advert)
572 			return true;
573 	} else {
574 		enum xgbe_mode cur_mode;
575 
576 		cur_mode = xgbe_phy_get_mode(pdata, pdata->phy.speed);
577 		if (cur_mode == mode)
578 			return true;
579 	}
580 
581 	return false;
582 }
583 
584 static bool xgbe_phy_use_mode(struct xgbe_prv_data *pdata, enum xgbe_mode mode)
585 {
586 	switch (mode) {
587 	case XGBE_MODE_KX_1000:
588 		return xgbe_phy_check_mode(pdata, mode,
589 					   ADVERTISED_1000baseKX_Full);
590 	case XGBE_MODE_KX_2500:
591 		return xgbe_phy_check_mode(pdata, mode,
592 					   ADVERTISED_2500baseX_Full);
593 	case XGBE_MODE_KR:
594 		return xgbe_phy_check_mode(pdata, mode,
595 					   ADVERTISED_10000baseKR_Full);
596 	default:
597 		return false;
598 	}
599 }
600 
601 static bool xgbe_phy_valid_speed(struct xgbe_prv_data *pdata, int speed)
602 {
603 	struct xgbe_phy_data *phy_data = pdata->phy_data;
604 
605 	switch (speed) {
606 	case SPEED_1000:
607 		if (phy_data->speed_set != XGBE_SPEEDSET_1000_10000)
608 			return false;
609 		return true;
610 	case SPEED_2500:
611 		if (phy_data->speed_set != XGBE_SPEEDSET_2500_10000)
612 			return false;
613 		return true;
614 	case SPEED_10000:
615 		return true;
616 	default:
617 		return false;
618 	}
619 }
620 
621 static int xgbe_phy_link_status(struct xgbe_prv_data *pdata, int *an_restart)
622 {
623 	unsigned int reg;
624 
625 	*an_restart = 0;
626 
627 	/* Link status is latched low, so read once to clear
628 	 * and then read again to get current state
629 	 */
630 	reg = XMDIO_READ(pdata, MDIO_MMD_PCS, MDIO_STAT1);
631 	reg = XMDIO_READ(pdata, MDIO_MMD_PCS, MDIO_STAT1);
632 
633 	return (reg & MDIO_STAT1_LSTATUS) ? 1 : 0;
634 }
635 
636 static void xgbe_phy_stop(struct xgbe_prv_data *pdata)
637 {
638 	/* Nothing uniquely required for stop */
639 }
640 
641 static int xgbe_phy_start(struct xgbe_prv_data *pdata)
642 {
643 	/* Nothing uniquely required for start */
644 	return 0;
645 }
646 
647 static int xgbe_phy_reset(struct xgbe_prv_data *pdata)
648 {
649 	unsigned int reg, count;
650 
651 	/* Perform a software reset of the PCS */
652 	reg = XMDIO_READ(pdata, MDIO_MMD_PCS, MDIO_CTRL1);
653 	reg |= MDIO_CTRL1_RESET;
654 	XMDIO_WRITE(pdata, MDIO_MMD_PCS, MDIO_CTRL1, reg);
655 
656 	count = 50;
657 	do {
658 		msleep(20);
659 		reg = XMDIO_READ(pdata, MDIO_MMD_PCS, MDIO_CTRL1);
660 	} while ((reg & MDIO_CTRL1_RESET) && --count);
661 
662 	if (reg & MDIO_CTRL1_RESET)
663 		return -ETIMEDOUT;
664 
665 	return 0;
666 }
667 
668 static void xgbe_phy_exit(struct xgbe_prv_data *pdata)
669 {
670 	/* Nothing uniquely required for exit */
671 }
672 
673 static int xgbe_phy_init(struct xgbe_prv_data *pdata)
674 {
675 	struct xgbe_phy_data *phy_data;
676 	int ret;
677 
678 	phy_data = devm_kzalloc(pdata->dev, sizeof(*phy_data), GFP_KERNEL);
679 	if (!phy_data)
680 		return -ENOMEM;
681 
682 	/* Retrieve the PHY speedset */
683 	ret = device_property_read_u32(pdata->phy_dev, XGBE_SPEEDSET_PROPERTY,
684 				       &phy_data->speed_set);
685 	if (ret) {
686 		dev_err(pdata->dev, "invalid %s property\n",
687 			XGBE_SPEEDSET_PROPERTY);
688 		return ret;
689 	}
690 
691 	switch (phy_data->speed_set) {
692 	case XGBE_SPEEDSET_1000_10000:
693 	case XGBE_SPEEDSET_2500_10000:
694 		break;
695 	default:
696 		dev_err(pdata->dev, "invalid %s property\n",
697 			XGBE_SPEEDSET_PROPERTY);
698 		return -EINVAL;
699 	}
700 
701 	/* Retrieve the PHY configuration properties */
702 	if (device_property_present(pdata->phy_dev, XGBE_BLWC_PROPERTY)) {
703 		ret = device_property_read_u32_array(pdata->phy_dev,
704 						     XGBE_BLWC_PROPERTY,
705 						     phy_data->blwc,
706 						     XGBE_SPEEDS);
707 		if (ret) {
708 			dev_err(pdata->dev, "invalid %s property\n",
709 				XGBE_BLWC_PROPERTY);
710 			return ret;
711 		}
712 	} else {
713 		memcpy(phy_data->blwc, xgbe_phy_blwc,
714 		       sizeof(phy_data->blwc));
715 	}
716 
717 	if (device_property_present(pdata->phy_dev, XGBE_CDR_RATE_PROPERTY)) {
718 		ret = device_property_read_u32_array(pdata->phy_dev,
719 						     XGBE_CDR_RATE_PROPERTY,
720 						     phy_data->cdr_rate,
721 						     XGBE_SPEEDS);
722 		if (ret) {
723 			dev_err(pdata->dev, "invalid %s property\n",
724 				XGBE_CDR_RATE_PROPERTY);
725 			return ret;
726 		}
727 	} else {
728 		memcpy(phy_data->cdr_rate, xgbe_phy_cdr_rate,
729 		       sizeof(phy_data->cdr_rate));
730 	}
731 
732 	if (device_property_present(pdata->phy_dev, XGBE_PQ_SKEW_PROPERTY)) {
733 		ret = device_property_read_u32_array(pdata->phy_dev,
734 						     XGBE_PQ_SKEW_PROPERTY,
735 						     phy_data->pq_skew,
736 						     XGBE_SPEEDS);
737 		if (ret) {
738 			dev_err(pdata->dev, "invalid %s property\n",
739 				XGBE_PQ_SKEW_PROPERTY);
740 			return ret;
741 		}
742 	} else {
743 		memcpy(phy_data->pq_skew, xgbe_phy_pq_skew,
744 		       sizeof(phy_data->pq_skew));
745 	}
746 
747 	if (device_property_present(pdata->phy_dev, XGBE_TX_AMP_PROPERTY)) {
748 		ret = device_property_read_u32_array(pdata->phy_dev,
749 						     XGBE_TX_AMP_PROPERTY,
750 						     phy_data->tx_amp,
751 						     XGBE_SPEEDS);
752 		if (ret) {
753 			dev_err(pdata->dev, "invalid %s property\n",
754 				XGBE_TX_AMP_PROPERTY);
755 			return ret;
756 		}
757 	} else {
758 		memcpy(phy_data->tx_amp, xgbe_phy_tx_amp,
759 		       sizeof(phy_data->tx_amp));
760 	}
761 
762 	if (device_property_present(pdata->phy_dev, XGBE_DFE_CFG_PROPERTY)) {
763 		ret = device_property_read_u32_array(pdata->phy_dev,
764 						     XGBE_DFE_CFG_PROPERTY,
765 						     phy_data->dfe_tap_cfg,
766 						     XGBE_SPEEDS);
767 		if (ret) {
768 			dev_err(pdata->dev, "invalid %s property\n",
769 				XGBE_DFE_CFG_PROPERTY);
770 			return ret;
771 		}
772 	} else {
773 		memcpy(phy_data->dfe_tap_cfg, xgbe_phy_dfe_tap_cfg,
774 		       sizeof(phy_data->dfe_tap_cfg));
775 	}
776 
777 	if (device_property_present(pdata->phy_dev, XGBE_DFE_ENA_PROPERTY)) {
778 		ret = device_property_read_u32_array(pdata->phy_dev,
779 						     XGBE_DFE_ENA_PROPERTY,
780 						     phy_data->dfe_tap_ena,
781 						     XGBE_SPEEDS);
782 		if (ret) {
783 			dev_err(pdata->dev, "invalid %s property\n",
784 				XGBE_DFE_ENA_PROPERTY);
785 			return ret;
786 		}
787 	} else {
788 		memcpy(phy_data->dfe_tap_ena, xgbe_phy_dfe_tap_ena,
789 		       sizeof(phy_data->dfe_tap_ena));
790 	}
791 
792 	/* Initialize supported features */
793 	pdata->phy.supported = SUPPORTED_Autoneg;
794 	pdata->phy.supported |= SUPPORTED_Pause | SUPPORTED_Asym_Pause;
795 	pdata->phy.supported |= SUPPORTED_Backplane;
796 	pdata->phy.supported |= SUPPORTED_10000baseKR_Full;
797 	switch (phy_data->speed_set) {
798 	case XGBE_SPEEDSET_1000_10000:
799 		pdata->phy.supported |= SUPPORTED_1000baseKX_Full;
800 		break;
801 	case XGBE_SPEEDSET_2500_10000:
802 		pdata->phy.supported |= SUPPORTED_2500baseX_Full;
803 		break;
804 	}
805 
806 	if (pdata->fec_ability & MDIO_PMA_10GBR_FECABLE_ABLE)
807 		pdata->phy.supported |= SUPPORTED_10000baseR_FEC;
808 
809 	pdata->phy_data = phy_data;
810 
811 	return 0;
812 }
813 
814 void xgbe_init_function_ptrs_phy_v1(struct xgbe_phy_if *phy_if)
815 {
816 	struct xgbe_phy_impl_if *phy_impl = &phy_if->phy_impl;
817 
818 	phy_impl->init			= xgbe_phy_init;
819 	phy_impl->exit			= xgbe_phy_exit;
820 
821 	phy_impl->reset			= xgbe_phy_reset;
822 	phy_impl->start			= xgbe_phy_start;
823 	phy_impl->stop			= xgbe_phy_stop;
824 
825 	phy_impl->link_status		= xgbe_phy_link_status;
826 
827 	phy_impl->valid_speed		= xgbe_phy_valid_speed;
828 
829 	phy_impl->use_mode		= xgbe_phy_use_mode;
830 	phy_impl->set_mode		= xgbe_phy_set_mode;
831 	phy_impl->get_mode		= xgbe_phy_get_mode;
832 	phy_impl->switch_mode		= xgbe_phy_switch_mode;
833 	phy_impl->cur_mode		= xgbe_phy_cur_mode;
834 
835 	phy_impl->an_mode		= xgbe_phy_an_mode;
836 
837 	phy_impl->an_config		= xgbe_phy_an_config;
838 
839 	phy_impl->an_advertising	= xgbe_phy_an_advertising;
840 
841 	phy_impl->an_outcome		= xgbe_phy_an_outcome;
842 
843 	phy_impl->kr_training_pre	= xgbe_phy_kr_training_pre;
844 	phy_impl->kr_training_post	= xgbe_phy_kr_training_post;
845 }
846