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