xref: /linux/drivers/net/dsa/b53/b53_common.c (revision 320bc9b8e2bc8a5fc75537082529b534ace633e3)
1 /*
2  * B53 switch driver main logic
3  *
4  * Copyright (C) 2011-2013 Jonas Gorski <jogo@openwrt.org>
5  * Copyright (C) 2016 Florian Fainelli <f.fainelli@gmail.com>
6  *
7  * Permission to use, copy, modify, and/or distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 #include <linux/delay.h>
21 #include <linux/export.h>
22 #include <linux/gpio/consumer.h>
23 #include <linux/kernel.h>
24 #include <linux/math.h>
25 #include <linux/minmax.h>
26 #include <linux/module.h>
27 #include <linux/platform_data/b53.h>
28 #include <linux/phy.h>
29 #include <linux/phylink.h>
30 #include <linux/etherdevice.h>
31 #include <linux/if_bridge.h>
32 #include <linux/if_vlan.h>
33 #include <net/dsa.h>
34 
35 #include "b53_regs.h"
36 #include "b53_priv.h"
37 
38 struct b53_mib_desc {
39 	u8 size;
40 	u8 offset;
41 	const char *name;
42 };
43 
44 /* BCM5365 MIB counters */
45 static const struct b53_mib_desc b53_mibs_65[] = {
46 	{ 8, 0x00, "TxOctets" },
47 	{ 4, 0x08, "TxDropPkts" },
48 	{ 4, 0x10, "TxBroadcastPkts" },
49 	{ 4, 0x14, "TxMulticastPkts" },
50 	{ 4, 0x18, "TxUnicastPkts" },
51 	{ 4, 0x1c, "TxCollisions" },
52 	{ 4, 0x20, "TxSingleCollision" },
53 	{ 4, 0x24, "TxMultipleCollision" },
54 	{ 4, 0x28, "TxDeferredTransmit" },
55 	{ 4, 0x2c, "TxLateCollision" },
56 	{ 4, 0x30, "TxExcessiveCollision" },
57 	{ 4, 0x38, "TxPausePkts" },
58 	{ 8, 0x44, "RxOctets" },
59 	{ 4, 0x4c, "RxUndersizePkts" },
60 	{ 4, 0x50, "RxPausePkts" },
61 	{ 4, 0x54, "Pkts64Octets" },
62 	{ 4, 0x58, "Pkts65to127Octets" },
63 	{ 4, 0x5c, "Pkts128to255Octets" },
64 	{ 4, 0x60, "Pkts256to511Octets" },
65 	{ 4, 0x64, "Pkts512to1023Octets" },
66 	{ 4, 0x68, "Pkts1024to1522Octets" },
67 	{ 4, 0x6c, "RxOversizePkts" },
68 	{ 4, 0x70, "RxJabbers" },
69 	{ 4, 0x74, "RxAlignmentErrors" },
70 	{ 4, 0x78, "RxFCSErrors" },
71 	{ 8, 0x7c, "RxGoodOctets" },
72 	{ 4, 0x84, "RxDropPkts" },
73 	{ 4, 0x88, "RxUnicastPkts" },
74 	{ 4, 0x8c, "RxMulticastPkts" },
75 	{ 4, 0x90, "RxBroadcastPkts" },
76 	{ 4, 0x94, "RxSAChanges" },
77 	{ 4, 0x98, "RxFragments" },
78 };
79 
80 #define B53_MIBS_65_SIZE	ARRAY_SIZE(b53_mibs_65)
81 
82 /* BCM63xx MIB counters */
83 static const struct b53_mib_desc b53_mibs_63xx[] = {
84 	{ 8, 0x00, "TxOctets" },
85 	{ 4, 0x08, "TxDropPkts" },
86 	{ 4, 0x0c, "TxQoSPkts" },
87 	{ 4, 0x10, "TxBroadcastPkts" },
88 	{ 4, 0x14, "TxMulticastPkts" },
89 	{ 4, 0x18, "TxUnicastPkts" },
90 	{ 4, 0x1c, "TxCollisions" },
91 	{ 4, 0x20, "TxSingleCollision" },
92 	{ 4, 0x24, "TxMultipleCollision" },
93 	{ 4, 0x28, "TxDeferredTransmit" },
94 	{ 4, 0x2c, "TxLateCollision" },
95 	{ 4, 0x30, "TxExcessiveCollision" },
96 	{ 4, 0x38, "TxPausePkts" },
97 	{ 8, 0x3c, "TxQoSOctets" },
98 	{ 8, 0x44, "RxOctets" },
99 	{ 4, 0x4c, "RxUndersizePkts" },
100 	{ 4, 0x50, "RxPausePkts" },
101 	{ 4, 0x54, "Pkts64Octets" },
102 	{ 4, 0x58, "Pkts65to127Octets" },
103 	{ 4, 0x5c, "Pkts128to255Octets" },
104 	{ 4, 0x60, "Pkts256to511Octets" },
105 	{ 4, 0x64, "Pkts512to1023Octets" },
106 	{ 4, 0x68, "Pkts1024to1522Octets" },
107 	{ 4, 0x6c, "RxOversizePkts" },
108 	{ 4, 0x70, "RxJabbers" },
109 	{ 4, 0x74, "RxAlignmentErrors" },
110 	{ 4, 0x78, "RxFCSErrors" },
111 	{ 8, 0x7c, "RxGoodOctets" },
112 	{ 4, 0x84, "RxDropPkts" },
113 	{ 4, 0x88, "RxUnicastPkts" },
114 	{ 4, 0x8c, "RxMulticastPkts" },
115 	{ 4, 0x90, "RxBroadcastPkts" },
116 	{ 4, 0x94, "RxSAChanges" },
117 	{ 4, 0x98, "RxFragments" },
118 	{ 4, 0xa0, "RxSymbolErrors" },
119 	{ 4, 0xa4, "RxQoSPkts" },
120 	{ 8, 0xa8, "RxQoSOctets" },
121 	{ 4, 0xb0, "Pkts1523to2047Octets" },
122 	{ 4, 0xb4, "Pkts2048to4095Octets" },
123 	{ 4, 0xb8, "Pkts4096to8191Octets" },
124 	{ 4, 0xbc, "Pkts8192to9728Octets" },
125 	{ 4, 0xc0, "RxDiscarded" },
126 };
127 
128 #define B53_MIBS_63XX_SIZE	ARRAY_SIZE(b53_mibs_63xx)
129 
130 /* MIB counters */
131 static const struct b53_mib_desc b53_mibs[] = {
132 	{ 8, 0x00, "TxOctets" },
133 	{ 4, 0x08, "TxDropPkts" },
134 	{ 4, 0x10, "TxBroadcastPkts" },
135 	{ 4, 0x14, "TxMulticastPkts" },
136 	{ 4, 0x18, "TxUnicastPkts" },
137 	{ 4, 0x1c, "TxCollisions" },
138 	{ 4, 0x20, "TxSingleCollision" },
139 	{ 4, 0x24, "TxMultipleCollision" },
140 	{ 4, 0x28, "TxDeferredTransmit" },
141 	{ 4, 0x2c, "TxLateCollision" },
142 	{ 4, 0x30, "TxExcessiveCollision" },
143 	{ 4, 0x38, "TxPausePkts" },
144 	{ 8, 0x50, "RxOctets" },
145 	{ 4, 0x58, "RxUndersizePkts" },
146 	{ 4, 0x5c, "RxPausePkts" },
147 	{ 4, 0x60, "Pkts64Octets" },
148 	{ 4, 0x64, "Pkts65to127Octets" },
149 	{ 4, 0x68, "Pkts128to255Octets" },
150 	{ 4, 0x6c, "Pkts256to511Octets" },
151 	{ 4, 0x70, "Pkts512to1023Octets" },
152 	{ 4, 0x74, "Pkts1024to1522Octets" },
153 	{ 4, 0x78, "RxOversizePkts" },
154 	{ 4, 0x7c, "RxJabbers" },
155 	{ 4, 0x80, "RxAlignmentErrors" },
156 	{ 4, 0x84, "RxFCSErrors" },
157 	{ 8, 0x88, "RxGoodOctets" },
158 	{ 4, 0x90, "RxDropPkts" },
159 	{ 4, 0x94, "RxUnicastPkts" },
160 	{ 4, 0x98, "RxMulticastPkts" },
161 	{ 4, 0x9c, "RxBroadcastPkts" },
162 	{ 4, 0xa0, "RxSAChanges" },
163 	{ 4, 0xa4, "RxFragments" },
164 	{ 4, 0xa8, "RxJumboPkts" },
165 	{ 4, 0xac, "RxSymbolErrors" },
166 	{ 4, 0xc0, "RxDiscarded" },
167 };
168 
169 #define B53_MIBS_SIZE	ARRAY_SIZE(b53_mibs)
170 
171 static const struct b53_mib_desc b53_mibs_58xx[] = {
172 	{ 8, 0x00, "TxOctets" },
173 	{ 4, 0x08, "TxDropPkts" },
174 	{ 4, 0x0c, "TxQPKTQ0" },
175 	{ 4, 0x10, "TxBroadcastPkts" },
176 	{ 4, 0x14, "TxMulticastPkts" },
177 	{ 4, 0x18, "TxUnicastPKts" },
178 	{ 4, 0x1c, "TxCollisions" },
179 	{ 4, 0x20, "TxSingleCollision" },
180 	{ 4, 0x24, "TxMultipleCollision" },
181 	{ 4, 0x28, "TxDeferredCollision" },
182 	{ 4, 0x2c, "TxLateCollision" },
183 	{ 4, 0x30, "TxExcessiveCollision" },
184 	{ 4, 0x34, "TxFrameInDisc" },
185 	{ 4, 0x38, "TxPausePkts" },
186 	{ 4, 0x3c, "TxQPKTQ1" },
187 	{ 4, 0x40, "TxQPKTQ2" },
188 	{ 4, 0x44, "TxQPKTQ3" },
189 	{ 4, 0x48, "TxQPKTQ4" },
190 	{ 4, 0x4c, "TxQPKTQ5" },
191 	{ 8, 0x50, "RxOctets" },
192 	{ 4, 0x58, "RxUndersizePkts" },
193 	{ 4, 0x5c, "RxPausePkts" },
194 	{ 4, 0x60, "RxPkts64Octets" },
195 	{ 4, 0x64, "RxPkts65to127Octets" },
196 	{ 4, 0x68, "RxPkts128to255Octets" },
197 	{ 4, 0x6c, "RxPkts256to511Octets" },
198 	{ 4, 0x70, "RxPkts512to1023Octets" },
199 	{ 4, 0x74, "RxPkts1024toMaxPktsOctets" },
200 	{ 4, 0x78, "RxOversizePkts" },
201 	{ 4, 0x7c, "RxJabbers" },
202 	{ 4, 0x80, "RxAlignmentErrors" },
203 	{ 4, 0x84, "RxFCSErrors" },
204 	{ 8, 0x88, "RxGoodOctets" },
205 	{ 4, 0x90, "RxDropPkts" },
206 	{ 4, 0x94, "RxUnicastPkts" },
207 	{ 4, 0x98, "RxMulticastPkts" },
208 	{ 4, 0x9c, "RxBroadcastPkts" },
209 	{ 4, 0xa0, "RxSAChanges" },
210 	{ 4, 0xa4, "RxFragments" },
211 	{ 4, 0xa8, "RxJumboPkt" },
212 	{ 4, 0xac, "RxSymblErr" },
213 	{ 4, 0xb0, "InRangeErrCount" },
214 	{ 4, 0xb4, "OutRangeErrCount" },
215 	{ 4, 0xb8, "EEELpiEvent" },
216 	{ 4, 0xbc, "EEELpiDuration" },
217 	{ 4, 0xc0, "RxDiscard" },
218 	{ 4, 0xc8, "TxQPKTQ6" },
219 	{ 4, 0xcc, "TxQPKTQ7" },
220 	{ 4, 0xd0, "TxPkts64Octets" },
221 	{ 4, 0xd4, "TxPkts65to127Octets" },
222 	{ 4, 0xd8, "TxPkts128to255Octets" },
223 	{ 4, 0xdc, "TxPkts256to511Ocets" },
224 	{ 4, 0xe0, "TxPkts512to1023Ocets" },
225 	{ 4, 0xe4, "TxPkts1024toMaxPktOcets" },
226 };
227 
228 #define B53_MIBS_58XX_SIZE	ARRAY_SIZE(b53_mibs_58xx)
229 
230 #define B53_MAX_MTU_25		(1536 - ETH_HLEN - VLAN_HLEN - ETH_FCS_LEN)
231 #define B53_MAX_MTU		(9720 - ETH_HLEN - VLAN_HLEN - ETH_FCS_LEN)
232 
233 static int b53_do_vlan_op(struct b53_device *dev, u8 op)
234 {
235 	unsigned int i;
236 
237 	b53_write8(dev, B53_ARLIO_PAGE, dev->vta_regs[0], VTA_START_CMD | op);
238 
239 	for (i = 0; i < 10; i++) {
240 		u8 vta;
241 
242 		b53_read8(dev, B53_ARLIO_PAGE, dev->vta_regs[0], &vta);
243 		if (!(vta & VTA_START_CMD))
244 			return 0;
245 
246 		usleep_range(100, 200);
247 	}
248 
249 	return -EIO;
250 }
251 
252 static void b53_set_vlan_entry(struct b53_device *dev, u16 vid,
253 			       struct b53_vlan *vlan)
254 {
255 	if (is5325(dev)) {
256 		u32 entry = 0;
257 
258 		if (vlan->members) {
259 			entry = ((vlan->untag & VA_UNTAG_MASK_25) <<
260 				 VA_UNTAG_S_25) | vlan->members;
261 			if (dev->core_rev >= 3)
262 				entry |= VA_VALID_25_R4 | vid << VA_VID_HIGH_S;
263 			else
264 				entry |= VA_VALID_25;
265 		}
266 
267 		b53_write32(dev, B53_VLAN_PAGE, B53_VLAN_WRITE_25, entry);
268 		b53_write16(dev, B53_VLAN_PAGE, B53_VLAN_TABLE_ACCESS_25, vid |
269 			    VTA_RW_STATE_WR | VTA_RW_OP_EN);
270 	} else if (is5365(dev)) {
271 		u16 entry = 0;
272 
273 		if (vlan->members)
274 			entry = ((vlan->untag & VA_UNTAG_MASK_65) <<
275 				 VA_UNTAG_S_65) | vlan->members | VA_VALID_65;
276 
277 		b53_write16(dev, B53_VLAN_PAGE, B53_VLAN_WRITE_65, entry);
278 		b53_write16(dev, B53_VLAN_PAGE, B53_VLAN_TABLE_ACCESS_65, vid |
279 			    VTA_RW_STATE_WR | VTA_RW_OP_EN);
280 	} else {
281 		b53_write16(dev, B53_ARLIO_PAGE, dev->vta_regs[1], vid);
282 		b53_write32(dev, B53_ARLIO_PAGE, dev->vta_regs[2],
283 			    (vlan->untag << VTE_UNTAG_S) | vlan->members);
284 
285 		b53_do_vlan_op(dev, VTA_CMD_WRITE);
286 	}
287 
288 	dev_dbg(dev->ds->dev, "VID: %d, members: 0x%04x, untag: 0x%04x\n",
289 		vid, vlan->members, vlan->untag);
290 }
291 
292 static void b53_get_vlan_entry(struct b53_device *dev, u16 vid,
293 			       struct b53_vlan *vlan)
294 {
295 	if (is5325(dev)) {
296 		u32 entry = 0;
297 
298 		b53_write16(dev, B53_VLAN_PAGE, B53_VLAN_TABLE_ACCESS_25, vid |
299 			    VTA_RW_STATE_RD | VTA_RW_OP_EN);
300 		b53_read32(dev, B53_VLAN_PAGE, B53_VLAN_WRITE_25, &entry);
301 
302 		if (dev->core_rev >= 3)
303 			vlan->valid = !!(entry & VA_VALID_25_R4);
304 		else
305 			vlan->valid = !!(entry & VA_VALID_25);
306 		vlan->members = entry & VA_MEMBER_MASK;
307 		vlan->untag = (entry >> VA_UNTAG_S_25) & VA_UNTAG_MASK_25;
308 
309 	} else if (is5365(dev)) {
310 		u16 entry = 0;
311 
312 		b53_write16(dev, B53_VLAN_PAGE, B53_VLAN_TABLE_ACCESS_65, vid |
313 			    VTA_RW_STATE_WR | VTA_RW_OP_EN);
314 		b53_read16(dev, B53_VLAN_PAGE, B53_VLAN_WRITE_65, &entry);
315 
316 		vlan->valid = !!(entry & VA_VALID_65);
317 		vlan->members = entry & VA_MEMBER_MASK;
318 		vlan->untag = (entry >> VA_UNTAG_S_65) & VA_UNTAG_MASK_65;
319 	} else {
320 		u32 entry = 0;
321 
322 		b53_write16(dev, B53_ARLIO_PAGE, dev->vta_regs[1], vid);
323 		b53_do_vlan_op(dev, VTA_CMD_READ);
324 		b53_read32(dev, B53_ARLIO_PAGE, dev->vta_regs[2], &entry);
325 		vlan->members = entry & VTE_MEMBERS;
326 		vlan->untag = (entry >> VTE_UNTAG_S) & VTE_MEMBERS;
327 		vlan->valid = true;
328 	}
329 }
330 
331 static void b53_set_eap_mode(struct b53_device *dev, int port, int mode)
332 {
333 	u64 eap_conf;
334 
335 	if (is5325(dev) || is5365(dev) || dev->chip_id == BCM5389_DEVICE_ID)
336 		return;
337 
338 	b53_read64(dev, B53_EAP_PAGE, B53_PORT_EAP_CONF(port), &eap_conf);
339 
340 	if (is63xx(dev)) {
341 		eap_conf &= ~EAP_MODE_MASK_63XX;
342 		eap_conf |= (u64)mode << EAP_MODE_SHIFT_63XX;
343 	} else {
344 		eap_conf &= ~EAP_MODE_MASK;
345 		eap_conf |= (u64)mode << EAP_MODE_SHIFT;
346 	}
347 
348 	b53_write64(dev, B53_EAP_PAGE, B53_PORT_EAP_CONF(port), eap_conf);
349 }
350 
351 static void b53_set_forwarding(struct b53_device *dev, int enable)
352 {
353 	u8 mgmt;
354 
355 	b53_read8(dev, B53_CTRL_PAGE, B53_SWITCH_MODE, &mgmt);
356 
357 	if (enable)
358 		mgmt |= SM_SW_FWD_EN;
359 	else
360 		mgmt &= ~SM_SW_FWD_EN;
361 
362 	b53_write8(dev, B53_CTRL_PAGE, B53_SWITCH_MODE, mgmt);
363 
364 	if (!is5325(dev)) {
365 		/* Include IMP port in dumb forwarding mode */
366 		b53_read8(dev, B53_CTRL_PAGE, B53_SWITCH_CTRL, &mgmt);
367 		mgmt |= B53_MII_DUMB_FWDG_EN;
368 		b53_write8(dev, B53_CTRL_PAGE, B53_SWITCH_CTRL, mgmt);
369 
370 		/* Look at B53_UC_FWD_EN and B53_MC_FWD_EN to decide whether
371 		 * frames should be flooded or not.
372 		 */
373 		b53_read8(dev, B53_CTRL_PAGE, B53_IP_MULTICAST_CTRL, &mgmt);
374 		mgmt |= B53_UC_FWD_EN | B53_MC_FWD_EN | B53_IP_MC;
375 		b53_write8(dev, B53_CTRL_PAGE, B53_IP_MULTICAST_CTRL, mgmt);
376 	} else {
377 		b53_read8(dev, B53_CTRL_PAGE, B53_IP_MULTICAST_CTRL, &mgmt);
378 		mgmt |= B53_IP_MC;
379 		b53_write8(dev, B53_CTRL_PAGE, B53_IP_MULTICAST_CTRL, mgmt);
380 	}
381 }
382 
383 static void b53_enable_vlan(struct b53_device *dev, int port, bool enable,
384 			    bool enable_filtering)
385 {
386 	u8 mgmt, vc0, vc1, vc4 = 0, vc5;
387 
388 	b53_read8(dev, B53_CTRL_PAGE, B53_SWITCH_MODE, &mgmt);
389 	b53_read8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL0, &vc0);
390 	b53_read8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL1, &vc1);
391 
392 	if (is5325(dev) || is5365(dev)) {
393 		b53_read8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL4_25, &vc4);
394 		b53_read8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL5_25, &vc5);
395 	} else if (is63xx(dev)) {
396 		b53_read8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL4_63XX, &vc4);
397 		b53_read8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL5_63XX, &vc5);
398 	} else {
399 		b53_read8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL4, &vc4);
400 		b53_read8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL5, &vc5);
401 	}
402 
403 	vc1 &= ~VC1_RX_MCST_FWD_EN;
404 
405 	if (enable) {
406 		vc0 |= VC0_VLAN_EN | VC0_VID_CHK_EN | VC0_VID_HASH_VID;
407 		vc1 |= VC1_RX_MCST_UNTAG_EN;
408 		vc4 &= ~VC4_ING_VID_CHECK_MASK;
409 		if (enable_filtering) {
410 			vc4 |= VC4_ING_VID_VIO_DROP << VC4_ING_VID_CHECK_S;
411 			vc5 |= VC5_DROP_VTABLE_MISS;
412 		} else {
413 			vc4 |= VC4_NO_ING_VID_CHK << VC4_ING_VID_CHECK_S;
414 			vc5 &= ~VC5_DROP_VTABLE_MISS;
415 		}
416 
417 		if (is5325(dev))
418 			vc0 &= ~VC0_RESERVED_1;
419 
420 		if (is5325(dev) || is5365(dev))
421 			vc1 |= VC1_RX_MCST_TAG_EN;
422 
423 	} else {
424 		vc0 &= ~(VC0_VLAN_EN | VC0_VID_CHK_EN | VC0_VID_HASH_VID);
425 		vc1 &= ~VC1_RX_MCST_UNTAG_EN;
426 		vc4 &= ~VC4_ING_VID_CHECK_MASK;
427 		vc5 &= ~VC5_DROP_VTABLE_MISS;
428 
429 		if (is5325(dev) || is5365(dev))
430 			vc4 |= VC4_ING_VID_VIO_FWD << VC4_ING_VID_CHECK_S;
431 		else
432 			vc4 |= VC4_ING_VID_VIO_TO_IMP << VC4_ING_VID_CHECK_S;
433 
434 		if (is5325(dev) || is5365(dev))
435 			vc1 &= ~VC1_RX_MCST_TAG_EN;
436 	}
437 
438 	if (!is5325(dev) && !is5365(dev))
439 		vc5 &= ~VC5_VID_FFF_EN;
440 
441 	b53_write8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL0, vc0);
442 	b53_write8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL1, vc1);
443 
444 	if (is5325(dev) || is5365(dev)) {
445 		/* enable the high 8 bit vid check on 5325 */
446 		if (is5325(dev) && enable)
447 			b53_write8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL3,
448 				   VC3_HIGH_8BIT_EN);
449 		else
450 			b53_write8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL3, 0);
451 
452 		b53_write8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL4_25, vc4);
453 		b53_write8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL5_25, vc5);
454 	} else if (is63xx(dev)) {
455 		b53_write16(dev, B53_VLAN_PAGE, B53_VLAN_CTRL3_63XX, 0);
456 		b53_write8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL4_63XX, vc4);
457 		b53_write8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL5_63XX, vc5);
458 	} else {
459 		b53_write16(dev, B53_VLAN_PAGE, B53_VLAN_CTRL3, 0);
460 		b53_write8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL4, vc4);
461 		b53_write8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL5, vc5);
462 	}
463 
464 	b53_write8(dev, B53_CTRL_PAGE, B53_SWITCH_MODE, mgmt);
465 
466 	dev->vlan_enabled = enable;
467 
468 	dev_dbg(dev->dev, "Port %d VLAN enabled: %d, filtering: %d\n",
469 		port, enable, enable_filtering);
470 }
471 
472 static int b53_set_jumbo(struct b53_device *dev, bool enable, bool allow_10_100)
473 {
474 	u32 port_mask = 0;
475 	u16 max_size = JMS_MIN_SIZE;
476 
477 	if (is5325(dev) || is5365(dev))
478 		return -EINVAL;
479 
480 	if (enable) {
481 		port_mask = dev->enabled_ports;
482 		max_size = JMS_MAX_SIZE;
483 		if (allow_10_100)
484 			port_mask |= JPM_10_100_JUMBO_EN;
485 	}
486 
487 	b53_write32(dev, B53_JUMBO_PAGE, dev->jumbo_pm_reg, port_mask);
488 	return b53_write16(dev, B53_JUMBO_PAGE, dev->jumbo_size_reg, max_size);
489 }
490 
491 static int b53_flush_arl(struct b53_device *dev, u8 mask)
492 {
493 	unsigned int i;
494 
495 	if (is5325(dev))
496 		return 0;
497 
498 	b53_write8(dev, B53_CTRL_PAGE, B53_FAST_AGE_CTRL,
499 		   FAST_AGE_DONE | FAST_AGE_DYNAMIC | mask);
500 
501 	for (i = 0; i < 10; i++) {
502 		u8 fast_age_ctrl;
503 
504 		b53_read8(dev, B53_CTRL_PAGE, B53_FAST_AGE_CTRL,
505 			  &fast_age_ctrl);
506 
507 		if (!(fast_age_ctrl & FAST_AGE_DONE))
508 			goto out;
509 
510 		msleep(1);
511 	}
512 
513 	return -ETIMEDOUT;
514 out:
515 	/* Only age dynamic entries (default behavior) */
516 	b53_write8(dev, B53_CTRL_PAGE, B53_FAST_AGE_CTRL, FAST_AGE_DYNAMIC);
517 	return 0;
518 }
519 
520 static int b53_fast_age_port(struct b53_device *dev, int port)
521 {
522 	if (is5325(dev))
523 		return 0;
524 
525 	b53_write8(dev, B53_CTRL_PAGE, B53_FAST_AGE_PORT_CTRL, port);
526 
527 	return b53_flush_arl(dev, FAST_AGE_PORT);
528 }
529 
530 static int b53_fast_age_vlan(struct b53_device *dev, u16 vid)
531 {
532 	if (is5325(dev))
533 		return 0;
534 
535 	b53_write16(dev, B53_CTRL_PAGE, B53_FAST_AGE_VID_CTRL, vid);
536 
537 	return b53_flush_arl(dev, FAST_AGE_VLAN);
538 }
539 
540 void b53_imp_vlan_setup(struct dsa_switch *ds, int cpu_port)
541 {
542 	struct b53_device *dev = ds->priv;
543 	unsigned int i;
544 	u16 pvlan;
545 
546 	/* BCM5325 CPU port is at 8 */
547 	if ((is5325(dev) || is5365(dev)) && cpu_port == B53_CPU_PORT_25)
548 		cpu_port = B53_CPU_PORT;
549 
550 	/* Enable the IMP port to be in the same VLAN as the other ports
551 	 * on a per-port basis such that we only have Port i and IMP in
552 	 * the same VLAN.
553 	 */
554 	b53_for_each_port(dev, i) {
555 		b53_read16(dev, B53_PVLAN_PAGE, B53_PVLAN_PORT_MASK(i), &pvlan);
556 		pvlan |= BIT(cpu_port);
557 		b53_write16(dev, B53_PVLAN_PAGE, B53_PVLAN_PORT_MASK(i), pvlan);
558 	}
559 }
560 EXPORT_SYMBOL(b53_imp_vlan_setup);
561 
562 static void b53_port_set_ucast_flood(struct b53_device *dev, int port,
563 				     bool unicast)
564 {
565 	u16 uc;
566 
567 	if (is5325(dev)) {
568 		if (port == B53_CPU_PORT_25)
569 			port = B53_CPU_PORT;
570 
571 		b53_read16(dev, B53_IEEE_PAGE, B53_IEEE_UCAST_DLF, &uc);
572 		if (unicast)
573 			uc |= BIT(port) | B53_IEEE_UCAST_DROP_EN;
574 		else
575 			uc &= ~BIT(port);
576 		b53_write16(dev, B53_IEEE_PAGE, B53_IEEE_UCAST_DLF, uc);
577 	} else {
578 		b53_read16(dev, B53_CTRL_PAGE, B53_UC_FLOOD_MASK, &uc);
579 		if (unicast)
580 			uc |= BIT(port);
581 		else
582 			uc &= ~BIT(port);
583 		b53_write16(dev, B53_CTRL_PAGE, B53_UC_FLOOD_MASK, uc);
584 	}
585 }
586 
587 static void b53_port_set_mcast_flood(struct b53_device *dev, int port,
588 				     bool multicast)
589 {
590 	u16 mc;
591 
592 	if (is5325(dev)) {
593 		if (port == B53_CPU_PORT_25)
594 			port = B53_CPU_PORT;
595 
596 		b53_read16(dev, B53_IEEE_PAGE, B53_IEEE_MCAST_DLF, &mc);
597 		if (multicast)
598 			mc |= BIT(port) | B53_IEEE_MCAST_DROP_EN;
599 		else
600 			mc &= ~BIT(port);
601 		b53_write16(dev, B53_IEEE_PAGE, B53_IEEE_MCAST_DLF, mc);
602 	} else {
603 		b53_read16(dev, B53_CTRL_PAGE, B53_MC_FLOOD_MASK, &mc);
604 		if (multicast)
605 			mc |= BIT(port);
606 		else
607 			mc &= ~BIT(port);
608 		b53_write16(dev, B53_CTRL_PAGE, B53_MC_FLOOD_MASK, mc);
609 
610 		b53_read16(dev, B53_CTRL_PAGE, B53_IPMC_FLOOD_MASK, &mc);
611 		if (multicast)
612 			mc |= BIT(port);
613 		else
614 			mc &= ~BIT(port);
615 		b53_write16(dev, B53_CTRL_PAGE, B53_IPMC_FLOOD_MASK, mc);
616 	}
617 }
618 
619 static void b53_port_set_learning(struct b53_device *dev, int port,
620 				  bool learning)
621 {
622 	u16 reg;
623 
624 	if (is5325(dev))
625 		return;
626 
627 	b53_read16(dev, B53_CTRL_PAGE, B53_DIS_LEARNING, &reg);
628 	if (learning)
629 		reg &= ~BIT(port);
630 	else
631 		reg |= BIT(port);
632 	b53_write16(dev, B53_CTRL_PAGE, B53_DIS_LEARNING, reg);
633 }
634 
635 static void b53_port_set_isolated(struct b53_device *dev, int port,
636 				  bool isolated)
637 {
638 	u8 offset;
639 	u16 reg;
640 
641 	if (is5325(dev))
642 		offset = B53_PROTECTED_PORT_SEL_25;
643 	else
644 		offset = B53_PROTECTED_PORT_SEL;
645 
646 	b53_read16(dev, B53_CTRL_PAGE, offset, &reg);
647 	if (isolated)
648 		reg |= BIT(port);
649 	else
650 		reg &= ~BIT(port);
651 	b53_write16(dev, B53_CTRL_PAGE, offset, reg);
652 }
653 
654 static void b53_eee_enable_set(struct dsa_switch *ds, int port, bool enable)
655 {
656 	struct b53_device *dev = ds->priv;
657 	u16 reg;
658 
659 	b53_read16(dev, B53_EEE_PAGE, B53_EEE_EN_CTRL, &reg);
660 	if (enable)
661 		reg |= BIT(port);
662 	else
663 		reg &= ~BIT(port);
664 	b53_write16(dev, B53_EEE_PAGE, B53_EEE_EN_CTRL, reg);
665 }
666 
667 int b53_setup_port(struct dsa_switch *ds, int port)
668 {
669 	struct b53_device *dev = ds->priv;
670 
671 	b53_port_set_ucast_flood(dev, port, true);
672 	b53_port_set_mcast_flood(dev, port, true);
673 	b53_port_set_learning(dev, port, false);
674 	b53_port_set_isolated(dev, port, false);
675 
676 	/* Force all traffic to go to the CPU port to prevent the ASIC from
677 	 * trying to forward to bridged ports on matching FDB entries, then
678 	 * dropping frames because it isn't allowed to forward there.
679 	 */
680 	if (dsa_is_user_port(ds, port))
681 		b53_set_eap_mode(dev, port, EAP_MODE_SIMPLIFIED);
682 
683 	if (is5325(dev) &&
684 	    in_range(port, 1, 4)) {
685 		u8 reg;
686 
687 		b53_read8(dev, B53_CTRL_PAGE, B53_PD_MODE_CTRL_25, &reg);
688 		reg &= ~PD_MODE_POWER_DOWN_PORT(0);
689 		if (dsa_is_unused_port(ds, port))
690 			reg |= PD_MODE_POWER_DOWN_PORT(port);
691 		else
692 			reg &= ~PD_MODE_POWER_DOWN_PORT(port);
693 		b53_write8(dev, B53_CTRL_PAGE, B53_PD_MODE_CTRL_25, reg);
694 	}
695 
696 	return 0;
697 }
698 EXPORT_SYMBOL(b53_setup_port);
699 
700 int b53_enable_port(struct dsa_switch *ds, int port, struct phy_device *phy)
701 {
702 	struct b53_device *dev = ds->priv;
703 	unsigned int cpu_port;
704 	int ret = 0;
705 	u16 pvlan;
706 
707 	if (!dsa_is_user_port(ds, port))
708 		return 0;
709 
710 	cpu_port = dsa_to_port(ds, port)->cpu_dp->index;
711 
712 	if (dev->ops->phy_enable)
713 		dev->ops->phy_enable(dev, port);
714 
715 	if (dev->ops->irq_enable)
716 		ret = dev->ops->irq_enable(dev, port);
717 	if (ret)
718 		return ret;
719 
720 	/* Clear the Rx and Tx disable bits and set to no spanning tree */
721 	b53_write8(dev, B53_CTRL_PAGE, B53_PORT_CTRL(port), 0);
722 
723 	/* Set this port, and only this one to be in the default VLAN,
724 	 * if member of a bridge, restore its membership prior to
725 	 * bringing down this port.
726 	 */
727 	b53_read16(dev, B53_PVLAN_PAGE, B53_PVLAN_PORT_MASK(port), &pvlan);
728 	pvlan &= ~0x1ff;
729 	pvlan |= BIT(port);
730 	pvlan |= dev->ports[port].vlan_ctl_mask;
731 	b53_write16(dev, B53_PVLAN_PAGE, B53_PVLAN_PORT_MASK(port), pvlan);
732 
733 	b53_imp_vlan_setup(ds, cpu_port);
734 
735 	/* If EEE was enabled, restore it */
736 	if (dev->ports[port].eee.eee_enabled)
737 		b53_eee_enable_set(ds, port, true);
738 
739 	return 0;
740 }
741 EXPORT_SYMBOL(b53_enable_port);
742 
743 void b53_disable_port(struct dsa_switch *ds, int port)
744 {
745 	struct b53_device *dev = ds->priv;
746 	u8 reg;
747 
748 	/* Disable Tx/Rx for the port */
749 	b53_read8(dev, B53_CTRL_PAGE, B53_PORT_CTRL(port), &reg);
750 	reg |= PORT_CTRL_RX_DISABLE | PORT_CTRL_TX_DISABLE;
751 	b53_write8(dev, B53_CTRL_PAGE, B53_PORT_CTRL(port), reg);
752 
753 	if (dev->ops->phy_disable)
754 		dev->ops->phy_disable(dev, port);
755 
756 	if (dev->ops->irq_disable)
757 		dev->ops->irq_disable(dev, port);
758 }
759 EXPORT_SYMBOL(b53_disable_port);
760 
761 void b53_brcm_hdr_setup(struct dsa_switch *ds, int port)
762 {
763 	struct b53_device *dev = ds->priv;
764 	bool tag_en = !(dev->tag_protocol == DSA_TAG_PROTO_NONE);
765 	u8 hdr_ctl, val;
766 	u16 reg;
767 
768 	/* Resolve which bit controls the Broadcom tag */
769 	switch (port) {
770 	case 8:
771 		val = BRCM_HDR_P8_EN;
772 		break;
773 	case 7:
774 		val = BRCM_HDR_P7_EN;
775 		break;
776 	case 5:
777 		val = BRCM_HDR_P5_EN;
778 		break;
779 	default:
780 		val = 0;
781 		break;
782 	}
783 
784 	/* Enable management mode if tagging is requested */
785 	b53_read8(dev, B53_CTRL_PAGE, B53_SWITCH_MODE, &hdr_ctl);
786 	if (tag_en)
787 		hdr_ctl |= SM_SW_FWD_MODE;
788 	else
789 		hdr_ctl &= ~SM_SW_FWD_MODE;
790 	b53_write8(dev, B53_CTRL_PAGE, B53_SWITCH_MODE, hdr_ctl);
791 
792 	/* Configure the appropriate IMP port */
793 	b53_read8(dev, B53_MGMT_PAGE, B53_GLOBAL_CONFIG, &hdr_ctl);
794 	if (port == 8)
795 		hdr_ctl |= GC_FRM_MGMT_PORT_MII;
796 	else if (port == 5)
797 		hdr_ctl |= GC_FRM_MGMT_PORT_M;
798 	b53_write8(dev, B53_MGMT_PAGE, B53_GLOBAL_CONFIG, hdr_ctl);
799 
800 	/* B53_BRCM_HDR not present on devices with legacy tags */
801 	if (dev->tag_protocol == DSA_TAG_PROTO_BRCM_LEGACY ||
802 	    dev->tag_protocol == DSA_TAG_PROTO_BRCM_LEGACY_FCS)
803 		return;
804 
805 	/* Enable Broadcom tags for IMP port */
806 	b53_read8(dev, B53_MGMT_PAGE, B53_BRCM_HDR, &hdr_ctl);
807 	if (tag_en)
808 		hdr_ctl |= val;
809 	else
810 		hdr_ctl &= ~val;
811 	b53_write8(dev, B53_MGMT_PAGE, B53_BRCM_HDR, hdr_ctl);
812 
813 	/* Registers below are only accessible on newer devices */
814 	if (!is58xx(dev))
815 		return;
816 
817 	/* Enable reception Broadcom tag for CPU TX (switch RX) to
818 	 * allow us to tag outgoing frames
819 	 */
820 	b53_read16(dev, B53_MGMT_PAGE, B53_BRCM_HDR_RX_DIS, &reg);
821 	if (tag_en)
822 		reg &= ~BIT(port);
823 	else
824 		reg |= BIT(port);
825 	b53_write16(dev, B53_MGMT_PAGE, B53_BRCM_HDR_RX_DIS, reg);
826 
827 	/* Enable transmission of Broadcom tags from the switch (CPU RX) to
828 	 * allow delivering frames to the per-port net_devices
829 	 */
830 	b53_read16(dev, B53_MGMT_PAGE, B53_BRCM_HDR_TX_DIS, &reg);
831 	if (tag_en)
832 		reg &= ~BIT(port);
833 	else
834 		reg |= BIT(port);
835 	b53_write16(dev, B53_MGMT_PAGE, B53_BRCM_HDR_TX_DIS, reg);
836 }
837 EXPORT_SYMBOL(b53_brcm_hdr_setup);
838 
839 int b53_enable_mcast_snooping(struct b53_device *dev)
840 {
841 	u16 val16;
842 	u32 val;
843 	int ret;
844 
845 	/* Older switches use different IGMP controls, not HL_PRTC_CTRL. */
846 	if (is5325(dev) || is5365(dev) || dev->chip_id == BCM5389_DEVICE_ID ||
847 	    is5397_98(dev) || is63xx(dev))
848 		return 0;
849 
850 	/* IGMPv2 and MLDv1 reports use the group's destination MAC. Once
851 	 * the first listener is offloaded, ordinary multicast forwarding
852 	 * can keep subsequent reports from reaching the bridge CPU.
853 	 */
854 
855 	/* BCM5395 has a narrower register and a single IGMP classifier.
856 	 * Its ICMPv6 classifier also covers MLD.
857 	 */
858 	if (dev->chip_id == BCM5395_DEVICE_ID) {
859 		ret = b53_read16(dev, B53_MGMT_PAGE, B53_HL_PRTC_CTRL, &val16);
860 		if (ret)
861 			return ret;
862 
863 		val16 |= HL_PRTC_IGMP_IP_EN_5395 | HL_PRTC_ICMPV6_EN;
864 		/* Zero forwarding modes copy control traffic to the CPU
865 		 * independently of the ARL lookup, retaining L2 forwarding.
866 		 */
867 		val16 &= ~(HL_PRTC_IGMP_FWD_MODE_5395 |
868 			   HL_PRTC_ICMPV6_FWD_MODE);
869 
870 		return b53_write16(dev, B53_MGMT_PAGE, B53_HL_PRTC_CTRL,
871 				   val16);
872 	}
873 
874 	ret = b53_read32(dev, B53_MGMT_PAGE, B53_HL_PRTC_CTRL, &val);
875 	if (ret)
876 		return ret;
877 
878 	val |= HL_PRTC_IGMP_RPTLVE_EN | HL_PRTC_IGMP_QRY_EN |
879 	       HL_PRTC_IGMP_UKN_EN | HL_PRTC_MLD_RPTDONE_EN |
880 	       HL_PRTC_MLD_QRY_EN;
881 	/* Zero forwarding modes copy control traffic to the CPU
882 	 * independently of the ARL lookup, retaining L2 forwarding.
883 	 */
884 	val &= ~(HL_PRTC_IGMP_RPTLVE_FWD_MODE | HL_PRTC_IGMP_QRY_FWD_MODE |
885 		 HL_PRTC_IGMP_UKN_FWD_MODE | HL_PRTC_MLD_RPTDONE_FWD_MODE |
886 		 HL_PRTC_MLD_QRY_FWD_MODE);
887 
888 	return b53_write32(dev, B53_MGMT_PAGE, B53_HL_PRTC_CTRL, val);
889 }
890 EXPORT_SYMBOL(b53_enable_mcast_snooping);
891 
892 static void b53_enable_cpu_port(struct b53_device *dev, int port)
893 {
894 	u8 port_ctrl;
895 
896 	/* BCM5325 CPU port is at 8 */
897 	if ((is5325(dev) || is5365(dev)) && port == B53_CPU_PORT_25)
898 		port = B53_CPU_PORT;
899 
900 	port_ctrl = PORT_CTRL_RX_BCST_EN |
901 		    PORT_CTRL_RX_MCST_EN |
902 		    PORT_CTRL_RX_UCST_EN;
903 	b53_write8(dev, B53_CTRL_PAGE, B53_PORT_CTRL(port), port_ctrl);
904 
905 	b53_brcm_hdr_setup(dev->ds, port);
906 }
907 
908 static void b53_enable_mib(struct b53_device *dev)
909 {
910 	u8 gc;
911 
912 	b53_read8(dev, B53_MGMT_PAGE, B53_GLOBAL_CONFIG, &gc);
913 	gc &= ~(GC_RESET_MIB | GC_MIB_AC_EN);
914 	b53_write8(dev, B53_MGMT_PAGE, B53_GLOBAL_CONFIG, gc);
915 }
916 
917 static void b53_enable_stp(struct b53_device *dev)
918 {
919 	u8 gc;
920 
921 	b53_read8(dev, B53_MGMT_PAGE, B53_GLOBAL_CONFIG, &gc);
922 	gc |= GC_RX_BPDU_EN;
923 	b53_write8(dev, B53_MGMT_PAGE, B53_GLOBAL_CONFIG, gc);
924 }
925 
926 static u16 b53_default_pvid(struct b53_device *dev)
927 {
928 	return 0;
929 }
930 
931 static bool b53_vlan_port_needs_forced_tagged(struct dsa_switch *ds, int port)
932 {
933 	struct b53_device *dev = ds->priv;
934 
935 	return dev->tag_protocol == DSA_TAG_PROTO_NONE && dsa_is_cpu_port(ds, port);
936 }
937 
938 static bool b53_vlan_port_may_join_untagged(struct dsa_switch *ds, int port)
939 {
940 	struct b53_device *dev = ds->priv;
941 	struct dsa_port *dp;
942 
943 	if (!dev->vlan_filtering)
944 		return true;
945 
946 	dp = dsa_to_port(ds, port);
947 
948 	if (dsa_port_is_cpu(dp))
949 		return true;
950 
951 	return dp->bridge == NULL;
952 }
953 
954 int b53_configure_vlan(struct dsa_switch *ds)
955 {
956 	struct b53_device *dev = ds->priv;
957 	struct b53_vlan vl = { 0 };
958 	struct b53_vlan *v;
959 	int i, def_vid;
960 	u16 vid;
961 
962 	def_vid = b53_default_pvid(dev);
963 
964 	/* clear all vlan entries */
965 	if (is5325(dev) || is5365(dev)) {
966 		for (i = def_vid; i < dev->num_vlans; i++)
967 			b53_set_vlan_entry(dev, i, &vl);
968 	} else {
969 		b53_do_vlan_op(dev, VTA_CMD_CLEAR);
970 	}
971 
972 	b53_enable_vlan(dev, -1, dev->vlan_enabled, dev->vlan_filtering);
973 
974 	/* Create an untagged VLAN entry for the default PVID in case
975 	 * CONFIG_VLAN_8021Q is disabled and there are no calls to
976 	 * dsa_user_vlan_rx_add_vid() to create the default VLAN
977 	 * entry. Do this only when the tagging protocol is not
978 	 * DSA_TAG_PROTO_NONE
979 	 */
980 	v = &dev->vlans[def_vid];
981 	b53_for_each_port(dev, i) {
982 		if (!b53_vlan_port_may_join_untagged(ds, i))
983 			continue;
984 
985 		vl.members |= BIT(i);
986 		if (!b53_vlan_port_needs_forced_tagged(ds, i))
987 			vl.untag = vl.members;
988 		b53_write16(dev, B53_VLAN_PAGE, B53_VLAN_PORT_DEF_TAG(i),
989 			    def_vid);
990 	}
991 	b53_set_vlan_entry(dev, def_vid, &vl);
992 
993 	if (dev->vlan_filtering) {
994 		/* Upon initial call we have not set-up any VLANs, but upon
995 		 * system resume, we need to restore all VLAN entries.
996 		 */
997 		for (vid = def_vid + 1; vid < dev->num_vlans; vid++) {
998 			v = &dev->vlans[vid];
999 
1000 			if (!v->members)
1001 				continue;
1002 
1003 			b53_set_vlan_entry(dev, vid, v);
1004 			b53_fast_age_vlan(dev, vid);
1005 		}
1006 
1007 		b53_for_each_port(dev, i) {
1008 			if (!dsa_is_cpu_port(ds, i))
1009 				b53_write16(dev, B53_VLAN_PAGE,
1010 					    B53_VLAN_PORT_DEF_TAG(i),
1011 					    dev->ports[i].pvid);
1012 		}
1013 	}
1014 
1015 	return 0;
1016 }
1017 EXPORT_SYMBOL(b53_configure_vlan);
1018 
1019 static void b53_switch_reset_gpio(struct b53_device *dev)
1020 {
1021 	struct gpio_desc *gpio = dev->reset_gpio;
1022 
1023 	if (IS_ERR(gpio))
1024 		return;
1025 
1026 	/* Reset sequence: RESET low(50ms)->high(20ms)
1027 	 */
1028 	gpiod_set_value(gpio, 0);
1029 	mdelay(50);
1030 
1031 	gpiod_set_value(gpio, 1);
1032 	mdelay(20);
1033 
1034 	dev->current_page = 0xff;
1035 }
1036 
1037 static int b53_switch_reset(struct b53_device *dev)
1038 {
1039 	unsigned int timeout = 1000;
1040 	u8 mgmt, reg;
1041 
1042 	b53_switch_reset_gpio(dev);
1043 
1044 	if (is539x(dev)) {
1045 		b53_write8(dev, B53_CTRL_PAGE, B53_SOFTRESET, 0x83);
1046 		b53_write8(dev, B53_CTRL_PAGE, B53_SOFTRESET, 0x00);
1047 	}
1048 
1049 	/* This is specific to 58xx devices here, do not use is58xx() which
1050 	 * covers the larger Starfigther 2 family, including 7445/7278 which
1051 	 * still use this driver as a library and need to perform the reset
1052 	 * earlier.
1053 	 */
1054 	if (dev->chip_id == BCM58XX_DEVICE_ID ||
1055 	    dev->chip_id == BCM583XX_DEVICE_ID) {
1056 		b53_read8(dev, B53_CTRL_PAGE, B53_SOFTRESET, &reg);
1057 		reg |= SW_RST | EN_SW_RST | EN_CH_RST;
1058 		b53_write8(dev, B53_CTRL_PAGE, B53_SOFTRESET, reg);
1059 
1060 		do {
1061 			b53_read8(dev, B53_CTRL_PAGE, B53_SOFTRESET, &reg);
1062 			if (!(reg & SW_RST))
1063 				break;
1064 
1065 			usleep_range(1000, 2000);
1066 		} while (timeout-- > 0);
1067 
1068 		if (timeout == 0) {
1069 			dev_err(dev->dev,
1070 				"Timeout waiting for SW_RST to clear!\n");
1071 			return -ETIMEDOUT;
1072 		}
1073 	}
1074 
1075 	b53_read8(dev, B53_CTRL_PAGE, B53_SWITCH_MODE, &mgmt);
1076 
1077 	if (!(mgmt & SM_SW_FWD_EN)) {
1078 		mgmt &= ~SM_SW_FWD_MODE;
1079 		mgmt |= SM_SW_FWD_EN;
1080 
1081 		b53_write8(dev, B53_CTRL_PAGE, B53_SWITCH_MODE, mgmt);
1082 		b53_read8(dev, B53_CTRL_PAGE, B53_SWITCH_MODE, &mgmt);
1083 
1084 		if (!(mgmt & SM_SW_FWD_EN)) {
1085 			dev_err(dev->dev, "Failed to enable switch!\n");
1086 			return -EINVAL;
1087 		}
1088 	}
1089 
1090 	b53_enable_mib(dev);
1091 	b53_enable_stp(dev);
1092 
1093 	return b53_flush_arl(dev, FAST_AGE_STATIC);
1094 }
1095 
1096 static int b53_phy_read16(struct dsa_switch *ds, int addr, int reg)
1097 {
1098 	struct b53_device *priv = ds->priv;
1099 	u16 value = 0;
1100 	int ret;
1101 
1102 	if (priv->ops->phy_read16)
1103 		ret = priv->ops->phy_read16(priv, addr, reg, &value);
1104 	else
1105 		ret = b53_read16(priv, B53_PORT_MII_PAGE(addr),
1106 				 reg * 2, &value);
1107 
1108 	return ret ? ret : value;
1109 }
1110 
1111 static int b53_phy_write16(struct dsa_switch *ds, int addr, int reg, u16 val)
1112 {
1113 	struct b53_device *priv = ds->priv;
1114 
1115 	if (priv->ops->phy_write16)
1116 		return priv->ops->phy_write16(priv, addr, reg, val);
1117 
1118 	return b53_write16(priv, B53_PORT_MII_PAGE(addr), reg * 2, val);
1119 }
1120 
1121 static int b53_reset_switch(struct b53_device *priv)
1122 {
1123 	/* reset vlans */
1124 	memset(priv->vlans, 0, sizeof(*priv->vlans) * priv->num_vlans);
1125 	memset(priv->ports, 0, sizeof(*priv->ports) * priv->num_ports);
1126 
1127 	priv->serdes_lane = B53_INVALID_LANE;
1128 
1129 	return b53_switch_reset(priv);
1130 }
1131 
1132 static int b53_apply_config(struct b53_device *priv)
1133 {
1134 	/* disable switching */
1135 	b53_set_forwarding(priv, 0);
1136 
1137 	b53_configure_vlan(priv->ds);
1138 
1139 	/* enable switching */
1140 	b53_set_forwarding(priv, 1);
1141 
1142 	return 0;
1143 }
1144 
1145 static void b53_reset_mib(struct b53_device *priv)
1146 {
1147 	u8 gc;
1148 
1149 	b53_read8(priv, B53_MGMT_PAGE, B53_GLOBAL_CONFIG, &gc);
1150 
1151 	b53_write8(priv, B53_MGMT_PAGE, B53_GLOBAL_CONFIG, gc | GC_RESET_MIB);
1152 	msleep(1);
1153 	b53_write8(priv, B53_MGMT_PAGE, B53_GLOBAL_CONFIG, gc & ~GC_RESET_MIB);
1154 	msleep(1);
1155 }
1156 
1157 static const struct b53_mib_desc *b53_get_mib(struct b53_device *dev)
1158 {
1159 	if (is5365(dev))
1160 		return b53_mibs_65;
1161 	else if (is63xx(dev))
1162 		return b53_mibs_63xx;
1163 	else if (is58xx(dev))
1164 		return b53_mibs_58xx;
1165 	else
1166 		return b53_mibs;
1167 }
1168 
1169 static unsigned int b53_get_mib_size(struct b53_device *dev)
1170 {
1171 	if (is5365(dev))
1172 		return B53_MIBS_65_SIZE;
1173 	else if (is63xx(dev))
1174 		return B53_MIBS_63XX_SIZE;
1175 	else if (is58xx(dev))
1176 		return B53_MIBS_58XX_SIZE;
1177 	else
1178 		return B53_MIBS_SIZE;
1179 }
1180 
1181 static struct phy_device *b53_get_phy_device(struct dsa_switch *ds, int port)
1182 {
1183 	/* These ports typically do not have built-in PHYs */
1184 	switch (port) {
1185 	case B53_CPU_PORT_25:
1186 	case 7:
1187 	case B53_CPU_PORT:
1188 		return NULL;
1189 	}
1190 
1191 	return mdiobus_get_phy(ds->user_mii_bus, port);
1192 }
1193 
1194 void b53_get_strings(struct dsa_switch *ds, int port, u32 stringset,
1195 		     uint8_t *data)
1196 {
1197 	struct b53_device *dev = ds->priv;
1198 	const struct b53_mib_desc *mibs = b53_get_mib(dev);
1199 	unsigned int mib_size = b53_get_mib_size(dev);
1200 	struct phy_device *phydev;
1201 	unsigned int i;
1202 
1203 	if (stringset == ETH_SS_STATS) {
1204 		for (i = 0; i < mib_size; i++)
1205 			ethtool_puts(&data, mibs[i].name);
1206 	} else if (stringset == ETH_SS_PHY_STATS) {
1207 		phydev = b53_get_phy_device(ds, port);
1208 		if (!phydev)
1209 			return;
1210 
1211 		phy_ethtool_get_strings(phydev, data);
1212 	}
1213 }
1214 EXPORT_SYMBOL(b53_get_strings);
1215 
1216 void b53_get_ethtool_stats(struct dsa_switch *ds, int port, uint64_t *data)
1217 {
1218 	struct b53_device *dev = ds->priv;
1219 	const struct b53_mib_desc *mibs = b53_get_mib(dev);
1220 	unsigned int mib_size = b53_get_mib_size(dev);
1221 	const struct b53_mib_desc *s;
1222 	unsigned int i;
1223 	u64 val = 0;
1224 
1225 	if (is5365(dev) && port == 5)
1226 		port = 8;
1227 
1228 	mutex_lock(&dev->stats_mutex);
1229 
1230 	for (i = 0; i < mib_size; i++) {
1231 		s = &mibs[i];
1232 
1233 		if (s->size == 8) {
1234 			b53_read64(dev, B53_MIB_PAGE(port), s->offset, &val);
1235 		} else {
1236 			u32 val32;
1237 
1238 			b53_read32(dev, B53_MIB_PAGE(port), s->offset,
1239 				   &val32);
1240 			val = val32;
1241 		}
1242 		data[i] = (u64)val;
1243 	}
1244 
1245 	mutex_unlock(&dev->stats_mutex);
1246 }
1247 EXPORT_SYMBOL(b53_get_ethtool_stats);
1248 
1249 void b53_get_ethtool_phy_stats(struct dsa_switch *ds, int port, uint64_t *data)
1250 {
1251 	struct phy_device *phydev;
1252 
1253 	phydev = b53_get_phy_device(ds, port);
1254 	if (!phydev)
1255 		return;
1256 
1257 	phy_ethtool_get_stats(phydev, NULL, data);
1258 }
1259 EXPORT_SYMBOL(b53_get_ethtool_phy_stats);
1260 
1261 int b53_get_sset_count(struct dsa_switch *ds, int port, int sset)
1262 {
1263 	struct b53_device *dev = ds->priv;
1264 	struct phy_device *phydev;
1265 
1266 	if (sset == ETH_SS_STATS) {
1267 		return b53_get_mib_size(dev);
1268 	} else if (sset == ETH_SS_PHY_STATS) {
1269 		phydev = b53_get_phy_device(ds, port);
1270 		if (!phydev)
1271 			return 0;
1272 
1273 		return phy_ethtool_get_sset_count(phydev);
1274 	}
1275 
1276 	return 0;
1277 }
1278 EXPORT_SYMBOL(b53_get_sset_count);
1279 
1280 enum b53_devlink_resource_id {
1281 	B53_DEVLINK_PARAM_ID_NONE,  /* DEVLINK_RESOURCE_ID_PARENT_TOP */
1282 	B53_DEVLINK_PARAM_ID_VLAN_TABLE,
1283 };
1284 
1285 static u64 b53_devlink_vlan_table_get(void *priv)
1286 {
1287 	struct b53_device *dev = priv;
1288 	struct b53_vlan *vl;
1289 	unsigned int i;
1290 	u64 count = 0;
1291 
1292 	for (i = 0; i < dev->num_vlans; i++) {
1293 		vl = &dev->vlans[i];
1294 		if (vl->members)
1295 			count++;
1296 	}
1297 
1298 	return count;
1299 }
1300 
1301 int b53_setup_devlink_resources(struct dsa_switch *ds)
1302 {
1303 	struct devlink_resource_size_params size_params;
1304 	struct b53_device *dev = ds->priv;
1305 	int err;
1306 
1307 	devlink_resource_size_params_init(&size_params, dev->num_vlans,
1308 					  dev->num_vlans,
1309 					  1, DEVLINK_RESOURCE_UNIT_ENTRY);
1310 
1311 	err = dsa_devlink_resource_register(ds, "VLAN", dev->num_vlans,
1312 					    B53_DEVLINK_PARAM_ID_VLAN_TABLE,
1313 					    DEVLINK_RESOURCE_ID_PARENT_TOP,
1314 					    &size_params);
1315 	if (err)
1316 		goto out;
1317 
1318 	dsa_devlink_resource_occ_get_register(ds,
1319 					      B53_DEVLINK_PARAM_ID_VLAN_TABLE,
1320 					      b53_devlink_vlan_table_get, dev);
1321 
1322 	return 0;
1323 out:
1324 	dsa_devlink_resources_unregister(ds);
1325 	return err;
1326 }
1327 EXPORT_SYMBOL(b53_setup_devlink_resources);
1328 
1329 static int b53_setup(struct dsa_switch *ds)
1330 {
1331 	struct b53_device *dev = ds->priv;
1332 	struct b53_vlan *vl;
1333 	unsigned int port;
1334 	u16 pvid;
1335 	int ret;
1336 
1337 	/* Request bridge PVID untagged when DSA_TAG_PROTO_NONE is set
1338 	 * which forces the CPU port to be tagged in all VLANs.
1339 	 */
1340 	ds->untag_bridge_pvid = dev->tag_protocol == DSA_TAG_PROTO_NONE;
1341 
1342 	/* The switch does not tell us the original VLAN for untagged
1343 	 * packets, so keep the CPU port always tagged.
1344 	 */
1345 	ds->untag_vlan_aware_bridge_pvid = true;
1346 
1347 	if (dev->chip_id == BCM53101_DEVICE_ID) {
1348 		/* BCM53101 uses 0.5 second increments */
1349 		ds->ageing_time_min = 1 * 500;
1350 		ds->ageing_time_max = AGE_TIME_MAX * 500;
1351 	} else {
1352 		/* Everything else uses 1 second increments */
1353 		ds->ageing_time_min = 1 * 1000;
1354 		ds->ageing_time_max = AGE_TIME_MAX * 1000;
1355 	}
1356 
1357 	ret = b53_reset_switch(dev);
1358 	if (ret) {
1359 		dev_err(ds->dev, "failed to reset switch\n");
1360 		return ret;
1361 	}
1362 
1363 	ret = b53_mirror_init(ds);
1364 	if (ret)
1365 		return ret;
1366 
1367 	/* setup default vlan for filtering mode */
1368 	pvid = b53_default_pvid(dev);
1369 	vl = &dev->vlans[pvid];
1370 	b53_for_each_port(dev, port) {
1371 		vl->members |= BIT(port);
1372 		if (!b53_vlan_port_needs_forced_tagged(ds, port))
1373 			vl->untag |= BIT(port);
1374 	}
1375 
1376 	b53_reset_mib(dev);
1377 
1378 	ret = b53_apply_config(dev);
1379 	if (ret) {
1380 		dev_err(ds->dev, "failed to apply configuration\n");
1381 		return ret;
1382 	}
1383 
1384 	/* Configure IMP/CPU port, disable all other ports. Enabled
1385 	 * ports will be configured with .port_enable
1386 	 */
1387 	for (port = 0; port < dev->num_ports; port++) {
1388 		if (dsa_is_cpu_port(ds, port))
1389 			b53_enable_cpu_port(dev, port);
1390 		else
1391 			b53_disable_port(ds, port);
1392 	}
1393 
1394 	ret = b53_enable_mcast_snooping(dev);
1395 	if (ret)
1396 		return ret;
1397 
1398 	return b53_setup_devlink_resources(ds);
1399 }
1400 
1401 static void b53_teardown(struct dsa_switch *ds)
1402 {
1403 	dsa_devlink_resources_unregister(ds);
1404 }
1405 
1406 static void b53_force_link(struct b53_device *dev, int port, int link)
1407 {
1408 	u8 reg, val, off;
1409 
1410 	/* Override the port settings */
1411 	if (port == dev->imp_port) {
1412 		off = B53_PORT_OVERRIDE_CTRL;
1413 		val = PORT_OVERRIDE_EN;
1414 	} else if (is5325(dev)) {
1415 		return;
1416 	} else {
1417 		off = B53_GMII_PORT_OVERRIDE_CTRL(port);
1418 		val = GMII_PO_EN;
1419 	}
1420 
1421 	b53_read8(dev, B53_CTRL_PAGE, off, &reg);
1422 	reg |= val;
1423 	if (link)
1424 		reg |= PORT_OVERRIDE_LINK;
1425 	else
1426 		reg &= ~PORT_OVERRIDE_LINK;
1427 	b53_write8(dev, B53_CTRL_PAGE, off, reg);
1428 }
1429 
1430 static void b53_force_port_config(struct b53_device *dev, int port,
1431 				  int speed, int duplex,
1432 				  bool tx_pause, bool rx_pause)
1433 {
1434 	u8 reg, val, off, speed2000;
1435 
1436 	/* Override the port settings */
1437 	if (port == dev->imp_port) {
1438 		off = B53_PORT_OVERRIDE_CTRL;
1439 		val = PORT_OVERRIDE_EN;
1440 		speed2000 = PORT_OVERRIDE_SPEED_2000M;
1441 	} else if (is5325(dev)) {
1442 		return;
1443 	} else {
1444 		off = B53_GMII_PORT_OVERRIDE_CTRL(port);
1445 		val = GMII_PO_EN;
1446 		speed2000 = GMII_PO_SPEED_2000M;
1447 	}
1448 
1449 	b53_read8(dev, B53_CTRL_PAGE, off, &reg);
1450 	reg |= val;
1451 	if (duplex == DUPLEX_FULL)
1452 		reg |= PORT_OVERRIDE_FULL_DUPLEX;
1453 	else
1454 		reg &= ~PORT_OVERRIDE_FULL_DUPLEX;
1455 
1456 	reg &= ~(0x3 << GMII_PO_SPEED_S);
1457 	if (is5301x(dev) || is58xx(dev))
1458 		reg &= ~speed2000;
1459 
1460 	switch (speed) {
1461 	case 2000:
1462 		if (is5301x(dev) || is58xx(dev))
1463 			reg |= speed2000;
1464 		fallthrough;
1465 	case SPEED_1000:
1466 		reg |= PORT_OVERRIDE_SPEED_1000M;
1467 		break;
1468 	case SPEED_100:
1469 		reg |= PORT_OVERRIDE_SPEED_100M;
1470 		break;
1471 	case SPEED_10:
1472 		reg |= PORT_OVERRIDE_SPEED_10M;
1473 		break;
1474 	default:
1475 		dev_err(dev->dev, "unknown speed: %d\n", speed);
1476 		return;
1477 	}
1478 
1479 	if (is5325(dev))
1480 		reg &= ~PORT_OVERRIDE_LP_FLOW_25;
1481 	else
1482 		reg &= ~(PORT_OVERRIDE_RX_FLOW | PORT_OVERRIDE_TX_FLOW);
1483 
1484 	if (rx_pause) {
1485 		if (is5325(dev))
1486 			reg |= PORT_OVERRIDE_LP_FLOW_25;
1487 		else
1488 			reg |= PORT_OVERRIDE_RX_FLOW;
1489 	}
1490 
1491 	if (tx_pause) {
1492 		if (is5325(dev))
1493 			reg |= PORT_OVERRIDE_LP_FLOW_25;
1494 		else
1495 			reg |= PORT_OVERRIDE_TX_FLOW;
1496 	}
1497 
1498 	b53_write8(dev, B53_CTRL_PAGE, off, reg);
1499 }
1500 
1501 static void b53_adjust_63xx_rgmii(struct dsa_switch *ds, int port,
1502 				  phy_interface_t interface)
1503 {
1504 	struct b53_device *dev = ds->priv;
1505 	u8 rgmii_ctrl = 0;
1506 
1507 	b53_read8(dev, B53_CTRL_PAGE, B53_RGMII_CTRL_P(port), &rgmii_ctrl);
1508 	rgmii_ctrl &= ~(RGMII_CTRL_DLL_RXC | RGMII_CTRL_DLL_TXC);
1509 
1510 	if (is6318_268(dev))
1511 		rgmii_ctrl |= RGMII_CTRL_MII_OVERRIDE;
1512 
1513 	rgmii_ctrl |= RGMII_CTRL_ENABLE_GMII;
1514 
1515 	b53_write8(dev, B53_CTRL_PAGE, B53_RGMII_CTRL_P(port), rgmii_ctrl);
1516 
1517 	dev_dbg(ds->dev, "Configured port %d for %s\n", port,
1518 		phy_modes(interface));
1519 }
1520 
1521 static void b53_adjust_531x5_rgmii(struct dsa_switch *ds, int port,
1522 				   phy_interface_t interface)
1523 {
1524 	struct b53_device *dev = ds->priv;
1525 	u8 rgmii_ctrl = 0, off;
1526 
1527 	if (port == dev->imp_port)
1528 		off = B53_RGMII_CTRL_IMP;
1529 	else
1530 		off = B53_RGMII_CTRL_P(port);
1531 
1532 	/* Configure the port RGMII clock delay by DLL disabled and
1533 	 * tx_clk aligned timing (restoring to reset defaults)
1534 	 */
1535 	b53_read8(dev, B53_CTRL_PAGE, off, &rgmii_ctrl);
1536 	rgmii_ctrl &= ~(RGMII_CTRL_DLL_RXC | RGMII_CTRL_DLL_TXC);
1537 
1538 	/* PHY_INTERFACE_MODE_RGMII_TXID means TX internal delay, make
1539 	 * sure that we enable the port TX clock internal delay to
1540 	 * account for this internal delay that is inserted, otherwise
1541 	 * the switch won't be able to receive correctly.
1542 	 *
1543 	 * PHY_INTERFACE_MODE_RGMII means that we are not introducing
1544 	 * any delay neither on transmission nor reception, so the
1545 	 * BCM53125 must also be configured accordingly to account for
1546 	 * the lack of delay and introduce
1547 	 *
1548 	 * The BCM53125 switch has its RX clock and TX clock control
1549 	 * swapped, hence the reason why we modify the TX clock path in
1550 	 * the "RGMII" case
1551 	 */
1552 	if (interface == PHY_INTERFACE_MODE_RGMII_TXID)
1553 		rgmii_ctrl |= RGMII_CTRL_DLL_TXC;
1554 	if (interface == PHY_INTERFACE_MODE_RGMII)
1555 		rgmii_ctrl |= RGMII_CTRL_DLL_TXC | RGMII_CTRL_DLL_RXC;
1556 
1557 	if (dev->chip_id != BCM53115_DEVICE_ID)
1558 		rgmii_ctrl |= RGMII_CTRL_TIMING_SEL;
1559 
1560 	b53_write8(dev, B53_CTRL_PAGE, off, rgmii_ctrl);
1561 
1562 	dev_info(ds->dev, "Configured port %d for %s\n", port,
1563 		 phy_modes(interface));
1564 }
1565 
1566 static void b53_adjust_5325_mii(struct dsa_switch *ds, int port)
1567 {
1568 	struct b53_device *dev = ds->priv;
1569 	u8 reg = 0;
1570 
1571 	b53_read8(dev, B53_CTRL_PAGE, B53_PORT_OVERRIDE_CTRL,
1572 		  &reg);
1573 
1574 	/* reverse mii needs to be enabled */
1575 	if (!(reg & PORT_OVERRIDE_RV_MII_25)) {
1576 		b53_write8(dev, B53_CTRL_PAGE, B53_PORT_OVERRIDE_CTRL,
1577 			   reg | PORT_OVERRIDE_RV_MII_25);
1578 		b53_read8(dev, B53_CTRL_PAGE, B53_PORT_OVERRIDE_CTRL,
1579 			  &reg);
1580 
1581 		if (!(reg & PORT_OVERRIDE_RV_MII_25)) {
1582 			dev_err(ds->dev,
1583 				"Failed to enable reverse MII mode\n");
1584 			return;
1585 		}
1586 	}
1587 }
1588 
1589 void b53_port_event(struct dsa_switch *ds, int port)
1590 {
1591 	struct b53_device *dev = ds->priv;
1592 	bool link;
1593 	u16 sts;
1594 
1595 	b53_read16(dev, B53_STAT_PAGE, B53_LINK_STAT, &sts);
1596 	link = !!(sts & BIT(port));
1597 	dsa_port_phylink_mac_change(ds, port, link);
1598 }
1599 EXPORT_SYMBOL(b53_port_event);
1600 
1601 static void b53_phylink_get_caps(struct dsa_switch *ds, int port,
1602 				 struct phylink_config *config)
1603 {
1604 	struct b53_device *dev = ds->priv;
1605 
1606 	/* Internal ports need GMII for PHYLIB */
1607 	__set_bit(PHY_INTERFACE_MODE_GMII, config->supported_interfaces);
1608 
1609 	/* These switches appear to support MII and RevMII too, but beyond
1610 	 * this, the code gives very few clues. FIXME: We probably need more
1611 	 * interface modes here.
1612 	 *
1613 	 * According to b53_srab_mux_init(), ports 3..5 can support:
1614 	 *  SGMII, MII, GMII, RGMII or INTERNAL depending on the MUX setting.
1615 	 * However, the interface mode read from the MUX configuration is
1616 	 * not passed back to DSA, so phylink uses NA.
1617 	 * DT can specify RGMII for ports 0, 1.
1618 	 * For MDIO, port 8 can be RGMII_TXID.
1619 	 */
1620 	__set_bit(PHY_INTERFACE_MODE_MII, config->supported_interfaces);
1621 	__set_bit(PHY_INTERFACE_MODE_REVMII, config->supported_interfaces);
1622 
1623 	/* BCM63xx RGMII ports support RGMII */
1624 	if (is63xx(dev) && in_range(port, B53_63XX_RGMII0, 4))
1625 		phy_interface_set_rgmii(config->supported_interfaces);
1626 
1627 	config->mac_capabilities = MAC_ASYM_PAUSE | MAC_SYM_PAUSE |
1628 		MAC_10 | MAC_100;
1629 
1630 	/* 5325/5365 are not capable of gigabit speeds, everything else is.
1631 	 * Note: the original code also exclulded Gigagbit for MII, RevMII
1632 	 * and 802.3z modes. MII and RevMII are not able to work above 100M,
1633 	 * so will be excluded by the generic validator implementation.
1634 	 * However, the exclusion of Gigabit for 802.3z just seems wrong.
1635 	 */
1636 	if (!(is5325(dev) || is5365(dev)))
1637 		config->mac_capabilities |= MAC_1000;
1638 
1639 	/* Get the implementation specific capabilities */
1640 	if (dev->ops->phylink_get_caps)
1641 		dev->ops->phylink_get_caps(dev, port, config);
1642 }
1643 
1644 static struct phylink_pcs *b53_phylink_mac_select_pcs(struct phylink_config *config,
1645 						      phy_interface_t interface)
1646 {
1647 	struct dsa_port *dp = dsa_phylink_to_port(config);
1648 	struct b53_device *dev = dp->ds->priv;
1649 
1650 	if (!dev->ops->phylink_mac_select_pcs)
1651 		return NULL;
1652 
1653 	return dev->ops->phylink_mac_select_pcs(dev, dp->index, interface);
1654 }
1655 
1656 static void b53_phylink_mac_config(struct phylink_config *config,
1657 				   unsigned int mode,
1658 				   const struct phylink_link_state *state)
1659 {
1660 	struct dsa_port *dp = dsa_phylink_to_port(config);
1661 	phy_interface_t interface = state->interface;
1662 	struct dsa_switch *ds = dp->ds;
1663 	struct b53_device *dev = ds->priv;
1664 	int port = dp->index;
1665 
1666 	if (is63xx(dev) && in_range(port, B53_63XX_RGMII0, 4))
1667 		b53_adjust_63xx_rgmii(ds, port, interface);
1668 
1669 	if (mode == MLO_AN_FIXED) {
1670 		if (is531x5(dev) && phy_interface_mode_is_rgmii(interface))
1671 			b53_adjust_531x5_rgmii(ds, port, interface);
1672 
1673 		/* configure MII port if necessary */
1674 		if (is5325(dev))
1675 			b53_adjust_5325_mii(ds, port);
1676 	}
1677 }
1678 
1679 static void b53_phylink_mac_link_down(struct phylink_config *config,
1680 				      unsigned int mode,
1681 				      phy_interface_t interface)
1682 {
1683 	struct dsa_port *dp = dsa_phylink_to_port(config);
1684 	struct b53_device *dev = dp->ds->priv;
1685 	int port = dp->index;
1686 
1687 	if (mode == MLO_AN_PHY) {
1688 		if (is63xx(dev) && in_range(port, B53_63XX_RGMII0, 4))
1689 			b53_force_link(dev, port, false);
1690 		return;
1691 	}
1692 
1693 	if (mode == MLO_AN_FIXED) {
1694 		b53_force_link(dev, port, false);
1695 		return;
1696 	}
1697 
1698 	if (phy_interface_mode_is_8023z(interface) &&
1699 	    dev->ops->serdes_link_set)
1700 		dev->ops->serdes_link_set(dev, port, mode, interface, false);
1701 }
1702 
1703 static void b53_phylink_mac_link_up(struct phylink_config *config,
1704 				    struct phy_device *phydev,
1705 				    unsigned int mode,
1706 				    phy_interface_t interface,
1707 				    int speed, int duplex,
1708 				    bool tx_pause, bool rx_pause)
1709 {
1710 	struct dsa_port *dp = dsa_phylink_to_port(config);
1711 	struct dsa_switch *ds = dp->ds;
1712 	struct b53_device *dev = ds->priv;
1713 	struct ethtool_keee *p = &dev->ports[dp->index].eee;
1714 	int port = dp->index;
1715 
1716 	if (mode == MLO_AN_PHY) {
1717 		/* Re-negotiate EEE if it was enabled already */
1718 		p->eee_enabled = b53_eee_init(ds, port, phydev);
1719 
1720 		if (is63xx(dev) && in_range(port, B53_63XX_RGMII0, 4)) {
1721 			b53_force_port_config(dev, port, speed, duplex,
1722 					      tx_pause, rx_pause);
1723 			b53_force_link(dev, port, true);
1724 		}
1725 
1726 		return;
1727 	}
1728 
1729 	if (mode == MLO_AN_FIXED) {
1730 		/* Force flow control and 2Gbits/sec on BCM5301x and NSP CPU
1731 		 * ports
1732 		 */
1733 		if ((is5301x(dev) || dev->chip_id == BCM58XX_DEVICE_ID) &&
1734 		    dsa_is_cpu_port(ds, port)) {
1735 			tx_pause = true;
1736 			rx_pause = true;
1737 			speed = 2000;
1738 		}
1739 
1740 		b53_force_port_config(dev, port, speed, duplex,
1741 				      tx_pause, rx_pause);
1742 		b53_force_link(dev, port, true);
1743 		return;
1744 	}
1745 
1746 	if (phy_interface_mode_is_8023z(interface) &&
1747 	    dev->ops->serdes_link_set)
1748 		dev->ops->serdes_link_set(dev, port, mode, interface, true);
1749 }
1750 
1751 int b53_vlan_filtering(struct dsa_switch *ds, int port, bool vlan_filtering,
1752 		       struct netlink_ext_ack *extack)
1753 {
1754 	struct b53_device *dev = ds->priv;
1755 
1756 	if (dev->vlan_filtering != vlan_filtering) {
1757 		dev->vlan_filtering = vlan_filtering;
1758 		b53_apply_config(dev);
1759 	}
1760 
1761 	return 0;
1762 }
1763 EXPORT_SYMBOL(b53_vlan_filtering);
1764 
1765 static int b53_vlan_prepare(struct dsa_switch *ds, int port,
1766 			    const struct switchdev_obj_port_vlan *vlan)
1767 {
1768 	struct b53_device *dev = ds->priv;
1769 
1770 	/* Port 7 on 7278 connects to the ASP's UniMAC which is not capable of
1771 	 * receiving VLAN tagged frames at all, we can still allow the port to
1772 	 * be configured for egress untagged.
1773 	 */
1774 	if (dev->chip_id == BCM7278_DEVICE_ID && port == 7 &&
1775 	    !(vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED))
1776 		return -EINVAL;
1777 
1778 	if (vlan->vid >= dev->num_vlans)
1779 		return -ERANGE;
1780 
1781 	b53_enable_vlan(dev, port, true, dev->vlan_filtering);
1782 
1783 	return 0;
1784 }
1785 
1786 int b53_vlan_add(struct dsa_switch *ds, int port,
1787 		 const struct switchdev_obj_port_vlan *vlan,
1788 		 struct netlink_ext_ack *extack)
1789 {
1790 	struct b53_device *dev = ds->priv;
1791 	bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
1792 	bool pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
1793 	struct b53_vlan *vl;
1794 	u16 old_pvid, new_pvid;
1795 	int err;
1796 
1797 	err = b53_vlan_prepare(ds, port, vlan);
1798 	if (err)
1799 		return err;
1800 
1801 	if (vlan->vid == 0)
1802 		return 0;
1803 
1804 	old_pvid = dev->ports[port].pvid;
1805 	if (pvid)
1806 		new_pvid = vlan->vid;
1807 	else if (!pvid && vlan->vid == old_pvid)
1808 		new_pvid = b53_default_pvid(dev);
1809 	else
1810 		new_pvid = old_pvid;
1811 	dev->ports[port].pvid = new_pvid;
1812 
1813 	vl = &dev->vlans[vlan->vid];
1814 
1815 	if (dsa_is_cpu_port(ds, port))
1816 		untagged = false;
1817 
1818 	vl->members |= BIT(port);
1819 	if (untagged && !b53_vlan_port_needs_forced_tagged(ds, port))
1820 		vl->untag |= BIT(port);
1821 	else
1822 		vl->untag &= ~BIT(port);
1823 
1824 	if (!dev->vlan_filtering)
1825 		return 0;
1826 
1827 	b53_set_vlan_entry(dev, vlan->vid, vl);
1828 	b53_fast_age_vlan(dev, vlan->vid);
1829 
1830 	if (!dsa_is_cpu_port(ds, port) && new_pvid != old_pvid) {
1831 		b53_write16(dev, B53_VLAN_PAGE, B53_VLAN_PORT_DEF_TAG(port),
1832 			    new_pvid);
1833 		b53_fast_age_vlan(dev, old_pvid);
1834 	}
1835 
1836 	return 0;
1837 }
1838 EXPORT_SYMBOL(b53_vlan_add);
1839 
1840 int b53_vlan_del(struct dsa_switch *ds, int port,
1841 		 const struct switchdev_obj_port_vlan *vlan)
1842 {
1843 	struct b53_device *dev = ds->priv;
1844 	bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
1845 	struct b53_vlan *vl;
1846 	u16 pvid;
1847 
1848 	if (vlan->vid == 0)
1849 		return 0;
1850 
1851 	pvid = dev->ports[port].pvid;
1852 
1853 	vl = &dev->vlans[vlan->vid];
1854 
1855 	vl->members &= ~BIT(port);
1856 
1857 	if (pvid == vlan->vid)
1858 		pvid = b53_default_pvid(dev);
1859 	dev->ports[port].pvid = pvid;
1860 
1861 	if (untagged && !b53_vlan_port_needs_forced_tagged(ds, port))
1862 		vl->untag &= ~(BIT(port));
1863 
1864 	if (!dev->vlan_filtering)
1865 		return 0;
1866 
1867 	b53_set_vlan_entry(dev, vlan->vid, vl);
1868 	b53_fast_age_vlan(dev, vlan->vid);
1869 
1870 	b53_write16(dev, B53_VLAN_PAGE, B53_VLAN_PORT_DEF_TAG(port), pvid);
1871 	b53_fast_age_vlan(dev, pvid);
1872 
1873 	return 0;
1874 }
1875 EXPORT_SYMBOL(b53_vlan_del);
1876 
1877 /* Address Resolution Logic routines. Caller must hold &dev->arl_mutex. */
1878 static int b53_arl_op_wait(struct b53_device *dev)
1879 {
1880 	unsigned int timeout = 10;
1881 	u8 reg;
1882 
1883 	do {
1884 		b53_read8(dev, B53_ARLIO_PAGE, B53_ARLTBL_RW_CTRL, &reg);
1885 		if (!(reg & ARLTBL_START_DONE))
1886 			return 0;
1887 
1888 		usleep_range(1000, 2000);
1889 	} while (timeout--);
1890 
1891 	dev_warn(dev->dev, "timeout waiting for ARL to finish: 0x%02x\n", reg);
1892 
1893 	return -ETIMEDOUT;
1894 }
1895 
1896 static int b53_arl_rw_op(struct b53_device *dev, unsigned int op)
1897 {
1898 	u8 reg;
1899 
1900 	if (op > ARLTBL_RW)
1901 		return -EINVAL;
1902 
1903 	b53_read8(dev, B53_ARLIO_PAGE, B53_ARLTBL_RW_CTRL, &reg);
1904 	reg |= ARLTBL_START_DONE;
1905 	if (op)
1906 		reg |= ARLTBL_RW;
1907 	else
1908 		reg &= ~ARLTBL_RW;
1909 	if (dev->vlan_enabled)
1910 		reg &= ~ARLTBL_IVL_SVL_SELECT;
1911 	else
1912 		reg |= ARLTBL_IVL_SVL_SELECT;
1913 	b53_write8(dev, B53_ARLIO_PAGE, B53_ARLTBL_RW_CTRL, reg);
1914 
1915 	return b53_arl_op_wait(dev);
1916 }
1917 
1918 static void b53_arl_read_entry_25(struct b53_device *dev,
1919 				  struct b53_arl_entry *ent, u8 idx)
1920 {
1921 	u8 vid_entry;
1922 	u64 mac_vid;
1923 
1924 	b53_read8(dev, B53_ARLIO_PAGE, B53_ARLTBL_VID_ENTRY_25(idx),
1925 		  &vid_entry);
1926 	b53_read64(dev, B53_ARLIO_PAGE, B53_ARLTBL_MAC_VID_ENTRY(idx),
1927 		   &mac_vid);
1928 	b53_arl_to_entry_25(ent, mac_vid, vid_entry);
1929 }
1930 
1931 static void b53_arl_write_entry_25(struct b53_device *dev,
1932 				   const struct b53_arl_entry *ent, u8 idx)
1933 {
1934 	u8 vid_entry;
1935 	u64 mac_vid;
1936 
1937 	b53_arl_from_entry_25(&mac_vid, &vid_entry, ent);
1938 	b53_write8(dev, B53_ARLIO_PAGE, B53_ARLTBL_VID_ENTRY_25(idx), vid_entry);
1939 	b53_write64(dev, B53_ARLIO_PAGE, B53_ARLTBL_MAC_VID_ENTRY(idx),
1940 		    mac_vid);
1941 }
1942 
1943 static void b53_arl_read_entry_89(struct b53_device *dev,
1944 				  struct b53_arl_entry *ent, u8 idx)
1945 {
1946 	u64 mac_vid;
1947 	u16 fwd_entry;
1948 
1949 	b53_read64(dev, B53_ARLIO_PAGE, B53_ARLTBL_MAC_VID_ENTRY(idx),
1950 		   &mac_vid);
1951 	b53_read16(dev, B53_ARLIO_PAGE, B53_ARLTBL_DATA_ENTRY(idx), &fwd_entry);
1952 	b53_arl_to_entry_89(ent, mac_vid, fwd_entry);
1953 }
1954 
1955 static void b53_arl_write_entry_89(struct b53_device *dev,
1956 				   const struct b53_arl_entry *ent, u8 idx)
1957 {
1958 	u32 fwd_entry;
1959 	u64 mac_vid;
1960 
1961 	b53_arl_from_entry_89(&mac_vid, &fwd_entry, ent);
1962 	b53_write64(dev, B53_ARLIO_PAGE,
1963 		    B53_ARLTBL_MAC_VID_ENTRY(idx), mac_vid);
1964 	b53_write16(dev, B53_ARLIO_PAGE,
1965 		    B53_ARLTBL_DATA_ENTRY(idx), fwd_entry);
1966 }
1967 
1968 static void b53_arl_read_entry_95(struct b53_device *dev,
1969 				  struct b53_arl_entry *ent, u8 idx)
1970 {
1971 	u32 fwd_entry;
1972 	u64 mac_vid;
1973 
1974 	b53_read64(dev, B53_ARLIO_PAGE, B53_ARLTBL_MAC_VID_ENTRY(idx),
1975 		   &mac_vid);
1976 	b53_read32(dev, B53_ARLIO_PAGE, B53_ARLTBL_DATA_ENTRY(idx), &fwd_entry);
1977 	b53_arl_to_entry(ent, mac_vid, fwd_entry);
1978 }
1979 
1980 static void b53_arl_write_entry_95(struct b53_device *dev,
1981 				   const struct b53_arl_entry *ent, u8 idx)
1982 {
1983 	u32 fwd_entry;
1984 	u64 mac_vid;
1985 
1986 	b53_arl_from_entry(&mac_vid, &fwd_entry, ent);
1987 	b53_write64(dev, B53_ARLIO_PAGE, B53_ARLTBL_MAC_VID_ENTRY(idx),
1988 		    mac_vid);
1989 	b53_write32(dev, B53_ARLIO_PAGE, B53_ARLTBL_DATA_ENTRY(idx),
1990 		    fwd_entry);
1991 }
1992 
1993 static int b53_arl_read(struct b53_device *dev, const u8 *mac,
1994 			u16 vid, struct b53_arl_entry *ent, u8 *idx)
1995 {
1996 	DECLARE_BITMAP(free_bins, B53_ARLTBL_MAX_BIN_ENTRIES);
1997 	unsigned int i;
1998 	int ret;
1999 
2000 	ret = b53_arl_op_wait(dev);
2001 	if (ret)
2002 		return ret;
2003 
2004 	bitmap_zero(free_bins, dev->num_arl_bins);
2005 
2006 	/* Read the bins */
2007 	for (i = 0; i < dev->num_arl_bins; i++) {
2008 		b53_arl_read_entry(dev, ent, i);
2009 
2010 		if (!ent->is_valid) {
2011 			set_bit(i, free_bins);
2012 			continue;
2013 		}
2014 		if (!ether_addr_equal(ent->mac, mac))
2015 			continue;
2016 		if (dev->vlan_enabled && ent->vid != vid)
2017 			continue;
2018 		*idx = i;
2019 		return 0;
2020 	}
2021 
2022 	*idx = find_first_bit(free_bins, dev->num_arl_bins);
2023 	return *idx >= dev->num_arl_bins ? -ENOSPC : -ENOENT;
2024 }
2025 
2026 static int b53_arl_op(struct b53_device *dev, int op, int port,
2027 		      const unsigned char *addr, u16 vid, bool is_valid)
2028 {
2029 	struct b53_arl_entry ent;
2030 	u8 idx = 0;
2031 	u64 mac;
2032 	int ret;
2033 
2034 	/* Convert the array into a 64-bit MAC */
2035 	mac = ether_addr_to_u64(addr);
2036 
2037 	/* Perform a read for the given MAC and VID */
2038 	b53_write48(dev, B53_ARLIO_PAGE, B53_MAC_ADDR_IDX, mac);
2039 	if (!is5325m(dev)) {
2040 		if (is5325(dev) || is5365(dev))
2041 			b53_write8(dev, B53_ARLIO_PAGE, B53_VLAN_ID_IDX, vid);
2042 		else
2043 			b53_write16(dev, B53_ARLIO_PAGE, B53_VLAN_ID_IDX, vid);
2044 	}
2045 
2046 	/* Issue a read operation for this MAC */
2047 	ret = b53_arl_rw_op(dev, 1);
2048 	if (ret)
2049 		return ret;
2050 
2051 	ret = b53_arl_read(dev, addr, vid, &ent, &idx);
2052 
2053 	/* If this is a read, just finish now */
2054 	if (op)
2055 		return ret;
2056 
2057 	switch (ret) {
2058 	case -ETIMEDOUT:
2059 		return ret;
2060 	case -ENOSPC:
2061 		dev_dbg(dev->dev, "{%pM,%.4d} no space left in ARL\n",
2062 			addr, vid);
2063 		return is_valid ? ret : 0;
2064 	case -ENOENT:
2065 		if (!is_valid)
2066 			return 0;
2067 
2068 		/* We could not find a matching MAC, so reset to a new entry */
2069 		dev_dbg(dev->dev, "{%pM,%.4d} not found, using idx: %d\n",
2070 			addr, vid, idx);
2071 		memset(&ent, 0, sizeof(ent));
2072 		break;
2073 	default:
2074 		dev_dbg(dev->dev, "{%pM,%.4d} found, using idx: %d\n",
2075 			addr, vid, idx);
2076 		break;
2077 	}
2078 
2079 	/* For multicast address, the port is a bitmask and the validity
2080 	 * is determined by having at least one port being still active
2081 	 */
2082 	if (!is_multicast_ether_addr(addr)) {
2083 		ent.port = port;
2084 		ent.is_valid = is_valid;
2085 	} else {
2086 		if (is_valid)
2087 			ent.port |= BIT(port);
2088 		else
2089 			ent.port &= ~BIT(port);
2090 
2091 		ent.is_valid = !!(ent.port);
2092 	}
2093 
2094 	ent.vid = vid;
2095 	ent.is_static = true;
2096 	ent.is_age = false;
2097 	memcpy(ent.mac, addr, ETH_ALEN);
2098 	b53_arl_write_entry(dev, &ent, idx);
2099 
2100 	return b53_arl_rw_op(dev, 0);
2101 }
2102 
2103 int b53_fdb_add(struct dsa_switch *ds, int port,
2104 		const unsigned char *addr, u16 vid,
2105 		struct dsa_db db)
2106 {
2107 	struct b53_device *priv = ds->priv;
2108 	int ret;
2109 
2110 	mutex_lock(&priv->arl_mutex);
2111 	ret = b53_arl_op(priv, 0, port, addr, vid, true);
2112 	mutex_unlock(&priv->arl_mutex);
2113 
2114 	return ret;
2115 }
2116 EXPORT_SYMBOL(b53_fdb_add);
2117 
2118 int b53_fdb_del(struct dsa_switch *ds, int port,
2119 		const unsigned char *addr, u16 vid,
2120 		struct dsa_db db)
2121 {
2122 	struct b53_device *priv = ds->priv;
2123 	int ret;
2124 
2125 	mutex_lock(&priv->arl_mutex);
2126 	ret = b53_arl_op(priv, 0, port, addr, vid, false);
2127 	mutex_unlock(&priv->arl_mutex);
2128 
2129 	return ret;
2130 }
2131 EXPORT_SYMBOL(b53_fdb_del);
2132 
2133 static void b53_read_arl_srch_ctl(struct b53_device *dev, u8 *val)
2134 {
2135 	u8 offset;
2136 
2137 	if (is5325(dev) || is5365(dev))
2138 		offset = B53_ARL_SRCH_CTL_25;
2139 	else if (dev->chip_id == BCM5389_DEVICE_ID || is5397_98(dev) ||
2140 		 is63xx(dev))
2141 		offset = B53_ARL_SRCH_CTL_89;
2142 	else
2143 		offset = B53_ARL_SRCH_CTL;
2144 
2145 	if (is63xx(dev)) {
2146 		u16 val16;
2147 
2148 		b53_read16(dev, B53_ARLIO_PAGE, offset, &val16);
2149 		*val = val16 & 0xff;
2150 	} else {
2151 		b53_read8(dev, B53_ARLIO_PAGE, offset, val);
2152 	}
2153 }
2154 
2155 static void b53_write_arl_srch_ctl(struct b53_device *dev, u8 val)
2156 {
2157 	u8 offset;
2158 
2159 	if (is5325(dev) || is5365(dev))
2160 		offset = B53_ARL_SRCH_CTL_25;
2161 	else if (dev->chip_id == BCM5389_DEVICE_ID || is5397_98(dev) ||
2162 		 is63xx(dev))
2163 		offset = B53_ARL_SRCH_CTL_89;
2164 	else
2165 		offset = B53_ARL_SRCH_CTL;
2166 
2167 	if (is63xx(dev))
2168 		b53_write16(dev, B53_ARLIO_PAGE, offset, val);
2169 	else
2170 		b53_write8(dev, B53_ARLIO_PAGE, offset, val);
2171 }
2172 
2173 static int b53_arl_search_wait(struct b53_device *dev)
2174 {
2175 	unsigned int timeout = 1000;
2176 	u8 reg;
2177 
2178 	do {
2179 		b53_read_arl_srch_ctl(dev, &reg);
2180 		if (!(reg & ARL_SRCH_STDN))
2181 			return -ENOENT;
2182 
2183 		if (reg & ARL_SRCH_VLID)
2184 			return 0;
2185 
2186 		usleep_range(1000, 2000);
2187 	} while (timeout--);
2188 
2189 	return -ETIMEDOUT;
2190 }
2191 
2192 static void b53_arl_search_read_25(struct b53_device *dev, u8 idx,
2193 				   struct b53_arl_entry *ent)
2194 {
2195 	u64 mac_vid;
2196 	u8 ext;
2197 
2198 	b53_read8(dev, B53_ARLIO_PAGE, B53_ARL_SRCH_RSLT_EXT_25, &ext);
2199 	b53_read64(dev, B53_ARLIO_PAGE, B53_ARL_SRCH_RSTL_0_MACVID_25,
2200 		   &mac_vid);
2201 	b53_arl_search_to_entry_25(ent, mac_vid, ext);
2202 }
2203 
2204 static void b53_arl_search_read_89(struct b53_device *dev, u8 idx,
2205 				   struct b53_arl_entry *ent)
2206 {
2207 	u16 fwd_entry;
2208 	u64 mac_vid;
2209 
2210 	b53_read64(dev, B53_ARLIO_PAGE, B53_ARL_SRCH_RSLT_MACVID_89,
2211 		   &mac_vid);
2212 	b53_read16(dev, B53_ARLIO_PAGE, B53_ARL_SRCH_RSLT_89, &fwd_entry);
2213 	b53_arl_to_entry_89(ent, mac_vid, fwd_entry);
2214 }
2215 
2216 static void b53_arl_search_read_63xx(struct b53_device *dev, u8 idx,
2217 				     struct b53_arl_entry *ent)
2218 {
2219 	u16 fwd_entry;
2220 	u64 mac_vid;
2221 
2222 	b53_read64(dev, B53_ARLIO_PAGE, B53_ARL_SRCH_RSLT_MACVID_63XX,
2223 		   &mac_vid);
2224 	b53_read16(dev, B53_ARLIO_PAGE, B53_ARL_SRCH_RSLT_63XX, &fwd_entry);
2225 	b53_arl_search_to_entry_63xx(ent, mac_vid, fwd_entry);
2226 }
2227 
2228 static void b53_arl_search_read_95(struct b53_device *dev, u8 idx,
2229 				   struct b53_arl_entry *ent)
2230 {
2231 	u32 fwd_entry;
2232 	u64 mac_vid;
2233 
2234 	b53_read64(dev, B53_ARLIO_PAGE, B53_ARL_SRCH_RSTL_MACVID(idx),
2235 		   &mac_vid);
2236 	b53_read32(dev, B53_ARLIO_PAGE, B53_ARL_SRCH_RSTL(idx),
2237 		   &fwd_entry);
2238 	b53_arl_to_entry(ent, mac_vid, fwd_entry);
2239 }
2240 
2241 static int b53_fdb_copy(int port, const struct b53_arl_entry *ent,
2242 			dsa_fdb_dump_cb_t *cb, void *data)
2243 {
2244 	if (!ent->is_valid)
2245 		return 0;
2246 
2247 	if (is_multicast_ether_addr(ent->mac))
2248 		return 0;
2249 
2250 	if (port != ent->port)
2251 		return 0;
2252 
2253 	return cb(ent->mac, ent->vid, ent->is_static, data);
2254 }
2255 
2256 int b53_fdb_dump(struct dsa_switch *ds, int port,
2257 		 dsa_fdb_dump_cb_t *cb, void *data)
2258 {
2259 	unsigned int count = 0, results_per_hit = 1;
2260 	struct b53_device *priv = ds->priv;
2261 	struct b53_arl_entry results[2];
2262 	int ret;
2263 
2264 	if (priv->num_arl_bins > 2)
2265 		results_per_hit = 2;
2266 
2267 	mutex_lock(&priv->arl_mutex);
2268 
2269 	/* Start search operation */
2270 	b53_write_arl_srch_ctl(priv, ARL_SRCH_STDN);
2271 
2272 	do {
2273 		ret = b53_arl_search_wait(priv);
2274 		if (ret)
2275 			break;
2276 
2277 		b53_arl_search_read(priv, 0, &results[0]);
2278 		ret = b53_fdb_copy(port, &results[0], cb, data);
2279 		if (ret)
2280 			break;
2281 
2282 		if (results_per_hit == 2) {
2283 			b53_arl_search_read(priv, 1, &results[1]);
2284 			ret = b53_fdb_copy(port, &results[1], cb, data);
2285 			if (ret)
2286 				break;
2287 
2288 			if (!results[0].is_valid && !results[1].is_valid)
2289 				break;
2290 		}
2291 
2292 	} while (count++ < b53_max_arl_entries(priv) / results_per_hit);
2293 
2294 	mutex_unlock(&priv->arl_mutex);
2295 
2296 	return ret;
2297 }
2298 EXPORT_SYMBOL(b53_fdb_dump);
2299 
2300 int b53_mdb_add(struct dsa_switch *ds, int port,
2301 		const struct switchdev_obj_port_mdb *mdb,
2302 		struct dsa_db db)
2303 {
2304 	struct b53_device *priv = ds->priv;
2305 	int ret;
2306 
2307 	/* 5325 and 5365 require some more massaging, but could
2308 	 * be supported eventually
2309 	 */
2310 	if (is5325(priv) || is5365(priv))
2311 		return -EOPNOTSUPP;
2312 
2313 	mutex_lock(&priv->arl_mutex);
2314 	ret = b53_arl_op(priv, 0, port, mdb->addr, mdb->vid, true);
2315 	mutex_unlock(&priv->arl_mutex);
2316 
2317 	return ret;
2318 }
2319 EXPORT_SYMBOL(b53_mdb_add);
2320 
2321 int b53_mdb_del(struct dsa_switch *ds, int port,
2322 		const struct switchdev_obj_port_mdb *mdb,
2323 		struct dsa_db db)
2324 {
2325 	struct b53_device *priv = ds->priv;
2326 	int ret;
2327 
2328 	mutex_lock(&priv->arl_mutex);
2329 	ret = b53_arl_op(priv, 0, port, mdb->addr, mdb->vid, false);
2330 	mutex_unlock(&priv->arl_mutex);
2331 	if (ret)
2332 		dev_err(ds->dev, "failed to delete MDB entry\n");
2333 
2334 	return ret;
2335 }
2336 EXPORT_SYMBOL(b53_mdb_del);
2337 
2338 int b53_br_join(struct dsa_switch *ds, int port, struct dsa_bridge bridge,
2339 		bool *tx_fwd_offload, struct netlink_ext_ack *extack)
2340 {
2341 	struct b53_device *dev = ds->priv;
2342 	struct b53_vlan *vl;
2343 	s8 cpu_port = dsa_to_port(ds, port)->cpu_dp->index;
2344 	u16 pvlan, reg, pvid;
2345 	unsigned int i;
2346 
2347 	/* On 7278, port 7 which connects to the ASP should only receive
2348 	 * traffic from matching CFP rules.
2349 	 */
2350 	if (dev->chip_id == BCM7278_DEVICE_ID && port == 7)
2351 		return -EINVAL;
2352 
2353 	pvid = b53_default_pvid(dev);
2354 	vl = &dev->vlans[pvid];
2355 
2356 	if (dev->vlan_filtering) {
2357 		/* Make this port leave the all VLANs join since we will have
2358 		 * proper VLAN entries from now on
2359 		 */
2360 		if (is58xx(dev)) {
2361 			b53_read16(dev, B53_VLAN_PAGE, B53_JOIN_ALL_VLAN_EN,
2362 				   &reg);
2363 			reg &= ~BIT(port);
2364 			if ((reg & BIT(cpu_port)) == BIT(cpu_port))
2365 				reg &= ~BIT(cpu_port);
2366 			b53_write16(dev, B53_VLAN_PAGE, B53_JOIN_ALL_VLAN_EN,
2367 				    reg);
2368 		}
2369 
2370 		b53_get_vlan_entry(dev, pvid, vl);
2371 		vl->members &= ~BIT(port);
2372 		b53_set_vlan_entry(dev, pvid, vl);
2373 	}
2374 
2375 	b53_read16(dev, B53_PVLAN_PAGE, B53_PVLAN_PORT_MASK(port), &pvlan);
2376 
2377 	b53_for_each_port(dev, i) {
2378 		if (!dsa_port_offloads_bridge(dsa_to_port(ds, i), &bridge))
2379 			continue;
2380 
2381 		/* Add this local port to the remote port VLAN control
2382 		 * membership and update the remote port bitmask
2383 		 */
2384 		b53_read16(dev, B53_PVLAN_PAGE, B53_PVLAN_PORT_MASK(i), &reg);
2385 		reg |= BIT(port);
2386 		b53_write16(dev, B53_PVLAN_PAGE, B53_PVLAN_PORT_MASK(i), reg);
2387 		dev->ports[i].vlan_ctl_mask = reg;
2388 
2389 		pvlan |= BIT(i);
2390 	}
2391 
2392 	/* Disable redirection of unknown SA to the CPU port */
2393 	b53_set_eap_mode(dev, port, EAP_MODE_BASIC);
2394 
2395 	/* Configure the local port VLAN control membership to include
2396 	 * remote ports and update the local port bitmask
2397 	 */
2398 	b53_write16(dev, B53_PVLAN_PAGE, B53_PVLAN_PORT_MASK(port), pvlan);
2399 	dev->ports[port].vlan_ctl_mask = pvlan;
2400 
2401 	return 0;
2402 }
2403 EXPORT_SYMBOL(b53_br_join);
2404 
2405 void b53_br_leave(struct dsa_switch *ds, int port, struct dsa_bridge bridge)
2406 {
2407 	struct b53_device *dev = ds->priv;
2408 	struct b53_vlan *vl;
2409 	s8 cpu_port = dsa_to_port(ds, port)->cpu_dp->index;
2410 	unsigned int i;
2411 	u16 pvlan, reg, pvid;
2412 
2413 	b53_read16(dev, B53_PVLAN_PAGE, B53_PVLAN_PORT_MASK(port), &pvlan);
2414 
2415 	b53_for_each_port(dev, i) {
2416 		/* Don't touch the remaining ports */
2417 		if (!dsa_port_offloads_bridge(dsa_to_port(ds, i), &bridge))
2418 			continue;
2419 
2420 		b53_read16(dev, B53_PVLAN_PAGE, B53_PVLAN_PORT_MASK(i), &reg);
2421 		reg &= ~BIT(port);
2422 		b53_write16(dev, B53_PVLAN_PAGE, B53_PVLAN_PORT_MASK(i), reg);
2423 		dev->ports[port].vlan_ctl_mask = reg;
2424 
2425 		/* Prevent self removal to preserve isolation */
2426 		if (port != i)
2427 			pvlan &= ~BIT(i);
2428 	}
2429 
2430 	/* Enable redirection of unknown SA to the CPU port */
2431 	b53_set_eap_mode(dev, port, EAP_MODE_SIMPLIFIED);
2432 
2433 	b53_write16(dev, B53_PVLAN_PAGE, B53_PVLAN_PORT_MASK(port), pvlan);
2434 	dev->ports[port].vlan_ctl_mask = pvlan;
2435 
2436 	pvid = b53_default_pvid(dev);
2437 	vl = &dev->vlans[pvid];
2438 
2439 	if (dev->vlan_filtering) {
2440 		/* Make this port join all VLANs without VLAN entries */
2441 		if (is58xx(dev)) {
2442 			b53_read16(dev, B53_VLAN_PAGE, B53_JOIN_ALL_VLAN_EN, &reg);
2443 			reg |= BIT(port);
2444 			if (!(reg & BIT(cpu_port)))
2445 				reg |= BIT(cpu_port);
2446 			b53_write16(dev, B53_VLAN_PAGE, B53_JOIN_ALL_VLAN_EN, reg);
2447 		}
2448 
2449 		b53_get_vlan_entry(dev, pvid, vl);
2450 		vl->members |= BIT(port);
2451 		b53_set_vlan_entry(dev, pvid, vl);
2452 	}
2453 }
2454 EXPORT_SYMBOL(b53_br_leave);
2455 
2456 void b53_br_set_stp_state(struct dsa_switch *ds, int port, u8 state)
2457 {
2458 	struct b53_device *dev = ds->priv;
2459 	u8 hw_state;
2460 	u8 reg;
2461 
2462 	switch (state) {
2463 	case BR_STATE_DISABLED:
2464 		hw_state = PORT_CTRL_DIS_STATE;
2465 		break;
2466 	case BR_STATE_LISTENING:
2467 		hw_state = PORT_CTRL_LISTEN_STATE;
2468 		break;
2469 	case BR_STATE_LEARNING:
2470 		hw_state = PORT_CTRL_LEARN_STATE;
2471 		break;
2472 	case BR_STATE_FORWARDING:
2473 		hw_state = PORT_CTRL_FWD_STATE;
2474 		break;
2475 	case BR_STATE_BLOCKING:
2476 		hw_state = PORT_CTRL_BLOCK_STATE;
2477 		break;
2478 	default:
2479 		dev_err(ds->dev, "invalid STP state: %d\n", state);
2480 		return;
2481 	}
2482 
2483 	b53_read8(dev, B53_CTRL_PAGE, B53_PORT_CTRL(port), &reg);
2484 	reg &= ~PORT_CTRL_STP_STATE_MASK;
2485 	reg |= hw_state;
2486 	b53_write8(dev, B53_CTRL_PAGE, B53_PORT_CTRL(port), reg);
2487 }
2488 EXPORT_SYMBOL(b53_br_set_stp_state);
2489 
2490 void b53_br_fast_age(struct dsa_switch *ds, int port)
2491 {
2492 	struct b53_device *dev = ds->priv;
2493 
2494 	if (b53_fast_age_port(dev, port))
2495 		dev_err(ds->dev, "fast ageing failed\n");
2496 }
2497 EXPORT_SYMBOL(b53_br_fast_age);
2498 
2499 int b53_br_flags_pre(struct dsa_switch *ds, int port,
2500 		     struct switchdev_brport_flags flags,
2501 		     struct netlink_ext_ack *extack)
2502 {
2503 	struct b53_device *dev = ds->priv;
2504 	unsigned long mask = (BR_FLOOD | BR_MCAST_FLOOD | BR_ISOLATED);
2505 
2506 	if (!is5325(dev))
2507 		mask |= BR_LEARNING;
2508 
2509 	if (flags.mask & ~mask)
2510 		return -EINVAL;
2511 
2512 	return 0;
2513 }
2514 EXPORT_SYMBOL(b53_br_flags_pre);
2515 
2516 int b53_br_flags(struct dsa_switch *ds, int port,
2517 		 struct switchdev_brport_flags flags,
2518 		 struct netlink_ext_ack *extack)
2519 {
2520 	if (flags.mask & BR_FLOOD)
2521 		b53_port_set_ucast_flood(ds->priv, port,
2522 					 !!(flags.val & BR_FLOOD));
2523 	if (flags.mask & BR_MCAST_FLOOD)
2524 		b53_port_set_mcast_flood(ds->priv, port,
2525 					 !!(flags.val & BR_MCAST_FLOOD));
2526 	if (flags.mask & BR_LEARNING)
2527 		b53_port_set_learning(ds->priv, port,
2528 				      !!(flags.val & BR_LEARNING));
2529 	if (flags.mask & BR_ISOLATED)
2530 		b53_port_set_isolated(ds->priv, port,
2531 				      !!(flags.val & BR_ISOLATED));
2532 
2533 	return 0;
2534 }
2535 EXPORT_SYMBOL(b53_br_flags);
2536 
2537 static bool b53_possible_cpu_port(struct dsa_switch *ds, int port)
2538 {
2539 	/* Broadcom switches will accept enabling Broadcom tags on the
2540 	 * following ports: 5, 7 and 8, any other port is not supported
2541 	 */
2542 	switch (port) {
2543 	case B53_CPU_PORT_25:
2544 	case 7:
2545 	case B53_CPU_PORT:
2546 		return true;
2547 	}
2548 
2549 	return false;
2550 }
2551 
2552 static bool b53_can_enable_brcm_tags(struct dsa_switch *ds, int port,
2553 				     enum dsa_tag_protocol tag_protocol)
2554 {
2555 	bool ret = b53_possible_cpu_port(ds, port);
2556 
2557 	if (!ret) {
2558 		dev_warn(ds->dev, "Port %d is not Broadcom tag capable\n",
2559 			 port);
2560 		return ret;
2561 	}
2562 
2563 	switch (tag_protocol) {
2564 	case DSA_TAG_PROTO_BRCM:
2565 	case DSA_TAG_PROTO_BRCM_PREPEND:
2566 		dev_warn(ds->dev,
2567 			 "Port %d is stacked to Broadcom tag switch\n", port);
2568 		ret = false;
2569 		break;
2570 	default:
2571 		ret = true;
2572 		break;
2573 	}
2574 
2575 	return ret;
2576 }
2577 
2578 enum dsa_tag_protocol b53_get_tag_protocol(struct dsa_switch *ds, int port,
2579 					   enum dsa_tag_protocol mprot)
2580 {
2581 	struct b53_device *dev = ds->priv;
2582 
2583 	if (!b53_can_enable_brcm_tags(ds, port, mprot)) {
2584 		dev->tag_protocol = DSA_TAG_PROTO_NONE;
2585 		goto out;
2586 	}
2587 
2588 	/* Older models require different 6 byte tags */
2589 	if (is5325(dev) || is5365(dev)) {
2590 		dev->tag_protocol = DSA_TAG_PROTO_BRCM_LEGACY_FCS;
2591 		goto out;
2592 	} else if (is63xx(dev)) {
2593 		dev->tag_protocol = DSA_TAG_PROTO_BRCM_LEGACY;
2594 		goto out;
2595 	}
2596 
2597 	/* Broadcom BCM58xx chips have a flow accelerator on Port 8
2598 	 * which requires us to use the prepended Broadcom tag type
2599 	 */
2600 	if (dev->chip_id == BCM58XX_DEVICE_ID && port == B53_CPU_PORT) {
2601 		dev->tag_protocol = DSA_TAG_PROTO_BRCM_PREPEND;
2602 		goto out;
2603 	}
2604 
2605 	dev->tag_protocol = DSA_TAG_PROTO_BRCM;
2606 out:
2607 	return dev->tag_protocol;
2608 }
2609 EXPORT_SYMBOL(b53_get_tag_protocol);
2610 
2611 static u16 b53_mirror_capture_port_mask(struct b53_device *dev)
2612 {
2613 	if (is5325(dev) || is5365(dev))
2614 		return CAP_PORT_MASK_25;
2615 
2616 	return CAP_PORT_MASK;
2617 }
2618 
2619 static u16 b53_mirror_port_mask(struct b53_device *dev, int port)
2620 {
2621 	/* These switches use a bitmap with the CPU port at bit 8. */
2622 	if ((is5325(dev) || is5365(dev)) && port == B53_CPU_PORT_25)
2623 		port = B53_CPU_PORT;
2624 
2625 	return BIT(port);
2626 }
2627 
2628 static u16 b53_mirror_capture_port(struct b53_device *dev, int port)
2629 {
2630 	if (is5325(dev) || is5365(dev))
2631 		return b53_mirror_port_mask(dev, port);
2632 
2633 	return port;
2634 }
2635 
2636 int b53_mirror_init(struct dsa_switch *ds)
2637 {
2638 	struct b53_device *dev = ds->priv;
2639 	u16 ingress = 0, egress = 0, cap_ctl = 0;
2640 	unsigned int port;
2641 	int ret;
2642 
2643 	/* A switch reset may leave the previous mirror configuration intact. */
2644 	ret = b53_write16(dev, B53_MGMT_PAGE, B53_MIR_CAP_CTL, 0);
2645 	if (ret)
2646 		return ret;
2647 
2648 	ret = b53_write16(dev, B53_MGMT_PAGE, B53_IG_MIR_CTL, 0);
2649 	if (ret)
2650 		return ret;
2651 
2652 	ret = b53_write16(dev, B53_MGMT_PAGE, B53_EG_MIR_CTL, 0);
2653 	if (ret)
2654 		return ret;
2655 
2656 	/* DSA retains matchall filters across suspend and resume. */
2657 	for (port = 0; port < dev->num_ports; port++) {
2658 		if (dev->ports[port].mirror_count[true])
2659 			ingress |= b53_mirror_port_mask(dev, port);
2660 		if (dev->ports[port].mirror_count[false])
2661 			egress |= b53_mirror_port_mask(dev, port);
2662 	}
2663 
2664 	if (!(ingress | egress))
2665 		return 0;
2666 
2667 	cap_ctl = MIRROR_EN | b53_mirror_capture_port(dev, dev->mirror_port);
2668 	ret = b53_write16(dev, B53_MGMT_PAGE, B53_MIR_CAP_CTL, cap_ctl);
2669 	if (ret)
2670 		return ret;
2671 
2672 	ret = b53_write16(dev, B53_MGMT_PAGE, B53_IG_MIR_CTL, ingress);
2673 	if (ret)
2674 		return ret;
2675 
2676 	return b53_write16(dev, B53_MGMT_PAGE, B53_EG_MIR_CTL, egress);
2677 }
2678 EXPORT_SYMBOL(b53_mirror_init);
2679 
2680 int b53_mirror_add(struct dsa_switch *ds, int port,
2681 		   struct dsa_mall_mirror_tc_entry *mirror, bool ingress,
2682 		   struct netlink_ext_ack *extack)
2683 {
2684 	struct b53_device *dev = ds->priv;
2685 	unsigned int *mirror_count = &dev->ports[port].mirror_count[ingress];
2686 	u16 reg, other_reg, loc, other_loc, cap_ctl;
2687 	u16 cap_mask = b53_mirror_capture_port_mask(dev);
2688 	u16 cap_port = b53_mirror_capture_port(dev, mirror->to_local_port);
2689 	int ret;
2690 
2691 	loc = ingress ? B53_IG_MIR_CTL : B53_EG_MIR_CTL;
2692 	other_loc = ingress ? B53_EG_MIR_CTL : B53_IG_MIR_CTL;
2693 
2694 	ret = b53_read16(dev, B53_MGMT_PAGE, loc, &reg);
2695 	if (ret)
2696 		return ret;
2697 
2698 	ret = b53_read16(dev, B53_MGMT_PAGE, other_loc, &other_reg);
2699 	if (ret)
2700 		return ret;
2701 
2702 	ret = b53_read16(dev, B53_MGMT_PAGE, B53_MIR_CAP_CTL, &cap_ctl);
2703 	if (ret)
2704 		return ret;
2705 
2706 	if (((reg | other_reg) & MIRROR_MASK) &&
2707 	    (cap_ctl & cap_mask) != cap_port) {
2708 		NL_SET_ERR_MSG_MOD(extack,
2709 				   "Only one mirroring destination is supported");
2710 		return -EOPNOTSUPP;
2711 	}
2712 
2713 	if (*mirror_count) {
2714 		(*mirror_count)++;
2715 		return 0;
2716 	}
2717 
2718 	/* Select the destination before enabling the new source. */
2719 	cap_ctl &= ~cap_mask;
2720 	cap_ctl |= cap_port;
2721 	cap_ctl |= MIRROR_EN;
2722 	ret = b53_write16(dev, B53_MGMT_PAGE, B53_MIR_CAP_CTL, cap_ctl);
2723 	if (ret)
2724 		return ret;
2725 
2726 	ret = b53_write16(dev, B53_MGMT_PAGE, loc,
2727 			  reg | b53_mirror_port_mask(dev, port));
2728 	if (ret)
2729 		return ret;
2730 
2731 	(*mirror_count)++;
2732 	dev->mirror_port = mirror->to_local_port;
2733 
2734 	return 0;
2735 }
2736 EXPORT_SYMBOL(b53_mirror_add);
2737 
2738 void b53_mirror_del(struct dsa_switch *ds, int port,
2739 		    struct dsa_mall_mirror_tc_entry *mirror)
2740 {
2741 	struct b53_device *dev = ds->priv;
2742 	unsigned int *mirror_count =
2743 		&dev->ports[port].mirror_count[mirror->ingress];
2744 	u16 reg, other_reg, loc, other_loc;
2745 	int ret;
2746 
2747 	if (!*mirror_count)
2748 		return;
2749 
2750 	if (--(*mirror_count))
2751 		return;
2752 
2753 	loc = mirror->ingress ? B53_IG_MIR_CTL : B53_EG_MIR_CTL;
2754 	other_loc = mirror->ingress ? B53_EG_MIR_CTL : B53_IG_MIR_CTL;
2755 
2756 	/* Update the desired ingress/egress register */
2757 	ret = b53_read16(dev, B53_MGMT_PAGE, loc, &reg);
2758 	if (ret)
2759 		goto err;
2760 
2761 	reg &= ~b53_mirror_port_mask(dev, port);
2762 	ret = b53_write16(dev, B53_MGMT_PAGE, loc, reg);
2763 	if (ret)
2764 		goto err;
2765 
2766 	/* Now look at the other one to know if we can disable mirroring
2767 	 * entirely
2768 	 */
2769 	ret = b53_read16(dev, B53_MGMT_PAGE, other_loc, &other_reg);
2770 	if (ret)
2771 		goto err;
2772 
2773 	if ((reg | other_reg) & MIRROR_MASK)
2774 		return;
2775 
2776 	ret = b53_read16(dev, B53_MGMT_PAGE, B53_MIR_CAP_CTL, &reg);
2777 	if (ret)
2778 		goto err;
2779 
2780 	reg &= ~(MIRROR_EN | b53_mirror_capture_port_mask(dev));
2781 	ret = b53_write16(dev, B53_MGMT_PAGE, B53_MIR_CAP_CTL, reg);
2782 	if (ret)
2783 		goto err;
2784 
2785 	return;
2786 
2787 err:
2788 	dev_err(dev->dev, "Failed to delete mirror rule for port %d: %d\n",
2789 		port, ret);
2790 }
2791 EXPORT_SYMBOL(b53_mirror_del);
2792 
2793 /* Returns 0 if EEE was not enabled, or 1 otherwise
2794  */
2795 int b53_eee_init(struct dsa_switch *ds, int port, struct phy_device *phy)
2796 {
2797 	int ret;
2798 
2799 	if (!b53_support_eee(ds, port))
2800 		return 0;
2801 
2802 	ret = phy_init_eee(phy, false);
2803 	if (ret)
2804 		return 0;
2805 
2806 	b53_eee_enable_set(ds, port, true);
2807 
2808 	return 1;
2809 }
2810 EXPORT_SYMBOL(b53_eee_init);
2811 
2812 bool b53_support_eee(struct dsa_switch *ds, int port)
2813 {
2814 	struct b53_device *dev = ds->priv;
2815 
2816 	return !is5325(dev) && !is5365(dev) && !is63xx(dev);
2817 }
2818 EXPORT_SYMBOL(b53_support_eee);
2819 
2820 int b53_set_mac_eee(struct dsa_switch *ds, int port, struct ethtool_keee *e)
2821 {
2822 	struct b53_device *dev = ds->priv;
2823 	struct ethtool_keee *p = &dev->ports[port].eee;
2824 
2825 	p->eee_enabled = e->eee_enabled;
2826 	b53_eee_enable_set(ds, port, e->eee_enabled);
2827 
2828 	return 0;
2829 }
2830 EXPORT_SYMBOL(b53_set_mac_eee);
2831 
2832 static int b53_change_mtu(struct dsa_switch *ds, int port, int mtu)
2833 {
2834 	struct b53_device *dev = ds->priv;
2835 	bool enable_jumbo;
2836 	bool allow_10_100;
2837 
2838 	if (is5325(dev) || is5365(dev))
2839 		return 0;
2840 
2841 	if (!dsa_is_cpu_port(ds, port))
2842 		return 0;
2843 
2844 	enable_jumbo = (mtu > ETH_DATA_LEN);
2845 	allow_10_100 = !is63xx(dev);
2846 
2847 	return b53_set_jumbo(dev, enable_jumbo, allow_10_100);
2848 }
2849 
2850 static int b53_get_max_mtu(struct dsa_switch *ds, int port)
2851 {
2852 	struct b53_device *dev = ds->priv;
2853 
2854 	if (is5325(dev) || is5365(dev))
2855 		return B53_MAX_MTU_25;
2856 
2857 	return B53_MAX_MTU;
2858 }
2859 
2860 int b53_set_ageing_time(struct dsa_switch *ds, unsigned int msecs)
2861 {
2862 	struct b53_device *dev = ds->priv;
2863 	u32 atc;
2864 	int reg;
2865 
2866 	if (is63xx(dev))
2867 		reg = B53_AGING_TIME_CONTROL_63XX;
2868 	else
2869 		reg = B53_AGING_TIME_CONTROL;
2870 
2871 	if (dev->chip_id == BCM53101_DEVICE_ID)
2872 		atc = DIV_ROUND_CLOSEST(msecs, 500);
2873 	else
2874 		atc = DIV_ROUND_CLOSEST(msecs, 1000);
2875 
2876 	if (!is5325(dev) && !is5365(dev))
2877 		atc |= AGE_CHANGE;
2878 
2879 	b53_write32(dev, B53_MGMT_PAGE, reg, atc);
2880 
2881 	return 0;
2882 }
2883 EXPORT_SYMBOL_GPL(b53_set_ageing_time);
2884 
2885 static const struct phylink_mac_ops b53_phylink_mac_ops = {
2886 	.mac_select_pcs	= b53_phylink_mac_select_pcs,
2887 	.mac_config	= b53_phylink_mac_config,
2888 	.mac_link_down	= b53_phylink_mac_link_down,
2889 	.mac_link_up	= b53_phylink_mac_link_up,
2890 };
2891 
2892 static const struct dsa_switch_ops b53_switch_ops = {
2893 	.get_tag_protocol	= b53_get_tag_protocol,
2894 	.setup			= b53_setup,
2895 	.teardown		= b53_teardown,
2896 	.get_strings		= b53_get_strings,
2897 	.get_ethtool_stats	= b53_get_ethtool_stats,
2898 	.get_sset_count		= b53_get_sset_count,
2899 	.get_ethtool_phy_stats	= b53_get_ethtool_phy_stats,
2900 	.phy_read		= b53_phy_read16,
2901 	.phy_write		= b53_phy_write16,
2902 	.phylink_get_caps	= b53_phylink_get_caps,
2903 	.port_setup		= b53_setup_port,
2904 	.port_enable		= b53_enable_port,
2905 	.port_disable		= b53_disable_port,
2906 	.support_eee		= b53_support_eee,
2907 	.set_mac_eee		= b53_set_mac_eee,
2908 	.set_ageing_time	= b53_set_ageing_time,
2909 	.port_bridge_join	= b53_br_join,
2910 	.port_bridge_leave	= b53_br_leave,
2911 	.port_pre_bridge_flags	= b53_br_flags_pre,
2912 	.port_bridge_flags	= b53_br_flags,
2913 	.port_stp_state_set	= b53_br_set_stp_state,
2914 	.port_fast_age		= b53_br_fast_age,
2915 	.port_vlan_filtering	= b53_vlan_filtering,
2916 	.port_vlan_add		= b53_vlan_add,
2917 	.port_vlan_del		= b53_vlan_del,
2918 	.port_fdb_dump		= b53_fdb_dump,
2919 	.port_fdb_add		= b53_fdb_add,
2920 	.port_fdb_del		= b53_fdb_del,
2921 	.port_mirror_add	= b53_mirror_add,
2922 	.port_mirror_del	= b53_mirror_del,
2923 	.port_mdb_add		= b53_mdb_add,
2924 	.port_mdb_del		= b53_mdb_del,
2925 	.port_max_mtu		= b53_get_max_mtu,
2926 	.port_change_mtu	= b53_change_mtu,
2927 };
2928 
2929 static const struct b53_arl_ops b53_arl_ops_25 = {
2930 	.arl_read_entry = b53_arl_read_entry_25,
2931 	.arl_write_entry = b53_arl_write_entry_25,
2932 	.arl_search_read = b53_arl_search_read_25,
2933 };
2934 
2935 static const struct b53_arl_ops b53_arl_ops_89 = {
2936 	.arl_read_entry = b53_arl_read_entry_89,
2937 	.arl_write_entry = b53_arl_write_entry_89,
2938 	.arl_search_read = b53_arl_search_read_89,
2939 };
2940 
2941 static const struct b53_arl_ops b53_arl_ops_63xx = {
2942 	.arl_read_entry = b53_arl_read_entry_89,
2943 	.arl_write_entry = b53_arl_write_entry_89,
2944 	.arl_search_read = b53_arl_search_read_63xx,
2945 };
2946 
2947 static const struct b53_arl_ops b53_arl_ops_95 = {
2948 	.arl_read_entry = b53_arl_read_entry_95,
2949 	.arl_write_entry = b53_arl_write_entry_95,
2950 	.arl_search_read = b53_arl_search_read_95,
2951 };
2952 
2953 struct b53_chip_data {
2954 	u32 chip_id;
2955 	const char *dev_name;
2956 	u16 vlans;
2957 	u16 enabled_ports;
2958 	u8 imp_port;
2959 	u8 cpu_port;
2960 	u8 vta_regs[3];
2961 	u8 arl_bins;
2962 	u16 arl_buckets;
2963 	u8 duplex_reg;
2964 	u8 jumbo_pm_reg;
2965 	u8 jumbo_size_reg;
2966 	const struct b53_arl_ops *arl_ops;
2967 };
2968 
2969 #define B53_VTA_REGS	\
2970 	{ B53_VT_ACCESS, B53_VT_INDEX, B53_VT_ENTRY }
2971 #define B53_VTA_REGS_9798 \
2972 	{ B53_VT_ACCESS_9798, B53_VT_INDEX_9798, B53_VT_ENTRY_9798 }
2973 #define B53_VTA_REGS_63XX \
2974 	{ B53_VT_ACCESS_63XX, B53_VT_INDEX_63XX, B53_VT_ENTRY_63XX }
2975 
2976 static const struct b53_chip_data b53_switch_chips[] = {
2977 	{
2978 		.chip_id = BCM5325_DEVICE_ID,
2979 		.dev_name = "BCM5325",
2980 		.vlans = 16,
2981 		.enabled_ports = 0x3f,
2982 		.arl_bins = 2,
2983 		.arl_buckets = 1024,
2984 		.imp_port = 5,
2985 		.duplex_reg = B53_DUPLEX_STAT_FE,
2986 		.arl_ops = &b53_arl_ops_25,
2987 	},
2988 	{
2989 		.chip_id = BCM5365_DEVICE_ID,
2990 		.dev_name = "BCM5365",
2991 		.vlans = 256,
2992 		.enabled_ports = 0x3f,
2993 		.arl_bins = 2,
2994 		.arl_buckets = 1024,
2995 		.imp_port = 5,
2996 		.duplex_reg = B53_DUPLEX_STAT_FE,
2997 		.arl_ops = &b53_arl_ops_25,
2998 	},
2999 	{
3000 		.chip_id = BCM5389_DEVICE_ID,
3001 		.dev_name = "BCM5389",
3002 		.vlans = 4096,
3003 		.enabled_ports = 0x11f,
3004 		.arl_bins = 4,
3005 		.arl_buckets = 1024,
3006 		.imp_port = 8,
3007 		.vta_regs = B53_VTA_REGS,
3008 		.duplex_reg = B53_DUPLEX_STAT_GE,
3009 		.jumbo_pm_reg = B53_JUMBO_PORT_MASK,
3010 		.jumbo_size_reg = B53_JUMBO_MAX_SIZE,
3011 		.arl_ops = &b53_arl_ops_89,
3012 	},
3013 	{
3014 		.chip_id = BCM5395_DEVICE_ID,
3015 		.dev_name = "BCM5395",
3016 		.vlans = 4096,
3017 		.enabled_ports = 0x11f,
3018 		.arl_bins = 4,
3019 		.arl_buckets = 1024,
3020 		.imp_port = 8,
3021 		.vta_regs = B53_VTA_REGS,
3022 		.duplex_reg = B53_DUPLEX_STAT_GE,
3023 		.jumbo_pm_reg = B53_JUMBO_PORT_MASK,
3024 		.jumbo_size_reg = B53_JUMBO_MAX_SIZE,
3025 		.arl_ops = &b53_arl_ops_95,
3026 	},
3027 	{
3028 		.chip_id = BCM5397_DEVICE_ID,
3029 		.dev_name = "BCM5397",
3030 		.vlans = 4096,
3031 		.enabled_ports = 0x11f,
3032 		.arl_bins = 4,
3033 		.arl_buckets = 1024,
3034 		.imp_port = 8,
3035 		.vta_regs = B53_VTA_REGS_9798,
3036 		.duplex_reg = B53_DUPLEX_STAT_GE,
3037 		.jumbo_pm_reg = B53_JUMBO_PORT_MASK,
3038 		.jumbo_size_reg = B53_JUMBO_MAX_SIZE,
3039 		.arl_ops = &b53_arl_ops_89,
3040 	},
3041 	{
3042 		.chip_id = BCM5398_DEVICE_ID,
3043 		.dev_name = "BCM5398",
3044 		.vlans = 4096,
3045 		.enabled_ports = 0x17f,
3046 		.arl_bins = 4,
3047 		.arl_buckets = 1024,
3048 		.imp_port = 8,
3049 		.vta_regs = B53_VTA_REGS_9798,
3050 		.duplex_reg = B53_DUPLEX_STAT_GE,
3051 		.jumbo_pm_reg = B53_JUMBO_PORT_MASK,
3052 		.jumbo_size_reg = B53_JUMBO_MAX_SIZE,
3053 		.arl_ops = &b53_arl_ops_89,
3054 	},
3055 	{
3056 		.chip_id = BCM53101_DEVICE_ID,
3057 		.dev_name = "BCM53101",
3058 		.vlans = 4096,
3059 		.enabled_ports = 0x11f,
3060 		.arl_bins = 4,
3061 		.arl_buckets = 512,
3062 		.vta_regs = B53_VTA_REGS,
3063 		.imp_port = 8,
3064 		.duplex_reg = B53_DUPLEX_STAT_GE,
3065 		.jumbo_pm_reg = B53_JUMBO_PORT_MASK,
3066 		.jumbo_size_reg = B53_JUMBO_MAX_SIZE,
3067 		.arl_ops = &b53_arl_ops_95,
3068 	},
3069 	{
3070 		.chip_id = BCM53115_DEVICE_ID,
3071 		.dev_name = "BCM53115",
3072 		.vlans = 4096,
3073 		.enabled_ports = 0x11f,
3074 		.arl_bins = 4,
3075 		.arl_buckets = 1024,
3076 		.vta_regs = B53_VTA_REGS,
3077 		.imp_port = 8,
3078 		.duplex_reg = B53_DUPLEX_STAT_GE,
3079 		.jumbo_pm_reg = B53_JUMBO_PORT_MASK,
3080 		.jumbo_size_reg = B53_JUMBO_MAX_SIZE,
3081 		.arl_ops = &b53_arl_ops_95,
3082 	},
3083 	{
3084 		.chip_id = BCM53125_DEVICE_ID,
3085 		.dev_name = "BCM53125",
3086 		.vlans = 4096,
3087 		.enabled_ports = 0x1ff,
3088 		.arl_bins = 4,
3089 		.arl_buckets = 1024,
3090 		.imp_port = 8,
3091 		.vta_regs = B53_VTA_REGS,
3092 		.duplex_reg = B53_DUPLEX_STAT_GE,
3093 		.jumbo_pm_reg = B53_JUMBO_PORT_MASK,
3094 		.jumbo_size_reg = B53_JUMBO_MAX_SIZE,
3095 		.arl_ops = &b53_arl_ops_95,
3096 	},
3097 	{
3098 		.chip_id = BCM53128_DEVICE_ID,
3099 		.dev_name = "BCM53128",
3100 		.vlans = 4096,
3101 		.enabled_ports = 0x1ff,
3102 		.arl_bins = 4,
3103 		.arl_buckets = 1024,
3104 		.imp_port = 8,
3105 		.vta_regs = B53_VTA_REGS,
3106 		.duplex_reg = B53_DUPLEX_STAT_GE,
3107 		.jumbo_pm_reg = B53_JUMBO_PORT_MASK,
3108 		.jumbo_size_reg = B53_JUMBO_MAX_SIZE,
3109 		.arl_ops = &b53_arl_ops_95,
3110 	},
3111 	{
3112 		.chip_id = BCM63XX_DEVICE_ID,
3113 		.dev_name = "BCM63xx",
3114 		.vlans = 4096,
3115 		.enabled_ports = 0, /* pdata must provide them */
3116 		.arl_bins = 1,
3117 		.arl_buckets = 4096,
3118 		.imp_port = 8,
3119 		.vta_regs = B53_VTA_REGS_63XX,
3120 		.duplex_reg = B53_DUPLEX_STAT_63XX,
3121 		.jumbo_pm_reg = B53_JUMBO_PORT_MASK_63XX,
3122 		.jumbo_size_reg = B53_JUMBO_MAX_SIZE_63XX,
3123 		.arl_ops = &b53_arl_ops_63xx,
3124 	},
3125 	{
3126 		.chip_id = BCM53010_DEVICE_ID,
3127 		.dev_name = "BCM53010",
3128 		.vlans = 4096,
3129 		.enabled_ports = 0x1bf,
3130 		.arl_bins = 4,
3131 		.arl_buckets = 1024,
3132 		.imp_port = 8,
3133 		.vta_regs = B53_VTA_REGS,
3134 		.duplex_reg = B53_DUPLEX_STAT_GE,
3135 		.jumbo_pm_reg = B53_JUMBO_PORT_MASK,
3136 		.jumbo_size_reg = B53_JUMBO_MAX_SIZE,
3137 		.arl_ops = &b53_arl_ops_95,
3138 	},
3139 	{
3140 		.chip_id = BCM53011_DEVICE_ID,
3141 		.dev_name = "BCM53011",
3142 		.vlans = 4096,
3143 		.enabled_ports = 0x1bf,
3144 		.arl_bins = 4,
3145 		.arl_buckets = 1024,
3146 		.imp_port = 8,
3147 		.vta_regs = B53_VTA_REGS,
3148 		.duplex_reg = B53_DUPLEX_STAT_GE,
3149 		.jumbo_pm_reg = B53_JUMBO_PORT_MASK,
3150 		.jumbo_size_reg = B53_JUMBO_MAX_SIZE,
3151 		.arl_ops = &b53_arl_ops_95,
3152 	},
3153 	{
3154 		.chip_id = BCM53012_DEVICE_ID,
3155 		.dev_name = "BCM53012",
3156 		.vlans = 4096,
3157 		.enabled_ports = 0x1bf,
3158 		.arl_bins = 4,
3159 		.arl_buckets = 1024,
3160 		.imp_port = 8,
3161 		.vta_regs = B53_VTA_REGS,
3162 		.duplex_reg = B53_DUPLEX_STAT_GE,
3163 		.jumbo_pm_reg = B53_JUMBO_PORT_MASK,
3164 		.jumbo_size_reg = B53_JUMBO_MAX_SIZE,
3165 		.arl_ops = &b53_arl_ops_95,
3166 	},
3167 	{
3168 		.chip_id = BCM53018_DEVICE_ID,
3169 		.dev_name = "BCM53018",
3170 		.vlans = 4096,
3171 		.enabled_ports = 0x1bf,
3172 		.arl_bins = 4,
3173 		.arl_buckets = 1024,
3174 		.imp_port = 8,
3175 		.vta_regs = B53_VTA_REGS,
3176 		.duplex_reg = B53_DUPLEX_STAT_GE,
3177 		.jumbo_pm_reg = B53_JUMBO_PORT_MASK,
3178 		.jumbo_size_reg = B53_JUMBO_MAX_SIZE,
3179 		.arl_ops = &b53_arl_ops_95,
3180 	},
3181 	{
3182 		.chip_id = BCM53019_DEVICE_ID,
3183 		.dev_name = "BCM53019",
3184 		.vlans = 4096,
3185 		.enabled_ports = 0x1bf,
3186 		.arl_bins = 4,
3187 		.arl_buckets = 1024,
3188 		.imp_port = 8,
3189 		.vta_regs = B53_VTA_REGS,
3190 		.duplex_reg = B53_DUPLEX_STAT_GE,
3191 		.jumbo_pm_reg = B53_JUMBO_PORT_MASK,
3192 		.jumbo_size_reg = B53_JUMBO_MAX_SIZE,
3193 		.arl_ops = &b53_arl_ops_95,
3194 	},
3195 	{
3196 		.chip_id = BCM58XX_DEVICE_ID,
3197 		.dev_name = "BCM585xx/586xx/88312",
3198 		.vlans	= 4096,
3199 		.enabled_ports = 0x1ff,
3200 		.arl_bins = 4,
3201 		.arl_buckets = 1024,
3202 		.imp_port = 8,
3203 		.vta_regs = B53_VTA_REGS,
3204 		.duplex_reg = B53_DUPLEX_STAT_GE,
3205 		.jumbo_pm_reg = B53_JUMBO_PORT_MASK,
3206 		.jumbo_size_reg = B53_JUMBO_MAX_SIZE,
3207 		.arl_ops = &b53_arl_ops_95,
3208 	},
3209 	{
3210 		.chip_id = BCM583XX_DEVICE_ID,
3211 		.dev_name = "BCM583xx/11360",
3212 		.vlans = 4096,
3213 		.enabled_ports = 0x103,
3214 		.arl_bins = 4,
3215 		.arl_buckets = 1024,
3216 		.imp_port = 8,
3217 		.vta_regs = B53_VTA_REGS,
3218 		.duplex_reg = B53_DUPLEX_STAT_GE,
3219 		.jumbo_pm_reg = B53_JUMBO_PORT_MASK,
3220 		.jumbo_size_reg = B53_JUMBO_MAX_SIZE,
3221 		.arl_ops = &b53_arl_ops_95,
3222 	},
3223 	/* Starfighter 2 */
3224 	{
3225 		.chip_id = BCM4908_DEVICE_ID,
3226 		.dev_name = "BCM4908",
3227 		.vlans = 4096,
3228 		.enabled_ports = 0x1bf,
3229 		.arl_bins = 4,
3230 		.arl_buckets = 256,
3231 		.imp_port = 8,
3232 		.vta_regs = B53_VTA_REGS,
3233 		.duplex_reg = B53_DUPLEX_STAT_GE,
3234 		.jumbo_pm_reg = B53_JUMBO_PORT_MASK,
3235 		.jumbo_size_reg = B53_JUMBO_MAX_SIZE,
3236 		.arl_ops = &b53_arl_ops_95,
3237 	},
3238 	{
3239 		.chip_id = BCM7445_DEVICE_ID,
3240 		.dev_name = "BCM7445",
3241 		.vlans	= 4096,
3242 		.enabled_ports = 0x1ff,
3243 		.arl_bins = 4,
3244 		.arl_buckets = 1024,
3245 		.imp_port = 8,
3246 		.vta_regs = B53_VTA_REGS,
3247 		.duplex_reg = B53_DUPLEX_STAT_GE,
3248 		.jumbo_pm_reg = B53_JUMBO_PORT_MASK,
3249 		.jumbo_size_reg = B53_JUMBO_MAX_SIZE,
3250 		.arl_ops = &b53_arl_ops_95,
3251 	},
3252 	{
3253 		.chip_id = BCM7278_DEVICE_ID,
3254 		.dev_name = "BCM7278",
3255 		.vlans = 4096,
3256 		.enabled_ports = 0x1ff,
3257 		.arl_bins = 4,
3258 		.arl_buckets = 256,
3259 		.imp_port = 8,
3260 		.vta_regs = B53_VTA_REGS,
3261 		.duplex_reg = B53_DUPLEX_STAT_GE,
3262 		.jumbo_pm_reg = B53_JUMBO_PORT_MASK,
3263 		.jumbo_size_reg = B53_JUMBO_MAX_SIZE,
3264 		.arl_ops = &b53_arl_ops_95,
3265 	},
3266 	{
3267 		.chip_id = BCM53134_DEVICE_ID,
3268 		.dev_name = "BCM53134",
3269 		.vlans = 4096,
3270 		.enabled_ports = 0x12f,
3271 		.imp_port = 8,
3272 		.cpu_port = B53_CPU_PORT,
3273 		.vta_regs = B53_VTA_REGS,
3274 		.arl_bins = 4,
3275 		.arl_buckets = 1024,
3276 		.duplex_reg = B53_DUPLEX_STAT_GE,
3277 		.jumbo_pm_reg = B53_JUMBO_PORT_MASK,
3278 		.jumbo_size_reg = B53_JUMBO_MAX_SIZE,
3279 		.arl_ops = &b53_arl_ops_95,
3280 	},
3281 };
3282 
3283 static int b53_switch_init(struct b53_device *dev)
3284 {
3285 	u32 chip_id = dev->chip_id;
3286 	unsigned int i;
3287 
3288 	if (is63xx(dev))
3289 		chip_id = BCM63XX_DEVICE_ID;
3290 
3291 	for (i = 0; i < ARRAY_SIZE(b53_switch_chips); i++) {
3292 		const struct b53_chip_data *chip = &b53_switch_chips[i];
3293 
3294 		if (chip->chip_id == chip_id) {
3295 			if (!dev->enabled_ports)
3296 				dev->enabled_ports = chip->enabled_ports;
3297 			dev->name = chip->dev_name;
3298 			dev->duplex_reg = chip->duplex_reg;
3299 			dev->vta_regs[0] = chip->vta_regs[0];
3300 			dev->vta_regs[1] = chip->vta_regs[1];
3301 			dev->vta_regs[2] = chip->vta_regs[2];
3302 			dev->jumbo_pm_reg = chip->jumbo_pm_reg;
3303 			dev->jumbo_size_reg = chip->jumbo_size_reg;
3304 			dev->imp_port = chip->imp_port;
3305 			dev->num_vlans = chip->vlans;
3306 			dev->num_arl_bins = chip->arl_bins;
3307 			dev->num_arl_buckets = chip->arl_buckets;
3308 			dev->arl_ops = chip->arl_ops;
3309 			break;
3310 		}
3311 	}
3312 
3313 	/* check which BCM5325x version we have */
3314 	if (is5325(dev)) {
3315 		u8 vc4;
3316 
3317 		b53_read8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL4_25, &vc4);
3318 
3319 		/* check reserved bits */
3320 		switch (vc4 & 3) {
3321 		case 1:
3322 			/* BCM5325E */
3323 			break;
3324 		case 3:
3325 			/* BCM5325F - do not use port 4 */
3326 			dev->enabled_ports &= ~BIT(4);
3327 			break;
3328 		default:
3329 /* On the BCM47XX SoCs this is the supported internal switch.*/
3330 #ifndef CONFIG_BCM47XX
3331 			/* BCM5325M */
3332 			return -EINVAL;
3333 #else
3334 			break;
3335 #endif
3336 		}
3337 	}
3338 
3339 	if (is5325e(dev))
3340 		dev->num_arl_buckets = 512;
3341 
3342 	dev->num_ports = fls(dev->enabled_ports);
3343 
3344 	dev->ds->num_ports = min_t(unsigned int, dev->num_ports, DSA_MAX_PORTS);
3345 
3346 	/* Include non standard CPU port built-in PHYs to be probed */
3347 	if (is539x(dev) || is531x5(dev)) {
3348 		for (i = 0; i < dev->num_ports; i++) {
3349 			if (!(dev->ds->phys_mii_mask & BIT(i)) &&
3350 			    !b53_possible_cpu_port(dev->ds, i))
3351 				dev->ds->phys_mii_mask |= BIT(i);
3352 		}
3353 	}
3354 
3355 	dev->ports = devm_kzalloc_objs(dev->dev, struct b53_port,
3356 				       dev->num_ports);
3357 	if (!dev->ports)
3358 		return -ENOMEM;
3359 
3360 	dev->vlans = devm_kzalloc_objs(dev->dev, struct b53_vlan,
3361 				       dev->num_vlans);
3362 	if (!dev->vlans)
3363 		return -ENOMEM;
3364 
3365 	dev->reset_gpio = b53_switch_get_reset_gpio(dev);
3366 
3367 	if (PTR_ERR(dev->reset_gpio) == -EPROBE_DEFER)
3368 		return -EPROBE_DEFER;
3369 
3370 	return 0;
3371 }
3372 
3373 struct b53_device *b53_switch_alloc(struct device *base,
3374 				    const struct b53_io_ops *ops,
3375 				    void *priv)
3376 {
3377 	struct dsa_switch *ds;
3378 	struct b53_device *dev;
3379 
3380 	ds = devm_kzalloc_obj(base, *ds);
3381 	if (!ds)
3382 		return NULL;
3383 
3384 	ds->dev = base;
3385 
3386 	dev = devm_kzalloc_obj(base, *dev);
3387 	if (!dev)
3388 		return NULL;
3389 
3390 	ds->priv = dev;
3391 	dev->dev = base;
3392 
3393 	dev->ds = ds;
3394 	dev->priv = priv;
3395 	dev->ops = ops;
3396 	ds->ops = &b53_switch_ops;
3397 	ds->phylink_mac_ops = &b53_phylink_mac_ops;
3398 	dev->vlan_enabled = true;
3399 	dev->vlan_filtering = false;
3400 	/* Let DSA handle the case were multiple bridges span the same switch
3401 	 * device and different VLAN awareness settings are requested, which
3402 	 * would be breaking filtering semantics for any of the other bridge
3403 	 * devices. (not hardware supported)
3404 	 */
3405 	ds->vlan_filtering_is_global = true;
3406 
3407 	mutex_init(&dev->reg_mutex);
3408 	mutex_init(&dev->stats_mutex);
3409 	mutex_init(&dev->arl_mutex);
3410 
3411 	return dev;
3412 }
3413 EXPORT_SYMBOL(b53_switch_alloc);
3414 
3415 int b53_switch_detect(struct b53_device *dev)
3416 {
3417 	u32 id32;
3418 	u16 tmp;
3419 	u8 id8;
3420 	int ret;
3421 
3422 	ret = b53_read8(dev, B53_MGMT_PAGE, B53_DEVICE_ID, &id8);
3423 	if (ret)
3424 		return ret;
3425 
3426 	switch (id8) {
3427 	case 0:
3428 		/* BCM5325 and BCM5365 do not have this register so reads
3429 		 * return 0. But the read operation did succeed, so assume this
3430 		 * is one of them.
3431 		 *
3432 		 * Next check if we can write to the 5325's VTA register; for
3433 		 * 5365 it is read only.
3434 		 */
3435 		b53_write16(dev, B53_VLAN_PAGE, B53_VLAN_TABLE_ACCESS_25, 0xf);
3436 		b53_read16(dev, B53_VLAN_PAGE, B53_VLAN_TABLE_ACCESS_25, &tmp);
3437 
3438 		if (tmp == 0xf) {
3439 			u32 phy_id;
3440 			int val;
3441 
3442 			dev->chip_id = BCM5325_DEVICE_ID;
3443 
3444 			val = b53_phy_read16(dev->ds, 0, MII_PHYSID1);
3445 			phy_id = (val & 0xffff) << 16;
3446 			val = b53_phy_read16(dev->ds, 0, MII_PHYSID2);
3447 			phy_id |= (val & 0xfff0);
3448 
3449 			if (phy_id == 0x00406330)
3450 				dev->variant_id = B53_VARIANT_5325M;
3451 			else if (phy_id == 0x0143bc30)
3452 				dev->variant_id = B53_VARIANT_5325E;
3453 		} else {
3454 			dev->chip_id = BCM5365_DEVICE_ID;
3455 		}
3456 		break;
3457 	case BCM5389_DEVICE_ID:
3458 	case BCM5395_DEVICE_ID:
3459 	case BCM5397_DEVICE_ID:
3460 	case BCM5398_DEVICE_ID:
3461 		dev->chip_id = id8;
3462 		break;
3463 	default:
3464 		ret = b53_read32(dev, B53_MGMT_PAGE, B53_DEVICE_ID, &id32);
3465 		if (ret)
3466 			return ret;
3467 
3468 		switch (id32) {
3469 		case BCM53101_DEVICE_ID:
3470 		case BCM53115_DEVICE_ID:
3471 		case BCM53125_DEVICE_ID:
3472 		case BCM53128_DEVICE_ID:
3473 		case BCM53010_DEVICE_ID:
3474 		case BCM53011_DEVICE_ID:
3475 		case BCM53012_DEVICE_ID:
3476 		case BCM53018_DEVICE_ID:
3477 		case BCM53019_DEVICE_ID:
3478 		case BCM53134_DEVICE_ID:
3479 			dev->chip_id = id32;
3480 			break;
3481 		default:
3482 			dev_err(dev->dev,
3483 				"unsupported switch detected (BCM53%02x/BCM%x)\n",
3484 				id8, id32);
3485 			return -ENODEV;
3486 		}
3487 	}
3488 
3489 	if (dev->chip_id == BCM5325_DEVICE_ID)
3490 		return b53_read8(dev, B53_STAT_PAGE, B53_REV_ID_25,
3491 				 &dev->core_rev);
3492 	else
3493 		return b53_read8(dev, B53_MGMT_PAGE, B53_REV_ID,
3494 				 &dev->core_rev);
3495 }
3496 EXPORT_SYMBOL(b53_switch_detect);
3497 
3498 int b53_switch_register(struct b53_device *dev)
3499 {
3500 	int ret;
3501 
3502 	if (dev->pdata) {
3503 		dev->chip_id = dev->pdata->chip_id;
3504 		dev->enabled_ports = dev->pdata->enabled_ports;
3505 	}
3506 
3507 	if (!dev->chip_id && b53_switch_detect(dev))
3508 		return -EINVAL;
3509 
3510 	ret = b53_switch_init(dev);
3511 	if (ret)
3512 		return ret;
3513 
3514 	dev_info(dev->dev, "found switch: %s, rev %i\n",
3515 		 dev->name, dev->core_rev);
3516 
3517 	return dsa_register_switch(dev->ds);
3518 }
3519 EXPORT_SYMBOL(b53_switch_register);
3520 
3521 MODULE_AUTHOR("Jonas Gorski <jogo@openwrt.org>");
3522 MODULE_DESCRIPTION("B53 switch library");
3523 MODULE_LICENSE("Dual BSD/GPL");
3524