blob: 481cf48c9b9e463feb0f4efd17f05c9d74bbef43 [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>
Daniel Mack13a56b42014-06-18 11:01:43 +020015#include <linux/of_gpio.h>
Michael Walle2f664822019-11-06 23:36:14 +010016#include <linux/bitfield.h>
Daniel Mack13a56b42014-06-18 11:01:43 +020017#include <linux/gpio/consumer.h>
Michael Walle2f664822019-11-06 23:36:14 +010018#include <linux/regulator/of_regulator.h>
19#include <linux/regulator/driver.h>
20#include <linux/regulator/consumer.h>
21#include <dt-bindings/net/qca-ar803x.h>
Matus Ujhelyi0ca71112012-10-14 19:07:16 +000022
Russell King06d5f342019-10-04 17:06:14 +010023#define AT803X_SPECIFIC_STATUS 0x11
24#define AT803X_SS_SPEED_MASK (3 << 14)
25#define AT803X_SS_SPEED_1000 (2 << 14)
26#define AT803X_SS_SPEED_100 (1 << 14)
27#define AT803X_SS_SPEED_10 (0 << 14)
28#define AT803X_SS_DUPLEX BIT(13)
29#define AT803X_SS_SPEED_DUPLEX_RESOLVED BIT(11)
30#define AT803X_SS_MDIX BIT(6)
31
Matus Ujhelyi0ca71112012-10-14 19:07:16 +000032#define AT803X_INTR_ENABLE 0x12
Martin Blumenstingle6e4a552016-01-15 01:55:24 +010033#define AT803X_INTR_ENABLE_AUTONEG_ERR BIT(15)
34#define AT803X_INTR_ENABLE_SPEED_CHANGED BIT(14)
35#define AT803X_INTR_ENABLE_DUPLEX_CHANGED BIT(13)
36#define AT803X_INTR_ENABLE_PAGE_RECEIVED BIT(12)
37#define AT803X_INTR_ENABLE_LINK_FAIL BIT(11)
38#define AT803X_INTR_ENABLE_LINK_SUCCESS BIT(10)
39#define AT803X_INTR_ENABLE_WIRESPEED_DOWNGRADE BIT(5)
40#define AT803X_INTR_ENABLE_POLARITY_CHANGED BIT(1)
41#define AT803X_INTR_ENABLE_WOL BIT(0)
42
Matus Ujhelyi0ca71112012-10-14 19:07:16 +000043#define AT803X_INTR_STATUS 0x13
Martin Blumenstingla46bd632016-01-15 01:55:23 +010044
Daniel Mack13a56b42014-06-18 11:01:43 +020045#define AT803X_SMART_SPEED 0x14
46#define AT803X_LED_CONTROL 0x18
Martin Blumenstingla46bd632016-01-15 01:55:23 +010047
Matus Ujhelyi0ca71112012-10-14 19:07:16 +000048#define AT803X_DEVICE_ADDR 0x03
49#define AT803X_LOC_MAC_ADDR_0_15_OFFSET 0x804C
50#define AT803X_LOC_MAC_ADDR_16_31_OFFSET 0x804B
51#define AT803X_LOC_MAC_ADDR_32_47_OFFSET 0x804A
Zefir Kurtisif62265b2016-10-24 12:40:54 +020052#define AT803X_REG_CHIP_CONFIG 0x1f
53#define AT803X_BT_BX_REG_SEL 0x8000
Martin Blumenstingla46bd632016-01-15 01:55:23 +010054
Mugunthan V N1ca6d1b2013-06-03 20:10:06 +000055#define AT803X_DEBUG_ADDR 0x1D
56#define AT803X_DEBUG_DATA 0x1E
Martin Blumenstingla46bd632016-01-15 01:55:23 +010057
Zefir Kurtisif62265b2016-10-24 12:40:54 +020058#define AT803X_MODE_CFG_MASK 0x0F
59#define AT803X_MODE_CFG_SGMII 0x01
60
61#define AT803X_PSSR 0x11 /*PHY-Specific Status Register*/
62#define AT803X_PSSR_MR_AN_COMPLETE 0x0200
63
Martin Blumenstingl2e5f9f22016-01-15 01:55:22 +010064#define AT803X_DEBUG_REG_0 0x00
65#define AT803X_DEBUG_RX_CLK_DLY_EN BIT(15)
Martin Blumenstingla46bd632016-01-15 01:55:23 +010066
Martin Blumenstingl2e5f9f22016-01-15 01:55:22 +010067#define AT803X_DEBUG_REG_5 0x05
68#define AT803X_DEBUG_TX_CLK_DLY_EN BIT(8)
Matus Ujhelyi0ca71112012-10-14 19:07:16 +000069
Michael Walle2f664822019-11-06 23:36:14 +010070#define AT803X_DEBUG_REG_1F 0x1F
71#define AT803X_DEBUG_PLL_ON BIT(2)
72#define AT803X_DEBUG_RGMII_1V8 BIT(3)
73
74/* AT803x supports either the XTAL input pad, an internal PLL or the
75 * DSP as clock reference for the clock output pad. The XTAL reference
76 * is only used for 25 MHz output, all other frequencies need the PLL.
77 * The DSP as a clock reference is used in synchronous ethernet
78 * applications.
79 *
80 * By default the PLL is only enabled if there is a link. Otherwise
81 * the PHY will go into low power state and disabled the PLL. You can
82 * set the PLL_ON bit (see debug register 0x1f) to keep the PLL always
83 * enabled.
84 */
85#define AT803X_MMD7_CLK25M 0x8016
86#define AT803X_CLK_OUT_MASK GENMASK(4, 2)
87#define AT803X_CLK_OUT_25MHZ_XTAL 0
88#define AT803X_CLK_OUT_25MHZ_DSP 1
89#define AT803X_CLK_OUT_50MHZ_PLL 2
90#define AT803X_CLK_OUT_50MHZ_DSP 3
91#define AT803X_CLK_OUT_62_5MHZ_PLL 4
92#define AT803X_CLK_OUT_62_5MHZ_DSP 5
93#define AT803X_CLK_OUT_125MHZ_PLL 6
94#define AT803X_CLK_OUT_125MHZ_DSP 7
95
Michael Walle428061f2019-11-06 23:36:15 +010096/* The AR8035 has another mask which is compatible with the AR8031/AR8033 mask
97 * but doesn't support choosing between XTAL/PLL and DSP.
Michael Walle2f664822019-11-06 23:36:14 +010098 */
99#define AT8035_CLK_OUT_MASK GENMASK(4, 3)
100
101#define AT803X_CLK_OUT_STRENGTH_MASK GENMASK(8, 7)
102#define AT803X_CLK_OUT_STRENGTH_FULL 0
103#define AT803X_CLK_OUT_STRENGTH_HALF 1
104#define AT803X_CLK_OUT_STRENGTH_QUARTER 2
105
Oleksij Rempel7908d2c2019-10-03 10:21:12 +0200106#define ATH9331_PHY_ID 0x004dd041
Daniel Mackbd8ca172014-06-18 11:01:42 +0200107#define ATH8030_PHY_ID 0x004dd076
108#define ATH8031_PHY_ID 0x004dd074
109#define ATH8035_PHY_ID 0x004dd072
Fabio Estevam58effd72016-10-26 14:03:54 -0200110#define AT803X_PHY_ID_MASK 0xffffffef
Daniel Mackbd8ca172014-06-18 11:01:42 +0200111
Michael Walle96c36712019-11-06 23:36:16 +0100112MODULE_DESCRIPTION("Qualcomm Atheros AR803x PHY driver");
Matus Ujhelyi0ca71112012-10-14 19:07:16 +0000113MODULE_AUTHOR("Matus Ujhelyi");
114MODULE_LICENSE("GPL");
115
Michael Walle2f664822019-11-06 23:36:14 +0100116struct at803x_priv {
117 int flags;
118#define AT803X_KEEP_PLL_ENABLED BIT(0) /* don't turn off internal PLL */
119 u16 clk_25m_reg;
120 u16 clk_25m_mask;
121 struct regulator_dev *vddio_rdev;
122 struct regulator_dev *vddh_rdev;
123 struct regulator *vddio;
124};
125
Daniel Mack13a56b42014-06-18 11:01:43 +0200126struct at803x_context {
127 u16 bmcr;
128 u16 advertise;
129 u16 control1000;
130 u16 int_enable;
131 u16 smart_speed;
132 u16 led_control;
133};
134
Martin Blumenstingl2e5f9f22016-01-15 01:55:22 +0100135static int at803x_debug_reg_read(struct phy_device *phydev, u16 reg)
136{
137 int ret;
138
139 ret = phy_write(phydev, AT803X_DEBUG_ADDR, reg);
140 if (ret < 0)
141 return ret;
142
143 return phy_read(phydev, AT803X_DEBUG_DATA);
144}
145
146static int at803x_debug_reg_mask(struct phy_device *phydev, u16 reg,
147 u16 clear, u16 set)
148{
149 u16 val;
150 int ret;
151
152 ret = at803x_debug_reg_read(phydev, reg);
153 if (ret < 0)
154 return ret;
155
156 val = ret & 0xffff;
157 val &= ~clear;
158 val |= set;
159
160 return phy_write(phydev, AT803X_DEBUG_DATA, val);
161}
162
Vinod Koul6d4cd042019-02-21 15:53:15 +0530163static int at803x_enable_rx_delay(struct phy_device *phydev)
164{
165 return at803x_debug_reg_mask(phydev, AT803X_DEBUG_REG_0, 0,
166 AT803X_DEBUG_RX_CLK_DLY_EN);
167}
168
169static int at803x_enable_tx_delay(struct phy_device *phydev)
170{
171 return at803x_debug_reg_mask(phydev, AT803X_DEBUG_REG_5, 0,
172 AT803X_DEBUG_TX_CLK_DLY_EN);
173}
174
Vinod Koul43f2ebd2019-02-21 15:53:14 +0530175static int at803x_disable_rx_delay(struct phy_device *phydev)
Martin Blumenstingl2e5f9f22016-01-15 01:55:22 +0100176{
Vinod Koulcd28d1d2019-01-21 14:43:17 +0530177 return at803x_debug_reg_mask(phydev, AT803X_DEBUG_REG_0,
178 AT803X_DEBUG_RX_CLK_DLY_EN, 0);
Martin Blumenstingl2e5f9f22016-01-15 01:55:22 +0100179}
180
Vinod Koul43f2ebd2019-02-21 15:53:14 +0530181static int at803x_disable_tx_delay(struct phy_device *phydev)
Martin Blumenstingl2e5f9f22016-01-15 01:55:22 +0100182{
Vinod Koulcd28d1d2019-01-21 14:43:17 +0530183 return at803x_debug_reg_mask(phydev, AT803X_DEBUG_REG_5,
184 AT803X_DEBUG_TX_CLK_DLY_EN, 0);
Martin Blumenstingl2e5f9f22016-01-15 01:55:22 +0100185}
186
Daniel Mack13a56b42014-06-18 11:01:43 +0200187/* save relevant PHY registers to private copy */
188static void at803x_context_save(struct phy_device *phydev,
189 struct at803x_context *context)
190{
191 context->bmcr = phy_read(phydev, MII_BMCR);
192 context->advertise = phy_read(phydev, MII_ADVERTISE);
193 context->control1000 = phy_read(phydev, MII_CTRL1000);
194 context->int_enable = phy_read(phydev, AT803X_INTR_ENABLE);
195 context->smart_speed = phy_read(phydev, AT803X_SMART_SPEED);
196 context->led_control = phy_read(phydev, AT803X_LED_CONTROL);
197}
198
199/* restore relevant PHY registers from private copy */
200static void at803x_context_restore(struct phy_device *phydev,
201 const struct at803x_context *context)
202{
203 phy_write(phydev, MII_BMCR, context->bmcr);
204 phy_write(phydev, MII_ADVERTISE, context->advertise);
205 phy_write(phydev, MII_CTRL1000, context->control1000);
206 phy_write(phydev, AT803X_INTR_ENABLE, context->int_enable);
207 phy_write(phydev, AT803X_SMART_SPEED, context->smart_speed);
208 phy_write(phydev, AT803X_LED_CONTROL, context->led_control);
209}
210
Mugunthan V Nea13c9e2013-06-03 20:10:05 +0000211static int at803x_set_wol(struct phy_device *phydev,
212 struct ethtool_wolinfo *wol)
Matus Ujhelyi0ca71112012-10-14 19:07:16 +0000213{
214 struct net_device *ndev = phydev->attached_dev;
215 const u8 *mac;
Mugunthan V Nea13c9e2013-06-03 20:10:05 +0000216 int ret;
217 u32 value;
Matus Ujhelyi0ca71112012-10-14 19:07:16 +0000218 unsigned int i, offsets[] = {
219 AT803X_LOC_MAC_ADDR_32_47_OFFSET,
220 AT803X_LOC_MAC_ADDR_16_31_OFFSET,
221 AT803X_LOC_MAC_ADDR_0_15_OFFSET,
222 };
223
224 if (!ndev)
Mugunthan V Nea13c9e2013-06-03 20:10:05 +0000225 return -ENODEV;
Matus Ujhelyi0ca71112012-10-14 19:07:16 +0000226
Mugunthan V Nea13c9e2013-06-03 20:10:05 +0000227 if (wol->wolopts & WAKE_MAGIC) {
228 mac = (const u8 *) ndev->dev_addr;
Matus Ujhelyi0ca71112012-10-14 19:07:16 +0000229
Mugunthan V Nea13c9e2013-06-03 20:10:05 +0000230 if (!is_valid_ether_addr(mac))
Dan Murphyfc755682017-10-10 12:42:56 -0500231 return -EINVAL;
Matus Ujhelyi0ca71112012-10-14 19:07:16 +0000232
Carlo Caione0e021392019-01-25 12:35:10 +0000233 for (i = 0; i < 3; i++)
234 phy_write_mmd(phydev, AT803X_DEVICE_ADDR, offsets[i],
235 mac[(i * 2) + 1] | (mac[(i * 2)] << 8));
Mugunthan V Nea13c9e2013-06-03 20:10:05 +0000236
237 value = phy_read(phydev, AT803X_INTR_ENABLE);
Martin Blumenstingle6e4a552016-01-15 01:55:24 +0100238 value |= AT803X_INTR_ENABLE_WOL;
Mugunthan V Nea13c9e2013-06-03 20:10:05 +0000239 ret = phy_write(phydev, AT803X_INTR_ENABLE, value);
240 if (ret)
241 return ret;
242 value = phy_read(phydev, AT803X_INTR_STATUS);
243 } else {
244 value = phy_read(phydev, AT803X_INTR_ENABLE);
Martin Blumenstingle6e4a552016-01-15 01:55:24 +0100245 value &= (~AT803X_INTR_ENABLE_WOL);
Mugunthan V Nea13c9e2013-06-03 20:10:05 +0000246 ret = phy_write(phydev, AT803X_INTR_ENABLE, value);
247 if (ret)
248 return ret;
249 value = phy_read(phydev, AT803X_INTR_STATUS);
Matus Ujhelyi0ca71112012-10-14 19:07:16 +0000250 }
Mugunthan V Nea13c9e2013-06-03 20:10:05 +0000251
252 return ret;
253}
254
255static void at803x_get_wol(struct phy_device *phydev,
256 struct ethtool_wolinfo *wol)
257{
258 u32 value;
259
260 wol->supported = WAKE_MAGIC;
261 wol->wolopts = 0;
262
263 value = phy_read(phydev, AT803X_INTR_ENABLE);
Martin Blumenstingle6e4a552016-01-15 01:55:24 +0100264 if (value & AT803X_INTR_ENABLE_WOL)
Mugunthan V Nea13c9e2013-06-03 20:10:05 +0000265 wol->wolopts |= WAKE_MAGIC;
Matus Ujhelyi0ca71112012-10-14 19:07:16 +0000266}
267
Daniel Mack6229ed12013-09-21 16:53:02 +0200268static int at803x_suspend(struct phy_device *phydev)
269{
270 int value;
271 int wol_enabled;
272
Daniel Mack6229ed12013-09-21 16:53:02 +0200273 value = phy_read(phydev, AT803X_INTR_ENABLE);
Martin Blumenstingle6e4a552016-01-15 01:55:24 +0100274 wol_enabled = value & AT803X_INTR_ENABLE_WOL;
Daniel Mack6229ed12013-09-21 16:53:02 +0200275
Daniel Mack6229ed12013-09-21 16:53:02 +0200276 if (wol_enabled)
Russell Kingfea23fb2018-01-02 10:58:58 +0000277 value = BMCR_ISOLATE;
Daniel Mack6229ed12013-09-21 16:53:02 +0200278 else
Russell Kingfea23fb2018-01-02 10:58:58 +0000279 value = BMCR_PDOWN;
Daniel Mack6229ed12013-09-21 16:53:02 +0200280
Russell Kingfea23fb2018-01-02 10:58:58 +0000281 phy_modify(phydev, MII_BMCR, 0, value);
Daniel Mack6229ed12013-09-21 16:53:02 +0200282
283 return 0;
284}
285
286static int at803x_resume(struct phy_device *phydev)
287{
Russell Kingf1028522018-01-05 16:07:10 +0000288 return phy_modify(phydev, MII_BMCR, BMCR_PDOWN | BMCR_ISOLATE, 0);
Daniel Mack6229ed12013-09-21 16:53:02 +0200289}
290
Michael Walle2f664822019-11-06 23:36:14 +0100291static int at803x_rgmii_reg_set_voltage_sel(struct regulator_dev *rdev,
292 unsigned int selector)
293{
294 struct phy_device *phydev = rdev_get_drvdata(rdev);
295
296 if (selector)
297 return at803x_debug_reg_mask(phydev, AT803X_DEBUG_REG_1F,
298 0, AT803X_DEBUG_RGMII_1V8);
299 else
300 return at803x_debug_reg_mask(phydev, AT803X_DEBUG_REG_1F,
301 AT803X_DEBUG_RGMII_1V8, 0);
302}
303
304static int at803x_rgmii_reg_get_voltage_sel(struct regulator_dev *rdev)
305{
306 struct phy_device *phydev = rdev_get_drvdata(rdev);
307 int val;
308
309 val = at803x_debug_reg_read(phydev, AT803X_DEBUG_REG_1F);
310 if (val < 0)
311 return val;
312
313 return (val & AT803X_DEBUG_RGMII_1V8) ? 1 : 0;
314}
315
316static struct regulator_ops vddio_regulator_ops = {
317 .list_voltage = regulator_list_voltage_table,
318 .set_voltage_sel = at803x_rgmii_reg_set_voltage_sel,
319 .get_voltage_sel = at803x_rgmii_reg_get_voltage_sel,
320};
321
322static const unsigned int vddio_voltage_table[] = {
323 1500000,
324 1800000,
325};
326
327static const struct regulator_desc vddio_desc = {
328 .name = "vddio",
329 .of_match = of_match_ptr("vddio-regulator"),
330 .n_voltages = ARRAY_SIZE(vddio_voltage_table),
331 .volt_table = vddio_voltage_table,
332 .ops = &vddio_regulator_ops,
333 .type = REGULATOR_VOLTAGE,
334 .owner = THIS_MODULE,
335};
336
337static struct regulator_ops vddh_regulator_ops = {
338};
339
340static const struct regulator_desc vddh_desc = {
341 .name = "vddh",
342 .of_match = of_match_ptr("vddh-regulator"),
343 .n_voltages = 1,
344 .fixed_uV = 2500000,
345 .ops = &vddh_regulator_ops,
346 .type = REGULATOR_VOLTAGE,
347 .owner = THIS_MODULE,
348};
349
350static int at8031_register_regulators(struct phy_device *phydev)
351{
352 struct at803x_priv *priv = phydev->priv;
353 struct device *dev = &phydev->mdio.dev;
354 struct regulator_config config = { };
355
356 config.dev = dev;
357 config.driver_data = phydev;
358
359 priv->vddio_rdev = devm_regulator_register(dev, &vddio_desc, &config);
360 if (IS_ERR(priv->vddio_rdev)) {
361 phydev_err(phydev, "failed to register VDDIO regulator\n");
362 return PTR_ERR(priv->vddio_rdev);
363 }
364
365 priv->vddh_rdev = devm_regulator_register(dev, &vddh_desc, &config);
366 if (IS_ERR(priv->vddh_rdev)) {
367 phydev_err(phydev, "failed to register VDDH regulator\n");
368 return PTR_ERR(priv->vddh_rdev);
369 }
370
371 return 0;
372}
373
374static bool at803x_match_phy_id(struct phy_device *phydev, u32 phy_id)
375{
376 return (phydev->phy_id & phydev->drv->phy_id_mask)
377 == (phy_id & phydev->drv->phy_id_mask);
378}
379
380static int at803x_parse_dt(struct phy_device *phydev)
381{
382 struct device_node *node = phydev->mdio.dev.of_node;
383 struct at803x_priv *priv = phydev->priv;
384 unsigned int sel, mask;
385 u32 freq, strength;
386 int ret;
387
388 if (!IS_ENABLED(CONFIG_OF_MDIO))
389 return 0;
390
391 ret = of_property_read_u32(node, "qca,clk-out-frequency", &freq);
392 if (!ret) {
393 mask = AT803X_CLK_OUT_MASK;
394 switch (freq) {
395 case 25000000:
396 sel = AT803X_CLK_OUT_25MHZ_XTAL;
397 break;
398 case 50000000:
399 sel = AT803X_CLK_OUT_50MHZ_PLL;
400 break;
401 case 62500000:
402 sel = AT803X_CLK_OUT_62_5MHZ_PLL;
403 break;
404 case 125000000:
405 sel = AT803X_CLK_OUT_125MHZ_PLL;
406 break;
407 default:
408 phydev_err(phydev, "invalid qca,clk-out-frequency\n");
409 return -EINVAL;
410 }
411
412 priv->clk_25m_reg |= FIELD_PREP(mask, sel);
413 priv->clk_25m_mask |= mask;
414
415 /* Fixup for the AR8030/AR8035. This chip has another mask and
416 * doesn't support the DSP reference. Eg. the lowest bit of the
417 * mask. The upper two bits select the same frequencies. Mask
418 * the lowest bit here.
419 *
420 * Warning:
421 * There was no datasheet for the AR8030 available so this is
422 * just a guess. But the AR8035 is listed as pin compatible
423 * to the AR8030 so there might be a good chance it works on
424 * the AR8030 too.
425 */
426 if (at803x_match_phy_id(phydev, ATH8030_PHY_ID) ||
427 at803x_match_phy_id(phydev, ATH8035_PHY_ID)) {
428 priv->clk_25m_reg &= ~AT8035_CLK_OUT_MASK;
429 priv->clk_25m_mask &= ~AT8035_CLK_OUT_MASK;
430 }
431 }
432
433 ret = of_property_read_u32(node, "qca,clk-out-strength", &strength);
434 if (!ret) {
435 priv->clk_25m_mask |= AT803X_CLK_OUT_STRENGTH_MASK;
436 switch (strength) {
437 case AR803X_STRENGTH_FULL:
438 priv->clk_25m_reg |= AT803X_CLK_OUT_STRENGTH_FULL;
439 break;
440 case AR803X_STRENGTH_HALF:
441 priv->clk_25m_reg |= AT803X_CLK_OUT_STRENGTH_HALF;
442 break;
443 case AR803X_STRENGTH_QUARTER:
444 priv->clk_25m_reg |= AT803X_CLK_OUT_STRENGTH_QUARTER;
445 break;
446 default:
447 phydev_err(phydev, "invalid qca,clk-out-strength\n");
448 return -EINVAL;
449 }
450 }
451
Michael Walle428061f2019-11-06 23:36:15 +0100452 /* Only supported on AR8031/AR8033, the AR8030/AR8035 use strapping
453 * options.
454 */
Michael Walle2f664822019-11-06 23:36:14 +0100455 if (at803x_match_phy_id(phydev, ATH8031_PHY_ID)) {
456 if (of_property_read_bool(node, "qca,keep-pll-enabled"))
457 priv->flags |= AT803X_KEEP_PLL_ENABLED;
458
459 ret = at8031_register_regulators(phydev);
460 if (ret < 0)
461 return ret;
462
463 priv->vddio = devm_regulator_get_optional(&phydev->mdio.dev,
464 "vddio");
465 if (IS_ERR(priv->vddio)) {
466 phydev_err(phydev, "failed to get VDDIO regulator\n");
467 return PTR_ERR(priv->vddio);
468 }
469
470 ret = regulator_enable(priv->vddio);
471 if (ret < 0)
472 return ret;
473 }
474
475 return 0;
476}
477
478static int at803x_probe(struct phy_device *phydev)
479{
480 struct device *dev = &phydev->mdio.dev;
481 struct at803x_priv *priv;
482
483 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
484 if (!priv)
485 return -ENOMEM;
486
487 phydev->priv = priv;
488
489 return at803x_parse_dt(phydev);
490}
491
Michael Walle2318ca82020-01-30 18:54:02 +0100492static void at803x_remove(struct phy_device *phydev)
493{
494 struct at803x_priv *priv = phydev->priv;
495
496 if (priv->vddio)
497 regulator_disable(priv->vddio);
498}
499
Michael Walle2f664822019-11-06 23:36:14 +0100500static int at803x_clk_out_config(struct phy_device *phydev)
501{
502 struct at803x_priv *priv = phydev->priv;
503 int val;
504
505 if (!priv->clk_25m_mask)
506 return 0;
507
508 val = phy_read_mmd(phydev, MDIO_MMD_AN, AT803X_MMD7_CLK25M);
509 if (val < 0)
510 return val;
511
512 val &= ~priv->clk_25m_mask;
513 val |= priv->clk_25m_reg;
514
515 return phy_write_mmd(phydev, MDIO_MMD_AN, AT803X_MMD7_CLK25M, val);
516}
517
518static int at8031_pll_config(struct phy_device *phydev)
519{
520 struct at803x_priv *priv = phydev->priv;
521
522 /* The default after hardware reset is PLL OFF. After a soft reset, the
523 * values are retained.
524 */
525 if (priv->flags & AT803X_KEEP_PLL_ENABLED)
526 return at803x_debug_reg_mask(phydev, AT803X_DEBUG_REG_1F,
527 0, AT803X_DEBUG_PLL_ON);
528 else
529 return at803x_debug_reg_mask(phydev, AT803X_DEBUG_REG_1F,
530 AT803X_DEBUG_PLL_ON, 0);
531}
532
Matus Ujhelyi0ca71112012-10-14 19:07:16 +0000533static int at803x_config_init(struct phy_device *phydev)
534{
Mugunthan V N1ca6d1b2013-06-03 20:10:06 +0000535 int ret;
Matus Ujhelyi0ca71112012-10-14 19:07:16 +0000536
Vinod Koul6d4cd042019-02-21 15:53:15 +0530537 /* The RX and TX delay default is:
538 * after HW reset: RX delay enabled and TX delay disabled
539 * after SW reset: RX delay enabled, while TX delay retains the
540 * value before reset.
Vinod Koul6d4cd042019-02-21 15:53:15 +0530541 */
Vinod Koul6d4cd042019-02-21 15:53:15 +0530542 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID ||
André Draszikbb0ce4c2019-08-09 12:20:25 +0100543 phydev->interface == PHY_INTERFACE_MODE_RGMII_RXID)
Vinod Koul6d4cd042019-02-21 15:53:15 +0530544 ret = at803x_enable_rx_delay(phydev);
André Draszikbb0ce4c2019-08-09 12:20:25 +0100545 else
546 ret = at803x_disable_rx_delay(phydev);
547 if (ret < 0)
548 return ret;
Martin Blumenstingl2e5f9f22016-01-15 01:55:22 +0100549
Vinod Koul6d4cd042019-02-21 15:53:15 +0530550 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID ||
André Draszikbb0ce4c2019-08-09 12:20:25 +0100551 phydev->interface == PHY_INTERFACE_MODE_RGMII_TXID)
Vinod Koul6d4cd042019-02-21 15:53:15 +0530552 ret = at803x_enable_tx_delay(phydev);
André Draszikbb0ce4c2019-08-09 12:20:25 +0100553 else
554 ret = at803x_disable_tx_delay(phydev);
Michael Walle2f664822019-11-06 23:36:14 +0100555 if (ret < 0)
556 return ret;
Mugunthan V N1ca6d1b2013-06-03 20:10:06 +0000557
Michael Walle2f664822019-11-06 23:36:14 +0100558 ret = at803x_clk_out_config(phydev);
559 if (ret < 0)
560 return ret;
561
562 if (at803x_match_phy_id(phydev, ATH8031_PHY_ID)) {
563 ret = at8031_pll_config(phydev);
564 if (ret < 0)
565 return ret;
566 }
567
568 return 0;
Matus Ujhelyi0ca71112012-10-14 19:07:16 +0000569}
570
Zhao Qiang77a99392014-03-28 15:39:41 +0800571static int at803x_ack_interrupt(struct phy_device *phydev)
572{
573 int err;
574
Martin Blumenstingla46bd632016-01-15 01:55:23 +0100575 err = phy_read(phydev, AT803X_INTR_STATUS);
Zhao Qiang77a99392014-03-28 15:39:41 +0800576
577 return (err < 0) ? err : 0;
578}
579
580static int at803x_config_intr(struct phy_device *phydev)
581{
582 int err;
583 int value;
584
Martin Blumenstingla46bd632016-01-15 01:55:23 +0100585 value = phy_read(phydev, AT803X_INTR_ENABLE);
Zhao Qiang77a99392014-03-28 15:39:41 +0800586
Martin Blumenstingle6e4a552016-01-15 01:55:24 +0100587 if (phydev->interrupts == PHY_INTERRUPT_ENABLED) {
588 value |= AT803X_INTR_ENABLE_AUTONEG_ERR;
589 value |= AT803X_INTR_ENABLE_SPEED_CHANGED;
590 value |= AT803X_INTR_ENABLE_DUPLEX_CHANGED;
591 value |= AT803X_INTR_ENABLE_LINK_FAIL;
592 value |= AT803X_INTR_ENABLE_LINK_SUCCESS;
593
594 err = phy_write(phydev, AT803X_INTR_ENABLE, value);
595 }
Zhao Qiang77a99392014-03-28 15:39:41 +0800596 else
Martin Blumenstingla46bd632016-01-15 01:55:23 +0100597 err = phy_write(phydev, AT803X_INTR_ENABLE, 0);
Zhao Qiang77a99392014-03-28 15:39:41 +0800598
599 return err;
600}
601
Daniel Mack13a56b42014-06-18 11:01:43 +0200602static void at803x_link_change_notify(struct phy_device *phydev)
603{
Daniel Mack13a56b42014-06-18 11:01:43 +0200604 /*
605 * Conduct a hardware reset for AT8030 every time a link loss is
606 * signalled. This is necessary to circumvent a hardware bug that
607 * occurs when the cable is unplugged while TX packets are pending
608 * in the FIFO. In such cases, the FIFO enters an error mode it
609 * cannot recover from by software.
610 */
David Bauer6110ed22019-04-17 23:59:22 +0200611 if (phydev->state == PHY_NOLINK && phydev->mdio.reset_gpio) {
Heiner Kallweit5c5f6262019-03-19 19:56:51 +0100612 struct at803x_context context;
Daniel Mack13a56b42014-06-18 11:01:43 +0200613
Heiner Kallweit5c5f6262019-03-19 19:56:51 +0100614 at803x_context_save(phydev, &context);
Daniel Mack13a56b42014-06-18 11:01:43 +0200615
Heiner Kallweit5c5f6262019-03-19 19:56:51 +0100616 phy_device_reset(phydev, 1);
617 msleep(1);
618 phy_device_reset(phydev, 0);
619 msleep(1);
Daniel Mack13a56b42014-06-18 11:01:43 +0200620
Heiner Kallweit5c5f6262019-03-19 19:56:51 +0100621 at803x_context_restore(phydev, &context);
Daniel Mack13a56b42014-06-18 11:01:43 +0200622
Heiner Kallweit5c5f6262019-03-19 19:56:51 +0100623 phydev_dbg(phydev, "%s(): phy was reset\n", __func__);
Daniel Mack13a56b42014-06-18 11:01:43 +0200624 }
625}
626
Zefir Kurtisif62265b2016-10-24 12:40:54 +0200627static int at803x_aneg_done(struct phy_device *phydev)
628{
629 int ccr;
630
631 int aneg_done = genphy_aneg_done(phydev);
632 if (aneg_done != BMSR_ANEGCOMPLETE)
633 return aneg_done;
634
635 /*
636 * in SGMII mode, if copper side autoneg is successful,
637 * also check SGMII side autoneg result
638 */
639 ccr = phy_read(phydev, AT803X_REG_CHIP_CONFIG);
640 if ((ccr & AT803X_MODE_CFG_MASK) != AT803X_MODE_CFG_SGMII)
641 return aneg_done;
642
643 /* switch to SGMII/fiber page */
644 phy_write(phydev, AT803X_REG_CHIP_CONFIG, ccr & ~AT803X_BT_BX_REG_SEL);
645
646 /* check if the SGMII link is OK. */
647 if (!(phy_read(phydev, AT803X_PSSR) & AT803X_PSSR_MR_AN_COMPLETE)) {
Andrew Lunnab2a6052018-09-29 23:04:10 +0200648 phydev_warn(phydev, "803x_aneg_done: SGMII link is not ok\n");
Zefir Kurtisif62265b2016-10-24 12:40:54 +0200649 aneg_done = 0;
650 }
651 /* switch back to copper page */
652 phy_write(phydev, AT803X_REG_CHIP_CONFIG, ccr | AT803X_BT_BX_REG_SEL);
653
654 return aneg_done;
655}
656
Russell King06d5f342019-10-04 17:06:14 +0100657static int at803x_read_status(struct phy_device *phydev)
658{
659 int ss, err, old_link = phydev->link;
660
661 /* Update the link, but return if there was an error */
662 err = genphy_update_link(phydev);
663 if (err)
664 return err;
665
666 /* why bother the PHY if nothing can have changed */
667 if (phydev->autoneg == AUTONEG_ENABLE && old_link && phydev->link)
668 return 0;
669
670 phydev->speed = SPEED_UNKNOWN;
671 phydev->duplex = DUPLEX_UNKNOWN;
672 phydev->pause = 0;
673 phydev->asym_pause = 0;
674
675 err = genphy_read_lpa(phydev);
676 if (err < 0)
677 return err;
678
679 /* Read the AT8035 PHY-Specific Status register, which indicates the
680 * speed and duplex that the PHY is actually using, irrespective of
681 * whether we are in autoneg mode or not.
682 */
683 ss = phy_read(phydev, AT803X_SPECIFIC_STATUS);
684 if (ss < 0)
685 return ss;
686
687 if (ss & AT803X_SS_SPEED_DUPLEX_RESOLVED) {
688 switch (ss & AT803X_SS_SPEED_MASK) {
689 case AT803X_SS_SPEED_10:
690 phydev->speed = SPEED_10;
691 break;
692 case AT803X_SS_SPEED_100:
693 phydev->speed = SPEED_100;
694 break;
695 case AT803X_SS_SPEED_1000:
696 phydev->speed = SPEED_1000;
697 break;
698 }
699 if (ss & AT803X_SS_DUPLEX)
700 phydev->duplex = DUPLEX_FULL;
701 else
702 phydev->duplex = DUPLEX_HALF;
703 if (ss & AT803X_SS_MDIX)
704 phydev->mdix = ETH_TP_MDI_X;
705 else
706 phydev->mdix = ETH_TP_MDI;
707 }
708
709 if (phydev->autoneg == AUTONEG_ENABLE && phydev->autoneg_complete)
710 phy_resolve_aneg_pause(phydev);
711
712 return 0;
713}
714
Mugunthan V N317420a2013-06-03 20:10:04 +0000715static struct phy_driver at803x_driver[] = {
716{
Michael Walle96c36712019-11-06 23:36:16 +0100717 /* Qualcomm Atheros AR8035 */
Daniel Mack13a56b42014-06-18 11:01:43 +0200718 .phy_id = ATH8035_PHY_ID,
Michael Walle96c36712019-11-06 23:36:16 +0100719 .name = "Qualcomm Atheros AR8035",
Fabio Estevam58effd72016-10-26 14:03:54 -0200720 .phy_id_mask = AT803X_PHY_ID_MASK,
Michael Walle2f664822019-11-06 23:36:14 +0100721 .probe = at803x_probe,
Michael Walle2318ca82020-01-30 18:54:02 +0100722 .remove = at803x_remove,
Daniel Mack13a56b42014-06-18 11:01:43 +0200723 .config_init = at803x_config_init,
Daniel Mack13a56b42014-06-18 11:01:43 +0200724 .set_wol = at803x_set_wol,
725 .get_wol = at803x_get_wol,
726 .suspend = at803x_suspend,
727 .resume = at803x_resume,
Heiner Kallweitdcdecdc2019-04-12 20:47:03 +0200728 /* PHY_GBIT_FEATURES */
Russell King06d5f342019-10-04 17:06:14 +0100729 .read_status = at803x_read_status,
Måns Rullgård0eae5982015-11-12 17:40:20 +0000730 .ack_interrupt = at803x_ack_interrupt,
731 .config_intr = at803x_config_intr,
Mugunthan V N317420a2013-06-03 20:10:04 +0000732}, {
Michael Walle96c36712019-11-06 23:36:16 +0100733 /* Qualcomm Atheros AR8030 */
Daniel Mack13a56b42014-06-18 11:01:43 +0200734 .phy_id = ATH8030_PHY_ID,
Michael Walle96c36712019-11-06 23:36:16 +0100735 .name = "Qualcomm Atheros AR8030",
Fabio Estevam58effd72016-10-26 14:03:54 -0200736 .phy_id_mask = AT803X_PHY_ID_MASK,
Michael Walle2f664822019-11-06 23:36:14 +0100737 .probe = at803x_probe,
Michael Walle2318ca82020-01-30 18:54:02 +0100738 .remove = at803x_remove,
Daniel Mack13a56b42014-06-18 11:01:43 +0200739 .config_init = at803x_config_init,
740 .link_change_notify = at803x_link_change_notify,
741 .set_wol = at803x_set_wol,
742 .get_wol = at803x_get_wol,
743 .suspend = at803x_suspend,
744 .resume = at803x_resume,
Heiner Kallweitdcdecdc2019-04-12 20:47:03 +0200745 /* PHY_BASIC_FEATURES */
Måns Rullgård0eae5982015-11-12 17:40:20 +0000746 .ack_interrupt = at803x_ack_interrupt,
747 .config_intr = at803x_config_intr,
Mugunthan V N05d7cce2013-06-03 20:10:07 +0000748}, {
Michael Walle96c36712019-11-06 23:36:16 +0100749 /* Qualcomm Atheros AR8031/AR8033 */
Daniel Mack13a56b42014-06-18 11:01:43 +0200750 .phy_id = ATH8031_PHY_ID,
Michael Walle96c36712019-11-06 23:36:16 +0100751 .name = "Qualcomm Atheros AR8031/AR8033",
Fabio Estevam58effd72016-10-26 14:03:54 -0200752 .phy_id_mask = AT803X_PHY_ID_MASK,
Michael Walle2f664822019-11-06 23:36:14 +0100753 .probe = at803x_probe,
Michael Walle2318ca82020-01-30 18:54:02 +0100754 .remove = at803x_remove,
Daniel Mack13a56b42014-06-18 11:01:43 +0200755 .config_init = at803x_config_init,
Daniel Mack13a56b42014-06-18 11:01:43 +0200756 .set_wol = at803x_set_wol,
757 .get_wol = at803x_get_wol,
758 .suspend = at803x_suspend,
759 .resume = at803x_resume,
Heiner Kallweitdcdecdc2019-04-12 20:47:03 +0200760 /* PHY_GBIT_FEATURES */
Russell King06d5f342019-10-04 17:06:14 +0100761 .read_status = at803x_read_status,
Zefir Kurtisif62265b2016-10-24 12:40:54 +0200762 .aneg_done = at803x_aneg_done,
Daniel Mack13a56b42014-06-18 11:01:43 +0200763 .ack_interrupt = &at803x_ack_interrupt,
764 .config_intr = &at803x_config_intr,
Oleksij Rempel7908d2c2019-10-03 10:21:12 +0200765}, {
766 /* ATHEROS AR9331 */
767 PHY_ID_MATCH_EXACT(ATH9331_PHY_ID),
Michael Walle96c36712019-11-06 23:36:16 +0100768 .name = "Qualcomm Atheros AR9331 built-in PHY",
Oleksij Rempel7908d2c2019-10-03 10:21:12 +0200769 .suspend = at803x_suspend,
770 .resume = at803x_resume,
771 /* PHY_BASIC_FEATURES */
772 .ack_interrupt = &at803x_ack_interrupt,
773 .config_intr = &at803x_config_intr,
Mugunthan V N317420a2013-06-03 20:10:04 +0000774} };
Matus Ujhelyi0ca71112012-10-14 19:07:16 +0000775
Johan Hovold50fd7152014-11-11 19:45:59 +0100776module_phy_driver(at803x_driver);
Matus Ujhelyi0ca71112012-10-14 19:07:16 +0000777
778static struct mdio_device_id __maybe_unused atheros_tbl[] = {
Fabio Estevam58effd72016-10-26 14:03:54 -0200779 { ATH8030_PHY_ID, AT803X_PHY_ID_MASK },
780 { ATH8031_PHY_ID, AT803X_PHY_ID_MASK },
781 { ATH8035_PHY_ID, AT803X_PHY_ID_MASK },
Oleksij Rempel7908d2c2019-10-03 10:21:12 +0200782 { PHY_ID_MATCH_EXACT(ATH9331_PHY_ID) },
Matus Ujhelyi0ca71112012-10-14 19:07:16 +0000783 { }
784};
785
786MODULE_DEVICE_TABLE(mdio, atheros_tbl);