xref: /openbmc/linux/drivers/net/dsa/b53/b53_common.c (revision b409a9efa183d92d99bc314fb26ebc1c2a8b4379)
1967dd82fSFlorian Fainelli /*
2967dd82fSFlorian Fainelli  * B53 switch driver main logic
3967dd82fSFlorian Fainelli  *
4967dd82fSFlorian Fainelli  * Copyright (C) 2011-2013 Jonas Gorski <jogo@openwrt.org>
5967dd82fSFlorian Fainelli  * Copyright (C) 2016 Florian Fainelli <f.fainelli@gmail.com>
6967dd82fSFlorian Fainelli  *
7967dd82fSFlorian Fainelli  * Permission to use, copy, modify, and/or distribute this software for any
8967dd82fSFlorian Fainelli  * purpose with or without fee is hereby granted, provided that the above
9967dd82fSFlorian Fainelli  * copyright notice and this permission notice appear in all copies.
10967dd82fSFlorian Fainelli  *
11967dd82fSFlorian Fainelli  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12967dd82fSFlorian Fainelli  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13967dd82fSFlorian Fainelli  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14967dd82fSFlorian Fainelli  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15967dd82fSFlorian Fainelli  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16967dd82fSFlorian Fainelli  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17967dd82fSFlorian Fainelli  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18967dd82fSFlorian Fainelli  */
19967dd82fSFlorian Fainelli 
20967dd82fSFlorian Fainelli #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
21967dd82fSFlorian Fainelli 
22967dd82fSFlorian Fainelli #include <linux/delay.h>
23967dd82fSFlorian Fainelli #include <linux/export.h>
24967dd82fSFlorian Fainelli #include <linux/gpio.h>
25967dd82fSFlorian Fainelli #include <linux/kernel.h>
26967dd82fSFlorian Fainelli #include <linux/module.h>
27967dd82fSFlorian Fainelli #include <linux/platform_data/b53.h>
28967dd82fSFlorian Fainelli #include <linux/phy.h>
291da6df85SFlorian Fainelli #include <linux/etherdevice.h>
30ff39c2d6SFlorian Fainelli #include <linux/if_bridge.h>
31967dd82fSFlorian Fainelli #include <net/dsa.h>
32967dd82fSFlorian Fainelli 
33967dd82fSFlorian Fainelli #include "b53_regs.h"
34967dd82fSFlorian Fainelli #include "b53_priv.h"
35967dd82fSFlorian Fainelli 
36967dd82fSFlorian Fainelli struct b53_mib_desc {
37967dd82fSFlorian Fainelli 	u8 size;
38967dd82fSFlorian Fainelli 	u8 offset;
39967dd82fSFlorian Fainelli 	const char *name;
40967dd82fSFlorian Fainelli };
41967dd82fSFlorian Fainelli 
42967dd82fSFlorian Fainelli /* BCM5365 MIB counters */
43967dd82fSFlorian Fainelli static const struct b53_mib_desc b53_mibs_65[] = {
44967dd82fSFlorian Fainelli 	{ 8, 0x00, "TxOctets" },
45967dd82fSFlorian Fainelli 	{ 4, 0x08, "TxDropPkts" },
46967dd82fSFlorian Fainelli 	{ 4, 0x10, "TxBroadcastPkts" },
47967dd82fSFlorian Fainelli 	{ 4, 0x14, "TxMulticastPkts" },
48967dd82fSFlorian Fainelli 	{ 4, 0x18, "TxUnicastPkts" },
49967dd82fSFlorian Fainelli 	{ 4, 0x1c, "TxCollisions" },
50967dd82fSFlorian Fainelli 	{ 4, 0x20, "TxSingleCollision" },
51967dd82fSFlorian Fainelli 	{ 4, 0x24, "TxMultipleCollision" },
52967dd82fSFlorian Fainelli 	{ 4, 0x28, "TxDeferredTransmit" },
53967dd82fSFlorian Fainelli 	{ 4, 0x2c, "TxLateCollision" },
54967dd82fSFlorian Fainelli 	{ 4, 0x30, "TxExcessiveCollision" },
55967dd82fSFlorian Fainelli 	{ 4, 0x38, "TxPausePkts" },
56967dd82fSFlorian Fainelli 	{ 8, 0x44, "RxOctets" },
57967dd82fSFlorian Fainelli 	{ 4, 0x4c, "RxUndersizePkts" },
58967dd82fSFlorian Fainelli 	{ 4, 0x50, "RxPausePkts" },
59967dd82fSFlorian Fainelli 	{ 4, 0x54, "Pkts64Octets" },
60967dd82fSFlorian Fainelli 	{ 4, 0x58, "Pkts65to127Octets" },
61967dd82fSFlorian Fainelli 	{ 4, 0x5c, "Pkts128to255Octets" },
62967dd82fSFlorian Fainelli 	{ 4, 0x60, "Pkts256to511Octets" },
63967dd82fSFlorian Fainelli 	{ 4, 0x64, "Pkts512to1023Octets" },
64967dd82fSFlorian Fainelli 	{ 4, 0x68, "Pkts1024to1522Octets" },
65967dd82fSFlorian Fainelli 	{ 4, 0x6c, "RxOversizePkts" },
66967dd82fSFlorian Fainelli 	{ 4, 0x70, "RxJabbers" },
67967dd82fSFlorian Fainelli 	{ 4, 0x74, "RxAlignmentErrors" },
68967dd82fSFlorian Fainelli 	{ 4, 0x78, "RxFCSErrors" },
69967dd82fSFlorian Fainelli 	{ 8, 0x7c, "RxGoodOctets" },
70967dd82fSFlorian Fainelli 	{ 4, 0x84, "RxDropPkts" },
71967dd82fSFlorian Fainelli 	{ 4, 0x88, "RxUnicastPkts" },
72967dd82fSFlorian Fainelli 	{ 4, 0x8c, "RxMulticastPkts" },
73967dd82fSFlorian Fainelli 	{ 4, 0x90, "RxBroadcastPkts" },
74967dd82fSFlorian Fainelli 	{ 4, 0x94, "RxSAChanges" },
75967dd82fSFlorian Fainelli 	{ 4, 0x98, "RxFragments" },
76967dd82fSFlorian Fainelli };
77967dd82fSFlorian Fainelli 
78967dd82fSFlorian Fainelli #define B53_MIBS_65_SIZE	ARRAY_SIZE(b53_mibs_65)
79967dd82fSFlorian Fainelli 
80967dd82fSFlorian Fainelli /* BCM63xx MIB counters */
81967dd82fSFlorian Fainelli static const struct b53_mib_desc b53_mibs_63xx[] = {
82967dd82fSFlorian Fainelli 	{ 8, 0x00, "TxOctets" },
83967dd82fSFlorian Fainelli 	{ 4, 0x08, "TxDropPkts" },
84967dd82fSFlorian Fainelli 	{ 4, 0x0c, "TxQoSPkts" },
85967dd82fSFlorian Fainelli 	{ 4, 0x10, "TxBroadcastPkts" },
86967dd82fSFlorian Fainelli 	{ 4, 0x14, "TxMulticastPkts" },
87967dd82fSFlorian Fainelli 	{ 4, 0x18, "TxUnicastPkts" },
88967dd82fSFlorian Fainelli 	{ 4, 0x1c, "TxCollisions" },
89967dd82fSFlorian Fainelli 	{ 4, 0x20, "TxSingleCollision" },
90967dd82fSFlorian Fainelli 	{ 4, 0x24, "TxMultipleCollision" },
91967dd82fSFlorian Fainelli 	{ 4, 0x28, "TxDeferredTransmit" },
92967dd82fSFlorian Fainelli 	{ 4, 0x2c, "TxLateCollision" },
93967dd82fSFlorian Fainelli 	{ 4, 0x30, "TxExcessiveCollision" },
94967dd82fSFlorian Fainelli 	{ 4, 0x38, "TxPausePkts" },
95967dd82fSFlorian Fainelli 	{ 8, 0x3c, "TxQoSOctets" },
96967dd82fSFlorian Fainelli 	{ 8, 0x44, "RxOctets" },
97967dd82fSFlorian Fainelli 	{ 4, 0x4c, "RxUndersizePkts" },
98967dd82fSFlorian Fainelli 	{ 4, 0x50, "RxPausePkts" },
99967dd82fSFlorian Fainelli 	{ 4, 0x54, "Pkts64Octets" },
100967dd82fSFlorian Fainelli 	{ 4, 0x58, "Pkts65to127Octets" },
101967dd82fSFlorian Fainelli 	{ 4, 0x5c, "Pkts128to255Octets" },
102967dd82fSFlorian Fainelli 	{ 4, 0x60, "Pkts256to511Octets" },
103967dd82fSFlorian Fainelli 	{ 4, 0x64, "Pkts512to1023Octets" },
104967dd82fSFlorian Fainelli 	{ 4, 0x68, "Pkts1024to1522Octets" },
105967dd82fSFlorian Fainelli 	{ 4, 0x6c, "RxOversizePkts" },
106967dd82fSFlorian Fainelli 	{ 4, 0x70, "RxJabbers" },
107967dd82fSFlorian Fainelli 	{ 4, 0x74, "RxAlignmentErrors" },
108967dd82fSFlorian Fainelli 	{ 4, 0x78, "RxFCSErrors" },
109967dd82fSFlorian Fainelli 	{ 8, 0x7c, "RxGoodOctets" },
110967dd82fSFlorian Fainelli 	{ 4, 0x84, "RxDropPkts" },
111967dd82fSFlorian Fainelli 	{ 4, 0x88, "RxUnicastPkts" },
112967dd82fSFlorian Fainelli 	{ 4, 0x8c, "RxMulticastPkts" },
113967dd82fSFlorian Fainelli 	{ 4, 0x90, "RxBroadcastPkts" },
114967dd82fSFlorian Fainelli 	{ 4, 0x94, "RxSAChanges" },
115967dd82fSFlorian Fainelli 	{ 4, 0x98, "RxFragments" },
116967dd82fSFlorian Fainelli 	{ 4, 0xa0, "RxSymbolErrors" },
117967dd82fSFlorian Fainelli 	{ 4, 0xa4, "RxQoSPkts" },
118967dd82fSFlorian Fainelli 	{ 8, 0xa8, "RxQoSOctets" },
119967dd82fSFlorian Fainelli 	{ 4, 0xb0, "Pkts1523to2047Octets" },
120967dd82fSFlorian Fainelli 	{ 4, 0xb4, "Pkts2048to4095Octets" },
121967dd82fSFlorian Fainelli 	{ 4, 0xb8, "Pkts4096to8191Octets" },
122967dd82fSFlorian Fainelli 	{ 4, 0xbc, "Pkts8192to9728Octets" },
123967dd82fSFlorian Fainelli 	{ 4, 0xc0, "RxDiscarded" },
124967dd82fSFlorian Fainelli };
125967dd82fSFlorian Fainelli 
126967dd82fSFlorian Fainelli #define B53_MIBS_63XX_SIZE	ARRAY_SIZE(b53_mibs_63xx)
127967dd82fSFlorian Fainelli 
128967dd82fSFlorian Fainelli /* MIB counters */
129967dd82fSFlorian Fainelli static const struct b53_mib_desc b53_mibs[] = {
130967dd82fSFlorian Fainelli 	{ 8, 0x00, "TxOctets" },
131967dd82fSFlorian Fainelli 	{ 4, 0x08, "TxDropPkts" },
132967dd82fSFlorian Fainelli 	{ 4, 0x10, "TxBroadcastPkts" },
133967dd82fSFlorian Fainelli 	{ 4, 0x14, "TxMulticastPkts" },
134967dd82fSFlorian Fainelli 	{ 4, 0x18, "TxUnicastPkts" },
135967dd82fSFlorian Fainelli 	{ 4, 0x1c, "TxCollisions" },
136967dd82fSFlorian Fainelli 	{ 4, 0x20, "TxSingleCollision" },
137967dd82fSFlorian Fainelli 	{ 4, 0x24, "TxMultipleCollision" },
138967dd82fSFlorian Fainelli 	{ 4, 0x28, "TxDeferredTransmit" },
139967dd82fSFlorian Fainelli 	{ 4, 0x2c, "TxLateCollision" },
140967dd82fSFlorian Fainelli 	{ 4, 0x30, "TxExcessiveCollision" },
141967dd82fSFlorian Fainelli 	{ 4, 0x38, "TxPausePkts" },
142967dd82fSFlorian Fainelli 	{ 8, 0x50, "RxOctets" },
143967dd82fSFlorian Fainelli 	{ 4, 0x58, "RxUndersizePkts" },
144967dd82fSFlorian Fainelli 	{ 4, 0x5c, "RxPausePkts" },
145967dd82fSFlorian Fainelli 	{ 4, 0x60, "Pkts64Octets" },
146967dd82fSFlorian Fainelli 	{ 4, 0x64, "Pkts65to127Octets" },
147967dd82fSFlorian Fainelli 	{ 4, 0x68, "Pkts128to255Octets" },
148967dd82fSFlorian Fainelli 	{ 4, 0x6c, "Pkts256to511Octets" },
149967dd82fSFlorian Fainelli 	{ 4, 0x70, "Pkts512to1023Octets" },
150967dd82fSFlorian Fainelli 	{ 4, 0x74, "Pkts1024to1522Octets" },
151967dd82fSFlorian Fainelli 	{ 4, 0x78, "RxOversizePkts" },
152967dd82fSFlorian Fainelli 	{ 4, 0x7c, "RxJabbers" },
153967dd82fSFlorian Fainelli 	{ 4, 0x80, "RxAlignmentErrors" },
154967dd82fSFlorian Fainelli 	{ 4, 0x84, "RxFCSErrors" },
155967dd82fSFlorian Fainelli 	{ 8, 0x88, "RxGoodOctets" },
156967dd82fSFlorian Fainelli 	{ 4, 0x90, "RxDropPkts" },
157967dd82fSFlorian Fainelli 	{ 4, 0x94, "RxUnicastPkts" },
158967dd82fSFlorian Fainelli 	{ 4, 0x98, "RxMulticastPkts" },
159967dd82fSFlorian Fainelli 	{ 4, 0x9c, "RxBroadcastPkts" },
160967dd82fSFlorian Fainelli 	{ 4, 0xa0, "RxSAChanges" },
161967dd82fSFlorian Fainelli 	{ 4, 0xa4, "RxFragments" },
162967dd82fSFlorian Fainelli 	{ 4, 0xa8, "RxJumboPkts" },
163967dd82fSFlorian Fainelli 	{ 4, 0xac, "RxSymbolErrors" },
164967dd82fSFlorian Fainelli 	{ 4, 0xc0, "RxDiscarded" },
165967dd82fSFlorian Fainelli };
166967dd82fSFlorian Fainelli 
167967dd82fSFlorian Fainelli #define B53_MIBS_SIZE	ARRAY_SIZE(b53_mibs)
168967dd82fSFlorian Fainelli 
169bde5d132SFlorian Fainelli static const struct b53_mib_desc b53_mibs_58xx[] = {
170bde5d132SFlorian Fainelli 	{ 8, 0x00, "TxOctets" },
171bde5d132SFlorian Fainelli 	{ 4, 0x08, "TxDropPkts" },
172bde5d132SFlorian Fainelli 	{ 4, 0x0c, "TxQPKTQ0" },
173bde5d132SFlorian Fainelli 	{ 4, 0x10, "TxBroadcastPkts" },
174bde5d132SFlorian Fainelli 	{ 4, 0x14, "TxMulticastPkts" },
175bde5d132SFlorian Fainelli 	{ 4, 0x18, "TxUnicastPKts" },
176bde5d132SFlorian Fainelli 	{ 4, 0x1c, "TxCollisions" },
177bde5d132SFlorian Fainelli 	{ 4, 0x20, "TxSingleCollision" },
178bde5d132SFlorian Fainelli 	{ 4, 0x24, "TxMultipleCollision" },
179bde5d132SFlorian Fainelli 	{ 4, 0x28, "TxDeferredCollision" },
180bde5d132SFlorian Fainelli 	{ 4, 0x2c, "TxLateCollision" },
181bde5d132SFlorian Fainelli 	{ 4, 0x30, "TxExcessiveCollision" },
182bde5d132SFlorian Fainelli 	{ 4, 0x34, "TxFrameInDisc" },
183bde5d132SFlorian Fainelli 	{ 4, 0x38, "TxPausePkts" },
184bde5d132SFlorian Fainelli 	{ 4, 0x3c, "TxQPKTQ1" },
185bde5d132SFlorian Fainelli 	{ 4, 0x40, "TxQPKTQ2" },
186bde5d132SFlorian Fainelli 	{ 4, 0x44, "TxQPKTQ3" },
187bde5d132SFlorian Fainelli 	{ 4, 0x48, "TxQPKTQ4" },
188bde5d132SFlorian Fainelli 	{ 4, 0x4c, "TxQPKTQ5" },
189bde5d132SFlorian Fainelli 	{ 8, 0x50, "RxOctets" },
190bde5d132SFlorian Fainelli 	{ 4, 0x58, "RxUndersizePkts" },
191bde5d132SFlorian Fainelli 	{ 4, 0x5c, "RxPausePkts" },
192bde5d132SFlorian Fainelli 	{ 4, 0x60, "RxPkts64Octets" },
193bde5d132SFlorian Fainelli 	{ 4, 0x64, "RxPkts65to127Octets" },
194bde5d132SFlorian Fainelli 	{ 4, 0x68, "RxPkts128to255Octets" },
195bde5d132SFlorian Fainelli 	{ 4, 0x6c, "RxPkts256to511Octets" },
196bde5d132SFlorian Fainelli 	{ 4, 0x70, "RxPkts512to1023Octets" },
197bde5d132SFlorian Fainelli 	{ 4, 0x74, "RxPkts1024toMaxPktsOctets" },
198bde5d132SFlorian Fainelli 	{ 4, 0x78, "RxOversizePkts" },
199bde5d132SFlorian Fainelli 	{ 4, 0x7c, "RxJabbers" },
200bde5d132SFlorian Fainelli 	{ 4, 0x80, "RxAlignmentErrors" },
201bde5d132SFlorian Fainelli 	{ 4, 0x84, "RxFCSErrors" },
202bde5d132SFlorian Fainelli 	{ 8, 0x88, "RxGoodOctets" },
203bde5d132SFlorian Fainelli 	{ 4, 0x90, "RxDropPkts" },
204bde5d132SFlorian Fainelli 	{ 4, 0x94, "RxUnicastPkts" },
205bde5d132SFlorian Fainelli 	{ 4, 0x98, "RxMulticastPkts" },
206bde5d132SFlorian Fainelli 	{ 4, 0x9c, "RxBroadcastPkts" },
207bde5d132SFlorian Fainelli 	{ 4, 0xa0, "RxSAChanges" },
208bde5d132SFlorian Fainelli 	{ 4, 0xa4, "RxFragments" },
209bde5d132SFlorian Fainelli 	{ 4, 0xa8, "RxJumboPkt" },
210bde5d132SFlorian Fainelli 	{ 4, 0xac, "RxSymblErr" },
211bde5d132SFlorian Fainelli 	{ 4, 0xb0, "InRangeErrCount" },
212bde5d132SFlorian Fainelli 	{ 4, 0xb4, "OutRangeErrCount" },
213bde5d132SFlorian Fainelli 	{ 4, 0xb8, "EEELpiEvent" },
214bde5d132SFlorian Fainelli 	{ 4, 0xbc, "EEELpiDuration" },
215bde5d132SFlorian Fainelli 	{ 4, 0xc0, "RxDiscard" },
216bde5d132SFlorian Fainelli 	{ 4, 0xc8, "TxQPKTQ6" },
217bde5d132SFlorian Fainelli 	{ 4, 0xcc, "TxQPKTQ7" },
218bde5d132SFlorian Fainelli 	{ 4, 0xd0, "TxPkts64Octets" },
219bde5d132SFlorian Fainelli 	{ 4, 0xd4, "TxPkts65to127Octets" },
220bde5d132SFlorian Fainelli 	{ 4, 0xd8, "TxPkts128to255Octets" },
221bde5d132SFlorian Fainelli 	{ 4, 0xdc, "TxPkts256to511Ocets" },
222bde5d132SFlorian Fainelli 	{ 4, 0xe0, "TxPkts512to1023Ocets" },
223bde5d132SFlorian Fainelli 	{ 4, 0xe4, "TxPkts1024toMaxPktOcets" },
224bde5d132SFlorian Fainelli };
225bde5d132SFlorian Fainelli 
226bde5d132SFlorian Fainelli #define B53_MIBS_58XX_SIZE	ARRAY_SIZE(b53_mibs_58xx)
227bde5d132SFlorian Fainelli 
228967dd82fSFlorian Fainelli static int b53_do_vlan_op(struct b53_device *dev, u8 op)
229967dd82fSFlorian Fainelli {
230967dd82fSFlorian Fainelli 	unsigned int i;
231967dd82fSFlorian Fainelli 
232967dd82fSFlorian Fainelli 	b53_write8(dev, B53_ARLIO_PAGE, dev->vta_regs[0], VTA_START_CMD | op);
233967dd82fSFlorian Fainelli 
234967dd82fSFlorian Fainelli 	for (i = 0; i < 10; i++) {
235967dd82fSFlorian Fainelli 		u8 vta;
236967dd82fSFlorian Fainelli 
237967dd82fSFlorian Fainelli 		b53_read8(dev, B53_ARLIO_PAGE, dev->vta_regs[0], &vta);
238967dd82fSFlorian Fainelli 		if (!(vta & VTA_START_CMD))
239967dd82fSFlorian Fainelli 			return 0;
240967dd82fSFlorian Fainelli 
241967dd82fSFlorian Fainelli 		usleep_range(100, 200);
242967dd82fSFlorian Fainelli 	}
243967dd82fSFlorian Fainelli 
244967dd82fSFlorian Fainelli 	return -EIO;
245967dd82fSFlorian Fainelli }
246967dd82fSFlorian Fainelli 
247a2482d2cSFlorian Fainelli static void b53_set_vlan_entry(struct b53_device *dev, u16 vid,
248a2482d2cSFlorian Fainelli 			       struct b53_vlan *vlan)
249967dd82fSFlorian Fainelli {
250967dd82fSFlorian Fainelli 	if (is5325(dev)) {
251967dd82fSFlorian Fainelli 		u32 entry = 0;
252967dd82fSFlorian Fainelli 
253a2482d2cSFlorian Fainelli 		if (vlan->members) {
254a2482d2cSFlorian Fainelli 			entry = ((vlan->untag & VA_UNTAG_MASK_25) <<
255a2482d2cSFlorian Fainelli 				 VA_UNTAG_S_25) | vlan->members;
256967dd82fSFlorian Fainelli 			if (dev->core_rev >= 3)
257967dd82fSFlorian Fainelli 				entry |= VA_VALID_25_R4 | vid << VA_VID_HIGH_S;
258967dd82fSFlorian Fainelli 			else
259967dd82fSFlorian Fainelli 				entry |= VA_VALID_25;
260967dd82fSFlorian Fainelli 		}
261967dd82fSFlorian Fainelli 
262967dd82fSFlorian Fainelli 		b53_write32(dev, B53_VLAN_PAGE, B53_VLAN_WRITE_25, entry);
263967dd82fSFlorian Fainelli 		b53_write16(dev, B53_VLAN_PAGE, B53_VLAN_TABLE_ACCESS_25, vid |
264967dd82fSFlorian Fainelli 			    VTA_RW_STATE_WR | VTA_RW_OP_EN);
265967dd82fSFlorian Fainelli 	} else if (is5365(dev)) {
266967dd82fSFlorian Fainelli 		u16 entry = 0;
267967dd82fSFlorian Fainelli 
268a2482d2cSFlorian Fainelli 		if (vlan->members)
269a2482d2cSFlorian Fainelli 			entry = ((vlan->untag & VA_UNTAG_MASK_65) <<
270a2482d2cSFlorian Fainelli 				 VA_UNTAG_S_65) | vlan->members | VA_VALID_65;
271967dd82fSFlorian Fainelli 
272967dd82fSFlorian Fainelli 		b53_write16(dev, B53_VLAN_PAGE, B53_VLAN_WRITE_65, entry);
273967dd82fSFlorian Fainelli 		b53_write16(dev, B53_VLAN_PAGE, B53_VLAN_TABLE_ACCESS_65, vid |
274967dd82fSFlorian Fainelli 			    VTA_RW_STATE_WR | VTA_RW_OP_EN);
275967dd82fSFlorian Fainelli 	} else {
276967dd82fSFlorian Fainelli 		b53_write16(dev, B53_ARLIO_PAGE, dev->vta_regs[1], vid);
277967dd82fSFlorian Fainelli 		b53_write32(dev, B53_ARLIO_PAGE, dev->vta_regs[2],
278a2482d2cSFlorian Fainelli 			    (vlan->untag << VTE_UNTAG_S) | vlan->members);
279967dd82fSFlorian Fainelli 
280967dd82fSFlorian Fainelli 		b53_do_vlan_op(dev, VTA_CMD_WRITE);
281967dd82fSFlorian Fainelli 	}
282a2482d2cSFlorian Fainelli 
283a2482d2cSFlorian Fainelli 	dev_dbg(dev->ds->dev, "VID: %d, members: 0x%04x, untag: 0x%04x\n",
284a2482d2cSFlorian Fainelli 		vid, vlan->members, vlan->untag);
285967dd82fSFlorian Fainelli }
286967dd82fSFlorian Fainelli 
287a2482d2cSFlorian Fainelli static void b53_get_vlan_entry(struct b53_device *dev, u16 vid,
288a2482d2cSFlorian Fainelli 			       struct b53_vlan *vlan)
289a2482d2cSFlorian Fainelli {
290a2482d2cSFlorian Fainelli 	if (is5325(dev)) {
291a2482d2cSFlorian Fainelli 		u32 entry = 0;
292a2482d2cSFlorian Fainelli 
293a2482d2cSFlorian Fainelli 		b53_write16(dev, B53_VLAN_PAGE, B53_VLAN_TABLE_ACCESS_25, vid |
294a2482d2cSFlorian Fainelli 			    VTA_RW_STATE_RD | VTA_RW_OP_EN);
295a2482d2cSFlorian Fainelli 		b53_read32(dev, B53_VLAN_PAGE, B53_VLAN_WRITE_25, &entry);
296a2482d2cSFlorian Fainelli 
297a2482d2cSFlorian Fainelli 		if (dev->core_rev >= 3)
298a2482d2cSFlorian Fainelli 			vlan->valid = !!(entry & VA_VALID_25_R4);
299a2482d2cSFlorian Fainelli 		else
300a2482d2cSFlorian Fainelli 			vlan->valid = !!(entry & VA_VALID_25);
301a2482d2cSFlorian Fainelli 		vlan->members = entry & VA_MEMBER_MASK;
302a2482d2cSFlorian Fainelli 		vlan->untag = (entry >> VA_UNTAG_S_25) & VA_UNTAG_MASK_25;
303a2482d2cSFlorian Fainelli 
304a2482d2cSFlorian Fainelli 	} else if (is5365(dev)) {
305a2482d2cSFlorian Fainelli 		u16 entry = 0;
306a2482d2cSFlorian Fainelli 
307a2482d2cSFlorian Fainelli 		b53_write16(dev, B53_VLAN_PAGE, B53_VLAN_TABLE_ACCESS_65, vid |
308a2482d2cSFlorian Fainelli 			    VTA_RW_STATE_WR | VTA_RW_OP_EN);
309a2482d2cSFlorian Fainelli 		b53_read16(dev, B53_VLAN_PAGE, B53_VLAN_WRITE_65, &entry);
310a2482d2cSFlorian Fainelli 
311a2482d2cSFlorian Fainelli 		vlan->valid = !!(entry & VA_VALID_65);
312a2482d2cSFlorian Fainelli 		vlan->members = entry & VA_MEMBER_MASK;
313a2482d2cSFlorian Fainelli 		vlan->untag = (entry >> VA_UNTAG_S_65) & VA_UNTAG_MASK_65;
314a2482d2cSFlorian Fainelli 	} else {
315a2482d2cSFlorian Fainelli 		u32 entry = 0;
316a2482d2cSFlorian Fainelli 
317a2482d2cSFlorian Fainelli 		b53_write16(dev, B53_ARLIO_PAGE, dev->vta_regs[1], vid);
318a2482d2cSFlorian Fainelli 		b53_do_vlan_op(dev, VTA_CMD_READ);
319a2482d2cSFlorian Fainelli 		b53_read32(dev, B53_ARLIO_PAGE, dev->vta_regs[2], &entry);
320a2482d2cSFlorian Fainelli 		vlan->members = entry & VTE_MEMBERS;
321a2482d2cSFlorian Fainelli 		vlan->untag = (entry >> VTE_UNTAG_S) & VTE_MEMBERS;
322a2482d2cSFlorian Fainelli 		vlan->valid = true;
323a2482d2cSFlorian Fainelli 	}
324a2482d2cSFlorian Fainelli }
325a2482d2cSFlorian Fainelli 
326a2482d2cSFlorian Fainelli static void b53_set_forwarding(struct b53_device *dev, int enable)
327967dd82fSFlorian Fainelli {
328a424f0deSFlorian Fainelli 	struct dsa_switch *ds = dev->ds;
329967dd82fSFlorian Fainelli 	u8 mgmt;
330967dd82fSFlorian Fainelli 
331967dd82fSFlorian Fainelli 	b53_read8(dev, B53_CTRL_PAGE, B53_SWITCH_MODE, &mgmt);
332967dd82fSFlorian Fainelli 
333967dd82fSFlorian Fainelli 	if (enable)
334967dd82fSFlorian Fainelli 		mgmt |= SM_SW_FWD_EN;
335967dd82fSFlorian Fainelli 	else
336967dd82fSFlorian Fainelli 		mgmt &= ~SM_SW_FWD_EN;
337967dd82fSFlorian Fainelli 
338967dd82fSFlorian Fainelli 	b53_write8(dev, B53_CTRL_PAGE, B53_SWITCH_MODE, mgmt);
339a424f0deSFlorian Fainelli 
340a424f0deSFlorian Fainelli 	/* Include IMP port in dumb forwarding mode when no tagging protocol is
341a424f0deSFlorian Fainelli 	 * set
342a424f0deSFlorian Fainelli 	 */
343a424f0deSFlorian Fainelli 	if (ds->ops->get_tag_protocol(ds) == DSA_TAG_PROTO_NONE) {
344a424f0deSFlorian Fainelli 		b53_read8(dev, B53_CTRL_PAGE, B53_SWITCH_CTRL, &mgmt);
345a424f0deSFlorian Fainelli 		mgmt |= B53_MII_DUMB_FWDG_EN;
346a424f0deSFlorian Fainelli 		b53_write8(dev, B53_CTRL_PAGE, B53_SWITCH_CTRL, mgmt);
347a424f0deSFlorian Fainelli 	}
348967dd82fSFlorian Fainelli }
349967dd82fSFlorian Fainelli 
350a2482d2cSFlorian Fainelli static void b53_enable_vlan(struct b53_device *dev, bool enable)
351967dd82fSFlorian Fainelli {
352967dd82fSFlorian Fainelli 	u8 mgmt, vc0, vc1, vc4 = 0, vc5;
353967dd82fSFlorian Fainelli 
354967dd82fSFlorian Fainelli 	b53_read8(dev, B53_CTRL_PAGE, B53_SWITCH_MODE, &mgmt);
355967dd82fSFlorian Fainelli 	b53_read8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL0, &vc0);
356967dd82fSFlorian Fainelli 	b53_read8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL1, &vc1);
357967dd82fSFlorian Fainelli 
358967dd82fSFlorian Fainelli 	if (is5325(dev) || is5365(dev)) {
359967dd82fSFlorian Fainelli 		b53_read8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL4_25, &vc4);
360967dd82fSFlorian Fainelli 		b53_read8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL5_25, &vc5);
361967dd82fSFlorian Fainelli 	} else if (is63xx(dev)) {
362967dd82fSFlorian Fainelli 		b53_read8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL4_63XX, &vc4);
363967dd82fSFlorian Fainelli 		b53_read8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL5_63XX, &vc5);
364967dd82fSFlorian Fainelli 	} else {
365967dd82fSFlorian Fainelli 		b53_read8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL4, &vc4);
366967dd82fSFlorian Fainelli 		b53_read8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL5, &vc5);
367967dd82fSFlorian Fainelli 	}
368967dd82fSFlorian Fainelli 
369967dd82fSFlorian Fainelli 	mgmt &= ~SM_SW_FWD_MODE;
370967dd82fSFlorian Fainelli 
371967dd82fSFlorian Fainelli 	if (enable) {
372967dd82fSFlorian Fainelli 		vc0 |= VC0_VLAN_EN | VC0_VID_CHK_EN | VC0_VID_HASH_VID;
373967dd82fSFlorian Fainelli 		vc1 |= VC1_RX_MCST_UNTAG_EN | VC1_RX_MCST_FWD_EN;
374967dd82fSFlorian Fainelli 		vc4 &= ~VC4_ING_VID_CHECK_MASK;
375967dd82fSFlorian Fainelli 		vc4 |= VC4_ING_VID_VIO_DROP << VC4_ING_VID_CHECK_S;
376967dd82fSFlorian Fainelli 		vc5 |= VC5_DROP_VTABLE_MISS;
377967dd82fSFlorian Fainelli 
378967dd82fSFlorian Fainelli 		if (is5325(dev))
379967dd82fSFlorian Fainelli 			vc0 &= ~VC0_RESERVED_1;
380967dd82fSFlorian Fainelli 
381967dd82fSFlorian Fainelli 		if (is5325(dev) || is5365(dev))
382967dd82fSFlorian Fainelli 			vc1 |= VC1_RX_MCST_TAG_EN;
383967dd82fSFlorian Fainelli 
384967dd82fSFlorian Fainelli 	} else {
385967dd82fSFlorian Fainelli 		vc0 &= ~(VC0_VLAN_EN | VC0_VID_CHK_EN | VC0_VID_HASH_VID);
386967dd82fSFlorian Fainelli 		vc1 &= ~(VC1_RX_MCST_UNTAG_EN | VC1_RX_MCST_FWD_EN);
387967dd82fSFlorian Fainelli 		vc4 &= ~VC4_ING_VID_CHECK_MASK;
388967dd82fSFlorian Fainelli 		vc5 &= ~VC5_DROP_VTABLE_MISS;
389967dd82fSFlorian Fainelli 
390967dd82fSFlorian Fainelli 		if (is5325(dev) || is5365(dev))
391967dd82fSFlorian Fainelli 			vc4 |= VC4_ING_VID_VIO_FWD << VC4_ING_VID_CHECK_S;
392967dd82fSFlorian Fainelli 		else
393967dd82fSFlorian Fainelli 			vc4 |= VC4_ING_VID_VIO_TO_IMP << VC4_ING_VID_CHECK_S;
394967dd82fSFlorian Fainelli 
395967dd82fSFlorian Fainelli 		if (is5325(dev) || is5365(dev))
396967dd82fSFlorian Fainelli 			vc1 &= ~VC1_RX_MCST_TAG_EN;
397a2482d2cSFlorian Fainelli 	}
398967dd82fSFlorian Fainelli 
399967dd82fSFlorian Fainelli 	if (!is5325(dev) && !is5365(dev))
400967dd82fSFlorian Fainelli 		vc5 &= ~VC5_VID_FFF_EN;
401967dd82fSFlorian Fainelli 
402967dd82fSFlorian Fainelli 	b53_write8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL0, vc0);
403967dd82fSFlorian Fainelli 	b53_write8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL1, vc1);
404967dd82fSFlorian Fainelli 
405967dd82fSFlorian Fainelli 	if (is5325(dev) || is5365(dev)) {
406967dd82fSFlorian Fainelli 		/* enable the high 8 bit vid check on 5325 */
407967dd82fSFlorian Fainelli 		if (is5325(dev) && enable)
408967dd82fSFlorian Fainelli 			b53_write8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL3,
409967dd82fSFlorian Fainelli 				   VC3_HIGH_8BIT_EN);
410967dd82fSFlorian Fainelli 		else
411967dd82fSFlorian Fainelli 			b53_write8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL3, 0);
412967dd82fSFlorian Fainelli 
413967dd82fSFlorian Fainelli 		b53_write8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL4_25, vc4);
414967dd82fSFlorian Fainelli 		b53_write8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL5_25, vc5);
415967dd82fSFlorian Fainelli 	} else if (is63xx(dev)) {
416967dd82fSFlorian Fainelli 		b53_write16(dev, B53_VLAN_PAGE, B53_VLAN_CTRL3_63XX, 0);
417967dd82fSFlorian Fainelli 		b53_write8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL4_63XX, vc4);
418967dd82fSFlorian Fainelli 		b53_write8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL5_63XX, vc5);
419967dd82fSFlorian Fainelli 	} else {
420967dd82fSFlorian Fainelli 		b53_write16(dev, B53_VLAN_PAGE, B53_VLAN_CTRL3, 0);
421967dd82fSFlorian Fainelli 		b53_write8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL4, vc4);
422967dd82fSFlorian Fainelli 		b53_write8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL5, vc5);
423967dd82fSFlorian Fainelli 	}
424967dd82fSFlorian Fainelli 
425967dd82fSFlorian Fainelli 	b53_write8(dev, B53_CTRL_PAGE, B53_SWITCH_MODE, mgmt);
426967dd82fSFlorian Fainelli }
427967dd82fSFlorian Fainelli 
428967dd82fSFlorian Fainelli static int b53_set_jumbo(struct b53_device *dev, bool enable, bool allow_10_100)
429967dd82fSFlorian Fainelli {
430967dd82fSFlorian Fainelli 	u32 port_mask = 0;
431967dd82fSFlorian Fainelli 	u16 max_size = JMS_MIN_SIZE;
432967dd82fSFlorian Fainelli 
433967dd82fSFlorian Fainelli 	if (is5325(dev) || is5365(dev))
434967dd82fSFlorian Fainelli 		return -EINVAL;
435967dd82fSFlorian Fainelli 
436967dd82fSFlorian Fainelli 	if (enable) {
437967dd82fSFlorian Fainelli 		port_mask = dev->enabled_ports;
438967dd82fSFlorian Fainelli 		max_size = JMS_MAX_SIZE;
439967dd82fSFlorian Fainelli 		if (allow_10_100)
440967dd82fSFlorian Fainelli 			port_mask |= JPM_10_100_JUMBO_EN;
441967dd82fSFlorian Fainelli 	}
442967dd82fSFlorian Fainelli 
443967dd82fSFlorian Fainelli 	b53_write32(dev, B53_JUMBO_PAGE, dev->jumbo_pm_reg, port_mask);
444967dd82fSFlorian Fainelli 	return b53_write16(dev, B53_JUMBO_PAGE, dev->jumbo_size_reg, max_size);
445967dd82fSFlorian Fainelli }
446967dd82fSFlorian Fainelli 
447ff39c2d6SFlorian Fainelli static int b53_flush_arl(struct b53_device *dev, u8 mask)
448967dd82fSFlorian Fainelli {
449967dd82fSFlorian Fainelli 	unsigned int i;
450967dd82fSFlorian Fainelli 
451967dd82fSFlorian Fainelli 	b53_write8(dev, B53_CTRL_PAGE, B53_FAST_AGE_CTRL,
452ff39c2d6SFlorian Fainelli 		   FAST_AGE_DONE | FAST_AGE_DYNAMIC | mask);
453967dd82fSFlorian Fainelli 
454967dd82fSFlorian Fainelli 	for (i = 0; i < 10; i++) {
455967dd82fSFlorian Fainelli 		u8 fast_age_ctrl;
456967dd82fSFlorian Fainelli 
457967dd82fSFlorian Fainelli 		b53_read8(dev, B53_CTRL_PAGE, B53_FAST_AGE_CTRL,
458967dd82fSFlorian Fainelli 			  &fast_age_ctrl);
459967dd82fSFlorian Fainelli 
460967dd82fSFlorian Fainelli 		if (!(fast_age_ctrl & FAST_AGE_DONE))
461967dd82fSFlorian Fainelli 			goto out;
462967dd82fSFlorian Fainelli 
463967dd82fSFlorian Fainelli 		msleep(1);
464967dd82fSFlorian Fainelli 	}
465967dd82fSFlorian Fainelli 
466967dd82fSFlorian Fainelli 	return -ETIMEDOUT;
467967dd82fSFlorian Fainelli out:
468967dd82fSFlorian Fainelli 	/* Only age dynamic entries (default behavior) */
469967dd82fSFlorian Fainelli 	b53_write8(dev, B53_CTRL_PAGE, B53_FAST_AGE_CTRL, FAST_AGE_DYNAMIC);
470967dd82fSFlorian Fainelli 	return 0;
471967dd82fSFlorian Fainelli }
472967dd82fSFlorian Fainelli 
473ff39c2d6SFlorian Fainelli static int b53_fast_age_port(struct b53_device *dev, int port)
474ff39c2d6SFlorian Fainelli {
475ff39c2d6SFlorian Fainelli 	b53_write8(dev, B53_CTRL_PAGE, B53_FAST_AGE_PORT_CTRL, port);
476ff39c2d6SFlorian Fainelli 
477ff39c2d6SFlorian Fainelli 	return b53_flush_arl(dev, FAST_AGE_PORT);
478ff39c2d6SFlorian Fainelli }
479ff39c2d6SFlorian Fainelli 
480a2482d2cSFlorian Fainelli static int b53_fast_age_vlan(struct b53_device *dev, u16 vid)
481a2482d2cSFlorian Fainelli {
482a2482d2cSFlorian Fainelli 	b53_write16(dev, B53_CTRL_PAGE, B53_FAST_AGE_VID_CTRL, vid);
483a2482d2cSFlorian Fainelli 
484a2482d2cSFlorian Fainelli 	return b53_flush_arl(dev, FAST_AGE_VLAN);
485a2482d2cSFlorian Fainelli }
486a2482d2cSFlorian Fainelli 
487ff39c2d6SFlorian Fainelli static void b53_imp_vlan_setup(struct dsa_switch *ds, int cpu_port)
488ff39c2d6SFlorian Fainelli {
48904bed143SVivien Didelot 	struct b53_device *dev = ds->priv;
490ff39c2d6SFlorian Fainelli 	unsigned int i;
491ff39c2d6SFlorian Fainelli 	u16 pvlan;
492ff39c2d6SFlorian Fainelli 
493ff39c2d6SFlorian Fainelli 	/* Enable the IMP port to be in the same VLAN as the other ports
494ff39c2d6SFlorian Fainelli 	 * on a per-port basis such that we only have Port i and IMP in
495ff39c2d6SFlorian Fainelli 	 * the same VLAN.
496ff39c2d6SFlorian Fainelli 	 */
497ff39c2d6SFlorian Fainelli 	b53_for_each_port(dev, i) {
498ff39c2d6SFlorian Fainelli 		b53_read16(dev, B53_PVLAN_PAGE, B53_PVLAN_PORT_MASK(i), &pvlan);
499ff39c2d6SFlorian Fainelli 		pvlan |= BIT(cpu_port);
500ff39c2d6SFlorian Fainelli 		b53_write16(dev, B53_PVLAN_PAGE, B53_PVLAN_PORT_MASK(i), pvlan);
501ff39c2d6SFlorian Fainelli 	}
502ff39c2d6SFlorian Fainelli }
503ff39c2d6SFlorian Fainelli 
504967dd82fSFlorian Fainelli static int b53_enable_port(struct dsa_switch *ds, int port,
505967dd82fSFlorian Fainelli 			   struct phy_device *phy)
506967dd82fSFlorian Fainelli {
50704bed143SVivien Didelot 	struct b53_device *dev = ds->priv;
508ff39c2d6SFlorian Fainelli 	unsigned int cpu_port = dev->cpu_port;
509ff39c2d6SFlorian Fainelli 	u16 pvlan;
510967dd82fSFlorian Fainelli 
511967dd82fSFlorian Fainelli 	/* Clear the Rx and Tx disable bits and set to no spanning tree */
512967dd82fSFlorian Fainelli 	b53_write8(dev, B53_CTRL_PAGE, B53_PORT_CTRL(port), 0);
513967dd82fSFlorian Fainelli 
514ff39c2d6SFlorian Fainelli 	/* Set this port, and only this one to be in the default VLAN,
515ff39c2d6SFlorian Fainelli 	 * if member of a bridge, restore its membership prior to
516ff39c2d6SFlorian Fainelli 	 * bringing down this port.
517ff39c2d6SFlorian Fainelli 	 */
518ff39c2d6SFlorian Fainelli 	b53_read16(dev, B53_PVLAN_PAGE, B53_PVLAN_PORT_MASK(port), &pvlan);
519ff39c2d6SFlorian Fainelli 	pvlan &= ~0x1ff;
520ff39c2d6SFlorian Fainelli 	pvlan |= BIT(port);
521ff39c2d6SFlorian Fainelli 	pvlan |= dev->ports[port].vlan_ctl_mask;
522ff39c2d6SFlorian Fainelli 	b53_write16(dev, B53_PVLAN_PAGE, B53_PVLAN_PORT_MASK(port), pvlan);
523ff39c2d6SFlorian Fainelli 
524ff39c2d6SFlorian Fainelli 	b53_imp_vlan_setup(ds, cpu_port);
525ff39c2d6SFlorian Fainelli 
526967dd82fSFlorian Fainelli 	return 0;
527967dd82fSFlorian Fainelli }
528967dd82fSFlorian Fainelli 
529967dd82fSFlorian Fainelli static void b53_disable_port(struct dsa_switch *ds, int port,
530967dd82fSFlorian Fainelli 			     struct phy_device *phy)
531967dd82fSFlorian Fainelli {
53204bed143SVivien Didelot 	struct b53_device *dev = ds->priv;
533967dd82fSFlorian Fainelli 	u8 reg;
534967dd82fSFlorian Fainelli 
535967dd82fSFlorian Fainelli 	/* Disable Tx/Rx for the port */
536967dd82fSFlorian Fainelli 	b53_read8(dev, B53_CTRL_PAGE, B53_PORT_CTRL(port), &reg);
537967dd82fSFlorian Fainelli 	reg |= PORT_CTRL_RX_DISABLE | PORT_CTRL_TX_DISABLE;
538967dd82fSFlorian Fainelli 	b53_write8(dev, B53_CTRL_PAGE, B53_PORT_CTRL(port), reg);
539967dd82fSFlorian Fainelli }
540967dd82fSFlorian Fainelli 
541*b409a9efSFlorian Fainelli void b53_brcm_hdr_setup(struct dsa_switch *ds, int port)
542*b409a9efSFlorian Fainelli {
543*b409a9efSFlorian Fainelli 	struct b53_device *dev = ds->priv;
544*b409a9efSFlorian Fainelli 	u8 hdr_ctl, val;
545*b409a9efSFlorian Fainelli 	u16 reg;
546*b409a9efSFlorian Fainelli 
547*b409a9efSFlorian Fainelli 	/* Resolve which bit controls the Broadcom tag */
548*b409a9efSFlorian Fainelli 	switch (port) {
549*b409a9efSFlorian Fainelli 	case 8:
550*b409a9efSFlorian Fainelli 		val = BRCM_HDR_P8_EN;
551*b409a9efSFlorian Fainelli 		break;
552*b409a9efSFlorian Fainelli 	case 7:
553*b409a9efSFlorian Fainelli 		val = BRCM_HDR_P7_EN;
554*b409a9efSFlorian Fainelli 		break;
555*b409a9efSFlorian Fainelli 	case 5:
556*b409a9efSFlorian Fainelli 		val = BRCM_HDR_P5_EN;
557*b409a9efSFlorian Fainelli 		break;
558*b409a9efSFlorian Fainelli 	default:
559*b409a9efSFlorian Fainelli 		val = 0;
560*b409a9efSFlorian Fainelli 		break;
561*b409a9efSFlorian Fainelli 	}
562*b409a9efSFlorian Fainelli 
563*b409a9efSFlorian Fainelli 	/* Enable Broadcom tags for IMP port */
564*b409a9efSFlorian Fainelli 	b53_read8(dev, B53_MGMT_PAGE, B53_BRCM_HDR, &hdr_ctl);
565*b409a9efSFlorian Fainelli 	hdr_ctl |= val;
566*b409a9efSFlorian Fainelli 	b53_write8(dev, B53_MGMT_PAGE, B53_BRCM_HDR, hdr_ctl);
567*b409a9efSFlorian Fainelli 
568*b409a9efSFlorian Fainelli 	/* Registers below are only accessible on newer devices */
569*b409a9efSFlorian Fainelli 	if (!is58xx(dev))
570*b409a9efSFlorian Fainelli 		return;
571*b409a9efSFlorian Fainelli 
572*b409a9efSFlorian Fainelli 	/* Enable reception Broadcom tag for CPU TX (switch RX) to
573*b409a9efSFlorian Fainelli 	 * allow us to tag outgoing frames
574*b409a9efSFlorian Fainelli 	 */
575*b409a9efSFlorian Fainelli 	b53_read16(dev, B53_MGMT_PAGE, B53_BRCM_HDR_RX_DIS, &reg);
576*b409a9efSFlorian Fainelli 	reg &= ~BIT(port);
577*b409a9efSFlorian Fainelli 	b53_write16(dev, B53_MGMT_PAGE, B53_BRCM_HDR_RX_DIS, reg);
578*b409a9efSFlorian Fainelli 
579*b409a9efSFlorian Fainelli 	/* Enable transmission of Broadcom tags from the switch (CPU RX) to
580*b409a9efSFlorian Fainelli 	 * allow delivering frames to the per-port net_devices
581*b409a9efSFlorian Fainelli 	 */
582*b409a9efSFlorian Fainelli 	b53_read16(dev, B53_MGMT_PAGE, B53_BRCM_HDR_TX_DIS, &reg);
583*b409a9efSFlorian Fainelli 	reg &= ~BIT(port);
584*b409a9efSFlorian Fainelli 	b53_write16(dev, B53_MGMT_PAGE, B53_BRCM_HDR_TX_DIS, reg);
585*b409a9efSFlorian Fainelli }
586*b409a9efSFlorian Fainelli EXPORT_SYMBOL(b53_brcm_hdr_setup);
587*b409a9efSFlorian Fainelli 
588299752a7SFlorian Fainelli static void b53_enable_cpu_port(struct b53_device *dev, int port)
589967dd82fSFlorian Fainelli {
590967dd82fSFlorian Fainelli 	u8 port_ctrl;
591967dd82fSFlorian Fainelli 
592967dd82fSFlorian Fainelli 	/* BCM5325 CPU port is at 8 */
593299752a7SFlorian Fainelli 	if ((is5325(dev) || is5365(dev)) && port == B53_CPU_PORT_25)
594299752a7SFlorian Fainelli 		port = B53_CPU_PORT;
595967dd82fSFlorian Fainelli 
596967dd82fSFlorian Fainelli 	port_ctrl = PORT_CTRL_RX_BCST_EN |
597967dd82fSFlorian Fainelli 		    PORT_CTRL_RX_MCST_EN |
598967dd82fSFlorian Fainelli 		    PORT_CTRL_RX_UCST_EN;
599299752a7SFlorian Fainelli 	b53_write8(dev, B53_CTRL_PAGE, B53_PORT_CTRL(port), port_ctrl);
600967dd82fSFlorian Fainelli }
601967dd82fSFlorian Fainelli 
602967dd82fSFlorian Fainelli static void b53_enable_mib(struct b53_device *dev)
603967dd82fSFlorian Fainelli {
604967dd82fSFlorian Fainelli 	u8 gc;
605967dd82fSFlorian Fainelli 
606967dd82fSFlorian Fainelli 	b53_read8(dev, B53_MGMT_PAGE, B53_GLOBAL_CONFIG, &gc);
607967dd82fSFlorian Fainelli 	gc &= ~(GC_RESET_MIB | GC_MIB_AC_EN);
608967dd82fSFlorian Fainelli 	b53_write8(dev, B53_MGMT_PAGE, B53_GLOBAL_CONFIG, gc);
609967dd82fSFlorian Fainelli }
610967dd82fSFlorian Fainelli 
611967dd82fSFlorian Fainelli static int b53_configure_vlan(struct b53_device *dev)
612967dd82fSFlorian Fainelli {
613a2482d2cSFlorian Fainelli 	struct b53_vlan vl = { 0 };
614967dd82fSFlorian Fainelli 	int i;
615967dd82fSFlorian Fainelli 
616967dd82fSFlorian Fainelli 	/* clear all vlan entries */
617967dd82fSFlorian Fainelli 	if (is5325(dev) || is5365(dev)) {
618967dd82fSFlorian Fainelli 		for (i = 1; i < dev->num_vlans; i++)
619a2482d2cSFlorian Fainelli 			b53_set_vlan_entry(dev, i, &vl);
620967dd82fSFlorian Fainelli 	} else {
621967dd82fSFlorian Fainelli 		b53_do_vlan_op(dev, VTA_CMD_CLEAR);
622967dd82fSFlorian Fainelli 	}
623967dd82fSFlorian Fainelli 
624967dd82fSFlorian Fainelli 	b53_enable_vlan(dev, false);
625967dd82fSFlorian Fainelli 
626967dd82fSFlorian Fainelli 	b53_for_each_port(dev, i)
627967dd82fSFlorian Fainelli 		b53_write16(dev, B53_VLAN_PAGE,
628967dd82fSFlorian Fainelli 			    B53_VLAN_PORT_DEF_TAG(i), 1);
629967dd82fSFlorian Fainelli 
630967dd82fSFlorian Fainelli 	if (!is5325(dev) && !is5365(dev))
631967dd82fSFlorian Fainelli 		b53_set_jumbo(dev, dev->enable_jumbo, false);
632967dd82fSFlorian Fainelli 
633967dd82fSFlorian Fainelli 	return 0;
634967dd82fSFlorian Fainelli }
635967dd82fSFlorian Fainelli 
636967dd82fSFlorian Fainelli static void b53_switch_reset_gpio(struct b53_device *dev)
637967dd82fSFlorian Fainelli {
638967dd82fSFlorian Fainelli 	int gpio = dev->reset_gpio;
639967dd82fSFlorian Fainelli 
640967dd82fSFlorian Fainelli 	if (gpio < 0)
641967dd82fSFlorian Fainelli 		return;
642967dd82fSFlorian Fainelli 
643967dd82fSFlorian Fainelli 	/* Reset sequence: RESET low(50ms)->high(20ms)
644967dd82fSFlorian Fainelli 	 */
645967dd82fSFlorian Fainelli 	gpio_set_value(gpio, 0);
646967dd82fSFlorian Fainelli 	mdelay(50);
647967dd82fSFlorian Fainelli 
648967dd82fSFlorian Fainelli 	gpio_set_value(gpio, 1);
649967dd82fSFlorian Fainelli 	mdelay(20);
650967dd82fSFlorian Fainelli 
651967dd82fSFlorian Fainelli 	dev->current_page = 0xff;
652967dd82fSFlorian Fainelli }
653967dd82fSFlorian Fainelli 
654967dd82fSFlorian Fainelli static int b53_switch_reset(struct b53_device *dev)
655967dd82fSFlorian Fainelli {
6563fb22b05SFlorian Fainelli 	unsigned int timeout = 1000;
6573fb22b05SFlorian Fainelli 	u8 mgmt, reg;
658967dd82fSFlorian Fainelli 
659967dd82fSFlorian Fainelli 	b53_switch_reset_gpio(dev);
660967dd82fSFlorian Fainelli 
661967dd82fSFlorian Fainelli 	if (is539x(dev)) {
662967dd82fSFlorian Fainelli 		b53_write8(dev, B53_CTRL_PAGE, B53_SOFTRESET, 0x83);
663967dd82fSFlorian Fainelli 		b53_write8(dev, B53_CTRL_PAGE, B53_SOFTRESET, 0x00);
664967dd82fSFlorian Fainelli 	}
665967dd82fSFlorian Fainelli 
6663fb22b05SFlorian Fainelli 	/* This is specific to 58xx devices here, do not use is58xx() which
6673fb22b05SFlorian Fainelli 	 * covers the larger Starfigther 2 family, including 7445/7278 which
6683fb22b05SFlorian Fainelli 	 * still use this driver as a library and need to perform the reset
6693fb22b05SFlorian Fainelli 	 * earlier.
6703fb22b05SFlorian Fainelli 	 */
6713fb22b05SFlorian Fainelli 	if (dev->chip_id == BCM58XX_DEVICE_ID) {
6723fb22b05SFlorian Fainelli 		b53_read8(dev, B53_CTRL_PAGE, B53_SOFTRESET, &reg);
6733fb22b05SFlorian Fainelli 		reg |= SW_RST | EN_SW_RST | EN_CH_RST;
6743fb22b05SFlorian Fainelli 		b53_write8(dev, B53_CTRL_PAGE, B53_SOFTRESET, reg);
6753fb22b05SFlorian Fainelli 
6763fb22b05SFlorian Fainelli 		do {
6773fb22b05SFlorian Fainelli 			b53_read8(dev, B53_CTRL_PAGE, B53_SOFTRESET, &reg);
6783fb22b05SFlorian Fainelli 			if (!(reg & SW_RST))
6793fb22b05SFlorian Fainelli 				break;
6803fb22b05SFlorian Fainelli 
6813fb22b05SFlorian Fainelli 			usleep_range(1000, 2000);
6823fb22b05SFlorian Fainelli 		} while (timeout-- > 0);
6833fb22b05SFlorian Fainelli 
6843fb22b05SFlorian Fainelli 		if (timeout == 0)
6853fb22b05SFlorian Fainelli 			return -ETIMEDOUT;
6863fb22b05SFlorian Fainelli 	}
6873fb22b05SFlorian Fainelli 
688967dd82fSFlorian Fainelli 	b53_read8(dev, B53_CTRL_PAGE, B53_SWITCH_MODE, &mgmt);
689967dd82fSFlorian Fainelli 
690967dd82fSFlorian Fainelli 	if (!(mgmt & SM_SW_FWD_EN)) {
691967dd82fSFlorian Fainelli 		mgmt &= ~SM_SW_FWD_MODE;
692967dd82fSFlorian Fainelli 		mgmt |= SM_SW_FWD_EN;
693967dd82fSFlorian Fainelli 
694967dd82fSFlorian Fainelli 		b53_write8(dev, B53_CTRL_PAGE, B53_SWITCH_MODE, mgmt);
695967dd82fSFlorian Fainelli 		b53_read8(dev, B53_CTRL_PAGE, B53_SWITCH_MODE, &mgmt);
696967dd82fSFlorian Fainelli 
697967dd82fSFlorian Fainelli 		if (!(mgmt & SM_SW_FWD_EN)) {
698967dd82fSFlorian Fainelli 			dev_err(dev->dev, "Failed to enable switch!\n");
699967dd82fSFlorian Fainelli 			return -EINVAL;
700967dd82fSFlorian Fainelli 		}
701967dd82fSFlorian Fainelli 	}
702967dd82fSFlorian Fainelli 
703967dd82fSFlorian Fainelli 	b53_enable_mib(dev);
704967dd82fSFlorian Fainelli 
705ff39c2d6SFlorian Fainelli 	return b53_flush_arl(dev, FAST_AGE_STATIC);
706967dd82fSFlorian Fainelli }
707967dd82fSFlorian Fainelli 
708967dd82fSFlorian Fainelli static int b53_phy_read16(struct dsa_switch *ds, int addr, int reg)
709967dd82fSFlorian Fainelli {
71004bed143SVivien Didelot 	struct b53_device *priv = ds->priv;
711967dd82fSFlorian Fainelli 	u16 value = 0;
712967dd82fSFlorian Fainelli 	int ret;
713967dd82fSFlorian Fainelli 
714967dd82fSFlorian Fainelli 	if (priv->ops->phy_read16)
715967dd82fSFlorian Fainelli 		ret = priv->ops->phy_read16(priv, addr, reg, &value);
716967dd82fSFlorian Fainelli 	else
717967dd82fSFlorian Fainelli 		ret = b53_read16(priv, B53_PORT_MII_PAGE(addr),
718967dd82fSFlorian Fainelli 				 reg * 2, &value);
719967dd82fSFlorian Fainelli 
720967dd82fSFlorian Fainelli 	return ret ? ret : value;
721967dd82fSFlorian Fainelli }
722967dd82fSFlorian Fainelli 
723967dd82fSFlorian Fainelli static int b53_phy_write16(struct dsa_switch *ds, int addr, int reg, u16 val)
724967dd82fSFlorian Fainelli {
72504bed143SVivien Didelot 	struct b53_device *priv = ds->priv;
726967dd82fSFlorian Fainelli 
727967dd82fSFlorian Fainelli 	if (priv->ops->phy_write16)
728967dd82fSFlorian Fainelli 		return priv->ops->phy_write16(priv, addr, reg, val);
729967dd82fSFlorian Fainelli 
730967dd82fSFlorian Fainelli 	return b53_write16(priv, B53_PORT_MII_PAGE(addr), reg * 2, val);
731967dd82fSFlorian Fainelli }
732967dd82fSFlorian Fainelli 
733967dd82fSFlorian Fainelli static int b53_reset_switch(struct b53_device *priv)
734967dd82fSFlorian Fainelli {
735967dd82fSFlorian Fainelli 	/* reset vlans */
736967dd82fSFlorian Fainelli 	priv->enable_jumbo = false;
737967dd82fSFlorian Fainelli 
738a2482d2cSFlorian Fainelli 	memset(priv->vlans, 0, sizeof(*priv->vlans) * priv->num_vlans);
739967dd82fSFlorian Fainelli 	memset(priv->ports, 0, sizeof(*priv->ports) * priv->num_ports);
740967dd82fSFlorian Fainelli 
741967dd82fSFlorian Fainelli 	return b53_switch_reset(priv);
742967dd82fSFlorian Fainelli }
743967dd82fSFlorian Fainelli 
744967dd82fSFlorian Fainelli static int b53_apply_config(struct b53_device *priv)
745967dd82fSFlorian Fainelli {
746967dd82fSFlorian Fainelli 	/* disable switching */
747967dd82fSFlorian Fainelli 	b53_set_forwarding(priv, 0);
748967dd82fSFlorian Fainelli 
749967dd82fSFlorian Fainelli 	b53_configure_vlan(priv);
750967dd82fSFlorian Fainelli 
751967dd82fSFlorian Fainelli 	/* enable switching */
752967dd82fSFlorian Fainelli 	b53_set_forwarding(priv, 1);
753967dd82fSFlorian Fainelli 
754967dd82fSFlorian Fainelli 	return 0;
755967dd82fSFlorian Fainelli }
756967dd82fSFlorian Fainelli 
757967dd82fSFlorian Fainelli static void b53_reset_mib(struct b53_device *priv)
758967dd82fSFlorian Fainelli {
759967dd82fSFlorian Fainelli 	u8 gc;
760967dd82fSFlorian Fainelli 
761967dd82fSFlorian Fainelli 	b53_read8(priv, B53_MGMT_PAGE, B53_GLOBAL_CONFIG, &gc);
762967dd82fSFlorian Fainelli 
763967dd82fSFlorian Fainelli 	b53_write8(priv, B53_MGMT_PAGE, B53_GLOBAL_CONFIG, gc | GC_RESET_MIB);
764967dd82fSFlorian Fainelli 	msleep(1);
765967dd82fSFlorian Fainelli 	b53_write8(priv, B53_MGMT_PAGE, B53_GLOBAL_CONFIG, gc & ~GC_RESET_MIB);
766967dd82fSFlorian Fainelli 	msleep(1);
767967dd82fSFlorian Fainelli }
768967dd82fSFlorian Fainelli 
769967dd82fSFlorian Fainelli static const struct b53_mib_desc *b53_get_mib(struct b53_device *dev)
770967dd82fSFlorian Fainelli {
771967dd82fSFlorian Fainelli 	if (is5365(dev))
772967dd82fSFlorian Fainelli 		return b53_mibs_65;
773967dd82fSFlorian Fainelli 	else if (is63xx(dev))
774967dd82fSFlorian Fainelli 		return b53_mibs_63xx;
775bde5d132SFlorian Fainelli 	else if (is58xx(dev))
776bde5d132SFlorian Fainelli 		return b53_mibs_58xx;
777967dd82fSFlorian Fainelli 	else
778967dd82fSFlorian Fainelli 		return b53_mibs;
779967dd82fSFlorian Fainelli }
780967dd82fSFlorian Fainelli 
781967dd82fSFlorian Fainelli static unsigned int b53_get_mib_size(struct b53_device *dev)
782967dd82fSFlorian Fainelli {
783967dd82fSFlorian Fainelli 	if (is5365(dev))
784967dd82fSFlorian Fainelli 		return B53_MIBS_65_SIZE;
785967dd82fSFlorian Fainelli 	else if (is63xx(dev))
786967dd82fSFlorian Fainelli 		return B53_MIBS_63XX_SIZE;
787bde5d132SFlorian Fainelli 	else if (is58xx(dev))
788bde5d132SFlorian Fainelli 		return B53_MIBS_58XX_SIZE;
789967dd82fSFlorian Fainelli 	else
790967dd82fSFlorian Fainelli 		return B53_MIBS_SIZE;
791967dd82fSFlorian Fainelli }
792967dd82fSFlorian Fainelli 
7933117455dSFlorian Fainelli void b53_get_strings(struct dsa_switch *ds, int port, uint8_t *data)
794967dd82fSFlorian Fainelli {
79504bed143SVivien Didelot 	struct b53_device *dev = ds->priv;
796967dd82fSFlorian Fainelli 	const struct b53_mib_desc *mibs = b53_get_mib(dev);
797967dd82fSFlorian Fainelli 	unsigned int mib_size = b53_get_mib_size(dev);
798967dd82fSFlorian Fainelli 	unsigned int i;
799967dd82fSFlorian Fainelli 
800967dd82fSFlorian Fainelli 	for (i = 0; i < mib_size; i++)
801967dd82fSFlorian Fainelli 		memcpy(data + i * ETH_GSTRING_LEN,
802967dd82fSFlorian Fainelli 		       mibs[i].name, ETH_GSTRING_LEN);
803967dd82fSFlorian Fainelli }
8043117455dSFlorian Fainelli EXPORT_SYMBOL(b53_get_strings);
805967dd82fSFlorian Fainelli 
8063117455dSFlorian Fainelli void b53_get_ethtool_stats(struct dsa_switch *ds, int port, uint64_t *data)
807967dd82fSFlorian Fainelli {
80804bed143SVivien Didelot 	struct b53_device *dev = ds->priv;
809967dd82fSFlorian Fainelli 	const struct b53_mib_desc *mibs = b53_get_mib(dev);
810967dd82fSFlorian Fainelli 	unsigned int mib_size = b53_get_mib_size(dev);
811967dd82fSFlorian Fainelli 	const struct b53_mib_desc *s;
812967dd82fSFlorian Fainelli 	unsigned int i;
813967dd82fSFlorian Fainelli 	u64 val = 0;
814967dd82fSFlorian Fainelli 
815967dd82fSFlorian Fainelli 	if (is5365(dev) && port == 5)
816967dd82fSFlorian Fainelli 		port = 8;
817967dd82fSFlorian Fainelli 
818967dd82fSFlorian Fainelli 	mutex_lock(&dev->stats_mutex);
819967dd82fSFlorian Fainelli 
820967dd82fSFlorian Fainelli 	for (i = 0; i < mib_size; i++) {
821967dd82fSFlorian Fainelli 		s = &mibs[i];
822967dd82fSFlorian Fainelli 
82351dca8a1SFlorian Fainelli 		if (s->size == 8) {
824967dd82fSFlorian Fainelli 			b53_read64(dev, B53_MIB_PAGE(port), s->offset, &val);
825967dd82fSFlorian Fainelli 		} else {
826967dd82fSFlorian Fainelli 			u32 val32;
827967dd82fSFlorian Fainelli 
828967dd82fSFlorian Fainelli 			b53_read32(dev, B53_MIB_PAGE(port), s->offset,
829967dd82fSFlorian Fainelli 				   &val32);
830967dd82fSFlorian Fainelli 			val = val32;
831967dd82fSFlorian Fainelli 		}
832967dd82fSFlorian Fainelli 		data[i] = (u64)val;
833967dd82fSFlorian Fainelli 	}
834967dd82fSFlorian Fainelli 
835967dd82fSFlorian Fainelli 	mutex_unlock(&dev->stats_mutex);
836967dd82fSFlorian Fainelli }
8373117455dSFlorian Fainelli EXPORT_SYMBOL(b53_get_ethtool_stats);
838967dd82fSFlorian Fainelli 
8393117455dSFlorian Fainelli int b53_get_sset_count(struct dsa_switch *ds)
840967dd82fSFlorian Fainelli {
84104bed143SVivien Didelot 	struct b53_device *dev = ds->priv;
842967dd82fSFlorian Fainelli 
843967dd82fSFlorian Fainelli 	return b53_get_mib_size(dev);
844967dd82fSFlorian Fainelli }
8453117455dSFlorian Fainelli EXPORT_SYMBOL(b53_get_sset_count);
846967dd82fSFlorian Fainelli 
847967dd82fSFlorian Fainelli static int b53_setup(struct dsa_switch *ds)
848967dd82fSFlorian Fainelli {
84904bed143SVivien Didelot 	struct b53_device *dev = ds->priv;
850967dd82fSFlorian Fainelli 	unsigned int port;
851967dd82fSFlorian Fainelli 	int ret;
852967dd82fSFlorian Fainelli 
853967dd82fSFlorian Fainelli 	ret = b53_reset_switch(dev);
854967dd82fSFlorian Fainelli 	if (ret) {
855967dd82fSFlorian Fainelli 		dev_err(ds->dev, "failed to reset switch\n");
856967dd82fSFlorian Fainelli 		return ret;
857967dd82fSFlorian Fainelli 	}
858967dd82fSFlorian Fainelli 
859967dd82fSFlorian Fainelli 	b53_reset_mib(dev);
860967dd82fSFlorian Fainelli 
861967dd82fSFlorian Fainelli 	ret = b53_apply_config(dev);
862967dd82fSFlorian Fainelli 	if (ret)
863967dd82fSFlorian Fainelli 		dev_err(ds->dev, "failed to apply configuration\n");
864967dd82fSFlorian Fainelli 
86534c8befdSFlorian Fainelli 	/* Configure IMP/CPU port, disable unused ports. Enabled
86634c8befdSFlorian Fainelli 	 * ports will be configured with .port_enable
86734c8befdSFlorian Fainelli 	 */
868967dd82fSFlorian Fainelli 	for (port = 0; port < dev->num_ports; port++) {
86934c8befdSFlorian Fainelli 		if (dsa_is_cpu_port(ds, port))
870299752a7SFlorian Fainelli 			b53_enable_cpu_port(dev, port);
87134c8befdSFlorian Fainelli 		else if (!(BIT(port) & ds->enabled_port_mask))
872967dd82fSFlorian Fainelli 			b53_disable_port(ds, port, NULL);
873967dd82fSFlorian Fainelli 	}
874967dd82fSFlorian Fainelli 
875967dd82fSFlorian Fainelli 	return ret;
876967dd82fSFlorian Fainelli }
877967dd82fSFlorian Fainelli 
878967dd82fSFlorian Fainelli static void b53_adjust_link(struct dsa_switch *ds, int port,
879967dd82fSFlorian Fainelli 			    struct phy_device *phydev)
880967dd82fSFlorian Fainelli {
88104bed143SVivien Didelot 	struct b53_device *dev = ds->priv;
882967dd82fSFlorian Fainelli 	u8 rgmii_ctrl = 0, reg = 0, off;
883967dd82fSFlorian Fainelli 
884967dd82fSFlorian Fainelli 	if (!phy_is_pseudo_fixed_link(phydev))
885967dd82fSFlorian Fainelli 		return;
886967dd82fSFlorian Fainelli 
887967dd82fSFlorian Fainelli 	/* Override the port settings */
888967dd82fSFlorian Fainelli 	if (port == dev->cpu_port) {
889967dd82fSFlorian Fainelli 		off = B53_PORT_OVERRIDE_CTRL;
890967dd82fSFlorian Fainelli 		reg = PORT_OVERRIDE_EN;
891967dd82fSFlorian Fainelli 	} else {
892967dd82fSFlorian Fainelli 		off = B53_GMII_PORT_OVERRIDE_CTRL(port);
893967dd82fSFlorian Fainelli 		reg = GMII_PO_EN;
894967dd82fSFlorian Fainelli 	}
895967dd82fSFlorian Fainelli 
896967dd82fSFlorian Fainelli 	/* Set the link UP */
897967dd82fSFlorian Fainelli 	if (phydev->link)
898967dd82fSFlorian Fainelli 		reg |= PORT_OVERRIDE_LINK;
899967dd82fSFlorian Fainelli 
900967dd82fSFlorian Fainelli 	if (phydev->duplex == DUPLEX_FULL)
901967dd82fSFlorian Fainelli 		reg |= PORT_OVERRIDE_FULL_DUPLEX;
902967dd82fSFlorian Fainelli 
903967dd82fSFlorian Fainelli 	switch (phydev->speed) {
904967dd82fSFlorian Fainelli 	case 2000:
905967dd82fSFlorian Fainelli 		reg |= PORT_OVERRIDE_SPEED_2000M;
906967dd82fSFlorian Fainelli 		/* fallthrough */
907967dd82fSFlorian Fainelli 	case SPEED_1000:
908967dd82fSFlorian Fainelli 		reg |= PORT_OVERRIDE_SPEED_1000M;
909967dd82fSFlorian Fainelli 		break;
910967dd82fSFlorian Fainelli 	case SPEED_100:
911967dd82fSFlorian Fainelli 		reg |= PORT_OVERRIDE_SPEED_100M;
912967dd82fSFlorian Fainelli 		break;
913967dd82fSFlorian Fainelli 	case SPEED_10:
914967dd82fSFlorian Fainelli 		reg |= PORT_OVERRIDE_SPEED_10M;
915967dd82fSFlorian Fainelli 		break;
916967dd82fSFlorian Fainelli 	default:
917967dd82fSFlorian Fainelli 		dev_err(ds->dev, "unknown speed: %d\n", phydev->speed);
918967dd82fSFlorian Fainelli 		return;
919967dd82fSFlorian Fainelli 	}
920967dd82fSFlorian Fainelli 
921967dd82fSFlorian Fainelli 	/* Enable flow control on BCM5301x's CPU port */
922967dd82fSFlorian Fainelli 	if (is5301x(dev) && port == dev->cpu_port)
923967dd82fSFlorian Fainelli 		reg |= PORT_OVERRIDE_RX_FLOW | PORT_OVERRIDE_TX_FLOW;
924967dd82fSFlorian Fainelli 
925967dd82fSFlorian Fainelli 	if (phydev->pause) {
926967dd82fSFlorian Fainelli 		if (phydev->asym_pause)
927967dd82fSFlorian Fainelli 			reg |= PORT_OVERRIDE_TX_FLOW;
928967dd82fSFlorian Fainelli 		reg |= PORT_OVERRIDE_RX_FLOW;
929967dd82fSFlorian Fainelli 	}
930967dd82fSFlorian Fainelli 
931967dd82fSFlorian Fainelli 	b53_write8(dev, B53_CTRL_PAGE, off, reg);
932967dd82fSFlorian Fainelli 
933967dd82fSFlorian Fainelli 	if (is531x5(dev) && phy_interface_is_rgmii(phydev)) {
934967dd82fSFlorian Fainelli 		if (port == 8)
935967dd82fSFlorian Fainelli 			off = B53_RGMII_CTRL_IMP;
936967dd82fSFlorian Fainelli 		else
937967dd82fSFlorian Fainelli 			off = B53_RGMII_CTRL_P(port);
938967dd82fSFlorian Fainelli 
939967dd82fSFlorian Fainelli 		/* Configure the port RGMII clock delay by DLL disabled and
940967dd82fSFlorian Fainelli 		 * tx_clk aligned timing (restoring to reset defaults)
941967dd82fSFlorian Fainelli 		 */
942967dd82fSFlorian Fainelli 		b53_read8(dev, B53_CTRL_PAGE, off, &rgmii_ctrl);
943967dd82fSFlorian Fainelli 		rgmii_ctrl &= ~(RGMII_CTRL_DLL_RXC | RGMII_CTRL_DLL_TXC |
944967dd82fSFlorian Fainelli 				RGMII_CTRL_TIMING_SEL);
945967dd82fSFlorian Fainelli 
946967dd82fSFlorian Fainelli 		/* PHY_INTERFACE_MODE_RGMII_TXID means TX internal delay, make
947967dd82fSFlorian Fainelli 		 * sure that we enable the port TX clock internal delay to
948967dd82fSFlorian Fainelli 		 * account for this internal delay that is inserted, otherwise
949967dd82fSFlorian Fainelli 		 * the switch won't be able to receive correctly.
950967dd82fSFlorian Fainelli 		 *
951967dd82fSFlorian Fainelli 		 * PHY_INTERFACE_MODE_RGMII means that we are not introducing
952967dd82fSFlorian Fainelli 		 * any delay neither on transmission nor reception, so the
953967dd82fSFlorian Fainelli 		 * BCM53125 must also be configured accordingly to account for
954967dd82fSFlorian Fainelli 		 * the lack of delay and introduce
955967dd82fSFlorian Fainelli 		 *
956967dd82fSFlorian Fainelli 		 * The BCM53125 switch has its RX clock and TX clock control
957967dd82fSFlorian Fainelli 		 * swapped, hence the reason why we modify the TX clock path in
958967dd82fSFlorian Fainelli 		 * the "RGMII" case
959967dd82fSFlorian Fainelli 		 */
960967dd82fSFlorian Fainelli 		if (phydev->interface == PHY_INTERFACE_MODE_RGMII_TXID)
961967dd82fSFlorian Fainelli 			rgmii_ctrl |= RGMII_CTRL_DLL_TXC;
962967dd82fSFlorian Fainelli 		if (phydev->interface == PHY_INTERFACE_MODE_RGMII)
963967dd82fSFlorian Fainelli 			rgmii_ctrl |= RGMII_CTRL_DLL_TXC | RGMII_CTRL_DLL_RXC;
964967dd82fSFlorian Fainelli 		rgmii_ctrl |= RGMII_CTRL_TIMING_SEL;
965967dd82fSFlorian Fainelli 		b53_write8(dev, B53_CTRL_PAGE, off, rgmii_ctrl);
966967dd82fSFlorian Fainelli 
967967dd82fSFlorian Fainelli 		dev_info(ds->dev, "Configured port %d for %s\n", port,
968967dd82fSFlorian Fainelli 			 phy_modes(phydev->interface));
969967dd82fSFlorian Fainelli 	}
970967dd82fSFlorian Fainelli 
971967dd82fSFlorian Fainelli 	/* configure MII port if necessary */
972967dd82fSFlorian Fainelli 	if (is5325(dev)) {
973967dd82fSFlorian Fainelli 		b53_read8(dev, B53_CTRL_PAGE, B53_PORT_OVERRIDE_CTRL,
974967dd82fSFlorian Fainelli 			  &reg);
975967dd82fSFlorian Fainelli 
976967dd82fSFlorian Fainelli 		/* reverse mii needs to be enabled */
977967dd82fSFlorian Fainelli 		if (!(reg & PORT_OVERRIDE_RV_MII_25)) {
978967dd82fSFlorian Fainelli 			b53_write8(dev, B53_CTRL_PAGE, B53_PORT_OVERRIDE_CTRL,
979967dd82fSFlorian Fainelli 				   reg | PORT_OVERRIDE_RV_MII_25);
980967dd82fSFlorian Fainelli 			b53_read8(dev, B53_CTRL_PAGE, B53_PORT_OVERRIDE_CTRL,
981967dd82fSFlorian Fainelli 				  &reg);
982967dd82fSFlorian Fainelli 
983967dd82fSFlorian Fainelli 			if (!(reg & PORT_OVERRIDE_RV_MII_25)) {
984967dd82fSFlorian Fainelli 				dev_err(ds->dev,
985967dd82fSFlorian Fainelli 					"Failed to enable reverse MII mode\n");
986967dd82fSFlorian Fainelli 				return;
987967dd82fSFlorian Fainelli 			}
988967dd82fSFlorian Fainelli 		}
989967dd82fSFlorian Fainelli 	} else if (is5301x(dev)) {
990967dd82fSFlorian Fainelli 		if (port != dev->cpu_port) {
991967dd82fSFlorian Fainelli 			u8 po_reg = B53_GMII_PORT_OVERRIDE_CTRL(dev->cpu_port);
992967dd82fSFlorian Fainelli 			u8 gmii_po;
993967dd82fSFlorian Fainelli 
994967dd82fSFlorian Fainelli 			b53_read8(dev, B53_CTRL_PAGE, po_reg, &gmii_po);
995967dd82fSFlorian Fainelli 			gmii_po |= GMII_PO_LINK |
996967dd82fSFlorian Fainelli 				   GMII_PO_RX_FLOW |
997967dd82fSFlorian Fainelli 				   GMII_PO_TX_FLOW |
998967dd82fSFlorian Fainelli 				   GMII_PO_EN |
999967dd82fSFlorian Fainelli 				   GMII_PO_SPEED_2000M;
1000967dd82fSFlorian Fainelli 			b53_write8(dev, B53_CTRL_PAGE, po_reg, gmii_po);
1001967dd82fSFlorian Fainelli 		}
1002967dd82fSFlorian Fainelli 	}
1003967dd82fSFlorian Fainelli }
1004967dd82fSFlorian Fainelli 
10053117455dSFlorian Fainelli int b53_vlan_filtering(struct dsa_switch *ds, int port, bool vlan_filtering)
1006a2482d2cSFlorian Fainelli {
1007a2482d2cSFlorian Fainelli 	return 0;
1008a2482d2cSFlorian Fainelli }
10093117455dSFlorian Fainelli EXPORT_SYMBOL(b53_vlan_filtering);
1010a2482d2cSFlorian Fainelli 
10113117455dSFlorian Fainelli int b53_vlan_prepare(struct dsa_switch *ds, int port,
1012a2482d2cSFlorian Fainelli 		     const struct switchdev_obj_port_vlan *vlan,
1013a2482d2cSFlorian Fainelli 		     struct switchdev_trans *trans)
1014a2482d2cSFlorian Fainelli {
101504bed143SVivien Didelot 	struct b53_device *dev = ds->priv;
1016a2482d2cSFlorian Fainelli 
1017a2482d2cSFlorian Fainelli 	if ((is5325(dev) || is5365(dev)) && vlan->vid_begin == 0)
1018a2482d2cSFlorian Fainelli 		return -EOPNOTSUPP;
1019a2482d2cSFlorian Fainelli 
1020a2482d2cSFlorian Fainelli 	if (vlan->vid_end > dev->num_vlans)
1021a2482d2cSFlorian Fainelli 		return -ERANGE;
1022a2482d2cSFlorian Fainelli 
1023a2482d2cSFlorian Fainelli 	b53_enable_vlan(dev, true);
1024a2482d2cSFlorian Fainelli 
1025a2482d2cSFlorian Fainelli 	return 0;
1026a2482d2cSFlorian Fainelli }
10273117455dSFlorian Fainelli EXPORT_SYMBOL(b53_vlan_prepare);
1028a2482d2cSFlorian Fainelli 
10293117455dSFlorian Fainelli void b53_vlan_add(struct dsa_switch *ds, int port,
1030a2482d2cSFlorian Fainelli 		  const struct switchdev_obj_port_vlan *vlan,
1031a2482d2cSFlorian Fainelli 		  struct switchdev_trans *trans)
1032a2482d2cSFlorian Fainelli {
103304bed143SVivien Didelot 	struct b53_device *dev = ds->priv;
1034a2482d2cSFlorian Fainelli 	bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
1035a2482d2cSFlorian Fainelli 	bool pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
1036a2482d2cSFlorian Fainelli 	unsigned int cpu_port = dev->cpu_port;
1037a2482d2cSFlorian Fainelli 	struct b53_vlan *vl;
1038a2482d2cSFlorian Fainelli 	u16 vid;
1039a2482d2cSFlorian Fainelli 
1040a2482d2cSFlorian Fainelli 	for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid) {
1041a2482d2cSFlorian Fainelli 		vl = &dev->vlans[vid];
1042a2482d2cSFlorian Fainelli 
1043a2482d2cSFlorian Fainelli 		b53_get_vlan_entry(dev, vid, vl);
1044a2482d2cSFlorian Fainelli 
1045a2482d2cSFlorian Fainelli 		vl->members |= BIT(port) | BIT(cpu_port);
1046a2482d2cSFlorian Fainelli 		if (untagged)
1047e47112d9SFlorian Fainelli 			vl->untag |= BIT(port);
1048a2482d2cSFlorian Fainelli 		else
1049e47112d9SFlorian Fainelli 			vl->untag &= ~BIT(port);
1050e47112d9SFlorian Fainelli 		vl->untag &= ~BIT(cpu_port);
1051a2482d2cSFlorian Fainelli 
1052a2482d2cSFlorian Fainelli 		b53_set_vlan_entry(dev, vid, vl);
1053a2482d2cSFlorian Fainelli 		b53_fast_age_vlan(dev, vid);
1054a2482d2cSFlorian Fainelli 	}
1055a2482d2cSFlorian Fainelli 
1056a2482d2cSFlorian Fainelli 	if (pvid) {
1057a2482d2cSFlorian Fainelli 		b53_write16(dev, B53_VLAN_PAGE, B53_VLAN_PORT_DEF_TAG(port),
1058a2482d2cSFlorian Fainelli 			    vlan->vid_end);
1059a2482d2cSFlorian Fainelli 		b53_fast_age_vlan(dev, vid);
1060a2482d2cSFlorian Fainelli 	}
1061a2482d2cSFlorian Fainelli }
10623117455dSFlorian Fainelli EXPORT_SYMBOL(b53_vlan_add);
1063a2482d2cSFlorian Fainelli 
10643117455dSFlorian Fainelli int b53_vlan_del(struct dsa_switch *ds, int port,
1065a2482d2cSFlorian Fainelli 		 const struct switchdev_obj_port_vlan *vlan)
1066a2482d2cSFlorian Fainelli {
106704bed143SVivien Didelot 	struct b53_device *dev = ds->priv;
1068a2482d2cSFlorian Fainelli 	bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
1069a2482d2cSFlorian Fainelli 	struct b53_vlan *vl;
1070a2482d2cSFlorian Fainelli 	u16 vid;
1071a2482d2cSFlorian Fainelli 	u16 pvid;
1072a2482d2cSFlorian Fainelli 
1073a2482d2cSFlorian Fainelli 	b53_read16(dev, B53_VLAN_PAGE, B53_VLAN_PORT_DEF_TAG(port), &pvid);
1074a2482d2cSFlorian Fainelli 
1075a2482d2cSFlorian Fainelli 	for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid) {
1076a2482d2cSFlorian Fainelli 		vl = &dev->vlans[vid];
1077a2482d2cSFlorian Fainelli 
1078a2482d2cSFlorian Fainelli 		b53_get_vlan_entry(dev, vid, vl);
1079a2482d2cSFlorian Fainelli 
1080a2482d2cSFlorian Fainelli 		vl->members &= ~BIT(port);
1081a2482d2cSFlorian Fainelli 
1082a2482d2cSFlorian Fainelli 		if (pvid == vid) {
1083a2482d2cSFlorian Fainelli 			if (is5325(dev) || is5365(dev))
1084a2482d2cSFlorian Fainelli 				pvid = 1;
1085a2482d2cSFlorian Fainelli 			else
1086a2482d2cSFlorian Fainelli 				pvid = 0;
1087a2482d2cSFlorian Fainelli 		}
1088a2482d2cSFlorian Fainelli 
1089e47112d9SFlorian Fainelli 		if (untagged)
1090a2482d2cSFlorian Fainelli 			vl->untag &= ~(BIT(port));
1091a2482d2cSFlorian Fainelli 
1092a2482d2cSFlorian Fainelli 		b53_set_vlan_entry(dev, vid, vl);
1093a2482d2cSFlorian Fainelli 		b53_fast_age_vlan(dev, vid);
1094a2482d2cSFlorian Fainelli 	}
1095a2482d2cSFlorian Fainelli 
1096a2482d2cSFlorian Fainelli 	b53_write16(dev, B53_VLAN_PAGE, B53_VLAN_PORT_DEF_TAG(port), pvid);
1097a2482d2cSFlorian Fainelli 	b53_fast_age_vlan(dev, pvid);
1098a2482d2cSFlorian Fainelli 
1099a2482d2cSFlorian Fainelli 	return 0;
1100a2482d2cSFlorian Fainelli }
11013117455dSFlorian Fainelli EXPORT_SYMBOL(b53_vlan_del);
1102a2482d2cSFlorian Fainelli 
11031da6df85SFlorian Fainelli /* Address Resolution Logic routines */
11041da6df85SFlorian Fainelli static int b53_arl_op_wait(struct b53_device *dev)
11051da6df85SFlorian Fainelli {
11061da6df85SFlorian Fainelli 	unsigned int timeout = 10;
11071da6df85SFlorian Fainelli 	u8 reg;
11081da6df85SFlorian Fainelli 
11091da6df85SFlorian Fainelli 	do {
11101da6df85SFlorian Fainelli 		b53_read8(dev, B53_ARLIO_PAGE, B53_ARLTBL_RW_CTRL, &reg);
11111da6df85SFlorian Fainelli 		if (!(reg & ARLTBL_START_DONE))
11121da6df85SFlorian Fainelli 			return 0;
11131da6df85SFlorian Fainelli 
11141da6df85SFlorian Fainelli 		usleep_range(1000, 2000);
11151da6df85SFlorian Fainelli 	} while (timeout--);
11161da6df85SFlorian Fainelli 
11171da6df85SFlorian Fainelli 	dev_warn(dev->dev, "timeout waiting for ARL to finish: 0x%02x\n", reg);
11181da6df85SFlorian Fainelli 
11191da6df85SFlorian Fainelli 	return -ETIMEDOUT;
11201da6df85SFlorian Fainelli }
11211da6df85SFlorian Fainelli 
11221da6df85SFlorian Fainelli static int b53_arl_rw_op(struct b53_device *dev, unsigned int op)
11231da6df85SFlorian Fainelli {
11241da6df85SFlorian Fainelli 	u8 reg;
11251da6df85SFlorian Fainelli 
11261da6df85SFlorian Fainelli 	if (op > ARLTBL_RW)
11271da6df85SFlorian Fainelli 		return -EINVAL;
11281da6df85SFlorian Fainelli 
11291da6df85SFlorian Fainelli 	b53_read8(dev, B53_ARLIO_PAGE, B53_ARLTBL_RW_CTRL, &reg);
11301da6df85SFlorian Fainelli 	reg |= ARLTBL_START_DONE;
11311da6df85SFlorian Fainelli 	if (op)
11321da6df85SFlorian Fainelli 		reg |= ARLTBL_RW;
11331da6df85SFlorian Fainelli 	else
11341da6df85SFlorian Fainelli 		reg &= ~ARLTBL_RW;
11351da6df85SFlorian Fainelli 	b53_write8(dev, B53_ARLIO_PAGE, B53_ARLTBL_RW_CTRL, reg);
11361da6df85SFlorian Fainelli 
11371da6df85SFlorian Fainelli 	return b53_arl_op_wait(dev);
11381da6df85SFlorian Fainelli }
11391da6df85SFlorian Fainelli 
11401da6df85SFlorian Fainelli static int b53_arl_read(struct b53_device *dev, u64 mac,
11411da6df85SFlorian Fainelli 			u16 vid, struct b53_arl_entry *ent, u8 *idx,
11421da6df85SFlorian Fainelli 			bool is_valid)
11431da6df85SFlorian Fainelli {
11441da6df85SFlorian Fainelli 	unsigned int i;
11451da6df85SFlorian Fainelli 	int ret;
11461da6df85SFlorian Fainelli 
11471da6df85SFlorian Fainelli 	ret = b53_arl_op_wait(dev);
11481da6df85SFlorian Fainelli 	if (ret)
11491da6df85SFlorian Fainelli 		return ret;
11501da6df85SFlorian Fainelli 
11511da6df85SFlorian Fainelli 	/* Read the bins */
11521da6df85SFlorian Fainelli 	for (i = 0; i < dev->num_arl_entries; i++) {
11531da6df85SFlorian Fainelli 		u64 mac_vid;
11541da6df85SFlorian Fainelli 		u32 fwd_entry;
11551da6df85SFlorian Fainelli 
11561da6df85SFlorian Fainelli 		b53_read64(dev, B53_ARLIO_PAGE,
11571da6df85SFlorian Fainelli 			   B53_ARLTBL_MAC_VID_ENTRY(i), &mac_vid);
11581da6df85SFlorian Fainelli 		b53_read32(dev, B53_ARLIO_PAGE,
11591da6df85SFlorian Fainelli 			   B53_ARLTBL_DATA_ENTRY(i), &fwd_entry);
11601da6df85SFlorian Fainelli 		b53_arl_to_entry(ent, mac_vid, fwd_entry);
11611da6df85SFlorian Fainelli 
11621da6df85SFlorian Fainelli 		if (!(fwd_entry & ARLTBL_VALID))
11631da6df85SFlorian Fainelli 			continue;
11641da6df85SFlorian Fainelli 		if ((mac_vid & ARLTBL_MAC_MASK) != mac)
11651da6df85SFlorian Fainelli 			continue;
11661da6df85SFlorian Fainelli 		*idx = i;
11671da6df85SFlorian Fainelli 	}
11681da6df85SFlorian Fainelli 
11691da6df85SFlorian Fainelli 	return -ENOENT;
11701da6df85SFlorian Fainelli }
11711da6df85SFlorian Fainelli 
11721da6df85SFlorian Fainelli static int b53_arl_op(struct b53_device *dev, int op, int port,
11731da6df85SFlorian Fainelli 		      const unsigned char *addr, u16 vid, bool is_valid)
11741da6df85SFlorian Fainelli {
11751da6df85SFlorian Fainelli 	struct b53_arl_entry ent;
11761da6df85SFlorian Fainelli 	u32 fwd_entry;
11771da6df85SFlorian Fainelli 	u64 mac, mac_vid = 0;
11781da6df85SFlorian Fainelli 	u8 idx = 0;
11791da6df85SFlorian Fainelli 	int ret;
11801da6df85SFlorian Fainelli 
11811da6df85SFlorian Fainelli 	/* Convert the array into a 64-bit MAC */
11824b92ea81SFlorian Fainelli 	mac = ether_addr_to_u64(addr);
11831da6df85SFlorian Fainelli 
11841da6df85SFlorian Fainelli 	/* Perform a read for the given MAC and VID */
11851da6df85SFlorian Fainelli 	b53_write48(dev, B53_ARLIO_PAGE, B53_MAC_ADDR_IDX, mac);
11861da6df85SFlorian Fainelli 	b53_write16(dev, B53_ARLIO_PAGE, B53_VLAN_ID_IDX, vid);
11871da6df85SFlorian Fainelli 
11881da6df85SFlorian Fainelli 	/* Issue a read operation for this MAC */
11891da6df85SFlorian Fainelli 	ret = b53_arl_rw_op(dev, 1);
11901da6df85SFlorian Fainelli 	if (ret)
11911da6df85SFlorian Fainelli 		return ret;
11921da6df85SFlorian Fainelli 
11931da6df85SFlorian Fainelli 	ret = b53_arl_read(dev, mac, vid, &ent, &idx, is_valid);
11941da6df85SFlorian Fainelli 	/* If this is a read, just finish now */
11951da6df85SFlorian Fainelli 	if (op)
11961da6df85SFlorian Fainelli 		return ret;
11971da6df85SFlorian Fainelli 
11981da6df85SFlorian Fainelli 	/* We could not find a matching MAC, so reset to a new entry */
11991da6df85SFlorian Fainelli 	if (ret) {
12001da6df85SFlorian Fainelli 		fwd_entry = 0;
12011da6df85SFlorian Fainelli 		idx = 1;
12021da6df85SFlorian Fainelli 	}
12031da6df85SFlorian Fainelli 
12041da6df85SFlorian Fainelli 	memset(&ent, 0, sizeof(ent));
12051da6df85SFlorian Fainelli 	ent.port = port;
12061da6df85SFlorian Fainelli 	ent.is_valid = is_valid;
12071da6df85SFlorian Fainelli 	ent.vid = vid;
12081da6df85SFlorian Fainelli 	ent.is_static = true;
12091da6df85SFlorian Fainelli 	memcpy(ent.mac, addr, ETH_ALEN);
12101da6df85SFlorian Fainelli 	b53_arl_from_entry(&mac_vid, &fwd_entry, &ent);
12111da6df85SFlorian Fainelli 
12121da6df85SFlorian Fainelli 	b53_write64(dev, B53_ARLIO_PAGE,
12131da6df85SFlorian Fainelli 		    B53_ARLTBL_MAC_VID_ENTRY(idx), mac_vid);
12141da6df85SFlorian Fainelli 	b53_write32(dev, B53_ARLIO_PAGE,
12151da6df85SFlorian Fainelli 		    B53_ARLTBL_DATA_ENTRY(idx), fwd_entry);
12161da6df85SFlorian Fainelli 
12171da6df85SFlorian Fainelli 	return b53_arl_rw_op(dev, 0);
12181da6df85SFlorian Fainelli }
12191da6df85SFlorian Fainelli 
12201b6dd556SArkadi Sharshevsky int b53_fdb_add(struct dsa_switch *ds, int port,
12216c2c1dcbSArkadi Sharshevsky 		const unsigned char *addr, u16 vid)
12221da6df85SFlorian Fainelli {
122304bed143SVivien Didelot 	struct b53_device *priv = ds->priv;
12241da6df85SFlorian Fainelli 
12251da6df85SFlorian Fainelli 	/* 5325 and 5365 require some more massaging, but could
12261da6df85SFlorian Fainelli 	 * be supported eventually
12271da6df85SFlorian Fainelli 	 */
12281da6df85SFlorian Fainelli 	if (is5325(priv) || is5365(priv))
12291da6df85SFlorian Fainelli 		return -EOPNOTSUPP;
12301da6df85SFlorian Fainelli 
12311b6dd556SArkadi Sharshevsky 	return b53_arl_op(priv, 0, port, addr, vid, true);
12321da6df85SFlorian Fainelli }
12333117455dSFlorian Fainelli EXPORT_SYMBOL(b53_fdb_add);
12341da6df85SFlorian Fainelli 
12353117455dSFlorian Fainelli int b53_fdb_del(struct dsa_switch *ds, int port,
12366c2c1dcbSArkadi Sharshevsky 		const unsigned char *addr, u16 vid)
12371da6df85SFlorian Fainelli {
123804bed143SVivien Didelot 	struct b53_device *priv = ds->priv;
12391da6df85SFlorian Fainelli 
12406c2c1dcbSArkadi Sharshevsky 	return b53_arl_op(priv, 0, port, addr, vid, false);
12411da6df85SFlorian Fainelli }
12423117455dSFlorian Fainelli EXPORT_SYMBOL(b53_fdb_del);
12431da6df85SFlorian Fainelli 
12441da6df85SFlorian Fainelli static int b53_arl_search_wait(struct b53_device *dev)
12451da6df85SFlorian Fainelli {
12461da6df85SFlorian Fainelli 	unsigned int timeout = 1000;
12471da6df85SFlorian Fainelli 	u8 reg;
12481da6df85SFlorian Fainelli 
12491da6df85SFlorian Fainelli 	do {
12501da6df85SFlorian Fainelli 		b53_read8(dev, B53_ARLIO_PAGE, B53_ARL_SRCH_CTL, &reg);
12511da6df85SFlorian Fainelli 		if (!(reg & ARL_SRCH_STDN))
12521da6df85SFlorian Fainelli 			return 0;
12531da6df85SFlorian Fainelli 
12541da6df85SFlorian Fainelli 		if (reg & ARL_SRCH_VLID)
12551da6df85SFlorian Fainelli 			return 0;
12561da6df85SFlorian Fainelli 
12571da6df85SFlorian Fainelli 		usleep_range(1000, 2000);
12581da6df85SFlorian Fainelli 	} while (timeout--);
12591da6df85SFlorian Fainelli 
12601da6df85SFlorian Fainelli 	return -ETIMEDOUT;
12611da6df85SFlorian Fainelli }
12621da6df85SFlorian Fainelli 
12631da6df85SFlorian Fainelli static void b53_arl_search_rd(struct b53_device *dev, u8 idx,
12641da6df85SFlorian Fainelli 			      struct b53_arl_entry *ent)
12651da6df85SFlorian Fainelli {
12661da6df85SFlorian Fainelli 	u64 mac_vid;
12671da6df85SFlorian Fainelli 	u32 fwd_entry;
12681da6df85SFlorian Fainelli 
12691da6df85SFlorian Fainelli 	b53_read64(dev, B53_ARLIO_PAGE,
12701da6df85SFlorian Fainelli 		   B53_ARL_SRCH_RSTL_MACVID(idx), &mac_vid);
12711da6df85SFlorian Fainelli 	b53_read32(dev, B53_ARLIO_PAGE,
12721da6df85SFlorian Fainelli 		   B53_ARL_SRCH_RSTL(idx), &fwd_entry);
12731da6df85SFlorian Fainelli 	b53_arl_to_entry(ent, mac_vid, fwd_entry);
12741da6df85SFlorian Fainelli }
12751da6df85SFlorian Fainelli 
1276e6cbef0cSVivien Didelot static int b53_fdb_copy(int port, const struct b53_arl_entry *ent,
12772bedde1aSArkadi Sharshevsky 			dsa_fdb_dump_cb_t *cb, void *data)
12781da6df85SFlorian Fainelli {
12791da6df85SFlorian Fainelli 	if (!ent->is_valid)
12801da6df85SFlorian Fainelli 		return 0;
12811da6df85SFlorian Fainelli 
12821da6df85SFlorian Fainelli 	if (port != ent->port)
12831da6df85SFlorian Fainelli 		return 0;
12841da6df85SFlorian Fainelli 
12852bedde1aSArkadi Sharshevsky 	return cb(ent->mac, ent->vid, ent->is_static, data);
12861da6df85SFlorian Fainelli }
12871da6df85SFlorian Fainelli 
12883117455dSFlorian Fainelli int b53_fdb_dump(struct dsa_switch *ds, int port,
12892bedde1aSArkadi Sharshevsky 		 dsa_fdb_dump_cb_t *cb, void *data)
12901da6df85SFlorian Fainelli {
129104bed143SVivien Didelot 	struct b53_device *priv = ds->priv;
12921da6df85SFlorian Fainelli 	struct b53_arl_entry results[2];
12931da6df85SFlorian Fainelli 	unsigned int count = 0;
12941da6df85SFlorian Fainelli 	int ret;
12951da6df85SFlorian Fainelli 	u8 reg;
12961da6df85SFlorian Fainelli 
12971da6df85SFlorian Fainelli 	/* Start search operation */
12981da6df85SFlorian Fainelli 	reg = ARL_SRCH_STDN;
12991da6df85SFlorian Fainelli 	b53_write8(priv, B53_ARLIO_PAGE, B53_ARL_SRCH_CTL, reg);
13001da6df85SFlorian Fainelli 
13011da6df85SFlorian Fainelli 	do {
13021da6df85SFlorian Fainelli 		ret = b53_arl_search_wait(priv);
13031da6df85SFlorian Fainelli 		if (ret)
13041da6df85SFlorian Fainelli 			return ret;
13051da6df85SFlorian Fainelli 
13061da6df85SFlorian Fainelli 		b53_arl_search_rd(priv, 0, &results[0]);
13072bedde1aSArkadi Sharshevsky 		ret = b53_fdb_copy(port, &results[0], cb, data);
13081da6df85SFlorian Fainelli 		if (ret)
13091da6df85SFlorian Fainelli 			return ret;
13101da6df85SFlorian Fainelli 
13111da6df85SFlorian Fainelli 		if (priv->num_arl_entries > 2) {
13121da6df85SFlorian Fainelli 			b53_arl_search_rd(priv, 1, &results[1]);
13132bedde1aSArkadi Sharshevsky 			ret = b53_fdb_copy(port, &results[1], cb, data);
13141da6df85SFlorian Fainelli 			if (ret)
13151da6df85SFlorian Fainelli 				return ret;
13161da6df85SFlorian Fainelli 
13171da6df85SFlorian Fainelli 			if (!results[0].is_valid && !results[1].is_valid)
13181da6df85SFlorian Fainelli 				break;
13191da6df85SFlorian Fainelli 		}
13201da6df85SFlorian Fainelli 
13211da6df85SFlorian Fainelli 	} while (count++ < 1024);
13221da6df85SFlorian Fainelli 
13231da6df85SFlorian Fainelli 	return 0;
13241da6df85SFlorian Fainelli }
13253117455dSFlorian Fainelli EXPORT_SYMBOL(b53_fdb_dump);
13261da6df85SFlorian Fainelli 
1327ddd3a0c8SVivien Didelot int b53_br_join(struct dsa_switch *ds, int port, struct net_device *br)
1328ff39c2d6SFlorian Fainelli {
132904bed143SVivien Didelot 	struct b53_device *dev = ds->priv;
13308b0d3ea5SVivien Didelot 	s8 cpu_port = ds->dst->cpu_dp->index;
1331ff39c2d6SFlorian Fainelli 	u16 pvlan, reg;
1332ff39c2d6SFlorian Fainelli 	unsigned int i;
1333ff39c2d6SFlorian Fainelli 
133448aea33aSFlorian Fainelli 	/* Make this port leave the all VLANs join since we will have proper
133548aea33aSFlorian Fainelli 	 * VLAN entries from now on
133648aea33aSFlorian Fainelli 	 */
133748aea33aSFlorian Fainelli 	if (is58xx(dev)) {
133848aea33aSFlorian Fainelli 		b53_read16(dev, B53_VLAN_PAGE, B53_JOIN_ALL_VLAN_EN, &reg);
133948aea33aSFlorian Fainelli 		reg &= ~BIT(port);
134048aea33aSFlorian Fainelli 		if ((reg & BIT(cpu_port)) == BIT(cpu_port))
134148aea33aSFlorian Fainelli 			reg &= ~BIT(cpu_port);
134248aea33aSFlorian Fainelli 		b53_write16(dev, B53_VLAN_PAGE, B53_JOIN_ALL_VLAN_EN, reg);
134348aea33aSFlorian Fainelli 	}
134448aea33aSFlorian Fainelli 
1345ff39c2d6SFlorian Fainelli 	b53_read16(dev, B53_PVLAN_PAGE, B53_PVLAN_PORT_MASK(port), &pvlan);
1346ff39c2d6SFlorian Fainelli 
1347ff39c2d6SFlorian Fainelli 	b53_for_each_port(dev, i) {
1348ddd3a0c8SVivien Didelot 		if (ds->ports[i].bridge_dev != br)
1349ff39c2d6SFlorian Fainelli 			continue;
1350ff39c2d6SFlorian Fainelli 
1351ff39c2d6SFlorian Fainelli 		/* Add this local port to the remote port VLAN control
1352ff39c2d6SFlorian Fainelli 		 * membership and update the remote port bitmask
1353ff39c2d6SFlorian Fainelli 		 */
1354ff39c2d6SFlorian Fainelli 		b53_read16(dev, B53_PVLAN_PAGE, B53_PVLAN_PORT_MASK(i), &reg);
1355ff39c2d6SFlorian Fainelli 		reg |= BIT(port);
1356ff39c2d6SFlorian Fainelli 		b53_write16(dev, B53_PVLAN_PAGE, B53_PVLAN_PORT_MASK(i), reg);
1357ff39c2d6SFlorian Fainelli 		dev->ports[i].vlan_ctl_mask = reg;
1358ff39c2d6SFlorian Fainelli 
1359ff39c2d6SFlorian Fainelli 		pvlan |= BIT(i);
1360ff39c2d6SFlorian Fainelli 	}
1361ff39c2d6SFlorian Fainelli 
1362ff39c2d6SFlorian Fainelli 	/* Configure the local port VLAN control membership to include
1363ff39c2d6SFlorian Fainelli 	 * remote ports and update the local port bitmask
1364ff39c2d6SFlorian Fainelli 	 */
1365ff39c2d6SFlorian Fainelli 	b53_write16(dev, B53_PVLAN_PAGE, B53_PVLAN_PORT_MASK(port), pvlan);
1366ff39c2d6SFlorian Fainelli 	dev->ports[port].vlan_ctl_mask = pvlan;
1367ff39c2d6SFlorian Fainelli 
1368ff39c2d6SFlorian Fainelli 	return 0;
1369ff39c2d6SFlorian Fainelli }
13703117455dSFlorian Fainelli EXPORT_SYMBOL(b53_br_join);
1371ff39c2d6SFlorian Fainelli 
1372f123f2fbSVivien Didelot void b53_br_leave(struct dsa_switch *ds, int port, struct net_device *br)
1373ff39c2d6SFlorian Fainelli {
137404bed143SVivien Didelot 	struct b53_device *dev = ds->priv;
1375a2482d2cSFlorian Fainelli 	struct b53_vlan *vl = &dev->vlans[0];
13768b0d3ea5SVivien Didelot 	s8 cpu_port = ds->dst->cpu_dp->index;
1377ff39c2d6SFlorian Fainelli 	unsigned int i;
1378a2482d2cSFlorian Fainelli 	u16 pvlan, reg, pvid;
1379ff39c2d6SFlorian Fainelli 
1380ff39c2d6SFlorian Fainelli 	b53_read16(dev, B53_PVLAN_PAGE, B53_PVLAN_PORT_MASK(port), &pvlan);
1381ff39c2d6SFlorian Fainelli 
1382ff39c2d6SFlorian Fainelli 	b53_for_each_port(dev, i) {
1383ff39c2d6SFlorian Fainelli 		/* Don't touch the remaining ports */
1384ddd3a0c8SVivien Didelot 		if (ds->ports[i].bridge_dev != br)
1385ff39c2d6SFlorian Fainelli 			continue;
1386ff39c2d6SFlorian Fainelli 
1387ff39c2d6SFlorian Fainelli 		b53_read16(dev, B53_PVLAN_PAGE, B53_PVLAN_PORT_MASK(i), &reg);
1388ff39c2d6SFlorian Fainelli 		reg &= ~BIT(port);
1389ff39c2d6SFlorian Fainelli 		b53_write16(dev, B53_PVLAN_PAGE, B53_PVLAN_PORT_MASK(i), reg);
1390ff39c2d6SFlorian Fainelli 		dev->ports[port].vlan_ctl_mask = reg;
1391ff39c2d6SFlorian Fainelli 
1392ff39c2d6SFlorian Fainelli 		/* Prevent self removal to preserve isolation */
1393ff39c2d6SFlorian Fainelli 		if (port != i)
1394ff39c2d6SFlorian Fainelli 			pvlan &= ~BIT(i);
1395ff39c2d6SFlorian Fainelli 	}
1396ff39c2d6SFlorian Fainelli 
1397ff39c2d6SFlorian Fainelli 	b53_write16(dev, B53_PVLAN_PAGE, B53_PVLAN_PORT_MASK(port), pvlan);
1398ff39c2d6SFlorian Fainelli 	dev->ports[port].vlan_ctl_mask = pvlan;
1399a2482d2cSFlorian Fainelli 
1400a2482d2cSFlorian Fainelli 	if (is5325(dev) || is5365(dev))
1401a2482d2cSFlorian Fainelli 		pvid = 1;
1402a2482d2cSFlorian Fainelli 	else
1403a2482d2cSFlorian Fainelli 		pvid = 0;
1404a2482d2cSFlorian Fainelli 
140548aea33aSFlorian Fainelli 	/* Make this port join all VLANs without VLAN entries */
140648aea33aSFlorian Fainelli 	if (is58xx(dev)) {
140748aea33aSFlorian Fainelli 		b53_read16(dev, B53_VLAN_PAGE, B53_JOIN_ALL_VLAN_EN, &reg);
140848aea33aSFlorian Fainelli 		reg |= BIT(port);
140948aea33aSFlorian Fainelli 		if (!(reg & BIT(cpu_port)))
141048aea33aSFlorian Fainelli 			reg |= BIT(cpu_port);
141148aea33aSFlorian Fainelli 		b53_write16(dev, B53_VLAN_PAGE, B53_JOIN_ALL_VLAN_EN, reg);
141248aea33aSFlorian Fainelli 	} else {
1413a2482d2cSFlorian Fainelli 		b53_get_vlan_entry(dev, pvid, vl);
1414a2482d2cSFlorian Fainelli 		vl->members |= BIT(port) | BIT(dev->cpu_port);
1415a2482d2cSFlorian Fainelli 		vl->untag |= BIT(port) | BIT(dev->cpu_port);
1416a2482d2cSFlorian Fainelli 		b53_set_vlan_entry(dev, pvid, vl);
1417ff39c2d6SFlorian Fainelli 	}
141848aea33aSFlorian Fainelli }
14193117455dSFlorian Fainelli EXPORT_SYMBOL(b53_br_leave);
1420ff39c2d6SFlorian Fainelli 
14213117455dSFlorian Fainelli void b53_br_set_stp_state(struct dsa_switch *ds, int port, u8 state)
1422ff39c2d6SFlorian Fainelli {
142304bed143SVivien Didelot 	struct b53_device *dev = ds->priv;
1424597698f1SVivien Didelot 	u8 hw_state;
1425ff39c2d6SFlorian Fainelli 	u8 reg;
1426ff39c2d6SFlorian Fainelli 
1427ff39c2d6SFlorian Fainelli 	switch (state) {
1428ff39c2d6SFlorian Fainelli 	case BR_STATE_DISABLED:
1429ff39c2d6SFlorian Fainelli 		hw_state = PORT_CTRL_DIS_STATE;
1430ff39c2d6SFlorian Fainelli 		break;
1431ff39c2d6SFlorian Fainelli 	case BR_STATE_LISTENING:
1432ff39c2d6SFlorian Fainelli 		hw_state = PORT_CTRL_LISTEN_STATE;
1433ff39c2d6SFlorian Fainelli 		break;
1434ff39c2d6SFlorian Fainelli 	case BR_STATE_LEARNING:
1435ff39c2d6SFlorian Fainelli 		hw_state = PORT_CTRL_LEARN_STATE;
1436ff39c2d6SFlorian Fainelli 		break;
1437ff39c2d6SFlorian Fainelli 	case BR_STATE_FORWARDING:
1438ff39c2d6SFlorian Fainelli 		hw_state = PORT_CTRL_FWD_STATE;
1439ff39c2d6SFlorian Fainelli 		break;
1440ff39c2d6SFlorian Fainelli 	case BR_STATE_BLOCKING:
1441ff39c2d6SFlorian Fainelli 		hw_state = PORT_CTRL_BLOCK_STATE;
1442ff39c2d6SFlorian Fainelli 		break;
1443ff39c2d6SFlorian Fainelli 	default:
1444ff39c2d6SFlorian Fainelli 		dev_err(ds->dev, "invalid STP state: %d\n", state);
1445ff39c2d6SFlorian Fainelli 		return;
1446ff39c2d6SFlorian Fainelli 	}
1447ff39c2d6SFlorian Fainelli 
1448ff39c2d6SFlorian Fainelli 	b53_read8(dev, B53_CTRL_PAGE, B53_PORT_CTRL(port), &reg);
1449ff39c2d6SFlorian Fainelli 	reg &= ~PORT_CTRL_STP_STATE_MASK;
1450ff39c2d6SFlorian Fainelli 	reg |= hw_state;
1451ff39c2d6SFlorian Fainelli 	b53_write8(dev, B53_CTRL_PAGE, B53_PORT_CTRL(port), reg);
1452ff39c2d6SFlorian Fainelli }
14533117455dSFlorian Fainelli EXPORT_SYMBOL(b53_br_set_stp_state);
1454ff39c2d6SFlorian Fainelli 
14553117455dSFlorian Fainelli void b53_br_fast_age(struct dsa_switch *ds, int port)
1456597698f1SVivien Didelot {
1457597698f1SVivien Didelot 	struct b53_device *dev = ds->priv;
1458597698f1SVivien Didelot 
1459597698f1SVivien Didelot 	if (b53_fast_age_port(dev, port))
1460597698f1SVivien Didelot 		dev_err(ds->dev, "fast ageing failed\n");
1461597698f1SVivien Didelot }
14623117455dSFlorian Fainelli EXPORT_SYMBOL(b53_br_fast_age);
1463597698f1SVivien Didelot 
14647b314362SAndrew Lunn static enum dsa_tag_protocol b53_get_tag_protocol(struct dsa_switch *ds)
14657b314362SAndrew Lunn {
14667b314362SAndrew Lunn 	return DSA_TAG_PROTO_NONE;
14677b314362SAndrew Lunn }
14687b314362SAndrew Lunn 
1469ed3af5fdSFlorian Fainelli int b53_mirror_add(struct dsa_switch *ds, int port,
1470ed3af5fdSFlorian Fainelli 		   struct dsa_mall_mirror_tc_entry *mirror, bool ingress)
1471ed3af5fdSFlorian Fainelli {
1472ed3af5fdSFlorian Fainelli 	struct b53_device *dev = ds->priv;
1473ed3af5fdSFlorian Fainelli 	u16 reg, loc;
1474ed3af5fdSFlorian Fainelli 
1475ed3af5fdSFlorian Fainelli 	if (ingress)
1476ed3af5fdSFlorian Fainelli 		loc = B53_IG_MIR_CTL;
1477ed3af5fdSFlorian Fainelli 	else
1478ed3af5fdSFlorian Fainelli 		loc = B53_EG_MIR_CTL;
1479ed3af5fdSFlorian Fainelli 
1480ed3af5fdSFlorian Fainelli 	b53_read16(dev, B53_MGMT_PAGE, loc, &reg);
1481ed3af5fdSFlorian Fainelli 	reg &= ~MIRROR_MASK;
1482ed3af5fdSFlorian Fainelli 	reg |= BIT(port);
1483ed3af5fdSFlorian Fainelli 	b53_write16(dev, B53_MGMT_PAGE, loc, reg);
1484ed3af5fdSFlorian Fainelli 
1485ed3af5fdSFlorian Fainelli 	b53_read16(dev, B53_MGMT_PAGE, B53_MIR_CAP_CTL, &reg);
1486ed3af5fdSFlorian Fainelli 	reg &= ~CAP_PORT_MASK;
1487ed3af5fdSFlorian Fainelli 	reg |= mirror->to_local_port;
1488ed3af5fdSFlorian Fainelli 	reg |= MIRROR_EN;
1489ed3af5fdSFlorian Fainelli 	b53_write16(dev, B53_MGMT_PAGE, B53_MIR_CAP_CTL, reg);
1490ed3af5fdSFlorian Fainelli 
1491ed3af5fdSFlorian Fainelli 	return 0;
1492ed3af5fdSFlorian Fainelli }
1493ed3af5fdSFlorian Fainelli EXPORT_SYMBOL(b53_mirror_add);
1494ed3af5fdSFlorian Fainelli 
1495ed3af5fdSFlorian Fainelli void b53_mirror_del(struct dsa_switch *ds, int port,
1496ed3af5fdSFlorian Fainelli 		    struct dsa_mall_mirror_tc_entry *mirror)
1497ed3af5fdSFlorian Fainelli {
1498ed3af5fdSFlorian Fainelli 	struct b53_device *dev = ds->priv;
1499ed3af5fdSFlorian Fainelli 	bool loc_disable = false, other_loc_disable = false;
1500ed3af5fdSFlorian Fainelli 	u16 reg, loc;
1501ed3af5fdSFlorian Fainelli 
1502ed3af5fdSFlorian Fainelli 	if (mirror->ingress)
1503ed3af5fdSFlorian Fainelli 		loc = B53_IG_MIR_CTL;
1504ed3af5fdSFlorian Fainelli 	else
1505ed3af5fdSFlorian Fainelli 		loc = B53_EG_MIR_CTL;
1506ed3af5fdSFlorian Fainelli 
1507ed3af5fdSFlorian Fainelli 	/* Update the desired ingress/egress register */
1508ed3af5fdSFlorian Fainelli 	b53_read16(dev, B53_MGMT_PAGE, loc, &reg);
1509ed3af5fdSFlorian Fainelli 	reg &= ~BIT(port);
1510ed3af5fdSFlorian Fainelli 	if (!(reg & MIRROR_MASK))
1511ed3af5fdSFlorian Fainelli 		loc_disable = true;
1512ed3af5fdSFlorian Fainelli 	b53_write16(dev, B53_MGMT_PAGE, loc, reg);
1513ed3af5fdSFlorian Fainelli 
1514ed3af5fdSFlorian Fainelli 	/* Now look at the other one to know if we can disable mirroring
1515ed3af5fdSFlorian Fainelli 	 * entirely
1516ed3af5fdSFlorian Fainelli 	 */
1517ed3af5fdSFlorian Fainelli 	if (mirror->ingress)
1518ed3af5fdSFlorian Fainelli 		b53_read16(dev, B53_MGMT_PAGE, B53_EG_MIR_CTL, &reg);
1519ed3af5fdSFlorian Fainelli 	else
1520ed3af5fdSFlorian Fainelli 		b53_read16(dev, B53_MGMT_PAGE, B53_IG_MIR_CTL, &reg);
1521ed3af5fdSFlorian Fainelli 	if (!(reg & MIRROR_MASK))
1522ed3af5fdSFlorian Fainelli 		other_loc_disable = true;
1523ed3af5fdSFlorian Fainelli 
1524ed3af5fdSFlorian Fainelli 	b53_read16(dev, B53_MGMT_PAGE, B53_MIR_CAP_CTL, &reg);
1525ed3af5fdSFlorian Fainelli 	/* Both no longer have ports, let's disable mirroring */
1526ed3af5fdSFlorian Fainelli 	if (loc_disable && other_loc_disable) {
1527ed3af5fdSFlorian Fainelli 		reg &= ~MIRROR_EN;
1528ed3af5fdSFlorian Fainelli 		reg &= ~mirror->to_local_port;
1529ed3af5fdSFlorian Fainelli 	}
1530ed3af5fdSFlorian Fainelli 	b53_write16(dev, B53_MGMT_PAGE, B53_MIR_CAP_CTL, reg);
1531ed3af5fdSFlorian Fainelli }
1532ed3af5fdSFlorian Fainelli EXPORT_SYMBOL(b53_mirror_del);
1533ed3af5fdSFlorian Fainelli 
1534a82f67afSFlorian Fainelli static const struct dsa_switch_ops b53_switch_ops = {
15357b314362SAndrew Lunn 	.get_tag_protocol	= b53_get_tag_protocol,
1536967dd82fSFlorian Fainelli 	.setup			= b53_setup,
1537967dd82fSFlorian Fainelli 	.get_strings		= b53_get_strings,
1538967dd82fSFlorian Fainelli 	.get_ethtool_stats	= b53_get_ethtool_stats,
1539967dd82fSFlorian Fainelli 	.get_sset_count		= b53_get_sset_count,
1540967dd82fSFlorian Fainelli 	.phy_read		= b53_phy_read16,
1541967dd82fSFlorian Fainelli 	.phy_write		= b53_phy_write16,
1542967dd82fSFlorian Fainelli 	.adjust_link		= b53_adjust_link,
1543967dd82fSFlorian Fainelli 	.port_enable		= b53_enable_port,
1544967dd82fSFlorian Fainelli 	.port_disable		= b53_disable_port,
1545ff39c2d6SFlorian Fainelli 	.port_bridge_join	= b53_br_join,
1546ff39c2d6SFlorian Fainelli 	.port_bridge_leave	= b53_br_leave,
1547ff39c2d6SFlorian Fainelli 	.port_stp_state_set	= b53_br_set_stp_state,
1548597698f1SVivien Didelot 	.port_fast_age		= b53_br_fast_age,
1549a2482d2cSFlorian Fainelli 	.port_vlan_filtering	= b53_vlan_filtering,
1550a2482d2cSFlorian Fainelli 	.port_vlan_prepare	= b53_vlan_prepare,
1551a2482d2cSFlorian Fainelli 	.port_vlan_add		= b53_vlan_add,
1552a2482d2cSFlorian Fainelli 	.port_vlan_del		= b53_vlan_del,
15531da6df85SFlorian Fainelli 	.port_fdb_dump		= b53_fdb_dump,
15541da6df85SFlorian Fainelli 	.port_fdb_add		= b53_fdb_add,
15551da6df85SFlorian Fainelli 	.port_fdb_del		= b53_fdb_del,
1556ed3af5fdSFlorian Fainelli 	.port_mirror_add	= b53_mirror_add,
1557ed3af5fdSFlorian Fainelli 	.port_mirror_del	= b53_mirror_del,
1558967dd82fSFlorian Fainelli };
1559967dd82fSFlorian Fainelli 
1560967dd82fSFlorian Fainelli struct b53_chip_data {
1561967dd82fSFlorian Fainelli 	u32 chip_id;
1562967dd82fSFlorian Fainelli 	const char *dev_name;
1563967dd82fSFlorian Fainelli 	u16 vlans;
1564967dd82fSFlorian Fainelli 	u16 enabled_ports;
1565967dd82fSFlorian Fainelli 	u8 cpu_port;
1566967dd82fSFlorian Fainelli 	u8 vta_regs[3];
15671da6df85SFlorian Fainelli 	u8 arl_entries;
1568967dd82fSFlorian Fainelli 	u8 duplex_reg;
1569967dd82fSFlorian Fainelli 	u8 jumbo_pm_reg;
1570967dd82fSFlorian Fainelli 	u8 jumbo_size_reg;
1571967dd82fSFlorian Fainelli };
1572967dd82fSFlorian Fainelli 
1573967dd82fSFlorian Fainelli #define B53_VTA_REGS	\
1574967dd82fSFlorian Fainelli 	{ B53_VT_ACCESS, B53_VT_INDEX, B53_VT_ENTRY }
1575967dd82fSFlorian Fainelli #define B53_VTA_REGS_9798 \
1576967dd82fSFlorian Fainelli 	{ B53_VT_ACCESS_9798, B53_VT_INDEX_9798, B53_VT_ENTRY_9798 }
1577967dd82fSFlorian Fainelli #define B53_VTA_REGS_63XX \
1578967dd82fSFlorian Fainelli 	{ B53_VT_ACCESS_63XX, B53_VT_INDEX_63XX, B53_VT_ENTRY_63XX }
1579967dd82fSFlorian Fainelli 
1580967dd82fSFlorian Fainelli static const struct b53_chip_data b53_switch_chips[] = {
1581967dd82fSFlorian Fainelli 	{
1582967dd82fSFlorian Fainelli 		.chip_id = BCM5325_DEVICE_ID,
1583967dd82fSFlorian Fainelli 		.dev_name = "BCM5325",
1584967dd82fSFlorian Fainelli 		.vlans = 16,
1585967dd82fSFlorian Fainelli 		.enabled_ports = 0x1f,
15861da6df85SFlorian Fainelli 		.arl_entries = 2,
1587967dd82fSFlorian Fainelli 		.cpu_port = B53_CPU_PORT_25,
1588967dd82fSFlorian Fainelli 		.duplex_reg = B53_DUPLEX_STAT_FE,
1589967dd82fSFlorian Fainelli 	},
1590967dd82fSFlorian Fainelli 	{
1591967dd82fSFlorian Fainelli 		.chip_id = BCM5365_DEVICE_ID,
1592967dd82fSFlorian Fainelli 		.dev_name = "BCM5365",
1593967dd82fSFlorian Fainelli 		.vlans = 256,
1594967dd82fSFlorian Fainelli 		.enabled_ports = 0x1f,
15951da6df85SFlorian Fainelli 		.arl_entries = 2,
1596967dd82fSFlorian Fainelli 		.cpu_port = B53_CPU_PORT_25,
1597967dd82fSFlorian Fainelli 		.duplex_reg = B53_DUPLEX_STAT_FE,
1598967dd82fSFlorian Fainelli 	},
1599967dd82fSFlorian Fainelli 	{
1600967dd82fSFlorian Fainelli 		.chip_id = BCM5395_DEVICE_ID,
1601967dd82fSFlorian Fainelli 		.dev_name = "BCM5395",
1602967dd82fSFlorian Fainelli 		.vlans = 4096,
1603967dd82fSFlorian Fainelli 		.enabled_ports = 0x1f,
16041da6df85SFlorian Fainelli 		.arl_entries = 4,
1605967dd82fSFlorian Fainelli 		.cpu_port = B53_CPU_PORT,
1606967dd82fSFlorian Fainelli 		.vta_regs = B53_VTA_REGS,
1607967dd82fSFlorian Fainelli 		.duplex_reg = B53_DUPLEX_STAT_GE,
1608967dd82fSFlorian Fainelli 		.jumbo_pm_reg = B53_JUMBO_PORT_MASK,
1609967dd82fSFlorian Fainelli 		.jumbo_size_reg = B53_JUMBO_MAX_SIZE,
1610967dd82fSFlorian Fainelli 	},
1611967dd82fSFlorian Fainelli 	{
1612967dd82fSFlorian Fainelli 		.chip_id = BCM5397_DEVICE_ID,
1613967dd82fSFlorian Fainelli 		.dev_name = "BCM5397",
1614967dd82fSFlorian Fainelli 		.vlans = 4096,
1615967dd82fSFlorian Fainelli 		.enabled_ports = 0x1f,
16161da6df85SFlorian Fainelli 		.arl_entries = 4,
1617967dd82fSFlorian Fainelli 		.cpu_port = B53_CPU_PORT,
1618967dd82fSFlorian Fainelli 		.vta_regs = B53_VTA_REGS_9798,
1619967dd82fSFlorian Fainelli 		.duplex_reg = B53_DUPLEX_STAT_GE,
1620967dd82fSFlorian Fainelli 		.jumbo_pm_reg = B53_JUMBO_PORT_MASK,
1621967dd82fSFlorian Fainelli 		.jumbo_size_reg = B53_JUMBO_MAX_SIZE,
1622967dd82fSFlorian Fainelli 	},
1623967dd82fSFlorian Fainelli 	{
1624967dd82fSFlorian Fainelli 		.chip_id = BCM5398_DEVICE_ID,
1625967dd82fSFlorian Fainelli 		.dev_name = "BCM5398",
1626967dd82fSFlorian Fainelli 		.vlans = 4096,
1627967dd82fSFlorian Fainelli 		.enabled_ports = 0x7f,
16281da6df85SFlorian Fainelli 		.arl_entries = 4,
1629967dd82fSFlorian Fainelli 		.cpu_port = B53_CPU_PORT,
1630967dd82fSFlorian Fainelli 		.vta_regs = B53_VTA_REGS_9798,
1631967dd82fSFlorian Fainelli 		.duplex_reg = B53_DUPLEX_STAT_GE,
1632967dd82fSFlorian Fainelli 		.jumbo_pm_reg = B53_JUMBO_PORT_MASK,
1633967dd82fSFlorian Fainelli 		.jumbo_size_reg = B53_JUMBO_MAX_SIZE,
1634967dd82fSFlorian Fainelli 	},
1635967dd82fSFlorian Fainelli 	{
1636967dd82fSFlorian Fainelli 		.chip_id = BCM53115_DEVICE_ID,
1637967dd82fSFlorian Fainelli 		.dev_name = "BCM53115",
1638967dd82fSFlorian Fainelli 		.vlans = 4096,
1639967dd82fSFlorian Fainelli 		.enabled_ports = 0x1f,
16401da6df85SFlorian Fainelli 		.arl_entries = 4,
1641967dd82fSFlorian Fainelli 		.vta_regs = B53_VTA_REGS,
1642967dd82fSFlorian Fainelli 		.cpu_port = B53_CPU_PORT,
1643967dd82fSFlorian Fainelli 		.duplex_reg = B53_DUPLEX_STAT_GE,
1644967dd82fSFlorian Fainelli 		.jumbo_pm_reg = B53_JUMBO_PORT_MASK,
1645967dd82fSFlorian Fainelli 		.jumbo_size_reg = B53_JUMBO_MAX_SIZE,
1646967dd82fSFlorian Fainelli 	},
1647967dd82fSFlorian Fainelli 	{
1648967dd82fSFlorian Fainelli 		.chip_id = BCM53125_DEVICE_ID,
1649967dd82fSFlorian Fainelli 		.dev_name = "BCM53125",
1650967dd82fSFlorian Fainelli 		.vlans = 4096,
1651967dd82fSFlorian Fainelli 		.enabled_ports = 0xff,
1652be35e8c5SFlorian Fainelli 		.arl_entries = 4,
1653967dd82fSFlorian Fainelli 		.cpu_port = B53_CPU_PORT,
1654967dd82fSFlorian Fainelli 		.vta_regs = B53_VTA_REGS,
1655967dd82fSFlorian Fainelli 		.duplex_reg = B53_DUPLEX_STAT_GE,
1656967dd82fSFlorian Fainelli 		.jumbo_pm_reg = B53_JUMBO_PORT_MASK,
1657967dd82fSFlorian Fainelli 		.jumbo_size_reg = B53_JUMBO_MAX_SIZE,
1658967dd82fSFlorian Fainelli 	},
1659967dd82fSFlorian Fainelli 	{
1660967dd82fSFlorian Fainelli 		.chip_id = BCM53128_DEVICE_ID,
1661967dd82fSFlorian Fainelli 		.dev_name = "BCM53128",
1662967dd82fSFlorian Fainelli 		.vlans = 4096,
1663967dd82fSFlorian Fainelli 		.enabled_ports = 0x1ff,
16641da6df85SFlorian Fainelli 		.arl_entries = 4,
1665967dd82fSFlorian Fainelli 		.cpu_port = B53_CPU_PORT,
1666967dd82fSFlorian Fainelli 		.vta_regs = B53_VTA_REGS,
1667967dd82fSFlorian Fainelli 		.duplex_reg = B53_DUPLEX_STAT_GE,
1668967dd82fSFlorian Fainelli 		.jumbo_pm_reg = B53_JUMBO_PORT_MASK,
1669967dd82fSFlorian Fainelli 		.jumbo_size_reg = B53_JUMBO_MAX_SIZE,
1670967dd82fSFlorian Fainelli 	},
1671967dd82fSFlorian Fainelli 	{
1672967dd82fSFlorian Fainelli 		.chip_id = BCM63XX_DEVICE_ID,
1673967dd82fSFlorian Fainelli 		.dev_name = "BCM63xx",
1674967dd82fSFlorian Fainelli 		.vlans = 4096,
1675967dd82fSFlorian Fainelli 		.enabled_ports = 0, /* pdata must provide them */
16761da6df85SFlorian Fainelli 		.arl_entries = 4,
1677967dd82fSFlorian Fainelli 		.cpu_port = B53_CPU_PORT,
1678967dd82fSFlorian Fainelli 		.vta_regs = B53_VTA_REGS_63XX,
1679967dd82fSFlorian Fainelli 		.duplex_reg = B53_DUPLEX_STAT_63XX,
1680967dd82fSFlorian Fainelli 		.jumbo_pm_reg = B53_JUMBO_PORT_MASK_63XX,
1681967dd82fSFlorian Fainelli 		.jumbo_size_reg = B53_JUMBO_MAX_SIZE_63XX,
1682967dd82fSFlorian Fainelli 	},
1683967dd82fSFlorian Fainelli 	{
1684967dd82fSFlorian Fainelli 		.chip_id = BCM53010_DEVICE_ID,
1685967dd82fSFlorian Fainelli 		.dev_name = "BCM53010",
1686967dd82fSFlorian Fainelli 		.vlans = 4096,
1687967dd82fSFlorian Fainelli 		.enabled_ports = 0x1f,
16881da6df85SFlorian Fainelli 		.arl_entries = 4,
1689967dd82fSFlorian Fainelli 		.cpu_port = B53_CPU_PORT_25, /* TODO: auto detect */
1690967dd82fSFlorian Fainelli 		.vta_regs = B53_VTA_REGS,
1691967dd82fSFlorian Fainelli 		.duplex_reg = B53_DUPLEX_STAT_GE,
1692967dd82fSFlorian Fainelli 		.jumbo_pm_reg = B53_JUMBO_PORT_MASK,
1693967dd82fSFlorian Fainelli 		.jumbo_size_reg = B53_JUMBO_MAX_SIZE,
1694967dd82fSFlorian Fainelli 	},
1695967dd82fSFlorian Fainelli 	{
1696967dd82fSFlorian Fainelli 		.chip_id = BCM53011_DEVICE_ID,
1697967dd82fSFlorian Fainelli 		.dev_name = "BCM53011",
1698967dd82fSFlorian Fainelli 		.vlans = 4096,
1699967dd82fSFlorian Fainelli 		.enabled_ports = 0x1bf,
17001da6df85SFlorian Fainelli 		.arl_entries = 4,
1701967dd82fSFlorian Fainelli 		.cpu_port = B53_CPU_PORT_25, /* TODO: auto detect */
1702967dd82fSFlorian Fainelli 		.vta_regs = B53_VTA_REGS,
1703967dd82fSFlorian Fainelli 		.duplex_reg = B53_DUPLEX_STAT_GE,
1704967dd82fSFlorian Fainelli 		.jumbo_pm_reg = B53_JUMBO_PORT_MASK,
1705967dd82fSFlorian Fainelli 		.jumbo_size_reg = B53_JUMBO_MAX_SIZE,
1706967dd82fSFlorian Fainelli 	},
1707967dd82fSFlorian Fainelli 	{
1708967dd82fSFlorian Fainelli 		.chip_id = BCM53012_DEVICE_ID,
1709967dd82fSFlorian Fainelli 		.dev_name = "BCM53012",
1710967dd82fSFlorian Fainelli 		.vlans = 4096,
1711967dd82fSFlorian Fainelli 		.enabled_ports = 0x1bf,
17121da6df85SFlorian Fainelli 		.arl_entries = 4,
1713967dd82fSFlorian Fainelli 		.cpu_port = B53_CPU_PORT_25, /* TODO: auto detect */
1714967dd82fSFlorian Fainelli 		.vta_regs = B53_VTA_REGS,
1715967dd82fSFlorian Fainelli 		.duplex_reg = B53_DUPLEX_STAT_GE,
1716967dd82fSFlorian Fainelli 		.jumbo_pm_reg = B53_JUMBO_PORT_MASK,
1717967dd82fSFlorian Fainelli 		.jumbo_size_reg = B53_JUMBO_MAX_SIZE,
1718967dd82fSFlorian Fainelli 	},
1719967dd82fSFlorian Fainelli 	{
1720967dd82fSFlorian Fainelli 		.chip_id = BCM53018_DEVICE_ID,
1721967dd82fSFlorian Fainelli 		.dev_name = "BCM53018",
1722967dd82fSFlorian Fainelli 		.vlans = 4096,
1723967dd82fSFlorian Fainelli 		.enabled_ports = 0x1f,
17241da6df85SFlorian Fainelli 		.arl_entries = 4,
1725967dd82fSFlorian Fainelli 		.cpu_port = B53_CPU_PORT_25, /* TODO: auto detect */
1726967dd82fSFlorian Fainelli 		.vta_regs = B53_VTA_REGS,
1727967dd82fSFlorian Fainelli 		.duplex_reg = B53_DUPLEX_STAT_GE,
1728967dd82fSFlorian Fainelli 		.jumbo_pm_reg = B53_JUMBO_PORT_MASK,
1729967dd82fSFlorian Fainelli 		.jumbo_size_reg = B53_JUMBO_MAX_SIZE,
1730967dd82fSFlorian Fainelli 	},
1731967dd82fSFlorian Fainelli 	{
1732967dd82fSFlorian Fainelli 		.chip_id = BCM53019_DEVICE_ID,
1733967dd82fSFlorian Fainelli 		.dev_name = "BCM53019",
1734967dd82fSFlorian Fainelli 		.vlans = 4096,
1735967dd82fSFlorian Fainelli 		.enabled_ports = 0x1f,
17361da6df85SFlorian Fainelli 		.arl_entries = 4,
1737967dd82fSFlorian Fainelli 		.cpu_port = B53_CPU_PORT_25, /* TODO: auto detect */
1738967dd82fSFlorian Fainelli 		.vta_regs = B53_VTA_REGS,
1739967dd82fSFlorian Fainelli 		.duplex_reg = B53_DUPLEX_STAT_GE,
1740967dd82fSFlorian Fainelli 		.jumbo_pm_reg = B53_JUMBO_PORT_MASK,
1741967dd82fSFlorian Fainelli 		.jumbo_size_reg = B53_JUMBO_MAX_SIZE,
1742967dd82fSFlorian Fainelli 	},
1743991a36bbSFlorian Fainelli 	{
1744991a36bbSFlorian Fainelli 		.chip_id = BCM58XX_DEVICE_ID,
1745991a36bbSFlorian Fainelli 		.dev_name = "BCM585xx/586xx/88312",
1746991a36bbSFlorian Fainelli 		.vlans	= 4096,
1747991a36bbSFlorian Fainelli 		.enabled_ports = 0x1ff,
1748991a36bbSFlorian Fainelli 		.arl_entries = 4,
1749bfcda65cSFlorian Fainelli 		.cpu_port = B53_CPU_PORT,
1750991a36bbSFlorian Fainelli 		.vta_regs = B53_VTA_REGS,
1751991a36bbSFlorian Fainelli 		.duplex_reg = B53_DUPLEX_STAT_GE,
1752991a36bbSFlorian Fainelli 		.jumbo_pm_reg = B53_JUMBO_PORT_MASK,
1753991a36bbSFlorian Fainelli 		.jumbo_size_reg = B53_JUMBO_MAX_SIZE,
1754991a36bbSFlorian Fainelli 	},
1755130401d9SFlorian Fainelli 	{
1756130401d9SFlorian Fainelli 		.chip_id = BCM7445_DEVICE_ID,
1757130401d9SFlorian Fainelli 		.dev_name = "BCM7445",
1758130401d9SFlorian Fainelli 		.vlans	= 4096,
1759130401d9SFlorian Fainelli 		.enabled_ports = 0x1ff,
1760130401d9SFlorian Fainelli 		.arl_entries = 4,
1761130401d9SFlorian Fainelli 		.cpu_port = B53_CPU_PORT,
1762130401d9SFlorian Fainelli 		.vta_regs = B53_VTA_REGS,
1763130401d9SFlorian Fainelli 		.duplex_reg = B53_DUPLEX_STAT_GE,
1764130401d9SFlorian Fainelli 		.jumbo_pm_reg = B53_JUMBO_PORT_MASK,
1765130401d9SFlorian Fainelli 		.jumbo_size_reg = B53_JUMBO_MAX_SIZE,
1766130401d9SFlorian Fainelli 	},
17670fe99338SFlorian Fainelli 	{
17680fe99338SFlorian Fainelli 		.chip_id = BCM7278_DEVICE_ID,
17690fe99338SFlorian Fainelli 		.dev_name = "BCM7278",
17700fe99338SFlorian Fainelli 		.vlans = 4096,
17710fe99338SFlorian Fainelli 		.enabled_ports = 0x1ff,
17720fe99338SFlorian Fainelli 		.arl_entries= 4,
17730fe99338SFlorian Fainelli 		.cpu_port = B53_CPU_PORT,
17740fe99338SFlorian Fainelli 		.vta_regs = B53_VTA_REGS,
17750fe99338SFlorian Fainelli 		.duplex_reg = B53_DUPLEX_STAT_GE,
17760fe99338SFlorian Fainelli 		.jumbo_pm_reg = B53_JUMBO_PORT_MASK,
17770fe99338SFlorian Fainelli 		.jumbo_size_reg = B53_JUMBO_MAX_SIZE,
17780fe99338SFlorian Fainelli 	},
1779967dd82fSFlorian Fainelli };
1780967dd82fSFlorian Fainelli 
1781967dd82fSFlorian Fainelli static int b53_switch_init(struct b53_device *dev)
1782967dd82fSFlorian Fainelli {
1783967dd82fSFlorian Fainelli 	unsigned int i;
1784967dd82fSFlorian Fainelli 	int ret;
1785967dd82fSFlorian Fainelli 
1786967dd82fSFlorian Fainelli 	for (i = 0; i < ARRAY_SIZE(b53_switch_chips); i++) {
1787967dd82fSFlorian Fainelli 		const struct b53_chip_data *chip = &b53_switch_chips[i];
1788967dd82fSFlorian Fainelli 
1789967dd82fSFlorian Fainelli 		if (chip->chip_id == dev->chip_id) {
1790967dd82fSFlorian Fainelli 			if (!dev->enabled_ports)
1791967dd82fSFlorian Fainelli 				dev->enabled_ports = chip->enabled_ports;
1792967dd82fSFlorian Fainelli 			dev->name = chip->dev_name;
1793967dd82fSFlorian Fainelli 			dev->duplex_reg = chip->duplex_reg;
1794967dd82fSFlorian Fainelli 			dev->vta_regs[0] = chip->vta_regs[0];
1795967dd82fSFlorian Fainelli 			dev->vta_regs[1] = chip->vta_regs[1];
1796967dd82fSFlorian Fainelli 			dev->vta_regs[2] = chip->vta_regs[2];
1797967dd82fSFlorian Fainelli 			dev->jumbo_pm_reg = chip->jumbo_pm_reg;
1798967dd82fSFlorian Fainelli 			dev->cpu_port = chip->cpu_port;
1799967dd82fSFlorian Fainelli 			dev->num_vlans = chip->vlans;
18001da6df85SFlorian Fainelli 			dev->num_arl_entries = chip->arl_entries;
1801967dd82fSFlorian Fainelli 			break;
1802967dd82fSFlorian Fainelli 		}
1803967dd82fSFlorian Fainelli 	}
1804967dd82fSFlorian Fainelli 
1805967dd82fSFlorian Fainelli 	/* check which BCM5325x version we have */
1806967dd82fSFlorian Fainelli 	if (is5325(dev)) {
1807967dd82fSFlorian Fainelli 		u8 vc4;
1808967dd82fSFlorian Fainelli 
1809967dd82fSFlorian Fainelli 		b53_read8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL4_25, &vc4);
1810967dd82fSFlorian Fainelli 
1811967dd82fSFlorian Fainelli 		/* check reserved bits */
1812967dd82fSFlorian Fainelli 		switch (vc4 & 3) {
1813967dd82fSFlorian Fainelli 		case 1:
1814967dd82fSFlorian Fainelli 			/* BCM5325E */
1815967dd82fSFlorian Fainelli 			break;
1816967dd82fSFlorian Fainelli 		case 3:
1817967dd82fSFlorian Fainelli 			/* BCM5325F - do not use port 4 */
1818967dd82fSFlorian Fainelli 			dev->enabled_ports &= ~BIT(4);
1819967dd82fSFlorian Fainelli 			break;
1820967dd82fSFlorian Fainelli 		default:
1821967dd82fSFlorian Fainelli /* On the BCM47XX SoCs this is the supported internal switch.*/
1822967dd82fSFlorian Fainelli #ifndef CONFIG_BCM47XX
1823967dd82fSFlorian Fainelli 			/* BCM5325M */
1824967dd82fSFlorian Fainelli 			return -EINVAL;
1825967dd82fSFlorian Fainelli #else
1826967dd82fSFlorian Fainelli 			break;
1827967dd82fSFlorian Fainelli #endif
1828967dd82fSFlorian Fainelli 		}
1829967dd82fSFlorian Fainelli 	} else if (dev->chip_id == BCM53115_DEVICE_ID) {
1830967dd82fSFlorian Fainelli 		u64 strap_value;
1831967dd82fSFlorian Fainelli 
1832967dd82fSFlorian Fainelli 		b53_read48(dev, B53_STAT_PAGE, B53_STRAP_VALUE, &strap_value);
1833967dd82fSFlorian Fainelli 		/* use second IMP port if GMII is enabled */
1834967dd82fSFlorian Fainelli 		if (strap_value & SV_GMII_CTRL_115)
1835967dd82fSFlorian Fainelli 			dev->cpu_port = 5;
1836967dd82fSFlorian Fainelli 	}
1837967dd82fSFlorian Fainelli 
1838967dd82fSFlorian Fainelli 	/* cpu port is always last */
1839967dd82fSFlorian Fainelli 	dev->num_ports = dev->cpu_port + 1;
1840967dd82fSFlorian Fainelli 	dev->enabled_ports |= BIT(dev->cpu_port);
1841967dd82fSFlorian Fainelli 
1842967dd82fSFlorian Fainelli 	dev->ports = devm_kzalloc(dev->dev,
1843967dd82fSFlorian Fainelli 				  sizeof(struct b53_port) * dev->num_ports,
1844967dd82fSFlorian Fainelli 				  GFP_KERNEL);
1845967dd82fSFlorian Fainelli 	if (!dev->ports)
1846967dd82fSFlorian Fainelli 		return -ENOMEM;
1847967dd82fSFlorian Fainelli 
1848a2482d2cSFlorian Fainelli 	dev->vlans = devm_kzalloc(dev->dev,
1849a2482d2cSFlorian Fainelli 				  sizeof(struct b53_vlan) * dev->num_vlans,
1850a2482d2cSFlorian Fainelli 				  GFP_KERNEL);
1851a2482d2cSFlorian Fainelli 	if (!dev->vlans)
1852a2482d2cSFlorian Fainelli 		return -ENOMEM;
1853a2482d2cSFlorian Fainelli 
1854967dd82fSFlorian Fainelli 	dev->reset_gpio = b53_switch_get_reset_gpio(dev);
1855967dd82fSFlorian Fainelli 	if (dev->reset_gpio >= 0) {
1856967dd82fSFlorian Fainelli 		ret = devm_gpio_request_one(dev->dev, dev->reset_gpio,
1857967dd82fSFlorian Fainelli 					    GPIOF_OUT_INIT_HIGH, "robo_reset");
1858967dd82fSFlorian Fainelli 		if (ret)
1859967dd82fSFlorian Fainelli 			return ret;
1860967dd82fSFlorian Fainelli 	}
1861967dd82fSFlorian Fainelli 
1862967dd82fSFlorian Fainelli 	return 0;
1863967dd82fSFlorian Fainelli }
1864967dd82fSFlorian Fainelli 
18650dff88d3SJulia Lawall struct b53_device *b53_switch_alloc(struct device *base,
18660dff88d3SJulia Lawall 				    const struct b53_io_ops *ops,
1867967dd82fSFlorian Fainelli 				    void *priv)
1868967dd82fSFlorian Fainelli {
1869967dd82fSFlorian Fainelli 	struct dsa_switch *ds;
1870967dd82fSFlorian Fainelli 	struct b53_device *dev;
1871967dd82fSFlorian Fainelli 
1872a0c02161SVivien Didelot 	ds = dsa_switch_alloc(base, DSA_MAX_PORTS);
1873967dd82fSFlorian Fainelli 	if (!ds)
1874967dd82fSFlorian Fainelli 		return NULL;
1875967dd82fSFlorian Fainelli 
1876a0c02161SVivien Didelot 	dev = devm_kzalloc(base, sizeof(*dev), GFP_KERNEL);
1877a0c02161SVivien Didelot 	if (!dev)
1878a0c02161SVivien Didelot 		return NULL;
1879967dd82fSFlorian Fainelli 
1880967dd82fSFlorian Fainelli 	ds->priv = dev;
1881967dd82fSFlorian Fainelli 	dev->dev = base;
1882967dd82fSFlorian Fainelli 
1883967dd82fSFlorian Fainelli 	dev->ds = ds;
1884967dd82fSFlorian Fainelli 	dev->priv = priv;
1885967dd82fSFlorian Fainelli 	dev->ops = ops;
1886485ebd61SFlorian Fainelli 	ds->ops = &b53_switch_ops;
1887967dd82fSFlorian Fainelli 	mutex_init(&dev->reg_mutex);
1888967dd82fSFlorian Fainelli 	mutex_init(&dev->stats_mutex);
1889967dd82fSFlorian Fainelli 
1890967dd82fSFlorian Fainelli 	return dev;
1891967dd82fSFlorian Fainelli }
1892967dd82fSFlorian Fainelli EXPORT_SYMBOL(b53_switch_alloc);
1893967dd82fSFlorian Fainelli 
1894967dd82fSFlorian Fainelli int b53_switch_detect(struct b53_device *dev)
1895967dd82fSFlorian Fainelli {
1896967dd82fSFlorian Fainelli 	u32 id32;
1897967dd82fSFlorian Fainelli 	u16 tmp;
1898967dd82fSFlorian Fainelli 	u8 id8;
1899967dd82fSFlorian Fainelli 	int ret;
1900967dd82fSFlorian Fainelli 
1901967dd82fSFlorian Fainelli 	ret = b53_read8(dev, B53_MGMT_PAGE, B53_DEVICE_ID, &id8);
1902967dd82fSFlorian Fainelli 	if (ret)
1903967dd82fSFlorian Fainelli 		return ret;
1904967dd82fSFlorian Fainelli 
1905967dd82fSFlorian Fainelli 	switch (id8) {
1906967dd82fSFlorian Fainelli 	case 0:
1907967dd82fSFlorian Fainelli 		/* BCM5325 and BCM5365 do not have this register so reads
1908967dd82fSFlorian Fainelli 		 * return 0. But the read operation did succeed, so assume this
1909967dd82fSFlorian Fainelli 		 * is one of them.
1910967dd82fSFlorian Fainelli 		 *
1911967dd82fSFlorian Fainelli 		 * Next check if we can write to the 5325's VTA register; for
1912967dd82fSFlorian Fainelli 		 * 5365 it is read only.
1913967dd82fSFlorian Fainelli 		 */
1914967dd82fSFlorian Fainelli 		b53_write16(dev, B53_VLAN_PAGE, B53_VLAN_TABLE_ACCESS_25, 0xf);
1915967dd82fSFlorian Fainelli 		b53_read16(dev, B53_VLAN_PAGE, B53_VLAN_TABLE_ACCESS_25, &tmp);
1916967dd82fSFlorian Fainelli 
1917967dd82fSFlorian Fainelli 		if (tmp == 0xf)
1918967dd82fSFlorian Fainelli 			dev->chip_id = BCM5325_DEVICE_ID;
1919967dd82fSFlorian Fainelli 		else
1920967dd82fSFlorian Fainelli 			dev->chip_id = BCM5365_DEVICE_ID;
1921967dd82fSFlorian Fainelli 		break;
1922967dd82fSFlorian Fainelli 	case BCM5395_DEVICE_ID:
1923967dd82fSFlorian Fainelli 	case BCM5397_DEVICE_ID:
1924967dd82fSFlorian Fainelli 	case BCM5398_DEVICE_ID:
1925967dd82fSFlorian Fainelli 		dev->chip_id = id8;
1926967dd82fSFlorian Fainelli 		break;
1927967dd82fSFlorian Fainelli 	default:
1928967dd82fSFlorian Fainelli 		ret = b53_read32(dev, B53_MGMT_PAGE, B53_DEVICE_ID, &id32);
1929967dd82fSFlorian Fainelli 		if (ret)
1930967dd82fSFlorian Fainelli 			return ret;
1931967dd82fSFlorian Fainelli 
1932967dd82fSFlorian Fainelli 		switch (id32) {
1933967dd82fSFlorian Fainelli 		case BCM53115_DEVICE_ID:
1934967dd82fSFlorian Fainelli 		case BCM53125_DEVICE_ID:
1935967dd82fSFlorian Fainelli 		case BCM53128_DEVICE_ID:
1936967dd82fSFlorian Fainelli 		case BCM53010_DEVICE_ID:
1937967dd82fSFlorian Fainelli 		case BCM53011_DEVICE_ID:
1938967dd82fSFlorian Fainelli 		case BCM53012_DEVICE_ID:
1939967dd82fSFlorian Fainelli 		case BCM53018_DEVICE_ID:
1940967dd82fSFlorian Fainelli 		case BCM53019_DEVICE_ID:
1941967dd82fSFlorian Fainelli 			dev->chip_id = id32;
1942967dd82fSFlorian Fainelli 			break;
1943967dd82fSFlorian Fainelli 		default:
1944967dd82fSFlorian Fainelli 			pr_err("unsupported switch detected (BCM53%02x/BCM%x)\n",
1945967dd82fSFlorian Fainelli 			       id8, id32);
1946967dd82fSFlorian Fainelli 			return -ENODEV;
1947967dd82fSFlorian Fainelli 		}
1948967dd82fSFlorian Fainelli 	}
1949967dd82fSFlorian Fainelli 
1950967dd82fSFlorian Fainelli 	if (dev->chip_id == BCM5325_DEVICE_ID)
1951967dd82fSFlorian Fainelli 		return b53_read8(dev, B53_STAT_PAGE, B53_REV_ID_25,
1952967dd82fSFlorian Fainelli 				 &dev->core_rev);
1953967dd82fSFlorian Fainelli 	else
1954967dd82fSFlorian Fainelli 		return b53_read8(dev, B53_MGMT_PAGE, B53_REV_ID,
1955967dd82fSFlorian Fainelli 				 &dev->core_rev);
1956967dd82fSFlorian Fainelli }
1957967dd82fSFlorian Fainelli EXPORT_SYMBOL(b53_switch_detect);
1958967dd82fSFlorian Fainelli 
1959967dd82fSFlorian Fainelli int b53_switch_register(struct b53_device *dev)
1960967dd82fSFlorian Fainelli {
1961967dd82fSFlorian Fainelli 	int ret;
1962967dd82fSFlorian Fainelli 
1963967dd82fSFlorian Fainelli 	if (dev->pdata) {
1964967dd82fSFlorian Fainelli 		dev->chip_id = dev->pdata->chip_id;
1965967dd82fSFlorian Fainelli 		dev->enabled_ports = dev->pdata->enabled_ports;
1966967dd82fSFlorian Fainelli 	}
1967967dd82fSFlorian Fainelli 
1968967dd82fSFlorian Fainelli 	if (!dev->chip_id && b53_switch_detect(dev))
1969967dd82fSFlorian Fainelli 		return -EINVAL;
1970967dd82fSFlorian Fainelli 
1971967dd82fSFlorian Fainelli 	ret = b53_switch_init(dev);
1972967dd82fSFlorian Fainelli 	if (ret)
1973967dd82fSFlorian Fainelli 		return ret;
1974967dd82fSFlorian Fainelli 
1975967dd82fSFlorian Fainelli 	pr_info("found switch: %s, rev %i\n", dev->name, dev->core_rev);
1976967dd82fSFlorian Fainelli 
197723c9ee49SVivien Didelot 	return dsa_register_switch(dev->ds);
1978967dd82fSFlorian Fainelli }
1979967dd82fSFlorian Fainelli EXPORT_SYMBOL(b53_switch_register);
1980967dd82fSFlorian Fainelli 
1981967dd82fSFlorian Fainelli MODULE_AUTHOR("Jonas Gorski <jogo@openwrt.org>");
1982967dd82fSFlorian Fainelli MODULE_DESCRIPTION("B53 switch library");
1983967dd82fSFlorian Fainelli MODULE_LICENSE("Dual BSD/GPL");
1984