1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2018 Marek Behun <marek.behun@nic.cz>
13 #include <mvebu/comphy.h>
15 #include <linux/string.h>
16 #include <linux/libfdt.h>
17 #include <fdt_support.h>
18 #include <environment.h>
22 #define MAX_MOX_MODULES 10
24 #define MOX_MODULE_SFP 0x1
25 #define MOX_MODULE_PCI 0x2
26 #define MOX_MODULE_TOPAZ 0x3
27 #define MOX_MODULE_PERIDOT 0x4
28 #define MOX_MODULE_USB3 0x5
29 #define MOX_MODULE_PASSPCI 0x6
31 #define ARMADA_37XX_NB_GPIO_SEL 0xd0013830
32 #define ARMADA_37XX_SPI_CTRL 0xd0010600
33 #define ARMADA_37XX_SPI_CFG 0xd0010604
34 #define ARMADA_37XX_SPI_DOUT 0xd0010608
35 #define ARMADA_37XX_SPI_DIN 0xd001060c
37 #define PCIE_PATH "/soc/pcie@d0070000"
39 DECLARE_GLOBAL_DATA_PTR;
44 gd->ram_size = (phys_size_t)get_ram_size(0, 0x40000000);
49 int dram_init_banksize(void)
51 gd->bd->bi_dram[0].start = (phys_addr_t)0;
52 gd->bd->bi_dram[0].size = gd->ram_size;
57 #if defined(CONFIG_OF_BOARD_FIXUP)
58 int board_fix_fdt(void *blob)
60 u8 topology[MAX_MOX_MODULES];
65 * SPI driver is not loaded in driver model yet, but we have to find out
66 * if pcie should be enabled in U-Boot's device tree. Therefore we have
67 * to read SPI by reading/writing SPI registers directly
70 writel(0x563fa, ARMADA_37XX_NB_GPIO_SEL);
71 writel(0x10df, ARMADA_37XX_SPI_CFG);
72 writel(0x2005b, ARMADA_37XX_SPI_CTRL);
74 while (!(readl(ARMADA_37XX_SPI_CTRL) & 0x2))
77 for (i = 0; i < MAX_MOX_MODULES; ++i) {
78 writel(0x0, ARMADA_37XX_SPI_DOUT);
80 while (!(readl(ARMADA_37XX_SPI_CTRL) & 0x2))
83 topology[i] = readl(ARMADA_37XX_SPI_DIN) & 0xff;
84 if (topology[i] == 0xff)
92 writel(0x5b, ARMADA_37XX_SPI_CTRL);
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");
121 /* address of boot parameters */
122 gd->bd->bi_boot_params = CONFIG_SYS_SDRAM_BASE + 0x100;
127 static int mox_do_spi(u8 *in, u8 *out, size_t size)
129 struct spi_slave *slave;
133 ret = spi_get_bus_and_cs(0, 1, 1000000, SPI_CPHA | SPI_CPOL,
134 "spi_generic_drv", "moxtet@1", &dev,
139 ret = spi_claim_bus(slave);
143 ret = spi_xfer(slave, size * 8, out, in, SPI_XFER_ONCE);
145 spi_release_bus(slave);
147 spi_free_slave(slave);
152 static int mox_get_topology(const u8 **ptopology, int *psize, int *pis_sd)
155 static u8 topology[MAX_MOX_MODULES - 1];
157 u8 din[MAX_MOX_MODULES], dout[MAX_MOX_MODULES];
162 *ptopology = topology;
170 memset(din, 0, MAX_MOX_MODULES);
171 memset(dout, 0, MAX_MOX_MODULES);
173 ret = mox_do_spi(din, dout, MAX_MOX_MODULES);
179 else if (din[0] == 0x00)
184 for (i = 1; i < MAX_MOX_MODULES && din[i] != 0xff; ++i)
185 topology[i - 1] = din[i] & 0xf;
189 *ptopology = topology;
198 int comphy_update_map(struct comphy_map *serdes_map, int count)
200 int ret, i, size, sfpindex = -1, swindex = -1;
203 ret = mox_get_topology(&topology, &size, NULL);
207 for (i = 0; i < size; ++i) {
208 if (topology[i] == MOX_MODULE_SFP && sfpindex == -1)
210 else if ((topology[i] == MOX_MODULE_TOPAZ ||
211 topology[i] == MOX_MODULE_PERIDOT) &&
216 if (sfpindex >= 0 && swindex >= 0) {
217 if (sfpindex < swindex)
218 serdes_map[0].speed = PHY_SPEED_1_25G;
220 serdes_map[0].speed = PHY_SPEED_3_125G;
221 } else if (sfpindex >= 0) {
222 serdes_map[0].speed = PHY_SPEED_1_25G;
223 } else if (swindex >= 0) {
224 serdes_map[0].speed = PHY_SPEED_3_125G;
230 #define SW_SMI_CMD_R(d, r) (0x9800 | (((d) & 0x1f) << 5) | ((r) & 0x1f))
231 #define SW_SMI_CMD_W(d, r) (0x9400 | (((d) & 0x1f) << 5) | ((r) & 0x1f))
233 static int sw_multi_read(struct mii_dev *bus, int sw, int dev, int reg)
235 bus->write(bus, sw, 0, 0, SW_SMI_CMD_R(dev, reg));
237 return bus->read(bus, sw, 0, 1);
240 static void sw_multi_write(struct mii_dev *bus, int sw, int dev, int reg,
243 bus->write(bus, sw, 0, 1, val);
244 bus->write(bus, sw, 0, 0, SW_SMI_CMD_W(dev, reg));
248 static int sw_scratch_read(struct mii_dev *bus, int sw, int reg)
250 sw_multi_write(bus, sw, 0x1c, 0x1a, (reg & 0x7f) << 8);
251 return sw_multi_read(bus, sw, 0x1c, 0x1a) & 0xff;
254 static void sw_led_write(struct mii_dev *bus, int sw, int port, int reg,
257 sw_multi_write(bus, sw, port, 0x16, 0x8000 | ((reg & 7) << 12)
261 static void sw_blink_leds(struct mii_dev *bus, int peridot, int topaz)
269 { 2, 0xef, 1 }, { 2, 0xfe, 1 }, { 2, 0x33, 0 },
270 { 4, 0xef, 1 }, { 4, 0xfe, 1 }, { 4, 0x33, 0 },
271 { 3, 0xfe, 1 }, { 3, 0xef, 1 }, { 3, 0x33, 0 },
272 { 1, 0xfe, 1 }, { 1, 0xef, 1 }, { 1, 0x33, 0 }
275 for (i = 0; i < 12; ++i) {
276 for (p = 0; p < peridot; ++p) {
277 sw_led_write(bus, 0x10 + p, regs[i].port, 0,
279 sw_led_write(bus, 0x10 + p, regs[i].port + 4, 0,
283 sw_led_write(bus, 0x2, 0x10 + regs[i].port, 0,
292 static void check_switch_address(struct mii_dev *bus, int addr)
294 if (sw_scratch_read(bus, addr, 0x70) >> 3 != addr)
295 printf("Check of switch MDIO address failed for 0x%02x\n",
299 static int sfp, pci, topaz, peridot, usb, passpci;
300 static int sfp_pos, peridot_pos[3];
301 static int module_count;
303 static int configure_peridots(struct gpio_desc *reset_gpio)
306 u8 dout[MAX_MOX_MODULES];
308 memset(dout, 0, MAX_MOX_MODULES);
310 /* set addresses of Peridot modules */
311 for (i = 0; i < peridot; ++i)
312 dout[module_count - peridot_pos[i]] = (~i) & 3;
315 * if there is a SFP module connected to the last Peridot module, set
316 * the P10_SMODE to 1 for the Peridot module
319 dout[module_count - peridot_pos[i - 1]] |= 1 << 3;
321 dm_gpio_set_value(reset_gpio, 1);
324 ret = mox_do_spi(NULL, dout, module_count + 1);
327 dm_gpio_set_value(reset_gpio, 0);
334 static int get_reset_gpio(struct gpio_desc *reset_gpio)
338 node = fdt_node_offset_by_compatible(gd->fdt_blob, 0, "cznic,moxtet");
340 printf("Cannot find Moxtet bus device node!\n");
344 gpio_request_by_name_nodev(offset_to_ofnode(node), "reset-gpios", 0,
345 reset_gpio, GPIOD_IS_OUT);
347 if (!dm_gpio_is_valid(reset_gpio)) {
348 printf("Cannot find reset GPIO for Moxtet bus!\n");
355 int misc_init_r(void)
360 ret = mbox_sp_get_board_info(NULL, mac1, mac2, NULL, NULL);
362 printf("Cannot read data from OTP!\n");
366 if (is_valid_ethaddr(mac1) && !env_get("ethaddr"))
367 eth_env_set_enetaddr("ethaddr", mac1);
369 if (is_valid_ethaddr(mac2) && !env_get("eth1addr"))
370 eth_env_set_enetaddr("eth1addr", mac2);
375 static void mox_print_info(void)
377 int ret, board_version, ram_size;
381 ret = mbox_sp_get_board_info(&serial_number, NULL, NULL, &board_version,
386 printf("Turris Mox:\n");
387 printf(" Board version: %i\n", board_version);
388 printf(" RAM size: %i MiB\n", ram_size);
389 printf(" Serial Number: %016llX\n", serial_number);
391 pub_key = mox_sp_get_ecdsa_public_key();
393 printf(" ECDSA Public Key: %s\n", pub_key);
395 printf("Cannot read ECDSA Public Key\n");
398 int last_stage_init(void)
404 struct gpio_desc reset_gpio = {};
408 ret = mox_get_topology(&topology, &module_count, &is_sd);
410 printf("Cannot read module topology!\n");
414 printf(" SD/eMMC version: %s\n", is_sd ? "SD" : "eMMC");
417 printf("Module Topology:\n");
419 for (i = 0; i < module_count; ++i) {
420 switch (topology[i]) {
422 printf("% 4i: SFP Module\n", i + 1);
425 printf("% 4i: Mini-PCIe Module\n", i + 1);
427 case MOX_MODULE_TOPAZ:
428 printf("% 4i: Topaz Switch Module (4-port)\n", i + 1);
430 case MOX_MODULE_PERIDOT:
431 printf("% 4i: Peridot Switch Module (8-port)\n", i + 1);
433 case MOX_MODULE_USB3:
434 printf("% 4i: USB 3.0 Module (4 ports)\n", i + 1);
436 case MOX_MODULE_PASSPCI:
437 printf("% 4i: Passthrough Mini-PCIe Module\n", i + 1);
440 printf("% 4i: unknown (ID %i)\n", i + 1, topology[i]);
444 /* now check if modules are connected in supported mode */
446 for (i = 0; i < module_count; ++i) {
447 switch (topology[i]) {
450 printf("Error: Only one SFP module is supported!\n");
452 printf("Error: SFP module cannot be connected after Topaz Switch module!\n");
460 printf("Error: Only one Mini-PCIe module is supported!\n");
462 printf("Error: Mini-PCIe module cannot come after USB 3.0 module!\n");
463 } else if (i && (i != 1 || !passpci)) {
464 printf("Error: Mini-PCIe module should be the first connected module or come right after Passthrough Mini-PCIe module!\n");
469 case MOX_MODULE_TOPAZ:
471 printf("Error: Only one Topaz module is supported!\n");
472 } else if (peridot >= 3) {
473 printf("Error: At most two Peridot modules can come before Topaz module!\n");
478 case MOX_MODULE_PERIDOT:
480 printf("Error: Peridot module must come before SFP or Topaz module!\n");
481 } else if (peridot >= 3) {
482 printf("Error: At most three Peridot modules are supported!\n");
484 peridot_pos[peridot] = i;
488 case MOX_MODULE_USB3:
490 printf("Error: USB 3.0 module cannot come after Mini-PCIe module!\n");
492 printf("Error: Only one USB 3.0 module is supported!\n");
493 } else if (i && (i != 1 || !passpci)) {
494 printf("Error: USB 3.0 module should be the first connected module or come right after Passthrough Mini-PCIe module!\n");
499 case MOX_MODULE_PASSPCI:
501 printf("Error: Only one Passthrough Mini-PCIe module is supported!\n");
503 printf("Error: Passthrough Mini-PCIe module should be the first connected module!\n");
510 /* now configure modules */
512 if (get_reset_gpio(&reset_gpio) < 0)
516 if (configure_peridots(&reset_gpio) < 0) {
517 printf("Cannot configure Peridot modules!\n");
521 dm_gpio_set_value(&reset_gpio, 1);
523 dm_gpio_set_value(&reset_gpio, 0);
527 if (peridot || topaz) {
529 * now check if the addresses are set by reading Scratch & Misc
530 * register 0x70 of Peridot (and potentially Topaz) modules
533 bus = miiphy_get_dev_by_name("neta@30000");
535 printf("Cannot get MDIO bus device!\n");
537 for (i = 0; i < peridot; ++i)
538 check_switch_address(bus, 0x10 + i);
541 check_switch_address(bus, 0x2);
543 sw_blink_leds(bus, peridot, topaz);