zynq_platform.c revision 1.11
11.11Sskrll/*	$NetBSD: zynq_platform.c,v 1.11 2023/04/07 08:55:31 skrll Exp $	*/
21.1Sskrll
31.1Sskrll/*-
41.1Sskrll * Copyright (c) 2019 The NetBSD Foundation, Inc.
51.1Sskrll * All rights reserved.
61.1Sskrll *
71.1Sskrll * This code is derived from software contributed to The NetBSD Foundation
81.1Sskrll * by Nick Hudson
91.1Sskrll *
101.1Sskrll * Redistribution and use in source and binary forms, with or without
111.1Sskrll * modification, are permitted provided that the following conditions
121.1Sskrll * are met:
131.1Sskrll * 1. Redistributions of source code must retain the above copyright
141.1Sskrll *    notice, this list of conditions and the following disclaimer.
151.1Sskrll * 2. Redistributions in binary form must reproduce the above copyright
161.1Sskrll *    notice, this list of conditions and the following disclaimer in the
171.1Sskrll *    documentation and/or other materials provided with the distribution.
181.1Sskrll *
191.1Sskrll * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
201.1Sskrll * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
211.1Sskrll * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
221.1Sskrll * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
231.1Sskrll * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
241.1Sskrll * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
251.1Sskrll * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
261.1Sskrll * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
271.1Sskrll * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
281.1Sskrll * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
291.1Sskrll * POSSIBILITY OF SUCH DAMAGE.
301.1Sskrll */
311.1Sskrll
321.1Sskrll#include "opt_console.h"
331.1Sskrll#include "opt_soc.h"
341.1Sskrll
351.1Sskrll#include "arml2cc.h"
361.1Sskrll
371.1Sskrll#include <sys/cdefs.h>
381.11Sskrll__KERNEL_RCSID(0, "$NetBSD: zynq_platform.c,v 1.11 2023/04/07 08:55:31 skrll Exp $");
391.1Sskrll
401.1Sskrll#include <sys/param.h>
411.1Sskrll#include <sys/bus.h>
421.1Sskrll#include <sys/cpu.h>
431.1Sskrll#include <sys/device.h>
441.1Sskrll
451.1Sskrll#include <dev/fdt/fdtvar.h>
461.1Sskrll#include <arm/fdt/arm_fdtvar.h>
471.1Sskrll
481.1Sskrll#include <uvm/uvm_extern.h>
491.1Sskrll
501.1Sskrll#include <machine/bootconfig.h>
511.1Sskrll
521.1Sskrll#include <arm/cortex/a9tmr_var.h>
531.7Sjmcneill#include <arm/cortex/scu_reg.h>
541.1Sskrll#include <arm/xilinx/zynq_uartreg.h>
551.1Sskrll
561.1Sskrll#include <evbarm/fdt/platform.h>
571.1Sskrll
581.1Sskrll#include <libfdt.h>
591.1Sskrll
601.1Sskrll#include <arm/cortex/pl310_var.h>
611.1Sskrll
621.7Sjmcneill#include <arm/arm32/machdep.h>
631.7Sjmcneill
641.1Sskrll#define	ZYNQ_REF_FREQ	24000000
651.1Sskrll#define	ZYNQ7000_DDR_PBASE	0x00000000
661.1Sskrll#define	ZYNQ7000_DDR_SIZE	0x40000000
671.1Sskrll
681.1Sskrll#define	ZYNQ_IOREG_VBASE	KERNEL_IO_VBASE
691.1Sskrll#define	ZYNQ_IOREG_PBASE	0xe0000000
701.1Sskrll#define ZYNQ_IOREG_SIZE		0x00200000
711.1Sskrll
721.10Sjmcneill#define	ZYNQ_SLCR_VBASE		(ZYNQ_IOREG_VBASE + ZYNQ_IOREG_SIZE)
731.10Sjmcneill#define	ZYNQ_SLCR_PBASE		0xf8000000
741.10Sjmcneill#define	ZYNQ_SLCR_SIZE		0x00100000
751.10Sjmcneill
761.10Sjmcneill#define ZYNQ_GPV_VBASE		(ZYNQ_SLCR_VBASE + ZYNQ_SLCR_SIZE)
771.1Sskrll#define ZYNQ_GPV_PBASE		0xf8900000
781.1Sskrll#define ZYNQ_GPV_SIZE		0x00100000
791.1Sskrll
801.1Sskrll#define ZYNQ_ARMCORE_VBASE	(ZYNQ_GPV_VBASE + ZYNQ_GPV_SIZE)
811.1Sskrll#define ZYNQ_ARMCORE_PBASE	0xf8f00000
821.9Sjmcneill#define ZYNQ_ARMCORE_SIZE	0x00100000
831.1Sskrll
841.8Sjmcneill#define	ZYNQ_OCM_VBASE		(ZYNQ_ARMCORE_VBASE + ZYNQ_ARMCORE_SIZE)
851.8Sjmcneill#define	ZYNQ_OCM_PBASE		0xfff00000
861.8Sjmcneill#define	ZYNQ_OCM_SIZE		0x00100000
871.8Sjmcneill
881.7Sjmcneill#define	ZYNQ_ARMCORE_SCU_BASE	0x00000000
891.7Sjmcneill#define	ZYNQ_ARMCORE_L2C_BASE	0x00002000
901.7Sjmcneill
911.7Sjmcneill#define	ZYNQ7000_CPU1_ENTRY	0xfffffff0
921.7Sjmcneill#define	ZYNQ7000_CPU1_ENTRY_SZ	4
931.7Sjmcneill
941.10Sjmcneill/* SLCR registers */
951.10Sjmcneill#define	SLCR_UNLOCK		0x008
961.10Sjmcneill#define	 UNLOCK_KEY		0xdf0d
971.10Sjmcneill#define	PSS_RST_CTRL		0x200
981.10Sjmcneill#define	 SOFT_RST		__BIT(0)
991.10Sjmcneill
1001.1Sskrllextern struct bus_space arm_generic_bs_tag;
1011.1Sskrllextern struct arm32_bus_dma_tag arm_generic_dma_tag;
1021.1Sskrll
1031.1Sskrllvoid zynq_platform_early_putchar(char);
1041.1Sskrll
1051.1Sskrllstatic const struct pmap_devmap *
1061.1Sskrllzynq_platform_devmap(void)
1071.1Sskrll{
1081.1Sskrll	static const struct pmap_devmap devmap[] = {
1091.1Sskrll		DEVMAP_ENTRY(ZYNQ_IOREG_VBASE,
1101.1Sskrll			     ZYNQ_IOREG_PBASE,
1111.1Sskrll			     ZYNQ_IOREG_SIZE),
1121.10Sjmcneill		DEVMAP_ENTRY(ZYNQ_SLCR_VBASE,
1131.10Sjmcneill			     ZYNQ_SLCR_PBASE,
1141.10Sjmcneill			     ZYNQ_SLCR_SIZE),
1151.1Sskrll		DEVMAP_ENTRY(ZYNQ_GPV_VBASE,
1161.1Sskrll			     ZYNQ_GPV_PBASE,
1171.1Sskrll			     ZYNQ_GPV_SIZE),
1181.1Sskrll		DEVMAP_ENTRY(ZYNQ_ARMCORE_VBASE,
1191.1Sskrll			     ZYNQ_ARMCORE_PBASE,
1201.1Sskrll			     ZYNQ_ARMCORE_SIZE),
1211.8Sjmcneill		DEVMAP_ENTRY(ZYNQ_OCM_VBASE,
1221.8Sjmcneill			     ZYNQ_OCM_PBASE,
1231.8Sjmcneill			     ZYNQ_OCM_SIZE),
1241.1Sskrll		DEVMAP_ENTRY_END
1251.1Sskrll	};
1261.1Sskrll
1271.1Sskrll	return devmap;
1281.1Sskrll}
1291.1Sskrll
1301.1Sskrllstatic void
1311.1Sskrllzynq_platform_init_attach_args(struct fdt_attach_args *faa)
1321.1Sskrll{
1331.1Sskrll	faa->faa_bst = &arm_generic_bs_tag;
1341.1Sskrll	faa->faa_dmat = &arm_generic_dma_tag;
1351.1Sskrll}
1361.1Sskrll
1371.2Sskrllvoid __noasan
1381.1Sskrllzynq_platform_early_putchar(char c)
1391.1Sskrll{
1401.1Sskrll#ifdef CONSADDR
1411.1Sskrll#define CONSADDR_VA     ((CONSADDR - ZYNQ_IOREG_PBASE) + ZYNQ_IOREG_VBASE)
1421.1Sskrll	volatile uint32_t *uartaddr = cpu_earlydevice_va_p() ?
1431.1Sskrll	    (volatile uint32_t *)CONSADDR_VA :
1441.1Sskrll	    (volatile uint32_t *)CONSADDR;
1451.1Sskrll
1461.1Sskrll	/* QEMU needs CR_TXEN to be set and CR_TXDIS to be unset */
1471.1Sskrll	uartaddr[UART_CONTROL / 4] = CR_TXEN;
1481.1Sskrll	while ((le32toh(uartaddr[UART_CHNL_INT_STS / 4]) & STS_TEMPTY) == 0)
1491.1Sskrll		;
1501.1Sskrll
1511.1Sskrll	uartaddr[UART_TX_RX_FIFO / 4] = htole32(c);
1521.1Sskrll#endif
1531.1Sskrll}
1541.1Sskrll
1551.1Sskrllstatic void
1561.1Sskrllzynq_platform_device_register(device_t dev, void *aux)
1571.1Sskrll{
1581.1Sskrll}
1591.1Sskrll
1601.1Sskrllstatic u_int
1611.1Sskrllzynq_platform_uart_freq(void)
1621.1Sskrll{
1631.1Sskrll	return ZYNQ_REF_FREQ;
1641.1Sskrll}
1651.1Sskrll
1661.7Sjmcneill#ifdef MULTIPROCESSOR
1671.7Sjmcneillstatic int
1681.7Sjmcneillzynq_platform_mpstart(void)
1691.7Sjmcneill{
1701.7Sjmcneill	bus_space_tag_t bst = &arm_generic_bs_tag;
1711.7Sjmcneill	bus_space_handle_t bsh;
1721.7Sjmcneill	uint32_t val;
1731.7Sjmcneill	int error;
1741.7Sjmcneill	u_int i;
1751.7Sjmcneill
1761.7Sjmcneill	/* Invalidate all SCU cache tags and enable SCU. */
1771.7Sjmcneill	bsh = ZYNQ_ARMCORE_VBASE + ZYNQ_ARMCORE_SCU_BASE;
1781.7Sjmcneill	bus_space_write_4(bst, bsh, SCU_INV_ALL_REG, 0xffff);
1791.7Sjmcneill	val = bus_space_read_4(bst, bsh, SCU_CTL);
1801.7Sjmcneill	bus_space_write_4(bst, bsh, SCU_CTL, val | SCU_CTL_SCU_ENA);
1811.7Sjmcneill	armv7_dcache_wbinv_all();
1821.7Sjmcneill
1831.7Sjmcneill	/* Write start address for CPU1. */
1841.7Sjmcneill	error = bus_space_map(bst, ZYNQ7000_CPU1_ENTRY,
1851.7Sjmcneill	    ZYNQ7000_CPU1_ENTRY_SZ, 0, &bsh);
1861.7Sjmcneill	if (error) {
1871.7Sjmcneill		panic("%s: Couldn't map OCM: %d", __func__, error);
1881.7Sjmcneill	}
1891.7Sjmcneill	bus_space_write_4(bst, bsh, 0, KERN_VTOPHYS((vaddr_t)cpu_mpstart));
1901.7Sjmcneill	bus_space_unmap(bst, bsh, ZYNQ7000_CPU1_ENTRY_SZ);
1911.7Sjmcneill
1921.7Sjmcneill	dsb(sy);
1931.7Sjmcneill	sev();
1941.7Sjmcneill
1951.7Sjmcneill	const u_int cpuindex = 1;
1961.7Sjmcneill	for (i = 0x10000000; i > 0; i--) {
1971.7Sjmcneill		if (cpu_hatched_p(cpuindex)) {
1981.7Sjmcneill			break;
1991.7Sjmcneill		}
2001.7Sjmcneill	}
2011.7Sjmcneill	if (i == 0) {
2021.7Sjmcneill		aprint_error("cpu%d: WARNING: AP failed to start\n",
2031.7Sjmcneill		    cpuindex);
2041.7Sjmcneill		return EIO;
2051.7Sjmcneill	}
2061.7Sjmcneill
2071.7Sjmcneill	return 0;
2081.7Sjmcneill}
2091.7Sjmcneill#endif
2101.7Sjmcneill
2111.1Sskrll#define ZYNQ_ARM_PL310_BASE	ZYNQ_ARMCORE_VBASE + ZYNQ_ARMCORE_L2C_BASE
2121.1Sskrll
2131.1Sskrllstatic void
2141.1Sskrllzynq_platform_bootstrap(void)
2151.1Sskrll{
2161.1Sskrll#if NARML2CC > 0
2171.1Sskrll	const bus_space_handle_t pl310_bh = ZYNQ_ARM_PL310_BASE;
2181.1Sskrll	arml2cc_init(&arm_generic_bs_tag, pl310_bh, 0);
2191.1Sskrll#endif
2201.1Sskrll
2211.1Sskrll	arm_fdt_cpu_bootstrap();
2221.1Sskrll
2231.1Sskrll	void *fdt_data = __UNCONST(fdtbus_get_data());
2241.1Sskrll	const int chosen_off = fdt_path_offset(fdt_data, "/chosen");
2251.1Sskrll	if (chosen_off < 0)
2261.1Sskrll		return;
2271.1Sskrll
2281.1Sskrll	if (match_bootconf_option(boot_args, "console", "fb")) {
2291.1Sskrll		const int framebuffer_off =
2301.1Sskrll		    fdt_path_offset(fdt_data, "/chosen/framebuffer");
2311.1Sskrll		if (framebuffer_off >= 0) {
2321.1Sskrll			const char *status = fdt_getprop(fdt_data,
2331.1Sskrll			    framebuffer_off, "status", NULL);
2341.1Sskrll			if (status == NULL || strncmp(status, "ok", 2) == 0) {
2351.1Sskrll				fdt_setprop_string(fdt_data, chosen_off,
2361.1Sskrll				    "stdout-path", "/chosen/framebuffer");
2371.1Sskrll			}
2381.1Sskrll		}
2391.1Sskrll	} else if (match_bootconf_option(boot_args, "console", "serial")) {
2401.1Sskrll		fdt_setprop_string(fdt_data, chosen_off,
2411.1Sskrll		    "stdout-path", "serial0:115200n8");
2421.1Sskrll	}
2431.1Sskrll}
2441.1Sskrll
2451.1Sskrllstatic void
2461.1Sskrllzynq_platform_reset(void)
2471.1Sskrll{
2481.10Sjmcneill	bus_space_tag_t bst = &arm_generic_bs_tag;
2491.10Sjmcneill	bus_space_handle_t bsh = ZYNQ_SLCR_VBASE;
2501.1Sskrll
2511.10Sjmcneill	bus_space_write_4(bst, bsh, SLCR_UNLOCK, UNLOCK_KEY);
2521.10Sjmcneill	bus_space_write_4(bst, bsh, PSS_RST_CTRL, SOFT_RST);
2531.1Sskrll}
2541.1Sskrll
2551.11Sskrllstatic const struct fdt_platform zynq_platform = {
2561.11Sskrll	.fp_devmap = zynq_platform_devmap,
2571.11Sskrll	.fp_bootstrap = zynq_platform_bootstrap,
2581.11Sskrll	.fp_init_attach_args = zynq_platform_init_attach_args,
2591.11Sskrll	.fp_device_register = zynq_platform_device_register,
2601.11Sskrll	.fp_reset = zynq_platform_reset,
2611.11Sskrll	.fp_delay = a9tmr_delay,
2621.11Sskrll	.fp_uart_freq = zynq_platform_uart_freq,
2631.7Sjmcneill#ifdef MULTIPROCESSOR
2641.11Sskrll	.fp_mpstart = zynq_platform_mpstart,
2651.1Sskrll#endif
2661.1Sskrll};
2671.1Sskrll
2681.1Sskrll
2691.11SskrllFDT_PLATFORM(zynq, "xlnx,zynq-7000", &zynq_platform);
270