board-common.c 3.2 KB

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