1 /* $NetBSD: rk_platform.c,v 1.9 2020/07/10 12:25:09 skrll 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.9 2020/07/10 12:25:09 skrll 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 extern struct bus_space arm_generic_a4x_bs_tag; 63 64 static void 65 rk_platform_init_attach_args(struct fdt_attach_args *faa) 66 { 67 faa->faa_bst = &arm_generic_bs_tag; 68 faa->faa_a4x_bst = &arm_generic_a4x_bs_tag; 69 faa->faa_dmat = &arm_generic_dma_tag; 70 } 71 72 static void 73 rk_platform_device_register(device_t self, void *aux) 74 { 75 } 76 77 static void 78 rk_platform_bootstrap(void) 79 { 80 void *fdt_data = __UNCONST(fdtbus_get_data()); 81 82 arm_fdt_cpu_bootstrap(); 83 84 const int chosen_off = fdt_path_offset(fdt_data, "/chosen"); 85 if (chosen_off < 0) 86 return; 87 88 if (match_bootconf_option(boot_args, "console", "fb")) { 89 const int framebuffer_off = 90 fdt_path_offset(fdt_data, "/chosen/framebuffer"); 91 if (framebuffer_off >= 0) { 92 const char *status = fdt_getprop(fdt_data, 93 framebuffer_off, "status", NULL); 94 if (status == NULL || strncmp(status, "ok", 2) == 0) { 95 fdt_setprop_string(fdt_data, chosen_off, 96 "stdout-path", "/chosen/framebuffer"); 97 } 98 } 99 } else if (match_bootconf_option(boot_args, "console", "serial")) { 100 fdt_setprop_string(fdt_data, chosen_off, 101 "stdout-path", "serial0:115200n8"); 102 } 103 } 104 105 #ifdef SOC_RK3328 106 107 #include <arm/rockchip/rk3328_platform.h> 108 109 static const struct pmap_devmap * 110 rk3328_platform_devmap(void) 111 { 112 static const struct pmap_devmap devmap[] = { 113 DEVMAP_ENTRY(RK3328_CORE_VBASE, 114 RK3328_CORE_PBASE, 115 RK3328_CORE_SIZE), 116 DEVMAP_ENTRY_END 117 }; 118 119 return devmap; 120 } 121 122 void rk3328_platform_early_putchar(char); 123 124 void __noasan 125 rk3328_platform_early_putchar(char c) 126 { 127 #ifdef CONSADDR 128 #define CONSADDR_VA ((CONSADDR - RK3328_CORE_PBASE) + RK3328_CORE_VBASE) 129 volatile uint32_t *uartaddr = cpu_earlydevice_va_p() ? 130 (volatile uint32_t *)CONSADDR_VA : 131 (volatile uint32_t *)CONSADDR; 132 133 while ((le32toh(uartaddr[com_lsr]) & LSR_TXRDY) == 0) 134 ; 135 136 uartaddr[com_data] = htole32(c); 137 #undef CONSADDR_VA 138 #endif 139 } 140 141 static u_int 142 rk3328_platform_uart_freq(void) 143 { 144 return RK3328_UART_FREQ; 145 } 146 147 static const struct arm_platform rk3328_platform = { 148 .ap_devmap = rk3328_platform_devmap, 149 .ap_bootstrap = rk_platform_bootstrap, 150 .ap_init_attach_args = rk_platform_init_attach_args, 151 .ap_device_register = rk_platform_device_register, 152 .ap_reset = psci_fdt_reset, 153 .ap_delay = gtmr_delay, 154 .ap_uart_freq = rk3328_platform_uart_freq, 155 .ap_mpstart = arm_fdt_cpu_mpstart, 156 }; 157 158 ARM_PLATFORM(rk3328, "rockchip,rk3328", &rk3328_platform); 159 160 #endif /* SOC_RK3328 */ 161 162 163 #ifdef SOC_RK3399 164 165 #include <arm/rockchip/rk3399_platform.h> 166 167 static const struct pmap_devmap * 168 rk3399_platform_devmap(void) 169 { 170 static const struct pmap_devmap devmap[] = { 171 DEVMAP_ENTRY(RK3399_CORE_VBASE, 172 RK3399_CORE_PBASE, 173 RK3399_CORE_SIZE), 174 DEVMAP_ENTRY_END 175 }; 176 177 return devmap; 178 } 179 180 void rk3399_platform_early_putchar(char); 181 182 void 183 rk3399_platform_early_putchar(char c) 184 { 185 #ifdef CONSADDR 186 #define CONSADDR_VA ((CONSADDR - RK3399_CORE_PBASE) + RK3399_CORE_VBASE) 187 volatile uint32_t *uartaddr = cpu_earlydevice_va_p() ? 188 (volatile uint32_t *)CONSADDR_VA : 189 (volatile uint32_t *)CONSADDR; 190 191 while ((le32toh(uartaddr[com_lsr]) & LSR_TXRDY) == 0) 192 ; 193 194 uartaddr[com_data] = htole32(c); 195 #undef CONSADDR_VA 196 #endif 197 } 198 199 static u_int 200 rk3399_platform_uart_freq(void) 201 { 202 return RK3399_UART_FREQ; 203 } 204 205 static const struct arm_platform rk3399_platform = { 206 .ap_devmap = rk3399_platform_devmap, 207 .ap_bootstrap = rk_platform_bootstrap, 208 .ap_init_attach_args = rk_platform_init_attach_args, 209 .ap_device_register = rk_platform_device_register, 210 .ap_reset = psci_fdt_reset, 211 .ap_delay = gtmr_delay, 212 .ap_uart_freq = rk3399_platform_uart_freq, 213 .ap_mpstart = arm_fdt_cpu_mpstart, 214 }; 215 216 ARM_PLATFORM(rk3399, "rockchip,rk3399", &rk3399_platform); 217 218 #endif /* SOC_RK3399 */ 219