blob: 903dc3dc9ea703dfae71dc4917b432e413735ee4 [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>
63#include <linux/netdevice.h>
64#include <linux/etherdevice.h>
65#include <linux/skbuff.h>
66#include <linux/mm.h>
67#include <linux/module.h>
68#include <linux/mii.h>
69#include <linux/ethtool.h>
70#include <linux/phy.h>
71#include <linux/mdio.h>
72#include <linux/io.h>
73#include <linux/of.h>
74#include <linux/of_platform.h>
75#include <linux/of_device.h>
76#include <linux/uaccess.h>
Lendacky, Thomas4d874b32014-06-05 09:15:12 -050077
Lendacky, Thomas4d874b32014-06-05 09:15:12 -050078MODULE_AUTHOR("Tom Lendacky <thomas.lendacky@amd.com>");
79MODULE_LICENSE("Dual BSD/GPL");
80MODULE_VERSION("1.0.0-a");
81MODULE_DESCRIPTION("AMD 10GbE (amd-xgbe) PHY driver");
82
83#define XGBE_PHY_ID 0x000162d0
84#define XGBE_PHY_MASK 0xfffffff0
85
Lendacky, Thomasf0476042014-07-29 08:57:25 -050086#define XGBE_PHY_SPEEDSET_PROPERTY "amd,speed-set"
87
Lendacky, Thomas4d874b32014-06-05 09:15:12 -050088#define XGBE_AN_INT_CMPLT 0x01
89#define XGBE_AN_INC_LINK 0x02
90#define XGBE_AN_PG_RCV 0x04
91
92#define XNP_MCF_NULL_MESSAGE 0x001
93#define XNP_ACK_PROCESSED (1 << 12)
94#define XNP_MP_FORMATTED (1 << 13)
95#define XNP_NP_EXCHANGE (1 << 15)
96
Lendacky, Thomas1fa1f2e2014-08-01 11:56:36 -050097#define XGBE_PHY_RATECHANGE_COUNT 500
Lendacky, Thomas169a6302014-07-29 08:57:37 -050098
Lendacky, Thomas4d874b32014-06-05 09:15:12 -050099#ifndef MDIO_PMA_10GBR_PMD_CTRL
100#define MDIO_PMA_10GBR_PMD_CTRL 0x0096
101#endif
Lendacky, Thomase6f05622014-09-03 12:14:22 -0500102
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500103#ifndef MDIO_PMA_10GBR_FEC_CTRL
104#define MDIO_PMA_10GBR_FEC_CTRL 0x00ab
105#endif
Lendacky, Thomase6f05622014-09-03 12:14:22 -0500106
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500107#ifndef MDIO_AN_XNP
108#define MDIO_AN_XNP 0x0016
109#endif
110
111#ifndef MDIO_AN_INTMASK
112#define MDIO_AN_INTMASK 0x8001
113#endif
Lendacky, Thomase6f05622014-09-03 12:14:22 -0500114
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500115#ifndef MDIO_AN_INT
116#define MDIO_AN_INT 0x8002
117#endif
118
Lendacky, Thomase6f05622014-09-03 12:14:22 -0500119#ifndef MDIO_AN_KR_CTRL
120#define MDIO_AN_KR_CTRL 0x8003
121#endif
122
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500123#ifndef MDIO_CTRL1_SPEED1G
124#define MDIO_CTRL1_SPEED1G (MDIO_CTRL1_SPEED10G & ~BMCR_SPEED100)
125#endif
126
Lendacky, Thomase6f05622014-09-03 12:14:22 -0500127#ifndef MDIO_KR_CTRL_PDETECT
128#define MDIO_KR_CTRL_PDETECT 0x01
129#endif
130
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500131/* SerDes integration register offsets */
Lendacky, Thomas5c10e5c2014-07-29 08:57:43 -0500132#define SIR0_KR_RT_1 0x002c
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500133#define SIR0_STATUS 0x0040
134#define SIR1_SPEED 0x0000
135
136/* SerDes integration register entry bit positions and sizes */
Lendacky, Thomas5c10e5c2014-07-29 08:57:43 -0500137#define SIR0_KR_RT_1_RESET_INDEX 11
138#define SIR0_KR_RT_1_RESET_WIDTH 1
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500139#define SIR0_STATUS_RX_READY_INDEX 0
140#define SIR0_STATUS_RX_READY_WIDTH 1
141#define SIR0_STATUS_TX_READY_INDEX 8
142#define SIR0_STATUS_TX_READY_WIDTH 1
143#define SIR1_SPEED_DATARATE_INDEX 4
144#define SIR1_SPEED_DATARATE_WIDTH 2
145#define SIR1_SPEED_PI_SPD_SEL_INDEX 12
146#define SIR1_SPEED_PI_SPD_SEL_WIDTH 4
147#define SIR1_SPEED_PLLSEL_INDEX 3
148#define SIR1_SPEED_PLLSEL_WIDTH 1
149#define SIR1_SPEED_RATECHANGE_INDEX 6
150#define SIR1_SPEED_RATECHANGE_WIDTH 1
151#define SIR1_SPEED_TXAMP_INDEX 8
152#define SIR1_SPEED_TXAMP_WIDTH 4
153#define SIR1_SPEED_WORDMODE_INDEX 0
154#define SIR1_SPEED_WORDMODE_WIDTH 3
155
156#define SPEED_10000_CDR 0x7
157#define SPEED_10000_PLL 0x1
158#define SPEED_10000_RATE 0x0
159#define SPEED_10000_TXAMP 0xa
160#define SPEED_10000_WORD 0x7
161
162#define SPEED_2500_CDR 0x2
163#define SPEED_2500_PLL 0x0
Lendacky, Thomasf0476042014-07-29 08:57:25 -0500164#define SPEED_2500_RATE 0x1
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500165#define SPEED_2500_TXAMP 0xf
166#define SPEED_2500_WORD 0x1
167
168#define SPEED_1000_CDR 0x2
169#define SPEED_1000_PLL 0x0
170#define SPEED_1000_RATE 0x3
171#define SPEED_1000_TXAMP 0xf
172#define SPEED_1000_WORD 0x1
173
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500174/* SerDes RxTx register offsets */
175#define RXTX_REG20 0x0050
176#define RXTX_REG114 0x01c8
177
178/* SerDes RxTx register entry bit positions and sizes */
179#define RXTX_REG20_BLWC_ENA_INDEX 2
180#define RXTX_REG20_BLWC_ENA_WIDTH 1
181#define RXTX_REG114_PQ_REG_INDEX 9
182#define RXTX_REG114_PQ_REG_WIDTH 7
183
184#define RXTX_10000_BLWC 0
185#define RXTX_10000_PQ 0x1e
186
187#define RXTX_2500_BLWC 1
188#define RXTX_2500_PQ 0xa
189
190#define RXTX_1000_BLWC 1
191#define RXTX_1000_PQ 0xa
192
193/* Bit setting and getting macros
194 * The get macro will extract the current bit field value from within
195 * the variable
196 *
197 * The set macro will clear the current bit field value within the
198 * variable and then set the bit field of the variable to the
199 * specified value
200 */
201#define GET_BITS(_var, _index, _width) \
202 (((_var) >> (_index)) & ((0x1 << (_width)) - 1))
203
204#define SET_BITS(_var, _index, _width, _val) \
205do { \
206 (_var) &= ~(((0x1 << (_width)) - 1) << (_index)); \
207 (_var) |= (((_val) & ((0x1 << (_width)) - 1)) << (_index)); \
208} while (0)
209
Lendacky, Thomas169a6302014-07-29 08:57:37 -0500210#define XSIR_GET_BITS(_var, _prefix, _field) \
211 GET_BITS((_var), \
212 _prefix##_##_field##_INDEX, \
213 _prefix##_##_field##_WIDTH)
214
215#define XSIR_SET_BITS(_var, _prefix, _field, _val) \
216 SET_BITS((_var), \
217 _prefix##_##_field##_INDEX, \
218 _prefix##_##_field##_WIDTH, (_val))
219
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500220/* Macros for reading or writing SerDes integration registers
221 * The ioread macros will get bit fields or full values using the
222 * register definitions formed using the input names
223 *
224 * The iowrite macros will set bit fields or full values using the
225 * register definitions formed using the input names
226 */
227#define XSIR0_IOREAD(_priv, _reg) \
228 ioread16((_priv)->sir0_regs + _reg)
229
230#define XSIR0_IOREAD_BITS(_priv, _reg, _field) \
231 GET_BITS(XSIR0_IOREAD((_priv), _reg), \
232 _reg##_##_field##_INDEX, \
233 _reg##_##_field##_WIDTH)
234
235#define XSIR0_IOWRITE(_priv, _reg, _val) \
236 iowrite16((_val), (_priv)->sir0_regs + _reg)
237
238#define XSIR0_IOWRITE_BITS(_priv, _reg, _field, _val) \
239do { \
240 u16 reg_val = XSIR0_IOREAD((_priv), _reg); \
241 SET_BITS(reg_val, \
242 _reg##_##_field##_INDEX, \
243 _reg##_##_field##_WIDTH, (_val)); \
244 XSIR0_IOWRITE((_priv), _reg, reg_val); \
245} while (0)
246
247#define XSIR1_IOREAD(_priv, _reg) \
248 ioread16((_priv)->sir1_regs + _reg)
249
250#define XSIR1_IOREAD_BITS(_priv, _reg, _field) \
251 GET_BITS(XSIR1_IOREAD((_priv), _reg), \
252 _reg##_##_field##_INDEX, \
253 _reg##_##_field##_WIDTH)
254
255#define XSIR1_IOWRITE(_priv, _reg, _val) \
256 iowrite16((_val), (_priv)->sir1_regs + _reg)
257
258#define XSIR1_IOWRITE_BITS(_priv, _reg, _field, _val) \
259do { \
260 u16 reg_val = XSIR1_IOREAD((_priv), _reg); \
261 SET_BITS(reg_val, \
262 _reg##_##_field##_INDEX, \
263 _reg##_##_field##_WIDTH, (_val)); \
264 XSIR1_IOWRITE((_priv), _reg, reg_val); \
265} while (0)
266
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500267/* Macros for reading or writing SerDes RxTx registers
268 * The ioread macros will get bit fields or full values using the
269 * register definitions formed using the input names
270 *
271 * The iowrite macros will set bit fields or full values using the
272 * register definitions formed using the input names
273 */
274#define XRXTX_IOREAD(_priv, _reg) \
275 ioread16((_priv)->rxtx_regs + _reg)
276
277#define XRXTX_IOREAD_BITS(_priv, _reg, _field) \
278 GET_BITS(XRXTX_IOREAD((_priv), _reg), \
279 _reg##_##_field##_INDEX, \
280 _reg##_##_field##_WIDTH)
281
282#define XRXTX_IOWRITE(_priv, _reg, _val) \
283 iowrite16((_val), (_priv)->rxtx_regs + _reg)
284
285#define XRXTX_IOWRITE_BITS(_priv, _reg, _field, _val) \
286do { \
287 u16 reg_val = XRXTX_IOREAD((_priv), _reg); \
288 SET_BITS(reg_val, \
289 _reg##_##_field##_INDEX, \
290 _reg##_##_field##_WIDTH, (_val)); \
291 XRXTX_IOWRITE((_priv), _reg, reg_val); \
292} while (0)
293
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500294enum amd_xgbe_phy_an {
295 AMD_XGBE_AN_READY = 0,
296 AMD_XGBE_AN_START,
297 AMD_XGBE_AN_EVENT,
298 AMD_XGBE_AN_PAGE_RECEIVED,
299 AMD_XGBE_AN_INCOMPAT_LINK,
300 AMD_XGBE_AN_COMPLETE,
301 AMD_XGBE_AN_NO_LINK,
302 AMD_XGBE_AN_EXIT,
303 AMD_XGBE_AN_ERROR,
304};
305
306enum amd_xgbe_phy_rx {
307 AMD_XGBE_RX_READY = 0,
308 AMD_XGBE_RX_BPA,
309 AMD_XGBE_RX_XNP,
310 AMD_XGBE_RX_COMPLETE,
311};
312
313enum amd_xgbe_phy_mode {
314 AMD_XGBE_MODE_KR,
315 AMD_XGBE_MODE_KX,
316};
317
Lendacky, Thomasf0476042014-07-29 08:57:25 -0500318enum amd_xgbe_phy_speedset {
319 AMD_XGBE_PHY_SPEEDSET_1000_10000,
320 AMD_XGBE_PHY_SPEEDSET_2500_10000,
321};
322
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500323struct amd_xgbe_phy_priv {
324 struct platform_device *pdev;
325 struct device *dev;
326
327 struct phy_device *phydev;
328
329 /* SerDes related mmio resources */
330 struct resource *rxtx_res;
331 struct resource *sir0_res;
332 struct resource *sir1_res;
333
334 /* SerDes related mmio registers */
335 void __iomem *rxtx_regs; /* SerDes Rx/Tx CSRs */
336 void __iomem *sir0_regs; /* SerDes integration registers (1/2) */
337 void __iomem *sir1_regs; /* SerDes integration registers (2/2) */
338
339 /* Maintain link status for re-starting auto-negotiation */
340 unsigned int link;
Lendacky, Thomasf0476042014-07-29 08:57:25 -0500341 unsigned int speed_set;
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500342
343 /* Auto-negotiation state machine support */
344 struct mutex an_mutex;
345 enum amd_xgbe_phy_an an_result;
346 enum amd_xgbe_phy_an an_state;
347 enum amd_xgbe_phy_rx kr_state;
348 enum amd_xgbe_phy_rx kx_state;
349 struct work_struct an_work;
350 struct workqueue_struct *an_workqueue;
Lendacky, Thomase6f05622014-09-03 12:14:22 -0500351 unsigned int parallel_detect;
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500352};
353
354static int amd_xgbe_an_enable_kr_training(struct phy_device *phydev)
355{
356 int ret;
357
358 ret = phy_read_mmd(phydev, MDIO_MMD_PMAPMD, MDIO_PMA_10GBR_PMD_CTRL);
359 if (ret < 0)
360 return ret;
361
362 ret |= 0x02;
363 phy_write_mmd(phydev, MDIO_MMD_PMAPMD, MDIO_PMA_10GBR_PMD_CTRL, ret);
364
365 return 0;
366}
367
368static int amd_xgbe_an_disable_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
376 ret &= ~0x02;
377 phy_write_mmd(phydev, MDIO_MMD_PMAPMD, MDIO_PMA_10GBR_PMD_CTRL, ret);
378
379 return 0;
380}
381
382static int amd_xgbe_phy_pcs_power_cycle(struct phy_device *phydev)
383{
384 int ret;
385
386 ret = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1);
387 if (ret < 0)
388 return ret;
389
390 ret |= MDIO_CTRL1_LPOWER;
391 phy_write_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1, ret);
392
393 usleep_range(75, 100);
394
395 ret &= ~MDIO_CTRL1_LPOWER;
396 phy_write_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1, ret);
397
398 return 0;
399}
400
401static void amd_xgbe_phy_serdes_start_ratechange(struct phy_device *phydev)
402{
403 struct amd_xgbe_phy_priv *priv = phydev->priv;
404
405 /* Assert Rx and Tx ratechange */
406 XSIR1_IOWRITE_BITS(priv, SIR1_SPEED, RATECHANGE, 1);
407}
408
409static void amd_xgbe_phy_serdes_complete_ratechange(struct phy_device *phydev)
410{
411 struct amd_xgbe_phy_priv *priv = phydev->priv;
Lendacky, Thomas169a6302014-07-29 08:57:37 -0500412 unsigned int wait;
413 u16 status;
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500414
415 /* Release Rx and Tx ratechange */
416 XSIR1_IOWRITE_BITS(priv, SIR1_SPEED, RATECHANGE, 0);
417
418 /* Wait for Rx and Tx ready */
Lendacky, Thomas169a6302014-07-29 08:57:37 -0500419 wait = XGBE_PHY_RATECHANGE_COUNT;
420 while (wait--) {
Lendacky, Thomas1fa1f2e2014-08-01 11:56:36 -0500421 usleep_range(50, 75);
Lendacky, Thomas169a6302014-07-29 08:57:37 -0500422
423 status = XSIR0_IOREAD(priv, SIR0_STATUS);
424 if (XSIR_GET_BITS(status, SIR0_STATUS, RX_READY) &&
425 XSIR_GET_BITS(status, SIR0_STATUS, TX_READY))
426 return;
427 }
428
Lendacky, Thomas1fa1f2e2014-08-01 11:56:36 -0500429 netdev_dbg(phydev->attached_dev, "SerDes rx/tx not ready (%#hx)\n",
Lendacky, Thomas169a6302014-07-29 08:57:37 -0500430 status);
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500431}
432
433static int amd_xgbe_phy_xgmii_mode(struct phy_device *phydev)
434{
435 struct amd_xgbe_phy_priv *priv = phydev->priv;
436 int ret;
437
438 /* Enable KR training */
439 ret = amd_xgbe_an_enable_kr_training(phydev);
440 if (ret < 0)
441 return ret;
442
443 /* Set PCS to KR/10G speed */
444 ret = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL2);
445 if (ret < 0)
446 return ret;
447
448 ret &= ~MDIO_PCS_CTRL2_TYPE;
449 ret |= MDIO_PCS_CTRL2_10GBR;
450 phy_write_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL2, ret);
451
452 ret = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1);
453 if (ret < 0)
454 return ret;
455
456 ret &= ~MDIO_CTRL1_SPEEDSEL;
457 ret |= MDIO_CTRL1_SPEED10G;
458 phy_write_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1, ret);
459
460 ret = amd_xgbe_phy_pcs_power_cycle(phydev);
461 if (ret < 0)
462 return ret;
463
464 /* Set SerDes to 10G speed */
465 amd_xgbe_phy_serdes_start_ratechange(phydev);
466
467 XSIR1_IOWRITE_BITS(priv, SIR1_SPEED, DATARATE, SPEED_10000_RATE);
468 XSIR1_IOWRITE_BITS(priv, SIR1_SPEED, WORDMODE, SPEED_10000_WORD);
469 XSIR1_IOWRITE_BITS(priv, SIR1_SPEED, TXAMP, SPEED_10000_TXAMP);
470 XSIR1_IOWRITE_BITS(priv, SIR1_SPEED, PLLSEL, SPEED_10000_PLL);
471 XSIR1_IOWRITE_BITS(priv, SIR1_SPEED, PI_SPD_SEL, SPEED_10000_CDR);
472
473 XRXTX_IOWRITE_BITS(priv, RXTX_REG20, BLWC_ENA, RXTX_10000_BLWC);
474 XRXTX_IOWRITE_BITS(priv, RXTX_REG114, PQ_REG, RXTX_10000_PQ);
475
476 amd_xgbe_phy_serdes_complete_ratechange(phydev);
477
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500478 return 0;
479}
480
481static int amd_xgbe_phy_gmii_2500_mode(struct phy_device *phydev)
482{
483 struct amd_xgbe_phy_priv *priv = phydev->priv;
484 int ret;
485
486 /* Disable KR training */
487 ret = amd_xgbe_an_disable_kr_training(phydev);
488 if (ret < 0)
489 return ret;
490
491 /* Set PCS to KX/1G speed */
492 ret = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL2);
493 if (ret < 0)
494 return ret;
495
496 ret &= ~MDIO_PCS_CTRL2_TYPE;
497 ret |= MDIO_PCS_CTRL2_10GBX;
498 phy_write_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL2, ret);
499
500 ret = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1);
501 if (ret < 0)
502 return ret;
503
504 ret &= ~MDIO_CTRL1_SPEEDSEL;
505 ret |= MDIO_CTRL1_SPEED1G;
506 phy_write_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1, ret);
507
508 ret = amd_xgbe_phy_pcs_power_cycle(phydev);
509 if (ret < 0)
510 return ret;
511
512 /* Set SerDes to 2.5G speed */
513 amd_xgbe_phy_serdes_start_ratechange(phydev);
514
515 XSIR1_IOWRITE_BITS(priv, SIR1_SPEED, DATARATE, SPEED_2500_RATE);
516 XSIR1_IOWRITE_BITS(priv, SIR1_SPEED, WORDMODE, SPEED_2500_WORD);
517 XSIR1_IOWRITE_BITS(priv, SIR1_SPEED, TXAMP, SPEED_2500_TXAMP);
518 XSIR1_IOWRITE_BITS(priv, SIR1_SPEED, PLLSEL, SPEED_2500_PLL);
519 XSIR1_IOWRITE_BITS(priv, SIR1_SPEED, PI_SPD_SEL, SPEED_2500_CDR);
520
521 XRXTX_IOWRITE_BITS(priv, RXTX_REG20, BLWC_ENA, RXTX_2500_BLWC);
522 XRXTX_IOWRITE_BITS(priv, RXTX_REG114, PQ_REG, RXTX_2500_PQ);
523
524 amd_xgbe_phy_serdes_complete_ratechange(phydev);
525
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500526 return 0;
527}
528
529static int amd_xgbe_phy_gmii_mode(struct phy_device *phydev)
530{
531 struct amd_xgbe_phy_priv *priv = phydev->priv;
532 int ret;
533
534 /* Disable KR training */
535 ret = amd_xgbe_an_disable_kr_training(phydev);
536 if (ret < 0)
537 return ret;
538
539 /* Set PCS to KX/1G speed */
540 ret = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL2);
541 if (ret < 0)
542 return ret;
543
544 ret &= ~MDIO_PCS_CTRL2_TYPE;
545 ret |= MDIO_PCS_CTRL2_10GBX;
546 phy_write_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL2, ret);
547
548 ret = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1);
549 if (ret < 0)
550 return ret;
551
552 ret &= ~MDIO_CTRL1_SPEEDSEL;
553 ret |= MDIO_CTRL1_SPEED1G;
554 phy_write_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1, ret);
555
556 ret = amd_xgbe_phy_pcs_power_cycle(phydev);
557 if (ret < 0)
558 return ret;
559
560 /* Set SerDes to 1G speed */
561 amd_xgbe_phy_serdes_start_ratechange(phydev);
562
563 XSIR1_IOWRITE_BITS(priv, SIR1_SPEED, DATARATE, SPEED_1000_RATE);
564 XSIR1_IOWRITE_BITS(priv, SIR1_SPEED, WORDMODE, SPEED_1000_WORD);
565 XSIR1_IOWRITE_BITS(priv, SIR1_SPEED, TXAMP, SPEED_1000_TXAMP);
566 XSIR1_IOWRITE_BITS(priv, SIR1_SPEED, PLLSEL, SPEED_1000_PLL);
567 XSIR1_IOWRITE_BITS(priv, SIR1_SPEED, PI_SPD_SEL, SPEED_1000_CDR);
568
569 XRXTX_IOWRITE_BITS(priv, RXTX_REG20, BLWC_ENA, RXTX_1000_BLWC);
570 XRXTX_IOWRITE_BITS(priv, RXTX_REG114, PQ_REG, RXTX_1000_PQ);
571
572 amd_xgbe_phy_serdes_complete_ratechange(phydev);
573
Lendacky, Thomase3eec4e2014-09-03 12:14:16 -0500574 return 0;
575}
576
577static int amd_xgbe_phy_cur_mode(struct phy_device *phydev,
578 enum amd_xgbe_phy_mode *mode)
579{
580 int ret;
581
582 ret = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL2);
583 if (ret < 0)
584 return ret;
585
586 if ((ret & MDIO_PCS_CTRL2_TYPE) == MDIO_PCS_CTRL2_10GBR)
587 *mode = AMD_XGBE_MODE_KR;
588 else
589 *mode = AMD_XGBE_MODE_KX;
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500590
591 return 0;
592}
593
Lendacky, Thomase3eec4e2014-09-03 12:14:16 -0500594static bool amd_xgbe_phy_in_kr_mode(struct phy_device *phydev)
595{
596 enum amd_xgbe_phy_mode mode;
597
598 if (amd_xgbe_phy_cur_mode(phydev, &mode))
599 return false;
600
601 return (mode == AMD_XGBE_MODE_KR);
602}
603
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500604static int amd_xgbe_phy_switch_mode(struct phy_device *phydev)
605{
606 struct amd_xgbe_phy_priv *priv = phydev->priv;
607 int ret;
608
609 /* If we are in KR switch to KX, and vice-versa */
Lendacky, Thomase3eec4e2014-09-03 12:14:16 -0500610 if (amd_xgbe_phy_in_kr_mode(phydev)) {
Lendacky, Thomasf0476042014-07-29 08:57:25 -0500611 if (priv->speed_set == AMD_XGBE_PHY_SPEEDSET_1000_10000)
612 ret = amd_xgbe_phy_gmii_mode(phydev);
613 else
614 ret = amd_xgbe_phy_gmii_2500_mode(phydev);
615 } else {
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500616 ret = amd_xgbe_phy_xgmii_mode(phydev);
Lendacky, Thomasf0476042014-07-29 08:57:25 -0500617 }
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500618
619 return ret;
620}
621
Lendacky, Thomase3eec4e2014-09-03 12:14:16 -0500622static int amd_xgbe_phy_set_mode(struct phy_device *phydev,
623 enum amd_xgbe_phy_mode mode)
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500624{
Lendacky, Thomase3eec4e2014-09-03 12:14:16 -0500625 enum amd_xgbe_phy_mode cur_mode;
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500626 int ret;
627
Lendacky, Thomase3eec4e2014-09-03 12:14:16 -0500628 ret = amd_xgbe_phy_cur_mode(phydev, &cur_mode);
629 if (ret)
630 return ret;
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500631
Lendacky, Thomase3eec4e2014-09-03 12:14:16 -0500632 if (mode != cur_mode)
633 ret = amd_xgbe_phy_switch_mode(phydev);
634
635 return ret;
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500636}
637
638static enum amd_xgbe_phy_an amd_xgbe_an_tx_training(struct phy_device *phydev,
639 enum amd_xgbe_phy_rx *state)
640{
Tom Lendackya42f5c12014-09-07 09:54:41 -0500641 struct amd_xgbe_phy_priv *priv = phydev->priv;
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500642 int ad_reg, lp_reg, ret;
643
644 *state = AMD_XGBE_RX_COMPLETE;
645
Lendacky, Thomase3eec4e2014-09-03 12:14:16 -0500646 /* If we're not in KR mode then we're done */
647 if (!amd_xgbe_phy_in_kr_mode(phydev))
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500648 return AMD_XGBE_AN_EVENT;
649
650 /* Enable/Disable FEC */
651 ad_reg = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_ADVERTISE + 2);
652 if (ad_reg < 0)
653 return AMD_XGBE_AN_ERROR;
654
655 lp_reg = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_LPA + 2);
656 if (lp_reg < 0)
657 return AMD_XGBE_AN_ERROR;
658
659 ret = phy_read_mmd(phydev, MDIO_MMD_PMAPMD, MDIO_PMA_10GBR_FEC_CTRL);
660 if (ret < 0)
661 return AMD_XGBE_AN_ERROR;
662
663 if ((ad_reg & 0xc000) && (lp_reg & 0xc000))
664 ret |= 0x01;
665 else
666 ret &= ~0x01;
667
668 phy_write_mmd(phydev, MDIO_MMD_PMAPMD, MDIO_PMA_10GBR_FEC_CTRL, ret);
669
670 /* Start KR training */
671 ret = phy_read_mmd(phydev, MDIO_MMD_PMAPMD, MDIO_PMA_10GBR_PMD_CTRL);
672 if (ret < 0)
673 return AMD_XGBE_AN_ERROR;
674
Lendacky, Thomas5c10e5c2014-07-29 08:57:43 -0500675 XSIR0_IOWRITE_BITS(priv, SIR0_KR_RT_1, RESET, 1);
676
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500677 ret |= 0x01;
678 phy_write_mmd(phydev, MDIO_MMD_PMAPMD, MDIO_PMA_10GBR_PMD_CTRL, ret);
679
Lendacky, Thomas5c10e5c2014-07-29 08:57:43 -0500680 XSIR0_IOWRITE_BITS(priv, SIR0_KR_RT_1, RESET, 0);
681
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500682 return AMD_XGBE_AN_EVENT;
683}
684
685static enum amd_xgbe_phy_an amd_xgbe_an_tx_xnp(struct phy_device *phydev,
686 enum amd_xgbe_phy_rx *state)
687{
688 u16 msg;
689
690 *state = AMD_XGBE_RX_XNP;
691
692 msg = XNP_MCF_NULL_MESSAGE;
693 msg |= XNP_MP_FORMATTED;
694
695 phy_write_mmd(phydev, MDIO_MMD_AN, MDIO_AN_XNP + 2, 0);
696 phy_write_mmd(phydev, MDIO_MMD_AN, MDIO_AN_XNP + 1, 0);
697 phy_write_mmd(phydev, MDIO_MMD_AN, MDIO_AN_XNP, msg);
698
699 return AMD_XGBE_AN_EVENT;
700}
701
702static enum amd_xgbe_phy_an amd_xgbe_an_rx_bpa(struct phy_device *phydev,
703 enum amd_xgbe_phy_rx *state)
704{
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500705 unsigned int link_support;
706 int ret, ad_reg, lp_reg;
707
708 /* Read Base Ability register 2 first */
709 ret = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_LPA + 1);
710 if (ret < 0)
711 return AMD_XGBE_AN_ERROR;
712
713 /* Check for a supported mode, otherwise restart in a different one */
Lendacky, Thomase3eec4e2014-09-03 12:14:16 -0500714 link_support = amd_xgbe_phy_in_kr_mode(phydev) ? 0x80 : 0x20;
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500715 if (!(ret & link_support))
Lendacky, Thomase3eec4e2014-09-03 12:14:16 -0500716 return AMD_XGBE_AN_INCOMPAT_LINK;
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500717
718 /* Check Extended Next Page support */
719 ad_reg = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_ADVERTISE);
720 if (ad_reg < 0)
721 return AMD_XGBE_AN_ERROR;
722
723 lp_reg = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_LPA);
724 if (lp_reg < 0)
725 return AMD_XGBE_AN_ERROR;
726
727 return ((ad_reg & XNP_NP_EXCHANGE) || (lp_reg & XNP_NP_EXCHANGE)) ?
728 amd_xgbe_an_tx_xnp(phydev, state) :
729 amd_xgbe_an_tx_training(phydev, state);
730}
731
732static enum amd_xgbe_phy_an amd_xgbe_an_rx_xnp(struct phy_device *phydev,
733 enum amd_xgbe_phy_rx *state)
734{
735 int ad_reg, lp_reg;
736
737 /* Check Extended Next Page support */
738 ad_reg = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_ADVERTISE);
739 if (ad_reg < 0)
740 return AMD_XGBE_AN_ERROR;
741
742 lp_reg = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_LPA);
743 if (lp_reg < 0)
744 return AMD_XGBE_AN_ERROR;
745
746 return ((ad_reg & XNP_NP_EXCHANGE) || (lp_reg & XNP_NP_EXCHANGE)) ?
747 amd_xgbe_an_tx_xnp(phydev, state) :
748 amd_xgbe_an_tx_training(phydev, state);
749}
750
751static enum amd_xgbe_phy_an amd_xgbe_an_start(struct phy_device *phydev)
752{
753 struct amd_xgbe_phy_priv *priv = phydev->priv;
754 int ret;
755
756 /* Be sure we aren't looping trying to negotiate */
Lendacky, Thomase3eec4e2014-09-03 12:14:16 -0500757 if (amd_xgbe_phy_in_kr_mode(phydev)) {
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500758 if (priv->kr_state != AMD_XGBE_RX_READY)
759 return AMD_XGBE_AN_NO_LINK;
760 priv->kr_state = AMD_XGBE_RX_BPA;
761 } else {
762 if (priv->kx_state != AMD_XGBE_RX_READY)
763 return AMD_XGBE_AN_NO_LINK;
764 priv->kx_state = AMD_XGBE_RX_BPA;
765 }
766
767 /* Set up Advertisement register 3 first */
768 ret = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_ADVERTISE + 2);
769 if (ret < 0)
770 return AMD_XGBE_AN_ERROR;
771
772 if (phydev->supported & SUPPORTED_10000baseR_FEC)
773 ret |= 0xc000;
774 else
775 ret &= ~0xc000;
776
777 phy_write_mmd(phydev, MDIO_MMD_AN, MDIO_AN_ADVERTISE + 2, ret);
778
779 /* Set up Advertisement register 2 next */
780 ret = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_ADVERTISE + 1);
781 if (ret < 0)
782 return AMD_XGBE_AN_ERROR;
783
784 if (phydev->supported & SUPPORTED_10000baseKR_Full)
785 ret |= 0x80;
786 else
787 ret &= ~0x80;
788
Lendacky, Thomasf0476042014-07-29 08:57:25 -0500789 if ((phydev->supported & SUPPORTED_1000baseKX_Full) ||
790 (phydev->supported & SUPPORTED_2500baseX_Full))
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500791 ret |= 0x20;
792 else
793 ret &= ~0x20;
794
795 phy_write_mmd(phydev, MDIO_MMD_AN, MDIO_AN_ADVERTISE + 1, ret);
796
797 /* Set up Advertisement register 1 last */
798 ret = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_ADVERTISE);
799 if (ret < 0)
800 return AMD_XGBE_AN_ERROR;
801
802 if (phydev->supported & SUPPORTED_Pause)
803 ret |= 0x400;
804 else
805 ret &= ~0x400;
806
807 if (phydev->supported & SUPPORTED_Asym_Pause)
808 ret |= 0x800;
809 else
810 ret &= ~0x800;
811
812 /* We don't intend to perform XNP */
813 ret &= ~XNP_NP_EXCHANGE;
814
815 phy_write_mmd(phydev, MDIO_MMD_AN, MDIO_AN_ADVERTISE, ret);
816
817 /* Enable and start auto-negotiation */
818 phy_write_mmd(phydev, MDIO_MMD_AN, MDIO_AN_INT, 0);
819
Lendacky, Thomase6f05622014-09-03 12:14:22 -0500820 ret = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_KR_CTRL);
821 if (ret < 0)
822 return AMD_XGBE_AN_ERROR;
823
824 ret |= MDIO_KR_CTRL_PDETECT;
825 phy_write_mmd(phydev, MDIO_MMD_AN, MDIO_AN_KR_CTRL, ret);
826
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500827 ret = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_CTRL1);
828 if (ret < 0)
829 return AMD_XGBE_AN_ERROR;
830
831 ret |= MDIO_AN_CTRL1_ENABLE;
832 ret |= MDIO_AN_CTRL1_RESTART;
833 phy_write_mmd(phydev, MDIO_MMD_AN, MDIO_CTRL1, ret);
834
835 return AMD_XGBE_AN_EVENT;
836}
837
838static enum amd_xgbe_phy_an amd_xgbe_an_event(struct phy_device *phydev)
839{
840 enum amd_xgbe_phy_an new_state;
841 int ret;
842
843 ret = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_INT);
844 if (ret < 0)
845 return AMD_XGBE_AN_ERROR;
846
847 new_state = AMD_XGBE_AN_EVENT;
848 if (ret & XGBE_AN_PG_RCV)
849 new_state = AMD_XGBE_AN_PAGE_RECEIVED;
850 else if (ret & XGBE_AN_INC_LINK)
851 new_state = AMD_XGBE_AN_INCOMPAT_LINK;
852 else if (ret & XGBE_AN_INT_CMPLT)
853 new_state = AMD_XGBE_AN_COMPLETE;
854
855 if (new_state != AMD_XGBE_AN_EVENT)
856 phy_write_mmd(phydev, MDIO_MMD_AN, MDIO_AN_INT, 0);
857
858 return new_state;
859}
860
861static enum amd_xgbe_phy_an amd_xgbe_an_page_received(struct phy_device *phydev)
862{
863 struct amd_xgbe_phy_priv *priv = phydev->priv;
864 enum amd_xgbe_phy_rx *state;
865 int ret;
866
Lendacky, Thomase3eec4e2014-09-03 12:14:16 -0500867 state = amd_xgbe_phy_in_kr_mode(phydev) ? &priv->kr_state
868 : &priv->kx_state;
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500869
870 switch (*state) {
871 case AMD_XGBE_RX_BPA:
872 ret = amd_xgbe_an_rx_bpa(phydev, state);
873 break;
874
875 case AMD_XGBE_RX_XNP:
876 ret = amd_xgbe_an_rx_xnp(phydev, state);
877 break;
878
879 default:
880 ret = AMD_XGBE_AN_ERROR;
881 }
882
883 return ret;
884}
885
886static enum amd_xgbe_phy_an amd_xgbe_an_incompat_link(struct phy_device *phydev)
887{
Lendacky, Thomase3eec4e2014-09-03 12:14:16 -0500888 int ret;
889
890 ret = amd_xgbe_phy_switch_mode(phydev);
891 if (ret)
892 return AMD_XGBE_AN_ERROR;
893
894 return AMD_XGBE_AN_START;
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500895}
896
897static void amd_xgbe_an_state_machine(struct work_struct *work)
898{
899 struct amd_xgbe_phy_priv *priv = container_of(work,
900 struct amd_xgbe_phy_priv,
901 an_work);
902 struct phy_device *phydev = priv->phydev;
903 enum amd_xgbe_phy_an cur_state;
904 int sleep;
Lendacky, Thomasb668a3a2014-07-29 08:57:49 -0500905 unsigned int an_supported = 0;
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500906
Lendacky, Thomase6f05622014-09-03 12:14:22 -0500907 /* Start in KX mode */
908 if (amd_xgbe_phy_set_mode(phydev, AMD_XGBE_MODE_KX))
909 priv->an_state = AMD_XGBE_AN_ERROR;
910
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500911 while (1) {
912 mutex_lock(&priv->an_mutex);
913
914 cur_state = priv->an_state;
915
916 switch (priv->an_state) {
917 case AMD_XGBE_AN_START:
Lendacky, Thomasb668a3a2014-07-29 08:57:49 -0500918 an_supported = 0;
Lendacky, Thomase6f05622014-09-03 12:14:22 -0500919 priv->parallel_detect = 0;
920 priv->an_state = amd_xgbe_an_start(phydev);
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500921 break;
922
923 case AMD_XGBE_AN_EVENT:
924 priv->an_state = amd_xgbe_an_event(phydev);
925 break;
926
927 case AMD_XGBE_AN_PAGE_RECEIVED:
928 priv->an_state = amd_xgbe_an_page_received(phydev);
Lendacky, Thomasb668a3a2014-07-29 08:57:49 -0500929 an_supported++;
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500930 break;
931
932 case AMD_XGBE_AN_INCOMPAT_LINK:
933 priv->an_state = amd_xgbe_an_incompat_link(phydev);
934 break;
935
936 case AMD_XGBE_AN_COMPLETE:
Lendacky, Thomase6f05622014-09-03 12:14:22 -0500937 priv->parallel_detect = an_supported ? 0 : 1;
Lendacky, Thomasb668a3a2014-07-29 08:57:49 -0500938 netdev_info(phydev->attached_dev, "%s successful\n",
939 an_supported ? "Auto negotiation"
940 : "Parallel detection");
941 /* fall through */
942
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500943 case AMD_XGBE_AN_NO_LINK:
944 case AMD_XGBE_AN_EXIT:
945 goto exit_unlock;
946
947 default:
948 priv->an_state = AMD_XGBE_AN_ERROR;
949 }
950
951 if (priv->an_state == AMD_XGBE_AN_ERROR) {
952 netdev_err(phydev->attached_dev,
953 "error during auto-negotiation, state=%u\n",
954 cur_state);
955 goto exit_unlock;
956 }
957
958 sleep = (priv->an_state == AMD_XGBE_AN_EVENT) ? 1 : 0;
959
960 mutex_unlock(&priv->an_mutex);
961
962 if (sleep)
963 usleep_range(20, 50);
964 }
965
966exit_unlock:
967 priv->an_result = priv->an_state;
968 priv->an_state = AMD_XGBE_AN_READY;
969
970 mutex_unlock(&priv->an_mutex);
971}
972
973static int amd_xgbe_phy_soft_reset(struct phy_device *phydev)
974{
975 int count, ret;
976
977 ret = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1);
978 if (ret < 0)
979 return ret;
980
981 ret |= MDIO_CTRL1_RESET;
982 phy_write_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1, ret);
983
984 count = 50;
985 do {
986 msleep(20);
987 ret = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1);
988 if (ret < 0)
989 return ret;
990 } while ((ret & MDIO_CTRL1_RESET) && --count);
991
992 if (ret & MDIO_CTRL1_RESET)
993 return -ETIMEDOUT;
994
Lendacky, Thomas0c95a1f2014-11-04 16:07:35 -0600995 /* Make sure the XPCS and SerDes are in compatible states */
996 return amd_xgbe_phy_xgmii_mode(phydev);
Lendacky, Thomas4d874b32014-06-05 09:15:12 -0500997}
998
999static int amd_xgbe_phy_config_init(struct phy_device *phydev)
1000{
Lendacky, Thomasf0476042014-07-29 08:57:25 -05001001 struct amd_xgbe_phy_priv *priv = phydev->priv;
1002
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001003 /* Initialize supported features */
1004 phydev->supported = SUPPORTED_Autoneg;
1005 phydev->supported |= SUPPORTED_Pause | SUPPORTED_Asym_Pause;
1006 phydev->supported |= SUPPORTED_Backplane;
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001007 phydev->supported |= SUPPORTED_10000baseKR_Full |
1008 SUPPORTED_10000baseR_FEC;
Lendacky, Thomasf0476042014-07-29 08:57:25 -05001009 switch (priv->speed_set) {
1010 case AMD_XGBE_PHY_SPEEDSET_1000_10000:
1011 phydev->supported |= SUPPORTED_1000baseKX_Full;
1012 break;
1013 case AMD_XGBE_PHY_SPEEDSET_2500_10000:
1014 phydev->supported |= SUPPORTED_2500baseX_Full;
1015 break;
1016 }
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001017 phydev->advertising = phydev->supported;
1018
1019 /* Turn off and clear interrupts */
1020 phy_write_mmd(phydev, MDIO_MMD_AN, MDIO_AN_INTMASK, 0);
1021 phy_write_mmd(phydev, MDIO_MMD_AN, MDIO_AN_INT, 0);
1022
1023 return 0;
1024}
1025
1026static int amd_xgbe_phy_setup_forced(struct phy_device *phydev)
1027{
1028 int ret;
1029
1030 /* Disable auto-negotiation */
1031 ret = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_CTRL1);
1032 if (ret < 0)
1033 return ret;
1034
1035 ret &= ~MDIO_AN_CTRL1_ENABLE;
1036 phy_write_mmd(phydev, MDIO_MMD_AN, MDIO_CTRL1, ret);
1037
1038 /* Validate/Set specified speed */
1039 switch (phydev->speed) {
1040 case SPEED_10000:
1041 ret = amd_xgbe_phy_xgmii_mode(phydev);
1042 break;
1043
1044 case SPEED_2500:
1045 ret = amd_xgbe_phy_gmii_2500_mode(phydev);
1046 break;
1047
1048 case SPEED_1000:
1049 ret = amd_xgbe_phy_gmii_mode(phydev);
1050 break;
1051
1052 default:
1053 ret = -EINVAL;
1054 }
1055
1056 if (ret < 0)
1057 return ret;
1058
1059 /* Validate duplex mode */
1060 if (phydev->duplex != DUPLEX_FULL)
1061 return -EINVAL;
1062
1063 phydev->pause = 0;
1064 phydev->asym_pause = 0;
1065
1066 return 0;
1067}
1068
1069static int amd_xgbe_phy_config_aneg(struct phy_device *phydev)
1070{
1071 struct amd_xgbe_phy_priv *priv = phydev->priv;
1072 u32 mmd_mask = phydev->c45_ids.devices_in_package;
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001073
1074 if (phydev->autoneg != AUTONEG_ENABLE)
1075 return amd_xgbe_phy_setup_forced(phydev);
1076
1077 /* Make sure we have the AN MMD present */
1078 if (!(mmd_mask & MDIO_DEVS_AN))
1079 return -EINVAL;
1080
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001081 /* Start/Restart the auto-negotiation state machine */
1082 mutex_lock(&priv->an_mutex);
1083 priv->an_result = AMD_XGBE_AN_READY;
1084 priv->an_state = AMD_XGBE_AN_START;
1085 priv->kr_state = AMD_XGBE_RX_READY;
1086 priv->kx_state = AMD_XGBE_RX_READY;
1087 mutex_unlock(&priv->an_mutex);
1088
1089 queue_work(priv->an_workqueue, &priv->an_work);
1090
1091 return 0;
1092}
1093
1094static int amd_xgbe_phy_aneg_done(struct phy_device *phydev)
1095{
1096 struct amd_xgbe_phy_priv *priv = phydev->priv;
1097 enum amd_xgbe_phy_an state;
1098
1099 mutex_lock(&priv->an_mutex);
1100 state = priv->an_result;
1101 mutex_unlock(&priv->an_mutex);
1102
1103 return (state == AMD_XGBE_AN_COMPLETE);
1104}
1105
1106static int amd_xgbe_phy_update_link(struct phy_device *phydev)
1107{
1108 struct amd_xgbe_phy_priv *priv = phydev->priv;
1109 enum amd_xgbe_phy_an state;
1110 unsigned int check_again, autoneg;
1111 int ret;
1112
1113 /* If we're doing auto-negotiation don't report link down */
1114 mutex_lock(&priv->an_mutex);
1115 state = priv->an_state;
1116 mutex_unlock(&priv->an_mutex);
1117
1118 if (state != AMD_XGBE_AN_READY) {
1119 phydev->link = 1;
1120 return 0;
1121 }
1122
1123 /* Since the device can be in the wrong mode when a link is
1124 * (re-)established (cable connected after the interface is
1125 * up, etc.), the link status may report no link. If there
1126 * is no link, try switching modes and checking the status
Lendacky, Thomasf0476042014-07-29 08:57:25 -05001127 * again if auto negotiation is enabled.
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001128 */
Lendacky, Thomasf0476042014-07-29 08:57:25 -05001129 check_again = (phydev->autoneg == AUTONEG_ENABLE) ? 1 : 0;
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001130again:
1131 /* Link status is latched low, so read once to clear
1132 * and then read again to get current state
1133 */
1134 ret = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_STAT1);
1135 if (ret < 0)
1136 return ret;
1137
1138 ret = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_STAT1);
1139 if (ret < 0)
1140 return ret;
1141
1142 phydev->link = (ret & MDIO_STAT1_LSTATUS) ? 1 : 0;
1143
1144 if (!phydev->link) {
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001145 if (check_again) {
Lendacky, Thomasf0476042014-07-29 08:57:25 -05001146 ret = amd_xgbe_phy_switch_mode(phydev);
1147 if (ret < 0)
1148 return ret;
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001149 check_again = 0;
1150 goto again;
1151 }
1152 }
1153
1154 autoneg = (phydev->link && !priv->link) ? 1 : 0;
1155 priv->link = phydev->link;
1156 if (autoneg) {
1157 /* Link is (back) up, re-start auto-negotiation */
1158 ret = amd_xgbe_phy_config_aneg(phydev);
1159 if (ret < 0)
1160 return ret;
1161 }
1162
1163 return 0;
1164}
1165
1166static int amd_xgbe_phy_read_status(struct phy_device *phydev)
1167{
Lendacky, Thomasf0476042014-07-29 08:57:25 -05001168 struct amd_xgbe_phy_priv *priv = phydev->priv;
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001169 u32 mmd_mask = phydev->c45_ids.devices_in_package;
Lendacky, Thomase3eec4e2014-09-03 12:14:16 -05001170 int ret, ad_ret, lp_ret;
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001171
1172 ret = amd_xgbe_phy_update_link(phydev);
1173 if (ret)
1174 return ret;
1175
Lendacky, Thomase6f05622014-09-03 12:14:22 -05001176 if ((phydev->autoneg == AUTONEG_ENABLE) &&
1177 !priv->parallel_detect) {
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001178 if (!(mmd_mask & MDIO_DEVS_AN))
1179 return -EINVAL;
1180
1181 if (!amd_xgbe_phy_aneg_done(phydev))
1182 return 0;
1183
1184 /* Compare Advertisement and Link Partner register 1 */
1185 ad_ret = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_ADVERTISE);
1186 if (ad_ret < 0)
1187 return ad_ret;
1188 lp_ret = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_LPA);
1189 if (lp_ret < 0)
1190 return lp_ret;
1191
1192 ad_ret &= lp_ret;
1193 phydev->pause = (ad_ret & 0x400) ? 1 : 0;
1194 phydev->asym_pause = (ad_ret & 0x800) ? 1 : 0;
1195
1196 /* Compare Advertisement and Link Partner register 2 */
1197 ad_ret = phy_read_mmd(phydev, MDIO_MMD_AN,
1198 MDIO_AN_ADVERTISE + 1);
1199 if (ad_ret < 0)
1200 return ad_ret;
1201 lp_ret = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_LPA + 1);
1202 if (lp_ret < 0)
1203 return lp_ret;
1204
1205 ad_ret &= lp_ret;
1206 if (ad_ret & 0x80) {
1207 phydev->speed = SPEED_10000;
Lendacky, Thomase3eec4e2014-09-03 12:14:16 -05001208 ret = amd_xgbe_phy_set_mode(phydev, AMD_XGBE_MODE_KR);
1209 if (ret)
1210 return ret;
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001211 } else {
Lendacky, Thomase3eec4e2014-09-03 12:14:16 -05001212 switch (priv->speed_set) {
1213 case AMD_XGBE_PHY_SPEEDSET_1000_10000:
Lendacky, Thomasf0476042014-07-29 08:57:25 -05001214 phydev->speed = SPEED_1000;
Lendacky, Thomase3eec4e2014-09-03 12:14:16 -05001215 break;
1216
1217 case AMD_XGBE_PHY_SPEEDSET_2500_10000:
Lendacky, Thomasf0476042014-07-29 08:57:25 -05001218 phydev->speed = SPEED_2500;
Lendacky, Thomase3eec4e2014-09-03 12:14:16 -05001219 break;
Lendacky, Thomasf0476042014-07-29 08:57:25 -05001220 }
1221
Lendacky, Thomase3eec4e2014-09-03 12:14:16 -05001222 ret = amd_xgbe_phy_set_mode(phydev, AMD_XGBE_MODE_KX);
1223 if (ret)
1224 return ret;
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001225 }
1226
1227 phydev->duplex = DUPLEX_FULL;
1228 } else {
Lendacky, Thomase3eec4e2014-09-03 12:14:16 -05001229 if (amd_xgbe_phy_in_kr_mode(phydev)) {
Lendacky, Thomasf0476042014-07-29 08:57:25 -05001230 phydev->speed = SPEED_10000;
1231 } else {
Lendacky, Thomase3eec4e2014-09-03 12:14:16 -05001232 switch (priv->speed_set) {
1233 case AMD_XGBE_PHY_SPEEDSET_1000_10000:
Lendacky, Thomasf0476042014-07-29 08:57:25 -05001234 phydev->speed = SPEED_1000;
Lendacky, Thomase3eec4e2014-09-03 12:14:16 -05001235 break;
1236
1237 case AMD_XGBE_PHY_SPEEDSET_2500_10000:
Lendacky, Thomasf0476042014-07-29 08:57:25 -05001238 phydev->speed = SPEED_2500;
Lendacky, Thomase3eec4e2014-09-03 12:14:16 -05001239 break;
1240 }
Lendacky, Thomasf0476042014-07-29 08:57:25 -05001241 }
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001242 phydev->duplex = DUPLEX_FULL;
1243 phydev->pause = 0;
1244 phydev->asym_pause = 0;
1245 }
1246
1247 return 0;
1248}
1249
1250static int amd_xgbe_phy_suspend(struct phy_device *phydev)
1251{
1252 int ret;
1253
1254 mutex_lock(&phydev->lock);
1255
1256 ret = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1);
1257 if (ret < 0)
1258 goto unlock;
1259
1260 ret |= MDIO_CTRL1_LPOWER;
1261 phy_write_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1, ret);
1262
1263 ret = 0;
1264
1265unlock:
1266 mutex_unlock(&phydev->lock);
1267
1268 return ret;
1269}
1270
1271static int amd_xgbe_phy_resume(struct phy_device *phydev)
1272{
1273 int ret;
1274
1275 mutex_lock(&phydev->lock);
1276
1277 ret = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1);
1278 if (ret < 0)
1279 goto unlock;
1280
1281 ret &= ~MDIO_CTRL1_LPOWER;
1282 phy_write_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1, ret);
1283
1284 ret = 0;
1285
1286unlock:
1287 mutex_unlock(&phydev->lock);
1288
1289 return ret;
1290}
1291
1292static int amd_xgbe_phy_probe(struct phy_device *phydev)
1293{
1294 struct amd_xgbe_phy_priv *priv;
1295 struct platform_device *pdev;
1296 struct device *dev;
1297 char *wq_name;
Lendacky, Thomasf0476042014-07-29 08:57:25 -05001298 const __be32 *property;
1299 unsigned int speed_set;
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001300 int ret;
1301
1302 if (!phydev->dev.of_node)
1303 return -EINVAL;
1304
1305 pdev = of_find_device_by_node(phydev->dev.of_node);
1306 if (!pdev)
1307 return -EINVAL;
1308 dev = &pdev->dev;
1309
1310 wq_name = kasprintf(GFP_KERNEL, "%s-amd-xgbe-phy", phydev->bus->name);
1311 if (!wq_name) {
1312 ret = -ENOMEM;
1313 goto err_pdev;
1314 }
1315
1316 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
1317 if (!priv) {
1318 ret = -ENOMEM;
1319 goto err_name;
1320 }
1321
1322 priv->pdev = pdev;
1323 priv->dev = dev;
1324 priv->phydev = phydev;
1325
1326 /* Get the device mmio areas */
1327 priv->rxtx_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1328 priv->rxtx_regs = devm_ioremap_resource(dev, priv->rxtx_res);
1329 if (IS_ERR(priv->rxtx_regs)) {
1330 dev_err(dev, "rxtx ioremap failed\n");
1331 ret = PTR_ERR(priv->rxtx_regs);
1332 goto err_priv;
1333 }
1334
1335 priv->sir0_res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1336 priv->sir0_regs = devm_ioremap_resource(dev, priv->sir0_res);
1337 if (IS_ERR(priv->sir0_regs)) {
1338 dev_err(dev, "sir0 ioremap failed\n");
1339 ret = PTR_ERR(priv->sir0_regs);
1340 goto err_rxtx;
1341 }
1342
1343 priv->sir1_res = platform_get_resource(pdev, IORESOURCE_MEM, 2);
1344 priv->sir1_regs = devm_ioremap_resource(dev, priv->sir1_res);
1345 if (IS_ERR(priv->sir1_regs)) {
1346 dev_err(dev, "sir1 ioremap failed\n");
1347 ret = PTR_ERR(priv->sir1_regs);
1348 goto err_sir0;
1349 }
1350
Lendacky, Thomasf0476042014-07-29 08:57:25 -05001351 /* Get the device speed set property */
1352 speed_set = 0;
1353 property = of_get_property(dev->of_node, XGBE_PHY_SPEEDSET_PROPERTY,
1354 NULL);
1355 if (property)
1356 speed_set = be32_to_cpu(*property);
1357
1358 switch (speed_set) {
1359 case 0:
1360 priv->speed_set = AMD_XGBE_PHY_SPEEDSET_1000_10000;
1361 break;
1362 case 1:
1363 priv->speed_set = AMD_XGBE_PHY_SPEEDSET_2500_10000;
1364 break;
1365 default:
1366 dev_err(dev, "invalid amd,speed-set property\n");
1367 ret = -EINVAL;
1368 goto err_sir1;
1369 }
1370
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001371 priv->link = 1;
1372
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001373 mutex_init(&priv->an_mutex);
1374 INIT_WORK(&priv->an_work, amd_xgbe_an_state_machine);
1375 priv->an_workqueue = create_singlethread_workqueue(wq_name);
1376 if (!priv->an_workqueue) {
1377 ret = -ENOMEM;
1378 goto err_sir1;
1379 }
1380
1381 phydev->priv = priv;
1382
1383 kfree(wq_name);
1384 of_dev_put(pdev);
1385
1386 return 0;
1387
1388err_sir1:
1389 devm_iounmap(dev, priv->sir1_regs);
1390 devm_release_mem_region(dev, priv->sir1_res->start,
1391 resource_size(priv->sir1_res));
1392
1393err_sir0:
1394 devm_iounmap(dev, priv->sir0_regs);
1395 devm_release_mem_region(dev, priv->sir0_res->start,
1396 resource_size(priv->sir0_res));
1397
1398err_rxtx:
1399 devm_iounmap(dev, priv->rxtx_regs);
1400 devm_release_mem_region(dev, priv->rxtx_res->start,
1401 resource_size(priv->rxtx_res));
1402
1403err_priv:
1404 devm_kfree(dev, priv);
1405
1406err_name:
1407 kfree(wq_name);
1408
1409err_pdev:
1410 of_dev_put(pdev);
1411
1412 return ret;
1413}
1414
1415static void amd_xgbe_phy_remove(struct phy_device *phydev)
1416{
1417 struct amd_xgbe_phy_priv *priv = phydev->priv;
1418 struct device *dev = priv->dev;
1419
1420 /* Stop any in process auto-negotiation */
1421 mutex_lock(&priv->an_mutex);
1422 priv->an_state = AMD_XGBE_AN_EXIT;
1423 mutex_unlock(&priv->an_mutex);
1424
1425 flush_workqueue(priv->an_workqueue);
1426 destroy_workqueue(priv->an_workqueue);
1427
1428 /* Release resources */
1429 devm_iounmap(dev, priv->sir1_regs);
1430 devm_release_mem_region(dev, priv->sir1_res->start,
1431 resource_size(priv->sir1_res));
1432
1433 devm_iounmap(dev, priv->sir0_regs);
1434 devm_release_mem_region(dev, priv->sir0_res->start,
1435 resource_size(priv->sir0_res));
1436
1437 devm_iounmap(dev, priv->rxtx_regs);
1438 devm_release_mem_region(dev, priv->rxtx_res->start,
1439 resource_size(priv->rxtx_res));
1440
1441 devm_kfree(dev, priv);
1442}
1443
1444static int amd_xgbe_match_phy_device(struct phy_device *phydev)
1445{
1446 return phydev->c45_ids.device_ids[MDIO_MMD_PCS] == XGBE_PHY_ID;
1447}
1448
1449static struct phy_driver amd_xgbe_phy_driver[] = {
1450 {
1451 .phy_id = XGBE_PHY_ID,
1452 .phy_id_mask = XGBE_PHY_MASK,
1453 .name = "AMD XGBE PHY",
1454 .features = 0,
1455 .probe = amd_xgbe_phy_probe,
1456 .remove = amd_xgbe_phy_remove,
1457 .soft_reset = amd_xgbe_phy_soft_reset,
1458 .config_init = amd_xgbe_phy_config_init,
1459 .suspend = amd_xgbe_phy_suspend,
1460 .resume = amd_xgbe_phy_resume,
1461 .config_aneg = amd_xgbe_phy_config_aneg,
1462 .aneg_done = amd_xgbe_phy_aneg_done,
1463 .read_status = amd_xgbe_phy_read_status,
1464 .match_phy_device = amd_xgbe_match_phy_device,
1465 .driver = {
1466 .owner = THIS_MODULE,
1467 },
1468 },
1469};
1470
Johan Hovold50fd7152014-11-11 19:45:59 +01001471module_phy_driver(amd_xgbe_phy_driver);
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001472
françois romieua25aafa2014-06-07 11:07:48 +02001473static struct mdio_device_id __maybe_unused amd_xgbe_phy_ids[] = {
Lendacky, Thomas4d874b32014-06-05 09:15:12 -05001474 { XGBE_PHY_ID, XGBE_PHY_MASK },
1475 { }
1476};
1477MODULE_DEVICE_TABLE(mdio, amd_xgbe_phy_ids);