fu540.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (c) 2019 Western Digital Corporation or its affiliates.
  4. *
  5. * Authors:
  6. * Anup Patel <anup.patel@wdc.com>
  7. */
  8. #include <dm.h>
  9. #include <env.h>
  10. #include <init.h>
  11. #include <log.h>
  12. #include <linux/bitops.h>
  13. #include <linux/bug.h>
  14. #include <linux/delay.h>
  15. #include <misc.h>
  16. #include <spl.h>
  17. #include <asm/arch/cache.h>
  18. /*
  19. * This define is a value used for error/unknown serial.
  20. * If we really care about distinguishing errors and 0 is
  21. * valid, we'll need a different one.
  22. */
  23. #define ERROR_READING_SERIAL_NUMBER 0
  24. #ifdef CONFIG_MISC_INIT_R
  25. #if CONFIG_IS_ENABLED(SIFIVE_OTP)
  26. static u32 otp_read_serialnum(struct udevice *dev)
  27. {
  28. int ret;
  29. u32 serial[2] = {0};
  30. for (int i = 0xfe * 4; i > 0; i -= 8) {
  31. ret = misc_read(dev, i, serial, sizeof(serial));
  32. if (ret != sizeof(serial)) {
  33. printf("%s: error reading serial from OTP\n", __func__);
  34. break;
  35. }
  36. if (serial[0] == ~serial[1])
  37. return serial[0];
  38. }
  39. return ERROR_READING_SERIAL_NUMBER;
  40. }
  41. #endif
  42. static u32 fu540_read_serialnum(void)
  43. {
  44. u32 serial = ERROR_READING_SERIAL_NUMBER;
  45. #if CONFIG_IS_ENABLED(SIFIVE_OTP)
  46. struct udevice *dev;
  47. int ret;
  48. /* init OTP */
  49. ret = uclass_get_device_by_driver(UCLASS_MISC,
  50. DM_DRIVER_GET(sifive_otp), &dev);
  51. if (ret) {
  52. debug("%s: could not find otp device\n", __func__);
  53. return serial;
  54. }
  55. /* read serial from OTP and set env var */
  56. serial = otp_read_serialnum(dev);
  57. #endif
  58. return serial;
  59. }
  60. static void fu540_setup_macaddr(u32 serialnum)
  61. {
  62. /* Default MAC address */
  63. unsigned char mac[6] = { 0x70, 0xb3, 0xd5, 0x92, 0xf0, 0x00 };
  64. /*
  65. * We derive our board MAC address by ORing last three bytes
  66. * of board serial number to above default MAC address.
  67. *
  68. * This logic of deriving board MAC address is taken from
  69. * SiFive FSBL and is kept unchanged.
  70. */
  71. mac[5] |= (serialnum >> 0) & 0xff;
  72. mac[4] |= (serialnum >> 8) & 0xff;
  73. mac[3] |= (serialnum >> 16) & 0xff;
  74. /* Update environment variable */
  75. eth_env_set_enetaddr("ethaddr", mac);
  76. }
  77. int misc_init_r(void)
  78. {
  79. u32 serial_num;
  80. char buf[9] = {0};
  81. /* Set ethaddr environment variable from board serial number */
  82. if (!env_get("serial#")) {
  83. serial_num = fu540_read_serialnum();
  84. if (!serial_num) {
  85. WARN(true, "Board serial number should not be 0 !!\n");
  86. return 0;
  87. }
  88. snprintf(buf, sizeof(buf), "%08x", serial_num);
  89. env_set("serial#", buf);
  90. fu540_setup_macaddr(serial_num);
  91. }
  92. return 0;
  93. }
  94. #endif
  95. int board_init(void)
  96. {
  97. int ret;
  98. /* enable all cache ways */
  99. ret = cache_enable_ways();
  100. if (ret) {
  101. debug("%s: could not enable cache ways\n", __func__);
  102. return ret;
  103. }
  104. return 0;
  105. }