fdt_machdep.c revision 1.6 1 1.6 jmcneill /* $NetBSD: fdt_machdep.c,v 1.6 2017/06/06 09:56:00 jmcneill Exp $ */
2 1.1 jmcneill
3 1.1 jmcneill /*-
4 1.1 jmcneill * Copyright (c) 2015-2017 Jared McNeill <jmcneill (at) invisible.ca>
5 1.1 jmcneill * All rights reserved.
6 1.1 jmcneill *
7 1.1 jmcneill * Redistribution and use in source and binary forms, with or without
8 1.1 jmcneill * modification, are permitted provided that the following conditions
9 1.1 jmcneill * are met:
10 1.1 jmcneill * 1. Redistributions of source code must retain the above copyright
11 1.1 jmcneill * notice, this list of conditions and the following disclaimer.
12 1.1 jmcneill * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 jmcneill * notice, this list of conditions and the following disclaimer in the
14 1.1 jmcneill * documentation and/or other materials provided with the distribution.
15 1.1 jmcneill *
16 1.1 jmcneill * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 1.1 jmcneill * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 1.1 jmcneill * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 1.1 jmcneill * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 1.1 jmcneill * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 1.1 jmcneill * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 1.1 jmcneill * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 1.1 jmcneill * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 1.1 jmcneill * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.1 jmcneill * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.1 jmcneill * SUCH DAMAGE.
27 1.1 jmcneill */
28 1.1 jmcneill
29 1.1 jmcneill #include <sys/cdefs.h>
30 1.6 jmcneill __KERNEL_RCSID(0, "$NetBSD: fdt_machdep.c,v 1.6 2017/06/06 09:56:00 jmcneill Exp $");
31 1.1 jmcneill
32 1.1 jmcneill #include "opt_machdep.h"
33 1.1 jmcneill #include "opt_ddb.h"
34 1.1 jmcneill #include "opt_md.h"
35 1.1 jmcneill #include "opt_arm_debug.h"
36 1.1 jmcneill #include "opt_multiprocessor.h"
37 1.1 jmcneill #include "opt_cpuoptions.h"
38 1.1 jmcneill
39 1.1 jmcneill #include <sys/param.h>
40 1.1 jmcneill #include <sys/systm.h>
41 1.1 jmcneill #include <sys/bus.h>
42 1.1 jmcneill #include <sys/atomic.h>
43 1.1 jmcneill #include <sys/cpu.h>
44 1.1 jmcneill #include <sys/device.h>
45 1.1 jmcneill #include <sys/exec.h>
46 1.1 jmcneill #include <sys/kernel.h>
47 1.1 jmcneill #include <sys/kmem.h>
48 1.1 jmcneill #include <sys/ksyms.h>
49 1.1 jmcneill #include <sys/msgbuf.h>
50 1.1 jmcneill #include <sys/proc.h>
51 1.1 jmcneill #include <sys/reboot.h>
52 1.1 jmcneill #include <sys/termios.h>
53 1.1 jmcneill
54 1.1 jmcneill #include <uvm/uvm_extern.h>
55 1.1 jmcneill
56 1.1 jmcneill #include <sys/conf.h>
57 1.1 jmcneill
58 1.1 jmcneill #include <machine/db_machdep.h>
59 1.1 jmcneill #include <ddb/db_sym.h>
60 1.1 jmcneill #include <ddb/db_extern.h>
61 1.1 jmcneill
62 1.1 jmcneill #include <machine/bootconfig.h>
63 1.1 jmcneill #include <arm/armreg.h>
64 1.1 jmcneill #include <arm/undefined.h>
65 1.1 jmcneill
66 1.1 jmcneill #include <arm/arm32/machdep.h>
67 1.1 jmcneill
68 1.1 jmcneill #include <evbarm/include/autoconf.h>
69 1.1 jmcneill #include <evbarm/fdt/platform.h>
70 1.1 jmcneill
71 1.1 jmcneill #include <arm/fdt/arm_fdtvar.h>
72 1.1 jmcneill
73 1.1 jmcneill #ifndef FDT_MAX_BOOT_STRING
74 1.1 jmcneill #define FDT_MAX_BOOT_STRING 1024
75 1.1 jmcneill #endif
76 1.1 jmcneill
77 1.1 jmcneill BootConfig bootconfig;
78 1.1 jmcneill char bootargs[FDT_MAX_BOOT_STRING] = "";
79 1.1 jmcneill char *boot_args = NULL;
80 1.1 jmcneill u_int uboot_args[4] = { 0 }; /* filled in by xxx_start.S (not in bss) */
81 1.1 jmcneill
82 1.1 jmcneill #include <libfdt.h>
83 1.1 jmcneill #include <dev/fdt/fdtvar.h>
84 1.1 jmcneill #define FDT_BUF_SIZE (128*1024)
85 1.1 jmcneill static uint8_t fdt_data[FDT_BUF_SIZE];
86 1.1 jmcneill
87 1.1 jmcneill extern char KERNEL_BASE_phys[];
88 1.1 jmcneill #define KERNEL_BASE_PHYS ((paddr_t)KERNEL_BASE_phys)
89 1.1 jmcneill
90 1.5 jmcneill static void fdt_update_stdout_path(void);
91 1.1 jmcneill static void fdt_device_register(device_t, void *);
92 1.1 jmcneill static void fdt_reset(void);
93 1.1 jmcneill static void fdt_powerdown(void);
94 1.1 jmcneill
95 1.1 jmcneill #ifdef PMAP_NEED_ALLOC_POOLPAGE
96 1.1 jmcneill static struct boot_physmem bp_lowgig = {
97 1.1 jmcneill .bp_pages = (KERNEL_VM_BASE - KERNEL_BASE) / NBPG,
98 1.1 jmcneill .bp_freelist = VM_FREELIST_ISADMA,
99 1.1 jmcneill .bp_flags = 0
100 1.1 jmcneill };
101 1.1 jmcneill #endif
102 1.1 jmcneill
103 1.1 jmcneill #ifdef VERBOSE_INIT_ARM
104 1.1 jmcneill static void
105 1.1 jmcneill fdt_putchar(char c)
106 1.1 jmcneill {
107 1.1 jmcneill const struct arm_platform *plat = arm_fdt_platform();
108 1.2 jmcneill if (plat && plat->early_putchar)
109 1.1 jmcneill plat->early_putchar(c);
110 1.1 jmcneill }
111 1.1 jmcneill
112 1.1 jmcneill static void
113 1.1 jmcneill fdt_putstr(const char *s)
114 1.1 jmcneill {
115 1.1 jmcneill for (const char *p = s; *p; p++)
116 1.1 jmcneill fdt_putchar(*p);
117 1.1 jmcneill }
118 1.1 jmcneill
119 1.1 jmcneill static void
120 1.1 jmcneill fdt_printn(u_int n, int base)
121 1.1 jmcneill {
122 1.1 jmcneill char *p, buf[(sizeof(u_int) * NBBY / 3) + 1 + 2 /* ALT + SIGN */];
123 1.1 jmcneill
124 1.1 jmcneill p = buf;
125 1.1 jmcneill do {
126 1.1 jmcneill *p++ = hexdigits[n % base];
127 1.1 jmcneill } while (n /= base);
128 1.1 jmcneill
129 1.1 jmcneill do {
130 1.1 jmcneill fdt_putchar(*--p);
131 1.1 jmcneill } while (p > buf);
132 1.1 jmcneill }
133 1.1 jmcneill #define DPRINTF(...) printf(__VA_ARGS__)
134 1.1 jmcneill #define DPRINT(x) fdt_putstr(x)
135 1.1 jmcneill #define DPRINTN(x,b) fdt_printn((x), (b))
136 1.1 jmcneill #else
137 1.1 jmcneill #define DPRINTF(...)
138 1.1 jmcneill #define DPRINT(x)
139 1.1 jmcneill #define DPRINTN(x,b)
140 1.1 jmcneill #endif
141 1.1 jmcneill
142 1.1 jmcneill u_int
143 1.1 jmcneill initarm(void *arg)
144 1.1 jmcneill {
145 1.1 jmcneill const struct arm_platform *plat;
146 1.1 jmcneill uint64_t memory_addr, memory_size;
147 1.1 jmcneill psize_t ram_size = 0;
148 1.1 jmcneill
149 1.1 jmcneill /* Load FDT */
150 1.1 jmcneill const uint8_t *fdt_addr_r = (const uint8_t *)uboot_args[2];
151 1.1 jmcneill int error = fdt_check_header(fdt_addr_r);
152 1.1 jmcneill if (error == 0) {
153 1.1 jmcneill error = fdt_move(fdt_addr_r, fdt_data, sizeof(fdt_data));
154 1.1 jmcneill if (error != 0)
155 1.1 jmcneill panic("fdt_move failed: %s", fdt_strerror(error));
156 1.1 jmcneill fdtbus_set_data(fdt_data);
157 1.1 jmcneill } else {
158 1.1 jmcneill panic("fdt_check_header failed: %s", fdt_strerror(error));
159 1.1 jmcneill }
160 1.1 jmcneill
161 1.1 jmcneill /* Lookup platform specific backend */
162 1.1 jmcneill plat = arm_fdt_platform();
163 1.1 jmcneill if (plat == NULL)
164 1.1 jmcneill panic("Kernel does not support this device");
165 1.1 jmcneill
166 1.1 jmcneill /* Early console may be available, announce ourselves. */
167 1.2 jmcneill DPRINT("FDT<");
168 1.2 jmcneill DPRINTN((uintptr_t)fdt_addr_r, 16);
169 1.2 jmcneill DPRINT(">");
170 1.1 jmcneill
171 1.6 jmcneill const int chosen = OF_finddevice("/chosen");
172 1.6 jmcneill if (chosen >= 0)
173 1.6 jmcneill OF_getprop(chosen, "bootargs", bootargs, sizeof(bootargs));
174 1.6 jmcneill boot_args = bootargs;
175 1.6 jmcneill
176 1.1 jmcneill DPRINT(" devmap");
177 1.1 jmcneill pmap_devmap_register(plat->devmap());
178 1.1 jmcneill
179 1.1 jmcneill DPRINT(" bootstrap");
180 1.1 jmcneill plat->bootstrap();
181 1.1 jmcneill
182 1.1 jmcneill /* Heads up ... Setup the CPU / MMU / TLB functions. */
183 1.1 jmcneill DPRINT(" cpufunc");
184 1.1 jmcneill if (set_cpufuncs())
185 1.1 jmcneill panic("cpu not recognized!");
186 1.1 jmcneill
187 1.5 jmcneill /*
188 1.5 jmcneill * If stdout-path is specified on the command line, override the
189 1.5 jmcneill * value in /chosen/stdout-path before initializing console.
190 1.5 jmcneill */
191 1.5 jmcneill fdt_update_stdout_path();
192 1.5 jmcneill
193 1.1 jmcneill DPRINT(" consinit");
194 1.1 jmcneill consinit();
195 1.1 jmcneill
196 1.1 jmcneill DPRINTF(" ok\n");
197 1.1 jmcneill
198 1.1 jmcneill DPRINTF("uboot: args %#x, %#x, %#x, %#x\n",
199 1.1 jmcneill uboot_args[0], uboot_args[1], uboot_args[2], uboot_args[3]);
200 1.1 jmcneill
201 1.1 jmcneill cpu_reset_address = fdt_reset;
202 1.1 jmcneill cpu_powerdown_address = fdt_powerdown;
203 1.1 jmcneill evbarm_device_register = fdt_device_register;
204 1.1 jmcneill
205 1.1 jmcneill /* Talk to the user */
206 1.1 jmcneill DPRINTF("\nNetBSD/evbarm (fdt) booting ...\n");
207 1.1 jmcneill
208 1.1 jmcneill #ifdef BOOT_ARGS
209 1.1 jmcneill char mi_bootargs[] = BOOT_ARGS;
210 1.1 jmcneill parse_mi_bootargs(mi_bootargs);
211 1.1 jmcneill #endif
212 1.1 jmcneill
213 1.1 jmcneill DPRINTF("KERNEL_BASE=0x%x, "
214 1.1 jmcneill "KERNEL_VM_BASE=0x%x, "
215 1.1 jmcneill "KERNEL_VM_BASE - KERNEL_BASE=0x%x, "
216 1.1 jmcneill "KERNEL_BASE_VOFFSET=0x%x\n",
217 1.1 jmcneill KERNEL_BASE,
218 1.1 jmcneill KERNEL_VM_BASE,
219 1.1 jmcneill KERNEL_VM_BASE - KERNEL_BASE,
220 1.1 jmcneill KERNEL_BASE_VOFFSET);
221 1.1 jmcneill
222 1.1 jmcneill const int memory = OF_finddevice("/memory");
223 1.1 jmcneill if (fdtbus_get_reg64(memory, 0, &memory_addr, &memory_size) != 0)
224 1.1 jmcneill panic("Cannot determine memory size");
225 1.1 jmcneill
226 1.1 jmcneill #if !defined(_LP64)
227 1.1 jmcneill /* Cannot map memory above 4GB */
228 1.1 jmcneill if (memory_addr + memory_size > 0x100000000)
229 1.1 jmcneill memory_size = 0x100000000 - memory_addr;
230 1.1 jmcneill #endif
231 1.1 jmcneill
232 1.1 jmcneill ram_size = (bus_size_t)memory_size;
233 1.1 jmcneill
234 1.1 jmcneill #ifdef __HAVE_MM_MD_DIRECT_MAPPED_PHYS
235 1.1 jmcneill const bool mapallmem_p = true;
236 1.1 jmcneill #ifndef PMAP_NEED_ALLOC_POOLPAGE
237 1.1 jmcneill if (ram_size > KERNEL_VM_BASE - KERNEL_BASE) {
238 1.1 jmcneill DPRINTF("%s: dropping RAM size from %luMB to %uMB\n",
239 1.1 jmcneill __func__, (unsigned long) (ram_size >> 20),
240 1.1 jmcneill (KERNEL_VM_BASE - KERNEL_BASE) >> 20);
241 1.1 jmcneill ram_size = KERNEL_VM_BASE - KERNEL_BASE;
242 1.1 jmcneill }
243 1.1 jmcneill #endif
244 1.1 jmcneill #else
245 1.1 jmcneill const bool mapallmem_p = false;
246 1.1 jmcneill #endif
247 1.1 jmcneill
248 1.1 jmcneill /* Fake bootconfig structure for the benefit of pmap.c. */
249 1.1 jmcneill bootconfig.dramblocks = 1;
250 1.1 jmcneill bootconfig.dram[0].address = (bus_addr_t)memory_addr;
251 1.1 jmcneill bootconfig.dram[0].pages = ram_size / PAGE_SIZE;
252 1.1 jmcneill
253 1.1 jmcneill arm32_bootmem_init(bootconfig.dram[0].address, ram_size,
254 1.1 jmcneill KERNEL_BASE_PHYS);
255 1.1 jmcneill arm32_kernel_vm_init(KERNEL_VM_BASE, ARM_VECTORS_HIGH, 0,
256 1.1 jmcneill plat->devmap(), mapallmem_p);
257 1.1 jmcneill
258 1.1 jmcneill DPRINTF("bootargs: %s\n", bootargs);
259 1.1 jmcneill
260 1.1 jmcneill parse_mi_bootargs(boot_args);
261 1.1 jmcneill
262 1.1 jmcneill #ifdef PMAP_NEED_ALLOC_POOLPAGE
263 1.1 jmcneill bp_lowgig.bp_start = memory_addr / NBPG;
264 1.1 jmcneill if (atop(ram_size) > bp_lowgig.bp_pages) {
265 1.1 jmcneill arm_poolpage_vmfreelist = bp_lowgig.bp_freelist;
266 1.1 jmcneill return initarm_common(KERNEL_VM_BASE, KERNEL_VM_SIZE,
267 1.1 jmcneill &bp_lowgig, 1);
268 1.1 jmcneill }
269 1.1 jmcneill #endif
270 1.1 jmcneill
271 1.1 jmcneill return initarm_common(KERNEL_VM_BASE, KERNEL_VM_SIZE, NULL, 0);
272 1.1 jmcneill
273 1.1 jmcneill }
274 1.1 jmcneill
275 1.5 jmcneill static void
276 1.5 jmcneill fdt_update_stdout_path(void)
277 1.5 jmcneill {
278 1.5 jmcneill char *stdout_path, *ep;
279 1.5 jmcneill int stdout_path_len;
280 1.5 jmcneill char buf[256];
281 1.5 jmcneill
282 1.5 jmcneill const int chosen_off = fdt_path_offset(fdt_data, "/chosen");
283 1.5 jmcneill if (chosen_off == -1)
284 1.5 jmcneill return;
285 1.5 jmcneill
286 1.5 jmcneill if (get_bootconf_option(boot_args, "stdout-path",
287 1.5 jmcneill BOOTOPT_TYPE_STRING, &stdout_path) == 0)
288 1.5 jmcneill return;
289 1.5 jmcneill
290 1.5 jmcneill ep = strchr(stdout_path, ' ');
291 1.5 jmcneill stdout_path_len = ep ? (ep - stdout_path) : strlen(stdout_path);
292 1.5 jmcneill if (stdout_path_len >= sizeof(buf))
293 1.5 jmcneill return;
294 1.5 jmcneill
295 1.5 jmcneill strncpy(buf, stdout_path, stdout_path_len);
296 1.5 jmcneill buf[stdout_path_len] = '\0';
297 1.5 jmcneill fdt_setprop(fdt_data, chosen_off, "stdout-path",
298 1.5 jmcneill buf, stdout_path_len + 1);
299 1.5 jmcneill }
300 1.5 jmcneill
301 1.1 jmcneill void
302 1.1 jmcneill consinit(void)
303 1.1 jmcneill {
304 1.1 jmcneill static bool initialized = false;
305 1.1 jmcneill const struct arm_platform *plat = arm_fdt_platform();
306 1.1 jmcneill const struct fdt_console *cons = fdtbus_get_console();
307 1.1 jmcneill struct fdt_attach_args faa;
308 1.4 jmcneill u_int uart_freq = 0;
309 1.1 jmcneill
310 1.1 jmcneill if (initialized || cons == NULL)
311 1.1 jmcneill return;
312 1.1 jmcneill
313 1.1 jmcneill plat->init_attach_args(&faa);
314 1.1 jmcneill faa.faa_phandle = fdtbus_get_stdout_phandle();
315 1.1 jmcneill
316 1.4 jmcneill if (plat->uart_freq != NULL)
317 1.4 jmcneill uart_freq = plat->uart_freq();
318 1.4 jmcneill
319 1.4 jmcneill cons->consinit(&faa, uart_freq);
320 1.1 jmcneill
321 1.1 jmcneill initialized = true;
322 1.1 jmcneill }
323 1.1 jmcneill
324 1.3 jmcneill void
325 1.3 jmcneill delay(u_int us)
326 1.3 jmcneill {
327 1.3 jmcneill const struct arm_platform *plat = arm_fdt_platform();
328 1.3 jmcneill
329 1.3 jmcneill plat->delay(us);
330 1.3 jmcneill }
331 1.3 jmcneill
332 1.1 jmcneill static void
333 1.1 jmcneill fdt_device_register(device_t self, void *aux)
334 1.1 jmcneill {
335 1.1 jmcneill const struct arm_platform *plat = arm_fdt_platform();
336 1.1 jmcneill
337 1.1 jmcneill if (plat && plat->device_register)
338 1.1 jmcneill plat->device_register(self, aux);
339 1.1 jmcneill }
340 1.1 jmcneill
341 1.1 jmcneill static void
342 1.1 jmcneill fdt_reset(void)
343 1.1 jmcneill {
344 1.1 jmcneill const struct arm_platform *plat = arm_fdt_platform();
345 1.1 jmcneill
346 1.1 jmcneill fdtbus_power_reset();
347 1.1 jmcneill
348 1.1 jmcneill if (plat && plat->reset)
349 1.1 jmcneill plat->reset();
350 1.1 jmcneill }
351 1.1 jmcneill
352 1.1 jmcneill static void
353 1.1 jmcneill fdt_powerdown(void)
354 1.1 jmcneill {
355 1.1 jmcneill fdtbus_power_poweroff();
356 1.1 jmcneill }
357