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