xref: /netbsd-src/sys/arch/arm/amlogic/meson_platform.c (revision 8d564c5dcfeea024762586ce07de3c286d3d30e1)
1*8d564c5dSskrll /* $NetBSD: meson_platform.c,v 1.21 2023/04/07 08:55:29 skrll Exp $ */
2912cfa14Sjmcneill 
3912cfa14Sjmcneill /*-
4912cfa14Sjmcneill  * Copyright (c) 2019 Jared McNeill <jmcneill@invisible.ca>
5912cfa14Sjmcneill  * All rights reserved.
6912cfa14Sjmcneill  *
7912cfa14Sjmcneill  * Redistribution and use in source and binary forms, with or without
8912cfa14Sjmcneill  * modification, are permitted provided that the following conditions
9912cfa14Sjmcneill  * are met:
10912cfa14Sjmcneill  * 1. Redistributions of source code must retain the above copyright
11912cfa14Sjmcneill  *    notice, this list of conditions and the following disclaimer.
12912cfa14Sjmcneill  * 2. Redistributions in binary form must reproduce the above copyright
13912cfa14Sjmcneill  *    notice, this list of conditions and the following disclaimer in the
14912cfa14Sjmcneill  *    documentation and/or other materials provided with the distribution.
15912cfa14Sjmcneill  *
16912cfa14Sjmcneill  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17912cfa14Sjmcneill  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18912cfa14Sjmcneill  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19912cfa14Sjmcneill  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20912cfa14Sjmcneill  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21912cfa14Sjmcneill  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22912cfa14Sjmcneill  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23912cfa14Sjmcneill  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24912cfa14Sjmcneill  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25912cfa14Sjmcneill  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26912cfa14Sjmcneill  * SUCH DAMAGE.
27912cfa14Sjmcneill  */
28912cfa14Sjmcneill 
29912cfa14Sjmcneill #include "opt_soc.h"
30912cfa14Sjmcneill #include "opt_multiprocessor.h"
31912cfa14Sjmcneill #include "opt_console.h"
32912cfa14Sjmcneill 
33912cfa14Sjmcneill #include "arml2cc.h"
34912cfa14Sjmcneill 
35912cfa14Sjmcneill #include <sys/cdefs.h>
36*8d564c5dSskrll __KERNEL_RCSID(0, "$NetBSD: meson_platform.c,v 1.21 2023/04/07 08:55:29 skrll Exp $");
37912cfa14Sjmcneill 
38912cfa14Sjmcneill #include <sys/param.h>
39912cfa14Sjmcneill #include <sys/bus.h>
40912cfa14Sjmcneill #include <sys/cpu.h>
41912cfa14Sjmcneill #include <sys/device.h>
42912cfa14Sjmcneill #include <sys/termios.h>
43912cfa14Sjmcneill 
44912cfa14Sjmcneill #include <dev/fdt/fdtvar.h>
45*8d564c5dSskrll 
46912cfa14Sjmcneill #include <arm/fdt/arm_fdtvar.h>
47912cfa14Sjmcneill 
48912cfa14Sjmcneill #include <uvm/uvm_extern.h>
49912cfa14Sjmcneill 
50912cfa14Sjmcneill #include <machine/bootconfig.h>
51912cfa14Sjmcneill #include <arm/cpufunc.h>
52912cfa14Sjmcneill 
53912cfa14Sjmcneill #include <arm/cortex/a9tmr_var.h>
547e38c880Sjmcneill #include <arm/cortex/gtmr_var.h>
55912cfa14Sjmcneill #include <arm/cortex/pl310_var.h>
56912cfa14Sjmcneill #include <arm/cortex/scu_reg.h>
57912cfa14Sjmcneill 
58912cfa14Sjmcneill #include <arm/amlogic/meson_uart.h>
59912cfa14Sjmcneill 
60912cfa14Sjmcneill #include <evbarm/fdt/platform.h>
61912cfa14Sjmcneill #include <evbarm/fdt/machdep.h>
62912cfa14Sjmcneill 
63f4728349Sjmcneill #include <net/if_ether.h>
64f4728349Sjmcneill 
65912cfa14Sjmcneill #include <libfdt.h>
66912cfa14Sjmcneill 
67912cfa14Sjmcneill #define	MESON_CORE_APB3_VBASE	KERNEL_IO_VBASE
68912cfa14Sjmcneill #define	MESON_CORE_APB3_PBASE	0xc0000000
697e38c880Sjmcneill #define	MESON_CORE_APB3_SIZE	0x01400000
70912cfa14Sjmcneill 
71912cfa14Sjmcneill #define	MESON_CBUS_OFFSET	0x01100000
72912cfa14Sjmcneill 
73fcb302d4Sjmcneill #define	MESON8B_WATCHDOG_BASE	0xc1109900
74fcb302d4Sjmcneill #define	MESON8B_WATCHDOG_SIZE	0x8
75fcb302d4Sjmcneill #define	 MESON8B_WATCHDOG_TC	0x00
76fcb302d4Sjmcneill #define	  MESON8B_WATCHDOG_TC_CPUS	__BITS(27,24)
77fcb302d4Sjmcneill #define	  MESON8B_WATCHDOG_TC_ENABLE	__BIT(19)
78fcb302d4Sjmcneill #define	  MESON8B_WATCHDOG_TC_TCNT	__BITS(15,0)
79fcb302d4Sjmcneill #define	 MESON8B_WATCHDOG_RESET	0x04
80fcb302d4Sjmcneill #define	  MESON8B_WATCHDOG_RESET_COUNT	__BITS(15,0)
81fcb302d4Sjmcneill 
82fcb302d4Sjmcneill #define	MESONGX_WATCHDOG_BASE	0xc11098d0
83fcb302d4Sjmcneill #define	MESONGX_WATCHDOG_SIZE	0x10
84fcb302d4Sjmcneill #define	 MESONGX_WATCHDOG_CNTL	0x00
857d466a06Sjmcneill #define	  MESONGX_WATCHDOG_CNTL_CLK_EN		__BIT(24)
867d466a06Sjmcneill #define	  MESONGX_WATCHDOG_CNTL_SYS_RESET_N_EN	__BIT(21)
87fcb302d4Sjmcneill #define	  MESONGX_WATCHDOG_CNTL_WDOG_EN		__BIT(18)
88fcb302d4Sjmcneill #define	 MESONGX_WATCHDOG_CNTL1	0x04
89fcb302d4Sjmcneill #define	 MESONGX_WATCHDOG_TCNT	0x08
90fcb302d4Sjmcneill #define	  MESONGX_WATCHDOG_TCNT_COUNT	__BITS(15,0)
91fcb302d4Sjmcneill #define	 MESONGX_WATCHDOG_RESET	0x0c
92912cfa14Sjmcneill 
93912cfa14Sjmcneill #define	MESON8B_ARM_VBASE	(MESON_CORE_APB3_VBASE + MESON_CORE_APB3_SIZE)
94912cfa14Sjmcneill #define	MESON8B_ARM_PBASE	0xc4200000
95912cfa14Sjmcneill #define	MESON8B_ARM_SIZE	0x00200000
96912cfa14Sjmcneill #define	MESON8B_ARM_PL310_BASE	0x00000000
97912cfa14Sjmcneill #define	MESON8B_ARM_SCU_BASE	0x00100000
98912cfa14Sjmcneill 
99912cfa14Sjmcneill #define	MESON8B_AOBUS_VBASE	(MESON8B_ARM_VBASE + MESON8B_ARM_SIZE)
1007e38c880Sjmcneill #define	MESON8B_AOBUS_PBASE	0xc8000000
1017e38c880Sjmcneill #define	MESON8B_AOBUS_SIZE	0x00200000
102ce7d27f7Sjmcneill #define	MESON8B_AOBUS_RTI_OFFSET 0x00100000
103912cfa14Sjmcneill 
104912cfa14Sjmcneill #define	MESON_AOBUS_PWR_CTRL0_REG	0xe0
105912cfa14Sjmcneill #define	MESON_AOBUS_PWR_CTRL1_REG	0xe4
106912cfa14Sjmcneill #define	MESON_AOBUS_PWR_MEM_PD0_REG	0xf4
107912cfa14Sjmcneill 
108912cfa14Sjmcneill #define	MESON_CBUS_CPU_CLK_CNTL_REG	0x419c
109912cfa14Sjmcneill 
110912cfa14Sjmcneill 
111912cfa14Sjmcneill #define	MESON8B_SRAM_VBASE	(MESON8B_AOBUS_VBASE + MESON8B_AOBUS_SIZE)
112912cfa14Sjmcneill #define	MESON8B_SRAM_PBASE	0xd9000000
1137e38c880Sjmcneill #define	MESON8B_SRAM_SIZE	0x00200000	/* 0x10000 rounded up */
114912cfa14Sjmcneill 
115912cfa14Sjmcneill #define	MESON8B_SRAM_CPUCONF_OFFSET		0x1ff80
116912cfa14Sjmcneill #define	MESON8B_SRAM_CPUCONF_CTRL_REG		0x00
117912cfa14Sjmcneill #define	MESON8B_SRAM_CPUCONF_CPU_ADDR_REG(n)	(0x04 * (n))
118912cfa14Sjmcneill 
119912cfa14Sjmcneill 
120912cfa14Sjmcneill extern struct arm32_bus_dma_tag arm_generic_dma_tag;
121912cfa14Sjmcneill extern struct bus_space arm_generic_bs_tag;
122912cfa14Sjmcneill 
123912cfa14Sjmcneill #define	meson_dma_tag		arm_generic_dma_tag
124912cfa14Sjmcneill #define	meson_bs_tag		arm_generic_bs_tag
125912cfa14Sjmcneill 
126912cfa14Sjmcneill static const struct pmap_devmap *
meson_platform_devmap(void)127912cfa14Sjmcneill meson_platform_devmap(void)
128912cfa14Sjmcneill {
129912cfa14Sjmcneill 	static const struct pmap_devmap devmap[] = {
130912cfa14Sjmcneill 		DEVMAP_ENTRY(MESON_CORE_APB3_VBASE,
131912cfa14Sjmcneill 			     MESON_CORE_APB3_PBASE,
132912cfa14Sjmcneill 			     MESON_CORE_APB3_SIZE),
133912cfa14Sjmcneill 		DEVMAP_ENTRY(MESON8B_ARM_VBASE,
134912cfa14Sjmcneill 			     MESON8B_ARM_PBASE,
135912cfa14Sjmcneill 			     MESON8B_ARM_SIZE),
136912cfa14Sjmcneill 		DEVMAP_ENTRY(MESON8B_AOBUS_VBASE,
137912cfa14Sjmcneill 			     MESON8B_AOBUS_PBASE,
138912cfa14Sjmcneill 			     MESON8B_AOBUS_SIZE),
139912cfa14Sjmcneill 		DEVMAP_ENTRY(MESON8B_SRAM_VBASE,
140912cfa14Sjmcneill 			     MESON8B_SRAM_PBASE,
141912cfa14Sjmcneill 			     MESON8B_SRAM_SIZE),
142912cfa14Sjmcneill 		DEVMAP_ENTRY_END
143912cfa14Sjmcneill 	};
144912cfa14Sjmcneill 
145912cfa14Sjmcneill 	return devmap;
146912cfa14Sjmcneill }
147912cfa14Sjmcneill 
148912cfa14Sjmcneill static void
meson_platform_init_attach_args(struct fdt_attach_args * faa)149912cfa14Sjmcneill meson_platform_init_attach_args(struct fdt_attach_args *faa)
150912cfa14Sjmcneill {
151912cfa14Sjmcneill 	faa->faa_bst = &meson_bs_tag;
152912cfa14Sjmcneill 	faa->faa_dmat = &meson_dma_tag;
153912cfa14Sjmcneill }
154912cfa14Sjmcneill 
155912cfa14Sjmcneill void meson_platform_early_putchar(char);
156912cfa14Sjmcneill 
157d329adb0Sskrll void __noasan
meson_platform_early_putchar(char c)158912cfa14Sjmcneill meson_platform_early_putchar(char c)
159912cfa14Sjmcneill {
160912cfa14Sjmcneill #ifdef CONSADDR
161912cfa14Sjmcneill #define	CONSADDR_VA	((CONSADDR - MESON8B_AOBUS_PBASE) + MESON8B_AOBUS_VBASE)
162912cfa14Sjmcneill 	volatile uint32_t *uartaddr = cpu_earlydevice_va_p() ?
163912cfa14Sjmcneill 	    (volatile uint32_t *)CONSADDR_VA :
164912cfa14Sjmcneill 	    (volatile uint32_t *)CONSADDR;
165912cfa14Sjmcneill 	int timo = 150000;
166912cfa14Sjmcneill 
167912cfa14Sjmcneill 	while ((uartaddr[UART_STATUS_REG/4] & UART_STATUS_TX_EMPTY) == 0) {
168912cfa14Sjmcneill 		if (--timo == 0)
169912cfa14Sjmcneill 			break;
170912cfa14Sjmcneill 	}
171912cfa14Sjmcneill 
172912cfa14Sjmcneill 	uartaddr[UART_WFIFO_REG/4] = c;
173912cfa14Sjmcneill 
174912cfa14Sjmcneill 	while ((uartaddr[UART_STATUS_REG/4] & UART_STATUS_TX_EMPTY) == 0) {
175912cfa14Sjmcneill 		if (--timo == 0)
176912cfa14Sjmcneill 			break;
177912cfa14Sjmcneill 	}
178912cfa14Sjmcneill #endif
179912cfa14Sjmcneill }
180912cfa14Sjmcneill 
181912cfa14Sjmcneill static void
meson_platform_device_register(device_t self,void * aux)182912cfa14Sjmcneill meson_platform_device_register(device_t self, void *aux)
183912cfa14Sjmcneill {
184f4728349Sjmcneill 	prop_dictionary_t dict = device_properties(self);
185f4728349Sjmcneill 
186f4728349Sjmcneill 	if (device_is_a(self, "awge") && device_unit(self) == 0) {
187f4728349Sjmcneill 		uint8_t enaddr[ETHER_ADDR_LEN];
188f4728349Sjmcneill 		if (get_bootconf_option(boot_args, "awge0.mac-address",
189f4728349Sjmcneill 		    BOOTOPT_TYPE_MACADDR, enaddr)) {
190d958cb46Sskrll 			prop_dictionary_set_data(dict, "mac-address", enaddr,
191f4728349Sjmcneill 			    sizeof(enaddr));
192f4728349Sjmcneill 		}
193f4728349Sjmcneill 	}
194f4728349Sjmcneill 
1957e38c880Sjmcneill 	if (device_is_a(self, "mesonfb")) {
196f4728349Sjmcneill 		int scale, depth;
197f4728349Sjmcneill 
198f4728349Sjmcneill 		if (get_bootconf_option(boot_args, "fb.scale",
199f4728349Sjmcneill 		    BOOTOPT_TYPE_INT, &scale) && scale > 0) {
200f4728349Sjmcneill 			prop_dictionary_set_uint32(dict, "scale", scale);
201f4728349Sjmcneill 		}
202f4728349Sjmcneill 		if (get_bootconf_option(boot_args, "fb.depth",
203f4728349Sjmcneill 		    BOOTOPT_TYPE_INT, &depth)) {
204f4728349Sjmcneill 			prop_dictionary_set_uint32(dict, "depth", depth);
205f4728349Sjmcneill 		}
206f4728349Sjmcneill 	}
207912cfa14Sjmcneill }
208912cfa14Sjmcneill 
2091b14cfdaSjmcneill #if defined(SOC_MESON8B)
2101b14cfdaSjmcneill #define	MESON8B_BOOTINFO_REG	0xd901ff04
2111b14cfdaSjmcneill static int
meson8b_get_boot_id(void)2121b14cfdaSjmcneill meson8b_get_boot_id(void)
2131b14cfdaSjmcneill {
2141b14cfdaSjmcneill 	static int boot_id = -1;
2151b14cfdaSjmcneill 	bus_space_tag_t bst = &arm_generic_bs_tag;
2161b14cfdaSjmcneill 	bus_space_handle_t bsh;
2171b14cfdaSjmcneill 
2181b14cfdaSjmcneill 	if (boot_id == -1) {
2191b14cfdaSjmcneill 		if (bus_space_map(bst, MESON8B_BOOTINFO_REG, 4, 0, &bsh) != 0)
2201b14cfdaSjmcneill 			return -1;
2211b14cfdaSjmcneill 
2221b14cfdaSjmcneill 		boot_id = (int)bus_space_read_4(bst, bsh, 0);
2231b14cfdaSjmcneill 
2241b14cfdaSjmcneill 		bus_space_unmap(bst, bsh, 4);
2251b14cfdaSjmcneill 	}
2261b14cfdaSjmcneill 
2271b14cfdaSjmcneill 	return boot_id;
2281b14cfdaSjmcneill }
2291b14cfdaSjmcneill 
2301b14cfdaSjmcneill static void
meson8b_platform_device_register(device_t self,void * aux)2311b14cfdaSjmcneill meson8b_platform_device_register(device_t self, void *aux)
2321b14cfdaSjmcneill {
2331b14cfdaSjmcneill 	device_t parent = device_parent(self);
2341b14cfdaSjmcneill 	char *ptr;
2351b14cfdaSjmcneill 
2361b14cfdaSjmcneill 	if (device_is_a(self, "ld") &&
2371b14cfdaSjmcneill 	    device_is_a(parent, "sdmmc") &&
2381b14cfdaSjmcneill 	    (device_is_a(device_parent(parent), "mesonsdhc") ||
2391b14cfdaSjmcneill 	     device_is_a(device_parent(parent), "mesonsdio"))) {
2401b14cfdaSjmcneill 
2411b14cfdaSjmcneill 		const int boot_id = meson8b_get_boot_id();
2421b14cfdaSjmcneill 		const bool has_rootdev = get_bootconf_option(boot_args, "root", BOOTOPT_TYPE_STRING, &ptr) != 0;
2431b14cfdaSjmcneill 
2441b14cfdaSjmcneill 		if (!has_rootdev) {
2451b14cfdaSjmcneill 			char rootarg[64];
2461b14cfdaSjmcneill 			snprintf(rootarg, sizeof(rootarg), " root=%sa", device_xname(self));
2471b14cfdaSjmcneill 
2481b14cfdaSjmcneill 			/* Assume that SDIO is used for SD cards and SDHC is used for eMMC */
2491b14cfdaSjmcneill 			if (device_is_a(device_parent(parent), "mesonsdhc") && boot_id == 0)
2501b14cfdaSjmcneill 				strcat(boot_args, rootarg);
2511b14cfdaSjmcneill 			else if (device_is_a(device_parent(parent), "mesonsdio") && boot_id != 0)
2521b14cfdaSjmcneill 				strcat(boot_args, rootarg);
2531b14cfdaSjmcneill 		}
2541b14cfdaSjmcneill 	}
2551b14cfdaSjmcneill 
2561b14cfdaSjmcneill 	meson_platform_device_register(self, aux);
2571b14cfdaSjmcneill }
2581b14cfdaSjmcneill #endif
2591b14cfdaSjmcneill 
260912cfa14Sjmcneill static u_int
meson_platform_uart_freq(void)261912cfa14Sjmcneill meson_platform_uart_freq(void)
262912cfa14Sjmcneill {
263912cfa14Sjmcneill 	return 0;
264912cfa14Sjmcneill }
265912cfa14Sjmcneill 
266912cfa14Sjmcneill static void
meson_platform_bootstrap(void)267912cfa14Sjmcneill meson_platform_bootstrap(void)
268912cfa14Sjmcneill {
269912cfa14Sjmcneill 	arm_fdt_cpu_bootstrap();
270912cfa14Sjmcneill 
271912cfa14Sjmcneill 	void *fdt_data = __UNCONST(fdtbus_get_data());
272912cfa14Sjmcneill 	const int chosen_off = fdt_path_offset(fdt_data, "/chosen");
273912cfa14Sjmcneill 	if (chosen_off < 0)
274912cfa14Sjmcneill 		return;
275912cfa14Sjmcneill 
276912cfa14Sjmcneill 	if (match_bootconf_option(boot_args, "console", "fb")) {
277912cfa14Sjmcneill 		const int framebuffer_off =
278912cfa14Sjmcneill 		    fdt_path_offset(fdt_data, "/chosen/framebuffer");
279912cfa14Sjmcneill 		if (framebuffer_off >= 0) {
280912cfa14Sjmcneill 			const char *status = fdt_getprop(fdt_data,
281912cfa14Sjmcneill 			    framebuffer_off, "status", NULL);
282912cfa14Sjmcneill 			if (status == NULL || strncmp(status, "ok", 2) == 0) {
283912cfa14Sjmcneill 				fdt_setprop_string(fdt_data, chosen_off,
284912cfa14Sjmcneill 				    "stdout-path", "/chosen/framebuffer");
285912cfa14Sjmcneill 			}
286912cfa14Sjmcneill 		}
287912cfa14Sjmcneill 	} else if (match_bootconf_option(boot_args, "console", "serial")) {
288912cfa14Sjmcneill 		fdt_setprop_string(fdt_data, chosen_off,
289912cfa14Sjmcneill 		    "stdout-path", "serial0:115200n8");
290912cfa14Sjmcneill 	}
291912cfa14Sjmcneill }
292912cfa14Sjmcneill 
293912cfa14Sjmcneill #if defined(SOC_MESON8B)
294912cfa14Sjmcneill static void
meson8b_platform_bootstrap(void)295912cfa14Sjmcneill meson8b_platform_bootstrap(void)
296912cfa14Sjmcneill {
297912cfa14Sjmcneill 
298912cfa14Sjmcneill #if NARML2CC > 0
299912cfa14Sjmcneill 	const bus_space_handle_t pl310_bh = MESON8B_ARM_VBASE + MESON8B_ARM_PL310_BASE;
300912cfa14Sjmcneill 	arml2cc_init(&arm_generic_bs_tag, pl310_bh, 0);
301912cfa14Sjmcneill #endif
302912cfa14Sjmcneill 
303912cfa14Sjmcneill 	meson_platform_bootstrap();
304912cfa14Sjmcneill }
305912cfa14Sjmcneill 
306912cfa14Sjmcneill static void
meson8b_platform_reset(void)307fcb302d4Sjmcneill meson8b_platform_reset(void)
308912cfa14Sjmcneill {
309912cfa14Sjmcneill 	bus_space_tag_t bst = &meson_bs_tag;
310912cfa14Sjmcneill 	bus_space_handle_t bsh;
311912cfa14Sjmcneill 
312fcb302d4Sjmcneill 	bus_space_map(bst, MESON8B_WATCHDOG_BASE, MESON8B_WATCHDOG_SIZE, 0, &bsh);
313912cfa14Sjmcneill 
314fcb302d4Sjmcneill 	bus_space_write_4(bst, bsh, MESON8B_WATCHDOG_TC,
315fcb302d4Sjmcneill 	    MESON8B_WATCHDOG_TC_CPUS | MESON8B_WATCHDOG_TC_ENABLE | __SHIFTIN(0xfff, MESON8B_WATCHDOG_TC_TCNT));
316fcb302d4Sjmcneill 	bus_space_write_4(bst, bsh, MESON8B_WATCHDOG_RESET, 0);
317912cfa14Sjmcneill 
318912cfa14Sjmcneill 	for (;;) {
319912cfa14Sjmcneill 		__asm("wfi");
320912cfa14Sjmcneill 	}
321912cfa14Sjmcneill }
322912cfa14Sjmcneill 
323ac3efc5dSrin #ifdef MULTIPROCESSOR
324912cfa14Sjmcneill static void
meson8b_mpinit_delay(u_int n)325912cfa14Sjmcneill meson8b_mpinit_delay(u_int n)
326912cfa14Sjmcneill {
327912cfa14Sjmcneill 	for (volatile int i = 0; i < n; i++)
328912cfa14Sjmcneill 		;
329912cfa14Sjmcneill }
330ac3efc5dSrin #endif
331912cfa14Sjmcneill 
332912cfa14Sjmcneill static int
cpu_enable_meson8b(int phandle)333912cfa14Sjmcneill cpu_enable_meson8b(int phandle)
334912cfa14Sjmcneill {
335ac3efc5dSrin #ifdef MULTIPROCESSOR
336912cfa14Sjmcneill 	const bus_addr_t cbar = armreg_cbar_read();
337912cfa14Sjmcneill 	bus_space_tag_t bst = &arm_generic_bs_tag;
338912cfa14Sjmcneill 
339912cfa14Sjmcneill 	const bus_space_handle_t scu_bsh =
340912cfa14Sjmcneill 	    cbar - MESON8B_ARM_PBASE + MESON8B_ARM_VBASE;
341912cfa14Sjmcneill 	const bus_space_handle_t cpuconf_bsh =
342912cfa14Sjmcneill 	    MESON8B_SRAM_VBASE + MESON8B_SRAM_CPUCONF_OFFSET;
343912cfa14Sjmcneill 	const bus_space_handle_t ao_bsh =
344ce7d27f7Sjmcneill 	    MESON8B_AOBUS_VBASE + MESON8B_AOBUS_RTI_OFFSET;
345912cfa14Sjmcneill 	const bus_space_handle_t cbus_bsh =
346912cfa14Sjmcneill 	    MESON_CORE_APB3_VBASE + MESON_CBUS_OFFSET;
347912cfa14Sjmcneill 	uint32_t pwr_sts, pwr_cntl0, pwr_cntl1, cpuclk, mempd0;
348912cfa14Sjmcneill 	uint64_t mpidr;
349912cfa14Sjmcneill 
350912cfa14Sjmcneill 	fdtbus_get_reg64(phandle, 0, &mpidr, NULL);
351912cfa14Sjmcneill 
352912cfa14Sjmcneill 	const u_int cpuno = __SHIFTOUT(mpidr, MPIDR_AFF0);
353912cfa14Sjmcneill 
354912cfa14Sjmcneill 	bus_space_write_4(bst, cpuconf_bsh, MESON8B_SRAM_CPUCONF_CPU_ADDR_REG(cpuno),
355912cfa14Sjmcneill 	    KERN_VTOPHYS((vaddr_t)cpu_mpstart));
356912cfa14Sjmcneill 
357912cfa14Sjmcneill 	pwr_sts = bus_space_read_4(bst, scu_bsh, SCU_CPU_PWR_STS);
358912cfa14Sjmcneill 	pwr_sts &= ~(3 << (8 * cpuno));
359912cfa14Sjmcneill 	bus_space_write_4(bst, scu_bsh, SCU_CPU_PWR_STS, pwr_sts);
360912cfa14Sjmcneill 
361912cfa14Sjmcneill 	pwr_cntl0 = bus_space_read_4(bst, ao_bsh, MESON_AOBUS_PWR_CTRL0_REG);
362912cfa14Sjmcneill 	pwr_cntl0 &= ~((3 << 18) << ((cpuno - 1) * 2));
363912cfa14Sjmcneill 	bus_space_write_4(bst, ao_bsh, MESON_AOBUS_PWR_CTRL0_REG, pwr_cntl0);
364912cfa14Sjmcneill 
365912cfa14Sjmcneill 	meson8b_mpinit_delay(5000);
366912cfa14Sjmcneill 
367912cfa14Sjmcneill 	cpuclk = bus_space_read_4(bst, cbus_bsh, MESON_CBUS_CPU_CLK_CNTL_REG);
368912cfa14Sjmcneill 	cpuclk |= (1 << (24 + cpuno));
369912cfa14Sjmcneill 	bus_space_write_4(bst, cbus_bsh, MESON_CBUS_CPU_CLK_CNTL_REG, cpuclk);
370912cfa14Sjmcneill 
371912cfa14Sjmcneill 	mempd0 = bus_space_read_4(bst, ao_bsh, MESON_AOBUS_PWR_MEM_PD0_REG);
372912cfa14Sjmcneill 	mempd0 &= ~((uint32_t)(0xf << 28) >> ((cpuno - 1) * 4));
373912cfa14Sjmcneill 	bus_space_write_4(bst, ao_bsh, MESON_AOBUS_PWR_MEM_PD0_REG, mempd0);
374912cfa14Sjmcneill 
375912cfa14Sjmcneill 	pwr_cntl1 = bus_space_read_4(bst, ao_bsh, MESON_AOBUS_PWR_CTRL1_REG);
376912cfa14Sjmcneill 	pwr_cntl1 &= ~((3 << 4) << ((cpuno - 1) * 2));
377912cfa14Sjmcneill 	bus_space_write_4(bst, ao_bsh, MESON_AOBUS_PWR_CTRL1_REG, pwr_cntl1);
378912cfa14Sjmcneill 
379912cfa14Sjmcneill 	meson8b_mpinit_delay(10000);
380912cfa14Sjmcneill 
381912cfa14Sjmcneill 	for (;;) {
382912cfa14Sjmcneill 		pwr_cntl1 = bus_space_read_4(bst, ao_bsh,
383912cfa14Sjmcneill 		    MESON_AOBUS_PWR_CTRL1_REG) & ((1 << 17) << (cpuno - 1));
384912cfa14Sjmcneill 		if (pwr_cntl1)
385912cfa14Sjmcneill 			break;
386912cfa14Sjmcneill 		meson8b_mpinit_delay(10000);
387912cfa14Sjmcneill 	}
388912cfa14Sjmcneill 
389912cfa14Sjmcneill 	pwr_cntl0 = bus_space_read_4(bst, ao_bsh, MESON_AOBUS_PWR_CTRL0_REG);
390912cfa14Sjmcneill 	pwr_cntl0 &= ~(1 << cpuno);
391912cfa14Sjmcneill 	bus_space_write_4(bst, ao_bsh, MESON_AOBUS_PWR_CTRL0_REG, pwr_cntl0);
392912cfa14Sjmcneill 
393912cfa14Sjmcneill 	cpuclk = bus_space_read_4(bst, cbus_bsh, MESON_CBUS_CPU_CLK_CNTL_REG);
394912cfa14Sjmcneill 	cpuclk &= ~(1 << (24 + cpuno));
395912cfa14Sjmcneill 	bus_space_write_4(bst, cbus_bsh, MESON_CBUS_CPU_CLK_CNTL_REG, cpuclk);
396912cfa14Sjmcneill 
397912cfa14Sjmcneill 	bus_space_write_4(bst, cpuconf_bsh, MESON8B_SRAM_CPUCONF_CPU_ADDR_REG(cpuno),
398912cfa14Sjmcneill 	    KERN_VTOPHYS((vaddr_t)cpu_mpstart));
399912cfa14Sjmcneill 
400912cfa14Sjmcneill 	uint32_t ctrl = bus_space_read_4(bst, cpuconf_bsh, MESON8B_SRAM_CPUCONF_CTRL_REG);
401912cfa14Sjmcneill 	ctrl |= __BITS(cpuno,0);
402912cfa14Sjmcneill 	bus_space_write_4(bst, cpuconf_bsh, MESON8B_SRAM_CPUCONF_CTRL_REG, ctrl);
403ac3efc5dSrin #endif
404912cfa14Sjmcneill 
405912cfa14Sjmcneill 	return 0;
406912cfa14Sjmcneill }
407912cfa14Sjmcneill 
408912cfa14Sjmcneill ARM_CPU_METHOD(meson8b, "amlogic,meson8b-smp", cpu_enable_meson8b);
409912cfa14Sjmcneill 
410a476a90dSskrll static int
meson8b_mpstart(void)4117e38c880Sjmcneill meson8b_mpstart(void)
412912cfa14Sjmcneill {
413a476a90dSskrll 	int ret = 0;
414912cfa14Sjmcneill 	const bus_addr_t cbar = armreg_cbar_read();
415912cfa14Sjmcneill 	bus_space_tag_t bst = &arm_generic_bs_tag;
416912cfa14Sjmcneill 
417912cfa14Sjmcneill 	if (cbar == 0)
418a476a90dSskrll 		return ret;
419912cfa14Sjmcneill 
420912cfa14Sjmcneill 	const bus_space_handle_t scu_bsh =
421912cfa14Sjmcneill 	    cbar - MESON8B_ARM_PBASE + MESON8B_ARM_VBASE;
422912cfa14Sjmcneill 
423912cfa14Sjmcneill 	const uint32_t scu_cfg = bus_space_read_4(bst, scu_bsh, SCU_CFG);
424912cfa14Sjmcneill 	const u_int ncpus = (scu_cfg & SCU_CFG_CPUMAX) + 1;
425912cfa14Sjmcneill 
426912cfa14Sjmcneill 	if (ncpus < 2)
427a476a90dSskrll 		return ret;
428912cfa14Sjmcneill 
429912cfa14Sjmcneill 	/*
430912cfa14Sjmcneill 	 * Invalidate all SCU cache tags. That is, for all cores (0-3)
431912cfa14Sjmcneill 	 */
432912cfa14Sjmcneill 	bus_space_write_4(bst, scu_bsh, SCU_INV_ALL_REG, 0xffff);
433912cfa14Sjmcneill 
434912cfa14Sjmcneill 	uint32_t scu_ctl = bus_space_read_4(bst, scu_bsh, SCU_CTL);
435912cfa14Sjmcneill 	scu_ctl |= SCU_CTL_SCU_ENA;
436912cfa14Sjmcneill 	bus_space_write_4(bst, scu_bsh, SCU_CTL, scu_ctl);
437912cfa14Sjmcneill 
438912cfa14Sjmcneill 	armv7_dcache_wbinv_all();
439912cfa14Sjmcneill 
440a476a90dSskrll 	ret = arm_fdt_cpu_mpstart();
441a476a90dSskrll 	return ret;
442912cfa14Sjmcneill }
443912cfa14Sjmcneill 
444*8d564c5dSskrll static const struct fdt_platform meson8b_platform = {
445*8d564c5dSskrll 	.fp_devmap = meson_platform_devmap,
446*8d564c5dSskrll 	.fp_bootstrap = meson8b_platform_bootstrap,
447*8d564c5dSskrll 	.fp_init_attach_args = meson_platform_init_attach_args,
448*8d564c5dSskrll 	.fp_device_register = meson8b_platform_device_register,
449*8d564c5dSskrll 	.fp_reset = meson8b_platform_reset,
450*8d564c5dSskrll 	.fp_delay = a9ptmr_delay,
451*8d564c5dSskrll 	.fp_uart_freq = meson_platform_uart_freq,
452*8d564c5dSskrll 	.fp_mpstart = meson8b_mpstart,
453912cfa14Sjmcneill };
454912cfa14Sjmcneill 
455*8d564c5dSskrll FDT_PLATFORM(meson8b, "amlogic,meson8b", &meson8b_platform);
4567e38c880Sjmcneill #endif	/* SOC_MESON8B */
4577e38c880Sjmcneill 
458229af777Sjmcneill #if defined(SOC_MESONGX)
459fcb302d4Sjmcneill static void
mesongx_platform_reset(void)460fcb302d4Sjmcneill mesongx_platform_reset(void)
461fcb302d4Sjmcneill {
462fcb302d4Sjmcneill 	bus_space_tag_t bst = &meson_bs_tag;
463fcb302d4Sjmcneill 	bus_space_handle_t bsh;
464fcb302d4Sjmcneill 	uint32_t val;
465fcb302d4Sjmcneill 
466fcb302d4Sjmcneill 	bus_space_map(bst, MESONGX_WATCHDOG_BASE, MESONGX_WATCHDOG_SIZE, 0, &bsh);
467fcb302d4Sjmcneill 
4687d466a06Sjmcneill 	val = MESONGX_WATCHDOG_CNTL_SYS_RESET_N_EN |
4697d466a06Sjmcneill 	      MESONGX_WATCHDOG_CNTL_WDOG_EN |
4707d466a06Sjmcneill 	      MESONGX_WATCHDOG_CNTL_CLK_EN;
471fcb302d4Sjmcneill 	bus_space_write_4(bst, bsh, MESONGX_WATCHDOG_CNTL, val);
472fcb302d4Sjmcneill 
473fcb302d4Sjmcneill 	bus_space_write_4(bst, bsh, MESONGX_WATCHDOG_TCNT, 1);
474fcb302d4Sjmcneill 
475fcb302d4Sjmcneill 	bus_space_write_4(bst, bsh, MESONGX_WATCHDOG_RESET, 0);
476fcb302d4Sjmcneill 
477fcb302d4Sjmcneill 	for (;;) {
478fcb302d4Sjmcneill 		__asm("wfi");
479fcb302d4Sjmcneill 	}
480fcb302d4Sjmcneill }
481fcb302d4Sjmcneill 
482*8d564c5dSskrll static const struct fdt_platform mesongx_platform = {
483*8d564c5dSskrll 	.fp_devmap = meson_platform_devmap,
484*8d564c5dSskrll 	.fp_bootstrap = meson_platform_bootstrap,
485*8d564c5dSskrll 	.fp_init_attach_args = meson_platform_init_attach_args,
486*8d564c5dSskrll 	.fp_device_register = meson_platform_device_register,
487*8d564c5dSskrll 	.fp_reset = mesongx_platform_reset,
488*8d564c5dSskrll 	.fp_delay = gtmr_delay,
489*8d564c5dSskrll 	.fp_uart_freq = meson_platform_uart_freq,
490*8d564c5dSskrll 	.fp_mpstart = arm_fdt_cpu_mpstart,
4917e38c880Sjmcneill };
4927e38c880Sjmcneill 
493229af777Sjmcneill #if defined(SOC_MESONGXBB)
494*8d564c5dSskrll FDT_PLATFORM(mesongxbb, "amlogic,meson-gxbb", &mesongx_platform);
495229af777Sjmcneill #endif	/* SOC_MESONGXBB */
496229af777Sjmcneill #if defined(SOC_MESONGXL)
497*8d564c5dSskrll FDT_PLATFORM(mesongxl, "amlogic,meson-gxl", &mesongx_platform);
498229af777Sjmcneill #endif	/* SOC_MESONGXL */
499229af777Sjmcneill #endif	/* SOC_MESONGX */
500