GNU Linux-libre 4.19.264-gnu1
[releases.git] / drivers / net / phy / marvell.c
1 /*
2  * drivers/net/phy/marvell.c
3  *
4  * Driver for Marvell PHYs
5  *
6  * Author: Andy Fleming
7  *
8  * Copyright (c) 2004 Freescale Semiconductor, Inc.
9  *
10  * Copyright (c) 2013 Michael Stapelberg <michael@stapelberg.de>
11  *
12  * This program is free software; you can redistribute  it and/or modify it
13  * under  the terms of  the GNU General  Public License as published by the
14  * Free Software Foundation;  either version 2 of the  License, or (at your
15  * option) any later version.
16  *
17  */
18 #include <linux/kernel.h>
19 #include <linux/string.h>
20 #include <linux/ctype.h>
21 #include <linux/errno.h>
22 #include <linux/unistd.h>
23 #include <linux/hwmon.h>
24 #include <linux/interrupt.h>
25 #include <linux/init.h>
26 #include <linux/delay.h>
27 #include <linux/netdevice.h>
28 #include <linux/etherdevice.h>
29 #include <linux/skbuff.h>
30 #include <linux/spinlock.h>
31 #include <linux/mm.h>
32 #include <linux/module.h>
33 #include <linux/mii.h>
34 #include <linux/ethtool.h>
35 #include <linux/phy.h>
36 #include <linux/marvell_phy.h>
37 #include <linux/of.h>
38
39 #include <linux/io.h>
40 #include <asm/irq.h>
41 #include <linux/uaccess.h>
42
43 #define MII_MARVELL_PHY_PAGE            22
44 #define MII_MARVELL_COPPER_PAGE         0x00
45 #define MII_MARVELL_FIBER_PAGE          0x01
46 #define MII_MARVELL_MSCR_PAGE           0x02
47 #define MII_MARVELL_LED_PAGE            0x03
48 #define MII_MARVELL_MISC_TEST_PAGE      0x06
49 #define MII_MARVELL_WOL_PAGE            0x11
50
51 #define MII_M1011_IEVENT                0x13
52 #define MII_M1011_IEVENT_CLEAR          0x0000
53
54 #define MII_M1011_IMASK                 0x12
55 #define MII_M1011_IMASK_INIT            0x6400
56 #define MII_M1011_IMASK_CLEAR           0x0000
57
58 #define MII_M1011_PHY_SCR                       0x10
59 #define MII_M1011_PHY_SCR_DOWNSHIFT_EN          BIT(11)
60 #define MII_M1011_PHY_SCR_DOWNSHIFT_SHIFT       12
61 #define MII_M1011_PHY_SRC_DOWNSHIFT_MASK        0x7800
62 #define MII_M1011_PHY_SCR_MDI                   (0x0 << 5)
63 #define MII_M1011_PHY_SCR_MDI_X                 (0x1 << 5)
64 #define MII_M1011_PHY_SCR_AUTO_CROSS            (0x3 << 5)
65
66 #define MII_M1111_PHY_LED_CONTROL       0x18
67 #define MII_M1111_PHY_LED_DIRECT        0x4100
68 #define MII_M1111_PHY_LED_COMBINE       0x411c
69 #define MII_M1111_PHY_EXT_CR            0x14
70 #define MII_M1111_RGMII_RX_DELAY        BIT(7)
71 #define MII_M1111_RGMII_TX_DELAY        BIT(1)
72 #define MII_M1111_PHY_EXT_SR            0x1b
73
74 #define MII_M1111_HWCFG_MODE_MASK               0xf
75 #define MII_M1111_HWCFG_MODE_FIBER_RGMII        0x3
76 #define MII_M1111_HWCFG_MODE_SGMII_NO_CLK       0x4
77 #define MII_M1111_HWCFG_MODE_RTBI               0x7
78 #define MII_M1111_HWCFG_MODE_COPPER_RTBI        0x9
79 #define MII_M1111_HWCFG_MODE_COPPER_RGMII       0xb
80 #define MII_M1111_HWCFG_FIBER_COPPER_RES        BIT(13)
81 #define MII_M1111_HWCFG_FIBER_COPPER_AUTO       BIT(15)
82
83 #define MII_88E1121_PHY_MSCR_REG        21
84 #define MII_88E1121_PHY_MSCR_RX_DELAY   BIT(5)
85 #define MII_88E1121_PHY_MSCR_TX_DELAY   BIT(4)
86 #define MII_88E1121_PHY_MSCR_DELAY_MASK (BIT(5) | BIT(4))
87
88 #define MII_88E1121_MISC_TEST                           0x1a
89 #define MII_88E1510_MISC_TEST_TEMP_THRESHOLD_MASK       0x1f00
90 #define MII_88E1510_MISC_TEST_TEMP_THRESHOLD_SHIFT      8
91 #define MII_88E1510_MISC_TEST_TEMP_IRQ_EN               BIT(7)
92 #define MII_88E1510_MISC_TEST_TEMP_IRQ                  BIT(6)
93 #define MII_88E1121_MISC_TEST_TEMP_SENSOR_EN            BIT(5)
94 #define MII_88E1121_MISC_TEST_TEMP_MASK                 0x1f
95
96 #define MII_88E1510_TEMP_SENSOR         0x1b
97 #define MII_88E1510_TEMP_SENSOR_MASK    0xff
98
99 #define MII_88E6390_MISC_TEST           0x1b
100 #define MII_88E6390_MISC_TEST_SAMPLE_1S         0
101 #define MII_88E6390_MISC_TEST_SAMPLE_10MS       BIT(14)
102 #define MII_88E6390_MISC_TEST_SAMPLE_DISABLE    BIT(15)
103 #define MII_88E6390_MISC_TEST_SAMPLE_ENABLE     0
104 #define MII_88E6390_MISC_TEST_SAMPLE_MASK       (0x3 << 14)
105
106 #define MII_88E6390_TEMP_SENSOR         0x1c
107 #define MII_88E6390_TEMP_SENSOR_MASK    0xff
108 #define MII_88E6390_TEMP_SENSOR_SAMPLES 10
109
110 #define MII_88E1318S_PHY_MSCR1_REG      16
111 #define MII_88E1318S_PHY_MSCR1_PAD_ODD  BIT(6)
112
113 /* Copper Specific Interrupt Enable Register */
114 #define MII_88E1318S_PHY_CSIER                          0x12
115 /* WOL Event Interrupt Enable */
116 #define MII_88E1318S_PHY_CSIER_WOL_EIE                  BIT(7)
117
118 /* LED Timer Control Register */
119 #define MII_88E1318S_PHY_LED_TCR                        0x12
120 #define MII_88E1318S_PHY_LED_TCR_FORCE_INT              BIT(15)
121 #define MII_88E1318S_PHY_LED_TCR_INTn_ENABLE            BIT(7)
122 #define MII_88E1318S_PHY_LED_TCR_INT_ACTIVE_LOW         BIT(11)
123
124 /* Magic Packet MAC address registers */
125 #define MII_88E1318S_PHY_MAGIC_PACKET_WORD2             0x17
126 #define MII_88E1318S_PHY_MAGIC_PACKET_WORD1             0x18
127 #define MII_88E1318S_PHY_MAGIC_PACKET_WORD0             0x19
128
129 #define MII_88E1318S_PHY_WOL_CTRL                               0x10
130 #define MII_88E1318S_PHY_WOL_CTRL_CLEAR_WOL_STATUS              BIT(12)
131 #define MII_88E1318S_PHY_WOL_CTRL_MAGIC_PACKET_MATCH_ENABLE     BIT(14)
132
133 #define MII_PHY_LED_CTRL                16
134 #define MII_88E1121_PHY_LED_DEF         0x0030
135 #define MII_88E1510_PHY_LED_DEF         0x1177
136
137 #define MII_M1011_PHY_STATUS            0x11
138 #define MII_M1011_PHY_STATUS_1000       0x8000
139 #define MII_M1011_PHY_STATUS_100        0x4000
140 #define MII_M1011_PHY_STATUS_SPD_MASK   0xc000
141 #define MII_M1011_PHY_STATUS_FULLDUPLEX 0x2000
142 #define MII_M1011_PHY_STATUS_RESOLVED   0x0800
143 #define MII_M1011_PHY_STATUS_LINK       0x0400
144
145 #define MII_88E3016_PHY_SPEC_CTRL       0x10
146 #define MII_88E3016_DISABLE_SCRAMBLER   0x0200
147 #define MII_88E3016_AUTO_MDIX_CROSSOVER 0x0030
148
149 #define MII_88E1510_GEN_CTRL_REG_1              0x14
150 #define MII_88E1510_GEN_CTRL_REG_1_MODE_MASK    0x7
151 #define MII_88E1510_GEN_CTRL_REG_1_MODE_SGMII   0x1     /* SGMII to copper */
152 #define MII_88E1510_GEN_CTRL_REG_1_RESET        0x8000  /* Soft reset */
153
154 #define LPA_FIBER_1000HALF      0x40
155 #define LPA_FIBER_1000FULL      0x20
156
157 #define LPA_PAUSE_FIBER         0x180
158 #define LPA_PAUSE_ASYM_FIBER    0x100
159
160 #define ADVERTISE_FIBER_1000HALF        0x40
161 #define ADVERTISE_FIBER_1000FULL        0x20
162
163 #define ADVERTISE_PAUSE_FIBER           0x180
164 #define ADVERTISE_PAUSE_ASYM_FIBER      0x100
165
166 #define REGISTER_LINK_STATUS    0x400
167 #define NB_FIBER_STATS  1
168
169 MODULE_DESCRIPTION("Marvell PHY driver");
170 MODULE_AUTHOR("Andy Fleming");
171 MODULE_LICENSE("GPL");
172
173 struct marvell_hw_stat {
174         const char *string;
175         u8 page;
176         u8 reg;
177         u8 bits;
178 };
179
180 static struct marvell_hw_stat marvell_hw_stats[] = {
181         { "phy_receive_errors_copper", 0, 21, 16},
182         { "phy_idle_errors", 0, 10, 8 },
183         { "phy_receive_errors_fiber", 1, 21, 16},
184 };
185
186 struct marvell_priv {
187         u64 stats[ARRAY_SIZE(marvell_hw_stats)];
188         char *hwmon_name;
189         struct device *hwmon_dev;
190 };
191
192 static int marvell_read_page(struct phy_device *phydev)
193 {
194         return __phy_read(phydev, MII_MARVELL_PHY_PAGE);
195 }
196
197 static int marvell_write_page(struct phy_device *phydev, int page)
198 {
199         return __phy_write(phydev, MII_MARVELL_PHY_PAGE, page);
200 }
201
202 static int marvell_set_page(struct phy_device *phydev, int page)
203 {
204         return phy_write(phydev, MII_MARVELL_PHY_PAGE, page);
205 }
206
207 static int marvell_ack_interrupt(struct phy_device *phydev)
208 {
209         int err;
210
211         /* Clear the interrupts by reading the reg */
212         err = phy_read(phydev, MII_M1011_IEVENT);
213
214         if (err < 0)
215                 return err;
216
217         return 0;
218 }
219
220 static int marvell_config_intr(struct phy_device *phydev)
221 {
222         int err;
223
224         if (phydev->interrupts == PHY_INTERRUPT_ENABLED)
225                 err = phy_write(phydev, MII_M1011_IMASK,
226                                 MII_M1011_IMASK_INIT);
227         else
228                 err = phy_write(phydev, MII_M1011_IMASK,
229                                 MII_M1011_IMASK_CLEAR);
230
231         return err;
232 }
233
234 static int marvell_set_polarity(struct phy_device *phydev, int polarity)
235 {
236         int reg;
237         int err;
238         int val;
239
240         /* get the current settings */
241         reg = phy_read(phydev, MII_M1011_PHY_SCR);
242         if (reg < 0)
243                 return reg;
244
245         val = reg;
246         val &= ~MII_M1011_PHY_SCR_AUTO_CROSS;
247         switch (polarity) {
248         case ETH_TP_MDI:
249                 val |= MII_M1011_PHY_SCR_MDI;
250                 break;
251         case ETH_TP_MDI_X:
252                 val |= MII_M1011_PHY_SCR_MDI_X;
253                 break;
254         case ETH_TP_MDI_AUTO:
255         case ETH_TP_MDI_INVALID:
256         default:
257                 val |= MII_M1011_PHY_SCR_AUTO_CROSS;
258                 break;
259         }
260
261         if (val != reg) {
262                 /* Set the new polarity value in the register */
263                 err = phy_write(phydev, MII_M1011_PHY_SCR, val);
264                 if (err)
265                         return err;
266         }
267
268         return 0;
269 }
270
271 static int marvell_set_downshift(struct phy_device *phydev, bool enable,
272                                  u8 retries)
273 {
274         int reg;
275
276         reg = phy_read(phydev, MII_M1011_PHY_SCR);
277         if (reg < 0)
278                 return reg;
279
280         reg &= MII_M1011_PHY_SRC_DOWNSHIFT_MASK;
281         reg |= ((retries - 1) << MII_M1011_PHY_SCR_DOWNSHIFT_SHIFT);
282         if (enable)
283                 reg |= MII_M1011_PHY_SCR_DOWNSHIFT_EN;
284
285         return phy_write(phydev, MII_M1011_PHY_SCR, reg);
286 }
287
288 static int marvell_config_aneg(struct phy_device *phydev)
289 {
290         int err;
291
292         err = marvell_set_polarity(phydev, phydev->mdix_ctrl);
293         if (err < 0)
294                 return err;
295
296         err = phy_write(phydev, MII_M1111_PHY_LED_CONTROL,
297                         MII_M1111_PHY_LED_DIRECT);
298         if (err < 0)
299                 return err;
300
301         err = genphy_config_aneg(phydev);
302         if (err < 0)
303                 return err;
304
305         if (phydev->autoneg != AUTONEG_ENABLE) {
306                 /* A write to speed/duplex bits (that is performed by
307                  * genphy_config_aneg() call above) must be followed by
308                  * a software reset. Otherwise, the write has no effect.
309                  */
310                 err = genphy_soft_reset(phydev);
311                 if (err < 0)
312                         return err;
313         }
314
315         return 0;
316 }
317
318 static int m88e1101_config_aneg(struct phy_device *phydev)
319 {
320         int err;
321
322         /* This Marvell PHY has an errata which requires
323          * that certain registers get written in order
324          * to restart autonegotiation
325          */
326         err = genphy_soft_reset(phydev);
327         if (err < 0)
328                 return err;
329
330         err = phy_write(phydev, 0x1d, 0x1f);
331         if (err < 0)
332                 return err;
333
334         err = phy_write(phydev, 0x1e, 0x200c);
335         if (err < 0)
336                 return err;
337
338         err = phy_write(phydev, 0x1d, 0x5);
339         if (err < 0)
340                 return err;
341
342         err = phy_write(phydev, 0x1e, 0);
343         if (err < 0)
344                 return err;
345
346         err = phy_write(phydev, 0x1e, 0x100);
347         if (err < 0)
348                 return err;
349
350         return marvell_config_aneg(phydev);
351 }
352
353 static int m88e1111_config_aneg(struct phy_device *phydev)
354 {
355         int err;
356
357         /* The Marvell PHY has an errata which requires
358          * that certain registers get written in order
359          * to restart autonegotiation
360          */
361         err = genphy_soft_reset(phydev);
362
363         err = marvell_set_polarity(phydev, phydev->mdix_ctrl);
364         if (err < 0)
365                 return err;
366
367         err = phy_write(phydev, MII_M1111_PHY_LED_CONTROL,
368                         MII_M1111_PHY_LED_DIRECT);
369         if (err < 0)
370                 return err;
371
372         err = genphy_config_aneg(phydev);
373         if (err < 0)
374                 return err;
375
376         if (phydev->autoneg != AUTONEG_ENABLE) {
377                 /* A write to speed/duplex bits (that is performed by
378                  * genphy_config_aneg() call above) must be followed by
379                  * a software reset. Otherwise, the write has no effect.
380                  */
381                 err = genphy_soft_reset(phydev);
382                 if (err < 0)
383                         return err;
384         }
385
386         return 0;
387 }
388
389 #ifdef CONFIG_OF_MDIO
390 /* Set and/or override some configuration registers based on the
391  * marvell,reg-init property stored in the of_node for the phydev.
392  *
393  * marvell,reg-init = <reg-page reg mask value>,...;
394  *
395  * There may be one or more sets of <reg-page reg mask value>:
396  *
397  * reg-page: which register bank to use.
398  * reg: the register.
399  * mask: if non-zero, ANDed with existing register value.
400  * value: ORed with the masked value and written to the regiser.
401  *
402  */
403 static int marvell_of_reg_init(struct phy_device *phydev)
404 {
405         const __be32 *paddr;
406         int len, i, saved_page, current_page, ret = 0;
407
408         if (!phydev->mdio.dev.of_node)
409                 return 0;
410
411         paddr = of_get_property(phydev->mdio.dev.of_node,
412                                 "marvell,reg-init", &len);
413         if (!paddr || len < (4 * sizeof(*paddr)))
414                 return 0;
415
416         saved_page = phy_save_page(phydev);
417         if (saved_page < 0)
418                 goto err;
419         current_page = saved_page;
420
421         len /= sizeof(*paddr);
422         for (i = 0; i < len - 3; i += 4) {
423                 u16 page = be32_to_cpup(paddr + i);
424                 u16 reg = be32_to_cpup(paddr + i + 1);
425                 u16 mask = be32_to_cpup(paddr + i + 2);
426                 u16 val_bits = be32_to_cpup(paddr + i + 3);
427                 int val;
428
429                 if (page != current_page) {
430                         current_page = page;
431                         ret = marvell_write_page(phydev, page);
432                         if (ret < 0)
433                                 goto err;
434                 }
435
436                 val = 0;
437                 if (mask) {
438                         val = __phy_read(phydev, reg);
439                         if (val < 0) {
440                                 ret = val;
441                                 goto err;
442                         }
443                         val &= mask;
444                 }
445                 val |= val_bits;
446
447                 ret = __phy_write(phydev, reg, val);
448                 if (ret < 0)
449                         goto err;
450         }
451 err:
452         return phy_restore_page(phydev, saved_page, ret);
453 }
454 #else
455 static int marvell_of_reg_init(struct phy_device *phydev)
456 {
457         return 0;
458 }
459 #endif /* CONFIG_OF_MDIO */
460
461 static int m88e1121_config_aneg_rgmii_delays(struct phy_device *phydev)
462 {
463         int mscr;
464
465         if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID)
466                 mscr = MII_88E1121_PHY_MSCR_RX_DELAY |
467                        MII_88E1121_PHY_MSCR_TX_DELAY;
468         else if (phydev->interface == PHY_INTERFACE_MODE_RGMII_RXID)
469                 mscr = MII_88E1121_PHY_MSCR_RX_DELAY;
470         else if (phydev->interface == PHY_INTERFACE_MODE_RGMII_TXID)
471                 mscr = MII_88E1121_PHY_MSCR_TX_DELAY;
472         else
473                 mscr = 0;
474
475         return phy_modify_paged(phydev, MII_MARVELL_MSCR_PAGE,
476                                 MII_88E1121_PHY_MSCR_REG,
477                                 MII_88E1121_PHY_MSCR_DELAY_MASK, mscr);
478 }
479
480 static int m88e1121_config_aneg(struct phy_device *phydev)
481 {
482         int err = 0;
483
484         if (phy_interface_is_rgmii(phydev)) {
485                 err = m88e1121_config_aneg_rgmii_delays(phydev);
486                 if (err < 0)
487                         return err;
488         }
489
490         err = genphy_soft_reset(phydev);
491         if (err < 0)
492                 return err;
493
494         err = marvell_set_polarity(phydev, phydev->mdix_ctrl);
495         if (err < 0)
496                 return err;
497
498         return genphy_config_aneg(phydev);
499 }
500
501 static int m88e1318_config_aneg(struct phy_device *phydev)
502 {
503         int err;
504
505         err = phy_modify_paged(phydev, MII_MARVELL_MSCR_PAGE,
506                                MII_88E1318S_PHY_MSCR1_REG,
507                                0, MII_88E1318S_PHY_MSCR1_PAD_ODD);
508         if (err < 0)
509                 return err;
510
511         return m88e1121_config_aneg(phydev);
512 }
513
514 /**
515  * ethtool_adv_to_fiber_adv_t
516  * @ethadv: the ethtool advertisement settings
517  *
518  * A small helper function that translates ethtool advertisement
519  * settings to phy autonegotiation advertisements for the
520  * MII_ADV register for fiber link.
521  */
522 static inline u32 ethtool_adv_to_fiber_adv_t(u32 ethadv)
523 {
524         u32 result = 0;
525
526         if (ethadv & ADVERTISED_1000baseT_Half)
527                 result |= ADVERTISE_FIBER_1000HALF;
528         if (ethadv & ADVERTISED_1000baseT_Full)
529                 result |= ADVERTISE_FIBER_1000FULL;
530
531         if ((ethadv & ADVERTISE_PAUSE_ASYM) && (ethadv & ADVERTISE_PAUSE_CAP))
532                 result |= LPA_PAUSE_ASYM_FIBER;
533         else if (ethadv & ADVERTISE_PAUSE_CAP)
534                 result |= (ADVERTISE_PAUSE_FIBER
535                            & (~ADVERTISE_PAUSE_ASYM_FIBER));
536
537         return result;
538 }
539
540 /**
541  * marvell_config_aneg_fiber - restart auto-negotiation or write BMCR
542  * @phydev: target phy_device struct
543  *
544  * Description: If auto-negotiation is enabled, we configure the
545  *   advertising, and then restart auto-negotiation.  If it is not
546  *   enabled, then we write the BMCR. Adapted for fiber link in
547  *   some Marvell's devices.
548  */
549 static int marvell_config_aneg_fiber(struct phy_device *phydev)
550 {
551         int changed = 0;
552         int err;
553         int adv, oldadv;
554         u32 advertise;
555
556         if (phydev->autoneg != AUTONEG_ENABLE)
557                 return genphy_setup_forced(phydev);
558
559         /* Only allow advertising what this PHY supports */
560         phydev->advertising &= phydev->supported;
561         advertise = phydev->advertising;
562
563         /* Setup fiber advertisement */
564         adv = phy_read(phydev, MII_ADVERTISE);
565         if (adv < 0)
566                 return adv;
567
568         oldadv = adv;
569         adv &= ~(ADVERTISE_FIBER_1000HALF | ADVERTISE_FIBER_1000FULL
570                 | LPA_PAUSE_FIBER);
571         adv |= ethtool_adv_to_fiber_adv_t(advertise);
572
573         if (adv != oldadv) {
574                 err = phy_write(phydev, MII_ADVERTISE, adv);
575                 if (err < 0)
576                         return err;
577
578                 changed = 1;
579         }
580
581         if (changed == 0) {
582                 /* Advertisement hasn't changed, but maybe aneg was never on to
583                  * begin with?  Or maybe phy was isolated?
584                  */
585                 int ctl = phy_read(phydev, MII_BMCR);
586
587                 if (ctl < 0)
588                         return ctl;
589
590                 if (!(ctl & BMCR_ANENABLE) || (ctl & BMCR_ISOLATE))
591                         changed = 1; /* do restart aneg */
592         }
593
594         /* Only restart aneg if we are advertising something different
595          * than we were before.
596          */
597         if (changed > 0)
598                 changed = genphy_restart_aneg(phydev);
599
600         return changed;
601 }
602
603 static int m88e1510_config_aneg(struct phy_device *phydev)
604 {
605         int err;
606
607         err = marvell_set_page(phydev, MII_MARVELL_COPPER_PAGE);
608         if (err < 0)
609                 goto error;
610
611         /* Configure the copper link first */
612         err = m88e1318_config_aneg(phydev);
613         if (err < 0)
614                 goto error;
615
616         /* Do not touch the fiber page if we're in copper->sgmii mode */
617         if (phydev->interface == PHY_INTERFACE_MODE_SGMII)
618                 return 0;
619
620         /* Then the fiber link */
621         err = marvell_set_page(phydev, MII_MARVELL_FIBER_PAGE);
622         if (err < 0)
623                 goto error;
624
625         err = marvell_config_aneg_fiber(phydev);
626         if (err < 0)
627                 goto error;
628
629         return marvell_set_page(phydev, MII_MARVELL_COPPER_PAGE);
630
631 error:
632         marvell_set_page(phydev, MII_MARVELL_COPPER_PAGE);
633         return err;
634 }
635
636 static void marvell_config_led(struct phy_device *phydev)
637 {
638         u16 def_config;
639         int err;
640
641         switch (MARVELL_PHY_FAMILY_ID(phydev->phy_id)) {
642         /* Default PHY LED config: LED[0] .. Link, LED[1] .. Activity */
643         case MARVELL_PHY_FAMILY_ID(MARVELL_PHY_ID_88E1121R):
644         case MARVELL_PHY_FAMILY_ID(MARVELL_PHY_ID_88E1318S):
645                 def_config = MII_88E1121_PHY_LED_DEF;
646                 break;
647         /* Default PHY LED config:
648          * LED[0] .. 1000Mbps Link
649          * LED[1] .. 100Mbps Link
650          * LED[2] .. Blink, Activity
651          */
652         case MARVELL_PHY_FAMILY_ID(MARVELL_PHY_ID_88E1510):
653                 def_config = MII_88E1510_PHY_LED_DEF;
654                 break;
655         default:
656                 return;
657         }
658
659         err = phy_write_paged(phydev, MII_MARVELL_LED_PAGE, MII_PHY_LED_CTRL,
660                               def_config);
661         if (err < 0)
662                 pr_warn("Fail to config marvell phy LED.\n");
663 }
664
665 static int marvell_config_init(struct phy_device *phydev)
666 {
667         /* Set defalut LED */
668         marvell_config_led(phydev);
669
670         /* Set registers from marvell,reg-init DT property */
671         return marvell_of_reg_init(phydev);
672 }
673
674 static int m88e1116r_config_init(struct phy_device *phydev)
675 {
676         int err;
677
678         err = genphy_soft_reset(phydev);
679         if (err < 0)
680                 return err;
681
682         msleep(500);
683
684         err = marvell_set_page(phydev, MII_MARVELL_COPPER_PAGE);
685         if (err < 0)
686                 return err;
687
688         err = marvell_set_polarity(phydev, phydev->mdix_ctrl);
689         if (err < 0)
690                 return err;
691
692         err = marvell_set_downshift(phydev, true, 8);
693         if (err < 0)
694                 return err;
695
696         if (phy_interface_is_rgmii(phydev)) {
697                 err = m88e1121_config_aneg_rgmii_delays(phydev);
698                 if (err < 0)
699                         return err;
700         }
701
702         err = genphy_soft_reset(phydev);
703         if (err < 0)
704                 return err;
705
706         return marvell_config_init(phydev);
707 }
708
709 static int m88e3016_config_init(struct phy_device *phydev)
710 {
711         int ret;
712
713         /* Enable Scrambler and Auto-Crossover */
714         ret = phy_modify(phydev, MII_88E3016_PHY_SPEC_CTRL,
715                          MII_88E3016_DISABLE_SCRAMBLER,
716                          MII_88E3016_AUTO_MDIX_CROSSOVER);
717         if (ret < 0)
718                 return ret;
719
720         return marvell_config_init(phydev);
721 }
722
723 static int m88e1111_config_init_hwcfg_mode(struct phy_device *phydev,
724                                            u16 mode,
725                                            int fibre_copper_auto)
726 {
727         if (fibre_copper_auto)
728                 mode |= MII_M1111_HWCFG_FIBER_COPPER_AUTO;
729
730         return phy_modify(phydev, MII_M1111_PHY_EXT_SR,
731                           MII_M1111_HWCFG_MODE_MASK |
732                           MII_M1111_HWCFG_FIBER_COPPER_AUTO |
733                           MII_M1111_HWCFG_FIBER_COPPER_RES,
734                           mode);
735 }
736
737 static int m88e1111_config_init_rgmii_delays(struct phy_device *phydev)
738 {
739         int delay;
740
741         if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID) {
742                 delay = MII_M1111_RGMII_RX_DELAY | MII_M1111_RGMII_TX_DELAY;
743         } else if (phydev->interface == PHY_INTERFACE_MODE_RGMII_RXID) {
744                 delay = MII_M1111_RGMII_RX_DELAY;
745         } else if (phydev->interface == PHY_INTERFACE_MODE_RGMII_TXID) {
746                 delay = MII_M1111_RGMII_TX_DELAY;
747         } else {
748                 delay = 0;
749         }
750
751         return phy_modify(phydev, MII_M1111_PHY_EXT_CR,
752                           MII_M1111_RGMII_RX_DELAY | MII_M1111_RGMII_TX_DELAY,
753                           delay);
754 }
755
756 static int m88e1111_config_init_rgmii(struct phy_device *phydev)
757 {
758         int temp;
759         int err;
760
761         err = m88e1111_config_init_rgmii_delays(phydev);
762         if (err < 0)
763                 return err;
764
765         temp = phy_read(phydev, MII_M1111_PHY_EXT_SR);
766         if (temp < 0)
767                 return temp;
768
769         temp &= ~(MII_M1111_HWCFG_MODE_MASK);
770
771         if (temp & MII_M1111_HWCFG_FIBER_COPPER_RES)
772                 temp |= MII_M1111_HWCFG_MODE_FIBER_RGMII;
773         else
774                 temp |= MII_M1111_HWCFG_MODE_COPPER_RGMII;
775
776         return phy_write(phydev, MII_M1111_PHY_EXT_SR, temp);
777 }
778
779 static int m88e1111_config_init_sgmii(struct phy_device *phydev)
780 {
781         int err;
782
783         err = m88e1111_config_init_hwcfg_mode(
784                 phydev,
785                 MII_M1111_HWCFG_MODE_SGMII_NO_CLK,
786                 MII_M1111_HWCFG_FIBER_COPPER_AUTO);
787         if (err < 0)
788                 return err;
789
790         /* make sure copper is selected */
791         return marvell_set_page(phydev, MII_MARVELL_COPPER_PAGE);
792 }
793
794 static int m88e1111_config_init_rtbi(struct phy_device *phydev)
795 {
796         int err;
797
798         err = m88e1111_config_init_rgmii_delays(phydev);
799         if (err < 0)
800                 return err;
801
802         err = m88e1111_config_init_hwcfg_mode(
803                 phydev,
804                 MII_M1111_HWCFG_MODE_RTBI,
805                 MII_M1111_HWCFG_FIBER_COPPER_AUTO);
806         if (err < 0)
807                 return err;
808
809         /* soft reset */
810         err = genphy_soft_reset(phydev);
811         if (err < 0)
812                 return err;
813
814         return m88e1111_config_init_hwcfg_mode(
815                 phydev,
816                 MII_M1111_HWCFG_MODE_RTBI,
817                 MII_M1111_HWCFG_FIBER_COPPER_AUTO);
818 }
819
820 static int m88e1111_config_init(struct phy_device *phydev)
821 {
822         int err;
823
824         if (phy_interface_is_rgmii(phydev)) {
825                 err = m88e1111_config_init_rgmii(phydev);
826                 if (err < 0)
827                         return err;
828         }
829
830         if (phydev->interface == PHY_INTERFACE_MODE_SGMII) {
831                 err = m88e1111_config_init_sgmii(phydev);
832                 if (err < 0)
833                         return err;
834         }
835
836         if (phydev->interface == PHY_INTERFACE_MODE_RTBI) {
837                 err = m88e1111_config_init_rtbi(phydev);
838                 if (err < 0)
839                         return err;
840         }
841
842         err = marvell_of_reg_init(phydev);
843         if (err < 0)
844                 return err;
845
846         return genphy_soft_reset(phydev);
847 }
848
849 static int m88e1318_config_init(struct phy_device *phydev)
850 {
851         if (phy_interrupt_is_valid(phydev)) {
852                 int err = phy_modify_paged(
853                         phydev, MII_MARVELL_LED_PAGE,
854                         MII_88E1318S_PHY_LED_TCR,
855                         MII_88E1318S_PHY_LED_TCR_FORCE_INT,
856                         MII_88E1318S_PHY_LED_TCR_INTn_ENABLE |
857                         MII_88E1318S_PHY_LED_TCR_INT_ACTIVE_LOW);
858                 if (err < 0)
859                         return err;
860         }
861
862         return marvell_config_init(phydev);
863 }
864
865 static int m88e1510_config_init(struct phy_device *phydev)
866 {
867         int err;
868
869         /* SGMII-to-Copper mode initialization */
870         if (phydev->interface == PHY_INTERFACE_MODE_SGMII) {
871                 /* Select page 18 */
872                 err = marvell_set_page(phydev, 18);
873                 if (err < 0)
874                         return err;
875
876                 /* In reg 20, write MODE[2:0] = 0x1 (SGMII to Copper) */
877                 err = phy_modify(phydev, MII_88E1510_GEN_CTRL_REG_1,
878                                  MII_88E1510_GEN_CTRL_REG_1_MODE_MASK,
879                                  MII_88E1510_GEN_CTRL_REG_1_MODE_SGMII);
880                 if (err < 0)
881                         return err;
882
883                 /* PHY reset is necessary after changing MODE[2:0] */
884                 err = phy_modify(phydev, MII_88E1510_GEN_CTRL_REG_1, 0,
885                                  MII_88E1510_GEN_CTRL_REG_1_RESET);
886                 if (err < 0)
887                         return err;
888
889                 /* Reset page selection */
890                 err = marvell_set_page(phydev, MII_MARVELL_COPPER_PAGE);
891                 if (err < 0)
892                         return err;
893         }
894
895         return m88e1318_config_init(phydev);
896 }
897
898 static int m88e1118_config_aneg(struct phy_device *phydev)
899 {
900         int err;
901
902         err = marvell_set_polarity(phydev, phydev->mdix_ctrl);
903         if (err < 0)
904                 return err;
905
906         err = genphy_config_aneg(phydev);
907         if (err < 0)
908                 return err;
909
910         return genphy_soft_reset(phydev);
911 }
912
913 static int m88e1118_config_init(struct phy_device *phydev)
914 {
915         int err;
916
917         /* Change address */
918         err = marvell_set_page(phydev, MII_MARVELL_MSCR_PAGE);
919         if (err < 0)
920                 return err;
921
922         /* Enable 1000 Mbit */
923         err = phy_write(phydev, 0x15, 0x1070);
924         if (err < 0)
925                 return err;
926
927         /* Change address */
928         err = marvell_set_page(phydev, MII_MARVELL_LED_PAGE);
929         if (err < 0)
930                 return err;
931
932         if (phy_interface_is_rgmii(phydev)) {
933                 err = m88e1121_config_aneg_rgmii_delays(phydev);
934                 if (err < 0)
935                         return err;
936         }
937
938         /* Adjust LED Control */
939         if (phydev->dev_flags & MARVELL_PHY_M1118_DNS323_LEDS)
940                 err = phy_write(phydev, 0x10, 0x1100);
941         else
942                 err = phy_write(phydev, 0x10, 0x021e);
943         if (err < 0)
944                 return err;
945
946         err = marvell_of_reg_init(phydev);
947         if (err < 0)
948                 return err;
949
950         /* Reset address */
951         err = marvell_set_page(phydev, MII_MARVELL_COPPER_PAGE);
952         if (err < 0)
953                 return err;
954
955         return genphy_soft_reset(phydev);
956 }
957
958 static int m88e1149_config_init(struct phy_device *phydev)
959 {
960         int err;
961
962         /* Change address */
963         err = marvell_set_page(phydev, MII_MARVELL_MSCR_PAGE);
964         if (err < 0)
965                 return err;
966
967         /* Enable 1000 Mbit */
968         err = phy_write(phydev, 0x15, 0x1048);
969         if (err < 0)
970                 return err;
971
972         err = marvell_of_reg_init(phydev);
973         if (err < 0)
974                 return err;
975
976         /* Reset address */
977         err = marvell_set_page(phydev, MII_MARVELL_COPPER_PAGE);
978         if (err < 0)
979                 return err;
980
981         return genphy_soft_reset(phydev);
982 }
983
984 static int m88e1145_config_init_rgmii(struct phy_device *phydev)
985 {
986         int err;
987
988         err = m88e1111_config_init_rgmii_delays(phydev);
989         if (err < 0)
990                 return err;
991
992         if (phydev->dev_flags & MARVELL_PHY_M1145_FLAGS_RESISTANCE) {
993                 err = phy_write(phydev, 0x1d, 0x0012);
994                 if (err < 0)
995                         return err;
996
997                 err = phy_modify(phydev, 0x1e, 0x0fc0,
998                                  2 << 9 | /* 36 ohm */
999                                  2 << 6); /* 39 ohm */
1000                 if (err < 0)
1001                         return err;
1002
1003                 err = phy_write(phydev, 0x1d, 0x3);
1004                 if (err < 0)
1005                         return err;
1006
1007                 err = phy_write(phydev, 0x1e, 0x8000);
1008         }
1009         return err;
1010 }
1011
1012 static int m88e1145_config_init_sgmii(struct phy_device *phydev)
1013 {
1014         return m88e1111_config_init_hwcfg_mode(
1015                 phydev, MII_M1111_HWCFG_MODE_SGMII_NO_CLK,
1016                 MII_M1111_HWCFG_FIBER_COPPER_AUTO);
1017 }
1018
1019 static int m88e1145_config_init(struct phy_device *phydev)
1020 {
1021         int err;
1022
1023         /* Take care of errata E0 & E1 */
1024         err = phy_write(phydev, 0x1d, 0x001b);
1025         if (err < 0)
1026                 return err;
1027
1028         err = phy_write(phydev, 0x1e, 0x418f);
1029         if (err < 0)
1030                 return err;
1031
1032         err = phy_write(phydev, 0x1d, 0x0016);
1033         if (err < 0)
1034                 return err;
1035
1036         err = phy_write(phydev, 0x1e, 0xa2da);
1037         if (err < 0)
1038                 return err;
1039
1040         if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID) {
1041                 err = m88e1145_config_init_rgmii(phydev);
1042                 if (err < 0)
1043                         return err;
1044         }
1045
1046         if (phydev->interface == PHY_INTERFACE_MODE_SGMII) {
1047                 err = m88e1145_config_init_sgmii(phydev);
1048                 if (err < 0)
1049                         return err;
1050         }
1051
1052         err = marvell_of_reg_init(phydev);
1053         if (err < 0)
1054                 return err;
1055
1056         return 0;
1057 }
1058
1059 /* The VOD can be out of specification on link up. Poke an
1060  * undocumented register, in an undocumented page, with a magic value
1061  * to fix this.
1062  */
1063 static int m88e6390_errata(struct phy_device *phydev)
1064 {
1065         int err;
1066
1067         err = phy_write(phydev, MII_BMCR,
1068                         BMCR_ANENABLE | BMCR_SPEED1000 | BMCR_FULLDPLX);
1069         if (err)
1070                 return err;
1071
1072         usleep_range(300, 400);
1073
1074         err = phy_write_paged(phydev, 0xf8, 0x08, 0x36);
1075         if (err)
1076                 return err;
1077
1078         return genphy_soft_reset(phydev);
1079 }
1080
1081 static int m88e6390_config_aneg(struct phy_device *phydev)
1082 {
1083         int err;
1084
1085         err = m88e6390_errata(phydev);
1086         if (err)
1087                 return err;
1088
1089         return m88e1510_config_aneg(phydev);
1090 }
1091
1092 /**
1093  * fiber_lpa_to_ethtool_lpa_t
1094  * @lpa: value of the MII_LPA register for fiber link
1095  *
1096  * A small helper function that translates MII_LPA
1097  * bits to ethtool LP advertisement settings.
1098  */
1099 static u32 fiber_lpa_to_ethtool_lpa_t(u32 lpa)
1100 {
1101         u32 result = 0;
1102
1103         if (lpa & LPA_FIBER_1000HALF)
1104                 result |= ADVERTISED_1000baseT_Half;
1105         if (lpa & LPA_FIBER_1000FULL)
1106                 result |= ADVERTISED_1000baseT_Full;
1107
1108         return result;
1109 }
1110
1111 /**
1112  * marvell_update_link - update link status in real time in @phydev
1113  * @phydev: target phy_device struct
1114  *
1115  * Description: Update the value in phydev->link to reflect the
1116  *   current link value.
1117  */
1118 static int marvell_update_link(struct phy_device *phydev, int fiber)
1119 {
1120         int status;
1121
1122         /* Use the generic register for copper link, or specific
1123          * register for fiber case
1124          */
1125         if (fiber) {
1126                 status = phy_read(phydev, MII_M1011_PHY_STATUS);
1127                 if (status < 0)
1128                         return status;
1129
1130                 if ((status & REGISTER_LINK_STATUS) == 0)
1131                         phydev->link = 0;
1132                 else
1133                         phydev->link = 1;
1134         } else {
1135                 return genphy_update_link(phydev);
1136         }
1137
1138         return 0;
1139 }
1140
1141 static int marvell_read_status_page_an(struct phy_device *phydev,
1142                                        int fiber)
1143 {
1144         int status;
1145         int lpa;
1146         int lpagb;
1147
1148         status = phy_read(phydev, MII_M1011_PHY_STATUS);
1149         if (status < 0)
1150                 return status;
1151
1152         lpa = phy_read(phydev, MII_LPA);
1153         if (lpa < 0)
1154                 return lpa;
1155
1156         lpagb = phy_read(phydev, MII_STAT1000);
1157         if (lpagb < 0)
1158                 return lpagb;
1159
1160         if (status & MII_M1011_PHY_STATUS_FULLDUPLEX)
1161                 phydev->duplex = DUPLEX_FULL;
1162         else
1163                 phydev->duplex = DUPLEX_HALF;
1164
1165         status = status & MII_M1011_PHY_STATUS_SPD_MASK;
1166         phydev->pause = 0;
1167         phydev->asym_pause = 0;
1168
1169         switch (status) {
1170         case MII_M1011_PHY_STATUS_1000:
1171                 phydev->speed = SPEED_1000;
1172                 break;
1173
1174         case MII_M1011_PHY_STATUS_100:
1175                 phydev->speed = SPEED_100;
1176                 break;
1177
1178         default:
1179                 phydev->speed = SPEED_10;
1180                 break;
1181         }
1182
1183         if (!fiber) {
1184                 phydev->lp_advertising =
1185                         mii_stat1000_to_ethtool_lpa_t(lpagb) |
1186                         mii_lpa_to_ethtool_lpa_t(lpa);
1187
1188                 if (phydev->duplex == DUPLEX_FULL) {
1189                         phydev->pause = lpa & LPA_PAUSE_CAP ? 1 : 0;
1190                         phydev->asym_pause = lpa & LPA_PAUSE_ASYM ? 1 : 0;
1191                 }
1192         } else {
1193                 /* The fiber link is only 1000M capable */
1194                 phydev->lp_advertising = fiber_lpa_to_ethtool_lpa_t(lpa);
1195
1196                 if (phydev->duplex == DUPLEX_FULL) {
1197                         if (!(lpa & LPA_PAUSE_FIBER)) {
1198                                 phydev->pause = 0;
1199                                 phydev->asym_pause = 0;
1200                         } else if ((lpa & LPA_PAUSE_ASYM_FIBER)) {
1201                                 phydev->pause = 1;
1202                                 phydev->asym_pause = 1;
1203                         } else {
1204                                 phydev->pause = 1;
1205                                 phydev->asym_pause = 0;
1206                         }
1207                 }
1208         }
1209         return 0;
1210 }
1211
1212 static int marvell_read_status_page_fixed(struct phy_device *phydev)
1213 {
1214         int bmcr = phy_read(phydev, MII_BMCR);
1215
1216         if (bmcr < 0)
1217                 return bmcr;
1218
1219         if (bmcr & BMCR_FULLDPLX)
1220                 phydev->duplex = DUPLEX_FULL;
1221         else
1222                 phydev->duplex = DUPLEX_HALF;
1223
1224         if (bmcr & BMCR_SPEED1000)
1225                 phydev->speed = SPEED_1000;
1226         else if (bmcr & BMCR_SPEED100)
1227                 phydev->speed = SPEED_100;
1228         else
1229                 phydev->speed = SPEED_10;
1230
1231         phydev->pause = 0;
1232         phydev->asym_pause = 0;
1233         phydev->lp_advertising = 0;
1234
1235         return 0;
1236 }
1237
1238 /* marvell_read_status_page
1239  *
1240  * Description:
1241  *   Check the link, then figure out the current state
1242  *   by comparing what we advertise with what the link partner
1243  *   advertises.  Start by checking the gigabit possibilities,
1244  *   then move on to 10/100.
1245  */
1246 static int marvell_read_status_page(struct phy_device *phydev, int page)
1247 {
1248         int fiber;
1249         int err;
1250
1251         /* Detect and update the link, but return if there
1252          * was an error
1253          */
1254         if (page == MII_MARVELL_FIBER_PAGE)
1255                 fiber = 1;
1256         else
1257                 fiber = 0;
1258
1259         err = marvell_update_link(phydev, fiber);
1260         if (err)
1261                 return err;
1262
1263         if (phydev->autoneg == AUTONEG_ENABLE)
1264                 err = marvell_read_status_page_an(phydev, fiber);
1265         else
1266                 err = marvell_read_status_page_fixed(phydev);
1267
1268         return err;
1269 }
1270
1271 /* marvell_read_status
1272  *
1273  * Some Marvell's phys have two modes: fiber and copper.
1274  * Both need status checked.
1275  * Description:
1276  *   First, check the fiber link and status.
1277  *   If the fiber link is down, check the copper link and status which
1278  *   will be the default value if both link are down.
1279  */
1280 static int marvell_read_status(struct phy_device *phydev)
1281 {
1282         int err;
1283
1284         /* Check the fiber mode first */
1285         if (phydev->supported & SUPPORTED_FIBRE &&
1286             phydev->interface != PHY_INTERFACE_MODE_SGMII) {
1287                 err = marvell_set_page(phydev, MII_MARVELL_FIBER_PAGE);
1288                 if (err < 0)
1289                         goto error;
1290
1291                 err = marvell_read_status_page(phydev, MII_MARVELL_FIBER_PAGE);
1292                 if (err < 0)
1293                         goto error;
1294
1295                 /* If the fiber link is up, it is the selected and
1296                  * used link. In this case, we need to stay in the
1297                  * fiber page. Please to be careful about that, avoid
1298                  * to restore Copper page in other functions which
1299                  * could break the behaviour for some fiber phy like
1300                  * 88E1512.
1301                  */
1302                 if (phydev->link)
1303                         return 0;
1304
1305                 /* If fiber link is down, check and save copper mode state */
1306                 err = marvell_set_page(phydev, MII_MARVELL_COPPER_PAGE);
1307                 if (err < 0)
1308                         goto error;
1309         }
1310
1311         return marvell_read_status_page(phydev, MII_MARVELL_COPPER_PAGE);
1312
1313 error:
1314         marvell_set_page(phydev, MII_MARVELL_COPPER_PAGE);
1315         return err;
1316 }
1317
1318 /* marvell_suspend
1319  *
1320  * Some Marvell's phys have two modes: fiber and copper.
1321  * Both need to be suspended
1322  */
1323 static int marvell_suspend(struct phy_device *phydev)
1324 {
1325         int err;
1326
1327         /* Suspend the fiber mode first */
1328         if (!(phydev->supported & SUPPORTED_FIBRE)) {
1329                 err = marvell_set_page(phydev, MII_MARVELL_FIBER_PAGE);
1330                 if (err < 0)
1331                         goto error;
1332
1333                 /* With the page set, use the generic suspend */
1334                 err = genphy_suspend(phydev);
1335                 if (err < 0)
1336                         goto error;
1337
1338                 /* Then, the copper link */
1339                 err = marvell_set_page(phydev, MII_MARVELL_COPPER_PAGE);
1340                 if (err < 0)
1341                         goto error;
1342         }
1343
1344         /* With the page set, use the generic suspend */
1345         return genphy_suspend(phydev);
1346
1347 error:
1348         marvell_set_page(phydev, MII_MARVELL_COPPER_PAGE);
1349         return err;
1350 }
1351
1352 /* marvell_resume
1353  *
1354  * Some Marvell's phys have two modes: fiber and copper.
1355  * Both need to be resumed
1356  */
1357 static int marvell_resume(struct phy_device *phydev)
1358 {
1359         int err;
1360
1361         /* Resume the fiber mode first */
1362         if (!(phydev->supported & SUPPORTED_FIBRE)) {
1363                 err = marvell_set_page(phydev, MII_MARVELL_FIBER_PAGE);
1364                 if (err < 0)
1365                         goto error;
1366
1367                 /* With the page set, use the generic resume */
1368                 err = genphy_resume(phydev);
1369                 if (err < 0)
1370                         goto error;
1371
1372                 /* Then, the copper link */
1373                 err = marvell_set_page(phydev, MII_MARVELL_COPPER_PAGE);
1374                 if (err < 0)
1375                         goto error;
1376         }
1377
1378         /* With the page set, use the generic resume */
1379         return genphy_resume(phydev);
1380
1381 error:
1382         marvell_set_page(phydev, MII_MARVELL_COPPER_PAGE);
1383         return err;
1384 }
1385
1386 static int marvell_aneg_done(struct phy_device *phydev)
1387 {
1388         int retval = phy_read(phydev, MII_M1011_PHY_STATUS);
1389
1390         return (retval < 0) ? retval : (retval & MII_M1011_PHY_STATUS_RESOLVED);
1391 }
1392
1393 static int m88e1121_did_interrupt(struct phy_device *phydev)
1394 {
1395         int imask;
1396
1397         imask = phy_read(phydev, MII_M1011_IEVENT);
1398
1399         if (imask & MII_M1011_IMASK_INIT)
1400                 return 1;
1401
1402         return 0;
1403 }
1404
1405 static void m88e1318_get_wol(struct phy_device *phydev,
1406                              struct ethtool_wolinfo *wol)
1407 {
1408         int oldpage, ret = 0;
1409
1410         wol->supported = WAKE_MAGIC;
1411         wol->wolopts = 0;
1412
1413         oldpage = phy_select_page(phydev, MII_MARVELL_WOL_PAGE);
1414         if (oldpage < 0)
1415                 goto error;
1416
1417         ret = __phy_read(phydev, MII_88E1318S_PHY_WOL_CTRL);
1418         if (ret & MII_88E1318S_PHY_WOL_CTRL_MAGIC_PACKET_MATCH_ENABLE)
1419                 wol->wolopts |= WAKE_MAGIC;
1420
1421 error:
1422         phy_restore_page(phydev, oldpage, ret);
1423 }
1424
1425 static int m88e1318_set_wol(struct phy_device *phydev,
1426                             struct ethtool_wolinfo *wol)
1427 {
1428         int err = 0, oldpage;
1429
1430         oldpage = phy_save_page(phydev);
1431         if (oldpage < 0)
1432                 goto error;
1433
1434         if (wol->wolopts & WAKE_MAGIC) {
1435                 /* Explicitly switch to page 0x00, just to be sure */
1436                 err = marvell_write_page(phydev, MII_MARVELL_COPPER_PAGE);
1437                 if (err < 0)
1438                         goto error;
1439
1440                 /* If WOL event happened once, the LED[2] interrupt pin
1441                  * will not be cleared unless we reading the interrupt status
1442                  * register. If interrupts are in use, the normal interrupt
1443                  * handling will clear the WOL event. Clear the WOL event
1444                  * before enabling it if !phy_interrupt_is_valid()
1445                  */
1446                 if (!phy_interrupt_is_valid(phydev))
1447                         __phy_read(phydev, MII_M1011_IEVENT);
1448
1449                 /* Enable the WOL interrupt */
1450                 err = __phy_modify(phydev, MII_88E1318S_PHY_CSIER, 0,
1451                                    MII_88E1318S_PHY_CSIER_WOL_EIE);
1452                 if (err < 0)
1453                         goto error;
1454
1455                 err = marvell_write_page(phydev, MII_MARVELL_LED_PAGE);
1456                 if (err < 0)
1457                         goto error;
1458
1459                 /* Setup LED[2] as interrupt pin (active low) */
1460                 err = __phy_modify(phydev, MII_88E1318S_PHY_LED_TCR,
1461                                    MII_88E1318S_PHY_LED_TCR_FORCE_INT,
1462                                    MII_88E1318S_PHY_LED_TCR_INTn_ENABLE |
1463                                    MII_88E1318S_PHY_LED_TCR_INT_ACTIVE_LOW);
1464                 if (err < 0)
1465                         goto error;
1466
1467                 err = marvell_write_page(phydev, MII_MARVELL_WOL_PAGE);
1468                 if (err < 0)
1469                         goto error;
1470
1471                 /* Store the device address for the magic packet */
1472                 err = __phy_write(phydev, MII_88E1318S_PHY_MAGIC_PACKET_WORD2,
1473                                 ((phydev->attached_dev->dev_addr[5] << 8) |
1474                                  phydev->attached_dev->dev_addr[4]));
1475                 if (err < 0)
1476                         goto error;
1477                 err = __phy_write(phydev, MII_88E1318S_PHY_MAGIC_PACKET_WORD1,
1478                                 ((phydev->attached_dev->dev_addr[3] << 8) |
1479                                  phydev->attached_dev->dev_addr[2]));
1480                 if (err < 0)
1481                         goto error;
1482                 err = __phy_write(phydev, MII_88E1318S_PHY_MAGIC_PACKET_WORD0,
1483                                 ((phydev->attached_dev->dev_addr[1] << 8) |
1484                                  phydev->attached_dev->dev_addr[0]));
1485                 if (err < 0)
1486                         goto error;
1487
1488                 /* Clear WOL status and enable magic packet matching */
1489                 err = __phy_modify(phydev, MII_88E1318S_PHY_WOL_CTRL, 0,
1490                                    MII_88E1318S_PHY_WOL_CTRL_CLEAR_WOL_STATUS |
1491                                    MII_88E1318S_PHY_WOL_CTRL_MAGIC_PACKET_MATCH_ENABLE);
1492                 if (err < 0)
1493                         goto error;
1494         } else {
1495                 err = marvell_write_page(phydev, MII_MARVELL_WOL_PAGE);
1496                 if (err < 0)
1497                         goto error;
1498
1499                 /* Clear WOL status and disable magic packet matching */
1500                 err = __phy_modify(phydev, MII_88E1318S_PHY_WOL_CTRL,
1501                                    MII_88E1318S_PHY_WOL_CTRL_MAGIC_PACKET_MATCH_ENABLE,
1502                                    MII_88E1318S_PHY_WOL_CTRL_CLEAR_WOL_STATUS);
1503                 if (err < 0)
1504                         goto error;
1505         }
1506
1507 error:
1508         return phy_restore_page(phydev, oldpage, err);
1509 }
1510
1511 static int marvell_get_sset_count(struct phy_device *phydev)
1512 {
1513         if (phydev->supported & SUPPORTED_FIBRE)
1514                 return ARRAY_SIZE(marvell_hw_stats);
1515         else
1516                 return ARRAY_SIZE(marvell_hw_stats) - NB_FIBER_STATS;
1517 }
1518
1519 static void marvell_get_strings(struct phy_device *phydev, u8 *data)
1520 {
1521         int count = marvell_get_sset_count(phydev);
1522         int i;
1523
1524         for (i = 0; i < count; i++) {
1525                 strlcpy(data + i * ETH_GSTRING_LEN,
1526                         marvell_hw_stats[i].string, ETH_GSTRING_LEN);
1527         }
1528 }
1529
1530 static u64 marvell_get_stat(struct phy_device *phydev, int i)
1531 {
1532         struct marvell_hw_stat stat = marvell_hw_stats[i];
1533         struct marvell_priv *priv = phydev->priv;
1534         int val;
1535         u64 ret;
1536
1537         val = phy_read_paged(phydev, stat.page, stat.reg);
1538         if (val < 0) {
1539                 ret = U64_MAX;
1540         } else {
1541                 val = val & ((1 << stat.bits) - 1);
1542                 priv->stats[i] += val;
1543                 ret = priv->stats[i];
1544         }
1545
1546         return ret;
1547 }
1548
1549 static void marvell_get_stats(struct phy_device *phydev,
1550                               struct ethtool_stats *stats, u64 *data)
1551 {
1552         int count = marvell_get_sset_count(phydev);
1553         int i;
1554
1555         for (i = 0; i < count; i++)
1556                 data[i] = marvell_get_stat(phydev, i);
1557 }
1558
1559 #ifdef CONFIG_HWMON
1560 static int m88e1121_get_temp(struct phy_device *phydev, long *temp)
1561 {
1562         int oldpage;
1563         int ret = 0;
1564         int val;
1565
1566         *temp = 0;
1567
1568         oldpage = phy_select_page(phydev, MII_MARVELL_MISC_TEST_PAGE);
1569         if (oldpage < 0)
1570                 goto error;
1571
1572         /* Enable temperature sensor */
1573         ret = __phy_read(phydev, MII_88E1121_MISC_TEST);
1574         if (ret < 0)
1575                 goto error;
1576
1577         ret = __phy_write(phydev, MII_88E1121_MISC_TEST,
1578                           ret | MII_88E1121_MISC_TEST_TEMP_SENSOR_EN);
1579         if (ret < 0)
1580                 goto error;
1581
1582         /* Wait for temperature to stabilize */
1583         usleep_range(10000, 12000);
1584
1585         val = __phy_read(phydev, MII_88E1121_MISC_TEST);
1586         if (val < 0) {
1587                 ret = val;
1588                 goto error;
1589         }
1590
1591         /* Disable temperature sensor */
1592         ret = __phy_write(phydev, MII_88E1121_MISC_TEST,
1593                           ret & ~MII_88E1121_MISC_TEST_TEMP_SENSOR_EN);
1594         if (ret < 0)
1595                 goto error;
1596
1597         *temp = ((val & MII_88E1121_MISC_TEST_TEMP_MASK) - 5) * 5000;
1598
1599 error:
1600         return phy_restore_page(phydev, oldpage, ret);
1601 }
1602
1603 static int m88e1121_hwmon_read(struct device *dev,
1604                                enum hwmon_sensor_types type,
1605                                u32 attr, int channel, long *temp)
1606 {
1607         struct phy_device *phydev = dev_get_drvdata(dev);
1608         int err;
1609
1610         switch (attr) {
1611         case hwmon_temp_input:
1612                 err = m88e1121_get_temp(phydev, temp);
1613                 break;
1614         default:
1615                 return -EOPNOTSUPP;
1616         }
1617
1618         return err;
1619 }
1620
1621 static umode_t m88e1121_hwmon_is_visible(const void *data,
1622                                          enum hwmon_sensor_types type,
1623                                          u32 attr, int channel)
1624 {
1625         if (type != hwmon_temp)
1626                 return 0;
1627
1628         switch (attr) {
1629         case hwmon_temp_input:
1630                 return 0444;
1631         default:
1632                 return 0;
1633         }
1634 }
1635
1636 static u32 m88e1121_hwmon_chip_config[] = {
1637         HWMON_C_REGISTER_TZ,
1638         0
1639 };
1640
1641 static const struct hwmon_channel_info m88e1121_hwmon_chip = {
1642         .type = hwmon_chip,
1643         .config = m88e1121_hwmon_chip_config,
1644 };
1645
1646 static u32 m88e1121_hwmon_temp_config[] = {
1647         HWMON_T_INPUT,
1648         0
1649 };
1650
1651 static const struct hwmon_channel_info m88e1121_hwmon_temp = {
1652         .type = hwmon_temp,
1653         .config = m88e1121_hwmon_temp_config,
1654 };
1655
1656 static const struct hwmon_channel_info *m88e1121_hwmon_info[] = {
1657         &m88e1121_hwmon_chip,
1658         &m88e1121_hwmon_temp,
1659         NULL
1660 };
1661
1662 static const struct hwmon_ops m88e1121_hwmon_hwmon_ops = {
1663         .is_visible = m88e1121_hwmon_is_visible,
1664         .read = m88e1121_hwmon_read,
1665 };
1666
1667 static const struct hwmon_chip_info m88e1121_hwmon_chip_info = {
1668         .ops = &m88e1121_hwmon_hwmon_ops,
1669         .info = m88e1121_hwmon_info,
1670 };
1671
1672 static int m88e1510_get_temp(struct phy_device *phydev, long *temp)
1673 {
1674         int ret;
1675
1676         *temp = 0;
1677
1678         ret = phy_read_paged(phydev, MII_MARVELL_MISC_TEST_PAGE,
1679                              MII_88E1510_TEMP_SENSOR);
1680         if (ret < 0)
1681                 return ret;
1682
1683         *temp = ((ret & MII_88E1510_TEMP_SENSOR_MASK) - 25) * 1000;
1684
1685         return 0;
1686 }
1687
1688 static int m88e1510_get_temp_critical(struct phy_device *phydev, long *temp)
1689 {
1690         int ret;
1691
1692         *temp = 0;
1693
1694         ret = phy_read_paged(phydev, MII_MARVELL_MISC_TEST_PAGE,
1695                              MII_88E1121_MISC_TEST);
1696         if (ret < 0)
1697                 return ret;
1698
1699         *temp = (((ret & MII_88E1510_MISC_TEST_TEMP_THRESHOLD_MASK) >>
1700                   MII_88E1510_MISC_TEST_TEMP_THRESHOLD_SHIFT) * 5) - 25;
1701         /* convert to mC */
1702         *temp *= 1000;
1703
1704         return 0;
1705 }
1706
1707 static int m88e1510_set_temp_critical(struct phy_device *phydev, long temp)
1708 {
1709         temp = temp / 1000;
1710         temp = clamp_val(DIV_ROUND_CLOSEST(temp, 5) + 5, 0, 0x1f);
1711
1712         return phy_modify_paged(phydev, MII_MARVELL_MISC_TEST_PAGE,
1713                                 MII_88E1121_MISC_TEST,
1714                                 MII_88E1510_MISC_TEST_TEMP_THRESHOLD_MASK,
1715                                 temp << MII_88E1510_MISC_TEST_TEMP_THRESHOLD_SHIFT);
1716 }
1717
1718 static int m88e1510_get_temp_alarm(struct phy_device *phydev, long *alarm)
1719 {
1720         int ret;
1721
1722         *alarm = false;
1723
1724         ret = phy_read_paged(phydev, MII_MARVELL_MISC_TEST_PAGE,
1725                              MII_88E1121_MISC_TEST);
1726         if (ret < 0)
1727                 return ret;
1728
1729         *alarm = !!(ret & MII_88E1510_MISC_TEST_TEMP_IRQ);
1730
1731         return 0;
1732 }
1733
1734 static int m88e1510_hwmon_read(struct device *dev,
1735                                enum hwmon_sensor_types type,
1736                                u32 attr, int channel, long *temp)
1737 {
1738         struct phy_device *phydev = dev_get_drvdata(dev);
1739         int err;
1740
1741         switch (attr) {
1742         case hwmon_temp_input:
1743                 err = m88e1510_get_temp(phydev, temp);
1744                 break;
1745         case hwmon_temp_crit:
1746                 err = m88e1510_get_temp_critical(phydev, temp);
1747                 break;
1748         case hwmon_temp_max_alarm:
1749                 err = m88e1510_get_temp_alarm(phydev, temp);
1750                 break;
1751         default:
1752                 return -EOPNOTSUPP;
1753         }
1754
1755         return err;
1756 }
1757
1758 static int m88e1510_hwmon_write(struct device *dev,
1759                                 enum hwmon_sensor_types type,
1760                                 u32 attr, int channel, long temp)
1761 {
1762         struct phy_device *phydev = dev_get_drvdata(dev);
1763         int err;
1764
1765         switch (attr) {
1766         case hwmon_temp_crit:
1767                 err = m88e1510_set_temp_critical(phydev, temp);
1768                 break;
1769         default:
1770                 return -EOPNOTSUPP;
1771         }
1772         return err;
1773 }
1774
1775 static umode_t m88e1510_hwmon_is_visible(const void *data,
1776                                          enum hwmon_sensor_types type,
1777                                          u32 attr, int channel)
1778 {
1779         if (type != hwmon_temp)
1780                 return 0;
1781
1782         switch (attr) {
1783         case hwmon_temp_input:
1784         case hwmon_temp_max_alarm:
1785                 return 0444;
1786         case hwmon_temp_crit:
1787                 return 0644;
1788         default:
1789                 return 0;
1790         }
1791 }
1792
1793 static u32 m88e1510_hwmon_temp_config[] = {
1794         HWMON_T_INPUT | HWMON_T_CRIT | HWMON_T_MAX_ALARM,
1795         0
1796 };
1797
1798 static const struct hwmon_channel_info m88e1510_hwmon_temp = {
1799         .type = hwmon_temp,
1800         .config = m88e1510_hwmon_temp_config,
1801 };
1802
1803 static const struct hwmon_channel_info *m88e1510_hwmon_info[] = {
1804         &m88e1121_hwmon_chip,
1805         &m88e1510_hwmon_temp,
1806         NULL
1807 };
1808
1809 static const struct hwmon_ops m88e1510_hwmon_hwmon_ops = {
1810         .is_visible = m88e1510_hwmon_is_visible,
1811         .read = m88e1510_hwmon_read,
1812         .write = m88e1510_hwmon_write,
1813 };
1814
1815 static const struct hwmon_chip_info m88e1510_hwmon_chip_info = {
1816         .ops = &m88e1510_hwmon_hwmon_ops,
1817         .info = m88e1510_hwmon_info,
1818 };
1819
1820 static int m88e6390_get_temp(struct phy_device *phydev, long *temp)
1821 {
1822         int sum = 0;
1823         int oldpage;
1824         int ret = 0;
1825         int i;
1826
1827         *temp = 0;
1828
1829         oldpage = phy_select_page(phydev, MII_MARVELL_MISC_TEST_PAGE);
1830         if (oldpage < 0)
1831                 goto error;
1832
1833         /* Enable temperature sensor */
1834         ret = __phy_read(phydev, MII_88E6390_MISC_TEST);
1835         if (ret < 0)
1836                 goto error;
1837
1838         ret = ret & ~MII_88E6390_MISC_TEST_SAMPLE_MASK;
1839         ret |= MII_88E6390_MISC_TEST_SAMPLE_ENABLE |
1840                 MII_88E6390_MISC_TEST_SAMPLE_1S;
1841
1842         ret = __phy_write(phydev, MII_88E6390_MISC_TEST, ret);
1843         if (ret < 0)
1844                 goto error;
1845
1846         /* Wait for temperature to stabilize */
1847         usleep_range(10000, 12000);
1848
1849         /* Reading the temperature sense has an errata. You need to read
1850          * a number of times and take an average.
1851          */
1852         for (i = 0; i < MII_88E6390_TEMP_SENSOR_SAMPLES; i++) {
1853                 ret = __phy_read(phydev, MII_88E6390_TEMP_SENSOR);
1854                 if (ret < 0)
1855                         goto error;
1856                 sum += ret & MII_88E6390_TEMP_SENSOR_MASK;
1857         }
1858
1859         sum /= MII_88E6390_TEMP_SENSOR_SAMPLES;
1860         *temp = (sum  - 75) * 1000;
1861
1862         /* Disable temperature sensor */
1863         ret = __phy_read(phydev, MII_88E6390_MISC_TEST);
1864         if (ret < 0)
1865                 goto error;
1866
1867         ret = ret & ~MII_88E6390_MISC_TEST_SAMPLE_MASK;
1868         ret |= MII_88E6390_MISC_TEST_SAMPLE_DISABLE;
1869
1870         ret = __phy_write(phydev, MII_88E6390_MISC_TEST, ret);
1871
1872 error:
1873         phy_restore_page(phydev, oldpage, ret);
1874
1875         return ret;
1876 }
1877
1878 static int m88e6390_hwmon_read(struct device *dev,
1879                                enum hwmon_sensor_types type,
1880                                u32 attr, int channel, long *temp)
1881 {
1882         struct phy_device *phydev = dev_get_drvdata(dev);
1883         int err;
1884
1885         switch (attr) {
1886         case hwmon_temp_input:
1887                 err = m88e6390_get_temp(phydev, temp);
1888                 break;
1889         default:
1890                 return -EOPNOTSUPP;
1891         }
1892
1893         return err;
1894 }
1895
1896 static umode_t m88e6390_hwmon_is_visible(const void *data,
1897                                          enum hwmon_sensor_types type,
1898                                          u32 attr, int channel)
1899 {
1900         if (type != hwmon_temp)
1901                 return 0;
1902
1903         switch (attr) {
1904         case hwmon_temp_input:
1905                 return 0444;
1906         default:
1907                 return 0;
1908         }
1909 }
1910
1911 static u32 m88e6390_hwmon_temp_config[] = {
1912         HWMON_T_INPUT,
1913         0
1914 };
1915
1916 static const struct hwmon_channel_info m88e6390_hwmon_temp = {
1917         .type = hwmon_temp,
1918         .config = m88e6390_hwmon_temp_config,
1919 };
1920
1921 static const struct hwmon_channel_info *m88e6390_hwmon_info[] = {
1922         &m88e1121_hwmon_chip,
1923         &m88e6390_hwmon_temp,
1924         NULL
1925 };
1926
1927 static const struct hwmon_ops m88e6390_hwmon_hwmon_ops = {
1928         .is_visible = m88e6390_hwmon_is_visible,
1929         .read = m88e6390_hwmon_read,
1930 };
1931
1932 static const struct hwmon_chip_info m88e6390_hwmon_chip_info = {
1933         .ops = &m88e6390_hwmon_hwmon_ops,
1934         .info = m88e6390_hwmon_info,
1935 };
1936
1937 static int marvell_hwmon_name(struct phy_device *phydev)
1938 {
1939         struct marvell_priv *priv = phydev->priv;
1940         struct device *dev = &phydev->mdio.dev;
1941         const char *devname = dev_name(dev);
1942         size_t len = strlen(devname);
1943         int i, j;
1944
1945         priv->hwmon_name = devm_kzalloc(dev, len, GFP_KERNEL);
1946         if (!priv->hwmon_name)
1947                 return -ENOMEM;
1948
1949         for (i = j = 0; i < len && devname[i]; i++) {
1950                 if (isalnum(devname[i]))
1951                         priv->hwmon_name[j++] = devname[i];
1952         }
1953
1954         return 0;
1955 }
1956
1957 static int marvell_hwmon_probe(struct phy_device *phydev,
1958                                const struct hwmon_chip_info *chip)
1959 {
1960         struct marvell_priv *priv = phydev->priv;
1961         struct device *dev = &phydev->mdio.dev;
1962         int err;
1963
1964         err = marvell_hwmon_name(phydev);
1965         if (err)
1966                 return err;
1967
1968         priv->hwmon_dev = devm_hwmon_device_register_with_info(
1969                 dev, priv->hwmon_name, phydev, chip, NULL);
1970
1971         return PTR_ERR_OR_ZERO(priv->hwmon_dev);
1972 }
1973
1974 static int m88e1121_hwmon_probe(struct phy_device *phydev)
1975 {
1976         return marvell_hwmon_probe(phydev, &m88e1121_hwmon_chip_info);
1977 }
1978
1979 static int m88e1510_hwmon_probe(struct phy_device *phydev)
1980 {
1981         return marvell_hwmon_probe(phydev, &m88e1510_hwmon_chip_info);
1982 }
1983
1984 static int m88e6390_hwmon_probe(struct phy_device *phydev)
1985 {
1986         return marvell_hwmon_probe(phydev, &m88e6390_hwmon_chip_info);
1987 }
1988 #else
1989 static int m88e1121_hwmon_probe(struct phy_device *phydev)
1990 {
1991         return 0;
1992 }
1993
1994 static int m88e1510_hwmon_probe(struct phy_device *phydev)
1995 {
1996         return 0;
1997 }
1998
1999 static int m88e6390_hwmon_probe(struct phy_device *phydev)
2000 {
2001         return 0;
2002 }
2003 #endif
2004
2005 static int marvell_probe(struct phy_device *phydev)
2006 {
2007         struct marvell_priv *priv;
2008
2009         priv = devm_kzalloc(&phydev->mdio.dev, sizeof(*priv), GFP_KERNEL);
2010         if (!priv)
2011                 return -ENOMEM;
2012
2013         phydev->priv = priv;
2014
2015         return 0;
2016 }
2017
2018 static int m88e1121_probe(struct phy_device *phydev)
2019 {
2020         int err;
2021
2022         err = marvell_probe(phydev);
2023         if (err)
2024                 return err;
2025
2026         return m88e1121_hwmon_probe(phydev);
2027 }
2028
2029 static int m88e1510_probe(struct phy_device *phydev)
2030 {
2031         int err;
2032
2033         err = marvell_probe(phydev);
2034         if (err)
2035                 return err;
2036
2037         return m88e1510_hwmon_probe(phydev);
2038 }
2039
2040 static int m88e6390_probe(struct phy_device *phydev)
2041 {
2042         int err;
2043
2044         err = marvell_probe(phydev);
2045         if (err)
2046                 return err;
2047
2048         return m88e6390_hwmon_probe(phydev);
2049 }
2050
2051 static struct phy_driver marvell_drivers[] = {
2052         {
2053                 .phy_id = MARVELL_PHY_ID_88E1101,
2054                 .phy_id_mask = MARVELL_PHY_ID_MASK,
2055                 .name = "Marvell 88E1101",
2056                 .features = PHY_GBIT_FEATURES,
2057                 .flags = PHY_HAS_INTERRUPT,
2058                 .probe = marvell_probe,
2059                 .config_init = &marvell_config_init,
2060                 .config_aneg = &m88e1101_config_aneg,
2061                 .ack_interrupt = &marvell_ack_interrupt,
2062                 .config_intr = &marvell_config_intr,
2063                 .resume = &genphy_resume,
2064                 .suspend = &genphy_suspend,
2065                 .read_page = marvell_read_page,
2066                 .write_page = marvell_write_page,
2067                 .get_sset_count = marvell_get_sset_count,
2068                 .get_strings = marvell_get_strings,
2069                 .get_stats = marvell_get_stats,
2070         },
2071         {
2072                 .phy_id = MARVELL_PHY_ID_88E1112,
2073                 .phy_id_mask = MARVELL_PHY_ID_MASK,
2074                 .name = "Marvell 88E1112",
2075                 .features = PHY_GBIT_FEATURES,
2076                 .flags = PHY_HAS_INTERRUPT,
2077                 .probe = marvell_probe,
2078                 .config_init = &m88e1111_config_init,
2079                 .config_aneg = &marvell_config_aneg,
2080                 .ack_interrupt = &marvell_ack_interrupt,
2081                 .config_intr = &marvell_config_intr,
2082                 .resume = &genphy_resume,
2083                 .suspend = &genphy_suspend,
2084                 .read_page = marvell_read_page,
2085                 .write_page = marvell_write_page,
2086                 .get_sset_count = marvell_get_sset_count,
2087                 .get_strings = marvell_get_strings,
2088                 .get_stats = marvell_get_stats,
2089         },
2090         {
2091                 .phy_id = MARVELL_PHY_ID_88E1111,
2092                 .phy_id_mask = MARVELL_PHY_ID_MASK,
2093                 .name = "Marvell 88E1111",
2094                 .features = PHY_GBIT_FEATURES,
2095                 .flags = PHY_HAS_INTERRUPT,
2096                 .probe = marvell_probe,
2097                 .config_init = &m88e1111_config_init,
2098                 .config_aneg = &m88e1111_config_aneg,
2099                 .read_status = &marvell_read_status,
2100                 .ack_interrupt = &marvell_ack_interrupt,
2101                 .config_intr = &marvell_config_intr,
2102                 .resume = &genphy_resume,
2103                 .suspend = &genphy_suspend,
2104                 .read_page = marvell_read_page,
2105                 .write_page = marvell_write_page,
2106                 .get_sset_count = marvell_get_sset_count,
2107                 .get_strings = marvell_get_strings,
2108                 .get_stats = marvell_get_stats,
2109         },
2110         {
2111                 .phy_id = MARVELL_PHY_ID_88E1118,
2112                 .phy_id_mask = MARVELL_PHY_ID_MASK,
2113                 .name = "Marvell 88E1118",
2114                 .features = PHY_GBIT_FEATURES,
2115                 .flags = PHY_HAS_INTERRUPT,
2116                 .probe = marvell_probe,
2117                 .config_init = &m88e1118_config_init,
2118                 .config_aneg = &m88e1118_config_aneg,
2119                 .ack_interrupt = &marvell_ack_interrupt,
2120                 .config_intr = &marvell_config_intr,
2121                 .resume = &genphy_resume,
2122                 .suspend = &genphy_suspend,
2123                 .read_page = marvell_read_page,
2124                 .write_page = marvell_write_page,
2125                 .get_sset_count = marvell_get_sset_count,
2126                 .get_strings = marvell_get_strings,
2127                 .get_stats = marvell_get_stats,
2128         },
2129         {
2130                 .phy_id = MARVELL_PHY_ID_88E1121R,
2131                 .phy_id_mask = MARVELL_PHY_ID_MASK,
2132                 .name = "Marvell 88E1121R",
2133                 .features = PHY_GBIT_FEATURES,
2134                 .flags = PHY_HAS_INTERRUPT,
2135                 .probe = &m88e1121_probe,
2136                 .config_init = &marvell_config_init,
2137                 .config_aneg = &m88e1121_config_aneg,
2138                 .read_status = &marvell_read_status,
2139                 .ack_interrupt = &marvell_ack_interrupt,
2140                 .config_intr = &marvell_config_intr,
2141                 .did_interrupt = &m88e1121_did_interrupt,
2142                 .resume = &genphy_resume,
2143                 .suspend = &genphy_suspend,
2144                 .read_page = marvell_read_page,
2145                 .write_page = marvell_write_page,
2146                 .get_sset_count = marvell_get_sset_count,
2147                 .get_strings = marvell_get_strings,
2148                 .get_stats = marvell_get_stats,
2149         },
2150         {
2151                 .phy_id = MARVELL_PHY_ID_88E1318S,
2152                 .phy_id_mask = MARVELL_PHY_ID_MASK,
2153                 .name = "Marvell 88E1318S",
2154                 .features = PHY_GBIT_FEATURES,
2155                 .flags = PHY_HAS_INTERRUPT,
2156                 .probe = marvell_probe,
2157                 .config_init = &m88e1318_config_init,
2158                 .config_aneg = &m88e1318_config_aneg,
2159                 .read_status = &marvell_read_status,
2160                 .ack_interrupt = &marvell_ack_interrupt,
2161                 .config_intr = &marvell_config_intr,
2162                 .did_interrupt = &m88e1121_did_interrupt,
2163                 .get_wol = &m88e1318_get_wol,
2164                 .set_wol = &m88e1318_set_wol,
2165                 .resume = &genphy_resume,
2166                 .suspend = &genphy_suspend,
2167                 .read_page = marvell_read_page,
2168                 .write_page = marvell_write_page,
2169                 .get_sset_count = marvell_get_sset_count,
2170                 .get_strings = marvell_get_strings,
2171                 .get_stats = marvell_get_stats,
2172         },
2173         {
2174                 .phy_id = MARVELL_PHY_ID_88E1145,
2175                 .phy_id_mask = MARVELL_PHY_ID_MASK,
2176                 .name = "Marvell 88E1145",
2177                 .features = PHY_GBIT_FEATURES,
2178                 .flags = PHY_HAS_INTERRUPT,
2179                 .probe = marvell_probe,
2180                 .config_init = &m88e1145_config_init,
2181                 .config_aneg = &m88e1101_config_aneg,
2182                 .read_status = &genphy_read_status,
2183                 .ack_interrupt = &marvell_ack_interrupt,
2184                 .config_intr = &marvell_config_intr,
2185                 .resume = &genphy_resume,
2186                 .suspend = &genphy_suspend,
2187                 .read_page = marvell_read_page,
2188                 .write_page = marvell_write_page,
2189                 .get_sset_count = marvell_get_sset_count,
2190                 .get_strings = marvell_get_strings,
2191                 .get_stats = marvell_get_stats,
2192         },
2193         {
2194                 .phy_id = MARVELL_PHY_ID_88E1149R,
2195                 .phy_id_mask = MARVELL_PHY_ID_MASK,
2196                 .name = "Marvell 88E1149R",
2197                 .features = PHY_GBIT_FEATURES,
2198                 .flags = PHY_HAS_INTERRUPT,
2199                 .probe = marvell_probe,
2200                 .config_init = &m88e1149_config_init,
2201                 .config_aneg = &m88e1118_config_aneg,
2202                 .ack_interrupt = &marvell_ack_interrupt,
2203                 .config_intr = &marvell_config_intr,
2204                 .resume = &genphy_resume,
2205                 .suspend = &genphy_suspend,
2206                 .read_page = marvell_read_page,
2207                 .write_page = marvell_write_page,
2208                 .get_sset_count = marvell_get_sset_count,
2209                 .get_strings = marvell_get_strings,
2210                 .get_stats = marvell_get_stats,
2211         },
2212         {
2213                 .phy_id = MARVELL_PHY_ID_88E1240,
2214                 .phy_id_mask = MARVELL_PHY_ID_MASK,
2215                 .name = "Marvell 88E1240",
2216                 .features = PHY_GBIT_FEATURES,
2217                 .flags = PHY_HAS_INTERRUPT,
2218                 .probe = marvell_probe,
2219                 .config_init = &m88e1111_config_init,
2220                 .config_aneg = &marvell_config_aneg,
2221                 .ack_interrupt = &marvell_ack_interrupt,
2222                 .config_intr = &marvell_config_intr,
2223                 .resume = &genphy_resume,
2224                 .suspend = &genphy_suspend,
2225                 .read_page = marvell_read_page,
2226                 .write_page = marvell_write_page,
2227                 .get_sset_count = marvell_get_sset_count,
2228                 .get_strings = marvell_get_strings,
2229                 .get_stats = marvell_get_stats,
2230         },
2231         {
2232                 .phy_id = MARVELL_PHY_ID_88E1116R,
2233                 .phy_id_mask = MARVELL_PHY_ID_MASK,
2234                 .name = "Marvell 88E1116R",
2235                 .features = PHY_GBIT_FEATURES,
2236                 .flags = PHY_HAS_INTERRUPT,
2237                 .probe = marvell_probe,
2238                 .config_init = &m88e1116r_config_init,
2239                 .ack_interrupt = &marvell_ack_interrupt,
2240                 .config_intr = &marvell_config_intr,
2241                 .resume = &genphy_resume,
2242                 .suspend = &genphy_suspend,
2243                 .read_page = marvell_read_page,
2244                 .write_page = marvell_write_page,
2245                 .get_sset_count = marvell_get_sset_count,
2246                 .get_strings = marvell_get_strings,
2247                 .get_stats = marvell_get_stats,
2248         },
2249         {
2250                 .phy_id = MARVELL_PHY_ID_88E1510,
2251                 .phy_id_mask = MARVELL_PHY_ID_MASK,
2252                 .name = "Marvell 88E1510",
2253                 .features = PHY_GBIT_FEATURES | SUPPORTED_FIBRE,
2254                 .flags = PHY_HAS_INTERRUPT,
2255                 .probe = &m88e1510_probe,
2256                 .config_init = &m88e1510_config_init,
2257                 .config_aneg = &m88e1510_config_aneg,
2258                 .read_status = &marvell_read_status,
2259                 .ack_interrupt = &marvell_ack_interrupt,
2260                 .config_intr = &marvell_config_intr,
2261                 .did_interrupt = &m88e1121_did_interrupt,
2262                 .get_wol = &m88e1318_get_wol,
2263                 .set_wol = &m88e1318_set_wol,
2264                 .resume = &marvell_resume,
2265                 .suspend = &marvell_suspend,
2266                 .read_page = marvell_read_page,
2267                 .write_page = marvell_write_page,
2268                 .get_sset_count = marvell_get_sset_count,
2269                 .get_strings = marvell_get_strings,
2270                 .get_stats = marvell_get_stats,
2271                 .set_loopback = genphy_loopback,
2272         },
2273         {
2274                 .phy_id = MARVELL_PHY_ID_88E1540,
2275                 .phy_id_mask = MARVELL_PHY_ID_MASK,
2276                 .name = "Marvell 88E1540",
2277                 .features = PHY_GBIT_FEATURES,
2278                 .flags = PHY_HAS_INTERRUPT,
2279                 .probe = m88e1510_probe,
2280                 .config_init = &marvell_config_init,
2281                 .config_aneg = &m88e1510_config_aneg,
2282                 .read_status = &marvell_read_status,
2283                 .ack_interrupt = &marvell_ack_interrupt,
2284                 .config_intr = &marvell_config_intr,
2285                 .did_interrupt = &m88e1121_did_interrupt,
2286                 .resume = &genphy_resume,
2287                 .suspend = &genphy_suspend,
2288                 .read_page = marvell_read_page,
2289                 .write_page = marvell_write_page,
2290                 .get_sset_count = marvell_get_sset_count,
2291                 .get_strings = marvell_get_strings,
2292                 .get_stats = marvell_get_stats,
2293         },
2294         {
2295                 .phy_id = MARVELL_PHY_ID_88E1545,
2296                 .phy_id_mask = MARVELL_PHY_ID_MASK,
2297                 .name = "Marvell 88E1545",
2298                 .probe = m88e1510_probe,
2299                 .features = PHY_GBIT_FEATURES,
2300                 .flags = PHY_HAS_INTERRUPT,
2301                 .config_init = &marvell_config_init,
2302                 .config_aneg = &m88e1510_config_aneg,
2303                 .read_status = &marvell_read_status,
2304                 .ack_interrupt = &marvell_ack_interrupt,
2305                 .config_intr = &marvell_config_intr,
2306                 .did_interrupt = &m88e1121_did_interrupt,
2307                 .resume = &genphy_resume,
2308                 .suspend = &genphy_suspend,
2309                 .read_page = marvell_read_page,
2310                 .write_page = marvell_write_page,
2311                 .get_sset_count = marvell_get_sset_count,
2312                 .get_strings = marvell_get_strings,
2313                 .get_stats = marvell_get_stats,
2314         },
2315         {
2316                 .phy_id = MARVELL_PHY_ID_88E3016,
2317                 .phy_id_mask = MARVELL_PHY_ID_MASK,
2318                 .name = "Marvell 88E3016",
2319                 .features = PHY_BASIC_FEATURES,
2320                 .flags = PHY_HAS_INTERRUPT,
2321                 .probe = marvell_probe,
2322                 .config_init = &m88e3016_config_init,
2323                 .aneg_done = &marvell_aneg_done,
2324                 .read_status = &marvell_read_status,
2325                 .ack_interrupt = &marvell_ack_interrupt,
2326                 .config_intr = &marvell_config_intr,
2327                 .did_interrupt = &m88e1121_did_interrupt,
2328                 .resume = &genphy_resume,
2329                 .suspend = &genphy_suspend,
2330                 .read_page = marvell_read_page,
2331                 .write_page = marvell_write_page,
2332                 .get_sset_count = marvell_get_sset_count,
2333                 .get_strings = marvell_get_strings,
2334                 .get_stats = marvell_get_stats,
2335         },
2336         {
2337                 .phy_id = MARVELL_PHY_ID_88E6341_FAMILY,
2338                 .phy_id_mask = MARVELL_PHY_ID_MASK,
2339                 .name = "Marvell 88E6341 Family",
2340                 .features = PHY_GBIT_FEATURES,
2341                 .flags = PHY_HAS_INTERRUPT,
2342                 .probe = m88e1510_probe,
2343                 .config_init = &marvell_config_init,
2344                 .config_aneg = &m88e6390_config_aneg,
2345                 .read_status = &marvell_read_status,
2346                 .ack_interrupt = &marvell_ack_interrupt,
2347                 .config_intr = &marvell_config_intr,
2348                 .did_interrupt = &m88e1121_did_interrupt,
2349                 .resume = &genphy_resume,
2350                 .suspend = &genphy_suspend,
2351                 .read_page = marvell_read_page,
2352                 .write_page = marvell_write_page,
2353                 .get_sset_count = marvell_get_sset_count,
2354                 .get_strings = marvell_get_strings,
2355                 .get_stats = marvell_get_stats,
2356         },
2357         {
2358                 .phy_id = MARVELL_PHY_ID_88E6390_FAMILY,
2359                 .phy_id_mask = MARVELL_PHY_ID_MASK,
2360                 .name = "Marvell 88E6390 Family",
2361                 .features = PHY_GBIT_FEATURES,
2362                 .flags = PHY_HAS_INTERRUPT,
2363                 .probe = m88e6390_probe,
2364                 .config_init = &marvell_config_init,
2365                 .config_aneg = &m88e6390_config_aneg,
2366                 .read_status = &marvell_read_status,
2367                 .ack_interrupt = &marvell_ack_interrupt,
2368                 .config_intr = &marvell_config_intr,
2369                 .did_interrupt = &m88e1121_did_interrupt,
2370                 .resume = &genphy_resume,
2371                 .suspend = &genphy_suspend,
2372                 .read_page = marvell_read_page,
2373                 .write_page = marvell_write_page,
2374                 .get_sset_count = marvell_get_sset_count,
2375                 .get_strings = marvell_get_strings,
2376                 .get_stats = marvell_get_stats,
2377         },
2378 };
2379
2380 module_phy_driver(marvell_drivers);
2381
2382 static struct mdio_device_id __maybe_unused marvell_tbl[] = {
2383         { MARVELL_PHY_ID_88E1101, MARVELL_PHY_ID_MASK },
2384         { MARVELL_PHY_ID_88E1112, MARVELL_PHY_ID_MASK },
2385         { MARVELL_PHY_ID_88E1111, MARVELL_PHY_ID_MASK },
2386         { MARVELL_PHY_ID_88E1118, MARVELL_PHY_ID_MASK },
2387         { MARVELL_PHY_ID_88E1121R, MARVELL_PHY_ID_MASK },
2388         { MARVELL_PHY_ID_88E1145, MARVELL_PHY_ID_MASK },
2389         { MARVELL_PHY_ID_88E1149R, MARVELL_PHY_ID_MASK },
2390         { MARVELL_PHY_ID_88E1240, MARVELL_PHY_ID_MASK },
2391         { MARVELL_PHY_ID_88E1318S, MARVELL_PHY_ID_MASK },
2392         { MARVELL_PHY_ID_88E1116R, MARVELL_PHY_ID_MASK },
2393         { MARVELL_PHY_ID_88E1510, MARVELL_PHY_ID_MASK },
2394         { MARVELL_PHY_ID_88E1540, MARVELL_PHY_ID_MASK },
2395         { MARVELL_PHY_ID_88E1545, MARVELL_PHY_ID_MASK },
2396         { MARVELL_PHY_ID_88E3016, MARVELL_PHY_ID_MASK },
2397         { MARVELL_PHY_ID_88E6341_FAMILY, MARVELL_PHY_ID_MASK },
2398         { MARVELL_PHY_ID_88E6390_FAMILY, MARVELL_PHY_ID_MASK },
2399         { }
2400 };
2401
2402 MODULE_DEVICE_TABLE(mdio, marvell_tbl);