blob: ae24d14b76263d3b976401116fc99608922301b4 [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 Glass274e0b02020-05-10 11:39:56 -060017#include <net.h>
Marek Behún09e16b82017-06-09 19:28:45 +020018#include <netdev.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060019#include <asm/global_data.h>
Marek Behún09e16b82017-06-09 19:28:45 +020020#include <asm/io.h>
21#include <asm/arch/cpu.h>
22#include <asm/arch/soc.h>
23#include <dm/uclass.h>
24#include <fdt_support.h>
25#include <time.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060026#include <linux/bitops.h>
Simon Glass48b6c6b2019-11-14 12:57:16 -070027#include <u-boot/crc.h>
Marek Behún09e16b82017-06-09 19:28:45 +020028# include <atsha204a-i2c.h>
Marek Behún09e16b82017-06-09 19:28:45 +020029
Chris Packham1a07d212018-05-10 13:28:29 +120030#include "../drivers/ddr/marvell/a38x/ddr3_init.h"
Marek Behún09e16b82017-06-09 19:28:45 +020031#include <../serdes/a38x/high_speed_env_spec.h>
32
33DECLARE_GLOBAL_DATA_PTR;
34
Marek Behún91ef59c2021-07-15 19:21:02 +020035#define OMNIA_SPI_NOR_PATH "/soc/spi@10600/spi-nor@0"
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ár7fcda0c2021-11-09 17:14:02 +010046#define SYS_RSTOUT_MASK MVEBU_REGISTER(0x18260)
47#define SYS_RSTOUT_MASK_WD BIT(10)
48
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únba53b6b2019-05-02 16:53:30 +020063enum mcu_commands {
64 CMD_GET_STATUS_WORD = 0x01,
65 CMD_GET_RESET = 0x09,
66 CMD_WATCHDOG_STATE = 0x0b,
67};
68
69enum status_word_bits {
70 CARD_DET_STSBIT = 0x0010,
71 MSATA_IND_STSBIT = 0x0020,
72};
Marek Behún09e16b82017-06-09 19:28:45 +020073
74#define OMNIA_ATSHA204_OTP_VERSION 0
75#define OMNIA_ATSHA204_OTP_SERIAL 1
76#define OMNIA_ATSHA204_OTP_MAC0 3
77#define OMNIA_ATSHA204_OTP_MAC1 4
78
Marek Behún09e16b82017-06-09 19:28:45 +020079/*
80 * Those values and defines are taken from the Marvell U-Boot version
81 * "u-boot-2013.01-2014_T3.0"
82 */
83#define OMNIA_GPP_OUT_ENA_LOW \
84 (~(BIT(1) | BIT(4) | BIT(6) | BIT(7) | BIT(8) | BIT(9) | \
85 BIT(10) | BIT(11) | BIT(19) | BIT(22) | BIT(23) | BIT(25) | \
86 BIT(26) | BIT(27) | BIT(29) | BIT(30) | BIT(31)))
87#define OMNIA_GPP_OUT_ENA_MID \
88 (~(BIT(0) | BIT(1) | BIT(2) | BIT(3) | BIT(4) | BIT(15) | \
89 BIT(16) | BIT(17) | BIT(18)))
90
91#define OMNIA_GPP_OUT_VAL_LOW 0x0
92#define OMNIA_GPP_OUT_VAL_MID 0x0
93#define OMNIA_GPP_POL_LOW 0x0
94#define OMNIA_GPP_POL_MID 0x0
95
96static struct serdes_map board_serdes_map_pex[] = {
97 {PEX0, SERDES_SPEED_5_GBPS, PEX_ROOT_COMPLEX_X1, 0, 0},
98 {USB3_HOST0, SERDES_SPEED_5_GBPS, SERDES_DEFAULT_MODE, 0, 0},
99 {PEX1, SERDES_SPEED_5_GBPS, PEX_ROOT_COMPLEX_X1, 0, 0},
100 {USB3_HOST1, SERDES_SPEED_5_GBPS, SERDES_DEFAULT_MODE, 0, 0},
101 {PEX2, SERDES_SPEED_5_GBPS, PEX_ROOT_COMPLEX_X1, 0, 0},
102 {SGMII2, SERDES_SPEED_1_25_GBPS, SERDES_DEFAULT_MODE, 0, 0}
103};
104
105static struct serdes_map board_serdes_map_sata[] = {
106 {SATA0, SERDES_SPEED_6_GBPS, SERDES_DEFAULT_MODE, 0, 0},
107 {USB3_HOST0, SERDES_SPEED_5_GBPS, SERDES_DEFAULT_MODE, 0, 0},
108 {PEX1, SERDES_SPEED_5_GBPS, PEX_ROOT_COMPLEX_X1, 0, 0},
109 {USB3_HOST1, SERDES_SPEED_5_GBPS, SERDES_DEFAULT_MODE, 0, 0},
110 {PEX2, SERDES_SPEED_5_GBPS, PEX_ROOT_COMPLEX_X1, 0, 0},
111 {SGMII2, SERDES_SPEED_1_25_GBPS, SERDES_DEFAULT_MODE, 0, 0}
112};
113
Marek Behúnba53b6b2019-05-02 16:53:30 +0200114static struct udevice *omnia_get_i2c_chip(const char *name, uint addr,
115 uint offset_len)
Marek Behún09e16b82017-06-09 19:28:45 +0200116{
117 struct udevice *bus, *dev;
Marek Behúnba53b6b2019-05-02 16:53:30 +0200118 int ret;
Marek Behún09e16b82017-06-09 19:28:45 +0200119
Marek Behúnba53b6b2019-05-02 16:53:30 +0200120 ret = uclass_get_device_by_name(UCLASS_I2C, OMNIA_I2C_BUS_NAME, &bus);
121 if (ret) {
122 printf("Cannot get I2C bus %s: uclass_get_device_by_name failed: %i\n",
123 OMNIA_I2C_BUS_NAME, ret);
124 return NULL;
Marek Behún09e16b82017-06-09 19:28:45 +0200125 }
126
Marek Behúnba53b6b2019-05-02 16:53:30 +0200127 ret = i2c_get_chip(bus, addr, offset_len, &dev);
Marek Behún09e16b82017-06-09 19:28:45 +0200128 if (ret) {
Marek Behúnba53b6b2019-05-02 16:53:30 +0200129 printf("Cannot get %s I2C chip: i2c_get_chip failed: %i\n",
130 name, ret);
131 return NULL;
Marek Behún09e16b82017-06-09 19:28:45 +0200132 }
133
Marek Behúnba53b6b2019-05-02 16:53:30 +0200134 return dev;
135}
Marek Behúnd0b374d2017-08-04 15:28:25 +0200136
Marek Behúnba53b6b2019-05-02 16:53:30 +0200137static int omnia_mcu_read(u8 cmd, void *buf, int len)
138{
139 struct udevice *chip;
140
141 chip = omnia_get_i2c_chip("MCU", OMNIA_I2C_MCU_CHIP_ADDR,
142 OMNIA_I2C_MCU_CHIP_LEN);
143 if (!chip)
144 return -ENODEV;
145
146 return dm_i2c_read(chip, cmd, buf, len);
147}
148
Marek Behúnba53b6b2019-05-02 16:53:30 +0200149static int omnia_mcu_write(u8 cmd, const void *buf, int len)
150{
151 struct udevice *chip;
152
153 chip = omnia_get_i2c_chip("MCU", OMNIA_I2C_MCU_CHIP_ADDR,
154 OMNIA_I2C_MCU_CHIP_LEN);
155 if (!chip)
156 return -ENODEV;
157
158 return dm_i2c_write(chip, cmd, buf, len);
159}
160
Pali Rohár7fcda0c2021-11-09 17:14:02 +0100161static void enable_a385_watchdog(unsigned int timeout_minutes)
162{
163 struct sar_freq_modes sar_freq;
164 u32 watchdog_freq;
165
166 printf("Enabling A385 watchdog with %u minutes timeout...\n",
167 timeout_minutes);
168
169 /*
170 * Use NBCLK clock (a.k.a. L2 clock) as watchdog input clock with
171 * its maximal ratio 7 instead of default fixed 25 MHz clock.
172 * It allows to set watchdog duration up to the 22 minutes.
173 */
174 clrsetbits_32(A385_WDT_GLOBAL_CTRL,
175 A385_WDT_GLOBAL_25MHZ | A385_WDT_GLOBAL_RATIO_MASK,
176 7 << A385_WDT_GLOBAL_RATIO_SHIFT);
177
178 /*
179 * Calculate watchdog clock frequency. It is defined by formula:
180 * freq = NBCLK / 2 / (2 ^ ratio)
181 * We set ratio to the maximal possible value 7.
182 */
183 get_sar_freq(&sar_freq);
184 watchdog_freq = sar_freq.nb_clk * 1000000 / 2 / (1 << 7);
185
186 /* Set watchdog duration */
187 writel(timeout_minutes * 60 * watchdog_freq, A385_WDT_DURATION);
188
189 /* Clear the watchdog expiration bit */
190 clrbits_32(A385_WDT_GLOBAL_STATUS, A385_WDT_GLOBAL_EXPIRED);
191
192 /* Enable watchdog timer */
193 setbits_32(A385_WDT_GLOBAL_CTRL, A385_WDT_GLOBAL_ENABLE);
194
195 /* Enable reset on watchdog */
196 setbits_32(A385_WD_RSTOUT_UNMASK, A385_WD_RSTOUT_UNMASK_GLOBAL);
197
198 /* Unmask reset for watchdog */
199 clrbits_32(SYS_RSTOUT_MASK, SYS_RSTOUT_MASK_WD);
200}
201
Marek Behúnba53b6b2019-05-02 16:53:30 +0200202static bool disable_mcu_watchdog(void)
203{
204 int ret;
205
206 puts("Disabling MCU watchdog... ");
207
208 ret = omnia_mcu_write(CMD_WATCHDOG_STATE, "\x00", 1);
209 if (ret) {
210 printf("omnia_mcu_write failed: %i\n", ret);
Marek Behún09e16b82017-06-09 19:28:45 +0200211 return false;
212 }
213
Marek Behúnba53b6b2019-05-02 16:53:30 +0200214 puts("disabled\n");
215
216 return true;
217}
Marek Behúnba53b6b2019-05-02 16:53:30 +0200218
219static bool omnia_detect_sata(void)
220{
221 int ret;
222 u16 stsword;
223
224 puts("MiniPCIe/mSATA card detection... ");
225
226 ret = omnia_mcu_read(CMD_GET_STATUS_WORD, &stsword, sizeof(stsword));
227 if (ret) {
228 printf("omnia_mcu_read failed: %i, defaulting to MiniPCIe card\n",
229 ret);
Marek Behún09e16b82017-06-09 19:28:45 +0200230 return false;
231 }
232
Marek Behúnba53b6b2019-05-02 16:53:30 +0200233 if (!(stsword & CARD_DET_STSBIT)) {
234 puts("none\n");
Marek Behún09e16b82017-06-09 19:28:45 +0200235 return false;
236 }
Marek Behúnba53b6b2019-05-02 16:53:30 +0200237
238 if (stsword & MSATA_IND_STSBIT)
239 puts("mSATA\n");
240 else
241 puts("MiniPCIe\n");
242
243 return stsword & MSATA_IND_STSBIT ? true : false;
Marek Behún09e16b82017-06-09 19:28:45 +0200244}
245
246int hws_board_topology_load(struct serdes_map **serdes_map_array, u8 *count)
247{
248 if (omnia_detect_sata()) {
249 *serdes_map_array = board_serdes_map_sata;
250 *count = ARRAY_SIZE(board_serdes_map_sata);
251 } else {
252 *serdes_map_array = board_serdes_map_pex;
253 *count = ARRAY_SIZE(board_serdes_map_pex);
254 }
255
256 return 0;
257}
258
259struct omnia_eeprom {
260 u32 magic;
261 u32 ramsize;
262 char region[4];
263 u32 crc;
264};
265
266static bool omnia_read_eeprom(struct omnia_eeprom *oep)
267{
Marek Behúnba53b6b2019-05-02 16:53:30 +0200268 struct udevice *chip;
269 u32 crc;
270 int ret;
Marek Behún09e16b82017-06-09 19:28:45 +0200271
Marek Behúnba53b6b2019-05-02 16:53:30 +0200272 chip = omnia_get_i2c_chip("EEPROM", OMNIA_I2C_EEPROM_CHIP_ADDR,
273 OMNIA_I2C_EEPROM_CHIP_LEN);
274
275 if (!chip)
Marek Behún09e16b82017-06-09 19:28:45 +0200276 return false;
Marek Behún09e16b82017-06-09 19:28:45 +0200277
Marek Behúnba53b6b2019-05-02 16:53:30 +0200278 ret = dm_i2c_read(chip, 0, (void *)oep, sizeof(*oep));
Marek Behún09e16b82017-06-09 19:28:45 +0200279 if (ret) {
Marek Behúnba53b6b2019-05-02 16:53:30 +0200280 printf("dm_i2c_read failed: %i, cannot read EEPROM\n", ret);
Marek Behún09e16b82017-06-09 19:28:45 +0200281 return false;
282 }
283
Marek Behúnba53b6b2019-05-02 16:53:30 +0200284 if (oep->magic != OMNIA_I2C_EEPROM_MAGIC) {
285 printf("bad EEPROM magic number (%08x, should be %08x)\n",
286 oep->magic, OMNIA_I2C_EEPROM_MAGIC);
287 return false;
Marek Behún09e16b82017-06-09 19:28:45 +0200288 }
289
Marek Behúnba53b6b2019-05-02 16:53:30 +0200290 crc = crc32(0, (void *)oep, sizeof(*oep) - 4);
291 if (crc != oep->crc) {
292 printf("bad EEPROM CRC (stored %08x, computed %08x)\n",
293 oep->crc, crc);
Marek Behún09e16b82017-06-09 19:28:45 +0200294 return false;
295 }
296
297 return true;
298}
299
Marek Behún77652c72019-05-02 16:53:33 +0200300static int omnia_get_ram_size_gb(void)
301{
302 static int ram_size;
303 struct omnia_eeprom oep;
304
305 if (!ram_size) {
306 /* Get the board config from EEPROM */
307 if (omnia_read_eeprom(&oep)) {
308 debug("Memory config in EEPROM: 0x%02x\n", oep.ramsize);
309
310 if (oep.ramsize == 0x2)
311 ram_size = 2;
312 else
313 ram_size = 1;
314 } else {
315 /* Hardcoded fallback */
316 puts("Memory config from EEPROM read failed!\n");
317 puts("Falling back to default 1 GiB!\n");
318 ram_size = 1;
319 }
320 }
321
322 return ram_size;
323}
324
Marek Behún09e16b82017-06-09 19:28:45 +0200325/*
326 * Define the DDR layout / topology here in the board file. This will
327 * be used by the DDR3 init code in the SPL U-Boot version to configure
328 * the DDR3 controller.
329 */
Chris Packham1a07d212018-05-10 13:28:29 +1200330static struct mv_ddr_topology_map board_topology_map_1g = {
331 DEBUG_LEVEL_ERROR,
Marek Behún09e16b82017-06-09 19:28:45 +0200332 0x1, /* active interfaces */
333 /* cs_mask, mirror, dqs_swap, ck_swap X PUPs */
334 { { { {0x1, 0, 0, 0},
335 {0x1, 0, 0, 0},
336 {0x1, 0, 0, 0},
337 {0x1, 0, 0, 0},
338 {0x1, 0, 0, 0} },
339 SPEED_BIN_DDR_1600K, /* speed_bin */
Chris Packham1a07d212018-05-10 13:28:29 +1200340 MV_DDR_DEV_WIDTH_16BIT, /* memory_width */
341 MV_DDR_DIE_CAP_4GBIT, /* mem_size */
Chris Packham4bf81db2018-12-03 14:26:49 +1300342 MV_DDR_FREQ_800, /* frequency */
Chris Packhamdd092bd2017-11-29 10:38:34 +1300343 0, 0, /* cas_wl cas_l */
Chris Packham3a09e132018-05-10 13:28:30 +1200344 MV_DDR_TEMP_NORMAL, /* temperature */
345 MV_DDR_TIM_2T} }, /* timing */
Chris Packham1a07d212018-05-10 13:28:29 +1200346 BUS_MASK_32BIT, /* Busses mask */
347 MV_DDR_CFG_DEFAULT, /* ddr configuration data source */
Moti Buskila498475e2021-02-19 17:11:19 +0100348 NOT_COMBINED, /* ddr twin-die combined */
Chris Packham1a07d212018-05-10 13:28:29 +1200349 { {0} }, /* raw spd data */
350 {0} /* timing parameters */
Marek Behún09e16b82017-06-09 19:28:45 +0200351};
352
Chris Packham1a07d212018-05-10 13:28:29 +1200353static struct mv_ddr_topology_map board_topology_map_2g = {
354 DEBUG_LEVEL_ERROR,
Marek Behún09e16b82017-06-09 19:28:45 +0200355 0x1, /* active interfaces */
356 /* cs_mask, mirror, dqs_swap, ck_swap X PUPs */
357 { { { {0x1, 0, 0, 0},
358 {0x1, 0, 0, 0},
359 {0x1, 0, 0, 0},
360 {0x1, 0, 0, 0},
361 {0x1, 0, 0, 0} },
362 SPEED_BIN_DDR_1600K, /* speed_bin */
Chris Packham1a07d212018-05-10 13:28:29 +1200363 MV_DDR_DEV_WIDTH_16BIT, /* memory_width */
364 MV_DDR_DIE_CAP_8GBIT, /* mem_size */
Chris Packham4bf81db2018-12-03 14:26:49 +1300365 MV_DDR_FREQ_800, /* frequency */
Chris Packhamdd092bd2017-11-29 10:38:34 +1300366 0, 0, /* cas_wl cas_l */
Chris Packham3a09e132018-05-10 13:28:30 +1200367 MV_DDR_TEMP_NORMAL, /* temperature */
368 MV_DDR_TIM_2T} }, /* timing */
Chris Packham1a07d212018-05-10 13:28:29 +1200369 BUS_MASK_32BIT, /* Busses mask */
370 MV_DDR_CFG_DEFAULT, /* ddr configuration data source */
Moti Buskila498475e2021-02-19 17:11:19 +0100371 NOT_COMBINED, /* ddr twin-die combined */
Chris Packham1a07d212018-05-10 13:28:29 +1200372 { {0} }, /* raw spd data */
373 {0} /* timing parameters */
Marek Behún09e16b82017-06-09 19:28:45 +0200374};
375
Chris Packham1a07d212018-05-10 13:28:29 +1200376struct mv_ddr_topology_map *mv_ddr_topology_map_get(void)
Marek Behún09e16b82017-06-09 19:28:45 +0200377{
Marek Behún77652c72019-05-02 16:53:33 +0200378 if (omnia_get_ram_size_gb() == 2)
Marek Behún09e16b82017-06-09 19:28:45 +0200379 return &board_topology_map_2g;
Marek Behún77652c72019-05-02 16:53:33 +0200380 else
381 return &board_topology_map_1g;
Marek Behún09e16b82017-06-09 19:28:45 +0200382}
383
Marek Behún09e16b82017-06-09 19:28:45 +0200384static int set_regdomain(void)
385{
386 struct omnia_eeprom oep;
387 char rd[3] = {' ', ' ', 0};
388
389 if (omnia_read_eeprom(&oep))
390 memcpy(rd, &oep.region, 2);
391 else
392 puts("EEPROM regdomain read failed.\n");
393
394 printf("Regdomain set to %s\n", rd);
Simon Glass6a38e412017-08-03 12:22:09 -0600395 return env_set("regdomain", rd);
Marek Behún09e16b82017-06-09 19:28:45 +0200396}
Marek Behún0f2e66a2019-05-02 16:53:37 +0200397
Marek Behún0f2e66a2019-05-02 16:53:37 +0200398static void handle_reset_button(void)
399{
Pali Rohár905c3bf2021-06-14 16:45:58 +0200400 const char * const vars[1] = { "bootcmd_rescue", };
Marek Behún0f2e66a2019-05-02 16:53:37 +0200401 int ret;
402 u8 reset_status;
403
Pali Rohár905c3bf2021-06-14 16:45:58 +0200404 /*
405 * Ensure that bootcmd_rescue has always stock value, so that running
406 * run bootcmd_rescue
407 * always works correctly.
408 */
409 env_set_default_vars(1, (char * const *)vars, 0);
410
Marek Behún0f2e66a2019-05-02 16:53:37 +0200411 ret = omnia_mcu_read(CMD_GET_RESET, &reset_status, 1);
412 if (ret) {
413 printf("omnia_mcu_read failed: %i, reset status unknown!\n",
414 ret);
415 return;
416 }
417
418 env_set_ulong("omnia_reset", reset_status);
419
420 if (reset_status) {
Pali Rohár905c3bf2021-06-14 16:45:58 +0200421 const char * const vars[2] = {
Marek Behún09f8de22021-05-28 10:00:49 +0200422 "bootcmd",
Marek Behún09f8de22021-05-28 10:00:49 +0200423 "distro_bootcmd",
424 };
425
426 /*
427 * Set the above envs to their default values, in case the user
428 * managed to break them.
429 */
Pali Rohár905c3bf2021-06-14 16:45:58 +0200430 env_set_default_vars(2, (char * const *)vars, 0);
Marek Behún09f8de22021-05-28 10:00:49 +0200431
432 /* Ensure bootcmd_rescue is used by distroboot */
433 env_set("boot_targets", "rescue");
434
Marek Behún0f2e66a2019-05-02 16:53:37 +0200435 printf("RESET button was pressed, overwriting bootcmd!\n");
Marek Behún09f8de22021-05-28 10:00:49 +0200436 } else {
437 /*
438 * In case the user somehow managed to save environment with
439 * boot_targets=rescue, reset boot_targets to default value.
440 * This could happen in subsequent commands if bootcmd_rescue
441 * failed.
442 */
443 if (!strcmp(env_get("boot_targets"), "rescue")) {
444 const char * const vars[1] = {
445 "boot_targets",
446 };
447
448 env_set_default_vars(1, (char * const *)vars, 0);
449 }
Marek Behún0f2e66a2019-05-02 16:53:37 +0200450 }
451}
Marek Behún09e16b82017-06-09 19:28:45 +0200452
453int board_early_init_f(void)
454{
Marek Behún09e16b82017-06-09 19:28:45 +0200455 /* Configure MPP */
456 writel(0x11111111, MVEBU_MPP_BASE + 0x00);
457 writel(0x11111111, MVEBU_MPP_BASE + 0x04);
458 writel(0x11244011, MVEBU_MPP_BASE + 0x08);
459 writel(0x22222111, MVEBU_MPP_BASE + 0x0c);
460 writel(0x22200002, MVEBU_MPP_BASE + 0x10);
461 writel(0x30042022, MVEBU_MPP_BASE + 0x14);
462 writel(0x55550555, MVEBU_MPP_BASE + 0x18);
463 writel(0x00005550, MVEBU_MPP_BASE + 0x1c);
464
465 /* Set GPP Out value */
466 writel(OMNIA_GPP_OUT_VAL_LOW, MVEBU_GPIO0_BASE + 0x00);
467 writel(OMNIA_GPP_OUT_VAL_MID, MVEBU_GPIO1_BASE + 0x00);
468
469 /* Set GPP Polarity */
470 writel(OMNIA_GPP_POL_LOW, MVEBU_GPIO0_BASE + 0x0c);
471 writel(OMNIA_GPP_POL_MID, MVEBU_GPIO1_BASE + 0x0c);
472
473 /* Set GPP Out Enable */
474 writel(OMNIA_GPP_OUT_ENA_LOW, MVEBU_GPIO0_BASE + 0x04);
475 writel(OMNIA_GPP_OUT_ENA_MID, MVEBU_GPIO1_BASE + 0x04);
476
Marek Behún09e16b82017-06-09 19:28:45 +0200477 return 0;
478}
479
Marek Behúnf3556162021-08-16 15:19:39 +0200480void spl_board_init(void)
481{
482 /*
483 * If booting from UART, disable MCU watchdog in SPL, since uploading
Pali Rohár7fcda0c2021-11-09 17:14:02 +0100484 * U-Boot proper can take too much time and trigger it. Instead enable
485 * A385 watchdog with very high timeout (10 minutes) to prevent hangup.
Marek Behúnf3556162021-08-16 15:19:39 +0200486 */
Pali Rohár7fcda0c2021-11-09 17:14:02 +0100487 if (get_boot_device() == BOOT_DEVICE_UART) {
488 enable_a385_watchdog(10);
Marek Behúnf3556162021-08-16 15:19:39 +0200489 disable_mcu_watchdog();
Pali Rohár7fcda0c2021-11-09 17:14:02 +0100490 }
Marek Behúnf3556162021-08-16 15:19:39 +0200491}
492
Marek Behún09e16b82017-06-09 19:28:45 +0200493int board_init(void)
494{
Marek Behún4dfc57e2019-05-02 16:53:31 +0200495 /* address of boot parameters */
Marek Behún09e16b82017-06-09 19:28:45 +0200496 gd->bd->bi_boot_params = mvebu_sdram_bar(0) + 0x100;
497
Marek Behún88dc0242021-08-16 15:19:40 +0200498 return 0;
499}
500
501int board_late_init(void)
502{
Marek Behúnf3556162021-08-16 15:19:39 +0200503 /*
504 * If not booting from UART, MCU watchdog was not disabled in SPL,
505 * disable it now.
506 */
507 if (get_boot_device() != BOOT_DEVICE_UART)
508 disable_mcu_watchdog();
Marek Behún09e16b82017-06-09 19:28:45 +0200509
Marek Behún09e16b82017-06-09 19:28:45 +0200510 set_regdomain();
Marek Behún0f2e66a2019-05-02 16:53:37 +0200511 handle_reset_button();
Marek Behúndb1e5c62019-05-24 14:57:53 +0200512 pci_init();
Marek Behún09e16b82017-06-09 19:28:45 +0200513
514 return 0;
515}
516
Marek Behún09e16b82017-06-09 19:28:45 +0200517static struct udevice *get_atsha204a_dev(void)
518{
Marek Behún4dfc57e2019-05-02 16:53:31 +0200519 static struct udevice *dev;
Marek Behún09e16b82017-06-09 19:28:45 +0200520
Marek Behún4dfc57e2019-05-02 16:53:31 +0200521 if (dev)
Marek Behún09e16b82017-06-09 19:28:45 +0200522 return dev;
523
524 if (uclass_get_device_by_name(UCLASS_MISC, "atsha204a@64", &dev)) {
525 puts("Cannot find ATSHA204A on I2C bus!\n");
526 dev = NULL;
527 }
528
529 return dev;
530}
Marek Behún09e16b82017-06-09 19:28:45 +0200531
Marek Behúnab9447f2021-10-09 19:33:44 +0200532int show_board_info(void)
Marek Behún09e16b82017-06-09 19:28:45 +0200533{
534 u32 version_num, serial_num;
535 int err = 1;
536
Marek Behún09e16b82017-06-09 19:28:45 +0200537 struct udevice *dev = get_atsha204a_dev();
538
539 if (dev) {
540 err = atsha204a_wakeup(dev);
541 if (err)
542 goto out;
543
544 err = atsha204a_read(dev, ATSHA204A_ZONE_OTP, false,
545 OMNIA_ATSHA204_OTP_VERSION,
Marek Behún4dfc57e2019-05-02 16:53:31 +0200546 (u8 *)&version_num);
Marek Behún09e16b82017-06-09 19:28:45 +0200547 if (err)
548 goto out;
549
550 err = atsha204a_read(dev, ATSHA204A_ZONE_OTP, false,
551 OMNIA_ATSHA204_OTP_SERIAL,
Marek Behún4dfc57e2019-05-02 16:53:31 +0200552 (u8 *)&serial_num);
Marek Behún09e16b82017-06-09 19:28:45 +0200553 if (err)
554 goto out;
555
556 atsha204a_sleep(dev);
557 }
558
559out:
Marek Behúnab9447f2021-10-09 19:33:44 +0200560 printf("Model: Turris Omnia\n");
Marek Behúnc4ba72a2019-05-02 16:53:34 +0200561 printf(" RAM size: %i MiB\n", omnia_get_ram_size_gb() * 1024);
Marek Behún09e16b82017-06-09 19:28:45 +0200562 if (err)
Marek Behúnc4ba72a2019-05-02 16:53:34 +0200563 printf(" Serial Number: unknown\n");
Marek Behún09e16b82017-06-09 19:28:45 +0200564 else
Marek Behúnc4ba72a2019-05-02 16:53:34 +0200565 printf(" Serial Number: %08X%08X\n", be32_to_cpu(version_num),
566 be32_to_cpu(serial_num));
Marek Behún09e16b82017-06-09 19:28:45 +0200567
568 return 0;
569}
570
571static void increment_mac(u8 *mac)
572{
573 int i;
574
575 for (i = 5; i >= 3; i--) {
576 mac[i] += 1;
577 if (mac[i])
578 break;
579 }
580}
581
Marek Behúnb8856142021-10-09 19:33:43 +0200582static void set_mac_if_invalid(int i, u8 *mac)
583{
584 u8 oldmac[6];
585
586 if (is_valid_ethaddr(mac) &&
587 !eth_env_get_enetaddr_by_index("eth", i, oldmac))
588 eth_env_set_enetaddr_by_index("eth", i, mac);
589}
590
Marek Behún09e16b82017-06-09 19:28:45 +0200591int misc_init_r(void)
592{
Marek Behún09e16b82017-06-09 19:28:45 +0200593 int err;
594 struct udevice *dev = get_atsha204a_dev();
595 u8 mac0[4], mac1[4], mac[6];
596
597 if (!dev)
598 goto out;
599
600 err = atsha204a_wakeup(dev);
601 if (err)
602 goto out;
603
604 err = atsha204a_read(dev, ATSHA204A_ZONE_OTP, false,
605 OMNIA_ATSHA204_OTP_MAC0, mac0);
606 if (err)
607 goto out;
608
609 err = atsha204a_read(dev, ATSHA204A_ZONE_OTP, false,
610 OMNIA_ATSHA204_OTP_MAC1, mac1);
611 if (err)
612 goto out;
613
614 atsha204a_sleep(dev);
615
616 mac[0] = mac0[1];
617 mac[1] = mac0[2];
618 mac[2] = mac0[3];
619 mac[3] = mac1[1];
620 mac[4] = mac1[2];
621 mac[5] = mac1[3];
622
Marek Behúnb8856142021-10-09 19:33:43 +0200623 set_mac_if_invalid(1, mac);
Marek Behún09e16b82017-06-09 19:28:45 +0200624 increment_mac(mac);
Marek Behúnb8856142021-10-09 19:33:43 +0200625 set_mac_if_invalid(2, mac);
Marek Behún09e16b82017-06-09 19:28:45 +0200626 increment_mac(mac);
Marek Behúnb8856142021-10-09 19:33:43 +0200627 set_mac_if_invalid(0, mac);
Marek Behún09e16b82017-06-09 19:28:45 +0200628
629out:
Marek Behún09e16b82017-06-09 19:28:45 +0200630 return 0;
631}
632
Marek Behún91ef59c2021-07-15 19:21:02 +0200633#if defined(CONFIG_OF_BOARD_SETUP)
634/*
635 * I plan to generalize this function and move it to common/fdt_support.c.
636 * This will require some more work on multiple boards, though, so for now leave
637 * it here.
638 */
639static bool fixup_mtd_partitions(void *blob, int offset, struct mtd_info *mtd)
640{
641 struct mtd_info *slave;
642 int parts;
643
644 parts = fdt_subnode_offset(blob, offset, "partitions");
645 if (parts < 0)
646 return false;
647
648 if (fdt_del_node(blob, parts) < 0)
649 return false;
650
651 parts = fdt_add_subnode(blob, offset, "partitions");
652 if (parts < 0)
653 return false;
654
655 if (fdt_setprop_u32(blob, parts, "#address-cells", 1) < 0)
656 return false;
657
658 if (fdt_setprop_u32(blob, parts, "#size-cells", 1) < 0)
659 return false;
660
661 if (fdt_setprop_string(blob, parts, "compatible",
662 "fixed-partitions") < 0)
663 return false;
664
665 mtd_probe_devices();
666
Pali Rohárd8210ef2021-10-21 17:55:48 +0200667 list_for_each_entry_reverse(slave, &mtd->partitions, node) {
Marek Behún91ef59c2021-07-15 19:21:02 +0200668 char name[32];
669 int part;
670
671 snprintf(name, sizeof(name), "partition@%llx", slave->offset);
672 part = fdt_add_subnode(blob, parts, name);
673 if (part < 0)
674 return false;
675
676 if (fdt_setprop_u32(blob, part, "reg", slave->offset) < 0)
677 return false;
678
679 if (fdt_appendprop_u32(blob, part, "reg", slave->size) < 0)
680 return false;
681
682 if (fdt_setprop_string(blob, part, "label", slave->name) < 0)
683 return false;
684
685 if (!(slave->flags & MTD_WRITEABLE))
686 if (fdt_setprop_empty(blob, part, "read-only") < 0)
687 return false;
688
689 if (slave->flags & MTD_POWERUP_LOCK)
690 if (fdt_setprop_empty(blob, part, "lock") < 0)
691 return false;
692 }
693
694 return true;
695}
696
697int ft_board_setup(void *blob, struct bd_info *bd)
698{
699 struct mtd_info *mtd;
700 int node;
701
702 mtd = get_mtd_device_nm(OMNIA_SPI_NOR_PATH);
703 if (IS_ERR_OR_NULL(mtd))
704 goto fail;
705
706 node = fdt_path_offset(blob, OMNIA_SPI_NOR_PATH);
707 if (node < 0)
708 goto fail;
709
710 if (!fixup_mtd_partitions(blob, node, mtd))
711 goto fail;
712
Marek Behún36feac92021-09-25 02:49:18 +0200713 put_mtd_device(mtd);
Marek Behún91ef59c2021-07-15 19:21:02 +0200714 return 0;
715
716fail:
717 printf("Failed fixing SPI NOR partitions!\n");
Marek Behún36feac92021-09-25 02:49:18 +0200718 if (!IS_ERR_OR_NULL(mtd))
719 put_mtd_device(mtd);
Marek Behún91ef59c2021-07-15 19:21:02 +0200720 return 0;
721}
722#endif