blob: 719e8750e60a622192fb8724f51ca727bed9e5d5 [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/*
3 * Copyright (C) 2017 Marek Behun <marek.behun@nic.cz>
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ú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>
22#include <fdt_support.h>
23#include <time.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060024#include <linux/bitops.h>
Simon Glass48b6c6b2019-11-14 12:57:16 -070025#include <u-boot/crc.h>
Marek Behún09e16b82017-06-09 19:28:45 +020026
Chris Packham1a07d212018-05-10 13:28:29 +120027#include "../drivers/ddr/marvell/a38x/ddr3_init.h"
Marek Behún09e16b82017-06-09 19:28:45 +020028#include <../serdes/a38x/high_speed_env_spec.h>
Pali Rohár0387f7f2022-04-08 16:30:12 +020029#include "../turris_atsha_otp.h"
Marek Behún09e16b82017-06-09 19:28:45 +020030
31DECLARE_GLOBAL_DATA_PTR;
32
Marek Behún91ef59c2021-07-15 19:21:02 +020033#define OMNIA_SPI_NOR_PATH "/soc/spi@10600/spi-nor@0"
34
Marek Behúnba53b6b2019-05-02 16:53:30 +020035#define OMNIA_I2C_BUS_NAME "i2c@11000->i2cmux@70->i2c@0"
36
37#define OMNIA_I2C_MCU_CHIP_ADDR 0x2a
38#define OMNIA_I2C_MCU_CHIP_LEN 1
39
40#define OMNIA_I2C_EEPROM_CHIP_ADDR 0x54
41#define OMNIA_I2C_EEPROM_CHIP_LEN 2
Marek Behún09e16b82017-06-09 19:28:45 +020042#define OMNIA_I2C_EEPROM_MAGIC 0x0341a034
43
Pali Rohár7fcda0c2021-11-09 17:14:02 +010044#define SYS_RSTOUT_MASK MVEBU_REGISTER(0x18260)
45#define SYS_RSTOUT_MASK_WD BIT(10)
46
47#define A385_WDT_GLOBAL_CTRL MVEBU_REGISTER(0x20300)
48#define A385_WDT_GLOBAL_RATIO_MASK GENMASK(18, 16)
49#define A385_WDT_GLOBAL_RATIO_SHIFT 16
50#define A385_WDT_GLOBAL_25MHZ BIT(10)
51#define A385_WDT_GLOBAL_ENABLE BIT(8)
52
53#define A385_WDT_GLOBAL_STATUS MVEBU_REGISTER(0x20304)
54#define A385_WDT_GLOBAL_EXPIRED BIT(31)
55
56#define A385_WDT_DURATION MVEBU_REGISTER(0x20334)
57
58#define A385_WD_RSTOUT_UNMASK MVEBU_REGISTER(0x20704)
59#define A385_WD_RSTOUT_UNMASK_GLOBAL BIT(8)
60
Marek Behúnba53b6b2019-05-02 16:53:30 +020061enum mcu_commands {
62 CMD_GET_STATUS_WORD = 0x01,
63 CMD_GET_RESET = 0x09,
64 CMD_WATCHDOG_STATE = 0x0b,
65};
66
67enum status_word_bits {
68 CARD_DET_STSBIT = 0x0010,
69 MSATA_IND_STSBIT = 0x0020,
70};
Marek Behún09e16b82017-06-09 19:28:45 +020071
Marek Behún09e16b82017-06-09 19:28:45 +020072/*
73 * Those values and defines are taken from the Marvell U-Boot version
74 * "u-boot-2013.01-2014_T3.0"
75 */
76#define OMNIA_GPP_OUT_ENA_LOW \
77 (~(BIT(1) | BIT(4) | BIT(6) | BIT(7) | BIT(8) | BIT(9) | \
78 BIT(10) | BIT(11) | BIT(19) | BIT(22) | BIT(23) | BIT(25) | \
79 BIT(26) | BIT(27) | BIT(29) | BIT(30) | BIT(31)))
80#define OMNIA_GPP_OUT_ENA_MID \
81 (~(BIT(0) | BIT(1) | BIT(2) | BIT(3) | BIT(4) | BIT(15) | \
82 BIT(16) | BIT(17) | BIT(18)))
83
84#define OMNIA_GPP_OUT_VAL_LOW 0x0
85#define OMNIA_GPP_OUT_VAL_MID 0x0
86#define OMNIA_GPP_POL_LOW 0x0
87#define OMNIA_GPP_POL_MID 0x0
88
89static struct serdes_map board_serdes_map_pex[] = {
90 {PEX0, SERDES_SPEED_5_GBPS, PEX_ROOT_COMPLEX_X1, 0, 0},
91 {USB3_HOST0, SERDES_SPEED_5_GBPS, SERDES_DEFAULT_MODE, 0, 0},
92 {PEX1, SERDES_SPEED_5_GBPS, PEX_ROOT_COMPLEX_X1, 0, 0},
93 {USB3_HOST1, SERDES_SPEED_5_GBPS, SERDES_DEFAULT_MODE, 0, 0},
94 {PEX2, SERDES_SPEED_5_GBPS, PEX_ROOT_COMPLEX_X1, 0, 0},
95 {SGMII2, SERDES_SPEED_1_25_GBPS, SERDES_DEFAULT_MODE, 0, 0}
96};
97
98static struct serdes_map board_serdes_map_sata[] = {
99 {SATA0, SERDES_SPEED_6_GBPS, SERDES_DEFAULT_MODE, 0, 0},
100 {USB3_HOST0, SERDES_SPEED_5_GBPS, SERDES_DEFAULT_MODE, 0, 0},
101 {PEX1, SERDES_SPEED_5_GBPS, PEX_ROOT_COMPLEX_X1, 0, 0},
102 {USB3_HOST1, SERDES_SPEED_5_GBPS, SERDES_DEFAULT_MODE, 0, 0},
103 {PEX2, SERDES_SPEED_5_GBPS, PEX_ROOT_COMPLEX_X1, 0, 0},
104 {SGMII2, SERDES_SPEED_1_25_GBPS, SERDES_DEFAULT_MODE, 0, 0}
105};
106
Marek Behúnba53b6b2019-05-02 16:53:30 +0200107static struct udevice *omnia_get_i2c_chip(const char *name, uint addr,
108 uint offset_len)
Marek Behún09e16b82017-06-09 19:28:45 +0200109{
110 struct udevice *bus, *dev;
Marek Behúnba53b6b2019-05-02 16:53:30 +0200111 int ret;
Marek Behún09e16b82017-06-09 19:28:45 +0200112
Marek Behúnba53b6b2019-05-02 16:53:30 +0200113 ret = uclass_get_device_by_name(UCLASS_I2C, OMNIA_I2C_BUS_NAME, &bus);
114 if (ret) {
115 printf("Cannot get I2C bus %s: uclass_get_device_by_name failed: %i\n",
116 OMNIA_I2C_BUS_NAME, ret);
117 return NULL;
Marek Behún09e16b82017-06-09 19:28:45 +0200118 }
119
Marek Behúnba53b6b2019-05-02 16:53:30 +0200120 ret = i2c_get_chip(bus, addr, offset_len, &dev);
Marek Behún09e16b82017-06-09 19:28:45 +0200121 if (ret) {
Marek Behúnba53b6b2019-05-02 16:53:30 +0200122 printf("Cannot get %s I2C chip: i2c_get_chip failed: %i\n",
123 name, ret);
124 return NULL;
Marek Behún09e16b82017-06-09 19:28:45 +0200125 }
126
Marek Behúnba53b6b2019-05-02 16:53:30 +0200127 return dev;
128}
Marek Behúnd0b374d2017-08-04 15:28:25 +0200129
Marek Behúnba53b6b2019-05-02 16:53:30 +0200130static int omnia_mcu_read(u8 cmd, void *buf, int len)
131{
132 struct udevice *chip;
133
134 chip = omnia_get_i2c_chip("MCU", OMNIA_I2C_MCU_CHIP_ADDR,
135 OMNIA_I2C_MCU_CHIP_LEN);
136 if (!chip)
137 return -ENODEV;
138
139 return dm_i2c_read(chip, cmd, buf, len);
140}
141
Marek Behúnba53b6b2019-05-02 16:53:30 +0200142static int omnia_mcu_write(u8 cmd, const void *buf, int len)
143{
144 struct udevice *chip;
145
146 chip = omnia_get_i2c_chip("MCU", OMNIA_I2C_MCU_CHIP_ADDR,
147 OMNIA_I2C_MCU_CHIP_LEN);
148 if (!chip)
149 return -ENODEV;
150
151 return dm_i2c_write(chip, cmd, buf, len);
152}
153
Pali Rohár7fcda0c2021-11-09 17:14:02 +0100154static void enable_a385_watchdog(unsigned int timeout_minutes)
155{
156 struct sar_freq_modes sar_freq;
157 u32 watchdog_freq;
158
159 printf("Enabling A385 watchdog with %u minutes timeout...\n",
160 timeout_minutes);
161
162 /*
163 * Use NBCLK clock (a.k.a. L2 clock) as watchdog input clock with
164 * its maximal ratio 7 instead of default fixed 25 MHz clock.
165 * It allows to set watchdog duration up to the 22 minutes.
166 */
167 clrsetbits_32(A385_WDT_GLOBAL_CTRL,
168 A385_WDT_GLOBAL_25MHZ | A385_WDT_GLOBAL_RATIO_MASK,
169 7 << A385_WDT_GLOBAL_RATIO_SHIFT);
170
171 /*
172 * Calculate watchdog clock frequency. It is defined by formula:
173 * freq = NBCLK / 2 / (2 ^ ratio)
174 * We set ratio to the maximal possible value 7.
175 */
176 get_sar_freq(&sar_freq);
177 watchdog_freq = sar_freq.nb_clk * 1000000 / 2 / (1 << 7);
178
179 /* Set watchdog duration */
180 writel(timeout_minutes * 60 * watchdog_freq, A385_WDT_DURATION);
181
182 /* Clear the watchdog expiration bit */
183 clrbits_32(A385_WDT_GLOBAL_STATUS, A385_WDT_GLOBAL_EXPIRED);
184
185 /* Enable watchdog timer */
186 setbits_32(A385_WDT_GLOBAL_CTRL, A385_WDT_GLOBAL_ENABLE);
187
188 /* Enable reset on watchdog */
189 setbits_32(A385_WD_RSTOUT_UNMASK, A385_WD_RSTOUT_UNMASK_GLOBAL);
190
191 /* Unmask reset for watchdog */
192 clrbits_32(SYS_RSTOUT_MASK, SYS_RSTOUT_MASK_WD);
193}
194
Marek Behúnba53b6b2019-05-02 16:53:30 +0200195static bool disable_mcu_watchdog(void)
196{
197 int ret;
198
199 puts("Disabling MCU watchdog... ");
200
201 ret = omnia_mcu_write(CMD_WATCHDOG_STATE, "\x00", 1);
202 if (ret) {
203 printf("omnia_mcu_write failed: %i\n", ret);
Marek Behún09e16b82017-06-09 19:28:45 +0200204 return false;
205 }
206
Marek Behúnba53b6b2019-05-02 16:53:30 +0200207 puts("disabled\n");
208
209 return true;
210}
Marek Behúnba53b6b2019-05-02 16:53:30 +0200211
212static bool omnia_detect_sata(void)
213{
214 int ret;
215 u16 stsword;
216
217 puts("MiniPCIe/mSATA card detection... ");
218
219 ret = omnia_mcu_read(CMD_GET_STATUS_WORD, &stsword, sizeof(stsword));
220 if (ret) {
221 printf("omnia_mcu_read failed: %i, defaulting to MiniPCIe card\n",
222 ret);
Marek Behún09e16b82017-06-09 19:28:45 +0200223 return false;
224 }
225
Marek Behúnba53b6b2019-05-02 16:53:30 +0200226 if (!(stsword & CARD_DET_STSBIT)) {
227 puts("none\n");
Marek Behún09e16b82017-06-09 19:28:45 +0200228 return false;
229 }
Marek Behúnba53b6b2019-05-02 16:53:30 +0200230
231 if (stsword & MSATA_IND_STSBIT)
232 puts("mSATA\n");
233 else
234 puts("MiniPCIe\n");
235
236 return stsword & MSATA_IND_STSBIT ? true : false;
Marek Behún09e16b82017-06-09 19:28:45 +0200237}
238
239int hws_board_topology_load(struct serdes_map **serdes_map_array, u8 *count)
240{
241 if (omnia_detect_sata()) {
242 *serdes_map_array = board_serdes_map_sata;
243 *count = ARRAY_SIZE(board_serdes_map_sata);
244 } else {
245 *serdes_map_array = board_serdes_map_pex;
246 *count = ARRAY_SIZE(board_serdes_map_pex);
247 }
248
249 return 0;
250}
251
252struct omnia_eeprom {
253 u32 magic;
254 u32 ramsize;
255 char region[4];
256 u32 crc;
257};
258
259static bool omnia_read_eeprom(struct omnia_eeprom *oep)
260{
Marek Behúnba53b6b2019-05-02 16:53:30 +0200261 struct udevice *chip;
262 u32 crc;
263 int ret;
Marek Behún09e16b82017-06-09 19:28:45 +0200264
Marek Behúnba53b6b2019-05-02 16:53:30 +0200265 chip = omnia_get_i2c_chip("EEPROM", OMNIA_I2C_EEPROM_CHIP_ADDR,
266 OMNIA_I2C_EEPROM_CHIP_LEN);
267
268 if (!chip)
Marek Behún09e16b82017-06-09 19:28:45 +0200269 return false;
Marek Behún09e16b82017-06-09 19:28:45 +0200270
Marek Behúnba53b6b2019-05-02 16:53:30 +0200271 ret = dm_i2c_read(chip, 0, (void *)oep, sizeof(*oep));
Marek Behún09e16b82017-06-09 19:28:45 +0200272 if (ret) {
Marek Behúnba53b6b2019-05-02 16:53:30 +0200273 printf("dm_i2c_read failed: %i, cannot read EEPROM\n", ret);
Marek Behún09e16b82017-06-09 19:28:45 +0200274 return false;
275 }
276
Marek Behúnba53b6b2019-05-02 16:53:30 +0200277 if (oep->magic != OMNIA_I2C_EEPROM_MAGIC) {
278 printf("bad EEPROM magic number (%08x, should be %08x)\n",
279 oep->magic, OMNIA_I2C_EEPROM_MAGIC);
280 return false;
Marek Behún09e16b82017-06-09 19:28:45 +0200281 }
282
Marek Behúnba53b6b2019-05-02 16:53:30 +0200283 crc = crc32(0, (void *)oep, sizeof(*oep) - 4);
284 if (crc != oep->crc) {
285 printf("bad EEPROM CRC (stored %08x, computed %08x)\n",
286 oep->crc, crc);
Marek Behún09e16b82017-06-09 19:28:45 +0200287 return false;
288 }
289
290 return true;
291}
292
Marek Behún77652c72019-05-02 16:53:33 +0200293static int omnia_get_ram_size_gb(void)
294{
295 static int ram_size;
296 struct omnia_eeprom oep;
297
298 if (!ram_size) {
299 /* Get the board config from EEPROM */
300 if (omnia_read_eeprom(&oep)) {
301 debug("Memory config in EEPROM: 0x%02x\n", oep.ramsize);
302
303 if (oep.ramsize == 0x2)
304 ram_size = 2;
305 else
306 ram_size = 1;
307 } else {
308 /* Hardcoded fallback */
309 puts("Memory config from EEPROM read failed!\n");
310 puts("Falling back to default 1 GiB!\n");
311 ram_size = 1;
312 }
313 }
314
315 return ram_size;
316}
317
Marek Behún09e16b82017-06-09 19:28:45 +0200318/*
319 * Define the DDR layout / topology here in the board file. This will
320 * be used by the DDR3 init code in the SPL U-Boot version to configure
321 * the DDR3 controller.
322 */
Chris Packham1a07d212018-05-10 13:28:29 +1200323static struct mv_ddr_topology_map board_topology_map_1g = {
324 DEBUG_LEVEL_ERROR,
Marek Behún09e16b82017-06-09 19:28:45 +0200325 0x1, /* active interfaces */
326 /* cs_mask, mirror, dqs_swap, ck_swap X PUPs */
327 { { { {0x1, 0, 0, 0},
328 {0x1, 0, 0, 0},
329 {0x1, 0, 0, 0},
330 {0x1, 0, 0, 0},
331 {0x1, 0, 0, 0} },
332 SPEED_BIN_DDR_1600K, /* speed_bin */
Chris Packham1a07d212018-05-10 13:28:29 +1200333 MV_DDR_DEV_WIDTH_16BIT, /* memory_width */
334 MV_DDR_DIE_CAP_4GBIT, /* mem_size */
Chris Packham4bf81db2018-12-03 14:26:49 +1300335 MV_DDR_FREQ_800, /* frequency */
Chris Packhamdd092bd2017-11-29 10:38:34 +1300336 0, 0, /* cas_wl cas_l */
Chris Packham3a09e132018-05-10 13:28:30 +1200337 MV_DDR_TEMP_NORMAL, /* temperature */
338 MV_DDR_TIM_2T} }, /* timing */
Chris Packham1a07d212018-05-10 13:28:29 +1200339 BUS_MASK_32BIT, /* Busses mask */
340 MV_DDR_CFG_DEFAULT, /* ddr configuration data source */
Moti Buskila498475e2021-02-19 17:11:19 +0100341 NOT_COMBINED, /* ddr twin-die combined */
Chris Packham1a07d212018-05-10 13:28:29 +1200342 { {0} }, /* raw spd data */
343 {0} /* timing parameters */
Marek Behún09e16b82017-06-09 19:28:45 +0200344};
345
Chris Packham1a07d212018-05-10 13:28:29 +1200346static struct mv_ddr_topology_map board_topology_map_2g = {
347 DEBUG_LEVEL_ERROR,
Marek Behún09e16b82017-06-09 19:28:45 +0200348 0x1, /* active interfaces */
349 /* cs_mask, mirror, dqs_swap, ck_swap X PUPs */
350 { { { {0x1, 0, 0, 0},
351 {0x1, 0, 0, 0},
352 {0x1, 0, 0, 0},
353 {0x1, 0, 0, 0},
354 {0x1, 0, 0, 0} },
355 SPEED_BIN_DDR_1600K, /* speed_bin */
Chris Packham1a07d212018-05-10 13:28:29 +1200356 MV_DDR_DEV_WIDTH_16BIT, /* memory_width */
357 MV_DDR_DIE_CAP_8GBIT, /* mem_size */
Chris Packham4bf81db2018-12-03 14:26:49 +1300358 MV_DDR_FREQ_800, /* frequency */
Chris Packhamdd092bd2017-11-29 10:38:34 +1300359 0, 0, /* cas_wl cas_l */
Chris Packham3a09e132018-05-10 13:28:30 +1200360 MV_DDR_TEMP_NORMAL, /* temperature */
361 MV_DDR_TIM_2T} }, /* timing */
Chris Packham1a07d212018-05-10 13:28:29 +1200362 BUS_MASK_32BIT, /* Busses mask */
363 MV_DDR_CFG_DEFAULT, /* ddr configuration data source */
Moti Buskila498475e2021-02-19 17:11:19 +0100364 NOT_COMBINED, /* ddr twin-die combined */
Chris Packham1a07d212018-05-10 13:28:29 +1200365 { {0} }, /* raw spd data */
366 {0} /* timing parameters */
Marek Behún09e16b82017-06-09 19:28:45 +0200367};
368
Chris Packham1a07d212018-05-10 13:28:29 +1200369struct mv_ddr_topology_map *mv_ddr_topology_map_get(void)
Marek Behún09e16b82017-06-09 19:28:45 +0200370{
Marek Behún77652c72019-05-02 16:53:33 +0200371 if (omnia_get_ram_size_gb() == 2)
Marek Behún09e16b82017-06-09 19:28:45 +0200372 return &board_topology_map_2g;
Marek Behún77652c72019-05-02 16:53:33 +0200373 else
374 return &board_topology_map_1g;
Marek Behún09e16b82017-06-09 19:28:45 +0200375}
376
Marek Behún09e16b82017-06-09 19:28:45 +0200377static int set_regdomain(void)
378{
379 struct omnia_eeprom oep;
380 char rd[3] = {' ', ' ', 0};
381
382 if (omnia_read_eeprom(&oep))
383 memcpy(rd, &oep.region, 2);
384 else
385 puts("EEPROM regdomain read failed.\n");
386
387 printf("Regdomain set to %s\n", rd);
Simon Glass6a38e412017-08-03 12:22:09 -0600388 return env_set("regdomain", rd);
Marek Behún09e16b82017-06-09 19:28:45 +0200389}
Marek Behún0f2e66a2019-05-02 16:53:37 +0200390
Marek Behún0f2e66a2019-05-02 16:53:37 +0200391static void handle_reset_button(void)
392{
Pali Rohár905c3bf2021-06-14 16:45:58 +0200393 const char * const vars[1] = { "bootcmd_rescue", };
Marek Behún0f2e66a2019-05-02 16:53:37 +0200394 int ret;
395 u8 reset_status;
396
Pali Rohár905c3bf2021-06-14 16:45:58 +0200397 /*
398 * Ensure that bootcmd_rescue has always stock value, so that running
399 * run bootcmd_rescue
400 * always works correctly.
401 */
402 env_set_default_vars(1, (char * const *)vars, 0);
403
Marek Behún0f2e66a2019-05-02 16:53:37 +0200404 ret = omnia_mcu_read(CMD_GET_RESET, &reset_status, 1);
405 if (ret) {
406 printf("omnia_mcu_read failed: %i, reset status unknown!\n",
407 ret);
408 return;
409 }
410
411 env_set_ulong("omnia_reset", reset_status);
412
413 if (reset_status) {
Pali Rohár905c3bf2021-06-14 16:45:58 +0200414 const char * const vars[2] = {
Marek Behún09f8de22021-05-28 10:00:49 +0200415 "bootcmd",
Marek Behún09f8de22021-05-28 10:00:49 +0200416 "distro_bootcmd",
417 };
418
419 /*
420 * Set the above envs to their default values, in case the user
421 * managed to break them.
422 */
Pali Rohár905c3bf2021-06-14 16:45:58 +0200423 env_set_default_vars(2, (char * const *)vars, 0);
Marek Behún09f8de22021-05-28 10:00:49 +0200424
425 /* Ensure bootcmd_rescue is used by distroboot */
426 env_set("boot_targets", "rescue");
427
Marek Behún0f2e66a2019-05-02 16:53:37 +0200428 printf("RESET button was pressed, overwriting bootcmd!\n");
Marek Behún09f8de22021-05-28 10:00:49 +0200429 } else {
430 /*
431 * In case the user somehow managed to save environment with
432 * boot_targets=rescue, reset boot_targets to default value.
433 * This could happen in subsequent commands if bootcmd_rescue
434 * failed.
435 */
436 if (!strcmp(env_get("boot_targets"), "rescue")) {
437 const char * const vars[1] = {
438 "boot_targets",
439 };
440
441 env_set_default_vars(1, (char * const *)vars, 0);
442 }
Marek Behún0f2e66a2019-05-02 16:53:37 +0200443 }
444}
Marek Behún09e16b82017-06-09 19:28:45 +0200445
446int board_early_init_f(void)
447{
Marek Behún09e16b82017-06-09 19:28:45 +0200448 /* Configure MPP */
449 writel(0x11111111, MVEBU_MPP_BASE + 0x00);
450 writel(0x11111111, MVEBU_MPP_BASE + 0x04);
451 writel(0x11244011, MVEBU_MPP_BASE + 0x08);
452 writel(0x22222111, MVEBU_MPP_BASE + 0x0c);
453 writel(0x22200002, MVEBU_MPP_BASE + 0x10);
454 writel(0x30042022, MVEBU_MPP_BASE + 0x14);
455 writel(0x55550555, MVEBU_MPP_BASE + 0x18);
456 writel(0x00005550, MVEBU_MPP_BASE + 0x1c);
457
458 /* Set GPP Out value */
459 writel(OMNIA_GPP_OUT_VAL_LOW, MVEBU_GPIO0_BASE + 0x00);
460 writel(OMNIA_GPP_OUT_VAL_MID, MVEBU_GPIO1_BASE + 0x00);
461
462 /* Set GPP Polarity */
463 writel(OMNIA_GPP_POL_LOW, MVEBU_GPIO0_BASE + 0x0c);
464 writel(OMNIA_GPP_POL_MID, MVEBU_GPIO1_BASE + 0x0c);
465
466 /* Set GPP Out Enable */
467 writel(OMNIA_GPP_OUT_ENA_LOW, MVEBU_GPIO0_BASE + 0x04);
468 writel(OMNIA_GPP_OUT_ENA_MID, MVEBU_GPIO1_BASE + 0x04);
469
Marek Behún09e16b82017-06-09 19:28:45 +0200470 return 0;
471}
472
Marek Behúnf3556162021-08-16 15:19:39 +0200473void spl_board_init(void)
474{
475 /*
476 * If booting from UART, disable MCU watchdog in SPL, since uploading
Pali Rohár7fcda0c2021-11-09 17:14:02 +0100477 * U-Boot proper can take too much time and trigger it. Instead enable
478 * A385 watchdog with very high timeout (10 minutes) to prevent hangup.
Marek Behúnf3556162021-08-16 15:19:39 +0200479 */
Pali Rohár7fcda0c2021-11-09 17:14:02 +0100480 if (get_boot_device() == BOOT_DEVICE_UART) {
481 enable_a385_watchdog(10);
Marek Behúnf3556162021-08-16 15:19:39 +0200482 disable_mcu_watchdog();
Pali Rohár7fcda0c2021-11-09 17:14:02 +0100483 }
Marek Behúnf3556162021-08-16 15:19:39 +0200484}
485
Pali Rohárcbda3e22022-01-10 11:47:18 +0100486#if IS_ENABLED(CONFIG_OF_BOARD_FIXUP) || IS_ENABLED(CONFIG_OF_BOARD_SETUP)
487
488static void fixup_serdes_0_nodes(void *blob)
489{
490 bool mode_sata;
491 int node;
492
493 /*
494 * Determine if SerDes 0 is configured to SATA mode.
495 * We do this instead of calling omnia_detect_sata() to avoid another
496 * call to the MCU. By this time the common PHYs are initialized (it is
497 * done in SPL), so we can read this common PHY register.
498 */
499 mode_sata = (readl(MVEBU_REGISTER(0x183fc)) & GENMASK(3, 0)) == 2;
500
501 /*
502 * We're either adding status = "disabled" property, or changing
503 * status = "okay" to status = "disabled". In both cases we'll need more
504 * space. Increase the size a little.
505 */
506 if (fdt_increase_size(blob, 32) < 0) {
507 printf("Cannot increase FDT size!\n");
508 return;
509 }
510
511 /* If mSATA card is not present, disable SATA DT node */
512 if (!mode_sata) {
513 fdt_for_each_node_by_compatible(node, blob, -1,
514 "marvell,armada-380-ahci") {
515 if (!fdtdec_get_is_enabled(blob, node))
516 continue;
517
518 if (fdt_status_disabled(blob, node) < 0)
519 printf("Cannot disable SATA DT node!\n");
520 else
521 debug("Disabled SATA DT node\n");
522
523 break;
524 }
525
526 return;
527 }
528
529 /* Otherwise disable PCIe port 0 DT node (MiniPCIe / mSATA port) */
530 fdt_for_each_node_by_compatible(node, blob, -1,
531 "marvell,armada-370-pcie") {
532 int port;
533
534 if (!fdtdec_get_is_enabled(blob, node))
535 continue;
536
537 fdt_for_each_subnode (port, blob, node) {
538 if (!fdtdec_get_is_enabled(blob, port))
539 continue;
540
541 if (fdtdec_get_int(blob, port, "marvell,pcie-port",
542 -1) != 0)
543 continue;
544
545 if (fdt_status_disabled(blob, port) < 0)
546 printf("Cannot disable PCIe port 0 DT node!\n");
547 else
548 debug("Disabled PCIe port 0 DT node\n");
549
550 return;
551 }
552 }
553}
554
555#endif
556
557#if IS_ENABLED(CONFIG_OF_BOARD_FIXUP)
558int board_fix_fdt(void *blob)
559{
560 fixup_serdes_0_nodes(blob);
561
562 return 0;
563}
564#endif
565
Marek Behún09e16b82017-06-09 19:28:45 +0200566int board_init(void)
567{
Marek Behún4dfc57e2019-05-02 16:53:31 +0200568 /* address of boot parameters */
Marek Behún09e16b82017-06-09 19:28:45 +0200569 gd->bd->bi_boot_params = mvebu_sdram_bar(0) + 0x100;
570
Marek Behún88dc0242021-08-16 15:19:40 +0200571 return 0;
572}
573
574int board_late_init(void)
575{
Marek Behúnf3556162021-08-16 15:19:39 +0200576 /*
577 * If not booting from UART, MCU watchdog was not disabled in SPL,
578 * disable it now.
579 */
580 if (get_boot_device() != BOOT_DEVICE_UART)
581 disable_mcu_watchdog();
Marek Behún09e16b82017-06-09 19:28:45 +0200582
Marek Behún09e16b82017-06-09 19:28:45 +0200583 set_regdomain();
Marek Behún0f2e66a2019-05-02 16:53:37 +0200584 handle_reset_button();
Marek Behúndb1e5c62019-05-24 14:57:53 +0200585 pci_init();
Marek Behún09e16b82017-06-09 19:28:45 +0200586
587 return 0;
588}
589
Marek Behúnab9447f2021-10-09 19:33:44 +0200590int show_board_info(void)
Marek Behún09e16b82017-06-09 19:28:45 +0200591{
592 u32 version_num, serial_num;
Pali Rohár0387f7f2022-04-08 16:30:12 +0200593 int err;
Marek Behún09e16b82017-06-09 19:28:45 +0200594
Pali Rohár0387f7f2022-04-08 16:30:12 +0200595 err = turris_atsha_otp_get_serial_number(&version_num, &serial_num);
Marek Behúnab9447f2021-10-09 19:33:44 +0200596 printf("Model: Turris Omnia\n");
Marek Behúnc4ba72a2019-05-02 16:53:34 +0200597 printf(" RAM size: %i MiB\n", omnia_get_ram_size_gb() * 1024);
Marek Behún09e16b82017-06-09 19:28:45 +0200598 if (err)
Marek Behúnc4ba72a2019-05-02 16:53:34 +0200599 printf(" Serial Number: unknown\n");
Marek Behún09e16b82017-06-09 19:28:45 +0200600 else
Marek Behúnc4ba72a2019-05-02 16:53:34 +0200601 printf(" Serial Number: %08X%08X\n", be32_to_cpu(version_num),
602 be32_to_cpu(serial_num));
Marek Behún09e16b82017-06-09 19:28:45 +0200603
604 return 0;
605}
606
Marek Behún09e16b82017-06-09 19:28:45 +0200607int misc_init_r(void)
608{
Pali Rohár0387f7f2022-04-08 16:30:12 +0200609 turris_atsha_otp_init_mac_addresses();
Marek Behún09e16b82017-06-09 19:28:45 +0200610 return 0;
611}
612
Marek Behún91ef59c2021-07-15 19:21:02 +0200613#if defined(CONFIG_OF_BOARD_SETUP)
614/*
615 * I plan to generalize this function and move it to common/fdt_support.c.
616 * This will require some more work on multiple boards, though, so for now leave
617 * it here.
618 */
619static bool fixup_mtd_partitions(void *blob, int offset, struct mtd_info *mtd)
620{
621 struct mtd_info *slave;
622 int parts;
623
624 parts = fdt_subnode_offset(blob, offset, "partitions");
625 if (parts < 0)
626 return false;
627
628 if (fdt_del_node(blob, parts) < 0)
629 return false;
630
631 parts = fdt_add_subnode(blob, offset, "partitions");
632 if (parts < 0)
633 return false;
634
635 if (fdt_setprop_u32(blob, parts, "#address-cells", 1) < 0)
636 return false;
637
638 if (fdt_setprop_u32(blob, parts, "#size-cells", 1) < 0)
639 return false;
640
641 if (fdt_setprop_string(blob, parts, "compatible",
642 "fixed-partitions") < 0)
643 return false;
644
645 mtd_probe_devices();
646
Pali Rohárd8210ef2021-10-21 17:55:48 +0200647 list_for_each_entry_reverse(slave, &mtd->partitions, node) {
Marek Behún91ef59c2021-07-15 19:21:02 +0200648 char name[32];
649 int part;
650
651 snprintf(name, sizeof(name), "partition@%llx", slave->offset);
652 part = fdt_add_subnode(blob, parts, name);
653 if (part < 0)
654 return false;
655
656 if (fdt_setprop_u32(blob, part, "reg", slave->offset) < 0)
657 return false;
658
659 if (fdt_appendprop_u32(blob, part, "reg", slave->size) < 0)
660 return false;
661
662 if (fdt_setprop_string(blob, part, "label", slave->name) < 0)
663 return false;
664
665 if (!(slave->flags & MTD_WRITEABLE))
666 if (fdt_setprop_empty(blob, part, "read-only") < 0)
667 return false;
668
669 if (slave->flags & MTD_POWERUP_LOCK)
670 if (fdt_setprop_empty(blob, part, "lock") < 0)
671 return false;
672 }
673
674 return true;
675}
676
Pali Rohárcbda3e22022-01-10 11:47:18 +0100677static void fixup_spi_nor_partitions(void *blob)
Marek Behún91ef59c2021-07-15 19:21:02 +0200678{
679 struct mtd_info *mtd;
680 int node;
681
682 mtd = get_mtd_device_nm(OMNIA_SPI_NOR_PATH);
683 if (IS_ERR_OR_NULL(mtd))
684 goto fail;
685
686 node = fdt_path_offset(blob, OMNIA_SPI_NOR_PATH);
687 if (node < 0)
688 goto fail;
689
690 if (!fixup_mtd_partitions(blob, node, mtd))
691 goto fail;
692
Marek Behún36feac92021-09-25 02:49:18 +0200693 put_mtd_device(mtd);
Pali Rohárcbda3e22022-01-10 11:47:18 +0100694 return;
Marek Behún91ef59c2021-07-15 19:21:02 +0200695
696fail:
697 printf("Failed fixing SPI NOR partitions!\n");
Marek Behún36feac92021-09-25 02:49:18 +0200698 if (!IS_ERR_OR_NULL(mtd))
699 put_mtd_device(mtd);
Pali Rohárcbda3e22022-01-10 11:47:18 +0100700}
701
702int ft_board_setup(void *blob, struct bd_info *bd)
703{
704 fixup_spi_nor_partitions(blob);
705 fixup_serdes_0_nodes(blob);
706
Marek Behún91ef59c2021-07-15 19:21:02 +0200707 return 0;
708}
709#endif