Home | History | Annotate | Line # | Download | only in rockchip
rk_platform.c revision 1.13
      1  1.13       mrg /* $NetBSD: rk_platform.c,v 1.13 2021/06/23 00:56:41 mrg 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.13       mrg __KERNEL_RCSID(0, "$NetBSD: rk_platform.c,v 1.13 2021/06/23 00:56:41 mrg 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.13       mrg #include <prop/proplib.h>
     43  1.13       mrg 
     44   1.1  jmcneill #include <dev/fdt/fdtvar.h>
     45   1.1  jmcneill #include <arm/fdt/arm_fdtvar.h>
     46   1.1  jmcneill 
     47   1.1  jmcneill #include <uvm/uvm_extern.h>
     48   1.1  jmcneill 
     49   1.1  jmcneill #include <machine/bootconfig.h>
     50   1.1  jmcneill #include <arm/cpufunc.h>
     51   1.1  jmcneill 
     52   1.1  jmcneill #include <arm/cortex/gtmr_var.h>
     53   1.1  jmcneill 
     54   1.1  jmcneill #include <dev/ic/ns16550reg.h>
     55   1.1  jmcneill #include <dev/ic/comreg.h>
     56   1.1  jmcneill 
     57   1.1  jmcneill #include <arm/arm/psci.h>
     58   1.4       ryo #include <arm/fdt/psci_fdtvar.h>
     59   1.1  jmcneill 
     60   1.1  jmcneill #include <libfdt.h>
     61   1.1  jmcneill 
     62   1.1  jmcneill extern struct arm32_bus_dma_tag arm_generic_dma_tag;
     63   1.1  jmcneill extern struct bus_space arm_generic_bs_tag;
     64   1.1  jmcneill 
     65   1.1  jmcneill static void
     66   1.1  jmcneill rk_platform_init_attach_args(struct fdt_attach_args *faa)
     67   1.1  jmcneill {
     68   1.1  jmcneill 	faa->faa_bst = &arm_generic_bs_tag;
     69   1.1  jmcneill 	faa->faa_dmat = &arm_generic_dma_tag;
     70   1.1  jmcneill }
     71   1.1  jmcneill 
     72   1.1  jmcneill static void
     73   1.1  jmcneill rk_platform_device_register(device_t self, void *aux)
     74   1.1  jmcneill {
     75  1.13       mrg 	prop_dictionary_t dict = device_properties(self);
     76  1.13       mrg 
     77  1.13       mrg 	if (device_is_a(self, "ahcisata")) {
     78  1.13       mrg 		/*
     79  1.13       mrg 		 * Marvel 9230 AHCI SATA controllers take between 1213 and 1216
     80  1.13       mrg 		 * milliseconds to reset, exceeding the AHCI spec of 1000.
     81  1.13       mrg 		 */
     82  1.13       mrg 		if (!prop_dictionary_set_uint32(dict, "ahci-reset-ms", 2000))
     83  1.13       mrg 			printf("%s: Failed to set \"ahci-reset-ms\" property"
     84  1.13       mrg 			       " on ahcisata\n", __func__);
     85  1.13       mrg 	}
     86   1.1  jmcneill }
     87   1.1  jmcneill 
     88   1.1  jmcneill static void
     89   1.1  jmcneill rk_platform_bootstrap(void)
     90   1.1  jmcneill {
     91   1.1  jmcneill 	void *fdt_data = __UNCONST(fdtbus_get_data());
     92   1.1  jmcneill 
     93   1.4       ryo 	arm_fdt_cpu_bootstrap();
     94   1.1  jmcneill 
     95   1.1  jmcneill 	const int chosen_off = fdt_path_offset(fdt_data, "/chosen");
     96   1.1  jmcneill 	if (chosen_off < 0)
     97   1.1  jmcneill 		return;
     98   1.1  jmcneill 
     99   1.1  jmcneill 	if (match_bootconf_option(boot_args, "console", "fb")) {
    100   1.1  jmcneill 		const int framebuffer_off =
    101   1.1  jmcneill 		    fdt_path_offset(fdt_data, "/chosen/framebuffer");
    102   1.1  jmcneill 		if (framebuffer_off >= 0) {
    103   1.1  jmcneill 			const char *status = fdt_getprop(fdt_data,
    104   1.1  jmcneill 			    framebuffer_off, "status", NULL);
    105   1.1  jmcneill 			if (status == NULL || strncmp(status, "ok", 2) == 0) {
    106   1.1  jmcneill 				fdt_setprop_string(fdt_data, chosen_off,
    107   1.1  jmcneill 				    "stdout-path", "/chosen/framebuffer");
    108   1.1  jmcneill 			}
    109   1.1  jmcneill 		}
    110   1.1  jmcneill 	} else if (match_bootconf_option(boot_args, "console", "serial")) {
    111   1.1  jmcneill 		fdt_setprop_string(fdt_data, chosen_off,
    112   1.1  jmcneill 		    "stdout-path", "serial0:115200n8");
    113   1.1  jmcneill 	}
    114   1.1  jmcneill }
    115   1.1  jmcneill 
    116   1.1  jmcneill #ifdef SOC_RK3328
    117   1.1  jmcneill 
    118   1.1  jmcneill #include <arm/rockchip/rk3328_platform.h>
    119   1.1  jmcneill 
    120   1.1  jmcneill static const struct pmap_devmap *
    121   1.1  jmcneill rk3328_platform_devmap(void)
    122   1.1  jmcneill {
    123   1.1  jmcneill 	static const struct pmap_devmap devmap[] = {
    124   1.1  jmcneill 		DEVMAP_ENTRY(RK3328_CORE_VBASE,
    125   1.1  jmcneill 			     RK3328_CORE_PBASE,
    126   1.1  jmcneill 			     RK3328_CORE_SIZE),
    127   1.1  jmcneill 		DEVMAP_ENTRY_END
    128   1.1  jmcneill 	};
    129   1.1  jmcneill 
    130   1.1  jmcneill 	return devmap;
    131   1.1  jmcneill }
    132   1.1  jmcneill 
    133   1.1  jmcneill void rk3328_platform_early_putchar(char);
    134   1.1  jmcneill 
    135   1.9     skrll void __noasan
    136   1.1  jmcneill rk3328_platform_early_putchar(char c)
    137   1.1  jmcneill {
    138   1.1  jmcneill #ifdef CONSADDR
    139   1.1  jmcneill #define CONSADDR_VA	((CONSADDR - RK3328_CORE_PBASE) + RK3328_CORE_VBASE)
    140   1.1  jmcneill 	volatile uint32_t *uartaddr = cpu_earlydevice_va_p() ?
    141   1.1  jmcneill 	    (volatile uint32_t *)CONSADDR_VA :
    142   1.1  jmcneill 	    (volatile uint32_t *)CONSADDR;
    143   1.1  jmcneill 
    144   1.1  jmcneill 	while ((le32toh(uartaddr[com_lsr]) & LSR_TXRDY) == 0)
    145   1.1  jmcneill 		;
    146   1.1  jmcneill 
    147   1.1  jmcneill 	uartaddr[com_data] = htole32(c);
    148   1.1  jmcneill #undef CONSADDR_VA
    149   1.1  jmcneill #endif
    150   1.1  jmcneill }
    151   1.1  jmcneill 
    152   1.1  jmcneill static u_int
    153   1.1  jmcneill rk3328_platform_uart_freq(void)
    154   1.1  jmcneill {
    155   1.1  jmcneill 	return RK3328_UART_FREQ;
    156   1.1  jmcneill }
    157   1.1  jmcneill 
    158   1.1  jmcneill static const struct arm_platform rk3328_platform = {
    159   1.2     skrll 	.ap_devmap = rk3328_platform_devmap,
    160   1.2     skrll 	.ap_bootstrap = rk_platform_bootstrap,
    161   1.2     skrll 	.ap_init_attach_args = rk_platform_init_attach_args,
    162   1.2     skrll 	.ap_device_register = rk_platform_device_register,
    163   1.2     skrll 	.ap_reset = psci_fdt_reset,
    164   1.2     skrll 	.ap_delay = gtmr_delay,
    165   1.2     skrll 	.ap_uart_freq = rk3328_platform_uart_freq,
    166   1.6     skrll 	.ap_mpstart = arm_fdt_cpu_mpstart,
    167   1.1  jmcneill };
    168   1.1  jmcneill 
    169   1.1  jmcneill ARM_PLATFORM(rk3328, "rockchip,rk3328", &rk3328_platform);
    170   1.1  jmcneill 
    171   1.1  jmcneill #endif /* SOC_RK3328 */
    172   1.3  jmcneill 
    173   1.3  jmcneill 
    174   1.3  jmcneill #ifdef SOC_RK3399
    175   1.3  jmcneill 
    176   1.3  jmcneill #include <arm/rockchip/rk3399_platform.h>
    177   1.3  jmcneill 
    178   1.3  jmcneill static const struct pmap_devmap *
    179   1.3  jmcneill rk3399_platform_devmap(void)
    180   1.3  jmcneill {
    181   1.3  jmcneill 	static const struct pmap_devmap devmap[] = {
    182   1.3  jmcneill 		DEVMAP_ENTRY(RK3399_CORE_VBASE,
    183   1.3  jmcneill 			     RK3399_CORE_PBASE,
    184   1.3  jmcneill 			     RK3399_CORE_SIZE),
    185   1.3  jmcneill 		DEVMAP_ENTRY_END
    186   1.3  jmcneill 	};
    187   1.3  jmcneill 
    188   1.3  jmcneill 	return devmap;
    189   1.3  jmcneill }
    190   1.3  jmcneill 
    191   1.3  jmcneill void rk3399_platform_early_putchar(char);
    192   1.3  jmcneill 
    193   1.3  jmcneill void
    194   1.3  jmcneill rk3399_platform_early_putchar(char c)
    195   1.3  jmcneill {
    196   1.3  jmcneill #ifdef CONSADDR
    197   1.3  jmcneill #define CONSADDR_VA	((CONSADDR - RK3399_CORE_PBASE) + RK3399_CORE_VBASE)
    198   1.3  jmcneill 	volatile uint32_t *uartaddr = cpu_earlydevice_va_p() ?
    199   1.3  jmcneill 	    (volatile uint32_t *)CONSADDR_VA :
    200   1.3  jmcneill 	    (volatile uint32_t *)CONSADDR;
    201   1.3  jmcneill 
    202   1.3  jmcneill 	while ((le32toh(uartaddr[com_lsr]) & LSR_TXRDY) == 0)
    203   1.3  jmcneill 		;
    204   1.3  jmcneill 
    205   1.3  jmcneill 	uartaddr[com_data] = htole32(c);
    206   1.3  jmcneill #undef CONSADDR_VA
    207   1.3  jmcneill #endif
    208   1.3  jmcneill }
    209   1.3  jmcneill 
    210   1.3  jmcneill static u_int
    211   1.3  jmcneill rk3399_platform_uart_freq(void)
    212   1.3  jmcneill {
    213   1.3  jmcneill 	return RK3399_UART_FREQ;
    214   1.3  jmcneill }
    215   1.3  jmcneill 
    216   1.3  jmcneill static const struct arm_platform rk3399_platform = {
    217   1.3  jmcneill 	.ap_devmap = rk3399_platform_devmap,
    218   1.3  jmcneill 	.ap_bootstrap = rk_platform_bootstrap,
    219   1.3  jmcneill 	.ap_init_attach_args = rk_platform_init_attach_args,
    220   1.3  jmcneill 	.ap_device_register = rk_platform_device_register,
    221   1.3  jmcneill 	.ap_reset = psci_fdt_reset,
    222   1.3  jmcneill 	.ap_delay = gtmr_delay,
    223   1.3  jmcneill 	.ap_uart_freq = rk3399_platform_uart_freq,
    224   1.7       ryo 	.ap_mpstart = arm_fdt_cpu_mpstart,
    225   1.3  jmcneill };
    226   1.3  jmcneill 
    227   1.3  jmcneill ARM_PLATFORM(rk3399, "rockchip,rk3399", &rk3399_platform);
    228   1.3  jmcneill 
    229   1.3  jmcneill #endif /* SOC_RK3399 */
    230