1*8d564c5dSskrll /* $NetBSD: zynq_platform.c,v 1.11 2023/04/07 08:55:31 skrll Exp $ */
2c8c5b6beSskrll
3c8c5b6beSskrll /*-
4c8c5b6beSskrll * Copyright (c) 2019 The NetBSD Foundation, Inc.
5c8c5b6beSskrll * All rights reserved.
6c8c5b6beSskrll *
7c8c5b6beSskrll * This code is derived from software contributed to The NetBSD Foundation
8c8c5b6beSskrll * by Nick Hudson
9c8c5b6beSskrll *
10c8c5b6beSskrll * Redistribution and use in source and binary forms, with or without
11c8c5b6beSskrll * modification, are permitted provided that the following conditions
12c8c5b6beSskrll * are met:
13c8c5b6beSskrll * 1. Redistributions of source code must retain the above copyright
14c8c5b6beSskrll * notice, this list of conditions and the following disclaimer.
15c8c5b6beSskrll * 2. Redistributions in binary form must reproduce the above copyright
16c8c5b6beSskrll * notice, this list of conditions and the following disclaimer in the
17c8c5b6beSskrll * documentation and/or other materials provided with the distribution.
18c8c5b6beSskrll *
19c8c5b6beSskrll * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20c8c5b6beSskrll * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21c8c5b6beSskrll * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22c8c5b6beSskrll * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23c8c5b6beSskrll * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24c8c5b6beSskrll * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25c8c5b6beSskrll * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26c8c5b6beSskrll * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27c8c5b6beSskrll * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28c8c5b6beSskrll * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29c8c5b6beSskrll * POSSIBILITY OF SUCH DAMAGE.
30c8c5b6beSskrll */
31c8c5b6beSskrll
32c8c5b6beSskrll #include "opt_console.h"
33c8c5b6beSskrll #include "opt_soc.h"
34c8c5b6beSskrll
35c8c5b6beSskrll #include "arml2cc.h"
36c8c5b6beSskrll
37c8c5b6beSskrll #include <sys/cdefs.h>
38*8d564c5dSskrll __KERNEL_RCSID(0, "$NetBSD: zynq_platform.c,v 1.11 2023/04/07 08:55:31 skrll Exp $");
39c8c5b6beSskrll
40c8c5b6beSskrll #include <sys/param.h>
41c8c5b6beSskrll #include <sys/bus.h>
42c8c5b6beSskrll #include <sys/cpu.h>
43c8c5b6beSskrll #include <sys/device.h>
44c8c5b6beSskrll
45c8c5b6beSskrll #include <dev/fdt/fdtvar.h>
46c8c5b6beSskrll #include <arm/fdt/arm_fdtvar.h>
47c8c5b6beSskrll
48c8c5b6beSskrll #include <uvm/uvm_extern.h>
49c8c5b6beSskrll
50c8c5b6beSskrll #include <machine/bootconfig.h>
51c8c5b6beSskrll
52c8c5b6beSskrll #include <arm/cortex/a9tmr_var.h>
537b821442Sjmcneill #include <arm/cortex/scu_reg.h>
54c8c5b6beSskrll #include <arm/xilinx/zynq_uartreg.h>
55c8c5b6beSskrll
56c8c5b6beSskrll #include <evbarm/fdt/platform.h>
57c8c5b6beSskrll
58c8c5b6beSskrll #include <libfdt.h>
59c8c5b6beSskrll
60c8c5b6beSskrll #include <arm/cortex/pl310_var.h>
61c8c5b6beSskrll
627b821442Sjmcneill #include <arm/arm32/machdep.h>
63c8c5b6beSskrll
647b821442Sjmcneill #define ZYNQ_REF_FREQ 24000000
65c8c5b6beSskrll #define ZYNQ7000_DDR_PBASE 0x00000000
66c8c5b6beSskrll #define ZYNQ7000_DDR_SIZE 0x40000000
67c8c5b6beSskrll
68c8c5b6beSskrll #define ZYNQ_IOREG_VBASE KERNEL_IO_VBASE
69c8c5b6beSskrll #define ZYNQ_IOREG_PBASE 0xe0000000
70c8c5b6beSskrll #define ZYNQ_IOREG_SIZE 0x00200000
71c8c5b6beSskrll
72be29fc62Sjmcneill #define ZYNQ_SLCR_VBASE (ZYNQ_IOREG_VBASE + ZYNQ_IOREG_SIZE)
73be29fc62Sjmcneill #define ZYNQ_SLCR_PBASE 0xf8000000
74be29fc62Sjmcneill #define ZYNQ_SLCR_SIZE 0x00100000
75be29fc62Sjmcneill
76be29fc62Sjmcneill #define ZYNQ_GPV_VBASE (ZYNQ_SLCR_VBASE + ZYNQ_SLCR_SIZE)
77c8c5b6beSskrll #define ZYNQ_GPV_PBASE 0xf8900000
78c8c5b6beSskrll #define ZYNQ_GPV_SIZE 0x00100000
79c8c5b6beSskrll
80c8c5b6beSskrll #define ZYNQ_ARMCORE_VBASE (ZYNQ_GPV_VBASE + ZYNQ_GPV_SIZE)
81c8c5b6beSskrll #define ZYNQ_ARMCORE_PBASE 0xf8f00000
82bca8bf30Sjmcneill #define ZYNQ_ARMCORE_SIZE 0x00100000
83c8c5b6beSskrll
84c56c2569Sjmcneill #define ZYNQ_OCM_VBASE (ZYNQ_ARMCORE_VBASE + ZYNQ_ARMCORE_SIZE)
85c56c2569Sjmcneill #define ZYNQ_OCM_PBASE 0xfff00000
86c56c2569Sjmcneill #define ZYNQ_OCM_SIZE 0x00100000
87c56c2569Sjmcneill
887b821442Sjmcneill #define ZYNQ_ARMCORE_SCU_BASE 0x00000000
897b821442Sjmcneill #define ZYNQ_ARMCORE_L2C_BASE 0x00002000
907b821442Sjmcneill
917b821442Sjmcneill #define ZYNQ7000_CPU1_ENTRY 0xfffffff0
927b821442Sjmcneill #define ZYNQ7000_CPU1_ENTRY_SZ 4
937b821442Sjmcneill
94be29fc62Sjmcneill /* SLCR registers */
95be29fc62Sjmcneill #define SLCR_UNLOCK 0x008
96be29fc62Sjmcneill #define UNLOCK_KEY 0xdf0d
97be29fc62Sjmcneill #define PSS_RST_CTRL 0x200
98be29fc62Sjmcneill #define SOFT_RST __BIT(0)
99be29fc62Sjmcneill
100c8c5b6beSskrll extern struct bus_space arm_generic_bs_tag;
101c8c5b6beSskrll extern struct arm32_bus_dma_tag arm_generic_dma_tag;
102c8c5b6beSskrll
103c8c5b6beSskrll void zynq_platform_early_putchar(char);
104c8c5b6beSskrll
105c8c5b6beSskrll static const struct pmap_devmap *
zynq_platform_devmap(void)106c8c5b6beSskrll zynq_platform_devmap(void)
107c8c5b6beSskrll {
108c8c5b6beSskrll static const struct pmap_devmap devmap[] = {
109c8c5b6beSskrll DEVMAP_ENTRY(ZYNQ_IOREG_VBASE,
110c8c5b6beSskrll ZYNQ_IOREG_PBASE,
111c8c5b6beSskrll ZYNQ_IOREG_SIZE),
112be29fc62Sjmcneill DEVMAP_ENTRY(ZYNQ_SLCR_VBASE,
113be29fc62Sjmcneill ZYNQ_SLCR_PBASE,
114be29fc62Sjmcneill ZYNQ_SLCR_SIZE),
115c8c5b6beSskrll DEVMAP_ENTRY(ZYNQ_GPV_VBASE,
116c8c5b6beSskrll ZYNQ_GPV_PBASE,
117c8c5b6beSskrll ZYNQ_GPV_SIZE),
118c8c5b6beSskrll DEVMAP_ENTRY(ZYNQ_ARMCORE_VBASE,
119c8c5b6beSskrll ZYNQ_ARMCORE_PBASE,
120c8c5b6beSskrll ZYNQ_ARMCORE_SIZE),
121c56c2569Sjmcneill DEVMAP_ENTRY(ZYNQ_OCM_VBASE,
122c56c2569Sjmcneill ZYNQ_OCM_PBASE,
123c56c2569Sjmcneill ZYNQ_OCM_SIZE),
124c8c5b6beSskrll DEVMAP_ENTRY_END
125c8c5b6beSskrll };
126c8c5b6beSskrll
127c8c5b6beSskrll return devmap;
128c8c5b6beSskrll }
129c8c5b6beSskrll
130c8c5b6beSskrll static void
zynq_platform_init_attach_args(struct fdt_attach_args * faa)131c8c5b6beSskrll zynq_platform_init_attach_args(struct fdt_attach_args *faa)
132c8c5b6beSskrll {
133c8c5b6beSskrll faa->faa_bst = &arm_generic_bs_tag;
134c8c5b6beSskrll faa->faa_dmat = &arm_generic_dma_tag;
135c8c5b6beSskrll }
136c8c5b6beSskrll
137d329adb0Sskrll void __noasan
zynq_platform_early_putchar(char c)138c8c5b6beSskrll zynq_platform_early_putchar(char c)
139c8c5b6beSskrll {
140c8c5b6beSskrll #ifdef CONSADDR
141c8c5b6beSskrll #define CONSADDR_VA ((CONSADDR - ZYNQ_IOREG_PBASE) + ZYNQ_IOREG_VBASE)
142c8c5b6beSskrll volatile uint32_t *uartaddr = cpu_earlydevice_va_p() ?
143c8c5b6beSskrll (volatile uint32_t *)CONSADDR_VA :
144c8c5b6beSskrll (volatile uint32_t *)CONSADDR;
145c8c5b6beSskrll
146c8c5b6beSskrll /* QEMU needs CR_TXEN to be set and CR_TXDIS to be unset */
147c8c5b6beSskrll uartaddr[UART_CONTROL / 4] = CR_TXEN;
148c8c5b6beSskrll while ((le32toh(uartaddr[UART_CHNL_INT_STS / 4]) & STS_TEMPTY) == 0)
149c8c5b6beSskrll ;
150c8c5b6beSskrll
151c8c5b6beSskrll uartaddr[UART_TX_RX_FIFO / 4] = htole32(c);
152c8c5b6beSskrll #endif
153c8c5b6beSskrll }
154c8c5b6beSskrll
155c8c5b6beSskrll static void
zynq_platform_device_register(device_t dev,void * aux)156c8c5b6beSskrll zynq_platform_device_register(device_t dev, void *aux)
157c8c5b6beSskrll {
158c8c5b6beSskrll }
159c8c5b6beSskrll
160c8c5b6beSskrll static u_int
zynq_platform_uart_freq(void)161c8c5b6beSskrll zynq_platform_uart_freq(void)
162c8c5b6beSskrll {
163c8c5b6beSskrll return ZYNQ_REF_FREQ;
164c8c5b6beSskrll }
165c8c5b6beSskrll
1667b821442Sjmcneill #ifdef MULTIPROCESSOR
1677b821442Sjmcneill static int
zynq_platform_mpstart(void)1687b821442Sjmcneill zynq_platform_mpstart(void)
1697b821442Sjmcneill {
1707b821442Sjmcneill bus_space_tag_t bst = &arm_generic_bs_tag;
1717b821442Sjmcneill bus_space_handle_t bsh;
1727b821442Sjmcneill uint32_t val;
1737b821442Sjmcneill int error;
1747b821442Sjmcneill u_int i;
1757b821442Sjmcneill
1767b821442Sjmcneill /* Invalidate all SCU cache tags and enable SCU. */
1777b821442Sjmcneill bsh = ZYNQ_ARMCORE_VBASE + ZYNQ_ARMCORE_SCU_BASE;
1787b821442Sjmcneill bus_space_write_4(bst, bsh, SCU_INV_ALL_REG, 0xffff);
1797b821442Sjmcneill val = bus_space_read_4(bst, bsh, SCU_CTL);
1807b821442Sjmcneill bus_space_write_4(bst, bsh, SCU_CTL, val | SCU_CTL_SCU_ENA);
1817b821442Sjmcneill armv7_dcache_wbinv_all();
1827b821442Sjmcneill
1837b821442Sjmcneill /* Write start address for CPU1. */
1847b821442Sjmcneill error = bus_space_map(bst, ZYNQ7000_CPU1_ENTRY,
1857b821442Sjmcneill ZYNQ7000_CPU1_ENTRY_SZ, 0, &bsh);
1867b821442Sjmcneill if (error) {
1877b821442Sjmcneill panic("%s: Couldn't map OCM: %d", __func__, error);
1887b821442Sjmcneill }
1897b821442Sjmcneill bus_space_write_4(bst, bsh, 0, KERN_VTOPHYS((vaddr_t)cpu_mpstart));
1907b821442Sjmcneill bus_space_unmap(bst, bsh, ZYNQ7000_CPU1_ENTRY_SZ);
1917b821442Sjmcneill
1927b821442Sjmcneill dsb(sy);
1937b821442Sjmcneill sev();
1947b821442Sjmcneill
1957b821442Sjmcneill const u_int cpuindex = 1;
1967b821442Sjmcneill for (i = 0x10000000; i > 0; i--) {
1977b821442Sjmcneill if (cpu_hatched_p(cpuindex)) {
1987b821442Sjmcneill break;
1997b821442Sjmcneill }
2007b821442Sjmcneill }
2017b821442Sjmcneill if (i == 0) {
2027b821442Sjmcneill aprint_error("cpu%d: WARNING: AP failed to start\n",
2037b821442Sjmcneill cpuindex);
2047b821442Sjmcneill return EIO;
2057b821442Sjmcneill }
2067b821442Sjmcneill
2077b821442Sjmcneill return 0;
2087b821442Sjmcneill }
2097b821442Sjmcneill #endif
2107b821442Sjmcneill
211c8c5b6beSskrll #define ZYNQ_ARM_PL310_BASE ZYNQ_ARMCORE_VBASE + ZYNQ_ARMCORE_L2C_BASE
212c8c5b6beSskrll
213c8c5b6beSskrll static void
zynq_platform_bootstrap(void)214c8c5b6beSskrll zynq_platform_bootstrap(void)
215c8c5b6beSskrll {
216c8c5b6beSskrll #if NARML2CC > 0
217c8c5b6beSskrll const bus_space_handle_t pl310_bh = ZYNQ_ARM_PL310_BASE;
218c8c5b6beSskrll arml2cc_init(&arm_generic_bs_tag, pl310_bh, 0);
219c8c5b6beSskrll #endif
220c8c5b6beSskrll
221c8c5b6beSskrll arm_fdt_cpu_bootstrap();
222c8c5b6beSskrll
223c8c5b6beSskrll void *fdt_data = __UNCONST(fdtbus_get_data());
224c8c5b6beSskrll const int chosen_off = fdt_path_offset(fdt_data, "/chosen");
225c8c5b6beSskrll if (chosen_off < 0)
226c8c5b6beSskrll return;
227c8c5b6beSskrll
228c8c5b6beSskrll if (match_bootconf_option(boot_args, "console", "fb")) {
229c8c5b6beSskrll const int framebuffer_off =
230c8c5b6beSskrll fdt_path_offset(fdt_data, "/chosen/framebuffer");
231c8c5b6beSskrll if (framebuffer_off >= 0) {
232c8c5b6beSskrll const char *status = fdt_getprop(fdt_data,
233c8c5b6beSskrll framebuffer_off, "status", NULL);
234c8c5b6beSskrll if (status == NULL || strncmp(status, "ok", 2) == 0) {
235c8c5b6beSskrll fdt_setprop_string(fdt_data, chosen_off,
236c8c5b6beSskrll "stdout-path", "/chosen/framebuffer");
237c8c5b6beSskrll }
238c8c5b6beSskrll }
239c8c5b6beSskrll } else if (match_bootconf_option(boot_args, "console", "serial")) {
240c8c5b6beSskrll fdt_setprop_string(fdt_data, chosen_off,
241c8c5b6beSskrll "stdout-path", "serial0:115200n8");
242c8c5b6beSskrll }
243c8c5b6beSskrll }
244c8c5b6beSskrll
245c8c5b6beSskrll static void
zynq_platform_reset(void)246c8c5b6beSskrll zynq_platform_reset(void)
247c8c5b6beSskrll {
248be29fc62Sjmcneill bus_space_tag_t bst = &arm_generic_bs_tag;
249be29fc62Sjmcneill bus_space_handle_t bsh = ZYNQ_SLCR_VBASE;
250c8c5b6beSskrll
251be29fc62Sjmcneill bus_space_write_4(bst, bsh, SLCR_UNLOCK, UNLOCK_KEY);
252be29fc62Sjmcneill bus_space_write_4(bst, bsh, PSS_RST_CTRL, SOFT_RST);
253c8c5b6beSskrll }
254c8c5b6beSskrll
255*8d564c5dSskrll static const struct fdt_platform zynq_platform = {
256*8d564c5dSskrll .fp_devmap = zynq_platform_devmap,
257*8d564c5dSskrll .fp_bootstrap = zynq_platform_bootstrap,
258*8d564c5dSskrll .fp_init_attach_args = zynq_platform_init_attach_args,
259*8d564c5dSskrll .fp_device_register = zynq_platform_device_register,
260*8d564c5dSskrll .fp_reset = zynq_platform_reset,
261*8d564c5dSskrll .fp_delay = a9tmr_delay,
262*8d564c5dSskrll .fp_uart_freq = zynq_platform_uart_freq,
2637b821442Sjmcneill #ifdef MULTIPROCESSOR
264*8d564c5dSskrll .fp_mpstart = zynq_platform_mpstart,
265c8c5b6beSskrll #endif
266c8c5b6beSskrll };
267c8c5b6beSskrll
268c8c5b6beSskrll
269*8d564c5dSskrll FDT_PLATFORM(zynq, "xlnx,zynq-7000", &zynq_platform);
270