2 * AMD 10Gb Ethernet PHY driver
4 * This file is available to you under your choice of the following two
9 * Copyright (c) 2014 Advanced Micro Devices, Inc.
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.
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.
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/>.
25 * License 2: Modified BSD
27 * Copyright (c) 2014 Advanced Micro Devices, Inc.
28 * All rights reserved.
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.
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.
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/workqueue.h>
64 #include <linux/netdevice.h>
65 #include <linux/etherdevice.h>
66 #include <linux/skbuff.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>
75 #include <linux/of_platform.h>
76 #include <linux/of_device.h>
77 #include <linux/uaccess.h>
78 #include <linux/bitops.h>
79 #include <linux/property.h>
80 #include <linux/acpi.h>
81 #include <linux/jiffies.h>
83 MODULE_AUTHOR("Tom Lendacky <thomas.lendacky@amd.com>");
84 MODULE_LICENSE("Dual BSD/GPL");
85 MODULE_VERSION("1.0.0-a");
86 MODULE_DESCRIPTION("AMD 10GbE (amd-xgbe) PHY driver");
88 #define XGBE_PHY_ID 0x000162d0
89 #define XGBE_PHY_MASK 0xfffffff0
91 #define XGBE_PHY_SPEEDSET_PROPERTY "amd,speed-set"
92 #define XGBE_PHY_BLWC_PROPERTY "amd,serdes-blwc"
93 #define XGBE_PHY_CDR_RATE_PROPERTY "amd,serdes-cdr-rate"
94 #define XGBE_PHY_PQ_SKEW_PROPERTY "amd,serdes-pq-skew"
95 #define XGBE_PHY_TX_AMP_PROPERTY "amd,serdes-tx-amp"
96 #define XGBE_PHY_DFE_CFG_PROPERTY "amd,serdes-dfe-tap-config"
97 #define XGBE_PHY_DFE_ENA_PROPERTY "amd,serdes-dfe-tap-enable"
99 #define XGBE_PHY_SPEEDS 3
100 #define XGBE_PHY_SPEED_1000 0
101 #define XGBE_PHY_SPEED_2500 1
102 #define XGBE_PHY_SPEED_10000 2
104 #define XGBE_AN_MS_TIMEOUT 500
106 #define XGBE_AN_INT_CMPLT 0x01
107 #define XGBE_AN_INC_LINK 0x02
108 #define XGBE_AN_PG_RCV 0x04
109 #define XGBE_AN_INT_MASK 0x07
111 #define XNP_MCF_NULL_MESSAGE 0x001
112 #define XNP_ACK_PROCESSED BIT(12)
113 #define XNP_MP_FORMATTED BIT(13)
114 #define XNP_NP_EXCHANGE BIT(15)
116 #define XGBE_PHY_RATECHANGE_COUNT 500
118 #define XGBE_PHY_KR_TRAINING_START 0x01
119 #define XGBE_PHY_KR_TRAINING_ENABLE 0x02
121 #define XGBE_PHY_FEC_ENABLE 0x01
122 #define XGBE_PHY_FEC_FORWARD 0x02
123 #define XGBE_PHY_FEC_MASK 0x03
125 #ifndef MDIO_PMA_10GBR_PMD_CTRL
126 #define MDIO_PMA_10GBR_PMD_CTRL 0x0096
129 #ifndef MDIO_PMA_10GBR_FEC_ABILITY
130 #define MDIO_PMA_10GBR_FEC_ABILITY 0x00aa
133 #ifndef MDIO_PMA_10GBR_FEC_CTRL
134 #define MDIO_PMA_10GBR_FEC_CTRL 0x00ab
138 #define MDIO_AN_XNP 0x0016
142 #define MDIO_AN_LPX 0x0019
145 #ifndef MDIO_AN_INTMASK
146 #define MDIO_AN_INTMASK 0x8001
150 #define MDIO_AN_INT 0x8002
153 #ifndef MDIO_CTRL1_SPEED1G
154 #define MDIO_CTRL1_SPEED1G (MDIO_CTRL1_SPEED10G & ~BMCR_SPEED100)
157 /* SerDes integration register offsets */
158 #define SIR0_KR_RT_1 0x002c
159 #define SIR0_STATUS 0x0040
160 #define SIR1_SPEED 0x0000
162 /* SerDes integration register entry bit positions and sizes */
163 #define SIR0_KR_RT_1_RESET_INDEX 11
164 #define SIR0_KR_RT_1_RESET_WIDTH 1
165 #define SIR0_STATUS_RX_READY_INDEX 0
166 #define SIR0_STATUS_RX_READY_WIDTH 1
167 #define SIR0_STATUS_TX_READY_INDEX 8
168 #define SIR0_STATUS_TX_READY_WIDTH 1
169 #define SIR1_SPEED_CDR_RATE_INDEX 12
170 #define SIR1_SPEED_CDR_RATE_WIDTH 4
171 #define SIR1_SPEED_DATARATE_INDEX 4
172 #define SIR1_SPEED_DATARATE_WIDTH 2
173 #define SIR1_SPEED_PLLSEL_INDEX 3
174 #define SIR1_SPEED_PLLSEL_WIDTH 1
175 #define SIR1_SPEED_RATECHANGE_INDEX 6
176 #define SIR1_SPEED_RATECHANGE_WIDTH 1
177 #define SIR1_SPEED_TXAMP_INDEX 8
178 #define SIR1_SPEED_TXAMP_WIDTH 4
179 #define SIR1_SPEED_WORDMODE_INDEX 0
180 #define SIR1_SPEED_WORDMODE_WIDTH 3
182 #define SPEED_10000_BLWC 0
183 #define SPEED_10000_CDR 0x7
184 #define SPEED_10000_PLL 0x1
185 #define SPEED_10000_PQ 0x12
186 #define SPEED_10000_RATE 0x0
187 #define SPEED_10000_TXAMP 0xa
188 #define SPEED_10000_WORD 0x7
189 #define SPEED_10000_DFE_TAP_CONFIG 0x1
190 #define SPEED_10000_DFE_TAP_ENABLE 0x7f
192 #define SPEED_2500_BLWC 1
193 #define SPEED_2500_CDR 0x2
194 #define SPEED_2500_PLL 0x0
195 #define SPEED_2500_PQ 0xa
196 #define SPEED_2500_RATE 0x1
197 #define SPEED_2500_TXAMP 0xf
198 #define SPEED_2500_WORD 0x1
199 #define SPEED_2500_DFE_TAP_CONFIG 0x3
200 #define SPEED_2500_DFE_TAP_ENABLE 0x0
202 #define SPEED_1000_BLWC 1
203 #define SPEED_1000_CDR 0x2
204 #define SPEED_1000_PLL 0x0
205 #define SPEED_1000_PQ 0xa
206 #define SPEED_1000_RATE 0x3
207 #define SPEED_1000_TXAMP 0xf
208 #define SPEED_1000_WORD 0x1
209 #define SPEED_1000_DFE_TAP_CONFIG 0x3
210 #define SPEED_1000_DFE_TAP_ENABLE 0x0
212 /* SerDes RxTx register offsets */
213 #define RXTX_REG6 0x0018
214 #define RXTX_REG20 0x0050
215 #define RXTX_REG22 0x0058
216 #define RXTX_REG114 0x01c8
217 #define RXTX_REG129 0x0204
219 /* SerDes RxTx register entry bit positions and sizes */
220 #define RXTX_REG6_RESETB_RXD_INDEX 8
221 #define RXTX_REG6_RESETB_RXD_WIDTH 1
222 #define RXTX_REG20_BLWC_ENA_INDEX 2
223 #define RXTX_REG20_BLWC_ENA_WIDTH 1
224 #define RXTX_REG114_PQ_REG_INDEX 9
225 #define RXTX_REG114_PQ_REG_WIDTH 7
226 #define RXTX_REG129_RXDFE_CONFIG_INDEX 14
227 #define RXTX_REG129_RXDFE_CONFIG_WIDTH 2
229 /* Bit setting and getting macros
230 * The get macro will extract the current bit field value from within
233 * The set macro will clear the current bit field value within the
234 * variable and then set the bit field of the variable to the
237 #define GET_BITS(_var, _index, _width) \
238 (((_var) >> (_index)) & ((0x1 << (_width)) - 1))
240 #define SET_BITS(_var, _index, _width, _val) \
242 (_var) &= ~(((0x1 << (_width)) - 1) << (_index)); \
243 (_var) |= (((_val) & ((0x1 << (_width)) - 1)) << (_index)); \
246 #define XSIR_GET_BITS(_var, _prefix, _field) \
248 _prefix##_##_field##_INDEX, \
249 _prefix##_##_field##_WIDTH)
251 #define XSIR_SET_BITS(_var, _prefix, _field, _val) \
253 _prefix##_##_field##_INDEX, \
254 _prefix##_##_field##_WIDTH, (_val))
256 /* Macros for reading or writing SerDes integration registers
257 * The ioread macros will get bit fields or full values using the
258 * register definitions formed using the input names
260 * The iowrite macros will set bit fields or full values using the
261 * register definitions formed using the input names
263 #define XSIR0_IOREAD(_priv, _reg) \
264 ioread16((_priv)->sir0_regs + _reg)
266 #define XSIR0_IOREAD_BITS(_priv, _reg, _field) \
267 GET_BITS(XSIR0_IOREAD((_priv), _reg), \
268 _reg##_##_field##_INDEX, \
269 _reg##_##_field##_WIDTH)
271 #define XSIR0_IOWRITE(_priv, _reg, _val) \
272 iowrite16((_val), (_priv)->sir0_regs + _reg)
274 #define XSIR0_IOWRITE_BITS(_priv, _reg, _field, _val) \
276 u16 reg_val = XSIR0_IOREAD((_priv), _reg); \
278 _reg##_##_field##_INDEX, \
279 _reg##_##_field##_WIDTH, (_val)); \
280 XSIR0_IOWRITE((_priv), _reg, reg_val); \
283 #define XSIR1_IOREAD(_priv, _reg) \
284 ioread16((_priv)->sir1_regs + _reg)
286 #define XSIR1_IOREAD_BITS(_priv, _reg, _field) \
287 GET_BITS(XSIR1_IOREAD((_priv), _reg), \
288 _reg##_##_field##_INDEX, \
289 _reg##_##_field##_WIDTH)
291 #define XSIR1_IOWRITE(_priv, _reg, _val) \
292 iowrite16((_val), (_priv)->sir1_regs + _reg)
294 #define XSIR1_IOWRITE_BITS(_priv, _reg, _field, _val) \
296 u16 reg_val = XSIR1_IOREAD((_priv), _reg); \
298 _reg##_##_field##_INDEX, \
299 _reg##_##_field##_WIDTH, (_val)); \
300 XSIR1_IOWRITE((_priv), _reg, reg_val); \
303 /* Macros for reading or writing SerDes RxTx registers
304 * The ioread macros will get bit fields or full values using the
305 * register definitions formed using the input names
307 * The iowrite macros will set bit fields or full values using the
308 * register definitions formed using the input names
310 #define XRXTX_IOREAD(_priv, _reg) \
311 ioread16((_priv)->rxtx_regs + _reg)
313 #define XRXTX_IOREAD_BITS(_priv, _reg, _field) \
314 GET_BITS(XRXTX_IOREAD((_priv), _reg), \
315 _reg##_##_field##_INDEX, \
316 _reg##_##_field##_WIDTH)
318 #define XRXTX_IOWRITE(_priv, _reg, _val) \
319 iowrite16((_val), (_priv)->rxtx_regs + _reg)
321 #define XRXTX_IOWRITE_BITS(_priv, _reg, _field, _val) \
323 u16 reg_val = XRXTX_IOREAD((_priv), _reg); \
325 _reg##_##_field##_INDEX, \
326 _reg##_##_field##_WIDTH, (_val)); \
327 XRXTX_IOWRITE((_priv), _reg, reg_val); \
330 static const u32 amd_xgbe_phy_serdes_blwc
[] = {
336 static const u32 amd_xgbe_phy_serdes_cdr_rate
[] = {
342 static const u32 amd_xgbe_phy_serdes_pq_skew
[] = {
348 static const u32 amd_xgbe_phy_serdes_tx_amp
[] = {
354 static const u32 amd_xgbe_phy_serdes_dfe_tap_cfg
[] = {
355 SPEED_1000_DFE_TAP_CONFIG
,
356 SPEED_2500_DFE_TAP_CONFIG
,
357 SPEED_10000_DFE_TAP_CONFIG
,
360 static const u32 amd_xgbe_phy_serdes_dfe_tap_ena
[] = {
361 SPEED_1000_DFE_TAP_ENABLE
,
362 SPEED_2500_DFE_TAP_ENABLE
,
363 SPEED_10000_DFE_TAP_ENABLE
,
366 enum amd_xgbe_phy_an
{
367 AMD_XGBE_AN_READY
= 0,
368 AMD_XGBE_AN_PAGE_RECEIVED
,
369 AMD_XGBE_AN_INCOMPAT_LINK
,
370 AMD_XGBE_AN_COMPLETE
,
375 enum amd_xgbe_phy_rx
{
378 AMD_XGBE_RX_COMPLETE
,
382 enum amd_xgbe_phy_mode
{
387 enum amd_xgbe_phy_speedset
{
388 AMD_XGBE_PHY_SPEEDSET_1000_10000
= 0,
389 AMD_XGBE_PHY_SPEEDSET_2500_10000
,
392 struct amd_xgbe_phy_priv
{
393 struct platform_device
*pdev
;
394 struct acpi_device
*adev
;
397 struct phy_device
*phydev
;
399 /* SerDes related mmio resources */
400 struct resource
*rxtx_res
;
401 struct resource
*sir0_res
;
402 struct resource
*sir1_res
;
404 /* SerDes related mmio registers */
405 void __iomem
*rxtx_regs
; /* SerDes Rx/Tx CSRs */
406 void __iomem
*sir0_regs
; /* SerDes integration registers (1/2) */
407 void __iomem
*sir1_regs
; /* SerDes integration registers (2/2) */
410 char an_irq_name
[IFNAMSIZ
+ 32];
411 struct work_struct an_irq_work
;
412 unsigned int an_irq_allocated
;
414 unsigned int speed_set
;
416 /* SerDes UEFI configurable settings.
417 * Switching between modes/speeds requires new values for some
418 * SerDes settings. The values can be supplied as device
419 * properties in array format. The first array entry is for
420 * 1GbE, second for 2.5GbE and third for 10GbE
422 u32 serdes_blwc
[XGBE_PHY_SPEEDS
];
423 u32 serdes_cdr_rate
[XGBE_PHY_SPEEDS
];
424 u32 serdes_pq_skew
[XGBE_PHY_SPEEDS
];
425 u32 serdes_tx_amp
[XGBE_PHY_SPEEDS
];
426 u32 serdes_dfe_tap_cfg
[XGBE_PHY_SPEEDS
];
427 u32 serdes_dfe_tap_ena
[XGBE_PHY_SPEEDS
];
429 /* Auto-negotiation state machine support */
430 struct mutex an_mutex
;
431 enum amd_xgbe_phy_an an_result
;
432 enum amd_xgbe_phy_an an_state
;
433 enum amd_xgbe_phy_rx kr_state
;
434 enum amd_xgbe_phy_rx kx_state
;
435 struct work_struct an_work
;
436 struct workqueue_struct
*an_workqueue
;
437 unsigned int an_supported
;
438 unsigned int parallel_detect
;
439 unsigned int fec_ability
;
440 unsigned long an_start
;
442 unsigned int lpm_ctrl
; /* CTRL1 for resume */
445 static int amd_xgbe_an_enable_kr_training(struct phy_device
*phydev
)
449 ret
= phy_read_mmd(phydev
, MDIO_MMD_PMAPMD
, MDIO_PMA_10GBR_PMD_CTRL
);
453 ret
|= XGBE_PHY_KR_TRAINING_ENABLE
;
454 phy_write_mmd(phydev
, MDIO_MMD_PMAPMD
, MDIO_PMA_10GBR_PMD_CTRL
, ret
);
459 static int amd_xgbe_an_disable_kr_training(struct phy_device
*phydev
)
463 ret
= phy_read_mmd(phydev
, MDIO_MMD_PMAPMD
, MDIO_PMA_10GBR_PMD_CTRL
);
467 ret
&= ~XGBE_PHY_KR_TRAINING_ENABLE
;
468 phy_write_mmd(phydev
, MDIO_MMD_PMAPMD
, MDIO_PMA_10GBR_PMD_CTRL
, ret
);
473 static int amd_xgbe_phy_pcs_power_cycle(struct phy_device
*phydev
)
477 ret
= phy_read_mmd(phydev
, MDIO_MMD_PCS
, MDIO_CTRL1
);
481 ret
|= MDIO_CTRL1_LPOWER
;
482 phy_write_mmd(phydev
, MDIO_MMD_PCS
, MDIO_CTRL1
, ret
);
484 usleep_range(75, 100);
486 ret
&= ~MDIO_CTRL1_LPOWER
;
487 phy_write_mmd(phydev
, MDIO_MMD_PCS
, MDIO_CTRL1
, ret
);
492 static void amd_xgbe_phy_serdes_start_ratechange(struct phy_device
*phydev
)
494 struct amd_xgbe_phy_priv
*priv
= phydev
->priv
;
496 /* Assert Rx and Tx ratechange */
497 XSIR1_IOWRITE_BITS(priv
, SIR1_SPEED
, RATECHANGE
, 1);
500 static void amd_xgbe_phy_serdes_complete_ratechange(struct phy_device
*phydev
)
502 struct amd_xgbe_phy_priv
*priv
= phydev
->priv
;
506 /* Release Rx and Tx ratechange */
507 XSIR1_IOWRITE_BITS(priv
, SIR1_SPEED
, RATECHANGE
, 0);
509 /* Wait for Rx and Tx ready */
510 wait
= XGBE_PHY_RATECHANGE_COUNT
;
512 usleep_range(50, 75);
514 status
= XSIR0_IOREAD(priv
, SIR0_STATUS
);
515 if (XSIR_GET_BITS(status
, SIR0_STATUS
, RX_READY
) &&
516 XSIR_GET_BITS(status
, SIR0_STATUS
, TX_READY
))
520 netdev_dbg(phydev
->attached_dev
, "SerDes rx/tx not ready (%#hx)\n",
524 /* Perform Rx reset for the DFE changes */
525 XRXTX_IOWRITE_BITS(priv
, RXTX_REG6
, RESETB_RXD
, 0);
526 XRXTX_IOWRITE_BITS(priv
, RXTX_REG6
, RESETB_RXD
, 1);
529 static int amd_xgbe_phy_xgmii_mode(struct phy_device
*phydev
)
531 struct amd_xgbe_phy_priv
*priv
= phydev
->priv
;
534 /* Enable KR training */
535 ret
= amd_xgbe_an_enable_kr_training(phydev
);
539 /* Set PCS to KR/10G speed */
540 ret
= phy_read_mmd(phydev
, MDIO_MMD_PCS
, MDIO_CTRL2
);
544 ret
&= ~MDIO_PCS_CTRL2_TYPE
;
545 ret
|= MDIO_PCS_CTRL2_10GBR
;
546 phy_write_mmd(phydev
, MDIO_MMD_PCS
, MDIO_CTRL2
, ret
);
548 ret
= phy_read_mmd(phydev
, MDIO_MMD_PCS
, MDIO_CTRL1
);
552 ret
&= ~MDIO_CTRL1_SPEEDSEL
;
553 ret
|= MDIO_CTRL1_SPEED10G
;
554 phy_write_mmd(phydev
, MDIO_MMD_PCS
, MDIO_CTRL1
, ret
);
556 ret
= amd_xgbe_phy_pcs_power_cycle(phydev
);
560 /* Set SerDes to 10G speed */
561 amd_xgbe_phy_serdes_start_ratechange(phydev
);
563 XSIR1_IOWRITE_BITS(priv
, SIR1_SPEED
, DATARATE
, SPEED_10000_RATE
);
564 XSIR1_IOWRITE_BITS(priv
, SIR1_SPEED
, WORDMODE
, SPEED_10000_WORD
);
565 XSIR1_IOWRITE_BITS(priv
, SIR1_SPEED
, PLLSEL
, SPEED_10000_PLL
);
567 XSIR1_IOWRITE_BITS(priv
, SIR1_SPEED
, CDR_RATE
,
568 priv
->serdes_cdr_rate
[XGBE_PHY_SPEED_10000
]);
569 XSIR1_IOWRITE_BITS(priv
, SIR1_SPEED
, TXAMP
,
570 priv
->serdes_tx_amp
[XGBE_PHY_SPEED_10000
]);
571 XRXTX_IOWRITE_BITS(priv
, RXTX_REG20
, BLWC_ENA
,
572 priv
->serdes_blwc
[XGBE_PHY_SPEED_10000
]);
573 XRXTX_IOWRITE_BITS(priv
, RXTX_REG114
, PQ_REG
,
574 priv
->serdes_pq_skew
[XGBE_PHY_SPEED_10000
]);
575 XRXTX_IOWRITE_BITS(priv
, RXTX_REG129
, RXDFE_CONFIG
,
576 priv
->serdes_dfe_tap_cfg
[XGBE_PHY_SPEED_10000
]);
577 XRXTX_IOWRITE(priv
, RXTX_REG22
,
578 priv
->serdes_dfe_tap_ena
[XGBE_PHY_SPEED_10000
]);
580 amd_xgbe_phy_serdes_complete_ratechange(phydev
);
585 static int amd_xgbe_phy_gmii_2500_mode(struct phy_device
*phydev
)
587 struct amd_xgbe_phy_priv
*priv
= phydev
->priv
;
590 /* Disable KR training */
591 ret
= amd_xgbe_an_disable_kr_training(phydev
);
595 /* Set PCS to KX/1G speed */
596 ret
= phy_read_mmd(phydev
, MDIO_MMD_PCS
, MDIO_CTRL2
);
600 ret
&= ~MDIO_PCS_CTRL2_TYPE
;
601 ret
|= MDIO_PCS_CTRL2_10GBX
;
602 phy_write_mmd(phydev
, MDIO_MMD_PCS
, MDIO_CTRL2
, ret
);
604 ret
= phy_read_mmd(phydev
, MDIO_MMD_PCS
, MDIO_CTRL1
);
608 ret
&= ~MDIO_CTRL1_SPEEDSEL
;
609 ret
|= MDIO_CTRL1_SPEED1G
;
610 phy_write_mmd(phydev
, MDIO_MMD_PCS
, MDIO_CTRL1
, ret
);
612 ret
= amd_xgbe_phy_pcs_power_cycle(phydev
);
616 /* Set SerDes to 2.5G speed */
617 amd_xgbe_phy_serdes_start_ratechange(phydev
);
619 XSIR1_IOWRITE_BITS(priv
, SIR1_SPEED
, DATARATE
, SPEED_2500_RATE
);
620 XSIR1_IOWRITE_BITS(priv
, SIR1_SPEED
, WORDMODE
, SPEED_2500_WORD
);
621 XSIR1_IOWRITE_BITS(priv
, SIR1_SPEED
, PLLSEL
, SPEED_2500_PLL
);
623 XSIR1_IOWRITE_BITS(priv
, SIR1_SPEED
, CDR_RATE
,
624 priv
->serdes_cdr_rate
[XGBE_PHY_SPEED_2500
]);
625 XSIR1_IOWRITE_BITS(priv
, SIR1_SPEED
, TXAMP
,
626 priv
->serdes_tx_amp
[XGBE_PHY_SPEED_2500
]);
627 XRXTX_IOWRITE_BITS(priv
, RXTX_REG20
, BLWC_ENA
,
628 priv
->serdes_blwc
[XGBE_PHY_SPEED_2500
]);
629 XRXTX_IOWRITE_BITS(priv
, RXTX_REG114
, PQ_REG
,
630 priv
->serdes_pq_skew
[XGBE_PHY_SPEED_2500
]);
631 XRXTX_IOWRITE_BITS(priv
, RXTX_REG129
, RXDFE_CONFIG
,
632 priv
->serdes_dfe_tap_cfg
[XGBE_PHY_SPEED_2500
]);
633 XRXTX_IOWRITE(priv
, RXTX_REG22
,
634 priv
->serdes_dfe_tap_ena
[XGBE_PHY_SPEED_2500
]);
636 amd_xgbe_phy_serdes_complete_ratechange(phydev
);
641 static int amd_xgbe_phy_gmii_mode(struct phy_device
*phydev
)
643 struct amd_xgbe_phy_priv
*priv
= phydev
->priv
;
646 /* Disable KR training */
647 ret
= amd_xgbe_an_disable_kr_training(phydev
);
651 /* Set PCS to KX/1G speed */
652 ret
= phy_read_mmd(phydev
, MDIO_MMD_PCS
, MDIO_CTRL2
);
656 ret
&= ~MDIO_PCS_CTRL2_TYPE
;
657 ret
|= MDIO_PCS_CTRL2_10GBX
;
658 phy_write_mmd(phydev
, MDIO_MMD_PCS
, MDIO_CTRL2
, ret
);
660 ret
= phy_read_mmd(phydev
, MDIO_MMD_PCS
, MDIO_CTRL1
);
664 ret
&= ~MDIO_CTRL1_SPEEDSEL
;
665 ret
|= MDIO_CTRL1_SPEED1G
;
666 phy_write_mmd(phydev
, MDIO_MMD_PCS
, MDIO_CTRL1
, ret
);
668 ret
= amd_xgbe_phy_pcs_power_cycle(phydev
);
672 /* Set SerDes to 1G speed */
673 amd_xgbe_phy_serdes_start_ratechange(phydev
);
675 XSIR1_IOWRITE_BITS(priv
, SIR1_SPEED
, DATARATE
, SPEED_1000_RATE
);
676 XSIR1_IOWRITE_BITS(priv
, SIR1_SPEED
, WORDMODE
, SPEED_1000_WORD
);
677 XSIR1_IOWRITE_BITS(priv
, SIR1_SPEED
, PLLSEL
, SPEED_1000_PLL
);
679 XSIR1_IOWRITE_BITS(priv
, SIR1_SPEED
, CDR_RATE
,
680 priv
->serdes_cdr_rate
[XGBE_PHY_SPEED_1000
]);
681 XSIR1_IOWRITE_BITS(priv
, SIR1_SPEED
, TXAMP
,
682 priv
->serdes_tx_amp
[XGBE_PHY_SPEED_1000
]);
683 XRXTX_IOWRITE_BITS(priv
, RXTX_REG20
, BLWC_ENA
,
684 priv
->serdes_blwc
[XGBE_PHY_SPEED_1000
]);
685 XRXTX_IOWRITE_BITS(priv
, RXTX_REG114
, PQ_REG
,
686 priv
->serdes_pq_skew
[XGBE_PHY_SPEED_1000
]);
687 XRXTX_IOWRITE_BITS(priv
, RXTX_REG129
, RXDFE_CONFIG
,
688 priv
->serdes_dfe_tap_cfg
[XGBE_PHY_SPEED_1000
]);
689 XRXTX_IOWRITE(priv
, RXTX_REG22
,
690 priv
->serdes_dfe_tap_ena
[XGBE_PHY_SPEED_1000
]);
692 amd_xgbe_phy_serdes_complete_ratechange(phydev
);
697 static int amd_xgbe_phy_cur_mode(struct phy_device
*phydev
,
698 enum amd_xgbe_phy_mode
*mode
)
702 ret
= phy_read_mmd(phydev
, MDIO_MMD_PCS
, MDIO_CTRL2
);
706 if ((ret
& MDIO_PCS_CTRL2_TYPE
) == MDIO_PCS_CTRL2_10GBR
)
707 *mode
= AMD_XGBE_MODE_KR
;
709 *mode
= AMD_XGBE_MODE_KX
;
714 static bool amd_xgbe_phy_in_kr_mode(struct phy_device
*phydev
)
716 enum amd_xgbe_phy_mode mode
;
718 if (amd_xgbe_phy_cur_mode(phydev
, &mode
))
721 return (mode
== AMD_XGBE_MODE_KR
);
724 static int amd_xgbe_phy_switch_mode(struct phy_device
*phydev
)
726 struct amd_xgbe_phy_priv
*priv
= phydev
->priv
;
729 /* If we are in KR switch to KX, and vice-versa */
730 if (amd_xgbe_phy_in_kr_mode(phydev
)) {
731 if (priv
->speed_set
== AMD_XGBE_PHY_SPEEDSET_1000_10000
)
732 ret
= amd_xgbe_phy_gmii_mode(phydev
);
734 ret
= amd_xgbe_phy_gmii_2500_mode(phydev
);
736 ret
= amd_xgbe_phy_xgmii_mode(phydev
);
742 static int amd_xgbe_phy_set_mode(struct phy_device
*phydev
,
743 enum amd_xgbe_phy_mode mode
)
745 enum amd_xgbe_phy_mode cur_mode
;
748 ret
= amd_xgbe_phy_cur_mode(phydev
, &cur_mode
);
752 if (mode
!= cur_mode
)
753 ret
= amd_xgbe_phy_switch_mode(phydev
);
758 static bool amd_xgbe_phy_use_xgmii_mode(struct phy_device
*phydev
)
760 if (phydev
->autoneg
== AUTONEG_ENABLE
) {
761 if (phydev
->advertising
& ADVERTISED_10000baseKR_Full
)
764 if (phydev
->speed
== SPEED_10000
)
771 static bool amd_xgbe_phy_use_gmii_2500_mode(struct phy_device
*phydev
)
773 if (phydev
->autoneg
== AUTONEG_ENABLE
) {
774 if (phydev
->advertising
& ADVERTISED_2500baseX_Full
)
777 if (phydev
->speed
== SPEED_2500
)
784 static bool amd_xgbe_phy_use_gmii_mode(struct phy_device
*phydev
)
786 if (phydev
->autoneg
== AUTONEG_ENABLE
) {
787 if (phydev
->advertising
& ADVERTISED_1000baseKX_Full
)
790 if (phydev
->speed
== SPEED_1000
)
797 static int amd_xgbe_phy_set_an(struct phy_device
*phydev
, bool enable
,
802 ret
= phy_read_mmd(phydev
, MDIO_MMD_AN
, MDIO_CTRL1
);
806 ret
&= ~MDIO_AN_CTRL1_ENABLE
;
809 ret
|= MDIO_AN_CTRL1_ENABLE
;
812 ret
|= MDIO_AN_CTRL1_RESTART
;
814 phy_write_mmd(phydev
, MDIO_MMD_AN
, MDIO_CTRL1
, ret
);
819 static int amd_xgbe_phy_restart_an(struct phy_device
*phydev
)
821 return amd_xgbe_phy_set_an(phydev
, true, true);
824 static int amd_xgbe_phy_disable_an(struct phy_device
*phydev
)
826 return amd_xgbe_phy_set_an(phydev
, false, false);
829 static enum amd_xgbe_phy_an
amd_xgbe_an_tx_training(struct phy_device
*phydev
,
830 enum amd_xgbe_phy_rx
*state
)
832 struct amd_xgbe_phy_priv
*priv
= phydev
->priv
;
833 int ad_reg
, lp_reg
, ret
;
835 *state
= AMD_XGBE_RX_COMPLETE
;
837 /* If we're not in KR mode then we're done */
838 if (!amd_xgbe_phy_in_kr_mode(phydev
))
839 return AMD_XGBE_AN_PAGE_RECEIVED
;
841 /* Enable/Disable FEC */
842 ad_reg
= phy_read_mmd(phydev
, MDIO_MMD_AN
, MDIO_AN_ADVERTISE
+ 2);
844 return AMD_XGBE_AN_ERROR
;
846 lp_reg
= phy_read_mmd(phydev
, MDIO_MMD_AN
, MDIO_AN_LPA
+ 2);
848 return AMD_XGBE_AN_ERROR
;
850 ret
= phy_read_mmd(phydev
, MDIO_MMD_PMAPMD
, MDIO_PMA_10GBR_FEC_CTRL
);
852 return AMD_XGBE_AN_ERROR
;
854 ret
&= ~XGBE_PHY_FEC_MASK
;
855 if ((ad_reg
& 0xc000) && (lp_reg
& 0xc000))
856 ret
|= priv
->fec_ability
;
858 phy_write_mmd(phydev
, MDIO_MMD_PMAPMD
, MDIO_PMA_10GBR_FEC_CTRL
, ret
);
860 /* Start KR training */
861 ret
= phy_read_mmd(phydev
, MDIO_MMD_PMAPMD
, MDIO_PMA_10GBR_PMD_CTRL
);
863 return AMD_XGBE_AN_ERROR
;
865 if (ret
& XGBE_PHY_KR_TRAINING_ENABLE
) {
866 XSIR0_IOWRITE_BITS(priv
, SIR0_KR_RT_1
, RESET
, 1);
868 ret
|= XGBE_PHY_KR_TRAINING_START
;
869 phy_write_mmd(phydev
, MDIO_MMD_PMAPMD
, MDIO_PMA_10GBR_PMD_CTRL
,
872 XSIR0_IOWRITE_BITS(priv
, SIR0_KR_RT_1
, RESET
, 0);
875 return AMD_XGBE_AN_PAGE_RECEIVED
;
878 static enum amd_xgbe_phy_an
amd_xgbe_an_tx_xnp(struct phy_device
*phydev
,
879 enum amd_xgbe_phy_rx
*state
)
883 *state
= AMD_XGBE_RX_XNP
;
885 msg
= XNP_MCF_NULL_MESSAGE
;
886 msg
|= XNP_MP_FORMATTED
;
888 phy_write_mmd(phydev
, MDIO_MMD_AN
, MDIO_AN_XNP
+ 2, 0);
889 phy_write_mmd(phydev
, MDIO_MMD_AN
, MDIO_AN_XNP
+ 1, 0);
890 phy_write_mmd(phydev
, MDIO_MMD_AN
, MDIO_AN_XNP
, msg
);
892 return AMD_XGBE_AN_PAGE_RECEIVED
;
895 static enum amd_xgbe_phy_an
amd_xgbe_an_rx_bpa(struct phy_device
*phydev
,
896 enum amd_xgbe_phy_rx
*state
)
898 unsigned int link_support
;
899 int ret
, ad_reg
, lp_reg
;
901 /* Read Base Ability register 2 first */
902 ret
= phy_read_mmd(phydev
, MDIO_MMD_AN
, MDIO_AN_LPA
+ 1);
904 return AMD_XGBE_AN_ERROR
;
906 /* Check for a supported mode, otherwise restart in a different one */
907 link_support
= amd_xgbe_phy_in_kr_mode(phydev
) ? 0x80 : 0x20;
908 if (!(ret
& link_support
))
909 return AMD_XGBE_AN_INCOMPAT_LINK
;
911 /* Check Extended Next Page support */
912 ad_reg
= phy_read_mmd(phydev
, MDIO_MMD_AN
, MDIO_AN_ADVERTISE
);
914 return AMD_XGBE_AN_ERROR
;
916 lp_reg
= phy_read_mmd(phydev
, MDIO_MMD_AN
, MDIO_AN_LPA
);
918 return AMD_XGBE_AN_ERROR
;
920 return ((ad_reg
& XNP_NP_EXCHANGE
) || (lp_reg
& XNP_NP_EXCHANGE
)) ?
921 amd_xgbe_an_tx_xnp(phydev
, state
) :
922 amd_xgbe_an_tx_training(phydev
, state
);
925 static enum amd_xgbe_phy_an
amd_xgbe_an_rx_xnp(struct phy_device
*phydev
,
926 enum amd_xgbe_phy_rx
*state
)
930 /* Check Extended Next Page support */
931 ad_reg
= phy_read_mmd(phydev
, MDIO_MMD_AN
, MDIO_AN_XNP
);
933 return AMD_XGBE_AN_ERROR
;
935 lp_reg
= phy_read_mmd(phydev
, MDIO_MMD_AN
, MDIO_AN_LPX
);
937 return AMD_XGBE_AN_ERROR
;
939 return ((ad_reg
& XNP_NP_EXCHANGE
) || (lp_reg
& XNP_NP_EXCHANGE
)) ?
940 amd_xgbe_an_tx_xnp(phydev
, state
) :
941 amd_xgbe_an_tx_training(phydev
, state
);
944 static enum amd_xgbe_phy_an
amd_xgbe_an_page_received(struct phy_device
*phydev
)
946 struct amd_xgbe_phy_priv
*priv
= phydev
->priv
;
947 enum amd_xgbe_phy_rx
*state
;
948 unsigned long an_timeout
;
951 if (!priv
->an_start
) {
952 priv
->an_start
= jiffies
;
954 an_timeout
= priv
->an_start
+
955 msecs_to_jiffies(XGBE_AN_MS_TIMEOUT
);
956 if (time_after(jiffies
, an_timeout
)) {
957 /* Auto-negotiation timed out, reset state */
958 priv
->kr_state
= AMD_XGBE_RX_BPA
;
959 priv
->kx_state
= AMD_XGBE_RX_BPA
;
961 priv
->an_start
= jiffies
;
965 state
= amd_xgbe_phy_in_kr_mode(phydev
) ? &priv
->kr_state
969 case AMD_XGBE_RX_BPA
:
970 ret
= amd_xgbe_an_rx_bpa(phydev
, state
);
973 case AMD_XGBE_RX_XNP
:
974 ret
= amd_xgbe_an_rx_xnp(phydev
, state
);
978 ret
= AMD_XGBE_AN_ERROR
;
984 static enum amd_xgbe_phy_an
amd_xgbe_an_incompat_link(struct phy_device
*phydev
)
986 struct amd_xgbe_phy_priv
*priv
= phydev
->priv
;
989 /* Be sure we aren't looping trying to negotiate */
990 if (amd_xgbe_phy_in_kr_mode(phydev
)) {
991 priv
->kr_state
= AMD_XGBE_RX_ERROR
;
993 if (!(phydev
->advertising
& SUPPORTED_1000baseKX_Full
) &&
994 !(phydev
->advertising
& SUPPORTED_2500baseX_Full
))
995 return AMD_XGBE_AN_NO_LINK
;
997 if (priv
->kx_state
!= AMD_XGBE_RX_BPA
)
998 return AMD_XGBE_AN_NO_LINK
;
1000 priv
->kx_state
= AMD_XGBE_RX_ERROR
;
1002 if (!(phydev
->advertising
& SUPPORTED_10000baseKR_Full
))
1003 return AMD_XGBE_AN_NO_LINK
;
1005 if (priv
->kr_state
!= AMD_XGBE_RX_BPA
)
1006 return AMD_XGBE_AN_NO_LINK
;
1009 ret
= amd_xgbe_phy_disable_an(phydev
);
1011 return AMD_XGBE_AN_ERROR
;
1013 ret
= amd_xgbe_phy_switch_mode(phydev
);
1015 return AMD_XGBE_AN_ERROR
;
1017 ret
= amd_xgbe_phy_restart_an(phydev
);
1019 return AMD_XGBE_AN_ERROR
;
1021 return AMD_XGBE_AN_INCOMPAT_LINK
;
1024 static irqreturn_t
amd_xgbe_an_isr(int irq
, void *data
)
1026 struct amd_xgbe_phy_priv
*priv
= (struct amd_xgbe_phy_priv
*)data
;
1028 /* Interrupt reason must be read and cleared outside of IRQ context */
1029 disable_irq_nosync(priv
->an_irq
);
1031 queue_work(priv
->an_workqueue
, &priv
->an_irq_work
);
1036 static void amd_xgbe_an_irq_work(struct work_struct
*work
)
1038 struct amd_xgbe_phy_priv
*priv
= container_of(work
,
1039 struct amd_xgbe_phy_priv
,
1042 /* Avoid a race between enabling the IRQ and exiting the work by
1043 * waiting for the work to finish and then queueing it
1045 flush_work(&priv
->an_work
);
1046 queue_work(priv
->an_workqueue
, &priv
->an_work
);
1049 static void amd_xgbe_an_state_machine(struct work_struct
*work
)
1051 struct amd_xgbe_phy_priv
*priv
= container_of(work
,
1052 struct amd_xgbe_phy_priv
,
1054 struct phy_device
*phydev
= priv
->phydev
;
1055 enum amd_xgbe_phy_an cur_state
= priv
->an_state
;
1056 int int_reg
, int_mask
;
1058 mutex_lock(&priv
->an_mutex
);
1060 /* Read the interrupt */
1061 int_reg
= phy_read_mmd(phydev
, MDIO_MMD_AN
, MDIO_AN_INT
);
1067 priv
->an_state
= AMD_XGBE_AN_ERROR
;
1068 int_mask
= XGBE_AN_INT_MASK
;
1069 } else if (int_reg
& XGBE_AN_PG_RCV
) {
1070 priv
->an_state
= AMD_XGBE_AN_PAGE_RECEIVED
;
1071 int_mask
= XGBE_AN_PG_RCV
;
1072 } else if (int_reg
& XGBE_AN_INC_LINK
) {
1073 priv
->an_state
= AMD_XGBE_AN_INCOMPAT_LINK
;
1074 int_mask
= XGBE_AN_INC_LINK
;
1075 } else if (int_reg
& XGBE_AN_INT_CMPLT
) {
1076 priv
->an_state
= AMD_XGBE_AN_COMPLETE
;
1077 int_mask
= XGBE_AN_INT_CMPLT
;
1079 priv
->an_state
= AMD_XGBE_AN_ERROR
;
1083 /* Clear the interrupt to be processed */
1084 int_reg
&= ~int_mask
;
1085 phy_write_mmd(phydev
, MDIO_MMD_AN
, MDIO_AN_INT
, int_reg
);
1087 priv
->an_result
= priv
->an_state
;
1090 cur_state
= priv
->an_state
;
1092 switch (priv
->an_state
) {
1093 case AMD_XGBE_AN_READY
:
1094 priv
->an_supported
= 0;
1097 case AMD_XGBE_AN_PAGE_RECEIVED
:
1098 priv
->an_state
= amd_xgbe_an_page_received(phydev
);
1099 priv
->an_supported
++;
1102 case AMD_XGBE_AN_INCOMPAT_LINK
:
1103 priv
->an_supported
= 0;
1104 priv
->parallel_detect
= 0;
1105 priv
->an_state
= amd_xgbe_an_incompat_link(phydev
);
1108 case AMD_XGBE_AN_COMPLETE
:
1109 priv
->parallel_detect
= priv
->an_supported
? 0 : 1;
1110 netdev_dbg(phydev
->attached_dev
, "%s successful\n",
1111 priv
->an_supported
? "Auto negotiation"
1112 : "Parallel detection");
1115 case AMD_XGBE_AN_NO_LINK
:
1119 priv
->an_state
= AMD_XGBE_AN_ERROR
;
1122 if (priv
->an_state
== AMD_XGBE_AN_NO_LINK
) {
1124 phy_write_mmd(phydev
, MDIO_MMD_AN
, MDIO_AN_INT
, 0);
1125 } else if (priv
->an_state
== AMD_XGBE_AN_ERROR
) {
1126 netdev_err(phydev
->attached_dev
,
1127 "error during auto-negotiation, state=%u\n",
1131 phy_write_mmd(phydev
, MDIO_MMD_AN
, MDIO_AN_INT
, 0);
1134 if (priv
->an_state
>= AMD_XGBE_AN_COMPLETE
) {
1135 priv
->an_result
= priv
->an_state
;
1136 priv
->an_state
= AMD_XGBE_AN_READY
;
1137 priv
->kr_state
= AMD_XGBE_RX_BPA
;
1138 priv
->kx_state
= AMD_XGBE_RX_BPA
;
1142 if (cur_state
!= priv
->an_state
)
1149 enable_irq(priv
->an_irq
);
1151 mutex_unlock(&priv
->an_mutex
);
1154 static int amd_xgbe_an_init(struct phy_device
*phydev
)
1158 /* Set up Advertisement register 3 first */
1159 ret
= phy_read_mmd(phydev
, MDIO_MMD_AN
, MDIO_AN_ADVERTISE
+ 2);
1163 if (phydev
->advertising
& SUPPORTED_10000baseR_FEC
)
1168 phy_write_mmd(phydev
, MDIO_MMD_AN
, MDIO_AN_ADVERTISE
+ 2, ret
);
1170 /* Set up Advertisement register 2 next */
1171 ret
= phy_read_mmd(phydev
, MDIO_MMD_AN
, MDIO_AN_ADVERTISE
+ 1);
1175 if (phydev
->advertising
& SUPPORTED_10000baseKR_Full
)
1180 if ((phydev
->advertising
& SUPPORTED_1000baseKX_Full
) ||
1181 (phydev
->advertising
& SUPPORTED_2500baseX_Full
))
1186 phy_write_mmd(phydev
, MDIO_MMD_AN
, MDIO_AN_ADVERTISE
+ 1, ret
);
1188 /* Set up Advertisement register 1 last */
1189 ret
= phy_read_mmd(phydev
, MDIO_MMD_AN
, MDIO_AN_ADVERTISE
);
1193 if (phydev
->advertising
& SUPPORTED_Pause
)
1198 if (phydev
->advertising
& SUPPORTED_Asym_Pause
)
1203 /* We don't intend to perform XNP */
1204 ret
&= ~XNP_NP_EXCHANGE
;
1206 phy_write_mmd(phydev
, MDIO_MMD_AN
, MDIO_AN_ADVERTISE
, ret
);
1211 static int amd_xgbe_phy_soft_reset(struct phy_device
*phydev
)
1215 ret
= phy_read_mmd(phydev
, MDIO_MMD_PCS
, MDIO_CTRL1
);
1219 ret
|= MDIO_CTRL1_RESET
;
1220 phy_write_mmd(phydev
, MDIO_MMD_PCS
, MDIO_CTRL1
, ret
);
1225 ret
= phy_read_mmd(phydev
, MDIO_MMD_PCS
, MDIO_CTRL1
);
1228 } while ((ret
& MDIO_CTRL1_RESET
) && --count
);
1230 if (ret
& MDIO_CTRL1_RESET
)
1233 /* Disable auto-negotiation for now */
1234 ret
= amd_xgbe_phy_disable_an(phydev
);
1238 /* Clear auto-negotiation interrupts */
1239 phy_write_mmd(phydev
, MDIO_MMD_AN
, MDIO_AN_INT
, 0);
1244 static int amd_xgbe_phy_config_init(struct phy_device
*phydev
)
1246 struct amd_xgbe_phy_priv
*priv
= phydev
->priv
;
1247 struct net_device
*netdev
= phydev
->attached_dev
;
1250 if (!priv
->an_irq_allocated
) {
1251 /* Allocate the auto-negotiation workqueue and interrupt */
1252 snprintf(priv
->an_irq_name
, sizeof(priv
->an_irq_name
) - 1,
1253 "%s-pcs", netdev_name(netdev
));
1255 priv
->an_workqueue
=
1256 create_singlethread_workqueue(priv
->an_irq_name
);
1257 if (!priv
->an_workqueue
) {
1258 netdev_err(netdev
, "phy workqueue creation failed\n");
1262 ret
= devm_request_irq(priv
->dev
, priv
->an_irq
,
1263 amd_xgbe_an_isr
, 0, priv
->an_irq_name
,
1266 netdev_err(netdev
, "phy irq request failed\n");
1267 destroy_workqueue(priv
->an_workqueue
);
1271 priv
->an_irq_allocated
= 1;
1274 /* Set initial mode - call the mode setting routines
1275 * directly to insure we are properly configured
1277 if (amd_xgbe_phy_use_xgmii_mode(phydev
))
1278 ret
= amd_xgbe_phy_xgmii_mode(phydev
);
1279 else if (amd_xgbe_phy_use_gmii_mode(phydev
))
1280 ret
= amd_xgbe_phy_gmii_mode(phydev
);
1281 else if (amd_xgbe_phy_use_gmii_2500_mode(phydev
))
1282 ret
= amd_xgbe_phy_gmii_2500_mode(phydev
);
1288 /* Set up advertisement registers based on current settings */
1289 ret
= amd_xgbe_an_init(phydev
);
1293 /* Enable auto-negotiation interrupts */
1294 phy_write_mmd(phydev
, MDIO_MMD_AN
, MDIO_AN_INTMASK
, 0x07);
1299 static int amd_xgbe_phy_setup_forced(struct phy_device
*phydev
)
1303 /* Disable auto-negotiation */
1304 ret
= amd_xgbe_phy_disable_an(phydev
);
1308 /* Validate/Set specified speed */
1309 switch (phydev
->speed
) {
1311 ret
= amd_xgbe_phy_set_mode(phydev
, AMD_XGBE_MODE_KR
);
1316 ret
= amd_xgbe_phy_set_mode(phydev
, AMD_XGBE_MODE_KX
);
1326 /* Validate duplex mode */
1327 if (phydev
->duplex
!= DUPLEX_FULL
)
1331 phydev
->asym_pause
= 0;
1336 static int __amd_xgbe_phy_config_aneg(struct phy_device
*phydev
)
1338 struct amd_xgbe_phy_priv
*priv
= phydev
->priv
;
1339 u32 mmd_mask
= phydev
->c45_ids
.devices_in_package
;
1342 if (phydev
->autoneg
!= AUTONEG_ENABLE
)
1343 return amd_xgbe_phy_setup_forced(phydev
);
1345 /* Make sure we have the AN MMD present */
1346 if (!(mmd_mask
& MDIO_DEVS_AN
))
1349 /* Disable auto-negotiation interrupt */
1350 disable_irq(priv
->an_irq
);
1352 /* Start auto-negotiation in a supported mode */
1353 if (phydev
->advertising
& SUPPORTED_10000baseKR_Full
)
1354 ret
= amd_xgbe_phy_set_mode(phydev
, AMD_XGBE_MODE_KR
);
1355 else if ((phydev
->advertising
& SUPPORTED_1000baseKX_Full
) ||
1356 (phydev
->advertising
& SUPPORTED_2500baseX_Full
))
1357 ret
= amd_xgbe_phy_set_mode(phydev
, AMD_XGBE_MODE_KX
);
1361 enable_irq(priv
->an_irq
);
1365 /* Disable and stop any in progress auto-negotiation */
1366 ret
= amd_xgbe_phy_disable_an(phydev
);
1370 /* Clear any auto-negotitation interrupts */
1371 phy_write_mmd(phydev
, MDIO_MMD_AN
, MDIO_AN_INT
, 0);
1373 priv
->an_result
= AMD_XGBE_AN_READY
;
1374 priv
->an_state
= AMD_XGBE_AN_READY
;
1375 priv
->kr_state
= AMD_XGBE_RX_BPA
;
1376 priv
->kx_state
= AMD_XGBE_RX_BPA
;
1378 /* Re-enable auto-negotiation interrupt */
1379 enable_irq(priv
->an_irq
);
1381 /* Set up advertisement registers based on current settings */
1382 ret
= amd_xgbe_an_init(phydev
);
1386 /* Enable and start auto-negotiation */
1387 return amd_xgbe_phy_restart_an(phydev
);
1390 static int amd_xgbe_phy_config_aneg(struct phy_device
*phydev
)
1392 struct amd_xgbe_phy_priv
*priv
= phydev
->priv
;
1395 mutex_lock(&priv
->an_mutex
);
1397 ret
= __amd_xgbe_phy_config_aneg(phydev
);
1399 mutex_unlock(&priv
->an_mutex
);
1404 static int amd_xgbe_phy_aneg_done(struct phy_device
*phydev
)
1406 struct amd_xgbe_phy_priv
*priv
= phydev
->priv
;
1408 return (priv
->an_result
== AMD_XGBE_AN_COMPLETE
);
1411 static int amd_xgbe_phy_update_link(struct phy_device
*phydev
)
1413 struct amd_xgbe_phy_priv
*priv
= phydev
->priv
;
1416 /* If we're doing auto-negotiation don't report link down */
1417 if (priv
->an_state
!= AMD_XGBE_AN_READY
) {
1422 /* Link status is latched low, so read once to clear
1423 * and then read again to get current state
1425 ret
= phy_read_mmd(phydev
, MDIO_MMD_PCS
, MDIO_STAT1
);
1429 ret
= phy_read_mmd(phydev
, MDIO_MMD_PCS
, MDIO_STAT1
);
1433 phydev
->link
= (ret
& MDIO_STAT1_LSTATUS
) ? 1 : 0;
1438 static int amd_xgbe_phy_read_status(struct phy_device
*phydev
)
1440 struct amd_xgbe_phy_priv
*priv
= phydev
->priv
;
1441 u32 mmd_mask
= phydev
->c45_ids
.devices_in_package
;
1442 int ret
, ad_ret
, lp_ret
;
1444 ret
= amd_xgbe_phy_update_link(phydev
);
1448 if ((phydev
->autoneg
== AUTONEG_ENABLE
) &&
1449 !priv
->parallel_detect
) {
1450 if (!(mmd_mask
& MDIO_DEVS_AN
))
1453 if (!amd_xgbe_phy_aneg_done(phydev
))
1456 /* Compare Advertisement and Link Partner register 1 */
1457 ad_ret
= phy_read_mmd(phydev
, MDIO_MMD_AN
, MDIO_AN_ADVERTISE
);
1460 lp_ret
= phy_read_mmd(phydev
, MDIO_MMD_AN
, MDIO_AN_LPA
);
1465 phydev
->pause
= (ad_ret
& 0x400) ? 1 : 0;
1466 phydev
->asym_pause
= (ad_ret
& 0x800) ? 1 : 0;
1468 /* Compare Advertisement and Link Partner register 2 */
1469 ad_ret
= phy_read_mmd(phydev
, MDIO_MMD_AN
,
1470 MDIO_AN_ADVERTISE
+ 1);
1473 lp_ret
= phy_read_mmd(phydev
, MDIO_MMD_AN
, MDIO_AN_LPA
+ 1);
1478 if (ad_ret
& 0x80) {
1479 phydev
->speed
= SPEED_10000
;
1480 ret
= amd_xgbe_phy_set_mode(phydev
, AMD_XGBE_MODE_KR
);
1484 switch (priv
->speed_set
) {
1485 case AMD_XGBE_PHY_SPEEDSET_1000_10000
:
1486 phydev
->speed
= SPEED_1000
;
1489 case AMD_XGBE_PHY_SPEEDSET_2500_10000
:
1490 phydev
->speed
= SPEED_2500
;
1494 ret
= amd_xgbe_phy_set_mode(phydev
, AMD_XGBE_MODE_KX
);
1499 phydev
->duplex
= DUPLEX_FULL
;
1501 if (amd_xgbe_phy_in_kr_mode(phydev
)) {
1502 phydev
->speed
= SPEED_10000
;
1504 switch (priv
->speed_set
) {
1505 case AMD_XGBE_PHY_SPEEDSET_1000_10000
:
1506 phydev
->speed
= SPEED_1000
;
1509 case AMD_XGBE_PHY_SPEEDSET_2500_10000
:
1510 phydev
->speed
= SPEED_2500
;
1514 phydev
->duplex
= DUPLEX_FULL
;
1516 phydev
->asym_pause
= 0;
1522 static int amd_xgbe_phy_suspend(struct phy_device
*phydev
)
1524 struct amd_xgbe_phy_priv
*priv
= phydev
->priv
;
1527 mutex_lock(&phydev
->lock
);
1529 ret
= phy_read_mmd(phydev
, MDIO_MMD_PCS
, MDIO_CTRL1
);
1533 priv
->lpm_ctrl
= ret
;
1535 ret
|= MDIO_CTRL1_LPOWER
;
1536 phy_write_mmd(phydev
, MDIO_MMD_PCS
, MDIO_CTRL1
, ret
);
1541 mutex_unlock(&phydev
->lock
);
1546 static int amd_xgbe_phy_resume(struct phy_device
*phydev
)
1548 struct amd_xgbe_phy_priv
*priv
= phydev
->priv
;
1550 mutex_lock(&phydev
->lock
);
1552 priv
->lpm_ctrl
&= ~MDIO_CTRL1_LPOWER
;
1553 phy_write_mmd(phydev
, MDIO_MMD_PCS
, MDIO_CTRL1
, priv
->lpm_ctrl
);
1555 mutex_unlock(&phydev
->lock
);
1560 static unsigned int amd_xgbe_phy_resource_count(struct platform_device
*pdev
,
1566 for (i
= 0, count
= 0; i
< pdev
->num_resources
; i
++) {
1567 struct resource
*r
= &pdev
->resource
[i
];
1569 if (type
== resource_type(r
))
1576 static int amd_xgbe_phy_probe(struct phy_device
*phydev
)
1578 struct amd_xgbe_phy_priv
*priv
;
1579 struct platform_device
*phy_pdev
;
1580 struct device
*dev
, *phy_dev
;
1581 unsigned int phy_resnum
, phy_irqnum
;
1584 if (!phydev
->bus
|| !phydev
->bus
->parent
)
1587 dev
= phydev
->bus
->parent
;
1589 priv
= devm_kzalloc(dev
, sizeof(*priv
), GFP_KERNEL
);
1593 priv
->pdev
= to_platform_device(dev
);
1594 priv
->adev
= ACPI_COMPANION(dev
);
1596 priv
->phydev
= phydev
;
1597 mutex_init(&priv
->an_mutex
);
1598 INIT_WORK(&priv
->an_irq_work
, amd_xgbe_an_irq_work
);
1599 INIT_WORK(&priv
->an_work
, amd_xgbe_an_state_machine
);
1601 if (!priv
->adev
|| acpi_disabled
) {
1602 struct device_node
*bus_node
;
1603 struct device_node
*phy_node
;
1605 bus_node
= priv
->dev
->of_node
;
1606 phy_node
= of_parse_phandle(bus_node
, "phy-handle", 0);
1608 dev_err(dev
, "unable to parse phy-handle\n");
1613 phy_pdev
= of_find_device_by_node(phy_node
);
1614 of_node_put(phy_node
);
1617 dev_err(dev
, "unable to obtain phy device\n");
1625 /* In ACPI, the XGBE and PHY resources are the grouped
1626 * together with the PHY resources at the end
1628 phy_pdev
= priv
->pdev
;
1629 phy_resnum
= amd_xgbe_phy_resource_count(phy_pdev
,
1630 IORESOURCE_MEM
) - 3;
1631 phy_irqnum
= amd_xgbe_phy_resource_count(phy_pdev
,
1632 IORESOURCE_IRQ
) - 1;
1634 phy_dev
= &phy_pdev
->dev
;
1636 /* Get the device mmio areas */
1637 priv
->rxtx_res
= platform_get_resource(phy_pdev
, IORESOURCE_MEM
,
1639 priv
->rxtx_regs
= devm_ioremap_resource(dev
, priv
->rxtx_res
);
1640 if (IS_ERR(priv
->rxtx_regs
)) {
1641 dev_err(dev
, "rxtx ioremap failed\n");
1642 ret
= PTR_ERR(priv
->rxtx_regs
);
1646 priv
->sir0_res
= platform_get_resource(phy_pdev
, IORESOURCE_MEM
,
1648 priv
->sir0_regs
= devm_ioremap_resource(dev
, priv
->sir0_res
);
1649 if (IS_ERR(priv
->sir0_regs
)) {
1650 dev_err(dev
, "sir0 ioremap failed\n");
1651 ret
= PTR_ERR(priv
->sir0_regs
);
1655 priv
->sir1_res
= platform_get_resource(phy_pdev
, IORESOURCE_MEM
,
1657 priv
->sir1_regs
= devm_ioremap_resource(dev
, priv
->sir1_res
);
1658 if (IS_ERR(priv
->sir1_regs
)) {
1659 dev_err(dev
, "sir1 ioremap failed\n");
1660 ret
= PTR_ERR(priv
->sir1_regs
);
1664 /* Get the auto-negotiation interrupt */
1665 ret
= platform_get_irq(phy_pdev
, phy_irqnum
);
1667 dev_err(dev
, "platform_get_irq failed\n");
1672 /* Get the device speed set property */
1673 ret
= device_property_read_u32(phy_dev
, XGBE_PHY_SPEEDSET_PROPERTY
,
1676 dev_err(dev
, "invalid %s property\n",
1677 XGBE_PHY_SPEEDSET_PROPERTY
);
1681 switch (priv
->speed_set
) {
1682 case AMD_XGBE_PHY_SPEEDSET_1000_10000
:
1683 case AMD_XGBE_PHY_SPEEDSET_2500_10000
:
1686 dev_err(dev
, "invalid %s property\n",
1687 XGBE_PHY_SPEEDSET_PROPERTY
);
1692 if (device_property_present(phy_dev
, XGBE_PHY_BLWC_PROPERTY
)) {
1693 ret
= device_property_read_u32_array(phy_dev
,
1694 XGBE_PHY_BLWC_PROPERTY
,
1698 dev_err(dev
, "invalid %s property\n",
1699 XGBE_PHY_BLWC_PROPERTY
);
1703 memcpy(priv
->serdes_blwc
, amd_xgbe_phy_serdes_blwc
,
1704 sizeof(priv
->serdes_blwc
));
1707 if (device_property_present(phy_dev
, XGBE_PHY_CDR_RATE_PROPERTY
)) {
1708 ret
= device_property_read_u32_array(phy_dev
,
1709 XGBE_PHY_CDR_RATE_PROPERTY
,
1710 priv
->serdes_cdr_rate
,
1713 dev_err(dev
, "invalid %s property\n",
1714 XGBE_PHY_CDR_RATE_PROPERTY
);
1718 memcpy(priv
->serdes_cdr_rate
, amd_xgbe_phy_serdes_cdr_rate
,
1719 sizeof(priv
->serdes_cdr_rate
));
1722 if (device_property_present(phy_dev
, XGBE_PHY_PQ_SKEW_PROPERTY
)) {
1723 ret
= device_property_read_u32_array(phy_dev
,
1724 XGBE_PHY_PQ_SKEW_PROPERTY
,
1725 priv
->serdes_pq_skew
,
1728 dev_err(dev
, "invalid %s property\n",
1729 XGBE_PHY_PQ_SKEW_PROPERTY
);
1733 memcpy(priv
->serdes_pq_skew
, amd_xgbe_phy_serdes_pq_skew
,
1734 sizeof(priv
->serdes_pq_skew
));
1737 if (device_property_present(phy_dev
, XGBE_PHY_TX_AMP_PROPERTY
)) {
1738 ret
= device_property_read_u32_array(phy_dev
,
1739 XGBE_PHY_TX_AMP_PROPERTY
,
1740 priv
->serdes_tx_amp
,
1743 dev_err(dev
, "invalid %s property\n",
1744 XGBE_PHY_TX_AMP_PROPERTY
);
1748 memcpy(priv
->serdes_tx_amp
, amd_xgbe_phy_serdes_tx_amp
,
1749 sizeof(priv
->serdes_tx_amp
));
1752 if (device_property_present(phy_dev
, XGBE_PHY_DFE_CFG_PROPERTY
)) {
1753 ret
= device_property_read_u32_array(phy_dev
,
1754 XGBE_PHY_DFE_CFG_PROPERTY
,
1755 priv
->serdes_dfe_tap_cfg
,
1758 dev_err(dev
, "invalid %s property\n",
1759 XGBE_PHY_DFE_CFG_PROPERTY
);
1763 memcpy(priv
->serdes_dfe_tap_cfg
,
1764 amd_xgbe_phy_serdes_dfe_tap_cfg
,
1765 sizeof(priv
->serdes_dfe_tap_cfg
));
1768 if (device_property_present(phy_dev
, XGBE_PHY_DFE_ENA_PROPERTY
)) {
1769 ret
= device_property_read_u32_array(phy_dev
,
1770 XGBE_PHY_DFE_ENA_PROPERTY
,
1771 priv
->serdes_dfe_tap_ena
,
1774 dev_err(dev
, "invalid %s property\n",
1775 XGBE_PHY_DFE_ENA_PROPERTY
);
1779 memcpy(priv
->serdes_dfe_tap_ena
,
1780 amd_xgbe_phy_serdes_dfe_tap_ena
,
1781 sizeof(priv
->serdes_dfe_tap_ena
));
1784 /* Initialize supported features */
1785 phydev
->supported
= SUPPORTED_Autoneg
;
1786 phydev
->supported
|= SUPPORTED_Pause
| SUPPORTED_Asym_Pause
;
1787 phydev
->supported
|= SUPPORTED_Backplane
;
1788 phydev
->supported
|= SUPPORTED_10000baseKR_Full
;
1789 switch (priv
->speed_set
) {
1790 case AMD_XGBE_PHY_SPEEDSET_1000_10000
:
1791 phydev
->supported
|= SUPPORTED_1000baseKX_Full
;
1793 case AMD_XGBE_PHY_SPEEDSET_2500_10000
:
1794 phydev
->supported
|= SUPPORTED_2500baseX_Full
;
1798 ret
= phy_read_mmd(phydev
, MDIO_MMD_PMAPMD
, MDIO_PMA_10GBR_FEC_ABILITY
);
1801 priv
->fec_ability
= ret
& XGBE_PHY_FEC_MASK
;
1802 if (priv
->fec_ability
& XGBE_PHY_FEC_ENABLE
)
1803 phydev
->supported
|= SUPPORTED_10000baseR_FEC
;
1805 phydev
->advertising
= phydev
->supported
;
1807 phydev
->priv
= priv
;
1809 if (!priv
->adev
|| acpi_disabled
)
1810 platform_device_put(phy_pdev
);
1815 devm_iounmap(dev
, priv
->sir1_regs
);
1816 devm_release_mem_region(dev
, priv
->sir1_res
->start
,
1817 resource_size(priv
->sir1_res
));
1820 devm_iounmap(dev
, priv
->sir0_regs
);
1821 devm_release_mem_region(dev
, priv
->sir0_res
->start
,
1822 resource_size(priv
->sir0_res
));
1825 devm_iounmap(dev
, priv
->rxtx_regs
);
1826 devm_release_mem_region(dev
, priv
->rxtx_res
->start
,
1827 resource_size(priv
->rxtx_res
));
1830 if (!priv
->adev
|| acpi_disabled
)
1831 platform_device_put(phy_pdev
);
1834 devm_kfree(dev
, priv
);
1839 static void amd_xgbe_phy_remove(struct phy_device
*phydev
)
1841 struct amd_xgbe_phy_priv
*priv
= phydev
->priv
;
1842 struct device
*dev
= priv
->dev
;
1844 if (priv
->an_irq_allocated
) {
1845 devm_free_irq(dev
, priv
->an_irq
, priv
);
1847 flush_workqueue(priv
->an_workqueue
);
1848 destroy_workqueue(priv
->an_workqueue
);
1851 /* Release resources */
1852 devm_iounmap(dev
, priv
->sir1_regs
);
1853 devm_release_mem_region(dev
, priv
->sir1_res
->start
,
1854 resource_size(priv
->sir1_res
));
1856 devm_iounmap(dev
, priv
->sir0_regs
);
1857 devm_release_mem_region(dev
, priv
->sir0_res
->start
,
1858 resource_size(priv
->sir0_res
));
1860 devm_iounmap(dev
, priv
->rxtx_regs
);
1861 devm_release_mem_region(dev
, priv
->rxtx_res
->start
,
1862 resource_size(priv
->rxtx_res
));
1864 devm_kfree(dev
, priv
);
1867 static int amd_xgbe_match_phy_device(struct phy_device
*phydev
)
1869 return phydev
->c45_ids
.device_ids
[MDIO_MMD_PCS
] == XGBE_PHY_ID
;
1872 static struct phy_driver amd_xgbe_phy_driver
[] = {
1874 .phy_id
= XGBE_PHY_ID
,
1875 .phy_id_mask
= XGBE_PHY_MASK
,
1876 .name
= "AMD XGBE PHY",
1878 .flags
= PHY_IS_INTERNAL
,
1879 .probe
= amd_xgbe_phy_probe
,
1880 .remove
= amd_xgbe_phy_remove
,
1881 .soft_reset
= amd_xgbe_phy_soft_reset
,
1882 .config_init
= amd_xgbe_phy_config_init
,
1883 .suspend
= amd_xgbe_phy_suspend
,
1884 .resume
= amd_xgbe_phy_resume
,
1885 .config_aneg
= amd_xgbe_phy_config_aneg
,
1886 .aneg_done
= amd_xgbe_phy_aneg_done
,
1887 .read_status
= amd_xgbe_phy_read_status
,
1888 .match_phy_device
= amd_xgbe_match_phy_device
,
1890 .owner
= THIS_MODULE
,
1895 module_phy_driver(amd_xgbe_phy_driver
);
1897 static struct mdio_device_id __maybe_unused amd_xgbe_phy_ids
[] = {
1898 { XGBE_PHY_ID
, XGBE_PHY_MASK
},
1901 MODULE_DEVICE_TABLE(mdio
, amd_xgbe_phy_ids
);