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