6500649d7b9eb74d889413f8ec67b1cb99f5eead
[oweals/u-boot.git] / board / freescale / lx2160a / eth_lx2160aqds.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright 2018-2019 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         ret = fdt_appendprop_cell(fdt, offset, "phy-handle", node_phandle);
620         if (ret)
621                 printf("%d@%s %d\n", __LINE__, __func__, ret);
622
623         phy_string = phy_string_for_interface(wriop_get_enet_if(dpmac_id));
624         ret = fdt_setprop_string(fdt, offset, "phy-connection-type",
625                                  phy_string);
626         if (ret)
627                 printf("%d@%s %d\n", __LINE__, __func__, ret);
628
629         return ret;
630 }
631
632 int fdt_get_ioslot_offset(void *fdt, struct mii_dev *mii_dev, int fpga_offset)
633 {
634         char mdio_ioslot_str[] = "mdio@00";
635         struct lx2160a_qds_mdio *priv;
636         u64 reg;
637         u32 phandle;
638         int offset, mux_val;
639
640         /*Test if the MDIO bus is real mdio bus or muxing front end ?*/
641         if (strncmp(mii_dev->name, "LX2160A_QDS_MDIO",
642                     strlen("LX2160A_QDS_MDIO")))
643                 return -1;
644
645         /*Get the real MDIO bus num and ioslot info from bus's priv data*/
646         priv = mii_dev->priv;
647
648         debug("real_bus_num = %d, ioslot = %d\n",
649               priv->realbusnum, priv->ioslot);
650
651         if (priv->realbusnum == EMI1)
652                 reg = CONFIG_SYS_FSL_WRIOP1_MDIO1;
653         else
654                 reg = CONFIG_SYS_FSL_WRIOP1_MDIO2;
655
656         offset = fdt_node_offset_by_compat_reg(fdt, "fsl,fman-memac-mdio", reg);
657         if (offset < 0) {
658                 printf("mdio@%llx node not found in device tree\n", reg);
659                 return offset;
660         }
661
662         phandle = fdt_get_phandle(fdt, offset);
663         phandle = cpu_to_fdt32(phandle);
664         offset = fdt_node_offset_by_prop_value(fdt, -1, "mdio-parent-bus",
665                                                &phandle, 4);
666         if (offset < 0) {
667                 printf("mdio-mux-%d node not found in device tree\n",
668                        priv->realbusnum == EMI1 ? 1 : 2);
669                 return offset;
670         }
671
672         mux_val = lx2160a_qds_get_mdio_mux_val(priv->realbusnum, priv->ioslot);
673         if (priv->realbusnum == EMI1)
674                 mux_val >>= BRDCFG4_EMI1SEL_SHIFT;
675         else
676                 mux_val >>= BRDCFG4_EMI2SEL_SHIFT;
677         sprintf(mdio_ioslot_str, "mdio@%x", (u8)mux_val);
678
679         offset = fdt_subnode_offset(fdt, offset, mdio_ioslot_str);
680         if (offset < 0) {
681                 printf("%s node not found in device tree\n", mdio_ioslot_str);
682                 return offset;
683         }
684
685         return offset;
686 }
687
688 int fdt_create_phy_node(void *fdt, int offset, u8 phyaddr, int *subnodeoffset,
689                         struct phy_device *phy_dev, int phandle)
690 {
691         char phy_node_name[] = "ethernet-phy@00";
692         char phy_id_compatible_str[] = "ethernet-phy-id0000.0000,";
693         int ret;
694
695         sprintf(phy_node_name, "ethernet-phy@%x", phyaddr);
696         debug("phy_node_name = %s\n", phy_node_name);
697
698         *subnodeoffset = fdt_add_subnode(fdt, offset, phy_node_name);
699         if (*subnodeoffset <= 0) {
700                 printf("Could not add subnode %s inside node %s err = %s\n",
701                        phy_node_name, fdt_get_name(fdt, offset, NULL),
702                        fdt_strerror(*subnodeoffset));
703                 return *subnodeoffset;
704         }
705
706         sprintf(phy_id_compatible_str, "ethernet-phy-id%04x.%04x,",
707                 phy_dev->phy_id >> 16, phy_dev->phy_id & 0xFFFF);
708         debug("phy_id_compatible_str %s\n", phy_id_compatible_str);
709
710         ret = fdt_setprop_string(fdt, *subnodeoffset, "compatible",
711                                  phy_id_compatible_str);
712         if (ret) {
713                 printf("%d@%s %d\n", __LINE__, __func__, ret);
714                 goto out;
715         }
716
717         if (phy_dev->is_c45) {
718                 ret = fdt_appendprop_string(fdt, *subnodeoffset, "compatible",
719                                             "ethernet-phy-ieee802.3-c45");
720                 if (ret) {
721                         printf("%d@%s %d\n", __LINE__, __func__, ret);
722                         goto out;
723                 }
724         } else {
725                 ret = fdt_appendprop_string(fdt, *subnodeoffset, "compatible",
726                                             "ethernet-phy-ieee802.3-c22");
727                 if (ret) {
728                         printf("%d@%s %d\n", __LINE__, __func__, ret);
729                         goto out;
730                 }
731         }
732
733         ret = fdt_setprop_cell(fdt, *subnodeoffset, "reg", phyaddr);
734         if (ret) {
735                 printf("%d@%s %d\n", __LINE__, __func__, ret);
736                 goto out;
737         }
738
739         ret = fdt_set_phandle(fdt, *subnodeoffset, phandle);
740         if (ret) {
741                 printf("%d@%s %d\n", __LINE__, __func__, ret);
742                 goto out;
743         }
744
745 out:
746         if (ret)
747                 fdt_del_node(fdt, *subnodeoffset);
748
749         return ret;
750 }
751
752 int fdt_fixup_board_phy(void *fdt)
753 {
754         int fpga_offset, offset, subnodeoffset;
755         struct mii_dev *mii_dev;
756         struct list_head *mii_devs, *entry;
757         int ret, dpmac_id, phandle, i;
758         struct phy_device *phy_dev;
759         char ethname[ETH_NAME_LEN];
760         phy_interface_t phy_iface;
761
762         ret = 0;
763         /* we know FPGA is connected to i2c0, therefore search path directly,
764          * instead of compatible property, as it saves time
765          */
766         fpga_offset = fdt_path_offset(fdt, "/soc/i2c@2000000/fpga");
767
768         if (fpga_offset < 0)
769                 fpga_offset = fdt_path_offset(fdt, "/i2c@2000000/fpga");
770
771         if (fpga_offset < 0) {
772                 printf("i2c@2000000/fpga node not found in device tree\n");
773                 return fpga_offset;
774         }
775
776         phandle = fdt_alloc_phandle(fdt);
777         mii_devs = mdio_get_list_head();
778
779         list_for_each(entry, mii_devs) {
780                 mii_dev = list_entry(entry, struct mii_dev, link);
781                 debug("mii_dev name : %s\n", mii_dev->name);
782                 offset = fdt_get_ioslot_offset(fdt, mii_dev, fpga_offset);
783                 if (offset < 0)
784                         continue;
785
786                 // Look for phy devices attached to MDIO bus muxing front end
787                 // and create their entries with compatible being the device id
788                 for (i = 0; i < PHY_MAX_ADDR; i++) {
789                         phy_dev = mii_dev->phymap[i];
790                         if (!phy_dev)
791                                 continue;
792
793                         // TODO: use sscanf instead of loop
794                         dpmac_id = WRIOP1_DPMAC1;
795                         while (dpmac_id < NUM_WRIOP_PORTS) {
796                                 phy_iface = wriop_get_enet_if(dpmac_id);
797                                 snprintf(ethname, ETH_NAME_LEN, "DPMAC%d@%s",
798                                          dpmac_id,
799                                          phy_string_for_interface(phy_iface));
800                                 if (strcmp(ethname, phy_dev->dev->name) == 0)
801                                         break;
802                                 dpmac_id++;
803                         }
804                         if (dpmac_id == NUM_WRIOP_PORTS)
805                                 continue;
806                         ret = fdt_create_phy_node(fdt, offset, i,
807                                                   &subnodeoffset,
808                                                   phy_dev, phandle);
809                         if (ret)
810                                 break;
811
812                         ret = fdt_fixup_dpmac_phy_handle(fdt,
813                                                          dpmac_id, phandle);
814                         if (ret) {
815                                 fdt_del_node(fdt, subnodeoffset);
816                                 break;
817                         }
818                         /* calculate offset again as new node addition may have
819                          * changed offset;
820                          */
821                         offset = fdt_get_ioslot_offset(fdt, mii_dev,
822                                                        fpga_offset);
823                         phandle++;
824                 }
825
826                 if (ret)
827                         break;
828         }
829
830         return ret;
831 }
832 #endif // CONFIG_FSL_MC_ENET
833