xref: /linux/drivers/net/dsa/lantiq/lantiq_gswip_common.c (revision 6574f01ef95dd9029a0230f4f56a62f93fdd8319)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Lantiq / Intel / MaxLinear GSWIP common function library
4  *
5  * Copyright (C) 2025 Daniel Golle <daniel@makrotopia.org>
6  * Copyright (C) 2023 - 2024 MaxLinear Inc.
7  * Copyright (C) 2022 Snap One, LLC.  All rights reserved.
8  * Copyright (C) 2017 - 2019 Hauke Mehrtens <hauke@hauke-m.de>
9  * Copyright (C) 2012 John Crispin <john@phrozen.org>
10  * Copyright (C) 2010 Lantiq Deutschland
11  *
12  * The VLAN and bridge model the GSWIP hardware uses does not directly
13  * matches the model DSA uses.
14  *
15  * The hardware has 64 possible table entries for bridges with one VLAN
16  * ID, one flow id and a list of ports for each bridge. All entries which
17  * match the same flow ID are combined in the mac learning table, they
18  * act as one global bridge.
19  * The hardware does not support VLAN filter on the port, but on the
20  * bridge, this driver converts the DSA model to the hardware.
21  *
22  * The CPU gets all the exception frames which do not match any forwarding
23  * rule and the CPU port is also added to all bridges. This makes it possible
24  * to handle all the special cases easily in software.
25  * At the initialization the driver allocates one bridge table entry for
26  * each switch port which is used when the port is used without an
27  * explicit bridge. This prevents the frames from being forwarded
28  * between all LAN ports by default.
29  */
30 
31 #include "lantiq_gswip.h"
32 
33 #include <linux/delay.h>
34 #include <linux/etherdevice.h>
35 #include <linux/if_bridge.h>
36 #include <linux/if_vlan.h>
37 #include <linux/iopoll.h>
38 #include <linux/module.h>
39 #include <linux/of_mdio.h>
40 #include <linux/of_net.h>
41 #include <linux/phy.h>
42 #include <linux/phylink.h>
43 #include <linux/regmap.h>
44 #include <net/dsa.h>
45 
46 struct gswip_pce_table_entry {
47 	u16 index;      // PCE_TBL_ADDR.ADDR = pData->table_index
48 	u16 table;      // PCE_TBL_CTRL.ADDR = pData->table
49 	u16 key[8];
50 	u16 val[5];
51 	u16 mask;
52 	u8 gmap;
53 	bool type;
54 	bool valid;
55 	bool key_mode;
56 };
57 
58 struct gswip_rmon_cnt_desc {
59 	unsigned int size;
60 	unsigned int offset;
61 	const char *name;
62 };
63 
64 #define MIB_DESC(_size, _offset, _name) {.size = _size, .offset = _offset, .name = _name}
65 
66 static const struct gswip_rmon_cnt_desc gswip_rmon_cnt[] = {
67 	/** Receive Packet Count (only packets that are accepted and not discarded). */
68 	MIB_DESC(1, 0x1F, "RxGoodPkts"),
69 	MIB_DESC(1, 0x23, "RxUnicastPkts"),
70 	MIB_DESC(1, 0x22, "RxMulticastPkts"),
71 	MIB_DESC(1, 0x21, "RxFCSErrorPkts"),
72 	MIB_DESC(1, 0x1D, "RxUnderSizeGoodPkts"),
73 	MIB_DESC(1, 0x1E, "RxUnderSizeErrorPkts"),
74 	MIB_DESC(1, 0x1B, "RxOversizeGoodPkts"),
75 	MIB_DESC(1, 0x1C, "RxOversizeErrorPkts"),
76 	MIB_DESC(1, 0x20, "RxGoodPausePkts"),
77 	MIB_DESC(1, 0x1A, "RxAlignErrorPkts"),
78 	MIB_DESC(1, 0x12, "Rx64BytePkts"),
79 	MIB_DESC(1, 0x13, "Rx127BytePkts"),
80 	MIB_DESC(1, 0x14, "Rx255BytePkts"),
81 	MIB_DESC(1, 0x15, "Rx511BytePkts"),
82 	MIB_DESC(1, 0x16, "Rx1023BytePkts"),
83 	/** Receive Size 1024-1522 (or more, if configured) Packet Count. */
84 	MIB_DESC(1, 0x17, "RxMaxBytePkts"),
85 	MIB_DESC(1, 0x18, "RxDroppedPkts"),
86 	MIB_DESC(1, 0x19, "RxFilteredPkts"),
87 	MIB_DESC(2, 0x24, "RxGoodBytes"),
88 	MIB_DESC(2, 0x26, "RxBadBytes"),
89 	MIB_DESC(1, 0x11, "TxAcmDroppedPkts"),
90 	MIB_DESC(1, 0x0C, "TxGoodPkts"),
91 	MIB_DESC(1, 0x06, "TxUnicastPkts"),
92 	MIB_DESC(1, 0x07, "TxMulticastPkts"),
93 	MIB_DESC(1, 0x00, "Tx64BytePkts"),
94 	MIB_DESC(1, 0x01, "Tx127BytePkts"),
95 	MIB_DESC(1, 0x02, "Tx255BytePkts"),
96 	MIB_DESC(1, 0x03, "Tx511BytePkts"),
97 	MIB_DESC(1, 0x04, "Tx1023BytePkts"),
98 	/** Transmit Size 1024-1522 (or more, if configured) Packet Count. */
99 	MIB_DESC(1, 0x05, "TxMaxBytePkts"),
100 	MIB_DESC(1, 0x08, "TxSingleCollCount"),
101 	MIB_DESC(1, 0x09, "TxMultCollCount"),
102 	MIB_DESC(1, 0x0A, "TxLateCollCount"),
103 	MIB_DESC(1, 0x0B, "TxExcessCollCount"),
104 	MIB_DESC(1, 0x0D, "TxPauseCount"),
105 	MIB_DESC(1, 0x10, "TxDroppedPkts"),
106 	MIB_DESC(2, 0x0E, "TxGoodBytes"),
107 };
108 
gswip_switch_r_timeout(struct gswip_priv * priv,u32 offset,u32 cleared)109 static u32 gswip_switch_r_timeout(struct gswip_priv *priv, u32 offset,
110 				  u32 cleared)
111 {
112 	u32 val;
113 
114 	return regmap_read_poll_timeout(priv->gswip, offset, val,
115 					!(val & cleared), 20, 50000);
116 }
117 
gswip_mii_mask_cfg(struct gswip_priv * priv,u32 mask,u32 set,int port)118 static void gswip_mii_mask_cfg(struct gswip_priv *priv, u32 mask, u32 set,
119 			       int port)
120 {
121 	/* MII_CFG register only exists for MII ports */
122 	if (priv->hw_info->mii_cfg[port] == -1)
123 		return;
124 
125 	regmap_write_bits(priv->mii, priv->hw_info->mii_cfg[port], mask,
126 			  set);
127 }
128 
gswip_mdio_poll(struct gswip_priv * priv)129 static int gswip_mdio_poll(struct gswip_priv *priv)
130 {
131 	u32 ctrl;
132 
133 	return regmap_read_poll_timeout(priv->mdio, GSWIP_MDIO_CTRL, ctrl,
134 					!(ctrl & GSWIP_MDIO_CTRL_BUSY), 40, 4000);
135 }
136 
gswip_mdio_wr(struct mii_bus * bus,int addr,int reg,u16 val)137 static int gswip_mdio_wr(struct mii_bus *bus, int addr, int reg, u16 val)
138 {
139 	struct gswip_priv *priv = bus->priv;
140 	int err;
141 
142 	err = gswip_mdio_poll(priv);
143 	if (err) {
144 		dev_err(&bus->dev, "waiting for MDIO bus busy timed out\n");
145 		return err;
146 	}
147 
148 	regmap_write(priv->mdio, GSWIP_MDIO_WRITE, val);
149 	regmap_write(priv->mdio, GSWIP_MDIO_CTRL,
150 		     GSWIP_MDIO_CTRL_BUSY | GSWIP_MDIO_CTRL_WR |
151 		     ((addr & GSWIP_MDIO_CTRL_PHYAD_MASK) << GSWIP_MDIO_CTRL_PHYAD_SHIFT) |
152 		     (reg & GSWIP_MDIO_CTRL_REGAD_MASK));
153 
154 	return 0;
155 }
156 
gswip_mdio_rd(struct mii_bus * bus,int addr,int reg)157 static int gswip_mdio_rd(struct mii_bus *bus, int addr, int reg)
158 {
159 	struct gswip_priv *priv = bus->priv;
160 	u32 val;
161 	int err;
162 
163 	err = gswip_mdio_poll(priv);
164 	if (err) {
165 		dev_err(&bus->dev, "waiting for MDIO bus busy timed out\n");
166 		return err;
167 	}
168 
169 	regmap_write(priv->mdio, GSWIP_MDIO_CTRL,
170 		     GSWIP_MDIO_CTRL_BUSY | GSWIP_MDIO_CTRL_RD |
171 		     ((addr & GSWIP_MDIO_CTRL_PHYAD_MASK) << GSWIP_MDIO_CTRL_PHYAD_SHIFT) |
172 		     (reg & GSWIP_MDIO_CTRL_REGAD_MASK));
173 
174 	err = gswip_mdio_poll(priv);
175 	if (err) {
176 		dev_err(&bus->dev, "waiting for MDIO bus busy timed out\n");
177 		return err;
178 	}
179 
180 	err = regmap_read(priv->mdio, GSWIP_MDIO_READ, &val);
181 	if (err)
182 		return err;
183 
184 	return val;
185 }
186 
gswip_mdio(struct gswip_priv * priv)187 static int gswip_mdio(struct gswip_priv *priv)
188 {
189 	struct device_node *mdio_np, *switch_np = priv->dev->of_node;
190 	struct device *dev = priv->dev;
191 	struct mii_bus *bus;
192 	int err = 0;
193 
194 	mdio_np = of_get_compatible_child(switch_np, "lantiq,xrx200-mdio");
195 	if (!mdio_np)
196 		mdio_np = of_get_child_by_name(switch_np, "mdio");
197 
198 	if (!of_device_is_available(mdio_np))
199 		goto out_put_node;
200 
201 	bus = devm_mdiobus_alloc(dev);
202 	if (!bus) {
203 		err = -ENOMEM;
204 		goto out_put_node;
205 	}
206 
207 	bus->priv = priv;
208 	bus->read = gswip_mdio_rd;
209 	bus->write = gswip_mdio_wr;
210 	bus->name = "lantiq,xrx200-mdio";
211 	snprintf(bus->id, MII_BUS_ID_SIZE, "%s-mii", dev_name(priv->dev));
212 	bus->parent = priv->dev;
213 
214 	err = devm_of_mdiobus_register(dev, bus, mdio_np);
215 
216 out_put_node:
217 	of_node_put(mdio_np);
218 
219 	return err;
220 }
221 
gswip_pce_table_entry_read(struct gswip_priv * priv,struct gswip_pce_table_entry * tbl)222 static int gswip_pce_table_entry_read(struct gswip_priv *priv,
223 				      struct gswip_pce_table_entry *tbl)
224 {
225 	int i;
226 	int err;
227 	u32 crtl;
228 	u32 tmp;
229 	u16 addr_mode = tbl->key_mode ? GSWIP_PCE_TBL_CTRL_OPMOD_KSRD :
230 					GSWIP_PCE_TBL_CTRL_OPMOD_ADRD;
231 
232 	mutex_lock(&priv->pce_table_lock);
233 
234 	err = gswip_switch_r_timeout(priv, GSWIP_PCE_TBL_CTRL,
235 				     GSWIP_PCE_TBL_CTRL_BAS);
236 	if (err)
237 		goto out_unlock;
238 
239 	regmap_write(priv->gswip, GSWIP_PCE_TBL_ADDR, tbl->index);
240 	regmap_write_bits(priv->gswip, GSWIP_PCE_TBL_CTRL,
241 			  GSWIP_PCE_TBL_CTRL_ADDR_MASK |
242 			  GSWIP_PCE_TBL_CTRL_OPMOD_MASK |
243 			  GSWIP_PCE_TBL_CTRL_BAS,
244 			  tbl->table | addr_mode | GSWIP_PCE_TBL_CTRL_BAS);
245 
246 	err = gswip_switch_r_timeout(priv, GSWIP_PCE_TBL_CTRL,
247 				     GSWIP_PCE_TBL_CTRL_BAS);
248 	if (err)
249 		goto out_unlock;
250 
251 	for (i = 0; i < ARRAY_SIZE(tbl->key); i++) {
252 		err = regmap_read(priv->gswip, GSWIP_PCE_TBL_KEY(i), &tmp);
253 		if (err)
254 			goto out_unlock;
255 		tbl->key[i] = tmp;
256 	}
257 	for (i = 0; i < ARRAY_SIZE(tbl->val); i++) {
258 		err = regmap_read(priv->gswip, GSWIP_PCE_TBL_VAL(i), &tmp);
259 		if (err)
260 			goto out_unlock;
261 		tbl->val[i] = tmp;
262 	}
263 
264 	err = regmap_read(priv->gswip, GSWIP_PCE_TBL_MASK, &tmp);
265 	if (err)
266 		goto out_unlock;
267 
268 	tbl->mask = tmp;
269 	err = regmap_read(priv->gswip, GSWIP_PCE_TBL_CTRL, &crtl);
270 	if (err)
271 		goto out_unlock;
272 
273 	tbl->type = !!(crtl & GSWIP_PCE_TBL_CTRL_TYPE);
274 	tbl->valid = !!(crtl & GSWIP_PCE_TBL_CTRL_VLD);
275 	tbl->gmap = (crtl & GSWIP_PCE_TBL_CTRL_GMAP_MASK) >> 7;
276 
277 out_unlock:
278 	mutex_unlock(&priv->pce_table_lock);
279 
280 	return err;
281 }
282 
gswip_pce_table_entry_write(struct gswip_priv * priv,struct gswip_pce_table_entry * tbl)283 static int gswip_pce_table_entry_write(struct gswip_priv *priv,
284 				       struct gswip_pce_table_entry *tbl)
285 {
286 	int i;
287 	int err;
288 	u32 crtl;
289 	u16 addr_mode = tbl->key_mode ? GSWIP_PCE_TBL_CTRL_OPMOD_KSWR :
290 					GSWIP_PCE_TBL_CTRL_OPMOD_ADWR;
291 
292 	mutex_lock(&priv->pce_table_lock);
293 
294 	err = gswip_switch_r_timeout(priv, GSWIP_PCE_TBL_CTRL,
295 				     GSWIP_PCE_TBL_CTRL_BAS);
296 	if (err) {
297 		mutex_unlock(&priv->pce_table_lock);
298 		return err;
299 	}
300 
301 	regmap_write(priv->gswip, GSWIP_PCE_TBL_ADDR, tbl->index);
302 	regmap_write_bits(priv->gswip, GSWIP_PCE_TBL_CTRL,
303 			  GSWIP_PCE_TBL_CTRL_ADDR_MASK |
304 			  GSWIP_PCE_TBL_CTRL_OPMOD_MASK,
305 			  tbl->table | addr_mode);
306 
307 	for (i = 0; i < ARRAY_SIZE(tbl->key); i++)
308 		regmap_write(priv->gswip, GSWIP_PCE_TBL_KEY(i), tbl->key[i]);
309 
310 	for (i = 0; i < ARRAY_SIZE(tbl->val); i++)
311 		regmap_write(priv->gswip, GSWIP_PCE_TBL_VAL(i), tbl->val[i]);
312 
313 	regmap_write_bits(priv->gswip, GSWIP_PCE_TBL_CTRL,
314 			  GSWIP_PCE_TBL_CTRL_ADDR_MASK |
315 			  GSWIP_PCE_TBL_CTRL_OPMOD_MASK,
316 			  tbl->table | addr_mode);
317 
318 	regmap_write(priv->gswip, GSWIP_PCE_TBL_MASK, tbl->mask);
319 
320 	regmap_read(priv->gswip, GSWIP_PCE_TBL_CTRL, &crtl);
321 	crtl &= ~(GSWIP_PCE_TBL_CTRL_TYPE | GSWIP_PCE_TBL_CTRL_VLD |
322 		  GSWIP_PCE_TBL_CTRL_GMAP_MASK);
323 	if (tbl->type)
324 		crtl |= GSWIP_PCE_TBL_CTRL_TYPE;
325 	if (tbl->valid)
326 		crtl |= GSWIP_PCE_TBL_CTRL_VLD;
327 	crtl |= (tbl->gmap << 7) & GSWIP_PCE_TBL_CTRL_GMAP_MASK;
328 	crtl |= GSWIP_PCE_TBL_CTRL_BAS;
329 	regmap_write(priv->gswip, GSWIP_PCE_TBL_CTRL, crtl);
330 
331 	err = gswip_switch_r_timeout(priv, GSWIP_PCE_TBL_CTRL,
332 				     GSWIP_PCE_TBL_CTRL_BAS);
333 
334 	mutex_unlock(&priv->pce_table_lock);
335 
336 	return err;
337 }
338 
339 /* Add the LAN port into a bridge with the CPU port by
340  * default. This prevents automatic forwarding of
341  * packages between the LAN ports when no explicit
342  * bridge is configured.
343  */
gswip_add_single_port_br(struct gswip_priv * priv,int port,bool add)344 static int gswip_add_single_port_br(struct gswip_priv *priv, int port, bool add)
345 {
346 	struct gswip_pce_table_entry vlan_active = {0,};
347 	struct gswip_pce_table_entry vlan_mapping = {0,};
348 	int err;
349 
350 	vlan_active.index = port + 1;
351 	vlan_active.table = GSWIP_TABLE_ACTIVE_VLAN;
352 	vlan_active.key[0] = GSWIP_VLAN_UNAWARE_PVID;
353 	vlan_active.val[0] = port + 1 /* fid */;
354 	vlan_active.valid = add;
355 	err = gswip_pce_table_entry_write(priv, &vlan_active);
356 	if (err) {
357 		dev_err(priv->dev, "failed to write active VLAN: %d\n", err);
358 		return err;
359 	}
360 
361 	if (!add)
362 		return 0;
363 
364 	vlan_mapping.index = port + 1;
365 	vlan_mapping.table = GSWIP_TABLE_VLAN_MAPPING;
366 	vlan_mapping.val[0] = GSWIP_VLAN_UNAWARE_PVID;
367 	vlan_mapping.val[1] = BIT(port) | dsa_cpu_ports(priv->ds);
368 	vlan_mapping.val[2] = 0;
369 	err = gswip_pce_table_entry_write(priv, &vlan_mapping);
370 	if (err) {
371 		dev_err(priv->dev, "failed to write VLAN mapping: %d\n", err);
372 		return err;
373 	}
374 
375 	return 0;
376 }
377 
gswip_port_set_learning(struct gswip_priv * priv,int port,bool enable)378 static int gswip_port_set_learning(struct gswip_priv *priv, int port,
379 				   bool enable)
380 {
381 	if (!GSWIP_VERSION_GE(priv, GSWIP_VERSION_2_2))
382 		return -EOPNOTSUPP;
383 
384 	/* learning disable bit */
385 	return regmap_update_bits(priv->gswip, GSWIP_PCE_PCTRL_3p(port),
386 				  GSWIP_PCE_PCTRL_3_LNDIS,
387 				  enable ? 0 : GSWIP_PCE_PCTRL_3_LNDIS);
388 }
389 
gswip_port_pre_bridge_flags(struct dsa_switch * ds,int port,struct switchdev_brport_flags flags,struct netlink_ext_ack * extack)390 static int gswip_port_pre_bridge_flags(struct dsa_switch *ds, int port,
391 				       struct switchdev_brport_flags flags,
392 				       struct netlink_ext_ack *extack)
393 {
394 	struct gswip_priv *priv = ds->priv;
395 	unsigned long supported = 0;
396 
397 	if (GSWIP_VERSION_GE(priv, GSWIP_VERSION_2_2))
398 		supported |= BR_LEARNING;
399 
400 	if (flags.mask & ~supported)
401 		return -EINVAL;
402 
403 	return 0;
404 }
405 
gswip_port_bridge_flags(struct dsa_switch * ds,int port,struct switchdev_brport_flags flags,struct netlink_ext_ack * extack)406 static int gswip_port_bridge_flags(struct dsa_switch *ds, int port,
407 				   struct switchdev_brport_flags flags,
408 				   struct netlink_ext_ack *extack)
409 {
410 	struct gswip_priv *priv = ds->priv;
411 
412 	if (flags.mask & BR_LEARNING)
413 		return gswip_port_set_learning(priv, port,
414 					       !!(flags.val & BR_LEARNING));
415 
416 	return 0;
417 }
418 
gswip_port_setup(struct dsa_switch * ds,int port)419 static int gswip_port_setup(struct dsa_switch *ds, int port)
420 {
421 	struct gswip_priv *priv = ds->priv;
422 	int err;
423 
424 	if (priv->hw_info->port_setup) {
425 		err = priv->hw_info->port_setup(ds, port);
426 		if (err)
427 			return err;
428 	}
429 
430 	if (!dsa_is_cpu_port(ds, port)) {
431 		err = gswip_add_single_port_br(priv, port, true);
432 		if (err)
433 			return err;
434 	}
435 
436 	return 0;
437 }
438 
gswip_port_enable(struct dsa_switch * ds,int port,struct phy_device * phydev)439 static int gswip_port_enable(struct dsa_switch *ds, int port,
440 			     struct phy_device *phydev)
441 {
442 	struct gswip_priv *priv = ds->priv;
443 
444 	if (!dsa_is_cpu_port(ds, port)) {
445 		u32 mdio_phy = 0;
446 
447 		if (phydev)
448 			mdio_phy = phydev->mdio.addr & GSWIP_MDIO_PHY_ADDR_MASK;
449 
450 		regmap_write_bits(priv->mdio, GSWIP_MDIO_PHYp(port),
451 				  GSWIP_MDIO_PHY_ADDR_MASK,
452 				  mdio_phy);
453 	}
454 
455 	/* RMON Counter Enable for port */
456 	regmap_write(priv->gswip, GSWIP_BM_PCFGp(port), GSWIP_BM_PCFG_CNTEN);
457 
458 	/* enable port fetch/store dma & VLAN Modification */
459 	regmap_set_bits(priv->gswip, GSWIP_FDMA_PCTRLp(port),
460 			GSWIP_FDMA_PCTRL_EN | GSWIP_FDMA_PCTRL_VLANMOD_BOTH);
461 	regmap_set_bits(priv->gswip, GSWIP_SDMA_PCTRLp(port),
462 			GSWIP_SDMA_PCTRL_EN);
463 
464 	return 0;
465 }
466 
gswip_port_disable(struct dsa_switch * ds,int port)467 static void gswip_port_disable(struct dsa_switch *ds, int port)
468 {
469 	struct gswip_priv *priv = ds->priv;
470 
471 	regmap_clear_bits(priv->gswip, GSWIP_FDMA_PCTRLp(port),
472 			  GSWIP_FDMA_PCTRL_EN);
473 	regmap_clear_bits(priv->gswip, GSWIP_SDMA_PCTRLp(port),
474 			  GSWIP_SDMA_PCTRL_EN);
475 }
476 
gswip_pce_load_microcode(struct gswip_priv * priv)477 static int gswip_pce_load_microcode(struct gswip_priv *priv)
478 {
479 	int i;
480 	int err;
481 
482 	regmap_write_bits(priv->gswip, GSWIP_PCE_TBL_CTRL,
483 			  GSWIP_PCE_TBL_CTRL_ADDR_MASK |
484 			  GSWIP_PCE_TBL_CTRL_OPMOD_MASK |
485 			  GSWIP_PCE_TBL_CTRL_OPMOD_ADWR,
486 			  GSWIP_PCE_TBL_CTRL_OPMOD_ADWR);
487 	regmap_write(priv->gswip, GSWIP_PCE_TBL_MASK, 0);
488 
489 	for (i = 0; i < priv->hw_info->pce_microcode_size; i++) {
490 		regmap_write(priv->gswip, GSWIP_PCE_TBL_ADDR, i);
491 		regmap_write(priv->gswip, GSWIP_PCE_TBL_VAL(0),
492 			     (*priv->hw_info->pce_microcode)[i].val_0);
493 		regmap_write(priv->gswip, GSWIP_PCE_TBL_VAL(1),
494 			     (*priv->hw_info->pce_microcode)[i].val_1);
495 		regmap_write(priv->gswip, GSWIP_PCE_TBL_VAL(2),
496 			     (*priv->hw_info->pce_microcode)[i].val_2);
497 		regmap_write(priv->gswip, GSWIP_PCE_TBL_VAL(3),
498 			     (*priv->hw_info->pce_microcode)[i].val_3);
499 
500 		/* start the table access: */
501 		regmap_set_bits(priv->gswip, GSWIP_PCE_TBL_CTRL,
502 				GSWIP_PCE_TBL_CTRL_BAS);
503 		err = gswip_switch_r_timeout(priv, GSWIP_PCE_TBL_CTRL,
504 					     GSWIP_PCE_TBL_CTRL_BAS);
505 		if (err)
506 			return err;
507 	}
508 
509 	/* tell the switch that the microcode is loaded */
510 	regmap_set_bits(priv->gswip, GSWIP_PCE_GCTRL_0,
511 			GSWIP_PCE_GCTRL_0_MC_VALID);
512 
513 	return 0;
514 }
515 
gswip_port_commit_pvid(struct gswip_priv * priv,int port)516 static void gswip_port_commit_pvid(struct gswip_priv *priv, int port)
517 {
518 	struct dsa_port *dp = dsa_to_port(priv->ds, port);
519 	struct net_device *br = dsa_port_bridge_dev_get(dp);
520 	u32 vinr;
521 	int idx;
522 
523 	if (!dsa_port_is_user(dp))
524 		return;
525 
526 	if (br) {
527 		u16 pvid = GSWIP_VLAN_UNAWARE_PVID;
528 
529 		if (br_vlan_enabled(br))
530 			br_vlan_get_pvid(br, &pvid);
531 
532 		/* VLAN-aware bridge ports with no PVID will use Active VLAN
533 		 * index 0. The expectation is that this drops all untagged and
534 		 * VID-0 tagged ingress traffic.
535 		 */
536 		idx = 0;
537 		for (int i = priv->hw_info->max_ports;
538 		     i < ARRAY_SIZE(priv->vlans); i++) {
539 			if (priv->vlans[i].bridge == br &&
540 			    priv->vlans[i].vid == pvid) {
541 				idx = i;
542 				break;
543 			}
544 		}
545 	} else {
546 		/* The Active VLAN table index as configured by
547 		 * gswip_add_single_port_br()
548 		 */
549 		idx = port + 1;
550 	}
551 
552 	vinr = idx ? GSWIP_PCE_VCTRL_VINR_ALL : GSWIP_PCE_VCTRL_VINR_TAGGED;
553 	regmap_write_bits(priv->gswip, GSWIP_PCE_VCTRL(port),
554 			  GSWIP_PCE_VCTRL_VINR,
555 			  FIELD_PREP(GSWIP_PCE_VCTRL_VINR, vinr));
556 
557 	/* Note that in GSWIP 2.2 VLAN mode the VID needs to be programmed
558 	 * directly instead of referencing the index in the Active VLAN Tablet.
559 	 * However, without the VLANMD bit (9) in PCE_GCTRL_1 (0x457) even
560 	 * GSWIP 2.2 and newer hardware maintain the GSWIP 2.1 behavior.
561 	 */
562 	regmap_write(priv->gswip, GSWIP_PCE_DEFPVID(port), idx);
563 }
564 
gswip_port_vlan_filtering(struct dsa_switch * ds,int port,bool vlan_filtering,struct netlink_ext_ack * extack)565 static int gswip_port_vlan_filtering(struct dsa_switch *ds, int port,
566 				     bool vlan_filtering,
567 				     struct netlink_ext_ack *extack)
568 {
569 	struct gswip_priv *priv = ds->priv;
570 
571 	if (vlan_filtering) {
572 		/* Use tag based VLAN */
573 		regmap_write_bits(priv->gswip, GSWIP_PCE_VCTRL(port),
574 				  GSWIP_PCE_VCTRL_VSR |
575 				  GSWIP_PCE_VCTRL_UVR |
576 				  GSWIP_PCE_VCTRL_VIMR |
577 				  GSWIP_PCE_VCTRL_VEMR |
578 				  GSWIP_PCE_VCTRL_VID0,
579 				  GSWIP_PCE_VCTRL_UVR |
580 				  GSWIP_PCE_VCTRL_VIMR |
581 				  GSWIP_PCE_VCTRL_VEMR |
582 				  GSWIP_PCE_VCTRL_VID0);
583 		regmap_clear_bits(priv->gswip, GSWIP_PCE_PCTRL_0p(port),
584 				  GSWIP_PCE_PCTRL_0_TVM);
585 	} else {
586 		/* Use port based VLAN */
587 		regmap_write_bits(priv->gswip, GSWIP_PCE_VCTRL(port),
588 				  GSWIP_PCE_VCTRL_UVR |
589 				  GSWIP_PCE_VCTRL_VIMR |
590 				  GSWIP_PCE_VCTRL_VEMR |
591 				  GSWIP_PCE_VCTRL_VID0 |
592 				  GSWIP_PCE_VCTRL_VSR,
593 				  GSWIP_PCE_VCTRL_VSR);
594 		regmap_set_bits(priv->gswip, GSWIP_PCE_PCTRL_0p(port),
595 				GSWIP_PCE_PCTRL_0_TVM);
596 	}
597 
598 	gswip_port_commit_pvid(priv, port);
599 
600 	return 0;
601 }
602 
gswip_mii_delay_setup(struct gswip_priv * priv,struct dsa_port * dp,phy_interface_t interface)603 static void gswip_mii_delay_setup(struct gswip_priv *priv, struct dsa_port *dp,
604 				  phy_interface_t interface)
605 {
606 	u32 tx_delay = GSWIP_MII_PCDU_TXDLY_DEFAULT;
607 	u32 rx_delay = GSWIP_MII_PCDU_RXDLY_DEFAULT;
608 	struct device_node *port_dn = dp->dn;
609 
610 	/* As MII_PCDU registers only exist for MII ports, silently return
611 	 * unless the port is an MII port
612 	 */
613 	if (priv->hw_info->mii_pcdu[dp->index] == -1)
614 		return;
615 
616 	/* legacy code to set default delays according to the interface mode */
617 	switch (interface) {
618 	case PHY_INTERFACE_MODE_RGMII_ID:
619 		tx_delay = 0;
620 		rx_delay = 0;
621 		break;
622 	case PHY_INTERFACE_MODE_RGMII_RXID:
623 		rx_delay = 0;
624 		break;
625 	case PHY_INTERFACE_MODE_RGMII_TXID:
626 		tx_delay = 0;
627 		break;
628 	default:
629 		break;
630 	}
631 
632 	/* allow settings delays using device tree properties */
633 	of_property_read_u32(port_dn, "rx-internal-delay-ps", &rx_delay);
634 	of_property_read_u32(port_dn, "tx-internal-delay-ps", &tx_delay);
635 
636 	regmap_write_bits(priv->mii, priv->hw_info->mii_pcdu[dp->index],
637 			  GSWIP_MII_PCDU_TXDLY_MASK |
638 			  GSWIP_MII_PCDU_RXDLY_MASK,
639 			  GSWIP_MII_PCDU_TXDLY(tx_delay) |
640 			  GSWIP_MII_PCDU_RXDLY(rx_delay));
641 }
642 
gswip_setup(struct dsa_switch * ds)643 static int gswip_setup(struct dsa_switch *ds)
644 {
645 	unsigned int cpu_ports = dsa_cpu_ports(ds);
646 	struct gswip_priv *priv = ds->priv;
647 	struct dsa_port *cpu_dp;
648 	int err, i;
649 
650 	regmap_write(priv->gswip, GSWIP_SWRES, GSWIP_SWRES_R0);
651 	usleep_range(5000, 10000);
652 	regmap_write(priv->gswip, GSWIP_SWRES, 0);
653 
654 	/* disable port fetch/store dma on all ports */
655 	for (i = 0; i < priv->hw_info->max_ports; i++) {
656 		gswip_port_disable(ds, i);
657 		gswip_port_vlan_filtering(ds, i, false, NULL);
658 	}
659 
660 	/* enable Switch */
661 	regmap_set_bits(priv->mdio, GSWIP_MDIO_GLOB, GSWIP_MDIO_GLOB_ENABLE);
662 
663 	err = gswip_pce_load_microcode(priv);
664 	if (err) {
665 		dev_err(priv->dev, "writing PCE microcode failed, %i\n", err);
666 		return err;
667 	}
668 
669 	/* Default unknown Broadcast/Multicast/Unicast port maps */
670 	regmap_write(priv->gswip, GSWIP_PCE_PMAP1, cpu_ports);
671 	regmap_write(priv->gswip, GSWIP_PCE_PMAP2, cpu_ports);
672 	regmap_write(priv->gswip, GSWIP_PCE_PMAP3, cpu_ports);
673 
674 	/* Deactivate MDIO PHY auto polling. Some PHYs as the AR8030 have an
675 	 * interoperability problem with this auto polling mechanism because
676 	 * their status registers think that the link is in a different state
677 	 * than it actually is. For the AR8030 it has the BMSR_ESTATEN bit set
678 	 * as well as ESTATUS_1000_TFULL and ESTATUS_1000_XFULL. This makes the
679 	 * auto polling state machine consider the link being negotiated with
680 	 * 1Gbit/s. Since the PHY itself is a Fast Ethernet RMII PHY this leads
681 	 * to the switch port being completely dead (RX and TX are both not
682 	 * working).
683 	 * Also with various other PHY / port combinations (PHY11G GPHY, PHY22F
684 	 * GPHY, external RGMII PEF7071/7072) any traffic would stop. Sometimes
685 	 * it would work fine for a few minutes to hours and then stop, on
686 	 * other device it would no traffic could be sent or received at all.
687 	 * Testing shows that when PHY auto polling is disabled these problems
688 	 * go away.
689 	 */
690 	regmap_write(priv->mdio, GSWIP_MDIO_MDC_CFG0, 0x0);
691 
692 	/* Configure the MDIO Clock 2.5 MHz */
693 	regmap_write_bits(priv->mdio, GSWIP_MDIO_MDC_CFG1, 0xff, 0x09);
694 
695 	/* bring up the mdio bus */
696 	err = gswip_mdio(priv);
697 	if (err) {
698 		dev_err(priv->dev, "mdio bus setup failed\n");
699 		return err;
700 	}
701 
702 	/* Disable the xMII interface and clear it's isolation bit */
703 	for (i = 0; i < priv->hw_info->max_ports; i++)
704 		gswip_mii_mask_cfg(priv,
705 				   GSWIP_MII_CFG_EN | GSWIP_MII_CFG_ISOLATE,
706 				   0, i);
707 
708 	dsa_switch_for_each_cpu_port(cpu_dp, ds) {
709 		/* enable special tag insertion on cpu port */
710 		regmap_set_bits(priv->gswip, GSWIP_FDMA_PCTRLp(cpu_dp->index),
711 				GSWIP_FDMA_PCTRL_STEN);
712 
713 		/* accept special tag in ingress direction */
714 		regmap_set_bits(priv->gswip,
715 				GSWIP_PCE_PCTRL_0p(cpu_dp->index),
716 				GSWIP_PCE_PCTRL_0_INGRESS);
717 	}
718 
719 	regmap_set_bits(priv->gswip, GSWIP_BM_QUEUE_GCTRL,
720 			GSWIP_BM_QUEUE_GCTRL_GL_MOD);
721 
722 	/* VLAN aware Switching */
723 	regmap_set_bits(priv->gswip, GSWIP_PCE_GCTRL_0,
724 			GSWIP_PCE_GCTRL_0_VLAN);
725 
726 	/* Flush MAC Table */
727 	regmap_set_bits(priv->gswip, GSWIP_PCE_GCTRL_0,
728 			GSWIP_PCE_GCTRL_0_MTFL);
729 
730 	err = gswip_switch_r_timeout(priv, GSWIP_PCE_GCTRL_0,
731 				     GSWIP_PCE_GCTRL_0_MTFL);
732 	if (err) {
733 		dev_err(priv->dev, "MAC flushing didn't finish\n");
734 		return err;
735 	}
736 
737 	ds->mtu_enforcement_ingress = true;
738 
739 	return 0;
740 }
741 
gswip_teardown(struct dsa_switch * ds)742 static void gswip_teardown(struct dsa_switch *ds)
743 {
744 	struct gswip_priv *priv = ds->priv;
745 
746 	regmap_clear_bits(priv->mdio, GSWIP_MDIO_GLOB, GSWIP_MDIO_GLOB_ENABLE);
747 }
748 
gswip_get_tag_protocol(struct dsa_switch * ds,int port,enum dsa_tag_protocol mp)749 static enum dsa_tag_protocol gswip_get_tag_protocol(struct dsa_switch *ds,
750 						    int port,
751 						    enum dsa_tag_protocol mp)
752 {
753 	struct gswip_priv *priv = ds->priv;
754 
755 	return priv->hw_info->tag_protocol;
756 }
757 
gswip_vlan_active_create(struct gswip_priv * priv,struct net_device * bridge,int fid,u16 vid)758 static int gswip_vlan_active_create(struct gswip_priv *priv,
759 				    struct net_device *bridge,
760 				    int fid, u16 vid)
761 {
762 	struct gswip_pce_table_entry vlan_active = {0,};
763 	unsigned int max_ports = priv->hw_info->max_ports;
764 	int idx = -1;
765 	int err;
766 	int i;
767 
768 	/* Look for a free slot */
769 	for (i = max_ports; i < ARRAY_SIZE(priv->vlans); i++) {
770 		if (!priv->vlans[i].bridge) {
771 			idx = i;
772 			break;
773 		}
774 	}
775 
776 	if (idx == -1)
777 		return -ENOSPC;
778 
779 	if (fid == -1)
780 		fid = idx;
781 
782 	vlan_active.index = idx;
783 	vlan_active.table = GSWIP_TABLE_ACTIVE_VLAN;
784 	vlan_active.key[0] = vid;
785 	vlan_active.val[0] = fid;
786 	vlan_active.valid = true;
787 
788 	err = gswip_pce_table_entry_write(priv, &vlan_active);
789 	if (err) {
790 		dev_err(priv->dev, "failed to write active VLAN: %d\n",	err);
791 		return err;
792 	}
793 
794 	priv->vlans[idx].bridge = bridge;
795 	priv->vlans[idx].vid = vid;
796 	priv->vlans[idx].fid = fid;
797 
798 	return idx;
799 }
800 
gswip_vlan_active_remove(struct gswip_priv * priv,int idx)801 static int gswip_vlan_active_remove(struct gswip_priv *priv, int idx)
802 {
803 	struct gswip_pce_table_entry vlan_active = {0,};
804 	int err;
805 
806 	vlan_active.index = idx;
807 	vlan_active.table = GSWIP_TABLE_ACTIVE_VLAN;
808 	vlan_active.valid = false;
809 	err = gswip_pce_table_entry_write(priv, &vlan_active);
810 	if (err)
811 		dev_err(priv->dev, "failed to delete active VLAN: %d\n", err);
812 	priv->vlans[idx].bridge = NULL;
813 
814 	return err;
815 }
816 
gswip_vlan_add(struct gswip_priv * priv,struct net_device * bridge,int port,u16 vid,bool untagged,bool pvid,bool vlan_aware)817 static int gswip_vlan_add(struct gswip_priv *priv, struct net_device *bridge,
818 			  int port, u16 vid, bool untagged, bool pvid,
819 			  bool vlan_aware)
820 {
821 	struct gswip_pce_table_entry vlan_mapping = {0,};
822 	unsigned int max_ports = priv->hw_info->max_ports;
823 	unsigned int cpu_ports = dsa_cpu_ports(priv->ds);
824 	bool active_vlan_created = false;
825 	int fid = -1, idx = -1;
826 	int i, err;
827 
828 	/* Check if there is already a page for this bridge */
829 	for (i = max_ports; i < ARRAY_SIZE(priv->vlans); i++) {
830 		if (priv->vlans[i].bridge == bridge) {
831 			if (vlan_aware) {
832 				if (fid != -1 && fid != priv->vlans[i].fid)
833 					dev_err(priv->dev, "one bridge with multiple flow ids\n");
834 				fid = priv->vlans[i].fid;
835 			}
836 			if (priv->vlans[i].vid == vid) {
837 				idx = i;
838 				break;
839 			}
840 		}
841 	}
842 
843 	/* If this bridge is not programmed yet, add a Active VLAN table
844 	 * entry in a free slot and prepare the VLAN mapping table entry.
845 	 */
846 	if (idx == -1) {
847 		idx = gswip_vlan_active_create(priv, bridge, fid, vid);
848 		if (idx < 0)
849 			return idx;
850 		active_vlan_created = true;
851 
852 		vlan_mapping.index = idx;
853 		vlan_mapping.table = GSWIP_TABLE_VLAN_MAPPING;
854 	} else {
855 		/* Read the existing VLAN mapping entry from the switch */
856 		vlan_mapping.index = idx;
857 		vlan_mapping.table = GSWIP_TABLE_VLAN_MAPPING;
858 		err = gswip_pce_table_entry_read(priv, &vlan_mapping);
859 		if (err) {
860 			dev_err(priv->dev, "failed to read VLAN mapping: %d\n",
861 				err);
862 			return err;
863 		}
864 	}
865 
866 	/* VLAN ID byte, maps to the VLAN ID of vlan active table */
867 	vlan_mapping.val[0] = vid;
868 	/* Update the VLAN mapping entry and write it to the switch */
869 	vlan_mapping.val[1] |= cpu_ports;
870 	vlan_mapping.val[1] |= BIT(port);
871 	if (vlan_aware)
872 		vlan_mapping.val[2] |= cpu_ports;
873 	if (untagged)
874 		vlan_mapping.val[2] &= ~BIT(port);
875 	else
876 		vlan_mapping.val[2] |= BIT(port);
877 	err = gswip_pce_table_entry_write(priv, &vlan_mapping);
878 	if (err) {
879 		dev_err(priv->dev, "failed to write VLAN mapping: %d\n", err);
880 		/* In case an Active VLAN was creaetd delete it again */
881 		if (active_vlan_created)
882 			gswip_vlan_active_remove(priv, idx);
883 		return err;
884 	}
885 
886 	gswip_port_commit_pvid(priv, port);
887 
888 	return 0;
889 }
890 
gswip_vlan_remove(struct gswip_priv * priv,struct net_device * bridge,int port,u16 vid)891 static int gswip_vlan_remove(struct gswip_priv *priv,
892 			     struct net_device *bridge, int port,
893 			     u16 vid)
894 {
895 	struct gswip_pce_table_entry vlan_mapping = {0,};
896 	unsigned int max_ports = priv->hw_info->max_ports;
897 	int idx = -1;
898 	int i;
899 	int err;
900 
901 	/* Check if there is already a page for this bridge */
902 	for (i = max_ports; i < ARRAY_SIZE(priv->vlans); i++) {
903 		if (priv->vlans[i].bridge == bridge &&
904 		    priv->vlans[i].vid == vid) {
905 			idx = i;
906 			break;
907 		}
908 	}
909 
910 	if (idx == -1) {
911 		dev_err(priv->dev, "Port %d cannot find VID %u of bridge %s\n",
912 			port, vid, bridge ? bridge->name : "(null)");
913 		return -ENOENT;
914 	}
915 
916 	vlan_mapping.index = idx;
917 	vlan_mapping.table = GSWIP_TABLE_VLAN_MAPPING;
918 	err = gswip_pce_table_entry_read(priv, &vlan_mapping);
919 	if (err) {
920 		dev_err(priv->dev, "failed to read VLAN mapping: %d\n",	err);
921 		return err;
922 	}
923 
924 	vlan_mapping.val[1] &= ~BIT(port);
925 	vlan_mapping.val[2] &= ~BIT(port);
926 	err = gswip_pce_table_entry_write(priv, &vlan_mapping);
927 	if (err) {
928 		dev_err(priv->dev, "failed to write VLAN mapping: %d\n", err);
929 		return err;
930 	}
931 
932 	/* In case all ports are removed from the bridge, remove the VLAN */
933 	if (!(vlan_mapping.val[1] & ~dsa_cpu_ports(priv->ds))) {
934 		err = gswip_vlan_active_remove(priv, idx);
935 		if (err) {
936 			dev_err(priv->dev, "failed to write active VLAN: %d\n",
937 				err);
938 			return err;
939 		}
940 	}
941 
942 	gswip_port_commit_pvid(priv, port);
943 
944 	return 0;
945 }
946 
gswip_port_bridge_join(struct dsa_switch * ds,int port,struct dsa_bridge bridge,bool * tx_fwd_offload,struct netlink_ext_ack * extack)947 static int gswip_port_bridge_join(struct dsa_switch *ds, int port,
948 				  struct dsa_bridge bridge,
949 				  bool *tx_fwd_offload,
950 				  struct netlink_ext_ack *extack)
951 {
952 	struct net_device *br = bridge.dev;
953 	struct gswip_priv *priv = ds->priv;
954 	int err;
955 
956 	/* Set up the VLAN for VLAN-unaware bridging for this port, and remove
957 	 * it from the "single-port bridge" through which it was operating as
958 	 * standalone.
959 	 */
960 	err = gswip_vlan_add(priv, br, port, GSWIP_VLAN_UNAWARE_PVID,
961 			     true, true, false);
962 	if (err)
963 		return err;
964 
965 	return gswip_add_single_port_br(priv, port, false);
966 }
967 
gswip_port_bridge_leave(struct dsa_switch * ds,int port,struct dsa_bridge bridge)968 static void gswip_port_bridge_leave(struct dsa_switch *ds, int port,
969 				    struct dsa_bridge bridge)
970 {
971 	struct net_device *br = bridge.dev;
972 	struct gswip_priv *priv = ds->priv;
973 
974 	/* Add the port back to the "single-port bridge", and remove it from
975 	 * the VLAN-unaware PVID created for this bridge.
976 	 */
977 	gswip_add_single_port_br(priv, port, true);
978 	gswip_vlan_remove(priv, br, port, GSWIP_VLAN_UNAWARE_PVID);
979 }
980 
gswip_port_vlan_prepare(struct dsa_switch * ds,int port,const struct switchdev_obj_port_vlan * vlan,struct netlink_ext_ack * extack)981 static int gswip_port_vlan_prepare(struct dsa_switch *ds, int port,
982 				   const struct switchdev_obj_port_vlan *vlan,
983 				   struct netlink_ext_ack *extack)
984 {
985 	struct net_device *bridge = dsa_port_bridge_dev_get(dsa_to_port(ds, port));
986 	struct gswip_priv *priv = ds->priv;
987 	unsigned int max_ports = priv->hw_info->max_ports;
988 	int pos = max_ports;
989 	int i, idx = -1;
990 
991 	/* We only support VLAN filtering on bridges */
992 	if (!dsa_is_cpu_port(ds, port) && !bridge)
993 		return -EOPNOTSUPP;
994 
995 	/* Check if there is already a page for this VLAN */
996 	for (i = max_ports; i < ARRAY_SIZE(priv->vlans); i++) {
997 		if (priv->vlans[i].bridge == bridge &&
998 		    priv->vlans[i].vid == vlan->vid) {
999 			idx = i;
1000 			break;
1001 		}
1002 	}
1003 
1004 	/* If this VLAN is not programmed yet, we have to reserve
1005 	 * one entry in the VLAN table. Make sure we start at the
1006 	 * next position round.
1007 	 */
1008 	if (idx == -1) {
1009 		/* Look for a free slot */
1010 		for (; pos < ARRAY_SIZE(priv->vlans); pos++) {
1011 			if (!priv->vlans[pos].bridge) {
1012 				idx = pos;
1013 				pos++;
1014 				break;
1015 			}
1016 		}
1017 
1018 		if (idx == -1) {
1019 			NL_SET_ERR_MSG_MOD(extack, "No slot in VLAN table");
1020 			return -ENOSPC;
1021 		}
1022 	}
1023 
1024 	return 0;
1025 }
1026 
gswip_port_vlan_add(struct dsa_switch * ds,int port,const struct switchdev_obj_port_vlan * vlan,struct netlink_ext_ack * extack)1027 static int gswip_port_vlan_add(struct dsa_switch *ds, int port,
1028 			       const struct switchdev_obj_port_vlan *vlan,
1029 			       struct netlink_ext_ack *extack)
1030 {
1031 	struct net_device *bridge = dsa_port_bridge_dev_get(dsa_to_port(ds, port));
1032 	struct gswip_priv *priv = ds->priv;
1033 	bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
1034 	bool pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
1035 	int err;
1036 
1037 	if (vlan->vid == GSWIP_VLAN_UNAWARE_PVID)
1038 		return 0;
1039 
1040 	err = gswip_port_vlan_prepare(ds, port, vlan, extack);
1041 	if (err)
1042 		return err;
1043 
1044 	/* We have to receive all packets on the CPU port and should not
1045 	 * do any VLAN filtering here. This is also called with bridge
1046 	 * NULL and then we do not know for which bridge to configure
1047 	 * this.
1048 	 */
1049 	if (dsa_is_cpu_port(ds, port))
1050 		return 0;
1051 
1052 	return gswip_vlan_add(priv, bridge, port, vlan->vid, untagged, pvid,
1053 			      true);
1054 }
1055 
gswip_port_vlan_del(struct dsa_switch * ds,int port,const struct switchdev_obj_port_vlan * vlan)1056 static int gswip_port_vlan_del(struct dsa_switch *ds, int port,
1057 			       const struct switchdev_obj_port_vlan *vlan)
1058 {
1059 	struct net_device *bridge = dsa_port_bridge_dev_get(dsa_to_port(ds, port));
1060 	struct gswip_priv *priv = ds->priv;
1061 
1062 	if (vlan->vid == GSWIP_VLAN_UNAWARE_PVID)
1063 		return 0;
1064 
1065 	/* We have to receive all packets on the CPU port and should not
1066 	 * do any VLAN filtering here. This is also called with bridge
1067 	 * NULL and then we do not know for which bridge to configure
1068 	 * this.
1069 	 */
1070 	if (dsa_is_cpu_port(ds, port))
1071 		return 0;
1072 
1073 	return gswip_vlan_remove(priv, bridge, port, vlan->vid);
1074 }
1075 
gswip_port_fast_age(struct dsa_switch * ds,int port)1076 static void gswip_port_fast_age(struct dsa_switch *ds, int port)
1077 {
1078 	struct gswip_priv *priv = ds->priv;
1079 	struct gswip_pce_table_entry mac_bridge = {0,};
1080 	int i;
1081 	int err;
1082 
1083 	for (i = 0; i < 2048; i++) {
1084 		mac_bridge.table = GSWIP_TABLE_MAC_BRIDGE;
1085 		mac_bridge.index = i;
1086 
1087 		err = gswip_pce_table_entry_read(priv, &mac_bridge);
1088 		if (err) {
1089 			dev_err(priv->dev, "failed to read mac bridge: %d\n",
1090 				err);
1091 			return;
1092 		}
1093 
1094 		if (!mac_bridge.valid)
1095 			continue;
1096 
1097 		if (mac_bridge.val[1] & GSWIP_TABLE_MAC_BRIDGE_VAL1_STATIC)
1098 			continue;
1099 
1100 		if (port != FIELD_GET(GSWIP_TABLE_MAC_BRIDGE_VAL0_PORT,
1101 				      mac_bridge.val[0]))
1102 			continue;
1103 
1104 		mac_bridge.valid = false;
1105 		err = gswip_pce_table_entry_write(priv, &mac_bridge);
1106 		if (err) {
1107 			dev_err(priv->dev, "failed to write mac bridge: %d\n",
1108 				err);
1109 			return;
1110 		}
1111 	}
1112 }
1113 
gswip_port_stp_state_set(struct dsa_switch * ds,int port,u8 state)1114 static void gswip_port_stp_state_set(struct dsa_switch *ds, int port, u8 state)
1115 {
1116 	struct gswip_priv *priv = ds->priv;
1117 	u32 stp_state;
1118 
1119 	switch (state) {
1120 	case BR_STATE_DISABLED:
1121 		regmap_clear_bits(priv->gswip, GSWIP_SDMA_PCTRLp(port),
1122 				  GSWIP_SDMA_PCTRL_EN);
1123 		return;
1124 	case BR_STATE_BLOCKING:
1125 	case BR_STATE_LISTENING:
1126 		stp_state = GSWIP_PCE_PCTRL_0_PSTATE_LISTEN;
1127 		break;
1128 	case BR_STATE_LEARNING:
1129 		stp_state = GSWIP_PCE_PCTRL_0_PSTATE_LEARNING;
1130 		break;
1131 	case BR_STATE_FORWARDING:
1132 		stp_state = GSWIP_PCE_PCTRL_0_PSTATE_FORWARDING;
1133 		break;
1134 	default:
1135 		dev_err(priv->dev, "invalid STP state: %d\n", state);
1136 		return;
1137 	}
1138 
1139 	regmap_set_bits(priv->gswip, GSWIP_SDMA_PCTRLp(port),
1140 			GSWIP_SDMA_PCTRL_EN);
1141 	regmap_write_bits(priv->gswip, GSWIP_PCE_PCTRL_0p(port),
1142 			  GSWIP_PCE_PCTRL_0_PSTATE_MASK,
1143 			  stp_state);
1144 }
1145 
gswip_port_fdb(struct dsa_switch * ds,int port,struct net_device * bridge,const unsigned char * addr,u16 vid,bool add)1146 static int gswip_port_fdb(struct dsa_switch *ds, int port,
1147 			  struct net_device *bridge, const unsigned char *addr,
1148 			  u16 vid, bool add)
1149 {
1150 	struct gswip_priv *priv = ds->priv;
1151 	struct gswip_pce_table_entry mac_bridge = {0,};
1152 	unsigned int max_ports = priv->hw_info->max_ports;
1153 	int fid = -1;
1154 	int i;
1155 	int err;
1156 
1157 	for (i = max_ports; i < ARRAY_SIZE(priv->vlans); i++) {
1158 		if (priv->vlans[i].bridge == bridge) {
1159 			fid = priv->vlans[i].fid;
1160 			break;
1161 		}
1162 	}
1163 
1164 	if (fid == -1) {
1165 		dev_err(priv->dev, "no FID found for bridge %s\n",
1166 			bridge->name);
1167 		return -EINVAL;
1168 	}
1169 
1170 	mac_bridge.table = GSWIP_TABLE_MAC_BRIDGE;
1171 	mac_bridge.key_mode = true;
1172 	mac_bridge.key[0] = addr[5] | (addr[4] << 8);
1173 	mac_bridge.key[1] = addr[3] | (addr[2] << 8);
1174 	mac_bridge.key[2] = addr[1] | (addr[0] << 8);
1175 	mac_bridge.key[3] = FIELD_PREP(GSWIP_TABLE_MAC_BRIDGE_KEY3_FID, fid);
1176 	mac_bridge.val[0] = add ? BIT(port) : 0; /* port map */
1177 	if (GSWIP_VERSION_GE(priv, GSWIP_VERSION_2_2_ETC))
1178 		mac_bridge.val[1] = add ? (GSWIP_TABLE_MAC_BRIDGE_VAL1_STATIC |
1179 					   GSWIP_TABLE_MAC_BRIDGE_VAL1_VALID) : 0;
1180 	else
1181 		mac_bridge.val[1] = GSWIP_TABLE_MAC_BRIDGE_VAL1_STATIC;
1182 
1183 	mac_bridge.valid = add;
1184 
1185 	err = gswip_pce_table_entry_write(priv, &mac_bridge);
1186 	if (err)
1187 		dev_err(priv->dev, "failed to write mac bridge: %d\n", err);
1188 
1189 	return err;
1190 }
1191 
gswip_port_fdb_add(struct dsa_switch * ds,int port,const unsigned char * addr,u16 vid,struct dsa_db db)1192 static int gswip_port_fdb_add(struct dsa_switch *ds, int port,
1193 			      const unsigned char *addr, u16 vid,
1194 			      struct dsa_db db)
1195 {
1196 	if (db.type != DSA_DB_BRIDGE)
1197 		return -EOPNOTSUPP;
1198 
1199 	return gswip_port_fdb(ds, port, db.bridge.dev, addr, vid, true);
1200 }
1201 
gswip_port_fdb_del(struct dsa_switch * ds,int port,const unsigned char * addr,u16 vid,struct dsa_db db)1202 static int gswip_port_fdb_del(struct dsa_switch *ds, int port,
1203 			      const unsigned char *addr, u16 vid,
1204 			      struct dsa_db db)
1205 {
1206 	if (db.type != DSA_DB_BRIDGE)
1207 		return -EOPNOTSUPP;
1208 
1209 	return gswip_port_fdb(ds, port, db.bridge.dev, addr, vid, false);
1210 }
1211 
gswip_port_fdb_dump(struct dsa_switch * ds,int port,dsa_fdb_dump_cb_t * cb,void * data)1212 static int gswip_port_fdb_dump(struct dsa_switch *ds, int port,
1213 			       dsa_fdb_dump_cb_t *cb, void *data)
1214 {
1215 	struct gswip_priv *priv = ds->priv;
1216 	struct gswip_pce_table_entry mac_bridge = {0,};
1217 	unsigned char addr[ETH_ALEN];
1218 	int i;
1219 	int err;
1220 
1221 	for (i = 0; i < 2048; i++) {
1222 		mac_bridge.table = GSWIP_TABLE_MAC_BRIDGE;
1223 		mac_bridge.index = i;
1224 
1225 		err = gswip_pce_table_entry_read(priv, &mac_bridge);
1226 		if (err) {
1227 			dev_err(priv->dev,
1228 				"failed to read mac bridge entry %d: %d\n",
1229 				i, err);
1230 			return err;
1231 		}
1232 
1233 		if (!mac_bridge.valid)
1234 			continue;
1235 
1236 		addr[5] = mac_bridge.key[0] & 0xff;
1237 		addr[4] = (mac_bridge.key[0] >> 8) & 0xff;
1238 		addr[3] = mac_bridge.key[1] & 0xff;
1239 		addr[2] = (mac_bridge.key[1] >> 8) & 0xff;
1240 		addr[1] = mac_bridge.key[2] & 0xff;
1241 		addr[0] = (mac_bridge.key[2] >> 8) & 0xff;
1242 		if (mac_bridge.val[1] & GSWIP_TABLE_MAC_BRIDGE_VAL1_STATIC) {
1243 			if (mac_bridge.val[0] & BIT(port)) {
1244 				err = cb(addr, 0, true, data);
1245 				if (err)
1246 					return err;
1247 			}
1248 		} else {
1249 			if (port == FIELD_GET(GSWIP_TABLE_MAC_BRIDGE_VAL0_PORT,
1250 					      mac_bridge.val[0])) {
1251 				err = cb(addr, 0, false, data);
1252 				if (err)
1253 					return err;
1254 			}
1255 		}
1256 	}
1257 	return 0;
1258 }
1259 
gswip_port_max_mtu(struct dsa_switch * ds,int port)1260 static int gswip_port_max_mtu(struct dsa_switch *ds, int port)
1261 {
1262 	/* Includes 8 bytes for special header. */
1263 	return GSWIP_MAX_PACKET_LENGTH - VLAN_ETH_HLEN - ETH_FCS_LEN;
1264 }
1265 
gswip_port_change_mtu(struct dsa_switch * ds,int port,int new_mtu)1266 static int gswip_port_change_mtu(struct dsa_switch *ds, int port, int new_mtu)
1267 {
1268 	struct gswip_priv *priv = ds->priv;
1269 
1270 	/* CPU port always has maximum mtu of user ports, so use it to set
1271 	 * switch frame size, including 8 byte special header.
1272 	 */
1273 	if (dsa_is_cpu_port(ds, port)) {
1274 		new_mtu += 8;
1275 		regmap_write(priv->gswip, GSWIP_MAC_FLEN,
1276 			     VLAN_ETH_HLEN + new_mtu + ETH_FCS_LEN);
1277 	}
1278 
1279 	/* Enable MLEN for ports with non-standard MTUs, including the special
1280 	 * header on the CPU port added above.
1281 	 */
1282 	if (new_mtu != ETH_DATA_LEN)
1283 		regmap_set_bits(priv->gswip, GSWIP_MAC_CTRL_2p(port),
1284 				GSWIP_MAC_CTRL_2_MLEN);
1285 	else
1286 		regmap_clear_bits(priv->gswip, GSWIP_MAC_CTRL_2p(port),
1287 				  GSWIP_MAC_CTRL_2_MLEN);
1288 
1289 	return 0;
1290 }
1291 
gswip_phylink_get_caps(struct dsa_switch * ds,int port,struct phylink_config * config)1292 static void gswip_phylink_get_caps(struct dsa_switch *ds, int port,
1293 				   struct phylink_config *config)
1294 {
1295 	struct gswip_priv *priv = ds->priv;
1296 
1297 	priv->hw_info->phylink_get_caps(ds, port, config);
1298 }
1299 
gswip_port_set_link(struct gswip_priv * priv,int port,bool link)1300 static void gswip_port_set_link(struct gswip_priv *priv, int port, bool link)
1301 {
1302 	u32 mdio_phy;
1303 
1304 	if (link)
1305 		mdio_phy = GSWIP_MDIO_PHY_LINK_UP;
1306 	else
1307 		mdio_phy = GSWIP_MDIO_PHY_LINK_DOWN;
1308 
1309 	regmap_write_bits(priv->mdio, GSWIP_MDIO_PHYp(port),
1310 			  GSWIP_MDIO_PHY_LINK_MASK, mdio_phy);
1311 }
1312 
gswip_port_set_speed(struct gswip_priv * priv,int port,int speed,phy_interface_t interface)1313 static void gswip_port_set_speed(struct gswip_priv *priv, int port, int speed,
1314 				 phy_interface_t interface)
1315 {
1316 	u32 mdio_phy = 0, mii_cfg = 0, mac_ctrl_0 = 0;
1317 
1318 	switch (speed) {
1319 	case SPEED_10:
1320 		mdio_phy = GSWIP_MDIO_PHY_SPEED_M10;
1321 
1322 		if (interface == PHY_INTERFACE_MODE_RMII)
1323 			mii_cfg = GSWIP_MII_CFG_RATE_M50;
1324 		else
1325 			mii_cfg = GSWIP_MII_CFG_RATE_M2P5;
1326 
1327 		mac_ctrl_0 = GSWIP_MAC_CTRL_0_GMII_MII;
1328 		break;
1329 
1330 	case SPEED_100:
1331 		mdio_phy = GSWIP_MDIO_PHY_SPEED_M100;
1332 
1333 		if (interface == PHY_INTERFACE_MODE_RMII)
1334 			mii_cfg = GSWIP_MII_CFG_RATE_M50;
1335 		else
1336 			mii_cfg = GSWIP_MII_CFG_RATE_M25;
1337 
1338 		mac_ctrl_0 = GSWIP_MAC_CTRL_0_GMII_MII;
1339 		break;
1340 
1341 	case SPEED_1000:
1342 		mdio_phy = GSWIP_MDIO_PHY_SPEED_G1;
1343 
1344 		mii_cfg = GSWIP_MII_CFG_RATE_M125;
1345 
1346 		mac_ctrl_0 = GSWIP_MAC_CTRL_0_GMII_RGMII;
1347 		break;
1348 	}
1349 
1350 	regmap_write_bits(priv->mdio, GSWIP_MDIO_PHYp(port),
1351 			  GSWIP_MDIO_PHY_SPEED_MASK, mdio_phy);
1352 	gswip_mii_mask_cfg(priv, GSWIP_MII_CFG_RATE_MASK, mii_cfg, port);
1353 	regmap_write_bits(priv->gswip, GSWIP_MAC_CTRL_0p(port),
1354 			  GSWIP_MAC_CTRL_0_GMII_MASK, mac_ctrl_0);
1355 }
1356 
gswip_port_set_duplex(struct gswip_priv * priv,int port,int duplex)1357 static void gswip_port_set_duplex(struct gswip_priv *priv, int port, int duplex)
1358 {
1359 	u32 mac_ctrl_0, mdio_phy;
1360 
1361 	if (duplex == DUPLEX_FULL) {
1362 		mac_ctrl_0 = GSWIP_MAC_CTRL_0_FDUP_EN;
1363 		mdio_phy = GSWIP_MDIO_PHY_FDUP_EN;
1364 	} else {
1365 		mac_ctrl_0 = GSWIP_MAC_CTRL_0_FDUP_DIS;
1366 		mdio_phy = GSWIP_MDIO_PHY_FDUP_DIS;
1367 	}
1368 
1369 	regmap_write_bits(priv->gswip, GSWIP_MAC_CTRL_0p(port),
1370 			  GSWIP_MAC_CTRL_0_FDUP_MASK, mac_ctrl_0);
1371 	regmap_write_bits(priv->mdio, GSWIP_MDIO_PHYp(port),
1372 			  GSWIP_MDIO_PHY_FDUP_MASK, mdio_phy);
1373 }
1374 
gswip_port_set_pause(struct gswip_priv * priv,int port,bool tx_pause,bool rx_pause)1375 static void gswip_port_set_pause(struct gswip_priv *priv, int port,
1376 				 bool tx_pause, bool rx_pause)
1377 {
1378 	u32 mac_ctrl_0, mdio_phy;
1379 
1380 	if (tx_pause && rx_pause) {
1381 		mac_ctrl_0 = GSWIP_MAC_CTRL_0_FCON_RXTX;
1382 		mdio_phy = GSWIP_MDIO_PHY_FCONTX_EN |
1383 			   GSWIP_MDIO_PHY_FCONRX_EN;
1384 	} else if (tx_pause) {
1385 		mac_ctrl_0 = GSWIP_MAC_CTRL_0_FCON_TX;
1386 		mdio_phy = GSWIP_MDIO_PHY_FCONTX_EN |
1387 			   GSWIP_MDIO_PHY_FCONRX_DIS;
1388 	} else if (rx_pause) {
1389 		mac_ctrl_0 = GSWIP_MAC_CTRL_0_FCON_RX;
1390 		mdio_phy = GSWIP_MDIO_PHY_FCONTX_DIS |
1391 			   GSWIP_MDIO_PHY_FCONRX_EN;
1392 	} else {
1393 		mac_ctrl_0 = GSWIP_MAC_CTRL_0_FCON_NONE;
1394 		mdio_phy = GSWIP_MDIO_PHY_FCONTX_DIS |
1395 			   GSWIP_MDIO_PHY_FCONRX_DIS;
1396 	}
1397 
1398 	regmap_write_bits(priv->gswip, GSWIP_MAC_CTRL_0p(port),
1399 			  GSWIP_MAC_CTRL_0_FCON_MASK, mac_ctrl_0);
1400 	regmap_write_bits(priv->mdio, GSWIP_MDIO_PHYp(port),
1401 			  GSWIP_MDIO_PHY_FCONTX_MASK | GSWIP_MDIO_PHY_FCONRX_MASK,
1402 			  mdio_phy);
1403 }
1404 
gswip_phylink_mac_config(struct phylink_config * config,unsigned int mode,const struct phylink_link_state * state)1405 static void gswip_phylink_mac_config(struct phylink_config *config,
1406 				     unsigned int mode,
1407 				     const struct phylink_link_state *state)
1408 {
1409 	struct dsa_port *dp = dsa_phylink_to_port(config);
1410 	struct gswip_priv *priv = dp->ds->priv;
1411 	int port = dp->index;
1412 	u32 miicfg = 0;
1413 
1414 	miicfg |= GSWIP_MII_CFG_LDCLKDIS;
1415 
1416 	switch (state->interface) {
1417 	case PHY_INTERFACE_MODE_SGMII:
1418 	case PHY_INTERFACE_MODE_1000BASEX:
1419 	case PHY_INTERFACE_MODE_2500BASEX:
1420 		return;
1421 	case PHY_INTERFACE_MODE_MII:
1422 	case PHY_INTERFACE_MODE_INTERNAL:
1423 		miicfg |= GSWIP_MII_CFG_MODE_MIIM;
1424 		break;
1425 	case PHY_INTERFACE_MODE_REVMII:
1426 		miicfg |= GSWIP_MII_CFG_MODE_MIIP;
1427 		break;
1428 	case PHY_INTERFACE_MODE_RMII:
1429 		miicfg |= GSWIP_MII_CFG_MODE_RMIIM;
1430 		if (of_property_read_bool(dp->dn, "maxlinear,rmii-refclk-out"))
1431 			miicfg |= GSWIP_MII_CFG_RMII_CLK;
1432 		break;
1433 	case PHY_INTERFACE_MODE_RGMII:
1434 	case PHY_INTERFACE_MODE_RGMII_ID:
1435 	case PHY_INTERFACE_MODE_RGMII_RXID:
1436 	case PHY_INTERFACE_MODE_RGMII_TXID:
1437 		miicfg |= GSWIP_MII_CFG_MODE_RGMII;
1438 		break;
1439 	case PHY_INTERFACE_MODE_GMII:
1440 		miicfg |= GSWIP_MII_CFG_MODE_GMII;
1441 		break;
1442 	default:
1443 		dev_err(dp->ds->dev,
1444 			"Unsupported interface: %d\n", state->interface);
1445 		return;
1446 	}
1447 
1448 	gswip_mii_mask_cfg(priv,
1449 			   GSWIP_MII_CFG_MODE_MASK | GSWIP_MII_CFG_RMII_CLK |
1450 			   GSWIP_MII_CFG_RGMII_IBS | GSWIP_MII_CFG_LDCLKDIS,
1451 			   miicfg, port);
1452 
1453 	gswip_mii_delay_setup(priv, dp, state->interface);
1454 }
1455 
gswip_phylink_mac_link_down(struct phylink_config * config,unsigned int mode,phy_interface_t interface)1456 static void gswip_phylink_mac_link_down(struct phylink_config *config,
1457 					unsigned int mode,
1458 					phy_interface_t interface)
1459 {
1460 	struct dsa_port *dp = dsa_phylink_to_port(config);
1461 	struct gswip_priv *priv = dp->ds->priv;
1462 	int port = dp->index;
1463 
1464 	gswip_mii_mask_cfg(priv, GSWIP_MII_CFG_EN, 0, port);
1465 
1466 	if (!dsa_port_is_cpu(dp))
1467 		gswip_port_set_link(priv, port, false);
1468 }
1469 
gswip_phylink_mac_link_up(struct phylink_config * config,struct phy_device * phydev,unsigned int mode,phy_interface_t interface,int speed,int duplex,bool tx_pause,bool rx_pause)1470 static void gswip_phylink_mac_link_up(struct phylink_config *config,
1471 				      struct phy_device *phydev,
1472 				      unsigned int mode,
1473 				      phy_interface_t interface,
1474 				      int speed, int duplex,
1475 				      bool tx_pause, bool rx_pause)
1476 {
1477 	struct dsa_port *dp = dsa_phylink_to_port(config);
1478 	struct gswip_priv *priv = dp->ds->priv;
1479 	int port = dp->index;
1480 
1481 	if (!dsa_port_is_cpu(dp) || interface != PHY_INTERFACE_MODE_INTERNAL) {
1482 		gswip_port_set_link(priv, port, true);
1483 		gswip_port_set_speed(priv, port, speed, interface);
1484 		gswip_port_set_duplex(priv, port, duplex);
1485 		gswip_port_set_pause(priv, port, tx_pause, rx_pause);
1486 	}
1487 
1488 	gswip_mii_mask_cfg(priv, GSWIP_MII_CFG_EN, GSWIP_MII_CFG_EN, port);
1489 }
1490 
gswip_get_strings(struct dsa_switch * ds,int port,u32 stringset,uint8_t * data)1491 static void gswip_get_strings(struct dsa_switch *ds, int port, u32 stringset,
1492 			      uint8_t *data)
1493 {
1494 	int i;
1495 
1496 	if (stringset != ETH_SS_STATS)
1497 		return;
1498 
1499 	for (i = 0; i < ARRAY_SIZE(gswip_rmon_cnt); i++)
1500 		ethtool_puts(&data, gswip_rmon_cnt[i].name);
1501 }
1502 
gswip_bcm_ram_entry_read(struct gswip_priv * priv,u32 table,u32 index)1503 static u32 gswip_bcm_ram_entry_read(struct gswip_priv *priv, u32 table,
1504 				    u32 index)
1505 {
1506 	u32 result, val;
1507 	int err;
1508 
1509 	regmap_write(priv->gswip, GSWIP_BM_RAM_ADDR, index);
1510 	regmap_write_bits(priv->gswip, GSWIP_BM_RAM_CTRL,
1511 			  GSWIP_BM_RAM_CTRL_ADDR_MASK | GSWIP_BM_RAM_CTRL_OPMOD |
1512 			  GSWIP_BM_RAM_CTRL_BAS,
1513 			  table | GSWIP_BM_RAM_CTRL_BAS);
1514 
1515 	err = gswip_switch_r_timeout(priv, GSWIP_BM_RAM_CTRL,
1516 				     GSWIP_BM_RAM_CTRL_BAS);
1517 	if (err) {
1518 		dev_err(priv->dev, "timeout while reading table: %u, index: %u\n",
1519 			table, index);
1520 		return 0;
1521 	}
1522 
1523 	regmap_read(priv->gswip, GSWIP_BM_RAM_VAL(0), &result);
1524 	regmap_read(priv->gswip, GSWIP_BM_RAM_VAL(1), &val);
1525 	result |= val << 16;
1526 
1527 	return result;
1528 }
1529 
gswip_get_ethtool_stats(struct dsa_switch * ds,int port,uint64_t * data)1530 static void gswip_get_ethtool_stats(struct dsa_switch *ds, int port,
1531 				    uint64_t *data)
1532 {
1533 	struct gswip_priv *priv = ds->priv;
1534 	const struct gswip_rmon_cnt_desc *rmon_cnt;
1535 	int i;
1536 	u64 high;
1537 
1538 	for (i = 0; i < ARRAY_SIZE(gswip_rmon_cnt); i++) {
1539 		rmon_cnt = &gswip_rmon_cnt[i];
1540 
1541 		data[i] = gswip_bcm_ram_entry_read(priv, port,
1542 						   rmon_cnt->offset);
1543 		if (rmon_cnt->size == 2) {
1544 			high = gswip_bcm_ram_entry_read(priv, port,
1545 							rmon_cnt->offset + 1);
1546 			data[i] |= high << 32;
1547 		}
1548 	}
1549 }
1550 
gswip_get_sset_count(struct dsa_switch * ds,int port,int sset)1551 static int gswip_get_sset_count(struct dsa_switch *ds, int port, int sset)
1552 {
1553 	if (sset != ETH_SS_STATS)
1554 		return 0;
1555 
1556 	return ARRAY_SIZE(gswip_rmon_cnt);
1557 }
1558 
gswip_set_mac_eee(struct dsa_switch * ds,int port,struct ethtool_keee * e)1559 static int gswip_set_mac_eee(struct dsa_switch *ds, int port,
1560 			     struct ethtool_keee *e)
1561 {
1562 	if (e->tx_lpi_timer > 0x7f)
1563 		return -EINVAL;
1564 
1565 	return 0;
1566 }
1567 
gswip_phylink_mac_disable_tx_lpi(struct phylink_config * config)1568 static void gswip_phylink_mac_disable_tx_lpi(struct phylink_config *config)
1569 {
1570 	struct dsa_port *dp = dsa_phylink_to_port(config);
1571 	struct gswip_priv *priv = dp->ds->priv;
1572 
1573 	regmap_clear_bits(priv->gswip, GSWIP_MAC_CTRL_4p(dp->index),
1574 			  GSWIP_MAC_CTRL_4_LPIEN);
1575 }
1576 
gswip_phylink_mac_enable_tx_lpi(struct phylink_config * config,u32 timer,bool tx_clock_stop)1577 static int gswip_phylink_mac_enable_tx_lpi(struct phylink_config *config,
1578 					   u32 timer, bool tx_clock_stop)
1579 {
1580 	struct dsa_port *dp = dsa_phylink_to_port(config);
1581 	struct gswip_priv *priv = dp->ds->priv;
1582 
1583 	return regmap_update_bits(priv->gswip, GSWIP_MAC_CTRL_4p(dp->index),
1584 				  GSWIP_MAC_CTRL_4_LPIEN |
1585 				  GSWIP_MAC_CTRL_4_GWAIT_MASK |
1586 				  GSWIP_MAC_CTRL_4_WAIT_MASK,
1587 				  GSWIP_MAC_CTRL_4_LPIEN |
1588 				  GSWIP_MAC_CTRL_4_GWAIT(timer) |
1589 				  GSWIP_MAC_CTRL_4_WAIT(timer));
1590 }
1591 
gswip_support_eee(struct dsa_switch * ds,int port)1592 static bool gswip_support_eee(struct dsa_switch *ds, int port)
1593 {
1594 	struct gswip_priv *priv = ds->priv;
1595 
1596 	if (GSWIP_VERSION_GE(priv, GSWIP_VERSION_2_2))
1597 		return true;
1598 
1599 	return false;
1600 }
1601 
gswip_phylink_mac_select_pcs(struct phylink_config * config,phy_interface_t interface)1602 static struct phylink_pcs *gswip_phylink_mac_select_pcs(struct phylink_config *config,
1603 							phy_interface_t interface)
1604 {
1605 	struct dsa_port *dp = dsa_phylink_to_port(config);
1606 	struct gswip_priv *priv = dp->ds->priv;
1607 
1608 	if (priv->hw_info->mac_select_pcs)
1609 		return priv->hw_info->mac_select_pcs(config, interface);
1610 
1611 	return NULL;
1612 }
1613 
1614 static const struct phylink_mac_ops gswip_phylink_mac_ops = {
1615 	.mac_config		= gswip_phylink_mac_config,
1616 	.mac_link_down		= gswip_phylink_mac_link_down,
1617 	.mac_link_up		= gswip_phylink_mac_link_up,
1618 	.mac_disable_tx_lpi	= gswip_phylink_mac_disable_tx_lpi,
1619 	.mac_enable_tx_lpi	= gswip_phylink_mac_enable_tx_lpi,
1620 	.mac_select_pcs		= gswip_phylink_mac_select_pcs,
1621 };
1622 
1623 static const struct dsa_switch_ops gswip_switch_ops = {
1624 	.get_tag_protocol	= gswip_get_tag_protocol,
1625 	.setup			= gswip_setup,
1626 	.teardown		= gswip_teardown,
1627 	.port_setup		= gswip_port_setup,
1628 	.port_enable		= gswip_port_enable,
1629 	.port_disable		= gswip_port_disable,
1630 	.port_pre_bridge_flags	= gswip_port_pre_bridge_flags,
1631 	.port_bridge_flags	= gswip_port_bridge_flags,
1632 	.port_bridge_join	= gswip_port_bridge_join,
1633 	.port_bridge_leave	= gswip_port_bridge_leave,
1634 	.port_fast_age		= gswip_port_fast_age,
1635 	.port_vlan_filtering	= gswip_port_vlan_filtering,
1636 	.port_vlan_add		= gswip_port_vlan_add,
1637 	.port_vlan_del		= gswip_port_vlan_del,
1638 	.port_stp_state_set	= gswip_port_stp_state_set,
1639 	.port_fdb_add		= gswip_port_fdb_add,
1640 	.port_fdb_del		= gswip_port_fdb_del,
1641 	.port_fdb_dump		= gswip_port_fdb_dump,
1642 	.port_change_mtu	= gswip_port_change_mtu,
1643 	.port_max_mtu		= gswip_port_max_mtu,
1644 	.phylink_get_caps	= gswip_phylink_get_caps,
1645 	.get_strings		= gswip_get_strings,
1646 	.get_ethtool_stats	= gswip_get_ethtool_stats,
1647 	.get_sset_count		= gswip_get_sset_count,
1648 	.set_mac_eee		= gswip_set_mac_eee,
1649 	.support_eee		= gswip_support_eee,
1650 	.port_hsr_join		= dsa_port_simple_hsr_join,
1651 	.port_hsr_leave		= dsa_port_simple_hsr_leave,
1652 };
1653 
gswip_validate_cpu_port(struct dsa_switch * ds)1654 static int gswip_validate_cpu_port(struct dsa_switch *ds)
1655 {
1656 	struct gswip_priv *priv = ds->priv;
1657 	struct dsa_port *cpu_dp;
1658 	int cpu_port = -1;
1659 
1660 	dsa_switch_for_each_cpu_port(cpu_dp, ds) {
1661 		if (cpu_port != -1)
1662 			return dev_err_probe(ds->dev, -EINVAL,
1663 					     "only a single CPU port is supported\n");
1664 
1665 		cpu_port = cpu_dp->index;
1666 	}
1667 
1668 	if (cpu_port == -1)
1669 		return dev_err_probe(ds->dev, -EINVAL, "no CPU port defined\n");
1670 
1671 	if (BIT(cpu_port) & ~priv->hw_info->allowed_cpu_ports)
1672 		return dev_err_probe(ds->dev, -EINVAL,
1673 				     "unsupported CPU port defined\n");
1674 
1675 	return 0;
1676 }
1677 
gswip_probe_common(struct gswip_priv * priv,u32 version)1678 int gswip_probe_common(struct gswip_priv *priv, u32 version)
1679 {
1680 	int err;
1681 
1682 	mutex_init(&priv->pce_table_lock);
1683 
1684 	priv->ds = devm_kzalloc(priv->dev, sizeof(*priv->ds), GFP_KERNEL);
1685 	if (!priv->ds)
1686 		return -ENOMEM;
1687 
1688 	priv->ds->dev = priv->dev;
1689 	priv->ds->num_ports = priv->hw_info->max_ports;
1690 	priv->ds->ops = &gswip_switch_ops;
1691 	priv->ds->phylink_mac_ops = &gswip_phylink_mac_ops;
1692 	priv->ds->priv = priv;
1693 
1694 	/* The hardware has the 'major/minor' version bytes in the wrong order
1695 	 * preventing numerical comparisons. Construct a 16-bit unsigned integer
1696 	 * having the REV field as most significant byte and the MOD field as
1697 	 * least significant byte. This is effectively swapping the two bytes of
1698 	 * the version variable, but other than using swab16 it doesn't affect
1699 	 * the source variable.
1700 	 */
1701 	priv->version = GSWIP_VERSION_REV(version) << 8 |
1702 			GSWIP_VERSION_MOD(version);
1703 
1704 	err = dsa_register_switch(priv->ds);
1705 	if (err)
1706 		return dev_err_probe(priv->dev, err, "dsa switch registration failed\n");
1707 
1708 	err = gswip_validate_cpu_port(priv->ds);
1709 	if (err)
1710 		goto unregister_switch;
1711 
1712 	dev_info(priv->dev, "probed GSWIP version %lx mod %lx\n",
1713 		 GSWIP_VERSION_REV(version), GSWIP_VERSION_MOD(version));
1714 
1715 	return 0;
1716 
1717 unregister_switch:
1718 	dsa_unregister_switch(priv->ds);
1719 
1720 	return err;
1721 }
1722 EXPORT_SYMBOL_GPL(gswip_probe_common);
1723 
1724 MODULE_AUTHOR("Hauke Mehrtens <hauke@hauke-m.de>");
1725 MODULE_AUTHOR("Daniel Golle <daniel@makrotopia.org>");
1726 MODULE_DESCRIPTION("Lantiq / Intel / MaxLinear GSWIP common functions");
1727 MODULE_LICENSE("GPL");
1728