blob: 544784e860e1e6cba488d577648ba9e3e0631f89 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Marek Behún09e16b82017-06-09 19:28:45 +02002/*
Marek Behúnd63726e2022-06-01 17:17:06 +02003 * Copyright (C) 2017 Marek Behún <kabel@kernel.org>
Marek Behún09e16b82017-06-09 19:28:45 +02004 * 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ún09e16b82017-06-09 19:28:45 +02008 */
9
Tom Rinidec7ea02024-05-20 13:35:03 -060010#include <config.h>
Simon Glass07dc93c2019-08-01 09:46:47 -060011#include <env.h>
Marek Behún09e16b82017-06-09 19:28:45 +020012#include <i2c.h>
Simon Glassa7b51302019-11-14 12:57:46 -070013#include <init.h>
Simon Glass0f2af882020-05-10 11:40:05 -060014#include <log.h>
Marek Behún09e16b82017-06-09 19:28:45 +020015#include <miiphy.h>
Marek Behún91ef59c2021-07-15 19:21:02 +020016#include <mtd.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060017#include <asm/global_data.h>
Marek Behún09e16b82017-06-09 19:28:45 +020018#include <asm/io.h>
19#include <asm/arch/cpu.h>
20#include <asm/arch/soc.h>
Marek Behúnc2d19d02024-04-04 09:50:54 +020021#include <asm/unaligned.h>
Marek Behún09e16b82017-06-09 19:28:45 +020022#include <dm/uclass.h>
Pali Rohár1e0a9752022-07-29 13:29:07 +020023#include <dt-bindings/gpio/gpio.h>
Marek Behún09e16b82017-06-09 19:28:45 +020024#include <fdt_support.h>
Pali Roháre16cc982022-08-10 11:00:25 +020025#include <hexdump.h>
Marek Behún09e16b82017-06-09 19:28:45 +020026#include <time.h>
Marek Behúnbb42a5b2024-04-04 09:50:51 +020027#include <turris-omnia-mcu-interface.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060028#include <linux/bitops.h>
Marek Behúnc2d19d02024-04-04 09:50:54 +020029#include <linux/bitrev.h>
Pali Rohár1e0a9752022-07-29 13:29:07 +020030#include <linux/delay.h>
Simon Glass48b6c6b2019-11-14 12:57:16 -070031#include <u-boot/crc.h>
Marek Behún09e16b82017-06-09 19:28:45 +020032
Chris Packham1a07d212018-05-10 13:28:29 +120033#include "../drivers/ddr/marvell/a38x/ddr3_init.h"
Marek Behún09e16b82017-06-09 19:28:45 +020034#include <../serdes/a38x/high_speed_env_spec.h>
Pali Rohár0387f7f2022-04-08 16:30:12 +020035#include "../turris_atsha_otp.h"
Marek Behúnc2d19d02024-04-04 09:50:54 +020036#include "../turris_common.h"
Marek Behún09e16b82017-06-09 19:28:45 +020037
38DECLARE_GLOBAL_DATA_PTR;
39
Marek Behúnba53b6b2019-05-02 16:53:30 +020040#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ún09e16b82017-06-09 19:28:45 +020047#define OMNIA_I2C_EEPROM_MAGIC 0x0341a034
48
Pali Rohár30e398d2022-04-29 13:53:25 +020049#define A385_SYS_RSTOUT_MASK MVEBU_REGISTER(0x18260)
50#define A385_SYS_RSTOUT_MASK_WD BIT(10)
Pali Rohár7fcda0c2021-11-09 17:14:02 +010051
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ún09e16b82017-06-09 19:28:45 +020066/*
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ár3c4dd982022-03-02 12:47:54 +010083static struct serdes_map board_serdes_map[] = {
Marek Behún09e16b82017-06-09 19:28:45 +020084 {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únba53b6b2019-05-02 16:53:30 +020092static struct udevice *omnia_get_i2c_chip(const char *name, uint addr,
93 uint offset_len)
Marek Behún09e16b82017-06-09 19:28:45 +020094{
95 struct udevice *bus, *dev;
Marek Behúnba53b6b2019-05-02 16:53:30 +020096 int ret;
Marek Behún09e16b82017-06-09 19:28:45 +020097
Marek Behúnba53b6b2019-05-02 16:53:30 +020098 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ún09e16b82017-06-09 19:28:45 +0200103 }
104
Marek Behúnba53b6b2019-05-02 16:53:30 +0200105 ret = i2c_get_chip(bus, addr, offset_len, &dev);
Marek Behún09e16b82017-06-09 19:28:45 +0200106 if (ret) {
Marek Behúnba53b6b2019-05-02 16:53:30 +0200107 printf("Cannot get %s I2C chip: i2c_get_chip failed: %i\n",
108 name, ret);
109 return NULL;
Marek Behún09e16b82017-06-09 19:28:45 +0200110 }
111
Marek Behúnba53b6b2019-05-02 16:53:30 +0200112 return dev;
113}
Marek Behúnd0b374d2017-08-04 15:28:25 +0200114
Marek Behúnba53b6b2019-05-02 16:53:30 +0200115static 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únba53b6b2019-05-02 16:53:30 +0200127static 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ún8b52b8c2024-04-04 09:50:53 +0200139static 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
177static int omnia_mcu_get_sts(u16 *sts)
178{
179 return omnia_mcu_get_sts_and_features(sts, NULL);
180}
181
182static 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únc2d19d02024-04-04 09:50:54 +0200192static 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 */
209static 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ún087b2352024-04-04 09:50:55 +0200256static 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ár7fcda0c2021-11-09 17:14:02 +0100274static 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ár30e398d2022-04-29 13:53:25 +0200312 clrbits_32(A385_SYS_RSTOUT_MASK, A385_SYS_RSTOUT_MASK_WD);
Pali Rohár7fcda0c2021-11-09 17:14:02 +0100313}
314
Marek Behúnba53b6b2019-05-02 16:53:30 +0200315static bool disable_mcu_watchdog(void)
316{
317 int ret;
318
319 puts("Disabling MCU watchdog... ");
320
Marek Behúnbb42a5b2024-04-04 09:50:51 +0200321 ret = omnia_mcu_write(CMD_SET_WATCHDOG_STATE, "\x00", 1);
Marek Behúnba53b6b2019-05-02 16:53:30 +0200322 if (ret) {
323 printf("omnia_mcu_write failed: %i\n", ret);
Marek Behún09e16b82017-06-09 19:28:45 +0200324 return false;
325 }
326
Marek Behúnba53b6b2019-05-02 16:53:30 +0200327 puts("disabled\n");
328
329 return true;
330}
Marek Behúnba53b6b2019-05-02 16:53:30 +0200331
Pali Rohárf8f305b2022-03-02 12:47:55 +0100332static bool omnia_detect_sata(const char *msata_slot)
Marek Behúnba53b6b2019-05-02 16:53:30 +0200333{
334 int ret;
Marek Behún8b52b8c2024-04-04 09:50:53 +0200335 u16 sts;
Marek Behúnba53b6b2019-05-02 16:53:30 +0200336
337 puts("MiniPCIe/mSATA card detection... ");
338
Pali Rohárf8f305b2022-03-02 12:47:55 +0100339 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ún8b52b8c2024-04-04 09:50:53 +0200351 ret = omnia_mcu_get_sts(&sts);
Marek Behúnba53b6b2019-05-02 16:53:30 +0200352 if (ret) {
353 printf("omnia_mcu_read failed: %i, defaulting to MiniPCIe card\n",
354 ret);
Marek Behún09e16b82017-06-09 19:28:45 +0200355 return false;
356 }
357
Marek Behún8b52b8c2024-04-04 09:50:53 +0200358 if (!(sts & STS_CARD_DET)) {
Marek Behúnba53b6b2019-05-02 16:53:30 +0200359 puts("none\n");
Marek Behún09e16b82017-06-09 19:28:45 +0200360 return false;
361 }
Marek Behúnba53b6b2019-05-02 16:53:30 +0200362
Marek Behún8b52b8c2024-04-04 09:50:53 +0200363 if (sts & STS_MSATA_IND)
Marek Behúnba53b6b2019-05-02 16:53:30 +0200364 puts("mSATA\n");
365 else
366 puts("MiniPCIe\n");
367
Marek Behún8b52b8c2024-04-04 09:50:53 +0200368 return sts & STS_MSATA_IND;
Marek Behún09e16b82017-06-09 19:28:45 +0200369}
370
Pali Rohár93a89c52022-03-02 12:47:58 +0100371static 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ún09e16b82017-06-09 19:28:45 +0200387int hws_board_topology_load(struct serdes_map **serdes_map_array, u8 *count)
388{
Pali Rohárf8f305b2022-03-02 12:47:55 +0100389#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ár3c4dd982022-03-02 12:47:54 +0100401 /* 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ún09e16b82017-06-09 19:28:45 +0200405 }
406
Pali Rohár93a89c52022-03-02 12:47:58 +0100407#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ár3c4dd982022-03-02 12:47:54 +0100421 *serdes_map_array = board_serdes_map;
422 *count = ARRAY_SIZE(board_serdes_map);
423
Marek Behún09e16b82017-06-09 19:28:45 +0200424 return 0;
425}
426
427struct omnia_eeprom {
428 u32 magic;
429 u32 ramsize;
430 char region[4];
431 u32 crc;
Marek Behúnc7548162024-06-18 17:34:33 +0200432
433 /* second part (only considered if crc2 is not all-ones) */
Marek Behún9535f792024-06-18 17:34:34 +0200434 char ddr_speed[5];
435 u8 reserved[39];
Marek Behúnc7548162024-06-18 17:34:33 +0200436 u32 crc2;
Marek Behún09e16b82017-06-09 19:28:45 +0200437};
438
Marek Behúnc7548162024-06-18 17:34:33 +0200439static bool is_omnia_eeprom_second_part_valid(const struct omnia_eeprom *oep)
440{
441 return oep->crc2 != 0xffffffff;
442}
443
444static void make_omnia_eeprom_second_part_invalid(struct omnia_eeprom *oep)
445{
446 oep->crc2 = 0xffffffff;
447}
448
449static bool check_eeprom_crc(const void *buf, size_t size, u32 expected,
450 const char *name)
451{
452 u32 crc;
453
454 crc = crc32(0, buf, size);
455 if (crc != expected) {
456 printf("bad %s EEPROM CRC (stored %08x, computed %08x)\n",
457 name, expected, crc);
458 return false;
459 }
460
461 return true;
462}
463
Marek Behún09e16b82017-06-09 19:28:45 +0200464static bool omnia_read_eeprom(struct omnia_eeprom *oep)
465{
Marek Behúnba53b6b2019-05-02 16:53:30 +0200466 struct udevice *chip;
Marek Behúnba53b6b2019-05-02 16:53:30 +0200467 int ret;
Marek Behún09e16b82017-06-09 19:28:45 +0200468
Marek Behúnba53b6b2019-05-02 16:53:30 +0200469 chip = omnia_get_i2c_chip("EEPROM", OMNIA_I2C_EEPROM_CHIP_ADDR,
470 OMNIA_I2C_EEPROM_CHIP_LEN);
471
472 if (!chip)
Marek Behún09e16b82017-06-09 19:28:45 +0200473 return false;
Marek Behún09e16b82017-06-09 19:28:45 +0200474
Marek Behúnba53b6b2019-05-02 16:53:30 +0200475 ret = dm_i2c_read(chip, 0, (void *)oep, sizeof(*oep));
Marek Behún09e16b82017-06-09 19:28:45 +0200476 if (ret) {
Marek Behúnba53b6b2019-05-02 16:53:30 +0200477 printf("dm_i2c_read failed: %i, cannot read EEPROM\n", ret);
Marek Behún09e16b82017-06-09 19:28:45 +0200478 return false;
479 }
480
Marek Behúnba53b6b2019-05-02 16:53:30 +0200481 if (oep->magic != OMNIA_I2C_EEPROM_MAGIC) {
482 printf("bad EEPROM magic number (%08x, should be %08x)\n",
483 oep->magic, OMNIA_I2C_EEPROM_MAGIC);
484 return false;
Marek Behún09e16b82017-06-09 19:28:45 +0200485 }
486
Marek Behúnc7548162024-06-18 17:34:33 +0200487 if (!check_eeprom_crc(oep, offsetof(struct omnia_eeprom, crc), oep->crc,
488 "first"))
Marek Behún09e16b82017-06-09 19:28:45 +0200489 return false;
Marek Behúnc7548162024-06-18 17:34:33 +0200490
491 if (is_omnia_eeprom_second_part_valid(oep) &&
492 !check_eeprom_crc(oep, offsetof(struct omnia_eeprom, crc2),
493 oep->crc2, "second"))
494 make_omnia_eeprom_second_part_invalid(oep);
Marek Behún09e16b82017-06-09 19:28:45 +0200495
496 return true;
497}
498
Marek Behún77652c72019-05-02 16:53:33 +0200499static int omnia_get_ram_size_gb(void)
500{
501 static int ram_size;
502 struct omnia_eeprom oep;
503
504 if (!ram_size) {
505 /* Get the board config from EEPROM */
506 if (omnia_read_eeprom(&oep)) {
507 debug("Memory config in EEPROM: 0x%02x\n", oep.ramsize);
508
509 if (oep.ramsize == 0x2)
510 ram_size = 2;
511 else
512 ram_size = 1;
513 } else {
514 /* Hardcoded fallback */
515 puts("Memory config from EEPROM read failed!\n");
516 puts("Falling back to default 1 GiB!\n");
517 ram_size = 1;
518 }
519 }
520
521 return ram_size;
522}
523
Marek Behún9535f792024-06-18 17:34:34 +0200524static const char *omnia_get_ddr_speed(void)
525{
526 struct omnia_eeprom oep;
527 static char speed[sizeof(oep.ddr_speed) + 1];
528
529 if (!omnia_read_eeprom(&oep))
530 return NULL;
531
532 if (!is_omnia_eeprom_second_part_valid(&oep))
533 return NULL;
534
535 if (!oep.ddr_speed[0] || oep.ddr_speed[0] == 0xff)
536 return NULL;
537
538 memcpy(&speed, &oep.ddr_speed, sizeof(oep.ddr_speed));
539 speed[sizeof(speed) - 1] = '\0';
540
541 return speed;
542}
543
Pali Rohár4798ba92022-07-29 13:29:06 +0200544static const char * const omnia_get_mcu_type(void)
545{
Marek Behúnbb42a5b2024-04-04 09:50:51 +0200546 static char result[] = "xxxxxxx (with peripheral resets)";
Marek Behún8b52b8c2024-04-04 09:50:53 +0200547 u16 sts;
Pali Rohár4798ba92022-07-29 13:29:06 +0200548 int ret;
549
Marek Behún8b52b8c2024-04-04 09:50:53 +0200550 ret = omnia_mcu_get_sts(&sts);
Pali Rohár4798ba92022-07-29 13:29:06 +0200551 if (ret)
552 return "unknown";
553
Marek Behún8b52b8c2024-04-04 09:50:53 +0200554 switch (sts & STS_MCU_TYPE_MASK) {
Marek Behúnbb42a5b2024-04-04 09:50:51 +0200555 case STS_MCU_TYPE_STM32:
556 strcpy(result, "STM32");
557 break;
558 case STS_MCU_TYPE_GD32:
559 strcpy(result, "GD32");
560 break;
561 case STS_MCU_TYPE_MKL:
562 strcpy(result, "MKL");
563 break;
564 default:
565 strcpy(result, "unknown");
566 break;
567 }
568
Marek Behún8b52b8c2024-04-04 09:50:53 +0200569 if (omnia_mcu_has_feature(FEAT_PERIPH_MCU))
570 strcat(result, " (with peripheral resets)");
Pali Rohár4798ba92022-07-29 13:29:06 +0200571
Marek Behúnbb42a5b2024-04-04 09:50:51 +0200572 return result;
Pali Rohár4798ba92022-07-29 13:29:06 +0200573}
574
Pali Roháre16cc982022-08-10 11:00:25 +0200575static const char * const omnia_get_mcu_version(void)
576{
577 static char version[82];
578 u8 version_app[20];
579 u8 version_boot[20];
580 int ret;
581
582 ret = omnia_mcu_read(CMD_GET_FW_VERSION_APP, &version_app, sizeof(version_app));
583 if (ret)
584 return "unknown";
585
586 ret = omnia_mcu_read(CMD_GET_FW_VERSION_BOOT, &version_boot, sizeof(version_boot));
587 if (ret)
588 return "unknown";
589
590 /*
591 * If git commits of MCU bootloader and MCU application are same then
592 * show version only once. If they are different then show both commits.
593 */
594 if (!memcmp(version_app, version_boot, 20)) {
595 bin2hex(version, version_app, 20);
596 version[40] = '\0';
597 } else {
598 bin2hex(version, version_boot, 20);
599 version[40] = '/';
600 bin2hex(version + 41, version_app, 20);
601 version[81] = '\0';
602 }
603
604 return version;
605}
606
Marek Behún09e16b82017-06-09 19:28:45 +0200607/*
608 * Define the DDR layout / topology here in the board file. This will
609 * be used by the DDR3 init code in the SPL U-Boot version to configure
610 * the DDR3 controller.
611 */
Chris Packham1a07d212018-05-10 13:28:29 +1200612static struct mv_ddr_topology_map board_topology_map_1g = {
613 DEBUG_LEVEL_ERROR,
Marek Behún09e16b82017-06-09 19:28:45 +0200614 0x1, /* active interfaces */
615 /* cs_mask, mirror, dqs_swap, ck_swap X PUPs */
616 { { { {0x1, 0, 0, 0},
617 {0x1, 0, 0, 0},
618 {0x1, 0, 0, 0},
619 {0x1, 0, 0, 0},
620 {0x1, 0, 0, 0} },
621 SPEED_BIN_DDR_1600K, /* speed_bin */
Chris Packham1a07d212018-05-10 13:28:29 +1200622 MV_DDR_DEV_WIDTH_16BIT, /* memory_width */
623 MV_DDR_DIE_CAP_4GBIT, /* mem_size */
Chris Packham4bf81db2018-12-03 14:26:49 +1300624 MV_DDR_FREQ_800, /* frequency */
Chris Packhamdd092bd2017-11-29 10:38:34 +1300625 0, 0, /* cas_wl cas_l */
Chris Packham3a09e132018-05-10 13:28:30 +1200626 MV_DDR_TEMP_NORMAL, /* temperature */
627 MV_DDR_TIM_2T} }, /* timing */
Chris Packham1a07d212018-05-10 13:28:29 +1200628 BUS_MASK_32BIT, /* Busses mask */
629 MV_DDR_CFG_DEFAULT, /* ddr configuration data source */
Moti Buskila498475e2021-02-19 17:11:19 +0100630 NOT_COMBINED, /* ddr twin-die combined */
Chris Packham1a07d212018-05-10 13:28:29 +1200631 { {0} }, /* raw spd data */
632 {0} /* timing parameters */
Marek Behún09e16b82017-06-09 19:28:45 +0200633};
634
Chris Packham1a07d212018-05-10 13:28:29 +1200635static struct mv_ddr_topology_map board_topology_map_2g = {
636 DEBUG_LEVEL_ERROR,
Marek Behún09e16b82017-06-09 19:28:45 +0200637 0x1, /* active interfaces */
638 /* cs_mask, mirror, dqs_swap, ck_swap X PUPs */
639 { { { {0x1, 0, 0, 0},
640 {0x1, 0, 0, 0},
641 {0x1, 0, 0, 0},
642 {0x1, 0, 0, 0},
643 {0x1, 0, 0, 0} },
644 SPEED_BIN_DDR_1600K, /* speed_bin */
Chris Packham1a07d212018-05-10 13:28:29 +1200645 MV_DDR_DEV_WIDTH_16BIT, /* memory_width */
646 MV_DDR_DIE_CAP_8GBIT, /* mem_size */
Chris Packham4bf81db2018-12-03 14:26:49 +1300647 MV_DDR_FREQ_800, /* frequency */
Chris Packhamdd092bd2017-11-29 10:38:34 +1300648 0, 0, /* cas_wl cas_l */
Chris Packham3a09e132018-05-10 13:28:30 +1200649 MV_DDR_TEMP_NORMAL, /* temperature */
650 MV_DDR_TIM_2T} }, /* timing */
Chris Packham1a07d212018-05-10 13:28:29 +1200651 BUS_MASK_32BIT, /* Busses mask */
652 MV_DDR_CFG_DEFAULT, /* ddr configuration data source */
Moti Buskila498475e2021-02-19 17:11:19 +0100653 NOT_COMBINED, /* ddr twin-die combined */
Chris Packham1a07d212018-05-10 13:28:29 +1200654 { {0} }, /* raw spd data */
655 {0} /* timing parameters */
Marek Behún09e16b82017-06-09 19:28:45 +0200656};
657
Marek Behún9535f792024-06-18 17:34:34 +0200658static const struct omnia_ddr_speed {
659 char name[5];
660 u8 speed_bin;
661 u8 freq;
662} omnia_ddr_speeds[] = {
663 { "1066F", SPEED_BIN_DDR_1066F, MV_DDR_FREQ_533 },
664 { "1333H", SPEED_BIN_DDR_1333H, MV_DDR_FREQ_667 },
665 { "1600K", SPEED_BIN_DDR_1600K, MV_DDR_FREQ_800 },
666};
667
668static const struct omnia_ddr_speed *find_ddr_speed_setting(const char *name)
669{
670 for (int i = 0; i < ARRAY_SIZE(omnia_ddr_speeds); ++i)
671 if (!strncmp(name, omnia_ddr_speeds[i].name, 5))
672 return &omnia_ddr_speeds[i];
673
674 return NULL;
675}
676
677bool omnia_valid_ddr_speed(const char *name)
678{
679 return find_ddr_speed_setting(name) != NULL;
680}
681
682void omnia_print_ddr_speeds(void)
683{
684 for (int i = 0; i < ARRAY_SIZE(omnia_ddr_speeds); ++i)
685 printf("%.5s%s", omnia_ddr_speeds[i].name,
686 i == ARRAY_SIZE(omnia_ddr_speeds) - 1 ? "\n" : ", ");
687}
688
689static void fixup_speed_in_ddr_topology(struct mv_ddr_topology_map *topology)
690{
691 typeof(topology->interface_params[0]) *params;
692 const struct omnia_ddr_speed *setting;
693 const char *speed;
694 static bool done;
695
696 if (done)
697 return;
698
699 done = true;
700
701 speed = omnia_get_ddr_speed();
702 if (!speed)
703 return;
704
705 setting = find_ddr_speed_setting(speed);
706 if (!setting) {
707 printf("Unsupported value %s for DDR3 speed in EEPROM!\n",
708 speed);
709 return;
710 }
711
712 params = &topology->interface_params[0];
713
714 /* don't inform if we are not changing the speed from the default one */
715 if (params->speed_bin_index == setting->speed_bin)
716 return;
717
718 printf("Fixing up DDR3 speed (EEPROM defines %s)\n", speed);
719
720 params->speed_bin_index = setting->speed_bin;
721 params->memory_freq = setting->freq;
722}
723
Chris Packham1a07d212018-05-10 13:28:29 +1200724struct mv_ddr_topology_map *mv_ddr_topology_map_get(void)
Marek Behún09e16b82017-06-09 19:28:45 +0200725{
Marek Behún9535f792024-06-18 17:34:34 +0200726 struct mv_ddr_topology_map *topology;
727
Marek Behún77652c72019-05-02 16:53:33 +0200728 if (omnia_get_ram_size_gb() == 2)
Marek Behún9535f792024-06-18 17:34:34 +0200729 topology = &board_topology_map_2g;
Marek Behún77652c72019-05-02 16:53:33 +0200730 else
Marek Behún9535f792024-06-18 17:34:34 +0200731 topology = &board_topology_map_1g;
732
733 fixup_speed_in_ddr_topology(topology);
734
735 return topology;
Marek Behún09e16b82017-06-09 19:28:45 +0200736}
737
Marek Behún09e16b82017-06-09 19:28:45 +0200738static int set_regdomain(void)
739{
740 struct omnia_eeprom oep;
741 char rd[3] = {' ', ' ', 0};
742
743 if (omnia_read_eeprom(&oep))
744 memcpy(rd, &oep.region, 2);
745 else
746 puts("EEPROM regdomain read failed.\n");
747
748 printf("Regdomain set to %s\n", rd);
Simon Glass6a38e412017-08-03 12:22:09 -0600749 return env_set("regdomain", rd);
Marek Behún09e16b82017-06-09 19:28:45 +0200750}
Marek Behún0f2e66a2019-05-02 16:53:37 +0200751
Marek Behún0f2e66a2019-05-02 16:53:37 +0200752static void handle_reset_button(void)
753{
Pali Rohár905c3bf2021-06-14 16:45:58 +0200754 const char * const vars[1] = { "bootcmd_rescue", };
Marek Behún0f2e66a2019-05-02 16:53:37 +0200755 int ret;
756 u8 reset_status;
757
Pali Rohár905c3bf2021-06-14 16:45:58 +0200758 /*
759 * Ensure that bootcmd_rescue has always stock value, so that running
760 * run bootcmd_rescue
761 * always works correctly.
762 */
763 env_set_default_vars(1, (char * const *)vars, 0);
764
Marek Behún0f2e66a2019-05-02 16:53:37 +0200765 ret = omnia_mcu_read(CMD_GET_RESET, &reset_status, 1);
766 if (ret) {
767 printf("omnia_mcu_read failed: %i, reset status unknown!\n",
768 ret);
769 return;
770 }
771
772 env_set_ulong("omnia_reset", reset_status);
773
774 if (reset_status) {
Pali Rohár8adec652022-08-27 20:49:20 +0200775 const char * const vars[3] = {
Marek Behún09f8de22021-05-28 10:00:49 +0200776 "bootcmd",
Pali Rohár8adec652022-08-27 20:49:20 +0200777 "bootdelay",
Marek Behún09f8de22021-05-28 10:00:49 +0200778 "distro_bootcmd",
779 };
780
781 /*
782 * Set the above envs to their default values, in case the user
783 * managed to break them.
784 */
Pali Rohár8adec652022-08-27 20:49:20 +0200785 env_set_default_vars(3, (char * const *)vars, 0);
Marek Behún09f8de22021-05-28 10:00:49 +0200786
787 /* Ensure bootcmd_rescue is used by distroboot */
788 env_set("boot_targets", "rescue");
789
Pali Rohár4f9e6fb2022-04-06 11:39:32 +0200790 printf("RESET button was pressed, overwriting boot_targets!\n");
Marek Behún09f8de22021-05-28 10:00:49 +0200791 } else {
792 /*
793 * In case the user somehow managed to save environment with
794 * boot_targets=rescue, reset boot_targets to default value.
795 * This could happen in subsequent commands if bootcmd_rescue
796 * failed.
797 */
798 if (!strcmp(env_get("boot_targets"), "rescue")) {
799 const char * const vars[1] = {
800 "boot_targets",
801 };
802
803 env_set_default_vars(1, (char * const *)vars, 0);
804 }
Marek Behún0f2e66a2019-05-02 16:53:37 +0200805 }
806}
Marek Behún09e16b82017-06-09 19:28:45 +0200807
Pali Rohár1e0a9752022-07-29 13:29:07 +0200808static void initialize_switch(void)
809{
810 u32 val, val04, val08, val10, val14;
811 u16 ctrl[2];
812 int err;
813
814 printf("Initializing LAN eth switch... ");
815
816 /* Change RGMII pins to GPIO mode */
817
818 val = val04 = readl(MVEBU_MPP_BASE + 0x04);
819 val &= ~GENMASK(19, 16); /* MPP[12] := GPIO */
820 val &= ~GENMASK(23, 20); /* MPP[13] := GPIO */
821 val &= ~GENMASK(27, 24); /* MPP[14] := GPIO */
822 val &= ~GENMASK(31, 28); /* MPP[15] := GPIO */
823 writel(val, MVEBU_MPP_BASE + 0x04);
824
825 val = val08 = readl(MVEBU_MPP_BASE + 0x08);
826 val &= ~GENMASK(3, 0); /* MPP[16] := GPIO */
827 val &= ~GENMASK(23, 20); /* MPP[21] := GPIO */
828 writel(val, MVEBU_MPP_BASE + 0x08);
829
830 val = val10 = readl(MVEBU_MPP_BASE + 0x10);
831 val &= ~GENMASK(27, 24); /* MPP[38] := GPIO */
832 val &= ~GENMASK(31, 28); /* MPP[39] := GPIO */
833 writel(val, MVEBU_MPP_BASE + 0x10);
834
835 val = val14 = readl(MVEBU_MPP_BASE + 0x14);
836 val &= ~GENMASK(3, 0); /* MPP[40] := GPIO */
837 val &= ~GENMASK(7, 4); /* MPP[41] := GPIO */
838 writel(val, MVEBU_MPP_BASE + 0x14);
839
840 /* Set initial values for switch reset strapping pins */
841
842 val = readl(MVEBU_GPIO0_BASE + 0x00);
843 val |= BIT(12); /* GPIO[12] := 1 */
844 val |= BIT(13); /* GPIO[13] := 1 */
845 val |= BIT(14); /* GPIO[14] := 1 */
846 val |= BIT(15); /* GPIO[15] := 1 */
847 val &= ~BIT(16); /* GPIO[16] := 0 */
848 val |= BIT(21); /* GPIO[21] := 1 */
849 writel(val, MVEBU_GPIO0_BASE + 0x00);
850
851 val = readl(MVEBU_GPIO1_BASE + 0x00);
852 val |= BIT(6); /* GPIO[38] := 1 */
853 val |= BIT(7); /* GPIO[39] := 1 */
854 val |= BIT(8); /* GPIO[40] := 1 */
855 val &= ~BIT(9); /* GPIO[41] := 0 */
856 writel(val, MVEBU_GPIO1_BASE + 0x00);
857
858 val = readl(MVEBU_GPIO0_BASE + 0x04);
859 val &= ~BIT(12); /* GPIO[12] := Out Enable */
860 val &= ~BIT(13); /* GPIO[13] := Out Enable */
861 val &= ~BIT(14); /* GPIO[14] := Out Enable */
862 val &= ~BIT(15); /* GPIO[15] := Out Enable */
863 val &= ~BIT(16); /* GPIO[16] := Out Enable */
864 val &= ~BIT(21); /* GPIO[21] := Out Enable */
865 writel(val, MVEBU_GPIO0_BASE + 0x04);
866
867 val = readl(MVEBU_GPIO1_BASE + 0x04);
868 val &= ~BIT(6); /* GPIO[38] := Out Enable */
869 val &= ~BIT(7); /* GPIO[39] := Out Enable */
870 val &= ~BIT(8); /* GPIO[40] := Out Enable */
871 val &= ~BIT(9); /* GPIO[41] := Out Enable */
872 writel(val, MVEBU_GPIO1_BASE + 0x04);
873
874 /* Release switch reset */
875
876 ctrl[0] = EXT_CTL_nRES_LAN;
877 ctrl[1] = EXT_CTL_nRES_LAN;
878 err = omnia_mcu_write(CMD_EXT_CONTROL, ctrl, sizeof(ctrl));
879
Marek Behún59aa4652022-09-13 18:10:28 +0200880 mdelay(50);
Pali Rohár1e0a9752022-07-29 13:29:07 +0200881
882 /* Change RGMII pins back to RGMII mode */
883
884 writel(val04, MVEBU_MPP_BASE + 0x04);
885 writel(val08, MVEBU_MPP_BASE + 0x08);
886 writel(val10, MVEBU_MPP_BASE + 0x10);
887 writel(val14, MVEBU_MPP_BASE + 0x14);
888
889 puts(err ? "failed\n" : "done\n");
890}
891
Marek Behún09e16b82017-06-09 19:28:45 +0200892int board_early_init_f(void)
893{
Marek Behún09e16b82017-06-09 19:28:45 +0200894 /* Configure MPP */
895 writel(0x11111111, MVEBU_MPP_BASE + 0x00);
896 writel(0x11111111, MVEBU_MPP_BASE + 0x04);
897 writel(0x11244011, MVEBU_MPP_BASE + 0x08);
898 writel(0x22222111, MVEBU_MPP_BASE + 0x0c);
899 writel(0x22200002, MVEBU_MPP_BASE + 0x10);
900 writel(0x30042022, MVEBU_MPP_BASE + 0x14);
901 writel(0x55550555, MVEBU_MPP_BASE + 0x18);
902 writel(0x00005550, MVEBU_MPP_BASE + 0x1c);
903
904 /* Set GPP Out value */
905 writel(OMNIA_GPP_OUT_VAL_LOW, MVEBU_GPIO0_BASE + 0x00);
906 writel(OMNIA_GPP_OUT_VAL_MID, MVEBU_GPIO1_BASE + 0x00);
907
908 /* Set GPP Polarity */
909 writel(OMNIA_GPP_POL_LOW, MVEBU_GPIO0_BASE + 0x0c);
910 writel(OMNIA_GPP_POL_MID, MVEBU_GPIO1_BASE + 0x0c);
911
912 /* Set GPP Out Enable */
913 writel(OMNIA_GPP_OUT_ENA_LOW, MVEBU_GPIO0_BASE + 0x04);
914 writel(OMNIA_GPP_OUT_ENA_MID, MVEBU_GPIO1_BASE + 0x04);
915
Marek Behún09e16b82017-06-09 19:28:45 +0200916 return 0;
917}
918
Marek Behúnf3556162021-08-16 15:19:39 +0200919void spl_board_init(void)
920{
921 /*
922 * If booting from UART, disable MCU watchdog in SPL, since uploading
Pali Rohár7fcda0c2021-11-09 17:14:02 +0100923 * U-Boot proper can take too much time and trigger it. Instead enable
924 * A385 watchdog with very high timeout (10 minutes) to prevent hangup.
Marek Behúnf3556162021-08-16 15:19:39 +0200925 */
Pali Rohár7fcda0c2021-11-09 17:14:02 +0100926 if (get_boot_device() == BOOT_DEVICE_UART) {
927 enable_a385_watchdog(10);
Marek Behúnf3556162021-08-16 15:19:39 +0200928 disable_mcu_watchdog();
Pali Rohár7fcda0c2021-11-09 17:14:02 +0100929 }
Pali Rohár1e0a9752022-07-29 13:29:07 +0200930
931 /*
932 * When MCU controls peripheral resets then release LAN eth switch from
933 * the reset and initialize it. When MCU does not control peripheral
934 * resets then LAN eth switch is initialized automatically by bootstrap
935 * pins when A385 is released from the reset.
936 */
Marek Behún8b52b8c2024-04-04 09:50:53 +0200937 if (omnia_mcu_has_feature(FEAT_PERIPH_MCU))
938 initialize_switch();
Marek Behúnf3556162021-08-16 15:19:39 +0200939}
940
Pali Rohárcbda3e22022-01-10 11:47:18 +0100941#if IS_ENABLED(CONFIG_OF_BOARD_FIXUP) || IS_ENABLED(CONFIG_OF_BOARD_SETUP)
942
Pali Rohár7cd41732022-03-02 12:47:56 +0100943static void disable_sata_node(void *blob)
Pali Rohárcbda3e22022-01-10 11:47:18 +0100944{
Pali Rohárcbda3e22022-01-10 11:47:18 +0100945 int node;
946
Pali Rohár7cd41732022-03-02 12:47:56 +0100947 fdt_for_each_node_by_compatible(node, blob, -1, "marvell,armada-380-ahci") {
948 if (!fdtdec_get_is_enabled(blob, node))
949 continue;
950
951 if (fdt_status_disabled(blob, node) < 0)
952 printf("Cannot disable SATA DT node!\n");
953 else
954 debug("Disabled SATA DT node\n");
955
Pali Roháre9105262022-03-02 12:47:57 +0100956 return;
Pali Rohár7cd41732022-03-02 12:47:56 +0100957 }
Pali Roháre9105262022-03-02 12:47:57 +0100958
959 printf("Cannot find SATA DT node!\n");
Pali Rohár7cd41732022-03-02 12:47:56 +0100960}
961
962static void disable_pcie_node(void *blob, int port)
963{
964 int node;
965
966 fdt_for_each_node_by_compatible(node, blob, -1, "marvell,armada-370-pcie") {
967 int port_node;
968
969 if (!fdtdec_get_is_enabled(blob, node))
970 continue;
971
972 fdt_for_each_subnode (port_node, blob, node) {
973 if (!fdtdec_get_is_enabled(blob, port_node))
974 continue;
975
976 if (fdtdec_get_int(blob, port_node, "marvell,pcie-port", -1) != port)
977 continue;
978
979 if (fdt_status_disabled(blob, port_node) < 0)
980 printf("Cannot disable PCIe port %d DT node!\n", port);
981 else
982 debug("Disabled PCIe port %d DT node\n", port);
983
984 return;
985 }
986 }
Pali Roháre9105262022-03-02 12:47:57 +0100987
988 printf("Cannot find PCIe port %d DT node!\n", port);
Pali Rohár7cd41732022-03-02 12:47:56 +0100989}
990
991static void fixup_msata_port_nodes(void *blob)
992{
993 bool mode_sata;
994
Pali Rohárcbda3e22022-01-10 11:47:18 +0100995 /*
996 * Determine if SerDes 0 is configured to SATA mode.
997 * We do this instead of calling omnia_detect_sata() to avoid another
998 * call to the MCU. By this time the common PHYs are initialized (it is
999 * done in SPL), so we can read this common PHY register.
1000 */
1001 mode_sata = (readl(MVEBU_REGISTER(0x183fc)) & GENMASK(3, 0)) == 2;
1002
1003 /*
1004 * We're either adding status = "disabled" property, or changing
1005 * status = "okay" to status = "disabled". In both cases we'll need more
1006 * space. Increase the size a little.
1007 */
1008 if (fdt_increase_size(blob, 32) < 0) {
1009 printf("Cannot increase FDT size!\n");
1010 return;
1011 }
1012
Pali Rohárcbda3e22022-01-10 11:47:18 +01001013 if (!mode_sata) {
Pali Rohár7cd41732022-03-02 12:47:56 +01001014 /* If mSATA card is not present, disable SATA DT node */
1015 disable_sata_node(blob);
1016 } else {
1017 /* Otherwise disable PCIe port 0 DT node (MiniPCIe / mSATA port) */
1018 disable_pcie_node(blob, 0);
Pali Rohárcbda3e22022-01-10 11:47:18 +01001019 }
Pali Rohár93a89c52022-03-02 12:47:58 +01001020}
1021
1022static void fixup_wwan_port_nodes(void *blob)
1023{
1024 bool mode_usb3;
1025
1026 /* Determine if SerDes 4 is configured to USB3 mode */
1027 mode_usb3 = ((readl(MVEBU_REGISTER(0x183fc)) & GENMASK(19, 16)) >> 16) == 4;
1028
1029 /* If SerDes 4 is not configured to USB3 mode then nothing is needed to fixup */
1030 if (!mode_usb3)
1031 return;
1032
1033 /*
1034 * We're either adding status = "disabled" property, or changing
1035 * status = "okay" to status = "disabled". In both cases we'll need more
1036 * space. Increase the size a little.
1037 */
1038 if (fdt_increase_size(blob, 32) < 0) {
1039 printf("Cannot increase FDT size!\n");
1040 return;
1041 }
1042
1043 /* Disable PCIe port 2 DT node (WWAN) */
1044 disable_pcie_node(blob, 2);
Pali Rohárcbda3e22022-01-10 11:47:18 +01001045}
1046
Pali Rohár1e0a9752022-07-29 13:29:07 +02001047static int insert_mcu_gpio_prop(void *blob, int node, const char *prop,
1048 unsigned int phandle, u32 bank, u32 gpio,
1049 u32 flags)
1050{
1051 fdt32_t val[4] = { cpu_to_fdt32(phandle), cpu_to_fdt32(bank),
1052 cpu_to_fdt32(gpio), cpu_to_fdt32(flags) };
1053 return fdt_setprop(blob, node, prop, &val, sizeof(val));
1054}
1055
1056static int fixup_mcu_gpio_in_pcie_nodes(void *blob)
1057{
1058 unsigned int mcu_phandle;
1059 int port, gpio;
1060 int pcie_node;
1061 int port_node;
1062 int ret;
1063
1064 ret = fdt_increase_size(blob, 128);
1065 if (ret < 0) {
1066 printf("Cannot increase FDT size!\n");
1067 return ret;
1068 }
1069
1070 mcu_phandle = fdt_create_phandle_by_compatible(blob, "cznic,turris-omnia-mcu");
1071 if (!mcu_phandle)
1072 return -FDT_ERR_NOPHANDLES;
1073
1074 fdt_for_each_node_by_compatible(pcie_node, blob, -1, "marvell,armada-370-pcie") {
1075 if (!fdtdec_get_is_enabled(blob, pcie_node))
1076 continue;
1077
1078 fdt_for_each_subnode(port_node, blob, pcie_node) {
1079 if (!fdtdec_get_is_enabled(blob, port_node))
1080 continue;
1081
1082 port = fdtdec_get_int(blob, port_node, "marvell,pcie-port", -1);
1083
1084 if (port == 0)
1085 gpio = ilog2(EXT_CTL_nPERST0);
1086 else if (port == 1)
1087 gpio = ilog2(EXT_CTL_nPERST1);
1088 else if (port == 2)
1089 gpio = ilog2(EXT_CTL_nPERST2);
1090 else
1091 continue;
1092
1093 /* insert: reset-gpios = <&mcu 2 gpio GPIO_ACTIVE_LOW>; */
1094 ret = insert_mcu_gpio_prop(blob, port_node, "reset-gpios",
1095 mcu_phandle, 2, gpio, GPIO_ACTIVE_LOW);
1096 if (ret < 0)
1097 return ret;
1098 }
1099 }
1100
1101 return 0;
1102}
1103
Marek Behún85e223e2024-06-18 17:34:30 +02001104static int get_phy_wan_node_offset(const void *blob)
1105{
1106 u32 phy_wan_phandle;
1107
1108 phy_wan_phandle = fdt_getprop_u32_default(blob, "ethernet2", "phy-handle", 0);
1109 if (!phy_wan_phandle)
1110 return -FDT_ERR_NOTFOUND;
1111
1112 return fdt_node_offset_by_phandle(blob, phy_wan_phandle);
1113}
1114
1115static int fixup_mcu_gpio_in_phy_wan_node(void *blob)
Pali Rohár1e0a9752022-07-29 13:29:07 +02001116{
1117 unsigned int mcu_phandle;
Marek Behún85e223e2024-06-18 17:34:30 +02001118 int phy_wan_node, ret;
Pali Rohár1e0a9752022-07-29 13:29:07 +02001119
1120 ret = fdt_increase_size(blob, 64);
1121 if (ret < 0) {
1122 printf("Cannot increase FDT size!\n");
1123 return ret;
1124 }
1125
Marek Behún85e223e2024-06-18 17:34:30 +02001126 phy_wan_node = get_phy_wan_node_offset(blob);
1127 if (phy_wan_node < 0)
1128 return phy_wan_node;
Pali Rohár1e0a9752022-07-29 13:29:07 +02001129
1130 mcu_phandle = fdt_create_phandle_by_compatible(blob, "cznic,turris-omnia-mcu");
1131 if (!mcu_phandle)
1132 return -FDT_ERR_NOPHANDLES;
1133
Marek Behún85e223e2024-06-18 17:34:30 +02001134 /* insert: reset-gpios = <&mcu 2 gpio GPIO_ACTIVE_LOW>; */
1135 return insert_mcu_gpio_prop(blob, phy_wan_node, "reset-gpios",
1136 mcu_phandle, 2, ilog2(EXT_CTL_nRES_PHY), GPIO_ACTIVE_LOW);
Pali Rohár1e0a9752022-07-29 13:29:07 +02001137}
1138
Marek Behún4cf5a032024-04-04 09:50:56 +02001139static void fixup_atsha_node(void *blob)
1140{
1141 int node;
1142
1143 if (!omnia_mcu_has_feature(FEAT_CRYPTO))
1144 return;
1145
1146 node = fdt_node_offset_by_compatible(blob, -1, "atmel,atsha204a");
1147 if (node < 0) {
1148 printf("Cannot find ATSHA204A node!\n");
1149 return;
1150 }
1151
1152 if (fdt_status_disabled(blob, node) < 0)
1153 printf("Cannot disable ATSHA204A node!\n");
1154 else
1155 debug("Disabled ATSHA204A node\n");
1156}
1157
Pali Rohárcbda3e22022-01-10 11:47:18 +01001158#endif
1159
1160#if IS_ENABLED(CONFIG_OF_BOARD_FIXUP)
1161int board_fix_fdt(void *blob)
1162{
Marek Behún8b52b8c2024-04-04 09:50:53 +02001163 if (omnia_mcu_has_feature(FEAT_PERIPH_MCU)) {
1164 fixup_mcu_gpio_in_pcie_nodes(blob);
Marek Behún85e223e2024-06-18 17:34:30 +02001165 fixup_mcu_gpio_in_phy_wan_node(blob);
Pali Rohár1e0a9752022-07-29 13:29:07 +02001166 }
1167
Pali Rohár7cd41732022-03-02 12:47:56 +01001168 fixup_msata_port_nodes(blob);
Pali Rohár93a89c52022-03-02 12:47:58 +01001169 fixup_wwan_port_nodes(blob);
Pali Rohárcbda3e22022-01-10 11:47:18 +01001170
Marek Behún4cf5a032024-04-04 09:50:56 +02001171 fixup_atsha_node(blob);
1172
Pali Rohárcbda3e22022-01-10 11:47:18 +01001173 return 0;
1174}
1175#endif
1176
Marek Behún09e16b82017-06-09 19:28:45 +02001177int board_init(void)
1178{
Marek Behún4dfc57e2019-05-02 16:53:31 +02001179 /* address of boot parameters */
Marek Behún09e16b82017-06-09 19:28:45 +02001180 gd->bd->bi_boot_params = mvebu_sdram_bar(0) + 0x100;
1181
Marek Behún88dc0242021-08-16 15:19:40 +02001182 return 0;
1183}
1184
1185int board_late_init(void)
1186{
Marek Behúnf3556162021-08-16 15:19:39 +02001187 /*
1188 * If not booting from UART, MCU watchdog was not disabled in SPL,
1189 * disable it now.
1190 */
1191 if (get_boot_device() != BOOT_DEVICE_UART)
1192 disable_mcu_watchdog();
Marek Behún09e16b82017-06-09 19:28:45 +02001193
Marek Behún09e16b82017-06-09 19:28:45 +02001194 set_regdomain();
Marek Behún0f2e66a2019-05-02 16:53:37 +02001195 handle_reset_button();
Marek Behúndb1e5c62019-05-24 14:57:53 +02001196 pci_init();
Marek Behún09e16b82017-06-09 19:28:45 +02001197
1198 return 0;
1199}
1200
Simon Glass629d9b62023-11-12 19:58:23 -07001201int checkboard(void)
Marek Behún09e16b82017-06-09 19:28:45 +02001202{
Marek Behún087b2352024-04-04 09:50:55 +02001203 char serial[17], version[4], pub_key[67];
Marek Behúnc2d19d02024-04-04 09:50:54 +02001204 bool has_version;
Pali Rohár0387f7f2022-04-08 16:30:12 +02001205 int err;
Marek Behún09e16b82017-06-09 19:28:45 +02001206
Pali Rohár4798ba92022-07-29 13:29:06 +02001207 printf(" MCU type: %s\n", omnia_get_mcu_type());
Pali Roháre16cc982022-08-10 11:00:25 +02001208 printf(" MCU version: %s\n", omnia_get_mcu_version());
Marek Behúnc4ba72a2019-05-02 16:53:34 +02001209 printf(" RAM size: %i MiB\n", omnia_get_ram_size_gb() * 1024);
Marek Behúnc2d19d02024-04-04 09:50:54 +02001210
1211 if (omnia_mcu_has_feature(FEAT_BOARD_INFO)) {
1212 err = omnia_mcu_board_info(serial, NULL, version);
1213 has_version = !err;
1214 } else {
1215 err = turris_atsha_otp_get_serial_number(serial);
1216 has_version = false;
1217 }
1218
1219 printf(" Board version: %s\n", has_version ? version : "unknown");
Pali Rohár38ecdab2022-08-27 20:06:30 +02001220 printf(" Serial Number: %s\n", !err ? serial : "unknown");
Marek Behún09e16b82017-06-09 19:28:45 +02001221
Marek Behún087b2352024-04-04 09:50:55 +02001222 if (omnia_mcu_has_feature(FEAT_CRYPTO)) {
1223 err = omnia_mcu_get_board_public_key(pub_key);
1224 printf(" ECDSA Public Key: %s\n", !err ? pub_key : "unknown");
1225 }
1226
Marek Behún09e16b82017-06-09 19:28:45 +02001227 return 0;
1228}
1229
Marek Behún09e16b82017-06-09 19:28:45 +02001230int misc_init_r(void)
1231{
Marek Behúnc2d19d02024-04-04 09:50:54 +02001232 if (omnia_mcu_has_feature(FEAT_BOARD_INFO)) {
1233 char serial[17];
1234 u8 first_mac[6];
1235
1236 if (!omnia_mcu_board_info(serial, first_mac, NULL))
1237 turris_init_mac_addresses(1, first_mac);
1238 } else {
1239 turris_atsha_otp_init_mac_addresses(1);
1240 turris_atsha_otp_init_serial_number();
1241 }
1242
Marek Behún09e16b82017-06-09 19:28:45 +02001243 return 0;
1244}
1245
Marek Behún91ef59c2021-07-15 19:21:02 +02001246#if defined(CONFIG_OF_BOARD_SETUP)
1247/*
1248 * I plan to generalize this function and move it to common/fdt_support.c.
1249 * This will require some more work on multiple boards, though, so for now leave
1250 * it here.
1251 */
1252static bool fixup_mtd_partitions(void *blob, int offset, struct mtd_info *mtd)
1253{
1254 struct mtd_info *slave;
1255 int parts;
1256
1257 parts = fdt_subnode_offset(blob, offset, "partitions");
Pali Roháre2b1ba02022-08-01 12:02:19 +02001258 if (parts >= 0) {
1259 if (fdt_del_node(blob, parts) < 0)
1260 return false;
1261 }
Marek Behún91ef59c2021-07-15 19:21:02 +02001262
Pali Rohárd35b6f22022-08-01 12:02:20 +02001263 if (fdt_increase_size(blob, 512) < 0)
1264 return false;
1265
Marek Behún91ef59c2021-07-15 19:21:02 +02001266 parts = fdt_add_subnode(blob, offset, "partitions");
1267 if (parts < 0)
1268 return false;
1269
1270 if (fdt_setprop_u32(blob, parts, "#address-cells", 1) < 0)
1271 return false;
1272
1273 if (fdt_setprop_u32(blob, parts, "#size-cells", 1) < 0)
1274 return false;
1275
1276 if (fdt_setprop_string(blob, parts, "compatible",
1277 "fixed-partitions") < 0)
1278 return false;
1279
1280 mtd_probe_devices();
1281
Pali Rohárd8210ef2021-10-21 17:55:48 +02001282 list_for_each_entry_reverse(slave, &mtd->partitions, node) {
Marek Behún91ef59c2021-07-15 19:21:02 +02001283 char name[32];
1284 int part;
1285
1286 snprintf(name, sizeof(name), "partition@%llx", slave->offset);
1287 part = fdt_add_subnode(blob, parts, name);
1288 if (part < 0)
1289 return false;
1290
1291 if (fdt_setprop_u32(blob, part, "reg", slave->offset) < 0)
1292 return false;
1293
1294 if (fdt_appendprop_u32(blob, part, "reg", slave->size) < 0)
1295 return false;
1296
1297 if (fdt_setprop_string(blob, part, "label", slave->name) < 0)
1298 return false;
1299
1300 if (!(slave->flags & MTD_WRITEABLE))
1301 if (fdt_setprop_empty(blob, part, "read-only") < 0)
1302 return false;
1303
1304 if (slave->flags & MTD_POWERUP_LOCK)
1305 if (fdt_setprop_empty(blob, part, "lock") < 0)
1306 return false;
1307 }
1308
1309 return true;
1310}
1311
Pali Rohárcbda3e22022-01-10 11:47:18 +01001312static void fixup_spi_nor_partitions(void *blob)
Marek Behún91ef59c2021-07-15 19:21:02 +02001313{
Pali Rohár3215c032022-08-01 23:58:42 +02001314 struct mtd_info *mtd = NULL;
1315 char mtd_path[64];
Marek Behún91ef59c2021-07-15 19:21:02 +02001316 int node;
1317
Pali Rohár3215c032022-08-01 23:58:42 +02001318 node = fdt_node_offset_by_compatible(gd->fdt_blob, -1, "jedec,spi-nor");
1319 if (node < 0)
1320 goto fail;
1321
1322 if (fdt_get_path(gd->fdt_blob, node, mtd_path, sizeof(mtd_path)) < 0)
1323 goto fail;
1324
1325 mtd = get_mtd_device_nm(mtd_path);
Marek Behún91ef59c2021-07-15 19:21:02 +02001326 if (IS_ERR_OR_NULL(mtd))
1327 goto fail;
1328
Pali Rohár3215c032022-08-01 23:58:42 +02001329 node = fdt_node_offset_by_compatible(blob, -1, "jedec,spi-nor");
Marek Behún91ef59c2021-07-15 19:21:02 +02001330 if (node < 0)
1331 goto fail;
1332
1333 if (!fixup_mtd_partitions(blob, node, mtd))
1334 goto fail;
1335
Marek Behún36feac92021-09-25 02:49:18 +02001336 put_mtd_device(mtd);
Pali Rohárcbda3e22022-01-10 11:47:18 +01001337 return;
Marek Behún91ef59c2021-07-15 19:21:02 +02001338
1339fail:
1340 printf("Failed fixing SPI NOR partitions!\n");
Marek Behún36feac92021-09-25 02:49:18 +02001341 if (!IS_ERR_OR_NULL(mtd))
1342 put_mtd_device(mtd);
Pali Rohárcbda3e22022-01-10 11:47:18 +01001343}
1344
1345int ft_board_setup(void *blob, struct bd_info *bd)
1346{
Pali Rohár1e0a9752022-07-29 13:29:07 +02001347 int node;
1348
1349 /*
Marek Behún85e223e2024-06-18 17:34:30 +02001350 * U-Boot's FDT blob contains reset-gpios in ethernet2 PHY node when MCU
1351 * controls all peripherals resets.
Pali Rohár1e0a9752022-07-29 13:29:07 +02001352 * Fixup MCU GPIO nodes in PCIe and eth wan nodes in this case.
1353 */
Marek Behún85e223e2024-06-18 17:34:30 +02001354 node = get_phy_wan_node_offset(gd->fdt_blob);
1355 if (node >= 0 && fdt_getprop(gd->fdt_blob, node, "reset-gpios", NULL)) {
Pali Rohár1e0a9752022-07-29 13:29:07 +02001356 fixup_mcu_gpio_in_pcie_nodes(blob);
Marek Behún85e223e2024-06-18 17:34:30 +02001357 fixup_mcu_gpio_in_phy_wan_node(blob);
Pali Rohár1e0a9752022-07-29 13:29:07 +02001358 }
1359
Pali Rohárcbda3e22022-01-10 11:47:18 +01001360 fixup_spi_nor_partitions(blob);
Pali Rohár7cd41732022-03-02 12:47:56 +01001361 fixup_msata_port_nodes(blob);
Pali Rohár93a89c52022-03-02 12:47:58 +01001362 fixup_wwan_port_nodes(blob);
Pali Rohárcbda3e22022-01-10 11:47:18 +01001363
Marek Behún4cf5a032024-04-04 09:50:56 +02001364 fixup_atsha_node(blob);
1365
Marek Behún91ef59c2021-07-15 19:21:02 +02001366 return 0;
1367}
1368#endif