blob: ae953d0e4bab7a8b088740cb8918caa3a03c2ece [file] [log] [blame]
Pascal Vizelieeac0372020-06-18 16:40:37 +02001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (C) 2020 BayLibre, SAS
4 * Author: Neil Armstrong <narmstrong@baylibre.com>
5 */
6
Pascal Vizelieeac0372020-06-18 16:40:37 +02007#include <dm.h>
Marek Szyprowski85c75642021-02-04 10:11:45 +01008#include <adc.h>
Pascal Vizelieeac0372020-06-18 16:40:37 +02009#include <env.h>
10#include <init.h>
11#include <net.h>
12#include <asm/io.h>
Marek Szyprowskib3e97122020-12-18 15:26:45 +010013#include <asm/arch/boot.h>
Pascal Vizelieeac0372020-06-18 16:40:37 +020014#include <asm/arch/sm.h>
15#include <asm/arch/eth.h>
Pascal Vizeli07168542020-11-27 17:28:21 +010016#include <asm/arch/boot.h>
Pascal Vizelieeac0372020-06-18 16:40:37 +020017
18#define EFUSE_MAC_OFFSET 20
19#define EFUSE_MAC_SIZE 12
20#define MAC_ADDR_LEN 6
21
Marek Szyprowski85c75642021-02-04 10:11:45 +010022#define ODROID_HW_VS_ADC_CHANNEL 1
23
24#define MESON_SOC_ID_G12B 0x29
25#define MESON_SOC_ID_SM1 0x2b
26
Marek Szyprowskib3e97122020-12-18 15:26:45 +010027int mmc_get_env_dev(void)
28{
29 if (meson_get_boot_device() == BOOT_DEVICE_EMMC)
30 return 1;
31 return 0;
32}
33
Marek Szyprowski85c75642021-02-04 10:11:45 +010034/* Variant detection is based on the ADC RAW values for the channel #1 */
35static struct meson_odroid_boards {
36 unsigned int soc_id;
37 unsigned int adc_min;
38 unsigned int adc_max;
39 char *variant;
40} boards[] = {
41 /* OdroidN2 rev 2018,7,23 */
42 { MESON_SOC_ID_G12B, 80 * 4, 90 * 4, "n2" },
43 /* OdroidN2 rev 2018,12,6 */
44 { MESON_SOC_ID_G12B, 160 * 4, 170 * 4, "n2" },
45 /* OdroidN2 rev 2019,1,17 */
46 { MESON_SOC_ID_G12B, 245 * 4, 255 * 4, "n2" },
47 /* OdroidN2 rev 2019,2,7 */
48 { MESON_SOC_ID_G12B, 330 * 4, 350 * 4, "n2" },
49 /* OdroidN2plus rev 2019,11,20 */
Christian Hewittc1bbc1a2021-08-04 11:01:07 +000050 { MESON_SOC_ID_G12B, 410 * 4, 430 * 4, "n2-plus" },
Marek Szyprowski85c75642021-02-04 10:11:45 +010051 /* OdroidC4 rev 2020,01,29 */
52 { MESON_SOC_ID_SM1, 80 * 4, 100 * 4, "c4" },
53 /* OdroidHC4 rev 2019,12,10 */
54 { MESON_SOC_ID_SM1, 300 * 4, 320 * 4, "hc4" },
55 /* OdroidC4 rev 2019,11,29 */
56 { MESON_SOC_ID_SM1, 335 * 4, 345 * 4, "c4" },
57 /* OdroidHC4 rev 2020,8,7 */
58 { MESON_SOC_ID_SM1, 590 * 4, 610 * 4, "hc4" },
59};
60
61static void odroid_set_fdtfile(char *soc, char *variant)
62{
63 char s[128];
64
65 snprintf(s, sizeof(s), "amlogic/meson-%s-odroid-%s.dtb", soc, variant);
66 env_set("fdtfile", s);
67}
68
69static int odroid_detect_variant(void)
70{
71 char *variant = "", *soc = "";
72 unsigned int adcval = 0;
73 int ret, i, soc_id = 0;
74
75 if (of_machine_is_compatible("amlogic,sm1")) {
76 soc_id = MESON_SOC_ID_SM1;
77 soc = "sm1";
78 } else if (of_machine_is_compatible("amlogic,g12b")) {
79 soc_id = MESON_SOC_ID_G12B;
80 soc = "g12b";
81 } else {
82 return -1;
83 }
84
85 ret = adc_channel_single_shot("adc@9000", ODROID_HW_VS_ADC_CHANNEL,
86 &adcval);
87 if (ret)
88 return ret;
89
90 for (i = 0 ; i < ARRAY_SIZE(boards) ; ++i) {
91 if (soc_id == boards[i].soc_id &&
92 adcval >= boards[i].adc_min &&
93 adcval < boards[i].adc_max) {
94 variant = boards[i].variant;
95 break;
96 }
97 }
98
99 printf("Board variant: %s\n", variant);
100 env_set("variant", variant);
101
102 odroid_set_fdtfile(soc, variant);
103
104 return 0;
105}
106
Pascal Vizelieeac0372020-06-18 16:40:37 +0200107int misc_init_r(void)
108{
Neil Armstrongbfbe81a2024-03-20 09:46:11 +0100109 u8 mac_addr[MAC_ADDR_LEN + 1];
Pascal Vizelieeac0372020-06-18 16:40:37 +0200110 char efuse_mac_addr[EFUSE_MAC_SIZE], tmp[3];
111 ssize_t len;
112
Pascal Vizeli07168542020-11-27 17:28:21 +0100113 if (IS_ENABLED(CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG) &&
114 meson_get_soc_rev(tmp, sizeof(tmp)) > 0)
115 env_set("soc_rev", tmp);
116
Pascal Vizelieeac0372020-06-18 16:40:37 +0200117 if (!eth_env_get_enetaddr("ethaddr", mac_addr)) {
118 len = meson_sm_read_efuse(EFUSE_MAC_OFFSET,
119 efuse_mac_addr, EFUSE_MAC_SIZE);
120 if (len != EFUSE_MAC_SIZE)
121 return 0;
122
123 /* MAC is stored in ASCII format, 1bytes = 2characters */
124 for (int i = 0; i < 6; i++) {
125 tmp[0] = efuse_mac_addr[i * 2];
126 tmp[1] = efuse_mac_addr[i * 2 + 1];
127 tmp[2] = '\0';
Simon Glass3ff49ec2021-07-24 09:03:29 -0600128 mac_addr[i] = hextoul(tmp, NULL);
Pascal Vizelieeac0372020-06-18 16:40:37 +0200129 }
Neil Armstrongbfbe81a2024-03-20 09:46:11 +0100130 mac_addr[MAC_ADDR_LEN] = '\0';
Pascal Vizelieeac0372020-06-18 16:40:37 +0200131
132 if (is_valid_ethaddr(mac_addr))
133 eth_env_set_enetaddr("ethaddr", mac_addr);
134 else
135 meson_generate_serial_ethaddr();
136 }
137
Marek Szyprowski85c75642021-02-04 10:11:45 +0100138 odroid_detect_variant();
Pascal Vizelieeac0372020-06-18 16:40:37 +0200139 return 0;
140}