blob: 0b781666e985d3b70f0838a2ccb9012f0e81d67b [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
9#include <common.h>
10#include <dm.h>
11#include <adc.h>
12#include <env.h>
13#include <init.h>
14#include <net.h>
15#include <asm/io.h>
16#include <asm/arch/gx.h>
17#include <asm/arch/sm.h>
18#include <asm/arch/eth.h>
19#include <asm/arch/mem.h>
20
21#define EFUSE_SN_OFFSET 50
22#define EFUSE_SN_SIZE 32
23#define EFUSE_MAC_OFFSET 0
24#define EFUSE_MAC_SIZE 6
25#define EFUSE_USID_OFFSET 18
26#define EFUSE_USID_SIZE 32
27
28int misc_init_r(void)
29{
Neil Armstrongbfbe81a2024-03-20 09:46:11 +010030 u8 mac_addr[EFUSE_MAC_SIZE + 1];
31 char serial[EFUSE_SN_SIZE + 1];
32 char usid[EFUSE_USID_SIZE + 1];
Vyacheslav Bocharov7f301d32021-09-20 11:40:15 +030033 ssize_t len;
34 unsigned int adcval;
35 int ret;
36
37 if (!eth_env_get_enetaddr("ethaddr", mac_addr)) {
38 len = meson_sm_read_efuse(EFUSE_MAC_OFFSET,
39 mac_addr, EFUSE_MAC_SIZE);
Neil Armstrongbfbe81a2024-03-20 09:46:11 +010040 mac_addr[len] = '\0';
Vyacheslav Bocharov7f301d32021-09-20 11:40:15 +030041 if (len == EFUSE_MAC_SIZE && is_valid_ethaddr(mac_addr))
42 eth_env_set_enetaddr("ethaddr", mac_addr);
43 else
44 meson_generate_serial_ethaddr();
45 }
46
47 if (!env_get("serial")) {
48 len = meson_sm_read_efuse(EFUSE_SN_OFFSET, serial,
49 EFUSE_SN_SIZE);
Neil Armstrongbfbe81a2024-03-20 09:46:11 +010050 serial[len] = '\0';
Vyacheslav Bocharov7f301d32021-09-20 11:40:15 +030051 if (len == EFUSE_SN_SIZE)
52 env_set("serial", serial);
53 }
54
55 if (!env_get("usid")) {
56 len = meson_sm_read_efuse(EFUSE_USID_OFFSET, usid,
57 EFUSE_USID_SIZE);
Neil Armstrongbfbe81a2024-03-20 09:46:11 +010058 usid[len] = '\0';
Vyacheslav Bocharov7f301d32021-09-20 11:40:15 +030059 if (len == EFUSE_USID_SIZE)
60 env_set("usid", usid);
61 }
62
63 ret = adc_channel_single_shot("adc@8680", 0, &adcval);
64 if (adcval < 3000)
65 env_set("userbutton", "true");
66 else
67 env_set("userbutton", "false");
68
69 return 0;
70}