Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Kevin Smith | 87b2c4e | 2016-03-31 19:33:12 +0000 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2015 |
| 4 | * Elecsys Corporation <www.elecsyscorp.com> |
| 5 | * Kevin Smith <kevin.smith@elecsyscorp.com> |
| 6 | * |
| 7 | * Original driver: |
| 8 | * (C) Copyright 2009 |
| 9 | * Marvell Semiconductor <www.marvell.com> |
| 10 | * Prafulla Wadaskar <prafulla@marvell.com> |
Kevin Smith | 87b2c4e | 2016-03-31 19:33:12 +0000 | [diff] [blame] | 11 | */ |
| 12 | |
| 13 | /* |
| 14 | * PHY driver for mv88e61xx ethernet switches. |
| 15 | * |
| 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 |
| 20 | * any PHY port. |
| 21 | * |
| 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). |
| 26 | * |
| 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. |
| 30 | */ |
| 31 | |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 32 | #include <log.h> |
Simon Glass | 4dcacfc | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 33 | #include <linux/bitops.h> |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 34 | #include <linux/delay.h> |
Kevin Smith | 87b2c4e | 2016-03-31 19:33:12 +0000 | [diff] [blame] | 35 | |
| 36 | #include <bitfield.h> |
| 37 | #include <errno.h> |
| 38 | #include <malloc.h> |
| 39 | #include <miiphy.h> |
| 40 | #include <netdev.h> |
| 41 | |
| 42 | #define PHY_AUTONEGOTIATE_TIMEOUT 5000 |
| 43 | |
Anatolij Gustschin | e477917 | 2019-10-27 01:14:37 +0200 | [diff] [blame] | 44 | #define PORT_MASK(port_count) ((1 << (port_count)) - 1) |
Kevin Smith | 87b2c4e | 2016-03-31 19:33:12 +0000 | [diff] [blame] | 45 | |
| 46 | /* Device addresses */ |
| 47 | #define DEVADDR_PHY(p) (p) |
Kevin Smith | 87b2c4e | 2016-03-31 19:33:12 +0000 | [diff] [blame] | 48 | #define DEVADDR_SERDES 0x0F |
Kevin Smith | 87b2c4e | 2016-03-31 19:33:12 +0000 | [diff] [blame] | 49 | |
| 50 | /* SMI indirection registers for multichip addressing mode */ |
| 51 | #define SMI_CMD_REG 0x00 |
| 52 | #define SMI_DATA_REG 0x01 |
| 53 | |
| 54 | /* Global registers */ |
| 55 | #define GLOBAL1_STATUS 0x00 |
| 56 | #define GLOBAL1_CTRL 0x04 |
| 57 | #define GLOBAL1_MON_CTRL 0x1A |
| 58 | |
| 59 | /* Global 2 registers */ |
| 60 | #define GLOBAL2_REG_PHY_CMD 0x18 |
| 61 | #define GLOBAL2_REG_PHY_DATA 0x19 |
| 62 | |
| 63 | /* Port registers */ |
| 64 | #define PORT_REG_STATUS 0x00 |
| 65 | #define PORT_REG_PHYS_CTRL 0x01 |
| 66 | #define PORT_REG_SWITCH_ID 0x03 |
| 67 | #define PORT_REG_CTRL 0x04 |
| 68 | #define PORT_REG_VLAN_MAP 0x06 |
| 69 | #define PORT_REG_VLAN_ID 0x07 |
| 70 | |
| 71 | /* Phy registers */ |
| 72 | #define PHY_REG_CTRL1 0x10 |
| 73 | #define PHY_REG_STATUS1 0x11 |
| 74 | #define PHY_REG_PAGE 0x16 |
| 75 | |
| 76 | /* Serdes registers */ |
| 77 | #define SERDES_REG_CTRL_1 0x10 |
| 78 | |
| 79 | /* Phy page numbers */ |
| 80 | #define PHY_PAGE_COPPER 0 |
| 81 | #define PHY_PAGE_SERDES 1 |
| 82 | |
| 83 | /* Register fields */ |
| 84 | #define GLOBAL1_CTRL_SWRESET BIT(15) |
| 85 | |
| 86 | #define GLOBAL1_MON_CTRL_CPUDEST_SHIFT 4 |
| 87 | #define GLOBAL1_MON_CTRL_CPUDEST_WIDTH 4 |
| 88 | |
Kevin Smith | 87b2c4e | 2016-03-31 19:33:12 +0000 | [diff] [blame] | 89 | #define PORT_REG_STATUS_SPEED_SHIFT 8 |
Kevin Smith | 87b2c4e | 2016-03-31 19:33:12 +0000 | [diff] [blame] | 90 | #define PORT_REG_STATUS_SPEED_10 0 |
| 91 | #define PORT_REG_STATUS_SPEED_100 1 |
| 92 | #define PORT_REG_STATUS_SPEED_1000 2 |
| 93 | |
| 94 | #define PORT_REG_STATUS_CMODE_MASK 0xF |
| 95 | #define PORT_REG_STATUS_CMODE_100BASE_X 0x8 |
| 96 | #define PORT_REG_STATUS_CMODE_1000BASE_X 0x9 |
| 97 | #define PORT_REG_STATUS_CMODE_SGMII 0xa |
| 98 | |
Chris Packham | 3da645f | 2016-08-26 17:30:26 +1200 | [diff] [blame] | 99 | #define PORT_REG_PHYS_CTRL_PCS_AN_EN BIT(10) |
| 100 | #define PORT_REG_PHYS_CTRL_PCS_AN_RST BIT(9) |
| 101 | #define PORT_REG_PHYS_CTRL_FC_VALUE BIT(7) |
| 102 | #define PORT_REG_PHYS_CTRL_FC_FORCE BIT(6) |
Kevin Smith | 87b2c4e | 2016-03-31 19:33:12 +0000 | [diff] [blame] | 103 | #define PORT_REG_PHYS_CTRL_LINK_VALUE BIT(5) |
| 104 | #define PORT_REG_PHYS_CTRL_LINK_FORCE BIT(4) |
Chris Packham | 3da645f | 2016-08-26 17:30:26 +1200 | [diff] [blame] | 105 | #define PORT_REG_PHYS_CTRL_DUPLEX_VALUE BIT(3) |
| 106 | #define PORT_REG_PHYS_CTRL_DUPLEX_FORCE BIT(2) |
| 107 | #define PORT_REG_PHYS_CTRL_SPD1000 BIT(1) |
Anatolij Gustschin | 2eadde4 | 2019-10-27 01:14:38 +0200 | [diff] [blame] | 108 | #define PORT_REG_PHYS_CTRL_SPD100 BIT(0) |
Chris Packham | 3da645f | 2016-08-26 17:30:26 +1200 | [diff] [blame] | 109 | #define PORT_REG_PHYS_CTRL_SPD_MASK (BIT(1) | BIT(0)) |
Kevin Smith | 87b2c4e | 2016-03-31 19:33:12 +0000 | [diff] [blame] | 110 | |
| 111 | #define PORT_REG_CTRL_PSTATE_SHIFT 0 |
| 112 | #define PORT_REG_CTRL_PSTATE_WIDTH 2 |
| 113 | |
| 114 | #define PORT_REG_VLAN_ID_DEF_VID_SHIFT 0 |
| 115 | #define PORT_REG_VLAN_ID_DEF_VID_WIDTH 12 |
| 116 | |
| 117 | #define PORT_REG_VLAN_MAP_TABLE_SHIFT 0 |
| 118 | #define PORT_REG_VLAN_MAP_TABLE_WIDTH 11 |
| 119 | |
| 120 | #define SERDES_REG_CTRL_1_FORCE_LINK BIT(10) |
| 121 | |
Kevin Smith | 87b2c4e | 2016-03-31 19:33:12 +0000 | [diff] [blame] | 122 | /* Field values */ |
| 123 | #define PORT_REG_CTRL_PSTATE_DISABLED 0 |
| 124 | #define PORT_REG_CTRL_PSTATE_FORWARD 3 |
| 125 | |
| 126 | #define PHY_REG_CTRL1_ENERGY_DET_OFF 0 |
Anatolij Gustschin | b88aeeb | 2019-10-27 01:14:39 +0200 | [diff] [blame] | 127 | #define PHY_REG_CTRL1_ENERGY_DET_SENSE_PULSE 1 |
Kevin Smith | 87b2c4e | 2016-03-31 19:33:12 +0000 | [diff] [blame] | 128 | #define PHY_REG_CTRL1_ENERGY_DET_SENSE_ONLY 2 |
| 129 | #define PHY_REG_CTRL1_ENERGY_DET_SENSE_XMIT 3 |
| 130 | |
| 131 | /* PHY Status Register */ |
| 132 | #define PHY_REG_STATUS1_SPEED 0xc000 |
| 133 | #define PHY_REG_STATUS1_GBIT 0x8000 |
| 134 | #define PHY_REG_STATUS1_100 0x4000 |
| 135 | #define PHY_REG_STATUS1_DUPLEX 0x2000 |
| 136 | #define PHY_REG_STATUS1_SPDDONE 0x0800 |
| 137 | #define PHY_REG_STATUS1_LINK 0x0400 |
| 138 | #define PHY_REG_STATUS1_ENERGY 0x0010 |
| 139 | |
| 140 | /* |
| 141 | * Macros for building commands for indirect addressing modes. These are valid |
| 142 | * for both the indirect multichip addressing mode and the PHY indirection |
| 143 | * required for the writes to any PHY register. |
| 144 | */ |
| 145 | #define SMI_BUSY BIT(15) |
| 146 | #define SMI_CMD_CLAUSE_22 BIT(12) |
| 147 | #define SMI_CMD_CLAUSE_22_OP_READ (2 << 10) |
| 148 | #define SMI_CMD_CLAUSE_22_OP_WRITE (1 << 10) |
| 149 | |
| 150 | #define SMI_CMD_READ (SMI_BUSY | SMI_CMD_CLAUSE_22 | \ |
| 151 | SMI_CMD_CLAUSE_22_OP_READ) |
| 152 | #define SMI_CMD_WRITE (SMI_BUSY | SMI_CMD_CLAUSE_22 | \ |
| 153 | SMI_CMD_CLAUSE_22_OP_WRITE) |
| 154 | |
| 155 | #define SMI_CMD_ADDR_SHIFT 5 |
| 156 | #define SMI_CMD_ADDR_WIDTH 5 |
| 157 | #define SMI_CMD_REG_SHIFT 0 |
| 158 | #define SMI_CMD_REG_WIDTH 5 |
| 159 | |
| 160 | /* Check for required macros */ |
| 161 | #ifndef CONFIG_MV88E61XX_PHY_PORTS |
| 162 | #error Define CONFIG_MV88E61XX_PHY_PORTS to indicate which physical ports \ |
| 163 | to activate |
| 164 | #endif |
| 165 | #ifndef CONFIG_MV88E61XX_CPU_PORT |
| 166 | #error Define CONFIG_MV88E61XX_CPU_PORT to the port the CPU is attached to |
| 167 | #endif |
| 168 | |
| 169 | /* ID register values for different switch models */ |
Anatolij Gustschin | e477917 | 2019-10-27 01:14:37 +0200 | [diff] [blame] | 170 | #define PORT_SWITCH_ID_6020 0x0200 |
| 171 | #define PORT_SWITCH_ID_6070 0x0700 |
| 172 | #define PORT_SWITCH_ID_6071 0x0710 |
Chris Packham | edc42a1 | 2016-08-26 17:30:25 +1200 | [diff] [blame] | 173 | #define PORT_SWITCH_ID_6096 0x0980 |
| 174 | #define PORT_SWITCH_ID_6097 0x0990 |
Kevin Smith | 87b2c4e | 2016-03-31 19:33:12 +0000 | [diff] [blame] | 175 | #define PORT_SWITCH_ID_6172 0x1720 |
| 176 | #define PORT_SWITCH_ID_6176 0x1760 |
Anatolij Gustschin | e477917 | 2019-10-27 01:14:37 +0200 | [diff] [blame] | 177 | #define PORT_SWITCH_ID_6220 0x2200 |
Kevin Smith | 87b2c4e | 2016-03-31 19:33:12 +0000 | [diff] [blame] | 178 | #define PORT_SWITCH_ID_6240 0x2400 |
Anatolij Gustschin | e477917 | 2019-10-27 01:14:37 +0200 | [diff] [blame] | 179 | #define PORT_SWITCH_ID_6250 0x2500 |
Kevin Smith | 87b2c4e | 2016-03-31 19:33:12 +0000 | [diff] [blame] | 180 | #define PORT_SWITCH_ID_6352 0x3520 |
| 181 | |
| 182 | struct mv88e61xx_phy_priv { |
| 183 | struct mii_dev *mdio_bus; |
| 184 | int smi_addr; |
| 185 | int id; |
Anatolij Gustschin | e477917 | 2019-10-27 01:14:37 +0200 | [diff] [blame] | 186 | int port_count; /* Number of switch ports */ |
| 187 | int port_reg_base; /* Base of the switch port registers */ |
Anatolij Gustschin | 2eadde4 | 2019-10-27 01:14:38 +0200 | [diff] [blame] | 188 | u16 port_stat_link_mask;/* Bitmask for port link status bits */ |
| 189 | u16 port_stat_dup_mask; /* Bitmask for port duplex status bits */ |
| 190 | u8 port_stat_speed_width;/* Width of speed status bitfield */ |
Anatolij Gustschin | e477917 | 2019-10-27 01:14:37 +0200 | [diff] [blame] | 191 | u8 global1; /* Offset of Switch Global 1 registers */ |
| 192 | u8 global2; /* Offset of Switch Global 2 registers */ |
Anatolij Gustschin | b88aeeb | 2019-10-27 01:14:39 +0200 | [diff] [blame] | 193 | u8 phy_ctrl1_en_det_shift; /* 'EDet' bit field offset */ |
| 194 | u8 phy_ctrl1_en_det_width; /* Width of 'EDet' bit field */ |
| 195 | u8 phy_ctrl1_en_det_ctrl; /* 'EDet' control value */ |
Kevin Smith | 87b2c4e | 2016-03-31 19:33:12 +0000 | [diff] [blame] | 196 | }; |
| 197 | |
| 198 | static inline int smi_cmd(int cmd, int addr, int reg) |
| 199 | { |
| 200 | cmd = bitfield_replace(cmd, SMI_CMD_ADDR_SHIFT, SMI_CMD_ADDR_WIDTH, |
| 201 | addr); |
| 202 | cmd = bitfield_replace(cmd, SMI_CMD_REG_SHIFT, SMI_CMD_REG_WIDTH, reg); |
| 203 | return cmd; |
| 204 | } |
| 205 | |
| 206 | static inline int smi_cmd_read(int addr, int reg) |
| 207 | { |
| 208 | return smi_cmd(SMI_CMD_READ, addr, reg); |
| 209 | } |
| 210 | |
| 211 | static inline int smi_cmd_write(int addr, int reg) |
| 212 | { |
| 213 | return smi_cmd(SMI_CMD_WRITE, addr, reg); |
| 214 | } |
| 215 | |
| 216 | __weak int mv88e61xx_hw_reset(struct phy_device *phydev) |
| 217 | { |
| 218 | return 0; |
| 219 | } |
| 220 | |
| 221 | /* Wait for the current SMI indirect command to complete */ |
| 222 | static int mv88e61xx_smi_wait(struct mii_dev *bus, int smi_addr) |
| 223 | { |
| 224 | int val; |
| 225 | u32 timeout = 100; |
| 226 | |
| 227 | do { |
| 228 | val = bus->read(bus, smi_addr, MDIO_DEVAD_NONE, SMI_CMD_REG); |
| 229 | if (val >= 0 && (val & SMI_BUSY) == 0) |
| 230 | return 0; |
| 231 | |
| 232 | mdelay(1); |
| 233 | } while (--timeout); |
| 234 | |
| 235 | puts("SMI busy timeout\n"); |
| 236 | return -ETIMEDOUT; |
| 237 | } |
| 238 | |
| 239 | /* |
| 240 | * The mv88e61xx has three types of addresses: the smi bus address, the device |
| 241 | * address, and the register address. The smi bus address distinguishes it on |
| 242 | * the smi bus from other PHYs or switches. The device address determines |
| 243 | * which on-chip register set you are reading/writing (the various PHYs, their |
| 244 | * associated ports, or global configuration registers). The register address |
| 245 | * is the offset of the register you are reading/writing. |
| 246 | * |
| 247 | * When the mv88e61xx is hardware configured to have address zero, it behaves in |
| 248 | * single-chip addressing mode, where it responds to all SMI addresses, using |
| 249 | * the smi address as its device address. This obviously only works when this |
| 250 | * is the only chip on the SMI bus. This allows the driver to access device |
| 251 | * registers without using indirection. When the chip is configured to a |
| 252 | * non-zero address, it only responds to that SMI address and requires indirect |
| 253 | * writes to access the different device addresses. |
| 254 | */ |
| 255 | static int mv88e61xx_reg_read(struct phy_device *phydev, int dev, int reg) |
| 256 | { |
| 257 | struct mv88e61xx_phy_priv *priv = phydev->priv; |
| 258 | struct mii_dev *mdio_bus = priv->mdio_bus; |
| 259 | int smi_addr = priv->smi_addr; |
| 260 | int res; |
| 261 | |
| 262 | /* In single-chip mode, the device can be addressed directly */ |
| 263 | if (smi_addr == 0) |
| 264 | return mdio_bus->read(mdio_bus, dev, MDIO_DEVAD_NONE, reg); |
| 265 | |
| 266 | /* Wait for the bus to become free */ |
| 267 | res = mv88e61xx_smi_wait(mdio_bus, smi_addr); |
| 268 | if (res < 0) |
| 269 | return res; |
| 270 | |
| 271 | /* Issue the read command */ |
| 272 | res = mdio_bus->write(mdio_bus, smi_addr, MDIO_DEVAD_NONE, SMI_CMD_REG, |
| 273 | smi_cmd_read(dev, reg)); |
| 274 | if (res < 0) |
| 275 | return res; |
| 276 | |
| 277 | /* Wait for the read command to complete */ |
| 278 | res = mv88e61xx_smi_wait(mdio_bus, smi_addr); |
| 279 | if (res < 0) |
| 280 | return res; |
| 281 | |
| 282 | /* Read the data */ |
| 283 | res = mdio_bus->read(mdio_bus, smi_addr, MDIO_DEVAD_NONE, SMI_DATA_REG); |
| 284 | if (res < 0) |
| 285 | return res; |
| 286 | |
| 287 | return bitfield_extract(res, 0, 16); |
| 288 | } |
| 289 | |
| 290 | /* See the comment above mv88e61xx_reg_read */ |
| 291 | static int mv88e61xx_reg_write(struct phy_device *phydev, int dev, int reg, |
| 292 | u16 val) |
| 293 | { |
| 294 | struct mv88e61xx_phy_priv *priv = phydev->priv; |
| 295 | struct mii_dev *mdio_bus = priv->mdio_bus; |
| 296 | int smi_addr = priv->smi_addr; |
| 297 | int res; |
| 298 | |
| 299 | /* In single-chip mode, the device can be addressed directly */ |
| 300 | if (smi_addr == 0) { |
| 301 | return mdio_bus->write(mdio_bus, dev, MDIO_DEVAD_NONE, reg, |
| 302 | val); |
| 303 | } |
| 304 | |
| 305 | /* Wait for the bus to become free */ |
| 306 | res = mv88e61xx_smi_wait(mdio_bus, smi_addr); |
| 307 | if (res < 0) |
| 308 | return res; |
| 309 | |
| 310 | /* Set the data to write */ |
| 311 | res = mdio_bus->write(mdio_bus, smi_addr, MDIO_DEVAD_NONE, |
| 312 | SMI_DATA_REG, val); |
| 313 | if (res < 0) |
| 314 | return res; |
| 315 | |
| 316 | /* Issue the write command */ |
| 317 | res = mdio_bus->write(mdio_bus, smi_addr, MDIO_DEVAD_NONE, SMI_CMD_REG, |
| 318 | smi_cmd_write(dev, reg)); |
| 319 | if (res < 0) |
| 320 | return res; |
| 321 | |
| 322 | /* Wait for the write command to complete */ |
| 323 | res = mv88e61xx_smi_wait(mdio_bus, smi_addr); |
| 324 | if (res < 0) |
| 325 | return res; |
| 326 | |
| 327 | return 0; |
| 328 | } |
| 329 | |
| 330 | static int mv88e61xx_phy_wait(struct phy_device *phydev) |
| 331 | { |
Anatolij Gustschin | e477917 | 2019-10-27 01:14:37 +0200 | [diff] [blame] | 332 | struct mv88e61xx_phy_priv *priv = phydev->priv; |
Kevin Smith | 87b2c4e | 2016-03-31 19:33:12 +0000 | [diff] [blame] | 333 | int val; |
| 334 | u32 timeout = 100; |
| 335 | |
| 336 | do { |
Anatolij Gustschin | e477917 | 2019-10-27 01:14:37 +0200 | [diff] [blame] | 337 | val = mv88e61xx_reg_read(phydev, priv->global2, |
Kevin Smith | 87b2c4e | 2016-03-31 19:33:12 +0000 | [diff] [blame] | 338 | GLOBAL2_REG_PHY_CMD); |
| 339 | if (val >= 0 && (val & SMI_BUSY) == 0) |
| 340 | return 0; |
| 341 | |
| 342 | mdelay(1); |
| 343 | } while (--timeout); |
| 344 | |
| 345 | return -ETIMEDOUT; |
| 346 | } |
| 347 | |
| 348 | static int mv88e61xx_phy_read_indirect(struct mii_dev *smi_wrapper, int dev, |
| 349 | int devad, int reg) |
| 350 | { |
Anatolij Gustschin | e477917 | 2019-10-27 01:14:37 +0200 | [diff] [blame] | 351 | struct mv88e61xx_phy_priv *priv; |
Kevin Smith | 87b2c4e | 2016-03-31 19:33:12 +0000 | [diff] [blame] | 352 | struct phy_device *phydev; |
| 353 | int res; |
| 354 | |
| 355 | phydev = (struct phy_device *)smi_wrapper->priv; |
Anatolij Gustschin | e477917 | 2019-10-27 01:14:37 +0200 | [diff] [blame] | 356 | priv = phydev->priv; |
Kevin Smith | 87b2c4e | 2016-03-31 19:33:12 +0000 | [diff] [blame] | 357 | |
| 358 | /* Issue command to read */ |
Anatolij Gustschin | e477917 | 2019-10-27 01:14:37 +0200 | [diff] [blame] | 359 | res = mv88e61xx_reg_write(phydev, priv->global2, |
Kevin Smith | 87b2c4e | 2016-03-31 19:33:12 +0000 | [diff] [blame] | 360 | GLOBAL2_REG_PHY_CMD, |
| 361 | smi_cmd_read(dev, reg)); |
| 362 | |
| 363 | /* Wait for data to be read */ |
| 364 | res = mv88e61xx_phy_wait(phydev); |
| 365 | if (res < 0) |
| 366 | return res; |
| 367 | |
| 368 | /* Read retrieved data */ |
Anatolij Gustschin | e477917 | 2019-10-27 01:14:37 +0200 | [diff] [blame] | 369 | return mv88e61xx_reg_read(phydev, priv->global2, |
Kevin Smith | 87b2c4e | 2016-03-31 19:33:12 +0000 | [diff] [blame] | 370 | GLOBAL2_REG_PHY_DATA); |
| 371 | } |
| 372 | |
| 373 | static int mv88e61xx_phy_write_indirect(struct mii_dev *smi_wrapper, int dev, |
| 374 | int devad, int reg, u16 data) |
| 375 | { |
Anatolij Gustschin | e477917 | 2019-10-27 01:14:37 +0200 | [diff] [blame] | 376 | struct mv88e61xx_phy_priv *priv; |
Kevin Smith | 87b2c4e | 2016-03-31 19:33:12 +0000 | [diff] [blame] | 377 | struct phy_device *phydev; |
| 378 | int res; |
| 379 | |
| 380 | phydev = (struct phy_device *)smi_wrapper->priv; |
Anatolij Gustschin | e477917 | 2019-10-27 01:14:37 +0200 | [diff] [blame] | 381 | priv = phydev->priv; |
Kevin Smith | 87b2c4e | 2016-03-31 19:33:12 +0000 | [diff] [blame] | 382 | |
| 383 | /* Set the data to write */ |
Anatolij Gustschin | e477917 | 2019-10-27 01:14:37 +0200 | [diff] [blame] | 384 | res = mv88e61xx_reg_write(phydev, priv->global2, |
Kevin Smith | 87b2c4e | 2016-03-31 19:33:12 +0000 | [diff] [blame] | 385 | GLOBAL2_REG_PHY_DATA, data); |
| 386 | if (res < 0) |
| 387 | return res; |
| 388 | /* Issue the write command */ |
Anatolij Gustschin | e477917 | 2019-10-27 01:14:37 +0200 | [diff] [blame] | 389 | res = mv88e61xx_reg_write(phydev, priv->global2, |
Kevin Smith | 87b2c4e | 2016-03-31 19:33:12 +0000 | [diff] [blame] | 390 | GLOBAL2_REG_PHY_CMD, |
| 391 | smi_cmd_write(dev, reg)); |
| 392 | if (res < 0) |
| 393 | return res; |
| 394 | |
| 395 | /* Wait for command to complete */ |
| 396 | return mv88e61xx_phy_wait(phydev); |
| 397 | } |
| 398 | |
| 399 | /* Wrapper function to make calls to phy_read_indirect simpler */ |
| 400 | static int mv88e61xx_phy_read(struct phy_device *phydev, int phy, int reg) |
| 401 | { |
| 402 | return mv88e61xx_phy_read_indirect(phydev->bus, DEVADDR_PHY(phy), |
| 403 | MDIO_DEVAD_NONE, reg); |
| 404 | } |
| 405 | |
| 406 | /* Wrapper function to make calls to phy_read_indirect simpler */ |
| 407 | static int mv88e61xx_phy_write(struct phy_device *phydev, int phy, |
| 408 | int reg, u16 val) |
| 409 | { |
| 410 | return mv88e61xx_phy_write_indirect(phydev->bus, DEVADDR_PHY(phy), |
| 411 | MDIO_DEVAD_NONE, reg, val); |
| 412 | } |
| 413 | |
| 414 | static int mv88e61xx_port_read(struct phy_device *phydev, u8 port, u8 reg) |
| 415 | { |
Anatolij Gustschin | e477917 | 2019-10-27 01:14:37 +0200 | [diff] [blame] | 416 | struct mv88e61xx_phy_priv *priv = phydev->priv; |
| 417 | |
| 418 | return mv88e61xx_reg_read(phydev, priv->port_reg_base + port, reg); |
Kevin Smith | 87b2c4e | 2016-03-31 19:33:12 +0000 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | static int mv88e61xx_port_write(struct phy_device *phydev, u8 port, u8 reg, |
| 422 | u16 val) |
| 423 | { |
Anatolij Gustschin | e477917 | 2019-10-27 01:14:37 +0200 | [diff] [blame] | 424 | struct mv88e61xx_phy_priv *priv = phydev->priv; |
| 425 | |
| 426 | return mv88e61xx_reg_write(phydev, priv->port_reg_base + port, |
| 427 | reg, val); |
Kevin Smith | 87b2c4e | 2016-03-31 19:33:12 +0000 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | static int mv88e61xx_set_page(struct phy_device *phydev, u8 phy, u8 page) |
| 431 | { |
| 432 | return mv88e61xx_phy_write(phydev, phy, PHY_REG_PAGE, page); |
| 433 | } |
| 434 | |
| 435 | static int mv88e61xx_get_switch_id(struct phy_device *phydev) |
| 436 | { |
| 437 | int res; |
| 438 | |
| 439 | res = mv88e61xx_port_read(phydev, 0, PORT_REG_SWITCH_ID); |
| 440 | if (res < 0) |
| 441 | return res; |
| 442 | return res & 0xfff0; |
| 443 | } |
| 444 | |
| 445 | static bool mv88e61xx_6352_family(struct phy_device *phydev) |
| 446 | { |
| 447 | struct mv88e61xx_phy_priv *priv = phydev->priv; |
| 448 | |
| 449 | switch (priv->id) { |
| 450 | case PORT_SWITCH_ID_6172: |
| 451 | case PORT_SWITCH_ID_6176: |
| 452 | case PORT_SWITCH_ID_6240: |
| 453 | case PORT_SWITCH_ID_6352: |
| 454 | return true; |
| 455 | } |
| 456 | return false; |
| 457 | } |
| 458 | |
| 459 | static int mv88e61xx_get_cmode(struct phy_device *phydev, u8 port) |
| 460 | { |
| 461 | int res; |
| 462 | |
| 463 | res = mv88e61xx_port_read(phydev, port, PORT_REG_STATUS); |
| 464 | if (res < 0) |
| 465 | return res; |
| 466 | return res & PORT_REG_STATUS_CMODE_MASK; |
| 467 | } |
| 468 | |
| 469 | static int mv88e61xx_parse_status(struct phy_device *phydev) |
| 470 | { |
| 471 | unsigned int speed; |
| 472 | unsigned int mii_reg; |
| 473 | |
| 474 | mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, PHY_REG_STATUS1); |
| 475 | |
| 476 | if ((mii_reg & PHY_REG_STATUS1_LINK) && |
| 477 | !(mii_reg & PHY_REG_STATUS1_SPDDONE)) { |
| 478 | int i = 0; |
| 479 | |
| 480 | puts("Waiting for PHY realtime link"); |
| 481 | while (!(mii_reg & PHY_REG_STATUS1_SPDDONE)) { |
| 482 | /* Timeout reached ? */ |
| 483 | if (i > PHY_AUTONEGOTIATE_TIMEOUT) { |
| 484 | puts(" TIMEOUT !\n"); |
| 485 | phydev->link = 0; |
| 486 | break; |
| 487 | } |
| 488 | |
| 489 | if ((i++ % 1000) == 0) |
| 490 | putc('.'); |
| 491 | udelay(1000); |
| 492 | mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, |
| 493 | PHY_REG_STATUS1); |
| 494 | } |
| 495 | puts(" done\n"); |
| 496 | udelay(500000); /* another 500 ms (results in faster booting) */ |
| 497 | } else { |
| 498 | if (mii_reg & PHY_REG_STATUS1_LINK) |
| 499 | phydev->link = 1; |
| 500 | else |
| 501 | phydev->link = 0; |
| 502 | } |
| 503 | |
| 504 | if (mii_reg & PHY_REG_STATUS1_DUPLEX) |
| 505 | phydev->duplex = DUPLEX_FULL; |
| 506 | else |
| 507 | phydev->duplex = DUPLEX_HALF; |
| 508 | |
| 509 | speed = mii_reg & PHY_REG_STATUS1_SPEED; |
| 510 | |
| 511 | switch (speed) { |
| 512 | case PHY_REG_STATUS1_GBIT: |
| 513 | phydev->speed = SPEED_1000; |
| 514 | break; |
| 515 | case PHY_REG_STATUS1_100: |
| 516 | phydev->speed = SPEED_100; |
| 517 | break; |
| 518 | default: |
| 519 | phydev->speed = SPEED_10; |
| 520 | break; |
| 521 | } |
| 522 | |
| 523 | return 0; |
| 524 | } |
| 525 | |
| 526 | static int mv88e61xx_switch_reset(struct phy_device *phydev) |
| 527 | { |
Anatolij Gustschin | e477917 | 2019-10-27 01:14:37 +0200 | [diff] [blame] | 528 | struct mv88e61xx_phy_priv *priv = phydev->priv; |
Kevin Smith | 87b2c4e | 2016-03-31 19:33:12 +0000 | [diff] [blame] | 529 | int time; |
| 530 | int val; |
| 531 | u8 port; |
| 532 | |
| 533 | /* Disable all ports */ |
Anatolij Gustschin | e477917 | 2019-10-27 01:14:37 +0200 | [diff] [blame] | 534 | for (port = 0; port < priv->port_count; port++) { |
Kevin Smith | 87b2c4e | 2016-03-31 19:33:12 +0000 | [diff] [blame] | 535 | val = mv88e61xx_port_read(phydev, port, PORT_REG_CTRL); |
| 536 | if (val < 0) |
| 537 | return val; |
| 538 | val = bitfield_replace(val, PORT_REG_CTRL_PSTATE_SHIFT, |
| 539 | PORT_REG_CTRL_PSTATE_WIDTH, |
| 540 | PORT_REG_CTRL_PSTATE_DISABLED); |
| 541 | val = mv88e61xx_port_write(phydev, port, PORT_REG_CTRL, val); |
| 542 | if (val < 0) |
| 543 | return val; |
| 544 | } |
| 545 | |
| 546 | /* Wait 2 ms for queues to drain */ |
| 547 | udelay(2000); |
| 548 | |
| 549 | /* Reset switch */ |
Anatolij Gustschin | e477917 | 2019-10-27 01:14:37 +0200 | [diff] [blame] | 550 | val = mv88e61xx_reg_read(phydev, priv->global1, GLOBAL1_CTRL); |
Kevin Smith | 87b2c4e | 2016-03-31 19:33:12 +0000 | [diff] [blame] | 551 | if (val < 0) |
| 552 | return val; |
| 553 | val |= GLOBAL1_CTRL_SWRESET; |
Anatolij Gustschin | e477917 | 2019-10-27 01:14:37 +0200 | [diff] [blame] | 554 | val = mv88e61xx_reg_write(phydev, priv->global1, |
| 555 | GLOBAL1_CTRL, val); |
Kevin Smith | 87b2c4e | 2016-03-31 19:33:12 +0000 | [diff] [blame] | 556 | if (val < 0) |
| 557 | return val; |
| 558 | |
| 559 | /* Wait up to 1 second for switch reset complete */ |
| 560 | for (time = 1000; time; time--) { |
Anatolij Gustschin | e477917 | 2019-10-27 01:14:37 +0200 | [diff] [blame] | 561 | val = mv88e61xx_reg_read(phydev, priv->global1, |
| 562 | GLOBAL1_CTRL); |
Kevin Smith | 87b2c4e | 2016-03-31 19:33:12 +0000 | [diff] [blame] | 563 | if (val >= 0 && ((val & GLOBAL1_CTRL_SWRESET) == 0)) |
| 564 | break; |
| 565 | udelay(1000); |
| 566 | } |
| 567 | if (!time) |
| 568 | return -ETIMEDOUT; |
| 569 | |
| 570 | return 0; |
| 571 | } |
| 572 | |
| 573 | static int mv88e61xx_serdes_init(struct phy_device *phydev) |
| 574 | { |
| 575 | int val; |
| 576 | |
| 577 | val = mv88e61xx_set_page(phydev, DEVADDR_SERDES, PHY_PAGE_SERDES); |
| 578 | if (val < 0) |
| 579 | return val; |
| 580 | |
| 581 | /* Power up serdes module */ |
| 582 | val = mv88e61xx_phy_read(phydev, DEVADDR_SERDES, MII_BMCR); |
| 583 | if (val < 0) |
| 584 | return val; |
| 585 | val &= ~(BMCR_PDOWN); |
| 586 | val = mv88e61xx_phy_write(phydev, DEVADDR_SERDES, MII_BMCR, val); |
| 587 | if (val < 0) |
| 588 | return val; |
| 589 | |
| 590 | return 0; |
| 591 | } |
| 592 | |
| 593 | static int mv88e61xx_port_enable(struct phy_device *phydev, u8 port) |
| 594 | { |
| 595 | int val; |
| 596 | |
| 597 | val = mv88e61xx_port_read(phydev, port, PORT_REG_CTRL); |
| 598 | if (val < 0) |
| 599 | return val; |
| 600 | val = bitfield_replace(val, PORT_REG_CTRL_PSTATE_SHIFT, |
| 601 | PORT_REG_CTRL_PSTATE_WIDTH, |
| 602 | PORT_REG_CTRL_PSTATE_FORWARD); |
| 603 | val = mv88e61xx_port_write(phydev, port, PORT_REG_CTRL, val); |
| 604 | if (val < 0) |
| 605 | return val; |
| 606 | |
| 607 | return 0; |
| 608 | } |
| 609 | |
| 610 | static int mv88e61xx_port_set_vlan(struct phy_device *phydev, u8 port, |
Chris Packham | edc42a1 | 2016-08-26 17:30:25 +1200 | [diff] [blame] | 611 | u16 mask) |
Kevin Smith | 87b2c4e | 2016-03-31 19:33:12 +0000 | [diff] [blame] | 612 | { |
| 613 | int val; |
| 614 | |
| 615 | /* Set VID to port number plus one */ |
| 616 | val = mv88e61xx_port_read(phydev, port, PORT_REG_VLAN_ID); |
| 617 | if (val < 0) |
| 618 | return val; |
| 619 | val = bitfield_replace(val, PORT_REG_VLAN_ID_DEF_VID_SHIFT, |
| 620 | PORT_REG_VLAN_ID_DEF_VID_WIDTH, |
| 621 | port + 1); |
| 622 | val = mv88e61xx_port_write(phydev, port, PORT_REG_VLAN_ID, val); |
| 623 | if (val < 0) |
| 624 | return val; |
| 625 | |
| 626 | /* Set VID mask */ |
| 627 | val = mv88e61xx_port_read(phydev, port, PORT_REG_VLAN_MAP); |
| 628 | if (val < 0) |
| 629 | return val; |
| 630 | val = bitfield_replace(val, PORT_REG_VLAN_MAP_TABLE_SHIFT, |
| 631 | PORT_REG_VLAN_MAP_TABLE_WIDTH, |
| 632 | mask); |
| 633 | val = mv88e61xx_port_write(phydev, port, PORT_REG_VLAN_MAP, val); |
| 634 | if (val < 0) |
| 635 | return val; |
| 636 | |
| 637 | return 0; |
| 638 | } |
| 639 | |
| 640 | static int mv88e61xx_read_port_config(struct phy_device *phydev, u8 port) |
| 641 | { |
Anatolij Gustschin | 2eadde4 | 2019-10-27 01:14:38 +0200 | [diff] [blame] | 642 | struct mv88e61xx_phy_priv *priv = phydev->priv; |
Kevin Smith | 87b2c4e | 2016-03-31 19:33:12 +0000 | [diff] [blame] | 643 | int res; |
| 644 | int val; |
| 645 | bool forced = false; |
| 646 | |
| 647 | val = mv88e61xx_port_read(phydev, port, PORT_REG_STATUS); |
| 648 | if (val < 0) |
| 649 | return val; |
Anatolij Gustschin | 2eadde4 | 2019-10-27 01:14:38 +0200 | [diff] [blame] | 650 | if (!(val & priv->port_stat_link_mask)) { |
Kevin Smith | 87b2c4e | 2016-03-31 19:33:12 +0000 | [diff] [blame] | 651 | /* Temporarily force link to read port configuration */ |
| 652 | u32 timeout = 100; |
| 653 | forced = true; |
| 654 | |
| 655 | val = mv88e61xx_port_read(phydev, port, PORT_REG_PHYS_CTRL); |
| 656 | if (val < 0) |
| 657 | return val; |
| 658 | val |= (PORT_REG_PHYS_CTRL_LINK_FORCE | |
| 659 | PORT_REG_PHYS_CTRL_LINK_VALUE); |
| 660 | val = mv88e61xx_port_write(phydev, port, PORT_REG_PHYS_CTRL, |
| 661 | val); |
| 662 | if (val < 0) |
| 663 | return val; |
| 664 | |
| 665 | /* Wait for status register to reflect forced link */ |
| 666 | do { |
| 667 | val = mv88e61xx_port_read(phydev, port, |
| 668 | PORT_REG_STATUS); |
Tom Rini | 0941865 | 2017-05-08 22:14:32 -0400 | [diff] [blame] | 669 | if (val < 0) { |
| 670 | res = -EIO; |
Kevin Smith | 87b2c4e | 2016-03-31 19:33:12 +0000 | [diff] [blame] | 671 | goto unforce; |
Tom Rini | 0941865 | 2017-05-08 22:14:32 -0400 | [diff] [blame] | 672 | } |
Anatolij Gustschin | 2eadde4 | 2019-10-27 01:14:38 +0200 | [diff] [blame] | 673 | if (val & priv->port_stat_link_mask) |
Kevin Smith | 87b2c4e | 2016-03-31 19:33:12 +0000 | [diff] [blame] | 674 | break; |
| 675 | } while (--timeout); |
| 676 | |
| 677 | if (timeout == 0) { |
| 678 | res = -ETIMEDOUT; |
| 679 | goto unforce; |
| 680 | } |
| 681 | } |
| 682 | |
Anatolij Gustschin | 2eadde4 | 2019-10-27 01:14:38 +0200 | [diff] [blame] | 683 | if (val & priv->port_stat_dup_mask) |
Kevin Smith | 87b2c4e | 2016-03-31 19:33:12 +0000 | [diff] [blame] | 684 | phydev->duplex = DUPLEX_FULL; |
| 685 | else |
| 686 | phydev->duplex = DUPLEX_HALF; |
| 687 | |
| 688 | val = bitfield_extract(val, PORT_REG_STATUS_SPEED_SHIFT, |
Anatolij Gustschin | 2eadde4 | 2019-10-27 01:14:38 +0200 | [diff] [blame] | 689 | priv->port_stat_speed_width); |
Kevin Smith | 87b2c4e | 2016-03-31 19:33:12 +0000 | [diff] [blame] | 690 | switch (val) { |
| 691 | case PORT_REG_STATUS_SPEED_1000: |
| 692 | phydev->speed = SPEED_1000; |
| 693 | break; |
| 694 | case PORT_REG_STATUS_SPEED_100: |
| 695 | phydev->speed = SPEED_100; |
| 696 | break; |
| 697 | default: |
| 698 | phydev->speed = SPEED_10; |
| 699 | break; |
| 700 | } |
| 701 | |
| 702 | res = 0; |
| 703 | |
| 704 | unforce: |
| 705 | if (forced) { |
| 706 | val = mv88e61xx_port_read(phydev, port, PORT_REG_PHYS_CTRL); |
| 707 | if (val < 0) |
| 708 | return val; |
| 709 | val &= ~(PORT_REG_PHYS_CTRL_LINK_FORCE | |
| 710 | PORT_REG_PHYS_CTRL_LINK_VALUE); |
| 711 | val = mv88e61xx_port_write(phydev, port, PORT_REG_PHYS_CTRL, |
| 712 | val); |
| 713 | if (val < 0) |
| 714 | return val; |
| 715 | } |
| 716 | |
| 717 | return res; |
| 718 | } |
| 719 | |
Chris Packham | 2178861 | 2018-06-03 16:21:26 +1200 | [diff] [blame] | 720 | static int mv88e61xx_fixed_port_setup(struct phy_device *phydev, u8 port) |
| 721 | { |
Anatolij Gustschin | 2eadde4 | 2019-10-27 01:14:38 +0200 | [diff] [blame] | 722 | struct mv88e61xx_phy_priv *priv = phydev->priv; |
Chris Packham | 2178861 | 2018-06-03 16:21:26 +1200 | [diff] [blame] | 723 | int val; |
| 724 | |
| 725 | val = mv88e61xx_port_read(phydev, port, PORT_REG_PHYS_CTRL); |
| 726 | if (val < 0) |
| 727 | return val; |
| 728 | |
| 729 | val &= ~(PORT_REG_PHYS_CTRL_SPD_MASK | |
Anatolij Gustschin | 2eadde4 | 2019-10-27 01:14:38 +0200 | [diff] [blame] | 730 | PORT_REG_PHYS_CTRL_FC_VALUE | |
| 731 | PORT_REG_PHYS_CTRL_FC_FORCE); |
| 732 | val |= PORT_REG_PHYS_CTRL_FC_FORCE | |
Chris Packham | 2178861 | 2018-06-03 16:21:26 +1200 | [diff] [blame] | 733 | PORT_REG_PHYS_CTRL_DUPLEX_VALUE | |
Anatolij Gustschin | 2eadde4 | 2019-10-27 01:14:38 +0200 | [diff] [blame] | 734 | PORT_REG_PHYS_CTRL_DUPLEX_FORCE; |
| 735 | |
| 736 | if (priv->id == PORT_SWITCH_ID_6071) { |
| 737 | val |= PORT_REG_PHYS_CTRL_SPD100; |
| 738 | } else { |
| 739 | val |= PORT_REG_PHYS_CTRL_PCS_AN_EN | |
| 740 | PORT_REG_PHYS_CTRL_PCS_AN_RST | |
| 741 | PORT_REG_PHYS_CTRL_SPD1000; |
| 742 | } |
Chris Packham | 2178861 | 2018-06-03 16:21:26 +1200 | [diff] [blame] | 743 | |
| 744 | if (port == CONFIG_MV88E61XX_CPU_PORT) |
| 745 | val |= PORT_REG_PHYS_CTRL_LINK_VALUE | |
| 746 | PORT_REG_PHYS_CTRL_LINK_FORCE; |
| 747 | |
| 748 | return mv88e61xx_port_write(phydev, port, PORT_REG_PHYS_CTRL, |
| 749 | val); |
| 750 | } |
| 751 | |
Kevin Smith | 87b2c4e | 2016-03-31 19:33:12 +0000 | [diff] [blame] | 752 | static int mv88e61xx_set_cpu_port(struct phy_device *phydev) |
| 753 | { |
Anatolij Gustschin | e477917 | 2019-10-27 01:14:37 +0200 | [diff] [blame] | 754 | struct mv88e61xx_phy_priv *priv = phydev->priv; |
Kevin Smith | 87b2c4e | 2016-03-31 19:33:12 +0000 | [diff] [blame] | 755 | int val; |
| 756 | |
| 757 | /* Set CPUDest */ |
Anatolij Gustschin | e477917 | 2019-10-27 01:14:37 +0200 | [diff] [blame] | 758 | val = mv88e61xx_reg_read(phydev, priv->global1, GLOBAL1_MON_CTRL); |
Kevin Smith | 87b2c4e | 2016-03-31 19:33:12 +0000 | [diff] [blame] | 759 | if (val < 0) |
| 760 | return val; |
| 761 | val = bitfield_replace(val, GLOBAL1_MON_CTRL_CPUDEST_SHIFT, |
| 762 | GLOBAL1_MON_CTRL_CPUDEST_WIDTH, |
| 763 | CONFIG_MV88E61XX_CPU_PORT); |
Anatolij Gustschin | e477917 | 2019-10-27 01:14:37 +0200 | [diff] [blame] | 764 | val = mv88e61xx_reg_write(phydev, priv->global1, |
| 765 | GLOBAL1_MON_CTRL, val); |
Kevin Smith | 87b2c4e | 2016-03-31 19:33:12 +0000 | [diff] [blame] | 766 | if (val < 0) |
| 767 | return val; |
| 768 | |
| 769 | /* Allow CPU to route to any port */ |
Anatolij Gustschin | e477917 | 2019-10-27 01:14:37 +0200 | [diff] [blame] | 770 | val = PORT_MASK(priv->port_count) & ~(1 << CONFIG_MV88E61XX_CPU_PORT); |
Kevin Smith | 87b2c4e | 2016-03-31 19:33:12 +0000 | [diff] [blame] | 771 | val = mv88e61xx_port_set_vlan(phydev, CONFIG_MV88E61XX_CPU_PORT, val); |
| 772 | if (val < 0) |
| 773 | return val; |
| 774 | |
| 775 | /* Enable CPU port */ |
| 776 | val = mv88e61xx_port_enable(phydev, CONFIG_MV88E61XX_CPU_PORT); |
| 777 | if (val < 0) |
| 778 | return val; |
| 779 | |
| 780 | val = mv88e61xx_read_port_config(phydev, CONFIG_MV88E61XX_CPU_PORT); |
| 781 | if (val < 0) |
| 782 | return val; |
| 783 | |
| 784 | /* If CPU is connected to serdes, initialize serdes */ |
| 785 | if (mv88e61xx_6352_family(phydev)) { |
| 786 | val = mv88e61xx_get_cmode(phydev, CONFIG_MV88E61XX_CPU_PORT); |
| 787 | if (val < 0) |
| 788 | return val; |
| 789 | if (val == PORT_REG_STATUS_CMODE_100BASE_X || |
| 790 | val == PORT_REG_STATUS_CMODE_1000BASE_X || |
| 791 | val == PORT_REG_STATUS_CMODE_SGMII) { |
| 792 | val = mv88e61xx_serdes_init(phydev); |
| 793 | if (val < 0) |
| 794 | return val; |
| 795 | } |
Chris Packham | 2178861 | 2018-06-03 16:21:26 +1200 | [diff] [blame] | 796 | } else { |
| 797 | val = mv88e61xx_fixed_port_setup(phydev, |
| 798 | CONFIG_MV88E61XX_CPU_PORT); |
| 799 | if (val < 0) |
| 800 | return val; |
Kevin Smith | 87b2c4e | 2016-03-31 19:33:12 +0000 | [diff] [blame] | 801 | } |
| 802 | |
| 803 | return 0; |
| 804 | } |
| 805 | |
| 806 | static int mv88e61xx_switch_init(struct phy_device *phydev) |
| 807 | { |
| 808 | static int init; |
| 809 | int res; |
| 810 | |
| 811 | if (init) |
| 812 | return 0; |
| 813 | |
| 814 | res = mv88e61xx_switch_reset(phydev); |
| 815 | if (res < 0) |
| 816 | return res; |
| 817 | |
| 818 | res = mv88e61xx_set_cpu_port(phydev); |
| 819 | if (res < 0) |
| 820 | return res; |
| 821 | |
| 822 | init = 1; |
| 823 | |
| 824 | return 0; |
| 825 | } |
| 826 | |
| 827 | static int mv88e61xx_phy_enable(struct phy_device *phydev, u8 phy) |
| 828 | { |
| 829 | int val; |
| 830 | |
| 831 | val = mv88e61xx_phy_read(phydev, phy, MII_BMCR); |
| 832 | if (val < 0) |
| 833 | return val; |
| 834 | val &= ~(BMCR_PDOWN); |
| 835 | val = mv88e61xx_phy_write(phydev, phy, MII_BMCR, val); |
| 836 | if (val < 0) |
| 837 | return val; |
| 838 | |
| 839 | return 0; |
| 840 | } |
| 841 | |
| 842 | static int mv88e61xx_phy_setup(struct phy_device *phydev, u8 phy) |
| 843 | { |
Anatolij Gustschin | b88aeeb | 2019-10-27 01:14:39 +0200 | [diff] [blame] | 844 | struct mv88e61xx_phy_priv *priv = phydev->priv; |
Kevin Smith | 87b2c4e | 2016-03-31 19:33:12 +0000 | [diff] [blame] | 845 | int val; |
| 846 | |
| 847 | /* |
| 848 | * Enable energy-detect sensing on PHY, used to determine when a PHY |
| 849 | * port is physically connected |
| 850 | */ |
| 851 | val = mv88e61xx_phy_read(phydev, phy, PHY_REG_CTRL1); |
| 852 | if (val < 0) |
| 853 | return val; |
Anatolij Gustschin | b88aeeb | 2019-10-27 01:14:39 +0200 | [diff] [blame] | 854 | val = bitfield_replace(val, priv->phy_ctrl1_en_det_shift, |
| 855 | priv->phy_ctrl1_en_det_width, |
| 856 | priv->phy_ctrl1_en_det_ctrl); |
Kevin Smith | 87b2c4e | 2016-03-31 19:33:12 +0000 | [diff] [blame] | 857 | val = mv88e61xx_phy_write(phydev, phy, PHY_REG_CTRL1, val); |
| 858 | if (val < 0) |
| 859 | return val; |
| 860 | |
| 861 | return 0; |
| 862 | } |
| 863 | |
| 864 | static int mv88e61xx_phy_config_port(struct phy_device *phydev, u8 phy) |
| 865 | { |
| 866 | int val; |
| 867 | |
| 868 | val = mv88e61xx_port_enable(phydev, phy); |
| 869 | if (val < 0) |
| 870 | return val; |
| 871 | |
| 872 | val = mv88e61xx_port_set_vlan(phydev, phy, |
| 873 | 1 << CONFIG_MV88E61XX_CPU_PORT); |
| 874 | if (val < 0) |
| 875 | return val; |
| 876 | |
| 877 | return 0; |
| 878 | } |
| 879 | |
Anatolij Gustschin | e477917 | 2019-10-27 01:14:37 +0200 | [diff] [blame] | 880 | /* |
| 881 | * This function is used to pre-configure the required register |
| 882 | * offsets, so that the indirect register access to the PHY registers |
| 883 | * is possible. This is necessary to be able to read the PHY ID |
| 884 | * while driver probing or in get_phy_id(). The globalN register |
| 885 | * offsets must be initialized correctly for a detected switch, |
| 886 | * otherwise detection of the PHY ID won't work! |
| 887 | */ |
| 888 | static int mv88e61xx_priv_reg_offs_pre_init(struct phy_device *phydev) |
| 889 | { |
| 890 | struct mv88e61xx_phy_priv *priv = phydev->priv; |
| 891 | |
| 892 | /* |
| 893 | * Initial 'port_reg_base' value must be an offset of existing |
| 894 | * port register, then reading the ID should succeed. First, try |
| 895 | * to read via port registers with device address 0x10 (88E6096 |
| 896 | * and compatible switches). |
| 897 | */ |
| 898 | priv->port_reg_base = 0x10; |
| 899 | priv->id = mv88e61xx_get_switch_id(phydev); |
| 900 | if (priv->id != 0xfff0) { |
| 901 | priv->global1 = 0x1B; |
| 902 | priv->global2 = 0x1C; |
| 903 | return 0; |
| 904 | } |
| 905 | |
| 906 | /* |
| 907 | * Now try via port registers with device address 0x08 |
| 908 | * (88E6020 and compatible switches). |
| 909 | */ |
| 910 | priv->port_reg_base = 0x08; |
| 911 | priv->id = mv88e61xx_get_switch_id(phydev); |
| 912 | if (priv->id != 0xfff0) { |
| 913 | priv->global1 = 0x0F; |
| 914 | priv->global2 = 0x07; |
| 915 | return 0; |
| 916 | } |
| 917 | |
| 918 | debug("%s Unknown ID 0x%x\n", __func__, priv->id); |
| 919 | return -ENODEV; |
| 920 | } |
| 921 | |
Kevin Smith | 87b2c4e | 2016-03-31 19:33:12 +0000 | [diff] [blame] | 922 | static int mv88e61xx_probe(struct phy_device *phydev) |
| 923 | { |
| 924 | struct mii_dev *smi_wrapper; |
| 925 | struct mv88e61xx_phy_priv *priv; |
| 926 | int res; |
| 927 | |
| 928 | res = mv88e61xx_hw_reset(phydev); |
| 929 | if (res < 0) |
| 930 | return res; |
| 931 | |
| 932 | priv = malloc(sizeof(*priv)); |
| 933 | if (!priv) |
| 934 | return -ENOMEM; |
| 935 | |
| 936 | memset(priv, 0, sizeof(*priv)); |
| 937 | |
| 938 | /* |
| 939 | * This device requires indirect reads/writes to the PHY registers |
| 940 | * which the generic PHY code can't handle. Make a wrapper MII device |
| 941 | * to handle reads/writes |
| 942 | */ |
| 943 | smi_wrapper = mdio_alloc(); |
| 944 | if (!smi_wrapper) { |
| 945 | free(priv); |
| 946 | return -ENOMEM; |
| 947 | } |
| 948 | |
| 949 | /* |
| 950 | * Store the mdio bus in the private data, as we are going to replace |
| 951 | * the bus with the wrapper bus |
| 952 | */ |
| 953 | priv->mdio_bus = phydev->bus; |
| 954 | |
| 955 | /* |
| 956 | * Store the smi bus address in private data. This lets us use the |
| 957 | * phydev addr field for device address instead, as the genphy code |
| 958 | * expects. |
| 959 | */ |
| 960 | priv->smi_addr = phydev->addr; |
| 961 | |
| 962 | /* |
| 963 | * Store the phy_device in the wrapper mii device. This lets us get it |
| 964 | * back when genphy functions call phy_read/phy_write. |
| 965 | */ |
| 966 | smi_wrapper->priv = phydev; |
| 967 | strncpy(smi_wrapper->name, "indirect mii", sizeof(smi_wrapper->name)); |
| 968 | smi_wrapper->read = mv88e61xx_phy_read_indirect; |
| 969 | smi_wrapper->write = mv88e61xx_phy_write_indirect; |
| 970 | |
| 971 | /* Replace the bus with the wrapper device */ |
| 972 | phydev->bus = smi_wrapper; |
| 973 | |
| 974 | phydev->priv = priv; |
| 975 | |
Anatolij Gustschin | e477917 | 2019-10-27 01:14:37 +0200 | [diff] [blame] | 976 | res = mv88e61xx_priv_reg_offs_pre_init(phydev); |
| 977 | if (res < 0) |
| 978 | return res; |
| 979 | |
| 980 | debug("%s ID 0x%x\n", __func__, priv->id); |
| 981 | |
| 982 | switch (priv->id) { |
| 983 | case PORT_SWITCH_ID_6096: |
| 984 | case PORT_SWITCH_ID_6097: |
| 985 | case PORT_SWITCH_ID_6172: |
| 986 | case PORT_SWITCH_ID_6176: |
| 987 | case PORT_SWITCH_ID_6240: |
| 988 | case PORT_SWITCH_ID_6352: |
| 989 | priv->port_count = 11; |
Anatolij Gustschin | 2eadde4 | 2019-10-27 01:14:38 +0200 | [diff] [blame] | 990 | priv->port_stat_link_mask = BIT(11); |
| 991 | priv->port_stat_dup_mask = BIT(10); |
| 992 | priv->port_stat_speed_width = 2; |
Anatolij Gustschin | b88aeeb | 2019-10-27 01:14:39 +0200 | [diff] [blame] | 993 | priv->phy_ctrl1_en_det_shift = 8; |
| 994 | priv->phy_ctrl1_en_det_width = 2; |
| 995 | priv->phy_ctrl1_en_det_ctrl = |
| 996 | PHY_REG_CTRL1_ENERGY_DET_SENSE_XMIT; |
Anatolij Gustschin | e477917 | 2019-10-27 01:14:37 +0200 | [diff] [blame] | 997 | break; |
| 998 | case PORT_SWITCH_ID_6020: |
| 999 | case PORT_SWITCH_ID_6070: |
| 1000 | case PORT_SWITCH_ID_6071: |
| 1001 | case PORT_SWITCH_ID_6220: |
| 1002 | case PORT_SWITCH_ID_6250: |
| 1003 | priv->port_count = 7; |
Anatolij Gustschin | 2eadde4 | 2019-10-27 01:14:38 +0200 | [diff] [blame] | 1004 | priv->port_stat_link_mask = BIT(12); |
| 1005 | priv->port_stat_dup_mask = BIT(9); |
| 1006 | priv->port_stat_speed_width = 1; |
Anatolij Gustschin | b88aeeb | 2019-10-27 01:14:39 +0200 | [diff] [blame] | 1007 | priv->phy_ctrl1_en_det_shift = 14; |
| 1008 | priv->phy_ctrl1_en_det_width = 1; |
| 1009 | priv->phy_ctrl1_en_det_ctrl = |
| 1010 | PHY_REG_CTRL1_ENERGY_DET_SENSE_PULSE; |
Anatolij Gustschin | e477917 | 2019-10-27 01:14:37 +0200 | [diff] [blame] | 1011 | break; |
| 1012 | default: |
| 1013 | free(priv); |
| 1014 | return -ENODEV; |
| 1015 | } |
| 1016 | |
| 1017 | res = mdio_register(smi_wrapper); |
| 1018 | if (res) |
| 1019 | printf("Failed to register SMI bus\n"); |
Kevin Smith | 87b2c4e | 2016-03-31 19:33:12 +0000 | [diff] [blame] | 1020 | |
| 1021 | return 0; |
| 1022 | } |
| 1023 | |
| 1024 | static int mv88e61xx_phy_config(struct phy_device *phydev) |
| 1025 | { |
Anatolij Gustschin | e477917 | 2019-10-27 01:14:37 +0200 | [diff] [blame] | 1026 | struct mv88e61xx_phy_priv *priv = phydev->priv; |
Kevin Smith | 87b2c4e | 2016-03-31 19:33:12 +0000 | [diff] [blame] | 1027 | int res; |
| 1028 | int i; |
| 1029 | int ret = -1; |
| 1030 | |
| 1031 | res = mv88e61xx_switch_init(phydev); |
| 1032 | if (res < 0) |
| 1033 | return res; |
| 1034 | |
Anatolij Gustschin | e477917 | 2019-10-27 01:14:37 +0200 | [diff] [blame] | 1035 | for (i = 0; i < priv->port_count; i++) { |
Kevin Smith | 87b2c4e | 2016-03-31 19:33:12 +0000 | [diff] [blame] | 1036 | if ((1 << i) & CONFIG_MV88E61XX_PHY_PORTS) { |
| 1037 | phydev->addr = i; |
| 1038 | |
| 1039 | res = mv88e61xx_phy_enable(phydev, i); |
| 1040 | if (res < 0) { |
| 1041 | printf("Error enabling PHY %i\n", i); |
| 1042 | continue; |
| 1043 | } |
| 1044 | res = mv88e61xx_phy_setup(phydev, i); |
| 1045 | if (res < 0) { |
| 1046 | printf("Error setting up PHY %i\n", i); |
| 1047 | continue; |
| 1048 | } |
| 1049 | res = mv88e61xx_phy_config_port(phydev, i); |
| 1050 | if (res < 0) { |
| 1051 | printf("Error configuring PHY %i\n", i); |
| 1052 | continue; |
| 1053 | } |
| 1054 | |
Tim Harvey | 6e434b9 | 2019-02-04 12:56:52 -0800 | [diff] [blame] | 1055 | res = phy_reset(phydev); |
Kevin Smith | 87b2c4e | 2016-03-31 19:33:12 +0000 | [diff] [blame] | 1056 | if (res < 0) { |
Tim Harvey | 6e434b9 | 2019-02-04 12:56:52 -0800 | [diff] [blame] | 1057 | printf("Error resetting PHY %i\n", i); |
Kevin Smith | 87b2c4e | 2016-03-31 19:33:12 +0000 | [diff] [blame] | 1058 | continue; |
| 1059 | } |
Tim Harvey | 6e434b9 | 2019-02-04 12:56:52 -0800 | [diff] [blame] | 1060 | res = genphy_config_aneg(phydev); |
Kevin Smith | 87b2c4e | 2016-03-31 19:33:12 +0000 | [diff] [blame] | 1061 | if (res < 0) { |
Tim Harvey | 6e434b9 | 2019-02-04 12:56:52 -0800 | [diff] [blame] | 1062 | printf("Error setting PHY %i autoneg\n", i); |
Kevin Smith | 87b2c4e | 2016-03-31 19:33:12 +0000 | [diff] [blame] | 1063 | continue; |
| 1064 | } |
| 1065 | |
| 1066 | /* Return success if any PHY succeeds */ |
| 1067 | ret = 0; |
Chris Packham | 3da645f | 2016-08-26 17:30:26 +1200 | [diff] [blame] | 1068 | } else if ((1 << i) & CONFIG_MV88E61XX_FIXED_PORTS) { |
| 1069 | res = mv88e61xx_fixed_port_setup(phydev, i); |
| 1070 | if (res < 0) { |
| 1071 | printf("Error configuring port %i\n", i); |
| 1072 | continue; |
| 1073 | } |
Kevin Smith | 87b2c4e | 2016-03-31 19:33:12 +0000 | [diff] [blame] | 1074 | } |
| 1075 | } |
| 1076 | |
| 1077 | return ret; |
| 1078 | } |
| 1079 | |
| 1080 | static int mv88e61xx_phy_is_connected(struct phy_device *phydev) |
| 1081 | { |
| 1082 | int val; |
| 1083 | |
| 1084 | val = mv88e61xx_phy_read(phydev, phydev->addr, PHY_REG_STATUS1); |
| 1085 | if (val < 0) |
| 1086 | return 0; |
| 1087 | |
| 1088 | /* |
| 1089 | * After reset, the energy detect signal remains high for a few seconds |
| 1090 | * regardless of whether a cable is connected. This function will |
| 1091 | * return false positives during this time. |
| 1092 | */ |
| 1093 | return (val & PHY_REG_STATUS1_ENERGY) == 0; |
| 1094 | } |
| 1095 | |
| 1096 | static int mv88e61xx_phy_startup(struct phy_device *phydev) |
| 1097 | { |
Anatolij Gustschin | e477917 | 2019-10-27 01:14:37 +0200 | [diff] [blame] | 1098 | struct mv88e61xx_phy_priv *priv = phydev->priv; |
Kevin Smith | 87b2c4e | 2016-03-31 19:33:12 +0000 | [diff] [blame] | 1099 | int i; |
| 1100 | int link = 0; |
| 1101 | int res; |
| 1102 | int speed = phydev->speed; |
| 1103 | int duplex = phydev->duplex; |
| 1104 | |
Anatolij Gustschin | e477917 | 2019-10-27 01:14:37 +0200 | [diff] [blame] | 1105 | for (i = 0; i < priv->port_count; i++) { |
Kevin Smith | 87b2c4e | 2016-03-31 19:33:12 +0000 | [diff] [blame] | 1106 | if ((1 << i) & CONFIG_MV88E61XX_PHY_PORTS) { |
| 1107 | phydev->addr = i; |
| 1108 | if (!mv88e61xx_phy_is_connected(phydev)) |
| 1109 | continue; |
| 1110 | res = genphy_update_link(phydev); |
| 1111 | if (res < 0) |
| 1112 | continue; |
| 1113 | res = mv88e61xx_parse_status(phydev); |
| 1114 | if (res < 0) |
| 1115 | continue; |
| 1116 | link = (link || phydev->link); |
| 1117 | } |
| 1118 | } |
| 1119 | phydev->link = link; |
| 1120 | |
| 1121 | /* Restore CPU interface speed and duplex after it was changed for |
| 1122 | * other ports */ |
| 1123 | phydev->speed = speed; |
| 1124 | phydev->duplex = duplex; |
| 1125 | |
| 1126 | return 0; |
| 1127 | } |
| 1128 | |
Marek Vasut | ebc4351 | 2023-03-19 18:02:51 +0100 | [diff] [blame] | 1129 | U_BOOT_PHY_DRIVER(mv88e61xx) = { |
Kevin Smith | 87b2c4e | 2016-03-31 19:33:12 +0000 | [diff] [blame] | 1130 | .name = "Marvell MV88E61xx", |
| 1131 | .uid = 0x01410eb1, |
| 1132 | .mask = 0xfffffff0, |
| 1133 | .features = PHY_GBIT_FEATURES, |
| 1134 | .probe = mv88e61xx_probe, |
| 1135 | .config = mv88e61xx_phy_config, |
| 1136 | .startup = mv88e61xx_phy_startup, |
| 1137 | .shutdown = &genphy_shutdown, |
| 1138 | }; |
| 1139 | |
Marek Vasut | ebc4351 | 2023-03-19 18:02:51 +0100 | [diff] [blame] | 1140 | U_BOOT_PHY_DRIVER(mv88e609x) = { |
Chris Packham | edc42a1 | 2016-08-26 17:30:25 +1200 | [diff] [blame] | 1141 | .name = "Marvell MV88E609x", |
| 1142 | .uid = 0x1410c89, |
| 1143 | .mask = 0xfffffff0, |
| 1144 | .features = PHY_GBIT_FEATURES, |
| 1145 | .probe = mv88e61xx_probe, |
| 1146 | .config = mv88e61xx_phy_config, |
| 1147 | .startup = mv88e61xx_phy_startup, |
| 1148 | .shutdown = &genphy_shutdown, |
| 1149 | }; |
| 1150 | |
Marek Vasut | ebc4351 | 2023-03-19 18:02:51 +0100 | [diff] [blame] | 1151 | U_BOOT_PHY_DRIVER(mv88e6071) = { |
Anatolij Gustschin | b8b125a | 2019-10-27 01:14:40 +0200 | [diff] [blame] | 1152 | .name = "Marvell MV88E6071", |
| 1153 | .uid = 0x1410db0, |
| 1154 | .mask = 0xfffffff0, |
| 1155 | .features = PHY_BASIC_FEATURES | SUPPORTED_MII, |
| 1156 | .probe = mv88e61xx_probe, |
| 1157 | .config = mv88e61xx_phy_config, |
| 1158 | .startup = mv88e61xx_phy_startup, |
| 1159 | .shutdown = &genphy_shutdown, |
| 1160 | }; |
| 1161 | |
Kevin Smith | 87b2c4e | 2016-03-31 19:33:12 +0000 | [diff] [blame] | 1162 | /* |
| 1163 | * Overload weak get_phy_id definition since we need non-standard functions |
| 1164 | * to read PHY registers |
| 1165 | */ |
| 1166 | int get_phy_id(struct mii_dev *bus, int smi_addr, int devad, u32 *phy_id) |
| 1167 | { |
| 1168 | struct phy_device temp_phy; |
| 1169 | struct mv88e61xx_phy_priv temp_priv; |
| 1170 | struct mii_dev temp_mii; |
| 1171 | int val; |
| 1172 | |
| 1173 | /* |
| 1174 | * Buid temporary data structures that the chip reading code needs to |
| 1175 | * read the ID |
| 1176 | */ |
| 1177 | temp_priv.mdio_bus = bus; |
| 1178 | temp_priv.smi_addr = smi_addr; |
| 1179 | temp_phy.priv = &temp_priv; |
| 1180 | temp_mii.priv = &temp_phy; |
| 1181 | |
Anatolij Gustschin | e477917 | 2019-10-27 01:14:37 +0200 | [diff] [blame] | 1182 | /* |
| 1183 | * get_phy_id() can be called by framework before mv88e61xx driver |
| 1184 | * probing, in this case the global register offsets are not |
| 1185 | * initialized yet. Do this initialization here before indirect |
| 1186 | * PHY register access. |
| 1187 | */ |
| 1188 | val = mv88e61xx_priv_reg_offs_pre_init(&temp_phy); |
| 1189 | if (val < 0) |
| 1190 | return val; |
| 1191 | |
Kevin Smith | 87b2c4e | 2016-03-31 19:33:12 +0000 | [diff] [blame] | 1192 | val = mv88e61xx_phy_read_indirect(&temp_mii, 0, devad, MII_PHYSID1); |
| 1193 | if (val < 0) |
| 1194 | return -EIO; |
| 1195 | |
| 1196 | *phy_id = val << 16; |
| 1197 | |
| 1198 | val = mv88e61xx_phy_read_indirect(&temp_mii, 0, devad, MII_PHYSID2); |
| 1199 | if (val < 0) |
| 1200 | return -EIO; |
| 1201 | |
| 1202 | *phy_id |= (val & 0xffff); |
| 1203 | |
| 1204 | return 0; |
| 1205 | } |