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 #define ETH1_PATH "/soc/internal-regs@d0000000/ethernet@40000"
45 #define MDIO_PATH "/soc/internal-regs@d0000000/mdio@32004"
46 #define SFP_GPIO_PATH "/soc/internal-regs@d0000000/spi@10600/moxtet@1/gpio@0"
47 #define PCIE_PATH "/soc/pcie@d0070000"
48 #define SFP_PATH "/sfp"
49 #define LED_PATH "/leds/led"
50 #define BUTTON_PATH "/gpio-keys/reset"
52 DECLARE_GLOBAL_DATA_PTR;
54 #if defined(CONFIG_OF_BOARD_FIXUP)
55 int board_fix_fdt(void *blob)
57 u8 topology[MAX_MOX_MODULES];
62 * SPI driver is not loaded in driver model yet, but we have to find out
63 * if pcie should be enabled in U-Boot's device tree. Therefore we have
64 * to read SPI by reading/writing SPI registers directly
67 writel(0x10df, ARMADA_37XX_SPI_CFG);
68 /* put pin from GPIO to SPI mode */
69 clrbits_le32(ARMADA_37XX_NB_GPIO_SEL, BIT(12));
71 setbits_le32(ARMADA_37XX_SPI_CTRL, BIT(17));
73 while (!(readl(ARMADA_37XX_SPI_CTRL) & 0x2))
76 for (i = 0; i < MAX_MOX_MODULES; ++i) {
77 writel(0x0, ARMADA_37XX_SPI_DOUT);
79 while (!(readl(ARMADA_37XX_SPI_CTRL) & 0x2))
82 topology[i] = readl(ARMADA_37XX_SPI_DIN) & 0xff;
83 if (topology[i] == 0xff)
92 clrbits_le32(ARMADA_37XX_SPI_CTRL, BIT(17));
94 if (size > 1 && (topology[1] == MOX_MODULE_PCI ||
95 topology[1] == MOX_MODULE_USB3 ||
96 topology[1] == MOX_MODULE_PASSPCI))
101 node = fdt_path_offset(blob, PCIE_PATH);
104 printf("Cannot find PCIe node in U-Boot's device tree!\n");
108 if (fdt_setprop_string(blob, node, "status",
109 enable ? "okay" : "disabled") < 0) {
110 printf("Cannot %s PCIe in U-Boot's device tree!\n",
111 enable ? "enable" : "disable");
115 if (a3700_fdt_fix_pcie_regions(blob) < 0) {
116 printf("Cannot fix PCIe regions in U-Boot's device tree!\n");
126 /* address of boot parameters */
127 gd->bd->bi_boot_params = CONFIG_SYS_SDRAM_BASE + 0x100;
132 static int mox_do_spi(u8 *in, u8 *out, size_t size)
134 struct spi_slave *slave;
138 ret = spi_get_bus_and_cs(0, 1, 1000000, SPI_CPHA | SPI_CPOL,
139 "spi_generic_drv", "moxtet@1", &dev,
144 ret = spi_claim_bus(slave);
148 ret = spi_xfer(slave, size * 8, out, in, SPI_XFER_ONCE);
150 spi_release_bus(slave);
152 spi_free_slave(slave);
157 static int mox_get_topology(const u8 **ptopology, int *psize, int *pis_sd)
160 static u8 topology[MAX_MOX_MODULES - 1];
162 u8 din[MAX_MOX_MODULES], dout[MAX_MOX_MODULES];
167 *ptopology = topology;
175 memset(din, 0, MAX_MOX_MODULES);
176 memset(dout, 0, MAX_MOX_MODULES);
178 ret = mox_do_spi(din, dout, MAX_MOX_MODULES);
184 else if (din[0] == 0x00)
189 for (i = 1; i < MAX_MOX_MODULES && din[i] != 0xff; ++i)
190 topology[i - 1] = din[i] & 0xf;
194 *ptopology = topology;
203 int comphy_update_map(struct comphy_map *serdes_map, int count)
205 int ret, i, size, sfpindex = -1, swindex = -1;
208 ret = mox_get_topology(&topology, &size, NULL);
212 for (i = 0; i < size; ++i) {
213 if (topology[i] == MOX_MODULE_SFP && sfpindex == -1)
215 else if ((topology[i] == MOX_MODULE_TOPAZ ||
216 topology[i] == MOX_MODULE_PERIDOT) &&
221 if (sfpindex >= 0 && swindex >= 0) {
222 if (sfpindex < swindex)
223 serdes_map[0].speed = COMPHY_SPEED_1_25G;
225 serdes_map[0].speed = COMPHY_SPEED_3_125G;
226 } else if (sfpindex >= 0) {
227 serdes_map[0].speed = COMPHY_SPEED_1_25G;
228 } else if (swindex >= 0) {
229 serdes_map[0].speed = COMPHY_SPEED_3_125G;
235 #define SW_SMI_CMD_R(d, r) (0x9800 | (((d) & 0x1f) << 5) | ((r) & 0x1f))
236 #define SW_SMI_CMD_W(d, r) (0x9400 | (((d) & 0x1f) << 5) | ((r) & 0x1f))
238 static int sw_multi_read(struct mii_dev *bus, int sw, int dev, int reg)
240 bus->write(bus, sw, 0, 0, SW_SMI_CMD_R(dev, reg));
242 return bus->read(bus, sw, 0, 1);
245 static void sw_multi_write(struct mii_dev *bus, int sw, int dev, int reg,
248 bus->write(bus, sw, 0, 1, val);
249 bus->write(bus, sw, 0, 0, SW_SMI_CMD_W(dev, reg));
253 static int sw_scratch_read(struct mii_dev *bus, int sw, int reg)
255 sw_multi_write(bus, sw, 0x1c, 0x1a, (reg & 0x7f) << 8);
256 return sw_multi_read(bus, sw, 0x1c, 0x1a) & 0xff;
259 static void sw_led_write(struct mii_dev *bus, int sw, int port, int reg,
262 sw_multi_write(bus, sw, port, 0x16, 0x8000 | ((reg & 7) << 12)
266 static void sw_blink_leds(struct mii_dev *bus, int peridot, int topaz)
274 { 2, 0xef, 1 }, { 2, 0xfe, 1 }, { 2, 0x33, 0 },
275 { 4, 0xef, 1 }, { 4, 0xfe, 1 }, { 4, 0x33, 0 },
276 { 3, 0xfe, 1 }, { 3, 0xef, 1 }, { 3, 0x33, 0 },
277 { 1, 0xfe, 1 }, { 1, 0xef, 1 }, { 1, 0x33, 0 }
280 for (i = 0; i < 12; ++i) {
281 for (p = 0; p < peridot; ++p) {
282 sw_led_write(bus, 0x10 + p, regs[i].port, 0,
284 sw_led_write(bus, 0x10 + p, regs[i].port + 4, 0,
288 sw_led_write(bus, 0x2, 0x10 + regs[i].port, 0,
297 static void check_switch_address(struct mii_dev *bus, int addr)
299 if (sw_scratch_read(bus, addr, 0x70) >> 3 != addr)
300 printf("Check of switch MDIO address failed for 0x%02x\n",
304 static int sfp, pci, topaz, peridot, usb, passpci;
305 static int sfp_pos, peridot_pos[3];
306 static int module_count;
308 static int configure_peridots(struct gpio_desc *reset_gpio)
311 u8 dout[MAX_MOX_MODULES];
313 memset(dout, 0, MAX_MOX_MODULES);
315 /* set addresses of Peridot modules */
316 for (i = 0; i < peridot; ++i)
317 dout[module_count - peridot_pos[i]] = (~i) & 3;
320 * if there is a SFP module connected to the last Peridot module, set
321 * the P10_SMODE to 1 for the Peridot module
324 dout[module_count - peridot_pos[i - 1]] |= 1 << 3;
326 dm_gpio_set_value(reset_gpio, 1);
329 ret = mox_do_spi(NULL, dout, module_count + 1);
332 dm_gpio_set_value(reset_gpio, 0);
339 static int get_reset_gpio(struct gpio_desc *reset_gpio)
343 node = fdt_node_offset_by_compatible(gd->fdt_blob, 0, "cznic,moxtet");
345 printf("Cannot find Moxtet bus device node!\n");
349 gpio_request_by_name_nodev(offset_to_ofnode(node), "reset-gpios", 0,
350 reset_gpio, GPIOD_IS_OUT);
352 if (!dm_gpio_is_valid(reset_gpio)) {
353 printf("Cannot find reset GPIO for Moxtet bus!\n");
360 int misc_init_r(void)
365 ret = mbox_sp_get_board_info(NULL, mac1, mac2, NULL, NULL);
367 printf("Cannot read data from OTP!\n");
371 if (is_valid_ethaddr(mac1) && !env_get("ethaddr"))
372 eth_env_set_enetaddr("ethaddr", mac1);
374 if (is_valid_ethaddr(mac2) && !env_get("eth1addr"))
375 eth_env_set_enetaddr("eth1addr", mac2);
380 static void mox_phy_modify(struct phy_device *phydev, int page, int reg,
385 val = phydev->drv->readext(phydev, MDIO_DEVAD_NONE, page, reg);
388 phydev->drv->writeext(phydev, MDIO_DEVAD_NONE, page, reg, val);
391 static void mox_phy_leds_start_blinking(void)
393 struct phy_device *phydev;
396 bus = miiphy_get_dev_by_name("neta@30000");
398 printf("Cannot get MDIO bus device!\n");
402 phydev = phy_find_by_mask(bus, BIT(1), PHY_INTERFACE_MODE_RGMII);
404 printf("Cannot get ethernet PHY!\n");
408 mox_phy_modify(phydev, 3, 0x12, 0x700, 0x400);
409 mox_phy_modify(phydev, 3, 0x10, 0xff, 0xbb);
412 static bool read_reset_button(void)
414 struct udevice *button, *led;
417 if (device_get_global_by_ofnode(ofnode_path(BUTTON_PATH), &button)) {
418 printf("Cannot find reset button!\n");
422 if (device_get_global_by_ofnode(ofnode_path(LED_PATH), &led)) {
423 printf("Cannot find status LED!\n");
427 led_set_state(led, LEDST_ON);
429 for (i = 0; i < 21; ++i) {
430 if (button_get_state(button) != BUTTON_ON)
436 led_set_state(led, LEDST_OFF);
441 static void handle_reset_button(void)
443 const char * const vars[1] = { "bootcmd_rescue", };
446 * Ensure that bootcmd_rescue has always stock value, so that running
448 * always works correctly.
450 env_set_default_vars(1, (char * const *)vars, 0);
452 if (read_reset_button()) {
453 const char * const vars[2] = {
459 * Set the above envs to their default values, in case the user
460 * managed to break them.
462 env_set_default_vars(2, (char * const *)vars, 0);
464 /* Ensure bootcmd_rescue is used by distroboot */
465 env_set("boot_targets", "rescue");
467 /* start blinking PHY LEDs */
468 mox_phy_leds_start_blinking();
470 printf("RESET button was pressed, overwriting boot_targets!\n");
473 * In case the user somehow managed to save environment with
474 * boot_targets=rescue, reset boot_targets to default value.
475 * This could happen in subsequent commands if bootcmd_rescue
478 if (!strcmp(env_get("boot_targets"), "rescue")) {
479 const char * const vars[1] = {
483 env_set_default_vars(1, (char * const *)vars, 0);
488 static void mox_print_info(void)
490 int ret, board_version, ram_size;
494 ret = mbox_sp_get_board_info(&serial_number, NULL, NULL, &board_version,
499 printf("Turris Mox:\n");
500 printf(" Board version: %i\n", board_version);
501 printf(" RAM size: %i MiB\n", ram_size);
502 printf(" Serial Number: %016llX\n", serial_number);
504 pub_key = mox_sp_get_ecdsa_public_key();
506 printf(" ECDSA Public Key: %s\n", pub_key);
508 printf("Cannot read ECDSA Public Key\n");
511 int last_stage_init(void)
517 struct gpio_desc reset_gpio = {};
521 ret = mox_get_topology(&topology, &module_count, &is_sd);
523 printf("Cannot read module topology!\n");
527 printf(" SD/eMMC version: %s\n", is_sd ? "SD" : "eMMC");
530 printf("Module Topology:\n");
532 for (i = 0; i < module_count; ++i) {
533 switch (topology[i]) {
535 printf("% 4i: SFP Module\n", i + 1);
538 printf("% 4i: Mini-PCIe Module\n", i + 1);
540 case MOX_MODULE_TOPAZ:
541 printf("% 4i: Topaz Switch Module (4-port)\n", i + 1);
543 case MOX_MODULE_PERIDOT:
544 printf("% 4i: Peridot Switch Module (8-port)\n", i + 1);
546 case MOX_MODULE_USB3:
547 printf("% 4i: USB 3.0 Module (4 ports)\n", i + 1);
549 case MOX_MODULE_PASSPCI:
550 printf("% 4i: Passthrough Mini-PCIe Module\n", i + 1);
553 printf("% 4i: unknown (ID %i)\n", i + 1, topology[i]);
557 /* now check if modules are connected in supported mode */
559 for (i = 0; i < module_count; ++i) {
560 switch (topology[i]) {
563 printf("Error: Only one SFP module is supported!\n");
565 printf("Error: SFP module cannot be connected after Topaz Switch module!\n");
573 printf("Error: Only one Mini-PCIe module is supported!\n");
575 printf("Error: Mini-PCIe module cannot come after USB 3.0 module!\n");
576 else if (i && (i != 1 || !passpci))
577 printf("Error: Mini-PCIe module should be the first connected module or come right after Passthrough Mini-PCIe module!\n");
581 case MOX_MODULE_TOPAZ:
583 printf("Error: Only one Topaz module is supported!\n");
584 else if (peridot >= 3)
585 printf("Error: At most two Peridot modules can come before Topaz module!\n");
589 case MOX_MODULE_PERIDOT:
591 printf("Error: Peridot module must come before SFP or Topaz module!\n");
592 } else if (peridot >= 3) {
593 printf("Error: At most three Peridot modules are supported!\n");
595 peridot_pos[peridot] = i;
599 case MOX_MODULE_USB3:
601 printf("Error: USB 3.0 module cannot come after Mini-PCIe module!\n");
603 printf("Error: Only one USB 3.0 module is supported!\n");
604 else if (i && (i != 1 || !passpci))
605 printf("Error: USB 3.0 module should be the first connected module or come right after Passthrough Mini-PCIe module!\n");
609 case MOX_MODULE_PASSPCI:
611 printf("Error: Only one Passthrough Mini-PCIe module is supported!\n");
613 printf("Error: Passthrough Mini-PCIe module should be the first connected module!\n");
619 /* now configure modules */
621 if (get_reset_gpio(&reset_gpio) < 0)
625 if (configure_peridots(&reset_gpio) < 0) {
626 printf("Cannot configure Peridot modules!\n");
630 dm_gpio_set_value(&reset_gpio, 1);
632 dm_gpio_set_value(&reset_gpio, 0);
636 if (peridot || topaz) {
638 * now check if the addresses are set by reading Scratch & Misc
639 * register 0x70 of Peridot (and potentially Topaz) modules
642 bus = miiphy_get_dev_by_name("neta@30000");
644 printf("Cannot get MDIO bus device!\n");
646 for (i = 0; i < peridot; ++i)
647 check_switch_address(bus, 0x10 + i);
650 check_switch_address(bus, 0x2);
652 sw_blink_leds(bus, peridot, topaz);
658 handle_reset_button();
663 #if defined(CONFIG_OF_BOARD_SETUP)
665 static int vnode_by_path(void *blob, const char *fmt, va_list ap)
669 vsnprintf(path, 128, fmt, ap);
670 return fdt_path_offset(blob, path);
673 static int node_by_path(void *blob, const char *fmt, ...)
679 res = vnode_by_path(blob, fmt, ap);
685 static int phandle_by_path(void *blob, const char *fmt, ...)
688 int node, phandle, res;
691 node = vnode_by_path(blob, fmt, ap);
697 phandle = fdt_get_phandle(blob, node);
701 phandle = fdt_get_max_phandle(blob);
707 res = fdt_setprop_u32(blob, node, "linux,phandle", phandle);
711 res = fdt_setprop_u32(blob, node, "phandle", phandle);
718 static int enable_by_path(void *blob, const char *fmt, ...)
724 node = vnode_by_path(blob, fmt, ap);
730 return fdt_setprop_string(blob, node, "status", "okay");
733 static bool is_topaz(int id)
735 return topaz && id == peridot + topaz - 1;
738 static int switch_addr(int id)
740 return is_topaz(id) ? 0x2 : 0x10 + id;
743 static int setup_switch(void *blob, int id)
745 int res, addr, i, node, phandle;
747 addr = switch_addr(id);
749 /* first enable the switch by setting status = "okay" */
750 res = enable_by_path(blob, MDIO_PATH "/switch%i@%x", id, addr);
755 * now if there are more switches or a SFP module coming after,
756 * enable corresponding ports
758 if (id < peridot + topaz - 1) {
759 res = enable_by_path(blob,
760 MDIO_PATH "/switch%i@%x/ports/port@a",
762 } else if (id == peridot - 1 && !topaz && sfp) {
763 res = enable_by_path(blob,
764 MDIO_PATH "/switch%i@%x/ports/port-sfp@a",
772 if (id >= peridot + topaz - 1)
775 /* finally change link property if needed */
776 node = node_by_path(blob, MDIO_PATH "/switch%i@%x/ports/port@a", id,
781 for (i = id + 1; i < peridot + topaz; ++i) {
782 phandle = phandle_by_path(blob,
783 MDIO_PATH "/switch%i@%x/ports/port@%x",
785 is_topaz(i) ? 5 : 9);
790 res = fdt_setprop_u32(blob, node, "link", phandle);
792 res = fdt_appendprop_u32(blob, node, "link", phandle);
800 static int remove_disabled_nodes(void *blob)
805 offset = fdt_node_offset_by_prop_value(blob, -1, "status",
810 res = fdt_del_node(blob, offset);
818 int ft_board_setup(void *blob, struct bd_info *bd)
820 int node, phandle, res;
823 * If MOX B (PCI), MOX F (USB) or MOX G (Passthrough PCI) modules are
824 * connected, enable the PCIe node.
826 if (pci || usb || passpci) {
827 node = fdt_path_offset(blob, PCIE_PATH);
831 res = fdt_setprop_string(blob, node, "status", "okay");
835 /* Fix PCIe regions for devices with 4 GB RAM */
836 res = a3700_fdt_fix_pcie_regions(blob);
842 * If MOX C (Topaz switch) and/or MOX E (Peridot switch) are connected,
843 * enable the eth1 node and setup the switches.
845 if (peridot || topaz) {
848 res = enable_by_path(blob, ETH1_PATH);
852 for (i = 0; i < peridot + topaz; ++i) {
853 res = setup_switch(blob, i);
860 * If MOX D (SFP cage module) is connected, enable the SFP node and eth1
861 * node. If there is no Peridot switch between MOX A and MOX D, add link
862 * to the SFP node to eth1 node.
863 * Also enable and configure SFP GPIO controller node.
866 res = enable_by_path(blob, SFP_PATH);
870 res = enable_by_path(blob, ETH1_PATH);
875 phandle = phandle_by_path(blob, SFP_PATH);
879 node = node_by_path(blob, ETH1_PATH);
883 res = fdt_setprop_u32(blob, node, "sfp", phandle);
887 res = fdt_setprop_string(blob, node, "phy-mode",
893 res = enable_by_path(blob, SFP_GPIO_PATH);
900 /* moxtet-sfp is on non-zero position, change default */
901 node = node_by_path(blob, SFP_GPIO_PATH);
905 res = fdt_setprop_u32(blob, node, "reg", sfp_pos);
909 sprintf(newname, "gpio@%x", sfp_pos);
911 res = fdt_set_name(blob, node, newname);
917 fdt_fixup_ethernet(blob);
919 /* Finally remove disabled nodes, as per Rob Herring's request. */
920 remove_disabled_nodes(blob);