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 --- a/drivers/net/phy/Kconfig
30 +++ b/drivers/net/phy/Kconfig
31 @@ -89,6 +89,12 @@ config MDIO_BUS_MUX_MMIOREG
35 +config MDIO_FSL_BACKPLANE
36 + tristate "Support for backplane on Freescale XFI interface"
39 + This module provides a driver for Freescale XFI's backplane.
42 tristate "GPIO lib-based bitbanged MDIO buses"
43 depends on MDIO_BITBANG && GPIOLIB
44 @@ -298,6 +304,11 @@ config CICADA_PHY
46 Currently supports the cis8204
49 + tristate "Cortina EDC CDR 10G Ethernet PHY"
51 + Currently supports the CS4340 phy.
54 tristate "Davicom PHYs"
56 --- a/drivers/net/phy/Makefile
57 +++ b/drivers/net/phy/Makefile
58 @@ -30,6 +30,7 @@ obj-$(CONFIG_MDIO_BUS_MUX_BCM_IPROC) +=
59 obj-$(CONFIG_MDIO_BUS_MUX_GPIO) += mdio-mux-gpio.o
60 obj-$(CONFIG_MDIO_BUS_MUX_MMIOREG) += mdio-mux-mmioreg.o
61 obj-$(CONFIG_MDIO_CAVIUM) += mdio-cavium.o
62 +obj-$(CONFIG_MDIO_FSL_BACKPLANE) += fsl_backplane.o
63 obj-$(CONFIG_MDIO_GPIO) += mdio-gpio.o
64 obj-$(CONFIG_MDIO_HISI_FEMAC) += mdio-hisi-femac.o
65 obj-$(CONFIG_MDIO_MOXART) += mdio-moxart.o
66 @@ -48,6 +49,7 @@ obj-$(CONFIG_BCM_CYGNUS_PHY) += bcm-cygn
67 obj-$(CONFIG_BCM_NET_PHYLIB) += bcm-phy-lib.o
68 obj-$(CONFIG_BROADCOM_PHY) += broadcom.o
69 obj-$(CONFIG_CICADA_PHY) += cicada.o
70 +obj-$(CONFIG_CORTINA_PHY) += cortina.o
71 obj-$(CONFIG_DAVICOM_PHY) += davicom.o
72 obj-$(CONFIG_DP83640_PHY) += dp83640.o
73 obj-$(CONFIG_DP83848_PHY) += dp83848.o
74 --- a/drivers/net/phy/aquantia.c
75 +++ b/drivers/net/phy/aquantia.c
77 #define PHY_ID_AQ1202 0x03a1b445
78 #define PHY_ID_AQ2104 0x03a1b460
79 #define PHY_ID_AQR105 0x03a1b4a2
80 +#define PHY_ID_AQR106 0x03a1b4d0
81 +#define PHY_ID_AQR107 0x03a1b4e0
82 #define PHY_ID_AQR405 0x03a1b4b0
84 #define PHY_AQUANTIA_FEATURES (SUPPORTED_10000baseT_Full | \
85 @@ -154,6 +156,30 @@ static struct phy_driver aquantia_driver
86 .read_status = aquantia_read_status,
89 + .phy_id = PHY_ID_AQR106,
90 + .phy_id_mask = 0xfffffff0,
91 + .name = "Aquantia AQR106",
92 + .features = PHY_AQUANTIA_FEATURES,
93 + .flags = PHY_HAS_INTERRUPT,
94 + .aneg_done = aquantia_aneg_done,
95 + .config_aneg = aquantia_config_aneg,
96 + .config_intr = aquantia_config_intr,
97 + .ack_interrupt = aquantia_ack_interrupt,
98 + .read_status = aquantia_read_status,
101 + .phy_id = PHY_ID_AQR107,
102 + .phy_id_mask = 0xfffffff0,
103 + .name = "Aquantia AQR107",
104 + .features = PHY_AQUANTIA_FEATURES,
105 + .flags = PHY_HAS_INTERRUPT,
106 + .aneg_done = aquantia_aneg_done,
107 + .config_aneg = aquantia_config_aneg,
108 + .config_intr = aquantia_config_intr,
109 + .ack_interrupt = aquantia_ack_interrupt,
110 + .read_status = aquantia_read_status,
113 .phy_id = PHY_ID_AQR405,
114 .phy_id_mask = 0xfffffff0,
115 .name = "Aquantia AQR405",
116 @@ -173,6 +199,8 @@ static struct mdio_device_id __maybe_unu
117 { PHY_ID_AQ1202, 0xfffffff0 },
118 { PHY_ID_AQ2104, 0xfffffff0 },
119 { PHY_ID_AQR105, 0xfffffff0 },
120 + { PHY_ID_AQR106, 0xfffffff0 },
121 + { PHY_ID_AQR107, 0xfffffff0 },
122 { PHY_ID_AQR405, 0xfffffff0 },
126 +++ b/drivers/net/phy/cortina.c
129 + * Copyright 2017 NXP
131 + * This program is free software; you can redistribute it and/or modify
132 + * it under the terms of the GNU General Public License as published by
133 + * the Free Software Foundation; either version 2 of the License, or
134 + * (at your option) any later version.
136 + * This program is distributed in the hope that it will be useful,
137 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
138 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
139 + * GNU General Public License for more details.
141 + * CORTINA is a registered trademark of Cortina Systems, Inc.
144 +#include <linux/module.h>
145 +#include <linux/phy.h>
147 +#define PHY_ID_CS4340 0x13e51002
149 +#define VILLA_GLOBAL_CHIP_ID_LSB 0x0
150 +#define VILLA_GLOBAL_CHIP_ID_MSB 0x1
152 +#define VILLA_GLOBAL_GPIO_1_INTS 0x017
154 +static int cortina_read_reg(struct phy_device *phydev, u16 regnum)
156 + return mdiobus_read(phydev->mdio.bus, phydev->mdio.addr,
157 + MII_ADDR_C45 | regnum);
160 +static int cortina_config_aneg(struct phy_device *phydev)
162 + phydev->supported = SUPPORTED_10000baseT_Full;
163 + phydev->advertising = SUPPORTED_10000baseT_Full;
168 +static int cortina_read_status(struct phy_device *phydev)
170 + int gpio_int_status, ret = 0;
172 + gpio_int_status = cortina_read_reg(phydev, VILLA_GLOBAL_GPIO_1_INTS);
173 + if (gpio_int_status < 0) {
174 + ret = gpio_int_status;
178 + if (gpio_int_status & 0x8) {
179 + /* up when edc_convergedS set */
180 + phydev->speed = SPEED_10000;
181 + phydev->duplex = DUPLEX_FULL;
191 +static int cortina_soft_reset(struct phy_device *phydev)
196 +static int cortina_probe(struct phy_device *phydev)
199 + int id_lsb = 0, id_msb = 0;
201 + /* Read device id from phy registers. */
202 + id_lsb = cortina_read_reg(phydev, VILLA_GLOBAL_CHIP_ID_LSB);
206 + phy_id = id_lsb << 16;
208 + id_msb = cortina_read_reg(phydev, VILLA_GLOBAL_CHIP_ID_MSB);
214 + /* Make sure the device tree binding matched the driver with the
217 + if (phy_id != phydev->drv->phy_id) {
218 + phydev_err(phydev, "Error matching phy with %s driver\n",
219 + phydev->drv->name);
226 +static struct phy_driver cortina_driver[] = {
228 + .phy_id = PHY_ID_CS4340,
229 + .phy_id_mask = 0xffffffff,
230 + .name = "Cortina CS4340",
231 + .config_aneg = cortina_config_aneg,
232 + .read_status = cortina_read_status,
233 + .soft_reset = cortina_soft_reset,
234 + .probe = cortina_probe,
238 +module_phy_driver(cortina_driver);
240 +static struct mdio_device_id __maybe_unused cortina_tbl[] = {
241 + { PHY_ID_CS4340, 0xffffffff},
245 +MODULE_DEVICE_TABLE(mdio, cortina_tbl);
247 +++ b/drivers/net/phy/fsl_backplane.c
249 +/* Freescale backplane driver.
250 + * Author: Shaohui Xie <Shaohui.Xie@freescale.com>
252 + * Copyright 2015 Freescale Semiconductor, Inc.
254 + * Licensed under the GPL-2 or later.
257 +#include <linux/kernel.h>
258 +#include <linux/module.h>
259 +#include <linux/mii.h>
260 +#include <linux/mdio.h>
261 +#include <linux/ethtool.h>
262 +#include <linux/phy.h>
263 +#include <linux/io.h>
264 +#include <linux/of.h>
265 +#include <linux/of_net.h>
266 +#include <linux/of_address.h>
267 +#include <linux/of_platform.h>
268 +#include <linux/timer.h>
269 +#include <linux/delay.h>
270 +#include <linux/workqueue.h>
272 +/* XFI PCS Device Identifier */
273 +#define FSL_PCS_PHY_ID 0x0083e400
275 +/* Freescale KR PMD registers */
276 +#define FSL_KR_PMD_CTRL 0x96
277 +#define FSL_KR_PMD_STATUS 0x97
278 +#define FSL_KR_LP_CU 0x98
279 +#define FSL_KR_LP_STATUS 0x99
280 +#define FSL_KR_LD_CU 0x9a
281 +#define FSL_KR_LD_STATUS 0x9b
283 +/* Freescale KR PMD defines */
284 +#define PMD_RESET 0x1
285 +#define PMD_STATUS_SUP_STAT 0x4
286 +#define PMD_STATUS_FRAME_LOCK 0x2
287 +#define TRAIN_EN 0x3
288 +#define TRAIN_DISABLE 0x1
291 +#define FSL_KR_RX_LINK_STAT_MASK 0x1000
292 +#define FSL_XFI_PCS_10GR_SR1 0x20
294 +/* Freescale KX PCS mode register */
295 +#define FSL_PCS_IF_MODE 0x8014
297 +/* Freescale KX PCS mode register init value */
298 +#define IF_MODE_INIT 0x8
300 +/* Freescale KX/KR AN registers */
301 +#define FSL_AN_AD1 0x11
302 +#define FSL_AN_BP_STAT 0x30
304 +/* Freescale KX/KR AN registers defines */
305 +#define AN_CTRL_INIT 0x1200
306 +#define KX_AN_AD1_INIT 0x25
307 +#define KR_AN_AD1_INIT 0x85
308 +#define AN_LNK_UP_MASK 0x4
309 +#define KR_AN_MASK 0x8
310 +#define TRAIN_FAIL 0x8
316 +#define BIN_M1_SEL 6
317 +#define BIN_Long_SEL 7
318 +#define CDR_SEL_MASK 0x00070000
319 +#define BIN_SNAPSHOT_NUM 5
320 +#define BIN_M1_THRESHOLD 3
321 +#define BIN_LONG_THRESHOLD 2
323 +#define PRE_COE_SHIFT 22
324 +#define POST_COE_SHIFT 16
325 +#define ZERO_COE_SHIFT 8
327 +#define PRE_COE_MAX 0x0
328 +#define PRE_COE_MIN 0x8
329 +#define POST_COE_MAX 0x0
330 +#define POST_COE_MIN 0x10
331 +#define ZERO_COE_MAX 0x30
332 +#define ZERO_COE_MIN 0x0
334 +#define TECR0_INIT 0x24200000
335 +#define RATIO_PREQ 0x3
336 +#define RATIO_PST1Q 0xd
337 +#define RATIO_EQ 0x20
339 +#define GCR0_RESET_MASK 0x600000
340 +#define GCR1_SNP_START_MASK 0x00000040
341 +#define GCR1_CTL_SNP_START_MASK 0x00002000
342 +#define GCR1_REIDL_TH_MASK 0x00700000
343 +#define GCR1_REIDL_EX_SEL_MASK 0x000c0000
344 +#define GCR1_REIDL_ET_MAS_MASK 0x00004000
345 +#define TECR0_AMP_RED_MASK 0x0000003f
347 +#define RECR1_CTL_SNP_DONE_MASK 0x00000002
348 +#define RECR1_SNP_DONE_MASK 0x00000004
349 +#define TCSR1_SNP_DATA_MASK 0x0000ffc0
350 +#define TCSR1_SNP_DATA_SHIFT 6
351 +#define TCSR1_EQ_SNPBIN_SIGN_MASK 0x100
353 +#define RECR1_GAINK2_MASK 0x0f000000
354 +#define RECR1_GAINK2_SHIFT 24
355 +#define RECR1_GAINK3_MASK 0x000f0000
356 +#define RECR1_GAINK3_SHIFT 16
357 +#define RECR1_OFFSET_MASK 0x00003f80
358 +#define RECR1_OFFSET_SHIFT 7
359 +#define RECR1_BLW_MASK 0x00000f80
360 +#define RECR1_BLW_SHIFT 7
361 +#define EYE_CTRL_SHIFT 12
362 +#define BASE_WAND_SHIFT 10
364 +#define XGKR_TIMEOUT 1050
368 +#define TIMEOUT_LONG 3
369 +#define TIMEOUT_M1 3
371 +#define RX_READY_MASK 0x8000
372 +#define PRESET_MASK 0x2000
373 +#define INIT_MASK 0x1000
374 +#define COP1_MASK 0x30
375 +#define COP1_SHIFT 4
376 +#define COZ_MASK 0xc
378 +#define COM1_MASK 0x3
379 +#define COM1_SHIFT 0
380 +#define REQUEST_MASK 0x3f
381 +#define LD_ALL_MASK (PRESET_MASK | INIT_MASK | \
382 + COP1_MASK | COZ_MASK | COM1_MASK)
384 +#define NEW_ALGORITHM_TRAIN_TX
385 +#ifdef NEW_ALGORITHM_TRAIN_TX
386 +#define FORCE_INC_COP1_NUMBER 0
387 +#define FORCE_INC_COM1_NUMBER 1
390 +#define VAL_INVALID 0xff
392 +static const u32 preq_table[] = {0x0, 0x1, 0x3, 0x5,
393 + 0x7, 0x9, 0xb, 0xc, VAL_INVALID};
394 +static const u32 pst1q_table[] = {0x0, 0x1, 0x3, 0x5, 0x7,
395 + 0x9, 0xb, 0xd, 0xf, 0x10, VAL_INVALID};
397 +enum backplane_mode {
398 + PHY_BACKPLANE_1000BASE_KX,
399 + PHY_BACKPLANE_10GBASE_KR,
400 + PHY_BACKPLANE_INVAL
422 +struct per_lane_ctrl_status {
423 + __be32 gcr0; /* 0x.000 - General Control Register 0 */
424 + __be32 gcr1; /* 0x.004 - General Control Register 1 */
425 + __be32 gcr2; /* 0x.008 - General Control Register 2 */
426 + __be32 resv1; /* 0x.00C - Reserved */
427 + __be32 recr0; /* 0x.010 - Receive Equalization Control Register 0 */
428 + __be32 recr1; /* 0x.014 - Receive Equalization Control Register 1 */
429 + __be32 tecr0; /* 0x.018 - Transmit Equalization Control Register 0 */
430 + __be32 resv2; /* 0x.01C - Reserved */
431 + __be32 tlcr0; /* 0x.020 - TTL Control Register 0 */
432 + __be32 tlcr1; /* 0x.024 - TTL Control Register 1 */
433 + __be32 tlcr2; /* 0x.028 - TTL Control Register 2 */
434 + __be32 tlcr3; /* 0x.02C - TTL Control Register 3 */
435 + __be32 tcsr0; /* 0x.030 - Test Control/Status Register 0 */
436 + __be32 tcsr1; /* 0x.034 - Test Control/Status Register 1 */
437 + __be32 tcsr2; /* 0x.038 - Test Control/Status Register 2 */
438 + __be32 tcsr3; /* 0x.03C - Test Control/Status Register 3 */
441 +struct tx_condition {
442 + bool bin_m1_late_early;
443 + bool bin_long_late_early;
445 + bool bin_long_stop;
448 + int m1_min_max_cnt;
449 + int long_min_max_cnt;
450 +#ifdef NEW_ALGORITHM_TRAIN_TX
456 +struct fsl_xgkr_inst {
458 + struct phy_device *phydev;
459 + struct tx_condition tx_c;
460 + struct delayed_work xgkr_wk;
461 + enum train_state state;
469 +static void tx_condition_init(struct tx_condition *tx_c)
471 + tx_c->bin_m1_late_early = true;
472 + tx_c->bin_long_late_early = false;
473 + tx_c->bin_m1_stop = false;
474 + tx_c->bin_long_stop = false;
475 + tx_c->tx_complete = false;
476 + tx_c->sent_init = false;
477 + tx_c->m1_min_max_cnt = 0;
478 + tx_c->long_min_max_cnt = 0;
479 +#ifdef NEW_ALGORITHM_TRAIN_TX
480 + tx_c->pre_inc = FORCE_INC_COM1_NUMBER;
481 + tx_c->post_inc = FORCE_INC_COP1_NUMBER;
485 +void tune_tecr0(struct fsl_xgkr_inst *inst)
487 + struct per_lane_ctrl_status *reg_base = inst->reg_base;
491 + inst->adpt_eq << ZERO_COE_SHIFT |
492 + inst->ratio_preq << PRE_COE_SHIFT |
493 + inst->ratio_pst1q << POST_COE_SHIFT;
495 + /* reset the lane */
496 + iowrite32(ioread32(®_base->gcr0) & ~GCR0_RESET_MASK,
499 + iowrite32(val, ®_base->tecr0);
501 + /* unreset the lane */
502 + iowrite32(ioread32(®_base->gcr0) | GCR0_RESET_MASK,
507 +static void start_lt(struct phy_device *phydev)
509 + phy_write_mmd(phydev, MDIO_MMD_PMAPMD, FSL_KR_PMD_CTRL, TRAIN_EN);
512 +static void stop_lt(struct phy_device *phydev)
514 + phy_write_mmd(phydev, MDIO_MMD_PMAPMD, FSL_KR_PMD_CTRL, TRAIN_DISABLE);
517 +static void reset_gcr0(struct fsl_xgkr_inst *inst)
519 + struct per_lane_ctrl_status *reg_base = inst->reg_base;
521 + iowrite32(ioread32(®_base->gcr0) & ~GCR0_RESET_MASK,
524 + iowrite32(ioread32(®_base->gcr0) | GCR0_RESET_MASK,
529 +void lane_set_1gkx(void *reg)
531 + struct per_lane_ctrl_status *reg_base = reg;
534 + /* reset the lane */
535 + iowrite32(ioread32(®_base->gcr0) & ~GCR0_RESET_MASK,
539 + /* set gcr1 for 1GKX */
540 + val = ioread32(®_base->gcr1);
541 + val &= ~(GCR1_REIDL_TH_MASK | GCR1_REIDL_EX_SEL_MASK |
542 + GCR1_REIDL_ET_MAS_MASK);
543 + iowrite32(val, ®_base->gcr1);
546 + /* set tecr0 for 1GKX */
547 + val = ioread32(®_base->tecr0);
548 + val &= ~TECR0_AMP_RED_MASK;
549 + iowrite32(val, ®_base->tecr0);
552 + /* unreset the lane */
553 + iowrite32(ioread32(®_base->gcr0) | GCR0_RESET_MASK,
558 +static void reset_lt(struct phy_device *phydev)
560 + phy_write_mmd(phydev, MDIO_MMD_PMAPMD, MDIO_CTRL1, PMD_RESET);
561 + phy_write_mmd(phydev, MDIO_MMD_PMAPMD, FSL_KR_PMD_CTRL, TRAIN_DISABLE);
562 + phy_write_mmd(phydev, MDIO_MMD_PMAPMD, FSL_KR_LD_CU, 0);
563 + phy_write_mmd(phydev, MDIO_MMD_PMAPMD, FSL_KR_LD_STATUS, 0);
564 + phy_write_mmd(phydev, MDIO_MMD_PMAPMD, FSL_KR_PMD_STATUS, 0);
565 + phy_write_mmd(phydev, MDIO_MMD_PMAPMD, FSL_KR_LP_CU, 0);
566 + phy_write_mmd(phydev, MDIO_MMD_PMAPMD, FSL_KR_LP_STATUS, 0);
569 +static void start_xgkr_state_machine(struct delayed_work *work)
571 + queue_delayed_work(system_power_efficient_wq, work,
572 + msecs_to_jiffies(XGKR_TIMEOUT));
575 +static void start_xgkr_an(struct phy_device *phydev)
577 + struct fsl_xgkr_inst *inst;
580 + phy_write_mmd(phydev, MDIO_MMD_AN, FSL_AN_AD1, KR_AN_AD1_INIT);
581 + phy_write_mmd(phydev, MDIO_MMD_AN, MDIO_CTRL1, AN_CTRL_INIT);
583 + inst = phydev->priv;
585 + /* start state machine*/
586 + start_xgkr_state_machine(&inst->xgkr_wk);
589 +static void start_1gkx_an(struct phy_device *phydev)
591 + phy_write_mmd(phydev, MDIO_MMD_PCS, FSL_PCS_IF_MODE, IF_MODE_INIT);
592 + phy_write_mmd(phydev, MDIO_MMD_AN, FSL_AN_AD1, KX_AN_AD1_INIT);
593 + phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_STAT1);
594 + phy_write_mmd(phydev, MDIO_MMD_AN, MDIO_CTRL1, AN_CTRL_INIT);
597 +static void ld_coe_status(struct fsl_xgkr_inst *inst)
599 + phy_write_mmd(inst->phydev, MDIO_MMD_PMAPMD,
600 + FSL_KR_LD_STATUS, inst->ld_status);
603 +static void ld_coe_update(struct fsl_xgkr_inst *inst)
605 + dev_dbg(&inst->phydev->mdio.dev, "sending request: %x\n", inst->ld_update);
606 + phy_write_mmd(inst->phydev, MDIO_MMD_PMAPMD,
607 + FSL_KR_LD_CU, inst->ld_update);
610 +static void init_inst(struct fsl_xgkr_inst *inst, int reset)
613 + inst->ratio_preq = RATIO_PREQ;
614 + inst->ratio_pst1q = RATIO_PST1Q;
615 + inst->adpt_eq = RATIO_EQ;
619 + tx_condition_init(&inst->tx_c);
620 + inst->state = DETECTING_LP;
621 + inst->ld_status &= RX_READY_MASK;
622 + ld_coe_status(inst);
623 + inst->ld_update = 0;
624 + inst->ld_status &= ~RX_READY_MASK;
625 + ld_coe_status(inst);
628 +#ifdef NEW_ALGORITHM_TRAIN_TX
629 +static int get_median_gaink2(u32 *reg)
631 + int gaink2_snap_shot[BIN_SNAPSHOT_NUM];
633 + struct per_lane_ctrl_status *reg_base;
635 + int i, j, tmp, pos;
637 + reg_base = (struct per_lane_ctrl_status *)reg;
639 + for (i = 0; i < BIN_SNAPSHOT_NUM; i++) {
640 + /* wait RECR1_CTL_SNP_DONE_MASK has cleared */
642 + while (ioread32(®_base->recr1) &
643 + RECR1_CTL_SNP_DONE_MASK) {
650 + /* start snap shot */
651 + iowrite32((ioread32(®_base->gcr1) |
652 + GCR1_CTL_SNP_START_MASK),
655 + /* wait for SNP done */
657 + while (!(ioread32(®_base->recr1) &
658 + RECR1_CTL_SNP_DONE_MASK)) {
665 + /* read and save the snap shot */
666 + rx_eq_snp = ioread32(®_base->recr1);
667 + gaink2_snap_shot[i] = (rx_eq_snp & RECR1_GAINK2_MASK) >>
668 + RECR1_GAINK2_SHIFT;
670 + /* terminate the snap shot by setting GCR1[REQ_CTL_SNP] */
671 + iowrite32((ioread32(®_base->gcr1) &
672 + ~GCR1_CTL_SNP_START_MASK),
676 + /* get median of the 5 snap shot */
677 + for (i = 0; i < BIN_SNAPSHOT_NUM - 1; i++) {
678 + tmp = gaink2_snap_shot[i];
680 + for (j = i + 1; j < BIN_SNAPSHOT_NUM; j++) {
681 + if (gaink2_snap_shot[j] < tmp) {
682 + tmp = gaink2_snap_shot[j];
687 + gaink2_snap_shot[pos] = gaink2_snap_shot[i];
688 + gaink2_snap_shot[i] = tmp;
691 + return gaink2_snap_shot[2];
695 +static bool is_bin_early(int bin_sel, void *reg)
697 + bool early = false;
698 + int bin_snap_shot[BIN_SNAPSHOT_NUM];
699 + int i, negative_count = 0;
700 + struct per_lane_ctrl_status *reg_base = reg;
703 + for (i = 0; i < BIN_SNAPSHOT_NUM; i++) {
704 + /* wait RECR1_SNP_DONE_MASK has cleared */
706 + while ((ioread32(®_base->recr1) & RECR1_SNP_DONE_MASK)) {
713 + /* set TCSR1[CDR_SEL] to BinM1/BinLong */
714 + if (bin_sel == BIN_M1) {
715 + iowrite32((ioread32(®_base->tcsr1) &
716 + ~CDR_SEL_MASK) | BIN_M1_SEL,
719 + iowrite32((ioread32(®_base->tcsr1) &
720 + ~CDR_SEL_MASK) | BIN_Long_SEL,
724 + /* start snap shot */
725 + iowrite32(ioread32(®_base->gcr1) | GCR1_SNP_START_MASK,
728 + /* wait for SNP done */
730 + while (!(ioread32(®_base->recr1) & RECR1_SNP_DONE_MASK)) {
737 + /* read and save the snap shot */
738 + bin_snap_shot[i] = (ioread32(®_base->tcsr1) &
739 + TCSR1_SNP_DATA_MASK) >> TCSR1_SNP_DATA_SHIFT;
740 + if (bin_snap_shot[i] & TCSR1_EQ_SNPBIN_SIGN_MASK)
743 + /* terminate the snap shot by setting GCR1[REQ_CTL_SNP] */
744 + iowrite32(ioread32(®_base->gcr1) & ~GCR1_SNP_START_MASK,
748 + if (((bin_sel == BIN_M1) && (negative_count > BIN_M1_THRESHOLD)) ||
749 + ((bin_sel == BIN_LONG && (negative_count > BIN_LONG_THRESHOLD)))) {
756 +static void train_tx(struct fsl_xgkr_inst *inst)
758 + struct phy_device *phydev = inst->phydev;
759 + struct tx_condition *tx_c = &inst->tx_c;
760 + bool bin_m1_early, bin_long_early;
761 + u32 lp_status, old_ld_update;
762 + u32 status_cop1, status_coz, status_com1;
763 + u32 req_cop1, req_coz, req_com1, req_preset, req_init;
765 +#ifdef NEW_ALGORITHM_TRAIN_TX
770 + if (tx_c->bin_long_stop && tx_c->bin_m1_stop) {
771 + tx_c->tx_complete = true;
772 + inst->ld_status |= RX_READY_MASK;
773 + ld_coe_status(inst);
774 + /* tell LP we are ready */
775 + phy_write_mmd(phydev, MDIO_MMD_PMAPMD,
776 + FSL_KR_PMD_STATUS, RX_STAT);
780 + /* We start by checking the current LP status. If we got any responses,
781 + * we can clear up the appropriate update request so that the
782 + * subsequent code may easily issue new update requests if needed.
784 + lp_status = phy_read_mmd(phydev, MDIO_MMD_PMAPMD, FSL_KR_LP_STATUS) &
786 + status_cop1 = (lp_status & COP1_MASK) >> COP1_SHIFT;
787 + status_coz = (lp_status & COZ_MASK) >> COZ_SHIFT;
788 + status_com1 = (lp_status & COM1_MASK) >> COM1_SHIFT;
790 + old_ld_update = inst->ld_update;
791 + req_cop1 = (old_ld_update & COP1_MASK) >> COP1_SHIFT;
792 + req_coz = (old_ld_update & COZ_MASK) >> COZ_SHIFT;
793 + req_com1 = (old_ld_update & COM1_MASK) >> COM1_SHIFT;
794 + req_preset = old_ld_update & PRESET_MASK;
795 + req_init = old_ld_update & INIT_MASK;
797 + /* IEEE802.3-2008, 72.6.10.2.3.1
798 + * We may clear PRESET when all coefficients show UPDATED or MAX.
801 + if ((status_cop1 == COE_UPDATED || status_cop1 == COE_MAX) &&
802 + (status_coz == COE_UPDATED || status_coz == COE_MAX) &&
803 + (status_com1 == COE_UPDATED || status_com1 == COE_MAX)) {
804 + inst->ld_update &= ~PRESET_MASK;
808 + /* IEEE802.3-2008, 72.6.10.2.3.2
809 + * We may clear INITIALIZE when no coefficients show NOT UPDATED.
812 + if (status_cop1 != COE_NOTUPDATED &&
813 + status_coz != COE_NOTUPDATED &&
814 + status_com1 != COE_NOTUPDATED) {
815 + inst->ld_update &= ~INIT_MASK;
819 + /* IEEE802.3-2008, 72.6.10.2.3.2
820 + * we send initialize to the other side to ensure default settings
821 + * for the LP. Naturally, we should do this only once.
823 + if (!tx_c->sent_init) {
824 + if (!lp_status && !(old_ld_update & (LD_ALL_MASK))) {
825 + inst->ld_update = INIT_MASK;
826 + tx_c->sent_init = true;
830 + /* IEEE802.3-2008, 72.6.10.2.3.3
831 + * We set coefficient requests to HOLD when we get the information
832 + * about any updates On clearing our prior response, we also update
833 + * our internal status.
835 + if (status_cop1 != COE_NOTUPDATED) {
837 + inst->ld_update &= ~COP1_MASK;
838 +#ifdef NEW_ALGORITHM_TRAIN_TX
839 + if (tx_c->post_inc) {
840 + if (req_cop1 == INCREMENT &&
841 + status_cop1 == COE_MAX) {
842 + tx_c->post_inc = 0;
843 + tx_c->bin_long_stop = true;
844 + tx_c->bin_m1_stop = true;
846 + tx_c->post_inc -= 1;
849 + ld_coe_update(inst);
853 + if ((req_cop1 == DECREMENT && status_cop1 == COE_MIN) ||
854 + (req_cop1 == INCREMENT && status_cop1 == COE_MAX)) {
855 + dev_dbg(&inst->phydev->mdio.dev, "COP1 hit limit %s",
856 + (status_cop1 == COE_MIN) ?
857 + "DEC MIN" : "INC MAX");
858 + tx_c->long_min_max_cnt++;
859 + if (tx_c->long_min_max_cnt >= TIMEOUT_LONG) {
860 + tx_c->bin_long_stop = true;
861 + ld_coe_update(inst);
868 + if (status_coz != COE_NOTUPDATED) {
870 + inst->ld_update &= ~COZ_MASK;
873 + if (status_com1 != COE_NOTUPDATED) {
875 + inst->ld_update &= ~COM1_MASK;
876 +#ifdef NEW_ALGORITHM_TRAIN_TX
877 + if (tx_c->pre_inc) {
878 + if (req_com1 == INCREMENT &&
879 + status_com1 == COE_MAX)
882 + tx_c->pre_inc -= 1;
884 + ld_coe_update(inst);
888 + /* Stop If we have reached the limit for a parameter. */
889 + if ((req_com1 == DECREMENT && status_com1 == COE_MIN) ||
890 + (req_com1 == INCREMENT && status_com1 == COE_MAX)) {
891 + dev_dbg(&inst->phydev->mdio.dev, "COM1 hit limit %s",
892 + (status_com1 == COE_MIN) ?
893 + "DEC MIN" : "INC MAX");
894 + tx_c->m1_min_max_cnt++;
895 + if (tx_c->m1_min_max_cnt >= TIMEOUT_M1) {
896 + tx_c->bin_m1_stop = true;
897 + ld_coe_update(inst);
904 + if (old_ld_update != inst->ld_update) {
905 + ld_coe_update(inst);
906 + /* Redo these status checks and updates until we have no more
907 + * changes, to speed up the overall process.
912 + /* Do nothing if we have pending request. */
913 + if ((req_coz || req_com1 || req_cop1))
915 + else if (lp_status)
916 + /* No pending request but LP status was not reverted to
921 +#ifdef NEW_ALGORITHM_TRAIN_TX
922 + if (!(inst->ld_update & (PRESET_MASK | INIT_MASK))) {
923 + if (tx_c->pre_inc) {
924 + inst->ld_update = INCREMENT << COM1_SHIFT;
925 + ld_coe_update(inst);
929 + if (status_cop1 != COE_MAX) {
930 + median_gaink2 = get_median_gaink2(inst->reg_base);
931 + if (median_gaink2 == 0xf) {
932 + tx_c->post_inc = 1;
934 + /* Gaink2 median lower than "F" */
935 + tx_c->bin_m1_stop = true;
936 + tx_c->bin_long_stop = true;
941 + tx_c->bin_m1_stop = true;
942 + tx_c->bin_long_stop = true;
946 + if (tx_c->post_inc) {
947 + inst->ld_update = INCREMENT << COP1_SHIFT;
948 + ld_coe_update(inst);
954 + /* snapshot and select bin */
955 + bin_m1_early = is_bin_early(BIN_M1, inst->reg_base);
956 + bin_long_early = is_bin_early(BIN_LONG, inst->reg_base);
958 + if (!tx_c->bin_m1_stop && !tx_c->bin_m1_late_early && bin_m1_early) {
959 + tx_c->bin_m1_stop = true;
963 + if (!tx_c->bin_long_stop &&
964 + tx_c->bin_long_late_early && !bin_long_early) {
965 + tx_c->bin_long_stop = true;
969 + /* IEEE802.3-2008, 72.6.10.2.3.3
970 + * We only request coefficient updates when no PRESET/INITIALIZE is
971 + * pending. We also only request coefficient updates when the
972 + * corresponding status is NOT UPDATED and nothing is pending.
974 + if (!(inst->ld_update & (PRESET_MASK | INIT_MASK))) {
975 + if (!tx_c->bin_long_stop) {
976 + /* BinM1 correction means changing COM1 */
977 + if (!status_com1 && !(inst->ld_update & COM1_MASK)) {
978 + /* Avoid BinM1Late by requesting an
979 + * immediate decrement.
981 + if (!bin_m1_early) {
982 + /* request decrement c(-1) */
983 + temp = DECREMENT << COM1_SHIFT;
984 + inst->ld_update = temp;
985 + ld_coe_update(inst);
986 + tx_c->bin_m1_late_early = bin_m1_early;
991 + /* BinLong correction means changing COP1 */
992 + if (!status_cop1 && !(inst->ld_update & COP1_MASK)) {
993 + /* Locate BinLong transition point (if any)
994 + * while avoiding BinM1Late.
996 + if (bin_long_early) {
997 + /* request increment c(1) */
998 + temp = INCREMENT << COP1_SHIFT;
999 + inst->ld_update = temp;
1001 + /* request decrement c(1) */
1002 + temp = DECREMENT << COP1_SHIFT;
1003 + inst->ld_update = temp;
1006 + ld_coe_update(inst);
1007 + tx_c->bin_long_late_early = bin_long_early;
1009 + /* We try to finish BinLong before we do BinM1 */
1013 + if (!tx_c->bin_m1_stop) {
1014 + /* BinM1 correction means changing COM1 */
1015 + if (!status_com1 && !(inst->ld_update & COM1_MASK)) {
1016 + /* Locate BinM1 transition point (if any) */
1017 + if (bin_m1_early) {
1018 + /* request increment c(-1) */
1019 + temp = INCREMENT << COM1_SHIFT;
1020 + inst->ld_update = temp;
1022 + /* request decrement c(-1) */
1023 + temp = DECREMENT << COM1_SHIFT;
1024 + inst->ld_update = temp;
1027 + ld_coe_update(inst);
1028 + tx_c->bin_m1_late_early = bin_m1_early;
1034 +static int is_link_up(struct phy_device *phydev)
1038 + phy_read_mmd(phydev, MDIO_MMD_PCS, FSL_XFI_PCS_10GR_SR1);
1039 + val = phy_read_mmd(phydev, MDIO_MMD_PCS, FSL_XFI_PCS_10GR_SR1);
1041 + return (val & FSL_KR_RX_LINK_STAT_MASK) ? 1 : 0;
1044 +static int is_link_training_fail(struct phy_device *phydev)
1047 + int timeout = 100;
1049 + val = phy_read_mmd(phydev, MDIO_MMD_PMAPMD, FSL_KR_PMD_STATUS);
1050 + if (!(val & TRAIN_FAIL) && (val & RX_STAT)) {
1051 + /* check LNK_STAT for sure */
1052 + while (timeout--) {
1053 + if (is_link_up(phydev))
1056 + usleep_range(100, 500);
1063 +static int check_rx(struct phy_device *phydev)
1065 + return phy_read_mmd(phydev, MDIO_MMD_PMAPMD, FSL_KR_LP_STATUS) &
1069 +/* Coefficient values have hardware restrictions */
1070 +static int is_ld_valid(struct fsl_xgkr_inst *inst)
1072 + u32 ratio_pst1q = inst->ratio_pst1q;
1073 + u32 adpt_eq = inst->adpt_eq;
1074 + u32 ratio_preq = inst->ratio_preq;
1076 + if ((ratio_pst1q + adpt_eq + ratio_preq) > 48)
1079 + if (((ratio_pst1q + adpt_eq + ratio_preq) * 4) >=
1080 + ((adpt_eq - ratio_pst1q - ratio_preq) * 17))
1083 + if (ratio_preq > ratio_pst1q)
1086 + if (ratio_preq > 8)
1092 + if (ratio_pst1q > 16)
1098 +static int is_value_allowed(const u32 *val_table, u32 val)
1102 + for (i = 0;; i++) {
1103 + if (*(val_table + i) == VAL_INVALID)
1105 + if (*(val_table + i) == val)
1110 +static int inc_dec(struct fsl_xgkr_inst *inst, int field, int request)
1112 + u32 ld_limit[3], ld_coe[3], step[3];
1114 + ld_coe[0] = inst->ratio_pst1q;
1115 + ld_coe[1] = inst->adpt_eq;
1116 + ld_coe[2] = inst->ratio_preq;
1118 + /* Information specific to the Freescale SerDes for 10GBase-KR:
1119 + * Incrementing C(+1) means *decrementing* RATIO_PST1Q
1120 + * Incrementing C(0) means incrementing ADPT_EQ
1121 + * Incrementing C(-1) means *decrementing* RATIO_PREQ
1127 + switch (request) {
1129 + ld_limit[0] = POST_COE_MAX;
1130 + ld_limit[1] = ZERO_COE_MAX;
1131 + ld_limit[2] = PRE_COE_MAX;
1132 + if (ld_coe[field] != ld_limit[field])
1133 + ld_coe[field] += step[field];
1139 + ld_limit[0] = POST_COE_MIN;
1140 + ld_limit[1] = ZERO_COE_MIN;
1141 + ld_limit[2] = PRE_COE_MIN;
1142 + if (ld_coe[field] != ld_limit[field])
1143 + ld_coe[field] -= step[field];
1152 + if (is_ld_valid(inst)) {
1153 + /* accept new ld */
1154 + inst->ratio_pst1q = ld_coe[0];
1155 + inst->adpt_eq = ld_coe[1];
1156 + inst->ratio_preq = ld_coe[2];
1157 + /* only some values for preq and pst1q can be used.
1158 + * for preq: 0x0, 0x1, 0x3, 0x5, 0x7, 0x9, 0xb, 0xc.
1159 + * for pst1q: 0x0, 0x1, 0x3, 0x5, 0x7, 0x9, 0xb, 0xd, 0xf, 0x10.
1161 + if (!is_value_allowed((const u32 *)&preq_table, ld_coe[2])) {
1162 + dev_dbg(&inst->phydev->mdio.dev,
1163 + "preq skipped value: %d\n", ld_coe[2]);
1167 + if (!is_value_allowed((const u32 *)&pst1q_table, ld_coe[0])) {
1168 + dev_dbg(&inst->phydev->mdio.dev,
1169 + "pst1q skipped value: %d\n", ld_coe[0]);
1175 + if (request == DECREMENT)
1178 + if (request == INCREMENT)
1186 +static void min_max_updated(struct fsl_xgkr_inst *inst, int field, int new_ld)
1188 + u32 ld_coe[] = {COE_UPDATED, COE_MIN, COE_MAX};
1194 + val = ld_coe[new_ld] << COP1_SHIFT;
1198 + val = ld_coe[new_ld] << COZ_SHIFT;
1202 + val = ld_coe[new_ld] << COM1_SHIFT;
1208 + inst->ld_status &= ~mask;
1209 + inst->ld_status |= val;
1212 +static void check_request(struct fsl_xgkr_inst *inst, int request)
1214 + int cop1_req, coz_req, com_req;
1215 + int old_status, new_ld_sta;
1217 + cop1_req = (request & COP1_MASK) >> COP1_SHIFT;
1218 + coz_req = (request & COZ_MASK) >> COZ_SHIFT;
1219 + com_req = (request & COM1_MASK) >> COM1_SHIFT;
1221 + /* IEEE802.3-2008, 72.6.10.2.5
1222 + * Ensure we only act on INCREMENT/DECREMENT when we are in NOT UPDATED
1224 + old_status = inst->ld_status;
1226 + if (cop1_req && !(inst->ld_status & COP1_MASK)) {
1227 + new_ld_sta = inc_dec(inst, COE_COP1, cop1_req);
1228 + min_max_updated(inst, COE_COP1, new_ld_sta);
1231 + if (coz_req && !(inst->ld_status & COZ_MASK)) {
1232 + new_ld_sta = inc_dec(inst, COE_COZ, coz_req);
1233 + min_max_updated(inst, COE_COZ, new_ld_sta);
1236 + if (com_req && !(inst->ld_status & COM1_MASK)) {
1237 + new_ld_sta = inc_dec(inst, COE_COM, com_req);
1238 + min_max_updated(inst, COE_COM, new_ld_sta);
1241 + if (old_status != inst->ld_status)
1242 + ld_coe_status(inst);
1245 +static void preset(struct fsl_xgkr_inst *inst)
1247 + /* These are all MAX values from the IEEE802.3 perspective. */
1248 + inst->ratio_pst1q = POST_COE_MAX;
1249 + inst->adpt_eq = ZERO_COE_MAX;
1250 + inst->ratio_preq = PRE_COE_MAX;
1253 + inst->ld_status &= ~(COP1_MASK | COZ_MASK | COM1_MASK);
1254 + inst->ld_status |= COE_MAX << COP1_SHIFT |
1255 + COE_MAX << COZ_SHIFT |
1256 + COE_MAX << COM1_SHIFT;
1257 + ld_coe_status(inst);
1260 +static void initialize(struct fsl_xgkr_inst *inst)
1262 + inst->ratio_preq = RATIO_PREQ;
1263 + inst->ratio_pst1q = RATIO_PST1Q;
1264 + inst->adpt_eq = RATIO_EQ;
1267 + inst->ld_status &= ~(COP1_MASK | COZ_MASK | COM1_MASK);
1268 + inst->ld_status |= COE_UPDATED << COP1_SHIFT |
1269 + COE_UPDATED << COZ_SHIFT |
1270 + COE_UPDATED << COM1_SHIFT;
1271 + ld_coe_status(inst);
1274 +static void train_rx(struct fsl_xgkr_inst *inst)
1276 + struct phy_device *phydev = inst->phydev;
1277 + int request, old_ld_status;
1279 + /* get request from LP */
1280 + request = phy_read_mmd(phydev, MDIO_MMD_PMAPMD, FSL_KR_LP_CU) &
1282 + old_ld_status = inst->ld_status;
1284 + /* IEEE802.3-2008, 72.6.10.2.5
1285 + * Ensure we always go to NOT UDPATED for status reporting in
1286 + * response to HOLD requests.
1287 + * IEEE802.3-2008, 72.6.10.2.3.1/2
1288 + * ... but only if PRESET/INITIALIZE are not active to ensure
1289 + * we keep status until they are released.
1291 + if (!(request & (PRESET_MASK | INIT_MASK))) {
1292 + if (!(request & COP1_MASK))
1293 + inst->ld_status &= ~COP1_MASK;
1295 + if (!(request & COZ_MASK))
1296 + inst->ld_status &= ~COZ_MASK;
1298 + if (!(request & COM1_MASK))
1299 + inst->ld_status &= ~COM1_MASK;
1301 + if (old_ld_status != inst->ld_status)
1302 + ld_coe_status(inst);
1305 + /* As soon as the LP shows ready, no need to do any more updates. */
1306 + if (check_rx(phydev)) {
1307 + /* LP receiver is ready */
1308 + if (inst->ld_status & (COP1_MASK | COZ_MASK | COM1_MASK)) {
1309 + inst->ld_status &= ~(COP1_MASK | COZ_MASK | COM1_MASK);
1310 + ld_coe_status(inst);
1313 + /* IEEE802.3-2008, 72.6.10.2.3.1/2
1314 + * only act on PRESET/INITIALIZE if all status is NOT UPDATED.
1316 + if (request & (PRESET_MASK | INIT_MASK)) {
1317 + if (!(inst->ld_status &
1318 + (COP1_MASK | COZ_MASK | COM1_MASK))) {
1319 + if (request & PRESET_MASK)
1322 + if (request & INIT_MASK)
1327 + /* LP Coefficient are not in HOLD */
1328 + if (request & REQUEST_MASK)
1329 + check_request(inst, request & REQUEST_MASK);
1333 +static void xgkr_start_train(struct phy_device *phydev)
1335 + struct fsl_xgkr_inst *inst = phydev->priv;
1336 + struct tx_condition *tx_c = &inst->tx_c;
1339 + unsigned long dead_line;
1342 + init_inst(inst, 0);
1345 + for (i = 0; i < 2;) {
1346 + dead_line = jiffies + msecs_to_jiffies(500);
1347 + while (time_before(jiffies, dead_line)) {
1348 + val = phy_read_mmd(phydev, MDIO_MMD_PMAPMD,
1349 + FSL_KR_PMD_STATUS);
1350 + if (val & TRAIN_FAIL) {
1351 + /* LT failed already, reset lane to avoid
1352 + * it run into hanging, then start LT again.
1356 + } else if ((val & PMD_STATUS_SUP_STAT) &&
1357 + (val & PMD_STATUS_FRAME_LOCK))
1359 + usleep_range(100, 500);
1362 + if (!((val & PMD_STATUS_FRAME_LOCK) &&
1363 + (val & PMD_STATUS_SUP_STAT))) {
1368 + /* init process */
1371 + /* the LT should be finished in 500ms, failed or OK. */
1372 + dead_line = jiffies + msecs_to_jiffies(500);
1374 + while (time_before(jiffies, dead_line)) {
1375 + /* check if the LT is already failed */
1376 + lt_state = phy_read_mmd(phydev, MDIO_MMD_PMAPMD,
1377 + FSL_KR_PMD_STATUS);
1378 + if (lt_state & TRAIN_FAIL) {
1383 + rx_ok = check_rx(phydev);
1384 + tx_ok = tx_c->tx_complete;
1386 + if (rx_ok && tx_ok)
1395 + usleep_range(100, 500);
1399 + /* check LT result */
1400 + if (is_link_training_fail(phydev)) {
1401 + init_inst(inst, 0);
1405 + inst->state = TRAINED;
1411 +static void xgkr_state_machine(struct work_struct *work)
1413 + struct delayed_work *dwork = to_delayed_work(work);
1414 + struct fsl_xgkr_inst *inst = container_of(dwork,
1415 + struct fsl_xgkr_inst,
1417 + struct phy_device *phydev = inst->phydev;
1419 + bool needs_train = false;
1421 + mutex_lock(&phydev->lock);
1423 + switch (inst->state) {
1424 + case DETECTING_LP:
1425 + phy_read_mmd(phydev, MDIO_MMD_AN, FSL_AN_BP_STAT);
1426 + an_state = phy_read_mmd(phydev, MDIO_MMD_AN, FSL_AN_BP_STAT);
1427 + if ((an_state & KR_AN_MASK))
1428 + needs_train = true;
1431 + if (!is_link_up(phydev)) {
1432 + dev_info(&phydev->mdio.dev,
1433 + "Detect hotplug, restart training\n");
1434 + init_inst(inst, 1);
1435 + start_xgkr_an(phydev);
1436 + inst->state = DETECTING_LP;
1442 + xgkr_start_train(phydev);
1444 + mutex_unlock(&phydev->lock);
1445 + queue_delayed_work(system_power_efficient_wq, &inst->xgkr_wk,
1446 + msecs_to_jiffies(XGKR_TIMEOUT));
1449 +static int fsl_backplane_probe(struct phy_device *phydev)
1451 + struct fsl_xgkr_inst *xgkr_inst;
1452 + struct device_node *phy_node, *lane_node;
1453 + struct resource res_lane;
1459 + phy_node = phydev->mdio.dev.of_node;
1460 + bp_mode = of_property_read_string(phy_node, "backplane-mode", &bm);
1464 + if (!strcasecmp(bm, "1000base-kx")) {
1465 + bp_mode = PHY_BACKPLANE_1000BASE_KX;
1466 + } else if (!strcasecmp(bm, "10gbase-kr")) {
1467 + bp_mode = PHY_BACKPLANE_10GBASE_KR;
1469 + dev_err(&phydev->mdio.dev, "Unknown backplane-mode\n");
1473 + lane_node = of_parse_phandle(phy_node, "fsl,lane-handle", 0);
1475 + dev_err(&phydev->mdio.dev, "parse fsl,lane-handle failed\n");
1479 + ret = of_address_to_resource(lane_node, 0, &res_lane);
1481 + dev_err(&phydev->mdio.dev, "could not obtain memory map\n");
1485 + of_node_put(lane_node);
1486 + ret = of_property_read_u32_array(phy_node, "fsl,lane-reg",
1489 + dev_err(&phydev->mdio.dev, "could not get fsl,lane-reg\n");
1493 + phydev->priv = devm_ioremap_nocache(&phydev->mdio.dev,
1494 + res_lane.start + lane[0],
1496 + if (!phydev->priv) {
1497 + dev_err(&phydev->mdio.dev, "ioremap_nocache failed\n");
1501 + if (bp_mode == PHY_BACKPLANE_1000BASE_KX) {
1502 + phydev->speed = SPEED_1000;
1503 + /* configure the lane for 1000BASE-KX */
1504 + lane_set_1gkx(phydev->priv);
1508 + xgkr_inst = devm_kzalloc(&phydev->mdio.dev,
1509 + sizeof(*xgkr_inst), GFP_KERNEL);
1513 + xgkr_inst->reg_base = phydev->priv;
1514 + xgkr_inst->phydev = phydev;
1515 + phydev->priv = xgkr_inst;
1517 + if (bp_mode == PHY_BACKPLANE_10GBASE_KR) {
1518 + phydev->speed = SPEED_10000;
1519 + INIT_DELAYED_WORK(&xgkr_inst->xgkr_wk, xgkr_state_machine);
1525 +static int fsl_backplane_aneg_done(struct phy_device *phydev)
1530 +static int fsl_backplane_config_aneg(struct phy_device *phydev)
1532 + if (phydev->speed == SPEED_10000) {
1533 + phydev->supported |= SUPPORTED_10000baseKR_Full;
1534 + start_xgkr_an(phydev);
1535 + } else if (phydev->speed == SPEED_1000) {
1536 + phydev->supported |= SUPPORTED_1000baseKX_Full;
1537 + start_1gkx_an(phydev);
1540 + phydev->advertising = phydev->supported;
1541 + phydev->duplex = 1;
1546 +static int fsl_backplane_suspend(struct phy_device *phydev)
1548 + if (phydev->speed == SPEED_10000) {
1549 + struct fsl_xgkr_inst *xgkr_inst = phydev->priv;
1551 + cancel_delayed_work_sync(&xgkr_inst->xgkr_wk);
1556 +static int fsl_backplane_resume(struct phy_device *phydev)
1558 + if (phydev->speed == SPEED_10000) {
1559 + struct fsl_xgkr_inst *xgkr_inst = phydev->priv;
1561 + init_inst(xgkr_inst, 1);
1562 + queue_delayed_work(system_power_efficient_wq,
1563 + &xgkr_inst->xgkr_wk,
1564 + msecs_to_jiffies(XGKR_TIMEOUT));
1569 +static int fsl_backplane_read_status(struct phy_device *phydev)
1571 + if (is_link_up(phydev))
1579 +static struct phy_driver fsl_backplane_driver[] = {
1581 + .phy_id = FSL_PCS_PHY_ID,
1582 + .name = "Freescale Backplane",
1583 + .phy_id_mask = 0xffffffff,
1584 + .features = SUPPORTED_Backplane | SUPPORTED_Autoneg |
1586 + .probe = fsl_backplane_probe,
1587 + .aneg_done = fsl_backplane_aneg_done,
1588 + .config_aneg = fsl_backplane_config_aneg,
1589 + .read_status = fsl_backplane_read_status,
1590 + .suspend = fsl_backplane_suspend,
1591 + .resume = fsl_backplane_resume,
1595 +module_phy_driver(fsl_backplane_driver);
1597 +static struct mdio_device_id __maybe_unused freescale_tbl[] = {
1598 + { FSL_PCS_PHY_ID, 0xffffffff },
1602 +MODULE_DEVICE_TABLE(mdio, freescale_tbl);
1604 +MODULE_DESCRIPTION("Freescale Backplane driver");
1605 +MODULE_AUTHOR("Shaohui Xie <Shaohui.Xie@freescale.com>");
1606 +MODULE_LICENSE("GPL v2");
1607 --- a/drivers/net/phy/phy.c
1608 +++ b/drivers/net/phy/phy.c
1609 @@ -585,7 +585,7 @@ int phy_mii_ioctl(struct phy_device *phy
1613 - if (phydev->drv->hwtstamp)
1614 + if (phydev->drv && phydev->drv->hwtstamp)
1615 return phydev->drv->hwtstamp(phydev, ifr);
1618 @@ -610,6 +610,9 @@ static int phy_start_aneg_priv(struct ph
1625 mutex_lock(&phydev->lock);
1627 if (AUTONEG_DISABLE == phydev->autoneg)
1628 @@ -1009,7 +1012,7 @@ void phy_state_machine(struct work_struc
1630 old_state = phydev->state;
1632 - if (phydev->drv->link_change_notify)
1633 + if (phydev->drv && phydev->drv->link_change_notify)
1634 phydev->drv->link_change_notify(phydev);
1636 switch (phydev->state) {
1637 @@ -1311,6 +1314,9 @@ EXPORT_SYMBOL(phy_write_mmd_indirect);
1639 int phy_init_eee(struct phy_device *phydev, bool clk_stop_enable)
1644 /* According to 802.3az,the EEE is supported only in full duplex-mode.
1645 * Also EEE feature is active when core is operating with MII, GMII
1646 * or RGMII (all kinds). Internal PHYs are also allowed to proceed and
1647 @@ -1388,6 +1394,9 @@ EXPORT_SYMBOL(phy_init_eee);
1649 int phy_get_eee_err(struct phy_device *phydev)
1654 return phy_read_mmd_indirect(phydev, MDIO_PCS_EEE_WK_ERR, MDIO_MMD_PCS);
1656 EXPORT_SYMBOL(phy_get_eee_err);
1657 @@ -1404,6 +1413,9 @@ int phy_ethtool_get_eee(struct phy_devic
1664 /* Get Supported EEE */
1665 val = phy_read_mmd_indirect(phydev, MDIO_PCS_EEE_ABLE, MDIO_MMD_PCS);
1667 @@ -1437,6 +1449,9 @@ int phy_ethtool_set_eee(struct phy_devic
1669 int val = ethtool_adv_to_mmd_eee_adv_t(data->advertised);
1674 /* Mask prohibited EEE modes */
1675 val &= ~phydev->eee_broken_modes;
1677 @@ -1448,7 +1463,7 @@ EXPORT_SYMBOL(phy_ethtool_set_eee);
1679 int phy_ethtool_set_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol)
1681 - if (phydev->drv->set_wol)
1682 + if (phydev->drv && phydev->drv->set_wol)
1683 return phydev->drv->set_wol(phydev, wol);
1686 @@ -1457,7 +1472,7 @@ EXPORT_SYMBOL(phy_ethtool_set_wol);
1688 void phy_ethtool_get_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol)
1690 - if (phydev->drv->get_wol)
1691 + if (phydev->drv && phydev->drv->get_wol)
1692 phydev->drv->get_wol(phydev, wol);
1694 EXPORT_SYMBOL(phy_ethtool_get_wol);
1695 --- a/drivers/net/phy/phy_device.c
1696 +++ b/drivers/net/phy/phy_device.c
1697 @@ -1046,7 +1046,7 @@ int phy_suspend(struct phy_device *phyde
1701 - if (phydrv->suspend)
1702 + if (phydev->drv && phydrv->suspend)
1703 ret = phydrv->suspend(phydev);
1706 @@ -1063,7 +1063,7 @@ int phy_resume(struct phy_device *phydev
1707 struct phy_driver *phydrv = to_phy_driver(phydev->mdio.dev.driver);
1710 - if (phydrv->resume)
1711 + if (phydev->drv && phydrv->resume)
1712 ret = phydrv->resume(phydev);
1715 @@ -1726,7 +1726,7 @@ static int phy_remove(struct device *dev
1716 phydev->state = PHY_DOWN;
1717 mutex_unlock(&phydev->lock);
1719 - if (phydev->drv->remove)
1720 + if (phydev->drv && phydev->drv->remove)
1721 phydev->drv->remove(phydev);
1724 --- a/drivers/net/phy/swphy.c
1725 +++ b/drivers/net/phy/swphy.c
1726 @@ -77,6 +77,7 @@ static const struct swmii_regs duplex[]
1727 static int swphy_decode_speed(int speed)
1732 return SWMII_SPEED_1000;
1734 --- a/include/linux/phy.h
1735 +++ b/include/linux/phy.h
1736 @@ -81,6 +81,7 @@ typedef enum {
1737 PHY_INTERFACE_MODE_MOCA,
1738 PHY_INTERFACE_MODE_QSGMII,
1739 PHY_INTERFACE_MODE_TRGMII,
1740 + PHY_INTERFACE_MODE_SGMII_2500,
1741 PHY_INTERFACE_MODE_MAX,
1744 @@ -791,6 +792,9 @@ int phy_stop_interrupts(struct phy_devic
1746 static inline int phy_read_status(struct phy_device *phydev)
1751 return phydev->drv->read_status(phydev);