ns3.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright 2020 Broadcom.
  4. *
  5. */
  6. #include <common.h>
  7. #include <fdt_support.h>
  8. #include <asm/io.h>
  9. #include <asm/gic-v3.h>
  10. #include <asm/global_data.h>
  11. #include <asm/system.h>
  12. #include <asm/armv8/mmu.h>
  13. #include <asm/arch-bcmns3/bl33_info.h>
  14. #include <dt-bindings/memory/bcm-ns3-mc.h>
  15. #include <broadcom/chimp.h>
  16. #define BANK_OFFSET(bank) ((u64)BCM_NS3_DDR_INFO_BASE + 8 + ((bank) * 16))
  17. /*
  18. * ns3_dram_bank - DDR bank details
  19. *
  20. * @start: DDR bank start address
  21. * @len: DDR bank length
  22. */
  23. struct ns3_dram_bank {
  24. u64 start[BCM_NS3_MAX_NR_BANKS];
  25. u64 len[BCM_NS3_MAX_NR_BANKS];
  26. };
  27. /*
  28. * ns3_dram_hdr - DDR header info
  29. *
  30. * @sig: DDR info signature
  31. * @bank: DDR bank details
  32. */
  33. struct ns3_dram_hdr {
  34. u32 sig;
  35. struct ns3_dram_bank bank;
  36. };
  37. static struct mm_region ns3_mem_map[] = {
  38. {
  39. .virt = 0x0UL,
  40. .phys = 0x0UL,
  41. .size = 0x80000000UL,
  42. .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
  43. PTE_BLOCK_NON_SHARE |
  44. PTE_BLOCK_PXN | PTE_BLOCK_UXN
  45. }, {
  46. .virt = BCM_NS3_MEM_START,
  47. .phys = BCM_NS3_MEM_START,
  48. .size = BCM_NS3_MEM_LEN,
  49. .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) |
  50. PTE_BLOCK_INNER_SHARE
  51. }, {
  52. .virt = BCM_NS3_BANK_1_MEM_START,
  53. .phys = BCM_NS3_BANK_1_MEM_START,
  54. .size = BCM_NS3_BANK_1_MEM_LEN,
  55. .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) |
  56. PTE_BLOCK_INNER_SHARE
  57. }, {
  58. /* List terminator */
  59. 0,
  60. }
  61. };
  62. struct mm_region *mem_map = ns3_mem_map;
  63. DECLARE_GLOBAL_DATA_PTR;
  64. /*
  65. * Force the bl33_info to the data-section, as .bss will not be valid
  66. * when save_boot_params is invoked.
  67. */
  68. struct bl33_info *bl33_info __section(".data");
  69. /*
  70. * Run modulo 256 checksum calculation and return the calculated checksum
  71. */
  72. static u8 checksum_calc(u8 *p, unsigned int len)
  73. {
  74. unsigned int i;
  75. u8 chksum = 0;
  76. for (i = 0; i < len; i++)
  77. chksum += p[i];
  78. return chksum;
  79. }
  80. /*
  81. * This function parses the memory layout information from a reserved area in
  82. * DDR, and then fix up the FDT before passing it to Linux.
  83. *
  84. * In the case of error, do nothing and the default memory layout in DT will
  85. * be used
  86. */
  87. static int mem_info_parse_fixup(void *fdt)
  88. {
  89. struct ns3_dram_hdr hdr;
  90. u32 *p32, i, nr_banks;
  91. u64 *p64;
  92. /* validate signature */
  93. p32 = (u32 *)BCM_NS3_DDR_INFO_BASE;
  94. hdr.sig = *p32;
  95. if (hdr.sig != BCM_NS3_DDR_INFO_SIG) {
  96. printf("DDR info signature 0x%x invalid\n", hdr.sig);
  97. return -EINVAL;
  98. }
  99. /* run checksum test to validate data */
  100. if (checksum_calc((u8 *)p32, BCM_NS3_DDR_INFO_LEN) != 0) {
  101. printf("Checksum on DDR info failed\n");
  102. return -EINVAL;
  103. }
  104. /* parse information for each bank */
  105. nr_banks = 0;
  106. for (i = 0; i < BCM_NS3_MAX_NR_BANKS; i++) {
  107. /* skip banks with a length of zero */
  108. p64 = (u64 *)BANK_OFFSET(i);
  109. if (*(p64 + 1) == 0)
  110. continue;
  111. hdr.bank.start[i] = *p64;
  112. hdr.bank.len[i] = *(p64 + 1);
  113. printf("mem[%u] 0x%llx - 0x%llx\n", i, hdr.bank.start[i],
  114. hdr.bank.start[i] + hdr.bank.len[i] - 1);
  115. nr_banks++;
  116. }
  117. if (!nr_banks) {
  118. printf("No DDR banks detected\n");
  119. return -ENOMEM;
  120. }
  121. return fdt_fixup_memory_banks(fdt, hdr.bank.start, hdr.bank.len,
  122. nr_banks);
  123. }
  124. int board_init(void)
  125. {
  126. /* Setup memory using "memory" node from DTB */
  127. if (fdtdec_setup_mem_size_base() != 0)
  128. return -EINVAL;
  129. fdtdec_setup_memory_banksize();
  130. if (bl33_info->version != BL33_INFO_VERSION)
  131. printf("*** warning: ATF BL31 and U-Boot not in sync! ***\n");
  132. #if IS_ENABLED(CONFIG_BNXT_ETH)
  133. if (chimp_fastboot_optee() != 0)
  134. printf("*** warning: secure chimp fastboot failed! ***\n");
  135. #endif
  136. return 0;
  137. }
  138. int board_late_init(void)
  139. {
  140. return 0;
  141. }
  142. int dram_init(void)
  143. {
  144. /*
  145. * Mark ram base as the last 16MB of 2GB DDR, which is 0xFF00_0000.
  146. * So that relocation happens with in the last 16MB memory.
  147. */
  148. gd->ram_base = (phys_size_t)(BCM_NS3_MEM_END - SZ_16M);
  149. gd->ram_size = (unsigned long)SZ_16M;
  150. return 0;
  151. }
  152. int dram_init_banksize(void)
  153. {
  154. gd->bd->bi_dram[0].start = (BCM_NS3_MEM_END - SZ_16M);
  155. gd->bd->bi_dram[0].size = SZ_16M;
  156. return 0;
  157. }
  158. /* Limit RAM used by U-Boot to the DDR first bank End region */
  159. phys_addr_t board_get_usable_ram_top(phys_size_t total_size)
  160. {
  161. return BCM_NS3_MEM_END;
  162. }
  163. void reset_cpu(void)
  164. {
  165. /* Perform a level 3 reset */
  166. psci_system_reset2(3, 0);
  167. }
  168. #ifdef CONFIG_OF_BOARD_SETUP
  169. int ft_board_setup(void *fdt, struct bd_info *bd)
  170. {
  171. u32 chimp_hs = CHIMP_HANDSHAKE_WAIT_TIMEOUT;
  172. /* FIXME: Need to call gic_lpi_tables_init correctly now */
  173. printf("%s: failed to init gic-lpi-tables\n", __func__);
  174. /*
  175. * Check for chimp handshake status.
  176. * Zero timeout value will actually fall to default timeout.
  177. *
  178. * System boot is independent of chimp handshake.
  179. * chimp handshake failure is not a catastrophic error.
  180. * Hence continue booting if chimp handshake fails.
  181. */
  182. chimp_handshake_status_optee(0, &chimp_hs);
  183. if (chimp_hs == CHIMP_HANDSHAKE_SUCCESS)
  184. printf("ChiMP handshake successful\n");
  185. else
  186. printf("ERROR: ChiMP handshake status 0x%x\n", chimp_hs);
  187. return mem_info_parse_fixup(fdt);
  188. }
  189. #endif /* CONFIG_OF_BOARD_SETUP */