2 * B53 switch driver main logic
4 * Copyright (C) 2011-2013 Jonas Gorski <jogo@openwrt.org>
5 * Copyright (C) 2016 Florian Fainelli <f.fainelli@gmail.com>
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.
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.
20 #include <linux/delay.h>
21 #include <linux/export.h>
22 #include <linux/gpio.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>
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" },
80 #define B53_MIBS_65_SIZE ARRAY_SIZE(b53_mibs_65)
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" },
128 #define B53_MIBS_63XX_SIZE ARRAY_SIZE(b53_mibs_63xx)
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" },
169 #define B53_MIBS_SIZE ARRAY_SIZE(b53_mibs)
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" },
228 #define B53_MIBS_58XX_SIZE ARRAY_SIZE(b53_mibs_58xx)
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)
233 static int b53_do_vlan_op(struct b53_device *dev, u8 op)
237 b53_write8(dev, B53_ARLIO_PAGE, dev->vta_regs[0], VTA_START_CMD | op);
239 for (i = 0; i < 10; i++) {
242 b53_read8(dev, B53_ARLIO_PAGE, dev->vta_regs[0], &vta);
243 if (!(vta & VTA_START_CMD))
246 usleep_range(100, 200);
252 static void b53_set_vlan_entry(struct b53_device *dev, u16 vid,
253 struct b53_vlan *vlan)
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;
264 entry |= VA_VALID_25;
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)) {
274 entry = ((vlan->untag & VA_UNTAG_MASK_65) <<
275 VA_UNTAG_S_65) | vlan->members | VA_VALID_65;
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);
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);
285 b53_do_vlan_op(dev, VTA_CMD_WRITE);
288 dev_dbg(dev->ds->dev, "VID: %d, members: 0x%04x, untag: 0x%04x\n",
289 vid, vlan->members, vlan->untag);
292 static void b53_get_vlan_entry(struct b53_device *dev, u16 vid,
293 struct b53_vlan *vlan)
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);
302 if (dev->core_rev >= 3)
303 vlan->valid = !!(entry & VA_VALID_25_R4);
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;
309 } else if (is5365(dev)) {
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);
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;
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;
331 static void b53_set_eap_mode(struct b53_device *dev, int port, int mode)
335 if (is5325(dev) || is5365(dev) || dev->chip_id == BCM5389_DEVICE_ID)
338 b53_read64(dev, B53_EAP_PAGE, B53_PORT_EAP_CONF(port), &eap_conf);
341 eap_conf &= ~EAP_MODE_MASK_63XX;
342 eap_conf |= (u64)mode << EAP_MODE_SHIFT_63XX;
344 eap_conf &= ~EAP_MODE_MASK;
345 eap_conf |= (u64)mode << EAP_MODE_SHIFT;
348 b53_write64(dev, B53_EAP_PAGE, B53_PORT_EAP_CONF(port), eap_conf);
351 static void b53_set_forwarding(struct b53_device *dev, int enable)
355 b53_read8(dev, B53_CTRL_PAGE, B53_SWITCH_MODE, &mgmt);
358 mgmt |= SM_SW_FWD_EN;
360 mgmt &= ~SM_SW_FWD_EN;
362 b53_write8(dev, B53_CTRL_PAGE, B53_SWITCH_MODE, mgmt);
364 /* 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);
370 /* Look at B53_UC_FWD_EN and B53_MC_FWD_EN to decide whether
371 * frames should be flooded or not.
373 b53_read8(dev, B53_CTRL_PAGE, B53_IP_MULTICAST_CTRL, &mgmt);
374 mgmt |= B53_UC_FWD_EN | B53_MC_FWD_EN | B53_IPMC_FWD_EN;
375 b53_write8(dev, B53_CTRL_PAGE, B53_IP_MULTICAST_CTRL, mgmt);
378 static void b53_enable_vlan(struct b53_device *dev, int port, bool enable,
379 bool enable_filtering)
381 u8 mgmt, vc0, vc1, vc4 = 0, vc5;
383 b53_read8(dev, B53_CTRL_PAGE, B53_SWITCH_MODE, &mgmt);
384 b53_read8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL0, &vc0);
385 b53_read8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL1, &vc1);
387 if (is5325(dev) || is5365(dev)) {
388 b53_read8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL4_25, &vc4);
389 b53_read8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL5_25, &vc5);
390 } else if (is63xx(dev)) {
391 b53_read8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL4_63XX, &vc4);
392 b53_read8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL5_63XX, &vc5);
394 b53_read8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL4, &vc4);
395 b53_read8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL5, &vc5);
398 vc1 &= ~VC1_RX_MCST_FWD_EN;
401 vc0 |= VC0_VLAN_EN | VC0_VID_CHK_EN | VC0_VID_HASH_VID;
402 vc1 |= VC1_RX_MCST_UNTAG_EN;
403 vc4 &= ~VC4_ING_VID_CHECK_MASK;
404 if (enable_filtering) {
405 vc4 |= VC4_ING_VID_VIO_DROP << VC4_ING_VID_CHECK_S;
406 vc5 |= VC5_DROP_VTABLE_MISS;
408 vc4 |= VC4_NO_ING_VID_CHK << VC4_ING_VID_CHECK_S;
409 vc5 &= ~VC5_DROP_VTABLE_MISS;
413 vc0 &= ~VC0_RESERVED_1;
415 if (is5325(dev) || is5365(dev))
416 vc1 |= VC1_RX_MCST_TAG_EN;
419 vc0 &= ~(VC0_VLAN_EN | VC0_VID_CHK_EN | VC0_VID_HASH_VID);
420 vc1 &= ~VC1_RX_MCST_UNTAG_EN;
421 vc4 &= ~VC4_ING_VID_CHECK_MASK;
422 vc5 &= ~VC5_DROP_VTABLE_MISS;
424 if (is5325(dev) || is5365(dev))
425 vc4 |= VC4_ING_VID_VIO_FWD << VC4_ING_VID_CHECK_S;
427 vc4 |= VC4_ING_VID_VIO_TO_IMP << VC4_ING_VID_CHECK_S;
429 if (is5325(dev) || is5365(dev))
430 vc1 &= ~VC1_RX_MCST_TAG_EN;
433 if (!is5325(dev) && !is5365(dev))
434 vc5 &= ~VC5_VID_FFF_EN;
436 b53_write8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL0, vc0);
437 b53_write8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL1, vc1);
439 if (is5325(dev) || is5365(dev)) {
440 /* enable the high 8 bit vid check on 5325 */
441 if (is5325(dev) && enable)
442 b53_write8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL3,
445 b53_write8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL3, 0);
447 b53_write8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL4_25, vc4);
448 b53_write8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL5_25, vc5);
449 } else if (is63xx(dev)) {
450 b53_write16(dev, B53_VLAN_PAGE, B53_VLAN_CTRL3_63XX, 0);
451 b53_write8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL4_63XX, vc4);
452 b53_write8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL5_63XX, vc5);
454 b53_write16(dev, B53_VLAN_PAGE, B53_VLAN_CTRL3, 0);
455 b53_write8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL4, vc4);
456 b53_write8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL5, vc5);
459 b53_write8(dev, B53_CTRL_PAGE, B53_SWITCH_MODE, mgmt);
461 dev->vlan_enabled = enable;
463 dev_dbg(dev->dev, "Port %d VLAN enabled: %d, filtering: %d\n",
464 port, enable, enable_filtering);
467 static int b53_set_jumbo(struct b53_device *dev, bool enable, bool allow_10_100)
470 u16 max_size = JMS_MIN_SIZE;
472 if (is5325(dev) || is5365(dev))
476 port_mask = dev->enabled_ports;
477 max_size = JMS_MAX_SIZE;
479 port_mask |= JPM_10_100_JUMBO_EN;
482 b53_write32(dev, B53_JUMBO_PAGE, dev->jumbo_pm_reg, port_mask);
483 return b53_write16(dev, B53_JUMBO_PAGE, dev->jumbo_size_reg, max_size);
486 static int b53_flush_arl(struct b53_device *dev, u8 mask)
490 b53_write8(dev, B53_CTRL_PAGE, B53_FAST_AGE_CTRL,
491 FAST_AGE_DONE | FAST_AGE_DYNAMIC | mask);
493 for (i = 0; i < 10; i++) {
496 b53_read8(dev, B53_CTRL_PAGE, B53_FAST_AGE_CTRL,
499 if (!(fast_age_ctrl & FAST_AGE_DONE))
507 /* Only age dynamic entries (default behavior) */
508 b53_write8(dev, B53_CTRL_PAGE, B53_FAST_AGE_CTRL, FAST_AGE_DYNAMIC);
512 static int b53_fast_age_port(struct b53_device *dev, int port)
514 b53_write8(dev, B53_CTRL_PAGE, B53_FAST_AGE_PORT_CTRL, port);
516 return b53_flush_arl(dev, FAST_AGE_PORT);
519 static int b53_fast_age_vlan(struct b53_device *dev, u16 vid)
521 b53_write16(dev, B53_CTRL_PAGE, B53_FAST_AGE_VID_CTRL, vid);
523 return b53_flush_arl(dev, FAST_AGE_VLAN);
526 void b53_imp_vlan_setup(struct dsa_switch *ds, int cpu_port)
528 struct b53_device *dev = ds->priv;
532 /* Enable the IMP port to be in the same VLAN as the other ports
533 * on a per-port basis such that we only have Port i and IMP in
536 b53_for_each_port(dev, i) {
537 b53_read16(dev, B53_PVLAN_PAGE, B53_PVLAN_PORT_MASK(i), &pvlan);
538 pvlan |= BIT(cpu_port);
539 b53_write16(dev, B53_PVLAN_PAGE, B53_PVLAN_PORT_MASK(i), pvlan);
542 EXPORT_SYMBOL(b53_imp_vlan_setup);
544 static void b53_port_set_ucast_flood(struct b53_device *dev, int port,
549 b53_read16(dev, B53_CTRL_PAGE, B53_UC_FLOOD_MASK, &uc);
554 b53_write16(dev, B53_CTRL_PAGE, B53_UC_FLOOD_MASK, uc);
557 static void b53_port_set_mcast_flood(struct b53_device *dev, int port,
562 b53_read16(dev, B53_CTRL_PAGE, B53_MC_FLOOD_MASK, &mc);
567 b53_write16(dev, B53_CTRL_PAGE, B53_MC_FLOOD_MASK, mc);
569 b53_read16(dev, B53_CTRL_PAGE, B53_IPMC_FLOOD_MASK, &mc);
574 b53_write16(dev, B53_CTRL_PAGE, B53_IPMC_FLOOD_MASK, mc);
577 static void b53_port_set_learning(struct b53_device *dev, int port,
582 b53_read16(dev, B53_CTRL_PAGE, B53_DIS_LEARNING, ®);
587 b53_write16(dev, B53_CTRL_PAGE, B53_DIS_LEARNING, reg);
590 static void b53_eee_enable_set(struct dsa_switch *ds, int port, bool enable)
592 struct b53_device *dev = ds->priv;
595 b53_read16(dev, B53_EEE_PAGE, B53_EEE_EN_CTRL, ®);
600 b53_write16(dev, B53_EEE_PAGE, B53_EEE_EN_CTRL, reg);
603 int b53_setup_port(struct dsa_switch *ds, int port)
605 struct b53_device *dev = ds->priv;
607 b53_port_set_ucast_flood(dev, port, true);
608 b53_port_set_mcast_flood(dev, port, true);
609 b53_port_set_learning(dev, port, false);
611 /* Force all traffic to go to the CPU port to prevent the ASIC from
612 * trying to forward to bridged ports on matching FDB entries, then
613 * dropping frames because it isn't allowed to forward there.
615 if (dsa_is_user_port(ds, port))
616 b53_set_eap_mode(dev, port, EAP_MODE_SIMPLIFIED);
620 EXPORT_SYMBOL(b53_setup_port);
622 int b53_enable_port(struct dsa_switch *ds, int port, struct phy_device *phy)
624 struct b53_device *dev = ds->priv;
625 unsigned int cpu_port;
629 if (!dsa_is_user_port(ds, port))
632 cpu_port = dsa_to_port(ds, port)->cpu_dp->index;
634 if (dev->ops->irq_enable)
635 ret = dev->ops->irq_enable(dev, port);
639 /* Clear the Rx and Tx disable bits and set to no spanning tree */
640 b53_write8(dev, B53_CTRL_PAGE, B53_PORT_CTRL(port), 0);
642 /* Set this port, and only this one to be in the default VLAN,
643 * if member of a bridge, restore its membership prior to
644 * bringing down this port.
646 b53_read16(dev, B53_PVLAN_PAGE, B53_PVLAN_PORT_MASK(port), &pvlan);
649 pvlan |= dev->ports[port].vlan_ctl_mask;
650 b53_write16(dev, B53_PVLAN_PAGE, B53_PVLAN_PORT_MASK(port), pvlan);
652 b53_imp_vlan_setup(ds, cpu_port);
654 /* If EEE was enabled, restore it */
655 if (dev->ports[port].eee.eee_enabled)
656 b53_eee_enable_set(ds, port, true);
660 EXPORT_SYMBOL(b53_enable_port);
662 void b53_disable_port(struct dsa_switch *ds, int port)
664 struct b53_device *dev = ds->priv;
667 /* Disable Tx/Rx for the port */
668 b53_read8(dev, B53_CTRL_PAGE, B53_PORT_CTRL(port), ®);
669 reg |= PORT_CTRL_RX_DISABLE | PORT_CTRL_TX_DISABLE;
670 b53_write8(dev, B53_CTRL_PAGE, B53_PORT_CTRL(port), reg);
672 if (dev->ops->irq_disable)
673 dev->ops->irq_disable(dev, port);
675 EXPORT_SYMBOL(b53_disable_port);
677 void b53_brcm_hdr_setup(struct dsa_switch *ds, int port)
679 struct b53_device *dev = ds->priv;
680 bool tag_en = !(dev->tag_protocol == DSA_TAG_PROTO_NONE);
684 /* Resolve which bit controls the Broadcom tag */
687 val = BRCM_HDR_P8_EN;
690 val = BRCM_HDR_P7_EN;
693 val = BRCM_HDR_P5_EN;
700 /* Enable management mode if tagging is requested */
701 b53_read8(dev, B53_CTRL_PAGE, B53_SWITCH_MODE, &hdr_ctl);
703 hdr_ctl |= SM_SW_FWD_MODE;
705 hdr_ctl &= ~SM_SW_FWD_MODE;
706 b53_write8(dev, B53_CTRL_PAGE, B53_SWITCH_MODE, hdr_ctl);
708 /* Configure the appropriate IMP port */
709 b53_read8(dev, B53_MGMT_PAGE, B53_GLOBAL_CONFIG, &hdr_ctl);
711 hdr_ctl |= GC_FRM_MGMT_PORT_MII;
713 hdr_ctl |= GC_FRM_MGMT_PORT_M;
714 b53_write8(dev, B53_MGMT_PAGE, B53_GLOBAL_CONFIG, hdr_ctl);
716 /* Enable Broadcom tags for IMP port */
717 b53_read8(dev, B53_MGMT_PAGE, B53_BRCM_HDR, &hdr_ctl);
722 b53_write8(dev, B53_MGMT_PAGE, B53_BRCM_HDR, hdr_ctl);
724 /* Registers below are only accessible on newer devices */
728 /* Enable reception Broadcom tag for CPU TX (switch RX) to
729 * allow us to tag outgoing frames
731 b53_read16(dev, B53_MGMT_PAGE, B53_BRCM_HDR_RX_DIS, ®);
736 b53_write16(dev, B53_MGMT_PAGE, B53_BRCM_HDR_RX_DIS, reg);
738 /* Enable transmission of Broadcom tags from the switch (CPU RX) to
739 * allow delivering frames to the per-port net_devices
741 b53_read16(dev, B53_MGMT_PAGE, B53_BRCM_HDR_TX_DIS, ®);
746 b53_write16(dev, B53_MGMT_PAGE, B53_BRCM_HDR_TX_DIS, reg);
748 EXPORT_SYMBOL(b53_brcm_hdr_setup);
750 static void b53_enable_cpu_port(struct b53_device *dev, int port)
754 /* BCM5325 CPU port is at 8 */
755 if ((is5325(dev) || is5365(dev)) && port == B53_CPU_PORT_25)
758 port_ctrl = PORT_CTRL_RX_BCST_EN |
759 PORT_CTRL_RX_MCST_EN |
760 PORT_CTRL_RX_UCST_EN;
761 b53_write8(dev, B53_CTRL_PAGE, B53_PORT_CTRL(port), port_ctrl);
763 b53_brcm_hdr_setup(dev->ds, port);
766 static void b53_enable_mib(struct b53_device *dev)
770 b53_read8(dev, B53_MGMT_PAGE, B53_GLOBAL_CONFIG, &gc);
771 gc &= ~(GC_RESET_MIB | GC_MIB_AC_EN);
772 b53_write8(dev, B53_MGMT_PAGE, B53_GLOBAL_CONFIG, gc);
775 static void b53_enable_stp(struct b53_device *dev)
779 b53_read8(dev, B53_MGMT_PAGE, B53_GLOBAL_CONFIG, &gc);
781 b53_write8(dev, B53_MGMT_PAGE, B53_GLOBAL_CONFIG, gc);
784 static u16 b53_default_pvid(struct b53_device *dev)
786 if (is5325(dev) || is5365(dev))
792 static bool b53_vlan_port_needs_forced_tagged(struct dsa_switch *ds, int port)
794 struct b53_device *dev = ds->priv;
796 return dev->tag_protocol == DSA_TAG_PROTO_NONE && dsa_is_cpu_port(ds, port);
799 static bool b53_vlan_port_may_join_untagged(struct dsa_switch *ds, int port)
801 struct b53_device *dev = ds->priv;
804 if (!dev->vlan_filtering)
807 dp = dsa_to_port(ds, port);
809 if (dsa_port_is_cpu(dp))
812 return dp->bridge == NULL;
815 int b53_configure_vlan(struct dsa_switch *ds)
817 struct b53_device *dev = ds->priv;
818 struct b53_vlan vl = { 0 };
823 def_vid = b53_default_pvid(dev);
825 /* clear all vlan entries */
826 if (is5325(dev) || is5365(dev)) {
827 for (i = def_vid; i < dev->num_vlans; i++)
828 b53_set_vlan_entry(dev, i, &vl);
830 b53_do_vlan_op(dev, VTA_CMD_CLEAR);
833 b53_enable_vlan(dev, -1, dev->vlan_enabled, dev->vlan_filtering);
835 /* Create an untagged VLAN entry for the default PVID in case
836 * CONFIG_VLAN_8021Q is disabled and there are no calls to
837 * dsa_user_vlan_rx_add_vid() to create the default VLAN
838 * entry. Do this only when the tagging protocol is not
841 v = &dev->vlans[def_vid];
842 b53_for_each_port(dev, i) {
843 if (!b53_vlan_port_may_join_untagged(ds, i))
846 vl.members |= BIT(i);
847 if (!b53_vlan_port_needs_forced_tagged(ds, i))
848 vl.untag = vl.members;
849 b53_write16(dev, B53_VLAN_PAGE, B53_VLAN_PORT_DEF_TAG(i),
852 b53_set_vlan_entry(dev, def_vid, &vl);
854 if (dev->vlan_filtering) {
855 /* Upon initial call we have not set-up any VLANs, but upon
856 * system resume, we need to restore all VLAN entries.
858 for (vid = def_vid + 1; vid < dev->num_vlans; vid++) {
859 v = &dev->vlans[vid];
864 b53_set_vlan_entry(dev, vid, v);
865 b53_fast_age_vlan(dev, vid);
868 b53_for_each_port(dev, i) {
869 if (!dsa_is_cpu_port(ds, i))
870 b53_write16(dev, B53_VLAN_PAGE,
871 B53_VLAN_PORT_DEF_TAG(i),
878 EXPORT_SYMBOL(b53_configure_vlan);
880 static void b53_switch_reset_gpio(struct b53_device *dev)
882 int gpio = dev->reset_gpio;
887 /* Reset sequence: RESET low(50ms)->high(20ms)
889 gpio_set_value(gpio, 0);
892 gpio_set_value(gpio, 1);
895 dev->current_page = 0xff;
898 static int b53_switch_reset(struct b53_device *dev)
900 unsigned int timeout = 1000;
903 b53_switch_reset_gpio(dev);
906 b53_write8(dev, B53_CTRL_PAGE, B53_SOFTRESET, 0x83);
907 b53_write8(dev, B53_CTRL_PAGE, B53_SOFTRESET, 0x00);
910 /* This is specific to 58xx devices here, do not use is58xx() which
911 * covers the larger Starfigther 2 family, including 7445/7278 which
912 * still use this driver as a library and need to perform the reset
915 if (dev->chip_id == BCM58XX_DEVICE_ID ||
916 dev->chip_id == BCM583XX_DEVICE_ID) {
917 b53_read8(dev, B53_CTRL_PAGE, B53_SOFTRESET, ®);
918 reg |= SW_RST | EN_SW_RST | EN_CH_RST;
919 b53_write8(dev, B53_CTRL_PAGE, B53_SOFTRESET, reg);
922 b53_read8(dev, B53_CTRL_PAGE, B53_SOFTRESET, ®);
926 usleep_range(1000, 2000);
927 } while (timeout-- > 0);
931 "Timeout waiting for SW_RST to clear!\n");
936 b53_read8(dev, B53_CTRL_PAGE, B53_SWITCH_MODE, &mgmt);
938 if (!(mgmt & SM_SW_FWD_EN)) {
939 mgmt &= ~SM_SW_FWD_MODE;
940 mgmt |= SM_SW_FWD_EN;
942 b53_write8(dev, B53_CTRL_PAGE, B53_SWITCH_MODE, mgmt);
943 b53_read8(dev, B53_CTRL_PAGE, B53_SWITCH_MODE, &mgmt);
945 if (!(mgmt & SM_SW_FWD_EN)) {
946 dev_err(dev->dev, "Failed to enable switch!\n");
954 return b53_flush_arl(dev, FAST_AGE_STATIC);
957 static int b53_phy_read16(struct dsa_switch *ds, int addr, int reg)
959 struct b53_device *priv = ds->priv;
963 if (priv->ops->phy_read16)
964 ret = priv->ops->phy_read16(priv, addr, reg, &value);
966 ret = b53_read16(priv, B53_PORT_MII_PAGE(addr),
969 return ret ? ret : value;
972 static int b53_phy_write16(struct dsa_switch *ds, int addr, int reg, u16 val)
974 struct b53_device *priv = ds->priv;
976 if (priv->ops->phy_write16)
977 return priv->ops->phy_write16(priv, addr, reg, val);
979 return b53_write16(priv, B53_PORT_MII_PAGE(addr), reg * 2, val);
982 static int b53_reset_switch(struct b53_device *priv)
985 memset(priv->vlans, 0, sizeof(*priv->vlans) * priv->num_vlans);
986 memset(priv->ports, 0, sizeof(*priv->ports) * priv->num_ports);
988 priv->serdes_lane = B53_INVALID_LANE;
990 return b53_switch_reset(priv);
993 static int b53_apply_config(struct b53_device *priv)
995 /* disable switching */
996 b53_set_forwarding(priv, 0);
998 b53_configure_vlan(priv->ds);
1000 /* enable switching */
1001 b53_set_forwarding(priv, 1);
1006 static void b53_reset_mib(struct b53_device *priv)
1010 b53_read8(priv, B53_MGMT_PAGE, B53_GLOBAL_CONFIG, &gc);
1012 b53_write8(priv, B53_MGMT_PAGE, B53_GLOBAL_CONFIG, gc | GC_RESET_MIB);
1014 b53_write8(priv, B53_MGMT_PAGE, B53_GLOBAL_CONFIG, gc & ~GC_RESET_MIB);
1018 static const struct b53_mib_desc *b53_get_mib(struct b53_device *dev)
1022 else if (is63xx(dev))
1023 return b53_mibs_63xx;
1024 else if (is58xx(dev))
1025 return b53_mibs_58xx;
1030 static unsigned int b53_get_mib_size(struct b53_device *dev)
1033 return B53_MIBS_65_SIZE;
1034 else if (is63xx(dev))
1035 return B53_MIBS_63XX_SIZE;
1036 else if (is58xx(dev))
1037 return B53_MIBS_58XX_SIZE;
1039 return B53_MIBS_SIZE;
1042 static struct phy_device *b53_get_phy_device(struct dsa_switch *ds, int port)
1044 /* These ports typically do not have built-in PHYs */
1046 case B53_CPU_PORT_25:
1052 return mdiobus_get_phy(ds->user_mii_bus, port);
1055 void b53_get_strings(struct dsa_switch *ds, int port, u32 stringset,
1058 struct b53_device *dev = ds->priv;
1059 const struct b53_mib_desc *mibs = b53_get_mib(dev);
1060 unsigned int mib_size = b53_get_mib_size(dev);
1061 struct phy_device *phydev;
1064 if (stringset == ETH_SS_STATS) {
1065 for (i = 0; i < mib_size; i++)
1066 ethtool_puts(&data, mibs[i].name);
1067 } else if (stringset == ETH_SS_PHY_STATS) {
1068 phydev = b53_get_phy_device(ds, port);
1072 phy_ethtool_get_strings(phydev, data);
1075 EXPORT_SYMBOL(b53_get_strings);
1077 void b53_get_ethtool_stats(struct dsa_switch *ds, int port, uint64_t *data)
1079 struct b53_device *dev = ds->priv;
1080 const struct b53_mib_desc *mibs = b53_get_mib(dev);
1081 unsigned int mib_size = b53_get_mib_size(dev);
1082 const struct b53_mib_desc *s;
1086 if (is5365(dev) && port == 5)
1089 mutex_lock(&dev->stats_mutex);
1091 for (i = 0; i < mib_size; i++) {
1095 b53_read64(dev, B53_MIB_PAGE(port), s->offset, &val);
1099 b53_read32(dev, B53_MIB_PAGE(port), s->offset,
1106 mutex_unlock(&dev->stats_mutex);
1108 EXPORT_SYMBOL(b53_get_ethtool_stats);
1110 void b53_get_ethtool_phy_stats(struct dsa_switch *ds, int port, uint64_t *data)
1112 struct phy_device *phydev;
1114 phydev = b53_get_phy_device(ds, port);
1118 phy_ethtool_get_stats(phydev, NULL, data);
1120 EXPORT_SYMBOL(b53_get_ethtool_phy_stats);
1122 int b53_get_sset_count(struct dsa_switch *ds, int port, int sset)
1124 struct b53_device *dev = ds->priv;
1125 struct phy_device *phydev;
1127 if (sset == ETH_SS_STATS) {
1128 return b53_get_mib_size(dev);
1129 } else if (sset == ETH_SS_PHY_STATS) {
1130 phydev = b53_get_phy_device(ds, port);
1134 return phy_ethtool_get_sset_count(phydev);
1139 EXPORT_SYMBOL(b53_get_sset_count);
1141 enum b53_devlink_resource_id {
1142 B53_DEVLINK_PARAM_ID_VLAN_TABLE,
1145 static u64 b53_devlink_vlan_table_get(void *priv)
1147 struct b53_device *dev = priv;
1148 struct b53_vlan *vl;
1152 for (i = 0; i < dev->num_vlans; i++) {
1153 vl = &dev->vlans[i];
1161 int b53_setup_devlink_resources(struct dsa_switch *ds)
1163 struct devlink_resource_size_params size_params;
1164 struct b53_device *dev = ds->priv;
1167 devlink_resource_size_params_init(&size_params, dev->num_vlans,
1169 1, DEVLINK_RESOURCE_UNIT_ENTRY);
1171 err = dsa_devlink_resource_register(ds, "VLAN", dev->num_vlans,
1172 B53_DEVLINK_PARAM_ID_VLAN_TABLE,
1173 DEVLINK_RESOURCE_ID_PARENT_TOP,
1178 dsa_devlink_resource_occ_get_register(ds,
1179 B53_DEVLINK_PARAM_ID_VLAN_TABLE,
1180 b53_devlink_vlan_table_get, dev);
1184 dsa_devlink_resources_unregister(ds);
1187 EXPORT_SYMBOL(b53_setup_devlink_resources);
1189 static int b53_setup(struct dsa_switch *ds)
1191 struct b53_device *dev = ds->priv;
1192 struct b53_vlan *vl;
1197 /* Request bridge PVID untagged when DSA_TAG_PROTO_NONE is set
1198 * which forces the CPU port to be tagged in all VLANs.
1200 ds->untag_bridge_pvid = dev->tag_protocol == DSA_TAG_PROTO_NONE;
1202 /* The switch does not tell us the original VLAN for untagged
1203 * packets, so keep the CPU port always tagged.
1205 ds->untag_vlan_aware_bridge_pvid = true;
1207 /* Ageing time is set in seconds */
1208 ds->ageing_time_min = 1 * 1000;
1209 ds->ageing_time_max = AGE_TIME_MAX * 1000;
1211 ret = b53_reset_switch(dev);
1213 dev_err(ds->dev, "failed to reset switch\n");
1217 /* setup default vlan for filtering mode */
1218 pvid = b53_default_pvid(dev);
1219 vl = &dev->vlans[pvid];
1220 b53_for_each_port(dev, port) {
1221 vl->members |= BIT(port);
1222 if (!b53_vlan_port_needs_forced_tagged(ds, port))
1223 vl->untag |= BIT(port);
1228 ret = b53_apply_config(dev);
1230 dev_err(ds->dev, "failed to apply configuration\n");
1234 /* Configure IMP/CPU port, disable all other ports. Enabled
1235 * ports will be configured with .port_enable
1237 for (port = 0; port < dev->num_ports; port++) {
1238 if (dsa_is_cpu_port(ds, port))
1239 b53_enable_cpu_port(dev, port);
1241 b53_disable_port(ds, port);
1244 return b53_setup_devlink_resources(ds);
1247 static void b53_teardown(struct dsa_switch *ds)
1249 dsa_devlink_resources_unregister(ds);
1252 static void b53_force_link(struct b53_device *dev, int port, int link)
1256 /* Override the port settings */
1257 if (port == dev->imp_port) {
1258 off = B53_PORT_OVERRIDE_CTRL;
1259 val = PORT_OVERRIDE_EN;
1261 off = B53_GMII_PORT_OVERRIDE_CTRL(port);
1265 b53_read8(dev, B53_CTRL_PAGE, off, ®);
1268 reg |= PORT_OVERRIDE_LINK;
1270 reg &= ~PORT_OVERRIDE_LINK;
1271 b53_write8(dev, B53_CTRL_PAGE, off, reg);
1274 static void b53_force_port_config(struct b53_device *dev, int port,
1275 int speed, int duplex,
1276 bool tx_pause, bool rx_pause)
1280 /* Override the port settings */
1281 if (port == dev->imp_port) {
1282 off = B53_PORT_OVERRIDE_CTRL;
1283 val = PORT_OVERRIDE_EN;
1285 off = B53_GMII_PORT_OVERRIDE_CTRL(port);
1289 b53_read8(dev, B53_CTRL_PAGE, off, ®);
1291 if (duplex == DUPLEX_FULL)
1292 reg |= PORT_OVERRIDE_FULL_DUPLEX;
1294 reg &= ~PORT_OVERRIDE_FULL_DUPLEX;
1298 reg |= PORT_OVERRIDE_SPEED_2000M;
1301 reg |= PORT_OVERRIDE_SPEED_1000M;
1304 reg |= PORT_OVERRIDE_SPEED_100M;
1307 reg |= PORT_OVERRIDE_SPEED_10M;
1310 dev_err(dev->dev, "unknown speed: %d\n", speed);
1315 reg |= PORT_OVERRIDE_RX_FLOW;
1317 reg |= PORT_OVERRIDE_TX_FLOW;
1319 b53_write8(dev, B53_CTRL_PAGE, off, reg);
1322 static void b53_adjust_63xx_rgmii(struct dsa_switch *ds, int port,
1323 phy_interface_t interface)
1325 struct b53_device *dev = ds->priv;
1328 b53_read8(dev, B53_CTRL_PAGE, B53_RGMII_CTRL_P(port), &rgmii_ctrl);
1329 rgmii_ctrl &= ~(RGMII_CTRL_DLL_RXC | RGMII_CTRL_DLL_TXC);
1332 rgmii_ctrl |= RGMII_CTRL_MII_OVERRIDE;
1334 rgmii_ctrl |= RGMII_CTRL_ENABLE_GMII;
1336 b53_write8(dev, B53_CTRL_PAGE, B53_RGMII_CTRL_P(port), rgmii_ctrl);
1338 dev_dbg(ds->dev, "Configured port %d for %s\n", port,
1339 phy_modes(interface));
1342 static void b53_adjust_531x5_rgmii(struct dsa_switch *ds, int port,
1343 phy_interface_t interface)
1345 struct b53_device *dev = ds->priv;
1346 u8 rgmii_ctrl = 0, off;
1348 if (port == dev->imp_port)
1349 off = B53_RGMII_CTRL_IMP;
1351 off = B53_RGMII_CTRL_P(port);
1353 /* Configure the port RGMII clock delay by DLL disabled and
1354 * tx_clk aligned timing (restoring to reset defaults)
1356 b53_read8(dev, B53_CTRL_PAGE, off, &rgmii_ctrl);
1357 rgmii_ctrl &= ~(RGMII_CTRL_DLL_RXC | RGMII_CTRL_DLL_TXC);
1359 /* PHY_INTERFACE_MODE_RGMII_TXID means TX internal delay, make
1360 * sure that we enable the port TX clock internal delay to
1361 * account for this internal delay that is inserted, otherwise
1362 * the switch won't be able to receive correctly.
1364 * PHY_INTERFACE_MODE_RGMII means that we are not introducing
1365 * any delay neither on transmission nor reception, so the
1366 * BCM53125 must also be configured accordingly to account for
1367 * the lack of delay and introduce
1369 * The BCM53125 switch has its RX clock and TX clock control
1370 * swapped, hence the reason why we modify the TX clock path in
1373 if (interface == PHY_INTERFACE_MODE_RGMII_TXID)
1374 rgmii_ctrl |= RGMII_CTRL_DLL_TXC;
1375 if (interface == PHY_INTERFACE_MODE_RGMII)
1376 rgmii_ctrl |= RGMII_CTRL_DLL_TXC | RGMII_CTRL_DLL_RXC;
1378 if (dev->chip_id != BCM53115_DEVICE_ID)
1379 rgmii_ctrl |= RGMII_CTRL_TIMING_SEL;
1381 b53_write8(dev, B53_CTRL_PAGE, off, rgmii_ctrl);
1383 dev_info(ds->dev, "Configured port %d for %s\n", port,
1384 phy_modes(interface));
1387 static void b53_adjust_5325_mii(struct dsa_switch *ds, int port)
1389 struct b53_device *dev = ds->priv;
1392 b53_read8(dev, B53_CTRL_PAGE, B53_PORT_OVERRIDE_CTRL,
1395 /* reverse mii needs to be enabled */
1396 if (!(reg & PORT_OVERRIDE_RV_MII_25)) {
1397 b53_write8(dev, B53_CTRL_PAGE, B53_PORT_OVERRIDE_CTRL,
1398 reg | PORT_OVERRIDE_RV_MII_25);
1399 b53_read8(dev, B53_CTRL_PAGE, B53_PORT_OVERRIDE_CTRL,
1402 if (!(reg & PORT_OVERRIDE_RV_MII_25)) {
1404 "Failed to enable reverse MII mode\n");
1410 void b53_port_event(struct dsa_switch *ds, int port)
1412 struct b53_device *dev = ds->priv;
1416 b53_read16(dev, B53_STAT_PAGE, B53_LINK_STAT, &sts);
1417 link = !!(sts & BIT(port));
1418 dsa_port_phylink_mac_change(ds, port, link);
1420 EXPORT_SYMBOL(b53_port_event);
1422 static void b53_phylink_get_caps(struct dsa_switch *ds, int port,
1423 struct phylink_config *config)
1425 struct b53_device *dev = ds->priv;
1427 /* Internal ports need GMII for PHYLIB */
1428 __set_bit(PHY_INTERFACE_MODE_GMII, config->supported_interfaces);
1430 /* These switches appear to support MII and RevMII too, but beyond
1431 * this, the code gives very few clues. FIXME: We probably need more
1432 * interface modes here.
1434 * According to b53_srab_mux_init(), ports 3..5 can support:
1435 * SGMII, MII, GMII, RGMII or INTERNAL depending on the MUX setting.
1436 * However, the interface mode read from the MUX configuration is
1437 * not passed back to DSA, so phylink uses NA.
1438 * DT can specify RGMII for ports 0, 1.
1439 * For MDIO, port 8 can be RGMII_TXID.
1441 __set_bit(PHY_INTERFACE_MODE_MII, config->supported_interfaces);
1442 __set_bit(PHY_INTERFACE_MODE_REVMII, config->supported_interfaces);
1444 /* BCM63xx RGMII ports support RGMII */
1445 if (is63xx(dev) && in_range(port, B53_63XX_RGMII0, 4))
1446 phy_interface_set_rgmii(config->supported_interfaces);
1448 config->mac_capabilities = MAC_ASYM_PAUSE | MAC_SYM_PAUSE |
1451 /* 5325/5365 are not capable of gigabit speeds, everything else is.
1452 * Note: the original code also exclulded Gigagbit for MII, RevMII
1453 * and 802.3z modes. MII and RevMII are not able to work above 100M,
1454 * so will be excluded by the generic validator implementation.
1455 * However, the exclusion of Gigabit for 802.3z just seems wrong.
1457 if (!(is5325(dev) || is5365(dev)))
1458 config->mac_capabilities |= MAC_1000;
1460 /* Get the implementation specific capabilities */
1461 if (dev->ops->phylink_get_caps)
1462 dev->ops->phylink_get_caps(dev, port, config);
1465 static struct phylink_pcs *b53_phylink_mac_select_pcs(struct phylink_config *config,
1466 phy_interface_t interface)
1468 struct dsa_port *dp = dsa_phylink_to_port(config);
1469 struct b53_device *dev = dp->ds->priv;
1471 if (!dev->ops->phylink_mac_select_pcs)
1474 return dev->ops->phylink_mac_select_pcs(dev, dp->index, interface);
1477 static void b53_phylink_mac_config(struct phylink_config *config,
1479 const struct phylink_link_state *state)
1481 struct dsa_port *dp = dsa_phylink_to_port(config);
1482 phy_interface_t interface = state->interface;
1483 struct dsa_switch *ds = dp->ds;
1484 struct b53_device *dev = ds->priv;
1485 int port = dp->index;
1487 if (is63xx(dev) && in_range(port, B53_63XX_RGMII0, 4))
1488 b53_adjust_63xx_rgmii(ds, port, interface);
1490 if (mode == MLO_AN_FIXED) {
1491 if (is531x5(dev) && phy_interface_mode_is_rgmii(interface))
1492 b53_adjust_531x5_rgmii(ds, port, interface);
1494 /* configure MII port if necessary */
1496 b53_adjust_5325_mii(ds, port);
1500 static void b53_phylink_mac_link_down(struct phylink_config *config,
1502 phy_interface_t interface)
1504 struct dsa_port *dp = dsa_phylink_to_port(config);
1505 struct b53_device *dev = dp->ds->priv;
1506 int port = dp->index;
1508 if (mode == MLO_AN_PHY)
1511 if (mode == MLO_AN_FIXED) {
1512 b53_force_link(dev, port, false);
1516 if (phy_interface_mode_is_8023z(interface) &&
1517 dev->ops->serdes_link_set)
1518 dev->ops->serdes_link_set(dev, port, mode, interface, false);
1521 static void b53_phylink_mac_link_up(struct phylink_config *config,
1522 struct phy_device *phydev,
1524 phy_interface_t interface,
1525 int speed, int duplex,
1526 bool tx_pause, bool rx_pause)
1528 struct dsa_port *dp = dsa_phylink_to_port(config);
1529 struct dsa_switch *ds = dp->ds;
1530 struct b53_device *dev = ds->priv;
1531 struct ethtool_keee *p = &dev->ports[dp->index].eee;
1532 int port = dp->index;
1534 if (mode == MLO_AN_PHY) {
1535 /* Re-negotiate EEE if it was enabled already */
1536 p->eee_enabled = b53_eee_init(ds, port, phydev);
1540 if (mode == MLO_AN_FIXED) {
1541 /* Force flow control on BCM5301x's CPU port */
1542 if (is5301x(dev) && dsa_is_cpu_port(ds, port))
1543 tx_pause = rx_pause = true;
1545 b53_force_port_config(dev, port, speed, duplex,
1546 tx_pause, rx_pause);
1547 b53_force_link(dev, port, true);
1551 if (phy_interface_mode_is_8023z(interface) &&
1552 dev->ops->serdes_link_set)
1553 dev->ops->serdes_link_set(dev, port, mode, interface, true);
1556 int b53_vlan_filtering(struct dsa_switch *ds, int port, bool vlan_filtering,
1557 struct netlink_ext_ack *extack)
1559 struct b53_device *dev = ds->priv;
1561 if (dev->vlan_filtering != vlan_filtering) {
1562 dev->vlan_filtering = vlan_filtering;
1563 b53_apply_config(dev);
1568 EXPORT_SYMBOL(b53_vlan_filtering);
1570 static int b53_vlan_prepare(struct dsa_switch *ds, int port,
1571 const struct switchdev_obj_port_vlan *vlan)
1573 struct b53_device *dev = ds->priv;
1575 if ((is5325(dev) || is5365(dev)) && vlan->vid == 0)
1578 /* Port 7 on 7278 connects to the ASP's UniMAC which is not capable of
1579 * receiving VLAN tagged frames at all, we can still allow the port to
1580 * be configured for egress untagged.
1582 if (dev->chip_id == BCM7278_DEVICE_ID && port == 7 &&
1583 !(vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED))
1586 if (vlan->vid >= dev->num_vlans)
1589 b53_enable_vlan(dev, port, true, dev->vlan_filtering);
1594 int b53_vlan_add(struct dsa_switch *ds, int port,
1595 const struct switchdev_obj_port_vlan *vlan,
1596 struct netlink_ext_ack *extack)
1598 struct b53_device *dev = ds->priv;
1599 bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
1600 bool pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
1601 struct b53_vlan *vl;
1602 u16 old_pvid, new_pvid;
1605 err = b53_vlan_prepare(ds, port, vlan);
1612 old_pvid = dev->ports[port].pvid;
1614 new_pvid = vlan->vid;
1615 else if (!pvid && vlan->vid == old_pvid)
1616 new_pvid = b53_default_pvid(dev);
1618 new_pvid = old_pvid;
1619 dev->ports[port].pvid = new_pvid;
1621 vl = &dev->vlans[vlan->vid];
1623 if (dsa_is_cpu_port(ds, port))
1626 vl->members |= BIT(port);
1627 if (untagged && !b53_vlan_port_needs_forced_tagged(ds, port))
1628 vl->untag |= BIT(port);
1630 vl->untag &= ~BIT(port);
1632 if (!dev->vlan_filtering)
1635 b53_set_vlan_entry(dev, vlan->vid, vl);
1636 b53_fast_age_vlan(dev, vlan->vid);
1638 if (!dsa_is_cpu_port(ds, port) && new_pvid != old_pvid) {
1639 b53_write16(dev, B53_VLAN_PAGE, B53_VLAN_PORT_DEF_TAG(port),
1641 b53_fast_age_vlan(dev, old_pvid);
1646 EXPORT_SYMBOL(b53_vlan_add);
1648 int b53_vlan_del(struct dsa_switch *ds, int port,
1649 const struct switchdev_obj_port_vlan *vlan)
1651 struct b53_device *dev = ds->priv;
1652 bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
1653 struct b53_vlan *vl;
1659 pvid = dev->ports[port].pvid;
1661 vl = &dev->vlans[vlan->vid];
1663 vl->members &= ~BIT(port);
1665 if (pvid == vlan->vid)
1666 pvid = b53_default_pvid(dev);
1667 dev->ports[port].pvid = pvid;
1669 if (untagged && !b53_vlan_port_needs_forced_tagged(ds, port))
1670 vl->untag &= ~(BIT(port));
1672 if (!dev->vlan_filtering)
1675 b53_set_vlan_entry(dev, vlan->vid, vl);
1676 b53_fast_age_vlan(dev, vlan->vid);
1678 b53_write16(dev, B53_VLAN_PAGE, B53_VLAN_PORT_DEF_TAG(port), pvid);
1679 b53_fast_age_vlan(dev, pvid);
1683 EXPORT_SYMBOL(b53_vlan_del);
1685 /* Address Resolution Logic routines. Caller must hold &dev->arl_mutex. */
1686 static int b53_arl_op_wait(struct b53_device *dev)
1688 unsigned int timeout = 10;
1692 b53_read8(dev, B53_ARLIO_PAGE, B53_ARLTBL_RW_CTRL, ®);
1693 if (!(reg & ARLTBL_START_DONE))
1696 usleep_range(1000, 2000);
1697 } while (timeout--);
1699 dev_warn(dev->dev, "timeout waiting for ARL to finish: 0x%02x\n", reg);
1704 static int b53_arl_rw_op(struct b53_device *dev, unsigned int op)
1711 b53_read8(dev, B53_ARLIO_PAGE, B53_ARLTBL_RW_CTRL, ®);
1712 reg |= ARLTBL_START_DONE;
1717 if (dev->vlan_enabled)
1718 reg &= ~ARLTBL_IVL_SVL_SELECT;
1720 reg |= ARLTBL_IVL_SVL_SELECT;
1721 b53_write8(dev, B53_ARLIO_PAGE, B53_ARLTBL_RW_CTRL, reg);
1723 return b53_arl_op_wait(dev);
1726 static int b53_arl_read(struct b53_device *dev, u64 mac,
1727 u16 vid, struct b53_arl_entry *ent, u8 *idx)
1729 DECLARE_BITMAP(free_bins, B53_ARLTBL_MAX_BIN_ENTRIES);
1733 ret = b53_arl_op_wait(dev);
1737 bitmap_zero(free_bins, dev->num_arl_bins);
1740 for (i = 0; i < dev->num_arl_bins; i++) {
1744 b53_read64(dev, B53_ARLIO_PAGE,
1745 B53_ARLTBL_MAC_VID_ENTRY(i), &mac_vid);
1746 b53_read32(dev, B53_ARLIO_PAGE,
1747 B53_ARLTBL_DATA_ENTRY(i), &fwd_entry);
1748 b53_arl_to_entry(ent, mac_vid, fwd_entry);
1750 if (!(fwd_entry & ARLTBL_VALID)) {
1751 set_bit(i, free_bins);
1754 if ((mac_vid & ARLTBL_MAC_MASK) != mac)
1756 if (dev->vlan_enabled &&
1757 ((mac_vid >> ARLTBL_VID_S) & ARLTBL_VID_MASK) != vid)
1763 *idx = find_first_bit(free_bins, dev->num_arl_bins);
1764 return *idx >= dev->num_arl_bins ? -ENOSPC : -ENOENT;
1767 static int b53_arl_op(struct b53_device *dev, int op, int port,
1768 const unsigned char *addr, u16 vid, bool is_valid)
1770 struct b53_arl_entry ent;
1772 u64 mac, mac_vid = 0;
1776 /* Convert the array into a 64-bit MAC */
1777 mac = ether_addr_to_u64(addr);
1779 /* Perform a read for the given MAC and VID */
1780 b53_write48(dev, B53_ARLIO_PAGE, B53_MAC_ADDR_IDX, mac);
1781 b53_write16(dev, B53_ARLIO_PAGE, B53_VLAN_ID_IDX, vid);
1783 /* Issue a read operation for this MAC */
1784 ret = b53_arl_rw_op(dev, 1);
1788 ret = b53_arl_read(dev, mac, vid, &ent, &idx);
1790 /* If this is a read, just finish now */
1798 dev_dbg(dev->dev, "{%pM,%.4d} no space left in ARL\n",
1800 return is_valid ? ret : 0;
1802 /* We could not find a matching MAC, so reset to a new entry */
1803 dev_dbg(dev->dev, "{%pM,%.4d} not found, using idx: %d\n",
1808 dev_dbg(dev->dev, "{%pM,%.4d} found, using idx: %d\n",
1813 /* For multicast address, the port is a bitmask and the validity
1814 * is determined by having at least one port being still active
1816 if (!is_multicast_ether_addr(addr)) {
1818 ent.is_valid = is_valid;
1821 ent.port |= BIT(port);
1823 ent.port &= ~BIT(port);
1825 ent.is_valid = !!(ent.port);
1829 ent.is_static = true;
1831 memcpy(ent.mac, addr, ETH_ALEN);
1832 b53_arl_from_entry(&mac_vid, &fwd_entry, &ent);
1834 b53_write64(dev, B53_ARLIO_PAGE,
1835 B53_ARLTBL_MAC_VID_ENTRY(idx), mac_vid);
1836 b53_write32(dev, B53_ARLIO_PAGE,
1837 B53_ARLTBL_DATA_ENTRY(idx), fwd_entry);
1839 return b53_arl_rw_op(dev, 0);
1842 int b53_fdb_add(struct dsa_switch *ds, int port,
1843 const unsigned char *addr, u16 vid,
1846 struct b53_device *priv = ds->priv;
1849 /* 5325 and 5365 require some more massaging, but could
1850 * be supported eventually
1852 if (is5325(priv) || is5365(priv))
1855 mutex_lock(&priv->arl_mutex);
1856 ret = b53_arl_op(priv, 0, port, addr, vid, true);
1857 mutex_unlock(&priv->arl_mutex);
1861 EXPORT_SYMBOL(b53_fdb_add);
1863 int b53_fdb_del(struct dsa_switch *ds, int port,
1864 const unsigned char *addr, u16 vid,
1867 struct b53_device *priv = ds->priv;
1870 mutex_lock(&priv->arl_mutex);
1871 ret = b53_arl_op(priv, 0, port, addr, vid, false);
1872 mutex_unlock(&priv->arl_mutex);
1876 EXPORT_SYMBOL(b53_fdb_del);
1878 static int b53_arl_search_wait(struct b53_device *dev)
1880 unsigned int timeout = 1000;
1884 b53_read8(dev, B53_ARLIO_PAGE, B53_ARL_SRCH_CTL, ®);
1885 if (!(reg & ARL_SRCH_STDN))
1888 if (reg & ARL_SRCH_VLID)
1891 usleep_range(1000, 2000);
1892 } while (timeout--);
1897 static void b53_arl_search_rd(struct b53_device *dev, u8 idx,
1898 struct b53_arl_entry *ent)
1903 b53_read64(dev, B53_ARLIO_PAGE,
1904 B53_ARL_SRCH_RSTL_MACVID(idx), &mac_vid);
1905 b53_read32(dev, B53_ARLIO_PAGE,
1906 B53_ARL_SRCH_RSTL(idx), &fwd_entry);
1907 b53_arl_to_entry(ent, mac_vid, fwd_entry);
1910 static int b53_fdb_copy(int port, const struct b53_arl_entry *ent,
1911 dsa_fdb_dump_cb_t *cb, void *data)
1916 if (port != ent->port)
1919 return cb(ent->mac, ent->vid, ent->is_static, data);
1922 int b53_fdb_dump(struct dsa_switch *ds, int port,
1923 dsa_fdb_dump_cb_t *cb, void *data)
1925 struct b53_device *priv = ds->priv;
1926 struct b53_arl_entry results[2];
1927 unsigned int count = 0;
1931 mutex_lock(&priv->arl_mutex);
1933 /* Start search operation */
1934 reg = ARL_SRCH_STDN;
1935 b53_write8(priv, B53_ARLIO_PAGE, B53_ARL_SRCH_CTL, reg);
1938 ret = b53_arl_search_wait(priv);
1942 b53_arl_search_rd(priv, 0, &results[0]);
1943 ret = b53_fdb_copy(port, &results[0], cb, data);
1947 if (priv->num_arl_bins > 2) {
1948 b53_arl_search_rd(priv, 1, &results[1]);
1949 ret = b53_fdb_copy(port, &results[1], cb, data);
1953 if (!results[0].is_valid && !results[1].is_valid)
1957 } while (count++ < b53_max_arl_entries(priv) / 2);
1959 mutex_unlock(&priv->arl_mutex);
1963 EXPORT_SYMBOL(b53_fdb_dump);
1965 int b53_mdb_add(struct dsa_switch *ds, int port,
1966 const struct switchdev_obj_port_mdb *mdb,
1969 struct b53_device *priv = ds->priv;
1972 /* 5325 and 5365 require some more massaging, but could
1973 * be supported eventually
1975 if (is5325(priv) || is5365(priv))
1978 mutex_lock(&priv->arl_mutex);
1979 ret = b53_arl_op(priv, 0, port, mdb->addr, mdb->vid, true);
1980 mutex_unlock(&priv->arl_mutex);
1984 EXPORT_SYMBOL(b53_mdb_add);
1986 int b53_mdb_del(struct dsa_switch *ds, int port,
1987 const struct switchdev_obj_port_mdb *mdb,
1990 struct b53_device *priv = ds->priv;
1993 mutex_lock(&priv->arl_mutex);
1994 ret = b53_arl_op(priv, 0, port, mdb->addr, mdb->vid, false);
1995 mutex_unlock(&priv->arl_mutex);
1997 dev_err(ds->dev, "failed to delete MDB entry\n");
2001 EXPORT_SYMBOL(b53_mdb_del);
2003 int b53_br_join(struct dsa_switch *ds, int port, struct dsa_bridge bridge,
2004 bool *tx_fwd_offload, struct netlink_ext_ack *extack)
2006 struct b53_device *dev = ds->priv;
2007 struct b53_vlan *vl;
2008 s8 cpu_port = dsa_to_port(ds, port)->cpu_dp->index;
2009 u16 pvlan, reg, pvid;
2012 /* On 7278, port 7 which connects to the ASP should only receive
2013 * traffic from matching CFP rules.
2015 if (dev->chip_id == BCM7278_DEVICE_ID && port == 7)
2018 pvid = b53_default_pvid(dev);
2019 vl = &dev->vlans[pvid];
2021 if (dev->vlan_filtering) {
2022 /* Make this port leave the all VLANs join since we will have
2023 * proper VLAN entries from now on
2026 b53_read16(dev, B53_VLAN_PAGE, B53_JOIN_ALL_VLAN_EN,
2029 if ((reg & BIT(cpu_port)) == BIT(cpu_port))
2030 reg &= ~BIT(cpu_port);
2031 b53_write16(dev, B53_VLAN_PAGE, B53_JOIN_ALL_VLAN_EN,
2035 b53_get_vlan_entry(dev, pvid, vl);
2036 vl->members &= ~BIT(port);
2037 b53_set_vlan_entry(dev, pvid, vl);
2040 b53_read16(dev, B53_PVLAN_PAGE, B53_PVLAN_PORT_MASK(port), &pvlan);
2042 b53_for_each_port(dev, i) {
2043 if (!dsa_port_offloads_bridge(dsa_to_port(ds, i), &bridge))
2046 /* Add this local port to the remote port VLAN control
2047 * membership and update the remote port bitmask
2049 b53_read16(dev, B53_PVLAN_PAGE, B53_PVLAN_PORT_MASK(i), ®);
2051 b53_write16(dev, B53_PVLAN_PAGE, B53_PVLAN_PORT_MASK(i), reg);
2052 dev->ports[i].vlan_ctl_mask = reg;
2057 /* Disable redirection of unknown SA to the CPU port */
2058 b53_set_eap_mode(dev, port, EAP_MODE_BASIC);
2060 /* Configure the local port VLAN control membership to include
2061 * remote ports and update the local port bitmask
2063 b53_write16(dev, B53_PVLAN_PAGE, B53_PVLAN_PORT_MASK(port), pvlan);
2064 dev->ports[port].vlan_ctl_mask = pvlan;
2068 EXPORT_SYMBOL(b53_br_join);
2070 void b53_br_leave(struct dsa_switch *ds, int port, struct dsa_bridge bridge)
2072 struct b53_device *dev = ds->priv;
2073 struct b53_vlan *vl;
2074 s8 cpu_port = dsa_to_port(ds, port)->cpu_dp->index;
2076 u16 pvlan, reg, pvid;
2078 b53_read16(dev, B53_PVLAN_PAGE, B53_PVLAN_PORT_MASK(port), &pvlan);
2080 b53_for_each_port(dev, i) {
2081 /* Don't touch the remaining ports */
2082 if (!dsa_port_offloads_bridge(dsa_to_port(ds, i), &bridge))
2085 b53_read16(dev, B53_PVLAN_PAGE, B53_PVLAN_PORT_MASK(i), ®);
2087 b53_write16(dev, B53_PVLAN_PAGE, B53_PVLAN_PORT_MASK(i), reg);
2088 dev->ports[port].vlan_ctl_mask = reg;
2090 /* Prevent self removal to preserve isolation */
2095 /* Enable redirection of unknown SA to the CPU port */
2096 b53_set_eap_mode(dev, port, EAP_MODE_SIMPLIFIED);
2098 b53_write16(dev, B53_PVLAN_PAGE, B53_PVLAN_PORT_MASK(port), pvlan);
2099 dev->ports[port].vlan_ctl_mask = pvlan;
2101 pvid = b53_default_pvid(dev);
2102 vl = &dev->vlans[pvid];
2104 if (dev->vlan_filtering) {
2105 /* Make this port join all VLANs without VLAN entries */
2107 b53_read16(dev, B53_VLAN_PAGE, B53_JOIN_ALL_VLAN_EN, ®);
2109 if (!(reg & BIT(cpu_port)))
2110 reg |= BIT(cpu_port);
2111 b53_write16(dev, B53_VLAN_PAGE, B53_JOIN_ALL_VLAN_EN, reg);
2114 b53_get_vlan_entry(dev, pvid, vl);
2115 vl->members |= BIT(port);
2116 b53_set_vlan_entry(dev, pvid, vl);
2119 EXPORT_SYMBOL(b53_br_leave);
2121 void b53_br_set_stp_state(struct dsa_switch *ds, int port, u8 state)
2123 struct b53_device *dev = ds->priv;
2128 case BR_STATE_DISABLED:
2129 hw_state = PORT_CTRL_DIS_STATE;
2131 case BR_STATE_LISTENING:
2132 hw_state = PORT_CTRL_LISTEN_STATE;
2134 case BR_STATE_LEARNING:
2135 hw_state = PORT_CTRL_LEARN_STATE;
2137 case BR_STATE_FORWARDING:
2138 hw_state = PORT_CTRL_FWD_STATE;
2140 case BR_STATE_BLOCKING:
2141 hw_state = PORT_CTRL_BLOCK_STATE;
2144 dev_err(ds->dev, "invalid STP state: %d\n", state);
2148 b53_read8(dev, B53_CTRL_PAGE, B53_PORT_CTRL(port), ®);
2149 reg &= ~PORT_CTRL_STP_STATE_MASK;
2151 b53_write8(dev, B53_CTRL_PAGE, B53_PORT_CTRL(port), reg);
2153 EXPORT_SYMBOL(b53_br_set_stp_state);
2155 void b53_br_fast_age(struct dsa_switch *ds, int port)
2157 struct b53_device *dev = ds->priv;
2159 if (b53_fast_age_port(dev, port))
2160 dev_err(ds->dev, "fast ageing failed\n");
2162 EXPORT_SYMBOL(b53_br_fast_age);
2164 int b53_br_flags_pre(struct dsa_switch *ds, int port,
2165 struct switchdev_brport_flags flags,
2166 struct netlink_ext_ack *extack)
2168 if (flags.mask & ~(BR_FLOOD | BR_MCAST_FLOOD | BR_LEARNING))
2173 EXPORT_SYMBOL(b53_br_flags_pre);
2175 int b53_br_flags(struct dsa_switch *ds, int port,
2176 struct switchdev_brport_flags flags,
2177 struct netlink_ext_ack *extack)
2179 if (flags.mask & BR_FLOOD)
2180 b53_port_set_ucast_flood(ds->priv, port,
2181 !!(flags.val & BR_FLOOD));
2182 if (flags.mask & BR_MCAST_FLOOD)
2183 b53_port_set_mcast_flood(ds->priv, port,
2184 !!(flags.val & BR_MCAST_FLOOD));
2185 if (flags.mask & BR_LEARNING)
2186 b53_port_set_learning(ds->priv, port,
2187 !!(flags.val & BR_LEARNING));
2191 EXPORT_SYMBOL(b53_br_flags);
2193 static bool b53_possible_cpu_port(struct dsa_switch *ds, int port)
2195 /* Broadcom switches will accept enabling Broadcom tags on the
2196 * following ports: 5, 7 and 8, any other port is not supported
2199 case B53_CPU_PORT_25:
2208 static bool b53_can_enable_brcm_tags(struct dsa_switch *ds, int port,
2209 enum dsa_tag_protocol tag_protocol)
2211 bool ret = b53_possible_cpu_port(ds, port);
2214 dev_warn(ds->dev, "Port %d is not Broadcom tag capable\n",
2219 switch (tag_protocol) {
2220 case DSA_TAG_PROTO_BRCM:
2221 case DSA_TAG_PROTO_BRCM_PREPEND:
2223 "Port %d is stacked to Broadcom tag switch\n", port);
2234 enum dsa_tag_protocol b53_get_tag_protocol(struct dsa_switch *ds, int port,
2235 enum dsa_tag_protocol mprot)
2237 struct b53_device *dev = ds->priv;
2239 if (!b53_can_enable_brcm_tags(ds, port, mprot)) {
2240 dev->tag_protocol = DSA_TAG_PROTO_NONE;
2244 /* Older models require a different 6 byte tag */
2245 if (is5325(dev) || is5365(dev) || is63xx(dev)) {
2246 dev->tag_protocol = DSA_TAG_PROTO_BRCM_LEGACY;
2250 /* Broadcom BCM58xx chips have a flow accelerator on Port 8
2251 * which requires us to use the prepended Broadcom tag type
2253 if (dev->chip_id == BCM58XX_DEVICE_ID && port == B53_CPU_PORT) {
2254 dev->tag_protocol = DSA_TAG_PROTO_BRCM_PREPEND;
2258 dev->tag_protocol = DSA_TAG_PROTO_BRCM;
2260 return dev->tag_protocol;
2262 EXPORT_SYMBOL(b53_get_tag_protocol);
2264 int b53_mirror_add(struct dsa_switch *ds, int port,
2265 struct dsa_mall_mirror_tc_entry *mirror, bool ingress,
2266 struct netlink_ext_ack *extack)
2268 struct b53_device *dev = ds->priv;
2272 loc = B53_IG_MIR_CTL;
2274 loc = B53_EG_MIR_CTL;
2276 b53_read16(dev, B53_MGMT_PAGE, loc, ®);
2278 b53_write16(dev, B53_MGMT_PAGE, loc, reg);
2280 b53_read16(dev, B53_MGMT_PAGE, B53_MIR_CAP_CTL, ®);
2281 reg &= ~CAP_PORT_MASK;
2282 reg |= mirror->to_local_port;
2284 b53_write16(dev, B53_MGMT_PAGE, B53_MIR_CAP_CTL, reg);
2288 EXPORT_SYMBOL(b53_mirror_add);
2290 void b53_mirror_del(struct dsa_switch *ds, int port,
2291 struct dsa_mall_mirror_tc_entry *mirror)
2293 struct b53_device *dev = ds->priv;
2294 bool loc_disable = false, other_loc_disable = false;
2297 if (mirror->ingress)
2298 loc = B53_IG_MIR_CTL;
2300 loc = B53_EG_MIR_CTL;
2302 /* Update the desired ingress/egress register */
2303 b53_read16(dev, B53_MGMT_PAGE, loc, ®);
2305 if (!(reg & MIRROR_MASK))
2307 b53_write16(dev, B53_MGMT_PAGE, loc, reg);
2309 /* Now look at the other one to know if we can disable mirroring
2312 if (mirror->ingress)
2313 b53_read16(dev, B53_MGMT_PAGE, B53_EG_MIR_CTL, ®);
2315 b53_read16(dev, B53_MGMT_PAGE, B53_IG_MIR_CTL, ®);
2316 if (!(reg & MIRROR_MASK))
2317 other_loc_disable = true;
2319 b53_read16(dev, B53_MGMT_PAGE, B53_MIR_CAP_CTL, ®);
2320 /* Both no longer have ports, let's disable mirroring */
2321 if (loc_disable && other_loc_disable) {
2323 reg &= ~mirror->to_local_port;
2325 b53_write16(dev, B53_MGMT_PAGE, B53_MIR_CAP_CTL, reg);
2327 EXPORT_SYMBOL(b53_mirror_del);
2329 /* Returns 0 if EEE was not enabled, or 1 otherwise
2331 int b53_eee_init(struct dsa_switch *ds, int port, struct phy_device *phy)
2335 if (!b53_support_eee(ds, port))
2338 ret = phy_init_eee(phy, false);
2342 b53_eee_enable_set(ds, port, true);
2346 EXPORT_SYMBOL(b53_eee_init);
2348 bool b53_support_eee(struct dsa_switch *ds, int port)
2350 struct b53_device *dev = ds->priv;
2352 return !is5325(dev) && !is5365(dev) && !is63xx(dev);
2354 EXPORT_SYMBOL(b53_support_eee);
2356 int b53_set_mac_eee(struct dsa_switch *ds, int port, struct ethtool_keee *e)
2358 struct b53_device *dev = ds->priv;
2359 struct ethtool_keee *p = &dev->ports[port].eee;
2361 p->eee_enabled = e->eee_enabled;
2362 b53_eee_enable_set(ds, port, e->eee_enabled);
2366 EXPORT_SYMBOL(b53_set_mac_eee);
2368 static int b53_change_mtu(struct dsa_switch *ds, int port, int mtu)
2370 struct b53_device *dev = ds->priv;
2374 if (is5325(dev) || is5365(dev))
2377 if (!dsa_is_cpu_port(ds, port))
2380 enable_jumbo = (mtu > ETH_DATA_LEN);
2381 allow_10_100 = !is63xx(dev);
2383 return b53_set_jumbo(dev, enable_jumbo, allow_10_100);
2386 static int b53_get_max_mtu(struct dsa_switch *ds, int port)
2388 struct b53_device *dev = ds->priv;
2390 if (is5325(dev) || is5365(dev))
2391 return B53_MAX_MTU_25;
2396 int b53_set_ageing_time(struct dsa_switch *ds, unsigned int msecs)
2398 struct b53_device *dev = ds->priv;
2403 reg = B53_AGING_TIME_CONTROL_63XX;
2405 reg = B53_AGING_TIME_CONTROL;
2407 atc = DIV_ROUND_CLOSEST(msecs, 1000);
2409 if (!is5325(dev) && !is5365(dev))
2412 b53_write32(dev, B53_MGMT_PAGE, reg, atc);
2416 EXPORT_SYMBOL_GPL(b53_set_ageing_time);
2418 static const struct phylink_mac_ops b53_phylink_mac_ops = {
2419 .mac_select_pcs = b53_phylink_mac_select_pcs,
2420 .mac_config = b53_phylink_mac_config,
2421 .mac_link_down = b53_phylink_mac_link_down,
2422 .mac_link_up = b53_phylink_mac_link_up,
2425 static const struct dsa_switch_ops b53_switch_ops = {
2426 .get_tag_protocol = b53_get_tag_protocol,
2428 .teardown = b53_teardown,
2429 .get_strings = b53_get_strings,
2430 .get_ethtool_stats = b53_get_ethtool_stats,
2431 .get_sset_count = b53_get_sset_count,
2432 .get_ethtool_phy_stats = b53_get_ethtool_phy_stats,
2433 .phy_read = b53_phy_read16,
2434 .phy_write = b53_phy_write16,
2435 .phylink_get_caps = b53_phylink_get_caps,
2436 .port_setup = b53_setup_port,
2437 .port_enable = b53_enable_port,
2438 .port_disable = b53_disable_port,
2439 .support_eee = b53_support_eee,
2440 .set_mac_eee = b53_set_mac_eee,
2441 .set_ageing_time = b53_set_ageing_time,
2442 .port_bridge_join = b53_br_join,
2443 .port_bridge_leave = b53_br_leave,
2444 .port_pre_bridge_flags = b53_br_flags_pre,
2445 .port_bridge_flags = b53_br_flags,
2446 .port_stp_state_set = b53_br_set_stp_state,
2447 .port_fast_age = b53_br_fast_age,
2448 .port_vlan_filtering = b53_vlan_filtering,
2449 .port_vlan_add = b53_vlan_add,
2450 .port_vlan_del = b53_vlan_del,
2451 .port_fdb_dump = b53_fdb_dump,
2452 .port_fdb_add = b53_fdb_add,
2453 .port_fdb_del = b53_fdb_del,
2454 .port_mirror_add = b53_mirror_add,
2455 .port_mirror_del = b53_mirror_del,
2456 .port_mdb_add = b53_mdb_add,
2457 .port_mdb_del = b53_mdb_del,
2458 .port_max_mtu = b53_get_max_mtu,
2459 .port_change_mtu = b53_change_mtu,
2462 struct b53_chip_data {
2464 const char *dev_name;
2477 #define B53_VTA_REGS \
2478 { B53_VT_ACCESS, B53_VT_INDEX, B53_VT_ENTRY }
2479 #define B53_VTA_REGS_9798 \
2480 { B53_VT_ACCESS_9798, B53_VT_INDEX_9798, B53_VT_ENTRY_9798 }
2481 #define B53_VTA_REGS_63XX \
2482 { B53_VT_ACCESS_63XX, B53_VT_INDEX_63XX, B53_VT_ENTRY_63XX }
2484 static const struct b53_chip_data b53_switch_chips[] = {
2486 .chip_id = BCM5325_DEVICE_ID,
2487 .dev_name = "BCM5325",
2489 .enabled_ports = 0x3f,
2491 .arl_buckets = 1024,
2493 .duplex_reg = B53_DUPLEX_STAT_FE,
2496 .chip_id = BCM5365_DEVICE_ID,
2497 .dev_name = "BCM5365",
2499 .enabled_ports = 0x3f,
2501 .arl_buckets = 1024,
2503 .duplex_reg = B53_DUPLEX_STAT_FE,
2506 .chip_id = BCM5389_DEVICE_ID,
2507 .dev_name = "BCM5389",
2509 .enabled_ports = 0x11f,
2511 .arl_buckets = 1024,
2513 .vta_regs = B53_VTA_REGS,
2514 .duplex_reg = B53_DUPLEX_STAT_GE,
2515 .jumbo_pm_reg = B53_JUMBO_PORT_MASK,
2516 .jumbo_size_reg = B53_JUMBO_MAX_SIZE,
2519 .chip_id = BCM5395_DEVICE_ID,
2520 .dev_name = "BCM5395",
2522 .enabled_ports = 0x11f,
2524 .arl_buckets = 1024,
2526 .vta_regs = B53_VTA_REGS,
2527 .duplex_reg = B53_DUPLEX_STAT_GE,
2528 .jumbo_pm_reg = B53_JUMBO_PORT_MASK,
2529 .jumbo_size_reg = B53_JUMBO_MAX_SIZE,
2532 .chip_id = BCM5397_DEVICE_ID,
2533 .dev_name = "BCM5397",
2535 .enabled_ports = 0x11f,
2537 .arl_buckets = 1024,
2539 .vta_regs = B53_VTA_REGS_9798,
2540 .duplex_reg = B53_DUPLEX_STAT_GE,
2541 .jumbo_pm_reg = B53_JUMBO_PORT_MASK,
2542 .jumbo_size_reg = B53_JUMBO_MAX_SIZE,
2545 .chip_id = BCM5398_DEVICE_ID,
2546 .dev_name = "BCM5398",
2548 .enabled_ports = 0x17f,
2550 .arl_buckets = 1024,
2552 .vta_regs = B53_VTA_REGS_9798,
2553 .duplex_reg = B53_DUPLEX_STAT_GE,
2554 .jumbo_pm_reg = B53_JUMBO_PORT_MASK,
2555 .jumbo_size_reg = B53_JUMBO_MAX_SIZE,
2558 .chip_id = BCM53101_DEVICE_ID,
2559 .dev_name = "BCM53101",
2561 .enabled_ports = 0x11f,
2564 .vta_regs = B53_VTA_REGS,
2566 .duplex_reg = B53_DUPLEX_STAT_GE,
2567 .jumbo_pm_reg = B53_JUMBO_PORT_MASK,
2568 .jumbo_size_reg = B53_JUMBO_MAX_SIZE,
2571 .chip_id = BCM53115_DEVICE_ID,
2572 .dev_name = "BCM53115",
2574 .enabled_ports = 0x11f,
2576 .arl_buckets = 1024,
2577 .vta_regs = B53_VTA_REGS,
2579 .duplex_reg = B53_DUPLEX_STAT_GE,
2580 .jumbo_pm_reg = B53_JUMBO_PORT_MASK,
2581 .jumbo_size_reg = B53_JUMBO_MAX_SIZE,
2584 .chip_id = BCM53125_DEVICE_ID,
2585 .dev_name = "BCM53125",
2587 .enabled_ports = 0x1ff,
2589 .arl_buckets = 1024,
2591 .vta_regs = B53_VTA_REGS,
2592 .duplex_reg = B53_DUPLEX_STAT_GE,
2593 .jumbo_pm_reg = B53_JUMBO_PORT_MASK,
2594 .jumbo_size_reg = B53_JUMBO_MAX_SIZE,
2597 .chip_id = BCM53128_DEVICE_ID,
2598 .dev_name = "BCM53128",
2600 .enabled_ports = 0x1ff,
2602 .arl_buckets = 1024,
2604 .vta_regs = B53_VTA_REGS,
2605 .duplex_reg = B53_DUPLEX_STAT_GE,
2606 .jumbo_pm_reg = B53_JUMBO_PORT_MASK,
2607 .jumbo_size_reg = B53_JUMBO_MAX_SIZE,
2610 .chip_id = BCM63XX_DEVICE_ID,
2611 .dev_name = "BCM63xx",
2613 .enabled_ports = 0, /* pdata must provide them */
2615 .arl_buckets = 1024,
2617 .vta_regs = B53_VTA_REGS_63XX,
2618 .duplex_reg = B53_DUPLEX_STAT_63XX,
2619 .jumbo_pm_reg = B53_JUMBO_PORT_MASK_63XX,
2620 .jumbo_size_reg = B53_JUMBO_MAX_SIZE_63XX,
2623 .chip_id = BCM63268_DEVICE_ID,
2624 .dev_name = "BCM63268",
2626 .enabled_ports = 0, /* pdata must provide them */
2628 .arl_buckets = 1024,
2630 .vta_regs = B53_VTA_REGS_63XX,
2631 .duplex_reg = B53_DUPLEX_STAT_63XX,
2632 .jumbo_pm_reg = B53_JUMBO_PORT_MASK_63XX,
2633 .jumbo_size_reg = B53_JUMBO_MAX_SIZE_63XX,
2636 .chip_id = BCM53010_DEVICE_ID,
2637 .dev_name = "BCM53010",
2639 .enabled_ports = 0x1bf,
2641 .arl_buckets = 1024,
2643 .vta_regs = B53_VTA_REGS,
2644 .duplex_reg = B53_DUPLEX_STAT_GE,
2645 .jumbo_pm_reg = B53_JUMBO_PORT_MASK,
2646 .jumbo_size_reg = B53_JUMBO_MAX_SIZE,
2649 .chip_id = BCM53011_DEVICE_ID,
2650 .dev_name = "BCM53011",
2652 .enabled_ports = 0x1bf,
2654 .arl_buckets = 1024,
2656 .vta_regs = B53_VTA_REGS,
2657 .duplex_reg = B53_DUPLEX_STAT_GE,
2658 .jumbo_pm_reg = B53_JUMBO_PORT_MASK,
2659 .jumbo_size_reg = B53_JUMBO_MAX_SIZE,
2662 .chip_id = BCM53012_DEVICE_ID,
2663 .dev_name = "BCM53012",
2665 .enabled_ports = 0x1bf,
2667 .arl_buckets = 1024,
2669 .vta_regs = B53_VTA_REGS,
2670 .duplex_reg = B53_DUPLEX_STAT_GE,
2671 .jumbo_pm_reg = B53_JUMBO_PORT_MASK,
2672 .jumbo_size_reg = B53_JUMBO_MAX_SIZE,
2675 .chip_id = BCM53018_DEVICE_ID,
2676 .dev_name = "BCM53018",
2678 .enabled_ports = 0x1bf,
2680 .arl_buckets = 1024,
2682 .vta_regs = B53_VTA_REGS,
2683 .duplex_reg = B53_DUPLEX_STAT_GE,
2684 .jumbo_pm_reg = B53_JUMBO_PORT_MASK,
2685 .jumbo_size_reg = B53_JUMBO_MAX_SIZE,
2688 .chip_id = BCM53019_DEVICE_ID,
2689 .dev_name = "BCM53019",
2691 .enabled_ports = 0x1bf,
2693 .arl_buckets = 1024,
2695 .vta_regs = B53_VTA_REGS,
2696 .duplex_reg = B53_DUPLEX_STAT_GE,
2697 .jumbo_pm_reg = B53_JUMBO_PORT_MASK,
2698 .jumbo_size_reg = B53_JUMBO_MAX_SIZE,
2701 .chip_id = BCM58XX_DEVICE_ID,
2702 .dev_name = "BCM585xx/586xx/88312",
2704 .enabled_ports = 0x1ff,
2706 .arl_buckets = 1024,
2708 .vta_regs = B53_VTA_REGS,
2709 .duplex_reg = B53_DUPLEX_STAT_GE,
2710 .jumbo_pm_reg = B53_JUMBO_PORT_MASK,
2711 .jumbo_size_reg = B53_JUMBO_MAX_SIZE,
2714 .chip_id = BCM583XX_DEVICE_ID,
2715 .dev_name = "BCM583xx/11360",
2717 .enabled_ports = 0x103,
2719 .arl_buckets = 1024,
2721 .vta_regs = B53_VTA_REGS,
2722 .duplex_reg = B53_DUPLEX_STAT_GE,
2723 .jumbo_pm_reg = B53_JUMBO_PORT_MASK,
2724 .jumbo_size_reg = B53_JUMBO_MAX_SIZE,
2728 .chip_id = BCM4908_DEVICE_ID,
2729 .dev_name = "BCM4908",
2731 .enabled_ports = 0x1bf,
2735 .vta_regs = B53_VTA_REGS,
2736 .duplex_reg = B53_DUPLEX_STAT_GE,
2737 .jumbo_pm_reg = B53_JUMBO_PORT_MASK,
2738 .jumbo_size_reg = B53_JUMBO_MAX_SIZE,
2741 .chip_id = BCM7445_DEVICE_ID,
2742 .dev_name = "BCM7445",
2744 .enabled_ports = 0x1ff,
2746 .arl_buckets = 1024,
2748 .vta_regs = B53_VTA_REGS,
2749 .duplex_reg = B53_DUPLEX_STAT_GE,
2750 .jumbo_pm_reg = B53_JUMBO_PORT_MASK,
2751 .jumbo_size_reg = B53_JUMBO_MAX_SIZE,
2754 .chip_id = BCM7278_DEVICE_ID,
2755 .dev_name = "BCM7278",
2757 .enabled_ports = 0x1ff,
2761 .vta_regs = B53_VTA_REGS,
2762 .duplex_reg = B53_DUPLEX_STAT_GE,
2763 .jumbo_pm_reg = B53_JUMBO_PORT_MASK,
2764 .jumbo_size_reg = B53_JUMBO_MAX_SIZE,
2767 .chip_id = BCM53134_DEVICE_ID,
2768 .dev_name = "BCM53134",
2770 .enabled_ports = 0x12f,
2772 .cpu_port = B53_CPU_PORT,
2773 .vta_regs = B53_VTA_REGS,
2775 .arl_buckets = 1024,
2776 .duplex_reg = B53_DUPLEX_STAT_GE,
2777 .jumbo_pm_reg = B53_JUMBO_PORT_MASK,
2778 .jumbo_size_reg = B53_JUMBO_MAX_SIZE,
2782 static int b53_switch_init(struct b53_device *dev)
2787 for (i = 0; i < ARRAY_SIZE(b53_switch_chips); i++) {
2788 const struct b53_chip_data *chip = &b53_switch_chips[i];
2790 if (chip->chip_id == dev->chip_id) {
2791 if (!dev->enabled_ports)
2792 dev->enabled_ports = chip->enabled_ports;
2793 dev->name = chip->dev_name;
2794 dev->duplex_reg = chip->duplex_reg;
2795 dev->vta_regs[0] = chip->vta_regs[0];
2796 dev->vta_regs[1] = chip->vta_regs[1];
2797 dev->vta_regs[2] = chip->vta_regs[2];
2798 dev->jumbo_pm_reg = chip->jumbo_pm_reg;
2799 dev->imp_port = chip->imp_port;
2800 dev->num_vlans = chip->vlans;
2801 dev->num_arl_bins = chip->arl_bins;
2802 dev->num_arl_buckets = chip->arl_buckets;
2807 /* check which BCM5325x version we have */
2811 b53_read8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL4_25, &vc4);
2813 /* check reserved bits */
2819 /* BCM5325F - do not use port 4 */
2820 dev->enabled_ports &= ~BIT(4);
2823 /* On the BCM47XX SoCs this is the supported internal switch.*/
2824 #ifndef CONFIG_BCM47XX
2833 dev->num_ports = fls(dev->enabled_ports);
2835 dev->ds->num_ports = min_t(unsigned int, dev->num_ports, DSA_MAX_PORTS);
2837 /* Include non standard CPU port built-in PHYs to be probed */
2838 if (is539x(dev) || is531x5(dev)) {
2839 for (i = 0; i < dev->num_ports; i++) {
2840 if (!(dev->ds->phys_mii_mask & BIT(i)) &&
2841 !b53_possible_cpu_port(dev->ds, i))
2842 dev->ds->phys_mii_mask |= BIT(i);
2846 dev->ports = devm_kcalloc(dev->dev,
2847 dev->num_ports, sizeof(struct b53_port),
2852 dev->vlans = devm_kcalloc(dev->dev,
2853 dev->num_vlans, sizeof(struct b53_vlan),
2858 dev->reset_gpio = b53_switch_get_reset_gpio(dev);
2859 if (dev->reset_gpio >= 0) {
2860 ret = devm_gpio_request_one(dev->dev, dev->reset_gpio,
2861 GPIOF_OUT_INIT_HIGH, "robo_reset");
2869 struct b53_device *b53_switch_alloc(struct device *base,
2870 const struct b53_io_ops *ops,
2873 struct dsa_switch *ds;
2874 struct b53_device *dev;
2876 ds = devm_kzalloc(base, sizeof(*ds), GFP_KERNEL);
2882 dev = devm_kzalloc(base, sizeof(*dev), GFP_KERNEL);
2892 ds->ops = &b53_switch_ops;
2893 ds->phylink_mac_ops = &b53_phylink_mac_ops;
2894 dev->vlan_enabled = true;
2895 dev->vlan_filtering = false;
2896 /* Let DSA handle the case were multiple bridges span the same switch
2897 * device and different VLAN awareness settings are requested, which
2898 * would be breaking filtering semantics for any of the other bridge
2899 * devices. (not hardware supported)
2901 ds->vlan_filtering_is_global = true;
2903 mutex_init(&dev->reg_mutex);
2904 mutex_init(&dev->stats_mutex);
2905 mutex_init(&dev->arl_mutex);
2909 EXPORT_SYMBOL(b53_switch_alloc);
2911 int b53_switch_detect(struct b53_device *dev)
2918 ret = b53_read8(dev, B53_MGMT_PAGE, B53_DEVICE_ID, &id8);
2924 /* BCM5325 and BCM5365 do not have this register so reads
2925 * return 0. But the read operation did succeed, so assume this
2928 * Next check if we can write to the 5325's VTA register; for
2929 * 5365 it is read only.
2931 b53_write16(dev, B53_VLAN_PAGE, B53_VLAN_TABLE_ACCESS_25, 0xf);
2932 b53_read16(dev, B53_VLAN_PAGE, B53_VLAN_TABLE_ACCESS_25, &tmp);
2935 dev->chip_id = BCM5325_DEVICE_ID;
2937 dev->chip_id = BCM5365_DEVICE_ID;
2939 case BCM5389_DEVICE_ID:
2940 case BCM5395_DEVICE_ID:
2941 case BCM5397_DEVICE_ID:
2942 case BCM5398_DEVICE_ID:
2946 ret = b53_read32(dev, B53_MGMT_PAGE, B53_DEVICE_ID, &id32);
2951 case BCM53101_DEVICE_ID:
2952 case BCM53115_DEVICE_ID:
2953 case BCM53125_DEVICE_ID:
2954 case BCM53128_DEVICE_ID:
2955 case BCM53010_DEVICE_ID:
2956 case BCM53011_DEVICE_ID:
2957 case BCM53012_DEVICE_ID:
2958 case BCM53018_DEVICE_ID:
2959 case BCM53019_DEVICE_ID:
2960 case BCM53134_DEVICE_ID:
2961 dev->chip_id = id32;
2965 "unsupported switch detected (BCM53%02x/BCM%x)\n",
2971 if (dev->chip_id == BCM5325_DEVICE_ID)
2972 return b53_read8(dev, B53_STAT_PAGE, B53_REV_ID_25,
2975 return b53_read8(dev, B53_MGMT_PAGE, B53_REV_ID,
2978 EXPORT_SYMBOL(b53_switch_detect);
2980 int b53_switch_register(struct b53_device *dev)
2985 dev->chip_id = dev->pdata->chip_id;
2986 dev->enabled_ports = dev->pdata->enabled_ports;
2989 if (!dev->chip_id && b53_switch_detect(dev))
2992 ret = b53_switch_init(dev);
2996 dev_info(dev->dev, "found switch: %s, rev %i\n",
2997 dev->name, dev->core_rev);
2999 return dsa_register_switch(dev->ds);
3001 EXPORT_SYMBOL(b53_switch_register);
3003 MODULE_AUTHOR("Jonas Gorski <jogo@openwrt.org>");
3004 MODULE_DESCRIPTION("B53 switch library");
3005 MODULE_LICENSE("Dual BSD/GPL");