Merge tag 'efi-2022-07-rc1-2' of https://source.denx.de/u-boot/custodians/u-boot-efi
[platform/kernel/u-boot.git] / board / CZ.NIC / turris_mox / turris_mox.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2018 Marek Behun <marek.behun@nic.cz>
4  */
5
6 #include <common.h>
7 #include <asm/arch/cpu.h>
8 #include <asm/arch/soc.h>
9 #include <net.h>
10 #include <asm/global_data.h>
11 #include <asm/io.h>
12 #include <asm/gpio.h>
13 #include <button.h>
14 #include <clk.h>
15 #include <dm.h>
16 #include <env.h>
17 #include <fdt_support.h>
18 #include <init.h>
19 #include <led.h>
20 #include <linux/delay.h>
21 #include <linux/libfdt.h>
22 #include <linux/string.h>
23 #include <miiphy.h>
24 #include <spi.h>
25
26 #include "mox_sp.h"
27
28 #define MAX_MOX_MODULES         10
29
30 #define MOX_MODULE_SFP          0x1
31 #define MOX_MODULE_PCI          0x2
32 #define MOX_MODULE_TOPAZ        0x3
33 #define MOX_MODULE_PERIDOT      0x4
34 #define MOX_MODULE_USB3         0x5
35 #define MOX_MODULE_PASSPCI      0x6
36
37 #define ARMADA_37XX_NB_GPIO_SEL (MVEBU_REGISTER(0x13830))
38 #define ARMADA_37XX_SPI_CTRL    (MVEBU_REGISTER(0x10600))
39 #define ARMADA_37XX_SPI_CFG     (MVEBU_REGISTER(0x10604))
40 #define ARMADA_37XX_SPI_DOUT    (MVEBU_REGISTER(0x10608))
41 #define ARMADA_37XX_SPI_DIN     (MVEBU_REGISTER(0x1060c))
42
43 DECLARE_GLOBAL_DATA_PTR;
44
45 #if defined(CONFIG_OF_BOARD_FIXUP)
46 int board_fix_fdt(void *blob)
47 {
48         enum fdt_status status_pcie, status_eth1;
49         u8 topology[MAX_MOX_MODULES];
50         int i, size, ret;
51         bool eth1_sgmii;
52
53         /*
54          * SPI driver is not loaded in driver model yet, but we have to find out
55          * if pcie should be enabled in U-Boot's device tree. Therefore we have
56          * to read SPI by reading/writing SPI registers directly
57          */
58
59         /* put pin from GPIO to SPI mode */
60         clrbits_le32(ARMADA_37XX_NB_GPIO_SEL, BIT(12));
61         /* configure cpol, cpha, prescale */
62         writel(0x10df, ARMADA_37XX_SPI_CFG);
63         mdelay(1);
64         /* enable SPI CS1 */
65         setbits_le32(ARMADA_37XX_SPI_CTRL, BIT(17));
66
67         while (!(readl(ARMADA_37XX_SPI_CTRL) & 0x2))
68                 udelay(1);
69
70         status_pcie = FDT_STATUS_DISABLED;
71         status_eth1 = FDT_STATUS_DISABLED;
72         eth1_sgmii = false;
73
74         for (i = 0; i < MAX_MOX_MODULES; ++i) {
75                 writel(0x0, ARMADA_37XX_SPI_DOUT);
76
77                 while (!(readl(ARMADA_37XX_SPI_CTRL) & 0x2))
78                         udelay(1);
79
80                 topology[i] = readl(ARMADA_37XX_SPI_DIN) & 0xff;
81                 if (topology[i] == 0xff)
82                         break;
83
84                 topology[i] &= 0xf;
85
86                 if (topology[i] == MOX_MODULE_SFP &&
87                     status_pcie == FDT_STATUS_DISABLED)
88                         eth1_sgmii = true;
89
90                 if (topology[i] == MOX_MODULE_SFP ||
91                     topology[i] == MOX_MODULE_TOPAZ ||
92                     topology[i] == MOX_MODULE_PERIDOT)
93                         status_eth1 = FDT_STATUS_OKAY;
94         }
95
96         size = i;
97
98         /* disable SPI CS1 */
99         clrbits_le32(ARMADA_37XX_SPI_CTRL, BIT(17));
100
101         ret = fdt_set_status_by_alias(blob, "ethernet1", status_eth1);
102         if (ret < 0)
103                 printf("Cannot set status for eth1 in U-Boot's device tree: %s!\n",
104                        fdt_strerror(ret));
105
106         if (eth1_sgmii) {
107                 ret = fdt_path_offset(blob, "ethernet1");
108                 if (ret >= 0)
109                         ret = fdt_setprop_string(blob, ret, "phy-mode", "sgmii");
110                 if (ret < 0)
111                         printf("Cannot set phy-mode for eth1 to sgmii in U-Boot device tree: %s!\n",
112                                fdt_strerror(ret));
113         }
114
115         if (size > 1 && (topology[1] == MOX_MODULE_PCI ||
116                          topology[1] == MOX_MODULE_USB3 ||
117                          topology[1] == MOX_MODULE_PASSPCI))
118                 status_pcie = FDT_STATUS_OKAY;
119
120         ret = fdt_set_status_by_compatible(blob, "marvell,armada-3700-pcie",
121                                            status_pcie);
122         if (ret < 0) {
123                 printf("Cannot set status for PCIe in U-Boot's device tree: %s!\n",
124                        fdt_strerror(ret));
125                 return 0;
126         }
127
128         if (a3700_fdt_fix_pcie_regions(blob) < 0) {
129                 printf("Cannot fix PCIe regions in U-Boot's device tree!\n");
130                 return 0;
131         }
132
133         return 0;
134 }
135 #endif
136
137 int board_init(void)
138 {
139         /* address of boot parameters */
140         gd->bd->bi_boot_params = CONFIG_SYS_SDRAM_BASE + 0x100;
141
142         return 0;
143 }
144
145 static int mox_do_spi(u8 *in, u8 *out, size_t size)
146 {
147         struct spi_slave *slave;
148         struct udevice *dev;
149         int ret;
150
151         ret = spi_get_bus_and_cs(0, 1, 1000000, SPI_CPHA | SPI_CPOL,
152                                  "spi_generic_drv", "moxtet@1", &dev,
153                                  &slave);
154         if (ret)
155                 goto fail;
156
157         ret = spi_claim_bus(slave);
158         if (ret)
159                 goto fail_free;
160
161         ret = spi_xfer(slave, size * 8, out, in, SPI_XFER_ONCE);
162
163         spi_release_bus(slave);
164 fail_free:
165         spi_free_slave(slave);
166 fail:
167         return ret;
168 }
169
170 static int mox_get_topology(const u8 **ptopology, int *psize, int *pis_sd)
171 {
172         static int is_sd;
173         static u8 topology[MAX_MOX_MODULES - 1];
174         static int size;
175         u8 din[MAX_MOX_MODULES], dout[MAX_MOX_MODULES];
176         int ret, i;
177
178         if (size) {
179                 if (ptopology)
180                         *ptopology = topology;
181                 if (psize)
182                         *psize = size;
183                 if (pis_sd)
184                         *pis_sd = is_sd;
185                 return 0;
186         }
187
188         memset(din, 0, MAX_MOX_MODULES);
189         memset(dout, 0, MAX_MOX_MODULES);
190
191         ret = mox_do_spi(din, dout, MAX_MOX_MODULES);
192         if (ret)
193                 return ret;
194
195         if (din[0] == 0x10)
196                 is_sd = 1;
197         else if (din[0] == 0x00)
198                 is_sd = 0;
199         else
200                 return -ENODEV;
201
202         for (i = 1; i < MAX_MOX_MODULES && din[i] != 0xff; ++i)
203                 topology[i - 1] = din[i] & 0xf;
204         size = i - 1;
205
206         if (ptopology)
207                 *ptopology = topology;
208         if (psize)
209                 *psize = size;
210         if (pis_sd)
211                 *pis_sd = is_sd;
212
213         return 0;
214 }
215
216 #define SW_SMI_CMD_R(d, r)      (0x9800 | (((d) & 0x1f) << 5) | ((r) & 0x1f))
217 #define SW_SMI_CMD_W(d, r)      (0x9400 | (((d) & 0x1f) << 5) | ((r) & 0x1f))
218
219 static int sw_multi_read(struct mii_dev *bus, int sw, int dev, int reg)
220 {
221         bus->write(bus, sw, 0, 0, SW_SMI_CMD_R(dev, reg));
222         mdelay(5);
223         return bus->read(bus, sw, 0, 1);
224 }
225
226 static void sw_multi_write(struct mii_dev *bus, int sw, int dev, int reg,
227                            u16 val)
228 {
229         bus->write(bus, sw, 0, 1, val);
230         bus->write(bus, sw, 0, 0, SW_SMI_CMD_W(dev, reg));
231         mdelay(5);
232 }
233
234 static int sw_scratch_read(struct mii_dev *bus, int sw, int reg)
235 {
236         sw_multi_write(bus, sw, 0x1c, 0x1a, (reg & 0x7f) << 8);
237         return sw_multi_read(bus, sw, 0x1c, 0x1a) & 0xff;
238 }
239
240 static void sw_led_write(struct mii_dev *bus, int sw, int port, int reg,
241                          u16 val)
242 {
243         sw_multi_write(bus, sw, port, 0x16, 0x8000 | ((reg & 7) << 12)
244                                             | (val & 0x7ff));
245 }
246
247 static void sw_blink_leds(struct mii_dev *bus, int peridot, int topaz)
248 {
249         int i, p;
250         struct {
251                 int port;
252                 u16 val;
253                 int wait;
254         } regs[] = {
255                 { 2, 0xef, 1 }, { 2, 0xfe, 1 }, { 2, 0x33, 0 },
256                 { 4, 0xef, 1 }, { 4, 0xfe, 1 }, { 4, 0x33, 0 },
257                 { 3, 0xfe, 1 }, { 3, 0xef, 1 }, { 3, 0x33, 0 },
258                 { 1, 0xfe, 1 }, { 1, 0xef, 1 }, { 1, 0x33, 0 }
259         };
260
261         for (i = 0; i < 12; ++i) {
262                 for (p = 0; p < peridot; ++p) {
263                         sw_led_write(bus, 0x10 + p, regs[i].port, 0,
264                                      regs[i].val);
265                         sw_led_write(bus, 0x10 + p, regs[i].port + 4, 0,
266                                      regs[i].val);
267                 }
268                 if (topaz) {
269                         sw_led_write(bus, 0x2, 0x10 + regs[i].port, 0,
270                                      regs[i].val);
271                 }
272
273                 if (regs[i].wait)
274                         mdelay(75);
275         }
276 }
277
278 static void check_switch_address(struct mii_dev *bus, int addr)
279 {
280         if (sw_scratch_read(bus, addr, 0x70) >> 3 != addr)
281                 printf("Check of switch MDIO address failed for 0x%02x\n",
282                        addr);
283 }
284
285 static int sfp, pci, topaz, peridot, usb, passpci;
286 static int sfp_pos, peridot_pos[3];
287 static int module_count;
288
289 static int configure_peridots(struct gpio_desc *reset_gpio)
290 {
291         int i, ret;
292         u8 dout[MAX_MOX_MODULES];
293
294         memset(dout, 0, MAX_MOX_MODULES);
295
296         /* set addresses of Peridot modules */
297         for (i = 0; i < peridot; ++i)
298                 dout[module_count - peridot_pos[i]] = (~i) & 3;
299
300         /*
301          * if there is a SFP module connected to the last Peridot module, set
302          * the P10_SMODE to 1 for the Peridot module
303          */
304         if (sfp)
305                 dout[module_count - peridot_pos[i - 1]] |= 1 << 3;
306
307         dm_gpio_set_value(reset_gpio, 1);
308         mdelay(10);
309
310         ret = mox_do_spi(NULL, dout, module_count + 1);
311
312         mdelay(10);
313         dm_gpio_set_value(reset_gpio, 0);
314
315         mdelay(50);
316
317         return ret;
318 }
319
320 static int get_reset_gpio(struct gpio_desc *reset_gpio)
321 {
322         int node;
323
324         node = fdt_node_offset_by_compatible(gd->fdt_blob, 0, "cznic,moxtet");
325         if (node < 0) {
326                 printf("Cannot find Moxtet bus device node!\n");
327                 return -1;
328         }
329
330         gpio_request_by_name_nodev(offset_to_ofnode(node), "reset-gpios", 0,
331                                    reset_gpio, GPIOD_IS_OUT);
332
333         if (!dm_gpio_is_valid(reset_gpio)) {
334                 printf("Cannot find reset GPIO for Moxtet bus!\n");
335                 return -1;
336         }
337
338         return 0;
339 }
340
341 int misc_init_r(void)
342 {
343         u8 mac[2][6];
344         int i, ret;
345
346         ret = mbox_sp_get_board_info(NULL, mac[0], mac[1], NULL, NULL);
347         if (ret < 0) {
348                 printf("Cannot read data from OTP!\n");
349                 return 0;
350         }
351
352         for (i = 0; i < 2; ++i) {
353                 u8 oldmac[6];
354
355                 if (is_valid_ethaddr(mac[i]) &&
356                     !eth_env_get_enetaddr_by_index("eth", i, oldmac))
357                         eth_env_set_enetaddr_by_index("eth", i, mac[i]);
358         }
359
360         return 0;
361 }
362
363 static void mox_phy_modify(struct phy_device *phydev, int page, int reg,
364                            u16 mask, u16 set)
365 {
366         int val;
367
368         val = phydev->drv->readext(phydev, MDIO_DEVAD_NONE, page, reg);
369         val &= ~mask;
370         val |= set;
371         phydev->drv->writeext(phydev, MDIO_DEVAD_NONE, page, reg, val);
372 }
373
374 static void mox_phy_leds_start_blinking(void)
375 {
376         struct phy_device *phydev;
377         struct mii_dev *bus;
378         const char *node_name;
379         int node;
380
381         node = fdt_path_offset(gd->fdt_blob, "ethernet0");
382         if (node < 0) {
383                 printf("Cannot get eth0!\n");
384                 return;
385         }
386
387         node_name = fdt_get_name(gd->fdt_blob, node, NULL);
388         if (!node_name) {
389                 printf("Cannot get eth0 node name!\n");
390                 return;
391         }
392
393         bus = miiphy_get_dev_by_name(node_name);
394         if (!bus) {
395                 printf("Cannot get MDIO bus device!\n");
396                 return;
397         }
398
399         phydev = phy_find_by_mask(bus, BIT(1));
400         if (!phydev) {
401                 printf("Cannot get ethernet PHY!\n");
402                 return;
403         }
404
405         mox_phy_modify(phydev, 3, 0x12, 0x700, 0x400);
406         mox_phy_modify(phydev, 3, 0x10, 0xff, 0xbb);
407 }
408
409 static bool read_reset_button(void)
410 {
411         struct udevice *button, *led;
412         int i;
413
414         if (device_get_global_by_ofnode(
415                         ofnode_first_subnode(ofnode_by_compatible(ofnode_null(),
416                                                                   "gpio-keys")),
417                         &button)) {
418                 printf("Cannot find reset button!\n");
419                 return false;
420         }
421
422         if (device_get_global_by_ofnode(
423                         ofnode_first_subnode(ofnode_by_compatible(ofnode_null(),
424                                                                   "gpio-leds")),
425                         &led)) {
426                 printf("Cannot find status LED!\n");
427                 return false;
428         }
429
430         led_set_state(led, LEDST_ON);
431
432         for (i = 0; i < 21; ++i) {
433                 if (button_get_state(button) != BUTTON_ON)
434                         return false;
435                 if (i < 20)
436                         mdelay(50);
437         }
438
439         led_set_state(led, LEDST_OFF);
440
441         return true;
442 }
443
444 static void handle_reset_button(void)
445 {
446         const char * const vars[1] = { "bootcmd_rescue", };
447
448         /*
449          * Ensure that bootcmd_rescue has always stock value, so that running
450          *   run bootcmd_rescue
451          * always works correctly.
452          */
453         env_set_default_vars(1, (char * const *)vars, 0);
454
455         if (read_reset_button()) {
456                 const char * const vars[2] = {
457                         "bootcmd",
458                         "distro_bootcmd",
459                 };
460
461                 /*
462                  * Set the above envs to their default values, in case the user
463                  * managed to break them.
464                  */
465                 env_set_default_vars(2, (char * const *)vars, 0);
466
467                 /* Ensure bootcmd_rescue is used by distroboot */
468                 env_set("boot_targets", "rescue");
469
470                 /* start blinking PHY LEDs */
471                 mox_phy_leds_start_blinking();
472
473                 printf("RESET button was pressed, overwriting boot_targets!\n");
474         } else {
475                 /*
476                  * In case the user somehow managed to save environment with
477                  * boot_targets=rescue, reset boot_targets to default value.
478                  * This could happen in subsequent commands if bootcmd_rescue
479                  * failed.
480                  */
481                 if (!strcmp(env_get("boot_targets"), "rescue")) {
482                         const char * const vars[1] = {
483                                 "boot_targets",
484                         };
485
486                         env_set_default_vars(1, (char * const *)vars, 0);
487                 }
488         }
489 }
490
491 int show_board_info(void)
492 {
493         int i, ret, board_version, ram_size, is_sd;
494         const char *pub_key;
495         const u8 *topology;
496         u64 serial_number;
497
498         printf("Model: CZ.NIC Turris Mox Board\n");
499
500         ret = mbox_sp_get_board_info(&serial_number, NULL, NULL, &board_version,
501                                      &ram_size);
502         if (ret < 0) {
503                 printf("  Cannot read board info: %i\n", ret);
504         } else {
505                 printf("  Board version: %i\n", board_version);
506                 printf("  RAM size: %i MiB\n", ram_size);
507                 printf("  Serial Number: %016llX\n", serial_number);
508         }
509
510         pub_key = mox_sp_get_ecdsa_public_key();
511         if (pub_key)
512                 printf("  ECDSA Public Key: %s\n", pub_key);
513         else
514                 printf("  Cannot read ECDSA Public Key\n");
515
516         ret = mox_get_topology(&topology, &module_count, &is_sd);
517         if (ret)
518                 printf("Cannot read module topology!\n");
519
520         printf("  SD/eMMC version: %s\n", is_sd ? "SD" : "eMMC");
521
522         if (module_count)
523                 printf("Module Topology:\n");
524
525         for (i = 0; i < module_count; ++i) {
526                 switch (topology[i]) {
527                 case MOX_MODULE_SFP:
528                         printf("% 4i: SFP Module\n", i + 1);
529                         break;
530                 case MOX_MODULE_PCI:
531                         printf("% 4i: Mini-PCIe Module\n", i + 1);
532                         break;
533                 case MOX_MODULE_TOPAZ:
534                         printf("% 4i: Topaz Switch Module (4-port)\n", i + 1);
535                         break;
536                 case MOX_MODULE_PERIDOT:
537                         printf("% 4i: Peridot Switch Module (8-port)\n", i + 1);
538                         break;
539                 case MOX_MODULE_USB3:
540                         printf("% 4i: USB 3.0 Module (4 ports)\n", i + 1);
541                         break;
542                 case MOX_MODULE_PASSPCI:
543                         printf("% 4i: Passthrough Mini-PCIe Module\n", i + 1);
544                         break;
545                 default:
546                         printf("% 4i: unknown (ID %i)\n", i + 1, topology[i]);
547                 }
548         }
549
550         /* check if modules are connected in supported mode */
551         for (i = 0; i < module_count; ++i) {
552                 switch (topology[i]) {
553                 case MOX_MODULE_SFP:
554                         if (sfp) {
555                                 printf("Error: Only one SFP module is supported!\n");
556                         } else if (topaz) {
557                                 printf("Error: SFP module cannot be connected after Topaz Switch module!\n");
558                         } else {
559                                 sfp_pos = i;
560                                 ++sfp;
561                         }
562                         break;
563                 case MOX_MODULE_PCI:
564                         if (pci)
565                                 printf("Error: Only one Mini-PCIe module is supported!\n");
566                         else if (usb)
567                                 printf("Error: Mini-PCIe module cannot come after USB 3.0 module!\n");
568                         else if (i && (i != 1 || !passpci))
569                                 printf("Error: Mini-PCIe module should be the first connected module or come right after Passthrough Mini-PCIe module!\n");
570                         else
571                                 ++pci;
572                         break;
573                 case MOX_MODULE_TOPAZ:
574                         if (topaz)
575                                 printf("Error: Only one Topaz module is supported!\n");
576                         else if (peridot >= 3)
577                                 printf("Error: At most two Peridot modules can come before Topaz module!\n");
578                         else
579                                 ++topaz;
580                         break;
581                 case MOX_MODULE_PERIDOT:
582                         if (sfp || topaz) {
583                                 printf("Error: Peridot module must come before SFP or Topaz module!\n");
584                         } else if (peridot >= 3) {
585                                 printf("Error: At most three Peridot modules are supported!\n");
586                         } else {
587                                 peridot_pos[peridot] = i;
588                                 ++peridot;
589                         }
590                         break;
591                 case MOX_MODULE_USB3:
592                         if (pci)
593                                 printf("Error: USB 3.0 module cannot come after Mini-PCIe module!\n");
594                         else if (usb)
595                                 printf("Error: Only one USB 3.0 module is supported!\n");
596                         else if (i && (i != 1 || !passpci))
597                                 printf("Error: USB 3.0 module should be the first connected module or come right after Passthrough Mini-PCIe module!\n");
598                         else
599                                 ++usb;
600                         break;
601                 case MOX_MODULE_PASSPCI:
602                         if (passpci)
603                                 printf("Error: Only one Passthrough Mini-PCIe module is supported!\n");
604                         else if (i != 0)
605                                 printf("Error: Passthrough Mini-PCIe module should be the first connected module!\n");
606                         else
607                                 ++passpci;
608                 }
609         }
610
611         return 0;
612 }
613
614 int last_stage_init(void)
615 {
616         struct gpio_desc reset_gpio = {};
617
618         /* configure modules */
619         if (get_reset_gpio(&reset_gpio) < 0)
620                 goto handle_reset_btn;
621
622         if (peridot > 0) {
623                 if (configure_peridots(&reset_gpio) < 0) {
624                         printf("Cannot configure Peridot modules!\n");
625                         peridot = 0;
626                 }
627         } else {
628                 dm_gpio_set_value(&reset_gpio, 1);
629                 mdelay(50);
630                 dm_gpio_set_value(&reset_gpio, 0);
631                 mdelay(50);
632         }
633
634         /*
635          * check if the addresses are set by reading Scratch & Misc register
636          * 0x70 of Peridot (and potentially Topaz) modules
637          */
638         if (peridot || topaz) {
639                 struct mii_dev *bus;
640                 const char *node_name;
641                 int node;
642
643                 node = fdt_path_offset(gd->fdt_blob, "ethernet0");
644                 node_name = (node >= 0) ? fdt_get_name(gd->fdt_blob, node, NULL) : NULL;
645                 bus = node_name ? miiphy_get_dev_by_name(node_name) : NULL;
646                 if (!bus) {
647                         printf("Cannot get MDIO bus device!\n");
648                 } else {
649                         int i;
650
651                         for (i = 0; i < peridot; ++i)
652                                 check_switch_address(bus, 0x10 + i);
653
654                         if (topaz)
655                                 check_switch_address(bus, 0x2);
656
657                         sw_blink_leds(bus, peridot, topaz);
658                 }
659         }
660
661 handle_reset_btn:
662         handle_reset_button();
663
664         return 0;
665 }
666
667 #if defined(CONFIG_OF_BOARD_SETUP)
668
669 static bool is_topaz(int id)
670 {
671         return topaz && id == peridot + topaz - 1;
672 }
673
674 static int switch_addr(int id)
675 {
676         return is_topaz(id) ? 0x2 : 0x10 + id;
677 }
678
679 static int setup_switch(void *blob, int id)
680 {
681         int res, addr, i, node;
682         char mdio_path[64];
683
684         node = fdt_node_offset_by_compatible(blob, -1, "marvell,orion-mdio");
685         if (node < 0)
686                 return node;
687
688         res = fdt_get_path(blob, node, mdio_path, sizeof(mdio_path));
689         if (res < 0)
690                 return res;
691
692         addr = switch_addr(id);
693
694         /* first enable the switch by setting status = "okay" */
695         res = fdt_status_okay_by_pathf(blob, "%s/switch%i@%x", mdio_path, id,
696                                        addr);
697         if (res < 0)
698                 return res;
699
700         /*
701          * now if there are more switches or a SFP module coming after,
702          * enable corresponding ports
703          */
704         if (id < peridot + topaz - 1) {
705                 res = fdt_status_okay_by_pathf(blob,
706                                                "%s/switch%i@%x/ports/port@a",
707                                                mdio_path, id, addr);
708         } else if (id == peridot - 1 && !topaz && sfp) {
709                 res = fdt_status_okay_by_pathf(blob,
710                                                "%s/switch%i@%x/ports/port-sfp@a",
711                                                mdio_path, id, addr);
712         } else {
713                 res = 0;
714         }
715         if (res < 0)
716                 return res;
717
718         if (id >= peridot + topaz - 1)
719                 return 0;
720
721         /* finally change link property if needed */
722         node = fdt_node_offset_by_pathf(blob, "%s/switch%i@%x/ports/port@a",
723                                         mdio_path, id, addr);
724         if (node < 0)
725                 return node;
726
727         for (i = id + 1; i < peridot + topaz; ++i) {
728                 unsigned int phandle;
729
730                 phandle = fdt_create_phandle_by_pathf(blob,
731                                                       "%s/switch%i@%x/ports/port@%x",
732                                                       mdio_path, i,
733                                                       switch_addr(i),
734                                                       is_topaz(i) ? 5 : 9);
735                 if (!phandle)
736                         return -FDT_ERR_NOPHANDLES;
737
738                 if (i == id + 1)
739                         res = fdt_setprop_u32(blob, node, "link", phandle);
740                 else
741                         res = fdt_appendprop_u32(blob, node, "link", phandle);
742                 if (res < 0)
743                         return res;
744         }
745
746         return 0;
747 }
748
749 int ft_board_setup(void *blob, struct bd_info *bd)
750 {
751         int res;
752
753         /*
754          * If MOX B (PCI), MOX F (USB) or MOX G (Passthrough PCI) modules are
755          * connected, enable the PCIe node.
756          */
757         if (pci || usb || passpci) {
758                 res = fdt_status_okay_by_compatible(blob,
759                                                     "marvell,armada-3700-pcie");
760                 if (res < 0)
761                         return res;
762
763                 /* Fix PCIe regions for devices with 4 GB RAM */
764                 res = a3700_fdt_fix_pcie_regions(blob);
765                 if (res < 0)
766                         return res;
767         }
768
769         /*
770          * If MOX C (Topaz switch) and/or MOX E (Peridot switch) are connected,
771          * enable the eth1 node and setup the switches.
772          */
773         if (peridot || topaz) {
774                 int i;
775
776                 res = fdt_status_okay_by_alias(blob, "ethernet1");
777                 if (res < 0)
778                         return res;
779
780                 for (i = 0; i < peridot + topaz; ++i) {
781                         res = setup_switch(blob, i);
782                         if (res < 0)
783                                 return res;
784                 }
785         }
786
787         /*
788          * If MOX D (SFP cage module) is connected, enable the SFP node and eth1
789          * node. If there is no Peridot switch between MOX A and MOX D, add link
790          * to the SFP node to eth1 node.
791          * Also enable and configure SFP GPIO controller node.
792          */
793         if (sfp) {
794                 int node;
795
796                 res = fdt_status_okay_by_compatible(blob, "sff,sfp");
797                 if (res < 0)
798                         return res;
799
800                 res = fdt_status_okay_by_alias(blob, "ethernet1");
801                 if (res < 0)
802                         return res;
803
804                 if (!peridot) {
805                         unsigned int phandle;
806
807                         phandle = fdt_create_phandle_by_compatible(blob,
808                                                                    "sff,sfp");
809                         if (!phandle)
810                                 return -FDT_ERR_NOPHANDLES;
811
812                         node = fdt_path_offset(blob, "ethernet1");
813                         if (node < 0)
814                                 return node;
815
816                         res = fdt_setprop_u32(blob, node, "sfp", phandle);
817                         if (res < 0)
818                                 return res;
819
820                         res = fdt_setprop_string(blob, node, "phy-mode",
821                                                  "sgmii");
822                         if (res < 0)
823                                 return res;
824                 }
825
826                 res = fdt_status_okay_by_compatible(blob, "cznic,moxtet-gpio");
827                 if (res < 0)
828                         return res;
829
830                 if (sfp_pos) {
831                         char newname[16];
832
833                         /* moxtet-sfp is on non-zero position, change default */
834                         node = fdt_node_offset_by_compatible(blob, -1,
835                                                              "cznic,moxtet-gpio");
836                         if (node < 0)
837                                 return node;
838
839                         res = fdt_setprop_u32(blob, node, "reg", sfp_pos);
840                         if (res < 0)
841                                 return res;
842
843                         sprintf(newname, "gpio@%x", sfp_pos);
844
845                         res = fdt_set_name(blob, node, newname);
846                         if (res < 0)
847                                 return res;
848                 }
849         }
850
851         fdt_fixup_ethernet(blob);
852
853         /* Finally remove disabled nodes, as per Rob Herring's request. */
854         fdt_delete_disabled_nodes(blob);
855
856         return 0;
857 }
858
859 #endif