qemu-ppce500.c 7.6 KB

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