3 * Marvell Semiconductor <www.marvell.com>
4 * Prafulla Wadaskar <prafulla@marvell.com>
6 * SPDX-License-Identifier: GPL-2.0+
11 #include "mv88e61xx.h"
14 * Uncomment either of the following line for local debug control;
15 * otherwise global debug control will apply.
21 #ifdef CONFIG_MV88E61XX_MULTICHIP_ADRMODE
23 * The Switch support two modes of operation
24 * 1. single chip mode and
26 * Refer section 9.2 &9.3 in chip datasheet-02 for more details
28 * By default single chip mode is configured
29 * multichip mode operation can be configured in board header
31 static int mv88e61xx_busychk_multic(char *name, u32 devaddr)
34 u32 timeout = MV88E61XX_PHY_TIMEOUT;
36 /* Poll till SMIBusy bit is clear */
38 miiphy_read(name, devaddr, 0x0, ®);
40 printf("SMI busy timeout\n");
43 } while (reg & (1 << 15));
47 static void mv88e61xx_switch_write(char *name, u32 phy_adr,
48 u32 reg_ofs, u16 data)
52 /* command to read PHY dev address */
53 if (miiphy_read(name, 0xEE, 0xEE, &mii_dev_addr)) {
54 printf("Error..could not read PHY dev address\n");
57 mv88e61xx_busychk_multic(name, mii_dev_addr);
58 /* Write data to Switch indirect data register */
59 miiphy_write(name, mii_dev_addr, 0x1, data);
60 /* Write command to Switch indirect command register (write) */
61 miiphy_write(name, mii_dev_addr, 0x0,
62 reg_ofs | (phy_adr << 5) | (1 << 10) | (1 << 12) | (1 <<
66 static void mv88e61xx_switch_read(char *name, u32 phy_adr,
67 u32 reg_ofs, u16 *data)
71 /* command to read PHY dev address */
72 if (miiphy_read(name, 0xEE, 0xEE, &mii_dev_addr)) {
73 printf("Error..could not read PHY dev address\n");
76 mv88e61xx_busychk_multic(name, mii_dev_addr);
77 /* Write command to Switch indirect command register (read) */
78 miiphy_write(name, mii_dev_addr, 0x0,
79 reg_ofs | (phy_adr << 5) | (1 << 11) | (1 << 12) | (1 <<
81 mv88e61xx_busychk_multic(name, mii_dev_addr);
82 /* Read data from Switch indirect data register */
83 miiphy_read(name, mii_dev_addr, 0x1, data);
85 #endif /* CONFIG_MV88E61XX_MULTICHIP_ADRMODE */
88 * Convenience macros for switch device/port reads/writes
89 * These macros output valid 'mv88e61xx' U_BOOT_CMDs
93 #define WR_SWITCH_REG wr_switch_reg
94 #define RD_SWITCH_REG rd_switch_reg
95 #define WR_SWITCH_PORT_REG(n, p, r, d) \
96 WR_SWITCH_REG(n, (MV88E61XX_PRT_OFST+p), r, d)
97 #define RD_SWITCH_PORT_REG(n, p, r, d) \
98 RD_SWITCH_REG(n, (MV88E61XX_PRT_OFST+p), r, d)
100 static void WR_SWITCH_REG(char *name, u32 dev_adr, u32 reg_ofs, u16 data)
102 printf("mv88e61xx %s dev %02x reg %02x write %04x\n",
103 name, dev_adr, reg_ofs, data);
104 wr_switch_reg(name, dev_adr, reg_ofs, data);
106 static void RD_SWITCH_REG(char *name, u32 dev_adr, u32 reg_ofs, u16 *data)
108 rd_switch_reg(name, dev_adr, reg_ofs, data);
109 printf("mv88e61xx %s dev %02x reg %02x read %04x\n",
110 name, dev_adr, reg_ofs, *data);
112 static void WR_SWITCH_PORT_REG(char *name, u32 prt_adr, u32 reg_ofs,
115 printf("mv88e61xx %s port %02x reg %02x write %04x\n",
116 name, prt_adr, reg_ofs, data);
117 wr_switch_reg(name, (MV88E61XX_PRT_OFST+prt_adr), reg_ofs, data);
119 static void RD_SWITCH_PORT_REG(char *name, u32 prt_adr, u32 reg_ofs,
122 rd_switch_reg(name, (MV88E61XX_PRT_OFST+prt_adr), reg_ofs, data);
123 printf("mv88e61xx %s port %02x reg %02x read %04x\n",
124 name, prt_adr, reg_ofs, *data);
129 * Local functions to read/write registers on the switch PHYs.
130 * NOTE! This goes through switch, not direct miiphy, writes and reads!
134 * Make sure SMIBusy bit cleared before another
135 * SMI operation can take place
137 static int mv88e61xx_busychk(char *name)
140 u32 timeout = MV88E61XX_PHY_TIMEOUT;
142 rd_switch_reg(name, MV88E61XX_GLB2REG_DEVADR,
143 MV88E61XX_PHY_CMD, ®);
144 if (timeout-- == 0) {
145 printf("SMI busy timeout\n");
148 } while (reg & 1 << 15); /* busy mask */
152 static inline int mv88e61xx_switch_miiphy_write(char *name, u32 phy,
155 /* write switch data reg then cmd reg then check completion */
156 wr_switch_reg(name, MV88E61XX_GLB2REG_DEVADR, MV88E61XX_PHY_DATA,
158 wr_switch_reg(name, MV88E61XX_GLB2REG_DEVADR, MV88E61XX_PHY_CMD,
159 (MV88E61XX_PHY_WRITE_CMD | (phy << 5) | reg));
160 return mv88e61xx_busychk(name);
163 static inline int mv88e61xx_switch_miiphy_read(char *name, u32 phy,
166 /* write switch cmd reg, check for completion */
167 wr_switch_reg(name, MV88E61XX_GLB2REG_DEVADR, MV88E61XX_PHY_CMD,
168 (MV88E61XX_PHY_READ_CMD | (phy << 5) | reg));
169 if (mv88e61xx_busychk(name))
171 /* read switch data reg and return success */
172 rd_switch_reg(name, MV88E61XX_GLB2REG_DEVADR, MV88E61XX_PHY_DATA, data);
177 * Convenience macros for switch PHY reads/writes
181 #define WR_SWITCH_PHY_REG mv88e61xx_switch_miiphy_write
182 #define RD_SWITCH_PHY_REG mv88e61xx_switch_miiphy_read
184 static inline int WR_SWITCH_PHY_REG(char *name, u32 phy_adr,
185 u32 reg_ofs, u16 data)
187 int r = mv88e61xx_switch_miiphy_write(name, phy_adr, reg_ofs, data);
189 printf("** ERROR writing mv88e61xx %s phy %02x reg %02x\n",
190 name, phy_adr, reg_ofs);
192 printf("mv88e61xx %s phy %02x reg %02x write %04x\n",
193 name, phy_adr, reg_ofs, data);
196 static inline int RD_SWITCH_PHY_REG(char *name, u32 phy_adr,
197 u32 reg_ofs, u16 *data)
199 int r = mv88e61xx_switch_miiphy_read(name, phy_adr, reg_ofs, data);
201 printf("** ERROR reading mv88e61xx %s phy %02x reg %02x\n",
202 name, phy_adr, reg_ofs);
204 printf("mv88e61xx %s phy %02x reg %02x read %04x\n",
205 name, phy_adr, reg_ofs, *data);
210 static void mv88e61xx_port_vlan_config(struct mv88e61xx_config *swconfig)
214 char *name = swconfig->name;
215 u32 port_mask = swconfig->ports_enabled;
217 /* apply internal vlan config */
218 for (prt = 0; prt < MV88E61XX_MAX_PORTS_NUM; prt++) {
219 /* only for enabled ports */
220 if ((1 << prt) & port_mask) {
221 /* take vlan map from swconfig */
222 u8 vlanmap = swconfig->vlancfg[prt];
223 /* remove disabled ports from vlan map */
224 vlanmap &= swconfig->ports_enabled;
225 /* apply vlan map to port */
226 RD_SWITCH_PORT_REG(name, prt,
227 MV88E61XX_PRT_VMAP_REG, ®);
228 reg &= ~((1 << MV88E61XX_MAX_PORTS_NUM) - 1);
230 WR_SWITCH_PORT_REG(name, prt,
231 MV88E61XX_PRT_VMAP_REG, reg);
237 * Power up the specified port and reset PHY
239 static int mv88361xx_powerup(struct mv88e61xx_config *swconfig, u32 phy)
241 char *name = swconfig->name;
243 /* Write Copper Specific control reg1 (0x10) for-
244 * Enable Phy power up
245 * Energy Detect on (sense&Xmit NLP Periodically
246 * reset other settings default
248 if (WR_SWITCH_PHY_REG(name, phy, 0x10, 0x3360))
251 /* Write PHY ctrl reg (0x0) to apply
252 * Phy reset (set bit 15 low)
253 * reset other default values
255 if (WR_SWITCH_PHY_REG(name, phy, 0x00, 0x9140))
262 * Default Setup for LED[0]_Control (ref: Table 46 Datasheet-3)
263 * is set to "On-1000Mb/s Link, Off Else"
264 * This function sets it to "On-Link, Blink-Activity, Off-NoLink"
266 * This is optional settings may be needed on some boards
267 * to setup PHY LEDs default configuration to detect 10/100/1000Mb/s
270 static int mv88361xx_led_init(struct mv88e61xx_config *swconfig, u32 phy)
272 char *name = swconfig->name;
274 if (swconfig->led_init != MV88E61XX_LED_INIT_EN)
277 /* set page address to 3 */
278 if (WR_SWITCH_PHY_REG(name, phy, 0x16, 0x0003))
282 * set LED Func Ctrl reg
283 * value 0x0001 = LED[0] On-Link, Blink-Activity, Off-NoLink
285 if (WR_SWITCH_PHY_REG(name, phy, 0x10, 0x0001))
288 /* set page address to 0 */
289 if (WR_SWITCH_PHY_REG(name, phy, 0x16, 0x0000))
296 * Reverse Transmit polarity for Media Dependent Interface
297 * Pins (MDIP) bits in Copper Specific Control Register 3
298 * (Page 0, Reg 20 for each phy (except cpu port)
299 * Reference: Section 1.1 Switch datasheet-3
301 * This is optional settings may be needed on some boards
302 * for PHY<->magnetics h/w tuning
304 static int mv88361xx_reverse_mdipn(struct mv88e61xx_config *swconfig, u32 phy)
306 char *name = swconfig->name;
308 if (swconfig->mdip != MV88E61XX_MDIP_REVERSE)
311 /*Reverse MDIP/N[3:0] bits */
312 if (WR_SWITCH_PHY_REG(name, phy, 0x14, 0x000f))
319 * Marvell 88E61XX Switch initialization
321 int mv88e61xx_switch_initialize(struct mv88e61xx_config *swconfig)
326 char *name = swconfig->name;
329 if (miiphy_set_current_dev(name)) {
330 printf("%s failed\n", __FUNCTION__);
334 if (!(swconfig->cpuport & ((1 << 4) | (1 << 5)))) {
335 swconfig->cpuport = (1 << 5);
336 printf("Invalid cpu port config, using default port5\n");
339 RD_SWITCH_PORT_REG(name, 0, MII_PHYSID2, ®);
340 switch (reg &= 0xfff0) {
349 /* ports 2,3,4 not available */
350 swconfig->ports_enabled &= 0x023;
353 /* Could not detect switch id */
358 /* be sure all ports are disabled */
359 for (prt = 0; prt < MV88E61XX_MAX_PORTS_NUM; prt++) {
360 RD_SWITCH_PORT_REG(name, prt, MV88E61XX_PRT_CTRL_REG, ®);
362 WR_SWITCH_PORT_REG(name, prt, MV88E61XX_PRT_CTRL_REG, reg);
365 /* wait 2 ms for queues to drain */
369 RD_SWITCH_REG(name, MV88E61XX_GLBREG_DEVADR, MV88E61XX_SGCR, ®);
371 WR_SWITCH_REG(name, MV88E61XX_GLBREG_DEVADR, MV88E61XX_SGCR, reg);
373 /* wait up to 1 second for switch reset complete */
374 for (time = 1000; time; time--) {
375 RD_SWITCH_REG(name, MV88E61XX_GLBREG_DEVADR, MV88E61XX_SGSR,
377 if ((reg & 0xc800) == 0xc800)
384 /* Port based VLANs configuration */
385 mv88e61xx_port_vlan_config(swconfig);
387 if (swconfig->rgmii_delay == MV88E61XX_RGMII_DELAY_EN) {
389 * Enable RGMII delay on Tx and Rx for CPU port
390 * Ref: sec 9.5 of chip datasheet-02
392 /*Force port link down */
393 WR_SWITCH_PORT_REG(name, 5, MV88E61XX_PCS_CTRL_REG, 0x10);
394 /* configure port RGMII delay */
395 WR_SWITCH_PORT_REG(name, 4,
396 MV88E61XX_RGMII_TIMECTRL_REG, 0x81e7);
397 RD_SWITCH_PORT_REG(name, 5,
398 MV88E61XX_RGMII_TIMECTRL_REG, ®);
399 WR_SWITCH_PORT_REG(name, 5,
400 MV88E61XX_RGMII_TIMECTRL_REG, reg | 0x18);
401 WR_SWITCH_PORT_REG(name, 4,
402 MV88E61XX_RGMII_TIMECTRL_REG, 0xc1e7);
403 /* Force port to RGMII FDX 1000Base then up */
404 WR_SWITCH_PORT_REG(name, 5, MV88E61XX_PCS_CTRL_REG, 0x1e);
405 WR_SWITCH_PORT_REG(name, 5, MV88E61XX_PCS_CTRL_REG, 0x3e);
408 for (prt = 0; prt < MV88E61XX_MAX_PORTS_NUM; prt++) {
410 /* configure port's PHY */
411 if (!((1 << prt) & swconfig->cpuport)) {
412 /* port 4 has phy 6, not 4 */
413 int phy = (prt == 4) ? 6 : prt;
414 if (mv88361xx_powerup(swconfig, phy))
416 if (mv88361xx_reverse_mdipn(swconfig, phy))
418 if (mv88361xx_led_init(swconfig, phy))
422 /* set port VID to port+1 except for cpu port */
423 if (!((1 << prt) & swconfig->cpuport)) {
424 RD_SWITCH_PORT_REG(name, prt,
425 MV88E61XX_PRT_VID_REG, ®);
426 WR_SWITCH_PORT_REG(name, prt,
427 MV88E61XX_PRT_VID_REG,
428 (reg & ~1023) | (prt+1));
431 /*Program port state */
432 RD_SWITCH_PORT_REG(name, prt,
433 MV88E61XX_PRT_CTRL_REG, ®);
434 WR_SWITCH_PORT_REG(name, prt,
435 MV88E61XX_PRT_CTRL_REG,
436 reg | (swconfig->portstate & 0x03));
440 printf("%s Initialized on %s\n", idstr, name);
444 #ifdef CONFIG_MV88E61XX_CMD
446 do_switch(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
450 enum { dev, prt, phy } target = dev;
451 u32 addrlo, addrhi, addr;
452 u32 reglo, reghi, reg;
460 if (strcmp(argv[2], "phy") == 0)
462 else if (strcmp(argv[2], "port") == 0)
464 else if (strcmp(argv[2], "dev") != 0)
467 addrlo = simple_strtoul(argv[3], &endp, 16);
472 while (*endp < '0' || *endp > '9')
474 addrhi = simple_strtoul(endp, NULL, 16);
477 reglo = simple_strtoul(argv[5], &endp, 16);
481 while (*endp < '0' || *endp > '9')
483 reghi = simple_strtoul(endp, NULL, 16);
486 if (strcmp(argv[6], "write") == 0)
488 else if (strcmp(argv[6], "read") != 0)
491 data = simple_strtoul(argv[7], NULL, 16);
493 for (addr = addrlo; addr <= addrhi; addr++) {
494 for (reg = reglo; reg <= reghi; reg++) {
497 mv88e61xx_switch_miiphy_write(
498 name, addr, reg, data);
499 else if (target == prt)
501 addr+MV88E61XX_PRT_OFST,
504 wr_switch_reg(name, addr, reg, data);
507 mv88e61xx_switch_miiphy_read(
508 name, addr, reg, &rdata);
509 else if (target == prt)
511 addr+MV88E61XX_PRT_OFST,
514 rd_switch_reg(name, addr, reg, &rdata);
515 printf("%s %s %s %02x %s %02x %s %04x\n",
516 argv[0], argv[1], argv[2], addr,
517 argv[4], reg, argv[6], rdata);
518 if (write && argc == 7 && rdata != data)
526 U_BOOT_CMD(mv88e61xx, 8, 0, do_switch,
527 "Read or write mv88e61xx switch registers",
528 "<ethdevice> dev|port|phy <addr> reg <reg> write <data>\n"
529 "<ethdevice> dev|port|phy <addr> reg <reg> read [<data>]\n"
530 " - read/write switch device, port or phy at (addr,reg)\n"
531 " addr=0..0x1C for dev, 0..5 for port or phy.\n"
533 " data=0..0xFFFF (tested if present against actual read).\n"
534 " All numeric parameters are assumed to be hex.\n"
535 " <addr> and <<reg> arguments can be ranges (x..y)"
537 #endif /* CONFIG_MV88E61XX_CMD */