xref: /openbmc/linux/drivers/net/dsa/ocelot/felix.c (revision 183be6f967fe37c3154bfac39e913c3bafe89d1b)
156051948SVladimir Oltean // SPDX-License-Identifier: GPL-2.0
256051948SVladimir Oltean /* Copyright 2019 NXP Semiconductors
356051948SVladimir Oltean  */
456051948SVladimir Oltean #include <uapi/linux/if_bridge.h>
507d985eeSVladimir Oltean #include <soc/mscc/ocelot_vcap.h>
6bdeced75SVladimir Oltean #include <soc/mscc/ocelot_qsys.h>
7bdeced75SVladimir Oltean #include <soc/mscc/ocelot_sys.h>
8bdeced75SVladimir Oltean #include <soc/mscc/ocelot_dev.h>
9bdeced75SVladimir Oltean #include <soc/mscc/ocelot_ana.h>
1056051948SVladimir Oltean #include <soc/mscc/ocelot.h>
11c0bcf537SYangbo Lu #include <linux/packing.h>
1256051948SVladimir Oltean #include <linux/module.h>
13bdeced75SVladimir Oltean #include <linux/of_net.h>
1456051948SVladimir Oltean #include <linux/pci.h>
1556051948SVladimir Oltean #include <linux/of.h>
16fc411eaaSVladimir Oltean #include <net/pkt_sched.h>
1756051948SVladimir Oltean #include <net/dsa.h>
1856051948SVladimir Oltean #include "felix.h"
1956051948SVladimir Oltean 
2056051948SVladimir Oltean static enum dsa_tag_protocol felix_get_tag_protocol(struct dsa_switch *ds,
214d776482SFlorian Fainelli 						    int port,
224d776482SFlorian Fainelli 						    enum dsa_tag_protocol mp)
2356051948SVladimir Oltean {
2456051948SVladimir Oltean 	return DSA_TAG_PROTO_OCELOT;
2556051948SVladimir Oltean }
2656051948SVladimir Oltean 
2756051948SVladimir Oltean static int felix_set_ageing_time(struct dsa_switch *ds,
2856051948SVladimir Oltean 				 unsigned int ageing_time)
2956051948SVladimir Oltean {
3056051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
3156051948SVladimir Oltean 
3256051948SVladimir Oltean 	ocelot_set_ageing_time(ocelot, ageing_time);
3356051948SVladimir Oltean 
3456051948SVladimir Oltean 	return 0;
3556051948SVladimir Oltean }
3656051948SVladimir Oltean 
3756051948SVladimir Oltean static int felix_fdb_dump(struct dsa_switch *ds, int port,
3856051948SVladimir Oltean 			  dsa_fdb_dump_cb_t *cb, void *data)
3956051948SVladimir Oltean {
4056051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
4156051948SVladimir Oltean 
4256051948SVladimir Oltean 	return ocelot_fdb_dump(ocelot, port, cb, data);
4356051948SVladimir Oltean }
4456051948SVladimir Oltean 
4556051948SVladimir Oltean static int felix_fdb_add(struct dsa_switch *ds, int port,
4656051948SVladimir Oltean 			 const unsigned char *addr, u16 vid)
4756051948SVladimir Oltean {
4856051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
4956051948SVladimir Oltean 
5087b0f983SVladimir Oltean 	return ocelot_fdb_add(ocelot, port, addr, vid);
5156051948SVladimir Oltean }
5256051948SVladimir Oltean 
5356051948SVladimir Oltean static int felix_fdb_del(struct dsa_switch *ds, int port,
5456051948SVladimir Oltean 			 const unsigned char *addr, u16 vid)
5556051948SVladimir Oltean {
5656051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
5756051948SVladimir Oltean 
5856051948SVladimir Oltean 	return ocelot_fdb_del(ocelot, port, addr, vid);
5956051948SVladimir Oltean }
6056051948SVladimir Oltean 
6156051948SVladimir Oltean static void felix_bridge_stp_state_set(struct dsa_switch *ds, int port,
6256051948SVladimir Oltean 				       u8 state)
6356051948SVladimir Oltean {
6456051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
6556051948SVladimir Oltean 
6656051948SVladimir Oltean 	return ocelot_bridge_stp_state_set(ocelot, port, state);
6756051948SVladimir Oltean }
6856051948SVladimir Oltean 
6956051948SVladimir Oltean static int felix_bridge_join(struct dsa_switch *ds, int port,
7056051948SVladimir Oltean 			     struct net_device *br)
7156051948SVladimir Oltean {
7256051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
7356051948SVladimir Oltean 
7456051948SVladimir Oltean 	return ocelot_port_bridge_join(ocelot, port, br);
7556051948SVladimir Oltean }
7656051948SVladimir Oltean 
7756051948SVladimir Oltean static void felix_bridge_leave(struct dsa_switch *ds, int port,
7856051948SVladimir Oltean 			       struct net_device *br)
7956051948SVladimir Oltean {
8056051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
8156051948SVladimir Oltean 
8256051948SVladimir Oltean 	ocelot_port_bridge_leave(ocelot, port, br);
8356051948SVladimir Oltean }
8456051948SVladimir Oltean 
8556051948SVladimir Oltean /* This callback needs to be present */
8656051948SVladimir Oltean static int felix_vlan_prepare(struct dsa_switch *ds, int port,
8756051948SVladimir Oltean 			      const struct switchdev_obj_port_vlan *vlan)
8856051948SVladimir Oltean {
8956051948SVladimir Oltean 	return 0;
9056051948SVladimir Oltean }
9156051948SVladimir Oltean 
9256051948SVladimir Oltean static int felix_vlan_filtering(struct dsa_switch *ds, int port, bool enabled)
9356051948SVladimir Oltean {
9456051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
9556051948SVladimir Oltean 
9656051948SVladimir Oltean 	ocelot_port_vlan_filtering(ocelot, port, enabled);
9756051948SVladimir Oltean 
9856051948SVladimir Oltean 	return 0;
9956051948SVladimir Oltean }
10056051948SVladimir Oltean 
10156051948SVladimir Oltean static void felix_vlan_add(struct dsa_switch *ds, int port,
10256051948SVladimir Oltean 			   const struct switchdev_obj_port_vlan *vlan)
10356051948SVladimir Oltean {
10456051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
105*183be6f9SVladimir Oltean 	u16 flags = vlan->flags;
10656051948SVladimir Oltean 	u16 vid;
10756051948SVladimir Oltean 	int err;
10856051948SVladimir Oltean 
109*183be6f9SVladimir Oltean 	if (dsa_is_cpu_port(ds, port))
110*183be6f9SVladimir Oltean 		flags &= ~BRIDGE_VLAN_INFO_UNTAGGED;
111*183be6f9SVladimir Oltean 
11256051948SVladimir Oltean 	for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) {
11356051948SVladimir Oltean 		err = ocelot_vlan_add(ocelot, port, vid,
114*183be6f9SVladimir Oltean 				      flags & BRIDGE_VLAN_INFO_PVID,
115*183be6f9SVladimir Oltean 				      flags & BRIDGE_VLAN_INFO_UNTAGGED);
11656051948SVladimir Oltean 		if (err) {
11756051948SVladimir Oltean 			dev_err(ds->dev, "Failed to add VLAN %d to port %d: %d\n",
11856051948SVladimir Oltean 				vid, port, err);
11956051948SVladimir Oltean 			return;
12056051948SVladimir Oltean 		}
12156051948SVladimir Oltean 	}
12256051948SVladimir Oltean }
12356051948SVladimir Oltean 
12456051948SVladimir Oltean static int felix_vlan_del(struct dsa_switch *ds, int port,
12556051948SVladimir Oltean 			  const struct switchdev_obj_port_vlan *vlan)
12656051948SVladimir Oltean {
12756051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
12856051948SVladimir Oltean 	u16 vid;
12956051948SVladimir Oltean 	int err;
13056051948SVladimir Oltean 
13156051948SVladimir Oltean 	for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) {
13256051948SVladimir Oltean 		err = ocelot_vlan_del(ocelot, port, vid);
13356051948SVladimir Oltean 		if (err) {
13456051948SVladimir Oltean 			dev_err(ds->dev, "Failed to remove VLAN %d from port %d: %d\n",
13556051948SVladimir Oltean 				vid, port, err);
13656051948SVladimir Oltean 			return err;
13756051948SVladimir Oltean 		}
13856051948SVladimir Oltean 	}
13956051948SVladimir Oltean 	return 0;
14056051948SVladimir Oltean }
14156051948SVladimir Oltean 
14256051948SVladimir Oltean static int felix_port_enable(struct dsa_switch *ds, int port,
14356051948SVladimir Oltean 			     struct phy_device *phy)
14456051948SVladimir Oltean {
14556051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
14656051948SVladimir Oltean 
14756051948SVladimir Oltean 	ocelot_port_enable(ocelot, port, phy);
14856051948SVladimir Oltean 
14956051948SVladimir Oltean 	return 0;
15056051948SVladimir Oltean }
15156051948SVladimir Oltean 
15256051948SVladimir Oltean static void felix_port_disable(struct dsa_switch *ds, int port)
15356051948SVladimir Oltean {
15456051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
15556051948SVladimir Oltean 
15656051948SVladimir Oltean 	return ocelot_port_disable(ocelot, port);
15756051948SVladimir Oltean }
15856051948SVladimir Oltean 
159bdeced75SVladimir Oltean static void felix_phylink_validate(struct dsa_switch *ds, int port,
160bdeced75SVladimir Oltean 				   unsigned long *supported,
161bdeced75SVladimir Oltean 				   struct phylink_link_state *state)
162bdeced75SVladimir Oltean {
163bdeced75SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
164bdeced75SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
165bdeced75SVladimir Oltean 	__ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, };
166bdeced75SVladimir Oltean 
167bdeced75SVladimir Oltean 	if (state->interface != PHY_INTERFACE_MODE_NA &&
168bdeced75SVladimir Oltean 	    state->interface != ocelot_port->phy_mode) {
169bdeced75SVladimir Oltean 		bitmap_zero(supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
170bdeced75SVladimir Oltean 		return;
171bdeced75SVladimir Oltean 	}
172bdeced75SVladimir Oltean 
173bdeced75SVladimir Oltean 	/* No half-duplex. */
174bdeced75SVladimir Oltean 	phylink_set_port_modes(mask);
175bdeced75SVladimir Oltean 	phylink_set(mask, Autoneg);
176bdeced75SVladimir Oltean 	phylink_set(mask, Pause);
177bdeced75SVladimir Oltean 	phylink_set(mask, Asym_Pause);
178bdeced75SVladimir Oltean 	phylink_set(mask, 10baseT_Full);
179bdeced75SVladimir Oltean 	phylink_set(mask, 100baseT_Full);
180bdeced75SVladimir Oltean 	phylink_set(mask, 1000baseT_Full);
18174984a19SAlex Marginean 
18228a134f5SVladimir Oltean 	if (state->interface == PHY_INTERFACE_MODE_INTERNAL ||
183bdeced75SVladimir Oltean 	    state->interface == PHY_INTERFACE_MODE_2500BASEX ||
184bdeced75SVladimir Oltean 	    state->interface == PHY_INTERFACE_MODE_USXGMII) {
185bdeced75SVladimir Oltean 		phylink_set(mask, 2500baseT_Full);
186bdeced75SVladimir Oltean 		phylink_set(mask, 2500baseX_Full);
187bdeced75SVladimir Oltean 	}
188bdeced75SVladimir Oltean 
189bdeced75SVladimir Oltean 	bitmap_and(supported, supported, mask,
190bdeced75SVladimir Oltean 		   __ETHTOOL_LINK_MODE_MASK_NBITS);
191bdeced75SVladimir Oltean 	bitmap_and(state->advertising, state->advertising, mask,
192bdeced75SVladimir Oltean 		   __ETHTOOL_LINK_MODE_MASK_NBITS);
193bdeced75SVladimir Oltean }
194bdeced75SVladimir Oltean 
195bdeced75SVladimir Oltean static int felix_phylink_mac_pcs_get_state(struct dsa_switch *ds, int port,
196bdeced75SVladimir Oltean 					   struct phylink_link_state *state)
197bdeced75SVladimir Oltean {
198bdeced75SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
199bdeced75SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
200bdeced75SVladimir Oltean 
201bdeced75SVladimir Oltean 	if (felix->info->pcs_link_state)
202bdeced75SVladimir Oltean 		felix->info->pcs_link_state(ocelot, port, state);
203bdeced75SVladimir Oltean 
204bdeced75SVladimir Oltean 	return 0;
205bdeced75SVladimir Oltean }
206bdeced75SVladimir Oltean 
207bdeced75SVladimir Oltean static void felix_phylink_mac_config(struct dsa_switch *ds, int port,
208bdeced75SVladimir Oltean 				     unsigned int link_an_mode,
209bdeced75SVladimir Oltean 				     const struct phylink_link_state *state)
210bdeced75SVladimir Oltean {
211bdeced75SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
212bdeced75SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
213bdeced75SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
214bdeced75SVladimir Oltean 	u32 mac_fc_cfg;
215bdeced75SVladimir Oltean 
216bdeced75SVladimir Oltean 	/* Take port out of reset by clearing the MAC_TX_RST, MAC_RX_RST and
217bdeced75SVladimir Oltean 	 * PORT_RST bits in CLOCK_CFG
218bdeced75SVladimir Oltean 	 */
219bdeced75SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_CLOCK_CFG_LINK_SPEED(state->speed),
220bdeced75SVladimir Oltean 			   DEV_CLOCK_CFG);
221bdeced75SVladimir Oltean 
222bdeced75SVladimir Oltean 	/* Flow control. Link speed is only used here to evaluate the time
223bdeced75SVladimir Oltean 	 * specification in incoming pause frames.
224bdeced75SVladimir Oltean 	 */
225bdeced75SVladimir Oltean 	mac_fc_cfg = SYS_MAC_FC_CFG_FC_LINK_SPEED(state->speed);
226f3660937SAlex Marginean 
227f3660937SAlex Marginean 	/* handle Rx pause in all cases, with 2500base-X this is used for rate
228f3660937SAlex Marginean 	 * adaptation.
229f3660937SAlex Marginean 	 */
230bdeced75SVladimir Oltean 	mac_fc_cfg |= SYS_MAC_FC_CFG_RX_FC_ENA;
231f3660937SAlex Marginean 
232bdeced75SVladimir Oltean 	if (state->pause & MLO_PAUSE_TX)
233bdeced75SVladimir Oltean 		mac_fc_cfg |= SYS_MAC_FC_CFG_TX_FC_ENA |
234bdeced75SVladimir Oltean 			      SYS_MAC_FC_CFG_PAUSE_VAL_CFG(0xffff) |
235bdeced75SVladimir Oltean 			      SYS_MAC_FC_CFG_FC_LATENCY_CFG(0x7) |
236bdeced75SVladimir Oltean 			      SYS_MAC_FC_CFG_ZERO_PAUSE_ENA;
237bdeced75SVladimir Oltean 	ocelot_write_rix(ocelot, mac_fc_cfg, SYS_MAC_FC_CFG, port);
238bdeced75SVladimir Oltean 
239bdeced75SVladimir Oltean 	ocelot_write_rix(ocelot, 0, ANA_POL_FLOWC, port);
240bdeced75SVladimir Oltean 
241bdeced75SVladimir Oltean 	if (felix->info->pcs_init)
242bdeced75SVladimir Oltean 		felix->info->pcs_init(ocelot, port, link_an_mode, state);
243bdeced75SVladimir Oltean }
244bdeced75SVladimir Oltean 
245bdeced75SVladimir Oltean static void felix_phylink_mac_an_restart(struct dsa_switch *ds, int port)
246bdeced75SVladimir Oltean {
247bdeced75SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
248bdeced75SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
249bdeced75SVladimir Oltean 
250bdeced75SVladimir Oltean 	if (felix->info->pcs_an_restart)
251bdeced75SVladimir Oltean 		felix->info->pcs_an_restart(ocelot, port);
252bdeced75SVladimir Oltean }
253bdeced75SVladimir Oltean 
254bdeced75SVladimir Oltean static void felix_phylink_mac_link_down(struct dsa_switch *ds, int port,
255bdeced75SVladimir Oltean 					unsigned int link_an_mode,
256bdeced75SVladimir Oltean 					phy_interface_t interface)
257bdeced75SVladimir Oltean {
258bdeced75SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
259bdeced75SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
260bdeced75SVladimir Oltean 
261bdeced75SVladimir Oltean 	ocelot_port_writel(ocelot_port, 0, DEV_MAC_ENA_CFG);
262bdeced75SVladimir Oltean 	ocelot_rmw_rix(ocelot, 0, QSYS_SWITCH_PORT_MODE_PORT_ENA,
263bdeced75SVladimir Oltean 		       QSYS_SWITCH_PORT_MODE, port);
264bdeced75SVladimir Oltean }
265bdeced75SVladimir Oltean 
266bdeced75SVladimir Oltean static void felix_phylink_mac_link_up(struct dsa_switch *ds, int port,
267bdeced75SVladimir Oltean 				      unsigned int link_an_mode,
268bdeced75SVladimir Oltean 				      phy_interface_t interface,
2695b502a7bSRussell King 				      struct phy_device *phydev,
2705b502a7bSRussell King 				      int speed, int duplex,
2715b502a7bSRussell King 				      bool tx_pause, bool rx_pause)
272bdeced75SVladimir Oltean {
273bdeced75SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
274bdeced75SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
275bdeced75SVladimir Oltean 
276bdeced75SVladimir Oltean 	/* Enable MAC module */
277bdeced75SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_MAC_ENA_CFG_RX_ENA |
278bdeced75SVladimir Oltean 			   DEV_MAC_ENA_CFG_TX_ENA, DEV_MAC_ENA_CFG);
279bdeced75SVladimir Oltean 
280bdeced75SVladimir Oltean 	/* Enable receiving frames on the port, and activate auto-learning of
281bdeced75SVladimir Oltean 	 * MAC addresses.
282bdeced75SVladimir Oltean 	 */
283bdeced75SVladimir Oltean 	ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_LEARNAUTO |
284bdeced75SVladimir Oltean 			 ANA_PORT_PORT_CFG_RECV_ENA |
285bdeced75SVladimir Oltean 			 ANA_PORT_PORT_CFG_PORTID_VAL(port),
286bdeced75SVladimir Oltean 			 ANA_PORT_PORT_CFG, port);
287bdeced75SVladimir Oltean 
288bdeced75SVladimir Oltean 	/* Core: Enable port for frame transfer */
289bdeced75SVladimir Oltean 	ocelot_write_rix(ocelot, QSYS_SWITCH_PORT_MODE_INGRESS_DROP_MODE |
290bdeced75SVladimir Oltean 			 QSYS_SWITCH_PORT_MODE_SCH_NEXT_CFG(1) |
291bdeced75SVladimir Oltean 			 QSYS_SWITCH_PORT_MODE_PORT_ENA,
292bdeced75SVladimir Oltean 			 QSYS_SWITCH_PORT_MODE, port);
293bdeced75SVladimir Oltean }
294bdeced75SVladimir Oltean 
29556051948SVladimir Oltean static void felix_get_strings(struct dsa_switch *ds, int port,
29656051948SVladimir Oltean 			      u32 stringset, u8 *data)
29756051948SVladimir Oltean {
29856051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
29956051948SVladimir Oltean 
30056051948SVladimir Oltean 	return ocelot_get_strings(ocelot, port, stringset, data);
30156051948SVladimir Oltean }
30256051948SVladimir Oltean 
30356051948SVladimir Oltean static void felix_get_ethtool_stats(struct dsa_switch *ds, int port, u64 *data)
30456051948SVladimir Oltean {
30556051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
30656051948SVladimir Oltean 
30756051948SVladimir Oltean 	ocelot_get_ethtool_stats(ocelot, port, data);
30856051948SVladimir Oltean }
30956051948SVladimir Oltean 
31056051948SVladimir Oltean static int felix_get_sset_count(struct dsa_switch *ds, int port, int sset)
31156051948SVladimir Oltean {
31256051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
31356051948SVladimir Oltean 
31456051948SVladimir Oltean 	return ocelot_get_sset_count(ocelot, port, sset);
31556051948SVladimir Oltean }
31656051948SVladimir Oltean 
31756051948SVladimir Oltean static int felix_get_ts_info(struct dsa_switch *ds, int port,
31856051948SVladimir Oltean 			     struct ethtool_ts_info *info)
31956051948SVladimir Oltean {
32056051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
32156051948SVladimir Oltean 
32256051948SVladimir Oltean 	return ocelot_get_ts_info(ocelot, port, info);
32356051948SVladimir Oltean }
32456051948SVladimir Oltean 
325bdeced75SVladimir Oltean static int felix_parse_ports_node(struct felix *felix,
326bdeced75SVladimir Oltean 				  struct device_node *ports_node,
327bdeced75SVladimir Oltean 				  phy_interface_t *port_phy_modes)
328bdeced75SVladimir Oltean {
329bdeced75SVladimir Oltean 	struct ocelot *ocelot = &felix->ocelot;
330bdeced75SVladimir Oltean 	struct device *dev = felix->ocelot.dev;
331bdeced75SVladimir Oltean 	struct device_node *child;
332bdeced75SVladimir Oltean 
33337fe45adSVladimir Oltean 	for_each_available_child_of_node(ports_node, child) {
334bdeced75SVladimir Oltean 		phy_interface_t phy_mode;
335bdeced75SVladimir Oltean 		u32 port;
336bdeced75SVladimir Oltean 		int err;
337bdeced75SVladimir Oltean 
338bdeced75SVladimir Oltean 		/* Get switch port number from DT */
339bdeced75SVladimir Oltean 		if (of_property_read_u32(child, "reg", &port) < 0) {
340bdeced75SVladimir Oltean 			dev_err(dev, "Port number not defined in device tree "
341bdeced75SVladimir Oltean 				"(property \"reg\")\n");
342bdeced75SVladimir Oltean 			of_node_put(child);
343bdeced75SVladimir Oltean 			return -ENODEV;
344bdeced75SVladimir Oltean 		}
345bdeced75SVladimir Oltean 
346bdeced75SVladimir Oltean 		/* Get PHY mode from DT */
347bdeced75SVladimir Oltean 		err = of_get_phy_mode(child, &phy_mode);
348bdeced75SVladimir Oltean 		if (err) {
349bdeced75SVladimir Oltean 			dev_err(dev, "Failed to read phy-mode or "
350bdeced75SVladimir Oltean 				"phy-interface-type property for port %d\n",
351bdeced75SVladimir Oltean 				port);
352bdeced75SVladimir Oltean 			of_node_put(child);
353bdeced75SVladimir Oltean 			return -ENODEV;
354bdeced75SVladimir Oltean 		}
355bdeced75SVladimir Oltean 
356bdeced75SVladimir Oltean 		err = felix->info->prevalidate_phy_mode(ocelot, port, phy_mode);
357bdeced75SVladimir Oltean 		if (err < 0) {
358bdeced75SVladimir Oltean 			dev_err(dev, "Unsupported PHY mode %s on port %d\n",
359bdeced75SVladimir Oltean 				phy_modes(phy_mode), port);
360bdeced75SVladimir Oltean 			return err;
361bdeced75SVladimir Oltean 		}
362bdeced75SVladimir Oltean 
363bdeced75SVladimir Oltean 		port_phy_modes[port] = phy_mode;
364bdeced75SVladimir Oltean 	}
365bdeced75SVladimir Oltean 
366bdeced75SVladimir Oltean 	return 0;
367bdeced75SVladimir Oltean }
368bdeced75SVladimir Oltean 
369bdeced75SVladimir Oltean static int felix_parse_dt(struct felix *felix, phy_interface_t *port_phy_modes)
370bdeced75SVladimir Oltean {
371bdeced75SVladimir Oltean 	struct device *dev = felix->ocelot.dev;
372bdeced75SVladimir Oltean 	struct device_node *switch_node;
373bdeced75SVladimir Oltean 	struct device_node *ports_node;
374bdeced75SVladimir Oltean 	int err;
375bdeced75SVladimir Oltean 
376bdeced75SVladimir Oltean 	switch_node = dev->of_node;
377bdeced75SVladimir Oltean 
378bdeced75SVladimir Oltean 	ports_node = of_get_child_by_name(switch_node, "ports");
379bdeced75SVladimir Oltean 	if (!ports_node) {
380bdeced75SVladimir Oltean 		dev_err(dev, "Incorrect bindings: absent \"ports\" node\n");
381bdeced75SVladimir Oltean 		return -ENODEV;
382bdeced75SVladimir Oltean 	}
383bdeced75SVladimir Oltean 
384bdeced75SVladimir Oltean 	err = felix_parse_ports_node(felix, ports_node, port_phy_modes);
385bdeced75SVladimir Oltean 	of_node_put(ports_node);
386bdeced75SVladimir Oltean 
387bdeced75SVladimir Oltean 	return err;
388bdeced75SVladimir Oltean }
389bdeced75SVladimir Oltean 
39056051948SVladimir Oltean static int felix_init_structs(struct felix *felix, int num_phys_ports)
39156051948SVladimir Oltean {
39256051948SVladimir Oltean 	struct ocelot *ocelot = &felix->ocelot;
393bdeced75SVladimir Oltean 	phy_interface_t *port_phy_modes;
394bdeced75SVladimir Oltean 	resource_size_t switch_base;
395b4024c9eSClaudiu Manoil 	struct resource res;
39656051948SVladimir Oltean 	int port, i, err;
39756051948SVladimir Oltean 
39856051948SVladimir Oltean 	ocelot->num_phys_ports = num_phys_ports;
39956051948SVladimir Oltean 	ocelot->ports = devm_kcalloc(ocelot->dev, num_phys_ports,
40056051948SVladimir Oltean 				     sizeof(struct ocelot_port *), GFP_KERNEL);
40156051948SVladimir Oltean 	if (!ocelot->ports)
40256051948SVladimir Oltean 		return -ENOMEM;
40356051948SVladimir Oltean 
40456051948SVladimir Oltean 	ocelot->map		= felix->info->map;
40556051948SVladimir Oltean 	ocelot->stats_layout	= felix->info->stats_layout;
40656051948SVladimir Oltean 	ocelot->num_stats	= felix->info->num_stats;
40756051948SVladimir Oltean 	ocelot->shared_queue_sz	= felix->info->shared_queue_sz;
40821ce7f3eSVladimir Oltean 	ocelot->num_mact_rows	= felix->info->num_mact_rows;
40907d985eeSVladimir Oltean 	ocelot->vcap_is2_keys	= felix->info->vcap_is2_keys;
41007d985eeSVladimir Oltean 	ocelot->vcap_is2_actions= felix->info->vcap_is2_actions;
41107d985eeSVladimir Oltean 	ocelot->vcap		= felix->info->vcap;
41256051948SVladimir Oltean 	ocelot->ops		= felix->info->ops;
41356051948SVladimir Oltean 
414bdeced75SVladimir Oltean 	port_phy_modes = kcalloc(num_phys_ports, sizeof(phy_interface_t),
415bdeced75SVladimir Oltean 				 GFP_KERNEL);
416bdeced75SVladimir Oltean 	if (!port_phy_modes)
417bdeced75SVladimir Oltean 		return -ENOMEM;
418bdeced75SVladimir Oltean 
419bdeced75SVladimir Oltean 	err = felix_parse_dt(felix, port_phy_modes);
420bdeced75SVladimir Oltean 	if (err) {
421bdeced75SVladimir Oltean 		kfree(port_phy_modes);
422bdeced75SVladimir Oltean 		return err;
423bdeced75SVladimir Oltean 	}
424bdeced75SVladimir Oltean 
425bdeced75SVladimir Oltean 	switch_base = pci_resource_start(felix->pdev,
426bdeced75SVladimir Oltean 					 felix->info->switch_pci_bar);
42756051948SVladimir Oltean 
42856051948SVladimir Oltean 	for (i = 0; i < TARGET_MAX; i++) {
42956051948SVladimir Oltean 		struct regmap *target;
43056051948SVladimir Oltean 
43156051948SVladimir Oltean 		if (!felix->info->target_io_res[i].name)
43256051948SVladimir Oltean 			continue;
43356051948SVladimir Oltean 
434b4024c9eSClaudiu Manoil 		memcpy(&res, &felix->info->target_io_res[i], sizeof(res));
435b4024c9eSClaudiu Manoil 		res.flags = IORESOURCE_MEM;
436b4024c9eSClaudiu Manoil 		res.start += switch_base;
437b4024c9eSClaudiu Manoil 		res.end += switch_base;
43856051948SVladimir Oltean 
439b4024c9eSClaudiu Manoil 		target = ocelot_regmap_init(ocelot, &res);
44056051948SVladimir Oltean 		if (IS_ERR(target)) {
44156051948SVladimir Oltean 			dev_err(ocelot->dev,
44256051948SVladimir Oltean 				"Failed to map device memory space\n");
443bdeced75SVladimir Oltean 			kfree(port_phy_modes);
44456051948SVladimir Oltean 			return PTR_ERR(target);
44556051948SVladimir Oltean 		}
44656051948SVladimir Oltean 
44756051948SVladimir Oltean 		ocelot->targets[i] = target;
44856051948SVladimir Oltean 	}
44956051948SVladimir Oltean 
45056051948SVladimir Oltean 	err = ocelot_regfields_init(ocelot, felix->info->regfields);
45156051948SVladimir Oltean 	if (err) {
45256051948SVladimir Oltean 		dev_err(ocelot->dev, "failed to init reg fields map\n");
453bdeced75SVladimir Oltean 		kfree(port_phy_modes);
45456051948SVladimir Oltean 		return err;
45556051948SVladimir Oltean 	}
45656051948SVladimir Oltean 
45756051948SVladimir Oltean 	for (port = 0; port < num_phys_ports; port++) {
45856051948SVladimir Oltean 		struct ocelot_port *ocelot_port;
45956051948SVladimir Oltean 		void __iomem *port_regs;
46056051948SVladimir Oltean 
46156051948SVladimir Oltean 		ocelot_port = devm_kzalloc(ocelot->dev,
46256051948SVladimir Oltean 					   sizeof(struct ocelot_port),
46356051948SVladimir Oltean 					   GFP_KERNEL);
46456051948SVladimir Oltean 		if (!ocelot_port) {
46556051948SVladimir Oltean 			dev_err(ocelot->dev,
46656051948SVladimir Oltean 				"failed to allocate port memory\n");
467bdeced75SVladimir Oltean 			kfree(port_phy_modes);
46856051948SVladimir Oltean 			return -ENOMEM;
46956051948SVladimir Oltean 		}
47056051948SVladimir Oltean 
471b4024c9eSClaudiu Manoil 		memcpy(&res, &felix->info->port_io_res[port], sizeof(res));
472b4024c9eSClaudiu Manoil 		res.flags = IORESOURCE_MEM;
473b4024c9eSClaudiu Manoil 		res.start += switch_base;
474b4024c9eSClaudiu Manoil 		res.end += switch_base;
47556051948SVladimir Oltean 
476b4024c9eSClaudiu Manoil 		port_regs = devm_ioremap_resource(ocelot->dev, &res);
47756051948SVladimir Oltean 		if (IS_ERR(port_regs)) {
47856051948SVladimir Oltean 			dev_err(ocelot->dev,
47956051948SVladimir Oltean 				"failed to map registers for port %d\n", port);
480bdeced75SVladimir Oltean 			kfree(port_phy_modes);
48156051948SVladimir Oltean 			return PTR_ERR(port_regs);
48256051948SVladimir Oltean 		}
48356051948SVladimir Oltean 
484bdeced75SVladimir Oltean 		ocelot_port->phy_mode = port_phy_modes[port];
48556051948SVladimir Oltean 		ocelot_port->ocelot = ocelot;
48656051948SVladimir Oltean 		ocelot_port->regs = port_regs;
48756051948SVladimir Oltean 		ocelot->ports[port] = ocelot_port;
48856051948SVladimir Oltean 	}
48956051948SVladimir Oltean 
490bdeced75SVladimir Oltean 	kfree(port_phy_modes);
491bdeced75SVladimir Oltean 
492bdeced75SVladimir Oltean 	if (felix->info->mdio_bus_alloc) {
493bdeced75SVladimir Oltean 		err = felix->info->mdio_bus_alloc(ocelot);
494bdeced75SVladimir Oltean 		if (err < 0)
495bdeced75SVladimir Oltean 			return err;
496bdeced75SVladimir Oltean 	}
497bdeced75SVladimir Oltean 
49856051948SVladimir Oltean 	return 0;
49956051948SVladimir Oltean }
50056051948SVladimir Oltean 
50156051948SVladimir Oltean /* Hardware initialization done here so that we can allocate structures with
50256051948SVladimir Oltean  * devm without fear of dsa_register_switch returning -EPROBE_DEFER and causing
50356051948SVladimir Oltean  * us to allocate structures twice (leak memory) and map PCI memory twice
50456051948SVladimir Oltean  * (which will not work).
50556051948SVladimir Oltean  */
50656051948SVladimir Oltean static int felix_setup(struct dsa_switch *ds)
50756051948SVladimir Oltean {
50856051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
50956051948SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
51056051948SVladimir Oltean 	int port, err;
51156051948SVladimir Oltean 
51256051948SVladimir Oltean 	err = felix_init_structs(felix, ds->num_ports);
51356051948SVladimir Oltean 	if (err)
51456051948SVladimir Oltean 		return err;
51556051948SVladimir Oltean 
51656051948SVladimir Oltean 	ocelot_init(ocelot);
51756051948SVladimir Oltean 
51856051948SVladimir Oltean 	for (port = 0; port < ds->num_ports; port++) {
51956051948SVladimir Oltean 		ocelot_init_port(ocelot, port);
52056051948SVladimir Oltean 
52169df578cSVladimir Oltean 		/* Bring up the CPU port module and configure the NPI port */
522b8fc7177SVladimir Oltean 		if (dsa_is_cpu_port(ds, port))
52369df578cSVladimir Oltean 			ocelot_configure_cpu(ocelot, port,
52456051948SVladimir Oltean 					     OCELOT_TAG_PREFIX_NONE,
52556051948SVladimir Oltean 					     OCELOT_TAG_PREFIX_LONG);
52656051948SVladimir Oltean 	}
52756051948SVladimir Oltean 
5281cf3299bSVladimir Oltean 	/* Include the CPU port module in the forwarding mask for unknown
5291cf3299bSVladimir Oltean 	 * unicast - the hardware default value for ANA_FLOODING_FLD_UNICAST
5301cf3299bSVladimir Oltean 	 * excludes BIT(ocelot->num_phys_ports), and so does ocelot_init, since
5311cf3299bSVladimir Oltean 	 * Ocelot relies on whitelisting MAC addresses towards PGID_CPU.
5321cf3299bSVladimir Oltean 	 */
5331cf3299bSVladimir Oltean 	ocelot_write_rix(ocelot,
5341cf3299bSVladimir Oltean 			 ANA_PGID_PGID_PGID(GENMASK(ocelot->num_phys_ports, 0)),
5351cf3299bSVladimir Oltean 			 ANA_PGID_PGID, PGID_UC);
5361cf3299bSVladimir Oltean 
5370b912fc9SVladimir Oltean 	ds->mtu_enforcement_ingress = true;
538bdeced75SVladimir Oltean 	/* It looks like the MAC/PCS interrupt register - PM0_IEVENT (0x8040)
539bdeced75SVladimir Oltean 	 * isn't instantiated for the Felix PF.
540bdeced75SVladimir Oltean 	 * In-band AN may take a few ms to complete, so we need to poll.
541bdeced75SVladimir Oltean 	 */
542bdeced75SVladimir Oltean 	ds->pcs_poll = true;
543bdeced75SVladimir Oltean 
54456051948SVladimir Oltean 	return 0;
54556051948SVladimir Oltean }
54656051948SVladimir Oltean 
54756051948SVladimir Oltean static void felix_teardown(struct dsa_switch *ds)
54856051948SVladimir Oltean {
54956051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
550bdeced75SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
551bdeced75SVladimir Oltean 
552bdeced75SVladimir Oltean 	if (felix->info->mdio_bus_free)
553bdeced75SVladimir Oltean 		felix->info->mdio_bus_free(ocelot);
55456051948SVladimir Oltean 
55556051948SVladimir Oltean 	/* stop workqueue thread */
55656051948SVladimir Oltean 	ocelot_deinit(ocelot);
55756051948SVladimir Oltean }
55856051948SVladimir Oltean 
559c0bcf537SYangbo Lu static int felix_hwtstamp_get(struct dsa_switch *ds, int port,
560c0bcf537SYangbo Lu 			      struct ifreq *ifr)
561c0bcf537SYangbo Lu {
562c0bcf537SYangbo Lu 	struct ocelot *ocelot = ds->priv;
563c0bcf537SYangbo Lu 
564c0bcf537SYangbo Lu 	return ocelot_hwstamp_get(ocelot, port, ifr);
565c0bcf537SYangbo Lu }
566c0bcf537SYangbo Lu 
567c0bcf537SYangbo Lu static int felix_hwtstamp_set(struct dsa_switch *ds, int port,
568c0bcf537SYangbo Lu 			      struct ifreq *ifr)
569c0bcf537SYangbo Lu {
570c0bcf537SYangbo Lu 	struct ocelot *ocelot = ds->priv;
571c0bcf537SYangbo Lu 
572c0bcf537SYangbo Lu 	return ocelot_hwstamp_set(ocelot, port, ifr);
573c0bcf537SYangbo Lu }
574c0bcf537SYangbo Lu 
575c0bcf537SYangbo Lu static bool felix_rxtstamp(struct dsa_switch *ds, int port,
576c0bcf537SYangbo Lu 			   struct sk_buff *skb, unsigned int type)
577c0bcf537SYangbo Lu {
578c0bcf537SYangbo Lu 	struct skb_shared_hwtstamps *shhwtstamps;
579c0bcf537SYangbo Lu 	struct ocelot *ocelot = ds->priv;
580c0bcf537SYangbo Lu 	u8 *extraction = skb->data - ETH_HLEN - OCELOT_TAG_LEN;
581c0bcf537SYangbo Lu 	u32 tstamp_lo, tstamp_hi;
582c0bcf537SYangbo Lu 	struct timespec64 ts;
583c0bcf537SYangbo Lu 	u64 tstamp, val;
584c0bcf537SYangbo Lu 
585c0bcf537SYangbo Lu 	ocelot_ptp_gettime64(&ocelot->ptp_info, &ts);
586c0bcf537SYangbo Lu 	tstamp = ktime_set(ts.tv_sec, ts.tv_nsec);
587c0bcf537SYangbo Lu 
588c0bcf537SYangbo Lu 	packing(extraction, &val,  116, 85, OCELOT_TAG_LEN, UNPACK, 0);
589c0bcf537SYangbo Lu 	tstamp_lo = (u32)val;
590c0bcf537SYangbo Lu 
591c0bcf537SYangbo Lu 	tstamp_hi = tstamp >> 32;
592c0bcf537SYangbo Lu 	if ((tstamp & 0xffffffff) < tstamp_lo)
593c0bcf537SYangbo Lu 		tstamp_hi--;
594c0bcf537SYangbo Lu 
595c0bcf537SYangbo Lu 	tstamp = ((u64)tstamp_hi << 32) | tstamp_lo;
596c0bcf537SYangbo Lu 
597c0bcf537SYangbo Lu 	shhwtstamps = skb_hwtstamps(skb);
598c0bcf537SYangbo Lu 	memset(shhwtstamps, 0, sizeof(struct skb_shared_hwtstamps));
599c0bcf537SYangbo Lu 	shhwtstamps->hwtstamp = tstamp;
600c0bcf537SYangbo Lu 	return false;
601c0bcf537SYangbo Lu }
602c0bcf537SYangbo Lu 
6033243e04aSChen Wandun static bool felix_txtstamp(struct dsa_switch *ds, int port,
604c0bcf537SYangbo Lu 			   struct sk_buff *clone, unsigned int type)
605c0bcf537SYangbo Lu {
606c0bcf537SYangbo Lu 	struct ocelot *ocelot = ds->priv;
607c0bcf537SYangbo Lu 	struct ocelot_port *ocelot_port = ocelot->ports[port];
608c0bcf537SYangbo Lu 
609c0bcf537SYangbo Lu 	if (!ocelot_port_add_txtstamp_skb(ocelot_port, clone))
610c0bcf537SYangbo Lu 		return true;
611c0bcf537SYangbo Lu 
612c0bcf537SYangbo Lu 	return false;
613c0bcf537SYangbo Lu }
614c0bcf537SYangbo Lu 
6150b912fc9SVladimir Oltean static int felix_change_mtu(struct dsa_switch *ds, int port, int new_mtu)
6160b912fc9SVladimir Oltean {
6170b912fc9SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
6180b912fc9SVladimir Oltean 
6190b912fc9SVladimir Oltean 	ocelot_port_set_maxlen(ocelot, port, new_mtu);
6200b912fc9SVladimir Oltean 
6210b912fc9SVladimir Oltean 	return 0;
6220b912fc9SVladimir Oltean }
6230b912fc9SVladimir Oltean 
6240b912fc9SVladimir Oltean static int felix_get_max_mtu(struct dsa_switch *ds, int port)
6250b912fc9SVladimir Oltean {
6260b912fc9SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
6270b912fc9SVladimir Oltean 
6280b912fc9SVladimir Oltean 	return ocelot_get_max_mtu(ocelot, port);
6290b912fc9SVladimir Oltean }
6300b912fc9SVladimir Oltean 
63107d985eeSVladimir Oltean static int felix_cls_flower_add(struct dsa_switch *ds, int port,
63207d985eeSVladimir Oltean 				struct flow_cls_offload *cls, bool ingress)
63307d985eeSVladimir Oltean {
63407d985eeSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
63507d985eeSVladimir Oltean 
63607d985eeSVladimir Oltean 	return ocelot_cls_flower_replace(ocelot, port, cls, ingress);
63707d985eeSVladimir Oltean }
63807d985eeSVladimir Oltean 
63907d985eeSVladimir Oltean static int felix_cls_flower_del(struct dsa_switch *ds, int port,
64007d985eeSVladimir Oltean 				struct flow_cls_offload *cls, bool ingress)
64107d985eeSVladimir Oltean {
64207d985eeSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
64307d985eeSVladimir Oltean 
64407d985eeSVladimir Oltean 	return ocelot_cls_flower_destroy(ocelot, port, cls, ingress);
64507d985eeSVladimir Oltean }
64607d985eeSVladimir Oltean 
64707d985eeSVladimir Oltean static int felix_cls_flower_stats(struct dsa_switch *ds, int port,
64807d985eeSVladimir Oltean 				  struct flow_cls_offload *cls, bool ingress)
64907d985eeSVladimir Oltean {
65007d985eeSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
65107d985eeSVladimir Oltean 
65207d985eeSVladimir Oltean 	return ocelot_cls_flower_stats(ocelot, port, cls, ingress);
65307d985eeSVladimir Oltean }
65407d985eeSVladimir Oltean 
655fc411eaaSVladimir Oltean static int felix_port_policer_add(struct dsa_switch *ds, int port,
656fc411eaaSVladimir Oltean 				  struct dsa_mall_policer_tc_entry *policer)
657fc411eaaSVladimir Oltean {
658fc411eaaSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
659fc411eaaSVladimir Oltean 	struct ocelot_policer pol = {
660fc411eaaSVladimir Oltean 		.rate = div_u64(policer->rate_bytes_per_sec, 1000) * 8,
661fc411eaaSVladimir Oltean 		.burst = div_u64(policer->rate_bytes_per_sec *
662fc411eaaSVladimir Oltean 				 PSCHED_NS2TICKS(policer->burst),
663fc411eaaSVladimir Oltean 				 PSCHED_TICKS_PER_SEC),
664fc411eaaSVladimir Oltean 	};
665fc411eaaSVladimir Oltean 
666fc411eaaSVladimir Oltean 	return ocelot_port_policer_add(ocelot, port, &pol);
667fc411eaaSVladimir Oltean }
668fc411eaaSVladimir Oltean 
669fc411eaaSVladimir Oltean static void felix_port_policer_del(struct dsa_switch *ds, int port)
670fc411eaaSVladimir Oltean {
671fc411eaaSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
672fc411eaaSVladimir Oltean 
673fc411eaaSVladimir Oltean 	ocelot_port_policer_del(ocelot, port);
674fc411eaaSVladimir Oltean }
675fc411eaaSVladimir Oltean 
67656051948SVladimir Oltean static const struct dsa_switch_ops felix_switch_ops = {
67756051948SVladimir Oltean 	.get_tag_protocol	= felix_get_tag_protocol,
67856051948SVladimir Oltean 	.setup			= felix_setup,
67956051948SVladimir Oltean 	.teardown		= felix_teardown,
68056051948SVladimir Oltean 	.set_ageing_time	= felix_set_ageing_time,
68156051948SVladimir Oltean 	.get_strings		= felix_get_strings,
68256051948SVladimir Oltean 	.get_ethtool_stats	= felix_get_ethtool_stats,
68356051948SVladimir Oltean 	.get_sset_count		= felix_get_sset_count,
68456051948SVladimir Oltean 	.get_ts_info		= felix_get_ts_info,
685bdeced75SVladimir Oltean 	.phylink_validate	= felix_phylink_validate,
686bdeced75SVladimir Oltean 	.phylink_mac_link_state	= felix_phylink_mac_pcs_get_state,
687bdeced75SVladimir Oltean 	.phylink_mac_config	= felix_phylink_mac_config,
688bdeced75SVladimir Oltean 	.phylink_mac_an_restart	= felix_phylink_mac_an_restart,
689bdeced75SVladimir Oltean 	.phylink_mac_link_down	= felix_phylink_mac_link_down,
690bdeced75SVladimir Oltean 	.phylink_mac_link_up	= felix_phylink_mac_link_up,
69156051948SVladimir Oltean 	.port_enable		= felix_port_enable,
69256051948SVladimir Oltean 	.port_disable		= felix_port_disable,
69356051948SVladimir Oltean 	.port_fdb_dump		= felix_fdb_dump,
69456051948SVladimir Oltean 	.port_fdb_add		= felix_fdb_add,
69556051948SVladimir Oltean 	.port_fdb_del		= felix_fdb_del,
69656051948SVladimir Oltean 	.port_bridge_join	= felix_bridge_join,
69756051948SVladimir Oltean 	.port_bridge_leave	= felix_bridge_leave,
69856051948SVladimir Oltean 	.port_stp_state_set	= felix_bridge_stp_state_set,
69956051948SVladimir Oltean 	.port_vlan_prepare	= felix_vlan_prepare,
70056051948SVladimir Oltean 	.port_vlan_filtering	= felix_vlan_filtering,
70156051948SVladimir Oltean 	.port_vlan_add		= felix_vlan_add,
70256051948SVladimir Oltean 	.port_vlan_del		= felix_vlan_del,
703c0bcf537SYangbo Lu 	.port_hwtstamp_get	= felix_hwtstamp_get,
704c0bcf537SYangbo Lu 	.port_hwtstamp_set	= felix_hwtstamp_set,
705c0bcf537SYangbo Lu 	.port_rxtstamp		= felix_rxtstamp,
706c0bcf537SYangbo Lu 	.port_txtstamp		= felix_txtstamp,
7070b912fc9SVladimir Oltean 	.port_change_mtu	= felix_change_mtu,
7080b912fc9SVladimir Oltean 	.port_max_mtu		= felix_get_max_mtu,
709fc411eaaSVladimir Oltean 	.port_policer_add	= felix_port_policer_add,
710fc411eaaSVladimir Oltean 	.port_policer_del	= felix_port_policer_del,
71107d985eeSVladimir Oltean 	.cls_flower_add		= felix_cls_flower_add,
71207d985eeSVladimir Oltean 	.cls_flower_del		= felix_cls_flower_del,
71307d985eeSVladimir Oltean 	.cls_flower_stats	= felix_cls_flower_stats,
71456051948SVladimir Oltean };
71556051948SVladimir Oltean 
71656051948SVladimir Oltean static struct felix_info *felix_instance_tbl[] = {
71756051948SVladimir Oltean 	[FELIX_INSTANCE_VSC9959] = &felix_info_vsc9959,
71856051948SVladimir Oltean };
71956051948SVladimir Oltean 
720c0bcf537SYangbo Lu static irqreturn_t felix_irq_handler(int irq, void *data)
721c0bcf537SYangbo Lu {
722c0bcf537SYangbo Lu 	struct ocelot *ocelot = (struct ocelot *)data;
723c0bcf537SYangbo Lu 
724c0bcf537SYangbo Lu 	/* The INTB interrupt is used for both PTP TX timestamp interrupt
725c0bcf537SYangbo Lu 	 * and preemption status change interrupt on each port.
726c0bcf537SYangbo Lu 	 *
727c0bcf537SYangbo Lu 	 * - Get txtstamp if have
728c0bcf537SYangbo Lu 	 * - TODO: handle preemption. Without handling it, driver may get
729c0bcf537SYangbo Lu 	 *   interrupt storm.
730c0bcf537SYangbo Lu 	 */
731c0bcf537SYangbo Lu 
732c0bcf537SYangbo Lu 	ocelot_get_txtstamp(ocelot);
733c0bcf537SYangbo Lu 
734c0bcf537SYangbo Lu 	return IRQ_HANDLED;
735c0bcf537SYangbo Lu }
736c0bcf537SYangbo Lu 
73756051948SVladimir Oltean static int felix_pci_probe(struct pci_dev *pdev,
73856051948SVladimir Oltean 			   const struct pci_device_id *id)
73956051948SVladimir Oltean {
74056051948SVladimir Oltean 	enum felix_instance instance = id->driver_data;
74156051948SVladimir Oltean 	struct dsa_switch *ds;
74256051948SVladimir Oltean 	struct ocelot *ocelot;
74356051948SVladimir Oltean 	struct felix *felix;
74456051948SVladimir Oltean 	int err;
74556051948SVladimir Oltean 
74656051948SVladimir Oltean 	err = pci_enable_device(pdev);
74756051948SVladimir Oltean 	if (err) {
74856051948SVladimir Oltean 		dev_err(&pdev->dev, "device enable failed\n");
74956051948SVladimir Oltean 		goto err_pci_enable;
75056051948SVladimir Oltean 	}
75156051948SVladimir Oltean 
75256051948SVladimir Oltean 	/* set up for high or low dma */
75356051948SVladimir Oltean 	err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
75456051948SVladimir Oltean 	if (err) {
75556051948SVladimir Oltean 		err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
75656051948SVladimir Oltean 		if (err) {
75756051948SVladimir Oltean 			dev_err(&pdev->dev,
75856051948SVladimir Oltean 				"DMA configuration failed: 0x%x\n", err);
75956051948SVladimir Oltean 			goto err_dma;
76056051948SVladimir Oltean 		}
76156051948SVladimir Oltean 	}
76256051948SVladimir Oltean 
76356051948SVladimir Oltean 	felix = kzalloc(sizeof(struct felix), GFP_KERNEL);
76456051948SVladimir Oltean 	if (!felix) {
76556051948SVladimir Oltean 		err = -ENOMEM;
76656051948SVladimir Oltean 		dev_err(&pdev->dev, "Failed to allocate driver memory\n");
76756051948SVladimir Oltean 		goto err_alloc_felix;
76856051948SVladimir Oltean 	}
76956051948SVladimir Oltean 
77056051948SVladimir Oltean 	pci_set_drvdata(pdev, felix);
77156051948SVladimir Oltean 	ocelot = &felix->ocelot;
77256051948SVladimir Oltean 	ocelot->dev = &pdev->dev;
77356051948SVladimir Oltean 	felix->pdev = pdev;
77456051948SVladimir Oltean 	felix->info = felix_instance_tbl[instance];
77556051948SVladimir Oltean 
77656051948SVladimir Oltean 	pci_set_master(pdev);
77756051948SVladimir Oltean 
778c0bcf537SYangbo Lu 	err = devm_request_threaded_irq(&pdev->dev, pdev->irq, NULL,
779c0bcf537SYangbo Lu 					&felix_irq_handler, IRQF_ONESHOT,
780c0bcf537SYangbo Lu 					"felix-intb", ocelot);
781c0bcf537SYangbo Lu 	if (err) {
782c0bcf537SYangbo Lu 		dev_err(&pdev->dev, "Failed to request irq\n");
783c0bcf537SYangbo Lu 		goto err_alloc_irq;
784c0bcf537SYangbo Lu 	}
785c0bcf537SYangbo Lu 
786c0bcf537SYangbo Lu 	ocelot->ptp = 1;
787c0bcf537SYangbo Lu 
78856051948SVladimir Oltean 	ds = kzalloc(sizeof(struct dsa_switch), GFP_KERNEL);
78956051948SVladimir Oltean 	if (!ds) {
79056051948SVladimir Oltean 		err = -ENOMEM;
79156051948SVladimir Oltean 		dev_err(&pdev->dev, "Failed to allocate DSA switch\n");
79256051948SVladimir Oltean 		goto err_alloc_ds;
79356051948SVladimir Oltean 	}
79456051948SVladimir Oltean 
79556051948SVladimir Oltean 	ds->dev = &pdev->dev;
79656051948SVladimir Oltean 	ds->num_ports = felix->info->num_ports;
79756051948SVladimir Oltean 	ds->ops = &felix_switch_ops;
79856051948SVladimir Oltean 	ds->priv = ocelot;
79956051948SVladimir Oltean 	felix->ds = ds;
80056051948SVladimir Oltean 
80156051948SVladimir Oltean 	err = dsa_register_switch(ds);
80256051948SVladimir Oltean 	if (err) {
80356051948SVladimir Oltean 		dev_err(&pdev->dev, "Failed to register DSA switch: %d\n", err);
80456051948SVladimir Oltean 		goto err_register_ds;
80556051948SVladimir Oltean 	}
80656051948SVladimir Oltean 
80756051948SVladimir Oltean 	return 0;
80856051948SVladimir Oltean 
80956051948SVladimir Oltean err_register_ds:
81056051948SVladimir Oltean 	kfree(ds);
81156051948SVladimir Oltean err_alloc_ds:
812c0bcf537SYangbo Lu err_alloc_irq:
81356051948SVladimir Oltean err_alloc_felix:
81456051948SVladimir Oltean 	kfree(felix);
81556051948SVladimir Oltean err_dma:
81656051948SVladimir Oltean 	pci_disable_device(pdev);
81756051948SVladimir Oltean err_pci_enable:
81856051948SVladimir Oltean 	return err;
81956051948SVladimir Oltean }
82056051948SVladimir Oltean 
82156051948SVladimir Oltean static void felix_pci_remove(struct pci_dev *pdev)
82256051948SVladimir Oltean {
82356051948SVladimir Oltean 	struct felix *felix;
82456051948SVladimir Oltean 
82556051948SVladimir Oltean 	felix = pci_get_drvdata(pdev);
82656051948SVladimir Oltean 
82756051948SVladimir Oltean 	dsa_unregister_switch(felix->ds);
82856051948SVladimir Oltean 
82956051948SVladimir Oltean 	kfree(felix->ds);
83056051948SVladimir Oltean 	kfree(felix);
83156051948SVladimir Oltean 
83256051948SVladimir Oltean 	pci_disable_device(pdev);
83356051948SVladimir Oltean }
83456051948SVladimir Oltean 
83556051948SVladimir Oltean static struct pci_device_id felix_ids[] = {
83656051948SVladimir Oltean 	{
83756051948SVladimir Oltean 		/* NXP LS1028A */
83856051948SVladimir Oltean 		PCI_DEVICE(PCI_VENDOR_ID_FREESCALE, 0xEEF0),
83956051948SVladimir Oltean 		.driver_data = FELIX_INSTANCE_VSC9959,
84056051948SVladimir Oltean 	},
84156051948SVladimir Oltean 	{ 0, }
84256051948SVladimir Oltean };
84356051948SVladimir Oltean MODULE_DEVICE_TABLE(pci, felix_ids);
84456051948SVladimir Oltean 
84556051948SVladimir Oltean static struct pci_driver felix_pci_driver = {
84656051948SVladimir Oltean 	.name		= KBUILD_MODNAME,
84756051948SVladimir Oltean 	.id_table	= felix_ids,
84856051948SVladimir Oltean 	.probe		= felix_pci_probe,
84956051948SVladimir Oltean 	.remove		= felix_pci_remove,
85056051948SVladimir Oltean };
85156051948SVladimir Oltean 
85256051948SVladimir Oltean module_pci_driver(felix_pci_driver);
85356051948SVladimir Oltean 
85456051948SVladimir Oltean MODULE_DESCRIPTION("Felix Switch driver");
85556051948SVladimir Oltean MODULE_LICENSE("GPL v2");
856