blob: 6317d0ae42d00e3fb1a48810e45678796a509279 [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,
1149 struct net_device *bridge)
1150{
1151 struct gswip_priv *priv = ds->priv;
1152 int err;
1153
Hauke Mehrtens9bbb1c02019-05-06 00:25:08 +02001154 /* When the bridge uses VLAN filtering we have to configure VLAN
1155 * specific bridges. No bridge is configured here.
1156 */
1157 if (!br_vlan_enabled(bridge)) {
1158 err = gswip_vlan_add_unaware(priv, bridge, port);
1159 if (err)
1160 return err;
1161 priv->port_vlan_filter &= ~BIT(port);
1162 } else {
1163 priv->port_vlan_filter |= BIT(port);
1164 }
Hauke Mehrtens8206e0c2019-05-06 00:25:07 +02001165 return gswip_add_single_port_br(priv, port, false);
1166}
1167
1168static void gswip_port_bridge_leave(struct dsa_switch *ds, int port,
1169 struct net_device *bridge)
1170{
1171 struct gswip_priv *priv = ds->priv;
1172
1173 gswip_add_single_port_br(priv, port, true);
1174
Hauke Mehrtens9bbb1c02019-05-06 00:25:08 +02001175 /* When the bridge uses VLAN filtering we have to configure VLAN
1176 * specific bridges. No bridge is configured here.
1177 */
1178 if (!br_vlan_enabled(bridge))
1179 gswip_vlan_remove(priv, bridge, port, 0, true, false);
1180}
1181
1182static int gswip_port_vlan_prepare(struct dsa_switch *ds, int port,
Vladimir Oltean31046a52021-02-13 22:43:18 +02001183 const struct switchdev_obj_port_vlan *vlan,
1184 struct netlink_ext_ack *extack)
Hauke Mehrtens9bbb1c02019-05-06 00:25:08 +02001185{
Vladimir Oltean41fb0cf2021-12-06 18:57:53 +02001186 struct net_device *bridge = dsa_port_bridge_dev_get(dsa_to_port(ds, port));
Hauke Mehrtens9bbb1c02019-05-06 00:25:08 +02001187 struct gswip_priv *priv = ds->priv;
Hauke Mehrtens9bbb1c02019-05-06 00:25:08 +02001188 unsigned int max_ports = priv->hw_info->max_ports;
Hauke Mehrtens9bbb1c02019-05-06 00:25:08 +02001189 int pos = max_ports;
Vladimir Olteanb7a9e0d2021-01-09 02:01:46 +02001190 int i, idx = -1;
Hauke Mehrtens9bbb1c02019-05-06 00:25:08 +02001191
1192 /* We only support VLAN filtering on bridges */
1193 if (!dsa_is_cpu_port(ds, port) && !bridge)
1194 return -EOPNOTSUPP;
1195
Vladimir Olteanb7a9e0d2021-01-09 02:01:46 +02001196 /* Check if there is already a page for this VLAN */
1197 for (i = max_ports; i < ARRAY_SIZE(priv->vlans); i++) {
1198 if (priv->vlans[i].bridge == bridge &&
1199 priv->vlans[i].vid == vlan->vid) {
1200 idx = i;
1201 break;
1202 }
1203 }
Hauke Mehrtens9bbb1c02019-05-06 00:25:08 +02001204
Vladimir Olteanb7a9e0d2021-01-09 02:01:46 +02001205 /* If this VLAN is not programmed yet, we have to reserve
1206 * one entry in the VLAN table. Make sure we start at the
1207 * next position round.
1208 */
1209 if (idx == -1) {
1210 /* Look for a free slot */
1211 for (; pos < ARRAY_SIZE(priv->vlans); pos++) {
1212 if (!priv->vlans[pos].bridge) {
1213 idx = pos;
1214 pos++;
Hauke Mehrtens9bbb1c02019-05-06 00:25:08 +02001215 break;
1216 }
1217 }
1218
Vladimir Oltean31046a52021-02-13 22:43:18 +02001219 if (idx == -1) {
1220 NL_SET_ERR_MSG_MOD(extack, "No slot in VLAN table");
Vladimir Olteanb7a9e0d2021-01-09 02:01:46 +02001221 return -ENOSPC;
Vladimir Oltean31046a52021-02-13 22:43:18 +02001222 }
Hauke Mehrtens9bbb1c02019-05-06 00:25:08 +02001223 }
1224
1225 return 0;
1226}
1227
Vladimir Oltean1958d582021-01-09 02:01:53 +02001228static int gswip_port_vlan_add(struct dsa_switch *ds, int port,
Vladimir Oltean31046a52021-02-13 22:43:18 +02001229 const struct switchdev_obj_port_vlan *vlan,
1230 struct netlink_ext_ack *extack)
Hauke Mehrtens9bbb1c02019-05-06 00:25:08 +02001231{
Vladimir Oltean41fb0cf2021-12-06 18:57:53 +02001232 struct net_device *bridge = dsa_port_bridge_dev_get(dsa_to_port(ds, port));
Hauke Mehrtens9bbb1c02019-05-06 00:25:08 +02001233 struct gswip_priv *priv = ds->priv;
Hauke Mehrtens9bbb1c02019-05-06 00:25:08 +02001234 bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
1235 bool pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
Vladimir Oltean1958d582021-01-09 02:01:53 +02001236 int err;
1237
Vladimir Oltean31046a52021-02-13 22:43:18 +02001238 err = gswip_port_vlan_prepare(ds, port, vlan, extack);
Vladimir Oltean1958d582021-01-09 02:01:53 +02001239 if (err)
1240 return err;
Hauke Mehrtens9bbb1c02019-05-06 00:25:08 +02001241
1242 /* We have to receive all packets on the CPU port and should not
1243 * do any VLAN filtering here. This is also called with bridge
1244 * NULL and then we do not know for which bridge to configure
1245 * this.
1246 */
1247 if (dsa_is_cpu_port(ds, port))
Vladimir Oltean1958d582021-01-09 02:01:53 +02001248 return 0;
Hauke Mehrtens9bbb1c02019-05-06 00:25:08 +02001249
Vladimir Oltean1958d582021-01-09 02:01:53 +02001250 return gswip_vlan_add_aware(priv, bridge, port, vlan->vid,
1251 untagged, pvid);
Hauke Mehrtens9bbb1c02019-05-06 00:25:08 +02001252}
1253
1254static int gswip_port_vlan_del(struct dsa_switch *ds, int port,
1255 const struct switchdev_obj_port_vlan *vlan)
1256{
Vladimir Oltean41fb0cf2021-12-06 18:57:53 +02001257 struct net_device *bridge = dsa_port_bridge_dev_get(dsa_to_port(ds, port));
Hauke Mehrtens9bbb1c02019-05-06 00:25:08 +02001258 struct gswip_priv *priv = ds->priv;
Hauke Mehrtens9bbb1c02019-05-06 00:25:08 +02001259 bool pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
Hauke Mehrtens9bbb1c02019-05-06 00:25:08 +02001260
1261 /* We have to receive all packets on the CPU port and should not
1262 * do any VLAN filtering here. This is also called with bridge
1263 * NULL and then we do not know for which bridge to configure
1264 * this.
1265 */
1266 if (dsa_is_cpu_port(ds, port))
1267 return 0;
1268
Vladimir Olteanb7a9e0d2021-01-09 02:01:46 +02001269 return gswip_vlan_remove(priv, bridge, port, vlan->vid, pvid, true);
Hauke Mehrtens8206e0c2019-05-06 00:25:07 +02001270}
1271
Hauke Mehrtens45813482019-05-06 00:25:09 +02001272static void gswip_port_fast_age(struct dsa_switch *ds, int port)
1273{
1274 struct gswip_priv *priv = ds->priv;
1275 struct gswip_pce_table_entry mac_bridge = {0,};
1276 int i;
1277 int err;
1278
1279 for (i = 0; i < 2048; i++) {
1280 mac_bridge.table = GSWIP_TABLE_MAC_BRIDGE;
1281 mac_bridge.index = i;
1282
1283 err = gswip_pce_table_entry_read(priv, &mac_bridge);
1284 if (err) {
Colin Ian Kingd6759172019-05-08 11:22:09 +01001285 dev_err(priv->dev, "failed to read mac bridge: %d\n",
Hauke Mehrtens45813482019-05-06 00:25:09 +02001286 err);
1287 return;
1288 }
1289
1290 if (!mac_bridge.valid)
1291 continue;
1292
1293 if (mac_bridge.val[1] & GSWIP_TABLE_MAC_BRIDGE_STATIC)
1294 continue;
1295
1296 if (((mac_bridge.val[0] & GENMASK(7, 4)) >> 4) != port)
1297 continue;
1298
1299 mac_bridge.valid = false;
1300 err = gswip_pce_table_entry_write(priv, &mac_bridge);
1301 if (err) {
Colin Ian Kingd6759172019-05-08 11:22:09 +01001302 dev_err(priv->dev, "failed to write mac bridge: %d\n",
Hauke Mehrtens45813482019-05-06 00:25:09 +02001303 err);
1304 return;
1305 }
1306 }
1307}
1308
Hauke Mehrtens8206e0c2019-05-06 00:25:07 +02001309static void gswip_port_stp_state_set(struct dsa_switch *ds, int port, u8 state)
1310{
1311 struct gswip_priv *priv = ds->priv;
1312 u32 stp_state;
1313
1314 switch (state) {
1315 case BR_STATE_DISABLED:
1316 gswip_switch_mask(priv, GSWIP_SDMA_PCTRL_EN, 0,
1317 GSWIP_SDMA_PCTRLp(port));
1318 return;
1319 case BR_STATE_BLOCKING:
1320 case BR_STATE_LISTENING:
1321 stp_state = GSWIP_PCE_PCTRL_0_PSTATE_LISTEN;
1322 break;
1323 case BR_STATE_LEARNING:
1324 stp_state = GSWIP_PCE_PCTRL_0_PSTATE_LEARNING;
1325 break;
1326 case BR_STATE_FORWARDING:
1327 stp_state = GSWIP_PCE_PCTRL_0_PSTATE_FORWARDING;
1328 break;
1329 default:
1330 dev_err(priv->dev, "invalid STP state: %d\n", state);
1331 return;
1332 }
1333
1334 gswip_switch_mask(priv, 0, GSWIP_SDMA_PCTRL_EN,
1335 GSWIP_SDMA_PCTRLp(port));
1336 gswip_switch_mask(priv, GSWIP_PCE_PCTRL_0_PSTATE_MASK, stp_state,
1337 GSWIP_PCE_PCTRL_0p(port));
1338}
1339
Hauke Mehrtens58c59ef2019-05-06 00:25:10 +02001340static int gswip_port_fdb(struct dsa_switch *ds, int port,
1341 const unsigned char *addr, u16 vid, bool add)
1342{
Vladimir Oltean41fb0cf2021-12-06 18:57:53 +02001343 struct net_device *bridge = dsa_port_bridge_dev_get(dsa_to_port(ds, port));
Hauke Mehrtens58c59ef2019-05-06 00:25:10 +02001344 struct gswip_priv *priv = ds->priv;
Hauke Mehrtens58c59ef2019-05-06 00:25:10 +02001345 struct gswip_pce_table_entry mac_bridge = {0,};
1346 unsigned int cpu_port = priv->hw_info->cpu_port;
1347 int fid = -1;
1348 int i;
1349 int err;
1350
1351 if (!bridge)
1352 return -EINVAL;
1353
1354 for (i = cpu_port; i < ARRAY_SIZE(priv->vlans); i++) {
1355 if (priv->vlans[i].bridge == bridge) {
1356 fid = priv->vlans[i].fid;
1357 break;
1358 }
1359 }
1360
1361 if (fid == -1) {
1362 dev_err(priv->dev, "Port not part of a bridge\n");
1363 return -EINVAL;
1364 }
1365
1366 mac_bridge.table = GSWIP_TABLE_MAC_BRIDGE;
1367 mac_bridge.key_mode = true;
1368 mac_bridge.key[0] = addr[5] | (addr[4] << 8);
1369 mac_bridge.key[1] = addr[3] | (addr[2] << 8);
1370 mac_bridge.key[2] = addr[1] | (addr[0] << 8);
1371 mac_bridge.key[3] = fid;
1372 mac_bridge.val[0] = add ? BIT(port) : 0; /* port map */
1373 mac_bridge.val[1] = GSWIP_TABLE_MAC_BRIDGE_STATIC;
1374 mac_bridge.valid = add;
1375
1376 err = gswip_pce_table_entry_write(priv, &mac_bridge);
1377 if (err)
Colin Ian Kingd6759172019-05-08 11:22:09 +01001378 dev_err(priv->dev, "failed to write mac bridge: %d\n", err);
Hauke Mehrtens58c59ef2019-05-06 00:25:10 +02001379
1380 return err;
1381}
1382
1383static int gswip_port_fdb_add(struct dsa_switch *ds, int port,
1384 const unsigned char *addr, u16 vid)
1385{
1386 return gswip_port_fdb(ds, port, addr, vid, true);
1387}
1388
1389static int gswip_port_fdb_del(struct dsa_switch *ds, int port,
1390 const unsigned char *addr, u16 vid)
1391{
1392 return gswip_port_fdb(ds, port, addr, vid, false);
1393}
1394
1395static int gswip_port_fdb_dump(struct dsa_switch *ds, int port,
1396 dsa_fdb_dump_cb_t *cb, void *data)
1397{
1398 struct gswip_priv *priv = ds->priv;
1399 struct gswip_pce_table_entry mac_bridge = {0,};
1400 unsigned char addr[6];
1401 int i;
1402 int err;
1403
1404 for (i = 0; i < 2048; i++) {
1405 mac_bridge.table = GSWIP_TABLE_MAC_BRIDGE;
1406 mac_bridge.index = i;
1407
1408 err = gswip_pce_table_entry_read(priv, &mac_bridge);
1409 if (err) {
Colin Ian Kingd6759172019-05-08 11:22:09 +01001410 dev_err(priv->dev, "failed to write mac bridge: %d\n",
Hauke Mehrtens58c59ef2019-05-06 00:25:10 +02001411 err);
1412 return err;
1413 }
1414
1415 if (!mac_bridge.valid)
1416 continue;
1417
1418 addr[5] = mac_bridge.key[0] & 0xff;
1419 addr[4] = (mac_bridge.key[0] >> 8) & 0xff;
1420 addr[3] = mac_bridge.key[1] & 0xff;
1421 addr[2] = (mac_bridge.key[1] >> 8) & 0xff;
1422 addr[1] = mac_bridge.key[2] & 0xff;
1423 addr[0] = (mac_bridge.key[2] >> 8) & 0xff;
1424 if (mac_bridge.val[1] & GSWIP_TABLE_MAC_BRIDGE_STATIC) {
Vladimir Oltean871a73a2021-08-10 14:19:55 +03001425 if (mac_bridge.val[0] & BIT(port)) {
1426 err = cb(addr, 0, true, data);
1427 if (err)
1428 return err;
1429 }
Hauke Mehrtens58c59ef2019-05-06 00:25:10 +02001430 } else {
Vladimir Oltean871a73a2021-08-10 14:19:55 +03001431 if (((mac_bridge.val[0] & GENMASK(7, 4)) >> 4) == port) {
1432 err = cb(addr, 0, false, data);
1433 if (err)
1434 return err;
1435 }
Hauke Mehrtens58c59ef2019-05-06 00:25:10 +02001436 }
1437 }
1438 return 0;
1439}
1440
Russell King (Oracle)a2279b02021-11-30 13:10:16 +00001441static void gswip_xrx200_phylink_get_caps(struct dsa_switch *ds, int port,
1442 struct phylink_config *config)
Aleksander Jan Bajkowskia09d0422021-03-22 21:37:15 +01001443{
1444 switch (port) {
1445 case 0:
1446 case 1:
Russell King (Oracle)a2279b02021-11-30 13:10:16 +00001447 phy_interface_set_rgmii(config->supported_interfaces);
1448 __set_bit(PHY_INTERFACE_MODE_MII,
1449 config->supported_interfaces);
1450 __set_bit(PHY_INTERFACE_MODE_REVMII,
1451 config->supported_interfaces);
1452 __set_bit(PHY_INTERFACE_MODE_RMII,
1453 config->supported_interfaces);
Aleksander Jan Bajkowskia09d0422021-03-22 21:37:15 +01001454 break;
Russell King (Oracle)a2279b02021-11-30 13:10:16 +00001455
Aleksander Jan Bajkowskia09d0422021-03-22 21:37:15 +01001456 case 2:
1457 case 3:
1458 case 4:
Russell King (Oracle)a2279b02021-11-30 13:10:16 +00001459 __set_bit(PHY_INTERFACE_MODE_INTERNAL,
1460 config->supported_interfaces);
Aleksander Jan Bajkowskia09d0422021-03-22 21:37:15 +01001461 break;
Russell King (Oracle)a2279b02021-11-30 13:10:16 +00001462
Aleksander Jan Bajkowskia09d0422021-03-22 21:37:15 +01001463 case 5:
Russell King (Oracle)a2279b02021-11-30 13:10:16 +00001464 phy_interface_set_rgmii(config->supported_interfaces);
1465 __set_bit(PHY_INTERFACE_MODE_INTERNAL,
1466 config->supported_interfaces);
Aleksander Jan Bajkowskia09d0422021-03-22 21:37:15 +01001467 break;
Aleksander Jan Bajkowskia09d0422021-03-22 21:37:15 +01001468 }
1469
Russell King (Oracle)a2279b02021-11-30 13:10:16 +00001470 config->mac_capabilities = MAC_ASYM_PAUSE | MAC_SYM_PAUSE |
1471 MAC_10 | MAC_100 | MAC_1000;
Aleksander Jan Bajkowskia09d0422021-03-22 21:37:15 +01001472}
1473
Russell King (Oracle)a2279b02021-11-30 13:10:16 +00001474static void gswip_xrx300_phylink_get_caps(struct dsa_switch *ds, int port,
1475 struct phylink_config *config)
Aleksander Jan Bajkowskia09d0422021-03-22 21:37:15 +01001476{
1477 switch (port) {
1478 case 0:
Russell King (Oracle)a2279b02021-11-30 13:10:16 +00001479 phy_interface_set_rgmii(config->supported_interfaces);
1480 __set_bit(PHY_INTERFACE_MODE_GMII,
1481 config->supported_interfaces);
1482 __set_bit(PHY_INTERFACE_MODE_RMII,
1483 config->supported_interfaces);
Aleksander Jan Bajkowskia09d0422021-03-22 21:37:15 +01001484 break;
Russell King (Oracle)a2279b02021-11-30 13:10:16 +00001485
Aleksander Jan Bajkowskia09d0422021-03-22 21:37:15 +01001486 case 1:
1487 case 2:
1488 case 3:
1489 case 4:
Russell King (Oracle)a2279b02021-11-30 13:10:16 +00001490 __set_bit(PHY_INTERFACE_MODE_INTERNAL,
1491 config->supported_interfaces);
Aleksander Jan Bajkowskia09d0422021-03-22 21:37:15 +01001492 break;
Russell King (Oracle)a2279b02021-11-30 13:10:16 +00001493
Aleksander Jan Bajkowskia09d0422021-03-22 21:37:15 +01001494 case 5:
Russell King (Oracle)a2279b02021-11-30 13:10:16 +00001495 phy_interface_set_rgmii(config->supported_interfaces);
1496 __set_bit(PHY_INTERFACE_MODE_INTERNAL,
1497 config->supported_interfaces);
1498 __set_bit(PHY_INTERFACE_MODE_RMII,
1499 config->supported_interfaces);
Aleksander Jan Bajkowskia09d0422021-03-22 21:37:15 +01001500 break;
Aleksander Jan Bajkowskia09d0422021-03-22 21:37:15 +01001501 }
1502
Russell King (Oracle)a2279b02021-11-30 13:10:16 +00001503 config->mac_capabilities = MAC_ASYM_PAUSE | MAC_SYM_PAUSE |
1504 MAC_10 | MAC_100 | MAC_1000;
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02001505}
1506
Martin Blumenstingl3e9005b2021-04-08 20:38:27 +02001507static void gswip_port_set_link(struct gswip_priv *priv, int port, bool link)
1508{
1509 u32 mdio_phy;
1510
1511 if (link)
1512 mdio_phy = GSWIP_MDIO_PHY_LINK_UP;
1513 else
1514 mdio_phy = GSWIP_MDIO_PHY_LINK_DOWN;
1515
1516 gswip_mdio_mask(priv, GSWIP_MDIO_PHY_LINK_MASK, mdio_phy,
1517 GSWIP_MDIO_PHYp(port));
1518}
1519
1520static void gswip_port_set_speed(struct gswip_priv *priv, int port, int speed,
1521 phy_interface_t interface)
1522{
1523 u32 mdio_phy = 0, mii_cfg = 0, mac_ctrl_0 = 0;
1524
1525 switch (speed) {
1526 case SPEED_10:
1527 mdio_phy = GSWIP_MDIO_PHY_SPEED_M10;
1528
1529 if (interface == PHY_INTERFACE_MODE_RMII)
1530 mii_cfg = GSWIP_MII_CFG_RATE_M50;
1531 else
1532 mii_cfg = GSWIP_MII_CFG_RATE_M2P5;
1533
1534 mac_ctrl_0 = GSWIP_MAC_CTRL_0_GMII_MII;
1535 break;
1536
1537 case SPEED_100:
1538 mdio_phy = GSWIP_MDIO_PHY_SPEED_M100;
1539
1540 if (interface == PHY_INTERFACE_MODE_RMII)
1541 mii_cfg = GSWIP_MII_CFG_RATE_M50;
1542 else
1543 mii_cfg = GSWIP_MII_CFG_RATE_M25;
1544
1545 mac_ctrl_0 = GSWIP_MAC_CTRL_0_GMII_MII;
1546 break;
1547
1548 case SPEED_1000:
1549 mdio_phy = GSWIP_MDIO_PHY_SPEED_G1;
1550
1551 mii_cfg = GSWIP_MII_CFG_RATE_M125;
1552
1553 mac_ctrl_0 = GSWIP_MAC_CTRL_0_GMII_RGMII;
1554 break;
1555 }
1556
1557 gswip_mdio_mask(priv, GSWIP_MDIO_PHY_SPEED_MASK, mdio_phy,
1558 GSWIP_MDIO_PHYp(port));
1559 gswip_mii_mask_cfg(priv, GSWIP_MII_CFG_RATE_MASK, mii_cfg, port);
1560 gswip_switch_mask(priv, GSWIP_MAC_CTRL_0_GMII_MASK, mac_ctrl_0,
1561 GSWIP_MAC_CTRL_0p(port));
1562}
1563
1564static void gswip_port_set_duplex(struct gswip_priv *priv, int port, int duplex)
1565{
1566 u32 mac_ctrl_0, mdio_phy;
1567
1568 if (duplex == DUPLEX_FULL) {
1569 mac_ctrl_0 = GSWIP_MAC_CTRL_0_FDUP_EN;
1570 mdio_phy = GSWIP_MDIO_PHY_FDUP_EN;
1571 } else {
1572 mac_ctrl_0 = GSWIP_MAC_CTRL_0_FDUP_DIS;
1573 mdio_phy = GSWIP_MDIO_PHY_FDUP_DIS;
1574 }
1575
1576 gswip_switch_mask(priv, GSWIP_MAC_CTRL_0_FDUP_MASK, mac_ctrl_0,
1577 GSWIP_MAC_CTRL_0p(port));
1578 gswip_mdio_mask(priv, GSWIP_MDIO_PHY_FDUP_MASK, mdio_phy,
1579 GSWIP_MDIO_PHYp(port));
1580}
1581
1582static void gswip_port_set_pause(struct gswip_priv *priv, int port,
1583 bool tx_pause, bool rx_pause)
1584{
1585 u32 mac_ctrl_0, mdio_phy;
1586
1587 if (tx_pause && rx_pause) {
1588 mac_ctrl_0 = GSWIP_MAC_CTRL_0_FCON_RXTX;
1589 mdio_phy = GSWIP_MDIO_PHY_FCONTX_EN |
1590 GSWIP_MDIO_PHY_FCONRX_EN;
1591 } else if (tx_pause) {
1592 mac_ctrl_0 = GSWIP_MAC_CTRL_0_FCON_TX;
1593 mdio_phy = GSWIP_MDIO_PHY_FCONTX_EN |
1594 GSWIP_MDIO_PHY_FCONRX_DIS;
1595 } else if (rx_pause) {
1596 mac_ctrl_0 = GSWIP_MAC_CTRL_0_FCON_RX;
1597 mdio_phy = GSWIP_MDIO_PHY_FCONTX_DIS |
1598 GSWIP_MDIO_PHY_FCONRX_EN;
1599 } else {
1600 mac_ctrl_0 = GSWIP_MAC_CTRL_0_FCON_NONE;
1601 mdio_phy = GSWIP_MDIO_PHY_FCONTX_DIS |
1602 GSWIP_MDIO_PHY_FCONRX_DIS;
1603 }
1604
1605 gswip_switch_mask(priv, GSWIP_MAC_CTRL_0_FCON_MASK,
1606 mac_ctrl_0, GSWIP_MAC_CTRL_0p(port));
1607 gswip_mdio_mask(priv,
1608 GSWIP_MDIO_PHY_FCONTX_MASK |
1609 GSWIP_MDIO_PHY_FCONRX_MASK,
1610 mdio_phy, GSWIP_MDIO_PHYp(port));
1611}
1612
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02001613static void gswip_phylink_mac_config(struct dsa_switch *ds, int port,
1614 unsigned int mode,
1615 const struct phylink_link_state *state)
1616{
1617 struct gswip_priv *priv = ds->priv;
1618 u32 miicfg = 0;
1619
1620 miicfg |= GSWIP_MII_CFG_LDCLKDIS;
1621
1622 switch (state->interface) {
1623 case PHY_INTERFACE_MODE_MII:
1624 case PHY_INTERFACE_MODE_INTERNAL:
1625 miicfg |= GSWIP_MII_CFG_MODE_MIIM;
1626 break;
1627 case PHY_INTERFACE_MODE_REVMII:
1628 miicfg |= GSWIP_MII_CFG_MODE_MIIP;
1629 break;
1630 case PHY_INTERFACE_MODE_RMII:
1631 miicfg |= GSWIP_MII_CFG_MODE_RMIIM;
Martin Blumenstingl4b592322021-04-08 20:38:28 +02001632
1633 /* Configure the RMII clock as output: */
1634 miicfg |= GSWIP_MII_CFG_RMII_CLK;
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02001635 break;
1636 case PHY_INTERFACE_MODE_RGMII:
1637 case PHY_INTERFACE_MODE_RGMII_ID:
1638 case PHY_INTERFACE_MODE_RGMII_RXID:
1639 case PHY_INTERFACE_MODE_RGMII_TXID:
1640 miicfg |= GSWIP_MII_CFG_MODE_RGMII;
1641 break;
Aleksander Jan Bajkowskia09d0422021-03-22 21:37:15 +01001642 case PHY_INTERFACE_MODE_GMII:
1643 miicfg |= GSWIP_MII_CFG_MODE_GMII;
1644 break;
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02001645 default:
1646 dev_err(ds->dev,
1647 "Unsupported interface: %d\n", state->interface);
1648 return;
1649 }
Martin Blumenstingl4b592322021-04-08 20:38:28 +02001650
1651 gswip_mii_mask_cfg(priv,
1652 GSWIP_MII_CFG_MODE_MASK | GSWIP_MII_CFG_RMII_CLK |
1653 GSWIP_MII_CFG_RGMII_IBS | GSWIP_MII_CFG_LDCLKDIS,
1654 miicfg, port);
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02001655
1656 switch (state->interface) {
1657 case PHY_INTERFACE_MODE_RGMII_ID:
1658 gswip_mii_mask_pcdu(priv, GSWIP_MII_PCDU_TXDLY_MASK |
1659 GSWIP_MII_PCDU_RXDLY_MASK, 0, port);
1660 break;
1661 case PHY_INTERFACE_MODE_RGMII_RXID:
1662 gswip_mii_mask_pcdu(priv, GSWIP_MII_PCDU_RXDLY_MASK, 0, port);
1663 break;
1664 case PHY_INTERFACE_MODE_RGMII_TXID:
1665 gswip_mii_mask_pcdu(priv, GSWIP_MII_PCDU_TXDLY_MASK, 0, port);
1666 break;
1667 default:
1668 break;
1669 }
1670}
1671
1672static void gswip_phylink_mac_link_down(struct dsa_switch *ds, int port,
1673 unsigned int mode,
1674 phy_interface_t interface)
1675{
1676 struct gswip_priv *priv = ds->priv;
1677
1678 gswip_mii_mask_cfg(priv, GSWIP_MII_CFG_EN, 0, port);
Martin Blumenstingl3e9005b2021-04-08 20:38:27 +02001679
1680 if (!dsa_is_cpu_port(ds, port))
1681 gswip_port_set_link(priv, port, false);
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02001682}
1683
1684static void gswip_phylink_mac_link_up(struct dsa_switch *ds, int port,
1685 unsigned int mode,
1686 phy_interface_t interface,
Russell King5b502a72020-02-26 10:23:46 +00001687 struct phy_device *phydev,
1688 int speed, int duplex,
1689 bool tx_pause, bool rx_pause)
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02001690{
1691 struct gswip_priv *priv = ds->priv;
1692
Martin Blumenstingl3e9005b2021-04-08 20:38:27 +02001693 if (!dsa_is_cpu_port(ds, port)) {
1694 gswip_port_set_link(priv, port, true);
1695 gswip_port_set_speed(priv, port, speed, interface);
1696 gswip_port_set_duplex(priv, port, duplex);
1697 gswip_port_set_pause(priv, port, tx_pause, rx_pause);
1698 }
1699
Martin Blumenstinglc1a9ec72021-01-03 02:25:43 +01001700 gswip_mii_mask_cfg(priv, 0, GSWIP_MII_CFG_EN, port);
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02001701}
1702
1703static void gswip_get_strings(struct dsa_switch *ds, int port, u32 stringset,
1704 uint8_t *data)
1705{
1706 int i;
1707
1708 if (stringset != ETH_SS_STATS)
1709 return;
1710
1711 for (i = 0; i < ARRAY_SIZE(gswip_rmon_cnt); i++)
1712 strncpy(data + i * ETH_GSTRING_LEN, gswip_rmon_cnt[i].name,
1713 ETH_GSTRING_LEN);
1714}
1715
1716static u32 gswip_bcm_ram_entry_read(struct gswip_priv *priv, u32 table,
1717 u32 index)
1718{
1719 u32 result;
1720 int err;
1721
1722 gswip_switch_w(priv, index, GSWIP_BM_RAM_ADDR);
1723 gswip_switch_mask(priv, GSWIP_BM_RAM_CTRL_ADDR_MASK |
1724 GSWIP_BM_RAM_CTRL_OPMOD,
1725 table | GSWIP_BM_RAM_CTRL_BAS,
1726 GSWIP_BM_RAM_CTRL);
1727
1728 err = gswip_switch_r_timeout(priv, GSWIP_BM_RAM_CTRL,
1729 GSWIP_BM_RAM_CTRL_BAS);
1730 if (err) {
1731 dev_err(priv->dev, "timeout while reading table: %u, index: %u",
1732 table, index);
1733 return 0;
1734 }
1735
1736 result = gswip_switch_r(priv, GSWIP_BM_RAM_VAL(0));
1737 result |= gswip_switch_r(priv, GSWIP_BM_RAM_VAL(1)) << 16;
1738
1739 return result;
1740}
1741
1742static void gswip_get_ethtool_stats(struct dsa_switch *ds, int port,
1743 uint64_t *data)
1744{
1745 struct gswip_priv *priv = ds->priv;
1746 const struct gswip_rmon_cnt_desc *rmon_cnt;
1747 int i;
1748 u64 high;
1749
1750 for (i = 0; i < ARRAY_SIZE(gswip_rmon_cnt); i++) {
1751 rmon_cnt = &gswip_rmon_cnt[i];
1752
1753 data[i] = gswip_bcm_ram_entry_read(priv, port,
1754 rmon_cnt->offset);
1755 if (rmon_cnt->size == 2) {
1756 high = gswip_bcm_ram_entry_read(priv, port,
1757 rmon_cnt->offset + 1);
1758 data[i] |= high << 32;
1759 }
1760 }
1761}
1762
1763static int gswip_get_sset_count(struct dsa_switch *ds, int port, int sset)
1764{
1765 if (sset != ETH_SS_STATS)
1766 return 0;
1767
1768 return ARRAY_SIZE(gswip_rmon_cnt);
1769}
1770
Aleksander Jan Bajkowskia09d0422021-03-22 21:37:15 +01001771static const struct dsa_switch_ops gswip_xrx200_switch_ops = {
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02001772 .get_tag_protocol = gswip_get_tag_protocol,
1773 .setup = gswip_setup,
1774 .port_enable = gswip_port_enable,
1775 .port_disable = gswip_port_disable,
Hauke Mehrtens8206e0c2019-05-06 00:25:07 +02001776 .port_bridge_join = gswip_port_bridge_join,
1777 .port_bridge_leave = gswip_port_bridge_leave,
Hauke Mehrtens45813482019-05-06 00:25:09 +02001778 .port_fast_age = gswip_port_fast_age,
Hauke Mehrtens9bbb1c02019-05-06 00:25:08 +02001779 .port_vlan_filtering = gswip_port_vlan_filtering,
Hauke Mehrtens9bbb1c02019-05-06 00:25:08 +02001780 .port_vlan_add = gswip_port_vlan_add,
1781 .port_vlan_del = gswip_port_vlan_del,
Hauke Mehrtens8206e0c2019-05-06 00:25:07 +02001782 .port_stp_state_set = gswip_port_stp_state_set,
Hauke Mehrtens58c59ef2019-05-06 00:25:10 +02001783 .port_fdb_add = gswip_port_fdb_add,
1784 .port_fdb_del = gswip_port_fdb_del,
1785 .port_fdb_dump = gswip_port_fdb_dump,
Russell King (Oracle)a2279b02021-11-30 13:10:16 +00001786 .phylink_get_caps = gswip_xrx200_phylink_get_caps,
Aleksander Jan Bajkowskia09d0422021-03-22 21:37:15 +01001787 .phylink_mac_config = gswip_phylink_mac_config,
1788 .phylink_mac_link_down = gswip_phylink_mac_link_down,
1789 .phylink_mac_link_up = gswip_phylink_mac_link_up,
1790 .get_strings = gswip_get_strings,
1791 .get_ethtool_stats = gswip_get_ethtool_stats,
1792 .get_sset_count = gswip_get_sset_count,
1793};
1794
1795static const struct dsa_switch_ops gswip_xrx300_switch_ops = {
1796 .get_tag_protocol = gswip_get_tag_protocol,
1797 .setup = gswip_setup,
1798 .port_enable = gswip_port_enable,
1799 .port_disable = gswip_port_disable,
1800 .port_bridge_join = gswip_port_bridge_join,
1801 .port_bridge_leave = gswip_port_bridge_leave,
1802 .port_fast_age = gswip_port_fast_age,
1803 .port_vlan_filtering = gswip_port_vlan_filtering,
1804 .port_vlan_add = gswip_port_vlan_add,
1805 .port_vlan_del = gswip_port_vlan_del,
1806 .port_stp_state_set = gswip_port_stp_state_set,
1807 .port_fdb_add = gswip_port_fdb_add,
1808 .port_fdb_del = gswip_port_fdb_del,
1809 .port_fdb_dump = gswip_port_fdb_dump,
Russell King (Oracle)a2279b02021-11-30 13:10:16 +00001810 .phylink_get_caps = gswip_xrx300_phylink_get_caps,
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02001811 .phylink_mac_config = gswip_phylink_mac_config,
1812 .phylink_mac_link_down = gswip_phylink_mac_link_down,
1813 .phylink_mac_link_up = gswip_phylink_mac_link_up,
1814 .get_strings = gswip_get_strings,
1815 .get_ethtool_stats = gswip_get_ethtool_stats,
1816 .get_sset_count = gswip_get_sset_count,
1817};
1818
1819static const struct xway_gphy_match_data xrx200a1x_gphy_data = {
1820 .fe_firmware_name = "lantiq/xrx200_phy22f_a14.bin",
1821 .ge_firmware_name = "lantiq/xrx200_phy11g_a14.bin",
1822};
1823
1824static const struct xway_gphy_match_data xrx200a2x_gphy_data = {
1825 .fe_firmware_name = "lantiq/xrx200_phy22f_a22.bin",
1826 .ge_firmware_name = "lantiq/xrx200_phy11g_a22.bin",
1827};
1828
1829static const struct xway_gphy_match_data xrx300_gphy_data = {
1830 .fe_firmware_name = "lantiq/xrx300_phy22f_a21.bin",
1831 .ge_firmware_name = "lantiq/xrx300_phy11g_a21.bin",
1832};
1833
1834static const struct of_device_id xway_gphy_match[] = {
1835 { .compatible = "lantiq,xrx200-gphy-fw", .data = NULL },
1836 { .compatible = "lantiq,xrx200a1x-gphy-fw", .data = &xrx200a1x_gphy_data },
1837 { .compatible = "lantiq,xrx200a2x-gphy-fw", .data = &xrx200a2x_gphy_data },
1838 { .compatible = "lantiq,xrx300-gphy-fw", .data = &xrx300_gphy_data },
1839 { .compatible = "lantiq,xrx330-gphy-fw", .data = &xrx300_gphy_data },
1840 {},
1841};
1842
1843static int gswip_gphy_fw_load(struct gswip_priv *priv, struct gswip_gphy_fw *gphy_fw)
1844{
1845 struct device *dev = priv->dev;
1846 const struct firmware *fw;
1847 void *fw_addr;
1848 dma_addr_t dma_addr;
1849 dma_addr_t dev_addr;
1850 size_t size;
1851 int ret;
1852
1853 ret = clk_prepare_enable(gphy_fw->clk_gate);
1854 if (ret)
1855 return ret;
1856
1857 reset_control_assert(gphy_fw->reset);
1858
Aleksander Jan Bajkowski111b64e2021-09-12 13:58:07 +02001859 /* The vendor BSP uses a 200ms delay after asserting the reset line.
1860 * Without this some users are observing that the PHY is not coming up
1861 * on the MDIO bus.
1862 */
1863 msleep(200);
1864
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02001865 ret = request_firmware(&fw, gphy_fw->fw_name, dev);
1866 if (ret) {
1867 dev_err(dev, "failed to load firmware: %s, error: %i\n",
1868 gphy_fw->fw_name, ret);
1869 return ret;
1870 }
1871
1872 /* GPHY cores need the firmware code in a persistent and contiguous
1873 * memory area with a 16 kB boundary aligned start address.
1874 */
1875 size = fw->size + XRX200_GPHY_FW_ALIGN;
1876
1877 fw_addr = dmam_alloc_coherent(dev, size, &dma_addr, GFP_KERNEL);
1878 if (fw_addr) {
1879 fw_addr = PTR_ALIGN(fw_addr, XRX200_GPHY_FW_ALIGN);
1880 dev_addr = ALIGN(dma_addr, XRX200_GPHY_FW_ALIGN);
1881 memcpy(fw_addr, fw->data, fw->size);
1882 } else {
1883 dev_err(dev, "failed to alloc firmware memory\n");
1884 release_firmware(fw);
1885 return -ENOMEM;
1886 }
1887
1888 release_firmware(fw);
1889
1890 ret = regmap_write(priv->rcu_regmap, gphy_fw->fw_addr_offset, dev_addr);
1891 if (ret)
1892 return ret;
1893
1894 reset_control_deassert(gphy_fw->reset);
1895
1896 return ret;
1897}
1898
1899static int gswip_gphy_fw_probe(struct gswip_priv *priv,
1900 struct gswip_gphy_fw *gphy_fw,
1901 struct device_node *gphy_fw_np, int i)
1902{
1903 struct device *dev = priv->dev;
1904 u32 gphy_mode;
1905 int ret;
1906 char gphyname[10];
1907
1908 snprintf(gphyname, sizeof(gphyname), "gphy%d", i);
1909
1910 gphy_fw->clk_gate = devm_clk_get(dev, gphyname);
1911 if (IS_ERR(gphy_fw->clk_gate)) {
1912 dev_err(dev, "Failed to lookup gate clock\n");
1913 return PTR_ERR(gphy_fw->clk_gate);
1914 }
1915
1916 ret = of_property_read_u32(gphy_fw_np, "reg", &gphy_fw->fw_addr_offset);
1917 if (ret)
1918 return ret;
1919
1920 ret = of_property_read_u32(gphy_fw_np, "lantiq,gphy-mode", &gphy_mode);
1921 /* Default to GE mode */
1922 if (ret)
1923 gphy_mode = GPHY_MODE_GE;
1924
1925 switch (gphy_mode) {
1926 case GPHY_MODE_FE:
1927 gphy_fw->fw_name = priv->gphy_fw_name_cfg->fe_firmware_name;
1928 break;
1929 case GPHY_MODE_GE:
1930 gphy_fw->fw_name = priv->gphy_fw_name_cfg->ge_firmware_name;
1931 break;
1932 default:
1933 dev_err(dev, "Unknown GPHY mode %d\n", gphy_mode);
1934 return -EINVAL;
1935 }
1936
1937 gphy_fw->reset = of_reset_control_array_get_exclusive(gphy_fw_np);
Wei Yongjunf592e0b2018-09-15 01:33:38 +00001938 if (IS_ERR(gphy_fw->reset)) {
1939 if (PTR_ERR(gphy_fw->reset) != -EPROBE_DEFER)
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02001940 dev_err(dev, "Failed to lookup gphy reset\n");
Wei Yongjunf592e0b2018-09-15 01:33:38 +00001941 return PTR_ERR(gphy_fw->reset);
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02001942 }
1943
1944 return gswip_gphy_fw_load(priv, gphy_fw);
1945}
1946
1947static void gswip_gphy_fw_remove(struct gswip_priv *priv,
1948 struct gswip_gphy_fw *gphy_fw)
1949{
1950 int ret;
1951
1952 /* check if the device was fully probed */
1953 if (!gphy_fw->fw_name)
1954 return;
1955
1956 ret = regmap_write(priv->rcu_regmap, gphy_fw->fw_addr_offset, 0);
1957 if (ret)
1958 dev_err(priv->dev, "can not reset GPHY FW pointer");
1959
1960 clk_disable_unprepare(gphy_fw->clk_gate);
1961
1962 reset_control_put(gphy_fw->reset);
1963}
1964
1965static int gswip_gphy_fw_list(struct gswip_priv *priv,
1966 struct device_node *gphy_fw_list_np, u32 version)
1967{
1968 struct device *dev = priv->dev;
1969 struct device_node *gphy_fw_np;
1970 const struct of_device_id *match;
1971 int err;
1972 int i = 0;
1973
Hauke Mehrtens0e630b52018-09-15 14:08:48 +02001974 /* The VRX200 rev 1.1 uses the GSWIP 2.0 and needs the older
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02001975 * GPHY firmware. The VRX200 rev 1.2 uses the GSWIP 2.1 and also
1976 * needs a different GPHY firmware.
1977 */
1978 if (of_device_is_compatible(gphy_fw_list_np, "lantiq,xrx200-gphy-fw")) {
1979 switch (version) {
1980 case GSWIP_VERSION_2_0:
1981 priv->gphy_fw_name_cfg = &xrx200a1x_gphy_data;
1982 break;
1983 case GSWIP_VERSION_2_1:
1984 priv->gphy_fw_name_cfg = &xrx200a2x_gphy_data;
1985 break;
1986 default:
1987 dev_err(dev, "unknown GSWIP version: 0x%x", version);
1988 return -ENOENT;
1989 }
1990 }
1991
1992 match = of_match_node(xway_gphy_match, gphy_fw_list_np);
1993 if (match && match->data)
1994 priv->gphy_fw_name_cfg = match->data;
1995
1996 if (!priv->gphy_fw_name_cfg) {
1997 dev_err(dev, "GPHY compatible type not supported");
1998 return -ENOENT;
1999 }
2000
2001 priv->num_gphy_fw = of_get_available_child_count(gphy_fw_list_np);
2002 if (!priv->num_gphy_fw)
2003 return -ENOENT;
2004
2005 priv->rcu_regmap = syscon_regmap_lookup_by_phandle(gphy_fw_list_np,
2006 "lantiq,rcu");
2007 if (IS_ERR(priv->rcu_regmap))
2008 return PTR_ERR(priv->rcu_regmap);
2009
2010 priv->gphy_fw = devm_kmalloc_array(dev, priv->num_gphy_fw,
2011 sizeof(*priv->gphy_fw),
2012 GFP_KERNEL | __GFP_ZERO);
2013 if (!priv->gphy_fw)
2014 return -ENOMEM;
2015
2016 for_each_available_child_of_node(gphy_fw_list_np, gphy_fw_np) {
2017 err = gswip_gphy_fw_probe(priv, &priv->gphy_fw[i],
2018 gphy_fw_np, i);
2019 if (err)
2020 goto remove_gphy;
2021 i++;
2022 }
2023
Martin Blumenstingl2a1828e2020-11-15 17:57:57 +01002024 /* The standalone PHY11G requires 300ms to be fully
2025 * initialized and ready for any MDIO communication after being
2026 * taken out of reset. For the SoC-internal GPHY variant there
2027 * is no (known) documentation for the minimum time after a
2028 * reset. Use the same value as for the standalone variant as
2029 * some users have reported internal PHYs not being detected
2030 * without any delay.
2031 */
2032 msleep(300);
2033
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02002034 return 0;
2035
2036remove_gphy:
2037 for (i = 0; i < priv->num_gphy_fw; i++)
2038 gswip_gphy_fw_remove(priv, &priv->gphy_fw[i]);
2039 return err;
2040}
2041
2042static int gswip_probe(struct platform_device *pdev)
2043{
2044 struct gswip_priv *priv;
Aleksander Jan Bajkowski204c7612021-03-22 21:37:16 +01002045 struct device_node *np, *mdio_np, *gphy_fw_np;
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02002046 struct device *dev = &pdev->dev;
2047 int err;
2048 int i;
2049 u32 version;
2050
2051 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
2052 if (!priv)
2053 return -ENOMEM;
2054
YueHaibing6551c8c2019-08-01 20:25:46 +08002055 priv->gswip = devm_platform_ioremap_resource(pdev, 0);
Wei Yongjunf5de8bf2018-09-15 01:33:21 +00002056 if (IS_ERR(priv->gswip))
2057 return PTR_ERR(priv->gswip);
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02002058
YueHaibing6551c8c2019-08-01 20:25:46 +08002059 priv->mdio = devm_platform_ioremap_resource(pdev, 1);
Wei Yongjunf5de8bf2018-09-15 01:33:21 +00002060 if (IS_ERR(priv->mdio))
2061 return PTR_ERR(priv->mdio);
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02002062
YueHaibing6551c8c2019-08-01 20:25:46 +08002063 priv->mii = devm_platform_ioremap_resource(pdev, 2);
Wei Yongjunf5de8bf2018-09-15 01:33:21 +00002064 if (IS_ERR(priv->mii))
2065 return PTR_ERR(priv->mii);
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02002066
2067 priv->hw_info = of_device_get_match_data(dev);
2068 if (!priv->hw_info)
2069 return -EINVAL;
2070
Vivien Didelot7e99e342019-10-21 16:51:30 -04002071 priv->ds = devm_kzalloc(dev, sizeof(*priv->ds), GFP_KERNEL);
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02002072 if (!priv->ds)
2073 return -ENOMEM;
2074
Vivien Didelot7e99e342019-10-21 16:51:30 -04002075 priv->ds->dev = dev;
2076 priv->ds->num_ports = priv->hw_info->max_ports;
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02002077 priv->ds->priv = priv;
Aleksander Jan Bajkowskia09d0422021-03-22 21:37:15 +01002078 priv->ds->ops = priv->hw_info->ops;
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02002079 priv->dev = dev;
Vladimir Olteancf231b42021-10-24 20:17:53 +03002080 mutex_init(&priv->pce_table_lock);
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02002081 version = gswip_switch_r(priv, GSWIP_VERSION);
2082
Aleksander Jan Bajkowski204c7612021-03-22 21:37:16 +01002083 np = dev->of_node;
2084 switch (version) {
2085 case GSWIP_VERSION_2_0:
2086 case GSWIP_VERSION_2_1:
2087 if (!of_device_is_compatible(np, "lantiq,xrx200-gswip"))
2088 return -EINVAL;
2089 break;
2090 case GSWIP_VERSION_2_2:
2091 case GSWIP_VERSION_2_2_ETC:
2092 if (!of_device_is_compatible(np, "lantiq,xrx300-gswip") &&
2093 !of_device_is_compatible(np, "lantiq,xrx330-gswip"))
2094 return -EINVAL;
2095 break;
2096 default:
2097 dev_err(dev, "unknown GSWIP version: 0x%x", version);
2098 return -ENOENT;
2099 }
2100
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02002101 /* bring up the mdio bus */
Johan Hovoldc8cbcb02019-01-16 11:23:34 +01002102 gphy_fw_np = of_get_compatible_child(dev->of_node, "lantiq,gphy-fw");
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02002103 if (gphy_fw_np) {
2104 err = gswip_gphy_fw_list(priv, gphy_fw_np, version);
Johan Hovoldc8cbcb02019-01-16 11:23:34 +01002105 of_node_put(gphy_fw_np);
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02002106 if (err) {
2107 dev_err(dev, "gphy fw probe failed\n");
2108 return err;
2109 }
2110 }
2111
2112 /* bring up the mdio bus */
Johan Hovoldc8cbcb02019-01-16 11:23:34 +01002113 mdio_np = of_get_compatible_child(dev->of_node, "lantiq,xrx200-mdio");
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02002114 if (mdio_np) {
2115 err = gswip_mdio(priv, mdio_np);
2116 if (err) {
2117 dev_err(dev, "mdio probe failed\n");
Johan Hovoldc8cbcb02019-01-16 11:23:34 +01002118 goto put_mdio_node;
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02002119 }
2120 }
2121
2122 err = dsa_register_switch(priv->ds);
2123 if (err) {
2124 dev_err(dev, "dsa switch register failed: %i\n", err);
2125 goto mdio_bus;
2126 }
Hauke Mehrtens0e630b52018-09-15 14:08:48 +02002127 if (!dsa_is_cpu_port(priv->ds, priv->hw_info->cpu_port)) {
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02002128 dev_err(dev, "wrong CPU port defined, HW only supports port: %i",
2129 priv->hw_info->cpu_port);
2130 err = -EINVAL;
Johan Hovoldaed13f22019-01-16 11:23:33 +01002131 goto disable_switch;
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02002132 }
2133
2134 platform_set_drvdata(pdev, priv);
2135
2136 dev_info(dev, "probed GSWIP version %lx mod %lx\n",
2137 (version & GSWIP_VERSION_REV_MASK) >> GSWIP_VERSION_REV_SHIFT,
2138 (version & GSWIP_VERSION_MOD_MASK) >> GSWIP_VERSION_MOD_SHIFT);
2139 return 0;
2140
Johan Hovoldaed13f22019-01-16 11:23:33 +01002141disable_switch:
2142 gswip_mdio_mask(priv, GSWIP_MDIO_GLOB_ENABLE, 0, GSWIP_MDIO_GLOB);
2143 dsa_unregister_switch(priv->ds);
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02002144mdio_bus:
2145 if (mdio_np)
2146 mdiobus_unregister(priv->ds->slave_mii_bus);
Johan Hovoldc8cbcb02019-01-16 11:23:34 +01002147put_mdio_node:
2148 of_node_put(mdio_np);
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02002149 for (i = 0; i < priv->num_gphy_fw; i++)
2150 gswip_gphy_fw_remove(priv, &priv->gphy_fw[i]);
2151 return err;
2152}
2153
2154static int gswip_remove(struct platform_device *pdev)
2155{
2156 struct gswip_priv *priv = platform_get_drvdata(pdev);
2157 int i;
2158
Vladimir Oltean0650bf52021-09-17 16:34:33 +03002159 if (!priv)
2160 return 0;
2161
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02002162 /* disable the switch */
2163 gswip_mdio_mask(priv, GSWIP_MDIO_GLOB_ENABLE, 0, GSWIP_MDIO_GLOB);
2164
2165 dsa_unregister_switch(priv->ds);
2166
Johan Hovoldc8cbcb02019-01-16 11:23:34 +01002167 if (priv->ds->slave_mii_bus) {
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02002168 mdiobus_unregister(priv->ds->slave_mii_bus);
Johan Hovoldc8cbcb02019-01-16 11:23:34 +01002169 of_node_put(priv->ds->slave_mii_bus->dev.of_node);
2170 }
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02002171
2172 for (i = 0; i < priv->num_gphy_fw; i++)
2173 gswip_gphy_fw_remove(priv, &priv->gphy_fw[i]);
2174
Vladimir Oltean0650bf52021-09-17 16:34:33 +03002175 platform_set_drvdata(pdev, NULL);
2176
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02002177 return 0;
2178}
2179
Vladimir Oltean0650bf52021-09-17 16:34:33 +03002180static void gswip_shutdown(struct platform_device *pdev)
2181{
2182 struct gswip_priv *priv = platform_get_drvdata(pdev);
2183
2184 if (!priv)
2185 return;
2186
2187 dsa_switch_shutdown(priv->ds);
2188
2189 platform_set_drvdata(pdev, NULL);
2190}
2191
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02002192static const struct gswip_hw_info gswip_xrx200 = {
2193 .max_ports = 7,
2194 .cpu_port = 6,
Aleksander Jan Bajkowskia09d0422021-03-22 21:37:15 +01002195 .ops = &gswip_xrx200_switch_ops,
2196};
2197
2198static const struct gswip_hw_info gswip_xrx300 = {
2199 .max_ports = 7,
2200 .cpu_port = 6,
2201 .ops = &gswip_xrx300_switch_ops,
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02002202};
2203
2204static const struct of_device_id gswip_of_match[] = {
2205 { .compatible = "lantiq,xrx200-gswip", .data = &gswip_xrx200 },
Aleksander Jan Bajkowskia09d0422021-03-22 21:37:15 +01002206 { .compatible = "lantiq,xrx300-gswip", .data = &gswip_xrx300 },
2207 { .compatible = "lantiq,xrx330-gswip", .data = &gswip_xrx300 },
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02002208 {},
2209};
2210MODULE_DEVICE_TABLE(of, gswip_of_match);
2211
2212static struct platform_driver gswip_driver = {
2213 .probe = gswip_probe,
2214 .remove = gswip_remove,
Vladimir Oltean0650bf52021-09-17 16:34:33 +03002215 .shutdown = gswip_shutdown,
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02002216 .driver = {
2217 .name = "gswip",
2218 .of_match_table = gswip_of_match,
2219 },
2220};
2221
2222module_platform_driver(gswip_driver);
2223
Hauke Mehrtenscffde202019-02-22 20:11:13 +01002224MODULE_FIRMWARE("lantiq/xrx300_phy11g_a21.bin");
2225MODULE_FIRMWARE("lantiq/xrx300_phy22f_a21.bin");
2226MODULE_FIRMWARE("lantiq/xrx200_phy11g_a14.bin");
2227MODULE_FIRMWARE("lantiq/xrx200_phy11g_a22.bin");
2228MODULE_FIRMWARE("lantiq/xrx200_phy22f_a14.bin");
2229MODULE_FIRMWARE("lantiq/xrx200_phy22f_a22.bin");
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02002230MODULE_AUTHOR("Hauke Mehrtens <hauke@hauke-m.de>");
2231MODULE_DESCRIPTION("Lantiq / Intel GSWIP driver");
2232MODULE_LICENSE("GPL v2");