1 // SPDX-License-Identifier: GPL-2.0+
4 * Elecsys Corporation <www.elecsyscorp.com>
5 * Kevin Smith <kevin.smith@elecsyscorp.com>
9 * Marvell Semiconductor <www.marvell.com>
10 * Prafulla Wadaskar <prafulla@marvell.com>
14 * PHY driver for mv88e61xx ethernet switches.
16 * This driver configures the mv88e61xx for basic use as a PHY. The switch
17 * supports a VLAN configuration that determines how traffic will be routed
18 * between the ports. This driver uses a simple configuration that routes
19 * traffic from each PHY port only to the CPU port, and from the CPU port to
22 * The configuration determines which PHY ports to activate using the
23 * CONFIG_MV88E61XX_PHY_PORTS bitmask. Setting bit 0 will activate port 0, bit
24 * 1 activates port 1, etc. Do not set the bit for the port the CPU is
25 * connected to unless it is connected over a PHY interface (not MII).
27 * This driver was written for and tested on the mv88e6176 with an SGMII
28 * connection. Other configurations should be supported, but some additions or
29 * changes may be required.
34 #include <linux/bitops.h>
35 #include <linux/delay.h>
43 #define PHY_AUTONEGOTIATE_TIMEOUT 5000
45 #define PORT_MASK(port_count) ((1 << (port_count)) - 1)
47 /* Device addresses */
48 #define DEVADDR_PHY(p) (p)
49 #define DEVADDR_SERDES 0x0F
51 /* SMI indirection registers for multichip addressing mode */
52 #define SMI_CMD_REG 0x00
53 #define SMI_DATA_REG 0x01
55 /* Global registers */
56 #define GLOBAL1_STATUS 0x00
57 #define GLOBAL1_CTRL 0x04
58 #define GLOBAL1_MON_CTRL 0x1A
60 /* Global 2 registers */
61 #define GLOBAL2_REG_PHY_CMD 0x18
62 #define GLOBAL2_REG_PHY_DATA 0x19
65 #define PORT_REG_STATUS 0x00
66 #define PORT_REG_PHYS_CTRL 0x01
67 #define PORT_REG_SWITCH_ID 0x03
68 #define PORT_REG_CTRL 0x04
69 #define PORT_REG_VLAN_MAP 0x06
70 #define PORT_REG_VLAN_ID 0x07
73 #define PHY_REG_CTRL1 0x10
74 #define PHY_REG_STATUS1 0x11
75 #define PHY_REG_PAGE 0x16
77 /* Serdes registers */
78 #define SERDES_REG_CTRL_1 0x10
80 /* Phy page numbers */
81 #define PHY_PAGE_COPPER 0
82 #define PHY_PAGE_SERDES 1
85 #define GLOBAL1_CTRL_SWRESET BIT(15)
87 #define GLOBAL1_MON_CTRL_CPUDEST_SHIFT 4
88 #define GLOBAL1_MON_CTRL_CPUDEST_WIDTH 4
90 #define PORT_REG_STATUS_SPEED_SHIFT 8
91 #define PORT_REG_STATUS_SPEED_10 0
92 #define PORT_REG_STATUS_SPEED_100 1
93 #define PORT_REG_STATUS_SPEED_1000 2
95 #define PORT_REG_STATUS_CMODE_MASK 0xF
96 #define PORT_REG_STATUS_CMODE_100BASE_X 0x8
97 #define PORT_REG_STATUS_CMODE_1000BASE_X 0x9
98 #define PORT_REG_STATUS_CMODE_SGMII 0xa
100 #define PORT_REG_PHYS_CTRL_PCS_AN_EN BIT(10)
101 #define PORT_REG_PHYS_CTRL_PCS_AN_RST BIT(9)
102 #define PORT_REG_PHYS_CTRL_FC_VALUE BIT(7)
103 #define PORT_REG_PHYS_CTRL_FC_FORCE BIT(6)
104 #define PORT_REG_PHYS_CTRL_LINK_VALUE BIT(5)
105 #define PORT_REG_PHYS_CTRL_LINK_FORCE BIT(4)
106 #define PORT_REG_PHYS_CTRL_DUPLEX_VALUE BIT(3)
107 #define PORT_REG_PHYS_CTRL_DUPLEX_FORCE BIT(2)
108 #define PORT_REG_PHYS_CTRL_SPD1000 BIT(1)
109 #define PORT_REG_PHYS_CTRL_SPD100 BIT(0)
110 #define PORT_REG_PHYS_CTRL_SPD_MASK (BIT(1) | BIT(0))
112 #define PORT_REG_CTRL_PSTATE_SHIFT 0
113 #define PORT_REG_CTRL_PSTATE_WIDTH 2
115 #define PORT_REG_VLAN_ID_DEF_VID_SHIFT 0
116 #define PORT_REG_VLAN_ID_DEF_VID_WIDTH 12
118 #define PORT_REG_VLAN_MAP_TABLE_SHIFT 0
119 #define PORT_REG_VLAN_MAP_TABLE_WIDTH 11
121 #define SERDES_REG_CTRL_1_FORCE_LINK BIT(10)
124 #define PORT_REG_CTRL_PSTATE_DISABLED 0
125 #define PORT_REG_CTRL_PSTATE_FORWARD 3
127 #define PHY_REG_CTRL1_ENERGY_DET_OFF 0
128 #define PHY_REG_CTRL1_ENERGY_DET_SENSE_PULSE 1
129 #define PHY_REG_CTRL1_ENERGY_DET_SENSE_ONLY 2
130 #define PHY_REG_CTRL1_ENERGY_DET_SENSE_XMIT 3
132 /* PHY Status Register */
133 #define PHY_REG_STATUS1_SPEED 0xc000
134 #define PHY_REG_STATUS1_GBIT 0x8000
135 #define PHY_REG_STATUS1_100 0x4000
136 #define PHY_REG_STATUS1_DUPLEX 0x2000
137 #define PHY_REG_STATUS1_SPDDONE 0x0800
138 #define PHY_REG_STATUS1_LINK 0x0400
139 #define PHY_REG_STATUS1_ENERGY 0x0010
142 * Macros for building commands for indirect addressing modes. These are valid
143 * for both the indirect multichip addressing mode and the PHY indirection
144 * required for the writes to any PHY register.
146 #define SMI_BUSY BIT(15)
147 #define SMI_CMD_CLAUSE_22 BIT(12)
148 #define SMI_CMD_CLAUSE_22_OP_READ (2 << 10)
149 #define SMI_CMD_CLAUSE_22_OP_WRITE (1 << 10)
151 #define SMI_CMD_READ (SMI_BUSY | SMI_CMD_CLAUSE_22 | \
152 SMI_CMD_CLAUSE_22_OP_READ)
153 #define SMI_CMD_WRITE (SMI_BUSY | SMI_CMD_CLAUSE_22 | \
154 SMI_CMD_CLAUSE_22_OP_WRITE)
156 #define SMI_CMD_ADDR_SHIFT 5
157 #define SMI_CMD_ADDR_WIDTH 5
158 #define SMI_CMD_REG_SHIFT 0
159 #define SMI_CMD_REG_WIDTH 5
161 /* Check for required macros */
162 #ifndef CONFIG_MV88E61XX_PHY_PORTS
163 #error Define CONFIG_MV88E61XX_PHY_PORTS to indicate which physical ports \
166 #ifndef CONFIG_MV88E61XX_CPU_PORT
167 #error Define CONFIG_MV88E61XX_CPU_PORT to the port the CPU is attached to
171 * These are ports without PHYs that may be wired directly
172 * to other serdes interfaces
174 #ifndef CONFIG_MV88E61XX_FIXED_PORTS
175 #define CONFIG_MV88E61XX_FIXED_PORTS 0
178 /* ID register values for different switch models */
179 #define PORT_SWITCH_ID_6020 0x0200
180 #define PORT_SWITCH_ID_6070 0x0700
181 #define PORT_SWITCH_ID_6071 0x0710
182 #define PORT_SWITCH_ID_6096 0x0980
183 #define PORT_SWITCH_ID_6097 0x0990
184 #define PORT_SWITCH_ID_6172 0x1720
185 #define PORT_SWITCH_ID_6176 0x1760
186 #define PORT_SWITCH_ID_6220 0x2200
187 #define PORT_SWITCH_ID_6240 0x2400
188 #define PORT_SWITCH_ID_6250 0x2500
189 #define PORT_SWITCH_ID_6352 0x3520
191 struct mv88e61xx_phy_priv {
192 struct mii_dev *mdio_bus;
195 int port_count; /* Number of switch ports */
196 int port_reg_base; /* Base of the switch port registers */
197 u16 port_stat_link_mask;/* Bitmask for port link status bits */
198 u16 port_stat_dup_mask; /* Bitmask for port duplex status bits */
199 u8 port_stat_speed_width;/* Width of speed status bitfield */
200 u8 global1; /* Offset of Switch Global 1 registers */
201 u8 global2; /* Offset of Switch Global 2 registers */
202 u8 phy_ctrl1_en_det_shift; /* 'EDet' bit field offset */
203 u8 phy_ctrl1_en_det_width; /* Width of 'EDet' bit field */
204 u8 phy_ctrl1_en_det_ctrl; /* 'EDet' control value */
207 static inline int smi_cmd(int cmd, int addr, int reg)
209 cmd = bitfield_replace(cmd, SMI_CMD_ADDR_SHIFT, SMI_CMD_ADDR_WIDTH,
211 cmd = bitfield_replace(cmd, SMI_CMD_REG_SHIFT, SMI_CMD_REG_WIDTH, reg);
215 static inline int smi_cmd_read(int addr, int reg)
217 return smi_cmd(SMI_CMD_READ, addr, reg);
220 static inline int smi_cmd_write(int addr, int reg)
222 return smi_cmd(SMI_CMD_WRITE, addr, reg);
225 __weak int mv88e61xx_hw_reset(struct phy_device *phydev)
230 /* Wait for the current SMI indirect command to complete */
231 static int mv88e61xx_smi_wait(struct mii_dev *bus, int smi_addr)
237 val = bus->read(bus, smi_addr, MDIO_DEVAD_NONE, SMI_CMD_REG);
238 if (val >= 0 && (val & SMI_BUSY) == 0)
244 puts("SMI busy timeout\n");
249 * The mv88e61xx has three types of addresses: the smi bus address, the device
250 * address, and the register address. The smi bus address distinguishes it on
251 * the smi bus from other PHYs or switches. The device address determines
252 * which on-chip register set you are reading/writing (the various PHYs, their
253 * associated ports, or global configuration registers). The register address
254 * is the offset of the register you are reading/writing.
256 * When the mv88e61xx is hardware configured to have address zero, it behaves in
257 * single-chip addressing mode, where it responds to all SMI addresses, using
258 * the smi address as its device address. This obviously only works when this
259 * is the only chip on the SMI bus. This allows the driver to access device
260 * registers without using indirection. When the chip is configured to a
261 * non-zero address, it only responds to that SMI address and requires indirect
262 * writes to access the different device addresses.
264 static int mv88e61xx_reg_read(struct phy_device *phydev, int dev, int reg)
266 struct mv88e61xx_phy_priv *priv = phydev->priv;
267 struct mii_dev *mdio_bus = priv->mdio_bus;
268 int smi_addr = priv->smi_addr;
271 /* In single-chip mode, the device can be addressed directly */
273 return mdio_bus->read(mdio_bus, dev, MDIO_DEVAD_NONE, reg);
275 /* Wait for the bus to become free */
276 res = mv88e61xx_smi_wait(mdio_bus, smi_addr);
280 /* Issue the read command */
281 res = mdio_bus->write(mdio_bus, smi_addr, MDIO_DEVAD_NONE, SMI_CMD_REG,
282 smi_cmd_read(dev, reg));
286 /* Wait for the read command to complete */
287 res = mv88e61xx_smi_wait(mdio_bus, smi_addr);
292 res = mdio_bus->read(mdio_bus, smi_addr, MDIO_DEVAD_NONE, SMI_DATA_REG);
296 return bitfield_extract(res, 0, 16);
299 /* See the comment above mv88e61xx_reg_read */
300 static int mv88e61xx_reg_write(struct phy_device *phydev, int dev, int reg,
303 struct mv88e61xx_phy_priv *priv = phydev->priv;
304 struct mii_dev *mdio_bus = priv->mdio_bus;
305 int smi_addr = priv->smi_addr;
308 /* In single-chip mode, the device can be addressed directly */
310 return mdio_bus->write(mdio_bus, dev, MDIO_DEVAD_NONE, reg,
314 /* Wait for the bus to become free */
315 res = mv88e61xx_smi_wait(mdio_bus, smi_addr);
319 /* Set the data to write */
320 res = mdio_bus->write(mdio_bus, smi_addr, MDIO_DEVAD_NONE,
325 /* Issue the write command */
326 res = mdio_bus->write(mdio_bus, smi_addr, MDIO_DEVAD_NONE, SMI_CMD_REG,
327 smi_cmd_write(dev, reg));
331 /* Wait for the write command to complete */
332 res = mv88e61xx_smi_wait(mdio_bus, smi_addr);
339 static int mv88e61xx_phy_wait(struct phy_device *phydev)
341 struct mv88e61xx_phy_priv *priv = phydev->priv;
346 val = mv88e61xx_reg_read(phydev, priv->global2,
347 GLOBAL2_REG_PHY_CMD);
348 if (val >= 0 && (val & SMI_BUSY) == 0)
357 static int mv88e61xx_phy_read_indirect(struct mii_dev *smi_wrapper, int dev,
360 struct mv88e61xx_phy_priv *priv;
361 struct phy_device *phydev;
364 phydev = (struct phy_device *)smi_wrapper->priv;
367 /* Issue command to read */
368 res = mv88e61xx_reg_write(phydev, priv->global2,
370 smi_cmd_read(dev, reg));
372 /* Wait for data to be read */
373 res = mv88e61xx_phy_wait(phydev);
377 /* Read retrieved data */
378 return mv88e61xx_reg_read(phydev, priv->global2,
379 GLOBAL2_REG_PHY_DATA);
382 static int mv88e61xx_phy_write_indirect(struct mii_dev *smi_wrapper, int dev,
383 int devad, int reg, u16 data)
385 struct mv88e61xx_phy_priv *priv;
386 struct phy_device *phydev;
389 phydev = (struct phy_device *)smi_wrapper->priv;
392 /* Set the data to write */
393 res = mv88e61xx_reg_write(phydev, priv->global2,
394 GLOBAL2_REG_PHY_DATA, data);
397 /* Issue the write command */
398 res = mv88e61xx_reg_write(phydev, priv->global2,
400 smi_cmd_write(dev, reg));
404 /* Wait for command to complete */
405 return mv88e61xx_phy_wait(phydev);
408 /* Wrapper function to make calls to phy_read_indirect simpler */
409 static int mv88e61xx_phy_read(struct phy_device *phydev, int phy, int reg)
411 return mv88e61xx_phy_read_indirect(phydev->bus, DEVADDR_PHY(phy),
412 MDIO_DEVAD_NONE, reg);
415 /* Wrapper function to make calls to phy_read_indirect simpler */
416 static int mv88e61xx_phy_write(struct phy_device *phydev, int phy,
419 return mv88e61xx_phy_write_indirect(phydev->bus, DEVADDR_PHY(phy),
420 MDIO_DEVAD_NONE, reg, val);
423 static int mv88e61xx_port_read(struct phy_device *phydev, u8 port, u8 reg)
425 struct mv88e61xx_phy_priv *priv = phydev->priv;
427 return mv88e61xx_reg_read(phydev, priv->port_reg_base + port, reg);
430 static int mv88e61xx_port_write(struct phy_device *phydev, u8 port, u8 reg,
433 struct mv88e61xx_phy_priv *priv = phydev->priv;
435 return mv88e61xx_reg_write(phydev, priv->port_reg_base + port,
439 static int mv88e61xx_set_page(struct phy_device *phydev, u8 phy, u8 page)
441 return mv88e61xx_phy_write(phydev, phy, PHY_REG_PAGE, page);
444 static int mv88e61xx_get_switch_id(struct phy_device *phydev)
448 res = mv88e61xx_port_read(phydev, 0, PORT_REG_SWITCH_ID);
454 static bool mv88e61xx_6352_family(struct phy_device *phydev)
456 struct mv88e61xx_phy_priv *priv = phydev->priv;
459 case PORT_SWITCH_ID_6172:
460 case PORT_SWITCH_ID_6176:
461 case PORT_SWITCH_ID_6240:
462 case PORT_SWITCH_ID_6352:
468 static int mv88e61xx_get_cmode(struct phy_device *phydev, u8 port)
472 res = mv88e61xx_port_read(phydev, port, PORT_REG_STATUS);
475 return res & PORT_REG_STATUS_CMODE_MASK;
478 static int mv88e61xx_parse_status(struct phy_device *phydev)
481 unsigned int mii_reg;
483 mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, PHY_REG_STATUS1);
485 if ((mii_reg & PHY_REG_STATUS1_LINK) &&
486 !(mii_reg & PHY_REG_STATUS1_SPDDONE)) {
489 puts("Waiting for PHY realtime link");
490 while (!(mii_reg & PHY_REG_STATUS1_SPDDONE)) {
491 /* Timeout reached ? */
492 if (i > PHY_AUTONEGOTIATE_TIMEOUT) {
493 puts(" TIMEOUT !\n");
498 if ((i++ % 1000) == 0)
501 mii_reg = phy_read(phydev, MDIO_DEVAD_NONE,
505 udelay(500000); /* another 500 ms (results in faster booting) */
507 if (mii_reg & PHY_REG_STATUS1_LINK)
513 if (mii_reg & PHY_REG_STATUS1_DUPLEX)
514 phydev->duplex = DUPLEX_FULL;
516 phydev->duplex = DUPLEX_HALF;
518 speed = mii_reg & PHY_REG_STATUS1_SPEED;
521 case PHY_REG_STATUS1_GBIT:
522 phydev->speed = SPEED_1000;
524 case PHY_REG_STATUS1_100:
525 phydev->speed = SPEED_100;
528 phydev->speed = SPEED_10;
535 static int mv88e61xx_switch_reset(struct phy_device *phydev)
537 struct mv88e61xx_phy_priv *priv = phydev->priv;
542 /* Disable all ports */
543 for (port = 0; port < priv->port_count; port++) {
544 val = mv88e61xx_port_read(phydev, port, PORT_REG_CTRL);
547 val = bitfield_replace(val, PORT_REG_CTRL_PSTATE_SHIFT,
548 PORT_REG_CTRL_PSTATE_WIDTH,
549 PORT_REG_CTRL_PSTATE_DISABLED);
550 val = mv88e61xx_port_write(phydev, port, PORT_REG_CTRL, val);
555 /* Wait 2 ms for queues to drain */
559 val = mv88e61xx_reg_read(phydev, priv->global1, GLOBAL1_CTRL);
562 val |= GLOBAL1_CTRL_SWRESET;
563 val = mv88e61xx_reg_write(phydev, priv->global1,
568 /* Wait up to 1 second for switch reset complete */
569 for (time = 1000; time; time--) {
570 val = mv88e61xx_reg_read(phydev, priv->global1,
572 if (val >= 0 && ((val & GLOBAL1_CTRL_SWRESET) == 0))
582 static int mv88e61xx_serdes_init(struct phy_device *phydev)
586 val = mv88e61xx_set_page(phydev, DEVADDR_SERDES, PHY_PAGE_SERDES);
590 /* Power up serdes module */
591 val = mv88e61xx_phy_read(phydev, DEVADDR_SERDES, MII_BMCR);
594 val &= ~(BMCR_PDOWN);
595 val = mv88e61xx_phy_write(phydev, DEVADDR_SERDES, MII_BMCR, val);
602 static int mv88e61xx_port_enable(struct phy_device *phydev, u8 port)
606 val = mv88e61xx_port_read(phydev, port, PORT_REG_CTRL);
609 val = bitfield_replace(val, PORT_REG_CTRL_PSTATE_SHIFT,
610 PORT_REG_CTRL_PSTATE_WIDTH,
611 PORT_REG_CTRL_PSTATE_FORWARD);
612 val = mv88e61xx_port_write(phydev, port, PORT_REG_CTRL, val);
619 static int mv88e61xx_port_set_vlan(struct phy_device *phydev, u8 port,
624 /* Set VID to port number plus one */
625 val = mv88e61xx_port_read(phydev, port, PORT_REG_VLAN_ID);
628 val = bitfield_replace(val, PORT_REG_VLAN_ID_DEF_VID_SHIFT,
629 PORT_REG_VLAN_ID_DEF_VID_WIDTH,
631 val = mv88e61xx_port_write(phydev, port, PORT_REG_VLAN_ID, val);
636 val = mv88e61xx_port_read(phydev, port, PORT_REG_VLAN_MAP);
639 val = bitfield_replace(val, PORT_REG_VLAN_MAP_TABLE_SHIFT,
640 PORT_REG_VLAN_MAP_TABLE_WIDTH,
642 val = mv88e61xx_port_write(phydev, port, PORT_REG_VLAN_MAP, val);
649 static int mv88e61xx_read_port_config(struct phy_device *phydev, u8 port)
651 struct mv88e61xx_phy_priv *priv = phydev->priv;
656 val = mv88e61xx_port_read(phydev, port, PORT_REG_STATUS);
659 if (!(val & priv->port_stat_link_mask)) {
660 /* Temporarily force link to read port configuration */
664 val = mv88e61xx_port_read(phydev, port, PORT_REG_PHYS_CTRL);
667 val |= (PORT_REG_PHYS_CTRL_LINK_FORCE |
668 PORT_REG_PHYS_CTRL_LINK_VALUE);
669 val = mv88e61xx_port_write(phydev, port, PORT_REG_PHYS_CTRL,
674 /* Wait for status register to reflect forced link */
676 val = mv88e61xx_port_read(phydev, port,
682 if (val & priv->port_stat_link_mask)
692 if (val & priv->port_stat_dup_mask)
693 phydev->duplex = DUPLEX_FULL;
695 phydev->duplex = DUPLEX_HALF;
697 val = bitfield_extract(val, PORT_REG_STATUS_SPEED_SHIFT,
698 priv->port_stat_speed_width);
700 case PORT_REG_STATUS_SPEED_1000:
701 phydev->speed = SPEED_1000;
703 case PORT_REG_STATUS_SPEED_100:
704 phydev->speed = SPEED_100;
707 phydev->speed = SPEED_10;
715 val = mv88e61xx_port_read(phydev, port, PORT_REG_PHYS_CTRL);
718 val &= ~(PORT_REG_PHYS_CTRL_LINK_FORCE |
719 PORT_REG_PHYS_CTRL_LINK_VALUE);
720 val = mv88e61xx_port_write(phydev, port, PORT_REG_PHYS_CTRL,
729 static int mv88e61xx_fixed_port_setup(struct phy_device *phydev, u8 port)
731 struct mv88e61xx_phy_priv *priv = phydev->priv;
734 val = mv88e61xx_port_read(phydev, port, PORT_REG_PHYS_CTRL);
738 val &= ~(PORT_REG_PHYS_CTRL_SPD_MASK |
739 PORT_REG_PHYS_CTRL_FC_VALUE |
740 PORT_REG_PHYS_CTRL_FC_FORCE);
741 val |= PORT_REG_PHYS_CTRL_FC_FORCE |
742 PORT_REG_PHYS_CTRL_DUPLEX_VALUE |
743 PORT_REG_PHYS_CTRL_DUPLEX_FORCE;
745 if (priv->id == PORT_SWITCH_ID_6071) {
746 val |= PORT_REG_PHYS_CTRL_SPD100;
748 val |= PORT_REG_PHYS_CTRL_PCS_AN_EN |
749 PORT_REG_PHYS_CTRL_PCS_AN_RST |
750 PORT_REG_PHYS_CTRL_SPD1000;
753 if (port == CONFIG_MV88E61XX_CPU_PORT)
754 val |= PORT_REG_PHYS_CTRL_LINK_VALUE |
755 PORT_REG_PHYS_CTRL_LINK_FORCE;
757 return mv88e61xx_port_write(phydev, port, PORT_REG_PHYS_CTRL,
761 static int mv88e61xx_set_cpu_port(struct phy_device *phydev)
763 struct mv88e61xx_phy_priv *priv = phydev->priv;
767 val = mv88e61xx_reg_read(phydev, priv->global1, GLOBAL1_MON_CTRL);
770 val = bitfield_replace(val, GLOBAL1_MON_CTRL_CPUDEST_SHIFT,
771 GLOBAL1_MON_CTRL_CPUDEST_WIDTH,
772 CONFIG_MV88E61XX_CPU_PORT);
773 val = mv88e61xx_reg_write(phydev, priv->global1,
774 GLOBAL1_MON_CTRL, val);
778 /* Allow CPU to route to any port */
779 val = PORT_MASK(priv->port_count) & ~(1 << CONFIG_MV88E61XX_CPU_PORT);
780 val = mv88e61xx_port_set_vlan(phydev, CONFIG_MV88E61XX_CPU_PORT, val);
784 /* Enable CPU port */
785 val = mv88e61xx_port_enable(phydev, CONFIG_MV88E61XX_CPU_PORT);
789 val = mv88e61xx_read_port_config(phydev, CONFIG_MV88E61XX_CPU_PORT);
793 /* If CPU is connected to serdes, initialize serdes */
794 if (mv88e61xx_6352_family(phydev)) {
795 val = mv88e61xx_get_cmode(phydev, CONFIG_MV88E61XX_CPU_PORT);
798 if (val == PORT_REG_STATUS_CMODE_100BASE_X ||
799 val == PORT_REG_STATUS_CMODE_1000BASE_X ||
800 val == PORT_REG_STATUS_CMODE_SGMII) {
801 val = mv88e61xx_serdes_init(phydev);
806 val = mv88e61xx_fixed_port_setup(phydev,
807 CONFIG_MV88E61XX_CPU_PORT);
815 static int mv88e61xx_switch_init(struct phy_device *phydev)
823 res = mv88e61xx_switch_reset(phydev);
827 res = mv88e61xx_set_cpu_port(phydev);
836 static int mv88e61xx_phy_enable(struct phy_device *phydev, u8 phy)
840 val = mv88e61xx_phy_read(phydev, phy, MII_BMCR);
843 val &= ~(BMCR_PDOWN);
844 val = mv88e61xx_phy_write(phydev, phy, MII_BMCR, val);
851 static int mv88e61xx_phy_setup(struct phy_device *phydev, u8 phy)
853 struct mv88e61xx_phy_priv *priv = phydev->priv;
857 * Enable energy-detect sensing on PHY, used to determine when a PHY
858 * port is physically connected
860 val = mv88e61xx_phy_read(phydev, phy, PHY_REG_CTRL1);
863 val = bitfield_replace(val, priv->phy_ctrl1_en_det_shift,
864 priv->phy_ctrl1_en_det_width,
865 priv->phy_ctrl1_en_det_ctrl);
866 val = mv88e61xx_phy_write(phydev, phy, PHY_REG_CTRL1, val);
873 static int mv88e61xx_phy_config_port(struct phy_device *phydev, u8 phy)
877 val = mv88e61xx_port_enable(phydev, phy);
881 val = mv88e61xx_port_set_vlan(phydev, phy,
882 1 << CONFIG_MV88E61XX_CPU_PORT);
890 * This function is used to pre-configure the required register
891 * offsets, so that the indirect register access to the PHY registers
892 * is possible. This is necessary to be able to read the PHY ID
893 * while driver probing or in get_phy_id(). The globalN register
894 * offsets must be initialized correctly for a detected switch,
895 * otherwise detection of the PHY ID won't work!
897 static int mv88e61xx_priv_reg_offs_pre_init(struct phy_device *phydev)
899 struct mv88e61xx_phy_priv *priv = phydev->priv;
902 * Initial 'port_reg_base' value must be an offset of existing
903 * port register, then reading the ID should succeed. First, try
904 * to read via port registers with device address 0x10 (88E6096
905 * and compatible switches).
907 priv->port_reg_base = 0x10;
908 priv->id = mv88e61xx_get_switch_id(phydev);
909 if (priv->id != 0xfff0) {
910 priv->global1 = 0x1B;
911 priv->global2 = 0x1C;
916 * Now try via port registers with device address 0x08
917 * (88E6020 and compatible switches).
919 priv->port_reg_base = 0x08;
920 priv->id = mv88e61xx_get_switch_id(phydev);
921 if (priv->id != 0xfff0) {
922 priv->global1 = 0x0F;
923 priv->global2 = 0x07;
927 debug("%s Unknown ID 0x%x\n", __func__, priv->id);
931 static int mv88e61xx_probe(struct phy_device *phydev)
933 struct mii_dev *smi_wrapper;
934 struct mv88e61xx_phy_priv *priv;
937 res = mv88e61xx_hw_reset(phydev);
941 priv = malloc(sizeof(*priv));
945 memset(priv, 0, sizeof(*priv));
948 * This device requires indirect reads/writes to the PHY registers
949 * which the generic PHY code can't handle. Make a wrapper MII device
950 * to handle reads/writes
952 smi_wrapper = mdio_alloc();
959 * Store the mdio bus in the private data, as we are going to replace
960 * the bus with the wrapper bus
962 priv->mdio_bus = phydev->bus;
965 * Store the smi bus address in private data. This lets us use the
966 * phydev addr field for device address instead, as the genphy code
969 priv->smi_addr = phydev->addr;
972 * Store the phy_device in the wrapper mii device. This lets us get it
973 * back when genphy functions call phy_read/phy_write.
975 smi_wrapper->priv = phydev;
976 strncpy(smi_wrapper->name, "indirect mii", sizeof(smi_wrapper->name));
977 smi_wrapper->read = mv88e61xx_phy_read_indirect;
978 smi_wrapper->write = mv88e61xx_phy_write_indirect;
980 /* Replace the bus with the wrapper device */
981 phydev->bus = smi_wrapper;
985 res = mv88e61xx_priv_reg_offs_pre_init(phydev);
989 debug("%s ID 0x%x\n", __func__, priv->id);
992 case PORT_SWITCH_ID_6096:
993 case PORT_SWITCH_ID_6097:
994 case PORT_SWITCH_ID_6172:
995 case PORT_SWITCH_ID_6176:
996 case PORT_SWITCH_ID_6240:
997 case PORT_SWITCH_ID_6352:
998 priv->port_count = 11;
999 priv->port_stat_link_mask = BIT(11);
1000 priv->port_stat_dup_mask = BIT(10);
1001 priv->port_stat_speed_width = 2;
1002 priv->phy_ctrl1_en_det_shift = 8;
1003 priv->phy_ctrl1_en_det_width = 2;
1004 priv->phy_ctrl1_en_det_ctrl =
1005 PHY_REG_CTRL1_ENERGY_DET_SENSE_XMIT;
1007 case PORT_SWITCH_ID_6020:
1008 case PORT_SWITCH_ID_6070:
1009 case PORT_SWITCH_ID_6071:
1010 case PORT_SWITCH_ID_6220:
1011 case PORT_SWITCH_ID_6250:
1012 priv->port_count = 7;
1013 priv->port_stat_link_mask = BIT(12);
1014 priv->port_stat_dup_mask = BIT(9);
1015 priv->port_stat_speed_width = 1;
1016 priv->phy_ctrl1_en_det_shift = 14;
1017 priv->phy_ctrl1_en_det_width = 1;
1018 priv->phy_ctrl1_en_det_ctrl =
1019 PHY_REG_CTRL1_ENERGY_DET_SENSE_PULSE;
1026 res = mdio_register(smi_wrapper);
1028 printf("Failed to register SMI bus\n");
1033 static int mv88e61xx_phy_config(struct phy_device *phydev)
1035 struct mv88e61xx_phy_priv *priv = phydev->priv;
1040 res = mv88e61xx_switch_init(phydev);
1044 for (i = 0; i < priv->port_count; i++) {
1045 if ((1 << i) & CONFIG_MV88E61XX_PHY_PORTS) {
1048 res = mv88e61xx_phy_enable(phydev, i);
1050 printf("Error enabling PHY %i\n", i);
1053 res = mv88e61xx_phy_setup(phydev, i);
1055 printf("Error setting up PHY %i\n", i);
1058 res = mv88e61xx_phy_config_port(phydev, i);
1060 printf("Error configuring PHY %i\n", i);
1064 res = phy_reset(phydev);
1066 printf("Error resetting PHY %i\n", i);
1069 res = genphy_config_aneg(phydev);
1071 printf("Error setting PHY %i autoneg\n", i);
1075 /* Return success if any PHY succeeds */
1077 } else if ((1 << i) & CONFIG_MV88E61XX_FIXED_PORTS) {
1078 res = mv88e61xx_fixed_port_setup(phydev, i);
1080 printf("Error configuring port %i\n", i);
1089 static int mv88e61xx_phy_is_connected(struct phy_device *phydev)
1093 val = mv88e61xx_phy_read(phydev, phydev->addr, PHY_REG_STATUS1);
1098 * After reset, the energy detect signal remains high for a few seconds
1099 * regardless of whether a cable is connected. This function will
1100 * return false positives during this time.
1102 return (val & PHY_REG_STATUS1_ENERGY) == 0;
1105 static int mv88e61xx_phy_startup(struct phy_device *phydev)
1107 struct mv88e61xx_phy_priv *priv = phydev->priv;
1111 int speed = phydev->speed;
1112 int duplex = phydev->duplex;
1114 for (i = 0; i < priv->port_count; i++) {
1115 if ((1 << i) & CONFIG_MV88E61XX_PHY_PORTS) {
1117 if (!mv88e61xx_phy_is_connected(phydev))
1119 res = genphy_update_link(phydev);
1122 res = mv88e61xx_parse_status(phydev);
1125 link = (link || phydev->link);
1128 phydev->link = link;
1130 /* Restore CPU interface speed and duplex after it was changed for
1132 phydev->speed = speed;
1133 phydev->duplex = duplex;
1138 static struct phy_driver mv88e61xx_driver = {
1139 .name = "Marvell MV88E61xx",
1142 .features = PHY_GBIT_FEATURES,
1143 .probe = mv88e61xx_probe,
1144 .config = mv88e61xx_phy_config,
1145 .startup = mv88e61xx_phy_startup,
1146 .shutdown = &genphy_shutdown,
1149 static struct phy_driver mv88e609x_driver = {
1150 .name = "Marvell MV88E609x",
1153 .features = PHY_GBIT_FEATURES,
1154 .probe = mv88e61xx_probe,
1155 .config = mv88e61xx_phy_config,
1156 .startup = mv88e61xx_phy_startup,
1157 .shutdown = &genphy_shutdown,
1160 static struct phy_driver mv88e6071_driver = {
1161 .name = "Marvell MV88E6071",
1164 .features = PHY_BASIC_FEATURES | SUPPORTED_MII,
1165 .probe = mv88e61xx_probe,
1166 .config = mv88e61xx_phy_config,
1167 .startup = mv88e61xx_phy_startup,
1168 .shutdown = &genphy_shutdown,
1171 int phy_mv88e61xx_init(void)
1173 phy_register(&mv88e61xx_driver);
1174 phy_register(&mv88e609x_driver);
1175 phy_register(&mv88e6071_driver);
1181 * Overload weak get_phy_id definition since we need non-standard functions
1182 * to read PHY registers
1184 int get_phy_id(struct mii_dev *bus, int smi_addr, int devad, u32 *phy_id)
1186 struct phy_device temp_phy;
1187 struct mv88e61xx_phy_priv temp_priv;
1188 struct mii_dev temp_mii;
1192 * Buid temporary data structures that the chip reading code needs to
1195 temp_priv.mdio_bus = bus;
1196 temp_priv.smi_addr = smi_addr;
1197 temp_phy.priv = &temp_priv;
1198 temp_mii.priv = &temp_phy;
1201 * get_phy_id() can be called by framework before mv88e61xx driver
1202 * probing, in this case the global register offsets are not
1203 * initialized yet. Do this initialization here before indirect
1204 * PHY register access.
1206 val = mv88e61xx_priv_reg_offs_pre_init(&temp_phy);
1210 val = mv88e61xx_phy_read_indirect(&temp_mii, 0, devad, MII_PHYSID1);
1214 *phy_id = val << 16;
1216 val = mv88e61xx_phy_read_indirect(&temp_mii, 0, devad, MII_PHYSID2);
1220 *phy_id |= (val & 0xffff);