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/netdevice.h>
10 #include <linux/of_mdio.h>
11 #include <linux/of_platform.h>
12 #include <linux/skbuff.h>
16 static int ocelot_parse_ifh(u32 *ifh, struct frame_info *info)
21 /* The IFH is in network order, switch to CPU order */
22 for (i = 0; i < IFH_LEN; i++)
23 ifh[i] = ntohl((__force __be32)ifh[i]);
25 wlen = (ifh[1] >> 7) & 0xff;
26 llen = (ifh[1] >> 15) & 0x3f;
27 info->len = OCELOT_BUFFER_CELL_SZ * wlen + llen - 80;
29 info->port = (ifh[2] & GENMASK(14, 11)) >> 11;
31 info->cpuq = (ifh[3] & GENMASK(27, 20)) >> 20;
32 info->tag_type = (ifh[3] & BIT(16)) >> 16;
33 info->vid = ifh[3] & GENMASK(11, 0);
38 static int ocelot_rx_frame_word(struct ocelot *ocelot, u8 grp, bool ifh,
44 val = ocelot_read_rix(ocelot, QS_XTR_RD, grp);
45 if (val == XTR_NOT_READY) {
50 val = ocelot_read_rix(ocelot, QS_XTR_RD, grp);
51 } while (val == XTR_NOT_READY);
62 bytes_valid = XTR_VALID_BYTES(val);
63 val = ocelot_read_rix(ocelot, QS_XTR_RD, grp);
64 if (val == XTR_ESCAPE)
65 *rval = ocelot_read_rix(ocelot, QS_XTR_RD, grp);
71 *rval = ocelot_read_rix(ocelot, QS_XTR_RD, grp);
81 static irqreturn_t ocelot_xtr_irq_handler(int irq, void *arg)
83 struct ocelot *ocelot = arg;
87 if (!(ocelot_read(ocelot, QS_XTR_DATA_PRESENT) & BIT(grp)))
92 struct net_device *dev;
97 struct frame_info info;
99 for (i = 0; i < IFH_LEN; i++) {
100 err = ocelot_rx_frame_word(ocelot, grp, true, &ifh[i]);
108 /* At this point the IFH was read correctly, so it is safe to
109 * presume that there is no error. The err needs to be reset
110 * otherwise a frame could come in CPU queue between the while
111 * condition and the check for error later on. And in that case
112 * the new frame is just removed and not processed.
116 ocelot_parse_ifh(ifh, &info);
118 dev = ocelot->ports[info.port]->dev;
120 skb = netdev_alloc_skb(dev, info.len);
122 if (unlikely(!skb)) {
123 netdev_err(dev, "Unable to allocate sk_buff\n");
127 buf_len = info.len - ETH_FCS_LEN;
128 buf = (u32 *)skb_put(skb, buf_len);
132 sz = ocelot_rx_frame_word(ocelot, grp, false, &val);
135 } while (len < buf_len);
137 /* Read the FCS and discard it */
138 sz = ocelot_rx_frame_word(ocelot, grp, false, &val);
139 /* Update the statistics if part of the FCS was read before */
140 len -= ETH_FCS_LEN - sz;
147 /* Everything we see on an interface that is in the HW bridge
148 * has already been forwarded.
150 if (ocelot->bridge_mask & BIT(info.port))
151 skb->offload_fwd_mark = 1;
153 skb->protocol = eth_type_trans(skb, dev);
155 dev->stats.rx_bytes += len;
156 dev->stats.rx_packets++;
157 } while (ocelot_read(ocelot, QS_XTR_DATA_PRESENT) & BIT(grp));
160 while (ocelot_read(ocelot, QS_XTR_DATA_PRESENT) & BIT(grp))
161 ocelot_read_rix(ocelot, QS_XTR_RD, grp);
166 static const struct of_device_id mscc_ocelot_match[] = {
167 { .compatible = "mscc,vsc7514-switch" },
170 MODULE_DEVICE_TABLE(of, mscc_ocelot_match);
172 static int mscc_ocelot_probe(struct platform_device *pdev)
176 struct device_node *np = pdev->dev.of_node;
177 struct device_node *ports, *portnp;
178 struct ocelot *ocelot;
182 enum ocelot_target id;
193 if (!np && !pdev->dev.platform_data)
196 ocelot = devm_kzalloc(&pdev->dev, sizeof(*ocelot), GFP_KERNEL);
200 platform_set_drvdata(pdev, ocelot);
201 ocelot->dev = &pdev->dev;
203 for (i = 0; i < ARRAY_SIZE(res); i++) {
204 struct regmap *target;
206 target = ocelot_io_platform_init(ocelot, pdev, res[i].name);
208 return PTR_ERR(target);
210 ocelot->targets[res[i].id] = target;
213 err = ocelot_chip_init(ocelot);
217 irq = platform_get_irq_byname(pdev, "xtr");
221 err = devm_request_threaded_irq(&pdev->dev, irq, NULL,
222 ocelot_xtr_irq_handler, IRQF_ONESHOT,
223 "frame extraction", ocelot);
227 regmap_field_write(ocelot->regfields[SYS_RESET_CFG_MEM_INIT], 1);
228 regmap_field_write(ocelot->regfields[SYS_RESET_CFG_MEM_ENA], 1);
232 regmap_field_read(ocelot->regfields[SYS_RESET_CFG_MEM_INIT],
236 regmap_field_write(ocelot->regfields[SYS_RESET_CFG_MEM_ENA], 1);
237 regmap_field_write(ocelot->regfields[SYS_RESET_CFG_CORE_ENA], 1);
239 ocelot->num_cpu_ports = 1; /* 1 port on the switch, two groups */
241 ports = of_get_child_by_name(np, "ethernet-ports");
243 dev_err(&pdev->dev, "no ethernet-ports child node found\n");
247 ocelot->num_phys_ports = of_get_child_count(ports);
249 ocelot->ports = devm_kcalloc(&pdev->dev, ocelot->num_phys_ports,
250 sizeof(struct ocelot_port *), GFP_KERNEL);
252 INIT_LIST_HEAD(&ocelot->multicast);
255 ocelot_rmw(ocelot, HSIO_HW_CFG_DEV1G_4_MODE |
256 HSIO_HW_CFG_DEV1G_6_MODE |
257 HSIO_HW_CFG_DEV1G_9_MODE,
258 HSIO_HW_CFG_DEV1G_4_MODE |
259 HSIO_HW_CFG_DEV1G_6_MODE |
260 HSIO_HW_CFG_DEV1G_9_MODE,
263 for_each_available_child_of_node(ports, portnp) {
264 struct device_node *phy_node;
265 struct phy_device *phy;
266 struct resource *res;
271 if (of_property_read_u32(portnp, "reg", &port))
274 snprintf(res_name, sizeof(res_name), "port%d", port);
276 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
278 regs = devm_ioremap_resource(&pdev->dev, res);
282 phy_node = of_parse_phandle(portnp, "phy-handle", 0);
286 phy = of_phy_find_device(phy_node);
290 err = ocelot_probe_port(ocelot, port, regs, phy);
292 dev_err(&pdev->dev, "failed to probe ports\n");
293 goto err_probe_ports;
297 register_netdevice_notifier(&ocelot_netdevice_nb);
299 dev_info(&pdev->dev, "Ocelot switch probed\n");
307 static int mscc_ocelot_remove(struct platform_device *pdev)
309 struct ocelot *ocelot = platform_get_drvdata(pdev);
311 ocelot_deinit(ocelot);
312 unregister_netdevice_notifier(&ocelot_netdevice_nb);
317 static struct platform_driver mscc_ocelot_driver = {
318 .probe = mscc_ocelot_probe,
319 .remove = mscc_ocelot_remove,
321 .name = "ocelot-switch",
322 .of_match_table = mscc_ocelot_match,
326 module_platform_driver(mscc_ocelot_driver);
328 MODULE_DESCRIPTION("Microsemi Ocelot switch driver");
329 MODULE_AUTHOR("Alexandre Belloni <alexandre.belloni@bootlin.com>");
330 MODULE_LICENSE("Dual MIT/GPL");