Vyacheslav Bocharov | 7f301d3 | 2021-09-20 11:40:15 +0300 | [diff] [blame] | 1 | // 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 Bocharov | 7f301d3 | 2021-09-20 11:40:15 +0300 | [diff] [blame] | 9 | #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 | |
| 27 | int misc_init_r(void) |
| 28 | { |
Neil Armstrong | bfbe81a | 2024-03-20 09:46:11 +0100 | [diff] [blame] | 29 | u8 mac_addr[EFUSE_MAC_SIZE + 1]; |
| 30 | char serial[EFUSE_SN_SIZE + 1]; |
| 31 | char usid[EFUSE_USID_SIZE + 1]; |
Vyacheslav Bocharov | 7f301d3 | 2021-09-20 11:40:15 +0300 | [diff] [blame] | 32 | 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 Armstrong | bfbe81a | 2024-03-20 09:46:11 +0100 | [diff] [blame] | 39 | mac_addr[len] = '\0'; |
Vyacheslav Bocharov | 7f301d3 | 2021-09-20 11:40:15 +0300 | [diff] [blame] | 40 | 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 Armstrong | bfbe81a | 2024-03-20 09:46:11 +0100 | [diff] [blame] | 49 | serial[len] = '\0'; |
Vyacheslav Bocharov | 7f301d3 | 2021-09-20 11:40:15 +0300 | [diff] [blame] | 50 | 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 Armstrong | bfbe81a | 2024-03-20 09:46:11 +0100 | [diff] [blame] | 57 | usid[len] = '\0'; |
Vyacheslav Bocharov | 7f301d3 | 2021-09-20 11:40:15 +0300 | [diff] [blame] | 58 | 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 | } |