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; |
Pali Rohár | a524db0 | 2022-02-14 11:34:26 +0100 | [diff] [blame] | 378 | const char *node_name; |
| 379 | int node; |
Marek Behún | d1a3079 | 2021-06-07 16:34:48 +0200 | [diff] [blame] | 380 | |
Pali Rohár | a524db0 | 2022-02-14 11:34:26 +0100 | [diff] [blame] | 381 | node = fdt_path_offset(gd->fdt_blob, "ethernet0"); |
| 382 | if (node < 0) { |
| 383 | printf("Cannot get eth0!\n"); |
| 384 | return; |
| 385 | } |
| 386 | |
| 387 | node_name = fdt_get_name(gd->fdt_blob, node, NULL); |
| 388 | if (!node_name) { |
| 389 | printf("Cannot get eth0 node name!\n"); |
| 390 | return; |
| 391 | } |
| 392 | |
| 393 | bus = miiphy_get_dev_by_name(node_name); |
Marek Behún | d1a3079 | 2021-06-07 16:34:48 +0200 | [diff] [blame] | 394 | if (!bus) { |
| 395 | printf("Cannot get MDIO bus device!\n"); |
| 396 | return; |
| 397 | } |
| 398 | |
Marek Behún | 3927efb | 2022-04-07 00:33:08 +0200 | [diff] [blame^] | 399 | phydev = phy_find_by_mask(bus, BIT(1)); |
Marek Behún | d1a3079 | 2021-06-07 16:34:48 +0200 | [diff] [blame] | 400 | if (!phydev) { |
| 401 | printf("Cannot get ethernet PHY!\n"); |
| 402 | return; |
| 403 | } |
| 404 | |
| 405 | mox_phy_modify(phydev, 3, 0x12, 0x700, 0x400); |
| 406 | mox_phy_modify(phydev, 3, 0x10, 0xff, 0xbb); |
| 407 | } |
| 408 | |
Marek Behún | 859ada5 | 2021-06-07 16:34:47 +0200 | [diff] [blame] | 409 | static bool read_reset_button(void) |
| 410 | { |
| 411 | struct udevice *button, *led; |
| 412 | int i; |
| 413 | |
Marek Behún | d050eab | 2021-11-26 14:57:11 +0100 | [diff] [blame] | 414 | if (device_get_global_by_ofnode( |
| 415 | ofnode_first_subnode(ofnode_by_compatible(ofnode_null(), |
| 416 | "gpio-keys")), |
| 417 | &button)) { |
Marek Behún | 859ada5 | 2021-06-07 16:34:47 +0200 | [diff] [blame] | 418 | printf("Cannot find reset button!\n"); |
| 419 | return false; |
| 420 | } |
| 421 | |
Marek Behún | d050eab | 2021-11-26 14:57:11 +0100 | [diff] [blame] | 422 | if (device_get_global_by_ofnode( |
| 423 | ofnode_first_subnode(ofnode_by_compatible(ofnode_null(), |
| 424 | "gpio-leds")), |
| 425 | &led)) { |
Marek Behún | 859ada5 | 2021-06-07 16:34:47 +0200 | [diff] [blame] | 426 | printf("Cannot find status LED!\n"); |
| 427 | return false; |
| 428 | } |
| 429 | |
| 430 | led_set_state(led, LEDST_ON); |
| 431 | |
| 432 | for (i = 0; i < 21; ++i) { |
| 433 | if (button_get_state(button) != BUTTON_ON) |
| 434 | return false; |
| 435 | if (i < 20) |
| 436 | mdelay(50); |
| 437 | } |
| 438 | |
| 439 | led_set_state(led, LEDST_OFF); |
| 440 | |
| 441 | return true; |
| 442 | } |
| 443 | |
| 444 | static void handle_reset_button(void) |
| 445 | { |
Pali Rohár | 905c3bf | 2021-06-14 16:45:58 +0200 | [diff] [blame] | 446 | const char * const vars[1] = { "bootcmd_rescue", }; |
| 447 | |
| 448 | /* |
| 449 | * Ensure that bootcmd_rescue has always stock value, so that running |
| 450 | * run bootcmd_rescue |
| 451 | * always works correctly. |
| 452 | */ |
| 453 | env_set_default_vars(1, (char * const *)vars, 0); |
| 454 | |
Marek Behún | 859ada5 | 2021-06-07 16:34:47 +0200 | [diff] [blame] | 455 | if (read_reset_button()) { |
Pali Rohár | 905c3bf | 2021-06-14 16:45:58 +0200 | [diff] [blame] | 456 | const char * const vars[2] = { |
Marek Behún | 859ada5 | 2021-06-07 16:34:47 +0200 | [diff] [blame] | 457 | "bootcmd", |
Marek Behún | 859ada5 | 2021-06-07 16:34:47 +0200 | [diff] [blame] | 458 | "distro_bootcmd", |
| 459 | }; |
| 460 | |
| 461 | /* |
| 462 | * Set the above envs to their default values, in case the user |
| 463 | * managed to break them. |
| 464 | */ |
Pali Rohár | 905c3bf | 2021-06-14 16:45:58 +0200 | [diff] [blame] | 465 | env_set_default_vars(2, (char * const *)vars, 0); |
Marek Behún | 859ada5 | 2021-06-07 16:34:47 +0200 | [diff] [blame] | 466 | |
| 467 | /* Ensure bootcmd_rescue is used by distroboot */ |
| 468 | env_set("boot_targets", "rescue"); |
| 469 | |
Marek Behún | d1a3079 | 2021-06-07 16:34:48 +0200 | [diff] [blame] | 470 | /* start blinking PHY LEDs */ |
| 471 | mox_phy_leds_start_blinking(); |
| 472 | |
Marek Behún | 859ada5 | 2021-06-07 16:34:47 +0200 | [diff] [blame] | 473 | printf("RESET button was pressed, overwriting boot_targets!\n"); |
| 474 | } else { |
| 475 | /* |
| 476 | * In case the user somehow managed to save environment with |
| 477 | * boot_targets=rescue, reset boot_targets to default value. |
| 478 | * This could happen in subsequent commands if bootcmd_rescue |
| 479 | * failed. |
| 480 | */ |
| 481 | if (!strcmp(env_get("boot_targets"), "rescue")) { |
| 482 | const char * const vars[1] = { |
| 483 | "boot_targets", |
| 484 | }; |
| 485 | |
| 486 | env_set_default_vars(1, (char * const *)vars, 0); |
| 487 | } |
| 488 | } |
| 489 | } |
| 490 | |
Marek Behún | 84ee2cd | 2021-10-09 19:33:40 +0200 | [diff] [blame] | 491 | int show_board_info(void) |
Marek Behún | 35572c9 | 2018-12-17 16:10:08 +0100 | [diff] [blame] | 492 | { |
Marek Behún | 84ee2cd | 2021-10-09 19:33:40 +0200 | [diff] [blame] | 493 | int i, ret, board_version, ram_size, is_sd; |
Marek Behún | 35572c9 | 2018-12-17 16:10:08 +0100 | [diff] [blame] | 494 | const char *pub_key; |
Marek Behún | 84ee2cd | 2021-10-09 19:33:40 +0200 | [diff] [blame] | 495 | const u8 *topology; |
| 496 | u64 serial_number; |
| 497 | |
| 498 | printf("Model: CZ.NIC Turris Mox Board\n"); |
Marek Behún | 35572c9 | 2018-12-17 16:10:08 +0100 | [diff] [blame] | 499 | |
| 500 | ret = mbox_sp_get_board_info(&serial_number, NULL, NULL, &board_version, |
| 501 | &ram_size); |
Marek Behún | 84ee2cd | 2021-10-09 19:33:40 +0200 | [diff] [blame] | 502 | if (ret < 0) { |
| 503 | printf(" Cannot read board info: %i\n", ret); |
| 504 | } else { |
| 505 | printf(" Board version: %i\n", board_version); |
| 506 | printf(" RAM size: %i MiB\n", ram_size); |
| 507 | printf(" Serial Number: %016llX\n", serial_number); |
| 508 | } |
Marek Behún | 35572c9 | 2018-12-17 16:10:08 +0100 | [diff] [blame] | 509 | |
| 510 | pub_key = mox_sp_get_ecdsa_public_key(); |
| 511 | if (pub_key) |
| 512 | printf(" ECDSA Public Key: %s\n", pub_key); |
| 513 | else |
Marek Behún | 84ee2cd | 2021-10-09 19:33:40 +0200 | [diff] [blame] | 514 | printf(" Cannot read ECDSA Public Key\n"); |
Marek Behún | 35572c9 | 2018-12-17 16:10:08 +0100 | [diff] [blame] | 515 | |
Marek Behún | f68ed8b | 2018-12-17 16:10:01 +0100 | [diff] [blame] | 516 | ret = mox_get_topology(&topology, &module_count, &is_sd); |
Marek Behún | 84ee2cd | 2021-10-09 19:33:40 +0200 | [diff] [blame] | 517 | if (ret) |
Marek Behún | f68ed8b | 2018-12-17 16:10:01 +0100 | [diff] [blame] | 518 | printf("Cannot read module topology!\n"); |
Marek Behún | f68ed8b | 2018-12-17 16:10:01 +0100 | [diff] [blame] | 519 | |
Marek Behún | 35572c9 | 2018-12-17 16:10:08 +0100 | [diff] [blame] | 520 | printf(" SD/eMMC version: %s\n", is_sd ? "SD" : "eMMC"); |
| 521 | |
| 522 | if (module_count) |
| 523 | printf("Module Topology:\n"); |
| 524 | |
Marek Behún | f68ed8b | 2018-12-17 16:10:01 +0100 | [diff] [blame] | 525 | for (i = 0; i < module_count; ++i) { |
| 526 | switch (topology[i]) { |
| 527 | case MOX_MODULE_SFP: |
| 528 | printf("% 4i: SFP Module\n", i + 1); |
Marek Behún | f835bed | 2018-04-24 17:21:31 +0200 | [diff] [blame] | 529 | break; |
Marek Behún | f68ed8b | 2018-12-17 16:10:01 +0100 | [diff] [blame] | 530 | case MOX_MODULE_PCI: |
| 531 | printf("% 4i: Mini-PCIe Module\n", i + 1); |
Marek Behún | f835bed | 2018-04-24 17:21:31 +0200 | [diff] [blame] | 532 | break; |
Marek Behún | f68ed8b | 2018-12-17 16:10:01 +0100 | [diff] [blame] | 533 | case MOX_MODULE_TOPAZ: |
| 534 | printf("% 4i: Topaz Switch Module (4-port)\n", i + 1); |
| 535 | break; |
| 536 | case MOX_MODULE_PERIDOT: |
| 537 | printf("% 4i: Peridot Switch Module (8-port)\n", i + 1); |
| 538 | break; |
| 539 | case MOX_MODULE_USB3: |
| 540 | printf("% 4i: USB 3.0 Module (4 ports)\n", i + 1); |
| 541 | break; |
| 542 | case MOX_MODULE_PASSPCI: |
| 543 | printf("% 4i: Passthrough Mini-PCIe Module\n", i + 1); |
Marek Behún | f835bed | 2018-04-24 17:21:31 +0200 | [diff] [blame] | 544 | break; |
| 545 | default: |
Marek Behún | f68ed8b | 2018-12-17 16:10:01 +0100 | [diff] [blame] | 546 | printf("% 4i: unknown (ID %i)\n", i + 1, topology[i]); |
Marek Behún | f835bed | 2018-04-24 17:21:31 +0200 | [diff] [blame] | 547 | } |
| 548 | } |
Marek Behún | f835bed | 2018-04-24 17:21:31 +0200 | [diff] [blame] | 549 | |
Marek Behún | 84ee2cd | 2021-10-09 19:33:40 +0200 | [diff] [blame] | 550 | /* check if modules are connected in supported mode */ |
Marek Behún | 35b3513 | 2018-12-17 16:10:03 +0100 | [diff] [blame] | 551 | for (i = 0; i < module_count; ++i) { |
| 552 | switch (topology[i]) { |
| 553 | case MOX_MODULE_SFP: |
| 554 | if (sfp) { |
| 555 | printf("Error: Only one SFP module is supported!\n"); |
| 556 | } else if (topaz) { |
| 557 | printf("Error: SFP module cannot be connected after Topaz Switch module!\n"); |
| 558 | } else { |
| 559 | sfp_pos = i; |
| 560 | ++sfp; |
| 561 | } |
| 562 | break; |
| 563 | case MOX_MODULE_PCI: |
Marek Behún | 4529fce | 2020-04-08 12:02:05 +0200 | [diff] [blame] | 564 | if (pci) |
Marek Behún | 35b3513 | 2018-12-17 16:10:03 +0100 | [diff] [blame] | 565 | printf("Error: Only one Mini-PCIe module is supported!\n"); |
Marek Behún | 4529fce | 2020-04-08 12:02:05 +0200 | [diff] [blame] | 566 | else if (usb) |
Marek Behún | 35b3513 | 2018-12-17 16:10:03 +0100 | [diff] [blame] | 567 | 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] | 568 | else if (i && (i != 1 || !passpci)) |
Marek Behún | 35b3513 | 2018-12-17 16:10:03 +0100 | [diff] [blame] | 569 | 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] | 570 | else |
Marek Behún | 35b3513 | 2018-12-17 16:10:03 +0100 | [diff] [blame] | 571 | ++pci; |
Marek Behún | 35b3513 | 2018-12-17 16:10:03 +0100 | [diff] [blame] | 572 | break; |
| 573 | case MOX_MODULE_TOPAZ: |
Marek Behún | 4529fce | 2020-04-08 12:02:05 +0200 | [diff] [blame] | 574 | if (topaz) |
Marek Behún | 35b3513 | 2018-12-17 16:10:03 +0100 | [diff] [blame] | 575 | printf("Error: Only one Topaz module is supported!\n"); |
Marek Behún | 4529fce | 2020-04-08 12:02:05 +0200 | [diff] [blame] | 576 | else if (peridot >= 3) |
Marek Behún | 35b3513 | 2018-12-17 16:10:03 +0100 | [diff] [blame] | 577 | 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] | 578 | else |
Marek Behún | 35b3513 | 2018-12-17 16:10:03 +0100 | [diff] [blame] | 579 | ++topaz; |
Marek Behún | 35b3513 | 2018-12-17 16:10:03 +0100 | [diff] [blame] | 580 | break; |
| 581 | case MOX_MODULE_PERIDOT: |
| 582 | if (sfp || topaz) { |
| 583 | printf("Error: Peridot module must come before SFP or Topaz module!\n"); |
| 584 | } else if (peridot >= 3) { |
| 585 | printf("Error: At most three Peridot modules are supported!\n"); |
| 586 | } else { |
| 587 | peridot_pos[peridot] = i; |
| 588 | ++peridot; |
| 589 | } |
| 590 | break; |
| 591 | case MOX_MODULE_USB3: |
Marek Behún | 4529fce | 2020-04-08 12:02:05 +0200 | [diff] [blame] | 592 | if (pci) |
Marek Behún | 35b3513 | 2018-12-17 16:10:03 +0100 | [diff] [blame] | 593 | 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] | 594 | else if (usb) |
Marek Behún | 35b3513 | 2018-12-17 16:10:03 +0100 | [diff] [blame] | 595 | printf("Error: Only one USB 3.0 module is supported!\n"); |
Marek Behún | 4529fce | 2020-04-08 12:02:05 +0200 | [diff] [blame] | 596 | else if (i && (i != 1 || !passpci)) |
Marek Behún | 35b3513 | 2018-12-17 16:10:03 +0100 | [diff] [blame] | 597 | 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] | 598 | else |
Marek Behún | 35b3513 | 2018-12-17 16:10:03 +0100 | [diff] [blame] | 599 | ++usb; |
Marek Behún | 35b3513 | 2018-12-17 16:10:03 +0100 | [diff] [blame] | 600 | break; |
| 601 | case MOX_MODULE_PASSPCI: |
Marek Behún | 4529fce | 2020-04-08 12:02:05 +0200 | [diff] [blame] | 602 | if (passpci) |
Marek Behún | 35b3513 | 2018-12-17 16:10:03 +0100 | [diff] [blame] | 603 | printf("Error: Only one Passthrough Mini-PCIe module is supported!\n"); |
Marek Behún | 4529fce | 2020-04-08 12:02:05 +0200 | [diff] [blame] | 604 | else if (i != 0) |
Marek Behún | 35b3513 | 2018-12-17 16:10:03 +0100 | [diff] [blame] | 605 | 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] | 606 | else |
Marek Behún | 35b3513 | 2018-12-17 16:10:03 +0100 | [diff] [blame] | 607 | ++passpci; |
Marek Behún | 35b3513 | 2018-12-17 16:10:03 +0100 | [diff] [blame] | 608 | } |
| 609 | } |
| 610 | |
Marek Behún | 84ee2cd | 2021-10-09 19:33:40 +0200 | [diff] [blame] | 611 | return 0; |
| 612 | } |
Marek Behún | 35b3513 | 2018-12-17 16:10:03 +0100 | [diff] [blame] | 613 | |
Marek Behún | 84ee2cd | 2021-10-09 19:33:40 +0200 | [diff] [blame] | 614 | int last_stage_init(void) |
| 615 | { |
| 616 | struct gpio_desc reset_gpio = {}; |
| 617 | |
| 618 | /* configure modules */ |
Marek Behún | 35b3513 | 2018-12-17 16:10:03 +0100 | [diff] [blame] | 619 | if (get_reset_gpio(&reset_gpio) < 0) |
Marek Behún | 84d67ac | 2021-10-09 19:33:41 +0200 | [diff] [blame] | 620 | goto handle_reset_btn; |
Marek Behún | 35b3513 | 2018-12-17 16:10:03 +0100 | [diff] [blame] | 621 | |
| 622 | if (peridot > 0) { |
| 623 | if (configure_peridots(&reset_gpio) < 0) { |
| 624 | printf("Cannot configure Peridot modules!\n"); |
| 625 | peridot = 0; |
| 626 | } |
| 627 | } else { |
| 628 | dm_gpio_set_value(&reset_gpio, 1); |
| 629 | mdelay(50); |
| 630 | dm_gpio_set_value(&reset_gpio, 0); |
| 631 | mdelay(50); |
| 632 | } |
| 633 | |
Marek Behún | 84ee2cd | 2021-10-09 19:33:40 +0200 | [diff] [blame] | 634 | /* |
| 635 | * check if the addresses are set by reading Scratch & Misc register |
| 636 | * 0x70 of Peridot (and potentially Topaz) modules |
| 637 | */ |
Marek Behún | 35b3513 | 2018-12-17 16:10:03 +0100 | [diff] [blame] | 638 | if (peridot || topaz) { |
Marek Behún | 84ee2cd | 2021-10-09 19:33:40 +0200 | [diff] [blame] | 639 | struct mii_dev *bus; |
Pali Rohár | a524db0 | 2022-02-14 11:34:26 +0100 | [diff] [blame] | 640 | const char *node_name; |
| 641 | int node; |
Marek Behún | 35b3513 | 2018-12-17 16:10:03 +0100 | [diff] [blame] | 642 | |
Pali Rohár | a524db0 | 2022-02-14 11:34:26 +0100 | [diff] [blame] | 643 | node = fdt_path_offset(gd->fdt_blob, "ethernet0"); |
| 644 | node_name = (node >= 0) ? fdt_get_name(gd->fdt_blob, node, NULL) : NULL; |
| 645 | bus = node_name ? miiphy_get_dev_by_name(node_name) : NULL; |
Marek Behún | 35b3513 | 2018-12-17 16:10:03 +0100 | [diff] [blame] | 646 | if (!bus) { |
| 647 | printf("Cannot get MDIO bus device!\n"); |
| 648 | } else { |
Marek Behún | 84ee2cd | 2021-10-09 19:33:40 +0200 | [diff] [blame] | 649 | int i; |
| 650 | |
Marek Behún | 35b3513 | 2018-12-17 16:10:03 +0100 | [diff] [blame] | 651 | for (i = 0; i < peridot; ++i) |
| 652 | check_switch_address(bus, 0x10 + i); |
| 653 | |
| 654 | if (topaz) |
| 655 | check_switch_address(bus, 0x2); |
| 656 | |
| 657 | sw_blink_leds(bus, peridot, topaz); |
| 658 | } |
| 659 | } |
| 660 | |
Marek Behún | 84d67ac | 2021-10-09 19:33:41 +0200 | [diff] [blame] | 661 | handle_reset_btn: |
Marek Behún | 859ada5 | 2021-06-07 16:34:47 +0200 | [diff] [blame] | 662 | handle_reset_button(); |
| 663 | |
Marek Behún | f68ed8b | 2018-12-17 16:10:01 +0100 | [diff] [blame] | 664 | return 0; |
Marek Behún | f835bed | 2018-04-24 17:21:31 +0200 | [diff] [blame] | 665 | } |
Marek Behún | 4529fce | 2020-04-08 12:02:05 +0200 | [diff] [blame] | 666 | |
| 667 | #if defined(CONFIG_OF_BOARD_SETUP) |
| 668 | |
Marek Behún | d050eab | 2021-11-26 14:57:11 +0100 | [diff] [blame] | 669 | static bool is_topaz(int id) |
Marek Behún | 4529fce | 2020-04-08 12:02:05 +0200 | [diff] [blame] | 670 | { |
Marek Behún | d050eab | 2021-11-26 14:57:11 +0100 | [diff] [blame] | 671 | return topaz && id == peridot + topaz - 1; |
Marek Behún | 4529fce | 2020-04-08 12:02:05 +0200 | [diff] [blame] | 672 | } |
| 673 | |
Marek Behún | d050eab | 2021-11-26 14:57:11 +0100 | [diff] [blame] | 674 | static int switch_addr(int id) |
Marek Behún | 4529fce | 2020-04-08 12:02:05 +0200 | [diff] [blame] | 675 | { |
Marek Behún | d050eab | 2021-11-26 14:57:11 +0100 | [diff] [blame] | 676 | return is_topaz(id) ? 0x2 : 0x10 + id; |
Marek Behún | 4529fce | 2020-04-08 12:02:05 +0200 | [diff] [blame] | 677 | } |
| 678 | |
Marek Behún | d050eab | 2021-11-26 14:57:11 +0100 | [diff] [blame] | 679 | static int setup_switch(void *blob, int id) |
Marek Behún | 4529fce | 2020-04-08 12:02:05 +0200 | [diff] [blame] | 680 | { |
Marek Behún | d050eab | 2021-11-26 14:57:11 +0100 | [diff] [blame] | 681 | int res, addr, i, node; |
| 682 | char mdio_path[64]; |
Marek Behún | 4529fce | 2020-04-08 12:02:05 +0200 | [diff] [blame] | 683 | |
Marek Behún | d050eab | 2021-11-26 14:57:11 +0100 | [diff] [blame] | 684 | node = fdt_node_offset_by_compatible(blob, -1, "marvell,orion-mdio"); |
Marek Behún | 4529fce | 2020-04-08 12:02:05 +0200 | [diff] [blame] | 685 | if (node < 0) |
| 686 | return node; |
| 687 | |
Marek Behún | d050eab | 2021-11-26 14:57:11 +0100 | [diff] [blame] | 688 | res = fdt_get_path(blob, node, mdio_path, sizeof(mdio_path)); |
Marek Behún | 4529fce | 2020-04-08 12:02:05 +0200 | [diff] [blame] | 689 | if (res < 0) |
| 690 | return res; |
| 691 | |
Marek Behún | 4529fce | 2020-04-08 12:02:05 +0200 | [diff] [blame] | 692 | addr = switch_addr(id); |
| 693 | |
| 694 | /* first enable the switch by setting status = "okay" */ |
Marek Behún | d050eab | 2021-11-26 14:57:11 +0100 | [diff] [blame] | 695 | res = fdt_status_okay_by_pathf(blob, "%s/switch%i@%x", mdio_path, id, |
| 696 | addr); |
Marek Behún | 4529fce | 2020-04-08 12:02:05 +0200 | [diff] [blame] | 697 | if (res < 0) |
| 698 | return res; |
| 699 | |
| 700 | /* |
| 701 | * now if there are more switches or a SFP module coming after, |
| 702 | * enable corresponding ports |
| 703 | */ |
| 704 | if (id < peridot + topaz - 1) { |
Marek Behún | d050eab | 2021-11-26 14:57:11 +0100 | [diff] [blame] | 705 | res = fdt_status_okay_by_pathf(blob, |
| 706 | "%s/switch%i@%x/ports/port@a", |
| 707 | mdio_path, id, addr); |
Marek Behún | 4529fce | 2020-04-08 12:02:05 +0200 | [diff] [blame] | 708 | } else if (id == peridot - 1 && !topaz && sfp) { |
Marek Behún | d050eab | 2021-11-26 14:57:11 +0100 | [diff] [blame] | 709 | res = fdt_status_okay_by_pathf(blob, |
| 710 | "%s/switch%i@%x/ports/port-sfp@a", |
| 711 | mdio_path, id, addr); |
Marek Behún | 4529fce | 2020-04-08 12:02:05 +0200 | [diff] [blame] | 712 | } else { |
| 713 | res = 0; |
| 714 | } |
| 715 | if (res < 0) |
| 716 | return res; |
| 717 | |
| 718 | if (id >= peridot + topaz - 1) |
| 719 | return 0; |
| 720 | |
| 721 | /* finally change link property if needed */ |
Marek Behún | d050eab | 2021-11-26 14:57:11 +0100 | [diff] [blame] | 722 | node = fdt_node_offset_by_pathf(blob, "%s/switch%i@%x/ports/port@a", |
| 723 | mdio_path, id, addr); |
Marek Behún | 4529fce | 2020-04-08 12:02:05 +0200 | [diff] [blame] | 724 | if (node < 0) |
| 725 | return node; |
| 726 | |
| 727 | for (i = id + 1; i < peridot + topaz; ++i) { |
Marek Behún | d050eab | 2021-11-26 14:57:11 +0100 | [diff] [blame] | 728 | unsigned int phandle; |
| 729 | |
| 730 | phandle = fdt_create_phandle_by_pathf(blob, |
| 731 | "%s/switch%i@%x/ports/port@%x", |
| 732 | mdio_path, i, |
| 733 | switch_addr(i), |
| 734 | is_topaz(i) ? 5 : 9); |
| 735 | if (!phandle) |
| 736 | return -FDT_ERR_NOPHANDLES; |
Marek Behún | 4529fce | 2020-04-08 12:02:05 +0200 | [diff] [blame] | 737 | |
| 738 | if (i == id + 1) |
| 739 | res = fdt_setprop_u32(blob, node, "link", phandle); |
| 740 | else |
| 741 | res = fdt_appendprop_u32(blob, node, "link", phandle); |
| 742 | if (res < 0) |
| 743 | return res; |
| 744 | } |
| 745 | |
| 746 | return 0; |
| 747 | } |
| 748 | |
Masahiro Yamada | f7ed78b | 2020-06-26 15:13:33 +0900 | [diff] [blame] | 749 | int ft_board_setup(void *blob, struct bd_info *bd) |
Marek Behún | 4529fce | 2020-04-08 12:02:05 +0200 | [diff] [blame] | 750 | { |
Marek Behún | d050eab | 2021-11-26 14:57:11 +0100 | [diff] [blame] | 751 | int res; |
Marek Behún | 4529fce | 2020-04-08 12:02:05 +0200 | [diff] [blame] | 752 | |
| 753 | /* |
| 754 | * If MOX B (PCI), MOX F (USB) or MOX G (Passthrough PCI) modules are |
| 755 | * connected, enable the PCIe node. |
| 756 | */ |
| 757 | if (pci || usb || passpci) { |
Marek Behún | d050eab | 2021-11-26 14:57:11 +0100 | [diff] [blame] | 758 | res = fdt_status_okay_by_compatible(blob, |
| 759 | "marvell,armada-3700-pcie"); |
Marek Behún | 4529fce | 2020-04-08 12:02:05 +0200 | [diff] [blame] | 760 | if (res < 0) |
| 761 | return res; |
Marek Behún | 455ef5b | 2020-04-08 19:25:22 +0200 | [diff] [blame] | 762 | |
| 763 | /* Fix PCIe regions for devices with 4 GB RAM */ |
| 764 | res = a3700_fdt_fix_pcie_regions(blob); |
| 765 | if (res < 0) |
| 766 | return res; |
Marek Behún | 4529fce | 2020-04-08 12:02:05 +0200 | [diff] [blame] | 767 | } |
| 768 | |
| 769 | /* |
| 770 | * If MOX C (Topaz switch) and/or MOX E (Peridot switch) are connected, |
| 771 | * enable the eth1 node and setup the switches. |
| 772 | */ |
| 773 | if (peridot || topaz) { |
| 774 | int i; |
| 775 | |
Marek Behún | d050eab | 2021-11-26 14:57:11 +0100 | [diff] [blame] | 776 | res = fdt_status_okay_by_alias(blob, "ethernet1"); |
Marek Behún | 4529fce | 2020-04-08 12:02:05 +0200 | [diff] [blame] | 777 | if (res < 0) |
| 778 | return res; |
| 779 | |
| 780 | for (i = 0; i < peridot + topaz; ++i) { |
| 781 | res = setup_switch(blob, i); |
| 782 | if (res < 0) |
| 783 | return res; |
| 784 | } |
| 785 | } |
| 786 | |
| 787 | /* |
| 788 | * If MOX D (SFP cage module) is connected, enable the SFP node and eth1 |
| 789 | * node. If there is no Peridot switch between MOX A and MOX D, add link |
| 790 | * to the SFP node to eth1 node. |
| 791 | * Also enable and configure SFP GPIO controller node. |
| 792 | */ |
| 793 | if (sfp) { |
Marek Behún | d050eab | 2021-11-26 14:57:11 +0100 | [diff] [blame] | 794 | int node; |
| 795 | |
| 796 | res = fdt_status_okay_by_compatible(blob, "sff,sfp"); |
Marek Behún | 4529fce | 2020-04-08 12:02:05 +0200 | [diff] [blame] | 797 | if (res < 0) |
| 798 | return res; |
| 799 | |
Marek Behún | d050eab | 2021-11-26 14:57:11 +0100 | [diff] [blame] | 800 | res = fdt_status_okay_by_alias(blob, "ethernet1"); |
Marek Behún | 4529fce | 2020-04-08 12:02:05 +0200 | [diff] [blame] | 801 | if (res < 0) |
| 802 | return res; |
| 803 | |
| 804 | if (!peridot) { |
Marek Behún | d050eab | 2021-11-26 14:57:11 +0100 | [diff] [blame] | 805 | unsigned int phandle; |
Marek Behún | 4529fce | 2020-04-08 12:02:05 +0200 | [diff] [blame] | 806 | |
Marek Behún | d050eab | 2021-11-26 14:57:11 +0100 | [diff] [blame] | 807 | phandle = fdt_create_phandle_by_compatible(blob, |
| 808 | "sff,sfp"); |
| 809 | if (!phandle) |
| 810 | return -FDT_ERR_NOPHANDLES; |
| 811 | |
| 812 | node = fdt_path_offset(blob, "ethernet1"); |
Marek Behún | 4529fce | 2020-04-08 12:02:05 +0200 | [diff] [blame] | 813 | if (node < 0) |
| 814 | return node; |
| 815 | |
| 816 | res = fdt_setprop_u32(blob, node, "sfp", phandle); |
| 817 | if (res < 0) |
| 818 | return res; |
| 819 | |
| 820 | res = fdt_setprop_string(blob, node, "phy-mode", |
| 821 | "sgmii"); |
| 822 | if (res < 0) |
| 823 | return res; |
| 824 | } |
| 825 | |
Marek Behún | d050eab | 2021-11-26 14:57:11 +0100 | [diff] [blame] | 826 | res = fdt_status_okay_by_compatible(blob, "cznic,moxtet-gpio"); |
Marek Behún | 4529fce | 2020-04-08 12:02:05 +0200 | [diff] [blame] | 827 | if (res < 0) |
| 828 | return res; |
| 829 | |
| 830 | if (sfp_pos) { |
| 831 | char newname[16]; |
| 832 | |
| 833 | /* moxtet-sfp is on non-zero position, change default */ |
Marek Behún | d050eab | 2021-11-26 14:57:11 +0100 | [diff] [blame] | 834 | node = fdt_node_offset_by_compatible(blob, -1, |
| 835 | "cznic,moxtet-gpio"); |
Marek Behún | 4529fce | 2020-04-08 12:02:05 +0200 | [diff] [blame] | 836 | if (node < 0) |
| 837 | return node; |
| 838 | |
| 839 | res = fdt_setprop_u32(blob, node, "reg", sfp_pos); |
| 840 | if (res < 0) |
| 841 | return res; |
| 842 | |
| 843 | sprintf(newname, "gpio@%x", sfp_pos); |
| 844 | |
| 845 | res = fdt_set_name(blob, node, newname); |
| 846 | if (res < 0) |
| 847 | return res; |
| 848 | } |
| 849 | } |
| 850 | |
| 851 | fdt_fixup_ethernet(blob); |
| 852 | |
| 853 | /* Finally remove disabled nodes, as per Rob Herring's request. */ |
Marek Behún | ac4d9ff | 2021-11-26 14:57:15 +0100 | [diff] [blame] | 854 | fdt_delete_disabled_nodes(blob); |
Marek Behún | 4529fce | 2020-04-08 12:02:05 +0200 | [diff] [blame] | 855 | |
| 856 | return 0; |
| 857 | } |
| 858 | |
| 859 | #endif |