qemu-ppce500.c 7.5 KB

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