1 From 924453aa9d2324e5611f8e2b71df746d8f0c79f1 Mon Sep 17 00:00:00 2001
2 From: =?UTF-8?q?Ren=C3=A9=20van=20Dorst?= <opensource@vdorst.com>
3 Date: Fri, 13 Nov 2020 16:11:32 +0100
4 Subject: [PATCH] net: phy: at803x: add support for SFP module in
7 Content-Type: text/plain; charset=UTF-8
8 Content-Transfer-Encoding: 8bit
10 Signed-off-by: René van Dorst <opensource@vdorst.com>
12 drivers/net/phy/at803x.c | 91 ++++++++++++++++++++++++++++++++++++++++
13 1 file changed, 91 insertions(+)
15 diff --git a/drivers/net/phy/at803x.c b/drivers/net/phy/at803x.c
16 index 31cd7d8a5a1b5..134c894ccf800 100644
17 --- a/drivers/net/phy/at803x.c
18 +++ b/drivers/net/phy/at803x.c
20 #include <linux/etherdevice.h>
21 #include <linux/of_gpio.h>
22 #include <linux/gpio/consumer.h>
23 +#include <linux/sfp.h>
24 +#include <linux/phylink.h>
26 #define AT803X_SPECIFIC_STATUS 0x11
27 #define AT803X_SS_SPEED_MASK (3 << 14)
30 #define AT803X_MODE_CFG_MASK 0x0F
31 #define AT803X_MODE_CFG_SGMII 0x01
32 +#define AT803X_MODE_CFG_BX1000_RGMII_50 0x02
33 +#define AT803X_MODE_CFG_BX1000_RGMII_75 0x03
34 +#define AT803X_MODE_FIBER 0x01
35 +#define AT803X_MODE_COPPER 0x00
37 #define AT803X_PSSR 0x11 /*PHY-Specific Status Register*/
38 #define AT803X_PSSR_MR_AN_COMPLETE 0x0200
39 +#define PSSR_LINK BIT(10)
40 +#define PSSR_SYNC_STATUS BIT(8)
41 +#define PSSR_DUPLEX BIT(13)
42 +#define PSSR_SPEED_1000 BIT(15)
43 +#define PSSR_SPEED_100 BIT(14)
45 #define AT803X_DEBUG_REG_0 0x00
46 #define AT803X_DEBUG_RX_CLK_DLY_EN BIT(15)
47 @@ -243,10 +254,72 @@ static int at803x_resume(struct phy_device *phydev)
48 return phy_modify(phydev, MII_BMCR, BMCR_PDOWN | BMCR_ISOLATE, 0);
51 +static int at803x_mode(struct phy_device *phydev)
55 + mode = phy_read(phydev, AT803X_REG_CHIP_CONFIG) & AT803X_MODE_CFG_MASK;
57 + if (mode == AT803X_MODE_CFG_BX1000_RGMII_50 ||
58 + mode == AT803X_MODE_CFG_BX1000_RGMII_75)
59 + return AT803X_MODE_FIBER;
60 + return AT803X_MODE_COPPER;
63 +static int at803x_sfp_insert(void *upstream, const struct sfp_eeprom_id *id)
65 + __ETHTOOL_DECLARE_LINK_MODE_MASK(at803x_support) = { 0, };
66 + __ETHTOOL_DECLARE_LINK_MODE_MASK(support) = { 0, };
67 + struct phy_device *phydev = upstream;
68 + phy_interface_t iface;
70 + phylink_set(at803x_support, 1000baseX_Full);
71 + /* AT803x only support 1000baseX but SGMII works fine when module runs
74 + phylink_set(at803x_support, 1000baseT_Full);
76 + sfp_parse_support(phydev->sfp_bus, id, support);
78 + // Limit to interfaces that both sides support
79 + linkmode_and(support, support, at803x_support);
81 + if (linkmode_empty(support))
82 + goto unsupported_mode;
84 + iface = sfp_select_interface(phydev->sfp_bus, support);
86 + if (iface != PHY_INTERFACE_MODE_SGMII &&
87 + iface != PHY_INTERFACE_MODE_1000BASEX)
88 + goto unsupported_mode;
90 + dev_info(&phydev->mdio.dev, "SFP interface %s", phy_modes(iface));
95 + dev_info(&phydev->mdio.dev, "incompatible SFP module inserted;"
96 + "Only SGMII at 1Gbit/1000BASEX are supported!\n");
100 +static const struct sfp_upstream_ops at803x_sfp_ops = {
101 + .attach = phy_sfp_attach,
102 + .detach = phy_sfp_detach,
103 + .module_insert = at803x_sfp_insert,
106 static int at803x_probe(struct phy_device *phydev)
108 struct device *dev = &phydev->mdio.dev;
109 struct at803x_priv *priv;
112 + if (at803x_mode(phydev) == AT803X_MODE_FIBER) {
113 + ret = phy_sfp_probe(phydev, &at803x_sfp_ops);
118 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
120 @@ -394,6 +467,10 @@ static int at803x_read_status(struct phy_device *phydev)
122 int ss, err, old_link = phydev->link;
124 + /* Handle (Fiber) SGMII to RGMII mode */
125 + if (at803x_mode(phydev) == AT803X_MODE_FIBER)
126 + return genphy_c37_read_status(phydev);
128 /* Update the link, but return if there was an error */
129 err = genphy_update_link(phydev);
131 @@ -448,6 +525,19 @@ static int at803x_read_status(struct phy_device *phydev)
135 +static int at803x_config_aneg(struct phy_device *phydev)
137 + /* Handle (Fiber) SerDes to RGMII mode */
138 + if (at803x_mode(phydev) == AT803X_MODE_FIBER) {
139 + pr_warn("%s: fiber\n", __func__);
140 + return genphy_c37_config_aneg(phydev);
143 + pr_warn("%s: enter\n", __func__);
145 + return genphy_config_aneg(phydev);
148 static struct phy_driver at803x_driver[] = {
151 @@ -491,6 +581,7 @@ static struct phy_driver at803x_driver[] = {
152 .suspend = at803x_suspend,
153 .resume = at803x_resume,
154 /* PHY_GBIT_FEATURES */
155 + .config_aneg = at803x_config_aneg,
156 .read_status = at803x_read_status,
157 .aneg_done = at803x_aneg_done,
158 .ack_interrupt = &at803x_ack_interrupt,