misc.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /* SPDX-License-Identifier: GPL-2.0+ */
  2. /*
  3. * RK3399: Architecture common definitions
  4. *
  5. * Copyright (C) 2019 Collabora Inc - https://www.collabora.com/
  6. * Rohan Garg <rohan.garg@collabora.com>
  7. *
  8. * Based on puma-rk3399.c:
  9. * (C) Copyright 2017 Theobroma Systems Design und Consulting GmbH
  10. */
  11. #include <common.h>
  12. #include <env.h>
  13. #include <dm.h>
  14. #include <dm/uclass-internal.h>
  15. #include <misc.h>
  16. #include <u-boot/crc.h>
  17. #include <u-boot/sha256.h>
  18. #include <asm/arch-rockchip/misc.h>
  19. int rockchip_setup_macaddr(void)
  20. {
  21. #if CONFIG_IS_ENABLED(CMD_NET)
  22. int ret;
  23. const char *cpuid = env_get("cpuid#");
  24. u8 hash[SHA256_SUM_LEN];
  25. int size = sizeof(hash);
  26. u8 mac_addr[6];
  27. /* Only generate a MAC address, if none is set in the environment */
  28. if (env_get("ethaddr"))
  29. return -1;
  30. if (!cpuid) {
  31. debug("%s: could not retrieve 'cpuid#'\n", __func__);
  32. return -1;
  33. }
  34. ret = hash_block("sha256", (void *)cpuid, strlen(cpuid), hash, &size);
  35. if (ret) {
  36. debug("%s: failed to calculate SHA256\n", __func__);
  37. return -1;
  38. }
  39. /* Copy 6 bytes of the hash to base the MAC address on */
  40. memcpy(mac_addr, hash, 6);
  41. /* Make this a valid MAC address and set it */
  42. mac_addr[0] &= 0xfe; /* clear multicast bit */
  43. mac_addr[0] |= 0x02; /* set local assignment bit (IEEE802) */
  44. eth_env_set_enetaddr("ethaddr", mac_addr);
  45. #endif
  46. return 0;
  47. }
  48. int rockchip_cpuid_from_efuse(const u32 cpuid_offset,
  49. const u32 cpuid_length,
  50. u8 *cpuid)
  51. {
  52. #if CONFIG_IS_ENABLED(ROCKCHIP_EFUSE) || CONFIG_IS_ENABLED(ROCKCHIP_OTP)
  53. struct udevice *dev;
  54. int ret;
  55. /* retrieve the device */
  56. #if CONFIG_IS_ENABLED(ROCKCHIP_EFUSE)
  57. ret = uclass_get_device_by_driver(UCLASS_MISC,
  58. DM_GET_DRIVER(rockchip_efuse), &dev);
  59. #elif CONFIG_IS_ENABLED(ROCKCHIP_OTP)
  60. ret = uclass_get_device_by_driver(UCLASS_MISC,
  61. DM_GET_DRIVER(rockchip_otp), &dev);
  62. #endif
  63. if (ret) {
  64. debug("%s: could not find efuse device\n", __func__);
  65. return -1;
  66. }
  67. /* read the cpu_id range from the efuses */
  68. ret = misc_read(dev, cpuid_offset, cpuid, cpuid_length);
  69. if (ret) {
  70. debug("%s: reading cpuid from the efuses failed\n",
  71. __func__);
  72. return -1;
  73. }
  74. #endif
  75. return 0;
  76. }
  77. int rockchip_cpuid_set(const u8 *cpuid, const u32 cpuid_length)
  78. {
  79. u8 low[cpuid_length / 2], high[cpuid_length / 2];
  80. char cpuid_str[cpuid_length * 2 + 1];
  81. u64 serialno;
  82. char serialno_str[17];
  83. int i;
  84. memset(cpuid_str, 0, sizeof(cpuid_str));
  85. for (i = 0; i < 16; i++)
  86. sprintf(&cpuid_str[i * 2], "%02x", cpuid[i]);
  87. debug("cpuid: %s\n", cpuid_str);
  88. /*
  89. * Mix the cpuid bytes using the same rules as in
  90. * ${linux}/drivers/soc/rockchip/rockchip-cpuinfo.c
  91. */
  92. for (i = 0; i < 8; i++) {
  93. low[i] = cpuid[1 + (i << 1)];
  94. high[i] = cpuid[i << 1];
  95. }
  96. serialno = crc32_no_comp(0, low, 8);
  97. serialno |= (u64)crc32_no_comp(serialno, high, 8) << 32;
  98. snprintf(serialno_str, sizeof(serialno_str), "%016llx", serialno);
  99. env_set("cpuid#", cpuid_str);
  100. env_set("serial#", serialno_str);
  101. return 0;
  102. }