blob: 33cec6587e19521576655e40f86f09312ecd1584 [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
Pali Rohárcbda3e22022-01-10 11:47:18 +0100493#if IS_ENABLED(CONFIG_OF_BOARD_FIXUP) || IS_ENABLED(CONFIG_OF_BOARD_SETUP)
494
495static void fixup_serdes_0_nodes(void *blob)
496{
497 bool mode_sata;
498 int node;
499
500 /*
501 * Determine if SerDes 0 is configured to SATA mode.
502 * We do this instead of calling omnia_detect_sata() to avoid another
503 * call to the MCU. By this time the common PHYs are initialized (it is
504 * done in SPL), so we can read this common PHY register.
505 */
506 mode_sata = (readl(MVEBU_REGISTER(0x183fc)) & GENMASK(3, 0)) == 2;
507
508 /*
509 * We're either adding status = "disabled" property, or changing
510 * status = "okay" to status = "disabled". In both cases we'll need more
511 * space. Increase the size a little.
512 */
513 if (fdt_increase_size(blob, 32) < 0) {
514 printf("Cannot increase FDT size!\n");
515 return;
516 }
517
518 /* If mSATA card is not present, disable SATA DT node */
519 if (!mode_sata) {
520 fdt_for_each_node_by_compatible(node, blob, -1,
521 "marvell,armada-380-ahci") {
522 if (!fdtdec_get_is_enabled(blob, node))
523 continue;
524
525 if (fdt_status_disabled(blob, node) < 0)
526 printf("Cannot disable SATA DT node!\n");
527 else
528 debug("Disabled SATA DT node\n");
529
530 break;
531 }
532
533 return;
534 }
535
536 /* Otherwise disable PCIe port 0 DT node (MiniPCIe / mSATA port) */
537 fdt_for_each_node_by_compatible(node, blob, -1,
538 "marvell,armada-370-pcie") {
539 int port;
540
541 if (!fdtdec_get_is_enabled(blob, node))
542 continue;
543
544 fdt_for_each_subnode (port, blob, node) {
545 if (!fdtdec_get_is_enabled(blob, port))
546 continue;
547
548 if (fdtdec_get_int(blob, port, "marvell,pcie-port",
549 -1) != 0)
550 continue;
551
552 if (fdt_status_disabled(blob, port) < 0)
553 printf("Cannot disable PCIe port 0 DT node!\n");
554 else
555 debug("Disabled PCIe port 0 DT node\n");
556
557 return;
558 }
559 }
560}
561
562#endif
563
564#if IS_ENABLED(CONFIG_OF_BOARD_FIXUP)
565int board_fix_fdt(void *blob)
566{
567 fixup_serdes_0_nodes(blob);
568
569 return 0;
570}
571#endif
572
Marek Behún09e16b82017-06-09 19:28:45 +0200573int board_init(void)
574{
Marek Behún4dfc57e2019-05-02 16:53:31 +0200575 /* address of boot parameters */
Marek Behún09e16b82017-06-09 19:28:45 +0200576 gd->bd->bi_boot_params = mvebu_sdram_bar(0) + 0x100;
577
Marek Behún88dc0242021-08-16 15:19:40 +0200578 return 0;
579}
580
581int board_late_init(void)
582{
Marek Behúnf3556162021-08-16 15:19:39 +0200583 /*
584 * If not booting from UART, MCU watchdog was not disabled in SPL,
585 * disable it now.
586 */
587 if (get_boot_device() != BOOT_DEVICE_UART)
588 disable_mcu_watchdog();
Marek Behún09e16b82017-06-09 19:28:45 +0200589
Marek Behún09e16b82017-06-09 19:28:45 +0200590 set_regdomain();
Marek Behún0f2e66a2019-05-02 16:53:37 +0200591 handle_reset_button();
Marek Behúndb1e5c62019-05-24 14:57:53 +0200592 pci_init();
Marek Behún09e16b82017-06-09 19:28:45 +0200593
594 return 0;
595}
596
Marek Behún09e16b82017-06-09 19:28:45 +0200597static struct udevice *get_atsha204a_dev(void)
598{
Marek Behún4dfc57e2019-05-02 16:53:31 +0200599 static struct udevice *dev;
Marek Behún09e16b82017-06-09 19:28:45 +0200600
Marek Behún4dfc57e2019-05-02 16:53:31 +0200601 if (dev)
Marek Behún09e16b82017-06-09 19:28:45 +0200602 return dev;
603
604 if (uclass_get_device_by_name(UCLASS_MISC, "atsha204a@64", &dev)) {
605 puts("Cannot find ATSHA204A on I2C bus!\n");
606 dev = NULL;
607 }
608
609 return dev;
610}
Marek Behún09e16b82017-06-09 19:28:45 +0200611
Marek Behúnab9447f2021-10-09 19:33:44 +0200612int show_board_info(void)
Marek Behún09e16b82017-06-09 19:28:45 +0200613{
614 u32 version_num, serial_num;
615 int err = 1;
616
Marek Behún09e16b82017-06-09 19:28:45 +0200617 struct udevice *dev = get_atsha204a_dev();
618
619 if (dev) {
620 err = atsha204a_wakeup(dev);
621 if (err)
622 goto out;
623
624 err = atsha204a_read(dev, ATSHA204A_ZONE_OTP, false,
625 OMNIA_ATSHA204_OTP_VERSION,
Marek Behún4dfc57e2019-05-02 16:53:31 +0200626 (u8 *)&version_num);
Marek Behún09e16b82017-06-09 19:28:45 +0200627 if (err)
628 goto out;
629
630 err = atsha204a_read(dev, ATSHA204A_ZONE_OTP, false,
631 OMNIA_ATSHA204_OTP_SERIAL,
Marek Behún4dfc57e2019-05-02 16:53:31 +0200632 (u8 *)&serial_num);
Marek Behún09e16b82017-06-09 19:28:45 +0200633 if (err)
634 goto out;
635
636 atsha204a_sleep(dev);
637 }
638
639out:
Marek Behúnab9447f2021-10-09 19:33:44 +0200640 printf("Model: Turris Omnia\n");
Marek Behúnc4ba72a2019-05-02 16:53:34 +0200641 printf(" RAM size: %i MiB\n", omnia_get_ram_size_gb() * 1024);
Marek Behún09e16b82017-06-09 19:28:45 +0200642 if (err)
Marek Behúnc4ba72a2019-05-02 16:53:34 +0200643 printf(" Serial Number: unknown\n");
Marek Behún09e16b82017-06-09 19:28:45 +0200644 else
Marek Behúnc4ba72a2019-05-02 16:53:34 +0200645 printf(" Serial Number: %08X%08X\n", be32_to_cpu(version_num),
646 be32_to_cpu(serial_num));
Marek Behún09e16b82017-06-09 19:28:45 +0200647
648 return 0;
649}
650
651static void increment_mac(u8 *mac)
652{
653 int i;
654
655 for (i = 5; i >= 3; i--) {
656 mac[i] += 1;
657 if (mac[i])
658 break;
659 }
660}
661
Marek Behúnb8856142021-10-09 19:33:43 +0200662static void set_mac_if_invalid(int i, u8 *mac)
663{
664 u8 oldmac[6];
665
666 if (is_valid_ethaddr(mac) &&
667 !eth_env_get_enetaddr_by_index("eth", i, oldmac))
668 eth_env_set_enetaddr_by_index("eth", i, mac);
669}
670
Marek Behún09e16b82017-06-09 19:28:45 +0200671int misc_init_r(void)
672{
Marek Behún09e16b82017-06-09 19:28:45 +0200673 int err;
674 struct udevice *dev = get_atsha204a_dev();
675 u8 mac0[4], mac1[4], mac[6];
676
677 if (!dev)
678 goto out;
679
680 err = atsha204a_wakeup(dev);
681 if (err)
682 goto out;
683
684 err = atsha204a_read(dev, ATSHA204A_ZONE_OTP, false,
685 OMNIA_ATSHA204_OTP_MAC0, mac0);
686 if (err)
687 goto out;
688
689 err = atsha204a_read(dev, ATSHA204A_ZONE_OTP, false,
690 OMNIA_ATSHA204_OTP_MAC1, mac1);
691 if (err)
692 goto out;
693
694 atsha204a_sleep(dev);
695
696 mac[0] = mac0[1];
697 mac[1] = mac0[2];
698 mac[2] = mac0[3];
699 mac[3] = mac1[1];
700 mac[4] = mac1[2];
701 mac[5] = mac1[3];
702
Marek Behúnb8856142021-10-09 19:33:43 +0200703 set_mac_if_invalid(1, mac);
Marek Behún09e16b82017-06-09 19:28:45 +0200704 increment_mac(mac);
Marek Behúnb8856142021-10-09 19:33:43 +0200705 set_mac_if_invalid(2, mac);
Marek Behún09e16b82017-06-09 19:28:45 +0200706 increment_mac(mac);
Marek Behúnb8856142021-10-09 19:33:43 +0200707 set_mac_if_invalid(0, mac);
Marek Behún09e16b82017-06-09 19:28:45 +0200708
709out:
Marek Behún09e16b82017-06-09 19:28:45 +0200710 return 0;
711}
712
Marek Behún91ef59c2021-07-15 19:21:02 +0200713#if defined(CONFIG_OF_BOARD_SETUP)
714/*
715 * I plan to generalize this function and move it to common/fdt_support.c.
716 * This will require some more work on multiple boards, though, so for now leave
717 * it here.
718 */
719static bool fixup_mtd_partitions(void *blob, int offset, struct mtd_info *mtd)
720{
721 struct mtd_info *slave;
722 int parts;
723
724 parts = fdt_subnode_offset(blob, offset, "partitions");
725 if (parts < 0)
726 return false;
727
728 if (fdt_del_node(blob, parts) < 0)
729 return false;
730
731 parts = fdt_add_subnode(blob, offset, "partitions");
732 if (parts < 0)
733 return false;
734
735 if (fdt_setprop_u32(blob, parts, "#address-cells", 1) < 0)
736 return false;
737
738 if (fdt_setprop_u32(blob, parts, "#size-cells", 1) < 0)
739 return false;
740
741 if (fdt_setprop_string(blob, parts, "compatible",
742 "fixed-partitions") < 0)
743 return false;
744
745 mtd_probe_devices();
746
Pali Rohárd8210ef2021-10-21 17:55:48 +0200747 list_for_each_entry_reverse(slave, &mtd->partitions, node) {
Marek Behún91ef59c2021-07-15 19:21:02 +0200748 char name[32];
749 int part;
750
751 snprintf(name, sizeof(name), "partition@%llx", slave->offset);
752 part = fdt_add_subnode(blob, parts, name);
753 if (part < 0)
754 return false;
755
756 if (fdt_setprop_u32(blob, part, "reg", slave->offset) < 0)
757 return false;
758
759 if (fdt_appendprop_u32(blob, part, "reg", slave->size) < 0)
760 return false;
761
762 if (fdt_setprop_string(blob, part, "label", slave->name) < 0)
763 return false;
764
765 if (!(slave->flags & MTD_WRITEABLE))
766 if (fdt_setprop_empty(blob, part, "read-only") < 0)
767 return false;
768
769 if (slave->flags & MTD_POWERUP_LOCK)
770 if (fdt_setprop_empty(blob, part, "lock") < 0)
771 return false;
772 }
773
774 return true;
775}
776
Pali Rohárcbda3e22022-01-10 11:47:18 +0100777static void fixup_spi_nor_partitions(void *blob)
Marek Behún91ef59c2021-07-15 19:21:02 +0200778{
779 struct mtd_info *mtd;
780 int node;
781
782 mtd = get_mtd_device_nm(OMNIA_SPI_NOR_PATH);
783 if (IS_ERR_OR_NULL(mtd))
784 goto fail;
785
786 node = fdt_path_offset(blob, OMNIA_SPI_NOR_PATH);
787 if (node < 0)
788 goto fail;
789
790 if (!fixup_mtd_partitions(blob, node, mtd))
791 goto fail;
792
Marek Behún36feac92021-09-25 02:49:18 +0200793 put_mtd_device(mtd);
Pali Rohárcbda3e22022-01-10 11:47:18 +0100794 return;
Marek Behún91ef59c2021-07-15 19:21:02 +0200795
796fail:
797 printf("Failed fixing SPI NOR partitions!\n");
Marek Behún36feac92021-09-25 02:49:18 +0200798 if (!IS_ERR_OR_NULL(mtd))
799 put_mtd_device(mtd);
Pali Rohárcbda3e22022-01-10 11:47:18 +0100800}
801
802int ft_board_setup(void *blob, struct bd_info *bd)
803{
804 fixup_spi_nor_partitions(blob);
805 fixup_serdes_0_nodes(blob);
806
Marek Behún91ef59c2021-07-15 19:21:02 +0200807 return 0;
808}
809#endif