]> git.ipfire.org Git - thirdparty/u-boot.git/blob - board/CZ.NIC/turris_mox/turris_mox.c
treewide: Fix Marek's name and change my e-mail address
[thirdparty/u-boot.git] / board / CZ.NIC / turris_mox / turris_mox.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Copyright (C) 2018 Marek BehĂșn <kabel@kernel.org>
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 <dm/of_extra.h>
17 #include <env.h>
18 #include <fdt_support.h>
19 #include <init.h>
20 #include <led.h>
21 #include <linux/delay.h>
22 #include <linux/libfdt.h>
23 #include <linux/string.h>
24 #include <miiphy.h>
25 #include <spi.h>
26
27 #include "mox_sp.h"
28
29 #define MAX_MOX_MODULES 10
30
31 #define MOX_MODULE_SFP 0x1
32 #define MOX_MODULE_PCI 0x2
33 #define MOX_MODULE_TOPAZ 0x3
34 #define MOX_MODULE_PERIDOT 0x4
35 #define MOX_MODULE_USB3 0x5
36 #define MOX_MODULE_PASSPCI 0x6
37
38 #define ARMADA_37XX_NB_GPIO_SEL (MVEBU_REGISTER(0x13830))
39 #define ARMADA_37XX_SPI_CTRL (MVEBU_REGISTER(0x10600))
40 #define ARMADA_37XX_SPI_CFG (MVEBU_REGISTER(0x10604))
41 #define ARMADA_37XX_SPI_DOUT (MVEBU_REGISTER(0x10608))
42 #define ARMADA_37XX_SPI_DIN (MVEBU_REGISTER(0x1060c))
43
44 DECLARE_GLOBAL_DATA_PTR;
45
46 #if defined(CONFIG_OF_BOARD_FIXUP)
47 int board_fix_fdt(void *blob)
48 {
49 enum fdt_status status_pcie, status_eth1;
50 u8 topology[MAX_MOX_MODULES];
51 int i, size, ret;
52 bool eth1_sgmii;
53
54 /*
55 * SPI driver is not loaded in driver model yet, but we have to find out
56 * if pcie should be enabled in U-Boot's device tree. Therefore we have
57 * to read SPI by reading/writing SPI registers directly
58 */
59
60 /* put pin from GPIO to SPI mode */
61 clrbits_le32(ARMADA_37XX_NB_GPIO_SEL, BIT(12));
62 /* configure cpol, cpha, prescale */
63 writel(0x10df, ARMADA_37XX_SPI_CFG);
64 mdelay(1);
65 /* enable SPI CS1 */
66 setbits_le32(ARMADA_37XX_SPI_CTRL, BIT(17));
67
68 while (!(readl(ARMADA_37XX_SPI_CTRL) & 0x2))
69 udelay(1);
70
71 status_pcie = FDT_STATUS_DISABLED;
72 status_eth1 = FDT_STATUS_DISABLED;
73 eth1_sgmii = false;
74
75 for (i = 0; i < MAX_MOX_MODULES; ++i) {
76 writel(0x0, ARMADA_37XX_SPI_DOUT);
77
78 while (!(readl(ARMADA_37XX_SPI_CTRL) & 0x2))
79 udelay(1);
80
81 topology[i] = readl(ARMADA_37XX_SPI_DIN) & 0xff;
82 if (topology[i] == 0xff)
83 break;
84
85 topology[i] &= 0xf;
86
87 if (topology[i] == MOX_MODULE_SFP &&
88 status_pcie == FDT_STATUS_DISABLED)
89 eth1_sgmii = true;
90
91 if (topology[i] == MOX_MODULE_SFP ||
92 topology[i] == MOX_MODULE_TOPAZ ||
93 topology[i] == MOX_MODULE_PERIDOT)
94 status_eth1 = FDT_STATUS_OKAY;
95 }
96
97 size = i;
98
99 /* disable SPI CS1 */
100 clrbits_le32(ARMADA_37XX_SPI_CTRL, BIT(17));
101
102 ret = fdt_set_status_by_alias(blob, "ethernet1", status_eth1);
103 if (ret < 0)
104 printf("Cannot set status for eth1 in U-Boot's device tree: %s!\n",
105 fdt_strerror(ret));
106
107 if (eth1_sgmii) {
108 ret = fdt_path_offset(blob, "ethernet1");
109 if (ret >= 0)
110 ret = fdt_setprop_string(blob, ret, "phy-mode", "sgmii");
111 if (ret < 0)
112 printf("Cannot set phy-mode for eth1 to sgmii in U-Boot device tree: %s!\n",
113 fdt_strerror(ret));
114 }
115
116 if (size > 1 && (topology[1] == MOX_MODULE_PCI ||
117 topology[1] == MOX_MODULE_USB3 ||
118 topology[1] == MOX_MODULE_PASSPCI))
119 status_pcie = FDT_STATUS_OKAY;
120
121 ret = fdt_set_status_by_compatible(blob, "marvell,armada-3700-pcie",
122 status_pcie);
123 if (ret < 0) {
124 printf("Cannot set status for PCIe in U-Boot's device tree: %s!\n",
125 fdt_strerror(ret));
126 return 0;
127 }
128
129 if (a3700_fdt_fix_pcie_regions(blob) < 0) {
130 printf("Cannot fix PCIe regions in U-Boot's device tree!\n");
131 return 0;
132 }
133
134 return 0;
135 }
136 #endif
137
138 int board_init(void)
139 {
140 /* address of boot parameters */
141 gd->bd->bi_boot_params = CONFIG_SYS_SDRAM_BASE + 0x100;
142
143 return 0;
144 }
145
146 static int mox_do_spi(u8 *in, u8 *out, size_t size)
147 {
148 struct spi_slave *slave;
149 struct udevice *dev;
150 int ret;
151
152 ret = _spi_get_bus_and_cs(0, 1, 1000000, SPI_CPHA | SPI_CPOL,
153 "spi_generic_drv", "moxtet@1", &dev,
154 &slave);
155 if (ret)
156 goto fail;
157
158 ret = spi_claim_bus(slave);
159 if (ret)
160 goto fail_free;
161
162 ret = spi_xfer(slave, size * 8, out, in, SPI_XFER_ONCE);
163
164 spi_release_bus(slave);
165 fail_free:
166 spi_free_slave(slave);
167 fail:
168 return ret;
169 }
170
171 static int mox_get_topology(const u8 **ptopology, int *psize, int *pis_sd)
172 {
173 static int is_sd;
174 static u8 topology[MAX_MOX_MODULES - 1];
175 static int size;
176 u8 din[MAX_MOX_MODULES], dout[MAX_MOX_MODULES];
177 int ret, i;
178
179 if (size) {
180 if (ptopology)
181 *ptopology = topology;
182 if (psize)
183 *psize = size;
184 if (pis_sd)
185 *pis_sd = is_sd;
186 return 0;
187 }
188
189 memset(din, 0, MAX_MOX_MODULES);
190 memset(dout, 0, MAX_MOX_MODULES);
191
192 ret = mox_do_spi(din, dout, MAX_MOX_MODULES);
193 if (ret)
194 return ret;
195
196 if (din[0] == 0x10)
197 is_sd = 1;
198 else if (din[0] == 0x00)
199 is_sd = 0;
200 else
201 return -ENODEV;
202
203 for (i = 1; i < MAX_MOX_MODULES && din[i] != 0xff; ++i)
204 topology[i - 1] = din[i] & 0xf;
205 size = i - 1;
206
207 if (ptopology)
208 *ptopology = topology;
209 if (psize)
210 *psize = size;
211 if (pis_sd)
212 *pis_sd = is_sd;
213
214 return 0;
215 }
216
217 #define SW_SMI_CMD_R(d, r) (0x9800 | (((d) & 0x1f) << 5) | ((r) & 0x1f))
218 #define SW_SMI_CMD_W(d, r) (0x9400 | (((d) & 0x1f) << 5) | ((r) & 0x1f))
219
220 static int sw_multi_read(struct udevice *bus, int sw, int dev, int reg)
221 {
222 dm_mdio_write(bus, sw, MDIO_DEVAD_NONE, 0, SW_SMI_CMD_R(dev, reg));
223 mdelay(5);
224 return dm_mdio_read(bus, sw, MDIO_DEVAD_NONE, 1);
225 }
226
227 static void sw_multi_write(struct udevice *bus, int sw, int dev, int reg,
228 u16 val)
229 {
230 dm_mdio_write(bus, sw, MDIO_DEVAD_NONE, 1, val);
231 dm_mdio_write(bus, sw, MDIO_DEVAD_NONE, 0, SW_SMI_CMD_W(dev, reg));
232 mdelay(5);
233 }
234
235 static int sw_scratch_read(struct udevice *bus, int sw, int reg)
236 {
237 sw_multi_write(bus, sw, 0x1c, 0x1a, (reg & 0x7f) << 8);
238 return sw_multi_read(bus, sw, 0x1c, 0x1a) & 0xff;
239 }
240
241 static void sw_led_write(struct udevice *bus, int sw, int port, int reg,
242 u16 val)
243 {
244 sw_multi_write(bus, sw, port, 0x16, 0x8000 | ((reg & 7) << 12)
245 | (val & 0x7ff));
246 }
247
248 static void sw_blink_leds(struct udevice *bus, int peridot, int topaz)
249 {
250 int i, p;
251 struct {
252 int port;
253 u16 val;
254 int wait;
255 } regs[] = {
256 { 2, 0xef, 1 }, { 2, 0xfe, 1 }, { 2, 0x33, 0 },
257 { 4, 0xef, 1 }, { 4, 0xfe, 1 }, { 4, 0x33, 0 },
258 { 3, 0xfe, 1 }, { 3, 0xef, 1 }, { 3, 0x33, 0 },
259 { 1, 0xfe, 1 }, { 1, 0xef, 1 }, { 1, 0x33, 0 }
260 };
261
262 for (i = 0; i < 12; ++i) {
263 for (p = 0; p < peridot; ++p) {
264 sw_led_write(bus, 0x10 + p, regs[i].port, 0,
265 regs[i].val);
266 sw_led_write(bus, 0x10 + p, regs[i].port + 4, 0,
267 regs[i].val);
268 }
269 if (topaz) {
270 sw_led_write(bus, 0x2, 0x10 + regs[i].port, 0,
271 regs[i].val);
272 }
273
274 if (regs[i].wait)
275 mdelay(75);
276 }
277 }
278
279 static void check_switch_address(struct udevice *bus, int addr)
280 {
281 if (sw_scratch_read(bus, addr, 0x70) >> 3 != addr)
282 printf("Check of switch MDIO address failed for 0x%02x\n",
283 addr);
284 }
285
286 static int sfp, pci, topaz, peridot, usb, passpci;
287 static int sfp_pos, peridot_pos[3];
288 static int module_count;
289
290 static int configure_peridots(struct gpio_desc *reset_gpio)
291 {
292 int i, ret;
293 u8 dout[MAX_MOX_MODULES];
294
295 memset(dout, 0, MAX_MOX_MODULES);
296
297 /* set addresses of Peridot modules */
298 for (i = 0; i < peridot; ++i)
299 dout[module_count - peridot_pos[i]] = (~i) & 3;
300
301 /*
302 * if there is a SFP module connected to the last Peridot module, set
303 * the P10_SMODE to 1 for the Peridot module
304 */
305 if (sfp)
306 dout[module_count - peridot_pos[i - 1]] |= 1 << 3;
307
308 dm_gpio_set_value(reset_gpio, 1);
309 mdelay(10);
310
311 ret = mox_do_spi(NULL, dout, module_count + 1);
312
313 mdelay(10);
314 dm_gpio_set_value(reset_gpio, 0);
315
316 mdelay(50);
317
318 return ret;
319 }
320
321 static int get_reset_gpio(struct gpio_desc *reset_gpio)
322 {
323 int node;
324
325 node = fdt_node_offset_by_compatible(gd->fdt_blob, 0, "cznic,moxtet");
326 if (node < 0) {
327 printf("Cannot find Moxtet bus device node!\n");
328 return -1;
329 }
330
331 gpio_request_by_name_nodev(offset_to_ofnode(node), "reset-gpios", 0,
332 reset_gpio, GPIOD_IS_OUT);
333
334 if (!dm_gpio_is_valid(reset_gpio)) {
335 printf("Cannot find reset GPIO for Moxtet bus!\n");
336 return -1;
337 }
338
339 return 0;
340 }
341
342 int misc_init_r(void)
343 {
344 u8 mac[2][6];
345 int i, ret;
346
347 ret = mbox_sp_get_board_info(NULL, mac[0], mac[1], NULL, NULL);
348 if (ret < 0) {
349 printf("Cannot read data from OTP!\n");
350 return 0;
351 }
352
353 for (i = 0; i < 2; ++i) {
354 u8 oldmac[6];
355
356 if (is_valid_ethaddr(mac[i]) &&
357 !eth_env_get_enetaddr_by_index("eth", i, oldmac))
358 eth_env_set_enetaddr_by_index("eth", i, mac[i]);
359 }
360
361 return 0;
362 }
363
364 static void mox_phy_modify(struct phy_device *phydev, int page, int reg,
365 u16 mask, u16 set)
366 {
367 int val;
368
369 val = phydev->drv->readext(phydev, MDIO_DEVAD_NONE, page, reg);
370 val &= ~mask;
371 val |= set;
372 phydev->drv->writeext(phydev, MDIO_DEVAD_NONE, page, reg, val);
373 }
374
375 static void mox_phy_leds_start_blinking(void)
376 {
377 struct phy_device *phydev;
378 ofnode phy_node;
379
380 phy_node = ofnode_get_phy_node(ofnode_path("ethernet0"));
381 if (!ofnode_valid(phy_node))
382 goto err;
383
384 phydev = dm_phy_find_by_ofnode(phy_node);
385 if (!phydev)
386 goto err;
387
388 mox_phy_modify(phydev, 3, 0x12, 0x700, 0x400);
389 mox_phy_modify(phydev, 3, 0x10, 0xff, 0xbb);
390
391 return;
392 err:
393 printf("Cannot get ethernet PHY!\n");
394 }
395
396 static bool read_reset_button(void)
397 {
398 struct udevice *button, *led;
399 int i;
400
401 if (device_get_global_by_ofnode(
402 ofnode_first_subnode(ofnode_by_compatible(ofnode_null(),
403 "gpio-keys")),
404 &button)) {
405 printf("Cannot find reset button!\n");
406 return false;
407 }
408
409 if (device_get_global_by_ofnode(
410 ofnode_first_subnode(ofnode_by_compatible(ofnode_null(),
411 "gpio-leds")),
412 &led)) {
413 printf("Cannot find status LED!\n");
414 return false;
415 }
416
417 led_set_state(led, LEDST_ON);
418
419 for (i = 0; i < 21; ++i) {
420 if (button_get_state(button) != BUTTON_ON)
421 return false;
422 if (i < 20)
423 mdelay(50);
424 }
425
426 led_set_state(led, LEDST_OFF);
427
428 return true;
429 }
430
431 static void handle_reset_button(void)
432 {
433 const char * const vars[1] = { "bootcmd_rescue", };
434
435 /*
436 * Ensure that bootcmd_rescue has always stock value, so that running
437 * run bootcmd_rescue
438 * always works correctly.
439 */
440 env_set_default_vars(1, (char * const *)vars, 0);
441
442 if (read_reset_button()) {
443 const char * const vars[2] = {
444 "bootcmd",
445 "distro_bootcmd",
446 };
447
448 /*
449 * Set the above envs to their default values, in case the user
450 * managed to break them.
451 */
452 env_set_default_vars(2, (char * const *)vars, 0);
453
454 /* Ensure bootcmd_rescue is used by distroboot */
455 env_set("boot_targets", "rescue");
456
457 /* start blinking PHY LEDs */
458 mox_phy_leds_start_blinking();
459
460 printf("RESET button was pressed, overwriting boot_targets!\n");
461 } else {
462 /*
463 * In case the user somehow managed to save environment with
464 * boot_targets=rescue, reset boot_targets to default value.
465 * This could happen in subsequent commands if bootcmd_rescue
466 * failed.
467 */
468 if (!strcmp(env_get("boot_targets"), "rescue")) {
469 const char * const vars[1] = {
470 "boot_targets",
471 };
472
473 env_set_default_vars(1, (char * const *)vars, 0);
474 }
475 }
476 }
477
478 int show_board_info(void)
479 {
480 int i, ret, board_version, ram_size, is_sd;
481 const char *pub_key;
482 const u8 *topology;
483 u64 serial_number;
484
485 printf("Model: CZ.NIC Turris Mox Board\n");
486
487 ret = mbox_sp_get_board_info(&serial_number, NULL, NULL, &board_version,
488 &ram_size);
489 if (ret < 0) {
490 printf(" Cannot read board info: %i\n", ret);
491 } else {
492 printf(" Board version: %i\n", board_version);
493 printf(" RAM size: %i MiB\n", ram_size);
494 printf(" Serial Number: %016llX\n", serial_number);
495 }
496
497 pub_key = mox_sp_get_ecdsa_public_key();
498 if (pub_key)
499 printf(" ECDSA Public Key: %s\n", pub_key);
500 else
501 printf(" Cannot read ECDSA Public Key\n");
502
503 ret = mox_get_topology(&topology, &module_count, &is_sd);
504 if (ret)
505 printf("Cannot read module topology!\n");
506
507 printf(" SD/eMMC version: %s\n", is_sd ? "SD" : "eMMC");
508
509 if (module_count)
510 printf("Module Topology:\n");
511
512 for (i = 0; i < module_count; ++i) {
513 switch (topology[i]) {
514 case MOX_MODULE_SFP:
515 printf("% 4i: SFP Module\n", i + 1);
516 break;
517 case MOX_MODULE_PCI:
518 printf("% 4i: Mini-PCIe Module\n", i + 1);
519 break;
520 case MOX_MODULE_TOPAZ:
521 printf("% 4i: Topaz Switch Module (4-port)\n", i + 1);
522 break;
523 case MOX_MODULE_PERIDOT:
524 printf("% 4i: Peridot Switch Module (8-port)\n", i + 1);
525 break;
526 case MOX_MODULE_USB3:
527 printf("% 4i: USB 3.0 Module (4 ports)\n", i + 1);
528 break;
529 case MOX_MODULE_PASSPCI:
530 printf("% 4i: Passthrough Mini-PCIe Module\n", i + 1);
531 break;
532 default:
533 printf("% 4i: unknown (ID %i)\n", i + 1, topology[i]);
534 }
535 }
536
537 /* check if modules are connected in supported mode */
538 for (i = 0; i < module_count; ++i) {
539 switch (topology[i]) {
540 case MOX_MODULE_SFP:
541 if (sfp) {
542 printf("Error: Only one SFP module is supported!\n");
543 } else if (topaz) {
544 printf("Error: SFP module cannot be connected after Topaz Switch module!\n");
545 } else {
546 sfp_pos = i;
547 ++sfp;
548 }
549 break;
550 case MOX_MODULE_PCI:
551 if (pci)
552 printf("Error: Only one Mini-PCIe module is supported!\n");
553 else if (usb)
554 printf("Error: Mini-PCIe module cannot come after USB 3.0 module!\n");
555 else if (i && (i != 1 || !passpci))
556 printf("Error: Mini-PCIe module should be the first connected module or come right after Passthrough Mini-PCIe module!\n");
557 else
558 ++pci;
559 break;
560 case MOX_MODULE_TOPAZ:
561 if (topaz)
562 printf("Error: Only one Topaz module is supported!\n");
563 else if (peridot >= 3)
564 printf("Error: At most two Peridot modules can come before Topaz module!\n");
565 else
566 ++topaz;
567 break;
568 case MOX_MODULE_PERIDOT:
569 if (sfp || topaz) {
570 printf("Error: Peridot module must come before SFP or Topaz module!\n");
571 } else if (peridot >= 3) {
572 printf("Error: At most three Peridot modules are supported!\n");
573 } else {
574 peridot_pos[peridot] = i;
575 ++peridot;
576 }
577 break;
578 case MOX_MODULE_USB3:
579 if (pci)
580 printf("Error: USB 3.0 module cannot come after Mini-PCIe module!\n");
581 else if (usb)
582 printf("Error: Only one USB 3.0 module is supported!\n");
583 else if (i && (i != 1 || !passpci))
584 printf("Error: USB 3.0 module should be the first connected module or come right after Passthrough Mini-PCIe module!\n");
585 else
586 ++usb;
587 break;
588 case MOX_MODULE_PASSPCI:
589 if (passpci)
590 printf("Error: Only one Passthrough Mini-PCIe module is supported!\n");
591 else if (i != 0)
592 printf("Error: Passthrough Mini-PCIe module should be the first connected module!\n");
593 else
594 ++passpci;
595 }
596 }
597
598 return 0;
599 }
600
601 static struct udevice *mox_mdio_bus(void)
602 {
603 struct udevice *bus;
604 ofnode node;
605
606 node = ofnode_by_compatible(ofnode_null(), "marvell,orion-mdio");
607 if (!ofnode_valid(node))
608 goto err;
609
610 dm_mdio_probe_devices();
611
612 if (uclass_get_device_by_ofnode(UCLASS_MDIO, node, &bus))
613 goto err;
614
615 return bus;
616 err:
617 printf("Cannot get MDIO bus device!\n");
618 return NULL;
619 }
620
621 int last_stage_init(void)
622 {
623 struct gpio_desc reset_gpio = {};
624
625 /* configure modules */
626 if (get_reset_gpio(&reset_gpio) < 0)
627 goto handle_reset_btn;
628
629 if (peridot > 0) {
630 if (configure_peridots(&reset_gpio) < 0) {
631 printf("Cannot configure Peridot modules!\n");
632 peridot = 0;
633 }
634 } else {
635 dm_gpio_set_value(&reset_gpio, 1);
636 mdelay(50);
637 dm_gpio_set_value(&reset_gpio, 0);
638 mdelay(50);
639 }
640
641 /*
642 * check if the addresses are set by reading Scratch & Misc register
643 * 0x70 of Peridot (and potentially Topaz) modules
644 */
645 if (peridot || topaz) {
646 struct udevice *bus = mox_mdio_bus();
647
648 if (bus) {
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