Merge tag 'u-boot-stm32-mcu-20190514' of https://github.com/pchotard/u-boot
[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_xfer.h"
19 #include "mscc_mac_table.h"
20
21 #define PHY_CFG                         0x0
22 #define PHY_CFG_ENA                             0xF
23 #define PHY_CFG_COMMON_RST                      BIT(4)
24 #define PHY_CFG_RST                             (0xF << 5)
25 #define PHY_STAT                        0x4
26 #define PHY_STAT_SUPERVISOR_COMPLETE            BIT(0)
27
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)
41
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))
48
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
83
84 #define SYS_FRM_AGING                   0x574
85 #define         SYS_FRM_AGING_ENA               BIT(20)
86
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)
98
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
103
104 /* Port registers */
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)
110
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)
115
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)
124
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)
132
133 #define IFH_INJ_BYPASS          BIT(31)
134 #define IFH_TAG_TYPE_C          0
135 #define MAC_VID                 1
136 #define CPU_PORT                11
137 #define INTERNAL_PORT_MSK       0x2FF
138 #define IFH_LEN                 4
139 #define ETH_ALEN                6
140 #define PGID_BROADCAST          13
141 #define PGID_UNICAST            14
142 #define PGID_SRC                80
143
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",
147 };
148
149 #define REGS_NAMES_COUNT ARRAY_SIZE(regs_names) + 1
150 #define MAX_PORT 11
151
152 enum ocelot_ctrl_regs {
153         SYS = MAX_PORT,
154         REW,
155         QS,
156         HSIO,
157         QSYS,
158         ANA,
159 };
160
161 #define OCELOT_MIIM_BUS_COUNT 2
162
163 struct ocelot_phy_port_t {
164         size_t phy_addr;
165         struct mii_dev *bus;
166         u8 serdes_index;
167         u8 phy_mode;
168 };
169
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];
174 };
175
176 struct mscc_miim_dev {
177         void __iomem *regs;
178         phys_addr_t miim_base;
179         unsigned long miim_size;
180         struct mii_dev *bus;
181 };
182
183 static struct mscc_miim_dev miim[OCELOT_MIIM_BUS_COUNT];
184 static int miim_count = -1;
185
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,
192 };
193
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,
198 };
199
200 static void mscc_phy_reset(void)
201 {
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");
209         }
210 }
211
212 static int mscc_miim_wait_ready(struct mscc_miim_dev *miim)
213 {
214         return wait_for_bit_le32(miim->regs + GCB_MIIM_MII_STATUS,
215                                  GCB_MIIM_STAT_BUSY, false, 250, false);
216 }
217
218 static int mscc_miim_read(struct mii_dev *bus, int addr, int devad, int reg)
219 {
220         struct mscc_miim_dev *miim = (struct mscc_miim_dev *)bus->priv;
221         u32 val;
222         int ret;
223
224         ret = mscc_miim_wait_ready(miim);
225         if (ret)
226                 goto out;
227
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);
231
232         ret = mscc_miim_wait_ready(miim);
233         if (ret)
234                 goto out;
235
236         val = readl(miim->regs + GCB_MIIM_DATA);
237         if (val & GCB_MIIM_DATA_ERROR) {
238                 ret = -EIO;
239                 goto out;
240         }
241
242         ret = val & 0xFFFF;
243  out:
244         return ret;
245 }
246
247 static int mscc_miim_write(struct mii_dev *bus, int addr, int devad, int reg,
248                            u16 val)
249 {
250         struct mscc_miim_dev *miim = (struct mscc_miim_dev *)bus->priv;
251         int ret;
252
253         ret = mscc_miim_wait_ready(miim);
254         if (ret < 0)
255                 goto out;
256
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);
260  out:
261         return ret;
262 }
263
264 static struct mii_dev *ocelot_mdiobus_init(phys_addr_t miim_base,
265                                            unsigned long miim_size)
266 {
267         struct mii_dev *bus;
268
269         bus = mdio_alloc();
270
271         if (!bus)
272                 return NULL;
273
274         ++miim_count;
275         sprintf(bus->name, "miim-bus%d", miim_count);
276
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;
283
284         if (mdio_register(bus))
285                 return NULL;
286
287         miim[miim_count].bus = bus;
288         return bus;
289 }
290
291 __weak void mscc_switch_reset(void)
292 {
293 }
294
295 static void ocelot_stop(struct udevice *dev)
296 {
297         mscc_switch_reset();
298         mscc_phy_reset();
299 }
300
301 static void ocelot_cpu_capture_setup(struct ocelot_private *priv)
302 {
303         int i;
304
305         /* map the 8 CPU extraction queues to CPU port 11 */
306         writel(0, priv->regs[QSYS] + QSYS_QMAP);
307
308         for (i = 0; i <= 1; i++) {
309                 /*
310                  * Do byte-swap and expect status after last data word
311                  * Extraction: Mode: manual extraction) | Byte_swap
312                  */
313                 writel(QS_XTR_GRP_CFG_MODE(1) | QS_XTR_GRP_CFG_BYTE_SWAP,
314                        priv->regs[QS] + QS_XTR_GRP_CFG(i));
315                 /*
316                  * Injection: Mode: manual extraction | Byte_swap
317                  */
318                 writel(QS_INJ_GRP_CFG_MODE(1) | QS_INJ_GRP_CFG_BYTE_SWAP,
319                        priv->regs[QS] + QS_INJ_GRP_CFG(i));
320         }
321
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));
327         /*
328          * Setup the CPU port as VLAN aware to support switching frames
329          * based on tags
330          */
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));
333
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));
337
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);
341
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);
345
346         setbits_le32(priv->regs[QSYS] + QSYS_EGR_NO_SHARING, BIT(CPU_PORT));
347 }
348
349 static void ocelot_port_init(struct ocelot_private *priv, int port)
350 {
351         void __iomem *regs = priv->regs[port];
352
353         /* Enable PCS */
354         writel(PCS1G_MODE_CFG_SGMII_MODE_ENA, regs + PCS1G_CFG);
355
356         /* Disable Signal Detect */
357         writel(0, regs + PCS1G_SD_CFG);
358
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);
362
363         /* Clear sgmii_mode_ena */
364         writel(0, regs + PCS1G_MODE_CFG);
365
366         /*
367          * Clear sw_resolve_ena(bit 0) and set adv_ability to
368          * something meaningful just in case
369          */
370         writel(PCS1G_ANEG_CFG_ADV_ABILITY(0x20), regs + PCS1G_ANEG_CFG);
371
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);
375
376         /* Set link speed and release all resets */
377         writel(DEV_CLOCK_CFG_LINK_SPEED_1000, regs + DEV_CLOCK_CFG);
378
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));
382
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);
386 }
387
388 static void serdes1g_write(void __iomem *base, u32 addr)
389 {
390         u32 data;
391
392         writel(HSIO_MCB_SERDES1G_CFG_WR_ONE_SHOT |
393                HSIO_MCB_SERDES1G_CFG_ADDR(addr),
394                base + HSIO_MCB_SERDES1G_CFG);
395
396         do {
397                 data = readl(base + HSIO_MCB_SERDES1G_CFG);
398         } while (data & HSIO_MCB_SERDES1G_CFG_WR_ONE_SHOT);
399 }
400
401 static void serdes1g_setup(void __iomem *base, uint32_t addr,
402                            phy_interface_t interface)
403 {
404         writel(0x34, base + HSIO_HW_CFGSTAT_HW_CFG);
405
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);
434
435         serdes1g_write(base, addr);
436
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);
442
443         writel(0x0, base + HSIO_DIG_SERDES1G_MISC_CFG);
444         serdes1g_write(base, addr);
445 }
446
447 static void serdes_setup(struct ocelot_private *priv)
448 {
449         size_t mask;
450         int i = 0;
451
452         for (i = 0; i < MAX_PORT; ++i) {
453                 if (!priv->ports[i].bus || priv->ports[i].serdes_index == 0xff)
454                         continue;
455
456                 mask = BIT(priv->ports[i].serdes_index);
457                 serdes1g_setup(priv->regs[HSIO], mask,
458                                priv->ports[i].phy_mode);
459         }
460 }
461
462 static int ocelot_switch_init(struct ocelot_private *priv)
463 {
464         /* Reset switch & memories */
465         writel(SYS_SYSTEM_RST_MEM_ENA | SYS_SYSTEM_RST_MEM_INIT,
466                priv->regs[SYS] + SYS_SYSTEM_RST_CFG);
467
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");
472                 return -EIO;
473         }
474
475         /* Enable switch core */
476         setbits_le32(priv->regs[SYS] + SYS_SYSTEM_RST_CFG,
477                      SYS_SYSTEM_RST_CORE_ENA);
478
479         serdes_setup(priv);
480         return 0;
481 }
482
483 static int ocelot_initialize(struct ocelot_private *priv)
484 {
485         int ret, i;
486
487         /* Initialize switch memories, enable core */
488         ret = ocelot_switch_init(priv);
489         if (ret)
490                 return ret;
491         /*
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.
495          */
496         for (i = 0; i < MAX_PORT; i++)
497                 writel(0, priv->regs[ANA] + ANA_PGID(PGID_SRC + i));
498
499         /* Flush queues */
500         mscc_flush(priv->regs[QS], ocelot_regs_qs);
501
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);
505
506         for (i = 0; i < MAX_PORT; i++)
507                 ocelot_port_init(priv, i);
508
509         ocelot_cpu_capture_setup(priv);
510
511         debug("Ports enabled\n");
512
513         return 0;
514 }
515
516 static int ocelot_write_hwaddr(struct udevice *dev)
517 {
518         struct ocelot_private *priv = dev_get_priv(dev);
519         struct eth_pdata *pdata = dev_get_platdata(dev);
520
521         mscc_mac_table_add(priv->regs[ANA], ocelot_regs_ana_table,
522                            pdata->enetaddr, PGID_UNICAST);
523
524         writel(BIT(CPU_PORT), priv->regs[ANA] + ANA_PGID(PGID_UNICAST));
525
526         return 0;
527 }
528
529 static int ocelot_start(struct udevice *dev)
530 {
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,
534                                               0xff };
535         int ret;
536
537         ret = ocelot_initialize(priv);
538         if (ret)
539                 return ret;
540
541         /* Set MAC address tables entries for CPU redirection */
542         mscc_mac_table_add(priv->regs[ANA], ocelot_regs_ana_table, mac,
543                            PGID_BROADCAST);
544
545         writel(BIT(CPU_PORT) | INTERNAL_PORT_MSK,
546                priv->regs[ANA] + ANA_PGID(PGID_BROADCAST));
547
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);
551
552         writel(BIT(CPU_PORT), priv->regs[ANA] + ANA_PGID(PGID_UNICAST));
553
554         return 0;
555 }
556
557 static int ocelot_send(struct udevice *dev, void *packet, int length)
558 {
559         struct ocelot_private *priv = dev_get_priv(dev);
560         u32 ifh[IFH_LEN];
561         int port = BIT(0);      /* use port 0 */
562         u32 *buf = packet;
563
564         /*
565          * Generate the IFH for frame injection
566          *
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
573          * bit 0-11: VID
574          */
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);
579
580         return mscc_send(priv->regs[QS], ocelot_regs_qs,
581                          ifh, IFH_LEN, buf, length);
582 }
583
584 static int ocelot_recv(struct udevice *dev, int flags, uchar **packetp)
585 {
586         struct ocelot_private *priv = dev_get_priv(dev);
587         u32 *rxbuf = (u32 *)net_rx_packets[0];
588         int byte_cnt;
589
590         byte_cnt = mscc_recv(priv->regs[QS], ocelot_regs_qs, rxbuf, IFH_LEN,
591                              false);
592
593         *packetp = net_rx_packets[0];
594
595         return byte_cnt;
596 }
597
598 static struct mii_dev *get_mdiobus(phys_addr_t base, unsigned long size)
599 {
600         int i = 0;
601
602         for (i = 0; i < OCELOT_MIIM_BUS_COUNT; ++i)
603                 if (miim[i].miim_base == base && miim[i].miim_size == size)
604                         return miim[i].bus;
605
606         return NULL;
607 }
608
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)
612 {
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;
617 }
618
619 static int external_bus(struct ocelot_private *priv, size_t port_index)
620 {
621         return priv->ports[port_index].serdes_index != 0xff;
622 }
623
624 static int ocelot_probe(struct udevice *dev)
625 {
626         struct ocelot_private *priv = dev_get_priv(dev);
627         int i, ret;
628         struct resource res;
629         fdt32_t faddr;
630         phys_addr_t addr_base;
631         unsigned long addr_size;
632         ofnode eth_node, node, mdio_node;
633         size_t phy_addr;
634         struct mii_dev *bus;
635         struct ofnode_phandle_args phandle;
636         struct phy_device *phy;
637
638         if (!priv)
639                 return -EINVAL;
640
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]) {
644                         debug
645                             ("Error can't get regs base addresses for %s\n",
646                              regs_names[i]);
647                         return -ENOMEM;
648                 }
649         }
650
651         /* Initialize miim buses */
652         memset(&miim, 0x0, sizeof(struct mscc_miim_dev) *
653                OCELOT_MIIM_BUS_COUNT);
654
655         /* iterate all the ports and find out on which bus they are */
656         i = 0;
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))
661                         return -ENOMEM;
662                 i = res.start;
663
664                 ofnode_parse_phandle_with_args(node, "phy-handle", NULL, 0, 0,
665                                                &phandle);
666
667                 /* Get phy address on mdio bus */
668                 if (ofnode_read_resource(phandle.node, 0, &res))
669                         return -ENOMEM;
670                 phy_addr = res.start;
671
672                 /* Get mdio node */
673                 mdio_node = ofnode_get_parent(phandle.node);
674
675                 if (ofnode_read_resource(mdio_node, 0, &res))
676                         return -ENOMEM;
677                 faddr = cpu_to_fdt32(res.start);
678
679                 addr_base = ofnode_translate_address(mdio_node, &faddr);
680                 addr_size = res.end - res.start;
681
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);
686
687                 /* Connect mdio bus with the port */
688                 bus = get_mdiobus(addr_base, addr_size);
689
690                 /* Get serdes info */
691                 ret = ofnode_parse_phandle_with_args(node, "phys", NULL,
692                                                      3, 0, &phandle);
693                 if (ret)
694                         add_port_entry(priv, i, phy_addr, bus, 0xff, 0xff);
695                 else
696                         add_port_entry(priv, i, phy_addr, bus, phandle.args[1],
697                                        phandle.args[2]);
698         }
699
700         mscc_phy_reset();
701
702         for (i = 0; i < MAX_PORT; i++) {
703                 if (!priv->ports[i].bus)
704                         continue;
705
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);
711         }
712
713         return 0;
714 }
715
716 static int ocelot_remove(struct udevice *dev)
717 {
718         struct ocelot_private *priv = dev_get_priv(dev);
719         int i;
720
721         for (i = 0; i < OCELOT_MIIM_BUS_COUNT; i++) {
722                 mdio_unregister(priv->bus[i]);
723                 mdio_free(priv->bus[i]);
724         }
725
726         return 0;
727 }
728
729 static const struct eth_ops ocelot_ops = {
730         .start        = ocelot_start,
731         .stop         = ocelot_stop,
732         .send         = ocelot_send,
733         .recv         = ocelot_recv,
734         .write_hwaddr = ocelot_write_hwaddr,
735 };
736
737 static const struct udevice_id mscc_ocelot_ids[] = {
738         {.compatible = "mscc,vsc7514-switch"},
739         { /* Sentinel */ }
740 };
741
742 U_BOOT_DRIVER(ocelot) = {
743         .name     = "ocelot-switch",
744         .id       = UCLASS_ETH,
745         .of_match = mscc_ocelot_ids,
746         .probe    = ocelot_probe,
747         .remove   = ocelot_remove,
748         .ops      = &ocelot_ops,
749         .priv_auto_alloc_size = sizeof(struct ocelot_private),
750         .platdata_auto_alloc_size = sizeof(struct eth_pdata),
751 };