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