9aba04253c48eb4bd8e58f060ee37ea95d7a694b
[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_LINK            BIT(11)
88 #define PORT_REG_STATUS_DUPLEX          BIT(10)
89
90 #define PORT_REG_STATUS_SPEED_SHIFT     8
91 #define PORT_REG_STATUS_SPEED_WIDTH     2
92 #define PORT_REG_STATUS_SPEED_10        0
93 #define PORT_REG_STATUS_SPEED_100       1
94 #define PORT_REG_STATUS_SPEED_1000      2
95
96 #define PORT_REG_STATUS_CMODE_MASK              0xF
97 #define PORT_REG_STATUS_CMODE_100BASE_X         0x8
98 #define PORT_REG_STATUS_CMODE_1000BASE_X        0x9
99 #define PORT_REG_STATUS_CMODE_SGMII             0xa
100
101 #define PORT_REG_PHYS_CTRL_PCS_AN_EN    BIT(10)
102 #define PORT_REG_PHYS_CTRL_PCS_AN_RST   BIT(9)
103 #define PORT_REG_PHYS_CTRL_FC_VALUE     BIT(7)
104 #define PORT_REG_PHYS_CTRL_FC_FORCE     BIT(6)
105 #define PORT_REG_PHYS_CTRL_LINK_VALUE   BIT(5)
106 #define PORT_REG_PHYS_CTRL_LINK_FORCE   BIT(4)
107 #define PORT_REG_PHYS_CTRL_DUPLEX_VALUE BIT(3)
108 #define PORT_REG_PHYS_CTRL_DUPLEX_FORCE BIT(2)
109 #define PORT_REG_PHYS_CTRL_SPD1000      BIT(1)
110 #define PORT_REG_PHYS_CTRL_SPD_MASK     (BIT(1) | BIT(0))
111
112 #define PORT_REG_CTRL_PSTATE_SHIFT      0
113 #define PORT_REG_CTRL_PSTATE_WIDTH      2
114
115 #define PORT_REG_VLAN_ID_DEF_VID_SHIFT  0
116 #define PORT_REG_VLAN_ID_DEF_VID_WIDTH  12
117
118 #define PORT_REG_VLAN_MAP_TABLE_SHIFT   0
119 #define PORT_REG_VLAN_MAP_TABLE_WIDTH   11
120
121 #define SERDES_REG_CTRL_1_FORCE_LINK    BIT(10)
122
123 #define PHY_REG_CTRL1_ENERGY_DET_SHIFT  8
124 #define PHY_REG_CTRL1_ENERGY_DET_WIDTH  2
125
126 /* Field values */
127 #define PORT_REG_CTRL_PSTATE_DISABLED   0
128 #define PORT_REG_CTRL_PSTATE_FORWARD    3
129
130 #define PHY_REG_CTRL1_ENERGY_DET_OFF    0
131 #define PHY_REG_CTRL1_ENERGY_DET_SENSE_ONLY     2
132 #define PHY_REG_CTRL1_ENERGY_DET_SENSE_XMIT     3
133
134 /* PHY Status Register */
135 #define PHY_REG_STATUS1_SPEED           0xc000
136 #define PHY_REG_STATUS1_GBIT            0x8000
137 #define PHY_REG_STATUS1_100             0x4000
138 #define PHY_REG_STATUS1_DUPLEX          0x2000
139 #define PHY_REG_STATUS1_SPDDONE         0x0800
140 #define PHY_REG_STATUS1_LINK            0x0400
141 #define PHY_REG_STATUS1_ENERGY          0x0010
142
143 /*
144  * Macros for building commands for indirect addressing modes.  These are valid
145  * for both the indirect multichip addressing mode and the PHY indirection
146  * required for the writes to any PHY register.
147  */
148 #define SMI_BUSY                        BIT(15)
149 #define SMI_CMD_CLAUSE_22               BIT(12)
150 #define SMI_CMD_CLAUSE_22_OP_READ       (2 << 10)
151 #define SMI_CMD_CLAUSE_22_OP_WRITE      (1 << 10)
152
153 #define SMI_CMD_READ                    (SMI_BUSY | SMI_CMD_CLAUSE_22 | \
154                                          SMI_CMD_CLAUSE_22_OP_READ)
155 #define SMI_CMD_WRITE                   (SMI_BUSY | SMI_CMD_CLAUSE_22 | \
156                                          SMI_CMD_CLAUSE_22_OP_WRITE)
157
158 #define SMI_CMD_ADDR_SHIFT              5
159 #define SMI_CMD_ADDR_WIDTH              5
160 #define SMI_CMD_REG_SHIFT               0
161 #define SMI_CMD_REG_WIDTH               5
162
163 /* Check for required macros */
164 #ifndef CONFIG_MV88E61XX_PHY_PORTS
165 #error Define CONFIG_MV88E61XX_PHY_PORTS to indicate which physical ports \
166         to activate
167 #endif
168 #ifndef CONFIG_MV88E61XX_CPU_PORT
169 #error Define CONFIG_MV88E61XX_CPU_PORT to the port the CPU is attached to
170 #endif
171
172 /*
173  *  These are ports without PHYs that may be wired directly
174  * to other serdes interfaces
175  */
176 #ifndef CONFIG_MV88E61XX_FIXED_PORTS
177 #define CONFIG_MV88E61XX_FIXED_PORTS 0
178 #endif
179
180 /* ID register values for different switch models */
181 #define PORT_SWITCH_ID_6020             0x0200
182 #define PORT_SWITCH_ID_6070             0x0700
183 #define PORT_SWITCH_ID_6071             0x0710
184 #define PORT_SWITCH_ID_6096             0x0980
185 #define PORT_SWITCH_ID_6097             0x0990
186 #define PORT_SWITCH_ID_6172             0x1720
187 #define PORT_SWITCH_ID_6176             0x1760
188 #define PORT_SWITCH_ID_6220             0x2200
189 #define PORT_SWITCH_ID_6240             0x2400
190 #define PORT_SWITCH_ID_6250             0x2500
191 #define PORT_SWITCH_ID_6352             0x3520
192
193 struct mv88e61xx_phy_priv {
194         struct mii_dev *mdio_bus;
195         int smi_addr;
196         int id;
197         int port_count;         /* Number of switch ports */
198         int port_reg_base;      /* Base of the switch port registers */
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         int res;
648         int val;
649         bool forced = false;
650
651         val = mv88e61xx_port_read(phydev, port, PORT_REG_STATUS);
652         if (val < 0)
653                 return val;
654         if (!(val & PORT_REG_STATUS_LINK)) {
655                 /* Temporarily force link to read port configuration */
656                 u32 timeout = 100;
657                 forced = true;
658
659                 val = mv88e61xx_port_read(phydev, port, PORT_REG_PHYS_CTRL);
660                 if (val < 0)
661                         return val;
662                 val |= (PORT_REG_PHYS_CTRL_LINK_FORCE |
663                                 PORT_REG_PHYS_CTRL_LINK_VALUE);
664                 val = mv88e61xx_port_write(phydev, port, PORT_REG_PHYS_CTRL,
665                                            val);
666                 if (val < 0)
667                         return val;
668
669                 /* Wait for status register to reflect forced link */
670                 do {
671                         val = mv88e61xx_port_read(phydev, port,
672                                                   PORT_REG_STATUS);
673                         if (val < 0) {
674                                 res = -EIO;
675                                 goto unforce;
676                         }
677                         if (val & PORT_REG_STATUS_LINK)
678                                 break;
679                 } while (--timeout);
680
681                 if (timeout == 0) {
682                         res = -ETIMEDOUT;
683                         goto unforce;
684                 }
685         }
686
687         if (val & PORT_REG_STATUS_DUPLEX)
688                 phydev->duplex = DUPLEX_FULL;
689         else
690                 phydev->duplex = DUPLEX_HALF;
691
692         val = bitfield_extract(val, PORT_REG_STATUS_SPEED_SHIFT,
693                                PORT_REG_STATUS_SPEED_WIDTH);
694         switch (val) {
695         case PORT_REG_STATUS_SPEED_1000:
696                 phydev->speed = SPEED_1000;
697                 break;
698         case PORT_REG_STATUS_SPEED_100:
699                 phydev->speed = SPEED_100;
700                 break;
701         default:
702                 phydev->speed = SPEED_10;
703                 break;
704         }
705
706         res = 0;
707
708 unforce:
709         if (forced) {
710                 val = mv88e61xx_port_read(phydev, port, PORT_REG_PHYS_CTRL);
711                 if (val < 0)
712                         return val;
713                 val &= ~(PORT_REG_PHYS_CTRL_LINK_FORCE |
714                                 PORT_REG_PHYS_CTRL_LINK_VALUE);
715                 val = mv88e61xx_port_write(phydev, port, PORT_REG_PHYS_CTRL,
716                                            val);
717                 if (val < 0)
718                         return val;
719         }
720
721         return res;
722 }
723
724 static int mv88e61xx_fixed_port_setup(struct phy_device *phydev, u8 port)
725 {
726         int val;
727
728         val = mv88e61xx_port_read(phydev, port, PORT_REG_PHYS_CTRL);
729         if (val < 0)
730                 return val;
731
732         val &= ~(PORT_REG_PHYS_CTRL_SPD_MASK |
733                  PORT_REG_PHYS_CTRL_FC_VALUE);
734         val |= PORT_REG_PHYS_CTRL_PCS_AN_EN |
735                PORT_REG_PHYS_CTRL_PCS_AN_RST |
736                PORT_REG_PHYS_CTRL_FC_FORCE |
737                PORT_REG_PHYS_CTRL_DUPLEX_VALUE |
738                PORT_REG_PHYS_CTRL_DUPLEX_FORCE |
739                PORT_REG_PHYS_CTRL_SPD1000;
740
741         if (port == CONFIG_MV88E61XX_CPU_PORT)
742                 val |= PORT_REG_PHYS_CTRL_LINK_VALUE |
743                        PORT_REG_PHYS_CTRL_LINK_FORCE;
744
745         return mv88e61xx_port_write(phydev, port, PORT_REG_PHYS_CTRL,
746                                    val);
747 }
748
749 static int mv88e61xx_set_cpu_port(struct phy_device *phydev)
750 {
751         struct mv88e61xx_phy_priv *priv = phydev->priv;
752         int val;
753
754         /* Set CPUDest */
755         val = mv88e61xx_reg_read(phydev, priv->global1, GLOBAL1_MON_CTRL);
756         if (val < 0)
757                 return val;
758         val = bitfield_replace(val, GLOBAL1_MON_CTRL_CPUDEST_SHIFT,
759                                GLOBAL1_MON_CTRL_CPUDEST_WIDTH,
760                                CONFIG_MV88E61XX_CPU_PORT);
761         val = mv88e61xx_reg_write(phydev, priv->global1,
762                                   GLOBAL1_MON_CTRL, val);
763         if (val < 0)
764                 return val;
765
766         /* Allow CPU to route to any port */
767         val = PORT_MASK(priv->port_count) & ~(1 << CONFIG_MV88E61XX_CPU_PORT);
768         val = mv88e61xx_port_set_vlan(phydev, CONFIG_MV88E61XX_CPU_PORT, val);
769         if (val < 0)
770                 return val;
771
772         /* Enable CPU port */
773         val = mv88e61xx_port_enable(phydev, CONFIG_MV88E61XX_CPU_PORT);
774         if (val < 0)
775                 return val;
776
777         val = mv88e61xx_read_port_config(phydev, CONFIG_MV88E61XX_CPU_PORT);
778         if (val < 0)
779                 return val;
780
781         /* If CPU is connected to serdes, initialize serdes */
782         if (mv88e61xx_6352_family(phydev)) {
783                 val = mv88e61xx_get_cmode(phydev, CONFIG_MV88E61XX_CPU_PORT);
784                 if (val < 0)
785                         return val;
786                 if (val == PORT_REG_STATUS_CMODE_100BASE_X ||
787                     val == PORT_REG_STATUS_CMODE_1000BASE_X ||
788                     val == PORT_REG_STATUS_CMODE_SGMII) {
789                         val = mv88e61xx_serdes_init(phydev);
790                         if (val < 0)
791                                 return val;
792                 }
793         } else {
794                 val = mv88e61xx_fixed_port_setup(phydev,
795                                                  CONFIG_MV88E61XX_CPU_PORT);
796                 if (val < 0)
797                         return val;
798         }
799
800         return 0;
801 }
802
803 static int mv88e61xx_switch_init(struct phy_device *phydev)
804 {
805         static int init;
806         int res;
807
808         if (init)
809                 return 0;
810
811         res = mv88e61xx_switch_reset(phydev);
812         if (res < 0)
813                 return res;
814
815         res = mv88e61xx_set_cpu_port(phydev);
816         if (res < 0)
817                 return res;
818
819         init = 1;
820
821         return 0;
822 }
823
824 static int mv88e61xx_phy_enable(struct phy_device *phydev, u8 phy)
825 {
826         int val;
827
828         val = mv88e61xx_phy_read(phydev, phy, MII_BMCR);
829         if (val < 0)
830                 return val;
831         val &= ~(BMCR_PDOWN);
832         val = mv88e61xx_phy_write(phydev, phy, MII_BMCR, val);
833         if (val < 0)
834                 return val;
835
836         return 0;
837 }
838
839 static int mv88e61xx_phy_setup(struct phy_device *phydev, u8 phy)
840 {
841         int val;
842
843         /*
844          * Enable energy-detect sensing on PHY, used to determine when a PHY
845          * port is physically connected
846          */
847         val = mv88e61xx_phy_read(phydev, phy, PHY_REG_CTRL1);
848         if (val < 0)
849                 return val;
850         val = bitfield_replace(val, PHY_REG_CTRL1_ENERGY_DET_SHIFT,
851                                PHY_REG_CTRL1_ENERGY_DET_WIDTH,
852                                PHY_REG_CTRL1_ENERGY_DET_SENSE_XMIT);
853         val = mv88e61xx_phy_write(phydev, phy, PHY_REG_CTRL1, val);
854         if (val < 0)
855                 return val;
856
857         return 0;
858 }
859
860 static int mv88e61xx_phy_config_port(struct phy_device *phydev, u8 phy)
861 {
862         int val;
863
864         val = mv88e61xx_port_enable(phydev, phy);
865         if (val < 0)
866                 return val;
867
868         val = mv88e61xx_port_set_vlan(phydev, phy,
869                         1 << CONFIG_MV88E61XX_CPU_PORT);
870         if (val < 0)
871                 return val;
872
873         return 0;
874 }
875
876 /*
877  * This function is used to pre-configure the required register
878  * offsets, so that the indirect register access to the PHY registers
879  * is possible. This is necessary to be able to read the PHY ID
880  * while driver probing or in get_phy_id(). The globalN register
881  * offsets must be initialized correctly for a detected switch,
882  * otherwise detection of the PHY ID won't work!
883  */
884 static int mv88e61xx_priv_reg_offs_pre_init(struct phy_device *phydev)
885 {
886         struct mv88e61xx_phy_priv *priv = phydev->priv;
887
888         /*
889          * Initial 'port_reg_base' value must be an offset of existing
890          * port register, then reading the ID should succeed. First, try
891          * to read via port registers with device address 0x10 (88E6096
892          * and compatible switches).
893          */
894         priv->port_reg_base = 0x10;
895         priv->id = mv88e61xx_get_switch_id(phydev);
896         if (priv->id != 0xfff0) {
897                 priv->global1 = 0x1B;
898                 priv->global2 = 0x1C;
899                 return 0;
900         }
901
902         /*
903          * Now try via port registers with device address 0x08
904          * (88E6020 and compatible switches).
905          */
906         priv->port_reg_base = 0x08;
907         priv->id = mv88e61xx_get_switch_id(phydev);
908         if (priv->id != 0xfff0) {
909                 priv->global1 = 0x0F;
910                 priv->global2 = 0x07;
911                 return 0;
912         }
913
914         debug("%s Unknown ID 0x%x\n", __func__, priv->id);
915         return -ENODEV;
916 }
917
918 static int mv88e61xx_probe(struct phy_device *phydev)
919 {
920         struct mii_dev *smi_wrapper;
921         struct mv88e61xx_phy_priv *priv;
922         int res;
923
924         res = mv88e61xx_hw_reset(phydev);
925         if (res < 0)
926                 return res;
927
928         priv = malloc(sizeof(*priv));
929         if (!priv)
930                 return -ENOMEM;
931
932         memset(priv, 0, sizeof(*priv));
933
934         /*
935          * This device requires indirect reads/writes to the PHY registers
936          * which the generic PHY code can't handle.  Make a wrapper MII device
937          * to handle reads/writes
938          */
939         smi_wrapper = mdio_alloc();
940         if (!smi_wrapper) {
941                 free(priv);
942                 return -ENOMEM;
943         }
944
945         /*
946          * Store the mdio bus in the private data, as we are going to replace
947          * the bus with the wrapper bus
948          */
949         priv->mdio_bus = phydev->bus;
950
951         /*
952          * Store the smi bus address in private data.  This lets us use the
953          * phydev addr field for device address instead, as the genphy code
954          * expects.
955          */
956         priv->smi_addr = phydev->addr;
957
958         /*
959          * Store the phy_device in the wrapper mii device. This lets us get it
960          * back when genphy functions call phy_read/phy_write.
961          */
962         smi_wrapper->priv = phydev;
963         strncpy(smi_wrapper->name, "indirect mii", sizeof(smi_wrapper->name));
964         smi_wrapper->read = mv88e61xx_phy_read_indirect;
965         smi_wrapper->write = mv88e61xx_phy_write_indirect;
966
967         /* Replace the bus with the wrapper device */
968         phydev->bus = smi_wrapper;
969
970         phydev->priv = priv;
971
972         res = mv88e61xx_priv_reg_offs_pre_init(phydev);
973         if (res < 0)
974                 return res;
975
976         debug("%s ID 0x%x\n", __func__, priv->id);
977
978         switch (priv->id) {
979         case PORT_SWITCH_ID_6096:
980         case PORT_SWITCH_ID_6097:
981         case PORT_SWITCH_ID_6172:
982         case PORT_SWITCH_ID_6176:
983         case PORT_SWITCH_ID_6240:
984         case PORT_SWITCH_ID_6352:
985                 priv->port_count = 11;
986                 break;
987         case PORT_SWITCH_ID_6020:
988         case PORT_SWITCH_ID_6070:
989         case PORT_SWITCH_ID_6071:
990         case PORT_SWITCH_ID_6220:
991         case PORT_SWITCH_ID_6250:
992                 priv->port_count = 7;
993                 break;
994         default:
995                 free(priv);
996                 return -ENODEV;
997         }
998
999         res = mdio_register(smi_wrapper);
1000         if (res)
1001                 printf("Failed to register SMI bus\n");
1002
1003         return 0;
1004 }
1005
1006 static int mv88e61xx_phy_config(struct phy_device *phydev)
1007 {
1008         struct mv88e61xx_phy_priv *priv = phydev->priv;
1009         int res;
1010         int i;
1011         int ret = -1;
1012
1013         res = mv88e61xx_switch_init(phydev);
1014         if (res < 0)
1015                 return res;
1016
1017         for (i = 0; i < priv->port_count; i++) {
1018                 if ((1 << i) & CONFIG_MV88E61XX_PHY_PORTS) {
1019                         phydev->addr = i;
1020
1021                         res = mv88e61xx_phy_enable(phydev, i);
1022                         if (res < 0) {
1023                                 printf("Error enabling PHY %i\n", i);
1024                                 continue;
1025                         }
1026                         res = mv88e61xx_phy_setup(phydev, i);
1027                         if (res < 0) {
1028                                 printf("Error setting up PHY %i\n", i);
1029                                 continue;
1030                         }
1031                         res = mv88e61xx_phy_config_port(phydev, i);
1032                         if (res < 0) {
1033                                 printf("Error configuring PHY %i\n", i);
1034                                 continue;
1035                         }
1036
1037                         res = phy_reset(phydev);
1038                         if (res < 0) {
1039                                 printf("Error resetting PHY %i\n", i);
1040                                 continue;
1041                         }
1042                         res = genphy_config_aneg(phydev);
1043                         if (res < 0) {
1044                                 printf("Error setting PHY %i autoneg\n", i);
1045                                 continue;
1046                         }
1047
1048                         /* Return success if any PHY succeeds */
1049                         ret = 0;
1050                 } else if ((1 << i) & CONFIG_MV88E61XX_FIXED_PORTS) {
1051                         res = mv88e61xx_fixed_port_setup(phydev, i);
1052                         if (res < 0) {
1053                                 printf("Error configuring port %i\n", i);
1054                                 continue;
1055                         }
1056                 }
1057         }
1058
1059         return ret;
1060 }
1061
1062 static int mv88e61xx_phy_is_connected(struct phy_device *phydev)
1063 {
1064         int val;
1065
1066         val = mv88e61xx_phy_read(phydev, phydev->addr, PHY_REG_STATUS1);
1067         if (val < 0)
1068                 return 0;
1069
1070         /*
1071          * After reset, the energy detect signal remains high for a few seconds
1072          * regardless of whether a cable is connected.  This function will
1073          * return false positives during this time.
1074          */
1075         return (val & PHY_REG_STATUS1_ENERGY) == 0;
1076 }
1077
1078 static int mv88e61xx_phy_startup(struct phy_device *phydev)
1079 {
1080         struct mv88e61xx_phy_priv *priv = phydev->priv;
1081         int i;
1082         int link = 0;
1083         int res;
1084         int speed = phydev->speed;
1085         int duplex = phydev->duplex;
1086
1087         for (i = 0; i < priv->port_count; i++) {
1088                 if ((1 << i) & CONFIG_MV88E61XX_PHY_PORTS) {
1089                         phydev->addr = i;
1090                         if (!mv88e61xx_phy_is_connected(phydev))
1091                                 continue;
1092                         res = genphy_update_link(phydev);
1093                         if (res < 0)
1094                                 continue;
1095                         res = mv88e61xx_parse_status(phydev);
1096                         if (res < 0)
1097                                 continue;
1098                         link = (link || phydev->link);
1099                 }
1100         }
1101         phydev->link = link;
1102
1103         /* Restore CPU interface speed and duplex after it was changed for
1104          * other ports */
1105         phydev->speed = speed;
1106         phydev->duplex = duplex;
1107
1108         return 0;
1109 }
1110
1111 static struct phy_driver mv88e61xx_driver = {
1112         .name = "Marvell MV88E61xx",
1113         .uid = 0x01410eb1,
1114         .mask = 0xfffffff0,
1115         .features = PHY_GBIT_FEATURES,
1116         .probe = mv88e61xx_probe,
1117         .config = mv88e61xx_phy_config,
1118         .startup = mv88e61xx_phy_startup,
1119         .shutdown = &genphy_shutdown,
1120 };
1121
1122 static struct phy_driver mv88e609x_driver = {
1123         .name = "Marvell MV88E609x",
1124         .uid = 0x1410c89,
1125         .mask = 0xfffffff0,
1126         .features = PHY_GBIT_FEATURES,
1127         .probe = mv88e61xx_probe,
1128         .config = mv88e61xx_phy_config,
1129         .startup = mv88e61xx_phy_startup,
1130         .shutdown = &genphy_shutdown,
1131 };
1132
1133 int phy_mv88e61xx_init(void)
1134 {
1135         phy_register(&mv88e61xx_driver);
1136         phy_register(&mv88e609x_driver);
1137
1138         return 0;
1139 }
1140
1141 /*
1142  * Overload weak get_phy_id definition since we need non-standard functions
1143  * to read PHY registers
1144  */
1145 int get_phy_id(struct mii_dev *bus, int smi_addr, int devad, u32 *phy_id)
1146 {
1147         struct phy_device temp_phy;
1148         struct mv88e61xx_phy_priv temp_priv;
1149         struct mii_dev temp_mii;
1150         int val;
1151
1152         /*
1153          * Buid temporary data structures that the chip reading code needs to
1154          * read the ID
1155          */
1156         temp_priv.mdio_bus = bus;
1157         temp_priv.smi_addr = smi_addr;
1158         temp_phy.priv = &temp_priv;
1159         temp_mii.priv = &temp_phy;
1160
1161         /*
1162          * get_phy_id() can be called by framework before mv88e61xx driver
1163          * probing, in this case the global register offsets are not
1164          * initialized yet. Do this initialization here before indirect
1165          * PHY register access.
1166          */
1167         val = mv88e61xx_priv_reg_offs_pre_init(&temp_phy);
1168         if (val < 0)
1169                 return val;
1170
1171         val = mv88e61xx_phy_read_indirect(&temp_mii, 0, devad, MII_PHYSID1);
1172         if (val < 0)
1173                 return -EIO;
1174
1175         *phy_id = val << 16;
1176
1177         val = mv88e61xx_phy_read_indirect(&temp_mii, 0, devad, MII_PHYSID2);
1178         if (val < 0)
1179                 return -EIO;
1180
1181         *phy_id |= (val & 0xffff);
1182
1183         return 0;
1184 }