blob: 2135457edd97938927c7e70859a4537d138a79dc [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
7#include <common.h>
8#include <dm.h>
Marek Szyprowski85c75642021-02-04 10:11:45 +01009#include <adc.h>
Pascal Vizelieeac0372020-06-18 16:40:37 +020010#include <env.h>
11#include <init.h>
12#include <net.h>
13#include <asm/io.h>
Marek Szyprowskib3e97122020-12-18 15:26:45 +010014#include <asm/arch/boot.h>
Pascal Vizelieeac0372020-06-18 16:40:37 +020015#include <asm/arch/sm.h>
16#include <asm/arch/eth.h>
Pascal Vizeli07168542020-11-27 17:28:21 +010017#include <asm/arch/boot.h>
Pascal Vizelieeac0372020-06-18 16:40:37 +020018
19#define EFUSE_MAC_OFFSET 20
20#define EFUSE_MAC_SIZE 12
21#define MAC_ADDR_LEN 6
22
Marek Szyprowski85c75642021-02-04 10:11:45 +010023#define ODROID_HW_VS_ADC_CHANNEL 1
24
25#define MESON_SOC_ID_G12B 0x29
26#define MESON_SOC_ID_SM1 0x2b
27
Marek Szyprowskib3e97122020-12-18 15:26:45 +010028int mmc_get_env_dev(void)
29{
30 if (meson_get_boot_device() == BOOT_DEVICE_EMMC)
31 return 1;
32 return 0;
33}
34
Marek Szyprowski85c75642021-02-04 10:11:45 +010035/* Variant detection is based on the ADC RAW values for the channel #1 */
36static struct meson_odroid_boards {
37 unsigned int soc_id;
38 unsigned int adc_min;
39 unsigned int adc_max;
40 char *variant;
41} boards[] = {
42 /* OdroidN2 rev 2018,7,23 */
43 { MESON_SOC_ID_G12B, 80 * 4, 90 * 4, "n2" },
44 /* OdroidN2 rev 2018,12,6 */
45 { MESON_SOC_ID_G12B, 160 * 4, 170 * 4, "n2" },
46 /* OdroidN2 rev 2019,1,17 */
47 { MESON_SOC_ID_G12B, 245 * 4, 255 * 4, "n2" },
48 /* OdroidN2 rev 2019,2,7 */
49 { MESON_SOC_ID_G12B, 330 * 4, 350 * 4, "n2" },
50 /* OdroidN2plus rev 2019,11,20 */
Christian Hewittc1bbc1a2021-08-04 11:01:07 +000051 { MESON_SOC_ID_G12B, 410 * 4, 430 * 4, "n2-plus" },
Marek Szyprowski85c75642021-02-04 10:11:45 +010052 /* OdroidC4 rev 2020,01,29 */
53 { MESON_SOC_ID_SM1, 80 * 4, 100 * 4, "c4" },
54 /* OdroidHC4 rev 2019,12,10 */
55 { MESON_SOC_ID_SM1, 300 * 4, 320 * 4, "hc4" },
56 /* OdroidC4 rev 2019,11,29 */
57 { MESON_SOC_ID_SM1, 335 * 4, 345 * 4, "c4" },
58 /* OdroidHC4 rev 2020,8,7 */
59 { MESON_SOC_ID_SM1, 590 * 4, 610 * 4, "hc4" },
60};
61
62static void odroid_set_fdtfile(char *soc, char *variant)
63{
64 char s[128];
65
66 snprintf(s, sizeof(s), "amlogic/meson-%s-odroid-%s.dtb", soc, variant);
67 env_set("fdtfile", s);
68}
69
70static int odroid_detect_variant(void)
71{
72 char *variant = "", *soc = "";
73 unsigned int adcval = 0;
74 int ret, i, soc_id = 0;
75
76 if (of_machine_is_compatible("amlogic,sm1")) {
77 soc_id = MESON_SOC_ID_SM1;
78 soc = "sm1";
79 } else if (of_machine_is_compatible("amlogic,g12b")) {
80 soc_id = MESON_SOC_ID_G12B;
81 soc = "g12b";
82 } else {
83 return -1;
84 }
85
86 ret = adc_channel_single_shot("adc@9000", ODROID_HW_VS_ADC_CHANNEL,
87 &adcval);
88 if (ret)
89 return ret;
90
91 for (i = 0 ; i < ARRAY_SIZE(boards) ; ++i) {
92 if (soc_id == boards[i].soc_id &&
93 adcval >= boards[i].adc_min &&
94 adcval < boards[i].adc_max) {
95 variant = boards[i].variant;
96 break;
97 }
98 }
99
100 printf("Board variant: %s\n", variant);
101 env_set("variant", variant);
102
103 odroid_set_fdtfile(soc, variant);
104
105 return 0;
106}
107
Pascal Vizelieeac0372020-06-18 16:40:37 +0200108int misc_init_r(void)
109{
110 u8 mac_addr[MAC_ADDR_LEN];
111 char efuse_mac_addr[EFUSE_MAC_SIZE], tmp[3];
112 ssize_t len;
113
Pascal Vizeli07168542020-11-27 17:28:21 +0100114 if (IS_ENABLED(CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG) &&
115 meson_get_soc_rev(tmp, sizeof(tmp)) > 0)
116 env_set("soc_rev", tmp);
117
Pascal Vizelieeac0372020-06-18 16:40:37 +0200118 if (!eth_env_get_enetaddr("ethaddr", mac_addr)) {
119 len = meson_sm_read_efuse(EFUSE_MAC_OFFSET,
120 efuse_mac_addr, EFUSE_MAC_SIZE);
121 if (len != EFUSE_MAC_SIZE)
122 return 0;
123
124 /* MAC is stored in ASCII format, 1bytes = 2characters */
125 for (int i = 0; i < 6; i++) {
126 tmp[0] = efuse_mac_addr[i * 2];
127 tmp[1] = efuse_mac_addr[i * 2 + 1];
128 tmp[2] = '\0';
Simon Glass3ff49ec2021-07-24 09:03:29 -0600129 mac_addr[i] = hextoul(tmp, NULL);
Pascal Vizelieeac0372020-06-18 16:40:37 +0200130 }
131
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}