1*a2443fd1SAndrew Lunn // SPDX-License-Identifier: GPL-2.0
2f61687c0SShaohui Xie /*
3f61687c0SShaohui Xie * Driver for Teranetics PHY
4f61687c0SShaohui Xie *
5f61687c0SShaohui Xie * Author: Shaohui Xie <Shaohui.Xie@freescale.com>
6f61687c0SShaohui Xie *
7f61687c0SShaohui Xie * Copyright 2015 Freescale Semiconductor, Inc.
8f61687c0SShaohui Xie */
9f61687c0SShaohui Xie
10f61687c0SShaohui Xie #include <linux/kernel.h>
11f61687c0SShaohui Xie #include <linux/module.h>
12f61687c0SShaohui Xie #include <linux/mii.h>
13f61687c0SShaohui Xie #include <linux/ethtool.h>
14f61687c0SShaohui Xie #include <linux/mdio.h>
15f61687c0SShaohui Xie #include <linux/phy.h>
16f61687c0SShaohui Xie
17f61687c0SShaohui Xie MODULE_DESCRIPTION("Teranetics PHY driver");
18f61687c0SShaohui Xie MODULE_AUTHOR("Shaohui Xie <Shaohui.Xie@freescale.com>");
19f61687c0SShaohui Xie MODULE_LICENSE("GPL v2");
20f61687c0SShaohui Xie
21f61687c0SShaohui Xie #define PHY_ID_TN2020 0x00a19410
22f61687c0SShaohui Xie #define MDIO_PHYXS_LNSTAT_SYNC0 0x0001
23f61687c0SShaohui Xie #define MDIO_PHYXS_LNSTAT_SYNC1 0x0002
24f61687c0SShaohui Xie #define MDIO_PHYXS_LNSTAT_SYNC2 0x0004
25f61687c0SShaohui Xie #define MDIO_PHYXS_LNSTAT_SYNC3 0x0008
26f61687c0SShaohui Xie #define MDIO_PHYXS_LNSTAT_ALIGN 0x1000
27f61687c0SShaohui Xie
28f61687c0SShaohui Xie #define MDIO_PHYXS_LANE_READY (MDIO_PHYXS_LNSTAT_SYNC0 | \
29f61687c0SShaohui Xie MDIO_PHYXS_LNSTAT_SYNC1 | \
30f61687c0SShaohui Xie MDIO_PHYXS_LNSTAT_SYNC2 | \
31f61687c0SShaohui Xie MDIO_PHYXS_LNSTAT_SYNC3 | \
32f61687c0SShaohui Xie MDIO_PHYXS_LNSTAT_ALIGN)
33f61687c0SShaohui Xie
teranetics_aneg_done(struct phy_device * phydev)34f61687c0SShaohui Xie static int teranetics_aneg_done(struct phy_device *phydev)
35f61687c0SShaohui Xie {
3694724d10SShaohui Xie /* auto negotiation state can only be checked when using copper
3794724d10SShaohui Xie * port, if using fiber port, just lie it's done.
3894724d10SShaohui Xie */
39aebc78a4SFlorian Fainelli if (!phy_read_mmd(phydev, MDIO_MMD_VEND1, 93))
40aebc78a4SFlorian Fainelli return genphy_c45_aneg_done(phydev);
41f61687c0SShaohui Xie
4294724d10SShaohui Xie return 1;
4394724d10SShaohui Xie }
4494724d10SShaohui Xie
teranetics_read_status(struct phy_device * phydev)45f61687c0SShaohui Xie static int teranetics_read_status(struct phy_device *phydev)
46f61687c0SShaohui Xie {
47f61687c0SShaohui Xie int reg;
48f61687c0SShaohui Xie
49f61687c0SShaohui Xie phydev->link = 1;
50f61687c0SShaohui Xie
51f61687c0SShaohui Xie phydev->speed = SPEED_10000;
52f61687c0SShaohui Xie phydev->duplex = DUPLEX_FULL;
53f61687c0SShaohui Xie
54f61687c0SShaohui Xie if (!phy_read_mmd(phydev, MDIO_MMD_VEND1, 93)) {
55f61687c0SShaohui Xie reg = phy_read_mmd(phydev, MDIO_MMD_PHYXS, MDIO_PHYXS_LNSTAT);
56f61687c0SShaohui Xie if (reg < 0 ||
57f61687c0SShaohui Xie !((reg & MDIO_PHYXS_LANE_READY) == MDIO_PHYXS_LANE_READY)) {
58f61687c0SShaohui Xie phydev->link = 0;
59f61687c0SShaohui Xie return 0;
60f61687c0SShaohui Xie }
61f61687c0SShaohui Xie
62f61687c0SShaohui Xie reg = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_STAT1);
63f61687c0SShaohui Xie if (reg < 0 || !(reg & MDIO_STAT1_LSTATUS))
64f61687c0SShaohui Xie phydev->link = 0;
65f61687c0SShaohui Xie }
66f61687c0SShaohui Xie
67f61687c0SShaohui Xie return 0;
68f61687c0SShaohui Xie }
69f61687c0SShaohui Xie
teranetics_match_phy_device(struct phy_device * phydev)70f61687c0SShaohui Xie static int teranetics_match_phy_device(struct phy_device *phydev)
71f61687c0SShaohui Xie {
72f61687c0SShaohui Xie return phydev->c45_ids.device_ids[3] == PHY_ID_TN2020;
73f61687c0SShaohui Xie }
74f61687c0SShaohui Xie
75f61687c0SShaohui Xie static struct phy_driver teranetics_driver[] = {
76f61687c0SShaohui Xie {
77f61687c0SShaohui Xie .phy_id = PHY_ID_TN2020,
78f61687c0SShaohui Xie .phy_id_mask = 0xffffffff,
79f61687c0SShaohui Xie .name = "Teranetics TN2020",
8040f89ebfSCamelia Groza .features = PHY_10GBIT_FEATURES,
81f61687c0SShaohui Xie .aneg_done = teranetics_aneg_done,
82aebc78a4SFlorian Fainelli .config_aneg = gen10g_config_aneg,
83f61687c0SShaohui Xie .read_status = teranetics_read_status,
84f61687c0SShaohui Xie .match_phy_device = teranetics_match_phy_device,
85f61687c0SShaohui Xie },
86f61687c0SShaohui Xie };
87f61687c0SShaohui Xie
88fb0801dcSAxel Lin module_phy_driver(teranetics_driver);
89f61687c0SShaohui Xie
90f61687c0SShaohui Xie static struct mdio_device_id __maybe_unused teranetics_tbl[] = {
91f61687c0SShaohui Xie { PHY_ID_TN2020, 0xffffffff },
92f61687c0SShaohui Xie { }
93f61687c0SShaohui Xie };
94f61687c0SShaohui Xie
95f61687c0SShaohui Xie MODULE_DEVICE_TABLE(mdio, teranetics_tbl);
96