xref: /openbmc/linux/drivers/net/dsa/b53/b53_common.c (revision 9f66816a6a4dd740bfa29cc8a8e19b90fd7df4e7)
1967dd82fSFlorian Fainelli /*
2967dd82fSFlorian Fainelli  * B53 switch driver main logic
3967dd82fSFlorian Fainelli  *
4967dd82fSFlorian Fainelli  * Copyright (C) 2011-2013 Jonas Gorski <jogo@openwrt.org>
5967dd82fSFlorian Fainelli  * Copyright (C) 2016 Florian Fainelli <f.fainelli@gmail.com>
6967dd82fSFlorian Fainelli  *
7967dd82fSFlorian Fainelli  * Permission to use, copy, modify, and/or distribute this software for any
8967dd82fSFlorian Fainelli  * purpose with or without fee is hereby granted, provided that the above
9967dd82fSFlorian Fainelli  * copyright notice and this permission notice appear in all copies.
10967dd82fSFlorian Fainelli  *
11967dd82fSFlorian Fainelli  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12967dd82fSFlorian Fainelli  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13967dd82fSFlorian Fainelli  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14967dd82fSFlorian Fainelli  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15967dd82fSFlorian Fainelli  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16967dd82fSFlorian Fainelli  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17967dd82fSFlorian Fainelli  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18967dd82fSFlorian Fainelli  */
19967dd82fSFlorian Fainelli 
20967dd82fSFlorian Fainelli #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
21967dd82fSFlorian Fainelli 
22967dd82fSFlorian Fainelli #include <linux/delay.h>
23967dd82fSFlorian Fainelli #include <linux/export.h>
24967dd82fSFlorian Fainelli #include <linux/gpio.h>
25967dd82fSFlorian Fainelli #include <linux/kernel.h>
26967dd82fSFlorian Fainelli #include <linux/module.h>
27967dd82fSFlorian Fainelli #include <linux/platform_data/b53.h>
28967dd82fSFlorian Fainelli #include <linux/phy.h>
291da6df85SFlorian Fainelli #include <linux/etherdevice.h>
30ff39c2d6SFlorian Fainelli #include <linux/if_bridge.h>
31967dd82fSFlorian Fainelli #include <net/dsa.h>
32967dd82fSFlorian Fainelli 
33967dd82fSFlorian Fainelli #include "b53_regs.h"
34967dd82fSFlorian Fainelli #include "b53_priv.h"
35967dd82fSFlorian Fainelli 
36967dd82fSFlorian Fainelli struct b53_mib_desc {
37967dd82fSFlorian Fainelli 	u8 size;
38967dd82fSFlorian Fainelli 	u8 offset;
39967dd82fSFlorian Fainelli 	const char *name;
40967dd82fSFlorian Fainelli };
41967dd82fSFlorian Fainelli 
42967dd82fSFlorian Fainelli /* BCM5365 MIB counters */
43967dd82fSFlorian Fainelli static const struct b53_mib_desc b53_mibs_65[] = {
44967dd82fSFlorian Fainelli 	{ 8, 0x00, "TxOctets" },
45967dd82fSFlorian Fainelli 	{ 4, 0x08, "TxDropPkts" },
46967dd82fSFlorian Fainelli 	{ 4, 0x10, "TxBroadcastPkts" },
47967dd82fSFlorian Fainelli 	{ 4, 0x14, "TxMulticastPkts" },
48967dd82fSFlorian Fainelli 	{ 4, 0x18, "TxUnicastPkts" },
49967dd82fSFlorian Fainelli 	{ 4, 0x1c, "TxCollisions" },
50967dd82fSFlorian Fainelli 	{ 4, 0x20, "TxSingleCollision" },
51967dd82fSFlorian Fainelli 	{ 4, 0x24, "TxMultipleCollision" },
52967dd82fSFlorian Fainelli 	{ 4, 0x28, "TxDeferredTransmit" },
53967dd82fSFlorian Fainelli 	{ 4, 0x2c, "TxLateCollision" },
54967dd82fSFlorian Fainelli 	{ 4, 0x30, "TxExcessiveCollision" },
55967dd82fSFlorian Fainelli 	{ 4, 0x38, "TxPausePkts" },
56967dd82fSFlorian Fainelli 	{ 8, 0x44, "RxOctets" },
57967dd82fSFlorian Fainelli 	{ 4, 0x4c, "RxUndersizePkts" },
58967dd82fSFlorian Fainelli 	{ 4, 0x50, "RxPausePkts" },
59967dd82fSFlorian Fainelli 	{ 4, 0x54, "Pkts64Octets" },
60967dd82fSFlorian Fainelli 	{ 4, 0x58, "Pkts65to127Octets" },
61967dd82fSFlorian Fainelli 	{ 4, 0x5c, "Pkts128to255Octets" },
62967dd82fSFlorian Fainelli 	{ 4, 0x60, "Pkts256to511Octets" },
63967dd82fSFlorian Fainelli 	{ 4, 0x64, "Pkts512to1023Octets" },
64967dd82fSFlorian Fainelli 	{ 4, 0x68, "Pkts1024to1522Octets" },
65967dd82fSFlorian Fainelli 	{ 4, 0x6c, "RxOversizePkts" },
66967dd82fSFlorian Fainelli 	{ 4, 0x70, "RxJabbers" },
67967dd82fSFlorian Fainelli 	{ 4, 0x74, "RxAlignmentErrors" },
68967dd82fSFlorian Fainelli 	{ 4, 0x78, "RxFCSErrors" },
69967dd82fSFlorian Fainelli 	{ 8, 0x7c, "RxGoodOctets" },
70967dd82fSFlorian Fainelli 	{ 4, 0x84, "RxDropPkts" },
71967dd82fSFlorian Fainelli 	{ 4, 0x88, "RxUnicastPkts" },
72967dd82fSFlorian Fainelli 	{ 4, 0x8c, "RxMulticastPkts" },
73967dd82fSFlorian Fainelli 	{ 4, 0x90, "RxBroadcastPkts" },
74967dd82fSFlorian Fainelli 	{ 4, 0x94, "RxSAChanges" },
75967dd82fSFlorian Fainelli 	{ 4, 0x98, "RxFragments" },
76967dd82fSFlorian Fainelli };
77967dd82fSFlorian Fainelli 
78967dd82fSFlorian Fainelli #define B53_MIBS_65_SIZE	ARRAY_SIZE(b53_mibs_65)
79967dd82fSFlorian Fainelli 
80967dd82fSFlorian Fainelli /* BCM63xx MIB counters */
81967dd82fSFlorian Fainelli static const struct b53_mib_desc b53_mibs_63xx[] = {
82967dd82fSFlorian Fainelli 	{ 8, 0x00, "TxOctets" },
83967dd82fSFlorian Fainelli 	{ 4, 0x08, "TxDropPkts" },
84967dd82fSFlorian Fainelli 	{ 4, 0x0c, "TxQoSPkts" },
85967dd82fSFlorian Fainelli 	{ 4, 0x10, "TxBroadcastPkts" },
86967dd82fSFlorian Fainelli 	{ 4, 0x14, "TxMulticastPkts" },
87967dd82fSFlorian Fainelli 	{ 4, 0x18, "TxUnicastPkts" },
88967dd82fSFlorian Fainelli 	{ 4, 0x1c, "TxCollisions" },
89967dd82fSFlorian Fainelli 	{ 4, 0x20, "TxSingleCollision" },
90967dd82fSFlorian Fainelli 	{ 4, 0x24, "TxMultipleCollision" },
91967dd82fSFlorian Fainelli 	{ 4, 0x28, "TxDeferredTransmit" },
92967dd82fSFlorian Fainelli 	{ 4, 0x2c, "TxLateCollision" },
93967dd82fSFlorian Fainelli 	{ 4, 0x30, "TxExcessiveCollision" },
94967dd82fSFlorian Fainelli 	{ 4, 0x38, "TxPausePkts" },
95967dd82fSFlorian Fainelli 	{ 8, 0x3c, "TxQoSOctets" },
96967dd82fSFlorian Fainelli 	{ 8, 0x44, "RxOctets" },
97967dd82fSFlorian Fainelli 	{ 4, 0x4c, "RxUndersizePkts" },
98967dd82fSFlorian Fainelli 	{ 4, 0x50, "RxPausePkts" },
99967dd82fSFlorian Fainelli 	{ 4, 0x54, "Pkts64Octets" },
100967dd82fSFlorian Fainelli 	{ 4, 0x58, "Pkts65to127Octets" },
101967dd82fSFlorian Fainelli 	{ 4, 0x5c, "Pkts128to255Octets" },
102967dd82fSFlorian Fainelli 	{ 4, 0x60, "Pkts256to511Octets" },
103967dd82fSFlorian Fainelli 	{ 4, 0x64, "Pkts512to1023Octets" },
104967dd82fSFlorian Fainelli 	{ 4, 0x68, "Pkts1024to1522Octets" },
105967dd82fSFlorian Fainelli 	{ 4, 0x6c, "RxOversizePkts" },
106967dd82fSFlorian Fainelli 	{ 4, 0x70, "RxJabbers" },
107967dd82fSFlorian Fainelli 	{ 4, 0x74, "RxAlignmentErrors" },
108967dd82fSFlorian Fainelli 	{ 4, 0x78, "RxFCSErrors" },
109967dd82fSFlorian Fainelli 	{ 8, 0x7c, "RxGoodOctets" },
110967dd82fSFlorian Fainelli 	{ 4, 0x84, "RxDropPkts" },
111967dd82fSFlorian Fainelli 	{ 4, 0x88, "RxUnicastPkts" },
112967dd82fSFlorian Fainelli 	{ 4, 0x8c, "RxMulticastPkts" },
113967dd82fSFlorian Fainelli 	{ 4, 0x90, "RxBroadcastPkts" },
114967dd82fSFlorian Fainelli 	{ 4, 0x94, "RxSAChanges" },
115967dd82fSFlorian Fainelli 	{ 4, 0x98, "RxFragments" },
116967dd82fSFlorian Fainelli 	{ 4, 0xa0, "RxSymbolErrors" },
117967dd82fSFlorian Fainelli 	{ 4, 0xa4, "RxQoSPkts" },
118967dd82fSFlorian Fainelli 	{ 8, 0xa8, "RxQoSOctets" },
119967dd82fSFlorian Fainelli 	{ 4, 0xb0, "Pkts1523to2047Octets" },
120967dd82fSFlorian Fainelli 	{ 4, 0xb4, "Pkts2048to4095Octets" },
121967dd82fSFlorian Fainelli 	{ 4, 0xb8, "Pkts4096to8191Octets" },
122967dd82fSFlorian Fainelli 	{ 4, 0xbc, "Pkts8192to9728Octets" },
123967dd82fSFlorian Fainelli 	{ 4, 0xc0, "RxDiscarded" },
124967dd82fSFlorian Fainelli };
125967dd82fSFlorian Fainelli 
126967dd82fSFlorian Fainelli #define B53_MIBS_63XX_SIZE	ARRAY_SIZE(b53_mibs_63xx)
127967dd82fSFlorian Fainelli 
128967dd82fSFlorian Fainelli /* MIB counters */
129967dd82fSFlorian Fainelli static const struct b53_mib_desc b53_mibs[] = {
130967dd82fSFlorian Fainelli 	{ 8, 0x00, "TxOctets" },
131967dd82fSFlorian Fainelli 	{ 4, 0x08, "TxDropPkts" },
132967dd82fSFlorian Fainelli 	{ 4, 0x10, "TxBroadcastPkts" },
133967dd82fSFlorian Fainelli 	{ 4, 0x14, "TxMulticastPkts" },
134967dd82fSFlorian Fainelli 	{ 4, 0x18, "TxUnicastPkts" },
135967dd82fSFlorian Fainelli 	{ 4, 0x1c, "TxCollisions" },
136967dd82fSFlorian Fainelli 	{ 4, 0x20, "TxSingleCollision" },
137967dd82fSFlorian Fainelli 	{ 4, 0x24, "TxMultipleCollision" },
138967dd82fSFlorian Fainelli 	{ 4, 0x28, "TxDeferredTransmit" },
139967dd82fSFlorian Fainelli 	{ 4, 0x2c, "TxLateCollision" },
140967dd82fSFlorian Fainelli 	{ 4, 0x30, "TxExcessiveCollision" },
141967dd82fSFlorian Fainelli 	{ 4, 0x38, "TxPausePkts" },
142967dd82fSFlorian Fainelli 	{ 8, 0x50, "RxOctets" },
143967dd82fSFlorian Fainelli 	{ 4, 0x58, "RxUndersizePkts" },
144967dd82fSFlorian Fainelli 	{ 4, 0x5c, "RxPausePkts" },
145967dd82fSFlorian Fainelli 	{ 4, 0x60, "Pkts64Octets" },
146967dd82fSFlorian Fainelli 	{ 4, 0x64, "Pkts65to127Octets" },
147967dd82fSFlorian Fainelli 	{ 4, 0x68, "Pkts128to255Octets" },
148967dd82fSFlorian Fainelli 	{ 4, 0x6c, "Pkts256to511Octets" },
149967dd82fSFlorian Fainelli 	{ 4, 0x70, "Pkts512to1023Octets" },
150967dd82fSFlorian Fainelli 	{ 4, 0x74, "Pkts1024to1522Octets" },
151967dd82fSFlorian Fainelli 	{ 4, 0x78, "RxOversizePkts" },
152967dd82fSFlorian Fainelli 	{ 4, 0x7c, "RxJabbers" },
153967dd82fSFlorian Fainelli 	{ 4, 0x80, "RxAlignmentErrors" },
154967dd82fSFlorian Fainelli 	{ 4, 0x84, "RxFCSErrors" },
155967dd82fSFlorian Fainelli 	{ 8, 0x88, "RxGoodOctets" },
156967dd82fSFlorian Fainelli 	{ 4, 0x90, "RxDropPkts" },
157967dd82fSFlorian Fainelli 	{ 4, 0x94, "RxUnicastPkts" },
158967dd82fSFlorian Fainelli 	{ 4, 0x98, "RxMulticastPkts" },
159967dd82fSFlorian Fainelli 	{ 4, 0x9c, "RxBroadcastPkts" },
160967dd82fSFlorian Fainelli 	{ 4, 0xa0, "RxSAChanges" },
161967dd82fSFlorian Fainelli 	{ 4, 0xa4, "RxFragments" },
162967dd82fSFlorian Fainelli 	{ 4, 0xa8, "RxJumboPkts" },
163967dd82fSFlorian Fainelli 	{ 4, 0xac, "RxSymbolErrors" },
164967dd82fSFlorian Fainelli 	{ 4, 0xc0, "RxDiscarded" },
165967dd82fSFlorian Fainelli };
166967dd82fSFlorian Fainelli 
167967dd82fSFlorian Fainelli #define B53_MIBS_SIZE	ARRAY_SIZE(b53_mibs)
168967dd82fSFlorian Fainelli 
169bde5d132SFlorian Fainelli static const struct b53_mib_desc b53_mibs_58xx[] = {
170bde5d132SFlorian Fainelli 	{ 8, 0x00, "TxOctets" },
171bde5d132SFlorian Fainelli 	{ 4, 0x08, "TxDropPkts" },
172bde5d132SFlorian Fainelli 	{ 4, 0x0c, "TxQPKTQ0" },
173bde5d132SFlorian Fainelli 	{ 4, 0x10, "TxBroadcastPkts" },
174bde5d132SFlorian Fainelli 	{ 4, 0x14, "TxMulticastPkts" },
175bde5d132SFlorian Fainelli 	{ 4, 0x18, "TxUnicastPKts" },
176bde5d132SFlorian Fainelli 	{ 4, 0x1c, "TxCollisions" },
177bde5d132SFlorian Fainelli 	{ 4, 0x20, "TxSingleCollision" },
178bde5d132SFlorian Fainelli 	{ 4, 0x24, "TxMultipleCollision" },
179bde5d132SFlorian Fainelli 	{ 4, 0x28, "TxDeferredCollision" },
180bde5d132SFlorian Fainelli 	{ 4, 0x2c, "TxLateCollision" },
181bde5d132SFlorian Fainelli 	{ 4, 0x30, "TxExcessiveCollision" },
182bde5d132SFlorian Fainelli 	{ 4, 0x34, "TxFrameInDisc" },
183bde5d132SFlorian Fainelli 	{ 4, 0x38, "TxPausePkts" },
184bde5d132SFlorian Fainelli 	{ 4, 0x3c, "TxQPKTQ1" },
185bde5d132SFlorian Fainelli 	{ 4, 0x40, "TxQPKTQ2" },
186bde5d132SFlorian Fainelli 	{ 4, 0x44, "TxQPKTQ3" },
187bde5d132SFlorian Fainelli 	{ 4, 0x48, "TxQPKTQ4" },
188bde5d132SFlorian Fainelli 	{ 4, 0x4c, "TxQPKTQ5" },
189bde5d132SFlorian Fainelli 	{ 8, 0x50, "RxOctets" },
190bde5d132SFlorian Fainelli 	{ 4, 0x58, "RxUndersizePkts" },
191bde5d132SFlorian Fainelli 	{ 4, 0x5c, "RxPausePkts" },
192bde5d132SFlorian Fainelli 	{ 4, 0x60, "RxPkts64Octets" },
193bde5d132SFlorian Fainelli 	{ 4, 0x64, "RxPkts65to127Octets" },
194bde5d132SFlorian Fainelli 	{ 4, 0x68, "RxPkts128to255Octets" },
195bde5d132SFlorian Fainelli 	{ 4, 0x6c, "RxPkts256to511Octets" },
196bde5d132SFlorian Fainelli 	{ 4, 0x70, "RxPkts512to1023Octets" },
197bde5d132SFlorian Fainelli 	{ 4, 0x74, "RxPkts1024toMaxPktsOctets" },
198bde5d132SFlorian Fainelli 	{ 4, 0x78, "RxOversizePkts" },
199bde5d132SFlorian Fainelli 	{ 4, 0x7c, "RxJabbers" },
200bde5d132SFlorian Fainelli 	{ 4, 0x80, "RxAlignmentErrors" },
201bde5d132SFlorian Fainelli 	{ 4, 0x84, "RxFCSErrors" },
202bde5d132SFlorian Fainelli 	{ 8, 0x88, "RxGoodOctets" },
203bde5d132SFlorian Fainelli 	{ 4, 0x90, "RxDropPkts" },
204bde5d132SFlorian Fainelli 	{ 4, 0x94, "RxUnicastPkts" },
205bde5d132SFlorian Fainelli 	{ 4, 0x98, "RxMulticastPkts" },
206bde5d132SFlorian Fainelli 	{ 4, 0x9c, "RxBroadcastPkts" },
207bde5d132SFlorian Fainelli 	{ 4, 0xa0, "RxSAChanges" },
208bde5d132SFlorian Fainelli 	{ 4, 0xa4, "RxFragments" },
209bde5d132SFlorian Fainelli 	{ 4, 0xa8, "RxJumboPkt" },
210bde5d132SFlorian Fainelli 	{ 4, 0xac, "RxSymblErr" },
211bde5d132SFlorian Fainelli 	{ 4, 0xb0, "InRangeErrCount" },
212bde5d132SFlorian Fainelli 	{ 4, 0xb4, "OutRangeErrCount" },
213bde5d132SFlorian Fainelli 	{ 4, 0xb8, "EEELpiEvent" },
214bde5d132SFlorian Fainelli 	{ 4, 0xbc, "EEELpiDuration" },
215bde5d132SFlorian Fainelli 	{ 4, 0xc0, "RxDiscard" },
216bde5d132SFlorian Fainelli 	{ 4, 0xc8, "TxQPKTQ6" },
217bde5d132SFlorian Fainelli 	{ 4, 0xcc, "TxQPKTQ7" },
218bde5d132SFlorian Fainelli 	{ 4, 0xd0, "TxPkts64Octets" },
219bde5d132SFlorian Fainelli 	{ 4, 0xd4, "TxPkts65to127Octets" },
220bde5d132SFlorian Fainelli 	{ 4, 0xd8, "TxPkts128to255Octets" },
221bde5d132SFlorian Fainelli 	{ 4, 0xdc, "TxPkts256to511Ocets" },
222bde5d132SFlorian Fainelli 	{ 4, 0xe0, "TxPkts512to1023Ocets" },
223bde5d132SFlorian Fainelli 	{ 4, 0xe4, "TxPkts1024toMaxPktOcets" },
224bde5d132SFlorian Fainelli };
225bde5d132SFlorian Fainelli 
226bde5d132SFlorian Fainelli #define B53_MIBS_58XX_SIZE	ARRAY_SIZE(b53_mibs_58xx)
227bde5d132SFlorian Fainelli 
228967dd82fSFlorian Fainelli static int b53_do_vlan_op(struct b53_device *dev, u8 op)
229967dd82fSFlorian Fainelli {
230967dd82fSFlorian Fainelli 	unsigned int i;
231967dd82fSFlorian Fainelli 
232967dd82fSFlorian Fainelli 	b53_write8(dev, B53_ARLIO_PAGE, dev->vta_regs[0], VTA_START_CMD | op);
233967dd82fSFlorian Fainelli 
234967dd82fSFlorian Fainelli 	for (i = 0; i < 10; i++) {
235967dd82fSFlorian Fainelli 		u8 vta;
236967dd82fSFlorian Fainelli 
237967dd82fSFlorian Fainelli 		b53_read8(dev, B53_ARLIO_PAGE, dev->vta_regs[0], &vta);
238967dd82fSFlorian Fainelli 		if (!(vta & VTA_START_CMD))
239967dd82fSFlorian Fainelli 			return 0;
240967dd82fSFlorian Fainelli 
241967dd82fSFlorian Fainelli 		usleep_range(100, 200);
242967dd82fSFlorian Fainelli 	}
243967dd82fSFlorian Fainelli 
244967dd82fSFlorian Fainelli 	return -EIO;
245967dd82fSFlorian Fainelli }
246967dd82fSFlorian Fainelli 
247a2482d2cSFlorian Fainelli static void b53_set_vlan_entry(struct b53_device *dev, u16 vid,
248a2482d2cSFlorian Fainelli 			       struct b53_vlan *vlan)
249967dd82fSFlorian Fainelli {
250967dd82fSFlorian Fainelli 	if (is5325(dev)) {
251967dd82fSFlorian Fainelli 		u32 entry = 0;
252967dd82fSFlorian Fainelli 
253a2482d2cSFlorian Fainelli 		if (vlan->members) {
254a2482d2cSFlorian Fainelli 			entry = ((vlan->untag & VA_UNTAG_MASK_25) <<
255a2482d2cSFlorian Fainelli 				 VA_UNTAG_S_25) | vlan->members;
256967dd82fSFlorian Fainelli 			if (dev->core_rev >= 3)
257967dd82fSFlorian Fainelli 				entry |= VA_VALID_25_R4 | vid << VA_VID_HIGH_S;
258967dd82fSFlorian Fainelli 			else
259967dd82fSFlorian Fainelli 				entry |= VA_VALID_25;
260967dd82fSFlorian Fainelli 		}
261967dd82fSFlorian Fainelli 
262967dd82fSFlorian Fainelli 		b53_write32(dev, B53_VLAN_PAGE, B53_VLAN_WRITE_25, entry);
263967dd82fSFlorian Fainelli 		b53_write16(dev, B53_VLAN_PAGE, B53_VLAN_TABLE_ACCESS_25, vid |
264967dd82fSFlorian Fainelli 			    VTA_RW_STATE_WR | VTA_RW_OP_EN);
265967dd82fSFlorian Fainelli 	} else if (is5365(dev)) {
266967dd82fSFlorian Fainelli 		u16 entry = 0;
267967dd82fSFlorian Fainelli 
268a2482d2cSFlorian Fainelli 		if (vlan->members)
269a2482d2cSFlorian Fainelli 			entry = ((vlan->untag & VA_UNTAG_MASK_65) <<
270a2482d2cSFlorian Fainelli 				 VA_UNTAG_S_65) | vlan->members | VA_VALID_65;
271967dd82fSFlorian Fainelli 
272967dd82fSFlorian Fainelli 		b53_write16(dev, B53_VLAN_PAGE, B53_VLAN_WRITE_65, entry);
273967dd82fSFlorian Fainelli 		b53_write16(dev, B53_VLAN_PAGE, B53_VLAN_TABLE_ACCESS_65, vid |
274967dd82fSFlorian Fainelli 			    VTA_RW_STATE_WR | VTA_RW_OP_EN);
275967dd82fSFlorian Fainelli 	} else {
276967dd82fSFlorian Fainelli 		b53_write16(dev, B53_ARLIO_PAGE, dev->vta_regs[1], vid);
277967dd82fSFlorian Fainelli 		b53_write32(dev, B53_ARLIO_PAGE, dev->vta_regs[2],
278a2482d2cSFlorian Fainelli 			    (vlan->untag << VTE_UNTAG_S) | vlan->members);
279967dd82fSFlorian Fainelli 
280967dd82fSFlorian Fainelli 		b53_do_vlan_op(dev, VTA_CMD_WRITE);
281967dd82fSFlorian Fainelli 	}
282a2482d2cSFlorian Fainelli 
283a2482d2cSFlorian Fainelli 	dev_dbg(dev->ds->dev, "VID: %d, members: 0x%04x, untag: 0x%04x\n",
284a2482d2cSFlorian Fainelli 		vid, vlan->members, vlan->untag);
285967dd82fSFlorian Fainelli }
286967dd82fSFlorian Fainelli 
287a2482d2cSFlorian Fainelli static void b53_get_vlan_entry(struct b53_device *dev, u16 vid,
288a2482d2cSFlorian Fainelli 			       struct b53_vlan *vlan)
289a2482d2cSFlorian Fainelli {
290a2482d2cSFlorian Fainelli 	if (is5325(dev)) {
291a2482d2cSFlorian Fainelli 		u32 entry = 0;
292a2482d2cSFlorian Fainelli 
293a2482d2cSFlorian Fainelli 		b53_write16(dev, B53_VLAN_PAGE, B53_VLAN_TABLE_ACCESS_25, vid |
294a2482d2cSFlorian Fainelli 			    VTA_RW_STATE_RD | VTA_RW_OP_EN);
295a2482d2cSFlorian Fainelli 		b53_read32(dev, B53_VLAN_PAGE, B53_VLAN_WRITE_25, &entry);
296a2482d2cSFlorian Fainelli 
297a2482d2cSFlorian Fainelli 		if (dev->core_rev >= 3)
298a2482d2cSFlorian Fainelli 			vlan->valid = !!(entry & VA_VALID_25_R4);
299a2482d2cSFlorian Fainelli 		else
300a2482d2cSFlorian Fainelli 			vlan->valid = !!(entry & VA_VALID_25);
301a2482d2cSFlorian Fainelli 		vlan->members = entry & VA_MEMBER_MASK;
302a2482d2cSFlorian Fainelli 		vlan->untag = (entry >> VA_UNTAG_S_25) & VA_UNTAG_MASK_25;
303a2482d2cSFlorian Fainelli 
304a2482d2cSFlorian Fainelli 	} else if (is5365(dev)) {
305a2482d2cSFlorian Fainelli 		u16 entry = 0;
306a2482d2cSFlorian Fainelli 
307a2482d2cSFlorian Fainelli 		b53_write16(dev, B53_VLAN_PAGE, B53_VLAN_TABLE_ACCESS_65, vid |
308a2482d2cSFlorian Fainelli 			    VTA_RW_STATE_WR | VTA_RW_OP_EN);
309a2482d2cSFlorian Fainelli 		b53_read16(dev, B53_VLAN_PAGE, B53_VLAN_WRITE_65, &entry);
310a2482d2cSFlorian Fainelli 
311a2482d2cSFlorian Fainelli 		vlan->valid = !!(entry & VA_VALID_65);
312a2482d2cSFlorian Fainelli 		vlan->members = entry & VA_MEMBER_MASK;
313a2482d2cSFlorian Fainelli 		vlan->untag = (entry >> VA_UNTAG_S_65) & VA_UNTAG_MASK_65;
314a2482d2cSFlorian Fainelli 	} else {
315a2482d2cSFlorian Fainelli 		u32 entry = 0;
316a2482d2cSFlorian Fainelli 
317a2482d2cSFlorian Fainelli 		b53_write16(dev, B53_ARLIO_PAGE, dev->vta_regs[1], vid);
318a2482d2cSFlorian Fainelli 		b53_do_vlan_op(dev, VTA_CMD_READ);
319a2482d2cSFlorian Fainelli 		b53_read32(dev, B53_ARLIO_PAGE, dev->vta_regs[2], &entry);
320a2482d2cSFlorian Fainelli 		vlan->members = entry & VTE_MEMBERS;
321a2482d2cSFlorian Fainelli 		vlan->untag = (entry >> VTE_UNTAG_S) & VTE_MEMBERS;
322a2482d2cSFlorian Fainelli 		vlan->valid = true;
323a2482d2cSFlorian Fainelli 	}
324a2482d2cSFlorian Fainelli }
325a2482d2cSFlorian Fainelli 
326a2482d2cSFlorian Fainelli static void b53_set_forwarding(struct b53_device *dev, int enable)
327967dd82fSFlorian Fainelli {
328967dd82fSFlorian Fainelli 	u8 mgmt;
329967dd82fSFlorian Fainelli 
330967dd82fSFlorian Fainelli 	b53_read8(dev, B53_CTRL_PAGE, B53_SWITCH_MODE, &mgmt);
331967dd82fSFlorian Fainelli 
332967dd82fSFlorian Fainelli 	if (enable)
333967dd82fSFlorian Fainelli 		mgmt |= SM_SW_FWD_EN;
334967dd82fSFlorian Fainelli 	else
335967dd82fSFlorian Fainelli 		mgmt &= ~SM_SW_FWD_EN;
336967dd82fSFlorian Fainelli 
337967dd82fSFlorian Fainelli 	b53_write8(dev, B53_CTRL_PAGE, B53_SWITCH_MODE, mgmt);
338a424f0deSFlorian Fainelli 
3397edc58d6SFlorian Fainelli 	/* Include IMP port in dumb forwarding mode
340a424f0deSFlorian Fainelli 	 */
341a424f0deSFlorian Fainelli 	b53_read8(dev, B53_CTRL_PAGE, B53_SWITCH_CTRL, &mgmt);
342a424f0deSFlorian Fainelli 	mgmt |= B53_MII_DUMB_FWDG_EN;
343a424f0deSFlorian Fainelli 	b53_write8(dev, B53_CTRL_PAGE, B53_SWITCH_CTRL, mgmt);
344a424f0deSFlorian Fainelli }
345967dd82fSFlorian Fainelli 
346a2482d2cSFlorian Fainelli static void b53_enable_vlan(struct b53_device *dev, bool enable)
347967dd82fSFlorian Fainelli {
348967dd82fSFlorian Fainelli 	u8 mgmt, vc0, vc1, vc4 = 0, vc5;
349967dd82fSFlorian Fainelli 
350967dd82fSFlorian Fainelli 	b53_read8(dev, B53_CTRL_PAGE, B53_SWITCH_MODE, &mgmt);
351967dd82fSFlorian Fainelli 	b53_read8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL0, &vc0);
352967dd82fSFlorian Fainelli 	b53_read8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL1, &vc1);
353967dd82fSFlorian Fainelli 
354967dd82fSFlorian Fainelli 	if (is5325(dev) || is5365(dev)) {
355967dd82fSFlorian Fainelli 		b53_read8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL4_25, &vc4);
356967dd82fSFlorian Fainelli 		b53_read8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL5_25, &vc5);
357967dd82fSFlorian Fainelli 	} else if (is63xx(dev)) {
358967dd82fSFlorian Fainelli 		b53_read8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL4_63XX, &vc4);
359967dd82fSFlorian Fainelli 		b53_read8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL5_63XX, &vc5);
360967dd82fSFlorian Fainelli 	} else {
361967dd82fSFlorian Fainelli 		b53_read8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL4, &vc4);
362967dd82fSFlorian Fainelli 		b53_read8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL5, &vc5);
363967dd82fSFlorian Fainelli 	}
364967dd82fSFlorian Fainelli 
365967dd82fSFlorian Fainelli 	mgmt &= ~SM_SW_FWD_MODE;
366967dd82fSFlorian Fainelli 
367967dd82fSFlorian Fainelli 	if (enable) {
368967dd82fSFlorian Fainelli 		vc0 |= VC0_VLAN_EN | VC0_VID_CHK_EN | VC0_VID_HASH_VID;
369967dd82fSFlorian Fainelli 		vc1 |= VC1_RX_MCST_UNTAG_EN | VC1_RX_MCST_FWD_EN;
370967dd82fSFlorian Fainelli 		vc4 &= ~VC4_ING_VID_CHECK_MASK;
371967dd82fSFlorian Fainelli 		vc4 |= VC4_ING_VID_VIO_DROP << VC4_ING_VID_CHECK_S;
372967dd82fSFlorian Fainelli 		vc5 |= VC5_DROP_VTABLE_MISS;
373967dd82fSFlorian Fainelli 
374967dd82fSFlorian Fainelli 		if (is5325(dev))
375967dd82fSFlorian Fainelli 			vc0 &= ~VC0_RESERVED_1;
376967dd82fSFlorian Fainelli 
377967dd82fSFlorian Fainelli 		if (is5325(dev) || is5365(dev))
378967dd82fSFlorian Fainelli 			vc1 |= VC1_RX_MCST_TAG_EN;
379967dd82fSFlorian Fainelli 
380967dd82fSFlorian Fainelli 	} else {
381967dd82fSFlorian Fainelli 		vc0 &= ~(VC0_VLAN_EN | VC0_VID_CHK_EN | VC0_VID_HASH_VID);
382967dd82fSFlorian Fainelli 		vc1 &= ~(VC1_RX_MCST_UNTAG_EN | VC1_RX_MCST_FWD_EN);
383967dd82fSFlorian Fainelli 		vc4 &= ~VC4_ING_VID_CHECK_MASK;
384967dd82fSFlorian Fainelli 		vc5 &= ~VC5_DROP_VTABLE_MISS;
385967dd82fSFlorian Fainelli 
386967dd82fSFlorian Fainelli 		if (is5325(dev) || is5365(dev))
387967dd82fSFlorian Fainelli 			vc4 |= VC4_ING_VID_VIO_FWD << VC4_ING_VID_CHECK_S;
388967dd82fSFlorian Fainelli 		else
389967dd82fSFlorian Fainelli 			vc4 |= VC4_ING_VID_VIO_TO_IMP << VC4_ING_VID_CHECK_S;
390967dd82fSFlorian Fainelli 
391967dd82fSFlorian Fainelli 		if (is5325(dev) || is5365(dev))
392967dd82fSFlorian Fainelli 			vc1 &= ~VC1_RX_MCST_TAG_EN;
393a2482d2cSFlorian Fainelli 	}
394967dd82fSFlorian Fainelli 
395967dd82fSFlorian Fainelli 	if (!is5325(dev) && !is5365(dev))
396967dd82fSFlorian Fainelli 		vc5 &= ~VC5_VID_FFF_EN;
397967dd82fSFlorian Fainelli 
398967dd82fSFlorian Fainelli 	b53_write8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL0, vc0);
399967dd82fSFlorian Fainelli 	b53_write8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL1, vc1);
400967dd82fSFlorian Fainelli 
401967dd82fSFlorian Fainelli 	if (is5325(dev) || is5365(dev)) {
402967dd82fSFlorian Fainelli 		/* enable the high 8 bit vid check on 5325 */
403967dd82fSFlorian Fainelli 		if (is5325(dev) && enable)
404967dd82fSFlorian Fainelli 			b53_write8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL3,
405967dd82fSFlorian Fainelli 				   VC3_HIGH_8BIT_EN);
406967dd82fSFlorian Fainelli 		else
407967dd82fSFlorian Fainelli 			b53_write8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL3, 0);
408967dd82fSFlorian Fainelli 
409967dd82fSFlorian Fainelli 		b53_write8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL4_25, vc4);
410967dd82fSFlorian Fainelli 		b53_write8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL5_25, vc5);
411967dd82fSFlorian Fainelli 	} else if (is63xx(dev)) {
412967dd82fSFlorian Fainelli 		b53_write16(dev, B53_VLAN_PAGE, B53_VLAN_CTRL3_63XX, 0);
413967dd82fSFlorian Fainelli 		b53_write8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL4_63XX, vc4);
414967dd82fSFlorian Fainelli 		b53_write8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL5_63XX, vc5);
415967dd82fSFlorian Fainelli 	} else {
416967dd82fSFlorian Fainelli 		b53_write16(dev, B53_VLAN_PAGE, B53_VLAN_CTRL3, 0);
417967dd82fSFlorian Fainelli 		b53_write8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL4, vc4);
418967dd82fSFlorian Fainelli 		b53_write8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL5, vc5);
419967dd82fSFlorian Fainelli 	}
420967dd82fSFlorian Fainelli 
421967dd82fSFlorian Fainelli 	b53_write8(dev, B53_CTRL_PAGE, B53_SWITCH_MODE, mgmt);
422967dd82fSFlorian Fainelli }
423967dd82fSFlorian Fainelli 
424967dd82fSFlorian Fainelli static int b53_set_jumbo(struct b53_device *dev, bool enable, bool allow_10_100)
425967dd82fSFlorian Fainelli {
426967dd82fSFlorian Fainelli 	u32 port_mask = 0;
427967dd82fSFlorian Fainelli 	u16 max_size = JMS_MIN_SIZE;
428967dd82fSFlorian Fainelli 
429967dd82fSFlorian Fainelli 	if (is5325(dev) || is5365(dev))
430967dd82fSFlorian Fainelli 		return -EINVAL;
431967dd82fSFlorian Fainelli 
432967dd82fSFlorian Fainelli 	if (enable) {
433967dd82fSFlorian Fainelli 		port_mask = dev->enabled_ports;
434967dd82fSFlorian Fainelli 		max_size = JMS_MAX_SIZE;
435967dd82fSFlorian Fainelli 		if (allow_10_100)
436967dd82fSFlorian Fainelli 			port_mask |= JPM_10_100_JUMBO_EN;
437967dd82fSFlorian Fainelli 	}
438967dd82fSFlorian Fainelli 
439967dd82fSFlorian Fainelli 	b53_write32(dev, B53_JUMBO_PAGE, dev->jumbo_pm_reg, port_mask);
440967dd82fSFlorian Fainelli 	return b53_write16(dev, B53_JUMBO_PAGE, dev->jumbo_size_reg, max_size);
441967dd82fSFlorian Fainelli }
442967dd82fSFlorian Fainelli 
443ff39c2d6SFlorian Fainelli static int b53_flush_arl(struct b53_device *dev, u8 mask)
444967dd82fSFlorian Fainelli {
445967dd82fSFlorian Fainelli 	unsigned int i;
446967dd82fSFlorian Fainelli 
447967dd82fSFlorian Fainelli 	b53_write8(dev, B53_CTRL_PAGE, B53_FAST_AGE_CTRL,
448ff39c2d6SFlorian Fainelli 		   FAST_AGE_DONE | FAST_AGE_DYNAMIC | mask);
449967dd82fSFlorian Fainelli 
450967dd82fSFlorian Fainelli 	for (i = 0; i < 10; i++) {
451967dd82fSFlorian Fainelli 		u8 fast_age_ctrl;
452967dd82fSFlorian Fainelli 
453967dd82fSFlorian Fainelli 		b53_read8(dev, B53_CTRL_PAGE, B53_FAST_AGE_CTRL,
454967dd82fSFlorian Fainelli 			  &fast_age_ctrl);
455967dd82fSFlorian Fainelli 
456967dd82fSFlorian Fainelli 		if (!(fast_age_ctrl & FAST_AGE_DONE))
457967dd82fSFlorian Fainelli 			goto out;
458967dd82fSFlorian Fainelli 
459967dd82fSFlorian Fainelli 		msleep(1);
460967dd82fSFlorian Fainelli 	}
461967dd82fSFlorian Fainelli 
462967dd82fSFlorian Fainelli 	return -ETIMEDOUT;
463967dd82fSFlorian Fainelli out:
464967dd82fSFlorian Fainelli 	/* Only age dynamic entries (default behavior) */
465967dd82fSFlorian Fainelli 	b53_write8(dev, B53_CTRL_PAGE, B53_FAST_AGE_CTRL, FAST_AGE_DYNAMIC);
466967dd82fSFlorian Fainelli 	return 0;
467967dd82fSFlorian Fainelli }
468967dd82fSFlorian Fainelli 
469ff39c2d6SFlorian Fainelli static int b53_fast_age_port(struct b53_device *dev, int port)
470ff39c2d6SFlorian Fainelli {
471ff39c2d6SFlorian Fainelli 	b53_write8(dev, B53_CTRL_PAGE, B53_FAST_AGE_PORT_CTRL, port);
472ff39c2d6SFlorian Fainelli 
473ff39c2d6SFlorian Fainelli 	return b53_flush_arl(dev, FAST_AGE_PORT);
474ff39c2d6SFlorian Fainelli }
475ff39c2d6SFlorian Fainelli 
476a2482d2cSFlorian Fainelli static int b53_fast_age_vlan(struct b53_device *dev, u16 vid)
477a2482d2cSFlorian Fainelli {
478a2482d2cSFlorian Fainelli 	b53_write16(dev, B53_CTRL_PAGE, B53_FAST_AGE_VID_CTRL, vid);
479a2482d2cSFlorian Fainelli 
480a2482d2cSFlorian Fainelli 	return b53_flush_arl(dev, FAST_AGE_VLAN);
481a2482d2cSFlorian Fainelli }
482a2482d2cSFlorian Fainelli 
483aac02867SFlorian Fainelli void b53_imp_vlan_setup(struct dsa_switch *ds, int cpu_port)
484ff39c2d6SFlorian Fainelli {
48504bed143SVivien Didelot 	struct b53_device *dev = ds->priv;
486ff39c2d6SFlorian Fainelli 	unsigned int i;
487ff39c2d6SFlorian Fainelli 	u16 pvlan;
488ff39c2d6SFlorian Fainelli 
489ff39c2d6SFlorian Fainelli 	/* Enable the IMP port to be in the same VLAN as the other ports
490ff39c2d6SFlorian Fainelli 	 * on a per-port basis such that we only have Port i and IMP in
491ff39c2d6SFlorian Fainelli 	 * the same VLAN.
492ff39c2d6SFlorian Fainelli 	 */
493ff39c2d6SFlorian Fainelli 	b53_for_each_port(dev, i) {
494ff39c2d6SFlorian Fainelli 		b53_read16(dev, B53_PVLAN_PAGE, B53_PVLAN_PORT_MASK(i), &pvlan);
495ff39c2d6SFlorian Fainelli 		pvlan |= BIT(cpu_port);
496ff39c2d6SFlorian Fainelli 		b53_write16(dev, B53_PVLAN_PAGE, B53_PVLAN_PORT_MASK(i), pvlan);
497ff39c2d6SFlorian Fainelli 	}
498ff39c2d6SFlorian Fainelli }
499aac02867SFlorian Fainelli EXPORT_SYMBOL(b53_imp_vlan_setup);
500ff39c2d6SFlorian Fainelli 
501f86ad77fSFlorian Fainelli int b53_enable_port(struct dsa_switch *ds, int port, struct phy_device *phy)
502967dd82fSFlorian Fainelli {
50304bed143SVivien Didelot 	struct b53_device *dev = ds->priv;
504c499696eSFlorian Fainelli 	unsigned int cpu_port = ds->ports[port].cpu_dp->index;
505ff39c2d6SFlorian Fainelli 	u16 pvlan;
506967dd82fSFlorian Fainelli 
507967dd82fSFlorian Fainelli 	/* Clear the Rx and Tx disable bits and set to no spanning tree */
508967dd82fSFlorian Fainelli 	b53_write8(dev, B53_CTRL_PAGE, B53_PORT_CTRL(port), 0);
509967dd82fSFlorian Fainelli 
510ff39c2d6SFlorian Fainelli 	/* Set this port, and only this one to be in the default VLAN,
511ff39c2d6SFlorian Fainelli 	 * if member of a bridge, restore its membership prior to
512ff39c2d6SFlorian Fainelli 	 * bringing down this port.
513ff39c2d6SFlorian Fainelli 	 */
514ff39c2d6SFlorian Fainelli 	b53_read16(dev, B53_PVLAN_PAGE, B53_PVLAN_PORT_MASK(port), &pvlan);
515ff39c2d6SFlorian Fainelli 	pvlan &= ~0x1ff;
516ff39c2d6SFlorian Fainelli 	pvlan |= BIT(port);
517ff39c2d6SFlorian Fainelli 	pvlan |= dev->ports[port].vlan_ctl_mask;
518ff39c2d6SFlorian Fainelli 	b53_write16(dev, B53_PVLAN_PAGE, B53_PVLAN_PORT_MASK(port), pvlan);
519ff39c2d6SFlorian Fainelli 
520ff39c2d6SFlorian Fainelli 	b53_imp_vlan_setup(ds, cpu_port);
521ff39c2d6SFlorian Fainelli 
522f43a2dbeSFlorian Fainelli 	/* If EEE was enabled, restore it */
523f43a2dbeSFlorian Fainelli 	if (dev->ports[port].eee.eee_enabled)
524f43a2dbeSFlorian Fainelli 		b53_eee_enable_set(ds, port, true);
525f43a2dbeSFlorian Fainelli 
526967dd82fSFlorian Fainelli 	return 0;
527967dd82fSFlorian Fainelli }
528f86ad77fSFlorian Fainelli EXPORT_SYMBOL(b53_enable_port);
529967dd82fSFlorian Fainelli 
530f86ad77fSFlorian Fainelli void b53_disable_port(struct dsa_switch *ds, int port, struct phy_device *phy)
531967dd82fSFlorian Fainelli {
53204bed143SVivien Didelot 	struct b53_device *dev = ds->priv;
533967dd82fSFlorian Fainelli 	u8 reg;
534967dd82fSFlorian Fainelli 
535967dd82fSFlorian Fainelli 	/* Disable Tx/Rx for the port */
536967dd82fSFlorian Fainelli 	b53_read8(dev, B53_CTRL_PAGE, B53_PORT_CTRL(port), &reg);
537967dd82fSFlorian Fainelli 	reg |= PORT_CTRL_RX_DISABLE | PORT_CTRL_TX_DISABLE;
538967dd82fSFlorian Fainelli 	b53_write8(dev, B53_CTRL_PAGE, B53_PORT_CTRL(port), reg);
539967dd82fSFlorian Fainelli }
540f86ad77fSFlorian Fainelli EXPORT_SYMBOL(b53_disable_port);
541967dd82fSFlorian Fainelli 
542b409a9efSFlorian Fainelli void b53_brcm_hdr_setup(struct dsa_switch *ds, int port)
543b409a9efSFlorian Fainelli {
54411606039SFlorian Fainelli 	bool tag_en = !(ds->ops->get_tag_protocol(ds, port) ==
54511606039SFlorian Fainelli 			 DSA_TAG_PROTO_NONE);
546b409a9efSFlorian Fainelli 	struct b53_device *dev = ds->priv;
547b409a9efSFlorian Fainelli 	u8 hdr_ctl, val;
548b409a9efSFlorian Fainelli 	u16 reg;
549b409a9efSFlorian Fainelli 
550b409a9efSFlorian Fainelli 	/* Resolve which bit controls the Broadcom tag */
551b409a9efSFlorian Fainelli 	switch (port) {
552b409a9efSFlorian Fainelli 	case 8:
553b409a9efSFlorian Fainelli 		val = BRCM_HDR_P8_EN;
554b409a9efSFlorian Fainelli 		break;
555b409a9efSFlorian Fainelli 	case 7:
556b409a9efSFlorian Fainelli 		val = BRCM_HDR_P7_EN;
557b409a9efSFlorian Fainelli 		break;
558b409a9efSFlorian Fainelli 	case 5:
559b409a9efSFlorian Fainelli 		val = BRCM_HDR_P5_EN;
560b409a9efSFlorian Fainelli 		break;
561b409a9efSFlorian Fainelli 	default:
562b409a9efSFlorian Fainelli 		val = 0;
563b409a9efSFlorian Fainelli 		break;
564b409a9efSFlorian Fainelli 	}
565b409a9efSFlorian Fainelli 
566b409a9efSFlorian Fainelli 	/* Enable Broadcom tags for IMP port */
567b409a9efSFlorian Fainelli 	b53_read8(dev, B53_MGMT_PAGE, B53_BRCM_HDR, &hdr_ctl);
568cdb583cfSFlorian Fainelli 	if (tag_en)
569b409a9efSFlorian Fainelli 		hdr_ctl |= val;
570cdb583cfSFlorian Fainelli 	else
571cdb583cfSFlorian Fainelli 		hdr_ctl &= ~val;
572b409a9efSFlorian Fainelli 	b53_write8(dev, B53_MGMT_PAGE, B53_BRCM_HDR, hdr_ctl);
573b409a9efSFlorian Fainelli 
574b409a9efSFlorian Fainelli 	/* Registers below are only accessible on newer devices */
575b409a9efSFlorian Fainelli 	if (!is58xx(dev))
576b409a9efSFlorian Fainelli 		return;
577b409a9efSFlorian Fainelli 
578b409a9efSFlorian Fainelli 	/* Enable reception Broadcom tag for CPU TX (switch RX) to
579b409a9efSFlorian Fainelli 	 * allow us to tag outgoing frames
580b409a9efSFlorian Fainelli 	 */
581b409a9efSFlorian Fainelli 	b53_read16(dev, B53_MGMT_PAGE, B53_BRCM_HDR_RX_DIS, &reg);
582cdb583cfSFlorian Fainelli 	if (tag_en)
583b409a9efSFlorian Fainelli 		reg &= ~BIT(port);
584cdb583cfSFlorian Fainelli 	else
585cdb583cfSFlorian Fainelli 		reg |= BIT(port);
586b409a9efSFlorian Fainelli 	b53_write16(dev, B53_MGMT_PAGE, B53_BRCM_HDR_RX_DIS, reg);
587b409a9efSFlorian Fainelli 
588b409a9efSFlorian Fainelli 	/* Enable transmission of Broadcom tags from the switch (CPU RX) to
589b409a9efSFlorian Fainelli 	 * allow delivering frames to the per-port net_devices
590b409a9efSFlorian Fainelli 	 */
591b409a9efSFlorian Fainelli 	b53_read16(dev, B53_MGMT_PAGE, B53_BRCM_HDR_TX_DIS, &reg);
592cdb583cfSFlorian Fainelli 	if (tag_en)
593b409a9efSFlorian Fainelli 		reg &= ~BIT(port);
594cdb583cfSFlorian Fainelli 	else
595cdb583cfSFlorian Fainelli 		reg |= BIT(port);
596b409a9efSFlorian Fainelli 	b53_write16(dev, B53_MGMT_PAGE, B53_BRCM_HDR_TX_DIS, reg);
597b409a9efSFlorian Fainelli }
598b409a9efSFlorian Fainelli EXPORT_SYMBOL(b53_brcm_hdr_setup);
599b409a9efSFlorian Fainelli 
600299752a7SFlorian Fainelli static void b53_enable_cpu_port(struct b53_device *dev, int port)
601967dd82fSFlorian Fainelli {
602967dd82fSFlorian Fainelli 	u8 port_ctrl;
603967dd82fSFlorian Fainelli 
604967dd82fSFlorian Fainelli 	/* BCM5325 CPU port is at 8 */
605299752a7SFlorian Fainelli 	if ((is5325(dev) || is5365(dev)) && port == B53_CPU_PORT_25)
606299752a7SFlorian Fainelli 		port = B53_CPU_PORT;
607967dd82fSFlorian Fainelli 
608967dd82fSFlorian Fainelli 	port_ctrl = PORT_CTRL_RX_BCST_EN |
609967dd82fSFlorian Fainelli 		    PORT_CTRL_RX_MCST_EN |
610967dd82fSFlorian Fainelli 		    PORT_CTRL_RX_UCST_EN;
611299752a7SFlorian Fainelli 	b53_write8(dev, B53_CTRL_PAGE, B53_PORT_CTRL(port), port_ctrl);
6127edc58d6SFlorian Fainelli 
6137edc58d6SFlorian Fainelli 	b53_brcm_hdr_setup(dev->ds, port);
614967dd82fSFlorian Fainelli }
615967dd82fSFlorian Fainelli 
616967dd82fSFlorian Fainelli static void b53_enable_mib(struct b53_device *dev)
617967dd82fSFlorian Fainelli {
618967dd82fSFlorian Fainelli 	u8 gc;
619967dd82fSFlorian Fainelli 
620967dd82fSFlorian Fainelli 	b53_read8(dev, B53_MGMT_PAGE, B53_GLOBAL_CONFIG, &gc);
621967dd82fSFlorian Fainelli 	gc &= ~(GC_RESET_MIB | GC_MIB_AC_EN);
622967dd82fSFlorian Fainelli 	b53_write8(dev, B53_MGMT_PAGE, B53_GLOBAL_CONFIG, gc);
623967dd82fSFlorian Fainelli }
624967dd82fSFlorian Fainelli 
6255c1a6eafSFlorian Fainelli int b53_configure_vlan(struct dsa_switch *ds)
626967dd82fSFlorian Fainelli {
6275c1a6eafSFlorian Fainelli 	struct b53_device *dev = ds->priv;
628a2482d2cSFlorian Fainelli 	struct b53_vlan vl = { 0 };
629967dd82fSFlorian Fainelli 	int i;
630967dd82fSFlorian Fainelli 
631967dd82fSFlorian Fainelli 	/* clear all vlan entries */
632967dd82fSFlorian Fainelli 	if (is5325(dev) || is5365(dev)) {
633967dd82fSFlorian Fainelli 		for (i = 1; i < dev->num_vlans; i++)
634a2482d2cSFlorian Fainelli 			b53_set_vlan_entry(dev, i, &vl);
635967dd82fSFlorian Fainelli 	} else {
636967dd82fSFlorian Fainelli 		b53_do_vlan_op(dev, VTA_CMD_CLEAR);
637967dd82fSFlorian Fainelli 	}
638967dd82fSFlorian Fainelli 
639967dd82fSFlorian Fainelli 	b53_enable_vlan(dev, false);
640967dd82fSFlorian Fainelli 
641967dd82fSFlorian Fainelli 	b53_for_each_port(dev, i)
642967dd82fSFlorian Fainelli 		b53_write16(dev, B53_VLAN_PAGE,
643967dd82fSFlorian Fainelli 			    B53_VLAN_PORT_DEF_TAG(i), 1);
644967dd82fSFlorian Fainelli 
645967dd82fSFlorian Fainelli 	if (!is5325(dev) && !is5365(dev))
646967dd82fSFlorian Fainelli 		b53_set_jumbo(dev, dev->enable_jumbo, false);
647967dd82fSFlorian Fainelli 
648967dd82fSFlorian Fainelli 	return 0;
649967dd82fSFlorian Fainelli }
6505c1a6eafSFlorian Fainelli EXPORT_SYMBOL(b53_configure_vlan);
651967dd82fSFlorian Fainelli 
652967dd82fSFlorian Fainelli static void b53_switch_reset_gpio(struct b53_device *dev)
653967dd82fSFlorian Fainelli {
654967dd82fSFlorian Fainelli 	int gpio = dev->reset_gpio;
655967dd82fSFlorian Fainelli 
656967dd82fSFlorian Fainelli 	if (gpio < 0)
657967dd82fSFlorian Fainelli 		return;
658967dd82fSFlorian Fainelli 
659967dd82fSFlorian Fainelli 	/* Reset sequence: RESET low(50ms)->high(20ms)
660967dd82fSFlorian Fainelli 	 */
661967dd82fSFlorian Fainelli 	gpio_set_value(gpio, 0);
662967dd82fSFlorian Fainelli 	mdelay(50);
663967dd82fSFlorian Fainelli 
664967dd82fSFlorian Fainelli 	gpio_set_value(gpio, 1);
665967dd82fSFlorian Fainelli 	mdelay(20);
666967dd82fSFlorian Fainelli 
667967dd82fSFlorian Fainelli 	dev->current_page = 0xff;
668967dd82fSFlorian Fainelli }
669967dd82fSFlorian Fainelli 
670967dd82fSFlorian Fainelli static int b53_switch_reset(struct b53_device *dev)
671967dd82fSFlorian Fainelli {
6723fb22b05SFlorian Fainelli 	unsigned int timeout = 1000;
6733fb22b05SFlorian Fainelli 	u8 mgmt, reg;
674967dd82fSFlorian Fainelli 
675967dd82fSFlorian Fainelli 	b53_switch_reset_gpio(dev);
676967dd82fSFlorian Fainelli 
677967dd82fSFlorian Fainelli 	if (is539x(dev)) {
678967dd82fSFlorian Fainelli 		b53_write8(dev, B53_CTRL_PAGE, B53_SOFTRESET, 0x83);
679967dd82fSFlorian Fainelli 		b53_write8(dev, B53_CTRL_PAGE, B53_SOFTRESET, 0x00);
680967dd82fSFlorian Fainelli 	}
681967dd82fSFlorian Fainelli 
6823fb22b05SFlorian Fainelli 	/* This is specific to 58xx devices here, do not use is58xx() which
6833fb22b05SFlorian Fainelli 	 * covers the larger Starfigther 2 family, including 7445/7278 which
6843fb22b05SFlorian Fainelli 	 * still use this driver as a library and need to perform the reset
6853fb22b05SFlorian Fainelli 	 * earlier.
6863fb22b05SFlorian Fainelli 	 */
6873fb22b05SFlorian Fainelli 	if (dev->chip_id == BCM58XX_DEVICE_ID) {
6883fb22b05SFlorian Fainelli 		b53_read8(dev, B53_CTRL_PAGE, B53_SOFTRESET, &reg);
6893fb22b05SFlorian Fainelli 		reg |= SW_RST | EN_SW_RST | EN_CH_RST;
6903fb22b05SFlorian Fainelli 		b53_write8(dev, B53_CTRL_PAGE, B53_SOFTRESET, reg);
6913fb22b05SFlorian Fainelli 
6923fb22b05SFlorian Fainelli 		do {
6933fb22b05SFlorian Fainelli 			b53_read8(dev, B53_CTRL_PAGE, B53_SOFTRESET, &reg);
6943fb22b05SFlorian Fainelli 			if (!(reg & SW_RST))
6953fb22b05SFlorian Fainelli 				break;
6963fb22b05SFlorian Fainelli 
6973fb22b05SFlorian Fainelli 			usleep_range(1000, 2000);
6983fb22b05SFlorian Fainelli 		} while (timeout-- > 0);
6993fb22b05SFlorian Fainelli 
7003fb22b05SFlorian Fainelli 		if (timeout == 0)
7013fb22b05SFlorian Fainelli 			return -ETIMEDOUT;
7023fb22b05SFlorian Fainelli 	}
7033fb22b05SFlorian Fainelli 
704967dd82fSFlorian Fainelli 	b53_read8(dev, B53_CTRL_PAGE, B53_SWITCH_MODE, &mgmt);
705967dd82fSFlorian Fainelli 
706967dd82fSFlorian Fainelli 	if (!(mgmt & SM_SW_FWD_EN)) {
707967dd82fSFlorian Fainelli 		mgmt &= ~SM_SW_FWD_MODE;
708967dd82fSFlorian Fainelli 		mgmt |= SM_SW_FWD_EN;
709967dd82fSFlorian Fainelli 
710967dd82fSFlorian Fainelli 		b53_write8(dev, B53_CTRL_PAGE, B53_SWITCH_MODE, mgmt);
711967dd82fSFlorian Fainelli 		b53_read8(dev, B53_CTRL_PAGE, B53_SWITCH_MODE, &mgmt);
712967dd82fSFlorian Fainelli 
713967dd82fSFlorian Fainelli 		if (!(mgmt & SM_SW_FWD_EN)) {
714967dd82fSFlorian Fainelli 			dev_err(dev->dev, "Failed to enable switch!\n");
715967dd82fSFlorian Fainelli 			return -EINVAL;
716967dd82fSFlorian Fainelli 		}
717967dd82fSFlorian Fainelli 	}
718967dd82fSFlorian Fainelli 
719967dd82fSFlorian Fainelli 	b53_enable_mib(dev);
720967dd82fSFlorian Fainelli 
721ff39c2d6SFlorian Fainelli 	return b53_flush_arl(dev, FAST_AGE_STATIC);
722967dd82fSFlorian Fainelli }
723967dd82fSFlorian Fainelli 
724967dd82fSFlorian Fainelli static int b53_phy_read16(struct dsa_switch *ds, int addr, int reg)
725967dd82fSFlorian Fainelli {
72604bed143SVivien Didelot 	struct b53_device *priv = ds->priv;
727967dd82fSFlorian Fainelli 	u16 value = 0;
728967dd82fSFlorian Fainelli 	int ret;
729967dd82fSFlorian Fainelli 
730967dd82fSFlorian Fainelli 	if (priv->ops->phy_read16)
731967dd82fSFlorian Fainelli 		ret = priv->ops->phy_read16(priv, addr, reg, &value);
732967dd82fSFlorian Fainelli 	else
733967dd82fSFlorian Fainelli 		ret = b53_read16(priv, B53_PORT_MII_PAGE(addr),
734967dd82fSFlorian Fainelli 				 reg * 2, &value);
735967dd82fSFlorian Fainelli 
736967dd82fSFlorian Fainelli 	return ret ? ret : value;
737967dd82fSFlorian Fainelli }
738967dd82fSFlorian Fainelli 
739967dd82fSFlorian Fainelli static int b53_phy_write16(struct dsa_switch *ds, int addr, int reg, u16 val)
740967dd82fSFlorian Fainelli {
74104bed143SVivien Didelot 	struct b53_device *priv = ds->priv;
742967dd82fSFlorian Fainelli 
743967dd82fSFlorian Fainelli 	if (priv->ops->phy_write16)
744967dd82fSFlorian Fainelli 		return priv->ops->phy_write16(priv, addr, reg, val);
745967dd82fSFlorian Fainelli 
746967dd82fSFlorian Fainelli 	return b53_write16(priv, B53_PORT_MII_PAGE(addr), reg * 2, val);
747967dd82fSFlorian Fainelli }
748967dd82fSFlorian Fainelli 
749967dd82fSFlorian Fainelli static int b53_reset_switch(struct b53_device *priv)
750967dd82fSFlorian Fainelli {
751967dd82fSFlorian Fainelli 	/* reset vlans */
752967dd82fSFlorian Fainelli 	priv->enable_jumbo = false;
753967dd82fSFlorian Fainelli 
754a2482d2cSFlorian Fainelli 	memset(priv->vlans, 0, sizeof(*priv->vlans) * priv->num_vlans);
755967dd82fSFlorian Fainelli 	memset(priv->ports, 0, sizeof(*priv->ports) * priv->num_ports);
756967dd82fSFlorian Fainelli 
757967dd82fSFlorian Fainelli 	return b53_switch_reset(priv);
758967dd82fSFlorian Fainelli }
759967dd82fSFlorian Fainelli 
760967dd82fSFlorian Fainelli static int b53_apply_config(struct b53_device *priv)
761967dd82fSFlorian Fainelli {
762967dd82fSFlorian Fainelli 	/* disable switching */
763967dd82fSFlorian Fainelli 	b53_set_forwarding(priv, 0);
764967dd82fSFlorian Fainelli 
7655c1a6eafSFlorian Fainelli 	b53_configure_vlan(priv->ds);
766967dd82fSFlorian Fainelli 
767967dd82fSFlorian Fainelli 	/* enable switching */
768967dd82fSFlorian Fainelli 	b53_set_forwarding(priv, 1);
769967dd82fSFlorian Fainelli 
770967dd82fSFlorian Fainelli 	return 0;
771967dd82fSFlorian Fainelli }
772967dd82fSFlorian Fainelli 
773967dd82fSFlorian Fainelli static void b53_reset_mib(struct b53_device *priv)
774967dd82fSFlorian Fainelli {
775967dd82fSFlorian Fainelli 	u8 gc;
776967dd82fSFlorian Fainelli 
777967dd82fSFlorian Fainelli 	b53_read8(priv, B53_MGMT_PAGE, B53_GLOBAL_CONFIG, &gc);
778967dd82fSFlorian Fainelli 
779967dd82fSFlorian Fainelli 	b53_write8(priv, B53_MGMT_PAGE, B53_GLOBAL_CONFIG, gc | GC_RESET_MIB);
780967dd82fSFlorian Fainelli 	msleep(1);
781967dd82fSFlorian Fainelli 	b53_write8(priv, B53_MGMT_PAGE, B53_GLOBAL_CONFIG, gc & ~GC_RESET_MIB);
782967dd82fSFlorian Fainelli 	msleep(1);
783967dd82fSFlorian Fainelli }
784967dd82fSFlorian Fainelli 
785967dd82fSFlorian Fainelli static const struct b53_mib_desc *b53_get_mib(struct b53_device *dev)
786967dd82fSFlorian Fainelli {
787967dd82fSFlorian Fainelli 	if (is5365(dev))
788967dd82fSFlorian Fainelli 		return b53_mibs_65;
789967dd82fSFlorian Fainelli 	else if (is63xx(dev))
790967dd82fSFlorian Fainelli 		return b53_mibs_63xx;
791bde5d132SFlorian Fainelli 	else if (is58xx(dev))
792bde5d132SFlorian Fainelli 		return b53_mibs_58xx;
793967dd82fSFlorian Fainelli 	else
794967dd82fSFlorian Fainelli 		return b53_mibs;
795967dd82fSFlorian Fainelli }
796967dd82fSFlorian Fainelli 
797967dd82fSFlorian Fainelli static unsigned int b53_get_mib_size(struct b53_device *dev)
798967dd82fSFlorian Fainelli {
799967dd82fSFlorian Fainelli 	if (is5365(dev))
800967dd82fSFlorian Fainelli 		return B53_MIBS_65_SIZE;
801967dd82fSFlorian Fainelli 	else if (is63xx(dev))
802967dd82fSFlorian Fainelli 		return B53_MIBS_63XX_SIZE;
803bde5d132SFlorian Fainelli 	else if (is58xx(dev))
804bde5d132SFlorian Fainelli 		return B53_MIBS_58XX_SIZE;
805967dd82fSFlorian Fainelli 	else
806967dd82fSFlorian Fainelli 		return B53_MIBS_SIZE;
807967dd82fSFlorian Fainelli }
808967dd82fSFlorian Fainelli 
8093117455dSFlorian Fainelli void b53_get_strings(struct dsa_switch *ds, int port, uint8_t *data)
810967dd82fSFlorian Fainelli {
81104bed143SVivien Didelot 	struct b53_device *dev = ds->priv;
812967dd82fSFlorian Fainelli 	const struct b53_mib_desc *mibs = b53_get_mib(dev);
813967dd82fSFlorian Fainelli 	unsigned int mib_size = b53_get_mib_size(dev);
814967dd82fSFlorian Fainelli 	unsigned int i;
815967dd82fSFlorian Fainelli 
816967dd82fSFlorian Fainelli 	for (i = 0; i < mib_size; i++)
817967dd82fSFlorian Fainelli 		memcpy(data + i * ETH_GSTRING_LEN,
818967dd82fSFlorian Fainelli 		       mibs[i].name, ETH_GSTRING_LEN);
819967dd82fSFlorian Fainelli }
8203117455dSFlorian Fainelli EXPORT_SYMBOL(b53_get_strings);
821967dd82fSFlorian Fainelli 
8223117455dSFlorian Fainelli void b53_get_ethtool_stats(struct dsa_switch *ds, int port, uint64_t *data)
823967dd82fSFlorian Fainelli {
82404bed143SVivien Didelot 	struct b53_device *dev = ds->priv;
825967dd82fSFlorian Fainelli 	const struct b53_mib_desc *mibs = b53_get_mib(dev);
826967dd82fSFlorian Fainelli 	unsigned int mib_size = b53_get_mib_size(dev);
827967dd82fSFlorian Fainelli 	const struct b53_mib_desc *s;
828967dd82fSFlorian Fainelli 	unsigned int i;
829967dd82fSFlorian Fainelli 	u64 val = 0;
830967dd82fSFlorian Fainelli 
831967dd82fSFlorian Fainelli 	if (is5365(dev) && port == 5)
832967dd82fSFlorian Fainelli 		port = 8;
833967dd82fSFlorian Fainelli 
834967dd82fSFlorian Fainelli 	mutex_lock(&dev->stats_mutex);
835967dd82fSFlorian Fainelli 
836967dd82fSFlorian Fainelli 	for (i = 0; i < mib_size; i++) {
837967dd82fSFlorian Fainelli 		s = &mibs[i];
838967dd82fSFlorian Fainelli 
83951dca8a1SFlorian Fainelli 		if (s->size == 8) {
840967dd82fSFlorian Fainelli 			b53_read64(dev, B53_MIB_PAGE(port), s->offset, &val);
841967dd82fSFlorian Fainelli 		} else {
842967dd82fSFlorian Fainelli 			u32 val32;
843967dd82fSFlorian Fainelli 
844967dd82fSFlorian Fainelli 			b53_read32(dev, B53_MIB_PAGE(port), s->offset,
845967dd82fSFlorian Fainelli 				   &val32);
846967dd82fSFlorian Fainelli 			val = val32;
847967dd82fSFlorian Fainelli 		}
848967dd82fSFlorian Fainelli 		data[i] = (u64)val;
849967dd82fSFlorian Fainelli 	}
850967dd82fSFlorian Fainelli 
851967dd82fSFlorian Fainelli 	mutex_unlock(&dev->stats_mutex);
852967dd82fSFlorian Fainelli }
8533117455dSFlorian Fainelli EXPORT_SYMBOL(b53_get_ethtool_stats);
854967dd82fSFlorian Fainelli 
8553117455dSFlorian Fainelli int b53_get_sset_count(struct dsa_switch *ds)
856967dd82fSFlorian Fainelli {
85704bed143SVivien Didelot 	struct b53_device *dev = ds->priv;
858967dd82fSFlorian Fainelli 
859967dd82fSFlorian Fainelli 	return b53_get_mib_size(dev);
860967dd82fSFlorian Fainelli }
8613117455dSFlorian Fainelli EXPORT_SYMBOL(b53_get_sset_count);
862967dd82fSFlorian Fainelli 
863967dd82fSFlorian Fainelli static int b53_setup(struct dsa_switch *ds)
864967dd82fSFlorian Fainelli {
86504bed143SVivien Didelot 	struct b53_device *dev = ds->priv;
866967dd82fSFlorian Fainelli 	unsigned int port;
867967dd82fSFlorian Fainelli 	int ret;
868967dd82fSFlorian Fainelli 
869967dd82fSFlorian Fainelli 	ret = b53_reset_switch(dev);
870967dd82fSFlorian Fainelli 	if (ret) {
871967dd82fSFlorian Fainelli 		dev_err(ds->dev, "failed to reset switch\n");
872967dd82fSFlorian Fainelli 		return ret;
873967dd82fSFlorian Fainelli 	}
874967dd82fSFlorian Fainelli 
875967dd82fSFlorian Fainelli 	b53_reset_mib(dev);
876967dd82fSFlorian Fainelli 
877967dd82fSFlorian Fainelli 	ret = b53_apply_config(dev);
878967dd82fSFlorian Fainelli 	if (ret)
879967dd82fSFlorian Fainelli 		dev_err(ds->dev, "failed to apply configuration\n");
880967dd82fSFlorian Fainelli 
88134c8befdSFlorian Fainelli 	/* Configure IMP/CPU port, disable unused ports. Enabled
88234c8befdSFlorian Fainelli 	 * ports will be configured with .port_enable
88334c8befdSFlorian Fainelli 	 */
884967dd82fSFlorian Fainelli 	for (port = 0; port < dev->num_ports; port++) {
88534c8befdSFlorian Fainelli 		if (dsa_is_cpu_port(ds, port))
886299752a7SFlorian Fainelli 			b53_enable_cpu_port(dev, port);
887bff7b688SVivien Didelot 		else if (dsa_is_unused_port(ds, port))
888967dd82fSFlorian Fainelli 			b53_disable_port(ds, port, NULL);
889967dd82fSFlorian Fainelli 	}
890967dd82fSFlorian Fainelli 
891967dd82fSFlorian Fainelli 	return ret;
892967dd82fSFlorian Fainelli }
893967dd82fSFlorian Fainelli 
894967dd82fSFlorian Fainelli static void b53_adjust_link(struct dsa_switch *ds, int port,
895967dd82fSFlorian Fainelli 			    struct phy_device *phydev)
896967dd82fSFlorian Fainelli {
89704bed143SVivien Didelot 	struct b53_device *dev = ds->priv;
898f43a2dbeSFlorian Fainelli 	struct ethtool_eee *p = &dev->ports[port].eee;
899967dd82fSFlorian Fainelli 	u8 rgmii_ctrl = 0, reg = 0, off;
900967dd82fSFlorian Fainelli 
901967dd82fSFlorian Fainelli 	if (!phy_is_pseudo_fixed_link(phydev))
902967dd82fSFlorian Fainelli 		return;
903967dd82fSFlorian Fainelli 
904967dd82fSFlorian Fainelli 	/* Override the port settings */
905967dd82fSFlorian Fainelli 	if (port == dev->cpu_port) {
906967dd82fSFlorian Fainelli 		off = B53_PORT_OVERRIDE_CTRL;
907967dd82fSFlorian Fainelli 		reg = PORT_OVERRIDE_EN;
908967dd82fSFlorian Fainelli 	} else {
909967dd82fSFlorian Fainelli 		off = B53_GMII_PORT_OVERRIDE_CTRL(port);
910967dd82fSFlorian Fainelli 		reg = GMII_PO_EN;
911967dd82fSFlorian Fainelli 	}
912967dd82fSFlorian Fainelli 
913967dd82fSFlorian Fainelli 	/* Set the link UP */
914967dd82fSFlorian Fainelli 	if (phydev->link)
915967dd82fSFlorian Fainelli 		reg |= PORT_OVERRIDE_LINK;
916967dd82fSFlorian Fainelli 
917967dd82fSFlorian Fainelli 	if (phydev->duplex == DUPLEX_FULL)
918967dd82fSFlorian Fainelli 		reg |= PORT_OVERRIDE_FULL_DUPLEX;
919967dd82fSFlorian Fainelli 
920967dd82fSFlorian Fainelli 	switch (phydev->speed) {
921967dd82fSFlorian Fainelli 	case 2000:
922967dd82fSFlorian Fainelli 		reg |= PORT_OVERRIDE_SPEED_2000M;
923967dd82fSFlorian Fainelli 		/* fallthrough */
924967dd82fSFlorian Fainelli 	case SPEED_1000:
925967dd82fSFlorian Fainelli 		reg |= PORT_OVERRIDE_SPEED_1000M;
926967dd82fSFlorian Fainelli 		break;
927967dd82fSFlorian Fainelli 	case SPEED_100:
928967dd82fSFlorian Fainelli 		reg |= PORT_OVERRIDE_SPEED_100M;
929967dd82fSFlorian Fainelli 		break;
930967dd82fSFlorian Fainelli 	case SPEED_10:
931967dd82fSFlorian Fainelli 		reg |= PORT_OVERRIDE_SPEED_10M;
932967dd82fSFlorian Fainelli 		break;
933967dd82fSFlorian Fainelli 	default:
934967dd82fSFlorian Fainelli 		dev_err(ds->dev, "unknown speed: %d\n", phydev->speed);
935967dd82fSFlorian Fainelli 		return;
936967dd82fSFlorian Fainelli 	}
937967dd82fSFlorian Fainelli 
938967dd82fSFlorian Fainelli 	/* Enable flow control on BCM5301x's CPU port */
939967dd82fSFlorian Fainelli 	if (is5301x(dev) && port == dev->cpu_port)
940967dd82fSFlorian Fainelli 		reg |= PORT_OVERRIDE_RX_FLOW | PORT_OVERRIDE_TX_FLOW;
941967dd82fSFlorian Fainelli 
942967dd82fSFlorian Fainelli 	if (phydev->pause) {
943967dd82fSFlorian Fainelli 		if (phydev->asym_pause)
944967dd82fSFlorian Fainelli 			reg |= PORT_OVERRIDE_TX_FLOW;
945967dd82fSFlorian Fainelli 		reg |= PORT_OVERRIDE_RX_FLOW;
946967dd82fSFlorian Fainelli 	}
947967dd82fSFlorian Fainelli 
948967dd82fSFlorian Fainelli 	b53_write8(dev, B53_CTRL_PAGE, off, reg);
949967dd82fSFlorian Fainelli 
950967dd82fSFlorian Fainelli 	if (is531x5(dev) && phy_interface_is_rgmii(phydev)) {
951967dd82fSFlorian Fainelli 		if (port == 8)
952967dd82fSFlorian Fainelli 			off = B53_RGMII_CTRL_IMP;
953967dd82fSFlorian Fainelli 		else
954967dd82fSFlorian Fainelli 			off = B53_RGMII_CTRL_P(port);
955967dd82fSFlorian Fainelli 
956967dd82fSFlorian Fainelli 		/* Configure the port RGMII clock delay by DLL disabled and
957967dd82fSFlorian Fainelli 		 * tx_clk aligned timing (restoring to reset defaults)
958967dd82fSFlorian Fainelli 		 */
959967dd82fSFlorian Fainelli 		b53_read8(dev, B53_CTRL_PAGE, off, &rgmii_ctrl);
960967dd82fSFlorian Fainelli 		rgmii_ctrl &= ~(RGMII_CTRL_DLL_RXC | RGMII_CTRL_DLL_TXC |
961967dd82fSFlorian Fainelli 				RGMII_CTRL_TIMING_SEL);
962967dd82fSFlorian Fainelli 
963967dd82fSFlorian Fainelli 		/* PHY_INTERFACE_MODE_RGMII_TXID means TX internal delay, make
964967dd82fSFlorian Fainelli 		 * sure that we enable the port TX clock internal delay to
965967dd82fSFlorian Fainelli 		 * account for this internal delay that is inserted, otherwise
966967dd82fSFlorian Fainelli 		 * the switch won't be able to receive correctly.
967967dd82fSFlorian Fainelli 		 *
968967dd82fSFlorian Fainelli 		 * PHY_INTERFACE_MODE_RGMII means that we are not introducing
969967dd82fSFlorian Fainelli 		 * any delay neither on transmission nor reception, so the
970967dd82fSFlorian Fainelli 		 * BCM53125 must also be configured accordingly to account for
971967dd82fSFlorian Fainelli 		 * the lack of delay and introduce
972967dd82fSFlorian Fainelli 		 *
973967dd82fSFlorian Fainelli 		 * The BCM53125 switch has its RX clock and TX clock control
974967dd82fSFlorian Fainelli 		 * swapped, hence the reason why we modify the TX clock path in
975967dd82fSFlorian Fainelli 		 * the "RGMII" case
976967dd82fSFlorian Fainelli 		 */
977967dd82fSFlorian Fainelli 		if (phydev->interface == PHY_INTERFACE_MODE_RGMII_TXID)
978967dd82fSFlorian Fainelli 			rgmii_ctrl |= RGMII_CTRL_DLL_TXC;
979967dd82fSFlorian Fainelli 		if (phydev->interface == PHY_INTERFACE_MODE_RGMII)
980967dd82fSFlorian Fainelli 			rgmii_ctrl |= RGMII_CTRL_DLL_TXC | RGMII_CTRL_DLL_RXC;
981967dd82fSFlorian Fainelli 		rgmii_ctrl |= RGMII_CTRL_TIMING_SEL;
982967dd82fSFlorian Fainelli 		b53_write8(dev, B53_CTRL_PAGE, off, rgmii_ctrl);
983967dd82fSFlorian Fainelli 
984967dd82fSFlorian Fainelli 		dev_info(ds->dev, "Configured port %d for %s\n", port,
985967dd82fSFlorian Fainelli 			 phy_modes(phydev->interface));
986967dd82fSFlorian Fainelli 	}
987967dd82fSFlorian Fainelli 
988967dd82fSFlorian Fainelli 	/* configure MII port if necessary */
989967dd82fSFlorian Fainelli 	if (is5325(dev)) {
990967dd82fSFlorian Fainelli 		b53_read8(dev, B53_CTRL_PAGE, B53_PORT_OVERRIDE_CTRL,
991967dd82fSFlorian Fainelli 			  &reg);
992967dd82fSFlorian Fainelli 
993967dd82fSFlorian Fainelli 		/* reverse mii needs to be enabled */
994967dd82fSFlorian Fainelli 		if (!(reg & PORT_OVERRIDE_RV_MII_25)) {
995967dd82fSFlorian Fainelli 			b53_write8(dev, B53_CTRL_PAGE, B53_PORT_OVERRIDE_CTRL,
996967dd82fSFlorian Fainelli 				   reg | PORT_OVERRIDE_RV_MII_25);
997967dd82fSFlorian Fainelli 			b53_read8(dev, B53_CTRL_PAGE, B53_PORT_OVERRIDE_CTRL,
998967dd82fSFlorian Fainelli 				  &reg);
999967dd82fSFlorian Fainelli 
1000967dd82fSFlorian Fainelli 			if (!(reg & PORT_OVERRIDE_RV_MII_25)) {
1001967dd82fSFlorian Fainelli 				dev_err(ds->dev,
1002967dd82fSFlorian Fainelli 					"Failed to enable reverse MII mode\n");
1003967dd82fSFlorian Fainelli 				return;
1004967dd82fSFlorian Fainelli 			}
1005967dd82fSFlorian Fainelli 		}
1006967dd82fSFlorian Fainelli 	} else if (is5301x(dev)) {
1007967dd82fSFlorian Fainelli 		if (port != dev->cpu_port) {
1008967dd82fSFlorian Fainelli 			u8 po_reg = B53_GMII_PORT_OVERRIDE_CTRL(dev->cpu_port);
1009967dd82fSFlorian Fainelli 			u8 gmii_po;
1010967dd82fSFlorian Fainelli 
1011967dd82fSFlorian Fainelli 			b53_read8(dev, B53_CTRL_PAGE, po_reg, &gmii_po);
1012967dd82fSFlorian Fainelli 			gmii_po |= GMII_PO_LINK |
1013967dd82fSFlorian Fainelli 				   GMII_PO_RX_FLOW |
1014967dd82fSFlorian Fainelli 				   GMII_PO_TX_FLOW |
1015967dd82fSFlorian Fainelli 				   GMII_PO_EN |
1016967dd82fSFlorian Fainelli 				   GMII_PO_SPEED_2000M;
1017967dd82fSFlorian Fainelli 			b53_write8(dev, B53_CTRL_PAGE, po_reg, gmii_po);
1018967dd82fSFlorian Fainelli 		}
1019967dd82fSFlorian Fainelli 	}
1020f43a2dbeSFlorian Fainelli 
1021f43a2dbeSFlorian Fainelli 	/* Re-negotiate EEE if it was enabled already */
1022f43a2dbeSFlorian Fainelli 	p->eee_enabled = b53_eee_init(ds, port, phydev);
1023967dd82fSFlorian Fainelli }
1024967dd82fSFlorian Fainelli 
10253117455dSFlorian Fainelli int b53_vlan_filtering(struct dsa_switch *ds, int port, bool vlan_filtering)
1026a2482d2cSFlorian Fainelli {
1027a2482d2cSFlorian Fainelli 	return 0;
1028a2482d2cSFlorian Fainelli }
10293117455dSFlorian Fainelli EXPORT_SYMBOL(b53_vlan_filtering);
1030a2482d2cSFlorian Fainelli 
10313117455dSFlorian Fainelli int b53_vlan_prepare(struct dsa_switch *ds, int port,
1032a2482d2cSFlorian Fainelli 		     const struct switchdev_obj_port_vlan *vlan,
1033a2482d2cSFlorian Fainelli 		     struct switchdev_trans *trans)
1034a2482d2cSFlorian Fainelli {
103504bed143SVivien Didelot 	struct b53_device *dev = ds->priv;
1036a2482d2cSFlorian Fainelli 
1037a2482d2cSFlorian Fainelli 	if ((is5325(dev) || is5365(dev)) && vlan->vid_begin == 0)
1038a2482d2cSFlorian Fainelli 		return -EOPNOTSUPP;
1039a2482d2cSFlorian Fainelli 
1040a2482d2cSFlorian Fainelli 	if (vlan->vid_end > dev->num_vlans)
1041a2482d2cSFlorian Fainelli 		return -ERANGE;
1042a2482d2cSFlorian Fainelli 
1043a2482d2cSFlorian Fainelli 	b53_enable_vlan(dev, true);
1044a2482d2cSFlorian Fainelli 
1045a2482d2cSFlorian Fainelli 	return 0;
1046a2482d2cSFlorian Fainelli }
10473117455dSFlorian Fainelli EXPORT_SYMBOL(b53_vlan_prepare);
1048a2482d2cSFlorian Fainelli 
10493117455dSFlorian Fainelli void b53_vlan_add(struct dsa_switch *ds, int port,
1050a2482d2cSFlorian Fainelli 		  const struct switchdev_obj_port_vlan *vlan,
1051a2482d2cSFlorian Fainelli 		  struct switchdev_trans *trans)
1052a2482d2cSFlorian Fainelli {
105304bed143SVivien Didelot 	struct b53_device *dev = ds->priv;
1054a2482d2cSFlorian Fainelli 	bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
1055a2482d2cSFlorian Fainelli 	bool pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
1056a2482d2cSFlorian Fainelli 	struct b53_vlan *vl;
1057a2482d2cSFlorian Fainelli 	u16 vid;
1058a2482d2cSFlorian Fainelli 
1059a2482d2cSFlorian Fainelli 	for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid) {
1060a2482d2cSFlorian Fainelli 		vl = &dev->vlans[vid];
1061a2482d2cSFlorian Fainelli 
1062a2482d2cSFlorian Fainelli 		b53_get_vlan_entry(dev, vid, vl);
1063a2482d2cSFlorian Fainelli 
1064c499696eSFlorian Fainelli 		vl->members |= BIT(port);
1065a2482d2cSFlorian Fainelli 		if (untagged)
1066e47112d9SFlorian Fainelli 			vl->untag |= BIT(port);
1067a2482d2cSFlorian Fainelli 		else
1068e47112d9SFlorian Fainelli 			vl->untag &= ~BIT(port);
1069a2482d2cSFlorian Fainelli 
1070a2482d2cSFlorian Fainelli 		b53_set_vlan_entry(dev, vid, vl);
1071a2482d2cSFlorian Fainelli 		b53_fast_age_vlan(dev, vid);
1072a2482d2cSFlorian Fainelli 	}
1073a2482d2cSFlorian Fainelli 
1074a2482d2cSFlorian Fainelli 	if (pvid) {
1075a2482d2cSFlorian Fainelli 		b53_write16(dev, B53_VLAN_PAGE, B53_VLAN_PORT_DEF_TAG(port),
1076a2482d2cSFlorian Fainelli 			    vlan->vid_end);
1077a2482d2cSFlorian Fainelli 		b53_fast_age_vlan(dev, vid);
1078a2482d2cSFlorian Fainelli 	}
1079a2482d2cSFlorian Fainelli }
10803117455dSFlorian Fainelli EXPORT_SYMBOL(b53_vlan_add);
1081a2482d2cSFlorian Fainelli 
10823117455dSFlorian Fainelli int b53_vlan_del(struct dsa_switch *ds, int port,
1083a2482d2cSFlorian Fainelli 		 const struct switchdev_obj_port_vlan *vlan)
1084a2482d2cSFlorian Fainelli {
108504bed143SVivien Didelot 	struct b53_device *dev = ds->priv;
1086a2482d2cSFlorian Fainelli 	bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
1087a2482d2cSFlorian Fainelli 	struct b53_vlan *vl;
1088a2482d2cSFlorian Fainelli 	u16 vid;
1089a2482d2cSFlorian Fainelli 	u16 pvid;
1090a2482d2cSFlorian Fainelli 
1091a2482d2cSFlorian Fainelli 	b53_read16(dev, B53_VLAN_PAGE, B53_VLAN_PORT_DEF_TAG(port), &pvid);
1092a2482d2cSFlorian Fainelli 
1093a2482d2cSFlorian Fainelli 	for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid) {
1094a2482d2cSFlorian Fainelli 		vl = &dev->vlans[vid];
1095a2482d2cSFlorian Fainelli 
1096a2482d2cSFlorian Fainelli 		b53_get_vlan_entry(dev, vid, vl);
1097a2482d2cSFlorian Fainelli 
1098a2482d2cSFlorian Fainelli 		vl->members &= ~BIT(port);
1099a2482d2cSFlorian Fainelli 
1100a2482d2cSFlorian Fainelli 		if (pvid == vid) {
1101a2482d2cSFlorian Fainelli 			if (is5325(dev) || is5365(dev))
1102a2482d2cSFlorian Fainelli 				pvid = 1;
1103a2482d2cSFlorian Fainelli 			else
1104a2482d2cSFlorian Fainelli 				pvid = 0;
1105a2482d2cSFlorian Fainelli 		}
1106a2482d2cSFlorian Fainelli 
1107e47112d9SFlorian Fainelli 		if (untagged)
1108a2482d2cSFlorian Fainelli 			vl->untag &= ~(BIT(port));
1109a2482d2cSFlorian Fainelli 
1110a2482d2cSFlorian Fainelli 		b53_set_vlan_entry(dev, vid, vl);
1111a2482d2cSFlorian Fainelli 		b53_fast_age_vlan(dev, vid);
1112a2482d2cSFlorian Fainelli 	}
1113a2482d2cSFlorian Fainelli 
1114a2482d2cSFlorian Fainelli 	b53_write16(dev, B53_VLAN_PAGE, B53_VLAN_PORT_DEF_TAG(port), pvid);
1115a2482d2cSFlorian Fainelli 	b53_fast_age_vlan(dev, pvid);
1116a2482d2cSFlorian Fainelli 
1117a2482d2cSFlorian Fainelli 	return 0;
1118a2482d2cSFlorian Fainelli }
11193117455dSFlorian Fainelli EXPORT_SYMBOL(b53_vlan_del);
1120a2482d2cSFlorian Fainelli 
11211da6df85SFlorian Fainelli /* Address Resolution Logic routines */
11221da6df85SFlorian Fainelli static int b53_arl_op_wait(struct b53_device *dev)
11231da6df85SFlorian Fainelli {
11241da6df85SFlorian Fainelli 	unsigned int timeout = 10;
11251da6df85SFlorian Fainelli 	u8 reg;
11261da6df85SFlorian Fainelli 
11271da6df85SFlorian Fainelli 	do {
11281da6df85SFlorian Fainelli 		b53_read8(dev, B53_ARLIO_PAGE, B53_ARLTBL_RW_CTRL, &reg);
11291da6df85SFlorian Fainelli 		if (!(reg & ARLTBL_START_DONE))
11301da6df85SFlorian Fainelli 			return 0;
11311da6df85SFlorian Fainelli 
11321da6df85SFlorian Fainelli 		usleep_range(1000, 2000);
11331da6df85SFlorian Fainelli 	} while (timeout--);
11341da6df85SFlorian Fainelli 
11351da6df85SFlorian Fainelli 	dev_warn(dev->dev, "timeout waiting for ARL to finish: 0x%02x\n", reg);
11361da6df85SFlorian Fainelli 
11371da6df85SFlorian Fainelli 	return -ETIMEDOUT;
11381da6df85SFlorian Fainelli }
11391da6df85SFlorian Fainelli 
11401da6df85SFlorian Fainelli static int b53_arl_rw_op(struct b53_device *dev, unsigned int op)
11411da6df85SFlorian Fainelli {
11421da6df85SFlorian Fainelli 	u8 reg;
11431da6df85SFlorian Fainelli 
11441da6df85SFlorian Fainelli 	if (op > ARLTBL_RW)
11451da6df85SFlorian Fainelli 		return -EINVAL;
11461da6df85SFlorian Fainelli 
11471da6df85SFlorian Fainelli 	b53_read8(dev, B53_ARLIO_PAGE, B53_ARLTBL_RW_CTRL, &reg);
11481da6df85SFlorian Fainelli 	reg |= ARLTBL_START_DONE;
11491da6df85SFlorian Fainelli 	if (op)
11501da6df85SFlorian Fainelli 		reg |= ARLTBL_RW;
11511da6df85SFlorian Fainelli 	else
11521da6df85SFlorian Fainelli 		reg &= ~ARLTBL_RW;
11531da6df85SFlorian Fainelli 	b53_write8(dev, B53_ARLIO_PAGE, B53_ARLTBL_RW_CTRL, reg);
11541da6df85SFlorian Fainelli 
11551da6df85SFlorian Fainelli 	return b53_arl_op_wait(dev);
11561da6df85SFlorian Fainelli }
11571da6df85SFlorian Fainelli 
11581da6df85SFlorian Fainelli static int b53_arl_read(struct b53_device *dev, u64 mac,
11591da6df85SFlorian Fainelli 			u16 vid, struct b53_arl_entry *ent, u8 *idx,
11601da6df85SFlorian Fainelli 			bool is_valid)
11611da6df85SFlorian Fainelli {
11621da6df85SFlorian Fainelli 	unsigned int i;
11631da6df85SFlorian Fainelli 	int ret;
11641da6df85SFlorian Fainelli 
11651da6df85SFlorian Fainelli 	ret = b53_arl_op_wait(dev);
11661da6df85SFlorian Fainelli 	if (ret)
11671da6df85SFlorian Fainelli 		return ret;
11681da6df85SFlorian Fainelli 
11691da6df85SFlorian Fainelli 	/* Read the bins */
11701da6df85SFlorian Fainelli 	for (i = 0; i < dev->num_arl_entries; i++) {
11711da6df85SFlorian Fainelli 		u64 mac_vid;
11721da6df85SFlorian Fainelli 		u32 fwd_entry;
11731da6df85SFlorian Fainelli 
11741da6df85SFlorian Fainelli 		b53_read64(dev, B53_ARLIO_PAGE,
11751da6df85SFlorian Fainelli 			   B53_ARLTBL_MAC_VID_ENTRY(i), &mac_vid);
11761da6df85SFlorian Fainelli 		b53_read32(dev, B53_ARLIO_PAGE,
11771da6df85SFlorian Fainelli 			   B53_ARLTBL_DATA_ENTRY(i), &fwd_entry);
11781da6df85SFlorian Fainelli 		b53_arl_to_entry(ent, mac_vid, fwd_entry);
11791da6df85SFlorian Fainelli 
11801da6df85SFlorian Fainelli 		if (!(fwd_entry & ARLTBL_VALID))
11811da6df85SFlorian Fainelli 			continue;
11821da6df85SFlorian Fainelli 		if ((mac_vid & ARLTBL_MAC_MASK) != mac)
11831da6df85SFlorian Fainelli 			continue;
11841da6df85SFlorian Fainelli 		*idx = i;
11851da6df85SFlorian Fainelli 	}
11861da6df85SFlorian Fainelli 
11871da6df85SFlorian Fainelli 	return -ENOENT;
11881da6df85SFlorian Fainelli }
11891da6df85SFlorian Fainelli 
11901da6df85SFlorian Fainelli static int b53_arl_op(struct b53_device *dev, int op, int port,
11911da6df85SFlorian Fainelli 		      const unsigned char *addr, u16 vid, bool is_valid)
11921da6df85SFlorian Fainelli {
11931da6df85SFlorian Fainelli 	struct b53_arl_entry ent;
11941da6df85SFlorian Fainelli 	u32 fwd_entry;
11951da6df85SFlorian Fainelli 	u64 mac, mac_vid = 0;
11961da6df85SFlorian Fainelli 	u8 idx = 0;
11971da6df85SFlorian Fainelli 	int ret;
11981da6df85SFlorian Fainelli 
11991da6df85SFlorian Fainelli 	/* Convert the array into a 64-bit MAC */
12004b92ea81SFlorian Fainelli 	mac = ether_addr_to_u64(addr);
12011da6df85SFlorian Fainelli 
12021da6df85SFlorian Fainelli 	/* Perform a read for the given MAC and VID */
12031da6df85SFlorian Fainelli 	b53_write48(dev, B53_ARLIO_PAGE, B53_MAC_ADDR_IDX, mac);
12041da6df85SFlorian Fainelli 	b53_write16(dev, B53_ARLIO_PAGE, B53_VLAN_ID_IDX, vid);
12051da6df85SFlorian Fainelli 
12061da6df85SFlorian Fainelli 	/* Issue a read operation for this MAC */
12071da6df85SFlorian Fainelli 	ret = b53_arl_rw_op(dev, 1);
12081da6df85SFlorian Fainelli 	if (ret)
12091da6df85SFlorian Fainelli 		return ret;
12101da6df85SFlorian Fainelli 
12111da6df85SFlorian Fainelli 	ret = b53_arl_read(dev, mac, vid, &ent, &idx, is_valid);
12121da6df85SFlorian Fainelli 	/* If this is a read, just finish now */
12131da6df85SFlorian Fainelli 	if (op)
12141da6df85SFlorian Fainelli 		return ret;
12151da6df85SFlorian Fainelli 
12161da6df85SFlorian Fainelli 	/* We could not find a matching MAC, so reset to a new entry */
12171da6df85SFlorian Fainelli 	if (ret) {
12181da6df85SFlorian Fainelli 		fwd_entry = 0;
12191da6df85SFlorian Fainelli 		idx = 1;
12201da6df85SFlorian Fainelli 	}
12211da6df85SFlorian Fainelli 
12221da6df85SFlorian Fainelli 	memset(&ent, 0, sizeof(ent));
12231da6df85SFlorian Fainelli 	ent.port = port;
12241da6df85SFlorian Fainelli 	ent.is_valid = is_valid;
12251da6df85SFlorian Fainelli 	ent.vid = vid;
12261da6df85SFlorian Fainelli 	ent.is_static = true;
12271da6df85SFlorian Fainelli 	memcpy(ent.mac, addr, ETH_ALEN);
12281da6df85SFlorian Fainelli 	b53_arl_from_entry(&mac_vid, &fwd_entry, &ent);
12291da6df85SFlorian Fainelli 
12301da6df85SFlorian Fainelli 	b53_write64(dev, B53_ARLIO_PAGE,
12311da6df85SFlorian Fainelli 		    B53_ARLTBL_MAC_VID_ENTRY(idx), mac_vid);
12321da6df85SFlorian Fainelli 	b53_write32(dev, B53_ARLIO_PAGE,
12331da6df85SFlorian Fainelli 		    B53_ARLTBL_DATA_ENTRY(idx), fwd_entry);
12341da6df85SFlorian Fainelli 
12351da6df85SFlorian Fainelli 	return b53_arl_rw_op(dev, 0);
12361da6df85SFlorian Fainelli }
12371da6df85SFlorian Fainelli 
12381b6dd556SArkadi Sharshevsky int b53_fdb_add(struct dsa_switch *ds, int port,
12396c2c1dcbSArkadi Sharshevsky 		const unsigned char *addr, u16 vid)
12401da6df85SFlorian Fainelli {
124104bed143SVivien Didelot 	struct b53_device *priv = ds->priv;
12421da6df85SFlorian Fainelli 
12431da6df85SFlorian Fainelli 	/* 5325 and 5365 require some more massaging, but could
12441da6df85SFlorian Fainelli 	 * be supported eventually
12451da6df85SFlorian Fainelli 	 */
12461da6df85SFlorian Fainelli 	if (is5325(priv) || is5365(priv))
12471da6df85SFlorian Fainelli 		return -EOPNOTSUPP;
12481da6df85SFlorian Fainelli 
12491b6dd556SArkadi Sharshevsky 	return b53_arl_op(priv, 0, port, addr, vid, true);
12501da6df85SFlorian Fainelli }
12513117455dSFlorian Fainelli EXPORT_SYMBOL(b53_fdb_add);
12521da6df85SFlorian Fainelli 
12533117455dSFlorian Fainelli int b53_fdb_del(struct dsa_switch *ds, int port,
12546c2c1dcbSArkadi Sharshevsky 		const unsigned char *addr, u16 vid)
12551da6df85SFlorian Fainelli {
125604bed143SVivien Didelot 	struct b53_device *priv = ds->priv;
12571da6df85SFlorian Fainelli 
12586c2c1dcbSArkadi Sharshevsky 	return b53_arl_op(priv, 0, port, addr, vid, false);
12591da6df85SFlorian Fainelli }
12603117455dSFlorian Fainelli EXPORT_SYMBOL(b53_fdb_del);
12611da6df85SFlorian Fainelli 
12621da6df85SFlorian Fainelli static int b53_arl_search_wait(struct b53_device *dev)
12631da6df85SFlorian Fainelli {
12641da6df85SFlorian Fainelli 	unsigned int timeout = 1000;
12651da6df85SFlorian Fainelli 	u8 reg;
12661da6df85SFlorian Fainelli 
12671da6df85SFlorian Fainelli 	do {
12681da6df85SFlorian Fainelli 		b53_read8(dev, B53_ARLIO_PAGE, B53_ARL_SRCH_CTL, &reg);
12691da6df85SFlorian Fainelli 		if (!(reg & ARL_SRCH_STDN))
12701da6df85SFlorian Fainelli 			return 0;
12711da6df85SFlorian Fainelli 
12721da6df85SFlorian Fainelli 		if (reg & ARL_SRCH_VLID)
12731da6df85SFlorian Fainelli 			return 0;
12741da6df85SFlorian Fainelli 
12751da6df85SFlorian Fainelli 		usleep_range(1000, 2000);
12761da6df85SFlorian Fainelli 	} while (timeout--);
12771da6df85SFlorian Fainelli 
12781da6df85SFlorian Fainelli 	return -ETIMEDOUT;
12791da6df85SFlorian Fainelli }
12801da6df85SFlorian Fainelli 
12811da6df85SFlorian Fainelli static void b53_arl_search_rd(struct b53_device *dev, u8 idx,
12821da6df85SFlorian Fainelli 			      struct b53_arl_entry *ent)
12831da6df85SFlorian Fainelli {
12841da6df85SFlorian Fainelli 	u64 mac_vid;
12851da6df85SFlorian Fainelli 	u32 fwd_entry;
12861da6df85SFlorian Fainelli 
12871da6df85SFlorian Fainelli 	b53_read64(dev, B53_ARLIO_PAGE,
12881da6df85SFlorian Fainelli 		   B53_ARL_SRCH_RSTL_MACVID(idx), &mac_vid);
12891da6df85SFlorian Fainelli 	b53_read32(dev, B53_ARLIO_PAGE,
12901da6df85SFlorian Fainelli 		   B53_ARL_SRCH_RSTL(idx), &fwd_entry);
12911da6df85SFlorian Fainelli 	b53_arl_to_entry(ent, mac_vid, fwd_entry);
12921da6df85SFlorian Fainelli }
12931da6df85SFlorian Fainelli 
1294e6cbef0cSVivien Didelot static int b53_fdb_copy(int port, const struct b53_arl_entry *ent,
12952bedde1aSArkadi Sharshevsky 			dsa_fdb_dump_cb_t *cb, void *data)
12961da6df85SFlorian Fainelli {
12971da6df85SFlorian Fainelli 	if (!ent->is_valid)
12981da6df85SFlorian Fainelli 		return 0;
12991da6df85SFlorian Fainelli 
13001da6df85SFlorian Fainelli 	if (port != ent->port)
13011da6df85SFlorian Fainelli 		return 0;
13021da6df85SFlorian Fainelli 
13032bedde1aSArkadi Sharshevsky 	return cb(ent->mac, ent->vid, ent->is_static, data);
13041da6df85SFlorian Fainelli }
13051da6df85SFlorian Fainelli 
13063117455dSFlorian Fainelli int b53_fdb_dump(struct dsa_switch *ds, int port,
13072bedde1aSArkadi Sharshevsky 		 dsa_fdb_dump_cb_t *cb, void *data)
13081da6df85SFlorian Fainelli {
130904bed143SVivien Didelot 	struct b53_device *priv = ds->priv;
13101da6df85SFlorian Fainelli 	struct b53_arl_entry results[2];
13111da6df85SFlorian Fainelli 	unsigned int count = 0;
13121da6df85SFlorian Fainelli 	int ret;
13131da6df85SFlorian Fainelli 	u8 reg;
13141da6df85SFlorian Fainelli 
13151da6df85SFlorian Fainelli 	/* Start search operation */
13161da6df85SFlorian Fainelli 	reg = ARL_SRCH_STDN;
13171da6df85SFlorian Fainelli 	b53_write8(priv, B53_ARLIO_PAGE, B53_ARL_SRCH_CTL, reg);
13181da6df85SFlorian Fainelli 
13191da6df85SFlorian Fainelli 	do {
13201da6df85SFlorian Fainelli 		ret = b53_arl_search_wait(priv);
13211da6df85SFlorian Fainelli 		if (ret)
13221da6df85SFlorian Fainelli 			return ret;
13231da6df85SFlorian Fainelli 
13241da6df85SFlorian Fainelli 		b53_arl_search_rd(priv, 0, &results[0]);
13252bedde1aSArkadi Sharshevsky 		ret = b53_fdb_copy(port, &results[0], cb, data);
13261da6df85SFlorian Fainelli 		if (ret)
13271da6df85SFlorian Fainelli 			return ret;
13281da6df85SFlorian Fainelli 
13291da6df85SFlorian Fainelli 		if (priv->num_arl_entries > 2) {
13301da6df85SFlorian Fainelli 			b53_arl_search_rd(priv, 1, &results[1]);
13312bedde1aSArkadi Sharshevsky 			ret = b53_fdb_copy(port, &results[1], cb, data);
13321da6df85SFlorian Fainelli 			if (ret)
13331da6df85SFlorian Fainelli 				return ret;
13341da6df85SFlorian Fainelli 
13351da6df85SFlorian Fainelli 			if (!results[0].is_valid && !results[1].is_valid)
13361da6df85SFlorian Fainelli 				break;
13371da6df85SFlorian Fainelli 		}
13381da6df85SFlorian Fainelli 
13391da6df85SFlorian Fainelli 	} while (count++ < 1024);
13401da6df85SFlorian Fainelli 
13411da6df85SFlorian Fainelli 	return 0;
13421da6df85SFlorian Fainelli }
13433117455dSFlorian Fainelli EXPORT_SYMBOL(b53_fdb_dump);
13441da6df85SFlorian Fainelli 
1345ddd3a0c8SVivien Didelot int b53_br_join(struct dsa_switch *ds, int port, struct net_device *br)
1346ff39c2d6SFlorian Fainelli {
134704bed143SVivien Didelot 	struct b53_device *dev = ds->priv;
13480abfd494SVivien Didelot 	s8 cpu_port = ds->ports[port].cpu_dp->index;
1349ff39c2d6SFlorian Fainelli 	u16 pvlan, reg;
1350ff39c2d6SFlorian Fainelli 	unsigned int i;
1351ff39c2d6SFlorian Fainelli 
135248aea33aSFlorian Fainelli 	/* Make this port leave the all VLANs join since we will have proper
135348aea33aSFlorian Fainelli 	 * VLAN entries from now on
135448aea33aSFlorian Fainelli 	 */
135548aea33aSFlorian Fainelli 	if (is58xx(dev)) {
135648aea33aSFlorian Fainelli 		b53_read16(dev, B53_VLAN_PAGE, B53_JOIN_ALL_VLAN_EN, &reg);
135748aea33aSFlorian Fainelli 		reg &= ~BIT(port);
135848aea33aSFlorian Fainelli 		if ((reg & BIT(cpu_port)) == BIT(cpu_port))
135948aea33aSFlorian Fainelli 			reg &= ~BIT(cpu_port);
136048aea33aSFlorian Fainelli 		b53_write16(dev, B53_VLAN_PAGE, B53_JOIN_ALL_VLAN_EN, reg);
136148aea33aSFlorian Fainelli 	}
136248aea33aSFlorian Fainelli 
1363ff39c2d6SFlorian Fainelli 	b53_read16(dev, B53_PVLAN_PAGE, B53_PVLAN_PORT_MASK(port), &pvlan);
1364ff39c2d6SFlorian Fainelli 
1365ff39c2d6SFlorian Fainelli 	b53_for_each_port(dev, i) {
1366c8652c83SVivien Didelot 		if (dsa_to_port(ds, i)->bridge_dev != br)
1367ff39c2d6SFlorian Fainelli 			continue;
1368ff39c2d6SFlorian Fainelli 
1369ff39c2d6SFlorian Fainelli 		/* Add this local port to the remote port VLAN control
1370ff39c2d6SFlorian Fainelli 		 * membership and update the remote port bitmask
1371ff39c2d6SFlorian Fainelli 		 */
1372ff39c2d6SFlorian Fainelli 		b53_read16(dev, B53_PVLAN_PAGE, B53_PVLAN_PORT_MASK(i), &reg);
1373ff39c2d6SFlorian Fainelli 		reg |= BIT(port);
1374ff39c2d6SFlorian Fainelli 		b53_write16(dev, B53_PVLAN_PAGE, B53_PVLAN_PORT_MASK(i), reg);
1375ff39c2d6SFlorian Fainelli 		dev->ports[i].vlan_ctl_mask = reg;
1376ff39c2d6SFlorian Fainelli 
1377ff39c2d6SFlorian Fainelli 		pvlan |= BIT(i);
1378ff39c2d6SFlorian Fainelli 	}
1379ff39c2d6SFlorian Fainelli 
1380ff39c2d6SFlorian Fainelli 	/* Configure the local port VLAN control membership to include
1381ff39c2d6SFlorian Fainelli 	 * remote ports and update the local port bitmask
1382ff39c2d6SFlorian Fainelli 	 */
1383ff39c2d6SFlorian Fainelli 	b53_write16(dev, B53_PVLAN_PAGE, B53_PVLAN_PORT_MASK(port), pvlan);
1384ff39c2d6SFlorian Fainelli 	dev->ports[port].vlan_ctl_mask = pvlan;
1385ff39c2d6SFlorian Fainelli 
1386ff39c2d6SFlorian Fainelli 	return 0;
1387ff39c2d6SFlorian Fainelli }
13883117455dSFlorian Fainelli EXPORT_SYMBOL(b53_br_join);
1389ff39c2d6SFlorian Fainelli 
1390f123f2fbSVivien Didelot void b53_br_leave(struct dsa_switch *ds, int port, struct net_device *br)
1391ff39c2d6SFlorian Fainelli {
139204bed143SVivien Didelot 	struct b53_device *dev = ds->priv;
1393a2482d2cSFlorian Fainelli 	struct b53_vlan *vl = &dev->vlans[0];
13940abfd494SVivien Didelot 	s8 cpu_port = ds->ports[port].cpu_dp->index;
1395ff39c2d6SFlorian Fainelli 	unsigned int i;
1396a2482d2cSFlorian Fainelli 	u16 pvlan, reg, pvid;
1397ff39c2d6SFlorian Fainelli 
1398ff39c2d6SFlorian Fainelli 	b53_read16(dev, B53_PVLAN_PAGE, B53_PVLAN_PORT_MASK(port), &pvlan);
1399ff39c2d6SFlorian Fainelli 
1400ff39c2d6SFlorian Fainelli 	b53_for_each_port(dev, i) {
1401ff39c2d6SFlorian Fainelli 		/* Don't touch the remaining ports */
1402c8652c83SVivien Didelot 		if (dsa_to_port(ds, i)->bridge_dev != br)
1403ff39c2d6SFlorian Fainelli 			continue;
1404ff39c2d6SFlorian Fainelli 
1405ff39c2d6SFlorian Fainelli 		b53_read16(dev, B53_PVLAN_PAGE, B53_PVLAN_PORT_MASK(i), &reg);
1406ff39c2d6SFlorian Fainelli 		reg &= ~BIT(port);
1407ff39c2d6SFlorian Fainelli 		b53_write16(dev, B53_PVLAN_PAGE, B53_PVLAN_PORT_MASK(i), reg);
1408ff39c2d6SFlorian Fainelli 		dev->ports[port].vlan_ctl_mask = reg;
1409ff39c2d6SFlorian Fainelli 
1410ff39c2d6SFlorian Fainelli 		/* Prevent self removal to preserve isolation */
1411ff39c2d6SFlorian Fainelli 		if (port != i)
1412ff39c2d6SFlorian Fainelli 			pvlan &= ~BIT(i);
1413ff39c2d6SFlorian Fainelli 	}
1414ff39c2d6SFlorian Fainelli 
1415ff39c2d6SFlorian Fainelli 	b53_write16(dev, B53_PVLAN_PAGE, B53_PVLAN_PORT_MASK(port), pvlan);
1416ff39c2d6SFlorian Fainelli 	dev->ports[port].vlan_ctl_mask = pvlan;
1417a2482d2cSFlorian Fainelli 
1418a2482d2cSFlorian Fainelli 	if (is5325(dev) || is5365(dev))
1419a2482d2cSFlorian Fainelli 		pvid = 1;
1420a2482d2cSFlorian Fainelli 	else
1421a2482d2cSFlorian Fainelli 		pvid = 0;
1422a2482d2cSFlorian Fainelli 
142348aea33aSFlorian Fainelli 	/* Make this port join all VLANs without VLAN entries */
142448aea33aSFlorian Fainelli 	if (is58xx(dev)) {
142548aea33aSFlorian Fainelli 		b53_read16(dev, B53_VLAN_PAGE, B53_JOIN_ALL_VLAN_EN, &reg);
142648aea33aSFlorian Fainelli 		reg |= BIT(port);
142748aea33aSFlorian Fainelli 		if (!(reg & BIT(cpu_port)))
142848aea33aSFlorian Fainelli 			reg |= BIT(cpu_port);
142948aea33aSFlorian Fainelli 		b53_write16(dev, B53_VLAN_PAGE, B53_JOIN_ALL_VLAN_EN, reg);
143048aea33aSFlorian Fainelli 	} else {
1431a2482d2cSFlorian Fainelli 		b53_get_vlan_entry(dev, pvid, vl);
1432c499696eSFlorian Fainelli 		vl->members |= BIT(port) | BIT(cpu_port);
1433c499696eSFlorian Fainelli 		vl->untag |= BIT(port) | BIT(cpu_port);
1434a2482d2cSFlorian Fainelli 		b53_set_vlan_entry(dev, pvid, vl);
1435ff39c2d6SFlorian Fainelli 	}
143648aea33aSFlorian Fainelli }
14373117455dSFlorian Fainelli EXPORT_SYMBOL(b53_br_leave);
1438ff39c2d6SFlorian Fainelli 
14393117455dSFlorian Fainelli void b53_br_set_stp_state(struct dsa_switch *ds, int port, u8 state)
1440ff39c2d6SFlorian Fainelli {
144104bed143SVivien Didelot 	struct b53_device *dev = ds->priv;
1442597698f1SVivien Didelot 	u8 hw_state;
1443ff39c2d6SFlorian Fainelli 	u8 reg;
1444ff39c2d6SFlorian Fainelli 
1445ff39c2d6SFlorian Fainelli 	switch (state) {
1446ff39c2d6SFlorian Fainelli 	case BR_STATE_DISABLED:
1447ff39c2d6SFlorian Fainelli 		hw_state = PORT_CTRL_DIS_STATE;
1448ff39c2d6SFlorian Fainelli 		break;
1449ff39c2d6SFlorian Fainelli 	case BR_STATE_LISTENING:
1450ff39c2d6SFlorian Fainelli 		hw_state = PORT_CTRL_LISTEN_STATE;
1451ff39c2d6SFlorian Fainelli 		break;
1452ff39c2d6SFlorian Fainelli 	case BR_STATE_LEARNING:
1453ff39c2d6SFlorian Fainelli 		hw_state = PORT_CTRL_LEARN_STATE;
1454ff39c2d6SFlorian Fainelli 		break;
1455ff39c2d6SFlorian Fainelli 	case BR_STATE_FORWARDING:
1456ff39c2d6SFlorian Fainelli 		hw_state = PORT_CTRL_FWD_STATE;
1457ff39c2d6SFlorian Fainelli 		break;
1458ff39c2d6SFlorian Fainelli 	case BR_STATE_BLOCKING:
1459ff39c2d6SFlorian Fainelli 		hw_state = PORT_CTRL_BLOCK_STATE;
1460ff39c2d6SFlorian Fainelli 		break;
1461ff39c2d6SFlorian Fainelli 	default:
1462ff39c2d6SFlorian Fainelli 		dev_err(ds->dev, "invalid STP state: %d\n", state);
1463ff39c2d6SFlorian Fainelli 		return;
1464ff39c2d6SFlorian Fainelli 	}
1465ff39c2d6SFlorian Fainelli 
1466ff39c2d6SFlorian Fainelli 	b53_read8(dev, B53_CTRL_PAGE, B53_PORT_CTRL(port), &reg);
1467ff39c2d6SFlorian Fainelli 	reg &= ~PORT_CTRL_STP_STATE_MASK;
1468ff39c2d6SFlorian Fainelli 	reg |= hw_state;
1469ff39c2d6SFlorian Fainelli 	b53_write8(dev, B53_CTRL_PAGE, B53_PORT_CTRL(port), reg);
1470ff39c2d6SFlorian Fainelli }
14713117455dSFlorian Fainelli EXPORT_SYMBOL(b53_br_set_stp_state);
1472ff39c2d6SFlorian Fainelli 
14733117455dSFlorian Fainelli void b53_br_fast_age(struct dsa_switch *ds, int port)
1474597698f1SVivien Didelot {
1475597698f1SVivien Didelot 	struct b53_device *dev = ds->priv;
1476597698f1SVivien Didelot 
1477597698f1SVivien Didelot 	if (b53_fast_age_port(dev, port))
1478597698f1SVivien Didelot 		dev_err(ds->dev, "fast ageing failed\n");
1479597698f1SVivien Didelot }
14803117455dSFlorian Fainelli EXPORT_SYMBOL(b53_br_fast_age);
1481597698f1SVivien Didelot 
14825ed4e3ebSFlorian Fainelli static bool b53_can_enable_brcm_tags(struct dsa_switch *ds, int port)
14837edc58d6SFlorian Fainelli {
14847edc58d6SFlorian Fainelli 	/* Broadcom switches will accept enabling Broadcom tags on the
14857edc58d6SFlorian Fainelli 	 * following ports: 5, 7 and 8, any other port is not supported
14867edc58d6SFlorian Fainelli 	 */
14875ed4e3ebSFlorian Fainelli 	switch (port) {
14885ed4e3ebSFlorian Fainelli 	case B53_CPU_PORT_25:
14895ed4e3ebSFlorian Fainelli 	case 7:
14905ed4e3ebSFlorian Fainelli 	case B53_CPU_PORT:
14917edc58d6SFlorian Fainelli 		return true;
14927edc58d6SFlorian Fainelli 	}
14937edc58d6SFlorian Fainelli 
14945ed4e3ebSFlorian Fainelli 	dev_warn(ds->dev, "Port %d is not Broadcom tag capable\n", port);
14955ed4e3ebSFlorian Fainelli 	return false;
14965ed4e3ebSFlorian Fainelli }
14975ed4e3ebSFlorian Fainelli 
1498*9f66816aSFlorian Fainelli enum dsa_tag_protocol b53_get_tag_protocol(struct dsa_switch *ds, int port)
14997b314362SAndrew Lunn {
15007edc58d6SFlorian Fainelli 	struct b53_device *dev = ds->priv;
15017edc58d6SFlorian Fainelli 
15027edc58d6SFlorian Fainelli 	/* Older models support a different tag format that we do not
15037edc58d6SFlorian Fainelli 	 * support in net/dsa/tag_brcm.c yet.
15047edc58d6SFlorian Fainelli 	 */
15055ed4e3ebSFlorian Fainelli 	if (is5325(dev) || is5365(dev) || !b53_can_enable_brcm_tags(ds, port))
15067b314362SAndrew Lunn 		return DSA_TAG_PROTO_NONE;
150711606039SFlorian Fainelli 
150811606039SFlorian Fainelli 	/* Broadcom BCM58xx chips have a flow accelerator on Port 8
150911606039SFlorian Fainelli 	 * which requires us to use the prepended Broadcom tag type
151011606039SFlorian Fainelli 	 */
151111606039SFlorian Fainelli 	if (dev->chip_id == BCM58XX_DEVICE_ID && port == B53_CPU_PORT)
151211606039SFlorian Fainelli 		return DSA_TAG_PROTO_BRCM_PREPEND;
151311606039SFlorian Fainelli 
15147edc58d6SFlorian Fainelli 	return DSA_TAG_PROTO_BRCM;
15157b314362SAndrew Lunn }
1516*9f66816aSFlorian Fainelli EXPORT_SYMBOL(b53_get_tag_protocol);
15177b314362SAndrew Lunn 
1518ed3af5fdSFlorian Fainelli int b53_mirror_add(struct dsa_switch *ds, int port,
1519ed3af5fdSFlorian Fainelli 		   struct dsa_mall_mirror_tc_entry *mirror, bool ingress)
1520ed3af5fdSFlorian Fainelli {
1521ed3af5fdSFlorian Fainelli 	struct b53_device *dev = ds->priv;
1522ed3af5fdSFlorian Fainelli 	u16 reg, loc;
1523ed3af5fdSFlorian Fainelli 
1524ed3af5fdSFlorian Fainelli 	if (ingress)
1525ed3af5fdSFlorian Fainelli 		loc = B53_IG_MIR_CTL;
1526ed3af5fdSFlorian Fainelli 	else
1527ed3af5fdSFlorian Fainelli 		loc = B53_EG_MIR_CTL;
1528ed3af5fdSFlorian Fainelli 
1529ed3af5fdSFlorian Fainelli 	b53_read16(dev, B53_MGMT_PAGE, loc, &reg);
1530ed3af5fdSFlorian Fainelli 	reg &= ~MIRROR_MASK;
1531ed3af5fdSFlorian Fainelli 	reg |= BIT(port);
1532ed3af5fdSFlorian Fainelli 	b53_write16(dev, B53_MGMT_PAGE, loc, reg);
1533ed3af5fdSFlorian Fainelli 
1534ed3af5fdSFlorian Fainelli 	b53_read16(dev, B53_MGMT_PAGE, B53_MIR_CAP_CTL, &reg);
1535ed3af5fdSFlorian Fainelli 	reg &= ~CAP_PORT_MASK;
1536ed3af5fdSFlorian Fainelli 	reg |= mirror->to_local_port;
1537ed3af5fdSFlorian Fainelli 	reg |= MIRROR_EN;
1538ed3af5fdSFlorian Fainelli 	b53_write16(dev, B53_MGMT_PAGE, B53_MIR_CAP_CTL, reg);
1539ed3af5fdSFlorian Fainelli 
1540ed3af5fdSFlorian Fainelli 	return 0;
1541ed3af5fdSFlorian Fainelli }
1542ed3af5fdSFlorian Fainelli EXPORT_SYMBOL(b53_mirror_add);
1543ed3af5fdSFlorian Fainelli 
1544ed3af5fdSFlorian Fainelli void b53_mirror_del(struct dsa_switch *ds, int port,
1545ed3af5fdSFlorian Fainelli 		    struct dsa_mall_mirror_tc_entry *mirror)
1546ed3af5fdSFlorian Fainelli {
1547ed3af5fdSFlorian Fainelli 	struct b53_device *dev = ds->priv;
1548ed3af5fdSFlorian Fainelli 	bool loc_disable = false, other_loc_disable = false;
1549ed3af5fdSFlorian Fainelli 	u16 reg, loc;
1550ed3af5fdSFlorian Fainelli 
1551ed3af5fdSFlorian Fainelli 	if (mirror->ingress)
1552ed3af5fdSFlorian Fainelli 		loc = B53_IG_MIR_CTL;
1553ed3af5fdSFlorian Fainelli 	else
1554ed3af5fdSFlorian Fainelli 		loc = B53_EG_MIR_CTL;
1555ed3af5fdSFlorian Fainelli 
1556ed3af5fdSFlorian Fainelli 	/* Update the desired ingress/egress register */
1557ed3af5fdSFlorian Fainelli 	b53_read16(dev, B53_MGMT_PAGE, loc, &reg);
1558ed3af5fdSFlorian Fainelli 	reg &= ~BIT(port);
1559ed3af5fdSFlorian Fainelli 	if (!(reg & MIRROR_MASK))
1560ed3af5fdSFlorian Fainelli 		loc_disable = true;
1561ed3af5fdSFlorian Fainelli 	b53_write16(dev, B53_MGMT_PAGE, loc, reg);
1562ed3af5fdSFlorian Fainelli 
1563ed3af5fdSFlorian Fainelli 	/* Now look at the other one to know if we can disable mirroring
1564ed3af5fdSFlorian Fainelli 	 * entirely
1565ed3af5fdSFlorian Fainelli 	 */
1566ed3af5fdSFlorian Fainelli 	if (mirror->ingress)
1567ed3af5fdSFlorian Fainelli 		b53_read16(dev, B53_MGMT_PAGE, B53_EG_MIR_CTL, &reg);
1568ed3af5fdSFlorian Fainelli 	else
1569ed3af5fdSFlorian Fainelli 		b53_read16(dev, B53_MGMT_PAGE, B53_IG_MIR_CTL, &reg);
1570ed3af5fdSFlorian Fainelli 	if (!(reg & MIRROR_MASK))
1571ed3af5fdSFlorian Fainelli 		other_loc_disable = true;
1572ed3af5fdSFlorian Fainelli 
1573ed3af5fdSFlorian Fainelli 	b53_read16(dev, B53_MGMT_PAGE, B53_MIR_CAP_CTL, &reg);
1574ed3af5fdSFlorian Fainelli 	/* Both no longer have ports, let's disable mirroring */
1575ed3af5fdSFlorian Fainelli 	if (loc_disable && other_loc_disable) {
1576ed3af5fdSFlorian Fainelli 		reg &= ~MIRROR_EN;
1577ed3af5fdSFlorian Fainelli 		reg &= ~mirror->to_local_port;
1578ed3af5fdSFlorian Fainelli 	}
1579ed3af5fdSFlorian Fainelli 	b53_write16(dev, B53_MGMT_PAGE, B53_MIR_CAP_CTL, reg);
1580ed3af5fdSFlorian Fainelli }
1581ed3af5fdSFlorian Fainelli EXPORT_SYMBOL(b53_mirror_del);
1582ed3af5fdSFlorian Fainelli 
158322256b0aSFlorian Fainelli void b53_eee_enable_set(struct dsa_switch *ds, int port, bool enable)
158422256b0aSFlorian Fainelli {
158522256b0aSFlorian Fainelli 	struct b53_device *dev = ds->priv;
158622256b0aSFlorian Fainelli 	u16 reg;
158722256b0aSFlorian Fainelli 
158822256b0aSFlorian Fainelli 	b53_read16(dev, B53_EEE_PAGE, B53_EEE_EN_CTRL, &reg);
158922256b0aSFlorian Fainelli 	if (enable)
159022256b0aSFlorian Fainelli 		reg |= BIT(port);
159122256b0aSFlorian Fainelli 	else
159222256b0aSFlorian Fainelli 		reg &= ~BIT(port);
159322256b0aSFlorian Fainelli 	b53_write16(dev, B53_EEE_PAGE, B53_EEE_EN_CTRL, reg);
159422256b0aSFlorian Fainelli }
159522256b0aSFlorian Fainelli EXPORT_SYMBOL(b53_eee_enable_set);
159622256b0aSFlorian Fainelli 
159722256b0aSFlorian Fainelli 
159822256b0aSFlorian Fainelli /* Returns 0 if EEE was not enabled, or 1 otherwise
159922256b0aSFlorian Fainelli  */
160022256b0aSFlorian Fainelli int b53_eee_init(struct dsa_switch *ds, int port, struct phy_device *phy)
160122256b0aSFlorian Fainelli {
160222256b0aSFlorian Fainelli 	int ret;
160322256b0aSFlorian Fainelli 
160422256b0aSFlorian Fainelli 	ret = phy_init_eee(phy, 0);
160522256b0aSFlorian Fainelli 	if (ret)
160622256b0aSFlorian Fainelli 		return 0;
160722256b0aSFlorian Fainelli 
160822256b0aSFlorian Fainelli 	b53_eee_enable_set(ds, port, true);
160922256b0aSFlorian Fainelli 
161022256b0aSFlorian Fainelli 	return 1;
161122256b0aSFlorian Fainelli }
161222256b0aSFlorian Fainelli EXPORT_SYMBOL(b53_eee_init);
161322256b0aSFlorian Fainelli 
161422256b0aSFlorian Fainelli int b53_get_mac_eee(struct dsa_switch *ds, int port, struct ethtool_eee *e)
161522256b0aSFlorian Fainelli {
161622256b0aSFlorian Fainelli 	struct b53_device *dev = ds->priv;
161722256b0aSFlorian Fainelli 	struct ethtool_eee *p = &dev->ports[port].eee;
161822256b0aSFlorian Fainelli 	u16 reg;
161922256b0aSFlorian Fainelli 
162022256b0aSFlorian Fainelli 	if (is5325(dev) || is5365(dev))
162122256b0aSFlorian Fainelli 		return -EOPNOTSUPP;
162222256b0aSFlorian Fainelli 
162322256b0aSFlorian Fainelli 	b53_read16(dev, B53_EEE_PAGE, B53_EEE_LPI_INDICATE, &reg);
162422256b0aSFlorian Fainelli 	e->eee_enabled = p->eee_enabled;
162522256b0aSFlorian Fainelli 	e->eee_active = !!(reg & BIT(port));
162622256b0aSFlorian Fainelli 
162722256b0aSFlorian Fainelli 	return 0;
162822256b0aSFlorian Fainelli }
162922256b0aSFlorian Fainelli EXPORT_SYMBOL(b53_get_mac_eee);
163022256b0aSFlorian Fainelli 
163122256b0aSFlorian Fainelli int b53_set_mac_eee(struct dsa_switch *ds, int port, struct ethtool_eee *e)
163222256b0aSFlorian Fainelli {
163322256b0aSFlorian Fainelli 	struct b53_device *dev = ds->priv;
163422256b0aSFlorian Fainelli 	struct ethtool_eee *p = &dev->ports[port].eee;
163522256b0aSFlorian Fainelli 
163622256b0aSFlorian Fainelli 	if (is5325(dev) || is5365(dev))
163722256b0aSFlorian Fainelli 		return -EOPNOTSUPP;
163822256b0aSFlorian Fainelli 
163922256b0aSFlorian Fainelli 	p->eee_enabled = e->eee_enabled;
164022256b0aSFlorian Fainelli 	b53_eee_enable_set(ds, port, e->eee_enabled);
164122256b0aSFlorian Fainelli 
164222256b0aSFlorian Fainelli 	return 0;
164322256b0aSFlorian Fainelli }
164422256b0aSFlorian Fainelli EXPORT_SYMBOL(b53_set_mac_eee);
164522256b0aSFlorian Fainelli 
1646a82f67afSFlorian Fainelli static const struct dsa_switch_ops b53_switch_ops = {
16477b314362SAndrew Lunn 	.get_tag_protocol	= b53_get_tag_protocol,
1648967dd82fSFlorian Fainelli 	.setup			= b53_setup,
1649967dd82fSFlorian Fainelli 	.get_strings		= b53_get_strings,
1650967dd82fSFlorian Fainelli 	.get_ethtool_stats	= b53_get_ethtool_stats,
1651967dd82fSFlorian Fainelli 	.get_sset_count		= b53_get_sset_count,
1652967dd82fSFlorian Fainelli 	.phy_read		= b53_phy_read16,
1653967dd82fSFlorian Fainelli 	.phy_write		= b53_phy_write16,
1654967dd82fSFlorian Fainelli 	.adjust_link		= b53_adjust_link,
1655967dd82fSFlorian Fainelli 	.port_enable		= b53_enable_port,
1656967dd82fSFlorian Fainelli 	.port_disable		= b53_disable_port,
1657f43a2dbeSFlorian Fainelli 	.get_mac_eee		= b53_get_mac_eee,
1658f43a2dbeSFlorian Fainelli 	.set_mac_eee		= b53_set_mac_eee,
1659ff39c2d6SFlorian Fainelli 	.port_bridge_join	= b53_br_join,
1660ff39c2d6SFlorian Fainelli 	.port_bridge_leave	= b53_br_leave,
1661ff39c2d6SFlorian Fainelli 	.port_stp_state_set	= b53_br_set_stp_state,
1662597698f1SVivien Didelot 	.port_fast_age		= b53_br_fast_age,
1663a2482d2cSFlorian Fainelli 	.port_vlan_filtering	= b53_vlan_filtering,
1664a2482d2cSFlorian Fainelli 	.port_vlan_prepare	= b53_vlan_prepare,
1665a2482d2cSFlorian Fainelli 	.port_vlan_add		= b53_vlan_add,
1666a2482d2cSFlorian Fainelli 	.port_vlan_del		= b53_vlan_del,
16671da6df85SFlorian Fainelli 	.port_fdb_dump		= b53_fdb_dump,
16681da6df85SFlorian Fainelli 	.port_fdb_add		= b53_fdb_add,
16691da6df85SFlorian Fainelli 	.port_fdb_del		= b53_fdb_del,
1670ed3af5fdSFlorian Fainelli 	.port_mirror_add	= b53_mirror_add,
1671ed3af5fdSFlorian Fainelli 	.port_mirror_del	= b53_mirror_del,
1672967dd82fSFlorian Fainelli };
1673967dd82fSFlorian Fainelli 
1674967dd82fSFlorian Fainelli struct b53_chip_data {
1675967dd82fSFlorian Fainelli 	u32 chip_id;
1676967dd82fSFlorian Fainelli 	const char *dev_name;
1677967dd82fSFlorian Fainelli 	u16 vlans;
1678967dd82fSFlorian Fainelli 	u16 enabled_ports;
1679967dd82fSFlorian Fainelli 	u8 cpu_port;
1680967dd82fSFlorian Fainelli 	u8 vta_regs[3];
16811da6df85SFlorian Fainelli 	u8 arl_entries;
1682967dd82fSFlorian Fainelli 	u8 duplex_reg;
1683967dd82fSFlorian Fainelli 	u8 jumbo_pm_reg;
1684967dd82fSFlorian Fainelli 	u8 jumbo_size_reg;
1685967dd82fSFlorian Fainelli };
1686967dd82fSFlorian Fainelli 
1687967dd82fSFlorian Fainelli #define B53_VTA_REGS	\
1688967dd82fSFlorian Fainelli 	{ B53_VT_ACCESS, B53_VT_INDEX, B53_VT_ENTRY }
1689967dd82fSFlorian Fainelli #define B53_VTA_REGS_9798 \
1690967dd82fSFlorian Fainelli 	{ B53_VT_ACCESS_9798, B53_VT_INDEX_9798, B53_VT_ENTRY_9798 }
1691967dd82fSFlorian Fainelli #define B53_VTA_REGS_63XX \
1692967dd82fSFlorian Fainelli 	{ B53_VT_ACCESS_63XX, B53_VT_INDEX_63XX, B53_VT_ENTRY_63XX }
1693967dd82fSFlorian Fainelli 
1694967dd82fSFlorian Fainelli static const struct b53_chip_data b53_switch_chips[] = {
1695967dd82fSFlorian Fainelli 	{
1696967dd82fSFlorian Fainelli 		.chip_id = BCM5325_DEVICE_ID,
1697967dd82fSFlorian Fainelli 		.dev_name = "BCM5325",
1698967dd82fSFlorian Fainelli 		.vlans = 16,
1699967dd82fSFlorian Fainelli 		.enabled_ports = 0x1f,
17001da6df85SFlorian Fainelli 		.arl_entries = 2,
1701967dd82fSFlorian Fainelli 		.cpu_port = B53_CPU_PORT_25,
1702967dd82fSFlorian Fainelli 		.duplex_reg = B53_DUPLEX_STAT_FE,
1703967dd82fSFlorian Fainelli 	},
1704967dd82fSFlorian Fainelli 	{
1705967dd82fSFlorian Fainelli 		.chip_id = BCM5365_DEVICE_ID,
1706967dd82fSFlorian Fainelli 		.dev_name = "BCM5365",
1707967dd82fSFlorian Fainelli 		.vlans = 256,
1708967dd82fSFlorian Fainelli 		.enabled_ports = 0x1f,
17091da6df85SFlorian Fainelli 		.arl_entries = 2,
1710967dd82fSFlorian Fainelli 		.cpu_port = B53_CPU_PORT_25,
1711967dd82fSFlorian Fainelli 		.duplex_reg = B53_DUPLEX_STAT_FE,
1712967dd82fSFlorian Fainelli 	},
1713967dd82fSFlorian Fainelli 	{
1714967dd82fSFlorian Fainelli 		.chip_id = BCM5395_DEVICE_ID,
1715967dd82fSFlorian Fainelli 		.dev_name = "BCM5395",
1716967dd82fSFlorian Fainelli 		.vlans = 4096,
1717967dd82fSFlorian Fainelli 		.enabled_ports = 0x1f,
17181da6df85SFlorian Fainelli 		.arl_entries = 4,
1719967dd82fSFlorian Fainelli 		.cpu_port = B53_CPU_PORT,
1720967dd82fSFlorian Fainelli 		.vta_regs = B53_VTA_REGS,
1721967dd82fSFlorian Fainelli 		.duplex_reg = B53_DUPLEX_STAT_GE,
1722967dd82fSFlorian Fainelli 		.jumbo_pm_reg = B53_JUMBO_PORT_MASK,
1723967dd82fSFlorian Fainelli 		.jumbo_size_reg = B53_JUMBO_MAX_SIZE,
1724967dd82fSFlorian Fainelli 	},
1725967dd82fSFlorian Fainelli 	{
1726967dd82fSFlorian Fainelli 		.chip_id = BCM5397_DEVICE_ID,
1727967dd82fSFlorian Fainelli 		.dev_name = "BCM5397",
1728967dd82fSFlorian Fainelli 		.vlans = 4096,
1729967dd82fSFlorian Fainelli 		.enabled_ports = 0x1f,
17301da6df85SFlorian Fainelli 		.arl_entries = 4,
1731967dd82fSFlorian Fainelli 		.cpu_port = B53_CPU_PORT,
1732967dd82fSFlorian Fainelli 		.vta_regs = B53_VTA_REGS_9798,
1733967dd82fSFlorian Fainelli 		.duplex_reg = B53_DUPLEX_STAT_GE,
1734967dd82fSFlorian Fainelli 		.jumbo_pm_reg = B53_JUMBO_PORT_MASK,
1735967dd82fSFlorian Fainelli 		.jumbo_size_reg = B53_JUMBO_MAX_SIZE,
1736967dd82fSFlorian Fainelli 	},
1737967dd82fSFlorian Fainelli 	{
1738967dd82fSFlorian Fainelli 		.chip_id = BCM5398_DEVICE_ID,
1739967dd82fSFlorian Fainelli 		.dev_name = "BCM5398",
1740967dd82fSFlorian Fainelli 		.vlans = 4096,
1741967dd82fSFlorian Fainelli 		.enabled_ports = 0x7f,
17421da6df85SFlorian Fainelli 		.arl_entries = 4,
1743967dd82fSFlorian Fainelli 		.cpu_port = B53_CPU_PORT,
1744967dd82fSFlorian Fainelli 		.vta_regs = B53_VTA_REGS_9798,
1745967dd82fSFlorian Fainelli 		.duplex_reg = B53_DUPLEX_STAT_GE,
1746967dd82fSFlorian Fainelli 		.jumbo_pm_reg = B53_JUMBO_PORT_MASK,
1747967dd82fSFlorian Fainelli 		.jumbo_size_reg = B53_JUMBO_MAX_SIZE,
1748967dd82fSFlorian Fainelli 	},
1749967dd82fSFlorian Fainelli 	{
1750967dd82fSFlorian Fainelli 		.chip_id = BCM53115_DEVICE_ID,
1751967dd82fSFlorian Fainelli 		.dev_name = "BCM53115",
1752967dd82fSFlorian Fainelli 		.vlans = 4096,
1753967dd82fSFlorian Fainelli 		.enabled_ports = 0x1f,
17541da6df85SFlorian Fainelli 		.arl_entries = 4,
1755967dd82fSFlorian Fainelli 		.vta_regs = B53_VTA_REGS,
1756967dd82fSFlorian Fainelli 		.cpu_port = B53_CPU_PORT,
1757967dd82fSFlorian Fainelli 		.duplex_reg = B53_DUPLEX_STAT_GE,
1758967dd82fSFlorian Fainelli 		.jumbo_pm_reg = B53_JUMBO_PORT_MASK,
1759967dd82fSFlorian Fainelli 		.jumbo_size_reg = B53_JUMBO_MAX_SIZE,
1760967dd82fSFlorian Fainelli 	},
1761967dd82fSFlorian Fainelli 	{
1762967dd82fSFlorian Fainelli 		.chip_id = BCM53125_DEVICE_ID,
1763967dd82fSFlorian Fainelli 		.dev_name = "BCM53125",
1764967dd82fSFlorian Fainelli 		.vlans = 4096,
1765967dd82fSFlorian Fainelli 		.enabled_ports = 0xff,
1766be35e8c5SFlorian Fainelli 		.arl_entries = 4,
1767967dd82fSFlorian Fainelli 		.cpu_port = B53_CPU_PORT,
1768967dd82fSFlorian Fainelli 		.vta_regs = B53_VTA_REGS,
1769967dd82fSFlorian Fainelli 		.duplex_reg = B53_DUPLEX_STAT_GE,
1770967dd82fSFlorian Fainelli 		.jumbo_pm_reg = B53_JUMBO_PORT_MASK,
1771967dd82fSFlorian Fainelli 		.jumbo_size_reg = B53_JUMBO_MAX_SIZE,
1772967dd82fSFlorian Fainelli 	},
1773967dd82fSFlorian Fainelli 	{
1774967dd82fSFlorian Fainelli 		.chip_id = BCM53128_DEVICE_ID,
1775967dd82fSFlorian Fainelli 		.dev_name = "BCM53128",
1776967dd82fSFlorian Fainelli 		.vlans = 4096,
1777967dd82fSFlorian Fainelli 		.enabled_ports = 0x1ff,
17781da6df85SFlorian Fainelli 		.arl_entries = 4,
1779967dd82fSFlorian Fainelli 		.cpu_port = B53_CPU_PORT,
1780967dd82fSFlorian Fainelli 		.vta_regs = B53_VTA_REGS,
1781967dd82fSFlorian Fainelli 		.duplex_reg = B53_DUPLEX_STAT_GE,
1782967dd82fSFlorian Fainelli 		.jumbo_pm_reg = B53_JUMBO_PORT_MASK,
1783967dd82fSFlorian Fainelli 		.jumbo_size_reg = B53_JUMBO_MAX_SIZE,
1784967dd82fSFlorian Fainelli 	},
1785967dd82fSFlorian Fainelli 	{
1786967dd82fSFlorian Fainelli 		.chip_id = BCM63XX_DEVICE_ID,
1787967dd82fSFlorian Fainelli 		.dev_name = "BCM63xx",
1788967dd82fSFlorian Fainelli 		.vlans = 4096,
1789967dd82fSFlorian Fainelli 		.enabled_ports = 0, /* pdata must provide them */
17901da6df85SFlorian Fainelli 		.arl_entries = 4,
1791967dd82fSFlorian Fainelli 		.cpu_port = B53_CPU_PORT,
1792967dd82fSFlorian Fainelli 		.vta_regs = B53_VTA_REGS_63XX,
1793967dd82fSFlorian Fainelli 		.duplex_reg = B53_DUPLEX_STAT_63XX,
1794967dd82fSFlorian Fainelli 		.jumbo_pm_reg = B53_JUMBO_PORT_MASK_63XX,
1795967dd82fSFlorian Fainelli 		.jumbo_size_reg = B53_JUMBO_MAX_SIZE_63XX,
1796967dd82fSFlorian Fainelli 	},
1797967dd82fSFlorian Fainelli 	{
1798967dd82fSFlorian Fainelli 		.chip_id = BCM53010_DEVICE_ID,
1799967dd82fSFlorian Fainelli 		.dev_name = "BCM53010",
1800967dd82fSFlorian Fainelli 		.vlans = 4096,
1801967dd82fSFlorian Fainelli 		.enabled_ports = 0x1f,
18021da6df85SFlorian Fainelli 		.arl_entries = 4,
1803967dd82fSFlorian Fainelli 		.cpu_port = B53_CPU_PORT_25, /* TODO: auto detect */
1804967dd82fSFlorian Fainelli 		.vta_regs = B53_VTA_REGS,
1805967dd82fSFlorian Fainelli 		.duplex_reg = B53_DUPLEX_STAT_GE,
1806967dd82fSFlorian Fainelli 		.jumbo_pm_reg = B53_JUMBO_PORT_MASK,
1807967dd82fSFlorian Fainelli 		.jumbo_size_reg = B53_JUMBO_MAX_SIZE,
1808967dd82fSFlorian Fainelli 	},
1809967dd82fSFlorian Fainelli 	{
1810967dd82fSFlorian Fainelli 		.chip_id = BCM53011_DEVICE_ID,
1811967dd82fSFlorian Fainelli 		.dev_name = "BCM53011",
1812967dd82fSFlorian Fainelli 		.vlans = 4096,
1813967dd82fSFlorian Fainelli 		.enabled_ports = 0x1bf,
18141da6df85SFlorian Fainelli 		.arl_entries = 4,
1815967dd82fSFlorian Fainelli 		.cpu_port = B53_CPU_PORT_25, /* TODO: auto detect */
1816967dd82fSFlorian Fainelli 		.vta_regs = B53_VTA_REGS,
1817967dd82fSFlorian Fainelli 		.duplex_reg = B53_DUPLEX_STAT_GE,
1818967dd82fSFlorian Fainelli 		.jumbo_pm_reg = B53_JUMBO_PORT_MASK,
1819967dd82fSFlorian Fainelli 		.jumbo_size_reg = B53_JUMBO_MAX_SIZE,
1820967dd82fSFlorian Fainelli 	},
1821967dd82fSFlorian Fainelli 	{
1822967dd82fSFlorian Fainelli 		.chip_id = BCM53012_DEVICE_ID,
1823967dd82fSFlorian Fainelli 		.dev_name = "BCM53012",
1824967dd82fSFlorian Fainelli 		.vlans = 4096,
1825967dd82fSFlorian Fainelli 		.enabled_ports = 0x1bf,
18261da6df85SFlorian Fainelli 		.arl_entries = 4,
1827967dd82fSFlorian Fainelli 		.cpu_port = B53_CPU_PORT_25, /* TODO: auto detect */
1828967dd82fSFlorian Fainelli 		.vta_regs = B53_VTA_REGS,
1829967dd82fSFlorian Fainelli 		.duplex_reg = B53_DUPLEX_STAT_GE,
1830967dd82fSFlorian Fainelli 		.jumbo_pm_reg = B53_JUMBO_PORT_MASK,
1831967dd82fSFlorian Fainelli 		.jumbo_size_reg = B53_JUMBO_MAX_SIZE,
1832967dd82fSFlorian Fainelli 	},
1833967dd82fSFlorian Fainelli 	{
1834967dd82fSFlorian Fainelli 		.chip_id = BCM53018_DEVICE_ID,
1835967dd82fSFlorian Fainelli 		.dev_name = "BCM53018",
1836967dd82fSFlorian Fainelli 		.vlans = 4096,
1837967dd82fSFlorian Fainelli 		.enabled_ports = 0x1f,
18381da6df85SFlorian Fainelli 		.arl_entries = 4,
1839967dd82fSFlorian Fainelli 		.cpu_port = B53_CPU_PORT_25, /* TODO: auto detect */
1840967dd82fSFlorian Fainelli 		.vta_regs = B53_VTA_REGS,
1841967dd82fSFlorian Fainelli 		.duplex_reg = B53_DUPLEX_STAT_GE,
1842967dd82fSFlorian Fainelli 		.jumbo_pm_reg = B53_JUMBO_PORT_MASK,
1843967dd82fSFlorian Fainelli 		.jumbo_size_reg = B53_JUMBO_MAX_SIZE,
1844967dd82fSFlorian Fainelli 	},
1845967dd82fSFlorian Fainelli 	{
1846967dd82fSFlorian Fainelli 		.chip_id = BCM53019_DEVICE_ID,
1847967dd82fSFlorian Fainelli 		.dev_name = "BCM53019",
1848967dd82fSFlorian Fainelli 		.vlans = 4096,
1849967dd82fSFlorian Fainelli 		.enabled_ports = 0x1f,
18501da6df85SFlorian Fainelli 		.arl_entries = 4,
1851967dd82fSFlorian Fainelli 		.cpu_port = B53_CPU_PORT_25, /* TODO: auto detect */
1852967dd82fSFlorian Fainelli 		.vta_regs = B53_VTA_REGS,
1853967dd82fSFlorian Fainelli 		.duplex_reg = B53_DUPLEX_STAT_GE,
1854967dd82fSFlorian Fainelli 		.jumbo_pm_reg = B53_JUMBO_PORT_MASK,
1855967dd82fSFlorian Fainelli 		.jumbo_size_reg = B53_JUMBO_MAX_SIZE,
1856967dd82fSFlorian Fainelli 	},
1857991a36bbSFlorian Fainelli 	{
1858991a36bbSFlorian Fainelli 		.chip_id = BCM58XX_DEVICE_ID,
1859991a36bbSFlorian Fainelli 		.dev_name = "BCM585xx/586xx/88312",
1860991a36bbSFlorian Fainelli 		.vlans	= 4096,
1861991a36bbSFlorian Fainelli 		.enabled_ports = 0x1ff,
1862991a36bbSFlorian Fainelli 		.arl_entries = 4,
1863bfcda65cSFlorian Fainelli 		.cpu_port = B53_CPU_PORT,
1864991a36bbSFlorian Fainelli 		.vta_regs = B53_VTA_REGS,
1865991a36bbSFlorian Fainelli 		.duplex_reg = B53_DUPLEX_STAT_GE,
1866991a36bbSFlorian Fainelli 		.jumbo_pm_reg = B53_JUMBO_PORT_MASK,
1867991a36bbSFlorian Fainelli 		.jumbo_size_reg = B53_JUMBO_MAX_SIZE,
1868991a36bbSFlorian Fainelli 	},
1869130401d9SFlorian Fainelli 	{
1870130401d9SFlorian Fainelli 		.chip_id = BCM7445_DEVICE_ID,
1871130401d9SFlorian Fainelli 		.dev_name = "BCM7445",
1872130401d9SFlorian Fainelli 		.vlans	= 4096,
1873130401d9SFlorian Fainelli 		.enabled_ports = 0x1ff,
1874130401d9SFlorian Fainelli 		.arl_entries = 4,
1875130401d9SFlorian Fainelli 		.cpu_port = B53_CPU_PORT,
1876130401d9SFlorian Fainelli 		.vta_regs = B53_VTA_REGS,
1877130401d9SFlorian Fainelli 		.duplex_reg = B53_DUPLEX_STAT_GE,
1878130401d9SFlorian Fainelli 		.jumbo_pm_reg = B53_JUMBO_PORT_MASK,
1879130401d9SFlorian Fainelli 		.jumbo_size_reg = B53_JUMBO_MAX_SIZE,
1880130401d9SFlorian Fainelli 	},
18810fe99338SFlorian Fainelli 	{
18820fe99338SFlorian Fainelli 		.chip_id = BCM7278_DEVICE_ID,
18830fe99338SFlorian Fainelli 		.dev_name = "BCM7278",
18840fe99338SFlorian Fainelli 		.vlans = 4096,
18850fe99338SFlorian Fainelli 		.enabled_ports = 0x1ff,
18860fe99338SFlorian Fainelli 		.arl_entries= 4,
18870fe99338SFlorian Fainelli 		.cpu_port = B53_CPU_PORT,
18880fe99338SFlorian Fainelli 		.vta_regs = B53_VTA_REGS,
18890fe99338SFlorian Fainelli 		.duplex_reg = B53_DUPLEX_STAT_GE,
18900fe99338SFlorian Fainelli 		.jumbo_pm_reg = B53_JUMBO_PORT_MASK,
18910fe99338SFlorian Fainelli 		.jumbo_size_reg = B53_JUMBO_MAX_SIZE,
18920fe99338SFlorian Fainelli 	},
1893967dd82fSFlorian Fainelli };
1894967dd82fSFlorian Fainelli 
1895967dd82fSFlorian Fainelli static int b53_switch_init(struct b53_device *dev)
1896967dd82fSFlorian Fainelli {
1897967dd82fSFlorian Fainelli 	unsigned int i;
1898967dd82fSFlorian Fainelli 	int ret;
1899967dd82fSFlorian Fainelli 
1900967dd82fSFlorian Fainelli 	for (i = 0; i < ARRAY_SIZE(b53_switch_chips); i++) {
1901967dd82fSFlorian Fainelli 		const struct b53_chip_data *chip = &b53_switch_chips[i];
1902967dd82fSFlorian Fainelli 
1903967dd82fSFlorian Fainelli 		if (chip->chip_id == dev->chip_id) {
1904967dd82fSFlorian Fainelli 			if (!dev->enabled_ports)
1905967dd82fSFlorian Fainelli 				dev->enabled_ports = chip->enabled_ports;
1906967dd82fSFlorian Fainelli 			dev->name = chip->dev_name;
1907967dd82fSFlorian Fainelli 			dev->duplex_reg = chip->duplex_reg;
1908967dd82fSFlorian Fainelli 			dev->vta_regs[0] = chip->vta_regs[0];
1909967dd82fSFlorian Fainelli 			dev->vta_regs[1] = chip->vta_regs[1];
1910967dd82fSFlorian Fainelli 			dev->vta_regs[2] = chip->vta_regs[2];
1911967dd82fSFlorian Fainelli 			dev->jumbo_pm_reg = chip->jumbo_pm_reg;
1912967dd82fSFlorian Fainelli 			dev->cpu_port = chip->cpu_port;
1913967dd82fSFlorian Fainelli 			dev->num_vlans = chip->vlans;
19141da6df85SFlorian Fainelli 			dev->num_arl_entries = chip->arl_entries;
1915967dd82fSFlorian Fainelli 			break;
1916967dd82fSFlorian Fainelli 		}
1917967dd82fSFlorian Fainelli 	}
1918967dd82fSFlorian Fainelli 
1919967dd82fSFlorian Fainelli 	/* check which BCM5325x version we have */
1920967dd82fSFlorian Fainelli 	if (is5325(dev)) {
1921967dd82fSFlorian Fainelli 		u8 vc4;
1922967dd82fSFlorian Fainelli 
1923967dd82fSFlorian Fainelli 		b53_read8(dev, B53_VLAN_PAGE, B53_VLAN_CTRL4_25, &vc4);
1924967dd82fSFlorian Fainelli 
1925967dd82fSFlorian Fainelli 		/* check reserved bits */
1926967dd82fSFlorian Fainelli 		switch (vc4 & 3) {
1927967dd82fSFlorian Fainelli 		case 1:
1928967dd82fSFlorian Fainelli 			/* BCM5325E */
1929967dd82fSFlorian Fainelli 			break;
1930967dd82fSFlorian Fainelli 		case 3:
1931967dd82fSFlorian Fainelli 			/* BCM5325F - do not use port 4 */
1932967dd82fSFlorian Fainelli 			dev->enabled_ports &= ~BIT(4);
1933967dd82fSFlorian Fainelli 			break;
1934967dd82fSFlorian Fainelli 		default:
1935967dd82fSFlorian Fainelli /* On the BCM47XX SoCs this is the supported internal switch.*/
1936967dd82fSFlorian Fainelli #ifndef CONFIG_BCM47XX
1937967dd82fSFlorian Fainelli 			/* BCM5325M */
1938967dd82fSFlorian Fainelli 			return -EINVAL;
1939967dd82fSFlorian Fainelli #else
1940967dd82fSFlorian Fainelli 			break;
1941967dd82fSFlorian Fainelli #endif
1942967dd82fSFlorian Fainelli 		}
1943967dd82fSFlorian Fainelli 	} else if (dev->chip_id == BCM53115_DEVICE_ID) {
1944967dd82fSFlorian Fainelli 		u64 strap_value;
1945967dd82fSFlorian Fainelli 
1946967dd82fSFlorian Fainelli 		b53_read48(dev, B53_STAT_PAGE, B53_STRAP_VALUE, &strap_value);
1947967dd82fSFlorian Fainelli 		/* use second IMP port if GMII is enabled */
1948967dd82fSFlorian Fainelli 		if (strap_value & SV_GMII_CTRL_115)
1949967dd82fSFlorian Fainelli 			dev->cpu_port = 5;
1950967dd82fSFlorian Fainelli 	}
1951967dd82fSFlorian Fainelli 
1952967dd82fSFlorian Fainelli 	/* cpu port is always last */
1953967dd82fSFlorian Fainelli 	dev->num_ports = dev->cpu_port + 1;
1954967dd82fSFlorian Fainelli 	dev->enabled_ports |= BIT(dev->cpu_port);
1955967dd82fSFlorian Fainelli 
1956967dd82fSFlorian Fainelli 	dev->ports = devm_kzalloc(dev->dev,
1957967dd82fSFlorian Fainelli 				  sizeof(struct b53_port) * dev->num_ports,
1958967dd82fSFlorian Fainelli 				  GFP_KERNEL);
1959967dd82fSFlorian Fainelli 	if (!dev->ports)
1960967dd82fSFlorian Fainelli 		return -ENOMEM;
1961967dd82fSFlorian Fainelli 
1962a2482d2cSFlorian Fainelli 	dev->vlans = devm_kzalloc(dev->dev,
1963a2482d2cSFlorian Fainelli 				  sizeof(struct b53_vlan) * dev->num_vlans,
1964a2482d2cSFlorian Fainelli 				  GFP_KERNEL);
1965a2482d2cSFlorian Fainelli 	if (!dev->vlans)
1966a2482d2cSFlorian Fainelli 		return -ENOMEM;
1967a2482d2cSFlorian Fainelli 
1968967dd82fSFlorian Fainelli 	dev->reset_gpio = b53_switch_get_reset_gpio(dev);
1969967dd82fSFlorian Fainelli 	if (dev->reset_gpio >= 0) {
1970967dd82fSFlorian Fainelli 		ret = devm_gpio_request_one(dev->dev, dev->reset_gpio,
1971967dd82fSFlorian Fainelli 					    GPIOF_OUT_INIT_HIGH, "robo_reset");
1972967dd82fSFlorian Fainelli 		if (ret)
1973967dd82fSFlorian Fainelli 			return ret;
1974967dd82fSFlorian Fainelli 	}
1975967dd82fSFlorian Fainelli 
1976967dd82fSFlorian Fainelli 	return 0;
1977967dd82fSFlorian Fainelli }
1978967dd82fSFlorian Fainelli 
19790dff88d3SJulia Lawall struct b53_device *b53_switch_alloc(struct device *base,
19800dff88d3SJulia Lawall 				    const struct b53_io_ops *ops,
1981967dd82fSFlorian Fainelli 				    void *priv)
1982967dd82fSFlorian Fainelli {
1983967dd82fSFlorian Fainelli 	struct dsa_switch *ds;
1984967dd82fSFlorian Fainelli 	struct b53_device *dev;
1985967dd82fSFlorian Fainelli 
1986a0c02161SVivien Didelot 	ds = dsa_switch_alloc(base, DSA_MAX_PORTS);
1987967dd82fSFlorian Fainelli 	if (!ds)
1988967dd82fSFlorian Fainelli 		return NULL;
1989967dd82fSFlorian Fainelli 
1990a0c02161SVivien Didelot 	dev = devm_kzalloc(base, sizeof(*dev), GFP_KERNEL);
1991a0c02161SVivien Didelot 	if (!dev)
1992a0c02161SVivien Didelot 		return NULL;
1993967dd82fSFlorian Fainelli 
1994967dd82fSFlorian Fainelli 	ds->priv = dev;
1995967dd82fSFlorian Fainelli 	dev->dev = base;
1996967dd82fSFlorian Fainelli 
1997967dd82fSFlorian Fainelli 	dev->ds = ds;
1998967dd82fSFlorian Fainelli 	dev->priv = priv;
1999967dd82fSFlorian Fainelli 	dev->ops = ops;
2000485ebd61SFlorian Fainelli 	ds->ops = &b53_switch_ops;
2001967dd82fSFlorian Fainelli 	mutex_init(&dev->reg_mutex);
2002967dd82fSFlorian Fainelli 	mutex_init(&dev->stats_mutex);
2003967dd82fSFlorian Fainelli 
2004967dd82fSFlorian Fainelli 	return dev;
2005967dd82fSFlorian Fainelli }
2006967dd82fSFlorian Fainelli EXPORT_SYMBOL(b53_switch_alloc);
2007967dd82fSFlorian Fainelli 
2008967dd82fSFlorian Fainelli int b53_switch_detect(struct b53_device *dev)
2009967dd82fSFlorian Fainelli {
2010967dd82fSFlorian Fainelli 	u32 id32;
2011967dd82fSFlorian Fainelli 	u16 tmp;
2012967dd82fSFlorian Fainelli 	u8 id8;
2013967dd82fSFlorian Fainelli 	int ret;
2014967dd82fSFlorian Fainelli 
2015967dd82fSFlorian Fainelli 	ret = b53_read8(dev, B53_MGMT_PAGE, B53_DEVICE_ID, &id8);
2016967dd82fSFlorian Fainelli 	if (ret)
2017967dd82fSFlorian Fainelli 		return ret;
2018967dd82fSFlorian Fainelli 
2019967dd82fSFlorian Fainelli 	switch (id8) {
2020967dd82fSFlorian Fainelli 	case 0:
2021967dd82fSFlorian Fainelli 		/* BCM5325 and BCM5365 do not have this register so reads
2022967dd82fSFlorian Fainelli 		 * return 0. But the read operation did succeed, so assume this
2023967dd82fSFlorian Fainelli 		 * is one of them.
2024967dd82fSFlorian Fainelli 		 *
2025967dd82fSFlorian Fainelli 		 * Next check if we can write to the 5325's VTA register; for
2026967dd82fSFlorian Fainelli 		 * 5365 it is read only.
2027967dd82fSFlorian Fainelli 		 */
2028967dd82fSFlorian Fainelli 		b53_write16(dev, B53_VLAN_PAGE, B53_VLAN_TABLE_ACCESS_25, 0xf);
2029967dd82fSFlorian Fainelli 		b53_read16(dev, B53_VLAN_PAGE, B53_VLAN_TABLE_ACCESS_25, &tmp);
2030967dd82fSFlorian Fainelli 
2031967dd82fSFlorian Fainelli 		if (tmp == 0xf)
2032967dd82fSFlorian Fainelli 			dev->chip_id = BCM5325_DEVICE_ID;
2033967dd82fSFlorian Fainelli 		else
2034967dd82fSFlorian Fainelli 			dev->chip_id = BCM5365_DEVICE_ID;
2035967dd82fSFlorian Fainelli 		break;
2036967dd82fSFlorian Fainelli 	case BCM5395_DEVICE_ID:
2037967dd82fSFlorian Fainelli 	case BCM5397_DEVICE_ID:
2038967dd82fSFlorian Fainelli 	case BCM5398_DEVICE_ID:
2039967dd82fSFlorian Fainelli 		dev->chip_id = id8;
2040967dd82fSFlorian Fainelli 		break;
2041967dd82fSFlorian Fainelli 	default:
2042967dd82fSFlorian Fainelli 		ret = b53_read32(dev, B53_MGMT_PAGE, B53_DEVICE_ID, &id32);
2043967dd82fSFlorian Fainelli 		if (ret)
2044967dd82fSFlorian Fainelli 			return ret;
2045967dd82fSFlorian Fainelli 
2046967dd82fSFlorian Fainelli 		switch (id32) {
2047967dd82fSFlorian Fainelli 		case BCM53115_DEVICE_ID:
2048967dd82fSFlorian Fainelli 		case BCM53125_DEVICE_ID:
2049967dd82fSFlorian Fainelli 		case BCM53128_DEVICE_ID:
2050967dd82fSFlorian Fainelli 		case BCM53010_DEVICE_ID:
2051967dd82fSFlorian Fainelli 		case BCM53011_DEVICE_ID:
2052967dd82fSFlorian Fainelli 		case BCM53012_DEVICE_ID:
2053967dd82fSFlorian Fainelli 		case BCM53018_DEVICE_ID:
2054967dd82fSFlorian Fainelli 		case BCM53019_DEVICE_ID:
2055967dd82fSFlorian Fainelli 			dev->chip_id = id32;
2056967dd82fSFlorian Fainelli 			break;
2057967dd82fSFlorian Fainelli 		default:
2058967dd82fSFlorian Fainelli 			pr_err("unsupported switch detected (BCM53%02x/BCM%x)\n",
2059967dd82fSFlorian Fainelli 			       id8, id32);
2060967dd82fSFlorian Fainelli 			return -ENODEV;
2061967dd82fSFlorian Fainelli 		}
2062967dd82fSFlorian Fainelli 	}
2063967dd82fSFlorian Fainelli 
2064967dd82fSFlorian Fainelli 	if (dev->chip_id == BCM5325_DEVICE_ID)
2065967dd82fSFlorian Fainelli 		return b53_read8(dev, B53_STAT_PAGE, B53_REV_ID_25,
2066967dd82fSFlorian Fainelli 				 &dev->core_rev);
2067967dd82fSFlorian Fainelli 	else
2068967dd82fSFlorian Fainelli 		return b53_read8(dev, B53_MGMT_PAGE, B53_REV_ID,
2069967dd82fSFlorian Fainelli 				 &dev->core_rev);
2070967dd82fSFlorian Fainelli }
2071967dd82fSFlorian Fainelli EXPORT_SYMBOL(b53_switch_detect);
2072967dd82fSFlorian Fainelli 
2073967dd82fSFlorian Fainelli int b53_switch_register(struct b53_device *dev)
2074967dd82fSFlorian Fainelli {
2075967dd82fSFlorian Fainelli 	int ret;
2076967dd82fSFlorian Fainelli 
2077967dd82fSFlorian Fainelli 	if (dev->pdata) {
2078967dd82fSFlorian Fainelli 		dev->chip_id = dev->pdata->chip_id;
2079967dd82fSFlorian Fainelli 		dev->enabled_ports = dev->pdata->enabled_ports;
2080967dd82fSFlorian Fainelli 	}
2081967dd82fSFlorian Fainelli 
2082967dd82fSFlorian Fainelli 	if (!dev->chip_id && b53_switch_detect(dev))
2083967dd82fSFlorian Fainelli 		return -EINVAL;
2084967dd82fSFlorian Fainelli 
2085967dd82fSFlorian Fainelli 	ret = b53_switch_init(dev);
2086967dd82fSFlorian Fainelli 	if (ret)
2087967dd82fSFlorian Fainelli 		return ret;
2088967dd82fSFlorian Fainelli 
2089967dd82fSFlorian Fainelli 	pr_info("found switch: %s, rev %i\n", dev->name, dev->core_rev);
2090967dd82fSFlorian Fainelli 
209123c9ee49SVivien Didelot 	return dsa_register_switch(dev->ds);
2092967dd82fSFlorian Fainelli }
2093967dd82fSFlorian Fainelli EXPORT_SYMBOL(b53_switch_register);
2094967dd82fSFlorian Fainelli 
2095967dd82fSFlorian Fainelli MODULE_AUTHOR("Jonas Gorski <jogo@openwrt.org>");
2096967dd82fSFlorian Fainelli MODULE_DESCRIPTION("B53 switch library");
2097967dd82fSFlorian Fainelli MODULE_LICENSE("Dual BSD/GPL");
2098