common: Drop image.h from common header
[platform/kernel/u-boot.git] / board / freescale / lx2160a / eth_lx2160aqds.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright 2018-2020 NXP
4  *
5  */
6
7 #include <common.h>
8 #include <env.h>
9 #include <fdt_support.h>
10 #include <hwconfig.h>
11 #include <command.h>
12 #include <net.h>
13 #include <netdev.h>
14 #include <malloc.h>
15 #include <fsl_mdio.h>
16 #include <miiphy.h>
17 #include <phy.h>
18 #include <fm_eth.h>
19 #include <asm/io.h>
20 #include <exports.h>
21 #include <asm/arch/fsl_serdes.h>
22 #include <fsl-mc/fsl_mc.h>
23 #include <fsl-mc/ldpaa_wriop.h>
24 #include <linux/libfdt.h>
25
26 #include "../common/qixis.h"
27
28 DECLARE_GLOBAL_DATA_PTR;
29
30 #define EMI_NONE        0
31 #define EMI1            1 /* Mdio Bus 1 */
32 #define EMI2            2 /* Mdio Bus 2 */
33
34 #if defined(CONFIG_FSL_MC_ENET)
35 enum io_slot {
36         IO_SLOT_NONE = 0,
37         IO_SLOT_1,
38         IO_SLOT_2,
39         IO_SLOT_3,
40         IO_SLOT_4,
41         IO_SLOT_5,
42         IO_SLOT_6,
43         IO_SLOT_7,
44         IO_SLOT_8,
45         EMI1_RGMII1,
46         EMI1_RGMII2,
47         IO_SLOT_MAX
48 };
49
50 struct lx2160a_qds_mdio {
51         enum io_slot ioslot : 4;
52         u8 realbusnum : 4;
53         struct mii_dev *realbus;
54 };
55
56 /* structure explaining the phy configuration on 8 lanes of a serdes*/
57 struct serdes_phy_config {
58         u8 serdes; /* serdes protocol */
59         struct phy_config {
60                 u8 dpmacid;
61                 /* -1 terminated array */
62                 int phy_address[WRIOP_MAX_PHY_NUM + 1];
63                 u8 mdio_bus;
64                 enum io_slot ioslot;
65         } phy_config[SRDS_MAX_LANES];
66 };
67
68 /* Table defining the phy configuration on 8 lanes of a serdes.
69  * Various assumptions have been made while defining this table.
70  * e.g. for serdes1 protocol 19 it is being assumed that X-M11-USXGMII
71  * card is being used for dpmac 3-4. (X-M12-XFI could also have been used)
72  * And also that this card is connected to IO Slot 1 (could have been connected
73  * to any of the 8 IO slots (IO slot 1 - IO slot 8)).
74  * similarly, it is also being assumed that MDIO 1 is selected on X-M7-40G card
75  * used in serdes1 protocol 19 (could have selected MDIO 2)
76  * To override these settings "dpmac" environment variable can be used after
77  * defining "dpmac_override" in hwconfig environment variable.
78  * This table has limited serdes protocol entries. It can be expanded as per
79  * requirement.
80  */
81 static const struct serdes_phy_config serdes1_phy_config[] = {
82         {3, {{WRIOP1_DPMAC3, {AQ_PHY_ADDR1, -1},
83               EMI1, IO_SLOT_1},
84             {WRIOP1_DPMAC4, {AQ_PHY_ADDR2, -1},
85              EMI1, IO_SLOT_1},
86             {WRIOP1_DPMAC5, {AQ_PHY_ADDR3, -1},
87              EMI1, IO_SLOT_1},
88             {WRIOP1_DPMAC6, {AQ_PHY_ADDR4, -1},
89              EMI1, IO_SLOT_1} } },
90         {7, {{WRIOP1_DPMAC3, {AQ_PHY_ADDR1, -1},
91               EMI1, IO_SLOT_1},
92             {WRIOP1_DPMAC4, {AQ_PHY_ADDR2, -1},
93              EMI1, IO_SLOT_1},
94             {WRIOP1_DPMAC5, {AQ_PHY_ADDR3, -1},
95              EMI1, IO_SLOT_1},
96             {WRIOP1_DPMAC6, {AQ_PHY_ADDR4, -1},
97              EMI1, IO_SLOT_1},
98             {WRIOP1_DPMAC7, {SGMII_CARD_PORT1_PHY_ADDR, -1},
99              EMI1, IO_SLOT_2},
100             {WRIOP1_DPMAC8, {SGMII_CARD_PORT2_PHY_ADDR, -1},
101              EMI1, IO_SLOT_2},
102             {WRIOP1_DPMAC9, {SGMII_CARD_PORT3_PHY_ADDR, -1},
103              EMI1, IO_SLOT_2},
104             {WRIOP1_DPMAC10, {SGMII_CARD_PORT4_PHY_ADDR, -1},
105              EMI1, IO_SLOT_2} } },
106         {8, {} },
107         {13, {{WRIOP1_DPMAC1, {INPHI_PHY_ADDR1, INPHI_PHY_ADDR2, -1},
108                EMI1, IO_SLOT_1},
109              {WRIOP1_DPMAC2, {INPHI_PHY_ADDR1, INPHI_PHY_ADDR2, -1},
110               EMI1, IO_SLOT_2} } },
111         {14, {{WRIOP1_DPMAC1, {INPHI_PHY_ADDR1, INPHI_PHY_ADDR2, -1},
112                EMI1, IO_SLOT_1} } },
113         {15, {{WRIOP1_DPMAC1, {INPHI_PHY_ADDR1, INPHI_PHY_ADDR2, -1},
114                EMI1, IO_SLOT_1},
115              {WRIOP1_DPMAC2, {INPHI_PHY_ADDR1, INPHI_PHY_ADDR2, -1},
116               EMI1, IO_SLOT_1} } },
117         {17, {{WRIOP1_DPMAC3, {INPHI_PHY_ADDR1, INPHI_PHY_ADDR2, -1},
118                EMI1, IO_SLOT_1},
119              {WRIOP1_DPMAC4, {INPHI_PHY_ADDR1, INPHI_PHY_ADDR2, -1},
120               EMI1, IO_SLOT_1},
121              {WRIOP1_DPMAC5, {INPHI_PHY_ADDR1, INPHI_PHY_ADDR2, -1},
122               EMI1, IO_SLOT_1},
123              {WRIOP1_DPMAC6, {INPHI_PHY_ADDR1, INPHI_PHY_ADDR2, -1},
124               EMI1, IO_SLOT_1} } },
125         {19, {{WRIOP1_DPMAC2, {CORTINA_PHY_ADDR1, -1},
126                EMI1, IO_SLOT_2},
127              {WRIOP1_DPMAC3, {AQ_PHY_ADDR1, -1},
128               EMI1, IO_SLOT_1},
129              {WRIOP1_DPMAC4, {AQ_PHY_ADDR2, -1},
130               EMI1, IO_SLOT_1},
131              {WRIOP1_DPMAC5, {INPHI_PHY_ADDR1, INPHI_PHY_ADDR2, -1},
132               EMI1, IO_SLOT_6},
133              {WRIOP1_DPMAC6, {INPHI_PHY_ADDR1, INPHI_PHY_ADDR2, -1},
134               EMI1, IO_SLOT_6} } },
135         {20, {{WRIOP1_DPMAC1, {CORTINA_PHY_ADDR1, -1},
136                EMI1, IO_SLOT_1},
137              {WRIOP1_DPMAC2, {CORTINA_PHY_ADDR1, -1},
138               EMI1, IO_SLOT_2} } }
139 };
140
141 static const struct serdes_phy_config serdes2_phy_config[] = {
142         {2, {} },
143         {3, {} },
144         {5, {} },
145         {11, {{WRIOP1_DPMAC12, {SGMII_CARD_PORT2_PHY_ADDR, -1},
146                EMI1, IO_SLOT_7},
147              {WRIOP1_DPMAC17, {SGMII_CARD_PORT3_PHY_ADDR, -1},
148               EMI1, IO_SLOT_7},
149              {WRIOP1_DPMAC18, {SGMII_CARD_PORT4_PHY_ADDR, -1},
150               EMI1, IO_SLOT_7},
151              {WRIOP1_DPMAC16, {SGMII_CARD_PORT2_PHY_ADDR, -1},
152               EMI1, IO_SLOT_8},
153              {WRIOP1_DPMAC13, {SGMII_CARD_PORT3_PHY_ADDR, -1},
154               EMI1, IO_SLOT_8},
155              {WRIOP1_DPMAC14, {SGMII_CARD_PORT4_PHY_ADDR, -1},
156               EMI1, IO_SLOT_8} } },
157 };
158
159 static const struct serdes_phy_config serdes3_phy_config[] = {
160         {2, {} },
161         {3, {} }
162 };
163
164 static inline
165 const struct phy_config *get_phy_config(u8 serdes,
166                                         const struct serdes_phy_config *table,
167                                         u8 table_size)
168 {
169         int i;
170
171         for (i = 0; i < table_size; i++) {
172                 if (table[i].serdes == serdes)
173                         return table[i].phy_config;
174         }
175
176         return NULL;
177 }
178
179 /* BRDCFG4 controls EMI routing for the board.
180  * Bits    Function
181  * 7-6     EMI Interface #1 Primary Routing (CFG_MUX1_EMI1) (1.8V):
182  * EMI1    00= On-board PHY #1
183  *         01= On-board PHY #2
184  *         10= (reserved)
185  *         11= Slots 1..8 multiplexer and translator.
186  * 5-3     EMI Interface #1 Secondary Routing (CFG_MUX2_EMI1) (2.5V):
187  * EMI1X   000= Slot #1
188  *         001= Slot #2
189  *         010= Slot #3
190  *         011= Slot #4
191  *         100= Slot #5
192  *         101= Slot #6
193  *         110= Slot #7
194  *         111= Slot #8
195  * 2-0     EMI Interface #2 Routing (CFG_MUX_EMI2):
196  * EMI2    000= Slot #1 (secondary EMI)
197  *         001= Slot #2 (secondary EMI)
198  *         010= Slot #3 (secondary EMI)
199  *         011= Slot #4 (secondary EMI)
200  *         100= Slot #5 (secondary EMI)
201  *         101= Slot #6 (secondary EMI)
202  *         110= Slot #7 (secondary EMI)
203  *         111= Slot #8 (secondary EMI)
204  */
205 static int lx2160a_qds_get_mdio_mux_val(u8 realbusnum, enum io_slot ioslot)
206 {
207         switch (realbusnum) {
208         case EMI1:
209                 switch (ioslot) {
210                 case EMI1_RGMII1:
211                         return 0;
212                 case EMI1_RGMII2:
213                         return 0x40;
214                 default:
215                         return (((ioslot - 1) << BRDCFG4_EMI1SEL_SHIFT) | 0xC0);
216                 }
217                 break;
218         case EMI2:
219                 return ((ioslot - 1) << BRDCFG4_EMI2SEL_SHIFT);
220         default:
221                 return -1;
222         }
223 }
224
225 static void lx2160a_qds_mux_mdio(struct lx2160a_qds_mdio *priv)
226 {
227         u8 brdcfg4, mux_val, reg;
228
229         brdcfg4 = QIXIS_READ(brdcfg[4]);
230         reg = brdcfg4;
231         mux_val = lx2160a_qds_get_mdio_mux_val(priv->realbusnum, priv->ioslot);
232
233         switch (priv->realbusnum) {
234         case EMI1:
235                 brdcfg4 &= ~BRDCFG4_EMI1SEL_MASK;
236                 brdcfg4 |= mux_val;
237                 break;
238         case EMI2:
239                 brdcfg4 &= ~BRDCFG4_EMI2SEL_MASK;
240                 brdcfg4 |= mux_val;
241                 break;
242         }
243
244         if (brdcfg4 ^ reg)
245                 QIXIS_WRITE(brdcfg[4], brdcfg4);
246 }
247
248 static int lx2160a_qds_mdio_read(struct mii_dev *bus, int addr,
249                                  int devad, int regnum)
250 {
251         struct lx2160a_qds_mdio *priv = bus->priv;
252
253         lx2160a_qds_mux_mdio(priv);
254
255         return priv->realbus->read(priv->realbus, addr, devad, regnum);
256 }
257
258 static int lx2160a_qds_mdio_write(struct mii_dev *bus, int addr, int devad,
259                                   int regnum, u16 value)
260 {
261         struct lx2160a_qds_mdio *priv = bus->priv;
262
263         lx2160a_qds_mux_mdio(priv);
264
265         return priv->realbus->write(priv->realbus, addr, devad, regnum, value);
266 }
267
268 static int lx2160a_qds_mdio_reset(struct mii_dev *bus)
269 {
270         struct lx2160a_qds_mdio *priv = bus->priv;
271
272         return priv->realbus->reset(priv->realbus);
273 }
274
275 static struct mii_dev *lx2160a_qds_mdio_init(u8 realbusnum, enum io_slot ioslot)
276 {
277         struct lx2160a_qds_mdio *pmdio;
278         struct mii_dev *bus;
279         /*should be within MDIO_NAME_LEN*/
280         char dummy_mdio_name[] = "LX2160A_QDS_MDIO1_IOSLOT1";
281
282         if (realbusnum == EMI2) {
283                 if (ioslot < IO_SLOT_1 || ioslot > IO_SLOT_8) {
284                         printf("invalid ioslot %d\n", ioslot);
285                         return NULL;
286                 }
287         } else if (realbusnum == EMI1) {
288                 if (ioslot < IO_SLOT_1 || ioslot > EMI1_RGMII2) {
289                         printf("invalid ioslot %d\n", ioslot);
290                         return NULL;
291                 }
292         } else {
293                 printf("not supported real mdio bus %d\n", realbusnum);
294                 return NULL;
295         }
296
297         if (ioslot == EMI1_RGMII1)
298                 strcpy(dummy_mdio_name, "LX2160A_QDS_MDIO1_RGMII1");
299         else if (ioslot == EMI1_RGMII2)
300                 strcpy(dummy_mdio_name, "LX2160A_QDS_MDIO1_RGMII2");
301         else
302                 sprintf(dummy_mdio_name, "LX2160A_QDS_MDIO%d_IOSLOT%d",
303                         realbusnum, ioslot);
304         bus = miiphy_get_dev_by_name(dummy_mdio_name);
305
306         if (bus)
307                 return bus;
308
309         bus = mdio_alloc();
310         if (!bus) {
311                 printf("Failed to allocate %s bus\n", dummy_mdio_name);
312                 return NULL;
313         }
314
315         pmdio = malloc(sizeof(*pmdio));
316         if (!pmdio) {
317                 printf("Failed to allocate %s private data\n", dummy_mdio_name);
318                 free(bus);
319                 return NULL;
320         }
321
322         switch (realbusnum) {
323         case EMI1:
324                 pmdio->realbus =
325                   miiphy_get_dev_by_name(DEFAULT_WRIOP_MDIO1_NAME);
326                 break;
327         case EMI2:
328                 pmdio->realbus =
329                   miiphy_get_dev_by_name(DEFAULT_WRIOP_MDIO2_NAME);
330                 break;
331         }
332
333         if (!pmdio->realbus) {
334                 printf("No real mdio bus num %d found\n", realbusnum);
335                 free(bus);
336                 free(pmdio);
337                 return NULL;
338         }
339
340         pmdio->realbusnum = realbusnum;
341         pmdio->ioslot = ioslot;
342         bus->read = lx2160a_qds_mdio_read;
343         bus->write = lx2160a_qds_mdio_write;
344         bus->reset = lx2160a_qds_mdio_reset;
345         strcpy(bus->name, dummy_mdio_name);
346         bus->priv = pmdio;
347
348         if (!mdio_register(bus))
349                 return bus;
350
351         printf("No bus with name %s\n", dummy_mdio_name);
352         free(bus);
353         free(pmdio);
354         return NULL;
355 }
356
357 static inline void do_phy_config(const struct phy_config *phy_config)
358 {
359         struct mii_dev *bus;
360         int i, phy_num, phy_address;
361
362         for (i = 0; i < SRDS_MAX_LANES; i++) {
363                 if (!phy_config[i].dpmacid)
364                         continue;
365
366                 for (phy_num = 0;
367                      phy_num < ARRAY_SIZE(phy_config[i].phy_address);
368                      phy_num++) {
369                         phy_address = phy_config[i].phy_address[phy_num];
370                         if (phy_address == -1)
371                                 break;
372                         wriop_set_phy_address(phy_config[i].dpmacid,
373                                               phy_num, phy_address);
374                 }
375                 /*Register the muxing front-ends to the MDIO buses*/
376                 bus = lx2160a_qds_mdio_init(phy_config[i].mdio_bus,
377                                             phy_config[i].ioslot);
378                 if (!bus)
379                         printf("could not get bus for mdio %d ioslot %d\n",
380                                phy_config[i].mdio_bus,
381                                phy_config[i].ioslot);
382                 else
383                         wriop_set_mdio(phy_config[i].dpmacid, bus);
384         }
385 }
386
387 static inline void do_dpmac_config(int dpmac, const char *arg_dpmacid,
388                                    char *env_dpmac)
389 {
390         const char *ret;
391         size_t len;
392         u8 realbusnum, ioslot;
393         struct mii_dev *bus;
394         int phy_num;
395         char *phystr = "phy00";
396
397         /*search phy in dpmac arg*/
398         for (phy_num = 0; phy_num < WRIOP_MAX_PHY_NUM; phy_num++) {
399                 sprintf(phystr, "phy%d", phy_num + 1);
400                 ret = hwconfig_subarg_f(arg_dpmacid, phystr, &len, env_dpmac);
401                 if (!ret) {
402                         /*look for phy instead of phy1*/
403                         if (!phy_num)
404                                 ret = hwconfig_subarg_f(arg_dpmacid, "phy",
405                                                         &len, env_dpmac);
406                         if (!ret)
407                                 continue;
408                 }
409
410                 if (len != 4 || strncmp(ret, "0x", 2))
411                         printf("invalid phy format in %s variable.\n"
412                                "specify phy%d for %s in hex format e.g. 0x12\n",
413                                env_dpmac, phy_num + 1, arg_dpmacid);
414                 else
415                         wriop_set_phy_address(dpmac, phy_num,
416                                               simple_strtoul(ret, NULL, 16));
417         }
418
419         /*search mdio in dpmac arg*/
420         ret = hwconfig_subarg_f(arg_dpmacid, "mdio", &len, env_dpmac);
421         if (ret)
422                 realbusnum = *ret - '0';
423         else
424                 realbusnum = EMI_NONE;
425
426         if (realbusnum) {
427                 /*search io in dpmac arg*/
428                 ret = hwconfig_subarg_f(arg_dpmacid, "io", &len, env_dpmac);
429                 if (ret)
430                         ioslot = *ret - '0';
431                 else
432                         ioslot = IO_SLOT_NONE;
433                 /*Register the muxing front-ends to the MDIO buses*/
434                 bus = lx2160a_qds_mdio_init(realbusnum, ioslot);
435                 if (!bus)
436                         printf("could not get bus for mdio %d ioslot %d\n",
437                                realbusnum, ioslot);
438                 else
439                         wriop_set_mdio(dpmac, bus);
440         }
441 }
442
443 #endif
444
445 int board_eth_init(bd_t *bis)
446 {
447 #if defined(CONFIG_FSL_MC_ENET)
448         struct memac_mdio_info mdio_info;
449         struct memac_mdio_controller *regs;
450         int i;
451         const char *ret;
452         char *env_dpmac;
453         char dpmacid[] = "dpmac00", srds[] = "00_00_00";
454         size_t len;
455         struct mii_dev *bus;
456         const struct phy_config *phy_config;
457         struct ccsr_gur *gur = (void *)(CONFIG_SYS_FSL_GUTS_ADDR);
458         u32 srds_s1, srds_s2, srds_s3;
459
460         srds_s1 = in_le32(&gur->rcwsr[28]) &
461                   FSL_CHASSIS3_RCWSR28_SRDS1_PRTCL_MASK;
462         srds_s1 >>= FSL_CHASSIS3_RCWSR28_SRDS1_PRTCL_SHIFT;
463
464         srds_s2 = in_le32(&gur->rcwsr[28]) &
465                   FSL_CHASSIS3_RCWSR28_SRDS2_PRTCL_MASK;
466         srds_s2 >>= FSL_CHASSIS3_RCWSR28_SRDS2_PRTCL_SHIFT;
467
468         srds_s3 = in_le32(&gur->rcwsr[28]) &
469                   FSL_CHASSIS3_RCWSR28_SRDS3_PRTCL_MASK;
470         srds_s3 >>= FSL_CHASSIS3_RCWSR28_SRDS3_PRTCL_SHIFT;
471
472         sprintf(srds, "%d_%d_%d", srds_s1, srds_s2, srds_s3);
473
474         regs = (struct memac_mdio_controller *)CONFIG_SYS_FSL_WRIOP1_MDIO1;
475         mdio_info.regs = regs;
476         mdio_info.name = DEFAULT_WRIOP_MDIO1_NAME;
477
478         /*Register the EMI 1*/
479         fm_memac_mdio_init(bis, &mdio_info);
480
481         regs = (struct memac_mdio_controller *)CONFIG_SYS_FSL_WRIOP1_MDIO2;
482         mdio_info.regs = regs;
483         mdio_info.name = DEFAULT_WRIOP_MDIO2_NAME;
484
485         /*Register the EMI 2*/
486         fm_memac_mdio_init(bis, &mdio_info);
487
488         /* "dpmac" environment variable can be used after
489          * defining "dpmac_override" in hwconfig environment variable.
490          */
491         if (hwconfig("dpmac_override")) {
492                 env_dpmac = env_get("dpmac");
493                 if (env_dpmac) {
494                         ret = hwconfig_arg_f("srds", &len, env_dpmac);
495                         if (ret) {
496                                 if (strncmp(ret, srds, strlen(srds))) {
497                                         printf("SERDES configuration changed.\n"
498                                                "previous: %.*s, current: %s.\n"
499                                                "update dpmac variable.\n",
500                                                (int)len, ret, srds);
501                                 }
502                         } else {
503                                 printf("SERDES configuration not found.\n"
504                                        "Please add srds:%s in dpmac variable\n",
505                                        srds);
506                         }
507
508                         for (i = WRIOP1_DPMAC1; i < NUM_WRIOP_PORTS; i++) {
509                                 /* Look for dpmac1 to dpmac24(current max) arg
510                                  * in dpmac environment variable
511                                  */
512                                 sprintf(dpmacid, "dpmac%d", i);
513                                 ret = hwconfig_arg_f(dpmacid, &len, env_dpmac);
514                                 if (ret)
515                                         do_dpmac_config(i, dpmacid, env_dpmac);
516                         }
517                 } else {
518                         printf("Warning: environment dpmac not found.\n"
519                                "DPAA network interfaces may not work\n");
520                 }
521         } else {
522                 /*Look for phy config for serdes1 in phy config table*/
523                 phy_config = get_phy_config(srds_s1, serdes1_phy_config,
524                                             ARRAY_SIZE(serdes1_phy_config));
525                 if (!phy_config) {
526                         printf("%s WRIOP: Unsupported SerDes1 Protocol %d\n",
527                                __func__, srds_s1);
528                 } else {
529                         do_phy_config(phy_config);
530                 }
531                 phy_config = get_phy_config(srds_s2, serdes2_phy_config,
532                                             ARRAY_SIZE(serdes2_phy_config));
533                 if (!phy_config) {
534                         printf("%s WRIOP: Unsupported SerDes2 Protocol %d\n",
535                                __func__, srds_s2);
536                 } else {
537                         do_phy_config(phy_config);
538                 }
539                 phy_config = get_phy_config(srds_s3, serdes3_phy_config,
540                                             ARRAY_SIZE(serdes3_phy_config));
541                 if (!phy_config) {
542                         printf("%s WRIOP: Unsupported SerDes3 Protocol %d\n",
543                                __func__, srds_s3);
544                 } else {
545                         do_phy_config(phy_config);
546                 }
547         }
548
549         if (wriop_get_enet_if(WRIOP1_DPMAC17) == PHY_INTERFACE_MODE_RGMII_ID) {
550                 wriop_set_phy_address(WRIOP1_DPMAC17, 0, RGMII_PHY_ADDR1);
551                 bus = lx2160a_qds_mdio_init(EMI1, EMI1_RGMII1);
552                 if (!bus)
553                         printf("could not get bus for RGMII1\n");
554                 else
555                         wriop_set_mdio(WRIOP1_DPMAC17, bus);
556         }
557
558         if (wriop_get_enet_if(WRIOP1_DPMAC18) == PHY_INTERFACE_MODE_RGMII_ID) {
559                 wriop_set_phy_address(WRIOP1_DPMAC18, 0, RGMII_PHY_ADDR2);
560                 bus = lx2160a_qds_mdio_init(EMI1, EMI1_RGMII2);
561                 if (!bus)
562                         printf("could not get bus for RGMII2\n");
563                 else
564                         wriop_set_mdio(WRIOP1_DPMAC18, bus);
565         }
566
567         cpu_eth_init(bis);
568 #endif /* CONFIG_FMAN_ENET */
569
570 #ifdef CONFIG_PHY_AQUANTIA
571         /*
572          * Export functions to be used by AQ firmware
573          * upload application
574          */
575         gd->jt->strcpy = strcpy;
576         gd->jt->mdelay = mdelay;
577         gd->jt->mdio_get_current_dev = mdio_get_current_dev;
578         gd->jt->phy_find_by_mask = phy_find_by_mask;
579         gd->jt->mdio_phydev_for_ethname = mdio_phydev_for_ethname;
580         gd->jt->miiphy_set_current_dev = miiphy_set_current_dev;
581 #endif
582         return pci_eth_init(bis);
583 }
584
585 #if defined(CONFIG_RESET_PHY_R)
586 void reset_phy(void)
587 {
588 #if defined(CONFIG_FSL_MC_ENET)
589         mc_env_boot();
590 #endif
591 }
592 #endif /* CONFIG_RESET_PHY_R */
593
594 #if defined(CONFIG_FSL_MC_ENET)
595 int fdt_fixup_dpmac_phy_handle(void *fdt, int dpmac_id, int node_phandle)
596 {
597         int offset;
598         int ret;
599         char dpmac_str[] = "dpmacs@00";
600         const char *phy_string;
601
602         offset = fdt_path_offset(fdt, "/soc/fsl-mc/dpmacs");
603
604         if (offset < 0)
605                 offset = fdt_path_offset(fdt, "/fsl-mc/dpmacs");
606
607         if (offset < 0) {
608                 printf("dpmacs node not found in device tree\n");
609                 return offset;
610         }
611
612         sprintf(dpmac_str, "dpmac@%x", dpmac_id);
613         debug("dpmac_str = %s\n", dpmac_str);
614
615         offset = fdt_subnode_offset(fdt, offset, dpmac_str);
616         if (offset < 0) {
617                 printf("%s node not found in device tree\n", dpmac_str);
618                 return offset;
619         }
620
621         phy_string = fdt_getprop(fdt, offset, "phy-connection-type", NULL);
622         if (is_backplane_mode(phy_string)) {
623                 /* Backplane KR mode: skip fixups */
624                 printf("Interface %d in backplane KR mode\n", dpmac_id);
625                 return 0;
626         }
627
628         ret = fdt_appendprop_cell(fdt, offset, "phy-handle", node_phandle);
629         if (ret)
630                 printf("%d@%s %d\n", __LINE__, __func__, ret);
631
632         phy_string = phy_string_for_interface(wriop_get_enet_if(dpmac_id));
633         ret = fdt_setprop_string(fdt, offset, "phy-connection-type",
634                                  phy_string);
635         if (ret)
636                 printf("%d@%s %d\n", __LINE__, __func__, ret);
637
638         return ret;
639 }
640
641 int fdt_get_ioslot_offset(void *fdt, struct mii_dev *mii_dev, int fpga_offset)
642 {
643         char mdio_ioslot_str[] = "mdio@00";
644         struct lx2160a_qds_mdio *priv;
645         u64 reg;
646         u32 phandle;
647         int offset, mux_val;
648
649         /*Test if the MDIO bus is real mdio bus or muxing front end ?*/
650         if (strncmp(mii_dev->name, "LX2160A_QDS_MDIO",
651                     strlen("LX2160A_QDS_MDIO")))
652                 return -1;
653
654         /*Get the real MDIO bus num and ioslot info from bus's priv data*/
655         priv = mii_dev->priv;
656
657         debug("real_bus_num = %d, ioslot = %d\n",
658               priv->realbusnum, priv->ioslot);
659
660         if (priv->realbusnum == EMI1)
661                 reg = CONFIG_SYS_FSL_WRIOP1_MDIO1;
662         else
663                 reg = CONFIG_SYS_FSL_WRIOP1_MDIO2;
664
665         offset = fdt_node_offset_by_compat_reg(fdt, "fsl,fman-memac-mdio", reg);
666         if (offset < 0) {
667                 printf("mdio@%llx node not found in device tree\n", reg);
668                 return offset;
669         }
670
671         phandle = fdt_get_phandle(fdt, offset);
672         phandle = cpu_to_fdt32(phandle);
673         offset = fdt_node_offset_by_prop_value(fdt, -1, "mdio-parent-bus",
674                                                &phandle, 4);
675         if (offset < 0) {
676                 printf("mdio-mux-%d node not found in device tree\n",
677                        priv->realbusnum == EMI1 ? 1 : 2);
678                 return offset;
679         }
680
681         mux_val = lx2160a_qds_get_mdio_mux_val(priv->realbusnum, priv->ioslot);
682         if (priv->realbusnum == EMI1)
683                 mux_val >>= BRDCFG4_EMI1SEL_SHIFT;
684         else
685                 mux_val >>= BRDCFG4_EMI2SEL_SHIFT;
686         sprintf(mdio_ioslot_str, "mdio@%x", (u8)mux_val);
687
688         offset = fdt_subnode_offset(fdt, offset, mdio_ioslot_str);
689         if (offset < 0) {
690                 printf("%s node not found in device tree\n", mdio_ioslot_str);
691                 return offset;
692         }
693
694         return offset;
695 }
696
697 int fdt_create_phy_node(void *fdt, int offset, u8 phyaddr, int *subnodeoffset,
698                         struct phy_device *phy_dev, int phandle)
699 {
700         char phy_node_name[] = "ethernet-phy@00";
701         char phy_id_compatible_str[] = "ethernet-phy-id0000.0000,";
702         int ret;
703
704         sprintf(phy_node_name, "ethernet-phy@%x", phyaddr);
705         debug("phy_node_name = %s\n", phy_node_name);
706
707         *subnodeoffset = fdt_add_subnode(fdt, offset, phy_node_name);
708         if (*subnodeoffset <= 0) {
709                 printf("Could not add subnode %s inside node %s err = %s\n",
710                        phy_node_name, fdt_get_name(fdt, offset, NULL),
711                        fdt_strerror(*subnodeoffset));
712                 return *subnodeoffset;
713         }
714
715         sprintf(phy_id_compatible_str, "ethernet-phy-id%04x.%04x,",
716                 phy_dev->phy_id >> 16, phy_dev->phy_id & 0xFFFF);
717         debug("phy_id_compatible_str %s\n", phy_id_compatible_str);
718
719         ret = fdt_setprop_string(fdt, *subnodeoffset, "compatible",
720                                  phy_id_compatible_str);
721         if (ret) {
722                 printf("%d@%s %d\n", __LINE__, __func__, ret);
723                 goto out;
724         }
725
726         if (phy_dev->is_c45) {
727                 ret = fdt_appendprop_string(fdt, *subnodeoffset, "compatible",
728                                             "ethernet-phy-ieee802.3-c45");
729                 if (ret) {
730                         printf("%d@%s %d\n", __LINE__, __func__, ret);
731                         goto out;
732                 }
733         } else {
734                 ret = fdt_appendprop_string(fdt, *subnodeoffset, "compatible",
735                                             "ethernet-phy-ieee802.3-c22");
736                 if (ret) {
737                         printf("%d@%s %d\n", __LINE__, __func__, ret);
738                         goto out;
739                 }
740         }
741
742         ret = fdt_setprop_cell(fdt, *subnodeoffset, "reg", phyaddr);
743         if (ret) {
744                 printf("%d@%s %d\n", __LINE__, __func__, ret);
745                 goto out;
746         }
747
748         ret = fdt_set_phandle(fdt, *subnodeoffset, phandle);
749         if (ret) {
750                 printf("%d@%s %d\n", __LINE__, __func__, ret);
751                 goto out;
752         }
753
754 out:
755         if (ret)
756                 fdt_del_node(fdt, *subnodeoffset);
757
758         return ret;
759 }
760
761 int fdt_fixup_board_phy(void *fdt)
762 {
763         int fpga_offset, offset, subnodeoffset;
764         struct mii_dev *mii_dev;
765         struct list_head *mii_devs, *entry;
766         int ret, dpmac_id, phandle, i;
767         struct phy_device *phy_dev;
768         char ethname[ETH_NAME_LEN];
769         phy_interface_t phy_iface;
770
771         ret = 0;
772         /* we know FPGA is connected to i2c0, therefore search path directly,
773          * instead of compatible property, as it saves time
774          */
775         fpga_offset = fdt_path_offset(fdt, "/soc/i2c@2000000/fpga");
776
777         if (fpga_offset < 0)
778                 fpga_offset = fdt_path_offset(fdt, "/i2c@2000000/fpga");
779
780         if (fpga_offset < 0) {
781                 printf("i2c@2000000/fpga node not found in device tree\n");
782                 return fpga_offset;
783         }
784
785         phandle = fdt_alloc_phandle(fdt);
786         mii_devs = mdio_get_list_head();
787
788         list_for_each(entry, mii_devs) {
789                 mii_dev = list_entry(entry, struct mii_dev, link);
790                 debug("mii_dev name : %s\n", mii_dev->name);
791                 offset = fdt_get_ioslot_offset(fdt, mii_dev, fpga_offset);
792                 if (offset < 0)
793                         continue;
794
795                 // Look for phy devices attached to MDIO bus muxing front end
796                 // and create their entries with compatible being the device id
797                 for (i = 0; i < PHY_MAX_ADDR; i++) {
798                         phy_dev = mii_dev->phymap[i];
799                         if (!phy_dev)
800                                 continue;
801
802                         // TODO: use sscanf instead of loop
803                         dpmac_id = WRIOP1_DPMAC1;
804                         while (dpmac_id < NUM_WRIOP_PORTS) {
805                                 phy_iface = wriop_get_enet_if(dpmac_id);
806                                 snprintf(ethname, ETH_NAME_LEN, "DPMAC%d@%s",
807                                          dpmac_id,
808                                          phy_string_for_interface(phy_iface));
809                                 if (strcmp(ethname, phy_dev->dev->name) == 0)
810                                         break;
811                                 dpmac_id++;
812                         }
813                         if (dpmac_id == NUM_WRIOP_PORTS)
814                                 continue;
815                         ret = fdt_create_phy_node(fdt, offset, i,
816                                                   &subnodeoffset,
817                                                   phy_dev, phandle);
818                         if (ret)
819                                 break;
820
821                         ret = fdt_fixup_dpmac_phy_handle(fdt,
822                                                          dpmac_id, phandle);
823                         if (ret) {
824                                 fdt_del_node(fdt, subnodeoffset);
825                                 break;
826                         }
827                         /* calculate offset again as new node addition may have
828                          * changed offset;
829                          */
830                         offset = fdt_get_ioslot_offset(fdt, mii_dev,
831                                                        fpga_offset);
832                         phandle++;
833                 }
834
835                 if (ret)
836                         break;
837         }
838
839         return ret;
840 }
841 #endif // CONFIG_FSL_MC_ENET
842