net: mscc: Move miim commands into separate file.
[platform/kernel/u-boot.git] / drivers / net / mscc_eswitch / ocelot_switch.c
1 // SPDX-License-Identifier: (GPL-2.0+ OR MIT)
2 /*
3  * Copyright (c) 2018 Microsemi Corporation
4  */
5
6 #include <common.h>
7 #include <config.h>
8 #include <dm.h>
9 #include <dm/of_access.h>
10 #include <dm/of_addr.h>
11 #include <fdt_support.h>
12 #include <linux/io.h>
13 #include <linux/ioport.h>
14 #include <miiphy.h>
15 #include <net.h>
16 #include <wait_bit.h>
17
18 #include "mscc_miim.h"
19
20 #define PHY_CFG                         0x0
21 #define PHY_CFG_ENA                             0xF
22 #define PHY_CFG_COMMON_RST                      BIT(4)
23 #define PHY_CFG_RST                             (0xF << 5)
24 #define PHY_STAT                        0x4
25 #define PHY_STAT_SUPERVISOR_COMPLETE            BIT(0)
26
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))
44
45 #define SYS_FRM_AGING                   0x574
46 #define         SYS_FRM_AGING_ENA               BIT(20)
47
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)
59
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
64
65 /* Port registers */
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)
71
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)
76
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)
85
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)
103
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)
112
113 #define IFH_INJ_BYPASS          BIT(31)
114 #define IFH_TAG_TYPE_C          0
115 #define XTR_VALID_BYTES(x)      (4 - ((x) & 3))
116 #define MAC_VID                 1
117 #define CPU_PORT                11
118 #define INTERNAL_PORT_MSK       0xF
119 #define IFH_LEN                 4
120 #define OCELOT_BUF_CELL_SZ      60
121 #define ETH_ALEN                6
122 #define PGID_BROADCAST          13
123 #define PGID_UNICAST            14
124 #define PGID_SRC                80
125
126 enum ocelot_target {
127         ANA,
128         QS,
129         QSYS,
130         REW,
131         SYS,
132         HSIO,
133         PORT0,
134         PORT1,
135         PORT2,
136         PORT3,
137         TARGET_MAX,
138 };
139
140 #define MAX_PORT (PORT3 - PORT0)
141
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.
147  */
148 enum macaccess_entry_type {
149         ENTRYTYPE_NORMAL = 0,
150         ENTRYTYPE_LOCKED,
151         ENTRYTYPE_MACv4,
152         ENTRYTYPE_MACv6,
153 };
154
155 enum ocelot_mdio_target {
156         MIIM,
157         PHY,
158         TARGET_MDIO_MAX,
159 };
160
161 enum ocelot_phy_id {
162         INTERNAL,
163         EXTERNAL,
164         NUM_PHY,
165 };
166
167 struct ocelot_private {
168         void __iomem *regs[TARGET_MAX];
169
170         struct mii_dev *bus[NUM_PHY];
171         struct phy_device *phydev;
172         int phy_mode;
173         int max_speed;
174
175         int rx_pos;
176         int rx_siz;
177         int rx_off;
178         int tx_num;
179
180         u8 tx_adj_packetbuf[PKTSIZE_ALIGN + PKTALIGN];
181         void *tx_adj_buf;
182 };
183
184 struct mscc_miim_dev miim[NUM_PHY];
185
186 static int mscc_miim_reset(struct mii_dev *bus)
187 {
188         struct mscc_miim_dev *miim = (struct mscc_miim_dev *)bus->priv;
189
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);
194                 mdelay(500);
195         }
196
197         return 0;
198 }
199
200 /* For now only setup the internal mdio bus */
201 static struct mii_dev *ocelot_mdiobus_init(struct udevice *dev)
202 {
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;
207         struct resource res;
208         struct mii_dev *bus;
209         fdt32_t faddr;
210         int i;
211
212         bus = mdio_alloc();
213
214         if (!bus)
215                 return NULL;
216
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,
221                                        &phandle);
222         mdio_node = ofnode_get_parent(phandle.node);
223
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__);
227                         return NULL;
228                 }
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;
232         }
233
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;
241
242         if (mdio_register(bus))
243                 return NULL;
244         else
245                 return bus;
246 }
247
248 __weak void mscc_switch_reset(void)
249 {
250 }
251
252 static void ocelot_stop(struct udevice *dev)
253 {
254         struct ocelot_private *priv = dev_get_priv(dev);
255         int i;
256
257         mscc_switch_reset();
258         for (i = 0; i < NUM_PHY; i++)
259                 if (priv->bus[i])
260                         mscc_miim_reset(priv->bus[i]);
261 }
262
263 static void ocelot_cpu_capture_setup(struct ocelot_private *priv)
264 {
265         int i;
266
267         /* map the 8 CPU extraction queues to CPU port 11 */
268         writel(0, priv->regs[QSYS] + QSYS_QMAP);
269
270         for (i = 0; i <= 1; i++) {
271                 /*
272                  * Do byte-swap and expect status after last data word
273                  * Extraction: Mode: manual extraction) | Byte_swap
274                  */
275                 writel(QS_XTR_GRP_CFG_MODE(1) | QS_XTR_GRP_CFG_BYTE_SWAP,
276                        priv->regs[QS] + QS_XTR_GRP_CFG(i));
277                 /*
278                  * Injection: Mode: manual extraction | Byte_swap
279                  */
280                 writel(QS_INJ_GRP_CFG_MODE(1) | QS_INJ_GRP_CFG_BYTE_SWAP,
281                        priv->regs[QS] + QS_INJ_GRP_CFG(i));
282         }
283
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));
289         /*
290          * Setup the CPU port as VLAN aware to support switching frames
291          * based on tags
292          */
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));
295
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));
299
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);
303
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);
307
308         setbits_le32(priv->regs[QSYS] + QSYS_EGR_NO_SHARING, BIT(CPU_PORT));
309 }
310
311 static void ocelot_port_init(struct ocelot_private *priv, int port)
312 {
313         void __iomem *regs = priv->regs[port];
314
315         /* Enable PCS */
316         writel(PCS1G_MODE_CFG_SGMII_MODE_ENA, regs + PCS1G_CFG);
317
318         /* Disable Signal Detect */
319         writel(0, regs + PCS1G_SD_CFG);
320
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);
324
325         /* Clear sgmii_mode_ena */
326         writel(0, regs + PCS1G_MODE_CFG);
327
328         /*
329          * Clear sw_resolve_ena(bit 0) and set adv_ability to
330          * something meaningful just in case
331          */
332         writel(PCS1G_ANEG_CFG_ADV_ABILITY(0x20), regs + PCS1G_ANEG_CFG);
333
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);
337
338         /* Set link speed and release all resets */
339         writel(DEV_CLOCK_CFG_LINK_SPEED_1000, regs + DEV_CLOCK_CFG);
340
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));
344
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);
348 }
349
350 static int ocelot_switch_init(struct ocelot_private *priv)
351 {
352         /* Reset switch & memories */
353         writel(SYS_SYSTEM_RST_MEM_ENA | SYS_SYSTEM_RST_MEM_INIT,
354                priv->regs[SYS] + SYS_SYSTEM_RST_CFG);
355
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");
360                 return -EIO;
361         }
362
363         /* Enable switch core */
364         setbits_le32(priv->regs[SYS] + SYS_SYSTEM_RST_CFG,
365                      SYS_SYSTEM_RST_CORE_ENA);
366
367         return 0;
368 }
369
370 static void ocelot_switch_flush(struct ocelot_private *priv)
371 {
372         /* All Queues flush */
373         setbits_le32(priv->regs[QS] + QS_XTR_FLUSH, QS_XTR_FLUSH_FLUSH);
374         /* Allow to drain */
375         mdelay(1);
376         /* All Queues normal */
377         clrbits_le32(priv->regs[QS] + QS_XTR_FLUSH, QS_XTR_FLUSH_FLUSH);
378 }
379
380 static int ocelot_initialize(struct ocelot_private *priv)
381 {
382         int ret, i;
383
384         /* Initialize switch memories, enable core */
385         ret = ocelot_switch_init(priv);
386         if (ret)
387                 return ret;
388         /*
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.
392          */
393         for (i = 0; i <= MAX_PORT; i++)
394                 writel(0, priv->regs[ANA] + ANA_PGID(PGID_SRC + i));
395
396         /* Flush queues */
397         ocelot_switch_flush(priv);
398
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);
402
403         for (i = PORT0; i <= PORT3; i++)
404                 ocelot_port_init(priv, i);
405
406         ocelot_cpu_capture_setup(priv);
407
408         debug("Ports enabled\n");
409
410         return 0;
411 }
412
413 static inline int ocelot_vlant_wait_for_completion(struct ocelot_private *priv)
414 {
415         unsigned int val, timeout = 10;
416
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.
420          */
421         do {
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--);
425
426         if (!timeout)
427                 return -ETIMEDOUT;
428
429         return 0;
430 }
431
432 static int ocelot_mac_table_add(struct ocelot_private *priv,
433                                 const unsigned char mac[ETH_ALEN], int pgid)
434 {
435         u32 macl = 0, mach = 0;
436         int ret;
437
438         /* Set the MAC address to handle and the vlan associated in a format
439          * understood by the hardware.
440          */
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;
448
449         writel(macl, priv->regs[ANA] + ANA_TABLES_MACLDATA);
450         writel(mach, priv->regs[ANA] + ANA_TABLES_MACHDATA);
451
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);
457
458         ret = ocelot_vlant_wait_for_completion(priv);
459
460         return ret;
461 }
462
463 static int ocelot_write_hwaddr(struct udevice *dev)
464 {
465         struct ocelot_private *priv = dev_get_priv(dev);
466         struct eth_pdata *pdata = dev_get_platdata(dev);
467
468         ocelot_mac_table_add(priv, pdata->enetaddr, PGID_UNICAST);
469
470         writel(BIT(CPU_PORT), priv->regs[ANA] + ANA_PGID(PGID_UNICAST));
471
472         return 0;
473 }
474
475 static int ocelot_start(struct udevice *dev)
476 {
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,
480                                               0xff };
481         int ret;
482
483         ret = ocelot_initialize(priv);
484         if (ret)
485                 return ret;
486
487         /* Set MAC address tables entries for CPU redirection */
488         ocelot_mac_table_add(priv, mac, PGID_BROADCAST);
489
490         writel(BIT(CPU_PORT) | INTERNAL_PORT_MSK,
491                priv->regs[ANA] + ANA_PGID(PGID_BROADCAST));
492
493         /* It should be setup latter in ocelot_write_hwaddr */
494         ocelot_mac_table_add(priv, pdata->enetaddr, PGID_UNICAST);
495
496         writel(BIT(CPU_PORT), priv->regs[ANA] + ANA_PGID(PGID_UNICAST));
497
498         return 0;
499 }
500
501 static int ocelot_send(struct udevice *dev, void *packet, int length)
502 {
503         struct ocelot_private *priv = dev_get_priv(dev);
504         u32 ifh[IFH_LEN];
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;
508         u32 *buf = packet;
509
510         writel(QS_INJ_CTRL_GAP_SIZE(1) | QS_INJ_CTRL_SOF,
511                priv->regs[QS] + QS_INJ_CTRL(grp));
512
513         /*
514          * Generate the IFH for frame injection
515          *
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
522          * bit 0-11: VID
523          */
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);
528
529         for (i = 0; i < IFH_LEN; i++)
530                 writel(ifh[i], priv->regs[QS] + QS_INJ_WR(grp));
531
532         for (i = 0; i < count; i++)
533                 writel(buf[i], priv->regs[QS] + QS_INJ_WR(grp));
534
535         /* Add padding */
536         while (i < (OCELOT_BUF_CELL_SZ / 4)) {
537                 writel(0, priv->regs[QS] + QS_INJ_WR(grp));
538                 i++;
539         }
540
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));
545
546         /* Add dummy CRC */
547         writel(0, priv->regs[QS] + QS_INJ_WR(grp));
548
549         return 0;
550 }
551
552 static int ocelot_recv(struct udevice *dev, int flags, uchar **packetp)
553 {
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];
557         int i, byte_cnt = 0;
558         bool eof_flag = false, pruned_flag = false, abort_flag = false;
559
560         if (!(readl(priv->regs[QS] + QS_XTR_DATA_PRESENT) & BIT(grp)))
561                 return -EAGAIN;
562
563         /* skip IFH */
564         for (i = 0; i < IFH_LEN; i++)
565                 readl(priv->regs[QS] + QS_XTR_RD(grp));
566
567         while (!eof_flag) {
568                 u32 val = readl(priv->regs[QS] + QS_XTR_RD(grp));
569
570                 switch (val) {
571                 case XTR_NOT_READY:
572                         debug("%d NOT_READY...?\n", byte_cnt);
573                         break;
574                 case XTR_ABORT:
575                         /* really nedeed?? not done in linux */
576                         *rxbuf = readl(priv->regs[QS] + QS_XTR_RD(grp));
577                         abort_flag = true;
578                         eof_flag = true;
579                         debug("XTR_ABORT\n");
580                         break;
581                 case XTR_EOF_0:
582                 case XTR_EOF_1:
583                 case XTR_EOF_2:
584                 case XTR_EOF_3:
585                         byte_cnt += XTR_VALID_BYTES(val);
586                         *rxbuf = readl(priv->regs[QS] + QS_XTR_RD(grp));
587                         eof_flag = true;
588                         debug("EOF\n");
589                         break;
590                 case XTR_PRUNED:
591                         /* But get the last 4 bytes as well */
592                         eof_flag = true;
593                         pruned_flag = true;
594                         debug("PRUNED\n");
595                         /* fallthrough */
596                 case XTR_ESCAPE:
597                         *rxbuf = readl(priv->regs[QS] + QS_XTR_RD(grp));
598                         byte_cnt += 4;
599                         rxbuf++;
600                         debug("ESCAPED\n");
601                         break;
602                 default:
603                         *rxbuf = val;
604                         byte_cnt += 4;
605                         rxbuf++;
606                 }
607         }
608
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);
612                 return -EAGAIN;
613         }
614
615         *packetp = net_rx_packets[0];
616
617         return byte_cnt;
618 }
619
620 static int ocelot_probe(struct udevice *dev)
621 {
622         struct ocelot_private *priv = dev_get_priv(dev);
623         int ret, i;
624
625         struct {
626                 enum ocelot_target id;
627                 char *name;
628         } reg[] = {
629                 { SYS, "sys" },
630                 { REW, "rew" },
631                 { QSYS, "qsys" },
632                 { ANA, "ana" },
633                 { QS, "qs" },
634                 { HSIO, "hsio" },
635                 { PORT0, "port0" },
636                 { PORT1, "port1" },
637                 { PORT2, "port2" },
638                 { PORT3, "port3" },
639         };
640
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]) {
644                         pr_err
645                             ("Error %d: can't get regs base addresses for %s\n",
646                              ret, reg[i].name);
647                         return -ENOMEM;
648                 }
649         }
650
651         priv->bus[INTERNAL] = ocelot_mdiobus_init(dev);
652
653         for (i = 0; i < 4; i++) {
654                 phy_connect(priv->bus[INTERNAL], i, dev,
655                             PHY_INTERFACE_MODE_NONE);
656         }
657
658         return 0;
659 }
660
661 static int ocelot_remove(struct udevice *dev)
662 {
663         struct ocelot_private *priv = dev_get_priv(dev);
664         int i;
665
666         for (i = 0; i < NUM_PHY; i++) {
667                 mdio_unregister(priv->bus[i]);
668                 mdio_free(priv->bus[i]);
669         }
670
671         return 0;
672 }
673
674 static const struct eth_ops ocelot_ops = {
675         .start        = ocelot_start,
676         .stop         = ocelot_stop,
677         .send         = ocelot_send,
678         .recv         = ocelot_recv,
679         .write_hwaddr = ocelot_write_hwaddr,
680 };
681
682 static const struct udevice_id mscc_ocelot_ids[] = {
683         {.compatible = "mscc,vsc7514-switch"},
684         { /* Sentinel */ }
685 };
686
687 U_BOOT_DRIVER(ocelot) = {
688         .name     = "ocelot-switch",
689         .id       = UCLASS_ETH,
690         .of_match = mscc_ocelot_ids,
691         .probe    = ocelot_probe,
692         .remove   = ocelot_remove,
693         .ops      = &ocelot_ops,
694         .priv_auto_alloc_size = sizeof(struct ocelot_private),
695         .platdata_auto_alloc_size = sizeof(struct eth_pdata),
696 };