1 // SPDX-License-Identifier: (GPL-2.0+ OR MIT)
3 * Copyright (c) 2018 Microsemi Corporation
9 #include <dm/of_access.h>
10 #include <dm/of_addr.h>
11 #include <fdt_support.h>
13 #include <linux/ioport.h>
18 #include "mscc_xfer.h"
19 #include "mscc_mac_table.h"
22 #define PHY_CFG_ENA 0xF
23 #define PHY_CFG_COMMON_RST BIT(4)
24 #define PHY_CFG_RST (0xF << 5)
26 #define PHY_STAT_SUPERVISOR_COMPLETE BIT(0)
28 #define GCB_MIIM_MII_STATUS 0x0
29 #define GCB_MIIM_STAT_BUSY BIT(3)
30 #define GCB_MIIM_MII_CMD 0x8
31 #define GCB_MIIM_MII_CMD_SCAN BIT(0)
32 #define GCB_MIIM_MII_CMD_OPR_WRITE BIT(1)
33 #define GCB_MIIM_MII_CMD_OPR_READ BIT(2)
34 #define GCB_MIIM_MII_CMD_SINGLE_SCAN BIT(3)
35 #define GCB_MIIM_MII_CMD_WRDATA(x) ((x) << 4)
36 #define GCB_MIIM_MII_CMD_REGAD(x) ((x) << 20)
37 #define GCB_MIIM_MII_CMD_PHYAD(x) ((x) << 25)
38 #define GCB_MIIM_MII_CMD_VLD BIT(31)
39 #define GCB_MIIM_DATA 0xC
40 #define GCB_MIIM_DATA_ERROR (0x3 << 16)
42 #define ANA_PORT_VLAN_CFG(x) (0x7000 + 0x100 * (x))
43 #define ANA_PORT_VLAN_CFG_AWARE_ENA BIT(20)
44 #define ANA_PORT_VLAN_CFG_POP_CNT(x) ((x) << 18)
45 #define ANA_PORT_PORT_CFG(x) (0x7070 + 0x100 * (x))
46 #define ANA_PORT_PORT_CFG_RECV_ENA BIT(6)
47 #define ANA_PGID(x) (0x8c00 + 4 * (x))
49 #define HSIO_ANA_SERDES1G_DES_CFG 0x4c
50 #define HSIO_ANA_SERDES1G_DES_CFG_BW_HYST(x) ((x) << 1)
51 #define HSIO_ANA_SERDES1G_DES_CFG_BW_ANA(x) ((x) << 5)
52 #define HSIO_ANA_SERDES1G_DES_CFG_MBTR_CTRL(x) ((x) << 8)
53 #define HSIO_ANA_SERDES1G_DES_CFG_PHS_CTRL(x) ((x) << 13)
54 #define HSIO_ANA_SERDES1G_IB_CFG 0x50
55 #define HSIO_ANA_SERDES1G_IB_CFG_RESISTOR_CTRL(x) (x)
56 #define HSIO_ANA_SERDES1G_IB_CFG_EQ_GAIN(x) ((x) << 6)
57 #define HSIO_ANA_SERDES1G_IB_CFG_ENA_OFFSET_COMP BIT(9)
58 #define HSIO_ANA_SERDES1G_IB_CFG_ENA_DETLEV BIT(11)
59 #define HSIO_ANA_SERDES1G_IB_CFG_ENA_CMV_TERM BIT(13)
60 #define HSIO_ANA_SERDES1G_IB_CFG_ACJTAG_HYST(x) ((x) << 24)
61 #define HSIO_ANA_SERDES1G_OB_CFG 0x54
62 #define HSIO_ANA_SERDES1G_OB_CFG_RESISTOR_CTRL(x) (x)
63 #define HSIO_ANA_SERDES1G_OB_CFG_VCM_CTRL(x) ((x) << 4)
64 #define HSIO_ANA_SERDES1G_OB_CFG_CMM_BIAS_CTRL(x) ((x) << 10)
65 #define HSIO_ANA_SERDES1G_OB_CFG_AMP_CTRL(x) ((x) << 13)
66 #define HSIO_ANA_SERDES1G_OB_CFG_SLP(x) ((x) << 17)
67 #define HSIO_ANA_SERDES1G_SER_CFG 0x58
68 #define HSIO_ANA_SERDES1G_COMMON_CFG 0x5c
69 #define HSIO_ANA_SERDES1G_COMMON_CFG_IF_MODE BIT(0)
70 #define HSIO_ANA_SERDES1G_COMMON_CFG_ENA_LANE BIT(18)
71 #define HSIO_ANA_SERDES1G_COMMON_CFG_SYS_RST BIT(31)
72 #define HSIO_ANA_SERDES1G_PLL_CFG 0x60
73 #define HSIO_ANA_SERDES1G_PLL_CFG_FSM_ENA BIT(7)
74 #define HSIO_ANA_SERDES1G_PLL_CFG_FSM_CTRL_DATA(x) ((x) << 8)
75 #define HSIO_ANA_SERDES1G_PLL_CFG_ENA_RC_DIV2 BIT(21)
76 #define HSIO_DIG_SERDES1G_DFT_CFG0 0x68
77 #define HSIO_DIG_SERDES1G_MISC_CFG 0x7c
78 #define HSIO_DIG_SERDES1G_MISC_CFG_LANE_RST BIT(0)
79 #define HSIO_MCB_SERDES1G_CFG 0x88
80 #define HSIO_MCB_SERDES1G_CFG_WR_ONE_SHOT BIT(31)
81 #define HSIO_MCB_SERDES1G_CFG_ADDR(x) (x)
82 #define HSIO_HW_CFGSTAT_HW_CFG 0x10c
84 #define SYS_FRM_AGING 0x574
85 #define SYS_FRM_AGING_ENA BIT(20)
87 #define SYS_SYSTEM_RST_CFG 0x508
88 #define SYS_SYSTEM_RST_MEM_INIT BIT(0)
89 #define SYS_SYSTEM_RST_MEM_ENA BIT(1)
90 #define SYS_SYSTEM_RST_CORE_ENA BIT(2)
91 #define SYS_PORT_MODE(x) (0x514 + 0x4 * (x))
92 #define SYS_PORT_MODE_INCL_INJ_HDR(x) ((x) << 3)
93 #define SYS_PORT_MODE_INCL_INJ_HDR_M GENMASK(4, 3)
94 #define SYS_PORT_MODE_INCL_XTR_HDR(x) ((x) << 1)
95 #define SYS_PORT_MODE_INCL_XTR_HDR_M GENMASK(2, 1)
96 #define SYS_PAUSE_CFG(x) (0x608 + 0x4 * (x))
97 #define SYS_PAUSE_CFG_PAUSE_ENA BIT(0)
99 #define QSYS_SWITCH_PORT_MODE(x) (0x11234 + 0x4 * (x))
100 #define QSYS_SWITCH_PORT_MODE_PORT_ENA BIT(14)
101 #define QSYS_QMAP 0x112d8
102 #define QSYS_EGR_NO_SHARING 0x1129c
105 #define DEV_CLOCK_CFG 0x0
106 #define DEV_CLOCK_CFG_LINK_SPEED_1000 1
107 #define DEV_MAC_ENA_CFG 0x1c
108 #define DEV_MAC_ENA_CFG_RX_ENA BIT(4)
109 #define DEV_MAC_ENA_CFG_TX_ENA BIT(0)
111 #define DEV_MAC_IFG_CFG 0x30
112 #define DEV_MAC_IFG_CFG_TX_IFG(x) ((x) << 8)
113 #define DEV_MAC_IFG_CFG_RX_IFG2(x) ((x) << 4)
114 #define DEV_MAC_IFG_CFG_RX_IFG1(x) (x)
116 #define PCS1G_CFG 0x48
117 #define PCS1G_MODE_CFG_SGMII_MODE_ENA BIT(0)
118 #define PCS1G_MODE_CFG 0x4c
119 #define PCS1G_MODE_CFG_UNIDIR_MODE_ENA BIT(4)
120 #define PCS1G_MODE_CFG_SGMII_MODE_ENA BIT(0)
121 #define PCS1G_SD_CFG 0x50
122 #define PCS1G_ANEG_CFG 0x54
123 #define PCS1G_ANEG_CFG_ADV_ABILITY(x) ((x) << 16)
125 #define QS_XTR_GRP_CFG(x) (4 * (x))
126 #define QS_XTR_GRP_CFG_MODE(x) ((x) << 2)
127 #define QS_XTR_GRP_CFG_STATUS_WORD_POS BIT(1)
128 #define QS_XTR_GRP_CFG_BYTE_SWAP BIT(0)
129 #define QS_INJ_GRP_CFG(x) (0x24 + (x) * 4)
130 #define QS_INJ_GRP_CFG_MODE(x) ((x) << 2)
131 #define QS_INJ_GRP_CFG_BYTE_SWAP BIT(0)
133 #define IFH_INJ_BYPASS BIT(31)
134 #define IFH_TAG_TYPE_C 0
137 #define INTERNAL_PORT_MSK 0x2FF
140 #define PGID_BROADCAST 13
141 #define PGID_UNICAST 14
144 static const char * const regs_names[] = {
145 "port0", "port1", "port2", "port3", "port4", "port5", "port6", "port7",
146 "port8", "port9", "port10", "sys", "rew", "qs", "hsio", "qsys", "ana",
149 #define REGS_NAMES_COUNT ARRAY_SIZE(regs_names) + 1
152 enum ocelot_ctrl_regs {
161 #define OCELOT_MIIM_BUS_COUNT 2
163 struct ocelot_phy_port_t {
170 struct ocelot_private {
171 void __iomem *regs[REGS_NAMES_COUNT];
172 struct mii_dev *bus[OCELOT_MIIM_BUS_COUNT];
173 struct ocelot_phy_port_t ports[MAX_PORT];
176 struct mscc_miim_dev {
178 phys_addr_t miim_base;
179 unsigned long miim_size;
183 static struct mscc_miim_dev miim[OCELOT_MIIM_BUS_COUNT];
184 static int miim_count = -1;
186 static const unsigned long ocelot_regs_qs[] = {
187 [MSCC_QS_XTR_RD] = 0x8,
188 [MSCC_QS_XTR_FLUSH] = 0x18,
189 [MSCC_QS_XTR_DATA_PRESENT] = 0x1c,
190 [MSCC_QS_INJ_WR] = 0x2c,
191 [MSCC_QS_INJ_CTRL] = 0x34,
194 static const unsigned long ocelot_regs_ana_table[] = {
195 [MSCC_ANA_TABLES_MACHDATA] = 0x8b34,
196 [MSCC_ANA_TABLES_MACLDATA] = 0x8b38,
197 [MSCC_ANA_TABLES_MACACCESS] = 0x8b3c,
200 static void mscc_phy_reset(void)
202 writel(0, BASE_DEVCPU_GCB + PERF_PHY_CFG + PHY_CFG);
203 writel(PHY_CFG_RST | PHY_CFG_COMMON_RST
204 | PHY_CFG_ENA, BASE_DEVCPU_GCB + PERF_PHY_CFG + PHY_CFG);
205 if (wait_for_bit_le32((const void *)(BASE_DEVCPU_GCB + PERF_PHY_CFG) +
206 PHY_STAT, PHY_STAT_SUPERVISOR_COMPLETE,
207 true, 2000, false)) {
208 pr_err("Timeout in phy reset\n");
212 static int mscc_miim_wait_ready(struct mscc_miim_dev *miim)
214 return wait_for_bit_le32(miim->regs + GCB_MIIM_MII_STATUS,
215 GCB_MIIM_STAT_BUSY, false, 250, false);
218 static int mscc_miim_read(struct mii_dev *bus, int addr, int devad, int reg)
220 struct mscc_miim_dev *miim = (struct mscc_miim_dev *)bus->priv;
224 ret = mscc_miim_wait_ready(miim);
228 writel(GCB_MIIM_MII_CMD_VLD | GCB_MIIM_MII_CMD_PHYAD(addr) |
229 GCB_MIIM_MII_CMD_REGAD(reg) | GCB_MIIM_MII_CMD_OPR_READ,
230 miim->regs + GCB_MIIM_MII_CMD);
232 ret = mscc_miim_wait_ready(miim);
236 val = readl(miim->regs + GCB_MIIM_DATA);
237 if (val & GCB_MIIM_DATA_ERROR) {
247 static int mscc_miim_write(struct mii_dev *bus, int addr, int devad, int reg,
250 struct mscc_miim_dev *miim = (struct mscc_miim_dev *)bus->priv;
253 ret = mscc_miim_wait_ready(miim);
257 writel(GCB_MIIM_MII_CMD_VLD | GCB_MIIM_MII_CMD_PHYAD(addr) |
258 GCB_MIIM_MII_CMD_REGAD(reg) | GCB_MIIM_MII_CMD_WRDATA(val) |
259 GCB_MIIM_MII_CMD_OPR_WRITE, miim->regs + GCB_MIIM_MII_CMD);
264 static struct mii_dev *ocelot_mdiobus_init(phys_addr_t miim_base,
265 unsigned long miim_size)
275 sprintf(bus->name, "miim-bus%d", miim_count);
277 miim[miim_count].regs = ioremap(miim_base, miim_size);
278 miim[miim_count].miim_base = miim_base;
279 miim[miim_count].miim_size = miim_size;
280 bus->priv = &miim[miim_count];
281 bus->read = mscc_miim_read;
282 bus->write = mscc_miim_write;
284 if (mdio_register(bus))
287 miim[miim_count].bus = bus;
291 __weak void mscc_switch_reset(void)
295 static void ocelot_stop(struct udevice *dev)
301 static void ocelot_cpu_capture_setup(struct ocelot_private *priv)
305 /* map the 8 CPU extraction queues to CPU port 11 */
306 writel(0, priv->regs[QSYS] + QSYS_QMAP);
308 for (i = 0; i <= 1; i++) {
310 * Do byte-swap and expect status after last data word
311 * Extraction: Mode: manual extraction) | Byte_swap
313 writel(QS_XTR_GRP_CFG_MODE(1) | QS_XTR_GRP_CFG_BYTE_SWAP,
314 priv->regs[QS] + QS_XTR_GRP_CFG(i));
316 * Injection: Mode: manual extraction | Byte_swap
318 writel(QS_INJ_GRP_CFG_MODE(1) | QS_INJ_GRP_CFG_BYTE_SWAP,
319 priv->regs[QS] + QS_INJ_GRP_CFG(i));
322 for (i = 0; i <= 1; i++)
323 /* Enable IFH insertion/parsing on CPU ports */
324 writel(SYS_PORT_MODE_INCL_INJ_HDR(1) |
325 SYS_PORT_MODE_INCL_XTR_HDR(1),
326 priv->regs[SYS] + SYS_PORT_MODE(CPU_PORT + i));
328 * Setup the CPU port as VLAN aware to support switching frames
331 writel(ANA_PORT_VLAN_CFG_AWARE_ENA | ANA_PORT_VLAN_CFG_POP_CNT(1) |
332 MAC_VID, priv->regs[ANA] + ANA_PORT_VLAN_CFG(CPU_PORT));
334 /* Disable learning (only RECV_ENA must be set) */
335 writel(ANA_PORT_PORT_CFG_RECV_ENA,
336 priv->regs[ANA] + ANA_PORT_PORT_CFG(CPU_PORT));
338 /* Enable switching to/from cpu port */
339 setbits_le32(priv->regs[QSYS] + QSYS_SWITCH_PORT_MODE(CPU_PORT),
340 QSYS_SWITCH_PORT_MODE_PORT_ENA);
342 /* No pause on CPU port - not needed (off by default) */
343 clrbits_le32(priv->regs[SYS] + SYS_PAUSE_CFG(CPU_PORT),
344 SYS_PAUSE_CFG_PAUSE_ENA);
346 setbits_le32(priv->regs[QSYS] + QSYS_EGR_NO_SHARING, BIT(CPU_PORT));
349 static void ocelot_port_init(struct ocelot_private *priv, int port)
351 void __iomem *regs = priv->regs[port];
354 writel(PCS1G_MODE_CFG_SGMII_MODE_ENA, regs + PCS1G_CFG);
356 /* Disable Signal Detect */
357 writel(0, regs + PCS1G_SD_CFG);
359 /* Enable MAC RX and TX */
360 writel(DEV_MAC_ENA_CFG_RX_ENA | DEV_MAC_ENA_CFG_TX_ENA,
361 regs + DEV_MAC_ENA_CFG);
363 /* Clear sgmii_mode_ena */
364 writel(0, regs + PCS1G_MODE_CFG);
367 * Clear sw_resolve_ena(bit 0) and set adv_ability to
368 * something meaningful just in case
370 writel(PCS1G_ANEG_CFG_ADV_ABILITY(0x20), regs + PCS1G_ANEG_CFG);
372 /* Set MAC IFG Gaps */
373 writel(DEV_MAC_IFG_CFG_TX_IFG(5) | DEV_MAC_IFG_CFG_RX_IFG1(5) |
374 DEV_MAC_IFG_CFG_RX_IFG2(1), regs + DEV_MAC_IFG_CFG);
376 /* Set link speed and release all resets */
377 writel(DEV_CLOCK_CFG_LINK_SPEED_1000, regs + DEV_CLOCK_CFG);
379 /* Make VLAN aware for CPU traffic */
380 writel(ANA_PORT_VLAN_CFG_AWARE_ENA | ANA_PORT_VLAN_CFG_POP_CNT(1) |
381 MAC_VID, priv->regs[ANA] + ANA_PORT_VLAN_CFG(port));
383 /* Enable the port in the core */
384 setbits_le32(priv->regs[QSYS] + QSYS_SWITCH_PORT_MODE(port),
385 QSYS_SWITCH_PORT_MODE_PORT_ENA);
388 static void serdes1g_write(void __iomem *base, u32 addr)
392 writel(HSIO_MCB_SERDES1G_CFG_WR_ONE_SHOT |
393 HSIO_MCB_SERDES1G_CFG_ADDR(addr),
394 base + HSIO_MCB_SERDES1G_CFG);
397 data = readl(base + HSIO_MCB_SERDES1G_CFG);
398 } while (data & HSIO_MCB_SERDES1G_CFG_WR_ONE_SHOT);
401 static void serdes1g_setup(void __iomem *base, uint32_t addr,
402 phy_interface_t interface)
404 writel(0x34, base + HSIO_HW_CFGSTAT_HW_CFG);
406 writel(0x0, base + HSIO_ANA_SERDES1G_SER_CFG);
407 writel(0x0, base + HSIO_DIG_SERDES1G_DFT_CFG0);
408 writel(HSIO_ANA_SERDES1G_IB_CFG_RESISTOR_CTRL(11) |
409 HSIO_ANA_SERDES1G_IB_CFG_EQ_GAIN(0) |
410 HSIO_ANA_SERDES1G_IB_CFG_ENA_OFFSET_COMP |
411 HSIO_ANA_SERDES1G_IB_CFG_ENA_CMV_TERM |
412 HSIO_ANA_SERDES1G_IB_CFG_ACJTAG_HYST(1),
413 base + HSIO_ANA_SERDES1G_IB_CFG);
414 writel(HSIO_ANA_SERDES1G_DES_CFG_BW_HYST(7) |
415 HSIO_ANA_SERDES1G_DES_CFG_BW_ANA(6) |
416 HSIO_ANA_SERDES1G_DES_CFG_MBTR_CTRL(2) |
417 HSIO_ANA_SERDES1G_DES_CFG_PHS_CTRL(6),
418 base + HSIO_ANA_SERDES1G_DES_CFG);
419 writel(HSIO_ANA_SERDES1G_OB_CFG_RESISTOR_CTRL(1) |
420 HSIO_ANA_SERDES1G_OB_CFG_VCM_CTRL(4) |
421 HSIO_ANA_SERDES1G_OB_CFG_CMM_BIAS_CTRL(2) |
422 HSIO_ANA_SERDES1G_OB_CFG_AMP_CTRL(12) |
423 HSIO_ANA_SERDES1G_OB_CFG_SLP(3),
424 base + HSIO_ANA_SERDES1G_OB_CFG);
425 writel(HSIO_ANA_SERDES1G_COMMON_CFG_IF_MODE |
426 HSIO_ANA_SERDES1G_COMMON_CFG_ENA_LANE,
427 base + HSIO_ANA_SERDES1G_COMMON_CFG);
428 writel(HSIO_ANA_SERDES1G_PLL_CFG_FSM_ENA |
429 HSIO_ANA_SERDES1G_PLL_CFG_FSM_CTRL_DATA(200) |
430 HSIO_ANA_SERDES1G_PLL_CFG_ENA_RC_DIV2,
431 base + HSIO_ANA_SERDES1G_PLL_CFG);
432 writel(HSIO_DIG_SERDES1G_MISC_CFG_LANE_RST,
433 base + HSIO_DIG_SERDES1G_MISC_CFG);
435 serdes1g_write(base, addr);
437 writel(HSIO_ANA_SERDES1G_COMMON_CFG_IF_MODE |
438 HSIO_ANA_SERDES1G_COMMON_CFG_ENA_LANE |
439 HSIO_ANA_SERDES1G_COMMON_CFG_SYS_RST,
440 base + HSIO_ANA_SERDES1G_COMMON_CFG);
441 serdes1g_write(base, addr);
443 writel(0x0, base + HSIO_DIG_SERDES1G_MISC_CFG);
444 serdes1g_write(base, addr);
447 static void serdes_setup(struct ocelot_private *priv)
452 for (i = 0; i < MAX_PORT; ++i) {
453 if (!priv->ports[i].bus || priv->ports[i].serdes_index == 0xff)
456 mask = BIT(priv->ports[i].serdes_index);
457 serdes1g_setup(priv->regs[HSIO], mask,
458 priv->ports[i].phy_mode);
462 static int ocelot_switch_init(struct ocelot_private *priv)
464 /* Reset switch & memories */
465 writel(SYS_SYSTEM_RST_MEM_ENA | SYS_SYSTEM_RST_MEM_INIT,
466 priv->regs[SYS] + SYS_SYSTEM_RST_CFG);
468 /* Wait to complete */
469 if (wait_for_bit_le32(priv->regs[SYS] + SYS_SYSTEM_RST_CFG,
470 SYS_SYSTEM_RST_MEM_INIT, false, 2000, false)) {
471 pr_err("Timeout in memory reset\n");
475 /* Enable switch core */
476 setbits_le32(priv->regs[SYS] + SYS_SYSTEM_RST_CFG,
477 SYS_SYSTEM_RST_CORE_ENA);
483 static int ocelot_initialize(struct ocelot_private *priv)
487 /* Initialize switch memories, enable core */
488 ret = ocelot_switch_init(priv);
492 * Disable port-to-port by switching
493 * Put fron ports in "port isolation modes" - i.e. they cant send
494 * to other ports - via the PGID sorce masks.
496 for (i = 0; i < MAX_PORT; i++)
497 writel(0, priv->regs[ANA] + ANA_PGID(PGID_SRC + i));
500 mscc_flush(priv->regs[QS], ocelot_regs_qs);
502 /* Setup frame ageing - "2 sec" - The unit is 6.5us on Ocelot */
503 writel(SYS_FRM_AGING_ENA | (20000000 / 65),
504 priv->regs[SYS] + SYS_FRM_AGING);
506 for (i = 0; i < MAX_PORT; i++)
507 ocelot_port_init(priv, i);
509 ocelot_cpu_capture_setup(priv);
511 debug("Ports enabled\n");
516 static int ocelot_write_hwaddr(struct udevice *dev)
518 struct ocelot_private *priv = dev_get_priv(dev);
519 struct eth_pdata *pdata = dev_get_platdata(dev);
521 mscc_mac_table_add(priv->regs[ANA], ocelot_regs_ana_table,
522 pdata->enetaddr, PGID_UNICAST);
524 writel(BIT(CPU_PORT), priv->regs[ANA] + ANA_PGID(PGID_UNICAST));
529 static int ocelot_start(struct udevice *dev)
531 struct ocelot_private *priv = dev_get_priv(dev);
532 struct eth_pdata *pdata = dev_get_platdata(dev);
533 const unsigned char mac[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff,
537 ret = ocelot_initialize(priv);
541 /* Set MAC address tables entries for CPU redirection */
542 mscc_mac_table_add(priv->regs[ANA], ocelot_regs_ana_table, mac,
545 writel(BIT(CPU_PORT) | INTERNAL_PORT_MSK,
546 priv->regs[ANA] + ANA_PGID(PGID_BROADCAST));
548 /* It should be setup latter in ocelot_write_hwaddr */
549 mscc_mac_table_add(priv->regs[ANA], ocelot_regs_ana_table,
550 pdata->enetaddr, PGID_UNICAST);
552 writel(BIT(CPU_PORT), priv->regs[ANA] + ANA_PGID(PGID_UNICAST));
557 static int ocelot_send(struct udevice *dev, void *packet, int length)
559 struct ocelot_private *priv = dev_get_priv(dev);
561 int port = BIT(0); /* use port 0 */
565 * Generate the IFH for frame injection
567 * The IFH is a 128bit-value
568 * bit 127: bypass the analyzer processing
569 * bit 56-67: destination mask
570 * bit 28-29: pop_cnt: 3 disables all rewriting of the frame
571 * bit 20-27: cpu extraction queue mask
572 * bit 16: tag type 0: C-tag, 1: S-tag
575 ifh[0] = IFH_INJ_BYPASS;
576 ifh[1] = (0xf00 & port) >> 8;
577 ifh[2] = (0xff & port) << 24;
578 ifh[3] = (IFH_TAG_TYPE_C << 16);
580 return mscc_send(priv->regs[QS], ocelot_regs_qs,
581 ifh, IFH_LEN, buf, length);
584 static int ocelot_recv(struct udevice *dev, int flags, uchar **packetp)
586 struct ocelot_private *priv = dev_get_priv(dev);
587 u32 *rxbuf = (u32 *)net_rx_packets[0];
590 byte_cnt = mscc_recv(priv->regs[QS], ocelot_regs_qs, rxbuf, IFH_LEN,
593 *packetp = net_rx_packets[0];
598 static struct mii_dev *get_mdiobus(phys_addr_t base, unsigned long size)
602 for (i = 0; i < OCELOT_MIIM_BUS_COUNT; ++i)
603 if (miim[i].miim_base == base && miim[i].miim_size == size)
609 static void add_port_entry(struct ocelot_private *priv, size_t index,
610 size_t phy_addr, struct mii_dev *bus,
611 u8 serdes_index, u8 phy_mode)
613 priv->ports[index].phy_addr = phy_addr;
614 priv->ports[index].bus = bus;
615 priv->ports[index].serdes_index = serdes_index;
616 priv->ports[index].phy_mode = phy_mode;
619 static int external_bus(struct ocelot_private *priv, size_t port_index)
621 return priv->ports[port_index].serdes_index != 0xff;
624 static int ocelot_probe(struct udevice *dev)
626 struct ocelot_private *priv = dev_get_priv(dev);
630 phys_addr_t addr_base;
631 unsigned long addr_size;
632 ofnode eth_node, node, mdio_node;
635 struct ofnode_phandle_args phandle;
636 struct phy_device *phy;
641 for (i = 0; i < ARRAY_SIZE(regs_names); i++) {
642 priv->regs[i] = dev_remap_addr_name(dev, regs_names[i]);
643 if (!priv->regs[i]) {
645 ("Error can't get regs base addresses for %s\n",
651 /* Initialize miim buses */
652 memset(&miim, 0x0, sizeof(struct mscc_miim_dev) *
653 OCELOT_MIIM_BUS_COUNT);
655 /* iterate all the ports and find out on which bus they are */
657 eth_node = dev_read_first_subnode(dev);
658 for (node = ofnode_first_subnode(eth_node); ofnode_valid(node);
659 node = ofnode_next_subnode(node)) {
660 if (ofnode_read_resource(node, 0, &res))
664 ofnode_parse_phandle_with_args(node, "phy-handle", NULL, 0, 0,
667 /* Get phy address on mdio bus */
668 if (ofnode_read_resource(phandle.node, 0, &res))
670 phy_addr = res.start;
673 mdio_node = ofnode_get_parent(phandle.node);
675 if (ofnode_read_resource(mdio_node, 0, &res))
677 faddr = cpu_to_fdt32(res.start);
679 addr_base = ofnode_translate_address(mdio_node, &faddr);
680 addr_size = res.end - res.start;
682 /* If the bus is new then create a new bus */
683 if (!get_mdiobus(addr_base, addr_size))
684 priv->bus[miim_count] =
685 ocelot_mdiobus_init(addr_base, addr_size);
687 /* Connect mdio bus with the port */
688 bus = get_mdiobus(addr_base, addr_size);
690 /* Get serdes info */
691 ret = ofnode_parse_phandle_with_args(node, "phys", NULL,
694 add_port_entry(priv, i, phy_addr, bus, 0xff, 0xff);
696 add_port_entry(priv, i, phy_addr, bus, phandle.args[1],
702 for (i = 0; i < MAX_PORT; i++) {
703 if (!priv->ports[i].bus)
706 phy = phy_connect(priv->ports[i].bus,
707 priv->ports[i].phy_addr, dev,
708 PHY_INTERFACE_MODE_NONE);
709 if (phy && external_bus(priv, i))
710 board_phy_config(phy);
716 static int ocelot_remove(struct udevice *dev)
718 struct ocelot_private *priv = dev_get_priv(dev);
721 for (i = 0; i < OCELOT_MIIM_BUS_COUNT; i++) {
722 mdio_unregister(priv->bus[i]);
723 mdio_free(priv->bus[i]);
729 static const struct eth_ops ocelot_ops = {
730 .start = ocelot_start,
734 .write_hwaddr = ocelot_write_hwaddr,
737 static const struct udevice_id mscc_ocelot_ids[] = {
738 {.compatible = "mscc,vsc7514-switch"},
742 U_BOOT_DRIVER(ocelot) = {
743 .name = "ocelot-switch",
745 .of_match = mscc_ocelot_ids,
746 .probe = ocelot_probe,
747 .remove = ocelot_remove,
749 .priv_auto_alloc_size = sizeof(struct ocelot_private),
750 .platdata_auto_alloc_size = sizeof(struct eth_pdata),