xref: /openbmc/linux/drivers/net/dsa/b53/b53_common.c (revision df561f6688fef775baa341a0f5d960becd248b11)
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>
295e004460SFlorian Fainelli #include <linux/phylink.h>
301da6df85SFlorian Fainelli #include <linux/etherdevice.h>
31ff39c2d6SFlorian Fainelli #include <linux/if_bridge.h>
32967dd82fSFlorian Fainelli #include <net/dsa.h>
33967dd82fSFlorian Fainelli 
34967dd82fSFlorian Fainelli #include "b53_regs.h"
35967dd82fSFlorian Fainelli #include "b53_priv.h"
36967dd82fSFlorian Fainelli 
37967dd82fSFlorian Fainelli struct b53_mib_desc {
38967dd82fSFlorian Fainelli 	u8 size;
39967dd82fSFlorian Fainelli 	u8 offset;
40967dd82fSFlorian Fainelli 	const char *name;
41967dd82fSFlorian Fainelli };
42967dd82fSFlorian Fainelli 
43967dd82fSFlorian Fainelli /* BCM5365 MIB counters */
44967dd82fSFlorian Fainelli static const struct b53_mib_desc b53_mibs_65[] = {
45967dd82fSFlorian Fainelli 	{ 8, 0x00, "TxOctets" },
46967dd82fSFlorian Fainelli 	{ 4, 0x08, "TxDropPkts" },
47967dd82fSFlorian Fainelli 	{ 4, 0x10, "TxBroadcastPkts" },
48967dd82fSFlorian Fainelli 	{ 4, 0x14, "TxMulticastPkts" },
49967dd82fSFlorian Fainelli 	{ 4, 0x18, "TxUnicastPkts" },
50967dd82fSFlorian Fainelli 	{ 4, 0x1c, "TxCollisions" },
51967dd82fSFlorian Fainelli 	{ 4, 0x20, "TxSingleCollision" },
52967dd82fSFlorian Fainelli 	{ 4, 0x24, "TxMultipleCollision" },
53967dd82fSFlorian Fainelli 	{ 4, 0x28, "TxDeferredTransmit" },
54967dd82fSFlorian Fainelli 	{ 4, 0x2c, "TxLateCollision" },
55967dd82fSFlorian Fainelli 	{ 4, 0x30, "TxExcessiveCollision" },
56967dd82fSFlorian Fainelli 	{ 4, 0x38, "TxPausePkts" },
57967dd82fSFlorian Fainelli 	{ 8, 0x44, "RxOctets" },
58967dd82fSFlorian Fainelli 	{ 4, 0x4c, "RxUndersizePkts" },
59967dd82fSFlorian Fainelli 	{ 4, 0x50, "RxPausePkts" },
60967dd82fSFlorian Fainelli 	{ 4, 0x54, "Pkts64Octets" },
61967dd82fSFlorian Fainelli 	{ 4, 0x58, "Pkts65to127Octets" },
62967dd82fSFlorian Fainelli 	{ 4, 0x5c, "Pkts128to255Octets" },
63967dd82fSFlorian Fainelli 	{ 4, 0x60, "Pkts256to511Octets" },
64967dd82fSFlorian Fainelli 	{ 4, 0x64, "Pkts512to1023Octets" },
65967dd82fSFlorian Fainelli 	{ 4, 0x68, "Pkts1024to1522Octets" },
66967dd82fSFlorian Fainelli 	{ 4, 0x6c, "RxOversizePkts" },
67967dd82fSFlorian Fainelli 	{ 4, 0x70, "RxJabbers" },
68967dd82fSFlorian Fainelli 	{ 4, 0x74, "RxAlignmentErrors" },
69967dd82fSFlorian Fainelli 	{ 4, 0x78, "RxFCSErrors" },
70967dd82fSFlorian Fainelli 	{ 8, 0x7c, "RxGoodOctets" },
71967dd82fSFlorian Fainelli 	{ 4, 0x84, "RxDropPkts" },
72967dd82fSFlorian Fainelli 	{ 4, 0x88, "RxUnicastPkts" },
73967dd82fSFlorian Fainelli 	{ 4, 0x8c, "RxMulticastPkts" },
74967dd82fSFlorian Fainelli 	{ 4, 0x90, "RxBroadcastPkts" },
75967dd82fSFlorian Fainelli 	{ 4, 0x94, "RxSAChanges" },
76967dd82fSFlorian Fainelli 	{ 4, 0x98, "RxFragments" },
77967dd82fSFlorian Fainelli };
78967dd82fSFlorian Fainelli 
79967dd82fSFlorian Fainelli #define B53_MIBS_65_SIZE	ARRAY_SIZE(b53_mibs_65)
80967dd82fSFlorian Fainelli 
81967dd82fSFlorian Fainelli /* BCM63xx MIB counters */
82967dd82fSFlorian Fainelli static const struct b53_mib_desc b53_mibs_63xx[] = {
83967dd82fSFlorian Fainelli 	{ 8, 0x00, "TxOctets" },
84967dd82fSFlorian Fainelli 	{ 4, 0x08, "TxDropPkts" },
85967dd82fSFlorian Fainelli 	{ 4, 0x0c, "TxQoSPkts" },
86967dd82fSFlorian Fainelli 	{ 4, 0x10, "TxBroadcastPkts" },
87967dd82fSFlorian Fainelli 	{ 4, 0x14, "TxMulticastPkts" },
88967dd82fSFlorian Fainelli 	{ 4, 0x18, "TxUnicastPkts" },
89967dd82fSFlorian Fainelli 	{ 4, 0x1c, "TxCollisions" },
90967dd82fSFlorian Fainelli 	{ 4, 0x20, "TxSingleCollision" },
91967dd82fSFlorian Fainelli 	{ 4, 0x24, "TxMultipleCollision" },
92967dd82fSFlorian Fainelli 	{ 4, 0x28, "TxDeferredTransmit" },
93967dd82fSFlorian Fainelli 	{ 4, 0x2c, "TxLateCollision" },
94967dd82fSFlorian Fainelli 	{ 4, 0x30, "TxExcessiveCollision" },
95967dd82fSFlorian Fainelli 	{ 4, 0x38, "TxPausePkts" },
96967dd82fSFlorian Fainelli 	{ 8, 0x3c, "TxQoSOctets" },
97967dd82fSFlorian Fainelli 	{ 8, 0x44, "RxOctets" },
98967dd82fSFlorian Fainelli 	{ 4, 0x4c, "RxUndersizePkts" },
99967dd82fSFlorian Fainelli 	{ 4, 0x50, "RxPausePkts" },
100967dd82fSFlorian Fainelli 	{ 4, 0x54, "Pkts64Octets" },
101967dd82fSFlorian Fainelli 	{ 4, 0x58, "Pkts65to127Octets" },
102967dd82fSFlorian Fainelli 	{ 4, 0x5c, "Pkts128to255Octets" },
103967dd82fSFlorian Fainelli 	{ 4, 0x60, "Pkts256to511Octets" },
104967dd82fSFlorian Fainelli 	{ 4, 0x64, "Pkts512to1023Octets" },
105967dd82fSFlorian Fainelli 	{ 4, 0x68, "Pkts1024to1522Octets" },
106967dd82fSFlorian Fainelli 	{ 4, 0x6c, "RxOversizePkts" },
107967dd82fSFlorian Fainelli 	{ 4, 0x70, "RxJabbers" },
108967dd82fSFlorian Fainelli 	{ 4, 0x74, "RxAlignmentErrors" },
109967dd82fSFlorian Fainelli 	{ 4, 0x78, "RxFCSErrors" },
110967dd82fSFlorian Fainelli 	{ 8, 0x7c, "RxGoodOctets" },
111967dd82fSFlorian Fainelli 	{ 4, 0x84, "RxDropPkts" },
112967dd82fSFlorian Fainelli 	{ 4, 0x88, "RxUnicastPkts" },
113967dd82fSFlorian Fainelli 	{ 4, 0x8c, "RxMulticastPkts" },
114967dd82fSFlorian Fainelli 	{ 4, 0x90, "RxBroadcastPkts" },
115967dd82fSFlorian Fainelli 	{ 4, 0x94, "RxSAChanges" },
116967dd82fSFlorian Fainelli 	{ 4, 0x98, "RxFragments" },
117967dd82fSFlorian Fainelli 	{ 4, 0xa0, "RxSymbolErrors" },
118967dd82fSFlorian Fainelli 	{ 4, 0xa4, "RxQoSPkts" },
119967dd82fSFlorian Fainelli 	{ 8, 0xa8, "RxQoSOctets" },
120967dd82fSFlorian Fainelli 	{ 4, 0xb0, "Pkts1523to2047Octets" },
121967dd82fSFlorian Fainelli 	{ 4, 0xb4, "Pkts2048to4095Octets" },
122967dd82fSFlorian Fainelli 	{ 4, 0xb8, "Pkts4096to8191Octets" },
123967dd82fSFlorian Fainelli 	{ 4, 0xbc, "Pkts8192to9728Octets" },
124967dd82fSFlorian Fainelli 	{ 4, 0xc0, "RxDiscarded" },
125967dd82fSFlorian Fainelli };
126967dd82fSFlorian Fainelli 
127967dd82fSFlorian Fainelli #define B53_MIBS_63XX_SIZE	ARRAY_SIZE(b53_mibs_63xx)
128967dd82fSFlorian Fainelli 
129967dd82fSFlorian Fainelli /* MIB counters */
130967dd82fSFlorian Fainelli static const struct b53_mib_desc b53_mibs[] = {
131967dd82fSFlorian Fainelli 	{ 8, 0x00, "TxOctets" },
132967dd82fSFlorian Fainelli 	{ 4, 0x08, "TxDropPkts" },
133967dd82fSFlorian Fainelli 	{ 4, 0x10, "TxBroadcastPkts" },
134967dd82fSFlorian Fainelli 	{ 4, 0x14, "TxMulticastPkts" },
135967dd82fSFlorian Fainelli 	{ 4, 0x18, "TxUnicastPkts" },
136967dd82fSFlorian Fainelli 	{ 4, 0x1c, "TxCollisions" },
137967dd82fSFlorian Fainelli 	{ 4, 0x20, "TxSingleCollision" },
138967dd82fSFlorian Fainelli 	{ 4, 0x24, "TxMultipleCollision" },
139967dd82fSFlorian Fainelli 	{ 4, 0x28, "TxDeferredTransmit" },
140967dd82fSFlorian Fainelli 	{ 4, 0x2c, "TxLateCollision" },
141967dd82fSFlorian Fainelli 	{ 4, 0x30, "TxExcessiveCollision" },
142967dd82fSFlorian Fainelli 	{ 4, 0x38, "TxPausePkts" },
143967dd82fSFlorian Fainelli 	{ 8, 0x50, "RxOctets" },
144967dd82fSFlorian Fainelli 	{ 4, 0x58, "RxUndersizePkts" },
145967dd82fSFlorian Fainelli 	{ 4, 0x5c, "RxPausePkts" },
146967dd82fSFlorian Fainelli 	{ 4, 0x60, "Pkts64Octets" },
147967dd82fSFlorian Fainelli 	{ 4, 0x64, "Pkts65to127Octets" },
148967dd82fSFlorian Fainelli 	{ 4, 0x68, "Pkts128to255Octets" },
149967dd82fSFlorian Fainelli 	{ 4, 0x6c, "Pkts256to511Octets" },
150967dd82fSFlorian Fainelli 	{ 4, 0x70, "Pkts512to1023Octets" },
151967dd82fSFlorian Fainelli 	{ 4, 0x74, "Pkts1024to1522Octets" },
152967dd82fSFlorian Fainelli 	{ 4, 0x78, "RxOversizePkts" },
153967dd82fSFlorian Fainelli 	{ 4, 0x7c, "RxJabbers" },
154967dd82fSFlorian Fainelli 	{ 4, 0x80, "RxAlignmentErrors" },
155967dd82fSFlorian Fainelli 	{ 4, 0x84, "RxFCSErrors" },
156967dd82fSFlorian Fainelli 	{ 8, 0x88, "RxGoodOctets" },
157967dd82fSFlorian Fainelli 	{ 4, 0x90, "RxDropPkts" },
158967dd82fSFlorian Fainelli 	{ 4, 0x94, "RxUnicastPkts" },
159967dd82fSFlorian Fainelli 	{ 4, 0x98, "RxMulticastPkts" },
160967dd82fSFlorian Fainelli 	{ 4, 0x9c, "RxBroadcastPkts" },
161967dd82fSFlorian Fainelli 	{ 4, 0xa0, "RxSAChanges" },
162967dd82fSFlorian Fainelli 	{ 4, 0xa4, "RxFragments" },
163967dd82fSFlorian Fainelli 	{ 4, 0xa8, "RxJumboPkts" },
164967dd82fSFlorian Fainelli 	{ 4, 0xac, "RxSymbolErrors" },
165967dd82fSFlorian Fainelli 	{ 4, 0xc0, "RxDiscarded" },
166967dd82fSFlorian Fainelli };
167967dd82fSFlorian Fainelli 
168967dd82fSFlorian Fainelli #define B53_MIBS_SIZE	ARRAY_SIZE(b53_mibs)
169967dd82fSFlorian Fainelli 
170bde5d132SFlorian Fainelli static const struct b53_mib_desc b53_mibs_58xx[] = {
171bde5d132SFlorian Fainelli 	{ 8, 0x00, "TxOctets" },
172bde5d132SFlorian Fainelli 	{ 4, 0x08, "TxDropPkts" },
173bde5d132SFlorian Fainelli 	{ 4, 0x0c, "TxQPKTQ0" },
174bde5d132SFlorian Fainelli 	{ 4, 0x10, "TxBroadcastPkts" },
175bde5d132SFlorian Fainelli 	{ 4, 0x14, "TxMulticastPkts" },
176bde5d132SFlorian Fainelli 	{ 4, 0x18, "TxUnicastPKts" },
177bde5d132SFlorian Fainelli 	{ 4, 0x1c, "TxCollisions" },
178bde5d132SFlorian Fainelli 	{ 4, 0x20, "TxSingleCollision" },
179bde5d132SFlorian Fainelli 	{ 4, 0x24, "TxMultipleCollision" },
180bde5d132SFlorian Fainelli 	{ 4, 0x28, "TxDeferredCollision" },
181bde5d132SFlorian Fainelli 	{ 4, 0x2c, "TxLateCollision" },
182bde5d132SFlorian Fainelli 	{ 4, 0x30, "TxExcessiveCollision" },
183bde5d132SFlorian Fainelli 	{ 4, 0x34, "TxFrameInDisc" },
184bde5d132SFlorian Fainelli 	{ 4, 0x38, "TxPausePkts" },
185bde5d132SFlorian Fainelli 	{ 4, 0x3c, "TxQPKTQ1" },
186bde5d132SFlorian Fainelli 	{ 4, 0x40, "TxQPKTQ2" },
187bde5d132SFlorian Fainelli 	{ 4, 0x44, "TxQPKTQ3" },
188bde5d132SFlorian Fainelli 	{ 4, 0x48, "TxQPKTQ4" },
189bde5d132SFlorian Fainelli 	{ 4, 0x4c, "TxQPKTQ5" },
190bde5d132SFlorian Fainelli 	{ 8, 0x50, "RxOctets" },
191bde5d132SFlorian Fainelli 	{ 4, 0x58, "RxUndersizePkts" },
192bde5d132SFlorian Fainelli 	{ 4, 0x5c, "RxPausePkts" },
193bde5d132SFlorian Fainelli 	{ 4, 0x60, "RxPkts64Octets" },
194bde5d132SFlorian Fainelli 	{ 4, 0x64, "RxPkts65to127Octets" },
195bde5d132SFlorian Fainelli 	{ 4, 0x68, "RxPkts128to255Octets" },
196bde5d132SFlorian Fainelli 	{ 4, 0x6c, "RxPkts256to511Octets" },
197bde5d132SFlorian Fainelli 	{ 4, 0x70, "RxPkts512to1023Octets" },
198bde5d132SFlorian Fainelli 	{ 4, 0x74, "RxPkts1024toMaxPktsOctets" },
199bde5d132SFlorian Fainelli 	{ 4, 0x78, "RxOversizePkts" },
200bde5d132SFlorian Fainelli 	{ 4, 0x7c, "RxJabbers" },
201bde5d132SFlorian Fainelli 	{ 4, 0x80, "RxAlignmentErrors" },
202bde5d132SFlorian Fainelli 	{ 4, 0x84, "RxFCSErrors" },
203bde5d132SFlorian Fainelli 	{ 8, 0x88, "RxGoodOctets" },
204bde5d132SFlorian Fainelli 	{ 4, 0x90, "RxDropPkts" },
205bde5d132SFlorian Fainelli 	{ 4, 0x94, "RxUnicastPkts" },
206bde5d132SFlorian Fainelli 	{ 4, 0x98, "RxMulticastPkts" },
207bde5d132SFlorian Fainelli 	{ 4, 0x9c, "RxBroadcastPkts" },
208bde5d132SFlorian Fainelli 	{ 4, 0xa0, "RxSAChanges" },
209bde5d132SFlorian Fainelli 	{ 4, 0xa4, "RxFragments" },
210bde5d132SFlorian Fainelli 	{ 4, 0xa8, "RxJumboPkt" },
211bde5d132SFlorian Fainelli 	{ 4, 0xac, "RxSymblErr" },
212bde5d132SFlorian Fainelli 	{ 4, 0xb0, "InRangeErrCount" },
213bde5d132SFlorian Fainelli 	{ 4, 0xb4, "OutRangeErrCount" },
214bde5d132SFlorian Fainelli 	{ 4, 0xb8, "EEELpiEvent" },
215bde5d132SFlorian Fainelli 	{ 4, 0xbc, "EEELpiDuration" },
216bde5d132SFlorian Fainelli 	{ 4, 0xc0, "RxDiscard" },
217bde5d132SFlorian Fainelli 	{ 4, 0xc8, "TxQPKTQ6" },
218bde5d132SFlorian Fainelli 	{ 4, 0xcc, "TxQPKTQ7" },
219bde5d132SFlorian Fainelli 	{ 4, 0xd0, "TxPkts64Octets" },
220bde5d132SFlorian Fainelli 	{ 4, 0xd4, "TxPkts65to127Octets" },
221bde5d132SFlorian Fainelli 	{ 4, 0xd8, "TxPkts128to255Octets" },
222bde5d132SFlorian Fainelli 	{ 4, 0xdc, "TxPkts256to511Ocets" },
223bde5d132SFlorian Fainelli 	{ 4, 0xe0, "TxPkts512to1023Ocets" },
224bde5d132SFlorian Fainelli 	{ 4, 0xe4, "TxPkts1024toMaxPktOcets" },
225bde5d132SFlorian Fainelli };
226bde5d132SFlorian Fainelli 
227bde5d132SFlorian Fainelli #define B53_MIBS_58XX_SIZE	ARRAY_SIZE(b53_mibs_58xx)
228bde5d132SFlorian Fainelli 
229967dd82fSFlorian Fainelli static int b53_do_vlan_op(struct b53_device *dev, u8 op)
230967dd82fSFlorian Fainelli {
231967dd82fSFlorian Fainelli 	unsigned int i;
232967dd82fSFlorian Fainelli 
233967dd82fSFlorian Fainelli 	b53_write8(dev, B53_ARLIO_PAGE, dev->vta_regs[0], VTA_START_CMD | op);
234967dd82fSFlorian Fainelli 
235967dd82fSFlorian Fainelli 	for (i = 0; i < 10; i++) {
236967dd82fSFlorian Fainelli 		u8 vta;
237967dd82fSFlorian Fainelli 
238967dd82fSFlorian Fainelli 		b53_read8(dev, B53_ARLIO_PAGE, dev->vta_regs[0], &vta);
239967dd82fSFlorian Fainelli 		if (!(vta & VTA_START_CMD))
240967dd82fSFlorian Fainelli 			return 0;
241967dd82fSFlorian Fainelli 
242967dd82fSFlorian Fainelli 		usleep_range(100, 200);
243967dd82fSFlorian Fainelli 	}
244967dd82fSFlorian Fainelli 
245967dd82fSFlorian Fainelli 	return -EIO;
246967dd82fSFlorian Fainelli }
247967dd82fSFlorian Fainelli 
248a2482d2cSFlorian Fainelli static void b53_set_vlan_entry(struct b53_device *dev, u16 vid,
249a2482d2cSFlorian Fainelli 			       struct b53_vlan *vlan)
250967dd82fSFlorian Fainelli {
251967dd82fSFlorian Fainelli 	if (is5325(dev)) {
252967dd82fSFlorian Fainelli 		u32 entry = 0;
253967dd82fSFlorian Fainelli 
254a2482d2cSFlorian Fainelli 		if (vlan->members) {
255a2482d2cSFlorian Fainelli 			entry = ((vlan->untag & VA_UNTAG_MASK_25) <<
256a2482d2cSFlorian Fainelli 				 VA_UNTAG_S_25) | vlan->members;
257967dd82fSFlorian Fainelli 			if (dev->core_rev >= 3)
258967dd82fSFlorian Fainelli 				entry |= VA_VALID_25_R4 | vid << VA_VID_HIGH_S;
259967dd82fSFlorian Fainelli 			else
260967dd82fSFlorian Fainelli 				entry |= VA_VALID_25;
261967dd82fSFlorian Fainelli 		}
262967dd82fSFlorian Fainelli 
263967dd82fSFlorian Fainelli 		b53_write32(dev, B53_VLAN_PAGE, B53_VLAN_WRITE_25, entry);
264967dd82fSFlorian Fainelli 		b53_write16(dev, B53_VLAN_PAGE, B53_VLAN_TABLE_ACCESS_25, vid |
265967dd82fSFlorian Fainelli 			    VTA_RW_STATE_WR | VTA_RW_OP_EN);
266967dd82fSFlorian Fainelli 	} else if (is5365(dev)) {
267967dd82fSFlorian Fainelli 		u16 entry = 0;
268967dd82fSFlorian Fainelli 
269a2482d2cSFlorian Fainelli 		if (vlan->members)
270a2482d2cSFlorian Fainelli 			entry = ((vlan->untag & VA_UNTAG_MASK_65) <<
271a2482d2cSFlorian Fainelli 				 VA_UNTAG_S_65) | vlan->members | VA_VALID_65;
272967dd82fSFlorian Fainelli 
273967dd82fSFlorian Fainelli 		b53_write16(dev, B53_VLAN_PAGE, B53_VLAN_WRITE_65, entry);
274967dd82fSFlorian Fainelli 		b53_write16(dev, B53_VLAN_PAGE, B53_VLAN_TABLE_ACCESS_65, vid |
275967dd82fSFlorian Fainelli 			    VTA_RW_STATE_WR | VTA_RW_OP_EN);
276967dd82fSFlorian Fainelli 	} else {
277967dd82fSFlorian Fainelli 		b53_write16(dev, B53_ARLIO_PAGE, dev->vta_regs[1], vid);
278967dd82fSFlorian Fainelli 		b53_write32(dev, B53_ARLIO_PAGE, dev->vta_regs[2],
279a2482d2cSFlorian Fainelli 			    (vlan->untag << VTE_UNTAG_S) | vlan->members);
280967dd82fSFlorian Fainelli 
281967dd82fSFlorian Fainelli 		b53_do_vlan_op(dev, VTA_CMD_WRITE);
282967dd82fSFlorian Fainelli 	}
283a2482d2cSFlorian Fainelli 
284a2482d2cSFlorian Fainelli 	dev_dbg(dev->ds->dev, "VID: %d, members: 0x%04x, untag: 0x%04x\n",
285a2482d2cSFlorian Fainelli 		vid, vlan->members, vlan->untag);
286967dd82fSFlorian Fainelli }
287967dd82fSFlorian Fainelli 
288a2482d2cSFlorian Fainelli static void b53_get_vlan_entry(struct b53_device *dev, u16 vid,
289a2482d2cSFlorian Fainelli 			       struct b53_vlan *vlan)
290a2482d2cSFlorian Fainelli {
291a2482d2cSFlorian Fainelli 	if (is5325(dev)) {
292a2482d2cSFlorian Fainelli 		u32 entry = 0;
293a2482d2cSFlorian Fainelli 
294a2482d2cSFlorian Fainelli 		b53_write16(dev, B53_VLAN_PAGE, B53_VLAN_TABLE_ACCESS_25, vid |
295a2482d2cSFlorian Fainelli 			    VTA_RW_STATE_RD | VTA_RW_OP_EN);
296a2482d2cSFlorian Fainelli 		b53_read32(dev, B53_VLAN_PAGE, B53_VLAN_WRITE_25, &entry);
297a2482d2cSFlorian Fainelli 
298a2482d2cSFlorian Fainelli 		if (dev->core_rev >= 3)
299a2482d2cSFlorian Fainelli 			vlan->valid = !!(entry & VA_VALID_25_R4);
300a2482d2cSFlorian Fainelli 		else
301a2482d2cSFlorian Fainelli 			vlan->valid = !!(entry & VA_VALID_25);
302a2482d2cSFlorian Fainelli 		vlan->members = entry & VA_MEMBER_MASK;
303a2482d2cSFlorian Fainelli 		vlan->untag = (entry >> VA_UNTAG_S_25) & VA_UNTAG_MASK_25;
304a2482d2cSFlorian Fainelli 
305a2482d2cSFlorian Fainelli 	} else if (is5365(dev)) {
306a2482d2cSFlorian Fainelli 		u16 entry = 0;
307a2482d2cSFlorian Fainelli 
308a2482d2cSFlorian Fainelli 		b53_write16(dev, B53_VLAN_PAGE, B53_VLAN_TABLE_ACCESS_65, vid |
309a2482d2cSFlorian Fainelli 			    VTA_RW_STATE_WR | VTA_RW_OP_EN);
310a2482d2cSFlorian Fainelli 		b53_read16(dev, B53_VLAN_PAGE, B53_VLAN_WRITE_65, &entry);
311a2482d2cSFlorian Fainelli 
312a2482d2cSFlorian Fainelli 		vlan->valid = !!(entry & VA_VALID_65);
313a2482d2cSFlorian Fainelli 		vlan->members = entry & VA_MEMBER_MASK;
314a2482d2cSFlorian Fainelli 		vlan->untag = (entry >> VA_UNTAG_S_65) & VA_UNTAG_MASK_65;
315a2482d2cSFlorian Fainelli 	} else {
316a2482d2cSFlorian Fainelli 		u32 entry = 0;
317a2482d2cSFlorian Fainelli 
318a2482d2cSFlorian Fainelli 		b53_write16(dev, B53_ARLIO_PAGE, dev->vta_regs[1], vid);
319a2482d2cSFlorian Fainelli 		b53_do_vlan_op(dev, VTA_CMD_READ);
320a2482d2cSFlorian Fainelli 		b53_read32(dev, B53_ARLIO_PAGE, dev->vta_regs[2], &entry);
321a2482d2cSFlorian Fainelli 		vlan->members = entry & VTE_MEMBERS;
322a2482d2cSFlorian Fainelli 		vlan->untag = (entry >> VTE_UNTAG_S) & VTE_MEMBERS;
323a2482d2cSFlorian Fainelli 		vlan->valid = true;
324a2482d2cSFlorian Fainelli 	}
325a2482d2cSFlorian Fainelli }
326a2482d2cSFlorian Fainelli 
327a2482d2cSFlorian Fainelli static void b53_set_forwarding(struct b53_device *dev, int enable)
328967dd82fSFlorian Fainelli {
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 
3407edc58d6SFlorian Fainelli 	/* Include IMP port in dumb forwarding mode
341a424f0deSFlorian Fainelli 	 */
342a424f0deSFlorian Fainelli 	b53_read8(dev, B53_CTRL_PAGE, B53_SWITCH_CTRL, &mgmt);
343a424f0deSFlorian Fainelli 	mgmt |= B53_MII_DUMB_FWDG_EN;
344a424f0deSFlorian Fainelli 	b53_write8(dev, B53_CTRL_PAGE, B53_SWITCH_CTRL, mgmt);
34553568438SFlorian Fainelli 
34653568438SFlorian Fainelli 	/* Look at B53_UC_FWD_EN and B53_MC_FWD_EN to decide whether
34753568438SFlorian Fainelli 	 * frames should be flooded or not.
34853568438SFlorian Fainelli 	 */
34953568438SFlorian Fainelli 	b53_read8(dev, B53_CTRL_PAGE, B53_IP_MULTICAST_CTRL, &mgmt);
35063cc54a6SFlorian Fainelli 	mgmt |= B53_UC_FWD_EN | B53_MC_FWD_EN | B53_IPMC_FWD_EN;
35153568438SFlorian Fainelli 	b53_write8(dev, B53_CTRL_PAGE, B53_IP_MULTICAST_CTRL, mgmt);
352a424f0deSFlorian Fainelli }
353967dd82fSFlorian Fainelli 
354dad8d7c6SFlorian Fainelli static void b53_enable_vlan(struct b53_device *dev, bool enable,
355dad8d7c6SFlorian Fainelli 			    bool enable_filtering)
356967dd82fSFlorian Fainelli {
357967dd82fSFlorian Fainelli 	u8 mgmt, vc0, vc1, vc4 = 0, vc5;
358967dd82fSFlorian Fainelli 
359967dd82fSFlorian Fainelli 	b53_read8(dev, B53_CTRL_PAGE, B53_SWITCH_MODE, &mgmt);
360967dd82fSFlorian Fainelli 	b53_read8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL0, &vc0);
361967dd82fSFlorian Fainelli 	b53_read8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL1, &vc1);
362967dd82fSFlorian Fainelli 
363967dd82fSFlorian Fainelli 	if (is5325(dev) || is5365(dev)) {
364967dd82fSFlorian Fainelli 		b53_read8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL4_25, &vc4);
365967dd82fSFlorian Fainelli 		b53_read8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL5_25, &vc5);
366967dd82fSFlorian Fainelli 	} else if (is63xx(dev)) {
367967dd82fSFlorian Fainelli 		b53_read8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL4_63XX, &vc4);
368967dd82fSFlorian Fainelli 		b53_read8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL5_63XX, &vc5);
369967dd82fSFlorian Fainelli 	} else {
370967dd82fSFlorian Fainelli 		b53_read8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL4, &vc4);
371967dd82fSFlorian Fainelli 		b53_read8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL5, &vc5);
372967dd82fSFlorian Fainelli 	}
373967dd82fSFlorian Fainelli 
374967dd82fSFlorian Fainelli 	if (enable) {
375967dd82fSFlorian Fainelli 		vc0 |= VC0_VLAN_EN | VC0_VID_CHK_EN | VC0_VID_HASH_VID;
376967dd82fSFlorian Fainelli 		vc1 |= VC1_RX_MCST_UNTAG_EN | VC1_RX_MCST_FWD_EN;
377967dd82fSFlorian Fainelli 		vc4 &= ~VC4_ING_VID_CHECK_MASK;
378dad8d7c6SFlorian Fainelli 		if (enable_filtering) {
379967dd82fSFlorian Fainelli 			vc4 |= VC4_ING_VID_VIO_DROP << VC4_ING_VID_CHECK_S;
380967dd82fSFlorian Fainelli 			vc5 |= VC5_DROP_VTABLE_MISS;
381dad8d7c6SFlorian Fainelli 		} else {
382dad8d7c6SFlorian Fainelli 			vc4 |= VC4_ING_VID_VIO_FWD << VC4_ING_VID_CHECK_S;
383dad8d7c6SFlorian Fainelli 			vc5 &= ~VC5_DROP_VTABLE_MISS;
384dad8d7c6SFlorian Fainelli 		}
385967dd82fSFlorian Fainelli 
386967dd82fSFlorian Fainelli 		if (is5325(dev))
387967dd82fSFlorian Fainelli 			vc0 &= ~VC0_RESERVED_1;
388967dd82fSFlorian Fainelli 
389967dd82fSFlorian Fainelli 		if (is5325(dev) || is5365(dev))
390967dd82fSFlorian Fainelli 			vc1 |= VC1_RX_MCST_TAG_EN;
391967dd82fSFlorian Fainelli 
392967dd82fSFlorian Fainelli 	} else {
393967dd82fSFlorian Fainelli 		vc0 &= ~(VC0_VLAN_EN | VC0_VID_CHK_EN | VC0_VID_HASH_VID);
394967dd82fSFlorian Fainelli 		vc1 &= ~(VC1_RX_MCST_UNTAG_EN | VC1_RX_MCST_FWD_EN);
395967dd82fSFlorian Fainelli 		vc4 &= ~VC4_ING_VID_CHECK_MASK;
396967dd82fSFlorian Fainelli 		vc5 &= ~VC5_DROP_VTABLE_MISS;
397967dd82fSFlorian Fainelli 
398967dd82fSFlorian Fainelli 		if (is5325(dev) || is5365(dev))
399967dd82fSFlorian Fainelli 			vc4 |= VC4_ING_VID_VIO_FWD << VC4_ING_VID_CHECK_S;
400967dd82fSFlorian Fainelli 		else
401967dd82fSFlorian Fainelli 			vc4 |= VC4_ING_VID_VIO_TO_IMP << VC4_ING_VID_CHECK_S;
402967dd82fSFlorian Fainelli 
403967dd82fSFlorian Fainelli 		if (is5325(dev) || is5365(dev))
404967dd82fSFlorian Fainelli 			vc1 &= ~VC1_RX_MCST_TAG_EN;
405a2482d2cSFlorian Fainelli 	}
406967dd82fSFlorian Fainelli 
407967dd82fSFlorian Fainelli 	if (!is5325(dev) && !is5365(dev))
408967dd82fSFlorian Fainelli 		vc5 &= ~VC5_VID_FFF_EN;
409967dd82fSFlorian Fainelli 
410967dd82fSFlorian Fainelli 	b53_write8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL0, vc0);
411967dd82fSFlorian Fainelli 	b53_write8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL1, vc1);
412967dd82fSFlorian Fainelli 
413967dd82fSFlorian Fainelli 	if (is5325(dev) || is5365(dev)) {
414967dd82fSFlorian Fainelli 		/* enable the high 8 bit vid check on 5325 */
415967dd82fSFlorian Fainelli 		if (is5325(dev) && enable)
416967dd82fSFlorian Fainelli 			b53_write8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL3,
417967dd82fSFlorian Fainelli 				   VC3_HIGH_8BIT_EN);
418967dd82fSFlorian Fainelli 		else
419967dd82fSFlorian Fainelli 			b53_write8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL3, 0);
420967dd82fSFlorian Fainelli 
421967dd82fSFlorian Fainelli 		b53_write8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL4_25, vc4);
422967dd82fSFlorian Fainelli 		b53_write8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL5_25, vc5);
423967dd82fSFlorian Fainelli 	} else if (is63xx(dev)) {
424967dd82fSFlorian Fainelli 		b53_write16(dev, B53_VLAN_PAGE, B53_VLAN_CTRL3_63XX, 0);
425967dd82fSFlorian Fainelli 		b53_write8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL4_63XX, vc4);
426967dd82fSFlorian Fainelli 		b53_write8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL5_63XX, vc5);
427967dd82fSFlorian Fainelli 	} else {
428967dd82fSFlorian Fainelli 		b53_write16(dev, B53_VLAN_PAGE, B53_VLAN_CTRL3, 0);
429967dd82fSFlorian Fainelli 		b53_write8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL4, vc4);
430967dd82fSFlorian Fainelli 		b53_write8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL5, vc5);
431967dd82fSFlorian Fainelli 	}
432967dd82fSFlorian Fainelli 
433967dd82fSFlorian Fainelli 	b53_write8(dev, B53_CTRL_PAGE, B53_SWITCH_MODE, mgmt);
434dad8d7c6SFlorian Fainelli 
435dad8d7c6SFlorian Fainelli 	dev->vlan_enabled = enable;
436967dd82fSFlorian Fainelli }
437967dd82fSFlorian Fainelli 
438967dd82fSFlorian Fainelli static int b53_set_jumbo(struct b53_device *dev, bool enable, bool allow_10_100)
439967dd82fSFlorian Fainelli {
440967dd82fSFlorian Fainelli 	u32 port_mask = 0;
441967dd82fSFlorian Fainelli 	u16 max_size = JMS_MIN_SIZE;
442967dd82fSFlorian Fainelli 
443967dd82fSFlorian Fainelli 	if (is5325(dev) || is5365(dev))
444967dd82fSFlorian Fainelli 		return -EINVAL;
445967dd82fSFlorian Fainelli 
446967dd82fSFlorian Fainelli 	if (enable) {
447967dd82fSFlorian Fainelli 		port_mask = dev->enabled_ports;
448967dd82fSFlorian Fainelli 		max_size = JMS_MAX_SIZE;
449967dd82fSFlorian Fainelli 		if (allow_10_100)
450967dd82fSFlorian Fainelli 			port_mask |= JPM_10_100_JUMBO_EN;
451967dd82fSFlorian Fainelli 	}
452967dd82fSFlorian Fainelli 
453967dd82fSFlorian Fainelli 	b53_write32(dev, B53_JUMBO_PAGE, dev->jumbo_pm_reg, port_mask);
454967dd82fSFlorian Fainelli 	return b53_write16(dev, B53_JUMBO_PAGE, dev->jumbo_size_reg, max_size);
455967dd82fSFlorian Fainelli }
456967dd82fSFlorian Fainelli 
457ff39c2d6SFlorian Fainelli static int b53_flush_arl(struct b53_device *dev, u8 mask)
458967dd82fSFlorian Fainelli {
459967dd82fSFlorian Fainelli 	unsigned int i;
460967dd82fSFlorian Fainelli 
461967dd82fSFlorian Fainelli 	b53_write8(dev, B53_CTRL_PAGE, B53_FAST_AGE_CTRL,
462ff39c2d6SFlorian Fainelli 		   FAST_AGE_DONE | FAST_AGE_DYNAMIC | mask);
463967dd82fSFlorian Fainelli 
464967dd82fSFlorian Fainelli 	for (i = 0; i < 10; i++) {
465967dd82fSFlorian Fainelli 		u8 fast_age_ctrl;
466967dd82fSFlorian Fainelli 
467967dd82fSFlorian Fainelli 		b53_read8(dev, B53_CTRL_PAGE, B53_FAST_AGE_CTRL,
468967dd82fSFlorian Fainelli 			  &fast_age_ctrl);
469967dd82fSFlorian Fainelli 
470967dd82fSFlorian Fainelli 		if (!(fast_age_ctrl & FAST_AGE_DONE))
471967dd82fSFlorian Fainelli 			goto out;
472967dd82fSFlorian Fainelli 
473967dd82fSFlorian Fainelli 		msleep(1);
474967dd82fSFlorian Fainelli 	}
475967dd82fSFlorian Fainelli 
476967dd82fSFlorian Fainelli 	return -ETIMEDOUT;
477967dd82fSFlorian Fainelli out:
478967dd82fSFlorian Fainelli 	/* Only age dynamic entries (default behavior) */
479967dd82fSFlorian Fainelli 	b53_write8(dev, B53_CTRL_PAGE, B53_FAST_AGE_CTRL, FAST_AGE_DYNAMIC);
480967dd82fSFlorian Fainelli 	return 0;
481967dd82fSFlorian Fainelli }
482967dd82fSFlorian Fainelli 
483ff39c2d6SFlorian Fainelli static int b53_fast_age_port(struct b53_device *dev, int port)
484ff39c2d6SFlorian Fainelli {
485ff39c2d6SFlorian Fainelli 	b53_write8(dev, B53_CTRL_PAGE, B53_FAST_AGE_PORT_CTRL, port);
486ff39c2d6SFlorian Fainelli 
487ff39c2d6SFlorian Fainelli 	return b53_flush_arl(dev, FAST_AGE_PORT);
488ff39c2d6SFlorian Fainelli }
489ff39c2d6SFlorian Fainelli 
490a2482d2cSFlorian Fainelli static int b53_fast_age_vlan(struct b53_device *dev, u16 vid)
491a2482d2cSFlorian Fainelli {
492a2482d2cSFlorian Fainelli 	b53_write16(dev, B53_CTRL_PAGE, B53_FAST_AGE_VID_CTRL, vid);
493a2482d2cSFlorian Fainelli 
494a2482d2cSFlorian Fainelli 	return b53_flush_arl(dev, FAST_AGE_VLAN);
495a2482d2cSFlorian Fainelli }
496a2482d2cSFlorian Fainelli 
497aac02867SFlorian Fainelli void b53_imp_vlan_setup(struct dsa_switch *ds, int cpu_port)
498ff39c2d6SFlorian Fainelli {
49904bed143SVivien Didelot 	struct b53_device *dev = ds->priv;
500ff39c2d6SFlorian Fainelli 	unsigned int i;
501ff39c2d6SFlorian Fainelli 	u16 pvlan;
502ff39c2d6SFlorian Fainelli 
503ff39c2d6SFlorian Fainelli 	/* Enable the IMP port to be in the same VLAN as the other ports
504ff39c2d6SFlorian Fainelli 	 * on a per-port basis such that we only have Port i and IMP in
505ff39c2d6SFlorian Fainelli 	 * the same VLAN.
506ff39c2d6SFlorian Fainelli 	 */
507ff39c2d6SFlorian Fainelli 	b53_for_each_port(dev, i) {
508ff39c2d6SFlorian Fainelli 		b53_read16(dev, B53_PVLAN_PAGE, B53_PVLAN_PORT_MASK(i), &pvlan);
509ff39c2d6SFlorian Fainelli 		pvlan |= BIT(cpu_port);
510ff39c2d6SFlorian Fainelli 		b53_write16(dev, B53_PVLAN_PAGE, B53_PVLAN_PORT_MASK(i), pvlan);
511ff39c2d6SFlorian Fainelli 	}
512ff39c2d6SFlorian Fainelli }
513aac02867SFlorian Fainelli EXPORT_SYMBOL(b53_imp_vlan_setup);
514ff39c2d6SFlorian Fainelli 
515f86ad77fSFlorian Fainelli int b53_enable_port(struct dsa_switch *ds, int port, struct phy_device *phy)
516967dd82fSFlorian Fainelli {
51704bed143SVivien Didelot 	struct b53_device *dev = ds->priv;
51874be4babSVivien Didelot 	unsigned int cpu_port;
5198ca7c160SFlorian Fainelli 	int ret = 0;
520ff39c2d6SFlorian Fainelli 	u16 pvlan;
521967dd82fSFlorian Fainelli 
52274be4babSVivien Didelot 	if (!dsa_is_user_port(ds, port))
52374be4babSVivien Didelot 		return 0;
52474be4babSVivien Didelot 
52568bb8ea8SVivien Didelot 	cpu_port = dsa_to_port(ds, port)->cpu_dp->index;
52674be4babSVivien Didelot 
52763cc54a6SFlorian Fainelli 	b53_br_egress_floods(ds, port, true, true);
52863cc54a6SFlorian Fainelli 
5298ca7c160SFlorian Fainelli 	if (dev->ops->irq_enable)
5308ca7c160SFlorian Fainelli 		ret = dev->ops->irq_enable(dev, port);
5318ca7c160SFlorian Fainelli 	if (ret)
5328ca7c160SFlorian Fainelli 		return ret;
5338ca7c160SFlorian Fainelli 
534967dd82fSFlorian Fainelli 	/* Clear the Rx and Tx disable bits and set to no spanning tree */
535967dd82fSFlorian Fainelli 	b53_write8(dev, B53_CTRL_PAGE, B53_PORT_CTRL(port), 0);
536967dd82fSFlorian Fainelli 
537ff39c2d6SFlorian Fainelli 	/* Set this port, and only this one to be in the default VLAN,
538ff39c2d6SFlorian Fainelli 	 * if member of a bridge, restore its membership prior to
539ff39c2d6SFlorian Fainelli 	 * bringing down this port.
540ff39c2d6SFlorian Fainelli 	 */
541ff39c2d6SFlorian Fainelli 	b53_read16(dev, B53_PVLAN_PAGE, B53_PVLAN_PORT_MASK(port), &pvlan);
542ff39c2d6SFlorian Fainelli 	pvlan &= ~0x1ff;
543ff39c2d6SFlorian Fainelli 	pvlan |= BIT(port);
544ff39c2d6SFlorian Fainelli 	pvlan |= dev->ports[port].vlan_ctl_mask;
545ff39c2d6SFlorian Fainelli 	b53_write16(dev, B53_PVLAN_PAGE, B53_PVLAN_PORT_MASK(port), pvlan);
546ff39c2d6SFlorian Fainelli 
547ff39c2d6SFlorian Fainelli 	b53_imp_vlan_setup(ds, cpu_port);
548ff39c2d6SFlorian Fainelli 
549f43a2dbeSFlorian Fainelli 	/* If EEE was enabled, restore it */
550f43a2dbeSFlorian Fainelli 	if (dev->ports[port].eee.eee_enabled)
551f43a2dbeSFlorian Fainelli 		b53_eee_enable_set(ds, port, true);
552f43a2dbeSFlorian Fainelli 
553967dd82fSFlorian Fainelli 	return 0;
554967dd82fSFlorian Fainelli }
555f86ad77fSFlorian Fainelli EXPORT_SYMBOL(b53_enable_port);
556967dd82fSFlorian Fainelli 
55775104db0SAndrew Lunn void b53_disable_port(struct dsa_switch *ds, int port)
558967dd82fSFlorian Fainelli {
55904bed143SVivien Didelot 	struct b53_device *dev = ds->priv;
560967dd82fSFlorian Fainelli 	u8 reg;
561967dd82fSFlorian Fainelli 
562967dd82fSFlorian Fainelli 	/* Disable Tx/Rx for the port */
563967dd82fSFlorian Fainelli 	b53_read8(dev, B53_CTRL_PAGE, B53_PORT_CTRL(port), &reg);
564967dd82fSFlorian Fainelli 	reg |= PORT_CTRL_RX_DISABLE | PORT_CTRL_TX_DISABLE;
565967dd82fSFlorian Fainelli 	b53_write8(dev, B53_CTRL_PAGE, B53_PORT_CTRL(port), reg);
5668ca7c160SFlorian Fainelli 
5678ca7c160SFlorian Fainelli 	if (dev->ops->irq_disable)
5688ca7c160SFlorian Fainelli 		dev->ops->irq_disable(dev, port);
569967dd82fSFlorian Fainelli }
570f86ad77fSFlorian Fainelli EXPORT_SYMBOL(b53_disable_port);
571967dd82fSFlorian Fainelli 
572b409a9efSFlorian Fainelli void b53_brcm_hdr_setup(struct dsa_switch *ds, int port)
573b409a9efSFlorian Fainelli {
574b409a9efSFlorian Fainelli 	struct b53_device *dev = ds->priv;
5754d776482SFlorian Fainelli 	bool tag_en = !(dev->tag_protocol == DSA_TAG_PROTO_NONE);
576b409a9efSFlorian Fainelli 	u8 hdr_ctl, val;
577b409a9efSFlorian Fainelli 	u16 reg;
578b409a9efSFlorian Fainelli 
579b409a9efSFlorian Fainelli 	/* Resolve which bit controls the Broadcom tag */
580b409a9efSFlorian Fainelli 	switch (port) {
581b409a9efSFlorian Fainelli 	case 8:
582b409a9efSFlorian Fainelli 		val = BRCM_HDR_P8_EN;
583b409a9efSFlorian Fainelli 		break;
584b409a9efSFlorian Fainelli 	case 7:
585b409a9efSFlorian Fainelli 		val = BRCM_HDR_P7_EN;
586b409a9efSFlorian Fainelli 		break;
587b409a9efSFlorian Fainelli 	case 5:
588b409a9efSFlorian Fainelli 		val = BRCM_HDR_P5_EN;
589b409a9efSFlorian Fainelli 		break;
590b409a9efSFlorian Fainelli 	default:
591b409a9efSFlorian Fainelli 		val = 0;
592b409a9efSFlorian Fainelli 		break;
593b409a9efSFlorian Fainelli 	}
594b409a9efSFlorian Fainelli 
5958fab459eSFlorian Fainelli 	/* Enable management mode if tagging is requested */
5968fab459eSFlorian Fainelli 	b53_read8(dev, B53_CTRL_PAGE, B53_SWITCH_MODE, &hdr_ctl);
5978fab459eSFlorian Fainelli 	if (tag_en)
5988fab459eSFlorian Fainelli 		hdr_ctl |= SM_SW_FWD_MODE;
5998fab459eSFlorian Fainelli 	else
6008fab459eSFlorian Fainelli 		hdr_ctl &= ~SM_SW_FWD_MODE;
6018fab459eSFlorian Fainelli 	b53_write8(dev, B53_CTRL_PAGE, B53_SWITCH_MODE, hdr_ctl);
6028fab459eSFlorian Fainelli 
6038fab459eSFlorian Fainelli 	/* Configure the appropriate IMP port */
6048fab459eSFlorian Fainelli 	b53_read8(dev, B53_MGMT_PAGE, B53_GLOBAL_CONFIG, &hdr_ctl);
6058fab459eSFlorian Fainelli 	if (port == 8)
6068fab459eSFlorian Fainelli 		hdr_ctl |= GC_FRM_MGMT_PORT_MII;
6078fab459eSFlorian Fainelli 	else if (port == 5)
6088fab459eSFlorian Fainelli 		hdr_ctl |= GC_FRM_MGMT_PORT_M;
6098fab459eSFlorian Fainelli 	b53_write8(dev, B53_MGMT_PAGE, B53_GLOBAL_CONFIG, hdr_ctl);
6108fab459eSFlorian Fainelli 
611b409a9efSFlorian Fainelli 	/* Enable Broadcom tags for IMP port */
612b409a9efSFlorian Fainelli 	b53_read8(dev, B53_MGMT_PAGE, B53_BRCM_HDR, &hdr_ctl);
613cdb583cfSFlorian Fainelli 	if (tag_en)
614b409a9efSFlorian Fainelli 		hdr_ctl |= val;
615cdb583cfSFlorian Fainelli 	else
616cdb583cfSFlorian Fainelli 		hdr_ctl &= ~val;
617b409a9efSFlorian Fainelli 	b53_write8(dev, B53_MGMT_PAGE, B53_BRCM_HDR, hdr_ctl);
618b409a9efSFlorian Fainelli 
619b409a9efSFlorian Fainelli 	/* Registers below are only accessible on newer devices */
620b409a9efSFlorian Fainelli 	if (!is58xx(dev))
621b409a9efSFlorian Fainelli 		return;
622b409a9efSFlorian Fainelli 
623b409a9efSFlorian Fainelli 	/* Enable reception Broadcom tag for CPU TX (switch RX) to
624b409a9efSFlorian Fainelli 	 * allow us to tag outgoing frames
625b409a9efSFlorian Fainelli 	 */
626b409a9efSFlorian Fainelli 	b53_read16(dev, B53_MGMT_PAGE, B53_BRCM_HDR_RX_DIS, &reg);
627cdb583cfSFlorian Fainelli 	if (tag_en)
628b409a9efSFlorian Fainelli 		reg &= ~BIT(port);
629cdb583cfSFlorian Fainelli 	else
630cdb583cfSFlorian Fainelli 		reg |= BIT(port);
631b409a9efSFlorian Fainelli 	b53_write16(dev, B53_MGMT_PAGE, B53_BRCM_HDR_RX_DIS, reg);
632b409a9efSFlorian Fainelli 
633b409a9efSFlorian Fainelli 	/* Enable transmission of Broadcom tags from the switch (CPU RX) to
634b409a9efSFlorian Fainelli 	 * allow delivering frames to the per-port net_devices
635b409a9efSFlorian Fainelli 	 */
636b409a9efSFlorian Fainelli 	b53_read16(dev, B53_MGMT_PAGE, B53_BRCM_HDR_TX_DIS, &reg);
637cdb583cfSFlorian Fainelli 	if (tag_en)
638b409a9efSFlorian Fainelli 		reg &= ~BIT(port);
639cdb583cfSFlorian Fainelli 	else
640cdb583cfSFlorian Fainelli 		reg |= BIT(port);
641b409a9efSFlorian Fainelli 	b53_write16(dev, B53_MGMT_PAGE, B53_BRCM_HDR_TX_DIS, reg);
642b409a9efSFlorian Fainelli }
643b409a9efSFlorian Fainelli EXPORT_SYMBOL(b53_brcm_hdr_setup);
644b409a9efSFlorian Fainelli 
645299752a7SFlorian Fainelli static void b53_enable_cpu_port(struct b53_device *dev, int port)
646967dd82fSFlorian Fainelli {
647967dd82fSFlorian Fainelli 	u8 port_ctrl;
648967dd82fSFlorian Fainelli 
649967dd82fSFlorian Fainelli 	/* BCM5325 CPU port is at 8 */
650299752a7SFlorian Fainelli 	if ((is5325(dev) || is5365(dev)) && port == B53_CPU_PORT_25)
651299752a7SFlorian Fainelli 		port = B53_CPU_PORT;
652967dd82fSFlorian Fainelli 
653967dd82fSFlorian Fainelli 	port_ctrl = PORT_CTRL_RX_BCST_EN |
654967dd82fSFlorian Fainelli 		    PORT_CTRL_RX_MCST_EN |
655967dd82fSFlorian Fainelli 		    PORT_CTRL_RX_UCST_EN;
656299752a7SFlorian Fainelli 	b53_write8(dev, B53_CTRL_PAGE, B53_PORT_CTRL(port), port_ctrl);
6577edc58d6SFlorian Fainelli 
6587edc58d6SFlorian Fainelli 	b53_brcm_hdr_setup(dev->ds, port);
65963cc54a6SFlorian Fainelli 
66063cc54a6SFlorian Fainelli 	b53_br_egress_floods(dev->ds, port, true, true);
661967dd82fSFlorian Fainelli }
662967dd82fSFlorian Fainelli 
663967dd82fSFlorian Fainelli static void b53_enable_mib(struct b53_device *dev)
664967dd82fSFlorian Fainelli {
665967dd82fSFlorian Fainelli 	u8 gc;
666967dd82fSFlorian Fainelli 
667967dd82fSFlorian Fainelli 	b53_read8(dev, B53_MGMT_PAGE, B53_GLOBAL_CONFIG, &gc);
668967dd82fSFlorian Fainelli 	gc &= ~(GC_RESET_MIB | GC_MIB_AC_EN);
669967dd82fSFlorian Fainelli 	b53_write8(dev, B53_MGMT_PAGE, B53_GLOBAL_CONFIG, gc);
670967dd82fSFlorian Fainelli }
671967dd82fSFlorian Fainelli 
672fea83353SFlorian Fainelli static u16 b53_default_pvid(struct b53_device *dev)
673fea83353SFlorian Fainelli {
674fea83353SFlorian Fainelli 	if (is5325(dev) || is5365(dev))
675fea83353SFlorian Fainelli 		return 1;
676fea83353SFlorian Fainelli 	else
677fea83353SFlorian Fainelli 		return 0;
678fea83353SFlorian Fainelli }
679fea83353SFlorian Fainelli 
6805c1a6eafSFlorian Fainelli int b53_configure_vlan(struct dsa_switch *ds)
681967dd82fSFlorian Fainelli {
6825c1a6eafSFlorian Fainelli 	struct b53_device *dev = ds->priv;
683a2482d2cSFlorian Fainelli 	struct b53_vlan vl = { 0 };
684d7a0b1f7SFlorian Fainelli 	struct b53_vlan *v;
685fea83353SFlorian Fainelli 	int i, def_vid;
686d7a0b1f7SFlorian Fainelli 	u16 vid;
687fea83353SFlorian Fainelli 
688fea83353SFlorian Fainelli 	def_vid = b53_default_pvid(dev);
689967dd82fSFlorian Fainelli 
690967dd82fSFlorian Fainelli 	/* clear all vlan entries */
691967dd82fSFlorian Fainelli 	if (is5325(dev) || is5365(dev)) {
692fea83353SFlorian Fainelli 		for (i = def_vid; i < dev->num_vlans; i++)
693a2482d2cSFlorian Fainelli 			b53_set_vlan_entry(dev, i, &vl);
694967dd82fSFlorian Fainelli 	} else {
695967dd82fSFlorian Fainelli 		b53_do_vlan_op(dev, VTA_CMD_CLEAR);
696967dd82fSFlorian Fainelli 	}
697967dd82fSFlorian Fainelli 
698df373702SFlorian Fainelli 	b53_enable_vlan(dev, dev->vlan_enabled, ds->vlan_filtering);
699967dd82fSFlorian Fainelli 
700967dd82fSFlorian Fainelli 	b53_for_each_port(dev, i)
701967dd82fSFlorian Fainelli 		b53_write16(dev, B53_VLAN_PAGE,
702fea83353SFlorian Fainelli 			    B53_VLAN_PORT_DEF_TAG(i), def_vid);
703967dd82fSFlorian Fainelli 
704d7a0b1f7SFlorian Fainelli 	/* Upon initial call we have not set-up any VLANs, but upon
705d7a0b1f7SFlorian Fainelli 	 * system resume, we need to restore all VLAN entries.
706d7a0b1f7SFlorian Fainelli 	 */
707d7a0b1f7SFlorian Fainelli 	for (vid = def_vid; vid < dev->num_vlans; vid++) {
708d7a0b1f7SFlorian Fainelli 		v = &dev->vlans[vid];
709d7a0b1f7SFlorian Fainelli 
710d7a0b1f7SFlorian Fainelli 		if (!v->members)
711d7a0b1f7SFlorian Fainelli 			continue;
712d7a0b1f7SFlorian Fainelli 
713d7a0b1f7SFlorian Fainelli 		b53_set_vlan_entry(dev, vid, v);
714d7a0b1f7SFlorian Fainelli 		b53_fast_age_vlan(dev, vid);
715d7a0b1f7SFlorian Fainelli 	}
716d7a0b1f7SFlorian Fainelli 
717967dd82fSFlorian Fainelli 	return 0;
718967dd82fSFlorian Fainelli }
7195c1a6eafSFlorian Fainelli EXPORT_SYMBOL(b53_configure_vlan);
720967dd82fSFlorian Fainelli 
721967dd82fSFlorian Fainelli static void b53_switch_reset_gpio(struct b53_device *dev)
722967dd82fSFlorian Fainelli {
723967dd82fSFlorian Fainelli 	int gpio = dev->reset_gpio;
724967dd82fSFlorian Fainelli 
725967dd82fSFlorian Fainelli 	if (gpio < 0)
726967dd82fSFlorian Fainelli 		return;
727967dd82fSFlorian Fainelli 
728967dd82fSFlorian Fainelli 	/* Reset sequence: RESET low(50ms)->high(20ms)
729967dd82fSFlorian Fainelli 	 */
730967dd82fSFlorian Fainelli 	gpio_set_value(gpio, 0);
731967dd82fSFlorian Fainelli 	mdelay(50);
732967dd82fSFlorian Fainelli 
733967dd82fSFlorian Fainelli 	gpio_set_value(gpio, 1);
734967dd82fSFlorian Fainelli 	mdelay(20);
735967dd82fSFlorian Fainelli 
736967dd82fSFlorian Fainelli 	dev->current_page = 0xff;
737967dd82fSFlorian Fainelli }
738967dd82fSFlorian Fainelli 
739967dd82fSFlorian Fainelli static int b53_switch_reset(struct b53_device *dev)
740967dd82fSFlorian Fainelli {
7413fb22b05SFlorian Fainelli 	unsigned int timeout = 1000;
7423fb22b05SFlorian Fainelli 	u8 mgmt, reg;
743967dd82fSFlorian Fainelli 
744967dd82fSFlorian Fainelli 	b53_switch_reset_gpio(dev);
745967dd82fSFlorian Fainelli 
746967dd82fSFlorian Fainelli 	if (is539x(dev)) {
747967dd82fSFlorian Fainelli 		b53_write8(dev, B53_CTRL_PAGE, B53_SOFTRESET, 0x83);
748967dd82fSFlorian Fainelli 		b53_write8(dev, B53_CTRL_PAGE, B53_SOFTRESET, 0x00);
749967dd82fSFlorian Fainelli 	}
750967dd82fSFlorian Fainelli 
7513fb22b05SFlorian Fainelli 	/* This is specific to 58xx devices here, do not use is58xx() which
7523fb22b05SFlorian Fainelli 	 * covers the larger Starfigther 2 family, including 7445/7278 which
7533fb22b05SFlorian Fainelli 	 * still use this driver as a library and need to perform the reset
7543fb22b05SFlorian Fainelli 	 * earlier.
7553fb22b05SFlorian Fainelli 	 */
7565040cc99SArun Parameswaran 	if (dev->chip_id == BCM58XX_DEVICE_ID ||
7575040cc99SArun Parameswaran 	    dev->chip_id == BCM583XX_DEVICE_ID) {
7583fb22b05SFlorian Fainelli 		b53_read8(dev, B53_CTRL_PAGE, B53_SOFTRESET, &reg);
7593fb22b05SFlorian Fainelli 		reg |= SW_RST | EN_SW_RST | EN_CH_RST;
7603fb22b05SFlorian Fainelli 		b53_write8(dev, B53_CTRL_PAGE, B53_SOFTRESET, reg);
7613fb22b05SFlorian Fainelli 
7623fb22b05SFlorian Fainelli 		do {
7633fb22b05SFlorian Fainelli 			b53_read8(dev, B53_CTRL_PAGE, B53_SOFTRESET, &reg);
7643fb22b05SFlorian Fainelli 			if (!(reg & SW_RST))
7653fb22b05SFlorian Fainelli 				break;
7663fb22b05SFlorian Fainelli 
7673fb22b05SFlorian Fainelli 			usleep_range(1000, 2000);
7683fb22b05SFlorian Fainelli 		} while (timeout-- > 0);
7693fb22b05SFlorian Fainelli 
7703fb22b05SFlorian Fainelli 		if (timeout == 0)
7713fb22b05SFlorian Fainelli 			return -ETIMEDOUT;
7723fb22b05SFlorian Fainelli 	}
7733fb22b05SFlorian Fainelli 
774967dd82fSFlorian Fainelli 	b53_read8(dev, B53_CTRL_PAGE, B53_SWITCH_MODE, &mgmt);
775967dd82fSFlorian Fainelli 
776967dd82fSFlorian Fainelli 	if (!(mgmt & SM_SW_FWD_EN)) {
777967dd82fSFlorian Fainelli 		mgmt &= ~SM_SW_FWD_MODE;
778967dd82fSFlorian Fainelli 		mgmt |= SM_SW_FWD_EN;
779967dd82fSFlorian Fainelli 
780967dd82fSFlorian Fainelli 		b53_write8(dev, B53_CTRL_PAGE, B53_SWITCH_MODE, mgmt);
781967dd82fSFlorian Fainelli 		b53_read8(dev, B53_CTRL_PAGE, B53_SWITCH_MODE, &mgmt);
782967dd82fSFlorian Fainelli 
783967dd82fSFlorian Fainelli 		if (!(mgmt & SM_SW_FWD_EN)) {
784967dd82fSFlorian Fainelli 			dev_err(dev->dev, "Failed to enable switch!\n");
785967dd82fSFlorian Fainelli 			return -EINVAL;
786967dd82fSFlorian Fainelli 		}
787967dd82fSFlorian Fainelli 	}
788967dd82fSFlorian Fainelli 
789967dd82fSFlorian Fainelli 	b53_enable_mib(dev);
790967dd82fSFlorian Fainelli 
791ff39c2d6SFlorian Fainelli 	return b53_flush_arl(dev, FAST_AGE_STATIC);
792967dd82fSFlorian Fainelli }
793967dd82fSFlorian Fainelli 
794967dd82fSFlorian Fainelli static int b53_phy_read16(struct dsa_switch *ds, int addr, int reg)
795967dd82fSFlorian Fainelli {
79604bed143SVivien Didelot 	struct b53_device *priv = ds->priv;
797967dd82fSFlorian Fainelli 	u16 value = 0;
798967dd82fSFlorian Fainelli 	int ret;
799967dd82fSFlorian Fainelli 
800967dd82fSFlorian Fainelli 	if (priv->ops->phy_read16)
801967dd82fSFlorian Fainelli 		ret = priv->ops->phy_read16(priv, addr, reg, &value);
802967dd82fSFlorian Fainelli 	else
803967dd82fSFlorian Fainelli 		ret = b53_read16(priv, B53_PORT_MII_PAGE(addr),
804967dd82fSFlorian Fainelli 				 reg * 2, &value);
805967dd82fSFlorian Fainelli 
806967dd82fSFlorian Fainelli 	return ret ? ret : value;
807967dd82fSFlorian Fainelli }
808967dd82fSFlorian Fainelli 
809967dd82fSFlorian Fainelli static int b53_phy_write16(struct dsa_switch *ds, int addr, int reg, u16 val)
810967dd82fSFlorian Fainelli {
81104bed143SVivien Didelot 	struct b53_device *priv = ds->priv;
812967dd82fSFlorian Fainelli 
813967dd82fSFlorian Fainelli 	if (priv->ops->phy_write16)
814967dd82fSFlorian Fainelli 		return priv->ops->phy_write16(priv, addr, reg, val);
815967dd82fSFlorian Fainelli 
816967dd82fSFlorian Fainelli 	return b53_write16(priv, B53_PORT_MII_PAGE(addr), reg * 2, val);
817967dd82fSFlorian Fainelli }
818967dd82fSFlorian Fainelli 
819967dd82fSFlorian Fainelli static int b53_reset_switch(struct b53_device *priv)
820967dd82fSFlorian Fainelli {
821967dd82fSFlorian Fainelli 	/* reset vlans */
822a2482d2cSFlorian Fainelli 	memset(priv->vlans, 0, sizeof(*priv->vlans) * priv->num_vlans);
823967dd82fSFlorian Fainelli 	memset(priv->ports, 0, sizeof(*priv->ports) * priv->num_ports);
824967dd82fSFlorian Fainelli 
8250e01491dSFlorian Fainelli 	priv->serdes_lane = B53_INVALID_LANE;
8260e01491dSFlorian Fainelli 
827967dd82fSFlorian Fainelli 	return b53_switch_reset(priv);
828967dd82fSFlorian Fainelli }
829967dd82fSFlorian Fainelli 
830967dd82fSFlorian Fainelli static int b53_apply_config(struct b53_device *priv)
831967dd82fSFlorian Fainelli {
832967dd82fSFlorian Fainelli 	/* disable switching */
833967dd82fSFlorian Fainelli 	b53_set_forwarding(priv, 0);
834967dd82fSFlorian Fainelli 
8355c1a6eafSFlorian Fainelli 	b53_configure_vlan(priv->ds);
836967dd82fSFlorian Fainelli 
837967dd82fSFlorian Fainelli 	/* enable switching */
838967dd82fSFlorian Fainelli 	b53_set_forwarding(priv, 1);
839967dd82fSFlorian Fainelli 
840967dd82fSFlorian Fainelli 	return 0;
841967dd82fSFlorian Fainelli }
842967dd82fSFlorian Fainelli 
843967dd82fSFlorian Fainelli static void b53_reset_mib(struct b53_device *priv)
844967dd82fSFlorian Fainelli {
845967dd82fSFlorian Fainelli 	u8 gc;
846967dd82fSFlorian Fainelli 
847967dd82fSFlorian Fainelli 	b53_read8(priv, B53_MGMT_PAGE, B53_GLOBAL_CONFIG, &gc);
848967dd82fSFlorian Fainelli 
849967dd82fSFlorian Fainelli 	b53_write8(priv, B53_MGMT_PAGE, B53_GLOBAL_CONFIG, gc | GC_RESET_MIB);
850967dd82fSFlorian Fainelli 	msleep(1);
851967dd82fSFlorian Fainelli 	b53_write8(priv, B53_MGMT_PAGE, B53_GLOBAL_CONFIG, gc & ~GC_RESET_MIB);
852967dd82fSFlorian Fainelli 	msleep(1);
853967dd82fSFlorian Fainelli }
854967dd82fSFlorian Fainelli 
855967dd82fSFlorian Fainelli static const struct b53_mib_desc *b53_get_mib(struct b53_device *dev)
856967dd82fSFlorian Fainelli {
857967dd82fSFlorian Fainelli 	if (is5365(dev))
858967dd82fSFlorian Fainelli 		return b53_mibs_65;
859967dd82fSFlorian Fainelli 	else if (is63xx(dev))
860967dd82fSFlorian Fainelli 		return b53_mibs_63xx;
861bde5d132SFlorian Fainelli 	else if (is58xx(dev))
862bde5d132SFlorian Fainelli 		return b53_mibs_58xx;
863967dd82fSFlorian Fainelli 	else
864967dd82fSFlorian Fainelli 		return b53_mibs;
865967dd82fSFlorian Fainelli }
866967dd82fSFlorian Fainelli 
867967dd82fSFlorian Fainelli static unsigned int b53_get_mib_size(struct b53_device *dev)
868967dd82fSFlorian Fainelli {
869967dd82fSFlorian Fainelli 	if (is5365(dev))
870967dd82fSFlorian Fainelli 		return B53_MIBS_65_SIZE;
871967dd82fSFlorian Fainelli 	else if (is63xx(dev))
872967dd82fSFlorian Fainelli 		return B53_MIBS_63XX_SIZE;
873bde5d132SFlorian Fainelli 	else if (is58xx(dev))
874bde5d132SFlorian Fainelli 		return B53_MIBS_58XX_SIZE;
875967dd82fSFlorian Fainelli 	else
876967dd82fSFlorian Fainelli 		return B53_MIBS_SIZE;
877967dd82fSFlorian Fainelli }
878967dd82fSFlorian Fainelli 
879c7d28c9dSFlorian Fainelli static struct phy_device *b53_get_phy_device(struct dsa_switch *ds, int port)
880c7d28c9dSFlorian Fainelli {
881c7d28c9dSFlorian Fainelli 	/* These ports typically do not have built-in PHYs */
882c7d28c9dSFlorian Fainelli 	switch (port) {
883c7d28c9dSFlorian Fainelli 	case B53_CPU_PORT_25:
884c7d28c9dSFlorian Fainelli 	case 7:
885c7d28c9dSFlorian Fainelli 	case B53_CPU_PORT:
886c7d28c9dSFlorian Fainelli 		return NULL;
887c7d28c9dSFlorian Fainelli 	}
888c7d28c9dSFlorian Fainelli 
889c7d28c9dSFlorian Fainelli 	return mdiobus_get_phy(ds->slave_mii_bus, port);
890c7d28c9dSFlorian Fainelli }
891c7d28c9dSFlorian Fainelli 
89289f09048SFlorian Fainelli void b53_get_strings(struct dsa_switch *ds, int port, u32 stringset,
89389f09048SFlorian Fainelli 		     uint8_t *data)
894967dd82fSFlorian Fainelli {
89504bed143SVivien Didelot 	struct b53_device *dev = ds->priv;
896967dd82fSFlorian Fainelli 	const struct b53_mib_desc *mibs = b53_get_mib(dev);
897967dd82fSFlorian Fainelli 	unsigned int mib_size = b53_get_mib_size(dev);
898c7d28c9dSFlorian Fainelli 	struct phy_device *phydev;
899967dd82fSFlorian Fainelli 	unsigned int i;
900967dd82fSFlorian Fainelli 
901c7d28c9dSFlorian Fainelli 	if (stringset == ETH_SS_STATS) {
902967dd82fSFlorian Fainelli 		for (i = 0; i < mib_size; i++)
903cd526676SFlorian Fainelli 			strlcpy(data + i * ETH_GSTRING_LEN,
904967dd82fSFlorian Fainelli 				mibs[i].name, ETH_GSTRING_LEN);
905c7d28c9dSFlorian Fainelli 	} else if (stringset == ETH_SS_PHY_STATS) {
906c7d28c9dSFlorian Fainelli 		phydev = b53_get_phy_device(ds, port);
907c7d28c9dSFlorian Fainelli 		if (!phydev)
908c7d28c9dSFlorian Fainelli 			return;
909c7d28c9dSFlorian Fainelli 
910c7d28c9dSFlorian Fainelli 		phy_ethtool_get_strings(phydev, data);
911c7d28c9dSFlorian Fainelli 	}
912967dd82fSFlorian Fainelli }
9133117455dSFlorian Fainelli EXPORT_SYMBOL(b53_get_strings);
914967dd82fSFlorian Fainelli 
9153117455dSFlorian Fainelli void b53_get_ethtool_stats(struct dsa_switch *ds, int port, uint64_t *data)
916967dd82fSFlorian Fainelli {
91704bed143SVivien Didelot 	struct b53_device *dev = ds->priv;
918967dd82fSFlorian Fainelli 	const struct b53_mib_desc *mibs = b53_get_mib(dev);
919967dd82fSFlorian Fainelli 	unsigned int mib_size = b53_get_mib_size(dev);
920967dd82fSFlorian Fainelli 	const struct b53_mib_desc *s;
921967dd82fSFlorian Fainelli 	unsigned int i;
922967dd82fSFlorian Fainelli 	u64 val = 0;
923967dd82fSFlorian Fainelli 
924967dd82fSFlorian Fainelli 	if (is5365(dev) && port == 5)
925967dd82fSFlorian Fainelli 		port = 8;
926967dd82fSFlorian Fainelli 
927967dd82fSFlorian Fainelli 	mutex_lock(&dev->stats_mutex);
928967dd82fSFlorian Fainelli 
929967dd82fSFlorian Fainelli 	for (i = 0; i < mib_size; i++) {
930967dd82fSFlorian Fainelli 		s = &mibs[i];
931967dd82fSFlorian Fainelli 
93251dca8a1SFlorian Fainelli 		if (s->size == 8) {
933967dd82fSFlorian Fainelli 			b53_read64(dev, B53_MIB_PAGE(port), s->offset, &val);
934967dd82fSFlorian Fainelli 		} else {
935967dd82fSFlorian Fainelli 			u32 val32;
936967dd82fSFlorian Fainelli 
937967dd82fSFlorian Fainelli 			b53_read32(dev, B53_MIB_PAGE(port), s->offset,
938967dd82fSFlorian Fainelli 				   &val32);
939967dd82fSFlorian Fainelli 			val = val32;
940967dd82fSFlorian Fainelli 		}
941967dd82fSFlorian Fainelli 		data[i] = (u64)val;
942967dd82fSFlorian Fainelli 	}
943967dd82fSFlorian Fainelli 
944967dd82fSFlorian Fainelli 	mutex_unlock(&dev->stats_mutex);
945967dd82fSFlorian Fainelli }
9463117455dSFlorian Fainelli EXPORT_SYMBOL(b53_get_ethtool_stats);
947967dd82fSFlorian Fainelli 
948c7d28c9dSFlorian Fainelli void b53_get_ethtool_phy_stats(struct dsa_switch *ds, int port, uint64_t *data)
949c7d28c9dSFlorian Fainelli {
950c7d28c9dSFlorian Fainelli 	struct phy_device *phydev;
951c7d28c9dSFlorian Fainelli 
952c7d28c9dSFlorian Fainelli 	phydev = b53_get_phy_device(ds, port);
953c7d28c9dSFlorian Fainelli 	if (!phydev)
954c7d28c9dSFlorian Fainelli 		return;
955c7d28c9dSFlorian Fainelli 
956c7d28c9dSFlorian Fainelli 	phy_ethtool_get_stats(phydev, NULL, data);
957c7d28c9dSFlorian Fainelli }
958c7d28c9dSFlorian Fainelli EXPORT_SYMBOL(b53_get_ethtool_phy_stats);
959c7d28c9dSFlorian Fainelli 
96089f09048SFlorian Fainelli int b53_get_sset_count(struct dsa_switch *ds, int port, int sset)
961967dd82fSFlorian Fainelli {
96204bed143SVivien Didelot 	struct b53_device *dev = ds->priv;
963c7d28c9dSFlorian Fainelli 	struct phy_device *phydev;
964967dd82fSFlorian Fainelli 
965c7d28c9dSFlorian Fainelli 	if (sset == ETH_SS_STATS) {
966c7d28c9dSFlorian Fainelli 		return b53_get_mib_size(dev);
967c7d28c9dSFlorian Fainelli 	} else if (sset == ETH_SS_PHY_STATS) {
968c7d28c9dSFlorian Fainelli 		phydev = b53_get_phy_device(ds, port);
969c7d28c9dSFlorian Fainelli 		if (!phydev)
97089f09048SFlorian Fainelli 			return 0;
97189f09048SFlorian Fainelli 
972c7d28c9dSFlorian Fainelli 		return phy_ethtool_get_sset_count(phydev);
973c7d28c9dSFlorian Fainelli 	}
974c7d28c9dSFlorian Fainelli 
975c7d28c9dSFlorian Fainelli 	return 0;
976967dd82fSFlorian Fainelli }
9773117455dSFlorian Fainelli EXPORT_SYMBOL(b53_get_sset_count);
978967dd82fSFlorian Fainelli 
979967dd82fSFlorian Fainelli static int b53_setup(struct dsa_switch *ds)
980967dd82fSFlorian Fainelli {
98104bed143SVivien Didelot 	struct b53_device *dev = ds->priv;
982967dd82fSFlorian Fainelli 	unsigned int port;
983967dd82fSFlorian Fainelli 	int ret;
984967dd82fSFlorian Fainelli 
985967dd82fSFlorian Fainelli 	ret = b53_reset_switch(dev);
986967dd82fSFlorian Fainelli 	if (ret) {
987967dd82fSFlorian Fainelli 		dev_err(ds->dev, "failed to reset switch\n");
988967dd82fSFlorian Fainelli 		return ret;
989967dd82fSFlorian Fainelli 	}
990967dd82fSFlorian Fainelli 
991967dd82fSFlorian Fainelli 	b53_reset_mib(dev);
992967dd82fSFlorian Fainelli 
993967dd82fSFlorian Fainelli 	ret = b53_apply_config(dev);
994967dd82fSFlorian Fainelli 	if (ret)
995967dd82fSFlorian Fainelli 		dev_err(ds->dev, "failed to apply configuration\n");
996967dd82fSFlorian Fainelli 
99775dad252SBenedikt Spranger 	/* Configure IMP/CPU port, disable all other ports. Enabled
99834c8befdSFlorian Fainelli 	 * ports will be configured with .port_enable
99934c8befdSFlorian Fainelli 	 */
1000967dd82fSFlorian Fainelli 	for (port = 0; port < dev->num_ports; port++) {
100134c8befdSFlorian Fainelli 		if (dsa_is_cpu_port(ds, port))
1002299752a7SFlorian Fainelli 			b53_enable_cpu_port(dev, port);
100375dad252SBenedikt Spranger 		else
100475104db0SAndrew Lunn 			b53_disable_port(ds, port);
1005967dd82fSFlorian Fainelli 	}
1006967dd82fSFlorian Fainelli 
10077228b23eSVladimir Oltean 	/* Let DSA handle the case were multiple bridges span the same switch
10087228b23eSVladimir Oltean 	 * device and different VLAN awareness settings are requested, which
10097228b23eSVladimir Oltean 	 * would be breaking filtering semantics for any of the other bridge
10107228b23eSVladimir Oltean 	 * devices. (not hardware supported)
10117228b23eSVladimir Oltean 	 */
10127228b23eSVladimir Oltean 	ds->vlan_filtering_is_global = true;
10137228b23eSVladimir Oltean 
1014967dd82fSFlorian Fainelli 	return ret;
1015967dd82fSFlorian Fainelli }
1016967dd82fSFlorian Fainelli 
10175e004460SFlorian Fainelli static void b53_force_link(struct b53_device *dev, int port, int link)
1018967dd82fSFlorian Fainelli {
10195e004460SFlorian Fainelli 	u8 reg, val, off;
1020967dd82fSFlorian Fainelli 
1021967dd82fSFlorian Fainelli 	/* Override the port settings */
1022967dd82fSFlorian Fainelli 	if (port == dev->cpu_port) {
1023967dd82fSFlorian Fainelli 		off = B53_PORT_OVERRIDE_CTRL;
10245e004460SFlorian Fainelli 		val = PORT_OVERRIDE_EN;
1025967dd82fSFlorian Fainelli 	} else {
1026967dd82fSFlorian Fainelli 		off = B53_GMII_PORT_OVERRIDE_CTRL(port);
10275e004460SFlorian Fainelli 		val = GMII_PO_EN;
1028967dd82fSFlorian Fainelli 	}
1029967dd82fSFlorian Fainelli 
10305e004460SFlorian Fainelli 	b53_read8(dev, B53_CTRL_PAGE, off, &reg);
10315e004460SFlorian Fainelli 	reg |= val;
10325e004460SFlorian Fainelli 	if (link)
1033967dd82fSFlorian Fainelli 		reg |= PORT_OVERRIDE_LINK;
10345e004460SFlorian Fainelli 	else
10355e004460SFlorian Fainelli 		reg &= ~PORT_OVERRIDE_LINK;
10365e004460SFlorian Fainelli 	b53_write8(dev, B53_CTRL_PAGE, off, reg);
10375e004460SFlorian Fainelli }
1038967dd82fSFlorian Fainelli 
10395e004460SFlorian Fainelli static void b53_force_port_config(struct b53_device *dev, int port,
10403cad1c8bSRussell King 				  int speed, int duplex,
10413cad1c8bSRussell King 				  bool tx_pause, bool rx_pause)
10425e004460SFlorian Fainelli {
10435e004460SFlorian Fainelli 	u8 reg, val, off;
10445e004460SFlorian Fainelli 
10455e004460SFlorian Fainelli 	/* Override the port settings */
10465e004460SFlorian Fainelli 	if (port == dev->cpu_port) {
10475e004460SFlorian Fainelli 		off = B53_PORT_OVERRIDE_CTRL;
10485e004460SFlorian Fainelli 		val = PORT_OVERRIDE_EN;
10495e004460SFlorian Fainelli 	} else {
10505e004460SFlorian Fainelli 		off = B53_GMII_PORT_OVERRIDE_CTRL(port);
10515e004460SFlorian Fainelli 		val = GMII_PO_EN;
10525e004460SFlorian Fainelli 	}
10535e004460SFlorian Fainelli 
10545e004460SFlorian Fainelli 	b53_read8(dev, B53_CTRL_PAGE, off, &reg);
10555e004460SFlorian Fainelli 	reg |= val;
10565e004460SFlorian Fainelli 	if (duplex == DUPLEX_FULL)
1057967dd82fSFlorian Fainelli 		reg |= PORT_OVERRIDE_FULL_DUPLEX;
10585e004460SFlorian Fainelli 	else
10595e004460SFlorian Fainelli 		reg &= ~PORT_OVERRIDE_FULL_DUPLEX;
1060967dd82fSFlorian Fainelli 
10615e004460SFlorian Fainelli 	switch (speed) {
1062967dd82fSFlorian Fainelli 	case 2000:
1063967dd82fSFlorian Fainelli 		reg |= PORT_OVERRIDE_SPEED_2000M;
1064*df561f66SGustavo A. R. Silva 		fallthrough;
1065967dd82fSFlorian Fainelli 	case SPEED_1000:
1066967dd82fSFlorian Fainelli 		reg |= PORT_OVERRIDE_SPEED_1000M;
1067967dd82fSFlorian Fainelli 		break;
1068967dd82fSFlorian Fainelli 	case SPEED_100:
1069967dd82fSFlorian Fainelli 		reg |= PORT_OVERRIDE_SPEED_100M;
1070967dd82fSFlorian Fainelli 		break;
1071967dd82fSFlorian Fainelli 	case SPEED_10:
1072967dd82fSFlorian Fainelli 		reg |= PORT_OVERRIDE_SPEED_10M;
1073967dd82fSFlorian Fainelli 		break;
1074967dd82fSFlorian Fainelli 	default:
10755e004460SFlorian Fainelli 		dev_err(dev->dev, "unknown speed: %d\n", speed);
1076967dd82fSFlorian Fainelli 		return;
1077967dd82fSFlorian Fainelli 	}
1078967dd82fSFlorian Fainelli 
10793cad1c8bSRussell King 	if (rx_pause)
10805e004460SFlorian Fainelli 		reg |= PORT_OVERRIDE_RX_FLOW;
10813cad1c8bSRussell King 	if (tx_pause)
10825e004460SFlorian Fainelli 		reg |= PORT_OVERRIDE_TX_FLOW;
10835e004460SFlorian Fainelli 
10845e004460SFlorian Fainelli 	b53_write8(dev, B53_CTRL_PAGE, off, reg);
10855e004460SFlorian Fainelli }
10865e004460SFlorian Fainelli 
10875e004460SFlorian Fainelli static void b53_adjust_link(struct dsa_switch *ds, int port,
10885e004460SFlorian Fainelli 			    struct phy_device *phydev)
10895e004460SFlorian Fainelli {
10905e004460SFlorian Fainelli 	struct b53_device *dev = ds->priv;
10915e004460SFlorian Fainelli 	struct ethtool_eee *p = &dev->ports[port].eee;
10925e004460SFlorian Fainelli 	u8 rgmii_ctrl = 0, reg = 0, off;
10933cad1c8bSRussell King 	bool tx_pause = false;
10943cad1c8bSRussell King 	bool rx_pause = false;
10955e004460SFlorian Fainelli 
10965e004460SFlorian Fainelli 	if (!phy_is_pseudo_fixed_link(phydev))
10975e004460SFlorian Fainelli 		return;
10985e004460SFlorian Fainelli 
1099967dd82fSFlorian Fainelli 	/* Enable flow control on BCM5301x's CPU port */
1100967dd82fSFlorian Fainelli 	if (is5301x(dev) && port == dev->cpu_port)
11013cad1c8bSRussell King 		tx_pause = rx_pause = true;
1102967dd82fSFlorian Fainelli 
1103967dd82fSFlorian Fainelli 	if (phydev->pause) {
1104967dd82fSFlorian Fainelli 		if (phydev->asym_pause)
11053cad1c8bSRussell King 			tx_pause = true;
11063cad1c8bSRussell King 		rx_pause = true;
1107967dd82fSFlorian Fainelli 	}
1108967dd82fSFlorian Fainelli 
11093cad1c8bSRussell King 	b53_force_port_config(dev, port, phydev->speed, phydev->duplex,
11103cad1c8bSRussell King 			      tx_pause, rx_pause);
11115e004460SFlorian Fainelli 	b53_force_link(dev, port, phydev->link);
1112967dd82fSFlorian Fainelli 
1113967dd82fSFlorian Fainelli 	if (is531x5(dev) && phy_interface_is_rgmii(phydev)) {
1114967dd82fSFlorian Fainelli 		if (port == 8)
1115967dd82fSFlorian Fainelli 			off = B53_RGMII_CTRL_IMP;
1116967dd82fSFlorian Fainelli 		else
1117967dd82fSFlorian Fainelli 			off = B53_RGMII_CTRL_P(port);
1118967dd82fSFlorian Fainelli 
1119967dd82fSFlorian Fainelli 		/* Configure the port RGMII clock delay by DLL disabled and
1120967dd82fSFlorian Fainelli 		 * tx_clk aligned timing (restoring to reset defaults)
1121967dd82fSFlorian Fainelli 		 */
1122967dd82fSFlorian Fainelli 		b53_read8(dev, B53_CTRL_PAGE, off, &rgmii_ctrl);
1123967dd82fSFlorian Fainelli 		rgmii_ctrl &= ~(RGMII_CTRL_DLL_RXC | RGMII_CTRL_DLL_TXC |
1124967dd82fSFlorian Fainelli 				RGMII_CTRL_TIMING_SEL);
1125967dd82fSFlorian Fainelli 
1126967dd82fSFlorian Fainelli 		/* PHY_INTERFACE_MODE_RGMII_TXID means TX internal delay, make
1127967dd82fSFlorian Fainelli 		 * sure that we enable the port TX clock internal delay to
1128967dd82fSFlorian Fainelli 		 * account for this internal delay that is inserted, otherwise
1129967dd82fSFlorian Fainelli 		 * the switch won't be able to receive correctly.
1130967dd82fSFlorian Fainelli 		 *
1131967dd82fSFlorian Fainelli 		 * PHY_INTERFACE_MODE_RGMII means that we are not introducing
1132967dd82fSFlorian Fainelli 		 * any delay neither on transmission nor reception, so the
1133967dd82fSFlorian Fainelli 		 * BCM53125 must also be configured accordingly to account for
1134967dd82fSFlorian Fainelli 		 * the lack of delay and introduce
1135967dd82fSFlorian Fainelli 		 *
1136967dd82fSFlorian Fainelli 		 * The BCM53125 switch has its RX clock and TX clock control
1137967dd82fSFlorian Fainelli 		 * swapped, hence the reason why we modify the TX clock path in
1138967dd82fSFlorian Fainelli 		 * the "RGMII" case
1139967dd82fSFlorian Fainelli 		 */
1140967dd82fSFlorian Fainelli 		if (phydev->interface == PHY_INTERFACE_MODE_RGMII_TXID)
1141967dd82fSFlorian Fainelli 			rgmii_ctrl |= RGMII_CTRL_DLL_TXC;
1142967dd82fSFlorian Fainelli 		if (phydev->interface == PHY_INTERFACE_MODE_RGMII)
1143967dd82fSFlorian Fainelli 			rgmii_ctrl |= RGMII_CTRL_DLL_TXC | RGMII_CTRL_DLL_RXC;
1144967dd82fSFlorian Fainelli 		rgmii_ctrl |= RGMII_CTRL_TIMING_SEL;
1145967dd82fSFlorian Fainelli 		b53_write8(dev, B53_CTRL_PAGE, off, rgmii_ctrl);
1146967dd82fSFlorian Fainelli 
1147967dd82fSFlorian Fainelli 		dev_info(ds->dev, "Configured port %d for %s\n", port,
1148967dd82fSFlorian Fainelli 			 phy_modes(phydev->interface));
1149967dd82fSFlorian Fainelli 	}
1150967dd82fSFlorian Fainelli 
1151967dd82fSFlorian Fainelli 	/* configure MII port if necessary */
1152967dd82fSFlorian Fainelli 	if (is5325(dev)) {
1153967dd82fSFlorian Fainelli 		b53_read8(dev, B53_CTRL_PAGE, B53_PORT_OVERRIDE_CTRL,
1154967dd82fSFlorian Fainelli 			  &reg);
1155967dd82fSFlorian Fainelli 
1156967dd82fSFlorian Fainelli 		/* reverse mii needs to be enabled */
1157967dd82fSFlorian Fainelli 		if (!(reg & PORT_OVERRIDE_RV_MII_25)) {
1158967dd82fSFlorian Fainelli 			b53_write8(dev, B53_CTRL_PAGE, B53_PORT_OVERRIDE_CTRL,
1159967dd82fSFlorian Fainelli 				   reg | PORT_OVERRIDE_RV_MII_25);
1160967dd82fSFlorian Fainelli 			b53_read8(dev, B53_CTRL_PAGE, B53_PORT_OVERRIDE_CTRL,
1161967dd82fSFlorian Fainelli 				  &reg);
1162967dd82fSFlorian Fainelli 
1163967dd82fSFlorian Fainelli 			if (!(reg & PORT_OVERRIDE_RV_MII_25)) {
1164967dd82fSFlorian Fainelli 				dev_err(ds->dev,
1165967dd82fSFlorian Fainelli 					"Failed to enable reverse MII mode\n");
1166967dd82fSFlorian Fainelli 				return;
1167967dd82fSFlorian Fainelli 			}
1168967dd82fSFlorian Fainelli 		}
1169967dd82fSFlorian Fainelli 	} else if (is5301x(dev)) {
1170967dd82fSFlorian Fainelli 		if (port != dev->cpu_port) {
11715e004460SFlorian Fainelli 			b53_force_port_config(dev, dev->cpu_port, 2000,
11723cad1c8bSRussell King 					      DUPLEX_FULL, true, true);
11735e004460SFlorian Fainelli 			b53_force_link(dev, dev->cpu_port, 1);
1174967dd82fSFlorian Fainelli 		}
1175967dd82fSFlorian Fainelli 	}
1176f43a2dbeSFlorian Fainelli 
1177f43a2dbeSFlorian Fainelli 	/* Re-negotiate EEE if it was enabled already */
1178f43a2dbeSFlorian Fainelli 	p->eee_enabled = b53_eee_init(ds, port, phydev);
1179967dd82fSFlorian Fainelli }
1180967dd82fSFlorian Fainelli 
1181a8e8b985SFlorian Fainelli void b53_port_event(struct dsa_switch *ds, int port)
1182a8e8b985SFlorian Fainelli {
1183a8e8b985SFlorian Fainelli 	struct b53_device *dev = ds->priv;
1184a8e8b985SFlorian Fainelli 	bool link;
1185a8e8b985SFlorian Fainelli 	u16 sts;
1186a8e8b985SFlorian Fainelli 
1187a8e8b985SFlorian Fainelli 	b53_read16(dev, B53_STAT_PAGE, B53_LINK_STAT, &sts);
1188a8e8b985SFlorian Fainelli 	link = !!(sts & BIT(port));
1189a8e8b985SFlorian Fainelli 	dsa_port_phylink_mac_change(ds, port, link);
1190a8e8b985SFlorian Fainelli }
1191a8e8b985SFlorian Fainelli EXPORT_SYMBOL(b53_port_event);
1192a8e8b985SFlorian Fainelli 
1193a8e8b985SFlorian Fainelli void b53_phylink_validate(struct dsa_switch *ds, int port,
1194a8e8b985SFlorian Fainelli 			  unsigned long *supported,
1195a8e8b985SFlorian Fainelli 			  struct phylink_link_state *state)
1196a8e8b985SFlorian Fainelli {
1197a8e8b985SFlorian Fainelli 	struct b53_device *dev = ds->priv;
1198a8e8b985SFlorian Fainelli 	__ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, };
1199a8e8b985SFlorian Fainelli 
12000e01491dSFlorian Fainelli 	if (dev->ops->serdes_phylink_validate)
12010e01491dSFlorian Fainelli 		dev->ops->serdes_phylink_validate(dev, port, mask, state);
12020e01491dSFlorian Fainelli 
1203a8e8b985SFlorian Fainelli 	/* Allow all the expected bits */
1204a8e8b985SFlorian Fainelli 	phylink_set(mask, Autoneg);
1205a8e8b985SFlorian Fainelli 	phylink_set_port_modes(mask);
1206a8e8b985SFlorian Fainelli 	phylink_set(mask, Pause);
1207a8e8b985SFlorian Fainelli 	phylink_set(mask, Asym_Pause);
1208a8e8b985SFlorian Fainelli 
1209a8e8b985SFlorian Fainelli 	/* With the exclusion of 5325/5365, MII, Reverse MII and 802.3z, we
1210a8e8b985SFlorian Fainelli 	 * support Gigabit, including Half duplex.
1211a8e8b985SFlorian Fainelli 	 */
1212a8e8b985SFlorian Fainelli 	if (state->interface != PHY_INTERFACE_MODE_MII &&
1213a8e8b985SFlorian Fainelli 	    state->interface != PHY_INTERFACE_MODE_REVMII &&
1214a8e8b985SFlorian Fainelli 	    !phy_interface_mode_is_8023z(state->interface) &&
1215a8e8b985SFlorian Fainelli 	    !(is5325(dev) || is5365(dev))) {
1216a8e8b985SFlorian Fainelli 		phylink_set(mask, 1000baseT_Full);
1217a8e8b985SFlorian Fainelli 		phylink_set(mask, 1000baseT_Half);
1218a8e8b985SFlorian Fainelli 	}
1219a8e8b985SFlorian Fainelli 
1220a8e8b985SFlorian Fainelli 	if (!phy_interface_mode_is_8023z(state->interface)) {
1221a8e8b985SFlorian Fainelli 		phylink_set(mask, 10baseT_Half);
1222a8e8b985SFlorian Fainelli 		phylink_set(mask, 10baseT_Full);
1223a8e8b985SFlorian Fainelli 		phylink_set(mask, 100baseT_Half);
1224a8e8b985SFlorian Fainelli 		phylink_set(mask, 100baseT_Full);
1225a8e8b985SFlorian Fainelli 	}
1226a8e8b985SFlorian Fainelli 
1227a8e8b985SFlorian Fainelli 	bitmap_and(supported, supported, mask,
1228a8e8b985SFlorian Fainelli 		   __ETHTOOL_LINK_MODE_MASK_NBITS);
1229a8e8b985SFlorian Fainelli 	bitmap_and(state->advertising, state->advertising, mask,
1230a8e8b985SFlorian Fainelli 		   __ETHTOOL_LINK_MODE_MASK_NBITS);
1231a8e8b985SFlorian Fainelli 
1232a8e8b985SFlorian Fainelli 	phylink_helper_basex_speed(state);
1233a8e8b985SFlorian Fainelli }
1234a8e8b985SFlorian Fainelli EXPORT_SYMBOL(b53_phylink_validate);
1235a8e8b985SFlorian Fainelli 
1236a8e8b985SFlorian Fainelli int b53_phylink_mac_link_state(struct dsa_switch *ds, int port,
1237a8e8b985SFlorian Fainelli 			       struct phylink_link_state *state)
1238a8e8b985SFlorian Fainelli {
12390e01491dSFlorian Fainelli 	struct b53_device *dev = ds->priv;
1240a8e8b985SFlorian Fainelli 	int ret = -EOPNOTSUPP;
1241a8e8b985SFlorian Fainelli 
124255a4d2eaSFlorian Fainelli 	if ((phy_interface_mode_is_8023z(state->interface) ||
124355a4d2eaSFlorian Fainelli 	     state->interface == PHY_INTERFACE_MODE_SGMII) &&
12440e01491dSFlorian Fainelli 	     dev->ops->serdes_link_state)
12450e01491dSFlorian Fainelli 		ret = dev->ops->serdes_link_state(dev, port, state);
12460e01491dSFlorian Fainelli 
1247a8e8b985SFlorian Fainelli 	return ret;
1248a8e8b985SFlorian Fainelli }
1249a8e8b985SFlorian Fainelli EXPORT_SYMBOL(b53_phylink_mac_link_state);
1250a8e8b985SFlorian Fainelli 
1251a8e8b985SFlorian Fainelli void b53_phylink_mac_config(struct dsa_switch *ds, int port,
1252a8e8b985SFlorian Fainelli 			    unsigned int mode,
1253a8e8b985SFlorian Fainelli 			    const struct phylink_link_state *state)
1254a8e8b985SFlorian Fainelli {
1255a8e8b985SFlorian Fainelli 	struct b53_device *dev = ds->priv;
1256a8e8b985SFlorian Fainelli 
1257ab017b79SRussell King 	if (mode == MLO_AN_PHY || mode == MLO_AN_FIXED)
1258a8e8b985SFlorian Fainelli 		return;
1259a8e8b985SFlorian Fainelli 
126055a4d2eaSFlorian Fainelli 	if ((phy_interface_mode_is_8023z(state->interface) ||
126155a4d2eaSFlorian Fainelli 	     state->interface == PHY_INTERFACE_MODE_SGMII) &&
12620e01491dSFlorian Fainelli 	     dev->ops->serdes_config)
12630e01491dSFlorian Fainelli 		dev->ops->serdes_config(dev, port, mode, state);
1264a8e8b985SFlorian Fainelli }
1265a8e8b985SFlorian Fainelli EXPORT_SYMBOL(b53_phylink_mac_config);
1266a8e8b985SFlorian Fainelli 
1267a8e8b985SFlorian Fainelli void b53_phylink_mac_an_restart(struct dsa_switch *ds, int port)
1268a8e8b985SFlorian Fainelli {
12690e01491dSFlorian Fainelli 	struct b53_device *dev = ds->priv;
12700e01491dSFlorian Fainelli 
12710e01491dSFlorian Fainelli 	if (dev->ops->serdes_an_restart)
12720e01491dSFlorian Fainelli 		dev->ops->serdes_an_restart(dev, port);
1273a8e8b985SFlorian Fainelli }
1274a8e8b985SFlorian Fainelli EXPORT_SYMBOL(b53_phylink_mac_an_restart);
1275a8e8b985SFlorian Fainelli 
1276a8e8b985SFlorian Fainelli void b53_phylink_mac_link_down(struct dsa_switch *ds, int port,
1277a8e8b985SFlorian Fainelli 			       unsigned int mode,
1278a8e8b985SFlorian Fainelli 			       phy_interface_t interface)
1279a8e8b985SFlorian Fainelli {
1280a8e8b985SFlorian Fainelli 	struct b53_device *dev = ds->priv;
1281a8e8b985SFlorian Fainelli 
1282a8e8b985SFlorian Fainelli 	if (mode == MLO_AN_PHY)
1283a8e8b985SFlorian Fainelli 		return;
1284a8e8b985SFlorian Fainelli 
1285a8e8b985SFlorian Fainelli 	if (mode == MLO_AN_FIXED) {
1286a8e8b985SFlorian Fainelli 		b53_force_link(dev, port, false);
1287a8e8b985SFlorian Fainelli 		return;
1288a8e8b985SFlorian Fainelli 	}
12890e01491dSFlorian Fainelli 
12900e01491dSFlorian Fainelli 	if (phy_interface_mode_is_8023z(interface) &&
12910e01491dSFlorian Fainelli 	    dev->ops->serdes_link_set)
12920e01491dSFlorian Fainelli 		dev->ops->serdes_link_set(dev, port, mode, interface, false);
1293a8e8b985SFlorian Fainelli }
1294a8e8b985SFlorian Fainelli EXPORT_SYMBOL(b53_phylink_mac_link_down);
1295a8e8b985SFlorian Fainelli 
1296a8e8b985SFlorian Fainelli void b53_phylink_mac_link_up(struct dsa_switch *ds, int port,
1297a8e8b985SFlorian Fainelli 			     unsigned int mode,
1298a8e8b985SFlorian Fainelli 			     phy_interface_t interface,
12995b502a7bSRussell King 			     struct phy_device *phydev,
13005b502a7bSRussell King 			     int speed, int duplex,
13015b502a7bSRussell King 			     bool tx_pause, bool rx_pause)
1302a8e8b985SFlorian Fainelli {
1303a8e8b985SFlorian Fainelli 	struct b53_device *dev = ds->priv;
1304a8e8b985SFlorian Fainelli 
1305a8e8b985SFlorian Fainelli 	if (mode == MLO_AN_PHY)
1306a8e8b985SFlorian Fainelli 		return;
1307a8e8b985SFlorian Fainelli 
1308a8e8b985SFlorian Fainelli 	if (mode == MLO_AN_FIXED) {
1309ab017b79SRussell King 		b53_force_port_config(dev, port, speed, duplex,
1310ab017b79SRussell King 				      tx_pause, rx_pause);
1311a8e8b985SFlorian Fainelli 		b53_force_link(dev, port, true);
1312a8e8b985SFlorian Fainelli 		return;
1313a8e8b985SFlorian Fainelli 	}
13140e01491dSFlorian Fainelli 
13150e01491dSFlorian Fainelli 	if (phy_interface_mode_is_8023z(interface) &&
13160e01491dSFlorian Fainelli 	    dev->ops->serdes_link_set)
13170e01491dSFlorian Fainelli 		dev->ops->serdes_link_set(dev, port, mode, interface, true);
1318a8e8b985SFlorian Fainelli }
1319a8e8b985SFlorian Fainelli EXPORT_SYMBOL(b53_phylink_mac_link_up);
1320a8e8b985SFlorian Fainelli 
13213117455dSFlorian Fainelli int b53_vlan_filtering(struct dsa_switch *ds, int port, bool vlan_filtering)
1322a2482d2cSFlorian Fainelli {
1323dad8d7c6SFlorian Fainelli 	struct b53_device *dev = ds->priv;
1324dad8d7c6SFlorian Fainelli 	u16 pvid, new_pvid;
1325dad8d7c6SFlorian Fainelli 
1326dad8d7c6SFlorian Fainelli 	b53_read16(dev, B53_VLAN_PAGE, B53_VLAN_PORT_DEF_TAG(port), &pvid);
1327864cd7b0SVladimir Oltean 	if (!vlan_filtering) {
1328dad8d7c6SFlorian Fainelli 		/* Filtering is currently enabled, use the default PVID since
1329dad8d7c6SFlorian Fainelli 		 * the bridge does not expect tagging anymore
1330dad8d7c6SFlorian Fainelli 		 */
1331dad8d7c6SFlorian Fainelli 		dev->ports[port].pvid = pvid;
1332dad8d7c6SFlorian Fainelli 		new_pvid = b53_default_pvid(dev);
1333864cd7b0SVladimir Oltean 	} else {
1334dad8d7c6SFlorian Fainelli 		/* Filtering is currently disabled, restore the previous PVID */
1335dad8d7c6SFlorian Fainelli 		new_pvid = dev->ports[port].pvid;
1336dad8d7c6SFlorian Fainelli 	}
1337dad8d7c6SFlorian Fainelli 
1338dad8d7c6SFlorian Fainelli 	if (pvid != new_pvid)
1339dad8d7c6SFlorian Fainelli 		b53_write16(dev, B53_VLAN_PAGE, B53_VLAN_PORT_DEF_TAG(port),
1340dad8d7c6SFlorian Fainelli 			    new_pvid);
1341dad8d7c6SFlorian Fainelli 
1342dad8d7c6SFlorian Fainelli 	b53_enable_vlan(dev, dev->vlan_enabled, vlan_filtering);
1343dad8d7c6SFlorian Fainelli 
1344a2482d2cSFlorian Fainelli 	return 0;
1345a2482d2cSFlorian Fainelli }
13463117455dSFlorian Fainelli EXPORT_SYMBOL(b53_vlan_filtering);
1347a2482d2cSFlorian Fainelli 
13483117455dSFlorian Fainelli int b53_vlan_prepare(struct dsa_switch *ds, int port,
134980e02360SVivien Didelot 		     const struct switchdev_obj_port_vlan *vlan)
1350a2482d2cSFlorian Fainelli {
135104bed143SVivien Didelot 	struct b53_device *dev = ds->priv;
1352a2482d2cSFlorian Fainelli 
1353a2482d2cSFlorian Fainelli 	if ((is5325(dev) || is5365(dev)) && vlan->vid_begin == 0)
1354a2482d2cSFlorian Fainelli 		return -EOPNOTSUPP;
1355a2482d2cSFlorian Fainelli 
135688631864SFlorian Fainelli 	/* Port 7 on 7278 connects to the ASP's UniMAC which is not capable of
135788631864SFlorian Fainelli 	 * receiving VLAN tagged frames at all, we can still allow the port to
135888631864SFlorian Fainelli 	 * be configured for egress untagged.
135988631864SFlorian Fainelli 	 */
136088631864SFlorian Fainelli 	if (dev->chip_id == BCM7278_DEVICE_ID && port == 7 &&
136188631864SFlorian Fainelli 	    !(vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED))
136288631864SFlorian Fainelli 		return -EINVAL;
136388631864SFlorian Fainelli 
1364a2482d2cSFlorian Fainelli 	if (vlan->vid_end > dev->num_vlans)
1365a2482d2cSFlorian Fainelli 		return -ERANGE;
1366a2482d2cSFlorian Fainelli 
1367e74f014eSVladimir Oltean 	b53_enable_vlan(dev, true, ds->vlan_filtering);
1368a2482d2cSFlorian Fainelli 
1369a2482d2cSFlorian Fainelli 	return 0;
1370a2482d2cSFlorian Fainelli }
13713117455dSFlorian Fainelli EXPORT_SYMBOL(b53_vlan_prepare);
1372a2482d2cSFlorian Fainelli 
13733117455dSFlorian Fainelli void b53_vlan_add(struct dsa_switch *ds, int port,
137480e02360SVivien Didelot 		  const struct switchdev_obj_port_vlan *vlan)
1375a2482d2cSFlorian Fainelli {
137604bed143SVivien Didelot 	struct b53_device *dev = ds->priv;
1377a2482d2cSFlorian Fainelli 	bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
1378a2482d2cSFlorian Fainelli 	bool pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
1379a2482d2cSFlorian Fainelli 	struct b53_vlan *vl;
1380a2482d2cSFlorian Fainelli 	u16 vid;
1381a2482d2cSFlorian Fainelli 
1382a2482d2cSFlorian Fainelli 	for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid) {
1383a2482d2cSFlorian Fainelli 		vl = &dev->vlans[vid];
1384a2482d2cSFlorian Fainelli 
1385a2482d2cSFlorian Fainelli 		b53_get_vlan_entry(dev, vid, vl);
1386a2482d2cSFlorian Fainelli 
1387d965a543SFlorian Fainelli 		if (vid == 0 && vid == b53_default_pvid(dev))
1388d965a543SFlorian Fainelli 			untagged = true;
1389d965a543SFlorian Fainelli 
1390c499696eSFlorian Fainelli 		vl->members |= BIT(port);
1391ca893194SFlorian Fainelli 		if (untagged && !dsa_is_cpu_port(ds, port))
1392e47112d9SFlorian Fainelli 			vl->untag |= BIT(port);
1393a2482d2cSFlorian Fainelli 		else
1394e47112d9SFlorian Fainelli 			vl->untag &= ~BIT(port);
1395a2482d2cSFlorian Fainelli 
1396a2482d2cSFlorian Fainelli 		b53_set_vlan_entry(dev, vid, vl);
1397a2482d2cSFlorian Fainelli 		b53_fast_age_vlan(dev, vid);
1398a2482d2cSFlorian Fainelli 	}
1399a2482d2cSFlorian Fainelli 
140010163aaeSFlorian Fainelli 	if (pvid && !dsa_is_cpu_port(ds, port)) {
1401a2482d2cSFlorian Fainelli 		b53_write16(dev, B53_VLAN_PAGE, B53_VLAN_PORT_DEF_TAG(port),
1402a2482d2cSFlorian Fainelli 			    vlan->vid_end);
1403a2482d2cSFlorian Fainelli 		b53_fast_age_vlan(dev, vid);
1404a2482d2cSFlorian Fainelli 	}
1405a2482d2cSFlorian Fainelli }
14063117455dSFlorian Fainelli EXPORT_SYMBOL(b53_vlan_add);
1407a2482d2cSFlorian Fainelli 
14083117455dSFlorian Fainelli int b53_vlan_del(struct dsa_switch *ds, int port,
1409a2482d2cSFlorian Fainelli 		 const struct switchdev_obj_port_vlan *vlan)
1410a2482d2cSFlorian Fainelli {
141104bed143SVivien Didelot 	struct b53_device *dev = ds->priv;
1412a2482d2cSFlorian Fainelli 	bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
1413a2482d2cSFlorian Fainelli 	struct b53_vlan *vl;
1414a2482d2cSFlorian Fainelli 	u16 vid;
1415a2482d2cSFlorian Fainelli 	u16 pvid;
1416a2482d2cSFlorian Fainelli 
1417a2482d2cSFlorian Fainelli 	b53_read16(dev, B53_VLAN_PAGE, B53_VLAN_PORT_DEF_TAG(port), &pvid);
1418a2482d2cSFlorian Fainelli 
1419a2482d2cSFlorian Fainelli 	for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid) {
1420a2482d2cSFlorian Fainelli 		vl = &dev->vlans[vid];
1421a2482d2cSFlorian Fainelli 
1422a2482d2cSFlorian Fainelli 		b53_get_vlan_entry(dev, vid, vl);
1423a2482d2cSFlorian Fainelli 
1424a2482d2cSFlorian Fainelli 		vl->members &= ~BIT(port);
1425a2482d2cSFlorian Fainelli 
1426fea83353SFlorian Fainelli 		if (pvid == vid)
1427fea83353SFlorian Fainelli 			pvid = b53_default_pvid(dev);
1428a2482d2cSFlorian Fainelli 
1429ca893194SFlorian Fainelli 		if (untagged && !dsa_is_cpu_port(ds, port))
1430a2482d2cSFlorian Fainelli 			vl->untag &= ~(BIT(port));
1431a2482d2cSFlorian Fainelli 
1432a2482d2cSFlorian Fainelli 		b53_set_vlan_entry(dev, vid, vl);
1433a2482d2cSFlorian Fainelli 		b53_fast_age_vlan(dev, vid);
1434a2482d2cSFlorian Fainelli 	}
1435a2482d2cSFlorian Fainelli 
1436a2482d2cSFlorian Fainelli 	b53_write16(dev, B53_VLAN_PAGE, B53_VLAN_PORT_DEF_TAG(port), pvid);
1437a2482d2cSFlorian Fainelli 	b53_fast_age_vlan(dev, pvid);
1438a2482d2cSFlorian Fainelli 
1439a2482d2cSFlorian Fainelli 	return 0;
1440a2482d2cSFlorian Fainelli }
14413117455dSFlorian Fainelli EXPORT_SYMBOL(b53_vlan_del);
1442a2482d2cSFlorian Fainelli 
14431da6df85SFlorian Fainelli /* Address Resolution Logic routines */
14441da6df85SFlorian Fainelli static int b53_arl_op_wait(struct b53_device *dev)
14451da6df85SFlorian Fainelli {
14461da6df85SFlorian Fainelli 	unsigned int timeout = 10;
14471da6df85SFlorian Fainelli 	u8 reg;
14481da6df85SFlorian Fainelli 
14491da6df85SFlorian Fainelli 	do {
14501da6df85SFlorian Fainelli 		b53_read8(dev, B53_ARLIO_PAGE, B53_ARLTBL_RW_CTRL, &reg);
14511da6df85SFlorian Fainelli 		if (!(reg & ARLTBL_START_DONE))
14521da6df85SFlorian Fainelli 			return 0;
14531da6df85SFlorian Fainelli 
14541da6df85SFlorian Fainelli 		usleep_range(1000, 2000);
14551da6df85SFlorian Fainelli 	} while (timeout--);
14561da6df85SFlorian Fainelli 
14571da6df85SFlorian Fainelli 	dev_warn(dev->dev, "timeout waiting for ARL to finish: 0x%02x\n", reg);
14581da6df85SFlorian Fainelli 
14591da6df85SFlorian Fainelli 	return -ETIMEDOUT;
14601da6df85SFlorian Fainelli }
14611da6df85SFlorian Fainelli 
14621da6df85SFlorian Fainelli static int b53_arl_rw_op(struct b53_device *dev, unsigned int op)
14631da6df85SFlorian Fainelli {
14641da6df85SFlorian Fainelli 	u8 reg;
14651da6df85SFlorian Fainelli 
14661da6df85SFlorian Fainelli 	if (op > ARLTBL_RW)
14671da6df85SFlorian Fainelli 		return -EINVAL;
14681da6df85SFlorian Fainelli 
14691da6df85SFlorian Fainelli 	b53_read8(dev, B53_ARLIO_PAGE, B53_ARLTBL_RW_CTRL, &reg);
14701da6df85SFlorian Fainelli 	reg |= ARLTBL_START_DONE;
14711da6df85SFlorian Fainelli 	if (op)
14721da6df85SFlorian Fainelli 		reg |= ARLTBL_RW;
14731da6df85SFlorian Fainelli 	else
14741da6df85SFlorian Fainelli 		reg &= ~ARLTBL_RW;
147564fec949SFlorian Fainelli 	if (dev->vlan_enabled)
147664fec949SFlorian Fainelli 		reg &= ~ARLTBL_IVL_SVL_SELECT;
147764fec949SFlorian Fainelli 	else
147864fec949SFlorian Fainelli 		reg |= ARLTBL_IVL_SVL_SELECT;
14791da6df85SFlorian Fainelli 	b53_write8(dev, B53_ARLIO_PAGE, B53_ARLTBL_RW_CTRL, reg);
14801da6df85SFlorian Fainelli 
14811da6df85SFlorian Fainelli 	return b53_arl_op_wait(dev);
14821da6df85SFlorian Fainelli }
14831da6df85SFlorian Fainelli 
14841da6df85SFlorian Fainelli static int b53_arl_read(struct b53_device *dev, u64 mac,
1485ef2a0bd9SFlorian Fainelli 			u16 vid, struct b53_arl_entry *ent, u8 *idx)
14861da6df85SFlorian Fainelli {
14876344dbdeSFlorian Fainelli 	DECLARE_BITMAP(free_bins, B53_ARLTBL_MAX_BIN_ENTRIES);
14881da6df85SFlorian Fainelli 	unsigned int i;
14891da6df85SFlorian Fainelli 	int ret;
14901da6df85SFlorian Fainelli 
14911da6df85SFlorian Fainelli 	ret = b53_arl_op_wait(dev);
14921da6df85SFlorian Fainelli 	if (ret)
14931da6df85SFlorian Fainelli 		return ret;
14941da6df85SFlorian Fainelli 
1495673e69a6SFlorian Fainelli 	bitmap_zero(free_bins, dev->num_arl_bins);
14966344dbdeSFlorian Fainelli 
14971da6df85SFlorian Fainelli 	/* Read the bins */
1498673e69a6SFlorian Fainelli 	for (i = 0; i < dev->num_arl_bins; i++) {
14991da6df85SFlorian Fainelli 		u64 mac_vid;
15001da6df85SFlorian Fainelli 		u32 fwd_entry;
15011da6df85SFlorian Fainelli 
15021da6df85SFlorian Fainelli 		b53_read64(dev, B53_ARLIO_PAGE,
15031da6df85SFlorian Fainelli 			   B53_ARLTBL_MAC_VID_ENTRY(i), &mac_vid);
15041da6df85SFlorian Fainelli 		b53_read32(dev, B53_ARLIO_PAGE,
15051da6df85SFlorian Fainelli 			   B53_ARLTBL_DATA_ENTRY(i), &fwd_entry);
15061da6df85SFlorian Fainelli 		b53_arl_to_entry(ent, mac_vid, fwd_entry);
15071da6df85SFlorian Fainelli 
15086344dbdeSFlorian Fainelli 		if (!(fwd_entry & ARLTBL_VALID)) {
15096344dbdeSFlorian Fainelli 			set_bit(i, free_bins);
15101da6df85SFlorian Fainelli 			continue;
15116344dbdeSFlorian Fainelli 		}
15121da6df85SFlorian Fainelli 		if ((mac_vid & ARLTBL_MAC_MASK) != mac)
15131da6df85SFlorian Fainelli 			continue;
15142e97b0cdSFlorian Fainelli 		if (dev->vlan_enabled &&
15152e97b0cdSFlorian Fainelli 		    ((mac_vid >> ARLTBL_VID_S) & ARLTBL_VID_MASK) != vid)
15162e97b0cdSFlorian Fainelli 			continue;
15171da6df85SFlorian Fainelli 		*idx = i;
15186344dbdeSFlorian Fainelli 		return 0;
15191da6df85SFlorian Fainelli 	}
15201da6df85SFlorian Fainelli 
1521673e69a6SFlorian Fainelli 	if (bitmap_weight(free_bins, dev->num_arl_bins) == 0)
15226344dbdeSFlorian Fainelli 		return -ENOSPC;
15236344dbdeSFlorian Fainelli 
1524673e69a6SFlorian Fainelli 	*idx = find_first_bit(free_bins, dev->num_arl_bins);
15256344dbdeSFlorian Fainelli 
15261da6df85SFlorian Fainelli 	return -ENOENT;
15271da6df85SFlorian Fainelli }
15281da6df85SFlorian Fainelli 
15291da6df85SFlorian Fainelli static int b53_arl_op(struct b53_device *dev, int op, int port,
15301da6df85SFlorian Fainelli 		      const unsigned char *addr, u16 vid, bool is_valid)
15311da6df85SFlorian Fainelli {
15321da6df85SFlorian Fainelli 	struct b53_arl_entry ent;
15331da6df85SFlorian Fainelli 	u32 fwd_entry;
15341da6df85SFlorian Fainelli 	u64 mac, mac_vid = 0;
15351da6df85SFlorian Fainelli 	u8 idx = 0;
15361da6df85SFlorian Fainelli 	int ret;
15371da6df85SFlorian Fainelli 
15381da6df85SFlorian Fainelli 	/* Convert the array into a 64-bit MAC */
15394b92ea81SFlorian Fainelli 	mac = ether_addr_to_u64(addr);
15401da6df85SFlorian Fainelli 
15411da6df85SFlorian Fainelli 	/* Perform a read for the given MAC and VID */
15421da6df85SFlorian Fainelli 	b53_write48(dev, B53_ARLIO_PAGE, B53_MAC_ADDR_IDX, mac);
15431da6df85SFlorian Fainelli 	b53_write16(dev, B53_ARLIO_PAGE, B53_VLAN_ID_IDX, vid);
15441da6df85SFlorian Fainelli 
15451da6df85SFlorian Fainelli 	/* Issue a read operation for this MAC */
15461da6df85SFlorian Fainelli 	ret = b53_arl_rw_op(dev, 1);
15471da6df85SFlorian Fainelli 	if (ret)
15481da6df85SFlorian Fainelli 		return ret;
15491da6df85SFlorian Fainelli 
1550ef2a0bd9SFlorian Fainelli 	ret = b53_arl_read(dev, mac, vid, &ent, &idx);
1551ef2a0bd9SFlorian Fainelli 
15521da6df85SFlorian Fainelli 	/* If this is a read, just finish now */
15531da6df85SFlorian Fainelli 	if (op)
15541da6df85SFlorian Fainelli 		return ret;
15551da6df85SFlorian Fainelli 
15566344dbdeSFlorian Fainelli 	switch (ret) {
1557774d977aSTom Rix 	case -ETIMEDOUT:
1558774d977aSTom Rix 		return ret;
15596344dbdeSFlorian Fainelli 	case -ENOSPC:
15606344dbdeSFlorian Fainelli 		dev_dbg(dev->dev, "{%pM,%.4d} no space left in ARL\n",
15616344dbdeSFlorian Fainelli 			addr, vid);
15626344dbdeSFlorian Fainelli 		return is_valid ? ret : 0;
15636344dbdeSFlorian Fainelli 	case -ENOENT:
15641da6df85SFlorian Fainelli 		/* We could not find a matching MAC, so reset to a new entry */
15656344dbdeSFlorian Fainelli 		dev_dbg(dev->dev, "{%pM,%.4d} not found, using idx: %d\n",
15666344dbdeSFlorian Fainelli 			addr, vid, idx);
15671da6df85SFlorian Fainelli 		fwd_entry = 0;
15686344dbdeSFlorian Fainelli 		break;
15696344dbdeSFlorian Fainelli 	default:
15706344dbdeSFlorian Fainelli 		dev_dbg(dev->dev, "{%pM,%.4d} found, using idx: %d\n",
15716344dbdeSFlorian Fainelli 			addr, vid, idx);
15726344dbdeSFlorian Fainelli 		break;
15731da6df85SFlorian Fainelli 	}
15741da6df85SFlorian Fainelli 
15755d65b64aSFlorian Fainelli 	/* For multicast address, the port is a bitmask and the validity
15765d65b64aSFlorian Fainelli 	 * is determined by having at least one port being still active
15775d65b64aSFlorian Fainelli 	 */
15785d65b64aSFlorian Fainelli 	if (!is_multicast_ether_addr(addr)) {
15791da6df85SFlorian Fainelli 		ent.port = port;
15801da6df85SFlorian Fainelli 		ent.is_valid = is_valid;
15815d65b64aSFlorian Fainelli 	} else {
15825d65b64aSFlorian Fainelli 		if (is_valid)
15835d65b64aSFlorian Fainelli 			ent.port |= BIT(port);
15845d65b64aSFlorian Fainelli 		else
15855d65b64aSFlorian Fainelli 			ent.port &= ~BIT(port);
15865d65b64aSFlorian Fainelli 
15875d65b64aSFlorian Fainelli 		ent.is_valid = !!(ent.port);
15885d65b64aSFlorian Fainelli 	}
15895d65b64aSFlorian Fainelli 
15901da6df85SFlorian Fainelli 	ent.vid = vid;
15911da6df85SFlorian Fainelli 	ent.is_static = true;
15925d65b64aSFlorian Fainelli 	ent.is_age = false;
15931da6df85SFlorian Fainelli 	memcpy(ent.mac, addr, ETH_ALEN);
15941da6df85SFlorian Fainelli 	b53_arl_from_entry(&mac_vid, &fwd_entry, &ent);
15951da6df85SFlorian Fainelli 
15961da6df85SFlorian Fainelli 	b53_write64(dev, B53_ARLIO_PAGE,
15971da6df85SFlorian Fainelli 		    B53_ARLTBL_MAC_VID_ENTRY(idx), mac_vid);
15981da6df85SFlorian Fainelli 	b53_write32(dev, B53_ARLIO_PAGE,
15991da6df85SFlorian Fainelli 		    B53_ARLTBL_DATA_ENTRY(idx), fwd_entry);
16001da6df85SFlorian Fainelli 
16011da6df85SFlorian Fainelli 	return b53_arl_rw_op(dev, 0);
16021da6df85SFlorian Fainelli }
16031da6df85SFlorian Fainelli 
16041b6dd556SArkadi Sharshevsky int b53_fdb_add(struct dsa_switch *ds, int port,
16056c2c1dcbSArkadi Sharshevsky 		const unsigned char *addr, u16 vid)
16061da6df85SFlorian Fainelli {
160704bed143SVivien Didelot 	struct b53_device *priv = ds->priv;
16081da6df85SFlorian Fainelli 
16091da6df85SFlorian Fainelli 	/* 5325 and 5365 require some more massaging, but could
16101da6df85SFlorian Fainelli 	 * be supported eventually
16111da6df85SFlorian Fainelli 	 */
16121da6df85SFlorian Fainelli 	if (is5325(priv) || is5365(priv))
16131da6df85SFlorian Fainelli 		return -EOPNOTSUPP;
16141da6df85SFlorian Fainelli 
16151b6dd556SArkadi Sharshevsky 	return b53_arl_op(priv, 0, port, addr, vid, true);
16161da6df85SFlorian Fainelli }
16173117455dSFlorian Fainelli EXPORT_SYMBOL(b53_fdb_add);
16181da6df85SFlorian Fainelli 
16193117455dSFlorian Fainelli int b53_fdb_del(struct dsa_switch *ds, int port,
16206c2c1dcbSArkadi Sharshevsky 		const unsigned char *addr, u16 vid)
16211da6df85SFlorian Fainelli {
162204bed143SVivien Didelot 	struct b53_device *priv = ds->priv;
16231da6df85SFlorian Fainelli 
16246c2c1dcbSArkadi Sharshevsky 	return b53_arl_op(priv, 0, port, addr, vid, false);
16251da6df85SFlorian Fainelli }
16263117455dSFlorian Fainelli EXPORT_SYMBOL(b53_fdb_del);
16271da6df85SFlorian Fainelli 
16281da6df85SFlorian Fainelli static int b53_arl_search_wait(struct b53_device *dev)
16291da6df85SFlorian Fainelli {
16301da6df85SFlorian Fainelli 	unsigned int timeout = 1000;
16311da6df85SFlorian Fainelli 	u8 reg;
16321da6df85SFlorian Fainelli 
16331da6df85SFlorian Fainelli 	do {
16341da6df85SFlorian Fainelli 		b53_read8(dev, B53_ARLIO_PAGE, B53_ARL_SRCH_CTL, &reg);
16351da6df85SFlorian Fainelli 		if (!(reg & ARL_SRCH_STDN))
16361da6df85SFlorian Fainelli 			return 0;
16371da6df85SFlorian Fainelli 
16381da6df85SFlorian Fainelli 		if (reg & ARL_SRCH_VLID)
16391da6df85SFlorian Fainelli 			return 0;
16401da6df85SFlorian Fainelli 
16411da6df85SFlorian Fainelli 		usleep_range(1000, 2000);
16421da6df85SFlorian Fainelli 	} while (timeout--);
16431da6df85SFlorian Fainelli 
16441da6df85SFlorian Fainelli 	return -ETIMEDOUT;
16451da6df85SFlorian Fainelli }
16461da6df85SFlorian Fainelli 
16471da6df85SFlorian Fainelli static void b53_arl_search_rd(struct b53_device *dev, u8 idx,
16481da6df85SFlorian Fainelli 			      struct b53_arl_entry *ent)
16491da6df85SFlorian Fainelli {
16501da6df85SFlorian Fainelli 	u64 mac_vid;
16511da6df85SFlorian Fainelli 	u32 fwd_entry;
16521da6df85SFlorian Fainelli 
16531da6df85SFlorian Fainelli 	b53_read64(dev, B53_ARLIO_PAGE,
16541da6df85SFlorian Fainelli 		   B53_ARL_SRCH_RSTL_MACVID(idx), &mac_vid);
16551da6df85SFlorian Fainelli 	b53_read32(dev, B53_ARLIO_PAGE,
16561da6df85SFlorian Fainelli 		   B53_ARL_SRCH_RSTL(idx), &fwd_entry);
16571da6df85SFlorian Fainelli 	b53_arl_to_entry(ent, mac_vid, fwd_entry);
16581da6df85SFlorian Fainelli }
16591da6df85SFlorian Fainelli 
1660e6cbef0cSVivien Didelot static int b53_fdb_copy(int port, const struct b53_arl_entry *ent,
16612bedde1aSArkadi Sharshevsky 			dsa_fdb_dump_cb_t *cb, void *data)
16621da6df85SFlorian Fainelli {
16631da6df85SFlorian Fainelli 	if (!ent->is_valid)
16641da6df85SFlorian Fainelli 		return 0;
16651da6df85SFlorian Fainelli 
16661da6df85SFlorian Fainelli 	if (port != ent->port)
16671da6df85SFlorian Fainelli 		return 0;
16681da6df85SFlorian Fainelli 
16692bedde1aSArkadi Sharshevsky 	return cb(ent->mac, ent->vid, ent->is_static, data);
16701da6df85SFlorian Fainelli }
16711da6df85SFlorian Fainelli 
16723117455dSFlorian Fainelli int b53_fdb_dump(struct dsa_switch *ds, int port,
16732bedde1aSArkadi Sharshevsky 		 dsa_fdb_dump_cb_t *cb, void *data)
16741da6df85SFlorian Fainelli {
167504bed143SVivien Didelot 	struct b53_device *priv = ds->priv;
16761da6df85SFlorian Fainelli 	struct b53_arl_entry results[2];
16771da6df85SFlorian Fainelli 	unsigned int count = 0;
16781da6df85SFlorian Fainelli 	int ret;
16791da6df85SFlorian Fainelli 	u8 reg;
16801da6df85SFlorian Fainelli 
16811da6df85SFlorian Fainelli 	/* Start search operation */
16821da6df85SFlorian Fainelli 	reg = ARL_SRCH_STDN;
16831da6df85SFlorian Fainelli 	b53_write8(priv, B53_ARLIO_PAGE, B53_ARL_SRCH_CTL, reg);
16841da6df85SFlorian Fainelli 
16851da6df85SFlorian Fainelli 	do {
16861da6df85SFlorian Fainelli 		ret = b53_arl_search_wait(priv);
16871da6df85SFlorian Fainelli 		if (ret)
16881da6df85SFlorian Fainelli 			return ret;
16891da6df85SFlorian Fainelli 
16901da6df85SFlorian Fainelli 		b53_arl_search_rd(priv, 0, &results[0]);
16912bedde1aSArkadi Sharshevsky 		ret = b53_fdb_copy(port, &results[0], cb, data);
16921da6df85SFlorian Fainelli 		if (ret)
16931da6df85SFlorian Fainelli 			return ret;
16941da6df85SFlorian Fainelli 
1695673e69a6SFlorian Fainelli 		if (priv->num_arl_bins > 2) {
16961da6df85SFlorian Fainelli 			b53_arl_search_rd(priv, 1, &results[1]);
16972bedde1aSArkadi Sharshevsky 			ret = b53_fdb_copy(port, &results[1], cb, data);
16981da6df85SFlorian Fainelli 			if (ret)
16991da6df85SFlorian Fainelli 				return ret;
17001da6df85SFlorian Fainelli 
17011da6df85SFlorian Fainelli 			if (!results[0].is_valid && !results[1].is_valid)
17021da6df85SFlorian Fainelli 				break;
17031da6df85SFlorian Fainelli 		}
17041da6df85SFlorian Fainelli 
1705cd169d79SFlorian Fainelli 	} while (count++ < b53_max_arl_entries(priv) / 2);
17061da6df85SFlorian Fainelli 
17071da6df85SFlorian Fainelli 	return 0;
17081da6df85SFlorian Fainelli }
17093117455dSFlorian Fainelli EXPORT_SYMBOL(b53_fdb_dump);
17101da6df85SFlorian Fainelli 
17115d65b64aSFlorian Fainelli int b53_mdb_prepare(struct dsa_switch *ds, int port,
17125d65b64aSFlorian Fainelli 		    const struct switchdev_obj_port_mdb *mdb)
17135d65b64aSFlorian Fainelli {
17145d65b64aSFlorian Fainelli 	struct b53_device *priv = ds->priv;
17155d65b64aSFlorian Fainelli 
17165d65b64aSFlorian Fainelli 	/* 5325 and 5365 require some more massaging, but could
17175d65b64aSFlorian Fainelli 	 * be supported eventually
17185d65b64aSFlorian Fainelli 	 */
17195d65b64aSFlorian Fainelli 	if (is5325(priv) || is5365(priv))
17205d65b64aSFlorian Fainelli 		return -EOPNOTSUPP;
17215d65b64aSFlorian Fainelli 
17225d65b64aSFlorian Fainelli 	return 0;
17235d65b64aSFlorian Fainelli }
17245d65b64aSFlorian Fainelli EXPORT_SYMBOL(b53_mdb_prepare);
17255d65b64aSFlorian Fainelli 
17265d65b64aSFlorian Fainelli void b53_mdb_add(struct dsa_switch *ds, int port,
17275d65b64aSFlorian Fainelli 		 const struct switchdev_obj_port_mdb *mdb)
17285d65b64aSFlorian Fainelli {
17295d65b64aSFlorian Fainelli 	struct b53_device *priv = ds->priv;
17305d65b64aSFlorian Fainelli 	int ret;
17315d65b64aSFlorian Fainelli 
17325d65b64aSFlorian Fainelli 	ret = b53_arl_op(priv, 0, port, mdb->addr, mdb->vid, true);
17335d65b64aSFlorian Fainelli 	if (ret)
17345d65b64aSFlorian Fainelli 		dev_err(ds->dev, "failed to add MDB entry\n");
17355d65b64aSFlorian Fainelli }
17365d65b64aSFlorian Fainelli EXPORT_SYMBOL(b53_mdb_add);
17375d65b64aSFlorian Fainelli 
17385d65b64aSFlorian Fainelli int b53_mdb_del(struct dsa_switch *ds, int port,
17395d65b64aSFlorian Fainelli 		const struct switchdev_obj_port_mdb *mdb)
17405d65b64aSFlorian Fainelli {
17415d65b64aSFlorian Fainelli 	struct b53_device *priv = ds->priv;
17425d65b64aSFlorian Fainelli 	int ret;
17435d65b64aSFlorian Fainelli 
17445d65b64aSFlorian Fainelli 	ret = b53_arl_op(priv, 0, port, mdb->addr, mdb->vid, false);
17455d65b64aSFlorian Fainelli 	if (ret)
17465d65b64aSFlorian Fainelli 		dev_err(ds->dev, "failed to delete MDB entry\n");
17475d65b64aSFlorian Fainelli 
17485d65b64aSFlorian Fainelli 	return ret;
17495d65b64aSFlorian Fainelli }
17505d65b64aSFlorian Fainelli EXPORT_SYMBOL(b53_mdb_del);
17515d65b64aSFlorian Fainelli 
1752ddd3a0c8SVivien Didelot int b53_br_join(struct dsa_switch *ds, int port, struct net_device *br)
1753ff39c2d6SFlorian Fainelli {
175404bed143SVivien Didelot 	struct b53_device *dev = ds->priv;
175568bb8ea8SVivien Didelot 	s8 cpu_port = dsa_to_port(ds, port)->cpu_dp->index;
1756ff39c2d6SFlorian Fainelli 	u16 pvlan, reg;
1757ff39c2d6SFlorian Fainelli 	unsigned int i;
1758ff39c2d6SFlorian Fainelli 
175931bfc2d4SFlorian Fainelli 	/* On 7278, port 7 which connects to the ASP should only receive
176031bfc2d4SFlorian Fainelli 	 * traffic from matching CFP rules.
176131bfc2d4SFlorian Fainelli 	 */
176231bfc2d4SFlorian Fainelli 	if (dev->chip_id == BCM7278_DEVICE_ID && port == 7)
176331bfc2d4SFlorian Fainelli 		return -EINVAL;
176431bfc2d4SFlorian Fainelli 
176548aea33aSFlorian Fainelli 	/* Make this port leave the all VLANs join since we will have proper
176648aea33aSFlorian Fainelli 	 * VLAN entries from now on
176748aea33aSFlorian Fainelli 	 */
176848aea33aSFlorian Fainelli 	if (is58xx(dev)) {
176948aea33aSFlorian Fainelli 		b53_read16(dev, B53_VLAN_PAGE, B53_JOIN_ALL_VLAN_EN, &reg);
177048aea33aSFlorian Fainelli 		reg &= ~BIT(port);
177148aea33aSFlorian Fainelli 		if ((reg & BIT(cpu_port)) == BIT(cpu_port))
177248aea33aSFlorian Fainelli 			reg &= ~BIT(cpu_port);
177348aea33aSFlorian Fainelli 		b53_write16(dev, B53_VLAN_PAGE, B53_JOIN_ALL_VLAN_EN, reg);
177448aea33aSFlorian Fainelli 	}
177548aea33aSFlorian Fainelli 
1776ff39c2d6SFlorian Fainelli 	b53_read16(dev, B53_PVLAN_PAGE, B53_PVLAN_PORT_MASK(port), &pvlan);
1777ff39c2d6SFlorian Fainelli 
1778ff39c2d6SFlorian Fainelli 	b53_for_each_port(dev, i) {
1779c8652c83SVivien Didelot 		if (dsa_to_port(ds, i)->bridge_dev != br)
1780ff39c2d6SFlorian Fainelli 			continue;
1781ff39c2d6SFlorian Fainelli 
1782ff39c2d6SFlorian Fainelli 		/* Add this local port to the remote port VLAN control
1783ff39c2d6SFlorian Fainelli 		 * membership and update the remote port bitmask
1784ff39c2d6SFlorian Fainelli 		 */
1785ff39c2d6SFlorian Fainelli 		b53_read16(dev, B53_PVLAN_PAGE, B53_PVLAN_PORT_MASK(i), &reg);
1786ff39c2d6SFlorian Fainelli 		reg |= BIT(port);
1787ff39c2d6SFlorian Fainelli 		b53_write16(dev, B53_PVLAN_PAGE, B53_PVLAN_PORT_MASK(i), reg);
1788ff39c2d6SFlorian Fainelli 		dev->ports[i].vlan_ctl_mask = reg;
1789ff39c2d6SFlorian Fainelli 
1790ff39c2d6SFlorian Fainelli 		pvlan |= BIT(i);
1791ff39c2d6SFlorian Fainelli 	}
1792ff39c2d6SFlorian Fainelli 
1793ff39c2d6SFlorian Fainelli 	/* Configure the local port VLAN control membership to include
1794ff39c2d6SFlorian Fainelli 	 * remote ports and update the local port bitmask
1795ff39c2d6SFlorian Fainelli 	 */
1796ff39c2d6SFlorian Fainelli 	b53_write16(dev, B53_PVLAN_PAGE, B53_PVLAN_PORT_MASK(port), pvlan);
1797ff39c2d6SFlorian Fainelli 	dev->ports[port].vlan_ctl_mask = pvlan;
1798ff39c2d6SFlorian Fainelli 
1799ff39c2d6SFlorian Fainelli 	return 0;
1800ff39c2d6SFlorian Fainelli }
18013117455dSFlorian Fainelli EXPORT_SYMBOL(b53_br_join);
1802ff39c2d6SFlorian Fainelli 
1803f123f2fbSVivien Didelot void b53_br_leave(struct dsa_switch *ds, int port, struct net_device *br)
1804ff39c2d6SFlorian Fainelli {
180504bed143SVivien Didelot 	struct b53_device *dev = ds->priv;
1806a2482d2cSFlorian Fainelli 	struct b53_vlan *vl = &dev->vlans[0];
180768bb8ea8SVivien Didelot 	s8 cpu_port = dsa_to_port(ds, port)->cpu_dp->index;
1808ff39c2d6SFlorian Fainelli 	unsigned int i;
1809a2482d2cSFlorian Fainelli 	u16 pvlan, reg, pvid;
1810ff39c2d6SFlorian Fainelli 
1811ff39c2d6SFlorian Fainelli 	b53_read16(dev, B53_PVLAN_PAGE, B53_PVLAN_PORT_MASK(port), &pvlan);
1812ff39c2d6SFlorian Fainelli 
1813ff39c2d6SFlorian Fainelli 	b53_for_each_port(dev, i) {
1814ff39c2d6SFlorian Fainelli 		/* Don't touch the remaining ports */
1815c8652c83SVivien Didelot 		if (dsa_to_port(ds, i)->bridge_dev != br)
1816ff39c2d6SFlorian Fainelli 			continue;
1817ff39c2d6SFlorian Fainelli 
1818ff39c2d6SFlorian Fainelli 		b53_read16(dev, B53_PVLAN_PAGE, B53_PVLAN_PORT_MASK(i), &reg);
1819ff39c2d6SFlorian Fainelli 		reg &= ~BIT(port);
1820ff39c2d6SFlorian Fainelli 		b53_write16(dev, B53_PVLAN_PAGE, B53_PVLAN_PORT_MASK(i), reg);
1821ff39c2d6SFlorian Fainelli 		dev->ports[port].vlan_ctl_mask = reg;
1822ff39c2d6SFlorian Fainelli 
1823ff39c2d6SFlorian Fainelli 		/* Prevent self removal to preserve isolation */
1824ff39c2d6SFlorian Fainelli 		if (port != i)
1825ff39c2d6SFlorian Fainelli 			pvlan &= ~BIT(i);
1826ff39c2d6SFlorian Fainelli 	}
1827ff39c2d6SFlorian Fainelli 
1828ff39c2d6SFlorian Fainelli 	b53_write16(dev, B53_PVLAN_PAGE, B53_PVLAN_PORT_MASK(port), pvlan);
1829ff39c2d6SFlorian Fainelli 	dev->ports[port].vlan_ctl_mask = pvlan;
1830a2482d2cSFlorian Fainelli 
1831fea83353SFlorian Fainelli 	pvid = b53_default_pvid(dev);
1832a2482d2cSFlorian Fainelli 
183348aea33aSFlorian Fainelli 	/* Make this port join all VLANs without VLAN entries */
183448aea33aSFlorian Fainelli 	if (is58xx(dev)) {
183548aea33aSFlorian Fainelli 		b53_read16(dev, B53_VLAN_PAGE, B53_JOIN_ALL_VLAN_EN, &reg);
183648aea33aSFlorian Fainelli 		reg |= BIT(port);
183748aea33aSFlorian Fainelli 		if (!(reg & BIT(cpu_port)))
183848aea33aSFlorian Fainelli 			reg |= BIT(cpu_port);
183948aea33aSFlorian Fainelli 		b53_write16(dev, B53_VLAN_PAGE, B53_JOIN_ALL_VLAN_EN, reg);
184048aea33aSFlorian Fainelli 	} else {
1841a2482d2cSFlorian Fainelli 		b53_get_vlan_entry(dev, pvid, vl);
1842c499696eSFlorian Fainelli 		vl->members |= BIT(port) | BIT(cpu_port);
1843c499696eSFlorian Fainelli 		vl->untag |= BIT(port) | BIT(cpu_port);
1844a2482d2cSFlorian Fainelli 		b53_set_vlan_entry(dev, pvid, vl);
1845ff39c2d6SFlorian Fainelli 	}
184648aea33aSFlorian Fainelli }
18473117455dSFlorian Fainelli EXPORT_SYMBOL(b53_br_leave);
1848ff39c2d6SFlorian Fainelli 
18493117455dSFlorian Fainelli void b53_br_set_stp_state(struct dsa_switch *ds, int port, u8 state)
1850ff39c2d6SFlorian Fainelli {
185104bed143SVivien Didelot 	struct b53_device *dev = ds->priv;
1852597698f1SVivien Didelot 	u8 hw_state;
1853ff39c2d6SFlorian Fainelli 	u8 reg;
1854ff39c2d6SFlorian Fainelli 
1855ff39c2d6SFlorian Fainelli 	switch (state) {
1856ff39c2d6SFlorian Fainelli 	case BR_STATE_DISABLED:
1857ff39c2d6SFlorian Fainelli 		hw_state = PORT_CTRL_DIS_STATE;
1858ff39c2d6SFlorian Fainelli 		break;
1859ff39c2d6SFlorian Fainelli 	case BR_STATE_LISTENING:
1860ff39c2d6SFlorian Fainelli 		hw_state = PORT_CTRL_LISTEN_STATE;
1861ff39c2d6SFlorian Fainelli 		break;
1862ff39c2d6SFlorian Fainelli 	case BR_STATE_LEARNING:
1863ff39c2d6SFlorian Fainelli 		hw_state = PORT_CTRL_LEARN_STATE;
1864ff39c2d6SFlorian Fainelli 		break;
1865ff39c2d6SFlorian Fainelli 	case BR_STATE_FORWARDING:
1866ff39c2d6SFlorian Fainelli 		hw_state = PORT_CTRL_FWD_STATE;
1867ff39c2d6SFlorian Fainelli 		break;
1868ff39c2d6SFlorian Fainelli 	case BR_STATE_BLOCKING:
1869ff39c2d6SFlorian Fainelli 		hw_state = PORT_CTRL_BLOCK_STATE;
1870ff39c2d6SFlorian Fainelli 		break;
1871ff39c2d6SFlorian Fainelli 	default:
1872ff39c2d6SFlorian Fainelli 		dev_err(ds->dev, "invalid STP state: %d\n", state);
1873ff39c2d6SFlorian Fainelli 		return;
1874ff39c2d6SFlorian Fainelli 	}
1875ff39c2d6SFlorian Fainelli 
1876ff39c2d6SFlorian Fainelli 	b53_read8(dev, B53_CTRL_PAGE, B53_PORT_CTRL(port), &reg);
1877ff39c2d6SFlorian Fainelli 	reg &= ~PORT_CTRL_STP_STATE_MASK;
1878ff39c2d6SFlorian Fainelli 	reg |= hw_state;
1879ff39c2d6SFlorian Fainelli 	b53_write8(dev, B53_CTRL_PAGE, B53_PORT_CTRL(port), reg);
1880ff39c2d6SFlorian Fainelli }
18813117455dSFlorian Fainelli EXPORT_SYMBOL(b53_br_set_stp_state);
1882ff39c2d6SFlorian Fainelli 
18833117455dSFlorian Fainelli void b53_br_fast_age(struct dsa_switch *ds, int port)
1884597698f1SVivien Didelot {
1885597698f1SVivien Didelot 	struct b53_device *dev = ds->priv;
1886597698f1SVivien Didelot 
1887597698f1SVivien Didelot 	if (b53_fast_age_port(dev, port))
1888597698f1SVivien Didelot 		dev_err(ds->dev, "fast ageing failed\n");
1889597698f1SVivien Didelot }
18903117455dSFlorian Fainelli EXPORT_SYMBOL(b53_br_fast_age);
1891597698f1SVivien Didelot 
189253568438SFlorian Fainelli int b53_br_egress_floods(struct dsa_switch *ds, int port,
189353568438SFlorian Fainelli 			 bool unicast, bool multicast)
189453568438SFlorian Fainelli {
189553568438SFlorian Fainelli 	struct b53_device *dev = ds->priv;
189653568438SFlorian Fainelli 	u16 uc, mc;
189753568438SFlorian Fainelli 
189863cc54a6SFlorian Fainelli 	b53_read16(dev, B53_CTRL_PAGE, B53_UC_FLOOD_MASK, &uc);
189953568438SFlorian Fainelli 	if (unicast)
190053568438SFlorian Fainelli 		uc |= BIT(port);
190153568438SFlorian Fainelli 	else
190253568438SFlorian Fainelli 		uc &= ~BIT(port);
190363cc54a6SFlorian Fainelli 	b53_write16(dev, B53_CTRL_PAGE, B53_UC_FLOOD_MASK, uc);
190453568438SFlorian Fainelli 
190563cc54a6SFlorian Fainelli 	b53_read16(dev, B53_CTRL_PAGE, B53_MC_FLOOD_MASK, &mc);
190653568438SFlorian Fainelli 	if (multicast)
190753568438SFlorian Fainelli 		mc |= BIT(port);
190853568438SFlorian Fainelli 	else
190953568438SFlorian Fainelli 		mc &= ~BIT(port);
191063cc54a6SFlorian Fainelli 	b53_write16(dev, B53_CTRL_PAGE, B53_MC_FLOOD_MASK, mc);
191163cc54a6SFlorian Fainelli 
191263cc54a6SFlorian Fainelli 	b53_read16(dev, B53_CTRL_PAGE, B53_IPMC_FLOOD_MASK, &mc);
191363cc54a6SFlorian Fainelli 	if (multicast)
191463cc54a6SFlorian Fainelli 		mc |= BIT(port);
191563cc54a6SFlorian Fainelli 	else
191663cc54a6SFlorian Fainelli 		mc &= ~BIT(port);
191763cc54a6SFlorian Fainelli 	b53_write16(dev, B53_CTRL_PAGE, B53_IPMC_FLOOD_MASK, mc);
191853568438SFlorian Fainelli 
191953568438SFlorian Fainelli 	return 0;
192053568438SFlorian Fainelli 
192153568438SFlorian Fainelli }
192253568438SFlorian Fainelli EXPORT_SYMBOL(b53_br_egress_floods);
192353568438SFlorian Fainelli 
1924c7d28c9dSFlorian Fainelli static bool b53_possible_cpu_port(struct dsa_switch *ds, int port)
19257edc58d6SFlorian Fainelli {
19267edc58d6SFlorian Fainelli 	/* Broadcom switches will accept enabling Broadcom tags on the
19277edc58d6SFlorian Fainelli 	 * following ports: 5, 7 and 8, any other port is not supported
19287edc58d6SFlorian Fainelli 	 */
19295ed4e3ebSFlorian Fainelli 	switch (port) {
19305ed4e3ebSFlorian Fainelli 	case B53_CPU_PORT_25:
19315ed4e3ebSFlorian Fainelli 	case 7:
19325ed4e3ebSFlorian Fainelli 	case B53_CPU_PORT:
19337edc58d6SFlorian Fainelli 		return true;
19347edc58d6SFlorian Fainelli 	}
19357edc58d6SFlorian Fainelli 
19365ed4e3ebSFlorian Fainelli 	return false;
19375ed4e3ebSFlorian Fainelli }
19385ed4e3ebSFlorian Fainelli 
19398fab459eSFlorian Fainelli static bool b53_can_enable_brcm_tags(struct dsa_switch *ds, int port,
19408fab459eSFlorian Fainelli 				     enum dsa_tag_protocol tag_protocol)
1941c7d28c9dSFlorian Fainelli {
1942c7d28c9dSFlorian Fainelli 	bool ret = b53_possible_cpu_port(ds, port);
1943c7d28c9dSFlorian Fainelli 
19448fab459eSFlorian Fainelli 	if (!ret) {
1945c7d28c9dSFlorian Fainelli 		dev_warn(ds->dev, "Port %d is not Broadcom tag capable\n",
1946c7d28c9dSFlorian Fainelli 			 port);
1947c7d28c9dSFlorian Fainelli 		return ret;
1948c7d28c9dSFlorian Fainelli 	}
1949c7d28c9dSFlorian Fainelli 
19508fab459eSFlorian Fainelli 	switch (tag_protocol) {
19518fab459eSFlorian Fainelli 	case DSA_TAG_PROTO_BRCM:
19528fab459eSFlorian Fainelli 	case DSA_TAG_PROTO_BRCM_PREPEND:
19538fab459eSFlorian Fainelli 		dev_warn(ds->dev,
19548fab459eSFlorian Fainelli 			 "Port %d is stacked to Broadcom tag switch\n", port);
19558fab459eSFlorian Fainelli 		ret = false;
19568fab459eSFlorian Fainelli 		break;
19578fab459eSFlorian Fainelli 	default:
19588fab459eSFlorian Fainelli 		ret = true;
19598fab459eSFlorian Fainelli 		break;
19608fab459eSFlorian Fainelli 	}
19618fab459eSFlorian Fainelli 
19628fab459eSFlorian Fainelli 	return ret;
19638fab459eSFlorian Fainelli }
19648fab459eSFlorian Fainelli 
19654d776482SFlorian Fainelli enum dsa_tag_protocol b53_get_tag_protocol(struct dsa_switch *ds, int port,
19664d776482SFlorian Fainelli 					   enum dsa_tag_protocol mprot)
19677b314362SAndrew Lunn {
19687edc58d6SFlorian Fainelli 	struct b53_device *dev = ds->priv;
19697edc58d6SFlorian Fainelli 
197054e98b5dSFlorian Fainelli 	/* Older models (5325, 5365) support a different tag format that we do
19718fab459eSFlorian Fainelli 	 * not support in net/dsa/tag_brcm.c yet.
19727edc58d6SFlorian Fainelli 	 */
19738fab459eSFlorian Fainelli 	if (is5325(dev) || is5365(dev) ||
19748fab459eSFlorian Fainelli 	    !b53_can_enable_brcm_tags(ds, port, mprot)) {
19754d776482SFlorian Fainelli 		dev->tag_protocol = DSA_TAG_PROTO_NONE;
19764d776482SFlorian Fainelli 		goto out;
19774d776482SFlorian Fainelli 	}
197811606039SFlorian Fainelli 
197911606039SFlorian Fainelli 	/* Broadcom BCM58xx chips have a flow accelerator on Port 8
198011606039SFlorian Fainelli 	 * which requires us to use the prepended Broadcom tag type
198111606039SFlorian Fainelli 	 */
19824d776482SFlorian Fainelli 	if (dev->chip_id == BCM58XX_DEVICE_ID && port == B53_CPU_PORT) {
19834d776482SFlorian Fainelli 		dev->tag_protocol = DSA_TAG_PROTO_BRCM_PREPEND;
19844d776482SFlorian Fainelli 		goto out;
19854d776482SFlorian Fainelli 	}
198611606039SFlorian Fainelli 
19874d776482SFlorian Fainelli 	dev->tag_protocol = DSA_TAG_PROTO_BRCM;
19884d776482SFlorian Fainelli out:
19894d776482SFlorian Fainelli 	return dev->tag_protocol;
19907b314362SAndrew Lunn }
19919f66816aSFlorian Fainelli EXPORT_SYMBOL(b53_get_tag_protocol);
19927b314362SAndrew Lunn 
1993ed3af5fdSFlorian Fainelli int b53_mirror_add(struct dsa_switch *ds, int port,
1994ed3af5fdSFlorian Fainelli 		   struct dsa_mall_mirror_tc_entry *mirror, bool ingress)
1995ed3af5fdSFlorian Fainelli {
1996ed3af5fdSFlorian Fainelli 	struct b53_device *dev = ds->priv;
1997ed3af5fdSFlorian Fainelli 	u16 reg, loc;
1998ed3af5fdSFlorian Fainelli 
1999ed3af5fdSFlorian Fainelli 	if (ingress)
2000ed3af5fdSFlorian Fainelli 		loc = B53_IG_MIR_CTL;
2001ed3af5fdSFlorian Fainelli 	else
2002ed3af5fdSFlorian Fainelli 		loc = B53_EG_MIR_CTL;
2003ed3af5fdSFlorian Fainelli 
2004ed3af5fdSFlorian Fainelli 	b53_read16(dev, B53_MGMT_PAGE, loc, &reg);
2005ed3af5fdSFlorian Fainelli 	reg |= BIT(port);
2006ed3af5fdSFlorian Fainelli 	b53_write16(dev, B53_MGMT_PAGE, loc, reg);
2007ed3af5fdSFlorian Fainelli 
2008ed3af5fdSFlorian Fainelli 	b53_read16(dev, B53_MGMT_PAGE, B53_MIR_CAP_CTL, &reg);
2009ed3af5fdSFlorian Fainelli 	reg &= ~CAP_PORT_MASK;
2010ed3af5fdSFlorian Fainelli 	reg |= mirror->to_local_port;
2011ed3af5fdSFlorian Fainelli 	reg |= MIRROR_EN;
2012ed3af5fdSFlorian Fainelli 	b53_write16(dev, B53_MGMT_PAGE, B53_MIR_CAP_CTL, reg);
2013ed3af5fdSFlorian Fainelli 
2014ed3af5fdSFlorian Fainelli 	return 0;
2015ed3af5fdSFlorian Fainelli }
2016ed3af5fdSFlorian Fainelli EXPORT_SYMBOL(b53_mirror_add);
2017ed3af5fdSFlorian Fainelli 
2018ed3af5fdSFlorian Fainelli void b53_mirror_del(struct dsa_switch *ds, int port,
2019ed3af5fdSFlorian Fainelli 		    struct dsa_mall_mirror_tc_entry *mirror)
2020ed3af5fdSFlorian Fainelli {
2021ed3af5fdSFlorian Fainelli 	struct b53_device *dev = ds->priv;
2022ed3af5fdSFlorian Fainelli 	bool loc_disable = false, other_loc_disable = false;
2023ed3af5fdSFlorian Fainelli 	u16 reg, loc;
2024ed3af5fdSFlorian Fainelli 
2025ed3af5fdSFlorian Fainelli 	if (mirror->ingress)
2026ed3af5fdSFlorian Fainelli 		loc = B53_IG_MIR_CTL;
2027ed3af5fdSFlorian Fainelli 	else
2028ed3af5fdSFlorian Fainelli 		loc = B53_EG_MIR_CTL;
2029ed3af5fdSFlorian Fainelli 
2030ed3af5fdSFlorian Fainelli 	/* Update the desired ingress/egress register */
2031ed3af5fdSFlorian Fainelli 	b53_read16(dev, B53_MGMT_PAGE, loc, &reg);
2032ed3af5fdSFlorian Fainelli 	reg &= ~BIT(port);
2033ed3af5fdSFlorian Fainelli 	if (!(reg & MIRROR_MASK))
2034ed3af5fdSFlorian Fainelli 		loc_disable = true;
2035ed3af5fdSFlorian Fainelli 	b53_write16(dev, B53_MGMT_PAGE, loc, reg);
2036ed3af5fdSFlorian Fainelli 
2037ed3af5fdSFlorian Fainelli 	/* Now look at the other one to know if we can disable mirroring
2038ed3af5fdSFlorian Fainelli 	 * entirely
2039ed3af5fdSFlorian Fainelli 	 */
2040ed3af5fdSFlorian Fainelli 	if (mirror->ingress)
2041ed3af5fdSFlorian Fainelli 		b53_read16(dev, B53_MGMT_PAGE, B53_EG_MIR_CTL, &reg);
2042ed3af5fdSFlorian Fainelli 	else
2043ed3af5fdSFlorian Fainelli 		b53_read16(dev, B53_MGMT_PAGE, B53_IG_MIR_CTL, &reg);
2044ed3af5fdSFlorian Fainelli 	if (!(reg & MIRROR_MASK))
2045ed3af5fdSFlorian Fainelli 		other_loc_disable = true;
2046ed3af5fdSFlorian Fainelli 
2047ed3af5fdSFlorian Fainelli 	b53_read16(dev, B53_MGMT_PAGE, B53_MIR_CAP_CTL, &reg);
2048ed3af5fdSFlorian Fainelli 	/* Both no longer have ports, let's disable mirroring */
2049ed3af5fdSFlorian Fainelli 	if (loc_disable && other_loc_disable) {
2050ed3af5fdSFlorian Fainelli 		reg &= ~MIRROR_EN;
2051ed3af5fdSFlorian Fainelli 		reg &= ~mirror->to_local_port;
2052ed3af5fdSFlorian Fainelli 	}
2053ed3af5fdSFlorian Fainelli 	b53_write16(dev, B53_MGMT_PAGE, B53_MIR_CAP_CTL, reg);
2054ed3af5fdSFlorian Fainelli }
2055ed3af5fdSFlorian Fainelli EXPORT_SYMBOL(b53_mirror_del);
2056ed3af5fdSFlorian Fainelli 
205722256b0aSFlorian Fainelli void b53_eee_enable_set(struct dsa_switch *ds, int port, bool enable)
205822256b0aSFlorian Fainelli {
205922256b0aSFlorian Fainelli 	struct b53_device *dev = ds->priv;
206022256b0aSFlorian Fainelli 	u16 reg;
206122256b0aSFlorian Fainelli 
206222256b0aSFlorian Fainelli 	b53_read16(dev, B53_EEE_PAGE, B53_EEE_EN_CTRL, &reg);
206322256b0aSFlorian Fainelli 	if (enable)
206422256b0aSFlorian Fainelli 		reg |= BIT(port);
206522256b0aSFlorian Fainelli 	else
206622256b0aSFlorian Fainelli 		reg &= ~BIT(port);
206722256b0aSFlorian Fainelli 	b53_write16(dev, B53_EEE_PAGE, B53_EEE_EN_CTRL, reg);
206822256b0aSFlorian Fainelli }
206922256b0aSFlorian Fainelli EXPORT_SYMBOL(b53_eee_enable_set);
207022256b0aSFlorian Fainelli 
207122256b0aSFlorian Fainelli 
207222256b0aSFlorian Fainelli /* Returns 0 if EEE was not enabled, or 1 otherwise
207322256b0aSFlorian Fainelli  */
207422256b0aSFlorian Fainelli int b53_eee_init(struct dsa_switch *ds, int port, struct phy_device *phy)
207522256b0aSFlorian Fainelli {
207622256b0aSFlorian Fainelli 	int ret;
207722256b0aSFlorian Fainelli 
207822256b0aSFlorian Fainelli 	ret = phy_init_eee(phy, 0);
207922256b0aSFlorian Fainelli 	if (ret)
208022256b0aSFlorian Fainelli 		return 0;
208122256b0aSFlorian Fainelli 
208222256b0aSFlorian Fainelli 	b53_eee_enable_set(ds, port, true);
208322256b0aSFlorian Fainelli 
208422256b0aSFlorian Fainelli 	return 1;
208522256b0aSFlorian Fainelli }
208622256b0aSFlorian Fainelli EXPORT_SYMBOL(b53_eee_init);
208722256b0aSFlorian Fainelli 
208822256b0aSFlorian Fainelli int b53_get_mac_eee(struct dsa_switch *ds, int port, struct ethtool_eee *e)
208922256b0aSFlorian Fainelli {
209022256b0aSFlorian Fainelli 	struct b53_device *dev = ds->priv;
209122256b0aSFlorian Fainelli 	struct ethtool_eee *p = &dev->ports[port].eee;
209222256b0aSFlorian Fainelli 	u16 reg;
209322256b0aSFlorian Fainelli 
209422256b0aSFlorian Fainelli 	if (is5325(dev) || is5365(dev))
209522256b0aSFlorian Fainelli 		return -EOPNOTSUPP;
209622256b0aSFlorian Fainelli 
209722256b0aSFlorian Fainelli 	b53_read16(dev, B53_EEE_PAGE, B53_EEE_LPI_INDICATE, &reg);
209822256b0aSFlorian Fainelli 	e->eee_enabled = p->eee_enabled;
209922256b0aSFlorian Fainelli 	e->eee_active = !!(reg & BIT(port));
210022256b0aSFlorian Fainelli 
210122256b0aSFlorian Fainelli 	return 0;
210222256b0aSFlorian Fainelli }
210322256b0aSFlorian Fainelli EXPORT_SYMBOL(b53_get_mac_eee);
210422256b0aSFlorian Fainelli 
210522256b0aSFlorian Fainelli int b53_set_mac_eee(struct dsa_switch *ds, int port, struct ethtool_eee *e)
210622256b0aSFlorian Fainelli {
210722256b0aSFlorian Fainelli 	struct b53_device *dev = ds->priv;
210822256b0aSFlorian Fainelli 	struct ethtool_eee *p = &dev->ports[port].eee;
210922256b0aSFlorian Fainelli 
211022256b0aSFlorian Fainelli 	if (is5325(dev) || is5365(dev))
211122256b0aSFlorian Fainelli 		return -EOPNOTSUPP;
211222256b0aSFlorian Fainelli 
211322256b0aSFlorian Fainelli 	p->eee_enabled = e->eee_enabled;
211422256b0aSFlorian Fainelli 	b53_eee_enable_set(ds, port, e->eee_enabled);
211522256b0aSFlorian Fainelli 
211622256b0aSFlorian Fainelli 	return 0;
211722256b0aSFlorian Fainelli }
211822256b0aSFlorian Fainelli EXPORT_SYMBOL(b53_set_mac_eee);
211922256b0aSFlorian Fainelli 
21206ae5834bSMurali Krishna Policharla static int b53_change_mtu(struct dsa_switch *ds, int port, int mtu)
21216ae5834bSMurali Krishna Policharla {
21226ae5834bSMurali Krishna Policharla 	struct b53_device *dev = ds->priv;
21236ae5834bSMurali Krishna Policharla 	bool enable_jumbo;
21246ae5834bSMurali Krishna Policharla 	bool allow_10_100;
21256ae5834bSMurali Krishna Policharla 
21266ae5834bSMurali Krishna Policharla 	if (is5325(dev) || is5365(dev))
21276ae5834bSMurali Krishna Policharla 		return -EOPNOTSUPP;
21286ae5834bSMurali Krishna Policharla 
21296ae5834bSMurali Krishna Policharla 	enable_jumbo = (mtu >= JMS_MIN_SIZE);
21306ae5834bSMurali Krishna Policharla 	allow_10_100 = (dev->chip_id == BCM583XX_DEVICE_ID);
21316ae5834bSMurali Krishna Policharla 
21326ae5834bSMurali Krishna Policharla 	return b53_set_jumbo(dev, enable_jumbo, allow_10_100);
21336ae5834bSMurali Krishna Policharla }
21346ae5834bSMurali Krishna Policharla 
21356ae5834bSMurali Krishna Policharla static int b53_get_max_mtu(struct dsa_switch *ds, int port)
21366ae5834bSMurali Krishna Policharla {
21376ae5834bSMurali Krishna Policharla 	return JMS_MAX_SIZE;
21386ae5834bSMurali Krishna Policharla }
21396ae5834bSMurali Krishna Policharla 
2140a82f67afSFlorian Fainelli static const struct dsa_switch_ops b53_switch_ops = {
21417b314362SAndrew Lunn 	.get_tag_protocol	= b53_get_tag_protocol,
2142967dd82fSFlorian Fainelli 	.setup			= b53_setup,
2143967dd82fSFlorian Fainelli 	.get_strings		= b53_get_strings,
2144967dd82fSFlorian Fainelli 	.get_ethtool_stats	= b53_get_ethtool_stats,
2145967dd82fSFlorian Fainelli 	.get_sset_count		= b53_get_sset_count,
2146c7d28c9dSFlorian Fainelli 	.get_ethtool_phy_stats	= b53_get_ethtool_phy_stats,
2147967dd82fSFlorian Fainelli 	.phy_read		= b53_phy_read16,
2148967dd82fSFlorian Fainelli 	.phy_write		= b53_phy_write16,
2149967dd82fSFlorian Fainelli 	.adjust_link		= b53_adjust_link,
2150a8e8b985SFlorian Fainelli 	.phylink_validate	= b53_phylink_validate,
2151a8e8b985SFlorian Fainelli 	.phylink_mac_link_state	= b53_phylink_mac_link_state,
2152a8e8b985SFlorian Fainelli 	.phylink_mac_config	= b53_phylink_mac_config,
2153a8e8b985SFlorian Fainelli 	.phylink_mac_an_restart	= b53_phylink_mac_an_restart,
2154a8e8b985SFlorian Fainelli 	.phylink_mac_link_down	= b53_phylink_mac_link_down,
2155a8e8b985SFlorian Fainelli 	.phylink_mac_link_up	= b53_phylink_mac_link_up,
2156967dd82fSFlorian Fainelli 	.port_enable		= b53_enable_port,
2157967dd82fSFlorian Fainelli 	.port_disable		= b53_disable_port,
2158f43a2dbeSFlorian Fainelli 	.get_mac_eee		= b53_get_mac_eee,
2159f43a2dbeSFlorian Fainelli 	.set_mac_eee		= b53_set_mac_eee,
2160ff39c2d6SFlorian Fainelli 	.port_bridge_join	= b53_br_join,
2161ff39c2d6SFlorian Fainelli 	.port_bridge_leave	= b53_br_leave,
2162ff39c2d6SFlorian Fainelli 	.port_stp_state_set	= b53_br_set_stp_state,
2163597698f1SVivien Didelot 	.port_fast_age		= b53_br_fast_age,
216453568438SFlorian Fainelli 	.port_egress_floods	= b53_br_egress_floods,
2165a2482d2cSFlorian Fainelli 	.port_vlan_filtering	= b53_vlan_filtering,
2166a2482d2cSFlorian Fainelli 	.port_vlan_prepare	= b53_vlan_prepare,
2167a2482d2cSFlorian Fainelli 	.port_vlan_add		= b53_vlan_add,
2168a2482d2cSFlorian Fainelli 	.port_vlan_del		= b53_vlan_del,
21691da6df85SFlorian Fainelli 	.port_fdb_dump		= b53_fdb_dump,
21701da6df85SFlorian Fainelli 	.port_fdb_add		= b53_fdb_add,
21711da6df85SFlorian Fainelli 	.port_fdb_del		= b53_fdb_del,
2172ed3af5fdSFlorian Fainelli 	.port_mirror_add	= b53_mirror_add,
2173ed3af5fdSFlorian Fainelli 	.port_mirror_del	= b53_mirror_del,
21745d65b64aSFlorian Fainelli 	.port_mdb_prepare	= b53_mdb_prepare,
21755d65b64aSFlorian Fainelli 	.port_mdb_add		= b53_mdb_add,
21765d65b64aSFlorian Fainelli 	.port_mdb_del		= b53_mdb_del,
21776ae5834bSMurali Krishna Policharla 	.port_max_mtu		= b53_get_max_mtu,
21786ae5834bSMurali Krishna Policharla 	.port_change_mtu	= b53_change_mtu,
2179967dd82fSFlorian Fainelli };
2180967dd82fSFlorian Fainelli 
2181967dd82fSFlorian Fainelli struct b53_chip_data {
2182967dd82fSFlorian Fainelli 	u32 chip_id;
2183967dd82fSFlorian Fainelli 	const char *dev_name;
2184967dd82fSFlorian Fainelli 	u16 vlans;
2185967dd82fSFlorian Fainelli 	u16 enabled_ports;
2186967dd82fSFlorian Fainelli 	u8 cpu_port;
2187967dd82fSFlorian Fainelli 	u8 vta_regs[3];
2188673e69a6SFlorian Fainelli 	u8 arl_bins;
2189e3da4038SFlorian Fainelli 	u16 arl_buckets;
2190967dd82fSFlorian Fainelli 	u8 duplex_reg;
2191967dd82fSFlorian Fainelli 	u8 jumbo_pm_reg;
2192967dd82fSFlorian Fainelli 	u8 jumbo_size_reg;
2193967dd82fSFlorian Fainelli };
2194967dd82fSFlorian Fainelli 
2195967dd82fSFlorian Fainelli #define B53_VTA_REGS	\
2196967dd82fSFlorian Fainelli 	{ B53_VT_ACCESS, B53_VT_INDEX, B53_VT_ENTRY }
2197967dd82fSFlorian Fainelli #define B53_VTA_REGS_9798 \
2198967dd82fSFlorian Fainelli 	{ B53_VT_ACCESS_9798, B53_VT_INDEX_9798, B53_VT_ENTRY_9798 }
2199967dd82fSFlorian Fainelli #define B53_VTA_REGS_63XX \
2200967dd82fSFlorian Fainelli 	{ B53_VT_ACCESS_63XX, B53_VT_INDEX_63XX, B53_VT_ENTRY_63XX }
2201967dd82fSFlorian Fainelli 
2202967dd82fSFlorian Fainelli static const struct b53_chip_data b53_switch_chips[] = {
2203967dd82fSFlorian Fainelli 	{
2204967dd82fSFlorian Fainelli 		.chip_id = BCM5325_DEVICE_ID,
2205967dd82fSFlorian Fainelli 		.dev_name = "BCM5325",
2206967dd82fSFlorian Fainelli 		.vlans = 16,
2207967dd82fSFlorian Fainelli 		.enabled_ports = 0x1f,
2208673e69a6SFlorian Fainelli 		.arl_bins = 2,
2209e3da4038SFlorian Fainelli 		.arl_buckets = 1024,
2210967dd82fSFlorian Fainelli 		.cpu_port = B53_CPU_PORT_25,
2211967dd82fSFlorian Fainelli 		.duplex_reg = B53_DUPLEX_STAT_FE,
2212967dd82fSFlorian Fainelli 	},
2213967dd82fSFlorian Fainelli 	{
2214967dd82fSFlorian Fainelli 		.chip_id = BCM5365_DEVICE_ID,
2215967dd82fSFlorian Fainelli 		.dev_name = "BCM5365",
2216967dd82fSFlorian Fainelli 		.vlans = 256,
2217967dd82fSFlorian Fainelli 		.enabled_ports = 0x1f,
2218673e69a6SFlorian Fainelli 		.arl_bins = 2,
2219e3da4038SFlorian Fainelli 		.arl_buckets = 1024,
2220967dd82fSFlorian Fainelli 		.cpu_port = B53_CPU_PORT_25,
2221967dd82fSFlorian Fainelli 		.duplex_reg = B53_DUPLEX_STAT_FE,
2222967dd82fSFlorian Fainelli 	},
2223967dd82fSFlorian Fainelli 	{
2224a95691bcSDamien Thébault 		.chip_id = BCM5389_DEVICE_ID,
2225a95691bcSDamien Thébault 		.dev_name = "BCM5389",
2226a95691bcSDamien Thébault 		.vlans = 4096,
2227a95691bcSDamien Thébault 		.enabled_ports = 0x1f,
2228673e69a6SFlorian Fainelli 		.arl_bins = 4,
2229e3da4038SFlorian Fainelli 		.arl_buckets = 1024,
2230a95691bcSDamien Thébault 		.cpu_port = B53_CPU_PORT,
2231a95691bcSDamien Thébault 		.vta_regs = B53_VTA_REGS,
2232a95691bcSDamien Thébault 		.duplex_reg = B53_DUPLEX_STAT_GE,
2233a95691bcSDamien Thébault 		.jumbo_pm_reg = B53_JUMBO_PORT_MASK,
2234a95691bcSDamien Thébault 		.jumbo_size_reg = B53_JUMBO_MAX_SIZE,
2235a95691bcSDamien Thébault 	},
2236a95691bcSDamien Thébault 	{
2237967dd82fSFlorian Fainelli 		.chip_id = BCM5395_DEVICE_ID,
2238967dd82fSFlorian Fainelli 		.dev_name = "BCM5395",
2239967dd82fSFlorian Fainelli 		.vlans = 4096,
2240967dd82fSFlorian Fainelli 		.enabled_ports = 0x1f,
2241673e69a6SFlorian Fainelli 		.arl_bins = 4,
2242e3da4038SFlorian Fainelli 		.arl_buckets = 1024,
2243967dd82fSFlorian Fainelli 		.cpu_port = B53_CPU_PORT,
2244967dd82fSFlorian Fainelli 		.vta_regs = B53_VTA_REGS,
2245967dd82fSFlorian Fainelli 		.duplex_reg = B53_DUPLEX_STAT_GE,
2246967dd82fSFlorian Fainelli 		.jumbo_pm_reg = B53_JUMBO_PORT_MASK,
2247967dd82fSFlorian Fainelli 		.jumbo_size_reg = B53_JUMBO_MAX_SIZE,
2248967dd82fSFlorian Fainelli 	},
2249967dd82fSFlorian Fainelli 	{
2250967dd82fSFlorian Fainelli 		.chip_id = BCM5397_DEVICE_ID,
2251967dd82fSFlorian Fainelli 		.dev_name = "BCM5397",
2252967dd82fSFlorian Fainelli 		.vlans = 4096,
2253967dd82fSFlorian Fainelli 		.enabled_ports = 0x1f,
2254673e69a6SFlorian Fainelli 		.arl_bins = 4,
2255e3da4038SFlorian Fainelli 		.arl_buckets = 1024,
2256967dd82fSFlorian Fainelli 		.cpu_port = B53_CPU_PORT,
2257967dd82fSFlorian Fainelli 		.vta_regs = B53_VTA_REGS_9798,
2258967dd82fSFlorian Fainelli 		.duplex_reg = B53_DUPLEX_STAT_GE,
2259967dd82fSFlorian Fainelli 		.jumbo_pm_reg = B53_JUMBO_PORT_MASK,
2260967dd82fSFlorian Fainelli 		.jumbo_size_reg = B53_JUMBO_MAX_SIZE,
2261967dd82fSFlorian Fainelli 	},
2262967dd82fSFlorian Fainelli 	{
2263967dd82fSFlorian Fainelli 		.chip_id = BCM5398_DEVICE_ID,
2264967dd82fSFlorian Fainelli 		.dev_name = "BCM5398",
2265967dd82fSFlorian Fainelli 		.vlans = 4096,
2266967dd82fSFlorian Fainelli 		.enabled_ports = 0x7f,
2267673e69a6SFlorian Fainelli 		.arl_bins = 4,
2268e3da4038SFlorian Fainelli 		.arl_buckets = 1024,
2269967dd82fSFlorian Fainelli 		.cpu_port = B53_CPU_PORT,
2270967dd82fSFlorian Fainelli 		.vta_regs = B53_VTA_REGS_9798,
2271967dd82fSFlorian Fainelli 		.duplex_reg = B53_DUPLEX_STAT_GE,
2272967dd82fSFlorian Fainelli 		.jumbo_pm_reg = B53_JUMBO_PORT_MASK,
2273967dd82fSFlorian Fainelli 		.jumbo_size_reg = B53_JUMBO_MAX_SIZE,
2274967dd82fSFlorian Fainelli 	},
2275967dd82fSFlorian Fainelli 	{
2276967dd82fSFlorian Fainelli 		.chip_id = BCM53115_DEVICE_ID,
2277967dd82fSFlorian Fainelli 		.dev_name = "BCM53115",
2278967dd82fSFlorian Fainelli 		.vlans = 4096,
2279967dd82fSFlorian Fainelli 		.enabled_ports = 0x1f,
2280673e69a6SFlorian Fainelli 		.arl_bins = 4,
2281e3da4038SFlorian Fainelli 		.arl_buckets = 1024,
2282967dd82fSFlorian Fainelli 		.vta_regs = B53_VTA_REGS,
2283967dd82fSFlorian Fainelli 		.cpu_port = B53_CPU_PORT,
2284967dd82fSFlorian Fainelli 		.duplex_reg = B53_DUPLEX_STAT_GE,
2285967dd82fSFlorian Fainelli 		.jumbo_pm_reg = B53_JUMBO_PORT_MASK,
2286967dd82fSFlorian Fainelli 		.jumbo_size_reg = B53_JUMBO_MAX_SIZE,
2287967dd82fSFlorian Fainelli 	},
2288967dd82fSFlorian Fainelli 	{
2289967dd82fSFlorian Fainelli 		.chip_id = BCM53125_DEVICE_ID,
2290967dd82fSFlorian Fainelli 		.dev_name = "BCM53125",
2291967dd82fSFlorian Fainelli 		.vlans = 4096,
2292967dd82fSFlorian Fainelli 		.enabled_ports = 0xff,
2293673e69a6SFlorian Fainelli 		.arl_bins = 4,
2294e3da4038SFlorian Fainelli 		.arl_buckets = 1024,
2295967dd82fSFlorian Fainelli 		.cpu_port = B53_CPU_PORT,
2296967dd82fSFlorian Fainelli 		.vta_regs = B53_VTA_REGS,
2297967dd82fSFlorian Fainelli 		.duplex_reg = B53_DUPLEX_STAT_GE,
2298967dd82fSFlorian Fainelli 		.jumbo_pm_reg = B53_JUMBO_PORT_MASK,
2299967dd82fSFlorian Fainelli 		.jumbo_size_reg = B53_JUMBO_MAX_SIZE,
2300967dd82fSFlorian Fainelli 	},
2301967dd82fSFlorian Fainelli 	{
2302967dd82fSFlorian Fainelli 		.chip_id = BCM53128_DEVICE_ID,
2303967dd82fSFlorian Fainelli 		.dev_name = "BCM53128",
2304967dd82fSFlorian Fainelli 		.vlans = 4096,
2305967dd82fSFlorian Fainelli 		.enabled_ports = 0x1ff,
2306673e69a6SFlorian Fainelli 		.arl_bins = 4,
2307e3da4038SFlorian Fainelli 		.arl_buckets = 1024,
2308967dd82fSFlorian Fainelli 		.cpu_port = B53_CPU_PORT,
2309967dd82fSFlorian Fainelli 		.vta_regs = B53_VTA_REGS,
2310967dd82fSFlorian Fainelli 		.duplex_reg = B53_DUPLEX_STAT_GE,
2311967dd82fSFlorian Fainelli 		.jumbo_pm_reg = B53_JUMBO_PORT_MASK,
2312967dd82fSFlorian Fainelli 		.jumbo_size_reg = B53_JUMBO_MAX_SIZE,
2313967dd82fSFlorian Fainelli 	},
2314967dd82fSFlorian Fainelli 	{
2315967dd82fSFlorian Fainelli 		.chip_id = BCM63XX_DEVICE_ID,
2316967dd82fSFlorian Fainelli 		.dev_name = "BCM63xx",
2317967dd82fSFlorian Fainelli 		.vlans = 4096,
2318967dd82fSFlorian Fainelli 		.enabled_ports = 0, /* pdata must provide them */
2319673e69a6SFlorian Fainelli 		.arl_bins = 4,
2320e3da4038SFlorian Fainelli 		.arl_buckets = 1024,
2321967dd82fSFlorian Fainelli 		.cpu_port = B53_CPU_PORT,
2322967dd82fSFlorian Fainelli 		.vta_regs = B53_VTA_REGS_63XX,
2323967dd82fSFlorian Fainelli 		.duplex_reg = B53_DUPLEX_STAT_63XX,
2324967dd82fSFlorian Fainelli 		.jumbo_pm_reg = B53_JUMBO_PORT_MASK_63XX,
2325967dd82fSFlorian Fainelli 		.jumbo_size_reg = B53_JUMBO_MAX_SIZE_63XX,
2326967dd82fSFlorian Fainelli 	},
2327967dd82fSFlorian Fainelli 	{
2328967dd82fSFlorian Fainelli 		.chip_id = BCM53010_DEVICE_ID,
2329967dd82fSFlorian Fainelli 		.dev_name = "BCM53010",
2330967dd82fSFlorian Fainelli 		.vlans = 4096,
2331967dd82fSFlorian Fainelli 		.enabled_ports = 0x1f,
2332673e69a6SFlorian Fainelli 		.arl_bins = 4,
2333e3da4038SFlorian Fainelli 		.arl_buckets = 1024,
2334967dd82fSFlorian Fainelli 		.cpu_port = B53_CPU_PORT_25, /* TODO: auto detect */
2335967dd82fSFlorian Fainelli 		.vta_regs = B53_VTA_REGS,
2336967dd82fSFlorian Fainelli 		.duplex_reg = B53_DUPLEX_STAT_GE,
2337967dd82fSFlorian Fainelli 		.jumbo_pm_reg = B53_JUMBO_PORT_MASK,
2338967dd82fSFlorian Fainelli 		.jumbo_size_reg = B53_JUMBO_MAX_SIZE,
2339967dd82fSFlorian Fainelli 	},
2340967dd82fSFlorian Fainelli 	{
2341967dd82fSFlorian Fainelli 		.chip_id = BCM53011_DEVICE_ID,
2342967dd82fSFlorian Fainelli 		.dev_name = "BCM53011",
2343967dd82fSFlorian Fainelli 		.vlans = 4096,
2344967dd82fSFlorian Fainelli 		.enabled_ports = 0x1bf,
2345673e69a6SFlorian Fainelli 		.arl_bins = 4,
2346e3da4038SFlorian Fainelli 		.arl_buckets = 1024,
2347967dd82fSFlorian Fainelli 		.cpu_port = B53_CPU_PORT_25, /* TODO: auto detect */
2348967dd82fSFlorian Fainelli 		.vta_regs = B53_VTA_REGS,
2349967dd82fSFlorian Fainelli 		.duplex_reg = B53_DUPLEX_STAT_GE,
2350967dd82fSFlorian Fainelli 		.jumbo_pm_reg = B53_JUMBO_PORT_MASK,
2351967dd82fSFlorian Fainelli 		.jumbo_size_reg = B53_JUMBO_MAX_SIZE,
2352967dd82fSFlorian Fainelli 	},
2353967dd82fSFlorian Fainelli 	{
2354967dd82fSFlorian Fainelli 		.chip_id = BCM53012_DEVICE_ID,
2355967dd82fSFlorian Fainelli 		.dev_name = "BCM53012",
2356967dd82fSFlorian Fainelli 		.vlans = 4096,
2357967dd82fSFlorian Fainelli 		.enabled_ports = 0x1bf,
2358673e69a6SFlorian Fainelli 		.arl_bins = 4,
2359e3da4038SFlorian Fainelli 		.arl_buckets = 1024,
2360967dd82fSFlorian Fainelli 		.cpu_port = B53_CPU_PORT_25, /* TODO: auto detect */
2361967dd82fSFlorian Fainelli 		.vta_regs = B53_VTA_REGS,
2362967dd82fSFlorian Fainelli 		.duplex_reg = B53_DUPLEX_STAT_GE,
2363967dd82fSFlorian Fainelli 		.jumbo_pm_reg = B53_JUMBO_PORT_MASK,
2364967dd82fSFlorian Fainelli 		.jumbo_size_reg = B53_JUMBO_MAX_SIZE,
2365967dd82fSFlorian Fainelli 	},
2366967dd82fSFlorian Fainelli 	{
2367967dd82fSFlorian Fainelli 		.chip_id = BCM53018_DEVICE_ID,
2368967dd82fSFlorian Fainelli 		.dev_name = "BCM53018",
2369967dd82fSFlorian Fainelli 		.vlans = 4096,
2370967dd82fSFlorian Fainelli 		.enabled_ports = 0x1f,
2371673e69a6SFlorian Fainelli 		.arl_bins = 4,
2372e3da4038SFlorian Fainelli 		.arl_buckets = 1024,
2373967dd82fSFlorian Fainelli 		.cpu_port = B53_CPU_PORT_25, /* TODO: auto detect */
2374967dd82fSFlorian Fainelli 		.vta_regs = B53_VTA_REGS,
2375967dd82fSFlorian Fainelli 		.duplex_reg = B53_DUPLEX_STAT_GE,
2376967dd82fSFlorian Fainelli 		.jumbo_pm_reg = B53_JUMBO_PORT_MASK,
2377967dd82fSFlorian Fainelli 		.jumbo_size_reg = B53_JUMBO_MAX_SIZE,
2378967dd82fSFlorian Fainelli 	},
2379967dd82fSFlorian Fainelli 	{
2380967dd82fSFlorian Fainelli 		.chip_id = BCM53019_DEVICE_ID,
2381967dd82fSFlorian Fainelli 		.dev_name = "BCM53019",
2382967dd82fSFlorian Fainelli 		.vlans = 4096,
2383967dd82fSFlorian Fainelli 		.enabled_ports = 0x1f,
2384673e69a6SFlorian Fainelli 		.arl_bins = 4,
2385e3da4038SFlorian Fainelli 		.arl_buckets = 1024,
2386967dd82fSFlorian Fainelli 		.cpu_port = B53_CPU_PORT_25, /* TODO: auto detect */
2387967dd82fSFlorian Fainelli 		.vta_regs = B53_VTA_REGS,
2388967dd82fSFlorian Fainelli 		.duplex_reg = B53_DUPLEX_STAT_GE,
2389967dd82fSFlorian Fainelli 		.jumbo_pm_reg = B53_JUMBO_PORT_MASK,
2390967dd82fSFlorian Fainelli 		.jumbo_size_reg = B53_JUMBO_MAX_SIZE,
2391967dd82fSFlorian Fainelli 	},
2392991a36bbSFlorian Fainelli 	{
2393991a36bbSFlorian Fainelli 		.chip_id = BCM58XX_DEVICE_ID,
2394991a36bbSFlorian Fainelli 		.dev_name = "BCM585xx/586xx/88312",
2395991a36bbSFlorian Fainelli 		.vlans	= 4096,
2396991a36bbSFlorian Fainelli 		.enabled_ports = 0x1ff,
2397673e69a6SFlorian Fainelli 		.arl_bins = 4,
2398e3da4038SFlorian Fainelli 		.arl_buckets = 1024,
2399bfcda65cSFlorian Fainelli 		.cpu_port = B53_CPU_PORT,
2400991a36bbSFlorian Fainelli 		.vta_regs = B53_VTA_REGS,
2401991a36bbSFlorian Fainelli 		.duplex_reg = B53_DUPLEX_STAT_GE,
2402991a36bbSFlorian Fainelli 		.jumbo_pm_reg = B53_JUMBO_PORT_MASK,
2403991a36bbSFlorian Fainelli 		.jumbo_size_reg = B53_JUMBO_MAX_SIZE,
2404991a36bbSFlorian Fainelli 	},
2405130401d9SFlorian Fainelli 	{
24065040cc99SArun Parameswaran 		.chip_id = BCM583XX_DEVICE_ID,
24075040cc99SArun Parameswaran 		.dev_name = "BCM583xx/11360",
24085040cc99SArun Parameswaran 		.vlans = 4096,
24095040cc99SArun Parameswaran 		.enabled_ports = 0x103,
2410673e69a6SFlorian Fainelli 		.arl_bins = 4,
2411e3da4038SFlorian Fainelli 		.arl_buckets = 1024,
24125040cc99SArun Parameswaran 		.cpu_port = B53_CPU_PORT,
24135040cc99SArun Parameswaran 		.vta_regs = B53_VTA_REGS,
24145040cc99SArun Parameswaran 		.duplex_reg = B53_DUPLEX_STAT_GE,
24155040cc99SArun Parameswaran 		.jumbo_pm_reg = B53_JUMBO_PORT_MASK,
24165040cc99SArun Parameswaran 		.jumbo_size_reg = B53_JUMBO_MAX_SIZE,
24175040cc99SArun Parameswaran 	},
24185040cc99SArun Parameswaran 	{
2419130401d9SFlorian Fainelli 		.chip_id = BCM7445_DEVICE_ID,
2420130401d9SFlorian Fainelli 		.dev_name = "BCM7445",
2421130401d9SFlorian Fainelli 		.vlans	= 4096,
2422130401d9SFlorian Fainelli 		.enabled_ports = 0x1ff,
2423673e69a6SFlorian Fainelli 		.arl_bins = 4,
2424e3da4038SFlorian Fainelli 		.arl_buckets = 1024,
2425130401d9SFlorian Fainelli 		.cpu_port = B53_CPU_PORT,
2426130401d9SFlorian Fainelli 		.vta_regs = B53_VTA_REGS,
2427130401d9SFlorian Fainelli 		.duplex_reg = B53_DUPLEX_STAT_GE,
2428130401d9SFlorian Fainelli 		.jumbo_pm_reg = B53_JUMBO_PORT_MASK,
2429130401d9SFlorian Fainelli 		.jumbo_size_reg = B53_JUMBO_MAX_SIZE,
2430130401d9SFlorian Fainelli 	},
24310fe99338SFlorian Fainelli 	{
24320fe99338SFlorian Fainelli 		.chip_id = BCM7278_DEVICE_ID,
24330fe99338SFlorian Fainelli 		.dev_name = "BCM7278",
24340fe99338SFlorian Fainelli 		.vlans = 4096,
24350fe99338SFlorian Fainelli 		.enabled_ports = 0x1ff,
2436673e69a6SFlorian Fainelli 		.arl_bins = 4,
2437e3da4038SFlorian Fainelli 		.arl_buckets = 256,
24380fe99338SFlorian Fainelli 		.cpu_port = B53_CPU_PORT,
24390fe99338SFlorian Fainelli 		.vta_regs = B53_VTA_REGS,
24400fe99338SFlorian Fainelli 		.duplex_reg = B53_DUPLEX_STAT_GE,
24410fe99338SFlorian Fainelli 		.jumbo_pm_reg = B53_JUMBO_PORT_MASK,
24420fe99338SFlorian Fainelli 		.jumbo_size_reg = B53_JUMBO_MAX_SIZE,
24430fe99338SFlorian Fainelli 	},
2444967dd82fSFlorian Fainelli };
2445967dd82fSFlorian Fainelli 
2446967dd82fSFlorian Fainelli static int b53_switch_init(struct b53_device *dev)
2447967dd82fSFlorian Fainelli {
2448967dd82fSFlorian Fainelli 	unsigned int i;
2449967dd82fSFlorian Fainelli 	int ret;
2450967dd82fSFlorian Fainelli 
2451967dd82fSFlorian Fainelli 	for (i = 0; i < ARRAY_SIZE(b53_switch_chips); i++) {
2452967dd82fSFlorian Fainelli 		const struct b53_chip_data *chip = &b53_switch_chips[i];
2453967dd82fSFlorian Fainelli 
2454967dd82fSFlorian Fainelli 		if (chip->chip_id == dev->chip_id) {
2455967dd82fSFlorian Fainelli 			if (!dev->enabled_ports)
2456967dd82fSFlorian Fainelli 				dev->enabled_ports = chip->enabled_ports;
2457967dd82fSFlorian Fainelli 			dev->name = chip->dev_name;
2458967dd82fSFlorian Fainelli 			dev->duplex_reg = chip->duplex_reg;
2459967dd82fSFlorian Fainelli 			dev->vta_regs[0] = chip->vta_regs[0];
2460967dd82fSFlorian Fainelli 			dev->vta_regs[1] = chip->vta_regs[1];
2461967dd82fSFlorian Fainelli 			dev->vta_regs[2] = chip->vta_regs[2];
2462967dd82fSFlorian Fainelli 			dev->jumbo_pm_reg = chip->jumbo_pm_reg;
2463967dd82fSFlorian Fainelli 			dev->cpu_port = chip->cpu_port;
2464967dd82fSFlorian Fainelli 			dev->num_vlans = chip->vlans;
2465673e69a6SFlorian Fainelli 			dev->num_arl_bins = chip->arl_bins;
2466e3da4038SFlorian Fainelli 			dev->num_arl_buckets = chip->arl_buckets;
2467967dd82fSFlorian Fainelli 			break;
2468967dd82fSFlorian Fainelli 		}
2469967dd82fSFlorian Fainelli 	}
2470967dd82fSFlorian Fainelli 
2471967dd82fSFlorian Fainelli 	/* check which BCM5325x version we have */
2472967dd82fSFlorian Fainelli 	if (is5325(dev)) {
2473967dd82fSFlorian Fainelli 		u8 vc4;
2474967dd82fSFlorian Fainelli 
2475967dd82fSFlorian Fainelli 		b53_read8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL4_25, &vc4);
2476967dd82fSFlorian Fainelli 
2477967dd82fSFlorian Fainelli 		/* check reserved bits */
2478967dd82fSFlorian Fainelli 		switch (vc4 & 3) {
2479967dd82fSFlorian Fainelli 		case 1:
2480967dd82fSFlorian Fainelli 			/* BCM5325E */
2481967dd82fSFlorian Fainelli 			break;
2482967dd82fSFlorian Fainelli 		case 3:
2483967dd82fSFlorian Fainelli 			/* BCM5325F - do not use port 4 */
2484967dd82fSFlorian Fainelli 			dev->enabled_ports &= ~BIT(4);
2485967dd82fSFlorian Fainelli 			break;
2486967dd82fSFlorian Fainelli 		default:
2487967dd82fSFlorian Fainelli /* On the BCM47XX SoCs this is the supported internal switch.*/
2488967dd82fSFlorian Fainelli #ifndef CONFIG_BCM47XX
2489967dd82fSFlorian Fainelli 			/* BCM5325M */
2490967dd82fSFlorian Fainelli 			return -EINVAL;
2491967dd82fSFlorian Fainelli #else
2492967dd82fSFlorian Fainelli 			break;
2493967dd82fSFlorian Fainelli #endif
2494967dd82fSFlorian Fainelli 		}
2495967dd82fSFlorian Fainelli 	} else if (dev->chip_id == BCM53115_DEVICE_ID) {
2496967dd82fSFlorian Fainelli 		u64 strap_value;
2497967dd82fSFlorian Fainelli 
2498967dd82fSFlorian Fainelli 		b53_read48(dev, B53_STAT_PAGE, B53_STRAP_VALUE, &strap_value);
2499967dd82fSFlorian Fainelli 		/* use second IMP port if GMII is enabled */
2500967dd82fSFlorian Fainelli 		if (strap_value & SV_GMII_CTRL_115)
2501967dd82fSFlorian Fainelli 			dev->cpu_port = 5;
2502967dd82fSFlorian Fainelli 	}
2503967dd82fSFlorian Fainelli 
2504967dd82fSFlorian Fainelli 	/* cpu port is always last */
2505967dd82fSFlorian Fainelli 	dev->num_ports = dev->cpu_port + 1;
2506967dd82fSFlorian Fainelli 	dev->enabled_ports |= BIT(dev->cpu_port);
2507967dd82fSFlorian Fainelli 
2508c7d28c9dSFlorian Fainelli 	/* Include non standard CPU port built-in PHYs to be probed */
2509c7d28c9dSFlorian Fainelli 	if (is539x(dev) || is531x5(dev)) {
2510c7d28c9dSFlorian Fainelli 		for (i = 0; i < dev->num_ports; i++) {
2511c7d28c9dSFlorian Fainelli 			if (!(dev->ds->phys_mii_mask & BIT(i)) &&
2512c7d28c9dSFlorian Fainelli 			    !b53_possible_cpu_port(dev->ds, i))
2513c7d28c9dSFlorian Fainelli 				dev->ds->phys_mii_mask |= BIT(i);
2514c7d28c9dSFlorian Fainelli 		}
2515c7d28c9dSFlorian Fainelli 	}
2516c7d28c9dSFlorian Fainelli 
2517a86854d0SKees Cook 	dev->ports = devm_kcalloc(dev->dev,
2518a86854d0SKees Cook 				  dev->num_ports, sizeof(struct b53_port),
2519967dd82fSFlorian Fainelli 				  GFP_KERNEL);
2520967dd82fSFlorian Fainelli 	if (!dev->ports)
2521967dd82fSFlorian Fainelli 		return -ENOMEM;
2522967dd82fSFlorian Fainelli 
2523a86854d0SKees Cook 	dev->vlans = devm_kcalloc(dev->dev,
2524a86854d0SKees Cook 				  dev->num_vlans, sizeof(struct b53_vlan),
2525a2482d2cSFlorian Fainelli 				  GFP_KERNEL);
2526a2482d2cSFlorian Fainelli 	if (!dev->vlans)
2527a2482d2cSFlorian Fainelli 		return -ENOMEM;
2528a2482d2cSFlorian Fainelli 
2529967dd82fSFlorian Fainelli 	dev->reset_gpio = b53_switch_get_reset_gpio(dev);
2530967dd82fSFlorian Fainelli 	if (dev->reset_gpio >= 0) {
2531967dd82fSFlorian Fainelli 		ret = devm_gpio_request_one(dev->dev, dev->reset_gpio,
2532967dd82fSFlorian Fainelli 					    GPIOF_OUT_INIT_HIGH, "robo_reset");
2533967dd82fSFlorian Fainelli 		if (ret)
2534967dd82fSFlorian Fainelli 			return ret;
2535967dd82fSFlorian Fainelli 	}
2536967dd82fSFlorian Fainelli 
2537967dd82fSFlorian Fainelli 	return 0;
2538967dd82fSFlorian Fainelli }
2539967dd82fSFlorian Fainelli 
25400dff88d3SJulia Lawall struct b53_device *b53_switch_alloc(struct device *base,
25410dff88d3SJulia Lawall 				    const struct b53_io_ops *ops,
2542967dd82fSFlorian Fainelli 				    void *priv)
2543967dd82fSFlorian Fainelli {
2544967dd82fSFlorian Fainelli 	struct dsa_switch *ds;
2545967dd82fSFlorian Fainelli 	struct b53_device *dev;
2546967dd82fSFlorian Fainelli 
25477e99e347SVivien Didelot 	ds = devm_kzalloc(base, sizeof(*ds), GFP_KERNEL);
2548967dd82fSFlorian Fainelli 	if (!ds)
2549967dd82fSFlorian Fainelli 		return NULL;
2550967dd82fSFlorian Fainelli 
25517e99e347SVivien Didelot 	ds->dev = base;
25527e99e347SVivien Didelot 	ds->num_ports = DSA_MAX_PORTS;
25537e99e347SVivien Didelot 
2554a0c02161SVivien Didelot 	dev = devm_kzalloc(base, sizeof(*dev), GFP_KERNEL);
2555a0c02161SVivien Didelot 	if (!dev)
2556a0c02161SVivien Didelot 		return NULL;
2557967dd82fSFlorian Fainelli 
2558967dd82fSFlorian Fainelli 	ds->priv = dev;
2559967dd82fSFlorian Fainelli 	dev->dev = base;
2560967dd82fSFlorian Fainelli 
2561967dd82fSFlorian Fainelli 	dev->ds = ds;
2562967dd82fSFlorian Fainelli 	dev->priv = priv;
2563967dd82fSFlorian Fainelli 	dev->ops = ops;
2564485ebd61SFlorian Fainelli 	ds->ops = &b53_switch_ops;
2565967dd82fSFlorian Fainelli 	mutex_init(&dev->reg_mutex);
2566967dd82fSFlorian Fainelli 	mutex_init(&dev->stats_mutex);
2567967dd82fSFlorian Fainelli 
2568967dd82fSFlorian Fainelli 	return dev;
2569967dd82fSFlorian Fainelli }
2570967dd82fSFlorian Fainelli EXPORT_SYMBOL(b53_switch_alloc);
2571967dd82fSFlorian Fainelli 
2572967dd82fSFlorian Fainelli int b53_switch_detect(struct b53_device *dev)
2573967dd82fSFlorian Fainelli {
2574967dd82fSFlorian Fainelli 	u32 id32;
2575967dd82fSFlorian Fainelli 	u16 tmp;
2576967dd82fSFlorian Fainelli 	u8 id8;
2577967dd82fSFlorian Fainelli 	int ret;
2578967dd82fSFlorian Fainelli 
2579967dd82fSFlorian Fainelli 	ret = b53_read8(dev, B53_MGMT_PAGE, B53_DEVICE_ID, &id8);
2580967dd82fSFlorian Fainelli 	if (ret)
2581967dd82fSFlorian Fainelli 		return ret;
2582967dd82fSFlorian Fainelli 
2583967dd82fSFlorian Fainelli 	switch (id8) {
2584967dd82fSFlorian Fainelli 	case 0:
2585967dd82fSFlorian Fainelli 		/* BCM5325 and BCM5365 do not have this register so reads
2586967dd82fSFlorian Fainelli 		 * return 0. But the read operation did succeed, so assume this
2587967dd82fSFlorian Fainelli 		 * is one of them.
2588967dd82fSFlorian Fainelli 		 *
2589967dd82fSFlorian Fainelli 		 * Next check if we can write to the 5325's VTA register; for
2590967dd82fSFlorian Fainelli 		 * 5365 it is read only.
2591967dd82fSFlorian Fainelli 		 */
2592967dd82fSFlorian Fainelli 		b53_write16(dev, B53_VLAN_PAGE, B53_VLAN_TABLE_ACCESS_25, 0xf);
2593967dd82fSFlorian Fainelli 		b53_read16(dev, B53_VLAN_PAGE, B53_VLAN_TABLE_ACCESS_25, &tmp);
2594967dd82fSFlorian Fainelli 
2595967dd82fSFlorian Fainelli 		if (tmp == 0xf)
2596967dd82fSFlorian Fainelli 			dev->chip_id = BCM5325_DEVICE_ID;
2597967dd82fSFlorian Fainelli 		else
2598967dd82fSFlorian Fainelli 			dev->chip_id = BCM5365_DEVICE_ID;
2599967dd82fSFlorian Fainelli 		break;
2600a95691bcSDamien Thébault 	case BCM5389_DEVICE_ID:
2601967dd82fSFlorian Fainelli 	case BCM5395_DEVICE_ID:
2602967dd82fSFlorian Fainelli 	case BCM5397_DEVICE_ID:
2603967dd82fSFlorian Fainelli 	case BCM5398_DEVICE_ID:
2604967dd82fSFlorian Fainelli 		dev->chip_id = id8;
2605967dd82fSFlorian Fainelli 		break;
2606967dd82fSFlorian Fainelli 	default:
2607967dd82fSFlorian Fainelli 		ret = b53_read32(dev, B53_MGMT_PAGE, B53_DEVICE_ID, &id32);
2608967dd82fSFlorian Fainelli 		if (ret)
2609967dd82fSFlorian Fainelli 			return ret;
2610967dd82fSFlorian Fainelli 
2611967dd82fSFlorian Fainelli 		switch (id32) {
2612967dd82fSFlorian Fainelli 		case BCM53115_DEVICE_ID:
2613967dd82fSFlorian Fainelli 		case BCM53125_DEVICE_ID:
2614967dd82fSFlorian Fainelli 		case BCM53128_DEVICE_ID:
2615967dd82fSFlorian Fainelli 		case BCM53010_DEVICE_ID:
2616967dd82fSFlorian Fainelli 		case BCM53011_DEVICE_ID:
2617967dd82fSFlorian Fainelli 		case BCM53012_DEVICE_ID:
2618967dd82fSFlorian Fainelli 		case BCM53018_DEVICE_ID:
2619967dd82fSFlorian Fainelli 		case BCM53019_DEVICE_ID:
2620967dd82fSFlorian Fainelli 			dev->chip_id = id32;
2621967dd82fSFlorian Fainelli 			break;
2622967dd82fSFlorian Fainelli 		default:
2623967dd82fSFlorian Fainelli 			pr_err("unsupported switch detected (BCM53%02x/BCM%x)\n",
2624967dd82fSFlorian Fainelli 			       id8, id32);
2625967dd82fSFlorian Fainelli 			return -ENODEV;
2626967dd82fSFlorian Fainelli 		}
2627967dd82fSFlorian Fainelli 	}
2628967dd82fSFlorian Fainelli 
2629967dd82fSFlorian Fainelli 	if (dev->chip_id == BCM5325_DEVICE_ID)
2630967dd82fSFlorian Fainelli 		return b53_read8(dev, B53_STAT_PAGE, B53_REV_ID_25,
2631967dd82fSFlorian Fainelli 				 &dev->core_rev);
2632967dd82fSFlorian Fainelli 	else
2633967dd82fSFlorian Fainelli 		return b53_read8(dev, B53_MGMT_PAGE, B53_REV_ID,
2634967dd82fSFlorian Fainelli 				 &dev->core_rev);
2635967dd82fSFlorian Fainelli }
2636967dd82fSFlorian Fainelli EXPORT_SYMBOL(b53_switch_detect);
2637967dd82fSFlorian Fainelli 
2638967dd82fSFlorian Fainelli int b53_switch_register(struct b53_device *dev)
2639967dd82fSFlorian Fainelli {
2640967dd82fSFlorian Fainelli 	int ret;
2641967dd82fSFlorian Fainelli 
2642967dd82fSFlorian Fainelli 	if (dev->pdata) {
2643967dd82fSFlorian Fainelli 		dev->chip_id = dev->pdata->chip_id;
2644967dd82fSFlorian Fainelli 		dev->enabled_ports = dev->pdata->enabled_ports;
2645967dd82fSFlorian Fainelli 	}
2646967dd82fSFlorian Fainelli 
2647967dd82fSFlorian Fainelli 	if (!dev->chip_id && b53_switch_detect(dev))
2648967dd82fSFlorian Fainelli 		return -EINVAL;
2649967dd82fSFlorian Fainelli 
2650967dd82fSFlorian Fainelli 	ret = b53_switch_init(dev);
2651967dd82fSFlorian Fainelli 	if (ret)
2652967dd82fSFlorian Fainelli 		return ret;
2653967dd82fSFlorian Fainelli 
2654967dd82fSFlorian Fainelli 	pr_info("found switch: %s, rev %i\n", dev->name, dev->core_rev);
2655967dd82fSFlorian Fainelli 
265623c9ee49SVivien Didelot 	return dsa_register_switch(dev->ds);
2657967dd82fSFlorian Fainelli }
2658967dd82fSFlorian Fainelli EXPORT_SYMBOL(b53_switch_register);
2659967dd82fSFlorian Fainelli 
2660967dd82fSFlorian Fainelli MODULE_AUTHOR("Jonas Gorski <jogo@openwrt.org>");
2661967dd82fSFlorian Fainelli MODULE_DESCRIPTION("B53 switch library");
2662967dd82fSFlorian Fainelli MODULE_LICENSE("Dual BSD/GPL");
2663