board-common.c 3.5 KB

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