1// SPDX-License-Identifier: GPL-2.0+
2/*
3 * (C) Copyright 2016 Beniamino Galvani <b.galvani@gmail.com>
4 */
5
6#include <common.h>
7#include <dm.h>
8#include <env.h>
9#include <init.h>
10#include <net.h>
11#include <asm/io.h>
12#include <asm/arch/gx.h>
13#include <asm/arch/sm.h>
14#include <asm/arch/eth.h>
15#include <asm/arch/mem.h>
16
17#define EFUSE_SN_OFFSET		20
18#define EFUSE_SN_SIZE		16
19#define EFUSE_MAC_OFFSET	52
20#define EFUSE_MAC_SIZE		6
21
22int misc_init_r(void)
23{
24	u8 mac_addr[EFUSE_MAC_SIZE + 1];
25	char serial[EFUSE_SN_SIZE + 1];
26	ssize_t len;
27
28	if (!eth_env_get_enetaddr("ethaddr", mac_addr)) {
29		len = meson_sm_read_efuse(EFUSE_MAC_OFFSET,
30					  mac_addr, EFUSE_MAC_SIZE);
31		mac_addr[len] = '\0';
32		if (len == EFUSE_MAC_SIZE && is_valid_ethaddr(mac_addr))
33			eth_env_set_enetaddr("ethaddr", mac_addr);
34		else
35			meson_generate_serial_ethaddr();
36	}
37
38	if (!env_get("serial#")) {
39		len = meson_sm_read_efuse(EFUSE_SN_OFFSET, serial,
40			EFUSE_SN_SIZE);
41		serial[len] = '\0';
42		if (len == EFUSE_SN_SIZE)
43			env_set("serial#", serial);
44	}
45
46	return 0;
47}
48