blob: e2074c75891c98e8bce254922f1817be70d2d6c6 [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, Thomas4d874b32014-06-05 09:15:12 -050079
Lendacky, Thomas4d874b32014-06-05 09:15:12 -050080MODULE_AUTHOR("Tom Lendacky <thomas.lendacky@amd.com>");
81MODULE_LICENSE("Dual BSD/GPL");
82MODULE_VERSION("1.0.0-a");
83MODULE_DESCRIPTION("AMD 10GbE (amd-xgbe) PHY driver");
84
85#define XGBE_PHY_ID 0x000162d0
86#define XGBE_PHY_MASK 0xfffffff0
87
Lendacky, Thomasf0476042014-07-29 08:57:25 -050088#define XGBE_PHY_SPEEDSET_PROPERTY "amd,speed-set"
89
Lendacky, Thomas4d874b32014-06-05 09:15:12 -050090#define XGBE_AN_INT_CMPLT 0x01
91#define XGBE_AN_INC_LINK 0x02
92#define XGBE_AN_PG_RCV 0x04
Lendacky, Thomasc3152d42015-01-16 12:47:00 -060093#define XGBE_AN_INT_MASK 0x07
Lendacky, Thomas4d874b32014-06-05 09:15:12 -050094
95#define XNP_MCF_NULL_MESSAGE 0x001
Lendacky, Thomascb69cb02015-01-16 12:46:29 -060096#define XNP_ACK_PROCESSED BIT(12)
97#define XNP_MP_FORMATTED BIT(13)
98#define XNP_NP_EXCHANGE BIT(15)
Lendacky, Thomas4d874b32014-06-05 09:15:12 -050099
Lendacky, Thomas1fa1f2e2014-08-01 11:56:36 -0500100#define XGBE_PHY_RATECHANGE_COUNT 500
Lendacky, Thomas169a6302014-07-29 08:57:37 -0500101
Lendacky, Thomascf262522015-01-16 12:47:05 -0600102#define XGBE_PHY_KR_TRAINING_START 0x01
103#define XGBE_PHY_KR_TRAINING_ENABLE 0x02
104
105#define XGBE_PHY_FEC_ENABLE 0x01
106#define XGBE_PHY_FEC_FORWARD 0x02
107#define XGBE_PHY_FEC_MASK 0x03
108
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500109#ifndef MDIO_PMA_10GBR_PMD_CTRL
110#define MDIO_PMA_10GBR_PMD_CTRL 0x0096
111#endif
Lendacky, Thomase6f05622014-09-03 12:14:22 -0500112
Lendacky, Thomascf262522015-01-16 12:47:05 -0600113#ifndef MDIO_PMA_10GBR_FEC_ABILITY
114#define MDIO_PMA_10GBR_FEC_ABILITY 0x00aa
115#endif
116
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500117#ifndef MDIO_PMA_10GBR_FEC_CTRL
118#define MDIO_PMA_10GBR_FEC_CTRL 0x00ab
119#endif
Lendacky, Thomase6f05622014-09-03 12:14:22 -0500120
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500121#ifndef MDIO_AN_XNP
122#define MDIO_AN_XNP 0x0016
123#endif
124
Lendacky, Thomas0d40b612015-01-16 12:47:10 -0600125#ifndef MDIO_AN_LPX
126#define MDIO_AN_LPX 0x0019
127#endif
128
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500129#ifndef MDIO_AN_INTMASK
130#define MDIO_AN_INTMASK 0x8001
131#endif
Lendacky, Thomase6f05622014-09-03 12:14:22 -0500132
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500133#ifndef MDIO_AN_INT
134#define MDIO_AN_INT 0x8002
135#endif
136
137#ifndef MDIO_CTRL1_SPEED1G
138#define MDIO_CTRL1_SPEED1G (MDIO_CTRL1_SPEED10G & ~BMCR_SPEED100)
139#endif
140
141/* SerDes integration register offsets */
Lendacky, Thomas5c10e5c2014-07-29 08:57:43 -0500142#define SIR0_KR_RT_1 0x002c
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500143#define SIR0_STATUS 0x0040
144#define SIR1_SPEED 0x0000
145
146/* SerDes integration register entry bit positions and sizes */
Lendacky, Thomas5c10e5c2014-07-29 08:57:43 -0500147#define SIR0_KR_RT_1_RESET_INDEX 11
148#define SIR0_KR_RT_1_RESET_WIDTH 1
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500149#define SIR0_STATUS_RX_READY_INDEX 0
150#define SIR0_STATUS_RX_READY_WIDTH 1
151#define SIR0_STATUS_TX_READY_INDEX 8
152#define SIR0_STATUS_TX_READY_WIDTH 1
153#define SIR1_SPEED_DATARATE_INDEX 4
154#define SIR1_SPEED_DATARATE_WIDTH 2
155#define SIR1_SPEED_PI_SPD_SEL_INDEX 12
156#define SIR1_SPEED_PI_SPD_SEL_WIDTH 4
157#define SIR1_SPEED_PLLSEL_INDEX 3
158#define SIR1_SPEED_PLLSEL_WIDTH 1
159#define SIR1_SPEED_RATECHANGE_INDEX 6
160#define SIR1_SPEED_RATECHANGE_WIDTH 1
161#define SIR1_SPEED_TXAMP_INDEX 8
162#define SIR1_SPEED_TXAMP_WIDTH 4
163#define SIR1_SPEED_WORDMODE_INDEX 0
164#define SIR1_SPEED_WORDMODE_WIDTH 3
165
166#define SPEED_10000_CDR 0x7
167#define SPEED_10000_PLL 0x1
168#define SPEED_10000_RATE 0x0
169#define SPEED_10000_TXAMP 0xa
170#define SPEED_10000_WORD 0x7
171
172#define SPEED_2500_CDR 0x2
173#define SPEED_2500_PLL 0x0
Lendacky, Thomasf0476042014-07-29 08:57:25 -0500174#define SPEED_2500_RATE 0x1
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500175#define SPEED_2500_TXAMP 0xf
176#define SPEED_2500_WORD 0x1
177
178#define SPEED_1000_CDR 0x2
179#define SPEED_1000_PLL 0x0
180#define SPEED_1000_RATE 0x3
181#define SPEED_1000_TXAMP 0xf
182#define SPEED_1000_WORD 0x1
183
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500184/* SerDes RxTx register offsets */
185#define RXTX_REG20 0x0050
186#define RXTX_REG114 0x01c8
187
188/* SerDes RxTx register entry bit positions and sizes */
189#define RXTX_REG20_BLWC_ENA_INDEX 2
190#define RXTX_REG20_BLWC_ENA_WIDTH 1
191#define RXTX_REG114_PQ_REG_INDEX 9
192#define RXTX_REG114_PQ_REG_WIDTH 7
193
194#define RXTX_10000_BLWC 0
195#define RXTX_10000_PQ 0x1e
196
197#define RXTX_2500_BLWC 1
198#define RXTX_2500_PQ 0xa
199
200#define RXTX_1000_BLWC 1
201#define RXTX_1000_PQ 0xa
202
203/* Bit setting and getting macros
204 * The get macro will extract the current bit field value from within
205 * the variable
206 *
207 * The set macro will clear the current bit field value within the
208 * variable and then set the bit field of the variable to the
209 * specified value
210 */
211#define GET_BITS(_var, _index, _width) \
212 (((_var) >> (_index)) & ((0x1 << (_width)) - 1))
213
214#define SET_BITS(_var, _index, _width, _val) \
215do { \
216 (_var) &= ~(((0x1 << (_width)) - 1) << (_index)); \
217 (_var) |= (((_val) & ((0x1 << (_width)) - 1)) << (_index)); \
218} while (0)
219
Lendacky, Thomas169a6302014-07-29 08:57:37 -0500220#define XSIR_GET_BITS(_var, _prefix, _field) \
221 GET_BITS((_var), \
222 _prefix##_##_field##_INDEX, \
223 _prefix##_##_field##_WIDTH)
224
225#define XSIR_SET_BITS(_var, _prefix, _field, _val) \
226 SET_BITS((_var), \
227 _prefix##_##_field##_INDEX, \
228 _prefix##_##_field##_WIDTH, (_val))
229
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500230/* Macros for reading or writing SerDes integration registers
231 * The ioread macros will get bit fields or full values using the
232 * register definitions formed using the input names
233 *
234 * The iowrite macros will set bit fields or full values using the
235 * register definitions formed using the input names
236 */
237#define XSIR0_IOREAD(_priv, _reg) \
238 ioread16((_priv)->sir0_regs + _reg)
239
240#define XSIR0_IOREAD_BITS(_priv, _reg, _field) \
241 GET_BITS(XSIR0_IOREAD((_priv), _reg), \
242 _reg##_##_field##_INDEX, \
243 _reg##_##_field##_WIDTH)
244
245#define XSIR0_IOWRITE(_priv, _reg, _val) \
246 iowrite16((_val), (_priv)->sir0_regs + _reg)
247
248#define XSIR0_IOWRITE_BITS(_priv, _reg, _field, _val) \
249do { \
250 u16 reg_val = XSIR0_IOREAD((_priv), _reg); \
251 SET_BITS(reg_val, \
252 _reg##_##_field##_INDEX, \
253 _reg##_##_field##_WIDTH, (_val)); \
254 XSIR0_IOWRITE((_priv), _reg, reg_val); \
255} while (0)
256
257#define XSIR1_IOREAD(_priv, _reg) \
258 ioread16((_priv)->sir1_regs + _reg)
259
260#define XSIR1_IOREAD_BITS(_priv, _reg, _field) \
261 GET_BITS(XSIR1_IOREAD((_priv), _reg), \
262 _reg##_##_field##_INDEX, \
263 _reg##_##_field##_WIDTH)
264
265#define XSIR1_IOWRITE(_priv, _reg, _val) \
266 iowrite16((_val), (_priv)->sir1_regs + _reg)
267
268#define XSIR1_IOWRITE_BITS(_priv, _reg, _field, _val) \
269do { \
270 u16 reg_val = XSIR1_IOREAD((_priv), _reg); \
271 SET_BITS(reg_val, \
272 _reg##_##_field##_INDEX, \
273 _reg##_##_field##_WIDTH, (_val)); \
274 XSIR1_IOWRITE((_priv), _reg, reg_val); \
275} while (0)
276
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500277/* Macros for reading or writing SerDes RxTx registers
278 * The ioread macros will get bit fields or full values using the
279 * register definitions formed using the input names
280 *
281 * The iowrite macros will set bit fields or full values using the
282 * register definitions formed using the input names
283 */
284#define XRXTX_IOREAD(_priv, _reg) \
285 ioread16((_priv)->rxtx_regs + _reg)
286
287#define XRXTX_IOREAD_BITS(_priv, _reg, _field) \
288 GET_BITS(XRXTX_IOREAD((_priv), _reg), \
289 _reg##_##_field##_INDEX, \
290 _reg##_##_field##_WIDTH)
291
292#define XRXTX_IOWRITE(_priv, _reg, _val) \
293 iowrite16((_val), (_priv)->rxtx_regs + _reg)
294
295#define XRXTX_IOWRITE_BITS(_priv, _reg, _field, _val) \
296do { \
297 u16 reg_val = XRXTX_IOREAD((_priv), _reg); \
298 SET_BITS(reg_val, \
299 _reg##_##_field##_INDEX, \
300 _reg##_##_field##_WIDTH, (_val)); \
301 XRXTX_IOWRITE((_priv), _reg, reg_val); \
302} while (0)
303
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500304enum amd_xgbe_phy_an {
305 AMD_XGBE_AN_READY = 0,
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500306 AMD_XGBE_AN_PAGE_RECEIVED,
307 AMD_XGBE_AN_INCOMPAT_LINK,
308 AMD_XGBE_AN_COMPLETE,
309 AMD_XGBE_AN_NO_LINK,
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500310 AMD_XGBE_AN_ERROR,
311};
312
313enum amd_xgbe_phy_rx {
Lendacky, Thomasc3152d42015-01-16 12:47:00 -0600314 AMD_XGBE_RX_BPA = 0,
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500315 AMD_XGBE_RX_XNP,
316 AMD_XGBE_RX_COMPLETE,
Lendacky, Thomasc3152d42015-01-16 12:47:00 -0600317 AMD_XGBE_RX_ERROR,
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500318};
319
320enum amd_xgbe_phy_mode {
321 AMD_XGBE_MODE_KR,
322 AMD_XGBE_MODE_KX,
323};
324
Lendacky, Thomasf0476042014-07-29 08:57:25 -0500325enum amd_xgbe_phy_speedset {
326 AMD_XGBE_PHY_SPEEDSET_1000_10000,
327 AMD_XGBE_PHY_SPEEDSET_2500_10000,
328};
329
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500330struct amd_xgbe_phy_priv {
331 struct platform_device *pdev;
332 struct device *dev;
333
334 struct phy_device *phydev;
335
336 /* SerDes related mmio resources */
337 struct resource *rxtx_res;
338 struct resource *sir0_res;
339 struct resource *sir1_res;
340
341 /* SerDes related mmio registers */
342 void __iomem *rxtx_regs; /* SerDes Rx/Tx CSRs */
343 void __iomem *sir0_regs; /* SerDes integration registers (1/2) */
344 void __iomem *sir1_regs; /* SerDes integration registers (2/2) */
345
Lendacky, Thomasc3152d42015-01-16 12:47:00 -0600346 int an_irq;
347 char an_irq_name[IFNAMSIZ + 32];
348 struct work_struct an_irq_work;
349 unsigned int an_irq_allocated;
350
Lendacky, Thomasf0476042014-07-29 08:57:25 -0500351 unsigned int speed_set;
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500352
353 /* Auto-negotiation state machine support */
354 struct mutex an_mutex;
355 enum amd_xgbe_phy_an an_result;
356 enum amd_xgbe_phy_an an_state;
357 enum amd_xgbe_phy_rx kr_state;
358 enum amd_xgbe_phy_rx kx_state;
359 struct work_struct an_work;
360 struct workqueue_struct *an_workqueue;
Lendacky, Thomasc3152d42015-01-16 12:47:00 -0600361 unsigned int an_supported;
Lendacky, Thomase6f05622014-09-03 12:14:22 -0500362 unsigned int parallel_detect;
Lendacky, Thomascf262522015-01-16 12:47:05 -0600363 unsigned int fec_ability;
Lendacky, Thomas03e50fd2015-01-16 12:46:39 -0600364
365 unsigned int lpm_ctrl; /* CTRL1 for resume */
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500366};
367
368static int amd_xgbe_an_enable_kr_training(struct phy_device *phydev)
369{
370 int ret;
371
372 ret = phy_read_mmd(phydev, MDIO_MMD_PMAPMD, MDIO_PMA_10GBR_PMD_CTRL);
373 if (ret < 0)
374 return ret;
375
Lendacky, Thomascf262522015-01-16 12:47:05 -0600376 ret |= XGBE_PHY_KR_TRAINING_ENABLE;
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500377 phy_write_mmd(phydev, MDIO_MMD_PMAPMD, MDIO_PMA_10GBR_PMD_CTRL, ret);
378
379 return 0;
380}
381
382static int amd_xgbe_an_disable_kr_training(struct phy_device *phydev)
383{
384 int ret;
385
386 ret = phy_read_mmd(phydev, MDIO_MMD_PMAPMD, MDIO_PMA_10GBR_PMD_CTRL);
387 if (ret < 0)
388 return ret;
389
Lendacky, Thomascf262522015-01-16 12:47:05 -0600390 ret &= ~XGBE_PHY_KR_TRAINING_ENABLE;
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500391 phy_write_mmd(phydev, MDIO_MMD_PMAPMD, MDIO_PMA_10GBR_PMD_CTRL, ret);
392
393 return 0;
394}
395
396static int amd_xgbe_phy_pcs_power_cycle(struct phy_device *phydev)
397{
398 int ret;
399
400 ret = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1);
401 if (ret < 0)
402 return ret;
403
404 ret |= MDIO_CTRL1_LPOWER;
405 phy_write_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1, ret);
406
407 usleep_range(75, 100);
408
409 ret &= ~MDIO_CTRL1_LPOWER;
410 phy_write_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1, ret);
411
412 return 0;
413}
414
415static void amd_xgbe_phy_serdes_start_ratechange(struct phy_device *phydev)
416{
417 struct amd_xgbe_phy_priv *priv = phydev->priv;
418
419 /* Assert Rx and Tx ratechange */
420 XSIR1_IOWRITE_BITS(priv, SIR1_SPEED, RATECHANGE, 1);
421}
422
423static void amd_xgbe_phy_serdes_complete_ratechange(struct phy_device *phydev)
424{
425 struct amd_xgbe_phy_priv *priv = phydev->priv;
Lendacky, Thomas169a6302014-07-29 08:57:37 -0500426 unsigned int wait;
427 u16 status;
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500428
429 /* Release Rx and Tx ratechange */
430 XSIR1_IOWRITE_BITS(priv, SIR1_SPEED, RATECHANGE, 0);
431
432 /* Wait for Rx and Tx ready */
Lendacky, Thomas169a6302014-07-29 08:57:37 -0500433 wait = XGBE_PHY_RATECHANGE_COUNT;
434 while (wait--) {
Lendacky, Thomas1fa1f2e2014-08-01 11:56:36 -0500435 usleep_range(50, 75);
Lendacky, Thomas169a6302014-07-29 08:57:37 -0500436
437 status = XSIR0_IOREAD(priv, SIR0_STATUS);
438 if (XSIR_GET_BITS(status, SIR0_STATUS, RX_READY) &&
439 XSIR_GET_BITS(status, SIR0_STATUS, TX_READY))
440 return;
441 }
442
Lendacky, Thomas1fa1f2e2014-08-01 11:56:36 -0500443 netdev_dbg(phydev->attached_dev, "SerDes rx/tx not ready (%#hx)\n",
Lendacky, Thomas169a6302014-07-29 08:57:37 -0500444 status);
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500445}
446
447static int amd_xgbe_phy_xgmii_mode(struct phy_device *phydev)
448{
449 struct amd_xgbe_phy_priv *priv = phydev->priv;
450 int ret;
451
452 /* Enable KR training */
453 ret = amd_xgbe_an_enable_kr_training(phydev);
454 if (ret < 0)
455 return ret;
456
457 /* Set PCS to KR/10G speed */
458 ret = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL2);
459 if (ret < 0)
460 return ret;
461
462 ret &= ~MDIO_PCS_CTRL2_TYPE;
463 ret |= MDIO_PCS_CTRL2_10GBR;
464 phy_write_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL2, ret);
465
466 ret = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1);
467 if (ret < 0)
468 return ret;
469
470 ret &= ~MDIO_CTRL1_SPEEDSEL;
471 ret |= MDIO_CTRL1_SPEED10G;
472 phy_write_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1, ret);
473
474 ret = amd_xgbe_phy_pcs_power_cycle(phydev);
475 if (ret < 0)
476 return ret;
477
478 /* Set SerDes to 10G speed */
479 amd_xgbe_phy_serdes_start_ratechange(phydev);
480
481 XSIR1_IOWRITE_BITS(priv, SIR1_SPEED, DATARATE, SPEED_10000_RATE);
482 XSIR1_IOWRITE_BITS(priv, SIR1_SPEED, WORDMODE, SPEED_10000_WORD);
483 XSIR1_IOWRITE_BITS(priv, SIR1_SPEED, TXAMP, SPEED_10000_TXAMP);
484 XSIR1_IOWRITE_BITS(priv, SIR1_SPEED, PLLSEL, SPEED_10000_PLL);
485 XSIR1_IOWRITE_BITS(priv, SIR1_SPEED, PI_SPD_SEL, SPEED_10000_CDR);
486
487 XRXTX_IOWRITE_BITS(priv, RXTX_REG20, BLWC_ENA, RXTX_10000_BLWC);
488 XRXTX_IOWRITE_BITS(priv, RXTX_REG114, PQ_REG, RXTX_10000_PQ);
489
490 amd_xgbe_phy_serdes_complete_ratechange(phydev);
491
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500492 return 0;
493}
494
495static int amd_xgbe_phy_gmii_2500_mode(struct phy_device *phydev)
496{
497 struct amd_xgbe_phy_priv *priv = phydev->priv;
498 int ret;
499
500 /* Disable KR training */
501 ret = amd_xgbe_an_disable_kr_training(phydev);
502 if (ret < 0)
503 return ret;
504
505 /* Set PCS to KX/1G speed */
506 ret = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL2);
507 if (ret < 0)
508 return ret;
509
510 ret &= ~MDIO_PCS_CTRL2_TYPE;
511 ret |= MDIO_PCS_CTRL2_10GBX;
512 phy_write_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL2, ret);
513
514 ret = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1);
515 if (ret < 0)
516 return ret;
517
518 ret &= ~MDIO_CTRL1_SPEEDSEL;
519 ret |= MDIO_CTRL1_SPEED1G;
520 phy_write_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1, ret);
521
522 ret = amd_xgbe_phy_pcs_power_cycle(phydev);
523 if (ret < 0)
524 return ret;
525
526 /* Set SerDes to 2.5G speed */
527 amd_xgbe_phy_serdes_start_ratechange(phydev);
528
529 XSIR1_IOWRITE_BITS(priv, SIR1_SPEED, DATARATE, SPEED_2500_RATE);
530 XSIR1_IOWRITE_BITS(priv, SIR1_SPEED, WORDMODE, SPEED_2500_WORD);
531 XSIR1_IOWRITE_BITS(priv, SIR1_SPEED, TXAMP, SPEED_2500_TXAMP);
532 XSIR1_IOWRITE_BITS(priv, SIR1_SPEED, PLLSEL, SPEED_2500_PLL);
533 XSIR1_IOWRITE_BITS(priv, SIR1_SPEED, PI_SPD_SEL, SPEED_2500_CDR);
534
535 XRXTX_IOWRITE_BITS(priv, RXTX_REG20, BLWC_ENA, RXTX_2500_BLWC);
536 XRXTX_IOWRITE_BITS(priv, RXTX_REG114, PQ_REG, RXTX_2500_PQ);
537
538 amd_xgbe_phy_serdes_complete_ratechange(phydev);
539
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500540 return 0;
541}
542
543static int amd_xgbe_phy_gmii_mode(struct phy_device *phydev)
544{
545 struct amd_xgbe_phy_priv *priv = phydev->priv;
546 int ret;
547
548 /* Disable KR training */
549 ret = amd_xgbe_an_disable_kr_training(phydev);
550 if (ret < 0)
551 return ret;
552
553 /* Set PCS to KX/1G speed */
554 ret = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL2);
555 if (ret < 0)
556 return ret;
557
558 ret &= ~MDIO_PCS_CTRL2_TYPE;
559 ret |= MDIO_PCS_CTRL2_10GBX;
560 phy_write_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL2, ret);
561
562 ret = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1);
563 if (ret < 0)
564 return ret;
565
566 ret &= ~MDIO_CTRL1_SPEEDSEL;
567 ret |= MDIO_CTRL1_SPEED1G;
568 phy_write_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1, ret);
569
570 ret = amd_xgbe_phy_pcs_power_cycle(phydev);
571 if (ret < 0)
572 return ret;
573
574 /* Set SerDes to 1G speed */
575 amd_xgbe_phy_serdes_start_ratechange(phydev);
576
577 XSIR1_IOWRITE_BITS(priv, SIR1_SPEED, DATARATE, SPEED_1000_RATE);
578 XSIR1_IOWRITE_BITS(priv, SIR1_SPEED, WORDMODE, SPEED_1000_WORD);
579 XSIR1_IOWRITE_BITS(priv, SIR1_SPEED, TXAMP, SPEED_1000_TXAMP);
580 XSIR1_IOWRITE_BITS(priv, SIR1_SPEED, PLLSEL, SPEED_1000_PLL);
581 XSIR1_IOWRITE_BITS(priv, SIR1_SPEED, PI_SPD_SEL, SPEED_1000_CDR);
582
583 XRXTX_IOWRITE_BITS(priv, RXTX_REG20, BLWC_ENA, RXTX_1000_BLWC);
584 XRXTX_IOWRITE_BITS(priv, RXTX_REG114, PQ_REG, RXTX_1000_PQ);
585
586 amd_xgbe_phy_serdes_complete_ratechange(phydev);
587
Lendacky, Thomase3eec4e2014-09-03 12:14:16 -0500588 return 0;
589}
590
591static int amd_xgbe_phy_cur_mode(struct phy_device *phydev,
592 enum amd_xgbe_phy_mode *mode)
593{
594 int ret;
595
596 ret = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL2);
597 if (ret < 0)
598 return ret;
599
600 if ((ret & MDIO_PCS_CTRL2_TYPE) == MDIO_PCS_CTRL2_10GBR)
601 *mode = AMD_XGBE_MODE_KR;
602 else
603 *mode = AMD_XGBE_MODE_KX;
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500604
605 return 0;
606}
607
Lendacky, Thomase3eec4e2014-09-03 12:14:16 -0500608static bool amd_xgbe_phy_in_kr_mode(struct phy_device *phydev)
609{
610 enum amd_xgbe_phy_mode mode;
611
612 if (amd_xgbe_phy_cur_mode(phydev, &mode))
613 return false;
614
615 return (mode == AMD_XGBE_MODE_KR);
616}
617
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500618static int amd_xgbe_phy_switch_mode(struct phy_device *phydev)
619{
620 struct amd_xgbe_phy_priv *priv = phydev->priv;
621 int ret;
622
623 /* If we are in KR switch to KX, and vice-versa */
Lendacky, Thomase3eec4e2014-09-03 12:14:16 -0500624 if (amd_xgbe_phy_in_kr_mode(phydev)) {
Lendacky, Thomasf0476042014-07-29 08:57:25 -0500625 if (priv->speed_set == AMD_XGBE_PHY_SPEEDSET_1000_10000)
626 ret = amd_xgbe_phy_gmii_mode(phydev);
627 else
628 ret = amd_xgbe_phy_gmii_2500_mode(phydev);
629 } else {
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500630 ret = amd_xgbe_phy_xgmii_mode(phydev);
Lendacky, Thomasf0476042014-07-29 08:57:25 -0500631 }
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500632
633 return ret;
634}
635
Lendacky, Thomase3eec4e2014-09-03 12:14:16 -0500636static int amd_xgbe_phy_set_mode(struct phy_device *phydev,
637 enum amd_xgbe_phy_mode mode)
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500638{
Lendacky, Thomase3eec4e2014-09-03 12:14:16 -0500639 enum amd_xgbe_phy_mode cur_mode;
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500640 int ret;
641
Lendacky, Thomase3eec4e2014-09-03 12:14:16 -0500642 ret = amd_xgbe_phy_cur_mode(phydev, &cur_mode);
643 if (ret)
644 return ret;
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500645
Lendacky, Thomase3eec4e2014-09-03 12:14:16 -0500646 if (mode != cur_mode)
647 ret = amd_xgbe_phy_switch_mode(phydev);
648
649 return ret;
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500650}
651
Lendacky, Thomasc3152d42015-01-16 12:47:00 -0600652static int amd_xgbe_phy_set_an(struct phy_device *phydev, bool enable,
653 bool restart)
654{
655 int ret;
656
657 ret = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_CTRL1);
658 if (ret < 0)
659 return ret;
660
661 ret &= ~MDIO_AN_CTRL1_ENABLE;
662
663 if (enable)
664 ret |= MDIO_AN_CTRL1_ENABLE;
665
666 if (restart)
667 ret |= MDIO_AN_CTRL1_RESTART;
668
669 phy_write_mmd(phydev, MDIO_MMD_AN, MDIO_CTRL1, ret);
670
671 return 0;
672}
673
674static int amd_xgbe_phy_restart_an(struct phy_device *phydev)
675{
676 return amd_xgbe_phy_set_an(phydev, true, true);
677}
678
679static int amd_xgbe_phy_disable_an(struct phy_device *phydev)
680{
681 return amd_xgbe_phy_set_an(phydev, false, false);
682}
683
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500684static enum amd_xgbe_phy_an amd_xgbe_an_tx_training(struct phy_device *phydev,
685 enum amd_xgbe_phy_rx *state)
686{
Tom Lendackya42f5c12014-09-07 09:54:41 -0500687 struct amd_xgbe_phy_priv *priv = phydev->priv;
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500688 int ad_reg, lp_reg, ret;
689
690 *state = AMD_XGBE_RX_COMPLETE;
691
Lendacky, Thomase3eec4e2014-09-03 12:14:16 -0500692 /* If we're not in KR mode then we're done */
693 if (!amd_xgbe_phy_in_kr_mode(phydev))
Lendacky, Thomasc3152d42015-01-16 12:47:00 -0600694 return AMD_XGBE_AN_PAGE_RECEIVED;
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500695
696 /* Enable/Disable FEC */
697 ad_reg = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_ADVERTISE + 2);
698 if (ad_reg < 0)
699 return AMD_XGBE_AN_ERROR;
700
701 lp_reg = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_LPA + 2);
702 if (lp_reg < 0)
703 return AMD_XGBE_AN_ERROR;
704
705 ret = phy_read_mmd(phydev, MDIO_MMD_PMAPMD, MDIO_PMA_10GBR_FEC_CTRL);
706 if (ret < 0)
707 return AMD_XGBE_AN_ERROR;
708
Lendacky, Thomascf262522015-01-16 12:47:05 -0600709 ret &= ~XGBE_PHY_FEC_MASK;
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500710 if ((ad_reg & 0xc000) && (lp_reg & 0xc000))
Lendacky, Thomascf262522015-01-16 12:47:05 -0600711 ret |= priv->fec_ability;
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500712
713 phy_write_mmd(phydev, MDIO_MMD_PMAPMD, MDIO_PMA_10GBR_FEC_CTRL, ret);
714
715 /* Start KR training */
716 ret = phy_read_mmd(phydev, MDIO_MMD_PMAPMD, MDIO_PMA_10GBR_PMD_CTRL);
717 if (ret < 0)
718 return AMD_XGBE_AN_ERROR;
719
Lendacky, Thomascf262522015-01-16 12:47:05 -0600720 if (ret & XGBE_PHY_KR_TRAINING_ENABLE) {
721 XSIR0_IOWRITE_BITS(priv, SIR0_KR_RT_1, RESET, 1);
Lendacky, Thomas5c10e5c2014-07-29 08:57:43 -0500722
Lendacky, Thomascf262522015-01-16 12:47:05 -0600723 ret |= XGBE_PHY_KR_TRAINING_START;
724 phy_write_mmd(phydev, MDIO_MMD_PMAPMD, MDIO_PMA_10GBR_PMD_CTRL,
725 ret);
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500726
Lendacky, Thomascf262522015-01-16 12:47:05 -0600727 XSIR0_IOWRITE_BITS(priv, SIR0_KR_RT_1, RESET, 0);
728 }
Lendacky, Thomas5c10e5c2014-07-29 08:57:43 -0500729
Lendacky, Thomasc3152d42015-01-16 12:47:00 -0600730 return AMD_XGBE_AN_PAGE_RECEIVED;
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500731}
732
733static enum amd_xgbe_phy_an amd_xgbe_an_tx_xnp(struct phy_device *phydev,
734 enum amd_xgbe_phy_rx *state)
735{
736 u16 msg;
737
738 *state = AMD_XGBE_RX_XNP;
739
740 msg = XNP_MCF_NULL_MESSAGE;
741 msg |= XNP_MP_FORMATTED;
742
743 phy_write_mmd(phydev, MDIO_MMD_AN, MDIO_AN_XNP + 2, 0);
744 phy_write_mmd(phydev, MDIO_MMD_AN, MDIO_AN_XNP + 1, 0);
745 phy_write_mmd(phydev, MDIO_MMD_AN, MDIO_AN_XNP, msg);
746
Lendacky, Thomasc3152d42015-01-16 12:47:00 -0600747 return AMD_XGBE_AN_PAGE_RECEIVED;
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500748}
749
750static enum amd_xgbe_phy_an amd_xgbe_an_rx_bpa(struct phy_device *phydev,
751 enum amd_xgbe_phy_rx *state)
752{
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500753 unsigned int link_support;
754 int ret, ad_reg, lp_reg;
755
756 /* Read Base Ability register 2 first */
757 ret = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_LPA + 1);
758 if (ret < 0)
759 return AMD_XGBE_AN_ERROR;
760
761 /* Check for a supported mode, otherwise restart in a different one */
Lendacky, Thomase3eec4e2014-09-03 12:14:16 -0500762 link_support = amd_xgbe_phy_in_kr_mode(phydev) ? 0x80 : 0x20;
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500763 if (!(ret & link_support))
Lendacky, Thomase3eec4e2014-09-03 12:14:16 -0500764 return AMD_XGBE_AN_INCOMPAT_LINK;
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500765
766 /* Check Extended Next Page support */
767 ad_reg = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_ADVERTISE);
768 if (ad_reg < 0)
769 return AMD_XGBE_AN_ERROR;
770
771 lp_reg = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_LPA);
772 if (lp_reg < 0)
773 return AMD_XGBE_AN_ERROR;
774
775 return ((ad_reg & XNP_NP_EXCHANGE) || (lp_reg & XNP_NP_EXCHANGE)) ?
776 amd_xgbe_an_tx_xnp(phydev, state) :
777 amd_xgbe_an_tx_training(phydev, state);
778}
779
780static enum amd_xgbe_phy_an amd_xgbe_an_rx_xnp(struct phy_device *phydev,
781 enum amd_xgbe_phy_rx *state)
782{
783 int ad_reg, lp_reg;
784
785 /* Check Extended Next Page support */
Lendacky, Thomas0d40b612015-01-16 12:47:10 -0600786 ad_reg = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_XNP);
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500787 if (ad_reg < 0)
788 return AMD_XGBE_AN_ERROR;
789
Lendacky, Thomas0d40b612015-01-16 12:47:10 -0600790 lp_reg = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_LPX);
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500791 if (lp_reg < 0)
792 return AMD_XGBE_AN_ERROR;
793
794 return ((ad_reg & XNP_NP_EXCHANGE) || (lp_reg & XNP_NP_EXCHANGE)) ?
795 amd_xgbe_an_tx_xnp(phydev, state) :
796 amd_xgbe_an_tx_training(phydev, state);
797}
798
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500799static enum amd_xgbe_phy_an amd_xgbe_an_page_received(struct phy_device *phydev)
800{
801 struct amd_xgbe_phy_priv *priv = phydev->priv;
802 enum amd_xgbe_phy_rx *state;
803 int ret;
804
Lendacky, Thomase3eec4e2014-09-03 12:14:16 -0500805 state = amd_xgbe_phy_in_kr_mode(phydev) ? &priv->kr_state
806 : &priv->kx_state;
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500807
808 switch (*state) {
809 case AMD_XGBE_RX_BPA:
810 ret = amd_xgbe_an_rx_bpa(phydev, state);
811 break;
812
813 case AMD_XGBE_RX_XNP:
814 ret = amd_xgbe_an_rx_xnp(phydev, state);
815 break;
816
817 default:
818 ret = AMD_XGBE_AN_ERROR;
819 }
820
821 return ret;
822}
823
824static enum amd_xgbe_phy_an amd_xgbe_an_incompat_link(struct phy_device *phydev)
825{
Lendacky, Thomasc3152d42015-01-16 12:47:00 -0600826 struct amd_xgbe_phy_priv *priv = phydev->priv;
Lendacky, Thomase3eec4e2014-09-03 12:14:16 -0500827 int ret;
828
Lendacky, Thomasc3152d42015-01-16 12:47:00 -0600829 /* Be sure we aren't looping trying to negotiate */
830 if (amd_xgbe_phy_in_kr_mode(phydev)) {
831 priv->kr_state = AMD_XGBE_RX_ERROR;
832
833 if (!(phydev->supported & SUPPORTED_1000baseKX_Full) &&
834 !(phydev->supported & SUPPORTED_2500baseX_Full))
835 return AMD_XGBE_AN_NO_LINK;
836
837 if (priv->kx_state != AMD_XGBE_RX_BPA)
838 return AMD_XGBE_AN_NO_LINK;
839 } else {
840 priv->kx_state = AMD_XGBE_RX_ERROR;
841
842 if (!(phydev->supported & SUPPORTED_10000baseKR_Full))
843 return AMD_XGBE_AN_NO_LINK;
844
845 if (priv->kr_state != AMD_XGBE_RX_BPA)
846 return AMD_XGBE_AN_NO_LINK;
847 }
848
849 ret = amd_xgbe_phy_disable_an(phydev);
850 if (ret)
851 return AMD_XGBE_AN_ERROR;
852
Lendacky, Thomase3eec4e2014-09-03 12:14:16 -0500853 ret = amd_xgbe_phy_switch_mode(phydev);
854 if (ret)
855 return AMD_XGBE_AN_ERROR;
856
Lendacky, Thomasc3152d42015-01-16 12:47:00 -0600857 ret = amd_xgbe_phy_restart_an(phydev);
858 if (ret)
859 return AMD_XGBE_AN_ERROR;
860
861 return AMD_XGBE_AN_INCOMPAT_LINK;
862}
863
864static irqreturn_t amd_xgbe_an_isr(int irq, void *data)
865{
866 struct amd_xgbe_phy_priv *priv = (struct amd_xgbe_phy_priv *)data;
867
868 /* Interrupt reason must be read and cleared outside of IRQ context */
869 disable_irq_nosync(priv->an_irq);
870
871 queue_work(priv->an_workqueue, &priv->an_irq_work);
872
873 return IRQ_HANDLED;
874}
875
876static void amd_xgbe_an_irq_work(struct work_struct *work)
877{
878 struct amd_xgbe_phy_priv *priv = container_of(work,
879 struct amd_xgbe_phy_priv,
880 an_irq_work);
881
882 /* Avoid a race between enabling the IRQ and exiting the work by
883 * waiting for the work to finish and then queueing it
884 */
885 flush_work(&priv->an_work);
886 queue_work(priv->an_workqueue, &priv->an_work);
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500887}
888
889static void amd_xgbe_an_state_machine(struct work_struct *work)
890{
891 struct amd_xgbe_phy_priv *priv = container_of(work,
892 struct amd_xgbe_phy_priv,
893 an_work);
894 struct phy_device *phydev = priv->phydev;
Lendacky, Thomasc3152d42015-01-16 12:47:00 -0600895 enum amd_xgbe_phy_an cur_state = priv->an_state;
896 int int_reg, int_mask;
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500897
Lendacky, Thomasc3152d42015-01-16 12:47:00 -0600898 mutex_lock(&priv->an_mutex);
899
900 /* Read the interrupt */
901 int_reg = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_INT);
902 if (!int_reg)
903 goto out;
904
905next_int:
906 if (int_reg < 0) {
Lendacky, Thomase6f05622014-09-03 12:14:22 -0500907 priv->an_state = AMD_XGBE_AN_ERROR;
Lendacky, Thomasc3152d42015-01-16 12:47:00 -0600908 int_mask = XGBE_AN_INT_MASK;
909 } else if (int_reg & XGBE_AN_PG_RCV) {
910 priv->an_state = AMD_XGBE_AN_PAGE_RECEIVED;
911 int_mask = XGBE_AN_PG_RCV;
912 } else if (int_reg & XGBE_AN_INC_LINK) {
913 priv->an_state = AMD_XGBE_AN_INCOMPAT_LINK;
914 int_mask = XGBE_AN_INC_LINK;
915 } else if (int_reg & XGBE_AN_INT_CMPLT) {
916 priv->an_state = AMD_XGBE_AN_COMPLETE;
917 int_mask = XGBE_AN_INT_CMPLT;
918 } else {
919 priv->an_state = AMD_XGBE_AN_ERROR;
920 int_mask = 0;
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500921 }
922
Lendacky, Thomasc3152d42015-01-16 12:47:00 -0600923 /* Clear the interrupt to be processed */
924 int_reg &= ~int_mask;
925 phy_write_mmd(phydev, MDIO_MMD_AN, MDIO_AN_INT, int_reg);
926
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500927 priv->an_result = priv->an_state;
Lendacky, Thomasc3152d42015-01-16 12:47:00 -0600928
929again:
930 cur_state = priv->an_state;
931
932 switch (priv->an_state) {
933 case AMD_XGBE_AN_READY:
934 priv->an_supported = 0;
935 break;
936
937 case AMD_XGBE_AN_PAGE_RECEIVED:
938 priv->an_state = amd_xgbe_an_page_received(phydev);
939 priv->an_supported++;
940 break;
941
942 case AMD_XGBE_AN_INCOMPAT_LINK:
943 priv->an_supported = 0;
944 priv->parallel_detect = 0;
945 priv->an_state = amd_xgbe_an_incompat_link(phydev);
946 break;
947
948 case AMD_XGBE_AN_COMPLETE:
949 priv->parallel_detect = priv->an_supported ? 0 : 1;
950 netdev_dbg(phydev->attached_dev, "%s successful\n",
951 priv->an_supported ? "Auto negotiation"
952 : "Parallel detection");
953 break;
954
955 case AMD_XGBE_AN_NO_LINK:
956 break;
957
958 default:
959 priv->an_state = AMD_XGBE_AN_ERROR;
960 }
961
962 if (priv->an_state == AMD_XGBE_AN_NO_LINK) {
963 int_reg = 0;
964 phy_write_mmd(phydev, MDIO_MMD_AN, MDIO_AN_INT, 0);
965 } else if (priv->an_state == AMD_XGBE_AN_ERROR) {
966 netdev_err(phydev->attached_dev,
967 "error during auto-negotiation, state=%u\n",
968 cur_state);
969
970 int_reg = 0;
971 phy_write_mmd(phydev, MDIO_MMD_AN, MDIO_AN_INT, 0);
972 }
973
974 if (priv->an_state >= AMD_XGBE_AN_COMPLETE) {
975 priv->an_result = priv->an_state;
976 priv->an_state = AMD_XGBE_AN_READY;
977 priv->kr_state = AMD_XGBE_RX_BPA;
978 priv->kx_state = AMD_XGBE_RX_BPA;
979 }
980
981 if (cur_state != priv->an_state)
982 goto again;
983
984 if (int_reg)
985 goto next_int;
986
987out:
988 enable_irq(priv->an_irq);
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500989
990 mutex_unlock(&priv->an_mutex);
991}
992
Lendacky, Thomasc3152d42015-01-16 12:47:00 -0600993static int amd_xgbe_an_init(struct phy_device *phydev)
994{
995 int ret;
996
997 /* Set up Advertisement register 3 first */
998 ret = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_ADVERTISE + 2);
999 if (ret < 0)
1000 return ret;
1001
1002 if (phydev->supported & SUPPORTED_10000baseR_FEC)
1003 ret |= 0xc000;
1004 else
1005 ret &= ~0xc000;
1006
1007 phy_write_mmd(phydev, MDIO_MMD_AN, MDIO_AN_ADVERTISE + 2, ret);
1008
1009 /* Set up Advertisement register 2 next */
1010 ret = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_ADVERTISE + 1);
1011 if (ret < 0)
1012 return ret;
1013
1014 if (phydev->supported & SUPPORTED_10000baseKR_Full)
1015 ret |= 0x80;
1016 else
1017 ret &= ~0x80;
1018
1019 if ((phydev->supported & SUPPORTED_1000baseKX_Full) ||
1020 (phydev->supported & SUPPORTED_2500baseX_Full))
1021 ret |= 0x20;
1022 else
1023 ret &= ~0x20;
1024
1025 phy_write_mmd(phydev, MDIO_MMD_AN, MDIO_AN_ADVERTISE + 1, ret);
1026
1027 /* Set up Advertisement register 1 last */
1028 ret = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_ADVERTISE);
1029 if (ret < 0)
1030 return ret;
1031
1032 if (phydev->supported & SUPPORTED_Pause)
1033 ret |= 0x400;
1034 else
1035 ret &= ~0x400;
1036
1037 if (phydev->supported & SUPPORTED_Asym_Pause)
1038 ret |= 0x800;
1039 else
1040 ret &= ~0x800;
1041
1042 /* We don't intend to perform XNP */
1043 ret &= ~XNP_NP_EXCHANGE;
1044
1045 phy_write_mmd(phydev, MDIO_MMD_AN, MDIO_AN_ADVERTISE, ret);
1046
1047 return 0;
1048}
1049
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001050static int amd_xgbe_phy_soft_reset(struct phy_device *phydev)
1051{
1052 int count, ret;
1053
1054 ret = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1);
1055 if (ret < 0)
1056 return ret;
1057
1058 ret |= MDIO_CTRL1_RESET;
1059 phy_write_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1, ret);
1060
1061 count = 50;
1062 do {
1063 msleep(20);
1064 ret = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1);
1065 if (ret < 0)
1066 return ret;
1067 } while ((ret & MDIO_CTRL1_RESET) && --count);
1068
1069 if (ret & MDIO_CTRL1_RESET)
1070 return -ETIMEDOUT;
1071
Lendacky, Thomasc3152d42015-01-16 12:47:00 -06001072 /* Disable auto-negotiation for now */
1073 ret = amd_xgbe_phy_disable_an(phydev);
1074 if (ret < 0)
1075 return ret;
1076
1077 /* Clear auto-negotiation interrupts */
1078 phy_write_mmd(phydev, MDIO_MMD_AN, MDIO_AN_INT, 0);
1079
1080 return 0;
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001081}
1082
1083static int amd_xgbe_phy_config_init(struct phy_device *phydev)
1084{
Lendacky, Thomasf0476042014-07-29 08:57:25 -05001085 struct amd_xgbe_phy_priv *priv = phydev->priv;
Lendacky, Thomasc3152d42015-01-16 12:47:00 -06001086 struct net_device *netdev = phydev->attached_dev;
1087 int ret;
1088
1089 if (!priv->an_irq_allocated) {
1090 /* Allocate the auto-negotiation workqueue and interrupt */
1091 snprintf(priv->an_irq_name, sizeof(priv->an_irq_name) - 1,
1092 "%s-pcs", netdev_name(netdev));
1093
1094 priv->an_workqueue =
1095 create_singlethread_workqueue(priv->an_irq_name);
1096 if (!priv->an_workqueue) {
1097 netdev_err(netdev, "phy workqueue creation failed\n");
1098 return -ENOMEM;
1099 }
1100
1101 ret = devm_request_irq(priv->dev, priv->an_irq,
1102 amd_xgbe_an_isr, 0, priv->an_irq_name,
1103 priv);
1104 if (ret) {
1105 netdev_err(netdev, "phy irq request failed\n");
1106 destroy_workqueue(priv->an_workqueue);
1107 return ret;
1108 }
1109
1110 priv->an_irq_allocated = 1;
1111 }
Lendacky, Thomasf0476042014-07-29 08:57:25 -05001112
Lendacky, Thomascf262522015-01-16 12:47:05 -06001113 ret = phy_read_mmd(phydev, MDIO_MMD_PMAPMD, MDIO_PMA_10GBR_FEC_ABILITY);
1114 if (ret < 0)
1115 return ret;
1116 priv->fec_ability = ret & XGBE_PHY_FEC_MASK;
1117
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001118 /* Initialize supported features */
1119 phydev->supported = SUPPORTED_Autoneg;
1120 phydev->supported |= SUPPORTED_Pause | SUPPORTED_Asym_Pause;
1121 phydev->supported |= SUPPORTED_Backplane;
Lendacky, Thomascf262522015-01-16 12:47:05 -06001122 phydev->supported |= SUPPORTED_10000baseKR_Full;
Lendacky, Thomasf0476042014-07-29 08:57:25 -05001123 switch (priv->speed_set) {
1124 case AMD_XGBE_PHY_SPEEDSET_1000_10000:
1125 phydev->supported |= SUPPORTED_1000baseKX_Full;
1126 break;
1127 case AMD_XGBE_PHY_SPEEDSET_2500_10000:
1128 phydev->supported |= SUPPORTED_2500baseX_Full;
1129 break;
1130 }
Lendacky, Thomascf262522015-01-16 12:47:05 -06001131
1132 if (priv->fec_ability & XGBE_PHY_FEC_ENABLE)
1133 phydev->supported |= SUPPORTED_10000baseR_FEC;
1134
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001135 phydev->advertising = phydev->supported;
1136
Lendacky, Thomasc3152d42015-01-16 12:47:00 -06001137 /* Set initial mode - call the mode setting routines
1138 * directly to insure we are properly configured
1139 */
1140 if (phydev->supported & SUPPORTED_10000baseKR_Full)
1141 ret = amd_xgbe_phy_xgmii_mode(phydev);
1142 else if (phydev->supported & SUPPORTED_1000baseKX_Full)
1143 ret = amd_xgbe_phy_gmii_mode(phydev);
1144 else if (phydev->supported & SUPPORTED_2500baseX_Full)
1145 ret = amd_xgbe_phy_gmii_2500_mode(phydev);
1146 else
1147 ret = -EINVAL;
1148 if (ret < 0)
1149 return ret;
1150
1151 /* Set up advertisement registers based on current settings */
1152 ret = amd_xgbe_an_init(phydev);
1153 if (ret)
1154 return ret;
1155
1156 /* Enable auto-negotiation interrupts */
1157 phy_write_mmd(phydev, MDIO_MMD_AN, MDIO_AN_INTMASK, 0x07);
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001158
1159 return 0;
1160}
1161
1162static int amd_xgbe_phy_setup_forced(struct phy_device *phydev)
1163{
1164 int ret;
1165
1166 /* Disable auto-negotiation */
Lendacky, Thomasc3152d42015-01-16 12:47:00 -06001167 ret = amd_xgbe_phy_disable_an(phydev);
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001168 if (ret < 0)
1169 return ret;
1170
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001171 /* Validate/Set specified speed */
1172 switch (phydev->speed) {
1173 case SPEED_10000:
Lendacky, Thomasc3152d42015-01-16 12:47:00 -06001174 ret = amd_xgbe_phy_set_mode(phydev, AMD_XGBE_MODE_KR);
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001175 break;
1176
1177 case SPEED_2500:
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001178 case SPEED_1000:
Lendacky, Thomasc3152d42015-01-16 12:47:00 -06001179 ret = amd_xgbe_phy_set_mode(phydev, AMD_XGBE_MODE_KX);
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001180 break;
1181
1182 default:
1183 ret = -EINVAL;
1184 }
1185
1186 if (ret < 0)
1187 return ret;
1188
1189 /* Validate duplex mode */
1190 if (phydev->duplex != DUPLEX_FULL)
1191 return -EINVAL;
1192
1193 phydev->pause = 0;
1194 phydev->asym_pause = 0;
1195
1196 return 0;
1197}
1198
Lendacky, Thomasc3152d42015-01-16 12:47:00 -06001199static int __amd_xgbe_phy_config_aneg(struct phy_device *phydev)
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001200{
1201 struct amd_xgbe_phy_priv *priv = phydev->priv;
1202 u32 mmd_mask = phydev->c45_ids.devices_in_package;
Lendacky, Thomasc3152d42015-01-16 12:47:00 -06001203 int ret;
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001204
1205 if (phydev->autoneg != AUTONEG_ENABLE)
1206 return amd_xgbe_phy_setup_forced(phydev);
1207
1208 /* Make sure we have the AN MMD present */
1209 if (!(mmd_mask & MDIO_DEVS_AN))
1210 return -EINVAL;
1211
Lendacky, Thomasc3152d42015-01-16 12:47:00 -06001212 /* Disable auto-negotiation interrupt */
1213 disable_irq(priv->an_irq);
1214
1215 /* Start auto-negotiation in a supported mode */
1216 if (phydev->supported & SUPPORTED_10000baseKR_Full)
1217 ret = amd_xgbe_phy_set_mode(phydev, AMD_XGBE_MODE_KR);
1218 else if ((phydev->supported & SUPPORTED_1000baseKX_Full) ||
1219 (phydev->supported & SUPPORTED_2500baseX_Full))
1220 ret = amd_xgbe_phy_set_mode(phydev, AMD_XGBE_MODE_KX);
1221 else
1222 ret = -EINVAL;
1223 if (ret < 0) {
1224 enable_irq(priv->an_irq);
1225 return ret;
1226 }
1227
1228 /* Disable and stop any in progress auto-negotiation */
1229 ret = amd_xgbe_phy_disable_an(phydev);
1230 if (ret < 0)
1231 return ret;
1232
1233 /* Clear any auto-negotitation interrupts */
1234 phy_write_mmd(phydev, MDIO_MMD_AN, MDIO_AN_INT, 0);
1235
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001236 priv->an_result = AMD_XGBE_AN_READY;
Lendacky, Thomasc3152d42015-01-16 12:47:00 -06001237 priv->an_state = AMD_XGBE_AN_READY;
1238 priv->kr_state = AMD_XGBE_RX_BPA;
1239 priv->kx_state = AMD_XGBE_RX_BPA;
1240
1241 /* Re-enable auto-negotiation interrupt */
1242 enable_irq(priv->an_irq);
1243
1244 /* Set up advertisement registers based on current settings */
1245 ret = amd_xgbe_an_init(phydev);
1246 if (ret)
1247 return ret;
1248
1249 /* Enable and start auto-negotiation */
1250 return amd_xgbe_phy_restart_an(phydev);
1251}
1252
1253static int amd_xgbe_phy_config_aneg(struct phy_device *phydev)
1254{
1255 struct amd_xgbe_phy_priv *priv = phydev->priv;
1256 int ret;
1257
1258 mutex_lock(&priv->an_mutex);
1259
1260 ret = __amd_xgbe_phy_config_aneg(phydev);
1261
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001262 mutex_unlock(&priv->an_mutex);
1263
Lendacky, Thomasc3152d42015-01-16 12:47:00 -06001264 return ret;
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001265}
1266
1267static int amd_xgbe_phy_aneg_done(struct phy_device *phydev)
1268{
1269 struct amd_xgbe_phy_priv *priv = phydev->priv;
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001270
Lendacky, Thomasc3152d42015-01-16 12:47:00 -06001271 return (priv->an_result == AMD_XGBE_AN_COMPLETE);
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001272}
1273
1274static int amd_xgbe_phy_update_link(struct phy_device *phydev)
1275{
1276 struct amd_xgbe_phy_priv *priv = phydev->priv;
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001277 int ret;
1278
1279 /* If we're doing auto-negotiation don't report link down */
Lendacky, Thomasc3152d42015-01-16 12:47:00 -06001280 if (priv->an_state != AMD_XGBE_AN_READY) {
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001281 phydev->link = 1;
1282 return 0;
1283 }
1284
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001285 /* Link status is latched low, so read once to clear
1286 * and then read again to get current state
1287 */
1288 ret = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_STAT1);
1289 if (ret < 0)
1290 return ret;
1291
1292 ret = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_STAT1);
1293 if (ret < 0)
1294 return ret;
1295
1296 phydev->link = (ret & MDIO_STAT1_LSTATUS) ? 1 : 0;
1297
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001298 return 0;
1299}
1300
1301static int amd_xgbe_phy_read_status(struct phy_device *phydev)
1302{
Lendacky, Thomasf0476042014-07-29 08:57:25 -05001303 struct amd_xgbe_phy_priv *priv = phydev->priv;
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001304 u32 mmd_mask = phydev->c45_ids.devices_in_package;
Lendacky, Thomase3eec4e2014-09-03 12:14:16 -05001305 int ret, ad_ret, lp_ret;
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001306
1307 ret = amd_xgbe_phy_update_link(phydev);
1308 if (ret)
1309 return ret;
1310
Lendacky, Thomase6f05622014-09-03 12:14:22 -05001311 if ((phydev->autoneg == AUTONEG_ENABLE) &&
1312 !priv->parallel_detect) {
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001313 if (!(mmd_mask & MDIO_DEVS_AN))
1314 return -EINVAL;
1315
1316 if (!amd_xgbe_phy_aneg_done(phydev))
1317 return 0;
1318
1319 /* Compare Advertisement and Link Partner register 1 */
1320 ad_ret = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_ADVERTISE);
1321 if (ad_ret < 0)
1322 return ad_ret;
1323 lp_ret = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_LPA);
1324 if (lp_ret < 0)
1325 return lp_ret;
1326
1327 ad_ret &= lp_ret;
1328 phydev->pause = (ad_ret & 0x400) ? 1 : 0;
1329 phydev->asym_pause = (ad_ret & 0x800) ? 1 : 0;
1330
1331 /* Compare Advertisement and Link Partner register 2 */
1332 ad_ret = phy_read_mmd(phydev, MDIO_MMD_AN,
1333 MDIO_AN_ADVERTISE + 1);
1334 if (ad_ret < 0)
1335 return ad_ret;
1336 lp_ret = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_LPA + 1);
1337 if (lp_ret < 0)
1338 return lp_ret;
1339
1340 ad_ret &= lp_ret;
1341 if (ad_ret & 0x80) {
1342 phydev->speed = SPEED_10000;
Lendacky, Thomase3eec4e2014-09-03 12:14:16 -05001343 ret = amd_xgbe_phy_set_mode(phydev, AMD_XGBE_MODE_KR);
1344 if (ret)
1345 return ret;
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001346 } else {
Lendacky, Thomase3eec4e2014-09-03 12:14:16 -05001347 switch (priv->speed_set) {
1348 case AMD_XGBE_PHY_SPEEDSET_1000_10000:
Lendacky, Thomasf0476042014-07-29 08:57:25 -05001349 phydev->speed = SPEED_1000;
Lendacky, Thomase3eec4e2014-09-03 12:14:16 -05001350 break;
1351
1352 case AMD_XGBE_PHY_SPEEDSET_2500_10000:
Lendacky, Thomasf0476042014-07-29 08:57:25 -05001353 phydev->speed = SPEED_2500;
Lendacky, Thomase3eec4e2014-09-03 12:14:16 -05001354 break;
Lendacky, Thomasf0476042014-07-29 08:57:25 -05001355 }
1356
Lendacky, Thomase3eec4e2014-09-03 12:14:16 -05001357 ret = amd_xgbe_phy_set_mode(phydev, AMD_XGBE_MODE_KX);
1358 if (ret)
1359 return ret;
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001360 }
1361
1362 phydev->duplex = DUPLEX_FULL;
1363 } else {
Lendacky, Thomase3eec4e2014-09-03 12:14:16 -05001364 if (amd_xgbe_phy_in_kr_mode(phydev)) {
Lendacky, Thomasf0476042014-07-29 08:57:25 -05001365 phydev->speed = SPEED_10000;
1366 } else {
Lendacky, Thomase3eec4e2014-09-03 12:14:16 -05001367 switch (priv->speed_set) {
1368 case AMD_XGBE_PHY_SPEEDSET_1000_10000:
Lendacky, Thomasf0476042014-07-29 08:57:25 -05001369 phydev->speed = SPEED_1000;
Lendacky, Thomase3eec4e2014-09-03 12:14:16 -05001370 break;
1371
1372 case AMD_XGBE_PHY_SPEEDSET_2500_10000:
Lendacky, Thomasf0476042014-07-29 08:57:25 -05001373 phydev->speed = SPEED_2500;
Lendacky, Thomase3eec4e2014-09-03 12:14:16 -05001374 break;
1375 }
Lendacky, Thomasf0476042014-07-29 08:57:25 -05001376 }
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001377 phydev->duplex = DUPLEX_FULL;
1378 phydev->pause = 0;
1379 phydev->asym_pause = 0;
1380 }
1381
1382 return 0;
1383}
1384
1385static int amd_xgbe_phy_suspend(struct phy_device *phydev)
1386{
Lendacky, Thomas03e50fd2015-01-16 12:46:39 -06001387 struct amd_xgbe_phy_priv *priv = phydev->priv;
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001388 int ret;
1389
1390 mutex_lock(&phydev->lock);
1391
1392 ret = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1);
1393 if (ret < 0)
1394 goto unlock;
1395
Lendacky, Thomas03e50fd2015-01-16 12:46:39 -06001396 priv->lpm_ctrl = ret;
1397
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001398 ret |= MDIO_CTRL1_LPOWER;
1399 phy_write_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1, ret);
1400
1401 ret = 0;
1402
1403unlock:
1404 mutex_unlock(&phydev->lock);
1405
1406 return ret;
1407}
1408
1409static int amd_xgbe_phy_resume(struct phy_device *phydev)
1410{
Lendacky, Thomas03e50fd2015-01-16 12:46:39 -06001411 struct amd_xgbe_phy_priv *priv = phydev->priv;
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001412
1413 mutex_lock(&phydev->lock);
1414
Lendacky, Thomas03e50fd2015-01-16 12:46:39 -06001415 priv->lpm_ctrl &= ~MDIO_CTRL1_LPOWER;
1416 phy_write_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1, priv->lpm_ctrl);
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001417
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001418 mutex_unlock(&phydev->lock);
1419
Lendacky, Thomas03e50fd2015-01-16 12:46:39 -06001420 return 0;
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001421}
1422
1423static int amd_xgbe_phy_probe(struct phy_device *phydev)
1424{
1425 struct amd_xgbe_phy_priv *priv;
1426 struct platform_device *pdev;
1427 struct device *dev;
Lendacky, Thomasf0476042014-07-29 08:57:25 -05001428 const __be32 *property;
1429 unsigned int speed_set;
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001430 int ret;
1431
1432 if (!phydev->dev.of_node)
1433 return -EINVAL;
1434
1435 pdev = of_find_device_by_node(phydev->dev.of_node);
1436 if (!pdev)
1437 return -EINVAL;
1438 dev = &pdev->dev;
1439
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001440 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
1441 if (!priv) {
1442 ret = -ENOMEM;
Lendacky, Thomasc3152d42015-01-16 12:47:00 -06001443 goto err_pdev;
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001444 }
1445
1446 priv->pdev = pdev;
1447 priv->dev = dev;
1448 priv->phydev = phydev;
Lendacky, Thomasc3152d42015-01-16 12:47:00 -06001449 mutex_init(&priv->an_mutex);
1450 INIT_WORK(&priv->an_irq_work, amd_xgbe_an_irq_work);
1451 INIT_WORK(&priv->an_work, amd_xgbe_an_state_machine);
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001452
1453 /* Get the device mmio areas */
1454 priv->rxtx_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1455 priv->rxtx_regs = devm_ioremap_resource(dev, priv->rxtx_res);
1456 if (IS_ERR(priv->rxtx_regs)) {
1457 dev_err(dev, "rxtx ioremap failed\n");
1458 ret = PTR_ERR(priv->rxtx_regs);
1459 goto err_priv;
1460 }
1461
1462 priv->sir0_res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1463 priv->sir0_regs = devm_ioremap_resource(dev, priv->sir0_res);
1464 if (IS_ERR(priv->sir0_regs)) {
1465 dev_err(dev, "sir0 ioremap failed\n");
1466 ret = PTR_ERR(priv->sir0_regs);
1467 goto err_rxtx;
1468 }
1469
1470 priv->sir1_res = platform_get_resource(pdev, IORESOURCE_MEM, 2);
1471 priv->sir1_regs = devm_ioremap_resource(dev, priv->sir1_res);
1472 if (IS_ERR(priv->sir1_regs)) {
1473 dev_err(dev, "sir1 ioremap failed\n");
1474 ret = PTR_ERR(priv->sir1_regs);
1475 goto err_sir0;
1476 }
1477
Lendacky, Thomasc3152d42015-01-16 12:47:00 -06001478 /* Get the auto-negotiation interrupt */
1479 ret = platform_get_irq(pdev, 0);
1480 if (ret < 0) {
1481 dev_err(dev, "platform_get_irq failed\n");
1482 goto err_sir1;
1483 }
1484 priv->an_irq = ret;
1485
Lendacky, Thomasf0476042014-07-29 08:57:25 -05001486 /* Get the device speed set property */
1487 speed_set = 0;
1488 property = of_get_property(dev->of_node, XGBE_PHY_SPEEDSET_PROPERTY,
1489 NULL);
1490 if (property)
1491 speed_set = be32_to_cpu(*property);
1492
1493 switch (speed_set) {
1494 case 0:
1495 priv->speed_set = AMD_XGBE_PHY_SPEEDSET_1000_10000;
1496 break;
1497 case 1:
1498 priv->speed_set = AMD_XGBE_PHY_SPEEDSET_2500_10000;
1499 break;
1500 default:
1501 dev_err(dev, "invalid amd,speed-set property\n");
1502 ret = -EINVAL;
1503 goto err_sir1;
1504 }
1505
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001506 phydev->priv = priv;
1507
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001508 of_dev_put(pdev);
1509
1510 return 0;
1511
1512err_sir1:
1513 devm_iounmap(dev, priv->sir1_regs);
1514 devm_release_mem_region(dev, priv->sir1_res->start,
1515 resource_size(priv->sir1_res));
1516
1517err_sir0:
1518 devm_iounmap(dev, priv->sir0_regs);
1519 devm_release_mem_region(dev, priv->sir0_res->start,
1520 resource_size(priv->sir0_res));
1521
1522err_rxtx:
1523 devm_iounmap(dev, priv->rxtx_regs);
1524 devm_release_mem_region(dev, priv->rxtx_res->start,
1525 resource_size(priv->rxtx_res));
1526
1527err_priv:
1528 devm_kfree(dev, priv);
1529
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001530err_pdev:
1531 of_dev_put(pdev);
1532
1533 return ret;
1534}
1535
1536static void amd_xgbe_phy_remove(struct phy_device *phydev)
1537{
1538 struct amd_xgbe_phy_priv *priv = phydev->priv;
1539 struct device *dev = priv->dev;
1540
Lendacky, Thomasc3152d42015-01-16 12:47:00 -06001541 if (priv->an_irq_allocated) {
1542 devm_free_irq(dev, priv->an_irq, priv);
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001543
Lendacky, Thomasc3152d42015-01-16 12:47:00 -06001544 flush_workqueue(priv->an_workqueue);
1545 destroy_workqueue(priv->an_workqueue);
1546 }
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001547
1548 /* Release resources */
1549 devm_iounmap(dev, priv->sir1_regs);
1550 devm_release_mem_region(dev, priv->sir1_res->start,
1551 resource_size(priv->sir1_res));
1552
1553 devm_iounmap(dev, priv->sir0_regs);
1554 devm_release_mem_region(dev, priv->sir0_res->start,
1555 resource_size(priv->sir0_res));
1556
1557 devm_iounmap(dev, priv->rxtx_regs);
1558 devm_release_mem_region(dev, priv->rxtx_res->start,
1559 resource_size(priv->rxtx_res));
1560
1561 devm_kfree(dev, priv);
1562}
1563
1564static int amd_xgbe_match_phy_device(struct phy_device *phydev)
1565{
1566 return phydev->c45_ids.device_ids[MDIO_MMD_PCS] == XGBE_PHY_ID;
1567}
1568
1569static struct phy_driver amd_xgbe_phy_driver[] = {
1570 {
1571 .phy_id = XGBE_PHY_ID,
1572 .phy_id_mask = XGBE_PHY_MASK,
1573 .name = "AMD XGBE PHY",
1574 .features = 0,
1575 .probe = amd_xgbe_phy_probe,
1576 .remove = amd_xgbe_phy_remove,
1577 .soft_reset = amd_xgbe_phy_soft_reset,
1578 .config_init = amd_xgbe_phy_config_init,
1579 .suspend = amd_xgbe_phy_suspend,
1580 .resume = amd_xgbe_phy_resume,
1581 .config_aneg = amd_xgbe_phy_config_aneg,
1582 .aneg_done = amd_xgbe_phy_aneg_done,
1583 .read_status = amd_xgbe_phy_read_status,
1584 .match_phy_device = amd_xgbe_match_phy_device,
1585 .driver = {
1586 .owner = THIS_MODULE,
1587 },
1588 },
1589};
1590
Johan Hovold50fd7152014-11-11 19:45:59 +01001591module_phy_driver(amd_xgbe_phy_driver);
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001592
françois romieua25aafa2014-06-07 11:07:48 +02001593static struct mdio_device_id __maybe_unused amd_xgbe_phy_ids[] = {
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001594 { XGBE_PHY_ID, XGBE_PHY_MASK },
1595 { }
1596};
1597MODULE_DEVICE_TABLE(mdio, amd_xgbe_phy_ids);