qemu-ppce500.c 7.8 KB

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