unleashed.c 2.7 KB

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