qemu-ppce500.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright 2007,2009-2014 Freescale Semiconductor, Inc.
  4. * Copyright (C) 2021, Bin Meng <bmeng.cn@gmail.com>
  5. */
  6. #include <common.h>
  7. #include <command.h>
  8. #include <cpu_func.h>
  9. #include <dm.h>
  10. #include <env.h>
  11. #include <init.h>
  12. #include <log.h>
  13. #include <net.h>
  14. #include <pci.h>
  15. #include <time.h>
  16. #include <asm/global_data.h>
  17. #include <asm/processor.h>
  18. #include <asm/mmu.h>
  19. #include <asm/fsl_pci.h>
  20. #include <asm/io.h>
  21. #include <linux/libfdt.h>
  22. #include <fdt_support.h>
  23. #include <netdev.h>
  24. #include <fdtdec.h>
  25. #include <errno.h>
  26. #include <malloc.h>
  27. #include <virtio_types.h>
  28. #include <virtio.h>
  29. DECLARE_GLOBAL_DATA_PTR;
  30. static void *get_fdt_virt(void)
  31. {
  32. if (gd->flags & GD_FLG_RELOC)
  33. return (void *)gd->fdt_blob;
  34. else
  35. return (void *)CONFIG_SYS_TMPVIRT;
  36. }
  37. static uint64_t get_fdt_phys(void)
  38. {
  39. return (uint64_t)(uintptr_t)gd->fdt_blob;
  40. }
  41. static void map_fdt_as(int esel)
  42. {
  43. u32 mas0, mas1, mas2, mas3, mas7;
  44. uint64_t fdt_phys = get_fdt_phys();
  45. unsigned long fdt_phys_tlb = fdt_phys & ~0xffffful;
  46. unsigned long fdt_virt_tlb = (ulong)get_fdt_virt() & ~0xffffful;
  47. mas0 = MAS0_TLBSEL(1) | MAS0_ESEL(esel);
  48. mas1 = MAS1_VALID | MAS1_TID(0) | MAS1_TS | MAS1_TSIZE(BOOKE_PAGESZ_1M);
  49. mas2 = FSL_BOOKE_MAS2(fdt_virt_tlb, 0);
  50. mas3 = FSL_BOOKE_MAS3(fdt_phys_tlb, 0, MAS3_SW|MAS3_SR);
  51. mas7 = FSL_BOOKE_MAS7(fdt_phys_tlb);
  52. write_tlb(mas0, mas1, mas2, mas3, mas7);
  53. }
  54. uint64_t get_phys_ccsrbar_addr_early(void)
  55. {
  56. void *fdt = get_fdt_virt();
  57. uint64_t r;
  58. int size, node;
  59. u32 naddr;
  60. const fdt32_t *prop;
  61. /*
  62. * To be able to read the FDT we need to create a temporary TLB
  63. * map for it.
  64. */
  65. map_fdt_as(10);
  66. node = fdt_path_offset(fdt, "/soc");
  67. naddr = fdt_address_cells(fdt, node);
  68. prop = fdt_getprop(fdt, node, "ranges", &size);
  69. r = fdt_translate_address(fdt, node, prop + naddr);
  70. disable_tlb(10);
  71. return r;
  72. }
  73. int checkboard(void)
  74. {
  75. return 0;
  76. }
  77. static int pci_map_region(phys_addr_t paddr, phys_size_t size, ulong *pmap_addr)
  78. {
  79. ulong map_addr;
  80. if (!pmap_addr)
  81. return 0;
  82. map_addr = *pmap_addr;
  83. /* Align map_addr */
  84. map_addr += size - 1;
  85. map_addr &= ~(size - 1);
  86. if (map_addr + size >= CONFIG_SYS_PCI_MAP_END)
  87. return -1;
  88. /* Map virtual memory for range */
  89. assert(!tlb_map_range(map_addr, paddr, size, TLB_MAP_IO));
  90. *pmap_addr = map_addr + size;
  91. return 0;
  92. }
  93. int misc_init_r(void)
  94. {
  95. struct udevice *dev;
  96. struct pci_region *io;
  97. struct pci_region *mem;
  98. struct pci_region *pre;
  99. ulong map_addr;
  100. int ret;
  101. /* Ensure PCI is probed */
  102. uclass_first_device(UCLASS_PCI, &dev);
  103. pci_get_regions(dev, &io, &mem, &pre);
  104. /* Start MMIO and PIO range maps above RAM */
  105. map_addr = CONFIG_SYS_PCI_MAP_START;
  106. /* Map MMIO range */
  107. ret = pci_map_region(mem->phys_start, mem->size, &map_addr);
  108. if (ret)
  109. return ret;
  110. /* Map PIO range */
  111. ret = pci_map_region(io->phys_start, io->size, &map_addr);
  112. if (ret)
  113. return ret;
  114. /*
  115. * Make sure virtio bus is enumerated so that peripherals
  116. * on the virtio bus can be discovered by their drivers.
  117. */
  118. virtio_init();
  119. /*
  120. * U-Boot is relocated to RAM already, let's delete the temporary FDT
  121. * virtual-physical mapping that was used in the pre-relocation phase.
  122. */
  123. disable_tlb(find_tlb_idx((void *)CONFIG_SYS_TMPVIRT, 1));
  124. return 0;
  125. }
  126. int last_stage_init(void)
  127. {
  128. void *fdt = get_fdt_virt();
  129. int len = 0;
  130. const uint64_t *prop;
  131. int chosen;
  132. chosen = fdt_path_offset(fdt, "/chosen");
  133. if (chosen < 0) {
  134. printf("Couldn't find /chosen node in fdt\n");
  135. return -EIO;
  136. }
  137. /* -kernel boot */
  138. prop = fdt_getprop(fdt, chosen, "qemu,boot-kernel", &len);
  139. if (prop && (len >= 8))
  140. env_set_hex("qemu_kernel_addr", *prop);
  141. return 0;
  142. }
  143. static uint64_t get_linear_ram_size(void)
  144. {
  145. void *fdt = get_fdt_virt();
  146. const void *prop;
  147. int memory;
  148. int len;
  149. memory = fdt_path_offset(fdt, "/memory");
  150. prop = fdt_getprop(fdt, memory, "reg", &len);
  151. if (prop && len >= 16)
  152. return *(uint64_t *)(prop+8);
  153. panic("Couldn't determine RAM size");
  154. }
  155. phys_size_t fsl_ddr_sdram_size(void)
  156. {
  157. return get_linear_ram_size();
  158. }
  159. void init_tlbs(void)
  160. {
  161. phys_size_t ram_size;
  162. /*
  163. * Create a temporary AS=1 map for the fdt
  164. *
  165. * We use ESEL=0 here to overwrite the previous AS=0 map for ourselves
  166. * which was only 4k big. This way we don't have to clear any other maps.
  167. */
  168. map_fdt_as(0);
  169. /* Fetch RAM size from the fdt */
  170. ram_size = get_linear_ram_size();
  171. /* And remove our fdt map again */
  172. disable_tlb(0);
  173. /* Create an internal map of manually created TLB maps */
  174. init_used_tlb_cams();
  175. /* Create a dynamic AS=0 CCSRBAR mapping */
  176. assert(!tlb_map_range(CONFIG_SYS_CCSRBAR, CONFIG_SYS_CCSRBAR_PHYS,
  177. 1024 * 1024, TLB_MAP_IO));
  178. /* Create a RAM map that spans all accessible RAM */
  179. setup_ddr_tlbs(ram_size >> 20);
  180. /* Create a map for the TLB */
  181. assert(!tlb_map_range((ulong)get_fdt_virt(), get_fdt_phys(),
  182. 1024 * 1024, TLB_MAP_RAM));
  183. }
  184. static uint32_t get_cpu_freq(void)
  185. {
  186. void *fdt = get_fdt_virt();
  187. int cpus_node = fdt_path_offset(fdt, "/cpus");
  188. int cpu_node = fdt_first_subnode(fdt, cpus_node);
  189. const char *prop = "clock-frequency";
  190. return fdt_getprop_u32_default_node(fdt, cpu_node, 0, prop, 0);
  191. }
  192. void get_sys_info(sys_info_t *sys_info)
  193. {
  194. int freq = get_cpu_freq();
  195. memset(sys_info, 0, sizeof(sys_info_t));
  196. sys_info->freq_systembus = freq;
  197. sys_info->freq_ddrbus = freq;
  198. sys_info->freq_processor[0] = freq;
  199. }
  200. int get_clocks(void)
  201. {
  202. sys_info_t sys_info;
  203. get_sys_info(&sys_info);
  204. gd->cpu_clk = sys_info.freq_processor[0];
  205. gd->bus_clk = sys_info.freq_systembus;
  206. gd->mem_clk = sys_info.freq_ddrbus;
  207. gd->arch.lbc_clk = sys_info.freq_ddrbus;
  208. return 0;
  209. }
  210. unsigned long get_tbclk(void)
  211. {
  212. void *fdt = get_fdt_virt();
  213. int cpus_node = fdt_path_offset(fdt, "/cpus");
  214. int cpu_node = fdt_first_subnode(fdt, cpus_node);
  215. const char *prop = "timebase-frequency";
  216. return fdt_getprop_u32_default_node(fdt, cpu_node, 0, prop, 0);
  217. }
  218. /********************************************
  219. * get_bus_freq
  220. * return system bus freq in Hz
  221. *********************************************/
  222. ulong get_bus_freq(ulong dummy)
  223. {
  224. sys_info_t sys_info;
  225. get_sys_info(&sys_info);
  226. return sys_info.freq_systembus;
  227. }
  228. /*
  229. * Return the number of cores on this SOC.
  230. */
  231. int cpu_numcores(void)
  232. {
  233. /*
  234. * The QEMU u-boot target only needs to drive the first core,
  235. * spinning and device tree nodes get driven by QEMU itself
  236. */
  237. return 1;
  238. }
  239. /*
  240. * Return a 32-bit mask indicating which cores are present on this SOC.
  241. */
  242. u32 cpu_mask(void)
  243. {
  244. return (1 << cpu_numcores()) - 1;
  245. }
  246. /**
  247. * Return the virtual address of FDT that was passed by QEMU
  248. *
  249. * @return virtual address of FDT received from QEMU in r3 register
  250. */
  251. void *board_fdt_blob_setup(void)
  252. {
  253. return get_fdt_virt();
  254. }
  255. /* See CONFIG_SYS_NS16550_CLK in arch/powerpc/include/asm/config.h */
  256. int get_serial_clock(void)
  257. {
  258. return get_bus_freq(0);
  259. }