blob: c7f91934cf82dd29d57538ecf26112b8e72ff973 [file] [log] [blame]
Andrew Lunna2443fd2019-01-21 19:05:50 +01001// SPDX-License-Identifier: GPL-2.0+
Matus Ujhelyi0ca71112012-10-14 19:07:16 +00002/*
3 * drivers/net/phy/at803x.c
4 *
Michael Walle96c36712019-11-06 23:36:16 +01005 * Driver for Qualcomm Atheros AR803x PHY
Matus Ujhelyi0ca71112012-10-14 19:07:16 +00006 *
7 * Author: Matus Ujhelyi <ujhelyi.m@gmail.com>
Matus Ujhelyi0ca71112012-10-14 19:07:16 +00008 */
9
10#include <linux/phy.h>
11#include <linux/module.h>
12#include <linux/string.h>
13#include <linux/netdevice.h>
14#include <linux/etherdevice.h>
Michael Walle6cb75762020-05-13 22:38:07 +020015#include <linux/ethtool_netlink.h>
Daniel Mack13a56b42014-06-18 11:01:43 +020016#include <linux/of_gpio.h>
Michael Walle2f664822019-11-06 23:36:14 +010017#include <linux/bitfield.h>
Daniel Mack13a56b42014-06-18 11:01:43 +020018#include <linux/gpio/consumer.h>
Michael Walle2f664822019-11-06 23:36:14 +010019#include <linux/regulator/of_regulator.h>
20#include <linux/regulator/driver.h>
21#include <linux/regulator/consumer.h>
22#include <dt-bindings/net/qca-ar803x.h>
Matus Ujhelyi0ca71112012-10-14 19:07:16 +000023
Oleksij Rempel7dce80c2020-07-19 10:05:30 +020024#define AT803X_SPECIFIC_FUNCTION_CONTROL 0x10
25#define AT803X_SFC_ASSERT_CRS BIT(11)
26#define AT803X_SFC_FORCE_LINK BIT(10)
27#define AT803X_SFC_MDI_CROSSOVER_MODE_M GENMASK(6, 5)
28#define AT803X_SFC_AUTOMATIC_CROSSOVER 0x3
29#define AT803X_SFC_MANUAL_MDIX 0x1
30#define AT803X_SFC_MANUAL_MDI 0x0
31#define AT803X_SFC_SQE_TEST BIT(2)
32#define AT803X_SFC_POLARITY_REVERSAL BIT(1)
33#define AT803X_SFC_DISABLE_JABBER BIT(0)
34
Russell King06d5f342019-10-04 17:06:14 +010035#define AT803X_SPECIFIC_STATUS 0x11
36#define AT803X_SS_SPEED_MASK (3 << 14)
37#define AT803X_SS_SPEED_1000 (2 << 14)
38#define AT803X_SS_SPEED_100 (1 << 14)
39#define AT803X_SS_SPEED_10 (0 << 14)
40#define AT803X_SS_DUPLEX BIT(13)
41#define AT803X_SS_SPEED_DUPLEX_RESOLVED BIT(11)
42#define AT803X_SS_MDIX BIT(6)
43
Matus Ujhelyi0ca71112012-10-14 19:07:16 +000044#define AT803X_INTR_ENABLE 0x12
Martin Blumenstingle6e4a552016-01-15 01:55:24 +010045#define AT803X_INTR_ENABLE_AUTONEG_ERR BIT(15)
46#define AT803X_INTR_ENABLE_SPEED_CHANGED BIT(14)
47#define AT803X_INTR_ENABLE_DUPLEX_CHANGED BIT(13)
48#define AT803X_INTR_ENABLE_PAGE_RECEIVED BIT(12)
49#define AT803X_INTR_ENABLE_LINK_FAIL BIT(11)
50#define AT803X_INTR_ENABLE_LINK_SUCCESS BIT(10)
51#define AT803X_INTR_ENABLE_WIRESPEED_DOWNGRADE BIT(5)
52#define AT803X_INTR_ENABLE_POLARITY_CHANGED BIT(1)
53#define AT803X_INTR_ENABLE_WOL BIT(0)
54
Matus Ujhelyi0ca71112012-10-14 19:07:16 +000055#define AT803X_INTR_STATUS 0x13
Martin Blumenstingla46bd632016-01-15 01:55:23 +010056
Daniel Mack13a56b42014-06-18 11:01:43 +020057#define AT803X_SMART_SPEED 0x14
Michael Wallecde0f4f2020-04-28 23:15:02 +020058#define AT803X_SMART_SPEED_ENABLE BIT(5)
59#define AT803X_SMART_SPEED_RETRY_LIMIT_MASK GENMASK(4, 2)
60#define AT803X_SMART_SPEED_BYPASS_TIMER BIT(1)
Michael Walle6cb75762020-05-13 22:38:07 +020061#define AT803X_CDT 0x16
62#define AT803X_CDT_MDI_PAIR_MASK GENMASK(9, 8)
63#define AT803X_CDT_ENABLE_TEST BIT(0)
64#define AT803X_CDT_STATUS 0x1c
65#define AT803X_CDT_STATUS_STAT_NORMAL 0
66#define AT803X_CDT_STATUS_STAT_SHORT 1
67#define AT803X_CDT_STATUS_STAT_OPEN 2
68#define AT803X_CDT_STATUS_STAT_FAIL 3
69#define AT803X_CDT_STATUS_STAT_MASK GENMASK(9, 8)
70#define AT803X_CDT_STATUS_DELTA_TIME_MASK GENMASK(7, 0)
Daniel Mack13a56b42014-06-18 11:01:43 +020071#define AT803X_LED_CONTROL 0x18
Martin Blumenstingla46bd632016-01-15 01:55:23 +010072
Matus Ujhelyi0ca71112012-10-14 19:07:16 +000073#define AT803X_DEVICE_ADDR 0x03
74#define AT803X_LOC_MAC_ADDR_0_15_OFFSET 0x804C
75#define AT803X_LOC_MAC_ADDR_16_31_OFFSET 0x804B
76#define AT803X_LOC_MAC_ADDR_32_47_OFFSET 0x804A
Zefir Kurtisif62265b2016-10-24 12:40:54 +020077#define AT803X_REG_CHIP_CONFIG 0x1f
78#define AT803X_BT_BX_REG_SEL 0x8000
Martin Blumenstingla46bd632016-01-15 01:55:23 +010079
Mugunthan V N1ca6d1b2013-06-03 20:10:06 +000080#define AT803X_DEBUG_ADDR 0x1D
81#define AT803X_DEBUG_DATA 0x1E
Martin Blumenstingla46bd632016-01-15 01:55:23 +010082
Zefir Kurtisif62265b2016-10-24 12:40:54 +020083#define AT803X_MODE_CFG_MASK 0x0F
84#define AT803X_MODE_CFG_SGMII 0x01
85
86#define AT803X_PSSR 0x11 /*PHY-Specific Status Register*/
87#define AT803X_PSSR_MR_AN_COMPLETE 0x0200
88
Martin Blumenstingl2e5f9f22016-01-15 01:55:22 +010089#define AT803X_DEBUG_REG_0 0x00
90#define AT803X_DEBUG_RX_CLK_DLY_EN BIT(15)
Martin Blumenstingla46bd632016-01-15 01:55:23 +010091
Martin Blumenstingl2e5f9f22016-01-15 01:55:22 +010092#define AT803X_DEBUG_REG_5 0x05
93#define AT803X_DEBUG_TX_CLK_DLY_EN BIT(8)
Matus Ujhelyi0ca71112012-10-14 19:07:16 +000094
Michael Walle2f664822019-11-06 23:36:14 +010095#define AT803X_DEBUG_REG_1F 0x1F
96#define AT803X_DEBUG_PLL_ON BIT(2)
97#define AT803X_DEBUG_RGMII_1V8 BIT(3)
98
99/* AT803x supports either the XTAL input pad, an internal PLL or the
100 * DSP as clock reference for the clock output pad. The XTAL reference
101 * is only used for 25 MHz output, all other frequencies need the PLL.
102 * The DSP as a clock reference is used in synchronous ethernet
103 * applications.
104 *
105 * By default the PLL is only enabled if there is a link. Otherwise
106 * the PHY will go into low power state and disabled the PLL. You can
107 * set the PLL_ON bit (see debug register 0x1f) to keep the PLL always
108 * enabled.
109 */
110#define AT803X_MMD7_CLK25M 0x8016
111#define AT803X_CLK_OUT_MASK GENMASK(4, 2)
112#define AT803X_CLK_OUT_25MHZ_XTAL 0
113#define AT803X_CLK_OUT_25MHZ_DSP 1
114#define AT803X_CLK_OUT_50MHZ_PLL 2
115#define AT803X_CLK_OUT_50MHZ_DSP 3
116#define AT803X_CLK_OUT_62_5MHZ_PLL 4
117#define AT803X_CLK_OUT_62_5MHZ_DSP 5
118#define AT803X_CLK_OUT_125MHZ_PLL 6
119#define AT803X_CLK_OUT_125MHZ_DSP 7
120
Michael Walle428061f2019-11-06 23:36:15 +0100121/* The AR8035 has another mask which is compatible with the AR8031/AR8033 mask
122 * but doesn't support choosing between XTAL/PLL and DSP.
Michael Walle2f664822019-11-06 23:36:14 +0100123 */
124#define AT8035_CLK_OUT_MASK GENMASK(4, 3)
125
126#define AT803X_CLK_OUT_STRENGTH_MASK GENMASK(8, 7)
127#define AT803X_CLK_OUT_STRENGTH_FULL 0
128#define AT803X_CLK_OUT_STRENGTH_HALF 1
129#define AT803X_CLK_OUT_STRENGTH_QUARTER 2
130
Michael Wallecde0f4f2020-04-28 23:15:02 +0200131#define AT803X_DEFAULT_DOWNSHIFT 5
132#define AT803X_MIN_DOWNSHIFT 2
133#define AT803X_MAX_DOWNSHIFT 9
134
Oleksij Rempel7908d2c2019-10-03 10:21:12 +0200135#define ATH9331_PHY_ID 0x004dd041
Daniel Mackbd8ca172014-06-18 11:01:42 +0200136#define ATH8030_PHY_ID 0x004dd076
137#define ATH8031_PHY_ID 0x004dd074
David Bauer58000912020-04-17 15:41:59 +0200138#define ATH8032_PHY_ID 0x004dd023
Daniel Mackbd8ca172014-06-18 11:01:42 +0200139#define ATH8035_PHY_ID 0x004dd072
Michael Walle0465d8f2020-05-22 11:53:31 +0200140#define AT8030_PHY_ID_MASK 0xffffffef
Daniel Mackbd8ca172014-06-18 11:01:42 +0200141
Michael Walle96c36712019-11-06 23:36:16 +0100142MODULE_DESCRIPTION("Qualcomm Atheros AR803x PHY driver");
Matus Ujhelyi0ca71112012-10-14 19:07:16 +0000143MODULE_AUTHOR("Matus Ujhelyi");
144MODULE_LICENSE("GPL");
145
Michael Walle2f664822019-11-06 23:36:14 +0100146struct at803x_priv {
147 int flags;
148#define AT803X_KEEP_PLL_ENABLED BIT(0) /* don't turn off internal PLL */
149 u16 clk_25m_reg;
150 u16 clk_25m_mask;
151 struct regulator_dev *vddio_rdev;
152 struct regulator_dev *vddh_rdev;
153 struct regulator *vddio;
154};
155
Daniel Mack13a56b42014-06-18 11:01:43 +0200156struct at803x_context {
157 u16 bmcr;
158 u16 advertise;
159 u16 control1000;
160 u16 int_enable;
161 u16 smart_speed;
162 u16 led_control;
163};
164
Martin Blumenstingl2e5f9f22016-01-15 01:55:22 +0100165static int at803x_debug_reg_read(struct phy_device *phydev, u16 reg)
166{
167 int ret;
168
169 ret = phy_write(phydev, AT803X_DEBUG_ADDR, reg);
170 if (ret < 0)
171 return ret;
172
173 return phy_read(phydev, AT803X_DEBUG_DATA);
174}
175
176static int at803x_debug_reg_mask(struct phy_device *phydev, u16 reg,
177 u16 clear, u16 set)
178{
179 u16 val;
180 int ret;
181
182 ret = at803x_debug_reg_read(phydev, reg);
183 if (ret < 0)
184 return ret;
185
186 val = ret & 0xffff;
187 val &= ~clear;
188 val |= set;
189
190 return phy_write(phydev, AT803X_DEBUG_DATA, val);
191}
192
Vinod Koul6d4cd042019-02-21 15:53:15 +0530193static int at803x_enable_rx_delay(struct phy_device *phydev)
194{
195 return at803x_debug_reg_mask(phydev, AT803X_DEBUG_REG_0, 0,
196 AT803X_DEBUG_RX_CLK_DLY_EN);
197}
198
199static int at803x_enable_tx_delay(struct phy_device *phydev)
200{
201 return at803x_debug_reg_mask(phydev, AT803X_DEBUG_REG_5, 0,
202 AT803X_DEBUG_TX_CLK_DLY_EN);
203}
204
Vinod Koul43f2ebd2019-02-21 15:53:14 +0530205static int at803x_disable_rx_delay(struct phy_device *phydev)
Martin Blumenstingl2e5f9f22016-01-15 01:55:22 +0100206{
Vinod Koulcd28d1d2019-01-21 14:43:17 +0530207 return at803x_debug_reg_mask(phydev, AT803X_DEBUG_REG_0,
208 AT803X_DEBUG_RX_CLK_DLY_EN, 0);
Martin Blumenstingl2e5f9f22016-01-15 01:55:22 +0100209}
210
Vinod Koul43f2ebd2019-02-21 15:53:14 +0530211static int at803x_disable_tx_delay(struct phy_device *phydev)
Martin Blumenstingl2e5f9f22016-01-15 01:55:22 +0100212{
Vinod Koulcd28d1d2019-01-21 14:43:17 +0530213 return at803x_debug_reg_mask(phydev, AT803X_DEBUG_REG_5,
214 AT803X_DEBUG_TX_CLK_DLY_EN, 0);
Martin Blumenstingl2e5f9f22016-01-15 01:55:22 +0100215}
216
Daniel Mack13a56b42014-06-18 11:01:43 +0200217/* save relevant PHY registers to private copy */
218static void at803x_context_save(struct phy_device *phydev,
219 struct at803x_context *context)
220{
221 context->bmcr = phy_read(phydev, MII_BMCR);
222 context->advertise = phy_read(phydev, MII_ADVERTISE);
223 context->control1000 = phy_read(phydev, MII_CTRL1000);
224 context->int_enable = phy_read(phydev, AT803X_INTR_ENABLE);
225 context->smart_speed = phy_read(phydev, AT803X_SMART_SPEED);
226 context->led_control = phy_read(phydev, AT803X_LED_CONTROL);
227}
228
229/* restore relevant PHY registers from private copy */
230static void at803x_context_restore(struct phy_device *phydev,
231 const struct at803x_context *context)
232{
233 phy_write(phydev, MII_BMCR, context->bmcr);
234 phy_write(phydev, MII_ADVERTISE, context->advertise);
235 phy_write(phydev, MII_CTRL1000, context->control1000);
236 phy_write(phydev, AT803X_INTR_ENABLE, context->int_enable);
237 phy_write(phydev, AT803X_SMART_SPEED, context->smart_speed);
238 phy_write(phydev, AT803X_LED_CONTROL, context->led_control);
239}
240
Mugunthan V Nea13c9e2013-06-03 20:10:05 +0000241static int at803x_set_wol(struct phy_device *phydev,
242 struct ethtool_wolinfo *wol)
Matus Ujhelyi0ca71112012-10-14 19:07:16 +0000243{
244 struct net_device *ndev = phydev->attached_dev;
245 const u8 *mac;
Mugunthan V Nea13c9e2013-06-03 20:10:05 +0000246 int ret;
247 u32 value;
Matus Ujhelyi0ca71112012-10-14 19:07:16 +0000248 unsigned int i, offsets[] = {
249 AT803X_LOC_MAC_ADDR_32_47_OFFSET,
250 AT803X_LOC_MAC_ADDR_16_31_OFFSET,
251 AT803X_LOC_MAC_ADDR_0_15_OFFSET,
252 };
253
254 if (!ndev)
Mugunthan V Nea13c9e2013-06-03 20:10:05 +0000255 return -ENODEV;
Matus Ujhelyi0ca71112012-10-14 19:07:16 +0000256
Mugunthan V Nea13c9e2013-06-03 20:10:05 +0000257 if (wol->wolopts & WAKE_MAGIC) {
258 mac = (const u8 *) ndev->dev_addr;
Matus Ujhelyi0ca71112012-10-14 19:07:16 +0000259
Mugunthan V Nea13c9e2013-06-03 20:10:05 +0000260 if (!is_valid_ether_addr(mac))
Dan Murphyfc755682017-10-10 12:42:56 -0500261 return -EINVAL;
Matus Ujhelyi0ca71112012-10-14 19:07:16 +0000262
Carlo Caione0e021392019-01-25 12:35:10 +0000263 for (i = 0; i < 3; i++)
264 phy_write_mmd(phydev, AT803X_DEVICE_ADDR, offsets[i],
265 mac[(i * 2) + 1] | (mac[(i * 2)] << 8));
Mugunthan V Nea13c9e2013-06-03 20:10:05 +0000266
267 value = phy_read(phydev, AT803X_INTR_ENABLE);
Martin Blumenstingle6e4a552016-01-15 01:55:24 +0100268 value |= AT803X_INTR_ENABLE_WOL;
Mugunthan V Nea13c9e2013-06-03 20:10:05 +0000269 ret = phy_write(phydev, AT803X_INTR_ENABLE, value);
270 if (ret)
271 return ret;
272 value = phy_read(phydev, AT803X_INTR_STATUS);
273 } else {
274 value = phy_read(phydev, AT803X_INTR_ENABLE);
Martin Blumenstingle6e4a552016-01-15 01:55:24 +0100275 value &= (~AT803X_INTR_ENABLE_WOL);
Mugunthan V Nea13c9e2013-06-03 20:10:05 +0000276 ret = phy_write(phydev, AT803X_INTR_ENABLE, value);
277 if (ret)
278 return ret;
279 value = phy_read(phydev, AT803X_INTR_STATUS);
Matus Ujhelyi0ca71112012-10-14 19:07:16 +0000280 }
Mugunthan V Nea13c9e2013-06-03 20:10:05 +0000281
282 return ret;
283}
284
285static void at803x_get_wol(struct phy_device *phydev,
286 struct ethtool_wolinfo *wol)
287{
288 u32 value;
289
290 wol->supported = WAKE_MAGIC;
291 wol->wolopts = 0;
292
293 value = phy_read(phydev, AT803X_INTR_ENABLE);
Martin Blumenstingle6e4a552016-01-15 01:55:24 +0100294 if (value & AT803X_INTR_ENABLE_WOL)
Mugunthan V Nea13c9e2013-06-03 20:10:05 +0000295 wol->wolopts |= WAKE_MAGIC;
Matus Ujhelyi0ca71112012-10-14 19:07:16 +0000296}
297
Daniel Mack6229ed12013-09-21 16:53:02 +0200298static int at803x_suspend(struct phy_device *phydev)
299{
300 int value;
301 int wol_enabled;
302
Daniel Mack6229ed12013-09-21 16:53:02 +0200303 value = phy_read(phydev, AT803X_INTR_ENABLE);
Martin Blumenstingle6e4a552016-01-15 01:55:24 +0100304 wol_enabled = value & AT803X_INTR_ENABLE_WOL;
Daniel Mack6229ed12013-09-21 16:53:02 +0200305
Daniel Mack6229ed12013-09-21 16:53:02 +0200306 if (wol_enabled)
Russell Kingfea23fb2018-01-02 10:58:58 +0000307 value = BMCR_ISOLATE;
Daniel Mack6229ed12013-09-21 16:53:02 +0200308 else
Russell Kingfea23fb2018-01-02 10:58:58 +0000309 value = BMCR_PDOWN;
Daniel Mack6229ed12013-09-21 16:53:02 +0200310
Russell Kingfea23fb2018-01-02 10:58:58 +0000311 phy_modify(phydev, MII_BMCR, 0, value);
Daniel Mack6229ed12013-09-21 16:53:02 +0200312
313 return 0;
314}
315
316static int at803x_resume(struct phy_device *phydev)
317{
Russell Kingf1028522018-01-05 16:07:10 +0000318 return phy_modify(phydev, MII_BMCR, BMCR_PDOWN | BMCR_ISOLATE, 0);
Daniel Mack6229ed12013-09-21 16:53:02 +0200319}
320
Michael Walle2f664822019-11-06 23:36:14 +0100321static int at803x_rgmii_reg_set_voltage_sel(struct regulator_dev *rdev,
322 unsigned int selector)
323{
324 struct phy_device *phydev = rdev_get_drvdata(rdev);
325
326 if (selector)
327 return at803x_debug_reg_mask(phydev, AT803X_DEBUG_REG_1F,
328 0, AT803X_DEBUG_RGMII_1V8);
329 else
330 return at803x_debug_reg_mask(phydev, AT803X_DEBUG_REG_1F,
331 AT803X_DEBUG_RGMII_1V8, 0);
332}
333
334static int at803x_rgmii_reg_get_voltage_sel(struct regulator_dev *rdev)
335{
336 struct phy_device *phydev = rdev_get_drvdata(rdev);
337 int val;
338
339 val = at803x_debug_reg_read(phydev, AT803X_DEBUG_REG_1F);
340 if (val < 0)
341 return val;
342
343 return (val & AT803X_DEBUG_RGMII_1V8) ? 1 : 0;
344}
345
Rikard Falkeborn3faaf532020-08-27 00:56:06 +0200346static const struct regulator_ops vddio_regulator_ops = {
Michael Walle2f664822019-11-06 23:36:14 +0100347 .list_voltage = regulator_list_voltage_table,
348 .set_voltage_sel = at803x_rgmii_reg_set_voltage_sel,
349 .get_voltage_sel = at803x_rgmii_reg_get_voltage_sel,
350};
351
352static const unsigned int vddio_voltage_table[] = {
353 1500000,
354 1800000,
355};
356
357static const struct regulator_desc vddio_desc = {
358 .name = "vddio",
359 .of_match = of_match_ptr("vddio-regulator"),
360 .n_voltages = ARRAY_SIZE(vddio_voltage_table),
361 .volt_table = vddio_voltage_table,
362 .ops = &vddio_regulator_ops,
363 .type = REGULATOR_VOLTAGE,
364 .owner = THIS_MODULE,
365};
366
Rikard Falkeborn3faaf532020-08-27 00:56:06 +0200367static const struct regulator_ops vddh_regulator_ops = {
Michael Walle2f664822019-11-06 23:36:14 +0100368};
369
370static const struct regulator_desc vddh_desc = {
371 .name = "vddh",
372 .of_match = of_match_ptr("vddh-regulator"),
373 .n_voltages = 1,
374 .fixed_uV = 2500000,
375 .ops = &vddh_regulator_ops,
376 .type = REGULATOR_VOLTAGE,
377 .owner = THIS_MODULE,
378};
379
380static int at8031_register_regulators(struct phy_device *phydev)
381{
382 struct at803x_priv *priv = phydev->priv;
383 struct device *dev = &phydev->mdio.dev;
384 struct regulator_config config = { };
385
386 config.dev = dev;
387 config.driver_data = phydev;
388
389 priv->vddio_rdev = devm_regulator_register(dev, &vddio_desc, &config);
390 if (IS_ERR(priv->vddio_rdev)) {
391 phydev_err(phydev, "failed to register VDDIO regulator\n");
392 return PTR_ERR(priv->vddio_rdev);
393 }
394
395 priv->vddh_rdev = devm_regulator_register(dev, &vddh_desc, &config);
396 if (IS_ERR(priv->vddh_rdev)) {
397 phydev_err(phydev, "failed to register VDDH regulator\n");
398 return PTR_ERR(priv->vddh_rdev);
399 }
400
401 return 0;
402}
403
404static bool at803x_match_phy_id(struct phy_device *phydev, u32 phy_id)
405{
406 return (phydev->phy_id & phydev->drv->phy_id_mask)
407 == (phy_id & phydev->drv->phy_id_mask);
408}
409
410static int at803x_parse_dt(struct phy_device *phydev)
411{
412 struct device_node *node = phydev->mdio.dev.of_node;
413 struct at803x_priv *priv = phydev->priv;
Michael Walle2f664822019-11-06 23:36:14 +0100414 u32 freq, strength;
Andrew Lunn3f2edd32020-07-07 03:49:33 +0200415 unsigned int sel;
Michael Walle2f664822019-11-06 23:36:14 +0100416 int ret;
417
418 if (!IS_ENABLED(CONFIG_OF_MDIO))
419 return 0;
420
421 ret = of_property_read_u32(node, "qca,clk-out-frequency", &freq);
422 if (!ret) {
Michael Walle2f664822019-11-06 23:36:14 +0100423 switch (freq) {
424 case 25000000:
425 sel = AT803X_CLK_OUT_25MHZ_XTAL;
426 break;
427 case 50000000:
428 sel = AT803X_CLK_OUT_50MHZ_PLL;
429 break;
430 case 62500000:
431 sel = AT803X_CLK_OUT_62_5MHZ_PLL;
432 break;
433 case 125000000:
434 sel = AT803X_CLK_OUT_125MHZ_PLL;
435 break;
436 default:
437 phydev_err(phydev, "invalid qca,clk-out-frequency\n");
438 return -EINVAL;
439 }
440
Andrew Lunn3f2edd32020-07-07 03:49:33 +0200441 priv->clk_25m_reg |= FIELD_PREP(AT803X_CLK_OUT_MASK, sel);
442 priv->clk_25m_mask |= AT803X_CLK_OUT_MASK;
Michael Walle2f664822019-11-06 23:36:14 +0100443
444 /* Fixup for the AR8030/AR8035. This chip has another mask and
445 * doesn't support the DSP reference. Eg. the lowest bit of the
446 * mask. The upper two bits select the same frequencies. Mask
447 * the lowest bit here.
448 *
449 * Warning:
450 * There was no datasheet for the AR8030 available so this is
451 * just a guess. But the AR8035 is listed as pin compatible
452 * to the AR8030 so there might be a good chance it works on
453 * the AR8030 too.
454 */
455 if (at803x_match_phy_id(phydev, ATH8030_PHY_ID) ||
456 at803x_match_phy_id(phydev, ATH8035_PHY_ID)) {
Oleksij Rempelb1f4c202020-04-01 11:57:32 +0200457 priv->clk_25m_reg &= AT8035_CLK_OUT_MASK;
458 priv->clk_25m_mask &= AT8035_CLK_OUT_MASK;
Michael Walle2f664822019-11-06 23:36:14 +0100459 }
460 }
461
462 ret = of_property_read_u32(node, "qca,clk-out-strength", &strength);
463 if (!ret) {
464 priv->clk_25m_mask |= AT803X_CLK_OUT_STRENGTH_MASK;
465 switch (strength) {
466 case AR803X_STRENGTH_FULL:
467 priv->clk_25m_reg |= AT803X_CLK_OUT_STRENGTH_FULL;
468 break;
469 case AR803X_STRENGTH_HALF:
470 priv->clk_25m_reg |= AT803X_CLK_OUT_STRENGTH_HALF;
471 break;
472 case AR803X_STRENGTH_QUARTER:
473 priv->clk_25m_reg |= AT803X_CLK_OUT_STRENGTH_QUARTER;
474 break;
475 default:
476 phydev_err(phydev, "invalid qca,clk-out-strength\n");
477 return -EINVAL;
478 }
479 }
480
Michael Walle428061f2019-11-06 23:36:15 +0100481 /* Only supported on AR8031/AR8033, the AR8030/AR8035 use strapping
482 * options.
483 */
Michael Walle2f664822019-11-06 23:36:14 +0100484 if (at803x_match_phy_id(phydev, ATH8031_PHY_ID)) {
485 if (of_property_read_bool(node, "qca,keep-pll-enabled"))
486 priv->flags |= AT803X_KEEP_PLL_ENABLED;
487
488 ret = at8031_register_regulators(phydev);
489 if (ret < 0)
490 return ret;
491
492 priv->vddio = devm_regulator_get_optional(&phydev->mdio.dev,
493 "vddio");
494 if (IS_ERR(priv->vddio)) {
495 phydev_err(phydev, "failed to get VDDIO regulator\n");
496 return PTR_ERR(priv->vddio);
497 }
498
499 ret = regulator_enable(priv->vddio);
500 if (ret < 0)
501 return ret;
502 }
503
504 return 0;
505}
506
507static int at803x_probe(struct phy_device *phydev)
508{
509 struct device *dev = &phydev->mdio.dev;
510 struct at803x_priv *priv;
511
512 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
513 if (!priv)
514 return -ENOMEM;
515
516 phydev->priv = priv;
517
518 return at803x_parse_dt(phydev);
519}
520
Michael Walle2318ca82020-01-30 18:54:02 +0100521static void at803x_remove(struct phy_device *phydev)
522{
523 struct at803x_priv *priv = phydev->priv;
524
525 if (priv->vddio)
526 regulator_disable(priv->vddio);
527}
528
Michael Walle2f664822019-11-06 23:36:14 +0100529static int at803x_clk_out_config(struct phy_device *phydev)
530{
531 struct at803x_priv *priv = phydev->priv;
532 int val;
533
534 if (!priv->clk_25m_mask)
535 return 0;
536
537 val = phy_read_mmd(phydev, MDIO_MMD_AN, AT803X_MMD7_CLK25M);
538 if (val < 0)
539 return val;
540
541 val &= ~priv->clk_25m_mask;
542 val |= priv->clk_25m_reg;
543
544 return phy_write_mmd(phydev, MDIO_MMD_AN, AT803X_MMD7_CLK25M, val);
545}
546
547static int at8031_pll_config(struct phy_device *phydev)
548{
549 struct at803x_priv *priv = phydev->priv;
550
551 /* The default after hardware reset is PLL OFF. After a soft reset, the
552 * values are retained.
553 */
554 if (priv->flags & AT803X_KEEP_PLL_ENABLED)
555 return at803x_debug_reg_mask(phydev, AT803X_DEBUG_REG_1F,
556 0, AT803X_DEBUG_PLL_ON);
557 else
558 return at803x_debug_reg_mask(phydev, AT803X_DEBUG_REG_1F,
559 AT803X_DEBUG_PLL_ON, 0);
560}
561
Matus Ujhelyi0ca71112012-10-14 19:07:16 +0000562static int at803x_config_init(struct phy_device *phydev)
563{
Mugunthan V N1ca6d1b2013-06-03 20:10:06 +0000564 int ret;
Matus Ujhelyi0ca71112012-10-14 19:07:16 +0000565
Vinod Koul6d4cd042019-02-21 15:53:15 +0530566 /* The RX and TX delay default is:
567 * after HW reset: RX delay enabled and TX delay disabled
568 * after SW reset: RX delay enabled, while TX delay retains the
569 * value before reset.
Vinod Koul6d4cd042019-02-21 15:53:15 +0530570 */
Vinod Koul6d4cd042019-02-21 15:53:15 +0530571 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID ||
André Draszikbb0ce4c2019-08-09 12:20:25 +0100572 phydev->interface == PHY_INTERFACE_MODE_RGMII_RXID)
Vinod Koul6d4cd042019-02-21 15:53:15 +0530573 ret = at803x_enable_rx_delay(phydev);
André Draszikbb0ce4c2019-08-09 12:20:25 +0100574 else
575 ret = at803x_disable_rx_delay(phydev);
576 if (ret < 0)
577 return ret;
Martin Blumenstingl2e5f9f22016-01-15 01:55:22 +0100578
Vinod Koul6d4cd042019-02-21 15:53:15 +0530579 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID ||
André Draszikbb0ce4c2019-08-09 12:20:25 +0100580 phydev->interface == PHY_INTERFACE_MODE_RGMII_TXID)
Vinod Koul6d4cd042019-02-21 15:53:15 +0530581 ret = at803x_enable_tx_delay(phydev);
André Draszikbb0ce4c2019-08-09 12:20:25 +0100582 else
583 ret = at803x_disable_tx_delay(phydev);
Michael Walle2f664822019-11-06 23:36:14 +0100584 if (ret < 0)
585 return ret;
Mugunthan V N1ca6d1b2013-06-03 20:10:06 +0000586
Michael Walle2f664822019-11-06 23:36:14 +0100587 ret = at803x_clk_out_config(phydev);
588 if (ret < 0)
589 return ret;
590
591 if (at803x_match_phy_id(phydev, ATH8031_PHY_ID)) {
592 ret = at8031_pll_config(phydev);
593 if (ret < 0)
594 return ret;
595 }
596
597 return 0;
Matus Ujhelyi0ca71112012-10-14 19:07:16 +0000598}
599
Zhao Qiang77a99392014-03-28 15:39:41 +0800600static int at803x_ack_interrupt(struct phy_device *phydev)
601{
602 int err;
603
Martin Blumenstingla46bd632016-01-15 01:55:23 +0100604 err = phy_read(phydev, AT803X_INTR_STATUS);
Zhao Qiang77a99392014-03-28 15:39:41 +0800605
606 return (err < 0) ? err : 0;
607}
608
609static int at803x_config_intr(struct phy_device *phydev)
610{
611 int err;
612 int value;
613
Martin Blumenstingla46bd632016-01-15 01:55:23 +0100614 value = phy_read(phydev, AT803X_INTR_ENABLE);
Zhao Qiang77a99392014-03-28 15:39:41 +0800615
Martin Blumenstingle6e4a552016-01-15 01:55:24 +0100616 if (phydev->interrupts == PHY_INTERRUPT_ENABLED) {
617 value |= AT803X_INTR_ENABLE_AUTONEG_ERR;
618 value |= AT803X_INTR_ENABLE_SPEED_CHANGED;
619 value |= AT803X_INTR_ENABLE_DUPLEX_CHANGED;
620 value |= AT803X_INTR_ENABLE_LINK_FAIL;
621 value |= AT803X_INTR_ENABLE_LINK_SUCCESS;
622
623 err = phy_write(phydev, AT803X_INTR_ENABLE, value);
624 }
Zhao Qiang77a99392014-03-28 15:39:41 +0800625 else
Martin Blumenstingla46bd632016-01-15 01:55:23 +0100626 err = phy_write(phydev, AT803X_INTR_ENABLE, 0);
Zhao Qiang77a99392014-03-28 15:39:41 +0800627
628 return err;
629}
630
Ioana Ciornei29773092020-11-01 14:50:59 +0200631static irqreturn_t at803x_handle_interrupt(struct phy_device *phydev)
632{
633 int irq_status, int_enabled;
634
635 irq_status = phy_read(phydev, AT803X_INTR_STATUS);
636 if (irq_status < 0) {
637 phy_error(phydev);
638 return IRQ_NONE;
639 }
640
641 /* Read the current enabled interrupts */
642 int_enabled = phy_read(phydev, AT803X_INTR_ENABLE);
643 if (int_enabled < 0) {
644 phy_error(phydev);
645 return IRQ_NONE;
646 }
647
648 /* See if this was one of our enabled interrupts */
649 if (!(irq_status & int_enabled))
650 return IRQ_NONE;
651
652 phy_trigger_machine(phydev);
653
654 return IRQ_HANDLED;
655}
656
Daniel Mack13a56b42014-06-18 11:01:43 +0200657static void at803x_link_change_notify(struct phy_device *phydev)
658{
Daniel Mack13a56b42014-06-18 11:01:43 +0200659 /*
660 * Conduct a hardware reset for AT8030 every time a link loss is
661 * signalled. This is necessary to circumvent a hardware bug that
662 * occurs when the cable is unplugged while TX packets are pending
663 * in the FIFO. In such cases, the FIFO enters an error mode it
664 * cannot recover from by software.
665 */
David Bauer6110ed22019-04-17 23:59:22 +0200666 if (phydev->state == PHY_NOLINK && phydev->mdio.reset_gpio) {
Heiner Kallweit5c5f6262019-03-19 19:56:51 +0100667 struct at803x_context context;
Daniel Mack13a56b42014-06-18 11:01:43 +0200668
Heiner Kallweit5c5f6262019-03-19 19:56:51 +0100669 at803x_context_save(phydev, &context);
Daniel Mack13a56b42014-06-18 11:01:43 +0200670
Heiner Kallweit5c5f6262019-03-19 19:56:51 +0100671 phy_device_reset(phydev, 1);
672 msleep(1);
673 phy_device_reset(phydev, 0);
674 msleep(1);
Daniel Mack13a56b42014-06-18 11:01:43 +0200675
Heiner Kallweit5c5f6262019-03-19 19:56:51 +0100676 at803x_context_restore(phydev, &context);
Daniel Mack13a56b42014-06-18 11:01:43 +0200677
Heiner Kallweit5c5f6262019-03-19 19:56:51 +0100678 phydev_dbg(phydev, "%s(): phy was reset\n", __func__);
Daniel Mack13a56b42014-06-18 11:01:43 +0200679 }
680}
681
Zefir Kurtisif62265b2016-10-24 12:40:54 +0200682static int at803x_aneg_done(struct phy_device *phydev)
683{
684 int ccr;
685
686 int aneg_done = genphy_aneg_done(phydev);
687 if (aneg_done != BMSR_ANEGCOMPLETE)
688 return aneg_done;
689
690 /*
691 * in SGMII mode, if copper side autoneg is successful,
692 * also check SGMII side autoneg result
693 */
694 ccr = phy_read(phydev, AT803X_REG_CHIP_CONFIG);
695 if ((ccr & AT803X_MODE_CFG_MASK) != AT803X_MODE_CFG_SGMII)
696 return aneg_done;
697
698 /* switch to SGMII/fiber page */
699 phy_write(phydev, AT803X_REG_CHIP_CONFIG, ccr & ~AT803X_BT_BX_REG_SEL);
700
701 /* check if the SGMII link is OK. */
702 if (!(phy_read(phydev, AT803X_PSSR) & AT803X_PSSR_MR_AN_COMPLETE)) {
Andrew Lunnab2a6052018-09-29 23:04:10 +0200703 phydev_warn(phydev, "803x_aneg_done: SGMII link is not ok\n");
Zefir Kurtisif62265b2016-10-24 12:40:54 +0200704 aneg_done = 0;
705 }
706 /* switch back to copper page */
707 phy_write(phydev, AT803X_REG_CHIP_CONFIG, ccr | AT803X_BT_BX_REG_SEL);
708
709 return aneg_done;
710}
711
Russell King06d5f342019-10-04 17:06:14 +0100712static int at803x_read_status(struct phy_device *phydev)
713{
714 int ss, err, old_link = phydev->link;
715
716 /* Update the link, but return if there was an error */
717 err = genphy_update_link(phydev);
718 if (err)
719 return err;
720
721 /* why bother the PHY if nothing can have changed */
722 if (phydev->autoneg == AUTONEG_ENABLE && old_link && phydev->link)
723 return 0;
724
725 phydev->speed = SPEED_UNKNOWN;
726 phydev->duplex = DUPLEX_UNKNOWN;
727 phydev->pause = 0;
728 phydev->asym_pause = 0;
729
730 err = genphy_read_lpa(phydev);
731 if (err < 0)
732 return err;
733
734 /* Read the AT8035 PHY-Specific Status register, which indicates the
735 * speed and duplex that the PHY is actually using, irrespective of
736 * whether we are in autoneg mode or not.
737 */
738 ss = phy_read(phydev, AT803X_SPECIFIC_STATUS);
739 if (ss < 0)
740 return ss;
741
742 if (ss & AT803X_SS_SPEED_DUPLEX_RESOLVED) {
Oleksij Rempel7dce80c2020-07-19 10:05:30 +0200743 int sfc;
744
745 sfc = phy_read(phydev, AT803X_SPECIFIC_FUNCTION_CONTROL);
746 if (sfc < 0)
747 return sfc;
748
Russell King06d5f342019-10-04 17:06:14 +0100749 switch (ss & AT803X_SS_SPEED_MASK) {
750 case AT803X_SS_SPEED_10:
751 phydev->speed = SPEED_10;
752 break;
753 case AT803X_SS_SPEED_100:
754 phydev->speed = SPEED_100;
755 break;
756 case AT803X_SS_SPEED_1000:
757 phydev->speed = SPEED_1000;
758 break;
759 }
760 if (ss & AT803X_SS_DUPLEX)
761 phydev->duplex = DUPLEX_FULL;
762 else
763 phydev->duplex = DUPLEX_HALF;
Oleksij Rempel7dce80c2020-07-19 10:05:30 +0200764
Russell King06d5f342019-10-04 17:06:14 +0100765 if (ss & AT803X_SS_MDIX)
766 phydev->mdix = ETH_TP_MDI_X;
767 else
768 phydev->mdix = ETH_TP_MDI;
Oleksij Rempel7dce80c2020-07-19 10:05:30 +0200769
770 switch (FIELD_GET(AT803X_SFC_MDI_CROSSOVER_MODE_M, sfc)) {
771 case AT803X_SFC_MANUAL_MDI:
772 phydev->mdix_ctrl = ETH_TP_MDI;
773 break;
774 case AT803X_SFC_MANUAL_MDIX:
775 phydev->mdix_ctrl = ETH_TP_MDI_X;
776 break;
777 case AT803X_SFC_AUTOMATIC_CROSSOVER:
778 phydev->mdix_ctrl = ETH_TP_MDI_AUTO;
779 break;
780 }
Russell King06d5f342019-10-04 17:06:14 +0100781 }
782
783 if (phydev->autoneg == AUTONEG_ENABLE && phydev->autoneg_complete)
784 phy_resolve_aneg_pause(phydev);
785
786 return 0;
787}
788
Oleksij Rempel7dce80c2020-07-19 10:05:30 +0200789static int at803x_config_mdix(struct phy_device *phydev, u8 ctrl)
790{
791 u16 val;
792
793 switch (ctrl) {
794 case ETH_TP_MDI:
795 val = AT803X_SFC_MANUAL_MDI;
796 break;
797 case ETH_TP_MDI_X:
798 val = AT803X_SFC_MANUAL_MDIX;
799 break;
800 case ETH_TP_MDI_AUTO:
801 val = AT803X_SFC_AUTOMATIC_CROSSOVER;
802 break;
803 default:
804 return 0;
805 }
806
807 return phy_modify_changed(phydev, AT803X_SPECIFIC_FUNCTION_CONTROL,
808 AT803X_SFC_MDI_CROSSOVER_MODE_M,
809 FIELD_PREP(AT803X_SFC_MDI_CROSSOVER_MODE_M, val));
810}
811
812static int at803x_config_aneg(struct phy_device *phydev)
813{
814 int ret;
815
816 ret = at803x_config_mdix(phydev, phydev->mdix_ctrl);
817 if (ret < 0)
818 return ret;
819
820 /* Changes of the midx bits are disruptive to the normal operation;
821 * therefore any changes to these registers must be followed by a
822 * software reset to take effect.
823 */
824 if (ret == 1) {
825 ret = genphy_soft_reset(phydev);
826 if (ret < 0)
827 return ret;
828 }
829
830 return genphy_config_aneg(phydev);
831}
832
Michael Wallecde0f4f2020-04-28 23:15:02 +0200833static int at803x_get_downshift(struct phy_device *phydev, u8 *d)
834{
835 int val;
836
837 val = phy_read(phydev, AT803X_SMART_SPEED);
838 if (val < 0)
839 return val;
840
841 if (val & AT803X_SMART_SPEED_ENABLE)
842 *d = FIELD_GET(AT803X_SMART_SPEED_RETRY_LIMIT_MASK, val) + 2;
843 else
844 *d = DOWNSHIFT_DEV_DISABLE;
845
846 return 0;
847}
848
849static int at803x_set_downshift(struct phy_device *phydev, u8 cnt)
850{
851 u16 mask, set;
852 int ret;
853
854 switch (cnt) {
855 case DOWNSHIFT_DEV_DEFAULT_COUNT:
856 cnt = AT803X_DEFAULT_DOWNSHIFT;
857 fallthrough;
858 case AT803X_MIN_DOWNSHIFT ... AT803X_MAX_DOWNSHIFT:
859 set = AT803X_SMART_SPEED_ENABLE |
860 AT803X_SMART_SPEED_BYPASS_TIMER |
861 FIELD_PREP(AT803X_SMART_SPEED_RETRY_LIMIT_MASK, cnt - 2);
862 mask = AT803X_SMART_SPEED_RETRY_LIMIT_MASK;
863 break;
864 case DOWNSHIFT_DEV_DISABLE:
865 set = 0;
866 mask = AT803X_SMART_SPEED_ENABLE |
867 AT803X_SMART_SPEED_BYPASS_TIMER;
868 break;
869 default:
870 return -EINVAL;
871 }
872
873 ret = phy_modify_changed(phydev, AT803X_SMART_SPEED, mask, set);
874
875 /* After changing the smart speed settings, we need to perform a
876 * software reset, use phy_init_hw() to make sure we set the
877 * reapply any values which might got lost during software reset.
878 */
879 if (ret == 1)
880 ret = phy_init_hw(phydev);
881
882 return ret;
883}
884
885static int at803x_get_tunable(struct phy_device *phydev,
886 struct ethtool_tunable *tuna, void *data)
887{
888 switch (tuna->id) {
889 case ETHTOOL_PHY_DOWNSHIFT:
890 return at803x_get_downshift(phydev, data);
891 default:
892 return -EOPNOTSUPP;
893 }
894}
895
896static int at803x_set_tunable(struct phy_device *phydev,
897 struct ethtool_tunable *tuna, const void *data)
898{
899 switch (tuna->id) {
900 case ETHTOOL_PHY_DOWNSHIFT:
901 return at803x_set_downshift(phydev, *(const u8 *)data);
902 default:
903 return -EOPNOTSUPP;
904 }
905}
906
Michael Walle6cb75762020-05-13 22:38:07 +0200907static int at803x_cable_test_result_trans(u16 status)
908{
909 switch (FIELD_GET(AT803X_CDT_STATUS_STAT_MASK, status)) {
910 case AT803X_CDT_STATUS_STAT_NORMAL:
911 return ETHTOOL_A_CABLE_RESULT_CODE_OK;
912 case AT803X_CDT_STATUS_STAT_SHORT:
913 return ETHTOOL_A_CABLE_RESULT_CODE_SAME_SHORT;
914 case AT803X_CDT_STATUS_STAT_OPEN:
915 return ETHTOOL_A_CABLE_RESULT_CODE_OPEN;
916 case AT803X_CDT_STATUS_STAT_FAIL:
917 default:
918 return ETHTOOL_A_CABLE_RESULT_CODE_UNSPEC;
919 }
920}
921
922static bool at803x_cdt_test_failed(u16 status)
923{
924 return FIELD_GET(AT803X_CDT_STATUS_STAT_MASK, status) ==
925 AT803X_CDT_STATUS_STAT_FAIL;
926}
927
928static bool at803x_cdt_fault_length_valid(u16 status)
929{
930 switch (FIELD_GET(AT803X_CDT_STATUS_STAT_MASK, status)) {
931 case AT803X_CDT_STATUS_STAT_OPEN:
932 case AT803X_CDT_STATUS_STAT_SHORT:
933 return true;
934 }
935 return false;
936}
937
938static int at803x_cdt_fault_length(u16 status)
939{
940 int dt;
941
942 /* According to the datasheet the distance to the fault is
943 * DELTA_TIME * 0.824 meters.
944 *
945 * The author suspect the correct formula is:
946 *
947 * fault_distance = DELTA_TIME * (c * VF) / 125MHz / 2
948 *
949 * where c is the speed of light, VF is the velocity factor of
950 * the twisted pair cable, 125MHz the counter frequency and
951 * we need to divide by 2 because the hardware will measure the
952 * round trip time to the fault and back to the PHY.
953 *
954 * With a VF of 0.69 we get the factor 0.824 mentioned in the
955 * datasheet.
956 */
957 dt = FIELD_GET(AT803X_CDT_STATUS_DELTA_TIME_MASK, status);
958
959 return (dt * 824) / 10;
960}
961
962static int at803x_cdt_start(struct phy_device *phydev, int pair)
963{
964 u16 cdt;
965
966 cdt = FIELD_PREP(AT803X_CDT_MDI_PAIR_MASK, pair) |
967 AT803X_CDT_ENABLE_TEST;
968
969 return phy_write(phydev, AT803X_CDT, cdt);
970}
971
972static int at803x_cdt_wait_for_completion(struct phy_device *phydev)
973{
974 int val, ret;
975
976 /* One test run takes about 25ms */
977 ret = phy_read_poll_timeout(phydev, AT803X_CDT, val,
978 !(val & AT803X_CDT_ENABLE_TEST),
979 30000, 100000, true);
980
981 return ret < 0 ? ret : 0;
982}
983
984static int at803x_cable_test_one_pair(struct phy_device *phydev, int pair)
985{
986 static const int ethtool_pair[] = {
987 ETHTOOL_A_CABLE_PAIR_A,
988 ETHTOOL_A_CABLE_PAIR_B,
989 ETHTOOL_A_CABLE_PAIR_C,
990 ETHTOOL_A_CABLE_PAIR_D,
991 };
992 int ret, val;
993
994 ret = at803x_cdt_start(phydev, pair);
995 if (ret)
996 return ret;
997
998 ret = at803x_cdt_wait_for_completion(phydev);
999 if (ret)
1000 return ret;
1001
1002 val = phy_read(phydev, AT803X_CDT_STATUS);
1003 if (val < 0)
1004 return val;
1005
1006 if (at803x_cdt_test_failed(val))
1007 return 0;
1008
1009 ethnl_cable_test_result(phydev, ethtool_pair[pair],
1010 at803x_cable_test_result_trans(val));
1011
1012 if (at803x_cdt_fault_length_valid(val))
1013 ethnl_cable_test_fault_length(phydev, ethtool_pair[pair],
1014 at803x_cdt_fault_length(val));
1015
1016 return 1;
1017}
1018
1019static int at803x_cable_test_get_status(struct phy_device *phydev,
1020 bool *finished)
1021{
Oleksij Rempeldc0f3ed2020-05-27 07:08:43 +02001022 unsigned long pair_mask;
Michael Walle6cb75762020-05-13 22:38:07 +02001023 int retries = 20;
1024 int pair, ret;
1025
Oleksij Rempeldc0f3ed2020-05-27 07:08:43 +02001026 if (phydev->phy_id == ATH9331_PHY_ID ||
1027 phydev->phy_id == ATH8032_PHY_ID)
1028 pair_mask = 0x3;
1029 else
1030 pair_mask = 0xf;
1031
Michael Walle6cb75762020-05-13 22:38:07 +02001032 *finished = false;
1033
1034 /* According to the datasheet the CDT can be performed when
1035 * there is no link partner or when the link partner is
1036 * auto-negotiating. Starting the test will restart the AN
1037 * automatically. It seems that doing this repeatedly we will
1038 * get a slot where our link partner won't disturb our
1039 * measurement.
1040 */
1041 while (pair_mask && retries--) {
1042 for_each_set_bit(pair, &pair_mask, 4) {
1043 ret = at803x_cable_test_one_pair(phydev, pair);
1044 if (ret < 0)
1045 return ret;
1046 if (ret)
1047 clear_bit(pair, &pair_mask);
1048 }
1049 if (pair_mask)
1050 msleep(250);
1051 }
1052
1053 *finished = true;
1054
1055 return 0;
1056}
1057
1058static int at803x_cable_test_start(struct phy_device *phydev)
1059{
1060 /* Enable auto-negotiation, but advertise no capabilities, no link
1061 * will be established. A restart of the auto-negotiation is not
1062 * required, because the cable test will automatically break the link.
1063 */
1064 phy_write(phydev, MII_BMCR, BMCR_ANENABLE);
1065 phy_write(phydev, MII_ADVERTISE, ADVERTISE_CSMA);
Oleksij Rempeldc0f3ed2020-05-27 07:08:43 +02001066 if (phydev->phy_id != ATH9331_PHY_ID &&
1067 phydev->phy_id != ATH8032_PHY_ID)
1068 phy_write(phydev, MII_CTRL1000, 0);
Michael Walle6cb75762020-05-13 22:38:07 +02001069
1070 /* we do all the (time consuming) work later */
1071 return 0;
1072}
1073
Mugunthan V N317420a2013-06-03 20:10:04 +00001074static struct phy_driver at803x_driver[] = {
1075{
Michael Walle96c36712019-11-06 23:36:16 +01001076 /* Qualcomm Atheros AR8035 */
Michael Walle0465d8f2020-05-22 11:53:31 +02001077 PHY_ID_MATCH_EXACT(ATH8035_PHY_ID),
Michael Walle96c36712019-11-06 23:36:16 +01001078 .name = "Qualcomm Atheros AR8035",
Michael Walle6cb75762020-05-13 22:38:07 +02001079 .flags = PHY_POLL_CABLE_TEST,
Michael Walle2f664822019-11-06 23:36:14 +01001080 .probe = at803x_probe,
Michael Walle2318ca82020-01-30 18:54:02 +01001081 .remove = at803x_remove,
Oleksij Rempel7dce80c2020-07-19 10:05:30 +02001082 .config_aneg = at803x_config_aneg,
Daniel Mack13a56b42014-06-18 11:01:43 +02001083 .config_init = at803x_config_init,
Michael Wallecde0f4f2020-04-28 23:15:02 +02001084 .soft_reset = genphy_soft_reset,
Daniel Mack13a56b42014-06-18 11:01:43 +02001085 .set_wol = at803x_set_wol,
1086 .get_wol = at803x_get_wol,
1087 .suspend = at803x_suspend,
1088 .resume = at803x_resume,
Heiner Kallweitdcdecdc2019-04-12 20:47:03 +02001089 /* PHY_GBIT_FEATURES */
Russell King06d5f342019-10-04 17:06:14 +01001090 .read_status = at803x_read_status,
Måns Rullgård0eae5982015-11-12 17:40:20 +00001091 .ack_interrupt = at803x_ack_interrupt,
1092 .config_intr = at803x_config_intr,
Ioana Ciornei29773092020-11-01 14:50:59 +02001093 .handle_interrupt = at803x_handle_interrupt,
Michael Wallecde0f4f2020-04-28 23:15:02 +02001094 .get_tunable = at803x_get_tunable,
1095 .set_tunable = at803x_set_tunable,
Michael Walle6cb75762020-05-13 22:38:07 +02001096 .cable_test_start = at803x_cable_test_start,
1097 .cable_test_get_status = at803x_cable_test_get_status,
Mugunthan V N317420a2013-06-03 20:10:04 +00001098}, {
Michael Walle96c36712019-11-06 23:36:16 +01001099 /* Qualcomm Atheros AR8030 */
Daniel Mack13a56b42014-06-18 11:01:43 +02001100 .phy_id = ATH8030_PHY_ID,
Michael Walle96c36712019-11-06 23:36:16 +01001101 .name = "Qualcomm Atheros AR8030",
Michael Walle0465d8f2020-05-22 11:53:31 +02001102 .phy_id_mask = AT8030_PHY_ID_MASK,
Michael Walle2f664822019-11-06 23:36:14 +01001103 .probe = at803x_probe,
Michael Walle2318ca82020-01-30 18:54:02 +01001104 .remove = at803x_remove,
Daniel Mack13a56b42014-06-18 11:01:43 +02001105 .config_init = at803x_config_init,
1106 .link_change_notify = at803x_link_change_notify,
1107 .set_wol = at803x_set_wol,
1108 .get_wol = at803x_get_wol,
1109 .suspend = at803x_suspend,
1110 .resume = at803x_resume,
Heiner Kallweitdcdecdc2019-04-12 20:47:03 +02001111 /* PHY_BASIC_FEATURES */
Måns Rullgård0eae5982015-11-12 17:40:20 +00001112 .ack_interrupt = at803x_ack_interrupt,
1113 .config_intr = at803x_config_intr,
Ioana Ciornei29773092020-11-01 14:50:59 +02001114 .handle_interrupt = at803x_handle_interrupt,
Mugunthan V N05d7cce2013-06-03 20:10:07 +00001115}, {
Michael Walle96c36712019-11-06 23:36:16 +01001116 /* Qualcomm Atheros AR8031/AR8033 */
Michael Walle0465d8f2020-05-22 11:53:31 +02001117 PHY_ID_MATCH_EXACT(ATH8031_PHY_ID),
Michael Walle96c36712019-11-06 23:36:16 +01001118 .name = "Qualcomm Atheros AR8031/AR8033",
Michael Walle6cb75762020-05-13 22:38:07 +02001119 .flags = PHY_POLL_CABLE_TEST,
Michael Walle2f664822019-11-06 23:36:14 +01001120 .probe = at803x_probe,
Michael Walle2318ca82020-01-30 18:54:02 +01001121 .remove = at803x_remove,
Daniel Mack13a56b42014-06-18 11:01:43 +02001122 .config_init = at803x_config_init,
Michael Wallecde0f4f2020-04-28 23:15:02 +02001123 .soft_reset = genphy_soft_reset,
Daniel Mack13a56b42014-06-18 11:01:43 +02001124 .set_wol = at803x_set_wol,
1125 .get_wol = at803x_get_wol,
1126 .suspend = at803x_suspend,
1127 .resume = at803x_resume,
Heiner Kallweitdcdecdc2019-04-12 20:47:03 +02001128 /* PHY_GBIT_FEATURES */
Russell King06d5f342019-10-04 17:06:14 +01001129 .read_status = at803x_read_status,
Zefir Kurtisif62265b2016-10-24 12:40:54 +02001130 .aneg_done = at803x_aneg_done,
Daniel Mack13a56b42014-06-18 11:01:43 +02001131 .ack_interrupt = &at803x_ack_interrupt,
1132 .config_intr = &at803x_config_intr,
Ioana Ciornei29773092020-11-01 14:50:59 +02001133 .handle_interrupt = at803x_handle_interrupt,
Michael Wallecde0f4f2020-04-28 23:15:02 +02001134 .get_tunable = at803x_get_tunable,
1135 .set_tunable = at803x_set_tunable,
Michael Walle6cb75762020-05-13 22:38:07 +02001136 .cable_test_start = at803x_cable_test_start,
1137 .cable_test_get_status = at803x_cable_test_get_status,
Oleksij Rempel7908d2c2019-10-03 10:21:12 +02001138}, {
David Bauer58000912020-04-17 15:41:59 +02001139 /* Qualcomm Atheros AR8032 */
1140 PHY_ID_MATCH_EXACT(ATH8032_PHY_ID),
1141 .name = "Qualcomm Atheros AR8032",
1142 .probe = at803x_probe,
1143 .remove = at803x_remove,
Oleksij Rempeldc0f3ed2020-05-27 07:08:43 +02001144 .flags = PHY_POLL_CABLE_TEST,
David Bauer58000912020-04-17 15:41:59 +02001145 .config_init = at803x_config_init,
1146 .link_change_notify = at803x_link_change_notify,
1147 .set_wol = at803x_set_wol,
1148 .get_wol = at803x_get_wol,
1149 .suspend = at803x_suspend,
1150 .resume = at803x_resume,
1151 /* PHY_BASIC_FEATURES */
1152 .ack_interrupt = at803x_ack_interrupt,
1153 .config_intr = at803x_config_intr,
Ioana Ciornei29773092020-11-01 14:50:59 +02001154 .handle_interrupt = at803x_handle_interrupt,
Oleksij Rempeldc0f3ed2020-05-27 07:08:43 +02001155 .cable_test_start = at803x_cable_test_start,
1156 .cable_test_get_status = at803x_cable_test_get_status,
David Bauer58000912020-04-17 15:41:59 +02001157}, {
Oleksij Rempel7908d2c2019-10-03 10:21:12 +02001158 /* ATHEROS AR9331 */
1159 PHY_ID_MATCH_EXACT(ATH9331_PHY_ID),
Michael Walle96c36712019-11-06 23:36:16 +01001160 .name = "Qualcomm Atheros AR9331 built-in PHY",
Oleksij Rempel7908d2c2019-10-03 10:21:12 +02001161 .suspend = at803x_suspend,
1162 .resume = at803x_resume,
Oleksij Rempeldc0f3ed2020-05-27 07:08:43 +02001163 .flags = PHY_POLL_CABLE_TEST,
Oleksij Rempel7908d2c2019-10-03 10:21:12 +02001164 /* PHY_BASIC_FEATURES */
1165 .ack_interrupt = &at803x_ack_interrupt,
1166 .config_intr = &at803x_config_intr,
Ioana Ciornei29773092020-11-01 14:50:59 +02001167 .handle_interrupt = at803x_handle_interrupt,
Oleksij Rempeldc0f3ed2020-05-27 07:08:43 +02001168 .cable_test_start = at803x_cable_test_start,
1169 .cable_test_get_status = at803x_cable_test_get_status,
Oleksij Rempel7dce80c2020-07-19 10:05:30 +02001170 .read_status = at803x_read_status,
1171 .soft_reset = genphy_soft_reset,
1172 .config_aneg = at803x_config_aneg,
Mugunthan V N317420a2013-06-03 20:10:04 +00001173} };
Matus Ujhelyi0ca71112012-10-14 19:07:16 +00001174
Johan Hovold50fd7152014-11-11 19:45:59 +01001175module_phy_driver(at803x_driver);
Matus Ujhelyi0ca71112012-10-14 19:07:16 +00001176
1177static struct mdio_device_id __maybe_unused atheros_tbl[] = {
Michael Walle0465d8f2020-05-22 11:53:31 +02001178 { ATH8030_PHY_ID, AT8030_PHY_ID_MASK },
1179 { PHY_ID_MATCH_EXACT(ATH8031_PHY_ID) },
David Bauer58000912020-04-17 15:41:59 +02001180 { PHY_ID_MATCH_EXACT(ATH8032_PHY_ID) },
Michael Walle0465d8f2020-05-22 11:53:31 +02001181 { PHY_ID_MATCH_EXACT(ATH8035_PHY_ID) },
Oleksij Rempel7908d2c2019-10-03 10:21:12 +02001182 { PHY_ID_MATCH_EXACT(ATH9331_PHY_ID) },
Matus Ujhelyi0ca71112012-10-14 19:07:16 +00001183 { }
1184};
1185
1186MODULE_DEVICE_TABLE(mdio, atheros_tbl);