blob: 03deacd83e613056c68f854cf886c6446da0b2bf [file] [log] [blame]
Linus Walleijd8652952018-07-14 11:45:55 +02001// SPDX-License-Identifier: GPL-2.0
2/* Realtek SMI subdriver for the Realtek RTL8366RB ethernet switch
3 *
4 * This is a sparsely documented chip, the only viable documentation seems
5 * to be a patched up code drop from the vendor that appear in various
6 * GPL source trees.
7 *
8 * Copyright (C) 2017 Linus Walleij <linus.walleij@linaro.org>
9 * Copyright (C) 2009-2010 Gabor Juhos <juhosg@openwrt.org>
10 * Copyright (C) 2010 Antti Seppälä <a.seppala@gmail.com>
11 * Copyright (C) 2010 Roman Yeryomin <roman@advem.lv>
12 * Copyright (C) 2011 Colin Leitner <colin.leitner@googlemail.com>
13 */
14
15#include <linux/bitops.h>
16#include <linux/etherdevice.h>
Linus Walleij56d8bb72021-10-05 21:47:02 +020017#include <linux/if_bridge.h>
Linus Walleijd8652952018-07-14 11:45:55 +020018#include <linux/interrupt.h>
19#include <linux/irqdomain.h>
20#include <linux/irqchip/chained_irq.h>
21#include <linux/of_irq.h>
22#include <linux/regmap.h>
23
Anders Roxellf0c03ee2019-06-13 13:35:03 +020024#include "realtek-smi-core.h"
Linus Walleijd8652952018-07-14 11:45:55 +020025
26#define RTL8366RB_PORT_NUM_CPU 5
27#define RTL8366RB_NUM_PORTS 6
28#define RTL8366RB_PHY_NO_MAX 4
29#define RTL8366RB_PHY_ADDR_MAX 31
30
31/* Switch Global Configuration register */
32#define RTL8366RB_SGCR 0x0000
33#define RTL8366RB_SGCR_EN_BC_STORM_CTRL BIT(0)
34#define RTL8366RB_SGCR_MAX_LENGTH(a) ((a) << 4)
35#define RTL8366RB_SGCR_MAX_LENGTH_MASK RTL8366RB_SGCR_MAX_LENGTH(0x3)
36#define RTL8366RB_SGCR_MAX_LENGTH_1522 RTL8366RB_SGCR_MAX_LENGTH(0x0)
37#define RTL8366RB_SGCR_MAX_LENGTH_1536 RTL8366RB_SGCR_MAX_LENGTH(0x1)
38#define RTL8366RB_SGCR_MAX_LENGTH_1552 RTL8366RB_SGCR_MAX_LENGTH(0x2)
Linus Walleij5f4a8ef2020-09-05 23:59:14 +020039#define RTL8366RB_SGCR_MAX_LENGTH_16000 RTL8366RB_SGCR_MAX_LENGTH(0x3)
Linus Walleijd8652952018-07-14 11:45:55 +020040#define RTL8366RB_SGCR_EN_VLAN BIT(13)
41#define RTL8366RB_SGCR_EN_VLAN_4KTB BIT(14)
42
43/* Port Enable Control register */
44#define RTL8366RB_PECR 0x0001
45
Linus Walleij56d8bb72021-10-05 21:47:02 +020046/* Switch per-port learning disablement register */
47#define RTL8366RB_PORT_LEARNDIS_CTRL 0x0002
48
49/* Security control, actually aging register */
50#define RTL8366RB_SECURITY_CTRL 0x0003
51
Linus Walleijd8652952018-07-14 11:45:55 +020052#define RTL8366RB_SSCR2 0x0004
53#define RTL8366RB_SSCR2_DROP_UNKNOWN_DA BIT(0)
54
Linus Walleij933de782018-08-08 14:38:55 +020055/* Port Mode Control registers */
56#define RTL8366RB_PMC0 0x0005
57#define RTL8366RB_PMC0_SPI BIT(0)
58#define RTL8366RB_PMC0_EN_AUTOLOAD BIT(1)
59#define RTL8366RB_PMC0_PROBE BIT(2)
60#define RTL8366RB_PMC0_DIS_BISR BIT(3)
61#define RTL8366RB_PMC0_ADCTEST BIT(4)
62#define RTL8366RB_PMC0_SRAM_DIAG BIT(5)
63#define RTL8366RB_PMC0_EN_SCAN BIT(6)
64#define RTL8366RB_PMC0_P4_IOMODE_SHIFT 7
65#define RTL8366RB_PMC0_P4_IOMODE_MASK GENMASK(9, 7)
66#define RTL8366RB_PMC0_P5_IOMODE_SHIFT 10
67#define RTL8366RB_PMC0_P5_IOMODE_MASK GENMASK(12, 10)
68#define RTL8366RB_PMC0_SDSMODE_SHIFT 13
69#define RTL8366RB_PMC0_SDSMODE_MASK GENMASK(15, 13)
70#define RTL8366RB_PMC1 0x0006
71
Linus Walleijd8652952018-07-14 11:45:55 +020072/* Port Mirror Control Register */
73#define RTL8366RB_PMCR 0x0007
74#define RTL8366RB_PMCR_SOURCE_PORT(a) (a)
75#define RTL8366RB_PMCR_SOURCE_PORT_MASK 0x000f
76#define RTL8366RB_PMCR_MONITOR_PORT(a) ((a) << 4)
77#define RTL8366RB_PMCR_MONITOR_PORT_MASK 0x00f0
78#define RTL8366RB_PMCR_MIRROR_RX BIT(8)
79#define RTL8366RB_PMCR_MIRROR_TX BIT(9)
80#define RTL8366RB_PMCR_MIRROR_SPC BIT(10)
81#define RTL8366RB_PMCR_MIRROR_ISO BIT(11)
82
83/* bits 0..7 = port 0, bits 8..15 = port 1 */
84#define RTL8366RB_PAACR0 0x0010
85/* bits 0..7 = port 2, bits 8..15 = port 3 */
86#define RTL8366RB_PAACR1 0x0011
87/* bits 0..7 = port 4, bits 8..15 = port 5 */
88#define RTL8366RB_PAACR2 0x0012
89#define RTL8366RB_PAACR_SPEED_10M 0
90#define RTL8366RB_PAACR_SPEED_100M 1
91#define RTL8366RB_PAACR_SPEED_1000M 2
92#define RTL8366RB_PAACR_FULL_DUPLEX BIT(2)
93#define RTL8366RB_PAACR_LINK_UP BIT(4)
94#define RTL8366RB_PAACR_TX_PAUSE BIT(5)
95#define RTL8366RB_PAACR_RX_PAUSE BIT(6)
96#define RTL8366RB_PAACR_AN BIT(7)
97
98#define RTL8366RB_PAACR_CPU_PORT (RTL8366RB_PAACR_SPEED_1000M | \
99 RTL8366RB_PAACR_FULL_DUPLEX | \
100 RTL8366RB_PAACR_LINK_UP | \
101 RTL8366RB_PAACR_TX_PAUSE | \
102 RTL8366RB_PAACR_RX_PAUSE)
103
104/* bits 0..7 = port 0, bits 8..15 = port 1 */
105#define RTL8366RB_PSTAT0 0x0014
106/* bits 0..7 = port 2, bits 8..15 = port 3 */
107#define RTL8366RB_PSTAT1 0x0015
108/* bits 0..7 = port 4, bits 8..15 = port 5 */
109#define RTL8366RB_PSTAT2 0x0016
110
111#define RTL8366RB_POWER_SAVING_REG 0x0021
112
Linus Walleije674cfd2021-10-05 21:47:04 +0200113/* Spanning tree status (STP) control, two bits per port per FID */
114#define RTL8366RB_STP_STATE_BASE 0x0050 /* 0x0050..0x0057 */
115#define RTL8366RB_STP_STATE_DISABLED 0x0
116#define RTL8366RB_STP_STATE_BLOCKING 0x1
117#define RTL8366RB_STP_STATE_LEARNING 0x2
118#define RTL8366RB_STP_STATE_FORWARDING 0x3
119#define RTL8366RB_STP_MASK GENMASK(1, 0)
120#define RTL8366RB_STP_STATE(port, state) \
121 ((state) << ((port) * 2))
122#define RTL8366RB_STP_STATE_MASK(port) \
123 RTL8366RB_STP_STATE((port), RTL8366RB_STP_MASK)
124
Linus Walleijd8652952018-07-14 11:45:55 +0200125/* CPU port control reg */
126#define RTL8368RB_CPU_CTRL_REG 0x0061
127#define RTL8368RB_CPU_PORTS_MSK 0x00FF
Linus Walleija20fafb2020-07-08 14:25:37 +0200128/* Disables inserting custom tag length/type 0x8899 */
129#define RTL8368RB_CPU_NO_TAG BIT(15)
Linus Walleijd8652952018-07-14 11:45:55 +0200130
131#define RTL8366RB_SMAR0 0x0070 /* bits 0..15 */
132#define RTL8366RB_SMAR1 0x0071 /* bits 16..31 */
133#define RTL8366RB_SMAR2 0x0072 /* bits 32..47 */
134
135#define RTL8366RB_RESET_CTRL_REG 0x0100
136#define RTL8366RB_CHIP_CTRL_RESET_HW BIT(0)
137#define RTL8366RB_CHIP_CTRL_RESET_SW BIT(1)
138
139#define RTL8366RB_CHIP_ID_REG 0x0509
140#define RTL8366RB_CHIP_ID_8366 0x5937
141#define RTL8366RB_CHIP_VERSION_CTRL_REG 0x050A
142#define RTL8366RB_CHIP_VERSION_MASK 0xf
143
144/* PHY registers control */
145#define RTL8366RB_PHY_ACCESS_CTRL_REG 0x8000
146#define RTL8366RB_PHY_CTRL_READ BIT(0)
147#define RTL8366RB_PHY_CTRL_WRITE 0
148#define RTL8366RB_PHY_ACCESS_BUSY_REG 0x8001
149#define RTL8366RB_PHY_INT_BUSY BIT(0)
150#define RTL8366RB_PHY_EXT_BUSY BIT(4)
151#define RTL8366RB_PHY_ACCESS_DATA_REG 0x8002
152#define RTL8366RB_PHY_EXT_CTRL_REG 0x8010
153#define RTL8366RB_PHY_EXT_WRDATA_REG 0x8011
154#define RTL8366RB_PHY_EXT_RDDATA_REG 0x8012
155
156#define RTL8366RB_PHY_REG_MASK 0x1f
157#define RTL8366RB_PHY_PAGE_OFFSET 5
158#define RTL8366RB_PHY_PAGE_MASK (0xf << 5)
159#define RTL8366RB_PHY_NO_OFFSET 9
160#define RTL8366RB_PHY_NO_MASK (0x1f << 9)
161
Linus Walleija4eff912021-09-26 00:59:26 +0200162/* VLAN Ingress Control Register 1, one bit per port.
163 * bit 0 .. 5 will make the switch drop ingress frames without
164 * VID such as untagged or priority-tagged frames for respective
165 * port.
166 * bit 6 .. 11 will make the switch drop ingress frames carrying
167 * a C-tag with VID != 0 for respective port.
168 */
169#define RTL8366RB_VLAN_INGRESS_CTRL1_REG 0x037E
170#define RTL8366RB_VLAN_INGRESS_CTRL1_DROP(port) (BIT((port)) | BIT((port) + 6))
171
172/* VLAN Ingress Control Register 2, one bit per port.
173 * bit0 .. bit5 will make the switch drop all ingress frames with
174 * a VLAN classification that does not include the port is in its
175 * member set.
176 */
Linus Walleijd8652952018-07-14 11:45:55 +0200177#define RTL8366RB_VLAN_INGRESS_CTRL2_REG 0x037f
178
179/* LED control registers */
180#define RTL8366RB_LED_BLINKRATE_REG 0x0430
181#define RTL8366RB_LED_BLINKRATE_MASK 0x0007
182#define RTL8366RB_LED_BLINKRATE_28MS 0x0000
183#define RTL8366RB_LED_BLINKRATE_56MS 0x0001
184#define RTL8366RB_LED_BLINKRATE_84MS 0x0002
185#define RTL8366RB_LED_BLINKRATE_111MS 0x0003
186#define RTL8366RB_LED_BLINKRATE_222MS 0x0004
187#define RTL8366RB_LED_BLINKRATE_446MS 0x0005
188
189#define RTL8366RB_LED_CTRL_REG 0x0431
190#define RTL8366RB_LED_OFF 0x0
191#define RTL8366RB_LED_DUP_COL 0x1
192#define RTL8366RB_LED_LINK_ACT 0x2
193#define RTL8366RB_LED_SPD1000 0x3
194#define RTL8366RB_LED_SPD100 0x4
195#define RTL8366RB_LED_SPD10 0x5
196#define RTL8366RB_LED_SPD1000_ACT 0x6
197#define RTL8366RB_LED_SPD100_ACT 0x7
198#define RTL8366RB_LED_SPD10_ACT 0x8
199#define RTL8366RB_LED_SPD100_10_ACT 0x9
200#define RTL8366RB_LED_FIBER 0xa
201#define RTL8366RB_LED_AN_FAULT 0xb
202#define RTL8366RB_LED_LINK_RX 0xc
203#define RTL8366RB_LED_LINK_TX 0xd
204#define RTL8366RB_LED_MASTER 0xe
205#define RTL8366RB_LED_FORCE 0xf
206#define RTL8366RB_LED_0_1_CTRL_REG 0x0432
207#define RTL8366RB_LED_1_OFFSET 6
208#define RTL8366RB_LED_2_3_CTRL_REG 0x0433
209#define RTL8366RB_LED_3_OFFSET 6
210
211#define RTL8366RB_MIB_COUNT 33
212#define RTL8366RB_GLOBAL_MIB_COUNT 1
213#define RTL8366RB_MIB_COUNTER_PORT_OFFSET 0x0050
214#define RTL8366RB_MIB_COUNTER_BASE 0x1000
215#define RTL8366RB_MIB_CTRL_REG 0x13F0
216#define RTL8366RB_MIB_CTRL_USER_MASK 0x0FFC
217#define RTL8366RB_MIB_CTRL_BUSY_MASK BIT(0)
218#define RTL8366RB_MIB_CTRL_RESET_MASK BIT(1)
219#define RTL8366RB_MIB_CTRL_PORT_RESET(_p) BIT(2 + (_p))
220#define RTL8366RB_MIB_CTRL_GLOBAL_RESET BIT(11)
221
222#define RTL8366RB_PORT_VLAN_CTRL_BASE 0x0063
223#define RTL8366RB_PORT_VLAN_CTRL_REG(_p) \
224 (RTL8366RB_PORT_VLAN_CTRL_BASE + (_p) / 4)
225#define RTL8366RB_PORT_VLAN_CTRL_MASK 0xf
226#define RTL8366RB_PORT_VLAN_CTRL_SHIFT(_p) (4 * ((_p) % 4))
227
228#define RTL8366RB_VLAN_TABLE_READ_BASE 0x018C
229#define RTL8366RB_VLAN_TABLE_WRITE_BASE 0x0185
230
231#define RTL8366RB_TABLE_ACCESS_CTRL_REG 0x0180
232#define RTL8366RB_TABLE_VLAN_READ_CTRL 0x0E01
233#define RTL8366RB_TABLE_VLAN_WRITE_CTRL 0x0F01
234
235#define RTL8366RB_VLAN_MC_BASE(_x) (0x0020 + (_x) * 3)
236
237#define RTL8366RB_PORT_LINK_STATUS_BASE 0x0014
238#define RTL8366RB_PORT_STATUS_SPEED_MASK 0x0003
239#define RTL8366RB_PORT_STATUS_DUPLEX_MASK 0x0004
240#define RTL8366RB_PORT_STATUS_LINK_MASK 0x0010
241#define RTL8366RB_PORT_STATUS_TXPAUSE_MASK 0x0020
242#define RTL8366RB_PORT_STATUS_RXPAUSE_MASK 0x0040
243#define RTL8366RB_PORT_STATUS_AN_MASK 0x0080
244
245#define RTL8366RB_NUM_VLANS 16
246#define RTL8366RB_NUM_LEDGROUPS 4
247#define RTL8366RB_NUM_VIDS 4096
248#define RTL8366RB_PRIORITYMAX 7
Linus Walleije674cfd2021-10-05 21:47:04 +0200249#define RTL8366RB_NUM_FIDS 8
Linus Walleijd8652952018-07-14 11:45:55 +0200250#define RTL8366RB_FIDMAX 7
251
252#define RTL8366RB_PORT_1 BIT(0) /* In userspace port 0 */
253#define RTL8366RB_PORT_2 BIT(1) /* In userspace port 1 */
254#define RTL8366RB_PORT_3 BIT(2) /* In userspace port 2 */
255#define RTL8366RB_PORT_4 BIT(3) /* In userspace port 3 */
256#define RTL8366RB_PORT_5 BIT(4) /* In userspace port 4 */
257
258#define RTL8366RB_PORT_CPU BIT(5) /* CPU port */
259
260#define RTL8366RB_PORT_ALL (RTL8366RB_PORT_1 | \
261 RTL8366RB_PORT_2 | \
262 RTL8366RB_PORT_3 | \
263 RTL8366RB_PORT_4 | \
264 RTL8366RB_PORT_5 | \
265 RTL8366RB_PORT_CPU)
266
267#define RTL8366RB_PORT_ALL_BUT_CPU (RTL8366RB_PORT_1 | \
268 RTL8366RB_PORT_2 | \
269 RTL8366RB_PORT_3 | \
270 RTL8366RB_PORT_4 | \
271 RTL8366RB_PORT_5)
272
273#define RTL8366RB_PORT_ALL_EXTERNAL (RTL8366RB_PORT_1 | \
274 RTL8366RB_PORT_2 | \
275 RTL8366RB_PORT_3 | \
276 RTL8366RB_PORT_4)
277
278#define RTL8366RB_PORT_ALL_INTERNAL RTL8366RB_PORT_CPU
279
280/* First configuration word per member config, VID and prio */
281#define RTL8366RB_VLAN_VID_MASK 0xfff
282#define RTL8366RB_VLAN_PRIORITY_SHIFT 12
283#define RTL8366RB_VLAN_PRIORITY_MASK 0x7
284/* Second configuration word per member config, member and untagged */
285#define RTL8366RB_VLAN_UNTAG_SHIFT 8
286#define RTL8366RB_VLAN_UNTAG_MASK 0xff
287#define RTL8366RB_VLAN_MEMBER_MASK 0xff
288/* Third config word per member config, STAG currently unused */
289#define RTL8366RB_VLAN_STAG_MBR_MASK 0xff
290#define RTL8366RB_VLAN_STAG_MBR_SHIFT 8
291#define RTL8366RB_VLAN_STAG_IDX_MASK 0x7
292#define RTL8366RB_VLAN_STAG_IDX_SHIFT 5
293#define RTL8366RB_VLAN_FID_MASK 0x7
294
295/* Port ingress bandwidth control */
296#define RTL8366RB_IB_BASE 0x0200
297#define RTL8366RB_IB_REG(pnum) (RTL8366RB_IB_BASE + (pnum))
298#define RTL8366RB_IB_BDTH_MASK 0x3fff
299#define RTL8366RB_IB_PREIFG BIT(14)
300
301/* Port egress bandwidth control */
302#define RTL8366RB_EB_BASE 0x02d1
303#define RTL8366RB_EB_REG(pnum) (RTL8366RB_EB_BASE + (pnum))
304#define RTL8366RB_EB_BDTH_MASK 0x3fff
305#define RTL8366RB_EB_PREIFG_REG 0x02f8
306#define RTL8366RB_EB_PREIFG BIT(9)
307
308#define RTL8366RB_BDTH_SW_MAX 1048512 /* 1048576? */
309#define RTL8366RB_BDTH_UNIT 64
310#define RTL8366RB_BDTH_REG_DEFAULT 16383
311
312/* QOS */
313#define RTL8366RB_QOS BIT(15)
314/* Include/Exclude Preamble and IFG (20 bytes). 0:Exclude, 1:Include. */
315#define RTL8366RB_QOS_DEFAULT_PREIFG 1
316
317/* Interrupt handling */
318#define RTL8366RB_INTERRUPT_CONTROL_REG 0x0440
319#define RTL8366RB_INTERRUPT_POLARITY BIT(0)
320#define RTL8366RB_P4_RGMII_LED BIT(2)
321#define RTL8366RB_INTERRUPT_MASK_REG 0x0441
322#define RTL8366RB_INTERRUPT_LINK_CHGALL GENMASK(11, 0)
323#define RTL8366RB_INTERRUPT_ACLEXCEED BIT(8)
324#define RTL8366RB_INTERRUPT_STORMEXCEED BIT(9)
325#define RTL8366RB_INTERRUPT_P4_FIBER BIT(12)
326#define RTL8366RB_INTERRUPT_P4_UTP BIT(13)
327#define RTL8366RB_INTERRUPT_VALID (RTL8366RB_INTERRUPT_LINK_CHGALL | \
328 RTL8366RB_INTERRUPT_ACLEXCEED | \
329 RTL8366RB_INTERRUPT_STORMEXCEED | \
330 RTL8366RB_INTERRUPT_P4_FIBER | \
331 RTL8366RB_INTERRUPT_P4_UTP)
332#define RTL8366RB_INTERRUPT_STATUS_REG 0x0442
333#define RTL8366RB_NUM_INTERRUPT 14 /* 0..13 */
334
DENG Qingfangd5a68022021-09-26 00:59:24 +0200335/* Port isolation registers */
336#define RTL8366RB_PORT_ISO_BASE 0x0F08
337#define RTL8366RB_PORT_ISO(pnum) (RTL8366RB_PORT_ISO_BASE + (pnum))
338#define RTL8366RB_PORT_ISO_EN BIT(0)
339#define RTL8366RB_PORT_ISO_PORTS_MASK GENMASK(7, 1)
340#define RTL8366RB_PORT_ISO_PORTS(pmask) ((pmask) << 1)
341
Linus Walleijd8652952018-07-14 11:45:55 +0200342/* bits 0..5 enable force when cleared */
343#define RTL8366RB_MAC_FORCE_CTRL_REG 0x0F11
344
345#define RTL8366RB_OAM_PARSER_REG 0x0F14
346#define RTL8366RB_OAM_MULTIPLEXER_REG 0x0F15
347
348#define RTL8366RB_GREEN_FEATURE_REG 0x0F51
349#define RTL8366RB_GREEN_FEATURE_MSK 0x0007
350#define RTL8366RB_GREEN_FEATURE_TX BIT(0)
351#define RTL8366RB_GREEN_FEATURE_RX BIT(2)
352
Linus Walleije0b2e0d2020-10-08 23:03:40 +0200353/**
354 * struct rtl8366rb - RTL8366RB-specific data
355 * @max_mtu: per-port max MTU setting
Linus Walleija4eff912021-09-26 00:59:26 +0200356 * @pvid_enabled: if PVID is set for respective port
Linus Walleije0b2e0d2020-10-08 23:03:40 +0200357 */
358struct rtl8366rb {
359 unsigned int max_mtu[RTL8366RB_NUM_PORTS];
Linus Walleija4eff912021-09-26 00:59:26 +0200360 bool pvid_enabled[RTL8366RB_NUM_PORTS];
Linus Walleije0b2e0d2020-10-08 23:03:40 +0200361};
362
Linus Walleijd8652952018-07-14 11:45:55 +0200363static struct rtl8366_mib_counter rtl8366rb_mib_counters[] = {
364 { 0, 0, 4, "IfInOctets" },
365 { 0, 4, 4, "EtherStatsOctets" },
366 { 0, 8, 2, "EtherStatsUnderSizePkts" },
367 { 0, 10, 2, "EtherFragments" },
368 { 0, 12, 2, "EtherStatsPkts64Octets" },
369 { 0, 14, 2, "EtherStatsPkts65to127Octets" },
370 { 0, 16, 2, "EtherStatsPkts128to255Octets" },
371 { 0, 18, 2, "EtherStatsPkts256to511Octets" },
372 { 0, 20, 2, "EtherStatsPkts512to1023Octets" },
373 { 0, 22, 2, "EtherStatsPkts1024to1518Octets" },
374 { 0, 24, 2, "EtherOversizeStats" },
375 { 0, 26, 2, "EtherStatsJabbers" },
376 { 0, 28, 2, "IfInUcastPkts" },
377 { 0, 30, 2, "EtherStatsMulticastPkts" },
378 { 0, 32, 2, "EtherStatsBroadcastPkts" },
379 { 0, 34, 2, "EtherStatsDropEvents" },
380 { 0, 36, 2, "Dot3StatsFCSErrors" },
381 { 0, 38, 2, "Dot3StatsSymbolErrors" },
382 { 0, 40, 2, "Dot3InPauseFrames" },
383 { 0, 42, 2, "Dot3ControlInUnknownOpcodes" },
384 { 0, 44, 4, "IfOutOctets" },
385 { 0, 48, 2, "Dot3StatsSingleCollisionFrames" },
386 { 0, 50, 2, "Dot3StatMultipleCollisionFrames" },
387 { 0, 52, 2, "Dot3sDeferredTransmissions" },
388 { 0, 54, 2, "Dot3StatsLateCollisions" },
389 { 0, 56, 2, "EtherStatsCollisions" },
390 { 0, 58, 2, "Dot3StatsExcessiveCollisions" },
391 { 0, 60, 2, "Dot3OutPauseFrames" },
392 { 0, 62, 2, "Dot1dBasePortDelayExceededDiscards" },
393 { 0, 64, 2, "Dot1dTpPortInDiscards" },
394 { 0, 66, 2, "IfOutUcastPkts" },
395 { 0, 68, 2, "IfOutMulticastPkts" },
396 { 0, 70, 2, "IfOutBroadcastPkts" },
397};
398
399static int rtl8366rb_get_mib_counter(struct realtek_smi *smi,
400 int port,
401 struct rtl8366_mib_counter *mib,
402 u64 *mibvalue)
403{
404 u32 addr, val;
405 int ret;
406 int i;
407
408 addr = RTL8366RB_MIB_COUNTER_BASE +
409 RTL8366RB_MIB_COUNTER_PORT_OFFSET * (port) +
410 mib->offset;
411
412 /* Writing access counter address first
413 * then ASIC will prepare 64bits counter wait for being retrived
414 */
415 ret = regmap_write(smi->map, addr, 0); /* Write whatever */
416 if (ret)
417 return ret;
418
419 /* Read MIB control register */
420 ret = regmap_read(smi->map, RTL8366RB_MIB_CTRL_REG, &val);
421 if (ret)
422 return -EIO;
423
424 if (val & RTL8366RB_MIB_CTRL_BUSY_MASK)
425 return -EBUSY;
426
427 if (val & RTL8366RB_MIB_CTRL_RESET_MASK)
428 return -EIO;
429
430 /* Read each individual MIB 16 bits at the time */
431 *mibvalue = 0;
432 for (i = mib->length; i > 0; i--) {
433 ret = regmap_read(smi->map, addr + (i - 1), &val);
434 if (ret)
435 return ret;
436 *mibvalue = (*mibvalue << 16) | (val & 0xFFFF);
437 }
438 return 0;
439}
440
441static u32 rtl8366rb_get_irqmask(struct irq_data *d)
442{
443 int line = irqd_to_hwirq(d);
444 u32 val;
445
446 /* For line interrupts we combine link down in bits
447 * 6..11 with link up in bits 0..5 into one interrupt.
448 */
449 if (line < 12)
450 val = BIT(line) | BIT(line + 6);
451 else
452 val = BIT(line);
453 return val;
454}
455
456static void rtl8366rb_mask_irq(struct irq_data *d)
457{
458 struct realtek_smi *smi = irq_data_get_irq_chip_data(d);
459 int ret;
460
461 ret = regmap_update_bits(smi->map, RTL8366RB_INTERRUPT_MASK_REG,
462 rtl8366rb_get_irqmask(d), 0);
463 if (ret)
464 dev_err(smi->dev, "could not mask IRQ\n");
465}
466
467static void rtl8366rb_unmask_irq(struct irq_data *d)
468{
469 struct realtek_smi *smi = irq_data_get_irq_chip_data(d);
470 int ret;
471
472 ret = regmap_update_bits(smi->map, RTL8366RB_INTERRUPT_MASK_REG,
473 rtl8366rb_get_irqmask(d),
474 rtl8366rb_get_irqmask(d));
475 if (ret)
476 dev_err(smi->dev, "could not unmask IRQ\n");
477}
478
479static irqreturn_t rtl8366rb_irq(int irq, void *data)
480{
481 struct realtek_smi *smi = data;
482 u32 stat;
483 int ret;
484
485 /* This clears the IRQ status register */
486 ret = regmap_read(smi->map, RTL8366RB_INTERRUPT_STATUS_REG,
487 &stat);
488 if (ret) {
489 dev_err(smi->dev, "can't read interrupt status\n");
490 return IRQ_NONE;
491 }
492 stat &= RTL8366RB_INTERRUPT_VALID;
493 if (!stat)
494 return IRQ_NONE;
495 while (stat) {
496 int line = __ffs(stat);
497 int child_irq;
498
499 stat &= ~BIT(line);
500 /* For line interrupts we combine link down in bits
501 * 6..11 with link up in bits 0..5 into one interrupt.
502 */
503 if (line < 12 && line > 5)
504 line -= 5;
505 child_irq = irq_find_mapping(smi->irqdomain, line);
506 handle_nested_irq(child_irq);
507 }
508 return IRQ_HANDLED;
509}
510
511static struct irq_chip rtl8366rb_irq_chip = {
512 .name = "RTL8366RB",
513 .irq_mask = rtl8366rb_mask_irq,
514 .irq_unmask = rtl8366rb_unmask_irq,
515};
516
517static int rtl8366rb_irq_map(struct irq_domain *domain, unsigned int irq,
518 irq_hw_number_t hwirq)
519{
520 irq_set_chip_data(irq, domain->host_data);
521 irq_set_chip_and_handler(irq, &rtl8366rb_irq_chip, handle_simple_irq);
522 irq_set_nested_thread(irq, 1);
523 irq_set_noprobe(irq);
524
525 return 0;
526}
527
528static void rtl8366rb_irq_unmap(struct irq_domain *d, unsigned int irq)
529{
530 irq_set_nested_thread(irq, 0);
531 irq_set_chip_and_handler(irq, NULL, NULL);
532 irq_set_chip_data(irq, NULL);
533}
534
535static const struct irq_domain_ops rtl8366rb_irqdomain_ops = {
536 .map = rtl8366rb_irq_map,
537 .unmap = rtl8366rb_irq_unmap,
538 .xlate = irq_domain_xlate_onecell,
539};
540
541static int rtl8366rb_setup_cascaded_irq(struct realtek_smi *smi)
542{
543 struct device_node *intc;
544 unsigned long irq_trig;
545 int irq;
546 int ret;
547 u32 val;
548 int i;
549
550 intc = of_get_child_by_name(smi->dev->of_node, "interrupt-controller");
551 if (!intc) {
552 dev_err(smi->dev, "missing child interrupt-controller node\n");
553 return -EINVAL;
554 }
555 /* RB8366RB IRQs cascade off this one */
556 irq = of_irq_get(intc, 0);
557 if (irq <= 0) {
558 dev_err(smi->dev, "failed to get parent IRQ\n");
Wen Yangf32eb9d2019-09-29 15:00:47 +0800559 ret = irq ? irq : -EINVAL;
560 goto out_put_node;
Linus Walleijd8652952018-07-14 11:45:55 +0200561 }
562
563 /* This clears the IRQ status register */
564 ret = regmap_read(smi->map, RTL8366RB_INTERRUPT_STATUS_REG,
565 &val);
566 if (ret) {
567 dev_err(smi->dev, "can't read interrupt status\n");
Wen Yangf32eb9d2019-09-29 15:00:47 +0800568 goto out_put_node;
Linus Walleijd8652952018-07-14 11:45:55 +0200569 }
570
571 /* Fetch IRQ edge information from the descriptor */
572 irq_trig = irqd_get_trigger_type(irq_get_irq_data(irq));
573 switch (irq_trig) {
574 case IRQF_TRIGGER_RISING:
575 case IRQF_TRIGGER_HIGH:
576 dev_info(smi->dev, "active high/rising IRQ\n");
577 val = 0;
578 break;
579 case IRQF_TRIGGER_FALLING:
580 case IRQF_TRIGGER_LOW:
581 dev_info(smi->dev, "active low/falling IRQ\n");
582 val = RTL8366RB_INTERRUPT_POLARITY;
583 break;
584 }
585 ret = regmap_update_bits(smi->map, RTL8366RB_INTERRUPT_CONTROL_REG,
586 RTL8366RB_INTERRUPT_POLARITY,
587 val);
588 if (ret) {
589 dev_err(smi->dev, "could not configure IRQ polarity\n");
Wen Yangf32eb9d2019-09-29 15:00:47 +0800590 goto out_put_node;
Linus Walleijd8652952018-07-14 11:45:55 +0200591 }
592
593 ret = devm_request_threaded_irq(smi->dev, irq, NULL,
594 rtl8366rb_irq, IRQF_ONESHOT,
595 "RTL8366RB", smi);
596 if (ret) {
597 dev_err(smi->dev, "unable to request irq: %d\n", ret);
Wen Yangf32eb9d2019-09-29 15:00:47 +0800598 goto out_put_node;
Linus Walleijd8652952018-07-14 11:45:55 +0200599 }
600 smi->irqdomain = irq_domain_add_linear(intc,
601 RTL8366RB_NUM_INTERRUPT,
602 &rtl8366rb_irqdomain_ops,
603 smi);
604 if (!smi->irqdomain) {
605 dev_err(smi->dev, "failed to create IRQ domain\n");
Wen Yangf32eb9d2019-09-29 15:00:47 +0800606 ret = -EINVAL;
607 goto out_put_node;
Linus Walleijd8652952018-07-14 11:45:55 +0200608 }
609 for (i = 0; i < smi->num_ports; i++)
610 irq_set_parent(irq_create_mapping(smi->irqdomain, i), irq);
611
Wen Yangf32eb9d2019-09-29 15:00:47 +0800612out_put_node:
613 of_node_put(intc);
614 return ret;
Linus Walleijd8652952018-07-14 11:45:55 +0200615}
616
617static int rtl8366rb_set_addr(struct realtek_smi *smi)
618{
619 u8 addr[ETH_ALEN];
620 u16 val;
621 int ret;
622
623 eth_random_addr(addr);
624
625 dev_info(smi->dev, "set MAC: %02X:%02X:%02X:%02X:%02X:%02X\n",
626 addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
627 val = addr[0] << 8 | addr[1];
628 ret = regmap_write(smi->map, RTL8366RB_SMAR0, val);
629 if (ret)
630 return ret;
631 val = addr[2] << 8 | addr[3];
632 ret = regmap_write(smi->map, RTL8366RB_SMAR1, val);
633 if (ret)
634 return ret;
635 val = addr[4] << 8 | addr[5];
636 ret = regmap_write(smi->map, RTL8366RB_SMAR2, val);
637 if (ret)
638 return ret;
639
640 return 0;
641}
642
643/* Found in a vendor driver */
644
Lorenzo Carlettid1f3bdd2021-01-27 02:06:32 +0100645/* Struct for handling the jam tables' entries */
646struct rtl8366rb_jam_tbl_entry {
647 u16 reg;
648 u16 val;
649};
650
Linus Walleijd8652952018-07-14 11:45:55 +0200651/* For the "version 0" early silicon, appear in most source releases */
Lorenzo Carlettid1f3bdd2021-01-27 02:06:32 +0100652static const struct rtl8366rb_jam_tbl_entry rtl8366rb_init_jam_ver_0[] = {
653 {0x000B, 0x0001}, {0x03A6, 0x0100}, {0x03A7, 0x0001}, {0x02D1, 0x3FFF},
654 {0x02D2, 0x3FFF}, {0x02D3, 0x3FFF}, {0x02D4, 0x3FFF}, {0x02D5, 0x3FFF},
655 {0x02D6, 0x3FFF}, {0x02D7, 0x3FFF}, {0x02D8, 0x3FFF}, {0x022B, 0x0688},
656 {0x022C, 0x0FAC}, {0x03D0, 0x4688}, {0x03D1, 0x01F5}, {0x0000, 0x0830},
657 {0x02F9, 0x0200}, {0x02F7, 0x7FFF}, {0x02F8, 0x03FF}, {0x0080, 0x03E8},
658 {0x0081, 0x00CE}, {0x0082, 0x00DA}, {0x0083, 0x0230}, {0xBE0F, 0x2000},
659 {0x0231, 0x422A}, {0x0232, 0x422A}, {0x0233, 0x422A}, {0x0234, 0x422A},
660 {0x0235, 0x422A}, {0x0236, 0x422A}, {0x0237, 0x422A}, {0x0238, 0x422A},
661 {0x0239, 0x422A}, {0x023A, 0x422A}, {0x023B, 0x422A}, {0x023C, 0x422A},
662 {0x023D, 0x422A}, {0x023E, 0x422A}, {0x023F, 0x422A}, {0x0240, 0x422A},
663 {0x0241, 0x422A}, {0x0242, 0x422A}, {0x0243, 0x422A}, {0x0244, 0x422A},
664 {0x0245, 0x422A}, {0x0246, 0x422A}, {0x0247, 0x422A}, {0x0248, 0x422A},
665 {0x0249, 0x0146}, {0x024A, 0x0146}, {0x024B, 0x0146}, {0xBE03, 0xC961},
666 {0x024D, 0x0146}, {0x024E, 0x0146}, {0x024F, 0x0146}, {0x0250, 0x0146},
667 {0xBE64, 0x0226}, {0x0252, 0x0146}, {0x0253, 0x0146}, {0x024C, 0x0146},
668 {0x0251, 0x0146}, {0x0254, 0x0146}, {0xBE62, 0x3FD0}, {0x0084, 0x0320},
669 {0x0255, 0x0146}, {0x0256, 0x0146}, {0x0257, 0x0146}, {0x0258, 0x0146},
670 {0x0259, 0x0146}, {0x025A, 0x0146}, {0x025B, 0x0146}, {0x025C, 0x0146},
671 {0x025D, 0x0146}, {0x025E, 0x0146}, {0x025F, 0x0146}, {0x0260, 0x0146},
672 {0x0261, 0xA23F}, {0x0262, 0x0294}, {0x0263, 0xA23F}, {0x0264, 0x0294},
673 {0x0265, 0xA23F}, {0x0266, 0x0294}, {0x0267, 0xA23F}, {0x0268, 0x0294},
674 {0x0269, 0xA23F}, {0x026A, 0x0294}, {0x026B, 0xA23F}, {0x026C, 0x0294},
675 {0x026D, 0xA23F}, {0x026E, 0x0294}, {0x026F, 0xA23F}, {0x0270, 0x0294},
676 {0x02F5, 0x0048}, {0xBE09, 0x0E00}, {0xBE1E, 0x0FA0}, {0xBE14, 0x8448},
677 {0xBE15, 0x1007}, {0xBE4A, 0xA284}, {0xC454, 0x3F0B}, {0xC474, 0x3F0B},
678 {0xBE48, 0x3672}, {0xBE4B, 0x17A7}, {0xBE4C, 0x0B15}, {0xBE52, 0x0EDD},
679 {0xBE49, 0x8C00}, {0xBE5B, 0x785C}, {0xBE5C, 0x785C}, {0xBE5D, 0x785C},
680 {0xBE61, 0x368A}, {0xBE63, 0x9B84}, {0xC456, 0xCC13}, {0xC476, 0xCC13},
681 {0xBE65, 0x307D}, {0xBE6D, 0x0005}, {0xBE6E, 0xE120}, {0xBE2E, 0x7BAF},
Linus Walleijd8652952018-07-14 11:45:55 +0200682};
683
684/* This v1 init sequence is from Belkin F5D8235 U-Boot release */
Lorenzo Carlettid1f3bdd2021-01-27 02:06:32 +0100685static const struct rtl8366rb_jam_tbl_entry rtl8366rb_init_jam_ver_1[] = {
686 {0x0000, 0x0830}, {0x0001, 0x8000}, {0x0400, 0x8130}, {0xBE78, 0x3C3C},
687 {0x0431, 0x5432}, {0xBE37, 0x0CE4}, {0x02FA, 0xFFDF}, {0x02FB, 0xFFE0},
688 {0xC44C, 0x1585}, {0xC44C, 0x1185}, {0xC44C, 0x1585}, {0xC46C, 0x1585},
689 {0xC46C, 0x1185}, {0xC46C, 0x1585}, {0xC451, 0x2135}, {0xC471, 0x2135},
690 {0xBE10, 0x8140}, {0xBE15, 0x0007}, {0xBE6E, 0xE120}, {0xBE69, 0xD20F},
691 {0xBE6B, 0x0320}, {0xBE24, 0xB000}, {0xBE23, 0xFF51}, {0xBE22, 0xDF20},
692 {0xBE21, 0x0140}, {0xBE20, 0x00BB}, {0xBE24, 0xB800}, {0xBE24, 0x0000},
693 {0xBE24, 0x7000}, {0xBE23, 0xFF51}, {0xBE22, 0xDF60}, {0xBE21, 0x0140},
694 {0xBE20, 0x0077}, {0xBE24, 0x7800}, {0xBE24, 0x0000}, {0xBE2E, 0x7B7A},
695 {0xBE36, 0x0CE4}, {0x02F5, 0x0048}, {0xBE77, 0x2940}, {0x000A, 0x83E0},
696 {0xBE79, 0x3C3C}, {0xBE00, 0x1340},
Linus Walleijd8652952018-07-14 11:45:55 +0200697};
698
699/* This v2 init sequence is from Belkin F5D8235 U-Boot release */
Lorenzo Carlettid1f3bdd2021-01-27 02:06:32 +0100700static const struct rtl8366rb_jam_tbl_entry rtl8366rb_init_jam_ver_2[] = {
701 {0x0450, 0x0000}, {0x0400, 0x8130}, {0x000A, 0x83ED}, {0x0431, 0x5432},
702 {0xC44F, 0x6250}, {0xC46F, 0x6250}, {0xC456, 0x0C14}, {0xC476, 0x0C14},
703 {0xC44C, 0x1C85}, {0xC44C, 0x1885}, {0xC44C, 0x1C85}, {0xC46C, 0x1C85},
704 {0xC46C, 0x1885}, {0xC46C, 0x1C85}, {0xC44C, 0x0885}, {0xC44C, 0x0881},
705 {0xC44C, 0x0885}, {0xC46C, 0x0885}, {0xC46C, 0x0881}, {0xC46C, 0x0885},
706 {0xBE2E, 0x7BA7}, {0xBE36, 0x1000}, {0xBE37, 0x1000}, {0x8000, 0x0001},
707 {0xBE69, 0xD50F}, {0x8000, 0x0000}, {0xBE69, 0xD50F}, {0xBE6E, 0x0320},
708 {0xBE77, 0x2940}, {0xBE78, 0x3C3C}, {0xBE79, 0x3C3C}, {0xBE6E, 0xE120},
709 {0x8000, 0x0001}, {0xBE15, 0x1007}, {0x8000, 0x0000}, {0xBE15, 0x1007},
710 {0xBE14, 0x0448}, {0xBE1E, 0x00A0}, {0xBE10, 0x8160}, {0xBE10, 0x8140},
711 {0xBE00, 0x1340}, {0x0F51, 0x0010},
Linus Walleijd8652952018-07-14 11:45:55 +0200712};
713
714/* Appears in a DDWRT code dump */
Lorenzo Carlettid1f3bdd2021-01-27 02:06:32 +0100715static const struct rtl8366rb_jam_tbl_entry rtl8366rb_init_jam_ver_3[] = {
716 {0x0000, 0x0830}, {0x0400, 0x8130}, {0x000A, 0x83ED}, {0x0431, 0x5432},
717 {0x0F51, 0x0017}, {0x02F5, 0x0048}, {0x02FA, 0xFFDF}, {0x02FB, 0xFFE0},
718 {0xC456, 0x0C14}, {0xC476, 0x0C14}, {0xC454, 0x3F8B}, {0xC474, 0x3F8B},
719 {0xC450, 0x2071}, {0xC470, 0x2071}, {0xC451, 0x226B}, {0xC471, 0x226B},
720 {0xC452, 0xA293}, {0xC472, 0xA293}, {0xC44C, 0x1585}, {0xC44C, 0x1185},
721 {0xC44C, 0x1585}, {0xC46C, 0x1585}, {0xC46C, 0x1185}, {0xC46C, 0x1585},
722 {0xC44C, 0x0185}, {0xC44C, 0x0181}, {0xC44C, 0x0185}, {0xC46C, 0x0185},
723 {0xC46C, 0x0181}, {0xC46C, 0x0185}, {0xBE24, 0xB000}, {0xBE23, 0xFF51},
724 {0xBE22, 0xDF20}, {0xBE21, 0x0140}, {0xBE20, 0x00BB}, {0xBE24, 0xB800},
725 {0xBE24, 0x0000}, {0xBE24, 0x7000}, {0xBE23, 0xFF51}, {0xBE22, 0xDF60},
726 {0xBE21, 0x0140}, {0xBE20, 0x0077}, {0xBE24, 0x7800}, {0xBE24, 0x0000},
727 {0xBE2E, 0x7BA7}, {0xBE36, 0x1000}, {0xBE37, 0x1000}, {0x8000, 0x0001},
728 {0xBE69, 0xD50F}, {0x8000, 0x0000}, {0xBE69, 0xD50F}, {0xBE6B, 0x0320},
729 {0xBE77, 0x2800}, {0xBE78, 0x3C3C}, {0xBE79, 0x3C3C}, {0xBE6E, 0xE120},
730 {0x8000, 0x0001}, {0xBE10, 0x8140}, {0x8000, 0x0000}, {0xBE10, 0x8140},
731 {0xBE15, 0x1007}, {0xBE14, 0x0448}, {0xBE1E, 0x00A0}, {0xBE10, 0x8160},
732 {0xBE10, 0x8140}, {0xBE00, 0x1340}, {0x0450, 0x0000}, {0x0401, 0x0000},
Linus Walleijd8652952018-07-14 11:45:55 +0200733};
734
735/* Belkin F5D8235 v1, "belkin,f5d8235-v1" */
Lorenzo Carlettid1f3bdd2021-01-27 02:06:32 +0100736static const struct rtl8366rb_jam_tbl_entry rtl8366rb_init_jam_f5d8235[] = {
737 {0x0242, 0x02BF}, {0x0245, 0x02BF}, {0x0248, 0x02BF}, {0x024B, 0x02BF},
738 {0x024E, 0x02BF}, {0x0251, 0x02BF}, {0x0254, 0x0A3F}, {0x0256, 0x0A3F},
739 {0x0258, 0x0A3F}, {0x025A, 0x0A3F}, {0x025C, 0x0A3F}, {0x025E, 0x0A3F},
740 {0x0263, 0x007C}, {0x0100, 0x0004}, {0xBE5B, 0x3500}, {0x800E, 0x200F},
741 {0xBE1D, 0x0F00}, {0x8001, 0x5011}, {0x800A, 0xA2F4}, {0x800B, 0x17A3},
742 {0xBE4B, 0x17A3}, {0xBE41, 0x5011}, {0xBE17, 0x2100}, {0x8000, 0x8304},
743 {0xBE40, 0x8304}, {0xBE4A, 0xA2F4}, {0x800C, 0xA8D5}, {0x8014, 0x5500},
744 {0x8015, 0x0004}, {0xBE4C, 0xA8D5}, {0xBE59, 0x0008}, {0xBE09, 0x0E00},
745 {0xBE36, 0x1036}, {0xBE37, 0x1036}, {0x800D, 0x00FF}, {0xBE4D, 0x00FF},
Linus Walleijd8652952018-07-14 11:45:55 +0200746};
747
748/* DGN3500, "netgear,dgn3500", "netgear,dgn3500b" */
Lorenzo Carlettid1f3bdd2021-01-27 02:06:32 +0100749static const struct rtl8366rb_jam_tbl_entry rtl8366rb_init_jam_dgn3500[] = {
750 {0x0000, 0x0830}, {0x0400, 0x8130}, {0x000A, 0x83ED}, {0x0F51, 0x0017},
751 {0x02F5, 0x0048}, {0x02FA, 0xFFDF}, {0x02FB, 0xFFE0}, {0x0450, 0x0000},
752 {0x0401, 0x0000}, {0x0431, 0x0960},
Linus Walleijd8652952018-07-14 11:45:55 +0200753};
754
755/* This jam table activates "green ethernet", which means low power mode
756 * and is claimed to detect the cable length and not use more power than
757 * necessary, and the ports should enter power saving mode 10 seconds after
758 * a cable is disconnected. Seems to always be the same.
759 */
Lorenzo Carlettid1f3bdd2021-01-27 02:06:32 +0100760static const struct rtl8366rb_jam_tbl_entry rtl8366rb_green_jam[] = {
Linus Walleijd8652952018-07-14 11:45:55 +0200761 {0xBE78, 0x323C}, {0xBE77, 0x5000}, {0xBE2E, 0x7BA7},
762 {0xBE59, 0x3459}, {0xBE5A, 0x745A}, {0xBE5B, 0x785C},
763 {0xBE5C, 0x785C}, {0xBE6E, 0xE120}, {0xBE79, 0x323C},
764};
765
Lorenzo Carlettid1f3bdd2021-01-27 02:06:32 +0100766/* Function that jams the tables in the proper registers */
767static int rtl8366rb_jam_table(const struct rtl8366rb_jam_tbl_entry *jam_table,
768 int jam_size, struct realtek_smi *smi,
769 bool write_dbg)
770{
771 u32 val;
772 int ret;
773 int i;
774
775 for (i = 0; i < jam_size; i++) {
776 if ((jam_table[i].reg & 0xBE00) == 0xBE00) {
777 ret = regmap_read(smi->map,
778 RTL8366RB_PHY_ACCESS_BUSY_REG,
779 &val);
780 if (ret)
781 return ret;
782 if (!(val & RTL8366RB_PHY_INT_BUSY)) {
783 ret = regmap_write(smi->map,
784 RTL8366RB_PHY_ACCESS_CTRL_REG,
785 RTL8366RB_PHY_CTRL_WRITE);
786 if (ret)
787 return ret;
788 }
789 }
790 if (write_dbg)
791 dev_dbg(smi->dev, "jam %04x into register %04x\n",
792 jam_table[i].val,
793 jam_table[i].reg);
794 ret = regmap_write(smi->map,
795 jam_table[i].reg,
796 jam_table[i].val);
797 if (ret)
798 return ret;
799 }
800 return 0;
801}
802
Linus Walleijd8652952018-07-14 11:45:55 +0200803static int rtl8366rb_setup(struct dsa_switch *ds)
804{
805 struct realtek_smi *smi = ds->priv;
Lorenzo Carlettid1f3bdd2021-01-27 02:06:32 +0100806 const struct rtl8366rb_jam_tbl_entry *jam_table;
Linus Walleije0b2e0d2020-10-08 23:03:40 +0200807 struct rtl8366rb *rb;
Linus Walleijd8652952018-07-14 11:45:55 +0200808 u32 chip_ver = 0;
809 u32 chip_id = 0;
810 int jam_size;
811 u32 val;
812 int ret;
813 int i;
814
Linus Walleije0b2e0d2020-10-08 23:03:40 +0200815 rb = smi->chip_data;
816
Linus Walleijd8652952018-07-14 11:45:55 +0200817 ret = regmap_read(smi->map, RTL8366RB_CHIP_ID_REG, &chip_id);
818 if (ret) {
819 dev_err(smi->dev, "unable to read chip id\n");
820 return ret;
821 }
822
823 switch (chip_id) {
824 case RTL8366RB_CHIP_ID_8366:
825 break;
826 default:
827 dev_err(smi->dev, "unknown chip id (%04x)\n", chip_id);
828 return -ENODEV;
829 }
830
831 ret = regmap_read(smi->map, RTL8366RB_CHIP_VERSION_CTRL_REG,
832 &chip_ver);
833 if (ret) {
834 dev_err(smi->dev, "unable to read chip version\n");
835 return ret;
836 }
837
838 dev_info(smi->dev, "RTL%04x ver %u chip found\n",
839 chip_id, chip_ver & RTL8366RB_CHIP_VERSION_MASK);
840
841 /* Do the init dance using the right jam table */
842 switch (chip_ver) {
843 case 0:
844 jam_table = rtl8366rb_init_jam_ver_0;
845 jam_size = ARRAY_SIZE(rtl8366rb_init_jam_ver_0);
846 break;
847 case 1:
848 jam_table = rtl8366rb_init_jam_ver_1;
849 jam_size = ARRAY_SIZE(rtl8366rb_init_jam_ver_1);
850 break;
851 case 2:
852 jam_table = rtl8366rb_init_jam_ver_2;
853 jam_size = ARRAY_SIZE(rtl8366rb_init_jam_ver_2);
854 break;
855 default:
856 jam_table = rtl8366rb_init_jam_ver_3;
857 jam_size = ARRAY_SIZE(rtl8366rb_init_jam_ver_3);
858 break;
859 }
860
861 /* Special jam tables for special routers
862 * TODO: are these necessary? Maintainers, please test
863 * without them, using just the off-the-shelf tables.
864 */
865 if (of_machine_is_compatible("belkin,f5d8235-v1")) {
866 jam_table = rtl8366rb_init_jam_f5d8235;
867 jam_size = ARRAY_SIZE(rtl8366rb_init_jam_f5d8235);
868 }
869 if (of_machine_is_compatible("netgear,dgn3500") ||
870 of_machine_is_compatible("netgear,dgn3500b")) {
871 jam_table = rtl8366rb_init_jam_dgn3500;
872 jam_size = ARRAY_SIZE(rtl8366rb_init_jam_dgn3500);
873 }
874
Lorenzo Carlettid1f3bdd2021-01-27 02:06:32 +0100875 ret = rtl8366rb_jam_table(jam_table, jam_size, smi, true);
876 if (ret)
877 return ret;
Linus Walleijd8652952018-07-14 11:45:55 +0200878
DENG Qingfangd5a68022021-09-26 00:59:24 +0200879 /* Isolate all user ports so they can only send packets to itself and the CPU port */
880 for (i = 0; i < RTL8366RB_PORT_NUM_CPU; i++) {
881 ret = regmap_write(smi->map, RTL8366RB_PORT_ISO(i),
882 RTL8366RB_PORT_ISO_PORTS(BIT(RTL8366RB_PORT_NUM_CPU)) |
883 RTL8366RB_PORT_ISO_EN);
884 if (ret)
885 return ret;
886 }
887 /* CPU port can send packets to all ports */
888 ret = regmap_write(smi->map, RTL8366RB_PORT_ISO(RTL8366RB_PORT_NUM_CPU),
889 RTL8366RB_PORT_ISO_PORTS(dsa_user_ports(ds)) |
890 RTL8366RB_PORT_ISO_EN);
891 if (ret)
892 return ret;
893
Linus Walleijd8652952018-07-14 11:45:55 +0200894 /* Set up the "green ethernet" feature */
Lorenzo Carlettid1f3bdd2021-01-27 02:06:32 +0100895 ret = rtl8366rb_jam_table(rtl8366rb_green_jam,
896 ARRAY_SIZE(rtl8366rb_green_jam), smi, false);
897 if (ret)
898 return ret;
899
Linus Walleijd8652952018-07-14 11:45:55 +0200900 ret = regmap_write(smi->map,
901 RTL8366RB_GREEN_FEATURE_REG,
902 (chip_ver == 1) ? 0x0007 : 0x0003);
903 if (ret)
904 return ret;
905
906 /* Vendor driver sets 0x240 in registers 0xc and 0xd (undocumented) */
907 ret = regmap_write(smi->map, 0x0c, 0x240);
908 if (ret)
909 return ret;
910 ret = regmap_write(smi->map, 0x0d, 0x240);
911 if (ret)
912 return ret;
913
914 /* Set some random MAC address */
915 ret = rtl8366rb_set_addr(smi);
916 if (ret)
917 return ret;
918
Linus Walleija20fafb2020-07-08 14:25:37 +0200919 /* Enable CPU port with custom DSA tag 8899.
Linus Walleijd8652952018-07-14 11:45:55 +0200920 *
Linus Walleija20fafb2020-07-08 14:25:37 +0200921 * If you set RTL8368RB_CPU_NO_TAG (bit 15) in this registers
922 * the custom tag is turned off.
Linus Walleijd8652952018-07-14 11:45:55 +0200923 */
924 ret = regmap_update_bits(smi->map, RTL8368RB_CPU_CTRL_REG,
925 0xFFFF,
Linus Walleija20fafb2020-07-08 14:25:37 +0200926 BIT(smi->cpu_port));
Linus Walleijd8652952018-07-14 11:45:55 +0200927 if (ret)
928 return ret;
929
930 /* Make sure we default-enable the fixed CPU port */
931 ret = regmap_update_bits(smi->map, RTL8366RB_PECR,
932 BIT(smi->cpu_port),
933 0);
934 if (ret)
935 return ret;
936
937 /* Set maximum packet length to 1536 bytes */
938 ret = regmap_update_bits(smi->map, RTL8366RB_SGCR,
939 RTL8366RB_SGCR_MAX_LENGTH_MASK,
940 RTL8366RB_SGCR_MAX_LENGTH_1536);
941 if (ret)
942 return ret;
Linus Walleije0b2e0d2020-10-08 23:03:40 +0200943 for (i = 0; i < RTL8366RB_NUM_PORTS; i++)
944 /* layer 2 size, see rtl8366rb_change_mtu() */
945 rb->max_mtu[i] = 1532;
Linus Walleijd8652952018-07-14 11:45:55 +0200946
Linus Walleij56d8bb72021-10-05 21:47:02 +0200947 /* Disable learning for all ports */
948 ret = regmap_write(smi->map, RTL8366RB_PORT_LEARNDIS_CTRL,
949 RTL8366RB_PORT_ALL);
Linus Walleijd8652952018-07-14 11:45:55 +0200950 if (ret)
951 return ret;
952
953 /* Enable auto ageing for all ports */
Linus Walleij56d8bb72021-10-05 21:47:02 +0200954 ret = regmap_write(smi->map, RTL8366RB_SECURITY_CTRL, 0);
Linus Walleijd8652952018-07-14 11:45:55 +0200955 if (ret)
956 return ret;
957
Linus Walleij933de782018-08-08 14:38:55 +0200958 /* Port 4 setup: this enables Port 4, usually the WAN port,
959 * common PHY IO mode is apparently mode 0, and this is not what
960 * the port is initialized to. There is no explanation of the
961 * IO modes in the Realtek source code, if your WAN port is
962 * connected to something exotic such as fiber, then this might
963 * be worth experimenting with.
964 */
965 ret = regmap_update_bits(smi->map, RTL8366RB_PMC0,
966 RTL8366RB_PMC0_P4_IOMODE_MASK,
967 0 << RTL8366RB_PMC0_P4_IOMODE_SHIFT);
968 if (ret)
969 return ret;
970
Linus Walleija4eff912021-09-26 00:59:26 +0200971 /* Accept all packets by default, we enable filtering on-demand */
972 ret = regmap_write(smi->map, RTL8366RB_VLAN_INGRESS_CTRL1_REG,
973 0);
974 if (ret)
975 return ret;
Linus Walleijd8652952018-07-14 11:45:55 +0200976 ret = regmap_write(smi->map, RTL8366RB_VLAN_INGRESS_CTRL2_REG,
Linus Walleija4eff912021-09-26 00:59:26 +0200977 0);
Linus Walleijd8652952018-07-14 11:45:55 +0200978 if (ret)
979 return ret;
980
981 /* Don't drop packets whose DA has not been learned */
982 ret = regmap_update_bits(smi->map, RTL8366RB_SSCR2,
983 RTL8366RB_SSCR2_DROP_UNKNOWN_DA, 0);
984 if (ret)
985 return ret;
986
987 /* Set blinking, TODO: make this configurable */
988 ret = regmap_update_bits(smi->map, RTL8366RB_LED_BLINKRATE_REG,
989 RTL8366RB_LED_BLINKRATE_MASK,
990 RTL8366RB_LED_BLINKRATE_56MS);
991 if (ret)
992 return ret;
993
994 /* Set up LED activity:
995 * Each port has 4 LEDs, we configure all ports to the same
996 * behaviour (no individual config) but we can set up each
997 * LED separately.
998 */
999 if (smi->leds_disabled) {
1000 /* Turn everything off */
1001 regmap_update_bits(smi->map,
1002 RTL8366RB_LED_0_1_CTRL_REG,
1003 0x0FFF, 0);
1004 regmap_update_bits(smi->map,
1005 RTL8366RB_LED_2_3_CTRL_REG,
1006 0x0FFF, 0);
1007 regmap_update_bits(smi->map,
1008 RTL8366RB_INTERRUPT_CONTROL_REG,
1009 RTL8366RB_P4_RGMII_LED,
1010 0);
1011 val = RTL8366RB_LED_OFF;
1012 } else {
1013 /* TODO: make this configurable per LED */
1014 val = RTL8366RB_LED_FORCE;
1015 }
1016 for (i = 0; i < 4; i++) {
1017 ret = regmap_update_bits(smi->map,
1018 RTL8366RB_LED_CTRL_REG,
1019 0xf << (i * 4),
1020 val << (i * 4));
1021 if (ret)
1022 return ret;
1023 }
1024
Linus Walleij7776e332021-09-26 00:59:25 +02001025 ret = rtl8366_reset_vlan(smi);
Linus Walleijd8652952018-07-14 11:45:55 +02001026 if (ret)
1027 return ret;
1028
1029 ret = rtl8366rb_setup_cascaded_irq(smi);
1030 if (ret)
1031 dev_info(smi->dev, "no interrupt support\n");
1032
1033 ret = realtek_smi_setup_mdio(smi);
1034 if (ret) {
1035 dev_info(smi->dev, "could not set up MDIO bus\n");
1036 return -ENODEV;
1037 }
1038
1039 return 0;
1040}
1041
1042static enum dsa_tag_protocol rtl8366_get_tag_protocol(struct dsa_switch *ds,
Florian Fainelli4d776482020-01-07 21:06:05 -08001043 int port,
1044 enum dsa_tag_protocol mp)
Linus Walleijd8652952018-07-14 11:45:55 +02001045{
Linus Walleija20fafb2020-07-08 14:25:37 +02001046 /* This switch uses the 4 byte protocol A Realtek DSA tag */
1047 return DSA_TAG_PROTO_RTL4_A;
Linus Walleijd8652952018-07-14 11:45:55 +02001048}
1049
Linus Walleijbb1416a2020-09-06 23:24:15 +02001050static void
1051rtl8366rb_mac_link_up(struct dsa_switch *ds, int port, unsigned int mode,
1052 phy_interface_t interface, struct phy_device *phydev,
1053 int speed, int duplex, bool tx_pause, bool rx_pause)
Linus Walleijd8652952018-07-14 11:45:55 +02001054{
1055 struct realtek_smi *smi = ds->priv;
1056 int ret;
1057
1058 if (port != smi->cpu_port)
1059 return;
1060
Linus Walleijbb1416a2020-09-06 23:24:15 +02001061 dev_dbg(smi->dev, "MAC link up on CPU port (%d)\n", port);
Linus Walleijd8652952018-07-14 11:45:55 +02001062
1063 /* Force the fixed CPU port into 1Gbit mode, no autonegotiation */
1064 ret = regmap_update_bits(smi->map, RTL8366RB_MAC_FORCE_CTRL_REG,
1065 BIT(port), BIT(port));
Linus Walleijbb1416a2020-09-06 23:24:15 +02001066 if (ret) {
1067 dev_err(smi->dev, "failed to force 1Gbit on CPU port\n");
Linus Walleijd8652952018-07-14 11:45:55 +02001068 return;
Linus Walleijbb1416a2020-09-06 23:24:15 +02001069 }
Linus Walleijd8652952018-07-14 11:45:55 +02001070
1071 ret = regmap_update_bits(smi->map, RTL8366RB_PAACR2,
1072 0xFF00U,
1073 RTL8366RB_PAACR_CPU_PORT << 8);
Linus Walleijbb1416a2020-09-06 23:24:15 +02001074 if (ret) {
1075 dev_err(smi->dev, "failed to set PAACR on CPU port\n");
Linus Walleijd8652952018-07-14 11:45:55 +02001076 return;
Linus Walleijbb1416a2020-09-06 23:24:15 +02001077 }
Linus Walleijd8652952018-07-14 11:45:55 +02001078
1079 /* Enable the CPU port */
1080 ret = regmap_update_bits(smi->map, RTL8366RB_PECR, BIT(port),
1081 0);
Linus Walleijbb1416a2020-09-06 23:24:15 +02001082 if (ret) {
1083 dev_err(smi->dev, "failed to enable the CPU port\n");
Linus Walleijd8652952018-07-14 11:45:55 +02001084 return;
Linus Walleijbb1416a2020-09-06 23:24:15 +02001085 }
1086}
1087
1088static void
1089rtl8366rb_mac_link_down(struct dsa_switch *ds, int port, unsigned int mode,
1090 phy_interface_t interface)
1091{
1092 struct realtek_smi *smi = ds->priv;
1093 int ret;
1094
1095 if (port != smi->cpu_port)
1096 return;
1097
1098 dev_dbg(smi->dev, "MAC link down on CPU port (%d)\n", port);
1099
1100 /* Disable the CPU port */
1101 ret = regmap_update_bits(smi->map, RTL8366RB_PECR, BIT(port),
1102 BIT(port));
1103 if (ret) {
1104 dev_err(smi->dev, "failed to disable the CPU port\n");
1105 return;
1106 }
Linus Walleijd8652952018-07-14 11:45:55 +02001107}
1108
1109static void rb8366rb_set_port_led(struct realtek_smi *smi,
1110 int port, bool enable)
1111{
1112 u16 val = enable ? 0x3f : 0;
1113 int ret;
1114
1115 if (smi->leds_disabled)
1116 return;
1117
1118 switch (port) {
1119 case 0:
1120 ret = regmap_update_bits(smi->map,
1121 RTL8366RB_LED_0_1_CTRL_REG,
1122 0x3F, val);
1123 break;
1124 case 1:
1125 ret = regmap_update_bits(smi->map,
1126 RTL8366RB_LED_0_1_CTRL_REG,
1127 0x3F << RTL8366RB_LED_1_OFFSET,
1128 val << RTL8366RB_LED_1_OFFSET);
1129 break;
1130 case 2:
1131 ret = regmap_update_bits(smi->map,
1132 RTL8366RB_LED_2_3_CTRL_REG,
1133 0x3F, val);
1134 break;
1135 case 3:
1136 ret = regmap_update_bits(smi->map,
1137 RTL8366RB_LED_2_3_CTRL_REG,
1138 0x3F << RTL8366RB_LED_3_OFFSET,
1139 val << RTL8366RB_LED_3_OFFSET);
1140 break;
1141 case 4:
1142 ret = regmap_update_bits(smi->map,
1143 RTL8366RB_INTERRUPT_CONTROL_REG,
1144 RTL8366RB_P4_RGMII_LED,
1145 enable ? RTL8366RB_P4_RGMII_LED : 0);
1146 break;
1147 default:
1148 dev_err(smi->dev, "no LED for port %d\n", port);
1149 return;
1150 }
1151 if (ret)
1152 dev_err(smi->dev, "error updating LED on port %d\n", port);
1153}
1154
1155static int
1156rtl8366rb_port_enable(struct dsa_switch *ds, int port,
1157 struct phy_device *phy)
1158{
1159 struct realtek_smi *smi = ds->priv;
1160 int ret;
1161
1162 dev_dbg(smi->dev, "enable port %d\n", port);
1163 ret = regmap_update_bits(smi->map, RTL8366RB_PECR, BIT(port),
1164 0);
1165 if (ret)
1166 return ret;
1167
1168 rb8366rb_set_port_led(smi, port, true);
1169 return 0;
1170}
1171
1172static void
Andrew Lunn75104db2019-02-24 20:44:43 +01001173rtl8366rb_port_disable(struct dsa_switch *ds, int port)
Linus Walleijd8652952018-07-14 11:45:55 +02001174{
1175 struct realtek_smi *smi = ds->priv;
1176 int ret;
1177
1178 dev_dbg(smi->dev, "disable port %d\n", port);
1179 ret = regmap_update_bits(smi->map, RTL8366RB_PECR, BIT(port),
1180 BIT(port));
1181 if (ret)
1182 return;
1183
1184 rb8366rb_set_port_led(smi, port, false);
1185}
1186
DENG Qingfangd5a68022021-09-26 00:59:24 +02001187static int
1188rtl8366rb_port_bridge_join(struct dsa_switch *ds, int port,
1189 struct net_device *bridge)
1190{
1191 struct realtek_smi *smi = ds->priv;
1192 unsigned int port_bitmap = 0;
1193 int ret, i;
1194
1195 /* Loop over all other ports than the current one */
1196 for (i = 0; i < RTL8366RB_PORT_NUM_CPU; i++) {
1197 /* Current port handled last */
1198 if (i == port)
1199 continue;
1200 /* Not on this bridge */
1201 if (dsa_to_port(ds, i)->bridge_dev != bridge)
1202 continue;
1203 /* Join this port to each other port on the bridge */
1204 ret = regmap_update_bits(smi->map, RTL8366RB_PORT_ISO(i),
1205 RTL8366RB_PORT_ISO_PORTS(BIT(port)),
1206 RTL8366RB_PORT_ISO_PORTS(BIT(port)));
1207 if (ret)
1208 dev_err(smi->dev, "failed to join port %d\n", port);
1209
1210 port_bitmap |= BIT(i);
1211 }
1212
1213 /* Set the bits for the ports we can access */
1214 return regmap_update_bits(smi->map, RTL8366RB_PORT_ISO(port),
1215 RTL8366RB_PORT_ISO_PORTS(port_bitmap),
1216 RTL8366RB_PORT_ISO_PORTS(port_bitmap));
1217}
1218
1219static void
1220rtl8366rb_port_bridge_leave(struct dsa_switch *ds, int port,
1221 struct net_device *bridge)
1222{
1223 struct realtek_smi *smi = ds->priv;
1224 unsigned int port_bitmap = 0;
1225 int ret, i;
1226
1227 /* Loop over all other ports than this one */
1228 for (i = 0; i < RTL8366RB_PORT_NUM_CPU; i++) {
1229 /* Current port handled last */
1230 if (i == port)
1231 continue;
1232 /* Not on this bridge */
1233 if (dsa_to_port(ds, i)->bridge_dev != bridge)
1234 continue;
1235 /* Remove this port from any other port on the bridge */
1236 ret = regmap_update_bits(smi->map, RTL8366RB_PORT_ISO(i),
1237 RTL8366RB_PORT_ISO_PORTS(BIT(port)), 0);
1238 if (ret)
1239 dev_err(smi->dev, "failed to leave port %d\n", port);
1240
1241 port_bitmap |= BIT(i);
1242 }
1243
1244 /* Clear the bits for the ports we can not access, leave ourselves */
1245 regmap_update_bits(smi->map, RTL8366RB_PORT_ISO(port),
1246 RTL8366RB_PORT_ISO_PORTS(port_bitmap), 0);
1247}
1248
Linus Walleija4eff912021-09-26 00:59:26 +02001249/**
1250 * rtl8366rb_drop_untagged() - make the switch drop untagged and C-tagged frames
1251 * @smi: SMI state container
1252 * @port: the port to drop untagged and C-tagged frames on
1253 * @drop: whether to drop or pass untagged and C-tagged frames
1254 */
1255static int rtl8366rb_drop_untagged(struct realtek_smi *smi, int port, bool drop)
1256{
1257 return regmap_update_bits(smi->map, RTL8366RB_VLAN_INGRESS_CTRL1_REG,
1258 RTL8366RB_VLAN_INGRESS_CTRL1_DROP(port),
1259 drop ? RTL8366RB_VLAN_INGRESS_CTRL1_DROP(port) : 0);
1260}
1261
1262static int rtl8366rb_vlan_filtering(struct dsa_switch *ds, int port,
1263 bool vlan_filtering,
1264 struct netlink_ext_ack *extack)
1265{
1266 struct realtek_smi *smi = ds->priv;
1267 struct rtl8366rb *rb;
1268 int ret;
1269
1270 rb = smi->chip_data;
1271
1272 dev_dbg(smi->dev, "port %d: %s VLAN filtering\n", port,
1273 vlan_filtering ? "enable" : "disable");
1274
1275 /* If the port is not in the member set, the frame will be dropped */
1276 ret = regmap_update_bits(smi->map, RTL8366RB_VLAN_INGRESS_CTRL2_REG,
1277 BIT(port), vlan_filtering ? BIT(port) : 0);
1278 if (ret)
1279 return ret;
1280
Linus Walleija4eff912021-09-26 00:59:26 +02001281 /* If VLAN filtering is enabled and PVID is also enabled, we must
1282 * not drop any untagged or C-tagged frames. If we turn off VLAN
Linus Walleij55b115c2021-09-29 13:23:22 +02001283 * filtering on a port, we need to accept any frames.
Linus Walleija4eff912021-09-26 00:59:26 +02001284 */
1285 if (vlan_filtering)
1286 ret = rtl8366rb_drop_untagged(smi, port, !rb->pvid_enabled[port]);
1287 else
1288 ret = rtl8366rb_drop_untagged(smi, port, false);
1289
1290 return ret;
1291}
1292
Linus Walleij56d8bb72021-10-05 21:47:02 +02001293static int
1294rtl8366rb_port_pre_bridge_flags(struct dsa_switch *ds, int port,
1295 struct switchdev_brport_flags flags,
1296 struct netlink_ext_ack *extack)
1297{
1298 /* We support enabling/disabling learning */
1299 if (flags.mask & ~(BR_LEARNING))
1300 return -EINVAL;
1301
1302 return 0;
1303}
1304
1305static int
1306rtl8366rb_port_bridge_flags(struct dsa_switch *ds, int port,
1307 struct switchdev_brport_flags flags,
1308 struct netlink_ext_ack *extack)
1309{
1310 struct realtek_smi *smi = ds->priv;
1311 int ret;
1312
1313 if (flags.mask & BR_LEARNING) {
1314 ret = regmap_update_bits(smi->map, RTL8366RB_PORT_LEARNDIS_CTRL,
1315 BIT(port),
1316 (flags.val & BR_LEARNING) ? 0 : BIT(port));
1317 if (ret)
1318 return ret;
1319 }
1320
1321 return 0;
1322}
1323
Linus Walleij1fbd19e2021-10-05 21:47:03 +02001324static void
Linus Walleije674cfd2021-10-05 21:47:04 +02001325rtl8366rb_port_stp_state_set(struct dsa_switch *ds, int port, u8 state)
1326{
1327 struct realtek_smi *smi = ds->priv;
1328 u32 val;
1329 int i;
1330
1331 switch (state) {
1332 case BR_STATE_DISABLED:
1333 val = RTL8366RB_STP_STATE_DISABLED;
1334 break;
1335 case BR_STATE_BLOCKING:
1336 case BR_STATE_LISTENING:
1337 val = RTL8366RB_STP_STATE_BLOCKING;
1338 break;
1339 case BR_STATE_LEARNING:
1340 val = RTL8366RB_STP_STATE_LEARNING;
1341 break;
1342 case BR_STATE_FORWARDING:
1343 val = RTL8366RB_STP_STATE_FORWARDING;
1344 break;
1345 default:
1346 dev_err(smi->dev, "unknown bridge state requested\n");
1347 return;
Yang Li339e75f2021-10-08 14:21:17 +08001348 }
Linus Walleije674cfd2021-10-05 21:47:04 +02001349
1350 /* Set the same status for the port on all the FIDs */
1351 for (i = 0; i < RTL8366RB_NUM_FIDS; i++) {
1352 regmap_update_bits(smi->map, RTL8366RB_STP_STATE_BASE + i,
1353 RTL8366RB_STP_STATE_MASK(port),
1354 RTL8366RB_STP_STATE(port, val));
1355 }
1356}
1357
1358static void
Linus Walleij1fbd19e2021-10-05 21:47:03 +02001359rtl8366rb_port_fast_age(struct dsa_switch *ds, int port)
1360{
1361 struct realtek_smi *smi = ds->priv;
1362
1363 /* This will age out any learned L2 entries */
1364 regmap_update_bits(smi->map, RTL8366RB_SECURITY_CTRL,
1365 BIT(port), BIT(port));
1366 /* Restore the normal state of things */
1367 regmap_update_bits(smi->map, RTL8366RB_SECURITY_CTRL,
1368 BIT(port), 0);
1369}
1370
Linus Walleij5f4a8ef2020-09-05 23:59:14 +02001371static int rtl8366rb_change_mtu(struct dsa_switch *ds, int port, int new_mtu)
1372{
1373 struct realtek_smi *smi = ds->priv;
Linus Walleije0b2e0d2020-10-08 23:03:40 +02001374 struct rtl8366rb *rb;
1375 unsigned int max_mtu;
Linus Walleij5f4a8ef2020-09-05 23:59:14 +02001376 u32 len;
Linus Walleije0b2e0d2020-10-08 23:03:40 +02001377 int i;
Linus Walleij5f4a8ef2020-09-05 23:59:14 +02001378
Linus Walleije0b2e0d2020-10-08 23:03:40 +02001379 /* Cache the per-port MTU setting */
1380 rb = smi->chip_data;
1381 rb->max_mtu[port] = new_mtu;
1382
1383 /* Roof out the MTU for the entire switch to the greatest
1384 * common denominator: the biggest set for any one port will
1385 * be the biggest MTU for the switch.
1386 *
1387 * The first setting, 1522 bytes, is max IP packet 1500 bytes,
Linus Walleij5f4a8ef2020-09-05 23:59:14 +02001388 * plus ethernet header, 1518 bytes, plus CPU tag, 4 bytes.
1389 * This function should consider the parameter an SDU, so the
1390 * MTU passed for this setting is 1518 bytes. The same logic
1391 * of subtracting the DSA tag of 4 bytes apply to the other
1392 * settings.
1393 */
Linus Walleije0b2e0d2020-10-08 23:03:40 +02001394 max_mtu = 1518;
1395 for (i = 0; i < RTL8366RB_NUM_PORTS; i++) {
1396 if (rb->max_mtu[i] > max_mtu)
1397 max_mtu = rb->max_mtu[i];
1398 }
1399 if (max_mtu <= 1518)
Linus Walleij5f4a8ef2020-09-05 23:59:14 +02001400 len = RTL8366RB_SGCR_MAX_LENGTH_1522;
Linus Walleije0b2e0d2020-10-08 23:03:40 +02001401 else if (max_mtu > 1518 && max_mtu <= 1532)
Linus Walleij5f4a8ef2020-09-05 23:59:14 +02001402 len = RTL8366RB_SGCR_MAX_LENGTH_1536;
Linus Walleije0b2e0d2020-10-08 23:03:40 +02001403 else if (max_mtu > 1532 && max_mtu <= 1548)
Linus Walleij5f4a8ef2020-09-05 23:59:14 +02001404 len = RTL8366RB_SGCR_MAX_LENGTH_1552;
1405 else
1406 len = RTL8366RB_SGCR_MAX_LENGTH_16000;
1407
1408 return regmap_update_bits(smi->map, RTL8366RB_SGCR,
1409 RTL8366RB_SGCR_MAX_LENGTH_MASK,
1410 len);
1411}
1412
1413static int rtl8366rb_max_mtu(struct dsa_switch *ds, int port)
1414{
1415 /* The max MTU is 16000 bytes, so we subtract the CPU tag
1416 * and the max presented to the system is 15996 bytes.
1417 */
1418 return 15996;
1419}
1420
Linus Walleijd8652952018-07-14 11:45:55 +02001421static int rtl8366rb_get_vlan_4k(struct realtek_smi *smi, u32 vid,
1422 struct rtl8366_vlan_4k *vlan4k)
1423{
1424 u32 data[3];
1425 int ret;
1426 int i;
1427
1428 memset(vlan4k, '\0', sizeof(struct rtl8366_vlan_4k));
1429
1430 if (vid >= RTL8366RB_NUM_VIDS)
1431 return -EINVAL;
1432
1433 /* write VID */
1434 ret = regmap_write(smi->map, RTL8366RB_VLAN_TABLE_WRITE_BASE,
1435 vid & RTL8366RB_VLAN_VID_MASK);
1436 if (ret)
1437 return ret;
1438
1439 /* write table access control word */
1440 ret = regmap_write(smi->map, RTL8366RB_TABLE_ACCESS_CTRL_REG,
1441 RTL8366RB_TABLE_VLAN_READ_CTRL);
1442 if (ret)
1443 return ret;
1444
1445 for (i = 0; i < 3; i++) {
1446 ret = regmap_read(smi->map,
1447 RTL8366RB_VLAN_TABLE_READ_BASE + i,
1448 &data[i]);
1449 if (ret)
1450 return ret;
1451 }
1452
1453 vlan4k->vid = vid;
1454 vlan4k->untag = (data[1] >> RTL8366RB_VLAN_UNTAG_SHIFT) &
1455 RTL8366RB_VLAN_UNTAG_MASK;
1456 vlan4k->member = data[1] & RTL8366RB_VLAN_MEMBER_MASK;
1457 vlan4k->fid = data[2] & RTL8366RB_VLAN_FID_MASK;
1458
1459 return 0;
1460}
1461
1462static int rtl8366rb_set_vlan_4k(struct realtek_smi *smi,
1463 const struct rtl8366_vlan_4k *vlan4k)
1464{
1465 u32 data[3];
1466 int ret;
1467 int i;
1468
1469 if (vlan4k->vid >= RTL8366RB_NUM_VIDS ||
1470 vlan4k->member > RTL8366RB_VLAN_MEMBER_MASK ||
1471 vlan4k->untag > RTL8366RB_VLAN_UNTAG_MASK ||
1472 vlan4k->fid > RTL8366RB_FIDMAX)
1473 return -EINVAL;
1474
1475 data[0] = vlan4k->vid & RTL8366RB_VLAN_VID_MASK;
1476 data[1] = (vlan4k->member & RTL8366RB_VLAN_MEMBER_MASK) |
1477 ((vlan4k->untag & RTL8366RB_VLAN_UNTAG_MASK) <<
1478 RTL8366RB_VLAN_UNTAG_SHIFT);
1479 data[2] = vlan4k->fid & RTL8366RB_VLAN_FID_MASK;
1480
1481 for (i = 0; i < 3; i++) {
1482 ret = regmap_write(smi->map,
1483 RTL8366RB_VLAN_TABLE_WRITE_BASE + i,
1484 data[i]);
1485 if (ret)
1486 return ret;
1487 }
1488
1489 /* write table access control word */
1490 ret = regmap_write(smi->map, RTL8366RB_TABLE_ACCESS_CTRL_REG,
1491 RTL8366RB_TABLE_VLAN_WRITE_CTRL);
1492
1493 return ret;
1494}
1495
1496static int rtl8366rb_get_vlan_mc(struct realtek_smi *smi, u32 index,
1497 struct rtl8366_vlan_mc *vlanmc)
1498{
1499 u32 data[3];
1500 int ret;
1501 int i;
1502
1503 memset(vlanmc, '\0', sizeof(struct rtl8366_vlan_mc));
1504
1505 if (index >= RTL8366RB_NUM_VLANS)
1506 return -EINVAL;
1507
1508 for (i = 0; i < 3; i++) {
1509 ret = regmap_read(smi->map,
1510 RTL8366RB_VLAN_MC_BASE(index) + i,
1511 &data[i]);
1512 if (ret)
1513 return ret;
1514 }
1515
1516 vlanmc->vid = data[0] & RTL8366RB_VLAN_VID_MASK;
1517 vlanmc->priority = (data[0] >> RTL8366RB_VLAN_PRIORITY_SHIFT) &
1518 RTL8366RB_VLAN_PRIORITY_MASK;
1519 vlanmc->untag = (data[1] >> RTL8366RB_VLAN_UNTAG_SHIFT) &
1520 RTL8366RB_VLAN_UNTAG_MASK;
1521 vlanmc->member = data[1] & RTL8366RB_VLAN_MEMBER_MASK;
1522 vlanmc->fid = data[2] & RTL8366RB_VLAN_FID_MASK;
1523
1524 return 0;
1525}
1526
1527static int rtl8366rb_set_vlan_mc(struct realtek_smi *smi, u32 index,
1528 const struct rtl8366_vlan_mc *vlanmc)
1529{
1530 u32 data[3];
1531 int ret;
1532 int i;
1533
1534 if (index >= RTL8366RB_NUM_VLANS ||
1535 vlanmc->vid >= RTL8366RB_NUM_VIDS ||
1536 vlanmc->priority > RTL8366RB_PRIORITYMAX ||
1537 vlanmc->member > RTL8366RB_VLAN_MEMBER_MASK ||
1538 vlanmc->untag > RTL8366RB_VLAN_UNTAG_MASK ||
1539 vlanmc->fid > RTL8366RB_FIDMAX)
1540 return -EINVAL;
1541
1542 data[0] = (vlanmc->vid & RTL8366RB_VLAN_VID_MASK) |
1543 ((vlanmc->priority & RTL8366RB_VLAN_PRIORITY_MASK) <<
1544 RTL8366RB_VLAN_PRIORITY_SHIFT);
1545 data[1] = (vlanmc->member & RTL8366RB_VLAN_MEMBER_MASK) |
1546 ((vlanmc->untag & RTL8366RB_VLAN_UNTAG_MASK) <<
1547 RTL8366RB_VLAN_UNTAG_SHIFT);
1548 data[2] = vlanmc->fid & RTL8366RB_VLAN_FID_MASK;
1549
1550 for (i = 0; i < 3; i++) {
1551 ret = regmap_write(smi->map,
1552 RTL8366RB_VLAN_MC_BASE(index) + i,
1553 data[i]);
1554 if (ret)
1555 return ret;
1556 }
1557
1558 return 0;
1559}
1560
1561static int rtl8366rb_get_mc_index(struct realtek_smi *smi, int port, int *val)
1562{
1563 u32 data;
1564 int ret;
1565
1566 if (port >= smi->num_ports)
1567 return -EINVAL;
1568
1569 ret = regmap_read(smi->map, RTL8366RB_PORT_VLAN_CTRL_REG(port),
1570 &data);
1571 if (ret)
1572 return ret;
1573
1574 *val = (data >> RTL8366RB_PORT_VLAN_CTRL_SHIFT(port)) &
1575 RTL8366RB_PORT_VLAN_CTRL_MASK;
1576
1577 return 0;
1578}
1579
1580static int rtl8366rb_set_mc_index(struct realtek_smi *smi, int port, int index)
1581{
Linus Walleija4eff912021-09-26 00:59:26 +02001582 struct rtl8366rb *rb;
1583 bool pvid_enabled;
1584 int ret;
1585
1586 rb = smi->chip_data;
1587 pvid_enabled = !!index;
1588
Linus Walleijd8652952018-07-14 11:45:55 +02001589 if (port >= smi->num_ports || index >= RTL8366RB_NUM_VLANS)
1590 return -EINVAL;
1591
Linus Walleija4eff912021-09-26 00:59:26 +02001592 ret = regmap_update_bits(smi->map, RTL8366RB_PORT_VLAN_CTRL_REG(port),
Linus Walleijd8652952018-07-14 11:45:55 +02001593 RTL8366RB_PORT_VLAN_CTRL_MASK <<
1594 RTL8366RB_PORT_VLAN_CTRL_SHIFT(port),
1595 (index & RTL8366RB_PORT_VLAN_CTRL_MASK) <<
1596 RTL8366RB_PORT_VLAN_CTRL_SHIFT(port));
Linus Walleija4eff912021-09-26 00:59:26 +02001597 if (ret)
1598 return ret;
1599
1600 rb->pvid_enabled[port] = pvid_enabled;
1601
1602 /* If VLAN filtering is enabled and PVID is also enabled, we must
1603 * not drop any untagged or C-tagged frames. Make sure to update the
1604 * filtering setting.
1605 */
Linus Walleij55b115c2021-09-29 13:23:22 +02001606 if (dsa_port_is_vlan_filtering(dsa_to_port(smi->ds, port)))
Linus Walleija4eff912021-09-26 00:59:26 +02001607 ret = rtl8366rb_drop_untagged(smi, port, !pvid_enabled);
1608
1609 return ret;
Linus Walleijd8652952018-07-14 11:45:55 +02001610}
1611
1612static bool rtl8366rb_is_vlan_valid(struct realtek_smi *smi, unsigned int vlan)
1613{
Linus Walleij5f5f12f2021-09-26 00:59:27 +02001614 unsigned int max = RTL8366RB_NUM_VLANS - 1;
Linus Walleijd8652952018-07-14 11:45:55 +02001615
1616 if (smi->vlan4k_enabled)
1617 max = RTL8366RB_NUM_VIDS - 1;
1618
Linus Walleija4eff912021-09-26 00:59:26 +02001619 if (vlan > max)
Linus Walleijd8652952018-07-14 11:45:55 +02001620 return false;
1621
1622 return true;
1623}
1624
1625static int rtl8366rb_enable_vlan(struct realtek_smi *smi, bool enable)
1626{
1627 dev_dbg(smi->dev, "%s VLAN\n", enable ? "enable" : "disable");
1628 return regmap_update_bits(smi->map,
1629 RTL8366RB_SGCR, RTL8366RB_SGCR_EN_VLAN,
1630 enable ? RTL8366RB_SGCR_EN_VLAN : 0);
1631}
1632
1633static int rtl8366rb_enable_vlan4k(struct realtek_smi *smi, bool enable)
1634{
1635 dev_dbg(smi->dev, "%s VLAN 4k\n", enable ? "enable" : "disable");
1636 return regmap_update_bits(smi->map, RTL8366RB_SGCR,
1637 RTL8366RB_SGCR_EN_VLAN_4KTB,
1638 enable ? RTL8366RB_SGCR_EN_VLAN_4KTB : 0);
1639}
1640
1641static int rtl8366rb_phy_read(struct realtek_smi *smi, int phy, int regnum)
1642{
1643 u32 val;
1644 u32 reg;
1645 int ret;
1646
1647 if (phy > RTL8366RB_PHY_NO_MAX)
1648 return -EINVAL;
1649
1650 ret = regmap_write(smi->map, RTL8366RB_PHY_ACCESS_CTRL_REG,
1651 RTL8366RB_PHY_CTRL_READ);
1652 if (ret)
1653 return ret;
1654
1655 reg = 0x8000 | (1 << (phy + RTL8366RB_PHY_NO_OFFSET)) | regnum;
1656
1657 ret = regmap_write(smi->map, reg, 0);
1658 if (ret) {
1659 dev_err(smi->dev,
1660 "failed to write PHY%d reg %04x @ %04x, ret %d\n",
1661 phy, regnum, reg, ret);
1662 return ret;
1663 }
1664
1665 ret = regmap_read(smi->map, RTL8366RB_PHY_ACCESS_DATA_REG, &val);
1666 if (ret)
1667 return ret;
1668
1669 dev_dbg(smi->dev, "read PHY%d register 0x%04x @ %08x, val <- %04x\n",
1670 phy, regnum, reg, val);
1671
1672 return val;
1673}
1674
1675static int rtl8366rb_phy_write(struct realtek_smi *smi, int phy, int regnum,
1676 u16 val)
1677{
1678 u32 reg;
1679 int ret;
1680
1681 if (phy > RTL8366RB_PHY_NO_MAX)
1682 return -EINVAL;
1683
1684 ret = regmap_write(smi->map, RTL8366RB_PHY_ACCESS_CTRL_REG,
1685 RTL8366RB_PHY_CTRL_WRITE);
1686 if (ret)
1687 return ret;
1688
1689 reg = 0x8000 | (1 << (phy + RTL8366RB_PHY_NO_OFFSET)) | regnum;
1690
1691 dev_dbg(smi->dev, "write PHY%d register 0x%04x @ %04x, val -> %04x\n",
1692 phy, regnum, reg, val);
1693
1694 ret = regmap_write(smi->map, reg, val);
1695 if (ret)
1696 return ret;
1697
1698 return 0;
1699}
1700
1701static int rtl8366rb_reset_chip(struct realtek_smi *smi)
1702{
1703 int timeout = 10;
1704 u32 val;
1705 int ret;
1706
1707 realtek_smi_write_reg_noack(smi, RTL8366RB_RESET_CTRL_REG,
1708 RTL8366RB_CHIP_CTRL_RESET_HW);
1709 do {
1710 usleep_range(20000, 25000);
1711 ret = regmap_read(smi->map, RTL8366RB_RESET_CTRL_REG, &val);
1712 if (ret)
1713 return ret;
1714
1715 if (!(val & RTL8366RB_CHIP_CTRL_RESET_HW))
1716 break;
1717 } while (--timeout);
1718
1719 if (!timeout) {
1720 dev_err(smi->dev, "timeout waiting for the switch to reset\n");
1721 return -EIO;
1722 }
1723
1724 return 0;
1725}
1726
1727static int rtl8366rb_detect(struct realtek_smi *smi)
1728{
1729 struct device *dev = smi->dev;
1730 int ret;
1731 u32 val;
1732
1733 /* Detect device */
1734 ret = regmap_read(smi->map, 0x5c, &val);
1735 if (ret) {
1736 dev_err(dev, "can't get chip ID (%d)\n", ret);
1737 return ret;
1738 }
1739
1740 switch (val) {
1741 case 0x6027:
1742 dev_info(dev, "found an RTL8366S switch\n");
1743 dev_err(dev, "this switch is not yet supported, submit patches!\n");
1744 return -ENODEV;
1745 case 0x5937:
1746 dev_info(dev, "found an RTL8366RB switch\n");
1747 smi->cpu_port = RTL8366RB_PORT_NUM_CPU;
1748 smi->num_ports = RTL8366RB_NUM_PORTS;
1749 smi->num_vlan_mc = RTL8366RB_NUM_VLANS;
1750 smi->mib_counters = rtl8366rb_mib_counters;
1751 smi->num_mib_counters = ARRAY_SIZE(rtl8366rb_mib_counters);
1752 break;
1753 default:
1754 dev_info(dev, "found an Unknown Realtek switch (id=0x%04x)\n",
1755 val);
1756 break;
1757 }
1758
1759 ret = rtl8366rb_reset_chip(smi);
1760 if (ret)
1761 return ret;
1762
1763 return 0;
1764}
1765
1766static const struct dsa_switch_ops rtl8366rb_switch_ops = {
1767 .get_tag_protocol = rtl8366_get_tag_protocol,
1768 .setup = rtl8366rb_setup,
Linus Walleijbb1416a2020-09-06 23:24:15 +02001769 .phylink_mac_link_up = rtl8366rb_mac_link_up,
1770 .phylink_mac_link_down = rtl8366rb_mac_link_down,
Linus Walleijd8652952018-07-14 11:45:55 +02001771 .get_strings = rtl8366_get_strings,
1772 .get_ethtool_stats = rtl8366_get_ethtool_stats,
1773 .get_sset_count = rtl8366_get_sset_count,
DENG Qingfangd5a68022021-09-26 00:59:24 +02001774 .port_bridge_join = rtl8366rb_port_bridge_join,
1775 .port_bridge_leave = rtl8366rb_port_bridge_leave,
Linus Walleija4eff912021-09-26 00:59:26 +02001776 .port_vlan_filtering = rtl8366rb_vlan_filtering,
Linus Walleijd8652952018-07-14 11:45:55 +02001777 .port_vlan_add = rtl8366_vlan_add,
1778 .port_vlan_del = rtl8366_vlan_del,
1779 .port_enable = rtl8366rb_port_enable,
1780 .port_disable = rtl8366rb_port_disable,
Linus Walleij56d8bb72021-10-05 21:47:02 +02001781 .port_pre_bridge_flags = rtl8366rb_port_pre_bridge_flags,
1782 .port_bridge_flags = rtl8366rb_port_bridge_flags,
Linus Walleije674cfd2021-10-05 21:47:04 +02001783 .port_stp_state_set = rtl8366rb_port_stp_state_set,
Linus Walleij1fbd19e2021-10-05 21:47:03 +02001784 .port_fast_age = rtl8366rb_port_fast_age,
Linus Walleij5f4a8ef2020-09-05 23:59:14 +02001785 .port_change_mtu = rtl8366rb_change_mtu,
1786 .port_max_mtu = rtl8366rb_max_mtu,
Linus Walleijd8652952018-07-14 11:45:55 +02001787};
1788
1789static const struct realtek_smi_ops rtl8366rb_smi_ops = {
1790 .detect = rtl8366rb_detect,
1791 .get_vlan_mc = rtl8366rb_get_vlan_mc,
1792 .set_vlan_mc = rtl8366rb_set_vlan_mc,
1793 .get_vlan_4k = rtl8366rb_get_vlan_4k,
1794 .set_vlan_4k = rtl8366rb_set_vlan_4k,
1795 .get_mc_index = rtl8366rb_get_mc_index,
1796 .set_mc_index = rtl8366rb_set_mc_index,
1797 .get_mib_counter = rtl8366rb_get_mib_counter,
1798 .is_vlan_valid = rtl8366rb_is_vlan_valid,
1799 .enable_vlan = rtl8366rb_enable_vlan,
1800 .enable_vlan4k = rtl8366rb_enable_vlan4k,
1801 .phy_read = rtl8366rb_phy_read,
1802 .phy_write = rtl8366rb_phy_write,
1803};
1804
1805const struct realtek_smi_variant rtl8366rb_variant = {
1806 .ds_ops = &rtl8366rb_switch_ops,
1807 .ops = &rtl8366rb_smi_ops,
1808 .clk_delay = 10,
1809 .cmd_read = 0xa9,
1810 .cmd_write = 0xa8,
Linus Walleije0b2e0d2020-10-08 23:03:40 +02001811 .chip_data_sz = sizeof(struct rtl8366rb),
Linus Walleijd8652952018-07-14 11:45:55 +02001812};
1813EXPORT_SYMBOL_GPL(rtl8366rb_variant);