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"
19 #include "mscc_xfer.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 ANA_PORT_VLAN_CFG(x) (0x7000 + 0x100 * (x))
29 #define ANA_PORT_VLAN_CFG_AWARE_ENA BIT(20)
30 #define ANA_PORT_VLAN_CFG_POP_CNT(x) ((x) << 18)
31 #define ANA_PORT_PORT_CFG(x) (0x7070 + 0x100 * (x))
32 #define ANA_PORT_PORT_CFG_RECV_ENA BIT(6)
33 #define ANA_TABLES_MACHDATA 0x8b34
34 #define ANA_TABLES_MACLDATA 0x8b38
35 #define ANA_TABLES_MACACCESS 0x8b3c
36 #define ANA_TABLES_MACACCESS_VALID BIT(11)
37 #define ANA_TABLES_MACACCESS_ENTRYTYPE(x) ((x) << 9)
38 #define ANA_TABLES_MACACCESS_DEST_IDX(x) ((x) << 3)
39 #define ANA_TABLES_MACACCESS_MAC_TABLE_CMD(x) (x)
40 #define ANA_TABLES_MACACCESS_MAC_TABLE_CMD_M GENMASK(2, 0)
41 #define MACACCESS_CMD_IDLE 0
42 #define MACACCESS_CMD_LEARN 1
43 #define MACACCESS_CMD_GET_NEXT 4
44 #define ANA_PGID(x) (0x8c00 + 4 * (x))
46 #define SYS_FRM_AGING 0x574
47 #define SYS_FRM_AGING_ENA BIT(20)
49 #define SYS_SYSTEM_RST_CFG 0x508
50 #define SYS_SYSTEM_RST_MEM_INIT BIT(0)
51 #define SYS_SYSTEM_RST_MEM_ENA BIT(1)
52 #define SYS_SYSTEM_RST_CORE_ENA BIT(2)
53 #define SYS_PORT_MODE(x) (0x514 + 0x4 * (x))
54 #define SYS_PORT_MODE_INCL_INJ_HDR(x) ((x) << 3)
55 #define SYS_PORT_MODE_INCL_INJ_HDR_M GENMASK(4, 3)
56 #define SYS_PORT_MODE_INCL_XTR_HDR(x) ((x) << 1)
57 #define SYS_PORT_MODE_INCL_XTR_HDR_M GENMASK(2, 1)
58 #define SYS_PAUSE_CFG(x) (0x608 + 0x4 * (x))
59 #define SYS_PAUSE_CFG_PAUSE_ENA BIT(0)
61 #define QSYS_SWITCH_PORT_MODE(x) (0x11234 + 0x4 * (x))
62 #define QSYS_SWITCH_PORT_MODE_PORT_ENA BIT(14)
63 #define QSYS_QMAP 0x112d8
64 #define QSYS_EGR_NO_SHARING 0x1129c
67 #define DEV_CLOCK_CFG 0x0
68 #define DEV_CLOCK_CFG_LINK_SPEED_1000 1
69 #define DEV_MAC_ENA_CFG 0x1c
70 #define DEV_MAC_ENA_CFG_RX_ENA BIT(4)
71 #define DEV_MAC_ENA_CFG_TX_ENA BIT(0)
73 #define DEV_MAC_IFG_CFG 0x30
74 #define DEV_MAC_IFG_CFG_TX_IFG(x) ((x) << 8)
75 #define DEV_MAC_IFG_CFG_RX_IFG2(x) ((x) << 4)
76 #define DEV_MAC_IFG_CFG_RX_IFG1(x) (x)
78 #define PCS1G_CFG 0x48
79 #define PCS1G_MODE_CFG_SGMII_MODE_ENA BIT(0)
80 #define PCS1G_MODE_CFG 0x4c
81 #define PCS1G_MODE_CFG_UNIDIR_MODE_ENA BIT(4)
82 #define PCS1G_MODE_CFG_SGMII_MODE_ENA BIT(0)
83 #define PCS1G_SD_CFG 0x50
84 #define PCS1G_ANEG_CFG 0x54
85 #define PCS1G_ANEG_CFG_ADV_ABILITY(x) ((x) << 16)
87 #define QS_XTR_GRP_CFG(x) (4 * (x))
88 #define QS_XTR_GRP_CFG_MODE(x) ((x) << 2)
89 #define QS_XTR_GRP_CFG_STATUS_WORD_POS BIT(1)
90 #define QS_XTR_GRP_CFG_BYTE_SWAP BIT(0)
91 #define QS_INJ_GRP_CFG(x) (0x24 + (x) * 4)
92 #define QS_INJ_GRP_CFG_MODE(x) ((x) << 2)
93 #define QS_INJ_GRP_CFG_BYTE_SWAP BIT(0)
95 #define IFH_INJ_BYPASS BIT(31)
96 #define IFH_TAG_TYPE_C 0
99 #define INTERNAL_PORT_MSK 0xF
102 #define PGID_BROADCAST 13
103 #define PGID_UNICAST 14
120 #define MAX_PORT (PORT3 - PORT0)
122 /* MAC table entry types.
123 * ENTRYTYPE_NORMAL is subject to aging.
124 * ENTRYTYPE_LOCKED is not subject to aging.
125 * ENTRYTYPE_MACv4 is not subject to aging. For IPv4 multicast.
126 * ENTRYTYPE_MACv6 is not subject to aging. For IPv6 multicast.
128 enum macaccess_entry_type {
129 ENTRYTYPE_NORMAL = 0,
135 enum ocelot_mdio_target {
147 struct ocelot_private {
148 void __iomem *regs[TARGET_MAX];
150 struct mii_dev *bus[NUM_PHY];
151 struct phy_device *phydev;
160 u8 tx_adj_packetbuf[PKTSIZE_ALIGN + PKTALIGN];
164 static const unsigned long ocelot_regs_qs[] = {
165 [MSCC_QS_XTR_RD] = 0x8,
166 [MSCC_QS_XTR_FLUSH] = 0x18,
167 [MSCC_QS_XTR_DATA_PRESENT] = 0x1c,
168 [MSCC_QS_INJ_WR] = 0x2c,
169 [MSCC_QS_INJ_CTRL] = 0x34,
172 struct mscc_miim_dev miim[NUM_PHY];
174 static int mscc_miim_reset(struct mii_dev *bus)
176 struct mscc_miim_dev *miim = (struct mscc_miim_dev *)bus->priv;
178 if (miim->phy_regs) {
179 writel(0, miim->phy_regs + PHY_CFG);
180 writel(PHY_CFG_RST | PHY_CFG_COMMON_RST
181 | PHY_CFG_ENA, miim->phy_regs + PHY_CFG);
188 /* For now only setup the internal mdio bus */
189 static struct mii_dev *ocelot_mdiobus_init(struct udevice *dev)
191 unsigned long phy_size[TARGET_MAX];
192 phys_addr_t phy_base[TARGET_MAX];
193 struct ofnode_phandle_args phandle;
194 ofnode eth_node, node, mdio_node;
205 /* gathered only the first mdio bus */
206 eth_node = dev_read_first_subnode(dev);
207 node = ofnode_first_subnode(eth_node);
208 ofnode_parse_phandle_with_args(node, "phy-handle", NULL, 0, 0,
210 mdio_node = ofnode_get_parent(phandle.node);
212 for (i = 0; i < TARGET_MDIO_MAX; i++) {
213 if (ofnode_read_resource(mdio_node, i, &res)) {
214 pr_err("%s: get OF resource failed\n", __func__);
217 faddr = cpu_to_fdt32(res.start);
218 phy_base[i] = ofnode_translate_address(mdio_node, &faddr);
219 phy_size[i] = res.end - res.start;
222 strcpy(bus->name, "miim-internal");
223 miim[INTERNAL].phy_regs = ioremap(phy_base[PHY], phy_size[PHY]);
224 miim[INTERNAL].regs = ioremap(phy_base[MIIM], phy_size[MIIM]);
225 bus->priv = &miim[INTERNAL];
226 bus->reset = mscc_miim_reset;
227 bus->read = mscc_miim_read;
228 bus->write = mscc_miim_write;
230 if (mdio_register(bus))
236 __weak void mscc_switch_reset(void)
240 static void ocelot_stop(struct udevice *dev)
242 struct ocelot_private *priv = dev_get_priv(dev);
246 for (i = 0; i < NUM_PHY; i++)
248 mscc_miim_reset(priv->bus[i]);
251 static void ocelot_cpu_capture_setup(struct ocelot_private *priv)
255 /* map the 8 CPU extraction queues to CPU port 11 */
256 writel(0, priv->regs[QSYS] + QSYS_QMAP);
258 for (i = 0; i <= 1; i++) {
260 * Do byte-swap and expect status after last data word
261 * Extraction: Mode: manual extraction) | Byte_swap
263 writel(QS_XTR_GRP_CFG_MODE(1) | QS_XTR_GRP_CFG_BYTE_SWAP,
264 priv->regs[QS] + QS_XTR_GRP_CFG(i));
266 * Injection: Mode: manual extraction | Byte_swap
268 writel(QS_INJ_GRP_CFG_MODE(1) | QS_INJ_GRP_CFG_BYTE_SWAP,
269 priv->regs[QS] + QS_INJ_GRP_CFG(i));
272 for (i = 0; i <= 1; i++)
273 /* Enable IFH insertion/parsing on CPU ports */
274 writel(SYS_PORT_MODE_INCL_INJ_HDR(1) |
275 SYS_PORT_MODE_INCL_XTR_HDR(1),
276 priv->regs[SYS] + SYS_PORT_MODE(CPU_PORT + i));
278 * Setup the CPU port as VLAN aware to support switching frames
281 writel(ANA_PORT_VLAN_CFG_AWARE_ENA | ANA_PORT_VLAN_CFG_POP_CNT(1) |
282 MAC_VID, priv->regs[ANA] + ANA_PORT_VLAN_CFG(CPU_PORT));
284 /* Disable learning (only RECV_ENA must be set) */
285 writel(ANA_PORT_PORT_CFG_RECV_ENA,
286 priv->regs[ANA] + ANA_PORT_PORT_CFG(CPU_PORT));
288 /* Enable switching to/from cpu port */
289 setbits_le32(priv->regs[QSYS] + QSYS_SWITCH_PORT_MODE(CPU_PORT),
290 QSYS_SWITCH_PORT_MODE_PORT_ENA);
292 /* No pause on CPU port - not needed (off by default) */
293 clrbits_le32(priv->regs[SYS] + SYS_PAUSE_CFG(CPU_PORT),
294 SYS_PAUSE_CFG_PAUSE_ENA);
296 setbits_le32(priv->regs[QSYS] + QSYS_EGR_NO_SHARING, BIT(CPU_PORT));
299 static void ocelot_port_init(struct ocelot_private *priv, int port)
301 void __iomem *regs = priv->regs[port];
304 writel(PCS1G_MODE_CFG_SGMII_MODE_ENA, regs + PCS1G_CFG);
306 /* Disable Signal Detect */
307 writel(0, regs + PCS1G_SD_CFG);
309 /* Enable MAC RX and TX */
310 writel(DEV_MAC_ENA_CFG_RX_ENA | DEV_MAC_ENA_CFG_TX_ENA,
311 regs + DEV_MAC_ENA_CFG);
313 /* Clear sgmii_mode_ena */
314 writel(0, regs + PCS1G_MODE_CFG);
317 * Clear sw_resolve_ena(bit 0) and set adv_ability to
318 * something meaningful just in case
320 writel(PCS1G_ANEG_CFG_ADV_ABILITY(0x20), regs + PCS1G_ANEG_CFG);
322 /* Set MAC IFG Gaps */
323 writel(DEV_MAC_IFG_CFG_TX_IFG(5) | DEV_MAC_IFG_CFG_RX_IFG1(5) |
324 DEV_MAC_IFG_CFG_RX_IFG2(1), regs + DEV_MAC_IFG_CFG);
326 /* Set link speed and release all resets */
327 writel(DEV_CLOCK_CFG_LINK_SPEED_1000, regs + DEV_CLOCK_CFG);
329 /* Make VLAN aware for CPU traffic */
330 writel(ANA_PORT_VLAN_CFG_AWARE_ENA | ANA_PORT_VLAN_CFG_POP_CNT(1) |
331 MAC_VID, priv->regs[ANA] + ANA_PORT_VLAN_CFG(port - PORT0));
333 /* Enable the port in the core */
334 setbits_le32(priv->regs[QSYS] + QSYS_SWITCH_PORT_MODE(port - PORT0),
335 QSYS_SWITCH_PORT_MODE_PORT_ENA);
338 static int ocelot_switch_init(struct ocelot_private *priv)
340 /* Reset switch & memories */
341 writel(SYS_SYSTEM_RST_MEM_ENA | SYS_SYSTEM_RST_MEM_INIT,
342 priv->regs[SYS] + SYS_SYSTEM_RST_CFG);
344 /* Wait to complete */
345 if (wait_for_bit_le32(priv->regs[SYS] + SYS_SYSTEM_RST_CFG,
346 SYS_SYSTEM_RST_MEM_INIT, false, 2000, false)) {
347 pr_err("Timeout in memory reset\n");
351 /* Enable switch core */
352 setbits_le32(priv->regs[SYS] + SYS_SYSTEM_RST_CFG,
353 SYS_SYSTEM_RST_CORE_ENA);
358 static int ocelot_initialize(struct ocelot_private *priv)
362 /* Initialize switch memories, enable core */
363 ret = ocelot_switch_init(priv);
367 * Disable port-to-port by switching
368 * Put fron ports in "port isolation modes" - i.e. they cant send
369 * to other ports - via the PGID sorce masks.
371 for (i = 0; i <= MAX_PORT; i++)
372 writel(0, priv->regs[ANA] + ANA_PGID(PGID_SRC + i));
375 mscc_flush(priv->regs[QS], ocelot_regs_qs);
377 /* Setup frame ageing - "2 sec" - The unit is 6.5us on Ocelot */
378 writel(SYS_FRM_AGING_ENA | (20000000 / 65),
379 priv->regs[SYS] + SYS_FRM_AGING);
381 for (i = PORT0; i <= PORT3; i++)
382 ocelot_port_init(priv, i);
384 ocelot_cpu_capture_setup(priv);
386 debug("Ports enabled\n");
391 static inline int ocelot_vlant_wait_for_completion(struct ocelot_private *priv)
393 unsigned int val, timeout = 10;
395 /* Wait for the issued mac table command to be completed, or timeout.
396 * When the command read from ANA_TABLES_MACACCESS is
397 * MACACCESS_CMD_IDLE, the issued command completed successfully.
400 val = readl(priv->regs[ANA] + ANA_TABLES_MACACCESS);
401 val &= ANA_TABLES_MACACCESS_MAC_TABLE_CMD_M;
402 } while (val != MACACCESS_CMD_IDLE && timeout--);
410 static int ocelot_mac_table_add(struct ocelot_private *priv,
411 const unsigned char mac[ETH_ALEN], int pgid)
413 u32 macl = 0, mach = 0;
416 /* Set the MAC address to handle and the vlan associated in a format
417 * understood by the hardware.
419 mach |= MAC_VID << 16;
420 mach |= ((u32)mac[0]) << 8;
421 mach |= ((u32)mac[1]) << 0;
422 macl |= ((u32)mac[2]) << 24;
423 macl |= ((u32)mac[3]) << 16;
424 macl |= ((u32)mac[4]) << 8;
425 macl |= ((u32)mac[5]) << 0;
427 writel(macl, priv->regs[ANA] + ANA_TABLES_MACLDATA);
428 writel(mach, priv->regs[ANA] + ANA_TABLES_MACHDATA);
430 writel(ANA_TABLES_MACACCESS_VALID |
431 ANA_TABLES_MACACCESS_DEST_IDX(pgid) |
432 ANA_TABLES_MACACCESS_ENTRYTYPE(ENTRYTYPE_LOCKED) |
433 ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_LEARN),
434 priv->regs[ANA] + ANA_TABLES_MACACCESS);
436 ret = ocelot_vlant_wait_for_completion(priv);
441 static int ocelot_write_hwaddr(struct udevice *dev)
443 struct ocelot_private *priv = dev_get_priv(dev);
444 struct eth_pdata *pdata = dev_get_platdata(dev);
446 ocelot_mac_table_add(priv, pdata->enetaddr, PGID_UNICAST);
448 writel(BIT(CPU_PORT), priv->regs[ANA] + ANA_PGID(PGID_UNICAST));
453 static int ocelot_start(struct udevice *dev)
455 struct ocelot_private *priv = dev_get_priv(dev);
456 struct eth_pdata *pdata = dev_get_platdata(dev);
457 const unsigned char mac[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff,
461 ret = ocelot_initialize(priv);
465 /* Set MAC address tables entries for CPU redirection */
466 ocelot_mac_table_add(priv, mac, PGID_BROADCAST);
468 writel(BIT(CPU_PORT) | INTERNAL_PORT_MSK,
469 priv->regs[ANA] + ANA_PGID(PGID_BROADCAST));
471 /* It should be setup latter in ocelot_write_hwaddr */
472 ocelot_mac_table_add(priv, pdata->enetaddr, PGID_UNICAST);
474 writel(BIT(CPU_PORT), priv->regs[ANA] + ANA_PGID(PGID_UNICAST));
479 static int ocelot_send(struct udevice *dev, void *packet, int length)
481 struct ocelot_private *priv = dev_get_priv(dev);
483 int port = BIT(0); /* use port 0 */
487 * Generate the IFH for frame injection
489 * The IFH is a 128bit-value
490 * bit 127: bypass the analyzer processing
491 * bit 56-67: destination mask
492 * bit 28-29: pop_cnt: 3 disables all rewriting of the frame
493 * bit 20-27: cpu extraction queue mask
494 * bit 16: tag type 0: C-tag, 1: S-tag
497 ifh[0] = IFH_INJ_BYPASS;
498 ifh[1] = (0xf00 & port) >> 8;
499 ifh[2] = (0xff & port) << 24;
500 ifh[3] = (IFH_TAG_TYPE_C << 16);
502 return mscc_send(priv->regs[QS], ocelot_regs_qs,
503 ifh, IFH_LEN, buf, length);
506 static int ocelot_recv(struct udevice *dev, int flags, uchar **packetp)
508 struct ocelot_private *priv = dev_get_priv(dev);
509 u32 *rxbuf = (u32 *)net_rx_packets[0];
512 byte_cnt = mscc_recv(priv->regs[QS], ocelot_regs_qs, rxbuf, IFH_LEN,
515 *packetp = net_rx_packets[0];
520 static int ocelot_probe(struct udevice *dev)
522 struct ocelot_private *priv = dev_get_priv(dev);
526 enum ocelot_target id;
541 for (i = 0; i < ARRAY_SIZE(reg); i++) {
542 priv->regs[reg[i].id] = dev_remap_addr_name(dev, reg[i].name);
543 if (!priv->regs[reg[i].id]) {
545 ("Error %d: can't get regs base addresses for %s\n",
551 priv->bus[INTERNAL] = ocelot_mdiobus_init(dev);
553 for (i = 0; i < 4; i++) {
554 phy_connect(priv->bus[INTERNAL], i, dev,
555 PHY_INTERFACE_MODE_NONE);
561 static int ocelot_remove(struct udevice *dev)
563 struct ocelot_private *priv = dev_get_priv(dev);
566 for (i = 0; i < NUM_PHY; i++) {
567 mdio_unregister(priv->bus[i]);
568 mdio_free(priv->bus[i]);
574 static const struct eth_ops ocelot_ops = {
575 .start = ocelot_start,
579 .write_hwaddr = ocelot_write_hwaddr,
582 static const struct udevice_id mscc_ocelot_ids[] = {
583 {.compatible = "mscc,vsc7514-switch"},
587 U_BOOT_DRIVER(ocelot) = {
588 .name = "ocelot-switch",
590 .of_match = mscc_ocelot_ids,
591 .probe = ocelot_probe,
592 .remove = ocelot_remove,
594 .priv_auto_alloc_size = sizeof(struct ocelot_private),
595 .platdata_auto_alloc_size = sizeof(struct eth_pdata),