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