2 * drivers/net/phy/marvell.c
4 * Driver for Marvell PHYs
8 * Copyright (c) 2004 Freescale Semiconductor, Inc.
10 * Copyright (c) 2013 Michael Stapelberg <michael@stapelberg.de>
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.
18 #include <linux/kernel.h>
19 #include <linux/string.h>
20 #include <linux/errno.h>
21 #include <linux/unistd.h>
22 #include <linux/interrupt.h>
23 #include <linux/init.h>
24 #include <linux/delay.h>
25 #include <linux/netdevice.h>
26 #include <linux/etherdevice.h>
27 #include <linux/skbuff.h>
28 #include <linux/spinlock.h>
30 #include <linux/module.h>
31 #include <linux/mii.h>
32 #include <linux/ethtool.h>
33 #include <linux/phy.h>
34 #include <linux/marvell_phy.h>
39 #include <linux/uaccess.h>
41 #define MII_MARVELL_PHY_PAGE 22
43 #define MII_M1011_IEVENT 0x13
44 #define MII_M1011_IEVENT_CLEAR 0x0000
46 #define MII_M1011_IMASK 0x12
47 #define MII_M1011_IMASK_INIT 0x6400
48 #define MII_M1011_IMASK_CLEAR 0x0000
50 #define MII_M1011_PHY_SCR 0x10
51 #define MII_M1011_PHY_SCR_MDI 0x0000
52 #define MII_M1011_PHY_SCR_MDI_X 0x0020
53 #define MII_M1011_PHY_SCR_AUTO_CROSS 0x0060
55 #define MII_M1145_PHY_EXT_ADDR_PAGE 0x16
56 #define MII_M1145_PHY_EXT_SR 0x1b
57 #define MII_M1145_PHY_EXT_CR 0x14
58 #define MII_M1145_RGMII_RX_DELAY 0x0080
59 #define MII_M1145_RGMII_TX_DELAY 0x0002
60 #define MII_M1145_HWCFG_MODE_SGMII_NO_CLK 0x4
61 #define MII_M1145_HWCFG_MODE_MASK 0xf
62 #define MII_M1145_HWCFG_FIBER_COPPER_AUTO 0x8000
64 #define MII_M1145_HWCFG_MODE_SGMII_NO_CLK 0x4
65 #define MII_M1145_HWCFG_MODE_MASK 0xf
66 #define MII_M1145_HWCFG_FIBER_COPPER_AUTO 0x8000
68 #define MII_M1111_PHY_LED_CONTROL 0x18
69 #define MII_M1111_PHY_LED_DIRECT 0x4100
70 #define MII_M1111_PHY_LED_COMBINE 0x411c
71 #define MII_M1111_PHY_EXT_CR 0x14
72 #define MII_M1111_RX_DELAY 0x80
73 #define MII_M1111_TX_DELAY 0x2
74 #define MII_M1111_PHY_EXT_SR 0x1b
76 #define MII_M1111_HWCFG_MODE_MASK 0xf
77 #define MII_M1111_HWCFG_MODE_COPPER_RGMII 0xb
78 #define MII_M1111_HWCFG_MODE_FIBER_RGMII 0x3
79 #define MII_M1111_HWCFG_MODE_SGMII_NO_CLK 0x4
80 #define MII_M1111_HWCFG_MODE_COPPER_RTBI 0x9
81 #define MII_M1111_HWCFG_FIBER_COPPER_AUTO 0x8000
82 #define MII_M1111_HWCFG_FIBER_COPPER_RES 0x2000
84 #define MII_M1111_COPPER 0
85 #define MII_M1111_FIBER 1
87 #define MII_88E1121_PHY_MSCR_PAGE 2
88 #define MII_88E1121_PHY_MSCR_REG 21
89 #define MII_88E1121_PHY_MSCR_RX_DELAY BIT(5)
90 #define MII_88E1121_PHY_MSCR_TX_DELAY BIT(4)
91 #define MII_88E1121_PHY_MSCR_DELAY_MASK (~(0x3 << 4))
93 #define MII_88E1318S_PHY_MSCR1_REG 16
94 #define MII_88E1318S_PHY_MSCR1_PAD_ODD BIT(6)
96 /* Copper Specific Interrupt Enable Register */
97 #define MII_88E1318S_PHY_CSIER 0x12
98 /* WOL Event Interrupt Enable */
99 #define MII_88E1318S_PHY_CSIER_WOL_EIE BIT(7)
101 /* LED Timer Control Register */
102 #define MII_88E1318S_PHY_LED_PAGE 0x03
103 #define MII_88E1318S_PHY_LED_TCR 0x12
104 #define MII_88E1318S_PHY_LED_TCR_FORCE_INT BIT(15)
105 #define MII_88E1318S_PHY_LED_TCR_INTn_ENABLE BIT(7)
106 #define MII_88E1318S_PHY_LED_TCR_INT_ACTIVE_LOW BIT(11)
108 /* Magic Packet MAC address registers */
109 #define MII_88E1318S_PHY_MAGIC_PACKET_WORD2 0x17
110 #define MII_88E1318S_PHY_MAGIC_PACKET_WORD1 0x18
111 #define MII_88E1318S_PHY_MAGIC_PACKET_WORD0 0x19
113 #define MII_88E1318S_PHY_WOL_PAGE 0x11
114 #define MII_88E1318S_PHY_WOL_CTRL 0x10
115 #define MII_88E1318S_PHY_WOL_CTRL_CLEAR_WOL_STATUS BIT(12)
116 #define MII_88E1318S_PHY_WOL_CTRL_MAGIC_PACKET_MATCH_ENABLE BIT(14)
118 #define MII_88E1121_PHY_LED_CTRL 16
119 #define MII_88E1121_PHY_LED_PAGE 3
120 #define MII_88E1121_PHY_LED_DEF 0x0030
122 #define MII_M1011_PHY_STATUS 0x11
123 #define MII_M1011_PHY_STATUS_1000 0x8000
124 #define MII_M1011_PHY_STATUS_100 0x4000
125 #define MII_M1011_PHY_STATUS_SPD_MASK 0xc000
126 #define MII_M1011_PHY_STATUS_FULLDUPLEX 0x2000
127 #define MII_M1011_PHY_STATUS_RESOLVED 0x0800
128 #define MII_M1011_PHY_STATUS_LINK 0x0400
130 #define MII_M1116R_CONTROL_REG_MAC 21
132 #define MII_88E3016_PHY_SPEC_CTRL 0x10
133 #define MII_88E3016_DISABLE_SCRAMBLER 0x0200
134 #define MII_88E3016_AUTO_MDIX_CROSSOVER 0x0030
136 #define MII_88E1510_GEN_CTRL_REG_1 0x14
137 #define MII_88E1510_GEN_CTRL_REG_1_MODE_MASK 0x7
138 #define MII_88E1510_GEN_CTRL_REG_1_MODE_SGMII 0x1 /* SGMII to copper */
139 #define MII_88E1510_GEN_CTRL_REG_1_RESET 0x8000 /* Soft reset */
141 MODULE_DESCRIPTION("Marvell PHY driver");
142 MODULE_AUTHOR("Andy Fleming");
143 MODULE_LICENSE("GPL");
145 struct marvell_hw_stat
{
152 static struct marvell_hw_stat marvell_hw_stats
[] = {
153 { "phy_receive_errors", 0, 21, 16},
154 { "phy_idle_errors", 0, 10, 8 },
157 struct marvell_priv
{
158 u64 stats
[ARRAY_SIZE(marvell_hw_stats
)];
161 static int marvell_ack_interrupt(struct phy_device
*phydev
)
165 /* Clear the interrupts by reading the reg */
166 err
= phy_read(phydev
, MII_M1011_IEVENT
);
174 static int marvell_config_intr(struct phy_device
*phydev
)
178 if (phydev
->interrupts
== PHY_INTERRUPT_ENABLED
)
179 err
= phy_write(phydev
, MII_M1011_IMASK
, MII_M1011_IMASK_INIT
);
181 err
= phy_write(phydev
, MII_M1011_IMASK
, MII_M1011_IMASK_CLEAR
);
186 static int marvell_set_polarity(struct phy_device
*phydev
, int polarity
)
192 /* get the current settings */
193 reg
= phy_read(phydev
, MII_M1011_PHY_SCR
);
198 val
&= ~MII_M1011_PHY_SCR_AUTO_CROSS
;
201 val
|= MII_M1011_PHY_SCR_MDI
;
204 val
|= MII_M1011_PHY_SCR_MDI_X
;
206 case ETH_TP_MDI_AUTO
:
207 case ETH_TP_MDI_INVALID
:
209 val
|= MII_M1011_PHY_SCR_AUTO_CROSS
;
214 /* Set the new polarity value in the register */
215 err
= phy_write(phydev
, MII_M1011_PHY_SCR
, val
);
223 static int marvell_config_aneg(struct phy_device
*phydev
)
227 /* The Marvell PHY has an errata which requires
228 * that certain registers get written in order
229 * to restart autonegotiation */
230 err
= phy_write(phydev
, MII_BMCR
, BMCR_RESET
);
235 err
= phy_write(phydev
, 0x1d, 0x1f);
239 err
= phy_write(phydev
, 0x1e, 0x200c);
243 err
= phy_write(phydev
, 0x1d, 0x5);
247 err
= phy_write(phydev
, 0x1e, 0);
251 err
= phy_write(phydev
, 0x1e, 0x100);
255 err
= marvell_set_polarity(phydev
, phydev
->mdix
);
259 err
= phy_write(phydev
, MII_M1111_PHY_LED_CONTROL
,
260 MII_M1111_PHY_LED_DIRECT
);
264 err
= genphy_config_aneg(phydev
);
268 if (phydev
->autoneg
!= AUTONEG_ENABLE
) {
272 * A write to speed/duplex bits (that is performed by
273 * genphy_config_aneg() call above) must be followed by
274 * a software reset. Otherwise, the write has no effect.
276 bmcr
= phy_read(phydev
, MII_BMCR
);
280 err
= phy_write(phydev
, MII_BMCR
, bmcr
| BMCR_RESET
);
288 #ifdef CONFIG_OF_MDIO
290 * Set and/or override some configuration registers based on the
291 * marvell,reg-init property stored in the of_node for the phydev.
293 * marvell,reg-init = <reg-page reg mask value>,...;
295 * There may be one or more sets of <reg-page reg mask value>:
297 * reg-page: which register bank to use.
299 * mask: if non-zero, ANDed with existing register value.
300 * value: ORed with the masked value and written to the regiser.
303 static int marvell_of_reg_init(struct phy_device
*phydev
)
306 int len
, i
, saved_page
, current_page
, page_changed
, ret
;
308 if (!phydev
->mdio
.dev
.of_node
)
311 paddr
= of_get_property(phydev
->mdio
.dev
.of_node
,
312 "marvell,reg-init", &len
);
313 if (!paddr
|| len
< (4 * sizeof(*paddr
)))
316 saved_page
= phy_read(phydev
, MII_MARVELL_PHY_PAGE
);
320 current_page
= saved_page
;
323 len
/= sizeof(*paddr
);
324 for (i
= 0; i
< len
- 3; i
+= 4) {
325 u16 reg_page
= be32_to_cpup(paddr
+ i
);
326 u16 reg
= be32_to_cpup(paddr
+ i
+ 1);
327 u16 mask
= be32_to_cpup(paddr
+ i
+ 2);
328 u16 val_bits
= be32_to_cpup(paddr
+ i
+ 3);
331 if (reg_page
!= current_page
) {
332 current_page
= reg_page
;
334 ret
= phy_write(phydev
, MII_MARVELL_PHY_PAGE
, reg_page
);
341 val
= phy_read(phydev
, reg
);
350 ret
= phy_write(phydev
, reg
, val
);
357 i
= phy_write(phydev
, MII_MARVELL_PHY_PAGE
, saved_page
);
364 static int marvell_of_reg_init(struct phy_device
*phydev
)
368 #endif /* CONFIG_OF_MDIO */
370 static int m88e1121_config_aneg(struct phy_device
*phydev
)
372 int err
, oldpage
, mscr
;
374 oldpage
= phy_read(phydev
, MII_MARVELL_PHY_PAGE
);
376 err
= phy_write(phydev
, MII_MARVELL_PHY_PAGE
,
377 MII_88E1121_PHY_MSCR_PAGE
);
381 if (phy_interface_is_rgmii(phydev
)) {
383 mscr
= phy_read(phydev
, MII_88E1121_PHY_MSCR_REG
) &
384 MII_88E1121_PHY_MSCR_DELAY_MASK
;
386 if (phydev
->interface
== PHY_INTERFACE_MODE_RGMII_ID
)
387 mscr
|= (MII_88E1121_PHY_MSCR_RX_DELAY
|
388 MII_88E1121_PHY_MSCR_TX_DELAY
);
389 else if (phydev
->interface
== PHY_INTERFACE_MODE_RGMII_RXID
)
390 mscr
|= MII_88E1121_PHY_MSCR_RX_DELAY
;
391 else if (phydev
->interface
== PHY_INTERFACE_MODE_RGMII_TXID
)
392 mscr
|= MII_88E1121_PHY_MSCR_TX_DELAY
;
394 err
= phy_write(phydev
, MII_88E1121_PHY_MSCR_REG
, mscr
);
399 phy_write(phydev
, MII_MARVELL_PHY_PAGE
, oldpage
);
401 err
= phy_write(phydev
, MII_BMCR
, BMCR_RESET
);
405 err
= phy_write(phydev
, MII_M1011_PHY_SCR
,
406 MII_M1011_PHY_SCR_AUTO_CROSS
);
410 oldpage
= phy_read(phydev
, MII_MARVELL_PHY_PAGE
);
412 phy_write(phydev
, MII_MARVELL_PHY_PAGE
, MII_88E1121_PHY_LED_PAGE
);
413 phy_write(phydev
, MII_88E1121_PHY_LED_CTRL
, MII_88E1121_PHY_LED_DEF
);
414 phy_write(phydev
, MII_MARVELL_PHY_PAGE
, oldpage
);
416 err
= genphy_config_aneg(phydev
);
421 static int m88e1318_config_aneg(struct phy_device
*phydev
)
423 int err
, oldpage
, mscr
;
425 oldpage
= phy_read(phydev
, MII_MARVELL_PHY_PAGE
);
427 err
= phy_write(phydev
, MII_MARVELL_PHY_PAGE
,
428 MII_88E1121_PHY_MSCR_PAGE
);
432 mscr
= phy_read(phydev
, MII_88E1318S_PHY_MSCR1_REG
);
433 mscr
|= MII_88E1318S_PHY_MSCR1_PAD_ODD
;
435 err
= phy_write(phydev
, MII_88E1318S_PHY_MSCR1_REG
, mscr
);
439 err
= phy_write(phydev
, MII_MARVELL_PHY_PAGE
, oldpage
);
443 return m88e1121_config_aneg(phydev
);
446 static int m88e1510_config_aneg(struct phy_device
*phydev
)
450 err
= m88e1318_config_aneg(phydev
);
457 static int marvell_config_init(struct phy_device
*phydev
)
459 /* Set registers from marvell,reg-init DT property */
460 return marvell_of_reg_init(phydev
);
463 static int m88e1116r_config_init(struct phy_device
*phydev
)
468 temp
= phy_read(phydev
, MII_BMCR
);
470 err
= phy_write(phydev
, MII_BMCR
, temp
);
476 err
= phy_write(phydev
, MII_MARVELL_PHY_PAGE
, 0);
480 temp
= phy_read(phydev
, MII_M1011_PHY_SCR
);
481 temp
|= (7 << 12); /* max number of gigabit attempts */
482 temp
|= (1 << 11); /* enable downshift */
483 temp
|= MII_M1011_PHY_SCR_AUTO_CROSS
;
484 err
= phy_write(phydev
, MII_M1011_PHY_SCR
, temp
);
488 err
= phy_write(phydev
, MII_MARVELL_PHY_PAGE
, 2);
491 temp
= phy_read(phydev
, MII_M1116R_CONTROL_REG_MAC
);
494 err
= phy_write(phydev
, MII_M1116R_CONTROL_REG_MAC
, temp
);
497 err
= phy_write(phydev
, MII_MARVELL_PHY_PAGE
, 0);
501 temp
= phy_read(phydev
, MII_BMCR
);
503 err
= phy_write(phydev
, MII_BMCR
, temp
);
509 return marvell_config_init(phydev
);
512 static int m88e3016_config_init(struct phy_device
*phydev
)
516 /* Enable Scrambler and Auto-Crossover */
517 reg
= phy_read(phydev
, MII_88E3016_PHY_SPEC_CTRL
);
521 reg
&= ~MII_88E3016_DISABLE_SCRAMBLER
;
522 reg
|= MII_88E3016_AUTO_MDIX_CROSSOVER
;
524 reg
= phy_write(phydev
, MII_88E3016_PHY_SPEC_CTRL
, reg
);
528 return marvell_config_init(phydev
);
531 static int m88e1111_config_init(struct phy_device
*phydev
)
536 if (phy_interface_is_rgmii(phydev
)) {
538 temp
= phy_read(phydev
, MII_M1111_PHY_EXT_CR
);
542 if (phydev
->interface
== PHY_INTERFACE_MODE_RGMII_ID
) {
543 temp
|= (MII_M1111_RX_DELAY
| MII_M1111_TX_DELAY
);
544 } else if (phydev
->interface
== PHY_INTERFACE_MODE_RGMII_RXID
) {
545 temp
&= ~MII_M1111_TX_DELAY
;
546 temp
|= MII_M1111_RX_DELAY
;
547 } else if (phydev
->interface
== PHY_INTERFACE_MODE_RGMII_TXID
) {
548 temp
&= ~MII_M1111_RX_DELAY
;
549 temp
|= MII_M1111_TX_DELAY
;
552 err
= phy_write(phydev
, MII_M1111_PHY_EXT_CR
, temp
);
556 temp
= phy_read(phydev
, MII_M1111_PHY_EXT_SR
);
560 temp
&= ~(MII_M1111_HWCFG_MODE_MASK
);
562 if (temp
& MII_M1111_HWCFG_FIBER_COPPER_RES
)
563 temp
|= MII_M1111_HWCFG_MODE_FIBER_RGMII
;
565 temp
|= MII_M1111_HWCFG_MODE_COPPER_RGMII
;
567 err
= phy_write(phydev
, MII_M1111_PHY_EXT_SR
, temp
);
572 if (phydev
->interface
== PHY_INTERFACE_MODE_SGMII
) {
573 temp
= phy_read(phydev
, MII_M1111_PHY_EXT_SR
);
577 temp
&= ~(MII_M1111_HWCFG_MODE_MASK
);
578 temp
|= MII_M1111_HWCFG_MODE_SGMII_NO_CLK
;
579 temp
|= MII_M1111_HWCFG_FIBER_COPPER_AUTO
;
581 err
= phy_write(phydev
, MII_M1111_PHY_EXT_SR
, temp
);
585 /* make sure copper is selected */
586 err
= phy_read(phydev
, MII_M1145_PHY_EXT_ADDR_PAGE
);
590 err
= phy_write(phydev
, MII_M1145_PHY_EXT_ADDR_PAGE
,
596 if (phydev
->interface
== PHY_INTERFACE_MODE_RTBI
) {
597 temp
= phy_read(phydev
, MII_M1111_PHY_EXT_CR
);
600 temp
|= (MII_M1111_RX_DELAY
| MII_M1111_TX_DELAY
);
601 err
= phy_write(phydev
, MII_M1111_PHY_EXT_CR
, temp
);
605 temp
= phy_read(phydev
, MII_M1111_PHY_EXT_SR
);
608 temp
&= ~(MII_M1111_HWCFG_MODE_MASK
| MII_M1111_HWCFG_FIBER_COPPER_RES
);
609 temp
|= 0x7 | MII_M1111_HWCFG_FIBER_COPPER_AUTO
;
610 err
= phy_write(phydev
, MII_M1111_PHY_EXT_SR
, temp
);
615 err
= phy_write(phydev
, MII_BMCR
, BMCR_RESET
);
619 temp
= phy_read(phydev
, MII_BMCR
);
620 while (temp
& BMCR_RESET
);
622 temp
= phy_read(phydev
, MII_M1111_PHY_EXT_SR
);
625 temp
&= ~(MII_M1111_HWCFG_MODE_MASK
| MII_M1111_HWCFG_FIBER_COPPER_RES
);
626 temp
|= MII_M1111_HWCFG_MODE_COPPER_RTBI
| MII_M1111_HWCFG_FIBER_COPPER_AUTO
;
627 err
= phy_write(phydev
, MII_M1111_PHY_EXT_SR
, temp
);
632 err
= marvell_of_reg_init(phydev
);
636 return phy_write(phydev
, MII_BMCR
, BMCR_RESET
);
639 static int m88e1510_config_init(struct phy_device
*phydev
)
644 /* SGMII-to-Copper mode initialization */
645 if (phydev
->interface
== PHY_INTERFACE_MODE_SGMII
) {
647 err
= phy_write(phydev
, MII_MARVELL_PHY_PAGE
, 18);
651 /* In reg 20, write MODE[2:0] = 0x1 (SGMII to Copper) */
652 temp
= phy_read(phydev
, MII_88E1510_GEN_CTRL_REG_1
);
653 temp
&= ~MII_88E1510_GEN_CTRL_REG_1_MODE_MASK
;
654 temp
|= MII_88E1510_GEN_CTRL_REG_1_MODE_SGMII
;
655 err
= phy_write(phydev
, MII_88E1510_GEN_CTRL_REG_1
, temp
);
659 /* PHY reset is necessary after changing MODE[2:0] */
660 temp
|= MII_88E1510_GEN_CTRL_REG_1_RESET
;
661 err
= phy_write(phydev
, MII_88E1510_GEN_CTRL_REG_1
, temp
);
665 /* Reset page selection */
666 err
= phy_write(phydev
, MII_MARVELL_PHY_PAGE
, 0);
671 return marvell_config_init(phydev
);
674 static int m88e1118_config_aneg(struct phy_device
*phydev
)
678 err
= phy_write(phydev
, MII_BMCR
, BMCR_RESET
);
682 err
= phy_write(phydev
, MII_M1011_PHY_SCR
,
683 MII_M1011_PHY_SCR_AUTO_CROSS
);
687 err
= genphy_config_aneg(phydev
);
691 static int m88e1118_config_init(struct phy_device
*phydev
)
696 err
= phy_write(phydev
, MII_MARVELL_PHY_PAGE
, 0x0002);
700 /* Enable 1000 Mbit */
701 err
= phy_write(phydev
, 0x15, 0x1070);
706 err
= phy_write(phydev
, MII_MARVELL_PHY_PAGE
, 0x0003);
710 /* Adjust LED Control */
711 if (phydev
->dev_flags
& MARVELL_PHY_M1118_DNS323_LEDS
)
712 err
= phy_write(phydev
, 0x10, 0x1100);
714 err
= phy_write(phydev
, 0x10, 0x021e);
718 err
= marvell_of_reg_init(phydev
);
723 err
= phy_write(phydev
, MII_MARVELL_PHY_PAGE
, 0x0);
727 return phy_write(phydev
, MII_BMCR
, BMCR_RESET
);
730 static int m88e1149_config_init(struct phy_device
*phydev
)
735 err
= phy_write(phydev
, MII_MARVELL_PHY_PAGE
, 0x0002);
739 /* Enable 1000 Mbit */
740 err
= phy_write(phydev
, 0x15, 0x1048);
744 err
= marvell_of_reg_init(phydev
);
749 err
= phy_write(phydev
, MII_MARVELL_PHY_PAGE
, 0x0);
753 return phy_write(phydev
, MII_BMCR
, BMCR_RESET
);
756 static int m88e1145_config_init(struct phy_device
*phydev
)
761 /* Take care of errata E0 & E1 */
762 err
= phy_write(phydev
, 0x1d, 0x001b);
766 err
= phy_write(phydev
, 0x1e, 0x418f);
770 err
= phy_write(phydev
, 0x1d, 0x0016);
774 err
= phy_write(phydev
, 0x1e, 0xa2da);
778 if (phydev
->interface
== PHY_INTERFACE_MODE_RGMII_ID
) {
779 int temp
= phy_read(phydev
, MII_M1145_PHY_EXT_CR
);
783 temp
|= (MII_M1145_RGMII_RX_DELAY
| MII_M1145_RGMII_TX_DELAY
);
785 err
= phy_write(phydev
, MII_M1145_PHY_EXT_CR
, temp
);
789 if (phydev
->dev_flags
& MARVELL_PHY_M1145_FLAGS_RESISTANCE
) {
790 err
= phy_write(phydev
, 0x1d, 0x0012);
794 temp
= phy_read(phydev
, 0x1e);
799 temp
|= 2 << 9; /* 36 ohm */
800 temp
|= 2 << 6; /* 39 ohm */
802 err
= phy_write(phydev
, 0x1e, temp
);
806 err
= phy_write(phydev
, 0x1d, 0x3);
810 err
= phy_write(phydev
, 0x1e, 0x8000);
816 if (phydev
->interface
== PHY_INTERFACE_MODE_SGMII
) {
817 temp
= phy_read(phydev
, MII_M1145_PHY_EXT_SR
);
821 temp
&= ~MII_M1145_HWCFG_MODE_MASK
;
822 temp
|= MII_M1145_HWCFG_MODE_SGMII_NO_CLK
;
823 temp
|= MII_M1145_HWCFG_FIBER_COPPER_AUTO
;
825 err
= phy_write(phydev
, MII_M1145_PHY_EXT_SR
, temp
);
830 err
= marvell_of_reg_init(phydev
);
837 /* marvell_read_status
839 * Generic status code does not detect Fiber correctly!
841 * Check the link, then figure out the current state
842 * by comparing what we advertise with what the link partner
843 * advertises. Start by checking the gigabit possibilities,
844 * then move on to 10/100.
846 static int marvell_read_status(struct phy_device
*phydev
)
854 /* Update the link, but return if there
856 err
= genphy_update_link(phydev
);
860 if (AUTONEG_ENABLE
== phydev
->autoneg
) {
861 status
= phy_read(phydev
, MII_M1011_PHY_STATUS
);
865 lpa
= phy_read(phydev
, MII_LPA
);
869 lpagb
= phy_read(phydev
, MII_STAT1000
);
873 adv
= phy_read(phydev
, MII_ADVERTISE
);
877 phydev
->lp_advertising
= mii_stat1000_to_ethtool_lpa_t(lpagb
) |
878 mii_lpa_to_ethtool_lpa_t(lpa
);
882 if (status
& MII_M1011_PHY_STATUS_FULLDUPLEX
)
883 phydev
->duplex
= DUPLEX_FULL
;
885 phydev
->duplex
= DUPLEX_HALF
;
887 status
= status
& MII_M1011_PHY_STATUS_SPD_MASK
;
888 phydev
->pause
= phydev
->asym_pause
= 0;
891 case MII_M1011_PHY_STATUS_1000
:
892 phydev
->speed
= SPEED_1000
;
895 case MII_M1011_PHY_STATUS_100
:
896 phydev
->speed
= SPEED_100
;
900 phydev
->speed
= SPEED_10
;
904 if (phydev
->duplex
== DUPLEX_FULL
) {
905 phydev
->pause
= lpa
& LPA_PAUSE_CAP
? 1 : 0;
906 phydev
->asym_pause
= lpa
& LPA_PAUSE_ASYM
? 1 : 0;
909 int bmcr
= phy_read(phydev
, MII_BMCR
);
914 if (bmcr
& BMCR_FULLDPLX
)
915 phydev
->duplex
= DUPLEX_FULL
;
917 phydev
->duplex
= DUPLEX_HALF
;
919 if (bmcr
& BMCR_SPEED1000
)
920 phydev
->speed
= SPEED_1000
;
921 else if (bmcr
& BMCR_SPEED100
)
922 phydev
->speed
= SPEED_100
;
924 phydev
->speed
= SPEED_10
;
926 phydev
->pause
= phydev
->asym_pause
= 0;
927 phydev
->lp_advertising
= 0;
933 static int marvell_aneg_done(struct phy_device
*phydev
)
935 int retval
= phy_read(phydev
, MII_M1011_PHY_STATUS
);
936 return (retval
< 0) ? retval
: (retval
& MII_M1011_PHY_STATUS_RESOLVED
);
939 static int m88e1121_did_interrupt(struct phy_device
*phydev
)
943 imask
= phy_read(phydev
, MII_M1011_IEVENT
);
945 if (imask
& MII_M1011_IMASK_INIT
)
951 static void m88e1318_get_wol(struct phy_device
*phydev
, struct ethtool_wolinfo
*wol
)
953 wol
->supported
= WAKE_MAGIC
;
956 if (phy_write(phydev
, MII_MARVELL_PHY_PAGE
,
957 MII_88E1318S_PHY_WOL_PAGE
) < 0)
960 if (phy_read(phydev
, MII_88E1318S_PHY_WOL_CTRL
) &
961 MII_88E1318S_PHY_WOL_CTRL_MAGIC_PACKET_MATCH_ENABLE
)
962 wol
->wolopts
|= WAKE_MAGIC
;
964 if (phy_write(phydev
, MII_MARVELL_PHY_PAGE
, 0x00) < 0)
968 static int m88e1318_set_wol(struct phy_device
*phydev
, struct ethtool_wolinfo
*wol
)
970 int err
, oldpage
, temp
;
972 oldpage
= phy_read(phydev
, MII_MARVELL_PHY_PAGE
);
974 if (wol
->wolopts
& WAKE_MAGIC
) {
975 /* Explicitly switch to page 0x00, just to be sure */
976 err
= phy_write(phydev
, MII_MARVELL_PHY_PAGE
, 0x00);
980 /* Enable the WOL interrupt */
981 temp
= phy_read(phydev
, MII_88E1318S_PHY_CSIER
);
982 temp
|= MII_88E1318S_PHY_CSIER_WOL_EIE
;
983 err
= phy_write(phydev
, MII_88E1318S_PHY_CSIER
, temp
);
987 err
= phy_write(phydev
, MII_MARVELL_PHY_PAGE
,
988 MII_88E1318S_PHY_LED_PAGE
);
992 /* Setup LED[2] as interrupt pin (active low) */
993 temp
= phy_read(phydev
, MII_88E1318S_PHY_LED_TCR
);
994 temp
&= ~MII_88E1318S_PHY_LED_TCR_FORCE_INT
;
995 temp
|= MII_88E1318S_PHY_LED_TCR_INTn_ENABLE
;
996 temp
|= MII_88E1318S_PHY_LED_TCR_INT_ACTIVE_LOW
;
997 err
= phy_write(phydev
, MII_88E1318S_PHY_LED_TCR
, temp
);
1001 err
= phy_write(phydev
, MII_MARVELL_PHY_PAGE
,
1002 MII_88E1318S_PHY_WOL_PAGE
);
1006 /* Store the device address for the magic packet */
1007 err
= phy_write(phydev
, MII_88E1318S_PHY_MAGIC_PACKET_WORD2
,
1008 ((phydev
->attached_dev
->dev_addr
[5] << 8) |
1009 phydev
->attached_dev
->dev_addr
[4]));
1012 err
= phy_write(phydev
, MII_88E1318S_PHY_MAGIC_PACKET_WORD1
,
1013 ((phydev
->attached_dev
->dev_addr
[3] << 8) |
1014 phydev
->attached_dev
->dev_addr
[2]));
1017 err
= phy_write(phydev
, MII_88E1318S_PHY_MAGIC_PACKET_WORD0
,
1018 ((phydev
->attached_dev
->dev_addr
[1] << 8) |
1019 phydev
->attached_dev
->dev_addr
[0]));
1023 /* Clear WOL status and enable magic packet matching */
1024 temp
= phy_read(phydev
, MII_88E1318S_PHY_WOL_CTRL
);
1025 temp
|= MII_88E1318S_PHY_WOL_CTRL_CLEAR_WOL_STATUS
;
1026 temp
|= MII_88E1318S_PHY_WOL_CTRL_MAGIC_PACKET_MATCH_ENABLE
;
1027 err
= phy_write(phydev
, MII_88E1318S_PHY_WOL_CTRL
, temp
);
1031 err
= phy_write(phydev
, MII_MARVELL_PHY_PAGE
,
1032 MII_88E1318S_PHY_WOL_PAGE
);
1036 /* Clear WOL status and disable magic packet matching */
1037 temp
= phy_read(phydev
, MII_88E1318S_PHY_WOL_CTRL
);
1038 temp
|= MII_88E1318S_PHY_WOL_CTRL_CLEAR_WOL_STATUS
;
1039 temp
&= ~MII_88E1318S_PHY_WOL_CTRL_MAGIC_PACKET_MATCH_ENABLE
;
1040 err
= phy_write(phydev
, MII_88E1318S_PHY_WOL_CTRL
, temp
);
1045 err
= phy_write(phydev
, MII_MARVELL_PHY_PAGE
, oldpage
);
1052 static int marvell_get_sset_count(struct phy_device
*phydev
)
1054 return ARRAY_SIZE(marvell_hw_stats
);
1057 static void marvell_get_strings(struct phy_device
*phydev
, u8
*data
)
1061 for (i
= 0; i
< ARRAY_SIZE(marvell_hw_stats
); i
++) {
1062 memcpy(data
+ i
* ETH_GSTRING_LEN
,
1063 marvell_hw_stats
[i
].string
, ETH_GSTRING_LEN
);
1068 #define UINT64_MAX (u64)(~((u64)0))
1070 static u64
marvell_get_stat(struct phy_device
*phydev
, int i
)
1072 struct marvell_hw_stat stat
= marvell_hw_stats
[i
];
1073 struct marvell_priv
*priv
= phydev
->priv
;
1074 int err
, oldpage
, val
;
1077 oldpage
= phy_read(phydev
, MII_MARVELL_PHY_PAGE
);
1078 err
= phy_write(phydev
, MII_MARVELL_PHY_PAGE
,
1083 val
= phy_read(phydev
, stat
.reg
);
1087 val
= val
& ((1 << stat
.bits
) - 1);
1088 priv
->stats
[i
] += val
;
1089 ret
= priv
->stats
[i
];
1092 phy_write(phydev
, MII_MARVELL_PHY_PAGE
, oldpage
);
1097 static void marvell_get_stats(struct phy_device
*phydev
,
1098 struct ethtool_stats
*stats
, u64
*data
)
1102 for (i
= 0; i
< ARRAY_SIZE(marvell_hw_stats
); i
++)
1103 data
[i
] = marvell_get_stat(phydev
, i
);
1106 static int marvell_probe(struct phy_device
*phydev
)
1108 struct marvell_priv
*priv
;
1110 priv
= devm_kzalloc(&phydev
->mdio
.dev
, sizeof(*priv
), GFP_KERNEL
);
1114 phydev
->priv
= priv
;
1119 static struct phy_driver marvell_drivers
[] = {
1121 .phy_id
= MARVELL_PHY_ID_88E1101
,
1122 .phy_id_mask
= MARVELL_PHY_ID_MASK
,
1123 .name
= "Marvell 88E1101",
1124 .features
= PHY_GBIT_FEATURES
,
1125 .probe
= marvell_probe
,
1126 .flags
= PHY_HAS_INTERRUPT
,
1127 .config_init
= &marvell_config_init
,
1128 .config_aneg
= &marvell_config_aneg
,
1129 .read_status
= &genphy_read_status
,
1130 .ack_interrupt
= &marvell_ack_interrupt
,
1131 .config_intr
= &marvell_config_intr
,
1132 .resume
= &genphy_resume
,
1133 .suspend
= &genphy_suspend
,
1134 .get_sset_count
= marvell_get_sset_count
,
1135 .get_strings
= marvell_get_strings
,
1136 .get_stats
= marvell_get_stats
,
1139 .phy_id
= MARVELL_PHY_ID_88E1112
,
1140 .phy_id_mask
= MARVELL_PHY_ID_MASK
,
1141 .name
= "Marvell 88E1112",
1142 .features
= PHY_GBIT_FEATURES
,
1143 .flags
= PHY_HAS_INTERRUPT
,
1144 .probe
= marvell_probe
,
1145 .config_init
= &m88e1111_config_init
,
1146 .config_aneg
= &marvell_config_aneg
,
1147 .read_status
= &genphy_read_status
,
1148 .ack_interrupt
= &marvell_ack_interrupt
,
1149 .config_intr
= &marvell_config_intr
,
1150 .resume
= &genphy_resume
,
1151 .suspend
= &genphy_suspend
,
1152 .get_sset_count
= marvell_get_sset_count
,
1153 .get_strings
= marvell_get_strings
,
1154 .get_stats
= marvell_get_stats
,
1157 .phy_id
= MARVELL_PHY_ID_88E1111
,
1158 .phy_id_mask
= MARVELL_PHY_ID_MASK
,
1159 .name
= "Marvell 88E1111",
1160 .features
= PHY_GBIT_FEATURES
,
1161 .flags
= PHY_HAS_INTERRUPT
,
1162 .probe
= marvell_probe
,
1163 .config_init
= &m88e1111_config_init
,
1164 .config_aneg
= &marvell_config_aneg
,
1165 .read_status
= &marvell_read_status
,
1166 .ack_interrupt
= &marvell_ack_interrupt
,
1167 .config_intr
= &marvell_config_intr
,
1168 .resume
= &genphy_resume
,
1169 .suspend
= &genphy_suspend
,
1170 .get_sset_count
= marvell_get_sset_count
,
1171 .get_strings
= marvell_get_strings
,
1172 .get_stats
= marvell_get_stats
,
1175 .phy_id
= MARVELL_PHY_ID_88E1118
,
1176 .phy_id_mask
= MARVELL_PHY_ID_MASK
,
1177 .name
= "Marvell 88E1118",
1178 .features
= PHY_GBIT_FEATURES
,
1179 .flags
= PHY_HAS_INTERRUPT
,
1180 .probe
= marvell_probe
,
1181 .config_init
= &m88e1118_config_init
,
1182 .config_aneg
= &m88e1118_config_aneg
,
1183 .read_status
= &genphy_read_status
,
1184 .ack_interrupt
= &marvell_ack_interrupt
,
1185 .config_intr
= &marvell_config_intr
,
1186 .resume
= &genphy_resume
,
1187 .suspend
= &genphy_suspend
,
1188 .get_sset_count
= marvell_get_sset_count
,
1189 .get_strings
= marvell_get_strings
,
1190 .get_stats
= marvell_get_stats
,
1193 .phy_id
= MARVELL_PHY_ID_88E1121R
,
1194 .phy_id_mask
= MARVELL_PHY_ID_MASK
,
1195 .name
= "Marvell 88E1121R",
1196 .features
= PHY_GBIT_FEATURES
,
1197 .flags
= PHY_HAS_INTERRUPT
,
1198 .probe
= marvell_probe
,
1199 .config_init
= &marvell_config_init
,
1200 .config_aneg
= &m88e1121_config_aneg
,
1201 .read_status
= &marvell_read_status
,
1202 .ack_interrupt
= &marvell_ack_interrupt
,
1203 .config_intr
= &marvell_config_intr
,
1204 .did_interrupt
= &m88e1121_did_interrupt
,
1205 .resume
= &genphy_resume
,
1206 .suspend
= &genphy_suspend
,
1207 .get_sset_count
= marvell_get_sset_count
,
1208 .get_strings
= marvell_get_strings
,
1209 .get_stats
= marvell_get_stats
,
1212 .phy_id
= MARVELL_PHY_ID_88E1318S
,
1213 .phy_id_mask
= MARVELL_PHY_ID_MASK
,
1214 .name
= "Marvell 88E1318S",
1215 .features
= PHY_GBIT_FEATURES
,
1216 .flags
= PHY_HAS_INTERRUPT
,
1217 .probe
= marvell_probe
,
1218 .config_init
= &marvell_config_init
,
1219 .config_aneg
= &m88e1318_config_aneg
,
1220 .read_status
= &marvell_read_status
,
1221 .ack_interrupt
= &marvell_ack_interrupt
,
1222 .config_intr
= &marvell_config_intr
,
1223 .did_interrupt
= &m88e1121_did_interrupt
,
1224 .get_wol
= &m88e1318_get_wol
,
1225 .set_wol
= &m88e1318_set_wol
,
1226 .resume
= &genphy_resume
,
1227 .suspend
= &genphy_suspend
,
1228 .get_sset_count
= marvell_get_sset_count
,
1229 .get_strings
= marvell_get_strings
,
1230 .get_stats
= marvell_get_stats
,
1233 .phy_id
= MARVELL_PHY_ID_88E1145
,
1234 .phy_id_mask
= MARVELL_PHY_ID_MASK
,
1235 .name
= "Marvell 88E1145",
1236 .features
= PHY_GBIT_FEATURES
,
1237 .flags
= PHY_HAS_INTERRUPT
,
1238 .probe
= marvell_probe
,
1239 .config_init
= &m88e1145_config_init
,
1240 .config_aneg
= &marvell_config_aneg
,
1241 .read_status
= &genphy_read_status
,
1242 .ack_interrupt
= &marvell_ack_interrupt
,
1243 .config_intr
= &marvell_config_intr
,
1244 .resume
= &genphy_resume
,
1245 .suspend
= &genphy_suspend
,
1246 .get_sset_count
= marvell_get_sset_count
,
1247 .get_strings
= marvell_get_strings
,
1248 .get_stats
= marvell_get_stats
,
1251 .phy_id
= MARVELL_PHY_ID_88E1149R
,
1252 .phy_id_mask
= MARVELL_PHY_ID_MASK
,
1253 .name
= "Marvell 88E1149R",
1254 .features
= PHY_GBIT_FEATURES
,
1255 .flags
= PHY_HAS_INTERRUPT
,
1256 .probe
= marvell_probe
,
1257 .config_init
= &m88e1149_config_init
,
1258 .config_aneg
= &m88e1118_config_aneg
,
1259 .read_status
= &genphy_read_status
,
1260 .ack_interrupt
= &marvell_ack_interrupt
,
1261 .config_intr
= &marvell_config_intr
,
1262 .resume
= &genphy_resume
,
1263 .suspend
= &genphy_suspend
,
1264 .get_sset_count
= marvell_get_sset_count
,
1265 .get_strings
= marvell_get_strings
,
1266 .get_stats
= marvell_get_stats
,
1269 .phy_id
= MARVELL_PHY_ID_88E1240
,
1270 .phy_id_mask
= MARVELL_PHY_ID_MASK
,
1271 .name
= "Marvell 88E1240",
1272 .features
= PHY_GBIT_FEATURES
,
1273 .flags
= PHY_HAS_INTERRUPT
,
1274 .probe
= marvell_probe
,
1275 .config_init
= &m88e1111_config_init
,
1276 .config_aneg
= &marvell_config_aneg
,
1277 .read_status
= &genphy_read_status
,
1278 .ack_interrupt
= &marvell_ack_interrupt
,
1279 .config_intr
= &marvell_config_intr
,
1280 .resume
= &genphy_resume
,
1281 .suspend
= &genphy_suspend
,
1282 .get_sset_count
= marvell_get_sset_count
,
1283 .get_strings
= marvell_get_strings
,
1284 .get_stats
= marvell_get_stats
,
1287 .phy_id
= MARVELL_PHY_ID_88E1116R
,
1288 .phy_id_mask
= MARVELL_PHY_ID_MASK
,
1289 .name
= "Marvell 88E1116R",
1290 .features
= PHY_GBIT_FEATURES
,
1291 .flags
= PHY_HAS_INTERRUPT
,
1292 .probe
= marvell_probe
,
1293 .config_init
= &m88e1116r_config_init
,
1294 .config_aneg
= &genphy_config_aneg
,
1295 .read_status
= &genphy_read_status
,
1296 .ack_interrupt
= &marvell_ack_interrupt
,
1297 .config_intr
= &marvell_config_intr
,
1298 .resume
= &genphy_resume
,
1299 .suspend
= &genphy_suspend
,
1300 .get_sset_count
= marvell_get_sset_count
,
1301 .get_strings
= marvell_get_strings
,
1302 .get_stats
= marvell_get_stats
,
1305 .phy_id
= MARVELL_PHY_ID_88E1510
,
1306 .phy_id_mask
= MARVELL_PHY_ID_MASK
,
1307 .name
= "Marvell 88E1510",
1308 .features
= PHY_GBIT_FEATURES
,
1309 .flags
= PHY_HAS_INTERRUPT
,
1310 .probe
= marvell_probe
,
1311 .config_init
= &m88e1510_config_init
,
1312 .config_aneg
= &m88e1510_config_aneg
,
1313 .read_status
= &marvell_read_status
,
1314 .ack_interrupt
= &marvell_ack_interrupt
,
1315 .config_intr
= &marvell_config_intr
,
1316 .did_interrupt
= &m88e1121_did_interrupt
,
1317 .resume
= &genphy_resume
,
1318 .suspend
= &genphy_suspend
,
1319 .get_sset_count
= marvell_get_sset_count
,
1320 .get_strings
= marvell_get_strings
,
1321 .get_stats
= marvell_get_stats
,
1324 .phy_id
= MARVELL_PHY_ID_88E1540
,
1325 .phy_id_mask
= MARVELL_PHY_ID_MASK
,
1326 .name
= "Marvell 88E1540",
1327 .features
= PHY_GBIT_FEATURES
,
1328 .flags
= PHY_HAS_INTERRUPT
,
1329 .probe
= marvell_probe
,
1330 .config_init
= &marvell_config_init
,
1331 .config_aneg
= &m88e1510_config_aneg
,
1332 .read_status
= &marvell_read_status
,
1333 .ack_interrupt
= &marvell_ack_interrupt
,
1334 .config_intr
= &marvell_config_intr
,
1335 .did_interrupt
= &m88e1121_did_interrupt
,
1336 .resume
= &genphy_resume
,
1337 .suspend
= &genphy_suspend
,
1338 .get_sset_count
= marvell_get_sset_count
,
1339 .get_strings
= marvell_get_strings
,
1340 .get_stats
= marvell_get_stats
,
1343 .phy_id
= MARVELL_PHY_ID_88E3016
,
1344 .phy_id_mask
= MARVELL_PHY_ID_MASK
,
1345 .name
= "Marvell 88E3016",
1346 .features
= PHY_BASIC_FEATURES
,
1347 .flags
= PHY_HAS_INTERRUPT
,
1348 .probe
= marvell_probe
,
1349 .config_aneg
= &genphy_config_aneg
,
1350 .config_init
= &m88e3016_config_init
,
1351 .aneg_done
= &marvell_aneg_done
,
1352 .read_status
= &marvell_read_status
,
1353 .ack_interrupt
= &marvell_ack_interrupt
,
1354 .config_intr
= &marvell_config_intr
,
1355 .did_interrupt
= &m88e1121_did_interrupt
,
1356 .resume
= &genphy_resume
,
1357 .suspend
= &genphy_suspend
,
1358 .get_sset_count
= marvell_get_sset_count
,
1359 .get_strings
= marvell_get_strings
,
1360 .get_stats
= marvell_get_stats
,
1364 module_phy_driver(marvell_drivers
);
1366 static struct mdio_device_id __maybe_unused marvell_tbl
[] = {
1367 { MARVELL_PHY_ID_88E1101
, MARVELL_PHY_ID_MASK
},
1368 { MARVELL_PHY_ID_88E1112
, MARVELL_PHY_ID_MASK
},
1369 { MARVELL_PHY_ID_88E1111
, MARVELL_PHY_ID_MASK
},
1370 { MARVELL_PHY_ID_88E1118
, MARVELL_PHY_ID_MASK
},
1371 { MARVELL_PHY_ID_88E1121R
, MARVELL_PHY_ID_MASK
},
1372 { MARVELL_PHY_ID_88E1145
, MARVELL_PHY_ID_MASK
},
1373 { MARVELL_PHY_ID_88E1149R
, MARVELL_PHY_ID_MASK
},
1374 { MARVELL_PHY_ID_88E1240
, MARVELL_PHY_ID_MASK
},
1375 { MARVELL_PHY_ID_88E1318S
, MARVELL_PHY_ID_MASK
},
1376 { MARVELL_PHY_ID_88E1116R
, MARVELL_PHY_ID_MASK
},
1377 { MARVELL_PHY_ID_88E1510
, MARVELL_PHY_ID_MASK
},
1378 { MARVELL_PHY_ID_88E1540
, MARVELL_PHY_ID_MASK
},
1379 { MARVELL_PHY_ID_88E3016
, MARVELL_PHY_ID_MASK
},
1383 MODULE_DEVICE_TABLE(mdio
, marvell_tbl
);