blob: dab5711cf0cf12b2b0e49fbdf71e3bc9894e9e0d [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>
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ár30e398d2022-04-29 13:53:25 +020044#define A385_SYS_RSTOUT_MASK MVEBU_REGISTER(0x18260)
45#define A385_SYS_RSTOUT_MASK_WD BIT(10)
Pali Rohár7fcda0c2021-11-09 17:14:02 +010046
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,
Pali Rohár4798ba92022-07-29 13:29:06 +020065
66 /* available if STS_FEATURES_SUPPORTED bit set in status word */
67 CMD_GET_FEATURES = 0x10,
Marek Behúnba53b6b2019-05-02 16:53:30 +020068};
69
70enum status_word_bits {
Pali Rohár4798ba92022-07-29 13:29:06 +020071 STS_MCU_TYPE_MASK = GENMASK(1, 0),
72 STS_MCU_TYPE_STM32 = 0,
73 STS_MCU_TYPE_GD32 = 1,
74 STS_MCU_TYPE_MKL = 2,
75 STS_MCU_TYPE_UNKN = 3,
76 STS_FEATURES_SUPPORTED = BIT(2),
Marek Behúnba53b6b2019-05-02 16:53:30 +020077 CARD_DET_STSBIT = 0x0010,
78 MSATA_IND_STSBIT = 0x0020,
79};
Marek Behún09e16b82017-06-09 19:28:45 +020080
Pali Rohár4798ba92022-07-29 13:29:06 +020081/* CMD_GET_FEATURES */
82enum features_e {
83 FEAT_PERIPH_MCU = BIT(0),
84};
85
Marek Behún09e16b82017-06-09 19:28:45 +020086/*
87 * Those values and defines are taken from the Marvell U-Boot version
88 * "u-boot-2013.01-2014_T3.0"
89 */
90#define OMNIA_GPP_OUT_ENA_LOW \
91 (~(BIT(1) | BIT(4) | BIT(6) | BIT(7) | BIT(8) | BIT(9) | \
92 BIT(10) | BIT(11) | BIT(19) | BIT(22) | BIT(23) | BIT(25) | \
93 BIT(26) | BIT(27) | BIT(29) | BIT(30) | BIT(31)))
94#define OMNIA_GPP_OUT_ENA_MID \
95 (~(BIT(0) | BIT(1) | BIT(2) | BIT(3) | BIT(4) | BIT(15) | \
96 BIT(16) | BIT(17) | BIT(18)))
97
98#define OMNIA_GPP_OUT_VAL_LOW 0x0
99#define OMNIA_GPP_OUT_VAL_MID 0x0
100#define OMNIA_GPP_POL_LOW 0x0
101#define OMNIA_GPP_POL_MID 0x0
102
Pali Rohár3c4dd9812022-03-02 12:47:54 +0100103static struct serdes_map board_serdes_map[] = {
Marek Behún09e16b82017-06-09 19:28:45 +0200104 {PEX0, SERDES_SPEED_5_GBPS, PEX_ROOT_COMPLEX_X1, 0, 0},
105 {USB3_HOST0, SERDES_SPEED_5_GBPS, SERDES_DEFAULT_MODE, 0, 0},
106 {PEX1, SERDES_SPEED_5_GBPS, PEX_ROOT_COMPLEX_X1, 0, 0},
107 {USB3_HOST1, SERDES_SPEED_5_GBPS, SERDES_DEFAULT_MODE, 0, 0},
108 {PEX2, SERDES_SPEED_5_GBPS, PEX_ROOT_COMPLEX_X1, 0, 0},
109 {SGMII2, SERDES_SPEED_1_25_GBPS, SERDES_DEFAULT_MODE, 0, 0}
110};
111
Marek Behúnba53b6b2019-05-02 16:53:30 +0200112static struct udevice *omnia_get_i2c_chip(const char *name, uint addr,
113 uint offset_len)
Marek Behún09e16b82017-06-09 19:28:45 +0200114{
115 struct udevice *bus, *dev;
Marek Behúnba53b6b2019-05-02 16:53:30 +0200116 int ret;
Marek Behún09e16b82017-06-09 19:28:45 +0200117
Marek Behúnba53b6b2019-05-02 16:53:30 +0200118 ret = uclass_get_device_by_name(UCLASS_I2C, OMNIA_I2C_BUS_NAME, &bus);
119 if (ret) {
120 printf("Cannot get I2C bus %s: uclass_get_device_by_name failed: %i\n",
121 OMNIA_I2C_BUS_NAME, ret);
122 return NULL;
Marek Behún09e16b82017-06-09 19:28:45 +0200123 }
124
Marek Behúnba53b6b2019-05-02 16:53:30 +0200125 ret = i2c_get_chip(bus, addr, offset_len, &dev);
Marek Behún09e16b82017-06-09 19:28:45 +0200126 if (ret) {
Marek Behúnba53b6b2019-05-02 16:53:30 +0200127 printf("Cannot get %s I2C chip: i2c_get_chip failed: %i\n",
128 name, ret);
129 return NULL;
Marek Behún09e16b82017-06-09 19:28:45 +0200130 }
131
Marek Behúnba53b6b2019-05-02 16:53:30 +0200132 return dev;
133}
Marek Behúnd0b374d2017-08-04 15:28:25 +0200134
Marek Behúnba53b6b2019-05-02 16:53:30 +0200135static int omnia_mcu_read(u8 cmd, void *buf, int len)
136{
137 struct udevice *chip;
138
139 chip = omnia_get_i2c_chip("MCU", OMNIA_I2C_MCU_CHIP_ADDR,
140 OMNIA_I2C_MCU_CHIP_LEN);
141 if (!chip)
142 return -ENODEV;
143
144 return dm_i2c_read(chip, cmd, buf, len);
145}
146
Marek Behúnba53b6b2019-05-02 16:53:30 +0200147static int omnia_mcu_write(u8 cmd, const void *buf, int len)
148{
149 struct udevice *chip;
150
151 chip = omnia_get_i2c_chip("MCU", OMNIA_I2C_MCU_CHIP_ADDR,
152 OMNIA_I2C_MCU_CHIP_LEN);
153 if (!chip)
154 return -ENODEV;
155
156 return dm_i2c_write(chip, cmd, buf, len);
157}
158
Pali Rohár7fcda0c2021-11-09 17:14:02 +0100159static void enable_a385_watchdog(unsigned int timeout_minutes)
160{
161 struct sar_freq_modes sar_freq;
162 u32 watchdog_freq;
163
164 printf("Enabling A385 watchdog with %u minutes timeout...\n",
165 timeout_minutes);
166
167 /*
168 * Use NBCLK clock (a.k.a. L2 clock) as watchdog input clock with
169 * its maximal ratio 7 instead of default fixed 25 MHz clock.
170 * It allows to set watchdog duration up to the 22 minutes.
171 */
172 clrsetbits_32(A385_WDT_GLOBAL_CTRL,
173 A385_WDT_GLOBAL_25MHZ | A385_WDT_GLOBAL_RATIO_MASK,
174 7 << A385_WDT_GLOBAL_RATIO_SHIFT);
175
176 /*
177 * Calculate watchdog clock frequency. It is defined by formula:
178 * freq = NBCLK / 2 / (2 ^ ratio)
179 * We set ratio to the maximal possible value 7.
180 */
181 get_sar_freq(&sar_freq);
182 watchdog_freq = sar_freq.nb_clk * 1000000 / 2 / (1 << 7);
183
184 /* Set watchdog duration */
185 writel(timeout_minutes * 60 * watchdog_freq, A385_WDT_DURATION);
186
187 /* Clear the watchdog expiration bit */
188 clrbits_32(A385_WDT_GLOBAL_STATUS, A385_WDT_GLOBAL_EXPIRED);
189
190 /* Enable watchdog timer */
191 setbits_32(A385_WDT_GLOBAL_CTRL, A385_WDT_GLOBAL_ENABLE);
192
193 /* Enable reset on watchdog */
194 setbits_32(A385_WD_RSTOUT_UNMASK, A385_WD_RSTOUT_UNMASK_GLOBAL);
195
196 /* Unmask reset for watchdog */
Pali Rohár30e398d2022-04-29 13:53:25 +0200197 clrbits_32(A385_SYS_RSTOUT_MASK, A385_SYS_RSTOUT_MASK_WD);
Pali Rohár7fcda0c2021-11-09 17:14:02 +0100198}
199
Marek Behúnba53b6b2019-05-02 16:53:30 +0200200static bool disable_mcu_watchdog(void)
201{
202 int ret;
203
204 puts("Disabling MCU watchdog... ");
205
206 ret = omnia_mcu_write(CMD_WATCHDOG_STATE, "\x00", 1);
207 if (ret) {
208 printf("omnia_mcu_write failed: %i\n", ret);
Marek Behún09e16b82017-06-09 19:28:45 +0200209 return false;
210 }
211
Marek Behúnba53b6b2019-05-02 16:53:30 +0200212 puts("disabled\n");
213
214 return true;
215}
Marek Behúnba53b6b2019-05-02 16:53:30 +0200216
Pali Rohárf8f305b2022-03-02 12:47:55 +0100217static bool omnia_detect_sata(const char *msata_slot)
Marek Behúnba53b6b2019-05-02 16:53:30 +0200218{
219 int ret;
220 u16 stsword;
221
222 puts("MiniPCIe/mSATA card detection... ");
223
Pali Rohárf8f305b2022-03-02 12:47:55 +0100224 if (msata_slot) {
225 if (strcmp(msata_slot, "pcie") == 0) {
226 puts("forced to MiniPCIe via env\n");
227 return false;
228 } else if (strcmp(msata_slot, "sata") == 0) {
229 puts("forced to mSATA via env\n");
230 return true;
231 } else if (strcmp(msata_slot, "auto") != 0) {
232 printf("unsupported env value '%s', fallback to... ", msata_slot);
233 }
234 }
235
Marek Behúnba53b6b2019-05-02 16:53:30 +0200236 ret = omnia_mcu_read(CMD_GET_STATUS_WORD, &stsword, sizeof(stsword));
237 if (ret) {
238 printf("omnia_mcu_read failed: %i, defaulting to MiniPCIe card\n",
239 ret);
Marek Behún09e16b82017-06-09 19:28:45 +0200240 return false;
241 }
242
Marek Behúnba53b6b2019-05-02 16:53:30 +0200243 if (!(stsword & CARD_DET_STSBIT)) {
244 puts("none\n");
Marek Behún09e16b82017-06-09 19:28:45 +0200245 return false;
246 }
Marek Behúnba53b6b2019-05-02 16:53:30 +0200247
248 if (stsword & MSATA_IND_STSBIT)
249 puts("mSATA\n");
250 else
251 puts("MiniPCIe\n");
252
253 return stsword & MSATA_IND_STSBIT ? true : false;
Marek Behún09e16b82017-06-09 19:28:45 +0200254}
255
Pali Rohár93a89c52022-03-02 12:47:58 +0100256static bool omnia_detect_wwan_usb3(const char *wwan_slot)
257{
258 puts("WWAN slot configuration... ");
259
260 if (wwan_slot && strcmp(wwan_slot, "usb3") == 0) {
261 puts("USB3.0\n");
262 return true;
263 }
264
265 if (wwan_slot && strcmp(wwan_slot, "pcie") != 0)
266 printf("unsupported env value '%s', fallback to... ", wwan_slot);
267
268 puts("PCIe+USB2.0\n");
269 return false;
270}
271
Pali Rohárc13401b2022-03-02 12:47:52 +0100272void *env_sf_get_env_addr(void)
273{
274 /* SPI Flash is mapped to address 0xD4000000 only in SPL */
275#ifdef CONFIG_SPL_BUILD
276 return (void *)0xD4000000 + CONFIG_ENV_OFFSET;
277#else
278 return NULL;
279#endif
280}
281
Marek Behún09e16b82017-06-09 19:28:45 +0200282int hws_board_topology_load(struct serdes_map **serdes_map_array, u8 *count)
283{
Pali Rohárf8f305b2022-03-02 12:47:55 +0100284#ifdef CONFIG_SPL_ENV_SUPPORT
285 /* Do not use env_load() as malloc() pool is too small at this stage */
286 bool has_env = (env_init() == 0);
287#endif
288 const char *env_value = NULL;
289
290#ifdef CONFIG_SPL_ENV_SUPPORT
291 /* beware that env_get() returns static allocated memory */
292 env_value = has_env ? env_get("omnia_msata_slot") : NULL;
293#endif
294
295 if (omnia_detect_sata(env_value)) {
Pali Rohár3c4dd9812022-03-02 12:47:54 +0100296 /* Change SerDes for first mPCIe port (mSATA) from PCIe to SATA */
297 board_serdes_map[0].serdes_type = SATA0;
298 board_serdes_map[0].serdes_speed = SERDES_SPEED_6_GBPS;
299 board_serdes_map[0].serdes_mode = SERDES_DEFAULT_MODE;
Marek Behún09e16b82017-06-09 19:28:45 +0200300 }
301
Pali Rohár93a89c52022-03-02 12:47:58 +0100302#ifdef CONFIG_SPL_ENV_SUPPORT
303 /* beware that env_get() returns static allocated memory */
304 env_value = has_env ? env_get("omnia_wwan_slot") : NULL;
305#endif
306
307 if (omnia_detect_wwan_usb3(env_value)) {
308 /* Disable SerDes for USB 3.0 pins on the front USB-A port */
309 board_serdes_map[1].serdes_type = DEFAULT_SERDES;
310 /* Change SerDes for third mPCIe port (WWAN) from PCIe to USB 3.0 */
311 board_serdes_map[4].serdes_type = USB3_HOST0;
312 board_serdes_map[4].serdes_speed = SERDES_SPEED_5_GBPS;
313 board_serdes_map[4].serdes_mode = SERDES_DEFAULT_MODE;
314 }
315
Pali Rohár3c4dd9812022-03-02 12:47:54 +0100316 *serdes_map_array = board_serdes_map;
317 *count = ARRAY_SIZE(board_serdes_map);
318
Marek Behún09e16b82017-06-09 19:28:45 +0200319 return 0;
320}
321
322struct omnia_eeprom {
323 u32 magic;
324 u32 ramsize;
325 char region[4];
326 u32 crc;
327};
328
329static bool omnia_read_eeprom(struct omnia_eeprom *oep)
330{
Marek Behúnba53b6b2019-05-02 16:53:30 +0200331 struct udevice *chip;
332 u32 crc;
333 int ret;
Marek Behún09e16b82017-06-09 19:28:45 +0200334
Marek Behúnba53b6b2019-05-02 16:53:30 +0200335 chip = omnia_get_i2c_chip("EEPROM", OMNIA_I2C_EEPROM_CHIP_ADDR,
336 OMNIA_I2C_EEPROM_CHIP_LEN);
337
338 if (!chip)
Marek Behún09e16b82017-06-09 19:28:45 +0200339 return false;
Marek Behún09e16b82017-06-09 19:28:45 +0200340
Marek Behúnba53b6b2019-05-02 16:53:30 +0200341 ret = dm_i2c_read(chip, 0, (void *)oep, sizeof(*oep));
Marek Behún09e16b82017-06-09 19:28:45 +0200342 if (ret) {
Marek Behúnba53b6b2019-05-02 16:53:30 +0200343 printf("dm_i2c_read failed: %i, cannot read EEPROM\n", ret);
Marek Behún09e16b82017-06-09 19:28:45 +0200344 return false;
345 }
346
Marek Behúnba53b6b2019-05-02 16:53:30 +0200347 if (oep->magic != OMNIA_I2C_EEPROM_MAGIC) {
348 printf("bad EEPROM magic number (%08x, should be %08x)\n",
349 oep->magic, OMNIA_I2C_EEPROM_MAGIC);
350 return false;
Marek Behún09e16b82017-06-09 19:28:45 +0200351 }
352
Marek Behúnba53b6b2019-05-02 16:53:30 +0200353 crc = crc32(0, (void *)oep, sizeof(*oep) - 4);
354 if (crc != oep->crc) {
355 printf("bad EEPROM CRC (stored %08x, computed %08x)\n",
356 oep->crc, crc);
Marek Behún09e16b82017-06-09 19:28:45 +0200357 return false;
358 }
359
360 return true;
361}
362
Marek Behún77652c72019-05-02 16:53:33 +0200363static int omnia_get_ram_size_gb(void)
364{
365 static int ram_size;
366 struct omnia_eeprom oep;
367
368 if (!ram_size) {
369 /* Get the board config from EEPROM */
370 if (omnia_read_eeprom(&oep)) {
371 debug("Memory config in EEPROM: 0x%02x\n", oep.ramsize);
372
373 if (oep.ramsize == 0x2)
374 ram_size = 2;
375 else
376 ram_size = 1;
377 } else {
378 /* Hardcoded fallback */
379 puts("Memory config from EEPROM read failed!\n");
380 puts("Falling back to default 1 GiB!\n");
381 ram_size = 1;
382 }
383 }
384
385 return ram_size;
386}
387
Pali Rohár4798ba92022-07-29 13:29:06 +0200388static const char * const omnia_get_mcu_type(void)
389{
390 static const char * const mcu_types[] = {
391 [STS_MCU_TYPE_STM32] = "STM32",
392 [STS_MCU_TYPE_GD32] = "GD32",
393 [STS_MCU_TYPE_MKL] = "MKL",
394 [STS_MCU_TYPE_UNKN] = "unknown",
395 };
396 static const char * const mcu_types_with_perip_resets[] = {
397 [STS_MCU_TYPE_STM32] = "STM32 (with peripheral resets)",
398 [STS_MCU_TYPE_GD32] = "GD32 (with peripheral resets)",
399 [STS_MCU_TYPE_MKL] = "MKL (with peripheral resets)",
400 [STS_MCU_TYPE_UNKN] = "unknown (with peripheral resets)",
401 };
402 u16 stsword, features;
403 int ret;
404
405 ret = omnia_mcu_read(CMD_GET_STATUS_WORD, &stsword, sizeof(stsword));
406 if (ret)
407 return "unknown";
408
409 if (stsword & STS_FEATURES_SUPPORTED) {
410 ret = omnia_mcu_read(CMD_GET_FEATURES, &features, sizeof(features));
411 if (ret == 0 && (features & FEAT_PERIPH_MCU))
412 return mcu_types_with_perip_resets[stsword & STS_MCU_TYPE_MASK];
413 }
414
415 return mcu_types[stsword & STS_MCU_TYPE_MASK];
416}
417
Marek Behún09e16b82017-06-09 19:28:45 +0200418/*
419 * Define the DDR layout / topology here in the board file. This will
420 * be used by the DDR3 init code in the SPL U-Boot version to configure
421 * the DDR3 controller.
422 */
Chris Packham1a07d212018-05-10 13:28:29 +1200423static struct mv_ddr_topology_map board_topology_map_1g = {
424 DEBUG_LEVEL_ERROR,
Marek Behún09e16b82017-06-09 19:28:45 +0200425 0x1, /* active interfaces */
426 /* cs_mask, mirror, dqs_swap, ck_swap X PUPs */
427 { { { {0x1, 0, 0, 0},
428 {0x1, 0, 0, 0},
429 {0x1, 0, 0, 0},
430 {0x1, 0, 0, 0},
431 {0x1, 0, 0, 0} },
432 SPEED_BIN_DDR_1600K, /* speed_bin */
Chris Packham1a07d212018-05-10 13:28:29 +1200433 MV_DDR_DEV_WIDTH_16BIT, /* memory_width */
434 MV_DDR_DIE_CAP_4GBIT, /* mem_size */
Chris Packham4bf81db2018-12-03 14:26:49 +1300435 MV_DDR_FREQ_800, /* frequency */
Chris Packhamdd092bd2017-11-29 10:38:34 +1300436 0, 0, /* cas_wl cas_l */
Chris Packham3a09e132018-05-10 13:28:30 +1200437 MV_DDR_TEMP_NORMAL, /* temperature */
438 MV_DDR_TIM_2T} }, /* timing */
Chris Packham1a07d212018-05-10 13:28:29 +1200439 BUS_MASK_32BIT, /* Busses mask */
440 MV_DDR_CFG_DEFAULT, /* ddr configuration data source */
Moti Buskila498475e2021-02-19 17:11:19 +0100441 NOT_COMBINED, /* ddr twin-die combined */
Chris Packham1a07d212018-05-10 13:28:29 +1200442 { {0} }, /* raw spd data */
443 {0} /* timing parameters */
Marek Behún09e16b82017-06-09 19:28:45 +0200444};
445
Chris Packham1a07d212018-05-10 13:28:29 +1200446static struct mv_ddr_topology_map board_topology_map_2g = {
447 DEBUG_LEVEL_ERROR,
Marek Behún09e16b82017-06-09 19:28:45 +0200448 0x1, /* active interfaces */
449 /* cs_mask, mirror, dqs_swap, ck_swap X PUPs */
450 { { { {0x1, 0, 0, 0},
451 {0x1, 0, 0, 0},
452 {0x1, 0, 0, 0},
453 {0x1, 0, 0, 0},
454 {0x1, 0, 0, 0} },
455 SPEED_BIN_DDR_1600K, /* speed_bin */
Chris Packham1a07d212018-05-10 13:28:29 +1200456 MV_DDR_DEV_WIDTH_16BIT, /* memory_width */
457 MV_DDR_DIE_CAP_8GBIT, /* mem_size */
Chris Packham4bf81db2018-12-03 14:26:49 +1300458 MV_DDR_FREQ_800, /* frequency */
Chris Packhamdd092bd2017-11-29 10:38:34 +1300459 0, 0, /* cas_wl cas_l */
Chris Packham3a09e132018-05-10 13:28:30 +1200460 MV_DDR_TEMP_NORMAL, /* temperature */
461 MV_DDR_TIM_2T} }, /* timing */
Chris Packham1a07d212018-05-10 13:28:29 +1200462 BUS_MASK_32BIT, /* Busses mask */
463 MV_DDR_CFG_DEFAULT, /* ddr configuration data source */
Moti Buskila498475e2021-02-19 17:11:19 +0100464 NOT_COMBINED, /* ddr twin-die combined */
Chris Packham1a07d212018-05-10 13:28:29 +1200465 { {0} }, /* raw spd data */
466 {0} /* timing parameters */
Marek Behún09e16b82017-06-09 19:28:45 +0200467};
468
Chris Packham1a07d212018-05-10 13:28:29 +1200469struct mv_ddr_topology_map *mv_ddr_topology_map_get(void)
Marek Behún09e16b82017-06-09 19:28:45 +0200470{
Marek Behún77652c72019-05-02 16:53:33 +0200471 if (omnia_get_ram_size_gb() == 2)
Marek Behún09e16b82017-06-09 19:28:45 +0200472 return &board_topology_map_2g;
Marek Behún77652c72019-05-02 16:53:33 +0200473 else
474 return &board_topology_map_1g;
Marek Behún09e16b82017-06-09 19:28:45 +0200475}
476
Marek Behún09e16b82017-06-09 19:28:45 +0200477static int set_regdomain(void)
478{
479 struct omnia_eeprom oep;
480 char rd[3] = {' ', ' ', 0};
481
482 if (omnia_read_eeprom(&oep))
483 memcpy(rd, &oep.region, 2);
484 else
485 puts("EEPROM regdomain read failed.\n");
486
487 printf("Regdomain set to %s\n", rd);
Simon Glass6a38e412017-08-03 12:22:09 -0600488 return env_set("regdomain", rd);
Marek Behún09e16b82017-06-09 19:28:45 +0200489}
Marek Behún0f2e66a2019-05-02 16:53:37 +0200490
Marek Behún0f2e66a2019-05-02 16:53:37 +0200491static void handle_reset_button(void)
492{
Pali Rohár905c3bf2021-06-14 16:45:58 +0200493 const char * const vars[1] = { "bootcmd_rescue", };
Marek Behún0f2e66a2019-05-02 16:53:37 +0200494 int ret;
495 u8 reset_status;
496
Pali Rohár905c3bf2021-06-14 16:45:58 +0200497 /*
498 * Ensure that bootcmd_rescue has always stock value, so that running
499 * run bootcmd_rescue
500 * always works correctly.
501 */
502 env_set_default_vars(1, (char * const *)vars, 0);
503
Marek Behún0f2e66a2019-05-02 16:53:37 +0200504 ret = omnia_mcu_read(CMD_GET_RESET, &reset_status, 1);
505 if (ret) {
506 printf("omnia_mcu_read failed: %i, reset status unknown!\n",
507 ret);
508 return;
509 }
510
511 env_set_ulong("omnia_reset", reset_status);
512
513 if (reset_status) {
Pali Rohár905c3bf2021-06-14 16:45:58 +0200514 const char * const vars[2] = {
Marek Behún09f8de22021-05-28 10:00:49 +0200515 "bootcmd",
Marek Behún09f8de22021-05-28 10:00:49 +0200516 "distro_bootcmd",
517 };
518
519 /*
520 * Set the above envs to their default values, in case the user
521 * managed to break them.
522 */
Pali Rohár905c3bf2021-06-14 16:45:58 +0200523 env_set_default_vars(2, (char * const *)vars, 0);
Marek Behún09f8de22021-05-28 10:00:49 +0200524
525 /* Ensure bootcmd_rescue is used by distroboot */
526 env_set("boot_targets", "rescue");
527
Pali Rohár4f9e6fb2022-04-06 11:39:32 +0200528 printf("RESET button was pressed, overwriting boot_targets!\n");
Marek Behún09f8de22021-05-28 10:00:49 +0200529 } else {
530 /*
531 * In case the user somehow managed to save environment with
532 * boot_targets=rescue, reset boot_targets to default value.
533 * This could happen in subsequent commands if bootcmd_rescue
534 * failed.
535 */
536 if (!strcmp(env_get("boot_targets"), "rescue")) {
537 const char * const vars[1] = {
538 "boot_targets",
539 };
540
541 env_set_default_vars(1, (char * const *)vars, 0);
542 }
Marek Behún0f2e66a2019-05-02 16:53:37 +0200543 }
544}
Marek Behún09e16b82017-06-09 19:28:45 +0200545
546int board_early_init_f(void)
547{
Marek Behún09e16b82017-06-09 19:28:45 +0200548 /* Configure MPP */
549 writel(0x11111111, MVEBU_MPP_BASE + 0x00);
550 writel(0x11111111, MVEBU_MPP_BASE + 0x04);
551 writel(0x11244011, MVEBU_MPP_BASE + 0x08);
552 writel(0x22222111, MVEBU_MPP_BASE + 0x0c);
553 writel(0x22200002, MVEBU_MPP_BASE + 0x10);
554 writel(0x30042022, MVEBU_MPP_BASE + 0x14);
555 writel(0x55550555, MVEBU_MPP_BASE + 0x18);
556 writel(0x00005550, MVEBU_MPP_BASE + 0x1c);
557
558 /* Set GPP Out value */
559 writel(OMNIA_GPP_OUT_VAL_LOW, MVEBU_GPIO0_BASE + 0x00);
560 writel(OMNIA_GPP_OUT_VAL_MID, MVEBU_GPIO1_BASE + 0x00);
561
562 /* Set GPP Polarity */
563 writel(OMNIA_GPP_POL_LOW, MVEBU_GPIO0_BASE + 0x0c);
564 writel(OMNIA_GPP_POL_MID, MVEBU_GPIO1_BASE + 0x0c);
565
566 /* Set GPP Out Enable */
567 writel(OMNIA_GPP_OUT_ENA_LOW, MVEBU_GPIO0_BASE + 0x04);
568 writel(OMNIA_GPP_OUT_ENA_MID, MVEBU_GPIO1_BASE + 0x04);
569
Marek Behún09e16b82017-06-09 19:28:45 +0200570 return 0;
571}
572
Marek Behúnf3556162021-08-16 15:19:39 +0200573void spl_board_init(void)
574{
575 /*
576 * If booting from UART, disable MCU watchdog in SPL, since uploading
Pali Rohár7fcda0c2021-11-09 17:14:02 +0100577 * U-Boot proper can take too much time and trigger it. Instead enable
578 * A385 watchdog with very high timeout (10 minutes) to prevent hangup.
Marek Behúnf3556162021-08-16 15:19:39 +0200579 */
Pali Rohár7fcda0c2021-11-09 17:14:02 +0100580 if (get_boot_device() == BOOT_DEVICE_UART) {
581 enable_a385_watchdog(10);
Marek Behúnf3556162021-08-16 15:19:39 +0200582 disable_mcu_watchdog();
Pali Rohár7fcda0c2021-11-09 17:14:02 +0100583 }
Marek Behúnf3556162021-08-16 15:19:39 +0200584}
585
Pali Rohárcbda3e22022-01-10 11:47:18 +0100586#if IS_ENABLED(CONFIG_OF_BOARD_FIXUP) || IS_ENABLED(CONFIG_OF_BOARD_SETUP)
587
Pali Rohár7cd41732022-03-02 12:47:56 +0100588static void disable_sata_node(void *blob)
Pali Rohárcbda3e22022-01-10 11:47:18 +0100589{
Pali Rohárcbda3e22022-01-10 11:47:18 +0100590 int node;
591
Pali Rohár7cd41732022-03-02 12:47:56 +0100592 fdt_for_each_node_by_compatible(node, blob, -1, "marvell,armada-380-ahci") {
593 if (!fdtdec_get_is_enabled(blob, node))
594 continue;
595
596 if (fdt_status_disabled(blob, node) < 0)
597 printf("Cannot disable SATA DT node!\n");
598 else
599 debug("Disabled SATA DT node\n");
600
Pali Roháre9105262022-03-02 12:47:57 +0100601 return;
Pali Rohár7cd41732022-03-02 12:47:56 +0100602 }
Pali Roháre9105262022-03-02 12:47:57 +0100603
604 printf("Cannot find SATA DT node!\n");
Pali Rohár7cd41732022-03-02 12:47:56 +0100605}
606
607static void disable_pcie_node(void *blob, int port)
608{
609 int node;
610
611 fdt_for_each_node_by_compatible(node, blob, -1, "marvell,armada-370-pcie") {
612 int port_node;
613
614 if (!fdtdec_get_is_enabled(blob, node))
615 continue;
616
617 fdt_for_each_subnode (port_node, blob, node) {
618 if (!fdtdec_get_is_enabled(blob, port_node))
619 continue;
620
621 if (fdtdec_get_int(blob, port_node, "marvell,pcie-port", -1) != port)
622 continue;
623
624 if (fdt_status_disabled(blob, port_node) < 0)
625 printf("Cannot disable PCIe port %d DT node!\n", port);
626 else
627 debug("Disabled PCIe port %d DT node\n", port);
628
629 return;
630 }
631 }
Pali Roháre9105262022-03-02 12:47:57 +0100632
633 printf("Cannot find PCIe port %d DT node!\n", port);
Pali Rohár7cd41732022-03-02 12:47:56 +0100634}
635
636static void fixup_msata_port_nodes(void *blob)
637{
638 bool mode_sata;
639
Pali Rohárcbda3e22022-01-10 11:47:18 +0100640 /*
641 * Determine if SerDes 0 is configured to SATA mode.
642 * We do this instead of calling omnia_detect_sata() to avoid another
643 * call to the MCU. By this time the common PHYs are initialized (it is
644 * done in SPL), so we can read this common PHY register.
645 */
646 mode_sata = (readl(MVEBU_REGISTER(0x183fc)) & GENMASK(3, 0)) == 2;
647
648 /*
649 * We're either adding status = "disabled" property, or changing
650 * status = "okay" to status = "disabled". In both cases we'll need more
651 * space. Increase the size a little.
652 */
653 if (fdt_increase_size(blob, 32) < 0) {
654 printf("Cannot increase FDT size!\n");
655 return;
656 }
657
Pali Rohárcbda3e22022-01-10 11:47:18 +0100658 if (!mode_sata) {
Pali Rohár7cd41732022-03-02 12:47:56 +0100659 /* If mSATA card is not present, disable SATA DT node */
660 disable_sata_node(blob);
661 } else {
662 /* Otherwise disable PCIe port 0 DT node (MiniPCIe / mSATA port) */
663 disable_pcie_node(blob, 0);
Pali Rohárcbda3e22022-01-10 11:47:18 +0100664 }
Pali Rohár93a89c52022-03-02 12:47:58 +0100665}
666
667static void fixup_wwan_port_nodes(void *blob)
668{
669 bool mode_usb3;
670
671 /* Determine if SerDes 4 is configured to USB3 mode */
672 mode_usb3 = ((readl(MVEBU_REGISTER(0x183fc)) & GENMASK(19, 16)) >> 16) == 4;
673
674 /* If SerDes 4 is not configured to USB3 mode then nothing is needed to fixup */
675 if (!mode_usb3)
676 return;
677
678 /*
679 * We're either adding status = "disabled" property, or changing
680 * status = "okay" to status = "disabled". In both cases we'll need more
681 * space. Increase the size a little.
682 */
683 if (fdt_increase_size(blob, 32) < 0) {
684 printf("Cannot increase FDT size!\n");
685 return;
686 }
687
688 /* Disable PCIe port 2 DT node (WWAN) */
689 disable_pcie_node(blob, 2);
Pali Rohárcbda3e22022-01-10 11:47:18 +0100690}
691
692#endif
693
694#if IS_ENABLED(CONFIG_OF_BOARD_FIXUP)
695int board_fix_fdt(void *blob)
696{
Pali Rohár7cd41732022-03-02 12:47:56 +0100697 fixup_msata_port_nodes(blob);
Pali Rohár93a89c52022-03-02 12:47:58 +0100698 fixup_wwan_port_nodes(blob);
Pali Rohárcbda3e22022-01-10 11:47:18 +0100699
700 return 0;
701}
702#endif
703
Marek Behún09e16b82017-06-09 19:28:45 +0200704int board_init(void)
705{
Marek Behún4dfc57e2019-05-02 16:53:31 +0200706 /* address of boot parameters */
Marek Behún09e16b82017-06-09 19:28:45 +0200707 gd->bd->bi_boot_params = mvebu_sdram_bar(0) + 0x100;
708
Marek Behún88dc0242021-08-16 15:19:40 +0200709 return 0;
710}
711
712int board_late_init(void)
713{
Marek Behúnf3556162021-08-16 15:19:39 +0200714 /*
715 * If not booting from UART, MCU watchdog was not disabled in SPL,
716 * disable it now.
717 */
718 if (get_boot_device() != BOOT_DEVICE_UART)
719 disable_mcu_watchdog();
Marek Behún09e16b82017-06-09 19:28:45 +0200720
Marek Behún09e16b82017-06-09 19:28:45 +0200721 set_regdomain();
Marek Behún0f2e66a2019-05-02 16:53:37 +0200722 handle_reset_button();
Marek Behúndb1e5c62019-05-24 14:57:53 +0200723 pci_init();
Marek Behún09e16b82017-06-09 19:28:45 +0200724
725 return 0;
726}
727
Marek Behúnab9447f2021-10-09 19:33:44 +0200728int show_board_info(void)
Marek Behún09e16b82017-06-09 19:28:45 +0200729{
730 u32 version_num, serial_num;
Pali Rohár0387f7f2022-04-08 16:30:12 +0200731 int err;
Marek Behún09e16b82017-06-09 19:28:45 +0200732
Pali Rohár0387f7f2022-04-08 16:30:12 +0200733 err = turris_atsha_otp_get_serial_number(&version_num, &serial_num);
Marek Behúnab9447f2021-10-09 19:33:44 +0200734 printf("Model: Turris Omnia\n");
Pali Rohár4798ba92022-07-29 13:29:06 +0200735 printf(" MCU type: %s\n", omnia_get_mcu_type());
Marek Behúnc4ba72a2019-05-02 16:53:34 +0200736 printf(" RAM size: %i MiB\n", omnia_get_ram_size_gb() * 1024);
Marek Behún09e16b82017-06-09 19:28:45 +0200737 if (err)
Marek Behúnc4ba72a2019-05-02 16:53:34 +0200738 printf(" Serial Number: unknown\n");
Marek Behún09e16b82017-06-09 19:28:45 +0200739 else
Marek Behúnc4ba72a2019-05-02 16:53:34 +0200740 printf(" Serial Number: %08X%08X\n", be32_to_cpu(version_num),
741 be32_to_cpu(serial_num));
Marek Behún09e16b82017-06-09 19:28:45 +0200742
743 return 0;
744}
745
Marek Behún09e16b82017-06-09 19:28:45 +0200746int misc_init_r(void)
747{
Pali Rohár60f37e82022-04-08 16:30:14 +0200748 turris_atsha_otp_init_mac_addresses(1);
Marek Behún09e16b82017-06-09 19:28:45 +0200749 return 0;
750}
751
Marek Behún91ef59c2021-07-15 19:21:02 +0200752#if defined(CONFIG_OF_BOARD_SETUP)
753/*
754 * I plan to generalize this function and move it to common/fdt_support.c.
755 * This will require some more work on multiple boards, though, so for now leave
756 * it here.
757 */
758static bool fixup_mtd_partitions(void *blob, int offset, struct mtd_info *mtd)
759{
760 struct mtd_info *slave;
761 int parts;
762
763 parts = fdt_subnode_offset(blob, offset, "partitions");
764 if (parts < 0)
765 return false;
766
767 if (fdt_del_node(blob, parts) < 0)
768 return false;
769
770 parts = fdt_add_subnode(blob, offset, "partitions");
771 if (parts < 0)
772 return false;
773
774 if (fdt_setprop_u32(blob, parts, "#address-cells", 1) < 0)
775 return false;
776
777 if (fdt_setprop_u32(blob, parts, "#size-cells", 1) < 0)
778 return false;
779
780 if (fdt_setprop_string(blob, parts, "compatible",
781 "fixed-partitions") < 0)
782 return false;
783
784 mtd_probe_devices();
785
Pali Rohárd8210ef2021-10-21 17:55:48 +0200786 list_for_each_entry_reverse(slave, &mtd->partitions, node) {
Marek Behún91ef59c2021-07-15 19:21:02 +0200787 char name[32];
788 int part;
789
790 snprintf(name, sizeof(name), "partition@%llx", slave->offset);
791 part = fdt_add_subnode(blob, parts, name);
792 if (part < 0)
793 return false;
794
795 if (fdt_setprop_u32(blob, part, "reg", slave->offset) < 0)
796 return false;
797
798 if (fdt_appendprop_u32(blob, part, "reg", slave->size) < 0)
799 return false;
800
801 if (fdt_setprop_string(blob, part, "label", slave->name) < 0)
802 return false;
803
804 if (!(slave->flags & MTD_WRITEABLE))
805 if (fdt_setprop_empty(blob, part, "read-only") < 0)
806 return false;
807
808 if (slave->flags & MTD_POWERUP_LOCK)
809 if (fdt_setprop_empty(blob, part, "lock") < 0)
810 return false;
811 }
812
813 return true;
814}
815
Pali Rohárcbda3e22022-01-10 11:47:18 +0100816static void fixup_spi_nor_partitions(void *blob)
Marek Behún91ef59c2021-07-15 19:21:02 +0200817{
818 struct mtd_info *mtd;
819 int node;
820
821 mtd = get_mtd_device_nm(OMNIA_SPI_NOR_PATH);
822 if (IS_ERR_OR_NULL(mtd))
823 goto fail;
824
825 node = fdt_path_offset(blob, OMNIA_SPI_NOR_PATH);
826 if (node < 0)
827 goto fail;
828
829 if (!fixup_mtd_partitions(blob, node, mtd))
830 goto fail;
831
Marek Behún36feac92021-09-25 02:49:18 +0200832 put_mtd_device(mtd);
Pali Rohárcbda3e22022-01-10 11:47:18 +0100833 return;
Marek Behún91ef59c2021-07-15 19:21:02 +0200834
835fail:
836 printf("Failed fixing SPI NOR partitions!\n");
Marek Behún36feac92021-09-25 02:49:18 +0200837 if (!IS_ERR_OR_NULL(mtd))
838 put_mtd_device(mtd);
Pali Rohárcbda3e22022-01-10 11:47:18 +0100839}
840
841int ft_board_setup(void *blob, struct bd_info *bd)
842{
843 fixup_spi_nor_partitions(blob);
Pali Rohár7cd41732022-03-02 12:47:56 +0100844 fixup_msata_port_nodes(blob);
Pali Rohár93a89c52022-03-02 12:47:58 +0100845 fixup_wwan_port_nodes(blob);
Pali Rohárcbda3e22022-01-10 11:47:18 +0100846
Marek Behún91ef59c2021-07-15 19:21:02 +0200847 return 0;
848}
849#endif