blob: 4d21e62e7338ed1e64d417a3e0e385daab29f0b0 [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>
13#include <miiphy.h>
14#include <netdev.h>
15#include <asm/io.h>
16#include <asm/arch/cpu.h>
17#include <asm/arch/soc.h>
18#include <dm/uclass.h>
19#include <fdt_support.h>
20#include <time.h>
Simon Glass48b6c6b2019-11-14 12:57:16 -070021#include <u-boot/crc.h>
Marek Behún09e16b82017-06-09 19:28:45 +020022# include <atsha204a-i2c.h>
Marek Behún09e16b82017-06-09 19:28:45 +020023
Chris Packham1a07d212018-05-10 13:28:29 +120024#include "../drivers/ddr/marvell/a38x/ddr3_init.h"
Marek Behún09e16b82017-06-09 19:28:45 +020025#include <../serdes/a38x/high_speed_env_spec.h>
26
27DECLARE_GLOBAL_DATA_PTR;
28
Marek Behúnba53b6b2019-05-02 16:53:30 +020029#define OMNIA_I2C_BUS_NAME "i2c@11000->i2cmux@70->i2c@0"
30
31#define OMNIA_I2C_MCU_CHIP_ADDR 0x2a
32#define OMNIA_I2C_MCU_CHIP_LEN 1
33
34#define OMNIA_I2C_EEPROM_CHIP_ADDR 0x54
35#define OMNIA_I2C_EEPROM_CHIP_LEN 2
Marek Behún09e16b82017-06-09 19:28:45 +020036#define OMNIA_I2C_EEPROM_MAGIC 0x0341a034
37
Marek Behúnba53b6b2019-05-02 16:53:30 +020038enum mcu_commands {
39 CMD_GET_STATUS_WORD = 0x01,
40 CMD_GET_RESET = 0x09,
41 CMD_WATCHDOG_STATE = 0x0b,
42};
43
44enum status_word_bits {
45 CARD_DET_STSBIT = 0x0010,
46 MSATA_IND_STSBIT = 0x0020,
47};
Marek Behún09e16b82017-06-09 19:28:45 +020048
49#define OMNIA_ATSHA204_OTP_VERSION 0
50#define OMNIA_ATSHA204_OTP_SERIAL 1
51#define OMNIA_ATSHA204_OTP_MAC0 3
52#define OMNIA_ATSHA204_OTP_MAC1 4
53
Marek Behún09e16b82017-06-09 19:28:45 +020054/*
55 * Those values and defines are taken from the Marvell U-Boot version
56 * "u-boot-2013.01-2014_T3.0"
57 */
58#define OMNIA_GPP_OUT_ENA_LOW \
59 (~(BIT(1) | BIT(4) | BIT(6) | BIT(7) | BIT(8) | BIT(9) | \
60 BIT(10) | BIT(11) | BIT(19) | BIT(22) | BIT(23) | BIT(25) | \
61 BIT(26) | BIT(27) | BIT(29) | BIT(30) | BIT(31)))
62#define OMNIA_GPP_OUT_ENA_MID \
63 (~(BIT(0) | BIT(1) | BIT(2) | BIT(3) | BIT(4) | BIT(15) | \
64 BIT(16) | BIT(17) | BIT(18)))
65
66#define OMNIA_GPP_OUT_VAL_LOW 0x0
67#define OMNIA_GPP_OUT_VAL_MID 0x0
68#define OMNIA_GPP_POL_LOW 0x0
69#define OMNIA_GPP_POL_MID 0x0
70
71static struct serdes_map board_serdes_map_pex[] = {
72 {PEX0, SERDES_SPEED_5_GBPS, PEX_ROOT_COMPLEX_X1, 0, 0},
73 {USB3_HOST0, SERDES_SPEED_5_GBPS, SERDES_DEFAULT_MODE, 0, 0},
74 {PEX1, SERDES_SPEED_5_GBPS, PEX_ROOT_COMPLEX_X1, 0, 0},
75 {USB3_HOST1, SERDES_SPEED_5_GBPS, SERDES_DEFAULT_MODE, 0, 0},
76 {PEX2, SERDES_SPEED_5_GBPS, PEX_ROOT_COMPLEX_X1, 0, 0},
77 {SGMII2, SERDES_SPEED_1_25_GBPS, SERDES_DEFAULT_MODE, 0, 0}
78};
79
80static struct serdes_map board_serdes_map_sata[] = {
81 {SATA0, SERDES_SPEED_6_GBPS, SERDES_DEFAULT_MODE, 0, 0},
82 {USB3_HOST0, SERDES_SPEED_5_GBPS, SERDES_DEFAULT_MODE, 0, 0},
83 {PEX1, SERDES_SPEED_5_GBPS, PEX_ROOT_COMPLEX_X1, 0, 0},
84 {USB3_HOST1, SERDES_SPEED_5_GBPS, SERDES_DEFAULT_MODE, 0, 0},
85 {PEX2, SERDES_SPEED_5_GBPS, PEX_ROOT_COMPLEX_X1, 0, 0},
86 {SGMII2, SERDES_SPEED_1_25_GBPS, SERDES_DEFAULT_MODE, 0, 0}
87};
88
Marek Behúnba53b6b2019-05-02 16:53:30 +020089static struct udevice *omnia_get_i2c_chip(const char *name, uint addr,
90 uint offset_len)
Marek Behún09e16b82017-06-09 19:28:45 +020091{
92 struct udevice *bus, *dev;
Marek Behúnba53b6b2019-05-02 16:53:30 +020093 int ret;
Marek Behún09e16b82017-06-09 19:28:45 +020094
Marek Behúnba53b6b2019-05-02 16:53:30 +020095 ret = uclass_get_device_by_name(UCLASS_I2C, OMNIA_I2C_BUS_NAME, &bus);
96 if (ret) {
97 printf("Cannot get I2C bus %s: uclass_get_device_by_name failed: %i\n",
98 OMNIA_I2C_BUS_NAME, ret);
99 return NULL;
Marek Behún09e16b82017-06-09 19:28:45 +0200100 }
101
Marek Behúnba53b6b2019-05-02 16:53:30 +0200102 ret = i2c_get_chip(bus, addr, offset_len, &dev);
Marek Behún09e16b82017-06-09 19:28:45 +0200103 if (ret) {
Marek Behúnba53b6b2019-05-02 16:53:30 +0200104 printf("Cannot get %s I2C chip: i2c_get_chip failed: %i\n",
105 name, ret);
106 return NULL;
Marek Behún09e16b82017-06-09 19:28:45 +0200107 }
108
Marek Behúnba53b6b2019-05-02 16:53:30 +0200109 return dev;
110}
Marek Behúnd0b374d2017-08-04 15:28:25 +0200111
Marek Behúnba53b6b2019-05-02 16:53:30 +0200112static int omnia_mcu_read(u8 cmd, void *buf, int len)
113{
114 struct udevice *chip;
115
116 chip = omnia_get_i2c_chip("MCU", OMNIA_I2C_MCU_CHIP_ADDR,
117 OMNIA_I2C_MCU_CHIP_LEN);
118 if (!chip)
119 return -ENODEV;
120
121 return dm_i2c_read(chip, cmd, buf, len);
122}
123
124#ifndef CONFIG_SPL_BUILD
125static int omnia_mcu_write(u8 cmd, const void *buf, int len)
126{
127 struct udevice *chip;
128
129 chip = omnia_get_i2c_chip("MCU", OMNIA_I2C_MCU_CHIP_ADDR,
130 OMNIA_I2C_MCU_CHIP_LEN);
131 if (!chip)
132 return -ENODEV;
133
134 return dm_i2c_write(chip, cmd, buf, len);
135}
136
137static bool disable_mcu_watchdog(void)
138{
139 int ret;
140
141 puts("Disabling MCU watchdog... ");
142
143 ret = omnia_mcu_write(CMD_WATCHDOG_STATE, "\x00", 1);
144 if (ret) {
145 printf("omnia_mcu_write failed: %i\n", ret);
Marek Behún09e16b82017-06-09 19:28:45 +0200146 return false;
147 }
148
Marek Behúnba53b6b2019-05-02 16:53:30 +0200149 puts("disabled\n");
150
151 return true;
152}
153#endif
154
155static bool omnia_detect_sata(void)
156{
157 int ret;
158 u16 stsword;
159
160 puts("MiniPCIe/mSATA card detection... ");
161
162 ret = omnia_mcu_read(CMD_GET_STATUS_WORD, &stsword, sizeof(stsword));
163 if (ret) {
164 printf("omnia_mcu_read failed: %i, defaulting to MiniPCIe card\n",
165 ret);
Marek Behún09e16b82017-06-09 19:28:45 +0200166 return false;
167 }
168
Marek Behúnba53b6b2019-05-02 16:53:30 +0200169 if (!(stsword & CARD_DET_STSBIT)) {
170 puts("none\n");
Marek Behún09e16b82017-06-09 19:28:45 +0200171 return false;
172 }
Marek Behúnba53b6b2019-05-02 16:53:30 +0200173
174 if (stsword & MSATA_IND_STSBIT)
175 puts("mSATA\n");
176 else
177 puts("MiniPCIe\n");
178
179 return stsword & MSATA_IND_STSBIT ? true : false;
Marek Behún09e16b82017-06-09 19:28:45 +0200180}
181
182int hws_board_topology_load(struct serdes_map **serdes_map_array, u8 *count)
183{
184 if (omnia_detect_sata()) {
185 *serdes_map_array = board_serdes_map_sata;
186 *count = ARRAY_SIZE(board_serdes_map_sata);
187 } else {
188 *serdes_map_array = board_serdes_map_pex;
189 *count = ARRAY_SIZE(board_serdes_map_pex);
190 }
191
192 return 0;
193}
194
195struct omnia_eeprom {
196 u32 magic;
197 u32 ramsize;
198 char region[4];
199 u32 crc;
200};
201
202static bool omnia_read_eeprom(struct omnia_eeprom *oep)
203{
Marek Behúnba53b6b2019-05-02 16:53:30 +0200204 struct udevice *chip;
205 u32 crc;
206 int ret;
Marek Behún09e16b82017-06-09 19:28:45 +0200207
Marek Behúnba53b6b2019-05-02 16:53:30 +0200208 chip = omnia_get_i2c_chip("EEPROM", OMNIA_I2C_EEPROM_CHIP_ADDR,
209 OMNIA_I2C_EEPROM_CHIP_LEN);
210
211 if (!chip)
Marek Behún09e16b82017-06-09 19:28:45 +0200212 return false;
Marek Behún09e16b82017-06-09 19:28:45 +0200213
Marek Behúnba53b6b2019-05-02 16:53:30 +0200214 ret = dm_i2c_read(chip, 0, (void *)oep, sizeof(*oep));
Marek Behún09e16b82017-06-09 19:28:45 +0200215 if (ret) {
Marek Behúnba53b6b2019-05-02 16:53:30 +0200216 printf("dm_i2c_read failed: %i, cannot read EEPROM\n", ret);
Marek Behún09e16b82017-06-09 19:28:45 +0200217 return false;
218 }
219
Marek Behúnba53b6b2019-05-02 16:53:30 +0200220 if (oep->magic != OMNIA_I2C_EEPROM_MAGIC) {
221 printf("bad EEPROM magic number (%08x, should be %08x)\n",
222 oep->magic, OMNIA_I2C_EEPROM_MAGIC);
223 return false;
Marek Behún09e16b82017-06-09 19:28:45 +0200224 }
225
Marek Behúnba53b6b2019-05-02 16:53:30 +0200226 crc = crc32(0, (void *)oep, sizeof(*oep) - 4);
227 if (crc != oep->crc) {
228 printf("bad EEPROM CRC (stored %08x, computed %08x)\n",
229 oep->crc, crc);
Marek Behún09e16b82017-06-09 19:28:45 +0200230 return false;
231 }
232
233 return true;
234}
235
Marek Behún77652c72019-05-02 16:53:33 +0200236static int omnia_get_ram_size_gb(void)
237{
238 static int ram_size;
239 struct omnia_eeprom oep;
240
241 if (!ram_size) {
242 /* Get the board config from EEPROM */
243 if (omnia_read_eeprom(&oep)) {
244 debug("Memory config in EEPROM: 0x%02x\n", oep.ramsize);
245
246 if (oep.ramsize == 0x2)
247 ram_size = 2;
248 else
249 ram_size = 1;
250 } else {
251 /* Hardcoded fallback */
252 puts("Memory config from EEPROM read failed!\n");
253 puts("Falling back to default 1 GiB!\n");
254 ram_size = 1;
255 }
256 }
257
258 return ram_size;
259}
260
Marek Behún09e16b82017-06-09 19:28:45 +0200261/*
262 * Define the DDR layout / topology here in the board file. This will
263 * be used by the DDR3 init code in the SPL U-Boot version to configure
264 * the DDR3 controller.
265 */
Chris Packham1a07d212018-05-10 13:28:29 +1200266static struct mv_ddr_topology_map board_topology_map_1g = {
267 DEBUG_LEVEL_ERROR,
Marek Behún09e16b82017-06-09 19:28:45 +0200268 0x1, /* active interfaces */
269 /* cs_mask, mirror, dqs_swap, ck_swap X PUPs */
270 { { { {0x1, 0, 0, 0},
271 {0x1, 0, 0, 0},
272 {0x1, 0, 0, 0},
273 {0x1, 0, 0, 0},
274 {0x1, 0, 0, 0} },
275 SPEED_BIN_DDR_1600K, /* speed_bin */
Chris Packham1a07d212018-05-10 13:28:29 +1200276 MV_DDR_DEV_WIDTH_16BIT, /* memory_width */
277 MV_DDR_DIE_CAP_4GBIT, /* mem_size */
Chris Packham4bf81db2018-12-03 14:26:49 +1300278 MV_DDR_FREQ_800, /* frequency */
Chris Packhamdd092bd2017-11-29 10:38:34 +1300279 0, 0, /* cas_wl cas_l */
Chris Packham3a09e132018-05-10 13:28:30 +1200280 MV_DDR_TEMP_NORMAL, /* temperature */
281 MV_DDR_TIM_2T} }, /* timing */
Chris Packham1a07d212018-05-10 13:28:29 +1200282 BUS_MASK_32BIT, /* Busses mask */
283 MV_DDR_CFG_DEFAULT, /* ddr configuration data source */
284 { {0} }, /* raw spd data */
285 {0} /* timing parameters */
Marek Behún09e16b82017-06-09 19:28:45 +0200286};
287
Chris Packham1a07d212018-05-10 13:28:29 +1200288static struct mv_ddr_topology_map board_topology_map_2g = {
289 DEBUG_LEVEL_ERROR,
Marek Behún09e16b82017-06-09 19:28:45 +0200290 0x1, /* active interfaces */
291 /* cs_mask, mirror, dqs_swap, ck_swap X PUPs */
292 { { { {0x1, 0, 0, 0},
293 {0x1, 0, 0, 0},
294 {0x1, 0, 0, 0},
295 {0x1, 0, 0, 0},
296 {0x1, 0, 0, 0} },
297 SPEED_BIN_DDR_1600K, /* speed_bin */
Chris Packham1a07d212018-05-10 13:28:29 +1200298 MV_DDR_DEV_WIDTH_16BIT, /* memory_width */
299 MV_DDR_DIE_CAP_8GBIT, /* mem_size */
Chris Packham4bf81db2018-12-03 14:26:49 +1300300 MV_DDR_FREQ_800, /* frequency */
Chris Packhamdd092bd2017-11-29 10:38:34 +1300301 0, 0, /* cas_wl cas_l */
Chris Packham3a09e132018-05-10 13:28:30 +1200302 MV_DDR_TEMP_NORMAL, /* temperature */
303 MV_DDR_TIM_2T} }, /* timing */
Chris Packham1a07d212018-05-10 13:28:29 +1200304 BUS_MASK_32BIT, /* Busses mask */
305 MV_DDR_CFG_DEFAULT, /* ddr configuration data source */
306 { {0} }, /* raw spd data */
307 {0} /* timing parameters */
Marek Behún09e16b82017-06-09 19:28:45 +0200308};
309
Chris Packham1a07d212018-05-10 13:28:29 +1200310struct mv_ddr_topology_map *mv_ddr_topology_map_get(void)
Marek Behún09e16b82017-06-09 19:28:45 +0200311{
Marek Behún77652c72019-05-02 16:53:33 +0200312 if (omnia_get_ram_size_gb() == 2)
Marek Behún09e16b82017-06-09 19:28:45 +0200313 return &board_topology_map_2g;
Marek Behún77652c72019-05-02 16:53:33 +0200314 else
315 return &board_topology_map_1g;
Marek Behún09e16b82017-06-09 19:28:45 +0200316}
317
318#ifndef CONFIG_SPL_BUILD
319static int set_regdomain(void)
320{
321 struct omnia_eeprom oep;
322 char rd[3] = {' ', ' ', 0};
323
324 if (omnia_read_eeprom(&oep))
325 memcpy(rd, &oep.region, 2);
326 else
327 puts("EEPROM regdomain read failed.\n");
328
329 printf("Regdomain set to %s\n", rd);
Simon Glass6a38e412017-08-03 12:22:09 -0600330 return env_set("regdomain", rd);
Marek Behún09e16b82017-06-09 19:28:45 +0200331}
Marek Behún0f2e66a2019-05-02 16:53:37 +0200332
333/*
334 * default factory reset bootcommand on Omnia first sets all the front LEDs
335 * to green and then tries to load the rescue image from SPI flash memory and
336 * boot it
337 */
338#define OMNIA_FACTORY_RESET_BOOTCMD \
339 "i2c dev 2; " \
340 "i2c mw 0x2a.1 0x3 0x1c 1; " \
341 "i2c mw 0x2a.1 0x4 0x1c 1; " \
342 "mw.l 0x01000000 0x00ff000c; " \
343 "i2c write 0x01000000 0x2a.1 0x5 4 -s; " \
Marek Behúnd9b86b02019-05-24 14:57:54 +0200344 "setenv bootargs \"earlyprintk console=ttyS0,115200" \
345 " omniarescue=$omnia_reset\"; " \
Marek Behún0f2e66a2019-05-02 16:53:37 +0200346 "sf probe; " \
347 "sf read 0x1000000 0x100000 0x700000; " \
348 "bootm 0x1000000; " \
349 "bootz 0x1000000"
350
351static void handle_reset_button(void)
352{
353 int ret;
354 u8 reset_status;
355
356 ret = omnia_mcu_read(CMD_GET_RESET, &reset_status, 1);
357 if (ret) {
358 printf("omnia_mcu_read failed: %i, reset status unknown!\n",
359 ret);
360 return;
361 }
362
363 env_set_ulong("omnia_reset", reset_status);
364
365 if (reset_status) {
366 printf("RESET button was pressed, overwriting bootcmd!\n");
367 env_set("bootcmd", OMNIA_FACTORY_RESET_BOOTCMD);
368 }
369}
Marek Behún09e16b82017-06-09 19:28:45 +0200370#endif
371
372int board_early_init_f(void)
373{
Marek Behún09e16b82017-06-09 19:28:45 +0200374 /* Configure MPP */
375 writel(0x11111111, MVEBU_MPP_BASE + 0x00);
376 writel(0x11111111, MVEBU_MPP_BASE + 0x04);
377 writel(0x11244011, MVEBU_MPP_BASE + 0x08);
378 writel(0x22222111, MVEBU_MPP_BASE + 0x0c);
379 writel(0x22200002, MVEBU_MPP_BASE + 0x10);
380 writel(0x30042022, MVEBU_MPP_BASE + 0x14);
381 writel(0x55550555, MVEBU_MPP_BASE + 0x18);
382 writel(0x00005550, MVEBU_MPP_BASE + 0x1c);
383
384 /* Set GPP Out value */
385 writel(OMNIA_GPP_OUT_VAL_LOW, MVEBU_GPIO0_BASE + 0x00);
386 writel(OMNIA_GPP_OUT_VAL_MID, MVEBU_GPIO1_BASE + 0x00);
387
388 /* Set GPP Polarity */
389 writel(OMNIA_GPP_POL_LOW, MVEBU_GPIO0_BASE + 0x0c);
390 writel(OMNIA_GPP_POL_MID, MVEBU_GPIO1_BASE + 0x0c);
391
392 /* Set GPP Out Enable */
393 writel(OMNIA_GPP_OUT_ENA_LOW, MVEBU_GPIO0_BASE + 0x04);
394 writel(OMNIA_GPP_OUT_ENA_MID, MVEBU_GPIO1_BASE + 0x04);
395
Marek Behún09e16b82017-06-09 19:28:45 +0200396 return 0;
397}
398
Marek Behún09e16b82017-06-09 19:28:45 +0200399int board_init(void)
400{
Marek Behún4dfc57e2019-05-02 16:53:31 +0200401 /* address of boot parameters */
Marek Behún09e16b82017-06-09 19:28:45 +0200402 gd->bd->bi_boot_params = mvebu_sdram_bar(0) + 0x100;
403
404#ifndef CONFIG_SPL_BUILD
Marek Behúnba53b6b2019-05-02 16:53:30 +0200405 disable_mcu_watchdog();
Marek Behún09e16b82017-06-09 19:28:45 +0200406#endif
407
408 return 0;
409}
Marek Behún09e16b82017-06-09 19:28:45 +0200410
411int board_late_init(void)
412{
413#ifndef CONFIG_SPL_BUILD
414 set_regdomain();
Marek Behún0f2e66a2019-05-02 16:53:37 +0200415 handle_reset_button();
Marek Behún09e16b82017-06-09 19:28:45 +0200416#endif
Marek Behúndb1e5c62019-05-24 14:57:53 +0200417 pci_init();
Marek Behún09e16b82017-06-09 19:28:45 +0200418
419 return 0;
420}
421
Marek Behún09e16b82017-06-09 19:28:45 +0200422static struct udevice *get_atsha204a_dev(void)
423{
Marek Behún4dfc57e2019-05-02 16:53:31 +0200424 static struct udevice *dev;
Marek Behún09e16b82017-06-09 19:28:45 +0200425
Marek Behún4dfc57e2019-05-02 16:53:31 +0200426 if (dev)
Marek Behún09e16b82017-06-09 19:28:45 +0200427 return dev;
428
429 if (uclass_get_device_by_name(UCLASS_MISC, "atsha204a@64", &dev)) {
430 puts("Cannot find ATSHA204A on I2C bus!\n");
431 dev = NULL;
432 }
433
434 return dev;
435}
Marek Behún09e16b82017-06-09 19:28:45 +0200436
437int checkboard(void)
438{
439 u32 version_num, serial_num;
440 int err = 1;
441
Marek Behún09e16b82017-06-09 19:28:45 +0200442 struct udevice *dev = get_atsha204a_dev();
443
444 if (dev) {
445 err = atsha204a_wakeup(dev);
446 if (err)
447 goto out;
448
449 err = atsha204a_read(dev, ATSHA204A_ZONE_OTP, false,
450 OMNIA_ATSHA204_OTP_VERSION,
Marek Behún4dfc57e2019-05-02 16:53:31 +0200451 (u8 *)&version_num);
Marek Behún09e16b82017-06-09 19:28:45 +0200452 if (err)
453 goto out;
454
455 err = atsha204a_read(dev, ATSHA204A_ZONE_OTP, false,
456 OMNIA_ATSHA204_OTP_SERIAL,
Marek Behún4dfc57e2019-05-02 16:53:31 +0200457 (u8 *)&serial_num);
Marek Behún09e16b82017-06-09 19:28:45 +0200458 if (err)
459 goto out;
460
461 atsha204a_sleep(dev);
462 }
463
464out:
Marek Behúnc4ba72a2019-05-02 16:53:34 +0200465 printf("Turris Omnia:\n");
466 printf(" RAM size: %i MiB\n", omnia_get_ram_size_gb() * 1024);
Marek Behún09e16b82017-06-09 19:28:45 +0200467 if (err)
Marek Behúnc4ba72a2019-05-02 16:53:34 +0200468 printf(" Serial Number: unknown\n");
Marek Behún09e16b82017-06-09 19:28:45 +0200469 else
Marek Behúnc4ba72a2019-05-02 16:53:34 +0200470 printf(" Serial Number: %08X%08X\n", be32_to_cpu(version_num),
471 be32_to_cpu(serial_num));
Marek Behún09e16b82017-06-09 19:28:45 +0200472
473 return 0;
474}
475
476static void increment_mac(u8 *mac)
477{
478 int i;
479
480 for (i = 5; i >= 3; i--) {
481 mac[i] += 1;
482 if (mac[i])
483 break;
484 }
485}
486
487int misc_init_r(void)
488{
Marek Behún09e16b82017-06-09 19:28:45 +0200489 int err;
490 struct udevice *dev = get_atsha204a_dev();
491 u8 mac0[4], mac1[4], mac[6];
492
493 if (!dev)
494 goto out;
495
496 err = atsha204a_wakeup(dev);
497 if (err)
498 goto out;
499
500 err = atsha204a_read(dev, ATSHA204A_ZONE_OTP, false,
501 OMNIA_ATSHA204_OTP_MAC0, mac0);
502 if (err)
503 goto out;
504
505 err = atsha204a_read(dev, ATSHA204A_ZONE_OTP, false,
506 OMNIA_ATSHA204_OTP_MAC1, mac1);
507 if (err)
508 goto out;
509
510 atsha204a_sleep(dev);
511
512 mac[0] = mac0[1];
513 mac[1] = mac0[2];
514 mac[2] = mac0[3];
515 mac[3] = mac1[1];
516 mac[4] = mac1[2];
517 mac[5] = mac1[3];
518
519 if (is_valid_ethaddr(mac))
Marek Behúncb50c712019-05-24 14:57:49 +0200520 eth_env_set_enetaddr("eth1addr", mac);
Marek Behún09e16b82017-06-09 19:28:45 +0200521
522 increment_mac(mac);
523
524 if (is_valid_ethaddr(mac))
Marek Behúncb50c712019-05-24 14:57:49 +0200525 eth_env_set_enetaddr("eth2addr", mac);
Marek Behún09e16b82017-06-09 19:28:45 +0200526
527 increment_mac(mac);
528
529 if (is_valid_ethaddr(mac))
Marek Behúncb50c712019-05-24 14:57:49 +0200530 eth_env_set_enetaddr("ethaddr", mac);
Marek Behún09e16b82017-06-09 19:28:45 +0200531
532out:
Marek Behún09e16b82017-06-09 19:28:45 +0200533 return 0;
534}
535