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