Marek Behún | f835bed | 2018-04-24 17:21:31 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
Marek Behún | d63726e | 2022-06-01 17:17:06 +0200 | [diff] [blame] | 3 | * Copyright (C) 2018 Marek Behún <kabel@kernel.org> |
Marek Behún | f835bed | 2018-04-24 17:21:31 +0200 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <common.h> |
Marek Behún | 455ef5b | 2020-04-08 19:25:22 +0200 | [diff] [blame] | 7 | #include <asm/arch/cpu.h> |
Marek Behún | 1055ce1 | 2020-04-08 12:02:07 +0200 | [diff] [blame] | 8 | #include <asm/arch/soc.h> |
Simon Glass | 274e0b0 | 2020-05-10 11:39:56 -0600 | [diff] [blame] | 9 | #include <net.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 10 | #include <asm/global_data.h> |
Marek Behún | 9602a8c | 2018-08-21 12:22:09 +0200 | [diff] [blame] | 11 | #include <asm/io.h> |
Marek Behún | 28e935f | 2020-04-08 12:02:08 +0200 | [diff] [blame] | 12 | #include <asm/gpio.h> |
Marek Behún | 859ada5 | 2021-06-07 16:34:47 +0200 | [diff] [blame] | 13 | #include <button.h> |
Marek Behún | f835bed | 2018-04-24 17:21:31 +0200 | [diff] [blame] | 14 | #include <clk.h> |
Marek Behún | 28e935f | 2020-04-08 12:02:08 +0200 | [diff] [blame] | 15 | #include <dm.h> |
Marek Behún | 1c8eb90 | 2022-04-27 12:41:50 +0200 | [diff] [blame] | 16 | #include <dm/of_extra.h> |
Simon Glass | 5e6201b | 2019-08-01 09:46:51 -0600 | [diff] [blame] | 17 | #include <env.h> |
Simon Glass | 1cedca1 | 2023-08-21 21:17:01 -0600 | [diff] [blame] | 18 | #include <event.h> |
Marek Behún | 9602a8c | 2018-08-21 12:22:09 +0200 | [diff] [blame] | 19 | #include <fdt_support.h> |
Marek Behún | 28e935f | 2020-04-08 12:02:08 +0200 | [diff] [blame] | 20 | #include <init.h> |
Marek Behún | 859ada5 | 2021-06-07 16:34:47 +0200 | [diff] [blame] | 21 | #include <led.h> |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 22 | #include <linux/delay.h> |
Marek Behún | 28e935f | 2020-04-08 12:02:08 +0200 | [diff] [blame] | 23 | #include <linux/libfdt.h> |
| 24 | #include <linux/string.h> |
| 25 | #include <miiphy.h> |
Marek Behún | 28e935f | 2020-04-08 12:02:08 +0200 | [diff] [blame] | 26 | #include <spi.h> |
Pali Rohár | 584256a | 2022-08-29 15:44:48 +0200 | [diff] [blame] | 27 | #include <spi_flash.h> |
Marek Behún | f835bed | 2018-04-24 17:21:31 +0200 | [diff] [blame] | 28 | |
Marek Behún | 35572c9 | 2018-12-17 16:10:08 +0100 | [diff] [blame] | 29 | #include "mox_sp.h" |
| 30 | |
Marek Behún | 9602a8c | 2018-08-21 12:22:09 +0200 | [diff] [blame] | 31 | #define MAX_MOX_MODULES 10 |
| 32 | |
| 33 | #define MOX_MODULE_SFP 0x1 |
| 34 | #define MOX_MODULE_PCI 0x2 |
| 35 | #define MOX_MODULE_TOPAZ 0x3 |
| 36 | #define MOX_MODULE_PERIDOT 0x4 |
| 37 | #define MOX_MODULE_USB3 0x5 |
| 38 | #define MOX_MODULE_PASSPCI 0x6 |
| 39 | |
Marek Behún | 1055ce1 | 2020-04-08 12:02:07 +0200 | [diff] [blame] | 40 | #define ARMADA_37XX_NB_GPIO_SEL (MVEBU_REGISTER(0x13830)) |
| 41 | #define ARMADA_37XX_SPI_CTRL (MVEBU_REGISTER(0x10600)) |
| 42 | #define ARMADA_37XX_SPI_CFG (MVEBU_REGISTER(0x10604)) |
| 43 | #define ARMADA_37XX_SPI_DOUT (MVEBU_REGISTER(0x10608)) |
| 44 | #define ARMADA_37XX_SPI_DIN (MVEBU_REGISTER(0x1060c)) |
Marek Behún | 9602a8c | 2018-08-21 12:22:09 +0200 | [diff] [blame] | 45 | |
Marek Behún | f835bed | 2018-04-24 17:21:31 +0200 | [diff] [blame] | 46 | DECLARE_GLOBAL_DATA_PTR; |
| 47 | |
Marek Behún | 9602a8c | 2018-08-21 12:22:09 +0200 | [diff] [blame] | 48 | #if defined(CONFIG_OF_BOARD_FIXUP) |
| 49 | int board_fix_fdt(void *blob) |
| 50 | { |
Pali Rohár | cbaaf5e | 2021-11-26 14:57:12 +0100 | [diff] [blame] | 51 | enum fdt_status status_pcie, status_eth1; |
Marek Behún | 9602a8c | 2018-08-21 12:22:09 +0200 | [diff] [blame] | 52 | u8 topology[MAX_MOX_MODULES]; |
Marek Behún | d050eab | 2021-11-26 14:57:11 +0100 | [diff] [blame] | 53 | int i, size, ret; |
Pali Rohár | 951b982 | 2022-01-18 10:33:19 +0100 | [diff] [blame] | 54 | bool eth1_sgmii; |
Marek Behún | 9602a8c | 2018-08-21 12:22:09 +0200 | [diff] [blame] | 55 | |
| 56 | /* |
| 57 | * SPI driver is not loaded in driver model yet, but we have to find out |
| 58 | * if pcie should be enabled in U-Boot's device tree. Therefore we have |
| 59 | * to read SPI by reading/writing SPI registers directly |
| 60 | */ |
| 61 | |
Marek Behún | a4e697f | 2020-04-08 12:02:03 +0200 | [diff] [blame] | 62 | /* put pin from GPIO to SPI mode */ |
| 63 | clrbits_le32(ARMADA_37XX_NB_GPIO_SEL, BIT(12)); |
Marek Behún | 77d9312 | 2021-11-26 14:57:14 +0100 | [diff] [blame] | 64 | /* configure cpol, cpha, prescale */ |
| 65 | writel(0x10df, ARMADA_37XX_SPI_CFG); |
| 66 | mdelay(1); |
Marek Behún | a4e697f | 2020-04-08 12:02:03 +0200 | [diff] [blame] | 67 | /* enable SPI CS1 */ |
| 68 | setbits_le32(ARMADA_37XX_SPI_CTRL, BIT(17)); |
Marek Behún | 9602a8c | 2018-08-21 12:22:09 +0200 | [diff] [blame] | 69 | |
| 70 | while (!(readl(ARMADA_37XX_SPI_CTRL) & 0x2)) |
| 71 | udelay(1); |
| 72 | |
Pali Rohár | cbaaf5e | 2021-11-26 14:57:12 +0100 | [diff] [blame] | 73 | status_pcie = FDT_STATUS_DISABLED; |
| 74 | status_eth1 = FDT_STATUS_DISABLED; |
Pali Rohár | 951b982 | 2022-01-18 10:33:19 +0100 | [diff] [blame] | 75 | eth1_sgmii = false; |
Pali Rohár | cbaaf5e | 2021-11-26 14:57:12 +0100 | [diff] [blame] | 76 | |
Marek Behún | 9602a8c | 2018-08-21 12:22:09 +0200 | [diff] [blame] | 77 | for (i = 0; i < MAX_MOX_MODULES; ++i) { |
| 78 | writel(0x0, ARMADA_37XX_SPI_DOUT); |
| 79 | |
| 80 | while (!(readl(ARMADA_37XX_SPI_CTRL) & 0x2)) |
| 81 | udelay(1); |
| 82 | |
| 83 | topology[i] = readl(ARMADA_37XX_SPI_DIN) & 0xff; |
| 84 | if (topology[i] == 0xff) |
| 85 | break; |
| 86 | |
| 87 | topology[i] &= 0xf; |
Pali Rohár | cbaaf5e | 2021-11-26 14:57:12 +0100 | [diff] [blame] | 88 | |
Pali Rohár | 951b982 | 2022-01-18 10:33:19 +0100 | [diff] [blame] | 89 | if (topology[i] == MOX_MODULE_SFP && |
| 90 | status_pcie == FDT_STATUS_DISABLED) |
| 91 | eth1_sgmii = true; |
| 92 | |
Pali Rohár | cbaaf5e | 2021-11-26 14:57:12 +0100 | [diff] [blame] | 93 | if (topology[i] == MOX_MODULE_SFP || |
| 94 | topology[i] == MOX_MODULE_TOPAZ || |
| 95 | topology[i] == MOX_MODULE_PERIDOT) |
| 96 | status_eth1 = FDT_STATUS_OKAY; |
Marek Behún | 9602a8c | 2018-08-21 12:22:09 +0200 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | size = i; |
| 100 | |
Marek Behún | a4e697f | 2020-04-08 12:02:03 +0200 | [diff] [blame] | 101 | /* disable SPI CS1 */ |
| 102 | clrbits_le32(ARMADA_37XX_SPI_CTRL, BIT(17)); |
Marek Behún | 9602a8c | 2018-08-21 12:22:09 +0200 | [diff] [blame] | 103 | |
Pali Rohár | cbaaf5e | 2021-11-26 14:57:12 +0100 | [diff] [blame] | 104 | ret = fdt_set_status_by_alias(blob, "ethernet1", status_eth1); |
| 105 | if (ret < 0) |
| 106 | printf("Cannot set status for eth1 in U-Boot's device tree: %s!\n", |
| 107 | fdt_strerror(ret)); |
| 108 | |
Pali Rohár | 951b982 | 2022-01-18 10:33:19 +0100 | [diff] [blame] | 109 | if (eth1_sgmii) { |
| 110 | ret = fdt_path_offset(blob, "ethernet1"); |
| 111 | if (ret >= 0) |
| 112 | ret = fdt_setprop_string(blob, ret, "phy-mode", "sgmii"); |
| 113 | if (ret < 0) |
| 114 | printf("Cannot set phy-mode for eth1 to sgmii in U-Boot device tree: %s!\n", |
| 115 | fdt_strerror(ret)); |
| 116 | } |
| 117 | |
Marek Behún | 9602a8c | 2018-08-21 12:22:09 +0200 | [diff] [blame] | 118 | if (size > 1 && (topology[1] == MOX_MODULE_PCI || |
| 119 | topology[1] == MOX_MODULE_USB3 || |
| 120 | topology[1] == MOX_MODULE_PASSPCI)) |
Pali Rohár | cbaaf5e | 2021-11-26 14:57:12 +0100 | [diff] [blame] | 121 | status_pcie = FDT_STATUS_OKAY; |
Marek Behún | 9602a8c | 2018-08-21 12:22:09 +0200 | [diff] [blame] | 122 | |
Marek Behún | d050eab | 2021-11-26 14:57:11 +0100 | [diff] [blame] | 123 | ret = fdt_set_status_by_compatible(blob, "marvell,armada-3700-pcie", |
Pali Rohár | cbaaf5e | 2021-11-26 14:57:12 +0100 | [diff] [blame] | 124 | status_pcie); |
Marek Behún | d050eab | 2021-11-26 14:57:11 +0100 | [diff] [blame] | 125 | if (ret < 0) { |
| 126 | printf("Cannot set status for PCIe in U-Boot's device tree: %s!\n", |
| 127 | fdt_strerror(ret)); |
Marek Behún | 9602a8c | 2018-08-21 12:22:09 +0200 | [diff] [blame] | 128 | return 0; |
| 129 | } |
| 130 | |
Marek Behún | 455ef5b | 2020-04-08 19:25:22 +0200 | [diff] [blame] | 131 | if (a3700_fdt_fix_pcie_regions(blob) < 0) { |
| 132 | printf("Cannot fix PCIe regions in U-Boot's device tree!\n"); |
| 133 | return 0; |
| 134 | } |
| 135 | |
Marek Behún | 9602a8c | 2018-08-21 12:22:09 +0200 | [diff] [blame] | 136 | return 0; |
| 137 | } |
| 138 | #endif |
| 139 | |
Marek Behún | f835bed | 2018-04-24 17:21:31 +0200 | [diff] [blame] | 140 | int board_init(void) |
| 141 | { |
| 142 | /* address of boot parameters */ |
Tom Rini | bb4dd96 | 2022-11-16 13:10:37 -0500 | [diff] [blame] | 143 | gd->bd->bi_boot_params = CFG_SYS_SDRAM_BASE + 0x100; |
Marek Behún | f835bed | 2018-04-24 17:21:31 +0200 | [diff] [blame] | 144 | |
Marek Behún | f835bed | 2018-04-24 17:21:31 +0200 | [diff] [blame] | 145 | return 0; |
| 146 | } |
| 147 | |
Marek Behún | f68ed8b | 2018-12-17 16:10:01 +0100 | [diff] [blame] | 148 | static int mox_do_spi(u8 *in, u8 *out, size_t size) |
Marek Behún | f835bed | 2018-04-24 17:21:31 +0200 | [diff] [blame] | 149 | { |
| 150 | struct spi_slave *slave; |
| 151 | struct udevice *dev; |
Marek Behún | f68ed8b | 2018-12-17 16:10:01 +0100 | [diff] [blame] | 152 | int ret; |
Marek Behún | f835bed | 2018-04-24 17:21:31 +0200 | [diff] [blame] | 153 | |
Patrice Chotard | 86e06ae | 2022-03-30 09:33:13 +0200 | [diff] [blame] | 154 | ret = _spi_get_bus_and_cs(0, 1, 1000000, SPI_CPHA | SPI_CPOL, |
| 155 | "spi_generic_drv", "moxtet@1", &dev, |
| 156 | &slave); |
Marek Behún | f835bed | 2018-04-24 17:21:31 +0200 | [diff] [blame] | 157 | if (ret) |
| 158 | goto fail; |
| 159 | |
| 160 | ret = spi_claim_bus(slave); |
| 161 | if (ret) |
| 162 | goto fail_free; |
| 163 | |
Marek Behún | f68ed8b | 2018-12-17 16:10:01 +0100 | [diff] [blame] | 164 | ret = spi_xfer(slave, size * 8, out, in, SPI_XFER_ONCE); |
| 165 | |
| 166 | spi_release_bus(slave); |
| 167 | fail_free: |
| 168 | spi_free_slave(slave); |
| 169 | fail: |
| 170 | return ret; |
| 171 | } |
| 172 | |
| 173 | static int mox_get_topology(const u8 **ptopology, int *psize, int *pis_sd) |
| 174 | { |
| 175 | static int is_sd; |
| 176 | static u8 topology[MAX_MOX_MODULES - 1]; |
| 177 | static int size; |
| 178 | u8 din[MAX_MOX_MODULES], dout[MAX_MOX_MODULES]; |
| 179 | int ret, i; |
| 180 | |
| 181 | if (size) { |
| 182 | if (ptopology) |
| 183 | *ptopology = topology; |
| 184 | if (psize) |
| 185 | *psize = size; |
| 186 | if (pis_sd) |
| 187 | *pis_sd = is_sd; |
| 188 | return 0; |
| 189 | } |
Marek Behún | f835bed | 2018-04-24 17:21:31 +0200 | [diff] [blame] | 190 | |
Marek Behún | f68ed8b | 2018-12-17 16:10:01 +0100 | [diff] [blame] | 191 | memset(din, 0, MAX_MOX_MODULES); |
| 192 | memset(dout, 0, MAX_MOX_MODULES); |
| 193 | |
| 194 | ret = mox_do_spi(din, dout, MAX_MOX_MODULES); |
Marek Behún | f835bed | 2018-04-24 17:21:31 +0200 | [diff] [blame] | 195 | if (ret) |
Marek Behún | f68ed8b | 2018-12-17 16:10:01 +0100 | [diff] [blame] | 196 | return ret; |
Marek Behún | f835bed | 2018-04-24 17:21:31 +0200 | [diff] [blame] | 197 | |
Marek Behún | f68ed8b | 2018-12-17 16:10:01 +0100 | [diff] [blame] | 198 | if (din[0] == 0x10) |
| 199 | is_sd = 1; |
| 200 | else if (din[0] == 0x00) |
| 201 | is_sd = 0; |
| 202 | else |
| 203 | return -ENODEV; |
Marek Behún | f835bed | 2018-04-24 17:21:31 +0200 | [diff] [blame] | 204 | |
Marek Behún | f68ed8b | 2018-12-17 16:10:01 +0100 | [diff] [blame] | 205 | for (i = 1; i < MAX_MOX_MODULES && din[i] != 0xff; ++i) |
| 206 | topology[i - 1] = din[i] & 0xf; |
| 207 | size = i - 1; |
| 208 | |
| 209 | if (ptopology) |
| 210 | *ptopology = topology; |
| 211 | if (psize) |
| 212 | *psize = size; |
| 213 | if (pis_sd) |
| 214 | *pis_sd = is_sd; |
| 215 | |
| 216 | return 0; |
| 217 | } |
| 218 | |
Marek Behún | 35b3513 | 2018-12-17 16:10:03 +0100 | [diff] [blame] | 219 | #define SW_SMI_CMD_R(d, r) (0x9800 | (((d) & 0x1f) << 5) | ((r) & 0x1f)) |
| 220 | #define SW_SMI_CMD_W(d, r) (0x9400 | (((d) & 0x1f) << 5) | ((r) & 0x1f)) |
| 221 | |
Marek Behún | 1c8eb90 | 2022-04-27 12:41:50 +0200 | [diff] [blame] | 222 | static int sw_multi_read(struct udevice *bus, int sw, int dev, int reg) |
Marek Behún | 35b3513 | 2018-12-17 16:10:03 +0100 | [diff] [blame] | 223 | { |
Marek Behún | 1c8eb90 | 2022-04-27 12:41:50 +0200 | [diff] [blame] | 224 | dm_mdio_write(bus, sw, MDIO_DEVAD_NONE, 0, SW_SMI_CMD_R(dev, reg)); |
Marek Behún | 35b3513 | 2018-12-17 16:10:03 +0100 | [diff] [blame] | 225 | mdelay(5); |
Marek Behún | 1c8eb90 | 2022-04-27 12:41:50 +0200 | [diff] [blame] | 226 | return dm_mdio_read(bus, sw, MDIO_DEVAD_NONE, 1); |
Marek Behún | 35b3513 | 2018-12-17 16:10:03 +0100 | [diff] [blame] | 227 | } |
| 228 | |
Marek Behún | 1c8eb90 | 2022-04-27 12:41:50 +0200 | [diff] [blame] | 229 | static void sw_multi_write(struct udevice *bus, int sw, int dev, int reg, |
Marek Behún | 35b3513 | 2018-12-17 16:10:03 +0100 | [diff] [blame] | 230 | u16 val) |
| 231 | { |
Marek Behún | 1c8eb90 | 2022-04-27 12:41:50 +0200 | [diff] [blame] | 232 | dm_mdio_write(bus, sw, MDIO_DEVAD_NONE, 1, val); |
| 233 | dm_mdio_write(bus, sw, MDIO_DEVAD_NONE, 0, SW_SMI_CMD_W(dev, reg)); |
Marek Behún | 35b3513 | 2018-12-17 16:10:03 +0100 | [diff] [blame] | 234 | mdelay(5); |
| 235 | } |
| 236 | |
Marek Behún | 1c8eb90 | 2022-04-27 12:41:50 +0200 | [diff] [blame] | 237 | static int sw_scratch_read(struct udevice *bus, int sw, int reg) |
Marek Behún | 35b3513 | 2018-12-17 16:10:03 +0100 | [diff] [blame] | 238 | { |
| 239 | sw_multi_write(bus, sw, 0x1c, 0x1a, (reg & 0x7f) << 8); |
| 240 | return sw_multi_read(bus, sw, 0x1c, 0x1a) & 0xff; |
| 241 | } |
| 242 | |
Marek Behún | 1c8eb90 | 2022-04-27 12:41:50 +0200 | [diff] [blame] | 243 | static void sw_led_write(struct udevice *bus, int sw, int port, int reg, |
Marek Behún | 35b3513 | 2018-12-17 16:10:03 +0100 | [diff] [blame] | 244 | u16 val) |
| 245 | { |
| 246 | sw_multi_write(bus, sw, port, 0x16, 0x8000 | ((reg & 7) << 12) |
| 247 | | (val & 0x7ff)); |
| 248 | } |
| 249 | |
Marek Behún | 1c8eb90 | 2022-04-27 12:41:50 +0200 | [diff] [blame] | 250 | static void sw_blink_leds(struct udevice *bus, int peridot, int topaz) |
Marek Behún | 35b3513 | 2018-12-17 16:10:03 +0100 | [diff] [blame] | 251 | { |
| 252 | int i, p; |
| 253 | struct { |
| 254 | int port; |
| 255 | u16 val; |
| 256 | int wait; |
| 257 | } regs[] = { |
| 258 | { 2, 0xef, 1 }, { 2, 0xfe, 1 }, { 2, 0x33, 0 }, |
| 259 | { 4, 0xef, 1 }, { 4, 0xfe, 1 }, { 4, 0x33, 0 }, |
| 260 | { 3, 0xfe, 1 }, { 3, 0xef, 1 }, { 3, 0x33, 0 }, |
| 261 | { 1, 0xfe, 1 }, { 1, 0xef, 1 }, { 1, 0x33, 0 } |
| 262 | }; |
| 263 | |
| 264 | for (i = 0; i < 12; ++i) { |
| 265 | for (p = 0; p < peridot; ++p) { |
| 266 | sw_led_write(bus, 0x10 + p, regs[i].port, 0, |
| 267 | regs[i].val); |
| 268 | sw_led_write(bus, 0x10 + p, regs[i].port + 4, 0, |
| 269 | regs[i].val); |
| 270 | } |
| 271 | if (topaz) { |
| 272 | sw_led_write(bus, 0x2, 0x10 + regs[i].port, 0, |
| 273 | regs[i].val); |
| 274 | } |
| 275 | |
| 276 | if (regs[i].wait) |
| 277 | mdelay(75); |
| 278 | } |
| 279 | } |
| 280 | |
Marek Behún | 1c8eb90 | 2022-04-27 12:41:50 +0200 | [diff] [blame] | 281 | static void check_switch_address(struct udevice *bus, int addr) |
Marek Behún | 35b3513 | 2018-12-17 16:10:03 +0100 | [diff] [blame] | 282 | { |
| 283 | if (sw_scratch_read(bus, addr, 0x70) >> 3 != addr) |
| 284 | printf("Check of switch MDIO address failed for 0x%02x\n", |
| 285 | addr); |
| 286 | } |
| 287 | |
| 288 | static int sfp, pci, topaz, peridot, usb, passpci; |
| 289 | static int sfp_pos, peridot_pos[3]; |
| 290 | static int module_count; |
| 291 | |
| 292 | static int configure_peridots(struct gpio_desc *reset_gpio) |
| 293 | { |
| 294 | int i, ret; |
| 295 | u8 dout[MAX_MOX_MODULES]; |
| 296 | |
| 297 | memset(dout, 0, MAX_MOX_MODULES); |
| 298 | |
| 299 | /* set addresses of Peridot modules */ |
| 300 | for (i = 0; i < peridot; ++i) |
| 301 | dout[module_count - peridot_pos[i]] = (~i) & 3; |
| 302 | |
| 303 | /* |
| 304 | * if there is a SFP module connected to the last Peridot module, set |
| 305 | * the P10_SMODE to 1 for the Peridot module |
| 306 | */ |
| 307 | if (sfp) |
| 308 | dout[module_count - peridot_pos[i - 1]] |= 1 << 3; |
| 309 | |
| 310 | dm_gpio_set_value(reset_gpio, 1); |
| 311 | mdelay(10); |
| 312 | |
| 313 | ret = mox_do_spi(NULL, dout, module_count + 1); |
| 314 | |
| 315 | mdelay(10); |
| 316 | dm_gpio_set_value(reset_gpio, 0); |
| 317 | |
| 318 | mdelay(50); |
| 319 | |
| 320 | return ret; |
| 321 | } |
| 322 | |
| 323 | static int get_reset_gpio(struct gpio_desc *reset_gpio) |
| 324 | { |
| 325 | int node; |
| 326 | |
| 327 | node = fdt_node_offset_by_compatible(gd->fdt_blob, 0, "cznic,moxtet"); |
| 328 | if (node < 0) { |
| 329 | printf("Cannot find Moxtet bus device node!\n"); |
| 330 | return -1; |
| 331 | } |
| 332 | |
| 333 | gpio_request_by_name_nodev(offset_to_ofnode(node), "reset-gpios", 0, |
| 334 | reset_gpio, GPIOD_IS_OUT); |
| 335 | |
| 336 | if (!dm_gpio_is_valid(reset_gpio)) { |
| 337 | printf("Cannot find reset GPIO for Moxtet bus!\n"); |
| 338 | return -1; |
| 339 | } |
| 340 | |
Marek Behún | 35572c9 | 2018-12-17 16:10:08 +0100 | [diff] [blame] | 341 | return 0; |
| 342 | } |
| 343 | |
Pali Rohár | 584256a | 2022-08-29 15:44:48 +0200 | [diff] [blame] | 344 | /* Load default system DTB binary to $fdr_addr */ |
| 345 | static void load_spi_dtb(void) |
| 346 | { |
| 347 | const char *const env_name[1] = { "fdt_addr" }; |
| 348 | unsigned long size, offset; |
| 349 | struct udevice *spi_dev; |
| 350 | struct spi_flash *flash; |
| 351 | const char *addr_str; |
| 352 | unsigned long addr; |
| 353 | void *buf; |
| 354 | |
| 355 | addr_str = env_get(env_name[0]); |
| 356 | if (!addr_str) { |
| 357 | env_set_default_vars(1, (char * const *)env_name, 0); |
| 358 | addr_str = env_get(env_name[0]); |
| 359 | } |
| 360 | |
| 361 | if (!addr_str) |
| 362 | return; |
| 363 | |
| 364 | addr = hextoul(addr_str, NULL); |
| 365 | if (!addr) |
| 366 | return; |
| 367 | |
| 368 | spi_flash_probe_bus_cs(CONFIG_SF_DEFAULT_BUS, CONFIG_SF_DEFAULT_CS, &spi_dev); |
| 369 | flash = dev_get_uclass_priv(spi_dev); |
| 370 | if (!flash) |
| 371 | return; |
| 372 | |
| 373 | /* |
| 374 | * SPI NOR "dtb" partition offset & size hardcoded for now because the |
| 375 | * mtd subsystem does not offer finding the partition yet and we do not |
| 376 | * want to reimplement OF partition parser here. |
| 377 | */ |
| 378 | offset = 0x7f0000; |
| 379 | size = 0x10000; |
| 380 | |
| 381 | buf = map_physmem(addr, size, MAP_WRBACK); |
| 382 | if (!buf) |
| 383 | return; |
| 384 | |
| 385 | spi_flash_read(flash, offset, size, buf); |
| 386 | unmap_physmem(buf, size); |
| 387 | } |
| 388 | |
Marek Behún | 35572c9 | 2018-12-17 16:10:08 +0100 | [diff] [blame] | 389 | int misc_init_r(void) |
| 390 | { |
Marek Behún | 98c86c4 | 2021-10-09 19:33:42 +0200 | [diff] [blame] | 391 | u8 mac[2][6]; |
| 392 | int i, ret; |
Marek Behún | 35572c9 | 2018-12-17 16:10:08 +0100 | [diff] [blame] | 393 | |
Marek Behún | 98c86c4 | 2021-10-09 19:33:42 +0200 | [diff] [blame] | 394 | ret = mbox_sp_get_board_info(NULL, mac[0], mac[1], NULL, NULL); |
Marek Behún | 35572c9 | 2018-12-17 16:10:08 +0100 | [diff] [blame] | 395 | if (ret < 0) { |
| 396 | printf("Cannot read data from OTP!\n"); |
| 397 | return 0; |
| 398 | } |
| 399 | |
Marek Behún | 98c86c4 | 2021-10-09 19:33:42 +0200 | [diff] [blame] | 400 | for (i = 0; i < 2; ++i) { |
| 401 | u8 oldmac[6]; |
Marek Behún | 35572c9 | 2018-12-17 16:10:08 +0100 | [diff] [blame] | 402 | |
Marek Behún | 98c86c4 | 2021-10-09 19:33:42 +0200 | [diff] [blame] | 403 | if (is_valid_ethaddr(mac[i]) && |
| 404 | !eth_env_get_enetaddr_by_index("eth", i, oldmac)) |
| 405 | eth_env_set_enetaddr_by_index("eth", i, mac[i]); |
| 406 | } |
Marek Behún | 35572c9 | 2018-12-17 16:10:08 +0100 | [diff] [blame] | 407 | |
Pali Rohár | 584256a | 2022-08-29 15:44:48 +0200 | [diff] [blame] | 408 | load_spi_dtb(); |
| 409 | |
Marek Behún | 35b3513 | 2018-12-17 16:10:03 +0100 | [diff] [blame] | 410 | return 0; |
| 411 | } |
| 412 | |
Marek Behún | d1a3079 | 2021-06-07 16:34:48 +0200 | [diff] [blame] | 413 | static void mox_phy_modify(struct phy_device *phydev, int page, int reg, |
| 414 | u16 mask, u16 set) |
| 415 | { |
| 416 | int val; |
| 417 | |
| 418 | val = phydev->drv->readext(phydev, MDIO_DEVAD_NONE, page, reg); |
| 419 | val &= ~mask; |
| 420 | val |= set; |
| 421 | phydev->drv->writeext(phydev, MDIO_DEVAD_NONE, page, reg, val); |
| 422 | } |
| 423 | |
| 424 | static void mox_phy_leds_start_blinking(void) |
| 425 | { |
| 426 | struct phy_device *phydev; |
Marek Behún | 1c8eb90 | 2022-04-27 12:41:50 +0200 | [diff] [blame] | 427 | ofnode phy_node; |
Pali Rohár | a524db0 | 2022-02-14 11:34:26 +0100 | [diff] [blame] | 428 | |
Marek Behún | 1c8eb90 | 2022-04-27 12:41:50 +0200 | [diff] [blame] | 429 | phy_node = ofnode_get_phy_node(ofnode_path("ethernet0")); |
| 430 | if (!ofnode_valid(phy_node)) |
| 431 | goto err; |
Marek Behún | d1a3079 | 2021-06-07 16:34:48 +0200 | [diff] [blame] | 432 | |
Marek Behún | 1c8eb90 | 2022-04-27 12:41:50 +0200 | [diff] [blame] | 433 | phydev = dm_phy_find_by_ofnode(phy_node); |
| 434 | if (!phydev) |
| 435 | goto err; |
Marek Behún | d1a3079 | 2021-06-07 16:34:48 +0200 | [diff] [blame] | 436 | |
| 437 | mox_phy_modify(phydev, 3, 0x12, 0x700, 0x400); |
| 438 | mox_phy_modify(phydev, 3, 0x10, 0xff, 0xbb); |
Marek Behún | 1c8eb90 | 2022-04-27 12:41:50 +0200 | [diff] [blame] | 439 | |
| 440 | return; |
| 441 | err: |
| 442 | printf("Cannot get ethernet PHY!\n"); |
Marek Behún | d1a3079 | 2021-06-07 16:34:48 +0200 | [diff] [blame] | 443 | } |
| 444 | |
Marek Behún | 859ada5 | 2021-06-07 16:34:47 +0200 | [diff] [blame] | 445 | static bool read_reset_button(void) |
| 446 | { |
| 447 | struct udevice *button, *led; |
| 448 | int i; |
| 449 | |
Marek Behún | d050eab | 2021-11-26 14:57:11 +0100 | [diff] [blame] | 450 | if (device_get_global_by_ofnode( |
| 451 | ofnode_first_subnode(ofnode_by_compatible(ofnode_null(), |
| 452 | "gpio-keys")), |
| 453 | &button)) { |
Marek Behún | 859ada5 | 2021-06-07 16:34:47 +0200 | [diff] [blame] | 454 | printf("Cannot find reset button!\n"); |
| 455 | return false; |
| 456 | } |
| 457 | |
Marek Behún | d050eab | 2021-11-26 14:57:11 +0100 | [diff] [blame] | 458 | if (device_get_global_by_ofnode( |
| 459 | ofnode_first_subnode(ofnode_by_compatible(ofnode_null(), |
| 460 | "gpio-leds")), |
| 461 | &led)) { |
Marek Behún | 859ada5 | 2021-06-07 16:34:47 +0200 | [diff] [blame] | 462 | printf("Cannot find status LED!\n"); |
| 463 | return false; |
| 464 | } |
| 465 | |
| 466 | led_set_state(led, LEDST_ON); |
| 467 | |
| 468 | for (i = 0; i < 21; ++i) { |
| 469 | if (button_get_state(button) != BUTTON_ON) |
| 470 | return false; |
| 471 | if (i < 20) |
| 472 | mdelay(50); |
| 473 | } |
| 474 | |
| 475 | led_set_state(led, LEDST_OFF); |
| 476 | |
| 477 | return true; |
| 478 | } |
| 479 | |
| 480 | static void handle_reset_button(void) |
| 481 | { |
Pali Rohár | 905c3bf | 2021-06-14 16:45:58 +0200 | [diff] [blame] | 482 | const char * const vars[1] = { "bootcmd_rescue", }; |
| 483 | |
| 484 | /* |
| 485 | * Ensure that bootcmd_rescue has always stock value, so that running |
| 486 | * run bootcmd_rescue |
| 487 | * always works correctly. |
| 488 | */ |
| 489 | env_set_default_vars(1, (char * const *)vars, 0); |
| 490 | |
Marek Behún | 859ada5 | 2021-06-07 16:34:47 +0200 | [diff] [blame] | 491 | if (read_reset_button()) { |
Pali Rohár | 8adec65 | 2022-08-27 20:49:20 +0200 | [diff] [blame] | 492 | const char * const vars[3] = { |
Marek Behún | 859ada5 | 2021-06-07 16:34:47 +0200 | [diff] [blame] | 493 | "bootcmd", |
Pali Rohár | 8adec65 | 2022-08-27 20:49:20 +0200 | [diff] [blame] | 494 | "bootdelay", |
Marek Behún | 859ada5 | 2021-06-07 16:34:47 +0200 | [diff] [blame] | 495 | "distro_bootcmd", |
| 496 | }; |
| 497 | |
| 498 | /* |
| 499 | * Set the above envs to their default values, in case the user |
| 500 | * managed to break them. |
| 501 | */ |
Pali Rohár | 8adec65 | 2022-08-27 20:49:20 +0200 | [diff] [blame] | 502 | env_set_default_vars(3, (char * const *)vars, 0); |
Marek Behún | 859ada5 | 2021-06-07 16:34:47 +0200 | [diff] [blame] | 503 | |
| 504 | /* Ensure bootcmd_rescue is used by distroboot */ |
| 505 | env_set("boot_targets", "rescue"); |
| 506 | |
Marek Behún | d1a3079 | 2021-06-07 16:34:48 +0200 | [diff] [blame] | 507 | /* start blinking PHY LEDs */ |
| 508 | mox_phy_leds_start_blinking(); |
| 509 | |
Marek Behún | 859ada5 | 2021-06-07 16:34:47 +0200 | [diff] [blame] | 510 | printf("RESET button was pressed, overwriting boot_targets!\n"); |
| 511 | } else { |
| 512 | /* |
| 513 | * In case the user somehow managed to save environment with |
| 514 | * boot_targets=rescue, reset boot_targets to default value. |
| 515 | * This could happen in subsequent commands if bootcmd_rescue |
| 516 | * failed. |
| 517 | */ |
| 518 | if (!strcmp(env_get("boot_targets"), "rescue")) { |
| 519 | const char * const vars[1] = { |
| 520 | "boot_targets", |
| 521 | }; |
| 522 | |
| 523 | env_set_default_vars(1, (char * const *)vars, 0); |
| 524 | } |
| 525 | } |
| 526 | } |
| 527 | |
Marek Behún | 84ee2cd | 2021-10-09 19:33:40 +0200 | [diff] [blame] | 528 | int show_board_info(void) |
Marek Behún | 35572c9 | 2018-12-17 16:10:08 +0100 | [diff] [blame] | 529 | { |
Marek Behún | 84ee2cd | 2021-10-09 19:33:40 +0200 | [diff] [blame] | 530 | int i, ret, board_version, ram_size, is_sd; |
Marek Behún | 35572c9 | 2018-12-17 16:10:08 +0100 | [diff] [blame] | 531 | const char *pub_key; |
Marek Behún | 84ee2cd | 2021-10-09 19:33:40 +0200 | [diff] [blame] | 532 | const u8 *topology; |
| 533 | u64 serial_number; |
| 534 | |
| 535 | printf("Model: CZ.NIC Turris Mox Board\n"); |
Marek Behún | 35572c9 | 2018-12-17 16:10:08 +0100 | [diff] [blame] | 536 | |
| 537 | ret = mbox_sp_get_board_info(&serial_number, NULL, NULL, &board_version, |
| 538 | &ram_size); |
Marek Behún | 84ee2cd | 2021-10-09 19:33:40 +0200 | [diff] [blame] | 539 | if (ret < 0) { |
| 540 | printf(" Cannot read board info: %i\n", ret); |
| 541 | } else { |
| 542 | printf(" Board version: %i\n", board_version); |
| 543 | printf(" RAM size: %i MiB\n", ram_size); |
| 544 | printf(" Serial Number: %016llX\n", serial_number); |
| 545 | } |
Marek Behún | 35572c9 | 2018-12-17 16:10:08 +0100 | [diff] [blame] | 546 | |
| 547 | pub_key = mox_sp_get_ecdsa_public_key(); |
| 548 | if (pub_key) |
| 549 | printf(" ECDSA Public Key: %s\n", pub_key); |
| 550 | else |
Marek Behún | 84ee2cd | 2021-10-09 19:33:40 +0200 | [diff] [blame] | 551 | printf(" Cannot read ECDSA Public Key\n"); |
Marek Behún | 35572c9 | 2018-12-17 16:10:08 +0100 | [diff] [blame] | 552 | |
Marek Behún | f68ed8b | 2018-12-17 16:10:01 +0100 | [diff] [blame] | 553 | ret = mox_get_topology(&topology, &module_count, &is_sd); |
Marek Behún | 84ee2cd | 2021-10-09 19:33:40 +0200 | [diff] [blame] | 554 | if (ret) |
Marek Behún | f68ed8b | 2018-12-17 16:10:01 +0100 | [diff] [blame] | 555 | printf("Cannot read module topology!\n"); |
Marek Behún | f68ed8b | 2018-12-17 16:10:01 +0100 | [diff] [blame] | 556 | |
Marek Behún | 35572c9 | 2018-12-17 16:10:08 +0100 | [diff] [blame] | 557 | printf(" SD/eMMC version: %s\n", is_sd ? "SD" : "eMMC"); |
| 558 | |
| 559 | if (module_count) |
| 560 | printf("Module Topology:\n"); |
| 561 | |
Marek Behún | f68ed8b | 2018-12-17 16:10:01 +0100 | [diff] [blame] | 562 | for (i = 0; i < module_count; ++i) { |
| 563 | switch (topology[i]) { |
| 564 | case MOX_MODULE_SFP: |
| 565 | printf("% 4i: SFP Module\n", i + 1); |
Marek Behún | f835bed | 2018-04-24 17:21:31 +0200 | [diff] [blame] | 566 | break; |
Marek Behún | f68ed8b | 2018-12-17 16:10:01 +0100 | [diff] [blame] | 567 | case MOX_MODULE_PCI: |
| 568 | printf("% 4i: Mini-PCIe Module\n", i + 1); |
Marek Behún | f835bed | 2018-04-24 17:21:31 +0200 | [diff] [blame] | 569 | break; |
Marek Behún | f68ed8b | 2018-12-17 16:10:01 +0100 | [diff] [blame] | 570 | case MOX_MODULE_TOPAZ: |
| 571 | printf("% 4i: Topaz Switch Module (4-port)\n", i + 1); |
| 572 | break; |
| 573 | case MOX_MODULE_PERIDOT: |
| 574 | printf("% 4i: Peridot Switch Module (8-port)\n", i + 1); |
| 575 | break; |
| 576 | case MOX_MODULE_USB3: |
| 577 | printf("% 4i: USB 3.0 Module (4 ports)\n", i + 1); |
| 578 | break; |
| 579 | case MOX_MODULE_PASSPCI: |
| 580 | printf("% 4i: Passthrough Mini-PCIe Module\n", i + 1); |
Marek Behún | f835bed | 2018-04-24 17:21:31 +0200 | [diff] [blame] | 581 | break; |
| 582 | default: |
Marek Behún | f68ed8b | 2018-12-17 16:10:01 +0100 | [diff] [blame] | 583 | printf("% 4i: unknown (ID %i)\n", i + 1, topology[i]); |
Marek Behún | f835bed | 2018-04-24 17:21:31 +0200 | [diff] [blame] | 584 | } |
| 585 | } |
Marek Behún | f835bed | 2018-04-24 17:21:31 +0200 | [diff] [blame] | 586 | |
Marek Behún | 84ee2cd | 2021-10-09 19:33:40 +0200 | [diff] [blame] | 587 | /* check if modules are connected in supported mode */ |
Marek Behún | 35b3513 | 2018-12-17 16:10:03 +0100 | [diff] [blame] | 588 | for (i = 0; i < module_count; ++i) { |
| 589 | switch (topology[i]) { |
| 590 | case MOX_MODULE_SFP: |
| 591 | if (sfp) { |
| 592 | printf("Error: Only one SFP module is supported!\n"); |
| 593 | } else if (topaz) { |
| 594 | printf("Error: SFP module cannot be connected after Topaz Switch module!\n"); |
| 595 | } else { |
| 596 | sfp_pos = i; |
| 597 | ++sfp; |
| 598 | } |
| 599 | break; |
| 600 | case MOX_MODULE_PCI: |
Marek Behún | 4529fce | 2020-04-08 12:02:05 +0200 | [diff] [blame] | 601 | if (pci) |
Marek Behún | 35b3513 | 2018-12-17 16:10:03 +0100 | [diff] [blame] | 602 | printf("Error: Only one Mini-PCIe module is supported!\n"); |
Marek Behún | 4529fce | 2020-04-08 12:02:05 +0200 | [diff] [blame] | 603 | else if (usb) |
Marek Behún | 35b3513 | 2018-12-17 16:10:03 +0100 | [diff] [blame] | 604 | printf("Error: Mini-PCIe module cannot come after USB 3.0 module!\n"); |
Marek Behún | 4529fce | 2020-04-08 12:02:05 +0200 | [diff] [blame] | 605 | else if (i && (i != 1 || !passpci)) |
Marek Behún | 35b3513 | 2018-12-17 16:10:03 +0100 | [diff] [blame] | 606 | printf("Error: Mini-PCIe module should be the first connected module or come right after Passthrough Mini-PCIe module!\n"); |
Marek Behún | 4529fce | 2020-04-08 12:02:05 +0200 | [diff] [blame] | 607 | else |
Marek Behún | 35b3513 | 2018-12-17 16:10:03 +0100 | [diff] [blame] | 608 | ++pci; |
Marek Behún | 35b3513 | 2018-12-17 16:10:03 +0100 | [diff] [blame] | 609 | break; |
| 610 | case MOX_MODULE_TOPAZ: |
Marek Behún | 4529fce | 2020-04-08 12:02:05 +0200 | [diff] [blame] | 611 | if (topaz) |
Marek Behún | 35b3513 | 2018-12-17 16:10:03 +0100 | [diff] [blame] | 612 | printf("Error: Only one Topaz module is supported!\n"); |
Marek Behún | 4529fce | 2020-04-08 12:02:05 +0200 | [diff] [blame] | 613 | else if (peridot >= 3) |
Marek Behún | 35b3513 | 2018-12-17 16:10:03 +0100 | [diff] [blame] | 614 | printf("Error: At most two Peridot modules can come before Topaz module!\n"); |
Marek Behún | 4529fce | 2020-04-08 12:02:05 +0200 | [diff] [blame] | 615 | else |
Marek Behún | 35b3513 | 2018-12-17 16:10:03 +0100 | [diff] [blame] | 616 | ++topaz; |
Marek Behún | 35b3513 | 2018-12-17 16:10:03 +0100 | [diff] [blame] | 617 | break; |
| 618 | case MOX_MODULE_PERIDOT: |
| 619 | if (sfp || topaz) { |
| 620 | printf("Error: Peridot module must come before SFP or Topaz module!\n"); |
| 621 | } else if (peridot >= 3) { |
| 622 | printf("Error: At most three Peridot modules are supported!\n"); |
| 623 | } else { |
| 624 | peridot_pos[peridot] = i; |
| 625 | ++peridot; |
| 626 | } |
| 627 | break; |
| 628 | case MOX_MODULE_USB3: |
Marek Behún | 4529fce | 2020-04-08 12:02:05 +0200 | [diff] [blame] | 629 | if (pci) |
Marek Behún | 35b3513 | 2018-12-17 16:10:03 +0100 | [diff] [blame] | 630 | printf("Error: USB 3.0 module cannot come after Mini-PCIe module!\n"); |
Marek Behún | 4529fce | 2020-04-08 12:02:05 +0200 | [diff] [blame] | 631 | else if (usb) |
Marek Behún | 35b3513 | 2018-12-17 16:10:03 +0100 | [diff] [blame] | 632 | printf("Error: Only one USB 3.0 module is supported!\n"); |
Marek Behún | 4529fce | 2020-04-08 12:02:05 +0200 | [diff] [blame] | 633 | else if (i && (i != 1 || !passpci)) |
Marek Behún | 35b3513 | 2018-12-17 16:10:03 +0100 | [diff] [blame] | 634 | printf("Error: USB 3.0 module should be the first connected module or come right after Passthrough Mini-PCIe module!\n"); |
Marek Behún | 4529fce | 2020-04-08 12:02:05 +0200 | [diff] [blame] | 635 | else |
Marek Behún | 35b3513 | 2018-12-17 16:10:03 +0100 | [diff] [blame] | 636 | ++usb; |
Marek Behún | 35b3513 | 2018-12-17 16:10:03 +0100 | [diff] [blame] | 637 | break; |
| 638 | case MOX_MODULE_PASSPCI: |
Marek Behún | 4529fce | 2020-04-08 12:02:05 +0200 | [diff] [blame] | 639 | if (passpci) |
Marek Behún | 35b3513 | 2018-12-17 16:10:03 +0100 | [diff] [blame] | 640 | printf("Error: Only one Passthrough Mini-PCIe module is supported!\n"); |
Marek Behún | 4529fce | 2020-04-08 12:02:05 +0200 | [diff] [blame] | 641 | else if (i != 0) |
Marek Behún | 35b3513 | 2018-12-17 16:10:03 +0100 | [diff] [blame] | 642 | printf("Error: Passthrough Mini-PCIe module should be the first connected module!\n"); |
Marek Behún | 4529fce | 2020-04-08 12:02:05 +0200 | [diff] [blame] | 643 | else |
Marek Behún | 35b3513 | 2018-12-17 16:10:03 +0100 | [diff] [blame] | 644 | ++passpci; |
Marek Behún | 35b3513 | 2018-12-17 16:10:03 +0100 | [diff] [blame] | 645 | } |
| 646 | } |
| 647 | |
Marek Behún | 84ee2cd | 2021-10-09 19:33:40 +0200 | [diff] [blame] | 648 | return 0; |
| 649 | } |
Marek Behún | 35b3513 | 2018-12-17 16:10:03 +0100 | [diff] [blame] | 650 | |
Marek Behún | 1c8eb90 | 2022-04-27 12:41:50 +0200 | [diff] [blame] | 651 | static struct udevice *mox_mdio_bus(void) |
| 652 | { |
| 653 | struct udevice *bus; |
| 654 | ofnode node; |
| 655 | |
| 656 | node = ofnode_by_compatible(ofnode_null(), "marvell,orion-mdio"); |
| 657 | if (!ofnode_valid(node)) |
| 658 | goto err; |
| 659 | |
| 660 | dm_mdio_probe_devices(); |
| 661 | |
| 662 | if (uclass_get_device_by_ofnode(UCLASS_MDIO, node, &bus)) |
| 663 | goto err; |
| 664 | |
| 665 | return bus; |
| 666 | err: |
| 667 | printf("Cannot get MDIO bus device!\n"); |
| 668 | return NULL; |
| 669 | } |
| 670 | |
Simon Glass | 1cedca1 | 2023-08-21 21:17:01 -0600 | [diff] [blame] | 671 | static int last_stage_init(void) |
Marek Behún | 84ee2cd | 2021-10-09 19:33:40 +0200 | [diff] [blame] | 672 | { |
| 673 | struct gpio_desc reset_gpio = {}; |
| 674 | |
| 675 | /* configure modules */ |
Marek Behún | 35b3513 | 2018-12-17 16:10:03 +0100 | [diff] [blame] | 676 | if (get_reset_gpio(&reset_gpio) < 0) |
Marek Behún | 84d67ac | 2021-10-09 19:33:41 +0200 | [diff] [blame] | 677 | goto handle_reset_btn; |
Marek Behún | 35b3513 | 2018-12-17 16:10:03 +0100 | [diff] [blame] | 678 | |
| 679 | if (peridot > 0) { |
| 680 | if (configure_peridots(&reset_gpio) < 0) { |
| 681 | printf("Cannot configure Peridot modules!\n"); |
| 682 | peridot = 0; |
| 683 | } |
| 684 | } else { |
| 685 | dm_gpio_set_value(&reset_gpio, 1); |
| 686 | mdelay(50); |
| 687 | dm_gpio_set_value(&reset_gpio, 0); |
| 688 | mdelay(50); |
| 689 | } |
| 690 | |
Marek Behún | 84ee2cd | 2021-10-09 19:33:40 +0200 | [diff] [blame] | 691 | /* |
| 692 | * check if the addresses are set by reading Scratch & Misc register |
| 693 | * 0x70 of Peridot (and potentially Topaz) modules |
| 694 | */ |
Marek Behún | 35b3513 | 2018-12-17 16:10:03 +0100 | [diff] [blame] | 695 | if (peridot || topaz) { |
Marek Behún | 1c8eb90 | 2022-04-27 12:41:50 +0200 | [diff] [blame] | 696 | struct udevice *bus = mox_mdio_bus(); |
Marek Behún | 35b3513 | 2018-12-17 16:10:03 +0100 | [diff] [blame] | 697 | |
Marek Behún | 1c8eb90 | 2022-04-27 12:41:50 +0200 | [diff] [blame] | 698 | if (bus) { |
Marek Behún | 84ee2cd | 2021-10-09 19:33:40 +0200 | [diff] [blame] | 699 | int i; |
| 700 | |
Marek Behún | 35b3513 | 2018-12-17 16:10:03 +0100 | [diff] [blame] | 701 | for (i = 0; i < peridot; ++i) |
| 702 | check_switch_address(bus, 0x10 + i); |
| 703 | |
| 704 | if (topaz) |
| 705 | check_switch_address(bus, 0x2); |
| 706 | |
| 707 | sw_blink_leds(bus, peridot, topaz); |
| 708 | } |
| 709 | } |
| 710 | |
Marek Behún | 84d67ac | 2021-10-09 19:33:41 +0200 | [diff] [blame] | 711 | handle_reset_btn: |
Marek Behún | 859ada5 | 2021-06-07 16:34:47 +0200 | [diff] [blame] | 712 | handle_reset_button(); |
| 713 | |
Marek Behún | f68ed8b | 2018-12-17 16:10:01 +0100 | [diff] [blame] | 714 | return 0; |
Marek Behún | f835bed | 2018-04-24 17:21:31 +0200 | [diff] [blame] | 715 | } |
Simon Glass | 1cedca1 | 2023-08-21 21:17:01 -0600 | [diff] [blame] | 716 | EVENT_SPY_SIMPLE(EVT_LAST_STAGE_INIT, last_stage_init); |
Marek Behún | 4529fce | 2020-04-08 12:02:05 +0200 | [diff] [blame] | 717 | |
| 718 | #if defined(CONFIG_OF_BOARD_SETUP) |
| 719 | |
Marek Behún | d050eab | 2021-11-26 14:57:11 +0100 | [diff] [blame] | 720 | static bool is_topaz(int id) |
Marek Behún | 4529fce | 2020-04-08 12:02:05 +0200 | [diff] [blame] | 721 | { |
Marek Behún | d050eab | 2021-11-26 14:57:11 +0100 | [diff] [blame] | 722 | return topaz && id == peridot + topaz - 1; |
Marek Behún | 4529fce | 2020-04-08 12:02:05 +0200 | [diff] [blame] | 723 | } |
| 724 | |
Marek Behún | d050eab | 2021-11-26 14:57:11 +0100 | [diff] [blame] | 725 | static int switch_addr(int id) |
Marek Behún | 4529fce | 2020-04-08 12:02:05 +0200 | [diff] [blame] | 726 | { |
Marek Behún | d050eab | 2021-11-26 14:57:11 +0100 | [diff] [blame] | 727 | return is_topaz(id) ? 0x2 : 0x10 + id; |
Marek Behún | 4529fce | 2020-04-08 12:02:05 +0200 | [diff] [blame] | 728 | } |
| 729 | |
Marek Behún | d050eab | 2021-11-26 14:57:11 +0100 | [diff] [blame] | 730 | static int setup_switch(void *blob, int id) |
Marek Behún | 4529fce | 2020-04-08 12:02:05 +0200 | [diff] [blame] | 731 | { |
Marek Behún | d050eab | 2021-11-26 14:57:11 +0100 | [diff] [blame] | 732 | int res, addr, i, node; |
| 733 | char mdio_path[64]; |
Marek Behún | 4529fce | 2020-04-08 12:02:05 +0200 | [diff] [blame] | 734 | |
Marek Behún | d050eab | 2021-11-26 14:57:11 +0100 | [diff] [blame] | 735 | node = fdt_node_offset_by_compatible(blob, -1, "marvell,orion-mdio"); |
Marek Behún | 4529fce | 2020-04-08 12:02:05 +0200 | [diff] [blame] | 736 | if (node < 0) |
| 737 | return node; |
| 738 | |
Marek Behún | d050eab | 2021-11-26 14:57:11 +0100 | [diff] [blame] | 739 | res = fdt_get_path(blob, node, mdio_path, sizeof(mdio_path)); |
Marek Behún | 4529fce | 2020-04-08 12:02:05 +0200 | [diff] [blame] | 740 | if (res < 0) |
| 741 | return res; |
| 742 | |
Marek Behún | 4529fce | 2020-04-08 12:02:05 +0200 | [diff] [blame] | 743 | addr = switch_addr(id); |
| 744 | |
| 745 | /* first enable the switch by setting status = "okay" */ |
Marek Behún | d050eab | 2021-11-26 14:57:11 +0100 | [diff] [blame] | 746 | res = fdt_status_okay_by_pathf(blob, "%s/switch%i@%x", mdio_path, id, |
| 747 | addr); |
Marek Behún | 4529fce | 2020-04-08 12:02:05 +0200 | [diff] [blame] | 748 | if (res < 0) |
| 749 | return res; |
| 750 | |
| 751 | /* |
| 752 | * now if there are more switches or a SFP module coming after, |
| 753 | * enable corresponding ports |
| 754 | */ |
| 755 | if (id < peridot + topaz - 1) { |
Marek Behún | d050eab | 2021-11-26 14:57:11 +0100 | [diff] [blame] | 756 | res = fdt_status_okay_by_pathf(blob, |
| 757 | "%s/switch%i@%x/ports/port@a", |
| 758 | mdio_path, id, addr); |
Marek Behún | 4529fce | 2020-04-08 12:02:05 +0200 | [diff] [blame] | 759 | } else if (id == peridot - 1 && !topaz && sfp) { |
Marek Behún | d050eab | 2021-11-26 14:57:11 +0100 | [diff] [blame] | 760 | res = fdt_status_okay_by_pathf(blob, |
| 761 | "%s/switch%i@%x/ports/port-sfp@a", |
| 762 | mdio_path, id, addr); |
Marek Behún | 4529fce | 2020-04-08 12:02:05 +0200 | [diff] [blame] | 763 | } else { |
| 764 | res = 0; |
| 765 | } |
| 766 | if (res < 0) |
| 767 | return res; |
| 768 | |
| 769 | if (id >= peridot + topaz - 1) |
| 770 | return 0; |
| 771 | |
| 772 | /* finally change link property if needed */ |
Marek Behún | d050eab | 2021-11-26 14:57:11 +0100 | [diff] [blame] | 773 | node = fdt_node_offset_by_pathf(blob, "%s/switch%i@%x/ports/port@a", |
| 774 | mdio_path, id, addr); |
Marek Behún | 4529fce | 2020-04-08 12:02:05 +0200 | [diff] [blame] | 775 | if (node < 0) |
| 776 | return node; |
| 777 | |
| 778 | for (i = id + 1; i < peridot + topaz; ++i) { |
Marek Behún | d050eab | 2021-11-26 14:57:11 +0100 | [diff] [blame] | 779 | unsigned int phandle; |
| 780 | |
| 781 | phandle = fdt_create_phandle_by_pathf(blob, |
| 782 | "%s/switch%i@%x/ports/port@%x", |
| 783 | mdio_path, i, |
| 784 | switch_addr(i), |
| 785 | is_topaz(i) ? 5 : 9); |
| 786 | if (!phandle) |
| 787 | return -FDT_ERR_NOPHANDLES; |
Marek Behún | 4529fce | 2020-04-08 12:02:05 +0200 | [diff] [blame] | 788 | |
| 789 | if (i == id + 1) |
| 790 | res = fdt_setprop_u32(blob, node, "link", phandle); |
| 791 | else |
| 792 | res = fdt_appendprop_u32(blob, node, "link", phandle); |
| 793 | if (res < 0) |
| 794 | return res; |
| 795 | } |
| 796 | |
| 797 | return 0; |
| 798 | } |
| 799 | |
Masahiro Yamada | f7ed78b | 2020-06-26 15:13:33 +0900 | [diff] [blame] | 800 | int ft_board_setup(void *blob, struct bd_info *bd) |
Marek Behún | 4529fce | 2020-04-08 12:02:05 +0200 | [diff] [blame] | 801 | { |
Marek Behún | d050eab | 2021-11-26 14:57:11 +0100 | [diff] [blame] | 802 | int res; |
Marek Behún | 4529fce | 2020-04-08 12:02:05 +0200 | [diff] [blame] | 803 | |
| 804 | /* |
| 805 | * If MOX B (PCI), MOX F (USB) or MOX G (Passthrough PCI) modules are |
| 806 | * connected, enable the PCIe node. |
| 807 | */ |
| 808 | if (pci || usb || passpci) { |
Marek Behún | d050eab | 2021-11-26 14:57:11 +0100 | [diff] [blame] | 809 | res = fdt_status_okay_by_compatible(blob, |
| 810 | "marvell,armada-3700-pcie"); |
Marek Behún | 4529fce | 2020-04-08 12:02:05 +0200 | [diff] [blame] | 811 | if (res < 0) |
| 812 | return res; |
Marek Behún | 455ef5b | 2020-04-08 19:25:22 +0200 | [diff] [blame] | 813 | |
| 814 | /* Fix PCIe regions for devices with 4 GB RAM */ |
| 815 | res = a3700_fdt_fix_pcie_regions(blob); |
| 816 | if (res < 0) |
| 817 | return res; |
Marek Behún | 4529fce | 2020-04-08 12:02:05 +0200 | [diff] [blame] | 818 | } |
| 819 | |
| 820 | /* |
| 821 | * If MOX C (Topaz switch) and/or MOX E (Peridot switch) are connected, |
| 822 | * enable the eth1 node and setup the switches. |
| 823 | */ |
| 824 | if (peridot || topaz) { |
| 825 | int i; |
| 826 | |
Marek Behún | d050eab | 2021-11-26 14:57:11 +0100 | [diff] [blame] | 827 | res = fdt_status_okay_by_alias(blob, "ethernet1"); |
Marek Behún | 4529fce | 2020-04-08 12:02:05 +0200 | [diff] [blame] | 828 | if (res < 0) |
| 829 | return res; |
| 830 | |
| 831 | for (i = 0; i < peridot + topaz; ++i) { |
| 832 | res = setup_switch(blob, i); |
| 833 | if (res < 0) |
| 834 | return res; |
| 835 | } |
| 836 | } |
| 837 | |
| 838 | /* |
| 839 | * If MOX D (SFP cage module) is connected, enable the SFP node and eth1 |
| 840 | * node. If there is no Peridot switch between MOX A and MOX D, add link |
| 841 | * to the SFP node to eth1 node. |
| 842 | * Also enable and configure SFP GPIO controller node. |
| 843 | */ |
| 844 | if (sfp) { |
Marek Behún | d050eab | 2021-11-26 14:57:11 +0100 | [diff] [blame] | 845 | int node; |
| 846 | |
| 847 | res = fdt_status_okay_by_compatible(blob, "sff,sfp"); |
Marek Behún | 4529fce | 2020-04-08 12:02:05 +0200 | [diff] [blame] | 848 | if (res < 0) |
| 849 | return res; |
| 850 | |
Marek Behún | d050eab | 2021-11-26 14:57:11 +0100 | [diff] [blame] | 851 | res = fdt_status_okay_by_alias(blob, "ethernet1"); |
Marek Behún | 4529fce | 2020-04-08 12:02:05 +0200 | [diff] [blame] | 852 | if (res < 0) |
| 853 | return res; |
| 854 | |
| 855 | if (!peridot) { |
Marek Behún | d050eab | 2021-11-26 14:57:11 +0100 | [diff] [blame] | 856 | unsigned int phandle; |
Marek Behún | 4529fce | 2020-04-08 12:02:05 +0200 | [diff] [blame] | 857 | |
Marek Behún | d050eab | 2021-11-26 14:57:11 +0100 | [diff] [blame] | 858 | phandle = fdt_create_phandle_by_compatible(blob, |
| 859 | "sff,sfp"); |
| 860 | if (!phandle) |
| 861 | return -FDT_ERR_NOPHANDLES; |
| 862 | |
| 863 | node = fdt_path_offset(blob, "ethernet1"); |
Marek Behún | 4529fce | 2020-04-08 12:02:05 +0200 | [diff] [blame] | 864 | if (node < 0) |
| 865 | return node; |
| 866 | |
| 867 | res = fdt_setprop_u32(blob, node, "sfp", phandle); |
| 868 | if (res < 0) |
| 869 | return res; |
| 870 | |
| 871 | res = fdt_setprop_string(blob, node, "phy-mode", |
| 872 | "sgmii"); |
| 873 | if (res < 0) |
| 874 | return res; |
Pali Rohár | db69aef | 2022-08-10 12:54:11 +0200 | [diff] [blame] | 875 | |
| 876 | res = fdt_setprop_string(blob, node, "label", |
| 877 | "sfp"); |
| 878 | if (res < 0) |
| 879 | return res; |
Marek Behún | 4529fce | 2020-04-08 12:02:05 +0200 | [diff] [blame] | 880 | } |
| 881 | |
Marek Behún | d050eab | 2021-11-26 14:57:11 +0100 | [diff] [blame] | 882 | res = fdt_status_okay_by_compatible(blob, "cznic,moxtet-gpio"); |
Marek Behún | 4529fce | 2020-04-08 12:02:05 +0200 | [diff] [blame] | 883 | if (res < 0) |
| 884 | return res; |
| 885 | |
| 886 | if (sfp_pos) { |
| 887 | char newname[16]; |
| 888 | |
| 889 | /* moxtet-sfp is on non-zero position, change default */ |
Marek Behún | d050eab | 2021-11-26 14:57:11 +0100 | [diff] [blame] | 890 | node = fdt_node_offset_by_compatible(blob, -1, |
| 891 | "cznic,moxtet-gpio"); |
Marek Behún | 4529fce | 2020-04-08 12:02:05 +0200 | [diff] [blame] | 892 | if (node < 0) |
| 893 | return node; |
| 894 | |
| 895 | res = fdt_setprop_u32(blob, node, "reg", sfp_pos); |
| 896 | if (res < 0) |
| 897 | return res; |
| 898 | |
| 899 | sprintf(newname, "gpio@%x", sfp_pos); |
| 900 | |
| 901 | res = fdt_set_name(blob, node, newname); |
| 902 | if (res < 0) |
| 903 | return res; |
| 904 | } |
| 905 | } |
| 906 | |
| 907 | fdt_fixup_ethernet(blob); |
| 908 | |
| 909 | /* Finally remove disabled nodes, as per Rob Herring's request. */ |
Marek Behún | ac4d9ff | 2021-11-26 14:57:15 +0100 | [diff] [blame] | 910 | fdt_delete_disabled_nodes(blob); |
Marek Behún | 4529fce | 2020-04-08 12:02:05 +0200 | [diff] [blame] | 911 | |
| 912 | return 0; |
| 913 | } |
| 914 | |
| 915 | #endif |