blob: 3f8aa7cd99c80087b8f4c44cfc33208e76031bd9 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Kevin Smith87b2c4e2016-03-31 19:33:12 +00002/*
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 Smith87b2c4e2016-03-31 19:33:12 +000011 */
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
32#include <common.h>
Simon Glass0f2af882020-05-10 11:40:05 -060033#include <log.h>
Simon Glassdbd79542020-05-10 11:40:11 -060034#include <linux/delay.h>
Kevin Smith87b2c4e2016-03-31 19:33:12 +000035
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 Gustschine4779172019-10-27 01:14:37 +020044#define PORT_MASK(port_count) ((1 << (port_count)) - 1)
Kevin Smith87b2c4e2016-03-31 19:33:12 +000045
46/* Device addresses */
47#define DEVADDR_PHY(p) (p)
Kevin Smith87b2c4e2016-03-31 19:33:12 +000048#define DEVADDR_SERDES 0x0F
Kevin Smith87b2c4e2016-03-31 19:33:12 +000049
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 Smith87b2c4e2016-03-31 19:33:12 +000089#define PORT_REG_STATUS_SPEED_SHIFT 8
Kevin Smith87b2c4e2016-03-31 19:33:12 +000090#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 Packham3da645f2016-08-26 17:30:26 +120099#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 Smith87b2c4e2016-03-31 19:33:12 +0000103#define PORT_REG_PHYS_CTRL_LINK_VALUE BIT(5)
104#define PORT_REG_PHYS_CTRL_LINK_FORCE BIT(4)
Chris Packham3da645f2016-08-26 17:30:26 +1200105#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 Gustschin2eadde42019-10-27 01:14:38 +0200108#define PORT_REG_PHYS_CTRL_SPD100 BIT(0)
Chris Packham3da645f2016-08-26 17:30:26 +1200109#define PORT_REG_PHYS_CTRL_SPD_MASK (BIT(1) | BIT(0))
Kevin Smith87b2c4e2016-03-31 19:33:12 +0000110
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 Smith87b2c4e2016-03-31 19:33:12 +0000122/* 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 Gustschinb88aeeb2019-10-27 01:14:39 +0200127#define PHY_REG_CTRL1_ENERGY_DET_SENSE_PULSE 1
Kevin Smith87b2c4e2016-03-31 19:33:12 +0000128#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
Chris Packham3da645f2016-08-26 17:30:26 +1200169/*
170 * These are ports without PHYs that may be wired directly
171 * to other serdes interfaces
172 */
173#ifndef CONFIG_MV88E61XX_FIXED_PORTS
174#define CONFIG_MV88E61XX_FIXED_PORTS 0
175#endif
176
Kevin Smith87b2c4e2016-03-31 19:33:12 +0000177/* ID register values for different switch models */
Anatolij Gustschine4779172019-10-27 01:14:37 +0200178#define PORT_SWITCH_ID_6020 0x0200
179#define PORT_SWITCH_ID_6070 0x0700
180#define PORT_SWITCH_ID_6071 0x0710
Chris Packhamedc42a12016-08-26 17:30:25 +1200181#define PORT_SWITCH_ID_6096 0x0980
182#define PORT_SWITCH_ID_6097 0x0990
Kevin Smith87b2c4e2016-03-31 19:33:12 +0000183#define PORT_SWITCH_ID_6172 0x1720
184#define PORT_SWITCH_ID_6176 0x1760
Anatolij Gustschine4779172019-10-27 01:14:37 +0200185#define PORT_SWITCH_ID_6220 0x2200
Kevin Smith87b2c4e2016-03-31 19:33:12 +0000186#define PORT_SWITCH_ID_6240 0x2400
Anatolij Gustschine4779172019-10-27 01:14:37 +0200187#define PORT_SWITCH_ID_6250 0x2500
Kevin Smith87b2c4e2016-03-31 19:33:12 +0000188#define PORT_SWITCH_ID_6352 0x3520
189
190struct mv88e61xx_phy_priv {
191 struct mii_dev *mdio_bus;
192 int smi_addr;
193 int id;
Anatolij Gustschine4779172019-10-27 01:14:37 +0200194 int port_count; /* Number of switch ports */
195 int port_reg_base; /* Base of the switch port registers */
Anatolij Gustschin2eadde42019-10-27 01:14:38 +0200196 u16 port_stat_link_mask;/* Bitmask for port link status bits */
197 u16 port_stat_dup_mask; /* Bitmask for port duplex status bits */
198 u8 port_stat_speed_width;/* Width of speed status bitfield */
Anatolij Gustschine4779172019-10-27 01:14:37 +0200199 u8 global1; /* Offset of Switch Global 1 registers */
200 u8 global2; /* Offset of Switch Global 2 registers */
Anatolij Gustschinb88aeeb2019-10-27 01:14:39 +0200201 u8 phy_ctrl1_en_det_shift; /* 'EDet' bit field offset */
202 u8 phy_ctrl1_en_det_width; /* Width of 'EDet' bit field */
203 u8 phy_ctrl1_en_det_ctrl; /* 'EDet' control value */
Kevin Smith87b2c4e2016-03-31 19:33:12 +0000204};
205
206static inline int smi_cmd(int cmd, int addr, int reg)
207{
208 cmd = bitfield_replace(cmd, SMI_CMD_ADDR_SHIFT, SMI_CMD_ADDR_WIDTH,
209 addr);
210 cmd = bitfield_replace(cmd, SMI_CMD_REG_SHIFT, SMI_CMD_REG_WIDTH, reg);
211 return cmd;
212}
213
214static inline int smi_cmd_read(int addr, int reg)
215{
216 return smi_cmd(SMI_CMD_READ, addr, reg);
217}
218
219static inline int smi_cmd_write(int addr, int reg)
220{
221 return smi_cmd(SMI_CMD_WRITE, addr, reg);
222}
223
224__weak int mv88e61xx_hw_reset(struct phy_device *phydev)
225{
226 return 0;
227}
228
229/* Wait for the current SMI indirect command to complete */
230static int mv88e61xx_smi_wait(struct mii_dev *bus, int smi_addr)
231{
232 int val;
233 u32 timeout = 100;
234
235 do {
236 val = bus->read(bus, smi_addr, MDIO_DEVAD_NONE, SMI_CMD_REG);
237 if (val >= 0 && (val & SMI_BUSY) == 0)
238 return 0;
239
240 mdelay(1);
241 } while (--timeout);
242
243 puts("SMI busy timeout\n");
244 return -ETIMEDOUT;
245}
246
247/*
248 * The mv88e61xx has three types of addresses: the smi bus address, the device
249 * address, and the register address. The smi bus address distinguishes it on
250 * the smi bus from other PHYs or switches. The device address determines
251 * which on-chip register set you are reading/writing (the various PHYs, their
252 * associated ports, or global configuration registers). The register address
253 * is the offset of the register you are reading/writing.
254 *
255 * When the mv88e61xx is hardware configured to have address zero, it behaves in
256 * single-chip addressing mode, where it responds to all SMI addresses, using
257 * the smi address as its device address. This obviously only works when this
258 * is the only chip on the SMI bus. This allows the driver to access device
259 * registers without using indirection. When the chip is configured to a
260 * non-zero address, it only responds to that SMI address and requires indirect
261 * writes to access the different device addresses.
262 */
263static int mv88e61xx_reg_read(struct phy_device *phydev, int dev, int reg)
264{
265 struct mv88e61xx_phy_priv *priv = phydev->priv;
266 struct mii_dev *mdio_bus = priv->mdio_bus;
267 int smi_addr = priv->smi_addr;
268 int res;
269
270 /* In single-chip mode, the device can be addressed directly */
271 if (smi_addr == 0)
272 return mdio_bus->read(mdio_bus, dev, MDIO_DEVAD_NONE, reg);
273
274 /* Wait for the bus to become free */
275 res = mv88e61xx_smi_wait(mdio_bus, smi_addr);
276 if (res < 0)
277 return res;
278
279 /* Issue the read command */
280 res = mdio_bus->write(mdio_bus, smi_addr, MDIO_DEVAD_NONE, SMI_CMD_REG,
281 smi_cmd_read(dev, reg));
282 if (res < 0)
283 return res;
284
285 /* Wait for the read command to complete */
286 res = mv88e61xx_smi_wait(mdio_bus, smi_addr);
287 if (res < 0)
288 return res;
289
290 /* Read the data */
291 res = mdio_bus->read(mdio_bus, smi_addr, MDIO_DEVAD_NONE, SMI_DATA_REG);
292 if (res < 0)
293 return res;
294
295 return bitfield_extract(res, 0, 16);
296}
297
298/* See the comment above mv88e61xx_reg_read */
299static int mv88e61xx_reg_write(struct phy_device *phydev, int dev, int reg,
300 u16 val)
301{
302 struct mv88e61xx_phy_priv *priv = phydev->priv;
303 struct mii_dev *mdio_bus = priv->mdio_bus;
304 int smi_addr = priv->smi_addr;
305 int res;
306
307 /* In single-chip mode, the device can be addressed directly */
308 if (smi_addr == 0) {
309 return mdio_bus->write(mdio_bus, dev, MDIO_DEVAD_NONE, reg,
310 val);
311 }
312
313 /* Wait for the bus to become free */
314 res = mv88e61xx_smi_wait(mdio_bus, smi_addr);
315 if (res < 0)
316 return res;
317
318 /* Set the data to write */
319 res = mdio_bus->write(mdio_bus, smi_addr, MDIO_DEVAD_NONE,
320 SMI_DATA_REG, val);
321 if (res < 0)
322 return res;
323
324 /* Issue the write command */
325 res = mdio_bus->write(mdio_bus, smi_addr, MDIO_DEVAD_NONE, SMI_CMD_REG,
326 smi_cmd_write(dev, reg));
327 if (res < 0)
328 return res;
329
330 /* Wait for the write command to complete */
331 res = mv88e61xx_smi_wait(mdio_bus, smi_addr);
332 if (res < 0)
333 return res;
334
335 return 0;
336}
337
338static int mv88e61xx_phy_wait(struct phy_device *phydev)
339{
Anatolij Gustschine4779172019-10-27 01:14:37 +0200340 struct mv88e61xx_phy_priv *priv = phydev->priv;
Kevin Smith87b2c4e2016-03-31 19:33:12 +0000341 int val;
342 u32 timeout = 100;
343
344 do {
Anatolij Gustschine4779172019-10-27 01:14:37 +0200345 val = mv88e61xx_reg_read(phydev, priv->global2,
Kevin Smith87b2c4e2016-03-31 19:33:12 +0000346 GLOBAL2_REG_PHY_CMD);
347 if (val >= 0 && (val & SMI_BUSY) == 0)
348 return 0;
349
350 mdelay(1);
351 } while (--timeout);
352
353 return -ETIMEDOUT;
354}
355
356static int mv88e61xx_phy_read_indirect(struct mii_dev *smi_wrapper, int dev,
357 int devad, int reg)
358{
Anatolij Gustschine4779172019-10-27 01:14:37 +0200359 struct mv88e61xx_phy_priv *priv;
Kevin Smith87b2c4e2016-03-31 19:33:12 +0000360 struct phy_device *phydev;
361 int res;
362
363 phydev = (struct phy_device *)smi_wrapper->priv;
Anatolij Gustschine4779172019-10-27 01:14:37 +0200364 priv = phydev->priv;
Kevin Smith87b2c4e2016-03-31 19:33:12 +0000365
366 /* Issue command to read */
Anatolij Gustschine4779172019-10-27 01:14:37 +0200367 res = mv88e61xx_reg_write(phydev, priv->global2,
Kevin Smith87b2c4e2016-03-31 19:33:12 +0000368 GLOBAL2_REG_PHY_CMD,
369 smi_cmd_read(dev, reg));
370
371 /* Wait for data to be read */
372 res = mv88e61xx_phy_wait(phydev);
373 if (res < 0)
374 return res;
375
376 /* Read retrieved data */
Anatolij Gustschine4779172019-10-27 01:14:37 +0200377 return mv88e61xx_reg_read(phydev, priv->global2,
Kevin Smith87b2c4e2016-03-31 19:33:12 +0000378 GLOBAL2_REG_PHY_DATA);
379}
380
381static int mv88e61xx_phy_write_indirect(struct mii_dev *smi_wrapper, int dev,
382 int devad, int reg, u16 data)
383{
Anatolij Gustschine4779172019-10-27 01:14:37 +0200384 struct mv88e61xx_phy_priv *priv;
Kevin Smith87b2c4e2016-03-31 19:33:12 +0000385 struct phy_device *phydev;
386 int res;
387
388 phydev = (struct phy_device *)smi_wrapper->priv;
Anatolij Gustschine4779172019-10-27 01:14:37 +0200389 priv = phydev->priv;
Kevin Smith87b2c4e2016-03-31 19:33:12 +0000390
391 /* Set the data to write */
Anatolij Gustschine4779172019-10-27 01:14:37 +0200392 res = mv88e61xx_reg_write(phydev, priv->global2,
Kevin Smith87b2c4e2016-03-31 19:33:12 +0000393 GLOBAL2_REG_PHY_DATA, data);
394 if (res < 0)
395 return res;
396 /* Issue the write command */
Anatolij Gustschine4779172019-10-27 01:14:37 +0200397 res = mv88e61xx_reg_write(phydev, priv->global2,
Kevin Smith87b2c4e2016-03-31 19:33:12 +0000398 GLOBAL2_REG_PHY_CMD,
399 smi_cmd_write(dev, reg));
400 if (res < 0)
401 return res;
402
403 /* Wait for command to complete */
404 return mv88e61xx_phy_wait(phydev);
405}
406
407/* Wrapper function to make calls to phy_read_indirect simpler */
408static int mv88e61xx_phy_read(struct phy_device *phydev, int phy, int reg)
409{
410 return mv88e61xx_phy_read_indirect(phydev->bus, DEVADDR_PHY(phy),
411 MDIO_DEVAD_NONE, reg);
412}
413
414/* Wrapper function to make calls to phy_read_indirect simpler */
415static int mv88e61xx_phy_write(struct phy_device *phydev, int phy,
416 int reg, u16 val)
417{
418 return mv88e61xx_phy_write_indirect(phydev->bus, DEVADDR_PHY(phy),
419 MDIO_DEVAD_NONE, reg, val);
420}
421
422static int mv88e61xx_port_read(struct phy_device *phydev, u8 port, u8 reg)
423{
Anatolij Gustschine4779172019-10-27 01:14:37 +0200424 struct mv88e61xx_phy_priv *priv = phydev->priv;
425
426 return mv88e61xx_reg_read(phydev, priv->port_reg_base + port, reg);
Kevin Smith87b2c4e2016-03-31 19:33:12 +0000427}
428
429static int mv88e61xx_port_write(struct phy_device *phydev, u8 port, u8 reg,
430 u16 val)
431{
Anatolij Gustschine4779172019-10-27 01:14:37 +0200432 struct mv88e61xx_phy_priv *priv = phydev->priv;
433
434 return mv88e61xx_reg_write(phydev, priv->port_reg_base + port,
435 reg, val);
Kevin Smith87b2c4e2016-03-31 19:33:12 +0000436}
437
438static int mv88e61xx_set_page(struct phy_device *phydev, u8 phy, u8 page)
439{
440 return mv88e61xx_phy_write(phydev, phy, PHY_REG_PAGE, page);
441}
442
443static int mv88e61xx_get_switch_id(struct phy_device *phydev)
444{
445 int res;
446
447 res = mv88e61xx_port_read(phydev, 0, PORT_REG_SWITCH_ID);
448 if (res < 0)
449 return res;
450 return res & 0xfff0;
451}
452
453static bool mv88e61xx_6352_family(struct phy_device *phydev)
454{
455 struct mv88e61xx_phy_priv *priv = phydev->priv;
456
457 switch (priv->id) {
458 case PORT_SWITCH_ID_6172:
459 case PORT_SWITCH_ID_6176:
460 case PORT_SWITCH_ID_6240:
461 case PORT_SWITCH_ID_6352:
462 return true;
463 }
464 return false;
465}
466
467static int mv88e61xx_get_cmode(struct phy_device *phydev, u8 port)
468{
469 int res;
470
471 res = mv88e61xx_port_read(phydev, port, PORT_REG_STATUS);
472 if (res < 0)
473 return res;
474 return res & PORT_REG_STATUS_CMODE_MASK;
475}
476
477static int mv88e61xx_parse_status(struct phy_device *phydev)
478{
479 unsigned int speed;
480 unsigned int mii_reg;
481
482 mii_reg = phy_read(phydev, MDIO_DEVAD_NONE, PHY_REG_STATUS1);
483
484 if ((mii_reg & PHY_REG_STATUS1_LINK) &&
485 !(mii_reg & PHY_REG_STATUS1_SPDDONE)) {
486 int i = 0;
487
488 puts("Waiting for PHY realtime link");
489 while (!(mii_reg & PHY_REG_STATUS1_SPDDONE)) {
490 /* Timeout reached ? */
491 if (i > PHY_AUTONEGOTIATE_TIMEOUT) {
492 puts(" TIMEOUT !\n");
493 phydev->link = 0;
494 break;
495 }
496
497 if ((i++ % 1000) == 0)
498 putc('.');
499 udelay(1000);
500 mii_reg = phy_read(phydev, MDIO_DEVAD_NONE,
501 PHY_REG_STATUS1);
502 }
503 puts(" done\n");
504 udelay(500000); /* another 500 ms (results in faster booting) */
505 } else {
506 if (mii_reg & PHY_REG_STATUS1_LINK)
507 phydev->link = 1;
508 else
509 phydev->link = 0;
510 }
511
512 if (mii_reg & PHY_REG_STATUS1_DUPLEX)
513 phydev->duplex = DUPLEX_FULL;
514 else
515 phydev->duplex = DUPLEX_HALF;
516
517 speed = mii_reg & PHY_REG_STATUS1_SPEED;
518
519 switch (speed) {
520 case PHY_REG_STATUS1_GBIT:
521 phydev->speed = SPEED_1000;
522 break;
523 case PHY_REG_STATUS1_100:
524 phydev->speed = SPEED_100;
525 break;
526 default:
527 phydev->speed = SPEED_10;
528 break;
529 }
530
531 return 0;
532}
533
534static int mv88e61xx_switch_reset(struct phy_device *phydev)
535{
Anatolij Gustschine4779172019-10-27 01:14:37 +0200536 struct mv88e61xx_phy_priv *priv = phydev->priv;
Kevin Smith87b2c4e2016-03-31 19:33:12 +0000537 int time;
538 int val;
539 u8 port;
540
541 /* Disable all ports */
Anatolij Gustschine4779172019-10-27 01:14:37 +0200542 for (port = 0; port < priv->port_count; port++) {
Kevin Smith87b2c4e2016-03-31 19:33:12 +0000543 val = mv88e61xx_port_read(phydev, port, PORT_REG_CTRL);
544 if (val < 0)
545 return val;
546 val = bitfield_replace(val, PORT_REG_CTRL_PSTATE_SHIFT,
547 PORT_REG_CTRL_PSTATE_WIDTH,
548 PORT_REG_CTRL_PSTATE_DISABLED);
549 val = mv88e61xx_port_write(phydev, port, PORT_REG_CTRL, val);
550 if (val < 0)
551 return val;
552 }
553
554 /* Wait 2 ms for queues to drain */
555 udelay(2000);
556
557 /* Reset switch */
Anatolij Gustschine4779172019-10-27 01:14:37 +0200558 val = mv88e61xx_reg_read(phydev, priv->global1, GLOBAL1_CTRL);
Kevin Smith87b2c4e2016-03-31 19:33:12 +0000559 if (val < 0)
560 return val;
561 val |= GLOBAL1_CTRL_SWRESET;
Anatolij Gustschine4779172019-10-27 01:14:37 +0200562 val = mv88e61xx_reg_write(phydev, priv->global1,
563 GLOBAL1_CTRL, val);
Kevin Smith87b2c4e2016-03-31 19:33:12 +0000564 if (val < 0)
565 return val;
566
567 /* Wait up to 1 second for switch reset complete */
568 for (time = 1000; time; time--) {
Anatolij Gustschine4779172019-10-27 01:14:37 +0200569 val = mv88e61xx_reg_read(phydev, priv->global1,
570 GLOBAL1_CTRL);
Kevin Smith87b2c4e2016-03-31 19:33:12 +0000571 if (val >= 0 && ((val & GLOBAL1_CTRL_SWRESET) == 0))
572 break;
573 udelay(1000);
574 }
575 if (!time)
576 return -ETIMEDOUT;
577
578 return 0;
579}
580
581static int mv88e61xx_serdes_init(struct phy_device *phydev)
582{
583 int val;
584
585 val = mv88e61xx_set_page(phydev, DEVADDR_SERDES, PHY_PAGE_SERDES);
586 if (val < 0)
587 return val;
588
589 /* Power up serdes module */
590 val = mv88e61xx_phy_read(phydev, DEVADDR_SERDES, MII_BMCR);
591 if (val < 0)
592 return val;
593 val &= ~(BMCR_PDOWN);
594 val = mv88e61xx_phy_write(phydev, DEVADDR_SERDES, MII_BMCR, val);
595 if (val < 0)
596 return val;
597
598 return 0;
599}
600
601static int mv88e61xx_port_enable(struct phy_device *phydev, u8 port)
602{
603 int val;
604
605 val = mv88e61xx_port_read(phydev, port, PORT_REG_CTRL);
606 if (val < 0)
607 return val;
608 val = bitfield_replace(val, PORT_REG_CTRL_PSTATE_SHIFT,
609 PORT_REG_CTRL_PSTATE_WIDTH,
610 PORT_REG_CTRL_PSTATE_FORWARD);
611 val = mv88e61xx_port_write(phydev, port, PORT_REG_CTRL, val);
612 if (val < 0)
613 return val;
614
615 return 0;
616}
617
618static int mv88e61xx_port_set_vlan(struct phy_device *phydev, u8 port,
Chris Packhamedc42a12016-08-26 17:30:25 +1200619 u16 mask)
Kevin Smith87b2c4e2016-03-31 19:33:12 +0000620{
621 int val;
622
623 /* Set VID to port number plus one */
624 val = mv88e61xx_port_read(phydev, port, PORT_REG_VLAN_ID);
625 if (val < 0)
626 return val;
627 val = bitfield_replace(val, PORT_REG_VLAN_ID_DEF_VID_SHIFT,
628 PORT_REG_VLAN_ID_DEF_VID_WIDTH,
629 port + 1);
630 val = mv88e61xx_port_write(phydev, port, PORT_REG_VLAN_ID, val);
631 if (val < 0)
632 return val;
633
634 /* Set VID mask */
635 val = mv88e61xx_port_read(phydev, port, PORT_REG_VLAN_MAP);
636 if (val < 0)
637 return val;
638 val = bitfield_replace(val, PORT_REG_VLAN_MAP_TABLE_SHIFT,
639 PORT_REG_VLAN_MAP_TABLE_WIDTH,
640 mask);
641 val = mv88e61xx_port_write(phydev, port, PORT_REG_VLAN_MAP, val);
642 if (val < 0)
643 return val;
644
645 return 0;
646}
647
648static int mv88e61xx_read_port_config(struct phy_device *phydev, u8 port)
649{
Anatolij Gustschin2eadde42019-10-27 01:14:38 +0200650 struct mv88e61xx_phy_priv *priv = phydev->priv;
Kevin Smith87b2c4e2016-03-31 19:33:12 +0000651 int res;
652 int val;
653 bool forced = false;
654
655 val = mv88e61xx_port_read(phydev, port, PORT_REG_STATUS);
656 if (val < 0)
657 return val;
Anatolij Gustschin2eadde42019-10-27 01:14:38 +0200658 if (!(val & priv->port_stat_link_mask)) {
Kevin Smith87b2c4e2016-03-31 19:33:12 +0000659 /* Temporarily force link to read port configuration */
660 u32 timeout = 100;
661 forced = true;
662
663 val = mv88e61xx_port_read(phydev, port, PORT_REG_PHYS_CTRL);
664 if (val < 0)
665 return val;
666 val |= (PORT_REG_PHYS_CTRL_LINK_FORCE |
667 PORT_REG_PHYS_CTRL_LINK_VALUE);
668 val = mv88e61xx_port_write(phydev, port, PORT_REG_PHYS_CTRL,
669 val);
670 if (val < 0)
671 return val;
672
673 /* Wait for status register to reflect forced link */
674 do {
675 val = mv88e61xx_port_read(phydev, port,
676 PORT_REG_STATUS);
Tom Rini09418652017-05-08 22:14:32 -0400677 if (val < 0) {
678 res = -EIO;
Kevin Smith87b2c4e2016-03-31 19:33:12 +0000679 goto unforce;
Tom Rini09418652017-05-08 22:14:32 -0400680 }
Anatolij Gustschin2eadde42019-10-27 01:14:38 +0200681 if (val & priv->port_stat_link_mask)
Kevin Smith87b2c4e2016-03-31 19:33:12 +0000682 break;
683 } while (--timeout);
684
685 if (timeout == 0) {
686 res = -ETIMEDOUT;
687 goto unforce;
688 }
689 }
690
Anatolij Gustschin2eadde42019-10-27 01:14:38 +0200691 if (val & priv->port_stat_dup_mask)
Kevin Smith87b2c4e2016-03-31 19:33:12 +0000692 phydev->duplex = DUPLEX_FULL;
693 else
694 phydev->duplex = DUPLEX_HALF;
695
696 val = bitfield_extract(val, PORT_REG_STATUS_SPEED_SHIFT,
Anatolij Gustschin2eadde42019-10-27 01:14:38 +0200697 priv->port_stat_speed_width);
Kevin Smith87b2c4e2016-03-31 19:33:12 +0000698 switch (val) {
699 case PORT_REG_STATUS_SPEED_1000:
700 phydev->speed = SPEED_1000;
701 break;
702 case PORT_REG_STATUS_SPEED_100:
703 phydev->speed = SPEED_100;
704 break;
705 default:
706 phydev->speed = SPEED_10;
707 break;
708 }
709
710 res = 0;
711
712unforce:
713 if (forced) {
714 val = mv88e61xx_port_read(phydev, port, PORT_REG_PHYS_CTRL);
715 if (val < 0)
716 return val;
717 val &= ~(PORT_REG_PHYS_CTRL_LINK_FORCE |
718 PORT_REG_PHYS_CTRL_LINK_VALUE);
719 val = mv88e61xx_port_write(phydev, port, PORT_REG_PHYS_CTRL,
720 val);
721 if (val < 0)
722 return val;
723 }
724
725 return res;
726}
727
Chris Packham21788612018-06-03 16:21:26 +1200728static int mv88e61xx_fixed_port_setup(struct phy_device *phydev, u8 port)
729{
Anatolij Gustschin2eadde42019-10-27 01:14:38 +0200730 struct mv88e61xx_phy_priv *priv = phydev->priv;
Chris Packham21788612018-06-03 16:21:26 +1200731 int val;
732
733 val = mv88e61xx_port_read(phydev, port, PORT_REG_PHYS_CTRL);
734 if (val < 0)
735 return val;
736
737 val &= ~(PORT_REG_PHYS_CTRL_SPD_MASK |
Anatolij Gustschin2eadde42019-10-27 01:14:38 +0200738 PORT_REG_PHYS_CTRL_FC_VALUE |
739 PORT_REG_PHYS_CTRL_FC_FORCE);
740 val |= PORT_REG_PHYS_CTRL_FC_FORCE |
Chris Packham21788612018-06-03 16:21:26 +1200741 PORT_REG_PHYS_CTRL_DUPLEX_VALUE |
Anatolij Gustschin2eadde42019-10-27 01:14:38 +0200742 PORT_REG_PHYS_CTRL_DUPLEX_FORCE;
743
744 if (priv->id == PORT_SWITCH_ID_6071) {
745 val |= PORT_REG_PHYS_CTRL_SPD100;
746 } else {
747 val |= PORT_REG_PHYS_CTRL_PCS_AN_EN |
748 PORT_REG_PHYS_CTRL_PCS_AN_RST |
749 PORT_REG_PHYS_CTRL_SPD1000;
750 }
Chris Packham21788612018-06-03 16:21:26 +1200751
752 if (port == CONFIG_MV88E61XX_CPU_PORT)
753 val |= PORT_REG_PHYS_CTRL_LINK_VALUE |
754 PORT_REG_PHYS_CTRL_LINK_FORCE;
755
756 return mv88e61xx_port_write(phydev, port, PORT_REG_PHYS_CTRL,
757 val);
758}
759
Kevin Smith87b2c4e2016-03-31 19:33:12 +0000760static int mv88e61xx_set_cpu_port(struct phy_device *phydev)
761{
Anatolij Gustschine4779172019-10-27 01:14:37 +0200762 struct mv88e61xx_phy_priv *priv = phydev->priv;
Kevin Smith87b2c4e2016-03-31 19:33:12 +0000763 int val;
764
765 /* Set CPUDest */
Anatolij Gustschine4779172019-10-27 01:14:37 +0200766 val = mv88e61xx_reg_read(phydev, priv->global1, GLOBAL1_MON_CTRL);
Kevin Smith87b2c4e2016-03-31 19:33:12 +0000767 if (val < 0)
768 return val;
769 val = bitfield_replace(val, GLOBAL1_MON_CTRL_CPUDEST_SHIFT,
770 GLOBAL1_MON_CTRL_CPUDEST_WIDTH,
771 CONFIG_MV88E61XX_CPU_PORT);
Anatolij Gustschine4779172019-10-27 01:14:37 +0200772 val = mv88e61xx_reg_write(phydev, priv->global1,
773 GLOBAL1_MON_CTRL, val);
Kevin Smith87b2c4e2016-03-31 19:33:12 +0000774 if (val < 0)
775 return val;
776
777 /* Allow CPU to route to any port */
Anatolij Gustschine4779172019-10-27 01:14:37 +0200778 val = PORT_MASK(priv->port_count) & ~(1 << CONFIG_MV88E61XX_CPU_PORT);
Kevin Smith87b2c4e2016-03-31 19:33:12 +0000779 val = mv88e61xx_port_set_vlan(phydev, CONFIG_MV88E61XX_CPU_PORT, val);
780 if (val < 0)
781 return val;
782
783 /* Enable CPU port */
784 val = mv88e61xx_port_enable(phydev, CONFIG_MV88E61XX_CPU_PORT);
785 if (val < 0)
786 return val;
787
788 val = mv88e61xx_read_port_config(phydev, CONFIG_MV88E61XX_CPU_PORT);
789 if (val < 0)
790 return val;
791
792 /* If CPU is connected to serdes, initialize serdes */
793 if (mv88e61xx_6352_family(phydev)) {
794 val = mv88e61xx_get_cmode(phydev, CONFIG_MV88E61XX_CPU_PORT);
795 if (val < 0)
796 return val;
797 if (val == PORT_REG_STATUS_CMODE_100BASE_X ||
798 val == PORT_REG_STATUS_CMODE_1000BASE_X ||
799 val == PORT_REG_STATUS_CMODE_SGMII) {
800 val = mv88e61xx_serdes_init(phydev);
801 if (val < 0)
802 return val;
803 }
Chris Packham21788612018-06-03 16:21:26 +1200804 } else {
805 val = mv88e61xx_fixed_port_setup(phydev,
806 CONFIG_MV88E61XX_CPU_PORT);
807 if (val < 0)
808 return val;
Kevin Smith87b2c4e2016-03-31 19:33:12 +0000809 }
810
811 return 0;
812}
813
814static int mv88e61xx_switch_init(struct phy_device *phydev)
815{
816 static int init;
817 int res;
818
819 if (init)
820 return 0;
821
822 res = mv88e61xx_switch_reset(phydev);
823 if (res < 0)
824 return res;
825
826 res = mv88e61xx_set_cpu_port(phydev);
827 if (res < 0)
828 return res;
829
830 init = 1;
831
832 return 0;
833}
834
835static int mv88e61xx_phy_enable(struct phy_device *phydev, u8 phy)
836{
837 int val;
838
839 val = mv88e61xx_phy_read(phydev, phy, MII_BMCR);
840 if (val < 0)
841 return val;
842 val &= ~(BMCR_PDOWN);
843 val = mv88e61xx_phy_write(phydev, phy, MII_BMCR, val);
844 if (val < 0)
845 return val;
846
847 return 0;
848}
849
850static int mv88e61xx_phy_setup(struct phy_device *phydev, u8 phy)
851{
Anatolij Gustschinb88aeeb2019-10-27 01:14:39 +0200852 struct mv88e61xx_phy_priv *priv = phydev->priv;
Kevin Smith87b2c4e2016-03-31 19:33:12 +0000853 int val;
854
855 /*
856 * Enable energy-detect sensing on PHY, used to determine when a PHY
857 * port is physically connected
858 */
859 val = mv88e61xx_phy_read(phydev, phy, PHY_REG_CTRL1);
860 if (val < 0)
861 return val;
Anatolij Gustschinb88aeeb2019-10-27 01:14:39 +0200862 val = bitfield_replace(val, priv->phy_ctrl1_en_det_shift,
863 priv->phy_ctrl1_en_det_width,
864 priv->phy_ctrl1_en_det_ctrl);
Kevin Smith87b2c4e2016-03-31 19:33:12 +0000865 val = mv88e61xx_phy_write(phydev, phy, PHY_REG_CTRL1, val);
866 if (val < 0)
867 return val;
868
869 return 0;
870}
871
872static int mv88e61xx_phy_config_port(struct phy_device *phydev, u8 phy)
873{
874 int val;
875
876 val = mv88e61xx_port_enable(phydev, phy);
877 if (val < 0)
878 return val;
879
880 val = mv88e61xx_port_set_vlan(phydev, phy,
881 1 << CONFIG_MV88E61XX_CPU_PORT);
882 if (val < 0)
883 return val;
884
885 return 0;
886}
887
Anatolij Gustschine4779172019-10-27 01:14:37 +0200888/*
889 * This function is used to pre-configure the required register
890 * offsets, so that the indirect register access to the PHY registers
891 * is possible. This is necessary to be able to read the PHY ID
892 * while driver probing or in get_phy_id(). The globalN register
893 * offsets must be initialized correctly for a detected switch,
894 * otherwise detection of the PHY ID won't work!
895 */
896static int mv88e61xx_priv_reg_offs_pre_init(struct phy_device *phydev)
897{
898 struct mv88e61xx_phy_priv *priv = phydev->priv;
899
900 /*
901 * Initial 'port_reg_base' value must be an offset of existing
902 * port register, then reading the ID should succeed. First, try
903 * to read via port registers with device address 0x10 (88E6096
904 * and compatible switches).
905 */
906 priv->port_reg_base = 0x10;
907 priv->id = mv88e61xx_get_switch_id(phydev);
908 if (priv->id != 0xfff0) {
909 priv->global1 = 0x1B;
910 priv->global2 = 0x1C;
911 return 0;
912 }
913
914 /*
915 * Now try via port registers with device address 0x08
916 * (88E6020 and compatible switches).
917 */
918 priv->port_reg_base = 0x08;
919 priv->id = mv88e61xx_get_switch_id(phydev);
920 if (priv->id != 0xfff0) {
921 priv->global1 = 0x0F;
922 priv->global2 = 0x07;
923 return 0;
924 }
925
926 debug("%s Unknown ID 0x%x\n", __func__, priv->id);
927 return -ENODEV;
928}
929
Kevin Smith87b2c4e2016-03-31 19:33:12 +0000930static int mv88e61xx_probe(struct phy_device *phydev)
931{
932 struct mii_dev *smi_wrapper;
933 struct mv88e61xx_phy_priv *priv;
934 int res;
935
936 res = mv88e61xx_hw_reset(phydev);
937 if (res < 0)
938 return res;
939
940 priv = malloc(sizeof(*priv));
941 if (!priv)
942 return -ENOMEM;
943
944 memset(priv, 0, sizeof(*priv));
945
946 /*
947 * This device requires indirect reads/writes to the PHY registers
948 * which the generic PHY code can't handle. Make a wrapper MII device
949 * to handle reads/writes
950 */
951 smi_wrapper = mdio_alloc();
952 if (!smi_wrapper) {
953 free(priv);
954 return -ENOMEM;
955 }
956
957 /*
958 * Store the mdio bus in the private data, as we are going to replace
959 * the bus with the wrapper bus
960 */
961 priv->mdio_bus = phydev->bus;
962
963 /*
964 * Store the smi bus address in private data. This lets us use the
965 * phydev addr field for device address instead, as the genphy code
966 * expects.
967 */
968 priv->smi_addr = phydev->addr;
969
970 /*
971 * Store the phy_device in the wrapper mii device. This lets us get it
972 * back when genphy functions call phy_read/phy_write.
973 */
974 smi_wrapper->priv = phydev;
975 strncpy(smi_wrapper->name, "indirect mii", sizeof(smi_wrapper->name));
976 smi_wrapper->read = mv88e61xx_phy_read_indirect;
977 smi_wrapper->write = mv88e61xx_phy_write_indirect;
978
979 /* Replace the bus with the wrapper device */
980 phydev->bus = smi_wrapper;
981
982 phydev->priv = priv;
983
Anatolij Gustschine4779172019-10-27 01:14:37 +0200984 res = mv88e61xx_priv_reg_offs_pre_init(phydev);
985 if (res < 0)
986 return res;
987
988 debug("%s ID 0x%x\n", __func__, priv->id);
989
990 switch (priv->id) {
991 case PORT_SWITCH_ID_6096:
992 case PORT_SWITCH_ID_6097:
993 case PORT_SWITCH_ID_6172:
994 case PORT_SWITCH_ID_6176:
995 case PORT_SWITCH_ID_6240:
996 case PORT_SWITCH_ID_6352:
997 priv->port_count = 11;
Anatolij Gustschin2eadde42019-10-27 01:14:38 +0200998 priv->port_stat_link_mask = BIT(11);
999 priv->port_stat_dup_mask = BIT(10);
1000 priv->port_stat_speed_width = 2;
Anatolij Gustschinb88aeeb2019-10-27 01:14:39 +02001001 priv->phy_ctrl1_en_det_shift = 8;
1002 priv->phy_ctrl1_en_det_width = 2;
1003 priv->phy_ctrl1_en_det_ctrl =
1004 PHY_REG_CTRL1_ENERGY_DET_SENSE_XMIT;
Anatolij Gustschine4779172019-10-27 01:14:37 +02001005 break;
1006 case PORT_SWITCH_ID_6020:
1007 case PORT_SWITCH_ID_6070:
1008 case PORT_SWITCH_ID_6071:
1009 case PORT_SWITCH_ID_6220:
1010 case PORT_SWITCH_ID_6250:
1011 priv->port_count = 7;
Anatolij Gustschin2eadde42019-10-27 01:14:38 +02001012 priv->port_stat_link_mask = BIT(12);
1013 priv->port_stat_dup_mask = BIT(9);
1014 priv->port_stat_speed_width = 1;
Anatolij Gustschinb88aeeb2019-10-27 01:14:39 +02001015 priv->phy_ctrl1_en_det_shift = 14;
1016 priv->phy_ctrl1_en_det_width = 1;
1017 priv->phy_ctrl1_en_det_ctrl =
1018 PHY_REG_CTRL1_ENERGY_DET_SENSE_PULSE;
Anatolij Gustschine4779172019-10-27 01:14:37 +02001019 break;
1020 default:
1021 free(priv);
1022 return -ENODEV;
1023 }
1024
1025 res = mdio_register(smi_wrapper);
1026 if (res)
1027 printf("Failed to register SMI bus\n");
Kevin Smith87b2c4e2016-03-31 19:33:12 +00001028
1029 return 0;
1030}
1031
1032static int mv88e61xx_phy_config(struct phy_device *phydev)
1033{
Anatolij Gustschine4779172019-10-27 01:14:37 +02001034 struct mv88e61xx_phy_priv *priv = phydev->priv;
Kevin Smith87b2c4e2016-03-31 19:33:12 +00001035 int res;
1036 int i;
1037 int ret = -1;
1038
1039 res = mv88e61xx_switch_init(phydev);
1040 if (res < 0)
1041 return res;
1042
Anatolij Gustschine4779172019-10-27 01:14:37 +02001043 for (i = 0; i < priv->port_count; i++) {
Kevin Smith87b2c4e2016-03-31 19:33:12 +00001044 if ((1 << i) & CONFIG_MV88E61XX_PHY_PORTS) {
1045 phydev->addr = i;
1046
1047 res = mv88e61xx_phy_enable(phydev, i);
1048 if (res < 0) {
1049 printf("Error enabling PHY %i\n", i);
1050 continue;
1051 }
1052 res = mv88e61xx_phy_setup(phydev, i);
1053 if (res < 0) {
1054 printf("Error setting up PHY %i\n", i);
1055 continue;
1056 }
1057 res = mv88e61xx_phy_config_port(phydev, i);
1058 if (res < 0) {
1059 printf("Error configuring PHY %i\n", i);
1060 continue;
1061 }
1062
Tim Harvey6e434b92019-02-04 12:56:52 -08001063 res = phy_reset(phydev);
Kevin Smith87b2c4e2016-03-31 19:33:12 +00001064 if (res < 0) {
Tim Harvey6e434b92019-02-04 12:56:52 -08001065 printf("Error resetting PHY %i\n", i);
Kevin Smith87b2c4e2016-03-31 19:33:12 +00001066 continue;
1067 }
Tim Harvey6e434b92019-02-04 12:56:52 -08001068 res = genphy_config_aneg(phydev);
Kevin Smith87b2c4e2016-03-31 19:33:12 +00001069 if (res < 0) {
Tim Harvey6e434b92019-02-04 12:56:52 -08001070 printf("Error setting PHY %i autoneg\n", i);
Kevin Smith87b2c4e2016-03-31 19:33:12 +00001071 continue;
1072 }
1073
1074 /* Return success if any PHY succeeds */
1075 ret = 0;
Chris Packham3da645f2016-08-26 17:30:26 +12001076 } else if ((1 << i) & CONFIG_MV88E61XX_FIXED_PORTS) {
1077 res = mv88e61xx_fixed_port_setup(phydev, i);
1078 if (res < 0) {
1079 printf("Error configuring port %i\n", i);
1080 continue;
1081 }
Kevin Smith87b2c4e2016-03-31 19:33:12 +00001082 }
1083 }
1084
1085 return ret;
1086}
1087
1088static int mv88e61xx_phy_is_connected(struct phy_device *phydev)
1089{
1090 int val;
1091
1092 val = mv88e61xx_phy_read(phydev, phydev->addr, PHY_REG_STATUS1);
1093 if (val < 0)
1094 return 0;
1095
1096 /*
1097 * After reset, the energy detect signal remains high for a few seconds
1098 * regardless of whether a cable is connected. This function will
1099 * return false positives during this time.
1100 */
1101 return (val & PHY_REG_STATUS1_ENERGY) == 0;
1102}
1103
1104static int mv88e61xx_phy_startup(struct phy_device *phydev)
1105{
Anatolij Gustschine4779172019-10-27 01:14:37 +02001106 struct mv88e61xx_phy_priv *priv = phydev->priv;
Kevin Smith87b2c4e2016-03-31 19:33:12 +00001107 int i;
1108 int link = 0;
1109 int res;
1110 int speed = phydev->speed;
1111 int duplex = phydev->duplex;
1112
Anatolij Gustschine4779172019-10-27 01:14:37 +02001113 for (i = 0; i < priv->port_count; i++) {
Kevin Smith87b2c4e2016-03-31 19:33:12 +00001114 if ((1 << i) & CONFIG_MV88E61XX_PHY_PORTS) {
1115 phydev->addr = i;
1116 if (!mv88e61xx_phy_is_connected(phydev))
1117 continue;
1118 res = genphy_update_link(phydev);
1119 if (res < 0)
1120 continue;
1121 res = mv88e61xx_parse_status(phydev);
1122 if (res < 0)
1123 continue;
1124 link = (link || phydev->link);
1125 }
1126 }
1127 phydev->link = link;
1128
1129 /* Restore CPU interface speed and duplex after it was changed for
1130 * other ports */
1131 phydev->speed = speed;
1132 phydev->duplex = duplex;
1133
1134 return 0;
1135}
1136
1137static struct phy_driver mv88e61xx_driver = {
1138 .name = "Marvell MV88E61xx",
1139 .uid = 0x01410eb1,
1140 .mask = 0xfffffff0,
1141 .features = PHY_GBIT_FEATURES,
1142 .probe = mv88e61xx_probe,
1143 .config = mv88e61xx_phy_config,
1144 .startup = mv88e61xx_phy_startup,
1145 .shutdown = &genphy_shutdown,
1146};
1147
Chris Packhamedc42a12016-08-26 17:30:25 +12001148static struct phy_driver mv88e609x_driver = {
1149 .name = "Marvell MV88E609x",
1150 .uid = 0x1410c89,
1151 .mask = 0xfffffff0,
1152 .features = PHY_GBIT_FEATURES,
1153 .probe = mv88e61xx_probe,
1154 .config = mv88e61xx_phy_config,
1155 .startup = mv88e61xx_phy_startup,
1156 .shutdown = &genphy_shutdown,
1157};
1158
Anatolij Gustschinb8b125a2019-10-27 01:14:40 +02001159static struct phy_driver mv88e6071_driver = {
1160 .name = "Marvell MV88E6071",
1161 .uid = 0x1410db0,
1162 .mask = 0xfffffff0,
1163 .features = PHY_BASIC_FEATURES | SUPPORTED_MII,
1164 .probe = mv88e61xx_probe,
1165 .config = mv88e61xx_phy_config,
1166 .startup = mv88e61xx_phy_startup,
1167 .shutdown = &genphy_shutdown,
1168};
1169
Kevin Smith87b2c4e2016-03-31 19:33:12 +00001170int phy_mv88e61xx_init(void)
1171{
1172 phy_register(&mv88e61xx_driver);
Chris Packhamedc42a12016-08-26 17:30:25 +12001173 phy_register(&mv88e609x_driver);
Anatolij Gustschinb8b125a2019-10-27 01:14:40 +02001174 phy_register(&mv88e6071_driver);
Kevin Smith87b2c4e2016-03-31 19:33:12 +00001175
1176 return 0;
1177}
1178
1179/*
1180 * Overload weak get_phy_id definition since we need non-standard functions
1181 * to read PHY registers
1182 */
1183int get_phy_id(struct mii_dev *bus, int smi_addr, int devad, u32 *phy_id)
1184{
1185 struct phy_device temp_phy;
1186 struct mv88e61xx_phy_priv temp_priv;
1187 struct mii_dev temp_mii;
1188 int val;
1189
1190 /*
1191 * Buid temporary data structures that the chip reading code needs to
1192 * read the ID
1193 */
1194 temp_priv.mdio_bus = bus;
1195 temp_priv.smi_addr = smi_addr;
1196 temp_phy.priv = &temp_priv;
1197 temp_mii.priv = &temp_phy;
1198
Anatolij Gustschine4779172019-10-27 01:14:37 +02001199 /*
1200 * get_phy_id() can be called by framework before mv88e61xx driver
1201 * probing, in this case the global register offsets are not
1202 * initialized yet. Do this initialization here before indirect
1203 * PHY register access.
1204 */
1205 val = mv88e61xx_priv_reg_offs_pre_init(&temp_phy);
1206 if (val < 0)
1207 return val;
1208
Kevin Smith87b2c4e2016-03-31 19:33:12 +00001209 val = mv88e61xx_phy_read_indirect(&temp_mii, 0, devad, MII_PHYSID1);
1210 if (val < 0)
1211 return -EIO;
1212
1213 *phy_id = val << 16;
1214
1215 val = mv88e61xx_phy_read_indirect(&temp_mii, 0, devad, MII_PHYSID2);
1216 if (val < 0)
1217 return -EIO;
1218
1219 *phy_id |= (val & 0xffff);
1220
1221 return 0;
1222}