blob: 6c2d7da528b3961d7592cd43a8686ada4c486239 [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
10#include <common.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>
21#include <dm/uclass.h>
Pali Rohár1e0a9752022-07-29 13:29:07 +020022#include <dt-bindings/gpio/gpio.h>
Marek Behún09e16b82017-06-09 19:28:45 +020023#include <fdt_support.h>
Pali Roháre16cc982022-08-10 11:00:25 +020024#include <hexdump.h>
Marek Behún09e16b82017-06-09 19:28:45 +020025#include <time.h>
Marek Behúnbb42a5b2024-04-04 09:50:51 +020026#include <turris-omnia-mcu-interface.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060027#include <linux/bitops.h>
Pali Rohár1e0a9752022-07-29 13:29:07 +020028#include <linux/delay.h>
Simon Glass48b6c6b2019-11-14 12:57:16 -070029#include <u-boot/crc.h>
Marek Behún09e16b82017-06-09 19:28:45 +020030
Chris Packham1a07d212018-05-10 13:28:29 +120031#include "../drivers/ddr/marvell/a38x/ddr3_init.h"
Marek Behún09e16b82017-06-09 19:28:45 +020032#include <../serdes/a38x/high_speed_env_spec.h>
Pali Rohár0387f7f2022-04-08 16:30:12 +020033#include "../turris_atsha_otp.h"
Marek Behún09e16b82017-06-09 19:28:45 +020034
35DECLARE_GLOBAL_DATA_PTR;
36
Marek Behúnba53b6b2019-05-02 16:53:30 +020037#define OMNIA_I2C_BUS_NAME "i2c@11000->i2cmux@70->i2c@0"
38
39#define OMNIA_I2C_MCU_CHIP_ADDR 0x2a
40#define OMNIA_I2C_MCU_CHIP_LEN 1
41
42#define OMNIA_I2C_EEPROM_CHIP_ADDR 0x54
43#define OMNIA_I2C_EEPROM_CHIP_LEN 2
Marek Behún09e16b82017-06-09 19:28:45 +020044#define OMNIA_I2C_EEPROM_MAGIC 0x0341a034
45
Pali Rohár30e398d2022-04-29 13:53:25 +020046#define A385_SYS_RSTOUT_MASK MVEBU_REGISTER(0x18260)
47#define A385_SYS_RSTOUT_MASK_WD BIT(10)
Pali Rohár7fcda0c2021-11-09 17:14:02 +010048
49#define A385_WDT_GLOBAL_CTRL MVEBU_REGISTER(0x20300)
50#define A385_WDT_GLOBAL_RATIO_MASK GENMASK(18, 16)
51#define A385_WDT_GLOBAL_RATIO_SHIFT 16
52#define A385_WDT_GLOBAL_25MHZ BIT(10)
53#define A385_WDT_GLOBAL_ENABLE BIT(8)
54
55#define A385_WDT_GLOBAL_STATUS MVEBU_REGISTER(0x20304)
56#define A385_WDT_GLOBAL_EXPIRED BIT(31)
57
58#define A385_WDT_DURATION MVEBU_REGISTER(0x20334)
59
60#define A385_WD_RSTOUT_UNMASK MVEBU_REGISTER(0x20704)
61#define A385_WD_RSTOUT_UNMASK_GLOBAL BIT(8)
62
Marek Behún09e16b82017-06-09 19:28:45 +020063/*
64 * Those values and defines are taken from the Marvell U-Boot version
65 * "u-boot-2013.01-2014_T3.0"
66 */
67#define OMNIA_GPP_OUT_ENA_LOW \
68 (~(BIT(1) | BIT(4) | BIT(6) | BIT(7) | BIT(8) | BIT(9) | \
69 BIT(10) | BIT(11) | BIT(19) | BIT(22) | BIT(23) | BIT(25) | \
70 BIT(26) | BIT(27) | BIT(29) | BIT(30) | BIT(31)))
71#define OMNIA_GPP_OUT_ENA_MID \
72 (~(BIT(0) | BIT(1) | BIT(2) | BIT(3) | BIT(4) | BIT(15) | \
73 BIT(16) | BIT(17) | BIT(18)))
74
75#define OMNIA_GPP_OUT_VAL_LOW 0x0
76#define OMNIA_GPP_OUT_VAL_MID 0x0
77#define OMNIA_GPP_POL_LOW 0x0
78#define OMNIA_GPP_POL_MID 0x0
79
Pali Rohár3c4dd982022-03-02 12:47:54 +010080static struct serdes_map board_serdes_map[] = {
Marek Behún09e16b82017-06-09 19:28:45 +020081 {PEX0, SERDES_SPEED_5_GBPS, PEX_ROOT_COMPLEX_X1, 0, 0},
82 {USB3_HOST0, SERDES_SPEED_5_GBPS, SERDES_DEFAULT_MODE, 0, 0},
83 {PEX1, SERDES_SPEED_5_GBPS, PEX_ROOT_COMPLEX_X1, 0, 0},
84 {USB3_HOST1, SERDES_SPEED_5_GBPS, SERDES_DEFAULT_MODE, 0, 0},
85 {PEX2, SERDES_SPEED_5_GBPS, PEX_ROOT_COMPLEX_X1, 0, 0},
86 {SGMII2, SERDES_SPEED_1_25_GBPS, SERDES_DEFAULT_MODE, 0, 0}
87};
88
Marek Behúnba53b6b2019-05-02 16:53:30 +020089static struct udevice *omnia_get_i2c_chip(const char *name, uint addr,
90 uint offset_len)
Marek Behún09e16b82017-06-09 19:28:45 +020091{
92 struct udevice *bus, *dev;
Marek Behúnba53b6b2019-05-02 16:53:30 +020093 int ret;
Marek Behún09e16b82017-06-09 19:28:45 +020094
Marek Behúnba53b6b2019-05-02 16:53:30 +020095 ret = uclass_get_device_by_name(UCLASS_I2C, OMNIA_I2C_BUS_NAME, &bus);
96 if (ret) {
97 printf("Cannot get I2C bus %s: uclass_get_device_by_name failed: %i\n",
98 OMNIA_I2C_BUS_NAME, ret);
99 return NULL;
Marek Behún09e16b82017-06-09 19:28:45 +0200100 }
101
Marek Behúnba53b6b2019-05-02 16:53:30 +0200102 ret = i2c_get_chip(bus, addr, offset_len, &dev);
Marek Behún09e16b82017-06-09 19:28:45 +0200103 if (ret) {
Marek Behúnba53b6b2019-05-02 16:53:30 +0200104 printf("Cannot get %s I2C chip: i2c_get_chip failed: %i\n",
105 name, ret);
106 return NULL;
Marek Behún09e16b82017-06-09 19:28:45 +0200107 }
108
Marek Behúnba53b6b2019-05-02 16:53:30 +0200109 return dev;
110}
Marek Behúnd0b374d2017-08-04 15:28:25 +0200111
Marek Behúnba53b6b2019-05-02 16:53:30 +0200112static int omnia_mcu_read(u8 cmd, void *buf, int len)
113{
114 struct udevice *chip;
115
116 chip = omnia_get_i2c_chip("MCU", OMNIA_I2C_MCU_CHIP_ADDR,
117 OMNIA_I2C_MCU_CHIP_LEN);
118 if (!chip)
119 return -ENODEV;
120
121 return dm_i2c_read(chip, cmd, buf, len);
122}
123
Marek Behúnba53b6b2019-05-02 16:53:30 +0200124static int omnia_mcu_write(u8 cmd, const void *buf, int len)
125{
126 struct udevice *chip;
127
128 chip = omnia_get_i2c_chip("MCU", OMNIA_I2C_MCU_CHIP_ADDR,
129 OMNIA_I2C_MCU_CHIP_LEN);
130 if (!chip)
131 return -ENODEV;
132
133 return dm_i2c_write(chip, cmd, buf, len);
134}
135
Pali Rohár7fcda0c2021-11-09 17:14:02 +0100136static void enable_a385_watchdog(unsigned int timeout_minutes)
137{
138 struct sar_freq_modes sar_freq;
139 u32 watchdog_freq;
140
141 printf("Enabling A385 watchdog with %u minutes timeout...\n",
142 timeout_minutes);
143
144 /*
145 * Use NBCLK clock (a.k.a. L2 clock) as watchdog input clock with
146 * its maximal ratio 7 instead of default fixed 25 MHz clock.
147 * It allows to set watchdog duration up to the 22 minutes.
148 */
149 clrsetbits_32(A385_WDT_GLOBAL_CTRL,
150 A385_WDT_GLOBAL_25MHZ | A385_WDT_GLOBAL_RATIO_MASK,
151 7 << A385_WDT_GLOBAL_RATIO_SHIFT);
152
153 /*
154 * Calculate watchdog clock frequency. It is defined by formula:
155 * freq = NBCLK / 2 / (2 ^ ratio)
156 * We set ratio to the maximal possible value 7.
157 */
158 get_sar_freq(&sar_freq);
159 watchdog_freq = sar_freq.nb_clk * 1000000 / 2 / (1 << 7);
160
161 /* Set watchdog duration */
162 writel(timeout_minutes * 60 * watchdog_freq, A385_WDT_DURATION);
163
164 /* Clear the watchdog expiration bit */
165 clrbits_32(A385_WDT_GLOBAL_STATUS, A385_WDT_GLOBAL_EXPIRED);
166
167 /* Enable watchdog timer */
168 setbits_32(A385_WDT_GLOBAL_CTRL, A385_WDT_GLOBAL_ENABLE);
169
170 /* Enable reset on watchdog */
171 setbits_32(A385_WD_RSTOUT_UNMASK, A385_WD_RSTOUT_UNMASK_GLOBAL);
172
173 /* Unmask reset for watchdog */
Pali Rohár30e398d2022-04-29 13:53:25 +0200174 clrbits_32(A385_SYS_RSTOUT_MASK, A385_SYS_RSTOUT_MASK_WD);
Pali Rohár7fcda0c2021-11-09 17:14:02 +0100175}
176
Marek Behúnba53b6b2019-05-02 16:53:30 +0200177static bool disable_mcu_watchdog(void)
178{
179 int ret;
180
181 puts("Disabling MCU watchdog... ");
182
Marek Behúnbb42a5b2024-04-04 09:50:51 +0200183 ret = omnia_mcu_write(CMD_SET_WATCHDOG_STATE, "\x00", 1);
Marek Behúnba53b6b2019-05-02 16:53:30 +0200184 if (ret) {
185 printf("omnia_mcu_write failed: %i\n", ret);
Marek Behún09e16b82017-06-09 19:28:45 +0200186 return false;
187 }
188
Marek Behúnba53b6b2019-05-02 16:53:30 +0200189 puts("disabled\n");
190
191 return true;
192}
Marek Behúnba53b6b2019-05-02 16:53:30 +0200193
Pali Rohárf8f305b2022-03-02 12:47:55 +0100194static bool omnia_detect_sata(const char *msata_slot)
Marek Behúnba53b6b2019-05-02 16:53:30 +0200195{
196 int ret;
197 u16 stsword;
198
199 puts("MiniPCIe/mSATA card detection... ");
200
Pali Rohárf8f305b2022-03-02 12:47:55 +0100201 if (msata_slot) {
202 if (strcmp(msata_slot, "pcie") == 0) {
203 puts("forced to MiniPCIe via env\n");
204 return false;
205 } else if (strcmp(msata_slot, "sata") == 0) {
206 puts("forced to mSATA via env\n");
207 return true;
208 } else if (strcmp(msata_slot, "auto") != 0) {
209 printf("unsupported env value '%s', fallback to... ", msata_slot);
210 }
211 }
212
Marek Behúnba53b6b2019-05-02 16:53:30 +0200213 ret = omnia_mcu_read(CMD_GET_STATUS_WORD, &stsword, sizeof(stsword));
214 if (ret) {
215 printf("omnia_mcu_read failed: %i, defaulting to MiniPCIe card\n",
216 ret);
Marek Behún09e16b82017-06-09 19:28:45 +0200217 return false;
218 }
219
Marek Behúnbb42a5b2024-04-04 09:50:51 +0200220 if (!(stsword & STS_CARD_DET)) {
Marek Behúnba53b6b2019-05-02 16:53:30 +0200221 puts("none\n");
Marek Behún09e16b82017-06-09 19:28:45 +0200222 return false;
223 }
Marek Behúnba53b6b2019-05-02 16:53:30 +0200224
Marek Behúnbb42a5b2024-04-04 09:50:51 +0200225 if (stsword & STS_MSATA_IND)
Marek Behúnba53b6b2019-05-02 16:53:30 +0200226 puts("mSATA\n");
227 else
228 puts("MiniPCIe\n");
229
Marek Behúnbb42a5b2024-04-04 09:50:51 +0200230 return stsword & STS_MSATA_IND;
Marek Behún09e16b82017-06-09 19:28:45 +0200231}
232
Pali Rohár93a89c52022-03-02 12:47:58 +0100233static bool omnia_detect_wwan_usb3(const char *wwan_slot)
234{
235 puts("WWAN slot configuration... ");
236
237 if (wwan_slot && strcmp(wwan_slot, "usb3") == 0) {
238 puts("USB3.0\n");
239 return true;
240 }
241
242 if (wwan_slot && strcmp(wwan_slot, "pcie") != 0)
243 printf("unsupported env value '%s', fallback to... ", wwan_slot);
244
245 puts("PCIe+USB2.0\n");
246 return false;
247}
248
Marek Behún09e16b82017-06-09 19:28:45 +0200249int hws_board_topology_load(struct serdes_map **serdes_map_array, u8 *count)
250{
Pali Rohárf8f305b2022-03-02 12:47:55 +0100251#ifdef CONFIG_SPL_ENV_SUPPORT
252 /* Do not use env_load() as malloc() pool is too small at this stage */
253 bool has_env = (env_init() == 0);
254#endif
255 const char *env_value = NULL;
256
257#ifdef CONFIG_SPL_ENV_SUPPORT
258 /* beware that env_get() returns static allocated memory */
259 env_value = has_env ? env_get("omnia_msata_slot") : NULL;
260#endif
261
262 if (omnia_detect_sata(env_value)) {
Pali Rohár3c4dd982022-03-02 12:47:54 +0100263 /* Change SerDes for first mPCIe port (mSATA) from PCIe to SATA */
264 board_serdes_map[0].serdes_type = SATA0;
265 board_serdes_map[0].serdes_speed = SERDES_SPEED_6_GBPS;
266 board_serdes_map[0].serdes_mode = SERDES_DEFAULT_MODE;
Marek Behún09e16b82017-06-09 19:28:45 +0200267 }
268
Pali Rohár93a89c52022-03-02 12:47:58 +0100269#ifdef CONFIG_SPL_ENV_SUPPORT
270 /* beware that env_get() returns static allocated memory */
271 env_value = has_env ? env_get("omnia_wwan_slot") : NULL;
272#endif
273
274 if (omnia_detect_wwan_usb3(env_value)) {
275 /* Disable SerDes for USB 3.0 pins on the front USB-A port */
276 board_serdes_map[1].serdes_type = DEFAULT_SERDES;
277 /* Change SerDes for third mPCIe port (WWAN) from PCIe to USB 3.0 */
278 board_serdes_map[4].serdes_type = USB3_HOST0;
279 board_serdes_map[4].serdes_speed = SERDES_SPEED_5_GBPS;
280 board_serdes_map[4].serdes_mode = SERDES_DEFAULT_MODE;
281 }
282
Pali Rohár3c4dd982022-03-02 12:47:54 +0100283 *serdes_map_array = board_serdes_map;
284 *count = ARRAY_SIZE(board_serdes_map);
285
Marek Behún09e16b82017-06-09 19:28:45 +0200286 return 0;
287}
288
289struct omnia_eeprom {
290 u32 magic;
291 u32 ramsize;
292 char region[4];
293 u32 crc;
294};
295
296static bool omnia_read_eeprom(struct omnia_eeprom *oep)
297{
Marek Behúnba53b6b2019-05-02 16:53:30 +0200298 struct udevice *chip;
299 u32 crc;
300 int ret;
Marek Behún09e16b82017-06-09 19:28:45 +0200301
Marek Behúnba53b6b2019-05-02 16:53:30 +0200302 chip = omnia_get_i2c_chip("EEPROM", OMNIA_I2C_EEPROM_CHIP_ADDR,
303 OMNIA_I2C_EEPROM_CHIP_LEN);
304
305 if (!chip)
Marek Behún09e16b82017-06-09 19:28:45 +0200306 return false;
Marek Behún09e16b82017-06-09 19:28:45 +0200307
Marek Behúnba53b6b2019-05-02 16:53:30 +0200308 ret = dm_i2c_read(chip, 0, (void *)oep, sizeof(*oep));
Marek Behún09e16b82017-06-09 19:28:45 +0200309 if (ret) {
Marek Behúnba53b6b2019-05-02 16:53:30 +0200310 printf("dm_i2c_read failed: %i, cannot read EEPROM\n", ret);
Marek Behún09e16b82017-06-09 19:28:45 +0200311 return false;
312 }
313
Marek Behúnba53b6b2019-05-02 16:53:30 +0200314 if (oep->magic != OMNIA_I2C_EEPROM_MAGIC) {
315 printf("bad EEPROM magic number (%08x, should be %08x)\n",
316 oep->magic, OMNIA_I2C_EEPROM_MAGIC);
317 return false;
Marek Behún09e16b82017-06-09 19:28:45 +0200318 }
319
Marek Behúnba53b6b2019-05-02 16:53:30 +0200320 crc = crc32(0, (void *)oep, sizeof(*oep) - 4);
321 if (crc != oep->crc) {
322 printf("bad EEPROM CRC (stored %08x, computed %08x)\n",
323 oep->crc, crc);
Marek Behún09e16b82017-06-09 19:28:45 +0200324 return false;
325 }
326
327 return true;
328}
329
Marek Behún77652c72019-05-02 16:53:33 +0200330static int omnia_get_ram_size_gb(void)
331{
332 static int ram_size;
333 struct omnia_eeprom oep;
334
335 if (!ram_size) {
336 /* Get the board config from EEPROM */
337 if (omnia_read_eeprom(&oep)) {
338 debug("Memory config in EEPROM: 0x%02x\n", oep.ramsize);
339
340 if (oep.ramsize == 0x2)
341 ram_size = 2;
342 else
343 ram_size = 1;
344 } else {
345 /* Hardcoded fallback */
346 puts("Memory config from EEPROM read failed!\n");
347 puts("Falling back to default 1 GiB!\n");
348 ram_size = 1;
349 }
350 }
351
352 return ram_size;
353}
354
Pali Rohár4798ba92022-07-29 13:29:06 +0200355static const char * const omnia_get_mcu_type(void)
356{
Marek Behúnbb42a5b2024-04-04 09:50:51 +0200357 static char result[] = "xxxxxxx (with peripheral resets)";
Pali Rohár4798ba92022-07-29 13:29:06 +0200358 u16 stsword, features;
359 int ret;
360
361 ret = omnia_mcu_read(CMD_GET_STATUS_WORD, &stsword, sizeof(stsword));
362 if (ret)
363 return "unknown";
364
Marek Behúnbb42a5b2024-04-04 09:50:51 +0200365 switch (stsword & STS_MCU_TYPE_MASK) {
366 case STS_MCU_TYPE_STM32:
367 strcpy(result, "STM32");
368 break;
369 case STS_MCU_TYPE_GD32:
370 strcpy(result, "GD32");
371 break;
372 case STS_MCU_TYPE_MKL:
373 strcpy(result, "MKL");
374 break;
375 default:
376 strcpy(result, "unknown");
377 break;
378 }
379
Pali Rohár4798ba92022-07-29 13:29:06 +0200380 if (stsword & STS_FEATURES_SUPPORTED) {
381 ret = omnia_mcu_read(CMD_GET_FEATURES, &features, sizeof(features));
382 if (ret == 0 && (features & FEAT_PERIPH_MCU))
Marek Behúnbb42a5b2024-04-04 09:50:51 +0200383 strcat(result, " (with peripheral resets)");
Pali Rohár4798ba92022-07-29 13:29:06 +0200384 }
385
Marek Behúnbb42a5b2024-04-04 09:50:51 +0200386 return result;
Pali Rohár4798ba92022-07-29 13:29:06 +0200387}
388
Pali Roháre16cc982022-08-10 11:00:25 +0200389static const char * const omnia_get_mcu_version(void)
390{
391 static char version[82];
392 u8 version_app[20];
393 u8 version_boot[20];
394 int ret;
395
396 ret = omnia_mcu_read(CMD_GET_FW_VERSION_APP, &version_app, sizeof(version_app));
397 if (ret)
398 return "unknown";
399
400 ret = omnia_mcu_read(CMD_GET_FW_VERSION_BOOT, &version_boot, sizeof(version_boot));
401 if (ret)
402 return "unknown";
403
404 /*
405 * If git commits of MCU bootloader and MCU application are same then
406 * show version only once. If they are different then show both commits.
407 */
408 if (!memcmp(version_app, version_boot, 20)) {
409 bin2hex(version, version_app, 20);
410 version[40] = '\0';
411 } else {
412 bin2hex(version, version_boot, 20);
413 version[40] = '/';
414 bin2hex(version + 41, version_app, 20);
415 version[81] = '\0';
416 }
417
418 return version;
419}
420
Marek Behún09e16b82017-06-09 19:28:45 +0200421/*
422 * Define the DDR layout / topology here in the board file. This will
423 * be used by the DDR3 init code in the SPL U-Boot version to configure
424 * the DDR3 controller.
425 */
Chris Packham1a07d212018-05-10 13:28:29 +1200426static struct mv_ddr_topology_map board_topology_map_1g = {
427 DEBUG_LEVEL_ERROR,
Marek Behún09e16b82017-06-09 19:28:45 +0200428 0x1, /* active interfaces */
429 /* cs_mask, mirror, dqs_swap, ck_swap X PUPs */
430 { { { {0x1, 0, 0, 0},
431 {0x1, 0, 0, 0},
432 {0x1, 0, 0, 0},
433 {0x1, 0, 0, 0},
434 {0x1, 0, 0, 0} },
435 SPEED_BIN_DDR_1600K, /* speed_bin */
Chris Packham1a07d212018-05-10 13:28:29 +1200436 MV_DDR_DEV_WIDTH_16BIT, /* memory_width */
437 MV_DDR_DIE_CAP_4GBIT, /* mem_size */
Chris Packham4bf81db2018-12-03 14:26:49 +1300438 MV_DDR_FREQ_800, /* frequency */
Chris Packhamdd092bd2017-11-29 10:38:34 +1300439 0, 0, /* cas_wl cas_l */
Chris Packham3a09e132018-05-10 13:28:30 +1200440 MV_DDR_TEMP_NORMAL, /* temperature */
441 MV_DDR_TIM_2T} }, /* timing */
Chris Packham1a07d212018-05-10 13:28:29 +1200442 BUS_MASK_32BIT, /* Busses mask */
443 MV_DDR_CFG_DEFAULT, /* ddr configuration data source */
Moti Buskila498475e2021-02-19 17:11:19 +0100444 NOT_COMBINED, /* ddr twin-die combined */
Chris Packham1a07d212018-05-10 13:28:29 +1200445 { {0} }, /* raw spd data */
446 {0} /* timing parameters */
Marek Behún09e16b82017-06-09 19:28:45 +0200447};
448
Chris Packham1a07d212018-05-10 13:28:29 +1200449static struct mv_ddr_topology_map board_topology_map_2g = {
450 DEBUG_LEVEL_ERROR,
Marek Behún09e16b82017-06-09 19:28:45 +0200451 0x1, /* active interfaces */
452 /* cs_mask, mirror, dqs_swap, ck_swap X PUPs */
453 { { { {0x1, 0, 0, 0},
454 {0x1, 0, 0, 0},
455 {0x1, 0, 0, 0},
456 {0x1, 0, 0, 0},
457 {0x1, 0, 0, 0} },
458 SPEED_BIN_DDR_1600K, /* speed_bin */
Chris Packham1a07d212018-05-10 13:28:29 +1200459 MV_DDR_DEV_WIDTH_16BIT, /* memory_width */
460 MV_DDR_DIE_CAP_8GBIT, /* mem_size */
Chris Packham4bf81db2018-12-03 14:26:49 +1300461 MV_DDR_FREQ_800, /* frequency */
Chris Packhamdd092bd2017-11-29 10:38:34 +1300462 0, 0, /* cas_wl cas_l */
Chris Packham3a09e132018-05-10 13:28:30 +1200463 MV_DDR_TEMP_NORMAL, /* temperature */
464 MV_DDR_TIM_2T} }, /* timing */
Chris Packham1a07d212018-05-10 13:28:29 +1200465 BUS_MASK_32BIT, /* Busses mask */
466 MV_DDR_CFG_DEFAULT, /* ddr configuration data source */
Moti Buskila498475e2021-02-19 17:11:19 +0100467 NOT_COMBINED, /* ddr twin-die combined */
Chris Packham1a07d212018-05-10 13:28:29 +1200468 { {0} }, /* raw spd data */
469 {0} /* timing parameters */
Marek Behún09e16b82017-06-09 19:28:45 +0200470};
471
Chris Packham1a07d212018-05-10 13:28:29 +1200472struct mv_ddr_topology_map *mv_ddr_topology_map_get(void)
Marek Behún09e16b82017-06-09 19:28:45 +0200473{
Marek Behún77652c72019-05-02 16:53:33 +0200474 if (omnia_get_ram_size_gb() == 2)
Marek Behún09e16b82017-06-09 19:28:45 +0200475 return &board_topology_map_2g;
Marek Behún77652c72019-05-02 16:53:33 +0200476 else
477 return &board_topology_map_1g;
Marek Behún09e16b82017-06-09 19:28:45 +0200478}
479
Marek Behún09e16b82017-06-09 19:28:45 +0200480static int set_regdomain(void)
481{
482 struct omnia_eeprom oep;
483 char rd[3] = {' ', ' ', 0};
484
485 if (omnia_read_eeprom(&oep))
486 memcpy(rd, &oep.region, 2);
487 else
488 puts("EEPROM regdomain read failed.\n");
489
490 printf("Regdomain set to %s\n", rd);
Simon Glass6a38e412017-08-03 12:22:09 -0600491 return env_set("regdomain", rd);
Marek Behún09e16b82017-06-09 19:28:45 +0200492}
Marek Behún0f2e66a2019-05-02 16:53:37 +0200493
Marek Behún0f2e66a2019-05-02 16:53:37 +0200494static void handle_reset_button(void)
495{
Pali Rohár905c3bf2021-06-14 16:45:58 +0200496 const char * const vars[1] = { "bootcmd_rescue", };
Marek Behún0f2e66a2019-05-02 16:53:37 +0200497 int ret;
498 u8 reset_status;
499
Pali Rohár905c3bf2021-06-14 16:45:58 +0200500 /*
501 * Ensure that bootcmd_rescue has always stock value, so that running
502 * run bootcmd_rescue
503 * always works correctly.
504 */
505 env_set_default_vars(1, (char * const *)vars, 0);
506
Marek Behún0f2e66a2019-05-02 16:53:37 +0200507 ret = omnia_mcu_read(CMD_GET_RESET, &reset_status, 1);
508 if (ret) {
509 printf("omnia_mcu_read failed: %i, reset status unknown!\n",
510 ret);
511 return;
512 }
513
514 env_set_ulong("omnia_reset", reset_status);
515
516 if (reset_status) {
Pali Rohár8adec652022-08-27 20:49:20 +0200517 const char * const vars[3] = {
Marek Behún09f8de22021-05-28 10:00:49 +0200518 "bootcmd",
Pali Rohár8adec652022-08-27 20:49:20 +0200519 "bootdelay",
Marek Behún09f8de22021-05-28 10:00:49 +0200520 "distro_bootcmd",
521 };
522
523 /*
524 * Set the above envs to their default values, in case the user
525 * managed to break them.
526 */
Pali Rohár8adec652022-08-27 20:49:20 +0200527 env_set_default_vars(3, (char * const *)vars, 0);
Marek Behún09f8de22021-05-28 10:00:49 +0200528
529 /* Ensure bootcmd_rescue is used by distroboot */
530 env_set("boot_targets", "rescue");
531
Pali Rohár4f9e6fb2022-04-06 11:39:32 +0200532 printf("RESET button was pressed, overwriting boot_targets!\n");
Marek Behún09f8de22021-05-28 10:00:49 +0200533 } else {
534 /*
535 * In case the user somehow managed to save environment with
536 * boot_targets=rescue, reset boot_targets to default value.
537 * This could happen in subsequent commands if bootcmd_rescue
538 * failed.
539 */
540 if (!strcmp(env_get("boot_targets"), "rescue")) {
541 const char * const vars[1] = {
542 "boot_targets",
543 };
544
545 env_set_default_vars(1, (char * const *)vars, 0);
546 }
Marek Behún0f2e66a2019-05-02 16:53:37 +0200547 }
548}
Marek Behún09e16b82017-06-09 19:28:45 +0200549
Pali Rohár1e0a9752022-07-29 13:29:07 +0200550static void initialize_switch(void)
551{
552 u32 val, val04, val08, val10, val14;
553 u16 ctrl[2];
554 int err;
555
556 printf("Initializing LAN eth switch... ");
557
558 /* Change RGMII pins to GPIO mode */
559
560 val = val04 = readl(MVEBU_MPP_BASE + 0x04);
561 val &= ~GENMASK(19, 16); /* MPP[12] := GPIO */
562 val &= ~GENMASK(23, 20); /* MPP[13] := GPIO */
563 val &= ~GENMASK(27, 24); /* MPP[14] := GPIO */
564 val &= ~GENMASK(31, 28); /* MPP[15] := GPIO */
565 writel(val, MVEBU_MPP_BASE + 0x04);
566
567 val = val08 = readl(MVEBU_MPP_BASE + 0x08);
568 val &= ~GENMASK(3, 0); /* MPP[16] := GPIO */
569 val &= ~GENMASK(23, 20); /* MPP[21] := GPIO */
570 writel(val, MVEBU_MPP_BASE + 0x08);
571
572 val = val10 = readl(MVEBU_MPP_BASE + 0x10);
573 val &= ~GENMASK(27, 24); /* MPP[38] := GPIO */
574 val &= ~GENMASK(31, 28); /* MPP[39] := GPIO */
575 writel(val, MVEBU_MPP_BASE + 0x10);
576
577 val = val14 = readl(MVEBU_MPP_BASE + 0x14);
578 val &= ~GENMASK(3, 0); /* MPP[40] := GPIO */
579 val &= ~GENMASK(7, 4); /* MPP[41] := GPIO */
580 writel(val, MVEBU_MPP_BASE + 0x14);
581
582 /* Set initial values for switch reset strapping pins */
583
584 val = readl(MVEBU_GPIO0_BASE + 0x00);
585 val |= BIT(12); /* GPIO[12] := 1 */
586 val |= BIT(13); /* GPIO[13] := 1 */
587 val |= BIT(14); /* GPIO[14] := 1 */
588 val |= BIT(15); /* GPIO[15] := 1 */
589 val &= ~BIT(16); /* GPIO[16] := 0 */
590 val |= BIT(21); /* GPIO[21] := 1 */
591 writel(val, MVEBU_GPIO0_BASE + 0x00);
592
593 val = readl(MVEBU_GPIO1_BASE + 0x00);
594 val |= BIT(6); /* GPIO[38] := 1 */
595 val |= BIT(7); /* GPIO[39] := 1 */
596 val |= BIT(8); /* GPIO[40] := 1 */
597 val &= ~BIT(9); /* GPIO[41] := 0 */
598 writel(val, MVEBU_GPIO1_BASE + 0x00);
599
600 val = readl(MVEBU_GPIO0_BASE + 0x04);
601 val &= ~BIT(12); /* GPIO[12] := Out Enable */
602 val &= ~BIT(13); /* GPIO[13] := Out Enable */
603 val &= ~BIT(14); /* GPIO[14] := Out Enable */
604 val &= ~BIT(15); /* GPIO[15] := Out Enable */
605 val &= ~BIT(16); /* GPIO[16] := Out Enable */
606 val &= ~BIT(21); /* GPIO[21] := Out Enable */
607 writel(val, MVEBU_GPIO0_BASE + 0x04);
608
609 val = readl(MVEBU_GPIO1_BASE + 0x04);
610 val &= ~BIT(6); /* GPIO[38] := Out Enable */
611 val &= ~BIT(7); /* GPIO[39] := Out Enable */
612 val &= ~BIT(8); /* GPIO[40] := Out Enable */
613 val &= ~BIT(9); /* GPIO[41] := Out Enable */
614 writel(val, MVEBU_GPIO1_BASE + 0x04);
615
616 /* Release switch reset */
617
618 ctrl[0] = EXT_CTL_nRES_LAN;
619 ctrl[1] = EXT_CTL_nRES_LAN;
620 err = omnia_mcu_write(CMD_EXT_CONTROL, ctrl, sizeof(ctrl));
621
Marek Behún59aa4652022-09-13 18:10:28 +0200622 mdelay(50);
Pali Rohár1e0a9752022-07-29 13:29:07 +0200623
624 /* Change RGMII pins back to RGMII mode */
625
626 writel(val04, MVEBU_MPP_BASE + 0x04);
627 writel(val08, MVEBU_MPP_BASE + 0x08);
628 writel(val10, MVEBU_MPP_BASE + 0x10);
629 writel(val14, MVEBU_MPP_BASE + 0x14);
630
631 puts(err ? "failed\n" : "done\n");
632}
633
Marek Behún09e16b82017-06-09 19:28:45 +0200634int board_early_init_f(void)
635{
Marek Behún09e16b82017-06-09 19:28:45 +0200636 /* Configure MPP */
637 writel(0x11111111, MVEBU_MPP_BASE + 0x00);
638 writel(0x11111111, MVEBU_MPP_BASE + 0x04);
639 writel(0x11244011, MVEBU_MPP_BASE + 0x08);
640 writel(0x22222111, MVEBU_MPP_BASE + 0x0c);
641 writel(0x22200002, MVEBU_MPP_BASE + 0x10);
642 writel(0x30042022, MVEBU_MPP_BASE + 0x14);
643 writel(0x55550555, MVEBU_MPP_BASE + 0x18);
644 writel(0x00005550, MVEBU_MPP_BASE + 0x1c);
645
646 /* Set GPP Out value */
647 writel(OMNIA_GPP_OUT_VAL_LOW, MVEBU_GPIO0_BASE + 0x00);
648 writel(OMNIA_GPP_OUT_VAL_MID, MVEBU_GPIO1_BASE + 0x00);
649
650 /* Set GPP Polarity */
651 writel(OMNIA_GPP_POL_LOW, MVEBU_GPIO0_BASE + 0x0c);
652 writel(OMNIA_GPP_POL_MID, MVEBU_GPIO1_BASE + 0x0c);
653
654 /* Set GPP Out Enable */
655 writel(OMNIA_GPP_OUT_ENA_LOW, MVEBU_GPIO0_BASE + 0x04);
656 writel(OMNIA_GPP_OUT_ENA_MID, MVEBU_GPIO1_BASE + 0x04);
657
Marek Behún09e16b82017-06-09 19:28:45 +0200658 return 0;
659}
660
Marek Behúnf3556162021-08-16 15:19:39 +0200661void spl_board_init(void)
662{
Pali Rohár1e0a9752022-07-29 13:29:07 +0200663 u16 val;
664 int ret;
665
Marek Behúnf3556162021-08-16 15:19:39 +0200666 /*
667 * If booting from UART, disable MCU watchdog in SPL, since uploading
Pali Rohár7fcda0c2021-11-09 17:14:02 +0100668 * U-Boot proper can take too much time and trigger it. Instead enable
669 * A385 watchdog with very high timeout (10 minutes) to prevent hangup.
Marek Behúnf3556162021-08-16 15:19:39 +0200670 */
Pali Rohár7fcda0c2021-11-09 17:14:02 +0100671 if (get_boot_device() == BOOT_DEVICE_UART) {
672 enable_a385_watchdog(10);
Marek Behúnf3556162021-08-16 15:19:39 +0200673 disable_mcu_watchdog();
Pali Rohár7fcda0c2021-11-09 17:14:02 +0100674 }
Pali Rohár1e0a9752022-07-29 13:29:07 +0200675
676 /*
677 * When MCU controls peripheral resets then release LAN eth switch from
678 * the reset and initialize it. When MCU does not control peripheral
679 * resets then LAN eth switch is initialized automatically by bootstrap
680 * pins when A385 is released from the reset.
681 */
682 ret = omnia_mcu_read(CMD_GET_STATUS_WORD, &val, sizeof(val));
683 if (ret == 0 && (val & STS_FEATURES_SUPPORTED)) {
684 ret = omnia_mcu_read(CMD_GET_FEATURES, &val, sizeof(val));
685 if (ret == 0 && (val & FEAT_PERIPH_MCU))
686 initialize_switch();
687 }
Marek Behúnf3556162021-08-16 15:19:39 +0200688}
689
Pali Rohárcbda3e22022-01-10 11:47:18 +0100690#if IS_ENABLED(CONFIG_OF_BOARD_FIXUP) || IS_ENABLED(CONFIG_OF_BOARD_SETUP)
691
Pali Rohár7cd41732022-03-02 12:47:56 +0100692static void disable_sata_node(void *blob)
Pali Rohárcbda3e22022-01-10 11:47:18 +0100693{
Pali Rohárcbda3e22022-01-10 11:47:18 +0100694 int node;
695
Pali Rohár7cd41732022-03-02 12:47:56 +0100696 fdt_for_each_node_by_compatible(node, blob, -1, "marvell,armada-380-ahci") {
697 if (!fdtdec_get_is_enabled(blob, node))
698 continue;
699
700 if (fdt_status_disabled(blob, node) < 0)
701 printf("Cannot disable SATA DT node!\n");
702 else
703 debug("Disabled SATA DT node\n");
704
Pali Roháre9105262022-03-02 12:47:57 +0100705 return;
Pali Rohár7cd41732022-03-02 12:47:56 +0100706 }
Pali Roháre9105262022-03-02 12:47:57 +0100707
708 printf("Cannot find SATA DT node!\n");
Pali Rohár7cd41732022-03-02 12:47:56 +0100709}
710
711static void disable_pcie_node(void *blob, int port)
712{
713 int node;
714
715 fdt_for_each_node_by_compatible(node, blob, -1, "marvell,armada-370-pcie") {
716 int port_node;
717
718 if (!fdtdec_get_is_enabled(blob, node))
719 continue;
720
721 fdt_for_each_subnode (port_node, blob, node) {
722 if (!fdtdec_get_is_enabled(blob, port_node))
723 continue;
724
725 if (fdtdec_get_int(blob, port_node, "marvell,pcie-port", -1) != port)
726 continue;
727
728 if (fdt_status_disabled(blob, port_node) < 0)
729 printf("Cannot disable PCIe port %d DT node!\n", port);
730 else
731 debug("Disabled PCIe port %d DT node\n", port);
732
733 return;
734 }
735 }
Pali Roháre9105262022-03-02 12:47:57 +0100736
737 printf("Cannot find PCIe port %d DT node!\n", port);
Pali Rohár7cd41732022-03-02 12:47:56 +0100738}
739
740static void fixup_msata_port_nodes(void *blob)
741{
742 bool mode_sata;
743
Pali Rohárcbda3e22022-01-10 11:47:18 +0100744 /*
745 * Determine if SerDes 0 is configured to SATA mode.
746 * We do this instead of calling omnia_detect_sata() to avoid another
747 * call to the MCU. By this time the common PHYs are initialized (it is
748 * done in SPL), so we can read this common PHY register.
749 */
750 mode_sata = (readl(MVEBU_REGISTER(0x183fc)) & GENMASK(3, 0)) == 2;
751
752 /*
753 * We're either adding status = "disabled" property, or changing
754 * status = "okay" to status = "disabled". In both cases we'll need more
755 * space. Increase the size a little.
756 */
757 if (fdt_increase_size(blob, 32) < 0) {
758 printf("Cannot increase FDT size!\n");
759 return;
760 }
761
Pali Rohárcbda3e22022-01-10 11:47:18 +0100762 if (!mode_sata) {
Pali Rohár7cd41732022-03-02 12:47:56 +0100763 /* If mSATA card is not present, disable SATA DT node */
764 disable_sata_node(blob);
765 } else {
766 /* Otherwise disable PCIe port 0 DT node (MiniPCIe / mSATA port) */
767 disable_pcie_node(blob, 0);
Pali Rohárcbda3e22022-01-10 11:47:18 +0100768 }
Pali Rohár93a89c52022-03-02 12:47:58 +0100769}
770
771static void fixup_wwan_port_nodes(void *blob)
772{
773 bool mode_usb3;
774
775 /* Determine if SerDes 4 is configured to USB3 mode */
776 mode_usb3 = ((readl(MVEBU_REGISTER(0x183fc)) & GENMASK(19, 16)) >> 16) == 4;
777
778 /* If SerDes 4 is not configured to USB3 mode then nothing is needed to fixup */
779 if (!mode_usb3)
780 return;
781
782 /*
783 * We're either adding status = "disabled" property, or changing
784 * status = "okay" to status = "disabled". In both cases we'll need more
785 * space. Increase the size a little.
786 */
787 if (fdt_increase_size(blob, 32) < 0) {
788 printf("Cannot increase FDT size!\n");
789 return;
790 }
791
792 /* Disable PCIe port 2 DT node (WWAN) */
793 disable_pcie_node(blob, 2);
Pali Rohárcbda3e22022-01-10 11:47:18 +0100794}
795
Pali Rohár1e0a9752022-07-29 13:29:07 +0200796static int insert_mcu_gpio_prop(void *blob, int node, const char *prop,
797 unsigned int phandle, u32 bank, u32 gpio,
798 u32 flags)
799{
800 fdt32_t val[4] = { cpu_to_fdt32(phandle), cpu_to_fdt32(bank),
801 cpu_to_fdt32(gpio), cpu_to_fdt32(flags) };
802 return fdt_setprop(blob, node, prop, &val, sizeof(val));
803}
804
805static int fixup_mcu_gpio_in_pcie_nodes(void *blob)
806{
807 unsigned int mcu_phandle;
808 int port, gpio;
809 int pcie_node;
810 int port_node;
811 int ret;
812
813 ret = fdt_increase_size(blob, 128);
814 if (ret < 0) {
815 printf("Cannot increase FDT size!\n");
816 return ret;
817 }
818
819 mcu_phandle = fdt_create_phandle_by_compatible(blob, "cznic,turris-omnia-mcu");
820 if (!mcu_phandle)
821 return -FDT_ERR_NOPHANDLES;
822
823 fdt_for_each_node_by_compatible(pcie_node, blob, -1, "marvell,armada-370-pcie") {
824 if (!fdtdec_get_is_enabled(blob, pcie_node))
825 continue;
826
827 fdt_for_each_subnode(port_node, blob, pcie_node) {
828 if (!fdtdec_get_is_enabled(blob, port_node))
829 continue;
830
831 port = fdtdec_get_int(blob, port_node, "marvell,pcie-port", -1);
832
833 if (port == 0)
834 gpio = ilog2(EXT_CTL_nPERST0);
835 else if (port == 1)
836 gpio = ilog2(EXT_CTL_nPERST1);
837 else if (port == 2)
838 gpio = ilog2(EXT_CTL_nPERST2);
839 else
840 continue;
841
842 /* insert: reset-gpios = <&mcu 2 gpio GPIO_ACTIVE_LOW>; */
843 ret = insert_mcu_gpio_prop(blob, port_node, "reset-gpios",
844 mcu_phandle, 2, gpio, GPIO_ACTIVE_LOW);
845 if (ret < 0)
846 return ret;
847 }
848 }
849
850 return 0;
851}
852
853static int fixup_mcu_gpio_in_eth_wan_node(void *blob)
854{
855 unsigned int mcu_phandle;
856 int eth_wan_node;
857 int ret;
858
859 ret = fdt_increase_size(blob, 64);
860 if (ret < 0) {
861 printf("Cannot increase FDT size!\n");
862 return ret;
863 }
864
865 eth_wan_node = fdt_path_offset(blob, "ethernet2");
866 if (eth_wan_node < 0)
867 return eth_wan_node;
868
869 mcu_phandle = fdt_create_phandle_by_compatible(blob, "cznic,turris-omnia-mcu");
870 if (!mcu_phandle)
871 return -FDT_ERR_NOPHANDLES;
872
873 /* insert: phy-reset-gpios = <&mcu 2 gpio GPIO_ACTIVE_LOW>; */
874 ret = insert_mcu_gpio_prop(blob, eth_wan_node, "phy-reset-gpios",
875 mcu_phandle, 2, ilog2(EXT_CTL_nRES_PHY), GPIO_ACTIVE_LOW);
876 if (ret < 0)
877 return ret;
878
879 return 0;
880}
881
Pali Rohárcbda3e22022-01-10 11:47:18 +0100882#endif
883
884#if IS_ENABLED(CONFIG_OF_BOARD_FIXUP)
885int board_fix_fdt(void *blob)
886{
Pali Rohár1e0a9752022-07-29 13:29:07 +0200887 u16 val;
888 int ret;
889
890 ret = omnia_mcu_read(CMD_GET_STATUS_WORD, &val, sizeof(val));
891 if (ret == 0 && (val & STS_FEATURES_SUPPORTED)) {
892 ret = omnia_mcu_read(CMD_GET_FEATURES, &val, sizeof(val));
893 if (ret == 0 && (val & FEAT_PERIPH_MCU)) {
894 fixup_mcu_gpio_in_pcie_nodes(blob);
895 fixup_mcu_gpio_in_eth_wan_node(blob);
896 }
897 }
898
Pali Rohár7cd41732022-03-02 12:47:56 +0100899 fixup_msata_port_nodes(blob);
Pali Rohár93a89c52022-03-02 12:47:58 +0100900 fixup_wwan_port_nodes(blob);
Pali Rohárcbda3e22022-01-10 11:47:18 +0100901
902 return 0;
903}
904#endif
905
Marek Behún09e16b82017-06-09 19:28:45 +0200906int board_init(void)
907{
Marek Behún4dfc57e2019-05-02 16:53:31 +0200908 /* address of boot parameters */
Marek Behún09e16b82017-06-09 19:28:45 +0200909 gd->bd->bi_boot_params = mvebu_sdram_bar(0) + 0x100;
910
Marek Behún88dc0242021-08-16 15:19:40 +0200911 return 0;
912}
913
914int board_late_init(void)
915{
Marek Behúnf3556162021-08-16 15:19:39 +0200916 /*
917 * If not booting from UART, MCU watchdog was not disabled in SPL,
918 * disable it now.
919 */
920 if (get_boot_device() != BOOT_DEVICE_UART)
921 disable_mcu_watchdog();
Marek Behún09e16b82017-06-09 19:28:45 +0200922
Marek Behún09e16b82017-06-09 19:28:45 +0200923 set_regdomain();
Marek Behún0f2e66a2019-05-02 16:53:37 +0200924 handle_reset_button();
Marek Behúndb1e5c62019-05-24 14:57:53 +0200925 pci_init();
Marek Behún09e16b82017-06-09 19:28:45 +0200926
927 return 0;
928}
929
Simon Glass629d9b62023-11-12 19:58:23 -0700930int checkboard(void)
Marek Behún09e16b82017-06-09 19:28:45 +0200931{
Pali Rohár38ecdab2022-08-27 20:06:30 +0200932 char serial[17];
Pali Rohár0387f7f2022-04-08 16:30:12 +0200933 int err;
Marek Behún09e16b82017-06-09 19:28:45 +0200934
Pali Rohár38ecdab2022-08-27 20:06:30 +0200935 err = turris_atsha_otp_get_serial_number(serial);
Marek Behúnab9447f2021-10-09 19:33:44 +0200936 printf("Model: Turris Omnia\n");
Pali Rohár4798ba92022-07-29 13:29:06 +0200937 printf(" MCU type: %s\n", omnia_get_mcu_type());
Pali Roháre16cc982022-08-10 11:00:25 +0200938 printf(" MCU version: %s\n", omnia_get_mcu_version());
Marek Behúnc4ba72a2019-05-02 16:53:34 +0200939 printf(" RAM size: %i MiB\n", omnia_get_ram_size_gb() * 1024);
Pali Rohár38ecdab2022-08-27 20:06:30 +0200940 printf(" Serial Number: %s\n", !err ? serial : "unknown");
Marek Behún09e16b82017-06-09 19:28:45 +0200941
942 return 0;
943}
944
Marek Behún09e16b82017-06-09 19:28:45 +0200945int misc_init_r(void)
946{
Pali Rohár60f37e82022-04-08 16:30:14 +0200947 turris_atsha_otp_init_mac_addresses(1);
Pali Rohár38ecdab2022-08-27 20:06:30 +0200948 turris_atsha_otp_init_serial_number();
Marek Behún09e16b82017-06-09 19:28:45 +0200949 return 0;
950}
951
Marek Behún91ef59c2021-07-15 19:21:02 +0200952#if defined(CONFIG_OF_BOARD_SETUP)
953/*
954 * I plan to generalize this function and move it to common/fdt_support.c.
955 * This will require some more work on multiple boards, though, so for now leave
956 * it here.
957 */
958static bool fixup_mtd_partitions(void *blob, int offset, struct mtd_info *mtd)
959{
960 struct mtd_info *slave;
961 int parts;
962
963 parts = fdt_subnode_offset(blob, offset, "partitions");
Pali Roháre2b1ba02022-08-01 12:02:19 +0200964 if (parts >= 0) {
965 if (fdt_del_node(blob, parts) < 0)
966 return false;
967 }
Marek Behún91ef59c2021-07-15 19:21:02 +0200968
Pali Rohárd35b6f22022-08-01 12:02:20 +0200969 if (fdt_increase_size(blob, 512) < 0)
970 return false;
971
Marek Behún91ef59c2021-07-15 19:21:02 +0200972 parts = fdt_add_subnode(blob, offset, "partitions");
973 if (parts < 0)
974 return false;
975
976 if (fdt_setprop_u32(blob, parts, "#address-cells", 1) < 0)
977 return false;
978
979 if (fdt_setprop_u32(blob, parts, "#size-cells", 1) < 0)
980 return false;
981
982 if (fdt_setprop_string(blob, parts, "compatible",
983 "fixed-partitions") < 0)
984 return false;
985
986 mtd_probe_devices();
987
Pali Rohárd8210ef2021-10-21 17:55:48 +0200988 list_for_each_entry_reverse(slave, &mtd->partitions, node) {
Marek Behún91ef59c2021-07-15 19:21:02 +0200989 char name[32];
990 int part;
991
992 snprintf(name, sizeof(name), "partition@%llx", slave->offset);
993 part = fdt_add_subnode(blob, parts, name);
994 if (part < 0)
995 return false;
996
997 if (fdt_setprop_u32(blob, part, "reg", slave->offset) < 0)
998 return false;
999
1000 if (fdt_appendprop_u32(blob, part, "reg", slave->size) < 0)
1001 return false;
1002
1003 if (fdt_setprop_string(blob, part, "label", slave->name) < 0)
1004 return false;
1005
1006 if (!(slave->flags & MTD_WRITEABLE))
1007 if (fdt_setprop_empty(blob, part, "read-only") < 0)
1008 return false;
1009
1010 if (slave->flags & MTD_POWERUP_LOCK)
1011 if (fdt_setprop_empty(blob, part, "lock") < 0)
1012 return false;
1013 }
1014
1015 return true;
1016}
1017
Pali Rohárcbda3e22022-01-10 11:47:18 +01001018static void fixup_spi_nor_partitions(void *blob)
Marek Behún91ef59c2021-07-15 19:21:02 +02001019{
Pali Rohár3215c032022-08-01 23:58:42 +02001020 struct mtd_info *mtd = NULL;
1021 char mtd_path[64];
Marek Behún91ef59c2021-07-15 19:21:02 +02001022 int node;
1023
Pali Rohár3215c032022-08-01 23:58:42 +02001024 node = fdt_node_offset_by_compatible(gd->fdt_blob, -1, "jedec,spi-nor");
1025 if (node < 0)
1026 goto fail;
1027
1028 if (fdt_get_path(gd->fdt_blob, node, mtd_path, sizeof(mtd_path)) < 0)
1029 goto fail;
1030
1031 mtd = get_mtd_device_nm(mtd_path);
Marek Behún91ef59c2021-07-15 19:21:02 +02001032 if (IS_ERR_OR_NULL(mtd))
1033 goto fail;
1034
Pali Rohár3215c032022-08-01 23:58:42 +02001035 node = fdt_node_offset_by_compatible(blob, -1, "jedec,spi-nor");
Marek Behún91ef59c2021-07-15 19:21:02 +02001036 if (node < 0)
1037 goto fail;
1038
1039 if (!fixup_mtd_partitions(blob, node, mtd))
1040 goto fail;
1041
Marek Behún36feac92021-09-25 02:49:18 +02001042 put_mtd_device(mtd);
Pali Rohárcbda3e22022-01-10 11:47:18 +01001043 return;
Marek Behún91ef59c2021-07-15 19:21:02 +02001044
1045fail:
1046 printf("Failed fixing SPI NOR partitions!\n");
Marek Behún36feac92021-09-25 02:49:18 +02001047 if (!IS_ERR_OR_NULL(mtd))
1048 put_mtd_device(mtd);
Pali Rohárcbda3e22022-01-10 11:47:18 +01001049}
1050
1051int ft_board_setup(void *blob, struct bd_info *bd)
1052{
Pali Rohár1e0a9752022-07-29 13:29:07 +02001053 int node;
1054
1055 /*
1056 * U-Boot's FDT blob contains phy-reset-gpios in ethernet2
1057 * node when MCU controls all peripherals resets.
1058 * Fixup MCU GPIO nodes in PCIe and eth wan nodes in this case.
1059 */
1060 node = fdt_path_offset(gd->fdt_blob, "ethernet2");
1061 if (node >= 0 && fdt_getprop(gd->fdt_blob, node, "phy-reset-gpios", NULL)) {
1062 fixup_mcu_gpio_in_pcie_nodes(blob);
1063 fixup_mcu_gpio_in_eth_wan_node(blob);
1064 }
1065
Pali Rohárcbda3e22022-01-10 11:47:18 +01001066 fixup_spi_nor_partitions(blob);
Pali Rohár7cd41732022-03-02 12:47:56 +01001067 fixup_msata_port_nodes(blob);
Pali Rohár93a89c52022-03-02 12:47:58 +01001068 fixup_wwan_port_nodes(blob);
Pali Rohárcbda3e22022-01-10 11:47:18 +01001069
Marek Behún91ef59c2021-07-15 19:21:02 +02001070 return 0;
1071}
1072#endif