2 * drivers/net/phy/marvell.c
4 * Driver for Marvell PHYs
8 * Copyright (c) 2004 Freescale Semiconductor, Inc.
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the
12 * Free Software Foundation; either version 2 of the License, or (at your
13 * option) any later version.
16 #include <linux/kernel.h>
17 #include <linux/string.h>
18 #include <linux/errno.h>
19 #include <linux/unistd.h>
20 #include <linux/interrupt.h>
21 #include <linux/init.h>
22 #include <linux/delay.h>
23 #include <linux/netdevice.h>
24 #include <linux/etherdevice.h>
25 #include <linux/skbuff.h>
26 #include <linux/spinlock.h>
28 #include <linux/module.h>
29 #include <linux/mii.h>
30 #include <linux/ethtool.h>
31 #include <linux/phy.h>
32 #include <linux/marvell_phy.h>
36 #include <asm/uaccess.h>
38 #define MII_M1011_IEVENT 0x13
39 #define MII_M1011_IEVENT_CLEAR 0x0000
41 #define MII_M1011_IMASK 0x12
42 #define MII_M1011_IMASK_INIT 0x6400
43 #define MII_M1011_IMASK_CLEAR 0x0000
45 #define MII_M1011_PHY_SCR 0x10
46 #define MII_M1011_PHY_SCR_AUTO_CROSS 0x0060
48 #define MII_M1145_PHY_EXT_CR 0x14
49 #define MII_M1145_RGMII_RX_DELAY 0x0080
50 #define MII_M1145_RGMII_TX_DELAY 0x0002
52 #define MII_M1111_PHY_LED_CONTROL 0x18
53 #define MII_M1111_PHY_LED_DIRECT 0x4100
54 #define MII_M1111_PHY_LED_COMBINE 0x411c
55 #define MII_M1111_PHY_EXT_CR 0x14
56 #define MII_M1111_RX_DELAY 0x80
57 #define MII_M1111_TX_DELAY 0x2
58 #define MII_M1111_PHY_EXT_SR 0x1b
60 #define MII_M1111_HWCFG_MODE_MASK 0xf
61 #define MII_M1111_HWCFG_MODE_COPPER_RGMII 0xb
62 #define MII_M1111_HWCFG_MODE_FIBER_RGMII 0x3
63 #define MII_M1111_HWCFG_MODE_SGMII_NO_CLK 0x4
64 #define MII_M1111_HWCFG_MODE_COPPER_RTBI 0x9
65 #define MII_M1111_HWCFG_FIBER_COPPER_AUTO 0x8000
66 #define MII_M1111_HWCFG_FIBER_COPPER_RES 0x2000
68 #define MII_M1111_COPPER 0
69 #define MII_M1111_FIBER 1
71 #define MII_88E1121_PHY_MSCR_PAGE 2
72 #define MII_88E1121_PHY_MSCR_REG 21
73 #define MII_88E1121_PHY_MSCR_RX_DELAY BIT(5)
74 #define MII_88E1121_PHY_MSCR_TX_DELAY BIT(4)
75 #define MII_88E1121_PHY_MSCR_DELAY_MASK (~(0x3 << 4))
77 #define MII_88EC048_PHY_MSCR1_REG 16
78 #define MII_88EC048_PHY_MSCR1_PAD_ODD BIT(6)
80 #define MII_88E1121_PHY_LED_CTRL 16
81 #define MII_88E1121_PHY_LED_PAGE 3
82 #define MII_88E1121_PHY_LED_DEF 0x0030
83 #define MII_88E1121_PHY_PAGE 22
85 #define MII_M1011_PHY_STATUS 0x11
86 #define MII_M1011_PHY_STATUS_1000 0x8000
87 #define MII_M1011_PHY_STATUS_100 0x4000
88 #define MII_M1011_PHY_STATUS_SPD_MASK 0xc000
89 #define MII_M1011_PHY_STATUS_FULLDUPLEX 0x2000
90 #define MII_M1011_PHY_STATUS_RESOLVED 0x0800
91 #define MII_M1011_PHY_STATUS_LINK 0x0400
94 MODULE_DESCRIPTION("Marvell PHY driver");
95 MODULE_AUTHOR("Andy Fleming");
96 MODULE_LICENSE("GPL");
98 static int marvell_ack_interrupt(struct phy_device
*phydev
)
102 /* Clear the interrupts by reading the reg */
103 err
= phy_read(phydev
, MII_M1011_IEVENT
);
111 static int marvell_config_intr(struct phy_device
*phydev
)
115 if (phydev
->interrupts
== PHY_INTERRUPT_ENABLED
)
116 err
= phy_write(phydev
, MII_M1011_IMASK
, MII_M1011_IMASK_INIT
);
118 err
= phy_write(phydev
, MII_M1011_IMASK
, MII_M1011_IMASK_CLEAR
);
123 static int marvell_config_aneg(struct phy_device
*phydev
)
127 /* The Marvell PHY has an errata which requires
128 * that certain registers get written in order
129 * to restart autonegotiation */
130 err
= phy_write(phydev
, MII_BMCR
, BMCR_RESET
);
135 err
= phy_write(phydev
, 0x1d, 0x1f);
139 err
= phy_write(phydev
, 0x1e, 0x200c);
143 err
= phy_write(phydev
, 0x1d, 0x5);
147 err
= phy_write(phydev
, 0x1e, 0);
151 err
= phy_write(phydev
, 0x1e, 0x100);
155 err
= phy_write(phydev
, MII_M1011_PHY_SCR
,
156 MII_M1011_PHY_SCR_AUTO_CROSS
);
160 err
= phy_write(phydev
, MII_M1111_PHY_LED_CONTROL
,
161 MII_M1111_PHY_LED_DIRECT
);
165 err
= genphy_config_aneg(phydev
);
169 if (phydev
->autoneg
!= AUTONEG_ENABLE
) {
173 * A write to speed/duplex bits (that is performed by
174 * genphy_config_aneg() call above) must be followed by
175 * a software reset. Otherwise, the write has no effect.
177 bmcr
= phy_read(phydev
, MII_BMCR
);
181 err
= phy_write(phydev
, MII_BMCR
, bmcr
| BMCR_RESET
);
189 static int m88e1121_config_aneg(struct phy_device
*phydev
)
191 int err
, oldpage
, mscr
;
193 oldpage
= phy_read(phydev
, MII_88E1121_PHY_PAGE
);
195 err
= phy_write(phydev
, MII_88E1121_PHY_PAGE
,
196 MII_88E1121_PHY_MSCR_PAGE
);
200 if ((phydev
->interface
== PHY_INTERFACE_MODE_RGMII
) ||
201 (phydev
->interface
== PHY_INTERFACE_MODE_RGMII_ID
) ||
202 (phydev
->interface
== PHY_INTERFACE_MODE_RGMII_RXID
) ||
203 (phydev
->interface
== PHY_INTERFACE_MODE_RGMII_TXID
)) {
205 mscr
= phy_read(phydev
, MII_88E1121_PHY_MSCR_REG
) &
206 MII_88E1121_PHY_MSCR_DELAY_MASK
;
208 if (phydev
->interface
== PHY_INTERFACE_MODE_RGMII_ID
)
209 mscr
|= (MII_88E1121_PHY_MSCR_RX_DELAY
|
210 MII_88E1121_PHY_MSCR_TX_DELAY
);
211 else if (phydev
->interface
== PHY_INTERFACE_MODE_RGMII_RXID
)
212 mscr
|= MII_88E1121_PHY_MSCR_RX_DELAY
;
213 else if (phydev
->interface
== PHY_INTERFACE_MODE_RGMII_TXID
)
214 mscr
|= MII_88E1121_PHY_MSCR_TX_DELAY
;
216 err
= phy_write(phydev
, MII_88E1121_PHY_MSCR_REG
, mscr
);
221 phy_write(phydev
, MII_88E1121_PHY_PAGE
, oldpage
);
223 err
= phy_write(phydev
, MII_BMCR
, BMCR_RESET
);
227 err
= phy_write(phydev
, MII_M1011_PHY_SCR
,
228 MII_M1011_PHY_SCR_AUTO_CROSS
);
232 oldpage
= phy_read(phydev
, MII_88E1121_PHY_PAGE
);
234 phy_write(phydev
, MII_88E1121_PHY_PAGE
, MII_88E1121_PHY_LED_PAGE
);
235 phy_write(phydev
, MII_88E1121_PHY_LED_CTRL
, MII_88E1121_PHY_LED_DEF
);
236 phy_write(phydev
, MII_88E1121_PHY_PAGE
, oldpage
);
238 err
= genphy_config_aneg(phydev
);
243 static int m88ec048_config_aneg(struct phy_device
*phydev
)
245 int err
, oldpage
, mscr
;
247 oldpage
= phy_read(phydev
, MII_88E1121_PHY_PAGE
);
249 err
= phy_write(phydev
, MII_88E1121_PHY_PAGE
,
250 MII_88E1121_PHY_MSCR_PAGE
);
254 mscr
= phy_read(phydev
, MII_88EC048_PHY_MSCR1_REG
);
255 mscr
|= MII_88EC048_PHY_MSCR1_PAD_ODD
;
257 err
= phy_write(phydev
, MII_88E1121_PHY_MSCR_REG
, mscr
);
261 err
= phy_write(phydev
, MII_88E1121_PHY_PAGE
, oldpage
);
265 return m88e1121_config_aneg(phydev
);
268 static int m88e1111_config_init(struct phy_device
*phydev
)
273 /* Enable Fiber/Copper auto selection */
274 temp
= phy_read(phydev
, MII_M1111_PHY_EXT_SR
);
275 temp
&= ~MII_M1111_HWCFG_FIBER_COPPER_AUTO
;
276 phy_write(phydev
, MII_M1111_PHY_EXT_SR
, temp
);
278 temp
= phy_read(phydev
, MII_BMCR
);
280 phy_write(phydev
, MII_BMCR
, temp
);
282 if ((phydev
->interface
== PHY_INTERFACE_MODE_RGMII
) ||
283 (phydev
->interface
== PHY_INTERFACE_MODE_RGMII_ID
) ||
284 (phydev
->interface
== PHY_INTERFACE_MODE_RGMII_RXID
) ||
285 (phydev
->interface
== PHY_INTERFACE_MODE_RGMII_TXID
)) {
287 temp
= phy_read(phydev
, MII_M1111_PHY_EXT_CR
);
291 if (phydev
->interface
== PHY_INTERFACE_MODE_RGMII_ID
) {
292 temp
|= (MII_M1111_RX_DELAY
| MII_M1111_TX_DELAY
);
293 } else if (phydev
->interface
== PHY_INTERFACE_MODE_RGMII_RXID
) {
294 temp
&= ~MII_M1111_TX_DELAY
;
295 temp
|= MII_M1111_RX_DELAY
;
296 } else if (phydev
->interface
== PHY_INTERFACE_MODE_RGMII_TXID
) {
297 temp
&= ~MII_M1111_RX_DELAY
;
298 temp
|= MII_M1111_TX_DELAY
;
301 err
= phy_write(phydev
, MII_M1111_PHY_EXT_CR
, temp
);
305 temp
= phy_read(phydev
, MII_M1111_PHY_EXT_SR
);
309 temp
&= ~(MII_M1111_HWCFG_MODE_MASK
);
311 if (temp
& MII_M1111_HWCFG_FIBER_COPPER_RES
)
312 temp
|= MII_M1111_HWCFG_MODE_FIBER_RGMII
;
314 temp
|= MII_M1111_HWCFG_MODE_COPPER_RGMII
;
316 err
= phy_write(phydev
, MII_M1111_PHY_EXT_SR
, temp
);
321 if (phydev
->interface
== PHY_INTERFACE_MODE_SGMII
) {
322 temp
= phy_read(phydev
, MII_M1111_PHY_EXT_SR
);
326 temp
&= ~(MII_M1111_HWCFG_MODE_MASK
);
327 temp
|= MII_M1111_HWCFG_MODE_SGMII_NO_CLK
;
328 temp
|= MII_M1111_HWCFG_FIBER_COPPER_AUTO
;
330 err
= phy_write(phydev
, MII_M1111_PHY_EXT_SR
, temp
);
335 if (phydev
->interface
== PHY_INTERFACE_MODE_RTBI
) {
336 temp
= phy_read(phydev
, MII_M1111_PHY_EXT_CR
);
339 temp
|= (MII_M1111_RX_DELAY
| MII_M1111_TX_DELAY
);
340 err
= phy_write(phydev
, MII_M1111_PHY_EXT_CR
, temp
);
344 temp
= phy_read(phydev
, MII_M1111_PHY_EXT_SR
);
347 temp
&= ~(MII_M1111_HWCFG_MODE_MASK
| MII_M1111_HWCFG_FIBER_COPPER_RES
);
348 temp
|= 0x7 | MII_M1111_HWCFG_FIBER_COPPER_AUTO
;
349 err
= phy_write(phydev
, MII_M1111_PHY_EXT_SR
, temp
);
354 err
= phy_write(phydev
, MII_BMCR
, BMCR_RESET
);
358 temp
= phy_read(phydev
, MII_BMCR
);
359 while (temp
& BMCR_RESET
);
361 temp
= phy_read(phydev
, MII_M1111_PHY_EXT_SR
);
364 temp
&= ~(MII_M1111_HWCFG_MODE_MASK
| MII_M1111_HWCFG_FIBER_COPPER_RES
);
365 temp
|= MII_M1111_HWCFG_MODE_COPPER_RTBI
| MII_M1111_HWCFG_FIBER_COPPER_AUTO
;
366 err
= phy_write(phydev
, MII_M1111_PHY_EXT_SR
, temp
);
372 err
= phy_write(phydev
, MII_BMCR
, BMCR_RESET
);
379 static int m88e1118_config_aneg(struct phy_device
*phydev
)
383 err
= phy_write(phydev
, MII_BMCR
, BMCR_RESET
);
387 err
= phy_write(phydev
, MII_M1011_PHY_SCR
,
388 MII_M1011_PHY_SCR_AUTO_CROSS
);
392 err
= genphy_config_aneg(phydev
);
396 static int m88e1118_config_init(struct phy_device
*phydev
)
401 err
= phy_write(phydev
, 0x16, 0x0002);
405 /* Enable 1000 Mbit */
406 err
= phy_write(phydev
, 0x15, 0x1070);
411 err
= phy_write(phydev
, 0x16, 0x0003);
415 /* Adjust LED Control */
416 if (phydev
->dev_flags
& MARVELL_PHY_M1118_DNS323_LEDS
)
417 err
= phy_write(phydev
, 0x10, 0x1100);
419 err
= phy_write(phydev
, 0x10, 0x021e);
424 err
= phy_write(phydev
, 0x16, 0x0);
428 err
= phy_write(phydev
, MII_BMCR
, BMCR_RESET
);
435 static int m88e1145_config_init(struct phy_device
*phydev
)
439 /* Take care of errata E0 & E1 */
440 err
= phy_write(phydev
, 0x1d, 0x001b);
444 err
= phy_write(phydev
, 0x1e, 0x418f);
448 err
= phy_write(phydev
, 0x1d, 0x0016);
452 err
= phy_write(phydev
, 0x1e, 0xa2da);
456 if (phydev
->interface
== PHY_INTERFACE_MODE_RGMII_ID
) {
457 int temp
= phy_read(phydev
, MII_M1145_PHY_EXT_CR
);
461 temp
|= (MII_M1145_RGMII_RX_DELAY
| MII_M1145_RGMII_TX_DELAY
);
463 err
= phy_write(phydev
, MII_M1145_PHY_EXT_CR
, temp
);
467 if (phydev
->dev_flags
& MARVELL_PHY_M1145_FLAGS_RESISTANCE
) {
468 err
= phy_write(phydev
, 0x1d, 0x0012);
472 temp
= phy_read(phydev
, 0x1e);
477 temp
|= 2 << 9; /* 36 ohm */
478 temp
|= 2 << 6; /* 39 ohm */
480 err
= phy_write(phydev
, 0x1e, temp
);
484 err
= phy_write(phydev
, 0x1d, 0x3);
488 err
= phy_write(phydev
, 0x1e, 0x8000);
497 /* marvell_read_status
499 * Generic status code does not detect Fiber correctly!
501 * Check the link, then figure out the current state
502 * by comparing what we advertise with what the link partner
503 * advertises. Start by checking the gigabit possibilities,
504 * then move on to 10/100.
506 static int marvell_read_status(struct phy_device
*phydev
)
513 /* Update the link, but return if there
515 err
= genphy_update_link(phydev
);
519 if (AUTONEG_ENABLE
== phydev
->autoneg
) {
520 status
= phy_read(phydev
, MII_M1011_PHY_STATUS
);
524 lpa
= phy_read(phydev
, MII_LPA
);
528 adv
= phy_read(phydev
, MII_ADVERTISE
);
534 if (status
& MII_M1011_PHY_STATUS_FULLDUPLEX
)
535 phydev
->duplex
= DUPLEX_FULL
;
537 phydev
->duplex
= DUPLEX_HALF
;
539 status
= status
& MII_M1011_PHY_STATUS_SPD_MASK
;
540 phydev
->pause
= phydev
->asym_pause
= 0;
543 case MII_M1011_PHY_STATUS_1000
:
544 phydev
->speed
= SPEED_1000
;
547 case MII_M1011_PHY_STATUS_100
:
548 phydev
->speed
= SPEED_100
;
552 phydev
->speed
= SPEED_10
;
556 if (phydev
->duplex
== DUPLEX_FULL
) {
557 phydev
->pause
= lpa
& LPA_PAUSE_CAP
? 1 : 0;
558 phydev
->asym_pause
= lpa
& LPA_PAUSE_ASYM
? 1 : 0;
561 int bmcr
= phy_read(phydev
, MII_BMCR
);
566 if (bmcr
& BMCR_FULLDPLX
)
567 phydev
->duplex
= DUPLEX_FULL
;
569 phydev
->duplex
= DUPLEX_HALF
;
571 if (bmcr
& BMCR_SPEED1000
)
572 phydev
->speed
= SPEED_1000
;
573 else if (bmcr
& BMCR_SPEED100
)
574 phydev
->speed
= SPEED_100
;
576 phydev
->speed
= SPEED_10
;
578 phydev
->pause
= phydev
->asym_pause
= 0;
584 static int m88e1121_did_interrupt(struct phy_device
*phydev
)
588 imask
= phy_read(phydev
, MII_M1011_IEVENT
);
590 if (imask
& MII_M1011_IMASK_INIT
)
596 static struct phy_driver marvell_drivers
[] = {
598 .phy_id
= MARVELL_PHY_ID_88E1101
,
599 .phy_id_mask
= MARVELL_PHY_ID_MASK
,
600 .name
= "Marvell 88E1101",
601 .features
= PHY_GBIT_FEATURES
,
602 .flags
= PHY_HAS_INTERRUPT
,
603 .config_aneg
= &marvell_config_aneg
,
604 .read_status
= &genphy_read_status
,
605 .ack_interrupt
= &marvell_ack_interrupt
,
606 .config_intr
= &marvell_config_intr
,
607 .driver
= { .owner
= THIS_MODULE
},
610 .phy_id
= MARVELL_PHY_ID_88E1112
,
611 .phy_id_mask
= MARVELL_PHY_ID_MASK
,
612 .name
= "Marvell 88E1112",
613 .features
= PHY_GBIT_FEATURES
,
614 .flags
= PHY_HAS_INTERRUPT
,
615 .config_init
= &m88e1111_config_init
,
616 .config_aneg
= &marvell_config_aneg
,
617 .read_status
= &genphy_read_status
,
618 .ack_interrupt
= &marvell_ack_interrupt
,
619 .config_intr
= &marvell_config_intr
,
620 .driver
= { .owner
= THIS_MODULE
},
623 .phy_id
= MARVELL_PHY_ID_88E1111
,
624 .phy_id_mask
= MARVELL_PHY_ID_MASK
,
625 .name
= "Marvell 88E1111",
626 .features
= PHY_GBIT_FEATURES
,
627 .flags
= PHY_HAS_INTERRUPT
,
628 .config_init
= &m88e1111_config_init
,
629 .config_aneg
= &marvell_config_aneg
,
630 .read_status
= &marvell_read_status
,
631 .ack_interrupt
= &marvell_ack_interrupt
,
632 .config_intr
= &marvell_config_intr
,
633 .driver
= { .owner
= THIS_MODULE
},
636 .phy_id
= MARVELL_PHY_ID_88E1118
,
637 .phy_id_mask
= MARVELL_PHY_ID_MASK
,
638 .name
= "Marvell 88E1118",
639 .features
= PHY_GBIT_FEATURES
,
640 .flags
= PHY_HAS_INTERRUPT
,
641 .config_init
= &m88e1118_config_init
,
642 .config_aneg
= &m88e1118_config_aneg
,
643 .read_status
= &genphy_read_status
,
644 .ack_interrupt
= &marvell_ack_interrupt
,
645 .config_intr
= &marvell_config_intr
,
646 .driver
= {.owner
= THIS_MODULE
,},
649 .phy_id
= MARVELL_PHY_ID_88E1121R
,
650 .phy_id_mask
= MARVELL_PHY_ID_MASK
,
651 .name
= "Marvell 88E1121R",
652 .features
= PHY_GBIT_FEATURES
,
653 .flags
= PHY_HAS_INTERRUPT
,
654 .config_aneg
= &m88e1121_config_aneg
,
655 .read_status
= &marvell_read_status
,
656 .ack_interrupt
= &marvell_ack_interrupt
,
657 .config_intr
= &marvell_config_intr
,
658 .did_interrupt
= &m88e1121_did_interrupt
,
659 .driver
= { .owner
= THIS_MODULE
},
662 .phy_id
= MARVELL_PHY_ID_88EC048
,
663 .phy_id_mask
= MARVELL_PHY_ID_MASK
,
664 .name
= "Marvell 88EC048",
665 .features
= PHY_GBIT_FEATURES
,
666 .flags
= PHY_HAS_INTERRUPT
,
667 .config_aneg
= &m88ec048_config_aneg
,
668 .read_status
= &marvell_read_status
,
669 .ack_interrupt
= &marvell_ack_interrupt
,
670 .config_intr
= &marvell_config_intr
,
671 .did_interrupt
= &m88e1121_did_interrupt
,
672 .driver
= { .owner
= THIS_MODULE
},
675 .phy_id
= MARVELL_PHY_ID_88E1145
,
676 .phy_id_mask
= MARVELL_PHY_ID_MASK
,
677 .name
= "Marvell 88E1145",
678 .features
= PHY_GBIT_FEATURES
,
679 .flags
= PHY_HAS_INTERRUPT
,
680 .config_init
= &m88e1145_config_init
,
681 .config_aneg
= &marvell_config_aneg
,
682 .read_status
= &genphy_read_status
,
683 .ack_interrupt
= &marvell_ack_interrupt
,
684 .config_intr
= &marvell_config_intr
,
685 .driver
= { .owner
= THIS_MODULE
},
688 .phy_id
= MARVELL_PHY_ID_88E1240
,
689 .phy_id_mask
= MARVELL_PHY_ID_MASK
,
690 .name
= "Marvell 88E1240",
691 .features
= PHY_GBIT_FEATURES
,
692 .flags
= PHY_HAS_INTERRUPT
,
693 .config_init
= &m88e1111_config_init
,
694 .config_aneg
= &marvell_config_aneg
,
695 .read_status
= &genphy_read_status
,
696 .ack_interrupt
= &marvell_ack_interrupt
,
697 .config_intr
= &marvell_config_intr
,
698 .driver
= { .owner
= THIS_MODULE
},
702 static int __init
marvell_init(void)
707 for (i
= 0; i
< ARRAY_SIZE(marvell_drivers
); i
++) {
708 ret
= phy_driver_register(&marvell_drivers
[i
]);
712 phy_driver_unregister(&marvell_drivers
[i
]);
720 static void __exit
marvell_exit(void)
724 for (i
= 0; i
< ARRAY_SIZE(marvell_drivers
); i
++)
725 phy_driver_unregister(&marvell_drivers
[i
]);
728 module_init(marvell_init
);
729 module_exit(marvell_exit
);
731 static struct mdio_device_id __maybe_unused marvell_tbl
[] = {
732 { 0x01410c60, 0xfffffff0 },
733 { 0x01410c90, 0xfffffff0 },
734 { 0x01410cc0, 0xfffffff0 },
735 { 0x01410e10, 0xfffffff0 },
736 { 0x01410cb0, 0xfffffff0 },
737 { 0x01410cd0, 0xfffffff0 },
738 { 0x01410e30, 0xfffffff0 },
739 { 0x01410e90, 0xfffffff0 },
743 MODULE_DEVICE_TABLE(mdio
, marvell_tbl
);