1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2007,2009-2014 Freescale Semiconductor, Inc.
14 #include <asm/processor.h>
16 #include <asm/fsl_pci.h>
18 #include <linux/libfdt.h>
19 #include <fdt_support.h>
25 DECLARE_GLOBAL_DATA_PTR;
27 static void *get_fdt_virt(void)
29 return (void *)CONFIG_SYS_TMPVIRT;
32 static uint64_t get_fdt_phys(void)
34 return (uint64_t)(uintptr_t)gd->fdt_blob;
37 static void map_fdt_as(int esel)
39 u32 mas0, mas1, mas2, mas3, mas7;
40 uint64_t fdt_phys = get_fdt_phys();
41 unsigned long fdt_phys_tlb = fdt_phys & ~0xffffful;
42 unsigned long fdt_virt_tlb = (ulong)get_fdt_virt() & ~0xffffful;
44 mas0 = MAS0_TLBSEL(1) | MAS0_ESEL(esel);
45 mas1 = MAS1_VALID | MAS1_TID(0) | MAS1_TS | MAS1_TSIZE(BOOKE_PAGESZ_1M);
46 mas2 = FSL_BOOKE_MAS2(fdt_virt_tlb, 0);
47 mas3 = FSL_BOOKE_MAS3(fdt_phys_tlb, 0, MAS3_SW|MAS3_SR);
48 mas7 = FSL_BOOKE_MAS7(fdt_phys_tlb);
50 write_tlb(mas0, mas1, mas2, mas3, mas7);
53 uint64_t get_phys_ccsrbar_addr_early(void)
55 void *fdt = get_fdt_virt();
62 * To be able to read the FDT we need to create a temporary TLB
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);
75 int board_early_init_f(void)
85 static int pci_map_region(void *fdt, int pci_node, int range_id,
86 phys_size_t *ppaddr, pci_addr_t *pvaddr,
87 pci_size_t *psize, ulong *pmap_addr)
94 r = fdt_read_range(fdt, pci_node, range_id, NULL, &addr, &size);
106 map_addr = *pmap_addr;
109 map_addr += size - 1;
110 map_addr &= ~(size - 1);
112 if (map_addr + size >= CONFIG_SYS_PCI_MAP_END)
115 /* Map virtual memory for range */
116 assert(!tlb_map_range(map_addr, addr, size, TLB_MAP_IO));
117 *pmap_addr = map_addr + size;
125 void pci_init_board(void)
127 struct pci_controller *pci_hoses;
128 void *fdt = get_fdt_virt();
136 /* Start MMIO and PIO range maps above RAM */
137 map_addr = CONFIG_SYS_PCI_MAP_START;
139 /* Count and allocate PCI buses */
140 pci_node = fdt_node_offset_by_prop_value(fdt, pci_node,
141 "device_type", "pci", 4);
142 while (pci_node != -FDT_ERR_NOTFOUND) {
143 pci_node = fdt_node_offset_by_prop_value(fdt, pci_node,
144 "device_type", "pci", 4);
149 pci_hoses = malloc(sizeof(struct pci_controller) * pci_count);
151 printf("PCI: disabled\n\n");
155 /* Spawn PCI buses based on device tree */
156 pci_node = fdt_node_offset_by_prop_value(fdt, pci_node,
157 "device_type", "pci", 4);
158 while (pci_node != -FDT_ERR_NOTFOUND) {
159 struct fsl_pci_info pci_info = { };
163 reg = fdt_getprop(fdt, pci_node, "reg", NULL);
164 pci_info.regs = fdt_translate_address(fdt, pci_node, reg);
167 r = pci_map_region(fdt, pci_node, 0, &pci_info.mem_phys, NULL,
168 &pci_info.mem_size, &map_addr);
173 r = pci_map_region(fdt, pci_node, 1, &pci_info.io_phys, NULL,
174 &pci_info.io_size, &map_addr);
179 * The PCI framework finds virtual addresses for the buses
180 * through our address map, so tell it the physical addresses.
182 pci_info.mem_bus = pci_info.mem_phys;
183 pci_info.io_bus = pci_info.io_phys;
186 pci_info.pci_num = pci_num + 1;
188 fsl_setup_hose(&pci_hoses[pci_num], pci_info.regs);
189 printf("PCI: base address %lx\n", pci_info.regs);
191 fsl_pci_init_port(&pci_info, &pci_hoses[pci_num], pci_num);
193 /* Jump to next PCI node */
194 pci_node = fdt_node_offset_by_prop_value(fdt, pci_node,
195 "device_type", "pci", 4);
202 int last_stage_init(void)
204 void *fdt = get_fdt_virt();
206 const uint64_t *prop;
209 chosen = fdt_path_offset(fdt, "/chosen");
211 printf("Couldn't find /chosen node in fdt\n");
216 prop = fdt_getprop(fdt, chosen, "qemu,boot-kernel", &len);
217 if (prop && (len >= 8))
218 env_set_hex("qemu_kernel_addr", *prop);
220 /* Give the user a variable for the host fdt */
221 env_set_hex("fdt_addr_r", (ulong)fdt);
226 static uint64_t get_linear_ram_size(void)
228 void *fdt = get_fdt_virt();
233 memory = fdt_path_offset(fdt, "/memory");
234 prop = fdt_getprop(fdt, memory, "reg", &len);
236 if (prop && len >= 16)
237 return *(uint64_t *)(prop+8);
239 panic("Couldn't determine RAM size");
242 int board_eth_init(bd_t *bis)
244 return pci_eth_init(bis);
247 #if defined(CONFIG_OF_BOARD_SETUP)
248 int ft_board_setup(void *blob, bd_t *bd)
256 void print_laws(void)
258 /* We don't emulate LAWs yet */
261 phys_size_t fixed_sdram(void)
263 return get_linear_ram_size();
266 phys_size_t fsl_ddr_sdram_size(void)
268 return get_linear_ram_size();
273 phys_size_t ram_size;
276 * Create a temporary AS=1 map for the fdt
278 * We use ESEL=0 here to overwrite the previous AS=0 map for ourselves
279 * which was only 4k big. This way we don't have to clear any other maps.
283 /* Fetch RAM size from the fdt */
284 ram_size = get_linear_ram_size();
286 /* And remove our fdt map again */
289 /* Create an internal map of manually created TLB maps */
290 init_used_tlb_cams();
292 /* Create a dynamic AS=0 CCSRBAR mapping */
293 assert(!tlb_map_range(CONFIG_SYS_CCSRBAR, CONFIG_SYS_CCSRBAR_PHYS,
294 1024 * 1024, TLB_MAP_IO));
296 /* Create a RAM map that spans all accessible RAM */
297 setup_ddr_tlbs(ram_size >> 20);
299 /* Create a map for the TLB */
300 assert(!tlb_map_range((ulong)get_fdt_virt(), get_fdt_phys(),
301 1024 * 1024, TLB_MAP_RAM));
306 /* We don't emulate LAWs yet */
309 static uint32_t get_cpu_freq(void)
311 void *fdt = get_fdt_virt();
312 int cpus_node = fdt_path_offset(fdt, "/cpus");
313 int cpu_node = fdt_first_subnode(fdt, cpus_node);
314 const char *prop = "clock-frequency";
315 return fdt_getprop_u32_default_node(fdt, cpu_node, 0, prop, 0);
318 void get_sys_info(sys_info_t *sys_info)
320 int freq = get_cpu_freq();
322 memset(sys_info, 0, sizeof(sys_info_t));
323 sys_info->freq_systembus = freq;
324 sys_info->freq_ddrbus = freq;
325 sys_info->freq_processor[0] = freq;
332 get_sys_info(&sys_info);
334 gd->cpu_clk = sys_info.freq_processor[0];
335 gd->bus_clk = sys_info.freq_systembus;
336 gd->mem_clk = sys_info.freq_ddrbus;
337 gd->arch.lbc_clk = sys_info.freq_ddrbus;
342 unsigned long get_tbclk(void)
344 void *fdt = get_fdt_virt();
345 int cpus_node = fdt_path_offset(fdt, "/cpus");
346 int cpu_node = fdt_first_subnode(fdt, cpus_node);
347 const char *prop = "timebase-frequency";
348 return fdt_getprop_u32_default_node(fdt, cpu_node, 0, prop, 0);
351 /********************************************
353 * return system bus freq in Hz
354 *********************************************/
355 ulong get_bus_freq(ulong dummy)
358 get_sys_info(&sys_info);
359 return sys_info.freq_systembus;
363 * Return the number of cores on this SOC.
365 int cpu_numcores(void)
368 * The QEMU u-boot target only needs to drive the first core,
369 * spinning and device tree nodes get driven by QEMU itself
375 * Return a 32-bit mask indicating which cores are present on this SOC.
379 return (1 << cpu_numcores()) - 1;