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