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