board-common.c 3.3 KB

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