board-common.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2016 Beniamino Galvani <b.galvani@gmail.com>
  4. */
  5. #include <common.h>
  6. #include <cpu_func.h>
  7. #include <fastboot.h>
  8. #include <init.h>
  9. #include <net.h>
  10. #include <asm/arch/boot.h>
  11. #include <env.h>
  12. #include <asm/cache.h>
  13. #include <asm/ptrace.h>
  14. #include <linux/libfdt.h>
  15. #include <linux/err.h>
  16. #include <asm/arch/mem.h>
  17. #include <asm/arch/sm.h>
  18. #include <asm/armv8/mmu.h>
  19. #include <asm/unaligned.h>
  20. #include <efi_loader.h>
  21. #include <u-boot/crc.h>
  22. #if CONFIG_IS_ENABLED(FASTBOOT)
  23. #include <asm/psci.h>
  24. #include <fastboot.h>
  25. #endif
  26. DECLARE_GLOBAL_DATA_PTR;
  27. __weak int board_init(void)
  28. {
  29. return 0;
  30. }
  31. int dram_init(void)
  32. {
  33. const fdt64_t *val;
  34. int offset;
  35. int len;
  36. offset = fdt_path_offset(gd->fdt_blob, "/memory");
  37. if (offset < 0)
  38. return -EINVAL;
  39. val = fdt_getprop(gd->fdt_blob, offset, "reg", &len);
  40. if (len < sizeof(*val) * 2)
  41. return -EINVAL;
  42. /* Use unaligned access since cache is still disabled */
  43. gd->ram_size = get_unaligned_be64(&val[1]);
  44. return 0;
  45. }
  46. __weak int meson_ft_board_setup(void *blob, struct bd_info *bd)
  47. {
  48. return 0;
  49. }
  50. int ft_board_setup(void *blob, struct bd_info *bd)
  51. {
  52. meson_init_reserved_memory(blob);
  53. return meson_ft_board_setup(blob, bd);
  54. }
  55. void meson_board_add_reserved_memory(void *fdt, u64 start, u64 size)
  56. {
  57. int ret;
  58. ret = fdt_add_mem_rsv(fdt, start, size);
  59. if (ret)
  60. printf("Could not reserve zone @ 0x%llx\n", start);
  61. if (IS_ENABLED(CONFIG_EFI_LOADER))
  62. efi_add_memory_map(start, size, EFI_RESERVED_MEMORY_TYPE);
  63. }
  64. int meson_generate_serial_ethaddr(void)
  65. {
  66. u8 mac_addr[ARP_HLEN];
  67. char serial[SM_SERIAL_SIZE];
  68. u32 sid;
  69. u16 sid16;
  70. if (!meson_sm_get_serial(serial, SM_SERIAL_SIZE)) {
  71. sid = crc32(0, (unsigned char *)serial, SM_SERIAL_SIZE);
  72. sid16 = crc16_ccitt(0, (unsigned char *)serial, SM_SERIAL_SIZE);
  73. /* Ensure the NIC specific bytes of the mac are not all 0 */
  74. if ((sid & 0xffffff) == 0)
  75. sid |= 0x800000;
  76. /* Non OUI / registered MAC address */
  77. mac_addr[0] = ((sid16 >> 8) & 0xfc) | 0x02;
  78. mac_addr[1] = (sid16 >> 0) & 0xff;
  79. mac_addr[2] = (sid >> 24) & 0xff;
  80. mac_addr[3] = (sid >> 16) & 0xff;
  81. mac_addr[4] = (sid >> 8) & 0xff;
  82. mac_addr[5] = (sid >> 0) & 0xff;
  83. eth_env_set_enetaddr("ethaddr", mac_addr);
  84. } else
  85. return -EINVAL;
  86. return 0;
  87. }
  88. static void meson_set_boot_source(void)
  89. {
  90. const char *source;
  91. switch (meson_get_boot_device()) {
  92. case BOOT_DEVICE_EMMC:
  93. source = "emmc";
  94. break;
  95. case BOOT_DEVICE_NAND:
  96. source = "nand";
  97. break;
  98. case BOOT_DEVICE_SPI:
  99. source = "spi";
  100. break;
  101. case BOOT_DEVICE_SD:
  102. source = "sd";
  103. break;
  104. case BOOT_DEVICE_USB:
  105. source = "usb";
  106. break;
  107. default:
  108. source = "unknown";
  109. }
  110. env_set("boot_source", source);
  111. }
  112. __weak int meson_board_late_init(void)
  113. {
  114. return 0;
  115. }
  116. int board_late_init(void)
  117. {
  118. meson_set_boot_source();
  119. return meson_board_late_init();
  120. }
  121. #if CONFIG_IS_ENABLED(FASTBOOT)
  122. static unsigned int reboot_reason = REBOOT_REASON_NORMAL;
  123. int fastboot_set_reboot_flag(enum fastboot_reboot_reason reason)
  124. {
  125. if (reason != FASTBOOT_REBOOT_REASON_BOOTLOADER)
  126. return -ENOTSUPP;
  127. reboot_reason = REBOOT_REASON_BOOTLOADER;
  128. printf("Using reboot reason: 0x%x\n", reboot_reason);
  129. return 0;
  130. }
  131. void reset_cpu(ulong addr)
  132. {
  133. struct pt_regs regs;
  134. regs.regs[0] = ARM_PSCI_0_2_FN_SYSTEM_RESET;
  135. regs.regs[1] = reboot_reason;
  136. printf("Rebooting with reason: 0x%lx\n", regs.regs[1]);
  137. smc_call(&regs);
  138. while (1)
  139. ;
  140. }
  141. #else
  142. void reset_cpu(ulong addr)
  143. {
  144. psci_system_reset();
  145. }
  146. #endif