1b59f70cea229ef0f675db21463f95e27c89cc09
[openwrt/staging/dedeckeh.git] / target / linux / ramips / patches-5.10 / 710-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 @@ -20,6 +20,8 @@
18 #include <linux/regulator/driver.h>
19 #include <linux/regulator/consumer.h>
20 #include <dt-bindings/net/qca-ar803x.h>
21 +#include <linux/sfp.h>
22 +#include <linux/phylink.h>
23
24 #define AT803X_SPECIFIC_FUNCTION_CONTROL 0x10
25 #define AT803X_SFC_ASSERT_CRS BIT(11)
26 @@ -82,9 +84,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_ANALOG_TEST_CTRL 0x00
44 #define QCA8327_DEBUG_MANU_CTRL_EN BIT(2)
45 @@ -629,12 +640,75 @@ static int at803x_parse_dt(struct phy_de
46 return 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 +
105 static int at803x_probe(struct phy_device *phydev)
106 {
107 struct device *dev = &phydev->mdio.dev;
108 struct at803x_priv *priv;
109 int ret;
110
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 return -ENOMEM;
121 @@ -651,6 +725,7 @@ static int at803x_probe(struct phy_devic
122 return ret;
123 }
124
125 +#if 0
126 /* Some bootloaders leave the fiber page selected.
127 * Switch to the copper page, as otherwise we read
128 * the PHY capabilities from the fiber side.
129 @@ -662,6 +737,7 @@ static int at803x_probe(struct phy_devic
130 if (ret)
131 goto err;
132 }
133 +#endif
134
135 return 0;
136
137 @@ -841,6 +917,10 @@ static int at803x_read_status(struct phy
138 {
139 int ss, err, old_link = phydev->link;
140
141 + /* Handle (Fiber) SGMII to RGMII mode */
142 + if (at803x_mode(phydev) == AT803X_MODE_FIBER)
143 + return genphy_c37_read_status(phydev);
144 +
145 /* Update the link, but return if there was an error */
146 err = genphy_update_link(phydev);
147 if (err)
148 @@ -941,6 +1021,12 @@ static int at803x_config_aneg(struct phy
149 {
150 int ret;
151
152 + /* Handle (Fiber) SerDes to RGMII mode */
153 + if (at803x_mode(phydev) == AT803X_MODE_FIBER) {
154 + pr_warn("%s: fiber\n", __func__);
155 + return genphy_c37_config_aneg(phydev);
156 + }
157 +
158 ret = at803x_config_mdix(phydev, phydev->mdix_ctrl);
159 if (ret < 0)
160 return ret;
161 @@ -1040,6 +1126,7 @@ static int at803x_get_features(struct ph
162 if (err)
163 return err;
164
165 +#if 0
166 if (!at803x_match_phy_id(phydev, ATH8031_PHY_ID))
167 return 0;
168
169 @@ -1057,6 +1144,7 @@ static int at803x_get_features(struct ph
170 */
171 linkmode_clear_bit(ETHTOOL_LINK_MODE_1000baseX_Full_BIT,
172 phydev->supported);
173 +#endif
174 return 0;
175 }
176
177 @@ -1381,6 +1469,7 @@ static struct phy_driver at803x_driver[]
178 /* Qualcomm Atheros AR8031/AR8033 */
179 PHY_ID_MATCH_EXACT(ATH8031_PHY_ID),
180 .name = "Qualcomm Atheros AR8031/AR8033",
181 + .config_aneg = at803x_config_aneg,
182 .flags = PHY_POLL_CABLE_TEST,
183 .probe = at803x_probe,
184 .remove = at803x_remove,