11.24Sthorpej/* $NetBSD: vexpress_platform.c,v 1.24 2025/09/06 21:02:41 thorpej Exp $ */
21.1Sjmcneill
31.1Sjmcneill/*-
41.1Sjmcneill * Copyright (c) 2017 Jared McNeill <jmcneill@invisible.ca>
51.1Sjmcneill * All rights reserved.
61.1Sjmcneill *
71.1Sjmcneill * Redistribution and use in source and binary forms, with or without
81.1Sjmcneill * modification, are permitted provided that the following conditions
91.1Sjmcneill * are met:
101.1Sjmcneill * 1. Redistributions of source code must retain the above copyright
111.1Sjmcneill *    notice, this list of conditions and the following disclaimer.
121.1Sjmcneill * 2. Redistributions in binary form must reproduce the above copyright
131.1Sjmcneill *    notice, this list of conditions and the following disclaimer in the
141.1Sjmcneill *    documentation and/or other materials provided with the distribution.
151.1Sjmcneill *
161.1Sjmcneill * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
171.1Sjmcneill * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
181.1Sjmcneill * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
191.1Sjmcneill * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
201.1Sjmcneill * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
211.1Sjmcneill * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
221.1Sjmcneill * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
231.1Sjmcneill * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
241.1Sjmcneill * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
251.1Sjmcneill * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
261.1Sjmcneill * SUCH DAMAGE.
271.1Sjmcneill */
281.1Sjmcneill
291.1Sjmcneill#include "opt_multiprocessor.h"
301.10Sskrll#include "opt_console.h"
311.1Sjmcneill
321.1Sjmcneill#include <sys/cdefs.h>
331.24Sthorpej__KERNEL_RCSID(0, "$NetBSD: vexpress_platform.c,v 1.24 2025/09/06 21:02:41 thorpej Exp $");
341.1Sjmcneill
351.1Sjmcneill#include <sys/param.h>
361.1Sjmcneill#include <sys/bus.h>
371.1Sjmcneill#include <sys/cpu.h>
381.1Sjmcneill#include <sys/device.h>
391.1Sjmcneill#include <sys/termios.h>
401.1Sjmcneill
411.1Sjmcneill#include <dev/fdt/fdtvar.h>
421.24Sthorpej#include <dev/fdt/fdt_platform.h>
431.1Sjmcneill
441.1Sjmcneill#include <uvm/uvm_extern.h>
451.1Sjmcneill
461.1Sjmcneill#include <machine/bootconfig.h>
471.1Sjmcneill#include <arm/cpufunc.h>
481.1Sjmcneill
491.1Sjmcneill#include <arm/fdt/arm_fdtvar.h>
501.1Sjmcneill
511.1Sjmcneill#include <arm/cortex/gtmr_var.h>
521.1Sjmcneill
531.2Sjmcneill#include <arm/cortex/gic_reg.h>
541.2Sjmcneill
551.8Sjakllsch#include <evbarm/dev/plcomreg.h>
561.11Sskrll#include <evbarm/fdt/machdep.h>
571.1Sjmcneill
581.2Sjmcneill#include <arm/vexpress/vexpress_platform.h>
591.2Sjmcneill
601.3Sjmcneill#include <libfdt.h>
611.3Sjmcneill
621.1Sjmcneill#define	VEXPRESS_REF_FREQ	24000000
631.1Sjmcneill
641.1Sjmcneillextern struct bus_space armv7_generic_bs_tag;
651.7Sryoextern struct arm32_bus_dma_tag arm_generic_dma_tag;
661.1Sjmcneill
671.1Sjmcneill#define	SYSREG_BASE		0x1c010000
681.1Sjmcneill#define	SYSREG_SIZE		0x1000
691.1Sjmcneill
701.1Sjmcneill#define	SYS_FLAGS		0x0030
711.1Sjmcneill#define	SYS_FLAGSCLR		0x0034
721.1Sjmcneill#define	SYS_CFGDATA		0x00a0
731.1Sjmcneill#define	SYS_CFGCTRL		0x00a4
741.1Sjmcneill#define	 SYS_CFGCTRL_START	__BIT(31)
751.1Sjmcneill#define	 SYS_CFGCTRL_WRITE	__BIT(30)
761.1Sjmcneill#define	 SYS_CFGCTRL_DCC	__BITS(29,26)
771.1Sjmcneill#define	 SYS_CFGCTRL_FUNCTION	__BITS(25,20)
781.1Sjmcneill#define	  SYS_CFGCTRL_FUNCTION_SHUTDOWN	8
791.1Sjmcneill#define	  SYS_CFGCTRL_FUNCTION_REBOOT	9
801.1Sjmcneill#define	 SYS_CFGCTRL_SITE	__BITS(17,16)
811.1Sjmcneill#define	 SYS_CFGCTRL_POSITION	__BITS(15,12)
821.1Sjmcneill#define	 SYS_CFGCTRL_DEVICE	__BITS(11,0)
831.1Sjmcneill#define	SYS_CFGSTAT		0x00a8
841.1Sjmcneill#define	 SYS_CFGSTAT_ERROR	__BIT(1)
851.1Sjmcneill#define	 SYS_CFGSTAT_COMPLETE	__BIT(0)
861.1Sjmcneill
871.1Sjmcneillstatic bus_space_tag_t sysreg_bst = &armv7_generic_bs_tag;
881.1Sjmcneillstatic bus_space_handle_t sysreg_bsh;
891.1Sjmcneill
901.1Sjmcneill#define	SYSREG_WRITE(o, v)	\
911.1Sjmcneill	bus_space_write_4(sysreg_bst, sysreg_bsh, (o), (v))
921.1Sjmcneill
931.11Sskrllvoid vexpress_platform_early_putchar(char);
941.1Sjmcneill
951.17Sskrllvoid __noasan
961.12Sskrllvexpress_platform_early_putchar(char c)
971.12Sskrll{
981.12Sskrll#ifdef CONSADDR
991.12Sskrll#define CONSADDR_VA ((CONSADDR - VEXPRESS_CORE_PBASE) + VEXPRESS_CORE_VBASE)
1001.12Sskrll	volatile uint32_t *uartaddr = cpu_earlydevice_va_p() ?
1011.12Sskrll	    (volatile uint32_t *)CONSADDR_VA :
1021.12Sskrll	    (volatile uint32_t *)CONSADDR;
1031.12Sskrll
1041.12Sskrll	while ((le32toh(uartaddr[PL01XCOM_FR / 4]) & PL01X_FR_TXFF) != 0)
1051.12Sskrll		continue;
1061.12Sskrll
1071.12Sskrll	uartaddr[PL01XCOM_DR / 4] = htole32(c);
1081.19Sskrll	dsb(sy);
1091.12Sskrll
1101.12Sskrll	while ((le32toh(uartaddr[PL01XCOM_FR / 4]) & PL01X_FR_TXFE) == 0)
1111.12Sskrll		continue;
1121.12Sskrll#endif
1131.12Sskrll}
1141.12Sskrll
1151.12Sskrll
1161.13Sskrllstatic int
1171.2Sjmcneillvexpress_a15_smp_init(void)
1181.2Sjmcneill{
1191.13Sskrll	int ret = 0;
1201.11Sskrll#ifdef MULTIPROCESSOR
1211.2Sjmcneill	bus_space_tag_t gicd_bst = &armv7_generic_bs_tag;
1221.2Sjmcneill	bus_space_handle_t gicd_bsh;
1231.2Sjmcneill
1241.2Sjmcneill	/* Write init vec to SYS_FLAGS register */
1251.2Sjmcneill	SYSREG_WRITE(SYS_FLAGSCLR, 0xffffffff);
1261.11Sskrll	SYSREG_WRITE(SYS_FLAGS, KERN_VTOPHYS((vaddr_t)cpu_mpstart));
1271.2Sjmcneill
1281.2Sjmcneill	/* Map GIC distributor */
1291.2Sjmcneill	bus_space_map(gicd_bst, VEXPRESS_GIC_PBASE + GICD_BASE,
1301.2Sjmcneill	    0x1000, 0, &gicd_bsh);
1311.2Sjmcneill
1321.2Sjmcneill	/* Enable GIC distributor */
1331.2Sjmcneill	bus_space_write_4(gicd_bst, gicd_bsh,
1341.2Sjmcneill	    GICD_CTRL, GICD_CTRL_Enable);
1351.2Sjmcneill
1361.2Sjmcneill	/* Send sw interrupt to APs */
1371.2Sjmcneill	const uint32_t sgir = GICD_SGIR_TargetListFilter_NotMe;
1381.2Sjmcneill	bus_space_write_4(gicd_bst, gicd_bsh, GICD_SGIR, sgir);
1391.2Sjmcneill
1401.16Sskrll	/* Bitmask of CPUs (non-BSP) to start */
1411.16Sskrll	for (u_int cpuindex = 1; cpuindex < arm_cpu_max; cpuindex++) {
1421.16Sskrll		u_int i;
1431.16Sskrll		for (i = 0x10000000; i > 0; i--) {
1441.16Sskrll			if (cpu_hatched_p(cpuindex))
1451.16Sskrll				break;
1461.16Sskrll		}
1471.16Sskrll
1481.16Sskrll		if (i == 0) {
1491.16Sskrll			ret++;
1501.16Sskrll			aprint_error("cpu%d: WARNING: AP failed to start\n",
1511.16Sskrll			    cpuindex);
1521.16Sskrll		}
1531.13Sskrll	}
1541.2Sjmcneill
1551.2Sjmcneill	/* Disable GIC distributor */
1561.2Sjmcneill	bus_space_write_4(gicd_bst, gicd_bsh, GICD_CTRL, 0);
1571.11Sskrll#endif
1581.13Sskrll	return ret;
1591.2Sjmcneill}
1601.2Sjmcneill
1611.2Sjmcneill
1621.1Sjmcneillstatic const struct pmap_devmap *
1631.1Sjmcneillvexpress_platform_devmap(void)
1641.1Sjmcneill{
1651.1Sjmcneill	static const struct pmap_devmap devmap[] = {
1661.1Sjmcneill		DEVMAP_ENTRY(VEXPRESS_CORE_VBASE,
1671.1Sjmcneill			     VEXPRESS_CORE_PBASE,
1681.1Sjmcneill			     VEXPRESS_CORE_SIZE),
1691.2Sjmcneill		DEVMAP_ENTRY(VEXPRESS_GIC_VBASE,
1701.2Sjmcneill			     VEXPRESS_GIC_PBASE,
1711.2Sjmcneill			     VEXPRESS_GIC_SIZE),
1721.1Sjmcneill		DEVMAP_ENTRY_END
1731.6Sskrll	};
1741.1Sjmcneill
1751.1Sjmcneill	return devmap;
1761.1Sjmcneill}
1771.1Sjmcneill
1781.1Sjmcneillstatic void
1791.1Sjmcneillvexpress_platform_bootstrap(void)
1801.1Sjmcneill{
1811.1Sjmcneill	bus_space_map(sysreg_bst, SYSREG_BASE, SYSREG_SIZE, 0,
1821.1Sjmcneill	    &sysreg_bsh);
1831.2Sjmcneill
1841.11Sskrll#ifdef MULTIPROCESSOR
1851.2Sjmcneill	arm_cpu_max = 1 + __SHIFTOUT(armreg_l2ctrl_read(), L2CTRL_NUMCPU);
1861.11Sskrll#endif
1871.1Sjmcneill}
1881.1Sjmcneill
1891.1Sjmcneillstatic void
1901.1Sjmcneillvexpress_platform_init_attach_args(struct fdt_attach_args *faa)
1911.1Sjmcneill{
1921.1Sjmcneill	faa->faa_bst = &armv7_generic_bs_tag;
1931.7Sryo	faa->faa_dmat = &arm_generic_dma_tag;
1941.1Sjmcneill}
1951.1Sjmcneill
1961.1Sjmcneillstatic void
1971.1Sjmcneillvexpress_platform_device_register(device_t self, void *aux)
1981.1Sjmcneill{
1991.1Sjmcneill}
2001.1Sjmcneill
2011.1Sjmcneillstatic void
2021.1Sjmcneillvexpress_platform_reset(void)
2031.1Sjmcneill{
2041.1Sjmcneill	SYSREG_WRITE(SYS_CFGSTAT, 0);
2051.1Sjmcneill	SYSREG_WRITE(SYS_CFGDATA, 0);
2061.1Sjmcneill	SYSREG_WRITE(SYS_CFGCTRL,
2071.1Sjmcneill	    SYS_CFGCTRL_START |
2081.1Sjmcneill	    SYS_CFGCTRL_WRITE |
2091.1Sjmcneill	    __SHIFTIN(SYS_CFGCTRL_FUNCTION_REBOOT,
2101.1Sjmcneill		      SYS_CFGCTRL_FUNCTION));
2111.1Sjmcneill}
2121.1Sjmcneill
2131.1Sjmcneillstatic u_int
2141.1Sjmcneillvexpress_platform_uart_freq(void)
2151.1Sjmcneill{
2161.1Sjmcneill	return VEXPRESS_REF_FREQ;
2171.1Sjmcneill}
2181.1Sjmcneill
2191.23Sskrllstatic const struct fdt_platform vexpress_platform = {
2201.23Sskrll	.fp_devmap = vexpress_platform_devmap,
2211.23Sskrll	.fp_bootstrap = vexpress_platform_bootstrap,
2221.23Sskrll	.fp_mpstart = vexpress_a15_smp_init,
2231.23Sskrll	.fp_init_attach_args = vexpress_platform_init_attach_args,
2241.23Sskrll	.fp_device_register = vexpress_platform_device_register,
2251.23Sskrll	.fp_reset = vexpress_platform_reset,
2261.23Sskrll	.fp_delay = gtmr_delay,
2271.23Sskrll	.fp_uart_freq = vexpress_platform_uart_freq,
2281.1Sjmcneill};
2291.1Sjmcneill
2301.23SskrllFDT_PLATFORM(vexpress, "arm,vexpress", &vexpress_platform);
231