acpi_platform.c revision 1.4.2.3 1 /* $NetBSD: acpi_platform.c,v 1.4.2.3 2018/11/26 01:52:17 pgoyette 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 (at) 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
36 #include <sys/cdefs.h>
37 __KERNEL_RCSID(0, "$NetBSD: acpi_platform.c,v 1.4.2.3 2018/11/26 01:52:17 pgoyette Exp $");
38
39 #include <sys/param.h>
40 #include <sys/bus.h>
41 #include <sys/cpu.h>
42 #include <sys/device.h>
43 #include <sys/termios.h>
44
45 #include <dev/fdt/fdtvar.h>
46 #include <arm/fdt/arm_fdtvar.h>
47
48 #include <uvm/uvm_extern.h>
49
50 #include <machine/bootconfig.h>
51 #include <arm/cpufunc.h>
52 #include <arm/locore.h>
53
54 #include <arm/cortex/gtmr_var.h>
55
56 #include <arm/arm/psci.h>
57 #include <arm/fdt/psci_fdtvar.h>
58
59 #include <evbarm/fdt/platform.h>
60
61 #include <evbarm/dev/plcomreg.h>
62 #include <evbarm/dev/plcomvar.h>
63 #include <dev/ic/ns16550reg.h>
64 #include <dev/ic/comreg.h>
65 #include <dev/ic/comvar.h>
66
67 #ifdef EFI_RUNTIME
68 #include <arm/arm/efi_runtime.h>
69 #endif
70
71 #include <dev/acpi/acpireg.h>
72 #include <dev/acpi/acpivar.h>
73 #include <arm/acpi/acpi_table.h>
74
75 #include <libfdt.h>
76
77 #define SPCR_INTERFACE_TYPE_PL011 0x0003
78 #define SPCR_INTERFACE_TYPE_SBSA_32BIT 0x000d
79 #define SPCR_INTERFACE_TYPE_SBSA_GENERIC 0x000e
80 #define SPCR_INTERFACE_TYPE_BCM2835 0x0010
81
82 #define SPCR_BAUD_UNKNOWN 0
83 #define SPCR_BAUD_9600 3
84 #define SPCR_BAUD_19200 4
85 #define SPCR_BAUD_57600 6
86 #define SPCR_BAUD_115200 7
87
88 extern struct bus_space arm_generic_bs_tag;
89 extern struct bus_space arm_generic_a4x_bs_tag;
90
91 #if NPLCOM > 0
92 static struct plcom_instance plcom_console;
93 #endif
94
95 static const struct pmap_devmap *
96 acpi_platform_devmap(void)
97 {
98 static const struct pmap_devmap devmap[] = {
99 DEVMAP_ENTRY_END
100 };
101
102 return devmap;
103 }
104
105 static void
106 acpi_platform_bootstrap(void)
107 {
108 }
109
110 static void
111 acpi_platform_startup(void)
112 {
113 ACPI_TABLE_SPCR *spcr;
114 ACPI_TABLE_FADT *fadt;
115 ACPI_TABLE_MADT *madt;
116 int baud_rate;
117
118 /*
119 * Setup serial console device
120 */
121 if (ACPI_SUCCESS(acpi_table_find(ACPI_SIG_SPCR, (void **)&spcr))) {
122
123 switch (spcr->BaudRate) {
124 case SPCR_BAUD_9600:
125 baud_rate = 9600;
126 break;
127 case SPCR_BAUD_19200:
128 baud_rate = 19200;
129 break;
130 case SPCR_BAUD_57600:
131 baud_rate = 57600;
132 break;
133 case SPCR_BAUD_115200:
134 case SPCR_BAUD_UNKNOWN:
135 default:
136 baud_rate = 115200;
137 break;
138 }
139
140 if (spcr->SerialPort.SpaceId == ACPI_ADR_SPACE_SYSTEM_MEMORY &&
141 spcr->SerialPort.Address != 0) {
142 switch (spcr->InterfaceType) {
143 #if NPLCOM > 0
144 case SPCR_INTERFACE_TYPE_PL011:
145 case SPCR_INTERFACE_TYPE_SBSA_32BIT:
146 case SPCR_INTERFACE_TYPE_SBSA_GENERIC:
147 plcom_console.pi_type = PLCOM_TYPE_PL011;
148 plcom_console.pi_iot = &arm_generic_bs_tag;
149 plcom_console.pi_iobase = spcr->SerialPort.Address;
150 plcom_console.pi_size = PL011COM_UART_SIZE;
151 if (spcr->InterfaceType == SPCR_INTERFACE_TYPE_SBSA_32BIT) {
152 plcom_console.pi_flags = PLC_FLAG_32BIT_ACCESS;
153 } else {
154 plcom_console.pi_flags = ACPI_ACCESS_BIT_WIDTH(spcr->SerialPort.AccessWidth) == 8 ?
155 0 : PLC_FLAG_32BIT_ACCESS;
156 }
157
158 plcomcnattach(&plcom_console, baud_rate, 0, TTYDEF_CFLAG, -1);
159 break;
160 #endif
161 #if NCOM > 0
162 case SPCR_INTERFACE_TYPE_BCM2835:
163 comcnattach(&arm_generic_a4x_bs_tag, spcr->SerialPort.Address + 0x40, baud_rate, -1,
164 COM_TYPE_BCMAUXUART, TTYDEF_CFLAG);
165 cn_set_magic("+++++");
166 break;
167 #endif
168 default:
169 printf("SPCR: kernel does not support interface type %#x\n", spcr->InterfaceType);
170 break;
171 }
172 }
173 acpi_table_unmap((ACPI_TABLE_HEADER *)spcr);
174 }
175
176 /*
177 * Initialize PSCI 0.2+ if implemented
178 */
179 if (ACPI_SUCCESS(acpi_table_find(ACPI_SIG_FADT, (void **)&fadt))) {
180 if (fadt->ArmBootFlags & ACPI_FADT_PSCI_COMPLIANT) {
181 if (fadt->ArmBootFlags & ACPI_FADT_PSCI_USE_HVC) {
182 psci_init(psci_call_hvc);
183 } else {
184 psci_init(psci_call_smc);
185 }
186 }
187 acpi_table_unmap((ACPI_TABLE_HEADER *)fadt);
188 }
189
190 /*
191 * Count CPUs
192 */
193 if (ACPI_SUCCESS(acpi_table_find(ACPI_SIG_MADT, (void **)&madt))) {
194 char *end = (char *)madt + madt->Header.Length;
195 char *where = (char *)madt + sizeof(ACPI_TABLE_MADT);
196 while (where < end) {
197 ACPI_SUBTABLE_HEADER *subtable = (ACPI_SUBTABLE_HEADER *)where;
198 if (subtable->Type == ACPI_MADT_TYPE_GENERIC_INTERRUPT)
199 arm_cpu_max++;
200 where += subtable->Length;
201 }
202 acpi_table_unmap((ACPI_TABLE_HEADER *)madt);
203 }
204 }
205
206 static void
207 acpi_platform_init_attach_args(struct fdt_attach_args *faa)
208 {
209 extern struct arm32_bus_dma_tag arm_generic_dma_tag;
210 extern struct bus_space arm_generic_bs_tag;
211 extern struct bus_space arm_generic_a4x_bs_tag;
212
213 faa->faa_bst = &arm_generic_bs_tag;
214 faa->faa_a4x_bst = &arm_generic_a4x_bs_tag;
215 faa->faa_dmat = &arm_generic_dma_tag;
216 }
217
218 static void
219 acpi_platform_device_register(device_t self, void *aux)
220 {
221 }
222
223 static void
224 acpi_platform_reset(void)
225 {
226 #ifdef EFI_RUNTIME
227 if (arm_efirt_reset(EFI_RESET_COLD) == 0)
228 return;
229 #endif
230 if (psci_available())
231 psci_system_reset();
232 }
233
234 static u_int
235 acpi_platform_uart_freq(void)
236 {
237 return 0;
238 }
239
240 static const struct arm_platform acpi_platform = {
241 .ap_devmap = acpi_platform_devmap,
242 .ap_bootstrap = acpi_platform_bootstrap,
243 .ap_startup = acpi_platform_startup,
244 .ap_init_attach_args = acpi_platform_init_attach_args,
245 .ap_device_register = acpi_platform_device_register,
246 .ap_reset = acpi_platform_reset,
247 .ap_delay = gtmr_delay,
248 .ap_uart_freq = acpi_platform_uart_freq,
249 };
250
251 ARM_PLATFORM(virt, "netbsd,generic-acpi", &acpi_platform);
252