1 From be07319b9897738a4ab1501880b7dd9be26eba66 Mon Sep 17 00:00:00 2001
2 From: Yangbo Lu <yangbo.lu@nxp.com>
3 Date: Mon, 25 Sep 2017 11:54:28 +0800
4 Subject: [PATCH] phy: support layerscape
6 This is a integrated patch for layerscape mdio-phy support.
8 Signed-off-by: Bogdan Purcareata <bogdan.purcareata@nxp.com>
9 Signed-off-by: Zhang Ying-22455 <ying.zhang22455@nxp.com>
10 Signed-off-by: costi <constantin.tudor@freescale.com>
11 Signed-off-by: Madalin Bucur <madalin.bucur@freescale.com>
12 Signed-off-by: Shaohui Xie <Shaohui.Xie@nxp.com>
13 Signed-off-by: Florian Fainelli <f.fainelli@gmail.com>
14 Signed-off-by: Yangbo Lu <yangbo.lu@nxp.com>
16 drivers/net/phy/Kconfig | 11 +
17 drivers/net/phy/Makefile | 2 +
18 drivers/net/phy/aquantia.c | 28 +
19 drivers/net/phy/cortina.c | 118 ++++
20 drivers/net/phy/fsl_backplane.c | 1358 +++++++++++++++++++++++++++++++++++++++
21 drivers/net/phy/phy.c | 23 +-
22 drivers/net/phy/phy_device.c | 6 +-
23 drivers/net/phy/swphy.c | 1 +
24 include/linux/phy.h | 4 +
25 9 files changed, 1544 insertions(+), 7 deletions(-)
26 create mode 100644 drivers/net/phy/cortina.c
27 create mode 100644 drivers/net/phy/fsl_backplane.c
29 diff --git a/drivers/net/phy/Kconfig b/drivers/net/phy/Kconfig
30 index 30a3a2f5..3521c1ac 100644
31 --- a/drivers/net/phy/Kconfig
32 +++ b/drivers/net/phy/Kconfig
33 @@ -89,6 +89,12 @@ config MDIO_BUS_MUX_MMIOREG
37 +config MDIO_FSL_BACKPLANE
38 + tristate "Support for backplane on Freescale XFI interface"
41 + This module provides a driver for Freescale XFI's backplane.
44 tristate "GPIO lib-based bitbanged MDIO buses"
45 depends on MDIO_BITBANG && GPIOLIB
46 @@ -298,6 +304,11 @@ config CICADA_PHY
48 Currently supports the cis8204
51 + tristate "Cortina EDC CDR 10G Ethernet PHY"
53 + Currently supports the CS4340 phy.
56 tristate "Davicom PHYs"
58 diff --git a/drivers/net/phy/Makefile b/drivers/net/phy/Makefile
59 index 93a68fcd..ef3ec265 100644
60 --- a/drivers/net/phy/Makefile
61 +++ b/drivers/net/phy/Makefile
62 @@ -30,6 +30,7 @@ obj-$(CONFIG_MDIO_BUS_MUX_BCM_IPROC) += mdio-mux-bcm-iproc.o
63 obj-$(CONFIG_MDIO_BUS_MUX_GPIO) += mdio-mux-gpio.o
64 obj-$(CONFIG_MDIO_BUS_MUX_MMIOREG) += mdio-mux-mmioreg.o
65 obj-$(CONFIG_MDIO_CAVIUM) += mdio-cavium.o
66 +obj-$(CONFIG_MDIO_FSL_BACKPLANE) += fsl_backplane.o
67 obj-$(CONFIG_MDIO_GPIO) += mdio-gpio.o
68 obj-$(CONFIG_MDIO_HISI_FEMAC) += mdio-hisi-femac.o
69 obj-$(CONFIG_MDIO_MOXART) += mdio-moxart.o
70 @@ -48,6 +49,7 @@ obj-$(CONFIG_BCM_CYGNUS_PHY) += bcm-cygnus.o
71 obj-$(CONFIG_BCM_NET_PHYLIB) += bcm-phy-lib.o
72 obj-$(CONFIG_BROADCOM_PHY) += broadcom.o
73 obj-$(CONFIG_CICADA_PHY) += cicada.o
74 +obj-$(CONFIG_CORTINA_PHY) += cortina.o
75 obj-$(CONFIG_DAVICOM_PHY) += davicom.o
76 obj-$(CONFIG_DP83640_PHY) += dp83640.o
77 obj-$(CONFIG_DP83848_PHY) += dp83848.o
78 diff --git a/drivers/net/phy/aquantia.c b/drivers/net/phy/aquantia.c
79 index 09b0b0aa..e8ae50e1 100644
80 --- a/drivers/net/phy/aquantia.c
81 +++ b/drivers/net/phy/aquantia.c
83 #define PHY_ID_AQ1202 0x03a1b445
84 #define PHY_ID_AQ2104 0x03a1b460
85 #define PHY_ID_AQR105 0x03a1b4a2
86 +#define PHY_ID_AQR106 0x03a1b4d0
87 +#define PHY_ID_AQR107 0x03a1b4e0
88 #define PHY_ID_AQR405 0x03a1b4b0
90 #define PHY_AQUANTIA_FEATURES (SUPPORTED_10000baseT_Full | \
91 @@ -153,6 +155,30 @@ static struct phy_driver aquantia_driver[] = {
92 .ack_interrupt = aquantia_ack_interrupt,
93 .read_status = aquantia_read_status,
96 + .phy_id = PHY_ID_AQR106,
97 + .phy_id_mask = 0xfffffff0,
98 + .name = "Aquantia AQR106",
99 + .features = PHY_AQUANTIA_FEATURES,
100 + .flags = PHY_HAS_INTERRUPT,
101 + .aneg_done = aquantia_aneg_done,
102 + .config_aneg = aquantia_config_aneg,
103 + .config_intr = aquantia_config_intr,
104 + .ack_interrupt = aquantia_ack_interrupt,
105 + .read_status = aquantia_read_status,
108 + .phy_id = PHY_ID_AQR107,
109 + .phy_id_mask = 0xfffffff0,
110 + .name = "Aquantia AQR107",
111 + .features = PHY_AQUANTIA_FEATURES,
112 + .flags = PHY_HAS_INTERRUPT,
113 + .aneg_done = aquantia_aneg_done,
114 + .config_aneg = aquantia_config_aneg,
115 + .config_intr = aquantia_config_intr,
116 + .ack_interrupt = aquantia_ack_interrupt,
117 + .read_status = aquantia_read_status,
120 .phy_id = PHY_ID_AQR405,
121 .phy_id_mask = 0xfffffff0,
122 @@ -173,6 +199,8 @@ static struct mdio_device_id __maybe_unused aquantia_tbl[] = {
123 { PHY_ID_AQ1202, 0xfffffff0 },
124 { PHY_ID_AQ2104, 0xfffffff0 },
125 { PHY_ID_AQR105, 0xfffffff0 },
126 + { PHY_ID_AQR106, 0xfffffff0 },
127 + { PHY_ID_AQR107, 0xfffffff0 },
128 { PHY_ID_AQR405, 0xfffffff0 },
131 diff --git a/drivers/net/phy/cortina.c b/drivers/net/phy/cortina.c
133 index 00000000..72f4228a
135 +++ b/drivers/net/phy/cortina.c
138 + * Copyright 2017 NXP
140 + * This program is free software; you can redistribute it and/or modify
141 + * it under the terms of the GNU General Public License as published by
142 + * the Free Software Foundation; either version 2 of the License, or
143 + * (at your option) any later version.
145 + * This program is distributed in the hope that it will be useful,
146 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
147 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
148 + * GNU General Public License for more details.
150 + * CORTINA is a registered trademark of Cortina Systems, Inc.
153 +#include <linux/module.h>
154 +#include <linux/phy.h>
156 +#define PHY_ID_CS4340 0x13e51002
158 +#define VILLA_GLOBAL_CHIP_ID_LSB 0x0
159 +#define VILLA_GLOBAL_CHIP_ID_MSB 0x1
161 +#define VILLA_GLOBAL_GPIO_1_INTS 0x017
163 +static int cortina_read_reg(struct phy_device *phydev, u16 regnum)
165 + return mdiobus_read(phydev->mdio.bus, phydev->mdio.addr,
166 + MII_ADDR_C45 | regnum);
169 +static int cortina_config_aneg(struct phy_device *phydev)
171 + phydev->supported = SUPPORTED_10000baseT_Full;
172 + phydev->advertising = SUPPORTED_10000baseT_Full;
177 +static int cortina_read_status(struct phy_device *phydev)
179 + int gpio_int_status, ret = 0;
181 + gpio_int_status = cortina_read_reg(phydev, VILLA_GLOBAL_GPIO_1_INTS);
182 + if (gpio_int_status < 0) {
183 + ret = gpio_int_status;
187 + if (gpio_int_status & 0x8) {
188 + /* up when edc_convergedS set */
189 + phydev->speed = SPEED_10000;
190 + phydev->duplex = DUPLEX_FULL;
200 +static int cortina_soft_reset(struct phy_device *phydev)
205 +static int cortina_probe(struct phy_device *phydev)
208 + int id_lsb = 0, id_msb = 0;
210 + /* Read device id from phy registers. */
211 + id_lsb = cortina_read_reg(phydev, VILLA_GLOBAL_CHIP_ID_LSB);
215 + phy_id = id_lsb << 16;
217 + id_msb = cortina_read_reg(phydev, VILLA_GLOBAL_CHIP_ID_MSB);
223 + /* Make sure the device tree binding matched the driver with the
226 + if (phy_id != phydev->drv->phy_id) {
227 + phydev_err(phydev, "Error matching phy with %s driver\n",
228 + phydev->drv->name);
235 +static struct phy_driver cortina_driver[] = {
237 + .phy_id = PHY_ID_CS4340,
238 + .phy_id_mask = 0xffffffff,
239 + .name = "Cortina CS4340",
240 + .config_aneg = cortina_config_aneg,
241 + .read_status = cortina_read_status,
242 + .soft_reset = cortina_soft_reset,
243 + .probe = cortina_probe,
247 +module_phy_driver(cortina_driver);
249 +static struct mdio_device_id __maybe_unused cortina_tbl[] = {
250 + { PHY_ID_CS4340, 0xffffffff},
254 +MODULE_DEVICE_TABLE(mdio, cortina_tbl);
255 diff --git a/drivers/net/phy/fsl_backplane.c b/drivers/net/phy/fsl_backplane.c
257 index 00000000..76865261
259 +++ b/drivers/net/phy/fsl_backplane.c
261 +/* Freescale backplane driver.
262 + * Author: Shaohui Xie <Shaohui.Xie@freescale.com>
264 + * Copyright 2015 Freescale Semiconductor, Inc.
266 + * Licensed under the GPL-2 or later.
269 +#include <linux/kernel.h>
270 +#include <linux/module.h>
271 +#include <linux/mii.h>
272 +#include <linux/mdio.h>
273 +#include <linux/ethtool.h>
274 +#include <linux/phy.h>
275 +#include <linux/io.h>
276 +#include <linux/of.h>
277 +#include <linux/of_net.h>
278 +#include <linux/of_address.h>
279 +#include <linux/of_platform.h>
280 +#include <linux/timer.h>
281 +#include <linux/delay.h>
282 +#include <linux/workqueue.h>
284 +/* XFI PCS Device Identifier */
285 +#define FSL_PCS_PHY_ID 0x0083e400
287 +/* Freescale KR PMD registers */
288 +#define FSL_KR_PMD_CTRL 0x96
289 +#define FSL_KR_PMD_STATUS 0x97
290 +#define FSL_KR_LP_CU 0x98
291 +#define FSL_KR_LP_STATUS 0x99
292 +#define FSL_KR_LD_CU 0x9a
293 +#define FSL_KR_LD_STATUS 0x9b
295 +/* Freescale KR PMD defines */
296 +#define PMD_RESET 0x1
297 +#define PMD_STATUS_SUP_STAT 0x4
298 +#define PMD_STATUS_FRAME_LOCK 0x2
299 +#define TRAIN_EN 0x3
300 +#define TRAIN_DISABLE 0x1
303 +#define FSL_KR_RX_LINK_STAT_MASK 0x1000
304 +#define FSL_XFI_PCS_10GR_SR1 0x20
306 +/* Freescale KX PCS mode register */
307 +#define FSL_PCS_IF_MODE 0x8014
309 +/* Freescale KX PCS mode register init value */
310 +#define IF_MODE_INIT 0x8
312 +/* Freescale KX/KR AN registers */
313 +#define FSL_AN_AD1 0x11
314 +#define FSL_AN_BP_STAT 0x30
316 +/* Freescale KX/KR AN registers defines */
317 +#define AN_CTRL_INIT 0x1200
318 +#define KX_AN_AD1_INIT 0x25
319 +#define KR_AN_AD1_INIT 0x85
320 +#define AN_LNK_UP_MASK 0x4
321 +#define KR_AN_MASK 0x8
322 +#define TRAIN_FAIL 0x8
328 +#define BIN_M1_SEL 6
329 +#define BIN_Long_SEL 7
330 +#define CDR_SEL_MASK 0x00070000
331 +#define BIN_SNAPSHOT_NUM 5
332 +#define BIN_M1_THRESHOLD 3
333 +#define BIN_LONG_THRESHOLD 2
335 +#define PRE_COE_SHIFT 22
336 +#define POST_COE_SHIFT 16
337 +#define ZERO_COE_SHIFT 8
339 +#define PRE_COE_MAX 0x0
340 +#define PRE_COE_MIN 0x8
341 +#define POST_COE_MAX 0x0
342 +#define POST_COE_MIN 0x10
343 +#define ZERO_COE_MAX 0x30
344 +#define ZERO_COE_MIN 0x0
346 +#define TECR0_INIT 0x24200000
347 +#define RATIO_PREQ 0x3
348 +#define RATIO_PST1Q 0xd
349 +#define RATIO_EQ 0x20
351 +#define GCR0_RESET_MASK 0x600000
352 +#define GCR1_SNP_START_MASK 0x00000040
353 +#define GCR1_CTL_SNP_START_MASK 0x00002000
354 +#define GCR1_REIDL_TH_MASK 0x00700000
355 +#define GCR1_REIDL_EX_SEL_MASK 0x000c0000
356 +#define GCR1_REIDL_ET_MAS_MASK 0x00004000
357 +#define TECR0_AMP_RED_MASK 0x0000003f
359 +#define RECR1_CTL_SNP_DONE_MASK 0x00000002
360 +#define RECR1_SNP_DONE_MASK 0x00000004
361 +#define TCSR1_SNP_DATA_MASK 0x0000ffc0
362 +#define TCSR1_SNP_DATA_SHIFT 6
363 +#define TCSR1_EQ_SNPBIN_SIGN_MASK 0x100
365 +#define RECR1_GAINK2_MASK 0x0f000000
366 +#define RECR1_GAINK2_SHIFT 24
367 +#define RECR1_GAINK3_MASK 0x000f0000
368 +#define RECR1_GAINK3_SHIFT 16
369 +#define RECR1_OFFSET_MASK 0x00003f80
370 +#define RECR1_OFFSET_SHIFT 7
371 +#define RECR1_BLW_MASK 0x00000f80
372 +#define RECR1_BLW_SHIFT 7
373 +#define EYE_CTRL_SHIFT 12
374 +#define BASE_WAND_SHIFT 10
376 +#define XGKR_TIMEOUT 1050
380 +#define TIMEOUT_LONG 3
381 +#define TIMEOUT_M1 3
383 +#define RX_READY_MASK 0x8000
384 +#define PRESET_MASK 0x2000
385 +#define INIT_MASK 0x1000
386 +#define COP1_MASK 0x30
387 +#define COP1_SHIFT 4
388 +#define COZ_MASK 0xc
390 +#define COM1_MASK 0x3
391 +#define COM1_SHIFT 0
392 +#define REQUEST_MASK 0x3f
393 +#define LD_ALL_MASK (PRESET_MASK | INIT_MASK | \
394 + COP1_MASK | COZ_MASK | COM1_MASK)
396 +#define NEW_ALGORITHM_TRAIN_TX
397 +#ifdef NEW_ALGORITHM_TRAIN_TX
398 +#define FORCE_INC_COP1_NUMBER 0
399 +#define FORCE_INC_COM1_NUMBER 1
402 +#define VAL_INVALID 0xff
404 +static const u32 preq_table[] = {0x0, 0x1, 0x3, 0x5,
405 + 0x7, 0x9, 0xb, 0xc, VAL_INVALID};
406 +static const u32 pst1q_table[] = {0x0, 0x1, 0x3, 0x5, 0x7,
407 + 0x9, 0xb, 0xd, 0xf, 0x10, VAL_INVALID};
409 +enum backplane_mode {
410 + PHY_BACKPLANE_1000BASE_KX,
411 + PHY_BACKPLANE_10GBASE_KR,
412 + PHY_BACKPLANE_INVAL
434 +struct per_lane_ctrl_status {
435 + __be32 gcr0; /* 0x.000 - General Control Register 0 */
436 + __be32 gcr1; /* 0x.004 - General Control Register 1 */
437 + __be32 gcr2; /* 0x.008 - General Control Register 2 */
438 + __be32 resv1; /* 0x.00C - Reserved */
439 + __be32 recr0; /* 0x.010 - Receive Equalization Control Register 0 */
440 + __be32 recr1; /* 0x.014 - Receive Equalization Control Register 1 */
441 + __be32 tecr0; /* 0x.018 - Transmit Equalization Control Register 0 */
442 + __be32 resv2; /* 0x.01C - Reserved */
443 + __be32 tlcr0; /* 0x.020 - TTL Control Register 0 */
444 + __be32 tlcr1; /* 0x.024 - TTL Control Register 1 */
445 + __be32 tlcr2; /* 0x.028 - TTL Control Register 2 */
446 + __be32 tlcr3; /* 0x.02C - TTL Control Register 3 */
447 + __be32 tcsr0; /* 0x.030 - Test Control/Status Register 0 */
448 + __be32 tcsr1; /* 0x.034 - Test Control/Status Register 1 */
449 + __be32 tcsr2; /* 0x.038 - Test Control/Status Register 2 */
450 + __be32 tcsr3; /* 0x.03C - Test Control/Status Register 3 */
453 +struct tx_condition {
454 + bool bin_m1_late_early;
455 + bool bin_long_late_early;
457 + bool bin_long_stop;
460 + int m1_min_max_cnt;
461 + int long_min_max_cnt;
462 +#ifdef NEW_ALGORITHM_TRAIN_TX
468 +struct fsl_xgkr_inst {
470 + struct phy_device *phydev;
471 + struct tx_condition tx_c;
472 + struct delayed_work xgkr_wk;
473 + enum train_state state;
481 +static void tx_condition_init(struct tx_condition *tx_c)
483 + tx_c->bin_m1_late_early = true;
484 + tx_c->bin_long_late_early = false;
485 + tx_c->bin_m1_stop = false;
486 + tx_c->bin_long_stop = false;
487 + tx_c->tx_complete = false;
488 + tx_c->sent_init = false;
489 + tx_c->m1_min_max_cnt = 0;
490 + tx_c->long_min_max_cnt = 0;
491 +#ifdef NEW_ALGORITHM_TRAIN_TX
492 + tx_c->pre_inc = FORCE_INC_COM1_NUMBER;
493 + tx_c->post_inc = FORCE_INC_COP1_NUMBER;
497 +void tune_tecr0(struct fsl_xgkr_inst *inst)
499 + struct per_lane_ctrl_status *reg_base = inst->reg_base;
503 + inst->adpt_eq << ZERO_COE_SHIFT |
504 + inst->ratio_preq << PRE_COE_SHIFT |
505 + inst->ratio_pst1q << POST_COE_SHIFT;
507 + /* reset the lane */
508 + iowrite32(ioread32(®_base->gcr0) & ~GCR0_RESET_MASK,
511 + iowrite32(val, ®_base->tecr0);
513 + /* unreset the lane */
514 + iowrite32(ioread32(®_base->gcr0) | GCR0_RESET_MASK,
519 +static void start_lt(struct phy_device *phydev)
521 + phy_write_mmd(phydev, MDIO_MMD_PMAPMD, FSL_KR_PMD_CTRL, TRAIN_EN);
524 +static void stop_lt(struct phy_device *phydev)
526 + phy_write_mmd(phydev, MDIO_MMD_PMAPMD, FSL_KR_PMD_CTRL, TRAIN_DISABLE);
529 +static void reset_gcr0(struct fsl_xgkr_inst *inst)
531 + struct per_lane_ctrl_status *reg_base = inst->reg_base;
533 + iowrite32(ioread32(®_base->gcr0) & ~GCR0_RESET_MASK,
536 + iowrite32(ioread32(®_base->gcr0) | GCR0_RESET_MASK,
541 +void lane_set_1gkx(void *reg)
543 + struct per_lane_ctrl_status *reg_base = reg;
546 + /* reset the lane */
547 + iowrite32(ioread32(®_base->gcr0) & ~GCR0_RESET_MASK,
551 + /* set gcr1 for 1GKX */
552 + val = ioread32(®_base->gcr1);
553 + val &= ~(GCR1_REIDL_TH_MASK | GCR1_REIDL_EX_SEL_MASK |
554 + GCR1_REIDL_ET_MAS_MASK);
555 + iowrite32(val, ®_base->gcr1);
558 + /* set tecr0 for 1GKX */
559 + val = ioread32(®_base->tecr0);
560 + val &= ~TECR0_AMP_RED_MASK;
561 + iowrite32(val, ®_base->tecr0);
564 + /* unreset the lane */
565 + iowrite32(ioread32(®_base->gcr0) | GCR0_RESET_MASK,
570 +static void reset_lt(struct phy_device *phydev)
572 + phy_write_mmd(phydev, MDIO_MMD_PMAPMD, MDIO_CTRL1, PMD_RESET);
573 + phy_write_mmd(phydev, MDIO_MMD_PMAPMD, FSL_KR_PMD_CTRL, TRAIN_DISABLE);
574 + phy_write_mmd(phydev, MDIO_MMD_PMAPMD, FSL_KR_LD_CU, 0);
575 + phy_write_mmd(phydev, MDIO_MMD_PMAPMD, FSL_KR_LD_STATUS, 0);
576 + phy_write_mmd(phydev, MDIO_MMD_PMAPMD, FSL_KR_PMD_STATUS, 0);
577 + phy_write_mmd(phydev, MDIO_MMD_PMAPMD, FSL_KR_LP_CU, 0);
578 + phy_write_mmd(phydev, MDIO_MMD_PMAPMD, FSL_KR_LP_STATUS, 0);
581 +static void start_xgkr_state_machine(struct delayed_work *work)
583 + queue_delayed_work(system_power_efficient_wq, work,
584 + msecs_to_jiffies(XGKR_TIMEOUT));
587 +static void start_xgkr_an(struct phy_device *phydev)
589 + struct fsl_xgkr_inst *inst;
592 + phy_write_mmd(phydev, MDIO_MMD_AN, FSL_AN_AD1, KR_AN_AD1_INIT);
593 + phy_write_mmd(phydev, MDIO_MMD_AN, MDIO_CTRL1, AN_CTRL_INIT);
595 + inst = phydev->priv;
597 + /* start state machine*/
598 + start_xgkr_state_machine(&inst->xgkr_wk);
601 +static void start_1gkx_an(struct phy_device *phydev)
603 + phy_write_mmd(phydev, MDIO_MMD_PCS, FSL_PCS_IF_MODE, IF_MODE_INIT);
604 + phy_write_mmd(phydev, MDIO_MMD_AN, FSL_AN_AD1, KX_AN_AD1_INIT);
605 + phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_STAT1);
606 + phy_write_mmd(phydev, MDIO_MMD_AN, MDIO_CTRL1, AN_CTRL_INIT);
609 +static void ld_coe_status(struct fsl_xgkr_inst *inst)
611 + phy_write_mmd(inst->phydev, MDIO_MMD_PMAPMD,
612 + FSL_KR_LD_STATUS, inst->ld_status);
615 +static void ld_coe_update(struct fsl_xgkr_inst *inst)
617 + dev_dbg(&inst->phydev->mdio.dev, "sending request: %x\n", inst->ld_update);
618 + phy_write_mmd(inst->phydev, MDIO_MMD_PMAPMD,
619 + FSL_KR_LD_CU, inst->ld_update);
622 +static void init_inst(struct fsl_xgkr_inst *inst, int reset)
625 + inst->ratio_preq = RATIO_PREQ;
626 + inst->ratio_pst1q = RATIO_PST1Q;
627 + inst->adpt_eq = RATIO_EQ;
631 + tx_condition_init(&inst->tx_c);
632 + inst->state = DETECTING_LP;
633 + inst->ld_status &= RX_READY_MASK;
634 + ld_coe_status(inst);
635 + inst->ld_update = 0;
636 + inst->ld_status &= ~RX_READY_MASK;
637 + ld_coe_status(inst);
640 +#ifdef NEW_ALGORITHM_TRAIN_TX
641 +static int get_median_gaink2(u32 *reg)
643 + int gaink2_snap_shot[BIN_SNAPSHOT_NUM];
645 + struct per_lane_ctrl_status *reg_base;
647 + int i, j, tmp, pos;
649 + reg_base = (struct per_lane_ctrl_status *)reg;
651 + for (i = 0; i < BIN_SNAPSHOT_NUM; i++) {
652 + /* wait RECR1_CTL_SNP_DONE_MASK has cleared */
654 + while (ioread32(®_base->recr1) &
655 + RECR1_CTL_SNP_DONE_MASK) {
662 + /* start snap shot */
663 + iowrite32((ioread32(®_base->gcr1) |
664 + GCR1_CTL_SNP_START_MASK),
667 + /* wait for SNP done */
669 + while (!(ioread32(®_base->recr1) &
670 + RECR1_CTL_SNP_DONE_MASK)) {
677 + /* read and save the snap shot */
678 + rx_eq_snp = ioread32(®_base->recr1);
679 + gaink2_snap_shot[i] = (rx_eq_snp & RECR1_GAINK2_MASK) >>
680 + RECR1_GAINK2_SHIFT;
682 + /* terminate the snap shot by setting GCR1[REQ_CTL_SNP] */
683 + iowrite32((ioread32(®_base->gcr1) &
684 + ~GCR1_CTL_SNP_START_MASK),
688 + /* get median of the 5 snap shot */
689 + for (i = 0; i < BIN_SNAPSHOT_NUM - 1; i++) {
690 + tmp = gaink2_snap_shot[i];
692 + for (j = i + 1; j < BIN_SNAPSHOT_NUM; j++) {
693 + if (gaink2_snap_shot[j] < tmp) {
694 + tmp = gaink2_snap_shot[j];
699 + gaink2_snap_shot[pos] = gaink2_snap_shot[i];
700 + gaink2_snap_shot[i] = tmp;
703 + return gaink2_snap_shot[2];
707 +static bool is_bin_early(int bin_sel, void *reg)
709 + bool early = false;
710 + int bin_snap_shot[BIN_SNAPSHOT_NUM];
711 + int i, negative_count = 0;
712 + struct per_lane_ctrl_status *reg_base = reg;
715 + for (i = 0; i < BIN_SNAPSHOT_NUM; i++) {
716 + /* wait RECR1_SNP_DONE_MASK has cleared */
718 + while ((ioread32(®_base->recr1) & RECR1_SNP_DONE_MASK)) {
725 + /* set TCSR1[CDR_SEL] to BinM1/BinLong */
726 + if (bin_sel == BIN_M1) {
727 + iowrite32((ioread32(®_base->tcsr1) &
728 + ~CDR_SEL_MASK) | BIN_M1_SEL,
731 + iowrite32((ioread32(®_base->tcsr1) &
732 + ~CDR_SEL_MASK) | BIN_Long_SEL,
736 + /* start snap shot */
737 + iowrite32(ioread32(®_base->gcr1) | GCR1_SNP_START_MASK,
740 + /* wait for SNP done */
742 + while (!(ioread32(®_base->recr1) & RECR1_SNP_DONE_MASK)) {
749 + /* read and save the snap shot */
750 + bin_snap_shot[i] = (ioread32(®_base->tcsr1) &
751 + TCSR1_SNP_DATA_MASK) >> TCSR1_SNP_DATA_SHIFT;
752 + if (bin_snap_shot[i] & TCSR1_EQ_SNPBIN_SIGN_MASK)
755 + /* terminate the snap shot by setting GCR1[REQ_CTL_SNP] */
756 + iowrite32(ioread32(®_base->gcr1) & ~GCR1_SNP_START_MASK,
760 + if (((bin_sel == BIN_M1) && (negative_count > BIN_M1_THRESHOLD)) ||
761 + ((bin_sel == BIN_LONG && (negative_count > BIN_LONG_THRESHOLD)))) {
768 +static void train_tx(struct fsl_xgkr_inst *inst)
770 + struct phy_device *phydev = inst->phydev;
771 + struct tx_condition *tx_c = &inst->tx_c;
772 + bool bin_m1_early, bin_long_early;
773 + u32 lp_status, old_ld_update;
774 + u32 status_cop1, status_coz, status_com1;
775 + u32 req_cop1, req_coz, req_com1, req_preset, req_init;
777 +#ifdef NEW_ALGORITHM_TRAIN_TX
782 + if (tx_c->bin_long_stop && tx_c->bin_m1_stop) {
783 + tx_c->tx_complete = true;
784 + inst->ld_status |= RX_READY_MASK;
785 + ld_coe_status(inst);
786 + /* tell LP we are ready */
787 + phy_write_mmd(phydev, MDIO_MMD_PMAPMD,
788 + FSL_KR_PMD_STATUS, RX_STAT);
792 + /* We start by checking the current LP status. If we got any responses,
793 + * we can clear up the appropriate update request so that the
794 + * subsequent code may easily issue new update requests if needed.
796 + lp_status = phy_read_mmd(phydev, MDIO_MMD_PMAPMD, FSL_KR_LP_STATUS) &
798 + status_cop1 = (lp_status & COP1_MASK) >> COP1_SHIFT;
799 + status_coz = (lp_status & COZ_MASK) >> COZ_SHIFT;
800 + status_com1 = (lp_status & COM1_MASK) >> COM1_SHIFT;
802 + old_ld_update = inst->ld_update;
803 + req_cop1 = (old_ld_update & COP1_MASK) >> COP1_SHIFT;
804 + req_coz = (old_ld_update & COZ_MASK) >> COZ_SHIFT;
805 + req_com1 = (old_ld_update & COM1_MASK) >> COM1_SHIFT;
806 + req_preset = old_ld_update & PRESET_MASK;
807 + req_init = old_ld_update & INIT_MASK;
809 + /* IEEE802.3-2008, 72.6.10.2.3.1
810 + * We may clear PRESET when all coefficients show UPDATED or MAX.
813 + if ((status_cop1 == COE_UPDATED || status_cop1 == COE_MAX) &&
814 + (status_coz == COE_UPDATED || status_coz == COE_MAX) &&
815 + (status_com1 == COE_UPDATED || status_com1 == COE_MAX)) {
816 + inst->ld_update &= ~PRESET_MASK;
820 + /* IEEE802.3-2008, 72.6.10.2.3.2
821 + * We may clear INITIALIZE when no coefficients show NOT UPDATED.
824 + if (status_cop1 != COE_NOTUPDATED &&
825 + status_coz != COE_NOTUPDATED &&
826 + status_com1 != COE_NOTUPDATED) {
827 + inst->ld_update &= ~INIT_MASK;
831 + /* IEEE802.3-2008, 72.6.10.2.3.2
832 + * we send initialize to the other side to ensure default settings
833 + * for the LP. Naturally, we should do this only once.
835 + if (!tx_c->sent_init) {
836 + if (!lp_status && !(old_ld_update & (LD_ALL_MASK))) {
837 + inst->ld_update = INIT_MASK;
838 + tx_c->sent_init = true;
842 + /* IEEE802.3-2008, 72.6.10.2.3.3
843 + * We set coefficient requests to HOLD when we get the information
844 + * about any updates On clearing our prior response, we also update
845 + * our internal status.
847 + if (status_cop1 != COE_NOTUPDATED) {
849 + inst->ld_update &= ~COP1_MASK;
850 +#ifdef NEW_ALGORITHM_TRAIN_TX
851 + if (tx_c->post_inc) {
852 + if (req_cop1 == INCREMENT &&
853 + status_cop1 == COE_MAX) {
854 + tx_c->post_inc = 0;
855 + tx_c->bin_long_stop = true;
856 + tx_c->bin_m1_stop = true;
858 + tx_c->post_inc -= 1;
861 + ld_coe_update(inst);
865 + if ((req_cop1 == DECREMENT && status_cop1 == COE_MIN) ||
866 + (req_cop1 == INCREMENT && status_cop1 == COE_MAX)) {
867 + dev_dbg(&inst->phydev->mdio.dev, "COP1 hit limit %s",
868 + (status_cop1 == COE_MIN) ?
869 + "DEC MIN" : "INC MAX");
870 + tx_c->long_min_max_cnt++;
871 + if (tx_c->long_min_max_cnt >= TIMEOUT_LONG) {
872 + tx_c->bin_long_stop = true;
873 + ld_coe_update(inst);
880 + if (status_coz != COE_NOTUPDATED) {
882 + inst->ld_update &= ~COZ_MASK;
885 + if (status_com1 != COE_NOTUPDATED) {
887 + inst->ld_update &= ~COM1_MASK;
888 +#ifdef NEW_ALGORITHM_TRAIN_TX
889 + if (tx_c->pre_inc) {
890 + if (req_com1 == INCREMENT &&
891 + status_com1 == COE_MAX)
894 + tx_c->pre_inc -= 1;
896 + ld_coe_update(inst);
900 + /* Stop If we have reached the limit for a parameter. */
901 + if ((req_com1 == DECREMENT && status_com1 == COE_MIN) ||
902 + (req_com1 == INCREMENT && status_com1 == COE_MAX)) {
903 + dev_dbg(&inst->phydev->mdio.dev, "COM1 hit limit %s",
904 + (status_com1 == COE_MIN) ?
905 + "DEC MIN" : "INC MAX");
906 + tx_c->m1_min_max_cnt++;
907 + if (tx_c->m1_min_max_cnt >= TIMEOUT_M1) {
908 + tx_c->bin_m1_stop = true;
909 + ld_coe_update(inst);
916 + if (old_ld_update != inst->ld_update) {
917 + ld_coe_update(inst);
918 + /* Redo these status checks and updates until we have no more
919 + * changes, to speed up the overall process.
924 + /* Do nothing if we have pending request. */
925 + if ((req_coz || req_com1 || req_cop1))
927 + else if (lp_status)
928 + /* No pending request but LP status was not reverted to
933 +#ifdef NEW_ALGORITHM_TRAIN_TX
934 + if (!(inst->ld_update & (PRESET_MASK | INIT_MASK))) {
935 + if (tx_c->pre_inc) {
936 + inst->ld_update = INCREMENT << COM1_SHIFT;
937 + ld_coe_update(inst);
941 + if (status_cop1 != COE_MAX) {
942 + median_gaink2 = get_median_gaink2(inst->reg_base);
943 + if (median_gaink2 == 0xf) {
944 + tx_c->post_inc = 1;
946 + /* Gaink2 median lower than "F" */
947 + tx_c->bin_m1_stop = true;
948 + tx_c->bin_long_stop = true;
953 + tx_c->bin_m1_stop = true;
954 + tx_c->bin_long_stop = true;
958 + if (tx_c->post_inc) {
959 + inst->ld_update = INCREMENT << COP1_SHIFT;
960 + ld_coe_update(inst);
966 + /* snapshot and select bin */
967 + bin_m1_early = is_bin_early(BIN_M1, inst->reg_base);
968 + bin_long_early = is_bin_early(BIN_LONG, inst->reg_base);
970 + if (!tx_c->bin_m1_stop && !tx_c->bin_m1_late_early && bin_m1_early) {
971 + tx_c->bin_m1_stop = true;
975 + if (!tx_c->bin_long_stop &&
976 + tx_c->bin_long_late_early && !bin_long_early) {
977 + tx_c->bin_long_stop = true;
981 + /* IEEE802.3-2008, 72.6.10.2.3.3
982 + * We only request coefficient updates when no PRESET/INITIALIZE is
983 + * pending. We also only request coefficient updates when the
984 + * corresponding status is NOT UPDATED and nothing is pending.
986 + if (!(inst->ld_update & (PRESET_MASK | INIT_MASK))) {
987 + if (!tx_c->bin_long_stop) {
988 + /* BinM1 correction means changing COM1 */
989 + if (!status_com1 && !(inst->ld_update & COM1_MASK)) {
990 + /* Avoid BinM1Late by requesting an
991 + * immediate decrement.
993 + if (!bin_m1_early) {
994 + /* request decrement c(-1) */
995 + temp = DECREMENT << COM1_SHIFT;
996 + inst->ld_update = temp;
997 + ld_coe_update(inst);
998 + tx_c->bin_m1_late_early = bin_m1_early;
1003 + /* BinLong correction means changing COP1 */
1004 + if (!status_cop1 && !(inst->ld_update & COP1_MASK)) {
1005 + /* Locate BinLong transition point (if any)
1006 + * while avoiding BinM1Late.
1008 + if (bin_long_early) {
1009 + /* request increment c(1) */
1010 + temp = INCREMENT << COP1_SHIFT;
1011 + inst->ld_update = temp;
1013 + /* request decrement c(1) */
1014 + temp = DECREMENT << COP1_SHIFT;
1015 + inst->ld_update = temp;
1018 + ld_coe_update(inst);
1019 + tx_c->bin_long_late_early = bin_long_early;
1021 + /* We try to finish BinLong before we do BinM1 */
1025 + if (!tx_c->bin_m1_stop) {
1026 + /* BinM1 correction means changing COM1 */
1027 + if (!status_com1 && !(inst->ld_update & COM1_MASK)) {
1028 + /* Locate BinM1 transition point (if any) */
1029 + if (bin_m1_early) {
1030 + /* request increment c(-1) */
1031 + temp = INCREMENT << COM1_SHIFT;
1032 + inst->ld_update = temp;
1034 + /* request decrement c(-1) */
1035 + temp = DECREMENT << COM1_SHIFT;
1036 + inst->ld_update = temp;
1039 + ld_coe_update(inst);
1040 + tx_c->bin_m1_late_early = bin_m1_early;
1046 +static int is_link_up(struct phy_device *phydev)
1050 + phy_read_mmd(phydev, MDIO_MMD_PCS, FSL_XFI_PCS_10GR_SR1);
1051 + val = phy_read_mmd(phydev, MDIO_MMD_PCS, FSL_XFI_PCS_10GR_SR1);
1053 + return (val & FSL_KR_RX_LINK_STAT_MASK) ? 1 : 0;
1056 +static int is_link_training_fail(struct phy_device *phydev)
1059 + int timeout = 100;
1061 + val = phy_read_mmd(phydev, MDIO_MMD_PMAPMD, FSL_KR_PMD_STATUS);
1062 + if (!(val & TRAIN_FAIL) && (val & RX_STAT)) {
1063 + /* check LNK_STAT for sure */
1064 + while (timeout--) {
1065 + if (is_link_up(phydev))
1068 + usleep_range(100, 500);
1075 +static int check_rx(struct phy_device *phydev)
1077 + return phy_read_mmd(phydev, MDIO_MMD_PMAPMD, FSL_KR_LP_STATUS) &
1081 +/* Coefficient values have hardware restrictions */
1082 +static int is_ld_valid(struct fsl_xgkr_inst *inst)
1084 + u32 ratio_pst1q = inst->ratio_pst1q;
1085 + u32 adpt_eq = inst->adpt_eq;
1086 + u32 ratio_preq = inst->ratio_preq;
1088 + if ((ratio_pst1q + adpt_eq + ratio_preq) > 48)
1091 + if (((ratio_pst1q + adpt_eq + ratio_preq) * 4) >=
1092 + ((adpt_eq - ratio_pst1q - ratio_preq) * 17))
1095 + if (ratio_preq > ratio_pst1q)
1098 + if (ratio_preq > 8)
1104 + if (ratio_pst1q > 16)
1110 +static int is_value_allowed(const u32 *val_table, u32 val)
1114 + for (i = 0;; i++) {
1115 + if (*(val_table + i) == VAL_INVALID)
1117 + if (*(val_table + i) == val)
1122 +static int inc_dec(struct fsl_xgkr_inst *inst, int field, int request)
1124 + u32 ld_limit[3], ld_coe[3], step[3];
1126 + ld_coe[0] = inst->ratio_pst1q;
1127 + ld_coe[1] = inst->adpt_eq;
1128 + ld_coe[2] = inst->ratio_preq;
1130 + /* Information specific to the Freescale SerDes for 10GBase-KR:
1131 + * Incrementing C(+1) means *decrementing* RATIO_PST1Q
1132 + * Incrementing C(0) means incrementing ADPT_EQ
1133 + * Incrementing C(-1) means *decrementing* RATIO_PREQ
1139 + switch (request) {
1141 + ld_limit[0] = POST_COE_MAX;
1142 + ld_limit[1] = ZERO_COE_MAX;
1143 + ld_limit[2] = PRE_COE_MAX;
1144 + if (ld_coe[field] != ld_limit[field])
1145 + ld_coe[field] += step[field];
1151 + ld_limit[0] = POST_COE_MIN;
1152 + ld_limit[1] = ZERO_COE_MIN;
1153 + ld_limit[2] = PRE_COE_MIN;
1154 + if (ld_coe[field] != ld_limit[field])
1155 + ld_coe[field] -= step[field];
1164 + if (is_ld_valid(inst)) {
1165 + /* accept new ld */
1166 + inst->ratio_pst1q = ld_coe[0];
1167 + inst->adpt_eq = ld_coe[1];
1168 + inst->ratio_preq = ld_coe[2];
1169 + /* only some values for preq and pst1q can be used.
1170 + * for preq: 0x0, 0x1, 0x3, 0x5, 0x7, 0x9, 0xb, 0xc.
1171 + * for pst1q: 0x0, 0x1, 0x3, 0x5, 0x7, 0x9, 0xb, 0xd, 0xf, 0x10.
1173 + if (!is_value_allowed((const u32 *)&preq_table, ld_coe[2])) {
1174 + dev_dbg(&inst->phydev->mdio.dev,
1175 + "preq skipped value: %d\n", ld_coe[2]);
1179 + if (!is_value_allowed((const u32 *)&pst1q_table, ld_coe[0])) {
1180 + dev_dbg(&inst->phydev->mdio.dev,
1181 + "pst1q skipped value: %d\n", ld_coe[0]);
1187 + if (request == DECREMENT)
1190 + if (request == INCREMENT)
1198 +static void min_max_updated(struct fsl_xgkr_inst *inst, int field, int new_ld)
1200 + u32 ld_coe[] = {COE_UPDATED, COE_MIN, COE_MAX};
1206 + val = ld_coe[new_ld] << COP1_SHIFT;
1210 + val = ld_coe[new_ld] << COZ_SHIFT;
1214 + val = ld_coe[new_ld] << COM1_SHIFT;
1220 + inst->ld_status &= ~mask;
1221 + inst->ld_status |= val;
1224 +static void check_request(struct fsl_xgkr_inst *inst, int request)
1226 + int cop1_req, coz_req, com_req;
1227 + int old_status, new_ld_sta;
1229 + cop1_req = (request & COP1_MASK) >> COP1_SHIFT;
1230 + coz_req = (request & COZ_MASK) >> COZ_SHIFT;
1231 + com_req = (request & COM1_MASK) >> COM1_SHIFT;
1233 + /* IEEE802.3-2008, 72.6.10.2.5
1234 + * Ensure we only act on INCREMENT/DECREMENT when we are in NOT UPDATED
1236 + old_status = inst->ld_status;
1238 + if (cop1_req && !(inst->ld_status & COP1_MASK)) {
1239 + new_ld_sta = inc_dec(inst, COE_COP1, cop1_req);
1240 + min_max_updated(inst, COE_COP1, new_ld_sta);
1243 + if (coz_req && !(inst->ld_status & COZ_MASK)) {
1244 + new_ld_sta = inc_dec(inst, COE_COZ, coz_req);
1245 + min_max_updated(inst, COE_COZ, new_ld_sta);
1248 + if (com_req && !(inst->ld_status & COM1_MASK)) {
1249 + new_ld_sta = inc_dec(inst, COE_COM, com_req);
1250 + min_max_updated(inst, COE_COM, new_ld_sta);
1253 + if (old_status != inst->ld_status)
1254 + ld_coe_status(inst);
1257 +static void preset(struct fsl_xgkr_inst *inst)
1259 + /* These are all MAX values from the IEEE802.3 perspective. */
1260 + inst->ratio_pst1q = POST_COE_MAX;
1261 + inst->adpt_eq = ZERO_COE_MAX;
1262 + inst->ratio_preq = PRE_COE_MAX;
1265 + inst->ld_status &= ~(COP1_MASK | COZ_MASK | COM1_MASK);
1266 + inst->ld_status |= COE_MAX << COP1_SHIFT |
1267 + COE_MAX << COZ_SHIFT |
1268 + COE_MAX << COM1_SHIFT;
1269 + ld_coe_status(inst);
1272 +static void initialize(struct fsl_xgkr_inst *inst)
1274 + inst->ratio_preq = RATIO_PREQ;
1275 + inst->ratio_pst1q = RATIO_PST1Q;
1276 + inst->adpt_eq = RATIO_EQ;
1279 + inst->ld_status &= ~(COP1_MASK | COZ_MASK | COM1_MASK);
1280 + inst->ld_status |= COE_UPDATED << COP1_SHIFT |
1281 + COE_UPDATED << COZ_SHIFT |
1282 + COE_UPDATED << COM1_SHIFT;
1283 + ld_coe_status(inst);
1286 +static void train_rx(struct fsl_xgkr_inst *inst)
1288 + struct phy_device *phydev = inst->phydev;
1289 + int request, old_ld_status;
1291 + /* get request from LP */
1292 + request = phy_read_mmd(phydev, MDIO_MMD_PMAPMD, FSL_KR_LP_CU) &
1294 + old_ld_status = inst->ld_status;
1296 + /* IEEE802.3-2008, 72.6.10.2.5
1297 + * Ensure we always go to NOT UDPATED for status reporting in
1298 + * response to HOLD requests.
1299 + * IEEE802.3-2008, 72.6.10.2.3.1/2
1300 + * ... but only if PRESET/INITIALIZE are not active to ensure
1301 + * we keep status until they are released.
1303 + if (!(request & (PRESET_MASK | INIT_MASK))) {
1304 + if (!(request & COP1_MASK))
1305 + inst->ld_status &= ~COP1_MASK;
1307 + if (!(request & COZ_MASK))
1308 + inst->ld_status &= ~COZ_MASK;
1310 + if (!(request & COM1_MASK))
1311 + inst->ld_status &= ~COM1_MASK;
1313 + if (old_ld_status != inst->ld_status)
1314 + ld_coe_status(inst);
1317 + /* As soon as the LP shows ready, no need to do any more updates. */
1318 + if (check_rx(phydev)) {
1319 + /* LP receiver is ready */
1320 + if (inst->ld_status & (COP1_MASK | COZ_MASK | COM1_MASK)) {
1321 + inst->ld_status &= ~(COP1_MASK | COZ_MASK | COM1_MASK);
1322 + ld_coe_status(inst);
1325 + /* IEEE802.3-2008, 72.6.10.2.3.1/2
1326 + * only act on PRESET/INITIALIZE if all status is NOT UPDATED.
1328 + if (request & (PRESET_MASK | INIT_MASK)) {
1329 + if (!(inst->ld_status &
1330 + (COP1_MASK | COZ_MASK | COM1_MASK))) {
1331 + if (request & PRESET_MASK)
1334 + if (request & INIT_MASK)
1339 + /* LP Coefficient are not in HOLD */
1340 + if (request & REQUEST_MASK)
1341 + check_request(inst, request & REQUEST_MASK);
1345 +static void xgkr_start_train(struct phy_device *phydev)
1347 + struct fsl_xgkr_inst *inst = phydev->priv;
1348 + struct tx_condition *tx_c = &inst->tx_c;
1351 + unsigned long dead_line;
1354 + init_inst(inst, 0);
1357 + for (i = 0; i < 2;) {
1358 + dead_line = jiffies + msecs_to_jiffies(500);
1359 + while (time_before(jiffies, dead_line)) {
1360 + val = phy_read_mmd(phydev, MDIO_MMD_PMAPMD,
1361 + FSL_KR_PMD_STATUS);
1362 + if (val & TRAIN_FAIL) {
1363 + /* LT failed already, reset lane to avoid
1364 + * it run into hanging, then start LT again.
1368 + } else if ((val & PMD_STATUS_SUP_STAT) &&
1369 + (val & PMD_STATUS_FRAME_LOCK))
1371 + usleep_range(100, 500);
1374 + if (!((val & PMD_STATUS_FRAME_LOCK) &&
1375 + (val & PMD_STATUS_SUP_STAT))) {
1380 + /* init process */
1383 + /* the LT should be finished in 500ms, failed or OK. */
1384 + dead_line = jiffies + msecs_to_jiffies(500);
1386 + while (time_before(jiffies, dead_line)) {
1387 + /* check if the LT is already failed */
1388 + lt_state = phy_read_mmd(phydev, MDIO_MMD_PMAPMD,
1389 + FSL_KR_PMD_STATUS);
1390 + if (lt_state & TRAIN_FAIL) {
1395 + rx_ok = check_rx(phydev);
1396 + tx_ok = tx_c->tx_complete;
1398 + if (rx_ok && tx_ok)
1407 + usleep_range(100, 500);
1411 + /* check LT result */
1412 + if (is_link_training_fail(phydev)) {
1413 + init_inst(inst, 0);
1417 + inst->state = TRAINED;
1423 +static void xgkr_state_machine(struct work_struct *work)
1425 + struct delayed_work *dwork = to_delayed_work(work);
1426 + struct fsl_xgkr_inst *inst = container_of(dwork,
1427 + struct fsl_xgkr_inst,
1429 + struct phy_device *phydev = inst->phydev;
1431 + bool needs_train = false;
1433 + mutex_lock(&phydev->lock);
1435 + switch (inst->state) {
1436 + case DETECTING_LP:
1437 + phy_read_mmd(phydev, MDIO_MMD_AN, FSL_AN_BP_STAT);
1438 + an_state = phy_read_mmd(phydev, MDIO_MMD_AN, FSL_AN_BP_STAT);
1439 + if ((an_state & KR_AN_MASK))
1440 + needs_train = true;
1443 + if (!is_link_up(phydev)) {
1444 + dev_info(&phydev->mdio.dev,
1445 + "Detect hotplug, restart training\n");
1446 + init_inst(inst, 1);
1447 + start_xgkr_an(phydev);
1448 + inst->state = DETECTING_LP;
1454 + xgkr_start_train(phydev);
1456 + mutex_unlock(&phydev->lock);
1457 + queue_delayed_work(system_power_efficient_wq, &inst->xgkr_wk,
1458 + msecs_to_jiffies(XGKR_TIMEOUT));
1461 +static int fsl_backplane_probe(struct phy_device *phydev)
1463 + struct fsl_xgkr_inst *xgkr_inst;
1464 + struct device_node *phy_node, *lane_node;
1465 + struct resource res_lane;
1471 + phy_node = phydev->mdio.dev.of_node;
1472 + bp_mode = of_property_read_string(phy_node, "backplane-mode", &bm);
1476 + if (!strcasecmp(bm, "1000base-kx")) {
1477 + bp_mode = PHY_BACKPLANE_1000BASE_KX;
1478 + } else if (!strcasecmp(bm, "10gbase-kr")) {
1479 + bp_mode = PHY_BACKPLANE_10GBASE_KR;
1481 + dev_err(&phydev->mdio.dev, "Unknown backplane-mode\n");
1485 + lane_node = of_parse_phandle(phy_node, "fsl,lane-handle", 0);
1487 + dev_err(&phydev->mdio.dev, "parse fsl,lane-handle failed\n");
1491 + ret = of_address_to_resource(lane_node, 0, &res_lane);
1493 + dev_err(&phydev->mdio.dev, "could not obtain memory map\n");
1497 + of_node_put(lane_node);
1498 + ret = of_property_read_u32_array(phy_node, "fsl,lane-reg",
1501 + dev_err(&phydev->mdio.dev, "could not get fsl,lane-reg\n");
1505 + phydev->priv = devm_ioremap_nocache(&phydev->mdio.dev,
1506 + res_lane.start + lane[0],
1508 + if (!phydev->priv) {
1509 + dev_err(&phydev->mdio.dev, "ioremap_nocache failed\n");
1513 + if (bp_mode == PHY_BACKPLANE_1000BASE_KX) {
1514 + phydev->speed = SPEED_1000;
1515 + /* configure the lane for 1000BASE-KX */
1516 + lane_set_1gkx(phydev->priv);
1520 + xgkr_inst = devm_kzalloc(&phydev->mdio.dev,
1521 + sizeof(*xgkr_inst), GFP_KERNEL);
1525 + xgkr_inst->reg_base = phydev->priv;
1526 + xgkr_inst->phydev = phydev;
1527 + phydev->priv = xgkr_inst;
1529 + if (bp_mode == PHY_BACKPLANE_10GBASE_KR) {
1530 + phydev->speed = SPEED_10000;
1531 + INIT_DELAYED_WORK(&xgkr_inst->xgkr_wk, xgkr_state_machine);
1537 +static int fsl_backplane_aneg_done(struct phy_device *phydev)
1542 +static int fsl_backplane_config_aneg(struct phy_device *phydev)
1544 + if (phydev->speed == SPEED_10000) {
1545 + phydev->supported |= SUPPORTED_10000baseKR_Full;
1546 + start_xgkr_an(phydev);
1547 + } else if (phydev->speed == SPEED_1000) {
1548 + phydev->supported |= SUPPORTED_1000baseKX_Full;
1549 + start_1gkx_an(phydev);
1552 + phydev->advertising = phydev->supported;
1553 + phydev->duplex = 1;
1558 +static int fsl_backplane_suspend(struct phy_device *phydev)
1560 + if (phydev->speed == SPEED_10000) {
1561 + struct fsl_xgkr_inst *xgkr_inst = phydev->priv;
1563 + cancel_delayed_work_sync(&xgkr_inst->xgkr_wk);
1568 +static int fsl_backplane_resume(struct phy_device *phydev)
1570 + if (phydev->speed == SPEED_10000) {
1571 + struct fsl_xgkr_inst *xgkr_inst = phydev->priv;
1573 + init_inst(xgkr_inst, 1);
1574 + queue_delayed_work(system_power_efficient_wq,
1575 + &xgkr_inst->xgkr_wk,
1576 + msecs_to_jiffies(XGKR_TIMEOUT));
1581 +static int fsl_backplane_read_status(struct phy_device *phydev)
1583 + if (is_link_up(phydev))
1591 +static struct phy_driver fsl_backplane_driver[] = {
1593 + .phy_id = FSL_PCS_PHY_ID,
1594 + .name = "Freescale Backplane",
1595 + .phy_id_mask = 0xffffffff,
1596 + .features = SUPPORTED_Backplane | SUPPORTED_Autoneg |
1598 + .probe = fsl_backplane_probe,
1599 + .aneg_done = fsl_backplane_aneg_done,
1600 + .config_aneg = fsl_backplane_config_aneg,
1601 + .read_status = fsl_backplane_read_status,
1602 + .suspend = fsl_backplane_suspend,
1603 + .resume = fsl_backplane_resume,
1607 +module_phy_driver(fsl_backplane_driver);
1609 +static struct mdio_device_id __maybe_unused freescale_tbl[] = {
1610 + { FSL_PCS_PHY_ID, 0xffffffff },
1614 +MODULE_DEVICE_TABLE(mdio, freescale_tbl);
1616 +MODULE_DESCRIPTION("Freescale Backplane driver");
1617 +MODULE_AUTHOR("Shaohui Xie <Shaohui.Xie@freescale.com>");
1618 +MODULE_LICENSE("GPL v2");
1619 diff --git a/drivers/net/phy/phy.c b/drivers/net/phy/phy.c
1620 index f3e64a89..42cdd5b7 100644
1621 --- a/drivers/net/phy/phy.c
1622 +++ b/drivers/net/phy/phy.c
1623 @@ -585,7 +585,7 @@ int phy_mii_ioctl(struct phy_device *phydev, struct ifreq *ifr, int cmd)
1627 - if (phydev->drv->hwtstamp)
1628 + if (phydev->drv && phydev->drv->hwtstamp)
1629 return phydev->drv->hwtstamp(phydev, ifr);
1632 @@ -610,6 +610,9 @@ static int phy_start_aneg_priv(struct phy_device *phydev, bool sync)
1639 mutex_lock(&phydev->lock);
1641 if (AUTONEG_DISABLE == phydev->autoneg)
1642 @@ -1009,7 +1012,7 @@ void phy_state_machine(struct work_struct *work)
1644 old_state = phydev->state;
1646 - if (phydev->drv->link_change_notify)
1647 + if (phydev->drv && phydev->drv->link_change_notify)
1648 phydev->drv->link_change_notify(phydev);
1650 switch (phydev->state) {
1651 @@ -1311,6 +1314,9 @@ EXPORT_SYMBOL(phy_write_mmd_indirect);
1653 int phy_init_eee(struct phy_device *phydev, bool clk_stop_enable)
1658 /* According to 802.3az,the EEE is supported only in full duplex-mode.
1659 * Also EEE feature is active when core is operating with MII, GMII
1660 * or RGMII (all kinds). Internal PHYs are also allowed to proceed and
1661 @@ -1388,6 +1394,9 @@ EXPORT_SYMBOL(phy_init_eee);
1663 int phy_get_eee_err(struct phy_device *phydev)
1668 return phy_read_mmd_indirect(phydev, MDIO_PCS_EEE_WK_ERR, MDIO_MMD_PCS);
1670 EXPORT_SYMBOL(phy_get_eee_err);
1671 @@ -1404,6 +1413,9 @@ int phy_ethtool_get_eee(struct phy_device *phydev, struct ethtool_eee *data)
1678 /* Get Supported EEE */
1679 val = phy_read_mmd_indirect(phydev, MDIO_PCS_EEE_ABLE, MDIO_MMD_PCS);
1681 @@ -1437,6 +1449,9 @@ int phy_ethtool_set_eee(struct phy_device *phydev, struct ethtool_eee *data)
1683 int val = ethtool_adv_to_mmd_eee_adv_t(data->advertised);
1688 /* Mask prohibited EEE modes */
1689 val &= ~phydev->eee_broken_modes;
1691 @@ -1448,7 +1463,7 @@ EXPORT_SYMBOL(phy_ethtool_set_eee);
1693 int phy_ethtool_set_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol)
1695 - if (phydev->drv->set_wol)
1696 + if (phydev->drv && phydev->drv->set_wol)
1697 return phydev->drv->set_wol(phydev, wol);
1700 @@ -1457,7 +1472,7 @@ EXPORT_SYMBOL(phy_ethtool_set_wol);
1702 void phy_ethtool_get_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol)
1704 - if (phydev->drv->get_wol)
1705 + if (phydev->drv && phydev->drv->get_wol)
1706 phydev->drv->get_wol(phydev, wol);
1708 EXPORT_SYMBOL(phy_ethtool_get_wol);
1709 diff --git a/drivers/net/phy/phy_device.c b/drivers/net/phy/phy_device.c
1710 index 5fdc491e..039f9664 100644
1711 --- a/drivers/net/phy/phy_device.c
1712 +++ b/drivers/net/phy/phy_device.c
1713 @@ -1046,7 +1046,7 @@ int phy_suspend(struct phy_device *phydev)
1717 - if (phydrv->suspend)
1718 + if (phydev->drv && phydrv->suspend)
1719 ret = phydrv->suspend(phydev);
1722 @@ -1063,7 +1063,7 @@ int phy_resume(struct phy_device *phydev)
1723 struct phy_driver *phydrv = to_phy_driver(phydev->mdio.dev.driver);
1726 - if (phydrv->resume)
1727 + if (phydev->drv && phydrv->resume)
1728 ret = phydrv->resume(phydev);
1731 @@ -1726,7 +1726,7 @@ static int phy_remove(struct device *dev)
1732 phydev->state = PHY_DOWN;
1733 mutex_unlock(&phydev->lock);
1735 - if (phydev->drv->remove)
1736 + if (phydev->drv && phydev->drv->remove)
1737 phydev->drv->remove(phydev);
1740 diff --git a/drivers/net/phy/swphy.c b/drivers/net/phy/swphy.c
1741 index 34f58f23..52ddddbe 100644
1742 --- a/drivers/net/phy/swphy.c
1743 +++ b/drivers/net/phy/swphy.c
1744 @@ -77,6 +77,7 @@ static const struct swmii_regs duplex[] = {
1745 static int swphy_decode_speed(int speed)
1750 return SWMII_SPEED_1000;
1752 diff --git a/include/linux/phy.h b/include/linux/phy.h
1753 index 850c8b51..5f253f1a 100644
1754 --- a/include/linux/phy.h
1755 +++ b/include/linux/phy.h
1756 @@ -81,6 +81,7 @@ typedef enum {
1757 PHY_INTERFACE_MODE_MOCA,
1758 PHY_INTERFACE_MODE_QSGMII,
1759 PHY_INTERFACE_MODE_TRGMII,
1760 + PHY_INTERFACE_MODE_SGMII_2500,
1761 PHY_INTERFACE_MODE_MAX,
1764 @@ -784,6 +785,9 @@ int phy_stop_interrupts(struct phy_device *phydev);
1766 static inline int phy_read_status(struct phy_device *phydev)
1771 return phydev->drv->read_status(phydev);