odroid-n2.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2020 BayLibre, SAS
  4. * Author: Neil Armstrong <narmstrong@baylibre.com>
  5. */
  6. #include <common.h>
  7. #include <dm.h>
  8. #include <adc.h>
  9. #include <env.h>
  10. #include <init.h>
  11. #include <net.h>
  12. #include <asm/io.h>
  13. #include <asm/arch/boot.h>
  14. #include <asm/arch/sm.h>
  15. #include <asm/arch/eth.h>
  16. #include <asm/arch/boot.h>
  17. #define EFUSE_MAC_OFFSET 20
  18. #define EFUSE_MAC_SIZE 12
  19. #define MAC_ADDR_LEN 6
  20. #define ODROID_HW_VS_ADC_CHANNEL 1
  21. #define MESON_SOC_ID_G12B 0x29
  22. #define MESON_SOC_ID_SM1 0x2b
  23. int mmc_get_env_dev(void)
  24. {
  25. if (meson_get_boot_device() == BOOT_DEVICE_EMMC)
  26. return 1;
  27. return 0;
  28. }
  29. /* Variant detection is based on the ADC RAW values for the channel #1 */
  30. static struct meson_odroid_boards {
  31. unsigned int soc_id;
  32. unsigned int adc_min;
  33. unsigned int adc_max;
  34. char *variant;
  35. } boards[] = {
  36. /* OdroidN2 rev 2018,7,23 */
  37. { MESON_SOC_ID_G12B, 80 * 4, 90 * 4, "n2" },
  38. /* OdroidN2 rev 2018,12,6 */
  39. { MESON_SOC_ID_G12B, 160 * 4, 170 * 4, "n2" },
  40. /* OdroidN2 rev 2019,1,17 */
  41. { MESON_SOC_ID_G12B, 245 * 4, 255 * 4, "n2" },
  42. /* OdroidN2 rev 2019,2,7 */
  43. { MESON_SOC_ID_G12B, 330 * 4, 350 * 4, "n2" },
  44. /* OdroidN2plus rev 2019,11,20 */
  45. { MESON_SOC_ID_G12B, 410 * 4, 430 * 4, "n2_plus" },
  46. /* OdroidC4 rev 2020,01,29 */
  47. { MESON_SOC_ID_SM1, 80 * 4, 100 * 4, "c4" },
  48. /* OdroidHC4 rev 2019,12,10 */
  49. { MESON_SOC_ID_SM1, 300 * 4, 320 * 4, "hc4" },
  50. /* OdroidC4 rev 2019,11,29 */
  51. { MESON_SOC_ID_SM1, 335 * 4, 345 * 4, "c4" },
  52. /* OdroidHC4 rev 2020,8,7 */
  53. { MESON_SOC_ID_SM1, 590 * 4, 610 * 4, "hc4" },
  54. };
  55. static void odroid_set_fdtfile(char *soc, char *variant)
  56. {
  57. char s[128];
  58. snprintf(s, sizeof(s), "amlogic/meson-%s-odroid-%s.dtb", soc, variant);
  59. env_set("fdtfile", s);
  60. }
  61. static int odroid_detect_variant(void)
  62. {
  63. char *variant = "", *soc = "";
  64. unsigned int adcval = 0;
  65. int ret, i, soc_id = 0;
  66. if (of_machine_is_compatible("amlogic,sm1")) {
  67. soc_id = MESON_SOC_ID_SM1;
  68. soc = "sm1";
  69. } else if (of_machine_is_compatible("amlogic,g12b")) {
  70. soc_id = MESON_SOC_ID_G12B;
  71. soc = "g12b";
  72. } else {
  73. return -1;
  74. }
  75. ret = adc_channel_single_shot("adc@9000", ODROID_HW_VS_ADC_CHANNEL,
  76. &adcval);
  77. if (ret)
  78. return ret;
  79. for (i = 0 ; i < ARRAY_SIZE(boards) ; ++i) {
  80. if (soc_id == boards[i].soc_id &&
  81. adcval >= boards[i].adc_min &&
  82. adcval < boards[i].adc_max) {
  83. variant = boards[i].variant;
  84. break;
  85. }
  86. }
  87. printf("Board variant: %s\n", variant);
  88. env_set("variant", variant);
  89. odroid_set_fdtfile(soc, variant);
  90. return 0;
  91. }
  92. int misc_init_r(void)
  93. {
  94. u8 mac_addr[MAC_ADDR_LEN];
  95. char efuse_mac_addr[EFUSE_MAC_SIZE], tmp[3];
  96. ssize_t len;
  97. if (IS_ENABLED(CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG) &&
  98. meson_get_soc_rev(tmp, sizeof(tmp)) > 0)
  99. env_set("soc_rev", tmp);
  100. if (!eth_env_get_enetaddr("ethaddr", mac_addr)) {
  101. len = meson_sm_read_efuse(EFUSE_MAC_OFFSET,
  102. efuse_mac_addr, EFUSE_MAC_SIZE);
  103. if (len != EFUSE_MAC_SIZE)
  104. return 0;
  105. /* MAC is stored in ASCII format, 1bytes = 2characters */
  106. for (int i = 0; i < 6; i++) {
  107. tmp[0] = efuse_mac_addr[i * 2];
  108. tmp[1] = efuse_mac_addr[i * 2 + 1];
  109. tmp[2] = '\0';
  110. mac_addr[i] = hextoul(tmp, NULL);
  111. }
  112. if (is_valid_ethaddr(mac_addr))
  113. eth_env_set_enetaddr("ethaddr", mac_addr);
  114. else
  115. meson_generate_serial_ethaddr();
  116. }
  117. odroid_detect_variant();
  118. return 0;
  119. }