fu540.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 <linux/io.h>
  16. #include <misc.h>
  17. #include <spl.h>
  18. #include <asm/arch/cache.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_GET_DRIVER(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. int board_init(void)
  97. {
  98. int ret;
  99. /* enable all cache ways */
  100. ret = cache_enable_ways();
  101. if (ret) {
  102. debug("%s: could not enable cache ways\n", __func__);
  103. return ret;
  104. }
  105. return 0;
  106. }
  107. #ifdef CONFIG_SPL
  108. #define MODE_SELECT_REG 0x1000
  109. #define MODE_SELECT_QSPI 0x6
  110. #define MODE_SELECT_SD 0xb
  111. #define MODE_SELECT_MASK GENMASK(3, 0)
  112. u32 spl_boot_device(void)
  113. {
  114. u32 mode_select = readl((void *)MODE_SELECT_REG);
  115. u32 boot_device = mode_select & MODE_SELECT_MASK;
  116. switch (boot_device) {
  117. case MODE_SELECT_QSPI:
  118. return BOOT_DEVICE_SPI;
  119. case MODE_SELECT_SD:
  120. return BOOT_DEVICE_MMC1;
  121. default:
  122. debug("Unsupported boot device 0x%x but trying MMC1\n",
  123. boot_device);
  124. return BOOT_DEVICE_MMC1;
  125. }
  126. }
  127. #endif
  128. #ifdef CONFIG_SPL_LOAD_FIT
  129. int board_fit_config_name_match(const char *name)
  130. {
  131. /* boot using first FIT config */
  132. return 0;
  133. }
  134. #endif