1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2018 Marek Behun <marek.behun@nic.cz>
12 #include <mvebu/comphy.h>
14 #include <linux/string.h>
15 #include <linux/libfdt.h>
16 #include <fdt_support.h>
17 #include <environment.h>
21 #define MAX_MOX_MODULES 10
23 #define MOX_MODULE_SFP 0x1
24 #define MOX_MODULE_PCI 0x2
25 #define MOX_MODULE_TOPAZ 0x3
26 #define MOX_MODULE_PERIDOT 0x4
27 #define MOX_MODULE_USB3 0x5
28 #define MOX_MODULE_PASSPCI 0x6
30 #define ARMADA_37XX_NB_GPIO_SEL 0xd0013830
31 #define ARMADA_37XX_SPI_CTRL 0xd0010600
32 #define ARMADA_37XX_SPI_CFG 0xd0010604
33 #define ARMADA_37XX_SPI_DOUT 0xd0010608
34 #define ARMADA_37XX_SPI_DIN 0xd001060c
36 #define PCIE_PATH "/soc/pcie@d0070000"
38 DECLARE_GLOBAL_DATA_PTR;
43 gd->ram_size = (phys_size_t)get_ram_size(0, 0x40000000);
48 int dram_init_banksize(void)
50 gd->bd->bi_dram[0].start = (phys_addr_t)0;
51 gd->bd->bi_dram[0].size = gd->ram_size;
56 #if defined(CONFIG_OF_BOARD_FIXUP)
57 int board_fix_fdt(void *blob)
59 u8 topology[MAX_MOX_MODULES];
64 * SPI driver is not loaded in driver model yet, but we have to find out
65 * if pcie should be enabled in U-Boot's device tree. Therefore we have
66 * to read SPI by reading/writing SPI registers directly
69 writel(0x563fa, ARMADA_37XX_NB_GPIO_SEL);
70 writel(0x10df, ARMADA_37XX_SPI_CFG);
71 writel(0x2005b, ARMADA_37XX_SPI_CTRL);
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)
91 writel(0x5b, ARMADA_37XX_SPI_CTRL);
93 if (size > 1 && (topology[1] == MOX_MODULE_PCI ||
94 topology[1] == MOX_MODULE_USB3 ||
95 topology[1] == MOX_MODULE_PASSPCI))
100 node = fdt_path_offset(blob, PCIE_PATH);
103 printf("Cannot find PCIe node in U-Boot's device tree!\n");
107 if (fdt_setprop_string(blob, node, "status",
108 enable ? "okay" : "disabled") < 0) {
109 printf("Cannot %s PCIe in U-Boot's device tree!\n",
110 enable ? "enable" : "disable");
120 /* address of boot parameters */
121 gd->bd->bi_boot_params = CONFIG_SYS_SDRAM_BASE + 0x100;
126 static int mox_do_spi(u8 *in, u8 *out, size_t size)
128 struct spi_slave *slave;
132 ret = spi_get_bus_and_cs(0, 1, 1000000, SPI_CPHA | SPI_CPOL,
133 "spi_generic_drv", "moxtet@1", &dev,
138 ret = spi_claim_bus(slave);
142 ret = spi_xfer(slave, size * 8, out, in, SPI_XFER_ONCE);
144 spi_release_bus(slave);
146 spi_free_slave(slave);
151 static int mox_get_topology(const u8 **ptopology, int *psize, int *pis_sd)
154 static u8 topology[MAX_MOX_MODULES - 1];
156 u8 din[MAX_MOX_MODULES], dout[MAX_MOX_MODULES];
161 *ptopology = topology;
169 memset(din, 0, MAX_MOX_MODULES);
170 memset(dout, 0, MAX_MOX_MODULES);
172 ret = mox_do_spi(din, dout, MAX_MOX_MODULES);
178 else if (din[0] == 0x00)
183 for (i = 1; i < MAX_MOX_MODULES && din[i] != 0xff; ++i)
184 topology[i - 1] = din[i] & 0xf;
188 *ptopology = topology;
197 int comphy_update_map(struct comphy_map *serdes_map, int count)
199 int ret, i, size, sfpindex = -1, swindex = -1;
202 ret = mox_get_topology(&topology, &size, NULL);
206 for (i = 0; i < size; ++i) {
207 if (topology[i] == MOX_MODULE_SFP && sfpindex == -1)
209 else if ((topology[i] == MOX_MODULE_TOPAZ ||
210 topology[i] == MOX_MODULE_PERIDOT) &&
215 if (sfpindex >= 0 && swindex >= 0) {
216 if (sfpindex < swindex)
217 serdes_map[0].speed = PHY_SPEED_1_25G;
219 serdes_map[0].speed = PHY_SPEED_3_125G;
220 } else if (sfpindex >= 0) {
221 serdes_map[0].speed = PHY_SPEED_1_25G;
222 } else if (swindex >= 0) {
223 serdes_map[0].speed = PHY_SPEED_3_125G;
229 #define SW_SMI_CMD_R(d, r) (0x9800 | (((d) & 0x1f) << 5) | ((r) & 0x1f))
230 #define SW_SMI_CMD_W(d, r) (0x9400 | (((d) & 0x1f) << 5) | ((r) & 0x1f))
232 static int sw_multi_read(struct mii_dev *bus, int sw, int dev, int reg)
234 bus->write(bus, sw, 0, 0, SW_SMI_CMD_R(dev, reg));
236 return bus->read(bus, sw, 0, 1);
239 static void sw_multi_write(struct mii_dev *bus, int sw, int dev, int reg,
242 bus->write(bus, sw, 0, 1, val);
243 bus->write(bus, sw, 0, 0, SW_SMI_CMD_W(dev, reg));
247 static int sw_scratch_read(struct mii_dev *bus, int sw, int reg)
249 sw_multi_write(bus, sw, 0x1c, 0x1a, (reg & 0x7f) << 8);
250 return sw_multi_read(bus, sw, 0x1c, 0x1a) & 0xff;
253 static void sw_led_write(struct mii_dev *bus, int sw, int port, int reg,
256 sw_multi_write(bus, sw, port, 0x16, 0x8000 | ((reg & 7) << 12)
260 static void sw_blink_leds(struct mii_dev *bus, int peridot, int topaz)
268 { 2, 0xef, 1 }, { 2, 0xfe, 1 }, { 2, 0x33, 0 },
269 { 4, 0xef, 1 }, { 4, 0xfe, 1 }, { 4, 0x33, 0 },
270 { 3, 0xfe, 1 }, { 3, 0xef, 1 }, { 3, 0x33, 0 },
271 { 1, 0xfe, 1 }, { 1, 0xef, 1 }, { 1, 0x33, 0 }
274 for (i = 0; i < 12; ++i) {
275 for (p = 0; p < peridot; ++p) {
276 sw_led_write(bus, 0x10 + p, regs[i].port, 0,
278 sw_led_write(bus, 0x10 + p, regs[i].port + 4, 0,
282 sw_led_write(bus, 0x2, 0x10 + regs[i].port, 0,
291 static void check_switch_address(struct mii_dev *bus, int addr)
293 if (sw_scratch_read(bus, addr, 0x70) >> 3 != addr)
294 printf("Check of switch MDIO address failed for 0x%02x\n",
298 static int sfp, pci, topaz, peridot, usb, passpci;
299 static int sfp_pos, peridot_pos[3];
300 static int module_count;
302 static int configure_peridots(struct gpio_desc *reset_gpio)
305 u8 dout[MAX_MOX_MODULES];
307 memset(dout, 0, MAX_MOX_MODULES);
309 /* set addresses of Peridot modules */
310 for (i = 0; i < peridot; ++i)
311 dout[module_count - peridot_pos[i]] = (~i) & 3;
314 * if there is a SFP module connected to the last Peridot module, set
315 * the P10_SMODE to 1 for the Peridot module
318 dout[module_count - peridot_pos[i - 1]] |= 1 << 3;
320 dm_gpio_set_value(reset_gpio, 1);
323 ret = mox_do_spi(NULL, dout, module_count + 1);
326 dm_gpio_set_value(reset_gpio, 0);
333 static int get_reset_gpio(struct gpio_desc *reset_gpio)
337 node = fdt_node_offset_by_compatible(gd->fdt_blob, 0, "cznic,moxtet");
339 printf("Cannot find Moxtet bus device node!\n");
343 gpio_request_by_name_nodev(offset_to_ofnode(node), "reset-gpios", 0,
344 reset_gpio, GPIOD_IS_OUT);
346 if (!dm_gpio_is_valid(reset_gpio)) {
347 printf("Cannot find reset GPIO for Moxtet bus!\n");
354 int misc_init_r(void)
359 ret = mbox_sp_get_board_info(NULL, mac1, mac2, NULL, NULL);
361 printf("Cannot read data from OTP!\n");
365 if (is_valid_ethaddr(mac1) && !env_get("ethaddr"))
366 eth_env_set_enetaddr("ethaddr", mac1);
368 if (is_valid_ethaddr(mac2) && !env_get("eth1addr"))
369 eth_env_set_enetaddr("eth1addr", mac2);
374 static void mox_print_info(void)
376 int ret, board_version, ram_size;
380 ret = mbox_sp_get_board_info(&serial_number, NULL, NULL, &board_version,
385 printf("Turris Mox:\n");
386 printf(" Board version: %i\n", board_version);
387 printf(" RAM size: %i MiB\n", ram_size);
388 printf(" Serial Number: %016llX\n", serial_number);
390 pub_key = mox_sp_get_ecdsa_public_key();
392 printf(" ECDSA Public Key: %s\n", pub_key);
394 printf("Cannot read ECDSA Public Key\n");
397 int last_stage_init(void)
403 struct gpio_desc reset_gpio = {};
407 ret = mox_get_topology(&topology, &module_count, &is_sd);
409 printf("Cannot read module topology!\n");
413 printf(" SD/eMMC version: %s\n", is_sd ? "SD" : "eMMC");
416 printf("Module Topology:\n");
418 for (i = 0; i < module_count; ++i) {
419 switch (topology[i]) {
421 printf("% 4i: SFP Module\n", i + 1);
424 printf("% 4i: Mini-PCIe Module\n", i + 1);
426 case MOX_MODULE_TOPAZ:
427 printf("% 4i: Topaz Switch Module (4-port)\n", i + 1);
429 case MOX_MODULE_PERIDOT:
430 printf("% 4i: Peridot Switch Module (8-port)\n", i + 1);
432 case MOX_MODULE_USB3:
433 printf("% 4i: USB 3.0 Module (4 ports)\n", i + 1);
435 case MOX_MODULE_PASSPCI:
436 printf("% 4i: Passthrough Mini-PCIe Module\n", i + 1);
439 printf("% 4i: unknown (ID %i)\n", i + 1, topology[i]);
443 /* now check if modules are connected in supported mode */
445 for (i = 0; i < module_count; ++i) {
446 switch (topology[i]) {
449 printf("Error: Only one SFP module is supported!\n");
451 printf("Error: SFP module cannot be connected after Topaz Switch module!\n");
459 printf("Error: Only one Mini-PCIe module is supported!\n");
461 printf("Error: Mini-PCIe module cannot come after USB 3.0 module!\n");
462 } else if (i && (i != 1 || !passpci)) {
463 printf("Error: Mini-PCIe module should be the first connected module or come right after Passthrough Mini-PCIe module!\n");
468 case MOX_MODULE_TOPAZ:
470 printf("Error: Only one Topaz module is supported!\n");
471 } else if (peridot >= 3) {
472 printf("Error: At most two Peridot modules can come before Topaz module!\n");
477 case MOX_MODULE_PERIDOT:
479 printf("Error: Peridot module must come before SFP or Topaz module!\n");
480 } else if (peridot >= 3) {
481 printf("Error: At most three Peridot modules are supported!\n");
483 peridot_pos[peridot] = i;
487 case MOX_MODULE_USB3:
489 printf("Error: USB 3.0 module cannot come after Mini-PCIe module!\n");
491 printf("Error: Only one USB 3.0 module is supported!\n");
492 } else if (i && (i != 1 || !passpci)) {
493 printf("Error: USB 3.0 module should be the first connected module or come right after Passthrough Mini-PCIe module!\n");
498 case MOX_MODULE_PASSPCI:
500 printf("Error: Only one Passthrough Mini-PCIe module is supported!\n");
502 printf("Error: Passthrough Mini-PCIe module should be the first connected module!\n");
509 /* now configure modules */
511 if (get_reset_gpio(&reset_gpio) < 0)
515 if (configure_peridots(&reset_gpio) < 0) {
516 printf("Cannot configure Peridot modules!\n");
520 dm_gpio_set_value(&reset_gpio, 1);
522 dm_gpio_set_value(&reset_gpio, 0);
526 if (peridot || topaz) {
528 * now check if the addresses are set by reading Scratch & Misc
529 * register 0x70 of Peridot (and potentially Topaz) modules
532 bus = miiphy_get_dev_by_name("neta@30000");
534 printf("Cannot get MDIO bus device!\n");
536 for (i = 0; i < peridot; ++i)
537 check_switch_address(bus, 0x10 + i);
540 check_switch_address(bus, 0x2);
542 sw_blink_leds(bus, peridot, topaz);