1 /* $NetBSD: rk_platform.c,v 1.11 2021/02/04 22:36:53 thorpej Exp $ */ 2 3 /*- 4 * Copyright (c) 2018 Jared McNeill <jmcneill@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 #include "opt_soc.h" 30 #include "opt_multiprocessor.h" 31 #include "opt_console.h" 32 33 #include <sys/cdefs.h> 34 __KERNEL_RCSID(0, "$NetBSD: rk_platform.c,v 1.11 2021/02/04 22:36:53 thorpej Exp $"); 35 36 #include <sys/param.h> 37 #include <sys/bus.h> 38 #include <sys/cpu.h> 39 #include <sys/device.h> 40 #include <sys/termios.h> 41 42 #include <dev/fdt/fdtvar.h> 43 #include <arm/fdt/arm_fdtvar.h> 44 45 #include <uvm/uvm_extern.h> 46 47 #include <machine/bootconfig.h> 48 #include <arm/cpufunc.h> 49 50 #include <arm/cortex/gtmr_var.h> 51 52 #include <dev/ic/ns16550reg.h> 53 #include <dev/ic/comreg.h> 54 55 #include <arm/arm/psci.h> 56 #include <arm/fdt/psci_fdtvar.h> 57 58 #include <libfdt.h> 59 60 extern struct arm32_bus_dma_tag arm_generic_dma_tag; 61 extern struct bus_space arm_generic_bs_tag; 62 63 static void 64 rk_platform_init_attach_args(struct fdt_attach_args *faa) 65 { 66 faa->faa_bst = &arm_generic_bs_tag; 67 faa->faa_dmat = &arm_generic_dma_tag; 68 } 69 70 static void 71 rk_platform_device_register(device_t self, void *aux) 72 { 73 fdtbus_device_register(self, aux); 74 } 75 76 static void 77 rk_platform_bootstrap(void) 78 { 79 void *fdt_data = __UNCONST(fdtbus_get_data()); 80 81 arm_fdt_cpu_bootstrap(); 82 83 const int chosen_off = fdt_path_offset(fdt_data, "/chosen"); 84 if (chosen_off < 0) 85 return; 86 87 if (match_bootconf_option(boot_args, "console", "fb")) { 88 const int framebuffer_off = 89 fdt_path_offset(fdt_data, "/chosen/framebuffer"); 90 if (framebuffer_off >= 0) { 91 const char *status = fdt_getprop(fdt_data, 92 framebuffer_off, "status", NULL); 93 if (status == NULL || strncmp(status, "ok", 2) == 0) { 94 fdt_setprop_string(fdt_data, chosen_off, 95 "stdout-path", "/chosen/framebuffer"); 96 } 97 } 98 } else if (match_bootconf_option(boot_args, "console", "serial")) { 99 fdt_setprop_string(fdt_data, chosen_off, 100 "stdout-path", "serial0:115200n8"); 101 } 102 } 103 104 #ifdef SOC_RK3328 105 106 #include <arm/rockchip/rk3328_platform.h> 107 108 static const struct pmap_devmap * 109 rk3328_platform_devmap(void) 110 { 111 static const struct pmap_devmap devmap[] = { 112 DEVMAP_ENTRY(RK3328_CORE_VBASE, 113 RK3328_CORE_PBASE, 114 RK3328_CORE_SIZE), 115 DEVMAP_ENTRY_END 116 }; 117 118 return devmap; 119 } 120 121 void rk3328_platform_early_putchar(char); 122 123 void __noasan 124 rk3328_platform_early_putchar(char c) 125 { 126 #ifdef CONSADDR 127 #define CONSADDR_VA ((CONSADDR - RK3328_CORE_PBASE) + RK3328_CORE_VBASE) 128 volatile uint32_t *uartaddr = cpu_earlydevice_va_p() ? 129 (volatile uint32_t *)CONSADDR_VA : 130 (volatile uint32_t *)CONSADDR; 131 132 while ((le32toh(uartaddr[com_lsr]) & LSR_TXRDY) == 0) 133 ; 134 135 uartaddr[com_data] = htole32(c); 136 #undef CONSADDR_VA 137 #endif 138 } 139 140 static u_int 141 rk3328_platform_uart_freq(void) 142 { 143 return RK3328_UART_FREQ; 144 } 145 146 static const struct arm_platform rk3328_platform = { 147 .ap_devmap = rk3328_platform_devmap, 148 .ap_bootstrap = rk_platform_bootstrap, 149 .ap_init_attach_args = rk_platform_init_attach_args, 150 .ap_device_register = rk_platform_device_register, 151 .ap_reset = psci_fdt_reset, 152 .ap_delay = gtmr_delay, 153 .ap_uart_freq = rk3328_platform_uart_freq, 154 .ap_mpstart = arm_fdt_cpu_mpstart, 155 }; 156 157 ARM_PLATFORM(rk3328, "rockchip,rk3328", &rk3328_platform); 158 159 #endif /* SOC_RK3328 */ 160 161 162 #ifdef SOC_RK3399 163 164 #include <arm/rockchip/rk3399_platform.h> 165 166 static const struct pmap_devmap * 167 rk3399_platform_devmap(void) 168 { 169 static const struct pmap_devmap devmap[] = { 170 DEVMAP_ENTRY(RK3399_CORE_VBASE, 171 RK3399_CORE_PBASE, 172 RK3399_CORE_SIZE), 173 DEVMAP_ENTRY_END 174 }; 175 176 return devmap; 177 } 178 179 void rk3399_platform_early_putchar(char); 180 181 void 182 rk3399_platform_early_putchar(char c) 183 { 184 #ifdef CONSADDR 185 #define CONSADDR_VA ((CONSADDR - RK3399_CORE_PBASE) + RK3399_CORE_VBASE) 186 volatile uint32_t *uartaddr = cpu_earlydevice_va_p() ? 187 (volatile uint32_t *)CONSADDR_VA : 188 (volatile uint32_t *)CONSADDR; 189 190 while ((le32toh(uartaddr[com_lsr]) & LSR_TXRDY) == 0) 191 ; 192 193 uartaddr[com_data] = htole32(c); 194 #undef CONSADDR_VA 195 #endif 196 } 197 198 static u_int 199 rk3399_platform_uart_freq(void) 200 { 201 return RK3399_UART_FREQ; 202 } 203 204 static const struct arm_platform rk3399_platform = { 205 .ap_devmap = rk3399_platform_devmap, 206 .ap_bootstrap = rk_platform_bootstrap, 207 .ap_init_attach_args = rk_platform_init_attach_args, 208 .ap_device_register = rk_platform_device_register, 209 .ap_reset = psci_fdt_reset, 210 .ap_delay = gtmr_delay, 211 .ap_uart_freq = rk3399_platform_uart_freq, 212 .ap_mpstart = arm_fdt_cpu_mpstart, 213 }; 214 215 ARM_PLATFORM(rk3399, "rockchip,rk3399", &rk3399_platform); 216 217 #endif /* SOC_RK3399 */ 218