blob: 126d4ea868baf73bc7173f909bc614f2aa92b6ff [file] [log] [blame]
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Lantiq / Intel GSWIP switch driver for VRX200 SoCs
4 *
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))
Hauke Mehrtens14fceff2018-09-09 22:20:39 +020096#define GSWIP_MII_CFG_EN BIT(14)
97#define GSWIP_MII_CFG_LDCLKDIS BIT(12)
98#define GSWIP_MII_CFG_MODE_MIIP 0x0
99#define GSWIP_MII_CFG_MODE_MIIM 0x1
100#define GSWIP_MII_CFG_MODE_RMIIP 0x2
101#define GSWIP_MII_CFG_MODE_RMIIM 0x3
102#define GSWIP_MII_CFG_MODE_RGMII 0x4
103#define GSWIP_MII_CFG_MODE_MASK 0xf
104#define GSWIP_MII_CFG_RATE_M2P5 0x00
105#define GSWIP_MII_CFG_RATE_M25 0x10
106#define GSWIP_MII_CFG_RATE_M125 0x20
107#define GSWIP_MII_CFG_RATE_M50 0x30
108#define GSWIP_MII_CFG_RATE_AUTO 0x40
109#define GSWIP_MII_CFG_RATE_MASK 0x70
110#define GSWIP_MII_PCDU0 0x01
111#define GSWIP_MII_PCDU1 0x03
112#define GSWIP_MII_PCDU5 0x05
113#define GSWIP_MII_PCDU_TXDLY_MASK GENMASK(2, 0)
114#define GSWIP_MII_PCDU_RXDLY_MASK GENMASK(9, 7)
115
116/* GSWIP Core Registers */
117#define GSWIP_SWRES 0x000
118#define GSWIP_SWRES_R1 BIT(1) /* GSWIP Software reset */
119#define GSWIP_SWRES_R0 BIT(0) /* GSWIP Hardware reset */
120#define GSWIP_VERSION 0x013
121#define GSWIP_VERSION_REV_SHIFT 0
122#define GSWIP_VERSION_REV_MASK GENMASK(7, 0)
123#define GSWIP_VERSION_MOD_SHIFT 8
124#define GSWIP_VERSION_MOD_MASK GENMASK(15, 8)
125#define GSWIP_VERSION_2_0 0x100
126#define GSWIP_VERSION_2_1 0x021
127#define GSWIP_VERSION_2_2 0x122
128#define GSWIP_VERSION_2_2_ETC 0x022
129
130#define GSWIP_BM_RAM_VAL(x) (0x043 - (x))
131#define GSWIP_BM_RAM_ADDR 0x044
132#define GSWIP_BM_RAM_CTRL 0x045
133#define GSWIP_BM_RAM_CTRL_BAS BIT(15)
134#define GSWIP_BM_RAM_CTRL_OPMOD BIT(5)
135#define GSWIP_BM_RAM_CTRL_ADDR_MASK GENMASK(4, 0)
136#define GSWIP_BM_QUEUE_GCTRL 0x04A
137#define GSWIP_BM_QUEUE_GCTRL_GL_MOD BIT(10)
138/* buffer management Port Configuration Register */
139#define GSWIP_BM_PCFGp(p) (0x080 + ((p) * 2))
140#define GSWIP_BM_PCFG_CNTEN BIT(0) /* RMON Counter Enable */
141#define GSWIP_BM_PCFG_IGCNT BIT(1) /* Ingres Special Tag RMON count */
142/* buffer management Port Control Register */
143#define GSWIP_BM_RMON_CTRLp(p) (0x81 + ((p) * 2))
144#define GSWIP_BM_CTRL_RMON_RAM1_RES BIT(0) /* Software Reset for RMON RAM 1 */
145#define GSWIP_BM_CTRL_RMON_RAM2_RES BIT(1) /* Software Reset for RMON RAM 2 */
146
147/* PCE */
148#define GSWIP_PCE_TBL_KEY(x) (0x447 - (x))
149#define GSWIP_PCE_TBL_MASK 0x448
150#define GSWIP_PCE_TBL_VAL(x) (0x44D - (x))
151#define GSWIP_PCE_TBL_ADDR 0x44E
152#define GSWIP_PCE_TBL_CTRL 0x44F
153#define GSWIP_PCE_TBL_CTRL_BAS BIT(15)
154#define GSWIP_PCE_TBL_CTRL_TYPE BIT(13)
155#define GSWIP_PCE_TBL_CTRL_VLD BIT(12)
156#define GSWIP_PCE_TBL_CTRL_KEYFORM BIT(11)
157#define GSWIP_PCE_TBL_CTRL_GMAP_MASK GENMASK(10, 7)
158#define GSWIP_PCE_TBL_CTRL_OPMOD_MASK GENMASK(6, 5)
159#define GSWIP_PCE_TBL_CTRL_OPMOD_ADRD 0x00
160#define GSWIP_PCE_TBL_CTRL_OPMOD_ADWR 0x20
161#define GSWIP_PCE_TBL_CTRL_OPMOD_KSRD 0x40
162#define GSWIP_PCE_TBL_CTRL_OPMOD_KSWR 0x60
163#define GSWIP_PCE_TBL_CTRL_ADDR_MASK GENMASK(4, 0)
164#define GSWIP_PCE_PMAP1 0x453 /* Monitoring port map */
165#define GSWIP_PCE_PMAP2 0x454 /* Default Multicast port map */
166#define GSWIP_PCE_PMAP3 0x455 /* Default Unknown Unicast port map */
167#define GSWIP_PCE_GCTRL_0 0x456
Hauke Mehrtens8206e0c2019-05-06 00:25:07 +0200168#define GSWIP_PCE_GCTRL_0_MTFL BIT(0) /* MAC Table Flushing */
Hauke Mehrtens14fceff2018-09-09 22:20:39 +0200169#define GSWIP_PCE_GCTRL_0_MC_VALID BIT(3)
170#define GSWIP_PCE_GCTRL_0_VLAN BIT(14) /* VLAN aware Switching */
171#define GSWIP_PCE_GCTRL_1 0x457
172#define GSWIP_PCE_GCTRL_1_MAC_GLOCK BIT(2) /* MAC Address table lock */
173#define GSWIP_PCE_GCTRL_1_MAC_GLOCK_MOD BIT(3) /* Mac address table lock forwarding mode */
174#define GSWIP_PCE_PCTRL_0p(p) (0x480 + ((p) * 0xA))
Hauke Mehrtens8206e0c2019-05-06 00:25:07 +0200175#define GSWIP_PCE_PCTRL_0_TVM BIT(5) /* Transparent VLAN mode */
176#define GSWIP_PCE_PCTRL_0_VREP BIT(6) /* VLAN Replace Mode */
177#define GSWIP_PCE_PCTRL_0_INGRESS BIT(11) /* Accept special tag in ingress */
Hauke Mehrtens14fceff2018-09-09 22:20:39 +0200178#define GSWIP_PCE_PCTRL_0_PSTATE_LISTEN 0x0
179#define GSWIP_PCE_PCTRL_0_PSTATE_RX 0x1
180#define GSWIP_PCE_PCTRL_0_PSTATE_TX 0x2
181#define GSWIP_PCE_PCTRL_0_PSTATE_LEARNING 0x3
182#define GSWIP_PCE_PCTRL_0_PSTATE_FORWARDING 0x7
183#define GSWIP_PCE_PCTRL_0_PSTATE_MASK GENMASK(2, 0)
Hauke Mehrtens8206e0c2019-05-06 00:25:07 +0200184#define GSWIP_PCE_VCTRL(p) (0x485 + ((p) * 0xA))
185#define GSWIP_PCE_VCTRL_UVR BIT(0) /* Unknown VLAN Rule */
186#define GSWIP_PCE_VCTRL_VIMR BIT(3) /* VLAN Ingress Member violation rule */
187#define GSWIP_PCE_VCTRL_VEMR BIT(4) /* VLAN Egress Member violation rule */
188#define GSWIP_PCE_VCTRL_VSR BIT(5) /* VLAN Security */
189#define GSWIP_PCE_VCTRL_VID0 BIT(6) /* Priority Tagged Rule */
190#define GSWIP_PCE_DEFPVID(p) (0x486 + ((p) * 0xA))
Hauke Mehrtens14fceff2018-09-09 22:20:39 +0200191
192#define GSWIP_MAC_FLEN 0x8C5
Martin Blumenstingl3e9005b2021-04-08 20:38:27 +0200193#define GSWIP_MAC_CTRL_0p(p) (0x903 + ((p) * 0xC))
194#define GSWIP_MAC_CTRL_0_PADEN BIT(8)
195#define GSWIP_MAC_CTRL_0_FCS_EN BIT(7)
196#define GSWIP_MAC_CTRL_0_FCON_MASK 0x0070
197#define GSWIP_MAC_CTRL_0_FCON_AUTO 0x0000
198#define GSWIP_MAC_CTRL_0_FCON_RX 0x0010
199#define GSWIP_MAC_CTRL_0_FCON_TX 0x0020
200#define GSWIP_MAC_CTRL_0_FCON_RXTX 0x0030
201#define GSWIP_MAC_CTRL_0_FCON_NONE 0x0040
202#define GSWIP_MAC_CTRL_0_FDUP_MASK 0x000C
203#define GSWIP_MAC_CTRL_0_FDUP_AUTO 0x0000
204#define GSWIP_MAC_CTRL_0_FDUP_EN 0x0004
205#define GSWIP_MAC_CTRL_0_FDUP_DIS 0x000C
206#define GSWIP_MAC_CTRL_0_GMII_MASK 0x0003
207#define GSWIP_MAC_CTRL_0_GMII_AUTO 0x0000
208#define GSWIP_MAC_CTRL_0_GMII_MII 0x0001
209#define GSWIP_MAC_CTRL_0_GMII_RGMII 0x0002
Hauke Mehrtens14fceff2018-09-09 22:20:39 +0200210#define GSWIP_MAC_CTRL_2p(p) (0x905 + ((p) * 0xC))
211#define GSWIP_MAC_CTRL_2_MLEN BIT(3) /* Maximum Untagged Frame Lnegth */
212
213/* Ethernet Switch Fetch DMA Port Control Register */
214#define GSWIP_FDMA_PCTRLp(p) (0xA80 + ((p) * 0x6))
215#define GSWIP_FDMA_PCTRL_EN BIT(0) /* FDMA Port Enable */
216#define GSWIP_FDMA_PCTRL_STEN BIT(1) /* Special Tag Insertion Enable */
217#define GSWIP_FDMA_PCTRL_VLANMOD_MASK GENMASK(4, 3) /* VLAN Modification Control */
218#define GSWIP_FDMA_PCTRL_VLANMOD_SHIFT 3 /* VLAN Modification Control */
219#define GSWIP_FDMA_PCTRL_VLANMOD_DIS (0x0 << GSWIP_FDMA_PCTRL_VLANMOD_SHIFT)
220#define GSWIP_FDMA_PCTRL_VLANMOD_PRIO (0x1 << GSWIP_FDMA_PCTRL_VLANMOD_SHIFT)
221#define GSWIP_FDMA_PCTRL_VLANMOD_ID (0x2 << GSWIP_FDMA_PCTRL_VLANMOD_SHIFT)
222#define GSWIP_FDMA_PCTRL_VLANMOD_BOTH (0x3 << GSWIP_FDMA_PCTRL_VLANMOD_SHIFT)
223
224/* Ethernet Switch Store DMA Port Control Register */
225#define GSWIP_SDMA_PCTRLp(p) (0xBC0 + ((p) * 0x6))
226#define GSWIP_SDMA_PCTRL_EN BIT(0) /* SDMA Port Enable */
227#define GSWIP_SDMA_PCTRL_FCEN BIT(1) /* Flow Control Enable */
228#define GSWIP_SDMA_PCTRL_PAUFWD BIT(1) /* Pause Frame Forwarding */
229
Hauke Mehrtens8206e0c2019-05-06 00:25:07 +0200230#define GSWIP_TABLE_ACTIVE_VLAN 0x01
231#define GSWIP_TABLE_VLAN_MAPPING 0x02
Hauke Mehrtens45813482019-05-06 00:25:09 +0200232#define GSWIP_TABLE_MAC_BRIDGE 0x0b
233#define GSWIP_TABLE_MAC_BRIDGE_STATIC 0x01 /* Static not, aging entry */
Hauke Mehrtens8206e0c2019-05-06 00:25:07 +0200234
Hauke Mehrtens14fceff2018-09-09 22:20:39 +0200235#define XRX200_GPHY_FW_ALIGN (16 * 1024)
236
237struct gswip_hw_info {
238 int max_ports;
239 int cpu_port;
240};
241
242struct xway_gphy_match_data {
243 char *fe_firmware_name;
244 char *ge_firmware_name;
245};
246
247struct gswip_gphy_fw {
248 struct clk *clk_gate;
249 struct reset_control *reset;
250 u32 fw_addr_offset;
251 char *fw_name;
252};
253
Hauke Mehrtens8206e0c2019-05-06 00:25:07 +0200254struct gswip_vlan {
255 struct net_device *bridge;
256 u16 vid;
257 u8 fid;
258};
259
Hauke Mehrtens14fceff2018-09-09 22:20:39 +0200260struct gswip_priv {
261 __iomem void *gswip;
262 __iomem void *mdio;
263 __iomem void *mii;
264 const struct gswip_hw_info *hw_info;
265 const struct xway_gphy_match_data *gphy_fw_name_cfg;
266 struct dsa_switch *ds;
267 struct device *dev;
268 struct regmap *rcu_regmap;
Hauke Mehrtens8206e0c2019-05-06 00:25:07 +0200269 struct gswip_vlan vlans[64];
Hauke Mehrtens14fceff2018-09-09 22:20:39 +0200270 int num_gphy_fw;
271 struct gswip_gphy_fw *gphy_fw;
Hauke Mehrtens9bbb1c02019-05-06 00:25:08 +0200272 u32 port_vlan_filter;
Hauke Mehrtens14fceff2018-09-09 22:20:39 +0200273};
274
Hauke Mehrtens8206e0c2019-05-06 00:25:07 +0200275struct gswip_pce_table_entry {
276 u16 index; // PCE_TBL_ADDR.ADDR = pData->table_index
277 u16 table; // PCE_TBL_CTRL.ADDR = pData->table
278 u16 key[8];
279 u16 val[5];
280 u16 mask;
281 u8 gmap;
282 bool type;
283 bool valid;
284 bool key_mode;
285};
286
Hauke Mehrtens14fceff2018-09-09 22:20:39 +0200287struct gswip_rmon_cnt_desc {
288 unsigned int size;
289 unsigned int offset;
290 const char *name;
291};
292
293#define MIB_DESC(_size, _offset, _name) {.size = _size, .offset = _offset, .name = _name}
294
295static const struct gswip_rmon_cnt_desc gswip_rmon_cnt[] = {
296 /** Receive Packet Count (only packets that are accepted and not discarded). */
297 MIB_DESC(1, 0x1F, "RxGoodPkts"),
298 MIB_DESC(1, 0x23, "RxUnicastPkts"),
299 MIB_DESC(1, 0x22, "RxMulticastPkts"),
300 MIB_DESC(1, 0x21, "RxFCSErrorPkts"),
301 MIB_DESC(1, 0x1D, "RxUnderSizeGoodPkts"),
302 MIB_DESC(1, 0x1E, "RxUnderSizeErrorPkts"),
303 MIB_DESC(1, 0x1B, "RxOversizeGoodPkts"),
304 MIB_DESC(1, 0x1C, "RxOversizeErrorPkts"),
305 MIB_DESC(1, 0x20, "RxGoodPausePkts"),
306 MIB_DESC(1, 0x1A, "RxAlignErrorPkts"),
307 MIB_DESC(1, 0x12, "Rx64BytePkts"),
308 MIB_DESC(1, 0x13, "Rx127BytePkts"),
309 MIB_DESC(1, 0x14, "Rx255BytePkts"),
310 MIB_DESC(1, 0x15, "Rx511BytePkts"),
311 MIB_DESC(1, 0x16, "Rx1023BytePkts"),
312 /** Receive Size 1024-1522 (or more, if configured) Packet Count. */
313 MIB_DESC(1, 0x17, "RxMaxBytePkts"),
314 MIB_DESC(1, 0x18, "RxDroppedPkts"),
315 MIB_DESC(1, 0x19, "RxFilteredPkts"),
316 MIB_DESC(2, 0x24, "RxGoodBytes"),
317 MIB_DESC(2, 0x26, "RxBadBytes"),
318 MIB_DESC(1, 0x11, "TxAcmDroppedPkts"),
319 MIB_DESC(1, 0x0C, "TxGoodPkts"),
320 MIB_DESC(1, 0x06, "TxUnicastPkts"),
321 MIB_DESC(1, 0x07, "TxMulticastPkts"),
322 MIB_DESC(1, 0x00, "Tx64BytePkts"),
323 MIB_DESC(1, 0x01, "Tx127BytePkts"),
324 MIB_DESC(1, 0x02, "Tx255BytePkts"),
325 MIB_DESC(1, 0x03, "Tx511BytePkts"),
326 MIB_DESC(1, 0x04, "Tx1023BytePkts"),
327 /** Transmit Size 1024-1522 (or more, if configured) Packet Count. */
328 MIB_DESC(1, 0x05, "TxMaxBytePkts"),
329 MIB_DESC(1, 0x08, "TxSingleCollCount"),
330 MIB_DESC(1, 0x09, "TxMultCollCount"),
331 MIB_DESC(1, 0x0A, "TxLateCollCount"),
332 MIB_DESC(1, 0x0B, "TxExcessCollCount"),
333 MIB_DESC(1, 0x0D, "TxPauseCount"),
334 MIB_DESC(1, 0x10, "TxDroppedPkts"),
335 MIB_DESC(2, 0x0E, "TxGoodBytes"),
336};
337
338static u32 gswip_switch_r(struct gswip_priv *priv, u32 offset)
339{
340 return __raw_readl(priv->gswip + (offset * 4));
341}
342
343static void gswip_switch_w(struct gswip_priv *priv, u32 val, u32 offset)
344{
345 __raw_writel(val, priv->gswip + (offset * 4));
346}
347
348static void gswip_switch_mask(struct gswip_priv *priv, u32 clear, u32 set,
349 u32 offset)
350{
351 u32 val = gswip_switch_r(priv, offset);
352
353 val &= ~(clear);
354 val |= set;
355 gswip_switch_w(priv, val, offset);
356}
357
358static u32 gswip_switch_r_timeout(struct gswip_priv *priv, u32 offset,
359 u32 cleared)
360{
361 u32 val;
362
363 return readx_poll_timeout(__raw_readl, priv->gswip + (offset * 4), val,
364 (val & cleared) == 0, 20, 50000);
365}
366
367static u32 gswip_mdio_r(struct gswip_priv *priv, u32 offset)
368{
369 return __raw_readl(priv->mdio + (offset * 4));
370}
371
372static void gswip_mdio_w(struct gswip_priv *priv, u32 val, u32 offset)
373{
374 __raw_writel(val, priv->mdio + (offset * 4));
375}
376
377static void gswip_mdio_mask(struct gswip_priv *priv, u32 clear, u32 set,
378 u32 offset)
379{
380 u32 val = gswip_mdio_r(priv, offset);
381
382 val &= ~(clear);
383 val |= set;
384 gswip_mdio_w(priv, val, offset);
385}
386
387static u32 gswip_mii_r(struct gswip_priv *priv, u32 offset)
388{
389 return __raw_readl(priv->mii + (offset * 4));
390}
391
392static void gswip_mii_w(struct gswip_priv *priv, u32 val, u32 offset)
393{
394 __raw_writel(val, priv->mii + (offset * 4));
395}
396
397static void gswip_mii_mask(struct gswip_priv *priv, u32 clear, u32 set,
398 u32 offset)
399{
400 u32 val = gswip_mii_r(priv, offset);
401
402 val &= ~(clear);
403 val |= set;
404 gswip_mii_w(priv, val, offset);
405}
406
407static void gswip_mii_mask_cfg(struct gswip_priv *priv, u32 clear, u32 set,
408 int port)
409{
Martin Blumenstingl709a3c92021-01-03 02:25:44 +0100410 /* There's no MII_CFG register for the CPU port */
411 if (!dsa_is_cpu_port(priv->ds, port))
412 gswip_mii_mask(priv, clear, set, GSWIP_MII_CFGp(port));
Hauke Mehrtens14fceff2018-09-09 22:20:39 +0200413}
414
415static void gswip_mii_mask_pcdu(struct gswip_priv *priv, u32 clear, u32 set,
416 int port)
417{
418 switch (port) {
419 case 0:
420 gswip_mii_mask(priv, clear, set, GSWIP_MII_PCDU0);
421 break;
422 case 1:
423 gswip_mii_mask(priv, clear, set, GSWIP_MII_PCDU1);
424 break;
425 case 5:
426 gswip_mii_mask(priv, clear, set, GSWIP_MII_PCDU5);
427 break;
428 }
429}
430
431static int gswip_mdio_poll(struct gswip_priv *priv)
432{
433 int cnt = 100;
434
435 while (likely(cnt--)) {
436 u32 ctrl = gswip_mdio_r(priv, GSWIP_MDIO_CTRL);
437
438 if ((ctrl & GSWIP_MDIO_CTRL_BUSY) == 0)
439 return 0;
440 usleep_range(20, 40);
441 }
442
443 return -ETIMEDOUT;
444}
445
446static int gswip_mdio_wr(struct mii_bus *bus, int addr, int reg, u16 val)
447{
448 struct gswip_priv *priv = bus->priv;
449 int err;
450
451 err = gswip_mdio_poll(priv);
452 if (err) {
453 dev_err(&bus->dev, "waiting for MDIO bus busy timed out\n");
454 return err;
455 }
456
457 gswip_mdio_w(priv, val, GSWIP_MDIO_WRITE);
458 gswip_mdio_w(priv, GSWIP_MDIO_CTRL_BUSY | GSWIP_MDIO_CTRL_WR |
459 ((addr & GSWIP_MDIO_CTRL_PHYAD_MASK) << GSWIP_MDIO_CTRL_PHYAD_SHIFT) |
460 (reg & GSWIP_MDIO_CTRL_REGAD_MASK),
461 GSWIP_MDIO_CTRL);
462
463 return 0;
464}
465
466static int gswip_mdio_rd(struct mii_bus *bus, int addr, int reg)
467{
468 struct gswip_priv *priv = bus->priv;
469 int err;
470
471 err = gswip_mdio_poll(priv);
472 if (err) {
473 dev_err(&bus->dev, "waiting for MDIO bus busy timed out\n");
474 return err;
475 }
476
477 gswip_mdio_w(priv, GSWIP_MDIO_CTRL_BUSY | GSWIP_MDIO_CTRL_RD |
478 ((addr & GSWIP_MDIO_CTRL_PHYAD_MASK) << GSWIP_MDIO_CTRL_PHYAD_SHIFT) |
479 (reg & GSWIP_MDIO_CTRL_REGAD_MASK),
480 GSWIP_MDIO_CTRL);
481
482 err = gswip_mdio_poll(priv);
483 if (err) {
484 dev_err(&bus->dev, "waiting for MDIO bus busy timed out\n");
485 return err;
486 }
487
488 return gswip_mdio_r(priv, GSWIP_MDIO_READ);
489}
490
491static int gswip_mdio(struct gswip_priv *priv, struct device_node *mdio_np)
492{
493 struct dsa_switch *ds = priv->ds;
494
495 ds->slave_mii_bus = devm_mdiobus_alloc(priv->dev);
496 if (!ds->slave_mii_bus)
497 return -ENOMEM;
498
499 ds->slave_mii_bus->priv = priv;
500 ds->slave_mii_bus->read = gswip_mdio_rd;
501 ds->slave_mii_bus->write = gswip_mdio_wr;
502 ds->slave_mii_bus->name = "lantiq,xrx200-mdio";
503 snprintf(ds->slave_mii_bus->id, MII_BUS_ID_SIZE, "%s-mii",
504 dev_name(priv->dev));
505 ds->slave_mii_bus->parent = priv->dev;
506 ds->slave_mii_bus->phy_mask = ~ds->phys_mii_mask;
507
508 return of_mdiobus_register(ds->slave_mii_bus, mdio_np);
509}
510
Hauke Mehrtens8206e0c2019-05-06 00:25:07 +0200511static int gswip_pce_table_entry_read(struct gswip_priv *priv,
512 struct gswip_pce_table_entry *tbl)
513{
514 int i;
515 int err;
516 u16 crtl;
517 u16 addr_mode = tbl->key_mode ? GSWIP_PCE_TBL_CTRL_OPMOD_KSRD :
518 GSWIP_PCE_TBL_CTRL_OPMOD_ADRD;
519
520 err = gswip_switch_r_timeout(priv, GSWIP_PCE_TBL_CTRL,
521 GSWIP_PCE_TBL_CTRL_BAS);
522 if (err)
523 return err;
524
525 gswip_switch_w(priv, tbl->index, GSWIP_PCE_TBL_ADDR);
526 gswip_switch_mask(priv, GSWIP_PCE_TBL_CTRL_ADDR_MASK |
527 GSWIP_PCE_TBL_CTRL_OPMOD_MASK,
528 tbl->table | addr_mode | GSWIP_PCE_TBL_CTRL_BAS,
529 GSWIP_PCE_TBL_CTRL);
530
531 err = gswip_switch_r_timeout(priv, GSWIP_PCE_TBL_CTRL,
532 GSWIP_PCE_TBL_CTRL_BAS);
533 if (err)
534 return err;
535
536 for (i = 0; i < ARRAY_SIZE(tbl->key); i++)
537 tbl->key[i] = gswip_switch_r(priv, GSWIP_PCE_TBL_KEY(i));
538
539 for (i = 0; i < ARRAY_SIZE(tbl->val); i++)
540 tbl->val[i] = gswip_switch_r(priv, GSWIP_PCE_TBL_VAL(i));
541
542 tbl->mask = gswip_switch_r(priv, GSWIP_PCE_TBL_MASK);
543
544 crtl = gswip_switch_r(priv, GSWIP_PCE_TBL_CTRL);
545
546 tbl->type = !!(crtl & GSWIP_PCE_TBL_CTRL_TYPE);
547 tbl->valid = !!(crtl & GSWIP_PCE_TBL_CTRL_VLD);
548 tbl->gmap = (crtl & GSWIP_PCE_TBL_CTRL_GMAP_MASK) >> 7;
549
550 return 0;
551}
552
553static int gswip_pce_table_entry_write(struct gswip_priv *priv,
554 struct gswip_pce_table_entry *tbl)
555{
556 int i;
557 int err;
558 u16 crtl;
559 u16 addr_mode = tbl->key_mode ? GSWIP_PCE_TBL_CTRL_OPMOD_KSWR :
560 GSWIP_PCE_TBL_CTRL_OPMOD_ADWR;
561
562 err = gswip_switch_r_timeout(priv, GSWIP_PCE_TBL_CTRL,
563 GSWIP_PCE_TBL_CTRL_BAS);
564 if (err)
565 return err;
566
567 gswip_switch_w(priv, tbl->index, GSWIP_PCE_TBL_ADDR);
568 gswip_switch_mask(priv, GSWIP_PCE_TBL_CTRL_ADDR_MASK |
569 GSWIP_PCE_TBL_CTRL_OPMOD_MASK,
570 tbl->table | addr_mode,
571 GSWIP_PCE_TBL_CTRL);
572
573 for (i = 0; i < ARRAY_SIZE(tbl->key); i++)
574 gswip_switch_w(priv, tbl->key[i], GSWIP_PCE_TBL_KEY(i));
575
576 for (i = 0; i < ARRAY_SIZE(tbl->val); i++)
577 gswip_switch_w(priv, tbl->val[i], GSWIP_PCE_TBL_VAL(i));
578
579 gswip_switch_mask(priv, GSWIP_PCE_TBL_CTRL_ADDR_MASK |
580 GSWIP_PCE_TBL_CTRL_OPMOD_MASK,
581 tbl->table | addr_mode,
582 GSWIP_PCE_TBL_CTRL);
583
584 gswip_switch_w(priv, tbl->mask, GSWIP_PCE_TBL_MASK);
585
586 crtl = gswip_switch_r(priv, GSWIP_PCE_TBL_CTRL);
587 crtl &= ~(GSWIP_PCE_TBL_CTRL_TYPE | GSWIP_PCE_TBL_CTRL_VLD |
588 GSWIP_PCE_TBL_CTRL_GMAP_MASK);
589 if (tbl->type)
590 crtl |= GSWIP_PCE_TBL_CTRL_TYPE;
591 if (tbl->valid)
592 crtl |= GSWIP_PCE_TBL_CTRL_VLD;
593 crtl |= (tbl->gmap << 7) & GSWIP_PCE_TBL_CTRL_GMAP_MASK;
594 crtl |= GSWIP_PCE_TBL_CTRL_BAS;
595 gswip_switch_w(priv, crtl, GSWIP_PCE_TBL_CTRL);
596
597 return gswip_switch_r_timeout(priv, GSWIP_PCE_TBL_CTRL,
598 GSWIP_PCE_TBL_CTRL_BAS);
599}
600
601/* Add the LAN port into a bridge with the CPU port by
602 * default. This prevents automatic forwarding of
603 * packages between the LAN ports when no explicit
604 * bridge is configured.
605 */
606static int gswip_add_single_port_br(struct gswip_priv *priv, int port, bool add)
607{
608 struct gswip_pce_table_entry vlan_active = {0,};
609 struct gswip_pce_table_entry vlan_mapping = {0,};
610 unsigned int cpu_port = priv->hw_info->cpu_port;
611 unsigned int max_ports = priv->hw_info->max_ports;
612 int err;
613
614 if (port >= max_ports) {
615 dev_err(priv->dev, "single port for %i supported\n", port);
616 return -EIO;
617 }
618
619 vlan_active.index = port + 1;
620 vlan_active.table = GSWIP_TABLE_ACTIVE_VLAN;
621 vlan_active.key[0] = 0; /* vid */
622 vlan_active.val[0] = port + 1 /* fid */;
623 vlan_active.valid = add;
624 err = gswip_pce_table_entry_write(priv, &vlan_active);
625 if (err) {
626 dev_err(priv->dev, "failed to write active VLAN: %d\n", err);
627 return err;
628 }
629
630 if (!add)
631 return 0;
632
633 vlan_mapping.index = port + 1;
634 vlan_mapping.table = GSWIP_TABLE_VLAN_MAPPING;
635 vlan_mapping.val[0] = 0 /* vid */;
636 vlan_mapping.val[1] = BIT(port) | BIT(cpu_port);
637 vlan_mapping.val[2] = 0;
638 err = gswip_pce_table_entry_write(priv, &vlan_mapping);
639 if (err) {
640 dev_err(priv->dev, "failed to write VLAN mapping: %d\n", err);
641 return err;
642 }
643
644 return 0;
645}
646
Hauke Mehrtens14fceff2018-09-09 22:20:39 +0200647static int gswip_port_enable(struct dsa_switch *ds, int port,
648 struct phy_device *phydev)
649{
650 struct gswip_priv *priv = ds->priv;
Hauke Mehrtens8206e0c2019-05-06 00:25:07 +0200651 int err;
652
Vivien Didelot74be4ba2019-08-19 16:00:49 -0400653 if (!dsa_is_user_port(ds, port))
654 return 0;
655
Hauke Mehrtens8206e0c2019-05-06 00:25:07 +0200656 if (!dsa_is_cpu_port(ds, port)) {
657 err = gswip_add_single_port_br(priv, port, true);
658 if (err)
659 return err;
660 }
Hauke Mehrtens14fceff2018-09-09 22:20:39 +0200661
662 /* RMON Counter Enable for port */
663 gswip_switch_w(priv, GSWIP_BM_PCFG_CNTEN, GSWIP_BM_PCFGp(port));
664
665 /* enable port fetch/store dma & VLAN Modification */
666 gswip_switch_mask(priv, 0, GSWIP_FDMA_PCTRL_EN |
667 GSWIP_FDMA_PCTRL_VLANMOD_BOTH,
668 GSWIP_FDMA_PCTRLp(port));
669 gswip_switch_mask(priv, 0, GSWIP_SDMA_PCTRL_EN,
670 GSWIP_SDMA_PCTRLp(port));
Hauke Mehrtens14fceff2018-09-09 22:20:39 +0200671
672 if (!dsa_is_cpu_port(ds, port)) {
Martin Blumenstingl3e9005b2021-04-08 20:38:27 +0200673 u32 mdio_phy = 0;
Hauke Mehrtens14fceff2018-09-09 22:20:39 +0200674
Martin Blumenstingl3e9005b2021-04-08 20:38:27 +0200675 if (phydev)
676 mdio_phy = phydev->mdio.addr & GSWIP_MDIO_PHY_ADDR_MASK;
677
678 gswip_mdio_mask(priv, GSWIP_MDIO_PHY_ADDR_MASK, mdio_phy,
679 GSWIP_MDIO_PHYp(port));
Hauke Mehrtens14fceff2018-09-09 22:20:39 +0200680 }
681
682 return 0;
683}
684
Andrew Lunn75104db2019-02-24 20:44:43 +0100685static void gswip_port_disable(struct dsa_switch *ds, int port)
Hauke Mehrtens14fceff2018-09-09 22:20:39 +0200686{
687 struct gswip_priv *priv = ds->priv;
688
Vivien Didelot74be4ba2019-08-19 16:00:49 -0400689 if (!dsa_is_user_port(ds, port))
690 return;
691
Hauke Mehrtens14fceff2018-09-09 22:20:39 +0200692 gswip_switch_mask(priv, GSWIP_FDMA_PCTRL_EN, 0,
693 GSWIP_FDMA_PCTRLp(port));
694 gswip_switch_mask(priv, GSWIP_SDMA_PCTRL_EN, 0,
695 GSWIP_SDMA_PCTRLp(port));
696}
697
698static int gswip_pce_load_microcode(struct gswip_priv *priv)
699{
700 int i;
701 int err;
702
703 gswip_switch_mask(priv, GSWIP_PCE_TBL_CTRL_ADDR_MASK |
704 GSWIP_PCE_TBL_CTRL_OPMOD_MASK,
705 GSWIP_PCE_TBL_CTRL_OPMOD_ADWR, GSWIP_PCE_TBL_CTRL);
706 gswip_switch_w(priv, 0, GSWIP_PCE_TBL_MASK);
707
708 for (i = 0; i < ARRAY_SIZE(gswip_pce_microcode); i++) {
709 gswip_switch_w(priv, i, GSWIP_PCE_TBL_ADDR);
710 gswip_switch_w(priv, gswip_pce_microcode[i].val_0,
711 GSWIP_PCE_TBL_VAL(0));
712 gswip_switch_w(priv, gswip_pce_microcode[i].val_1,
713 GSWIP_PCE_TBL_VAL(1));
714 gswip_switch_w(priv, gswip_pce_microcode[i].val_2,
715 GSWIP_PCE_TBL_VAL(2));
716 gswip_switch_w(priv, gswip_pce_microcode[i].val_3,
717 GSWIP_PCE_TBL_VAL(3));
718
719 /* start the table access: */
720 gswip_switch_mask(priv, 0, GSWIP_PCE_TBL_CTRL_BAS,
721 GSWIP_PCE_TBL_CTRL);
722 err = gswip_switch_r_timeout(priv, GSWIP_PCE_TBL_CTRL,
723 GSWIP_PCE_TBL_CTRL_BAS);
724 if (err)
725 return err;
726 }
727
728 /* tell the switch that the microcode is loaded */
729 gswip_switch_mask(priv, 0, GSWIP_PCE_GCTRL_0_MC_VALID,
730 GSWIP_PCE_GCTRL_0);
731
732 return 0;
733}
734
Hauke Mehrtens8206e0c2019-05-06 00:25:07 +0200735static int gswip_port_vlan_filtering(struct dsa_switch *ds, int port,
Vladimir Oltean89153ed2021-02-13 22:43:19 +0200736 bool vlan_filtering,
737 struct netlink_ext_ack *extack)
Hauke Mehrtens8206e0c2019-05-06 00:25:07 +0200738{
Vladimir Olteanbae33f22021-01-09 02:01:50 +0200739 struct net_device *bridge = dsa_to_port(ds, port)->bridge_dev;
Hauke Mehrtens8206e0c2019-05-06 00:25:07 +0200740 struct gswip_priv *priv = ds->priv;
Hauke Mehrtens9bbb1c02019-05-06 00:25:08 +0200741
742 /* Do not allow changing the VLAN filtering options while in bridge */
Vladimir Oltean89153ed2021-02-13 22:43:19 +0200743 if (bridge && !!(priv->port_vlan_filter & BIT(port)) != vlan_filtering) {
744 NL_SET_ERR_MSG_MOD(extack,
745 "Dynamic toggling of vlan_filtering not supported");
Vladimir Olteanbae33f22021-01-09 02:01:50 +0200746 return -EIO;
Vladimir Oltean89153ed2021-02-13 22:43:19 +0200747 }
Hauke Mehrtens8206e0c2019-05-06 00:25:07 +0200748
749 if (vlan_filtering) {
750 /* Use port based VLAN tag */
751 gswip_switch_mask(priv,
752 GSWIP_PCE_VCTRL_VSR,
753 GSWIP_PCE_VCTRL_UVR | GSWIP_PCE_VCTRL_VIMR |
754 GSWIP_PCE_VCTRL_VEMR,
755 GSWIP_PCE_VCTRL(port));
756 gswip_switch_mask(priv, GSWIP_PCE_PCTRL_0_TVM, 0,
757 GSWIP_PCE_PCTRL_0p(port));
758 } else {
759 /* Use port based VLAN tag */
760 gswip_switch_mask(priv,
761 GSWIP_PCE_VCTRL_UVR | GSWIP_PCE_VCTRL_VIMR |
762 GSWIP_PCE_VCTRL_VEMR,
763 GSWIP_PCE_VCTRL_VSR,
764 GSWIP_PCE_VCTRL(port));
765 gswip_switch_mask(priv, 0, GSWIP_PCE_PCTRL_0_TVM,
766 GSWIP_PCE_PCTRL_0p(port));
767 }
768
769 return 0;
770}
771
Hauke Mehrtens14fceff2018-09-09 22:20:39 +0200772static int gswip_setup(struct dsa_switch *ds)
773{
774 struct gswip_priv *priv = ds->priv;
775 unsigned int cpu_port = priv->hw_info->cpu_port;
776 int i;
777 int err;
778
779 gswip_switch_w(priv, GSWIP_SWRES_R0, GSWIP_SWRES);
780 usleep_range(5000, 10000);
781 gswip_switch_w(priv, 0, GSWIP_SWRES);
782
783 /* disable port fetch/store dma on all ports */
Hauke Mehrtens8206e0c2019-05-06 00:25:07 +0200784 for (i = 0; i < priv->hw_info->max_ports; i++) {
Andrew Lunn75104db2019-02-24 20:44:43 +0100785 gswip_port_disable(ds, i);
Vladimir Oltean89153ed2021-02-13 22:43:19 +0200786 gswip_port_vlan_filtering(ds, i, false, NULL);
Hauke Mehrtens8206e0c2019-05-06 00:25:07 +0200787 }
Hauke Mehrtens14fceff2018-09-09 22:20:39 +0200788
789 /* enable Switch */
790 gswip_mdio_mask(priv, 0, GSWIP_MDIO_GLOB_ENABLE, GSWIP_MDIO_GLOB);
791
792 err = gswip_pce_load_microcode(priv);
793 if (err) {
794 dev_err(priv->dev, "writing PCE microcode failed, %i", err);
795 return err;
796 }
797
798 /* Default unknown Broadcast/Multicast/Unicast port maps */
799 gswip_switch_w(priv, BIT(cpu_port), GSWIP_PCE_PMAP1);
800 gswip_switch_w(priv, BIT(cpu_port), GSWIP_PCE_PMAP2);
801 gswip_switch_w(priv, BIT(cpu_port), GSWIP_PCE_PMAP3);
802
Martin Blumenstingl3e9005b2021-04-08 20:38:27 +0200803 /* Deactivate MDIO PHY auto polling. Some PHYs as the AR8030 have an
804 * interoperability problem with this auto polling mechanism because
805 * their status registers think that the link is in a different state
806 * than it actually is. For the AR8030 it has the BMSR_ESTATEN bit set
807 * as well as ESTATUS_1000_TFULL and ESTATUS_1000_XFULL. This makes the
808 * auto polling state machine consider the link being negotiated with
809 * 1Gbit/s. Since the PHY itself is a Fast Ethernet RMII PHY this leads
810 * to the switch port being completely dead (RX and TX are both not
811 * working).
812 * Also with various other PHY / port combinations (PHY11G GPHY, PHY22F
813 * GPHY, external RGMII PEF7071/7072) any traffic would stop. Sometimes
814 * it would work fine for a few minutes to hours and then stop, on
815 * other device it would no traffic could be sent or received at all.
816 * Testing shows that when PHY auto polling is disabled these problems
817 * go away.
818 */
Hauke Mehrtens14fceff2018-09-09 22:20:39 +0200819 gswip_mdio_w(priv, 0x0, GSWIP_MDIO_MDC_CFG0);
Martin Blumenstingl3e9005b2021-04-08 20:38:27 +0200820
Hauke Mehrtens14fceff2018-09-09 22:20:39 +0200821 /* Configure the MDIO Clock 2.5 MHz */
822 gswip_mdio_mask(priv, 0xff, 0x09, GSWIP_MDIO_MDC_CFG1);
823
Martin Blumenstingl3e9005b2021-04-08 20:38:27 +0200824 /* Disable the xMII link */
825 for (i = 0; i < priv->hw_info->max_ports; i++)
Martin Blumenstingl709a3c92021-01-03 02:25:44 +0100826 gswip_mii_mask_cfg(priv, GSWIP_MII_CFG_EN, 0, i);
Hauke Mehrtens14fceff2018-09-09 22:20:39 +0200827
828 /* enable special tag insertion on cpu port */
829 gswip_switch_mask(priv, 0, GSWIP_FDMA_PCTRL_STEN,
830 GSWIP_FDMA_PCTRLp(cpu_port));
831
Hauke Mehrtens30d893832019-05-06 00:25:06 +0200832 /* accept special tag in ingress direction */
833 gswip_switch_mask(priv, 0, GSWIP_PCE_PCTRL_0_INGRESS,
834 GSWIP_PCE_PCTRL_0p(cpu_port));
835
Hauke Mehrtens14fceff2018-09-09 22:20:39 +0200836 gswip_switch_mask(priv, 0, GSWIP_MAC_CTRL_2_MLEN,
837 GSWIP_MAC_CTRL_2p(cpu_port));
838 gswip_switch_w(priv, VLAN_ETH_FRAME_LEN + 8, GSWIP_MAC_FLEN);
839 gswip_switch_mask(priv, 0, GSWIP_BM_QUEUE_GCTRL_GL_MOD,
840 GSWIP_BM_QUEUE_GCTRL);
841
842 /* VLAN aware Switching */
843 gswip_switch_mask(priv, 0, GSWIP_PCE_GCTRL_0_VLAN, GSWIP_PCE_GCTRL_0);
844
Hauke Mehrtens8206e0c2019-05-06 00:25:07 +0200845 /* Flush MAC Table */
846 gswip_switch_mask(priv, 0, GSWIP_PCE_GCTRL_0_MTFL, GSWIP_PCE_GCTRL_0);
847
848 err = gswip_switch_r_timeout(priv, GSWIP_PCE_GCTRL_0,
849 GSWIP_PCE_GCTRL_0_MTFL);
850 if (err) {
851 dev_err(priv->dev, "MAC flushing didn't finish\n");
852 return err;
853 }
Hauke Mehrtens14fceff2018-09-09 22:20:39 +0200854
855 gswip_port_enable(ds, cpu_port, NULL);
Vladimir Oltean0ee2af42021-01-16 01:19:19 +0200856
857 ds->configure_vlan_while_not_filtering = false;
858
Hauke Mehrtens14fceff2018-09-09 22:20:39 +0200859 return 0;
860}
861
862static enum dsa_tag_protocol gswip_get_tag_protocol(struct dsa_switch *ds,
Florian Fainelli4d776482020-01-07 21:06:05 -0800863 int port,
864 enum dsa_tag_protocol mp)
Hauke Mehrtens14fceff2018-09-09 22:20:39 +0200865{
866 return DSA_TAG_PROTO_GSWIP;
867}
868
Hauke Mehrtens8206e0c2019-05-06 00:25:07 +0200869static int gswip_vlan_active_create(struct gswip_priv *priv,
870 struct net_device *bridge,
871 int fid, u16 vid)
872{
873 struct gswip_pce_table_entry vlan_active = {0,};
874 unsigned int max_ports = priv->hw_info->max_ports;
875 int idx = -1;
876 int err;
877 int i;
878
879 /* Look for a free slot */
880 for (i = max_ports; i < ARRAY_SIZE(priv->vlans); i++) {
881 if (!priv->vlans[i].bridge) {
882 idx = i;
883 break;
884 }
885 }
886
887 if (idx == -1)
888 return -ENOSPC;
889
890 if (fid == -1)
891 fid = idx;
892
893 vlan_active.index = idx;
894 vlan_active.table = GSWIP_TABLE_ACTIVE_VLAN;
895 vlan_active.key[0] = vid;
896 vlan_active.val[0] = fid;
897 vlan_active.valid = true;
898
899 err = gswip_pce_table_entry_write(priv, &vlan_active);
900 if (err) {
901 dev_err(priv->dev, "failed to write active VLAN: %d\n", err);
902 return err;
903 }
904
905 priv->vlans[idx].bridge = bridge;
906 priv->vlans[idx].vid = vid;
907 priv->vlans[idx].fid = fid;
908
909 return idx;
910}
911
912static int gswip_vlan_active_remove(struct gswip_priv *priv, int idx)
913{
914 struct gswip_pce_table_entry vlan_active = {0,};
915 int err;
916
917 vlan_active.index = idx;
918 vlan_active.table = GSWIP_TABLE_ACTIVE_VLAN;
919 vlan_active.valid = false;
920 err = gswip_pce_table_entry_write(priv, &vlan_active);
921 if (err)
922 dev_err(priv->dev, "failed to delete active VLAN: %d\n", err);
923 priv->vlans[idx].bridge = NULL;
924
925 return err;
926}
927
928static int gswip_vlan_add_unaware(struct gswip_priv *priv,
929 struct net_device *bridge, int port)
930{
931 struct gswip_pce_table_entry vlan_mapping = {0,};
932 unsigned int max_ports = priv->hw_info->max_ports;
933 unsigned int cpu_port = priv->hw_info->cpu_port;
934 bool active_vlan_created = false;
935 int idx = -1;
936 int i;
937 int err;
938
939 /* Check if there is already a page for this bridge */
940 for (i = max_ports; i < ARRAY_SIZE(priv->vlans); i++) {
941 if (priv->vlans[i].bridge == bridge) {
942 idx = i;
943 break;
944 }
945 }
946
947 /* If this bridge is not programmed yet, add a Active VLAN table
948 * entry in a free slot and prepare the VLAN mapping table entry.
949 */
950 if (idx == -1) {
951 idx = gswip_vlan_active_create(priv, bridge, -1, 0);
952 if (idx < 0)
953 return idx;
954 active_vlan_created = true;
955
956 vlan_mapping.index = idx;
957 vlan_mapping.table = GSWIP_TABLE_VLAN_MAPPING;
958 /* VLAN ID byte, maps to the VLAN ID of vlan active table */
959 vlan_mapping.val[0] = 0;
960 } else {
961 /* Read the existing VLAN mapping entry from the switch */
962 vlan_mapping.index = idx;
963 vlan_mapping.table = GSWIP_TABLE_VLAN_MAPPING;
964 err = gswip_pce_table_entry_read(priv, &vlan_mapping);
965 if (err) {
966 dev_err(priv->dev, "failed to read VLAN mapping: %d\n",
967 err);
968 return err;
969 }
970 }
971
972 /* Update the VLAN mapping entry and write it to the switch */
973 vlan_mapping.val[1] |= BIT(cpu_port);
974 vlan_mapping.val[1] |= BIT(port);
975 err = gswip_pce_table_entry_write(priv, &vlan_mapping);
976 if (err) {
977 dev_err(priv->dev, "failed to write VLAN mapping: %d\n", err);
978 /* In case an Active VLAN was creaetd delete it again */
979 if (active_vlan_created)
980 gswip_vlan_active_remove(priv, idx);
981 return err;
982 }
983
984 gswip_switch_w(priv, 0, GSWIP_PCE_DEFPVID(port));
985 return 0;
986}
987
Hauke Mehrtens9bbb1c02019-05-06 00:25:08 +0200988static int gswip_vlan_add_aware(struct gswip_priv *priv,
989 struct net_device *bridge, int port,
990 u16 vid, bool untagged,
991 bool pvid)
992{
993 struct gswip_pce_table_entry vlan_mapping = {0,};
994 unsigned int max_ports = priv->hw_info->max_ports;
995 unsigned int cpu_port = priv->hw_info->cpu_port;
996 bool active_vlan_created = false;
997 int idx = -1;
998 int fid = -1;
999 int i;
1000 int err;
1001
1002 /* Check if there is already a page for this bridge */
1003 for (i = max_ports; i < ARRAY_SIZE(priv->vlans); i++) {
1004 if (priv->vlans[i].bridge == bridge) {
1005 if (fid != -1 && fid != priv->vlans[i].fid)
1006 dev_err(priv->dev, "one bridge with multiple flow ids\n");
1007 fid = priv->vlans[i].fid;
1008 if (priv->vlans[i].vid == vid) {
1009 idx = i;
1010 break;
1011 }
1012 }
1013 }
1014
1015 /* If this bridge is not programmed yet, add a Active VLAN table
1016 * entry in a free slot and prepare the VLAN mapping table entry.
1017 */
1018 if (idx == -1) {
1019 idx = gswip_vlan_active_create(priv, bridge, fid, vid);
1020 if (idx < 0)
1021 return idx;
1022 active_vlan_created = true;
1023
1024 vlan_mapping.index = idx;
1025 vlan_mapping.table = GSWIP_TABLE_VLAN_MAPPING;
1026 /* VLAN ID byte, maps to the VLAN ID of vlan active table */
1027 vlan_mapping.val[0] = vid;
1028 } else {
1029 /* Read the existing VLAN mapping entry from the switch */
1030 vlan_mapping.index = idx;
1031 vlan_mapping.table = GSWIP_TABLE_VLAN_MAPPING;
1032 err = gswip_pce_table_entry_read(priv, &vlan_mapping);
1033 if (err) {
1034 dev_err(priv->dev, "failed to read VLAN mapping: %d\n",
1035 err);
1036 return err;
1037 }
1038 }
1039
1040 vlan_mapping.val[0] = vid;
1041 /* Update the VLAN mapping entry and write it to the switch */
1042 vlan_mapping.val[1] |= BIT(cpu_port);
1043 vlan_mapping.val[2] |= BIT(cpu_port);
1044 vlan_mapping.val[1] |= BIT(port);
1045 if (untagged)
1046 vlan_mapping.val[2] &= ~BIT(port);
1047 else
1048 vlan_mapping.val[2] |= BIT(port);
1049 err = gswip_pce_table_entry_write(priv, &vlan_mapping);
1050 if (err) {
1051 dev_err(priv->dev, "failed to write VLAN mapping: %d\n", err);
1052 /* In case an Active VLAN was creaetd delete it again */
1053 if (active_vlan_created)
1054 gswip_vlan_active_remove(priv, idx);
1055 return err;
1056 }
1057
1058 if (pvid)
1059 gswip_switch_w(priv, idx, GSWIP_PCE_DEFPVID(port));
1060
1061 return 0;
1062}
1063
Hauke Mehrtens8206e0c2019-05-06 00:25:07 +02001064static int gswip_vlan_remove(struct gswip_priv *priv,
1065 struct net_device *bridge, int port,
1066 u16 vid, bool pvid, bool vlan_aware)
1067{
1068 struct gswip_pce_table_entry vlan_mapping = {0,};
1069 unsigned int max_ports = priv->hw_info->max_ports;
1070 unsigned int cpu_port = priv->hw_info->cpu_port;
1071 int idx = -1;
1072 int i;
1073 int err;
1074
1075 /* Check if there is already a page for this bridge */
1076 for (i = max_ports; i < ARRAY_SIZE(priv->vlans); i++) {
1077 if (priv->vlans[i].bridge == bridge &&
1078 (!vlan_aware || priv->vlans[i].vid == vid)) {
1079 idx = i;
1080 break;
1081 }
1082 }
1083
1084 if (idx == -1) {
1085 dev_err(priv->dev, "bridge to leave does not exists\n");
1086 return -ENOENT;
1087 }
1088
1089 vlan_mapping.index = idx;
1090 vlan_mapping.table = GSWIP_TABLE_VLAN_MAPPING;
1091 err = gswip_pce_table_entry_read(priv, &vlan_mapping);
1092 if (err) {
1093 dev_err(priv->dev, "failed to read VLAN mapping: %d\n", err);
1094 return err;
1095 }
1096
1097 vlan_mapping.val[1] &= ~BIT(port);
1098 vlan_mapping.val[2] &= ~BIT(port);
1099 err = gswip_pce_table_entry_write(priv, &vlan_mapping);
1100 if (err) {
1101 dev_err(priv->dev, "failed to write VLAN mapping: %d\n", err);
1102 return err;
1103 }
1104
1105 /* In case all ports are removed from the bridge, remove the VLAN */
1106 if ((vlan_mapping.val[1] & ~BIT(cpu_port)) == 0) {
1107 err = gswip_vlan_active_remove(priv, idx);
1108 if (err) {
1109 dev_err(priv->dev, "failed to write active VLAN: %d\n",
1110 err);
1111 return err;
1112 }
1113 }
1114
1115 /* GSWIP 2.2 (GRX300) and later program here the VID directly. */
1116 if (pvid)
1117 gswip_switch_w(priv, 0, GSWIP_PCE_DEFPVID(port));
1118
1119 return 0;
1120}
1121
1122static int gswip_port_bridge_join(struct dsa_switch *ds, int port,
1123 struct net_device *bridge)
1124{
1125 struct gswip_priv *priv = ds->priv;
1126 int err;
1127
Hauke Mehrtens9bbb1c02019-05-06 00:25:08 +02001128 /* When the bridge uses VLAN filtering we have to configure VLAN
1129 * specific bridges. No bridge is configured here.
1130 */
1131 if (!br_vlan_enabled(bridge)) {
1132 err = gswip_vlan_add_unaware(priv, bridge, port);
1133 if (err)
1134 return err;
1135 priv->port_vlan_filter &= ~BIT(port);
1136 } else {
1137 priv->port_vlan_filter |= BIT(port);
1138 }
Hauke Mehrtens8206e0c2019-05-06 00:25:07 +02001139 return gswip_add_single_port_br(priv, port, false);
1140}
1141
1142static void gswip_port_bridge_leave(struct dsa_switch *ds, int port,
1143 struct net_device *bridge)
1144{
1145 struct gswip_priv *priv = ds->priv;
1146
1147 gswip_add_single_port_br(priv, port, true);
1148
Hauke Mehrtens9bbb1c02019-05-06 00:25:08 +02001149 /* When the bridge uses VLAN filtering we have to configure VLAN
1150 * specific bridges. No bridge is configured here.
1151 */
1152 if (!br_vlan_enabled(bridge))
1153 gswip_vlan_remove(priv, bridge, port, 0, true, false);
1154}
1155
1156static int gswip_port_vlan_prepare(struct dsa_switch *ds, int port,
Vladimir Oltean31046a52021-02-13 22:43:18 +02001157 const struct switchdev_obj_port_vlan *vlan,
1158 struct netlink_ext_ack *extack)
Hauke Mehrtens9bbb1c02019-05-06 00:25:08 +02001159{
1160 struct gswip_priv *priv = ds->priv;
1161 struct net_device *bridge = dsa_to_port(ds, port)->bridge_dev;
1162 unsigned int max_ports = priv->hw_info->max_ports;
Hauke Mehrtens9bbb1c02019-05-06 00:25:08 +02001163 int pos = max_ports;
Vladimir Olteanb7a9e0d2021-01-09 02:01:46 +02001164 int i, idx = -1;
Hauke Mehrtens9bbb1c02019-05-06 00:25:08 +02001165
1166 /* We only support VLAN filtering on bridges */
1167 if (!dsa_is_cpu_port(ds, port) && !bridge)
1168 return -EOPNOTSUPP;
1169
Vladimir Olteanb7a9e0d2021-01-09 02:01:46 +02001170 /* Check if there is already a page for this VLAN */
1171 for (i = max_ports; i < ARRAY_SIZE(priv->vlans); i++) {
1172 if (priv->vlans[i].bridge == bridge &&
1173 priv->vlans[i].vid == vlan->vid) {
1174 idx = i;
1175 break;
1176 }
1177 }
Hauke Mehrtens9bbb1c02019-05-06 00:25:08 +02001178
Vladimir Olteanb7a9e0d2021-01-09 02:01:46 +02001179 /* If this VLAN is not programmed yet, we have to reserve
1180 * one entry in the VLAN table. Make sure we start at the
1181 * next position round.
1182 */
1183 if (idx == -1) {
1184 /* Look for a free slot */
1185 for (; pos < ARRAY_SIZE(priv->vlans); pos++) {
1186 if (!priv->vlans[pos].bridge) {
1187 idx = pos;
1188 pos++;
Hauke Mehrtens9bbb1c02019-05-06 00:25:08 +02001189 break;
1190 }
1191 }
1192
Vladimir Oltean31046a52021-02-13 22:43:18 +02001193 if (idx == -1) {
1194 NL_SET_ERR_MSG_MOD(extack, "No slot in VLAN table");
Vladimir Olteanb7a9e0d2021-01-09 02:01:46 +02001195 return -ENOSPC;
Vladimir Oltean31046a52021-02-13 22:43:18 +02001196 }
Hauke Mehrtens9bbb1c02019-05-06 00:25:08 +02001197 }
1198
1199 return 0;
1200}
1201
Vladimir Oltean1958d582021-01-09 02:01:53 +02001202static int gswip_port_vlan_add(struct dsa_switch *ds, int port,
Vladimir Oltean31046a52021-02-13 22:43:18 +02001203 const struct switchdev_obj_port_vlan *vlan,
1204 struct netlink_ext_ack *extack)
Hauke Mehrtens9bbb1c02019-05-06 00:25:08 +02001205{
1206 struct gswip_priv *priv = ds->priv;
1207 struct net_device *bridge = dsa_to_port(ds, port)->bridge_dev;
1208 bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
1209 bool pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
Vladimir Oltean1958d582021-01-09 02:01:53 +02001210 int err;
1211
Vladimir Oltean31046a52021-02-13 22:43:18 +02001212 err = gswip_port_vlan_prepare(ds, port, vlan, extack);
Vladimir Oltean1958d582021-01-09 02:01:53 +02001213 if (err)
1214 return err;
Hauke Mehrtens9bbb1c02019-05-06 00:25:08 +02001215
1216 /* We have to receive all packets on the CPU port and should not
1217 * do any VLAN filtering here. This is also called with bridge
1218 * NULL and then we do not know for which bridge to configure
1219 * this.
1220 */
1221 if (dsa_is_cpu_port(ds, port))
Vladimir Oltean1958d582021-01-09 02:01:53 +02001222 return 0;
Hauke Mehrtens9bbb1c02019-05-06 00:25:08 +02001223
Vladimir Oltean1958d582021-01-09 02:01:53 +02001224 return gswip_vlan_add_aware(priv, bridge, port, vlan->vid,
1225 untagged, pvid);
Hauke Mehrtens9bbb1c02019-05-06 00:25:08 +02001226}
1227
1228static int gswip_port_vlan_del(struct dsa_switch *ds, int port,
1229 const struct switchdev_obj_port_vlan *vlan)
1230{
1231 struct gswip_priv *priv = ds->priv;
1232 struct net_device *bridge = dsa_to_port(ds, port)->bridge_dev;
1233 bool pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
Hauke Mehrtens9bbb1c02019-05-06 00:25:08 +02001234
1235 /* We have to receive all packets on the CPU port and should not
1236 * do any VLAN filtering here. This is also called with bridge
1237 * NULL and then we do not know for which bridge to configure
1238 * this.
1239 */
1240 if (dsa_is_cpu_port(ds, port))
1241 return 0;
1242
Vladimir Olteanb7a9e0d2021-01-09 02:01:46 +02001243 return gswip_vlan_remove(priv, bridge, port, vlan->vid, pvid, true);
Hauke Mehrtens8206e0c2019-05-06 00:25:07 +02001244}
1245
Hauke Mehrtens45813482019-05-06 00:25:09 +02001246static void gswip_port_fast_age(struct dsa_switch *ds, int port)
1247{
1248 struct gswip_priv *priv = ds->priv;
1249 struct gswip_pce_table_entry mac_bridge = {0,};
1250 int i;
1251 int err;
1252
1253 for (i = 0; i < 2048; i++) {
1254 mac_bridge.table = GSWIP_TABLE_MAC_BRIDGE;
1255 mac_bridge.index = i;
1256
1257 err = gswip_pce_table_entry_read(priv, &mac_bridge);
1258 if (err) {
Colin Ian Kingd6759172019-05-08 11:22:09 +01001259 dev_err(priv->dev, "failed to read mac bridge: %d\n",
Hauke Mehrtens45813482019-05-06 00:25:09 +02001260 err);
1261 return;
1262 }
1263
1264 if (!mac_bridge.valid)
1265 continue;
1266
1267 if (mac_bridge.val[1] & GSWIP_TABLE_MAC_BRIDGE_STATIC)
1268 continue;
1269
1270 if (((mac_bridge.val[0] & GENMASK(7, 4)) >> 4) != port)
1271 continue;
1272
1273 mac_bridge.valid = false;
1274 err = gswip_pce_table_entry_write(priv, &mac_bridge);
1275 if (err) {
Colin Ian Kingd6759172019-05-08 11:22:09 +01001276 dev_err(priv->dev, "failed to write mac bridge: %d\n",
Hauke Mehrtens45813482019-05-06 00:25:09 +02001277 err);
1278 return;
1279 }
1280 }
1281}
1282
Hauke Mehrtens8206e0c2019-05-06 00:25:07 +02001283static void gswip_port_stp_state_set(struct dsa_switch *ds, int port, u8 state)
1284{
1285 struct gswip_priv *priv = ds->priv;
1286 u32 stp_state;
1287
1288 switch (state) {
1289 case BR_STATE_DISABLED:
1290 gswip_switch_mask(priv, GSWIP_SDMA_PCTRL_EN, 0,
1291 GSWIP_SDMA_PCTRLp(port));
1292 return;
1293 case BR_STATE_BLOCKING:
1294 case BR_STATE_LISTENING:
1295 stp_state = GSWIP_PCE_PCTRL_0_PSTATE_LISTEN;
1296 break;
1297 case BR_STATE_LEARNING:
1298 stp_state = GSWIP_PCE_PCTRL_0_PSTATE_LEARNING;
1299 break;
1300 case BR_STATE_FORWARDING:
1301 stp_state = GSWIP_PCE_PCTRL_0_PSTATE_FORWARDING;
1302 break;
1303 default:
1304 dev_err(priv->dev, "invalid STP state: %d\n", state);
1305 return;
1306 }
1307
1308 gswip_switch_mask(priv, 0, GSWIP_SDMA_PCTRL_EN,
1309 GSWIP_SDMA_PCTRLp(port));
1310 gswip_switch_mask(priv, GSWIP_PCE_PCTRL_0_PSTATE_MASK, stp_state,
1311 GSWIP_PCE_PCTRL_0p(port));
1312}
1313
Hauke Mehrtens58c59ef2019-05-06 00:25:10 +02001314static int gswip_port_fdb(struct dsa_switch *ds, int port,
1315 const unsigned char *addr, u16 vid, bool add)
1316{
1317 struct gswip_priv *priv = ds->priv;
1318 struct net_device *bridge = dsa_to_port(ds, port)->bridge_dev;
1319 struct gswip_pce_table_entry mac_bridge = {0,};
1320 unsigned int cpu_port = priv->hw_info->cpu_port;
1321 int fid = -1;
1322 int i;
1323 int err;
1324
1325 if (!bridge)
1326 return -EINVAL;
1327
1328 for (i = cpu_port; i < ARRAY_SIZE(priv->vlans); i++) {
1329 if (priv->vlans[i].bridge == bridge) {
1330 fid = priv->vlans[i].fid;
1331 break;
1332 }
1333 }
1334
1335 if (fid == -1) {
1336 dev_err(priv->dev, "Port not part of a bridge\n");
1337 return -EINVAL;
1338 }
1339
1340 mac_bridge.table = GSWIP_TABLE_MAC_BRIDGE;
1341 mac_bridge.key_mode = true;
1342 mac_bridge.key[0] = addr[5] | (addr[4] << 8);
1343 mac_bridge.key[1] = addr[3] | (addr[2] << 8);
1344 mac_bridge.key[2] = addr[1] | (addr[0] << 8);
1345 mac_bridge.key[3] = fid;
1346 mac_bridge.val[0] = add ? BIT(port) : 0; /* port map */
1347 mac_bridge.val[1] = GSWIP_TABLE_MAC_BRIDGE_STATIC;
1348 mac_bridge.valid = add;
1349
1350 err = gswip_pce_table_entry_write(priv, &mac_bridge);
1351 if (err)
Colin Ian Kingd6759172019-05-08 11:22:09 +01001352 dev_err(priv->dev, "failed to write mac bridge: %d\n", err);
Hauke Mehrtens58c59ef2019-05-06 00:25:10 +02001353
1354 return err;
1355}
1356
1357static int gswip_port_fdb_add(struct dsa_switch *ds, int port,
1358 const unsigned char *addr, u16 vid)
1359{
1360 return gswip_port_fdb(ds, port, addr, vid, true);
1361}
1362
1363static int gswip_port_fdb_del(struct dsa_switch *ds, int port,
1364 const unsigned char *addr, u16 vid)
1365{
1366 return gswip_port_fdb(ds, port, addr, vid, false);
1367}
1368
1369static int gswip_port_fdb_dump(struct dsa_switch *ds, int port,
1370 dsa_fdb_dump_cb_t *cb, void *data)
1371{
1372 struct gswip_priv *priv = ds->priv;
1373 struct gswip_pce_table_entry mac_bridge = {0,};
1374 unsigned char addr[6];
1375 int i;
1376 int err;
1377
1378 for (i = 0; i < 2048; i++) {
1379 mac_bridge.table = GSWIP_TABLE_MAC_BRIDGE;
1380 mac_bridge.index = i;
1381
1382 err = gswip_pce_table_entry_read(priv, &mac_bridge);
1383 if (err) {
Colin Ian Kingd6759172019-05-08 11:22:09 +01001384 dev_err(priv->dev, "failed to write mac bridge: %d\n",
Hauke Mehrtens58c59ef2019-05-06 00:25:10 +02001385 err);
1386 return err;
1387 }
1388
1389 if (!mac_bridge.valid)
1390 continue;
1391
1392 addr[5] = mac_bridge.key[0] & 0xff;
1393 addr[4] = (mac_bridge.key[0] >> 8) & 0xff;
1394 addr[3] = mac_bridge.key[1] & 0xff;
1395 addr[2] = (mac_bridge.key[1] >> 8) & 0xff;
1396 addr[1] = mac_bridge.key[2] & 0xff;
1397 addr[0] = (mac_bridge.key[2] >> 8) & 0xff;
1398 if (mac_bridge.val[1] & GSWIP_TABLE_MAC_BRIDGE_STATIC) {
1399 if (mac_bridge.val[0] & BIT(port))
1400 cb(addr, 0, true, data);
1401 } else {
1402 if (((mac_bridge.val[0] & GENMASK(7, 4)) >> 4) == port)
1403 cb(addr, 0, false, data);
1404 }
1405 }
1406 return 0;
1407}
1408
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02001409static void gswip_phylink_validate(struct dsa_switch *ds, int port,
1410 unsigned long *supported,
1411 struct phylink_link_state *state)
1412{
1413 __ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, };
1414
1415 switch (port) {
1416 case 0:
1417 case 1:
1418 if (!phy_interface_mode_is_rgmii(state->interface) &&
1419 state->interface != PHY_INTERFACE_MODE_MII &&
1420 state->interface != PHY_INTERFACE_MODE_REVMII &&
Hauke Mehrtens0e630b52018-09-15 14:08:48 +02001421 state->interface != PHY_INTERFACE_MODE_RMII)
1422 goto unsupported;
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02001423 break;
1424 case 2:
1425 case 3:
1426 case 4:
Hauke Mehrtens0e630b52018-09-15 14:08:48 +02001427 if (state->interface != PHY_INTERFACE_MODE_INTERNAL)
1428 goto unsupported;
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02001429 break;
1430 case 5:
1431 if (!phy_interface_mode_is_rgmii(state->interface) &&
Hauke Mehrtens0e630b52018-09-15 14:08:48 +02001432 state->interface != PHY_INTERFACE_MODE_INTERNAL)
1433 goto unsupported;
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02001434 break;
Hauke Mehrtens0e630b52018-09-15 14:08:48 +02001435 default:
1436 bitmap_zero(supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
1437 dev_err(ds->dev, "Unsupported port: %i\n", port);
1438 return;
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02001439 }
1440
1441 /* Allow all the expected bits */
1442 phylink_set(mask, Autoneg);
1443 phylink_set_port_modes(mask);
1444 phylink_set(mask, Pause);
1445 phylink_set(mask, Asym_Pause);
1446
Aleksander Jan Bajkowski35454542021-01-07 20:58:18 +01001447 /* With the exclusion of MII, Reverse MII and Reduced MII, we
1448 * support Gigabit, including Half duplex
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02001449 */
1450 if (state->interface != PHY_INTERFACE_MODE_MII &&
Aleksander Jan Bajkowski35454542021-01-07 20:58:18 +01001451 state->interface != PHY_INTERFACE_MODE_REVMII &&
1452 state->interface != PHY_INTERFACE_MODE_RMII) {
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02001453 phylink_set(mask, 1000baseT_Full);
1454 phylink_set(mask, 1000baseT_Half);
1455 }
1456
1457 phylink_set(mask, 10baseT_Half);
1458 phylink_set(mask, 10baseT_Full);
1459 phylink_set(mask, 100baseT_Half);
1460 phylink_set(mask, 100baseT_Full);
1461
1462 bitmap_and(supported, supported, mask,
1463 __ETHTOOL_LINK_MODE_MASK_NBITS);
1464 bitmap_and(state->advertising, state->advertising, mask,
1465 __ETHTOOL_LINK_MODE_MASK_NBITS);
Hauke Mehrtens0e630b52018-09-15 14:08:48 +02001466 return;
1467
1468unsupported:
1469 bitmap_zero(supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
Martin Blumenstingl4d3da2d2020-06-07 15:02:58 +02001470 dev_err(ds->dev, "Unsupported interface '%s' for port %d\n",
1471 phy_modes(state->interface), port);
Hauke Mehrtens0e630b52018-09-15 14:08:48 +02001472 return;
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02001473}
1474
Martin Blumenstingl3e9005b2021-04-08 20:38:27 +02001475static void gswip_port_set_link(struct gswip_priv *priv, int port, bool link)
1476{
1477 u32 mdio_phy;
1478
1479 if (link)
1480 mdio_phy = GSWIP_MDIO_PHY_LINK_UP;
1481 else
1482 mdio_phy = GSWIP_MDIO_PHY_LINK_DOWN;
1483
1484 gswip_mdio_mask(priv, GSWIP_MDIO_PHY_LINK_MASK, mdio_phy,
1485 GSWIP_MDIO_PHYp(port));
1486}
1487
1488static void gswip_port_set_speed(struct gswip_priv *priv, int port, int speed,
1489 phy_interface_t interface)
1490{
1491 u32 mdio_phy = 0, mii_cfg = 0, mac_ctrl_0 = 0;
1492
1493 switch (speed) {
1494 case SPEED_10:
1495 mdio_phy = GSWIP_MDIO_PHY_SPEED_M10;
1496
1497 if (interface == PHY_INTERFACE_MODE_RMII)
1498 mii_cfg = GSWIP_MII_CFG_RATE_M50;
1499 else
1500 mii_cfg = GSWIP_MII_CFG_RATE_M2P5;
1501
1502 mac_ctrl_0 = GSWIP_MAC_CTRL_0_GMII_MII;
1503 break;
1504
1505 case SPEED_100:
1506 mdio_phy = GSWIP_MDIO_PHY_SPEED_M100;
1507
1508 if (interface == PHY_INTERFACE_MODE_RMII)
1509 mii_cfg = GSWIP_MII_CFG_RATE_M50;
1510 else
1511 mii_cfg = GSWIP_MII_CFG_RATE_M25;
1512
1513 mac_ctrl_0 = GSWIP_MAC_CTRL_0_GMII_MII;
1514 break;
1515
1516 case SPEED_1000:
1517 mdio_phy = GSWIP_MDIO_PHY_SPEED_G1;
1518
1519 mii_cfg = GSWIP_MII_CFG_RATE_M125;
1520
1521 mac_ctrl_0 = GSWIP_MAC_CTRL_0_GMII_RGMII;
1522 break;
1523 }
1524
1525 gswip_mdio_mask(priv, GSWIP_MDIO_PHY_SPEED_MASK, mdio_phy,
1526 GSWIP_MDIO_PHYp(port));
1527 gswip_mii_mask_cfg(priv, GSWIP_MII_CFG_RATE_MASK, mii_cfg, port);
1528 gswip_switch_mask(priv, GSWIP_MAC_CTRL_0_GMII_MASK, mac_ctrl_0,
1529 GSWIP_MAC_CTRL_0p(port));
1530}
1531
1532static void gswip_port_set_duplex(struct gswip_priv *priv, int port, int duplex)
1533{
1534 u32 mac_ctrl_0, mdio_phy;
1535
1536 if (duplex == DUPLEX_FULL) {
1537 mac_ctrl_0 = GSWIP_MAC_CTRL_0_FDUP_EN;
1538 mdio_phy = GSWIP_MDIO_PHY_FDUP_EN;
1539 } else {
1540 mac_ctrl_0 = GSWIP_MAC_CTRL_0_FDUP_DIS;
1541 mdio_phy = GSWIP_MDIO_PHY_FDUP_DIS;
1542 }
1543
1544 gswip_switch_mask(priv, GSWIP_MAC_CTRL_0_FDUP_MASK, mac_ctrl_0,
1545 GSWIP_MAC_CTRL_0p(port));
1546 gswip_mdio_mask(priv, GSWIP_MDIO_PHY_FDUP_MASK, mdio_phy,
1547 GSWIP_MDIO_PHYp(port));
1548}
1549
1550static void gswip_port_set_pause(struct gswip_priv *priv, int port,
1551 bool tx_pause, bool rx_pause)
1552{
1553 u32 mac_ctrl_0, mdio_phy;
1554
1555 if (tx_pause && rx_pause) {
1556 mac_ctrl_0 = GSWIP_MAC_CTRL_0_FCON_RXTX;
1557 mdio_phy = GSWIP_MDIO_PHY_FCONTX_EN |
1558 GSWIP_MDIO_PHY_FCONRX_EN;
1559 } else if (tx_pause) {
1560 mac_ctrl_0 = GSWIP_MAC_CTRL_0_FCON_TX;
1561 mdio_phy = GSWIP_MDIO_PHY_FCONTX_EN |
1562 GSWIP_MDIO_PHY_FCONRX_DIS;
1563 } else if (rx_pause) {
1564 mac_ctrl_0 = GSWIP_MAC_CTRL_0_FCON_RX;
1565 mdio_phy = GSWIP_MDIO_PHY_FCONTX_DIS |
1566 GSWIP_MDIO_PHY_FCONRX_EN;
1567 } else {
1568 mac_ctrl_0 = GSWIP_MAC_CTRL_0_FCON_NONE;
1569 mdio_phy = GSWIP_MDIO_PHY_FCONTX_DIS |
1570 GSWIP_MDIO_PHY_FCONRX_DIS;
1571 }
1572
1573 gswip_switch_mask(priv, GSWIP_MAC_CTRL_0_FCON_MASK,
1574 mac_ctrl_0, GSWIP_MAC_CTRL_0p(port));
1575 gswip_mdio_mask(priv,
1576 GSWIP_MDIO_PHY_FCONTX_MASK |
1577 GSWIP_MDIO_PHY_FCONRX_MASK,
1578 mdio_phy, GSWIP_MDIO_PHYp(port));
1579}
1580
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02001581static void gswip_phylink_mac_config(struct dsa_switch *ds, int port,
1582 unsigned int mode,
1583 const struct phylink_link_state *state)
1584{
1585 struct gswip_priv *priv = ds->priv;
1586 u32 miicfg = 0;
1587
1588 miicfg |= GSWIP_MII_CFG_LDCLKDIS;
1589
1590 switch (state->interface) {
1591 case PHY_INTERFACE_MODE_MII:
1592 case PHY_INTERFACE_MODE_INTERNAL:
1593 miicfg |= GSWIP_MII_CFG_MODE_MIIM;
1594 break;
1595 case PHY_INTERFACE_MODE_REVMII:
1596 miicfg |= GSWIP_MII_CFG_MODE_MIIP;
1597 break;
1598 case PHY_INTERFACE_MODE_RMII:
1599 miicfg |= GSWIP_MII_CFG_MODE_RMIIM;
1600 break;
1601 case PHY_INTERFACE_MODE_RGMII:
1602 case PHY_INTERFACE_MODE_RGMII_ID:
1603 case PHY_INTERFACE_MODE_RGMII_RXID:
1604 case PHY_INTERFACE_MODE_RGMII_TXID:
1605 miicfg |= GSWIP_MII_CFG_MODE_RGMII;
1606 break;
1607 default:
1608 dev_err(ds->dev,
1609 "Unsupported interface: %d\n", state->interface);
1610 return;
1611 }
1612 gswip_mii_mask_cfg(priv, GSWIP_MII_CFG_MODE_MASK, miicfg, port);
1613
1614 switch (state->interface) {
1615 case PHY_INTERFACE_MODE_RGMII_ID:
1616 gswip_mii_mask_pcdu(priv, GSWIP_MII_PCDU_TXDLY_MASK |
1617 GSWIP_MII_PCDU_RXDLY_MASK, 0, port);
1618 break;
1619 case PHY_INTERFACE_MODE_RGMII_RXID:
1620 gswip_mii_mask_pcdu(priv, GSWIP_MII_PCDU_RXDLY_MASK, 0, port);
1621 break;
1622 case PHY_INTERFACE_MODE_RGMII_TXID:
1623 gswip_mii_mask_pcdu(priv, GSWIP_MII_PCDU_TXDLY_MASK, 0, port);
1624 break;
1625 default:
1626 break;
1627 }
1628}
1629
1630static void gswip_phylink_mac_link_down(struct dsa_switch *ds, int port,
1631 unsigned int mode,
1632 phy_interface_t interface)
1633{
1634 struct gswip_priv *priv = ds->priv;
1635
1636 gswip_mii_mask_cfg(priv, GSWIP_MII_CFG_EN, 0, port);
Martin Blumenstingl3e9005b2021-04-08 20:38:27 +02001637
1638 if (!dsa_is_cpu_port(ds, port))
1639 gswip_port_set_link(priv, port, false);
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02001640}
1641
1642static void gswip_phylink_mac_link_up(struct dsa_switch *ds, int port,
1643 unsigned int mode,
1644 phy_interface_t interface,
Russell King5b502a72020-02-26 10:23:46 +00001645 struct phy_device *phydev,
1646 int speed, int duplex,
1647 bool tx_pause, bool rx_pause)
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02001648{
1649 struct gswip_priv *priv = ds->priv;
1650
Martin Blumenstingl3e9005b2021-04-08 20:38:27 +02001651 if (!dsa_is_cpu_port(ds, port)) {
1652 gswip_port_set_link(priv, port, true);
1653 gswip_port_set_speed(priv, port, speed, interface);
1654 gswip_port_set_duplex(priv, port, duplex);
1655 gswip_port_set_pause(priv, port, tx_pause, rx_pause);
1656 }
1657
Martin Blumenstinglc1a9ec72021-01-03 02:25:43 +01001658 gswip_mii_mask_cfg(priv, 0, GSWIP_MII_CFG_EN, port);
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02001659}
1660
1661static void gswip_get_strings(struct dsa_switch *ds, int port, u32 stringset,
1662 uint8_t *data)
1663{
1664 int i;
1665
1666 if (stringset != ETH_SS_STATS)
1667 return;
1668
1669 for (i = 0; i < ARRAY_SIZE(gswip_rmon_cnt); i++)
1670 strncpy(data + i * ETH_GSTRING_LEN, gswip_rmon_cnt[i].name,
1671 ETH_GSTRING_LEN);
1672}
1673
1674static u32 gswip_bcm_ram_entry_read(struct gswip_priv *priv, u32 table,
1675 u32 index)
1676{
1677 u32 result;
1678 int err;
1679
1680 gswip_switch_w(priv, index, GSWIP_BM_RAM_ADDR);
1681 gswip_switch_mask(priv, GSWIP_BM_RAM_CTRL_ADDR_MASK |
1682 GSWIP_BM_RAM_CTRL_OPMOD,
1683 table | GSWIP_BM_RAM_CTRL_BAS,
1684 GSWIP_BM_RAM_CTRL);
1685
1686 err = gswip_switch_r_timeout(priv, GSWIP_BM_RAM_CTRL,
1687 GSWIP_BM_RAM_CTRL_BAS);
1688 if (err) {
1689 dev_err(priv->dev, "timeout while reading table: %u, index: %u",
1690 table, index);
1691 return 0;
1692 }
1693
1694 result = gswip_switch_r(priv, GSWIP_BM_RAM_VAL(0));
1695 result |= gswip_switch_r(priv, GSWIP_BM_RAM_VAL(1)) << 16;
1696
1697 return result;
1698}
1699
1700static void gswip_get_ethtool_stats(struct dsa_switch *ds, int port,
1701 uint64_t *data)
1702{
1703 struct gswip_priv *priv = ds->priv;
1704 const struct gswip_rmon_cnt_desc *rmon_cnt;
1705 int i;
1706 u64 high;
1707
1708 for (i = 0; i < ARRAY_SIZE(gswip_rmon_cnt); i++) {
1709 rmon_cnt = &gswip_rmon_cnt[i];
1710
1711 data[i] = gswip_bcm_ram_entry_read(priv, port,
1712 rmon_cnt->offset);
1713 if (rmon_cnt->size == 2) {
1714 high = gswip_bcm_ram_entry_read(priv, port,
1715 rmon_cnt->offset + 1);
1716 data[i] |= high << 32;
1717 }
1718 }
1719}
1720
1721static int gswip_get_sset_count(struct dsa_switch *ds, int port, int sset)
1722{
1723 if (sset != ETH_SS_STATS)
1724 return 0;
1725
1726 return ARRAY_SIZE(gswip_rmon_cnt);
1727}
1728
1729static const struct dsa_switch_ops gswip_switch_ops = {
1730 .get_tag_protocol = gswip_get_tag_protocol,
1731 .setup = gswip_setup,
1732 .port_enable = gswip_port_enable,
1733 .port_disable = gswip_port_disable,
Hauke Mehrtens8206e0c2019-05-06 00:25:07 +02001734 .port_bridge_join = gswip_port_bridge_join,
1735 .port_bridge_leave = gswip_port_bridge_leave,
Hauke Mehrtens45813482019-05-06 00:25:09 +02001736 .port_fast_age = gswip_port_fast_age,
Hauke Mehrtens9bbb1c02019-05-06 00:25:08 +02001737 .port_vlan_filtering = gswip_port_vlan_filtering,
Hauke Mehrtens9bbb1c02019-05-06 00:25:08 +02001738 .port_vlan_add = gswip_port_vlan_add,
1739 .port_vlan_del = gswip_port_vlan_del,
Hauke Mehrtens8206e0c2019-05-06 00:25:07 +02001740 .port_stp_state_set = gswip_port_stp_state_set,
Hauke Mehrtens58c59ef2019-05-06 00:25:10 +02001741 .port_fdb_add = gswip_port_fdb_add,
1742 .port_fdb_del = gswip_port_fdb_del,
1743 .port_fdb_dump = gswip_port_fdb_dump,
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02001744 .phylink_validate = gswip_phylink_validate,
1745 .phylink_mac_config = gswip_phylink_mac_config,
1746 .phylink_mac_link_down = gswip_phylink_mac_link_down,
1747 .phylink_mac_link_up = gswip_phylink_mac_link_up,
1748 .get_strings = gswip_get_strings,
1749 .get_ethtool_stats = gswip_get_ethtool_stats,
1750 .get_sset_count = gswip_get_sset_count,
1751};
1752
1753static const struct xway_gphy_match_data xrx200a1x_gphy_data = {
1754 .fe_firmware_name = "lantiq/xrx200_phy22f_a14.bin",
1755 .ge_firmware_name = "lantiq/xrx200_phy11g_a14.bin",
1756};
1757
1758static const struct xway_gphy_match_data xrx200a2x_gphy_data = {
1759 .fe_firmware_name = "lantiq/xrx200_phy22f_a22.bin",
1760 .ge_firmware_name = "lantiq/xrx200_phy11g_a22.bin",
1761};
1762
1763static const struct xway_gphy_match_data xrx300_gphy_data = {
1764 .fe_firmware_name = "lantiq/xrx300_phy22f_a21.bin",
1765 .ge_firmware_name = "lantiq/xrx300_phy11g_a21.bin",
1766};
1767
1768static const struct of_device_id xway_gphy_match[] = {
1769 { .compatible = "lantiq,xrx200-gphy-fw", .data = NULL },
1770 { .compatible = "lantiq,xrx200a1x-gphy-fw", .data = &xrx200a1x_gphy_data },
1771 { .compatible = "lantiq,xrx200a2x-gphy-fw", .data = &xrx200a2x_gphy_data },
1772 { .compatible = "lantiq,xrx300-gphy-fw", .data = &xrx300_gphy_data },
1773 { .compatible = "lantiq,xrx330-gphy-fw", .data = &xrx300_gphy_data },
1774 {},
1775};
1776
1777static int gswip_gphy_fw_load(struct gswip_priv *priv, struct gswip_gphy_fw *gphy_fw)
1778{
1779 struct device *dev = priv->dev;
1780 const struct firmware *fw;
1781 void *fw_addr;
1782 dma_addr_t dma_addr;
1783 dma_addr_t dev_addr;
1784 size_t size;
1785 int ret;
1786
1787 ret = clk_prepare_enable(gphy_fw->clk_gate);
1788 if (ret)
1789 return ret;
1790
1791 reset_control_assert(gphy_fw->reset);
1792
1793 ret = request_firmware(&fw, gphy_fw->fw_name, dev);
1794 if (ret) {
1795 dev_err(dev, "failed to load firmware: %s, error: %i\n",
1796 gphy_fw->fw_name, ret);
1797 return ret;
1798 }
1799
1800 /* GPHY cores need the firmware code in a persistent and contiguous
1801 * memory area with a 16 kB boundary aligned start address.
1802 */
1803 size = fw->size + XRX200_GPHY_FW_ALIGN;
1804
1805 fw_addr = dmam_alloc_coherent(dev, size, &dma_addr, GFP_KERNEL);
1806 if (fw_addr) {
1807 fw_addr = PTR_ALIGN(fw_addr, XRX200_GPHY_FW_ALIGN);
1808 dev_addr = ALIGN(dma_addr, XRX200_GPHY_FW_ALIGN);
1809 memcpy(fw_addr, fw->data, fw->size);
1810 } else {
1811 dev_err(dev, "failed to alloc firmware memory\n");
1812 release_firmware(fw);
1813 return -ENOMEM;
1814 }
1815
1816 release_firmware(fw);
1817
1818 ret = regmap_write(priv->rcu_regmap, gphy_fw->fw_addr_offset, dev_addr);
1819 if (ret)
1820 return ret;
1821
1822 reset_control_deassert(gphy_fw->reset);
1823
1824 return ret;
1825}
1826
1827static int gswip_gphy_fw_probe(struct gswip_priv *priv,
1828 struct gswip_gphy_fw *gphy_fw,
1829 struct device_node *gphy_fw_np, int i)
1830{
1831 struct device *dev = priv->dev;
1832 u32 gphy_mode;
1833 int ret;
1834 char gphyname[10];
1835
1836 snprintf(gphyname, sizeof(gphyname), "gphy%d", i);
1837
1838 gphy_fw->clk_gate = devm_clk_get(dev, gphyname);
1839 if (IS_ERR(gphy_fw->clk_gate)) {
1840 dev_err(dev, "Failed to lookup gate clock\n");
1841 return PTR_ERR(gphy_fw->clk_gate);
1842 }
1843
1844 ret = of_property_read_u32(gphy_fw_np, "reg", &gphy_fw->fw_addr_offset);
1845 if (ret)
1846 return ret;
1847
1848 ret = of_property_read_u32(gphy_fw_np, "lantiq,gphy-mode", &gphy_mode);
1849 /* Default to GE mode */
1850 if (ret)
1851 gphy_mode = GPHY_MODE_GE;
1852
1853 switch (gphy_mode) {
1854 case GPHY_MODE_FE:
1855 gphy_fw->fw_name = priv->gphy_fw_name_cfg->fe_firmware_name;
1856 break;
1857 case GPHY_MODE_GE:
1858 gphy_fw->fw_name = priv->gphy_fw_name_cfg->ge_firmware_name;
1859 break;
1860 default:
1861 dev_err(dev, "Unknown GPHY mode %d\n", gphy_mode);
1862 return -EINVAL;
1863 }
1864
1865 gphy_fw->reset = of_reset_control_array_get_exclusive(gphy_fw_np);
Wei Yongjunf592e0b2018-09-15 01:33:38 +00001866 if (IS_ERR(gphy_fw->reset)) {
1867 if (PTR_ERR(gphy_fw->reset) != -EPROBE_DEFER)
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02001868 dev_err(dev, "Failed to lookup gphy reset\n");
Wei Yongjunf592e0b2018-09-15 01:33:38 +00001869 return PTR_ERR(gphy_fw->reset);
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02001870 }
1871
1872 return gswip_gphy_fw_load(priv, gphy_fw);
1873}
1874
1875static void gswip_gphy_fw_remove(struct gswip_priv *priv,
1876 struct gswip_gphy_fw *gphy_fw)
1877{
1878 int ret;
1879
1880 /* check if the device was fully probed */
1881 if (!gphy_fw->fw_name)
1882 return;
1883
1884 ret = regmap_write(priv->rcu_regmap, gphy_fw->fw_addr_offset, 0);
1885 if (ret)
1886 dev_err(priv->dev, "can not reset GPHY FW pointer");
1887
1888 clk_disable_unprepare(gphy_fw->clk_gate);
1889
1890 reset_control_put(gphy_fw->reset);
1891}
1892
1893static int gswip_gphy_fw_list(struct gswip_priv *priv,
1894 struct device_node *gphy_fw_list_np, u32 version)
1895{
1896 struct device *dev = priv->dev;
1897 struct device_node *gphy_fw_np;
1898 const struct of_device_id *match;
1899 int err;
1900 int i = 0;
1901
Hauke Mehrtens0e630b52018-09-15 14:08:48 +02001902 /* The VRX200 rev 1.1 uses the GSWIP 2.0 and needs the older
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02001903 * GPHY firmware. The VRX200 rev 1.2 uses the GSWIP 2.1 and also
1904 * needs a different GPHY firmware.
1905 */
1906 if (of_device_is_compatible(gphy_fw_list_np, "lantiq,xrx200-gphy-fw")) {
1907 switch (version) {
1908 case GSWIP_VERSION_2_0:
1909 priv->gphy_fw_name_cfg = &xrx200a1x_gphy_data;
1910 break;
1911 case GSWIP_VERSION_2_1:
1912 priv->gphy_fw_name_cfg = &xrx200a2x_gphy_data;
1913 break;
1914 default:
1915 dev_err(dev, "unknown GSWIP version: 0x%x", version);
1916 return -ENOENT;
1917 }
1918 }
1919
1920 match = of_match_node(xway_gphy_match, gphy_fw_list_np);
1921 if (match && match->data)
1922 priv->gphy_fw_name_cfg = match->data;
1923
1924 if (!priv->gphy_fw_name_cfg) {
1925 dev_err(dev, "GPHY compatible type not supported");
1926 return -ENOENT;
1927 }
1928
1929 priv->num_gphy_fw = of_get_available_child_count(gphy_fw_list_np);
1930 if (!priv->num_gphy_fw)
1931 return -ENOENT;
1932
1933 priv->rcu_regmap = syscon_regmap_lookup_by_phandle(gphy_fw_list_np,
1934 "lantiq,rcu");
1935 if (IS_ERR(priv->rcu_regmap))
1936 return PTR_ERR(priv->rcu_regmap);
1937
1938 priv->gphy_fw = devm_kmalloc_array(dev, priv->num_gphy_fw,
1939 sizeof(*priv->gphy_fw),
1940 GFP_KERNEL | __GFP_ZERO);
1941 if (!priv->gphy_fw)
1942 return -ENOMEM;
1943
1944 for_each_available_child_of_node(gphy_fw_list_np, gphy_fw_np) {
1945 err = gswip_gphy_fw_probe(priv, &priv->gphy_fw[i],
1946 gphy_fw_np, i);
1947 if (err)
1948 goto remove_gphy;
1949 i++;
1950 }
1951
Martin Blumenstingl2a1828e2020-11-15 17:57:57 +01001952 /* The standalone PHY11G requires 300ms to be fully
1953 * initialized and ready for any MDIO communication after being
1954 * taken out of reset. For the SoC-internal GPHY variant there
1955 * is no (known) documentation for the minimum time after a
1956 * reset. Use the same value as for the standalone variant as
1957 * some users have reported internal PHYs not being detected
1958 * without any delay.
1959 */
1960 msleep(300);
1961
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02001962 return 0;
1963
1964remove_gphy:
1965 for (i = 0; i < priv->num_gphy_fw; i++)
1966 gswip_gphy_fw_remove(priv, &priv->gphy_fw[i]);
1967 return err;
1968}
1969
1970static int gswip_probe(struct platform_device *pdev)
1971{
1972 struct gswip_priv *priv;
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02001973 struct device_node *mdio_np, *gphy_fw_np;
1974 struct device *dev = &pdev->dev;
1975 int err;
1976 int i;
1977 u32 version;
1978
1979 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
1980 if (!priv)
1981 return -ENOMEM;
1982
YueHaibing6551c8c2019-08-01 20:25:46 +08001983 priv->gswip = devm_platform_ioremap_resource(pdev, 0);
Wei Yongjunf5de8bf2018-09-15 01:33:21 +00001984 if (IS_ERR(priv->gswip))
1985 return PTR_ERR(priv->gswip);
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02001986
YueHaibing6551c8c2019-08-01 20:25:46 +08001987 priv->mdio = devm_platform_ioremap_resource(pdev, 1);
Wei Yongjunf5de8bf2018-09-15 01:33:21 +00001988 if (IS_ERR(priv->mdio))
1989 return PTR_ERR(priv->mdio);
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02001990
YueHaibing6551c8c2019-08-01 20:25:46 +08001991 priv->mii = devm_platform_ioremap_resource(pdev, 2);
Wei Yongjunf5de8bf2018-09-15 01:33:21 +00001992 if (IS_ERR(priv->mii))
1993 return PTR_ERR(priv->mii);
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02001994
1995 priv->hw_info = of_device_get_match_data(dev);
1996 if (!priv->hw_info)
1997 return -EINVAL;
1998
Vivien Didelot7e99e342019-10-21 16:51:30 -04001999 priv->ds = devm_kzalloc(dev, sizeof(*priv->ds), GFP_KERNEL);
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02002000 if (!priv->ds)
2001 return -ENOMEM;
2002
Vivien Didelot7e99e342019-10-21 16:51:30 -04002003 priv->ds->dev = dev;
2004 priv->ds->num_ports = priv->hw_info->max_ports;
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02002005 priv->ds->priv = priv;
2006 priv->ds->ops = &gswip_switch_ops;
2007 priv->dev = dev;
2008 version = gswip_switch_r(priv, GSWIP_VERSION);
2009
2010 /* bring up the mdio bus */
Johan Hovoldc8cbcb02019-01-16 11:23:34 +01002011 gphy_fw_np = of_get_compatible_child(dev->of_node, "lantiq,gphy-fw");
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02002012 if (gphy_fw_np) {
2013 err = gswip_gphy_fw_list(priv, gphy_fw_np, version);
Johan Hovoldc8cbcb02019-01-16 11:23:34 +01002014 of_node_put(gphy_fw_np);
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02002015 if (err) {
2016 dev_err(dev, "gphy fw probe failed\n");
2017 return err;
2018 }
2019 }
2020
2021 /* bring up the mdio bus */
Johan Hovoldc8cbcb02019-01-16 11:23:34 +01002022 mdio_np = of_get_compatible_child(dev->of_node, "lantiq,xrx200-mdio");
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02002023 if (mdio_np) {
2024 err = gswip_mdio(priv, mdio_np);
2025 if (err) {
2026 dev_err(dev, "mdio probe failed\n");
Johan Hovoldc8cbcb02019-01-16 11:23:34 +01002027 goto put_mdio_node;
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02002028 }
2029 }
2030
2031 err = dsa_register_switch(priv->ds);
2032 if (err) {
2033 dev_err(dev, "dsa switch register failed: %i\n", err);
2034 goto mdio_bus;
2035 }
Hauke Mehrtens0e630b52018-09-15 14:08:48 +02002036 if (!dsa_is_cpu_port(priv->ds, priv->hw_info->cpu_port)) {
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02002037 dev_err(dev, "wrong CPU port defined, HW only supports port: %i",
2038 priv->hw_info->cpu_port);
2039 err = -EINVAL;
Johan Hovoldaed13f22019-01-16 11:23:33 +01002040 goto disable_switch;
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02002041 }
2042
2043 platform_set_drvdata(pdev, priv);
2044
2045 dev_info(dev, "probed GSWIP version %lx mod %lx\n",
2046 (version & GSWIP_VERSION_REV_MASK) >> GSWIP_VERSION_REV_SHIFT,
2047 (version & GSWIP_VERSION_MOD_MASK) >> GSWIP_VERSION_MOD_SHIFT);
2048 return 0;
2049
Johan Hovoldaed13f22019-01-16 11:23:33 +01002050disable_switch:
2051 gswip_mdio_mask(priv, GSWIP_MDIO_GLOB_ENABLE, 0, GSWIP_MDIO_GLOB);
2052 dsa_unregister_switch(priv->ds);
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02002053mdio_bus:
2054 if (mdio_np)
2055 mdiobus_unregister(priv->ds->slave_mii_bus);
Johan Hovoldc8cbcb02019-01-16 11:23:34 +01002056put_mdio_node:
2057 of_node_put(mdio_np);
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02002058 for (i = 0; i < priv->num_gphy_fw; i++)
2059 gswip_gphy_fw_remove(priv, &priv->gphy_fw[i]);
2060 return err;
2061}
2062
2063static int gswip_remove(struct platform_device *pdev)
2064{
2065 struct gswip_priv *priv = platform_get_drvdata(pdev);
2066 int i;
2067
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02002068 /* disable the switch */
2069 gswip_mdio_mask(priv, GSWIP_MDIO_GLOB_ENABLE, 0, GSWIP_MDIO_GLOB);
2070
2071 dsa_unregister_switch(priv->ds);
2072
Johan Hovoldc8cbcb02019-01-16 11:23:34 +01002073 if (priv->ds->slave_mii_bus) {
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02002074 mdiobus_unregister(priv->ds->slave_mii_bus);
Johan Hovoldc8cbcb02019-01-16 11:23:34 +01002075 of_node_put(priv->ds->slave_mii_bus->dev.of_node);
2076 }
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02002077
2078 for (i = 0; i < priv->num_gphy_fw; i++)
2079 gswip_gphy_fw_remove(priv, &priv->gphy_fw[i]);
2080
2081 return 0;
2082}
2083
2084static const struct gswip_hw_info gswip_xrx200 = {
2085 .max_ports = 7,
2086 .cpu_port = 6,
2087};
2088
2089static const struct of_device_id gswip_of_match[] = {
2090 { .compatible = "lantiq,xrx200-gswip", .data = &gswip_xrx200 },
2091 {},
2092};
2093MODULE_DEVICE_TABLE(of, gswip_of_match);
2094
2095static struct platform_driver gswip_driver = {
2096 .probe = gswip_probe,
2097 .remove = gswip_remove,
2098 .driver = {
2099 .name = "gswip",
2100 .of_match_table = gswip_of_match,
2101 },
2102};
2103
2104module_platform_driver(gswip_driver);
2105
Hauke Mehrtenscffde202019-02-22 20:11:13 +01002106MODULE_FIRMWARE("lantiq/xrx300_phy11g_a21.bin");
2107MODULE_FIRMWARE("lantiq/xrx300_phy22f_a21.bin");
2108MODULE_FIRMWARE("lantiq/xrx200_phy11g_a14.bin");
2109MODULE_FIRMWARE("lantiq/xrx200_phy11g_a22.bin");
2110MODULE_FIRMWARE("lantiq/xrx200_phy22f_a14.bin");
2111MODULE_FIRMWARE("lantiq/xrx200_phy22f_a22.bin");
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02002112MODULE_AUTHOR("Hauke Mehrtens <hauke@hauke-m.de>");
2113MODULE_DESCRIPTION("Lantiq / Intel GSWIP driver");
2114MODULE_LICENSE("GPL v2");