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