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