arm_platform.c revision 1.7 1 1.7 skrll /* $NetBSD: arm_platform.c,v 1.7 2023/04/07 08:55:30 skrll Exp $ */
2 1.1 jmcneill
3 1.1 jmcneill /*-
4 1.1 jmcneill * Copyright (c) 2020 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 /*
30 1.1 jmcneill * This is the default Arm FDT platform implementation. It assumes the
31 1.1 jmcneill * following:
32 1.1 jmcneill *
33 1.1 jmcneill * - Generic timer
34 1.1 jmcneill * - PSCI support
35 1.1 jmcneill * - Console UART is pre-configured by firmware
36 1.1 jmcneill */
37 1.1 jmcneill
38 1.6 skrll #include "opt_console.h"
39 1.6 skrll
40 1.1 jmcneill #include <sys/cdefs.h>
41 1.7 skrll __KERNEL_RCSID(0, "$NetBSD: arm_platform.c,v 1.7 2023/04/07 08:55:30 skrll Exp $");
42 1.1 jmcneill
43 1.1 jmcneill #include <sys/param.h>
44 1.1 jmcneill #include <sys/bus.h>
45 1.1 jmcneill #include <sys/cpu.h>
46 1.1 jmcneill #include <sys/device.h>
47 1.1 jmcneill #include <sys/termios.h>
48 1.1 jmcneill
49 1.1 jmcneill #include <dev/fdt/fdtvar.h>
50 1.1 jmcneill
51 1.1 jmcneill #include <uvm/uvm_extern.h>
52 1.1 jmcneill
53 1.1 jmcneill #include <machine/bootconfig.h>
54 1.2 skrll
55 1.1 jmcneill #include <arm/cpufunc.h>
56 1.1 jmcneill
57 1.1 jmcneill #include <arm/cortex/gtmr_var.h>
58 1.1 jmcneill
59 1.1 jmcneill #include <arm/arm/psci.h>
60 1.7 skrll
61 1.7 skrll #include <arm/fdt/arm_fdtvar.h>
62 1.1 jmcneill #include <arm/fdt/psci_fdtvar.h>
63 1.1 jmcneill
64 1.6 skrll #include <evbarm/dev/plcomreg.h>
65 1.6 skrll #include <evbarm/dev/plcomvar.h>
66 1.6 skrll
67 1.1 jmcneill #include <libfdt.h>
68 1.1 jmcneill
69 1.1 jmcneill #include <arch/evbarm/fdt/platform.h>
70 1.1 jmcneill
71 1.1 jmcneill extern struct arm32_bus_dma_tag arm_generic_dma_tag;
72 1.1 jmcneill extern struct bus_space arm_generic_bs_tag;
73 1.1 jmcneill
74 1.6 skrll void plcom_platform_early_putchar(char);
75 1.6 skrll
76 1.6 skrll #define ARM_PTOV(p) (((p) - DEVMAP_ALIGN(uart_base)) + KERNEL_IO_VBASE)
77 1.6 skrll
78 1.6 skrll void __noasan
79 1.6 skrll plcom_platform_early_putchar(char c)
80 1.6 skrll {
81 1.6 skrll #ifdef CONSADDR
82 1.6 skrll bus_addr_t uart_base = CONSADDR;
83 1.6 skrll
84 1.6 skrll volatile uint32_t *uartaddr = cpu_earlydevice_va_p() ?
85 1.6 skrll (volatile uint32_t *)ARM_PTOV(uart_base):
86 1.6 skrll (volatile uint32_t *)uart_base;
87 1.6 skrll
88 1.6 skrll while ((le32toh(uartaddr[PL01XCOM_FR / 4]) & PL01X_FR_TXFF) != 0)
89 1.6 skrll continue;
90 1.6 skrll
91 1.6 skrll uartaddr[PL01XCOM_DR / 4] = htole32(c);
92 1.6 skrll dsb(sy);
93 1.6 skrll
94 1.6 skrll while ((le32toh(uartaddr[PL01XCOM_FR / 4]) & PL01X_FR_TXFE) == 0)
95 1.6 skrll continue;
96 1.6 skrll #endif
97 1.6 skrll }
98 1.6 skrll
99 1.1 jmcneill static void
100 1.1 jmcneill arm_platform_init_attach_args(struct fdt_attach_args *faa)
101 1.1 jmcneill {
102 1.1 jmcneill faa->faa_bst = &arm_generic_bs_tag;
103 1.1 jmcneill faa->faa_dmat = &arm_generic_dma_tag;
104 1.1 jmcneill }
105 1.1 jmcneill
106 1.1 jmcneill static void
107 1.1 jmcneill arm_platform_device_register(device_t self, void *aux)
108 1.1 jmcneill {
109 1.1 jmcneill }
110 1.1 jmcneill
111 1.1 jmcneill static const struct pmap_devmap *
112 1.1 jmcneill arm_platform_devmap(void)
113 1.1 jmcneill {
114 1.1 jmcneill static const struct pmap_devmap devmap_empty[] = {
115 1.1 jmcneill DEVMAP_ENTRY_END
116 1.1 jmcneill };
117 1.1 jmcneill static struct pmap_devmap devmap_uart[] = {
118 1.2 skrll DEVMAP_ENTRY(KERNEL_IO_VBASE, 0, PAGE_SIZE),
119 1.1 jmcneill DEVMAP_ENTRY_END
120 1.1 jmcneill };
121 1.1 jmcneill
122 1.1 jmcneill const int phandle = fdtbus_get_stdout_phandle();
123 1.1 jmcneill if (phandle <= 0)
124 1.1 jmcneill return devmap_empty;
125 1.1 jmcneill
126 1.6 skrll bus_addr_t uart_base;
127 1.1 jmcneill if (fdtbus_get_reg(phandle, 0, &uart_base, NULL) != 0)
128 1.1 jmcneill return devmap_empty;
129 1.1 jmcneill
130 1.2 skrll devmap_uart[0].pd_pa = DEVMAP_ALIGN(uart_base);
131 1.1 jmcneill
132 1.1 jmcneill return devmap_uart;
133 1.1 jmcneill }
134 1.1 jmcneill
135 1.1 jmcneill static u_int
136 1.1 jmcneill arm_platform_uart_freq(void)
137 1.1 jmcneill {
138 1.1 jmcneill return 0;
139 1.1 jmcneill }
140 1.1 jmcneill
141 1.7 skrll static const struct fdt_platform arm_platform = {
142 1.7 skrll .fp_devmap = arm_platform_devmap,
143 1.7 skrll .fp_bootstrap = arm_fdt_cpu_bootstrap,
144 1.7 skrll .fp_init_attach_args = arm_platform_init_attach_args,
145 1.7 skrll .fp_device_register = arm_platform_device_register,
146 1.7 skrll .fp_reset = psci_fdt_reset,
147 1.7 skrll .fp_delay = gtmr_delay,
148 1.7 skrll .fp_uart_freq = arm_platform_uart_freq,
149 1.7 skrll .fp_mpstart = arm_fdt_cpu_mpstart,
150 1.1 jmcneill };
151 1.1 jmcneill
152 1.7 skrll FDT_PLATFORM(arm, FDT_PLATFORM_DEFAULT, &arm_platform);
153