Home | History | Annotate | Line # | Download | only in fdt
fdt_machdep.c revision 1.4
      1  1.4  jmcneill /* $NetBSD: fdt_machdep.c,v 1.4 2017/06/02 13:53:29 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.4  jmcneill __KERNEL_RCSID(0, "$NetBSD: fdt_machdep.c,v 1.4 2017/06/02 13:53:29 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.1  jmcneill static void fdt_device_register(device_t, void *);
     91  1.1  jmcneill static void fdt_reset(void);
     92  1.1  jmcneill static void fdt_powerdown(void);
     93  1.1  jmcneill 
     94  1.1  jmcneill #ifdef PMAP_NEED_ALLOC_POOLPAGE
     95  1.1  jmcneill static struct boot_physmem bp_lowgig = {
     96  1.1  jmcneill 	.bp_pages = (KERNEL_VM_BASE - KERNEL_BASE) / NBPG,
     97  1.1  jmcneill 	.bp_freelist = VM_FREELIST_ISADMA,
     98  1.1  jmcneill 	.bp_flags = 0
     99  1.1  jmcneill };
    100  1.1  jmcneill #endif
    101  1.1  jmcneill 
    102  1.1  jmcneill #ifdef VERBOSE_INIT_ARM
    103  1.1  jmcneill static void
    104  1.1  jmcneill fdt_putchar(char c)
    105  1.1  jmcneill {
    106  1.1  jmcneill 	const struct arm_platform *plat = arm_fdt_platform();
    107  1.2  jmcneill 	if (plat && plat->early_putchar)
    108  1.1  jmcneill 		plat->early_putchar(c);
    109  1.1  jmcneill }
    110  1.1  jmcneill 
    111  1.1  jmcneill static void
    112  1.1  jmcneill fdt_putstr(const char *s)
    113  1.1  jmcneill {
    114  1.1  jmcneill 	for (const char *p = s; *p; p++)
    115  1.1  jmcneill 		fdt_putchar(*p);
    116  1.1  jmcneill }
    117  1.1  jmcneill 
    118  1.1  jmcneill static void
    119  1.1  jmcneill fdt_printn(u_int n, int base)
    120  1.1  jmcneill {
    121  1.1  jmcneill 	char *p, buf[(sizeof(u_int) * NBBY / 3) + 1 + 2 /* ALT + SIGN */];
    122  1.1  jmcneill 
    123  1.1  jmcneill 	p = buf;
    124  1.1  jmcneill 	do {
    125  1.1  jmcneill 		*p++ = hexdigits[n % base];
    126  1.1  jmcneill 	} while (n /= base);
    127  1.1  jmcneill 
    128  1.1  jmcneill 	do {
    129  1.1  jmcneill 		fdt_putchar(*--p);
    130  1.1  jmcneill 	} while (p > buf);
    131  1.1  jmcneill }
    132  1.1  jmcneill #define DPRINTF(...)		printf(__VA_ARGS__)
    133  1.1  jmcneill #define DPRINT(x)		fdt_putstr(x)
    134  1.1  jmcneill #define DPRINTN(x,b)		fdt_printn((x), (b))
    135  1.1  jmcneill #else
    136  1.1  jmcneill #define DPRINTF(...)
    137  1.1  jmcneill #define DPRINT(x)
    138  1.1  jmcneill #define DPRINTN(x,b)
    139  1.1  jmcneill #endif
    140  1.1  jmcneill 
    141  1.1  jmcneill u_int
    142  1.1  jmcneill initarm(void *arg)
    143  1.1  jmcneill {
    144  1.1  jmcneill 	const struct arm_platform *plat;
    145  1.1  jmcneill 	uint64_t memory_addr, memory_size;
    146  1.1  jmcneill 	psize_t ram_size = 0;
    147  1.1  jmcneill 
    148  1.1  jmcneill 	/* Load FDT */
    149  1.1  jmcneill 	const uint8_t *fdt_addr_r = (const uint8_t *)uboot_args[2];
    150  1.1  jmcneill 	int error = fdt_check_header(fdt_addr_r);
    151  1.1  jmcneill 	if (error == 0) {
    152  1.1  jmcneill 		error = fdt_move(fdt_addr_r, fdt_data, sizeof(fdt_data));
    153  1.1  jmcneill 		if (error != 0)
    154  1.1  jmcneill 			panic("fdt_move failed: %s", fdt_strerror(error));
    155  1.1  jmcneill 		fdtbus_set_data(fdt_data);
    156  1.1  jmcneill 	} else {
    157  1.1  jmcneill 		panic("fdt_check_header failed: %s", fdt_strerror(error));
    158  1.1  jmcneill 	}
    159  1.1  jmcneill 
    160  1.1  jmcneill 	/* Lookup platform specific backend */
    161  1.1  jmcneill 	plat = arm_fdt_platform();
    162  1.1  jmcneill 	if (plat == NULL)
    163  1.1  jmcneill 		panic("Kernel does not support this device");
    164  1.1  jmcneill 
    165  1.1  jmcneill 	/* Early console may be available, announce ourselves. */
    166  1.2  jmcneill 	DPRINT("FDT<");
    167  1.2  jmcneill 	DPRINTN((uintptr_t)fdt_addr_r, 16);
    168  1.2  jmcneill 	DPRINT(">");
    169  1.1  jmcneill 
    170  1.1  jmcneill 	DPRINT(" devmap");
    171  1.1  jmcneill 	pmap_devmap_register(plat->devmap());
    172  1.1  jmcneill 
    173  1.1  jmcneill 	DPRINT(" bootstrap");
    174  1.1  jmcneill 	plat->bootstrap();
    175  1.1  jmcneill 
    176  1.1  jmcneill 	/* Heads up ... Setup the CPU / MMU / TLB functions. */
    177  1.1  jmcneill 	DPRINT(" cpufunc");
    178  1.1  jmcneill 	if (set_cpufuncs())
    179  1.1  jmcneill 		panic("cpu not recognized!");
    180  1.1  jmcneill 
    181  1.1  jmcneill 	DPRINT(" consinit");
    182  1.1  jmcneill 	consinit();
    183  1.1  jmcneill 
    184  1.1  jmcneill 	DPRINTF(" ok\n");
    185  1.1  jmcneill 
    186  1.1  jmcneill 	DPRINTF("uboot: args %#x, %#x, %#x, %#x\n",
    187  1.1  jmcneill 	    uboot_args[0], uboot_args[1], uboot_args[2], uboot_args[3]);
    188  1.1  jmcneill 
    189  1.1  jmcneill 	cpu_reset_address = fdt_reset;
    190  1.1  jmcneill 	cpu_powerdown_address = fdt_powerdown;
    191  1.1  jmcneill 	evbarm_device_register = fdt_device_register;
    192  1.1  jmcneill 
    193  1.1  jmcneill 	/* Talk to the user */
    194  1.1  jmcneill 	DPRINTF("\nNetBSD/evbarm (fdt) booting ...\n");
    195  1.1  jmcneill 
    196  1.1  jmcneill #ifdef BOOT_ARGS
    197  1.1  jmcneill 	char mi_bootargs[] = BOOT_ARGS;
    198  1.1  jmcneill 	parse_mi_bootargs(mi_bootargs);
    199  1.1  jmcneill #endif
    200  1.1  jmcneill 
    201  1.1  jmcneill 	DPRINTF("KERNEL_BASE=0x%x, "
    202  1.1  jmcneill 		"KERNEL_VM_BASE=0x%x, "
    203  1.1  jmcneill 		"KERNEL_VM_BASE - KERNEL_BASE=0x%x, "
    204  1.1  jmcneill 		"KERNEL_BASE_VOFFSET=0x%x\n",
    205  1.1  jmcneill 		KERNEL_BASE,
    206  1.1  jmcneill 		KERNEL_VM_BASE,
    207  1.1  jmcneill 		KERNEL_VM_BASE - KERNEL_BASE,
    208  1.1  jmcneill 		KERNEL_BASE_VOFFSET);
    209  1.1  jmcneill 
    210  1.1  jmcneill 	const int memory = OF_finddevice("/memory");
    211  1.1  jmcneill 	if (fdtbus_get_reg64(memory, 0, &memory_addr, &memory_size) != 0)
    212  1.1  jmcneill 		panic("Cannot determine memory size");
    213  1.1  jmcneill 
    214  1.1  jmcneill #if !defined(_LP64)
    215  1.1  jmcneill 	/* Cannot map memory above 4GB */
    216  1.1  jmcneill 	if (memory_addr + memory_size > 0x100000000)
    217  1.1  jmcneill 		memory_size = 0x100000000 - memory_addr;
    218  1.1  jmcneill #endif
    219  1.1  jmcneill 
    220  1.1  jmcneill 	ram_size = (bus_size_t)memory_size;
    221  1.1  jmcneill 
    222  1.1  jmcneill #ifdef __HAVE_MM_MD_DIRECT_MAPPED_PHYS
    223  1.1  jmcneill 	const bool mapallmem_p = true;
    224  1.1  jmcneill #ifndef PMAP_NEED_ALLOC_POOLPAGE
    225  1.1  jmcneill 	if (ram_size > KERNEL_VM_BASE - KERNEL_BASE) {
    226  1.1  jmcneill 		DPRINTF("%s: dropping RAM size from %luMB to %uMB\n",
    227  1.1  jmcneill 		    __func__, (unsigned long) (ram_size >> 20),
    228  1.1  jmcneill 		    (KERNEL_VM_BASE - KERNEL_BASE) >> 20);
    229  1.1  jmcneill 		ram_size = KERNEL_VM_BASE - KERNEL_BASE;
    230  1.1  jmcneill 	}
    231  1.1  jmcneill #endif
    232  1.1  jmcneill #else
    233  1.1  jmcneill 	const bool mapallmem_p = false;
    234  1.1  jmcneill #endif
    235  1.1  jmcneill 
    236  1.1  jmcneill 	/* Fake bootconfig structure for the benefit of pmap.c. */
    237  1.1  jmcneill 	bootconfig.dramblocks = 1;
    238  1.1  jmcneill 	bootconfig.dram[0].address = (bus_addr_t)memory_addr;
    239  1.1  jmcneill 	bootconfig.dram[0].pages = ram_size / PAGE_SIZE;
    240  1.1  jmcneill 
    241  1.1  jmcneill 	arm32_bootmem_init(bootconfig.dram[0].address, ram_size,
    242  1.1  jmcneill 	    KERNEL_BASE_PHYS);
    243  1.1  jmcneill 	arm32_kernel_vm_init(KERNEL_VM_BASE, ARM_VECTORS_HIGH, 0,
    244  1.1  jmcneill 	    plat->devmap(), mapallmem_p);
    245  1.1  jmcneill 
    246  1.1  jmcneill 	const int chosen = OF_finddevice("/chosen");
    247  1.1  jmcneill 	if (chosen >= 0)
    248  1.1  jmcneill 		OF_getprop(chosen, "bootargs", bootargs, sizeof(bootargs));
    249  1.1  jmcneill 
    250  1.1  jmcneill 	DPRINTF("bootargs: %s\n", bootargs);
    251  1.1  jmcneill 
    252  1.1  jmcneill 	boot_args = bootargs;
    253  1.1  jmcneill 	parse_mi_bootargs(boot_args);
    254  1.1  jmcneill 
    255  1.1  jmcneill #ifdef PMAP_NEED_ALLOC_POOLPAGE
    256  1.1  jmcneill 	bp_lowgig.bp_start = memory_addr / NBPG;
    257  1.1  jmcneill 	if (atop(ram_size) > bp_lowgig.bp_pages) {
    258  1.1  jmcneill 		arm_poolpage_vmfreelist = bp_lowgig.bp_freelist;
    259  1.1  jmcneill 		return initarm_common(KERNEL_VM_BASE, KERNEL_VM_SIZE,
    260  1.1  jmcneill 		    &bp_lowgig, 1);
    261  1.1  jmcneill 	}
    262  1.1  jmcneill #endif
    263  1.1  jmcneill 
    264  1.1  jmcneill 	return initarm_common(KERNEL_VM_BASE, KERNEL_VM_SIZE, NULL, 0);
    265  1.1  jmcneill 
    266  1.1  jmcneill }
    267  1.1  jmcneill 
    268  1.1  jmcneill void
    269  1.1  jmcneill consinit(void)
    270  1.1  jmcneill {
    271  1.1  jmcneill 	static bool initialized = false;
    272  1.1  jmcneill 	const struct arm_platform *plat = arm_fdt_platform();
    273  1.1  jmcneill 	const struct fdt_console *cons = fdtbus_get_console();
    274  1.1  jmcneill 	struct fdt_attach_args faa;
    275  1.4  jmcneill 	u_int uart_freq = 0;
    276  1.1  jmcneill 
    277  1.1  jmcneill 	if (initialized || cons == NULL)
    278  1.1  jmcneill 		return;
    279  1.1  jmcneill 
    280  1.1  jmcneill 	plat->init_attach_args(&faa);
    281  1.1  jmcneill 	faa.faa_phandle = fdtbus_get_stdout_phandle();
    282  1.1  jmcneill 
    283  1.4  jmcneill 	if (plat->uart_freq != NULL)
    284  1.4  jmcneill 		uart_freq = plat->uart_freq();
    285  1.4  jmcneill 
    286  1.4  jmcneill 	cons->consinit(&faa, uart_freq);
    287  1.1  jmcneill 
    288  1.1  jmcneill 	initialized = true;
    289  1.1  jmcneill }
    290  1.1  jmcneill 
    291  1.3  jmcneill void
    292  1.3  jmcneill delay(u_int us)
    293  1.3  jmcneill {
    294  1.3  jmcneill 	const struct arm_platform *plat = arm_fdt_platform();
    295  1.3  jmcneill 
    296  1.3  jmcneill 	plat->delay(us);
    297  1.3  jmcneill }
    298  1.3  jmcneill 
    299  1.1  jmcneill static void
    300  1.1  jmcneill fdt_device_register(device_t self, void *aux)
    301  1.1  jmcneill {
    302  1.1  jmcneill 	const struct arm_platform *plat = arm_fdt_platform();
    303  1.1  jmcneill 
    304  1.1  jmcneill 	if (plat && plat->device_register)
    305  1.1  jmcneill 		plat->device_register(self, aux);
    306  1.1  jmcneill }
    307  1.1  jmcneill 
    308  1.1  jmcneill static void
    309  1.1  jmcneill fdt_reset(void)
    310  1.1  jmcneill {
    311  1.1  jmcneill 	const struct arm_platform *plat = arm_fdt_platform();
    312  1.1  jmcneill 
    313  1.1  jmcneill 	fdtbus_power_reset();
    314  1.1  jmcneill 
    315  1.1  jmcneill 	if (plat && plat->reset)
    316  1.1  jmcneill 		plat->reset();
    317  1.1  jmcneill }
    318  1.1  jmcneill 
    319  1.1  jmcneill static void
    320  1.1  jmcneill fdt_powerdown(void)
    321  1.1  jmcneill {
    322  1.1  jmcneill 	fdtbus_power_poweroff();
    323  1.1  jmcneill }
    324