xref: /linux/drivers/net/dsa/lantiq_gswip.c (revision 06b9cce42634a50f2840777a66553b02320db5ef)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Lantiq / Intel GSWIP switch driver for VRX200, xRX300 and xRX330 SoCs
4  *
5  * Copyright (C) 2010 Lantiq Deutschland
6  * Copyright (C) 2012 John Crispin <john@phrozen.org>
7  * Copyright (C) 2017 - 2019 Hauke Mehrtens <hauke@hauke-m.de>
8  *
9  * The VLAN and bridge model the GSWIP hardware uses does not directly
10  * matches the model DSA uses.
11  *
12  * The hardware has 64 possible table entries for bridges with one VLAN
13  * ID, one flow id and a list of ports for each bridge. All entries which
14  * match the same flow ID are combined in the mac learning table, they
15  * act as one global bridge.
16  * The hardware does not support VLAN filter on the port, but on the
17  * bridge, this driver converts the DSA model to the hardware.
18  *
19  * The CPU gets all the exception frames which do not match any forwarding
20  * rule and the CPU port is also added to all bridges. This makes it possible
21  * to handle all the special cases easily in software.
22  * At the initialization the driver allocates one bridge table entry for
23  * each switch port which is used when the port is used without an
24  * explicit bridge. This prevents the frames from being forwarded
25  * between all LAN ports by default.
26  */
27 
28 #include <linux/clk.h>
29 #include <linux/delay.h>
30 #include <linux/etherdevice.h>
31 #include <linux/firmware.h>
32 #include <linux/if_bridge.h>
33 #include <linux/if_vlan.h>
34 #include <linux/iopoll.h>
35 #include <linux/mfd/syscon.h>
36 #include <linux/module.h>
37 #include <linux/of_mdio.h>
38 #include <linux/of_net.h>
39 #include <linux/of_platform.h>
40 #include <linux/phy.h>
41 #include <linux/phylink.h>
42 #include <linux/platform_device.h>
43 #include <linux/regmap.h>
44 #include <linux/reset.h>
45 #include <net/dsa.h>
46 #include <dt-bindings/mips/lantiq_rcu_gphy.h>
47 
48 #include "lantiq_pce.h"
49 
50 /* GSWIP MDIO Registers */
51 #define GSWIP_MDIO_GLOB			0x00
52 #define  GSWIP_MDIO_GLOB_ENABLE		BIT(15)
53 #define GSWIP_MDIO_CTRL			0x08
54 #define  GSWIP_MDIO_CTRL_BUSY		BIT(12)
55 #define  GSWIP_MDIO_CTRL_RD		BIT(11)
56 #define  GSWIP_MDIO_CTRL_WR		BIT(10)
57 #define  GSWIP_MDIO_CTRL_PHYAD_MASK	0x1f
58 #define  GSWIP_MDIO_CTRL_PHYAD_SHIFT	5
59 #define  GSWIP_MDIO_CTRL_REGAD_MASK	0x1f
60 #define GSWIP_MDIO_READ			0x09
61 #define GSWIP_MDIO_WRITE		0x0A
62 #define GSWIP_MDIO_MDC_CFG0		0x0B
63 #define GSWIP_MDIO_MDC_CFG1		0x0C
64 #define GSWIP_MDIO_PHYp(p)		(0x15 - (p))
65 #define  GSWIP_MDIO_PHY_LINK_MASK	0x6000
66 #define  GSWIP_MDIO_PHY_LINK_AUTO	0x0000
67 #define  GSWIP_MDIO_PHY_LINK_DOWN	0x4000
68 #define  GSWIP_MDIO_PHY_LINK_UP		0x2000
69 #define  GSWIP_MDIO_PHY_SPEED_MASK	0x1800
70 #define  GSWIP_MDIO_PHY_SPEED_AUTO	0x1800
71 #define  GSWIP_MDIO_PHY_SPEED_M10	0x0000
72 #define  GSWIP_MDIO_PHY_SPEED_M100	0x0800
73 #define  GSWIP_MDIO_PHY_SPEED_G1	0x1000
74 #define  GSWIP_MDIO_PHY_FDUP_MASK	0x0600
75 #define  GSWIP_MDIO_PHY_FDUP_AUTO	0x0000
76 #define  GSWIP_MDIO_PHY_FDUP_EN		0x0200
77 #define  GSWIP_MDIO_PHY_FDUP_DIS	0x0600
78 #define  GSWIP_MDIO_PHY_FCONTX_MASK	0x0180
79 #define  GSWIP_MDIO_PHY_FCONTX_AUTO	0x0000
80 #define  GSWIP_MDIO_PHY_FCONTX_EN	0x0100
81 #define  GSWIP_MDIO_PHY_FCONTX_DIS	0x0180
82 #define  GSWIP_MDIO_PHY_FCONRX_MASK	0x0060
83 #define  GSWIP_MDIO_PHY_FCONRX_AUTO	0x0000
84 #define  GSWIP_MDIO_PHY_FCONRX_EN	0x0020
85 #define  GSWIP_MDIO_PHY_FCONRX_DIS	0x0060
86 #define  GSWIP_MDIO_PHY_ADDR_MASK	0x001f
87 #define  GSWIP_MDIO_PHY_MASK		(GSWIP_MDIO_PHY_ADDR_MASK | \
88 					 GSWIP_MDIO_PHY_FCONRX_MASK | \
89 					 GSWIP_MDIO_PHY_FCONTX_MASK | \
90 					 GSWIP_MDIO_PHY_LINK_MASK | \
91 					 GSWIP_MDIO_PHY_SPEED_MASK | \
92 					 GSWIP_MDIO_PHY_FDUP_MASK)
93 
94 /* GSWIP MII Registers */
95 #define GSWIP_MII_CFGp(p)		(0x2 * (p))
96 #define  GSWIP_MII_CFG_RESET		BIT(15)
97 #define  GSWIP_MII_CFG_EN		BIT(14)
98 #define  GSWIP_MII_CFG_ISOLATE		BIT(13)
99 #define  GSWIP_MII_CFG_LDCLKDIS		BIT(12)
100 #define  GSWIP_MII_CFG_RGMII_IBS	BIT(8)
101 #define  GSWIP_MII_CFG_RMII_CLK		BIT(7)
102 #define  GSWIP_MII_CFG_MODE_MIIP	0x0
103 #define  GSWIP_MII_CFG_MODE_MIIM	0x1
104 #define  GSWIP_MII_CFG_MODE_RMIIP	0x2
105 #define  GSWIP_MII_CFG_MODE_RMIIM	0x3
106 #define  GSWIP_MII_CFG_MODE_RGMII	0x4
107 #define  GSWIP_MII_CFG_MODE_GMII	0x9
108 #define  GSWIP_MII_CFG_MODE_MASK	0xf
109 #define  GSWIP_MII_CFG_RATE_M2P5	0x00
110 #define  GSWIP_MII_CFG_RATE_M25	0x10
111 #define  GSWIP_MII_CFG_RATE_M125	0x20
112 #define  GSWIP_MII_CFG_RATE_M50	0x30
113 #define  GSWIP_MII_CFG_RATE_AUTO	0x40
114 #define  GSWIP_MII_CFG_RATE_MASK	0x70
115 #define GSWIP_MII_PCDU0			0x01
116 #define GSWIP_MII_PCDU1			0x03
117 #define GSWIP_MII_PCDU5			0x05
118 #define  GSWIP_MII_PCDU_TXDLY_MASK	GENMASK(2, 0)
119 #define  GSWIP_MII_PCDU_RXDLY_MASK	GENMASK(9, 7)
120 
121 /* GSWIP Core Registers */
122 #define GSWIP_SWRES			0x000
123 #define  GSWIP_SWRES_R1			BIT(1)	/* GSWIP Software reset */
124 #define  GSWIP_SWRES_R0			BIT(0)	/* GSWIP Hardware reset */
125 #define GSWIP_VERSION			0x013
126 #define  GSWIP_VERSION_REV_SHIFT	0
127 #define  GSWIP_VERSION_REV_MASK		GENMASK(7, 0)
128 #define  GSWIP_VERSION_MOD_SHIFT	8
129 #define  GSWIP_VERSION_MOD_MASK		GENMASK(15, 8)
130 #define   GSWIP_VERSION_2_0		0x100
131 #define   GSWIP_VERSION_2_1		0x021
132 #define   GSWIP_VERSION_2_2		0x122
133 #define   GSWIP_VERSION_2_2_ETC		0x022
134 
135 #define GSWIP_BM_RAM_VAL(x)		(0x043 - (x))
136 #define GSWIP_BM_RAM_ADDR		0x044
137 #define GSWIP_BM_RAM_CTRL		0x045
138 #define  GSWIP_BM_RAM_CTRL_BAS		BIT(15)
139 #define  GSWIP_BM_RAM_CTRL_OPMOD	BIT(5)
140 #define  GSWIP_BM_RAM_CTRL_ADDR_MASK	GENMASK(4, 0)
141 #define GSWIP_BM_QUEUE_GCTRL		0x04A
142 #define  GSWIP_BM_QUEUE_GCTRL_GL_MOD	BIT(10)
143 /* buffer management Port Configuration Register */
144 #define GSWIP_BM_PCFGp(p)		(0x080 + ((p) * 2))
145 #define  GSWIP_BM_PCFG_CNTEN		BIT(0)	/* RMON Counter Enable */
146 #define  GSWIP_BM_PCFG_IGCNT		BIT(1)	/* Ingres Special Tag RMON count */
147 /* buffer management Port Control Register */
148 #define GSWIP_BM_RMON_CTRLp(p)		(0x81 + ((p) * 2))
149 #define  GSWIP_BM_CTRL_RMON_RAM1_RES	BIT(0)	/* Software Reset for RMON RAM 1 */
150 #define  GSWIP_BM_CTRL_RMON_RAM2_RES	BIT(1)	/* Software Reset for RMON RAM 2 */
151 
152 /* PCE */
153 #define GSWIP_PCE_TBL_KEY(x)		(0x447 - (x))
154 #define GSWIP_PCE_TBL_MASK		0x448
155 #define GSWIP_PCE_TBL_VAL(x)		(0x44D - (x))
156 #define GSWIP_PCE_TBL_ADDR		0x44E
157 #define GSWIP_PCE_TBL_CTRL		0x44F
158 #define  GSWIP_PCE_TBL_CTRL_BAS		BIT(15)
159 #define  GSWIP_PCE_TBL_CTRL_TYPE	BIT(13)
160 #define  GSWIP_PCE_TBL_CTRL_VLD		BIT(12)
161 #define  GSWIP_PCE_TBL_CTRL_KEYFORM	BIT(11)
162 #define  GSWIP_PCE_TBL_CTRL_GMAP_MASK	GENMASK(10, 7)
163 #define  GSWIP_PCE_TBL_CTRL_OPMOD_MASK	GENMASK(6, 5)
164 #define  GSWIP_PCE_TBL_CTRL_OPMOD_ADRD	0x00
165 #define  GSWIP_PCE_TBL_CTRL_OPMOD_ADWR	0x20
166 #define  GSWIP_PCE_TBL_CTRL_OPMOD_KSRD	0x40
167 #define  GSWIP_PCE_TBL_CTRL_OPMOD_KSWR	0x60
168 #define  GSWIP_PCE_TBL_CTRL_ADDR_MASK	GENMASK(4, 0)
169 #define GSWIP_PCE_PMAP1			0x453	/* Monitoring port map */
170 #define GSWIP_PCE_PMAP2			0x454	/* Default Multicast port map */
171 #define GSWIP_PCE_PMAP3			0x455	/* Default Unknown Unicast port map */
172 #define GSWIP_PCE_GCTRL_0		0x456
173 #define  GSWIP_PCE_GCTRL_0_MTFL		BIT(0)  /* MAC Table Flushing */
174 #define  GSWIP_PCE_GCTRL_0_MC_VALID	BIT(3)
175 #define  GSWIP_PCE_GCTRL_0_VLAN		BIT(14) /* VLAN aware Switching */
176 #define GSWIP_PCE_GCTRL_1		0x457
177 #define  GSWIP_PCE_GCTRL_1_MAC_GLOCK	BIT(2)	/* MAC Address table lock */
178 #define  GSWIP_PCE_GCTRL_1_MAC_GLOCK_MOD	BIT(3) /* Mac address table lock forwarding mode */
179 #define GSWIP_PCE_PCTRL_0p(p)		(0x480 + ((p) * 0xA))
180 #define  GSWIP_PCE_PCTRL_0_TVM		BIT(5)	/* Transparent VLAN mode */
181 #define  GSWIP_PCE_PCTRL_0_VREP		BIT(6)	/* VLAN Replace Mode */
182 #define  GSWIP_PCE_PCTRL_0_INGRESS	BIT(11)	/* Accept special tag in ingress */
183 #define  GSWIP_PCE_PCTRL_0_PSTATE_LISTEN	0x0
184 #define  GSWIP_PCE_PCTRL_0_PSTATE_RX		0x1
185 #define  GSWIP_PCE_PCTRL_0_PSTATE_TX		0x2
186 #define  GSWIP_PCE_PCTRL_0_PSTATE_LEARNING	0x3
187 #define  GSWIP_PCE_PCTRL_0_PSTATE_FORWARDING	0x7
188 #define  GSWIP_PCE_PCTRL_0_PSTATE_MASK	GENMASK(2, 0)
189 #define GSWIP_PCE_VCTRL(p)		(0x485 + ((p) * 0xA))
190 #define  GSWIP_PCE_VCTRL_UVR		BIT(0)	/* Unknown VLAN Rule */
191 #define  GSWIP_PCE_VCTRL_VIMR		BIT(3)	/* VLAN Ingress Member violation rule */
192 #define  GSWIP_PCE_VCTRL_VEMR		BIT(4)	/* VLAN Egress Member violation rule */
193 #define  GSWIP_PCE_VCTRL_VSR		BIT(5)	/* VLAN Security */
194 #define  GSWIP_PCE_VCTRL_VID0		BIT(6)	/* Priority Tagged Rule */
195 #define GSWIP_PCE_DEFPVID(p)		(0x486 + ((p) * 0xA))
196 
197 #define GSWIP_MAC_FLEN			0x8C5
198 #define GSWIP_MAC_CTRL_0p(p)		(0x903 + ((p) * 0xC))
199 #define  GSWIP_MAC_CTRL_0_PADEN		BIT(8)
200 #define  GSWIP_MAC_CTRL_0_FCS_EN	BIT(7)
201 #define  GSWIP_MAC_CTRL_0_FCON_MASK	0x0070
202 #define  GSWIP_MAC_CTRL_0_FCON_AUTO	0x0000
203 #define  GSWIP_MAC_CTRL_0_FCON_RX	0x0010
204 #define  GSWIP_MAC_CTRL_0_FCON_TX	0x0020
205 #define  GSWIP_MAC_CTRL_0_FCON_RXTX	0x0030
206 #define  GSWIP_MAC_CTRL_0_FCON_NONE	0x0040
207 #define  GSWIP_MAC_CTRL_0_FDUP_MASK	0x000C
208 #define  GSWIP_MAC_CTRL_0_FDUP_AUTO	0x0000
209 #define  GSWIP_MAC_CTRL_0_FDUP_EN	0x0004
210 #define  GSWIP_MAC_CTRL_0_FDUP_DIS	0x000C
211 #define  GSWIP_MAC_CTRL_0_GMII_MASK	0x0003
212 #define  GSWIP_MAC_CTRL_0_GMII_AUTO	0x0000
213 #define  GSWIP_MAC_CTRL_0_GMII_MII	0x0001
214 #define  GSWIP_MAC_CTRL_0_GMII_RGMII	0x0002
215 #define GSWIP_MAC_CTRL_2p(p)		(0x905 + ((p) * 0xC))
216 #define GSWIP_MAC_CTRL_2_MLEN		BIT(3) /* Maximum Untagged Frame Lnegth */
217 
218 /* Ethernet Switch Fetch DMA Port Control Register */
219 #define GSWIP_FDMA_PCTRLp(p)		(0xA80 + ((p) * 0x6))
220 #define  GSWIP_FDMA_PCTRL_EN		BIT(0)	/* FDMA Port Enable */
221 #define  GSWIP_FDMA_PCTRL_STEN		BIT(1)	/* Special Tag Insertion Enable */
222 #define  GSWIP_FDMA_PCTRL_VLANMOD_MASK	GENMASK(4, 3)	/* VLAN Modification Control */
223 #define  GSWIP_FDMA_PCTRL_VLANMOD_SHIFT	3	/* VLAN Modification Control */
224 #define  GSWIP_FDMA_PCTRL_VLANMOD_DIS	(0x0 << GSWIP_FDMA_PCTRL_VLANMOD_SHIFT)
225 #define  GSWIP_FDMA_PCTRL_VLANMOD_PRIO	(0x1 << GSWIP_FDMA_PCTRL_VLANMOD_SHIFT)
226 #define  GSWIP_FDMA_PCTRL_VLANMOD_ID	(0x2 << GSWIP_FDMA_PCTRL_VLANMOD_SHIFT)
227 #define  GSWIP_FDMA_PCTRL_VLANMOD_BOTH	(0x3 << GSWIP_FDMA_PCTRL_VLANMOD_SHIFT)
228 
229 /* Ethernet Switch Store DMA Port Control Register */
230 #define GSWIP_SDMA_PCTRLp(p)		(0xBC0 + ((p) * 0x6))
231 #define  GSWIP_SDMA_PCTRL_EN		BIT(0)	/* SDMA Port Enable */
232 #define  GSWIP_SDMA_PCTRL_FCEN		BIT(1)	/* Flow Control Enable */
233 #define  GSWIP_SDMA_PCTRL_PAUFWD	BIT(3)	/* Pause Frame Forwarding */
234 
235 #define GSWIP_TABLE_ACTIVE_VLAN		0x01
236 #define GSWIP_TABLE_VLAN_MAPPING	0x02
237 #define GSWIP_TABLE_MAC_BRIDGE		0x0b
238 #define  GSWIP_TABLE_MAC_BRIDGE_STATIC	0x01	/* Static not, aging entry */
239 
240 #define XRX200_GPHY_FW_ALIGN	(16 * 1024)
241 
242 struct gswip_hw_info {
243 	int max_ports;
244 	int cpu_port;
245 	const struct dsa_switch_ops *ops;
246 };
247 
248 struct xway_gphy_match_data {
249 	char *fe_firmware_name;
250 	char *ge_firmware_name;
251 };
252 
253 struct gswip_gphy_fw {
254 	struct clk *clk_gate;
255 	struct reset_control *reset;
256 	u32 fw_addr_offset;
257 	char *fw_name;
258 };
259 
260 struct gswip_vlan {
261 	struct net_device *bridge;
262 	u16 vid;
263 	u8 fid;
264 };
265 
266 struct gswip_priv {
267 	__iomem void *gswip;
268 	__iomem void *mdio;
269 	__iomem void *mii;
270 	const struct gswip_hw_info *hw_info;
271 	const struct xway_gphy_match_data *gphy_fw_name_cfg;
272 	struct dsa_switch *ds;
273 	struct device *dev;
274 	struct regmap *rcu_regmap;
275 	struct gswip_vlan vlans[64];
276 	int num_gphy_fw;
277 	struct gswip_gphy_fw *gphy_fw;
278 	u32 port_vlan_filter;
279 	struct mutex pce_table_lock;
280 };
281 
282 struct gswip_pce_table_entry {
283 	u16 index;      // PCE_TBL_ADDR.ADDR = pData->table_index
284 	u16 table;      // PCE_TBL_CTRL.ADDR = pData->table
285 	u16 key[8];
286 	u16 val[5];
287 	u16 mask;
288 	u8 gmap;
289 	bool type;
290 	bool valid;
291 	bool key_mode;
292 };
293 
294 struct gswip_rmon_cnt_desc {
295 	unsigned int size;
296 	unsigned int offset;
297 	const char *name;
298 };
299 
300 #define MIB_DESC(_size, _offset, _name) {.size = _size, .offset = _offset, .name = _name}
301 
302 static const struct gswip_rmon_cnt_desc gswip_rmon_cnt[] = {
303 	/** Receive Packet Count (only packets that are accepted and not discarded). */
304 	MIB_DESC(1, 0x1F, "RxGoodPkts"),
305 	MIB_DESC(1, 0x23, "RxUnicastPkts"),
306 	MIB_DESC(1, 0x22, "RxMulticastPkts"),
307 	MIB_DESC(1, 0x21, "RxFCSErrorPkts"),
308 	MIB_DESC(1, 0x1D, "RxUnderSizeGoodPkts"),
309 	MIB_DESC(1, 0x1E, "RxUnderSizeErrorPkts"),
310 	MIB_DESC(1, 0x1B, "RxOversizeGoodPkts"),
311 	MIB_DESC(1, 0x1C, "RxOversizeErrorPkts"),
312 	MIB_DESC(1, 0x20, "RxGoodPausePkts"),
313 	MIB_DESC(1, 0x1A, "RxAlignErrorPkts"),
314 	MIB_DESC(1, 0x12, "Rx64BytePkts"),
315 	MIB_DESC(1, 0x13, "Rx127BytePkts"),
316 	MIB_DESC(1, 0x14, "Rx255BytePkts"),
317 	MIB_DESC(1, 0x15, "Rx511BytePkts"),
318 	MIB_DESC(1, 0x16, "Rx1023BytePkts"),
319 	/** Receive Size 1024-1522 (or more, if configured) Packet Count. */
320 	MIB_DESC(1, 0x17, "RxMaxBytePkts"),
321 	MIB_DESC(1, 0x18, "RxDroppedPkts"),
322 	MIB_DESC(1, 0x19, "RxFilteredPkts"),
323 	MIB_DESC(2, 0x24, "RxGoodBytes"),
324 	MIB_DESC(2, 0x26, "RxBadBytes"),
325 	MIB_DESC(1, 0x11, "TxAcmDroppedPkts"),
326 	MIB_DESC(1, 0x0C, "TxGoodPkts"),
327 	MIB_DESC(1, 0x06, "TxUnicastPkts"),
328 	MIB_DESC(1, 0x07, "TxMulticastPkts"),
329 	MIB_DESC(1, 0x00, "Tx64BytePkts"),
330 	MIB_DESC(1, 0x01, "Tx127BytePkts"),
331 	MIB_DESC(1, 0x02, "Tx255BytePkts"),
332 	MIB_DESC(1, 0x03, "Tx511BytePkts"),
333 	MIB_DESC(1, 0x04, "Tx1023BytePkts"),
334 	/** Transmit Size 1024-1522 (or more, if configured) Packet Count. */
335 	MIB_DESC(1, 0x05, "TxMaxBytePkts"),
336 	MIB_DESC(1, 0x08, "TxSingleCollCount"),
337 	MIB_DESC(1, 0x09, "TxMultCollCount"),
338 	MIB_DESC(1, 0x0A, "TxLateCollCount"),
339 	MIB_DESC(1, 0x0B, "TxExcessCollCount"),
340 	MIB_DESC(1, 0x0D, "TxPauseCount"),
341 	MIB_DESC(1, 0x10, "TxDroppedPkts"),
342 	MIB_DESC(2, 0x0E, "TxGoodBytes"),
343 };
344 
345 static u32 gswip_switch_r(struct gswip_priv *priv, u32 offset)
346 {
347 	return __raw_readl(priv->gswip + (offset * 4));
348 }
349 
350 static void gswip_switch_w(struct gswip_priv *priv, u32 val, u32 offset)
351 {
352 	__raw_writel(val, priv->gswip + (offset * 4));
353 }
354 
355 static void gswip_switch_mask(struct gswip_priv *priv, u32 clear, u32 set,
356 			      u32 offset)
357 {
358 	u32 val = gswip_switch_r(priv, offset);
359 
360 	val &= ~(clear);
361 	val |= set;
362 	gswip_switch_w(priv, val, offset);
363 }
364 
365 static u32 gswip_switch_r_timeout(struct gswip_priv *priv, u32 offset,
366 				  u32 cleared)
367 {
368 	u32 val;
369 
370 	return readx_poll_timeout(__raw_readl, priv->gswip + (offset * 4), val,
371 				  (val & cleared) == 0, 20, 50000);
372 }
373 
374 static u32 gswip_mdio_r(struct gswip_priv *priv, u32 offset)
375 {
376 	return __raw_readl(priv->mdio + (offset * 4));
377 }
378 
379 static void gswip_mdio_w(struct gswip_priv *priv, u32 val, u32 offset)
380 {
381 	__raw_writel(val, priv->mdio + (offset * 4));
382 }
383 
384 static void gswip_mdio_mask(struct gswip_priv *priv, u32 clear, u32 set,
385 			    u32 offset)
386 {
387 	u32 val = gswip_mdio_r(priv, offset);
388 
389 	val &= ~(clear);
390 	val |= set;
391 	gswip_mdio_w(priv, val, offset);
392 }
393 
394 static u32 gswip_mii_r(struct gswip_priv *priv, u32 offset)
395 {
396 	return __raw_readl(priv->mii + (offset * 4));
397 }
398 
399 static void gswip_mii_w(struct gswip_priv *priv, u32 val, u32 offset)
400 {
401 	__raw_writel(val, priv->mii + (offset * 4));
402 }
403 
404 static void gswip_mii_mask(struct gswip_priv *priv, u32 clear, u32 set,
405 			   u32 offset)
406 {
407 	u32 val = gswip_mii_r(priv, offset);
408 
409 	val &= ~(clear);
410 	val |= set;
411 	gswip_mii_w(priv, val, offset);
412 }
413 
414 static void gswip_mii_mask_cfg(struct gswip_priv *priv, u32 clear, u32 set,
415 			       int port)
416 {
417 	/* There's no MII_CFG register for the CPU port */
418 	if (!dsa_is_cpu_port(priv->ds, port))
419 		gswip_mii_mask(priv, clear, set, GSWIP_MII_CFGp(port));
420 }
421 
422 static void gswip_mii_mask_pcdu(struct gswip_priv *priv, u32 clear, u32 set,
423 				int port)
424 {
425 	switch (port) {
426 	case 0:
427 		gswip_mii_mask(priv, clear, set, GSWIP_MII_PCDU0);
428 		break;
429 	case 1:
430 		gswip_mii_mask(priv, clear, set, GSWIP_MII_PCDU1);
431 		break;
432 	case 5:
433 		gswip_mii_mask(priv, clear, set, GSWIP_MII_PCDU5);
434 		break;
435 	}
436 }
437 
438 static int gswip_mdio_poll(struct gswip_priv *priv)
439 {
440 	int cnt = 100;
441 
442 	while (likely(cnt--)) {
443 		u32 ctrl = gswip_mdio_r(priv, GSWIP_MDIO_CTRL);
444 
445 		if ((ctrl & GSWIP_MDIO_CTRL_BUSY) == 0)
446 			return 0;
447 		usleep_range(20, 40);
448 	}
449 
450 	return -ETIMEDOUT;
451 }
452 
453 static int gswip_mdio_wr(struct mii_bus *bus, int addr, int reg, u16 val)
454 {
455 	struct gswip_priv *priv = bus->priv;
456 	int err;
457 
458 	err = gswip_mdio_poll(priv);
459 	if (err) {
460 		dev_err(&bus->dev, "waiting for MDIO bus busy timed out\n");
461 		return err;
462 	}
463 
464 	gswip_mdio_w(priv, val, GSWIP_MDIO_WRITE);
465 	gswip_mdio_w(priv, GSWIP_MDIO_CTRL_BUSY | GSWIP_MDIO_CTRL_WR |
466 		((addr & GSWIP_MDIO_CTRL_PHYAD_MASK) << GSWIP_MDIO_CTRL_PHYAD_SHIFT) |
467 		(reg & GSWIP_MDIO_CTRL_REGAD_MASK),
468 		GSWIP_MDIO_CTRL);
469 
470 	return 0;
471 }
472 
473 static int gswip_mdio_rd(struct mii_bus *bus, int addr, int reg)
474 {
475 	struct gswip_priv *priv = bus->priv;
476 	int err;
477 
478 	err = gswip_mdio_poll(priv);
479 	if (err) {
480 		dev_err(&bus->dev, "waiting for MDIO bus busy timed out\n");
481 		return err;
482 	}
483 
484 	gswip_mdio_w(priv, GSWIP_MDIO_CTRL_BUSY | GSWIP_MDIO_CTRL_RD |
485 		((addr & GSWIP_MDIO_CTRL_PHYAD_MASK) << GSWIP_MDIO_CTRL_PHYAD_SHIFT) |
486 		(reg & GSWIP_MDIO_CTRL_REGAD_MASK),
487 		GSWIP_MDIO_CTRL);
488 
489 	err = gswip_mdio_poll(priv);
490 	if (err) {
491 		dev_err(&bus->dev, "waiting for MDIO bus busy timed out\n");
492 		return err;
493 	}
494 
495 	return gswip_mdio_r(priv, GSWIP_MDIO_READ);
496 }
497 
498 static int gswip_mdio(struct gswip_priv *priv, struct device_node *mdio_np)
499 {
500 	struct dsa_switch *ds = priv->ds;
501 	int err;
502 
503 	ds->slave_mii_bus = mdiobus_alloc();
504 	if (!ds->slave_mii_bus)
505 		return -ENOMEM;
506 
507 	ds->slave_mii_bus->priv = priv;
508 	ds->slave_mii_bus->read = gswip_mdio_rd;
509 	ds->slave_mii_bus->write = gswip_mdio_wr;
510 	ds->slave_mii_bus->name = "lantiq,xrx200-mdio";
511 	snprintf(ds->slave_mii_bus->id, MII_BUS_ID_SIZE, "%s-mii",
512 		 dev_name(priv->dev));
513 	ds->slave_mii_bus->parent = priv->dev;
514 	ds->slave_mii_bus->phy_mask = ~ds->phys_mii_mask;
515 
516 	err = of_mdiobus_register(ds->slave_mii_bus, mdio_np);
517 	if (err)
518 		mdiobus_free(ds->slave_mii_bus);
519 
520 	return err;
521 }
522 
523 static int gswip_pce_table_entry_read(struct gswip_priv *priv,
524 				      struct gswip_pce_table_entry *tbl)
525 {
526 	int i;
527 	int err;
528 	u16 crtl;
529 	u16 addr_mode = tbl->key_mode ? GSWIP_PCE_TBL_CTRL_OPMOD_KSRD :
530 					GSWIP_PCE_TBL_CTRL_OPMOD_ADRD;
531 
532 	mutex_lock(&priv->pce_table_lock);
533 
534 	err = gswip_switch_r_timeout(priv, GSWIP_PCE_TBL_CTRL,
535 				     GSWIP_PCE_TBL_CTRL_BAS);
536 	if (err) {
537 		mutex_unlock(&priv->pce_table_lock);
538 		return err;
539 	}
540 
541 	gswip_switch_w(priv, tbl->index, GSWIP_PCE_TBL_ADDR);
542 	gswip_switch_mask(priv, GSWIP_PCE_TBL_CTRL_ADDR_MASK |
543 				GSWIP_PCE_TBL_CTRL_OPMOD_MASK,
544 			  tbl->table | addr_mode | GSWIP_PCE_TBL_CTRL_BAS,
545 			  GSWIP_PCE_TBL_CTRL);
546 
547 	err = gswip_switch_r_timeout(priv, GSWIP_PCE_TBL_CTRL,
548 				     GSWIP_PCE_TBL_CTRL_BAS);
549 	if (err) {
550 		mutex_unlock(&priv->pce_table_lock);
551 		return err;
552 	}
553 
554 	for (i = 0; i < ARRAY_SIZE(tbl->key); i++)
555 		tbl->key[i] = gswip_switch_r(priv, GSWIP_PCE_TBL_KEY(i));
556 
557 	for (i = 0; i < ARRAY_SIZE(tbl->val); i++)
558 		tbl->val[i] = gswip_switch_r(priv, GSWIP_PCE_TBL_VAL(i));
559 
560 	tbl->mask = gswip_switch_r(priv, GSWIP_PCE_TBL_MASK);
561 
562 	crtl = gswip_switch_r(priv, GSWIP_PCE_TBL_CTRL);
563 
564 	tbl->type = !!(crtl & GSWIP_PCE_TBL_CTRL_TYPE);
565 	tbl->valid = !!(crtl & GSWIP_PCE_TBL_CTRL_VLD);
566 	tbl->gmap = (crtl & GSWIP_PCE_TBL_CTRL_GMAP_MASK) >> 7;
567 
568 	mutex_unlock(&priv->pce_table_lock);
569 
570 	return 0;
571 }
572 
573 static int gswip_pce_table_entry_write(struct gswip_priv *priv,
574 				       struct gswip_pce_table_entry *tbl)
575 {
576 	int i;
577 	int err;
578 	u16 crtl;
579 	u16 addr_mode = tbl->key_mode ? GSWIP_PCE_TBL_CTRL_OPMOD_KSWR :
580 					GSWIP_PCE_TBL_CTRL_OPMOD_ADWR;
581 
582 	mutex_lock(&priv->pce_table_lock);
583 
584 	err = gswip_switch_r_timeout(priv, GSWIP_PCE_TBL_CTRL,
585 				     GSWIP_PCE_TBL_CTRL_BAS);
586 	if (err) {
587 		mutex_unlock(&priv->pce_table_lock);
588 		return err;
589 	}
590 
591 	gswip_switch_w(priv, tbl->index, GSWIP_PCE_TBL_ADDR);
592 	gswip_switch_mask(priv, GSWIP_PCE_TBL_CTRL_ADDR_MASK |
593 				GSWIP_PCE_TBL_CTRL_OPMOD_MASK,
594 			  tbl->table | addr_mode,
595 			  GSWIP_PCE_TBL_CTRL);
596 
597 	for (i = 0; i < ARRAY_SIZE(tbl->key); i++)
598 		gswip_switch_w(priv, tbl->key[i], GSWIP_PCE_TBL_KEY(i));
599 
600 	for (i = 0; i < ARRAY_SIZE(tbl->val); i++)
601 		gswip_switch_w(priv, tbl->val[i], GSWIP_PCE_TBL_VAL(i));
602 
603 	gswip_switch_mask(priv, GSWIP_PCE_TBL_CTRL_ADDR_MASK |
604 				GSWIP_PCE_TBL_CTRL_OPMOD_MASK,
605 			  tbl->table | addr_mode,
606 			  GSWIP_PCE_TBL_CTRL);
607 
608 	gswip_switch_w(priv, tbl->mask, GSWIP_PCE_TBL_MASK);
609 
610 	crtl = gswip_switch_r(priv, GSWIP_PCE_TBL_CTRL);
611 	crtl &= ~(GSWIP_PCE_TBL_CTRL_TYPE | GSWIP_PCE_TBL_CTRL_VLD |
612 		  GSWIP_PCE_TBL_CTRL_GMAP_MASK);
613 	if (tbl->type)
614 		crtl |= GSWIP_PCE_TBL_CTRL_TYPE;
615 	if (tbl->valid)
616 		crtl |= GSWIP_PCE_TBL_CTRL_VLD;
617 	crtl |= (tbl->gmap << 7) & GSWIP_PCE_TBL_CTRL_GMAP_MASK;
618 	crtl |= GSWIP_PCE_TBL_CTRL_BAS;
619 	gswip_switch_w(priv, crtl, GSWIP_PCE_TBL_CTRL);
620 
621 	err = gswip_switch_r_timeout(priv, GSWIP_PCE_TBL_CTRL,
622 				     GSWIP_PCE_TBL_CTRL_BAS);
623 
624 	mutex_unlock(&priv->pce_table_lock);
625 
626 	return err;
627 }
628 
629 /* Add the LAN port into a bridge with the CPU port by
630  * default. This prevents automatic forwarding of
631  * packages between the LAN ports when no explicit
632  * bridge is configured.
633  */
634 static int gswip_add_single_port_br(struct gswip_priv *priv, int port, bool add)
635 {
636 	struct gswip_pce_table_entry vlan_active = {0,};
637 	struct gswip_pce_table_entry vlan_mapping = {0,};
638 	unsigned int cpu_port = priv->hw_info->cpu_port;
639 	unsigned int max_ports = priv->hw_info->max_ports;
640 	int err;
641 
642 	if (port >= max_ports) {
643 		dev_err(priv->dev, "single port for %i supported\n", port);
644 		return -EIO;
645 	}
646 
647 	vlan_active.index = port + 1;
648 	vlan_active.table = GSWIP_TABLE_ACTIVE_VLAN;
649 	vlan_active.key[0] = 0; /* vid */
650 	vlan_active.val[0] = port + 1 /* fid */;
651 	vlan_active.valid = add;
652 	err = gswip_pce_table_entry_write(priv, &vlan_active);
653 	if (err) {
654 		dev_err(priv->dev, "failed to write active VLAN: %d\n", err);
655 		return err;
656 	}
657 
658 	if (!add)
659 		return 0;
660 
661 	vlan_mapping.index = port + 1;
662 	vlan_mapping.table = GSWIP_TABLE_VLAN_MAPPING;
663 	vlan_mapping.val[0] = 0 /* vid */;
664 	vlan_mapping.val[1] = BIT(port) | BIT(cpu_port);
665 	vlan_mapping.val[2] = 0;
666 	err = gswip_pce_table_entry_write(priv, &vlan_mapping);
667 	if (err) {
668 		dev_err(priv->dev, "failed to write VLAN mapping: %d\n", err);
669 		return err;
670 	}
671 
672 	return 0;
673 }
674 
675 static int gswip_port_enable(struct dsa_switch *ds, int port,
676 			     struct phy_device *phydev)
677 {
678 	struct gswip_priv *priv = ds->priv;
679 	int err;
680 
681 	if (!dsa_is_user_port(ds, port))
682 		return 0;
683 
684 	if (!dsa_is_cpu_port(ds, port)) {
685 		err = gswip_add_single_port_br(priv, port, true);
686 		if (err)
687 			return err;
688 	}
689 
690 	/* RMON Counter Enable for port */
691 	gswip_switch_w(priv, GSWIP_BM_PCFG_CNTEN, GSWIP_BM_PCFGp(port));
692 
693 	/* enable port fetch/store dma & VLAN Modification */
694 	gswip_switch_mask(priv, 0, GSWIP_FDMA_PCTRL_EN |
695 				   GSWIP_FDMA_PCTRL_VLANMOD_BOTH,
696 			 GSWIP_FDMA_PCTRLp(port));
697 	gswip_switch_mask(priv, 0, GSWIP_SDMA_PCTRL_EN,
698 			  GSWIP_SDMA_PCTRLp(port));
699 
700 	if (!dsa_is_cpu_port(ds, port)) {
701 		u32 mdio_phy = 0;
702 
703 		if (phydev)
704 			mdio_phy = phydev->mdio.addr & GSWIP_MDIO_PHY_ADDR_MASK;
705 
706 		gswip_mdio_mask(priv, GSWIP_MDIO_PHY_ADDR_MASK, mdio_phy,
707 				GSWIP_MDIO_PHYp(port));
708 	}
709 
710 	return 0;
711 }
712 
713 static void gswip_port_disable(struct dsa_switch *ds, int port)
714 {
715 	struct gswip_priv *priv = ds->priv;
716 
717 	if (!dsa_is_user_port(ds, port))
718 		return;
719 
720 	gswip_switch_mask(priv, GSWIP_FDMA_PCTRL_EN, 0,
721 			  GSWIP_FDMA_PCTRLp(port));
722 	gswip_switch_mask(priv, GSWIP_SDMA_PCTRL_EN, 0,
723 			  GSWIP_SDMA_PCTRLp(port));
724 }
725 
726 static int gswip_pce_load_microcode(struct gswip_priv *priv)
727 {
728 	int i;
729 	int err;
730 
731 	gswip_switch_mask(priv, GSWIP_PCE_TBL_CTRL_ADDR_MASK |
732 				GSWIP_PCE_TBL_CTRL_OPMOD_MASK,
733 			  GSWIP_PCE_TBL_CTRL_OPMOD_ADWR, GSWIP_PCE_TBL_CTRL);
734 	gswip_switch_w(priv, 0, GSWIP_PCE_TBL_MASK);
735 
736 	for (i = 0; i < ARRAY_SIZE(gswip_pce_microcode); i++) {
737 		gswip_switch_w(priv, i, GSWIP_PCE_TBL_ADDR);
738 		gswip_switch_w(priv, gswip_pce_microcode[i].val_0,
739 			       GSWIP_PCE_TBL_VAL(0));
740 		gswip_switch_w(priv, gswip_pce_microcode[i].val_1,
741 			       GSWIP_PCE_TBL_VAL(1));
742 		gswip_switch_w(priv, gswip_pce_microcode[i].val_2,
743 			       GSWIP_PCE_TBL_VAL(2));
744 		gswip_switch_w(priv, gswip_pce_microcode[i].val_3,
745 			       GSWIP_PCE_TBL_VAL(3));
746 
747 		/* start the table access: */
748 		gswip_switch_mask(priv, 0, GSWIP_PCE_TBL_CTRL_BAS,
749 				  GSWIP_PCE_TBL_CTRL);
750 		err = gswip_switch_r_timeout(priv, GSWIP_PCE_TBL_CTRL,
751 					     GSWIP_PCE_TBL_CTRL_BAS);
752 		if (err)
753 			return err;
754 	}
755 
756 	/* tell the switch that the microcode is loaded */
757 	gswip_switch_mask(priv, 0, GSWIP_PCE_GCTRL_0_MC_VALID,
758 			  GSWIP_PCE_GCTRL_0);
759 
760 	return 0;
761 }
762 
763 static int gswip_port_vlan_filtering(struct dsa_switch *ds, int port,
764 				     bool vlan_filtering,
765 				     struct netlink_ext_ack *extack)
766 {
767 	struct net_device *bridge = dsa_port_bridge_dev_get(dsa_to_port(ds, port));
768 	struct gswip_priv *priv = ds->priv;
769 
770 	/* Do not allow changing the VLAN filtering options while in bridge */
771 	if (bridge && !!(priv->port_vlan_filter & BIT(port)) != vlan_filtering) {
772 		NL_SET_ERR_MSG_MOD(extack,
773 				   "Dynamic toggling of vlan_filtering not supported");
774 		return -EIO;
775 	}
776 
777 	if (vlan_filtering) {
778 		/* Use port based VLAN tag */
779 		gswip_switch_mask(priv,
780 				  GSWIP_PCE_VCTRL_VSR,
781 				  GSWIP_PCE_VCTRL_UVR | GSWIP_PCE_VCTRL_VIMR |
782 				  GSWIP_PCE_VCTRL_VEMR,
783 				  GSWIP_PCE_VCTRL(port));
784 		gswip_switch_mask(priv, GSWIP_PCE_PCTRL_0_TVM, 0,
785 				  GSWIP_PCE_PCTRL_0p(port));
786 	} else {
787 		/* Use port based VLAN tag */
788 		gswip_switch_mask(priv,
789 				  GSWIP_PCE_VCTRL_UVR | GSWIP_PCE_VCTRL_VIMR |
790 				  GSWIP_PCE_VCTRL_VEMR,
791 				  GSWIP_PCE_VCTRL_VSR,
792 				  GSWIP_PCE_VCTRL(port));
793 		gswip_switch_mask(priv, 0, GSWIP_PCE_PCTRL_0_TVM,
794 				  GSWIP_PCE_PCTRL_0p(port));
795 	}
796 
797 	return 0;
798 }
799 
800 static int gswip_setup(struct dsa_switch *ds)
801 {
802 	struct gswip_priv *priv = ds->priv;
803 	unsigned int cpu_port = priv->hw_info->cpu_port;
804 	int i;
805 	int err;
806 
807 	gswip_switch_w(priv, GSWIP_SWRES_R0, GSWIP_SWRES);
808 	usleep_range(5000, 10000);
809 	gswip_switch_w(priv, 0, GSWIP_SWRES);
810 
811 	/* disable port fetch/store dma on all ports */
812 	for (i = 0; i < priv->hw_info->max_ports; i++) {
813 		gswip_port_disable(ds, i);
814 		gswip_port_vlan_filtering(ds, i, false, NULL);
815 	}
816 
817 	/* enable Switch */
818 	gswip_mdio_mask(priv, 0, GSWIP_MDIO_GLOB_ENABLE, GSWIP_MDIO_GLOB);
819 
820 	err = gswip_pce_load_microcode(priv);
821 	if (err) {
822 		dev_err(priv->dev, "writing PCE microcode failed, %i", err);
823 		return err;
824 	}
825 
826 	/* Default unknown Broadcast/Multicast/Unicast port maps */
827 	gswip_switch_w(priv, BIT(cpu_port), GSWIP_PCE_PMAP1);
828 	gswip_switch_w(priv, BIT(cpu_port), GSWIP_PCE_PMAP2);
829 	gswip_switch_w(priv, BIT(cpu_port), GSWIP_PCE_PMAP3);
830 
831 	/* Deactivate MDIO PHY auto polling. Some PHYs as the AR8030 have an
832 	 * interoperability problem with this auto polling mechanism because
833 	 * their status registers think that the link is in a different state
834 	 * than it actually is. For the AR8030 it has the BMSR_ESTATEN bit set
835 	 * as well as ESTATUS_1000_TFULL and ESTATUS_1000_XFULL. This makes the
836 	 * auto polling state machine consider the link being negotiated with
837 	 * 1Gbit/s. Since the PHY itself is a Fast Ethernet RMII PHY this leads
838 	 * to the switch port being completely dead (RX and TX are both not
839 	 * working).
840 	 * Also with various other PHY / port combinations (PHY11G GPHY, PHY22F
841 	 * GPHY, external RGMII PEF7071/7072) any traffic would stop. Sometimes
842 	 * it would work fine for a few minutes to hours and then stop, on
843 	 * other device it would no traffic could be sent or received at all.
844 	 * Testing shows that when PHY auto polling is disabled these problems
845 	 * go away.
846 	 */
847 	gswip_mdio_w(priv, 0x0, GSWIP_MDIO_MDC_CFG0);
848 
849 	/* Configure the MDIO Clock 2.5 MHz */
850 	gswip_mdio_mask(priv, 0xff, 0x09, GSWIP_MDIO_MDC_CFG1);
851 
852 	/* Disable the xMII interface and clear it's isolation bit */
853 	for (i = 0; i < priv->hw_info->max_ports; i++)
854 		gswip_mii_mask_cfg(priv,
855 				   GSWIP_MII_CFG_EN | GSWIP_MII_CFG_ISOLATE,
856 				   0, i);
857 
858 	/* enable special tag insertion on cpu port */
859 	gswip_switch_mask(priv, 0, GSWIP_FDMA_PCTRL_STEN,
860 			  GSWIP_FDMA_PCTRLp(cpu_port));
861 
862 	/* accept special tag in ingress direction */
863 	gswip_switch_mask(priv, 0, GSWIP_PCE_PCTRL_0_INGRESS,
864 			  GSWIP_PCE_PCTRL_0p(cpu_port));
865 
866 	gswip_switch_mask(priv, 0, GSWIP_MAC_CTRL_2_MLEN,
867 			  GSWIP_MAC_CTRL_2p(cpu_port));
868 	gswip_switch_w(priv, VLAN_ETH_FRAME_LEN + 8 + ETH_FCS_LEN,
869 		       GSWIP_MAC_FLEN);
870 	gswip_switch_mask(priv, 0, GSWIP_BM_QUEUE_GCTRL_GL_MOD,
871 			  GSWIP_BM_QUEUE_GCTRL);
872 
873 	/* VLAN aware Switching */
874 	gswip_switch_mask(priv, 0, GSWIP_PCE_GCTRL_0_VLAN, GSWIP_PCE_GCTRL_0);
875 
876 	/* Flush MAC Table */
877 	gswip_switch_mask(priv, 0, GSWIP_PCE_GCTRL_0_MTFL, GSWIP_PCE_GCTRL_0);
878 
879 	err = gswip_switch_r_timeout(priv, GSWIP_PCE_GCTRL_0,
880 				     GSWIP_PCE_GCTRL_0_MTFL);
881 	if (err) {
882 		dev_err(priv->dev, "MAC flushing didn't finish\n");
883 		return err;
884 	}
885 
886 	gswip_port_enable(ds, cpu_port, NULL);
887 
888 	ds->configure_vlan_while_not_filtering = false;
889 
890 	return 0;
891 }
892 
893 static enum dsa_tag_protocol gswip_get_tag_protocol(struct dsa_switch *ds,
894 						    int port,
895 						    enum dsa_tag_protocol mp)
896 {
897 	return DSA_TAG_PROTO_GSWIP;
898 }
899 
900 static int gswip_vlan_active_create(struct gswip_priv *priv,
901 				    struct net_device *bridge,
902 				    int fid, u16 vid)
903 {
904 	struct gswip_pce_table_entry vlan_active = {0,};
905 	unsigned int max_ports = priv->hw_info->max_ports;
906 	int idx = -1;
907 	int err;
908 	int i;
909 
910 	/* Look for a free slot */
911 	for (i = max_ports; i < ARRAY_SIZE(priv->vlans); i++) {
912 		if (!priv->vlans[i].bridge) {
913 			idx = i;
914 			break;
915 		}
916 	}
917 
918 	if (idx == -1)
919 		return -ENOSPC;
920 
921 	if (fid == -1)
922 		fid = idx;
923 
924 	vlan_active.index = idx;
925 	vlan_active.table = GSWIP_TABLE_ACTIVE_VLAN;
926 	vlan_active.key[0] = vid;
927 	vlan_active.val[0] = fid;
928 	vlan_active.valid = true;
929 
930 	err = gswip_pce_table_entry_write(priv, &vlan_active);
931 	if (err) {
932 		dev_err(priv->dev, "failed to write active VLAN: %d\n",	err);
933 		return err;
934 	}
935 
936 	priv->vlans[idx].bridge = bridge;
937 	priv->vlans[idx].vid = vid;
938 	priv->vlans[idx].fid = fid;
939 
940 	return idx;
941 }
942 
943 static int gswip_vlan_active_remove(struct gswip_priv *priv, int idx)
944 {
945 	struct gswip_pce_table_entry vlan_active = {0,};
946 	int err;
947 
948 	vlan_active.index = idx;
949 	vlan_active.table = GSWIP_TABLE_ACTIVE_VLAN;
950 	vlan_active.valid = false;
951 	err = gswip_pce_table_entry_write(priv, &vlan_active);
952 	if (err)
953 		dev_err(priv->dev, "failed to delete active VLAN: %d\n", err);
954 	priv->vlans[idx].bridge = NULL;
955 
956 	return err;
957 }
958 
959 static int gswip_vlan_add_unaware(struct gswip_priv *priv,
960 				  struct net_device *bridge, int port)
961 {
962 	struct gswip_pce_table_entry vlan_mapping = {0,};
963 	unsigned int max_ports = priv->hw_info->max_ports;
964 	unsigned int cpu_port = priv->hw_info->cpu_port;
965 	bool active_vlan_created = false;
966 	int idx = -1;
967 	int i;
968 	int err;
969 
970 	/* Check if there is already a page for this bridge */
971 	for (i = max_ports; i < ARRAY_SIZE(priv->vlans); i++) {
972 		if (priv->vlans[i].bridge == bridge) {
973 			idx = i;
974 			break;
975 		}
976 	}
977 
978 	/* If this bridge is not programmed yet, add a Active VLAN table
979 	 * entry in a free slot and prepare the VLAN mapping table entry.
980 	 */
981 	if (idx == -1) {
982 		idx = gswip_vlan_active_create(priv, bridge, -1, 0);
983 		if (idx < 0)
984 			return idx;
985 		active_vlan_created = true;
986 
987 		vlan_mapping.index = idx;
988 		vlan_mapping.table = GSWIP_TABLE_VLAN_MAPPING;
989 		/* VLAN ID byte, maps to the VLAN ID of vlan active table */
990 		vlan_mapping.val[0] = 0;
991 	} else {
992 		/* Read the existing VLAN mapping entry from the switch */
993 		vlan_mapping.index = idx;
994 		vlan_mapping.table = GSWIP_TABLE_VLAN_MAPPING;
995 		err = gswip_pce_table_entry_read(priv, &vlan_mapping);
996 		if (err) {
997 			dev_err(priv->dev, "failed to read VLAN mapping: %d\n",
998 				err);
999 			return err;
1000 		}
1001 	}
1002 
1003 	/* Update the VLAN mapping entry and write it to the switch */
1004 	vlan_mapping.val[1] |= BIT(cpu_port);
1005 	vlan_mapping.val[1] |= BIT(port);
1006 	err = gswip_pce_table_entry_write(priv, &vlan_mapping);
1007 	if (err) {
1008 		dev_err(priv->dev, "failed to write VLAN mapping: %d\n", err);
1009 		/* In case an Active VLAN was creaetd delete it again */
1010 		if (active_vlan_created)
1011 			gswip_vlan_active_remove(priv, idx);
1012 		return err;
1013 	}
1014 
1015 	gswip_switch_w(priv, 0, GSWIP_PCE_DEFPVID(port));
1016 	return 0;
1017 }
1018 
1019 static int gswip_vlan_add_aware(struct gswip_priv *priv,
1020 				struct net_device *bridge, int port,
1021 				u16 vid, bool untagged,
1022 				bool pvid)
1023 {
1024 	struct gswip_pce_table_entry vlan_mapping = {0,};
1025 	unsigned int max_ports = priv->hw_info->max_ports;
1026 	unsigned int cpu_port = priv->hw_info->cpu_port;
1027 	bool active_vlan_created = false;
1028 	int idx = -1;
1029 	int fid = -1;
1030 	int i;
1031 	int err;
1032 
1033 	/* Check if there is already a page for this bridge */
1034 	for (i = max_ports; i < ARRAY_SIZE(priv->vlans); i++) {
1035 		if (priv->vlans[i].bridge == bridge) {
1036 			if (fid != -1 && fid != priv->vlans[i].fid)
1037 				dev_err(priv->dev, "one bridge with multiple flow ids\n");
1038 			fid = priv->vlans[i].fid;
1039 			if (priv->vlans[i].vid == vid) {
1040 				idx = i;
1041 				break;
1042 			}
1043 		}
1044 	}
1045 
1046 	/* If this bridge is not programmed yet, add a Active VLAN table
1047 	 * entry in a free slot and prepare the VLAN mapping table entry.
1048 	 */
1049 	if (idx == -1) {
1050 		idx = gswip_vlan_active_create(priv, bridge, fid, vid);
1051 		if (idx < 0)
1052 			return idx;
1053 		active_vlan_created = true;
1054 
1055 		vlan_mapping.index = idx;
1056 		vlan_mapping.table = GSWIP_TABLE_VLAN_MAPPING;
1057 		/* VLAN ID byte, maps to the VLAN ID of vlan active table */
1058 		vlan_mapping.val[0] = vid;
1059 	} else {
1060 		/* Read the existing VLAN mapping entry from the switch */
1061 		vlan_mapping.index = idx;
1062 		vlan_mapping.table = GSWIP_TABLE_VLAN_MAPPING;
1063 		err = gswip_pce_table_entry_read(priv, &vlan_mapping);
1064 		if (err) {
1065 			dev_err(priv->dev, "failed to read VLAN mapping: %d\n",
1066 				err);
1067 			return err;
1068 		}
1069 	}
1070 
1071 	vlan_mapping.val[0] = vid;
1072 	/* Update the VLAN mapping entry and write it to the switch */
1073 	vlan_mapping.val[1] |= BIT(cpu_port);
1074 	vlan_mapping.val[2] |= BIT(cpu_port);
1075 	vlan_mapping.val[1] |= BIT(port);
1076 	if (untagged)
1077 		vlan_mapping.val[2] &= ~BIT(port);
1078 	else
1079 		vlan_mapping.val[2] |= BIT(port);
1080 	err = gswip_pce_table_entry_write(priv, &vlan_mapping);
1081 	if (err) {
1082 		dev_err(priv->dev, "failed to write VLAN mapping: %d\n", err);
1083 		/* In case an Active VLAN was creaetd delete it again */
1084 		if (active_vlan_created)
1085 			gswip_vlan_active_remove(priv, idx);
1086 		return err;
1087 	}
1088 
1089 	if (pvid)
1090 		gswip_switch_w(priv, idx, GSWIP_PCE_DEFPVID(port));
1091 
1092 	return 0;
1093 }
1094 
1095 static int gswip_vlan_remove(struct gswip_priv *priv,
1096 			     struct net_device *bridge, int port,
1097 			     u16 vid, bool pvid, bool vlan_aware)
1098 {
1099 	struct gswip_pce_table_entry vlan_mapping = {0,};
1100 	unsigned int max_ports = priv->hw_info->max_ports;
1101 	unsigned int cpu_port = priv->hw_info->cpu_port;
1102 	int idx = -1;
1103 	int i;
1104 	int err;
1105 
1106 	/* Check if there is already a page for this bridge */
1107 	for (i = max_ports; i < ARRAY_SIZE(priv->vlans); i++) {
1108 		if (priv->vlans[i].bridge == bridge &&
1109 		    (!vlan_aware || priv->vlans[i].vid == vid)) {
1110 			idx = i;
1111 			break;
1112 		}
1113 	}
1114 
1115 	if (idx == -1) {
1116 		dev_err(priv->dev, "bridge to leave does not exists\n");
1117 		return -ENOENT;
1118 	}
1119 
1120 	vlan_mapping.index = idx;
1121 	vlan_mapping.table = GSWIP_TABLE_VLAN_MAPPING;
1122 	err = gswip_pce_table_entry_read(priv, &vlan_mapping);
1123 	if (err) {
1124 		dev_err(priv->dev, "failed to read VLAN mapping: %d\n",	err);
1125 		return err;
1126 	}
1127 
1128 	vlan_mapping.val[1] &= ~BIT(port);
1129 	vlan_mapping.val[2] &= ~BIT(port);
1130 	err = gswip_pce_table_entry_write(priv, &vlan_mapping);
1131 	if (err) {
1132 		dev_err(priv->dev, "failed to write VLAN mapping: %d\n", err);
1133 		return err;
1134 	}
1135 
1136 	/* In case all ports are removed from the bridge, remove the VLAN */
1137 	if ((vlan_mapping.val[1] & ~BIT(cpu_port)) == 0) {
1138 		err = gswip_vlan_active_remove(priv, idx);
1139 		if (err) {
1140 			dev_err(priv->dev, "failed to write active VLAN: %d\n",
1141 				err);
1142 			return err;
1143 		}
1144 	}
1145 
1146 	/* GSWIP 2.2 (GRX300) and later program here the VID directly. */
1147 	if (pvid)
1148 		gswip_switch_w(priv, 0, GSWIP_PCE_DEFPVID(port));
1149 
1150 	return 0;
1151 }
1152 
1153 static int gswip_port_bridge_join(struct dsa_switch *ds, int port,
1154 				  struct dsa_bridge bridge,
1155 				  bool *tx_fwd_offload,
1156 				  struct netlink_ext_ack *extack)
1157 {
1158 	struct net_device *br = bridge.dev;
1159 	struct gswip_priv *priv = ds->priv;
1160 	int err;
1161 
1162 	/* When the bridge uses VLAN filtering we have to configure VLAN
1163 	 * specific bridges. No bridge is configured here.
1164 	 */
1165 	if (!br_vlan_enabled(br)) {
1166 		err = gswip_vlan_add_unaware(priv, br, port);
1167 		if (err)
1168 			return err;
1169 		priv->port_vlan_filter &= ~BIT(port);
1170 	} else {
1171 		priv->port_vlan_filter |= BIT(port);
1172 	}
1173 	return gswip_add_single_port_br(priv, port, false);
1174 }
1175 
1176 static void gswip_port_bridge_leave(struct dsa_switch *ds, int port,
1177 				    struct dsa_bridge bridge)
1178 {
1179 	struct net_device *br = bridge.dev;
1180 	struct gswip_priv *priv = ds->priv;
1181 
1182 	gswip_add_single_port_br(priv, port, true);
1183 
1184 	/* When the bridge uses VLAN filtering we have to configure VLAN
1185 	 * specific bridges. No bridge is configured here.
1186 	 */
1187 	if (!br_vlan_enabled(br))
1188 		gswip_vlan_remove(priv, br, port, 0, true, false);
1189 }
1190 
1191 static int gswip_port_vlan_prepare(struct dsa_switch *ds, int port,
1192 				   const struct switchdev_obj_port_vlan *vlan,
1193 				   struct netlink_ext_ack *extack)
1194 {
1195 	struct net_device *bridge = dsa_port_bridge_dev_get(dsa_to_port(ds, port));
1196 	struct gswip_priv *priv = ds->priv;
1197 	unsigned int max_ports = priv->hw_info->max_ports;
1198 	int pos = max_ports;
1199 	int i, idx = -1;
1200 
1201 	/* We only support VLAN filtering on bridges */
1202 	if (!dsa_is_cpu_port(ds, port) && !bridge)
1203 		return -EOPNOTSUPP;
1204 
1205 	/* Check if there is already a page for this VLAN */
1206 	for (i = max_ports; i < ARRAY_SIZE(priv->vlans); i++) {
1207 		if (priv->vlans[i].bridge == bridge &&
1208 		    priv->vlans[i].vid == vlan->vid) {
1209 			idx = i;
1210 			break;
1211 		}
1212 	}
1213 
1214 	/* If this VLAN is not programmed yet, we have to reserve
1215 	 * one entry in the VLAN table. Make sure we start at the
1216 	 * next position round.
1217 	 */
1218 	if (idx == -1) {
1219 		/* Look for a free slot */
1220 		for (; pos < ARRAY_SIZE(priv->vlans); pos++) {
1221 			if (!priv->vlans[pos].bridge) {
1222 				idx = pos;
1223 				pos++;
1224 				break;
1225 			}
1226 		}
1227 
1228 		if (idx == -1) {
1229 			NL_SET_ERR_MSG_MOD(extack, "No slot in VLAN table");
1230 			return -ENOSPC;
1231 		}
1232 	}
1233 
1234 	return 0;
1235 }
1236 
1237 static int gswip_port_vlan_add(struct dsa_switch *ds, int port,
1238 			       const struct switchdev_obj_port_vlan *vlan,
1239 			       struct netlink_ext_ack *extack)
1240 {
1241 	struct net_device *bridge = dsa_port_bridge_dev_get(dsa_to_port(ds, port));
1242 	struct gswip_priv *priv = ds->priv;
1243 	bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
1244 	bool pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
1245 	int err;
1246 
1247 	err = gswip_port_vlan_prepare(ds, port, vlan, extack);
1248 	if (err)
1249 		return err;
1250 
1251 	/* We have to receive all packets on the CPU port and should not
1252 	 * do any VLAN filtering here. This is also called with bridge
1253 	 * NULL and then we do not know for which bridge to configure
1254 	 * this.
1255 	 */
1256 	if (dsa_is_cpu_port(ds, port))
1257 		return 0;
1258 
1259 	return gswip_vlan_add_aware(priv, bridge, port, vlan->vid,
1260 				    untagged, pvid);
1261 }
1262 
1263 static int gswip_port_vlan_del(struct dsa_switch *ds, int port,
1264 			       const struct switchdev_obj_port_vlan *vlan)
1265 {
1266 	struct net_device *bridge = dsa_port_bridge_dev_get(dsa_to_port(ds, port));
1267 	struct gswip_priv *priv = ds->priv;
1268 	bool pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
1269 
1270 	/* We have to receive all packets on the CPU port and should not
1271 	 * do any VLAN filtering here. This is also called with bridge
1272 	 * NULL and then we do not know for which bridge to configure
1273 	 * this.
1274 	 */
1275 	if (dsa_is_cpu_port(ds, port))
1276 		return 0;
1277 
1278 	return gswip_vlan_remove(priv, bridge, port, vlan->vid, pvid, true);
1279 }
1280 
1281 static void gswip_port_fast_age(struct dsa_switch *ds, int port)
1282 {
1283 	struct gswip_priv *priv = ds->priv;
1284 	struct gswip_pce_table_entry mac_bridge = {0,};
1285 	int i;
1286 	int err;
1287 
1288 	for (i = 0; i < 2048; i++) {
1289 		mac_bridge.table = GSWIP_TABLE_MAC_BRIDGE;
1290 		mac_bridge.index = i;
1291 
1292 		err = gswip_pce_table_entry_read(priv, &mac_bridge);
1293 		if (err) {
1294 			dev_err(priv->dev, "failed to read mac bridge: %d\n",
1295 				err);
1296 			return;
1297 		}
1298 
1299 		if (!mac_bridge.valid)
1300 			continue;
1301 
1302 		if (mac_bridge.val[1] & GSWIP_TABLE_MAC_BRIDGE_STATIC)
1303 			continue;
1304 
1305 		if (((mac_bridge.val[0] & GENMASK(7, 4)) >> 4) != port)
1306 			continue;
1307 
1308 		mac_bridge.valid = false;
1309 		err = gswip_pce_table_entry_write(priv, &mac_bridge);
1310 		if (err) {
1311 			dev_err(priv->dev, "failed to write mac bridge: %d\n",
1312 				err);
1313 			return;
1314 		}
1315 	}
1316 }
1317 
1318 static void gswip_port_stp_state_set(struct dsa_switch *ds, int port, u8 state)
1319 {
1320 	struct gswip_priv *priv = ds->priv;
1321 	u32 stp_state;
1322 
1323 	switch (state) {
1324 	case BR_STATE_DISABLED:
1325 		gswip_switch_mask(priv, GSWIP_SDMA_PCTRL_EN, 0,
1326 				  GSWIP_SDMA_PCTRLp(port));
1327 		return;
1328 	case BR_STATE_BLOCKING:
1329 	case BR_STATE_LISTENING:
1330 		stp_state = GSWIP_PCE_PCTRL_0_PSTATE_LISTEN;
1331 		break;
1332 	case BR_STATE_LEARNING:
1333 		stp_state = GSWIP_PCE_PCTRL_0_PSTATE_LEARNING;
1334 		break;
1335 	case BR_STATE_FORWARDING:
1336 		stp_state = GSWIP_PCE_PCTRL_0_PSTATE_FORWARDING;
1337 		break;
1338 	default:
1339 		dev_err(priv->dev, "invalid STP state: %d\n", state);
1340 		return;
1341 	}
1342 
1343 	gswip_switch_mask(priv, 0, GSWIP_SDMA_PCTRL_EN,
1344 			  GSWIP_SDMA_PCTRLp(port));
1345 	gswip_switch_mask(priv, GSWIP_PCE_PCTRL_0_PSTATE_MASK, stp_state,
1346 			  GSWIP_PCE_PCTRL_0p(port));
1347 }
1348 
1349 static int gswip_port_fdb(struct dsa_switch *ds, int port,
1350 			  const unsigned char *addr, u16 vid, bool add)
1351 {
1352 	struct net_device *bridge = dsa_port_bridge_dev_get(dsa_to_port(ds, port));
1353 	struct gswip_priv *priv = ds->priv;
1354 	struct gswip_pce_table_entry mac_bridge = {0,};
1355 	unsigned int cpu_port = priv->hw_info->cpu_port;
1356 	int fid = -1;
1357 	int i;
1358 	int err;
1359 
1360 	if (!bridge)
1361 		return -EINVAL;
1362 
1363 	for (i = cpu_port; i < ARRAY_SIZE(priv->vlans); i++) {
1364 		if (priv->vlans[i].bridge == bridge) {
1365 			fid = priv->vlans[i].fid;
1366 			break;
1367 		}
1368 	}
1369 
1370 	if (fid == -1) {
1371 		dev_err(priv->dev, "Port not part of a bridge\n");
1372 		return -EINVAL;
1373 	}
1374 
1375 	mac_bridge.table = GSWIP_TABLE_MAC_BRIDGE;
1376 	mac_bridge.key_mode = true;
1377 	mac_bridge.key[0] = addr[5] | (addr[4] << 8);
1378 	mac_bridge.key[1] = addr[3] | (addr[2] << 8);
1379 	mac_bridge.key[2] = addr[1] | (addr[0] << 8);
1380 	mac_bridge.key[3] = fid;
1381 	mac_bridge.val[0] = add ? BIT(port) : 0; /* port map */
1382 	mac_bridge.val[1] = GSWIP_TABLE_MAC_BRIDGE_STATIC;
1383 	mac_bridge.valid = add;
1384 
1385 	err = gswip_pce_table_entry_write(priv, &mac_bridge);
1386 	if (err)
1387 		dev_err(priv->dev, "failed to write mac bridge: %d\n", err);
1388 
1389 	return err;
1390 }
1391 
1392 static int gswip_port_fdb_add(struct dsa_switch *ds, int port,
1393 			      const unsigned char *addr, u16 vid,
1394 			      struct dsa_db db)
1395 {
1396 	return gswip_port_fdb(ds, port, addr, vid, true);
1397 }
1398 
1399 static int gswip_port_fdb_del(struct dsa_switch *ds, int port,
1400 			      const unsigned char *addr, u16 vid,
1401 			      struct dsa_db db)
1402 {
1403 	return gswip_port_fdb(ds, port, addr, vid, false);
1404 }
1405 
1406 static int gswip_port_fdb_dump(struct dsa_switch *ds, int port,
1407 			       dsa_fdb_dump_cb_t *cb, void *data)
1408 {
1409 	struct gswip_priv *priv = ds->priv;
1410 	struct gswip_pce_table_entry mac_bridge = {0,};
1411 	unsigned char addr[6];
1412 	int i;
1413 	int err;
1414 
1415 	for (i = 0; i < 2048; i++) {
1416 		mac_bridge.table = GSWIP_TABLE_MAC_BRIDGE;
1417 		mac_bridge.index = i;
1418 
1419 		err = gswip_pce_table_entry_read(priv, &mac_bridge);
1420 		if (err) {
1421 			dev_err(priv->dev, "failed to write mac bridge: %d\n",
1422 				err);
1423 			return err;
1424 		}
1425 
1426 		if (!mac_bridge.valid)
1427 			continue;
1428 
1429 		addr[5] = mac_bridge.key[0] & 0xff;
1430 		addr[4] = (mac_bridge.key[0] >> 8) & 0xff;
1431 		addr[3] = mac_bridge.key[1] & 0xff;
1432 		addr[2] = (mac_bridge.key[1] >> 8) & 0xff;
1433 		addr[1] = mac_bridge.key[2] & 0xff;
1434 		addr[0] = (mac_bridge.key[2] >> 8) & 0xff;
1435 		if (mac_bridge.val[1] & GSWIP_TABLE_MAC_BRIDGE_STATIC) {
1436 			if (mac_bridge.val[0] & BIT(port)) {
1437 				err = cb(addr, 0, true, data);
1438 				if (err)
1439 					return err;
1440 			}
1441 		} else {
1442 			if (((mac_bridge.val[0] & GENMASK(7, 4)) >> 4) == port) {
1443 				err = cb(addr, 0, false, data);
1444 				if (err)
1445 					return err;
1446 			}
1447 		}
1448 	}
1449 	return 0;
1450 }
1451 
1452 static void gswip_xrx200_phylink_get_caps(struct dsa_switch *ds, int port,
1453 					  struct phylink_config *config)
1454 {
1455 	switch (port) {
1456 	case 0:
1457 	case 1:
1458 		phy_interface_set_rgmii(config->supported_interfaces);
1459 		__set_bit(PHY_INTERFACE_MODE_MII,
1460 			  config->supported_interfaces);
1461 		__set_bit(PHY_INTERFACE_MODE_REVMII,
1462 			  config->supported_interfaces);
1463 		__set_bit(PHY_INTERFACE_MODE_RMII,
1464 			  config->supported_interfaces);
1465 		break;
1466 
1467 	case 2:
1468 	case 3:
1469 	case 4:
1470 		__set_bit(PHY_INTERFACE_MODE_INTERNAL,
1471 			  config->supported_interfaces);
1472 		break;
1473 
1474 	case 5:
1475 		phy_interface_set_rgmii(config->supported_interfaces);
1476 		__set_bit(PHY_INTERFACE_MODE_INTERNAL,
1477 			  config->supported_interfaces);
1478 		break;
1479 	}
1480 
1481 	config->mac_capabilities = MAC_ASYM_PAUSE | MAC_SYM_PAUSE |
1482 		MAC_10 | MAC_100 | MAC_1000;
1483 }
1484 
1485 static void gswip_xrx300_phylink_get_caps(struct dsa_switch *ds, int port,
1486 					  struct phylink_config *config)
1487 {
1488 	switch (port) {
1489 	case 0:
1490 		phy_interface_set_rgmii(config->supported_interfaces);
1491 		__set_bit(PHY_INTERFACE_MODE_GMII,
1492 			  config->supported_interfaces);
1493 		__set_bit(PHY_INTERFACE_MODE_RMII,
1494 			  config->supported_interfaces);
1495 		break;
1496 
1497 	case 1:
1498 	case 2:
1499 	case 3:
1500 	case 4:
1501 		__set_bit(PHY_INTERFACE_MODE_INTERNAL,
1502 			  config->supported_interfaces);
1503 		break;
1504 
1505 	case 5:
1506 		phy_interface_set_rgmii(config->supported_interfaces);
1507 		__set_bit(PHY_INTERFACE_MODE_INTERNAL,
1508 			  config->supported_interfaces);
1509 		__set_bit(PHY_INTERFACE_MODE_RMII,
1510 			  config->supported_interfaces);
1511 		break;
1512 	}
1513 
1514 	config->mac_capabilities = MAC_ASYM_PAUSE | MAC_SYM_PAUSE |
1515 		MAC_10 | MAC_100 | MAC_1000;
1516 }
1517 
1518 static void gswip_port_set_link(struct gswip_priv *priv, int port, bool link)
1519 {
1520 	u32 mdio_phy;
1521 
1522 	if (link)
1523 		mdio_phy = GSWIP_MDIO_PHY_LINK_UP;
1524 	else
1525 		mdio_phy = GSWIP_MDIO_PHY_LINK_DOWN;
1526 
1527 	gswip_mdio_mask(priv, GSWIP_MDIO_PHY_LINK_MASK, mdio_phy,
1528 			GSWIP_MDIO_PHYp(port));
1529 }
1530 
1531 static void gswip_port_set_speed(struct gswip_priv *priv, int port, int speed,
1532 				 phy_interface_t interface)
1533 {
1534 	u32 mdio_phy = 0, mii_cfg = 0, mac_ctrl_0 = 0;
1535 
1536 	switch (speed) {
1537 	case SPEED_10:
1538 		mdio_phy = GSWIP_MDIO_PHY_SPEED_M10;
1539 
1540 		if (interface == PHY_INTERFACE_MODE_RMII)
1541 			mii_cfg = GSWIP_MII_CFG_RATE_M50;
1542 		else
1543 			mii_cfg = GSWIP_MII_CFG_RATE_M2P5;
1544 
1545 		mac_ctrl_0 = GSWIP_MAC_CTRL_0_GMII_MII;
1546 		break;
1547 
1548 	case SPEED_100:
1549 		mdio_phy = GSWIP_MDIO_PHY_SPEED_M100;
1550 
1551 		if (interface == PHY_INTERFACE_MODE_RMII)
1552 			mii_cfg = GSWIP_MII_CFG_RATE_M50;
1553 		else
1554 			mii_cfg = GSWIP_MII_CFG_RATE_M25;
1555 
1556 		mac_ctrl_0 = GSWIP_MAC_CTRL_0_GMII_MII;
1557 		break;
1558 
1559 	case SPEED_1000:
1560 		mdio_phy = GSWIP_MDIO_PHY_SPEED_G1;
1561 
1562 		mii_cfg = GSWIP_MII_CFG_RATE_M125;
1563 
1564 		mac_ctrl_0 = GSWIP_MAC_CTRL_0_GMII_RGMII;
1565 		break;
1566 	}
1567 
1568 	gswip_mdio_mask(priv, GSWIP_MDIO_PHY_SPEED_MASK, mdio_phy,
1569 			GSWIP_MDIO_PHYp(port));
1570 	gswip_mii_mask_cfg(priv, GSWIP_MII_CFG_RATE_MASK, mii_cfg, port);
1571 	gswip_switch_mask(priv, GSWIP_MAC_CTRL_0_GMII_MASK, mac_ctrl_0,
1572 			  GSWIP_MAC_CTRL_0p(port));
1573 }
1574 
1575 static void gswip_port_set_duplex(struct gswip_priv *priv, int port, int duplex)
1576 {
1577 	u32 mac_ctrl_0, mdio_phy;
1578 
1579 	if (duplex == DUPLEX_FULL) {
1580 		mac_ctrl_0 = GSWIP_MAC_CTRL_0_FDUP_EN;
1581 		mdio_phy = GSWIP_MDIO_PHY_FDUP_EN;
1582 	} else {
1583 		mac_ctrl_0 = GSWIP_MAC_CTRL_0_FDUP_DIS;
1584 		mdio_phy = GSWIP_MDIO_PHY_FDUP_DIS;
1585 	}
1586 
1587 	gswip_switch_mask(priv, GSWIP_MAC_CTRL_0_FDUP_MASK, mac_ctrl_0,
1588 			  GSWIP_MAC_CTRL_0p(port));
1589 	gswip_mdio_mask(priv, GSWIP_MDIO_PHY_FDUP_MASK, mdio_phy,
1590 			GSWIP_MDIO_PHYp(port));
1591 }
1592 
1593 static void gswip_port_set_pause(struct gswip_priv *priv, int port,
1594 				 bool tx_pause, bool rx_pause)
1595 {
1596 	u32 mac_ctrl_0, mdio_phy;
1597 
1598 	if (tx_pause && rx_pause) {
1599 		mac_ctrl_0 = GSWIP_MAC_CTRL_0_FCON_RXTX;
1600 		mdio_phy = GSWIP_MDIO_PHY_FCONTX_EN |
1601 			   GSWIP_MDIO_PHY_FCONRX_EN;
1602 	} else if (tx_pause) {
1603 		mac_ctrl_0 = GSWIP_MAC_CTRL_0_FCON_TX;
1604 		mdio_phy = GSWIP_MDIO_PHY_FCONTX_EN |
1605 			   GSWIP_MDIO_PHY_FCONRX_DIS;
1606 	} else if (rx_pause) {
1607 		mac_ctrl_0 = GSWIP_MAC_CTRL_0_FCON_RX;
1608 		mdio_phy = GSWIP_MDIO_PHY_FCONTX_DIS |
1609 			   GSWIP_MDIO_PHY_FCONRX_EN;
1610 	} else {
1611 		mac_ctrl_0 = GSWIP_MAC_CTRL_0_FCON_NONE;
1612 		mdio_phy = GSWIP_MDIO_PHY_FCONTX_DIS |
1613 			   GSWIP_MDIO_PHY_FCONRX_DIS;
1614 	}
1615 
1616 	gswip_switch_mask(priv, GSWIP_MAC_CTRL_0_FCON_MASK,
1617 			  mac_ctrl_0, GSWIP_MAC_CTRL_0p(port));
1618 	gswip_mdio_mask(priv,
1619 			GSWIP_MDIO_PHY_FCONTX_MASK |
1620 			GSWIP_MDIO_PHY_FCONRX_MASK,
1621 			mdio_phy, GSWIP_MDIO_PHYp(port));
1622 }
1623 
1624 static void gswip_phylink_mac_config(struct dsa_switch *ds, int port,
1625 				     unsigned int mode,
1626 				     const struct phylink_link_state *state)
1627 {
1628 	struct gswip_priv *priv = ds->priv;
1629 	u32 miicfg = 0;
1630 
1631 	miicfg |= GSWIP_MII_CFG_LDCLKDIS;
1632 
1633 	switch (state->interface) {
1634 	case PHY_INTERFACE_MODE_MII:
1635 	case PHY_INTERFACE_MODE_INTERNAL:
1636 		miicfg |= GSWIP_MII_CFG_MODE_MIIM;
1637 		break;
1638 	case PHY_INTERFACE_MODE_REVMII:
1639 		miicfg |= GSWIP_MII_CFG_MODE_MIIP;
1640 		break;
1641 	case PHY_INTERFACE_MODE_RMII:
1642 		miicfg |= GSWIP_MII_CFG_MODE_RMIIM;
1643 
1644 		/* Configure the RMII clock as output: */
1645 		miicfg |= GSWIP_MII_CFG_RMII_CLK;
1646 		break;
1647 	case PHY_INTERFACE_MODE_RGMII:
1648 	case PHY_INTERFACE_MODE_RGMII_ID:
1649 	case PHY_INTERFACE_MODE_RGMII_RXID:
1650 	case PHY_INTERFACE_MODE_RGMII_TXID:
1651 		miicfg |= GSWIP_MII_CFG_MODE_RGMII;
1652 		break;
1653 	case PHY_INTERFACE_MODE_GMII:
1654 		miicfg |= GSWIP_MII_CFG_MODE_GMII;
1655 		break;
1656 	default:
1657 		dev_err(ds->dev,
1658 			"Unsupported interface: %d\n", state->interface);
1659 		return;
1660 	}
1661 
1662 	gswip_mii_mask_cfg(priv,
1663 			   GSWIP_MII_CFG_MODE_MASK | GSWIP_MII_CFG_RMII_CLK |
1664 			   GSWIP_MII_CFG_RGMII_IBS | GSWIP_MII_CFG_LDCLKDIS,
1665 			   miicfg, port);
1666 
1667 	switch (state->interface) {
1668 	case PHY_INTERFACE_MODE_RGMII_ID:
1669 		gswip_mii_mask_pcdu(priv, GSWIP_MII_PCDU_TXDLY_MASK |
1670 					  GSWIP_MII_PCDU_RXDLY_MASK, 0, port);
1671 		break;
1672 	case PHY_INTERFACE_MODE_RGMII_RXID:
1673 		gswip_mii_mask_pcdu(priv, GSWIP_MII_PCDU_RXDLY_MASK, 0, port);
1674 		break;
1675 	case PHY_INTERFACE_MODE_RGMII_TXID:
1676 		gswip_mii_mask_pcdu(priv, GSWIP_MII_PCDU_TXDLY_MASK, 0, port);
1677 		break;
1678 	default:
1679 		break;
1680 	}
1681 }
1682 
1683 static void gswip_phylink_mac_link_down(struct dsa_switch *ds, int port,
1684 					unsigned int mode,
1685 					phy_interface_t interface)
1686 {
1687 	struct gswip_priv *priv = ds->priv;
1688 
1689 	gswip_mii_mask_cfg(priv, GSWIP_MII_CFG_EN, 0, port);
1690 
1691 	if (!dsa_is_cpu_port(ds, port))
1692 		gswip_port_set_link(priv, port, false);
1693 }
1694 
1695 static void gswip_phylink_mac_link_up(struct dsa_switch *ds, int port,
1696 				      unsigned int mode,
1697 				      phy_interface_t interface,
1698 				      struct phy_device *phydev,
1699 				      int speed, int duplex,
1700 				      bool tx_pause, bool rx_pause)
1701 {
1702 	struct gswip_priv *priv = ds->priv;
1703 
1704 	if (!dsa_is_cpu_port(ds, port)) {
1705 		gswip_port_set_link(priv, port, true);
1706 		gswip_port_set_speed(priv, port, speed, interface);
1707 		gswip_port_set_duplex(priv, port, duplex);
1708 		gswip_port_set_pause(priv, port, tx_pause, rx_pause);
1709 	}
1710 
1711 	gswip_mii_mask_cfg(priv, 0, GSWIP_MII_CFG_EN, port);
1712 }
1713 
1714 static void gswip_get_strings(struct dsa_switch *ds, int port, u32 stringset,
1715 			      uint8_t *data)
1716 {
1717 	int i;
1718 
1719 	if (stringset != ETH_SS_STATS)
1720 		return;
1721 
1722 	for (i = 0; i < ARRAY_SIZE(gswip_rmon_cnt); i++)
1723 		strncpy(data + i * ETH_GSTRING_LEN, gswip_rmon_cnt[i].name,
1724 			ETH_GSTRING_LEN);
1725 }
1726 
1727 static u32 gswip_bcm_ram_entry_read(struct gswip_priv *priv, u32 table,
1728 				    u32 index)
1729 {
1730 	u32 result;
1731 	int err;
1732 
1733 	gswip_switch_w(priv, index, GSWIP_BM_RAM_ADDR);
1734 	gswip_switch_mask(priv, GSWIP_BM_RAM_CTRL_ADDR_MASK |
1735 				GSWIP_BM_RAM_CTRL_OPMOD,
1736 			      table | GSWIP_BM_RAM_CTRL_BAS,
1737 			      GSWIP_BM_RAM_CTRL);
1738 
1739 	err = gswip_switch_r_timeout(priv, GSWIP_BM_RAM_CTRL,
1740 				     GSWIP_BM_RAM_CTRL_BAS);
1741 	if (err) {
1742 		dev_err(priv->dev, "timeout while reading table: %u, index: %u",
1743 			table, index);
1744 		return 0;
1745 	}
1746 
1747 	result = gswip_switch_r(priv, GSWIP_BM_RAM_VAL(0));
1748 	result |= gswip_switch_r(priv, GSWIP_BM_RAM_VAL(1)) << 16;
1749 
1750 	return result;
1751 }
1752 
1753 static void gswip_get_ethtool_stats(struct dsa_switch *ds, int port,
1754 				    uint64_t *data)
1755 {
1756 	struct gswip_priv *priv = ds->priv;
1757 	const struct gswip_rmon_cnt_desc *rmon_cnt;
1758 	int i;
1759 	u64 high;
1760 
1761 	for (i = 0; i < ARRAY_SIZE(gswip_rmon_cnt); i++) {
1762 		rmon_cnt = &gswip_rmon_cnt[i];
1763 
1764 		data[i] = gswip_bcm_ram_entry_read(priv, port,
1765 						   rmon_cnt->offset);
1766 		if (rmon_cnt->size == 2) {
1767 			high = gswip_bcm_ram_entry_read(priv, port,
1768 							rmon_cnt->offset + 1);
1769 			data[i] |= high << 32;
1770 		}
1771 	}
1772 }
1773 
1774 static int gswip_get_sset_count(struct dsa_switch *ds, int port, int sset)
1775 {
1776 	if (sset != ETH_SS_STATS)
1777 		return 0;
1778 
1779 	return ARRAY_SIZE(gswip_rmon_cnt);
1780 }
1781 
1782 static const struct dsa_switch_ops gswip_xrx200_switch_ops = {
1783 	.get_tag_protocol	= gswip_get_tag_protocol,
1784 	.setup			= gswip_setup,
1785 	.port_enable		= gswip_port_enable,
1786 	.port_disable		= gswip_port_disable,
1787 	.port_bridge_join	= gswip_port_bridge_join,
1788 	.port_bridge_leave	= gswip_port_bridge_leave,
1789 	.port_fast_age		= gswip_port_fast_age,
1790 	.port_vlan_filtering	= gswip_port_vlan_filtering,
1791 	.port_vlan_add		= gswip_port_vlan_add,
1792 	.port_vlan_del		= gswip_port_vlan_del,
1793 	.port_stp_state_set	= gswip_port_stp_state_set,
1794 	.port_fdb_add		= gswip_port_fdb_add,
1795 	.port_fdb_del		= gswip_port_fdb_del,
1796 	.port_fdb_dump		= gswip_port_fdb_dump,
1797 	.phylink_get_caps	= gswip_xrx200_phylink_get_caps,
1798 	.phylink_mac_config	= gswip_phylink_mac_config,
1799 	.phylink_mac_link_down	= gswip_phylink_mac_link_down,
1800 	.phylink_mac_link_up	= gswip_phylink_mac_link_up,
1801 	.get_strings		= gswip_get_strings,
1802 	.get_ethtool_stats	= gswip_get_ethtool_stats,
1803 	.get_sset_count		= gswip_get_sset_count,
1804 };
1805 
1806 static const struct dsa_switch_ops gswip_xrx300_switch_ops = {
1807 	.get_tag_protocol	= gswip_get_tag_protocol,
1808 	.setup			= gswip_setup,
1809 	.port_enable		= gswip_port_enable,
1810 	.port_disable		= gswip_port_disable,
1811 	.port_bridge_join	= gswip_port_bridge_join,
1812 	.port_bridge_leave	= gswip_port_bridge_leave,
1813 	.port_fast_age		= gswip_port_fast_age,
1814 	.port_vlan_filtering	= gswip_port_vlan_filtering,
1815 	.port_vlan_add		= gswip_port_vlan_add,
1816 	.port_vlan_del		= gswip_port_vlan_del,
1817 	.port_stp_state_set	= gswip_port_stp_state_set,
1818 	.port_fdb_add		= gswip_port_fdb_add,
1819 	.port_fdb_del		= gswip_port_fdb_del,
1820 	.port_fdb_dump		= gswip_port_fdb_dump,
1821 	.phylink_get_caps	= gswip_xrx300_phylink_get_caps,
1822 	.phylink_mac_config	= gswip_phylink_mac_config,
1823 	.phylink_mac_link_down	= gswip_phylink_mac_link_down,
1824 	.phylink_mac_link_up	= gswip_phylink_mac_link_up,
1825 	.get_strings		= gswip_get_strings,
1826 	.get_ethtool_stats	= gswip_get_ethtool_stats,
1827 	.get_sset_count		= gswip_get_sset_count,
1828 };
1829 
1830 static const struct xway_gphy_match_data xrx200a1x_gphy_data = {
1831 	.fe_firmware_name = "lantiq/xrx200_phy22f_a14.bin",
1832 	.ge_firmware_name = "lantiq/xrx200_phy11g_a14.bin",
1833 };
1834 
1835 static const struct xway_gphy_match_data xrx200a2x_gphy_data = {
1836 	.fe_firmware_name = "lantiq/xrx200_phy22f_a22.bin",
1837 	.ge_firmware_name = "lantiq/xrx200_phy11g_a22.bin",
1838 };
1839 
1840 static const struct xway_gphy_match_data xrx300_gphy_data = {
1841 	.fe_firmware_name = "lantiq/xrx300_phy22f_a21.bin",
1842 	.ge_firmware_name = "lantiq/xrx300_phy11g_a21.bin",
1843 };
1844 
1845 static const struct of_device_id xway_gphy_match[] = {
1846 	{ .compatible = "lantiq,xrx200-gphy-fw", .data = NULL },
1847 	{ .compatible = "lantiq,xrx200a1x-gphy-fw", .data = &xrx200a1x_gphy_data },
1848 	{ .compatible = "lantiq,xrx200a2x-gphy-fw", .data = &xrx200a2x_gphy_data },
1849 	{ .compatible = "lantiq,xrx300-gphy-fw", .data = &xrx300_gphy_data },
1850 	{ .compatible = "lantiq,xrx330-gphy-fw", .data = &xrx300_gphy_data },
1851 	{},
1852 };
1853 
1854 static int gswip_gphy_fw_load(struct gswip_priv *priv, struct gswip_gphy_fw *gphy_fw)
1855 {
1856 	struct device *dev = priv->dev;
1857 	const struct firmware *fw;
1858 	void *fw_addr;
1859 	dma_addr_t dma_addr;
1860 	dma_addr_t dev_addr;
1861 	size_t size;
1862 	int ret;
1863 
1864 	ret = clk_prepare_enable(gphy_fw->clk_gate);
1865 	if (ret)
1866 		return ret;
1867 
1868 	reset_control_assert(gphy_fw->reset);
1869 
1870 	/* The vendor BSP uses a 200ms delay after asserting the reset line.
1871 	 * Without this some users are observing that the PHY is not coming up
1872 	 * on the MDIO bus.
1873 	 */
1874 	msleep(200);
1875 
1876 	ret = request_firmware(&fw, gphy_fw->fw_name, dev);
1877 	if (ret) {
1878 		dev_err(dev, "failed to load firmware: %s, error: %i\n",
1879 			gphy_fw->fw_name, ret);
1880 		return ret;
1881 	}
1882 
1883 	/* GPHY cores need the firmware code in a persistent and contiguous
1884 	 * memory area with a 16 kB boundary aligned start address.
1885 	 */
1886 	size = fw->size + XRX200_GPHY_FW_ALIGN;
1887 
1888 	fw_addr = dmam_alloc_coherent(dev, size, &dma_addr, GFP_KERNEL);
1889 	if (fw_addr) {
1890 		fw_addr = PTR_ALIGN(fw_addr, XRX200_GPHY_FW_ALIGN);
1891 		dev_addr = ALIGN(dma_addr, XRX200_GPHY_FW_ALIGN);
1892 		memcpy(fw_addr, fw->data, fw->size);
1893 	} else {
1894 		dev_err(dev, "failed to alloc firmware memory\n");
1895 		release_firmware(fw);
1896 		return -ENOMEM;
1897 	}
1898 
1899 	release_firmware(fw);
1900 
1901 	ret = regmap_write(priv->rcu_regmap, gphy_fw->fw_addr_offset, dev_addr);
1902 	if (ret)
1903 		return ret;
1904 
1905 	reset_control_deassert(gphy_fw->reset);
1906 
1907 	return ret;
1908 }
1909 
1910 static int gswip_gphy_fw_probe(struct gswip_priv *priv,
1911 			       struct gswip_gphy_fw *gphy_fw,
1912 			       struct device_node *gphy_fw_np, int i)
1913 {
1914 	struct device *dev = priv->dev;
1915 	u32 gphy_mode;
1916 	int ret;
1917 	char gphyname[10];
1918 
1919 	snprintf(gphyname, sizeof(gphyname), "gphy%d", i);
1920 
1921 	gphy_fw->clk_gate = devm_clk_get(dev, gphyname);
1922 	if (IS_ERR(gphy_fw->clk_gate)) {
1923 		dev_err(dev, "Failed to lookup gate clock\n");
1924 		return PTR_ERR(gphy_fw->clk_gate);
1925 	}
1926 
1927 	ret = of_property_read_u32(gphy_fw_np, "reg", &gphy_fw->fw_addr_offset);
1928 	if (ret)
1929 		return ret;
1930 
1931 	ret = of_property_read_u32(gphy_fw_np, "lantiq,gphy-mode", &gphy_mode);
1932 	/* Default to GE mode */
1933 	if (ret)
1934 		gphy_mode = GPHY_MODE_GE;
1935 
1936 	switch (gphy_mode) {
1937 	case GPHY_MODE_FE:
1938 		gphy_fw->fw_name = priv->gphy_fw_name_cfg->fe_firmware_name;
1939 		break;
1940 	case GPHY_MODE_GE:
1941 		gphy_fw->fw_name = priv->gphy_fw_name_cfg->ge_firmware_name;
1942 		break;
1943 	default:
1944 		dev_err(dev, "Unknown GPHY mode %d\n", gphy_mode);
1945 		return -EINVAL;
1946 	}
1947 
1948 	gphy_fw->reset = of_reset_control_array_get_exclusive(gphy_fw_np);
1949 	if (IS_ERR(gphy_fw->reset)) {
1950 		if (PTR_ERR(gphy_fw->reset) != -EPROBE_DEFER)
1951 			dev_err(dev, "Failed to lookup gphy reset\n");
1952 		return PTR_ERR(gphy_fw->reset);
1953 	}
1954 
1955 	return gswip_gphy_fw_load(priv, gphy_fw);
1956 }
1957 
1958 static void gswip_gphy_fw_remove(struct gswip_priv *priv,
1959 				 struct gswip_gphy_fw *gphy_fw)
1960 {
1961 	int ret;
1962 
1963 	/* check if the device was fully probed */
1964 	if (!gphy_fw->fw_name)
1965 		return;
1966 
1967 	ret = regmap_write(priv->rcu_regmap, gphy_fw->fw_addr_offset, 0);
1968 	if (ret)
1969 		dev_err(priv->dev, "can not reset GPHY FW pointer");
1970 
1971 	clk_disable_unprepare(gphy_fw->clk_gate);
1972 
1973 	reset_control_put(gphy_fw->reset);
1974 }
1975 
1976 static int gswip_gphy_fw_list(struct gswip_priv *priv,
1977 			      struct device_node *gphy_fw_list_np, u32 version)
1978 {
1979 	struct device *dev = priv->dev;
1980 	struct device_node *gphy_fw_np;
1981 	const struct of_device_id *match;
1982 	int err;
1983 	int i = 0;
1984 
1985 	/* The VRX200 rev 1.1 uses the GSWIP 2.0 and needs the older
1986 	 * GPHY firmware. The VRX200 rev 1.2 uses the GSWIP 2.1 and also
1987 	 * needs a different GPHY firmware.
1988 	 */
1989 	if (of_device_is_compatible(gphy_fw_list_np, "lantiq,xrx200-gphy-fw")) {
1990 		switch (version) {
1991 		case GSWIP_VERSION_2_0:
1992 			priv->gphy_fw_name_cfg = &xrx200a1x_gphy_data;
1993 			break;
1994 		case GSWIP_VERSION_2_1:
1995 			priv->gphy_fw_name_cfg = &xrx200a2x_gphy_data;
1996 			break;
1997 		default:
1998 			dev_err(dev, "unknown GSWIP version: 0x%x", version);
1999 			return -ENOENT;
2000 		}
2001 	}
2002 
2003 	match = of_match_node(xway_gphy_match, gphy_fw_list_np);
2004 	if (match && match->data)
2005 		priv->gphy_fw_name_cfg = match->data;
2006 
2007 	if (!priv->gphy_fw_name_cfg) {
2008 		dev_err(dev, "GPHY compatible type not supported");
2009 		return -ENOENT;
2010 	}
2011 
2012 	priv->num_gphy_fw = of_get_available_child_count(gphy_fw_list_np);
2013 	if (!priv->num_gphy_fw)
2014 		return -ENOENT;
2015 
2016 	priv->rcu_regmap = syscon_regmap_lookup_by_phandle(gphy_fw_list_np,
2017 							   "lantiq,rcu");
2018 	if (IS_ERR(priv->rcu_regmap))
2019 		return PTR_ERR(priv->rcu_regmap);
2020 
2021 	priv->gphy_fw = devm_kmalloc_array(dev, priv->num_gphy_fw,
2022 					   sizeof(*priv->gphy_fw),
2023 					   GFP_KERNEL | __GFP_ZERO);
2024 	if (!priv->gphy_fw)
2025 		return -ENOMEM;
2026 
2027 	for_each_available_child_of_node(gphy_fw_list_np, gphy_fw_np) {
2028 		err = gswip_gphy_fw_probe(priv, &priv->gphy_fw[i],
2029 					  gphy_fw_np, i);
2030 		if (err)
2031 			goto remove_gphy;
2032 		i++;
2033 	}
2034 
2035 	/* The standalone PHY11G requires 300ms to be fully
2036 	 * initialized and ready for any MDIO communication after being
2037 	 * taken out of reset. For the SoC-internal GPHY variant there
2038 	 * is no (known) documentation for the minimum time after a
2039 	 * reset. Use the same value as for the standalone variant as
2040 	 * some users have reported internal PHYs not being detected
2041 	 * without any delay.
2042 	 */
2043 	msleep(300);
2044 
2045 	return 0;
2046 
2047 remove_gphy:
2048 	for (i = 0; i < priv->num_gphy_fw; i++)
2049 		gswip_gphy_fw_remove(priv, &priv->gphy_fw[i]);
2050 	return err;
2051 }
2052 
2053 static int gswip_probe(struct platform_device *pdev)
2054 {
2055 	struct gswip_priv *priv;
2056 	struct device_node *np, *mdio_np, *gphy_fw_np;
2057 	struct device *dev = &pdev->dev;
2058 	int err;
2059 	int i;
2060 	u32 version;
2061 
2062 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
2063 	if (!priv)
2064 		return -ENOMEM;
2065 
2066 	priv->gswip = devm_platform_ioremap_resource(pdev, 0);
2067 	if (IS_ERR(priv->gswip))
2068 		return PTR_ERR(priv->gswip);
2069 
2070 	priv->mdio = devm_platform_ioremap_resource(pdev, 1);
2071 	if (IS_ERR(priv->mdio))
2072 		return PTR_ERR(priv->mdio);
2073 
2074 	priv->mii = devm_platform_ioremap_resource(pdev, 2);
2075 	if (IS_ERR(priv->mii))
2076 		return PTR_ERR(priv->mii);
2077 
2078 	priv->hw_info = of_device_get_match_data(dev);
2079 	if (!priv->hw_info)
2080 		return -EINVAL;
2081 
2082 	priv->ds = devm_kzalloc(dev, sizeof(*priv->ds), GFP_KERNEL);
2083 	if (!priv->ds)
2084 		return -ENOMEM;
2085 
2086 	priv->ds->dev = dev;
2087 	priv->ds->num_ports = priv->hw_info->max_ports;
2088 	priv->ds->priv = priv;
2089 	priv->ds->ops = priv->hw_info->ops;
2090 	priv->dev = dev;
2091 	mutex_init(&priv->pce_table_lock);
2092 	version = gswip_switch_r(priv, GSWIP_VERSION);
2093 
2094 	np = dev->of_node;
2095 	switch (version) {
2096 	case GSWIP_VERSION_2_0:
2097 	case GSWIP_VERSION_2_1:
2098 		if (!of_device_is_compatible(np, "lantiq,xrx200-gswip"))
2099 			return -EINVAL;
2100 		break;
2101 	case GSWIP_VERSION_2_2:
2102 	case GSWIP_VERSION_2_2_ETC:
2103 		if (!of_device_is_compatible(np, "lantiq,xrx300-gswip") &&
2104 		    !of_device_is_compatible(np, "lantiq,xrx330-gswip"))
2105 			return -EINVAL;
2106 		break;
2107 	default:
2108 		dev_err(dev, "unknown GSWIP version: 0x%x", version);
2109 		return -ENOENT;
2110 	}
2111 
2112 	/* bring up the mdio bus */
2113 	gphy_fw_np = of_get_compatible_child(dev->of_node, "lantiq,gphy-fw");
2114 	if (gphy_fw_np) {
2115 		err = gswip_gphy_fw_list(priv, gphy_fw_np, version);
2116 		of_node_put(gphy_fw_np);
2117 		if (err) {
2118 			dev_err(dev, "gphy fw probe failed\n");
2119 			return err;
2120 		}
2121 	}
2122 
2123 	/* bring up the mdio bus */
2124 	mdio_np = of_get_compatible_child(dev->of_node, "lantiq,xrx200-mdio");
2125 	if (mdio_np) {
2126 		err = gswip_mdio(priv, mdio_np);
2127 		if (err) {
2128 			dev_err(dev, "mdio probe failed\n");
2129 			goto put_mdio_node;
2130 		}
2131 	}
2132 
2133 	err = dsa_register_switch(priv->ds);
2134 	if (err) {
2135 		dev_err(dev, "dsa switch register failed: %i\n", err);
2136 		goto mdio_bus;
2137 	}
2138 	if (!dsa_is_cpu_port(priv->ds, priv->hw_info->cpu_port)) {
2139 		dev_err(dev, "wrong CPU port defined, HW only supports port: %i",
2140 			priv->hw_info->cpu_port);
2141 		err = -EINVAL;
2142 		goto disable_switch;
2143 	}
2144 
2145 	platform_set_drvdata(pdev, priv);
2146 
2147 	dev_info(dev, "probed GSWIP version %lx mod %lx\n",
2148 		 (version & GSWIP_VERSION_REV_MASK) >> GSWIP_VERSION_REV_SHIFT,
2149 		 (version & GSWIP_VERSION_MOD_MASK) >> GSWIP_VERSION_MOD_SHIFT);
2150 	return 0;
2151 
2152 disable_switch:
2153 	gswip_mdio_mask(priv, GSWIP_MDIO_GLOB_ENABLE, 0, GSWIP_MDIO_GLOB);
2154 	dsa_unregister_switch(priv->ds);
2155 mdio_bus:
2156 	if (mdio_np) {
2157 		mdiobus_unregister(priv->ds->slave_mii_bus);
2158 		mdiobus_free(priv->ds->slave_mii_bus);
2159 	}
2160 put_mdio_node:
2161 	of_node_put(mdio_np);
2162 	for (i = 0; i < priv->num_gphy_fw; i++)
2163 		gswip_gphy_fw_remove(priv, &priv->gphy_fw[i]);
2164 	return err;
2165 }
2166 
2167 static int gswip_remove(struct platform_device *pdev)
2168 {
2169 	struct gswip_priv *priv = platform_get_drvdata(pdev);
2170 	int i;
2171 
2172 	if (!priv)
2173 		return 0;
2174 
2175 	/* disable the switch */
2176 	gswip_mdio_mask(priv, GSWIP_MDIO_GLOB_ENABLE, 0, GSWIP_MDIO_GLOB);
2177 
2178 	dsa_unregister_switch(priv->ds);
2179 
2180 	if (priv->ds->slave_mii_bus) {
2181 		mdiobus_unregister(priv->ds->slave_mii_bus);
2182 		of_node_put(priv->ds->slave_mii_bus->dev.of_node);
2183 		mdiobus_free(priv->ds->slave_mii_bus);
2184 	}
2185 
2186 	for (i = 0; i < priv->num_gphy_fw; i++)
2187 		gswip_gphy_fw_remove(priv, &priv->gphy_fw[i]);
2188 
2189 	platform_set_drvdata(pdev, NULL);
2190 
2191 	return 0;
2192 }
2193 
2194 static void gswip_shutdown(struct platform_device *pdev)
2195 {
2196 	struct gswip_priv *priv = platform_get_drvdata(pdev);
2197 
2198 	if (!priv)
2199 		return;
2200 
2201 	dsa_switch_shutdown(priv->ds);
2202 
2203 	platform_set_drvdata(pdev, NULL);
2204 }
2205 
2206 static const struct gswip_hw_info gswip_xrx200 = {
2207 	.max_ports = 7,
2208 	.cpu_port = 6,
2209 	.ops = &gswip_xrx200_switch_ops,
2210 };
2211 
2212 static const struct gswip_hw_info gswip_xrx300 = {
2213 	.max_ports = 7,
2214 	.cpu_port = 6,
2215 	.ops = &gswip_xrx300_switch_ops,
2216 };
2217 
2218 static const struct of_device_id gswip_of_match[] = {
2219 	{ .compatible = "lantiq,xrx200-gswip", .data = &gswip_xrx200 },
2220 	{ .compatible = "lantiq,xrx300-gswip", .data = &gswip_xrx300 },
2221 	{ .compatible = "lantiq,xrx330-gswip", .data = &gswip_xrx300 },
2222 	{},
2223 };
2224 MODULE_DEVICE_TABLE(of, gswip_of_match);
2225 
2226 static struct platform_driver gswip_driver = {
2227 	.probe = gswip_probe,
2228 	.remove = gswip_remove,
2229 	.shutdown = gswip_shutdown,
2230 	.driver = {
2231 		.name = "gswip",
2232 		.of_match_table = gswip_of_match,
2233 	},
2234 };
2235 
2236 module_platform_driver(gswip_driver);
2237 
2238 MODULE_FIRMWARE("lantiq/xrx300_phy11g_a21.bin");
2239 MODULE_FIRMWARE("lantiq/xrx300_phy22f_a21.bin");
2240 MODULE_FIRMWARE("lantiq/xrx200_phy11g_a14.bin");
2241 MODULE_FIRMWARE("lantiq/xrx200_phy11g_a22.bin");
2242 MODULE_FIRMWARE("lantiq/xrx200_phy22f_a14.bin");
2243 MODULE_FIRMWARE("lantiq/xrx200_phy22f_a22.bin");
2244 MODULE_AUTHOR("Hauke Mehrtens <hauke@hauke-m.de>");
2245 MODULE_DESCRIPTION("Lantiq / Intel GSWIP driver");
2246 MODULE_LICENSE("GPL v2");
2247