1 // SPDX-License-Identifier: GPL-2.0+
3 #include <linux/module.h>
4 #include <linux/if_bridge.h>
5 #include <linux/if_vlan.h>
6 #include <linux/iopoll.h>
8 #include <linux/of_platform.h>
9 #include <linux/of_net.h>
10 #include <linux/packing.h>
11 #include <linux/phy/phy.h>
12 #include <linux/reset.h>
13 #include <net/addrconf.h>
15 #include "lan966x_main.h"
17 #define XTR_EOF_0 0x00000080U
18 #define XTR_EOF_1 0x01000080U
19 #define XTR_EOF_2 0x02000080U
20 #define XTR_EOF_3 0x03000080U
21 #define XTR_PRUNED 0x04000080U
22 #define XTR_ABORT 0x05000080U
23 #define XTR_ESCAPE 0x06000080U
24 #define XTR_NOT_READY 0x07000080U
25 #define XTR_VALID_BYTES(x) (4 - (((x) >> 24) & 3))
27 #define READL_SLEEP_US 10
28 #define READL_TIMEOUT_US 100000000
32 static const struct of_device_id lan966x_match[] = {
33 { .compatible = "microchip,lan966x-switch" },
36 MODULE_DEVICE_TABLE(of, lan966x_match);
38 struct lan966x_main_io_resource {
39 enum lan966x_target id;
44 static const struct lan966x_main_io_resource lan966x_main_iomap[] = {
45 { TARGET_CPU, 0xc0000, 0 }, /* 0xe00c0000 */
46 { TARGET_ORG, 0, 1 }, /* 0xe2000000 */
47 { TARGET_GCB, 0x4000, 1 }, /* 0xe2004000 */
48 { TARGET_QS, 0x8000, 1 }, /* 0xe2008000 */
49 { TARGET_PTP, 0xc000, 1 }, /* 0xe200c000 */
50 { TARGET_CHIP_TOP, 0x10000, 1 }, /* 0xe2010000 */
51 { TARGET_REW, 0x14000, 1 }, /* 0xe2014000 */
52 { TARGET_SYS, 0x28000, 1 }, /* 0xe2028000 */
53 { TARGET_DEV, 0x34000, 1 }, /* 0xe2034000 */
54 { TARGET_DEV + 1, 0x38000, 1 }, /* 0xe2038000 */
55 { TARGET_DEV + 2, 0x3c000, 1 }, /* 0xe203c000 */
56 { TARGET_DEV + 3, 0x40000, 1 }, /* 0xe2040000 */
57 { TARGET_DEV + 4, 0x44000, 1 }, /* 0xe2044000 */
58 { TARGET_DEV + 5, 0x48000, 1 }, /* 0xe2048000 */
59 { TARGET_DEV + 6, 0x4c000, 1 }, /* 0xe204c000 */
60 { TARGET_DEV + 7, 0x50000, 1 }, /* 0xe2050000 */
61 { TARGET_QSYS, 0x100000, 1 }, /* 0xe2100000 */
62 { TARGET_AFI, 0x120000, 1 }, /* 0xe2120000 */
63 { TARGET_ANA, 0x140000, 1 }, /* 0xe2140000 */
66 static int lan966x_create_targets(struct platform_device *pdev,
67 struct lan966x *lan966x)
69 struct resource *iores[IO_RANGES];
70 void __iomem *begin[IO_RANGES];
73 /* Initially map the entire range and after that update each target to
74 * point inside the region at the correct offset. It is possible that
75 * other devices access the same region so don't add any checks about
78 for (idx = 0; idx < IO_RANGES; idx++) {
79 iores[idx] = platform_get_resource(pdev, IORESOURCE_MEM,
82 dev_err(&pdev->dev, "Invalid resource\n");
86 begin[idx] = devm_ioremap(&pdev->dev,
88 resource_size(iores[idx]));
90 dev_err(&pdev->dev, "Unable to get registers: %s\n",
96 for (idx = 0; idx < ARRAY_SIZE(lan966x_main_iomap); idx++) {
97 const struct lan966x_main_io_resource *iomap =
98 &lan966x_main_iomap[idx];
100 lan966x->regs[iomap->id] = begin[iomap->range] + iomap->offset;
106 static int lan966x_port_set_mac_address(struct net_device *dev, void *p)
108 struct lan966x_port *port = netdev_priv(dev);
109 struct lan966x *lan966x = port->lan966x;
110 const struct sockaddr *addr = p;
113 /* Learn the new net device MAC address in the mac table. */
114 ret = lan966x_mac_cpu_learn(lan966x, addr->sa_data, HOST_PVID);
118 /* Then forget the previous one. */
119 ret = lan966x_mac_cpu_forget(lan966x, dev->dev_addr, HOST_PVID);
123 eth_hw_addr_set(dev, addr->sa_data);
127 static int lan966x_port_get_phys_port_name(struct net_device *dev,
128 char *buf, size_t len)
130 struct lan966x_port *port = netdev_priv(dev);
133 ret = snprintf(buf, len, "p%d", port->chip_port);
140 static int lan966x_port_open(struct net_device *dev)
142 struct lan966x_port *port = netdev_priv(dev);
143 struct lan966x *lan966x = port->lan966x;
146 /* Enable receiving frames on the port, and activate auto-learning of
149 lan_rmw(ANA_PORT_CFG_LEARNAUTO_SET(1) |
150 ANA_PORT_CFG_RECV_ENA_SET(1) |
151 ANA_PORT_CFG_PORTID_VAL_SET(port->chip_port),
152 ANA_PORT_CFG_LEARNAUTO |
153 ANA_PORT_CFG_RECV_ENA |
154 ANA_PORT_CFG_PORTID_VAL,
155 lan966x, ANA_PORT_CFG(port->chip_port));
157 err = phylink_fwnode_phy_connect(port->phylink, port->fwnode, 0);
159 netdev_err(dev, "Could not attach to PHY\n");
163 phylink_start(port->phylink);
168 static int lan966x_port_stop(struct net_device *dev)
170 struct lan966x_port *port = netdev_priv(dev);
172 lan966x_port_config_down(port);
173 phylink_stop(port->phylink);
174 phylink_disconnect_phy(port->phylink);
179 static int lan966x_port_inj_status(struct lan966x *lan966x)
181 return lan_rd(lan966x, QS_INJ_STATUS);
184 static int lan966x_port_inj_ready(struct lan966x *lan966x, u8 grp)
188 if (lan_rd(lan966x, QS_INJ_STATUS) & QS_INJ_STATUS_FIFO_RDY_SET(BIT(grp)))
191 return readx_poll_timeout_atomic(lan966x_port_inj_status, lan966x, val,
192 QS_INJ_STATUS_FIFO_RDY_GET(val) & BIT(grp),
193 READL_SLEEP_US, READL_TIMEOUT_US);
196 static int lan966x_port_ifh_xmit(struct sk_buff *skb,
198 struct net_device *dev)
200 struct lan966x_port *port = netdev_priv(dev);
201 struct lan966x *lan966x = port->lan966x;
207 val = lan_rd(lan966x, QS_INJ_STATUS);
208 if (!(QS_INJ_STATUS_FIFO_RDY_GET(val) & BIT(grp)) ||
209 (QS_INJ_STATUS_WMARK_REACHED_GET(val) & BIT(grp)))
212 /* Write start of frame */
213 lan_wr(QS_INJ_CTRL_GAP_SIZE_SET(1) |
214 QS_INJ_CTRL_SOF_SET(1),
215 lan966x, QS_INJ_CTRL(grp));
217 /* Write IFH header */
218 for (i = 0; i < IFH_LEN; ++i) {
219 /* Wait until the fifo is ready */
220 err = lan966x_port_inj_ready(lan966x, grp);
224 lan_wr((__force u32)ifh[i], lan966x, QS_INJ_WR(grp));
228 count = DIV_ROUND_UP(skb->len, 4);
230 for (i = 0; i < count; ++i) {
231 /* Wait until the fifo is ready */
232 err = lan966x_port_inj_ready(lan966x, grp);
236 lan_wr(((u32 *)skb->data)[i], lan966x, QS_INJ_WR(grp));
240 while (i < (LAN966X_BUFFER_MIN_SZ / 4)) {
241 /* Wait until the fifo is ready */
242 err = lan966x_port_inj_ready(lan966x, grp);
246 lan_wr(0, lan966x, QS_INJ_WR(grp));
250 /* Inidcate EOF and valid bytes in the last word */
251 lan_wr(QS_INJ_CTRL_GAP_SIZE_SET(1) |
252 QS_INJ_CTRL_VLD_BYTES_SET(skb->len < LAN966X_BUFFER_MIN_SZ ?
254 QS_INJ_CTRL_EOF_SET(1),
255 lan966x, QS_INJ_CTRL(grp));
258 lan_wr(0, lan966x, QS_INJ_WR(grp));
259 skb_tx_timestamp(skb);
261 dev->stats.tx_packets++;
262 dev->stats.tx_bytes += skb->len;
264 if (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP &&
265 LAN966X_SKB_CB(skb)->rew_op == IFH_REW_OP_TWO_STEP_PTP)
268 dev_consume_skb_any(skb);
272 if (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP &&
273 LAN966X_SKB_CB(skb)->rew_op == IFH_REW_OP_TWO_STEP_PTP)
274 lan966x_ptp_txtstamp_release(port, skb);
276 return NETDEV_TX_BUSY;
279 static void lan966x_ifh_set_bypass(void *ifh, u64 bypass)
281 packing(ifh, &bypass, IFH_POS_BYPASS + IFH_WID_BYPASS - 1,
282 IFH_POS_BYPASS, IFH_LEN * 4, PACK, 0);
285 static void lan966x_ifh_set_port(void *ifh, u64 bypass)
287 packing(ifh, &bypass, IFH_POS_DSTS + IFH_WID_DSTS - 1,
288 IFH_POS_DSTS, IFH_LEN * 4, PACK, 0);
291 static void lan966x_ifh_set_qos_class(void *ifh, u64 bypass)
293 packing(ifh, &bypass, IFH_POS_QOS_CLASS + IFH_WID_QOS_CLASS - 1,
294 IFH_POS_QOS_CLASS, IFH_LEN * 4, PACK, 0);
297 static void lan966x_ifh_set_ipv(void *ifh, u64 bypass)
299 packing(ifh, &bypass, IFH_POS_IPV + IFH_WID_IPV - 1,
300 IFH_POS_IPV, IFH_LEN * 4, PACK, 0);
303 static void lan966x_ifh_set_vid(void *ifh, u64 vid)
305 packing(ifh, &vid, IFH_POS_TCI + IFH_WID_TCI - 1,
306 IFH_POS_TCI, IFH_LEN * 4, PACK, 0);
309 static void lan966x_ifh_set_rew_op(void *ifh, u64 rew_op)
311 packing(ifh, &rew_op, IFH_POS_REW_CMD + IFH_WID_REW_CMD - 1,
312 IFH_POS_REW_CMD, IFH_LEN * 4, PACK, 0);
315 static void lan966x_ifh_set_timestamp(void *ifh, u64 timestamp)
317 packing(ifh, ×tamp, IFH_POS_TIMESTAMP + IFH_WID_TIMESTAMP - 1,
318 IFH_POS_TIMESTAMP, IFH_LEN * 4, PACK, 0);
321 static int lan966x_port_xmit(struct sk_buff *skb, struct net_device *dev)
323 struct lan966x_port *port = netdev_priv(dev);
324 struct lan966x *lan966x = port->lan966x;
328 memset(ifh, 0x0, sizeof(__be32) * IFH_LEN);
330 lan966x_ifh_set_bypass(ifh, 1);
331 lan966x_ifh_set_port(ifh, BIT_ULL(port->chip_port));
332 lan966x_ifh_set_qos_class(ifh, skb->priority >= 7 ? 0x7 : skb->priority);
333 lan966x_ifh_set_ipv(ifh, skb->priority >= 7 ? 0x7 : skb->priority);
334 lan966x_ifh_set_vid(ifh, skb_vlan_tag_get(skb));
336 if (port->lan966x->ptp && skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) {
337 err = lan966x_ptp_txtstamp_request(port, skb);
341 lan966x_ifh_set_rew_op(ifh, LAN966X_SKB_CB(skb)->rew_op);
342 lan966x_ifh_set_timestamp(ifh, LAN966X_SKB_CB(skb)->ts_id);
345 spin_lock(&lan966x->tx_lock);
346 err = lan966x_port_ifh_xmit(skb, ifh, dev);
347 spin_unlock(&lan966x->tx_lock);
352 static int lan966x_port_change_mtu(struct net_device *dev, int new_mtu)
354 struct lan966x_port *port = netdev_priv(dev);
355 struct lan966x *lan966x = port->lan966x;
357 lan_wr(DEV_MAC_MAXLEN_CFG_MAX_LEN_SET(new_mtu),
358 lan966x, DEV_MAC_MAXLEN_CFG(port->chip_port));
364 static int lan966x_mc_unsync(struct net_device *dev, const unsigned char *addr)
366 struct lan966x_port *port = netdev_priv(dev);
367 struct lan966x *lan966x = port->lan966x;
369 return lan966x_mac_forget(lan966x, addr, HOST_PVID, ENTRYTYPE_LOCKED);
372 static int lan966x_mc_sync(struct net_device *dev, const unsigned char *addr)
374 struct lan966x_port *port = netdev_priv(dev);
375 struct lan966x *lan966x = port->lan966x;
377 return lan966x_mac_cpu_learn(lan966x, addr, HOST_PVID);
380 static void lan966x_port_set_rx_mode(struct net_device *dev)
382 __dev_mc_sync(dev, lan966x_mc_sync, lan966x_mc_unsync);
385 static int lan966x_port_get_parent_id(struct net_device *dev,
386 struct netdev_phys_item_id *ppid)
388 struct lan966x_port *port = netdev_priv(dev);
389 struct lan966x *lan966x = port->lan966x;
391 ppid->id_len = sizeof(lan966x->base_mac);
392 memcpy(&ppid->id, &lan966x->base_mac, ppid->id_len);
397 static int lan966x_port_ioctl(struct net_device *dev, struct ifreq *ifr,
400 struct lan966x_port *port = netdev_priv(dev);
402 if (!phy_has_hwtstamp(dev->phydev) && port->lan966x->ptp) {
405 return lan966x_ptp_hwtstamp_set(port, ifr);
407 return lan966x_ptp_hwtstamp_get(port, ifr);
411 return phy_mii_ioctl(dev->phydev, ifr, cmd);
414 static const struct net_device_ops lan966x_port_netdev_ops = {
415 .ndo_open = lan966x_port_open,
416 .ndo_stop = lan966x_port_stop,
417 .ndo_start_xmit = lan966x_port_xmit,
418 .ndo_change_mtu = lan966x_port_change_mtu,
419 .ndo_set_rx_mode = lan966x_port_set_rx_mode,
420 .ndo_get_phys_port_name = lan966x_port_get_phys_port_name,
421 .ndo_get_stats64 = lan966x_stats_get,
422 .ndo_set_mac_address = lan966x_port_set_mac_address,
423 .ndo_get_port_parent_id = lan966x_port_get_parent_id,
424 .ndo_eth_ioctl = lan966x_port_ioctl,
427 bool lan966x_netdevice_check(const struct net_device *dev)
429 return dev->netdev_ops == &lan966x_port_netdev_ops;
432 static bool lan966x_hw_offload(struct lan966x *lan966x, u32 port,
437 /* The IGMP and MLD frames are not forward by the HW if
438 * multicast snooping is enabled, therefor don't mark as
439 * offload to allow the SW to forward the frames accordingly.
441 val = lan_rd(lan966x, ANA_CPU_FWD_CFG(port));
442 if (!(val & (ANA_CPU_FWD_CFG_IGMP_REDIR_ENA |
443 ANA_CPU_FWD_CFG_MLD_REDIR_ENA)))
446 if (skb->protocol == htons(ETH_P_IP) &&
447 ip_hdr(skb)->protocol == IPPROTO_IGMP)
450 if (IS_ENABLED(CONFIG_IPV6) &&
451 skb->protocol == htons(ETH_P_IPV6) &&
452 ipv6_addr_is_multicast(&ipv6_hdr(skb)->daddr) &&
453 !ipv6_mc_check_mld(skb))
459 static int lan966x_port_xtr_status(struct lan966x *lan966x, u8 grp)
461 return lan_rd(lan966x, QS_XTR_RD(grp));
464 static int lan966x_port_xtr_ready(struct lan966x *lan966x, u8 grp)
468 return read_poll_timeout(lan966x_port_xtr_status, val,
469 val != XTR_NOT_READY,
470 READL_SLEEP_US, READL_TIMEOUT_US, false,
474 static int lan966x_rx_frame_word(struct lan966x *lan966x, u8 grp, u32 *rval)
480 val = lan_rd(lan966x, QS_XTR_RD(grp));
481 if (val == XTR_NOT_READY) {
482 err = lan966x_port_xtr_ready(lan966x, grp);
495 bytes_valid = XTR_VALID_BYTES(val);
496 val = lan_rd(lan966x, QS_XTR_RD(grp));
497 if (val == XTR_ESCAPE)
498 *rval = lan_rd(lan966x, QS_XTR_RD(grp));
504 *rval = lan_rd(lan966x, QS_XTR_RD(grp));
514 static void lan966x_ifh_get_src_port(void *ifh, u64 *src_port)
516 packing(ifh, src_port, IFH_POS_SRCPORT + IFH_WID_SRCPORT - 1,
517 IFH_POS_SRCPORT, IFH_LEN * 4, UNPACK, 0);
520 static void lan966x_ifh_get_len(void *ifh, u64 *len)
522 packing(ifh, len, IFH_POS_LEN + IFH_WID_LEN - 1,
523 IFH_POS_LEN, IFH_LEN * 4, UNPACK, 0);
526 static void lan966x_ifh_get_timestamp(void *ifh, u64 *timestamp)
528 packing(ifh, timestamp, IFH_POS_TIMESTAMP + IFH_WID_TIMESTAMP - 1,
529 IFH_POS_TIMESTAMP, IFH_LEN * 4, UNPACK, 0);
532 static irqreturn_t lan966x_xtr_irq_handler(int irq, void *args)
534 struct lan966x *lan966x = args;
535 int i, grp = 0, err = 0;
537 if (!(lan_rd(lan966x, QS_XTR_DATA_PRESENT) & BIT(grp)))
541 u64 src_port, len, timestamp;
542 struct net_device *dev;
549 for (i = 0; i < IFH_LEN; i++) {
550 err = lan966x_rx_frame_word(lan966x, grp, &ifh[i]);
557 lan966x_ifh_get_src_port(ifh, &src_port);
558 lan966x_ifh_get_len(ifh, &len);
559 lan966x_ifh_get_timestamp(ifh, ×tamp);
561 WARN_ON(src_port >= lan966x->num_phys_ports);
563 dev = lan966x->ports[src_port]->dev;
564 skb = netdev_alloc_skb(dev, len);
565 if (unlikely(!skb)) {
566 netdev_err(dev, "Unable to allocate sk_buff\n");
570 buf_len = len - ETH_FCS_LEN;
571 buf = (u32 *)skb_put(skb, buf_len);
575 sz = lan966x_rx_frame_word(lan966x, grp, &val);
583 } while (len < buf_len);
586 sz = lan966x_rx_frame_word(lan966x, grp, &val);
592 /* Update the statistics if part of the FCS was read before */
593 len -= ETH_FCS_LEN - sz;
595 if (unlikely(dev->features & NETIF_F_RXFCS)) {
596 buf = (u32 *)skb_put(skb, ETH_FCS_LEN);
600 lan966x_ptp_rxtstamp(lan966x, skb, timestamp);
601 skb->protocol = eth_type_trans(skb, dev);
603 if (lan966x->bridge_mask & BIT(src_port)) {
604 skb->offload_fwd_mark = 1;
606 skb_reset_network_header(skb);
607 if (!lan966x_hw_offload(lan966x, src_port, skb))
608 skb->offload_fwd_mark = 0;
611 if (!skb_defer_rx_timestamp(skb))
614 dev->stats.rx_bytes += len;
615 dev->stats.rx_packets++;
619 lan_rd(lan966x, QS_XTR_RD(grp));
621 } while (lan_rd(lan966x, QS_XTR_DATA_PRESENT) & BIT(grp));
626 static irqreturn_t lan966x_ana_irq_handler(int irq, void *args)
628 struct lan966x *lan966x = args;
630 return lan966x_mac_irq_handler(lan966x);
633 static void lan966x_cleanup_ports(struct lan966x *lan966x)
635 struct lan966x_port *port;
638 for (p = 0; p < lan966x->num_phys_ports; p++) {
639 port = lan966x->ports[p];
644 unregister_netdev(port->dev);
648 lan966x_port_stop(port->dev);
650 phylink_destroy(port->phylink);
651 port->phylink = NULL;
655 fwnode_handle_put(port->fwnode);
658 disable_irq(lan966x->xtr_irq);
659 lan966x->xtr_irq = -ENXIO;
661 if (lan966x->ana_irq) {
662 disable_irq(lan966x->ana_irq);
663 lan966x->ana_irq = -ENXIO;
667 static int lan966x_probe_port(struct lan966x *lan966x, u32 p,
668 phy_interface_t phy_mode,
669 struct fwnode_handle *portnp)
671 struct lan966x_port *port;
672 struct phylink *phylink;
673 struct net_device *dev;
676 if (p >= lan966x->num_phys_ports)
679 dev = devm_alloc_etherdev_mqs(lan966x->dev,
680 sizeof(struct lan966x_port), 8, 1);
684 SET_NETDEV_DEV(dev, lan966x->dev);
685 port = netdev_priv(dev);
687 port->lan966x = lan966x;
689 lan966x->ports[p] = port;
691 dev->max_mtu = ETH_MAX_MTU;
693 dev->netdev_ops = &lan966x_port_netdev_ops;
694 dev->ethtool_ops = &lan966x_ethtool_ops;
695 dev->features |= NETIF_F_HW_VLAN_CTAG_TX |
696 NETIF_F_HW_VLAN_STAG_TX;
697 dev->needed_headroom = IFH_LEN * sizeof(u32);
699 eth_hw_addr_gen(dev, lan966x->base_mac, p + 1);
701 lan966x_mac_learn(lan966x, PGID_CPU, dev->dev_addr, HOST_PVID,
704 port->phylink_config.dev = &port->dev->dev;
705 port->phylink_config.type = PHYLINK_NETDEV;
706 port->phylink_pcs.poll = true;
707 port->phylink_pcs.ops = &lan966x_phylink_pcs_ops;
709 port->phylink_config.mac_capabilities = MAC_ASYM_PAUSE | MAC_SYM_PAUSE |
710 MAC_10 | MAC_100 | MAC_1000FD | MAC_2500FD;
712 __set_bit(PHY_INTERFACE_MODE_MII,
713 port->phylink_config.supported_interfaces);
714 __set_bit(PHY_INTERFACE_MODE_GMII,
715 port->phylink_config.supported_interfaces);
716 __set_bit(PHY_INTERFACE_MODE_SGMII,
717 port->phylink_config.supported_interfaces);
718 __set_bit(PHY_INTERFACE_MODE_QSGMII,
719 port->phylink_config.supported_interfaces);
720 __set_bit(PHY_INTERFACE_MODE_1000BASEX,
721 port->phylink_config.supported_interfaces);
722 __set_bit(PHY_INTERFACE_MODE_2500BASEX,
723 port->phylink_config.supported_interfaces);
725 phylink = phylink_create(&port->phylink_config,
728 &lan966x_phylink_mac_ops);
729 if (IS_ERR(phylink)) {
731 return PTR_ERR(phylink);
734 port->phylink = phylink;
736 err = register_netdev(dev);
738 dev_err(lan966x->dev, "register_netdev failed\n");
742 lan966x_vlan_port_set_vlan_aware(port, 0);
743 lan966x_vlan_port_set_vid(port, HOST_PVID, false, false);
744 lan966x_vlan_port_apply(port);
749 static void lan966x_init(struct lan966x *lan966x)
753 /* MAC table initialization */
754 lan966x_mac_init(lan966x);
756 lan966x_vlan_init(lan966x);
759 lan_wr(lan_rd(lan966x, QS_XTR_FLUSH) |
761 lan966x, QS_XTR_FLUSH);
766 /* All Queues normal */
767 lan_wr(lan_rd(lan966x, QS_XTR_FLUSH) &
769 lan966x, QS_XTR_FLUSH);
771 /* Set MAC age time to default value, the entry is aged after
774 lan_wr(ANA_AUTOAGE_AGE_PERIOD_SET(BR_DEFAULT_AGEING_TIME / 2 / HZ),
775 lan966x, ANA_AUTOAGE);
777 /* Disable learning for frames discarded by VLAN ingress filtering */
778 lan_rmw(ANA_ADVLEARN_VLAN_CHK_SET(1),
779 ANA_ADVLEARN_VLAN_CHK,
780 lan966x, ANA_ADVLEARN);
782 /* Setup frame ageing - "2 sec" - The unit is 6.5 us on lan966x */
783 lan_wr(SYS_FRM_AGING_AGE_TX_ENA_SET(1) |
785 lan966x, SYS_FRM_AGING);
787 /* Map the 8 CPU extraction queues to CPU port */
788 lan_wr(0, lan966x, QSYS_CPU_GROUP_MAP);
790 /* Do byte-swap and expect status after last data word
791 * Extraction: Mode: manual extraction) | Byte_swap
793 lan_wr(QS_XTR_GRP_CFG_MODE_SET(1) |
794 QS_XTR_GRP_CFG_BYTE_SWAP_SET(1),
795 lan966x, QS_XTR_GRP_CFG(0));
797 /* Injection: Mode: manual injection | Byte_swap */
798 lan_wr(QS_INJ_GRP_CFG_MODE_SET(1) |
799 QS_INJ_GRP_CFG_BYTE_SWAP_SET(1),
800 lan966x, QS_INJ_GRP_CFG(0));
802 lan_rmw(QS_INJ_CTRL_GAP_SIZE_SET(0),
803 QS_INJ_CTRL_GAP_SIZE,
804 lan966x, QS_INJ_CTRL(0));
806 /* Enable IFH insertion/parsing on CPU ports */
807 lan_wr(SYS_PORT_MODE_INCL_INJ_HDR_SET(1) |
808 SYS_PORT_MODE_INCL_XTR_HDR_SET(1),
809 lan966x, SYS_PORT_MODE(CPU_PORT));
811 /* Setup flooding PGIDs */
812 lan_wr(ANA_FLOODING_IPMC_FLD_MC4_DATA_SET(PGID_MCIPV4) |
813 ANA_FLOODING_IPMC_FLD_MC4_CTRL_SET(PGID_MC) |
814 ANA_FLOODING_IPMC_FLD_MC6_DATA_SET(PGID_MCIPV6) |
815 ANA_FLOODING_IPMC_FLD_MC6_CTRL_SET(PGID_MC),
816 lan966x, ANA_FLOODING_IPMC);
818 /* There are 8 priorities */
819 for (i = 0; i < 8; ++i)
820 lan_rmw(ANA_FLOODING_FLD_MULTICAST_SET(PGID_MC) |
821 ANA_FLOODING_FLD_UNICAST_SET(PGID_UC) |
822 ANA_FLOODING_FLD_BROADCAST_SET(PGID_BC),
823 ANA_FLOODING_FLD_MULTICAST |
824 ANA_FLOODING_FLD_UNICAST |
825 ANA_FLOODING_FLD_BROADCAST,
826 lan966x, ANA_FLOODING(i));
828 for (i = 0; i < PGID_ENTRIES; ++i)
829 /* Set all the entries to obey VLAN_VLAN */
830 lan_rmw(ANA_PGID_CFG_OBEY_VLAN_SET(1),
831 ANA_PGID_CFG_OBEY_VLAN,
832 lan966x, ANA_PGID_CFG(i));
834 for (p = 0; p < lan966x->num_phys_ports; p++) {
835 /* Disable bridging by default */
836 lan_rmw(ANA_PGID_PGID_SET(0x0),
838 lan966x, ANA_PGID(p + PGID_SRC));
840 /* Do not forward BPDU frames to the front ports and copy them
843 lan_wr(0xffff, lan966x, ANA_CPU_FWD_BPDU_CFG(p));
846 /* Set source buffer size for each priority and each port to 1500 bytes */
847 for (i = 0; i <= QSYS_Q_RSRV; ++i) {
848 lan_wr(1500 / 64, lan966x, QSYS_RES_CFG(i));
849 lan_wr(1500 / 64, lan966x, QSYS_RES_CFG(512 + i));
852 /* Enable switching to/from cpu port */
853 lan_wr(QSYS_SW_PORT_MODE_PORT_ENA_SET(1) |
854 QSYS_SW_PORT_MODE_SCH_NEXT_CFG_SET(1) |
855 QSYS_SW_PORT_MODE_INGRESS_DROP_MODE_SET(1),
856 lan966x, QSYS_SW_PORT_MODE(CPU_PORT));
858 /* Configure and enable the CPU port */
859 lan_rmw(ANA_PGID_PGID_SET(0),
861 lan966x, ANA_PGID(CPU_PORT));
862 lan_rmw(ANA_PGID_PGID_SET(BIT(CPU_PORT)),
864 lan966x, ANA_PGID(PGID_CPU));
866 /* Multicast to all other ports */
867 lan_rmw(GENMASK(lan966x->num_phys_ports - 1, 0),
869 lan966x, ANA_PGID(PGID_MC));
871 /* This will be controlled by mrouter ports */
872 lan_rmw(GENMASK(lan966x->num_phys_ports - 1, 0),
874 lan966x, ANA_PGID(PGID_MCIPV4));
876 lan_rmw(GENMASK(lan966x->num_phys_ports - 1, 0),
878 lan966x, ANA_PGID(PGID_MCIPV6));
880 /* Unicast to all other ports */
881 lan_rmw(GENMASK(lan966x->num_phys_ports - 1, 0),
883 lan966x, ANA_PGID(PGID_UC));
885 /* Broadcast to the CPU port and to other ports */
886 lan_rmw(ANA_PGID_PGID_SET(BIT(CPU_PORT) | GENMASK(lan966x->num_phys_ports - 1, 0)),
888 lan966x, ANA_PGID(PGID_BC));
890 lan_wr(REW_PORT_CFG_NO_REWRITE_SET(1),
891 lan966x, REW_PORT_CFG(CPU_PORT));
893 lan_rmw(ANA_ANAINTR_INTR_ENA_SET(1),
894 ANA_ANAINTR_INTR_ENA,
895 lan966x, ANA_ANAINTR);
897 spin_lock_init(&lan966x->tx_lock);
900 static int lan966x_ram_init(struct lan966x *lan966x)
902 return lan_rd(lan966x, SYS_RAM_INIT);
905 static int lan966x_reset_switch(struct lan966x *lan966x)
907 struct reset_control *switch_reset, *phy_reset;
911 switch_reset = devm_reset_control_get_shared(lan966x->dev, "switch");
912 if (IS_ERR(switch_reset))
913 return dev_err_probe(lan966x->dev, PTR_ERR(switch_reset),
914 "Could not obtain switch reset");
916 phy_reset = devm_reset_control_get_shared(lan966x->dev, "phy");
917 if (IS_ERR(phy_reset))
918 return dev_err_probe(lan966x->dev, PTR_ERR(phy_reset),
919 "Could not obtain phy reset\n");
921 reset_control_reset(switch_reset);
922 reset_control_reset(phy_reset);
924 lan_wr(SYS_RESET_CFG_CORE_ENA_SET(0), lan966x, SYS_RESET_CFG);
925 lan_wr(SYS_RAM_INIT_RAM_INIT_SET(1), lan966x, SYS_RAM_INIT);
926 ret = readx_poll_timeout(lan966x_ram_init, lan966x,
927 val, (val & BIT(1)) == 0, READL_SLEEP_US,
932 lan_wr(SYS_RESET_CFG_CORE_ENA_SET(1), lan966x, SYS_RESET_CFG);
937 static int lan966x_probe(struct platform_device *pdev)
939 struct fwnode_handle *ports, *portnp;
940 struct lan966x *lan966x;
941 u8 mac_addr[ETH_ALEN];
944 lan966x = devm_kzalloc(&pdev->dev, sizeof(*lan966x), GFP_KERNEL);
948 platform_set_drvdata(pdev, lan966x);
949 lan966x->dev = &pdev->dev;
951 if (!device_get_mac_address(&pdev->dev, mac_addr)) {
952 ether_addr_copy(lan966x->base_mac, mac_addr);
954 pr_info("MAC addr was not set, use random MAC\n");
955 eth_random_addr(lan966x->base_mac);
956 lan966x->base_mac[5] &= 0xf0;
959 ports = device_get_named_child_node(&pdev->dev, "ethernet-ports");
961 return dev_err_probe(&pdev->dev, -ENODEV,
962 "no ethernet-ports child found\n");
964 err = lan966x_create_targets(pdev, lan966x);
966 return dev_err_probe(&pdev->dev, err,
967 "Failed to create targets");
969 err = lan966x_reset_switch(lan966x);
971 return dev_err_probe(&pdev->dev, err, "Reset failed");
974 fwnode_for_each_available_child_node(ports, portnp)
977 lan966x->num_phys_ports = i;
978 lan966x->ports = devm_kcalloc(&pdev->dev, lan966x->num_phys_ports,
979 sizeof(struct lan966x_port *),
984 /* There QS system has 32KB of memory */
985 lan966x->shared_queue_sz = LAN966X_BUFFER_MEMORY;
988 lan966x->xtr_irq = platform_get_irq_byname(pdev, "xtr");
989 if (lan966x->xtr_irq <= 0)
992 err = devm_request_threaded_irq(&pdev->dev, lan966x->xtr_irq, NULL,
993 lan966x_xtr_irq_handler, IRQF_ONESHOT,
994 "frame extraction", lan966x);
996 pr_err("Unable to use xtr irq");
1000 lan966x->ana_irq = platform_get_irq_byname(pdev, "ana");
1001 if (lan966x->ana_irq) {
1002 err = devm_request_threaded_irq(&pdev->dev, lan966x->ana_irq, NULL,
1003 lan966x_ana_irq_handler, IRQF_ONESHOT,
1004 "ana irq", lan966x);
1006 return dev_err_probe(&pdev->dev, err, "Unable to use ana irq");
1009 lan966x->ptp_irq = platform_get_irq_byname(pdev, "ptp");
1010 if (lan966x->ptp_irq > 0) {
1011 err = devm_request_threaded_irq(&pdev->dev, lan966x->ptp_irq, NULL,
1012 lan966x_ptp_irq_handler, IRQF_ONESHOT,
1013 "ptp irq", lan966x);
1015 return dev_err_probe(&pdev->dev, err, "Unable to use ptp irq");
1021 lan966x_init(lan966x);
1022 lan966x_stats_init(lan966x);
1024 /* go over the child nodes */
1025 fwnode_for_each_available_child_node(ports, portnp) {
1026 phy_interface_t phy_mode;
1030 if (fwnode_property_read_u32(portnp, "reg", &p))
1033 phy_mode = fwnode_get_phy_mode(portnp);
1034 err = lan966x_probe_port(lan966x, p, phy_mode, portnp);
1038 /* Read needed configuration */
1039 lan966x->ports[p]->config.portmode = phy_mode;
1040 lan966x->ports[p]->fwnode = fwnode_handle_get(portnp);
1042 serdes = devm_of_phy_get(lan966x->dev, to_of_node(portnp), NULL);
1043 if (!IS_ERR(serdes))
1044 lan966x->ports[p]->serdes = serdes;
1046 lan966x_port_init(lan966x->ports[p]);
1049 lan966x_mdb_init(lan966x);
1050 err = lan966x_fdb_init(lan966x);
1054 err = lan966x_ptp_init(lan966x);
1061 lan966x_fdb_deinit(lan966x);
1064 fwnode_handle_put(portnp);
1066 lan966x_cleanup_ports(lan966x);
1068 cancel_delayed_work_sync(&lan966x->stats_work);
1069 destroy_workqueue(lan966x->stats_queue);
1070 mutex_destroy(&lan966x->stats_lock);
1075 static int lan966x_remove(struct platform_device *pdev)
1077 struct lan966x *lan966x = platform_get_drvdata(pdev);
1079 lan966x_cleanup_ports(lan966x);
1081 cancel_delayed_work_sync(&lan966x->stats_work);
1082 destroy_workqueue(lan966x->stats_queue);
1083 mutex_destroy(&lan966x->stats_lock);
1085 lan966x_mac_purge_entries(lan966x);
1086 lan966x_mdb_deinit(lan966x);
1087 lan966x_fdb_deinit(lan966x);
1088 lan966x_ptp_deinit(lan966x);
1093 static struct platform_driver lan966x_driver = {
1094 .probe = lan966x_probe,
1095 .remove = lan966x_remove,
1097 .name = "lan966x-switch",
1098 .of_match_table = lan966x_match,
1102 static int __init lan966x_switch_driver_init(void)
1106 lan966x_register_notifier_blocks();
1108 ret = platform_driver_register(&lan966x_driver);
1115 lan966x_unregister_notifier_blocks();
1119 static void __exit lan966x_switch_driver_exit(void)
1121 platform_driver_unregister(&lan966x_driver);
1122 lan966x_unregister_notifier_blocks();
1125 module_init(lan966x_switch_driver_init);
1126 module_exit(lan966x_switch_driver_exit);
1128 MODULE_DESCRIPTION("Microchip LAN966X switch driver");
1129 MODULE_AUTHOR("Horatiu Vultur <horatiu.vultur@microchip.com>");
1130 MODULE_LICENSE("Dual MIT/GPL");