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