board-common.c 3.3 KB

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