2 * Copyright 2014 Freescale Semiconductor, Inc.
4 * SPDX-License-Identifier: GPL-2.0+
6 * Driver for the Vitesse VSC9953 L2 Switch
10 #include <asm/fsl_serdes.h>
12 #include <fsl_memac.h>
18 static struct vsc9953_info vsc9953_l2sw = {
19 .port[0] = VSC9953_PORT_INFO_INITIALIZER(0),
20 .port[1] = VSC9953_PORT_INFO_INITIALIZER(1),
21 .port[2] = VSC9953_PORT_INFO_INITIALIZER(2),
22 .port[3] = VSC9953_PORT_INFO_INITIALIZER(3),
23 .port[4] = VSC9953_PORT_INFO_INITIALIZER(4),
24 .port[5] = VSC9953_PORT_INFO_INITIALIZER(5),
25 .port[6] = VSC9953_PORT_INFO_INITIALIZER(6),
26 .port[7] = VSC9953_PORT_INFO_INITIALIZER(7),
27 .port[8] = VSC9953_PORT_INFO_INITIALIZER(8),
28 .port[9] = VSC9953_PORT_INFO_INITIALIZER(9),
31 void vsc9953_port_info_set_mdio(int port_no, struct mii_dev *bus)
33 if (!VSC9953_PORT_CHECK(port_no))
36 vsc9953_l2sw.port[port_no].bus = bus;
39 void vsc9953_port_info_set_phy_address(int port_no, int address)
41 if (!VSC9953_PORT_CHECK(port_no))
44 vsc9953_l2sw.port[port_no].phyaddr = address;
47 void vsc9953_port_info_set_phy_int(int port_no, phy_interface_t phy_int)
49 if (!VSC9953_PORT_CHECK(port_no))
52 vsc9953_l2sw.port[port_no].enet_if = phy_int;
55 void vsc9953_port_enable(int port_no)
57 if (!VSC9953_PORT_CHECK(port_no))
60 vsc9953_l2sw.port[port_no].enabled = 1;
63 void vsc9953_port_disable(int port_no)
65 if (!VSC9953_PORT_CHECK(port_no))
68 vsc9953_l2sw.port[port_no].enabled = 0;
71 static void vsc9953_mdio_write(struct vsc9953_mii_mng *phyregs, int port_addr,
72 int regnum, int value)
76 out_le32(&phyregs->miimcmd, (0x1 << 31) | ((port_addr & 0x1f) << 25) |
77 ((regnum & 0x1f) << 20) | ((value & 0xffff) << 4) |
81 while ((in_le32(&phyregs->miimstatus) & 0x8) && --timeout)
85 debug("Timeout waiting for MDIO write\n");
88 static int vsc9953_mdio_read(struct vsc9953_mii_mng *phyregs, int port_addr,
94 while ((in_le32(&phyregs->miimstatus) & MIIMIND_OPR_PEND) && --timeout)
97 debug("Timeout waiting for MDIO operation to finish\n");
101 /* Put the address of the phy, and the register
104 out_le32(&phyregs->miimcmd, (0x1 << 31) | ((port_addr & 0x1f) << 25) |
105 ((regnum & 0x1f) << 20) | ((value & 0xffff) << 4) |
109 /* Wait for the the indication that the read is done */
110 while ((in_le32(&phyregs->miimstatus) & 0x8) && --timeout)
113 debug("Timeout waiting for MDIO read\n");
115 /* Grab the value read from the PHY */
116 value = in_le32(&phyregs->miimdata);
118 if ((value & 0x00030000) == 0)
119 return value & 0x0000ffff;
124 static int init_phy(struct eth_device *dev)
126 struct vsc9953_port_info *l2sw_port = dev->priv;
127 struct phy_device *phydev = NULL;
132 phydev = phy_connect(l2sw_port->bus, l2sw_port->phyaddr, dev,
135 printf("Failed to connect\n");
139 phydev->supported &= SUPPORTED_10baseT_Half |
140 SUPPORTED_10baseT_Full |
141 SUPPORTED_100baseT_Half |
142 SUPPORTED_100baseT_Full |
143 SUPPORTED_1000baseT_Full;
144 phydev->advertising = phydev->supported;
146 l2sw_port->phydev = phydev;
154 static int vsc9953_port_init(int port_no)
156 struct eth_device *dev;
158 /* Internal ports never have a PHY */
159 if (VSC9953_INTERNAL_PORT_CHECK(port_no))
162 /* alloc eth device */
163 dev = (struct eth_device *)calloc(1, sizeof(struct eth_device));
167 sprintf(dev->name, "SW@PORT%d", port_no);
168 dev->priv = &vsc9953_l2sw.port[port_no];
182 static int vsc9953_vlan_table_poll_idle(void)
184 struct vsc9953_analyzer *l2ana_reg;
187 l2ana_reg = (struct vsc9953_analyzer *)(VSC9953_OFFSET +
191 while (((in_le32(&l2ana_reg->ana_tables.vlan_access) &
192 VSC9953_VLAN_CMD_MASK) != VSC9953_VLAN_CMD_IDLE) && --timeout)
195 return timeout ? 0 : -EBUSY;
198 /* vlan table set/clear all membership of vid */
199 static void vsc9953_vlan_table_membership_all_set(int vid, int set_member)
202 struct vsc9953_analyzer *l2ana_reg;
204 l2ana_reg = (struct vsc9953_analyzer *)(VSC9953_OFFSET +
207 if (vsc9953_vlan_table_poll_idle() < 0) {
208 debug("VLAN table timeout\n");
212 /* read current vlan configuration */
213 val = in_le32(&l2ana_reg->ana_tables.vlan_tidx);
214 out_le32(&l2ana_reg->ana_tables.vlan_tidx,
215 bitfield_replace_by_mask(val, VSC9953_ANA_TBL_VID_MASK, vid));
217 clrsetbits_le32(&l2ana_reg->ana_tables.vlan_access,
218 VSC9953_VLAN_CMD_MASK, VSC9953_VLAN_CMD_READ);
220 if (vsc9953_vlan_table_poll_idle() < 0) {
221 debug("VLAN table timeout\n");
225 val = in_le32(&l2ana_reg->ana_tables.vlan_tidx);
226 out_le32(&l2ana_reg->ana_tables.vlan_tidx,
227 bitfield_replace_by_mask(val, VSC9953_ANA_TBL_VID_MASK, vid));
229 clrsetbits_le32(&l2ana_reg->ana_tables.vlan_access,
230 VSC9953_VLAN_PORT_MASK | VSC9953_VLAN_CMD_MASK,
231 VSC9953_VLAN_CMD_WRITE |
232 (set_member ? VSC9953_VLAN_PORT_MASK : 0));
235 /* Set PVID for a VSC9953 port */
236 static void vsc9953_port_vlan_pvid_set(int port_no, int pvid)
239 struct vsc9953_analyzer *l2ana_reg;
240 struct vsc9953_rew_reg *l2rew_reg;
242 /* Administrative down */
243 if (!vsc9953_l2sw.port[port_no].enabled) {
244 printf("Port %d is administrative down\n", port_no);
248 l2ana_reg = (struct vsc9953_analyzer *)(VSC9953_OFFSET +
250 l2rew_reg = (struct vsc9953_rew_reg *)(VSC9953_OFFSET +
253 /* Set PVID on ingress */
254 val = in_le32(&l2ana_reg->port[port_no].vlan_cfg);
255 val = bitfield_replace_by_mask(val, VSC9953_VLAN_CFG_VID_MASK, pvid);
256 out_le32(&l2ana_reg->port[port_no].vlan_cfg, val);
258 /* Set PVID on egress */
259 val = in_le32(&l2rew_reg->port[port_no].port_vlan_cfg);
260 val = bitfield_replace_by_mask(val, VSC9953_PORT_VLAN_CFG_VID_MASK,
262 out_le32(&l2rew_reg->port[port_no].port_vlan_cfg, val);
265 static void vsc9953_port_all_vlan_pvid_set(int pvid)
269 for (i = 0; i < VSC9953_MAX_PORTS; i++)
270 vsc9953_port_vlan_pvid_set(i, pvid);
273 /* Enable/disable vlan aware of a VSC9953 port */
274 static void vsc9953_port_vlan_aware_set(int port_no, int enabled)
276 struct vsc9953_analyzer *l2ana_reg;
278 /* Administrative down */
279 if (!vsc9953_l2sw.port[port_no].enabled) {
280 printf("Port %d is administrative down\n", port_no);
284 l2ana_reg = (struct vsc9953_analyzer *)(VSC9953_OFFSET +
288 setbits_le32(&l2ana_reg->port[port_no].vlan_cfg,
289 VSC9953_VLAN_CFG_AWARE_ENA);
291 clrbits_le32(&l2ana_reg->port[port_no].vlan_cfg,
292 VSC9953_VLAN_CFG_AWARE_ENA);
295 /* Set all VSC9953 ports' vlan aware */
296 static void vsc9953_port_all_vlan_aware_set(int enabled)
300 for (i = 0; i < VSC9953_MAX_PORTS; i++)
301 vsc9953_port_vlan_aware_set(i, enabled);
304 /* Enable/disable vlan pop count of a VSC9953 port */
305 static void vsc9953_port_vlan_popcnt_set(int port_no, int popcnt)
308 struct vsc9953_analyzer *l2ana_reg;
310 /* Administrative down */
311 if (!vsc9953_l2sw.port[port_no].enabled) {
312 printf("Port %d is administrative down\n", port_no);
316 if (popcnt > 3 || popcnt < 0) {
317 printf("Invalid pop count value: %d\n", port_no);
321 l2ana_reg = (struct vsc9953_analyzer *)(VSC9953_OFFSET +
324 val = in_le32(&l2ana_reg->port[port_no].vlan_cfg);
325 val = bitfield_replace_by_mask(val, VSC9953_VLAN_CFG_POP_CNT_MASK,
327 out_le32(&l2ana_reg->port[port_no].vlan_cfg, val);
330 /* Set all VSC9953 ports' pop count */
331 static void vsc9953_port_all_vlan_poncnt_set(int popcnt)
335 for (i = 0; i < VSC9953_MAX_PORTS; i++)
336 vsc9953_port_vlan_popcnt_set(i, popcnt);
339 /* Enable/disable learning for frames dropped due to ingress filtering */
340 static void vsc9953_vlan_ingr_fltr_learn_drop(int enable)
342 struct vsc9953_analyzer *l2ana_reg;
344 l2ana_reg = (struct vsc9953_analyzer *)(VSC9953_OFFSET +
348 setbits_le32(&l2ana_reg->ana.adv_learn, VSC9953_VLAN_CHK);
350 clrbits_le32(&l2ana_reg->ana.adv_learn, VSC9953_VLAN_CHK);
353 /* Egress untag modes of a VSC9953 port */
354 enum egress_untag_mode {
355 EGRESS_UNTAG_ALL = 0,
356 EGRESS_UNTAG_PVID_AND_ZERO,
361 static void vsc9953_port_vlan_egr_untag_set(int port_no,
362 enum egress_untag_mode mode)
364 struct vsc9953_rew_reg *l2rew_reg;
366 /* Administrative down */
367 if (!vsc9953_l2sw.port[port_no].enabled) {
368 printf("Port %d is administrative down\n", port_no);
372 l2rew_reg = (struct vsc9953_rew_reg *)(VSC9953_OFFSET +
376 case EGRESS_UNTAG_ALL:
377 clrsetbits_le32(&l2rew_reg->port[port_no].port_tag_cfg,
378 VSC9953_TAG_CFG_MASK, VSC9953_TAG_CFG_NONE);
380 case EGRESS_UNTAG_PVID_AND_ZERO:
381 clrsetbits_le32(&l2rew_reg->port[port_no].port_tag_cfg,
382 VSC9953_TAG_CFG_MASK,
383 VSC9953_TAG_CFG_ALL_BUT_PVID_ZERO);
385 case EGRESS_UNTAG_ZERO:
386 clrsetbits_le32(&l2rew_reg->port[port_no].port_tag_cfg,
387 VSC9953_TAG_CFG_MASK,
388 VSC9953_TAG_CFG_ALL_BUT_ZERO);
390 case EGRESS_UNTAG_NONE:
391 clrsetbits_le32(&l2rew_reg->port[port_no].port_tag_cfg,
392 VSC9953_TAG_CFG_MASK, VSC9953_TAG_CFG_ALL);
395 printf("Unknown untag mode for port %d\n", port_no);
399 static void vsc9953_port_all_vlan_egress_untagged_set(
400 enum egress_untag_mode mode)
404 for (i = 0; i < VSC9953_MAX_PORTS; i++)
405 vsc9953_port_vlan_egr_untag_set(i, mode);
408 /*****************************************************************************
409 At startup, the default configuration would be:
410 - HW learning enabled on all ports; (HW default)
411 - All ports are in VLAN 1;
412 - All ports are VLAN aware;
413 - All ports have POP_COUNT 1;
414 - All ports have PVID 1;
415 - All ports have TPID 0x8100; (HW default)
416 - All ports tag frames classified to all VLANs that are not PVID;
417 *****************************************************************************/
418 void vsc9953_default_configuration(void)
422 for (i = 0; i < VSC9953_MAX_VLAN; i++)
423 vsc9953_vlan_table_membership_all_set(i, 0);
424 vsc9953_port_all_vlan_aware_set(1);
425 vsc9953_port_all_vlan_pvid_set(1);
426 vsc9953_port_all_vlan_poncnt_set(1);
427 vsc9953_vlan_table_membership_all_set(1, 1);
428 vsc9953_vlan_ingr_fltr_learn_drop(1);
429 vsc9953_port_all_vlan_egress_untagged_set(EGRESS_UNTAG_PVID_AND_ZERO);
432 void vsc9953_init(bd_t *bis)
438 struct vsc9953_system_reg *l2sys_reg;
439 struct vsc9953_qsys_reg *l2qsys_reg;
440 struct vsc9953_dev_gmii *l2dev_gmii_reg;
441 struct vsc9953_analyzer *l2ana_reg;
442 struct vsc9953_devcpu_gcb *l2dev_gcb;
444 l2dev_gmii_reg = (struct vsc9953_dev_gmii *)(VSC9953_OFFSET +
445 VSC9953_DEV_GMII_OFFSET);
447 l2ana_reg = (struct vsc9953_analyzer *)(VSC9953_OFFSET +
450 l2sys_reg = (struct vsc9953_system_reg *)(VSC9953_OFFSET +
453 l2qsys_reg = (struct vsc9953_qsys_reg *)(VSC9953_OFFSET +
454 VSC9953_QSYS_OFFSET);
456 l2dev_gcb = (struct vsc9953_devcpu_gcb *)(VSC9953_OFFSET +
459 out_le32(&l2dev_gcb->chip_regs.soft_rst,
460 VSC9953_SOFT_SWC_RST_ENA);
462 while ((in_le32(&l2dev_gcb->chip_regs.soft_rst) &
463 VSC9953_SOFT_SWC_RST_ENA) && --timeout)
464 udelay(1); /* busy wait for vsc9953 soft reset */
466 debug("Timeout waiting for VSC9953 to reset\n");
468 out_le32(&l2sys_reg->sys.reset_cfg, VSC9953_MEM_ENABLE |
472 while ((in_le32(&l2sys_reg->sys.reset_cfg) &
473 VSC9953_MEM_INIT) && --timeout)
474 udelay(1); /* busy wait for vsc9953 memory init */
476 debug("Timeout waiting for VSC9953 memory to initialize\n");
478 out_le32(&l2sys_reg->sys.reset_cfg, (in_le32(&l2sys_reg->sys.reset_cfg)
479 | VSC9953_CORE_ENABLE));
481 /* VSC9953 Setting to be done once only */
482 out_le32(&l2qsys_reg->sys.ext_cpu_cfg, 0x00000b00);
484 for (i = 0; i < VSC9953_MAX_PORTS; i++) {
485 if (vsc9953_port_init(i))
486 printf("Failed to initialize l2switch port %d\n", i);
488 /* Enable VSC9953 GMII Ports Port ID 0 - 7 */
489 if (VSC9953_INTERNAL_PORT_CHECK(i)) {
490 out_le32(&l2ana_reg->pfc[i].pfc_cfg,
491 VSC9953_PFC_FC_QSGMII);
492 out_le32(&l2sys_reg->pause_cfg.mac_fc_cfg[i],
493 VSC9953_MAC_FC_CFG_QSGMII);
495 out_le32(&l2ana_reg->pfc[i].pfc_cfg,
497 out_le32(&l2sys_reg->pause_cfg.mac_fc_cfg[i],
500 out_le32(&l2dev_gmii_reg->port_mode.clock_cfg,
502 out_le32(&l2dev_gmii_reg->mac_cfg_status.mac_ena_cfg,
503 VSC9953_MAC_ENA_CFG);
504 out_le32(&l2dev_gmii_reg->mac_cfg_status.mac_mode_cfg,
505 VSC9953_MAC_MODE_CFG);
506 out_le32(&l2dev_gmii_reg->mac_cfg_status.mac_ifg_cfg,
507 VSC9953_MAC_IFG_CFG);
508 /* mac_hdx_cfg varies with port id*/
509 hdx_cfg = VSC9953_MAC_HDX_CFG | (i << 16);
510 out_le32(&l2dev_gmii_reg->mac_cfg_status.mac_hdx_cfg, hdx_cfg);
511 out_le32(&l2sys_reg->sys.front_port_mode[i],
512 VSC9953_FRONT_PORT_MODE);
513 setbits_le32(&l2qsys_reg->sys.switch_port_mode[i],
515 out_le32(&l2dev_gmii_reg->mac_cfg_status.mac_maxlen_cfg,
516 VSC9953_MAC_MAX_LEN);
517 out_le32(&l2sys_reg->pause_cfg.pause_cfg[i],
522 l2dev_gmii_reg = (struct vsc9953_dev_gmii *)(
523 (char *)l2dev_gmii_reg
524 + T1040_SWITCH_GMII_DEV_OFFSET);
526 /* Initialize Lynx PHY Wrappers */
528 if (vsc9953_l2sw.port[i].enet_if ==
529 PHY_INTERFACE_MODE_QSGMII)
530 phy_addr = (i + 0x4) & 0x1F;
531 else if (vsc9953_l2sw.port[i].enet_if ==
532 PHY_INTERFACE_MODE_SGMII)
533 phy_addr = (i + 1) & 0x1F;
536 /* SGMII IF mode + AN enable */
537 vsc9953_mdio_write(&l2dev_gcb->mii_mng[0], phy_addr,
538 0x14, PHY_SGMII_IF_MODE_AN |
539 PHY_SGMII_IF_MODE_SGMII);
540 /* Dev ability according to SGMII specification */
541 vsc9953_mdio_write(&l2dev_gcb->mii_mng[0], phy_addr,
542 0x4, PHY_SGMII_DEV_ABILITY_SGMII);
543 /* Adjust link timer for SGMII
544 * 1.6 ms in units of 8 ns = 2 * 10^5 = 0x30d40
546 vsc9953_mdio_write(&l2dev_gcb->mii_mng[0], phy_addr,
548 vsc9953_mdio_write(&l2dev_gcb->mii_mng[0], phy_addr,
551 vsc9953_mdio_write(&l2dev_gcb->mii_mng[0], phy_addr,
552 0x0, PHY_SGMII_CR_DEF_VAL |
553 PHY_SGMII_CR_RESET_AN);
556 while ((vsc9953_mdio_read(&l2dev_gcb->mii_mng[0],
557 phy_addr, 0x01) & 0x0020) && --timeout)
558 udelay(1); /* wait for AN to complete */
560 debug("Timeout waiting for AN to complete\n");
564 vsc9953_default_configuration();
566 printf("VSC9953 L2 switch initialized\n");
570 #ifdef CONFIG_VSC9953_CMD
571 /* Enable/disable status of a VSC9953 port */
572 static void vsc9953_port_status_set(int port_no, u8 enabled)
574 struct vsc9953_qsys_reg *l2qsys_reg;
576 /* Administrative down */
577 if (!vsc9953_l2sw.port[port_no].enabled)
580 l2qsys_reg = (struct vsc9953_qsys_reg *)(VSC9953_OFFSET +
581 VSC9953_QSYS_OFFSET);
584 setbits_le32(&l2qsys_reg->sys.switch_port_mode[port_no],
587 clrbits_le32(&l2qsys_reg->sys.switch_port_mode[port_no],
591 /* Set all VSC9953 ports' status */
592 static void vsc9953_port_all_status_set(u8 enabled)
596 for (i = 0; i < VSC9953_MAX_PORTS; i++)
597 vsc9953_port_status_set(i, enabled);
600 /* Start autonegotiation for a VSC9953 PHY */
601 static void vsc9953_phy_autoneg(int port_no)
603 if (!vsc9953_l2sw.port[port_no].phydev)
606 if (vsc9953_l2sw.port[port_no].phydev->drv->startup(
607 vsc9953_l2sw.port[port_no].phydev))
608 printf("Failed to start PHY for port %d\n", port_no);
611 /* Start autonegotiation for all VSC9953 PHYs */
612 static void vsc9953_phy_all_autoneg(void)
616 for (i = 0; i < VSC9953_MAX_PORTS; i++)
617 vsc9953_phy_autoneg(i);
620 /* Print a VSC9953 port's configuration */
621 static void vsc9953_port_config_show(int port_no)
628 struct vsc9953_qsys_reg *l2qsys_reg;
630 l2qsys_reg = (struct vsc9953_qsys_reg *)(VSC9953_OFFSET +
631 VSC9953_QSYS_OFFSET);
633 val = in_le32(&l2qsys_reg->sys.switch_port_mode[port_no]);
634 enabled = vsc9953_l2sw.port[port_no].enabled &&
635 (val & VSC9953_PORT_ENA);
637 /* internal ports (8 and 9) are fixed */
638 if (VSC9953_INTERNAL_PORT_CHECK(port_no)) {
641 duplex = DUPLEX_FULL;
643 if (vsc9953_l2sw.port[port_no].phydev) {
644 link = vsc9953_l2sw.port[port_no].phydev->link;
645 speed = vsc9953_l2sw.port[port_no].phydev->speed;
646 duplex = vsc9953_l2sw.port[port_no].phydev->duplex;
654 printf("%8d ", port_no);
655 printf("%8s ", enabled == 1 ? "enabled" : "disabled");
656 printf("%8s ", link == 1 ? "up" : "down");
666 printf("%8d ", 1000);
669 printf("%8d ", 2500);
672 printf("%8d ", 10000);
678 printf("%8s\n", duplex == DUPLEX_FULL ? "full" : "half");
681 /* Print VSC9953 ports' configuration */
682 static void vsc9953_port_all_config_show(void)
686 for (i = 0; i < VSC9953_MAX_PORTS; i++)
687 vsc9953_port_config_show(i);
690 /* function to interpret commands starting with "ethsw " */
691 static int do_ethsw(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
699 if (strcmp(argv[1], "port"))
702 if (!strcmp(argv[3], "show")) {
703 if (!strcmp(argv[2], "all")) {
704 vsc9953_phy_all_autoneg();
705 printf("%8s %8s %8s %8s %8s\n",
706 "Port", "Status", "Link", "Speed",
708 vsc9953_port_all_config_show();
711 port = simple_strtoul(argv[2], NULL, 10);
712 if (!VSC9953_PORT_CHECK(port))
714 vsc9953_phy_autoneg(port);
715 printf("%8s %8s %8s %8s %8s\n",
716 "Port", "Status", "Link", "Speed",
718 vsc9953_port_config_show(port);
721 } else if (!strcmp(argv[3], "enable")) {
723 } else if (!strcmp(argv[3], "disable")) {
729 if (!strcmp(argv[2], "all")) {
730 vsc9953_port_all_status_set(enable);
733 port = simple_strtoul(argv[2], NULL, 10);
734 if (!VSC9953_PORT_CHECK(port))
736 vsc9953_port_status_set(port, enable);
743 U_BOOT_CMD(ethsw, 5, 0, do_ethsw,
744 "vsc9953 l2 switch commands",
745 "port <port_no> enable|disable\n"
746 " - enable/disable an l2 switch port\n"
747 " port_no=0..9; use \"all\" for all ports\n"
748 "ethsw port <port_no> show\n"
749 " - show an l2 switch port's configuration\n"
750 " port_no=0..9; use \"all\" for all ports\n"
752 #endif /* CONFIG_VSC9953_CMD */