blob: 1d3cefe703b8f49c4fdaec5d1dbcc4fdd5138f93 [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>
Simon Glass274e0b02020-05-10 11:39:56 -060016#include <net.h>
Marek Behún09e16b82017-06-09 19:28:45 +020017#include <netdev.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060018#include <asm/global_data.h>
Marek Behún09e16b82017-06-09 19:28:45 +020019#include <asm/io.h>
20#include <asm/arch/cpu.h>
21#include <asm/arch/soc.h>
22#include <dm/uclass.h>
23#include <fdt_support.h>
24#include <time.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060025#include <linux/bitops.h>
Simon Glass48b6c6b2019-11-14 12:57:16 -070026#include <u-boot/crc.h>
Marek Behún09e16b82017-06-09 19:28:45 +020027# include <atsha204a-i2c.h>
Marek Behún09e16b82017-06-09 19:28:45 +020028
Chris Packham1a07d212018-05-10 13:28:29 +120029#include "../drivers/ddr/marvell/a38x/ddr3_init.h"
Marek Behún09e16b82017-06-09 19:28:45 +020030#include <../serdes/a38x/high_speed_env_spec.h>
31
32DECLARE_GLOBAL_DATA_PTR;
33
Marek Behúnba53b6b2019-05-02 16:53:30 +020034#define OMNIA_I2C_BUS_NAME "i2c@11000->i2cmux@70->i2c@0"
35
36#define OMNIA_I2C_MCU_CHIP_ADDR 0x2a
37#define OMNIA_I2C_MCU_CHIP_LEN 1
38
39#define OMNIA_I2C_EEPROM_CHIP_ADDR 0x54
40#define OMNIA_I2C_EEPROM_CHIP_LEN 2
Marek Behún09e16b82017-06-09 19:28:45 +020041#define OMNIA_I2C_EEPROM_MAGIC 0x0341a034
42
Marek Behúnba53b6b2019-05-02 16:53:30 +020043enum mcu_commands {
44 CMD_GET_STATUS_WORD = 0x01,
45 CMD_GET_RESET = 0x09,
46 CMD_WATCHDOG_STATE = 0x0b,
47};
48
49enum status_word_bits {
50 CARD_DET_STSBIT = 0x0010,
51 MSATA_IND_STSBIT = 0x0020,
52};
Marek Behún09e16b82017-06-09 19:28:45 +020053
54#define OMNIA_ATSHA204_OTP_VERSION 0
55#define OMNIA_ATSHA204_OTP_SERIAL 1
56#define OMNIA_ATSHA204_OTP_MAC0 3
57#define OMNIA_ATSHA204_OTP_MAC1 4
58
Marek Behún09e16b82017-06-09 19:28:45 +020059/*
60 * Those values and defines are taken from the Marvell U-Boot version
61 * "u-boot-2013.01-2014_T3.0"
62 */
63#define OMNIA_GPP_OUT_ENA_LOW \
64 (~(BIT(1) | BIT(4) | BIT(6) | BIT(7) | BIT(8) | BIT(9) | \
65 BIT(10) | BIT(11) | BIT(19) | BIT(22) | BIT(23) | BIT(25) | \
66 BIT(26) | BIT(27) | BIT(29) | BIT(30) | BIT(31)))
67#define OMNIA_GPP_OUT_ENA_MID \
68 (~(BIT(0) | BIT(1) | BIT(2) | BIT(3) | BIT(4) | BIT(15) | \
69 BIT(16) | BIT(17) | BIT(18)))
70
71#define OMNIA_GPP_OUT_VAL_LOW 0x0
72#define OMNIA_GPP_OUT_VAL_MID 0x0
73#define OMNIA_GPP_POL_LOW 0x0
74#define OMNIA_GPP_POL_MID 0x0
75
76static struct serdes_map board_serdes_map_pex[] = {
77 {PEX0, SERDES_SPEED_5_GBPS, PEX_ROOT_COMPLEX_X1, 0, 0},
78 {USB3_HOST0, SERDES_SPEED_5_GBPS, SERDES_DEFAULT_MODE, 0, 0},
79 {PEX1, SERDES_SPEED_5_GBPS, PEX_ROOT_COMPLEX_X1, 0, 0},
80 {USB3_HOST1, SERDES_SPEED_5_GBPS, SERDES_DEFAULT_MODE, 0, 0},
81 {PEX2, SERDES_SPEED_5_GBPS, PEX_ROOT_COMPLEX_X1, 0, 0},
82 {SGMII2, SERDES_SPEED_1_25_GBPS, SERDES_DEFAULT_MODE, 0, 0}
83};
84
85static struct serdes_map board_serdes_map_sata[] = {
86 {SATA0, SERDES_SPEED_6_GBPS, SERDES_DEFAULT_MODE, 0, 0},
87 {USB3_HOST0, SERDES_SPEED_5_GBPS, SERDES_DEFAULT_MODE, 0, 0},
88 {PEX1, SERDES_SPEED_5_GBPS, PEX_ROOT_COMPLEX_X1, 0, 0},
89 {USB3_HOST1, SERDES_SPEED_5_GBPS, SERDES_DEFAULT_MODE, 0, 0},
90 {PEX2, SERDES_SPEED_5_GBPS, PEX_ROOT_COMPLEX_X1, 0, 0},
91 {SGMII2, SERDES_SPEED_1_25_GBPS, SERDES_DEFAULT_MODE, 0, 0}
92};
93
Marek Behúnba53b6b2019-05-02 16:53:30 +020094static struct udevice *omnia_get_i2c_chip(const char *name, uint addr,
95 uint offset_len)
Marek Behún09e16b82017-06-09 19:28:45 +020096{
97 struct udevice *bus, *dev;
Marek Behúnba53b6b2019-05-02 16:53:30 +020098 int ret;
Marek Behún09e16b82017-06-09 19:28:45 +020099
Marek Behúnba53b6b2019-05-02 16:53:30 +0200100 ret = uclass_get_device_by_name(UCLASS_I2C, OMNIA_I2C_BUS_NAME, &bus);
101 if (ret) {
102 printf("Cannot get I2C bus %s: uclass_get_device_by_name failed: %i\n",
103 OMNIA_I2C_BUS_NAME, ret);
104 return NULL;
Marek Behún09e16b82017-06-09 19:28:45 +0200105 }
106
Marek Behúnba53b6b2019-05-02 16:53:30 +0200107 ret = i2c_get_chip(bus, addr, offset_len, &dev);
Marek Behún09e16b82017-06-09 19:28:45 +0200108 if (ret) {
Marek Behúnba53b6b2019-05-02 16:53:30 +0200109 printf("Cannot get %s I2C chip: i2c_get_chip failed: %i\n",
110 name, ret);
111 return NULL;
Marek Behún09e16b82017-06-09 19:28:45 +0200112 }
113
Marek Behúnba53b6b2019-05-02 16:53:30 +0200114 return dev;
115}
Marek Behúnd0b374d2017-08-04 15:28:25 +0200116
Marek Behúnba53b6b2019-05-02 16:53:30 +0200117static int omnia_mcu_read(u8 cmd, void *buf, int len)
118{
119 struct udevice *chip;
120
121 chip = omnia_get_i2c_chip("MCU", OMNIA_I2C_MCU_CHIP_ADDR,
122 OMNIA_I2C_MCU_CHIP_LEN);
123 if (!chip)
124 return -ENODEV;
125
126 return dm_i2c_read(chip, cmd, buf, len);
127}
128
129#ifndef CONFIG_SPL_BUILD
130static int omnia_mcu_write(u8 cmd, const 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_write(chip, cmd, buf, len);
140}
141
142static bool disable_mcu_watchdog(void)
143{
144 int ret;
145
146 puts("Disabling MCU watchdog... ");
147
148 ret = omnia_mcu_write(CMD_WATCHDOG_STATE, "\x00", 1);
149 if (ret) {
150 printf("omnia_mcu_write failed: %i\n", ret);
Marek Behún09e16b82017-06-09 19:28:45 +0200151 return false;
152 }
153
Marek Behúnba53b6b2019-05-02 16:53:30 +0200154 puts("disabled\n");
155
156 return true;
157}
158#endif
159
160static bool omnia_detect_sata(void)
161{
162 int ret;
163 u16 stsword;
164
165 puts("MiniPCIe/mSATA card detection... ");
166
167 ret = omnia_mcu_read(CMD_GET_STATUS_WORD, &stsword, sizeof(stsword));
168 if (ret) {
169 printf("omnia_mcu_read failed: %i, defaulting to MiniPCIe card\n",
170 ret);
Marek Behún09e16b82017-06-09 19:28:45 +0200171 return false;
172 }
173
Marek Behúnba53b6b2019-05-02 16:53:30 +0200174 if (!(stsword & CARD_DET_STSBIT)) {
175 puts("none\n");
Marek Behún09e16b82017-06-09 19:28:45 +0200176 return false;
177 }
Marek Behúnba53b6b2019-05-02 16:53:30 +0200178
179 if (stsword & MSATA_IND_STSBIT)
180 puts("mSATA\n");
181 else
182 puts("MiniPCIe\n");
183
184 return stsword & MSATA_IND_STSBIT ? true : false;
Marek Behún09e16b82017-06-09 19:28:45 +0200185}
186
187int hws_board_topology_load(struct serdes_map **serdes_map_array, u8 *count)
188{
189 if (omnia_detect_sata()) {
190 *serdes_map_array = board_serdes_map_sata;
191 *count = ARRAY_SIZE(board_serdes_map_sata);
192 } else {
193 *serdes_map_array = board_serdes_map_pex;
194 *count = ARRAY_SIZE(board_serdes_map_pex);
195 }
196
197 return 0;
198}
199
200struct omnia_eeprom {
201 u32 magic;
202 u32 ramsize;
203 char region[4];
204 u32 crc;
205};
206
207static bool omnia_read_eeprom(struct omnia_eeprom *oep)
208{
Marek Behúnba53b6b2019-05-02 16:53:30 +0200209 struct udevice *chip;
210 u32 crc;
211 int ret;
Marek Behún09e16b82017-06-09 19:28:45 +0200212
Marek Behúnba53b6b2019-05-02 16:53:30 +0200213 chip = omnia_get_i2c_chip("EEPROM", OMNIA_I2C_EEPROM_CHIP_ADDR,
214 OMNIA_I2C_EEPROM_CHIP_LEN);
215
216 if (!chip)
Marek Behún09e16b82017-06-09 19:28:45 +0200217 return false;
Marek Behún09e16b82017-06-09 19:28:45 +0200218
Marek Behúnba53b6b2019-05-02 16:53:30 +0200219 ret = dm_i2c_read(chip, 0, (void *)oep, sizeof(*oep));
Marek Behún09e16b82017-06-09 19:28:45 +0200220 if (ret) {
Marek Behúnba53b6b2019-05-02 16:53:30 +0200221 printf("dm_i2c_read failed: %i, cannot read EEPROM\n", ret);
Marek Behún09e16b82017-06-09 19:28:45 +0200222 return false;
223 }
224
Marek Behúnba53b6b2019-05-02 16:53:30 +0200225 if (oep->magic != OMNIA_I2C_EEPROM_MAGIC) {
226 printf("bad EEPROM magic number (%08x, should be %08x)\n",
227 oep->magic, OMNIA_I2C_EEPROM_MAGIC);
228 return false;
Marek Behún09e16b82017-06-09 19:28:45 +0200229 }
230
Marek Behúnba53b6b2019-05-02 16:53:30 +0200231 crc = crc32(0, (void *)oep, sizeof(*oep) - 4);
232 if (crc != oep->crc) {
233 printf("bad EEPROM CRC (stored %08x, computed %08x)\n",
234 oep->crc, crc);
Marek Behún09e16b82017-06-09 19:28:45 +0200235 return false;
236 }
237
238 return true;
239}
240
Marek Behún77652c72019-05-02 16:53:33 +0200241static int omnia_get_ram_size_gb(void)
242{
243 static int ram_size;
244 struct omnia_eeprom oep;
245
246 if (!ram_size) {
247 /* Get the board config from EEPROM */
248 if (omnia_read_eeprom(&oep)) {
249 debug("Memory config in EEPROM: 0x%02x\n", oep.ramsize);
250
251 if (oep.ramsize == 0x2)
252 ram_size = 2;
253 else
254 ram_size = 1;
255 } else {
256 /* Hardcoded fallback */
257 puts("Memory config from EEPROM read failed!\n");
258 puts("Falling back to default 1 GiB!\n");
259 ram_size = 1;
260 }
261 }
262
263 return ram_size;
264}
265
Marek Behún09e16b82017-06-09 19:28:45 +0200266/*
267 * Define the DDR layout / topology here in the board file. This will
268 * be used by the DDR3 init code in the SPL U-Boot version to configure
269 * the DDR3 controller.
270 */
Chris Packham1a07d212018-05-10 13:28:29 +1200271static struct mv_ddr_topology_map board_topology_map_1g = {
272 DEBUG_LEVEL_ERROR,
Marek Behún09e16b82017-06-09 19:28:45 +0200273 0x1, /* active interfaces */
274 /* cs_mask, mirror, dqs_swap, ck_swap X PUPs */
275 { { { {0x1, 0, 0, 0},
276 {0x1, 0, 0, 0},
277 {0x1, 0, 0, 0},
278 {0x1, 0, 0, 0},
279 {0x1, 0, 0, 0} },
280 SPEED_BIN_DDR_1600K, /* speed_bin */
Chris Packham1a07d212018-05-10 13:28:29 +1200281 MV_DDR_DEV_WIDTH_16BIT, /* memory_width */
282 MV_DDR_DIE_CAP_4GBIT, /* mem_size */
Chris Packham4bf81db2018-12-03 14:26:49 +1300283 MV_DDR_FREQ_800, /* frequency */
Chris Packhamdd092bd2017-11-29 10:38:34 +1300284 0, 0, /* cas_wl cas_l */
Chris Packham3a09e132018-05-10 13:28:30 +1200285 MV_DDR_TEMP_NORMAL, /* temperature */
286 MV_DDR_TIM_2T} }, /* timing */
Chris Packham1a07d212018-05-10 13:28:29 +1200287 BUS_MASK_32BIT, /* Busses mask */
288 MV_DDR_CFG_DEFAULT, /* ddr configuration data source */
Moti Buskila498475e2021-02-19 17:11:19 +0100289 NOT_COMBINED, /* ddr twin-die combined */
Chris Packham1a07d212018-05-10 13:28:29 +1200290 { {0} }, /* raw spd data */
291 {0} /* timing parameters */
Marek Behún09e16b82017-06-09 19:28:45 +0200292};
293
Chris Packham1a07d212018-05-10 13:28:29 +1200294static struct mv_ddr_topology_map board_topology_map_2g = {
295 DEBUG_LEVEL_ERROR,
Marek Behún09e16b82017-06-09 19:28:45 +0200296 0x1, /* active interfaces */
297 /* cs_mask, mirror, dqs_swap, ck_swap X PUPs */
298 { { { {0x1, 0, 0, 0},
299 {0x1, 0, 0, 0},
300 {0x1, 0, 0, 0},
301 {0x1, 0, 0, 0},
302 {0x1, 0, 0, 0} },
303 SPEED_BIN_DDR_1600K, /* speed_bin */
Chris Packham1a07d212018-05-10 13:28:29 +1200304 MV_DDR_DEV_WIDTH_16BIT, /* memory_width */
305 MV_DDR_DIE_CAP_8GBIT, /* mem_size */
Chris Packham4bf81db2018-12-03 14:26:49 +1300306 MV_DDR_FREQ_800, /* frequency */
Chris Packhamdd092bd2017-11-29 10:38:34 +1300307 0, 0, /* cas_wl cas_l */
Chris Packham3a09e132018-05-10 13:28:30 +1200308 MV_DDR_TEMP_NORMAL, /* temperature */
309 MV_DDR_TIM_2T} }, /* timing */
Chris Packham1a07d212018-05-10 13:28:29 +1200310 BUS_MASK_32BIT, /* Busses mask */
311 MV_DDR_CFG_DEFAULT, /* ddr configuration data source */
Moti Buskila498475e2021-02-19 17:11:19 +0100312 NOT_COMBINED, /* ddr twin-die combined */
Chris Packham1a07d212018-05-10 13:28:29 +1200313 { {0} }, /* raw spd data */
314 {0} /* timing parameters */
Marek Behún09e16b82017-06-09 19:28:45 +0200315};
316
Chris Packham1a07d212018-05-10 13:28:29 +1200317struct mv_ddr_topology_map *mv_ddr_topology_map_get(void)
Marek Behún09e16b82017-06-09 19:28:45 +0200318{
Marek Behún77652c72019-05-02 16:53:33 +0200319 if (omnia_get_ram_size_gb() == 2)
Marek Behún09e16b82017-06-09 19:28:45 +0200320 return &board_topology_map_2g;
Marek Behún77652c72019-05-02 16:53:33 +0200321 else
322 return &board_topology_map_1g;
Marek Behún09e16b82017-06-09 19:28:45 +0200323}
324
325#ifndef CONFIG_SPL_BUILD
326static int set_regdomain(void)
327{
328 struct omnia_eeprom oep;
329 char rd[3] = {' ', ' ', 0};
330
331 if (omnia_read_eeprom(&oep))
332 memcpy(rd, &oep.region, 2);
333 else
334 puts("EEPROM regdomain read failed.\n");
335
336 printf("Regdomain set to %s\n", rd);
Simon Glass6a38e412017-08-03 12:22:09 -0600337 return env_set("regdomain", rd);
Marek Behún09e16b82017-06-09 19:28:45 +0200338}
Marek Behún0f2e66a2019-05-02 16:53:37 +0200339
340/*
341 * default factory reset bootcommand on Omnia first sets all the front LEDs
342 * to green and then tries to load the rescue image from SPI flash memory and
343 * boot it
344 */
345#define OMNIA_FACTORY_RESET_BOOTCMD \
346 "i2c dev 2; " \
347 "i2c mw 0x2a.1 0x3 0x1c 1; " \
348 "i2c mw 0x2a.1 0x4 0x1c 1; " \
349 "mw.l 0x01000000 0x00ff000c; " \
350 "i2c write 0x01000000 0x2a.1 0x5 4 -s; " \
Marek Behúnd9b86b02019-05-24 14:57:54 +0200351 "setenv bootargs \"earlyprintk console=ttyS0,115200" \
352 " omniarescue=$omnia_reset\"; " \
Marek Behún0f2e66a2019-05-02 16:53:37 +0200353 "sf probe; " \
354 "sf read 0x1000000 0x100000 0x700000; " \
355 "bootm 0x1000000; " \
356 "bootz 0x1000000"
357
358static void handle_reset_button(void)
359{
360 int ret;
361 u8 reset_status;
362
363 ret = omnia_mcu_read(CMD_GET_RESET, &reset_status, 1);
364 if (ret) {
365 printf("omnia_mcu_read failed: %i, reset status unknown!\n",
366 ret);
367 return;
368 }
369
370 env_set_ulong("omnia_reset", reset_status);
371
372 if (reset_status) {
373 printf("RESET button was pressed, overwriting bootcmd!\n");
374 env_set("bootcmd", OMNIA_FACTORY_RESET_BOOTCMD);
375 }
376}
Marek Behún09e16b82017-06-09 19:28:45 +0200377#endif
378
379int board_early_init_f(void)
380{
Marek Behún09e16b82017-06-09 19:28:45 +0200381 /* Configure MPP */
382 writel(0x11111111, MVEBU_MPP_BASE + 0x00);
383 writel(0x11111111, MVEBU_MPP_BASE + 0x04);
384 writel(0x11244011, MVEBU_MPP_BASE + 0x08);
385 writel(0x22222111, MVEBU_MPP_BASE + 0x0c);
386 writel(0x22200002, MVEBU_MPP_BASE + 0x10);
387 writel(0x30042022, MVEBU_MPP_BASE + 0x14);
388 writel(0x55550555, MVEBU_MPP_BASE + 0x18);
389 writel(0x00005550, MVEBU_MPP_BASE + 0x1c);
390
391 /* Set GPP Out value */
392 writel(OMNIA_GPP_OUT_VAL_LOW, MVEBU_GPIO0_BASE + 0x00);
393 writel(OMNIA_GPP_OUT_VAL_MID, MVEBU_GPIO1_BASE + 0x00);
394
395 /* Set GPP Polarity */
396 writel(OMNIA_GPP_POL_LOW, MVEBU_GPIO0_BASE + 0x0c);
397 writel(OMNIA_GPP_POL_MID, MVEBU_GPIO1_BASE + 0x0c);
398
399 /* Set GPP Out Enable */
400 writel(OMNIA_GPP_OUT_ENA_LOW, MVEBU_GPIO0_BASE + 0x04);
401 writel(OMNIA_GPP_OUT_ENA_MID, MVEBU_GPIO1_BASE + 0x04);
402
Marek Behún09e16b82017-06-09 19:28:45 +0200403 return 0;
404}
405
Marek Behún09e16b82017-06-09 19:28:45 +0200406int board_init(void)
407{
Marek Behún4dfc57e2019-05-02 16:53:31 +0200408 /* address of boot parameters */
Marek Behún09e16b82017-06-09 19:28:45 +0200409 gd->bd->bi_boot_params = mvebu_sdram_bar(0) + 0x100;
410
411#ifndef CONFIG_SPL_BUILD
Marek Behúnba53b6b2019-05-02 16:53:30 +0200412 disable_mcu_watchdog();
Marek Behún09e16b82017-06-09 19:28:45 +0200413#endif
414
415 return 0;
416}
Marek Behún09e16b82017-06-09 19:28:45 +0200417
418int board_late_init(void)
419{
420#ifndef CONFIG_SPL_BUILD
421 set_regdomain();
Marek Behún0f2e66a2019-05-02 16:53:37 +0200422 handle_reset_button();
Marek Behún09e16b82017-06-09 19:28:45 +0200423#endif
Marek Behúndb1e5c62019-05-24 14:57:53 +0200424 pci_init();
Marek Behún09e16b82017-06-09 19:28:45 +0200425
426 return 0;
427}
428
Marek Behún09e16b82017-06-09 19:28:45 +0200429static struct udevice *get_atsha204a_dev(void)
430{
Marek Behún4dfc57e2019-05-02 16:53:31 +0200431 static struct udevice *dev;
Marek Behún09e16b82017-06-09 19:28:45 +0200432
Marek Behún4dfc57e2019-05-02 16:53:31 +0200433 if (dev)
Marek Behún09e16b82017-06-09 19:28:45 +0200434 return dev;
435
436 if (uclass_get_device_by_name(UCLASS_MISC, "atsha204a@64", &dev)) {
437 puts("Cannot find ATSHA204A on I2C bus!\n");
438 dev = NULL;
439 }
440
441 return dev;
442}
Marek Behún09e16b82017-06-09 19:28:45 +0200443
444int checkboard(void)
445{
446 u32 version_num, serial_num;
447 int err = 1;
448
Marek Behún09e16b82017-06-09 19:28:45 +0200449 struct udevice *dev = get_atsha204a_dev();
450
451 if (dev) {
452 err = atsha204a_wakeup(dev);
453 if (err)
454 goto out;
455
456 err = atsha204a_read(dev, ATSHA204A_ZONE_OTP, false,
457 OMNIA_ATSHA204_OTP_VERSION,
Marek Behún4dfc57e2019-05-02 16:53:31 +0200458 (u8 *)&version_num);
Marek Behún09e16b82017-06-09 19:28:45 +0200459 if (err)
460 goto out;
461
462 err = atsha204a_read(dev, ATSHA204A_ZONE_OTP, false,
463 OMNIA_ATSHA204_OTP_SERIAL,
Marek Behún4dfc57e2019-05-02 16:53:31 +0200464 (u8 *)&serial_num);
Marek Behún09e16b82017-06-09 19:28:45 +0200465 if (err)
466 goto out;
467
468 atsha204a_sleep(dev);
469 }
470
471out:
Marek Behúnc4ba72a2019-05-02 16:53:34 +0200472 printf("Turris Omnia:\n");
473 printf(" RAM size: %i MiB\n", omnia_get_ram_size_gb() * 1024);
Marek Behún09e16b82017-06-09 19:28:45 +0200474 if (err)
Marek Behúnc4ba72a2019-05-02 16:53:34 +0200475 printf(" Serial Number: unknown\n");
Marek Behún09e16b82017-06-09 19:28:45 +0200476 else
Marek Behúnc4ba72a2019-05-02 16:53:34 +0200477 printf(" Serial Number: %08X%08X\n", be32_to_cpu(version_num),
478 be32_to_cpu(serial_num));
Marek Behún09e16b82017-06-09 19:28:45 +0200479
480 return 0;
481}
482
483static void increment_mac(u8 *mac)
484{
485 int i;
486
487 for (i = 5; i >= 3; i--) {
488 mac[i] += 1;
489 if (mac[i])
490 break;
491 }
492}
493
494int misc_init_r(void)
495{
Marek Behún09e16b82017-06-09 19:28:45 +0200496 int err;
497 struct udevice *dev = get_atsha204a_dev();
498 u8 mac0[4], mac1[4], mac[6];
499
500 if (!dev)
501 goto out;
502
503 err = atsha204a_wakeup(dev);
504 if (err)
505 goto out;
506
507 err = atsha204a_read(dev, ATSHA204A_ZONE_OTP, false,
508 OMNIA_ATSHA204_OTP_MAC0, mac0);
509 if (err)
510 goto out;
511
512 err = atsha204a_read(dev, ATSHA204A_ZONE_OTP, false,
513 OMNIA_ATSHA204_OTP_MAC1, mac1);
514 if (err)
515 goto out;
516
517 atsha204a_sleep(dev);
518
519 mac[0] = mac0[1];
520 mac[1] = mac0[2];
521 mac[2] = mac0[3];
522 mac[3] = mac1[1];
523 mac[4] = mac1[2];
524 mac[5] = mac1[3];
525
526 if (is_valid_ethaddr(mac))
Marek Behúncb50c712019-05-24 14:57:49 +0200527 eth_env_set_enetaddr("eth1addr", mac);
Marek Behún09e16b82017-06-09 19:28:45 +0200528
529 increment_mac(mac);
530
531 if (is_valid_ethaddr(mac))
Marek Behúncb50c712019-05-24 14:57:49 +0200532 eth_env_set_enetaddr("eth2addr", mac);
Marek Behún09e16b82017-06-09 19:28:45 +0200533
534 increment_mac(mac);
535
536 if (is_valid_ethaddr(mac))
Marek Behúncb50c712019-05-24 14:57:49 +0200537 eth_env_set_enetaddr("ethaddr", mac);
Marek Behún09e16b82017-06-09 19:28:45 +0200538
539out:
Marek Behún09e16b82017-06-09 19:28:45 +0200540 return 0;
541}
542