Home | History | Annotate | Line # | Download | only in fdt
      1 /* $NetBSD: arm_platform.c,v 1.9 2025/09/06 22:53:47 thorpej Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2020 Jared McNeill <jmcneill (at) invisible.ca>
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  * SUCH DAMAGE.
     27  */
     28 
     29 /*
     30  * This is the default Arm FDT platform implementation. It assumes the
     31  * following:
     32  *
     33  *  - Generic timer
     34  *  - PSCI support
     35  *  - Console UART is pre-configured by firmware
     36  */
     37 
     38 #include "opt_console.h"
     39 
     40 #include <sys/cdefs.h>
     41 __KERNEL_RCSID(0, "$NetBSD: arm_platform.c,v 1.9 2025/09/06 22:53:47 thorpej Exp $");
     42 
     43 #include <sys/param.h>
     44 #include <sys/bus.h>
     45 #include <sys/cpu.h>
     46 #include <sys/device.h>
     47 #include <sys/termios.h>
     48 
     49 #include <dev/fdt/fdtvar.h>
     50 #include <dev/fdt/fdt_console.h>
     51 #include <dev/fdt/fdt_platform.h>
     52 
     53 #include <uvm/uvm_extern.h>
     54 
     55 #include <machine/bootconfig.h>
     56 
     57 #include <arm/cpufunc.h>
     58 
     59 #include <arm/cortex/gtmr_var.h>
     60 
     61 #include <arm/arm/psci.h>
     62 
     63 #include <arm/fdt/arm_fdtvar.h>
     64 #include <arm/fdt/psci_fdtvar.h>
     65 
     66 #include <evbarm/dev/plcomreg.h>
     67 #include <evbarm/dev/plcomvar.h>
     68 
     69 #include <libfdt.h>
     70 
     71 #include <arch/evbarm/fdt/platform.h>
     72 
     73 extern struct arm32_bus_dma_tag arm_generic_dma_tag;
     74 extern struct bus_space arm_generic_bs_tag;
     75 
     76 void plcom_platform_early_putchar(char);
     77 
     78 #define	ARM_PTOV(p)       (((p) - DEVMAP_ALIGN(uart_base)) + KERNEL_IO_VBASE)
     79 
     80 void __noasan
     81 plcom_platform_early_putchar(char c)
     82 {
     83 #ifdef CONSADDR
     84 	bus_addr_t uart_base = CONSADDR;
     85 
     86 	volatile uint32_t *uartaddr = cpu_earlydevice_va_p() ?
     87 		(volatile uint32_t *)ARM_PTOV(uart_base):
     88 		(volatile uint32_t *)uart_base;
     89 
     90 	while ((le32toh(uartaddr[PL01XCOM_FR / 4]) & PL01X_FR_TXFF) != 0)
     91 		continue;
     92 
     93 	uartaddr[PL01XCOM_DR / 4] = htole32(c);
     94 	dsb(sy);
     95 
     96 	while ((le32toh(uartaddr[PL01XCOM_FR / 4]) & PL01X_FR_TXFE) == 0)
     97 		continue;
     98 #endif
     99 }
    100 
    101 static void
    102 arm_platform_init_attach_args(struct fdt_attach_args *faa)
    103 {
    104 	faa->faa_bst = &arm_generic_bs_tag;
    105 	faa->faa_dmat = &arm_generic_dma_tag;
    106 }
    107 
    108 static void
    109 arm_platform_device_register(device_t self, void *aux)
    110 {
    111 }
    112 
    113 static const struct pmap_devmap *
    114 arm_platform_devmap(void)
    115 {
    116 	static const struct pmap_devmap devmap_empty[] = {
    117 		DEVMAP_ENTRY_END
    118 	};
    119 	static struct pmap_devmap devmap_uart[] = {
    120 		DEVMAP_ENTRY(KERNEL_IO_VBASE, 0, PAGE_SIZE),
    121 		DEVMAP_ENTRY_END
    122 	};
    123 
    124 	const int phandle = fdtbus_get_stdout_phandle();
    125 	if (phandle <= 0)
    126 		return devmap_empty;
    127 
    128 	bus_addr_t uart_base;
    129 	if (fdtbus_get_reg(phandle, 0, &uart_base, NULL) != 0)
    130 		return devmap_empty;
    131 
    132 	devmap_uart[0].pd_pa = DEVMAP_ALIGN(uart_base);
    133 
    134 	return devmap_uart;
    135 }
    136 
    137 static u_int
    138 arm_platform_uart_freq(void)
    139 {
    140 	return 0;
    141 }
    142 
    143 static const struct fdt_platform arm_platform = {
    144 	.fp_devmap = arm_platform_devmap,
    145 	.fp_bootstrap = arm_fdt_cpu_bootstrap,
    146 	.fp_init_attach_args = arm_platform_init_attach_args,
    147 	.fp_device_register = arm_platform_device_register,
    148 	.fp_reset = psci_fdt_reset,
    149 	.fp_delay = gtmr_delay,
    150 	.fp_uart_freq = arm_platform_uart_freq,
    151 	.fp_mpstart = arm_fdt_cpu_mpstart,
    152 };
    153 
    154 FDT_PLATFORM(arm, FDT_PLATFORM_DEFAULT, &arm_platform);
    155