acpi_platform.c revision 1.8 1 1.8 jmcneill /* $NetBSD: acpi_platform.c,v 1.8 2018/11/27 18:29:17 jmcneill Exp $ */
2 1.1 jmcneill
3 1.1 jmcneill /*-
4 1.1 jmcneill * Copyright (c) 2018 The NetBSD Foundation, Inc.
5 1.1 jmcneill * All rights reserved.
6 1.1 jmcneill *
7 1.1 jmcneill * This code is derived from software contributed to The NetBSD Foundation
8 1.1 jmcneill * by Jared McNeill <jmcneill (at) invisible.ca>.
9 1.1 jmcneill *
10 1.1 jmcneill * Redistribution and use in source and binary forms, with or without
11 1.1 jmcneill * modification, are permitted provided that the following conditions
12 1.1 jmcneill * are met:
13 1.1 jmcneill * 1. Redistributions of source code must retain the above copyright
14 1.1 jmcneill * notice, this list of conditions and the following disclaimer.
15 1.1 jmcneill * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 jmcneill * notice, this list of conditions and the following disclaimer in the
17 1.1 jmcneill * documentation and/or other materials provided with the distribution.
18 1.1 jmcneill *
19 1.1 jmcneill * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 jmcneill * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 jmcneill * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 jmcneill * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 jmcneill * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 jmcneill * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 jmcneill * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 jmcneill * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 jmcneill * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 jmcneill * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 jmcneill * POSSIBILITY OF SUCH DAMAGE.
30 1.1 jmcneill */
31 1.1 jmcneill
32 1.4 jmcneill #include "com.h"
33 1.4 jmcneill #include "plcom.h"
34 1.5 jmcneill #include "opt_efi.h"
35 1.4 jmcneill
36 1.1 jmcneill #include <sys/cdefs.h>
37 1.8 jmcneill __KERNEL_RCSID(0, "$NetBSD: acpi_platform.c,v 1.8 2018/11/27 18:29:17 jmcneill Exp $");
38 1.1 jmcneill
39 1.1 jmcneill #include <sys/param.h>
40 1.1 jmcneill #include <sys/bus.h>
41 1.1 jmcneill #include <sys/cpu.h>
42 1.1 jmcneill #include <sys/device.h>
43 1.1 jmcneill #include <sys/termios.h>
44 1.1 jmcneill
45 1.1 jmcneill #include <dev/fdt/fdtvar.h>
46 1.1 jmcneill #include <arm/fdt/arm_fdtvar.h>
47 1.1 jmcneill
48 1.1 jmcneill #include <uvm/uvm_extern.h>
49 1.1 jmcneill
50 1.1 jmcneill #include <machine/bootconfig.h>
51 1.1 jmcneill #include <arm/cpufunc.h>
52 1.1 jmcneill #include <arm/locore.h>
53 1.1 jmcneill
54 1.1 jmcneill #include <arm/cortex/gtmr_var.h>
55 1.1 jmcneill
56 1.1 jmcneill #include <arm/arm/psci.h>
57 1.1 jmcneill #include <arm/fdt/psci_fdtvar.h>
58 1.1 jmcneill
59 1.1 jmcneill #include <evbarm/fdt/platform.h>
60 1.1 jmcneill
61 1.1 jmcneill #include <evbarm/dev/plcomreg.h>
62 1.1 jmcneill #include <evbarm/dev/plcomvar.h>
63 1.1 jmcneill #include <dev/ic/ns16550reg.h>
64 1.1 jmcneill #include <dev/ic/comreg.h>
65 1.4 jmcneill #include <dev/ic/comvar.h>
66 1.1 jmcneill
67 1.5 jmcneill #ifdef EFI_RUNTIME
68 1.5 jmcneill #include <arm/arm/efi_runtime.h>
69 1.5 jmcneill #endif
70 1.5 jmcneill
71 1.1 jmcneill #include <dev/acpi/acpireg.h>
72 1.1 jmcneill #include <dev/acpi/acpivar.h>
73 1.5 jmcneill #include <arm/acpi/acpi_table.h>
74 1.5 jmcneill
75 1.5 jmcneill #include <libfdt.h>
76 1.1 jmcneill
77 1.8 jmcneill #define SPCR_INTERFACE_TYPE_COM16550 0x0000
78 1.8 jmcneill #define SPCR_INTERFACE_TYPE_COM16450 0x0001
79 1.3 jmcneill #define SPCR_INTERFACE_TYPE_PL011 0x0003
80 1.3 jmcneill #define SPCR_INTERFACE_TYPE_SBSA_32BIT 0x000d
81 1.3 jmcneill #define SPCR_INTERFACE_TYPE_SBSA_GENERIC 0x000e
82 1.3 jmcneill #define SPCR_INTERFACE_TYPE_BCM2835 0x0010
83 1.3 jmcneill
84 1.3 jmcneill #define SPCR_BAUD_UNKNOWN 0
85 1.3 jmcneill #define SPCR_BAUD_9600 3
86 1.3 jmcneill #define SPCR_BAUD_19200 4
87 1.3 jmcneill #define SPCR_BAUD_57600 6
88 1.3 jmcneill #define SPCR_BAUD_115200 7
89 1.1 jmcneill
90 1.1 jmcneill extern struct bus_space arm_generic_bs_tag;
91 1.4 jmcneill extern struct bus_space arm_generic_a4x_bs_tag;
92 1.1 jmcneill
93 1.7 rjs #if NPLCOM > 0
94 1.1 jmcneill static struct plcom_instance plcom_console;
95 1.7 rjs #endif
96 1.1 jmcneill
97 1.1 jmcneill static const struct pmap_devmap *
98 1.1 jmcneill acpi_platform_devmap(void)
99 1.1 jmcneill {
100 1.1 jmcneill static const struct pmap_devmap devmap[] = {
101 1.1 jmcneill DEVMAP_ENTRY_END
102 1.1 jmcneill };
103 1.1 jmcneill
104 1.1 jmcneill return devmap;
105 1.1 jmcneill }
106 1.1 jmcneill
107 1.1 jmcneill static void
108 1.1 jmcneill acpi_platform_bootstrap(void)
109 1.1 jmcneill {
110 1.1 jmcneill }
111 1.1 jmcneill
112 1.1 jmcneill static void
113 1.1 jmcneill acpi_platform_startup(void)
114 1.1 jmcneill {
115 1.1 jmcneill ACPI_TABLE_SPCR *spcr;
116 1.1 jmcneill ACPI_TABLE_FADT *fadt;
117 1.1 jmcneill ACPI_TABLE_MADT *madt;
118 1.3 jmcneill int baud_rate;
119 1.1 jmcneill
120 1.1 jmcneill /*
121 1.1 jmcneill * Setup serial console device
122 1.1 jmcneill */
123 1.1 jmcneill if (ACPI_SUCCESS(acpi_table_find(ACPI_SIG_SPCR, (void **)&spcr))) {
124 1.4 jmcneill
125 1.4 jmcneill switch (spcr->BaudRate) {
126 1.4 jmcneill case SPCR_BAUD_9600:
127 1.4 jmcneill baud_rate = 9600;
128 1.4 jmcneill break;
129 1.4 jmcneill case SPCR_BAUD_19200:
130 1.4 jmcneill baud_rate = 19200;
131 1.4 jmcneill break;
132 1.4 jmcneill case SPCR_BAUD_57600:
133 1.4 jmcneill baud_rate = 57600;
134 1.4 jmcneill break;
135 1.4 jmcneill case SPCR_BAUD_115200:
136 1.4 jmcneill case SPCR_BAUD_UNKNOWN:
137 1.4 jmcneill default:
138 1.4 jmcneill baud_rate = 115200;
139 1.4 jmcneill break;
140 1.4 jmcneill }
141 1.4 jmcneill
142 1.3 jmcneill if (spcr->SerialPort.SpaceId == ACPI_ADR_SPACE_SYSTEM_MEMORY &&
143 1.1 jmcneill spcr->SerialPort.Address != 0) {
144 1.3 jmcneill switch (spcr->InterfaceType) {
145 1.4 jmcneill #if NPLCOM > 0
146 1.3 jmcneill case SPCR_INTERFACE_TYPE_PL011:
147 1.3 jmcneill case SPCR_INTERFACE_TYPE_SBSA_32BIT:
148 1.3 jmcneill case SPCR_INTERFACE_TYPE_SBSA_GENERIC:
149 1.3 jmcneill plcom_console.pi_type = PLCOM_TYPE_PL011;
150 1.3 jmcneill plcom_console.pi_iot = &arm_generic_bs_tag;
151 1.3 jmcneill plcom_console.pi_iobase = spcr->SerialPort.Address;
152 1.3 jmcneill plcom_console.pi_size = PL011COM_UART_SIZE;
153 1.3 jmcneill if (spcr->InterfaceType == SPCR_INTERFACE_TYPE_SBSA_32BIT) {
154 1.3 jmcneill plcom_console.pi_flags = PLC_FLAG_32BIT_ACCESS;
155 1.3 jmcneill } else {
156 1.3 jmcneill plcom_console.pi_flags = ACPI_ACCESS_BIT_WIDTH(spcr->SerialPort.AccessWidth) == 8 ?
157 1.3 jmcneill 0 : PLC_FLAG_32BIT_ACCESS;
158 1.3 jmcneill }
159 1.1 jmcneill
160 1.3 jmcneill plcomcnattach(&plcom_console, baud_rate, 0, TTYDEF_CFLAG, -1);
161 1.3 jmcneill break;
162 1.4 jmcneill #endif
163 1.4 jmcneill #if NCOM > 0
164 1.8 jmcneill case SPCR_INTERFACE_TYPE_COM16550:
165 1.8 jmcneill case SPCR_INTERFACE_TYPE_COM16450:
166 1.8 jmcneill if (ACPI_ACCESS_BIT_WIDTH(spcr->SerialPort.AccessWidth) == 8) {
167 1.8 jmcneill comcnattach(&arm_generic_bs_tag, spcr->SerialPort.Address, baud_rate, -1,
168 1.8 jmcneill COM_TYPE_NORMAL, TTYDEF_CFLAG);
169 1.8 jmcneill } else {
170 1.8 jmcneill comcnattach(&arm_generic_a4x_bs_tag, spcr->SerialPort.Address, baud_rate, -1,
171 1.8 jmcneill COM_TYPE_NORMAL, TTYDEF_CFLAG);
172 1.8 jmcneill }
173 1.8 jmcneill break;
174 1.4 jmcneill case SPCR_INTERFACE_TYPE_BCM2835:
175 1.4 jmcneill comcnattach(&arm_generic_a4x_bs_tag, spcr->SerialPort.Address + 0x40, baud_rate, -1,
176 1.4 jmcneill COM_TYPE_BCMAUXUART, TTYDEF_CFLAG);
177 1.4 jmcneill cn_set_magic("+++++");
178 1.4 jmcneill break;
179 1.4 jmcneill #endif
180 1.4 jmcneill default:
181 1.4 jmcneill printf("SPCR: kernel does not support interface type %#x\n", spcr->InterfaceType);
182 1.4 jmcneill break;
183 1.3 jmcneill }
184 1.1 jmcneill }
185 1.1 jmcneill acpi_table_unmap((ACPI_TABLE_HEADER *)spcr);
186 1.1 jmcneill }
187 1.1 jmcneill
188 1.1 jmcneill /*
189 1.1 jmcneill * Initialize PSCI 0.2+ if implemented
190 1.1 jmcneill */
191 1.1 jmcneill if (ACPI_SUCCESS(acpi_table_find(ACPI_SIG_FADT, (void **)&fadt))) {
192 1.1 jmcneill if (fadt->ArmBootFlags & ACPI_FADT_PSCI_COMPLIANT) {
193 1.1 jmcneill if (fadt->ArmBootFlags & ACPI_FADT_PSCI_USE_HVC) {
194 1.1 jmcneill psci_init(psci_call_hvc);
195 1.1 jmcneill } else {
196 1.1 jmcneill psci_init(psci_call_smc);
197 1.1 jmcneill }
198 1.1 jmcneill }
199 1.1 jmcneill acpi_table_unmap((ACPI_TABLE_HEADER *)fadt);
200 1.1 jmcneill }
201 1.1 jmcneill
202 1.1 jmcneill /*
203 1.1 jmcneill * Count CPUs
204 1.1 jmcneill */
205 1.1 jmcneill if (ACPI_SUCCESS(acpi_table_find(ACPI_SIG_MADT, (void **)&madt))) {
206 1.1 jmcneill char *end = (char *)madt + madt->Header.Length;
207 1.1 jmcneill char *where = (char *)madt + sizeof(ACPI_TABLE_MADT);
208 1.1 jmcneill while (where < end) {
209 1.1 jmcneill ACPI_SUBTABLE_HEADER *subtable = (ACPI_SUBTABLE_HEADER *)where;
210 1.1 jmcneill if (subtable->Type == ACPI_MADT_TYPE_GENERIC_INTERRUPT)
211 1.1 jmcneill arm_cpu_max++;
212 1.1 jmcneill where += subtable->Length;
213 1.1 jmcneill }
214 1.1 jmcneill acpi_table_unmap((ACPI_TABLE_HEADER *)madt);
215 1.1 jmcneill }
216 1.1 jmcneill }
217 1.1 jmcneill
218 1.1 jmcneill static void
219 1.1 jmcneill acpi_platform_init_attach_args(struct fdt_attach_args *faa)
220 1.1 jmcneill {
221 1.1 jmcneill extern struct arm32_bus_dma_tag arm_generic_dma_tag;
222 1.1 jmcneill extern struct bus_space arm_generic_bs_tag;
223 1.1 jmcneill extern struct bus_space arm_generic_a4x_bs_tag;
224 1.1 jmcneill
225 1.1 jmcneill faa->faa_bst = &arm_generic_bs_tag;
226 1.1 jmcneill faa->faa_a4x_bst = &arm_generic_a4x_bs_tag;
227 1.1 jmcneill faa->faa_dmat = &arm_generic_dma_tag;
228 1.1 jmcneill }
229 1.1 jmcneill
230 1.1 jmcneill static void
231 1.1 jmcneill acpi_platform_device_register(device_t self, void *aux)
232 1.1 jmcneill {
233 1.1 jmcneill }
234 1.1 jmcneill
235 1.2 jmcneill static void
236 1.2 jmcneill acpi_platform_reset(void)
237 1.2 jmcneill {
238 1.5 jmcneill #ifdef EFI_RUNTIME
239 1.5 jmcneill if (arm_efirt_reset(EFI_RESET_COLD) == 0)
240 1.5 jmcneill return;
241 1.5 jmcneill #endif
242 1.2 jmcneill if (psci_available())
243 1.2 jmcneill psci_system_reset();
244 1.2 jmcneill }
245 1.2 jmcneill
246 1.1 jmcneill static u_int
247 1.1 jmcneill acpi_platform_uart_freq(void)
248 1.1 jmcneill {
249 1.1 jmcneill return 0;
250 1.1 jmcneill }
251 1.1 jmcneill
252 1.1 jmcneill static const struct arm_platform acpi_platform = {
253 1.1 jmcneill .ap_devmap = acpi_platform_devmap,
254 1.1 jmcneill .ap_bootstrap = acpi_platform_bootstrap,
255 1.1 jmcneill .ap_startup = acpi_platform_startup,
256 1.1 jmcneill .ap_init_attach_args = acpi_platform_init_attach_args,
257 1.1 jmcneill .ap_device_register = acpi_platform_device_register,
258 1.2 jmcneill .ap_reset = acpi_platform_reset,
259 1.1 jmcneill .ap_delay = gtmr_delay,
260 1.1 jmcneill .ap_uart_freq = acpi_platform_uart_freq,
261 1.1 jmcneill };
262 1.1 jmcneill
263 1.1 jmcneill ARM_PLATFORM(virt, "netbsd,generic-acpi", &acpi_platform);
264