Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Marek Behún | 09e16b8 | 2017-06-09 19:28:45 +0200 | [diff] [blame] | 2 | /* |
Marek Behún | d63726e | 2022-06-01 17:17:06 +0200 | [diff] [blame] | 3 | * Copyright (C) 2017 Marek Behún <kabel@kernel.org> |
Marek Behún | 09e16b8 | 2017-06-09 19:28:45 +0200 | [diff] [blame] | 4 | * Copyright (C) 2016 Tomas Hlavacek <tomas.hlavacek@nic.cz> |
| 5 | * |
| 6 | * Derived from the code for |
| 7 | * Marvell/db-88f6820-gp by Stefan Roese <sr@denx.de> |
Marek Behún | 09e16b8 | 2017-06-09 19:28:45 +0200 | [diff] [blame] | 8 | */ |
| 9 | |
Tom Rini | dec7ea0 | 2024-05-20 13:35:03 -0600 | [diff] [blame] | 10 | #include <config.h> |
Simon Glass | 07dc93c | 2019-08-01 09:46:47 -0600 | [diff] [blame] | 11 | #include <env.h> |
Marek Behún | 09e16b8 | 2017-06-09 19:28:45 +0200 | [diff] [blame] | 12 | #include <i2c.h> |
Simon Glass | a7b5130 | 2019-11-14 12:57:46 -0700 | [diff] [blame] | 13 | #include <init.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 14 | #include <log.h> |
Marek Behún | 09e16b8 | 2017-06-09 19:28:45 +0200 | [diff] [blame] | 15 | #include <miiphy.h> |
Marek Behún | 91ef59c | 2021-07-15 19:21:02 +0200 | [diff] [blame] | 16 | #include <mtd.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 17 | #include <asm/global_data.h> |
Marek Behún | 09e16b8 | 2017-06-09 19:28:45 +0200 | [diff] [blame] | 18 | #include <asm/io.h> |
| 19 | #include <asm/arch/cpu.h> |
| 20 | #include <asm/arch/soc.h> |
Marek Behún | c2d19d0 | 2024-04-04 09:50:54 +0200 | [diff] [blame] | 21 | #include <asm/unaligned.h> |
Marek Behún | 09e16b8 | 2017-06-09 19:28:45 +0200 | [diff] [blame] | 22 | #include <dm/uclass.h> |
Pali Rohár | 1e0a975 | 2022-07-29 13:29:07 +0200 | [diff] [blame] | 23 | #include <dt-bindings/gpio/gpio.h> |
Marek Behún | 09e16b8 | 2017-06-09 19:28:45 +0200 | [diff] [blame] | 24 | #include <fdt_support.h> |
Pali Rohár | e16cc98 | 2022-08-10 11:00:25 +0200 | [diff] [blame] | 25 | #include <hexdump.h> |
Marek Behún | 09e16b8 | 2017-06-09 19:28:45 +0200 | [diff] [blame] | 26 | #include <time.h> |
Marek Behún | bb42a5b | 2024-04-04 09:50:51 +0200 | [diff] [blame] | 27 | #include <turris-omnia-mcu-interface.h> |
Simon Glass | 4dcacfc | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 28 | #include <linux/bitops.h> |
Marek Behún | c2d19d0 | 2024-04-04 09:50:54 +0200 | [diff] [blame] | 29 | #include <linux/bitrev.h> |
Pali Rohár | 1e0a975 | 2022-07-29 13:29:07 +0200 | [diff] [blame] | 30 | #include <linux/delay.h> |
Simon Glass | 48b6c6b | 2019-11-14 12:57:16 -0700 | [diff] [blame] | 31 | #include <u-boot/crc.h> |
Marek Behún | 09e16b8 | 2017-06-09 19:28:45 +0200 | [diff] [blame] | 32 | |
Chris Packham | 1a07d21 | 2018-05-10 13:28:29 +1200 | [diff] [blame] | 33 | #include "../drivers/ddr/marvell/a38x/ddr3_init.h" |
Marek Behún | 09e16b8 | 2017-06-09 19:28:45 +0200 | [diff] [blame] | 34 | #include <../serdes/a38x/high_speed_env_spec.h> |
Pali Rohár | 0387f7f | 2022-04-08 16:30:12 +0200 | [diff] [blame] | 35 | #include "../turris_atsha_otp.h" |
Marek Behún | c2d19d0 | 2024-04-04 09:50:54 +0200 | [diff] [blame] | 36 | #include "../turris_common.h" |
Marek Behún | 09e16b8 | 2017-06-09 19:28:45 +0200 | [diff] [blame] | 37 | |
| 38 | DECLARE_GLOBAL_DATA_PTR; |
| 39 | |
Marek Behún | ba53b6b | 2019-05-02 16:53:30 +0200 | [diff] [blame] | 40 | #define OMNIA_I2C_BUS_NAME "i2c@11000->i2cmux@70->i2c@0" |
| 41 | |
| 42 | #define OMNIA_I2C_MCU_CHIP_ADDR 0x2a |
| 43 | #define OMNIA_I2C_MCU_CHIP_LEN 1 |
| 44 | |
| 45 | #define OMNIA_I2C_EEPROM_CHIP_ADDR 0x54 |
| 46 | #define OMNIA_I2C_EEPROM_CHIP_LEN 2 |
Marek Behún | 09e16b8 | 2017-06-09 19:28:45 +0200 | [diff] [blame] | 47 | #define OMNIA_I2C_EEPROM_MAGIC 0x0341a034 |
| 48 | |
Pali Rohár | 30e398d | 2022-04-29 13:53:25 +0200 | [diff] [blame] | 49 | #define A385_SYS_RSTOUT_MASK MVEBU_REGISTER(0x18260) |
| 50 | #define A385_SYS_RSTOUT_MASK_WD BIT(10) |
Pali Rohár | 7fcda0c | 2021-11-09 17:14:02 +0100 | [diff] [blame] | 51 | |
| 52 | #define A385_WDT_GLOBAL_CTRL MVEBU_REGISTER(0x20300) |
| 53 | #define A385_WDT_GLOBAL_RATIO_MASK GENMASK(18, 16) |
| 54 | #define A385_WDT_GLOBAL_RATIO_SHIFT 16 |
| 55 | #define A385_WDT_GLOBAL_25MHZ BIT(10) |
| 56 | #define A385_WDT_GLOBAL_ENABLE BIT(8) |
| 57 | |
| 58 | #define A385_WDT_GLOBAL_STATUS MVEBU_REGISTER(0x20304) |
| 59 | #define A385_WDT_GLOBAL_EXPIRED BIT(31) |
| 60 | |
| 61 | #define A385_WDT_DURATION MVEBU_REGISTER(0x20334) |
| 62 | |
| 63 | #define A385_WD_RSTOUT_UNMASK MVEBU_REGISTER(0x20704) |
| 64 | #define A385_WD_RSTOUT_UNMASK_GLOBAL BIT(8) |
| 65 | |
Marek Behún | 09e16b8 | 2017-06-09 19:28:45 +0200 | [diff] [blame] | 66 | /* |
| 67 | * Those values and defines are taken from the Marvell U-Boot version |
| 68 | * "u-boot-2013.01-2014_T3.0" |
| 69 | */ |
| 70 | #define OMNIA_GPP_OUT_ENA_LOW \ |
| 71 | (~(BIT(1) | BIT(4) | BIT(6) | BIT(7) | BIT(8) | BIT(9) | \ |
| 72 | BIT(10) | BIT(11) | BIT(19) | BIT(22) | BIT(23) | BIT(25) | \ |
| 73 | BIT(26) | BIT(27) | BIT(29) | BIT(30) | BIT(31))) |
| 74 | #define OMNIA_GPP_OUT_ENA_MID \ |
| 75 | (~(BIT(0) | BIT(1) | BIT(2) | BIT(3) | BIT(4) | BIT(15) | \ |
| 76 | BIT(16) | BIT(17) | BIT(18))) |
| 77 | |
| 78 | #define OMNIA_GPP_OUT_VAL_LOW 0x0 |
| 79 | #define OMNIA_GPP_OUT_VAL_MID 0x0 |
| 80 | #define OMNIA_GPP_POL_LOW 0x0 |
| 81 | #define OMNIA_GPP_POL_MID 0x0 |
| 82 | |
Pali Rohár | 3c4dd98 | 2022-03-02 12:47:54 +0100 | [diff] [blame] | 83 | static struct serdes_map board_serdes_map[] = { |
Marek Behún | 09e16b8 | 2017-06-09 19:28:45 +0200 | [diff] [blame] | 84 | {PEX0, SERDES_SPEED_5_GBPS, PEX_ROOT_COMPLEX_X1, 0, 0}, |
| 85 | {USB3_HOST0, SERDES_SPEED_5_GBPS, SERDES_DEFAULT_MODE, 0, 0}, |
| 86 | {PEX1, SERDES_SPEED_5_GBPS, PEX_ROOT_COMPLEX_X1, 0, 0}, |
| 87 | {USB3_HOST1, SERDES_SPEED_5_GBPS, SERDES_DEFAULT_MODE, 0, 0}, |
| 88 | {PEX2, SERDES_SPEED_5_GBPS, PEX_ROOT_COMPLEX_X1, 0, 0}, |
| 89 | {SGMII2, SERDES_SPEED_1_25_GBPS, SERDES_DEFAULT_MODE, 0, 0} |
| 90 | }; |
| 91 | |
Marek Behún | ba53b6b | 2019-05-02 16:53:30 +0200 | [diff] [blame] | 92 | static struct udevice *omnia_get_i2c_chip(const char *name, uint addr, |
| 93 | uint offset_len) |
Marek Behún | 09e16b8 | 2017-06-09 19:28:45 +0200 | [diff] [blame] | 94 | { |
| 95 | struct udevice *bus, *dev; |
Marek Behún | ba53b6b | 2019-05-02 16:53:30 +0200 | [diff] [blame] | 96 | int ret; |
Marek Behún | 09e16b8 | 2017-06-09 19:28:45 +0200 | [diff] [blame] | 97 | |
Marek Behún | ba53b6b | 2019-05-02 16:53:30 +0200 | [diff] [blame] | 98 | ret = uclass_get_device_by_name(UCLASS_I2C, OMNIA_I2C_BUS_NAME, &bus); |
| 99 | if (ret) { |
| 100 | printf("Cannot get I2C bus %s: uclass_get_device_by_name failed: %i\n", |
| 101 | OMNIA_I2C_BUS_NAME, ret); |
| 102 | return NULL; |
Marek Behún | 09e16b8 | 2017-06-09 19:28:45 +0200 | [diff] [blame] | 103 | } |
| 104 | |
Marek Behún | ba53b6b | 2019-05-02 16:53:30 +0200 | [diff] [blame] | 105 | ret = i2c_get_chip(bus, addr, offset_len, &dev); |
Marek Behún | 09e16b8 | 2017-06-09 19:28:45 +0200 | [diff] [blame] | 106 | if (ret) { |
Marek Behún | ba53b6b | 2019-05-02 16:53:30 +0200 | [diff] [blame] | 107 | printf("Cannot get %s I2C chip: i2c_get_chip failed: %i\n", |
| 108 | name, ret); |
| 109 | return NULL; |
Marek Behún | 09e16b8 | 2017-06-09 19:28:45 +0200 | [diff] [blame] | 110 | } |
| 111 | |
Marek Behún | ba53b6b | 2019-05-02 16:53:30 +0200 | [diff] [blame] | 112 | return dev; |
| 113 | } |
Marek Behún | d0b374d | 2017-08-04 15:28:25 +0200 | [diff] [blame] | 114 | |
Marek Behún | ba53b6b | 2019-05-02 16:53:30 +0200 | [diff] [blame] | 115 | static int omnia_mcu_read(u8 cmd, void *buf, int len) |
| 116 | { |
| 117 | struct udevice *chip; |
| 118 | |
| 119 | chip = omnia_get_i2c_chip("MCU", OMNIA_I2C_MCU_CHIP_ADDR, |
| 120 | OMNIA_I2C_MCU_CHIP_LEN); |
| 121 | if (!chip) |
| 122 | return -ENODEV; |
| 123 | |
| 124 | return dm_i2c_read(chip, cmd, buf, len); |
| 125 | } |
| 126 | |
Marek Behún | ba53b6b | 2019-05-02 16:53:30 +0200 | [diff] [blame] | 127 | static int omnia_mcu_write(u8 cmd, const void *buf, int len) |
| 128 | { |
| 129 | struct udevice *chip; |
| 130 | |
| 131 | chip = omnia_get_i2c_chip("MCU", OMNIA_I2C_MCU_CHIP_ADDR, |
| 132 | OMNIA_I2C_MCU_CHIP_LEN); |
| 133 | if (!chip) |
| 134 | return -ENODEV; |
| 135 | |
| 136 | return dm_i2c_write(chip, cmd, buf, len); |
| 137 | } |
| 138 | |
Marek Behún | 8b52b8c | 2024-04-04 09:50:53 +0200 | [diff] [blame] | 139 | static int omnia_mcu_get_sts_and_features(u16 *psts, u32 *pfeatures) |
| 140 | { |
| 141 | u16 sts, feat16; |
| 142 | int ret; |
| 143 | |
| 144 | ret = omnia_mcu_read(CMD_GET_STATUS_WORD, &sts, sizeof(sts)); |
| 145 | if (ret) |
| 146 | return ret; |
| 147 | |
| 148 | if (psts) |
| 149 | *psts = sts; |
| 150 | |
| 151 | if (!pfeatures) |
| 152 | return 0; |
| 153 | |
| 154 | if (sts & STS_FEATURES_SUPPORTED) { |
| 155 | /* try read 32-bit features */ |
| 156 | ret = omnia_mcu_read(CMD_GET_FEATURES, pfeatures, |
| 157 | sizeof(*pfeatures)); |
| 158 | if (ret) { |
| 159 | /* try read 16-bit features */ |
| 160 | ret = omnia_mcu_read(CMD_GET_FEATURES, &feat16, |
| 161 | sizeof(&feat16)); |
| 162 | if (ret) |
| 163 | return ret; |
| 164 | |
| 165 | *pfeatures = feat16; |
| 166 | } else { |
| 167 | if (*pfeatures & FEAT_FROM_BIT_16_INVALID) |
| 168 | *pfeatures &= GENMASK(15, 0); |
| 169 | } |
| 170 | } else { |
| 171 | *pfeatures = 0; |
| 172 | } |
| 173 | |
| 174 | return 0; |
| 175 | } |
| 176 | |
| 177 | static int omnia_mcu_get_sts(u16 *sts) |
| 178 | { |
| 179 | return omnia_mcu_get_sts_and_features(sts, NULL); |
| 180 | } |
| 181 | |
| 182 | static bool omnia_mcu_has_feature(u32 feature) |
| 183 | { |
| 184 | u32 features; |
| 185 | |
| 186 | if (omnia_mcu_get_sts_and_features(NULL, &features)) |
| 187 | return false; |
| 188 | |
| 189 | return feature & features; |
| 190 | } |
| 191 | |
Marek Behún | c2d19d0 | 2024-04-04 09:50:54 +0200 | [diff] [blame] | 192 | static u32 omnia_mcu_crc32(const void *p, size_t len) |
| 193 | { |
| 194 | u32 val, crc = 0; |
| 195 | |
| 196 | compiletime_assert(!(len % 4), "length has to be a multiple of 4"); |
| 197 | |
| 198 | while (len) { |
| 199 | val = bitrev32(get_unaligned_le32(p)); |
| 200 | crc = crc32(crc, (void *)&val, 4); |
| 201 | p += 4; |
| 202 | len -= 4; |
| 203 | } |
| 204 | |
| 205 | return ~bitrev32(crc); |
| 206 | } |
| 207 | |
| 208 | /* Can only be called after relocation, since it needs cleared BSS */ |
| 209 | static int omnia_mcu_board_info(char *serial, u8 *mac, char *version) |
| 210 | { |
| 211 | static u8 reply[17]; |
| 212 | static bool cached; |
| 213 | |
| 214 | if (!cached) { |
| 215 | u8 csum; |
| 216 | int ret; |
| 217 | |
| 218 | ret = omnia_mcu_read(CMD_BOARD_INFO_GET, reply, sizeof(reply)); |
| 219 | if (ret) |
| 220 | return ret; |
| 221 | |
| 222 | if (reply[0] != 16) |
| 223 | return -EBADMSG; |
| 224 | |
| 225 | csum = reply[16]; |
| 226 | reply[16] = 0; |
| 227 | |
| 228 | if ((omnia_mcu_crc32(&reply[1], 16) & 0xff) != csum) |
| 229 | return -EBADMSG; |
| 230 | |
| 231 | cached = true; |
| 232 | } |
| 233 | |
| 234 | if (serial) { |
| 235 | const char *serial_env; |
| 236 | |
| 237 | serial_env = env_get("serial#"); |
| 238 | if (serial_env && strlen(serial_env) == 16) { |
| 239 | strcpy(serial, serial_env); |
| 240 | } else { |
| 241 | sprintf(serial, "%016llX", |
| 242 | get_unaligned_le64(&reply[1])); |
| 243 | env_set("serial#", serial); |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | if (mac) |
| 248 | memcpy(mac, &reply[9], ETH_ALEN); |
| 249 | |
| 250 | if (version) |
| 251 | sprintf(version, "%u", reply[15]); |
| 252 | |
| 253 | return 0; |
| 254 | } |
| 255 | |
Marek Behún | 087b235 | 2024-04-04 09:50:55 +0200 | [diff] [blame] | 256 | static int omnia_mcu_get_board_public_key(char pub_key[static 67]) |
| 257 | { |
| 258 | u8 reply[34]; |
| 259 | int ret; |
| 260 | |
| 261 | ret = omnia_mcu_read(CMD_CRYPTO_GET_PUBLIC_KEY, reply, sizeof(reply)); |
| 262 | if (ret) |
| 263 | return ret; |
| 264 | |
| 265 | if (reply[0] != 33) |
| 266 | return -EBADMSG; |
| 267 | |
| 268 | bin2hex(pub_key, &reply[1], 33); |
| 269 | pub_key[66] = '\0'; |
| 270 | |
| 271 | return 0; |
| 272 | } |
| 273 | |
Pali Rohár | 7fcda0c | 2021-11-09 17:14:02 +0100 | [diff] [blame] | 274 | static void enable_a385_watchdog(unsigned int timeout_minutes) |
| 275 | { |
| 276 | struct sar_freq_modes sar_freq; |
| 277 | u32 watchdog_freq; |
| 278 | |
| 279 | printf("Enabling A385 watchdog with %u minutes timeout...\n", |
| 280 | timeout_minutes); |
| 281 | |
| 282 | /* |
| 283 | * Use NBCLK clock (a.k.a. L2 clock) as watchdog input clock with |
| 284 | * its maximal ratio 7 instead of default fixed 25 MHz clock. |
| 285 | * It allows to set watchdog duration up to the 22 minutes. |
| 286 | */ |
| 287 | clrsetbits_32(A385_WDT_GLOBAL_CTRL, |
| 288 | A385_WDT_GLOBAL_25MHZ | A385_WDT_GLOBAL_RATIO_MASK, |
| 289 | 7 << A385_WDT_GLOBAL_RATIO_SHIFT); |
| 290 | |
| 291 | /* |
| 292 | * Calculate watchdog clock frequency. It is defined by formula: |
| 293 | * freq = NBCLK / 2 / (2 ^ ratio) |
| 294 | * We set ratio to the maximal possible value 7. |
| 295 | */ |
| 296 | get_sar_freq(&sar_freq); |
| 297 | watchdog_freq = sar_freq.nb_clk * 1000000 / 2 / (1 << 7); |
| 298 | |
| 299 | /* Set watchdog duration */ |
| 300 | writel(timeout_minutes * 60 * watchdog_freq, A385_WDT_DURATION); |
| 301 | |
| 302 | /* Clear the watchdog expiration bit */ |
| 303 | clrbits_32(A385_WDT_GLOBAL_STATUS, A385_WDT_GLOBAL_EXPIRED); |
| 304 | |
| 305 | /* Enable watchdog timer */ |
| 306 | setbits_32(A385_WDT_GLOBAL_CTRL, A385_WDT_GLOBAL_ENABLE); |
| 307 | |
| 308 | /* Enable reset on watchdog */ |
| 309 | setbits_32(A385_WD_RSTOUT_UNMASK, A385_WD_RSTOUT_UNMASK_GLOBAL); |
| 310 | |
| 311 | /* Unmask reset for watchdog */ |
Pali Rohár | 30e398d | 2022-04-29 13:53:25 +0200 | [diff] [blame] | 312 | clrbits_32(A385_SYS_RSTOUT_MASK, A385_SYS_RSTOUT_MASK_WD); |
Pali Rohár | 7fcda0c | 2021-11-09 17:14:02 +0100 | [diff] [blame] | 313 | } |
| 314 | |
Marek Behún | ba53b6b | 2019-05-02 16:53:30 +0200 | [diff] [blame] | 315 | static bool disable_mcu_watchdog(void) |
| 316 | { |
| 317 | int ret; |
| 318 | |
| 319 | puts("Disabling MCU watchdog... "); |
| 320 | |
Marek Behún | bb42a5b | 2024-04-04 09:50:51 +0200 | [diff] [blame] | 321 | ret = omnia_mcu_write(CMD_SET_WATCHDOG_STATE, "\x00", 1); |
Marek Behún | ba53b6b | 2019-05-02 16:53:30 +0200 | [diff] [blame] | 322 | if (ret) { |
| 323 | printf("omnia_mcu_write failed: %i\n", ret); |
Marek Behún | 09e16b8 | 2017-06-09 19:28:45 +0200 | [diff] [blame] | 324 | return false; |
| 325 | } |
| 326 | |
Marek Behún | ba53b6b | 2019-05-02 16:53:30 +0200 | [diff] [blame] | 327 | puts("disabled\n"); |
| 328 | |
| 329 | return true; |
| 330 | } |
Marek Behún | ba53b6b | 2019-05-02 16:53:30 +0200 | [diff] [blame] | 331 | |
Pali Rohár | f8f305b | 2022-03-02 12:47:55 +0100 | [diff] [blame] | 332 | static bool omnia_detect_sata(const char *msata_slot) |
Marek Behún | ba53b6b | 2019-05-02 16:53:30 +0200 | [diff] [blame] | 333 | { |
| 334 | int ret; |
Marek Behún | 8b52b8c | 2024-04-04 09:50:53 +0200 | [diff] [blame] | 335 | u16 sts; |
Marek Behún | ba53b6b | 2019-05-02 16:53:30 +0200 | [diff] [blame] | 336 | |
| 337 | puts("MiniPCIe/mSATA card detection... "); |
| 338 | |
Pali Rohár | f8f305b | 2022-03-02 12:47:55 +0100 | [diff] [blame] | 339 | if (msata_slot) { |
| 340 | if (strcmp(msata_slot, "pcie") == 0) { |
| 341 | puts("forced to MiniPCIe via env\n"); |
| 342 | return false; |
| 343 | } else if (strcmp(msata_slot, "sata") == 0) { |
| 344 | puts("forced to mSATA via env\n"); |
| 345 | return true; |
| 346 | } else if (strcmp(msata_slot, "auto") != 0) { |
| 347 | printf("unsupported env value '%s', fallback to... ", msata_slot); |
| 348 | } |
| 349 | } |
| 350 | |
Marek Behún | 8b52b8c | 2024-04-04 09:50:53 +0200 | [diff] [blame] | 351 | ret = omnia_mcu_get_sts(&sts); |
Marek Behún | ba53b6b | 2019-05-02 16:53:30 +0200 | [diff] [blame] | 352 | if (ret) { |
| 353 | printf("omnia_mcu_read failed: %i, defaulting to MiniPCIe card\n", |
| 354 | ret); |
Marek Behún | 09e16b8 | 2017-06-09 19:28:45 +0200 | [diff] [blame] | 355 | return false; |
| 356 | } |
| 357 | |
Marek Behún | 8b52b8c | 2024-04-04 09:50:53 +0200 | [diff] [blame] | 358 | if (!(sts & STS_CARD_DET)) { |
Marek Behún | ba53b6b | 2019-05-02 16:53:30 +0200 | [diff] [blame] | 359 | puts("none\n"); |
Marek Behún | 09e16b8 | 2017-06-09 19:28:45 +0200 | [diff] [blame] | 360 | return false; |
| 361 | } |
Marek Behún | ba53b6b | 2019-05-02 16:53:30 +0200 | [diff] [blame] | 362 | |
Marek Behún | 8b52b8c | 2024-04-04 09:50:53 +0200 | [diff] [blame] | 363 | if (sts & STS_MSATA_IND) |
Marek Behún | ba53b6b | 2019-05-02 16:53:30 +0200 | [diff] [blame] | 364 | puts("mSATA\n"); |
| 365 | else |
| 366 | puts("MiniPCIe\n"); |
| 367 | |
Marek Behún | 8b52b8c | 2024-04-04 09:50:53 +0200 | [diff] [blame] | 368 | return sts & STS_MSATA_IND; |
Marek Behún | 09e16b8 | 2017-06-09 19:28:45 +0200 | [diff] [blame] | 369 | } |
| 370 | |
Pali Rohár | 93a89c5 | 2022-03-02 12:47:58 +0100 | [diff] [blame] | 371 | static bool omnia_detect_wwan_usb3(const char *wwan_slot) |
| 372 | { |
| 373 | puts("WWAN slot configuration... "); |
| 374 | |
| 375 | if (wwan_slot && strcmp(wwan_slot, "usb3") == 0) { |
| 376 | puts("USB3.0\n"); |
| 377 | return true; |
| 378 | } |
| 379 | |
| 380 | if (wwan_slot && strcmp(wwan_slot, "pcie") != 0) |
| 381 | printf("unsupported env value '%s', fallback to... ", wwan_slot); |
| 382 | |
| 383 | puts("PCIe+USB2.0\n"); |
| 384 | return false; |
| 385 | } |
| 386 | |
Marek Behún | 09e16b8 | 2017-06-09 19:28:45 +0200 | [diff] [blame] | 387 | int hws_board_topology_load(struct serdes_map **serdes_map_array, u8 *count) |
| 388 | { |
Pali Rohár | f8f305b | 2022-03-02 12:47:55 +0100 | [diff] [blame] | 389 | #ifdef CONFIG_SPL_ENV_SUPPORT |
| 390 | /* Do not use env_load() as malloc() pool is too small at this stage */ |
| 391 | bool has_env = (env_init() == 0); |
| 392 | #endif |
| 393 | const char *env_value = NULL; |
| 394 | |
| 395 | #ifdef CONFIG_SPL_ENV_SUPPORT |
| 396 | /* beware that env_get() returns static allocated memory */ |
| 397 | env_value = has_env ? env_get("omnia_msata_slot") : NULL; |
| 398 | #endif |
| 399 | |
| 400 | if (omnia_detect_sata(env_value)) { |
Pali Rohár | 3c4dd98 | 2022-03-02 12:47:54 +0100 | [diff] [blame] | 401 | /* Change SerDes for first mPCIe port (mSATA) from PCIe to SATA */ |
| 402 | board_serdes_map[0].serdes_type = SATA0; |
| 403 | board_serdes_map[0].serdes_speed = SERDES_SPEED_6_GBPS; |
| 404 | board_serdes_map[0].serdes_mode = SERDES_DEFAULT_MODE; |
Marek Behún | 09e16b8 | 2017-06-09 19:28:45 +0200 | [diff] [blame] | 405 | } |
| 406 | |
Pali Rohár | 93a89c5 | 2022-03-02 12:47:58 +0100 | [diff] [blame] | 407 | #ifdef CONFIG_SPL_ENV_SUPPORT |
| 408 | /* beware that env_get() returns static allocated memory */ |
| 409 | env_value = has_env ? env_get("omnia_wwan_slot") : NULL; |
| 410 | #endif |
| 411 | |
| 412 | if (omnia_detect_wwan_usb3(env_value)) { |
| 413 | /* Disable SerDes for USB 3.0 pins on the front USB-A port */ |
| 414 | board_serdes_map[1].serdes_type = DEFAULT_SERDES; |
| 415 | /* Change SerDes for third mPCIe port (WWAN) from PCIe to USB 3.0 */ |
| 416 | board_serdes_map[4].serdes_type = USB3_HOST0; |
| 417 | board_serdes_map[4].serdes_speed = SERDES_SPEED_5_GBPS; |
| 418 | board_serdes_map[4].serdes_mode = SERDES_DEFAULT_MODE; |
| 419 | } |
| 420 | |
Pali Rohár | 3c4dd98 | 2022-03-02 12:47:54 +0100 | [diff] [blame] | 421 | *serdes_map_array = board_serdes_map; |
| 422 | *count = ARRAY_SIZE(board_serdes_map); |
| 423 | |
Marek Behún | 09e16b8 | 2017-06-09 19:28:45 +0200 | [diff] [blame] | 424 | return 0; |
| 425 | } |
| 426 | |
| 427 | struct omnia_eeprom { |
| 428 | u32 magic; |
| 429 | u32 ramsize; |
| 430 | char region[4]; |
| 431 | u32 crc; |
Marek Behún | c754816 | 2024-06-18 17:34:33 +0200 | [diff] [blame] | 432 | |
| 433 | /* second part (only considered if crc2 is not all-ones) */ |
Marek Behún | 9535f79 | 2024-06-18 17:34:34 +0200 | [diff] [blame] | 434 | char ddr_speed[5]; |
Marek Behún | d1e6844 | 2024-06-18 17:34:39 +0200 | [diff] [blame] | 435 | u8 old_ddr_training; |
| 436 | u8 reserved[38]; |
Marek Behún | c754816 | 2024-06-18 17:34:33 +0200 | [diff] [blame] | 437 | u32 crc2; |
Marek Behún | 09e16b8 | 2017-06-09 19:28:45 +0200 | [diff] [blame] | 438 | }; |
| 439 | |
Marek Behún | c754816 | 2024-06-18 17:34:33 +0200 | [diff] [blame] | 440 | static bool is_omnia_eeprom_second_part_valid(const struct omnia_eeprom *oep) |
| 441 | { |
| 442 | return oep->crc2 != 0xffffffff; |
| 443 | } |
| 444 | |
| 445 | static void make_omnia_eeprom_second_part_invalid(struct omnia_eeprom *oep) |
| 446 | { |
| 447 | oep->crc2 = 0xffffffff; |
| 448 | } |
| 449 | |
| 450 | static bool check_eeprom_crc(const void *buf, size_t size, u32 expected, |
| 451 | const char *name) |
| 452 | { |
| 453 | u32 crc; |
| 454 | |
| 455 | crc = crc32(0, buf, size); |
| 456 | if (crc != expected) { |
| 457 | printf("bad %s EEPROM CRC (stored %08x, computed %08x)\n", |
| 458 | name, expected, crc); |
| 459 | return false; |
| 460 | } |
| 461 | |
| 462 | return true; |
| 463 | } |
| 464 | |
Marek Behún | 09e16b8 | 2017-06-09 19:28:45 +0200 | [diff] [blame] | 465 | static bool omnia_read_eeprom(struct omnia_eeprom *oep) |
| 466 | { |
Marek Behún | ba53b6b | 2019-05-02 16:53:30 +0200 | [diff] [blame] | 467 | struct udevice *chip; |
Marek Behún | ba53b6b | 2019-05-02 16:53:30 +0200 | [diff] [blame] | 468 | int ret; |
Marek Behún | 09e16b8 | 2017-06-09 19:28:45 +0200 | [diff] [blame] | 469 | |
Marek Behún | ba53b6b | 2019-05-02 16:53:30 +0200 | [diff] [blame] | 470 | chip = omnia_get_i2c_chip("EEPROM", OMNIA_I2C_EEPROM_CHIP_ADDR, |
| 471 | OMNIA_I2C_EEPROM_CHIP_LEN); |
| 472 | |
| 473 | if (!chip) |
Marek Behún | 09e16b8 | 2017-06-09 19:28:45 +0200 | [diff] [blame] | 474 | return false; |
Marek Behún | 09e16b8 | 2017-06-09 19:28:45 +0200 | [diff] [blame] | 475 | |
Marek Behún | ba53b6b | 2019-05-02 16:53:30 +0200 | [diff] [blame] | 476 | ret = dm_i2c_read(chip, 0, (void *)oep, sizeof(*oep)); |
Marek Behún | 09e16b8 | 2017-06-09 19:28:45 +0200 | [diff] [blame] | 477 | if (ret) { |
Marek Behún | ba53b6b | 2019-05-02 16:53:30 +0200 | [diff] [blame] | 478 | printf("dm_i2c_read failed: %i, cannot read EEPROM\n", ret); |
Marek Behún | 09e16b8 | 2017-06-09 19:28:45 +0200 | [diff] [blame] | 479 | return false; |
| 480 | } |
| 481 | |
Marek Behún | ba53b6b | 2019-05-02 16:53:30 +0200 | [diff] [blame] | 482 | if (oep->magic != OMNIA_I2C_EEPROM_MAGIC) { |
| 483 | printf("bad EEPROM magic number (%08x, should be %08x)\n", |
| 484 | oep->magic, OMNIA_I2C_EEPROM_MAGIC); |
| 485 | return false; |
Marek Behún | 09e16b8 | 2017-06-09 19:28:45 +0200 | [diff] [blame] | 486 | } |
| 487 | |
Marek Behún | c754816 | 2024-06-18 17:34:33 +0200 | [diff] [blame] | 488 | if (!check_eeprom_crc(oep, offsetof(struct omnia_eeprom, crc), oep->crc, |
| 489 | "first")) |
Marek Behún | 09e16b8 | 2017-06-09 19:28:45 +0200 | [diff] [blame] | 490 | return false; |
Marek Behún | c754816 | 2024-06-18 17:34:33 +0200 | [diff] [blame] | 491 | |
| 492 | if (is_omnia_eeprom_second_part_valid(oep) && |
| 493 | !check_eeprom_crc(oep, offsetof(struct omnia_eeprom, crc2), |
| 494 | oep->crc2, "second")) |
| 495 | make_omnia_eeprom_second_part_invalid(oep); |
Marek Behún | 09e16b8 | 2017-06-09 19:28:45 +0200 | [diff] [blame] | 496 | |
| 497 | return true; |
| 498 | } |
| 499 | |
Marek Behún | d1e6844 | 2024-06-18 17:34:39 +0200 | [diff] [blame] | 500 | int omnia_get_ram_size_gb(void) |
Marek Behún | 77652c7 | 2019-05-02 16:53:33 +0200 | [diff] [blame] | 501 | { |
| 502 | static int ram_size; |
| 503 | struct omnia_eeprom oep; |
| 504 | |
| 505 | if (!ram_size) { |
| 506 | /* Get the board config from EEPROM */ |
| 507 | if (omnia_read_eeprom(&oep)) { |
| 508 | debug("Memory config in EEPROM: 0x%02x\n", oep.ramsize); |
| 509 | |
| 510 | if (oep.ramsize == 0x2) |
| 511 | ram_size = 2; |
| 512 | else |
| 513 | ram_size = 1; |
| 514 | } else { |
| 515 | /* Hardcoded fallback */ |
| 516 | puts("Memory config from EEPROM read failed!\n"); |
| 517 | puts("Falling back to default 1 GiB!\n"); |
| 518 | ram_size = 1; |
| 519 | } |
| 520 | } |
| 521 | |
| 522 | return ram_size; |
| 523 | } |
| 524 | |
Marek Behún | d1e6844 | 2024-06-18 17:34:39 +0200 | [diff] [blame] | 525 | bool board_use_old_ddr3_training(void) |
| 526 | { |
| 527 | struct omnia_eeprom oep; |
| 528 | |
| 529 | if (!omnia_read_eeprom(&oep)) |
| 530 | return false; |
| 531 | |
| 532 | if (!is_omnia_eeprom_second_part_valid(&oep)) |
| 533 | return false; |
| 534 | |
| 535 | return oep.old_ddr_training == 1; |
| 536 | } |
| 537 | |
Marek Behún | 9535f79 | 2024-06-18 17:34:34 +0200 | [diff] [blame] | 538 | static const char *omnia_get_ddr_speed(void) |
| 539 | { |
| 540 | struct omnia_eeprom oep; |
| 541 | static char speed[sizeof(oep.ddr_speed) + 1]; |
| 542 | |
| 543 | if (!omnia_read_eeprom(&oep)) |
| 544 | return NULL; |
| 545 | |
| 546 | if (!is_omnia_eeprom_second_part_valid(&oep)) |
| 547 | return NULL; |
| 548 | |
| 549 | if (!oep.ddr_speed[0] || oep.ddr_speed[0] == 0xff) |
| 550 | return NULL; |
| 551 | |
| 552 | memcpy(&speed, &oep.ddr_speed, sizeof(oep.ddr_speed)); |
| 553 | speed[sizeof(speed) - 1] = '\0'; |
| 554 | |
| 555 | return speed; |
| 556 | } |
| 557 | |
Pali Rohár | 4798ba9 | 2022-07-29 13:29:06 +0200 | [diff] [blame] | 558 | static const char * const omnia_get_mcu_type(void) |
| 559 | { |
Marek Behún | bb42a5b | 2024-04-04 09:50:51 +0200 | [diff] [blame] | 560 | static char result[] = "xxxxxxx (with peripheral resets)"; |
Marek Behún | 8b52b8c | 2024-04-04 09:50:53 +0200 | [diff] [blame] | 561 | u16 sts; |
Pali Rohár | 4798ba9 | 2022-07-29 13:29:06 +0200 | [diff] [blame] | 562 | int ret; |
| 563 | |
Marek Behún | 8b52b8c | 2024-04-04 09:50:53 +0200 | [diff] [blame] | 564 | ret = omnia_mcu_get_sts(&sts); |
Pali Rohár | 4798ba9 | 2022-07-29 13:29:06 +0200 | [diff] [blame] | 565 | if (ret) |
| 566 | return "unknown"; |
| 567 | |
Marek Behún | 8b52b8c | 2024-04-04 09:50:53 +0200 | [diff] [blame] | 568 | switch (sts & STS_MCU_TYPE_MASK) { |
Marek Behún | bb42a5b | 2024-04-04 09:50:51 +0200 | [diff] [blame] | 569 | case STS_MCU_TYPE_STM32: |
| 570 | strcpy(result, "STM32"); |
| 571 | break; |
| 572 | case STS_MCU_TYPE_GD32: |
| 573 | strcpy(result, "GD32"); |
| 574 | break; |
| 575 | case STS_MCU_TYPE_MKL: |
| 576 | strcpy(result, "MKL"); |
| 577 | break; |
| 578 | default: |
| 579 | strcpy(result, "unknown"); |
| 580 | break; |
| 581 | } |
| 582 | |
Marek Behún | 8b52b8c | 2024-04-04 09:50:53 +0200 | [diff] [blame] | 583 | if (omnia_mcu_has_feature(FEAT_PERIPH_MCU)) |
| 584 | strcat(result, " (with peripheral resets)"); |
Pali Rohár | 4798ba9 | 2022-07-29 13:29:06 +0200 | [diff] [blame] | 585 | |
Marek Behún | bb42a5b | 2024-04-04 09:50:51 +0200 | [diff] [blame] | 586 | return result; |
Pali Rohár | 4798ba9 | 2022-07-29 13:29:06 +0200 | [diff] [blame] | 587 | } |
| 588 | |
Pali Rohár | e16cc98 | 2022-08-10 11:00:25 +0200 | [diff] [blame] | 589 | static const char * const omnia_get_mcu_version(void) |
| 590 | { |
| 591 | static char version[82]; |
| 592 | u8 version_app[20]; |
| 593 | u8 version_boot[20]; |
| 594 | int ret; |
| 595 | |
| 596 | ret = omnia_mcu_read(CMD_GET_FW_VERSION_APP, &version_app, sizeof(version_app)); |
| 597 | if (ret) |
| 598 | return "unknown"; |
| 599 | |
| 600 | ret = omnia_mcu_read(CMD_GET_FW_VERSION_BOOT, &version_boot, sizeof(version_boot)); |
| 601 | if (ret) |
| 602 | return "unknown"; |
| 603 | |
| 604 | /* |
| 605 | * If git commits of MCU bootloader and MCU application are same then |
| 606 | * show version only once. If they are different then show both commits. |
| 607 | */ |
| 608 | if (!memcmp(version_app, version_boot, 20)) { |
| 609 | bin2hex(version, version_app, 20); |
| 610 | version[40] = '\0'; |
| 611 | } else { |
| 612 | bin2hex(version, version_boot, 20); |
| 613 | version[40] = '/'; |
| 614 | bin2hex(version + 41, version_app, 20); |
| 615 | version[81] = '\0'; |
| 616 | } |
| 617 | |
| 618 | return version; |
| 619 | } |
| 620 | |
Marek Behún | 09e16b8 | 2017-06-09 19:28:45 +0200 | [diff] [blame] | 621 | /* |
| 622 | * Define the DDR layout / topology here in the board file. This will |
| 623 | * be used by the DDR3 init code in the SPL U-Boot version to configure |
| 624 | * the DDR3 controller. |
| 625 | */ |
Chris Packham | 1a07d21 | 2018-05-10 13:28:29 +1200 | [diff] [blame] | 626 | static struct mv_ddr_topology_map board_topology_map_1g = { |
| 627 | DEBUG_LEVEL_ERROR, |
Marek Behún | 09e16b8 | 2017-06-09 19:28:45 +0200 | [diff] [blame] | 628 | 0x1, /* active interfaces */ |
| 629 | /* cs_mask, mirror, dqs_swap, ck_swap X PUPs */ |
| 630 | { { { {0x1, 0, 0, 0}, |
| 631 | {0x1, 0, 0, 0}, |
| 632 | {0x1, 0, 0, 0}, |
| 633 | {0x1, 0, 0, 0}, |
| 634 | {0x1, 0, 0, 0} }, |
| 635 | SPEED_BIN_DDR_1600K, /* speed_bin */ |
Chris Packham | 1a07d21 | 2018-05-10 13:28:29 +1200 | [diff] [blame] | 636 | MV_DDR_DEV_WIDTH_16BIT, /* memory_width */ |
| 637 | MV_DDR_DIE_CAP_4GBIT, /* mem_size */ |
Chris Packham | 4bf81db | 2018-12-03 14:26:49 +1300 | [diff] [blame] | 638 | MV_DDR_FREQ_800, /* frequency */ |
Chris Packham | dd092bd | 2017-11-29 10:38:34 +1300 | [diff] [blame] | 639 | 0, 0, /* cas_wl cas_l */ |
Chris Packham | 3a09e13 | 2018-05-10 13:28:30 +1200 | [diff] [blame] | 640 | MV_DDR_TEMP_NORMAL, /* temperature */ |
| 641 | MV_DDR_TIM_2T} }, /* timing */ |
Chris Packham | 1a07d21 | 2018-05-10 13:28:29 +1200 | [diff] [blame] | 642 | BUS_MASK_32BIT, /* Busses mask */ |
| 643 | MV_DDR_CFG_DEFAULT, /* ddr configuration data source */ |
Moti Buskila | 498475e | 2021-02-19 17:11:19 +0100 | [diff] [blame] | 644 | NOT_COMBINED, /* ddr twin-die combined */ |
Chris Packham | 1a07d21 | 2018-05-10 13:28:29 +1200 | [diff] [blame] | 645 | { {0} }, /* raw spd data */ |
| 646 | {0} /* timing parameters */ |
Marek Behún | 09e16b8 | 2017-06-09 19:28:45 +0200 | [diff] [blame] | 647 | }; |
| 648 | |
Chris Packham | 1a07d21 | 2018-05-10 13:28:29 +1200 | [diff] [blame] | 649 | static struct mv_ddr_topology_map board_topology_map_2g = { |
| 650 | DEBUG_LEVEL_ERROR, |
Marek Behún | 09e16b8 | 2017-06-09 19:28:45 +0200 | [diff] [blame] | 651 | 0x1, /* active interfaces */ |
| 652 | /* cs_mask, mirror, dqs_swap, ck_swap X PUPs */ |
| 653 | { { { {0x1, 0, 0, 0}, |
| 654 | {0x1, 0, 0, 0}, |
| 655 | {0x1, 0, 0, 0}, |
| 656 | {0x1, 0, 0, 0}, |
| 657 | {0x1, 0, 0, 0} }, |
| 658 | SPEED_BIN_DDR_1600K, /* speed_bin */ |
Chris Packham | 1a07d21 | 2018-05-10 13:28:29 +1200 | [diff] [blame] | 659 | MV_DDR_DEV_WIDTH_16BIT, /* memory_width */ |
| 660 | MV_DDR_DIE_CAP_8GBIT, /* mem_size */ |
Chris Packham | 4bf81db | 2018-12-03 14:26:49 +1300 | [diff] [blame] | 661 | MV_DDR_FREQ_800, /* frequency */ |
Chris Packham | dd092bd | 2017-11-29 10:38:34 +1300 | [diff] [blame] | 662 | 0, 0, /* cas_wl cas_l */ |
Chris Packham | 3a09e13 | 2018-05-10 13:28:30 +1200 | [diff] [blame] | 663 | MV_DDR_TEMP_NORMAL, /* temperature */ |
| 664 | MV_DDR_TIM_2T} }, /* timing */ |
Chris Packham | 1a07d21 | 2018-05-10 13:28:29 +1200 | [diff] [blame] | 665 | BUS_MASK_32BIT, /* Busses mask */ |
| 666 | MV_DDR_CFG_DEFAULT, /* ddr configuration data source */ |
Moti Buskila | 498475e | 2021-02-19 17:11:19 +0100 | [diff] [blame] | 667 | NOT_COMBINED, /* ddr twin-die combined */ |
Chris Packham | 1a07d21 | 2018-05-10 13:28:29 +1200 | [diff] [blame] | 668 | { {0} }, /* raw spd data */ |
| 669 | {0} /* timing parameters */ |
Marek Behún | 09e16b8 | 2017-06-09 19:28:45 +0200 | [diff] [blame] | 670 | }; |
| 671 | |
Marek Behún | 9535f79 | 2024-06-18 17:34:34 +0200 | [diff] [blame] | 672 | static const struct omnia_ddr_speed { |
| 673 | char name[5]; |
| 674 | u8 speed_bin; |
| 675 | u8 freq; |
| 676 | } omnia_ddr_speeds[] = { |
| 677 | { "1066F", SPEED_BIN_DDR_1066F, MV_DDR_FREQ_533 }, |
| 678 | { "1333H", SPEED_BIN_DDR_1333H, MV_DDR_FREQ_667 }, |
| 679 | { "1600K", SPEED_BIN_DDR_1600K, MV_DDR_FREQ_800 }, |
| 680 | }; |
| 681 | |
| 682 | static const struct omnia_ddr_speed *find_ddr_speed_setting(const char *name) |
| 683 | { |
| 684 | for (int i = 0; i < ARRAY_SIZE(omnia_ddr_speeds); ++i) |
| 685 | if (!strncmp(name, omnia_ddr_speeds[i].name, 5)) |
| 686 | return &omnia_ddr_speeds[i]; |
| 687 | |
| 688 | return NULL; |
| 689 | } |
| 690 | |
| 691 | bool omnia_valid_ddr_speed(const char *name) |
| 692 | { |
| 693 | return find_ddr_speed_setting(name) != NULL; |
| 694 | } |
| 695 | |
| 696 | void omnia_print_ddr_speeds(void) |
| 697 | { |
| 698 | for (int i = 0; i < ARRAY_SIZE(omnia_ddr_speeds); ++i) |
| 699 | printf("%.5s%s", omnia_ddr_speeds[i].name, |
| 700 | i == ARRAY_SIZE(omnia_ddr_speeds) - 1 ? "\n" : ", "); |
| 701 | } |
| 702 | |
| 703 | static void fixup_speed_in_ddr_topology(struct mv_ddr_topology_map *topology) |
| 704 | { |
| 705 | typeof(topology->interface_params[0]) *params; |
| 706 | const struct omnia_ddr_speed *setting; |
| 707 | const char *speed; |
| 708 | static bool done; |
| 709 | |
| 710 | if (done) |
| 711 | return; |
| 712 | |
| 713 | done = true; |
| 714 | |
| 715 | speed = omnia_get_ddr_speed(); |
| 716 | if (!speed) |
| 717 | return; |
| 718 | |
| 719 | setting = find_ddr_speed_setting(speed); |
| 720 | if (!setting) { |
| 721 | printf("Unsupported value %s for DDR3 speed in EEPROM!\n", |
| 722 | speed); |
| 723 | return; |
| 724 | } |
| 725 | |
| 726 | params = &topology->interface_params[0]; |
| 727 | |
| 728 | /* don't inform if we are not changing the speed from the default one */ |
| 729 | if (params->speed_bin_index == setting->speed_bin) |
| 730 | return; |
| 731 | |
| 732 | printf("Fixing up DDR3 speed (EEPROM defines %s)\n", speed); |
| 733 | |
| 734 | params->speed_bin_index = setting->speed_bin; |
| 735 | params->memory_freq = setting->freq; |
| 736 | } |
| 737 | |
Chris Packham | 1a07d21 | 2018-05-10 13:28:29 +1200 | [diff] [blame] | 738 | struct mv_ddr_topology_map *mv_ddr_topology_map_get(void) |
Marek Behún | 09e16b8 | 2017-06-09 19:28:45 +0200 | [diff] [blame] | 739 | { |
Marek Behún | 9535f79 | 2024-06-18 17:34:34 +0200 | [diff] [blame] | 740 | struct mv_ddr_topology_map *topology; |
| 741 | |
Marek Behún | 77652c7 | 2019-05-02 16:53:33 +0200 | [diff] [blame] | 742 | if (omnia_get_ram_size_gb() == 2) |
Marek Behún | 9535f79 | 2024-06-18 17:34:34 +0200 | [diff] [blame] | 743 | topology = &board_topology_map_2g; |
Marek Behún | 77652c7 | 2019-05-02 16:53:33 +0200 | [diff] [blame] | 744 | else |
Marek Behún | 9535f79 | 2024-06-18 17:34:34 +0200 | [diff] [blame] | 745 | topology = &board_topology_map_1g; |
| 746 | |
| 747 | fixup_speed_in_ddr_topology(topology); |
| 748 | |
| 749 | return topology; |
Marek Behún | 09e16b8 | 2017-06-09 19:28:45 +0200 | [diff] [blame] | 750 | } |
| 751 | |
Marek Behún | 09e16b8 | 2017-06-09 19:28:45 +0200 | [diff] [blame] | 752 | static int set_regdomain(void) |
| 753 | { |
| 754 | struct omnia_eeprom oep; |
| 755 | char rd[3] = {' ', ' ', 0}; |
| 756 | |
| 757 | if (omnia_read_eeprom(&oep)) |
| 758 | memcpy(rd, &oep.region, 2); |
| 759 | else |
| 760 | puts("EEPROM regdomain read failed.\n"); |
| 761 | |
| 762 | printf("Regdomain set to %s\n", rd); |
Simon Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 763 | return env_set("regdomain", rd); |
Marek Behún | 09e16b8 | 2017-06-09 19:28:45 +0200 | [diff] [blame] | 764 | } |
Marek Behún | 0f2e66a | 2019-05-02 16:53:37 +0200 | [diff] [blame] | 765 | |
Marek Behún | 0f2e66a | 2019-05-02 16:53:37 +0200 | [diff] [blame] | 766 | static void handle_reset_button(void) |
| 767 | { |
Pali Rohár | 905c3bf | 2021-06-14 16:45:58 +0200 | [diff] [blame] | 768 | const char * const vars[1] = { "bootcmd_rescue", }; |
Marek Behún | 0f2e66a | 2019-05-02 16:53:37 +0200 | [diff] [blame] | 769 | int ret; |
| 770 | u8 reset_status; |
| 771 | |
Pali Rohár | 905c3bf | 2021-06-14 16:45:58 +0200 | [diff] [blame] | 772 | /* |
| 773 | * Ensure that bootcmd_rescue has always stock value, so that running |
| 774 | * run bootcmd_rescue |
| 775 | * always works correctly. |
| 776 | */ |
| 777 | env_set_default_vars(1, (char * const *)vars, 0); |
| 778 | |
Marek Behún | 0f2e66a | 2019-05-02 16:53:37 +0200 | [diff] [blame] | 779 | ret = omnia_mcu_read(CMD_GET_RESET, &reset_status, 1); |
| 780 | if (ret) { |
| 781 | printf("omnia_mcu_read failed: %i, reset status unknown!\n", |
| 782 | ret); |
| 783 | return; |
| 784 | } |
| 785 | |
| 786 | env_set_ulong("omnia_reset", reset_status); |
| 787 | |
| 788 | if (reset_status) { |
Pali Rohár | 8adec65 | 2022-08-27 20:49:20 +0200 | [diff] [blame] | 789 | const char * const vars[3] = { |
Marek Behún | 09f8de2 | 2021-05-28 10:00:49 +0200 | [diff] [blame] | 790 | "bootcmd", |
Pali Rohár | 8adec65 | 2022-08-27 20:49:20 +0200 | [diff] [blame] | 791 | "bootdelay", |
Marek Behún | 09f8de2 | 2021-05-28 10:00:49 +0200 | [diff] [blame] | 792 | "distro_bootcmd", |
| 793 | }; |
| 794 | |
| 795 | /* |
| 796 | * Set the above envs to their default values, in case the user |
| 797 | * managed to break them. |
| 798 | */ |
Pali Rohár | 8adec65 | 2022-08-27 20:49:20 +0200 | [diff] [blame] | 799 | env_set_default_vars(3, (char * const *)vars, 0); |
Marek Behún | 09f8de2 | 2021-05-28 10:00:49 +0200 | [diff] [blame] | 800 | |
| 801 | /* Ensure bootcmd_rescue is used by distroboot */ |
| 802 | env_set("boot_targets", "rescue"); |
| 803 | |
Pali Rohár | 4f9e6fb | 2022-04-06 11:39:32 +0200 | [diff] [blame] | 804 | printf("RESET button was pressed, overwriting boot_targets!\n"); |
Marek Behún | 09f8de2 | 2021-05-28 10:00:49 +0200 | [diff] [blame] | 805 | } else { |
| 806 | /* |
| 807 | * In case the user somehow managed to save environment with |
| 808 | * boot_targets=rescue, reset boot_targets to default value. |
| 809 | * This could happen in subsequent commands if bootcmd_rescue |
| 810 | * failed. |
| 811 | */ |
| 812 | if (!strcmp(env_get("boot_targets"), "rescue")) { |
| 813 | const char * const vars[1] = { |
| 814 | "boot_targets", |
| 815 | }; |
| 816 | |
| 817 | env_set_default_vars(1, (char * const *)vars, 0); |
| 818 | } |
Marek Behún | 0f2e66a | 2019-05-02 16:53:37 +0200 | [diff] [blame] | 819 | } |
| 820 | } |
Marek Behún | 09e16b8 | 2017-06-09 19:28:45 +0200 | [diff] [blame] | 821 | |
Pali Rohár | 1e0a975 | 2022-07-29 13:29:07 +0200 | [diff] [blame] | 822 | static void initialize_switch(void) |
| 823 | { |
| 824 | u32 val, val04, val08, val10, val14; |
| 825 | u16 ctrl[2]; |
| 826 | int err; |
| 827 | |
| 828 | printf("Initializing LAN eth switch... "); |
| 829 | |
| 830 | /* Change RGMII pins to GPIO mode */ |
| 831 | |
| 832 | val = val04 = readl(MVEBU_MPP_BASE + 0x04); |
| 833 | val &= ~GENMASK(19, 16); /* MPP[12] := GPIO */ |
| 834 | val &= ~GENMASK(23, 20); /* MPP[13] := GPIO */ |
| 835 | val &= ~GENMASK(27, 24); /* MPP[14] := GPIO */ |
| 836 | val &= ~GENMASK(31, 28); /* MPP[15] := GPIO */ |
| 837 | writel(val, MVEBU_MPP_BASE + 0x04); |
| 838 | |
| 839 | val = val08 = readl(MVEBU_MPP_BASE + 0x08); |
| 840 | val &= ~GENMASK(3, 0); /* MPP[16] := GPIO */ |
| 841 | val &= ~GENMASK(23, 20); /* MPP[21] := GPIO */ |
| 842 | writel(val, MVEBU_MPP_BASE + 0x08); |
| 843 | |
| 844 | val = val10 = readl(MVEBU_MPP_BASE + 0x10); |
| 845 | val &= ~GENMASK(27, 24); /* MPP[38] := GPIO */ |
| 846 | val &= ~GENMASK(31, 28); /* MPP[39] := GPIO */ |
| 847 | writel(val, MVEBU_MPP_BASE + 0x10); |
| 848 | |
| 849 | val = val14 = readl(MVEBU_MPP_BASE + 0x14); |
| 850 | val &= ~GENMASK(3, 0); /* MPP[40] := GPIO */ |
| 851 | val &= ~GENMASK(7, 4); /* MPP[41] := GPIO */ |
| 852 | writel(val, MVEBU_MPP_BASE + 0x14); |
| 853 | |
| 854 | /* Set initial values for switch reset strapping pins */ |
| 855 | |
| 856 | val = readl(MVEBU_GPIO0_BASE + 0x00); |
| 857 | val |= BIT(12); /* GPIO[12] := 1 */ |
| 858 | val |= BIT(13); /* GPIO[13] := 1 */ |
| 859 | val |= BIT(14); /* GPIO[14] := 1 */ |
| 860 | val |= BIT(15); /* GPIO[15] := 1 */ |
| 861 | val &= ~BIT(16); /* GPIO[16] := 0 */ |
| 862 | val |= BIT(21); /* GPIO[21] := 1 */ |
| 863 | writel(val, MVEBU_GPIO0_BASE + 0x00); |
| 864 | |
| 865 | val = readl(MVEBU_GPIO1_BASE + 0x00); |
| 866 | val |= BIT(6); /* GPIO[38] := 1 */ |
| 867 | val |= BIT(7); /* GPIO[39] := 1 */ |
| 868 | val |= BIT(8); /* GPIO[40] := 1 */ |
| 869 | val &= ~BIT(9); /* GPIO[41] := 0 */ |
| 870 | writel(val, MVEBU_GPIO1_BASE + 0x00); |
| 871 | |
| 872 | val = readl(MVEBU_GPIO0_BASE + 0x04); |
| 873 | val &= ~BIT(12); /* GPIO[12] := Out Enable */ |
| 874 | val &= ~BIT(13); /* GPIO[13] := Out Enable */ |
| 875 | val &= ~BIT(14); /* GPIO[14] := Out Enable */ |
| 876 | val &= ~BIT(15); /* GPIO[15] := Out Enable */ |
| 877 | val &= ~BIT(16); /* GPIO[16] := Out Enable */ |
| 878 | val &= ~BIT(21); /* GPIO[21] := Out Enable */ |
| 879 | writel(val, MVEBU_GPIO0_BASE + 0x04); |
| 880 | |
| 881 | val = readl(MVEBU_GPIO1_BASE + 0x04); |
| 882 | val &= ~BIT(6); /* GPIO[38] := Out Enable */ |
| 883 | val &= ~BIT(7); /* GPIO[39] := Out Enable */ |
| 884 | val &= ~BIT(8); /* GPIO[40] := Out Enable */ |
| 885 | val &= ~BIT(9); /* GPIO[41] := Out Enable */ |
| 886 | writel(val, MVEBU_GPIO1_BASE + 0x04); |
| 887 | |
| 888 | /* Release switch reset */ |
| 889 | |
| 890 | ctrl[0] = EXT_CTL_nRES_LAN; |
| 891 | ctrl[1] = EXT_CTL_nRES_LAN; |
| 892 | err = omnia_mcu_write(CMD_EXT_CONTROL, ctrl, sizeof(ctrl)); |
| 893 | |
Marek Behún | 59aa465 | 2022-09-13 18:10:28 +0200 | [diff] [blame] | 894 | mdelay(50); |
Pali Rohár | 1e0a975 | 2022-07-29 13:29:07 +0200 | [diff] [blame] | 895 | |
| 896 | /* Change RGMII pins back to RGMII mode */ |
| 897 | |
| 898 | writel(val04, MVEBU_MPP_BASE + 0x04); |
| 899 | writel(val08, MVEBU_MPP_BASE + 0x08); |
| 900 | writel(val10, MVEBU_MPP_BASE + 0x10); |
| 901 | writel(val14, MVEBU_MPP_BASE + 0x14); |
| 902 | |
| 903 | puts(err ? "failed\n" : "done\n"); |
| 904 | } |
| 905 | |
Marek Behún | 09e16b8 | 2017-06-09 19:28:45 +0200 | [diff] [blame] | 906 | int board_early_init_f(void) |
| 907 | { |
Marek Behún | 09e16b8 | 2017-06-09 19:28:45 +0200 | [diff] [blame] | 908 | /* Configure MPP */ |
| 909 | writel(0x11111111, MVEBU_MPP_BASE + 0x00); |
| 910 | writel(0x11111111, MVEBU_MPP_BASE + 0x04); |
| 911 | writel(0x11244011, MVEBU_MPP_BASE + 0x08); |
| 912 | writel(0x22222111, MVEBU_MPP_BASE + 0x0c); |
| 913 | writel(0x22200002, MVEBU_MPP_BASE + 0x10); |
| 914 | writel(0x30042022, MVEBU_MPP_BASE + 0x14); |
| 915 | writel(0x55550555, MVEBU_MPP_BASE + 0x18); |
| 916 | writel(0x00005550, MVEBU_MPP_BASE + 0x1c); |
| 917 | |
| 918 | /* Set GPP Out value */ |
| 919 | writel(OMNIA_GPP_OUT_VAL_LOW, MVEBU_GPIO0_BASE + 0x00); |
| 920 | writel(OMNIA_GPP_OUT_VAL_MID, MVEBU_GPIO1_BASE + 0x00); |
| 921 | |
| 922 | /* Set GPP Polarity */ |
| 923 | writel(OMNIA_GPP_POL_LOW, MVEBU_GPIO0_BASE + 0x0c); |
| 924 | writel(OMNIA_GPP_POL_MID, MVEBU_GPIO1_BASE + 0x0c); |
| 925 | |
| 926 | /* Set GPP Out Enable */ |
| 927 | writel(OMNIA_GPP_OUT_ENA_LOW, MVEBU_GPIO0_BASE + 0x04); |
| 928 | writel(OMNIA_GPP_OUT_ENA_MID, MVEBU_GPIO1_BASE + 0x04); |
| 929 | |
Marek Behún | 09e16b8 | 2017-06-09 19:28:45 +0200 | [diff] [blame] | 930 | return 0; |
| 931 | } |
| 932 | |
Marek Behún | f355616 | 2021-08-16 15:19:39 +0200 | [diff] [blame] | 933 | void spl_board_init(void) |
| 934 | { |
| 935 | /* |
| 936 | * If booting from UART, disable MCU watchdog in SPL, since uploading |
Pali Rohár | 7fcda0c | 2021-11-09 17:14:02 +0100 | [diff] [blame] | 937 | * U-Boot proper can take too much time and trigger it. Instead enable |
| 938 | * A385 watchdog with very high timeout (10 minutes) to prevent hangup. |
Marek Behún | f355616 | 2021-08-16 15:19:39 +0200 | [diff] [blame] | 939 | */ |
Pali Rohár | 7fcda0c | 2021-11-09 17:14:02 +0100 | [diff] [blame] | 940 | if (get_boot_device() == BOOT_DEVICE_UART) { |
| 941 | enable_a385_watchdog(10); |
Marek Behún | f355616 | 2021-08-16 15:19:39 +0200 | [diff] [blame] | 942 | disable_mcu_watchdog(); |
Pali Rohár | 7fcda0c | 2021-11-09 17:14:02 +0100 | [diff] [blame] | 943 | } |
Pali Rohár | 1e0a975 | 2022-07-29 13:29:07 +0200 | [diff] [blame] | 944 | |
| 945 | /* |
| 946 | * When MCU controls peripheral resets then release LAN eth switch from |
| 947 | * the reset and initialize it. When MCU does not control peripheral |
| 948 | * resets then LAN eth switch is initialized automatically by bootstrap |
| 949 | * pins when A385 is released from the reset. |
| 950 | */ |
Marek Behún | 8b52b8c | 2024-04-04 09:50:53 +0200 | [diff] [blame] | 951 | if (omnia_mcu_has_feature(FEAT_PERIPH_MCU)) |
| 952 | initialize_switch(); |
Marek Behún | f355616 | 2021-08-16 15:19:39 +0200 | [diff] [blame] | 953 | } |
| 954 | |
Pali Rohár | cbda3e2 | 2022-01-10 11:47:18 +0100 | [diff] [blame] | 955 | #if IS_ENABLED(CONFIG_OF_BOARD_FIXUP) || IS_ENABLED(CONFIG_OF_BOARD_SETUP) |
| 956 | |
Pali Rohár | 7cd4173 | 2022-03-02 12:47:56 +0100 | [diff] [blame] | 957 | static void disable_sata_node(void *blob) |
Pali Rohár | cbda3e2 | 2022-01-10 11:47:18 +0100 | [diff] [blame] | 958 | { |
Pali Rohár | cbda3e2 | 2022-01-10 11:47:18 +0100 | [diff] [blame] | 959 | int node; |
| 960 | |
Pali Rohár | 7cd4173 | 2022-03-02 12:47:56 +0100 | [diff] [blame] | 961 | fdt_for_each_node_by_compatible(node, blob, -1, "marvell,armada-380-ahci") { |
| 962 | if (!fdtdec_get_is_enabled(blob, node)) |
| 963 | continue; |
| 964 | |
| 965 | if (fdt_status_disabled(blob, node) < 0) |
| 966 | printf("Cannot disable SATA DT node!\n"); |
| 967 | else |
| 968 | debug("Disabled SATA DT node\n"); |
| 969 | |
Pali Rohár | e910526 | 2022-03-02 12:47:57 +0100 | [diff] [blame] | 970 | return; |
Pali Rohár | 7cd4173 | 2022-03-02 12:47:56 +0100 | [diff] [blame] | 971 | } |
Pali Rohár | e910526 | 2022-03-02 12:47:57 +0100 | [diff] [blame] | 972 | |
| 973 | printf("Cannot find SATA DT node!\n"); |
Pali Rohár | 7cd4173 | 2022-03-02 12:47:56 +0100 | [diff] [blame] | 974 | } |
| 975 | |
| 976 | static void disable_pcie_node(void *blob, int port) |
| 977 | { |
| 978 | int node; |
| 979 | |
| 980 | fdt_for_each_node_by_compatible(node, blob, -1, "marvell,armada-370-pcie") { |
| 981 | int port_node; |
| 982 | |
| 983 | if (!fdtdec_get_is_enabled(blob, node)) |
| 984 | continue; |
| 985 | |
| 986 | fdt_for_each_subnode (port_node, blob, node) { |
| 987 | if (!fdtdec_get_is_enabled(blob, port_node)) |
| 988 | continue; |
| 989 | |
| 990 | if (fdtdec_get_int(blob, port_node, "marvell,pcie-port", -1) != port) |
| 991 | continue; |
| 992 | |
| 993 | if (fdt_status_disabled(blob, port_node) < 0) |
| 994 | printf("Cannot disable PCIe port %d DT node!\n", port); |
| 995 | else |
| 996 | debug("Disabled PCIe port %d DT node\n", port); |
| 997 | |
| 998 | return; |
| 999 | } |
| 1000 | } |
Pali Rohár | e910526 | 2022-03-02 12:47:57 +0100 | [diff] [blame] | 1001 | |
| 1002 | printf("Cannot find PCIe port %d DT node!\n", port); |
Pali Rohár | 7cd4173 | 2022-03-02 12:47:56 +0100 | [diff] [blame] | 1003 | } |
| 1004 | |
| 1005 | static void fixup_msata_port_nodes(void *blob) |
| 1006 | { |
| 1007 | bool mode_sata; |
| 1008 | |
Pali Rohár | cbda3e2 | 2022-01-10 11:47:18 +0100 | [diff] [blame] | 1009 | /* |
| 1010 | * Determine if SerDes 0 is configured to SATA mode. |
| 1011 | * We do this instead of calling omnia_detect_sata() to avoid another |
| 1012 | * call to the MCU. By this time the common PHYs are initialized (it is |
| 1013 | * done in SPL), so we can read this common PHY register. |
| 1014 | */ |
| 1015 | mode_sata = (readl(MVEBU_REGISTER(0x183fc)) & GENMASK(3, 0)) == 2; |
| 1016 | |
| 1017 | /* |
| 1018 | * We're either adding status = "disabled" property, or changing |
| 1019 | * status = "okay" to status = "disabled". In both cases we'll need more |
| 1020 | * space. Increase the size a little. |
| 1021 | */ |
| 1022 | if (fdt_increase_size(blob, 32) < 0) { |
| 1023 | printf("Cannot increase FDT size!\n"); |
| 1024 | return; |
| 1025 | } |
| 1026 | |
Pali Rohár | cbda3e2 | 2022-01-10 11:47:18 +0100 | [diff] [blame] | 1027 | if (!mode_sata) { |
Pali Rohár | 7cd4173 | 2022-03-02 12:47:56 +0100 | [diff] [blame] | 1028 | /* If mSATA card is not present, disable SATA DT node */ |
| 1029 | disable_sata_node(blob); |
| 1030 | } else { |
| 1031 | /* Otherwise disable PCIe port 0 DT node (MiniPCIe / mSATA port) */ |
| 1032 | disable_pcie_node(blob, 0); |
Pali Rohár | cbda3e2 | 2022-01-10 11:47:18 +0100 | [diff] [blame] | 1033 | } |
Pali Rohár | 93a89c5 | 2022-03-02 12:47:58 +0100 | [diff] [blame] | 1034 | } |
| 1035 | |
| 1036 | static void fixup_wwan_port_nodes(void *blob) |
| 1037 | { |
| 1038 | bool mode_usb3; |
| 1039 | |
| 1040 | /* Determine if SerDes 4 is configured to USB3 mode */ |
| 1041 | mode_usb3 = ((readl(MVEBU_REGISTER(0x183fc)) & GENMASK(19, 16)) >> 16) == 4; |
| 1042 | |
| 1043 | /* If SerDes 4 is not configured to USB3 mode then nothing is needed to fixup */ |
| 1044 | if (!mode_usb3) |
| 1045 | return; |
| 1046 | |
| 1047 | /* |
| 1048 | * We're either adding status = "disabled" property, or changing |
| 1049 | * status = "okay" to status = "disabled". In both cases we'll need more |
| 1050 | * space. Increase the size a little. |
| 1051 | */ |
| 1052 | if (fdt_increase_size(blob, 32) < 0) { |
| 1053 | printf("Cannot increase FDT size!\n"); |
| 1054 | return; |
| 1055 | } |
| 1056 | |
| 1057 | /* Disable PCIe port 2 DT node (WWAN) */ |
| 1058 | disable_pcie_node(blob, 2); |
Pali Rohár | cbda3e2 | 2022-01-10 11:47:18 +0100 | [diff] [blame] | 1059 | } |
| 1060 | |
Pali Rohár | 1e0a975 | 2022-07-29 13:29:07 +0200 | [diff] [blame] | 1061 | static int insert_mcu_gpio_prop(void *blob, int node, const char *prop, |
| 1062 | unsigned int phandle, u32 bank, u32 gpio, |
| 1063 | u32 flags) |
| 1064 | { |
| 1065 | fdt32_t val[4] = { cpu_to_fdt32(phandle), cpu_to_fdt32(bank), |
| 1066 | cpu_to_fdt32(gpio), cpu_to_fdt32(flags) }; |
| 1067 | return fdt_setprop(blob, node, prop, &val, sizeof(val)); |
| 1068 | } |
| 1069 | |
| 1070 | static int fixup_mcu_gpio_in_pcie_nodes(void *blob) |
| 1071 | { |
| 1072 | unsigned int mcu_phandle; |
| 1073 | int port, gpio; |
| 1074 | int pcie_node; |
| 1075 | int port_node; |
| 1076 | int ret; |
| 1077 | |
| 1078 | ret = fdt_increase_size(blob, 128); |
| 1079 | if (ret < 0) { |
| 1080 | printf("Cannot increase FDT size!\n"); |
| 1081 | return ret; |
| 1082 | } |
| 1083 | |
| 1084 | mcu_phandle = fdt_create_phandle_by_compatible(blob, "cznic,turris-omnia-mcu"); |
| 1085 | if (!mcu_phandle) |
| 1086 | return -FDT_ERR_NOPHANDLES; |
| 1087 | |
| 1088 | fdt_for_each_node_by_compatible(pcie_node, blob, -1, "marvell,armada-370-pcie") { |
| 1089 | if (!fdtdec_get_is_enabled(blob, pcie_node)) |
| 1090 | continue; |
| 1091 | |
| 1092 | fdt_for_each_subnode(port_node, blob, pcie_node) { |
| 1093 | if (!fdtdec_get_is_enabled(blob, port_node)) |
| 1094 | continue; |
| 1095 | |
| 1096 | port = fdtdec_get_int(blob, port_node, "marvell,pcie-port", -1); |
| 1097 | |
| 1098 | if (port == 0) |
| 1099 | gpio = ilog2(EXT_CTL_nPERST0); |
| 1100 | else if (port == 1) |
| 1101 | gpio = ilog2(EXT_CTL_nPERST1); |
| 1102 | else if (port == 2) |
| 1103 | gpio = ilog2(EXT_CTL_nPERST2); |
| 1104 | else |
| 1105 | continue; |
| 1106 | |
| 1107 | /* insert: reset-gpios = <&mcu 2 gpio GPIO_ACTIVE_LOW>; */ |
| 1108 | ret = insert_mcu_gpio_prop(blob, port_node, "reset-gpios", |
| 1109 | mcu_phandle, 2, gpio, GPIO_ACTIVE_LOW); |
| 1110 | if (ret < 0) |
| 1111 | return ret; |
| 1112 | } |
| 1113 | } |
| 1114 | |
| 1115 | return 0; |
| 1116 | } |
| 1117 | |
Marek Behún | 85e223e | 2024-06-18 17:34:30 +0200 | [diff] [blame] | 1118 | static int get_phy_wan_node_offset(const void *blob) |
| 1119 | { |
| 1120 | u32 phy_wan_phandle; |
| 1121 | |
| 1122 | phy_wan_phandle = fdt_getprop_u32_default(blob, "ethernet2", "phy-handle", 0); |
| 1123 | if (!phy_wan_phandle) |
| 1124 | return -FDT_ERR_NOTFOUND; |
| 1125 | |
| 1126 | return fdt_node_offset_by_phandle(blob, phy_wan_phandle); |
| 1127 | } |
| 1128 | |
| 1129 | static int fixup_mcu_gpio_in_phy_wan_node(void *blob) |
Pali Rohár | 1e0a975 | 2022-07-29 13:29:07 +0200 | [diff] [blame] | 1130 | { |
| 1131 | unsigned int mcu_phandle; |
Marek Behún | 85e223e | 2024-06-18 17:34:30 +0200 | [diff] [blame] | 1132 | int phy_wan_node, ret; |
Pali Rohár | 1e0a975 | 2022-07-29 13:29:07 +0200 | [diff] [blame] | 1133 | |
| 1134 | ret = fdt_increase_size(blob, 64); |
| 1135 | if (ret < 0) { |
| 1136 | printf("Cannot increase FDT size!\n"); |
| 1137 | return ret; |
| 1138 | } |
| 1139 | |
Marek Behún | 85e223e | 2024-06-18 17:34:30 +0200 | [diff] [blame] | 1140 | phy_wan_node = get_phy_wan_node_offset(blob); |
| 1141 | if (phy_wan_node < 0) |
| 1142 | return phy_wan_node; |
Pali Rohár | 1e0a975 | 2022-07-29 13:29:07 +0200 | [diff] [blame] | 1143 | |
| 1144 | mcu_phandle = fdt_create_phandle_by_compatible(blob, "cznic,turris-omnia-mcu"); |
| 1145 | if (!mcu_phandle) |
| 1146 | return -FDT_ERR_NOPHANDLES; |
| 1147 | |
Marek Behún | 85e223e | 2024-06-18 17:34:30 +0200 | [diff] [blame] | 1148 | /* insert: reset-gpios = <&mcu 2 gpio GPIO_ACTIVE_LOW>; */ |
| 1149 | return insert_mcu_gpio_prop(blob, phy_wan_node, "reset-gpios", |
| 1150 | mcu_phandle, 2, ilog2(EXT_CTL_nRES_PHY), GPIO_ACTIVE_LOW); |
Pali Rohár | 1e0a975 | 2022-07-29 13:29:07 +0200 | [diff] [blame] | 1151 | } |
| 1152 | |
Marek Behún | 4cf5a03 | 2024-04-04 09:50:56 +0200 | [diff] [blame] | 1153 | static void fixup_atsha_node(void *blob) |
| 1154 | { |
| 1155 | int node; |
| 1156 | |
| 1157 | if (!omnia_mcu_has_feature(FEAT_CRYPTO)) |
| 1158 | return; |
| 1159 | |
| 1160 | node = fdt_node_offset_by_compatible(blob, -1, "atmel,atsha204a"); |
| 1161 | if (node < 0) { |
| 1162 | printf("Cannot find ATSHA204A node!\n"); |
| 1163 | return; |
| 1164 | } |
| 1165 | |
| 1166 | if (fdt_status_disabled(blob, node) < 0) |
| 1167 | printf("Cannot disable ATSHA204A node!\n"); |
| 1168 | else |
| 1169 | debug("Disabled ATSHA204A node\n"); |
| 1170 | } |
| 1171 | |
Pali Rohár | cbda3e2 | 2022-01-10 11:47:18 +0100 | [diff] [blame] | 1172 | #endif |
| 1173 | |
| 1174 | #if IS_ENABLED(CONFIG_OF_BOARD_FIXUP) |
| 1175 | int board_fix_fdt(void *blob) |
| 1176 | { |
Marek Behún | 8b52b8c | 2024-04-04 09:50:53 +0200 | [diff] [blame] | 1177 | if (omnia_mcu_has_feature(FEAT_PERIPH_MCU)) { |
| 1178 | fixup_mcu_gpio_in_pcie_nodes(blob); |
Marek Behún | 85e223e | 2024-06-18 17:34:30 +0200 | [diff] [blame] | 1179 | fixup_mcu_gpio_in_phy_wan_node(blob); |
Pali Rohár | 1e0a975 | 2022-07-29 13:29:07 +0200 | [diff] [blame] | 1180 | } |
| 1181 | |
Pali Rohár | 7cd4173 | 2022-03-02 12:47:56 +0100 | [diff] [blame] | 1182 | fixup_msata_port_nodes(blob); |
Pali Rohár | 93a89c5 | 2022-03-02 12:47:58 +0100 | [diff] [blame] | 1183 | fixup_wwan_port_nodes(blob); |
Pali Rohár | cbda3e2 | 2022-01-10 11:47:18 +0100 | [diff] [blame] | 1184 | |
Marek Behún | 4cf5a03 | 2024-04-04 09:50:56 +0200 | [diff] [blame] | 1185 | fixup_atsha_node(blob); |
| 1186 | |
Pali Rohár | cbda3e2 | 2022-01-10 11:47:18 +0100 | [diff] [blame] | 1187 | return 0; |
| 1188 | } |
| 1189 | #endif |
| 1190 | |
Marek Behún | 09e16b8 | 2017-06-09 19:28:45 +0200 | [diff] [blame] | 1191 | int board_init(void) |
| 1192 | { |
Marek Behún | 4dfc57e | 2019-05-02 16:53:31 +0200 | [diff] [blame] | 1193 | /* address of boot parameters */ |
Marek Behún | 09e16b8 | 2017-06-09 19:28:45 +0200 | [diff] [blame] | 1194 | gd->bd->bi_boot_params = mvebu_sdram_bar(0) + 0x100; |
| 1195 | |
Marek Behún | 88dc024 | 2021-08-16 15:19:40 +0200 | [diff] [blame] | 1196 | return 0; |
| 1197 | } |
| 1198 | |
| 1199 | int board_late_init(void) |
| 1200 | { |
Marek Behún | f355616 | 2021-08-16 15:19:39 +0200 | [diff] [blame] | 1201 | /* |
| 1202 | * If not booting from UART, MCU watchdog was not disabled in SPL, |
| 1203 | * disable it now. |
| 1204 | */ |
| 1205 | if (get_boot_device() != BOOT_DEVICE_UART) |
| 1206 | disable_mcu_watchdog(); |
Marek Behún | 09e16b8 | 2017-06-09 19:28:45 +0200 | [diff] [blame] | 1207 | |
Marek Behún | 09e16b8 | 2017-06-09 19:28:45 +0200 | [diff] [blame] | 1208 | set_regdomain(); |
Marek Behún | 0f2e66a | 2019-05-02 16:53:37 +0200 | [diff] [blame] | 1209 | handle_reset_button(); |
Marek Behún | db1e5c6 | 2019-05-24 14:57:53 +0200 | [diff] [blame] | 1210 | pci_init(); |
Marek Behún | 09e16b8 | 2017-06-09 19:28:45 +0200 | [diff] [blame] | 1211 | |
| 1212 | return 0; |
| 1213 | } |
| 1214 | |
Simon Glass | 629d9b6 | 2023-11-12 19:58:23 -0700 | [diff] [blame] | 1215 | int checkboard(void) |
Marek Behún | 09e16b8 | 2017-06-09 19:28:45 +0200 | [diff] [blame] | 1216 | { |
Marek Behún | 087b235 | 2024-04-04 09:50:55 +0200 | [diff] [blame] | 1217 | char serial[17], version[4], pub_key[67]; |
Marek Behún | c2d19d0 | 2024-04-04 09:50:54 +0200 | [diff] [blame] | 1218 | bool has_version; |
Pali Rohár | 0387f7f | 2022-04-08 16:30:12 +0200 | [diff] [blame] | 1219 | int err; |
Marek Behún | 09e16b8 | 2017-06-09 19:28:45 +0200 | [diff] [blame] | 1220 | |
Pali Rohár | 4798ba9 | 2022-07-29 13:29:06 +0200 | [diff] [blame] | 1221 | printf(" MCU type: %s\n", omnia_get_mcu_type()); |
Pali Rohár | e16cc98 | 2022-08-10 11:00:25 +0200 | [diff] [blame] | 1222 | printf(" MCU version: %s\n", omnia_get_mcu_version()); |
Marek Behún | c4ba72a | 2019-05-02 16:53:34 +0200 | [diff] [blame] | 1223 | printf(" RAM size: %i MiB\n", omnia_get_ram_size_gb() * 1024); |
Marek Behún | c2d19d0 | 2024-04-04 09:50:54 +0200 | [diff] [blame] | 1224 | |
| 1225 | if (omnia_mcu_has_feature(FEAT_BOARD_INFO)) { |
| 1226 | err = omnia_mcu_board_info(serial, NULL, version); |
| 1227 | has_version = !err; |
| 1228 | } else { |
| 1229 | err = turris_atsha_otp_get_serial_number(serial); |
| 1230 | has_version = false; |
| 1231 | } |
| 1232 | |
| 1233 | printf(" Board version: %s\n", has_version ? version : "unknown"); |
Pali Rohár | 38ecdab | 2022-08-27 20:06:30 +0200 | [diff] [blame] | 1234 | printf(" Serial Number: %s\n", !err ? serial : "unknown"); |
Marek Behún | 09e16b8 | 2017-06-09 19:28:45 +0200 | [diff] [blame] | 1235 | |
Marek Behún | 087b235 | 2024-04-04 09:50:55 +0200 | [diff] [blame] | 1236 | if (omnia_mcu_has_feature(FEAT_CRYPTO)) { |
| 1237 | err = omnia_mcu_get_board_public_key(pub_key); |
| 1238 | printf(" ECDSA Public Key: %s\n", !err ? pub_key : "unknown"); |
| 1239 | } |
| 1240 | |
Marek Behún | 09e16b8 | 2017-06-09 19:28:45 +0200 | [diff] [blame] | 1241 | return 0; |
| 1242 | } |
| 1243 | |
Marek Behún | 09e16b8 | 2017-06-09 19:28:45 +0200 | [diff] [blame] | 1244 | int misc_init_r(void) |
| 1245 | { |
Marek Behún | c2d19d0 | 2024-04-04 09:50:54 +0200 | [diff] [blame] | 1246 | if (omnia_mcu_has_feature(FEAT_BOARD_INFO)) { |
| 1247 | char serial[17]; |
| 1248 | u8 first_mac[6]; |
| 1249 | |
| 1250 | if (!omnia_mcu_board_info(serial, first_mac, NULL)) |
| 1251 | turris_init_mac_addresses(1, first_mac); |
| 1252 | } else { |
| 1253 | turris_atsha_otp_init_mac_addresses(1); |
| 1254 | turris_atsha_otp_init_serial_number(); |
| 1255 | } |
| 1256 | |
Marek Behún | 09e16b8 | 2017-06-09 19:28:45 +0200 | [diff] [blame] | 1257 | return 0; |
| 1258 | } |
| 1259 | |
Marek Behún | 91ef59c | 2021-07-15 19:21:02 +0200 | [diff] [blame] | 1260 | #if defined(CONFIG_OF_BOARD_SETUP) |
| 1261 | /* |
| 1262 | * I plan to generalize this function and move it to common/fdt_support.c. |
| 1263 | * This will require some more work on multiple boards, though, so for now leave |
| 1264 | * it here. |
| 1265 | */ |
| 1266 | static bool fixup_mtd_partitions(void *blob, int offset, struct mtd_info *mtd) |
| 1267 | { |
| 1268 | struct mtd_info *slave; |
| 1269 | int parts; |
| 1270 | |
| 1271 | parts = fdt_subnode_offset(blob, offset, "partitions"); |
Pali Rohár | e2b1ba0 | 2022-08-01 12:02:19 +0200 | [diff] [blame] | 1272 | if (parts >= 0) { |
| 1273 | if (fdt_del_node(blob, parts) < 0) |
| 1274 | return false; |
| 1275 | } |
Marek Behún | 91ef59c | 2021-07-15 19:21:02 +0200 | [diff] [blame] | 1276 | |
Pali Rohár | d35b6f2 | 2022-08-01 12:02:20 +0200 | [diff] [blame] | 1277 | if (fdt_increase_size(blob, 512) < 0) |
| 1278 | return false; |
| 1279 | |
Marek Behún | 91ef59c | 2021-07-15 19:21:02 +0200 | [diff] [blame] | 1280 | parts = fdt_add_subnode(blob, offset, "partitions"); |
| 1281 | if (parts < 0) |
| 1282 | return false; |
| 1283 | |
| 1284 | if (fdt_setprop_u32(blob, parts, "#address-cells", 1) < 0) |
| 1285 | return false; |
| 1286 | |
| 1287 | if (fdt_setprop_u32(blob, parts, "#size-cells", 1) < 0) |
| 1288 | return false; |
| 1289 | |
| 1290 | if (fdt_setprop_string(blob, parts, "compatible", |
| 1291 | "fixed-partitions") < 0) |
| 1292 | return false; |
| 1293 | |
| 1294 | mtd_probe_devices(); |
| 1295 | |
Pali Rohár | d8210ef | 2021-10-21 17:55:48 +0200 | [diff] [blame] | 1296 | list_for_each_entry_reverse(slave, &mtd->partitions, node) { |
Marek Behún | 91ef59c | 2021-07-15 19:21:02 +0200 | [diff] [blame] | 1297 | char name[32]; |
| 1298 | int part; |
| 1299 | |
| 1300 | snprintf(name, sizeof(name), "partition@%llx", slave->offset); |
| 1301 | part = fdt_add_subnode(blob, parts, name); |
| 1302 | if (part < 0) |
| 1303 | return false; |
| 1304 | |
| 1305 | if (fdt_setprop_u32(blob, part, "reg", slave->offset) < 0) |
| 1306 | return false; |
| 1307 | |
| 1308 | if (fdt_appendprop_u32(blob, part, "reg", slave->size) < 0) |
| 1309 | return false; |
| 1310 | |
| 1311 | if (fdt_setprop_string(blob, part, "label", slave->name) < 0) |
| 1312 | return false; |
| 1313 | |
| 1314 | if (!(slave->flags & MTD_WRITEABLE)) |
| 1315 | if (fdt_setprop_empty(blob, part, "read-only") < 0) |
| 1316 | return false; |
| 1317 | |
| 1318 | if (slave->flags & MTD_POWERUP_LOCK) |
| 1319 | if (fdt_setprop_empty(blob, part, "lock") < 0) |
| 1320 | return false; |
| 1321 | } |
| 1322 | |
| 1323 | return true; |
| 1324 | } |
| 1325 | |
Pali Rohár | cbda3e2 | 2022-01-10 11:47:18 +0100 | [diff] [blame] | 1326 | static void fixup_spi_nor_partitions(void *blob) |
Marek Behún | 91ef59c | 2021-07-15 19:21:02 +0200 | [diff] [blame] | 1327 | { |
Pali Rohár | 3215c03 | 2022-08-01 23:58:42 +0200 | [diff] [blame] | 1328 | struct mtd_info *mtd = NULL; |
| 1329 | char mtd_path[64]; |
Marek Behún | 91ef59c | 2021-07-15 19:21:02 +0200 | [diff] [blame] | 1330 | int node; |
| 1331 | |
Pali Rohár | 3215c03 | 2022-08-01 23:58:42 +0200 | [diff] [blame] | 1332 | node = fdt_node_offset_by_compatible(gd->fdt_blob, -1, "jedec,spi-nor"); |
| 1333 | if (node < 0) |
| 1334 | goto fail; |
| 1335 | |
| 1336 | if (fdt_get_path(gd->fdt_blob, node, mtd_path, sizeof(mtd_path)) < 0) |
| 1337 | goto fail; |
| 1338 | |
| 1339 | mtd = get_mtd_device_nm(mtd_path); |
Marek Behún | 91ef59c | 2021-07-15 19:21:02 +0200 | [diff] [blame] | 1340 | if (IS_ERR_OR_NULL(mtd)) |
| 1341 | goto fail; |
| 1342 | |
Pali Rohár | 3215c03 | 2022-08-01 23:58:42 +0200 | [diff] [blame] | 1343 | node = fdt_node_offset_by_compatible(blob, -1, "jedec,spi-nor"); |
Marek Behún | 91ef59c | 2021-07-15 19:21:02 +0200 | [diff] [blame] | 1344 | if (node < 0) |
| 1345 | goto fail; |
| 1346 | |
| 1347 | if (!fixup_mtd_partitions(blob, node, mtd)) |
| 1348 | goto fail; |
| 1349 | |
Marek Behún | 36feac9 | 2021-09-25 02:49:18 +0200 | [diff] [blame] | 1350 | put_mtd_device(mtd); |
Pali Rohár | cbda3e2 | 2022-01-10 11:47:18 +0100 | [diff] [blame] | 1351 | return; |
Marek Behún | 91ef59c | 2021-07-15 19:21:02 +0200 | [diff] [blame] | 1352 | |
| 1353 | fail: |
| 1354 | printf("Failed fixing SPI NOR partitions!\n"); |
Marek Behún | 36feac9 | 2021-09-25 02:49:18 +0200 | [diff] [blame] | 1355 | if (!IS_ERR_OR_NULL(mtd)) |
| 1356 | put_mtd_device(mtd); |
Pali Rohár | cbda3e2 | 2022-01-10 11:47:18 +0100 | [diff] [blame] | 1357 | } |
| 1358 | |
| 1359 | int ft_board_setup(void *blob, struct bd_info *bd) |
| 1360 | { |
Pali Rohár | 1e0a975 | 2022-07-29 13:29:07 +0200 | [diff] [blame] | 1361 | int node; |
| 1362 | |
| 1363 | /* |
Marek Behún | 85e223e | 2024-06-18 17:34:30 +0200 | [diff] [blame] | 1364 | * U-Boot's FDT blob contains reset-gpios in ethernet2 PHY node when MCU |
| 1365 | * controls all peripherals resets. |
Pali Rohár | 1e0a975 | 2022-07-29 13:29:07 +0200 | [diff] [blame] | 1366 | * Fixup MCU GPIO nodes in PCIe and eth wan nodes in this case. |
| 1367 | */ |
Marek Behún | 85e223e | 2024-06-18 17:34:30 +0200 | [diff] [blame] | 1368 | node = get_phy_wan_node_offset(gd->fdt_blob); |
| 1369 | if (node >= 0 && fdt_getprop(gd->fdt_blob, node, "reset-gpios", NULL)) { |
Pali Rohár | 1e0a975 | 2022-07-29 13:29:07 +0200 | [diff] [blame] | 1370 | fixup_mcu_gpio_in_pcie_nodes(blob); |
Marek Behún | 85e223e | 2024-06-18 17:34:30 +0200 | [diff] [blame] | 1371 | fixup_mcu_gpio_in_phy_wan_node(blob); |
Pali Rohár | 1e0a975 | 2022-07-29 13:29:07 +0200 | [diff] [blame] | 1372 | } |
| 1373 | |
Pali Rohár | cbda3e2 | 2022-01-10 11:47:18 +0100 | [diff] [blame] | 1374 | fixup_spi_nor_partitions(blob); |
Pali Rohár | 7cd4173 | 2022-03-02 12:47:56 +0100 | [diff] [blame] | 1375 | fixup_msata_port_nodes(blob); |
Pali Rohár | 93a89c5 | 2022-03-02 12:47:58 +0100 | [diff] [blame] | 1376 | fixup_wwan_port_nodes(blob); |
Pali Rohár | cbda3e2 | 2022-01-10 11:47:18 +0100 | [diff] [blame] | 1377 | |
Marek Behún | 4cf5a03 | 2024-04-04 09:50:56 +0200 | [diff] [blame] | 1378 | fixup_atsha_node(blob); |
| 1379 | |
Marek Behún | 91ef59c | 2021-07-15 19:21:02 +0200 | [diff] [blame] | 1380 | return 0; |
| 1381 | } |
| 1382 | #endif |