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