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
);
199 mscr
= phy_read(phydev
, MII_88E1121_PHY_MSCR_REG
) &
200 MII_88E1121_PHY_MSCR_DELAY_MASK
;
202 if (phydev
->interface
== PHY_INTERFACE_MODE_RGMII_ID
)
203 mscr
|= (MII_88E1121_PHY_MSCR_RX_DELAY
|
204 MII_88E1121_PHY_MSCR_TX_DELAY
);
205 else if (phydev
->interface
== PHY_INTERFACE_MODE_RGMII_RXID
)
206 mscr
|= MII_88E1121_PHY_MSCR_RX_DELAY
;
207 else if (phydev
->interface
== PHY_INTERFACE_MODE_RGMII_TXID
)
208 mscr
|= MII_88E1121_PHY_MSCR_TX_DELAY
;
210 err
= phy_write(phydev
, MII_88E1121_PHY_MSCR_REG
, mscr
);
214 phy_write(phydev
, MII_88E1121_PHY_PAGE
, oldpage
);
216 err
= phy_write(phydev
, MII_BMCR
, BMCR_RESET
);
220 err
= phy_write(phydev
, MII_M1011_PHY_SCR
,
221 MII_M1011_PHY_SCR_AUTO_CROSS
);
225 oldpage
= phy_read(phydev
, MII_88E1121_PHY_PAGE
);
227 phy_write(phydev
, MII_88E1121_PHY_PAGE
, MII_88E1121_PHY_LED_PAGE
);
228 phy_write(phydev
, MII_88E1121_PHY_LED_CTRL
, MII_88E1121_PHY_LED_DEF
);
229 phy_write(phydev
, MII_88E1121_PHY_PAGE
, oldpage
);
231 err
= genphy_config_aneg(phydev
);
236 static int m88ec048_config_aneg(struct phy_device
*phydev
)
238 int err
, oldpage
, mscr
;
240 oldpage
= phy_read(phydev
, MII_88E1121_PHY_PAGE
);
242 err
= phy_write(phydev
, MII_88E1121_PHY_PAGE
,
243 MII_88E1121_PHY_MSCR_PAGE
);
247 mscr
= phy_read(phydev
, MII_88EC048_PHY_MSCR1_REG
);
248 mscr
|= MII_88EC048_PHY_MSCR1_PAD_ODD
;
250 err
= phy_write(phydev
, MII_88E1121_PHY_MSCR_REG
, mscr
);
254 err
= phy_write(phydev
, MII_88E1121_PHY_PAGE
, oldpage
);
258 return m88e1121_config_aneg(phydev
);
261 static int m88e1111_config_init(struct phy_device
*phydev
)
266 /* Enable Fiber/Copper auto selection */
267 temp
= phy_read(phydev
, MII_M1111_PHY_EXT_SR
);
268 temp
&= ~MII_M1111_HWCFG_FIBER_COPPER_AUTO
;
269 phy_write(phydev
, MII_M1111_PHY_EXT_SR
, temp
);
271 temp
= phy_read(phydev
, MII_BMCR
);
273 phy_write(phydev
, MII_BMCR
, temp
);
275 if ((phydev
->interface
== PHY_INTERFACE_MODE_RGMII
) ||
276 (phydev
->interface
== PHY_INTERFACE_MODE_RGMII_ID
) ||
277 (phydev
->interface
== PHY_INTERFACE_MODE_RGMII_RXID
) ||
278 (phydev
->interface
== PHY_INTERFACE_MODE_RGMII_TXID
)) {
280 temp
= phy_read(phydev
, MII_M1111_PHY_EXT_CR
);
284 if (phydev
->interface
== PHY_INTERFACE_MODE_RGMII_ID
) {
285 temp
|= (MII_M1111_RX_DELAY
| MII_M1111_TX_DELAY
);
286 } else if (phydev
->interface
== PHY_INTERFACE_MODE_RGMII_RXID
) {
287 temp
&= ~MII_M1111_TX_DELAY
;
288 temp
|= MII_M1111_RX_DELAY
;
289 } else if (phydev
->interface
== PHY_INTERFACE_MODE_RGMII_TXID
) {
290 temp
&= ~MII_M1111_RX_DELAY
;
291 temp
|= MII_M1111_TX_DELAY
;
294 err
= phy_write(phydev
, MII_M1111_PHY_EXT_CR
, temp
);
298 temp
= phy_read(phydev
, MII_M1111_PHY_EXT_SR
);
302 temp
&= ~(MII_M1111_HWCFG_MODE_MASK
);
304 if (temp
& MII_M1111_HWCFG_FIBER_COPPER_RES
)
305 temp
|= MII_M1111_HWCFG_MODE_FIBER_RGMII
;
307 temp
|= MII_M1111_HWCFG_MODE_COPPER_RGMII
;
309 err
= phy_write(phydev
, MII_M1111_PHY_EXT_SR
, temp
);
314 if (phydev
->interface
== PHY_INTERFACE_MODE_SGMII
) {
315 temp
= phy_read(phydev
, MII_M1111_PHY_EXT_SR
);
319 temp
&= ~(MII_M1111_HWCFG_MODE_MASK
);
320 temp
|= MII_M1111_HWCFG_MODE_SGMII_NO_CLK
;
321 temp
|= MII_M1111_HWCFG_FIBER_COPPER_AUTO
;
323 err
= phy_write(phydev
, MII_M1111_PHY_EXT_SR
, temp
);
328 if (phydev
->interface
== PHY_INTERFACE_MODE_RTBI
) {
329 temp
= phy_read(phydev
, MII_M1111_PHY_EXT_CR
);
332 temp
|= (MII_M1111_RX_DELAY
| MII_M1111_TX_DELAY
);
333 err
= phy_write(phydev
, MII_M1111_PHY_EXT_CR
, temp
);
337 temp
= phy_read(phydev
, MII_M1111_PHY_EXT_SR
);
340 temp
&= ~(MII_M1111_HWCFG_MODE_MASK
| MII_M1111_HWCFG_FIBER_COPPER_RES
);
341 temp
|= 0x7 | MII_M1111_HWCFG_FIBER_COPPER_AUTO
;
342 err
= phy_write(phydev
, MII_M1111_PHY_EXT_SR
, temp
);
347 err
= phy_write(phydev
, MII_BMCR
, BMCR_RESET
);
351 temp
= phy_read(phydev
, MII_BMCR
);
352 while (temp
& BMCR_RESET
);
354 temp
= phy_read(phydev
, MII_M1111_PHY_EXT_SR
);
357 temp
&= ~(MII_M1111_HWCFG_MODE_MASK
| MII_M1111_HWCFG_FIBER_COPPER_RES
);
358 temp
|= MII_M1111_HWCFG_MODE_COPPER_RTBI
| MII_M1111_HWCFG_FIBER_COPPER_AUTO
;
359 err
= phy_write(phydev
, MII_M1111_PHY_EXT_SR
, temp
);
365 err
= phy_write(phydev
, MII_BMCR
, BMCR_RESET
);
372 static int m88e1118_config_aneg(struct phy_device
*phydev
)
376 err
= phy_write(phydev
, MII_BMCR
, BMCR_RESET
);
380 err
= phy_write(phydev
, MII_M1011_PHY_SCR
,
381 MII_M1011_PHY_SCR_AUTO_CROSS
);
385 err
= genphy_config_aneg(phydev
);
389 static int m88e1118_config_init(struct phy_device
*phydev
)
394 err
= phy_write(phydev
, 0x16, 0x0002);
398 /* Enable 1000 Mbit */
399 err
= phy_write(phydev
, 0x15, 0x1070);
404 err
= phy_write(phydev
, 0x16, 0x0003);
408 /* Adjust LED Control */
409 if (phydev
->dev_flags
& MARVELL_PHY_M1118_DNS323_LEDS
)
410 err
= phy_write(phydev
, 0x10, 0x1100);
412 err
= phy_write(phydev
, 0x10, 0x021e);
417 err
= phy_write(phydev
, 0x16, 0x0);
421 err
= phy_write(phydev
, MII_BMCR
, BMCR_RESET
);
428 static int m88e1145_config_init(struct phy_device
*phydev
)
432 /* Take care of errata E0 & E1 */
433 err
= phy_write(phydev
, 0x1d, 0x001b);
437 err
= phy_write(phydev
, 0x1e, 0x418f);
441 err
= phy_write(phydev
, 0x1d, 0x0016);
445 err
= phy_write(phydev
, 0x1e, 0xa2da);
449 if (phydev
->interface
== PHY_INTERFACE_MODE_RGMII_ID
) {
450 int temp
= phy_read(phydev
, MII_M1145_PHY_EXT_CR
);
454 temp
|= (MII_M1145_RGMII_RX_DELAY
| MII_M1145_RGMII_TX_DELAY
);
456 err
= phy_write(phydev
, MII_M1145_PHY_EXT_CR
, temp
);
460 if (phydev
->dev_flags
& MARVELL_PHY_M1145_FLAGS_RESISTANCE
) {
461 err
= phy_write(phydev
, 0x1d, 0x0012);
465 temp
= phy_read(phydev
, 0x1e);
470 temp
|= 2 << 9; /* 36 ohm */
471 temp
|= 2 << 6; /* 39 ohm */
473 err
= phy_write(phydev
, 0x1e, temp
);
477 err
= phy_write(phydev
, 0x1d, 0x3);
481 err
= phy_write(phydev
, 0x1e, 0x8000);
490 /* marvell_read_status
492 * Generic status code does not detect Fiber correctly!
494 * Check the link, then figure out the current state
495 * by comparing what we advertise with what the link partner
496 * advertises. Start by checking the gigabit possibilities,
497 * then move on to 10/100.
499 static int marvell_read_status(struct phy_device
*phydev
)
506 /* Update the link, but return if there
508 err
= genphy_update_link(phydev
);
512 if (AUTONEG_ENABLE
== phydev
->autoneg
) {
513 status
= phy_read(phydev
, MII_M1011_PHY_STATUS
);
517 lpa
= phy_read(phydev
, MII_LPA
);
521 adv
= phy_read(phydev
, MII_ADVERTISE
);
527 if (status
& MII_M1011_PHY_STATUS_FULLDUPLEX
)
528 phydev
->duplex
= DUPLEX_FULL
;
530 phydev
->duplex
= DUPLEX_HALF
;
532 status
= status
& MII_M1011_PHY_STATUS_SPD_MASK
;
533 phydev
->pause
= phydev
->asym_pause
= 0;
536 case MII_M1011_PHY_STATUS_1000
:
537 phydev
->speed
= SPEED_1000
;
540 case MII_M1011_PHY_STATUS_100
:
541 phydev
->speed
= SPEED_100
;
545 phydev
->speed
= SPEED_10
;
549 if (phydev
->duplex
== DUPLEX_FULL
) {
550 phydev
->pause
= lpa
& LPA_PAUSE_CAP
? 1 : 0;
551 phydev
->asym_pause
= lpa
& LPA_PAUSE_ASYM
? 1 : 0;
554 int bmcr
= phy_read(phydev
, MII_BMCR
);
559 if (bmcr
& BMCR_FULLDPLX
)
560 phydev
->duplex
= DUPLEX_FULL
;
562 phydev
->duplex
= DUPLEX_HALF
;
564 if (bmcr
& BMCR_SPEED1000
)
565 phydev
->speed
= SPEED_1000
;
566 else if (bmcr
& BMCR_SPEED100
)
567 phydev
->speed
= SPEED_100
;
569 phydev
->speed
= SPEED_10
;
571 phydev
->pause
= phydev
->asym_pause
= 0;
577 static int m88e1121_did_interrupt(struct phy_device
*phydev
)
581 imask
= phy_read(phydev
, MII_M1011_IEVENT
);
583 if (imask
& MII_M1011_IMASK_INIT
)
589 static struct phy_driver marvell_drivers
[] = {
591 .phy_id
= MARVELL_PHY_ID_88E1101
,
592 .phy_id_mask
= MARVELL_PHY_ID_MASK
,
593 .name
= "Marvell 88E1101",
594 .features
= PHY_GBIT_FEATURES
,
595 .flags
= PHY_HAS_INTERRUPT
,
596 .config_aneg
= &marvell_config_aneg
,
597 .read_status
= &genphy_read_status
,
598 .ack_interrupt
= &marvell_ack_interrupt
,
599 .config_intr
= &marvell_config_intr
,
600 .driver
= { .owner
= THIS_MODULE
},
603 .phy_id
= MARVELL_PHY_ID_88E1112
,
604 .phy_id_mask
= MARVELL_PHY_ID_MASK
,
605 .name
= "Marvell 88E1112",
606 .features
= PHY_GBIT_FEATURES
,
607 .flags
= PHY_HAS_INTERRUPT
,
608 .config_init
= &m88e1111_config_init
,
609 .config_aneg
= &marvell_config_aneg
,
610 .read_status
= &genphy_read_status
,
611 .ack_interrupt
= &marvell_ack_interrupt
,
612 .config_intr
= &marvell_config_intr
,
613 .driver
= { .owner
= THIS_MODULE
},
616 .phy_id
= MARVELL_PHY_ID_88E1111
,
617 .phy_id_mask
= MARVELL_PHY_ID_MASK
,
618 .name
= "Marvell 88E1111",
619 .features
= PHY_GBIT_FEATURES
,
620 .flags
= PHY_HAS_INTERRUPT
,
621 .config_init
= &m88e1111_config_init
,
622 .config_aneg
= &marvell_config_aneg
,
623 .read_status
= &marvell_read_status
,
624 .ack_interrupt
= &marvell_ack_interrupt
,
625 .config_intr
= &marvell_config_intr
,
626 .driver
= { .owner
= THIS_MODULE
},
629 .phy_id
= MARVELL_PHY_ID_88E1118
,
630 .phy_id_mask
= MARVELL_PHY_ID_MASK
,
631 .name
= "Marvell 88E1118",
632 .features
= PHY_GBIT_FEATURES
,
633 .flags
= PHY_HAS_INTERRUPT
,
634 .config_init
= &m88e1118_config_init
,
635 .config_aneg
= &m88e1118_config_aneg
,
636 .read_status
= &genphy_read_status
,
637 .ack_interrupt
= &marvell_ack_interrupt
,
638 .config_intr
= &marvell_config_intr
,
639 .driver
= {.owner
= THIS_MODULE
,},
642 .phy_id
= MARVELL_PHY_ID_88E1121R
,
643 .phy_id_mask
= MARVELL_PHY_ID_MASK
,
644 .name
= "Marvell 88E1121R",
645 .features
= PHY_GBIT_FEATURES
,
646 .flags
= PHY_HAS_INTERRUPT
,
647 .config_aneg
= &m88e1121_config_aneg
,
648 .read_status
= &marvell_read_status
,
649 .ack_interrupt
= &marvell_ack_interrupt
,
650 .config_intr
= &marvell_config_intr
,
651 .did_interrupt
= &m88e1121_did_interrupt
,
652 .driver
= { .owner
= THIS_MODULE
},
655 .phy_id
= MARVELL_PHY_ID_88EC048
,
656 .phy_id_mask
= MARVELL_PHY_ID_MASK
,
657 .name
= "Marvell 88EC048",
658 .features
= PHY_GBIT_FEATURES
,
659 .flags
= PHY_HAS_INTERRUPT
,
660 .config_aneg
= &m88ec048_config_aneg
,
661 .read_status
= &marvell_read_status
,
662 .ack_interrupt
= &marvell_ack_interrupt
,
663 .config_intr
= &marvell_config_intr
,
664 .did_interrupt
= &m88e1121_did_interrupt
,
665 .driver
= { .owner
= THIS_MODULE
},
668 .phy_id
= MARVELL_PHY_ID_88E1145
,
669 .phy_id_mask
= MARVELL_PHY_ID_MASK
,
670 .name
= "Marvell 88E1145",
671 .features
= PHY_GBIT_FEATURES
,
672 .flags
= PHY_HAS_INTERRUPT
,
673 .config_init
= &m88e1145_config_init
,
674 .config_aneg
= &marvell_config_aneg
,
675 .read_status
= &genphy_read_status
,
676 .ack_interrupt
= &marvell_ack_interrupt
,
677 .config_intr
= &marvell_config_intr
,
678 .driver
= { .owner
= THIS_MODULE
},
681 .phy_id
= MARVELL_PHY_ID_88E1240
,
682 .phy_id_mask
= MARVELL_PHY_ID_MASK
,
683 .name
= "Marvell 88E1240",
684 .features
= PHY_GBIT_FEATURES
,
685 .flags
= PHY_HAS_INTERRUPT
,
686 .config_init
= &m88e1111_config_init
,
687 .config_aneg
= &marvell_config_aneg
,
688 .read_status
= &genphy_read_status
,
689 .ack_interrupt
= &marvell_ack_interrupt
,
690 .config_intr
= &marvell_config_intr
,
691 .driver
= { .owner
= THIS_MODULE
},
695 static int __init
marvell_init(void)
700 for (i
= 0; i
< ARRAY_SIZE(marvell_drivers
); i
++) {
701 ret
= phy_driver_register(&marvell_drivers
[i
]);
705 phy_driver_unregister(&marvell_drivers
[i
]);
713 static void __exit
marvell_exit(void)
717 for (i
= 0; i
< ARRAY_SIZE(marvell_drivers
); i
++)
718 phy_driver_unregister(&marvell_drivers
[i
]);
721 module_init(marvell_init
);
722 module_exit(marvell_exit
);
724 static struct mdio_device_id marvell_tbl
[] = {
725 { 0x01410c60, 0xfffffff0 },
726 { 0x01410c90, 0xfffffff0 },
727 { 0x01410cc0, 0xfffffff0 },
728 { 0x01410e10, 0xfffffff0 },
729 { 0x01410cb0, 0xfffffff0 },
730 { 0x01410cd0, 0xfffffff0 },
731 { 0x01410e30, 0xfffffff0 },
732 { 0x01410e90, 0xfffffff0 },
736 MODULE_DEVICE_TABLE(mdio
, marvell_tbl
);