blob: c62d923377e347eaac83d87a2d8accc4e3133c2a [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>
29#include <linux/etherdevice.h>
30#include <linux/firmware.h>
31#include <linux/if_bridge.h>
32#include <linux/if_vlan.h>
33#include <linux/iopoll.h>
34#include <linux/mfd/syscon.h>
35#include <linux/module.h>
36#include <linux/of_mdio.h>
37#include <linux/of_net.h>
38#include <linux/of_platform.h>
39#include <linux/phy.h>
40#include <linux/phylink.h>
41#include <linux/platform_device.h>
42#include <linux/regmap.h>
43#include <linux/reset.h>
44#include <net/dsa.h>
45#include <dt-bindings/mips/lantiq_rcu_gphy.h>
46
47#include "lantiq_pce.h"
48
49/* GSWIP MDIO Registers */
50#define GSWIP_MDIO_GLOB 0x00
51#define GSWIP_MDIO_GLOB_ENABLE BIT(15)
52#define GSWIP_MDIO_CTRL 0x08
53#define GSWIP_MDIO_CTRL_BUSY BIT(12)
54#define GSWIP_MDIO_CTRL_RD BIT(11)
55#define GSWIP_MDIO_CTRL_WR BIT(10)
56#define GSWIP_MDIO_CTRL_PHYAD_MASK 0x1f
57#define GSWIP_MDIO_CTRL_PHYAD_SHIFT 5
58#define GSWIP_MDIO_CTRL_REGAD_MASK 0x1f
59#define GSWIP_MDIO_READ 0x09
60#define GSWIP_MDIO_WRITE 0x0A
61#define GSWIP_MDIO_MDC_CFG0 0x0B
62#define GSWIP_MDIO_MDC_CFG1 0x0C
63#define GSWIP_MDIO_PHYp(p) (0x15 - (p))
64#define GSWIP_MDIO_PHY_LINK_MASK 0x6000
65#define GSWIP_MDIO_PHY_LINK_AUTO 0x0000
66#define GSWIP_MDIO_PHY_LINK_DOWN 0x4000
67#define GSWIP_MDIO_PHY_LINK_UP 0x2000
68#define GSWIP_MDIO_PHY_SPEED_MASK 0x1800
69#define GSWIP_MDIO_PHY_SPEED_AUTO 0x1800
70#define GSWIP_MDIO_PHY_SPEED_M10 0x0000
71#define GSWIP_MDIO_PHY_SPEED_M100 0x0800
72#define GSWIP_MDIO_PHY_SPEED_G1 0x1000
73#define GSWIP_MDIO_PHY_FDUP_MASK 0x0600
74#define GSWIP_MDIO_PHY_FDUP_AUTO 0x0000
75#define GSWIP_MDIO_PHY_FDUP_EN 0x0200
76#define GSWIP_MDIO_PHY_FDUP_DIS 0x0600
77#define GSWIP_MDIO_PHY_FCONTX_MASK 0x0180
78#define GSWIP_MDIO_PHY_FCONTX_AUTO 0x0000
79#define GSWIP_MDIO_PHY_FCONTX_EN 0x0100
80#define GSWIP_MDIO_PHY_FCONTX_DIS 0x0180
81#define GSWIP_MDIO_PHY_FCONRX_MASK 0x0060
82#define GSWIP_MDIO_PHY_FCONRX_AUTO 0x0000
83#define GSWIP_MDIO_PHY_FCONRX_EN 0x0020
84#define GSWIP_MDIO_PHY_FCONRX_DIS 0x0060
85#define GSWIP_MDIO_PHY_ADDR_MASK 0x001f
86#define GSWIP_MDIO_PHY_MASK (GSWIP_MDIO_PHY_ADDR_MASK | \
87 GSWIP_MDIO_PHY_FCONRX_MASK | \
88 GSWIP_MDIO_PHY_FCONTX_MASK | \
89 GSWIP_MDIO_PHY_LINK_MASK | \
90 GSWIP_MDIO_PHY_SPEED_MASK | \
91 GSWIP_MDIO_PHY_FDUP_MASK)
92
93/* GSWIP MII Registers */
94#define GSWIP_MII_CFG0 0x00
95#define GSWIP_MII_CFG1 0x02
96#define GSWIP_MII_CFG5 0x04
97#define GSWIP_MII_CFG_EN BIT(14)
98#define GSWIP_MII_CFG_LDCLKDIS BIT(12)
99#define GSWIP_MII_CFG_MODE_MIIP 0x0
100#define GSWIP_MII_CFG_MODE_MIIM 0x1
101#define GSWIP_MII_CFG_MODE_RMIIP 0x2
102#define GSWIP_MII_CFG_MODE_RMIIM 0x3
103#define GSWIP_MII_CFG_MODE_RGMII 0x4
104#define GSWIP_MII_CFG_MODE_MASK 0xf
105#define GSWIP_MII_CFG_RATE_M2P5 0x00
106#define GSWIP_MII_CFG_RATE_M25 0x10
107#define GSWIP_MII_CFG_RATE_M125 0x20
108#define GSWIP_MII_CFG_RATE_M50 0x30
109#define GSWIP_MII_CFG_RATE_AUTO 0x40
110#define GSWIP_MII_CFG_RATE_MASK 0x70
111#define GSWIP_MII_PCDU0 0x01
112#define GSWIP_MII_PCDU1 0x03
113#define GSWIP_MII_PCDU5 0x05
114#define GSWIP_MII_PCDU_TXDLY_MASK GENMASK(2, 0)
115#define GSWIP_MII_PCDU_RXDLY_MASK GENMASK(9, 7)
116
117/* GSWIP Core Registers */
118#define GSWIP_SWRES 0x000
119#define GSWIP_SWRES_R1 BIT(1) /* GSWIP Software reset */
120#define GSWIP_SWRES_R0 BIT(0) /* GSWIP Hardware reset */
121#define GSWIP_VERSION 0x013
122#define GSWIP_VERSION_REV_SHIFT 0
123#define GSWIP_VERSION_REV_MASK GENMASK(7, 0)
124#define GSWIP_VERSION_MOD_SHIFT 8
125#define GSWIP_VERSION_MOD_MASK GENMASK(15, 8)
126#define GSWIP_VERSION_2_0 0x100
127#define GSWIP_VERSION_2_1 0x021
128#define GSWIP_VERSION_2_2 0x122
129#define GSWIP_VERSION_2_2_ETC 0x022
130
131#define GSWIP_BM_RAM_VAL(x) (0x043 - (x))
132#define GSWIP_BM_RAM_ADDR 0x044
133#define GSWIP_BM_RAM_CTRL 0x045
134#define GSWIP_BM_RAM_CTRL_BAS BIT(15)
135#define GSWIP_BM_RAM_CTRL_OPMOD BIT(5)
136#define GSWIP_BM_RAM_CTRL_ADDR_MASK GENMASK(4, 0)
137#define GSWIP_BM_QUEUE_GCTRL 0x04A
138#define GSWIP_BM_QUEUE_GCTRL_GL_MOD BIT(10)
139/* buffer management Port Configuration Register */
140#define GSWIP_BM_PCFGp(p) (0x080 + ((p) * 2))
141#define GSWIP_BM_PCFG_CNTEN BIT(0) /* RMON Counter Enable */
142#define GSWIP_BM_PCFG_IGCNT BIT(1) /* Ingres Special Tag RMON count */
143/* buffer management Port Control Register */
144#define GSWIP_BM_RMON_CTRLp(p) (0x81 + ((p) * 2))
145#define GSWIP_BM_CTRL_RMON_RAM1_RES BIT(0) /* Software Reset for RMON RAM 1 */
146#define GSWIP_BM_CTRL_RMON_RAM2_RES BIT(1) /* Software Reset for RMON RAM 2 */
147
148/* PCE */
149#define GSWIP_PCE_TBL_KEY(x) (0x447 - (x))
150#define GSWIP_PCE_TBL_MASK 0x448
151#define GSWIP_PCE_TBL_VAL(x) (0x44D - (x))
152#define GSWIP_PCE_TBL_ADDR 0x44E
153#define GSWIP_PCE_TBL_CTRL 0x44F
154#define GSWIP_PCE_TBL_CTRL_BAS BIT(15)
155#define GSWIP_PCE_TBL_CTRL_TYPE BIT(13)
156#define GSWIP_PCE_TBL_CTRL_VLD BIT(12)
157#define GSWIP_PCE_TBL_CTRL_KEYFORM BIT(11)
158#define GSWIP_PCE_TBL_CTRL_GMAP_MASK GENMASK(10, 7)
159#define GSWIP_PCE_TBL_CTRL_OPMOD_MASK GENMASK(6, 5)
160#define GSWIP_PCE_TBL_CTRL_OPMOD_ADRD 0x00
161#define GSWIP_PCE_TBL_CTRL_OPMOD_ADWR 0x20
162#define GSWIP_PCE_TBL_CTRL_OPMOD_KSRD 0x40
163#define GSWIP_PCE_TBL_CTRL_OPMOD_KSWR 0x60
164#define GSWIP_PCE_TBL_CTRL_ADDR_MASK GENMASK(4, 0)
165#define GSWIP_PCE_PMAP1 0x453 /* Monitoring port map */
166#define GSWIP_PCE_PMAP2 0x454 /* Default Multicast port map */
167#define GSWIP_PCE_PMAP3 0x455 /* Default Unknown Unicast port map */
168#define GSWIP_PCE_GCTRL_0 0x456
Hauke Mehrtens8206e0c2019-05-06 00:25:07 +0200169#define GSWIP_PCE_GCTRL_0_MTFL BIT(0) /* MAC Table Flushing */
Hauke Mehrtens14fceff2018-09-09 22:20:39 +0200170#define GSWIP_PCE_GCTRL_0_MC_VALID BIT(3)
171#define GSWIP_PCE_GCTRL_0_VLAN BIT(14) /* VLAN aware Switching */
172#define GSWIP_PCE_GCTRL_1 0x457
173#define GSWIP_PCE_GCTRL_1_MAC_GLOCK BIT(2) /* MAC Address table lock */
174#define GSWIP_PCE_GCTRL_1_MAC_GLOCK_MOD BIT(3) /* Mac address table lock forwarding mode */
175#define GSWIP_PCE_PCTRL_0p(p) (0x480 + ((p) * 0xA))
Hauke Mehrtens8206e0c2019-05-06 00:25:07 +0200176#define GSWIP_PCE_PCTRL_0_TVM BIT(5) /* Transparent VLAN mode */
177#define GSWIP_PCE_PCTRL_0_VREP BIT(6) /* VLAN Replace Mode */
178#define GSWIP_PCE_PCTRL_0_INGRESS BIT(11) /* Accept special tag in ingress */
Hauke Mehrtens14fceff2018-09-09 22:20:39 +0200179#define GSWIP_PCE_PCTRL_0_PSTATE_LISTEN 0x0
180#define GSWIP_PCE_PCTRL_0_PSTATE_RX 0x1
181#define GSWIP_PCE_PCTRL_0_PSTATE_TX 0x2
182#define GSWIP_PCE_PCTRL_0_PSTATE_LEARNING 0x3
183#define GSWIP_PCE_PCTRL_0_PSTATE_FORWARDING 0x7
184#define GSWIP_PCE_PCTRL_0_PSTATE_MASK GENMASK(2, 0)
Hauke Mehrtens8206e0c2019-05-06 00:25:07 +0200185#define GSWIP_PCE_VCTRL(p) (0x485 + ((p) * 0xA))
186#define GSWIP_PCE_VCTRL_UVR BIT(0) /* Unknown VLAN Rule */
187#define GSWIP_PCE_VCTRL_VIMR BIT(3) /* VLAN Ingress Member violation rule */
188#define GSWIP_PCE_VCTRL_VEMR BIT(4) /* VLAN Egress Member violation rule */
189#define GSWIP_PCE_VCTRL_VSR BIT(5) /* VLAN Security */
190#define GSWIP_PCE_VCTRL_VID0 BIT(6) /* Priority Tagged Rule */
191#define GSWIP_PCE_DEFPVID(p) (0x486 + ((p) * 0xA))
Hauke Mehrtens14fceff2018-09-09 22:20:39 +0200192
193#define GSWIP_MAC_FLEN 0x8C5
194#define GSWIP_MAC_CTRL_2p(p) (0x905 + ((p) * 0xC))
195#define GSWIP_MAC_CTRL_2_MLEN BIT(3) /* Maximum Untagged Frame Lnegth */
196
197/* Ethernet Switch Fetch DMA Port Control Register */
198#define GSWIP_FDMA_PCTRLp(p) (0xA80 + ((p) * 0x6))
199#define GSWIP_FDMA_PCTRL_EN BIT(0) /* FDMA Port Enable */
200#define GSWIP_FDMA_PCTRL_STEN BIT(1) /* Special Tag Insertion Enable */
201#define GSWIP_FDMA_PCTRL_VLANMOD_MASK GENMASK(4, 3) /* VLAN Modification Control */
202#define GSWIP_FDMA_PCTRL_VLANMOD_SHIFT 3 /* VLAN Modification Control */
203#define GSWIP_FDMA_PCTRL_VLANMOD_DIS (0x0 << GSWIP_FDMA_PCTRL_VLANMOD_SHIFT)
204#define GSWIP_FDMA_PCTRL_VLANMOD_PRIO (0x1 << GSWIP_FDMA_PCTRL_VLANMOD_SHIFT)
205#define GSWIP_FDMA_PCTRL_VLANMOD_ID (0x2 << GSWIP_FDMA_PCTRL_VLANMOD_SHIFT)
206#define GSWIP_FDMA_PCTRL_VLANMOD_BOTH (0x3 << GSWIP_FDMA_PCTRL_VLANMOD_SHIFT)
207
208/* Ethernet Switch Store DMA Port Control Register */
209#define GSWIP_SDMA_PCTRLp(p) (0xBC0 + ((p) * 0x6))
210#define GSWIP_SDMA_PCTRL_EN BIT(0) /* SDMA Port Enable */
211#define GSWIP_SDMA_PCTRL_FCEN BIT(1) /* Flow Control Enable */
212#define GSWIP_SDMA_PCTRL_PAUFWD BIT(1) /* Pause Frame Forwarding */
213
Hauke Mehrtens8206e0c2019-05-06 00:25:07 +0200214#define GSWIP_TABLE_ACTIVE_VLAN 0x01
215#define GSWIP_TABLE_VLAN_MAPPING 0x02
216
Hauke Mehrtens14fceff2018-09-09 22:20:39 +0200217#define XRX200_GPHY_FW_ALIGN (16 * 1024)
218
219struct gswip_hw_info {
220 int max_ports;
221 int cpu_port;
222};
223
224struct xway_gphy_match_data {
225 char *fe_firmware_name;
226 char *ge_firmware_name;
227};
228
229struct gswip_gphy_fw {
230 struct clk *clk_gate;
231 struct reset_control *reset;
232 u32 fw_addr_offset;
233 char *fw_name;
234};
235
Hauke Mehrtens8206e0c2019-05-06 00:25:07 +0200236struct gswip_vlan {
237 struct net_device *bridge;
238 u16 vid;
239 u8 fid;
240};
241
Hauke Mehrtens14fceff2018-09-09 22:20:39 +0200242struct gswip_priv {
243 __iomem void *gswip;
244 __iomem void *mdio;
245 __iomem void *mii;
246 const struct gswip_hw_info *hw_info;
247 const struct xway_gphy_match_data *gphy_fw_name_cfg;
248 struct dsa_switch *ds;
249 struct device *dev;
250 struct regmap *rcu_regmap;
Hauke Mehrtens8206e0c2019-05-06 00:25:07 +0200251 struct gswip_vlan vlans[64];
Hauke Mehrtens14fceff2018-09-09 22:20:39 +0200252 int num_gphy_fw;
253 struct gswip_gphy_fw *gphy_fw;
Hauke Mehrtens9bbb1c02019-05-06 00:25:08 +0200254 u32 port_vlan_filter;
Hauke Mehrtens14fceff2018-09-09 22:20:39 +0200255};
256
Hauke Mehrtens8206e0c2019-05-06 00:25:07 +0200257struct gswip_pce_table_entry {
258 u16 index; // PCE_TBL_ADDR.ADDR = pData->table_index
259 u16 table; // PCE_TBL_CTRL.ADDR = pData->table
260 u16 key[8];
261 u16 val[5];
262 u16 mask;
263 u8 gmap;
264 bool type;
265 bool valid;
266 bool key_mode;
267};
268
Hauke Mehrtens14fceff2018-09-09 22:20:39 +0200269struct gswip_rmon_cnt_desc {
270 unsigned int size;
271 unsigned int offset;
272 const char *name;
273};
274
275#define MIB_DESC(_size, _offset, _name) {.size = _size, .offset = _offset, .name = _name}
276
277static const struct gswip_rmon_cnt_desc gswip_rmon_cnt[] = {
278 /** Receive Packet Count (only packets that are accepted and not discarded). */
279 MIB_DESC(1, 0x1F, "RxGoodPkts"),
280 MIB_DESC(1, 0x23, "RxUnicastPkts"),
281 MIB_DESC(1, 0x22, "RxMulticastPkts"),
282 MIB_DESC(1, 0x21, "RxFCSErrorPkts"),
283 MIB_DESC(1, 0x1D, "RxUnderSizeGoodPkts"),
284 MIB_DESC(1, 0x1E, "RxUnderSizeErrorPkts"),
285 MIB_DESC(1, 0x1B, "RxOversizeGoodPkts"),
286 MIB_DESC(1, 0x1C, "RxOversizeErrorPkts"),
287 MIB_DESC(1, 0x20, "RxGoodPausePkts"),
288 MIB_DESC(1, 0x1A, "RxAlignErrorPkts"),
289 MIB_DESC(1, 0x12, "Rx64BytePkts"),
290 MIB_DESC(1, 0x13, "Rx127BytePkts"),
291 MIB_DESC(1, 0x14, "Rx255BytePkts"),
292 MIB_DESC(1, 0x15, "Rx511BytePkts"),
293 MIB_DESC(1, 0x16, "Rx1023BytePkts"),
294 /** Receive Size 1024-1522 (or more, if configured) Packet Count. */
295 MIB_DESC(1, 0x17, "RxMaxBytePkts"),
296 MIB_DESC(1, 0x18, "RxDroppedPkts"),
297 MIB_DESC(1, 0x19, "RxFilteredPkts"),
298 MIB_DESC(2, 0x24, "RxGoodBytes"),
299 MIB_DESC(2, 0x26, "RxBadBytes"),
300 MIB_DESC(1, 0x11, "TxAcmDroppedPkts"),
301 MIB_DESC(1, 0x0C, "TxGoodPkts"),
302 MIB_DESC(1, 0x06, "TxUnicastPkts"),
303 MIB_DESC(1, 0x07, "TxMulticastPkts"),
304 MIB_DESC(1, 0x00, "Tx64BytePkts"),
305 MIB_DESC(1, 0x01, "Tx127BytePkts"),
306 MIB_DESC(1, 0x02, "Tx255BytePkts"),
307 MIB_DESC(1, 0x03, "Tx511BytePkts"),
308 MIB_DESC(1, 0x04, "Tx1023BytePkts"),
309 /** Transmit Size 1024-1522 (or more, if configured) Packet Count. */
310 MIB_DESC(1, 0x05, "TxMaxBytePkts"),
311 MIB_DESC(1, 0x08, "TxSingleCollCount"),
312 MIB_DESC(1, 0x09, "TxMultCollCount"),
313 MIB_DESC(1, 0x0A, "TxLateCollCount"),
314 MIB_DESC(1, 0x0B, "TxExcessCollCount"),
315 MIB_DESC(1, 0x0D, "TxPauseCount"),
316 MIB_DESC(1, 0x10, "TxDroppedPkts"),
317 MIB_DESC(2, 0x0E, "TxGoodBytes"),
318};
319
320static u32 gswip_switch_r(struct gswip_priv *priv, u32 offset)
321{
322 return __raw_readl(priv->gswip + (offset * 4));
323}
324
325static void gswip_switch_w(struct gswip_priv *priv, u32 val, u32 offset)
326{
327 __raw_writel(val, priv->gswip + (offset * 4));
328}
329
330static void gswip_switch_mask(struct gswip_priv *priv, u32 clear, u32 set,
331 u32 offset)
332{
333 u32 val = gswip_switch_r(priv, offset);
334
335 val &= ~(clear);
336 val |= set;
337 gswip_switch_w(priv, val, offset);
338}
339
340static u32 gswip_switch_r_timeout(struct gswip_priv *priv, u32 offset,
341 u32 cleared)
342{
343 u32 val;
344
345 return readx_poll_timeout(__raw_readl, priv->gswip + (offset * 4), val,
346 (val & cleared) == 0, 20, 50000);
347}
348
349static u32 gswip_mdio_r(struct gswip_priv *priv, u32 offset)
350{
351 return __raw_readl(priv->mdio + (offset * 4));
352}
353
354static void gswip_mdio_w(struct gswip_priv *priv, u32 val, u32 offset)
355{
356 __raw_writel(val, priv->mdio + (offset * 4));
357}
358
359static void gswip_mdio_mask(struct gswip_priv *priv, u32 clear, u32 set,
360 u32 offset)
361{
362 u32 val = gswip_mdio_r(priv, offset);
363
364 val &= ~(clear);
365 val |= set;
366 gswip_mdio_w(priv, val, offset);
367}
368
369static u32 gswip_mii_r(struct gswip_priv *priv, u32 offset)
370{
371 return __raw_readl(priv->mii + (offset * 4));
372}
373
374static void gswip_mii_w(struct gswip_priv *priv, u32 val, u32 offset)
375{
376 __raw_writel(val, priv->mii + (offset * 4));
377}
378
379static void gswip_mii_mask(struct gswip_priv *priv, u32 clear, u32 set,
380 u32 offset)
381{
382 u32 val = gswip_mii_r(priv, offset);
383
384 val &= ~(clear);
385 val |= set;
386 gswip_mii_w(priv, val, offset);
387}
388
389static void gswip_mii_mask_cfg(struct gswip_priv *priv, u32 clear, u32 set,
390 int port)
391{
392 switch (port) {
393 case 0:
394 gswip_mii_mask(priv, clear, set, GSWIP_MII_CFG0);
395 break;
396 case 1:
397 gswip_mii_mask(priv, clear, set, GSWIP_MII_CFG1);
398 break;
399 case 5:
400 gswip_mii_mask(priv, clear, set, GSWIP_MII_CFG5);
401 break;
402 }
403}
404
405static void gswip_mii_mask_pcdu(struct gswip_priv *priv, u32 clear, u32 set,
406 int port)
407{
408 switch (port) {
409 case 0:
410 gswip_mii_mask(priv, clear, set, GSWIP_MII_PCDU0);
411 break;
412 case 1:
413 gswip_mii_mask(priv, clear, set, GSWIP_MII_PCDU1);
414 break;
415 case 5:
416 gswip_mii_mask(priv, clear, set, GSWIP_MII_PCDU5);
417 break;
418 }
419}
420
421static int gswip_mdio_poll(struct gswip_priv *priv)
422{
423 int cnt = 100;
424
425 while (likely(cnt--)) {
426 u32 ctrl = gswip_mdio_r(priv, GSWIP_MDIO_CTRL);
427
428 if ((ctrl & GSWIP_MDIO_CTRL_BUSY) == 0)
429 return 0;
430 usleep_range(20, 40);
431 }
432
433 return -ETIMEDOUT;
434}
435
436static int gswip_mdio_wr(struct mii_bus *bus, int addr, int reg, u16 val)
437{
438 struct gswip_priv *priv = bus->priv;
439 int err;
440
441 err = gswip_mdio_poll(priv);
442 if (err) {
443 dev_err(&bus->dev, "waiting for MDIO bus busy timed out\n");
444 return err;
445 }
446
447 gswip_mdio_w(priv, val, GSWIP_MDIO_WRITE);
448 gswip_mdio_w(priv, GSWIP_MDIO_CTRL_BUSY | GSWIP_MDIO_CTRL_WR |
449 ((addr & GSWIP_MDIO_CTRL_PHYAD_MASK) << GSWIP_MDIO_CTRL_PHYAD_SHIFT) |
450 (reg & GSWIP_MDIO_CTRL_REGAD_MASK),
451 GSWIP_MDIO_CTRL);
452
453 return 0;
454}
455
456static int gswip_mdio_rd(struct mii_bus *bus, int addr, int reg)
457{
458 struct gswip_priv *priv = bus->priv;
459 int err;
460
461 err = gswip_mdio_poll(priv);
462 if (err) {
463 dev_err(&bus->dev, "waiting for MDIO bus busy timed out\n");
464 return err;
465 }
466
467 gswip_mdio_w(priv, GSWIP_MDIO_CTRL_BUSY | GSWIP_MDIO_CTRL_RD |
468 ((addr & GSWIP_MDIO_CTRL_PHYAD_MASK) << GSWIP_MDIO_CTRL_PHYAD_SHIFT) |
469 (reg & GSWIP_MDIO_CTRL_REGAD_MASK),
470 GSWIP_MDIO_CTRL);
471
472 err = gswip_mdio_poll(priv);
473 if (err) {
474 dev_err(&bus->dev, "waiting for MDIO bus busy timed out\n");
475 return err;
476 }
477
478 return gswip_mdio_r(priv, GSWIP_MDIO_READ);
479}
480
481static int gswip_mdio(struct gswip_priv *priv, struct device_node *mdio_np)
482{
483 struct dsa_switch *ds = priv->ds;
484
485 ds->slave_mii_bus = devm_mdiobus_alloc(priv->dev);
486 if (!ds->slave_mii_bus)
487 return -ENOMEM;
488
489 ds->slave_mii_bus->priv = priv;
490 ds->slave_mii_bus->read = gswip_mdio_rd;
491 ds->slave_mii_bus->write = gswip_mdio_wr;
492 ds->slave_mii_bus->name = "lantiq,xrx200-mdio";
493 snprintf(ds->slave_mii_bus->id, MII_BUS_ID_SIZE, "%s-mii",
494 dev_name(priv->dev));
495 ds->slave_mii_bus->parent = priv->dev;
496 ds->slave_mii_bus->phy_mask = ~ds->phys_mii_mask;
497
498 return of_mdiobus_register(ds->slave_mii_bus, mdio_np);
499}
500
Hauke Mehrtens8206e0c2019-05-06 00:25:07 +0200501static int gswip_pce_table_entry_read(struct gswip_priv *priv,
502 struct gswip_pce_table_entry *tbl)
503{
504 int i;
505 int err;
506 u16 crtl;
507 u16 addr_mode = tbl->key_mode ? GSWIP_PCE_TBL_CTRL_OPMOD_KSRD :
508 GSWIP_PCE_TBL_CTRL_OPMOD_ADRD;
509
510 err = gswip_switch_r_timeout(priv, GSWIP_PCE_TBL_CTRL,
511 GSWIP_PCE_TBL_CTRL_BAS);
512 if (err)
513 return err;
514
515 gswip_switch_w(priv, tbl->index, GSWIP_PCE_TBL_ADDR);
516 gswip_switch_mask(priv, GSWIP_PCE_TBL_CTRL_ADDR_MASK |
517 GSWIP_PCE_TBL_CTRL_OPMOD_MASK,
518 tbl->table | addr_mode | GSWIP_PCE_TBL_CTRL_BAS,
519 GSWIP_PCE_TBL_CTRL);
520
521 err = gswip_switch_r_timeout(priv, GSWIP_PCE_TBL_CTRL,
522 GSWIP_PCE_TBL_CTRL_BAS);
523 if (err)
524 return err;
525
526 for (i = 0; i < ARRAY_SIZE(tbl->key); i++)
527 tbl->key[i] = gswip_switch_r(priv, GSWIP_PCE_TBL_KEY(i));
528
529 for (i = 0; i < ARRAY_SIZE(tbl->val); i++)
530 tbl->val[i] = gswip_switch_r(priv, GSWIP_PCE_TBL_VAL(i));
531
532 tbl->mask = gswip_switch_r(priv, GSWIP_PCE_TBL_MASK);
533
534 crtl = gswip_switch_r(priv, GSWIP_PCE_TBL_CTRL);
535
536 tbl->type = !!(crtl & GSWIP_PCE_TBL_CTRL_TYPE);
537 tbl->valid = !!(crtl & GSWIP_PCE_TBL_CTRL_VLD);
538 tbl->gmap = (crtl & GSWIP_PCE_TBL_CTRL_GMAP_MASK) >> 7;
539
540 return 0;
541}
542
543static int gswip_pce_table_entry_write(struct gswip_priv *priv,
544 struct gswip_pce_table_entry *tbl)
545{
546 int i;
547 int err;
548 u16 crtl;
549 u16 addr_mode = tbl->key_mode ? GSWIP_PCE_TBL_CTRL_OPMOD_KSWR :
550 GSWIP_PCE_TBL_CTRL_OPMOD_ADWR;
551
552 err = gswip_switch_r_timeout(priv, GSWIP_PCE_TBL_CTRL,
553 GSWIP_PCE_TBL_CTRL_BAS);
554 if (err)
555 return err;
556
557 gswip_switch_w(priv, tbl->index, GSWIP_PCE_TBL_ADDR);
558 gswip_switch_mask(priv, GSWIP_PCE_TBL_CTRL_ADDR_MASK |
559 GSWIP_PCE_TBL_CTRL_OPMOD_MASK,
560 tbl->table | addr_mode,
561 GSWIP_PCE_TBL_CTRL);
562
563 for (i = 0; i < ARRAY_SIZE(tbl->key); i++)
564 gswip_switch_w(priv, tbl->key[i], GSWIP_PCE_TBL_KEY(i));
565
566 for (i = 0; i < ARRAY_SIZE(tbl->val); i++)
567 gswip_switch_w(priv, tbl->val[i], GSWIP_PCE_TBL_VAL(i));
568
569 gswip_switch_mask(priv, GSWIP_PCE_TBL_CTRL_ADDR_MASK |
570 GSWIP_PCE_TBL_CTRL_OPMOD_MASK,
571 tbl->table | addr_mode,
572 GSWIP_PCE_TBL_CTRL);
573
574 gswip_switch_w(priv, tbl->mask, GSWIP_PCE_TBL_MASK);
575
576 crtl = gswip_switch_r(priv, GSWIP_PCE_TBL_CTRL);
577 crtl &= ~(GSWIP_PCE_TBL_CTRL_TYPE | GSWIP_PCE_TBL_CTRL_VLD |
578 GSWIP_PCE_TBL_CTRL_GMAP_MASK);
579 if (tbl->type)
580 crtl |= GSWIP_PCE_TBL_CTRL_TYPE;
581 if (tbl->valid)
582 crtl |= GSWIP_PCE_TBL_CTRL_VLD;
583 crtl |= (tbl->gmap << 7) & GSWIP_PCE_TBL_CTRL_GMAP_MASK;
584 crtl |= GSWIP_PCE_TBL_CTRL_BAS;
585 gswip_switch_w(priv, crtl, GSWIP_PCE_TBL_CTRL);
586
587 return gswip_switch_r_timeout(priv, GSWIP_PCE_TBL_CTRL,
588 GSWIP_PCE_TBL_CTRL_BAS);
589}
590
591/* Add the LAN port into a bridge with the CPU port by
592 * default. This prevents automatic forwarding of
593 * packages between the LAN ports when no explicit
594 * bridge is configured.
595 */
596static int gswip_add_single_port_br(struct gswip_priv *priv, int port, bool add)
597{
598 struct gswip_pce_table_entry vlan_active = {0,};
599 struct gswip_pce_table_entry vlan_mapping = {0,};
600 unsigned int cpu_port = priv->hw_info->cpu_port;
601 unsigned int max_ports = priv->hw_info->max_ports;
602 int err;
603
604 if (port >= max_ports) {
605 dev_err(priv->dev, "single port for %i supported\n", port);
606 return -EIO;
607 }
608
609 vlan_active.index = port + 1;
610 vlan_active.table = GSWIP_TABLE_ACTIVE_VLAN;
611 vlan_active.key[0] = 0; /* vid */
612 vlan_active.val[0] = port + 1 /* fid */;
613 vlan_active.valid = add;
614 err = gswip_pce_table_entry_write(priv, &vlan_active);
615 if (err) {
616 dev_err(priv->dev, "failed to write active VLAN: %d\n", err);
617 return err;
618 }
619
620 if (!add)
621 return 0;
622
623 vlan_mapping.index = port + 1;
624 vlan_mapping.table = GSWIP_TABLE_VLAN_MAPPING;
625 vlan_mapping.val[0] = 0 /* vid */;
626 vlan_mapping.val[1] = BIT(port) | BIT(cpu_port);
627 vlan_mapping.val[2] = 0;
628 err = gswip_pce_table_entry_write(priv, &vlan_mapping);
629 if (err) {
630 dev_err(priv->dev, "failed to write VLAN mapping: %d\n", err);
631 return err;
632 }
633
634 return 0;
635}
636
Hauke Mehrtens14fceff2018-09-09 22:20:39 +0200637static int gswip_port_enable(struct dsa_switch *ds, int port,
638 struct phy_device *phydev)
639{
640 struct gswip_priv *priv = ds->priv;
Hauke Mehrtens8206e0c2019-05-06 00:25:07 +0200641 int err;
642
643 if (!dsa_is_cpu_port(ds, port)) {
644 err = gswip_add_single_port_br(priv, port, true);
645 if (err)
646 return err;
647 }
Hauke Mehrtens14fceff2018-09-09 22:20:39 +0200648
649 /* RMON Counter Enable for port */
650 gswip_switch_w(priv, GSWIP_BM_PCFG_CNTEN, GSWIP_BM_PCFGp(port));
651
652 /* enable port fetch/store dma & VLAN Modification */
653 gswip_switch_mask(priv, 0, GSWIP_FDMA_PCTRL_EN |
654 GSWIP_FDMA_PCTRL_VLANMOD_BOTH,
655 GSWIP_FDMA_PCTRLp(port));
656 gswip_switch_mask(priv, 0, GSWIP_SDMA_PCTRL_EN,
657 GSWIP_SDMA_PCTRLp(port));
Hauke Mehrtens14fceff2018-09-09 22:20:39 +0200658
659 if (!dsa_is_cpu_port(ds, port)) {
660 u32 macconf = GSWIP_MDIO_PHY_LINK_AUTO |
661 GSWIP_MDIO_PHY_SPEED_AUTO |
662 GSWIP_MDIO_PHY_FDUP_AUTO |
663 GSWIP_MDIO_PHY_FCONTX_AUTO |
664 GSWIP_MDIO_PHY_FCONRX_AUTO |
665 (phydev->mdio.addr & GSWIP_MDIO_PHY_ADDR_MASK);
666
667 gswip_mdio_w(priv, macconf, GSWIP_MDIO_PHYp(port));
668 /* Activate MDIO auto polling */
669 gswip_mdio_mask(priv, 0, BIT(port), GSWIP_MDIO_MDC_CFG0);
670 }
671
672 return 0;
673}
674
Andrew Lunn75104db2019-02-24 20:44:43 +0100675static void gswip_port_disable(struct dsa_switch *ds, int port)
Hauke Mehrtens14fceff2018-09-09 22:20:39 +0200676{
677 struct gswip_priv *priv = ds->priv;
678
679 if (!dsa_is_cpu_port(ds, port)) {
680 gswip_mdio_mask(priv, GSWIP_MDIO_PHY_LINK_DOWN,
681 GSWIP_MDIO_PHY_LINK_MASK,
682 GSWIP_MDIO_PHYp(port));
683 /* Deactivate MDIO auto polling */
684 gswip_mdio_mask(priv, BIT(port), 0, GSWIP_MDIO_MDC_CFG0);
685 }
686
687 gswip_switch_mask(priv, GSWIP_FDMA_PCTRL_EN, 0,
688 GSWIP_FDMA_PCTRLp(port));
689 gswip_switch_mask(priv, GSWIP_SDMA_PCTRL_EN, 0,
690 GSWIP_SDMA_PCTRLp(port));
691}
692
693static int gswip_pce_load_microcode(struct gswip_priv *priv)
694{
695 int i;
696 int err;
697
698 gswip_switch_mask(priv, GSWIP_PCE_TBL_CTRL_ADDR_MASK |
699 GSWIP_PCE_TBL_CTRL_OPMOD_MASK,
700 GSWIP_PCE_TBL_CTRL_OPMOD_ADWR, GSWIP_PCE_TBL_CTRL);
701 gswip_switch_w(priv, 0, GSWIP_PCE_TBL_MASK);
702
703 for (i = 0; i < ARRAY_SIZE(gswip_pce_microcode); i++) {
704 gswip_switch_w(priv, i, GSWIP_PCE_TBL_ADDR);
705 gswip_switch_w(priv, gswip_pce_microcode[i].val_0,
706 GSWIP_PCE_TBL_VAL(0));
707 gswip_switch_w(priv, gswip_pce_microcode[i].val_1,
708 GSWIP_PCE_TBL_VAL(1));
709 gswip_switch_w(priv, gswip_pce_microcode[i].val_2,
710 GSWIP_PCE_TBL_VAL(2));
711 gswip_switch_w(priv, gswip_pce_microcode[i].val_3,
712 GSWIP_PCE_TBL_VAL(3));
713
714 /* start the table access: */
715 gswip_switch_mask(priv, 0, GSWIP_PCE_TBL_CTRL_BAS,
716 GSWIP_PCE_TBL_CTRL);
717 err = gswip_switch_r_timeout(priv, GSWIP_PCE_TBL_CTRL,
718 GSWIP_PCE_TBL_CTRL_BAS);
719 if (err)
720 return err;
721 }
722
723 /* tell the switch that the microcode is loaded */
724 gswip_switch_mask(priv, 0, GSWIP_PCE_GCTRL_0_MC_VALID,
725 GSWIP_PCE_GCTRL_0);
726
727 return 0;
728}
729
Hauke Mehrtens8206e0c2019-05-06 00:25:07 +0200730static int gswip_port_vlan_filtering(struct dsa_switch *ds, int port,
731 bool vlan_filtering)
732{
733 struct gswip_priv *priv = ds->priv;
Hauke Mehrtens9bbb1c02019-05-06 00:25:08 +0200734 struct net_device *bridge = dsa_to_port(ds, port)->bridge_dev;
735
736 /* Do not allow changing the VLAN filtering options while in bridge */
737 if (!!(priv->port_vlan_filter & BIT(port)) != vlan_filtering && bridge)
738 return -EIO;
Hauke Mehrtens8206e0c2019-05-06 00:25:07 +0200739
740 if (vlan_filtering) {
741 /* Use port based VLAN tag */
742 gswip_switch_mask(priv,
743 GSWIP_PCE_VCTRL_VSR,
744 GSWIP_PCE_VCTRL_UVR | GSWIP_PCE_VCTRL_VIMR |
745 GSWIP_PCE_VCTRL_VEMR,
746 GSWIP_PCE_VCTRL(port));
747 gswip_switch_mask(priv, GSWIP_PCE_PCTRL_0_TVM, 0,
748 GSWIP_PCE_PCTRL_0p(port));
749 } else {
750 /* Use port based VLAN tag */
751 gswip_switch_mask(priv,
752 GSWIP_PCE_VCTRL_UVR | GSWIP_PCE_VCTRL_VIMR |
753 GSWIP_PCE_VCTRL_VEMR,
754 GSWIP_PCE_VCTRL_VSR,
755 GSWIP_PCE_VCTRL(port));
756 gswip_switch_mask(priv, 0, GSWIP_PCE_PCTRL_0_TVM,
757 GSWIP_PCE_PCTRL_0p(port));
758 }
759
760 return 0;
761}
762
Hauke Mehrtens14fceff2018-09-09 22:20:39 +0200763static int gswip_setup(struct dsa_switch *ds)
764{
765 struct gswip_priv *priv = ds->priv;
766 unsigned int cpu_port = priv->hw_info->cpu_port;
767 int i;
768 int err;
769
770 gswip_switch_w(priv, GSWIP_SWRES_R0, GSWIP_SWRES);
771 usleep_range(5000, 10000);
772 gswip_switch_w(priv, 0, GSWIP_SWRES);
773
774 /* disable port fetch/store dma on all ports */
Hauke Mehrtens8206e0c2019-05-06 00:25:07 +0200775 for (i = 0; i < priv->hw_info->max_ports; i++) {
Andrew Lunn75104db2019-02-24 20:44:43 +0100776 gswip_port_disable(ds, i);
Hauke Mehrtens8206e0c2019-05-06 00:25:07 +0200777 gswip_port_vlan_filtering(ds, i, false);
778 }
Hauke Mehrtens14fceff2018-09-09 22:20:39 +0200779
780 /* enable Switch */
781 gswip_mdio_mask(priv, 0, GSWIP_MDIO_GLOB_ENABLE, GSWIP_MDIO_GLOB);
782
783 err = gswip_pce_load_microcode(priv);
784 if (err) {
785 dev_err(priv->dev, "writing PCE microcode failed, %i", err);
786 return err;
787 }
788
789 /* Default unknown Broadcast/Multicast/Unicast port maps */
790 gswip_switch_w(priv, BIT(cpu_port), GSWIP_PCE_PMAP1);
791 gswip_switch_w(priv, BIT(cpu_port), GSWIP_PCE_PMAP2);
792 gswip_switch_w(priv, BIT(cpu_port), GSWIP_PCE_PMAP3);
793
794 /* disable PHY auto polling */
795 gswip_mdio_w(priv, 0x0, GSWIP_MDIO_MDC_CFG0);
796 /* Configure the MDIO Clock 2.5 MHz */
797 gswip_mdio_mask(priv, 0xff, 0x09, GSWIP_MDIO_MDC_CFG1);
798
799 /* Disable the xMII link */
800 gswip_mii_mask_cfg(priv, GSWIP_MII_CFG_EN, 0, 0);
801 gswip_mii_mask_cfg(priv, GSWIP_MII_CFG_EN, 0, 1);
802 gswip_mii_mask_cfg(priv, GSWIP_MII_CFG_EN, 0, 5);
803
804 /* enable special tag insertion on cpu port */
805 gswip_switch_mask(priv, 0, GSWIP_FDMA_PCTRL_STEN,
806 GSWIP_FDMA_PCTRLp(cpu_port));
807
Hauke Mehrtens30d893832019-05-06 00:25:06 +0200808 /* accept special tag in ingress direction */
809 gswip_switch_mask(priv, 0, GSWIP_PCE_PCTRL_0_INGRESS,
810 GSWIP_PCE_PCTRL_0p(cpu_port));
811
Hauke Mehrtens14fceff2018-09-09 22:20:39 +0200812 gswip_switch_mask(priv, 0, GSWIP_MAC_CTRL_2_MLEN,
813 GSWIP_MAC_CTRL_2p(cpu_port));
814 gswip_switch_w(priv, VLAN_ETH_FRAME_LEN + 8, GSWIP_MAC_FLEN);
815 gswip_switch_mask(priv, 0, GSWIP_BM_QUEUE_GCTRL_GL_MOD,
816 GSWIP_BM_QUEUE_GCTRL);
817
818 /* VLAN aware Switching */
819 gswip_switch_mask(priv, 0, GSWIP_PCE_GCTRL_0_VLAN, GSWIP_PCE_GCTRL_0);
820
Hauke Mehrtens8206e0c2019-05-06 00:25:07 +0200821 /* Flush MAC Table */
822 gswip_switch_mask(priv, 0, GSWIP_PCE_GCTRL_0_MTFL, GSWIP_PCE_GCTRL_0);
823
824 err = gswip_switch_r_timeout(priv, GSWIP_PCE_GCTRL_0,
825 GSWIP_PCE_GCTRL_0_MTFL);
826 if (err) {
827 dev_err(priv->dev, "MAC flushing didn't finish\n");
828 return err;
829 }
Hauke Mehrtens14fceff2018-09-09 22:20:39 +0200830
831 gswip_port_enable(ds, cpu_port, NULL);
832 return 0;
833}
834
835static enum dsa_tag_protocol gswip_get_tag_protocol(struct dsa_switch *ds,
836 int port)
837{
838 return DSA_TAG_PROTO_GSWIP;
839}
840
Hauke Mehrtens8206e0c2019-05-06 00:25:07 +0200841static int gswip_vlan_active_create(struct gswip_priv *priv,
842 struct net_device *bridge,
843 int fid, u16 vid)
844{
845 struct gswip_pce_table_entry vlan_active = {0,};
846 unsigned int max_ports = priv->hw_info->max_ports;
847 int idx = -1;
848 int err;
849 int i;
850
851 /* Look for a free slot */
852 for (i = max_ports; i < ARRAY_SIZE(priv->vlans); i++) {
853 if (!priv->vlans[i].bridge) {
854 idx = i;
855 break;
856 }
857 }
858
859 if (idx == -1)
860 return -ENOSPC;
861
862 if (fid == -1)
863 fid = idx;
864
865 vlan_active.index = idx;
866 vlan_active.table = GSWIP_TABLE_ACTIVE_VLAN;
867 vlan_active.key[0] = vid;
868 vlan_active.val[0] = fid;
869 vlan_active.valid = true;
870
871 err = gswip_pce_table_entry_write(priv, &vlan_active);
872 if (err) {
873 dev_err(priv->dev, "failed to write active VLAN: %d\n", err);
874 return err;
875 }
876
877 priv->vlans[idx].bridge = bridge;
878 priv->vlans[idx].vid = vid;
879 priv->vlans[idx].fid = fid;
880
881 return idx;
882}
883
884static int gswip_vlan_active_remove(struct gswip_priv *priv, int idx)
885{
886 struct gswip_pce_table_entry vlan_active = {0,};
887 int err;
888
889 vlan_active.index = idx;
890 vlan_active.table = GSWIP_TABLE_ACTIVE_VLAN;
891 vlan_active.valid = false;
892 err = gswip_pce_table_entry_write(priv, &vlan_active);
893 if (err)
894 dev_err(priv->dev, "failed to delete active VLAN: %d\n", err);
895 priv->vlans[idx].bridge = NULL;
896
897 return err;
898}
899
900static int gswip_vlan_add_unaware(struct gswip_priv *priv,
901 struct net_device *bridge, int port)
902{
903 struct gswip_pce_table_entry vlan_mapping = {0,};
904 unsigned int max_ports = priv->hw_info->max_ports;
905 unsigned int cpu_port = priv->hw_info->cpu_port;
906 bool active_vlan_created = false;
907 int idx = -1;
908 int i;
909 int err;
910
911 /* Check if there is already a page for this bridge */
912 for (i = max_ports; i < ARRAY_SIZE(priv->vlans); i++) {
913 if (priv->vlans[i].bridge == bridge) {
914 idx = i;
915 break;
916 }
917 }
918
919 /* If this bridge is not programmed yet, add a Active VLAN table
920 * entry in a free slot and prepare the VLAN mapping table entry.
921 */
922 if (idx == -1) {
923 idx = gswip_vlan_active_create(priv, bridge, -1, 0);
924 if (idx < 0)
925 return idx;
926 active_vlan_created = true;
927
928 vlan_mapping.index = idx;
929 vlan_mapping.table = GSWIP_TABLE_VLAN_MAPPING;
930 /* VLAN ID byte, maps to the VLAN ID of vlan active table */
931 vlan_mapping.val[0] = 0;
932 } else {
933 /* Read the existing VLAN mapping entry from the switch */
934 vlan_mapping.index = idx;
935 vlan_mapping.table = GSWIP_TABLE_VLAN_MAPPING;
936 err = gswip_pce_table_entry_read(priv, &vlan_mapping);
937 if (err) {
938 dev_err(priv->dev, "failed to read VLAN mapping: %d\n",
939 err);
940 return err;
941 }
942 }
943
944 /* Update the VLAN mapping entry and write it to the switch */
945 vlan_mapping.val[1] |= BIT(cpu_port);
946 vlan_mapping.val[1] |= BIT(port);
947 err = gswip_pce_table_entry_write(priv, &vlan_mapping);
948 if (err) {
949 dev_err(priv->dev, "failed to write VLAN mapping: %d\n", err);
950 /* In case an Active VLAN was creaetd delete it again */
951 if (active_vlan_created)
952 gswip_vlan_active_remove(priv, idx);
953 return err;
954 }
955
956 gswip_switch_w(priv, 0, GSWIP_PCE_DEFPVID(port));
957 return 0;
958}
959
Hauke Mehrtens9bbb1c02019-05-06 00:25:08 +0200960static int gswip_vlan_add_aware(struct gswip_priv *priv,
961 struct net_device *bridge, int port,
962 u16 vid, bool untagged,
963 bool pvid)
964{
965 struct gswip_pce_table_entry vlan_mapping = {0,};
966 unsigned int max_ports = priv->hw_info->max_ports;
967 unsigned int cpu_port = priv->hw_info->cpu_port;
968 bool active_vlan_created = false;
969 int idx = -1;
970 int fid = -1;
971 int i;
972 int err;
973
974 /* Check if there is already a page for this bridge */
975 for (i = max_ports; i < ARRAY_SIZE(priv->vlans); i++) {
976 if (priv->vlans[i].bridge == bridge) {
977 if (fid != -1 && fid != priv->vlans[i].fid)
978 dev_err(priv->dev, "one bridge with multiple flow ids\n");
979 fid = priv->vlans[i].fid;
980 if (priv->vlans[i].vid == vid) {
981 idx = i;
982 break;
983 }
984 }
985 }
986
987 /* If this bridge is not programmed yet, add a Active VLAN table
988 * entry in a free slot and prepare the VLAN mapping table entry.
989 */
990 if (idx == -1) {
991 idx = gswip_vlan_active_create(priv, bridge, fid, vid);
992 if (idx < 0)
993 return idx;
994 active_vlan_created = true;
995
996 vlan_mapping.index = idx;
997 vlan_mapping.table = GSWIP_TABLE_VLAN_MAPPING;
998 /* VLAN ID byte, maps to the VLAN ID of vlan active table */
999 vlan_mapping.val[0] = vid;
1000 } else {
1001 /* Read the existing VLAN mapping entry from the switch */
1002 vlan_mapping.index = idx;
1003 vlan_mapping.table = GSWIP_TABLE_VLAN_MAPPING;
1004 err = gswip_pce_table_entry_read(priv, &vlan_mapping);
1005 if (err) {
1006 dev_err(priv->dev, "failed to read VLAN mapping: %d\n",
1007 err);
1008 return err;
1009 }
1010 }
1011
1012 vlan_mapping.val[0] = vid;
1013 /* Update the VLAN mapping entry and write it to the switch */
1014 vlan_mapping.val[1] |= BIT(cpu_port);
1015 vlan_mapping.val[2] |= BIT(cpu_port);
1016 vlan_mapping.val[1] |= BIT(port);
1017 if (untagged)
1018 vlan_mapping.val[2] &= ~BIT(port);
1019 else
1020 vlan_mapping.val[2] |= BIT(port);
1021 err = gswip_pce_table_entry_write(priv, &vlan_mapping);
1022 if (err) {
1023 dev_err(priv->dev, "failed to write VLAN mapping: %d\n", err);
1024 /* In case an Active VLAN was creaetd delete it again */
1025 if (active_vlan_created)
1026 gswip_vlan_active_remove(priv, idx);
1027 return err;
1028 }
1029
1030 if (pvid)
1031 gswip_switch_w(priv, idx, GSWIP_PCE_DEFPVID(port));
1032
1033 return 0;
1034}
1035
Hauke Mehrtens8206e0c2019-05-06 00:25:07 +02001036static int gswip_vlan_remove(struct gswip_priv *priv,
1037 struct net_device *bridge, int port,
1038 u16 vid, bool pvid, bool vlan_aware)
1039{
1040 struct gswip_pce_table_entry vlan_mapping = {0,};
1041 unsigned int max_ports = priv->hw_info->max_ports;
1042 unsigned int cpu_port = priv->hw_info->cpu_port;
1043 int idx = -1;
1044 int i;
1045 int err;
1046
1047 /* Check if there is already a page for this bridge */
1048 for (i = max_ports; i < ARRAY_SIZE(priv->vlans); i++) {
1049 if (priv->vlans[i].bridge == bridge &&
1050 (!vlan_aware || priv->vlans[i].vid == vid)) {
1051 idx = i;
1052 break;
1053 }
1054 }
1055
1056 if (idx == -1) {
1057 dev_err(priv->dev, "bridge to leave does not exists\n");
1058 return -ENOENT;
1059 }
1060
1061 vlan_mapping.index = idx;
1062 vlan_mapping.table = GSWIP_TABLE_VLAN_MAPPING;
1063 err = gswip_pce_table_entry_read(priv, &vlan_mapping);
1064 if (err) {
1065 dev_err(priv->dev, "failed to read VLAN mapping: %d\n", err);
1066 return err;
1067 }
1068
1069 vlan_mapping.val[1] &= ~BIT(port);
1070 vlan_mapping.val[2] &= ~BIT(port);
1071 err = gswip_pce_table_entry_write(priv, &vlan_mapping);
1072 if (err) {
1073 dev_err(priv->dev, "failed to write VLAN mapping: %d\n", err);
1074 return err;
1075 }
1076
1077 /* In case all ports are removed from the bridge, remove the VLAN */
1078 if ((vlan_mapping.val[1] & ~BIT(cpu_port)) == 0) {
1079 err = gswip_vlan_active_remove(priv, idx);
1080 if (err) {
1081 dev_err(priv->dev, "failed to write active VLAN: %d\n",
1082 err);
1083 return err;
1084 }
1085 }
1086
1087 /* GSWIP 2.2 (GRX300) and later program here the VID directly. */
1088 if (pvid)
1089 gswip_switch_w(priv, 0, GSWIP_PCE_DEFPVID(port));
1090
1091 return 0;
1092}
1093
1094static int gswip_port_bridge_join(struct dsa_switch *ds, int port,
1095 struct net_device *bridge)
1096{
1097 struct gswip_priv *priv = ds->priv;
1098 int err;
1099
Hauke Mehrtens9bbb1c02019-05-06 00:25:08 +02001100 /* When the bridge uses VLAN filtering we have to configure VLAN
1101 * specific bridges. No bridge is configured here.
1102 */
1103 if (!br_vlan_enabled(bridge)) {
1104 err = gswip_vlan_add_unaware(priv, bridge, port);
1105 if (err)
1106 return err;
1107 priv->port_vlan_filter &= ~BIT(port);
1108 } else {
1109 priv->port_vlan_filter |= BIT(port);
1110 }
Hauke Mehrtens8206e0c2019-05-06 00:25:07 +02001111 return gswip_add_single_port_br(priv, port, false);
1112}
1113
1114static void gswip_port_bridge_leave(struct dsa_switch *ds, int port,
1115 struct net_device *bridge)
1116{
1117 struct gswip_priv *priv = ds->priv;
1118
1119 gswip_add_single_port_br(priv, port, true);
1120
Hauke Mehrtens9bbb1c02019-05-06 00:25:08 +02001121 /* When the bridge uses VLAN filtering we have to configure VLAN
1122 * specific bridges. No bridge is configured here.
1123 */
1124 if (!br_vlan_enabled(bridge))
1125 gswip_vlan_remove(priv, bridge, port, 0, true, false);
1126}
1127
1128static int gswip_port_vlan_prepare(struct dsa_switch *ds, int port,
1129 const struct switchdev_obj_port_vlan *vlan)
1130{
1131 struct gswip_priv *priv = ds->priv;
1132 struct net_device *bridge = dsa_to_port(ds, port)->bridge_dev;
1133 unsigned int max_ports = priv->hw_info->max_ports;
1134 u16 vid;
1135 int i;
1136 int pos = max_ports;
1137
1138 /* We only support VLAN filtering on bridges */
1139 if (!dsa_is_cpu_port(ds, port) && !bridge)
1140 return -EOPNOTSUPP;
1141
1142 for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid) {
1143 int idx = -1;
1144
1145 /* Check if there is already a page for this VLAN */
1146 for (i = max_ports; i < ARRAY_SIZE(priv->vlans); i++) {
1147 if (priv->vlans[i].bridge == bridge &&
1148 priv->vlans[i].vid == vid) {
1149 idx = i;
1150 break;
1151 }
1152 }
1153
1154 /* If this VLAN is not programmed yet, we have to reserve
1155 * one entry in the VLAN table. Make sure we start at the
1156 * next position round.
1157 */
1158 if (idx == -1) {
1159 /* Look for a free slot */
1160 for (; pos < ARRAY_SIZE(priv->vlans); pos++) {
1161 if (!priv->vlans[pos].bridge) {
1162 idx = pos;
1163 pos++;
1164 break;
1165 }
1166 }
1167
1168 if (idx == -1)
1169 return -ENOSPC;
1170 }
1171 }
1172
1173 return 0;
1174}
1175
1176static void gswip_port_vlan_add(struct dsa_switch *ds, int port,
1177 const struct switchdev_obj_port_vlan *vlan)
1178{
1179 struct gswip_priv *priv = ds->priv;
1180 struct net_device *bridge = dsa_to_port(ds, port)->bridge_dev;
1181 bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
1182 bool pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
1183 u16 vid;
1184
1185 /* We have to receive all packets on the CPU port and should not
1186 * do any VLAN filtering here. This is also called with bridge
1187 * NULL and then we do not know for which bridge to configure
1188 * this.
1189 */
1190 if (dsa_is_cpu_port(ds, port))
1191 return;
1192
1193 for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid)
1194 gswip_vlan_add_aware(priv, bridge, port, vid, untagged, pvid);
1195}
1196
1197static int gswip_port_vlan_del(struct dsa_switch *ds, int port,
1198 const struct switchdev_obj_port_vlan *vlan)
1199{
1200 struct gswip_priv *priv = ds->priv;
1201 struct net_device *bridge = dsa_to_port(ds, port)->bridge_dev;
1202 bool pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
1203 u16 vid;
1204 int err;
1205
1206 /* We have to receive all packets on the CPU port and should not
1207 * do any VLAN filtering here. This is also called with bridge
1208 * NULL and then we do not know for which bridge to configure
1209 * this.
1210 */
1211 if (dsa_is_cpu_port(ds, port))
1212 return 0;
1213
1214 for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid) {
1215 err = gswip_vlan_remove(priv, bridge, port, vid, pvid, true);
1216 if (err)
1217 return err;
1218 }
1219
1220 return 0;
Hauke Mehrtens8206e0c2019-05-06 00:25:07 +02001221}
1222
1223static void gswip_port_stp_state_set(struct dsa_switch *ds, int port, u8 state)
1224{
1225 struct gswip_priv *priv = ds->priv;
1226 u32 stp_state;
1227
1228 switch (state) {
1229 case BR_STATE_DISABLED:
1230 gswip_switch_mask(priv, GSWIP_SDMA_PCTRL_EN, 0,
1231 GSWIP_SDMA_PCTRLp(port));
1232 return;
1233 case BR_STATE_BLOCKING:
1234 case BR_STATE_LISTENING:
1235 stp_state = GSWIP_PCE_PCTRL_0_PSTATE_LISTEN;
1236 break;
1237 case BR_STATE_LEARNING:
1238 stp_state = GSWIP_PCE_PCTRL_0_PSTATE_LEARNING;
1239 break;
1240 case BR_STATE_FORWARDING:
1241 stp_state = GSWIP_PCE_PCTRL_0_PSTATE_FORWARDING;
1242 break;
1243 default:
1244 dev_err(priv->dev, "invalid STP state: %d\n", state);
1245 return;
1246 }
1247
1248 gswip_switch_mask(priv, 0, GSWIP_SDMA_PCTRL_EN,
1249 GSWIP_SDMA_PCTRLp(port));
1250 gswip_switch_mask(priv, GSWIP_PCE_PCTRL_0_PSTATE_MASK, stp_state,
1251 GSWIP_PCE_PCTRL_0p(port));
1252}
1253
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02001254static void gswip_phylink_validate(struct dsa_switch *ds, int port,
1255 unsigned long *supported,
1256 struct phylink_link_state *state)
1257{
1258 __ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, };
1259
1260 switch (port) {
1261 case 0:
1262 case 1:
1263 if (!phy_interface_mode_is_rgmii(state->interface) &&
1264 state->interface != PHY_INTERFACE_MODE_MII &&
1265 state->interface != PHY_INTERFACE_MODE_REVMII &&
Hauke Mehrtens0e630b52018-09-15 14:08:48 +02001266 state->interface != PHY_INTERFACE_MODE_RMII)
1267 goto unsupported;
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02001268 break;
1269 case 2:
1270 case 3:
1271 case 4:
Hauke Mehrtens0e630b52018-09-15 14:08:48 +02001272 if (state->interface != PHY_INTERFACE_MODE_INTERNAL)
1273 goto unsupported;
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02001274 break;
1275 case 5:
1276 if (!phy_interface_mode_is_rgmii(state->interface) &&
Hauke Mehrtens0e630b52018-09-15 14:08:48 +02001277 state->interface != PHY_INTERFACE_MODE_INTERNAL)
1278 goto unsupported;
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02001279 break;
Hauke Mehrtens0e630b52018-09-15 14:08:48 +02001280 default:
1281 bitmap_zero(supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
1282 dev_err(ds->dev, "Unsupported port: %i\n", port);
1283 return;
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02001284 }
1285
1286 /* Allow all the expected bits */
1287 phylink_set(mask, Autoneg);
1288 phylink_set_port_modes(mask);
1289 phylink_set(mask, Pause);
1290 phylink_set(mask, Asym_Pause);
1291
1292 /* With the exclusion of MII and Reverse MII, we support Gigabit,
1293 * including Half duplex
1294 */
1295 if (state->interface != PHY_INTERFACE_MODE_MII &&
1296 state->interface != PHY_INTERFACE_MODE_REVMII) {
1297 phylink_set(mask, 1000baseT_Full);
1298 phylink_set(mask, 1000baseT_Half);
1299 }
1300
1301 phylink_set(mask, 10baseT_Half);
1302 phylink_set(mask, 10baseT_Full);
1303 phylink_set(mask, 100baseT_Half);
1304 phylink_set(mask, 100baseT_Full);
1305
1306 bitmap_and(supported, supported, mask,
1307 __ETHTOOL_LINK_MODE_MASK_NBITS);
1308 bitmap_and(state->advertising, state->advertising, mask,
1309 __ETHTOOL_LINK_MODE_MASK_NBITS);
Hauke Mehrtens0e630b52018-09-15 14:08:48 +02001310 return;
1311
1312unsupported:
1313 bitmap_zero(supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
1314 dev_err(ds->dev, "Unsupported interface: %d\n", state->interface);
1315 return;
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02001316}
1317
1318static void gswip_phylink_mac_config(struct dsa_switch *ds, int port,
1319 unsigned int mode,
1320 const struct phylink_link_state *state)
1321{
1322 struct gswip_priv *priv = ds->priv;
1323 u32 miicfg = 0;
1324
1325 miicfg |= GSWIP_MII_CFG_LDCLKDIS;
1326
1327 switch (state->interface) {
1328 case PHY_INTERFACE_MODE_MII:
1329 case PHY_INTERFACE_MODE_INTERNAL:
1330 miicfg |= GSWIP_MII_CFG_MODE_MIIM;
1331 break;
1332 case PHY_INTERFACE_MODE_REVMII:
1333 miicfg |= GSWIP_MII_CFG_MODE_MIIP;
1334 break;
1335 case PHY_INTERFACE_MODE_RMII:
1336 miicfg |= GSWIP_MII_CFG_MODE_RMIIM;
1337 break;
1338 case PHY_INTERFACE_MODE_RGMII:
1339 case PHY_INTERFACE_MODE_RGMII_ID:
1340 case PHY_INTERFACE_MODE_RGMII_RXID:
1341 case PHY_INTERFACE_MODE_RGMII_TXID:
1342 miicfg |= GSWIP_MII_CFG_MODE_RGMII;
1343 break;
1344 default:
1345 dev_err(ds->dev,
1346 "Unsupported interface: %d\n", state->interface);
1347 return;
1348 }
1349 gswip_mii_mask_cfg(priv, GSWIP_MII_CFG_MODE_MASK, miicfg, port);
1350
1351 switch (state->interface) {
1352 case PHY_INTERFACE_MODE_RGMII_ID:
1353 gswip_mii_mask_pcdu(priv, GSWIP_MII_PCDU_TXDLY_MASK |
1354 GSWIP_MII_PCDU_RXDLY_MASK, 0, port);
1355 break;
1356 case PHY_INTERFACE_MODE_RGMII_RXID:
1357 gswip_mii_mask_pcdu(priv, GSWIP_MII_PCDU_RXDLY_MASK, 0, port);
1358 break;
1359 case PHY_INTERFACE_MODE_RGMII_TXID:
1360 gswip_mii_mask_pcdu(priv, GSWIP_MII_PCDU_TXDLY_MASK, 0, port);
1361 break;
1362 default:
1363 break;
1364 }
1365}
1366
1367static void gswip_phylink_mac_link_down(struct dsa_switch *ds, int port,
1368 unsigned int mode,
1369 phy_interface_t interface)
1370{
1371 struct gswip_priv *priv = ds->priv;
1372
1373 gswip_mii_mask_cfg(priv, GSWIP_MII_CFG_EN, 0, port);
1374}
1375
1376static void gswip_phylink_mac_link_up(struct dsa_switch *ds, int port,
1377 unsigned int mode,
1378 phy_interface_t interface,
1379 struct phy_device *phydev)
1380{
1381 struct gswip_priv *priv = ds->priv;
1382
1383 /* Enable the xMII interface only for the external PHY */
1384 if (interface != PHY_INTERFACE_MODE_INTERNAL)
1385 gswip_mii_mask_cfg(priv, 0, GSWIP_MII_CFG_EN, port);
1386}
1387
1388static void gswip_get_strings(struct dsa_switch *ds, int port, u32 stringset,
1389 uint8_t *data)
1390{
1391 int i;
1392
1393 if (stringset != ETH_SS_STATS)
1394 return;
1395
1396 for (i = 0; i < ARRAY_SIZE(gswip_rmon_cnt); i++)
1397 strncpy(data + i * ETH_GSTRING_LEN, gswip_rmon_cnt[i].name,
1398 ETH_GSTRING_LEN);
1399}
1400
1401static u32 gswip_bcm_ram_entry_read(struct gswip_priv *priv, u32 table,
1402 u32 index)
1403{
1404 u32 result;
1405 int err;
1406
1407 gswip_switch_w(priv, index, GSWIP_BM_RAM_ADDR);
1408 gswip_switch_mask(priv, GSWIP_BM_RAM_CTRL_ADDR_MASK |
1409 GSWIP_BM_RAM_CTRL_OPMOD,
1410 table | GSWIP_BM_RAM_CTRL_BAS,
1411 GSWIP_BM_RAM_CTRL);
1412
1413 err = gswip_switch_r_timeout(priv, GSWIP_BM_RAM_CTRL,
1414 GSWIP_BM_RAM_CTRL_BAS);
1415 if (err) {
1416 dev_err(priv->dev, "timeout while reading table: %u, index: %u",
1417 table, index);
1418 return 0;
1419 }
1420
1421 result = gswip_switch_r(priv, GSWIP_BM_RAM_VAL(0));
1422 result |= gswip_switch_r(priv, GSWIP_BM_RAM_VAL(1)) << 16;
1423
1424 return result;
1425}
1426
1427static void gswip_get_ethtool_stats(struct dsa_switch *ds, int port,
1428 uint64_t *data)
1429{
1430 struct gswip_priv *priv = ds->priv;
1431 const struct gswip_rmon_cnt_desc *rmon_cnt;
1432 int i;
1433 u64 high;
1434
1435 for (i = 0; i < ARRAY_SIZE(gswip_rmon_cnt); i++) {
1436 rmon_cnt = &gswip_rmon_cnt[i];
1437
1438 data[i] = gswip_bcm_ram_entry_read(priv, port,
1439 rmon_cnt->offset);
1440 if (rmon_cnt->size == 2) {
1441 high = gswip_bcm_ram_entry_read(priv, port,
1442 rmon_cnt->offset + 1);
1443 data[i] |= high << 32;
1444 }
1445 }
1446}
1447
1448static int gswip_get_sset_count(struct dsa_switch *ds, int port, int sset)
1449{
1450 if (sset != ETH_SS_STATS)
1451 return 0;
1452
1453 return ARRAY_SIZE(gswip_rmon_cnt);
1454}
1455
1456static const struct dsa_switch_ops gswip_switch_ops = {
1457 .get_tag_protocol = gswip_get_tag_protocol,
1458 .setup = gswip_setup,
1459 .port_enable = gswip_port_enable,
1460 .port_disable = gswip_port_disable,
Hauke Mehrtens8206e0c2019-05-06 00:25:07 +02001461 .port_bridge_join = gswip_port_bridge_join,
1462 .port_bridge_leave = gswip_port_bridge_leave,
Hauke Mehrtens9bbb1c02019-05-06 00:25:08 +02001463 .port_vlan_filtering = gswip_port_vlan_filtering,
1464 .port_vlan_prepare = gswip_port_vlan_prepare,
1465 .port_vlan_add = gswip_port_vlan_add,
1466 .port_vlan_del = gswip_port_vlan_del,
Hauke Mehrtens8206e0c2019-05-06 00:25:07 +02001467 .port_stp_state_set = gswip_port_stp_state_set,
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02001468 .phylink_validate = gswip_phylink_validate,
1469 .phylink_mac_config = gswip_phylink_mac_config,
1470 .phylink_mac_link_down = gswip_phylink_mac_link_down,
1471 .phylink_mac_link_up = gswip_phylink_mac_link_up,
1472 .get_strings = gswip_get_strings,
1473 .get_ethtool_stats = gswip_get_ethtool_stats,
1474 .get_sset_count = gswip_get_sset_count,
1475};
1476
1477static const struct xway_gphy_match_data xrx200a1x_gphy_data = {
1478 .fe_firmware_name = "lantiq/xrx200_phy22f_a14.bin",
1479 .ge_firmware_name = "lantiq/xrx200_phy11g_a14.bin",
1480};
1481
1482static const struct xway_gphy_match_data xrx200a2x_gphy_data = {
1483 .fe_firmware_name = "lantiq/xrx200_phy22f_a22.bin",
1484 .ge_firmware_name = "lantiq/xrx200_phy11g_a22.bin",
1485};
1486
1487static const struct xway_gphy_match_data xrx300_gphy_data = {
1488 .fe_firmware_name = "lantiq/xrx300_phy22f_a21.bin",
1489 .ge_firmware_name = "lantiq/xrx300_phy11g_a21.bin",
1490};
1491
1492static const struct of_device_id xway_gphy_match[] = {
1493 { .compatible = "lantiq,xrx200-gphy-fw", .data = NULL },
1494 { .compatible = "lantiq,xrx200a1x-gphy-fw", .data = &xrx200a1x_gphy_data },
1495 { .compatible = "lantiq,xrx200a2x-gphy-fw", .data = &xrx200a2x_gphy_data },
1496 { .compatible = "lantiq,xrx300-gphy-fw", .data = &xrx300_gphy_data },
1497 { .compatible = "lantiq,xrx330-gphy-fw", .data = &xrx300_gphy_data },
1498 {},
1499};
1500
1501static int gswip_gphy_fw_load(struct gswip_priv *priv, struct gswip_gphy_fw *gphy_fw)
1502{
1503 struct device *dev = priv->dev;
1504 const struct firmware *fw;
1505 void *fw_addr;
1506 dma_addr_t dma_addr;
1507 dma_addr_t dev_addr;
1508 size_t size;
1509 int ret;
1510
1511 ret = clk_prepare_enable(gphy_fw->clk_gate);
1512 if (ret)
1513 return ret;
1514
1515 reset_control_assert(gphy_fw->reset);
1516
1517 ret = request_firmware(&fw, gphy_fw->fw_name, dev);
1518 if (ret) {
1519 dev_err(dev, "failed to load firmware: %s, error: %i\n",
1520 gphy_fw->fw_name, ret);
1521 return ret;
1522 }
1523
1524 /* GPHY cores need the firmware code in a persistent and contiguous
1525 * memory area with a 16 kB boundary aligned start address.
1526 */
1527 size = fw->size + XRX200_GPHY_FW_ALIGN;
1528
1529 fw_addr = dmam_alloc_coherent(dev, size, &dma_addr, GFP_KERNEL);
1530 if (fw_addr) {
1531 fw_addr = PTR_ALIGN(fw_addr, XRX200_GPHY_FW_ALIGN);
1532 dev_addr = ALIGN(dma_addr, XRX200_GPHY_FW_ALIGN);
1533 memcpy(fw_addr, fw->data, fw->size);
1534 } else {
1535 dev_err(dev, "failed to alloc firmware memory\n");
1536 release_firmware(fw);
1537 return -ENOMEM;
1538 }
1539
1540 release_firmware(fw);
1541
1542 ret = regmap_write(priv->rcu_regmap, gphy_fw->fw_addr_offset, dev_addr);
1543 if (ret)
1544 return ret;
1545
1546 reset_control_deassert(gphy_fw->reset);
1547
1548 return ret;
1549}
1550
1551static int gswip_gphy_fw_probe(struct gswip_priv *priv,
1552 struct gswip_gphy_fw *gphy_fw,
1553 struct device_node *gphy_fw_np, int i)
1554{
1555 struct device *dev = priv->dev;
1556 u32 gphy_mode;
1557 int ret;
1558 char gphyname[10];
1559
1560 snprintf(gphyname, sizeof(gphyname), "gphy%d", i);
1561
1562 gphy_fw->clk_gate = devm_clk_get(dev, gphyname);
1563 if (IS_ERR(gphy_fw->clk_gate)) {
1564 dev_err(dev, "Failed to lookup gate clock\n");
1565 return PTR_ERR(gphy_fw->clk_gate);
1566 }
1567
1568 ret = of_property_read_u32(gphy_fw_np, "reg", &gphy_fw->fw_addr_offset);
1569 if (ret)
1570 return ret;
1571
1572 ret = of_property_read_u32(gphy_fw_np, "lantiq,gphy-mode", &gphy_mode);
1573 /* Default to GE mode */
1574 if (ret)
1575 gphy_mode = GPHY_MODE_GE;
1576
1577 switch (gphy_mode) {
1578 case GPHY_MODE_FE:
1579 gphy_fw->fw_name = priv->gphy_fw_name_cfg->fe_firmware_name;
1580 break;
1581 case GPHY_MODE_GE:
1582 gphy_fw->fw_name = priv->gphy_fw_name_cfg->ge_firmware_name;
1583 break;
1584 default:
1585 dev_err(dev, "Unknown GPHY mode %d\n", gphy_mode);
1586 return -EINVAL;
1587 }
1588
1589 gphy_fw->reset = of_reset_control_array_get_exclusive(gphy_fw_np);
Wei Yongjunf592e0b2018-09-15 01:33:38 +00001590 if (IS_ERR(gphy_fw->reset)) {
1591 if (PTR_ERR(gphy_fw->reset) != -EPROBE_DEFER)
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02001592 dev_err(dev, "Failed to lookup gphy reset\n");
Wei Yongjunf592e0b2018-09-15 01:33:38 +00001593 return PTR_ERR(gphy_fw->reset);
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02001594 }
1595
1596 return gswip_gphy_fw_load(priv, gphy_fw);
1597}
1598
1599static void gswip_gphy_fw_remove(struct gswip_priv *priv,
1600 struct gswip_gphy_fw *gphy_fw)
1601{
1602 int ret;
1603
1604 /* check if the device was fully probed */
1605 if (!gphy_fw->fw_name)
1606 return;
1607
1608 ret = regmap_write(priv->rcu_regmap, gphy_fw->fw_addr_offset, 0);
1609 if (ret)
1610 dev_err(priv->dev, "can not reset GPHY FW pointer");
1611
1612 clk_disable_unprepare(gphy_fw->clk_gate);
1613
1614 reset_control_put(gphy_fw->reset);
1615}
1616
1617static int gswip_gphy_fw_list(struct gswip_priv *priv,
1618 struct device_node *gphy_fw_list_np, u32 version)
1619{
1620 struct device *dev = priv->dev;
1621 struct device_node *gphy_fw_np;
1622 const struct of_device_id *match;
1623 int err;
1624 int i = 0;
1625
Hauke Mehrtens0e630b52018-09-15 14:08:48 +02001626 /* The VRX200 rev 1.1 uses the GSWIP 2.0 and needs the older
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02001627 * GPHY firmware. The VRX200 rev 1.2 uses the GSWIP 2.1 and also
1628 * needs a different GPHY firmware.
1629 */
1630 if (of_device_is_compatible(gphy_fw_list_np, "lantiq,xrx200-gphy-fw")) {
1631 switch (version) {
1632 case GSWIP_VERSION_2_0:
1633 priv->gphy_fw_name_cfg = &xrx200a1x_gphy_data;
1634 break;
1635 case GSWIP_VERSION_2_1:
1636 priv->gphy_fw_name_cfg = &xrx200a2x_gphy_data;
1637 break;
1638 default:
1639 dev_err(dev, "unknown GSWIP version: 0x%x", version);
1640 return -ENOENT;
1641 }
1642 }
1643
1644 match = of_match_node(xway_gphy_match, gphy_fw_list_np);
1645 if (match && match->data)
1646 priv->gphy_fw_name_cfg = match->data;
1647
1648 if (!priv->gphy_fw_name_cfg) {
1649 dev_err(dev, "GPHY compatible type not supported");
1650 return -ENOENT;
1651 }
1652
1653 priv->num_gphy_fw = of_get_available_child_count(gphy_fw_list_np);
1654 if (!priv->num_gphy_fw)
1655 return -ENOENT;
1656
1657 priv->rcu_regmap = syscon_regmap_lookup_by_phandle(gphy_fw_list_np,
1658 "lantiq,rcu");
1659 if (IS_ERR(priv->rcu_regmap))
1660 return PTR_ERR(priv->rcu_regmap);
1661
1662 priv->gphy_fw = devm_kmalloc_array(dev, priv->num_gphy_fw,
1663 sizeof(*priv->gphy_fw),
1664 GFP_KERNEL | __GFP_ZERO);
1665 if (!priv->gphy_fw)
1666 return -ENOMEM;
1667
1668 for_each_available_child_of_node(gphy_fw_list_np, gphy_fw_np) {
1669 err = gswip_gphy_fw_probe(priv, &priv->gphy_fw[i],
1670 gphy_fw_np, i);
1671 if (err)
1672 goto remove_gphy;
1673 i++;
1674 }
1675
1676 return 0;
1677
1678remove_gphy:
1679 for (i = 0; i < priv->num_gphy_fw; i++)
1680 gswip_gphy_fw_remove(priv, &priv->gphy_fw[i]);
1681 return err;
1682}
1683
1684static int gswip_probe(struct platform_device *pdev)
1685{
1686 struct gswip_priv *priv;
1687 struct resource *gswip_res, *mdio_res, *mii_res;
1688 struct device_node *mdio_np, *gphy_fw_np;
1689 struct device *dev = &pdev->dev;
1690 int err;
1691 int i;
1692 u32 version;
1693
1694 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
1695 if (!priv)
1696 return -ENOMEM;
1697
1698 gswip_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1699 priv->gswip = devm_ioremap_resource(dev, gswip_res);
Wei Yongjunf5de8bf2018-09-15 01:33:21 +00001700 if (IS_ERR(priv->gswip))
1701 return PTR_ERR(priv->gswip);
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02001702
1703 mdio_res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1704 priv->mdio = devm_ioremap_resource(dev, mdio_res);
Wei Yongjunf5de8bf2018-09-15 01:33:21 +00001705 if (IS_ERR(priv->mdio))
1706 return PTR_ERR(priv->mdio);
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02001707
1708 mii_res = platform_get_resource(pdev, IORESOURCE_MEM, 2);
1709 priv->mii = devm_ioremap_resource(dev, mii_res);
Wei Yongjunf5de8bf2018-09-15 01:33:21 +00001710 if (IS_ERR(priv->mii))
1711 return PTR_ERR(priv->mii);
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02001712
1713 priv->hw_info = of_device_get_match_data(dev);
1714 if (!priv->hw_info)
1715 return -EINVAL;
1716
1717 priv->ds = dsa_switch_alloc(dev, priv->hw_info->max_ports);
1718 if (!priv->ds)
1719 return -ENOMEM;
1720
1721 priv->ds->priv = priv;
1722 priv->ds->ops = &gswip_switch_ops;
1723 priv->dev = dev;
1724 version = gswip_switch_r(priv, GSWIP_VERSION);
1725
1726 /* bring up the mdio bus */
Johan Hovoldc8cbcb02019-01-16 11:23:34 +01001727 gphy_fw_np = of_get_compatible_child(dev->of_node, "lantiq,gphy-fw");
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02001728 if (gphy_fw_np) {
1729 err = gswip_gphy_fw_list(priv, gphy_fw_np, version);
Johan Hovoldc8cbcb02019-01-16 11:23:34 +01001730 of_node_put(gphy_fw_np);
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02001731 if (err) {
1732 dev_err(dev, "gphy fw probe failed\n");
1733 return err;
1734 }
1735 }
1736
1737 /* bring up the mdio bus */
Johan Hovoldc8cbcb02019-01-16 11:23:34 +01001738 mdio_np = of_get_compatible_child(dev->of_node, "lantiq,xrx200-mdio");
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02001739 if (mdio_np) {
1740 err = gswip_mdio(priv, mdio_np);
1741 if (err) {
1742 dev_err(dev, "mdio probe failed\n");
Johan Hovoldc8cbcb02019-01-16 11:23:34 +01001743 goto put_mdio_node;
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02001744 }
1745 }
1746
1747 err = dsa_register_switch(priv->ds);
1748 if (err) {
1749 dev_err(dev, "dsa switch register failed: %i\n", err);
1750 goto mdio_bus;
1751 }
Hauke Mehrtens0e630b52018-09-15 14:08:48 +02001752 if (!dsa_is_cpu_port(priv->ds, priv->hw_info->cpu_port)) {
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02001753 dev_err(dev, "wrong CPU port defined, HW only supports port: %i",
1754 priv->hw_info->cpu_port);
1755 err = -EINVAL;
Johan Hovoldaed13f22019-01-16 11:23:33 +01001756 goto disable_switch;
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02001757 }
1758
1759 platform_set_drvdata(pdev, priv);
1760
1761 dev_info(dev, "probed GSWIP version %lx mod %lx\n",
1762 (version & GSWIP_VERSION_REV_MASK) >> GSWIP_VERSION_REV_SHIFT,
1763 (version & GSWIP_VERSION_MOD_MASK) >> GSWIP_VERSION_MOD_SHIFT);
1764 return 0;
1765
Johan Hovoldaed13f22019-01-16 11:23:33 +01001766disable_switch:
1767 gswip_mdio_mask(priv, GSWIP_MDIO_GLOB_ENABLE, 0, GSWIP_MDIO_GLOB);
1768 dsa_unregister_switch(priv->ds);
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02001769mdio_bus:
1770 if (mdio_np)
1771 mdiobus_unregister(priv->ds->slave_mii_bus);
Johan Hovoldc8cbcb02019-01-16 11:23:34 +01001772put_mdio_node:
1773 of_node_put(mdio_np);
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02001774 for (i = 0; i < priv->num_gphy_fw; i++)
1775 gswip_gphy_fw_remove(priv, &priv->gphy_fw[i]);
1776 return err;
1777}
1778
1779static int gswip_remove(struct platform_device *pdev)
1780{
1781 struct gswip_priv *priv = platform_get_drvdata(pdev);
1782 int i;
1783
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02001784 /* disable the switch */
1785 gswip_mdio_mask(priv, GSWIP_MDIO_GLOB_ENABLE, 0, GSWIP_MDIO_GLOB);
1786
1787 dsa_unregister_switch(priv->ds);
1788
Johan Hovoldc8cbcb02019-01-16 11:23:34 +01001789 if (priv->ds->slave_mii_bus) {
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02001790 mdiobus_unregister(priv->ds->slave_mii_bus);
Johan Hovoldc8cbcb02019-01-16 11:23:34 +01001791 of_node_put(priv->ds->slave_mii_bus->dev.of_node);
1792 }
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02001793
1794 for (i = 0; i < priv->num_gphy_fw; i++)
1795 gswip_gphy_fw_remove(priv, &priv->gphy_fw[i]);
1796
1797 return 0;
1798}
1799
1800static const struct gswip_hw_info gswip_xrx200 = {
1801 .max_ports = 7,
1802 .cpu_port = 6,
1803};
1804
1805static const struct of_device_id gswip_of_match[] = {
1806 { .compatible = "lantiq,xrx200-gswip", .data = &gswip_xrx200 },
1807 {},
1808};
1809MODULE_DEVICE_TABLE(of, gswip_of_match);
1810
1811static struct platform_driver gswip_driver = {
1812 .probe = gswip_probe,
1813 .remove = gswip_remove,
1814 .driver = {
1815 .name = "gswip",
1816 .of_match_table = gswip_of_match,
1817 },
1818};
1819
1820module_platform_driver(gswip_driver);
1821
Hauke Mehrtenscffde202019-02-22 20:11:13 +01001822MODULE_FIRMWARE("lantiq/xrx300_phy11g_a21.bin");
1823MODULE_FIRMWARE("lantiq/xrx300_phy22f_a21.bin");
1824MODULE_FIRMWARE("lantiq/xrx200_phy11g_a14.bin");
1825MODULE_FIRMWARE("lantiq/xrx200_phy11g_a22.bin");
1826MODULE_FIRMWARE("lantiq/xrx200_phy22f_a14.bin");
1827MODULE_FIRMWARE("lantiq/xrx200_phy22f_a22.bin");
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02001828MODULE_AUTHOR("Hauke Mehrtens <hauke@hauke-m.de>");
1829MODULE_DESCRIPTION("Lantiq / Intel GSWIP driver");
1830MODULE_LICENSE("GPL v2");