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_miim.h"
21 #define PHY_CFG_ENA 0xF
22 #define PHY_CFG_COMMON_RST BIT(4)
23 #define PHY_CFG_RST (0xF << 5)
25 #define PHY_STAT_SUPERVISOR_COMPLETE BIT(0)
27 #define ANA_PORT_VLAN_CFG(x) (0x7000 + 0x100 * (x))
28 #define ANA_PORT_VLAN_CFG_AWARE_ENA BIT(20)
29 #define ANA_PORT_VLAN_CFG_POP_CNT(x) ((x) << 18)
30 #define ANA_PORT_PORT_CFG(x) (0x7070 + 0x100 * (x))
31 #define ANA_PORT_PORT_CFG_RECV_ENA BIT(6)
32 #define ANA_TABLES_MACHDATA 0x8b34
33 #define ANA_TABLES_MACLDATA 0x8b38
34 #define ANA_TABLES_MACACCESS 0x8b3c
35 #define ANA_TABLES_MACACCESS_VALID BIT(11)
36 #define ANA_TABLES_MACACCESS_ENTRYTYPE(x) ((x) << 9)
37 #define ANA_TABLES_MACACCESS_DEST_IDX(x) ((x) << 3)
38 #define ANA_TABLES_MACACCESS_MAC_TABLE_CMD(x) (x)
39 #define ANA_TABLES_MACACCESS_MAC_TABLE_CMD_M GENMASK(2, 0)
40 #define MACACCESS_CMD_IDLE 0
41 #define MACACCESS_CMD_LEARN 1
42 #define MACACCESS_CMD_GET_NEXT 4
43 #define ANA_PGID(x) (0x8c00 + 4 * (x))
45 #define SYS_FRM_AGING 0x574
46 #define SYS_FRM_AGING_ENA BIT(20)
48 #define SYS_SYSTEM_RST_CFG 0x508
49 #define SYS_SYSTEM_RST_MEM_INIT BIT(0)
50 #define SYS_SYSTEM_RST_MEM_ENA BIT(1)
51 #define SYS_SYSTEM_RST_CORE_ENA BIT(2)
52 #define SYS_PORT_MODE(x) (0x514 + 0x4 * (x))
53 #define SYS_PORT_MODE_INCL_INJ_HDR(x) ((x) << 3)
54 #define SYS_PORT_MODE_INCL_INJ_HDR_M GENMASK(4, 3)
55 #define SYS_PORT_MODE_INCL_XTR_HDR(x) ((x) << 1)
56 #define SYS_PORT_MODE_INCL_XTR_HDR_M GENMASK(2, 1)
57 #define SYS_PAUSE_CFG(x) (0x608 + 0x4 * (x))
58 #define SYS_PAUSE_CFG_PAUSE_ENA BIT(0)
60 #define QSYS_SWITCH_PORT_MODE(x) (0x11234 + 0x4 * (x))
61 #define QSYS_SWITCH_PORT_MODE_PORT_ENA BIT(14)
62 #define QSYS_QMAP 0x112d8
63 #define QSYS_EGR_NO_SHARING 0x1129c
66 #define DEV_CLOCK_CFG 0x0
67 #define DEV_CLOCK_CFG_LINK_SPEED_1000 1
68 #define DEV_MAC_ENA_CFG 0x1c
69 #define DEV_MAC_ENA_CFG_RX_ENA BIT(4)
70 #define DEV_MAC_ENA_CFG_TX_ENA BIT(0)
72 #define DEV_MAC_IFG_CFG 0x30
73 #define DEV_MAC_IFG_CFG_TX_IFG(x) ((x) << 8)
74 #define DEV_MAC_IFG_CFG_RX_IFG2(x) ((x) << 4)
75 #define DEV_MAC_IFG_CFG_RX_IFG1(x) (x)
77 #define PCS1G_CFG 0x48
78 #define PCS1G_MODE_CFG_SGMII_MODE_ENA BIT(0)
79 #define PCS1G_MODE_CFG 0x4c
80 #define PCS1G_MODE_CFG_UNIDIR_MODE_ENA BIT(4)
81 #define PCS1G_MODE_CFG_SGMII_MODE_ENA BIT(0)
82 #define PCS1G_SD_CFG 0x50
83 #define PCS1G_ANEG_CFG 0x54
84 #define PCS1G_ANEG_CFG_ADV_ABILITY(x) ((x) << 16)
86 #define QS_XTR_GRP_CFG(x) (4 * (x))
87 #define QS_XTR_GRP_CFG_MODE(x) ((x) << 2)
88 #define QS_XTR_GRP_CFG_STATUS_WORD_POS BIT(1)
89 #define QS_XTR_GRP_CFG_BYTE_SWAP BIT(0)
90 #define QS_XTR_RD(x) (0x8 + 4 * (x))
91 #define QS_XTR_FLUSH 0x18
92 #define QS_XTR_FLUSH_FLUSH GENMASK(1, 0)
93 #define QS_XTR_DATA_PRESENT 0x1c
94 #define QS_INJ_GRP_CFG(x) (0x24 + (x) * 4)
95 #define QS_INJ_GRP_CFG_MODE(x) ((x) << 2)
96 #define QS_INJ_GRP_CFG_BYTE_SWAP BIT(0)
97 #define QS_INJ_WR(x) (0x2c + 4 * (x))
98 #define QS_INJ_CTRL(x) (0x34 + 4 * (x))
99 #define QS_INJ_CTRL_GAP_SIZE(x) ((x) << 21)
100 #define QS_INJ_CTRL_EOF BIT(19)
101 #define QS_INJ_CTRL_SOF BIT(18)
102 #define QS_INJ_CTRL_VLD_BYTES(x) ((x) << 16)
104 #define XTR_EOF_0 ntohl(0x80000000u)
105 #define XTR_EOF_1 ntohl(0x80000001u)
106 #define XTR_EOF_2 ntohl(0x80000002u)
107 #define XTR_EOF_3 ntohl(0x80000003u)
108 #define XTR_PRUNED ntohl(0x80000004u)
109 #define XTR_ABORT ntohl(0x80000005u)
110 #define XTR_ESCAPE ntohl(0x80000006u)
111 #define XTR_NOT_READY ntohl(0x80000007u)
113 #define IFH_INJ_BYPASS BIT(31)
114 #define IFH_TAG_TYPE_C 0
115 #define XTR_VALID_BYTES(x) (4 - ((x) & 3))
118 #define INTERNAL_PORT_MSK 0xF
120 #define OCELOT_BUF_CELL_SZ 60
122 #define PGID_BROADCAST 13
123 #define PGID_UNICAST 14
140 #define MAX_PORT (PORT3 - PORT0)
142 /* MAC table entry types.
143 * ENTRYTYPE_NORMAL is subject to aging.
144 * ENTRYTYPE_LOCKED is not subject to aging.
145 * ENTRYTYPE_MACv4 is not subject to aging. For IPv4 multicast.
146 * ENTRYTYPE_MACv6 is not subject to aging. For IPv6 multicast.
148 enum macaccess_entry_type {
149 ENTRYTYPE_NORMAL = 0,
155 enum ocelot_mdio_target {
167 struct ocelot_private {
168 void __iomem *regs[TARGET_MAX];
170 struct mii_dev *bus[NUM_PHY];
171 struct phy_device *phydev;
180 u8 tx_adj_packetbuf[PKTSIZE_ALIGN + PKTALIGN];
184 struct mscc_miim_dev miim[NUM_PHY];
186 static int mscc_miim_reset(struct mii_dev *bus)
188 struct mscc_miim_dev *miim = (struct mscc_miim_dev *)bus->priv;
190 if (miim->phy_regs) {
191 writel(0, miim->phy_regs + PHY_CFG);
192 writel(PHY_CFG_RST | PHY_CFG_COMMON_RST
193 | PHY_CFG_ENA, miim->phy_regs + PHY_CFG);
200 /* For now only setup the internal mdio bus */
201 static struct mii_dev *ocelot_mdiobus_init(struct udevice *dev)
203 unsigned long phy_size[TARGET_MAX];
204 phys_addr_t phy_base[TARGET_MAX];
205 struct ofnode_phandle_args phandle;
206 ofnode eth_node, node, mdio_node;
217 /* gathered only the first mdio bus */
218 eth_node = dev_read_first_subnode(dev);
219 node = ofnode_first_subnode(eth_node);
220 ofnode_parse_phandle_with_args(node, "phy-handle", NULL, 0, 0,
222 mdio_node = ofnode_get_parent(phandle.node);
224 for (i = 0; i < TARGET_MDIO_MAX; i++) {
225 if (ofnode_read_resource(mdio_node, i, &res)) {
226 pr_err("%s: get OF resource failed\n", __func__);
229 faddr = cpu_to_fdt32(res.start);
230 phy_base[i] = ofnode_translate_address(mdio_node, &faddr);
231 phy_size[i] = res.end - res.start;
234 strcpy(bus->name, "miim-internal");
235 miim[INTERNAL].phy_regs = ioremap(phy_base[PHY], phy_size[PHY]);
236 miim[INTERNAL].regs = ioremap(phy_base[MIIM], phy_size[MIIM]);
237 bus->priv = &miim[INTERNAL];
238 bus->reset = mscc_miim_reset;
239 bus->read = mscc_miim_read;
240 bus->write = mscc_miim_write;
242 if (mdio_register(bus))
248 __weak void mscc_switch_reset(void)
252 static void ocelot_stop(struct udevice *dev)
254 struct ocelot_private *priv = dev_get_priv(dev);
258 for (i = 0; i < NUM_PHY; i++)
260 mscc_miim_reset(priv->bus[i]);
263 static void ocelot_cpu_capture_setup(struct ocelot_private *priv)
267 /* map the 8 CPU extraction queues to CPU port 11 */
268 writel(0, priv->regs[QSYS] + QSYS_QMAP);
270 for (i = 0; i <= 1; i++) {
272 * Do byte-swap and expect status after last data word
273 * Extraction: Mode: manual extraction) | Byte_swap
275 writel(QS_XTR_GRP_CFG_MODE(1) | QS_XTR_GRP_CFG_BYTE_SWAP,
276 priv->regs[QS] + QS_XTR_GRP_CFG(i));
278 * Injection: Mode: manual extraction | Byte_swap
280 writel(QS_INJ_GRP_CFG_MODE(1) | QS_INJ_GRP_CFG_BYTE_SWAP,
281 priv->regs[QS] + QS_INJ_GRP_CFG(i));
284 for (i = 0; i <= 1; i++)
285 /* Enable IFH insertion/parsing on CPU ports */
286 writel(SYS_PORT_MODE_INCL_INJ_HDR(1) |
287 SYS_PORT_MODE_INCL_XTR_HDR(1),
288 priv->regs[SYS] + SYS_PORT_MODE(CPU_PORT + i));
290 * Setup the CPU port as VLAN aware to support switching frames
293 writel(ANA_PORT_VLAN_CFG_AWARE_ENA | ANA_PORT_VLAN_CFG_POP_CNT(1) |
294 MAC_VID, priv->regs[ANA] + ANA_PORT_VLAN_CFG(CPU_PORT));
296 /* Disable learning (only RECV_ENA must be set) */
297 writel(ANA_PORT_PORT_CFG_RECV_ENA,
298 priv->regs[ANA] + ANA_PORT_PORT_CFG(CPU_PORT));
300 /* Enable switching to/from cpu port */
301 setbits_le32(priv->regs[QSYS] + QSYS_SWITCH_PORT_MODE(CPU_PORT),
302 QSYS_SWITCH_PORT_MODE_PORT_ENA);
304 /* No pause on CPU port - not needed (off by default) */
305 clrbits_le32(priv->regs[SYS] + SYS_PAUSE_CFG(CPU_PORT),
306 SYS_PAUSE_CFG_PAUSE_ENA);
308 setbits_le32(priv->regs[QSYS] + QSYS_EGR_NO_SHARING, BIT(CPU_PORT));
311 static void ocelot_port_init(struct ocelot_private *priv, int port)
313 void __iomem *regs = priv->regs[port];
316 writel(PCS1G_MODE_CFG_SGMII_MODE_ENA, regs + PCS1G_CFG);
318 /* Disable Signal Detect */
319 writel(0, regs + PCS1G_SD_CFG);
321 /* Enable MAC RX and TX */
322 writel(DEV_MAC_ENA_CFG_RX_ENA | DEV_MAC_ENA_CFG_TX_ENA,
323 regs + DEV_MAC_ENA_CFG);
325 /* Clear sgmii_mode_ena */
326 writel(0, regs + PCS1G_MODE_CFG);
329 * Clear sw_resolve_ena(bit 0) and set adv_ability to
330 * something meaningful just in case
332 writel(PCS1G_ANEG_CFG_ADV_ABILITY(0x20), regs + PCS1G_ANEG_CFG);
334 /* Set MAC IFG Gaps */
335 writel(DEV_MAC_IFG_CFG_TX_IFG(5) | DEV_MAC_IFG_CFG_RX_IFG1(5) |
336 DEV_MAC_IFG_CFG_RX_IFG2(1), regs + DEV_MAC_IFG_CFG);
338 /* Set link speed and release all resets */
339 writel(DEV_CLOCK_CFG_LINK_SPEED_1000, regs + DEV_CLOCK_CFG);
341 /* Make VLAN aware for CPU traffic */
342 writel(ANA_PORT_VLAN_CFG_AWARE_ENA | ANA_PORT_VLAN_CFG_POP_CNT(1) |
343 MAC_VID, priv->regs[ANA] + ANA_PORT_VLAN_CFG(port - PORT0));
345 /* Enable the port in the core */
346 setbits_le32(priv->regs[QSYS] + QSYS_SWITCH_PORT_MODE(port - PORT0),
347 QSYS_SWITCH_PORT_MODE_PORT_ENA);
350 static int ocelot_switch_init(struct ocelot_private *priv)
352 /* Reset switch & memories */
353 writel(SYS_SYSTEM_RST_MEM_ENA | SYS_SYSTEM_RST_MEM_INIT,
354 priv->regs[SYS] + SYS_SYSTEM_RST_CFG);
356 /* Wait to complete */
357 if (wait_for_bit_le32(priv->regs[SYS] + SYS_SYSTEM_RST_CFG,
358 SYS_SYSTEM_RST_MEM_INIT, false, 2000, false)) {
359 pr_err("Timeout in memory reset\n");
363 /* Enable switch core */
364 setbits_le32(priv->regs[SYS] + SYS_SYSTEM_RST_CFG,
365 SYS_SYSTEM_RST_CORE_ENA);
370 static void ocelot_switch_flush(struct ocelot_private *priv)
372 /* All Queues flush */
373 setbits_le32(priv->regs[QS] + QS_XTR_FLUSH, QS_XTR_FLUSH_FLUSH);
376 /* All Queues normal */
377 clrbits_le32(priv->regs[QS] + QS_XTR_FLUSH, QS_XTR_FLUSH_FLUSH);
380 static int ocelot_initialize(struct ocelot_private *priv)
384 /* Initialize switch memories, enable core */
385 ret = ocelot_switch_init(priv);
389 * Disable port-to-port by switching
390 * Put fron ports in "port isolation modes" - i.e. they cant send
391 * to other ports - via the PGID sorce masks.
393 for (i = 0; i <= MAX_PORT; i++)
394 writel(0, priv->regs[ANA] + ANA_PGID(PGID_SRC + i));
397 ocelot_switch_flush(priv);
399 /* Setup frame ageing - "2 sec" - The unit is 6.5us on Ocelot */
400 writel(SYS_FRM_AGING_ENA | (20000000 / 65),
401 priv->regs[SYS] + SYS_FRM_AGING);
403 for (i = PORT0; i <= PORT3; i++)
404 ocelot_port_init(priv, i);
406 ocelot_cpu_capture_setup(priv);
408 debug("Ports enabled\n");
413 static inline int ocelot_vlant_wait_for_completion(struct ocelot_private *priv)
415 unsigned int val, timeout = 10;
417 /* Wait for the issued mac table command to be completed, or timeout.
418 * When the command read from ANA_TABLES_MACACCESS is
419 * MACACCESS_CMD_IDLE, the issued command completed successfully.
422 val = readl(priv->regs[ANA] + ANA_TABLES_MACACCESS);
423 val &= ANA_TABLES_MACACCESS_MAC_TABLE_CMD_M;
424 } while (val != MACACCESS_CMD_IDLE && timeout--);
432 static int ocelot_mac_table_add(struct ocelot_private *priv,
433 const unsigned char mac[ETH_ALEN], int pgid)
435 u32 macl = 0, mach = 0;
438 /* Set the MAC address to handle and the vlan associated in a format
439 * understood by the hardware.
441 mach |= MAC_VID << 16;
442 mach |= ((u32)mac[0]) << 8;
443 mach |= ((u32)mac[1]) << 0;
444 macl |= ((u32)mac[2]) << 24;
445 macl |= ((u32)mac[3]) << 16;
446 macl |= ((u32)mac[4]) << 8;
447 macl |= ((u32)mac[5]) << 0;
449 writel(macl, priv->regs[ANA] + ANA_TABLES_MACLDATA);
450 writel(mach, priv->regs[ANA] + ANA_TABLES_MACHDATA);
452 writel(ANA_TABLES_MACACCESS_VALID |
453 ANA_TABLES_MACACCESS_DEST_IDX(pgid) |
454 ANA_TABLES_MACACCESS_ENTRYTYPE(ENTRYTYPE_LOCKED) |
455 ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_LEARN),
456 priv->regs[ANA] + ANA_TABLES_MACACCESS);
458 ret = ocelot_vlant_wait_for_completion(priv);
463 static int ocelot_write_hwaddr(struct udevice *dev)
465 struct ocelot_private *priv = dev_get_priv(dev);
466 struct eth_pdata *pdata = dev_get_platdata(dev);
468 ocelot_mac_table_add(priv, pdata->enetaddr, PGID_UNICAST);
470 writel(BIT(CPU_PORT), priv->regs[ANA] + ANA_PGID(PGID_UNICAST));
475 static int ocelot_start(struct udevice *dev)
477 struct ocelot_private *priv = dev_get_priv(dev);
478 struct eth_pdata *pdata = dev_get_platdata(dev);
479 const unsigned char mac[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff,
483 ret = ocelot_initialize(priv);
487 /* Set MAC address tables entries for CPU redirection */
488 ocelot_mac_table_add(priv, mac, PGID_BROADCAST);
490 writel(BIT(CPU_PORT) | INTERNAL_PORT_MSK,
491 priv->regs[ANA] + ANA_PGID(PGID_BROADCAST));
493 /* It should be setup latter in ocelot_write_hwaddr */
494 ocelot_mac_table_add(priv, pdata->enetaddr, PGID_UNICAST);
496 writel(BIT(CPU_PORT), priv->regs[ANA] + ANA_PGID(PGID_UNICAST));
501 static int ocelot_send(struct udevice *dev, void *packet, int length)
503 struct ocelot_private *priv = dev_get_priv(dev);
505 int port = BIT(0); /* use port 0 */
506 u8 grp = 0; /* Send everything on CPU group 0 */
507 int i, count = (length + 3) / 4, last = length % 4;
510 writel(QS_INJ_CTRL_GAP_SIZE(1) | QS_INJ_CTRL_SOF,
511 priv->regs[QS] + QS_INJ_CTRL(grp));
514 * Generate the IFH for frame injection
516 * The IFH is a 128bit-value
517 * bit 127: bypass the analyzer processing
518 * bit 56-67: destination mask
519 * bit 28-29: pop_cnt: 3 disables all rewriting of the frame
520 * bit 20-27: cpu extraction queue mask
521 * bit 16: tag type 0: C-tag, 1: S-tag
524 ifh[0] = IFH_INJ_BYPASS;
525 ifh[1] = (0xf00 & port) >> 8;
526 ifh[2] = (0xff & port) << 24;
527 ifh[3] = (IFH_TAG_TYPE_C << 16);
529 for (i = 0; i < IFH_LEN; i++)
530 writel(ifh[i], priv->regs[QS] + QS_INJ_WR(grp));
532 for (i = 0; i < count; i++)
533 writel(buf[i], priv->regs[QS] + QS_INJ_WR(grp));
536 while (i < (OCELOT_BUF_CELL_SZ / 4)) {
537 writel(0, priv->regs[QS] + QS_INJ_WR(grp));
541 /* Indicate EOF and valid bytes in last word */
542 writel(QS_INJ_CTRL_GAP_SIZE(1) |
543 QS_INJ_CTRL_VLD_BYTES(length < OCELOT_BUF_CELL_SZ ? 0 : last) |
544 QS_INJ_CTRL_EOF, priv->regs[QS] + QS_INJ_CTRL(grp));
547 writel(0, priv->regs[QS] + QS_INJ_WR(grp));
552 static int ocelot_recv(struct udevice *dev, int flags, uchar **packetp)
554 struct ocelot_private *priv = dev_get_priv(dev);
555 u8 grp = 0; /* Send everything on CPU group 0 */
556 u32 *rxbuf = (u32 *)net_rx_packets[0];
558 bool eof_flag = false, pruned_flag = false, abort_flag = false;
560 if (!(readl(priv->regs[QS] + QS_XTR_DATA_PRESENT) & BIT(grp)))
564 for (i = 0; i < IFH_LEN; i++)
565 readl(priv->regs[QS] + QS_XTR_RD(grp));
568 u32 val = readl(priv->regs[QS] + QS_XTR_RD(grp));
572 debug("%d NOT_READY...?\n", byte_cnt);
575 /* really nedeed?? not done in linux */
576 *rxbuf = readl(priv->regs[QS] + QS_XTR_RD(grp));
579 debug("XTR_ABORT\n");
585 byte_cnt += XTR_VALID_BYTES(val);
586 *rxbuf = readl(priv->regs[QS] + QS_XTR_RD(grp));
591 /* But get the last 4 bytes as well */
597 *rxbuf = readl(priv->regs[QS] + QS_XTR_RD(grp));
609 if (abort_flag || pruned_flag || !eof_flag) {
610 debug("Discarded frame: abort:%d pruned:%d eof:%d\n",
611 abort_flag, pruned_flag, eof_flag);
615 *packetp = net_rx_packets[0];
620 static int ocelot_probe(struct udevice *dev)
622 struct ocelot_private *priv = dev_get_priv(dev);
626 enum ocelot_target id;
641 for (i = 0; i < ARRAY_SIZE(reg); i++) {
642 priv->regs[reg[i].id] = dev_remap_addr_name(dev, reg[i].name);
643 if (!priv->regs[reg[i].id]) {
645 ("Error %d: can't get regs base addresses for %s\n",
651 priv->bus[INTERNAL] = ocelot_mdiobus_init(dev);
653 for (i = 0; i < 4; i++) {
654 phy_connect(priv->bus[INTERNAL], i, dev,
655 PHY_INTERFACE_MODE_NONE);
661 static int ocelot_remove(struct udevice *dev)
663 struct ocelot_private *priv = dev_get_priv(dev);
666 for (i = 0; i < NUM_PHY; i++) {
667 mdio_unregister(priv->bus[i]);
668 mdio_free(priv->bus[i]);
674 static const struct eth_ops ocelot_ops = {
675 .start = ocelot_start,
679 .write_hwaddr = ocelot_write_hwaddr,
682 static const struct udevice_id mscc_ocelot_ids[] = {
683 {.compatible = "mscc,vsc7514-switch"},
687 U_BOOT_DRIVER(ocelot) = {
688 .name = "ocelot-switch",
690 .of_match = mscc_ocelot_ids,
691 .probe = ocelot_probe,
692 .remove = ocelot_remove,
694 .priv_auto_alloc_size = sizeof(struct ocelot_private),
695 .platdata_auto_alloc_size = sizeof(struct eth_pdata),