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