95411211b2d3f5951431f0396871f5e182fc3667
[openwrt/staging/wigyori.git] / target / linux / ramips / patches-5.4 / 991-at803x.patch
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
5 RGMII-to-x-base mode
6 MIME-Version: 1.0
7 Content-Type: text/plain; charset=UTF-8
8 Content-Transfer-Encoding: 8bit
9
10 Signed-off-by: René van Dorst <opensource@vdorst.com>
11 ---
12 drivers/net/phy/at803x.c | 91 ++++++++++++++++++++++++++++++++++++++++
13 1 file changed, 91 insertions(+)
14
15 --- a/drivers/net/phy/at803x.c
16 +++ b/drivers/net/phy/at803x.c
17 @@ -14,6 +14,8 @@
18 #include <linux/etherdevice.h>
19 #include <linux/of_gpio.h>
20 #include <linux/gpio/consumer.h>
21 +#include <linux/sfp.h>
22 +#include <linux/phylink.h>
23
24 #define AT803X_SPECIFIC_STATUS 0x11
25 #define AT803X_SS_SPEED_MASK (3 << 14)
26 @@ -53,9 +55,18 @@
27
28 #define AT803X_MODE_CFG_MASK 0x0F
29 #define AT803X_MODE_CFG_SGMII 0x01
30 +#define AT803X_MODE_CFG_BX1000_RGMII_50 0x02
31 +#define AT803X_MODE_CFG_BX1000_RGMII_75 0x03
32 +#define AT803X_MODE_FIBER 0x01
33 +#define AT803X_MODE_COPPER 0x00
34
35 #define AT803X_PSSR 0x11 /*PHY-Specific Status Register*/
36 #define AT803X_PSSR_MR_AN_COMPLETE 0x0200
37 +#define PSSR_LINK BIT(10)
38 +#define PSSR_SYNC_STATUS BIT(8)
39 +#define PSSR_DUPLEX BIT(13)
40 +#define PSSR_SPEED_1000 BIT(15)
41 +#define PSSR_SPEED_100 BIT(14)
42
43 #define AT803X_DEBUG_REG_0 0x00
44 #define AT803X_DEBUG_RX_CLK_DLY_EN BIT(15)
45 @@ -243,10 +254,72 @@ static int at803x_resume(struct phy_devi
46 return phy_modify(phydev, MII_BMCR, BMCR_PDOWN | BMCR_ISOLATE, 0);
47 }
48
49 +static int at803x_mode(struct phy_device *phydev)
50 +{
51 + int mode;
52 +
53 + mode = phy_read(phydev, AT803X_REG_CHIP_CONFIG) & AT803X_MODE_CFG_MASK;
54 +
55 + if (mode == AT803X_MODE_CFG_BX1000_RGMII_50 ||
56 + mode == AT803X_MODE_CFG_BX1000_RGMII_75)
57 + return AT803X_MODE_FIBER;
58 + return AT803X_MODE_COPPER;
59 +}
60 +
61 +static int at803x_sfp_insert(void *upstream, const struct sfp_eeprom_id *id)
62 +{
63 + __ETHTOOL_DECLARE_LINK_MODE_MASK(at803x_support) = { 0, };
64 + __ETHTOOL_DECLARE_LINK_MODE_MASK(support) = { 0, };
65 + struct phy_device *phydev = upstream;
66 + phy_interface_t iface;
67 +
68 + phylink_set(at803x_support, 1000baseX_Full);
69 + /* AT803x only support 1000baseX but SGMII works fine when module runs
70 + * at 1Gbit.
71 + */
72 + phylink_set(at803x_support, 1000baseT_Full);
73 +
74 + sfp_parse_support(phydev->sfp_bus, id, support);
75 +
76 + // Limit to interfaces that both sides support
77 + linkmode_and(support, support, at803x_support);
78 +
79 + if (linkmode_empty(support))
80 + goto unsupported_mode;
81 +
82 + iface = sfp_select_interface(phydev->sfp_bus, support);
83 +
84 + if (iface != PHY_INTERFACE_MODE_SGMII &&
85 + iface != PHY_INTERFACE_MODE_1000BASEX)
86 + goto unsupported_mode;
87 +
88 + dev_info(&phydev->mdio.dev, "SFP interface %s", phy_modes(iface));
89 +
90 + return 0;
91 +
92 +unsupported_mode:
93 + dev_info(&phydev->mdio.dev, "incompatible SFP module inserted;"
94 + "Only SGMII at 1Gbit/1000BASEX are supported!\n");
95 + return -EINVAL;
96 +}
97 +
98 +static const struct sfp_upstream_ops at803x_sfp_ops = {
99 + .attach = phy_sfp_attach,
100 + .detach = phy_sfp_detach,
101 + .module_insert = at803x_sfp_insert,
102 +};
103 +
104 static int at803x_probe(struct phy_device *phydev)
105 {
106 struct device *dev = &phydev->mdio.dev;
107 struct at803x_priv *priv;
108 + int ret;
109 +
110 + if (at803x_mode(phydev) == AT803X_MODE_FIBER) {
111 + ret = phy_sfp_probe(phydev, &at803x_sfp_ops);
112 + if (ret < 0)
113 + return ret;
114 + }
115
116 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
117 if (!priv)
118 @@ -394,6 +467,10 @@ static int at803x_read_status(struct phy
119 {
120 int ss, err, old_link = phydev->link;
121
122 + /* Handle (Fiber) SGMII to RGMII mode */
123 + if (at803x_mode(phydev) == AT803X_MODE_FIBER)
124 + return genphy_c37_read_status(phydev);
125 +
126 /* Update the link, but return if there was an error */
127 err = genphy_update_link(phydev);
128 if (err)
129 @@ -448,6 +525,19 @@ static int at803x_read_status(struct phy
130 return 0;
131 }
132
133 +static int at803x_config_aneg(struct phy_device *phydev)
134 +{
135 + /* Handle (Fiber) SerDes to RGMII mode */
136 + if (at803x_mode(phydev) == AT803X_MODE_FIBER) {
137 + pr_warn("%s: fiber\n", __func__);
138 + return genphy_c37_config_aneg(phydev);
139 + }
140 +
141 + pr_warn("%s: enter\n", __func__);
142 +
143 + return genphy_config_aneg(phydev);
144 +}
145 +
146 static struct phy_driver at803x_driver[] = {
147 {
148 /* ATHEROS 8035 */
149 @@ -461,6 +551,7 @@ static struct phy_driver at803x_driver[]
150 .suspend = at803x_suspend,
151 .resume = at803x_resume,
152 /* PHY_GBIT_FEATURES */
153 + .config_aneg = at803x_config_aneg,
154 .read_status = at803x_read_status,
155 .ack_interrupt = at803x_ack_interrupt,
156 .config_intr = at803x_config_intr,