Merge branch 'x86-pti-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux-2.6-block.git] / drivers / net / phy / at803x.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * drivers/net/phy/at803x.c
4  *
5  * Driver for Qualcomm Atheros AR803x PHY
6  *
7  * Author: Matus Ujhelyi <ujhelyi.m@gmail.com>
8  */
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>
15 #include <linux/of_gpio.h>
16 #include <linux/bitfield.h>
17 #include <linux/gpio/consumer.h>
18 #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>
22
23 #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
32 #define AT803X_INTR_ENABLE                      0x12
33 #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
43 #define AT803X_INTR_STATUS                      0x13
44
45 #define AT803X_SMART_SPEED                      0x14
46 #define AT803X_LED_CONTROL                      0x18
47
48 #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
52 #define AT803X_REG_CHIP_CONFIG                  0x1f
53 #define AT803X_BT_BX_REG_SEL                    0x8000
54
55 #define AT803X_DEBUG_ADDR                       0x1D
56 #define AT803X_DEBUG_DATA                       0x1E
57
58 #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
64 #define AT803X_DEBUG_REG_0                      0x00
65 #define AT803X_DEBUG_RX_CLK_DLY_EN              BIT(15)
66
67 #define AT803X_DEBUG_REG_5                      0x05
68 #define AT803X_DEBUG_TX_CLK_DLY_EN              BIT(8)
69
70 #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
96 /* The AR8035 has another mask which is compatible with the AR8031/AR8033 mask
97  * but doesn't support choosing between XTAL/PLL and DSP.
98  */
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
106 #define ATH9331_PHY_ID 0x004dd041
107 #define ATH8030_PHY_ID 0x004dd076
108 #define ATH8031_PHY_ID 0x004dd074
109 #define ATH8035_PHY_ID 0x004dd072
110 #define AT803X_PHY_ID_MASK                      0xffffffef
111
112 MODULE_DESCRIPTION("Qualcomm Atheros AR803x PHY driver");
113 MODULE_AUTHOR("Matus Ujhelyi");
114 MODULE_LICENSE("GPL");
115
116 struct 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
126 struct 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
135 static 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
146 static 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
163 static 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
169 static 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
175 static int at803x_disable_rx_delay(struct phy_device *phydev)
176 {
177         return at803x_debug_reg_mask(phydev, AT803X_DEBUG_REG_0,
178                                      AT803X_DEBUG_RX_CLK_DLY_EN, 0);
179 }
180
181 static int at803x_disable_tx_delay(struct phy_device *phydev)
182 {
183         return at803x_debug_reg_mask(phydev, AT803X_DEBUG_REG_5,
184                                      AT803X_DEBUG_TX_CLK_DLY_EN, 0);
185 }
186
187 /* save relevant PHY registers to private copy */
188 static 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 */
200 static 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
211 static int at803x_set_wol(struct phy_device *phydev,
212                           struct ethtool_wolinfo *wol)
213 {
214         struct net_device *ndev = phydev->attached_dev;
215         const u8 *mac;
216         int ret;
217         u32 value;
218         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)
225                 return -ENODEV;
226
227         if (wol->wolopts & WAKE_MAGIC) {
228                 mac = (const u8 *) ndev->dev_addr;
229
230                 if (!is_valid_ether_addr(mac))
231                         return -EINVAL;
232
233                 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));
236
237                 value = phy_read(phydev, AT803X_INTR_ENABLE);
238                 value |= AT803X_INTR_ENABLE_WOL;
239                 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);
245                 value &= (~AT803X_INTR_ENABLE_WOL);
246                 ret = phy_write(phydev, AT803X_INTR_ENABLE, value);
247                 if (ret)
248                         return ret;
249                 value = phy_read(phydev, AT803X_INTR_STATUS);
250         }
251
252         return ret;
253 }
254
255 static 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);
264         if (value & AT803X_INTR_ENABLE_WOL)
265                 wol->wolopts |= WAKE_MAGIC;
266 }
267
268 static int at803x_suspend(struct phy_device *phydev)
269 {
270         int value;
271         int wol_enabled;
272
273         value = phy_read(phydev, AT803X_INTR_ENABLE);
274         wol_enabled = value & AT803X_INTR_ENABLE_WOL;
275
276         if (wol_enabled)
277                 value = BMCR_ISOLATE;
278         else
279                 value = BMCR_PDOWN;
280
281         phy_modify(phydev, MII_BMCR, 0, value);
282
283         return 0;
284 }
285
286 static int at803x_resume(struct phy_device *phydev)
287 {
288         return phy_modify(phydev, MII_BMCR, BMCR_PDOWN | BMCR_ISOLATE, 0);
289 }
290
291 static 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
304 static 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
316 static 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
322 static const unsigned int vddio_voltage_table[] = {
323         1500000,
324         1800000,
325 };
326
327 static 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
337 static struct regulator_ops vddh_regulator_ops = {
338 };
339
340 static 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
350 static 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
374 static 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
380 static 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
452         /* Only supported on AR8031/AR8033, the AR8030/AR8035 use strapping
453          * options.
454          */
455         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
478 static 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
492 static int at803x_clk_out_config(struct phy_device *phydev)
493 {
494         struct at803x_priv *priv = phydev->priv;
495         int val;
496
497         if (!priv->clk_25m_mask)
498                 return 0;
499
500         val = phy_read_mmd(phydev, MDIO_MMD_AN, AT803X_MMD7_CLK25M);
501         if (val < 0)
502                 return val;
503
504         val &= ~priv->clk_25m_mask;
505         val |= priv->clk_25m_reg;
506
507         return phy_write_mmd(phydev, MDIO_MMD_AN, AT803X_MMD7_CLK25M, val);
508 }
509
510 static int at8031_pll_config(struct phy_device *phydev)
511 {
512         struct at803x_priv *priv = phydev->priv;
513
514         /* The default after hardware reset is PLL OFF. After a soft reset, the
515          * values are retained.
516          */
517         if (priv->flags & AT803X_KEEP_PLL_ENABLED)
518                 return at803x_debug_reg_mask(phydev, AT803X_DEBUG_REG_1F,
519                                              0, AT803X_DEBUG_PLL_ON);
520         else
521                 return at803x_debug_reg_mask(phydev, AT803X_DEBUG_REG_1F,
522                                              AT803X_DEBUG_PLL_ON, 0);
523 }
524
525 static int at803x_config_init(struct phy_device *phydev)
526 {
527         int ret;
528
529         /* The RX and TX delay default is:
530          *   after HW reset: RX delay enabled and TX delay disabled
531          *   after SW reset: RX delay enabled, while TX delay retains the
532          *   value before reset.
533          */
534         if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID ||
535             phydev->interface == PHY_INTERFACE_MODE_RGMII_RXID)
536                 ret = at803x_enable_rx_delay(phydev);
537         else
538                 ret = at803x_disable_rx_delay(phydev);
539         if (ret < 0)
540                 return ret;
541
542         if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID ||
543             phydev->interface == PHY_INTERFACE_MODE_RGMII_TXID)
544                 ret = at803x_enable_tx_delay(phydev);
545         else
546                 ret = at803x_disable_tx_delay(phydev);
547         if (ret < 0)
548                 return ret;
549
550         ret = at803x_clk_out_config(phydev);
551         if (ret < 0)
552                 return ret;
553
554         if (at803x_match_phy_id(phydev, ATH8031_PHY_ID)) {
555                 ret = at8031_pll_config(phydev);
556                 if (ret < 0)
557                         return ret;
558         }
559
560         return 0;
561 }
562
563 static int at803x_ack_interrupt(struct phy_device *phydev)
564 {
565         int err;
566
567         err = phy_read(phydev, AT803X_INTR_STATUS);
568
569         return (err < 0) ? err : 0;
570 }
571
572 static int at803x_config_intr(struct phy_device *phydev)
573 {
574         int err;
575         int value;
576
577         value = phy_read(phydev, AT803X_INTR_ENABLE);
578
579         if (phydev->interrupts == PHY_INTERRUPT_ENABLED) {
580                 value |= AT803X_INTR_ENABLE_AUTONEG_ERR;
581                 value |= AT803X_INTR_ENABLE_SPEED_CHANGED;
582                 value |= AT803X_INTR_ENABLE_DUPLEX_CHANGED;
583                 value |= AT803X_INTR_ENABLE_LINK_FAIL;
584                 value |= AT803X_INTR_ENABLE_LINK_SUCCESS;
585
586                 err = phy_write(phydev, AT803X_INTR_ENABLE, value);
587         }
588         else
589                 err = phy_write(phydev, AT803X_INTR_ENABLE, 0);
590
591         return err;
592 }
593
594 static void at803x_link_change_notify(struct phy_device *phydev)
595 {
596         /*
597          * Conduct a hardware reset for AT8030 every time a link loss is
598          * signalled. This is necessary to circumvent a hardware bug that
599          * occurs when the cable is unplugged while TX packets are pending
600          * in the FIFO. In such cases, the FIFO enters an error mode it
601          * cannot recover from by software.
602          */
603         if (phydev->state == PHY_NOLINK && phydev->mdio.reset_gpio) {
604                 struct at803x_context context;
605
606                 at803x_context_save(phydev, &context);
607
608                 phy_device_reset(phydev, 1);
609                 msleep(1);
610                 phy_device_reset(phydev, 0);
611                 msleep(1);
612
613                 at803x_context_restore(phydev, &context);
614
615                 phydev_dbg(phydev, "%s(): phy was reset\n", __func__);
616         }
617 }
618
619 static int at803x_aneg_done(struct phy_device *phydev)
620 {
621         int ccr;
622
623         int aneg_done = genphy_aneg_done(phydev);
624         if (aneg_done != BMSR_ANEGCOMPLETE)
625                 return aneg_done;
626
627         /*
628          * in SGMII mode, if copper side autoneg is successful,
629          * also check SGMII side autoneg result
630          */
631         ccr = phy_read(phydev, AT803X_REG_CHIP_CONFIG);
632         if ((ccr & AT803X_MODE_CFG_MASK) != AT803X_MODE_CFG_SGMII)
633                 return aneg_done;
634
635         /* switch to SGMII/fiber page */
636         phy_write(phydev, AT803X_REG_CHIP_CONFIG, ccr & ~AT803X_BT_BX_REG_SEL);
637
638         /* check if the SGMII link is OK. */
639         if (!(phy_read(phydev, AT803X_PSSR) & AT803X_PSSR_MR_AN_COMPLETE)) {
640                 phydev_warn(phydev, "803x_aneg_done: SGMII link is not ok\n");
641                 aneg_done = 0;
642         }
643         /* switch back to copper page */
644         phy_write(phydev, AT803X_REG_CHIP_CONFIG, ccr | AT803X_BT_BX_REG_SEL);
645
646         return aneg_done;
647 }
648
649 static int at803x_read_status(struct phy_device *phydev)
650 {
651         int ss, err, old_link = phydev->link;
652
653         /* Update the link, but return if there was an error */
654         err = genphy_update_link(phydev);
655         if (err)
656                 return err;
657
658         /* why bother the PHY if nothing can have changed */
659         if (phydev->autoneg == AUTONEG_ENABLE && old_link && phydev->link)
660                 return 0;
661
662         phydev->speed = SPEED_UNKNOWN;
663         phydev->duplex = DUPLEX_UNKNOWN;
664         phydev->pause = 0;
665         phydev->asym_pause = 0;
666
667         err = genphy_read_lpa(phydev);
668         if (err < 0)
669                 return err;
670
671         /* Read the AT8035 PHY-Specific Status register, which indicates the
672          * speed and duplex that the PHY is actually using, irrespective of
673          * whether we are in autoneg mode or not.
674          */
675         ss = phy_read(phydev, AT803X_SPECIFIC_STATUS);
676         if (ss < 0)
677                 return ss;
678
679         if (ss & AT803X_SS_SPEED_DUPLEX_RESOLVED) {
680                 switch (ss & AT803X_SS_SPEED_MASK) {
681                 case AT803X_SS_SPEED_10:
682                         phydev->speed = SPEED_10;
683                         break;
684                 case AT803X_SS_SPEED_100:
685                         phydev->speed = SPEED_100;
686                         break;
687                 case AT803X_SS_SPEED_1000:
688                         phydev->speed = SPEED_1000;
689                         break;
690                 }
691                 if (ss & AT803X_SS_DUPLEX)
692                         phydev->duplex = DUPLEX_FULL;
693                 else
694                         phydev->duplex = DUPLEX_HALF;
695                 if (ss & AT803X_SS_MDIX)
696                         phydev->mdix = ETH_TP_MDI_X;
697                 else
698                         phydev->mdix = ETH_TP_MDI;
699         }
700
701         if (phydev->autoneg == AUTONEG_ENABLE && phydev->autoneg_complete)
702                 phy_resolve_aneg_pause(phydev);
703
704         return 0;
705 }
706
707 static struct phy_driver at803x_driver[] = {
708 {
709         /* Qualcomm Atheros AR8035 */
710         .phy_id                 = ATH8035_PHY_ID,
711         .name                   = "Qualcomm Atheros AR8035",
712         .phy_id_mask            = AT803X_PHY_ID_MASK,
713         .probe                  = at803x_probe,
714         .config_init            = at803x_config_init,
715         .set_wol                = at803x_set_wol,
716         .get_wol                = at803x_get_wol,
717         .suspend                = at803x_suspend,
718         .resume                 = at803x_resume,
719         /* PHY_GBIT_FEATURES */
720         .read_status            = at803x_read_status,
721         .ack_interrupt          = at803x_ack_interrupt,
722         .config_intr            = at803x_config_intr,
723 }, {
724         /* Qualcomm Atheros AR8030 */
725         .phy_id                 = ATH8030_PHY_ID,
726         .name                   = "Qualcomm Atheros AR8030",
727         .phy_id_mask            = AT803X_PHY_ID_MASK,
728         .probe                  = at803x_probe,
729         .config_init            = at803x_config_init,
730         .link_change_notify     = at803x_link_change_notify,
731         .set_wol                = at803x_set_wol,
732         .get_wol                = at803x_get_wol,
733         .suspend                = at803x_suspend,
734         .resume                 = at803x_resume,
735         /* PHY_BASIC_FEATURES */
736         .ack_interrupt          = at803x_ack_interrupt,
737         .config_intr            = at803x_config_intr,
738 }, {
739         /* Qualcomm Atheros AR8031/AR8033 */
740         .phy_id                 = ATH8031_PHY_ID,
741         .name                   = "Qualcomm Atheros AR8031/AR8033",
742         .phy_id_mask            = AT803X_PHY_ID_MASK,
743         .probe                  = at803x_probe,
744         .config_init            = at803x_config_init,
745         .set_wol                = at803x_set_wol,
746         .get_wol                = at803x_get_wol,
747         .suspend                = at803x_suspend,
748         .resume                 = at803x_resume,
749         /* PHY_GBIT_FEATURES */
750         .read_status            = at803x_read_status,
751         .aneg_done              = at803x_aneg_done,
752         .ack_interrupt          = &at803x_ack_interrupt,
753         .config_intr            = &at803x_config_intr,
754 }, {
755         /* ATHEROS AR9331 */
756         PHY_ID_MATCH_EXACT(ATH9331_PHY_ID),
757         .name                   = "Qualcomm Atheros AR9331 built-in PHY",
758         .suspend                = at803x_suspend,
759         .resume                 = at803x_resume,
760         /* PHY_BASIC_FEATURES */
761         .ack_interrupt          = &at803x_ack_interrupt,
762         .config_intr            = &at803x_config_intr,
763 } };
764
765 module_phy_driver(at803x_driver);
766
767 static struct mdio_device_id __maybe_unused atheros_tbl[] = {
768         { ATH8030_PHY_ID, AT803X_PHY_ID_MASK },
769         { ATH8031_PHY_ID, AT803X_PHY_ID_MASK },
770         { ATH8035_PHY_ID, AT803X_PHY_ID_MASK },
771         { PHY_ID_MATCH_EXACT(ATH9331_PHY_ID) },
772         { }
773 };
774
775 MODULE_DEVICE_TABLE(mdio, atheros_tbl);