qemu-ppce500.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright 2007,2009-2014 Freescale Semiconductor, Inc.
  4. */
  5. #include <common.h>
  6. #include <command.h>
  7. #include <pci.h>
  8. #include <asm/processor.h>
  9. #include <asm/mmu.h>
  10. #include <asm/fsl_pci.h>
  11. #include <asm/io.h>
  12. #include <linux/libfdt.h>
  13. #include <fdt_support.h>
  14. #include <netdev.h>
  15. #include <fdtdec.h>
  16. #include <errno.h>
  17. #include <malloc.h>
  18. DECLARE_GLOBAL_DATA_PTR;
  19. static void *get_fdt_virt(void)
  20. {
  21. return (void *)CONFIG_SYS_TMPVIRT;
  22. }
  23. static uint64_t get_fdt_phys(void)
  24. {
  25. return (uint64_t)(uintptr_t)gd->fdt_blob;
  26. }
  27. static void map_fdt_as(int esel)
  28. {
  29. u32 mas0, mas1, mas2, mas3, mas7;
  30. uint64_t fdt_phys = get_fdt_phys();
  31. unsigned long fdt_phys_tlb = fdt_phys & ~0xffffful;
  32. unsigned long fdt_virt_tlb = (ulong)get_fdt_virt() & ~0xffffful;
  33. mas0 = MAS0_TLBSEL(1) | MAS0_ESEL(esel);
  34. mas1 = MAS1_VALID | MAS1_TID(0) | MAS1_TS | MAS1_TSIZE(BOOKE_PAGESZ_1M);
  35. mas2 = FSL_BOOKE_MAS2(fdt_virt_tlb, 0);
  36. mas3 = FSL_BOOKE_MAS3(fdt_phys_tlb, 0, MAS3_SW|MAS3_SR);
  37. mas7 = FSL_BOOKE_MAS7(fdt_phys_tlb);
  38. write_tlb(mas0, mas1, mas2, mas3, mas7);
  39. }
  40. uint64_t get_phys_ccsrbar_addr_early(void)
  41. {
  42. void *fdt = get_fdt_virt();
  43. uint64_t r;
  44. int size, node;
  45. u32 naddr;
  46. const fdt32_t *prop;
  47. /*
  48. * To be able to read the FDT we need to create a temporary TLB
  49. * map for it.
  50. */
  51. map_fdt_as(10);
  52. node = fdt_path_offset(fdt, "/soc");
  53. naddr = fdt_address_cells(fdt, node);
  54. prop = fdt_getprop(fdt, node, "ranges", &size);
  55. r = fdt_translate_address(fdt, node, prop + naddr);
  56. disable_tlb(10);
  57. return r;
  58. }
  59. int board_early_init_f(void)
  60. {
  61. return 0;
  62. }
  63. int checkboard(void)
  64. {
  65. return 0;
  66. }
  67. static int pci_map_region(void *fdt, int pci_node, int range_id,
  68. phys_size_t *ppaddr, pci_addr_t *pvaddr,
  69. pci_size_t *psize, ulong *pmap_addr)
  70. {
  71. uint64_t addr;
  72. uint64_t size;
  73. ulong map_addr;
  74. int r;
  75. r = fdt_read_range(fdt, pci_node, range_id, NULL, &addr, &size);
  76. if (r)
  77. return r;
  78. if (ppaddr)
  79. *ppaddr = addr;
  80. if (psize)
  81. *psize = size;
  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, addr, size, TLB_MAP_IO));
  92. *pmap_addr = map_addr + size;
  93. if (pvaddr)
  94. *pvaddr = map_addr;
  95. return 0;
  96. }
  97. void pci_init_board(void)
  98. {
  99. struct pci_controller *pci_hoses;
  100. void *fdt = get_fdt_virt();
  101. int pci_node = -1;
  102. int pci_num = 0;
  103. int pci_count = 0;
  104. ulong map_addr;
  105. puts("\n");
  106. /* Start MMIO and PIO range maps above RAM */
  107. map_addr = CONFIG_SYS_PCI_MAP_START;
  108. /* Count and allocate PCI buses */
  109. pci_node = fdt_node_offset_by_prop_value(fdt, pci_node,
  110. "device_type", "pci", 4);
  111. while (pci_node != -FDT_ERR_NOTFOUND) {
  112. pci_node = fdt_node_offset_by_prop_value(fdt, pci_node,
  113. "device_type", "pci", 4);
  114. pci_count++;
  115. }
  116. if (pci_count) {
  117. pci_hoses = malloc(sizeof(struct pci_controller) * pci_count);
  118. } else {
  119. printf("PCI: disabled\n\n");
  120. return;
  121. }
  122. /* Spawn PCI buses based on device tree */
  123. pci_node = fdt_node_offset_by_prop_value(fdt, pci_node,
  124. "device_type", "pci", 4);
  125. while (pci_node != -FDT_ERR_NOTFOUND) {
  126. struct fsl_pci_info pci_info = { };
  127. const fdt32_t *reg;
  128. int r;
  129. reg = fdt_getprop(fdt, pci_node, "reg", NULL);
  130. pci_info.regs = fdt_translate_address(fdt, pci_node, reg);
  131. /* Map MMIO range */
  132. r = pci_map_region(fdt, pci_node, 0, &pci_info.mem_phys, NULL,
  133. &pci_info.mem_size, &map_addr);
  134. if (r)
  135. break;
  136. /* Map PIO range */
  137. r = pci_map_region(fdt, pci_node, 1, &pci_info.io_phys, NULL,
  138. &pci_info.io_size, &map_addr);
  139. if (r)
  140. break;
  141. /*
  142. * The PCI framework finds virtual addresses for the buses
  143. * through our address map, so tell it the physical addresses.
  144. */
  145. pci_info.mem_bus = pci_info.mem_phys;
  146. pci_info.io_bus = pci_info.io_phys;
  147. /* Instantiate */
  148. pci_info.pci_num = pci_num + 1;
  149. fsl_setup_hose(&pci_hoses[pci_num], pci_info.regs);
  150. printf("PCI: base address %lx\n", pci_info.regs);
  151. fsl_pci_init_port(&pci_info, &pci_hoses[pci_num], pci_num);
  152. /* Jump to next PCI node */
  153. pci_node = fdt_node_offset_by_prop_value(fdt, pci_node,
  154. "device_type", "pci", 4);
  155. pci_num++;
  156. }
  157. puts("\n");
  158. }
  159. int last_stage_init(void)
  160. {
  161. void *fdt = get_fdt_virt();
  162. int len = 0;
  163. const uint64_t *prop;
  164. int chosen;
  165. chosen = fdt_path_offset(fdt, "/chosen");
  166. if (chosen < 0) {
  167. printf("Couldn't find /chosen node in fdt\n");
  168. return -EIO;
  169. }
  170. /* -kernel boot */
  171. prop = fdt_getprop(fdt, chosen, "qemu,boot-kernel", &len);
  172. if (prop && (len >= 8))
  173. env_set_hex("qemu_kernel_addr", *prop);
  174. /* Give the user a variable for the host fdt */
  175. env_set_hex("fdt_addr_r", (ulong)fdt);
  176. return 0;
  177. }
  178. static uint64_t get_linear_ram_size(void)
  179. {
  180. void *fdt = get_fdt_virt();
  181. const void *prop;
  182. int memory;
  183. int len;
  184. memory = fdt_path_offset(fdt, "/memory");
  185. prop = fdt_getprop(fdt, memory, "reg", &len);
  186. if (prop && len >= 16)
  187. return *(uint64_t *)(prop+8);
  188. panic("Couldn't determine RAM size");
  189. }
  190. int board_eth_init(bd_t *bis)
  191. {
  192. return pci_eth_init(bis);
  193. }
  194. #if defined(CONFIG_OF_BOARD_SETUP)
  195. int ft_board_setup(void *blob, bd_t *bd)
  196. {
  197. FT_FSL_PCI_SETUP;
  198. return 0;
  199. }
  200. #endif
  201. void print_laws(void)
  202. {
  203. /* We don't emulate LAWs yet */
  204. }
  205. phys_size_t fixed_sdram(void)
  206. {
  207. return get_linear_ram_size();
  208. }
  209. phys_size_t fsl_ddr_sdram_size(void)
  210. {
  211. return get_linear_ram_size();
  212. }
  213. void init_tlbs(void)
  214. {
  215. phys_size_t ram_size;
  216. /*
  217. * Create a temporary AS=1 map for the fdt
  218. *
  219. * We use ESEL=0 here to overwrite the previous AS=0 map for ourselves
  220. * which was only 4k big. This way we don't have to clear any other maps.
  221. */
  222. map_fdt_as(0);
  223. /* Fetch RAM size from the fdt */
  224. ram_size = get_linear_ram_size();
  225. /* And remove our fdt map again */
  226. disable_tlb(0);
  227. /* Create an internal map of manually created TLB maps */
  228. init_used_tlb_cams();
  229. /* Create a dynamic AS=0 CCSRBAR mapping */
  230. assert(!tlb_map_range(CONFIG_SYS_CCSRBAR, CONFIG_SYS_CCSRBAR_PHYS,
  231. 1024 * 1024, TLB_MAP_IO));
  232. /* Create a RAM map that spans all accessible RAM */
  233. setup_ddr_tlbs(ram_size >> 20);
  234. /* Create a map for the TLB */
  235. assert(!tlb_map_range((ulong)get_fdt_virt(), get_fdt_phys(),
  236. 1024 * 1024, TLB_MAP_RAM));
  237. }
  238. void init_laws(void)
  239. {
  240. /* We don't emulate LAWs yet */
  241. }
  242. static uint32_t get_cpu_freq(void)
  243. {
  244. void *fdt = get_fdt_virt();
  245. int cpus_node = fdt_path_offset(fdt, "/cpus");
  246. int cpu_node = fdt_first_subnode(fdt, cpus_node);
  247. const char *prop = "clock-frequency";
  248. return fdt_getprop_u32_default_node(fdt, cpu_node, 0, prop, 0);
  249. }
  250. void get_sys_info(sys_info_t *sys_info)
  251. {
  252. int freq = get_cpu_freq();
  253. memset(sys_info, 0, sizeof(sys_info_t));
  254. sys_info->freq_systembus = freq;
  255. sys_info->freq_ddrbus = freq;
  256. sys_info->freq_processor[0] = freq;
  257. }
  258. int get_clocks (void)
  259. {
  260. sys_info_t sys_info;
  261. get_sys_info(&sys_info);
  262. gd->cpu_clk = sys_info.freq_processor[0];
  263. gd->bus_clk = sys_info.freq_systembus;
  264. gd->mem_clk = sys_info.freq_ddrbus;
  265. gd->arch.lbc_clk = sys_info.freq_ddrbus;
  266. return 0;
  267. }
  268. unsigned long get_tbclk (void)
  269. {
  270. void *fdt = get_fdt_virt();
  271. int cpus_node = fdt_path_offset(fdt, "/cpus");
  272. int cpu_node = fdt_first_subnode(fdt, cpus_node);
  273. const char *prop = "timebase-frequency";
  274. return fdt_getprop_u32_default_node(fdt, cpu_node, 0, prop, 0);
  275. }
  276. /********************************************
  277. * get_bus_freq
  278. * return system bus freq in Hz
  279. *********************************************/
  280. ulong get_bus_freq (ulong dummy)
  281. {
  282. sys_info_t sys_info;
  283. get_sys_info(&sys_info);
  284. return sys_info.freq_systembus;
  285. }
  286. /*
  287. * Return the number of cores on this SOC.
  288. */
  289. int cpu_numcores(void)
  290. {
  291. /*
  292. * The QEMU u-boot target only needs to drive the first core,
  293. * spinning and device tree nodes get driven by QEMU itself
  294. */
  295. return 1;
  296. }
  297. /*
  298. * Return a 32-bit mask indicating which cores are present on this SOC.
  299. */
  300. u32 cpu_mask(void)
  301. {
  302. return (1 << cpu_numcores()) - 1;
  303. }