blob: 4b95caf08aa9a3ad7a43ba8eff390d3e1dc727d8 [file] [log] [blame]
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001/*
2 * AMD 10Gb Ethernet PHY 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) 2014 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 *
25 * License 2: Modified BSD
26 *
27 * Copyright (c) 2014 Advanced Micro Devices, Inc.
28 * All rights reserved.
29 *
30 * Redistribution and use in source and binary forms, with or without
31 * modification, are permitted provided that the following conditions are met:
32 * * Redistributions of source code must retain the above copyright
33 * notice, this list of conditions and the following disclaimer.
34 * * Redistributions in binary form must reproduce the above copyright
35 * notice, this list of conditions and the following disclaimer in the
36 * documentation and/or other materials provided with the distribution.
37 * * Neither the name of Advanced Micro Devices, Inc. nor the
38 * names of its contributors may be used to endorse or promote products
39 * derived from this software without specific prior written permission.
40 *
41 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
42 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
45 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
46 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
48 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
49 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
50 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
51 */
52
53#include <linux/kernel.h>
54#include <linux/device.h>
55#include <linux/platform_device.h>
56#include <linux/string.h>
57#include <linux/errno.h>
58#include <linux/unistd.h>
59#include <linux/slab.h>
60#include <linux/interrupt.h>
61#include <linux/init.h>
62#include <linux/delay.h>
Lendacky, Thomasc3152d42015-01-16 12:47:00 -060063#include <linux/workqueue.h>
Lendacky, Thomas4d874b32014-06-05 09:15:12 -050064#include <linux/netdevice.h>
65#include <linux/etherdevice.h>
66#include <linux/skbuff.h>
67#include <linux/mm.h>
68#include <linux/module.h>
69#include <linux/mii.h>
70#include <linux/ethtool.h>
71#include <linux/phy.h>
72#include <linux/mdio.h>
73#include <linux/io.h>
74#include <linux/of.h>
75#include <linux/of_platform.h>
76#include <linux/of_device.h>
77#include <linux/uaccess.h>
Lendacky, Thomascb69cb02015-01-16 12:46:29 -060078#include <linux/bitops.h>
Lendacky, Thomas82a19032015-01-16 12:47:16 -060079#include <linux/property.h>
80#include <linux/acpi.h>
Lendacky, Thomas4d874b32014-06-05 09:15:12 -050081
Lendacky, Thomas4d874b32014-06-05 09:15:12 -050082MODULE_AUTHOR("Tom Lendacky <thomas.lendacky@amd.com>");
83MODULE_LICENSE("Dual BSD/GPL");
84MODULE_VERSION("1.0.0-a");
85MODULE_DESCRIPTION("AMD 10GbE (amd-xgbe) PHY driver");
86
87#define XGBE_PHY_ID 0x000162d0
88#define XGBE_PHY_MASK 0xfffffff0
89
Lendacky, Thomasf0476042014-07-29 08:57:25 -050090#define XGBE_PHY_SPEEDSET_PROPERTY "amd,speed-set"
Lendacky, Thomas8fdb1a092015-01-16 12:47:21 -060091#define XGBE_PHY_BLWC_PROPERTY "amd,serdes-blwc"
92#define XGBE_PHY_CDR_RATE_PROPERTY "amd,serdes-cdr-rate"
93#define XGBE_PHY_PQ_SKEW_PROPERTY "amd,serdes-pq-skew"
94#define XGBE_PHY_TX_AMP_PROPERTY "amd,serdes-tx-amp"
Tom Lendacky74ad7522015-02-24 10:47:49 -060095#define XGBE_PHY_DFE_CFG_PROPERTY "amd,serdes-dfe-tap-config"
96#define XGBE_PHY_DFE_ENA_PROPERTY "amd,serdes-dfe-tap-enable"
Lendacky, Thomas8fdb1a092015-01-16 12:47:21 -060097
98#define XGBE_PHY_SPEEDS 3
99#define XGBE_PHY_SPEED_1000 0
100#define XGBE_PHY_SPEED_2500 1
101#define XGBE_PHY_SPEED_10000 2
Lendacky, Thomasf0476042014-07-29 08:57:25 -0500102
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500103#define XGBE_AN_INT_CMPLT 0x01
104#define XGBE_AN_INC_LINK 0x02
105#define XGBE_AN_PG_RCV 0x04
Lendacky, Thomasc3152d42015-01-16 12:47:00 -0600106#define XGBE_AN_INT_MASK 0x07
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500107
108#define XNP_MCF_NULL_MESSAGE 0x001
Lendacky, Thomascb69cb02015-01-16 12:46:29 -0600109#define XNP_ACK_PROCESSED BIT(12)
110#define XNP_MP_FORMATTED BIT(13)
111#define XNP_NP_EXCHANGE BIT(15)
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500112
Lendacky, Thomas1fa1f2e2014-08-01 11:56:36 -0500113#define XGBE_PHY_RATECHANGE_COUNT 500
Lendacky, Thomas169a6302014-07-29 08:57:37 -0500114
Lendacky, Thomascf262522015-01-16 12:47:05 -0600115#define XGBE_PHY_KR_TRAINING_START 0x01
116#define XGBE_PHY_KR_TRAINING_ENABLE 0x02
117
118#define XGBE_PHY_FEC_ENABLE 0x01
119#define XGBE_PHY_FEC_FORWARD 0x02
120#define XGBE_PHY_FEC_MASK 0x03
121
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500122#ifndef MDIO_PMA_10GBR_PMD_CTRL
123#define MDIO_PMA_10GBR_PMD_CTRL 0x0096
124#endif
Lendacky, Thomase6f05622014-09-03 12:14:22 -0500125
Lendacky, Thomascf262522015-01-16 12:47:05 -0600126#ifndef MDIO_PMA_10GBR_FEC_ABILITY
127#define MDIO_PMA_10GBR_FEC_ABILITY 0x00aa
128#endif
129
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500130#ifndef MDIO_PMA_10GBR_FEC_CTRL
131#define MDIO_PMA_10GBR_FEC_CTRL 0x00ab
132#endif
Lendacky, Thomase6f05622014-09-03 12:14:22 -0500133
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500134#ifndef MDIO_AN_XNP
135#define MDIO_AN_XNP 0x0016
136#endif
137
Lendacky, Thomas0d40b612015-01-16 12:47:10 -0600138#ifndef MDIO_AN_LPX
139#define MDIO_AN_LPX 0x0019
140#endif
141
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500142#ifndef MDIO_AN_INTMASK
143#define MDIO_AN_INTMASK 0x8001
144#endif
Lendacky, Thomase6f05622014-09-03 12:14:22 -0500145
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500146#ifndef MDIO_AN_INT
147#define MDIO_AN_INT 0x8002
148#endif
149
150#ifndef MDIO_CTRL1_SPEED1G
151#define MDIO_CTRL1_SPEED1G (MDIO_CTRL1_SPEED10G & ~BMCR_SPEED100)
152#endif
153
154/* SerDes integration register offsets */
Lendacky, Thomas5c10e5c2014-07-29 08:57:43 -0500155#define SIR0_KR_RT_1 0x002c
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500156#define SIR0_STATUS 0x0040
157#define SIR1_SPEED 0x0000
158
159/* SerDes integration register entry bit positions and sizes */
Lendacky, Thomas5c10e5c2014-07-29 08:57:43 -0500160#define SIR0_KR_RT_1_RESET_INDEX 11
161#define SIR0_KR_RT_1_RESET_WIDTH 1
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500162#define SIR0_STATUS_RX_READY_INDEX 0
163#define SIR0_STATUS_RX_READY_WIDTH 1
164#define SIR0_STATUS_TX_READY_INDEX 8
165#define SIR0_STATUS_TX_READY_WIDTH 1
Lendacky, Thomas8fdb1a092015-01-16 12:47:21 -0600166#define SIR1_SPEED_CDR_RATE_INDEX 12
167#define SIR1_SPEED_CDR_RATE_WIDTH 4
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500168#define SIR1_SPEED_DATARATE_INDEX 4
169#define SIR1_SPEED_DATARATE_WIDTH 2
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500170#define SIR1_SPEED_PLLSEL_INDEX 3
171#define SIR1_SPEED_PLLSEL_WIDTH 1
172#define SIR1_SPEED_RATECHANGE_INDEX 6
173#define SIR1_SPEED_RATECHANGE_WIDTH 1
174#define SIR1_SPEED_TXAMP_INDEX 8
175#define SIR1_SPEED_TXAMP_WIDTH 4
176#define SIR1_SPEED_WORDMODE_INDEX 0
177#define SIR1_SPEED_WORDMODE_WIDTH 3
178
Lendacky, Thomas8fdb1a092015-01-16 12:47:21 -0600179#define SPEED_10000_BLWC 0
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500180#define SPEED_10000_CDR 0x7
181#define SPEED_10000_PLL 0x1
Tom Lendacky74ad7522015-02-24 10:47:49 -0600182#define SPEED_10000_PQ 0x12
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500183#define SPEED_10000_RATE 0x0
184#define SPEED_10000_TXAMP 0xa
185#define SPEED_10000_WORD 0x7
Tom Lendacky74ad7522015-02-24 10:47:49 -0600186#define SPEED_10000_DFE_TAP_CONFIG 0x1
187#define SPEED_10000_DFE_TAP_ENABLE 0x7f
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500188
Lendacky, Thomas8fdb1a092015-01-16 12:47:21 -0600189#define SPEED_2500_BLWC 1
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500190#define SPEED_2500_CDR 0x2
191#define SPEED_2500_PLL 0x0
Lendacky, Thomas8fdb1a092015-01-16 12:47:21 -0600192#define SPEED_2500_PQ 0xa
Lendacky, Thomasf0476042014-07-29 08:57:25 -0500193#define SPEED_2500_RATE 0x1
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500194#define SPEED_2500_TXAMP 0xf
195#define SPEED_2500_WORD 0x1
Tom Lendacky74ad7522015-02-24 10:47:49 -0600196#define SPEED_2500_DFE_TAP_CONFIG 0x3
197#define SPEED_2500_DFE_TAP_ENABLE 0x0
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500198
Lendacky, Thomas8fdb1a092015-01-16 12:47:21 -0600199#define SPEED_1000_BLWC 1
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500200#define SPEED_1000_CDR 0x2
201#define SPEED_1000_PLL 0x0
Lendacky, Thomas8fdb1a092015-01-16 12:47:21 -0600202#define SPEED_1000_PQ 0xa
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500203#define SPEED_1000_RATE 0x3
204#define SPEED_1000_TXAMP 0xf
205#define SPEED_1000_WORD 0x1
Tom Lendacky74ad7522015-02-24 10:47:49 -0600206#define SPEED_1000_DFE_TAP_CONFIG 0x3
207#define SPEED_1000_DFE_TAP_ENABLE 0x0
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500208
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500209/* SerDes RxTx register offsets */
Tom Lendacky74ad7522015-02-24 10:47:49 -0600210#define RXTX_REG6 0x0018
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500211#define RXTX_REG20 0x0050
Tom Lendacky74ad7522015-02-24 10:47:49 -0600212#define RXTX_REG22 0x0058
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500213#define RXTX_REG114 0x01c8
Tom Lendacky74ad7522015-02-24 10:47:49 -0600214#define RXTX_REG129 0x0204
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500215
216/* SerDes RxTx register entry bit positions and sizes */
Tom Lendacky74ad7522015-02-24 10:47:49 -0600217#define RXTX_REG6_RESETB_RXD_INDEX 8
218#define RXTX_REG6_RESETB_RXD_WIDTH 1
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500219#define RXTX_REG20_BLWC_ENA_INDEX 2
220#define RXTX_REG20_BLWC_ENA_WIDTH 1
221#define RXTX_REG114_PQ_REG_INDEX 9
222#define RXTX_REG114_PQ_REG_WIDTH 7
Tom Lendacky74ad7522015-02-24 10:47:49 -0600223#define RXTX_REG129_RXDFE_CONFIG_INDEX 14
224#define RXTX_REG129_RXDFE_CONFIG_WIDTH 2
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500225
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500226/* Bit setting and getting macros
227 * The get macro will extract the current bit field value from within
228 * the variable
229 *
230 * The set macro will clear the current bit field value within the
231 * variable and then set the bit field of the variable to the
232 * specified value
233 */
234#define GET_BITS(_var, _index, _width) \
235 (((_var) >> (_index)) & ((0x1 << (_width)) - 1))
236
237#define SET_BITS(_var, _index, _width, _val) \
238do { \
239 (_var) &= ~(((0x1 << (_width)) - 1) << (_index)); \
240 (_var) |= (((_val) & ((0x1 << (_width)) - 1)) << (_index)); \
241} while (0)
242
Lendacky, Thomas169a6302014-07-29 08:57:37 -0500243#define XSIR_GET_BITS(_var, _prefix, _field) \
244 GET_BITS((_var), \
245 _prefix##_##_field##_INDEX, \
246 _prefix##_##_field##_WIDTH)
247
248#define XSIR_SET_BITS(_var, _prefix, _field, _val) \
249 SET_BITS((_var), \
250 _prefix##_##_field##_INDEX, \
251 _prefix##_##_field##_WIDTH, (_val))
252
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500253/* Macros for reading or writing SerDes integration registers
254 * The ioread macros will get bit fields or full values using the
255 * register definitions formed using the input names
256 *
257 * The iowrite macros will set bit fields or full values using the
258 * register definitions formed using the input names
259 */
260#define XSIR0_IOREAD(_priv, _reg) \
261 ioread16((_priv)->sir0_regs + _reg)
262
263#define XSIR0_IOREAD_BITS(_priv, _reg, _field) \
264 GET_BITS(XSIR0_IOREAD((_priv), _reg), \
265 _reg##_##_field##_INDEX, \
266 _reg##_##_field##_WIDTH)
267
268#define XSIR0_IOWRITE(_priv, _reg, _val) \
269 iowrite16((_val), (_priv)->sir0_regs + _reg)
270
271#define XSIR0_IOWRITE_BITS(_priv, _reg, _field, _val) \
272do { \
273 u16 reg_val = XSIR0_IOREAD((_priv), _reg); \
274 SET_BITS(reg_val, \
275 _reg##_##_field##_INDEX, \
276 _reg##_##_field##_WIDTH, (_val)); \
277 XSIR0_IOWRITE((_priv), _reg, reg_val); \
278} while (0)
279
280#define XSIR1_IOREAD(_priv, _reg) \
281 ioread16((_priv)->sir1_regs + _reg)
282
283#define XSIR1_IOREAD_BITS(_priv, _reg, _field) \
284 GET_BITS(XSIR1_IOREAD((_priv), _reg), \
285 _reg##_##_field##_INDEX, \
286 _reg##_##_field##_WIDTH)
287
288#define XSIR1_IOWRITE(_priv, _reg, _val) \
289 iowrite16((_val), (_priv)->sir1_regs + _reg)
290
291#define XSIR1_IOWRITE_BITS(_priv, _reg, _field, _val) \
292do { \
293 u16 reg_val = XSIR1_IOREAD((_priv), _reg); \
294 SET_BITS(reg_val, \
295 _reg##_##_field##_INDEX, \
296 _reg##_##_field##_WIDTH, (_val)); \
297 XSIR1_IOWRITE((_priv), _reg, reg_val); \
298} while (0)
299
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500300/* Macros for reading or writing SerDes RxTx registers
301 * The ioread macros will get bit fields or full values using the
302 * register definitions formed using the input names
303 *
304 * The iowrite macros will set bit fields or full values using the
305 * register definitions formed using the input names
306 */
307#define XRXTX_IOREAD(_priv, _reg) \
308 ioread16((_priv)->rxtx_regs + _reg)
309
310#define XRXTX_IOREAD_BITS(_priv, _reg, _field) \
311 GET_BITS(XRXTX_IOREAD((_priv), _reg), \
312 _reg##_##_field##_INDEX, \
313 _reg##_##_field##_WIDTH)
314
315#define XRXTX_IOWRITE(_priv, _reg, _val) \
316 iowrite16((_val), (_priv)->rxtx_regs + _reg)
317
318#define XRXTX_IOWRITE_BITS(_priv, _reg, _field, _val) \
319do { \
320 u16 reg_val = XRXTX_IOREAD((_priv), _reg); \
321 SET_BITS(reg_val, \
322 _reg##_##_field##_INDEX, \
323 _reg##_##_field##_WIDTH, (_val)); \
324 XRXTX_IOWRITE((_priv), _reg, reg_val); \
325} while (0)
326
Lendacky, Thomas8fdb1a092015-01-16 12:47:21 -0600327static const u32 amd_xgbe_phy_serdes_blwc[] = {
328 SPEED_1000_BLWC,
329 SPEED_2500_BLWC,
330 SPEED_10000_BLWC,
331};
332
333static const u32 amd_xgbe_phy_serdes_cdr_rate[] = {
334 SPEED_1000_CDR,
335 SPEED_2500_CDR,
336 SPEED_10000_CDR,
337};
338
339static const u32 amd_xgbe_phy_serdes_pq_skew[] = {
340 SPEED_1000_PQ,
341 SPEED_2500_PQ,
342 SPEED_10000_PQ,
343};
344
345static const u32 amd_xgbe_phy_serdes_tx_amp[] = {
346 SPEED_1000_TXAMP,
347 SPEED_2500_TXAMP,
348 SPEED_10000_TXAMP,
349};
350
Tom Lendacky74ad7522015-02-24 10:47:49 -0600351static const u32 amd_xgbe_phy_serdes_dfe_tap_cfg[] = {
352 SPEED_1000_DFE_TAP_CONFIG,
353 SPEED_2500_DFE_TAP_CONFIG,
354 SPEED_10000_DFE_TAP_CONFIG,
355};
356
357static const u32 amd_xgbe_phy_serdes_dfe_tap_ena[] = {
358 SPEED_1000_DFE_TAP_ENABLE,
359 SPEED_2500_DFE_TAP_ENABLE,
360 SPEED_10000_DFE_TAP_ENABLE,
361};
362
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500363enum amd_xgbe_phy_an {
364 AMD_XGBE_AN_READY = 0,
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500365 AMD_XGBE_AN_PAGE_RECEIVED,
366 AMD_XGBE_AN_INCOMPAT_LINK,
367 AMD_XGBE_AN_COMPLETE,
368 AMD_XGBE_AN_NO_LINK,
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500369 AMD_XGBE_AN_ERROR,
370};
371
372enum amd_xgbe_phy_rx {
Lendacky, Thomasc3152d42015-01-16 12:47:00 -0600373 AMD_XGBE_RX_BPA = 0,
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500374 AMD_XGBE_RX_XNP,
375 AMD_XGBE_RX_COMPLETE,
Lendacky, Thomasc3152d42015-01-16 12:47:00 -0600376 AMD_XGBE_RX_ERROR,
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500377};
378
379enum amd_xgbe_phy_mode {
380 AMD_XGBE_MODE_KR,
381 AMD_XGBE_MODE_KX,
382};
383
Lendacky, Thomasf0476042014-07-29 08:57:25 -0500384enum amd_xgbe_phy_speedset {
Lendacky, Thomas82a19032015-01-16 12:47:16 -0600385 AMD_XGBE_PHY_SPEEDSET_1000_10000 = 0,
Lendacky, Thomasf0476042014-07-29 08:57:25 -0500386 AMD_XGBE_PHY_SPEEDSET_2500_10000,
387};
388
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500389struct amd_xgbe_phy_priv {
390 struct platform_device *pdev;
Lendacky, Thomas82a19032015-01-16 12:47:16 -0600391 struct acpi_device *adev;
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500392 struct device *dev;
393
394 struct phy_device *phydev;
395
396 /* SerDes related mmio resources */
397 struct resource *rxtx_res;
398 struct resource *sir0_res;
399 struct resource *sir1_res;
400
401 /* SerDes related mmio registers */
402 void __iomem *rxtx_regs; /* SerDes Rx/Tx CSRs */
403 void __iomem *sir0_regs; /* SerDes integration registers (1/2) */
404 void __iomem *sir1_regs; /* SerDes integration registers (2/2) */
405
Lendacky, Thomasc3152d42015-01-16 12:47:00 -0600406 int an_irq;
407 char an_irq_name[IFNAMSIZ + 32];
408 struct work_struct an_irq_work;
409 unsigned int an_irq_allocated;
410
Lendacky, Thomasf0476042014-07-29 08:57:25 -0500411 unsigned int speed_set;
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500412
Lendacky, Thomas8fdb1a092015-01-16 12:47:21 -0600413 /* SerDes UEFI configurable settings.
414 * Switching between modes/speeds requires new values for some
415 * SerDes settings. The values can be supplied as device
416 * properties in array format. The first array entry is for
417 * 1GbE, second for 2.5GbE and third for 10GbE
418 */
419 u32 serdes_blwc[XGBE_PHY_SPEEDS];
420 u32 serdes_cdr_rate[XGBE_PHY_SPEEDS];
421 u32 serdes_pq_skew[XGBE_PHY_SPEEDS];
422 u32 serdes_tx_amp[XGBE_PHY_SPEEDS];
Tom Lendacky74ad7522015-02-24 10:47:49 -0600423 u32 serdes_dfe_tap_cfg[XGBE_PHY_SPEEDS];
424 u32 serdes_dfe_tap_ena[XGBE_PHY_SPEEDS];
Lendacky, Thomas8fdb1a092015-01-16 12:47:21 -0600425
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500426 /* Auto-negotiation state machine support */
427 struct mutex an_mutex;
428 enum amd_xgbe_phy_an an_result;
429 enum amd_xgbe_phy_an an_state;
430 enum amd_xgbe_phy_rx kr_state;
431 enum amd_xgbe_phy_rx kx_state;
432 struct work_struct an_work;
433 struct workqueue_struct *an_workqueue;
Lendacky, Thomasc3152d42015-01-16 12:47:00 -0600434 unsigned int an_supported;
Lendacky, Thomase6f05622014-09-03 12:14:22 -0500435 unsigned int parallel_detect;
Lendacky, Thomascf262522015-01-16 12:47:05 -0600436 unsigned int fec_ability;
Lendacky, Thomas03e50fd2015-01-16 12:46:39 -0600437
438 unsigned int lpm_ctrl; /* CTRL1 for resume */
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500439};
440
441static int amd_xgbe_an_enable_kr_training(struct phy_device *phydev)
442{
443 int ret;
444
445 ret = phy_read_mmd(phydev, MDIO_MMD_PMAPMD, MDIO_PMA_10GBR_PMD_CTRL);
446 if (ret < 0)
447 return ret;
448
Lendacky, Thomascf262522015-01-16 12:47:05 -0600449 ret |= XGBE_PHY_KR_TRAINING_ENABLE;
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500450 phy_write_mmd(phydev, MDIO_MMD_PMAPMD, MDIO_PMA_10GBR_PMD_CTRL, ret);
451
452 return 0;
453}
454
455static int amd_xgbe_an_disable_kr_training(struct phy_device *phydev)
456{
457 int ret;
458
459 ret = phy_read_mmd(phydev, MDIO_MMD_PMAPMD, MDIO_PMA_10GBR_PMD_CTRL);
460 if (ret < 0)
461 return ret;
462
Lendacky, Thomascf262522015-01-16 12:47:05 -0600463 ret &= ~XGBE_PHY_KR_TRAINING_ENABLE;
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500464 phy_write_mmd(phydev, MDIO_MMD_PMAPMD, MDIO_PMA_10GBR_PMD_CTRL, ret);
465
466 return 0;
467}
468
469static int amd_xgbe_phy_pcs_power_cycle(struct phy_device *phydev)
470{
471 int ret;
472
473 ret = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1);
474 if (ret < 0)
475 return ret;
476
477 ret |= MDIO_CTRL1_LPOWER;
478 phy_write_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1, ret);
479
480 usleep_range(75, 100);
481
482 ret &= ~MDIO_CTRL1_LPOWER;
483 phy_write_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1, ret);
484
485 return 0;
486}
487
488static void amd_xgbe_phy_serdes_start_ratechange(struct phy_device *phydev)
489{
490 struct amd_xgbe_phy_priv *priv = phydev->priv;
491
492 /* Assert Rx and Tx ratechange */
493 XSIR1_IOWRITE_BITS(priv, SIR1_SPEED, RATECHANGE, 1);
494}
495
496static void amd_xgbe_phy_serdes_complete_ratechange(struct phy_device *phydev)
497{
498 struct amd_xgbe_phy_priv *priv = phydev->priv;
Lendacky, Thomas169a6302014-07-29 08:57:37 -0500499 unsigned int wait;
500 u16 status;
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500501
502 /* Release Rx and Tx ratechange */
503 XSIR1_IOWRITE_BITS(priv, SIR1_SPEED, RATECHANGE, 0);
504
505 /* Wait for Rx and Tx ready */
Lendacky, Thomas169a6302014-07-29 08:57:37 -0500506 wait = XGBE_PHY_RATECHANGE_COUNT;
507 while (wait--) {
Lendacky, Thomas1fa1f2e2014-08-01 11:56:36 -0500508 usleep_range(50, 75);
Lendacky, Thomas169a6302014-07-29 08:57:37 -0500509
510 status = XSIR0_IOREAD(priv, SIR0_STATUS);
511 if (XSIR_GET_BITS(status, SIR0_STATUS, RX_READY) &&
512 XSIR_GET_BITS(status, SIR0_STATUS, TX_READY))
Tom Lendacky74ad7522015-02-24 10:47:49 -0600513 goto rx_reset;
Lendacky, Thomas169a6302014-07-29 08:57:37 -0500514 }
515
Lendacky, Thomas1fa1f2e2014-08-01 11:56:36 -0500516 netdev_dbg(phydev->attached_dev, "SerDes rx/tx not ready (%#hx)\n",
Lendacky, Thomas169a6302014-07-29 08:57:37 -0500517 status);
Tom Lendacky74ad7522015-02-24 10:47:49 -0600518
519rx_reset:
520 /* Perform Rx reset for the DFE changes */
521 XRXTX_IOWRITE_BITS(priv, RXTX_REG6, RESETB_RXD, 0);
522 XRXTX_IOWRITE_BITS(priv, RXTX_REG6, RESETB_RXD, 1);
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500523}
524
525static int amd_xgbe_phy_xgmii_mode(struct phy_device *phydev)
526{
527 struct amd_xgbe_phy_priv *priv = phydev->priv;
528 int ret;
529
530 /* Enable KR training */
531 ret = amd_xgbe_an_enable_kr_training(phydev);
532 if (ret < 0)
533 return ret;
534
535 /* Set PCS to KR/10G speed */
536 ret = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL2);
537 if (ret < 0)
538 return ret;
539
540 ret &= ~MDIO_PCS_CTRL2_TYPE;
541 ret |= MDIO_PCS_CTRL2_10GBR;
542 phy_write_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL2, ret);
543
544 ret = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1);
545 if (ret < 0)
546 return ret;
547
548 ret &= ~MDIO_CTRL1_SPEEDSEL;
549 ret |= MDIO_CTRL1_SPEED10G;
550 phy_write_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1, ret);
551
552 ret = amd_xgbe_phy_pcs_power_cycle(phydev);
553 if (ret < 0)
554 return ret;
555
556 /* Set SerDes to 10G speed */
557 amd_xgbe_phy_serdes_start_ratechange(phydev);
558
559 XSIR1_IOWRITE_BITS(priv, SIR1_SPEED, DATARATE, SPEED_10000_RATE);
560 XSIR1_IOWRITE_BITS(priv, SIR1_SPEED, WORDMODE, SPEED_10000_WORD);
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500561 XSIR1_IOWRITE_BITS(priv, SIR1_SPEED, PLLSEL, SPEED_10000_PLL);
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500562
Lendacky, Thomas8fdb1a092015-01-16 12:47:21 -0600563 XSIR1_IOWRITE_BITS(priv, SIR1_SPEED, CDR_RATE,
564 priv->serdes_cdr_rate[XGBE_PHY_SPEED_10000]);
565 XSIR1_IOWRITE_BITS(priv, SIR1_SPEED, TXAMP,
566 priv->serdes_tx_amp[XGBE_PHY_SPEED_10000]);
567 XRXTX_IOWRITE_BITS(priv, RXTX_REG20, BLWC_ENA,
568 priv->serdes_blwc[XGBE_PHY_SPEED_10000]);
569 XRXTX_IOWRITE_BITS(priv, RXTX_REG114, PQ_REG,
570 priv->serdes_pq_skew[XGBE_PHY_SPEED_10000]);
Tom Lendacky74ad7522015-02-24 10:47:49 -0600571 XRXTX_IOWRITE_BITS(priv, RXTX_REG129, RXDFE_CONFIG,
572 priv->serdes_dfe_tap_cfg[XGBE_PHY_SPEED_10000]);
573 XRXTX_IOWRITE(priv, RXTX_REG22,
574 priv->serdes_dfe_tap_ena[XGBE_PHY_SPEED_10000]);
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500575
576 amd_xgbe_phy_serdes_complete_ratechange(phydev);
577
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500578 return 0;
579}
580
581static int amd_xgbe_phy_gmii_2500_mode(struct phy_device *phydev)
582{
583 struct amd_xgbe_phy_priv *priv = phydev->priv;
584 int ret;
585
586 /* Disable KR training */
587 ret = amd_xgbe_an_disable_kr_training(phydev);
588 if (ret < 0)
589 return ret;
590
591 /* Set PCS to KX/1G speed */
592 ret = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL2);
593 if (ret < 0)
594 return ret;
595
596 ret &= ~MDIO_PCS_CTRL2_TYPE;
597 ret |= MDIO_PCS_CTRL2_10GBX;
598 phy_write_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL2, ret);
599
600 ret = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1);
601 if (ret < 0)
602 return ret;
603
604 ret &= ~MDIO_CTRL1_SPEEDSEL;
605 ret |= MDIO_CTRL1_SPEED1G;
606 phy_write_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1, ret);
607
608 ret = amd_xgbe_phy_pcs_power_cycle(phydev);
609 if (ret < 0)
610 return ret;
611
612 /* Set SerDes to 2.5G speed */
613 amd_xgbe_phy_serdes_start_ratechange(phydev);
614
615 XSIR1_IOWRITE_BITS(priv, SIR1_SPEED, DATARATE, SPEED_2500_RATE);
616 XSIR1_IOWRITE_BITS(priv, SIR1_SPEED, WORDMODE, SPEED_2500_WORD);
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500617 XSIR1_IOWRITE_BITS(priv, SIR1_SPEED, PLLSEL, SPEED_2500_PLL);
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500618
Lendacky, Thomas8fdb1a092015-01-16 12:47:21 -0600619 XSIR1_IOWRITE_BITS(priv, SIR1_SPEED, CDR_RATE,
620 priv->serdes_cdr_rate[XGBE_PHY_SPEED_2500]);
621 XSIR1_IOWRITE_BITS(priv, SIR1_SPEED, TXAMP,
622 priv->serdes_tx_amp[XGBE_PHY_SPEED_2500]);
623 XRXTX_IOWRITE_BITS(priv, RXTX_REG20, BLWC_ENA,
624 priv->serdes_blwc[XGBE_PHY_SPEED_2500]);
625 XRXTX_IOWRITE_BITS(priv, RXTX_REG114, PQ_REG,
626 priv->serdes_pq_skew[XGBE_PHY_SPEED_2500]);
Tom Lendacky74ad7522015-02-24 10:47:49 -0600627 XRXTX_IOWRITE_BITS(priv, RXTX_REG129, RXDFE_CONFIG,
628 priv->serdes_dfe_tap_cfg[XGBE_PHY_SPEED_2500]);
629 XRXTX_IOWRITE(priv, RXTX_REG22,
630 priv->serdes_dfe_tap_ena[XGBE_PHY_SPEED_2500]);
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500631
632 amd_xgbe_phy_serdes_complete_ratechange(phydev);
633
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500634 return 0;
635}
636
637static int amd_xgbe_phy_gmii_mode(struct phy_device *phydev)
638{
639 struct amd_xgbe_phy_priv *priv = phydev->priv;
640 int ret;
641
642 /* Disable KR training */
643 ret = amd_xgbe_an_disable_kr_training(phydev);
644 if (ret < 0)
645 return ret;
646
647 /* Set PCS to KX/1G speed */
648 ret = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL2);
649 if (ret < 0)
650 return ret;
651
652 ret &= ~MDIO_PCS_CTRL2_TYPE;
653 ret |= MDIO_PCS_CTRL2_10GBX;
654 phy_write_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL2, ret);
655
656 ret = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1);
657 if (ret < 0)
658 return ret;
659
660 ret &= ~MDIO_CTRL1_SPEEDSEL;
661 ret |= MDIO_CTRL1_SPEED1G;
662 phy_write_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1, ret);
663
664 ret = amd_xgbe_phy_pcs_power_cycle(phydev);
665 if (ret < 0)
666 return ret;
667
668 /* Set SerDes to 1G speed */
669 amd_xgbe_phy_serdes_start_ratechange(phydev);
670
671 XSIR1_IOWRITE_BITS(priv, SIR1_SPEED, DATARATE, SPEED_1000_RATE);
672 XSIR1_IOWRITE_BITS(priv, SIR1_SPEED, WORDMODE, SPEED_1000_WORD);
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500673 XSIR1_IOWRITE_BITS(priv, SIR1_SPEED, PLLSEL, SPEED_1000_PLL);
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500674
Lendacky, Thomas8fdb1a092015-01-16 12:47:21 -0600675 XSIR1_IOWRITE_BITS(priv, SIR1_SPEED, CDR_RATE,
676 priv->serdes_cdr_rate[XGBE_PHY_SPEED_1000]);
677 XSIR1_IOWRITE_BITS(priv, SIR1_SPEED, TXAMP,
678 priv->serdes_tx_amp[XGBE_PHY_SPEED_1000]);
679 XRXTX_IOWRITE_BITS(priv, RXTX_REG20, BLWC_ENA,
680 priv->serdes_blwc[XGBE_PHY_SPEED_1000]);
681 XRXTX_IOWRITE_BITS(priv, RXTX_REG114, PQ_REG,
682 priv->serdes_pq_skew[XGBE_PHY_SPEED_1000]);
Tom Lendacky74ad7522015-02-24 10:47:49 -0600683 XRXTX_IOWRITE_BITS(priv, RXTX_REG129, RXDFE_CONFIG,
684 priv->serdes_dfe_tap_cfg[XGBE_PHY_SPEED_1000]);
685 XRXTX_IOWRITE(priv, RXTX_REG22,
686 priv->serdes_dfe_tap_ena[XGBE_PHY_SPEED_1000]);
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500687
688 amd_xgbe_phy_serdes_complete_ratechange(phydev);
689
Lendacky, Thomase3eec4e2014-09-03 12:14:16 -0500690 return 0;
691}
692
693static int amd_xgbe_phy_cur_mode(struct phy_device *phydev,
694 enum amd_xgbe_phy_mode *mode)
695{
696 int ret;
697
698 ret = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL2);
699 if (ret < 0)
700 return ret;
701
702 if ((ret & MDIO_PCS_CTRL2_TYPE) == MDIO_PCS_CTRL2_10GBR)
703 *mode = AMD_XGBE_MODE_KR;
704 else
705 *mode = AMD_XGBE_MODE_KX;
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500706
707 return 0;
708}
709
Lendacky, Thomase3eec4e2014-09-03 12:14:16 -0500710static bool amd_xgbe_phy_in_kr_mode(struct phy_device *phydev)
711{
712 enum amd_xgbe_phy_mode mode;
713
714 if (amd_xgbe_phy_cur_mode(phydev, &mode))
715 return false;
716
717 return (mode == AMD_XGBE_MODE_KR);
718}
719
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500720static int amd_xgbe_phy_switch_mode(struct phy_device *phydev)
721{
722 struct amd_xgbe_phy_priv *priv = phydev->priv;
723 int ret;
724
725 /* If we are in KR switch to KX, and vice-versa */
Lendacky, Thomase3eec4e2014-09-03 12:14:16 -0500726 if (amd_xgbe_phy_in_kr_mode(phydev)) {
Lendacky, Thomasf0476042014-07-29 08:57:25 -0500727 if (priv->speed_set == AMD_XGBE_PHY_SPEEDSET_1000_10000)
728 ret = amd_xgbe_phy_gmii_mode(phydev);
729 else
730 ret = amd_xgbe_phy_gmii_2500_mode(phydev);
731 } else {
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500732 ret = amd_xgbe_phy_xgmii_mode(phydev);
Lendacky, Thomasf0476042014-07-29 08:57:25 -0500733 }
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500734
735 return ret;
736}
737
Lendacky, Thomase3eec4e2014-09-03 12:14:16 -0500738static int amd_xgbe_phy_set_mode(struct phy_device *phydev,
739 enum amd_xgbe_phy_mode mode)
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500740{
Lendacky, Thomase3eec4e2014-09-03 12:14:16 -0500741 enum amd_xgbe_phy_mode cur_mode;
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500742 int ret;
743
Lendacky, Thomase3eec4e2014-09-03 12:14:16 -0500744 ret = amd_xgbe_phy_cur_mode(phydev, &cur_mode);
745 if (ret)
746 return ret;
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500747
Lendacky, Thomase3eec4e2014-09-03 12:14:16 -0500748 if (mode != cur_mode)
749 ret = amd_xgbe_phy_switch_mode(phydev);
750
751 return ret;
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500752}
753
Lendacky, Thomasc3152d42015-01-16 12:47:00 -0600754static int amd_xgbe_phy_set_an(struct phy_device *phydev, bool enable,
755 bool restart)
756{
757 int ret;
758
759 ret = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_CTRL1);
760 if (ret < 0)
761 return ret;
762
763 ret &= ~MDIO_AN_CTRL1_ENABLE;
764
765 if (enable)
766 ret |= MDIO_AN_CTRL1_ENABLE;
767
768 if (restart)
769 ret |= MDIO_AN_CTRL1_RESTART;
770
771 phy_write_mmd(phydev, MDIO_MMD_AN, MDIO_CTRL1, ret);
772
773 return 0;
774}
775
776static int amd_xgbe_phy_restart_an(struct phy_device *phydev)
777{
778 return amd_xgbe_phy_set_an(phydev, true, true);
779}
780
781static int amd_xgbe_phy_disable_an(struct phy_device *phydev)
782{
783 return amd_xgbe_phy_set_an(phydev, false, false);
784}
785
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500786static enum amd_xgbe_phy_an amd_xgbe_an_tx_training(struct phy_device *phydev,
787 enum amd_xgbe_phy_rx *state)
788{
Tom Lendackya42f5c12014-09-07 09:54:41 -0500789 struct amd_xgbe_phy_priv *priv = phydev->priv;
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500790 int ad_reg, lp_reg, ret;
791
792 *state = AMD_XGBE_RX_COMPLETE;
793
Lendacky, Thomase3eec4e2014-09-03 12:14:16 -0500794 /* If we're not in KR mode then we're done */
795 if (!amd_xgbe_phy_in_kr_mode(phydev))
Lendacky, Thomasc3152d42015-01-16 12:47:00 -0600796 return AMD_XGBE_AN_PAGE_RECEIVED;
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500797
798 /* Enable/Disable FEC */
799 ad_reg = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_ADVERTISE + 2);
800 if (ad_reg < 0)
801 return AMD_XGBE_AN_ERROR;
802
803 lp_reg = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_LPA + 2);
804 if (lp_reg < 0)
805 return AMD_XGBE_AN_ERROR;
806
807 ret = phy_read_mmd(phydev, MDIO_MMD_PMAPMD, MDIO_PMA_10GBR_FEC_CTRL);
808 if (ret < 0)
809 return AMD_XGBE_AN_ERROR;
810
Lendacky, Thomascf262522015-01-16 12:47:05 -0600811 ret &= ~XGBE_PHY_FEC_MASK;
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500812 if ((ad_reg & 0xc000) && (lp_reg & 0xc000))
Lendacky, Thomascf262522015-01-16 12:47:05 -0600813 ret |= priv->fec_ability;
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500814
815 phy_write_mmd(phydev, MDIO_MMD_PMAPMD, MDIO_PMA_10GBR_FEC_CTRL, ret);
816
817 /* Start KR training */
818 ret = phy_read_mmd(phydev, MDIO_MMD_PMAPMD, MDIO_PMA_10GBR_PMD_CTRL);
819 if (ret < 0)
820 return AMD_XGBE_AN_ERROR;
821
Lendacky, Thomascf262522015-01-16 12:47:05 -0600822 if (ret & XGBE_PHY_KR_TRAINING_ENABLE) {
823 XSIR0_IOWRITE_BITS(priv, SIR0_KR_RT_1, RESET, 1);
Lendacky, Thomas5c10e5c2014-07-29 08:57:43 -0500824
Lendacky, Thomascf262522015-01-16 12:47:05 -0600825 ret |= XGBE_PHY_KR_TRAINING_START;
826 phy_write_mmd(phydev, MDIO_MMD_PMAPMD, MDIO_PMA_10GBR_PMD_CTRL,
827 ret);
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500828
Lendacky, Thomascf262522015-01-16 12:47:05 -0600829 XSIR0_IOWRITE_BITS(priv, SIR0_KR_RT_1, RESET, 0);
830 }
Lendacky, Thomas5c10e5c2014-07-29 08:57:43 -0500831
Lendacky, Thomasc3152d42015-01-16 12:47:00 -0600832 return AMD_XGBE_AN_PAGE_RECEIVED;
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500833}
834
835static enum amd_xgbe_phy_an amd_xgbe_an_tx_xnp(struct phy_device *phydev,
836 enum amd_xgbe_phy_rx *state)
837{
838 u16 msg;
839
840 *state = AMD_XGBE_RX_XNP;
841
842 msg = XNP_MCF_NULL_MESSAGE;
843 msg |= XNP_MP_FORMATTED;
844
845 phy_write_mmd(phydev, MDIO_MMD_AN, MDIO_AN_XNP + 2, 0);
846 phy_write_mmd(phydev, MDIO_MMD_AN, MDIO_AN_XNP + 1, 0);
847 phy_write_mmd(phydev, MDIO_MMD_AN, MDIO_AN_XNP, msg);
848
Lendacky, Thomasc3152d42015-01-16 12:47:00 -0600849 return AMD_XGBE_AN_PAGE_RECEIVED;
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500850}
851
852static enum amd_xgbe_phy_an amd_xgbe_an_rx_bpa(struct phy_device *phydev,
853 enum amd_xgbe_phy_rx *state)
854{
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500855 unsigned int link_support;
856 int ret, ad_reg, lp_reg;
857
858 /* Read Base Ability register 2 first */
859 ret = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_LPA + 1);
860 if (ret < 0)
861 return AMD_XGBE_AN_ERROR;
862
863 /* Check for a supported mode, otherwise restart in a different one */
Lendacky, Thomase3eec4e2014-09-03 12:14:16 -0500864 link_support = amd_xgbe_phy_in_kr_mode(phydev) ? 0x80 : 0x20;
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500865 if (!(ret & link_support))
Lendacky, Thomase3eec4e2014-09-03 12:14:16 -0500866 return AMD_XGBE_AN_INCOMPAT_LINK;
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500867
868 /* Check Extended Next Page support */
869 ad_reg = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_ADVERTISE);
870 if (ad_reg < 0)
871 return AMD_XGBE_AN_ERROR;
872
873 lp_reg = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_LPA);
874 if (lp_reg < 0)
875 return AMD_XGBE_AN_ERROR;
876
877 return ((ad_reg & XNP_NP_EXCHANGE) || (lp_reg & XNP_NP_EXCHANGE)) ?
878 amd_xgbe_an_tx_xnp(phydev, state) :
879 amd_xgbe_an_tx_training(phydev, state);
880}
881
882static enum amd_xgbe_phy_an amd_xgbe_an_rx_xnp(struct phy_device *phydev,
883 enum amd_xgbe_phy_rx *state)
884{
885 int ad_reg, lp_reg;
886
887 /* Check Extended Next Page support */
Lendacky, Thomas0d40b612015-01-16 12:47:10 -0600888 ad_reg = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_XNP);
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500889 if (ad_reg < 0)
890 return AMD_XGBE_AN_ERROR;
891
Lendacky, Thomas0d40b612015-01-16 12:47:10 -0600892 lp_reg = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_LPX);
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500893 if (lp_reg < 0)
894 return AMD_XGBE_AN_ERROR;
895
896 return ((ad_reg & XNP_NP_EXCHANGE) || (lp_reg & XNP_NP_EXCHANGE)) ?
897 amd_xgbe_an_tx_xnp(phydev, state) :
898 amd_xgbe_an_tx_training(phydev, state);
899}
900
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500901static enum amd_xgbe_phy_an amd_xgbe_an_page_received(struct phy_device *phydev)
902{
903 struct amd_xgbe_phy_priv *priv = phydev->priv;
904 enum amd_xgbe_phy_rx *state;
905 int ret;
906
Lendacky, Thomase3eec4e2014-09-03 12:14:16 -0500907 state = amd_xgbe_phy_in_kr_mode(phydev) ? &priv->kr_state
908 : &priv->kx_state;
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500909
910 switch (*state) {
911 case AMD_XGBE_RX_BPA:
912 ret = amd_xgbe_an_rx_bpa(phydev, state);
913 break;
914
915 case AMD_XGBE_RX_XNP:
916 ret = amd_xgbe_an_rx_xnp(phydev, state);
917 break;
918
919 default:
920 ret = AMD_XGBE_AN_ERROR;
921 }
922
923 return ret;
924}
925
926static enum amd_xgbe_phy_an amd_xgbe_an_incompat_link(struct phy_device *phydev)
927{
Lendacky, Thomasc3152d42015-01-16 12:47:00 -0600928 struct amd_xgbe_phy_priv *priv = phydev->priv;
Lendacky, Thomase3eec4e2014-09-03 12:14:16 -0500929 int ret;
930
Lendacky, Thomasc3152d42015-01-16 12:47:00 -0600931 /* Be sure we aren't looping trying to negotiate */
932 if (amd_xgbe_phy_in_kr_mode(phydev)) {
933 priv->kr_state = AMD_XGBE_RX_ERROR;
934
Lendacky, Thomasd9663c82015-03-20 11:49:42 -0500935 if (!(phydev->advertising & SUPPORTED_1000baseKX_Full) &&
936 !(phydev->advertising & SUPPORTED_2500baseX_Full))
Lendacky, Thomasc3152d42015-01-16 12:47:00 -0600937 return AMD_XGBE_AN_NO_LINK;
938
939 if (priv->kx_state != AMD_XGBE_RX_BPA)
940 return AMD_XGBE_AN_NO_LINK;
941 } else {
942 priv->kx_state = AMD_XGBE_RX_ERROR;
943
Lendacky, Thomasd9663c82015-03-20 11:49:42 -0500944 if (!(phydev->advertising & SUPPORTED_10000baseKR_Full))
Lendacky, Thomasc3152d42015-01-16 12:47:00 -0600945 return AMD_XGBE_AN_NO_LINK;
946
947 if (priv->kr_state != AMD_XGBE_RX_BPA)
948 return AMD_XGBE_AN_NO_LINK;
949 }
950
951 ret = amd_xgbe_phy_disable_an(phydev);
952 if (ret)
953 return AMD_XGBE_AN_ERROR;
954
Lendacky, Thomase3eec4e2014-09-03 12:14:16 -0500955 ret = amd_xgbe_phy_switch_mode(phydev);
956 if (ret)
957 return AMD_XGBE_AN_ERROR;
958
Lendacky, Thomasc3152d42015-01-16 12:47:00 -0600959 ret = amd_xgbe_phy_restart_an(phydev);
960 if (ret)
961 return AMD_XGBE_AN_ERROR;
962
963 return AMD_XGBE_AN_INCOMPAT_LINK;
964}
965
966static irqreturn_t amd_xgbe_an_isr(int irq, void *data)
967{
968 struct amd_xgbe_phy_priv *priv = (struct amd_xgbe_phy_priv *)data;
969
970 /* Interrupt reason must be read and cleared outside of IRQ context */
971 disable_irq_nosync(priv->an_irq);
972
973 queue_work(priv->an_workqueue, &priv->an_irq_work);
974
975 return IRQ_HANDLED;
976}
977
978static void amd_xgbe_an_irq_work(struct work_struct *work)
979{
980 struct amd_xgbe_phy_priv *priv = container_of(work,
981 struct amd_xgbe_phy_priv,
982 an_irq_work);
983
984 /* Avoid a race between enabling the IRQ and exiting the work by
985 * waiting for the work to finish and then queueing it
986 */
987 flush_work(&priv->an_work);
988 queue_work(priv->an_workqueue, &priv->an_work);
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500989}
990
991static void amd_xgbe_an_state_machine(struct work_struct *work)
992{
993 struct amd_xgbe_phy_priv *priv = container_of(work,
994 struct amd_xgbe_phy_priv,
995 an_work);
996 struct phy_device *phydev = priv->phydev;
Lendacky, Thomasc3152d42015-01-16 12:47:00 -0600997 enum amd_xgbe_phy_an cur_state = priv->an_state;
998 int int_reg, int_mask;
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500999
Lendacky, Thomasc3152d42015-01-16 12:47:00 -06001000 mutex_lock(&priv->an_mutex);
1001
1002 /* Read the interrupt */
1003 int_reg = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_INT);
1004 if (!int_reg)
1005 goto out;
1006
1007next_int:
1008 if (int_reg < 0) {
Lendacky, Thomase6f05622014-09-03 12:14:22 -05001009 priv->an_state = AMD_XGBE_AN_ERROR;
Lendacky, Thomasc3152d42015-01-16 12:47:00 -06001010 int_mask = XGBE_AN_INT_MASK;
1011 } else if (int_reg & XGBE_AN_PG_RCV) {
1012 priv->an_state = AMD_XGBE_AN_PAGE_RECEIVED;
1013 int_mask = XGBE_AN_PG_RCV;
1014 } else if (int_reg & XGBE_AN_INC_LINK) {
1015 priv->an_state = AMD_XGBE_AN_INCOMPAT_LINK;
1016 int_mask = XGBE_AN_INC_LINK;
1017 } else if (int_reg & XGBE_AN_INT_CMPLT) {
1018 priv->an_state = AMD_XGBE_AN_COMPLETE;
1019 int_mask = XGBE_AN_INT_CMPLT;
1020 } else {
1021 priv->an_state = AMD_XGBE_AN_ERROR;
1022 int_mask = 0;
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001023 }
1024
Lendacky, Thomasc3152d42015-01-16 12:47:00 -06001025 /* Clear the interrupt to be processed */
1026 int_reg &= ~int_mask;
1027 phy_write_mmd(phydev, MDIO_MMD_AN, MDIO_AN_INT, int_reg);
1028
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001029 priv->an_result = priv->an_state;
Lendacky, Thomasc3152d42015-01-16 12:47:00 -06001030
1031again:
1032 cur_state = priv->an_state;
1033
1034 switch (priv->an_state) {
1035 case AMD_XGBE_AN_READY:
1036 priv->an_supported = 0;
1037 break;
1038
1039 case AMD_XGBE_AN_PAGE_RECEIVED:
1040 priv->an_state = amd_xgbe_an_page_received(phydev);
1041 priv->an_supported++;
1042 break;
1043
1044 case AMD_XGBE_AN_INCOMPAT_LINK:
1045 priv->an_supported = 0;
1046 priv->parallel_detect = 0;
1047 priv->an_state = amd_xgbe_an_incompat_link(phydev);
1048 break;
1049
1050 case AMD_XGBE_AN_COMPLETE:
1051 priv->parallel_detect = priv->an_supported ? 0 : 1;
1052 netdev_dbg(phydev->attached_dev, "%s successful\n",
1053 priv->an_supported ? "Auto negotiation"
1054 : "Parallel detection");
1055 break;
1056
1057 case AMD_XGBE_AN_NO_LINK:
1058 break;
1059
1060 default:
1061 priv->an_state = AMD_XGBE_AN_ERROR;
1062 }
1063
1064 if (priv->an_state == AMD_XGBE_AN_NO_LINK) {
1065 int_reg = 0;
1066 phy_write_mmd(phydev, MDIO_MMD_AN, MDIO_AN_INT, 0);
1067 } else if (priv->an_state == AMD_XGBE_AN_ERROR) {
1068 netdev_err(phydev->attached_dev,
1069 "error during auto-negotiation, state=%u\n",
1070 cur_state);
1071
1072 int_reg = 0;
1073 phy_write_mmd(phydev, MDIO_MMD_AN, MDIO_AN_INT, 0);
1074 }
1075
1076 if (priv->an_state >= AMD_XGBE_AN_COMPLETE) {
1077 priv->an_result = priv->an_state;
1078 priv->an_state = AMD_XGBE_AN_READY;
1079 priv->kr_state = AMD_XGBE_RX_BPA;
1080 priv->kx_state = AMD_XGBE_RX_BPA;
1081 }
1082
1083 if (cur_state != priv->an_state)
1084 goto again;
1085
1086 if (int_reg)
1087 goto next_int;
1088
1089out:
1090 enable_irq(priv->an_irq);
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001091
1092 mutex_unlock(&priv->an_mutex);
1093}
1094
Lendacky, Thomasc3152d42015-01-16 12:47:00 -06001095static int amd_xgbe_an_init(struct phy_device *phydev)
1096{
1097 int ret;
1098
1099 /* Set up Advertisement register 3 first */
1100 ret = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_ADVERTISE + 2);
1101 if (ret < 0)
1102 return ret;
1103
Lendacky, Thomasd9663c82015-03-20 11:49:42 -05001104 if (phydev->advertising & SUPPORTED_10000baseR_FEC)
Lendacky, Thomasc3152d42015-01-16 12:47:00 -06001105 ret |= 0xc000;
1106 else
1107 ret &= ~0xc000;
1108
1109 phy_write_mmd(phydev, MDIO_MMD_AN, MDIO_AN_ADVERTISE + 2, ret);
1110
1111 /* Set up Advertisement register 2 next */
1112 ret = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_ADVERTISE + 1);
1113 if (ret < 0)
1114 return ret;
1115
Lendacky, Thomasd9663c82015-03-20 11:49:42 -05001116 if (phydev->advertising & SUPPORTED_10000baseKR_Full)
Lendacky, Thomasc3152d42015-01-16 12:47:00 -06001117 ret |= 0x80;
1118 else
1119 ret &= ~0x80;
1120
Lendacky, Thomasd9663c82015-03-20 11:49:42 -05001121 if ((phydev->advertising & SUPPORTED_1000baseKX_Full) ||
1122 (phydev->advertising & SUPPORTED_2500baseX_Full))
Lendacky, Thomasc3152d42015-01-16 12:47:00 -06001123 ret |= 0x20;
1124 else
1125 ret &= ~0x20;
1126
1127 phy_write_mmd(phydev, MDIO_MMD_AN, MDIO_AN_ADVERTISE + 1, ret);
1128
1129 /* Set up Advertisement register 1 last */
1130 ret = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_ADVERTISE);
1131 if (ret < 0)
1132 return ret;
1133
Lendacky, Thomasd9663c82015-03-20 11:49:42 -05001134 if (phydev->advertising & SUPPORTED_Pause)
Lendacky, Thomasc3152d42015-01-16 12:47:00 -06001135 ret |= 0x400;
1136 else
1137 ret &= ~0x400;
1138
Lendacky, Thomasd9663c82015-03-20 11:49:42 -05001139 if (phydev->advertising & SUPPORTED_Asym_Pause)
Lendacky, Thomasc3152d42015-01-16 12:47:00 -06001140 ret |= 0x800;
1141 else
1142 ret &= ~0x800;
1143
1144 /* We don't intend to perform XNP */
1145 ret &= ~XNP_NP_EXCHANGE;
1146
1147 phy_write_mmd(phydev, MDIO_MMD_AN, MDIO_AN_ADVERTISE, ret);
1148
1149 return 0;
1150}
1151
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001152static int amd_xgbe_phy_soft_reset(struct phy_device *phydev)
1153{
1154 int count, ret;
1155
1156 ret = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1);
1157 if (ret < 0)
1158 return ret;
1159
1160 ret |= MDIO_CTRL1_RESET;
1161 phy_write_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1, ret);
1162
1163 count = 50;
1164 do {
1165 msleep(20);
1166 ret = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1);
1167 if (ret < 0)
1168 return ret;
1169 } while ((ret & MDIO_CTRL1_RESET) && --count);
1170
1171 if (ret & MDIO_CTRL1_RESET)
1172 return -ETIMEDOUT;
1173
Lendacky, Thomasc3152d42015-01-16 12:47:00 -06001174 /* Disable auto-negotiation for now */
1175 ret = amd_xgbe_phy_disable_an(phydev);
1176 if (ret < 0)
1177 return ret;
1178
1179 /* Clear auto-negotiation interrupts */
1180 phy_write_mmd(phydev, MDIO_MMD_AN, MDIO_AN_INT, 0);
1181
1182 return 0;
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001183}
1184
1185static int amd_xgbe_phy_config_init(struct phy_device *phydev)
1186{
Lendacky, Thomasf0476042014-07-29 08:57:25 -05001187 struct amd_xgbe_phy_priv *priv = phydev->priv;
Lendacky, Thomasc3152d42015-01-16 12:47:00 -06001188 struct net_device *netdev = phydev->attached_dev;
1189 int ret;
1190
1191 if (!priv->an_irq_allocated) {
1192 /* Allocate the auto-negotiation workqueue and interrupt */
1193 snprintf(priv->an_irq_name, sizeof(priv->an_irq_name) - 1,
1194 "%s-pcs", netdev_name(netdev));
1195
1196 priv->an_workqueue =
1197 create_singlethread_workqueue(priv->an_irq_name);
1198 if (!priv->an_workqueue) {
1199 netdev_err(netdev, "phy workqueue creation failed\n");
1200 return -ENOMEM;
1201 }
1202
1203 ret = devm_request_irq(priv->dev, priv->an_irq,
1204 amd_xgbe_an_isr, 0, priv->an_irq_name,
1205 priv);
1206 if (ret) {
1207 netdev_err(netdev, "phy irq request failed\n");
1208 destroy_workqueue(priv->an_workqueue);
1209 return ret;
1210 }
1211
1212 priv->an_irq_allocated = 1;
1213 }
Lendacky, Thomasf0476042014-07-29 08:57:25 -05001214
Lendacky, Thomasc3152d42015-01-16 12:47:00 -06001215 /* Set initial mode - call the mode setting routines
1216 * directly to insure we are properly configured
1217 */
Lendacky, Thomasd9663c82015-03-20 11:49:42 -05001218 if (phydev->advertising & SUPPORTED_10000baseKR_Full)
Lendacky, Thomasc3152d42015-01-16 12:47:00 -06001219 ret = amd_xgbe_phy_xgmii_mode(phydev);
Lendacky, Thomasd9663c82015-03-20 11:49:42 -05001220 else if (phydev->advertising & SUPPORTED_1000baseKX_Full)
Lendacky, Thomasc3152d42015-01-16 12:47:00 -06001221 ret = amd_xgbe_phy_gmii_mode(phydev);
Lendacky, Thomasd9663c82015-03-20 11:49:42 -05001222 else if (phydev->advertising & SUPPORTED_2500baseX_Full)
Lendacky, Thomasc3152d42015-01-16 12:47:00 -06001223 ret = amd_xgbe_phy_gmii_2500_mode(phydev);
1224 else
1225 ret = -EINVAL;
1226 if (ret < 0)
1227 return ret;
1228
1229 /* Set up advertisement registers based on current settings */
1230 ret = amd_xgbe_an_init(phydev);
1231 if (ret)
1232 return ret;
1233
1234 /* Enable auto-negotiation interrupts */
1235 phy_write_mmd(phydev, MDIO_MMD_AN, MDIO_AN_INTMASK, 0x07);
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001236
1237 return 0;
1238}
1239
1240static int amd_xgbe_phy_setup_forced(struct phy_device *phydev)
1241{
1242 int ret;
1243
1244 /* Disable auto-negotiation */
Lendacky, Thomasc3152d42015-01-16 12:47:00 -06001245 ret = amd_xgbe_phy_disable_an(phydev);
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001246 if (ret < 0)
1247 return ret;
1248
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001249 /* Validate/Set specified speed */
1250 switch (phydev->speed) {
1251 case SPEED_10000:
Lendacky, Thomasc3152d42015-01-16 12:47:00 -06001252 ret = amd_xgbe_phy_set_mode(phydev, AMD_XGBE_MODE_KR);
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001253 break;
1254
1255 case SPEED_2500:
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001256 case SPEED_1000:
Lendacky, Thomasc3152d42015-01-16 12:47:00 -06001257 ret = amd_xgbe_phy_set_mode(phydev, AMD_XGBE_MODE_KX);
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001258 break;
1259
1260 default:
1261 ret = -EINVAL;
1262 }
1263
1264 if (ret < 0)
1265 return ret;
1266
1267 /* Validate duplex mode */
1268 if (phydev->duplex != DUPLEX_FULL)
1269 return -EINVAL;
1270
1271 phydev->pause = 0;
1272 phydev->asym_pause = 0;
1273
1274 return 0;
1275}
1276
Lendacky, Thomasc3152d42015-01-16 12:47:00 -06001277static int __amd_xgbe_phy_config_aneg(struct phy_device *phydev)
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001278{
1279 struct amd_xgbe_phy_priv *priv = phydev->priv;
1280 u32 mmd_mask = phydev->c45_ids.devices_in_package;
Lendacky, Thomasc3152d42015-01-16 12:47:00 -06001281 int ret;
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001282
1283 if (phydev->autoneg != AUTONEG_ENABLE)
1284 return amd_xgbe_phy_setup_forced(phydev);
1285
1286 /* Make sure we have the AN MMD present */
1287 if (!(mmd_mask & MDIO_DEVS_AN))
1288 return -EINVAL;
1289
Lendacky, Thomasc3152d42015-01-16 12:47:00 -06001290 /* Disable auto-negotiation interrupt */
1291 disable_irq(priv->an_irq);
1292
1293 /* Start auto-negotiation in a supported mode */
Lendacky, Thomasd9663c82015-03-20 11:49:42 -05001294 if (phydev->advertising & SUPPORTED_10000baseKR_Full)
Lendacky, Thomasc3152d42015-01-16 12:47:00 -06001295 ret = amd_xgbe_phy_set_mode(phydev, AMD_XGBE_MODE_KR);
Lendacky, Thomasd9663c82015-03-20 11:49:42 -05001296 else if ((phydev->advertising & SUPPORTED_1000baseKX_Full) ||
1297 (phydev->advertising & SUPPORTED_2500baseX_Full))
Lendacky, Thomasc3152d42015-01-16 12:47:00 -06001298 ret = amd_xgbe_phy_set_mode(phydev, AMD_XGBE_MODE_KX);
1299 else
1300 ret = -EINVAL;
1301 if (ret < 0) {
1302 enable_irq(priv->an_irq);
1303 return ret;
1304 }
1305
1306 /* Disable and stop any in progress auto-negotiation */
1307 ret = amd_xgbe_phy_disable_an(phydev);
1308 if (ret < 0)
1309 return ret;
1310
1311 /* Clear any auto-negotitation interrupts */
1312 phy_write_mmd(phydev, MDIO_MMD_AN, MDIO_AN_INT, 0);
1313
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001314 priv->an_result = AMD_XGBE_AN_READY;
Lendacky, Thomasc3152d42015-01-16 12:47:00 -06001315 priv->an_state = AMD_XGBE_AN_READY;
1316 priv->kr_state = AMD_XGBE_RX_BPA;
1317 priv->kx_state = AMD_XGBE_RX_BPA;
1318
1319 /* Re-enable auto-negotiation interrupt */
1320 enable_irq(priv->an_irq);
1321
1322 /* Set up advertisement registers based on current settings */
1323 ret = amd_xgbe_an_init(phydev);
1324 if (ret)
1325 return ret;
1326
1327 /* Enable and start auto-negotiation */
1328 return amd_xgbe_phy_restart_an(phydev);
1329}
1330
1331static int amd_xgbe_phy_config_aneg(struct phy_device *phydev)
1332{
1333 struct amd_xgbe_phy_priv *priv = phydev->priv;
1334 int ret;
1335
1336 mutex_lock(&priv->an_mutex);
1337
1338 ret = __amd_xgbe_phy_config_aneg(phydev);
1339
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001340 mutex_unlock(&priv->an_mutex);
1341
Lendacky, Thomasc3152d42015-01-16 12:47:00 -06001342 return ret;
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001343}
1344
1345static int amd_xgbe_phy_aneg_done(struct phy_device *phydev)
1346{
1347 struct amd_xgbe_phy_priv *priv = phydev->priv;
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001348
Lendacky, Thomasc3152d42015-01-16 12:47:00 -06001349 return (priv->an_result == AMD_XGBE_AN_COMPLETE);
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001350}
1351
1352static int amd_xgbe_phy_update_link(struct phy_device *phydev)
1353{
1354 struct amd_xgbe_phy_priv *priv = phydev->priv;
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001355 int ret;
1356
1357 /* If we're doing auto-negotiation don't report link down */
Lendacky, Thomasc3152d42015-01-16 12:47:00 -06001358 if (priv->an_state != AMD_XGBE_AN_READY) {
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001359 phydev->link = 1;
1360 return 0;
1361 }
1362
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001363 /* Link status is latched low, so read once to clear
1364 * and then read again to get current state
1365 */
1366 ret = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_STAT1);
1367 if (ret < 0)
1368 return ret;
1369
1370 ret = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_STAT1);
1371 if (ret < 0)
1372 return ret;
1373
1374 phydev->link = (ret & MDIO_STAT1_LSTATUS) ? 1 : 0;
1375
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001376 return 0;
1377}
1378
1379static int amd_xgbe_phy_read_status(struct phy_device *phydev)
1380{
Lendacky, Thomasf0476042014-07-29 08:57:25 -05001381 struct amd_xgbe_phy_priv *priv = phydev->priv;
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001382 u32 mmd_mask = phydev->c45_ids.devices_in_package;
Lendacky, Thomase3eec4e2014-09-03 12:14:16 -05001383 int ret, ad_ret, lp_ret;
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001384
1385 ret = amd_xgbe_phy_update_link(phydev);
1386 if (ret)
1387 return ret;
1388
Lendacky, Thomase6f05622014-09-03 12:14:22 -05001389 if ((phydev->autoneg == AUTONEG_ENABLE) &&
1390 !priv->parallel_detect) {
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001391 if (!(mmd_mask & MDIO_DEVS_AN))
1392 return -EINVAL;
1393
1394 if (!amd_xgbe_phy_aneg_done(phydev))
1395 return 0;
1396
1397 /* Compare Advertisement and Link Partner register 1 */
1398 ad_ret = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_ADVERTISE);
1399 if (ad_ret < 0)
1400 return ad_ret;
1401 lp_ret = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_LPA);
1402 if (lp_ret < 0)
1403 return lp_ret;
1404
1405 ad_ret &= lp_ret;
1406 phydev->pause = (ad_ret & 0x400) ? 1 : 0;
1407 phydev->asym_pause = (ad_ret & 0x800) ? 1 : 0;
1408
1409 /* Compare Advertisement and Link Partner register 2 */
1410 ad_ret = phy_read_mmd(phydev, MDIO_MMD_AN,
1411 MDIO_AN_ADVERTISE + 1);
1412 if (ad_ret < 0)
1413 return ad_ret;
1414 lp_ret = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_LPA + 1);
1415 if (lp_ret < 0)
1416 return lp_ret;
1417
1418 ad_ret &= lp_ret;
1419 if (ad_ret & 0x80) {
1420 phydev->speed = SPEED_10000;
Lendacky, Thomase3eec4e2014-09-03 12:14:16 -05001421 ret = amd_xgbe_phy_set_mode(phydev, AMD_XGBE_MODE_KR);
1422 if (ret)
1423 return ret;
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001424 } else {
Lendacky, Thomase3eec4e2014-09-03 12:14:16 -05001425 switch (priv->speed_set) {
1426 case AMD_XGBE_PHY_SPEEDSET_1000_10000:
Lendacky, Thomasf0476042014-07-29 08:57:25 -05001427 phydev->speed = SPEED_1000;
Lendacky, Thomase3eec4e2014-09-03 12:14:16 -05001428 break;
1429
1430 case AMD_XGBE_PHY_SPEEDSET_2500_10000:
Lendacky, Thomasf0476042014-07-29 08:57:25 -05001431 phydev->speed = SPEED_2500;
Lendacky, Thomase3eec4e2014-09-03 12:14:16 -05001432 break;
Lendacky, Thomasf0476042014-07-29 08:57:25 -05001433 }
1434
Lendacky, Thomase3eec4e2014-09-03 12:14:16 -05001435 ret = amd_xgbe_phy_set_mode(phydev, AMD_XGBE_MODE_KX);
1436 if (ret)
1437 return ret;
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001438 }
1439
1440 phydev->duplex = DUPLEX_FULL;
1441 } else {
Lendacky, Thomase3eec4e2014-09-03 12:14:16 -05001442 if (amd_xgbe_phy_in_kr_mode(phydev)) {
Lendacky, Thomasf0476042014-07-29 08:57:25 -05001443 phydev->speed = SPEED_10000;
1444 } else {
Lendacky, Thomase3eec4e2014-09-03 12:14:16 -05001445 switch (priv->speed_set) {
1446 case AMD_XGBE_PHY_SPEEDSET_1000_10000:
Lendacky, Thomasf0476042014-07-29 08:57:25 -05001447 phydev->speed = SPEED_1000;
Lendacky, Thomase3eec4e2014-09-03 12:14:16 -05001448 break;
1449
1450 case AMD_XGBE_PHY_SPEEDSET_2500_10000:
Lendacky, Thomasf0476042014-07-29 08:57:25 -05001451 phydev->speed = SPEED_2500;
Lendacky, Thomase3eec4e2014-09-03 12:14:16 -05001452 break;
1453 }
Lendacky, Thomasf0476042014-07-29 08:57:25 -05001454 }
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001455 phydev->duplex = DUPLEX_FULL;
1456 phydev->pause = 0;
1457 phydev->asym_pause = 0;
1458 }
1459
1460 return 0;
1461}
1462
1463static int amd_xgbe_phy_suspend(struct phy_device *phydev)
1464{
Lendacky, Thomas03e50fd2015-01-16 12:46:39 -06001465 struct amd_xgbe_phy_priv *priv = phydev->priv;
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001466 int ret;
1467
1468 mutex_lock(&phydev->lock);
1469
1470 ret = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1);
1471 if (ret < 0)
1472 goto unlock;
1473
Lendacky, Thomas03e50fd2015-01-16 12:46:39 -06001474 priv->lpm_ctrl = ret;
1475
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001476 ret |= MDIO_CTRL1_LPOWER;
1477 phy_write_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1, ret);
1478
1479 ret = 0;
1480
1481unlock:
1482 mutex_unlock(&phydev->lock);
1483
1484 return ret;
1485}
1486
1487static int amd_xgbe_phy_resume(struct phy_device *phydev)
1488{
Lendacky, Thomas03e50fd2015-01-16 12:46:39 -06001489 struct amd_xgbe_phy_priv *priv = phydev->priv;
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001490
1491 mutex_lock(&phydev->lock);
1492
Lendacky, Thomas03e50fd2015-01-16 12:46:39 -06001493 priv->lpm_ctrl &= ~MDIO_CTRL1_LPOWER;
1494 phy_write_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1, priv->lpm_ctrl);
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001495
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001496 mutex_unlock(&phydev->lock);
1497
Lendacky, Thomas03e50fd2015-01-16 12:46:39 -06001498 return 0;
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001499}
1500
Lendacky, Thomas82a19032015-01-16 12:47:16 -06001501static unsigned int amd_xgbe_phy_resource_count(struct platform_device *pdev,
1502 unsigned int type)
1503{
1504 unsigned int count;
1505 int i;
1506
1507 for (i = 0, count = 0; i < pdev->num_resources; i++) {
1508 struct resource *r = &pdev->resource[i];
1509
1510 if (type == resource_type(r))
1511 count++;
1512 }
1513
1514 return count;
1515}
1516
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001517static int amd_xgbe_phy_probe(struct phy_device *phydev)
1518{
1519 struct amd_xgbe_phy_priv *priv;
Lendacky, Thomas82a19032015-01-16 12:47:16 -06001520 struct platform_device *phy_pdev;
1521 struct device *dev, *phy_dev;
1522 unsigned int phy_resnum, phy_irqnum;
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001523 int ret;
1524
Lendacky, Thomas82a19032015-01-16 12:47:16 -06001525 if (!phydev->bus || !phydev->bus->parent)
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001526 return -EINVAL;
1527
Lendacky, Thomas82a19032015-01-16 12:47:16 -06001528 dev = phydev->bus->parent;
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001529
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001530 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
Lendacky, Thomas82a19032015-01-16 12:47:16 -06001531 if (!priv)
1532 return -ENOMEM;
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001533
Lendacky, Thomas82a19032015-01-16 12:47:16 -06001534 priv->pdev = to_platform_device(dev);
1535 priv->adev = ACPI_COMPANION(dev);
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001536 priv->dev = dev;
1537 priv->phydev = phydev;
Lendacky, Thomasc3152d42015-01-16 12:47:00 -06001538 mutex_init(&priv->an_mutex);
1539 INIT_WORK(&priv->an_irq_work, amd_xgbe_an_irq_work);
1540 INIT_WORK(&priv->an_work, amd_xgbe_an_state_machine);
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001541
Lendacky, Thomas82a19032015-01-16 12:47:16 -06001542 if (!priv->adev || acpi_disabled) {
1543 struct device_node *bus_node;
1544 struct device_node *phy_node;
1545
1546 bus_node = priv->dev->of_node;
1547 phy_node = of_parse_phandle(bus_node, "phy-handle", 0);
1548 if (!phy_node) {
1549 dev_err(dev, "unable to parse phy-handle\n");
1550 ret = -EINVAL;
1551 goto err_priv;
1552 }
1553
1554 phy_pdev = of_find_device_by_node(phy_node);
1555 of_node_put(phy_node);
1556
1557 if (!phy_pdev) {
1558 dev_err(dev, "unable to obtain phy device\n");
1559 ret = -EINVAL;
1560 goto err_priv;
1561 }
1562
1563 phy_resnum = 0;
1564 phy_irqnum = 0;
1565 } else {
1566 /* In ACPI, the XGBE and PHY resources are the grouped
1567 * together with the PHY resources at the end
1568 */
1569 phy_pdev = priv->pdev;
1570 phy_resnum = amd_xgbe_phy_resource_count(phy_pdev,
1571 IORESOURCE_MEM) - 3;
1572 phy_irqnum = amd_xgbe_phy_resource_count(phy_pdev,
1573 IORESOURCE_IRQ) - 1;
1574 }
1575 phy_dev = &phy_pdev->dev;
1576
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001577 /* Get the device mmio areas */
Lendacky, Thomas82a19032015-01-16 12:47:16 -06001578 priv->rxtx_res = platform_get_resource(phy_pdev, IORESOURCE_MEM,
1579 phy_resnum++);
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001580 priv->rxtx_regs = devm_ioremap_resource(dev, priv->rxtx_res);
1581 if (IS_ERR(priv->rxtx_regs)) {
1582 dev_err(dev, "rxtx ioremap failed\n");
1583 ret = PTR_ERR(priv->rxtx_regs);
Lendacky, Thomas82a19032015-01-16 12:47:16 -06001584 goto err_put;
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001585 }
1586
Lendacky, Thomas82a19032015-01-16 12:47:16 -06001587 priv->sir0_res = platform_get_resource(phy_pdev, IORESOURCE_MEM,
1588 phy_resnum++);
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001589 priv->sir0_regs = devm_ioremap_resource(dev, priv->sir0_res);
1590 if (IS_ERR(priv->sir0_regs)) {
1591 dev_err(dev, "sir0 ioremap failed\n");
1592 ret = PTR_ERR(priv->sir0_regs);
1593 goto err_rxtx;
1594 }
1595
Lendacky, Thomas82a19032015-01-16 12:47:16 -06001596 priv->sir1_res = platform_get_resource(phy_pdev, IORESOURCE_MEM,
1597 phy_resnum++);
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001598 priv->sir1_regs = devm_ioremap_resource(dev, priv->sir1_res);
1599 if (IS_ERR(priv->sir1_regs)) {
1600 dev_err(dev, "sir1 ioremap failed\n");
1601 ret = PTR_ERR(priv->sir1_regs);
1602 goto err_sir0;
1603 }
1604
Lendacky, Thomasc3152d42015-01-16 12:47:00 -06001605 /* Get the auto-negotiation interrupt */
Lendacky, Thomas82a19032015-01-16 12:47:16 -06001606 ret = platform_get_irq(phy_pdev, phy_irqnum);
Lendacky, Thomasc3152d42015-01-16 12:47:00 -06001607 if (ret < 0) {
1608 dev_err(dev, "platform_get_irq failed\n");
1609 goto err_sir1;
1610 }
1611 priv->an_irq = ret;
1612
Lendacky, Thomasf0476042014-07-29 08:57:25 -05001613 /* Get the device speed set property */
Lendacky, Thomas82a19032015-01-16 12:47:16 -06001614 ret = device_property_read_u32(phy_dev, XGBE_PHY_SPEEDSET_PROPERTY,
1615 &priv->speed_set);
1616 if (ret) {
1617 dev_err(dev, "invalid %s property\n",
1618 XGBE_PHY_SPEEDSET_PROPERTY);
1619 goto err_sir1;
1620 }
Lendacky, Thomasf0476042014-07-29 08:57:25 -05001621
Lendacky, Thomas82a19032015-01-16 12:47:16 -06001622 switch (priv->speed_set) {
1623 case AMD_XGBE_PHY_SPEEDSET_1000_10000:
1624 case AMD_XGBE_PHY_SPEEDSET_2500_10000:
Lendacky, Thomasf0476042014-07-29 08:57:25 -05001625 break;
1626 default:
Lendacky, Thomas82a19032015-01-16 12:47:16 -06001627 dev_err(dev, "invalid %s property\n",
1628 XGBE_PHY_SPEEDSET_PROPERTY);
Lendacky, Thomasf0476042014-07-29 08:57:25 -05001629 ret = -EINVAL;
1630 goto err_sir1;
1631 }
1632
Lendacky, Thomas8fdb1a092015-01-16 12:47:21 -06001633 if (device_property_present(phy_dev, XGBE_PHY_BLWC_PROPERTY)) {
1634 ret = device_property_read_u32_array(phy_dev,
1635 XGBE_PHY_BLWC_PROPERTY,
1636 priv->serdes_blwc,
1637 XGBE_PHY_SPEEDS);
1638 if (ret) {
1639 dev_err(dev, "invalid %s property\n",
1640 XGBE_PHY_BLWC_PROPERTY);
1641 goto err_sir1;
1642 }
1643 } else {
1644 memcpy(priv->serdes_blwc, amd_xgbe_phy_serdes_blwc,
1645 sizeof(priv->serdes_blwc));
1646 }
1647
1648 if (device_property_present(phy_dev, XGBE_PHY_CDR_RATE_PROPERTY)) {
1649 ret = device_property_read_u32_array(phy_dev,
1650 XGBE_PHY_CDR_RATE_PROPERTY,
1651 priv->serdes_cdr_rate,
1652 XGBE_PHY_SPEEDS);
1653 if (ret) {
1654 dev_err(dev, "invalid %s property\n",
1655 XGBE_PHY_CDR_RATE_PROPERTY);
1656 goto err_sir1;
1657 }
1658 } else {
1659 memcpy(priv->serdes_cdr_rate, amd_xgbe_phy_serdes_cdr_rate,
1660 sizeof(priv->serdes_cdr_rate));
1661 }
1662
1663 if (device_property_present(phy_dev, XGBE_PHY_PQ_SKEW_PROPERTY)) {
1664 ret = device_property_read_u32_array(phy_dev,
1665 XGBE_PHY_PQ_SKEW_PROPERTY,
1666 priv->serdes_pq_skew,
1667 XGBE_PHY_SPEEDS);
1668 if (ret) {
1669 dev_err(dev, "invalid %s property\n",
1670 XGBE_PHY_PQ_SKEW_PROPERTY);
1671 goto err_sir1;
1672 }
1673 } else {
1674 memcpy(priv->serdes_pq_skew, amd_xgbe_phy_serdes_pq_skew,
1675 sizeof(priv->serdes_pq_skew));
1676 }
1677
1678 if (device_property_present(phy_dev, XGBE_PHY_TX_AMP_PROPERTY)) {
1679 ret = device_property_read_u32_array(phy_dev,
1680 XGBE_PHY_TX_AMP_PROPERTY,
1681 priv->serdes_tx_amp,
1682 XGBE_PHY_SPEEDS);
1683 if (ret) {
1684 dev_err(dev, "invalid %s property\n",
1685 XGBE_PHY_TX_AMP_PROPERTY);
1686 goto err_sir1;
1687 }
1688 } else {
1689 memcpy(priv->serdes_tx_amp, amd_xgbe_phy_serdes_tx_amp,
1690 sizeof(priv->serdes_tx_amp));
1691 }
1692
Tom Lendacky74ad7522015-02-24 10:47:49 -06001693 if (device_property_present(phy_dev, XGBE_PHY_DFE_CFG_PROPERTY)) {
1694 ret = device_property_read_u32_array(phy_dev,
1695 XGBE_PHY_DFE_CFG_PROPERTY,
1696 priv->serdes_dfe_tap_cfg,
1697 XGBE_PHY_SPEEDS);
1698 if (ret) {
1699 dev_err(dev, "invalid %s property\n",
1700 XGBE_PHY_DFE_CFG_PROPERTY);
1701 goto err_sir1;
1702 }
1703 } else {
1704 memcpy(priv->serdes_dfe_tap_cfg,
1705 amd_xgbe_phy_serdes_dfe_tap_cfg,
1706 sizeof(priv->serdes_dfe_tap_cfg));
1707 }
1708
1709 if (device_property_present(phy_dev, XGBE_PHY_DFE_ENA_PROPERTY)) {
1710 ret = device_property_read_u32_array(phy_dev,
1711 XGBE_PHY_DFE_ENA_PROPERTY,
1712 priv->serdes_dfe_tap_ena,
1713 XGBE_PHY_SPEEDS);
1714 if (ret) {
1715 dev_err(dev, "invalid %s property\n",
1716 XGBE_PHY_DFE_ENA_PROPERTY);
1717 goto err_sir1;
1718 }
1719 } else {
1720 memcpy(priv->serdes_dfe_tap_ena,
1721 amd_xgbe_phy_serdes_dfe_tap_ena,
1722 sizeof(priv->serdes_dfe_tap_ena));
1723 }
1724
Lendacky, Thomasd9663c82015-03-20 11:49:42 -05001725 /* Initialize supported features */
1726 phydev->supported = SUPPORTED_Autoneg;
1727 phydev->supported |= SUPPORTED_Pause | SUPPORTED_Asym_Pause;
1728 phydev->supported |= SUPPORTED_Backplane;
1729 phydev->supported |= SUPPORTED_10000baseKR_Full;
1730 switch (priv->speed_set) {
1731 case AMD_XGBE_PHY_SPEEDSET_1000_10000:
1732 phydev->supported |= SUPPORTED_1000baseKX_Full;
1733 break;
1734 case AMD_XGBE_PHY_SPEEDSET_2500_10000:
1735 phydev->supported |= SUPPORTED_2500baseX_Full;
1736 break;
1737 }
1738
1739 ret = phy_read_mmd(phydev, MDIO_MMD_PMAPMD, MDIO_PMA_10GBR_FEC_ABILITY);
1740 if (ret < 0)
1741 return ret;
1742 priv->fec_ability = ret & XGBE_PHY_FEC_MASK;
1743 if (priv->fec_ability & XGBE_PHY_FEC_ENABLE)
1744 phydev->supported |= SUPPORTED_10000baseR_FEC;
1745
1746 phydev->advertising = phydev->supported;
1747
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001748 phydev->priv = priv;
1749
Lendacky, Thomas82a19032015-01-16 12:47:16 -06001750 if (!priv->adev || acpi_disabled)
1751 platform_device_put(phy_pdev);
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001752
1753 return 0;
1754
1755err_sir1:
1756 devm_iounmap(dev, priv->sir1_regs);
1757 devm_release_mem_region(dev, priv->sir1_res->start,
1758 resource_size(priv->sir1_res));
1759
1760err_sir0:
1761 devm_iounmap(dev, priv->sir0_regs);
1762 devm_release_mem_region(dev, priv->sir0_res->start,
1763 resource_size(priv->sir0_res));
1764
1765err_rxtx:
1766 devm_iounmap(dev, priv->rxtx_regs);
1767 devm_release_mem_region(dev, priv->rxtx_res->start,
1768 resource_size(priv->rxtx_res));
1769
Lendacky, Thomas82a19032015-01-16 12:47:16 -06001770err_put:
1771 if (!priv->adev || acpi_disabled)
1772 platform_device_put(phy_pdev);
1773
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001774err_priv:
1775 devm_kfree(dev, priv);
1776
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001777 return ret;
1778}
1779
1780static void amd_xgbe_phy_remove(struct phy_device *phydev)
1781{
1782 struct amd_xgbe_phy_priv *priv = phydev->priv;
1783 struct device *dev = priv->dev;
1784
Lendacky, Thomasc3152d42015-01-16 12:47:00 -06001785 if (priv->an_irq_allocated) {
1786 devm_free_irq(dev, priv->an_irq, priv);
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001787
Lendacky, Thomasc3152d42015-01-16 12:47:00 -06001788 flush_workqueue(priv->an_workqueue);
1789 destroy_workqueue(priv->an_workqueue);
1790 }
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001791
1792 /* Release resources */
1793 devm_iounmap(dev, priv->sir1_regs);
1794 devm_release_mem_region(dev, priv->sir1_res->start,
1795 resource_size(priv->sir1_res));
1796
1797 devm_iounmap(dev, priv->sir0_regs);
1798 devm_release_mem_region(dev, priv->sir0_res->start,
1799 resource_size(priv->sir0_res));
1800
1801 devm_iounmap(dev, priv->rxtx_regs);
1802 devm_release_mem_region(dev, priv->rxtx_res->start,
1803 resource_size(priv->rxtx_res));
1804
1805 devm_kfree(dev, priv);
1806}
1807
1808static int amd_xgbe_match_phy_device(struct phy_device *phydev)
1809{
1810 return phydev->c45_ids.device_ids[MDIO_MMD_PCS] == XGBE_PHY_ID;
1811}
1812
1813static struct phy_driver amd_xgbe_phy_driver[] = {
1814 {
1815 .phy_id = XGBE_PHY_ID,
1816 .phy_id_mask = XGBE_PHY_MASK,
1817 .name = "AMD XGBE PHY",
1818 .features = 0,
Lendacky, Thomas65f57cb2015-03-20 11:49:53 -05001819 .flags = PHY_IS_INTERNAL,
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001820 .probe = amd_xgbe_phy_probe,
1821 .remove = amd_xgbe_phy_remove,
1822 .soft_reset = amd_xgbe_phy_soft_reset,
1823 .config_init = amd_xgbe_phy_config_init,
1824 .suspend = amd_xgbe_phy_suspend,
1825 .resume = amd_xgbe_phy_resume,
1826 .config_aneg = amd_xgbe_phy_config_aneg,
1827 .aneg_done = amd_xgbe_phy_aneg_done,
1828 .read_status = amd_xgbe_phy_read_status,
1829 .match_phy_device = amd_xgbe_match_phy_device,
1830 .driver = {
1831 .owner = THIS_MODULE,
1832 },
1833 },
1834};
1835
Johan Hovold50fd7152014-11-11 19:45:59 +01001836module_phy_driver(amd_xgbe_phy_driver);
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001837
françois romieua25aafa2014-06-07 11:07:48 +02001838static struct mdio_device_id __maybe_unused amd_xgbe_phy_ids[] = {
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001839 { XGBE_PHY_ID, XGBE_PHY_MASK },
1840 { }
1841};
1842MODULE_DEVICE_TABLE(mdio, amd_xgbe_phy_ids);