Pascal Vizeli | eeac037 | 2020-06-18 16:40:37 +0200 | [diff] [blame] | 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> |
Marek Szyprowski | 85c7564 | 2021-02-04 10:11:45 +0100 | [diff] [blame] | 9 | #include <adc.h> |
Pascal Vizeli | eeac037 | 2020-06-18 16:40:37 +0200 | [diff] [blame] | 10 | #include <env.h> |
| 11 | #include <init.h> |
| 12 | #include <net.h> |
| 13 | #include <asm/io.h> |
Marek Szyprowski | b3e9712 | 2020-12-18 15:26:45 +0100 | [diff] [blame] | 14 | #include <asm/arch/boot.h> |
Pascal Vizeli | eeac037 | 2020-06-18 16:40:37 +0200 | [diff] [blame] | 15 | #include <asm/arch/sm.h> |
| 16 | #include <asm/arch/eth.h> |
Pascal Vizeli | 0716854 | 2020-11-27 17:28:21 +0100 | [diff] [blame] | 17 | #include <asm/arch/boot.h> |
Pascal Vizeli | eeac037 | 2020-06-18 16:40:37 +0200 | [diff] [blame] | 18 | |
| 19 | #define EFUSE_MAC_OFFSET 20 |
| 20 | #define EFUSE_MAC_SIZE 12 |
| 21 | #define MAC_ADDR_LEN 6 |
| 22 | |
Marek Szyprowski | 85c7564 | 2021-02-04 10:11:45 +0100 | [diff] [blame] | 23 | #define ODROID_HW_VS_ADC_CHANNEL 1 |
| 24 | |
| 25 | #define MESON_SOC_ID_G12B 0x29 |
| 26 | #define MESON_SOC_ID_SM1 0x2b |
| 27 | |
Marek Szyprowski | b3e9712 | 2020-12-18 15:26:45 +0100 | [diff] [blame] | 28 | int mmc_get_env_dev(void) |
| 29 | { |
| 30 | if (meson_get_boot_device() == BOOT_DEVICE_EMMC) |
| 31 | return 1; |
| 32 | return 0; |
| 33 | } |
| 34 | |
Marek Szyprowski | 85c7564 | 2021-02-04 10:11:45 +0100 | [diff] [blame] | 35 | /* Variant detection is based on the ADC RAW values for the channel #1 */ |
| 36 | static 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 Hewitt | c1bbc1a | 2021-08-04 11:01:07 +0000 | [diff] [blame] | 51 | { MESON_SOC_ID_G12B, 410 * 4, 430 * 4, "n2-plus" }, |
Marek Szyprowski | 85c7564 | 2021-02-04 10:11:45 +0100 | [diff] [blame] | 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 | |
| 62 | static 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 | |
| 70 | static 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 Vizeli | eeac037 | 2020-06-18 16:40:37 +0200 | [diff] [blame] | 108 | int misc_init_r(void) |
| 109 | { |
Neil Armstrong | bfbe81a | 2024-03-20 09:46:11 +0100 | [diff] [blame^] | 110 | u8 mac_addr[MAC_ADDR_LEN + 1]; |
Pascal Vizeli | eeac037 | 2020-06-18 16:40:37 +0200 | [diff] [blame] | 111 | char efuse_mac_addr[EFUSE_MAC_SIZE], tmp[3]; |
| 112 | ssize_t len; |
| 113 | |
Pascal Vizeli | 0716854 | 2020-11-27 17:28:21 +0100 | [diff] [blame] | 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 | |
Pascal Vizeli | eeac037 | 2020-06-18 16:40:37 +0200 | [diff] [blame] | 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'; |
Simon Glass | 3ff49ec | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 129 | mac_addr[i] = hextoul(tmp, NULL); |
Pascal Vizeli | eeac037 | 2020-06-18 16:40:37 +0200 | [diff] [blame] | 130 | } |
Neil Armstrong | bfbe81a | 2024-03-20 09:46:11 +0100 | [diff] [blame^] | 131 | mac_addr[MAC_ADDR_LEN] = '\0'; |
Pascal Vizeli | eeac037 | 2020-06-18 16:40:37 +0200 | [diff] [blame] | 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 | |
Marek Szyprowski | 85c7564 | 2021-02-04 10:11:45 +0100 | [diff] [blame] | 139 | odroid_detect_variant(); |
Pascal Vizeli | eeac037 | 2020-06-18 16:40:37 +0200 | [diff] [blame] | 140 | return 0; |
| 141 | } |