1 1.9 thorpej /* $NetBSD: arm_platform.c,v 1.9 2025/09/06 22:53:47 thorpej 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.9 thorpej __KERNEL_RCSID(0, "$NetBSD: arm_platform.c,v 1.9 2025/09/06 22:53:47 thorpej 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.9 thorpej #include <dev/fdt/fdt_console.h> 51 1.8 thorpej #include <dev/fdt/fdt_platform.h> 52 1.1 jmcneill 53 1.1 jmcneill #include <uvm/uvm_extern.h> 54 1.1 jmcneill 55 1.1 jmcneill #include <machine/bootconfig.h> 56 1.2 skrll 57 1.1 jmcneill #include <arm/cpufunc.h> 58 1.1 jmcneill 59 1.1 jmcneill #include <arm/cortex/gtmr_var.h> 60 1.1 jmcneill 61 1.1 jmcneill #include <arm/arm/psci.h> 62 1.7 skrll 63 1.7 skrll #include <arm/fdt/arm_fdtvar.h> 64 1.1 jmcneill #include <arm/fdt/psci_fdtvar.h> 65 1.1 jmcneill 66 1.6 skrll #include <evbarm/dev/plcomreg.h> 67 1.6 skrll #include <evbarm/dev/plcomvar.h> 68 1.6 skrll 69 1.1 jmcneill #include <libfdt.h> 70 1.1 jmcneill 71 1.1 jmcneill #include <arch/evbarm/fdt/platform.h> 72 1.1 jmcneill 73 1.1 jmcneill extern struct arm32_bus_dma_tag arm_generic_dma_tag; 74 1.1 jmcneill extern struct bus_space arm_generic_bs_tag; 75 1.1 jmcneill 76 1.6 skrll void plcom_platform_early_putchar(char); 77 1.6 skrll 78 1.6 skrll #define ARM_PTOV(p) (((p) - DEVMAP_ALIGN(uart_base)) + KERNEL_IO_VBASE) 79 1.6 skrll 80 1.6 skrll void __noasan 81 1.6 skrll plcom_platform_early_putchar(char c) 82 1.6 skrll { 83 1.6 skrll #ifdef CONSADDR 84 1.6 skrll bus_addr_t uart_base = CONSADDR; 85 1.6 skrll 86 1.6 skrll volatile uint32_t *uartaddr = cpu_earlydevice_va_p() ? 87 1.6 skrll (volatile uint32_t *)ARM_PTOV(uart_base): 88 1.6 skrll (volatile uint32_t *)uart_base; 89 1.6 skrll 90 1.6 skrll while ((le32toh(uartaddr[PL01XCOM_FR / 4]) & PL01X_FR_TXFF) != 0) 91 1.6 skrll continue; 92 1.6 skrll 93 1.6 skrll uartaddr[PL01XCOM_DR / 4] = htole32(c); 94 1.6 skrll dsb(sy); 95 1.6 skrll 96 1.6 skrll while ((le32toh(uartaddr[PL01XCOM_FR / 4]) & PL01X_FR_TXFE) == 0) 97 1.6 skrll continue; 98 1.6 skrll #endif 99 1.6 skrll } 100 1.6 skrll 101 1.1 jmcneill static void 102 1.1 jmcneill arm_platform_init_attach_args(struct fdt_attach_args *faa) 103 1.1 jmcneill { 104 1.1 jmcneill faa->faa_bst = &arm_generic_bs_tag; 105 1.1 jmcneill faa->faa_dmat = &arm_generic_dma_tag; 106 1.1 jmcneill } 107 1.1 jmcneill 108 1.1 jmcneill static void 109 1.1 jmcneill arm_platform_device_register(device_t self, void *aux) 110 1.1 jmcneill { 111 1.1 jmcneill } 112 1.1 jmcneill 113 1.1 jmcneill static const struct pmap_devmap * 114 1.1 jmcneill arm_platform_devmap(void) 115 1.1 jmcneill { 116 1.1 jmcneill static const struct pmap_devmap devmap_empty[] = { 117 1.1 jmcneill DEVMAP_ENTRY_END 118 1.1 jmcneill }; 119 1.1 jmcneill static struct pmap_devmap devmap_uart[] = { 120 1.2 skrll DEVMAP_ENTRY(KERNEL_IO_VBASE, 0, PAGE_SIZE), 121 1.1 jmcneill DEVMAP_ENTRY_END 122 1.1 jmcneill }; 123 1.1 jmcneill 124 1.1 jmcneill const int phandle = fdtbus_get_stdout_phandle(); 125 1.1 jmcneill if (phandle <= 0) 126 1.1 jmcneill return devmap_empty; 127 1.1 jmcneill 128 1.6 skrll bus_addr_t uart_base; 129 1.1 jmcneill if (fdtbus_get_reg(phandle, 0, &uart_base, NULL) != 0) 130 1.1 jmcneill return devmap_empty; 131 1.1 jmcneill 132 1.2 skrll devmap_uart[0].pd_pa = DEVMAP_ALIGN(uart_base); 133 1.1 jmcneill 134 1.1 jmcneill return devmap_uart; 135 1.1 jmcneill } 136 1.1 jmcneill 137 1.1 jmcneill static u_int 138 1.1 jmcneill arm_platform_uart_freq(void) 139 1.1 jmcneill { 140 1.1 jmcneill return 0; 141 1.1 jmcneill } 142 1.1 jmcneill 143 1.7 skrll static const struct fdt_platform arm_platform = { 144 1.7 skrll .fp_devmap = arm_platform_devmap, 145 1.7 skrll .fp_bootstrap = arm_fdt_cpu_bootstrap, 146 1.7 skrll .fp_init_attach_args = arm_platform_init_attach_args, 147 1.7 skrll .fp_device_register = arm_platform_device_register, 148 1.7 skrll .fp_reset = psci_fdt_reset, 149 1.7 skrll .fp_delay = gtmr_delay, 150 1.7 skrll .fp_uart_freq = arm_platform_uart_freq, 151 1.7 skrll .fp_mpstart = arm_fdt_cpu_mpstart, 152 1.1 jmcneill }; 153 1.1 jmcneill 154 1.7 skrll FDT_PLATFORM(arm, FDT_PLATFORM_DEFAULT, &arm_platform); 155