xref: /openbmc/linux/drivers/net/dsa/ocelot/felix.c (revision 37fe45ad126dd5e9ade8be7ae5010fa1072abbbc)
156051948SVladimir Oltean // SPDX-License-Identifier: GPL-2.0
256051948SVladimir Oltean /* Copyright 2019 NXP Semiconductors
356051948SVladimir Oltean  */
456051948SVladimir Oltean #include <uapi/linux/if_bridge.h>
5bdeced75SVladimir Oltean #include <soc/mscc/ocelot_qsys.h>
6bdeced75SVladimir Oltean #include <soc/mscc/ocelot_sys.h>
7bdeced75SVladimir Oltean #include <soc/mscc/ocelot_dev.h>
8bdeced75SVladimir Oltean #include <soc/mscc/ocelot_ana.h>
956051948SVladimir Oltean #include <soc/mscc/ocelot.h>
10c0bcf537SYangbo Lu #include <linux/packing.h>
1156051948SVladimir Oltean #include <linux/module.h>
12bdeced75SVladimir Oltean #include <linux/of_net.h>
1356051948SVladimir Oltean #include <linux/pci.h>
1456051948SVladimir Oltean #include <linux/of.h>
1556051948SVladimir Oltean #include <net/dsa.h>
1656051948SVladimir Oltean #include "felix.h"
1756051948SVladimir Oltean 
1856051948SVladimir Oltean static enum dsa_tag_protocol felix_get_tag_protocol(struct dsa_switch *ds,
194d776482SFlorian Fainelli 						    int port,
204d776482SFlorian Fainelli 						    enum dsa_tag_protocol mp)
2156051948SVladimir Oltean {
2256051948SVladimir Oltean 	return DSA_TAG_PROTO_OCELOT;
2356051948SVladimir Oltean }
2456051948SVladimir Oltean 
2556051948SVladimir Oltean static int felix_set_ageing_time(struct dsa_switch *ds,
2656051948SVladimir Oltean 				 unsigned int ageing_time)
2756051948SVladimir Oltean {
2856051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
2956051948SVladimir Oltean 
3056051948SVladimir Oltean 	ocelot_set_ageing_time(ocelot, ageing_time);
3156051948SVladimir Oltean 
3256051948SVladimir Oltean 	return 0;
3356051948SVladimir Oltean }
3456051948SVladimir Oltean 
3556051948SVladimir Oltean static int felix_fdb_dump(struct dsa_switch *ds, int port,
3656051948SVladimir Oltean 			  dsa_fdb_dump_cb_t *cb, void *data)
3756051948SVladimir Oltean {
3856051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
3956051948SVladimir Oltean 
4056051948SVladimir Oltean 	return ocelot_fdb_dump(ocelot, port, cb, data);
4156051948SVladimir Oltean }
4256051948SVladimir Oltean 
4356051948SVladimir Oltean static int felix_fdb_add(struct dsa_switch *ds, int port,
4456051948SVladimir Oltean 			 const unsigned char *addr, u16 vid)
4556051948SVladimir Oltean {
4656051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
4756051948SVladimir Oltean 	bool vlan_aware;
4856051948SVladimir Oltean 
4956051948SVladimir Oltean 	vlan_aware = dsa_port_is_vlan_filtering(dsa_to_port(ds, port));
5056051948SVladimir Oltean 
5156051948SVladimir Oltean 	return ocelot_fdb_add(ocelot, port, addr, vid, vlan_aware);
5256051948SVladimir Oltean }
5356051948SVladimir Oltean 
5456051948SVladimir Oltean static int felix_fdb_del(struct dsa_switch *ds, int port,
5556051948SVladimir Oltean 			 const unsigned char *addr, u16 vid)
5656051948SVladimir Oltean {
5756051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
5856051948SVladimir Oltean 
5956051948SVladimir Oltean 	return ocelot_fdb_del(ocelot, port, addr, vid);
6056051948SVladimir Oltean }
6156051948SVladimir Oltean 
6256051948SVladimir Oltean static void felix_bridge_stp_state_set(struct dsa_switch *ds, int port,
6356051948SVladimir Oltean 				       u8 state)
6456051948SVladimir Oltean {
6556051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
6656051948SVladimir Oltean 
6756051948SVladimir Oltean 	return ocelot_bridge_stp_state_set(ocelot, port, state);
6856051948SVladimir Oltean }
6956051948SVladimir Oltean 
7056051948SVladimir Oltean static int felix_bridge_join(struct dsa_switch *ds, int port,
7156051948SVladimir Oltean 			     struct net_device *br)
7256051948SVladimir Oltean {
7356051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
7456051948SVladimir Oltean 
7556051948SVladimir Oltean 	return ocelot_port_bridge_join(ocelot, port, br);
7656051948SVladimir Oltean }
7756051948SVladimir Oltean 
7856051948SVladimir Oltean static void felix_bridge_leave(struct dsa_switch *ds, int port,
7956051948SVladimir Oltean 			       struct net_device *br)
8056051948SVladimir Oltean {
8156051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
8256051948SVladimir Oltean 
8356051948SVladimir Oltean 	ocelot_port_bridge_leave(ocelot, port, br);
8456051948SVladimir Oltean }
8556051948SVladimir Oltean 
8656051948SVladimir Oltean /* This callback needs to be present */
8756051948SVladimir Oltean static int felix_vlan_prepare(struct dsa_switch *ds, int port,
8856051948SVladimir Oltean 			      const struct switchdev_obj_port_vlan *vlan)
8956051948SVladimir Oltean {
9056051948SVladimir Oltean 	return 0;
9156051948SVladimir Oltean }
9256051948SVladimir Oltean 
9356051948SVladimir Oltean static int felix_vlan_filtering(struct dsa_switch *ds, int port, bool enabled)
9456051948SVladimir Oltean {
9556051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
9656051948SVladimir Oltean 
9756051948SVladimir Oltean 	ocelot_port_vlan_filtering(ocelot, port, enabled);
9856051948SVladimir Oltean 
9956051948SVladimir Oltean 	return 0;
10056051948SVladimir Oltean }
10156051948SVladimir Oltean 
10256051948SVladimir Oltean static void felix_vlan_add(struct dsa_switch *ds, int port,
10356051948SVladimir Oltean 			   const struct switchdev_obj_port_vlan *vlan)
10456051948SVladimir Oltean {
10556051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
10656051948SVladimir Oltean 	u16 vid;
10756051948SVladimir Oltean 	int err;
10856051948SVladimir Oltean 
10956051948SVladimir Oltean 	for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) {
11056051948SVladimir Oltean 		err = ocelot_vlan_add(ocelot, port, vid,
11156051948SVladimir Oltean 				      vlan->flags & BRIDGE_VLAN_INFO_PVID,
11256051948SVladimir Oltean 				      vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED);
11356051948SVladimir Oltean 		if (err) {
11456051948SVladimir Oltean 			dev_err(ds->dev, "Failed to add VLAN %d to port %d: %d\n",
11556051948SVladimir Oltean 				vid, port, err);
11656051948SVladimir Oltean 			return;
11756051948SVladimir Oltean 		}
11856051948SVladimir Oltean 	}
11956051948SVladimir Oltean }
12056051948SVladimir Oltean 
12156051948SVladimir Oltean static int felix_vlan_del(struct dsa_switch *ds, int port,
12256051948SVladimir Oltean 			  const struct switchdev_obj_port_vlan *vlan)
12356051948SVladimir Oltean {
12456051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
12556051948SVladimir Oltean 	u16 vid;
12656051948SVladimir Oltean 	int err;
12756051948SVladimir Oltean 
12856051948SVladimir Oltean 	for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) {
12956051948SVladimir Oltean 		err = ocelot_vlan_del(ocelot, port, vid);
13056051948SVladimir Oltean 		if (err) {
13156051948SVladimir Oltean 			dev_err(ds->dev, "Failed to remove VLAN %d from port %d: %d\n",
13256051948SVladimir Oltean 				vid, port, err);
13356051948SVladimir Oltean 			return err;
13456051948SVladimir Oltean 		}
13556051948SVladimir Oltean 	}
13656051948SVladimir Oltean 	return 0;
13756051948SVladimir Oltean }
13856051948SVladimir Oltean 
13956051948SVladimir Oltean static int felix_port_enable(struct dsa_switch *ds, int port,
14056051948SVladimir Oltean 			     struct phy_device *phy)
14156051948SVladimir Oltean {
14256051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
14356051948SVladimir Oltean 
14456051948SVladimir Oltean 	ocelot_port_enable(ocelot, port, phy);
14556051948SVladimir Oltean 
14656051948SVladimir Oltean 	return 0;
14756051948SVladimir Oltean }
14856051948SVladimir Oltean 
14956051948SVladimir Oltean static void felix_port_disable(struct dsa_switch *ds, int port)
15056051948SVladimir Oltean {
15156051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
15256051948SVladimir Oltean 
15356051948SVladimir Oltean 	return ocelot_port_disable(ocelot, port);
15456051948SVladimir Oltean }
15556051948SVladimir Oltean 
156bdeced75SVladimir Oltean static void felix_phylink_validate(struct dsa_switch *ds, int port,
157bdeced75SVladimir Oltean 				   unsigned long *supported,
158bdeced75SVladimir Oltean 				   struct phylink_link_state *state)
159bdeced75SVladimir Oltean {
160bdeced75SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
161bdeced75SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
162bdeced75SVladimir Oltean 	__ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, };
163bdeced75SVladimir Oltean 
164bdeced75SVladimir Oltean 	if (state->interface != PHY_INTERFACE_MODE_NA &&
165bdeced75SVladimir Oltean 	    state->interface != ocelot_port->phy_mode) {
166bdeced75SVladimir Oltean 		bitmap_zero(supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
167bdeced75SVladimir Oltean 		return;
168bdeced75SVladimir Oltean 	}
169bdeced75SVladimir Oltean 
170bdeced75SVladimir Oltean 	/* No half-duplex. */
171bdeced75SVladimir Oltean 	phylink_set_port_modes(mask);
172bdeced75SVladimir Oltean 	phylink_set(mask, Autoneg);
173bdeced75SVladimir Oltean 	phylink_set(mask, Pause);
174bdeced75SVladimir Oltean 	phylink_set(mask, Asym_Pause);
175bdeced75SVladimir Oltean 	if (state->interface != PHY_INTERFACE_MODE_2500BASEX) {
176bdeced75SVladimir Oltean 		phylink_set(mask, 10baseT_Full);
177bdeced75SVladimir Oltean 		phylink_set(mask, 100baseT_Full);
178bdeced75SVladimir Oltean 		phylink_set(mask, 1000baseT_Full);
179bdeced75SVladimir Oltean 	}
180bdeced75SVladimir Oltean 	/* The internal ports that run at 2.5G are overclocked GMII */
181bdeced75SVladimir Oltean 	if (state->interface == PHY_INTERFACE_MODE_GMII ||
182bdeced75SVladimir Oltean 	    state->interface == PHY_INTERFACE_MODE_2500BASEX ||
183bdeced75SVladimir Oltean 	    state->interface == PHY_INTERFACE_MODE_USXGMII) {
184bdeced75SVladimir Oltean 		phylink_set(mask, 2500baseT_Full);
185bdeced75SVladimir Oltean 		phylink_set(mask, 2500baseX_Full);
186bdeced75SVladimir Oltean 	}
187bdeced75SVladimir Oltean 
188bdeced75SVladimir Oltean 	bitmap_and(supported, supported, mask,
189bdeced75SVladimir Oltean 		   __ETHTOOL_LINK_MODE_MASK_NBITS);
190bdeced75SVladimir Oltean 	bitmap_and(state->advertising, state->advertising, mask,
191bdeced75SVladimir Oltean 		   __ETHTOOL_LINK_MODE_MASK_NBITS);
192bdeced75SVladimir Oltean }
193bdeced75SVladimir Oltean 
194bdeced75SVladimir Oltean static int felix_phylink_mac_pcs_get_state(struct dsa_switch *ds, int port,
195bdeced75SVladimir Oltean 					   struct phylink_link_state *state)
196bdeced75SVladimir Oltean {
197bdeced75SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
198bdeced75SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
199bdeced75SVladimir Oltean 
200bdeced75SVladimir Oltean 	if (felix->info->pcs_link_state)
201bdeced75SVladimir Oltean 		felix->info->pcs_link_state(ocelot, port, state);
202bdeced75SVladimir Oltean 
203bdeced75SVladimir Oltean 	return 0;
204bdeced75SVladimir Oltean }
205bdeced75SVladimir Oltean 
206bdeced75SVladimir Oltean static void felix_phylink_mac_config(struct dsa_switch *ds, int port,
207bdeced75SVladimir Oltean 				     unsigned int link_an_mode,
208bdeced75SVladimir Oltean 				     const struct phylink_link_state *state)
209bdeced75SVladimir Oltean {
210bdeced75SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
211bdeced75SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
212bdeced75SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
213bdeced75SVladimir Oltean 	u32 mac_fc_cfg;
214bdeced75SVladimir Oltean 
215bdeced75SVladimir Oltean 	/* Take port out of reset by clearing the MAC_TX_RST, MAC_RX_RST and
216bdeced75SVladimir Oltean 	 * PORT_RST bits in CLOCK_CFG
217bdeced75SVladimir Oltean 	 */
218bdeced75SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_CLOCK_CFG_LINK_SPEED(state->speed),
219bdeced75SVladimir Oltean 			   DEV_CLOCK_CFG);
220bdeced75SVladimir Oltean 
221bdeced75SVladimir Oltean 	/* Flow control. Link speed is only used here to evaluate the time
222bdeced75SVladimir Oltean 	 * specification in incoming pause frames.
223bdeced75SVladimir Oltean 	 */
224bdeced75SVladimir Oltean 	mac_fc_cfg = SYS_MAC_FC_CFG_FC_LINK_SPEED(state->speed);
225bdeced75SVladimir Oltean 	if (state->pause & MLO_PAUSE_RX)
226bdeced75SVladimir Oltean 		mac_fc_cfg |= SYS_MAC_FC_CFG_RX_FC_ENA;
227bdeced75SVladimir Oltean 	if (state->pause & MLO_PAUSE_TX)
228bdeced75SVladimir Oltean 		mac_fc_cfg |= SYS_MAC_FC_CFG_TX_FC_ENA |
229bdeced75SVladimir Oltean 			      SYS_MAC_FC_CFG_PAUSE_VAL_CFG(0xffff) |
230bdeced75SVladimir Oltean 			      SYS_MAC_FC_CFG_FC_LATENCY_CFG(0x7) |
231bdeced75SVladimir Oltean 			      SYS_MAC_FC_CFG_ZERO_PAUSE_ENA;
232bdeced75SVladimir Oltean 	ocelot_write_rix(ocelot, mac_fc_cfg, SYS_MAC_FC_CFG, port);
233bdeced75SVladimir Oltean 
234bdeced75SVladimir Oltean 	ocelot_write_rix(ocelot, 0, ANA_POL_FLOWC, port);
235bdeced75SVladimir Oltean 
236bdeced75SVladimir Oltean 	if (felix->info->pcs_init)
237bdeced75SVladimir Oltean 		felix->info->pcs_init(ocelot, port, link_an_mode, state);
238bdeced75SVladimir Oltean }
239bdeced75SVladimir Oltean 
240bdeced75SVladimir Oltean static void felix_phylink_mac_an_restart(struct dsa_switch *ds, int port)
241bdeced75SVladimir Oltean {
242bdeced75SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
243bdeced75SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
244bdeced75SVladimir Oltean 
245bdeced75SVladimir Oltean 	if (felix->info->pcs_an_restart)
246bdeced75SVladimir Oltean 		felix->info->pcs_an_restart(ocelot, port);
247bdeced75SVladimir Oltean }
248bdeced75SVladimir Oltean 
249bdeced75SVladimir Oltean static void felix_phylink_mac_link_down(struct dsa_switch *ds, int port,
250bdeced75SVladimir Oltean 					unsigned int link_an_mode,
251bdeced75SVladimir Oltean 					phy_interface_t interface)
252bdeced75SVladimir Oltean {
253bdeced75SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
254bdeced75SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
255bdeced75SVladimir Oltean 
256bdeced75SVladimir Oltean 	ocelot_port_writel(ocelot_port, 0, DEV_MAC_ENA_CFG);
257bdeced75SVladimir Oltean 	ocelot_rmw_rix(ocelot, 0, QSYS_SWITCH_PORT_MODE_PORT_ENA,
258bdeced75SVladimir Oltean 		       QSYS_SWITCH_PORT_MODE, port);
259bdeced75SVladimir Oltean }
260bdeced75SVladimir Oltean 
261bdeced75SVladimir Oltean static void felix_phylink_mac_link_up(struct dsa_switch *ds, int port,
262bdeced75SVladimir Oltean 				      unsigned int link_an_mode,
263bdeced75SVladimir Oltean 				      phy_interface_t interface,
264bdeced75SVladimir Oltean 				      struct phy_device *phydev)
265bdeced75SVladimir Oltean {
266bdeced75SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
267bdeced75SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
268bdeced75SVladimir Oltean 
269bdeced75SVladimir Oltean 	/* Enable MAC module */
270bdeced75SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_MAC_ENA_CFG_RX_ENA |
271bdeced75SVladimir Oltean 			   DEV_MAC_ENA_CFG_TX_ENA, DEV_MAC_ENA_CFG);
272bdeced75SVladimir Oltean 
273bdeced75SVladimir Oltean 	/* Enable receiving frames on the port, and activate auto-learning of
274bdeced75SVladimir Oltean 	 * MAC addresses.
275bdeced75SVladimir Oltean 	 */
276bdeced75SVladimir Oltean 	ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_LEARNAUTO |
277bdeced75SVladimir Oltean 			 ANA_PORT_PORT_CFG_RECV_ENA |
278bdeced75SVladimir Oltean 			 ANA_PORT_PORT_CFG_PORTID_VAL(port),
279bdeced75SVladimir Oltean 			 ANA_PORT_PORT_CFG, port);
280bdeced75SVladimir Oltean 
281bdeced75SVladimir Oltean 	/* Core: Enable port for frame transfer */
282bdeced75SVladimir Oltean 	ocelot_write_rix(ocelot, QSYS_SWITCH_PORT_MODE_INGRESS_DROP_MODE |
283bdeced75SVladimir Oltean 			 QSYS_SWITCH_PORT_MODE_SCH_NEXT_CFG(1) |
284bdeced75SVladimir Oltean 			 QSYS_SWITCH_PORT_MODE_PORT_ENA,
285bdeced75SVladimir Oltean 			 QSYS_SWITCH_PORT_MODE, port);
286bdeced75SVladimir Oltean }
287bdeced75SVladimir Oltean 
28856051948SVladimir Oltean static void felix_get_strings(struct dsa_switch *ds, int port,
28956051948SVladimir Oltean 			      u32 stringset, u8 *data)
29056051948SVladimir Oltean {
29156051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
29256051948SVladimir Oltean 
29356051948SVladimir Oltean 	return ocelot_get_strings(ocelot, port, stringset, data);
29456051948SVladimir Oltean }
29556051948SVladimir Oltean 
29656051948SVladimir Oltean static void felix_get_ethtool_stats(struct dsa_switch *ds, int port, u64 *data)
29756051948SVladimir Oltean {
29856051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
29956051948SVladimir Oltean 
30056051948SVladimir Oltean 	ocelot_get_ethtool_stats(ocelot, port, data);
30156051948SVladimir Oltean }
30256051948SVladimir Oltean 
30356051948SVladimir Oltean static int felix_get_sset_count(struct dsa_switch *ds, int port, int sset)
30456051948SVladimir Oltean {
30556051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
30656051948SVladimir Oltean 
30756051948SVladimir Oltean 	return ocelot_get_sset_count(ocelot, port, sset);
30856051948SVladimir Oltean }
30956051948SVladimir Oltean 
31056051948SVladimir Oltean static int felix_get_ts_info(struct dsa_switch *ds, int port,
31156051948SVladimir Oltean 			     struct ethtool_ts_info *info)
31256051948SVladimir Oltean {
31356051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
31456051948SVladimir Oltean 
31556051948SVladimir Oltean 	return ocelot_get_ts_info(ocelot, port, info);
31656051948SVladimir Oltean }
31756051948SVladimir Oltean 
318bdeced75SVladimir Oltean static int felix_parse_ports_node(struct felix *felix,
319bdeced75SVladimir Oltean 				  struct device_node *ports_node,
320bdeced75SVladimir Oltean 				  phy_interface_t *port_phy_modes)
321bdeced75SVladimir Oltean {
322bdeced75SVladimir Oltean 	struct ocelot *ocelot = &felix->ocelot;
323bdeced75SVladimir Oltean 	struct device *dev = felix->ocelot.dev;
324bdeced75SVladimir Oltean 	struct device_node *child;
325bdeced75SVladimir Oltean 
326*37fe45adSVladimir Oltean 	for_each_available_child_of_node(ports_node, child) {
327bdeced75SVladimir Oltean 		phy_interface_t phy_mode;
328bdeced75SVladimir Oltean 		u32 port;
329bdeced75SVladimir Oltean 		int err;
330bdeced75SVladimir Oltean 
331bdeced75SVladimir Oltean 		/* Get switch port number from DT */
332bdeced75SVladimir Oltean 		if (of_property_read_u32(child, "reg", &port) < 0) {
333bdeced75SVladimir Oltean 			dev_err(dev, "Port number not defined in device tree "
334bdeced75SVladimir Oltean 				"(property \"reg\")\n");
335bdeced75SVladimir Oltean 			of_node_put(child);
336bdeced75SVladimir Oltean 			return -ENODEV;
337bdeced75SVladimir Oltean 		}
338bdeced75SVladimir Oltean 
339bdeced75SVladimir Oltean 		/* Get PHY mode from DT */
340bdeced75SVladimir Oltean 		err = of_get_phy_mode(child, &phy_mode);
341bdeced75SVladimir Oltean 		if (err) {
342bdeced75SVladimir Oltean 			dev_err(dev, "Failed to read phy-mode or "
343bdeced75SVladimir Oltean 				"phy-interface-type property for port %d\n",
344bdeced75SVladimir Oltean 				port);
345bdeced75SVladimir Oltean 			of_node_put(child);
346bdeced75SVladimir Oltean 			return -ENODEV;
347bdeced75SVladimir Oltean 		}
348bdeced75SVladimir Oltean 
349bdeced75SVladimir Oltean 		err = felix->info->prevalidate_phy_mode(ocelot, port, phy_mode);
350bdeced75SVladimir Oltean 		if (err < 0) {
351bdeced75SVladimir Oltean 			dev_err(dev, "Unsupported PHY mode %s on port %d\n",
352bdeced75SVladimir Oltean 				phy_modes(phy_mode), port);
353bdeced75SVladimir Oltean 			return err;
354bdeced75SVladimir Oltean 		}
355bdeced75SVladimir Oltean 
356bdeced75SVladimir Oltean 		port_phy_modes[port] = phy_mode;
357bdeced75SVladimir Oltean 	}
358bdeced75SVladimir Oltean 
359bdeced75SVladimir Oltean 	return 0;
360bdeced75SVladimir Oltean }
361bdeced75SVladimir Oltean 
362bdeced75SVladimir Oltean static int felix_parse_dt(struct felix *felix, phy_interface_t *port_phy_modes)
363bdeced75SVladimir Oltean {
364bdeced75SVladimir Oltean 	struct device *dev = felix->ocelot.dev;
365bdeced75SVladimir Oltean 	struct device_node *switch_node;
366bdeced75SVladimir Oltean 	struct device_node *ports_node;
367bdeced75SVladimir Oltean 	int err;
368bdeced75SVladimir Oltean 
369bdeced75SVladimir Oltean 	switch_node = dev->of_node;
370bdeced75SVladimir Oltean 
371bdeced75SVladimir Oltean 	ports_node = of_get_child_by_name(switch_node, "ports");
372bdeced75SVladimir Oltean 	if (!ports_node) {
373bdeced75SVladimir Oltean 		dev_err(dev, "Incorrect bindings: absent \"ports\" node\n");
374bdeced75SVladimir Oltean 		return -ENODEV;
375bdeced75SVladimir Oltean 	}
376bdeced75SVladimir Oltean 
377bdeced75SVladimir Oltean 	err = felix_parse_ports_node(felix, ports_node, port_phy_modes);
378bdeced75SVladimir Oltean 	of_node_put(ports_node);
379bdeced75SVladimir Oltean 
380bdeced75SVladimir Oltean 	return err;
381bdeced75SVladimir Oltean }
382bdeced75SVladimir Oltean 
38356051948SVladimir Oltean static int felix_init_structs(struct felix *felix, int num_phys_ports)
38456051948SVladimir Oltean {
38556051948SVladimir Oltean 	struct ocelot *ocelot = &felix->ocelot;
386bdeced75SVladimir Oltean 	phy_interface_t *port_phy_modes;
387bdeced75SVladimir Oltean 	resource_size_t switch_base;
38856051948SVladimir Oltean 	int port, i, err;
38956051948SVladimir Oltean 
39056051948SVladimir Oltean 	ocelot->num_phys_ports = num_phys_ports;
39156051948SVladimir Oltean 	ocelot->ports = devm_kcalloc(ocelot->dev, num_phys_ports,
39256051948SVladimir Oltean 				     sizeof(struct ocelot_port *), GFP_KERNEL);
39356051948SVladimir Oltean 	if (!ocelot->ports)
39456051948SVladimir Oltean 		return -ENOMEM;
39556051948SVladimir Oltean 
39656051948SVladimir Oltean 	ocelot->map		= felix->info->map;
39756051948SVladimir Oltean 	ocelot->stats_layout	= felix->info->stats_layout;
39856051948SVladimir Oltean 	ocelot->num_stats	= felix->info->num_stats;
39956051948SVladimir Oltean 	ocelot->shared_queue_sz	= felix->info->shared_queue_sz;
40056051948SVladimir Oltean 	ocelot->ops		= felix->info->ops;
40156051948SVladimir Oltean 
402bdeced75SVladimir Oltean 	port_phy_modes = kcalloc(num_phys_ports, sizeof(phy_interface_t),
403bdeced75SVladimir Oltean 				 GFP_KERNEL);
404bdeced75SVladimir Oltean 	if (!port_phy_modes)
405bdeced75SVladimir Oltean 		return -ENOMEM;
406bdeced75SVladimir Oltean 
407bdeced75SVladimir Oltean 	err = felix_parse_dt(felix, port_phy_modes);
408bdeced75SVladimir Oltean 	if (err) {
409bdeced75SVladimir Oltean 		kfree(port_phy_modes);
410bdeced75SVladimir Oltean 		return err;
411bdeced75SVladimir Oltean 	}
412bdeced75SVladimir Oltean 
413bdeced75SVladimir Oltean 	switch_base = pci_resource_start(felix->pdev,
414bdeced75SVladimir Oltean 					 felix->info->switch_pci_bar);
41556051948SVladimir Oltean 
41656051948SVladimir Oltean 	for (i = 0; i < TARGET_MAX; i++) {
41756051948SVladimir Oltean 		struct regmap *target;
41856051948SVladimir Oltean 		struct resource *res;
41956051948SVladimir Oltean 
42056051948SVladimir Oltean 		if (!felix->info->target_io_res[i].name)
42156051948SVladimir Oltean 			continue;
42256051948SVladimir Oltean 
42356051948SVladimir Oltean 		res = &felix->info->target_io_res[i];
42456051948SVladimir Oltean 		res->flags = IORESOURCE_MEM;
425bdeced75SVladimir Oltean 		res->start += switch_base;
426bdeced75SVladimir Oltean 		res->end += switch_base;
42756051948SVladimir Oltean 
42856051948SVladimir Oltean 		target = ocelot_regmap_init(ocelot, res);
42956051948SVladimir Oltean 		if (IS_ERR(target)) {
43056051948SVladimir Oltean 			dev_err(ocelot->dev,
43156051948SVladimir Oltean 				"Failed to map device memory space\n");
432bdeced75SVladimir Oltean 			kfree(port_phy_modes);
43356051948SVladimir Oltean 			return PTR_ERR(target);
43456051948SVladimir Oltean 		}
43556051948SVladimir Oltean 
43656051948SVladimir Oltean 		ocelot->targets[i] = target;
43756051948SVladimir Oltean 	}
43856051948SVladimir Oltean 
43956051948SVladimir Oltean 	err = ocelot_regfields_init(ocelot, felix->info->regfields);
44056051948SVladimir Oltean 	if (err) {
44156051948SVladimir Oltean 		dev_err(ocelot->dev, "failed to init reg fields map\n");
442bdeced75SVladimir Oltean 		kfree(port_phy_modes);
44356051948SVladimir Oltean 		return err;
44456051948SVladimir Oltean 	}
44556051948SVladimir Oltean 
44656051948SVladimir Oltean 	for (port = 0; port < num_phys_ports; port++) {
44756051948SVladimir Oltean 		struct ocelot_port *ocelot_port;
44856051948SVladimir Oltean 		void __iomem *port_regs;
44956051948SVladimir Oltean 		struct resource *res;
45056051948SVladimir Oltean 
45156051948SVladimir Oltean 		ocelot_port = devm_kzalloc(ocelot->dev,
45256051948SVladimir Oltean 					   sizeof(struct ocelot_port),
45356051948SVladimir Oltean 					   GFP_KERNEL);
45456051948SVladimir Oltean 		if (!ocelot_port) {
45556051948SVladimir Oltean 			dev_err(ocelot->dev,
45656051948SVladimir Oltean 				"failed to allocate port memory\n");
457bdeced75SVladimir Oltean 			kfree(port_phy_modes);
45856051948SVladimir Oltean 			return -ENOMEM;
45956051948SVladimir Oltean 		}
46056051948SVladimir Oltean 
46156051948SVladimir Oltean 		res = &felix->info->port_io_res[port];
46256051948SVladimir Oltean 		res->flags = IORESOURCE_MEM;
463bdeced75SVladimir Oltean 		res->start += switch_base;
464bdeced75SVladimir Oltean 		res->end += switch_base;
46556051948SVladimir Oltean 
46656051948SVladimir Oltean 		port_regs = devm_ioremap_resource(ocelot->dev, res);
46756051948SVladimir Oltean 		if (IS_ERR(port_regs)) {
46856051948SVladimir Oltean 			dev_err(ocelot->dev,
46956051948SVladimir Oltean 				"failed to map registers for port %d\n", port);
470bdeced75SVladimir Oltean 			kfree(port_phy_modes);
47156051948SVladimir Oltean 			return PTR_ERR(port_regs);
47256051948SVladimir Oltean 		}
47356051948SVladimir Oltean 
474bdeced75SVladimir Oltean 		ocelot_port->phy_mode = port_phy_modes[port];
47556051948SVladimir Oltean 		ocelot_port->ocelot = ocelot;
47656051948SVladimir Oltean 		ocelot_port->regs = port_regs;
47756051948SVladimir Oltean 		ocelot->ports[port] = ocelot_port;
47856051948SVladimir Oltean 	}
47956051948SVladimir Oltean 
480bdeced75SVladimir Oltean 	kfree(port_phy_modes);
481bdeced75SVladimir Oltean 
482bdeced75SVladimir Oltean 	if (felix->info->mdio_bus_alloc) {
483bdeced75SVladimir Oltean 		err = felix->info->mdio_bus_alloc(ocelot);
484bdeced75SVladimir Oltean 		if (err < 0)
485bdeced75SVladimir Oltean 			return err;
486bdeced75SVladimir Oltean 	}
487bdeced75SVladimir Oltean 
48856051948SVladimir Oltean 	return 0;
48956051948SVladimir Oltean }
49056051948SVladimir Oltean 
49156051948SVladimir Oltean /* Hardware initialization done here so that we can allocate structures with
49256051948SVladimir Oltean  * devm without fear of dsa_register_switch returning -EPROBE_DEFER and causing
49356051948SVladimir Oltean  * us to allocate structures twice (leak memory) and map PCI memory twice
49456051948SVladimir Oltean  * (which will not work).
49556051948SVladimir Oltean  */
49656051948SVladimir Oltean static int felix_setup(struct dsa_switch *ds)
49756051948SVladimir Oltean {
49856051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
49956051948SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
50056051948SVladimir Oltean 	int port, err;
50156051948SVladimir Oltean 
50256051948SVladimir Oltean 	err = felix_init_structs(felix, ds->num_ports);
50356051948SVladimir Oltean 	if (err)
50456051948SVladimir Oltean 		return err;
50556051948SVladimir Oltean 
50656051948SVladimir Oltean 	ocelot_init(ocelot);
50756051948SVladimir Oltean 
50856051948SVladimir Oltean 	for (port = 0; port < ds->num_ports; port++) {
50956051948SVladimir Oltean 		ocelot_init_port(ocelot, port);
51056051948SVladimir Oltean 
511b8fc7177SVladimir Oltean 		if (dsa_is_cpu_port(ds, port))
51256051948SVladimir Oltean 			ocelot_set_cpu_port(ocelot, port,
51356051948SVladimir Oltean 					    OCELOT_TAG_PREFIX_NONE,
51456051948SVladimir Oltean 					    OCELOT_TAG_PREFIX_LONG);
51556051948SVladimir Oltean 	}
51656051948SVladimir Oltean 
517bdeced75SVladimir Oltean 	/* It looks like the MAC/PCS interrupt register - PM0_IEVENT (0x8040)
518bdeced75SVladimir Oltean 	 * isn't instantiated for the Felix PF.
519bdeced75SVladimir Oltean 	 * In-band AN may take a few ms to complete, so we need to poll.
520bdeced75SVladimir Oltean 	 */
521bdeced75SVladimir Oltean 	ds->pcs_poll = true;
522bdeced75SVladimir Oltean 
52356051948SVladimir Oltean 	return 0;
52456051948SVladimir Oltean }
52556051948SVladimir Oltean 
52656051948SVladimir Oltean static void felix_teardown(struct dsa_switch *ds)
52756051948SVladimir Oltean {
52856051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
529bdeced75SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
530bdeced75SVladimir Oltean 
531bdeced75SVladimir Oltean 	if (felix->info->mdio_bus_free)
532bdeced75SVladimir Oltean 		felix->info->mdio_bus_free(ocelot);
53356051948SVladimir Oltean 
53456051948SVladimir Oltean 	/* stop workqueue thread */
53556051948SVladimir Oltean 	ocelot_deinit(ocelot);
53656051948SVladimir Oltean }
53756051948SVladimir Oltean 
538c0bcf537SYangbo Lu static int felix_hwtstamp_get(struct dsa_switch *ds, int port,
539c0bcf537SYangbo Lu 			      struct ifreq *ifr)
540c0bcf537SYangbo Lu {
541c0bcf537SYangbo Lu 	struct ocelot *ocelot = ds->priv;
542c0bcf537SYangbo Lu 
543c0bcf537SYangbo Lu 	return ocelot_hwstamp_get(ocelot, port, ifr);
544c0bcf537SYangbo Lu }
545c0bcf537SYangbo Lu 
546c0bcf537SYangbo Lu static int felix_hwtstamp_set(struct dsa_switch *ds, int port,
547c0bcf537SYangbo Lu 			      struct ifreq *ifr)
548c0bcf537SYangbo Lu {
549c0bcf537SYangbo Lu 	struct ocelot *ocelot = ds->priv;
550c0bcf537SYangbo Lu 
551c0bcf537SYangbo Lu 	return ocelot_hwstamp_set(ocelot, port, ifr);
552c0bcf537SYangbo Lu }
553c0bcf537SYangbo Lu 
554c0bcf537SYangbo Lu static bool felix_rxtstamp(struct dsa_switch *ds, int port,
555c0bcf537SYangbo Lu 			   struct sk_buff *skb, unsigned int type)
556c0bcf537SYangbo Lu {
557c0bcf537SYangbo Lu 	struct skb_shared_hwtstamps *shhwtstamps;
558c0bcf537SYangbo Lu 	struct ocelot *ocelot = ds->priv;
559c0bcf537SYangbo Lu 	u8 *extraction = skb->data - ETH_HLEN - OCELOT_TAG_LEN;
560c0bcf537SYangbo Lu 	u32 tstamp_lo, tstamp_hi;
561c0bcf537SYangbo Lu 	struct timespec64 ts;
562c0bcf537SYangbo Lu 	u64 tstamp, val;
563c0bcf537SYangbo Lu 
564c0bcf537SYangbo Lu 	ocelot_ptp_gettime64(&ocelot->ptp_info, &ts);
565c0bcf537SYangbo Lu 	tstamp = ktime_set(ts.tv_sec, ts.tv_nsec);
566c0bcf537SYangbo Lu 
567c0bcf537SYangbo Lu 	packing(extraction, &val,  116, 85, OCELOT_TAG_LEN, UNPACK, 0);
568c0bcf537SYangbo Lu 	tstamp_lo = (u32)val;
569c0bcf537SYangbo Lu 
570c0bcf537SYangbo Lu 	tstamp_hi = tstamp >> 32;
571c0bcf537SYangbo Lu 	if ((tstamp & 0xffffffff) < tstamp_lo)
572c0bcf537SYangbo Lu 		tstamp_hi--;
573c0bcf537SYangbo Lu 
574c0bcf537SYangbo Lu 	tstamp = ((u64)tstamp_hi << 32) | tstamp_lo;
575c0bcf537SYangbo Lu 
576c0bcf537SYangbo Lu 	shhwtstamps = skb_hwtstamps(skb);
577c0bcf537SYangbo Lu 	memset(shhwtstamps, 0, sizeof(struct skb_shared_hwtstamps));
578c0bcf537SYangbo Lu 	shhwtstamps->hwtstamp = tstamp;
579c0bcf537SYangbo Lu 	return false;
580c0bcf537SYangbo Lu }
581c0bcf537SYangbo Lu 
5823243e04aSChen Wandun static bool felix_txtstamp(struct dsa_switch *ds, int port,
583c0bcf537SYangbo Lu 			   struct sk_buff *clone, unsigned int type)
584c0bcf537SYangbo Lu {
585c0bcf537SYangbo Lu 	struct ocelot *ocelot = ds->priv;
586c0bcf537SYangbo Lu 	struct ocelot_port *ocelot_port = ocelot->ports[port];
587c0bcf537SYangbo Lu 
588c0bcf537SYangbo Lu 	if (!ocelot_port_add_txtstamp_skb(ocelot_port, clone))
589c0bcf537SYangbo Lu 		return true;
590c0bcf537SYangbo Lu 
591c0bcf537SYangbo Lu 	return false;
592c0bcf537SYangbo Lu }
593c0bcf537SYangbo Lu 
59456051948SVladimir Oltean static const struct dsa_switch_ops felix_switch_ops = {
59556051948SVladimir Oltean 	.get_tag_protocol	= felix_get_tag_protocol,
59656051948SVladimir Oltean 	.setup			= felix_setup,
59756051948SVladimir Oltean 	.teardown		= felix_teardown,
59856051948SVladimir Oltean 	.set_ageing_time	= felix_set_ageing_time,
59956051948SVladimir Oltean 	.get_strings		= felix_get_strings,
60056051948SVladimir Oltean 	.get_ethtool_stats	= felix_get_ethtool_stats,
60156051948SVladimir Oltean 	.get_sset_count		= felix_get_sset_count,
60256051948SVladimir Oltean 	.get_ts_info		= felix_get_ts_info,
603bdeced75SVladimir Oltean 	.phylink_validate	= felix_phylink_validate,
604bdeced75SVladimir Oltean 	.phylink_mac_link_state	= felix_phylink_mac_pcs_get_state,
605bdeced75SVladimir Oltean 	.phylink_mac_config	= felix_phylink_mac_config,
606bdeced75SVladimir Oltean 	.phylink_mac_an_restart	= felix_phylink_mac_an_restart,
607bdeced75SVladimir Oltean 	.phylink_mac_link_down	= felix_phylink_mac_link_down,
608bdeced75SVladimir Oltean 	.phylink_mac_link_up	= felix_phylink_mac_link_up,
60956051948SVladimir Oltean 	.port_enable		= felix_port_enable,
61056051948SVladimir Oltean 	.port_disable		= felix_port_disable,
61156051948SVladimir Oltean 	.port_fdb_dump		= felix_fdb_dump,
61256051948SVladimir Oltean 	.port_fdb_add		= felix_fdb_add,
61356051948SVladimir Oltean 	.port_fdb_del		= felix_fdb_del,
61456051948SVladimir Oltean 	.port_bridge_join	= felix_bridge_join,
61556051948SVladimir Oltean 	.port_bridge_leave	= felix_bridge_leave,
61656051948SVladimir Oltean 	.port_stp_state_set	= felix_bridge_stp_state_set,
61756051948SVladimir Oltean 	.port_vlan_prepare	= felix_vlan_prepare,
61856051948SVladimir Oltean 	.port_vlan_filtering	= felix_vlan_filtering,
61956051948SVladimir Oltean 	.port_vlan_add		= felix_vlan_add,
62056051948SVladimir Oltean 	.port_vlan_del		= felix_vlan_del,
621c0bcf537SYangbo Lu 	.port_hwtstamp_get	= felix_hwtstamp_get,
622c0bcf537SYangbo Lu 	.port_hwtstamp_set	= felix_hwtstamp_set,
623c0bcf537SYangbo Lu 	.port_rxtstamp		= felix_rxtstamp,
624c0bcf537SYangbo Lu 	.port_txtstamp		= felix_txtstamp,
62556051948SVladimir Oltean };
62656051948SVladimir Oltean 
62756051948SVladimir Oltean static struct felix_info *felix_instance_tbl[] = {
62856051948SVladimir Oltean 	[FELIX_INSTANCE_VSC9959] = &felix_info_vsc9959,
62956051948SVladimir Oltean };
63056051948SVladimir Oltean 
631c0bcf537SYangbo Lu static irqreturn_t felix_irq_handler(int irq, void *data)
632c0bcf537SYangbo Lu {
633c0bcf537SYangbo Lu 	struct ocelot *ocelot = (struct ocelot *)data;
634c0bcf537SYangbo Lu 
635c0bcf537SYangbo Lu 	/* The INTB interrupt is used for both PTP TX timestamp interrupt
636c0bcf537SYangbo Lu 	 * and preemption status change interrupt on each port.
637c0bcf537SYangbo Lu 	 *
638c0bcf537SYangbo Lu 	 * - Get txtstamp if have
639c0bcf537SYangbo Lu 	 * - TODO: handle preemption. Without handling it, driver may get
640c0bcf537SYangbo Lu 	 *   interrupt storm.
641c0bcf537SYangbo Lu 	 */
642c0bcf537SYangbo Lu 
643c0bcf537SYangbo Lu 	ocelot_get_txtstamp(ocelot);
644c0bcf537SYangbo Lu 
645c0bcf537SYangbo Lu 	return IRQ_HANDLED;
646c0bcf537SYangbo Lu }
647c0bcf537SYangbo Lu 
64856051948SVladimir Oltean static int felix_pci_probe(struct pci_dev *pdev,
64956051948SVladimir Oltean 			   const struct pci_device_id *id)
65056051948SVladimir Oltean {
65156051948SVladimir Oltean 	enum felix_instance instance = id->driver_data;
65256051948SVladimir Oltean 	struct dsa_switch *ds;
65356051948SVladimir Oltean 	struct ocelot *ocelot;
65456051948SVladimir Oltean 	struct felix *felix;
65556051948SVladimir Oltean 	int err;
65656051948SVladimir Oltean 
65756051948SVladimir Oltean 	err = pci_enable_device(pdev);
65856051948SVladimir Oltean 	if (err) {
65956051948SVladimir Oltean 		dev_err(&pdev->dev, "device enable failed\n");
66056051948SVladimir Oltean 		goto err_pci_enable;
66156051948SVladimir Oltean 	}
66256051948SVladimir Oltean 
66356051948SVladimir Oltean 	/* set up for high or low dma */
66456051948SVladimir Oltean 	err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
66556051948SVladimir Oltean 	if (err) {
66656051948SVladimir Oltean 		err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
66756051948SVladimir Oltean 		if (err) {
66856051948SVladimir Oltean 			dev_err(&pdev->dev,
66956051948SVladimir Oltean 				"DMA configuration failed: 0x%x\n", err);
67056051948SVladimir Oltean 			goto err_dma;
67156051948SVladimir Oltean 		}
67256051948SVladimir Oltean 	}
67356051948SVladimir Oltean 
67456051948SVladimir Oltean 	felix = kzalloc(sizeof(struct felix), GFP_KERNEL);
67556051948SVladimir Oltean 	if (!felix) {
67656051948SVladimir Oltean 		err = -ENOMEM;
67756051948SVladimir Oltean 		dev_err(&pdev->dev, "Failed to allocate driver memory\n");
67856051948SVladimir Oltean 		goto err_alloc_felix;
67956051948SVladimir Oltean 	}
68056051948SVladimir Oltean 
68156051948SVladimir Oltean 	pci_set_drvdata(pdev, felix);
68256051948SVladimir Oltean 	ocelot = &felix->ocelot;
68356051948SVladimir Oltean 	ocelot->dev = &pdev->dev;
68456051948SVladimir Oltean 	felix->pdev = pdev;
68556051948SVladimir Oltean 	felix->info = felix_instance_tbl[instance];
68656051948SVladimir Oltean 
68756051948SVladimir Oltean 	pci_set_master(pdev);
68856051948SVladimir Oltean 
689c0bcf537SYangbo Lu 	err = devm_request_threaded_irq(&pdev->dev, pdev->irq, NULL,
690c0bcf537SYangbo Lu 					&felix_irq_handler, IRQF_ONESHOT,
691c0bcf537SYangbo Lu 					"felix-intb", ocelot);
692c0bcf537SYangbo Lu 	if (err) {
693c0bcf537SYangbo Lu 		dev_err(&pdev->dev, "Failed to request irq\n");
694c0bcf537SYangbo Lu 		goto err_alloc_irq;
695c0bcf537SYangbo Lu 	}
696c0bcf537SYangbo Lu 
697c0bcf537SYangbo Lu 	ocelot->ptp = 1;
698c0bcf537SYangbo Lu 
69956051948SVladimir Oltean 	ds = kzalloc(sizeof(struct dsa_switch), GFP_KERNEL);
70056051948SVladimir Oltean 	if (!ds) {
70156051948SVladimir Oltean 		err = -ENOMEM;
70256051948SVladimir Oltean 		dev_err(&pdev->dev, "Failed to allocate DSA switch\n");
70356051948SVladimir Oltean 		goto err_alloc_ds;
70456051948SVladimir Oltean 	}
70556051948SVladimir Oltean 
70656051948SVladimir Oltean 	ds->dev = &pdev->dev;
70756051948SVladimir Oltean 	ds->num_ports = felix->info->num_ports;
70856051948SVladimir Oltean 	ds->ops = &felix_switch_ops;
70956051948SVladimir Oltean 	ds->priv = ocelot;
71056051948SVladimir Oltean 	felix->ds = ds;
71156051948SVladimir Oltean 
71256051948SVladimir Oltean 	err = dsa_register_switch(ds);
71356051948SVladimir Oltean 	if (err) {
71456051948SVladimir Oltean 		dev_err(&pdev->dev, "Failed to register DSA switch: %d\n", err);
71556051948SVladimir Oltean 		goto err_register_ds;
71656051948SVladimir Oltean 	}
71756051948SVladimir Oltean 
71856051948SVladimir Oltean 	return 0;
71956051948SVladimir Oltean 
72056051948SVladimir Oltean err_register_ds:
72156051948SVladimir Oltean 	kfree(ds);
72256051948SVladimir Oltean err_alloc_ds:
723c0bcf537SYangbo Lu err_alloc_irq:
72456051948SVladimir Oltean err_alloc_felix:
72556051948SVladimir Oltean 	kfree(felix);
72656051948SVladimir Oltean err_dma:
72756051948SVladimir Oltean 	pci_disable_device(pdev);
72856051948SVladimir Oltean err_pci_enable:
72956051948SVladimir Oltean 	return err;
73056051948SVladimir Oltean }
73156051948SVladimir Oltean 
73256051948SVladimir Oltean static void felix_pci_remove(struct pci_dev *pdev)
73356051948SVladimir Oltean {
73456051948SVladimir Oltean 	struct felix *felix;
73556051948SVladimir Oltean 
73656051948SVladimir Oltean 	felix = pci_get_drvdata(pdev);
73756051948SVladimir Oltean 
73856051948SVladimir Oltean 	dsa_unregister_switch(felix->ds);
73956051948SVladimir Oltean 
74056051948SVladimir Oltean 	kfree(felix->ds);
74156051948SVladimir Oltean 	kfree(felix);
74256051948SVladimir Oltean 
74356051948SVladimir Oltean 	pci_disable_device(pdev);
74456051948SVladimir Oltean }
74556051948SVladimir Oltean 
74656051948SVladimir Oltean static struct pci_device_id felix_ids[] = {
74756051948SVladimir Oltean 	{
74856051948SVladimir Oltean 		/* NXP LS1028A */
74956051948SVladimir Oltean 		PCI_DEVICE(PCI_VENDOR_ID_FREESCALE, 0xEEF0),
75056051948SVladimir Oltean 		.driver_data = FELIX_INSTANCE_VSC9959,
75156051948SVladimir Oltean 	},
75256051948SVladimir Oltean 	{ 0, }
75356051948SVladimir Oltean };
75456051948SVladimir Oltean MODULE_DEVICE_TABLE(pci, felix_ids);
75556051948SVladimir Oltean 
75656051948SVladimir Oltean static struct pci_driver felix_pci_driver = {
75756051948SVladimir Oltean 	.name		= KBUILD_MODNAME,
75856051948SVladimir Oltean 	.id_table	= felix_ids,
75956051948SVladimir Oltean 	.probe		= felix_pci_probe,
76056051948SVladimir Oltean 	.remove		= felix_pci_remove,
76156051948SVladimir Oltean };
76256051948SVladimir Oltean 
76356051948SVladimir Oltean module_pci_driver(felix_pci_driver);
76456051948SVladimir Oltean 
76556051948SVladimir Oltean MODULE_DESCRIPTION("Felix Switch driver");
76656051948SVladimir Oltean MODULE_LICENSE("GPL v2");
767