1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2018 Marek Behun <marek.behun@nic.cz>
7 #include <asm/arch/cpu.h>
8 #include <asm/arch/soc.h>
10 #include <asm/global_data.h>
17 #include <fdt_support.h>
20 #include <linux/delay.h>
21 #include <linux/libfdt.h>
22 #include <linux/string.h>
24 #include <mvebu/comphy.h>
29 #define MAX_MOX_MODULES 10
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
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))
44 DECLARE_GLOBAL_DATA_PTR;
46 #if defined(CONFIG_OF_BOARD_FIXUP)
47 int board_fix_fdt(void *blob)
49 u8 topology[MAX_MOX_MODULES];
50 enum fdt_status status;
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
59 writel(0x10df, ARMADA_37XX_SPI_CFG);
60 /* put pin from GPIO to SPI mode */
61 clrbits_le32(ARMADA_37XX_NB_GPIO_SEL, BIT(12));
63 setbits_le32(ARMADA_37XX_SPI_CTRL, BIT(17));
65 while (!(readl(ARMADA_37XX_SPI_CTRL) & 0x2))
68 for (i = 0; i < MAX_MOX_MODULES; ++i) {
69 writel(0x0, ARMADA_37XX_SPI_DOUT);
71 while (!(readl(ARMADA_37XX_SPI_CTRL) & 0x2))
74 topology[i] = readl(ARMADA_37XX_SPI_DIN) & 0xff;
75 if (topology[i] == 0xff)
84 clrbits_le32(ARMADA_37XX_SPI_CTRL, BIT(17));
86 if (size > 1 && (topology[1] == MOX_MODULE_PCI ||
87 topology[1] == MOX_MODULE_USB3 ||
88 topology[1] == MOX_MODULE_PASSPCI))
89 status = FDT_STATUS_OKAY;
91 status = FDT_STATUS_DISABLED;
93 ret = fdt_set_status_by_compatible(blob, "marvell,armada-3700-pcie",
96 printf("Cannot set status for PCIe in U-Boot's device tree: %s!\n",
101 if (a3700_fdt_fix_pcie_regions(blob) < 0) {
102 printf("Cannot fix PCIe regions in U-Boot's device tree!\n");
112 /* address of boot parameters */
113 gd->bd->bi_boot_params = CONFIG_SYS_SDRAM_BASE + 0x100;
118 static int mox_do_spi(u8 *in, u8 *out, size_t size)
120 struct spi_slave *slave;
124 ret = spi_get_bus_and_cs(0, 1, 1000000, SPI_CPHA | SPI_CPOL,
125 "spi_generic_drv", "moxtet@1", &dev,
130 ret = spi_claim_bus(slave);
134 ret = spi_xfer(slave, size * 8, out, in, SPI_XFER_ONCE);
136 spi_release_bus(slave);
138 spi_free_slave(slave);
143 static int mox_get_topology(const u8 **ptopology, int *psize, int *pis_sd)
146 static u8 topology[MAX_MOX_MODULES - 1];
148 u8 din[MAX_MOX_MODULES], dout[MAX_MOX_MODULES];
153 *ptopology = topology;
161 memset(din, 0, MAX_MOX_MODULES);
162 memset(dout, 0, MAX_MOX_MODULES);
164 ret = mox_do_spi(din, dout, MAX_MOX_MODULES);
170 else if (din[0] == 0x00)
175 for (i = 1; i < MAX_MOX_MODULES && din[i] != 0xff; ++i)
176 topology[i - 1] = din[i] & 0xf;
180 *ptopology = topology;
189 int comphy_update_map(struct comphy_map *serdes_map, int count)
191 int ret, i, size, sfpindex = -1, swindex = -1;
194 ret = mox_get_topology(&topology, &size, NULL);
198 for (i = 0; i < size; ++i) {
199 if (topology[i] == MOX_MODULE_SFP && sfpindex == -1)
201 else if ((topology[i] == MOX_MODULE_TOPAZ ||
202 topology[i] == MOX_MODULE_PERIDOT) &&
207 if (sfpindex >= 0 && swindex >= 0) {
208 if (sfpindex < swindex)
209 serdes_map[0].speed = COMPHY_SPEED_1_25G;
211 serdes_map[0].speed = COMPHY_SPEED_3_125G;
212 } else if (sfpindex >= 0) {
213 serdes_map[0].speed = COMPHY_SPEED_1_25G;
214 } else if (swindex >= 0) {
215 serdes_map[0].speed = COMPHY_SPEED_3_125G;
221 #define SW_SMI_CMD_R(d, r) (0x9800 | (((d) & 0x1f) << 5) | ((r) & 0x1f))
222 #define SW_SMI_CMD_W(d, r) (0x9400 | (((d) & 0x1f) << 5) | ((r) & 0x1f))
224 static int sw_multi_read(struct mii_dev *bus, int sw, int dev, int reg)
226 bus->write(bus, sw, 0, 0, SW_SMI_CMD_R(dev, reg));
228 return bus->read(bus, sw, 0, 1);
231 static void sw_multi_write(struct mii_dev *bus, int sw, int dev, int reg,
234 bus->write(bus, sw, 0, 1, val);
235 bus->write(bus, sw, 0, 0, SW_SMI_CMD_W(dev, reg));
239 static int sw_scratch_read(struct mii_dev *bus, int sw, int reg)
241 sw_multi_write(bus, sw, 0x1c, 0x1a, (reg & 0x7f) << 8);
242 return sw_multi_read(bus, sw, 0x1c, 0x1a) & 0xff;
245 static void sw_led_write(struct mii_dev *bus, int sw, int port, int reg,
248 sw_multi_write(bus, sw, port, 0x16, 0x8000 | ((reg & 7) << 12)
252 static void sw_blink_leds(struct mii_dev *bus, int peridot, int topaz)
260 { 2, 0xef, 1 }, { 2, 0xfe, 1 }, { 2, 0x33, 0 },
261 { 4, 0xef, 1 }, { 4, 0xfe, 1 }, { 4, 0x33, 0 },
262 { 3, 0xfe, 1 }, { 3, 0xef, 1 }, { 3, 0x33, 0 },
263 { 1, 0xfe, 1 }, { 1, 0xef, 1 }, { 1, 0x33, 0 }
266 for (i = 0; i < 12; ++i) {
267 for (p = 0; p < peridot; ++p) {
268 sw_led_write(bus, 0x10 + p, regs[i].port, 0,
270 sw_led_write(bus, 0x10 + p, regs[i].port + 4, 0,
274 sw_led_write(bus, 0x2, 0x10 + regs[i].port, 0,
283 static void check_switch_address(struct mii_dev *bus, int addr)
285 if (sw_scratch_read(bus, addr, 0x70) >> 3 != addr)
286 printf("Check of switch MDIO address failed for 0x%02x\n",
290 static int sfp, pci, topaz, peridot, usb, passpci;
291 static int sfp_pos, peridot_pos[3];
292 static int module_count;
294 static int configure_peridots(struct gpio_desc *reset_gpio)
297 u8 dout[MAX_MOX_MODULES];
299 memset(dout, 0, MAX_MOX_MODULES);
301 /* set addresses of Peridot modules */
302 for (i = 0; i < peridot; ++i)
303 dout[module_count - peridot_pos[i]] = (~i) & 3;
306 * if there is a SFP module connected to the last Peridot module, set
307 * the P10_SMODE to 1 for the Peridot module
310 dout[module_count - peridot_pos[i - 1]] |= 1 << 3;
312 dm_gpio_set_value(reset_gpio, 1);
315 ret = mox_do_spi(NULL, dout, module_count + 1);
318 dm_gpio_set_value(reset_gpio, 0);
325 static int get_reset_gpio(struct gpio_desc *reset_gpio)
329 node = fdt_node_offset_by_compatible(gd->fdt_blob, 0, "cznic,moxtet");
331 printf("Cannot find Moxtet bus device node!\n");
335 gpio_request_by_name_nodev(offset_to_ofnode(node), "reset-gpios", 0,
336 reset_gpio, GPIOD_IS_OUT);
338 if (!dm_gpio_is_valid(reset_gpio)) {
339 printf("Cannot find reset GPIO for Moxtet bus!\n");
346 int misc_init_r(void)
351 ret = mbox_sp_get_board_info(NULL, mac[0], mac[1], NULL, NULL);
353 printf("Cannot read data from OTP!\n");
357 for (i = 0; i < 2; ++i) {
360 if (is_valid_ethaddr(mac[i]) &&
361 !eth_env_get_enetaddr_by_index("eth", i, oldmac))
362 eth_env_set_enetaddr_by_index("eth", i, mac[i]);
368 static void mox_phy_modify(struct phy_device *phydev, int page, int reg,
373 val = phydev->drv->readext(phydev, MDIO_DEVAD_NONE, page, reg);
376 phydev->drv->writeext(phydev, MDIO_DEVAD_NONE, page, reg, val);
379 static void mox_phy_leds_start_blinking(void)
381 struct phy_device *phydev;
384 bus = miiphy_get_dev_by_name("neta@30000");
386 printf("Cannot get MDIO bus device!\n");
390 phydev = phy_find_by_mask(bus, BIT(1), PHY_INTERFACE_MODE_RGMII);
392 printf("Cannot get ethernet PHY!\n");
396 mox_phy_modify(phydev, 3, 0x12, 0x700, 0x400);
397 mox_phy_modify(phydev, 3, 0x10, 0xff, 0xbb);
400 static bool read_reset_button(void)
402 struct udevice *button, *led;
405 if (device_get_global_by_ofnode(
406 ofnode_first_subnode(ofnode_by_compatible(ofnode_null(),
409 printf("Cannot find reset button!\n");
413 if (device_get_global_by_ofnode(
414 ofnode_first_subnode(ofnode_by_compatible(ofnode_null(),
417 printf("Cannot find status LED!\n");
421 led_set_state(led, LEDST_ON);
423 for (i = 0; i < 21; ++i) {
424 if (button_get_state(button) != BUTTON_ON)
430 led_set_state(led, LEDST_OFF);
435 static void handle_reset_button(void)
437 const char * const vars[1] = { "bootcmd_rescue", };
440 * Ensure that bootcmd_rescue has always stock value, so that running
442 * always works correctly.
444 env_set_default_vars(1, (char * const *)vars, 0);
446 if (read_reset_button()) {
447 const char * const vars[2] = {
453 * Set the above envs to their default values, in case the user
454 * managed to break them.
456 env_set_default_vars(2, (char * const *)vars, 0);
458 /* Ensure bootcmd_rescue is used by distroboot */
459 env_set("boot_targets", "rescue");
461 /* start blinking PHY LEDs */
462 mox_phy_leds_start_blinking();
464 printf("RESET button was pressed, overwriting boot_targets!\n");
467 * In case the user somehow managed to save environment with
468 * boot_targets=rescue, reset boot_targets to default value.
469 * This could happen in subsequent commands if bootcmd_rescue
472 if (!strcmp(env_get("boot_targets"), "rescue")) {
473 const char * const vars[1] = {
477 env_set_default_vars(1, (char * const *)vars, 0);
482 int show_board_info(void)
484 int i, ret, board_version, ram_size, is_sd;
489 printf("Model: CZ.NIC Turris Mox Board\n");
491 ret = mbox_sp_get_board_info(&serial_number, NULL, NULL, &board_version,
494 printf(" Cannot read board info: %i\n", ret);
496 printf(" Board version: %i\n", board_version);
497 printf(" RAM size: %i MiB\n", ram_size);
498 printf(" Serial Number: %016llX\n", serial_number);
501 pub_key = mox_sp_get_ecdsa_public_key();
503 printf(" ECDSA Public Key: %s\n", pub_key);
505 printf(" Cannot read ECDSA Public Key\n");
507 ret = mox_get_topology(&topology, &module_count, &is_sd);
509 printf("Cannot read module topology!\n");
511 printf(" SD/eMMC version: %s\n", is_sd ? "SD" : "eMMC");
514 printf("Module Topology:\n");
516 for (i = 0; i < module_count; ++i) {
517 switch (topology[i]) {
519 printf("% 4i: SFP Module\n", i + 1);
522 printf("% 4i: Mini-PCIe Module\n", i + 1);
524 case MOX_MODULE_TOPAZ:
525 printf("% 4i: Topaz Switch Module (4-port)\n", i + 1);
527 case MOX_MODULE_PERIDOT:
528 printf("% 4i: Peridot Switch Module (8-port)\n", i + 1);
530 case MOX_MODULE_USB3:
531 printf("% 4i: USB 3.0 Module (4 ports)\n", i + 1);
533 case MOX_MODULE_PASSPCI:
534 printf("% 4i: Passthrough Mini-PCIe Module\n", i + 1);
537 printf("% 4i: unknown (ID %i)\n", i + 1, topology[i]);
541 /* check if modules are connected in supported mode */
542 for (i = 0; i < module_count; ++i) {
543 switch (topology[i]) {
546 printf("Error: Only one SFP module is supported!\n");
548 printf("Error: SFP module cannot be connected after Topaz Switch module!\n");
556 printf("Error: Only one Mini-PCIe module is supported!\n");
558 printf("Error: Mini-PCIe module cannot come after USB 3.0 module!\n");
559 else if (i && (i != 1 || !passpci))
560 printf("Error: Mini-PCIe module should be the first connected module or come right after Passthrough Mini-PCIe module!\n");
564 case MOX_MODULE_TOPAZ:
566 printf("Error: Only one Topaz module is supported!\n");
567 else if (peridot >= 3)
568 printf("Error: At most two Peridot modules can come before Topaz module!\n");
572 case MOX_MODULE_PERIDOT:
574 printf("Error: Peridot module must come before SFP or Topaz module!\n");
575 } else if (peridot >= 3) {
576 printf("Error: At most three Peridot modules are supported!\n");
578 peridot_pos[peridot] = i;
582 case MOX_MODULE_USB3:
584 printf("Error: USB 3.0 module cannot come after Mini-PCIe module!\n");
586 printf("Error: Only one USB 3.0 module is supported!\n");
587 else if (i && (i != 1 || !passpci))
588 printf("Error: USB 3.0 module should be the first connected module or come right after Passthrough Mini-PCIe module!\n");
592 case MOX_MODULE_PASSPCI:
594 printf("Error: Only one Passthrough Mini-PCIe module is supported!\n");
596 printf("Error: Passthrough Mini-PCIe module should be the first connected module!\n");
608 int last_stage_init(void)
610 struct gpio_desc reset_gpio = {};
612 /* configure modules */
613 if (get_reset_gpio(&reset_gpio) < 0)
614 goto handle_reset_btn;
617 if (configure_peridots(&reset_gpio) < 0) {
618 printf("Cannot configure Peridot modules!\n");
622 dm_gpio_set_value(&reset_gpio, 1);
624 dm_gpio_set_value(&reset_gpio, 0);
629 * check if the addresses are set by reading Scratch & Misc register
630 * 0x70 of Peridot (and potentially Topaz) modules
632 if (peridot || topaz) {
635 bus = miiphy_get_dev_by_name("neta@30000");
637 printf("Cannot get MDIO bus device!\n");
641 for (i = 0; i < peridot; ++i)
642 check_switch_address(bus, 0x10 + i);
645 check_switch_address(bus, 0x2);
647 sw_blink_leds(bus, peridot, topaz);
652 handle_reset_button();
657 #if defined(CONFIG_OF_BOARD_SETUP)
659 static bool is_topaz(int id)
661 return topaz && id == peridot + topaz - 1;
664 static int switch_addr(int id)
666 return is_topaz(id) ? 0x2 : 0x10 + id;
669 static int setup_switch(void *blob, int id)
671 int res, addr, i, node;
674 node = fdt_node_offset_by_compatible(blob, -1, "marvell,orion-mdio");
678 res = fdt_get_path(blob, node, mdio_path, sizeof(mdio_path));
682 addr = switch_addr(id);
684 /* first enable the switch by setting status = "okay" */
685 res = fdt_status_okay_by_pathf(blob, "%s/switch%i@%x", mdio_path, id,
691 * now if there are more switches or a SFP module coming after,
692 * enable corresponding ports
694 if (id < peridot + topaz - 1) {
695 res = fdt_status_okay_by_pathf(blob,
696 "%s/switch%i@%x/ports/port@a",
697 mdio_path, id, addr);
698 } else if (id == peridot - 1 && !topaz && sfp) {
699 res = fdt_status_okay_by_pathf(blob,
700 "%s/switch%i@%x/ports/port-sfp@a",
701 mdio_path, id, addr);
708 if (id >= peridot + topaz - 1)
711 /* finally change link property if needed */
712 node = fdt_node_offset_by_pathf(blob, "%s/switch%i@%x/ports/port@a",
713 mdio_path, id, addr);
717 for (i = id + 1; i < peridot + topaz; ++i) {
718 unsigned int phandle;
720 phandle = fdt_create_phandle_by_pathf(blob,
721 "%s/switch%i@%x/ports/port@%x",
724 is_topaz(i) ? 5 : 9);
726 return -FDT_ERR_NOPHANDLES;
729 res = fdt_setprop_u32(blob, node, "link", phandle);
731 res = fdt_appendprop_u32(blob, node, "link", phandle);
739 static int remove_disabled_nodes(void *blob)
744 offset = fdt_node_offset_by_prop_value(blob, -1, "status",
749 res = fdt_del_node(blob, offset);
757 int ft_board_setup(void *blob, struct bd_info *bd)
762 * If MOX B (PCI), MOX F (USB) or MOX G (Passthrough PCI) modules are
763 * connected, enable the PCIe node.
765 if (pci || usb || passpci) {
766 res = fdt_status_okay_by_compatible(blob,
767 "marvell,armada-3700-pcie");
771 /* Fix PCIe regions for devices with 4 GB RAM */
772 res = a3700_fdt_fix_pcie_regions(blob);
778 * If MOX C (Topaz switch) and/or MOX E (Peridot switch) are connected,
779 * enable the eth1 node and setup the switches.
781 if (peridot || topaz) {
784 res = fdt_status_okay_by_alias(blob, "ethernet1");
788 for (i = 0; i < peridot + topaz; ++i) {
789 res = setup_switch(blob, i);
796 * If MOX D (SFP cage module) is connected, enable the SFP node and eth1
797 * node. If there is no Peridot switch between MOX A and MOX D, add link
798 * to the SFP node to eth1 node.
799 * Also enable and configure SFP GPIO controller node.
804 res = fdt_status_okay_by_compatible(blob, "sff,sfp");
808 res = fdt_status_okay_by_alias(blob, "ethernet1");
813 unsigned int phandle;
815 phandle = fdt_create_phandle_by_compatible(blob,
818 return -FDT_ERR_NOPHANDLES;
820 node = fdt_path_offset(blob, "ethernet1");
824 res = fdt_setprop_u32(blob, node, "sfp", phandle);
828 res = fdt_setprop_string(blob, node, "phy-mode",
834 res = fdt_status_okay_by_compatible(blob, "cznic,moxtet-gpio");
841 /* moxtet-sfp is on non-zero position, change default */
842 node = fdt_node_offset_by_compatible(blob, -1,
843 "cznic,moxtet-gpio");
847 res = fdt_setprop_u32(blob, node, "reg", sfp_pos);
851 sprintf(newname, "gpio@%x", sfp_pos);
853 res = fdt_set_name(blob, node, newname);
859 fdt_fixup_ethernet(blob);
861 /* Finally remove disabled nodes, as per Rob Herring's request. */
862 remove_disabled_nodes(blob);