1 // SPDX-License-Identifier: (GPL-2.0 OR MIT)
3 * Microsemi Ocelot Switch driver
5 * Copyright (c) 2017 Microsemi Corporation
7 #include <linux/interrupt.h>
8 #include <linux/module.h>
9 #include <linux/of_net.h>
10 #include <linux/netdevice.h>
11 #include <linux/of_mdio.h>
12 #include <linux/of_platform.h>
13 #include <linux/mfd/syscon.h>
14 #include <linux/skbuff.h>
15 #include <net/switchdev.h>
19 #define IFH_EXTRACT_BITFIELD64(x, o, w) (((x) >> (o)) & GENMASK_ULL((w) - 1, 0))
21 static int ocelot_parse_ifh(u32 *_ifh, struct frame_info *info)
26 ifh[0] = be64_to_cpu(((__force __be64 *)_ifh)[0]);
27 ifh[1] = be64_to_cpu(((__force __be64 *)_ifh)[1]);
29 wlen = IFH_EXTRACT_BITFIELD64(ifh[0], 7, 8);
30 llen = IFH_EXTRACT_BITFIELD64(ifh[0], 15, 6);
32 info->len = OCELOT_BUFFER_CELL_SZ * wlen + llen - 80;
34 info->port = IFH_EXTRACT_BITFIELD64(ifh[1], 43, 4);
36 info->tag_type = IFH_EXTRACT_BITFIELD64(ifh[1], 16, 1);
37 info->vid = IFH_EXTRACT_BITFIELD64(ifh[1], 0, 12);
42 static int ocelot_rx_frame_word(struct ocelot *ocelot, u8 grp, bool ifh,
48 val = ocelot_read_rix(ocelot, QS_XTR_RD, grp);
49 if (val == XTR_NOT_READY) {
54 val = ocelot_read_rix(ocelot, QS_XTR_RD, grp);
55 } while (val == XTR_NOT_READY);
66 bytes_valid = XTR_VALID_BYTES(val);
67 val = ocelot_read_rix(ocelot, QS_XTR_RD, grp);
68 if (val == XTR_ESCAPE)
69 *rval = ocelot_read_rix(ocelot, QS_XTR_RD, grp);
75 *rval = ocelot_read_rix(ocelot, QS_XTR_RD, grp);
85 static irqreturn_t ocelot_xtr_irq_handler(int irq, void *arg)
87 struct ocelot *ocelot = arg;
91 if (!(ocelot_read(ocelot, QS_XTR_DATA_PRESENT) & BIT(grp)))
96 struct net_device *dev;
101 struct frame_info info;
103 for (i = 0; i < IFH_LEN; i++) {
104 err = ocelot_rx_frame_word(ocelot, grp, true, &ifh[i]);
112 ocelot_parse_ifh(ifh, &info);
114 dev = ocelot->ports[info.port]->dev;
116 skb = netdev_alloc_skb(dev, info.len);
118 if (unlikely(!skb)) {
119 netdev_err(dev, "Unable to allocate sk_buff\n");
123 buf_len = info.len - ETH_FCS_LEN;
124 buf = (u32 *)skb_put(skb, buf_len);
128 sz = ocelot_rx_frame_word(ocelot, grp, false, &val);
131 } while (len < buf_len);
134 sz = ocelot_rx_frame_word(ocelot, grp, false, &val);
135 /* Update the statistics if part of the FCS was read before */
136 len -= ETH_FCS_LEN - sz;
138 if (unlikely(dev->features & NETIF_F_RXFCS)) {
139 buf = (u32 *)skb_put(skb, ETH_FCS_LEN);
148 /* Everything we see on an interface that is in the HW bridge
149 * has already been forwarded.
151 if (ocelot->bridge_mask & BIT(info.port))
152 skb->offload_fwd_mark = 1;
154 skb->protocol = eth_type_trans(skb, dev);
156 dev->stats.rx_bytes += len;
157 dev->stats.rx_packets++;
158 } while (ocelot_read(ocelot, QS_XTR_DATA_PRESENT) & BIT(grp));
161 while (ocelot_read(ocelot, QS_XTR_DATA_PRESENT) & BIT(grp))
162 ocelot_read_rix(ocelot, QS_XTR_RD, grp);
167 static const struct of_device_id mscc_ocelot_match[] = {
168 { .compatible = "mscc,vsc7514-switch" },
171 MODULE_DEVICE_TABLE(of, mscc_ocelot_match);
173 static int mscc_ocelot_probe(struct platform_device *pdev)
177 struct device_node *np = pdev->dev.of_node;
178 struct device_node *ports, *portnp;
179 struct ocelot *ocelot;
184 enum ocelot_target id;
197 if (!np && !pdev->dev.platform_data)
200 ocelot = devm_kzalloc(&pdev->dev, sizeof(*ocelot), GFP_KERNEL);
204 platform_set_drvdata(pdev, ocelot);
205 ocelot->dev = &pdev->dev;
207 for (i = 0; i < ARRAY_SIZE(res); i++) {
208 struct regmap *target;
210 target = ocelot_io_platform_init(ocelot, pdev, res[i].name);
211 if (IS_ERR(target)) {
212 if (res[i].optional) {
213 ocelot->targets[res[i].id] = NULL;
217 return PTR_ERR(target);
220 ocelot->targets[res[i].id] = target;
223 hsio = syscon_regmap_lookup_by_compatible("mscc,ocelot-hsio");
225 dev_err(&pdev->dev, "missing hsio syscon\n");
226 return PTR_ERR(hsio);
229 ocelot->targets[HSIO] = hsio;
231 err = ocelot_chip_init(ocelot);
235 irq = platform_get_irq_byname(pdev, "xtr");
239 err = devm_request_threaded_irq(&pdev->dev, irq, NULL,
240 ocelot_xtr_irq_handler, IRQF_ONESHOT,
241 "frame extraction", ocelot);
245 regmap_field_write(ocelot->regfields[SYS_RESET_CFG_MEM_INIT], 1);
246 regmap_field_write(ocelot->regfields[SYS_RESET_CFG_MEM_ENA], 1);
250 regmap_field_read(ocelot->regfields[SYS_RESET_CFG_MEM_INIT],
254 regmap_field_write(ocelot->regfields[SYS_RESET_CFG_MEM_ENA], 1);
255 regmap_field_write(ocelot->regfields[SYS_RESET_CFG_CORE_ENA], 1);
257 ocelot->num_cpu_ports = 1; /* 1 port on the switch, two groups */
259 ports = of_get_child_by_name(np, "ethernet-ports");
261 dev_err(&pdev->dev, "no ethernet-ports child node found\n");
265 ocelot->num_phys_ports = of_get_child_count(ports);
267 ocelot->ports = devm_kcalloc(&pdev->dev, ocelot->num_phys_ports,
268 sizeof(struct ocelot_port *), GFP_KERNEL);
270 INIT_LIST_HEAD(&ocelot->multicast);
273 for_each_available_child_of_node(ports, portnp) {
274 struct device_node *phy_node;
275 struct phy_device *phy;
276 struct resource *res;
283 if (of_property_read_u32(portnp, "reg", &port))
286 snprintf(res_name, sizeof(res_name), "port%d", port);
288 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
290 regs = devm_ioremap_resource(&pdev->dev, res);
294 phy_node = of_parse_phandle(portnp, "phy-handle", 0);
298 phy = of_phy_find_device(phy_node);
302 err = ocelot_probe_port(ocelot, port, regs, phy);
308 phy_mode = of_get_phy_mode(portnp);
310 ocelot->ports[port]->phy_mode = PHY_INTERFACE_MODE_NA;
312 ocelot->ports[port]->phy_mode = phy_mode;
314 switch (ocelot->ports[port]->phy_mode) {
315 case PHY_INTERFACE_MODE_NA:
317 case PHY_INTERFACE_MODE_SGMII:
319 case PHY_INTERFACE_MODE_QSGMII:
320 /* Ensure clock signals and speed is set on all
323 ocelot_port_writel(ocelot->ports[port],
324 DEV_CLOCK_CFG_LINK_SPEED
330 "invalid phy mode for port%d, (Q)SGMII only\n",
336 serdes = devm_of_phy_get(ocelot->dev, portnp, NULL);
337 if (IS_ERR(serdes)) {
338 err = PTR_ERR(serdes);
339 if (err == -EPROBE_DEFER)
340 dev_dbg(ocelot->dev, "deferring probe\n");
343 "missing SerDes phys for port%d\n",
346 goto err_probe_ports;
349 ocelot->ports[port]->serdes = serdes;
352 register_netdevice_notifier(&ocelot_netdevice_nb);
353 register_switchdev_notifier(&ocelot_switchdev_nb);
354 register_switchdev_blocking_notifier(&ocelot_switchdev_blocking_nb);
356 dev_info(&pdev->dev, "Ocelot switch probed\n");
364 static int mscc_ocelot_remove(struct platform_device *pdev)
366 struct ocelot *ocelot = platform_get_drvdata(pdev);
368 ocelot_deinit(ocelot);
369 unregister_switchdev_blocking_notifier(&ocelot_switchdev_blocking_nb);
370 unregister_switchdev_notifier(&ocelot_switchdev_nb);
371 unregister_netdevice_notifier(&ocelot_netdevice_nb);
376 static struct platform_driver mscc_ocelot_driver = {
377 .probe = mscc_ocelot_probe,
378 .remove = mscc_ocelot_remove,
380 .name = "ocelot-switch",
381 .of_match_table = mscc_ocelot_match,
385 module_platform_driver(mscc_ocelot_driver);
387 MODULE_DESCRIPTION("Microsemi Ocelot switch driver");
388 MODULE_AUTHOR("Alexandre Belloni <alexandre.belloni@bootlin.com>");
389 MODULE_LICENSE("Dual MIT/GPL");