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