blob: 1f59fefc29c17b79ff2ff3845b4efd3d18b9efcc [file] [log] [blame]
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02001// SPDX-License-Identifier: GPL-2.0
2/*
Aleksander Jan Bajkowskia09d0422021-03-22 21:37:15 +01003 * Lantiq / Intel GSWIP switch driver for VRX200, xRX300 and xRX330 SoCs
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02004 *
5 * Copyright (C) 2010 Lantiq Deutschland
6 * Copyright (C) 2012 John Crispin <john@phrozen.org>
Hauke Mehrtens8206e0c2019-05-06 00:25:07 +02007 * Copyright (C) 2017 - 2019 Hauke Mehrtens <hauke@hauke-m.de>
8 *
9 * The VLAN and bridge model the GSWIP hardware uses does not directly
10 * matches the model DSA uses.
11 *
12 * The hardware has 64 possible table entries for bridges with one VLAN
13 * ID, one flow id and a list of ports for each bridge. All entries which
14 * match the same flow ID are combined in the mac learning table, they
15 * act as one global bridge.
16 * The hardware does not support VLAN filter on the port, but on the
17 * bridge, this driver converts the DSA model to the hardware.
18 *
19 * The CPU gets all the exception frames which do not match any forwarding
20 * rule and the CPU port is also added to all bridges. This makes it possible
21 * to handle all the special cases easily in software.
22 * At the initialization the driver allocates one bridge table entry for
23 * each switch port which is used when the port is used without an
24 * explicit bridge. This prevents the frames from being forwarded
25 * between all LAN ports by default.
Hauke Mehrtens14fceff2018-09-09 22:20:39 +020026 */
27
28#include <linux/clk.h>
Martin Blumenstingl2a1828e2020-11-15 17:57:57 +010029#include <linux/delay.h>
Hauke Mehrtens14fceff2018-09-09 22:20:39 +020030#include <linux/etherdevice.h>
31#include <linux/firmware.h>
32#include <linux/if_bridge.h>
33#include <linux/if_vlan.h>
34#include <linux/iopoll.h>
35#include <linux/mfd/syscon.h>
36#include <linux/module.h>
37#include <linux/of_mdio.h>
38#include <linux/of_net.h>
39#include <linux/of_platform.h>
40#include <linux/phy.h>
41#include <linux/phylink.h>
42#include <linux/platform_device.h>
43#include <linux/regmap.h>
44#include <linux/reset.h>
45#include <net/dsa.h>
46#include <dt-bindings/mips/lantiq_rcu_gphy.h>
47
48#include "lantiq_pce.h"
49
50/* GSWIP MDIO Registers */
51#define GSWIP_MDIO_GLOB 0x00
52#define GSWIP_MDIO_GLOB_ENABLE BIT(15)
53#define GSWIP_MDIO_CTRL 0x08
54#define GSWIP_MDIO_CTRL_BUSY BIT(12)
55#define GSWIP_MDIO_CTRL_RD BIT(11)
56#define GSWIP_MDIO_CTRL_WR BIT(10)
57#define GSWIP_MDIO_CTRL_PHYAD_MASK 0x1f
58#define GSWIP_MDIO_CTRL_PHYAD_SHIFT 5
59#define GSWIP_MDIO_CTRL_REGAD_MASK 0x1f
60#define GSWIP_MDIO_READ 0x09
61#define GSWIP_MDIO_WRITE 0x0A
62#define GSWIP_MDIO_MDC_CFG0 0x0B
63#define GSWIP_MDIO_MDC_CFG1 0x0C
64#define GSWIP_MDIO_PHYp(p) (0x15 - (p))
65#define GSWIP_MDIO_PHY_LINK_MASK 0x6000
66#define GSWIP_MDIO_PHY_LINK_AUTO 0x0000
67#define GSWIP_MDIO_PHY_LINK_DOWN 0x4000
68#define GSWIP_MDIO_PHY_LINK_UP 0x2000
69#define GSWIP_MDIO_PHY_SPEED_MASK 0x1800
70#define GSWIP_MDIO_PHY_SPEED_AUTO 0x1800
71#define GSWIP_MDIO_PHY_SPEED_M10 0x0000
72#define GSWIP_MDIO_PHY_SPEED_M100 0x0800
73#define GSWIP_MDIO_PHY_SPEED_G1 0x1000
74#define GSWIP_MDIO_PHY_FDUP_MASK 0x0600
75#define GSWIP_MDIO_PHY_FDUP_AUTO 0x0000
76#define GSWIP_MDIO_PHY_FDUP_EN 0x0200
77#define GSWIP_MDIO_PHY_FDUP_DIS 0x0600
78#define GSWIP_MDIO_PHY_FCONTX_MASK 0x0180
79#define GSWIP_MDIO_PHY_FCONTX_AUTO 0x0000
80#define GSWIP_MDIO_PHY_FCONTX_EN 0x0100
81#define GSWIP_MDIO_PHY_FCONTX_DIS 0x0180
82#define GSWIP_MDIO_PHY_FCONRX_MASK 0x0060
83#define GSWIP_MDIO_PHY_FCONRX_AUTO 0x0000
84#define GSWIP_MDIO_PHY_FCONRX_EN 0x0020
85#define GSWIP_MDIO_PHY_FCONRX_DIS 0x0060
86#define GSWIP_MDIO_PHY_ADDR_MASK 0x001f
87#define GSWIP_MDIO_PHY_MASK (GSWIP_MDIO_PHY_ADDR_MASK | \
88 GSWIP_MDIO_PHY_FCONRX_MASK | \
89 GSWIP_MDIO_PHY_FCONTX_MASK | \
90 GSWIP_MDIO_PHY_LINK_MASK | \
91 GSWIP_MDIO_PHY_SPEED_MASK | \
92 GSWIP_MDIO_PHY_FDUP_MASK)
93
94/* GSWIP MII Registers */
Martin Blumenstingl709a3c92021-01-03 02:25:44 +010095#define GSWIP_MII_CFGp(p) (0x2 * (p))
Martin Blumenstingl4b592322021-04-08 20:38:28 +020096#define GSWIP_MII_CFG_RESET BIT(15)
Hauke Mehrtens14fceff2018-09-09 22:20:39 +020097#define GSWIP_MII_CFG_EN BIT(14)
Martin Blumenstingl4b592322021-04-08 20:38:28 +020098#define GSWIP_MII_CFG_ISOLATE BIT(13)
Hauke Mehrtens14fceff2018-09-09 22:20:39 +020099#define GSWIP_MII_CFG_LDCLKDIS BIT(12)
Martin Blumenstingl4b592322021-04-08 20:38:28 +0200100#define GSWIP_MII_CFG_RGMII_IBS BIT(8)
101#define GSWIP_MII_CFG_RMII_CLK BIT(7)
Hauke Mehrtens14fceff2018-09-09 22:20:39 +0200102#define GSWIP_MII_CFG_MODE_MIIP 0x0
103#define GSWIP_MII_CFG_MODE_MIIM 0x1
104#define GSWIP_MII_CFG_MODE_RMIIP 0x2
105#define GSWIP_MII_CFG_MODE_RMIIM 0x3
106#define GSWIP_MII_CFG_MODE_RGMII 0x4
Aleksander Jan Bajkowskia09d0422021-03-22 21:37:15 +0100107#define GSWIP_MII_CFG_MODE_GMII 0x9
Hauke Mehrtens14fceff2018-09-09 22:20:39 +0200108#define GSWIP_MII_CFG_MODE_MASK 0xf
109#define GSWIP_MII_CFG_RATE_M2P5 0x00
110#define GSWIP_MII_CFG_RATE_M25 0x10
111#define GSWIP_MII_CFG_RATE_M125 0x20
112#define GSWIP_MII_CFG_RATE_M50 0x30
113#define GSWIP_MII_CFG_RATE_AUTO 0x40
114#define GSWIP_MII_CFG_RATE_MASK 0x70
115#define GSWIP_MII_PCDU0 0x01
116#define GSWIP_MII_PCDU1 0x03
117#define GSWIP_MII_PCDU5 0x05
118#define GSWIP_MII_PCDU_TXDLY_MASK GENMASK(2, 0)
119#define GSWIP_MII_PCDU_RXDLY_MASK GENMASK(9, 7)
120
121/* GSWIP Core Registers */
122#define GSWIP_SWRES 0x000
123#define GSWIP_SWRES_R1 BIT(1) /* GSWIP Software reset */
124#define GSWIP_SWRES_R0 BIT(0) /* GSWIP Hardware reset */
125#define GSWIP_VERSION 0x013
126#define GSWIP_VERSION_REV_SHIFT 0
127#define GSWIP_VERSION_REV_MASK GENMASK(7, 0)
128#define GSWIP_VERSION_MOD_SHIFT 8
129#define GSWIP_VERSION_MOD_MASK GENMASK(15, 8)
130#define GSWIP_VERSION_2_0 0x100
131#define GSWIP_VERSION_2_1 0x021
132#define GSWIP_VERSION_2_2 0x122
133#define GSWIP_VERSION_2_2_ETC 0x022
134
135#define GSWIP_BM_RAM_VAL(x) (0x043 - (x))
136#define GSWIP_BM_RAM_ADDR 0x044
137#define GSWIP_BM_RAM_CTRL 0x045
138#define GSWIP_BM_RAM_CTRL_BAS BIT(15)
139#define GSWIP_BM_RAM_CTRL_OPMOD BIT(5)
140#define GSWIP_BM_RAM_CTRL_ADDR_MASK GENMASK(4, 0)
141#define GSWIP_BM_QUEUE_GCTRL 0x04A
142#define GSWIP_BM_QUEUE_GCTRL_GL_MOD BIT(10)
143/* buffer management Port Configuration Register */
144#define GSWIP_BM_PCFGp(p) (0x080 + ((p) * 2))
145#define GSWIP_BM_PCFG_CNTEN BIT(0) /* RMON Counter Enable */
146#define GSWIP_BM_PCFG_IGCNT BIT(1) /* Ingres Special Tag RMON count */
147/* buffer management Port Control Register */
148#define GSWIP_BM_RMON_CTRLp(p) (0x81 + ((p) * 2))
149#define GSWIP_BM_CTRL_RMON_RAM1_RES BIT(0) /* Software Reset for RMON RAM 1 */
150#define GSWIP_BM_CTRL_RMON_RAM2_RES BIT(1) /* Software Reset for RMON RAM 2 */
151
152/* PCE */
153#define GSWIP_PCE_TBL_KEY(x) (0x447 - (x))
154#define GSWIP_PCE_TBL_MASK 0x448
155#define GSWIP_PCE_TBL_VAL(x) (0x44D - (x))
156#define GSWIP_PCE_TBL_ADDR 0x44E
157#define GSWIP_PCE_TBL_CTRL 0x44F
158#define GSWIP_PCE_TBL_CTRL_BAS BIT(15)
159#define GSWIP_PCE_TBL_CTRL_TYPE BIT(13)
160#define GSWIP_PCE_TBL_CTRL_VLD BIT(12)
161#define GSWIP_PCE_TBL_CTRL_KEYFORM BIT(11)
162#define GSWIP_PCE_TBL_CTRL_GMAP_MASK GENMASK(10, 7)
163#define GSWIP_PCE_TBL_CTRL_OPMOD_MASK GENMASK(6, 5)
164#define GSWIP_PCE_TBL_CTRL_OPMOD_ADRD 0x00
165#define GSWIP_PCE_TBL_CTRL_OPMOD_ADWR 0x20
166#define GSWIP_PCE_TBL_CTRL_OPMOD_KSRD 0x40
167#define GSWIP_PCE_TBL_CTRL_OPMOD_KSWR 0x60
168#define GSWIP_PCE_TBL_CTRL_ADDR_MASK GENMASK(4, 0)
169#define GSWIP_PCE_PMAP1 0x453 /* Monitoring port map */
170#define GSWIP_PCE_PMAP2 0x454 /* Default Multicast port map */
171#define GSWIP_PCE_PMAP3 0x455 /* Default Unknown Unicast port map */
172#define GSWIP_PCE_GCTRL_0 0x456
Hauke Mehrtens8206e0c2019-05-06 00:25:07 +0200173#define GSWIP_PCE_GCTRL_0_MTFL BIT(0) /* MAC Table Flushing */
Hauke Mehrtens14fceff2018-09-09 22:20:39 +0200174#define GSWIP_PCE_GCTRL_0_MC_VALID BIT(3)
175#define GSWIP_PCE_GCTRL_0_VLAN BIT(14) /* VLAN aware Switching */
176#define GSWIP_PCE_GCTRL_1 0x457
177#define GSWIP_PCE_GCTRL_1_MAC_GLOCK BIT(2) /* MAC Address table lock */
178#define GSWIP_PCE_GCTRL_1_MAC_GLOCK_MOD BIT(3) /* Mac address table lock forwarding mode */
179#define GSWIP_PCE_PCTRL_0p(p) (0x480 + ((p) * 0xA))
Hauke Mehrtens8206e0c2019-05-06 00:25:07 +0200180#define GSWIP_PCE_PCTRL_0_TVM BIT(5) /* Transparent VLAN mode */
181#define GSWIP_PCE_PCTRL_0_VREP BIT(6) /* VLAN Replace Mode */
182#define GSWIP_PCE_PCTRL_0_INGRESS BIT(11) /* Accept special tag in ingress */
Hauke Mehrtens14fceff2018-09-09 22:20:39 +0200183#define GSWIP_PCE_PCTRL_0_PSTATE_LISTEN 0x0
184#define GSWIP_PCE_PCTRL_0_PSTATE_RX 0x1
185#define GSWIP_PCE_PCTRL_0_PSTATE_TX 0x2
186#define GSWIP_PCE_PCTRL_0_PSTATE_LEARNING 0x3
187#define GSWIP_PCE_PCTRL_0_PSTATE_FORWARDING 0x7
188#define GSWIP_PCE_PCTRL_0_PSTATE_MASK GENMASK(2, 0)
Hauke Mehrtens8206e0c2019-05-06 00:25:07 +0200189#define GSWIP_PCE_VCTRL(p) (0x485 + ((p) * 0xA))
190#define GSWIP_PCE_VCTRL_UVR BIT(0) /* Unknown VLAN Rule */
191#define GSWIP_PCE_VCTRL_VIMR BIT(3) /* VLAN Ingress Member violation rule */
192#define GSWIP_PCE_VCTRL_VEMR BIT(4) /* VLAN Egress Member violation rule */
193#define GSWIP_PCE_VCTRL_VSR BIT(5) /* VLAN Security */
194#define GSWIP_PCE_VCTRL_VID0 BIT(6) /* Priority Tagged Rule */
195#define GSWIP_PCE_DEFPVID(p) (0x486 + ((p) * 0xA))
Hauke Mehrtens14fceff2018-09-09 22:20:39 +0200196
197#define GSWIP_MAC_FLEN 0x8C5
Martin Blumenstingl3e9005b2021-04-08 20:38:27 +0200198#define GSWIP_MAC_CTRL_0p(p) (0x903 + ((p) * 0xC))
199#define GSWIP_MAC_CTRL_0_PADEN BIT(8)
200#define GSWIP_MAC_CTRL_0_FCS_EN BIT(7)
201#define GSWIP_MAC_CTRL_0_FCON_MASK 0x0070
202#define GSWIP_MAC_CTRL_0_FCON_AUTO 0x0000
203#define GSWIP_MAC_CTRL_0_FCON_RX 0x0010
204#define GSWIP_MAC_CTRL_0_FCON_TX 0x0020
205#define GSWIP_MAC_CTRL_0_FCON_RXTX 0x0030
206#define GSWIP_MAC_CTRL_0_FCON_NONE 0x0040
207#define GSWIP_MAC_CTRL_0_FDUP_MASK 0x000C
208#define GSWIP_MAC_CTRL_0_FDUP_AUTO 0x0000
209#define GSWIP_MAC_CTRL_0_FDUP_EN 0x0004
210#define GSWIP_MAC_CTRL_0_FDUP_DIS 0x000C
211#define GSWIP_MAC_CTRL_0_GMII_MASK 0x0003
212#define GSWIP_MAC_CTRL_0_GMII_AUTO 0x0000
213#define GSWIP_MAC_CTRL_0_GMII_MII 0x0001
214#define GSWIP_MAC_CTRL_0_GMII_RGMII 0x0002
Hauke Mehrtens14fceff2018-09-09 22:20:39 +0200215#define GSWIP_MAC_CTRL_2p(p) (0x905 + ((p) * 0xC))
216#define GSWIP_MAC_CTRL_2_MLEN BIT(3) /* Maximum Untagged Frame Lnegth */
217
218/* Ethernet Switch Fetch DMA Port Control Register */
219#define GSWIP_FDMA_PCTRLp(p) (0xA80 + ((p) * 0x6))
220#define GSWIP_FDMA_PCTRL_EN BIT(0) /* FDMA Port Enable */
221#define GSWIP_FDMA_PCTRL_STEN BIT(1) /* Special Tag Insertion Enable */
222#define GSWIP_FDMA_PCTRL_VLANMOD_MASK GENMASK(4, 3) /* VLAN Modification Control */
223#define GSWIP_FDMA_PCTRL_VLANMOD_SHIFT 3 /* VLAN Modification Control */
224#define GSWIP_FDMA_PCTRL_VLANMOD_DIS (0x0 << GSWIP_FDMA_PCTRL_VLANMOD_SHIFT)
225#define GSWIP_FDMA_PCTRL_VLANMOD_PRIO (0x1 << GSWIP_FDMA_PCTRL_VLANMOD_SHIFT)
226#define GSWIP_FDMA_PCTRL_VLANMOD_ID (0x2 << GSWIP_FDMA_PCTRL_VLANMOD_SHIFT)
227#define GSWIP_FDMA_PCTRL_VLANMOD_BOTH (0x3 << GSWIP_FDMA_PCTRL_VLANMOD_SHIFT)
228
229/* Ethernet Switch Store DMA Port Control Register */
230#define GSWIP_SDMA_PCTRLp(p) (0xBC0 + ((p) * 0x6))
231#define GSWIP_SDMA_PCTRL_EN BIT(0) /* SDMA Port Enable */
232#define GSWIP_SDMA_PCTRL_FCEN BIT(1) /* Flow Control Enable */
Aleksander Jan Bajkowski66d26282021-10-16 00:10:20 +0200233#define GSWIP_SDMA_PCTRL_PAUFWD BIT(3) /* Pause Frame Forwarding */
Hauke Mehrtens14fceff2018-09-09 22:20:39 +0200234
Hauke Mehrtens8206e0c2019-05-06 00:25:07 +0200235#define GSWIP_TABLE_ACTIVE_VLAN 0x01
236#define GSWIP_TABLE_VLAN_MAPPING 0x02
Hauke Mehrtens45813482019-05-06 00:25:09 +0200237#define GSWIP_TABLE_MAC_BRIDGE 0x0b
238#define GSWIP_TABLE_MAC_BRIDGE_STATIC 0x01 /* Static not, aging entry */
Hauke Mehrtens8206e0c2019-05-06 00:25:07 +0200239
Hauke Mehrtens14fceff2018-09-09 22:20:39 +0200240#define XRX200_GPHY_FW_ALIGN (16 * 1024)
241
242struct gswip_hw_info {
243 int max_ports;
244 int cpu_port;
Aleksander Jan Bajkowskia09d0422021-03-22 21:37:15 +0100245 const struct dsa_switch_ops *ops;
Hauke Mehrtens14fceff2018-09-09 22:20:39 +0200246};
247
248struct xway_gphy_match_data {
249 char *fe_firmware_name;
250 char *ge_firmware_name;
251};
252
253struct gswip_gphy_fw {
254 struct clk *clk_gate;
255 struct reset_control *reset;
256 u32 fw_addr_offset;
257 char *fw_name;
258};
259
Hauke Mehrtens8206e0c2019-05-06 00:25:07 +0200260struct gswip_vlan {
261 struct net_device *bridge;
262 u16 vid;
263 u8 fid;
264};
265
Hauke Mehrtens14fceff2018-09-09 22:20:39 +0200266struct gswip_priv {
267 __iomem void *gswip;
268 __iomem void *mdio;
269 __iomem void *mii;
270 const struct gswip_hw_info *hw_info;
271 const struct xway_gphy_match_data *gphy_fw_name_cfg;
272 struct dsa_switch *ds;
273 struct device *dev;
274 struct regmap *rcu_regmap;
Hauke Mehrtens8206e0c2019-05-06 00:25:07 +0200275 struct gswip_vlan vlans[64];
Hauke Mehrtens14fceff2018-09-09 22:20:39 +0200276 int num_gphy_fw;
277 struct gswip_gphy_fw *gphy_fw;
Hauke Mehrtens9bbb1c02019-05-06 00:25:08 +0200278 u32 port_vlan_filter;
Vladimir Olteancf231b42021-10-24 20:17:53 +0300279 struct mutex pce_table_lock;
Hauke Mehrtens14fceff2018-09-09 22:20:39 +0200280};
281
Hauke Mehrtens8206e0c2019-05-06 00:25:07 +0200282struct gswip_pce_table_entry {
283 u16 index; // PCE_TBL_ADDR.ADDR = pData->table_index
284 u16 table; // PCE_TBL_CTRL.ADDR = pData->table
285 u16 key[8];
286 u16 val[5];
287 u16 mask;
288 u8 gmap;
289 bool type;
290 bool valid;
291 bool key_mode;
292};
293
Hauke Mehrtens14fceff2018-09-09 22:20:39 +0200294struct gswip_rmon_cnt_desc {
295 unsigned int size;
296 unsigned int offset;
297 const char *name;
298};
299
300#define MIB_DESC(_size, _offset, _name) {.size = _size, .offset = _offset, .name = _name}
301
302static const struct gswip_rmon_cnt_desc gswip_rmon_cnt[] = {
303 /** Receive Packet Count (only packets that are accepted and not discarded). */
304 MIB_DESC(1, 0x1F, "RxGoodPkts"),
305 MIB_DESC(1, 0x23, "RxUnicastPkts"),
306 MIB_DESC(1, 0x22, "RxMulticastPkts"),
307 MIB_DESC(1, 0x21, "RxFCSErrorPkts"),
308 MIB_DESC(1, 0x1D, "RxUnderSizeGoodPkts"),
309 MIB_DESC(1, 0x1E, "RxUnderSizeErrorPkts"),
310 MIB_DESC(1, 0x1B, "RxOversizeGoodPkts"),
311 MIB_DESC(1, 0x1C, "RxOversizeErrorPkts"),
312 MIB_DESC(1, 0x20, "RxGoodPausePkts"),
313 MIB_DESC(1, 0x1A, "RxAlignErrorPkts"),
314 MIB_DESC(1, 0x12, "Rx64BytePkts"),
315 MIB_DESC(1, 0x13, "Rx127BytePkts"),
316 MIB_DESC(1, 0x14, "Rx255BytePkts"),
317 MIB_DESC(1, 0x15, "Rx511BytePkts"),
318 MIB_DESC(1, 0x16, "Rx1023BytePkts"),
319 /** Receive Size 1024-1522 (or more, if configured) Packet Count. */
320 MIB_DESC(1, 0x17, "RxMaxBytePkts"),
321 MIB_DESC(1, 0x18, "RxDroppedPkts"),
322 MIB_DESC(1, 0x19, "RxFilteredPkts"),
323 MIB_DESC(2, 0x24, "RxGoodBytes"),
324 MIB_DESC(2, 0x26, "RxBadBytes"),
325 MIB_DESC(1, 0x11, "TxAcmDroppedPkts"),
326 MIB_DESC(1, 0x0C, "TxGoodPkts"),
327 MIB_DESC(1, 0x06, "TxUnicastPkts"),
328 MIB_DESC(1, 0x07, "TxMulticastPkts"),
329 MIB_DESC(1, 0x00, "Tx64BytePkts"),
330 MIB_DESC(1, 0x01, "Tx127BytePkts"),
331 MIB_DESC(1, 0x02, "Tx255BytePkts"),
332 MIB_DESC(1, 0x03, "Tx511BytePkts"),
333 MIB_DESC(1, 0x04, "Tx1023BytePkts"),
334 /** Transmit Size 1024-1522 (or more, if configured) Packet Count. */
335 MIB_DESC(1, 0x05, "TxMaxBytePkts"),
336 MIB_DESC(1, 0x08, "TxSingleCollCount"),
337 MIB_DESC(1, 0x09, "TxMultCollCount"),
338 MIB_DESC(1, 0x0A, "TxLateCollCount"),
339 MIB_DESC(1, 0x0B, "TxExcessCollCount"),
340 MIB_DESC(1, 0x0D, "TxPauseCount"),
341 MIB_DESC(1, 0x10, "TxDroppedPkts"),
342 MIB_DESC(2, 0x0E, "TxGoodBytes"),
343};
344
345static u32 gswip_switch_r(struct gswip_priv *priv, u32 offset)
346{
347 return __raw_readl(priv->gswip + (offset * 4));
348}
349
350static void gswip_switch_w(struct gswip_priv *priv, u32 val, u32 offset)
351{
352 __raw_writel(val, priv->gswip + (offset * 4));
353}
354
355static void gswip_switch_mask(struct gswip_priv *priv, u32 clear, u32 set,
356 u32 offset)
357{
358 u32 val = gswip_switch_r(priv, offset);
359
360 val &= ~(clear);
361 val |= set;
362 gswip_switch_w(priv, val, offset);
363}
364
365static u32 gswip_switch_r_timeout(struct gswip_priv *priv, u32 offset,
366 u32 cleared)
367{
368 u32 val;
369
370 return readx_poll_timeout(__raw_readl, priv->gswip + (offset * 4), val,
371 (val & cleared) == 0, 20, 50000);
372}
373
374static u32 gswip_mdio_r(struct gswip_priv *priv, u32 offset)
375{
376 return __raw_readl(priv->mdio + (offset * 4));
377}
378
379static void gswip_mdio_w(struct gswip_priv *priv, u32 val, u32 offset)
380{
381 __raw_writel(val, priv->mdio + (offset * 4));
382}
383
384static void gswip_mdio_mask(struct gswip_priv *priv, u32 clear, u32 set,
385 u32 offset)
386{
387 u32 val = gswip_mdio_r(priv, offset);
388
389 val &= ~(clear);
390 val |= set;
391 gswip_mdio_w(priv, val, offset);
392}
393
394static u32 gswip_mii_r(struct gswip_priv *priv, u32 offset)
395{
396 return __raw_readl(priv->mii + (offset * 4));
397}
398
399static void gswip_mii_w(struct gswip_priv *priv, u32 val, u32 offset)
400{
401 __raw_writel(val, priv->mii + (offset * 4));
402}
403
404static void gswip_mii_mask(struct gswip_priv *priv, u32 clear, u32 set,
405 u32 offset)
406{
407 u32 val = gswip_mii_r(priv, offset);
408
409 val &= ~(clear);
410 val |= set;
411 gswip_mii_w(priv, val, offset);
412}
413
414static void gswip_mii_mask_cfg(struct gswip_priv *priv, u32 clear, u32 set,
415 int port)
416{
Martin Blumenstingl709a3c92021-01-03 02:25:44 +0100417 /* There's no MII_CFG register for the CPU port */
418 if (!dsa_is_cpu_port(priv->ds, port))
419 gswip_mii_mask(priv, clear, set, GSWIP_MII_CFGp(port));
Hauke Mehrtens14fceff2018-09-09 22:20:39 +0200420}
421
422static void gswip_mii_mask_pcdu(struct gswip_priv *priv, u32 clear, u32 set,
423 int port)
424{
425 switch (port) {
426 case 0:
427 gswip_mii_mask(priv, clear, set, GSWIP_MII_PCDU0);
428 break;
429 case 1:
430 gswip_mii_mask(priv, clear, set, GSWIP_MII_PCDU1);
431 break;
432 case 5:
433 gswip_mii_mask(priv, clear, set, GSWIP_MII_PCDU5);
434 break;
435 }
436}
437
438static int gswip_mdio_poll(struct gswip_priv *priv)
439{
440 int cnt = 100;
441
442 while (likely(cnt--)) {
443 u32 ctrl = gswip_mdio_r(priv, GSWIP_MDIO_CTRL);
444
445 if ((ctrl & GSWIP_MDIO_CTRL_BUSY) == 0)
446 return 0;
447 usleep_range(20, 40);
448 }
449
450 return -ETIMEDOUT;
451}
452
453static int gswip_mdio_wr(struct mii_bus *bus, int addr, int reg, u16 val)
454{
455 struct gswip_priv *priv = bus->priv;
456 int err;
457
458 err = gswip_mdio_poll(priv);
459 if (err) {
460 dev_err(&bus->dev, "waiting for MDIO bus busy timed out\n");
461 return err;
462 }
463
464 gswip_mdio_w(priv, val, GSWIP_MDIO_WRITE);
465 gswip_mdio_w(priv, GSWIP_MDIO_CTRL_BUSY | GSWIP_MDIO_CTRL_WR |
466 ((addr & GSWIP_MDIO_CTRL_PHYAD_MASK) << GSWIP_MDIO_CTRL_PHYAD_SHIFT) |
467 (reg & GSWIP_MDIO_CTRL_REGAD_MASK),
468 GSWIP_MDIO_CTRL);
469
470 return 0;
471}
472
473static int gswip_mdio_rd(struct mii_bus *bus, int addr, int reg)
474{
475 struct gswip_priv *priv = bus->priv;
476 int err;
477
478 err = gswip_mdio_poll(priv);
479 if (err) {
480 dev_err(&bus->dev, "waiting for MDIO bus busy timed out\n");
481 return err;
482 }
483
484 gswip_mdio_w(priv, GSWIP_MDIO_CTRL_BUSY | GSWIP_MDIO_CTRL_RD |
485 ((addr & GSWIP_MDIO_CTRL_PHYAD_MASK) << GSWIP_MDIO_CTRL_PHYAD_SHIFT) |
486 (reg & GSWIP_MDIO_CTRL_REGAD_MASK),
487 GSWIP_MDIO_CTRL);
488
489 err = gswip_mdio_poll(priv);
490 if (err) {
491 dev_err(&bus->dev, "waiting for MDIO bus busy timed out\n");
492 return err;
493 }
494
495 return gswip_mdio_r(priv, GSWIP_MDIO_READ);
496}
497
498static int gswip_mdio(struct gswip_priv *priv, struct device_node *mdio_np)
499{
500 struct dsa_switch *ds = priv->ds;
501
502 ds->slave_mii_bus = devm_mdiobus_alloc(priv->dev);
503 if (!ds->slave_mii_bus)
504 return -ENOMEM;
505
506 ds->slave_mii_bus->priv = priv;
507 ds->slave_mii_bus->read = gswip_mdio_rd;
508 ds->slave_mii_bus->write = gswip_mdio_wr;
509 ds->slave_mii_bus->name = "lantiq,xrx200-mdio";
510 snprintf(ds->slave_mii_bus->id, MII_BUS_ID_SIZE, "%s-mii",
511 dev_name(priv->dev));
512 ds->slave_mii_bus->parent = priv->dev;
513 ds->slave_mii_bus->phy_mask = ~ds->phys_mii_mask;
514
515 return of_mdiobus_register(ds->slave_mii_bus, mdio_np);
516}
517
Hauke Mehrtens8206e0c2019-05-06 00:25:07 +0200518static int gswip_pce_table_entry_read(struct gswip_priv *priv,
519 struct gswip_pce_table_entry *tbl)
520{
521 int i;
522 int err;
523 u16 crtl;
524 u16 addr_mode = tbl->key_mode ? GSWIP_PCE_TBL_CTRL_OPMOD_KSRD :
525 GSWIP_PCE_TBL_CTRL_OPMOD_ADRD;
526
Vladimir Olteancf231b42021-10-24 20:17:53 +0300527 mutex_lock(&priv->pce_table_lock);
528
Hauke Mehrtens8206e0c2019-05-06 00:25:07 +0200529 err = gswip_switch_r_timeout(priv, GSWIP_PCE_TBL_CTRL,
530 GSWIP_PCE_TBL_CTRL_BAS);
Vladimir Olteancf231b42021-10-24 20:17:53 +0300531 if (err) {
532 mutex_unlock(&priv->pce_table_lock);
Hauke Mehrtens8206e0c2019-05-06 00:25:07 +0200533 return err;
Vladimir Olteancf231b42021-10-24 20:17:53 +0300534 }
Hauke Mehrtens8206e0c2019-05-06 00:25:07 +0200535
536 gswip_switch_w(priv, tbl->index, GSWIP_PCE_TBL_ADDR);
537 gswip_switch_mask(priv, GSWIP_PCE_TBL_CTRL_ADDR_MASK |
538 GSWIP_PCE_TBL_CTRL_OPMOD_MASK,
539 tbl->table | addr_mode | GSWIP_PCE_TBL_CTRL_BAS,
540 GSWIP_PCE_TBL_CTRL);
541
542 err = gswip_switch_r_timeout(priv, GSWIP_PCE_TBL_CTRL,
543 GSWIP_PCE_TBL_CTRL_BAS);
Vladimir Olteancf231b42021-10-24 20:17:53 +0300544 if (err) {
545 mutex_unlock(&priv->pce_table_lock);
Hauke Mehrtens8206e0c2019-05-06 00:25:07 +0200546 return err;
Vladimir Olteancf231b42021-10-24 20:17:53 +0300547 }
Hauke Mehrtens8206e0c2019-05-06 00:25:07 +0200548
549 for (i = 0; i < ARRAY_SIZE(tbl->key); i++)
550 tbl->key[i] = gswip_switch_r(priv, GSWIP_PCE_TBL_KEY(i));
551
552 for (i = 0; i < ARRAY_SIZE(tbl->val); i++)
553 tbl->val[i] = gswip_switch_r(priv, GSWIP_PCE_TBL_VAL(i));
554
555 tbl->mask = gswip_switch_r(priv, GSWIP_PCE_TBL_MASK);
556
557 crtl = gswip_switch_r(priv, GSWIP_PCE_TBL_CTRL);
558
559 tbl->type = !!(crtl & GSWIP_PCE_TBL_CTRL_TYPE);
560 tbl->valid = !!(crtl & GSWIP_PCE_TBL_CTRL_VLD);
561 tbl->gmap = (crtl & GSWIP_PCE_TBL_CTRL_GMAP_MASK) >> 7;
562
Vladimir Olteancf231b42021-10-24 20:17:53 +0300563 mutex_unlock(&priv->pce_table_lock);
564
Hauke Mehrtens8206e0c2019-05-06 00:25:07 +0200565 return 0;
566}
567
568static int gswip_pce_table_entry_write(struct gswip_priv *priv,
569 struct gswip_pce_table_entry *tbl)
570{
571 int i;
572 int err;
573 u16 crtl;
574 u16 addr_mode = tbl->key_mode ? GSWIP_PCE_TBL_CTRL_OPMOD_KSWR :
575 GSWIP_PCE_TBL_CTRL_OPMOD_ADWR;
576
Vladimir Olteancf231b42021-10-24 20:17:53 +0300577 mutex_lock(&priv->pce_table_lock);
578
Hauke Mehrtens8206e0c2019-05-06 00:25:07 +0200579 err = gswip_switch_r_timeout(priv, GSWIP_PCE_TBL_CTRL,
580 GSWIP_PCE_TBL_CTRL_BAS);
Vladimir Olteancf231b42021-10-24 20:17:53 +0300581 if (err) {
582 mutex_unlock(&priv->pce_table_lock);
Hauke Mehrtens8206e0c2019-05-06 00:25:07 +0200583 return err;
Vladimir Olteancf231b42021-10-24 20:17:53 +0300584 }
Hauke Mehrtens8206e0c2019-05-06 00:25:07 +0200585
586 gswip_switch_w(priv, tbl->index, GSWIP_PCE_TBL_ADDR);
587 gswip_switch_mask(priv, GSWIP_PCE_TBL_CTRL_ADDR_MASK |
588 GSWIP_PCE_TBL_CTRL_OPMOD_MASK,
589 tbl->table | addr_mode,
590 GSWIP_PCE_TBL_CTRL);
591
592 for (i = 0; i < ARRAY_SIZE(tbl->key); i++)
593 gswip_switch_w(priv, tbl->key[i], GSWIP_PCE_TBL_KEY(i));
594
595 for (i = 0; i < ARRAY_SIZE(tbl->val); i++)
596 gswip_switch_w(priv, tbl->val[i], GSWIP_PCE_TBL_VAL(i));
597
598 gswip_switch_mask(priv, GSWIP_PCE_TBL_CTRL_ADDR_MASK |
599 GSWIP_PCE_TBL_CTRL_OPMOD_MASK,
600 tbl->table | addr_mode,
601 GSWIP_PCE_TBL_CTRL);
602
603 gswip_switch_w(priv, tbl->mask, GSWIP_PCE_TBL_MASK);
604
605 crtl = gswip_switch_r(priv, GSWIP_PCE_TBL_CTRL);
606 crtl &= ~(GSWIP_PCE_TBL_CTRL_TYPE | GSWIP_PCE_TBL_CTRL_VLD |
607 GSWIP_PCE_TBL_CTRL_GMAP_MASK);
608 if (tbl->type)
609 crtl |= GSWIP_PCE_TBL_CTRL_TYPE;
610 if (tbl->valid)
611 crtl |= GSWIP_PCE_TBL_CTRL_VLD;
612 crtl |= (tbl->gmap << 7) & GSWIP_PCE_TBL_CTRL_GMAP_MASK;
613 crtl |= GSWIP_PCE_TBL_CTRL_BAS;
614 gswip_switch_w(priv, crtl, GSWIP_PCE_TBL_CTRL);
615
Vladimir Olteancf231b42021-10-24 20:17:53 +0300616 err = gswip_switch_r_timeout(priv, GSWIP_PCE_TBL_CTRL,
617 GSWIP_PCE_TBL_CTRL_BAS);
618
619 mutex_unlock(&priv->pce_table_lock);
620
621 return err;
Hauke Mehrtens8206e0c2019-05-06 00:25:07 +0200622}
623
624/* Add the LAN port into a bridge with the CPU port by
625 * default. This prevents automatic forwarding of
626 * packages between the LAN ports when no explicit
627 * bridge is configured.
628 */
629static int gswip_add_single_port_br(struct gswip_priv *priv, int port, bool add)
630{
631 struct gswip_pce_table_entry vlan_active = {0,};
632 struct gswip_pce_table_entry vlan_mapping = {0,};
633 unsigned int cpu_port = priv->hw_info->cpu_port;
634 unsigned int max_ports = priv->hw_info->max_ports;
635 int err;
636
637 if (port >= max_ports) {
638 dev_err(priv->dev, "single port for %i supported\n", port);
639 return -EIO;
640 }
641
642 vlan_active.index = port + 1;
643 vlan_active.table = GSWIP_TABLE_ACTIVE_VLAN;
644 vlan_active.key[0] = 0; /* vid */
645 vlan_active.val[0] = port + 1 /* fid */;
646 vlan_active.valid = add;
647 err = gswip_pce_table_entry_write(priv, &vlan_active);
648 if (err) {
649 dev_err(priv->dev, "failed to write active VLAN: %d\n", err);
650 return err;
651 }
652
653 if (!add)
654 return 0;
655
656 vlan_mapping.index = port + 1;
657 vlan_mapping.table = GSWIP_TABLE_VLAN_MAPPING;
658 vlan_mapping.val[0] = 0 /* vid */;
659 vlan_mapping.val[1] = BIT(port) | BIT(cpu_port);
660 vlan_mapping.val[2] = 0;
661 err = gswip_pce_table_entry_write(priv, &vlan_mapping);
662 if (err) {
663 dev_err(priv->dev, "failed to write VLAN mapping: %d\n", err);
664 return err;
665 }
666
667 return 0;
668}
669
Hauke Mehrtens14fceff2018-09-09 22:20:39 +0200670static int gswip_port_enable(struct dsa_switch *ds, int port,
671 struct phy_device *phydev)
672{
673 struct gswip_priv *priv = ds->priv;
Hauke Mehrtens8206e0c2019-05-06 00:25:07 +0200674 int err;
675
Vivien Didelot74be4ba2019-08-19 16:00:49 -0400676 if (!dsa_is_user_port(ds, port))
677 return 0;
678
Hauke Mehrtens8206e0c2019-05-06 00:25:07 +0200679 if (!dsa_is_cpu_port(ds, port)) {
680 err = gswip_add_single_port_br(priv, port, true);
681 if (err)
682 return err;
683 }
Hauke Mehrtens14fceff2018-09-09 22:20:39 +0200684
685 /* RMON Counter Enable for port */
686 gswip_switch_w(priv, GSWIP_BM_PCFG_CNTEN, GSWIP_BM_PCFGp(port));
687
688 /* enable port fetch/store dma & VLAN Modification */
689 gswip_switch_mask(priv, 0, GSWIP_FDMA_PCTRL_EN |
690 GSWIP_FDMA_PCTRL_VLANMOD_BOTH,
691 GSWIP_FDMA_PCTRLp(port));
692 gswip_switch_mask(priv, 0, GSWIP_SDMA_PCTRL_EN,
693 GSWIP_SDMA_PCTRLp(port));
Hauke Mehrtens14fceff2018-09-09 22:20:39 +0200694
695 if (!dsa_is_cpu_port(ds, port)) {
Martin Blumenstingl3e9005b2021-04-08 20:38:27 +0200696 u32 mdio_phy = 0;
Hauke Mehrtens14fceff2018-09-09 22:20:39 +0200697
Martin Blumenstingl3e9005b2021-04-08 20:38:27 +0200698 if (phydev)
699 mdio_phy = phydev->mdio.addr & GSWIP_MDIO_PHY_ADDR_MASK;
700
701 gswip_mdio_mask(priv, GSWIP_MDIO_PHY_ADDR_MASK, mdio_phy,
702 GSWIP_MDIO_PHYp(port));
Hauke Mehrtens14fceff2018-09-09 22:20:39 +0200703 }
704
705 return 0;
706}
707
Andrew Lunn75104db2019-02-24 20:44:43 +0100708static void gswip_port_disable(struct dsa_switch *ds, int port)
Hauke Mehrtens14fceff2018-09-09 22:20:39 +0200709{
710 struct gswip_priv *priv = ds->priv;
711
Vivien Didelot74be4ba2019-08-19 16:00:49 -0400712 if (!dsa_is_user_port(ds, port))
713 return;
714
Hauke Mehrtens14fceff2018-09-09 22:20:39 +0200715 gswip_switch_mask(priv, GSWIP_FDMA_PCTRL_EN, 0,
716 GSWIP_FDMA_PCTRLp(port));
717 gswip_switch_mask(priv, GSWIP_SDMA_PCTRL_EN, 0,
718 GSWIP_SDMA_PCTRLp(port));
719}
720
721static int gswip_pce_load_microcode(struct gswip_priv *priv)
722{
723 int i;
724 int err;
725
726 gswip_switch_mask(priv, GSWIP_PCE_TBL_CTRL_ADDR_MASK |
727 GSWIP_PCE_TBL_CTRL_OPMOD_MASK,
728 GSWIP_PCE_TBL_CTRL_OPMOD_ADWR, GSWIP_PCE_TBL_CTRL);
729 gswip_switch_w(priv, 0, GSWIP_PCE_TBL_MASK);
730
731 for (i = 0; i < ARRAY_SIZE(gswip_pce_microcode); i++) {
732 gswip_switch_w(priv, i, GSWIP_PCE_TBL_ADDR);
733 gswip_switch_w(priv, gswip_pce_microcode[i].val_0,
734 GSWIP_PCE_TBL_VAL(0));
735 gswip_switch_w(priv, gswip_pce_microcode[i].val_1,
736 GSWIP_PCE_TBL_VAL(1));
737 gswip_switch_w(priv, gswip_pce_microcode[i].val_2,
738 GSWIP_PCE_TBL_VAL(2));
739 gswip_switch_w(priv, gswip_pce_microcode[i].val_3,
740 GSWIP_PCE_TBL_VAL(3));
741
742 /* start the table access: */
743 gswip_switch_mask(priv, 0, GSWIP_PCE_TBL_CTRL_BAS,
744 GSWIP_PCE_TBL_CTRL);
745 err = gswip_switch_r_timeout(priv, GSWIP_PCE_TBL_CTRL,
746 GSWIP_PCE_TBL_CTRL_BAS);
747 if (err)
748 return err;
749 }
750
751 /* tell the switch that the microcode is loaded */
752 gswip_switch_mask(priv, 0, GSWIP_PCE_GCTRL_0_MC_VALID,
753 GSWIP_PCE_GCTRL_0);
754
755 return 0;
756}
757
Hauke Mehrtens8206e0c2019-05-06 00:25:07 +0200758static int gswip_port_vlan_filtering(struct dsa_switch *ds, int port,
Vladimir Oltean89153ed2021-02-13 22:43:19 +0200759 bool vlan_filtering,
760 struct netlink_ext_ack *extack)
Hauke Mehrtens8206e0c2019-05-06 00:25:07 +0200761{
Vladimir Oltean41fb0cf2021-12-06 18:57:53 +0200762 struct net_device *bridge = dsa_port_bridge_dev_get(dsa_to_port(ds, port));
Hauke Mehrtens8206e0c2019-05-06 00:25:07 +0200763 struct gswip_priv *priv = ds->priv;
Hauke Mehrtens9bbb1c02019-05-06 00:25:08 +0200764
765 /* Do not allow changing the VLAN filtering options while in bridge */
Vladimir Oltean89153ed2021-02-13 22:43:19 +0200766 if (bridge && !!(priv->port_vlan_filter & BIT(port)) != vlan_filtering) {
767 NL_SET_ERR_MSG_MOD(extack,
768 "Dynamic toggling of vlan_filtering not supported");
Vladimir Olteanbae33f22021-01-09 02:01:50 +0200769 return -EIO;
Vladimir Oltean89153ed2021-02-13 22:43:19 +0200770 }
Hauke Mehrtens8206e0c2019-05-06 00:25:07 +0200771
772 if (vlan_filtering) {
773 /* Use port based VLAN tag */
774 gswip_switch_mask(priv,
775 GSWIP_PCE_VCTRL_VSR,
776 GSWIP_PCE_VCTRL_UVR | GSWIP_PCE_VCTRL_VIMR |
777 GSWIP_PCE_VCTRL_VEMR,
778 GSWIP_PCE_VCTRL(port));
779 gswip_switch_mask(priv, GSWIP_PCE_PCTRL_0_TVM, 0,
780 GSWIP_PCE_PCTRL_0p(port));
781 } else {
782 /* Use port based VLAN tag */
783 gswip_switch_mask(priv,
784 GSWIP_PCE_VCTRL_UVR | GSWIP_PCE_VCTRL_VIMR |
785 GSWIP_PCE_VCTRL_VEMR,
786 GSWIP_PCE_VCTRL_VSR,
787 GSWIP_PCE_VCTRL(port));
788 gswip_switch_mask(priv, 0, GSWIP_PCE_PCTRL_0_TVM,
789 GSWIP_PCE_PCTRL_0p(port));
790 }
791
792 return 0;
793}
794
Hauke Mehrtens14fceff2018-09-09 22:20:39 +0200795static int gswip_setup(struct dsa_switch *ds)
796{
797 struct gswip_priv *priv = ds->priv;
798 unsigned int cpu_port = priv->hw_info->cpu_port;
799 int i;
800 int err;
801
802 gswip_switch_w(priv, GSWIP_SWRES_R0, GSWIP_SWRES);
803 usleep_range(5000, 10000);
804 gswip_switch_w(priv, 0, GSWIP_SWRES);
805
806 /* disable port fetch/store dma on all ports */
Hauke Mehrtens8206e0c2019-05-06 00:25:07 +0200807 for (i = 0; i < priv->hw_info->max_ports; i++) {
Andrew Lunn75104db2019-02-24 20:44:43 +0100808 gswip_port_disable(ds, i);
Vladimir Oltean89153ed2021-02-13 22:43:19 +0200809 gswip_port_vlan_filtering(ds, i, false, NULL);
Hauke Mehrtens8206e0c2019-05-06 00:25:07 +0200810 }
Hauke Mehrtens14fceff2018-09-09 22:20:39 +0200811
812 /* enable Switch */
813 gswip_mdio_mask(priv, 0, GSWIP_MDIO_GLOB_ENABLE, GSWIP_MDIO_GLOB);
814
815 err = gswip_pce_load_microcode(priv);
816 if (err) {
817 dev_err(priv->dev, "writing PCE microcode failed, %i", err);
818 return err;
819 }
820
821 /* Default unknown Broadcast/Multicast/Unicast port maps */
822 gswip_switch_w(priv, BIT(cpu_port), GSWIP_PCE_PMAP1);
823 gswip_switch_w(priv, BIT(cpu_port), GSWIP_PCE_PMAP2);
824 gswip_switch_w(priv, BIT(cpu_port), GSWIP_PCE_PMAP3);
825
Martin Blumenstingl3e9005b2021-04-08 20:38:27 +0200826 /* Deactivate MDIO PHY auto polling. Some PHYs as the AR8030 have an
827 * interoperability problem with this auto polling mechanism because
828 * their status registers think that the link is in a different state
829 * than it actually is. For the AR8030 it has the BMSR_ESTATEN bit set
830 * as well as ESTATUS_1000_TFULL and ESTATUS_1000_XFULL. This makes the
831 * auto polling state machine consider the link being negotiated with
832 * 1Gbit/s. Since the PHY itself is a Fast Ethernet RMII PHY this leads
833 * to the switch port being completely dead (RX and TX are both not
834 * working).
835 * Also with various other PHY / port combinations (PHY11G GPHY, PHY22F
836 * GPHY, external RGMII PEF7071/7072) any traffic would stop. Sometimes
837 * it would work fine for a few minutes to hours and then stop, on
838 * other device it would no traffic could be sent or received at all.
839 * Testing shows that when PHY auto polling is disabled these problems
840 * go away.
841 */
Hauke Mehrtens14fceff2018-09-09 22:20:39 +0200842 gswip_mdio_w(priv, 0x0, GSWIP_MDIO_MDC_CFG0);
Martin Blumenstingl3e9005b2021-04-08 20:38:27 +0200843
Hauke Mehrtens14fceff2018-09-09 22:20:39 +0200844 /* Configure the MDIO Clock 2.5 MHz */
845 gswip_mdio_mask(priv, 0xff, 0x09, GSWIP_MDIO_MDC_CFG1);
846
Martin Blumenstingl4b592322021-04-08 20:38:28 +0200847 /* Disable the xMII interface and clear it's isolation bit */
Martin Blumenstingl709a3c92021-01-03 02:25:44 +0100848 for (i = 0; i < priv->hw_info->max_ports; i++)
Martin Blumenstingl4b592322021-04-08 20:38:28 +0200849 gswip_mii_mask_cfg(priv,
850 GSWIP_MII_CFG_EN | GSWIP_MII_CFG_ISOLATE,
851 0, i);
Hauke Mehrtens14fceff2018-09-09 22:20:39 +0200852
853 /* enable special tag insertion on cpu port */
854 gswip_switch_mask(priv, 0, GSWIP_FDMA_PCTRL_STEN,
855 GSWIP_FDMA_PCTRLp(cpu_port));
856
Hauke Mehrtens30d893832019-05-06 00:25:06 +0200857 /* accept special tag in ingress direction */
858 gswip_switch_mask(priv, 0, GSWIP_PCE_PCTRL_0_INGRESS,
859 GSWIP_PCE_PCTRL_0p(cpu_port));
860
Hauke Mehrtens14fceff2018-09-09 22:20:39 +0200861 gswip_switch_mask(priv, 0, GSWIP_MAC_CTRL_2_MLEN,
862 GSWIP_MAC_CTRL_2p(cpu_port));
Jan Hoffmann552799f2021-09-01 20:49:33 +0200863 gswip_switch_w(priv, VLAN_ETH_FRAME_LEN + 8 + ETH_FCS_LEN,
864 GSWIP_MAC_FLEN);
Hauke Mehrtens14fceff2018-09-09 22:20:39 +0200865 gswip_switch_mask(priv, 0, GSWIP_BM_QUEUE_GCTRL_GL_MOD,
866 GSWIP_BM_QUEUE_GCTRL);
867
868 /* VLAN aware Switching */
869 gswip_switch_mask(priv, 0, GSWIP_PCE_GCTRL_0_VLAN, GSWIP_PCE_GCTRL_0);
870
Hauke Mehrtens8206e0c2019-05-06 00:25:07 +0200871 /* Flush MAC Table */
872 gswip_switch_mask(priv, 0, GSWIP_PCE_GCTRL_0_MTFL, GSWIP_PCE_GCTRL_0);
873
874 err = gswip_switch_r_timeout(priv, GSWIP_PCE_GCTRL_0,
875 GSWIP_PCE_GCTRL_0_MTFL);
876 if (err) {
877 dev_err(priv->dev, "MAC flushing didn't finish\n");
878 return err;
879 }
Hauke Mehrtens14fceff2018-09-09 22:20:39 +0200880
881 gswip_port_enable(ds, cpu_port, NULL);
Vladimir Oltean0ee2af42021-01-16 01:19:19 +0200882
883 ds->configure_vlan_while_not_filtering = false;
884
Hauke Mehrtens14fceff2018-09-09 22:20:39 +0200885 return 0;
886}
887
888static enum dsa_tag_protocol gswip_get_tag_protocol(struct dsa_switch *ds,
Florian Fainelli4d776482020-01-07 21:06:05 -0800889 int port,
890 enum dsa_tag_protocol mp)
Hauke Mehrtens14fceff2018-09-09 22:20:39 +0200891{
892 return DSA_TAG_PROTO_GSWIP;
893}
894
Hauke Mehrtens8206e0c2019-05-06 00:25:07 +0200895static int gswip_vlan_active_create(struct gswip_priv *priv,
896 struct net_device *bridge,
897 int fid, u16 vid)
898{
899 struct gswip_pce_table_entry vlan_active = {0,};
900 unsigned int max_ports = priv->hw_info->max_ports;
901 int idx = -1;
902 int err;
903 int i;
904
905 /* Look for a free slot */
906 for (i = max_ports; i < ARRAY_SIZE(priv->vlans); i++) {
907 if (!priv->vlans[i].bridge) {
908 idx = i;
909 break;
910 }
911 }
912
913 if (idx == -1)
914 return -ENOSPC;
915
916 if (fid == -1)
917 fid = idx;
918
919 vlan_active.index = idx;
920 vlan_active.table = GSWIP_TABLE_ACTIVE_VLAN;
921 vlan_active.key[0] = vid;
922 vlan_active.val[0] = fid;
923 vlan_active.valid = true;
924
925 err = gswip_pce_table_entry_write(priv, &vlan_active);
926 if (err) {
927 dev_err(priv->dev, "failed to write active VLAN: %d\n", err);
928 return err;
929 }
930
931 priv->vlans[idx].bridge = bridge;
932 priv->vlans[idx].vid = vid;
933 priv->vlans[idx].fid = fid;
934
935 return idx;
936}
937
938static int gswip_vlan_active_remove(struct gswip_priv *priv, int idx)
939{
940 struct gswip_pce_table_entry vlan_active = {0,};
941 int err;
942
943 vlan_active.index = idx;
944 vlan_active.table = GSWIP_TABLE_ACTIVE_VLAN;
945 vlan_active.valid = false;
946 err = gswip_pce_table_entry_write(priv, &vlan_active);
947 if (err)
948 dev_err(priv->dev, "failed to delete active VLAN: %d\n", err);
949 priv->vlans[idx].bridge = NULL;
950
951 return err;
952}
953
954static int gswip_vlan_add_unaware(struct gswip_priv *priv,
955 struct net_device *bridge, int port)
956{
957 struct gswip_pce_table_entry vlan_mapping = {0,};
958 unsigned int max_ports = priv->hw_info->max_ports;
959 unsigned int cpu_port = priv->hw_info->cpu_port;
960 bool active_vlan_created = false;
961 int idx = -1;
962 int i;
963 int err;
964
965 /* Check if there is already a page for this bridge */
966 for (i = max_ports; i < ARRAY_SIZE(priv->vlans); i++) {
967 if (priv->vlans[i].bridge == bridge) {
968 idx = i;
969 break;
970 }
971 }
972
973 /* If this bridge is not programmed yet, add a Active VLAN table
974 * entry in a free slot and prepare the VLAN mapping table entry.
975 */
976 if (idx == -1) {
977 idx = gswip_vlan_active_create(priv, bridge, -1, 0);
978 if (idx < 0)
979 return idx;
980 active_vlan_created = true;
981
982 vlan_mapping.index = idx;
983 vlan_mapping.table = GSWIP_TABLE_VLAN_MAPPING;
984 /* VLAN ID byte, maps to the VLAN ID of vlan active table */
985 vlan_mapping.val[0] = 0;
986 } else {
987 /* Read the existing VLAN mapping entry from the switch */
988 vlan_mapping.index = idx;
989 vlan_mapping.table = GSWIP_TABLE_VLAN_MAPPING;
990 err = gswip_pce_table_entry_read(priv, &vlan_mapping);
991 if (err) {
992 dev_err(priv->dev, "failed to read VLAN mapping: %d\n",
993 err);
994 return err;
995 }
996 }
997
998 /* Update the VLAN mapping entry and write it to the switch */
999 vlan_mapping.val[1] |= BIT(cpu_port);
1000 vlan_mapping.val[1] |= BIT(port);
1001 err = gswip_pce_table_entry_write(priv, &vlan_mapping);
1002 if (err) {
1003 dev_err(priv->dev, "failed to write VLAN mapping: %d\n", err);
1004 /* In case an Active VLAN was creaetd delete it again */
1005 if (active_vlan_created)
1006 gswip_vlan_active_remove(priv, idx);
1007 return err;
1008 }
1009
1010 gswip_switch_w(priv, 0, GSWIP_PCE_DEFPVID(port));
1011 return 0;
1012}
1013
Hauke Mehrtens9bbb1c02019-05-06 00:25:08 +02001014static int gswip_vlan_add_aware(struct gswip_priv *priv,
1015 struct net_device *bridge, int port,
1016 u16 vid, bool untagged,
1017 bool pvid)
1018{
1019 struct gswip_pce_table_entry vlan_mapping = {0,};
1020 unsigned int max_ports = priv->hw_info->max_ports;
1021 unsigned int cpu_port = priv->hw_info->cpu_port;
1022 bool active_vlan_created = false;
1023 int idx = -1;
1024 int fid = -1;
1025 int i;
1026 int err;
1027
1028 /* Check if there is already a page for this bridge */
1029 for (i = max_ports; i < ARRAY_SIZE(priv->vlans); i++) {
1030 if (priv->vlans[i].bridge == bridge) {
1031 if (fid != -1 && fid != priv->vlans[i].fid)
1032 dev_err(priv->dev, "one bridge with multiple flow ids\n");
1033 fid = priv->vlans[i].fid;
1034 if (priv->vlans[i].vid == vid) {
1035 idx = i;
1036 break;
1037 }
1038 }
1039 }
1040
1041 /* If this bridge is not programmed yet, add a Active VLAN table
1042 * entry in a free slot and prepare the VLAN mapping table entry.
1043 */
1044 if (idx == -1) {
1045 idx = gswip_vlan_active_create(priv, bridge, fid, vid);
1046 if (idx < 0)
1047 return idx;
1048 active_vlan_created = true;
1049
1050 vlan_mapping.index = idx;
1051 vlan_mapping.table = GSWIP_TABLE_VLAN_MAPPING;
1052 /* VLAN ID byte, maps to the VLAN ID of vlan active table */
1053 vlan_mapping.val[0] = vid;
1054 } else {
1055 /* Read the existing VLAN mapping entry from the switch */
1056 vlan_mapping.index = idx;
1057 vlan_mapping.table = GSWIP_TABLE_VLAN_MAPPING;
1058 err = gswip_pce_table_entry_read(priv, &vlan_mapping);
1059 if (err) {
1060 dev_err(priv->dev, "failed to read VLAN mapping: %d\n",
1061 err);
1062 return err;
1063 }
1064 }
1065
1066 vlan_mapping.val[0] = vid;
1067 /* Update the VLAN mapping entry and write it to the switch */
1068 vlan_mapping.val[1] |= BIT(cpu_port);
1069 vlan_mapping.val[2] |= BIT(cpu_port);
1070 vlan_mapping.val[1] |= BIT(port);
1071 if (untagged)
1072 vlan_mapping.val[2] &= ~BIT(port);
1073 else
1074 vlan_mapping.val[2] |= BIT(port);
1075 err = gswip_pce_table_entry_write(priv, &vlan_mapping);
1076 if (err) {
1077 dev_err(priv->dev, "failed to write VLAN mapping: %d\n", err);
1078 /* In case an Active VLAN was creaetd delete it again */
1079 if (active_vlan_created)
1080 gswip_vlan_active_remove(priv, idx);
1081 return err;
1082 }
1083
1084 if (pvid)
1085 gswip_switch_w(priv, idx, GSWIP_PCE_DEFPVID(port));
1086
1087 return 0;
1088}
1089
Hauke Mehrtens8206e0c2019-05-06 00:25:07 +02001090static int gswip_vlan_remove(struct gswip_priv *priv,
1091 struct net_device *bridge, int port,
1092 u16 vid, bool pvid, bool vlan_aware)
1093{
1094 struct gswip_pce_table_entry vlan_mapping = {0,};
1095 unsigned int max_ports = priv->hw_info->max_ports;
1096 unsigned int cpu_port = priv->hw_info->cpu_port;
1097 int idx = -1;
1098 int i;
1099 int err;
1100
1101 /* Check if there is already a page for this bridge */
1102 for (i = max_ports; i < ARRAY_SIZE(priv->vlans); i++) {
1103 if (priv->vlans[i].bridge == bridge &&
1104 (!vlan_aware || priv->vlans[i].vid == vid)) {
1105 idx = i;
1106 break;
1107 }
1108 }
1109
1110 if (idx == -1) {
1111 dev_err(priv->dev, "bridge to leave does not exists\n");
1112 return -ENOENT;
1113 }
1114
1115 vlan_mapping.index = idx;
1116 vlan_mapping.table = GSWIP_TABLE_VLAN_MAPPING;
1117 err = gswip_pce_table_entry_read(priv, &vlan_mapping);
1118 if (err) {
1119 dev_err(priv->dev, "failed to read VLAN mapping: %d\n", err);
1120 return err;
1121 }
1122
1123 vlan_mapping.val[1] &= ~BIT(port);
1124 vlan_mapping.val[2] &= ~BIT(port);
1125 err = gswip_pce_table_entry_write(priv, &vlan_mapping);
1126 if (err) {
1127 dev_err(priv->dev, "failed to write VLAN mapping: %d\n", err);
1128 return err;
1129 }
1130
1131 /* In case all ports are removed from the bridge, remove the VLAN */
1132 if ((vlan_mapping.val[1] & ~BIT(cpu_port)) == 0) {
1133 err = gswip_vlan_active_remove(priv, idx);
1134 if (err) {
1135 dev_err(priv->dev, "failed to write active VLAN: %d\n",
1136 err);
1137 return err;
1138 }
1139 }
1140
1141 /* GSWIP 2.2 (GRX300) and later program here the VID directly. */
1142 if (pvid)
1143 gswip_switch_w(priv, 0, GSWIP_PCE_DEFPVID(port));
1144
1145 return 0;
1146}
1147
1148static int gswip_port_bridge_join(struct dsa_switch *ds, int port,
Vladimir Olteand3eed0e2021-12-06 18:57:56 +02001149 struct dsa_bridge bridge)
Hauke Mehrtens8206e0c2019-05-06 00:25:07 +02001150{
Vladimir Olteand3eed0e2021-12-06 18:57:56 +02001151 struct net_device *br = bridge.dev;
Hauke Mehrtens8206e0c2019-05-06 00:25:07 +02001152 struct gswip_priv *priv = ds->priv;
1153 int err;
1154
Hauke Mehrtens9bbb1c02019-05-06 00:25:08 +02001155 /* When the bridge uses VLAN filtering we have to configure VLAN
1156 * specific bridges. No bridge is configured here.
1157 */
Vladimir Olteand3eed0e2021-12-06 18:57:56 +02001158 if (!br_vlan_enabled(br)) {
1159 err = gswip_vlan_add_unaware(priv, br, port);
Hauke Mehrtens9bbb1c02019-05-06 00:25:08 +02001160 if (err)
1161 return err;
1162 priv->port_vlan_filter &= ~BIT(port);
1163 } else {
1164 priv->port_vlan_filter |= BIT(port);
1165 }
Hauke Mehrtens8206e0c2019-05-06 00:25:07 +02001166 return gswip_add_single_port_br(priv, port, false);
1167}
1168
1169static void gswip_port_bridge_leave(struct dsa_switch *ds, int port,
Vladimir Olteand3eed0e2021-12-06 18:57:56 +02001170 struct dsa_bridge bridge)
Hauke Mehrtens8206e0c2019-05-06 00:25:07 +02001171{
Vladimir Olteand3eed0e2021-12-06 18:57:56 +02001172 struct net_device *br = bridge.dev;
Hauke Mehrtens8206e0c2019-05-06 00:25:07 +02001173 struct gswip_priv *priv = ds->priv;
1174
1175 gswip_add_single_port_br(priv, port, true);
1176
Hauke Mehrtens9bbb1c02019-05-06 00:25:08 +02001177 /* When the bridge uses VLAN filtering we have to configure VLAN
1178 * specific bridges. No bridge is configured here.
1179 */
Vladimir Olteand3eed0e2021-12-06 18:57:56 +02001180 if (!br_vlan_enabled(br))
1181 gswip_vlan_remove(priv, br, port, 0, true, false);
Hauke Mehrtens9bbb1c02019-05-06 00:25:08 +02001182}
1183
1184static int gswip_port_vlan_prepare(struct dsa_switch *ds, int port,
Vladimir Oltean31046a52021-02-13 22:43:18 +02001185 const struct switchdev_obj_port_vlan *vlan,
1186 struct netlink_ext_ack *extack)
Hauke Mehrtens9bbb1c02019-05-06 00:25:08 +02001187{
Vladimir Oltean41fb0cf2021-12-06 18:57:53 +02001188 struct net_device *bridge = dsa_port_bridge_dev_get(dsa_to_port(ds, port));
Hauke Mehrtens9bbb1c02019-05-06 00:25:08 +02001189 struct gswip_priv *priv = ds->priv;
Hauke Mehrtens9bbb1c02019-05-06 00:25:08 +02001190 unsigned int max_ports = priv->hw_info->max_ports;
Hauke Mehrtens9bbb1c02019-05-06 00:25:08 +02001191 int pos = max_ports;
Vladimir Olteanb7a9e0d2021-01-09 02:01:46 +02001192 int i, idx = -1;
Hauke Mehrtens9bbb1c02019-05-06 00:25:08 +02001193
1194 /* We only support VLAN filtering on bridges */
1195 if (!dsa_is_cpu_port(ds, port) && !bridge)
1196 return -EOPNOTSUPP;
1197
Vladimir Olteanb7a9e0d2021-01-09 02:01:46 +02001198 /* Check if there is already a page for this VLAN */
1199 for (i = max_ports; i < ARRAY_SIZE(priv->vlans); i++) {
1200 if (priv->vlans[i].bridge == bridge &&
1201 priv->vlans[i].vid == vlan->vid) {
1202 idx = i;
1203 break;
1204 }
1205 }
Hauke Mehrtens9bbb1c02019-05-06 00:25:08 +02001206
Vladimir Olteanb7a9e0d2021-01-09 02:01:46 +02001207 /* If this VLAN is not programmed yet, we have to reserve
1208 * one entry in the VLAN table. Make sure we start at the
1209 * next position round.
1210 */
1211 if (idx == -1) {
1212 /* Look for a free slot */
1213 for (; pos < ARRAY_SIZE(priv->vlans); pos++) {
1214 if (!priv->vlans[pos].bridge) {
1215 idx = pos;
1216 pos++;
Hauke Mehrtens9bbb1c02019-05-06 00:25:08 +02001217 break;
1218 }
1219 }
1220
Vladimir Oltean31046a52021-02-13 22:43:18 +02001221 if (idx == -1) {
1222 NL_SET_ERR_MSG_MOD(extack, "No slot in VLAN table");
Vladimir Olteanb7a9e0d2021-01-09 02:01:46 +02001223 return -ENOSPC;
Vladimir Oltean31046a52021-02-13 22:43:18 +02001224 }
Hauke Mehrtens9bbb1c02019-05-06 00:25:08 +02001225 }
1226
1227 return 0;
1228}
1229
Vladimir Oltean1958d582021-01-09 02:01:53 +02001230static int gswip_port_vlan_add(struct dsa_switch *ds, int port,
Vladimir Oltean31046a52021-02-13 22:43:18 +02001231 const struct switchdev_obj_port_vlan *vlan,
1232 struct netlink_ext_ack *extack)
Hauke Mehrtens9bbb1c02019-05-06 00:25:08 +02001233{
Vladimir Oltean41fb0cf2021-12-06 18:57:53 +02001234 struct net_device *bridge = dsa_port_bridge_dev_get(dsa_to_port(ds, port));
Hauke Mehrtens9bbb1c02019-05-06 00:25:08 +02001235 struct gswip_priv *priv = ds->priv;
Hauke Mehrtens9bbb1c02019-05-06 00:25:08 +02001236 bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
1237 bool pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
Vladimir Oltean1958d582021-01-09 02:01:53 +02001238 int err;
1239
Vladimir Oltean31046a52021-02-13 22:43:18 +02001240 err = gswip_port_vlan_prepare(ds, port, vlan, extack);
Vladimir Oltean1958d582021-01-09 02:01:53 +02001241 if (err)
1242 return err;
Hauke Mehrtens9bbb1c02019-05-06 00:25:08 +02001243
1244 /* We have to receive all packets on the CPU port and should not
1245 * do any VLAN filtering here. This is also called with bridge
1246 * NULL and then we do not know for which bridge to configure
1247 * this.
1248 */
1249 if (dsa_is_cpu_port(ds, port))
Vladimir Oltean1958d582021-01-09 02:01:53 +02001250 return 0;
Hauke Mehrtens9bbb1c02019-05-06 00:25:08 +02001251
Vladimir Oltean1958d582021-01-09 02:01:53 +02001252 return gswip_vlan_add_aware(priv, bridge, port, vlan->vid,
1253 untagged, pvid);
Hauke Mehrtens9bbb1c02019-05-06 00:25:08 +02001254}
1255
1256static int gswip_port_vlan_del(struct dsa_switch *ds, int port,
1257 const struct switchdev_obj_port_vlan *vlan)
1258{
Vladimir Oltean41fb0cf2021-12-06 18:57:53 +02001259 struct net_device *bridge = dsa_port_bridge_dev_get(dsa_to_port(ds, port));
Hauke Mehrtens9bbb1c02019-05-06 00:25:08 +02001260 struct gswip_priv *priv = ds->priv;
Hauke Mehrtens9bbb1c02019-05-06 00:25:08 +02001261 bool pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
Hauke Mehrtens9bbb1c02019-05-06 00:25:08 +02001262
1263 /* We have to receive all packets on the CPU port and should not
1264 * do any VLAN filtering here. This is also called with bridge
1265 * NULL and then we do not know for which bridge to configure
1266 * this.
1267 */
1268 if (dsa_is_cpu_port(ds, port))
1269 return 0;
1270
Vladimir Olteanb7a9e0d2021-01-09 02:01:46 +02001271 return gswip_vlan_remove(priv, bridge, port, vlan->vid, pvid, true);
Hauke Mehrtens8206e0c2019-05-06 00:25:07 +02001272}
1273
Hauke Mehrtens45813482019-05-06 00:25:09 +02001274static void gswip_port_fast_age(struct dsa_switch *ds, int port)
1275{
1276 struct gswip_priv *priv = ds->priv;
1277 struct gswip_pce_table_entry mac_bridge = {0,};
1278 int i;
1279 int err;
1280
1281 for (i = 0; i < 2048; i++) {
1282 mac_bridge.table = GSWIP_TABLE_MAC_BRIDGE;
1283 mac_bridge.index = i;
1284
1285 err = gswip_pce_table_entry_read(priv, &mac_bridge);
1286 if (err) {
Colin Ian Kingd6759172019-05-08 11:22:09 +01001287 dev_err(priv->dev, "failed to read mac bridge: %d\n",
Hauke Mehrtens45813482019-05-06 00:25:09 +02001288 err);
1289 return;
1290 }
1291
1292 if (!mac_bridge.valid)
1293 continue;
1294
1295 if (mac_bridge.val[1] & GSWIP_TABLE_MAC_BRIDGE_STATIC)
1296 continue;
1297
1298 if (((mac_bridge.val[0] & GENMASK(7, 4)) >> 4) != port)
1299 continue;
1300
1301 mac_bridge.valid = false;
1302 err = gswip_pce_table_entry_write(priv, &mac_bridge);
1303 if (err) {
Colin Ian Kingd6759172019-05-08 11:22:09 +01001304 dev_err(priv->dev, "failed to write mac bridge: %d\n",
Hauke Mehrtens45813482019-05-06 00:25:09 +02001305 err);
1306 return;
1307 }
1308 }
1309}
1310
Hauke Mehrtens8206e0c2019-05-06 00:25:07 +02001311static void gswip_port_stp_state_set(struct dsa_switch *ds, int port, u8 state)
1312{
1313 struct gswip_priv *priv = ds->priv;
1314 u32 stp_state;
1315
1316 switch (state) {
1317 case BR_STATE_DISABLED:
1318 gswip_switch_mask(priv, GSWIP_SDMA_PCTRL_EN, 0,
1319 GSWIP_SDMA_PCTRLp(port));
1320 return;
1321 case BR_STATE_BLOCKING:
1322 case BR_STATE_LISTENING:
1323 stp_state = GSWIP_PCE_PCTRL_0_PSTATE_LISTEN;
1324 break;
1325 case BR_STATE_LEARNING:
1326 stp_state = GSWIP_PCE_PCTRL_0_PSTATE_LEARNING;
1327 break;
1328 case BR_STATE_FORWARDING:
1329 stp_state = GSWIP_PCE_PCTRL_0_PSTATE_FORWARDING;
1330 break;
1331 default:
1332 dev_err(priv->dev, "invalid STP state: %d\n", state);
1333 return;
1334 }
1335
1336 gswip_switch_mask(priv, 0, GSWIP_SDMA_PCTRL_EN,
1337 GSWIP_SDMA_PCTRLp(port));
1338 gswip_switch_mask(priv, GSWIP_PCE_PCTRL_0_PSTATE_MASK, stp_state,
1339 GSWIP_PCE_PCTRL_0p(port));
1340}
1341
Hauke Mehrtens58c59ef2019-05-06 00:25:10 +02001342static int gswip_port_fdb(struct dsa_switch *ds, int port,
1343 const unsigned char *addr, u16 vid, bool add)
1344{
Vladimir Oltean41fb0cf2021-12-06 18:57:53 +02001345 struct net_device *bridge = dsa_port_bridge_dev_get(dsa_to_port(ds, port));
Hauke Mehrtens58c59ef2019-05-06 00:25:10 +02001346 struct gswip_priv *priv = ds->priv;
Hauke Mehrtens58c59ef2019-05-06 00:25:10 +02001347 struct gswip_pce_table_entry mac_bridge = {0,};
1348 unsigned int cpu_port = priv->hw_info->cpu_port;
1349 int fid = -1;
1350 int i;
1351 int err;
1352
1353 if (!bridge)
1354 return -EINVAL;
1355
1356 for (i = cpu_port; i < ARRAY_SIZE(priv->vlans); i++) {
1357 if (priv->vlans[i].bridge == bridge) {
1358 fid = priv->vlans[i].fid;
1359 break;
1360 }
1361 }
1362
1363 if (fid == -1) {
1364 dev_err(priv->dev, "Port not part of a bridge\n");
1365 return -EINVAL;
1366 }
1367
1368 mac_bridge.table = GSWIP_TABLE_MAC_BRIDGE;
1369 mac_bridge.key_mode = true;
1370 mac_bridge.key[0] = addr[5] | (addr[4] << 8);
1371 mac_bridge.key[1] = addr[3] | (addr[2] << 8);
1372 mac_bridge.key[2] = addr[1] | (addr[0] << 8);
1373 mac_bridge.key[3] = fid;
1374 mac_bridge.val[0] = add ? BIT(port) : 0; /* port map */
1375 mac_bridge.val[1] = GSWIP_TABLE_MAC_BRIDGE_STATIC;
1376 mac_bridge.valid = add;
1377
1378 err = gswip_pce_table_entry_write(priv, &mac_bridge);
1379 if (err)
Colin Ian Kingd6759172019-05-08 11:22:09 +01001380 dev_err(priv->dev, "failed to write mac bridge: %d\n", err);
Hauke Mehrtens58c59ef2019-05-06 00:25:10 +02001381
1382 return err;
1383}
1384
1385static int gswip_port_fdb_add(struct dsa_switch *ds, int port,
1386 const unsigned char *addr, u16 vid)
1387{
1388 return gswip_port_fdb(ds, port, addr, vid, true);
1389}
1390
1391static int gswip_port_fdb_del(struct dsa_switch *ds, int port,
1392 const unsigned char *addr, u16 vid)
1393{
1394 return gswip_port_fdb(ds, port, addr, vid, false);
1395}
1396
1397static int gswip_port_fdb_dump(struct dsa_switch *ds, int port,
1398 dsa_fdb_dump_cb_t *cb, void *data)
1399{
1400 struct gswip_priv *priv = ds->priv;
1401 struct gswip_pce_table_entry mac_bridge = {0,};
1402 unsigned char addr[6];
1403 int i;
1404 int err;
1405
1406 for (i = 0; i < 2048; i++) {
1407 mac_bridge.table = GSWIP_TABLE_MAC_BRIDGE;
1408 mac_bridge.index = i;
1409
1410 err = gswip_pce_table_entry_read(priv, &mac_bridge);
1411 if (err) {
Colin Ian Kingd6759172019-05-08 11:22:09 +01001412 dev_err(priv->dev, "failed to write mac bridge: %d\n",
Hauke Mehrtens58c59ef2019-05-06 00:25:10 +02001413 err);
1414 return err;
1415 }
1416
1417 if (!mac_bridge.valid)
1418 continue;
1419
1420 addr[5] = mac_bridge.key[0] & 0xff;
1421 addr[4] = (mac_bridge.key[0] >> 8) & 0xff;
1422 addr[3] = mac_bridge.key[1] & 0xff;
1423 addr[2] = (mac_bridge.key[1] >> 8) & 0xff;
1424 addr[1] = mac_bridge.key[2] & 0xff;
1425 addr[0] = (mac_bridge.key[2] >> 8) & 0xff;
1426 if (mac_bridge.val[1] & GSWIP_TABLE_MAC_BRIDGE_STATIC) {
Vladimir Oltean871a73a2021-08-10 14:19:55 +03001427 if (mac_bridge.val[0] & BIT(port)) {
1428 err = cb(addr, 0, true, data);
1429 if (err)
1430 return err;
1431 }
Hauke Mehrtens58c59ef2019-05-06 00:25:10 +02001432 } else {
Vladimir Oltean871a73a2021-08-10 14:19:55 +03001433 if (((mac_bridge.val[0] & GENMASK(7, 4)) >> 4) == port) {
1434 err = cb(addr, 0, false, data);
1435 if (err)
1436 return err;
1437 }
Hauke Mehrtens58c59ef2019-05-06 00:25:10 +02001438 }
1439 }
1440 return 0;
1441}
1442
Russell King (Oracle)a2279b02021-11-30 13:10:16 +00001443static void gswip_xrx200_phylink_get_caps(struct dsa_switch *ds, int port,
1444 struct phylink_config *config)
Aleksander Jan Bajkowskia09d0422021-03-22 21:37:15 +01001445{
1446 switch (port) {
1447 case 0:
1448 case 1:
Russell King (Oracle)a2279b02021-11-30 13:10:16 +00001449 phy_interface_set_rgmii(config->supported_interfaces);
1450 __set_bit(PHY_INTERFACE_MODE_MII,
1451 config->supported_interfaces);
1452 __set_bit(PHY_INTERFACE_MODE_REVMII,
1453 config->supported_interfaces);
1454 __set_bit(PHY_INTERFACE_MODE_RMII,
1455 config->supported_interfaces);
Aleksander Jan Bajkowskia09d0422021-03-22 21:37:15 +01001456 break;
Russell King (Oracle)a2279b02021-11-30 13:10:16 +00001457
Aleksander Jan Bajkowskia09d0422021-03-22 21:37:15 +01001458 case 2:
1459 case 3:
1460 case 4:
Russell King (Oracle)a2279b02021-11-30 13:10:16 +00001461 __set_bit(PHY_INTERFACE_MODE_INTERNAL,
1462 config->supported_interfaces);
Aleksander Jan Bajkowskia09d0422021-03-22 21:37:15 +01001463 break;
Russell King (Oracle)a2279b02021-11-30 13:10:16 +00001464
Aleksander Jan Bajkowskia09d0422021-03-22 21:37:15 +01001465 case 5:
Russell King (Oracle)a2279b02021-11-30 13:10:16 +00001466 phy_interface_set_rgmii(config->supported_interfaces);
1467 __set_bit(PHY_INTERFACE_MODE_INTERNAL,
1468 config->supported_interfaces);
Aleksander Jan Bajkowskia09d0422021-03-22 21:37:15 +01001469 break;
Aleksander Jan Bajkowskia09d0422021-03-22 21:37:15 +01001470 }
1471
Russell King (Oracle)a2279b02021-11-30 13:10:16 +00001472 config->mac_capabilities = MAC_ASYM_PAUSE | MAC_SYM_PAUSE |
1473 MAC_10 | MAC_100 | MAC_1000;
Aleksander Jan Bajkowskia09d0422021-03-22 21:37:15 +01001474}
1475
Russell King (Oracle)a2279b02021-11-30 13:10:16 +00001476static void gswip_xrx300_phylink_get_caps(struct dsa_switch *ds, int port,
1477 struct phylink_config *config)
Aleksander Jan Bajkowskia09d0422021-03-22 21:37:15 +01001478{
1479 switch (port) {
1480 case 0:
Russell King (Oracle)a2279b02021-11-30 13:10:16 +00001481 phy_interface_set_rgmii(config->supported_interfaces);
1482 __set_bit(PHY_INTERFACE_MODE_GMII,
1483 config->supported_interfaces);
1484 __set_bit(PHY_INTERFACE_MODE_RMII,
1485 config->supported_interfaces);
Aleksander Jan Bajkowskia09d0422021-03-22 21:37:15 +01001486 break;
Russell King (Oracle)a2279b02021-11-30 13:10:16 +00001487
Aleksander Jan Bajkowskia09d0422021-03-22 21:37:15 +01001488 case 1:
1489 case 2:
1490 case 3:
1491 case 4:
Russell King (Oracle)a2279b02021-11-30 13:10:16 +00001492 __set_bit(PHY_INTERFACE_MODE_INTERNAL,
1493 config->supported_interfaces);
Aleksander Jan Bajkowskia09d0422021-03-22 21:37:15 +01001494 break;
Russell King (Oracle)a2279b02021-11-30 13:10:16 +00001495
Aleksander Jan Bajkowskia09d0422021-03-22 21:37:15 +01001496 case 5:
Russell King (Oracle)a2279b02021-11-30 13:10:16 +00001497 phy_interface_set_rgmii(config->supported_interfaces);
1498 __set_bit(PHY_INTERFACE_MODE_INTERNAL,
1499 config->supported_interfaces);
1500 __set_bit(PHY_INTERFACE_MODE_RMII,
1501 config->supported_interfaces);
Aleksander Jan Bajkowskia09d0422021-03-22 21:37:15 +01001502 break;
Aleksander Jan Bajkowskia09d0422021-03-22 21:37:15 +01001503 }
1504
Russell King (Oracle)a2279b02021-11-30 13:10:16 +00001505 config->mac_capabilities = MAC_ASYM_PAUSE | MAC_SYM_PAUSE |
1506 MAC_10 | MAC_100 | MAC_1000;
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02001507}
1508
Martin Blumenstingl3e9005b2021-04-08 20:38:27 +02001509static void gswip_port_set_link(struct gswip_priv *priv, int port, bool link)
1510{
1511 u32 mdio_phy;
1512
1513 if (link)
1514 mdio_phy = GSWIP_MDIO_PHY_LINK_UP;
1515 else
1516 mdio_phy = GSWIP_MDIO_PHY_LINK_DOWN;
1517
1518 gswip_mdio_mask(priv, GSWIP_MDIO_PHY_LINK_MASK, mdio_phy,
1519 GSWIP_MDIO_PHYp(port));
1520}
1521
1522static void gswip_port_set_speed(struct gswip_priv *priv, int port, int speed,
1523 phy_interface_t interface)
1524{
1525 u32 mdio_phy = 0, mii_cfg = 0, mac_ctrl_0 = 0;
1526
1527 switch (speed) {
1528 case SPEED_10:
1529 mdio_phy = GSWIP_MDIO_PHY_SPEED_M10;
1530
1531 if (interface == PHY_INTERFACE_MODE_RMII)
1532 mii_cfg = GSWIP_MII_CFG_RATE_M50;
1533 else
1534 mii_cfg = GSWIP_MII_CFG_RATE_M2P5;
1535
1536 mac_ctrl_0 = GSWIP_MAC_CTRL_0_GMII_MII;
1537 break;
1538
1539 case SPEED_100:
1540 mdio_phy = GSWIP_MDIO_PHY_SPEED_M100;
1541
1542 if (interface == PHY_INTERFACE_MODE_RMII)
1543 mii_cfg = GSWIP_MII_CFG_RATE_M50;
1544 else
1545 mii_cfg = GSWIP_MII_CFG_RATE_M25;
1546
1547 mac_ctrl_0 = GSWIP_MAC_CTRL_0_GMII_MII;
1548 break;
1549
1550 case SPEED_1000:
1551 mdio_phy = GSWIP_MDIO_PHY_SPEED_G1;
1552
1553 mii_cfg = GSWIP_MII_CFG_RATE_M125;
1554
1555 mac_ctrl_0 = GSWIP_MAC_CTRL_0_GMII_RGMII;
1556 break;
1557 }
1558
1559 gswip_mdio_mask(priv, GSWIP_MDIO_PHY_SPEED_MASK, mdio_phy,
1560 GSWIP_MDIO_PHYp(port));
1561 gswip_mii_mask_cfg(priv, GSWIP_MII_CFG_RATE_MASK, mii_cfg, port);
1562 gswip_switch_mask(priv, GSWIP_MAC_CTRL_0_GMII_MASK, mac_ctrl_0,
1563 GSWIP_MAC_CTRL_0p(port));
1564}
1565
1566static void gswip_port_set_duplex(struct gswip_priv *priv, int port, int duplex)
1567{
1568 u32 mac_ctrl_0, mdio_phy;
1569
1570 if (duplex == DUPLEX_FULL) {
1571 mac_ctrl_0 = GSWIP_MAC_CTRL_0_FDUP_EN;
1572 mdio_phy = GSWIP_MDIO_PHY_FDUP_EN;
1573 } else {
1574 mac_ctrl_0 = GSWIP_MAC_CTRL_0_FDUP_DIS;
1575 mdio_phy = GSWIP_MDIO_PHY_FDUP_DIS;
1576 }
1577
1578 gswip_switch_mask(priv, GSWIP_MAC_CTRL_0_FDUP_MASK, mac_ctrl_0,
1579 GSWIP_MAC_CTRL_0p(port));
1580 gswip_mdio_mask(priv, GSWIP_MDIO_PHY_FDUP_MASK, mdio_phy,
1581 GSWIP_MDIO_PHYp(port));
1582}
1583
1584static void gswip_port_set_pause(struct gswip_priv *priv, int port,
1585 bool tx_pause, bool rx_pause)
1586{
1587 u32 mac_ctrl_0, mdio_phy;
1588
1589 if (tx_pause && rx_pause) {
1590 mac_ctrl_0 = GSWIP_MAC_CTRL_0_FCON_RXTX;
1591 mdio_phy = GSWIP_MDIO_PHY_FCONTX_EN |
1592 GSWIP_MDIO_PHY_FCONRX_EN;
1593 } else if (tx_pause) {
1594 mac_ctrl_0 = GSWIP_MAC_CTRL_0_FCON_TX;
1595 mdio_phy = GSWIP_MDIO_PHY_FCONTX_EN |
1596 GSWIP_MDIO_PHY_FCONRX_DIS;
1597 } else if (rx_pause) {
1598 mac_ctrl_0 = GSWIP_MAC_CTRL_0_FCON_RX;
1599 mdio_phy = GSWIP_MDIO_PHY_FCONTX_DIS |
1600 GSWIP_MDIO_PHY_FCONRX_EN;
1601 } else {
1602 mac_ctrl_0 = GSWIP_MAC_CTRL_0_FCON_NONE;
1603 mdio_phy = GSWIP_MDIO_PHY_FCONTX_DIS |
1604 GSWIP_MDIO_PHY_FCONRX_DIS;
1605 }
1606
1607 gswip_switch_mask(priv, GSWIP_MAC_CTRL_0_FCON_MASK,
1608 mac_ctrl_0, GSWIP_MAC_CTRL_0p(port));
1609 gswip_mdio_mask(priv,
1610 GSWIP_MDIO_PHY_FCONTX_MASK |
1611 GSWIP_MDIO_PHY_FCONRX_MASK,
1612 mdio_phy, GSWIP_MDIO_PHYp(port));
1613}
1614
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02001615static void gswip_phylink_mac_config(struct dsa_switch *ds, int port,
1616 unsigned int mode,
1617 const struct phylink_link_state *state)
1618{
1619 struct gswip_priv *priv = ds->priv;
1620 u32 miicfg = 0;
1621
1622 miicfg |= GSWIP_MII_CFG_LDCLKDIS;
1623
1624 switch (state->interface) {
1625 case PHY_INTERFACE_MODE_MII:
1626 case PHY_INTERFACE_MODE_INTERNAL:
1627 miicfg |= GSWIP_MII_CFG_MODE_MIIM;
1628 break;
1629 case PHY_INTERFACE_MODE_REVMII:
1630 miicfg |= GSWIP_MII_CFG_MODE_MIIP;
1631 break;
1632 case PHY_INTERFACE_MODE_RMII:
1633 miicfg |= GSWIP_MII_CFG_MODE_RMIIM;
Martin Blumenstingl4b592322021-04-08 20:38:28 +02001634
1635 /* Configure the RMII clock as output: */
1636 miicfg |= GSWIP_MII_CFG_RMII_CLK;
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02001637 break;
1638 case PHY_INTERFACE_MODE_RGMII:
1639 case PHY_INTERFACE_MODE_RGMII_ID:
1640 case PHY_INTERFACE_MODE_RGMII_RXID:
1641 case PHY_INTERFACE_MODE_RGMII_TXID:
1642 miicfg |= GSWIP_MII_CFG_MODE_RGMII;
1643 break;
Aleksander Jan Bajkowskia09d0422021-03-22 21:37:15 +01001644 case PHY_INTERFACE_MODE_GMII:
1645 miicfg |= GSWIP_MII_CFG_MODE_GMII;
1646 break;
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02001647 default:
1648 dev_err(ds->dev,
1649 "Unsupported interface: %d\n", state->interface);
1650 return;
1651 }
Martin Blumenstingl4b592322021-04-08 20:38:28 +02001652
1653 gswip_mii_mask_cfg(priv,
1654 GSWIP_MII_CFG_MODE_MASK | GSWIP_MII_CFG_RMII_CLK |
1655 GSWIP_MII_CFG_RGMII_IBS | GSWIP_MII_CFG_LDCLKDIS,
1656 miicfg, port);
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02001657
1658 switch (state->interface) {
1659 case PHY_INTERFACE_MODE_RGMII_ID:
1660 gswip_mii_mask_pcdu(priv, GSWIP_MII_PCDU_TXDLY_MASK |
1661 GSWIP_MII_PCDU_RXDLY_MASK, 0, port);
1662 break;
1663 case PHY_INTERFACE_MODE_RGMII_RXID:
1664 gswip_mii_mask_pcdu(priv, GSWIP_MII_PCDU_RXDLY_MASK, 0, port);
1665 break;
1666 case PHY_INTERFACE_MODE_RGMII_TXID:
1667 gswip_mii_mask_pcdu(priv, GSWIP_MII_PCDU_TXDLY_MASK, 0, port);
1668 break;
1669 default:
1670 break;
1671 }
1672}
1673
1674static void gswip_phylink_mac_link_down(struct dsa_switch *ds, int port,
1675 unsigned int mode,
1676 phy_interface_t interface)
1677{
1678 struct gswip_priv *priv = ds->priv;
1679
1680 gswip_mii_mask_cfg(priv, GSWIP_MII_CFG_EN, 0, port);
Martin Blumenstingl3e9005b2021-04-08 20:38:27 +02001681
1682 if (!dsa_is_cpu_port(ds, port))
1683 gswip_port_set_link(priv, port, false);
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02001684}
1685
1686static void gswip_phylink_mac_link_up(struct dsa_switch *ds, int port,
1687 unsigned int mode,
1688 phy_interface_t interface,
Russell King5b502a72020-02-26 10:23:46 +00001689 struct phy_device *phydev,
1690 int speed, int duplex,
1691 bool tx_pause, bool rx_pause)
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02001692{
1693 struct gswip_priv *priv = ds->priv;
1694
Martin Blumenstingl3e9005b2021-04-08 20:38:27 +02001695 if (!dsa_is_cpu_port(ds, port)) {
1696 gswip_port_set_link(priv, port, true);
1697 gswip_port_set_speed(priv, port, speed, interface);
1698 gswip_port_set_duplex(priv, port, duplex);
1699 gswip_port_set_pause(priv, port, tx_pause, rx_pause);
1700 }
1701
Martin Blumenstinglc1a9ec72021-01-03 02:25:43 +01001702 gswip_mii_mask_cfg(priv, 0, GSWIP_MII_CFG_EN, port);
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02001703}
1704
1705static void gswip_get_strings(struct dsa_switch *ds, int port, u32 stringset,
1706 uint8_t *data)
1707{
1708 int i;
1709
1710 if (stringset != ETH_SS_STATS)
1711 return;
1712
1713 for (i = 0; i < ARRAY_SIZE(gswip_rmon_cnt); i++)
1714 strncpy(data + i * ETH_GSTRING_LEN, gswip_rmon_cnt[i].name,
1715 ETH_GSTRING_LEN);
1716}
1717
1718static u32 gswip_bcm_ram_entry_read(struct gswip_priv *priv, u32 table,
1719 u32 index)
1720{
1721 u32 result;
1722 int err;
1723
1724 gswip_switch_w(priv, index, GSWIP_BM_RAM_ADDR);
1725 gswip_switch_mask(priv, GSWIP_BM_RAM_CTRL_ADDR_MASK |
1726 GSWIP_BM_RAM_CTRL_OPMOD,
1727 table | GSWIP_BM_RAM_CTRL_BAS,
1728 GSWIP_BM_RAM_CTRL);
1729
1730 err = gswip_switch_r_timeout(priv, GSWIP_BM_RAM_CTRL,
1731 GSWIP_BM_RAM_CTRL_BAS);
1732 if (err) {
1733 dev_err(priv->dev, "timeout while reading table: %u, index: %u",
1734 table, index);
1735 return 0;
1736 }
1737
1738 result = gswip_switch_r(priv, GSWIP_BM_RAM_VAL(0));
1739 result |= gswip_switch_r(priv, GSWIP_BM_RAM_VAL(1)) << 16;
1740
1741 return result;
1742}
1743
1744static void gswip_get_ethtool_stats(struct dsa_switch *ds, int port,
1745 uint64_t *data)
1746{
1747 struct gswip_priv *priv = ds->priv;
1748 const struct gswip_rmon_cnt_desc *rmon_cnt;
1749 int i;
1750 u64 high;
1751
1752 for (i = 0; i < ARRAY_SIZE(gswip_rmon_cnt); i++) {
1753 rmon_cnt = &gswip_rmon_cnt[i];
1754
1755 data[i] = gswip_bcm_ram_entry_read(priv, port,
1756 rmon_cnt->offset);
1757 if (rmon_cnt->size == 2) {
1758 high = gswip_bcm_ram_entry_read(priv, port,
1759 rmon_cnt->offset + 1);
1760 data[i] |= high << 32;
1761 }
1762 }
1763}
1764
1765static int gswip_get_sset_count(struct dsa_switch *ds, int port, int sset)
1766{
1767 if (sset != ETH_SS_STATS)
1768 return 0;
1769
1770 return ARRAY_SIZE(gswip_rmon_cnt);
1771}
1772
Aleksander Jan Bajkowskia09d0422021-03-22 21:37:15 +01001773static const struct dsa_switch_ops gswip_xrx200_switch_ops = {
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02001774 .get_tag_protocol = gswip_get_tag_protocol,
1775 .setup = gswip_setup,
1776 .port_enable = gswip_port_enable,
1777 .port_disable = gswip_port_disable,
Hauke Mehrtens8206e0c2019-05-06 00:25:07 +02001778 .port_bridge_join = gswip_port_bridge_join,
1779 .port_bridge_leave = gswip_port_bridge_leave,
Hauke Mehrtens45813482019-05-06 00:25:09 +02001780 .port_fast_age = gswip_port_fast_age,
Hauke Mehrtens9bbb1c02019-05-06 00:25:08 +02001781 .port_vlan_filtering = gswip_port_vlan_filtering,
Hauke Mehrtens9bbb1c02019-05-06 00:25:08 +02001782 .port_vlan_add = gswip_port_vlan_add,
1783 .port_vlan_del = gswip_port_vlan_del,
Hauke Mehrtens8206e0c2019-05-06 00:25:07 +02001784 .port_stp_state_set = gswip_port_stp_state_set,
Hauke Mehrtens58c59ef2019-05-06 00:25:10 +02001785 .port_fdb_add = gswip_port_fdb_add,
1786 .port_fdb_del = gswip_port_fdb_del,
1787 .port_fdb_dump = gswip_port_fdb_dump,
Russell King (Oracle)a2279b02021-11-30 13:10:16 +00001788 .phylink_get_caps = gswip_xrx200_phylink_get_caps,
Aleksander Jan Bajkowskia09d0422021-03-22 21:37:15 +01001789 .phylink_mac_config = gswip_phylink_mac_config,
1790 .phylink_mac_link_down = gswip_phylink_mac_link_down,
1791 .phylink_mac_link_up = gswip_phylink_mac_link_up,
1792 .get_strings = gswip_get_strings,
1793 .get_ethtool_stats = gswip_get_ethtool_stats,
1794 .get_sset_count = gswip_get_sset_count,
1795};
1796
1797static const struct dsa_switch_ops gswip_xrx300_switch_ops = {
1798 .get_tag_protocol = gswip_get_tag_protocol,
1799 .setup = gswip_setup,
1800 .port_enable = gswip_port_enable,
1801 .port_disable = gswip_port_disable,
1802 .port_bridge_join = gswip_port_bridge_join,
1803 .port_bridge_leave = gswip_port_bridge_leave,
1804 .port_fast_age = gswip_port_fast_age,
1805 .port_vlan_filtering = gswip_port_vlan_filtering,
1806 .port_vlan_add = gswip_port_vlan_add,
1807 .port_vlan_del = gswip_port_vlan_del,
1808 .port_stp_state_set = gswip_port_stp_state_set,
1809 .port_fdb_add = gswip_port_fdb_add,
1810 .port_fdb_del = gswip_port_fdb_del,
1811 .port_fdb_dump = gswip_port_fdb_dump,
Russell King (Oracle)a2279b02021-11-30 13:10:16 +00001812 .phylink_get_caps = gswip_xrx300_phylink_get_caps,
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02001813 .phylink_mac_config = gswip_phylink_mac_config,
1814 .phylink_mac_link_down = gswip_phylink_mac_link_down,
1815 .phylink_mac_link_up = gswip_phylink_mac_link_up,
1816 .get_strings = gswip_get_strings,
1817 .get_ethtool_stats = gswip_get_ethtool_stats,
1818 .get_sset_count = gswip_get_sset_count,
1819};
1820
1821static const struct xway_gphy_match_data xrx200a1x_gphy_data = {
1822 .fe_firmware_name = "lantiq/xrx200_phy22f_a14.bin",
1823 .ge_firmware_name = "lantiq/xrx200_phy11g_a14.bin",
1824};
1825
1826static const struct xway_gphy_match_data xrx200a2x_gphy_data = {
1827 .fe_firmware_name = "lantiq/xrx200_phy22f_a22.bin",
1828 .ge_firmware_name = "lantiq/xrx200_phy11g_a22.bin",
1829};
1830
1831static const struct xway_gphy_match_data xrx300_gphy_data = {
1832 .fe_firmware_name = "lantiq/xrx300_phy22f_a21.bin",
1833 .ge_firmware_name = "lantiq/xrx300_phy11g_a21.bin",
1834};
1835
1836static const struct of_device_id xway_gphy_match[] = {
1837 { .compatible = "lantiq,xrx200-gphy-fw", .data = NULL },
1838 { .compatible = "lantiq,xrx200a1x-gphy-fw", .data = &xrx200a1x_gphy_data },
1839 { .compatible = "lantiq,xrx200a2x-gphy-fw", .data = &xrx200a2x_gphy_data },
1840 { .compatible = "lantiq,xrx300-gphy-fw", .data = &xrx300_gphy_data },
1841 { .compatible = "lantiq,xrx330-gphy-fw", .data = &xrx300_gphy_data },
1842 {},
1843};
1844
1845static int gswip_gphy_fw_load(struct gswip_priv *priv, struct gswip_gphy_fw *gphy_fw)
1846{
1847 struct device *dev = priv->dev;
1848 const struct firmware *fw;
1849 void *fw_addr;
1850 dma_addr_t dma_addr;
1851 dma_addr_t dev_addr;
1852 size_t size;
1853 int ret;
1854
1855 ret = clk_prepare_enable(gphy_fw->clk_gate);
1856 if (ret)
1857 return ret;
1858
1859 reset_control_assert(gphy_fw->reset);
1860
Aleksander Jan Bajkowski111b64e2021-09-12 13:58:07 +02001861 /* The vendor BSP uses a 200ms delay after asserting the reset line.
1862 * Without this some users are observing that the PHY is not coming up
1863 * on the MDIO bus.
1864 */
1865 msleep(200);
1866
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02001867 ret = request_firmware(&fw, gphy_fw->fw_name, dev);
1868 if (ret) {
1869 dev_err(dev, "failed to load firmware: %s, error: %i\n",
1870 gphy_fw->fw_name, ret);
1871 return ret;
1872 }
1873
1874 /* GPHY cores need the firmware code in a persistent and contiguous
1875 * memory area with a 16 kB boundary aligned start address.
1876 */
1877 size = fw->size + XRX200_GPHY_FW_ALIGN;
1878
1879 fw_addr = dmam_alloc_coherent(dev, size, &dma_addr, GFP_KERNEL);
1880 if (fw_addr) {
1881 fw_addr = PTR_ALIGN(fw_addr, XRX200_GPHY_FW_ALIGN);
1882 dev_addr = ALIGN(dma_addr, XRX200_GPHY_FW_ALIGN);
1883 memcpy(fw_addr, fw->data, fw->size);
1884 } else {
1885 dev_err(dev, "failed to alloc firmware memory\n");
1886 release_firmware(fw);
1887 return -ENOMEM;
1888 }
1889
1890 release_firmware(fw);
1891
1892 ret = regmap_write(priv->rcu_regmap, gphy_fw->fw_addr_offset, dev_addr);
1893 if (ret)
1894 return ret;
1895
1896 reset_control_deassert(gphy_fw->reset);
1897
1898 return ret;
1899}
1900
1901static int gswip_gphy_fw_probe(struct gswip_priv *priv,
1902 struct gswip_gphy_fw *gphy_fw,
1903 struct device_node *gphy_fw_np, int i)
1904{
1905 struct device *dev = priv->dev;
1906 u32 gphy_mode;
1907 int ret;
1908 char gphyname[10];
1909
1910 snprintf(gphyname, sizeof(gphyname), "gphy%d", i);
1911
1912 gphy_fw->clk_gate = devm_clk_get(dev, gphyname);
1913 if (IS_ERR(gphy_fw->clk_gate)) {
1914 dev_err(dev, "Failed to lookup gate clock\n");
1915 return PTR_ERR(gphy_fw->clk_gate);
1916 }
1917
1918 ret = of_property_read_u32(gphy_fw_np, "reg", &gphy_fw->fw_addr_offset);
1919 if (ret)
1920 return ret;
1921
1922 ret = of_property_read_u32(gphy_fw_np, "lantiq,gphy-mode", &gphy_mode);
1923 /* Default to GE mode */
1924 if (ret)
1925 gphy_mode = GPHY_MODE_GE;
1926
1927 switch (gphy_mode) {
1928 case GPHY_MODE_FE:
1929 gphy_fw->fw_name = priv->gphy_fw_name_cfg->fe_firmware_name;
1930 break;
1931 case GPHY_MODE_GE:
1932 gphy_fw->fw_name = priv->gphy_fw_name_cfg->ge_firmware_name;
1933 break;
1934 default:
1935 dev_err(dev, "Unknown GPHY mode %d\n", gphy_mode);
1936 return -EINVAL;
1937 }
1938
1939 gphy_fw->reset = of_reset_control_array_get_exclusive(gphy_fw_np);
Wei Yongjunf592e0b2018-09-15 01:33:38 +00001940 if (IS_ERR(gphy_fw->reset)) {
1941 if (PTR_ERR(gphy_fw->reset) != -EPROBE_DEFER)
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02001942 dev_err(dev, "Failed to lookup gphy reset\n");
Wei Yongjunf592e0b2018-09-15 01:33:38 +00001943 return PTR_ERR(gphy_fw->reset);
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02001944 }
1945
1946 return gswip_gphy_fw_load(priv, gphy_fw);
1947}
1948
1949static void gswip_gphy_fw_remove(struct gswip_priv *priv,
1950 struct gswip_gphy_fw *gphy_fw)
1951{
1952 int ret;
1953
1954 /* check if the device was fully probed */
1955 if (!gphy_fw->fw_name)
1956 return;
1957
1958 ret = regmap_write(priv->rcu_regmap, gphy_fw->fw_addr_offset, 0);
1959 if (ret)
1960 dev_err(priv->dev, "can not reset GPHY FW pointer");
1961
1962 clk_disable_unprepare(gphy_fw->clk_gate);
1963
1964 reset_control_put(gphy_fw->reset);
1965}
1966
1967static int gswip_gphy_fw_list(struct gswip_priv *priv,
1968 struct device_node *gphy_fw_list_np, u32 version)
1969{
1970 struct device *dev = priv->dev;
1971 struct device_node *gphy_fw_np;
1972 const struct of_device_id *match;
1973 int err;
1974 int i = 0;
1975
Hauke Mehrtens0e630b52018-09-15 14:08:48 +02001976 /* The VRX200 rev 1.1 uses the GSWIP 2.0 and needs the older
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02001977 * GPHY firmware. The VRX200 rev 1.2 uses the GSWIP 2.1 and also
1978 * needs a different GPHY firmware.
1979 */
1980 if (of_device_is_compatible(gphy_fw_list_np, "lantiq,xrx200-gphy-fw")) {
1981 switch (version) {
1982 case GSWIP_VERSION_2_0:
1983 priv->gphy_fw_name_cfg = &xrx200a1x_gphy_data;
1984 break;
1985 case GSWIP_VERSION_2_1:
1986 priv->gphy_fw_name_cfg = &xrx200a2x_gphy_data;
1987 break;
1988 default:
1989 dev_err(dev, "unknown GSWIP version: 0x%x", version);
1990 return -ENOENT;
1991 }
1992 }
1993
1994 match = of_match_node(xway_gphy_match, gphy_fw_list_np);
1995 if (match && match->data)
1996 priv->gphy_fw_name_cfg = match->data;
1997
1998 if (!priv->gphy_fw_name_cfg) {
1999 dev_err(dev, "GPHY compatible type not supported");
2000 return -ENOENT;
2001 }
2002
2003 priv->num_gphy_fw = of_get_available_child_count(gphy_fw_list_np);
2004 if (!priv->num_gphy_fw)
2005 return -ENOENT;
2006
2007 priv->rcu_regmap = syscon_regmap_lookup_by_phandle(gphy_fw_list_np,
2008 "lantiq,rcu");
2009 if (IS_ERR(priv->rcu_regmap))
2010 return PTR_ERR(priv->rcu_regmap);
2011
2012 priv->gphy_fw = devm_kmalloc_array(dev, priv->num_gphy_fw,
2013 sizeof(*priv->gphy_fw),
2014 GFP_KERNEL | __GFP_ZERO);
2015 if (!priv->gphy_fw)
2016 return -ENOMEM;
2017
2018 for_each_available_child_of_node(gphy_fw_list_np, gphy_fw_np) {
2019 err = gswip_gphy_fw_probe(priv, &priv->gphy_fw[i],
2020 gphy_fw_np, i);
2021 if (err)
2022 goto remove_gphy;
2023 i++;
2024 }
2025
Martin Blumenstingl2a1828e2020-11-15 17:57:57 +01002026 /* The standalone PHY11G requires 300ms to be fully
2027 * initialized and ready for any MDIO communication after being
2028 * taken out of reset. For the SoC-internal GPHY variant there
2029 * is no (known) documentation for the minimum time after a
2030 * reset. Use the same value as for the standalone variant as
2031 * some users have reported internal PHYs not being detected
2032 * without any delay.
2033 */
2034 msleep(300);
2035
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02002036 return 0;
2037
2038remove_gphy:
2039 for (i = 0; i < priv->num_gphy_fw; i++)
2040 gswip_gphy_fw_remove(priv, &priv->gphy_fw[i]);
2041 return err;
2042}
2043
2044static int gswip_probe(struct platform_device *pdev)
2045{
2046 struct gswip_priv *priv;
Aleksander Jan Bajkowski204c7612021-03-22 21:37:16 +01002047 struct device_node *np, *mdio_np, *gphy_fw_np;
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02002048 struct device *dev = &pdev->dev;
2049 int err;
2050 int i;
2051 u32 version;
2052
2053 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
2054 if (!priv)
2055 return -ENOMEM;
2056
YueHaibing6551c8c2019-08-01 20:25:46 +08002057 priv->gswip = devm_platform_ioremap_resource(pdev, 0);
Wei Yongjunf5de8bf2018-09-15 01:33:21 +00002058 if (IS_ERR(priv->gswip))
2059 return PTR_ERR(priv->gswip);
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02002060
YueHaibing6551c8c2019-08-01 20:25:46 +08002061 priv->mdio = devm_platform_ioremap_resource(pdev, 1);
Wei Yongjunf5de8bf2018-09-15 01:33:21 +00002062 if (IS_ERR(priv->mdio))
2063 return PTR_ERR(priv->mdio);
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02002064
YueHaibing6551c8c2019-08-01 20:25:46 +08002065 priv->mii = devm_platform_ioremap_resource(pdev, 2);
Wei Yongjunf5de8bf2018-09-15 01:33:21 +00002066 if (IS_ERR(priv->mii))
2067 return PTR_ERR(priv->mii);
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02002068
2069 priv->hw_info = of_device_get_match_data(dev);
2070 if (!priv->hw_info)
2071 return -EINVAL;
2072
Vivien Didelot7e99e342019-10-21 16:51:30 -04002073 priv->ds = devm_kzalloc(dev, sizeof(*priv->ds), GFP_KERNEL);
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02002074 if (!priv->ds)
2075 return -ENOMEM;
2076
Vivien Didelot7e99e342019-10-21 16:51:30 -04002077 priv->ds->dev = dev;
2078 priv->ds->num_ports = priv->hw_info->max_ports;
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02002079 priv->ds->priv = priv;
Aleksander Jan Bajkowskia09d0422021-03-22 21:37:15 +01002080 priv->ds->ops = priv->hw_info->ops;
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02002081 priv->dev = dev;
Vladimir Olteancf231b42021-10-24 20:17:53 +03002082 mutex_init(&priv->pce_table_lock);
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02002083 version = gswip_switch_r(priv, GSWIP_VERSION);
2084
Aleksander Jan Bajkowski204c7612021-03-22 21:37:16 +01002085 np = dev->of_node;
2086 switch (version) {
2087 case GSWIP_VERSION_2_0:
2088 case GSWIP_VERSION_2_1:
2089 if (!of_device_is_compatible(np, "lantiq,xrx200-gswip"))
2090 return -EINVAL;
2091 break;
2092 case GSWIP_VERSION_2_2:
2093 case GSWIP_VERSION_2_2_ETC:
2094 if (!of_device_is_compatible(np, "lantiq,xrx300-gswip") &&
2095 !of_device_is_compatible(np, "lantiq,xrx330-gswip"))
2096 return -EINVAL;
2097 break;
2098 default:
2099 dev_err(dev, "unknown GSWIP version: 0x%x", version);
2100 return -ENOENT;
2101 }
2102
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02002103 /* bring up the mdio bus */
Johan Hovoldc8cbcb02019-01-16 11:23:34 +01002104 gphy_fw_np = of_get_compatible_child(dev->of_node, "lantiq,gphy-fw");
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02002105 if (gphy_fw_np) {
2106 err = gswip_gphy_fw_list(priv, gphy_fw_np, version);
Johan Hovoldc8cbcb02019-01-16 11:23:34 +01002107 of_node_put(gphy_fw_np);
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02002108 if (err) {
2109 dev_err(dev, "gphy fw probe failed\n");
2110 return err;
2111 }
2112 }
2113
2114 /* bring up the mdio bus */
Johan Hovoldc8cbcb02019-01-16 11:23:34 +01002115 mdio_np = of_get_compatible_child(dev->of_node, "lantiq,xrx200-mdio");
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02002116 if (mdio_np) {
2117 err = gswip_mdio(priv, mdio_np);
2118 if (err) {
2119 dev_err(dev, "mdio probe failed\n");
Johan Hovoldc8cbcb02019-01-16 11:23:34 +01002120 goto put_mdio_node;
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02002121 }
2122 }
2123
2124 err = dsa_register_switch(priv->ds);
2125 if (err) {
2126 dev_err(dev, "dsa switch register failed: %i\n", err);
2127 goto mdio_bus;
2128 }
Hauke Mehrtens0e630b52018-09-15 14:08:48 +02002129 if (!dsa_is_cpu_port(priv->ds, priv->hw_info->cpu_port)) {
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02002130 dev_err(dev, "wrong CPU port defined, HW only supports port: %i",
2131 priv->hw_info->cpu_port);
2132 err = -EINVAL;
Johan Hovoldaed13f22019-01-16 11:23:33 +01002133 goto disable_switch;
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02002134 }
2135
2136 platform_set_drvdata(pdev, priv);
2137
2138 dev_info(dev, "probed GSWIP version %lx mod %lx\n",
2139 (version & GSWIP_VERSION_REV_MASK) >> GSWIP_VERSION_REV_SHIFT,
2140 (version & GSWIP_VERSION_MOD_MASK) >> GSWIP_VERSION_MOD_SHIFT);
2141 return 0;
2142
Johan Hovoldaed13f22019-01-16 11:23:33 +01002143disable_switch:
2144 gswip_mdio_mask(priv, GSWIP_MDIO_GLOB_ENABLE, 0, GSWIP_MDIO_GLOB);
2145 dsa_unregister_switch(priv->ds);
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02002146mdio_bus:
2147 if (mdio_np)
2148 mdiobus_unregister(priv->ds->slave_mii_bus);
Johan Hovoldc8cbcb02019-01-16 11:23:34 +01002149put_mdio_node:
2150 of_node_put(mdio_np);
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02002151 for (i = 0; i < priv->num_gphy_fw; i++)
2152 gswip_gphy_fw_remove(priv, &priv->gphy_fw[i]);
2153 return err;
2154}
2155
2156static int gswip_remove(struct platform_device *pdev)
2157{
2158 struct gswip_priv *priv = platform_get_drvdata(pdev);
2159 int i;
2160
Vladimir Oltean0650bf52021-09-17 16:34:33 +03002161 if (!priv)
2162 return 0;
2163
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02002164 /* disable the switch */
2165 gswip_mdio_mask(priv, GSWIP_MDIO_GLOB_ENABLE, 0, GSWIP_MDIO_GLOB);
2166
2167 dsa_unregister_switch(priv->ds);
2168
Johan Hovoldc8cbcb02019-01-16 11:23:34 +01002169 if (priv->ds->slave_mii_bus) {
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02002170 mdiobus_unregister(priv->ds->slave_mii_bus);
Johan Hovoldc8cbcb02019-01-16 11:23:34 +01002171 of_node_put(priv->ds->slave_mii_bus->dev.of_node);
2172 }
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02002173
2174 for (i = 0; i < priv->num_gphy_fw; i++)
2175 gswip_gphy_fw_remove(priv, &priv->gphy_fw[i]);
2176
Vladimir Oltean0650bf52021-09-17 16:34:33 +03002177 platform_set_drvdata(pdev, NULL);
2178
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02002179 return 0;
2180}
2181
Vladimir Oltean0650bf52021-09-17 16:34:33 +03002182static void gswip_shutdown(struct platform_device *pdev)
2183{
2184 struct gswip_priv *priv = platform_get_drvdata(pdev);
2185
2186 if (!priv)
2187 return;
2188
2189 dsa_switch_shutdown(priv->ds);
2190
2191 platform_set_drvdata(pdev, NULL);
2192}
2193
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02002194static const struct gswip_hw_info gswip_xrx200 = {
2195 .max_ports = 7,
2196 .cpu_port = 6,
Aleksander Jan Bajkowskia09d0422021-03-22 21:37:15 +01002197 .ops = &gswip_xrx200_switch_ops,
2198};
2199
2200static const struct gswip_hw_info gswip_xrx300 = {
2201 .max_ports = 7,
2202 .cpu_port = 6,
2203 .ops = &gswip_xrx300_switch_ops,
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02002204};
2205
2206static const struct of_device_id gswip_of_match[] = {
2207 { .compatible = "lantiq,xrx200-gswip", .data = &gswip_xrx200 },
Aleksander Jan Bajkowskia09d0422021-03-22 21:37:15 +01002208 { .compatible = "lantiq,xrx300-gswip", .data = &gswip_xrx300 },
2209 { .compatible = "lantiq,xrx330-gswip", .data = &gswip_xrx300 },
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02002210 {},
2211};
2212MODULE_DEVICE_TABLE(of, gswip_of_match);
2213
2214static struct platform_driver gswip_driver = {
2215 .probe = gswip_probe,
2216 .remove = gswip_remove,
Vladimir Oltean0650bf52021-09-17 16:34:33 +03002217 .shutdown = gswip_shutdown,
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02002218 .driver = {
2219 .name = "gswip",
2220 .of_match_table = gswip_of_match,
2221 },
2222};
2223
2224module_platform_driver(gswip_driver);
2225
Hauke Mehrtenscffde202019-02-22 20:11:13 +01002226MODULE_FIRMWARE("lantiq/xrx300_phy11g_a21.bin");
2227MODULE_FIRMWARE("lantiq/xrx300_phy22f_a21.bin");
2228MODULE_FIRMWARE("lantiq/xrx200_phy11g_a14.bin");
2229MODULE_FIRMWARE("lantiq/xrx200_phy11g_a22.bin");
2230MODULE_FIRMWARE("lantiq/xrx200_phy22f_a14.bin");
2231MODULE_FIRMWARE("lantiq/xrx200_phy22f_a22.bin");
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02002232MODULE_AUTHOR("Hauke Mehrtens <hauke@hauke-m.de>");
2233MODULE_DESCRIPTION("Lantiq / Intel GSWIP driver");
2234MODULE_LICENSE("GPL v2");