blob: 2f210743673843845caf3a4b0ae27505eb5fc5b9 [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"
91
Lendacky, Thomas4d874b32014-06-05 09:15:12 -050092#define XGBE_AN_INT_CMPLT 0x01
93#define XGBE_AN_INC_LINK 0x02
94#define XGBE_AN_PG_RCV 0x04
Lendacky, Thomasc3152d42015-01-16 12:47:00 -060095#define XGBE_AN_INT_MASK 0x07
Lendacky, Thomas4d874b32014-06-05 09:15:12 -050096
97#define XNP_MCF_NULL_MESSAGE 0x001
Lendacky, Thomascb69cb02015-01-16 12:46:29 -060098#define XNP_ACK_PROCESSED BIT(12)
99#define XNP_MP_FORMATTED BIT(13)
100#define XNP_NP_EXCHANGE BIT(15)
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500101
Lendacky, Thomas1fa1f2e2014-08-01 11:56:36 -0500102#define XGBE_PHY_RATECHANGE_COUNT 500
Lendacky, Thomas169a6302014-07-29 08:57:37 -0500103
Lendacky, Thomascf262522015-01-16 12:47:05 -0600104#define XGBE_PHY_KR_TRAINING_START 0x01
105#define XGBE_PHY_KR_TRAINING_ENABLE 0x02
106
107#define XGBE_PHY_FEC_ENABLE 0x01
108#define XGBE_PHY_FEC_FORWARD 0x02
109#define XGBE_PHY_FEC_MASK 0x03
110
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500111#ifndef MDIO_PMA_10GBR_PMD_CTRL
112#define MDIO_PMA_10GBR_PMD_CTRL 0x0096
113#endif
Lendacky, Thomase6f05622014-09-03 12:14:22 -0500114
Lendacky, Thomascf262522015-01-16 12:47:05 -0600115#ifndef MDIO_PMA_10GBR_FEC_ABILITY
116#define MDIO_PMA_10GBR_FEC_ABILITY 0x00aa
117#endif
118
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500119#ifndef MDIO_PMA_10GBR_FEC_CTRL
120#define MDIO_PMA_10GBR_FEC_CTRL 0x00ab
121#endif
Lendacky, Thomase6f05622014-09-03 12:14:22 -0500122
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500123#ifndef MDIO_AN_XNP
124#define MDIO_AN_XNP 0x0016
125#endif
126
Lendacky, Thomas0d40b612015-01-16 12:47:10 -0600127#ifndef MDIO_AN_LPX
128#define MDIO_AN_LPX 0x0019
129#endif
130
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500131#ifndef MDIO_AN_INTMASK
132#define MDIO_AN_INTMASK 0x8001
133#endif
Lendacky, Thomase6f05622014-09-03 12:14:22 -0500134
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500135#ifndef MDIO_AN_INT
136#define MDIO_AN_INT 0x8002
137#endif
138
139#ifndef MDIO_CTRL1_SPEED1G
140#define MDIO_CTRL1_SPEED1G (MDIO_CTRL1_SPEED10G & ~BMCR_SPEED100)
141#endif
142
143/* SerDes integration register offsets */
Lendacky, Thomas5c10e5c2014-07-29 08:57:43 -0500144#define SIR0_KR_RT_1 0x002c
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500145#define SIR0_STATUS 0x0040
146#define SIR1_SPEED 0x0000
147
148/* SerDes integration register entry bit positions and sizes */
Lendacky, Thomas5c10e5c2014-07-29 08:57:43 -0500149#define SIR0_KR_RT_1_RESET_INDEX 11
150#define SIR0_KR_RT_1_RESET_WIDTH 1
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500151#define SIR0_STATUS_RX_READY_INDEX 0
152#define SIR0_STATUS_RX_READY_WIDTH 1
153#define SIR0_STATUS_TX_READY_INDEX 8
154#define SIR0_STATUS_TX_READY_WIDTH 1
155#define SIR1_SPEED_DATARATE_INDEX 4
156#define SIR1_SPEED_DATARATE_WIDTH 2
157#define SIR1_SPEED_PI_SPD_SEL_INDEX 12
158#define SIR1_SPEED_PI_SPD_SEL_WIDTH 4
159#define SIR1_SPEED_PLLSEL_INDEX 3
160#define SIR1_SPEED_PLLSEL_WIDTH 1
161#define SIR1_SPEED_RATECHANGE_INDEX 6
162#define SIR1_SPEED_RATECHANGE_WIDTH 1
163#define SIR1_SPEED_TXAMP_INDEX 8
164#define SIR1_SPEED_TXAMP_WIDTH 4
165#define SIR1_SPEED_WORDMODE_INDEX 0
166#define SIR1_SPEED_WORDMODE_WIDTH 3
167
168#define SPEED_10000_CDR 0x7
169#define SPEED_10000_PLL 0x1
170#define SPEED_10000_RATE 0x0
171#define SPEED_10000_TXAMP 0xa
172#define SPEED_10000_WORD 0x7
173
174#define SPEED_2500_CDR 0x2
175#define SPEED_2500_PLL 0x0
Lendacky, Thomasf0476042014-07-29 08:57:25 -0500176#define SPEED_2500_RATE 0x1
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500177#define SPEED_2500_TXAMP 0xf
178#define SPEED_2500_WORD 0x1
179
180#define SPEED_1000_CDR 0x2
181#define SPEED_1000_PLL 0x0
182#define SPEED_1000_RATE 0x3
183#define SPEED_1000_TXAMP 0xf
184#define SPEED_1000_WORD 0x1
185
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500186/* SerDes RxTx register offsets */
187#define RXTX_REG20 0x0050
188#define RXTX_REG114 0x01c8
189
190/* SerDes RxTx register entry bit positions and sizes */
191#define RXTX_REG20_BLWC_ENA_INDEX 2
192#define RXTX_REG20_BLWC_ENA_WIDTH 1
193#define RXTX_REG114_PQ_REG_INDEX 9
194#define RXTX_REG114_PQ_REG_WIDTH 7
195
196#define RXTX_10000_BLWC 0
197#define RXTX_10000_PQ 0x1e
198
199#define RXTX_2500_BLWC 1
200#define RXTX_2500_PQ 0xa
201
202#define RXTX_1000_BLWC 1
203#define RXTX_1000_PQ 0xa
204
205/* Bit setting and getting macros
206 * The get macro will extract the current bit field value from within
207 * the variable
208 *
209 * The set macro will clear the current bit field value within the
210 * variable and then set the bit field of the variable to the
211 * specified value
212 */
213#define GET_BITS(_var, _index, _width) \
214 (((_var) >> (_index)) & ((0x1 << (_width)) - 1))
215
216#define SET_BITS(_var, _index, _width, _val) \
217do { \
218 (_var) &= ~(((0x1 << (_width)) - 1) << (_index)); \
219 (_var) |= (((_val) & ((0x1 << (_width)) - 1)) << (_index)); \
220} while (0)
221
Lendacky, Thomas169a6302014-07-29 08:57:37 -0500222#define XSIR_GET_BITS(_var, _prefix, _field) \
223 GET_BITS((_var), \
224 _prefix##_##_field##_INDEX, \
225 _prefix##_##_field##_WIDTH)
226
227#define XSIR_SET_BITS(_var, _prefix, _field, _val) \
228 SET_BITS((_var), \
229 _prefix##_##_field##_INDEX, \
230 _prefix##_##_field##_WIDTH, (_val))
231
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500232/* Macros for reading or writing SerDes integration registers
233 * The ioread macros will get bit fields or full values using the
234 * register definitions formed using the input names
235 *
236 * The iowrite macros will set bit fields or full values using the
237 * register definitions formed using the input names
238 */
239#define XSIR0_IOREAD(_priv, _reg) \
240 ioread16((_priv)->sir0_regs + _reg)
241
242#define XSIR0_IOREAD_BITS(_priv, _reg, _field) \
243 GET_BITS(XSIR0_IOREAD((_priv), _reg), \
244 _reg##_##_field##_INDEX, \
245 _reg##_##_field##_WIDTH)
246
247#define XSIR0_IOWRITE(_priv, _reg, _val) \
248 iowrite16((_val), (_priv)->sir0_regs + _reg)
249
250#define XSIR0_IOWRITE_BITS(_priv, _reg, _field, _val) \
251do { \
252 u16 reg_val = XSIR0_IOREAD((_priv), _reg); \
253 SET_BITS(reg_val, \
254 _reg##_##_field##_INDEX, \
255 _reg##_##_field##_WIDTH, (_val)); \
256 XSIR0_IOWRITE((_priv), _reg, reg_val); \
257} while (0)
258
259#define XSIR1_IOREAD(_priv, _reg) \
260 ioread16((_priv)->sir1_regs + _reg)
261
262#define XSIR1_IOREAD_BITS(_priv, _reg, _field) \
263 GET_BITS(XSIR1_IOREAD((_priv), _reg), \
264 _reg##_##_field##_INDEX, \
265 _reg##_##_field##_WIDTH)
266
267#define XSIR1_IOWRITE(_priv, _reg, _val) \
268 iowrite16((_val), (_priv)->sir1_regs + _reg)
269
270#define XSIR1_IOWRITE_BITS(_priv, _reg, _field, _val) \
271do { \
272 u16 reg_val = XSIR1_IOREAD((_priv), _reg); \
273 SET_BITS(reg_val, \
274 _reg##_##_field##_INDEX, \
275 _reg##_##_field##_WIDTH, (_val)); \
276 XSIR1_IOWRITE((_priv), _reg, reg_val); \
277} while (0)
278
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500279/* Macros for reading or writing SerDes RxTx registers
280 * The ioread macros will get bit fields or full values using the
281 * register definitions formed using the input names
282 *
283 * The iowrite macros will set bit fields or full values using the
284 * register definitions formed using the input names
285 */
286#define XRXTX_IOREAD(_priv, _reg) \
287 ioread16((_priv)->rxtx_regs + _reg)
288
289#define XRXTX_IOREAD_BITS(_priv, _reg, _field) \
290 GET_BITS(XRXTX_IOREAD((_priv), _reg), \
291 _reg##_##_field##_INDEX, \
292 _reg##_##_field##_WIDTH)
293
294#define XRXTX_IOWRITE(_priv, _reg, _val) \
295 iowrite16((_val), (_priv)->rxtx_regs + _reg)
296
297#define XRXTX_IOWRITE_BITS(_priv, _reg, _field, _val) \
298do { \
299 u16 reg_val = XRXTX_IOREAD((_priv), _reg); \
300 SET_BITS(reg_val, \
301 _reg##_##_field##_INDEX, \
302 _reg##_##_field##_WIDTH, (_val)); \
303 XRXTX_IOWRITE((_priv), _reg, reg_val); \
304} while (0)
305
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500306enum amd_xgbe_phy_an {
307 AMD_XGBE_AN_READY = 0,
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500308 AMD_XGBE_AN_PAGE_RECEIVED,
309 AMD_XGBE_AN_INCOMPAT_LINK,
310 AMD_XGBE_AN_COMPLETE,
311 AMD_XGBE_AN_NO_LINK,
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500312 AMD_XGBE_AN_ERROR,
313};
314
315enum amd_xgbe_phy_rx {
Lendacky, Thomasc3152d42015-01-16 12:47:00 -0600316 AMD_XGBE_RX_BPA = 0,
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500317 AMD_XGBE_RX_XNP,
318 AMD_XGBE_RX_COMPLETE,
Lendacky, Thomasc3152d42015-01-16 12:47:00 -0600319 AMD_XGBE_RX_ERROR,
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500320};
321
322enum amd_xgbe_phy_mode {
323 AMD_XGBE_MODE_KR,
324 AMD_XGBE_MODE_KX,
325};
326
Lendacky, Thomasf0476042014-07-29 08:57:25 -0500327enum amd_xgbe_phy_speedset {
Lendacky, Thomas82a19032015-01-16 12:47:16 -0600328 AMD_XGBE_PHY_SPEEDSET_1000_10000 = 0,
Lendacky, Thomasf0476042014-07-29 08:57:25 -0500329 AMD_XGBE_PHY_SPEEDSET_2500_10000,
330};
331
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500332struct amd_xgbe_phy_priv {
333 struct platform_device *pdev;
Lendacky, Thomas82a19032015-01-16 12:47:16 -0600334 struct acpi_device *adev;
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500335 struct device *dev;
336
337 struct phy_device *phydev;
338
339 /* SerDes related mmio resources */
340 struct resource *rxtx_res;
341 struct resource *sir0_res;
342 struct resource *sir1_res;
343
344 /* SerDes related mmio registers */
345 void __iomem *rxtx_regs; /* SerDes Rx/Tx CSRs */
346 void __iomem *sir0_regs; /* SerDes integration registers (1/2) */
347 void __iomem *sir1_regs; /* SerDes integration registers (2/2) */
348
Lendacky, Thomasc3152d42015-01-16 12:47:00 -0600349 int an_irq;
350 char an_irq_name[IFNAMSIZ + 32];
351 struct work_struct an_irq_work;
352 unsigned int an_irq_allocated;
353
Lendacky, Thomasf0476042014-07-29 08:57:25 -0500354 unsigned int speed_set;
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500355
356 /* Auto-negotiation state machine support */
357 struct mutex an_mutex;
358 enum amd_xgbe_phy_an an_result;
359 enum amd_xgbe_phy_an an_state;
360 enum amd_xgbe_phy_rx kr_state;
361 enum amd_xgbe_phy_rx kx_state;
362 struct work_struct an_work;
363 struct workqueue_struct *an_workqueue;
Lendacky, Thomasc3152d42015-01-16 12:47:00 -0600364 unsigned int an_supported;
Lendacky, Thomase6f05622014-09-03 12:14:22 -0500365 unsigned int parallel_detect;
Lendacky, Thomascf262522015-01-16 12:47:05 -0600366 unsigned int fec_ability;
Lendacky, Thomas03e50fd2015-01-16 12:46:39 -0600367
368 unsigned int lpm_ctrl; /* CTRL1 for resume */
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500369};
370
371static int amd_xgbe_an_enable_kr_training(struct phy_device *phydev)
372{
373 int ret;
374
375 ret = phy_read_mmd(phydev, MDIO_MMD_PMAPMD, MDIO_PMA_10GBR_PMD_CTRL);
376 if (ret < 0)
377 return ret;
378
Lendacky, Thomascf262522015-01-16 12:47:05 -0600379 ret |= XGBE_PHY_KR_TRAINING_ENABLE;
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500380 phy_write_mmd(phydev, MDIO_MMD_PMAPMD, MDIO_PMA_10GBR_PMD_CTRL, ret);
381
382 return 0;
383}
384
385static int amd_xgbe_an_disable_kr_training(struct phy_device *phydev)
386{
387 int ret;
388
389 ret = phy_read_mmd(phydev, MDIO_MMD_PMAPMD, MDIO_PMA_10GBR_PMD_CTRL);
390 if (ret < 0)
391 return ret;
392
Lendacky, Thomascf262522015-01-16 12:47:05 -0600393 ret &= ~XGBE_PHY_KR_TRAINING_ENABLE;
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500394 phy_write_mmd(phydev, MDIO_MMD_PMAPMD, MDIO_PMA_10GBR_PMD_CTRL, ret);
395
396 return 0;
397}
398
399static int amd_xgbe_phy_pcs_power_cycle(struct phy_device *phydev)
400{
401 int ret;
402
403 ret = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1);
404 if (ret < 0)
405 return ret;
406
407 ret |= MDIO_CTRL1_LPOWER;
408 phy_write_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1, ret);
409
410 usleep_range(75, 100);
411
412 ret &= ~MDIO_CTRL1_LPOWER;
413 phy_write_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1, ret);
414
415 return 0;
416}
417
418static void amd_xgbe_phy_serdes_start_ratechange(struct phy_device *phydev)
419{
420 struct amd_xgbe_phy_priv *priv = phydev->priv;
421
422 /* Assert Rx and Tx ratechange */
423 XSIR1_IOWRITE_BITS(priv, SIR1_SPEED, RATECHANGE, 1);
424}
425
426static void amd_xgbe_phy_serdes_complete_ratechange(struct phy_device *phydev)
427{
428 struct amd_xgbe_phy_priv *priv = phydev->priv;
Lendacky, Thomas169a6302014-07-29 08:57:37 -0500429 unsigned int wait;
430 u16 status;
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500431
432 /* Release Rx and Tx ratechange */
433 XSIR1_IOWRITE_BITS(priv, SIR1_SPEED, RATECHANGE, 0);
434
435 /* Wait for Rx and Tx ready */
Lendacky, Thomas169a6302014-07-29 08:57:37 -0500436 wait = XGBE_PHY_RATECHANGE_COUNT;
437 while (wait--) {
Lendacky, Thomas1fa1f2e2014-08-01 11:56:36 -0500438 usleep_range(50, 75);
Lendacky, Thomas169a6302014-07-29 08:57:37 -0500439
440 status = XSIR0_IOREAD(priv, SIR0_STATUS);
441 if (XSIR_GET_BITS(status, SIR0_STATUS, RX_READY) &&
442 XSIR_GET_BITS(status, SIR0_STATUS, TX_READY))
443 return;
444 }
445
Lendacky, Thomas1fa1f2e2014-08-01 11:56:36 -0500446 netdev_dbg(phydev->attached_dev, "SerDes rx/tx not ready (%#hx)\n",
Lendacky, Thomas169a6302014-07-29 08:57:37 -0500447 status);
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500448}
449
450static int amd_xgbe_phy_xgmii_mode(struct phy_device *phydev)
451{
452 struct amd_xgbe_phy_priv *priv = phydev->priv;
453 int ret;
454
455 /* Enable KR training */
456 ret = amd_xgbe_an_enable_kr_training(phydev);
457 if (ret < 0)
458 return ret;
459
460 /* Set PCS to KR/10G speed */
461 ret = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL2);
462 if (ret < 0)
463 return ret;
464
465 ret &= ~MDIO_PCS_CTRL2_TYPE;
466 ret |= MDIO_PCS_CTRL2_10GBR;
467 phy_write_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL2, ret);
468
469 ret = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1);
470 if (ret < 0)
471 return ret;
472
473 ret &= ~MDIO_CTRL1_SPEEDSEL;
474 ret |= MDIO_CTRL1_SPEED10G;
475 phy_write_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1, ret);
476
477 ret = amd_xgbe_phy_pcs_power_cycle(phydev);
478 if (ret < 0)
479 return ret;
480
481 /* Set SerDes to 10G speed */
482 amd_xgbe_phy_serdes_start_ratechange(phydev);
483
484 XSIR1_IOWRITE_BITS(priv, SIR1_SPEED, DATARATE, SPEED_10000_RATE);
485 XSIR1_IOWRITE_BITS(priv, SIR1_SPEED, WORDMODE, SPEED_10000_WORD);
486 XSIR1_IOWRITE_BITS(priv, SIR1_SPEED, TXAMP, SPEED_10000_TXAMP);
487 XSIR1_IOWRITE_BITS(priv, SIR1_SPEED, PLLSEL, SPEED_10000_PLL);
488 XSIR1_IOWRITE_BITS(priv, SIR1_SPEED, PI_SPD_SEL, SPEED_10000_CDR);
489
490 XRXTX_IOWRITE_BITS(priv, RXTX_REG20, BLWC_ENA, RXTX_10000_BLWC);
491 XRXTX_IOWRITE_BITS(priv, RXTX_REG114, PQ_REG, RXTX_10000_PQ);
492
493 amd_xgbe_phy_serdes_complete_ratechange(phydev);
494
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500495 return 0;
496}
497
498static int amd_xgbe_phy_gmii_2500_mode(struct phy_device *phydev)
499{
500 struct amd_xgbe_phy_priv *priv = phydev->priv;
501 int ret;
502
503 /* Disable KR training */
504 ret = amd_xgbe_an_disable_kr_training(phydev);
505 if (ret < 0)
506 return ret;
507
508 /* Set PCS to KX/1G speed */
509 ret = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL2);
510 if (ret < 0)
511 return ret;
512
513 ret &= ~MDIO_PCS_CTRL2_TYPE;
514 ret |= MDIO_PCS_CTRL2_10GBX;
515 phy_write_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL2, ret);
516
517 ret = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1);
518 if (ret < 0)
519 return ret;
520
521 ret &= ~MDIO_CTRL1_SPEEDSEL;
522 ret |= MDIO_CTRL1_SPEED1G;
523 phy_write_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1, ret);
524
525 ret = amd_xgbe_phy_pcs_power_cycle(phydev);
526 if (ret < 0)
527 return ret;
528
529 /* Set SerDes to 2.5G speed */
530 amd_xgbe_phy_serdes_start_ratechange(phydev);
531
532 XSIR1_IOWRITE_BITS(priv, SIR1_SPEED, DATARATE, SPEED_2500_RATE);
533 XSIR1_IOWRITE_BITS(priv, SIR1_SPEED, WORDMODE, SPEED_2500_WORD);
534 XSIR1_IOWRITE_BITS(priv, SIR1_SPEED, TXAMP, SPEED_2500_TXAMP);
535 XSIR1_IOWRITE_BITS(priv, SIR1_SPEED, PLLSEL, SPEED_2500_PLL);
536 XSIR1_IOWRITE_BITS(priv, SIR1_SPEED, PI_SPD_SEL, SPEED_2500_CDR);
537
538 XRXTX_IOWRITE_BITS(priv, RXTX_REG20, BLWC_ENA, RXTX_2500_BLWC);
539 XRXTX_IOWRITE_BITS(priv, RXTX_REG114, PQ_REG, RXTX_2500_PQ);
540
541 amd_xgbe_phy_serdes_complete_ratechange(phydev);
542
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500543 return 0;
544}
545
546static int amd_xgbe_phy_gmii_mode(struct phy_device *phydev)
547{
548 struct amd_xgbe_phy_priv *priv = phydev->priv;
549 int ret;
550
551 /* Disable KR training */
552 ret = amd_xgbe_an_disable_kr_training(phydev);
553 if (ret < 0)
554 return ret;
555
556 /* Set PCS to KX/1G speed */
557 ret = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL2);
558 if (ret < 0)
559 return ret;
560
561 ret &= ~MDIO_PCS_CTRL2_TYPE;
562 ret |= MDIO_PCS_CTRL2_10GBX;
563 phy_write_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL2, ret);
564
565 ret = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1);
566 if (ret < 0)
567 return ret;
568
569 ret &= ~MDIO_CTRL1_SPEEDSEL;
570 ret |= MDIO_CTRL1_SPEED1G;
571 phy_write_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1, ret);
572
573 ret = amd_xgbe_phy_pcs_power_cycle(phydev);
574 if (ret < 0)
575 return ret;
576
577 /* Set SerDes to 1G speed */
578 amd_xgbe_phy_serdes_start_ratechange(phydev);
579
580 XSIR1_IOWRITE_BITS(priv, SIR1_SPEED, DATARATE, SPEED_1000_RATE);
581 XSIR1_IOWRITE_BITS(priv, SIR1_SPEED, WORDMODE, SPEED_1000_WORD);
582 XSIR1_IOWRITE_BITS(priv, SIR1_SPEED, TXAMP, SPEED_1000_TXAMP);
583 XSIR1_IOWRITE_BITS(priv, SIR1_SPEED, PLLSEL, SPEED_1000_PLL);
584 XSIR1_IOWRITE_BITS(priv, SIR1_SPEED, PI_SPD_SEL, SPEED_1000_CDR);
585
586 XRXTX_IOWRITE_BITS(priv, RXTX_REG20, BLWC_ENA, RXTX_1000_BLWC);
587 XRXTX_IOWRITE_BITS(priv, RXTX_REG114, PQ_REG, RXTX_1000_PQ);
588
589 amd_xgbe_phy_serdes_complete_ratechange(phydev);
590
Lendacky, Thomase3eec4e2014-09-03 12:14:16 -0500591 return 0;
592}
593
594static int amd_xgbe_phy_cur_mode(struct phy_device *phydev,
595 enum amd_xgbe_phy_mode *mode)
596{
597 int ret;
598
599 ret = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL2);
600 if (ret < 0)
601 return ret;
602
603 if ((ret & MDIO_PCS_CTRL2_TYPE) == MDIO_PCS_CTRL2_10GBR)
604 *mode = AMD_XGBE_MODE_KR;
605 else
606 *mode = AMD_XGBE_MODE_KX;
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500607
608 return 0;
609}
610
Lendacky, Thomase3eec4e2014-09-03 12:14:16 -0500611static bool amd_xgbe_phy_in_kr_mode(struct phy_device *phydev)
612{
613 enum amd_xgbe_phy_mode mode;
614
615 if (amd_xgbe_phy_cur_mode(phydev, &mode))
616 return false;
617
618 return (mode == AMD_XGBE_MODE_KR);
619}
620
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500621static int amd_xgbe_phy_switch_mode(struct phy_device *phydev)
622{
623 struct amd_xgbe_phy_priv *priv = phydev->priv;
624 int ret;
625
626 /* If we are in KR switch to KX, and vice-versa */
Lendacky, Thomase3eec4e2014-09-03 12:14:16 -0500627 if (amd_xgbe_phy_in_kr_mode(phydev)) {
Lendacky, Thomasf0476042014-07-29 08:57:25 -0500628 if (priv->speed_set == AMD_XGBE_PHY_SPEEDSET_1000_10000)
629 ret = amd_xgbe_phy_gmii_mode(phydev);
630 else
631 ret = amd_xgbe_phy_gmii_2500_mode(phydev);
632 } else {
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500633 ret = amd_xgbe_phy_xgmii_mode(phydev);
Lendacky, Thomasf0476042014-07-29 08:57:25 -0500634 }
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500635
636 return ret;
637}
638
Lendacky, Thomase3eec4e2014-09-03 12:14:16 -0500639static int amd_xgbe_phy_set_mode(struct phy_device *phydev,
640 enum amd_xgbe_phy_mode mode)
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500641{
Lendacky, Thomase3eec4e2014-09-03 12:14:16 -0500642 enum amd_xgbe_phy_mode cur_mode;
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500643 int ret;
644
Lendacky, Thomase3eec4e2014-09-03 12:14:16 -0500645 ret = amd_xgbe_phy_cur_mode(phydev, &cur_mode);
646 if (ret)
647 return ret;
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500648
Lendacky, Thomase3eec4e2014-09-03 12:14:16 -0500649 if (mode != cur_mode)
650 ret = amd_xgbe_phy_switch_mode(phydev);
651
652 return ret;
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500653}
654
Lendacky, Thomasc3152d42015-01-16 12:47:00 -0600655static int amd_xgbe_phy_set_an(struct phy_device *phydev, bool enable,
656 bool restart)
657{
658 int ret;
659
660 ret = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_CTRL1);
661 if (ret < 0)
662 return ret;
663
664 ret &= ~MDIO_AN_CTRL1_ENABLE;
665
666 if (enable)
667 ret |= MDIO_AN_CTRL1_ENABLE;
668
669 if (restart)
670 ret |= MDIO_AN_CTRL1_RESTART;
671
672 phy_write_mmd(phydev, MDIO_MMD_AN, MDIO_CTRL1, ret);
673
674 return 0;
675}
676
677static int amd_xgbe_phy_restart_an(struct phy_device *phydev)
678{
679 return amd_xgbe_phy_set_an(phydev, true, true);
680}
681
682static int amd_xgbe_phy_disable_an(struct phy_device *phydev)
683{
684 return amd_xgbe_phy_set_an(phydev, false, false);
685}
686
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500687static enum amd_xgbe_phy_an amd_xgbe_an_tx_training(struct phy_device *phydev,
688 enum amd_xgbe_phy_rx *state)
689{
Tom Lendackya42f5c12014-09-07 09:54:41 -0500690 struct amd_xgbe_phy_priv *priv = phydev->priv;
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500691 int ad_reg, lp_reg, ret;
692
693 *state = AMD_XGBE_RX_COMPLETE;
694
Lendacky, Thomase3eec4e2014-09-03 12:14:16 -0500695 /* If we're not in KR mode then we're done */
696 if (!amd_xgbe_phy_in_kr_mode(phydev))
Lendacky, Thomasc3152d42015-01-16 12:47:00 -0600697 return AMD_XGBE_AN_PAGE_RECEIVED;
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500698
699 /* Enable/Disable FEC */
700 ad_reg = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_ADVERTISE + 2);
701 if (ad_reg < 0)
702 return AMD_XGBE_AN_ERROR;
703
704 lp_reg = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_LPA + 2);
705 if (lp_reg < 0)
706 return AMD_XGBE_AN_ERROR;
707
708 ret = phy_read_mmd(phydev, MDIO_MMD_PMAPMD, MDIO_PMA_10GBR_FEC_CTRL);
709 if (ret < 0)
710 return AMD_XGBE_AN_ERROR;
711
Lendacky, Thomascf262522015-01-16 12:47:05 -0600712 ret &= ~XGBE_PHY_FEC_MASK;
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500713 if ((ad_reg & 0xc000) && (lp_reg & 0xc000))
Lendacky, Thomascf262522015-01-16 12:47:05 -0600714 ret |= priv->fec_ability;
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500715
716 phy_write_mmd(phydev, MDIO_MMD_PMAPMD, MDIO_PMA_10GBR_FEC_CTRL, ret);
717
718 /* Start KR training */
719 ret = phy_read_mmd(phydev, MDIO_MMD_PMAPMD, MDIO_PMA_10GBR_PMD_CTRL);
720 if (ret < 0)
721 return AMD_XGBE_AN_ERROR;
722
Lendacky, Thomascf262522015-01-16 12:47:05 -0600723 if (ret & XGBE_PHY_KR_TRAINING_ENABLE) {
724 XSIR0_IOWRITE_BITS(priv, SIR0_KR_RT_1, RESET, 1);
Lendacky, Thomas5c10e5c2014-07-29 08:57:43 -0500725
Lendacky, Thomascf262522015-01-16 12:47:05 -0600726 ret |= XGBE_PHY_KR_TRAINING_START;
727 phy_write_mmd(phydev, MDIO_MMD_PMAPMD, MDIO_PMA_10GBR_PMD_CTRL,
728 ret);
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500729
Lendacky, Thomascf262522015-01-16 12:47:05 -0600730 XSIR0_IOWRITE_BITS(priv, SIR0_KR_RT_1, RESET, 0);
731 }
Lendacky, Thomas5c10e5c2014-07-29 08:57:43 -0500732
Lendacky, Thomasc3152d42015-01-16 12:47:00 -0600733 return AMD_XGBE_AN_PAGE_RECEIVED;
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500734}
735
736static enum amd_xgbe_phy_an amd_xgbe_an_tx_xnp(struct phy_device *phydev,
737 enum amd_xgbe_phy_rx *state)
738{
739 u16 msg;
740
741 *state = AMD_XGBE_RX_XNP;
742
743 msg = XNP_MCF_NULL_MESSAGE;
744 msg |= XNP_MP_FORMATTED;
745
746 phy_write_mmd(phydev, MDIO_MMD_AN, MDIO_AN_XNP + 2, 0);
747 phy_write_mmd(phydev, MDIO_MMD_AN, MDIO_AN_XNP + 1, 0);
748 phy_write_mmd(phydev, MDIO_MMD_AN, MDIO_AN_XNP, msg);
749
Lendacky, Thomasc3152d42015-01-16 12:47:00 -0600750 return AMD_XGBE_AN_PAGE_RECEIVED;
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500751}
752
753static enum amd_xgbe_phy_an amd_xgbe_an_rx_bpa(struct phy_device *phydev,
754 enum amd_xgbe_phy_rx *state)
755{
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500756 unsigned int link_support;
757 int ret, ad_reg, lp_reg;
758
759 /* Read Base Ability register 2 first */
760 ret = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_LPA + 1);
761 if (ret < 0)
762 return AMD_XGBE_AN_ERROR;
763
764 /* Check for a supported mode, otherwise restart in a different one */
Lendacky, Thomase3eec4e2014-09-03 12:14:16 -0500765 link_support = amd_xgbe_phy_in_kr_mode(phydev) ? 0x80 : 0x20;
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500766 if (!(ret & link_support))
Lendacky, Thomase3eec4e2014-09-03 12:14:16 -0500767 return AMD_XGBE_AN_INCOMPAT_LINK;
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500768
769 /* Check Extended Next Page support */
770 ad_reg = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_ADVERTISE);
771 if (ad_reg < 0)
772 return AMD_XGBE_AN_ERROR;
773
774 lp_reg = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_LPA);
775 if (lp_reg < 0)
776 return AMD_XGBE_AN_ERROR;
777
778 return ((ad_reg & XNP_NP_EXCHANGE) || (lp_reg & XNP_NP_EXCHANGE)) ?
779 amd_xgbe_an_tx_xnp(phydev, state) :
780 amd_xgbe_an_tx_training(phydev, state);
781}
782
783static enum amd_xgbe_phy_an amd_xgbe_an_rx_xnp(struct phy_device *phydev,
784 enum amd_xgbe_phy_rx *state)
785{
786 int ad_reg, lp_reg;
787
788 /* Check Extended Next Page support */
Lendacky, Thomas0d40b612015-01-16 12:47:10 -0600789 ad_reg = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_XNP);
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500790 if (ad_reg < 0)
791 return AMD_XGBE_AN_ERROR;
792
Lendacky, Thomas0d40b612015-01-16 12:47:10 -0600793 lp_reg = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_LPX);
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500794 if (lp_reg < 0)
795 return AMD_XGBE_AN_ERROR;
796
797 return ((ad_reg & XNP_NP_EXCHANGE) || (lp_reg & XNP_NP_EXCHANGE)) ?
798 amd_xgbe_an_tx_xnp(phydev, state) :
799 amd_xgbe_an_tx_training(phydev, state);
800}
801
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500802static enum amd_xgbe_phy_an amd_xgbe_an_page_received(struct phy_device *phydev)
803{
804 struct amd_xgbe_phy_priv *priv = phydev->priv;
805 enum amd_xgbe_phy_rx *state;
806 int ret;
807
Lendacky, Thomase3eec4e2014-09-03 12:14:16 -0500808 state = amd_xgbe_phy_in_kr_mode(phydev) ? &priv->kr_state
809 : &priv->kx_state;
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500810
811 switch (*state) {
812 case AMD_XGBE_RX_BPA:
813 ret = amd_xgbe_an_rx_bpa(phydev, state);
814 break;
815
816 case AMD_XGBE_RX_XNP:
817 ret = amd_xgbe_an_rx_xnp(phydev, state);
818 break;
819
820 default:
821 ret = AMD_XGBE_AN_ERROR;
822 }
823
824 return ret;
825}
826
827static enum amd_xgbe_phy_an amd_xgbe_an_incompat_link(struct phy_device *phydev)
828{
Lendacky, Thomasc3152d42015-01-16 12:47:00 -0600829 struct amd_xgbe_phy_priv *priv = phydev->priv;
Lendacky, Thomase3eec4e2014-09-03 12:14:16 -0500830 int ret;
831
Lendacky, Thomasc3152d42015-01-16 12:47:00 -0600832 /* Be sure we aren't looping trying to negotiate */
833 if (amd_xgbe_phy_in_kr_mode(phydev)) {
834 priv->kr_state = AMD_XGBE_RX_ERROR;
835
836 if (!(phydev->supported & SUPPORTED_1000baseKX_Full) &&
837 !(phydev->supported & SUPPORTED_2500baseX_Full))
838 return AMD_XGBE_AN_NO_LINK;
839
840 if (priv->kx_state != AMD_XGBE_RX_BPA)
841 return AMD_XGBE_AN_NO_LINK;
842 } else {
843 priv->kx_state = AMD_XGBE_RX_ERROR;
844
845 if (!(phydev->supported & SUPPORTED_10000baseKR_Full))
846 return AMD_XGBE_AN_NO_LINK;
847
848 if (priv->kr_state != AMD_XGBE_RX_BPA)
849 return AMD_XGBE_AN_NO_LINK;
850 }
851
852 ret = amd_xgbe_phy_disable_an(phydev);
853 if (ret)
854 return AMD_XGBE_AN_ERROR;
855
Lendacky, Thomase3eec4e2014-09-03 12:14:16 -0500856 ret = amd_xgbe_phy_switch_mode(phydev);
857 if (ret)
858 return AMD_XGBE_AN_ERROR;
859
Lendacky, Thomasc3152d42015-01-16 12:47:00 -0600860 ret = amd_xgbe_phy_restart_an(phydev);
861 if (ret)
862 return AMD_XGBE_AN_ERROR;
863
864 return AMD_XGBE_AN_INCOMPAT_LINK;
865}
866
867static irqreturn_t amd_xgbe_an_isr(int irq, void *data)
868{
869 struct amd_xgbe_phy_priv *priv = (struct amd_xgbe_phy_priv *)data;
870
871 /* Interrupt reason must be read and cleared outside of IRQ context */
872 disable_irq_nosync(priv->an_irq);
873
874 queue_work(priv->an_workqueue, &priv->an_irq_work);
875
876 return IRQ_HANDLED;
877}
878
879static void amd_xgbe_an_irq_work(struct work_struct *work)
880{
881 struct amd_xgbe_phy_priv *priv = container_of(work,
882 struct amd_xgbe_phy_priv,
883 an_irq_work);
884
885 /* Avoid a race between enabling the IRQ and exiting the work by
886 * waiting for the work to finish and then queueing it
887 */
888 flush_work(&priv->an_work);
889 queue_work(priv->an_workqueue, &priv->an_work);
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500890}
891
892static void amd_xgbe_an_state_machine(struct work_struct *work)
893{
894 struct amd_xgbe_phy_priv *priv = container_of(work,
895 struct amd_xgbe_phy_priv,
896 an_work);
897 struct phy_device *phydev = priv->phydev;
Lendacky, Thomasc3152d42015-01-16 12:47:00 -0600898 enum amd_xgbe_phy_an cur_state = priv->an_state;
899 int int_reg, int_mask;
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500900
Lendacky, Thomasc3152d42015-01-16 12:47:00 -0600901 mutex_lock(&priv->an_mutex);
902
903 /* Read the interrupt */
904 int_reg = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_INT);
905 if (!int_reg)
906 goto out;
907
908next_int:
909 if (int_reg < 0) {
Lendacky, Thomase6f05622014-09-03 12:14:22 -0500910 priv->an_state = AMD_XGBE_AN_ERROR;
Lendacky, Thomasc3152d42015-01-16 12:47:00 -0600911 int_mask = XGBE_AN_INT_MASK;
912 } else if (int_reg & XGBE_AN_PG_RCV) {
913 priv->an_state = AMD_XGBE_AN_PAGE_RECEIVED;
914 int_mask = XGBE_AN_PG_RCV;
915 } else if (int_reg & XGBE_AN_INC_LINK) {
916 priv->an_state = AMD_XGBE_AN_INCOMPAT_LINK;
917 int_mask = XGBE_AN_INC_LINK;
918 } else if (int_reg & XGBE_AN_INT_CMPLT) {
919 priv->an_state = AMD_XGBE_AN_COMPLETE;
920 int_mask = XGBE_AN_INT_CMPLT;
921 } else {
922 priv->an_state = AMD_XGBE_AN_ERROR;
923 int_mask = 0;
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500924 }
925
Lendacky, Thomasc3152d42015-01-16 12:47:00 -0600926 /* Clear the interrupt to be processed */
927 int_reg &= ~int_mask;
928 phy_write_mmd(phydev, MDIO_MMD_AN, MDIO_AN_INT, int_reg);
929
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500930 priv->an_result = priv->an_state;
Lendacky, Thomasc3152d42015-01-16 12:47:00 -0600931
932again:
933 cur_state = priv->an_state;
934
935 switch (priv->an_state) {
936 case AMD_XGBE_AN_READY:
937 priv->an_supported = 0;
938 break;
939
940 case AMD_XGBE_AN_PAGE_RECEIVED:
941 priv->an_state = amd_xgbe_an_page_received(phydev);
942 priv->an_supported++;
943 break;
944
945 case AMD_XGBE_AN_INCOMPAT_LINK:
946 priv->an_supported = 0;
947 priv->parallel_detect = 0;
948 priv->an_state = amd_xgbe_an_incompat_link(phydev);
949 break;
950
951 case AMD_XGBE_AN_COMPLETE:
952 priv->parallel_detect = priv->an_supported ? 0 : 1;
953 netdev_dbg(phydev->attached_dev, "%s successful\n",
954 priv->an_supported ? "Auto negotiation"
955 : "Parallel detection");
956 break;
957
958 case AMD_XGBE_AN_NO_LINK:
959 break;
960
961 default:
962 priv->an_state = AMD_XGBE_AN_ERROR;
963 }
964
965 if (priv->an_state == AMD_XGBE_AN_NO_LINK) {
966 int_reg = 0;
967 phy_write_mmd(phydev, MDIO_MMD_AN, MDIO_AN_INT, 0);
968 } else if (priv->an_state == AMD_XGBE_AN_ERROR) {
969 netdev_err(phydev->attached_dev,
970 "error during auto-negotiation, state=%u\n",
971 cur_state);
972
973 int_reg = 0;
974 phy_write_mmd(phydev, MDIO_MMD_AN, MDIO_AN_INT, 0);
975 }
976
977 if (priv->an_state >= AMD_XGBE_AN_COMPLETE) {
978 priv->an_result = priv->an_state;
979 priv->an_state = AMD_XGBE_AN_READY;
980 priv->kr_state = AMD_XGBE_RX_BPA;
981 priv->kx_state = AMD_XGBE_RX_BPA;
982 }
983
984 if (cur_state != priv->an_state)
985 goto again;
986
987 if (int_reg)
988 goto next_int;
989
990out:
991 enable_irq(priv->an_irq);
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500992
993 mutex_unlock(&priv->an_mutex);
994}
995
Lendacky, Thomasc3152d42015-01-16 12:47:00 -0600996static int amd_xgbe_an_init(struct phy_device *phydev)
997{
998 int ret;
999
1000 /* Set up Advertisement register 3 first */
1001 ret = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_ADVERTISE + 2);
1002 if (ret < 0)
1003 return ret;
1004
1005 if (phydev->supported & SUPPORTED_10000baseR_FEC)
1006 ret |= 0xc000;
1007 else
1008 ret &= ~0xc000;
1009
1010 phy_write_mmd(phydev, MDIO_MMD_AN, MDIO_AN_ADVERTISE + 2, ret);
1011
1012 /* Set up Advertisement register 2 next */
1013 ret = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_ADVERTISE + 1);
1014 if (ret < 0)
1015 return ret;
1016
1017 if (phydev->supported & SUPPORTED_10000baseKR_Full)
1018 ret |= 0x80;
1019 else
1020 ret &= ~0x80;
1021
1022 if ((phydev->supported & SUPPORTED_1000baseKX_Full) ||
1023 (phydev->supported & SUPPORTED_2500baseX_Full))
1024 ret |= 0x20;
1025 else
1026 ret &= ~0x20;
1027
1028 phy_write_mmd(phydev, MDIO_MMD_AN, MDIO_AN_ADVERTISE + 1, ret);
1029
1030 /* Set up Advertisement register 1 last */
1031 ret = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_ADVERTISE);
1032 if (ret < 0)
1033 return ret;
1034
1035 if (phydev->supported & SUPPORTED_Pause)
1036 ret |= 0x400;
1037 else
1038 ret &= ~0x400;
1039
1040 if (phydev->supported & SUPPORTED_Asym_Pause)
1041 ret |= 0x800;
1042 else
1043 ret &= ~0x800;
1044
1045 /* We don't intend to perform XNP */
1046 ret &= ~XNP_NP_EXCHANGE;
1047
1048 phy_write_mmd(phydev, MDIO_MMD_AN, MDIO_AN_ADVERTISE, ret);
1049
1050 return 0;
1051}
1052
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001053static int amd_xgbe_phy_soft_reset(struct phy_device *phydev)
1054{
1055 int count, ret;
1056
1057 ret = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1);
1058 if (ret < 0)
1059 return ret;
1060
1061 ret |= MDIO_CTRL1_RESET;
1062 phy_write_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1, ret);
1063
1064 count = 50;
1065 do {
1066 msleep(20);
1067 ret = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1);
1068 if (ret < 0)
1069 return ret;
1070 } while ((ret & MDIO_CTRL1_RESET) && --count);
1071
1072 if (ret & MDIO_CTRL1_RESET)
1073 return -ETIMEDOUT;
1074
Lendacky, Thomasc3152d42015-01-16 12:47:00 -06001075 /* Disable auto-negotiation for now */
1076 ret = amd_xgbe_phy_disable_an(phydev);
1077 if (ret < 0)
1078 return ret;
1079
1080 /* Clear auto-negotiation interrupts */
1081 phy_write_mmd(phydev, MDIO_MMD_AN, MDIO_AN_INT, 0);
1082
1083 return 0;
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001084}
1085
1086static int amd_xgbe_phy_config_init(struct phy_device *phydev)
1087{
Lendacky, Thomasf0476042014-07-29 08:57:25 -05001088 struct amd_xgbe_phy_priv *priv = phydev->priv;
Lendacky, Thomasc3152d42015-01-16 12:47:00 -06001089 struct net_device *netdev = phydev->attached_dev;
1090 int ret;
1091
1092 if (!priv->an_irq_allocated) {
1093 /* Allocate the auto-negotiation workqueue and interrupt */
1094 snprintf(priv->an_irq_name, sizeof(priv->an_irq_name) - 1,
1095 "%s-pcs", netdev_name(netdev));
1096
1097 priv->an_workqueue =
1098 create_singlethread_workqueue(priv->an_irq_name);
1099 if (!priv->an_workqueue) {
1100 netdev_err(netdev, "phy workqueue creation failed\n");
1101 return -ENOMEM;
1102 }
1103
1104 ret = devm_request_irq(priv->dev, priv->an_irq,
1105 amd_xgbe_an_isr, 0, priv->an_irq_name,
1106 priv);
1107 if (ret) {
1108 netdev_err(netdev, "phy irq request failed\n");
1109 destroy_workqueue(priv->an_workqueue);
1110 return ret;
1111 }
1112
1113 priv->an_irq_allocated = 1;
1114 }
Lendacky, Thomasf0476042014-07-29 08:57:25 -05001115
Lendacky, Thomascf262522015-01-16 12:47:05 -06001116 ret = phy_read_mmd(phydev, MDIO_MMD_PMAPMD, MDIO_PMA_10GBR_FEC_ABILITY);
1117 if (ret < 0)
1118 return ret;
1119 priv->fec_ability = ret & XGBE_PHY_FEC_MASK;
1120
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001121 /* Initialize supported features */
1122 phydev->supported = SUPPORTED_Autoneg;
1123 phydev->supported |= SUPPORTED_Pause | SUPPORTED_Asym_Pause;
1124 phydev->supported |= SUPPORTED_Backplane;
Lendacky, Thomascf262522015-01-16 12:47:05 -06001125 phydev->supported |= SUPPORTED_10000baseKR_Full;
Lendacky, Thomasf0476042014-07-29 08:57:25 -05001126 switch (priv->speed_set) {
1127 case AMD_XGBE_PHY_SPEEDSET_1000_10000:
1128 phydev->supported |= SUPPORTED_1000baseKX_Full;
1129 break;
1130 case AMD_XGBE_PHY_SPEEDSET_2500_10000:
1131 phydev->supported |= SUPPORTED_2500baseX_Full;
1132 break;
1133 }
Lendacky, Thomascf262522015-01-16 12:47:05 -06001134
1135 if (priv->fec_ability & XGBE_PHY_FEC_ENABLE)
1136 phydev->supported |= SUPPORTED_10000baseR_FEC;
1137
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001138 phydev->advertising = phydev->supported;
1139
Lendacky, Thomasc3152d42015-01-16 12:47:00 -06001140 /* Set initial mode - call the mode setting routines
1141 * directly to insure we are properly configured
1142 */
1143 if (phydev->supported & SUPPORTED_10000baseKR_Full)
1144 ret = amd_xgbe_phy_xgmii_mode(phydev);
1145 else if (phydev->supported & SUPPORTED_1000baseKX_Full)
1146 ret = amd_xgbe_phy_gmii_mode(phydev);
1147 else if (phydev->supported & SUPPORTED_2500baseX_Full)
1148 ret = amd_xgbe_phy_gmii_2500_mode(phydev);
1149 else
1150 ret = -EINVAL;
1151 if (ret < 0)
1152 return ret;
1153
1154 /* Set up advertisement registers based on current settings */
1155 ret = amd_xgbe_an_init(phydev);
1156 if (ret)
1157 return ret;
1158
1159 /* Enable auto-negotiation interrupts */
1160 phy_write_mmd(phydev, MDIO_MMD_AN, MDIO_AN_INTMASK, 0x07);
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001161
1162 return 0;
1163}
1164
1165static int amd_xgbe_phy_setup_forced(struct phy_device *phydev)
1166{
1167 int ret;
1168
1169 /* Disable auto-negotiation */
Lendacky, Thomasc3152d42015-01-16 12:47:00 -06001170 ret = amd_xgbe_phy_disable_an(phydev);
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001171 if (ret < 0)
1172 return ret;
1173
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001174 /* Validate/Set specified speed */
1175 switch (phydev->speed) {
1176 case SPEED_10000:
Lendacky, Thomasc3152d42015-01-16 12:47:00 -06001177 ret = amd_xgbe_phy_set_mode(phydev, AMD_XGBE_MODE_KR);
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001178 break;
1179
1180 case SPEED_2500:
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001181 case SPEED_1000:
Lendacky, Thomasc3152d42015-01-16 12:47:00 -06001182 ret = amd_xgbe_phy_set_mode(phydev, AMD_XGBE_MODE_KX);
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001183 break;
1184
1185 default:
1186 ret = -EINVAL;
1187 }
1188
1189 if (ret < 0)
1190 return ret;
1191
1192 /* Validate duplex mode */
1193 if (phydev->duplex != DUPLEX_FULL)
1194 return -EINVAL;
1195
1196 phydev->pause = 0;
1197 phydev->asym_pause = 0;
1198
1199 return 0;
1200}
1201
Lendacky, Thomasc3152d42015-01-16 12:47:00 -06001202static int __amd_xgbe_phy_config_aneg(struct phy_device *phydev)
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001203{
1204 struct amd_xgbe_phy_priv *priv = phydev->priv;
1205 u32 mmd_mask = phydev->c45_ids.devices_in_package;
Lendacky, Thomasc3152d42015-01-16 12:47:00 -06001206 int ret;
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001207
1208 if (phydev->autoneg != AUTONEG_ENABLE)
1209 return amd_xgbe_phy_setup_forced(phydev);
1210
1211 /* Make sure we have the AN MMD present */
1212 if (!(mmd_mask & MDIO_DEVS_AN))
1213 return -EINVAL;
1214
Lendacky, Thomasc3152d42015-01-16 12:47:00 -06001215 /* Disable auto-negotiation interrupt */
1216 disable_irq(priv->an_irq);
1217
1218 /* Start auto-negotiation in a supported mode */
1219 if (phydev->supported & SUPPORTED_10000baseKR_Full)
1220 ret = amd_xgbe_phy_set_mode(phydev, AMD_XGBE_MODE_KR);
1221 else if ((phydev->supported & SUPPORTED_1000baseKX_Full) ||
1222 (phydev->supported & SUPPORTED_2500baseX_Full))
1223 ret = amd_xgbe_phy_set_mode(phydev, AMD_XGBE_MODE_KX);
1224 else
1225 ret = -EINVAL;
1226 if (ret < 0) {
1227 enable_irq(priv->an_irq);
1228 return ret;
1229 }
1230
1231 /* Disable and stop any in progress auto-negotiation */
1232 ret = amd_xgbe_phy_disable_an(phydev);
1233 if (ret < 0)
1234 return ret;
1235
1236 /* Clear any auto-negotitation interrupts */
1237 phy_write_mmd(phydev, MDIO_MMD_AN, MDIO_AN_INT, 0);
1238
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001239 priv->an_result = AMD_XGBE_AN_READY;
Lendacky, Thomasc3152d42015-01-16 12:47:00 -06001240 priv->an_state = AMD_XGBE_AN_READY;
1241 priv->kr_state = AMD_XGBE_RX_BPA;
1242 priv->kx_state = AMD_XGBE_RX_BPA;
1243
1244 /* Re-enable auto-negotiation interrupt */
1245 enable_irq(priv->an_irq);
1246
1247 /* Set up advertisement registers based on current settings */
1248 ret = amd_xgbe_an_init(phydev);
1249 if (ret)
1250 return ret;
1251
1252 /* Enable and start auto-negotiation */
1253 return amd_xgbe_phy_restart_an(phydev);
1254}
1255
1256static int amd_xgbe_phy_config_aneg(struct phy_device *phydev)
1257{
1258 struct amd_xgbe_phy_priv *priv = phydev->priv;
1259 int ret;
1260
1261 mutex_lock(&priv->an_mutex);
1262
1263 ret = __amd_xgbe_phy_config_aneg(phydev);
1264
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001265 mutex_unlock(&priv->an_mutex);
1266
Lendacky, Thomasc3152d42015-01-16 12:47:00 -06001267 return ret;
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001268}
1269
1270static int amd_xgbe_phy_aneg_done(struct phy_device *phydev)
1271{
1272 struct amd_xgbe_phy_priv *priv = phydev->priv;
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001273
Lendacky, Thomasc3152d42015-01-16 12:47:00 -06001274 return (priv->an_result == AMD_XGBE_AN_COMPLETE);
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001275}
1276
1277static int amd_xgbe_phy_update_link(struct phy_device *phydev)
1278{
1279 struct amd_xgbe_phy_priv *priv = phydev->priv;
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001280 int ret;
1281
1282 /* If we're doing auto-negotiation don't report link down */
Lendacky, Thomasc3152d42015-01-16 12:47:00 -06001283 if (priv->an_state != AMD_XGBE_AN_READY) {
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001284 phydev->link = 1;
1285 return 0;
1286 }
1287
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001288 /* Link status is latched low, so read once to clear
1289 * and then read again to get current state
1290 */
1291 ret = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_STAT1);
1292 if (ret < 0)
1293 return ret;
1294
1295 ret = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_STAT1);
1296 if (ret < 0)
1297 return ret;
1298
1299 phydev->link = (ret & MDIO_STAT1_LSTATUS) ? 1 : 0;
1300
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001301 return 0;
1302}
1303
1304static int amd_xgbe_phy_read_status(struct phy_device *phydev)
1305{
Lendacky, Thomasf0476042014-07-29 08:57:25 -05001306 struct amd_xgbe_phy_priv *priv = phydev->priv;
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001307 u32 mmd_mask = phydev->c45_ids.devices_in_package;
Lendacky, Thomase3eec4e2014-09-03 12:14:16 -05001308 int ret, ad_ret, lp_ret;
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001309
1310 ret = amd_xgbe_phy_update_link(phydev);
1311 if (ret)
1312 return ret;
1313
Lendacky, Thomase6f05622014-09-03 12:14:22 -05001314 if ((phydev->autoneg == AUTONEG_ENABLE) &&
1315 !priv->parallel_detect) {
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001316 if (!(mmd_mask & MDIO_DEVS_AN))
1317 return -EINVAL;
1318
1319 if (!amd_xgbe_phy_aneg_done(phydev))
1320 return 0;
1321
1322 /* Compare Advertisement and Link Partner register 1 */
1323 ad_ret = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_ADVERTISE);
1324 if (ad_ret < 0)
1325 return ad_ret;
1326 lp_ret = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_LPA);
1327 if (lp_ret < 0)
1328 return lp_ret;
1329
1330 ad_ret &= lp_ret;
1331 phydev->pause = (ad_ret & 0x400) ? 1 : 0;
1332 phydev->asym_pause = (ad_ret & 0x800) ? 1 : 0;
1333
1334 /* Compare Advertisement and Link Partner register 2 */
1335 ad_ret = phy_read_mmd(phydev, MDIO_MMD_AN,
1336 MDIO_AN_ADVERTISE + 1);
1337 if (ad_ret < 0)
1338 return ad_ret;
1339 lp_ret = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_LPA + 1);
1340 if (lp_ret < 0)
1341 return lp_ret;
1342
1343 ad_ret &= lp_ret;
1344 if (ad_ret & 0x80) {
1345 phydev->speed = SPEED_10000;
Lendacky, Thomase3eec4e2014-09-03 12:14:16 -05001346 ret = amd_xgbe_phy_set_mode(phydev, AMD_XGBE_MODE_KR);
1347 if (ret)
1348 return ret;
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001349 } else {
Lendacky, Thomase3eec4e2014-09-03 12:14:16 -05001350 switch (priv->speed_set) {
1351 case AMD_XGBE_PHY_SPEEDSET_1000_10000:
Lendacky, Thomasf0476042014-07-29 08:57:25 -05001352 phydev->speed = SPEED_1000;
Lendacky, Thomase3eec4e2014-09-03 12:14:16 -05001353 break;
1354
1355 case AMD_XGBE_PHY_SPEEDSET_2500_10000:
Lendacky, Thomasf0476042014-07-29 08:57:25 -05001356 phydev->speed = SPEED_2500;
Lendacky, Thomase3eec4e2014-09-03 12:14:16 -05001357 break;
Lendacky, Thomasf0476042014-07-29 08:57:25 -05001358 }
1359
Lendacky, Thomase3eec4e2014-09-03 12:14:16 -05001360 ret = amd_xgbe_phy_set_mode(phydev, AMD_XGBE_MODE_KX);
1361 if (ret)
1362 return ret;
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001363 }
1364
1365 phydev->duplex = DUPLEX_FULL;
1366 } else {
Lendacky, Thomase3eec4e2014-09-03 12:14:16 -05001367 if (amd_xgbe_phy_in_kr_mode(phydev)) {
Lendacky, Thomasf0476042014-07-29 08:57:25 -05001368 phydev->speed = SPEED_10000;
1369 } else {
Lendacky, Thomase3eec4e2014-09-03 12:14:16 -05001370 switch (priv->speed_set) {
1371 case AMD_XGBE_PHY_SPEEDSET_1000_10000:
Lendacky, Thomasf0476042014-07-29 08:57:25 -05001372 phydev->speed = SPEED_1000;
Lendacky, Thomase3eec4e2014-09-03 12:14:16 -05001373 break;
1374
1375 case AMD_XGBE_PHY_SPEEDSET_2500_10000:
Lendacky, Thomasf0476042014-07-29 08:57:25 -05001376 phydev->speed = SPEED_2500;
Lendacky, Thomase3eec4e2014-09-03 12:14:16 -05001377 break;
1378 }
Lendacky, Thomasf0476042014-07-29 08:57:25 -05001379 }
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001380 phydev->duplex = DUPLEX_FULL;
1381 phydev->pause = 0;
1382 phydev->asym_pause = 0;
1383 }
1384
1385 return 0;
1386}
1387
1388static int amd_xgbe_phy_suspend(struct phy_device *phydev)
1389{
Lendacky, Thomas03e50fd2015-01-16 12:46:39 -06001390 struct amd_xgbe_phy_priv *priv = phydev->priv;
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001391 int ret;
1392
1393 mutex_lock(&phydev->lock);
1394
1395 ret = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1);
1396 if (ret < 0)
1397 goto unlock;
1398
Lendacky, Thomas03e50fd2015-01-16 12:46:39 -06001399 priv->lpm_ctrl = ret;
1400
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001401 ret |= MDIO_CTRL1_LPOWER;
1402 phy_write_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1, ret);
1403
1404 ret = 0;
1405
1406unlock:
1407 mutex_unlock(&phydev->lock);
1408
1409 return ret;
1410}
1411
1412static int amd_xgbe_phy_resume(struct phy_device *phydev)
1413{
Lendacky, Thomas03e50fd2015-01-16 12:46:39 -06001414 struct amd_xgbe_phy_priv *priv = phydev->priv;
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001415
1416 mutex_lock(&phydev->lock);
1417
Lendacky, Thomas03e50fd2015-01-16 12:46:39 -06001418 priv->lpm_ctrl &= ~MDIO_CTRL1_LPOWER;
1419 phy_write_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1, priv->lpm_ctrl);
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001420
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001421 mutex_unlock(&phydev->lock);
1422
Lendacky, Thomas03e50fd2015-01-16 12:46:39 -06001423 return 0;
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001424}
1425
Lendacky, Thomas82a19032015-01-16 12:47:16 -06001426static unsigned int amd_xgbe_phy_resource_count(struct platform_device *pdev,
1427 unsigned int type)
1428{
1429 unsigned int count;
1430 int i;
1431
1432 for (i = 0, count = 0; i < pdev->num_resources; i++) {
1433 struct resource *r = &pdev->resource[i];
1434
1435 if (type == resource_type(r))
1436 count++;
1437 }
1438
1439 return count;
1440}
1441
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001442static int amd_xgbe_phy_probe(struct phy_device *phydev)
1443{
1444 struct amd_xgbe_phy_priv *priv;
Lendacky, Thomas82a19032015-01-16 12:47:16 -06001445 struct platform_device *phy_pdev;
1446 struct device *dev, *phy_dev;
1447 unsigned int phy_resnum, phy_irqnum;
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001448 int ret;
1449
Lendacky, Thomas82a19032015-01-16 12:47:16 -06001450 if (!phydev->bus || !phydev->bus->parent)
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001451 return -EINVAL;
1452
Lendacky, Thomas82a19032015-01-16 12:47:16 -06001453 dev = phydev->bus->parent;
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001454
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001455 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
Lendacky, Thomas82a19032015-01-16 12:47:16 -06001456 if (!priv)
1457 return -ENOMEM;
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001458
Lendacky, Thomas82a19032015-01-16 12:47:16 -06001459 priv->pdev = to_platform_device(dev);
1460 priv->adev = ACPI_COMPANION(dev);
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001461 priv->dev = dev;
1462 priv->phydev = phydev;
Lendacky, Thomasc3152d42015-01-16 12:47:00 -06001463 mutex_init(&priv->an_mutex);
1464 INIT_WORK(&priv->an_irq_work, amd_xgbe_an_irq_work);
1465 INIT_WORK(&priv->an_work, amd_xgbe_an_state_machine);
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001466
Lendacky, Thomas82a19032015-01-16 12:47:16 -06001467 if (!priv->adev || acpi_disabled) {
1468 struct device_node *bus_node;
1469 struct device_node *phy_node;
1470
1471 bus_node = priv->dev->of_node;
1472 phy_node = of_parse_phandle(bus_node, "phy-handle", 0);
1473 if (!phy_node) {
1474 dev_err(dev, "unable to parse phy-handle\n");
1475 ret = -EINVAL;
1476 goto err_priv;
1477 }
1478
1479 phy_pdev = of_find_device_by_node(phy_node);
1480 of_node_put(phy_node);
1481
1482 if (!phy_pdev) {
1483 dev_err(dev, "unable to obtain phy device\n");
1484 ret = -EINVAL;
1485 goto err_priv;
1486 }
1487
1488 phy_resnum = 0;
1489 phy_irqnum = 0;
1490 } else {
1491 /* In ACPI, the XGBE and PHY resources are the grouped
1492 * together with the PHY resources at the end
1493 */
1494 phy_pdev = priv->pdev;
1495 phy_resnum = amd_xgbe_phy_resource_count(phy_pdev,
1496 IORESOURCE_MEM) - 3;
1497 phy_irqnum = amd_xgbe_phy_resource_count(phy_pdev,
1498 IORESOURCE_IRQ) - 1;
1499 }
1500 phy_dev = &phy_pdev->dev;
1501
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001502 /* Get the device mmio areas */
Lendacky, Thomas82a19032015-01-16 12:47:16 -06001503 priv->rxtx_res = platform_get_resource(phy_pdev, IORESOURCE_MEM,
1504 phy_resnum++);
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001505 priv->rxtx_regs = devm_ioremap_resource(dev, priv->rxtx_res);
1506 if (IS_ERR(priv->rxtx_regs)) {
1507 dev_err(dev, "rxtx ioremap failed\n");
1508 ret = PTR_ERR(priv->rxtx_regs);
Lendacky, Thomas82a19032015-01-16 12:47:16 -06001509 goto err_put;
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001510 }
1511
Lendacky, Thomas82a19032015-01-16 12:47:16 -06001512 priv->sir0_res = platform_get_resource(phy_pdev, IORESOURCE_MEM,
1513 phy_resnum++);
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001514 priv->sir0_regs = devm_ioremap_resource(dev, priv->sir0_res);
1515 if (IS_ERR(priv->sir0_regs)) {
1516 dev_err(dev, "sir0 ioremap failed\n");
1517 ret = PTR_ERR(priv->sir0_regs);
1518 goto err_rxtx;
1519 }
1520
Lendacky, Thomas82a19032015-01-16 12:47:16 -06001521 priv->sir1_res = platform_get_resource(phy_pdev, IORESOURCE_MEM,
1522 phy_resnum++);
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001523 priv->sir1_regs = devm_ioremap_resource(dev, priv->sir1_res);
1524 if (IS_ERR(priv->sir1_regs)) {
1525 dev_err(dev, "sir1 ioremap failed\n");
1526 ret = PTR_ERR(priv->sir1_regs);
1527 goto err_sir0;
1528 }
1529
Lendacky, Thomasc3152d42015-01-16 12:47:00 -06001530 /* Get the auto-negotiation interrupt */
Lendacky, Thomas82a19032015-01-16 12:47:16 -06001531 ret = platform_get_irq(phy_pdev, phy_irqnum);
Lendacky, Thomasc3152d42015-01-16 12:47:00 -06001532 if (ret < 0) {
1533 dev_err(dev, "platform_get_irq failed\n");
1534 goto err_sir1;
1535 }
1536 priv->an_irq = ret;
1537
Lendacky, Thomasf0476042014-07-29 08:57:25 -05001538 /* Get the device speed set property */
Lendacky, Thomas82a19032015-01-16 12:47:16 -06001539 ret = device_property_read_u32(phy_dev, XGBE_PHY_SPEEDSET_PROPERTY,
1540 &priv->speed_set);
1541 if (ret) {
1542 dev_err(dev, "invalid %s property\n",
1543 XGBE_PHY_SPEEDSET_PROPERTY);
1544 goto err_sir1;
1545 }
Lendacky, Thomasf0476042014-07-29 08:57:25 -05001546
Lendacky, Thomas82a19032015-01-16 12:47:16 -06001547 switch (priv->speed_set) {
1548 case AMD_XGBE_PHY_SPEEDSET_1000_10000:
1549 case AMD_XGBE_PHY_SPEEDSET_2500_10000:
Lendacky, Thomasf0476042014-07-29 08:57:25 -05001550 break;
1551 default:
Lendacky, Thomas82a19032015-01-16 12:47:16 -06001552 dev_err(dev, "invalid %s property\n",
1553 XGBE_PHY_SPEEDSET_PROPERTY);
Lendacky, Thomasf0476042014-07-29 08:57:25 -05001554 ret = -EINVAL;
1555 goto err_sir1;
1556 }
1557
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001558 phydev->priv = priv;
1559
Lendacky, Thomas82a19032015-01-16 12:47:16 -06001560 if (!priv->adev || acpi_disabled)
1561 platform_device_put(phy_pdev);
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001562
1563 return 0;
1564
1565err_sir1:
1566 devm_iounmap(dev, priv->sir1_regs);
1567 devm_release_mem_region(dev, priv->sir1_res->start,
1568 resource_size(priv->sir1_res));
1569
1570err_sir0:
1571 devm_iounmap(dev, priv->sir0_regs);
1572 devm_release_mem_region(dev, priv->sir0_res->start,
1573 resource_size(priv->sir0_res));
1574
1575err_rxtx:
1576 devm_iounmap(dev, priv->rxtx_regs);
1577 devm_release_mem_region(dev, priv->rxtx_res->start,
1578 resource_size(priv->rxtx_res));
1579
Lendacky, Thomas82a19032015-01-16 12:47:16 -06001580err_put:
1581 if (!priv->adev || acpi_disabled)
1582 platform_device_put(phy_pdev);
1583
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001584err_priv:
1585 devm_kfree(dev, priv);
1586
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001587 return ret;
1588}
1589
1590static void amd_xgbe_phy_remove(struct phy_device *phydev)
1591{
1592 struct amd_xgbe_phy_priv *priv = phydev->priv;
1593 struct device *dev = priv->dev;
1594
Lendacky, Thomasc3152d42015-01-16 12:47:00 -06001595 if (priv->an_irq_allocated) {
1596 devm_free_irq(dev, priv->an_irq, priv);
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001597
Lendacky, Thomasc3152d42015-01-16 12:47:00 -06001598 flush_workqueue(priv->an_workqueue);
1599 destroy_workqueue(priv->an_workqueue);
1600 }
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001601
1602 /* Release resources */
1603 devm_iounmap(dev, priv->sir1_regs);
1604 devm_release_mem_region(dev, priv->sir1_res->start,
1605 resource_size(priv->sir1_res));
1606
1607 devm_iounmap(dev, priv->sir0_regs);
1608 devm_release_mem_region(dev, priv->sir0_res->start,
1609 resource_size(priv->sir0_res));
1610
1611 devm_iounmap(dev, priv->rxtx_regs);
1612 devm_release_mem_region(dev, priv->rxtx_res->start,
1613 resource_size(priv->rxtx_res));
1614
1615 devm_kfree(dev, priv);
1616}
1617
1618static int amd_xgbe_match_phy_device(struct phy_device *phydev)
1619{
1620 return phydev->c45_ids.device_ids[MDIO_MMD_PCS] == XGBE_PHY_ID;
1621}
1622
1623static struct phy_driver amd_xgbe_phy_driver[] = {
1624 {
1625 .phy_id = XGBE_PHY_ID,
1626 .phy_id_mask = XGBE_PHY_MASK,
1627 .name = "AMD XGBE PHY",
1628 .features = 0,
1629 .probe = amd_xgbe_phy_probe,
1630 .remove = amd_xgbe_phy_remove,
1631 .soft_reset = amd_xgbe_phy_soft_reset,
1632 .config_init = amd_xgbe_phy_config_init,
1633 .suspend = amd_xgbe_phy_suspend,
1634 .resume = amd_xgbe_phy_resume,
1635 .config_aneg = amd_xgbe_phy_config_aneg,
1636 .aneg_done = amd_xgbe_phy_aneg_done,
1637 .read_status = amd_xgbe_phy_read_status,
1638 .match_phy_device = amd_xgbe_match_phy_device,
1639 .driver = {
1640 .owner = THIS_MODULE,
1641 },
1642 },
1643};
1644
Johan Hovold50fd7152014-11-11 19:45:59 +01001645module_phy_driver(amd_xgbe_phy_driver);
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001646
françois romieua25aafa2014-06-07 11:07:48 +02001647static struct mdio_device_id __maybe_unused amd_xgbe_phy_ids[] = {
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001648 { XGBE_PHY_ID, XGBE_PHY_MASK },
1649 { }
1650};
1651MODULE_DEVICE_TABLE(mdio, amd_xgbe_phy_ids);