blob: 50167d546f831de3faffafea4fdfcfed69328994 [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>
Simon Glass0f2af882020-05-10 11:40:05 -060011#include <log.h>
Simon Glass274e0b02020-05-10 11:39:56 -060012#include <net.h>
Alexander Grafc3468482014-04-11 17:09:45 +020013#include <pci.h>
Simon Glassa9dc0682019-12-28 10:44:59 -070014#include <time.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060015#include <asm/global_data.h>
Alexander Grafc3468482014-04-11 17:09:45 +020016#include <asm/processor.h>
17#include <asm/mmu.h>
18#include <asm/fsl_pci.h>
19#include <asm/io.h>
Masahiro Yamada75f82d02018-03-05 01:20:11 +090020#include <linux/libfdt.h>
Alexander Grafc3468482014-04-11 17:09:45 +020021#include <fdt_support.h>
22#include <netdev.h>
23#include <fdtdec.h>
24#include <errno.h>
25#include <malloc.h>
26
27DECLARE_GLOBAL_DATA_PTR;
28
29static void *get_fdt_virt(void)
30{
31 return (void *)CONFIG_SYS_TMPVIRT;
32}
33
34static uint64_t get_fdt_phys(void)
35{
36 return (uint64_t)(uintptr_t)gd->fdt_blob;
37}
38
39static 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
55uint64_t get_phys_ccsrbar_addr_early(void)
56{
57 void *fdt = get_fdt_virt();
58 uint64_t r;
Tom Rini661d6d82017-08-03 08:53:36 -040059 int size, node;
60 u32 naddr;
61 const fdt32_t *prop;
Alexander Grafc3468482014-04-11 17:09:45 +020062
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);
Tom Rini661d6d82017-08-03 08:53:36 -040068 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);
Alexander Grafc3468482014-04-11 17:09:45 +020072 disable_tlb(10);
73
74 return r;
75}
76
Alexander Grafc3468482014-04-11 17:09:45 +020077int checkboard(void)
78{
79 return 0;
80}
81
82static int pci_map_region(void *fdt, int pci_node, int range_id,
Bin Mengd8632422021-02-25 17:22:25 +080083 phys_addr_t *pbaddr, phys_size_t *ppaddr,
84 pci_addr_t *pvaddr, pci_size_t *psize,
85 ulong *pmap_addr)
Alexander Grafc3468482014-04-11 17:09:45 +020086{
Bin Mengd8632422021-02-25 17:22:25 +080087 uint64_t baddr;
88 uint64_t paddr;
Alexander Grafc3468482014-04-11 17:09:45 +020089 uint64_t size;
90 ulong map_addr;
91 int r;
92
Bin Mengd8632422021-02-25 17:22:25 +080093 r = fdt_read_range(fdt, pci_node, range_id, &baddr, &paddr, &size);
Alexander Grafc3468482014-04-11 17:09:45 +020094 if (r)
95 return r;
96
Bin Mengd8632422021-02-25 17:22:25 +080097 if (pbaddr)
98 *pbaddr = baddr;
Alexander Grafc3468482014-04-11 17:09:45 +020099 if (ppaddr)
Bin Mengd8632422021-02-25 17:22:25 +0800100 *ppaddr = paddr;
Alexander Grafc3468482014-04-11 17:09:45 +0200101 if (psize)
102 *psize = size;
103
104 if (!pmap_addr)
105 return 0;
106
107 map_addr = *pmap_addr;
108
109 /* Align map_addr */
110 map_addr += size - 1;
111 map_addr &= ~(size - 1);
112
113 if (map_addr + size >= CONFIG_SYS_PCI_MAP_END)
114 return -1;
115
116 /* Map virtual memory for range */
Bin Mengd8632422021-02-25 17:22:25 +0800117 assert(!tlb_map_range(map_addr, paddr, size, TLB_MAP_IO));
Alexander Grafc3468482014-04-11 17:09:45 +0200118 *pmap_addr = map_addr + size;
119
120 if (pvaddr)
121 *pvaddr = map_addr;
122
123 return 0;
124}
125
126void pci_init_board(void)
127{
128 struct pci_controller *pci_hoses;
129 void *fdt = get_fdt_virt();
130 int pci_node = -1;
131 int pci_num = 0;
132 int pci_count = 0;
133 ulong map_addr;
134
135 puts("\n");
136
137 /* Start MMIO and PIO range maps above RAM */
138 map_addr = CONFIG_SYS_PCI_MAP_START;
139
140 /* Count and allocate PCI buses */
141 pci_node = fdt_node_offset_by_prop_value(fdt, pci_node,
142 "device_type", "pci", 4);
143 while (pci_node != -FDT_ERR_NOTFOUND) {
144 pci_node = fdt_node_offset_by_prop_value(fdt, pci_node,
145 "device_type", "pci", 4);
146 pci_count++;
147 }
148
149 if (pci_count) {
150 pci_hoses = malloc(sizeof(struct pci_controller) * pci_count);
151 } else {
152 printf("PCI: disabled\n\n");
153 return;
154 }
155
156 /* Spawn PCI buses based on device tree */
157 pci_node = fdt_node_offset_by_prop_value(fdt, pci_node,
158 "device_type", "pci", 4);
159 while (pci_node != -FDT_ERR_NOTFOUND) {
160 struct fsl_pci_info pci_info = { };
161 const fdt32_t *reg;
162 int r;
163
164 reg = fdt_getprop(fdt, pci_node, "reg", NULL);
165 pci_info.regs = fdt_translate_address(fdt, pci_node, reg);
166
167 /* Map MMIO range */
Bin Mengd8632422021-02-25 17:22:25 +0800168 r = pci_map_region(fdt, pci_node, 0, &pci_info.mem_bus,
169 &pci_info.mem_phys, NULL,
Alexander Grafc3468482014-04-11 17:09:45 +0200170 &pci_info.mem_size, &map_addr);
171 if (r)
172 break;
173
174 /* Map PIO range */
Bin Mengd8632422021-02-25 17:22:25 +0800175 r = pci_map_region(fdt, pci_node, 1, &pci_info.io_bus,
176 &pci_info.io_phys, NULL,
Alexander Grafc3468482014-04-11 17:09:45 +0200177 &pci_info.io_size, &map_addr);
178 if (r)
179 break;
180
Alexander Grafc3468482014-04-11 17:09:45 +0200181 /* Instantiate */
182 pci_info.pci_num = pci_num + 1;
183
184 fsl_setup_hose(&pci_hoses[pci_num], pci_info.regs);
185 printf("PCI: base address %lx\n", pci_info.regs);
186
187 fsl_pci_init_port(&pci_info, &pci_hoses[pci_num], pci_num);
188
189 /* Jump to next PCI node */
190 pci_node = fdt_node_offset_by_prop_value(fdt, pci_node,
191 "device_type", "pci", 4);
192 pci_num++;
193 }
194
195 puts("\n");
196}
197
198int last_stage_init(void)
199{
200 void *fdt = get_fdt_virt();
201 int len = 0;
202 const uint64_t *prop;
203 int chosen;
204
205 chosen = fdt_path_offset(fdt, "/chosen");
206 if (chosen < 0) {
207 printf("Couldn't find /chosen node in fdt\n");
208 return -EIO;
209 }
210
211 /* -kernel boot */
212 prop = fdt_getprop(fdt, chosen, "qemu,boot-kernel", &len);
213 if (prop && (len >= 8))
Simon Glass4d949a22017-08-03 12:22:10 -0600214 env_set_hex("qemu_kernel_addr", *prop);
Alexander Grafc3468482014-04-11 17:09:45 +0200215
216 /* Give the user a variable for the host fdt */
Simon Glass4d949a22017-08-03 12:22:10 -0600217 env_set_hex("fdt_addr_r", (ulong)fdt);
Alexander Grafc3468482014-04-11 17:09:45 +0200218
219 return 0;
220}
221
222static uint64_t get_linear_ram_size(void)
223{
224 void *fdt = get_fdt_virt();
225 const void *prop;
226 int memory;
227 int len;
228
229 memory = fdt_path_offset(fdt, "/memory");
230 prop = fdt_getprop(fdt, memory, "reg", &len);
231
232 if (prop && len >= 16)
233 return *(uint64_t *)(prop+8);
234
235 panic("Couldn't determine RAM size");
236}
237
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +0900238int board_eth_init(struct bd_info *bis)
Alexander Grafc3468482014-04-11 17:09:45 +0200239{
240 return pci_eth_init(bis);
241}
242
243#if defined(CONFIG_OF_BOARD_SETUP)
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +0900244int ft_board_setup(void *blob, struct bd_info *bd)
Alexander Grafc3468482014-04-11 17:09:45 +0200245{
246 FT_FSL_PCI_SETUP;
Simon Glass2aec3cc2014-10-23 18:58:47 -0600247
248 return 0;
Alexander Grafc3468482014-04-11 17:09:45 +0200249}
250#endif
251
Alexander Grafc3468482014-04-11 17:09:45 +0200252phys_size_t fixed_sdram(void)
253{
254 return get_linear_ram_size();
255}
256
257phys_size_t fsl_ddr_sdram_size(void)
258{
259 return get_linear_ram_size();
260}
261
262void init_tlbs(void)
263{
264 phys_size_t ram_size;
265
266 /*
267 * Create a temporary AS=1 map for the fdt
268 *
269 * We use ESEL=0 here to overwrite the previous AS=0 map for ourselves
270 * which was only 4k big. This way we don't have to clear any other maps.
271 */
272 map_fdt_as(0);
273
274 /* Fetch RAM size from the fdt */
275 ram_size = get_linear_ram_size();
276
277 /* And remove our fdt map again */
278 disable_tlb(0);
279
280 /* Create an internal map of manually created TLB maps */
281 init_used_tlb_cams();
282
283 /* Create a dynamic AS=0 CCSRBAR mapping */
284 assert(!tlb_map_range(CONFIG_SYS_CCSRBAR, CONFIG_SYS_CCSRBAR_PHYS,
285 1024 * 1024, TLB_MAP_IO));
286
287 /* Create a RAM map that spans all accessible RAM */
288 setup_ddr_tlbs(ram_size >> 20);
289
290 /* Create a map for the TLB */
291 assert(!tlb_map_range((ulong)get_fdt_virt(), get_fdt_phys(),
292 1024 * 1024, TLB_MAP_RAM));
293}
294
Alexander Grafc3468482014-04-11 17:09:45 +0200295static uint32_t get_cpu_freq(void)
296{
297 void *fdt = get_fdt_virt();
298 int cpus_node = fdt_path_offset(fdt, "/cpus");
299 int cpu_node = fdt_first_subnode(fdt, cpus_node);
300 const char *prop = "clock-frequency";
301 return fdt_getprop_u32_default_node(fdt, cpu_node, 0, prop, 0);
302}
303
304void get_sys_info(sys_info_t *sys_info)
305{
306 int freq = get_cpu_freq();
307
308 memset(sys_info, 0, sizeof(sys_info_t));
309 sys_info->freq_systembus = freq;
310 sys_info->freq_ddrbus = freq;
311 sys_info->freq_processor[0] = freq;
312}
313
Simon Glass85d65312019-12-28 10:44:58 -0700314int get_clocks(void)
Alexander Grafc3468482014-04-11 17:09:45 +0200315{
316 sys_info_t sys_info;
317
318 get_sys_info(&sys_info);
319
320 gd->cpu_clk = sys_info.freq_processor[0];
321 gd->bus_clk = sys_info.freq_systembus;
322 gd->mem_clk = sys_info.freq_ddrbus;
323 gd->arch.lbc_clk = sys_info.freq_ddrbus;
324
325 return 0;
326}
327
Simon Glassa9dc0682019-12-28 10:44:59 -0700328unsigned long get_tbclk(void)
Alexander Grafc3468482014-04-11 17:09:45 +0200329{
330 void *fdt = get_fdt_virt();
331 int cpus_node = fdt_path_offset(fdt, "/cpus");
332 int cpu_node = fdt_first_subnode(fdt, cpus_node);
333 const char *prop = "timebase-frequency";
334 return fdt_getprop_u32_default_node(fdt, cpu_node, 0, prop, 0);
335}
336
337/********************************************
338 * get_bus_freq
339 * return system bus freq in Hz
340 *********************************************/
Simon Glass85d65312019-12-28 10:44:58 -0700341ulong get_bus_freq(ulong dummy)
Alexander Grafc3468482014-04-11 17:09:45 +0200342{
343 sys_info_t sys_info;
344 get_sys_info(&sys_info);
345 return sys_info.freq_systembus;
346}
Alexander Graf5b9e18c2014-04-30 19:21:10 +0200347
348/*
349 * Return the number of cores on this SOC.
350 */
351int cpu_numcores(void)
352{
353 /*
354 * The QEMU u-boot target only needs to drive the first core,
355 * spinning and device tree nodes get driven by QEMU itself
356 */
357 return 1;
358}
359
360/*
361 * Return a 32-bit mask indicating which cores are present on this SOC.
362 */
363u32 cpu_mask(void)
364{
365 return (1 << cpu_numcores()) - 1;
366}
Bin Meng11f7a3f2021-02-25 17:22:30 +0800367
368/**
369 * Return the virtual address of FDT that was passed by QEMU
370 *
371 * @return virtual address of FDT received from QEMU in r3 register
372 */
373void *board_fdt_blob_setup(void)
374{
375 return get_fdt_virt();
376}