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