xref: /linux/drivers/net/dsa/ocelot/felix.c (revision 5f035af7)
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>
102b49d128SYangbo Lu #include <soc/mscc/ocelot_ptp.h>
1156051948SVladimir Oltean #include <soc/mscc/ocelot.h>
12c0bcf537SYangbo Lu #include <linux/packing.h>
1356051948SVladimir Oltean #include <linux/module.h>
14bdeced75SVladimir Oltean #include <linux/of_net.h>
1556051948SVladimir Oltean #include <linux/pci.h>
1656051948SVladimir Oltean #include <linux/of.h>
17fc411eaaSVladimir Oltean #include <net/pkt_sched.h>
1856051948SVladimir Oltean #include <net/dsa.h>
1956051948SVladimir Oltean #include "felix.h"
2056051948SVladimir Oltean 
2156051948SVladimir Oltean static enum dsa_tag_protocol felix_get_tag_protocol(struct dsa_switch *ds,
224d776482SFlorian Fainelli 						    int port,
234d776482SFlorian Fainelli 						    enum dsa_tag_protocol mp)
2456051948SVladimir Oltean {
2556051948SVladimir Oltean 	return DSA_TAG_PROTO_OCELOT;
2656051948SVladimir Oltean }
2756051948SVladimir Oltean 
2856051948SVladimir Oltean static int felix_set_ageing_time(struct dsa_switch *ds,
2956051948SVladimir Oltean 				 unsigned int ageing_time)
3056051948SVladimir Oltean {
3156051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
3256051948SVladimir Oltean 
3356051948SVladimir Oltean 	ocelot_set_ageing_time(ocelot, ageing_time);
3456051948SVladimir Oltean 
3556051948SVladimir Oltean 	return 0;
3656051948SVladimir Oltean }
3756051948SVladimir Oltean 
3856051948SVladimir Oltean static int felix_fdb_dump(struct dsa_switch *ds, int port,
3956051948SVladimir Oltean 			  dsa_fdb_dump_cb_t *cb, void *data)
4056051948SVladimir Oltean {
4156051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
4256051948SVladimir Oltean 
4356051948SVladimir Oltean 	return ocelot_fdb_dump(ocelot, port, cb, data);
4456051948SVladimir Oltean }
4556051948SVladimir Oltean 
4656051948SVladimir Oltean static int felix_fdb_add(struct dsa_switch *ds, int port,
4756051948SVladimir Oltean 			 const unsigned char *addr, u16 vid)
4856051948SVladimir Oltean {
4956051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
5056051948SVladimir Oltean 
5187b0f983SVladimir Oltean 	return ocelot_fdb_add(ocelot, port, addr, vid);
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 
62209edf95SVladimir Oltean /* This callback needs to be present */
63209edf95SVladimir Oltean static int felix_mdb_prepare(struct dsa_switch *ds, int port,
64209edf95SVladimir Oltean 			     const struct switchdev_obj_port_mdb *mdb)
65209edf95SVladimir Oltean {
66209edf95SVladimir Oltean 	return 0;
67209edf95SVladimir Oltean }
68209edf95SVladimir Oltean 
69209edf95SVladimir Oltean static void felix_mdb_add(struct dsa_switch *ds, int port,
70209edf95SVladimir Oltean 			  const struct switchdev_obj_port_mdb *mdb)
71209edf95SVladimir Oltean {
72209edf95SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
73209edf95SVladimir Oltean 
74209edf95SVladimir Oltean 	ocelot_port_mdb_add(ocelot, port, mdb);
75209edf95SVladimir Oltean }
76209edf95SVladimir Oltean 
77209edf95SVladimir Oltean static int felix_mdb_del(struct dsa_switch *ds, int port,
78209edf95SVladimir Oltean 			 const struct switchdev_obj_port_mdb *mdb)
79209edf95SVladimir Oltean {
80209edf95SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
81209edf95SVladimir Oltean 
82209edf95SVladimir Oltean 	return ocelot_port_mdb_del(ocelot, port, mdb);
83209edf95SVladimir Oltean }
84209edf95SVladimir Oltean 
8556051948SVladimir Oltean static void felix_bridge_stp_state_set(struct dsa_switch *ds, int port,
8656051948SVladimir Oltean 				       u8 state)
8756051948SVladimir Oltean {
8856051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
8956051948SVladimir Oltean 
9056051948SVladimir Oltean 	return ocelot_bridge_stp_state_set(ocelot, port, state);
9156051948SVladimir Oltean }
9256051948SVladimir Oltean 
9356051948SVladimir Oltean static int felix_bridge_join(struct dsa_switch *ds, int port,
9456051948SVladimir Oltean 			     struct net_device *br)
9556051948SVladimir Oltean {
9656051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
9756051948SVladimir Oltean 
9856051948SVladimir Oltean 	return ocelot_port_bridge_join(ocelot, port, br);
9956051948SVladimir Oltean }
10056051948SVladimir Oltean 
10156051948SVladimir Oltean static void felix_bridge_leave(struct dsa_switch *ds, int port,
10256051948SVladimir Oltean 			       struct net_device *br)
10356051948SVladimir Oltean {
10456051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
10556051948SVladimir Oltean 
10656051948SVladimir Oltean 	ocelot_port_bridge_leave(ocelot, port, br);
10756051948SVladimir Oltean }
10856051948SVladimir Oltean 
10956051948SVladimir Oltean /* This callback needs to be present */
11056051948SVladimir Oltean static int felix_vlan_prepare(struct dsa_switch *ds, int port,
11156051948SVladimir Oltean 			      const struct switchdev_obj_port_vlan *vlan)
11256051948SVladimir Oltean {
11356051948SVladimir Oltean 	return 0;
11456051948SVladimir Oltean }
11556051948SVladimir Oltean 
11656051948SVladimir Oltean static int felix_vlan_filtering(struct dsa_switch *ds, int port, bool enabled)
11756051948SVladimir Oltean {
11856051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
11956051948SVladimir Oltean 
12056051948SVladimir Oltean 	ocelot_port_vlan_filtering(ocelot, port, enabled);
12156051948SVladimir Oltean 
12256051948SVladimir Oltean 	return 0;
12356051948SVladimir Oltean }
12456051948SVladimir Oltean 
12556051948SVladimir Oltean static void felix_vlan_add(struct dsa_switch *ds, int port,
12656051948SVladimir Oltean 			   const struct switchdev_obj_port_vlan *vlan)
12756051948SVladimir Oltean {
12856051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
129183be6f9SVladimir Oltean 	u16 flags = vlan->flags;
13056051948SVladimir Oltean 	u16 vid;
13156051948SVladimir Oltean 	int err;
13256051948SVladimir Oltean 
133183be6f9SVladimir Oltean 	if (dsa_is_cpu_port(ds, port))
134183be6f9SVladimir Oltean 		flags &= ~BRIDGE_VLAN_INFO_UNTAGGED;
135183be6f9SVladimir Oltean 
13656051948SVladimir Oltean 	for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) {
13756051948SVladimir Oltean 		err = ocelot_vlan_add(ocelot, port, vid,
138183be6f9SVladimir Oltean 				      flags & BRIDGE_VLAN_INFO_PVID,
139183be6f9SVladimir Oltean 				      flags & BRIDGE_VLAN_INFO_UNTAGGED);
14056051948SVladimir Oltean 		if (err) {
14156051948SVladimir Oltean 			dev_err(ds->dev, "Failed to add VLAN %d to port %d: %d\n",
14256051948SVladimir Oltean 				vid, port, err);
14356051948SVladimir Oltean 			return;
14456051948SVladimir Oltean 		}
14556051948SVladimir Oltean 	}
14656051948SVladimir Oltean }
14756051948SVladimir Oltean 
14856051948SVladimir Oltean static int felix_vlan_del(struct dsa_switch *ds, int port,
14956051948SVladimir Oltean 			  const struct switchdev_obj_port_vlan *vlan)
15056051948SVladimir Oltean {
15156051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
15256051948SVladimir Oltean 	u16 vid;
15356051948SVladimir Oltean 	int err;
15456051948SVladimir Oltean 
15556051948SVladimir Oltean 	for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) {
15656051948SVladimir Oltean 		err = ocelot_vlan_del(ocelot, port, vid);
15756051948SVladimir Oltean 		if (err) {
15856051948SVladimir Oltean 			dev_err(ds->dev, "Failed to remove VLAN %d from port %d: %d\n",
15956051948SVladimir Oltean 				vid, port, err);
16056051948SVladimir Oltean 			return err;
16156051948SVladimir Oltean 		}
16256051948SVladimir Oltean 	}
16356051948SVladimir Oltean 	return 0;
16456051948SVladimir Oltean }
16556051948SVladimir Oltean 
16656051948SVladimir Oltean static int felix_port_enable(struct dsa_switch *ds, int port,
16756051948SVladimir Oltean 			     struct phy_device *phy)
16856051948SVladimir Oltean {
16956051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
17056051948SVladimir Oltean 
17156051948SVladimir Oltean 	ocelot_port_enable(ocelot, port, phy);
17256051948SVladimir Oltean 
17356051948SVladimir Oltean 	return 0;
17456051948SVladimir Oltean }
17556051948SVladimir Oltean 
17656051948SVladimir Oltean static void felix_port_disable(struct dsa_switch *ds, int port)
17756051948SVladimir Oltean {
17856051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
17956051948SVladimir Oltean 
18056051948SVladimir Oltean 	return ocelot_port_disable(ocelot, port);
18156051948SVladimir Oltean }
18256051948SVladimir Oltean 
183bdeced75SVladimir Oltean static void felix_phylink_validate(struct dsa_switch *ds, int port,
184bdeced75SVladimir Oltean 				   unsigned long *supported,
185bdeced75SVladimir Oltean 				   struct phylink_link_state *state)
186bdeced75SVladimir Oltean {
187bdeced75SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
188bdeced75SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
189bdeced75SVladimir Oltean 	__ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, };
190bdeced75SVladimir Oltean 
191bdeced75SVladimir Oltean 	if (state->interface != PHY_INTERFACE_MODE_NA &&
192bdeced75SVladimir Oltean 	    state->interface != ocelot_port->phy_mode) {
193bdeced75SVladimir Oltean 		bitmap_zero(supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
194bdeced75SVladimir Oltean 		return;
195bdeced75SVladimir Oltean 	}
196bdeced75SVladimir Oltean 
197bdeced75SVladimir Oltean 	/* No half-duplex. */
198bdeced75SVladimir Oltean 	phylink_set_port_modes(mask);
199bdeced75SVladimir Oltean 	phylink_set(mask, Autoneg);
200bdeced75SVladimir Oltean 	phylink_set(mask, Pause);
201bdeced75SVladimir Oltean 	phylink_set(mask, Asym_Pause);
202bdeced75SVladimir Oltean 	phylink_set(mask, 10baseT_Full);
203bdeced75SVladimir Oltean 	phylink_set(mask, 100baseT_Full);
204bdeced75SVladimir Oltean 	phylink_set(mask, 1000baseT_Full);
20574984a19SAlex Marginean 
20628a134f5SVladimir Oltean 	if (state->interface == PHY_INTERFACE_MODE_INTERNAL ||
207bdeced75SVladimir Oltean 	    state->interface == PHY_INTERFACE_MODE_2500BASEX ||
208bdeced75SVladimir Oltean 	    state->interface == PHY_INTERFACE_MODE_USXGMII) {
209bdeced75SVladimir Oltean 		phylink_set(mask, 2500baseT_Full);
210bdeced75SVladimir Oltean 		phylink_set(mask, 2500baseX_Full);
211bdeced75SVladimir Oltean 	}
212bdeced75SVladimir Oltean 
213bdeced75SVladimir Oltean 	bitmap_and(supported, supported, mask,
214bdeced75SVladimir Oltean 		   __ETHTOOL_LINK_MODE_MASK_NBITS);
215bdeced75SVladimir Oltean 	bitmap_and(state->advertising, state->advertising, mask,
216bdeced75SVladimir Oltean 		   __ETHTOOL_LINK_MODE_MASK_NBITS);
217bdeced75SVladimir Oltean }
218bdeced75SVladimir Oltean 
219bdeced75SVladimir Oltean static int felix_phylink_mac_pcs_get_state(struct dsa_switch *ds, int port,
220bdeced75SVladimir Oltean 					   struct phylink_link_state *state)
221bdeced75SVladimir Oltean {
222bdeced75SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
223bdeced75SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
224bdeced75SVladimir Oltean 
225bdeced75SVladimir Oltean 	if (felix->info->pcs_link_state)
226bdeced75SVladimir Oltean 		felix->info->pcs_link_state(ocelot, port, state);
227bdeced75SVladimir Oltean 
228bdeced75SVladimir Oltean 	return 0;
229bdeced75SVladimir Oltean }
230bdeced75SVladimir Oltean 
231bdeced75SVladimir Oltean static void felix_phylink_mac_config(struct dsa_switch *ds, int port,
232bdeced75SVladimir Oltean 				     unsigned int link_an_mode,
233bdeced75SVladimir Oltean 				     const struct phylink_link_state *state)
234bdeced75SVladimir Oltean {
235bdeced75SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
236bdeced75SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
237bdeced75SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
238bdeced75SVladimir Oltean 	u32 mac_fc_cfg;
239bdeced75SVladimir Oltean 
240bdeced75SVladimir Oltean 	/* Take port out of reset by clearing the MAC_TX_RST, MAC_RX_RST and
241bdeced75SVladimir Oltean 	 * PORT_RST bits in CLOCK_CFG
242bdeced75SVladimir Oltean 	 */
243bdeced75SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_CLOCK_CFG_LINK_SPEED(state->speed),
244bdeced75SVladimir Oltean 			   DEV_CLOCK_CFG);
245bdeced75SVladimir Oltean 
246bdeced75SVladimir Oltean 	/* Flow control. Link speed is only used here to evaluate the time
247bdeced75SVladimir Oltean 	 * specification in incoming pause frames.
248bdeced75SVladimir Oltean 	 */
249bdeced75SVladimir Oltean 	mac_fc_cfg = SYS_MAC_FC_CFG_FC_LINK_SPEED(state->speed);
250f3660937SAlex Marginean 
251f3660937SAlex Marginean 	/* handle Rx pause in all cases, with 2500base-X this is used for rate
252f3660937SAlex Marginean 	 * adaptation.
253f3660937SAlex Marginean 	 */
254bdeced75SVladimir Oltean 	mac_fc_cfg |= SYS_MAC_FC_CFG_RX_FC_ENA;
255f3660937SAlex Marginean 
256bdeced75SVladimir Oltean 	if (state->pause & MLO_PAUSE_TX)
257bdeced75SVladimir Oltean 		mac_fc_cfg |= SYS_MAC_FC_CFG_TX_FC_ENA |
258bdeced75SVladimir Oltean 			      SYS_MAC_FC_CFG_PAUSE_VAL_CFG(0xffff) |
259bdeced75SVladimir Oltean 			      SYS_MAC_FC_CFG_FC_LATENCY_CFG(0x7) |
260bdeced75SVladimir Oltean 			      SYS_MAC_FC_CFG_ZERO_PAUSE_ENA;
261bdeced75SVladimir Oltean 	ocelot_write_rix(ocelot, mac_fc_cfg, SYS_MAC_FC_CFG, port);
262bdeced75SVladimir Oltean 
263bdeced75SVladimir Oltean 	ocelot_write_rix(ocelot, 0, ANA_POL_FLOWC, port);
264bdeced75SVladimir Oltean 
265bdeced75SVladimir Oltean 	if (felix->info->pcs_init)
266bdeced75SVladimir Oltean 		felix->info->pcs_init(ocelot, port, link_an_mode, state);
267de143c0eSXiaoliang Yang 
268de143c0eSXiaoliang Yang 	if (felix->info->port_sched_speed_set)
269de143c0eSXiaoliang Yang 		felix->info->port_sched_speed_set(ocelot, port,
270de143c0eSXiaoliang Yang 						  state->speed);
271bdeced75SVladimir Oltean }
272bdeced75SVladimir Oltean 
273bdeced75SVladimir Oltean static void felix_phylink_mac_an_restart(struct dsa_switch *ds, int port)
274bdeced75SVladimir Oltean {
275bdeced75SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
276bdeced75SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
277bdeced75SVladimir Oltean 
278bdeced75SVladimir Oltean 	if (felix->info->pcs_an_restart)
279bdeced75SVladimir Oltean 		felix->info->pcs_an_restart(ocelot, port);
280bdeced75SVladimir Oltean }
281bdeced75SVladimir Oltean 
282bdeced75SVladimir Oltean static void felix_phylink_mac_link_down(struct dsa_switch *ds, int port,
283bdeced75SVladimir Oltean 					unsigned int link_an_mode,
284bdeced75SVladimir Oltean 					phy_interface_t interface)
285bdeced75SVladimir Oltean {
286bdeced75SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
287bdeced75SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
288bdeced75SVladimir Oltean 
289bdeced75SVladimir Oltean 	ocelot_port_writel(ocelot_port, 0, DEV_MAC_ENA_CFG);
290bdeced75SVladimir Oltean 	ocelot_rmw_rix(ocelot, 0, QSYS_SWITCH_PORT_MODE_PORT_ENA,
291bdeced75SVladimir Oltean 		       QSYS_SWITCH_PORT_MODE, port);
292bdeced75SVladimir Oltean }
293bdeced75SVladimir Oltean 
294bdeced75SVladimir Oltean static void felix_phylink_mac_link_up(struct dsa_switch *ds, int port,
295bdeced75SVladimir Oltean 				      unsigned int link_an_mode,
296bdeced75SVladimir Oltean 				      phy_interface_t interface,
2975b502a7bSRussell King 				      struct phy_device *phydev,
2985b502a7bSRussell King 				      int speed, int duplex,
2995b502a7bSRussell King 				      bool tx_pause, bool rx_pause)
300bdeced75SVladimir Oltean {
301bdeced75SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
302bdeced75SVladimir Oltean 	struct ocelot_port *ocelot_port = ocelot->ports[port];
303bdeced75SVladimir Oltean 
304bdeced75SVladimir Oltean 	/* Enable MAC module */
305bdeced75SVladimir Oltean 	ocelot_port_writel(ocelot_port, DEV_MAC_ENA_CFG_RX_ENA |
306bdeced75SVladimir Oltean 			   DEV_MAC_ENA_CFG_TX_ENA, DEV_MAC_ENA_CFG);
307bdeced75SVladimir Oltean 
308bdeced75SVladimir Oltean 	/* Enable receiving frames on the port, and activate auto-learning of
309bdeced75SVladimir Oltean 	 * MAC addresses.
310bdeced75SVladimir Oltean 	 */
311bdeced75SVladimir Oltean 	ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_LEARNAUTO |
312bdeced75SVladimir Oltean 			 ANA_PORT_PORT_CFG_RECV_ENA |
313bdeced75SVladimir Oltean 			 ANA_PORT_PORT_CFG_PORTID_VAL(port),
314bdeced75SVladimir Oltean 			 ANA_PORT_PORT_CFG, port);
315bdeced75SVladimir Oltean 
316bdeced75SVladimir Oltean 	/* Core: Enable port for frame transfer */
317bdeced75SVladimir Oltean 	ocelot_write_rix(ocelot, QSYS_SWITCH_PORT_MODE_INGRESS_DROP_MODE |
318bdeced75SVladimir Oltean 			 QSYS_SWITCH_PORT_MODE_SCH_NEXT_CFG(1) |
319bdeced75SVladimir Oltean 			 QSYS_SWITCH_PORT_MODE_PORT_ENA,
320bdeced75SVladimir Oltean 			 QSYS_SWITCH_PORT_MODE, port);
321bdeced75SVladimir Oltean }
322bdeced75SVladimir Oltean 
323bd2b3161SXiaoliang Yang static void felix_port_qos_map_init(struct ocelot *ocelot, int port)
324bd2b3161SXiaoliang Yang {
325bd2b3161SXiaoliang Yang 	int i;
326bd2b3161SXiaoliang Yang 
327bd2b3161SXiaoliang Yang 	ocelot_rmw_gix(ocelot,
328bd2b3161SXiaoliang Yang 		       ANA_PORT_QOS_CFG_QOS_PCP_ENA,
329bd2b3161SXiaoliang Yang 		       ANA_PORT_QOS_CFG_QOS_PCP_ENA,
330bd2b3161SXiaoliang Yang 		       ANA_PORT_QOS_CFG,
331bd2b3161SXiaoliang Yang 		       port);
332bd2b3161SXiaoliang Yang 
333bd2b3161SXiaoliang Yang 	for (i = 0; i < FELIX_NUM_TC * 2; i++) {
334bd2b3161SXiaoliang Yang 		ocelot_rmw_ix(ocelot,
335bd2b3161SXiaoliang Yang 			      (ANA_PORT_PCP_DEI_MAP_DP_PCP_DEI_VAL & i) |
336bd2b3161SXiaoliang Yang 			      ANA_PORT_PCP_DEI_MAP_QOS_PCP_DEI_VAL(i),
337bd2b3161SXiaoliang Yang 			      ANA_PORT_PCP_DEI_MAP_DP_PCP_DEI_VAL |
338bd2b3161SXiaoliang Yang 			      ANA_PORT_PCP_DEI_MAP_QOS_PCP_DEI_VAL_M,
339bd2b3161SXiaoliang Yang 			      ANA_PORT_PCP_DEI_MAP,
340bd2b3161SXiaoliang Yang 			      port, i);
341bd2b3161SXiaoliang Yang 	}
342bd2b3161SXiaoliang Yang }
343bd2b3161SXiaoliang Yang 
34456051948SVladimir Oltean static void felix_get_strings(struct dsa_switch *ds, int port,
34556051948SVladimir Oltean 			      u32 stringset, u8 *data)
34656051948SVladimir Oltean {
34756051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
34856051948SVladimir Oltean 
34956051948SVladimir Oltean 	return ocelot_get_strings(ocelot, port, stringset, data);
35056051948SVladimir Oltean }
35156051948SVladimir Oltean 
35256051948SVladimir Oltean static void felix_get_ethtool_stats(struct dsa_switch *ds, int port, u64 *data)
35356051948SVladimir Oltean {
35456051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
35556051948SVladimir Oltean 
35656051948SVladimir Oltean 	ocelot_get_ethtool_stats(ocelot, port, data);
35756051948SVladimir Oltean }
35856051948SVladimir Oltean 
35956051948SVladimir Oltean static int felix_get_sset_count(struct dsa_switch *ds, int port, int sset)
36056051948SVladimir Oltean {
36156051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
36256051948SVladimir Oltean 
36356051948SVladimir Oltean 	return ocelot_get_sset_count(ocelot, port, sset);
36456051948SVladimir Oltean }
36556051948SVladimir Oltean 
36656051948SVladimir Oltean static int felix_get_ts_info(struct dsa_switch *ds, int port,
36756051948SVladimir Oltean 			     struct ethtool_ts_info *info)
36856051948SVladimir Oltean {
36956051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
37056051948SVladimir Oltean 
37156051948SVladimir Oltean 	return ocelot_get_ts_info(ocelot, port, info);
37256051948SVladimir Oltean }
37356051948SVladimir Oltean 
374bdeced75SVladimir Oltean static int felix_parse_ports_node(struct felix *felix,
375bdeced75SVladimir Oltean 				  struct device_node *ports_node,
376bdeced75SVladimir Oltean 				  phy_interface_t *port_phy_modes)
377bdeced75SVladimir Oltean {
378bdeced75SVladimir Oltean 	struct ocelot *ocelot = &felix->ocelot;
379bdeced75SVladimir Oltean 	struct device *dev = felix->ocelot.dev;
380bdeced75SVladimir Oltean 	struct device_node *child;
381bdeced75SVladimir Oltean 
38237fe45adSVladimir Oltean 	for_each_available_child_of_node(ports_node, child) {
383bdeced75SVladimir Oltean 		phy_interface_t phy_mode;
384bdeced75SVladimir Oltean 		u32 port;
385bdeced75SVladimir Oltean 		int err;
386bdeced75SVladimir Oltean 
387bdeced75SVladimir Oltean 		/* Get switch port number from DT */
388bdeced75SVladimir Oltean 		if (of_property_read_u32(child, "reg", &port) < 0) {
389bdeced75SVladimir Oltean 			dev_err(dev, "Port number not defined in device tree "
390bdeced75SVladimir Oltean 				"(property \"reg\")\n");
391bdeced75SVladimir Oltean 			of_node_put(child);
392bdeced75SVladimir Oltean 			return -ENODEV;
393bdeced75SVladimir Oltean 		}
394bdeced75SVladimir Oltean 
395bdeced75SVladimir Oltean 		/* Get PHY mode from DT */
396bdeced75SVladimir Oltean 		err = of_get_phy_mode(child, &phy_mode);
397bdeced75SVladimir Oltean 		if (err) {
398bdeced75SVladimir Oltean 			dev_err(dev, "Failed to read phy-mode or "
399bdeced75SVladimir Oltean 				"phy-interface-type property for port %d\n",
400bdeced75SVladimir Oltean 				port);
401bdeced75SVladimir Oltean 			of_node_put(child);
402bdeced75SVladimir Oltean 			return -ENODEV;
403bdeced75SVladimir Oltean 		}
404bdeced75SVladimir Oltean 
405bdeced75SVladimir Oltean 		err = felix->info->prevalidate_phy_mode(ocelot, port, phy_mode);
406bdeced75SVladimir Oltean 		if (err < 0) {
407bdeced75SVladimir Oltean 			dev_err(dev, "Unsupported PHY mode %s on port %d\n",
408bdeced75SVladimir Oltean 				phy_modes(phy_mode), port);
409bdeced75SVladimir Oltean 			return err;
410bdeced75SVladimir Oltean 		}
411bdeced75SVladimir Oltean 
412bdeced75SVladimir Oltean 		port_phy_modes[port] = phy_mode;
413bdeced75SVladimir Oltean 	}
414bdeced75SVladimir Oltean 
415bdeced75SVladimir Oltean 	return 0;
416bdeced75SVladimir Oltean }
417bdeced75SVladimir Oltean 
418bdeced75SVladimir Oltean static int felix_parse_dt(struct felix *felix, phy_interface_t *port_phy_modes)
419bdeced75SVladimir Oltean {
420bdeced75SVladimir Oltean 	struct device *dev = felix->ocelot.dev;
421bdeced75SVladimir Oltean 	struct device_node *switch_node;
422bdeced75SVladimir Oltean 	struct device_node *ports_node;
423bdeced75SVladimir Oltean 	int err;
424bdeced75SVladimir Oltean 
425bdeced75SVladimir Oltean 	switch_node = dev->of_node;
426bdeced75SVladimir Oltean 
427bdeced75SVladimir Oltean 	ports_node = of_get_child_by_name(switch_node, "ports");
428bdeced75SVladimir Oltean 	if (!ports_node) {
429bdeced75SVladimir Oltean 		dev_err(dev, "Incorrect bindings: absent \"ports\" node\n");
430bdeced75SVladimir Oltean 		return -ENODEV;
431bdeced75SVladimir Oltean 	}
432bdeced75SVladimir Oltean 
433bdeced75SVladimir Oltean 	err = felix_parse_ports_node(felix, ports_node, port_phy_modes);
434bdeced75SVladimir Oltean 	of_node_put(ports_node);
435bdeced75SVladimir Oltean 
436bdeced75SVladimir Oltean 	return err;
437bdeced75SVladimir Oltean }
438bdeced75SVladimir Oltean 
43956051948SVladimir Oltean static int felix_init_structs(struct felix *felix, int num_phys_ports)
44056051948SVladimir Oltean {
44156051948SVladimir Oltean 	struct ocelot *ocelot = &felix->ocelot;
442bdeced75SVladimir Oltean 	phy_interface_t *port_phy_modes;
443bdeced75SVladimir Oltean 	resource_size_t switch_base;
444b4024c9eSClaudiu Manoil 	struct resource res;
44556051948SVladimir Oltean 	int port, i, err;
44656051948SVladimir Oltean 
44756051948SVladimir Oltean 	ocelot->num_phys_ports = num_phys_ports;
44856051948SVladimir Oltean 	ocelot->ports = devm_kcalloc(ocelot->dev, num_phys_ports,
44956051948SVladimir Oltean 				     sizeof(struct ocelot_port *), GFP_KERNEL);
45056051948SVladimir Oltean 	if (!ocelot->ports)
45156051948SVladimir Oltean 		return -ENOMEM;
45256051948SVladimir Oltean 
45356051948SVladimir Oltean 	ocelot->map		= felix->info->map;
45456051948SVladimir Oltean 	ocelot->stats_layout	= felix->info->stats_layout;
45556051948SVladimir Oltean 	ocelot->num_stats	= felix->info->num_stats;
45656051948SVladimir Oltean 	ocelot->shared_queue_sz	= felix->info->shared_queue_sz;
45721ce7f3eSVladimir Oltean 	ocelot->num_mact_rows	= felix->info->num_mact_rows;
45807d985eeSVladimir Oltean 	ocelot->vcap_is2_keys	= felix->info->vcap_is2_keys;
45907d985eeSVladimir Oltean 	ocelot->vcap_is2_actions= felix->info->vcap_is2_actions;
46007d985eeSVladimir Oltean 	ocelot->vcap		= felix->info->vcap;
46156051948SVladimir Oltean 	ocelot->ops		= felix->info->ops;
46256051948SVladimir Oltean 
463bdeced75SVladimir Oltean 	port_phy_modes = kcalloc(num_phys_ports, sizeof(phy_interface_t),
464bdeced75SVladimir Oltean 				 GFP_KERNEL);
465bdeced75SVladimir Oltean 	if (!port_phy_modes)
466bdeced75SVladimir Oltean 		return -ENOMEM;
467bdeced75SVladimir Oltean 
468bdeced75SVladimir Oltean 	err = felix_parse_dt(felix, port_phy_modes);
469bdeced75SVladimir Oltean 	if (err) {
470bdeced75SVladimir Oltean 		kfree(port_phy_modes);
471bdeced75SVladimir Oltean 		return err;
472bdeced75SVladimir Oltean 	}
473bdeced75SVladimir Oltean 
474bdeced75SVladimir Oltean 	switch_base = pci_resource_start(felix->pdev,
475bdeced75SVladimir Oltean 					 felix->info->switch_pci_bar);
47656051948SVladimir Oltean 
47756051948SVladimir Oltean 	for (i = 0; i < TARGET_MAX; i++) {
47856051948SVladimir Oltean 		struct regmap *target;
47956051948SVladimir Oltean 
48056051948SVladimir Oltean 		if (!felix->info->target_io_res[i].name)
48156051948SVladimir Oltean 			continue;
48256051948SVladimir Oltean 
483b4024c9eSClaudiu Manoil 		memcpy(&res, &felix->info->target_io_res[i], sizeof(res));
484b4024c9eSClaudiu Manoil 		res.flags = IORESOURCE_MEM;
485b4024c9eSClaudiu Manoil 		res.start += switch_base;
486b4024c9eSClaudiu Manoil 		res.end += switch_base;
48756051948SVladimir Oltean 
488b4024c9eSClaudiu Manoil 		target = ocelot_regmap_init(ocelot, &res);
48956051948SVladimir Oltean 		if (IS_ERR(target)) {
49056051948SVladimir Oltean 			dev_err(ocelot->dev,
49156051948SVladimir Oltean 				"Failed to map device memory space\n");
492bdeced75SVladimir Oltean 			kfree(port_phy_modes);
49356051948SVladimir Oltean 			return PTR_ERR(target);
49456051948SVladimir Oltean 		}
49556051948SVladimir Oltean 
49656051948SVladimir Oltean 		ocelot->targets[i] = target;
49756051948SVladimir Oltean 	}
49856051948SVladimir Oltean 
49956051948SVladimir Oltean 	err = ocelot_regfields_init(ocelot, felix->info->regfields);
50056051948SVladimir Oltean 	if (err) {
50156051948SVladimir Oltean 		dev_err(ocelot->dev, "failed to init reg fields map\n");
502bdeced75SVladimir Oltean 		kfree(port_phy_modes);
50356051948SVladimir Oltean 		return err;
50456051948SVladimir Oltean 	}
50556051948SVladimir Oltean 
50656051948SVladimir Oltean 	for (port = 0; port < num_phys_ports; port++) {
50756051948SVladimir Oltean 		struct ocelot_port *ocelot_port;
50856051948SVladimir Oltean 		void __iomem *port_regs;
50956051948SVladimir Oltean 
51056051948SVladimir Oltean 		ocelot_port = devm_kzalloc(ocelot->dev,
51156051948SVladimir Oltean 					   sizeof(struct ocelot_port),
51256051948SVladimir Oltean 					   GFP_KERNEL);
51356051948SVladimir Oltean 		if (!ocelot_port) {
51456051948SVladimir Oltean 			dev_err(ocelot->dev,
51556051948SVladimir Oltean 				"failed to allocate port memory\n");
516bdeced75SVladimir Oltean 			kfree(port_phy_modes);
51756051948SVladimir Oltean 			return -ENOMEM;
51856051948SVladimir Oltean 		}
51956051948SVladimir Oltean 
520b4024c9eSClaudiu Manoil 		memcpy(&res, &felix->info->port_io_res[port], sizeof(res));
521b4024c9eSClaudiu Manoil 		res.flags = IORESOURCE_MEM;
522b4024c9eSClaudiu Manoil 		res.start += switch_base;
523b4024c9eSClaudiu Manoil 		res.end += switch_base;
52456051948SVladimir Oltean 
525b4024c9eSClaudiu Manoil 		port_regs = devm_ioremap_resource(ocelot->dev, &res);
52656051948SVladimir Oltean 		if (IS_ERR(port_regs)) {
52756051948SVladimir Oltean 			dev_err(ocelot->dev,
52856051948SVladimir Oltean 				"failed to map registers for port %d\n", port);
529bdeced75SVladimir Oltean 			kfree(port_phy_modes);
53056051948SVladimir Oltean 			return PTR_ERR(port_regs);
53156051948SVladimir Oltean 		}
53256051948SVladimir Oltean 
533bdeced75SVladimir Oltean 		ocelot_port->phy_mode = port_phy_modes[port];
53456051948SVladimir Oltean 		ocelot_port->ocelot = ocelot;
53556051948SVladimir Oltean 		ocelot_port->regs = port_regs;
53656051948SVladimir Oltean 		ocelot->ports[port] = ocelot_port;
53756051948SVladimir Oltean 	}
53856051948SVladimir Oltean 
539bdeced75SVladimir Oltean 	kfree(port_phy_modes);
540bdeced75SVladimir Oltean 
541bdeced75SVladimir Oltean 	if (felix->info->mdio_bus_alloc) {
542bdeced75SVladimir Oltean 		err = felix->info->mdio_bus_alloc(ocelot);
543bdeced75SVladimir Oltean 		if (err < 0)
544bdeced75SVladimir Oltean 			return err;
545bdeced75SVladimir Oltean 	}
546bdeced75SVladimir Oltean 
54756051948SVladimir Oltean 	return 0;
54856051948SVladimir Oltean }
54956051948SVladimir Oltean 
5502b49d128SYangbo Lu static struct ptp_clock_info ocelot_ptp_clock_info = {
5512b49d128SYangbo Lu 	.owner		= THIS_MODULE,
5522b49d128SYangbo Lu 	.name		= "felix ptp",
5532b49d128SYangbo Lu 	.max_adj	= 0x7fffffff,
5542b49d128SYangbo Lu 	.n_alarm	= 0,
5552b49d128SYangbo Lu 	.n_ext_ts	= 0,
5565287be40SYangbo Lu 	.n_per_out	= OCELOT_PTP_PINS_NUM,
5575287be40SYangbo Lu 	.n_pins		= OCELOT_PTP_PINS_NUM,
5582b49d128SYangbo Lu 	.pps		= 0,
5592b49d128SYangbo Lu 	.gettime64	= ocelot_ptp_gettime64,
5602b49d128SYangbo Lu 	.settime64	= ocelot_ptp_settime64,
5612b49d128SYangbo Lu 	.adjtime	= ocelot_ptp_adjtime,
5622b49d128SYangbo Lu 	.adjfine	= ocelot_ptp_adjfine,
5635287be40SYangbo Lu 	.verify		= ocelot_ptp_verify,
5645287be40SYangbo Lu 	.enable		= ocelot_ptp_enable,
5652b49d128SYangbo Lu };
5662b49d128SYangbo Lu 
56756051948SVladimir Oltean /* Hardware initialization done here so that we can allocate structures with
56856051948SVladimir Oltean  * devm without fear of dsa_register_switch returning -EPROBE_DEFER and causing
56956051948SVladimir Oltean  * us to allocate structures twice (leak memory) and map PCI memory twice
57056051948SVladimir Oltean  * (which will not work).
57156051948SVladimir Oltean  */
57256051948SVladimir Oltean static int felix_setup(struct dsa_switch *ds)
57356051948SVladimir Oltean {
57456051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
57556051948SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
57656051948SVladimir Oltean 	int port, err;
5773c7b51bdSXiaoliang Yang 	int tc;
57856051948SVladimir Oltean 
57956051948SVladimir Oltean 	err = felix_init_structs(felix, ds->num_ports);
58056051948SVladimir Oltean 	if (err)
58156051948SVladimir Oltean 		return err;
58256051948SVladimir Oltean 
58356051948SVladimir Oltean 	ocelot_init(ocelot);
5842b49d128SYangbo Lu 	if (ocelot->ptp) {
5852b49d128SYangbo Lu 		err = ocelot_init_timestamp(ocelot, &ocelot_ptp_clock_info);
5862b49d128SYangbo Lu 		if (err) {
5872b49d128SYangbo Lu 			dev_err(ocelot->dev,
5882b49d128SYangbo Lu 				"Timestamp initialization failed\n");
5892b49d128SYangbo Lu 			ocelot->ptp = 0;
5902b49d128SYangbo Lu 		}
5912b49d128SYangbo Lu 	}
59256051948SVladimir Oltean 
59356051948SVladimir Oltean 	for (port = 0; port < ds->num_ports; port++) {
59456051948SVladimir Oltean 		ocelot_init_port(ocelot, port);
59556051948SVladimir Oltean 
59669df578cSVladimir Oltean 		/* Bring up the CPU port module and configure the NPI port */
597b8fc7177SVladimir Oltean 		if (dsa_is_cpu_port(ds, port))
59869df578cSVladimir Oltean 			ocelot_configure_cpu(ocelot, port,
59956051948SVladimir Oltean 					     OCELOT_TAG_PREFIX_NONE,
60056051948SVladimir Oltean 					     OCELOT_TAG_PREFIX_LONG);
601bd2b3161SXiaoliang Yang 
602bd2b3161SXiaoliang Yang 		/* Set the default QoS Classification based on PCP and DEI
603bd2b3161SXiaoliang Yang 		 * bits of vlan tag.
604bd2b3161SXiaoliang Yang 		 */
605bd2b3161SXiaoliang Yang 		felix_port_qos_map_init(ocelot, port);
60656051948SVladimir Oltean 	}
60756051948SVladimir Oltean 
6081cf3299bSVladimir Oltean 	/* Include the CPU port module in the forwarding mask for unknown
6091cf3299bSVladimir Oltean 	 * unicast - the hardware default value for ANA_FLOODING_FLD_UNICAST
6101cf3299bSVladimir Oltean 	 * excludes BIT(ocelot->num_phys_ports), and so does ocelot_init, since
6111cf3299bSVladimir Oltean 	 * Ocelot relies on whitelisting MAC addresses towards PGID_CPU.
6121cf3299bSVladimir Oltean 	 */
6131cf3299bSVladimir Oltean 	ocelot_write_rix(ocelot,
6141cf3299bSVladimir Oltean 			 ANA_PGID_PGID_PGID(GENMASK(ocelot->num_phys_ports, 0)),
6151cf3299bSVladimir Oltean 			 ANA_PGID_PGID, PGID_UC);
6163c7b51bdSXiaoliang Yang 	/* Setup the per-traffic class flooding PGIDs */
6173c7b51bdSXiaoliang Yang 	for (tc = 0; tc < FELIX_NUM_TC; tc++)
6183c7b51bdSXiaoliang Yang 		ocelot_write_rix(ocelot, ANA_FLOODING_FLD_MULTICAST(PGID_MC) |
6193c7b51bdSXiaoliang Yang 				 ANA_FLOODING_FLD_BROADCAST(PGID_MC) |
6203c7b51bdSXiaoliang Yang 				 ANA_FLOODING_FLD_UNICAST(PGID_UC),
6213c7b51bdSXiaoliang Yang 				 ANA_FLOODING, tc);
6221cf3299bSVladimir Oltean 
6230b912fc9SVladimir Oltean 	ds->mtu_enforcement_ingress = true;
624626a8323SVladimir Oltean 	ds->configure_vlan_while_not_filtering = true;
625bdeced75SVladimir Oltean 	/* It looks like the MAC/PCS interrupt register - PM0_IEVENT (0x8040)
626bdeced75SVladimir Oltean 	 * isn't instantiated for the Felix PF.
627bdeced75SVladimir Oltean 	 * In-band AN may take a few ms to complete, so we need to poll.
628bdeced75SVladimir Oltean 	 */
629bdeced75SVladimir Oltean 	ds->pcs_poll = true;
630bdeced75SVladimir Oltean 
63156051948SVladimir Oltean 	return 0;
63256051948SVladimir Oltean }
63356051948SVladimir Oltean 
63456051948SVladimir Oltean static void felix_teardown(struct dsa_switch *ds)
63556051948SVladimir Oltean {
63656051948SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
637bdeced75SVladimir Oltean 	struct felix *felix = ocelot_to_felix(ocelot);
638bdeced75SVladimir Oltean 
639bdeced75SVladimir Oltean 	if (felix->info->mdio_bus_free)
640bdeced75SVladimir Oltean 		felix->info->mdio_bus_free(ocelot);
64156051948SVladimir Oltean 
6422b49d128SYangbo Lu 	ocelot_deinit_timestamp(ocelot);
64356051948SVladimir Oltean 	/* stop workqueue thread */
64456051948SVladimir Oltean 	ocelot_deinit(ocelot);
64556051948SVladimir Oltean }
64656051948SVladimir Oltean 
647c0bcf537SYangbo Lu static int felix_hwtstamp_get(struct dsa_switch *ds, int port,
648c0bcf537SYangbo Lu 			      struct ifreq *ifr)
649c0bcf537SYangbo Lu {
650c0bcf537SYangbo Lu 	struct ocelot *ocelot = ds->priv;
651c0bcf537SYangbo Lu 
652c0bcf537SYangbo Lu 	return ocelot_hwstamp_get(ocelot, port, ifr);
653c0bcf537SYangbo Lu }
654c0bcf537SYangbo Lu 
655c0bcf537SYangbo Lu static int felix_hwtstamp_set(struct dsa_switch *ds, int port,
656c0bcf537SYangbo Lu 			      struct ifreq *ifr)
657c0bcf537SYangbo Lu {
658c0bcf537SYangbo Lu 	struct ocelot *ocelot = ds->priv;
659c0bcf537SYangbo Lu 
660c0bcf537SYangbo Lu 	return ocelot_hwstamp_set(ocelot, port, ifr);
661c0bcf537SYangbo Lu }
662c0bcf537SYangbo Lu 
663c0bcf537SYangbo Lu static bool felix_rxtstamp(struct dsa_switch *ds, int port,
664c0bcf537SYangbo Lu 			   struct sk_buff *skb, unsigned int type)
665c0bcf537SYangbo Lu {
666c0bcf537SYangbo Lu 	struct skb_shared_hwtstamps *shhwtstamps;
667c0bcf537SYangbo Lu 	struct ocelot *ocelot = ds->priv;
668c0bcf537SYangbo Lu 	u8 *extraction = skb->data - ETH_HLEN - OCELOT_TAG_LEN;
669c0bcf537SYangbo Lu 	u32 tstamp_lo, tstamp_hi;
670c0bcf537SYangbo Lu 	struct timespec64 ts;
671c0bcf537SYangbo Lu 	u64 tstamp, val;
672c0bcf537SYangbo Lu 
673c0bcf537SYangbo Lu 	ocelot_ptp_gettime64(&ocelot->ptp_info, &ts);
674c0bcf537SYangbo Lu 	tstamp = ktime_set(ts.tv_sec, ts.tv_nsec);
675c0bcf537SYangbo Lu 
676c0bcf537SYangbo Lu 	packing(extraction, &val,  116, 85, OCELOT_TAG_LEN, UNPACK, 0);
677c0bcf537SYangbo Lu 	tstamp_lo = (u32)val;
678c0bcf537SYangbo Lu 
679c0bcf537SYangbo Lu 	tstamp_hi = tstamp >> 32;
680c0bcf537SYangbo Lu 	if ((tstamp & 0xffffffff) < tstamp_lo)
681c0bcf537SYangbo Lu 		tstamp_hi--;
682c0bcf537SYangbo Lu 
683c0bcf537SYangbo Lu 	tstamp = ((u64)tstamp_hi << 32) | tstamp_lo;
684c0bcf537SYangbo Lu 
685c0bcf537SYangbo Lu 	shhwtstamps = skb_hwtstamps(skb);
686c0bcf537SYangbo Lu 	memset(shhwtstamps, 0, sizeof(struct skb_shared_hwtstamps));
687c0bcf537SYangbo Lu 	shhwtstamps->hwtstamp = tstamp;
688c0bcf537SYangbo Lu 	return false;
689c0bcf537SYangbo Lu }
690c0bcf537SYangbo Lu 
6913243e04aSChen Wandun static bool felix_txtstamp(struct dsa_switch *ds, int port,
692c0bcf537SYangbo Lu 			   struct sk_buff *clone, unsigned int type)
693c0bcf537SYangbo Lu {
694c0bcf537SYangbo Lu 	struct ocelot *ocelot = ds->priv;
695c0bcf537SYangbo Lu 	struct ocelot_port *ocelot_port = ocelot->ports[port];
696c0bcf537SYangbo Lu 
697c0bcf537SYangbo Lu 	if (!ocelot_port_add_txtstamp_skb(ocelot_port, clone))
698c0bcf537SYangbo Lu 		return true;
699c0bcf537SYangbo Lu 
700c0bcf537SYangbo Lu 	return false;
701c0bcf537SYangbo Lu }
702c0bcf537SYangbo Lu 
7030b912fc9SVladimir Oltean static int felix_change_mtu(struct dsa_switch *ds, int port, int new_mtu)
7040b912fc9SVladimir Oltean {
7050b912fc9SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
7060b912fc9SVladimir Oltean 
7070b912fc9SVladimir Oltean 	ocelot_port_set_maxlen(ocelot, port, new_mtu);
7080b912fc9SVladimir Oltean 
7090b912fc9SVladimir Oltean 	return 0;
7100b912fc9SVladimir Oltean }
7110b912fc9SVladimir Oltean 
7120b912fc9SVladimir Oltean static int felix_get_max_mtu(struct dsa_switch *ds, int port)
7130b912fc9SVladimir Oltean {
7140b912fc9SVladimir Oltean 	struct ocelot *ocelot = ds->priv;
7150b912fc9SVladimir Oltean 
7160b912fc9SVladimir Oltean 	return ocelot_get_max_mtu(ocelot, port);
7170b912fc9SVladimir Oltean }
7180b912fc9SVladimir Oltean 
71907d985eeSVladimir Oltean static int felix_cls_flower_add(struct dsa_switch *ds, int port,
72007d985eeSVladimir Oltean 				struct flow_cls_offload *cls, bool ingress)
72107d985eeSVladimir Oltean {
72207d985eeSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
72307d985eeSVladimir Oltean 
72407d985eeSVladimir Oltean 	return ocelot_cls_flower_replace(ocelot, port, cls, ingress);
72507d985eeSVladimir Oltean }
72607d985eeSVladimir Oltean 
72707d985eeSVladimir Oltean static int felix_cls_flower_del(struct dsa_switch *ds, int port,
72807d985eeSVladimir Oltean 				struct flow_cls_offload *cls, bool ingress)
72907d985eeSVladimir Oltean {
73007d985eeSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
73107d985eeSVladimir Oltean 
73207d985eeSVladimir Oltean 	return ocelot_cls_flower_destroy(ocelot, port, cls, ingress);
73307d985eeSVladimir Oltean }
73407d985eeSVladimir Oltean 
73507d985eeSVladimir Oltean static int felix_cls_flower_stats(struct dsa_switch *ds, int port,
73607d985eeSVladimir Oltean 				  struct flow_cls_offload *cls, bool ingress)
73707d985eeSVladimir Oltean {
73807d985eeSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
73907d985eeSVladimir Oltean 
74007d985eeSVladimir Oltean 	return ocelot_cls_flower_stats(ocelot, port, cls, ingress);
74107d985eeSVladimir Oltean }
74207d985eeSVladimir Oltean 
743fc411eaaSVladimir Oltean static int felix_port_policer_add(struct dsa_switch *ds, int port,
744fc411eaaSVladimir Oltean 				  struct dsa_mall_policer_tc_entry *policer)
745fc411eaaSVladimir Oltean {
746fc411eaaSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
747fc411eaaSVladimir Oltean 	struct ocelot_policer pol = {
748fc411eaaSVladimir Oltean 		.rate = div_u64(policer->rate_bytes_per_sec, 1000) * 8,
749*5f035af7SPo Liu 		.burst = policer->burst,
750fc411eaaSVladimir Oltean 	};
751fc411eaaSVladimir Oltean 
752fc411eaaSVladimir Oltean 	return ocelot_port_policer_add(ocelot, port, &pol);
753fc411eaaSVladimir Oltean }
754fc411eaaSVladimir Oltean 
755fc411eaaSVladimir Oltean static void felix_port_policer_del(struct dsa_switch *ds, int port)
756fc411eaaSVladimir Oltean {
757fc411eaaSVladimir Oltean 	struct ocelot *ocelot = ds->priv;
758fc411eaaSVladimir Oltean 
759fc411eaaSVladimir Oltean 	ocelot_port_policer_del(ocelot, port);
760fc411eaaSVladimir Oltean }
761fc411eaaSVladimir Oltean 
762de143c0eSXiaoliang Yang static int felix_port_setup_tc(struct dsa_switch *ds, int port,
763de143c0eSXiaoliang Yang 			       enum tc_setup_type type,
764de143c0eSXiaoliang Yang 			       void *type_data)
765de143c0eSXiaoliang Yang {
766de143c0eSXiaoliang Yang 	struct ocelot *ocelot = ds->priv;
767de143c0eSXiaoliang Yang 	struct felix *felix = ocelot_to_felix(ocelot);
768de143c0eSXiaoliang Yang 
769de143c0eSXiaoliang Yang 	if (felix->info->port_setup_tc)
770de143c0eSXiaoliang Yang 		return felix->info->port_setup_tc(ds, port, type, type_data);
771de143c0eSXiaoliang Yang 	else
772de143c0eSXiaoliang Yang 		return -EOPNOTSUPP;
773de143c0eSXiaoliang Yang }
774de143c0eSXiaoliang Yang 
77556051948SVladimir Oltean static const struct dsa_switch_ops felix_switch_ops = {
77656051948SVladimir Oltean 	.get_tag_protocol	= felix_get_tag_protocol,
77756051948SVladimir Oltean 	.setup			= felix_setup,
77856051948SVladimir Oltean 	.teardown		= felix_teardown,
77956051948SVladimir Oltean 	.set_ageing_time	= felix_set_ageing_time,
78056051948SVladimir Oltean 	.get_strings		= felix_get_strings,
78156051948SVladimir Oltean 	.get_ethtool_stats	= felix_get_ethtool_stats,
78256051948SVladimir Oltean 	.get_sset_count		= felix_get_sset_count,
78356051948SVladimir Oltean 	.get_ts_info		= felix_get_ts_info,
784bdeced75SVladimir Oltean 	.phylink_validate	= felix_phylink_validate,
785bdeced75SVladimir Oltean 	.phylink_mac_link_state	= felix_phylink_mac_pcs_get_state,
786bdeced75SVladimir Oltean 	.phylink_mac_config	= felix_phylink_mac_config,
787bdeced75SVladimir Oltean 	.phylink_mac_an_restart	= felix_phylink_mac_an_restart,
788bdeced75SVladimir Oltean 	.phylink_mac_link_down	= felix_phylink_mac_link_down,
789bdeced75SVladimir Oltean 	.phylink_mac_link_up	= felix_phylink_mac_link_up,
79056051948SVladimir Oltean 	.port_enable		= felix_port_enable,
79156051948SVladimir Oltean 	.port_disable		= felix_port_disable,
79256051948SVladimir Oltean 	.port_fdb_dump		= felix_fdb_dump,
79356051948SVladimir Oltean 	.port_fdb_add		= felix_fdb_add,
79456051948SVladimir Oltean 	.port_fdb_del		= felix_fdb_del,
795209edf95SVladimir Oltean 	.port_mdb_prepare	= felix_mdb_prepare,
796209edf95SVladimir Oltean 	.port_mdb_add		= felix_mdb_add,
797209edf95SVladimir Oltean 	.port_mdb_del		= felix_mdb_del,
79856051948SVladimir Oltean 	.port_bridge_join	= felix_bridge_join,
79956051948SVladimir Oltean 	.port_bridge_leave	= felix_bridge_leave,
80056051948SVladimir Oltean 	.port_stp_state_set	= felix_bridge_stp_state_set,
80156051948SVladimir Oltean 	.port_vlan_prepare	= felix_vlan_prepare,
80256051948SVladimir Oltean 	.port_vlan_filtering	= felix_vlan_filtering,
80356051948SVladimir Oltean 	.port_vlan_add		= felix_vlan_add,
80456051948SVladimir Oltean 	.port_vlan_del		= felix_vlan_del,
805c0bcf537SYangbo Lu 	.port_hwtstamp_get	= felix_hwtstamp_get,
806c0bcf537SYangbo Lu 	.port_hwtstamp_set	= felix_hwtstamp_set,
807c0bcf537SYangbo Lu 	.port_rxtstamp		= felix_rxtstamp,
808c0bcf537SYangbo Lu 	.port_txtstamp		= felix_txtstamp,
8090b912fc9SVladimir Oltean 	.port_change_mtu	= felix_change_mtu,
8100b912fc9SVladimir Oltean 	.port_max_mtu		= felix_get_max_mtu,
811fc411eaaSVladimir Oltean 	.port_policer_add	= felix_port_policer_add,
812fc411eaaSVladimir Oltean 	.port_policer_del	= felix_port_policer_del,
81307d985eeSVladimir Oltean 	.cls_flower_add		= felix_cls_flower_add,
81407d985eeSVladimir Oltean 	.cls_flower_del		= felix_cls_flower_del,
81507d985eeSVladimir Oltean 	.cls_flower_stats	= felix_cls_flower_stats,
816de143c0eSXiaoliang Yang 	.port_setup_tc          = felix_port_setup_tc,
81756051948SVladimir Oltean };
81856051948SVladimir Oltean 
81956051948SVladimir Oltean static struct felix_info *felix_instance_tbl[] = {
82056051948SVladimir Oltean 	[FELIX_INSTANCE_VSC9959] = &felix_info_vsc9959,
82156051948SVladimir Oltean };
82256051948SVladimir Oltean 
823c0bcf537SYangbo Lu static irqreturn_t felix_irq_handler(int irq, void *data)
824c0bcf537SYangbo Lu {
825c0bcf537SYangbo Lu 	struct ocelot *ocelot = (struct ocelot *)data;
826c0bcf537SYangbo Lu 
827c0bcf537SYangbo Lu 	/* The INTB interrupt is used for both PTP TX timestamp interrupt
828c0bcf537SYangbo Lu 	 * and preemption status change interrupt on each port.
829c0bcf537SYangbo Lu 	 *
830c0bcf537SYangbo Lu 	 * - Get txtstamp if have
831c0bcf537SYangbo Lu 	 * - TODO: handle preemption. Without handling it, driver may get
832c0bcf537SYangbo Lu 	 *   interrupt storm.
833c0bcf537SYangbo Lu 	 */
834c0bcf537SYangbo Lu 
835c0bcf537SYangbo Lu 	ocelot_get_txtstamp(ocelot);
836c0bcf537SYangbo Lu 
837c0bcf537SYangbo Lu 	return IRQ_HANDLED;
838c0bcf537SYangbo Lu }
839c0bcf537SYangbo Lu 
84056051948SVladimir Oltean static int felix_pci_probe(struct pci_dev *pdev,
84156051948SVladimir Oltean 			   const struct pci_device_id *id)
84256051948SVladimir Oltean {
84356051948SVladimir Oltean 	enum felix_instance instance = id->driver_data;
84456051948SVladimir Oltean 	struct dsa_switch *ds;
84556051948SVladimir Oltean 	struct ocelot *ocelot;
84656051948SVladimir Oltean 	struct felix *felix;
84756051948SVladimir Oltean 	int err;
84856051948SVladimir Oltean 
849e90c9fceSMichael Walle 	if (pdev->dev.of_node && !of_device_is_available(pdev->dev.of_node)) {
850e90c9fceSMichael Walle 		dev_info(&pdev->dev, "device is disabled, skipping\n");
851e90c9fceSMichael Walle 		return -ENODEV;
852e90c9fceSMichael Walle 	}
853e90c9fceSMichael Walle 
85456051948SVladimir Oltean 	err = pci_enable_device(pdev);
85556051948SVladimir Oltean 	if (err) {
85656051948SVladimir Oltean 		dev_err(&pdev->dev, "device enable failed\n");
85756051948SVladimir Oltean 		goto err_pci_enable;
85856051948SVladimir Oltean 	}
85956051948SVladimir Oltean 
86056051948SVladimir Oltean 	/* set up for high or low dma */
86156051948SVladimir Oltean 	err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
86256051948SVladimir Oltean 	if (err) {
86356051948SVladimir Oltean 		err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
86456051948SVladimir Oltean 		if (err) {
86556051948SVladimir Oltean 			dev_err(&pdev->dev,
86656051948SVladimir Oltean 				"DMA configuration failed: 0x%x\n", err);
86756051948SVladimir Oltean 			goto err_dma;
86856051948SVladimir Oltean 		}
86956051948SVladimir Oltean 	}
87056051948SVladimir Oltean 
87156051948SVladimir Oltean 	felix = kzalloc(sizeof(struct felix), GFP_KERNEL);
87256051948SVladimir Oltean 	if (!felix) {
87356051948SVladimir Oltean 		err = -ENOMEM;
87456051948SVladimir Oltean 		dev_err(&pdev->dev, "Failed to allocate driver memory\n");
87556051948SVladimir Oltean 		goto err_alloc_felix;
87656051948SVladimir Oltean 	}
87756051948SVladimir Oltean 
87856051948SVladimir Oltean 	pci_set_drvdata(pdev, felix);
87956051948SVladimir Oltean 	ocelot = &felix->ocelot;
88056051948SVladimir Oltean 	ocelot->dev = &pdev->dev;
88156051948SVladimir Oltean 	felix->pdev = pdev;
88256051948SVladimir Oltean 	felix->info = felix_instance_tbl[instance];
88356051948SVladimir Oltean 
88456051948SVladimir Oltean 	pci_set_master(pdev);
88556051948SVladimir Oltean 
886c0bcf537SYangbo Lu 	err = devm_request_threaded_irq(&pdev->dev, pdev->irq, NULL,
887c0bcf537SYangbo Lu 					&felix_irq_handler, IRQF_ONESHOT,
888c0bcf537SYangbo Lu 					"felix-intb", ocelot);
889c0bcf537SYangbo Lu 	if (err) {
890c0bcf537SYangbo Lu 		dev_err(&pdev->dev, "Failed to request irq\n");
891c0bcf537SYangbo Lu 		goto err_alloc_irq;
892c0bcf537SYangbo Lu 	}
893c0bcf537SYangbo Lu 
894c0bcf537SYangbo Lu 	ocelot->ptp = 1;
895c0bcf537SYangbo Lu 
89656051948SVladimir Oltean 	ds = kzalloc(sizeof(struct dsa_switch), GFP_KERNEL);
89756051948SVladimir Oltean 	if (!ds) {
89856051948SVladimir Oltean 		err = -ENOMEM;
89956051948SVladimir Oltean 		dev_err(&pdev->dev, "Failed to allocate DSA switch\n");
90056051948SVladimir Oltean 		goto err_alloc_ds;
90156051948SVladimir Oltean 	}
90256051948SVladimir Oltean 
90356051948SVladimir Oltean 	ds->dev = &pdev->dev;
90456051948SVladimir Oltean 	ds->num_ports = felix->info->num_ports;
905de143c0eSXiaoliang Yang 	ds->num_tx_queues = felix->info->num_tx_queues;
90656051948SVladimir Oltean 	ds->ops = &felix_switch_ops;
90756051948SVladimir Oltean 	ds->priv = ocelot;
90856051948SVladimir Oltean 	felix->ds = ds;
90956051948SVladimir Oltean 
91056051948SVladimir Oltean 	err = dsa_register_switch(ds);
91156051948SVladimir Oltean 	if (err) {
91256051948SVladimir Oltean 		dev_err(&pdev->dev, "Failed to register DSA switch: %d\n", err);
91356051948SVladimir Oltean 		goto err_register_ds;
91456051948SVladimir Oltean 	}
91556051948SVladimir Oltean 
91656051948SVladimir Oltean 	return 0;
91756051948SVladimir Oltean 
91856051948SVladimir Oltean err_register_ds:
91956051948SVladimir Oltean 	kfree(ds);
92056051948SVladimir Oltean err_alloc_ds:
921c0bcf537SYangbo Lu err_alloc_irq:
92256051948SVladimir Oltean err_alloc_felix:
92356051948SVladimir Oltean 	kfree(felix);
92456051948SVladimir Oltean err_dma:
92556051948SVladimir Oltean 	pci_disable_device(pdev);
92656051948SVladimir Oltean err_pci_enable:
92756051948SVladimir Oltean 	return err;
92856051948SVladimir Oltean }
92956051948SVladimir Oltean 
93056051948SVladimir Oltean static void felix_pci_remove(struct pci_dev *pdev)
93156051948SVladimir Oltean {
93256051948SVladimir Oltean 	struct felix *felix;
93356051948SVladimir Oltean 
93456051948SVladimir Oltean 	felix = pci_get_drvdata(pdev);
93556051948SVladimir Oltean 
93656051948SVladimir Oltean 	dsa_unregister_switch(felix->ds);
93756051948SVladimir Oltean 
93856051948SVladimir Oltean 	kfree(felix->ds);
93956051948SVladimir Oltean 	kfree(felix);
94056051948SVladimir Oltean 
94156051948SVladimir Oltean 	pci_disable_device(pdev);
94256051948SVladimir Oltean }
94356051948SVladimir Oltean 
94456051948SVladimir Oltean static struct pci_device_id felix_ids[] = {
94556051948SVladimir Oltean 	{
94656051948SVladimir Oltean 		/* NXP LS1028A */
94756051948SVladimir Oltean 		PCI_DEVICE(PCI_VENDOR_ID_FREESCALE, 0xEEF0),
94856051948SVladimir Oltean 		.driver_data = FELIX_INSTANCE_VSC9959,
94956051948SVladimir Oltean 	},
95056051948SVladimir Oltean 	{ 0, }
95156051948SVladimir Oltean };
95256051948SVladimir Oltean MODULE_DEVICE_TABLE(pci, felix_ids);
95356051948SVladimir Oltean 
95456051948SVladimir Oltean static struct pci_driver felix_pci_driver = {
95556051948SVladimir Oltean 	.name		= KBUILD_MODNAME,
95656051948SVladimir Oltean 	.id_table	= felix_ids,
95756051948SVladimir Oltean 	.probe		= felix_pci_probe,
95856051948SVladimir Oltean 	.remove		= felix_pci_remove,
95956051948SVladimir Oltean };
96056051948SVladimir Oltean 
96156051948SVladimir Oltean module_pci_driver(felix_pci_driver);
96256051948SVladimir Oltean 
96356051948SVladimir Oltean MODULE_DESCRIPTION("Felix Switch driver");
96456051948SVladimir Oltean MODULE_LICENSE("GPL v2");
965