blob: 07a08dcd17001b567b27648204bc6987e367e570 [file] [log] [blame]
Vyacheslav Bocharov7f301d32021-09-20 11:40:15 +03001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (C) 2021 Vyacheslav Bocharov
4 * Author: Vyacheslav Bocharov <adeep@lexina.in>
5 * Author: Neil Armstrong <narmstrong@baylibre.com>
6 *
7 */
8
Vyacheslav Bocharov7f301d32021-09-20 11:40:15 +03009#include <dm.h>
10#include <adc.h>
11#include <env.h>
12#include <init.h>
13#include <net.h>
14#include <asm/io.h>
15#include <asm/arch/gx.h>
16#include <asm/arch/sm.h>
17#include <asm/arch/eth.h>
18#include <asm/arch/mem.h>
19
20#define EFUSE_SN_OFFSET 50
21#define EFUSE_SN_SIZE 32
22#define EFUSE_MAC_OFFSET 0
23#define EFUSE_MAC_SIZE 6
24#define EFUSE_USID_OFFSET 18
25#define EFUSE_USID_SIZE 32
26
27int misc_init_r(void)
28{
Neil Armstrongbfbe81a2024-03-20 09:46:11 +010029 u8 mac_addr[EFUSE_MAC_SIZE + 1];
30 char serial[EFUSE_SN_SIZE + 1];
31 char usid[EFUSE_USID_SIZE + 1];
Vyacheslav Bocharov7f301d32021-09-20 11:40:15 +030032 ssize_t len;
33 unsigned int adcval;
34 int ret;
35
36 if (!eth_env_get_enetaddr("ethaddr", mac_addr)) {
37 len = meson_sm_read_efuse(EFUSE_MAC_OFFSET,
38 mac_addr, EFUSE_MAC_SIZE);
Neil Armstrongbfbe81a2024-03-20 09:46:11 +010039 mac_addr[len] = '\0';
Vyacheslav Bocharov7f301d32021-09-20 11:40:15 +030040 if (len == EFUSE_MAC_SIZE && is_valid_ethaddr(mac_addr))
41 eth_env_set_enetaddr("ethaddr", mac_addr);
42 else
43 meson_generate_serial_ethaddr();
44 }
45
46 if (!env_get("serial")) {
47 len = meson_sm_read_efuse(EFUSE_SN_OFFSET, serial,
48 EFUSE_SN_SIZE);
Neil Armstrongbfbe81a2024-03-20 09:46:11 +010049 serial[len] = '\0';
Vyacheslav Bocharov7f301d32021-09-20 11:40:15 +030050 if (len == EFUSE_SN_SIZE)
51 env_set("serial", serial);
52 }
53
54 if (!env_get("usid")) {
55 len = meson_sm_read_efuse(EFUSE_USID_OFFSET, usid,
56 EFUSE_USID_SIZE);
Neil Armstrongbfbe81a2024-03-20 09:46:11 +010057 usid[len] = '\0';
Vyacheslav Bocharov7f301d32021-09-20 11:40:15 +030058 if (len == EFUSE_USID_SIZE)
59 env_set("usid", usid);
60 }
61
62 ret = adc_channel_single_shot("adc@8680", 0, &adcval);
63 if (adcval < 3000)
64 env_set("userbutton", "true");
65 else
66 env_set("userbutton", "false");
67
68 return 0;
69}