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>
35 #include <asm/uaccess.h>
37 #define MII_M1011_IEVENT 0x13
38 #define MII_M1011_IEVENT_CLEAR 0x0000
40 #define MII_M1011_IMASK 0x12
41 #define MII_M1011_IMASK_INIT 0x6400
42 #define MII_M1011_IMASK_CLEAR 0x0000
44 #define MII_M1011_PHY_SCR 0x10
45 #define MII_M1011_PHY_SCR_AUTO_CROSS 0x0060
47 #define MII_M1145_PHY_EXT_CR 0x14
48 #define MII_M1145_RGMII_RX_DELAY 0x0080
49 #define MII_M1145_RGMII_TX_DELAY 0x0002
51 #define M1145_DEV_FLAGS_RESISTANCE 0x00000001
53 #define MII_M1111_PHY_LED_CONTROL 0x18
54 #define MII_M1111_PHY_LED_DIRECT 0x4100
55 #define MII_M1111_PHY_LED_COMBINE 0x411c
56 #define MII_M1111_PHY_EXT_CR 0x14
57 #define MII_M1111_RX_DELAY 0x80
58 #define MII_M1111_TX_DELAY 0x2
59 #define MII_M1111_PHY_EXT_SR 0x1b
61 #define MII_M1111_HWCFG_MODE_MASK 0xf
62 #define MII_M1111_HWCFG_MODE_COPPER_RGMII 0xb
63 #define MII_M1111_HWCFG_MODE_FIBER_RGMII 0x3
64 #define MII_M1111_HWCFG_MODE_SGMII_NO_CLK 0x4
65 #define MII_M1111_HWCFG_MODE_COPPER_RTBI 0x9
66 #define MII_M1111_HWCFG_FIBER_COPPER_AUTO 0x8000
67 #define MII_M1111_HWCFG_FIBER_COPPER_RES 0x2000
69 #define MII_M1111_COPPER 0
70 #define MII_M1111_FIBER 1
72 #define MII_88E1121_PHY_MSCR_PAGE 2
73 #define MII_88E1121_PHY_MSCR_REG 21
74 #define MII_88E1121_PHY_MSCR_RX_DELAY BIT(5)
75 #define MII_88E1121_PHY_MSCR_TX_DELAY BIT(4)
76 #define MII_88E1121_PHY_MSCR_DELAY_MASK (~(0x3 << 4))
78 #define MII_88EC048_PHY_MSCR1_REG 16
79 #define MII_88EC048_PHY_MSCR1_PAD_ODD BIT(6)
81 #define MII_88E1121_PHY_LED_CTRL 16
82 #define MII_88E1121_PHY_LED_PAGE 3
83 #define MII_88E1121_PHY_LED_DEF 0x0030
84 #define MII_88E1121_PHY_PAGE 22
86 #define MII_M1011_PHY_STATUS 0x11
87 #define MII_M1011_PHY_STATUS_1000 0x8000
88 #define MII_M1011_PHY_STATUS_100 0x4000
89 #define MII_M1011_PHY_STATUS_SPD_MASK 0xc000
90 #define MII_M1011_PHY_STATUS_FULLDUPLEX 0x2000
91 #define MII_M1011_PHY_STATUS_RESOLVED 0x0800
92 #define MII_M1011_PHY_STATUS_LINK 0x0400
95 MODULE_DESCRIPTION("Marvell PHY driver");
96 MODULE_AUTHOR("Andy Fleming");
97 MODULE_LICENSE("GPL");
99 static int marvell_ack_interrupt(struct phy_device *phydev)
103 /* Clear the interrupts by reading the reg */
104 err = phy_read(phydev, MII_M1011_IEVENT);
112 static int marvell_config_intr(struct phy_device *phydev)
116 if (phydev->interrupts == PHY_INTERRUPT_ENABLED)
117 err = phy_write(phydev, MII_M1011_IMASK, MII_M1011_IMASK_INIT);
119 err = phy_write(phydev, MII_M1011_IMASK, MII_M1011_IMASK_CLEAR);
124 static int marvell_config_aneg(struct phy_device *phydev)
128 /* The Marvell PHY has an errata which requires
129 * that certain registers get written in order
130 * to restart autonegotiation */
131 err = phy_write(phydev, MII_BMCR, BMCR_RESET);
136 err = phy_write(phydev, 0x1d, 0x1f);
140 err = phy_write(phydev, 0x1e, 0x200c);
144 err = phy_write(phydev, 0x1d, 0x5);
148 err = phy_write(phydev, 0x1e, 0);
152 err = phy_write(phydev, 0x1e, 0x100);
156 err = phy_write(phydev, MII_M1011_PHY_SCR,
157 MII_M1011_PHY_SCR_AUTO_CROSS);
161 err = phy_write(phydev, MII_M1111_PHY_LED_CONTROL,
162 MII_M1111_PHY_LED_DIRECT);
166 err = genphy_config_aneg(phydev);
170 if (phydev->autoneg != AUTONEG_ENABLE) {
174 * A write to speed/duplex bits (that is performed by
175 * genphy_config_aneg() call above) must be followed by
176 * a software reset. Otherwise, the write has no effect.
178 bmcr = phy_read(phydev, MII_BMCR);
182 err = phy_write(phydev, MII_BMCR, bmcr | BMCR_RESET);
190 static int m88e1121_config_aneg(struct phy_device *phydev)
192 int err, oldpage, mscr;
194 oldpage = phy_read(phydev, MII_88E1121_PHY_PAGE);
196 err = phy_write(phydev, MII_88E1121_PHY_PAGE,
197 MII_88E1121_PHY_MSCR_PAGE);
200 mscr = phy_read(phydev, MII_88E1121_PHY_MSCR_REG) &
201 MII_88E1121_PHY_MSCR_DELAY_MASK;
203 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID)
204 mscr |= (MII_88E1121_PHY_MSCR_RX_DELAY |
205 MII_88E1121_PHY_MSCR_TX_DELAY);
206 else if (phydev->interface == PHY_INTERFACE_MODE_RGMII_RXID)
207 mscr |= MII_88E1121_PHY_MSCR_RX_DELAY;
208 else if (phydev->interface == PHY_INTERFACE_MODE_RGMII_TXID)
209 mscr |= MII_88E1121_PHY_MSCR_TX_DELAY;
211 err = phy_write(phydev, MII_88E1121_PHY_MSCR_REG, mscr);
215 phy_write(phydev, MII_88E1121_PHY_PAGE, oldpage);
217 err = phy_write(phydev, MII_BMCR, BMCR_RESET);
221 err = phy_write(phydev, MII_M1011_PHY_SCR,
222 MII_M1011_PHY_SCR_AUTO_CROSS);
226 oldpage = phy_read(phydev, MII_88E1121_PHY_PAGE);
228 phy_write(phydev, MII_88E1121_PHY_PAGE, MII_88E1121_PHY_LED_PAGE);
229 phy_write(phydev, MII_88E1121_PHY_LED_CTRL, MII_88E1121_PHY_LED_DEF);
230 phy_write(phydev, MII_88E1121_PHY_PAGE, oldpage);
232 err = genphy_config_aneg(phydev);
237 static int m88ec048_config_aneg(struct phy_device *phydev)
239 int err, oldpage, mscr;
241 oldpage = phy_read(phydev, MII_88E1121_PHY_PAGE);
243 err = phy_write(phydev, MII_88E1121_PHY_PAGE,
244 MII_88E1121_PHY_MSCR_PAGE);
248 mscr = phy_read(phydev, MII_88EC048_PHY_MSCR1_REG);
249 mscr |= MII_88EC048_PHY_MSCR1_PAD_ODD;
251 err = phy_write(phydev, MII_88E1121_PHY_MSCR_REG, mscr);
255 err = phy_write(phydev, MII_88E1121_PHY_PAGE, oldpage);
259 return m88e1121_config_aneg(phydev);
262 static int m88e1111_config_init(struct phy_device *phydev)
267 /* Enable Fiber/Copper auto selection */
268 temp = phy_read(phydev, MII_M1111_PHY_EXT_SR);
269 temp &= ~MII_M1111_HWCFG_FIBER_COPPER_AUTO;
270 phy_write(phydev, MII_M1111_PHY_EXT_SR, temp);
272 temp = phy_read(phydev, MII_BMCR);
274 phy_write(phydev, MII_BMCR, temp);
276 if ((phydev->interface == PHY_INTERFACE_MODE_RGMII) ||
277 (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID) ||
278 (phydev->interface == PHY_INTERFACE_MODE_RGMII_RXID) ||
279 (phydev->interface == PHY_INTERFACE_MODE_RGMII_TXID)) {
281 temp = phy_read(phydev, MII_M1111_PHY_EXT_CR);
285 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID) {
286 temp |= (MII_M1111_RX_DELAY | MII_M1111_TX_DELAY);
287 } else if (phydev->interface == PHY_INTERFACE_MODE_RGMII_RXID) {
288 temp &= ~MII_M1111_TX_DELAY;
289 temp |= MII_M1111_RX_DELAY;
290 } else if (phydev->interface == PHY_INTERFACE_MODE_RGMII_TXID) {
291 temp &= ~MII_M1111_RX_DELAY;
292 temp |= MII_M1111_TX_DELAY;
295 err = phy_write(phydev, MII_M1111_PHY_EXT_CR, temp);
299 temp = phy_read(phydev, MII_M1111_PHY_EXT_SR);
303 temp &= ~(MII_M1111_HWCFG_MODE_MASK);
305 if (temp & MII_M1111_HWCFG_FIBER_COPPER_RES)
306 temp |= MII_M1111_HWCFG_MODE_FIBER_RGMII;
308 temp |= MII_M1111_HWCFG_MODE_COPPER_RGMII;
310 err = phy_write(phydev, MII_M1111_PHY_EXT_SR, temp);
315 if (phydev->interface == PHY_INTERFACE_MODE_SGMII) {
316 temp = phy_read(phydev, MII_M1111_PHY_EXT_SR);
320 temp &= ~(MII_M1111_HWCFG_MODE_MASK);
321 temp |= MII_M1111_HWCFG_MODE_SGMII_NO_CLK;
322 temp |= MII_M1111_HWCFG_FIBER_COPPER_AUTO;
324 err = phy_write(phydev, MII_M1111_PHY_EXT_SR, temp);
329 if (phydev->interface == PHY_INTERFACE_MODE_RTBI) {
330 temp = phy_read(phydev, MII_M1111_PHY_EXT_CR);
333 temp |= (MII_M1111_RX_DELAY | MII_M1111_TX_DELAY);
334 err = phy_write(phydev, MII_M1111_PHY_EXT_CR, temp);
338 temp = phy_read(phydev, MII_M1111_PHY_EXT_SR);
341 temp &= ~(MII_M1111_HWCFG_MODE_MASK | MII_M1111_HWCFG_FIBER_COPPER_RES);
342 temp |= 0x7 | MII_M1111_HWCFG_FIBER_COPPER_AUTO;
343 err = phy_write(phydev, MII_M1111_PHY_EXT_SR, temp);
348 err = phy_write(phydev, MII_BMCR, BMCR_RESET);
352 temp = phy_read(phydev, MII_BMCR);
353 while (temp & BMCR_RESET);
355 temp = phy_read(phydev, MII_M1111_PHY_EXT_SR);
358 temp &= ~(MII_M1111_HWCFG_MODE_MASK | MII_M1111_HWCFG_FIBER_COPPER_RES);
359 temp |= MII_M1111_HWCFG_MODE_COPPER_RTBI | MII_M1111_HWCFG_FIBER_COPPER_AUTO;
360 err = phy_write(phydev, MII_M1111_PHY_EXT_SR, temp);
366 err = phy_write(phydev, MII_BMCR, BMCR_RESET);
373 static int m88e1118_config_aneg(struct phy_device *phydev)
377 err = phy_write(phydev, MII_BMCR, BMCR_RESET);
381 err = phy_write(phydev, MII_M1011_PHY_SCR,
382 MII_M1011_PHY_SCR_AUTO_CROSS);
386 err = genphy_config_aneg(phydev);
390 static int m88e1118_config_init(struct phy_device *phydev)
395 err = phy_write(phydev, 0x16, 0x0002);
399 /* Enable 1000 Mbit */
400 err = phy_write(phydev, 0x15, 0x1070);
405 err = phy_write(phydev, 0x16, 0x0003);
409 /* Adjust LED Control */
410 err = phy_write(phydev, 0x10, 0x021e);
415 err = phy_write(phydev, 0x16, 0x0);
419 err = phy_write(phydev, MII_BMCR, BMCR_RESET);
426 static int m88e1145_config_init(struct phy_device *phydev)
430 /* Take care of errata E0 & E1 */
431 err = phy_write(phydev, 0x1d, 0x001b);
435 err = phy_write(phydev, 0x1e, 0x418f);
439 err = phy_write(phydev, 0x1d, 0x0016);
443 err = phy_write(phydev, 0x1e, 0xa2da);
447 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID) {
448 int temp = phy_read(phydev, MII_M1145_PHY_EXT_CR);
452 temp |= (MII_M1145_RGMII_RX_DELAY | MII_M1145_RGMII_TX_DELAY);
454 err = phy_write(phydev, MII_M1145_PHY_EXT_CR, temp);
458 if (phydev->dev_flags & M1145_DEV_FLAGS_RESISTANCE) {
459 err = phy_write(phydev, 0x1d, 0x0012);
463 temp = phy_read(phydev, 0x1e);
468 temp |= 2 << 9; /* 36 ohm */
469 temp |= 2 << 6; /* 39 ohm */
471 err = phy_write(phydev, 0x1e, temp);
475 err = phy_write(phydev, 0x1d, 0x3);
479 err = phy_write(phydev, 0x1e, 0x8000);
488 /* marvell_read_status
490 * Generic status code does not detect Fiber correctly!
492 * Check the link, then figure out the current state
493 * by comparing what we advertise with what the link partner
494 * advertises. Start by checking the gigabit possibilities,
495 * then move on to 10/100.
497 static int marvell_read_status(struct phy_device *phydev)
504 /* Update the link, but return if there
506 err = genphy_update_link(phydev);
510 if (AUTONEG_ENABLE == phydev->autoneg) {
511 status = phy_read(phydev, MII_M1011_PHY_STATUS);
515 lpa = phy_read(phydev, MII_LPA);
519 adv = phy_read(phydev, MII_ADVERTISE);
525 if (status & MII_M1011_PHY_STATUS_FULLDUPLEX)
526 phydev->duplex = DUPLEX_FULL;
528 phydev->duplex = DUPLEX_HALF;
530 status = status & MII_M1011_PHY_STATUS_SPD_MASK;
531 phydev->pause = phydev->asym_pause = 0;
534 case MII_M1011_PHY_STATUS_1000:
535 phydev->speed = SPEED_1000;
538 case MII_M1011_PHY_STATUS_100:
539 phydev->speed = SPEED_100;
543 phydev->speed = SPEED_10;
547 if (phydev->duplex == DUPLEX_FULL) {
548 phydev->pause = lpa & LPA_PAUSE_CAP ? 1 : 0;
549 phydev->asym_pause = lpa & LPA_PAUSE_ASYM ? 1 : 0;
552 int bmcr = phy_read(phydev, MII_BMCR);
557 if (bmcr & BMCR_FULLDPLX)
558 phydev->duplex = DUPLEX_FULL;
560 phydev->duplex = DUPLEX_HALF;
562 if (bmcr & BMCR_SPEED1000)
563 phydev->speed = SPEED_1000;
564 else if (bmcr & BMCR_SPEED100)
565 phydev->speed = SPEED_100;
567 phydev->speed = SPEED_10;
569 phydev->pause = phydev->asym_pause = 0;
575 static int m88e1121_did_interrupt(struct phy_device *phydev)
579 imask = phy_read(phydev, MII_M1011_IEVENT);
581 if (imask & MII_M1011_IMASK_INIT)
587 static struct phy_driver marvell_drivers[] = {
589 .phy_id = 0x01410c60,
590 .phy_id_mask = 0xfffffff0,
591 .name = "Marvell 88E1101",
592 .features = PHY_GBIT_FEATURES,
593 .flags = PHY_HAS_INTERRUPT,
594 .config_aneg = &marvell_config_aneg,
595 .read_status = &genphy_read_status,
596 .ack_interrupt = &marvell_ack_interrupt,
597 .config_intr = &marvell_config_intr,
598 .driver = { .owner = THIS_MODULE },
601 .phy_id = 0x01410c90,
602 .phy_id_mask = 0xfffffff0,
603 .name = "Marvell 88E1112",
604 .features = PHY_GBIT_FEATURES,
605 .flags = PHY_HAS_INTERRUPT,
606 .config_init = &m88e1111_config_init,
607 .config_aneg = &marvell_config_aneg,
608 .read_status = &genphy_read_status,
609 .ack_interrupt = &marvell_ack_interrupt,
610 .config_intr = &marvell_config_intr,
611 .driver = { .owner = THIS_MODULE },
614 .phy_id = 0x01410cc0,
615 .phy_id_mask = 0xfffffff0,
616 .name = "Marvell 88E1111",
617 .features = PHY_GBIT_FEATURES,
618 .flags = PHY_HAS_INTERRUPT,
619 .config_init = &m88e1111_config_init,
620 .config_aneg = &marvell_config_aneg,
621 .read_status = &marvell_read_status,
622 .ack_interrupt = &marvell_ack_interrupt,
623 .config_intr = &marvell_config_intr,
624 .driver = { .owner = THIS_MODULE },
627 .phy_id = 0x01410e10,
628 .phy_id_mask = 0xfffffff0,
629 .name = "Marvell 88E1118",
630 .features = PHY_GBIT_FEATURES,
631 .flags = PHY_HAS_INTERRUPT,
632 .config_init = &m88e1118_config_init,
633 .config_aneg = &m88e1118_config_aneg,
634 .read_status = &genphy_read_status,
635 .ack_interrupt = &marvell_ack_interrupt,
636 .config_intr = &marvell_config_intr,
637 .driver = {.owner = THIS_MODULE,},
640 .phy_id = 0x01410cb0,
641 .phy_id_mask = 0xfffffff0,
642 .name = "Marvell 88E1121R",
643 .features = PHY_GBIT_FEATURES,
644 .flags = PHY_HAS_INTERRUPT,
645 .config_aneg = &m88e1121_config_aneg,
646 .read_status = &marvell_read_status,
647 .ack_interrupt = &marvell_ack_interrupt,
648 .config_intr = &marvell_config_intr,
649 .did_interrupt = &m88e1121_did_interrupt,
650 .driver = { .owner = THIS_MODULE },
653 .phy_id = 0x01410e90,
654 .phy_id_mask = 0xfffffff0,
655 .name = "Marvell 88EC048",
656 .features = PHY_GBIT_FEATURES,
657 .flags = PHY_HAS_INTERRUPT,
658 .config_aneg = &m88ec048_config_aneg,
659 .read_status = &marvell_read_status,
660 .ack_interrupt = &marvell_ack_interrupt,
661 .config_intr = &marvell_config_intr,
662 .did_interrupt = &m88e1121_did_interrupt,
663 .driver = { .owner = THIS_MODULE },
666 .phy_id = 0x01410cd0,
667 .phy_id_mask = 0xfffffff0,
668 .name = "Marvell 88E1145",
669 .features = PHY_GBIT_FEATURES,
670 .flags = PHY_HAS_INTERRUPT,
671 .config_init = &m88e1145_config_init,
672 .config_aneg = &marvell_config_aneg,
673 .read_status = &genphy_read_status,
674 .ack_interrupt = &marvell_ack_interrupt,
675 .config_intr = &marvell_config_intr,
676 .driver = { .owner = THIS_MODULE },
679 .phy_id = 0x01410e30,
680 .phy_id_mask = 0xfffffff0,
681 .name = "Marvell 88E1240",
682 .features = PHY_GBIT_FEATURES,
683 .flags = PHY_HAS_INTERRUPT,
684 .config_init = &m88e1111_config_init,
685 .config_aneg = &marvell_config_aneg,
686 .read_status = &genphy_read_status,
687 .ack_interrupt = &marvell_ack_interrupt,
688 .config_intr = &marvell_config_intr,
689 .driver = { .owner = THIS_MODULE },
693 static int __init marvell_init(void)
698 for (i = 0; i < ARRAY_SIZE(marvell_drivers); i++) {
699 ret = phy_driver_register(&marvell_drivers[i]);
703 phy_driver_unregister(&marvell_drivers[i]);
711 static void __exit marvell_exit(void)
715 for (i = 0; i < ARRAY_SIZE(marvell_drivers); i++)
716 phy_driver_unregister(&marvell_drivers[i]);
719 module_init(marvell_init);
720 module_exit(marvell_exit);
722 static struct mdio_device_id marvell_tbl[] = {
723 { 0x01410c60, 0xfffffff0 },
724 { 0x01410c90, 0xfffffff0 },
725 { 0x01410cc0, 0xfffffff0 },
726 { 0x01410e10, 0xfffffff0 },
727 { 0x01410cb0, 0xfffffff0 },
728 { 0x01410cd0, 0xfffffff0 },
729 { 0x01410e30, 0xfffffff0 },
730 { 0x01410e90, 0xfffffff0 },
734 MODULE_DEVICE_TABLE(mdio, marvell_tbl);