Home | History | Annotate | Line # | Download | only in sunxi
sunxi_platform.c revision 1.14
      1 /* $NetBSD: sunxi_platform.c,v 1.14 2017/10/23 19:36:01 jakllsch 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.14 2017/10/23 19:36:01 jakllsch 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 #include <libfdt.h>
     62 
     63 #define	SUNXI_REF_FREQ	24000000
     64 
     65 #define	SUN4I_TIMER_BASE	0x01c20c00
     66 #define	SUN4I_TIMER_SIZE	0x90
     67 #define	SUN4I_TIMER_0_VAL	0x18
     68 
     69 #define	SUN4I_WDT_BASE		0x01c20c90
     70 #define	SUN4I_WDT_SIZE		0x10
     71 #define	SUN4I_WDT_CTRL		0x00
     72 #define	 SUN4I_WDT_CTRL_KEY	(0x333 << 1)
     73 #define	 SUN4I_WDT_CTRL_RESTART	__BIT(0)
     74 #define	SUN4I_WDT_MODE		0x04
     75 #define	 SUN4I_WDT_MODE_RST_EN	__BIT(1)
     76 #define	 SUN4I_WDT_MODE_EN	__BIT(0)
     77 
     78 #define	SUN6I_WDT_BASE		0x01c20ca0
     79 #define	SUN6I_WDT_SIZE		0x20
     80 #define	SUN6I_WDT_CFG		0x14
     81 #define	 SUN6I_WDT_CFG_SYS	__BIT(0)
     82 #define	SUN6I_WDT_MODE		0x18
     83 #define	 SUN6I_WDT_MODE_EN	__BIT(0)
     84 
     85 #define	SUN9I_WDT_BASE		0x06000ca0
     86 #define	SUN9I_WDT_SIZE		0x20
     87 #define	SUN9I_WDT_CFG		0x14
     88 #define	 SUN9I_WDT_CFG_SYS	__BIT(0)
     89 #define	SUN9I_WDT_MODE		0x18
     90 #define	 SUN9I_WDT_MODE_EN	__BIT(0)
     91 
     92 extern struct bus_space armv7_generic_bs_tag;
     93 extern struct bus_space armv7_generic_a4x_bs_tag;
     94 extern struct arm32_bus_dma_tag armv7_generic_dma_tag;
     95 
     96 static const struct pmap_devmap *
     97 sunxi_platform_devmap(void)
     98 {
     99 	static const struct pmap_devmap devmap[] = {
    100 		DEVMAP_ENTRY(SUNXI_CORE_VBASE,
    101 			     SUNXI_CORE_PBASE,
    102 			     SUNXI_CORE_SIZE),
    103 		DEVMAP_ENTRY_END
    104 	};
    105 
    106 	return devmap;
    107 }
    108 
    109 static void
    110 sunxi_platform_init_attach_args(struct fdt_attach_args *faa)
    111 {
    112 	faa->faa_bst = &armv7_generic_bs_tag;
    113 	faa->faa_a4x_bst = &armv7_generic_a4x_bs_tag;
    114 	faa->faa_dmat = &armv7_generic_dma_tag;
    115 }
    116 
    117 static void
    118 sunxi_platform_early_putchar(char c)
    119 {
    120 #ifdef CONSADDR
    121 #define CONSADDR_VA     ((CONSADDR - SUNXI_CORE_PBASE) + SUNXI_CORE_VBASE)
    122 	volatile uint32_t *uartaddr = (volatile uint32_t *)CONSADDR_VA;
    123 
    124 	while ((le32toh(uartaddr[com_lsr]) & LSR_TXRDY) == 0)
    125 		;
    126 
    127 	uartaddr[com_data] = htole32(c);
    128 #endif
    129 }
    130 
    131 static void
    132 sunxi_platform_device_register(device_t self, void *aux)
    133 {
    134 }
    135 
    136 static u_int
    137 sunxi_platform_uart_freq(void)
    138 {
    139 	return SUNXI_REF_FREQ;
    140 }
    141 
    142 static void
    143 sunxi_platform_bootstrap(void)
    144 {
    145 	if (match_bootconf_option(boot_args, "console", "fb")) {
    146 		void *fdt_data = __UNCONST(fdtbus_get_data());
    147 		const int chosen_off = fdt_path_offset(fdt_data, "/chosen");
    148 		const int framebuffer_off =
    149 		    fdt_path_offset(fdt_data, "/chosen/framebuffer");
    150 		if (chosen_off >= 0 && framebuffer_off >= 0)
    151 			fdt_setprop_string(fdt_data, chosen_off, "stdout-path",
    152 			    "/chosen/framebuffer");
    153 	}
    154 }
    155 
    156 static void
    157 sunxi_platform_psci_bootstrap(void)
    158 {
    159 	psci_fdt_bootstrap();
    160 	sunxi_platform_bootstrap();
    161 }
    162 
    163 static void
    164 sun4i_platform_reset(void)
    165 {
    166 	bus_space_tag_t bst = &armv7_generic_bs_tag;
    167 	bus_space_handle_t bsh;
    168 
    169 	bus_space_map(bst, SUN4I_WDT_BASE, SUN4I_WDT_SIZE, 0, &bsh);
    170 
    171 	bus_space_write_4(bst, bsh, SUN4I_WDT_CTRL,
    172 	    SUN4I_WDT_CTRL_KEY | SUN4I_WDT_CTRL_RESTART);
    173 	for (;;) {
    174 		bus_space_write_4(bst, bsh, SUN4I_WDT_MODE,
    175 		    SUN4I_WDT_MODE_EN | SUN4I_WDT_MODE_RST_EN);
    176 	}
    177 }
    178 
    179 static void
    180 sun4i_platform_delay(u_int n)
    181 {
    182 	static bus_space_tag_t bst = &armv7_generic_bs_tag;
    183 	static bus_space_handle_t bsh = 0;
    184 	const long incs_per_us = SUNXI_REF_FREQ / 1000000;
    185 	long ticks = n * incs_per_us;
    186 	uint32_t cur, prev;
    187 
    188 	if (bsh == 0)
    189 		bus_space_map(bst, SUN4I_TIMER_BASE, SUN4I_TIMER_SIZE, 0, &bsh);
    190 
    191 	prev = ~bus_space_read_4(bst, bsh, SUN4I_TIMER_0_VAL);
    192 	while (ticks > 0) {
    193 		cur = ~bus_space_read_4(bst, bsh, SUN4I_TIMER_0_VAL);
    194 		if (cur > prev)
    195 			ticks -= (cur - prev);
    196 		else
    197 			ticks -= (~0U - cur + prev);
    198 		prev = cur;
    199 	}
    200 }
    201 
    202 static void
    203 sun6i_platform_reset(void)
    204 {
    205 	bus_space_tag_t bst = &armv7_generic_bs_tag;
    206 	bus_space_handle_t bsh;
    207 
    208 	bus_space_map(bst, SUN6I_WDT_BASE, SUN6I_WDT_SIZE, 0, &bsh);
    209 
    210 	bus_space_write_4(bst, bsh, SUN6I_WDT_CFG, SUN6I_WDT_CFG_SYS);
    211 	bus_space_write_4(bst, bsh, SUN6I_WDT_MODE, SUN6I_WDT_MODE_EN);
    212 }
    213 
    214 static void
    215 sun9i_platform_reset(void)
    216 {
    217 	bus_space_tag_t bst = &armv7_generic_bs_tag;
    218 	bus_space_handle_t bsh;
    219 
    220 	bus_space_map(bst, SUN9I_WDT_BASE, SUN9I_WDT_SIZE, 0, &bsh);
    221 
    222 	bus_space_write_4(bst, bsh, SUN9I_WDT_CFG, SUN9I_WDT_CFG_SYS);
    223 	bus_space_write_4(bst, bsh, SUN9I_WDT_MODE, SUN9I_WDT_MODE_EN);
    224 }
    225 
    226 static const struct arm_platform sun4i_platform = {
    227 	.devmap = sunxi_platform_devmap,
    228 	.bootstrap = sunxi_platform_bootstrap,
    229 	.init_attach_args = sunxi_platform_init_attach_args,
    230 	.early_putchar = sunxi_platform_early_putchar,
    231 	.device_register = sunxi_platform_device_register,
    232 	.reset = sun4i_platform_reset,
    233 	.delay = sun4i_platform_delay,
    234 	.uart_freq = sunxi_platform_uart_freq,
    235 };
    236 
    237 ARM_PLATFORM(sun4i_a10, "allwinner,sun4i-a10", &sun4i_platform);
    238 
    239 static const struct arm_platform sun5i_platform = {
    240 	.devmap = sunxi_platform_devmap,
    241 	.bootstrap = sunxi_platform_bootstrap,
    242 	.init_attach_args = sunxi_platform_init_attach_args,
    243 	.early_putchar = sunxi_platform_early_putchar,
    244 	.device_register = sunxi_platform_device_register,
    245 	.reset = sun4i_platform_reset,
    246 	.delay = sun4i_platform_delay,
    247 	.uart_freq = sunxi_platform_uart_freq,
    248 };
    249 
    250 ARM_PLATFORM(sun5i_a13, "allwinner,sun5i-a13", &sun5i_platform);
    251 
    252 static const struct arm_platform sun6i_platform = {
    253 	.devmap = sunxi_platform_devmap,
    254 	.bootstrap = sunxi_platform_psci_bootstrap,
    255 	.init_attach_args = sunxi_platform_init_attach_args,
    256 	.early_putchar = sunxi_platform_early_putchar,
    257 	.device_register = sunxi_platform_device_register,
    258 	.reset = sun6i_platform_reset,
    259 	.delay = gtmr_delay,
    260 	.uart_freq = sunxi_platform_uart_freq,
    261 };
    262 
    263 ARM_PLATFORM(sun6i_a31, "allwinner,sun6i-a31", &sun6i_platform);
    264 
    265 static const struct arm_platform sun7i_platform = {
    266 	.devmap = sunxi_platform_devmap,
    267 	.bootstrap = sunxi_platform_psci_bootstrap,
    268 	.init_attach_args = sunxi_platform_init_attach_args,
    269 	.early_putchar = sunxi_platform_early_putchar,
    270 	.device_register = sunxi_platform_device_register,
    271 	.reset = sun4i_platform_reset,
    272 	.delay = sun4i_platform_delay,
    273 	.uart_freq = sunxi_platform_uart_freq,
    274 };
    275 
    276 ARM_PLATFORM(sun7i_a20, "allwinner,sun7i-a20", &sun7i_platform);
    277 
    278 static const struct arm_platform sun8i_platform = {
    279 	.devmap = sunxi_platform_devmap,
    280 	.bootstrap = sunxi_platform_psci_bootstrap,
    281 	.init_attach_args = sunxi_platform_init_attach_args,
    282 	.early_putchar = sunxi_platform_early_putchar,
    283 	.device_register = sunxi_platform_device_register,
    284 	.reset = sun6i_platform_reset,
    285 	.delay = gtmr_delay,
    286 	.uart_freq = sunxi_platform_uart_freq,
    287 };
    288 
    289 ARM_PLATFORM(sun8i_h2plus, "allwinner,sun8i-h2-plus", &sun8i_platform);
    290 ARM_PLATFORM(sun8i_h3, "allwinner,sun8i-h3", &sun8i_platform);
    291 ARM_PLATFORM(sun8i_a83t, "allwinner,sun8i-a83t", &sun8i_platform);
    292 
    293 static const struct arm_platform sun9i_platform = {
    294 	.devmap = sunxi_platform_devmap,
    295 	.bootstrap = sunxi_platform_bootstrap,
    296 	.init_attach_args = sunxi_platform_init_attach_args,
    297 	.early_putchar = sunxi_platform_early_putchar,
    298 	.device_register = sunxi_platform_device_register,
    299 	.reset = sun9i_platform_reset,
    300 	.delay = gtmr_delay,
    301 	.uart_freq = sunxi_platform_uart_freq,
    302 };
    303 
    304 ARM_PLATFORM(sun9i_a80, "allwinner,sun9i-a80", &sun9i_platform);
    305 
    306 static const struct arm_platform sun50i_platform = {
    307 	.devmap = sunxi_platform_devmap,
    308 	.bootstrap = sunxi_platform_bootstrap,
    309 	.init_attach_args = sunxi_platform_init_attach_args,
    310 	.early_putchar = sunxi_platform_early_putchar,
    311 	.device_register = sunxi_platform_device_register,
    312 	.reset = sun6i_platform_reset,
    313 	.delay = gtmr_delay,
    314 	.uart_freq = sunxi_platform_uart_freq,
    315 };
    316 
    317 ARM_PLATFORM(sun50i_a64, "allwinner,sun50i-a64", &sun50i_platform);
    318 ARM_PLATFORM(sun50i_h5, "allwinner,sun50i-h5", &sun50i_platform);
    319