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