Home | History | Annotate | Line # | Download | only in fdt
arm_platform.c revision 1.6
      1  1.6     skrll /* $NetBSD: arm_platform.c,v 1.6 2023/02/25 08:19:35 skrll Exp $ */
      2  1.1  jmcneill 
      3  1.1  jmcneill /*-
      4  1.1  jmcneill  * Copyright (c) 2020 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 /*
     30  1.1  jmcneill  * This is the default Arm FDT platform implementation. It assumes the
     31  1.1  jmcneill  * following:
     32  1.1  jmcneill  *
     33  1.1  jmcneill  *  - Generic timer
     34  1.1  jmcneill  *  - PSCI support
     35  1.1  jmcneill  *  - Console UART is pre-configured by firmware
     36  1.1  jmcneill  */
     37  1.1  jmcneill 
     38  1.6     skrll #include "opt_console.h"
     39  1.6     skrll 
     40  1.1  jmcneill #include <sys/cdefs.h>
     41  1.6     skrll __KERNEL_RCSID(0, "$NetBSD: arm_platform.c,v 1.6 2023/02/25 08:19:35 skrll Exp $");
     42  1.1  jmcneill 
     43  1.1  jmcneill #include <sys/param.h>
     44  1.1  jmcneill #include <sys/bus.h>
     45  1.1  jmcneill #include <sys/cpu.h>
     46  1.1  jmcneill #include <sys/device.h>
     47  1.1  jmcneill #include <sys/termios.h>
     48  1.1  jmcneill 
     49  1.1  jmcneill #include <dev/fdt/fdtvar.h>
     50  1.1  jmcneill #include <arm/fdt/arm_fdtvar.h>
     51  1.1  jmcneill 
     52  1.1  jmcneill #include <uvm/uvm_extern.h>
     53  1.1  jmcneill 
     54  1.1  jmcneill #include <machine/bootconfig.h>
     55  1.2     skrll 
     56  1.1  jmcneill #include <arm/cpufunc.h>
     57  1.1  jmcneill 
     58  1.1  jmcneill #include <arm/cortex/gtmr_var.h>
     59  1.1  jmcneill 
     60  1.1  jmcneill #include <arm/arm/psci.h>
     61  1.1  jmcneill #include <arm/fdt/psci_fdtvar.h>
     62  1.1  jmcneill 
     63  1.6     skrll #include <evbarm/dev/plcomreg.h>
     64  1.6     skrll #include <evbarm/dev/plcomvar.h>
     65  1.6     skrll 
     66  1.1  jmcneill #include <libfdt.h>
     67  1.1  jmcneill 
     68  1.1  jmcneill #include <arch/evbarm/fdt/platform.h>
     69  1.1  jmcneill 
     70  1.1  jmcneill extern struct arm32_bus_dma_tag arm_generic_dma_tag;
     71  1.1  jmcneill extern struct bus_space arm_generic_bs_tag;
     72  1.1  jmcneill 
     73  1.6     skrll void plcom_platform_early_putchar(char);
     74  1.6     skrll 
     75  1.6     skrll #define	ARM_PTOV(p)       (((p) - DEVMAP_ALIGN(uart_base)) + KERNEL_IO_VBASE)
     76  1.6     skrll 
     77  1.6     skrll void __noasan
     78  1.6     skrll plcom_platform_early_putchar(char c)
     79  1.6     skrll {
     80  1.6     skrll #ifdef CONSADDR
     81  1.6     skrll 	bus_addr_t uart_base = CONSADDR;
     82  1.6     skrll 
     83  1.6     skrll 	volatile uint32_t *uartaddr = cpu_earlydevice_va_p() ?
     84  1.6     skrll 		(volatile uint32_t *)ARM_PTOV(uart_base):
     85  1.6     skrll 		(volatile uint32_t *)uart_base;
     86  1.6     skrll 
     87  1.6     skrll 	while ((le32toh(uartaddr[PL01XCOM_FR / 4]) & PL01X_FR_TXFF) != 0)
     88  1.6     skrll 		continue;
     89  1.6     skrll 
     90  1.6     skrll 	uartaddr[PL01XCOM_DR / 4] = htole32(c);
     91  1.6     skrll 	dsb(sy);
     92  1.6     skrll 
     93  1.6     skrll 	while ((le32toh(uartaddr[PL01XCOM_FR / 4]) & PL01X_FR_TXFE) == 0)
     94  1.6     skrll 		continue;
     95  1.6     skrll #endif
     96  1.6     skrll }
     97  1.6     skrll 
     98  1.1  jmcneill static void
     99  1.1  jmcneill arm_platform_init_attach_args(struct fdt_attach_args *faa)
    100  1.1  jmcneill {
    101  1.1  jmcneill 	faa->faa_bst = &arm_generic_bs_tag;
    102  1.1  jmcneill 	faa->faa_dmat = &arm_generic_dma_tag;
    103  1.1  jmcneill }
    104  1.1  jmcneill 
    105  1.1  jmcneill static void
    106  1.1  jmcneill arm_platform_device_register(device_t self, void *aux)
    107  1.1  jmcneill {
    108  1.1  jmcneill }
    109  1.1  jmcneill 
    110  1.1  jmcneill static const struct pmap_devmap *
    111  1.1  jmcneill arm_platform_devmap(void)
    112  1.1  jmcneill {
    113  1.1  jmcneill 	static const struct pmap_devmap devmap_empty[] = {
    114  1.1  jmcneill 		DEVMAP_ENTRY_END
    115  1.1  jmcneill 	};
    116  1.1  jmcneill 	static struct pmap_devmap devmap_uart[] = {
    117  1.2     skrll 		DEVMAP_ENTRY(KERNEL_IO_VBASE, 0, PAGE_SIZE),
    118  1.1  jmcneill 		DEVMAP_ENTRY_END
    119  1.1  jmcneill 	};
    120  1.1  jmcneill 
    121  1.1  jmcneill 	const int phandle = fdtbus_get_stdout_phandle();
    122  1.1  jmcneill 	if (phandle <= 0)
    123  1.1  jmcneill 		return devmap_empty;
    124  1.1  jmcneill 
    125  1.6     skrll 	bus_addr_t uart_base;
    126  1.1  jmcneill 	if (fdtbus_get_reg(phandle, 0, &uart_base, NULL) != 0)
    127  1.1  jmcneill 		return devmap_empty;
    128  1.1  jmcneill 
    129  1.2     skrll 	devmap_uart[0].pd_pa = DEVMAP_ALIGN(uart_base);
    130  1.1  jmcneill 
    131  1.1  jmcneill 	return devmap_uart;
    132  1.1  jmcneill }
    133  1.1  jmcneill 
    134  1.1  jmcneill static u_int
    135  1.1  jmcneill arm_platform_uart_freq(void)
    136  1.1  jmcneill {
    137  1.1  jmcneill 	return 0;
    138  1.1  jmcneill }
    139  1.1  jmcneill 
    140  1.1  jmcneill static const struct arm_platform arm_platform = {
    141  1.1  jmcneill 	.ap_devmap = arm_platform_devmap,
    142  1.1  jmcneill 	.ap_bootstrap = arm_fdt_cpu_bootstrap,
    143  1.1  jmcneill 	.ap_init_attach_args = arm_platform_init_attach_args,
    144  1.1  jmcneill 	.ap_device_register = arm_platform_device_register,
    145  1.1  jmcneill 	.ap_reset = psci_fdt_reset,
    146  1.1  jmcneill 	.ap_delay = gtmr_delay,
    147  1.1  jmcneill 	.ap_uart_freq = arm_platform_uart_freq,
    148  1.1  jmcneill 	.ap_mpstart = arm_fdt_cpu_mpstart,
    149  1.1  jmcneill };
    150  1.1  jmcneill 
    151  1.1  jmcneill ARM_PLATFORM(arm, ARM_PLATFORM_DEFAULT, &arm_platform);
    152