64bc48fe7a489cd3acea026645571ab862dd6e42
[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 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
19 @@ -14,6 +14,8 @@
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>
25
26 #define AT803X_SPECIFIC_STATUS 0x11
27 #define AT803X_SS_SPEED_MASK (3 << 14)
28 @@ -53,9 +55,18 @@
29
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
36
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)
44
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);
49 }
50
51 +static int at803x_mode(struct phy_device *phydev)
52 +{
53 + int mode;
54 +
55 + mode = phy_read(phydev, AT803X_REG_CHIP_CONFIG) & AT803X_MODE_CFG_MASK;
56 +
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;
61 +}
62 +
63 +static int at803x_sfp_insert(void *upstream, const struct sfp_eeprom_id *id)
64 +{
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;
69 +
70 + phylink_set(at803x_support, 1000baseX_Full);
71 + /* AT803x only support 1000baseX but SGMII works fine when module runs
72 + * at 1Gbit.
73 + */
74 + phylink_set(at803x_support, 1000baseT_Full);
75 +
76 + sfp_parse_support(phydev->sfp_bus, id, support);
77 +
78 + // Limit to interfaces that both sides support
79 + linkmode_and(support, support, at803x_support);
80 +
81 + if (linkmode_empty(support))
82 + goto unsupported_mode;
83 +
84 + iface = sfp_select_interface(phydev->sfp_bus, support);
85 +
86 + if (iface != PHY_INTERFACE_MODE_SGMII &&
87 + iface != PHY_INTERFACE_MODE_1000BASEX)
88 + goto unsupported_mode;
89 +
90 + dev_info(&phydev->mdio.dev, "SFP interface %s", phy_modes(iface));
91 +
92 + return 0;
93 +
94 +unsupported_mode:
95 + dev_info(&phydev->mdio.dev, "incompatible SFP module inserted;"
96 + "Only SGMII at 1Gbit/1000BASEX are supported!\n");
97 + return -EINVAL;
98 +}
99 +
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,
104 +};
105 +
106 static int at803x_probe(struct phy_device *phydev)
107 {
108 struct device *dev = &phydev->mdio.dev;
109 struct at803x_priv *priv;
110 + int ret;
111 +
112 + if (at803x_mode(phydev) == AT803X_MODE_FIBER) {
113 + ret = phy_sfp_probe(phydev, &at803x_sfp_ops);
114 + if (ret < 0)
115 + return ret;
116 + }
117
118 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
119 if (!priv)
120 @@ -394,6 +467,10 @@ static int at803x_read_status(struct phy_device *phydev)
121 {
122 int ss, err, old_link = phydev->link;
123
124 + /* Handle (Fiber) SGMII to RGMII mode */
125 + if (at803x_mode(phydev) == AT803X_MODE_FIBER)
126 + return genphy_c37_read_status(phydev);
127 +
128 /* Update the link, but return if there was an error */
129 err = genphy_update_link(phydev);
130 if (err)
131 @@ -448,6 +525,19 @@ static int at803x_read_status(struct phy_device *phydev)
132 return 0;
133 }
134
135 +static int at803x_config_aneg(struct phy_device *phydev)
136 +{
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);
141 + }
142 +
143 + pr_warn("%s: enter\n", __func__);
144 +
145 + return genphy_config_aneg(phydev);
146 +}
147 +
148 static struct phy_driver at803x_driver[] = {
149 {
150 /* ATHEROS 8035 */
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,