Home | History | Annotate | Line # | Download | only in sunxi
sunxi_platform.c revision 1.6
      1 /* $NetBSD: sunxi_platform.c,v 1.6 2017/07/23 10:16:08 jmcneill Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2017 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_fdt_arm.h"
     32 
     33 #include <sys/cdefs.h>
     34 __KERNEL_RCSID(0, "$NetBSD: sunxi_platform.c,v 1.6 2017/07/23 10:16:08 jmcneill 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 #include <arm/cortex/gic_reg.h>
     52 
     53 #include <dev/ic/ns16550reg.h>
     54 #include <dev/ic/comreg.h>
     55 
     56 #include <arm/arm/psci.h>
     57 #include <arm/fdt/psci_fdt.h>
     58 
     59 #include <arm/sunxi/sunxi_platform.h>
     60 
     61 #define	SUNXI_REF_FREQ	24000000
     62 
     63 #define	SUN6I_WDT_BASE		0x01c20ca0
     64 #define	SUN6I_WDT_SIZE		0x20
     65 #define	SUN6I_WDT_CFG		0x14
     66 #define	 SUN6I_WDT_CFG_SYS	1
     67 #define	SUN6I_WDT_MODE		0x18
     68 #define	 SUN6I_WDT_MODE_EN	1
     69 
     70 
     71 #define	DEVMAP_ALIGN(a)	((a) & ~L1_S_OFFSET)
     72 #define	DEVMAP_SIZE(s)	roundup2((s), L1_S_SIZE)
     73 #define	DEVMAP_ENTRY(va, pa, sz)			\
     74 	{						\
     75 		.pd_va = DEVMAP_ALIGN(va),		\
     76 		.pd_pa = DEVMAP_ALIGN(pa),		\
     77 		.pd_size = DEVMAP_SIZE(sz),		\
     78 		.pd_prot = VM_PROT_READ|VM_PROT_WRITE,	\
     79 		.pd_cache = PTE_NOCACHE			\
     80 	}
     81 #define	DEVMAP_ENTRY_END	{ 0 }
     82 
     83 extern struct bus_space armv7_generic_bs_tag;
     84 extern struct bus_space armv7_generic_a4x_bs_tag;
     85 extern struct arm32_bus_dma_tag armv7_generic_dma_tag;
     86 
     87 static const struct pmap_devmap *
     88 sunxi_platform_devmap(void)
     89 {
     90 	static const struct pmap_devmap devmap[] = {
     91 		DEVMAP_ENTRY(SUNXI_CORE_VBASE,
     92 			     SUNXI_CORE_PBASE,
     93 			     SUNXI_CORE_SIZE),
     94 		DEVMAP_ENTRY_END
     95 	};
     96 
     97 	return devmap;
     98 }
     99 
    100 static void
    101 sunxi_platform_init_attach_args(struct fdt_attach_args *faa)
    102 {
    103 	faa->faa_bst = &armv7_generic_bs_tag;
    104 	faa->faa_a4x_bst = &armv7_generic_a4x_bs_tag;
    105 	faa->faa_dmat = &armv7_generic_dma_tag;
    106 }
    107 
    108 static void
    109 sunxi_platform_early_putchar(char c)
    110 {
    111 #ifdef CONSADDR
    112 #define CONSADDR_VA     ((CONSADDR - SUNXI_CORE_PBASE) + SUNXI_CORE_VBASE)
    113 	volatile uint32_t *uartaddr = (volatile uint32_t *)CONSADDR_VA;
    114 
    115 	while ((uartaddr[com_lsr] & LSR_TXRDY) == 0)
    116 		;
    117 
    118 	uartaddr[com_data] = c;
    119 #endif
    120 }
    121 
    122 static void
    123 sunxi_platform_device_register(device_t self, void *aux)
    124 {
    125 }
    126 
    127 static u_int
    128 sunxi_platform_uart_freq(void)
    129 {
    130 	return SUNXI_REF_FREQ;
    131 }
    132 
    133 static void
    134 sun6i_platform_reset(void)
    135 {
    136 	bus_space_tag_t bst = &armv7_generic_bs_tag;
    137 	bus_space_handle_t bsh;
    138 
    139 	bus_space_map(bst, SUN6I_WDT_BASE, SUN6I_WDT_SIZE, 0, &bsh);
    140 
    141 	bus_space_write_4(bst, bsh, SUN6I_WDT_CFG, SUN6I_WDT_CFG_SYS);
    142 	bus_space_write_4(bst, bsh, SUN6I_WDT_MODE, SUN6I_WDT_MODE_EN);
    143 }
    144 
    145 static void
    146 sun50i_platform_bootstrap(void)
    147 {
    148 	/* XXX
    149   	 * This should use psci_fdt_bootstrap, but it hangs
    150   	 * (at least in aarch32 mode)
    151   	 */
    152 }
    153 
    154 static const struct arm_platform sun6i_platform = {
    155 	.devmap = sunxi_platform_devmap,
    156 	.bootstrap = psci_fdt_bootstrap,
    157 	.init_attach_args = sunxi_platform_init_attach_args,
    158 	.early_putchar = sunxi_platform_early_putchar,
    159 	.device_register = sunxi_platform_device_register,
    160 	.reset = sun6i_platform_reset,
    161 	.delay = gtmr_delay,
    162 	.uart_freq = sunxi_platform_uart_freq,
    163 };
    164 
    165 ARM_PLATFORM(sun6i_a31, "allwinner,sun6i-a31", &sun6i_platform);
    166 
    167 static const struct arm_platform sun8i_platform = {
    168 	.devmap = sunxi_platform_devmap,
    169 	.bootstrap = psci_fdt_bootstrap,
    170 	.init_attach_args = sunxi_platform_init_attach_args,
    171 	.early_putchar = sunxi_platform_early_putchar,
    172 	.device_register = sunxi_platform_device_register,
    173 	.reset = sun6i_platform_reset,
    174 	.delay = gtmr_delay,
    175 	.uart_freq = sunxi_platform_uart_freq,
    176 };
    177 
    178 ARM_PLATFORM(sun8i_h2plus, "allwinner,sun8i-h2-plus", &sun8i_platform);
    179 ARM_PLATFORM(sun8i_h3, "allwinner,sun8i-h3", &sun8i_platform);
    180 ARM_PLATFORM(sun8i_a83t, "allwinner,sun8i-a83t", &sun8i_platform);
    181 
    182 static const struct arm_platform sun50i_platform = {
    183 	.devmap = sunxi_platform_devmap,
    184 	.bootstrap = sun50i_platform_bootstrap,
    185 	.init_attach_args = sunxi_platform_init_attach_args,
    186 	.early_putchar = sunxi_platform_early_putchar,
    187 	.device_register = sunxi_platform_device_register,
    188 	.reset = sun6i_platform_reset,
    189 	.delay = gtmr_delay,
    190 	.uart_freq = sunxi_platform_uart_freq,
    191 };
    192 
    193 ARM_PLATFORM(sun50i_a64, "allwinner,sun50i-a64", &sun50i_platform);
    194