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