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