1589aa6e7SVladimir Oltean // SPDX-License-Identifier: (GPL-2.0 OR MIT)
2589aa6e7SVladimir Oltean /*
3589aa6e7SVladimir Oltean  * Microsemi Ocelot Switch driver
4589aa6e7SVladimir Oltean  *
5589aa6e7SVladimir Oltean  * Copyright (c) 2017 Microsemi Corporation
6589aa6e7SVladimir Oltean  */
740d3f295SVladimir Oltean #include <linux/dsa/ocelot.h>
8589aa6e7SVladimir Oltean #include <linux/interrupt.h>
9589aa6e7SVladimir Oltean #include <linux/module.h>
10589aa6e7SVladimir Oltean #include <linux/of_net.h>
11589aa6e7SVladimir Oltean #include <linux/netdevice.h>
12e6e12df6SVladimir Oltean #include <linux/phylink.h>
13*3d40aed8SRob Herring #include <linux/of.h>
14589aa6e7SVladimir Oltean #include <linux/of_mdio.h>
15*3d40aed8SRob Herring #include <linux/platform_device.h>
16589aa6e7SVladimir Oltean #include <linux/mfd/syscon.h>
17589aa6e7SVladimir Oltean #include <linux/skbuff.h>
18589aa6e7SVladimir Oltean #include <net/switchdev.h>
19589aa6e7SVladimir Oltean 
20b67f5502SColin Foster #include <soc/mscc/ocelot.h>
21589aa6e7SVladimir Oltean #include <soc/mscc/ocelot_vcap.h>
2232ecd22bSColin Foster #include <soc/mscc/vsc7514_regs.h>
23753a026cSClément Léger #include "ocelot_fdma.h"
24589aa6e7SVladimir Oltean #include "ocelot.h"
25589aa6e7SVladimir Oltean 
2677043c37SXiaoliang Yang #define VSC7514_VCAP_POLICER_BASE			128
2777043c37SXiaoliang Yang #define VSC7514_VCAP_POLICER_MAX			191
2877043c37SXiaoliang Yang 
ocelot_chip_init(struct ocelot * ocelot,const struct ocelot_ops * ops)29d9feb904SVladimir Oltean static int ocelot_chip_init(struct ocelot *ocelot, const struct ocelot_ops *ops)
30d9feb904SVladimir Oltean {
31d9feb904SVladimir Oltean 	int ret;
32d9feb904SVladimir Oltean 
332efaca41SColin Foster 	ocelot->map = vsc7514_regmap;
34d9feb904SVladimir Oltean 	ocelot->num_mact_rows = 1024;
35d9feb904SVladimir Oltean 	ocelot->ops = ops;
36d9feb904SVladimir Oltean 
37728d8019SColin Foster 	ret = ocelot_regfields_init(ocelot, vsc7514_regfields);
38d9feb904SVladimir Oltean 	if (ret)
39d9feb904SVladimir Oltean 		return ret;
40d9feb904SVladimir Oltean 
41d9feb904SVladimir Oltean 	ocelot_pll5_init(ocelot);
42d9feb904SVladimir Oltean 
43d9feb904SVladimir Oltean 	eth_random_addr(ocelot->base_mac);
44d9feb904SVladimir Oltean 	ocelot->base_mac[5] &= 0xf0;
45d9feb904SVladimir Oltean 
46d9feb904SVladimir Oltean 	return 0;
47d9feb904SVladimir Oltean }
48d9feb904SVladimir Oltean 
ocelot_xtr_irq_handler(int irq,void * arg)49589aa6e7SVladimir Oltean static irqreturn_t ocelot_xtr_irq_handler(int irq, void *arg)
50589aa6e7SVladimir Oltean {
51589aa6e7SVladimir Oltean 	struct ocelot *ocelot = arg;
52924ee317SVladimir Oltean 	int grp = 0, err;
53589aa6e7SVladimir Oltean 
54f833ca29SVladimir Oltean 	while (ocelot_read(ocelot, QS_XTR_DATA_PRESENT) & BIT(grp)) {
55589aa6e7SVladimir Oltean 		struct sk_buff *skb;
56589aa6e7SVladimir Oltean 
57924ee317SVladimir Oltean 		err = ocelot_xtr_poll_frame(ocelot, grp, &skb);
58924ee317SVladimir Oltean 		if (err)
59a94306ceSVladimir Oltean 			goto out;
60589aa6e7SVladimir Oltean 
61924ee317SVladimir Oltean 		skb->dev->stats.rx_bytes += skb->len;
62924ee317SVladimir Oltean 		skb->dev->stats.rx_packets++;
63589aa6e7SVladimir Oltean 
64589aa6e7SVladimir Oltean 		if (!skb_defer_rx_timestamp(skb))
65589aa6e7SVladimir Oltean 			netif_rx(skb);
66f833ca29SVladimir Oltean 	}
67589aa6e7SVladimir Oltean 
68a94306ceSVladimir Oltean out:
69d7795f8fSVladimir Oltean 	if (err < 0)
700a6f17c6SVladimir Oltean 		ocelot_drain_cpu_queue(ocelot, 0);
71589aa6e7SVladimir Oltean 
72589aa6e7SVladimir Oltean 	return IRQ_HANDLED;
73589aa6e7SVladimir Oltean }
74589aa6e7SVladimir Oltean 
ocelot_ptp_rdy_irq_handler(int irq,void * arg)75589aa6e7SVladimir Oltean static irqreturn_t ocelot_ptp_rdy_irq_handler(int irq, void *arg)
76589aa6e7SVladimir Oltean {
77589aa6e7SVladimir Oltean 	struct ocelot *ocelot = arg;
78589aa6e7SVladimir Oltean 
79589aa6e7SVladimir Oltean 	ocelot_get_txtstamp(ocelot);
80589aa6e7SVladimir Oltean 
81589aa6e7SVladimir Oltean 	return IRQ_HANDLED;
82589aa6e7SVladimir Oltean }
83589aa6e7SVladimir Oltean 
84589aa6e7SVladimir Oltean static const struct of_device_id mscc_ocelot_match[] = {
85589aa6e7SVladimir Oltean 	{ .compatible = "mscc,vsc7514-switch" },
86589aa6e7SVladimir Oltean 	{ }
87589aa6e7SVladimir Oltean };
88589aa6e7SVladimir Oltean MODULE_DEVICE_TABLE(of, mscc_ocelot_match);
89589aa6e7SVladimir Oltean 
90589aa6e7SVladimir Oltean static const struct ocelot_ops ocelot_ops = {
91589aa6e7SVladimir Oltean 	.reset			= ocelot_reset,
92aa92d836SMaxim Kochetkov 	.wm_enc			= ocelot_wm_enc,
93703b7621SVladimir Oltean 	.wm_dec			= ocelot_wm_dec,
94703b7621SVladimir Oltean 	.wm_stat		= ocelot_wm_stat,
95319e4dd1SVladimir Oltean 	.port_to_netdev		= ocelot_port_to_netdev,
96319e4dd1SVladimir Oltean 	.netdev_to_port		= ocelot_netdev_to_port,
97589aa6e7SVladimir Oltean };
98589aa6e7SVladimir Oltean 
99589aa6e7SVladimir Oltean static struct ptp_clock_info ocelot_ptp_clock_info = {
100589aa6e7SVladimir Oltean 	.owner		= THIS_MODULE,
101589aa6e7SVladimir Oltean 	.name		= "ocelot ptp",
102589aa6e7SVladimir Oltean 	.max_adj	= 0x7fffffff,
103589aa6e7SVladimir Oltean 	.n_alarm	= 0,
104589aa6e7SVladimir Oltean 	.n_ext_ts	= 0,
105589aa6e7SVladimir Oltean 	.n_per_out	= OCELOT_PTP_PINS_NUM,
106589aa6e7SVladimir Oltean 	.n_pins		= OCELOT_PTP_PINS_NUM,
107589aa6e7SVladimir Oltean 	.pps		= 0,
108589aa6e7SVladimir Oltean 	.gettime64	= ocelot_ptp_gettime64,
109589aa6e7SVladimir Oltean 	.settime64	= ocelot_ptp_settime64,
110589aa6e7SVladimir Oltean 	.adjtime	= ocelot_ptp_adjtime,
111589aa6e7SVladimir Oltean 	.adjfine	= ocelot_ptp_adjfine,
112589aa6e7SVladimir Oltean 	.verify		= ocelot_ptp_verify,
113589aa6e7SVladimir Oltean 	.enable		= ocelot_ptp_enable,
114589aa6e7SVladimir Oltean };
115589aa6e7SVladimir Oltean 
mscc_ocelot_teardown_devlink_ports(struct ocelot * ocelot)1166c30384eSVladimir Oltean static void mscc_ocelot_teardown_devlink_ports(struct ocelot *ocelot)
1176c30384eSVladimir Oltean {
1186c30384eSVladimir Oltean 	int port;
1196c30384eSVladimir Oltean 
1206c30384eSVladimir Oltean 	for (port = 0; port < ocelot->num_phys_ports; port++)
1216c30384eSVladimir Oltean 		ocelot_port_devlink_teardown(ocelot, port);
1226c30384eSVladimir Oltean }
1236c30384eSVladimir Oltean 
mscc_ocelot_release_ports(struct ocelot * ocelot)12422cdb493SVladimir Oltean static void mscc_ocelot_release_ports(struct ocelot *ocelot)
12522cdb493SVladimir Oltean {
12622cdb493SVladimir Oltean 	int port;
12722cdb493SVladimir Oltean 
12822cdb493SVladimir Oltean 	for (port = 0; port < ocelot->num_phys_ports; port++) {
12922cdb493SVladimir Oltean 		struct ocelot_port *ocelot_port;
13022cdb493SVladimir Oltean 
13122cdb493SVladimir Oltean 		ocelot_port = ocelot->ports[port];
13222cdb493SVladimir Oltean 		if (!ocelot_port)
13322cdb493SVladimir Oltean 			continue;
13422cdb493SVladimir Oltean 
135e5fb512dSVladimir Oltean 		ocelot_deinit_port(ocelot, port);
136e0c16233SDan Carpenter 		ocelot_release_port(ocelot_port);
13722cdb493SVladimir Oltean 	}
13822cdb493SVladimir Oltean }
13922cdb493SVladimir Oltean 
mscc_ocelot_init_ports(struct platform_device * pdev,struct device_node * ports)1407c411799SVladimir Oltean static int mscc_ocelot_init_ports(struct platform_device *pdev,
1417c411799SVladimir Oltean 				  struct device_node *ports)
1427c411799SVladimir Oltean {
1437c411799SVladimir Oltean 	struct ocelot *ocelot = platform_get_drvdata(pdev);
144e0c16233SDan Carpenter 	u32 devlink_ports_registered = 0;
1457c411799SVladimir Oltean 	struct device_node *portnp;
1466c30384eSVladimir Oltean 	int port, err;
1476c30384eSVladimir Oltean 	u32 reg;
1487c411799SVladimir Oltean 
1497c411799SVladimir Oltean 	ocelot->ports = devm_kcalloc(ocelot->dev, ocelot->num_phys_ports,
1507c411799SVladimir Oltean 				     sizeof(struct ocelot_port *), GFP_KERNEL);
1517c411799SVladimir Oltean 	if (!ocelot->ports)
1527c411799SVladimir Oltean 		return -ENOMEM;
1537c411799SVladimir Oltean 
1546c30384eSVladimir Oltean 	ocelot->devlink_ports = devm_kcalloc(ocelot->dev,
1556c30384eSVladimir Oltean 					     ocelot->num_phys_ports,
1566c30384eSVladimir Oltean 					     sizeof(*ocelot->devlink_ports),
1576c30384eSVladimir Oltean 					     GFP_KERNEL);
1586c30384eSVladimir Oltean 	if (!ocelot->devlink_ports)
1596c30384eSVladimir Oltean 		return -ENOMEM;
1606c30384eSVladimir Oltean 
1617c411799SVladimir Oltean 	for_each_available_child_of_node(ports, portnp) {
1627c411799SVladimir Oltean 		struct regmap *target;
1637c411799SVladimir Oltean 		struct resource *res;
1647c411799SVladimir Oltean 		char res_name[8];
1657c411799SVladimir Oltean 
1666c30384eSVladimir Oltean 		if (of_property_read_u32(portnp, "reg", &reg))
1677c411799SVladimir Oltean 			continue;
1687c411799SVladimir Oltean 
1696c30384eSVladimir Oltean 		port = reg;
170e0c16233SDan Carpenter 		if (port < 0 || port >= ocelot->num_phys_ports) {
171e0c16233SDan Carpenter 			dev_err(ocelot->dev,
172e0c16233SDan Carpenter 				"invalid port number: %d >= %d\n", port,
173e0c16233SDan Carpenter 				ocelot->num_phys_ports);
174e0c16233SDan Carpenter 			continue;
175e0c16233SDan Carpenter 		}
1766c30384eSVladimir Oltean 
1777c411799SVladimir Oltean 		snprintf(res_name, sizeof(res_name), "port%d", port);
1787c411799SVladimir Oltean 
1797c411799SVladimir Oltean 		res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
1807c411799SVladimir Oltean 						   res_name);
1817c411799SVladimir Oltean 		target = ocelot_regmap_init(ocelot, res);
182e0c16233SDan Carpenter 		if (IS_ERR(target)) {
183e0c16233SDan Carpenter 			err = PTR_ERR(target);
184d1a7b9e4SWan Jiabing 			of_node_put(portnp);
185e0c16233SDan Carpenter 			goto out_teardown;
186e0c16233SDan Carpenter 		}
1877c411799SVladimir Oltean 
1886c30384eSVladimir Oltean 		err = ocelot_port_devlink_init(ocelot, port,
1896c30384eSVladimir Oltean 					       DEVLINK_PORT_FLAVOUR_PHYSICAL);
1906c30384eSVladimir Oltean 		if (err) {
1916c30384eSVladimir Oltean 			of_node_put(portnp);
1926c30384eSVladimir Oltean 			goto out_teardown;
1936c30384eSVladimir Oltean 		}
1946c30384eSVladimir Oltean 
195e6e12df6SVladimir Oltean 		err = ocelot_probe_port(ocelot, port, target, portnp);
1967c411799SVladimir Oltean 		if (err) {
1975c8bb71dSVladimir Oltean 			ocelot_port_devlink_teardown(ocelot, port);
1985c8bb71dSVladimir Oltean 			continue;
1997c411799SVladimir Oltean 		}
2007c411799SVladimir Oltean 
2015c8bb71dSVladimir Oltean 		devlink_ports_registered |= BIT(port);
2027c411799SVladimir Oltean 	}
2037c411799SVladimir Oltean 
2046c30384eSVladimir Oltean 	/* Initialize unused devlink ports at the end */
2056c30384eSVladimir Oltean 	for (port = 0; port < ocelot->num_phys_ports; port++) {
206e0c16233SDan Carpenter 		if (devlink_ports_registered & BIT(port))
2076c30384eSVladimir Oltean 			continue;
2086c30384eSVladimir Oltean 
2096c30384eSVladimir Oltean 		err = ocelot_port_devlink_init(ocelot, port,
2106c30384eSVladimir Oltean 					       DEVLINK_PORT_FLAVOUR_UNUSED);
211e0c16233SDan Carpenter 		if (err)
2126c30384eSVladimir Oltean 			goto out_teardown;
2136c30384eSVladimir Oltean 
214e0c16233SDan Carpenter 		devlink_ports_registered |= BIT(port);
215e0c16233SDan Carpenter 	}
2166c30384eSVladimir Oltean 
2177c411799SVladimir Oltean 	return 0;
2186c30384eSVladimir Oltean 
2196c30384eSVladimir Oltean out_teardown:
2206c30384eSVladimir Oltean 	/* Unregister the network interfaces */
2216c30384eSVladimir Oltean 	mscc_ocelot_release_ports(ocelot);
2226c30384eSVladimir Oltean 	/* Tear down devlink ports for the registered network interfaces */
2236c30384eSVladimir Oltean 	for (port = 0; port < ocelot->num_phys_ports; port++) {
224e0c16233SDan Carpenter 		if (devlink_ports_registered & BIT(port))
2256c30384eSVladimir Oltean 			ocelot_port_devlink_teardown(ocelot, port);
2266c30384eSVladimir Oltean 	}
2276c30384eSVladimir Oltean 	return err;
2287c411799SVladimir Oltean }
2297c411799SVladimir Oltean 
mscc_ocelot_probe(struct platform_device * pdev)230589aa6e7SVladimir Oltean static int mscc_ocelot_probe(struct platform_device *pdev)
231589aa6e7SVladimir Oltean {
232589aa6e7SVladimir Oltean 	struct device_node *np = pdev->dev.of_node;
233589aa6e7SVladimir Oltean 	int err, irq_xtr, irq_ptp_rdy;
2347c411799SVladimir Oltean 	struct device_node *ports;
2356c30384eSVladimir Oltean 	struct devlink *devlink;
236589aa6e7SVladimir Oltean 	struct ocelot *ocelot;
237589aa6e7SVladimir Oltean 	struct regmap *hsio;
238589aa6e7SVladimir Oltean 	unsigned int i;
239589aa6e7SVladimir Oltean 
240589aa6e7SVladimir Oltean 	struct {
241589aa6e7SVladimir Oltean 		enum ocelot_target id;
242589aa6e7SVladimir Oltean 		char *name;
243589aa6e7SVladimir Oltean 		u8 optional:1;
244589aa6e7SVladimir Oltean 	} io_target[] = {
245589aa6e7SVladimir Oltean 		{ SYS, "sys" },
246589aa6e7SVladimir Oltean 		{ REW, "rew" },
247589aa6e7SVladimir Oltean 		{ QSYS, "qsys" },
248589aa6e7SVladimir Oltean 		{ ANA, "ana" },
249589aa6e7SVladimir Oltean 		{ QS, "qs" },
250e3aea296SVladimir Oltean 		{ S0, "s0" },
251a61e365dSVladimir Oltean 		{ S1, "s1" },
252589aa6e7SVladimir Oltean 		{ S2, "s2" },
253589aa6e7SVladimir Oltean 		{ PTP, "ptp", 1 },
254753a026cSClément Léger 		{ FDMA, "fdma", 1 },
255589aa6e7SVladimir Oltean 	};
256589aa6e7SVladimir Oltean 
257589aa6e7SVladimir Oltean 	if (!np && !pdev->dev.platform_data)
258589aa6e7SVladimir Oltean 		return -ENODEV;
259589aa6e7SVladimir Oltean 
260919d13a7SLeon Romanovsky 	devlink =
261919d13a7SLeon Romanovsky 		devlink_alloc(&ocelot_devlink_ops, sizeof(*ocelot), &pdev->dev);
2626c30384eSVladimir Oltean 	if (!devlink)
263589aa6e7SVladimir Oltean 		return -ENOMEM;
264589aa6e7SVladimir Oltean 
2656c30384eSVladimir Oltean 	ocelot = devlink_priv(devlink);
2666c30384eSVladimir Oltean 	ocelot->devlink = priv_to_devlink(ocelot);
267589aa6e7SVladimir Oltean 	platform_set_drvdata(pdev, ocelot);
268589aa6e7SVladimir Oltean 	ocelot->dev = &pdev->dev;
269589aa6e7SVladimir Oltean 
270589aa6e7SVladimir Oltean 	for (i = 0; i < ARRAY_SIZE(io_target); i++) {
271589aa6e7SVladimir Oltean 		struct regmap *target;
272589aa6e7SVladimir Oltean 		struct resource *res;
273589aa6e7SVladimir Oltean 
274589aa6e7SVladimir Oltean 		res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
275589aa6e7SVladimir Oltean 						   io_target[i].name);
276589aa6e7SVladimir Oltean 
277589aa6e7SVladimir Oltean 		target = ocelot_regmap_init(ocelot, res);
278589aa6e7SVladimir Oltean 		if (IS_ERR(target)) {
279589aa6e7SVladimir Oltean 			if (io_target[i].optional) {
280589aa6e7SVladimir Oltean 				ocelot->targets[io_target[i].id] = NULL;
281589aa6e7SVladimir Oltean 				continue;
282589aa6e7SVladimir Oltean 			}
2836c30384eSVladimir Oltean 			err = PTR_ERR(target);
2846c30384eSVladimir Oltean 			goto out_free_devlink;
285589aa6e7SVladimir Oltean 		}
286589aa6e7SVladimir Oltean 
287589aa6e7SVladimir Oltean 		ocelot->targets[io_target[i].id] = target;
288589aa6e7SVladimir Oltean 	}
289589aa6e7SVladimir Oltean 
290753a026cSClément Léger 	if (ocelot->targets[FDMA])
291753a026cSClément Léger 		ocelot_fdma_init(pdev, ocelot);
292753a026cSClément Léger 
293589aa6e7SVladimir Oltean 	hsio = syscon_regmap_lookup_by_compatible("mscc,ocelot-hsio");
294589aa6e7SVladimir Oltean 	if (IS_ERR(hsio)) {
295589aa6e7SVladimir Oltean 		dev_err(&pdev->dev, "missing hsio syscon\n");
2966c30384eSVladimir Oltean 		err = PTR_ERR(hsio);
2976c30384eSVladimir Oltean 		goto out_free_devlink;
298589aa6e7SVladimir Oltean 	}
299589aa6e7SVladimir Oltean 
300589aa6e7SVladimir Oltean 	ocelot->targets[HSIO] = hsio;
301589aa6e7SVladimir Oltean 
302589aa6e7SVladimir Oltean 	err = ocelot_chip_init(ocelot, &ocelot_ops);
303589aa6e7SVladimir Oltean 	if (err)
3046c30384eSVladimir Oltean 		goto out_free_devlink;
305589aa6e7SVladimir Oltean 
306589aa6e7SVladimir Oltean 	irq_xtr = platform_get_irq_byname(pdev, "xtr");
3074160d9ecSDan Carpenter 	if (irq_xtr < 0) {
3084160d9ecSDan Carpenter 		err = irq_xtr;
3096c30384eSVladimir Oltean 		goto out_free_devlink;
3104160d9ecSDan Carpenter 	}
311589aa6e7SVladimir Oltean 
312589aa6e7SVladimir Oltean 	err = devm_request_threaded_irq(&pdev->dev, irq_xtr, NULL,
313589aa6e7SVladimir Oltean 					ocelot_xtr_irq_handler, IRQF_ONESHOT,
314589aa6e7SVladimir Oltean 					"frame extraction", ocelot);
315589aa6e7SVladimir Oltean 	if (err)
3166c30384eSVladimir Oltean 		goto out_free_devlink;
317589aa6e7SVladimir Oltean 
318589aa6e7SVladimir Oltean 	irq_ptp_rdy = platform_get_irq_byname(pdev, "ptp_rdy");
319589aa6e7SVladimir Oltean 	if (irq_ptp_rdy > 0 && ocelot->targets[PTP]) {
320589aa6e7SVladimir Oltean 		err = devm_request_threaded_irq(&pdev->dev, irq_ptp_rdy, NULL,
321589aa6e7SVladimir Oltean 						ocelot_ptp_rdy_irq_handler,
322589aa6e7SVladimir Oltean 						IRQF_ONESHOT, "ptp ready",
323589aa6e7SVladimir Oltean 						ocelot);
324589aa6e7SVladimir Oltean 		if (err)
3256c30384eSVladimir Oltean 			goto out_free_devlink;
326589aa6e7SVladimir Oltean 
327589aa6e7SVladimir Oltean 		/* Both the PTP interrupt and the PTP bank are available */
328589aa6e7SVladimir Oltean 		ocelot->ptp = 1;
329589aa6e7SVladimir Oltean 	}
330589aa6e7SVladimir Oltean 
331589aa6e7SVladimir Oltean 	ports = of_get_child_by_name(np, "ethernet-ports");
332589aa6e7SVladimir Oltean 	if (!ports) {
3337c411799SVladimir Oltean 		dev_err(ocelot->dev, "no ethernet-ports child node found\n");
3346c30384eSVladimir Oltean 		err = -ENODEV;
3356c30384eSVladimir Oltean 		goto out_free_devlink;
336589aa6e7SVladimir Oltean 	}
337589aa6e7SVladimir Oltean 
338589aa6e7SVladimir Oltean 	ocelot->num_phys_ports = of_get_child_count(ports);
339edd2410bSVladimir Oltean 	ocelot->num_flooding_pgids = 1;
340589aa6e7SVladimir Oltean 
341589aa6e7SVladimir Oltean 	ocelot->vcap = vsc7514_vcap_props;
34277043c37SXiaoliang Yang 
34377043c37SXiaoliang Yang 	ocelot->vcap_pol.base = VSC7514_VCAP_POLICER_BASE;
34477043c37SXiaoliang Yang 	ocelot->vcap_pol.max = VSC7514_VCAP_POLICER_MAX;
34577043c37SXiaoliang Yang 
3462d44b097SVladimir Oltean 	ocelot->npi = -1;
347589aa6e7SVladimir Oltean 
348d1cc0e93SVladimir Oltean 	err = ocelot_init(ocelot);
349d1cc0e93SVladimir Oltean 	if (err)
350d1cc0e93SVladimir Oltean 		goto out_put_ports;
351d1cc0e93SVladimir Oltean 
3526c30384eSVladimir Oltean 	err = mscc_ocelot_init_ports(pdev, ports);
3536c30384eSVladimir Oltean 	if (err)
3546c30384eSVladimir Oltean 		goto out_ocelot_devlink_unregister;
3556c30384eSVladimir Oltean 
356753a026cSClément Léger 	if (ocelot->fdma)
357753a026cSClément Léger 		ocelot_fdma_start(ocelot);
358753a026cSClément Léger 
359f59fd9caSVladimir Oltean 	err = ocelot_devlink_sb_register(ocelot);
360f59fd9caSVladimir Oltean 	if (err)
361f59fd9caSVladimir Oltean 		goto out_ocelot_release_ports;
362f59fd9caSVladimir Oltean 
363589aa6e7SVladimir Oltean 	if (ocelot->ptp) {
364589aa6e7SVladimir Oltean 		err = ocelot_init_timestamp(ocelot, &ocelot_ptp_clock_info);
365589aa6e7SVladimir Oltean 		if (err) {
366589aa6e7SVladimir Oltean 			dev_err(ocelot->dev,
367589aa6e7SVladimir Oltean 				"Timestamp initialization failed\n");
368589aa6e7SVladimir Oltean 			ocelot->ptp = 0;
369589aa6e7SVladimir Oltean 		}
370589aa6e7SVladimir Oltean 	}
371589aa6e7SVladimir Oltean 
372589aa6e7SVladimir Oltean 	register_netdevice_notifier(&ocelot_netdevice_nb);
373589aa6e7SVladimir Oltean 	register_switchdev_notifier(&ocelot_switchdev_nb);
374589aa6e7SVladimir Oltean 	register_switchdev_blocking_notifier(&ocelot_switchdev_blocking_nb);
375589aa6e7SVladimir Oltean 
376f87675b8SChristophe JAILLET 	of_node_put(ports);
37767d78e7fSLeon Romanovsky 	devlink_register(devlink);
378f87675b8SChristophe JAILLET 
379589aa6e7SVladimir Oltean 	dev_info(&pdev->dev, "Ocelot switch probed\n");
380589aa6e7SVladimir Oltean 
381f87675b8SChristophe JAILLET 	return 0;
382f87675b8SChristophe JAILLET 
383f59fd9caSVladimir Oltean out_ocelot_release_ports:
384f59fd9caSVladimir Oltean 	mscc_ocelot_release_ports(ocelot);
385f59fd9caSVladimir Oltean 	mscc_ocelot_teardown_devlink_ports(ocelot);
3866c30384eSVladimir Oltean out_ocelot_devlink_unregister:
387f87675b8SChristophe JAILLET 	ocelot_deinit(ocelot);
388589aa6e7SVladimir Oltean out_put_ports:
389589aa6e7SVladimir Oltean 	of_node_put(ports);
3906c30384eSVladimir Oltean out_free_devlink:
3916c30384eSVladimir Oltean 	devlink_free(devlink);
392589aa6e7SVladimir Oltean 	return err;
393589aa6e7SVladimir Oltean }
394589aa6e7SVladimir Oltean 
mscc_ocelot_remove(struct platform_device * pdev)395589aa6e7SVladimir Oltean static int mscc_ocelot_remove(struct platform_device *pdev)
396589aa6e7SVladimir Oltean {
397589aa6e7SVladimir Oltean 	struct ocelot *ocelot = platform_get_drvdata(pdev);
398589aa6e7SVladimir Oltean 
399753a026cSClément Léger 	if (ocelot->fdma)
400753a026cSClément Léger 		ocelot_fdma_deinit(ocelot);
40167d78e7fSLeon Romanovsky 	devlink_unregister(ocelot->devlink);
402589aa6e7SVladimir Oltean 	ocelot_deinit_timestamp(ocelot);
403f59fd9caSVladimir Oltean 	ocelot_devlink_sb_unregister(ocelot);
40422cdb493SVladimir Oltean 	mscc_ocelot_release_ports(ocelot);
4056c30384eSVladimir Oltean 	mscc_ocelot_teardown_devlink_ports(ocelot);
406589aa6e7SVladimir Oltean 	ocelot_deinit(ocelot);
407589aa6e7SVladimir Oltean 	unregister_switchdev_blocking_notifier(&ocelot_switchdev_blocking_nb);
408589aa6e7SVladimir Oltean 	unregister_switchdev_notifier(&ocelot_switchdev_nb);
409589aa6e7SVladimir Oltean 	unregister_netdevice_notifier(&ocelot_netdevice_nb);
4106c30384eSVladimir Oltean 	devlink_free(ocelot->devlink);
411589aa6e7SVladimir Oltean 
412589aa6e7SVladimir Oltean 	return 0;
413589aa6e7SVladimir Oltean }
414589aa6e7SVladimir Oltean 
415589aa6e7SVladimir Oltean static struct platform_driver mscc_ocelot_driver = {
416589aa6e7SVladimir Oltean 	.probe = mscc_ocelot_probe,
417589aa6e7SVladimir Oltean 	.remove = mscc_ocelot_remove,
418589aa6e7SVladimir Oltean 	.driver = {
419589aa6e7SVladimir Oltean 		.name = "ocelot-switch",
420589aa6e7SVladimir Oltean 		.of_match_table = mscc_ocelot_match,
421589aa6e7SVladimir Oltean 	},
422589aa6e7SVladimir Oltean };
423589aa6e7SVladimir Oltean 
424589aa6e7SVladimir Oltean module_platform_driver(mscc_ocelot_driver);
425589aa6e7SVladimir Oltean 
426589aa6e7SVladimir Oltean MODULE_DESCRIPTION("Microsemi Ocelot switch driver");
427589aa6e7SVladimir Oltean MODULE_AUTHOR("Alexandre Belloni <alexandre.belloni@bootlin.com>");
428589aa6e7SVladimir Oltean MODULE_LICENSE("Dual MIT/GPL");
429