blob: 0a2259cb09df740a94509e08bebc35c4f9d8b26c [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>
7 * Copyright (C) 2017 - 2018 Hauke Mehrtens <hauke@hauke-m.de>
8 */
9
10#include <linux/clk.h>
11#include <linux/etherdevice.h>
12#include <linux/firmware.h>
13#include <linux/if_bridge.h>
14#include <linux/if_vlan.h>
15#include <linux/iopoll.h>
16#include <linux/mfd/syscon.h>
17#include <linux/module.h>
18#include <linux/of_mdio.h>
19#include <linux/of_net.h>
20#include <linux/of_platform.h>
21#include <linux/phy.h>
22#include <linux/phylink.h>
23#include <linux/platform_device.h>
24#include <linux/regmap.h>
25#include <linux/reset.h>
26#include <net/dsa.h>
27#include <dt-bindings/mips/lantiq_rcu_gphy.h>
28
29#include "lantiq_pce.h"
30
31/* GSWIP MDIO Registers */
32#define GSWIP_MDIO_GLOB 0x00
33#define GSWIP_MDIO_GLOB_ENABLE BIT(15)
34#define GSWIP_MDIO_CTRL 0x08
35#define GSWIP_MDIO_CTRL_BUSY BIT(12)
36#define GSWIP_MDIO_CTRL_RD BIT(11)
37#define GSWIP_MDIO_CTRL_WR BIT(10)
38#define GSWIP_MDIO_CTRL_PHYAD_MASK 0x1f
39#define GSWIP_MDIO_CTRL_PHYAD_SHIFT 5
40#define GSWIP_MDIO_CTRL_REGAD_MASK 0x1f
41#define GSWIP_MDIO_READ 0x09
42#define GSWIP_MDIO_WRITE 0x0A
43#define GSWIP_MDIO_MDC_CFG0 0x0B
44#define GSWIP_MDIO_MDC_CFG1 0x0C
45#define GSWIP_MDIO_PHYp(p) (0x15 - (p))
46#define GSWIP_MDIO_PHY_LINK_MASK 0x6000
47#define GSWIP_MDIO_PHY_LINK_AUTO 0x0000
48#define GSWIP_MDIO_PHY_LINK_DOWN 0x4000
49#define GSWIP_MDIO_PHY_LINK_UP 0x2000
50#define GSWIP_MDIO_PHY_SPEED_MASK 0x1800
51#define GSWIP_MDIO_PHY_SPEED_AUTO 0x1800
52#define GSWIP_MDIO_PHY_SPEED_M10 0x0000
53#define GSWIP_MDIO_PHY_SPEED_M100 0x0800
54#define GSWIP_MDIO_PHY_SPEED_G1 0x1000
55#define GSWIP_MDIO_PHY_FDUP_MASK 0x0600
56#define GSWIP_MDIO_PHY_FDUP_AUTO 0x0000
57#define GSWIP_MDIO_PHY_FDUP_EN 0x0200
58#define GSWIP_MDIO_PHY_FDUP_DIS 0x0600
59#define GSWIP_MDIO_PHY_FCONTX_MASK 0x0180
60#define GSWIP_MDIO_PHY_FCONTX_AUTO 0x0000
61#define GSWIP_MDIO_PHY_FCONTX_EN 0x0100
62#define GSWIP_MDIO_PHY_FCONTX_DIS 0x0180
63#define GSWIP_MDIO_PHY_FCONRX_MASK 0x0060
64#define GSWIP_MDIO_PHY_FCONRX_AUTO 0x0000
65#define GSWIP_MDIO_PHY_FCONRX_EN 0x0020
66#define GSWIP_MDIO_PHY_FCONRX_DIS 0x0060
67#define GSWIP_MDIO_PHY_ADDR_MASK 0x001f
68#define GSWIP_MDIO_PHY_MASK (GSWIP_MDIO_PHY_ADDR_MASK | \
69 GSWIP_MDIO_PHY_FCONRX_MASK | \
70 GSWIP_MDIO_PHY_FCONTX_MASK | \
71 GSWIP_MDIO_PHY_LINK_MASK | \
72 GSWIP_MDIO_PHY_SPEED_MASK | \
73 GSWIP_MDIO_PHY_FDUP_MASK)
74
75/* GSWIP MII Registers */
76#define GSWIP_MII_CFG0 0x00
77#define GSWIP_MII_CFG1 0x02
78#define GSWIP_MII_CFG5 0x04
79#define GSWIP_MII_CFG_EN BIT(14)
80#define GSWIP_MII_CFG_LDCLKDIS BIT(12)
81#define GSWIP_MII_CFG_MODE_MIIP 0x0
82#define GSWIP_MII_CFG_MODE_MIIM 0x1
83#define GSWIP_MII_CFG_MODE_RMIIP 0x2
84#define GSWIP_MII_CFG_MODE_RMIIM 0x3
85#define GSWIP_MII_CFG_MODE_RGMII 0x4
86#define GSWIP_MII_CFG_MODE_MASK 0xf
87#define GSWIP_MII_CFG_RATE_M2P5 0x00
88#define GSWIP_MII_CFG_RATE_M25 0x10
89#define GSWIP_MII_CFG_RATE_M125 0x20
90#define GSWIP_MII_CFG_RATE_M50 0x30
91#define GSWIP_MII_CFG_RATE_AUTO 0x40
92#define GSWIP_MII_CFG_RATE_MASK 0x70
93#define GSWIP_MII_PCDU0 0x01
94#define GSWIP_MII_PCDU1 0x03
95#define GSWIP_MII_PCDU5 0x05
96#define GSWIP_MII_PCDU_TXDLY_MASK GENMASK(2, 0)
97#define GSWIP_MII_PCDU_RXDLY_MASK GENMASK(9, 7)
98
99/* GSWIP Core Registers */
100#define GSWIP_SWRES 0x000
101#define GSWIP_SWRES_R1 BIT(1) /* GSWIP Software reset */
102#define GSWIP_SWRES_R0 BIT(0) /* GSWIP Hardware reset */
103#define GSWIP_VERSION 0x013
104#define GSWIP_VERSION_REV_SHIFT 0
105#define GSWIP_VERSION_REV_MASK GENMASK(7, 0)
106#define GSWIP_VERSION_MOD_SHIFT 8
107#define GSWIP_VERSION_MOD_MASK GENMASK(15, 8)
108#define GSWIP_VERSION_2_0 0x100
109#define GSWIP_VERSION_2_1 0x021
110#define GSWIP_VERSION_2_2 0x122
111#define GSWIP_VERSION_2_2_ETC 0x022
112
113#define GSWIP_BM_RAM_VAL(x) (0x043 - (x))
114#define GSWIP_BM_RAM_ADDR 0x044
115#define GSWIP_BM_RAM_CTRL 0x045
116#define GSWIP_BM_RAM_CTRL_BAS BIT(15)
117#define GSWIP_BM_RAM_CTRL_OPMOD BIT(5)
118#define GSWIP_BM_RAM_CTRL_ADDR_MASK GENMASK(4, 0)
119#define GSWIP_BM_QUEUE_GCTRL 0x04A
120#define GSWIP_BM_QUEUE_GCTRL_GL_MOD BIT(10)
121/* buffer management Port Configuration Register */
122#define GSWIP_BM_PCFGp(p) (0x080 + ((p) * 2))
123#define GSWIP_BM_PCFG_CNTEN BIT(0) /* RMON Counter Enable */
124#define GSWIP_BM_PCFG_IGCNT BIT(1) /* Ingres Special Tag RMON count */
125/* buffer management Port Control Register */
126#define GSWIP_BM_RMON_CTRLp(p) (0x81 + ((p) * 2))
127#define GSWIP_BM_CTRL_RMON_RAM1_RES BIT(0) /* Software Reset for RMON RAM 1 */
128#define GSWIP_BM_CTRL_RMON_RAM2_RES BIT(1) /* Software Reset for RMON RAM 2 */
129
130/* PCE */
131#define GSWIP_PCE_TBL_KEY(x) (0x447 - (x))
132#define GSWIP_PCE_TBL_MASK 0x448
133#define GSWIP_PCE_TBL_VAL(x) (0x44D - (x))
134#define GSWIP_PCE_TBL_ADDR 0x44E
135#define GSWIP_PCE_TBL_CTRL 0x44F
136#define GSWIP_PCE_TBL_CTRL_BAS BIT(15)
137#define GSWIP_PCE_TBL_CTRL_TYPE BIT(13)
138#define GSWIP_PCE_TBL_CTRL_VLD BIT(12)
139#define GSWIP_PCE_TBL_CTRL_KEYFORM BIT(11)
140#define GSWIP_PCE_TBL_CTRL_GMAP_MASK GENMASK(10, 7)
141#define GSWIP_PCE_TBL_CTRL_OPMOD_MASK GENMASK(6, 5)
142#define GSWIP_PCE_TBL_CTRL_OPMOD_ADRD 0x00
143#define GSWIP_PCE_TBL_CTRL_OPMOD_ADWR 0x20
144#define GSWIP_PCE_TBL_CTRL_OPMOD_KSRD 0x40
145#define GSWIP_PCE_TBL_CTRL_OPMOD_KSWR 0x60
146#define GSWIP_PCE_TBL_CTRL_ADDR_MASK GENMASK(4, 0)
147#define GSWIP_PCE_PMAP1 0x453 /* Monitoring port map */
148#define GSWIP_PCE_PMAP2 0x454 /* Default Multicast port map */
149#define GSWIP_PCE_PMAP3 0x455 /* Default Unknown Unicast port map */
150#define GSWIP_PCE_GCTRL_0 0x456
151#define GSWIP_PCE_GCTRL_0_MC_VALID BIT(3)
152#define GSWIP_PCE_GCTRL_0_VLAN BIT(14) /* VLAN aware Switching */
153#define GSWIP_PCE_GCTRL_1 0x457
154#define GSWIP_PCE_GCTRL_1_MAC_GLOCK BIT(2) /* MAC Address table lock */
155#define GSWIP_PCE_GCTRL_1_MAC_GLOCK_MOD BIT(3) /* Mac address table lock forwarding mode */
156#define GSWIP_PCE_PCTRL_0p(p) (0x480 + ((p) * 0xA))
157#define GSWIP_PCE_PCTRL_0_INGRESS BIT(11)
158#define GSWIP_PCE_PCTRL_0_PSTATE_LISTEN 0x0
159#define GSWIP_PCE_PCTRL_0_PSTATE_RX 0x1
160#define GSWIP_PCE_PCTRL_0_PSTATE_TX 0x2
161#define GSWIP_PCE_PCTRL_0_PSTATE_LEARNING 0x3
162#define GSWIP_PCE_PCTRL_0_PSTATE_FORWARDING 0x7
163#define GSWIP_PCE_PCTRL_0_PSTATE_MASK GENMASK(2, 0)
164
165#define GSWIP_MAC_FLEN 0x8C5
166#define GSWIP_MAC_CTRL_2p(p) (0x905 + ((p) * 0xC))
167#define GSWIP_MAC_CTRL_2_MLEN BIT(3) /* Maximum Untagged Frame Lnegth */
168
169/* Ethernet Switch Fetch DMA Port Control Register */
170#define GSWIP_FDMA_PCTRLp(p) (0xA80 + ((p) * 0x6))
171#define GSWIP_FDMA_PCTRL_EN BIT(0) /* FDMA Port Enable */
172#define GSWIP_FDMA_PCTRL_STEN BIT(1) /* Special Tag Insertion Enable */
173#define GSWIP_FDMA_PCTRL_VLANMOD_MASK GENMASK(4, 3) /* VLAN Modification Control */
174#define GSWIP_FDMA_PCTRL_VLANMOD_SHIFT 3 /* VLAN Modification Control */
175#define GSWIP_FDMA_PCTRL_VLANMOD_DIS (0x0 << GSWIP_FDMA_PCTRL_VLANMOD_SHIFT)
176#define GSWIP_FDMA_PCTRL_VLANMOD_PRIO (0x1 << GSWIP_FDMA_PCTRL_VLANMOD_SHIFT)
177#define GSWIP_FDMA_PCTRL_VLANMOD_ID (0x2 << GSWIP_FDMA_PCTRL_VLANMOD_SHIFT)
178#define GSWIP_FDMA_PCTRL_VLANMOD_BOTH (0x3 << GSWIP_FDMA_PCTRL_VLANMOD_SHIFT)
179
180/* Ethernet Switch Store DMA Port Control Register */
181#define GSWIP_SDMA_PCTRLp(p) (0xBC0 + ((p) * 0x6))
182#define GSWIP_SDMA_PCTRL_EN BIT(0) /* SDMA Port Enable */
183#define GSWIP_SDMA_PCTRL_FCEN BIT(1) /* Flow Control Enable */
184#define GSWIP_SDMA_PCTRL_PAUFWD BIT(1) /* Pause Frame Forwarding */
185
186#define XRX200_GPHY_FW_ALIGN (16 * 1024)
187
188struct gswip_hw_info {
189 int max_ports;
190 int cpu_port;
191};
192
193struct xway_gphy_match_data {
194 char *fe_firmware_name;
195 char *ge_firmware_name;
196};
197
198struct gswip_gphy_fw {
199 struct clk *clk_gate;
200 struct reset_control *reset;
201 u32 fw_addr_offset;
202 char *fw_name;
203};
204
205struct gswip_priv {
206 __iomem void *gswip;
207 __iomem void *mdio;
208 __iomem void *mii;
209 const struct gswip_hw_info *hw_info;
210 const struct xway_gphy_match_data *gphy_fw_name_cfg;
211 struct dsa_switch *ds;
212 struct device *dev;
213 struct regmap *rcu_regmap;
214 int num_gphy_fw;
215 struct gswip_gphy_fw *gphy_fw;
216};
217
218struct gswip_rmon_cnt_desc {
219 unsigned int size;
220 unsigned int offset;
221 const char *name;
222};
223
224#define MIB_DESC(_size, _offset, _name) {.size = _size, .offset = _offset, .name = _name}
225
226static const struct gswip_rmon_cnt_desc gswip_rmon_cnt[] = {
227 /** Receive Packet Count (only packets that are accepted and not discarded). */
228 MIB_DESC(1, 0x1F, "RxGoodPkts"),
229 MIB_DESC(1, 0x23, "RxUnicastPkts"),
230 MIB_DESC(1, 0x22, "RxMulticastPkts"),
231 MIB_DESC(1, 0x21, "RxFCSErrorPkts"),
232 MIB_DESC(1, 0x1D, "RxUnderSizeGoodPkts"),
233 MIB_DESC(1, 0x1E, "RxUnderSizeErrorPkts"),
234 MIB_DESC(1, 0x1B, "RxOversizeGoodPkts"),
235 MIB_DESC(1, 0x1C, "RxOversizeErrorPkts"),
236 MIB_DESC(1, 0x20, "RxGoodPausePkts"),
237 MIB_DESC(1, 0x1A, "RxAlignErrorPkts"),
238 MIB_DESC(1, 0x12, "Rx64BytePkts"),
239 MIB_DESC(1, 0x13, "Rx127BytePkts"),
240 MIB_DESC(1, 0x14, "Rx255BytePkts"),
241 MIB_DESC(1, 0x15, "Rx511BytePkts"),
242 MIB_DESC(1, 0x16, "Rx1023BytePkts"),
243 /** Receive Size 1024-1522 (or more, if configured) Packet Count. */
244 MIB_DESC(1, 0x17, "RxMaxBytePkts"),
245 MIB_DESC(1, 0x18, "RxDroppedPkts"),
246 MIB_DESC(1, 0x19, "RxFilteredPkts"),
247 MIB_DESC(2, 0x24, "RxGoodBytes"),
248 MIB_DESC(2, 0x26, "RxBadBytes"),
249 MIB_DESC(1, 0x11, "TxAcmDroppedPkts"),
250 MIB_DESC(1, 0x0C, "TxGoodPkts"),
251 MIB_DESC(1, 0x06, "TxUnicastPkts"),
252 MIB_DESC(1, 0x07, "TxMulticastPkts"),
253 MIB_DESC(1, 0x00, "Tx64BytePkts"),
254 MIB_DESC(1, 0x01, "Tx127BytePkts"),
255 MIB_DESC(1, 0x02, "Tx255BytePkts"),
256 MIB_DESC(1, 0x03, "Tx511BytePkts"),
257 MIB_DESC(1, 0x04, "Tx1023BytePkts"),
258 /** Transmit Size 1024-1522 (or more, if configured) Packet Count. */
259 MIB_DESC(1, 0x05, "TxMaxBytePkts"),
260 MIB_DESC(1, 0x08, "TxSingleCollCount"),
261 MIB_DESC(1, 0x09, "TxMultCollCount"),
262 MIB_DESC(1, 0x0A, "TxLateCollCount"),
263 MIB_DESC(1, 0x0B, "TxExcessCollCount"),
264 MIB_DESC(1, 0x0D, "TxPauseCount"),
265 MIB_DESC(1, 0x10, "TxDroppedPkts"),
266 MIB_DESC(2, 0x0E, "TxGoodBytes"),
267};
268
269static u32 gswip_switch_r(struct gswip_priv *priv, u32 offset)
270{
271 return __raw_readl(priv->gswip + (offset * 4));
272}
273
274static void gswip_switch_w(struct gswip_priv *priv, u32 val, u32 offset)
275{
276 __raw_writel(val, priv->gswip + (offset * 4));
277}
278
279static void gswip_switch_mask(struct gswip_priv *priv, u32 clear, u32 set,
280 u32 offset)
281{
282 u32 val = gswip_switch_r(priv, offset);
283
284 val &= ~(clear);
285 val |= set;
286 gswip_switch_w(priv, val, offset);
287}
288
289static u32 gswip_switch_r_timeout(struct gswip_priv *priv, u32 offset,
290 u32 cleared)
291{
292 u32 val;
293
294 return readx_poll_timeout(__raw_readl, priv->gswip + (offset * 4), val,
295 (val & cleared) == 0, 20, 50000);
296}
297
298static u32 gswip_mdio_r(struct gswip_priv *priv, u32 offset)
299{
300 return __raw_readl(priv->mdio + (offset * 4));
301}
302
303static void gswip_mdio_w(struct gswip_priv *priv, u32 val, u32 offset)
304{
305 __raw_writel(val, priv->mdio + (offset * 4));
306}
307
308static void gswip_mdio_mask(struct gswip_priv *priv, u32 clear, u32 set,
309 u32 offset)
310{
311 u32 val = gswip_mdio_r(priv, offset);
312
313 val &= ~(clear);
314 val |= set;
315 gswip_mdio_w(priv, val, offset);
316}
317
318static u32 gswip_mii_r(struct gswip_priv *priv, u32 offset)
319{
320 return __raw_readl(priv->mii + (offset * 4));
321}
322
323static void gswip_mii_w(struct gswip_priv *priv, u32 val, u32 offset)
324{
325 __raw_writel(val, priv->mii + (offset * 4));
326}
327
328static void gswip_mii_mask(struct gswip_priv *priv, u32 clear, u32 set,
329 u32 offset)
330{
331 u32 val = gswip_mii_r(priv, offset);
332
333 val &= ~(clear);
334 val |= set;
335 gswip_mii_w(priv, val, offset);
336}
337
338static void gswip_mii_mask_cfg(struct gswip_priv *priv, u32 clear, u32 set,
339 int port)
340{
341 switch (port) {
342 case 0:
343 gswip_mii_mask(priv, clear, set, GSWIP_MII_CFG0);
344 break;
345 case 1:
346 gswip_mii_mask(priv, clear, set, GSWIP_MII_CFG1);
347 break;
348 case 5:
349 gswip_mii_mask(priv, clear, set, GSWIP_MII_CFG5);
350 break;
351 }
352}
353
354static void gswip_mii_mask_pcdu(struct gswip_priv *priv, u32 clear, u32 set,
355 int port)
356{
357 switch (port) {
358 case 0:
359 gswip_mii_mask(priv, clear, set, GSWIP_MII_PCDU0);
360 break;
361 case 1:
362 gswip_mii_mask(priv, clear, set, GSWIP_MII_PCDU1);
363 break;
364 case 5:
365 gswip_mii_mask(priv, clear, set, GSWIP_MII_PCDU5);
366 break;
367 }
368}
369
370static int gswip_mdio_poll(struct gswip_priv *priv)
371{
372 int cnt = 100;
373
374 while (likely(cnt--)) {
375 u32 ctrl = gswip_mdio_r(priv, GSWIP_MDIO_CTRL);
376
377 if ((ctrl & GSWIP_MDIO_CTRL_BUSY) == 0)
378 return 0;
379 usleep_range(20, 40);
380 }
381
382 return -ETIMEDOUT;
383}
384
385static int gswip_mdio_wr(struct mii_bus *bus, int addr, int reg, u16 val)
386{
387 struct gswip_priv *priv = bus->priv;
388 int err;
389
390 err = gswip_mdio_poll(priv);
391 if (err) {
392 dev_err(&bus->dev, "waiting for MDIO bus busy timed out\n");
393 return err;
394 }
395
396 gswip_mdio_w(priv, val, GSWIP_MDIO_WRITE);
397 gswip_mdio_w(priv, GSWIP_MDIO_CTRL_BUSY | GSWIP_MDIO_CTRL_WR |
398 ((addr & GSWIP_MDIO_CTRL_PHYAD_MASK) << GSWIP_MDIO_CTRL_PHYAD_SHIFT) |
399 (reg & GSWIP_MDIO_CTRL_REGAD_MASK),
400 GSWIP_MDIO_CTRL);
401
402 return 0;
403}
404
405static int gswip_mdio_rd(struct mii_bus *bus, int addr, int reg)
406{
407 struct gswip_priv *priv = bus->priv;
408 int err;
409
410 err = gswip_mdio_poll(priv);
411 if (err) {
412 dev_err(&bus->dev, "waiting for MDIO bus busy timed out\n");
413 return err;
414 }
415
416 gswip_mdio_w(priv, GSWIP_MDIO_CTRL_BUSY | GSWIP_MDIO_CTRL_RD |
417 ((addr & GSWIP_MDIO_CTRL_PHYAD_MASK) << GSWIP_MDIO_CTRL_PHYAD_SHIFT) |
418 (reg & GSWIP_MDIO_CTRL_REGAD_MASK),
419 GSWIP_MDIO_CTRL);
420
421 err = gswip_mdio_poll(priv);
422 if (err) {
423 dev_err(&bus->dev, "waiting for MDIO bus busy timed out\n");
424 return err;
425 }
426
427 return gswip_mdio_r(priv, GSWIP_MDIO_READ);
428}
429
430static int gswip_mdio(struct gswip_priv *priv, struct device_node *mdio_np)
431{
432 struct dsa_switch *ds = priv->ds;
433
434 ds->slave_mii_bus = devm_mdiobus_alloc(priv->dev);
435 if (!ds->slave_mii_bus)
436 return -ENOMEM;
437
438 ds->slave_mii_bus->priv = priv;
439 ds->slave_mii_bus->read = gswip_mdio_rd;
440 ds->slave_mii_bus->write = gswip_mdio_wr;
441 ds->slave_mii_bus->name = "lantiq,xrx200-mdio";
442 snprintf(ds->slave_mii_bus->id, MII_BUS_ID_SIZE, "%s-mii",
443 dev_name(priv->dev));
444 ds->slave_mii_bus->parent = priv->dev;
445 ds->slave_mii_bus->phy_mask = ~ds->phys_mii_mask;
446
447 return of_mdiobus_register(ds->slave_mii_bus, mdio_np);
448}
449
450static int gswip_port_enable(struct dsa_switch *ds, int port,
451 struct phy_device *phydev)
452{
453 struct gswip_priv *priv = ds->priv;
454
455 /* RMON Counter Enable for port */
456 gswip_switch_w(priv, GSWIP_BM_PCFG_CNTEN, GSWIP_BM_PCFGp(port));
457
458 /* enable port fetch/store dma & VLAN Modification */
459 gswip_switch_mask(priv, 0, GSWIP_FDMA_PCTRL_EN |
460 GSWIP_FDMA_PCTRL_VLANMOD_BOTH,
461 GSWIP_FDMA_PCTRLp(port));
462 gswip_switch_mask(priv, 0, GSWIP_SDMA_PCTRL_EN,
463 GSWIP_SDMA_PCTRLp(port));
Hauke Mehrtens14fceff2018-09-09 22:20:39 +0200464
465 if (!dsa_is_cpu_port(ds, port)) {
466 u32 macconf = GSWIP_MDIO_PHY_LINK_AUTO |
467 GSWIP_MDIO_PHY_SPEED_AUTO |
468 GSWIP_MDIO_PHY_FDUP_AUTO |
469 GSWIP_MDIO_PHY_FCONTX_AUTO |
470 GSWIP_MDIO_PHY_FCONRX_AUTO |
471 (phydev->mdio.addr & GSWIP_MDIO_PHY_ADDR_MASK);
472
473 gswip_mdio_w(priv, macconf, GSWIP_MDIO_PHYp(port));
474 /* Activate MDIO auto polling */
475 gswip_mdio_mask(priv, 0, BIT(port), GSWIP_MDIO_MDC_CFG0);
476 }
477
478 return 0;
479}
480
Andrew Lunn75104db2019-02-24 20:44:43 +0100481static void gswip_port_disable(struct dsa_switch *ds, int port)
Hauke Mehrtens14fceff2018-09-09 22:20:39 +0200482{
483 struct gswip_priv *priv = ds->priv;
484
485 if (!dsa_is_cpu_port(ds, port)) {
486 gswip_mdio_mask(priv, GSWIP_MDIO_PHY_LINK_DOWN,
487 GSWIP_MDIO_PHY_LINK_MASK,
488 GSWIP_MDIO_PHYp(port));
489 /* Deactivate MDIO auto polling */
490 gswip_mdio_mask(priv, BIT(port), 0, GSWIP_MDIO_MDC_CFG0);
491 }
492
493 gswip_switch_mask(priv, GSWIP_FDMA_PCTRL_EN, 0,
494 GSWIP_FDMA_PCTRLp(port));
495 gswip_switch_mask(priv, GSWIP_SDMA_PCTRL_EN, 0,
496 GSWIP_SDMA_PCTRLp(port));
497}
498
499static int gswip_pce_load_microcode(struct gswip_priv *priv)
500{
501 int i;
502 int err;
503
504 gswip_switch_mask(priv, GSWIP_PCE_TBL_CTRL_ADDR_MASK |
505 GSWIP_PCE_TBL_CTRL_OPMOD_MASK,
506 GSWIP_PCE_TBL_CTRL_OPMOD_ADWR, GSWIP_PCE_TBL_CTRL);
507 gswip_switch_w(priv, 0, GSWIP_PCE_TBL_MASK);
508
509 for (i = 0; i < ARRAY_SIZE(gswip_pce_microcode); i++) {
510 gswip_switch_w(priv, i, GSWIP_PCE_TBL_ADDR);
511 gswip_switch_w(priv, gswip_pce_microcode[i].val_0,
512 GSWIP_PCE_TBL_VAL(0));
513 gswip_switch_w(priv, gswip_pce_microcode[i].val_1,
514 GSWIP_PCE_TBL_VAL(1));
515 gswip_switch_w(priv, gswip_pce_microcode[i].val_2,
516 GSWIP_PCE_TBL_VAL(2));
517 gswip_switch_w(priv, gswip_pce_microcode[i].val_3,
518 GSWIP_PCE_TBL_VAL(3));
519
520 /* start the table access: */
521 gswip_switch_mask(priv, 0, GSWIP_PCE_TBL_CTRL_BAS,
522 GSWIP_PCE_TBL_CTRL);
523 err = gswip_switch_r_timeout(priv, GSWIP_PCE_TBL_CTRL,
524 GSWIP_PCE_TBL_CTRL_BAS);
525 if (err)
526 return err;
527 }
528
529 /* tell the switch that the microcode is loaded */
530 gswip_switch_mask(priv, 0, GSWIP_PCE_GCTRL_0_MC_VALID,
531 GSWIP_PCE_GCTRL_0);
532
533 return 0;
534}
535
536static int gswip_setup(struct dsa_switch *ds)
537{
538 struct gswip_priv *priv = ds->priv;
539 unsigned int cpu_port = priv->hw_info->cpu_port;
540 int i;
541 int err;
542
543 gswip_switch_w(priv, GSWIP_SWRES_R0, GSWIP_SWRES);
544 usleep_range(5000, 10000);
545 gswip_switch_w(priv, 0, GSWIP_SWRES);
546
547 /* disable port fetch/store dma on all ports */
548 for (i = 0; i < priv->hw_info->max_ports; i++)
Andrew Lunn75104db2019-02-24 20:44:43 +0100549 gswip_port_disable(ds, i);
Hauke Mehrtens14fceff2018-09-09 22:20:39 +0200550
551 /* enable Switch */
552 gswip_mdio_mask(priv, 0, GSWIP_MDIO_GLOB_ENABLE, GSWIP_MDIO_GLOB);
553
554 err = gswip_pce_load_microcode(priv);
555 if (err) {
556 dev_err(priv->dev, "writing PCE microcode failed, %i", err);
557 return err;
558 }
559
560 /* Default unknown Broadcast/Multicast/Unicast port maps */
561 gswip_switch_w(priv, BIT(cpu_port), GSWIP_PCE_PMAP1);
562 gswip_switch_w(priv, BIT(cpu_port), GSWIP_PCE_PMAP2);
563 gswip_switch_w(priv, BIT(cpu_port), GSWIP_PCE_PMAP3);
564
565 /* disable PHY auto polling */
566 gswip_mdio_w(priv, 0x0, GSWIP_MDIO_MDC_CFG0);
567 /* Configure the MDIO Clock 2.5 MHz */
568 gswip_mdio_mask(priv, 0xff, 0x09, GSWIP_MDIO_MDC_CFG1);
569
570 /* Disable the xMII link */
571 gswip_mii_mask_cfg(priv, GSWIP_MII_CFG_EN, 0, 0);
572 gswip_mii_mask_cfg(priv, GSWIP_MII_CFG_EN, 0, 1);
573 gswip_mii_mask_cfg(priv, GSWIP_MII_CFG_EN, 0, 5);
574
575 /* enable special tag insertion on cpu port */
576 gswip_switch_mask(priv, 0, GSWIP_FDMA_PCTRL_STEN,
577 GSWIP_FDMA_PCTRLp(cpu_port));
578
Hauke Mehrtens30d893832019-05-06 00:25:06 +0200579 /* accept special tag in ingress direction */
580 gswip_switch_mask(priv, 0, GSWIP_PCE_PCTRL_0_INGRESS,
581 GSWIP_PCE_PCTRL_0p(cpu_port));
582
Hauke Mehrtens14fceff2018-09-09 22:20:39 +0200583 gswip_switch_mask(priv, 0, GSWIP_MAC_CTRL_2_MLEN,
584 GSWIP_MAC_CTRL_2p(cpu_port));
585 gswip_switch_w(priv, VLAN_ETH_FRAME_LEN + 8, GSWIP_MAC_FLEN);
586 gswip_switch_mask(priv, 0, GSWIP_BM_QUEUE_GCTRL_GL_MOD,
587 GSWIP_BM_QUEUE_GCTRL);
588
589 /* VLAN aware Switching */
590 gswip_switch_mask(priv, 0, GSWIP_PCE_GCTRL_0_VLAN, GSWIP_PCE_GCTRL_0);
591
592 /* Mac Address Table Lock */
593 gswip_switch_mask(priv, 0, GSWIP_PCE_GCTRL_1_MAC_GLOCK |
594 GSWIP_PCE_GCTRL_1_MAC_GLOCK_MOD,
595 GSWIP_PCE_GCTRL_1);
596
597 gswip_port_enable(ds, cpu_port, NULL);
598 return 0;
599}
600
601static enum dsa_tag_protocol gswip_get_tag_protocol(struct dsa_switch *ds,
602 int port)
603{
604 return DSA_TAG_PROTO_GSWIP;
605}
606
607static void gswip_phylink_validate(struct dsa_switch *ds, int port,
608 unsigned long *supported,
609 struct phylink_link_state *state)
610{
611 __ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, };
612
613 switch (port) {
614 case 0:
615 case 1:
616 if (!phy_interface_mode_is_rgmii(state->interface) &&
617 state->interface != PHY_INTERFACE_MODE_MII &&
618 state->interface != PHY_INTERFACE_MODE_REVMII &&
Hauke Mehrtens0e630b52018-09-15 14:08:48 +0200619 state->interface != PHY_INTERFACE_MODE_RMII)
620 goto unsupported;
Hauke Mehrtens14fceff2018-09-09 22:20:39 +0200621 break;
622 case 2:
623 case 3:
624 case 4:
Hauke Mehrtens0e630b52018-09-15 14:08:48 +0200625 if (state->interface != PHY_INTERFACE_MODE_INTERNAL)
626 goto unsupported;
Hauke Mehrtens14fceff2018-09-09 22:20:39 +0200627 break;
628 case 5:
629 if (!phy_interface_mode_is_rgmii(state->interface) &&
Hauke Mehrtens0e630b52018-09-15 14:08:48 +0200630 state->interface != PHY_INTERFACE_MODE_INTERNAL)
631 goto unsupported;
Hauke Mehrtens14fceff2018-09-09 22:20:39 +0200632 break;
Hauke Mehrtens0e630b52018-09-15 14:08:48 +0200633 default:
634 bitmap_zero(supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
635 dev_err(ds->dev, "Unsupported port: %i\n", port);
636 return;
Hauke Mehrtens14fceff2018-09-09 22:20:39 +0200637 }
638
639 /* Allow all the expected bits */
640 phylink_set(mask, Autoneg);
641 phylink_set_port_modes(mask);
642 phylink_set(mask, Pause);
643 phylink_set(mask, Asym_Pause);
644
645 /* With the exclusion of MII and Reverse MII, we support Gigabit,
646 * including Half duplex
647 */
648 if (state->interface != PHY_INTERFACE_MODE_MII &&
649 state->interface != PHY_INTERFACE_MODE_REVMII) {
650 phylink_set(mask, 1000baseT_Full);
651 phylink_set(mask, 1000baseT_Half);
652 }
653
654 phylink_set(mask, 10baseT_Half);
655 phylink_set(mask, 10baseT_Full);
656 phylink_set(mask, 100baseT_Half);
657 phylink_set(mask, 100baseT_Full);
658
659 bitmap_and(supported, supported, mask,
660 __ETHTOOL_LINK_MODE_MASK_NBITS);
661 bitmap_and(state->advertising, state->advertising, mask,
662 __ETHTOOL_LINK_MODE_MASK_NBITS);
Hauke Mehrtens0e630b52018-09-15 14:08:48 +0200663 return;
664
665unsupported:
666 bitmap_zero(supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
667 dev_err(ds->dev, "Unsupported interface: %d\n", state->interface);
668 return;
Hauke Mehrtens14fceff2018-09-09 22:20:39 +0200669}
670
671static void gswip_phylink_mac_config(struct dsa_switch *ds, int port,
672 unsigned int mode,
673 const struct phylink_link_state *state)
674{
675 struct gswip_priv *priv = ds->priv;
676 u32 miicfg = 0;
677
678 miicfg |= GSWIP_MII_CFG_LDCLKDIS;
679
680 switch (state->interface) {
681 case PHY_INTERFACE_MODE_MII:
682 case PHY_INTERFACE_MODE_INTERNAL:
683 miicfg |= GSWIP_MII_CFG_MODE_MIIM;
684 break;
685 case PHY_INTERFACE_MODE_REVMII:
686 miicfg |= GSWIP_MII_CFG_MODE_MIIP;
687 break;
688 case PHY_INTERFACE_MODE_RMII:
689 miicfg |= GSWIP_MII_CFG_MODE_RMIIM;
690 break;
691 case PHY_INTERFACE_MODE_RGMII:
692 case PHY_INTERFACE_MODE_RGMII_ID:
693 case PHY_INTERFACE_MODE_RGMII_RXID:
694 case PHY_INTERFACE_MODE_RGMII_TXID:
695 miicfg |= GSWIP_MII_CFG_MODE_RGMII;
696 break;
697 default:
698 dev_err(ds->dev,
699 "Unsupported interface: %d\n", state->interface);
700 return;
701 }
702 gswip_mii_mask_cfg(priv, GSWIP_MII_CFG_MODE_MASK, miicfg, port);
703
704 switch (state->interface) {
705 case PHY_INTERFACE_MODE_RGMII_ID:
706 gswip_mii_mask_pcdu(priv, GSWIP_MII_PCDU_TXDLY_MASK |
707 GSWIP_MII_PCDU_RXDLY_MASK, 0, port);
708 break;
709 case PHY_INTERFACE_MODE_RGMII_RXID:
710 gswip_mii_mask_pcdu(priv, GSWIP_MII_PCDU_RXDLY_MASK, 0, port);
711 break;
712 case PHY_INTERFACE_MODE_RGMII_TXID:
713 gswip_mii_mask_pcdu(priv, GSWIP_MII_PCDU_TXDLY_MASK, 0, port);
714 break;
715 default:
716 break;
717 }
718}
719
720static void gswip_phylink_mac_link_down(struct dsa_switch *ds, int port,
721 unsigned int mode,
722 phy_interface_t interface)
723{
724 struct gswip_priv *priv = ds->priv;
725
726 gswip_mii_mask_cfg(priv, GSWIP_MII_CFG_EN, 0, port);
727}
728
729static void gswip_phylink_mac_link_up(struct dsa_switch *ds, int port,
730 unsigned int mode,
731 phy_interface_t interface,
732 struct phy_device *phydev)
733{
734 struct gswip_priv *priv = ds->priv;
735
736 /* Enable the xMII interface only for the external PHY */
737 if (interface != PHY_INTERFACE_MODE_INTERNAL)
738 gswip_mii_mask_cfg(priv, 0, GSWIP_MII_CFG_EN, port);
739}
740
741static void gswip_get_strings(struct dsa_switch *ds, int port, u32 stringset,
742 uint8_t *data)
743{
744 int i;
745
746 if (stringset != ETH_SS_STATS)
747 return;
748
749 for (i = 0; i < ARRAY_SIZE(gswip_rmon_cnt); i++)
750 strncpy(data + i * ETH_GSTRING_LEN, gswip_rmon_cnt[i].name,
751 ETH_GSTRING_LEN);
752}
753
754static u32 gswip_bcm_ram_entry_read(struct gswip_priv *priv, u32 table,
755 u32 index)
756{
757 u32 result;
758 int err;
759
760 gswip_switch_w(priv, index, GSWIP_BM_RAM_ADDR);
761 gswip_switch_mask(priv, GSWIP_BM_RAM_CTRL_ADDR_MASK |
762 GSWIP_BM_RAM_CTRL_OPMOD,
763 table | GSWIP_BM_RAM_CTRL_BAS,
764 GSWIP_BM_RAM_CTRL);
765
766 err = gswip_switch_r_timeout(priv, GSWIP_BM_RAM_CTRL,
767 GSWIP_BM_RAM_CTRL_BAS);
768 if (err) {
769 dev_err(priv->dev, "timeout while reading table: %u, index: %u",
770 table, index);
771 return 0;
772 }
773
774 result = gswip_switch_r(priv, GSWIP_BM_RAM_VAL(0));
775 result |= gswip_switch_r(priv, GSWIP_BM_RAM_VAL(1)) << 16;
776
777 return result;
778}
779
780static void gswip_get_ethtool_stats(struct dsa_switch *ds, int port,
781 uint64_t *data)
782{
783 struct gswip_priv *priv = ds->priv;
784 const struct gswip_rmon_cnt_desc *rmon_cnt;
785 int i;
786 u64 high;
787
788 for (i = 0; i < ARRAY_SIZE(gswip_rmon_cnt); i++) {
789 rmon_cnt = &gswip_rmon_cnt[i];
790
791 data[i] = gswip_bcm_ram_entry_read(priv, port,
792 rmon_cnt->offset);
793 if (rmon_cnt->size == 2) {
794 high = gswip_bcm_ram_entry_read(priv, port,
795 rmon_cnt->offset + 1);
796 data[i] |= high << 32;
797 }
798 }
799}
800
801static int gswip_get_sset_count(struct dsa_switch *ds, int port, int sset)
802{
803 if (sset != ETH_SS_STATS)
804 return 0;
805
806 return ARRAY_SIZE(gswip_rmon_cnt);
807}
808
809static const struct dsa_switch_ops gswip_switch_ops = {
810 .get_tag_protocol = gswip_get_tag_protocol,
811 .setup = gswip_setup,
812 .port_enable = gswip_port_enable,
813 .port_disable = gswip_port_disable,
814 .phylink_validate = gswip_phylink_validate,
815 .phylink_mac_config = gswip_phylink_mac_config,
816 .phylink_mac_link_down = gswip_phylink_mac_link_down,
817 .phylink_mac_link_up = gswip_phylink_mac_link_up,
818 .get_strings = gswip_get_strings,
819 .get_ethtool_stats = gswip_get_ethtool_stats,
820 .get_sset_count = gswip_get_sset_count,
821};
822
823static const struct xway_gphy_match_data xrx200a1x_gphy_data = {
824 .fe_firmware_name = "lantiq/xrx200_phy22f_a14.bin",
825 .ge_firmware_name = "lantiq/xrx200_phy11g_a14.bin",
826};
827
828static const struct xway_gphy_match_data xrx200a2x_gphy_data = {
829 .fe_firmware_name = "lantiq/xrx200_phy22f_a22.bin",
830 .ge_firmware_name = "lantiq/xrx200_phy11g_a22.bin",
831};
832
833static const struct xway_gphy_match_data xrx300_gphy_data = {
834 .fe_firmware_name = "lantiq/xrx300_phy22f_a21.bin",
835 .ge_firmware_name = "lantiq/xrx300_phy11g_a21.bin",
836};
837
838static const struct of_device_id xway_gphy_match[] = {
839 { .compatible = "lantiq,xrx200-gphy-fw", .data = NULL },
840 { .compatible = "lantiq,xrx200a1x-gphy-fw", .data = &xrx200a1x_gphy_data },
841 { .compatible = "lantiq,xrx200a2x-gphy-fw", .data = &xrx200a2x_gphy_data },
842 { .compatible = "lantiq,xrx300-gphy-fw", .data = &xrx300_gphy_data },
843 { .compatible = "lantiq,xrx330-gphy-fw", .data = &xrx300_gphy_data },
844 {},
845};
846
847static int gswip_gphy_fw_load(struct gswip_priv *priv, struct gswip_gphy_fw *gphy_fw)
848{
849 struct device *dev = priv->dev;
850 const struct firmware *fw;
851 void *fw_addr;
852 dma_addr_t dma_addr;
853 dma_addr_t dev_addr;
854 size_t size;
855 int ret;
856
857 ret = clk_prepare_enable(gphy_fw->clk_gate);
858 if (ret)
859 return ret;
860
861 reset_control_assert(gphy_fw->reset);
862
863 ret = request_firmware(&fw, gphy_fw->fw_name, dev);
864 if (ret) {
865 dev_err(dev, "failed to load firmware: %s, error: %i\n",
866 gphy_fw->fw_name, ret);
867 return ret;
868 }
869
870 /* GPHY cores need the firmware code in a persistent and contiguous
871 * memory area with a 16 kB boundary aligned start address.
872 */
873 size = fw->size + XRX200_GPHY_FW_ALIGN;
874
875 fw_addr = dmam_alloc_coherent(dev, size, &dma_addr, GFP_KERNEL);
876 if (fw_addr) {
877 fw_addr = PTR_ALIGN(fw_addr, XRX200_GPHY_FW_ALIGN);
878 dev_addr = ALIGN(dma_addr, XRX200_GPHY_FW_ALIGN);
879 memcpy(fw_addr, fw->data, fw->size);
880 } else {
881 dev_err(dev, "failed to alloc firmware memory\n");
882 release_firmware(fw);
883 return -ENOMEM;
884 }
885
886 release_firmware(fw);
887
888 ret = regmap_write(priv->rcu_regmap, gphy_fw->fw_addr_offset, dev_addr);
889 if (ret)
890 return ret;
891
892 reset_control_deassert(gphy_fw->reset);
893
894 return ret;
895}
896
897static int gswip_gphy_fw_probe(struct gswip_priv *priv,
898 struct gswip_gphy_fw *gphy_fw,
899 struct device_node *gphy_fw_np, int i)
900{
901 struct device *dev = priv->dev;
902 u32 gphy_mode;
903 int ret;
904 char gphyname[10];
905
906 snprintf(gphyname, sizeof(gphyname), "gphy%d", i);
907
908 gphy_fw->clk_gate = devm_clk_get(dev, gphyname);
909 if (IS_ERR(gphy_fw->clk_gate)) {
910 dev_err(dev, "Failed to lookup gate clock\n");
911 return PTR_ERR(gphy_fw->clk_gate);
912 }
913
914 ret = of_property_read_u32(gphy_fw_np, "reg", &gphy_fw->fw_addr_offset);
915 if (ret)
916 return ret;
917
918 ret = of_property_read_u32(gphy_fw_np, "lantiq,gphy-mode", &gphy_mode);
919 /* Default to GE mode */
920 if (ret)
921 gphy_mode = GPHY_MODE_GE;
922
923 switch (gphy_mode) {
924 case GPHY_MODE_FE:
925 gphy_fw->fw_name = priv->gphy_fw_name_cfg->fe_firmware_name;
926 break;
927 case GPHY_MODE_GE:
928 gphy_fw->fw_name = priv->gphy_fw_name_cfg->ge_firmware_name;
929 break;
930 default:
931 dev_err(dev, "Unknown GPHY mode %d\n", gphy_mode);
932 return -EINVAL;
933 }
934
935 gphy_fw->reset = of_reset_control_array_get_exclusive(gphy_fw_np);
Wei Yongjunf592e0b2018-09-15 01:33:38 +0000936 if (IS_ERR(gphy_fw->reset)) {
937 if (PTR_ERR(gphy_fw->reset) != -EPROBE_DEFER)
Hauke Mehrtens14fceff2018-09-09 22:20:39 +0200938 dev_err(dev, "Failed to lookup gphy reset\n");
Wei Yongjunf592e0b2018-09-15 01:33:38 +0000939 return PTR_ERR(gphy_fw->reset);
Hauke Mehrtens14fceff2018-09-09 22:20:39 +0200940 }
941
942 return gswip_gphy_fw_load(priv, gphy_fw);
943}
944
945static void gswip_gphy_fw_remove(struct gswip_priv *priv,
946 struct gswip_gphy_fw *gphy_fw)
947{
948 int ret;
949
950 /* check if the device was fully probed */
951 if (!gphy_fw->fw_name)
952 return;
953
954 ret = regmap_write(priv->rcu_regmap, gphy_fw->fw_addr_offset, 0);
955 if (ret)
956 dev_err(priv->dev, "can not reset GPHY FW pointer");
957
958 clk_disable_unprepare(gphy_fw->clk_gate);
959
960 reset_control_put(gphy_fw->reset);
961}
962
963static int gswip_gphy_fw_list(struct gswip_priv *priv,
964 struct device_node *gphy_fw_list_np, u32 version)
965{
966 struct device *dev = priv->dev;
967 struct device_node *gphy_fw_np;
968 const struct of_device_id *match;
969 int err;
970 int i = 0;
971
Hauke Mehrtens0e630b52018-09-15 14:08:48 +0200972 /* The VRX200 rev 1.1 uses the GSWIP 2.0 and needs the older
Hauke Mehrtens14fceff2018-09-09 22:20:39 +0200973 * GPHY firmware. The VRX200 rev 1.2 uses the GSWIP 2.1 and also
974 * needs a different GPHY firmware.
975 */
976 if (of_device_is_compatible(gphy_fw_list_np, "lantiq,xrx200-gphy-fw")) {
977 switch (version) {
978 case GSWIP_VERSION_2_0:
979 priv->gphy_fw_name_cfg = &xrx200a1x_gphy_data;
980 break;
981 case GSWIP_VERSION_2_1:
982 priv->gphy_fw_name_cfg = &xrx200a2x_gphy_data;
983 break;
984 default:
985 dev_err(dev, "unknown GSWIP version: 0x%x", version);
986 return -ENOENT;
987 }
988 }
989
990 match = of_match_node(xway_gphy_match, gphy_fw_list_np);
991 if (match && match->data)
992 priv->gphy_fw_name_cfg = match->data;
993
994 if (!priv->gphy_fw_name_cfg) {
995 dev_err(dev, "GPHY compatible type not supported");
996 return -ENOENT;
997 }
998
999 priv->num_gphy_fw = of_get_available_child_count(gphy_fw_list_np);
1000 if (!priv->num_gphy_fw)
1001 return -ENOENT;
1002
1003 priv->rcu_regmap = syscon_regmap_lookup_by_phandle(gphy_fw_list_np,
1004 "lantiq,rcu");
1005 if (IS_ERR(priv->rcu_regmap))
1006 return PTR_ERR(priv->rcu_regmap);
1007
1008 priv->gphy_fw = devm_kmalloc_array(dev, priv->num_gphy_fw,
1009 sizeof(*priv->gphy_fw),
1010 GFP_KERNEL | __GFP_ZERO);
1011 if (!priv->gphy_fw)
1012 return -ENOMEM;
1013
1014 for_each_available_child_of_node(gphy_fw_list_np, gphy_fw_np) {
1015 err = gswip_gphy_fw_probe(priv, &priv->gphy_fw[i],
1016 gphy_fw_np, i);
1017 if (err)
1018 goto remove_gphy;
1019 i++;
1020 }
1021
1022 return 0;
1023
1024remove_gphy:
1025 for (i = 0; i < priv->num_gphy_fw; i++)
1026 gswip_gphy_fw_remove(priv, &priv->gphy_fw[i]);
1027 return err;
1028}
1029
1030static int gswip_probe(struct platform_device *pdev)
1031{
1032 struct gswip_priv *priv;
1033 struct resource *gswip_res, *mdio_res, *mii_res;
1034 struct device_node *mdio_np, *gphy_fw_np;
1035 struct device *dev = &pdev->dev;
1036 int err;
1037 int i;
1038 u32 version;
1039
1040 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
1041 if (!priv)
1042 return -ENOMEM;
1043
1044 gswip_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1045 priv->gswip = devm_ioremap_resource(dev, gswip_res);
Wei Yongjunf5de8bf2018-09-15 01:33:21 +00001046 if (IS_ERR(priv->gswip))
1047 return PTR_ERR(priv->gswip);
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02001048
1049 mdio_res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1050 priv->mdio = devm_ioremap_resource(dev, mdio_res);
Wei Yongjunf5de8bf2018-09-15 01:33:21 +00001051 if (IS_ERR(priv->mdio))
1052 return PTR_ERR(priv->mdio);
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02001053
1054 mii_res = platform_get_resource(pdev, IORESOURCE_MEM, 2);
1055 priv->mii = devm_ioremap_resource(dev, mii_res);
Wei Yongjunf5de8bf2018-09-15 01:33:21 +00001056 if (IS_ERR(priv->mii))
1057 return PTR_ERR(priv->mii);
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02001058
1059 priv->hw_info = of_device_get_match_data(dev);
1060 if (!priv->hw_info)
1061 return -EINVAL;
1062
1063 priv->ds = dsa_switch_alloc(dev, priv->hw_info->max_ports);
1064 if (!priv->ds)
1065 return -ENOMEM;
1066
1067 priv->ds->priv = priv;
1068 priv->ds->ops = &gswip_switch_ops;
1069 priv->dev = dev;
1070 version = gswip_switch_r(priv, GSWIP_VERSION);
1071
1072 /* bring up the mdio bus */
Johan Hovoldc8cbcb02019-01-16 11:23:34 +01001073 gphy_fw_np = of_get_compatible_child(dev->of_node, "lantiq,gphy-fw");
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02001074 if (gphy_fw_np) {
1075 err = gswip_gphy_fw_list(priv, gphy_fw_np, version);
Johan Hovoldc8cbcb02019-01-16 11:23:34 +01001076 of_node_put(gphy_fw_np);
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02001077 if (err) {
1078 dev_err(dev, "gphy fw probe failed\n");
1079 return err;
1080 }
1081 }
1082
1083 /* bring up the mdio bus */
Johan Hovoldc8cbcb02019-01-16 11:23:34 +01001084 mdio_np = of_get_compatible_child(dev->of_node, "lantiq,xrx200-mdio");
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02001085 if (mdio_np) {
1086 err = gswip_mdio(priv, mdio_np);
1087 if (err) {
1088 dev_err(dev, "mdio probe failed\n");
Johan Hovoldc8cbcb02019-01-16 11:23:34 +01001089 goto put_mdio_node;
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02001090 }
1091 }
1092
1093 err = dsa_register_switch(priv->ds);
1094 if (err) {
1095 dev_err(dev, "dsa switch register failed: %i\n", err);
1096 goto mdio_bus;
1097 }
Hauke Mehrtens0e630b52018-09-15 14:08:48 +02001098 if (!dsa_is_cpu_port(priv->ds, priv->hw_info->cpu_port)) {
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02001099 dev_err(dev, "wrong CPU port defined, HW only supports port: %i",
1100 priv->hw_info->cpu_port);
1101 err = -EINVAL;
Johan Hovoldaed13f22019-01-16 11:23:33 +01001102 goto disable_switch;
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02001103 }
1104
1105 platform_set_drvdata(pdev, priv);
1106
1107 dev_info(dev, "probed GSWIP version %lx mod %lx\n",
1108 (version & GSWIP_VERSION_REV_MASK) >> GSWIP_VERSION_REV_SHIFT,
1109 (version & GSWIP_VERSION_MOD_MASK) >> GSWIP_VERSION_MOD_SHIFT);
1110 return 0;
1111
Johan Hovoldaed13f22019-01-16 11:23:33 +01001112disable_switch:
1113 gswip_mdio_mask(priv, GSWIP_MDIO_GLOB_ENABLE, 0, GSWIP_MDIO_GLOB);
1114 dsa_unregister_switch(priv->ds);
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02001115mdio_bus:
1116 if (mdio_np)
1117 mdiobus_unregister(priv->ds->slave_mii_bus);
Johan Hovoldc8cbcb02019-01-16 11:23:34 +01001118put_mdio_node:
1119 of_node_put(mdio_np);
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02001120 for (i = 0; i < priv->num_gphy_fw; i++)
1121 gswip_gphy_fw_remove(priv, &priv->gphy_fw[i]);
1122 return err;
1123}
1124
1125static int gswip_remove(struct platform_device *pdev)
1126{
1127 struct gswip_priv *priv = platform_get_drvdata(pdev);
1128 int i;
1129
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02001130 /* disable the switch */
1131 gswip_mdio_mask(priv, GSWIP_MDIO_GLOB_ENABLE, 0, GSWIP_MDIO_GLOB);
1132
1133 dsa_unregister_switch(priv->ds);
1134
Johan Hovoldc8cbcb02019-01-16 11:23:34 +01001135 if (priv->ds->slave_mii_bus) {
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02001136 mdiobus_unregister(priv->ds->slave_mii_bus);
Johan Hovoldc8cbcb02019-01-16 11:23:34 +01001137 of_node_put(priv->ds->slave_mii_bus->dev.of_node);
1138 }
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02001139
1140 for (i = 0; i < priv->num_gphy_fw; i++)
1141 gswip_gphy_fw_remove(priv, &priv->gphy_fw[i]);
1142
1143 return 0;
1144}
1145
1146static const struct gswip_hw_info gswip_xrx200 = {
1147 .max_ports = 7,
1148 .cpu_port = 6,
1149};
1150
1151static const struct of_device_id gswip_of_match[] = {
1152 { .compatible = "lantiq,xrx200-gswip", .data = &gswip_xrx200 },
1153 {},
1154};
1155MODULE_DEVICE_TABLE(of, gswip_of_match);
1156
1157static struct platform_driver gswip_driver = {
1158 .probe = gswip_probe,
1159 .remove = gswip_remove,
1160 .driver = {
1161 .name = "gswip",
1162 .of_match_table = gswip_of_match,
1163 },
1164};
1165
1166module_platform_driver(gswip_driver);
1167
Hauke Mehrtenscffde202019-02-22 20:11:13 +01001168MODULE_FIRMWARE("lantiq/xrx300_phy11g_a21.bin");
1169MODULE_FIRMWARE("lantiq/xrx300_phy22f_a21.bin");
1170MODULE_FIRMWARE("lantiq/xrx200_phy11g_a14.bin");
1171MODULE_FIRMWARE("lantiq/xrx200_phy11g_a22.bin");
1172MODULE_FIRMWARE("lantiq/xrx200_phy22f_a14.bin");
1173MODULE_FIRMWARE("lantiq/xrx200_phy22f_a22.bin");
Hauke Mehrtens14fceff2018-09-09 22:20:39 +02001174MODULE_AUTHOR("Hauke Mehrtens <hauke@hauke-m.de>");
1175MODULE_DESCRIPTION("Lantiq / Intel GSWIP driver");
1176MODULE_LICENSE("GPL v2");