Home | History | Annotate | Line # | Download | only in amlogic
meson_platform.c revision 1.19.2.1
      1  1.19.2.1   thorpej /* $NetBSD: meson_platform.c,v 1.19.2.1 2021/04/03 21:21:07 thorpej Exp $ */
      2       1.1  jmcneill 
      3       1.1  jmcneill /*-
      4       1.1  jmcneill  * Copyright (c) 2019 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_console.h"
     32       1.1  jmcneill 
     33       1.1  jmcneill #include "arml2cc.h"
     34       1.1  jmcneill 
     35       1.1  jmcneill #include <sys/cdefs.h>
     36  1.19.2.1   thorpej __KERNEL_RCSID(0, "$NetBSD: meson_platform.c,v 1.19.2.1 2021/04/03 21:21:07 thorpej Exp $");
     37       1.1  jmcneill 
     38       1.1  jmcneill #include <sys/param.h>
     39       1.1  jmcneill #include <sys/bus.h>
     40       1.1  jmcneill #include <sys/cpu.h>
     41       1.1  jmcneill #include <sys/device.h>
     42       1.1  jmcneill #include <sys/termios.h>
     43       1.1  jmcneill 
     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/a9tmr_var.h>
     53       1.5  jmcneill #include <arm/cortex/gtmr_var.h>
     54       1.1  jmcneill #include <arm/cortex/pl310_var.h>
     55       1.1  jmcneill #include <arm/cortex/scu_reg.h>
     56       1.1  jmcneill 
     57       1.1  jmcneill #include <arm/amlogic/meson_uart.h>
     58       1.1  jmcneill 
     59       1.1  jmcneill #include <evbarm/fdt/platform.h>
     60       1.1  jmcneill #include <evbarm/fdt/machdep.h>
     61       1.1  jmcneill 
     62       1.2  jmcneill #include <net/if_ether.h>
     63       1.2  jmcneill 
     64       1.1  jmcneill #include <libfdt.h>
     65       1.1  jmcneill 
     66       1.1  jmcneill #define	MESON_CORE_APB3_VBASE	KERNEL_IO_VBASE
     67       1.1  jmcneill #define	MESON_CORE_APB3_PBASE	0xc0000000
     68       1.5  jmcneill #define	MESON_CORE_APB3_SIZE	0x01400000
     69       1.1  jmcneill 
     70       1.6  jmcneill #define	MESON_CBUS_OFFSET	0x01100000
     71       1.1  jmcneill 
     72       1.9  jmcneill #define	MESON8B_WATCHDOG_BASE	0xc1109900
     73       1.9  jmcneill #define	MESON8B_WATCHDOG_SIZE	0x8
     74       1.9  jmcneill #define	 MESON8B_WATCHDOG_TC	0x00
     75       1.9  jmcneill #define	  MESON8B_WATCHDOG_TC_CPUS	__BITS(27,24)
     76       1.9  jmcneill #define	  MESON8B_WATCHDOG_TC_ENABLE	__BIT(19)
     77       1.9  jmcneill #define	  MESON8B_WATCHDOG_TC_TCNT	__BITS(15,0)
     78       1.9  jmcneill #define	 MESON8B_WATCHDOG_RESET	0x04
     79       1.9  jmcneill #define	  MESON8B_WATCHDOG_RESET_COUNT	__BITS(15,0)
     80       1.9  jmcneill 
     81       1.9  jmcneill #define	MESONGX_WATCHDOG_BASE	0xc11098d0
     82       1.9  jmcneill #define	MESONGX_WATCHDOG_SIZE	0x10
     83       1.9  jmcneill #define	 MESONGX_WATCHDOG_CNTL	0x00
     84      1.10  jmcneill #define	  MESONGX_WATCHDOG_CNTL_CLK_EN		__BIT(24)
     85      1.10  jmcneill #define	  MESONGX_WATCHDOG_CNTL_SYS_RESET_N_EN	__BIT(21)
     86      1.10  jmcneill #define	  MESONGX_WATCHDOG_CNTL_WDOG_EN		__BIT(18)
     87       1.9  jmcneill #define	 MESONGX_WATCHDOG_CNTL1	0x04
     88       1.9  jmcneill #define	 MESONGX_WATCHDOG_TCNT	0x08
     89       1.9  jmcneill #define	  MESONGX_WATCHDOG_TCNT_COUNT	__BITS(15,0)
     90       1.9  jmcneill #define	 MESONGX_WATCHDOG_RESET	0x0c
     91       1.1  jmcneill 
     92       1.6  jmcneill #define	MESON8B_ARM_VBASE	(MESON_CORE_APB3_VBASE + MESON_CORE_APB3_SIZE)
     93       1.1  jmcneill #define	MESON8B_ARM_PBASE	0xc4200000
     94       1.6  jmcneill #define	MESON8B_ARM_SIZE	0x00200000
     95       1.6  jmcneill #define	MESON8B_ARM_PL310_BASE	0x00000000
     96       1.6  jmcneill #define	MESON8B_ARM_SCU_BASE	0x00100000
     97       1.1  jmcneill 
     98       1.6  jmcneill #define	MESON8B_AOBUS_VBASE	(MESON8B_ARM_VBASE + MESON8B_ARM_SIZE)
     99       1.5  jmcneill #define	MESON8B_AOBUS_PBASE	0xc8000000
    100       1.6  jmcneill #define	MESON8B_AOBUS_SIZE	0x00200000
    101       1.7  jmcneill #define	MESON8B_AOBUS_RTI_OFFSET 0x00100000
    102       1.1  jmcneill 
    103       1.6  jmcneill #define	MESON_AOBUS_PWR_CTRL0_REG	0xe0
    104       1.6  jmcneill #define	MESON_AOBUS_PWR_CTRL1_REG	0xe4
    105       1.6  jmcneill #define	MESON_AOBUS_PWR_MEM_PD0_REG	0xf4
    106       1.1  jmcneill 
    107       1.6  jmcneill #define	MESON_CBUS_CPU_CLK_CNTL_REG	0x419c
    108       1.1  jmcneill 
    109       1.1  jmcneill 
    110       1.6  jmcneill #define	MESON8B_SRAM_VBASE	(MESON8B_AOBUS_VBASE + MESON8B_AOBUS_SIZE)
    111       1.6  jmcneill #define	MESON8B_SRAM_PBASE	0xd9000000
    112       1.6  jmcneill #define	MESON8B_SRAM_SIZE	0x00200000	/* 0x10000 rounded up */
    113       1.1  jmcneill 
    114       1.6  jmcneill #define	MESON8B_SRAM_CPUCONF_OFFSET		0x1ff80
    115       1.6  jmcneill #define	MESON8B_SRAM_CPUCONF_CTRL_REG		0x00
    116       1.6  jmcneill #define	MESON8B_SRAM_CPUCONF_CPU_ADDR_REG(n)	(0x04 * (n))
    117       1.1  jmcneill 
    118       1.1  jmcneill 
    119       1.1  jmcneill extern struct arm32_bus_dma_tag arm_generic_dma_tag;
    120       1.1  jmcneill extern struct bus_space arm_generic_bs_tag;
    121       1.1  jmcneill 
    122       1.1  jmcneill #define	meson_dma_tag		arm_generic_dma_tag
    123       1.1  jmcneill #define	meson_bs_tag		arm_generic_bs_tag
    124       1.1  jmcneill 
    125       1.1  jmcneill static const struct pmap_devmap *
    126       1.1  jmcneill meson_platform_devmap(void)
    127       1.1  jmcneill {
    128       1.1  jmcneill 	static const struct pmap_devmap devmap[] = {
    129       1.1  jmcneill 		DEVMAP_ENTRY(MESON_CORE_APB3_VBASE,
    130       1.1  jmcneill 			     MESON_CORE_APB3_PBASE,
    131       1.1  jmcneill 			     MESON_CORE_APB3_SIZE),
    132       1.1  jmcneill 		DEVMAP_ENTRY(MESON8B_ARM_VBASE,
    133       1.1  jmcneill 			     MESON8B_ARM_PBASE,
    134       1.1  jmcneill 			     MESON8B_ARM_SIZE),
    135       1.1  jmcneill 		DEVMAP_ENTRY(MESON8B_AOBUS_VBASE,
    136       1.1  jmcneill 			     MESON8B_AOBUS_PBASE,
    137       1.1  jmcneill 			     MESON8B_AOBUS_SIZE),
    138       1.1  jmcneill 		DEVMAP_ENTRY(MESON8B_SRAM_VBASE,
    139       1.1  jmcneill 			     MESON8B_SRAM_PBASE,
    140       1.1  jmcneill 			     MESON8B_SRAM_SIZE),
    141       1.1  jmcneill 		DEVMAP_ENTRY_END
    142       1.1  jmcneill 	};
    143       1.1  jmcneill 
    144       1.1  jmcneill 	return devmap;
    145       1.1  jmcneill }
    146       1.1  jmcneill 
    147       1.1  jmcneill static void
    148       1.1  jmcneill meson_platform_init_attach_args(struct fdt_attach_args *faa)
    149       1.1  jmcneill {
    150       1.1  jmcneill 	faa->faa_bst = &meson_bs_tag;
    151       1.1  jmcneill 	faa->faa_dmat = &meson_dma_tag;
    152       1.1  jmcneill }
    153       1.1  jmcneill 
    154       1.1  jmcneill void meson_platform_early_putchar(char);
    155       1.1  jmcneill 
    156      1.15     skrll void __noasan
    157       1.1  jmcneill meson_platform_early_putchar(char c)
    158       1.1  jmcneill {
    159       1.1  jmcneill #ifdef CONSADDR
    160       1.6  jmcneill #define	CONSADDR_VA	((CONSADDR - MESON8B_AOBUS_PBASE) + MESON8B_AOBUS_VBASE)
    161       1.1  jmcneill 	volatile uint32_t *uartaddr = cpu_earlydevice_va_p() ?
    162       1.1  jmcneill 	    (volatile uint32_t *)CONSADDR_VA :
    163       1.1  jmcneill 	    (volatile uint32_t *)CONSADDR;
    164       1.1  jmcneill 	int timo = 150000;
    165       1.1  jmcneill 
    166       1.1  jmcneill 	while ((uartaddr[UART_STATUS_REG/4] & UART_STATUS_TX_EMPTY) == 0) {
    167       1.1  jmcneill 		if (--timo == 0)
    168       1.1  jmcneill 			break;
    169       1.1  jmcneill 	}
    170       1.1  jmcneill 
    171       1.1  jmcneill 	uartaddr[UART_WFIFO_REG/4] = c;
    172       1.1  jmcneill 
    173       1.1  jmcneill 	while ((uartaddr[UART_STATUS_REG/4] & UART_STATUS_TX_EMPTY) == 0) {
    174       1.1  jmcneill 		if (--timo == 0)
    175       1.1  jmcneill 			break;
    176       1.1  jmcneill 	}
    177       1.1  jmcneill #endif
    178       1.1  jmcneill }
    179       1.1  jmcneill 
    180       1.1  jmcneill static void
    181       1.1  jmcneill meson_platform_device_register(device_t self, void *aux)
    182       1.1  jmcneill {
    183       1.2  jmcneill 	prop_dictionary_t dict = device_properties(self);
    184       1.2  jmcneill 
    185       1.2  jmcneill 	if (device_is_a(self, "awge") && device_unit(self) == 0) {
    186       1.2  jmcneill 		uint8_t enaddr[ETHER_ADDR_LEN];
    187       1.2  jmcneill 		if (get_bootconf_option(boot_args, "awge0.mac-address",
    188       1.2  jmcneill 		    BOOTOPT_TYPE_MACADDR, enaddr)) {
    189      1.14     skrll 			prop_dictionary_set_data(dict, "mac-address", enaddr,
    190       1.2  jmcneill 			    sizeof(enaddr));
    191       1.2  jmcneill 		}
    192       1.2  jmcneill 	}
    193       1.2  jmcneill 
    194       1.5  jmcneill 	if (device_is_a(self, "mesonfb")) {
    195       1.2  jmcneill 		int scale, depth;
    196       1.2  jmcneill 
    197       1.2  jmcneill 		if (get_bootconf_option(boot_args, "fb.scale",
    198       1.2  jmcneill 		    BOOTOPT_TYPE_INT, &scale) && scale > 0) {
    199       1.2  jmcneill 			prop_dictionary_set_uint32(dict, "scale", scale);
    200       1.2  jmcneill 		}
    201       1.2  jmcneill 		if (get_bootconf_option(boot_args, "fb.depth",
    202       1.2  jmcneill 		    BOOTOPT_TYPE_INT, &depth)) {
    203       1.2  jmcneill 			prop_dictionary_set_uint32(dict, "depth", depth);
    204       1.2  jmcneill 		}
    205       1.2  jmcneill 	}
    206       1.1  jmcneill }
    207       1.1  jmcneill 
    208       1.3  jmcneill #if defined(SOC_MESON8B)
    209       1.3  jmcneill #define	MESON8B_BOOTINFO_REG	0xd901ff04
    210       1.3  jmcneill static int
    211       1.3  jmcneill meson8b_get_boot_id(void)
    212       1.3  jmcneill {
    213       1.3  jmcneill 	static int boot_id = -1;
    214       1.3  jmcneill 	bus_space_tag_t bst = &arm_generic_bs_tag;
    215       1.3  jmcneill 	bus_space_handle_t bsh;
    216       1.3  jmcneill 
    217       1.3  jmcneill 	if (boot_id == -1) {
    218       1.3  jmcneill 		if (bus_space_map(bst, MESON8B_BOOTINFO_REG, 4, 0, &bsh) != 0)
    219       1.3  jmcneill 			return -1;
    220       1.3  jmcneill 
    221       1.3  jmcneill 		boot_id = (int)bus_space_read_4(bst, bsh, 0);
    222       1.3  jmcneill 
    223       1.3  jmcneill 		bus_space_unmap(bst, bsh, 4);
    224       1.3  jmcneill 	}
    225       1.3  jmcneill 
    226       1.3  jmcneill 	return boot_id;
    227       1.3  jmcneill }
    228       1.3  jmcneill 
    229       1.3  jmcneill static void
    230       1.3  jmcneill meson8b_platform_device_register(device_t self, void *aux)
    231       1.3  jmcneill {
    232       1.3  jmcneill 	device_t parent = device_parent(self);
    233       1.3  jmcneill 	char *ptr;
    234       1.3  jmcneill 
    235       1.3  jmcneill 	if (device_is_a(self, "ld") &&
    236       1.3  jmcneill 	    device_is_a(parent, "sdmmc") &&
    237       1.3  jmcneill 	    (device_is_a(device_parent(parent), "mesonsdhc") ||
    238       1.3  jmcneill 	     device_is_a(device_parent(parent), "mesonsdio"))) {
    239       1.3  jmcneill 
    240       1.3  jmcneill 		const int boot_id = meson8b_get_boot_id();
    241       1.3  jmcneill 		const bool has_rootdev = get_bootconf_option(boot_args, "root", BOOTOPT_TYPE_STRING, &ptr) != 0;
    242       1.3  jmcneill 
    243       1.3  jmcneill 		if (!has_rootdev) {
    244       1.3  jmcneill 			char rootarg[64];
    245       1.3  jmcneill 			snprintf(rootarg, sizeof(rootarg), " root=%sa", device_xname(self));
    246       1.3  jmcneill 
    247       1.3  jmcneill 			/* Assume that SDIO is used for SD cards and SDHC is used for eMMC */
    248       1.3  jmcneill 			if (device_is_a(device_parent(parent), "mesonsdhc") && boot_id == 0)
    249       1.3  jmcneill 				strcat(boot_args, rootarg);
    250       1.3  jmcneill 			else if (device_is_a(device_parent(parent), "mesonsdio") && boot_id != 0)
    251       1.3  jmcneill 				strcat(boot_args, rootarg);
    252       1.3  jmcneill 		}
    253       1.3  jmcneill 	}
    254      1.12     skrll 
    255       1.3  jmcneill 	meson_platform_device_register(self, aux);
    256       1.3  jmcneill }
    257       1.3  jmcneill #endif
    258       1.3  jmcneill 
    259       1.1  jmcneill static u_int
    260       1.1  jmcneill meson_platform_uart_freq(void)
    261       1.1  jmcneill {
    262       1.1  jmcneill 	return 0;
    263       1.1  jmcneill }
    264       1.1  jmcneill 
    265       1.1  jmcneill static void
    266       1.1  jmcneill meson_platform_bootstrap(void)
    267       1.1  jmcneill {
    268       1.1  jmcneill 	arm_fdt_cpu_bootstrap();
    269       1.1  jmcneill 
    270       1.1  jmcneill 	void *fdt_data = __UNCONST(fdtbus_get_data());
    271       1.1  jmcneill 	const int chosen_off = fdt_path_offset(fdt_data, "/chosen");
    272       1.1  jmcneill 	if (chosen_off < 0)
    273       1.1  jmcneill 		return;
    274       1.1  jmcneill 
    275       1.1  jmcneill 	if (match_bootconf_option(boot_args, "console", "fb")) {
    276       1.1  jmcneill 		const int framebuffer_off =
    277       1.1  jmcneill 		    fdt_path_offset(fdt_data, "/chosen/framebuffer");
    278       1.1  jmcneill 		if (framebuffer_off >= 0) {
    279       1.1  jmcneill 			const char *status = fdt_getprop(fdt_data,
    280       1.1  jmcneill 			    framebuffer_off, "status", NULL);
    281       1.1  jmcneill 			if (status == NULL || strncmp(status, "ok", 2) == 0) {
    282       1.1  jmcneill 				fdt_setprop_string(fdt_data, chosen_off,
    283       1.1  jmcneill 				    "stdout-path", "/chosen/framebuffer");
    284       1.1  jmcneill 			}
    285       1.1  jmcneill 		}
    286       1.1  jmcneill 	} else if (match_bootconf_option(boot_args, "console", "serial")) {
    287       1.1  jmcneill 		fdt_setprop_string(fdt_data, chosen_off,
    288       1.1  jmcneill 		    "stdout-path", "serial0:115200n8");
    289       1.1  jmcneill 	}
    290       1.1  jmcneill }
    291       1.1  jmcneill 
    292       1.1  jmcneill #if defined(SOC_MESON8B)
    293       1.1  jmcneill static void
    294       1.1  jmcneill meson8b_platform_bootstrap(void)
    295       1.1  jmcneill {
    296       1.1  jmcneill 
    297       1.1  jmcneill #if NARML2CC > 0
    298       1.1  jmcneill 	const bus_space_handle_t pl310_bh = MESON8B_ARM_VBASE + MESON8B_ARM_PL310_BASE;
    299       1.1  jmcneill 	arml2cc_init(&arm_generic_bs_tag, pl310_bh, 0);
    300       1.1  jmcneill #endif
    301       1.1  jmcneill 
    302       1.1  jmcneill 	meson_platform_bootstrap();
    303       1.1  jmcneill }
    304       1.1  jmcneill 
    305       1.1  jmcneill static void
    306       1.9  jmcneill meson8b_platform_reset(void)
    307       1.1  jmcneill {
    308       1.1  jmcneill 	bus_space_tag_t bst = &meson_bs_tag;
    309       1.1  jmcneill 	bus_space_handle_t bsh;
    310       1.1  jmcneill 
    311       1.9  jmcneill 	bus_space_map(bst, MESON8B_WATCHDOG_BASE, MESON8B_WATCHDOG_SIZE, 0, &bsh);
    312       1.1  jmcneill 
    313       1.9  jmcneill 	bus_space_write_4(bst, bsh, MESON8B_WATCHDOG_TC,
    314       1.9  jmcneill 	    MESON8B_WATCHDOG_TC_CPUS | MESON8B_WATCHDOG_TC_ENABLE | __SHIFTIN(0xfff, MESON8B_WATCHDOG_TC_TCNT));
    315       1.9  jmcneill 	bus_space_write_4(bst, bsh, MESON8B_WATCHDOG_RESET, 0);
    316       1.1  jmcneill 
    317       1.1  jmcneill 	for (;;) {
    318       1.1  jmcneill 		__asm("wfi");
    319       1.1  jmcneill 	}
    320       1.1  jmcneill }
    321       1.1  jmcneill 
    322      1.17       rin #ifdef MULTIPROCESSOR
    323       1.1  jmcneill static void
    324       1.1  jmcneill meson8b_mpinit_delay(u_int n)
    325       1.1  jmcneill {
    326       1.1  jmcneill 	for (volatile int i = 0; i < n; i++)
    327       1.1  jmcneill 		;
    328       1.1  jmcneill }
    329      1.17       rin #endif
    330       1.1  jmcneill 
    331       1.1  jmcneill static int
    332       1.1  jmcneill cpu_enable_meson8b(int phandle)
    333       1.1  jmcneill {
    334      1.17       rin #ifdef MULTIPROCESSOR
    335       1.1  jmcneill 	const bus_addr_t cbar = armreg_cbar_read();
    336       1.1  jmcneill 	bus_space_tag_t bst = &arm_generic_bs_tag;
    337       1.1  jmcneill 
    338       1.1  jmcneill 	const bus_space_handle_t scu_bsh =
    339       1.1  jmcneill 	    cbar - MESON8B_ARM_PBASE + MESON8B_ARM_VBASE;
    340       1.1  jmcneill 	const bus_space_handle_t cpuconf_bsh =
    341       1.1  jmcneill 	    MESON8B_SRAM_VBASE + MESON8B_SRAM_CPUCONF_OFFSET;
    342       1.1  jmcneill 	const bus_space_handle_t ao_bsh =
    343       1.7  jmcneill 	    MESON8B_AOBUS_VBASE + MESON8B_AOBUS_RTI_OFFSET;
    344       1.1  jmcneill 	const bus_space_handle_t cbus_bsh =
    345       1.1  jmcneill 	    MESON_CORE_APB3_VBASE + MESON_CBUS_OFFSET;
    346       1.1  jmcneill 	uint32_t pwr_sts, pwr_cntl0, pwr_cntl1, cpuclk, mempd0;
    347       1.1  jmcneill 	uint64_t mpidr;
    348       1.1  jmcneill 
    349       1.1  jmcneill 	fdtbus_get_reg64(phandle, 0, &mpidr, NULL);
    350       1.1  jmcneill 
    351       1.1  jmcneill 	const u_int cpuno = __SHIFTOUT(mpidr, MPIDR_AFF0);
    352       1.1  jmcneill 
    353       1.1  jmcneill 	bus_space_write_4(bst, cpuconf_bsh, MESON8B_SRAM_CPUCONF_CPU_ADDR_REG(cpuno),
    354       1.1  jmcneill 	    KERN_VTOPHYS((vaddr_t)cpu_mpstart));
    355       1.1  jmcneill 
    356       1.1  jmcneill 	pwr_sts = bus_space_read_4(bst, scu_bsh, SCU_CPU_PWR_STS);
    357       1.1  jmcneill 	pwr_sts &= ~(3 << (8 * cpuno));
    358       1.1  jmcneill 	bus_space_write_4(bst, scu_bsh, SCU_CPU_PWR_STS, pwr_sts);
    359       1.1  jmcneill 
    360       1.1  jmcneill 	pwr_cntl0 = bus_space_read_4(bst, ao_bsh, MESON_AOBUS_PWR_CTRL0_REG);
    361       1.1  jmcneill 	pwr_cntl0 &= ~((3 << 18) << ((cpuno - 1) * 2));
    362       1.1  jmcneill 	bus_space_write_4(bst, ao_bsh, MESON_AOBUS_PWR_CTRL0_REG, pwr_cntl0);
    363       1.1  jmcneill 
    364       1.1  jmcneill 	meson8b_mpinit_delay(5000);
    365       1.1  jmcneill 
    366       1.1  jmcneill 	cpuclk = bus_space_read_4(bst, cbus_bsh, MESON_CBUS_CPU_CLK_CNTL_REG);
    367       1.1  jmcneill 	cpuclk |= (1 << (24 + cpuno));
    368       1.1  jmcneill 	bus_space_write_4(bst, cbus_bsh, MESON_CBUS_CPU_CLK_CNTL_REG, cpuclk);
    369       1.1  jmcneill 
    370       1.1  jmcneill 	mempd0 = bus_space_read_4(bst, ao_bsh, MESON_AOBUS_PWR_MEM_PD0_REG);
    371       1.1  jmcneill 	mempd0 &= ~((uint32_t)(0xf << 28) >> ((cpuno - 1) * 4));
    372       1.1  jmcneill 	bus_space_write_4(bst, ao_bsh, MESON_AOBUS_PWR_MEM_PD0_REG, mempd0);
    373       1.1  jmcneill 
    374       1.1  jmcneill 	pwr_cntl1 = bus_space_read_4(bst, ao_bsh, MESON_AOBUS_PWR_CTRL1_REG);
    375       1.1  jmcneill 	pwr_cntl1 &= ~((3 << 4) << ((cpuno - 1) * 2));
    376       1.1  jmcneill 	bus_space_write_4(bst, ao_bsh, MESON_AOBUS_PWR_CTRL1_REG, pwr_cntl1);
    377       1.1  jmcneill 
    378       1.1  jmcneill 	meson8b_mpinit_delay(10000);
    379       1.1  jmcneill 
    380       1.1  jmcneill 	for (;;) {
    381       1.1  jmcneill 		pwr_cntl1 = bus_space_read_4(bst, ao_bsh,
    382       1.1  jmcneill 		    MESON_AOBUS_PWR_CTRL1_REG) & ((1 << 17) << (cpuno - 1));
    383       1.1  jmcneill 		if (pwr_cntl1)
    384       1.1  jmcneill 			break;
    385       1.1  jmcneill 		meson8b_mpinit_delay(10000);
    386       1.1  jmcneill 	}
    387       1.1  jmcneill 
    388       1.1  jmcneill 	pwr_cntl0 = bus_space_read_4(bst, ao_bsh, MESON_AOBUS_PWR_CTRL0_REG);
    389       1.1  jmcneill 	pwr_cntl0 &= ~(1 << cpuno);
    390       1.1  jmcneill 	bus_space_write_4(bst, ao_bsh, MESON_AOBUS_PWR_CTRL0_REG, pwr_cntl0);
    391       1.1  jmcneill 
    392       1.1  jmcneill 	cpuclk = bus_space_read_4(bst, cbus_bsh, MESON_CBUS_CPU_CLK_CNTL_REG);
    393       1.1  jmcneill 	cpuclk &= ~(1 << (24 + cpuno));
    394       1.1  jmcneill 	bus_space_write_4(bst, cbus_bsh, MESON_CBUS_CPU_CLK_CNTL_REG, cpuclk);
    395       1.1  jmcneill 
    396       1.1  jmcneill 	bus_space_write_4(bst, cpuconf_bsh, MESON8B_SRAM_CPUCONF_CPU_ADDR_REG(cpuno),
    397       1.1  jmcneill 	    KERN_VTOPHYS((vaddr_t)cpu_mpstart));
    398       1.1  jmcneill 
    399       1.1  jmcneill 	uint32_t ctrl = bus_space_read_4(bst, cpuconf_bsh, MESON8B_SRAM_CPUCONF_CTRL_REG);
    400       1.1  jmcneill 	ctrl |= __BITS(cpuno,0);
    401       1.1  jmcneill 	bus_space_write_4(bst, cpuconf_bsh, MESON8B_SRAM_CPUCONF_CTRL_REG, ctrl);
    402      1.17       rin #endif
    403       1.1  jmcneill 
    404       1.1  jmcneill 	return 0;
    405       1.1  jmcneill }
    406       1.1  jmcneill 
    407       1.1  jmcneill ARM_CPU_METHOD(meson8b, "amlogic,meson8b-smp", cpu_enable_meson8b);
    408       1.1  jmcneill 
    409       1.4     skrll static int
    410       1.5  jmcneill meson8b_mpstart(void)
    411       1.1  jmcneill {
    412       1.4     skrll 	int ret = 0;
    413       1.1  jmcneill 	const bus_addr_t cbar = armreg_cbar_read();
    414       1.1  jmcneill 	bus_space_tag_t bst = &arm_generic_bs_tag;
    415       1.1  jmcneill 
    416       1.1  jmcneill 	if (cbar == 0)
    417       1.4     skrll 		return ret;
    418       1.1  jmcneill 
    419       1.1  jmcneill 	const bus_space_handle_t scu_bsh =
    420       1.1  jmcneill 	    cbar - MESON8B_ARM_PBASE + MESON8B_ARM_VBASE;
    421       1.1  jmcneill 
    422       1.1  jmcneill 	const uint32_t scu_cfg = bus_space_read_4(bst, scu_bsh, SCU_CFG);
    423       1.1  jmcneill 	const u_int ncpus = (scu_cfg & SCU_CFG_CPUMAX) + 1;
    424       1.1  jmcneill 
    425       1.1  jmcneill 	if (ncpus < 2)
    426       1.4     skrll 		return ret;
    427       1.1  jmcneill 
    428       1.1  jmcneill 	/*
    429       1.1  jmcneill 	 * Invalidate all SCU cache tags. That is, for all cores (0-3)
    430       1.1  jmcneill 	 */
    431       1.1  jmcneill 	bus_space_write_4(bst, scu_bsh, SCU_INV_ALL_REG, 0xffff);
    432       1.1  jmcneill 
    433       1.1  jmcneill 	uint32_t scu_ctl = bus_space_read_4(bst, scu_bsh, SCU_CTL);
    434       1.1  jmcneill 	scu_ctl |= SCU_CTL_SCU_ENA;
    435       1.1  jmcneill 	bus_space_write_4(bst, scu_bsh, SCU_CTL, scu_ctl);
    436       1.1  jmcneill 
    437       1.1  jmcneill 	armv7_dcache_wbinv_all();
    438       1.1  jmcneill 
    439       1.4     skrll 	ret = arm_fdt_cpu_mpstart();
    440       1.4     skrll 	return ret;
    441       1.1  jmcneill }
    442       1.1  jmcneill 
    443       1.1  jmcneill static const struct arm_platform meson8b_platform = {
    444      1.11  jmcneill 	.ap_devmap = meson_platform_devmap,
    445       1.1  jmcneill 	.ap_bootstrap = meson8b_platform_bootstrap,
    446       1.1  jmcneill 	.ap_init_attach_args = meson_platform_init_attach_args,
    447       1.3  jmcneill 	.ap_device_register = meson8b_platform_device_register,
    448      1.11  jmcneill 	.ap_reset = meson8b_platform_reset,
    449      1.13     skrll 	.ap_delay = a9ptmr_delay,
    450       1.1  jmcneill 	.ap_uart_freq = meson_platform_uart_freq,
    451       1.5  jmcneill 	.ap_mpstart = meson8b_mpstart,
    452       1.1  jmcneill };
    453       1.1  jmcneill 
    454       1.1  jmcneill ARM_PLATFORM(meson8b, "amlogic,meson8b", &meson8b_platform);
    455       1.5  jmcneill #endif	/* SOC_MESON8B */
    456       1.5  jmcneill 
    457       1.8  jmcneill #if defined(SOC_MESONGX)
    458       1.9  jmcneill static void
    459       1.9  jmcneill mesongx_platform_reset(void)
    460       1.9  jmcneill {
    461       1.9  jmcneill 	bus_space_tag_t bst = &meson_bs_tag;
    462       1.9  jmcneill 	bus_space_handle_t bsh;
    463       1.9  jmcneill 	uint32_t val;
    464       1.9  jmcneill 
    465       1.9  jmcneill 	bus_space_map(bst, MESONGX_WATCHDOG_BASE, MESONGX_WATCHDOG_SIZE, 0, &bsh);
    466       1.9  jmcneill 
    467      1.10  jmcneill 	val = MESONGX_WATCHDOG_CNTL_SYS_RESET_N_EN |
    468      1.10  jmcneill 	      MESONGX_WATCHDOG_CNTL_WDOG_EN |
    469      1.10  jmcneill 	      MESONGX_WATCHDOG_CNTL_CLK_EN;
    470       1.9  jmcneill 	bus_space_write_4(bst, bsh, MESONGX_WATCHDOG_CNTL, val);
    471       1.9  jmcneill 
    472       1.9  jmcneill 	bus_space_write_4(bst, bsh, MESONGX_WATCHDOG_TCNT, 1);
    473       1.9  jmcneill 
    474       1.9  jmcneill 	bus_space_write_4(bst, bsh, MESONGX_WATCHDOG_RESET, 0);
    475       1.9  jmcneill 
    476       1.9  jmcneill 	for (;;) {
    477       1.9  jmcneill 		__asm("wfi");
    478       1.9  jmcneill 	}
    479       1.9  jmcneill }
    480       1.9  jmcneill 
    481       1.8  jmcneill static const struct arm_platform mesongx_platform = {
    482       1.5  jmcneill 	.ap_devmap = meson_platform_devmap,
    483       1.5  jmcneill 	.ap_bootstrap = meson_platform_bootstrap,
    484       1.5  jmcneill 	.ap_init_attach_args = meson_platform_init_attach_args,
    485       1.5  jmcneill 	.ap_device_register = meson_platform_device_register,
    486       1.9  jmcneill 	.ap_reset = mesongx_platform_reset,
    487       1.5  jmcneill 	.ap_delay = gtmr_delay,
    488       1.5  jmcneill 	.ap_uart_freq = meson_platform_uart_freq,
    489       1.5  jmcneill 	.ap_mpstart = arm_fdt_cpu_mpstart,
    490       1.5  jmcneill };
    491       1.5  jmcneill 
    492       1.8  jmcneill #if defined(SOC_MESONGXBB)
    493       1.8  jmcneill ARM_PLATFORM(mesongxbb, "amlogic,meson-gxbb", &mesongx_platform);
    494       1.8  jmcneill #endif	/* SOC_MESONGXBB */
    495       1.8  jmcneill #if defined(SOC_MESONGXL)
    496       1.8  jmcneill ARM_PLATFORM(mesongxl, "amlogic,meson-gxl", &mesongx_platform);
    497       1.8  jmcneill #endif	/* SOC_MESONGXL */
    498       1.8  jmcneill #endif	/* SOC_MESONGX */
    499