1// 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> 9#include <adc.h> 10#include <env.h> 11#include <init.h> 12#include <net.h> 13#include <asm/io.h> 14#include <asm/arch/boot.h> 15#include <asm/arch/sm.h> 16#include <asm/arch/eth.h> 17#include <asm/arch/boot.h> 18 19#define EFUSE_MAC_OFFSET 20 20#define EFUSE_MAC_SIZE 12 21#define MAC_ADDR_LEN 6 22 23#define ODROID_HW_VS_ADC_CHANNEL 1 24 25#define MESON_SOC_ID_G12B 0x29 26#define MESON_SOC_ID_SM1 0x2b 27 28int mmc_get_env_dev(void) 29{ 30 if (meson_get_boot_device() == BOOT_DEVICE_EMMC) 31 return 1; 32 return 0; 33} 34 35/* 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 */ 51 { MESON_SOC_ID_G12B, 410 * 4, 430 * 4, "n2-plus" }, 52 /* 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 108int misc_init_r(void) 109{ 110 u8 mac_addr[MAC_ADDR_LEN + 1]; 111 char efuse_mac_addr[EFUSE_MAC_SIZE], tmp[3]; 112 ssize_t len; 113 114 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 118 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'; 129 mac_addr[i] = hextoul(tmp, NULL); 130 } 131 mac_addr[MAC_ADDR_LEN] = '\0'; 132 133 if (is_valid_ethaddr(mac_addr)) 134 eth_env_set_enetaddr("ethaddr", mac_addr); 135 else 136 meson_generate_serial_ethaddr(); 137 } 138 139 odroid_detect_variant(); 140 return 0; 141} 142