acpi_platform.c revision 1.5 1 /* $NetBSD: acpi_platform.c,v 1.5 2018/10/28 10:21:42 jmcneill 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.5 2018/10/28 10:21:42 jmcneill 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 static struct plcom_instance plcom_console;
92
93 static const struct pmap_devmap *
94 acpi_platform_devmap(void)
95 {
96 static const struct pmap_devmap devmap[] = {
97 DEVMAP_ENTRY_END
98 };
99
100 return devmap;
101 }
102
103 static void
104 acpi_platform_bootstrap(void)
105 {
106 }
107
108 static void
109 acpi_platform_startup(void)
110 {
111 ACPI_TABLE_SPCR *spcr;
112 ACPI_TABLE_FADT *fadt;
113 ACPI_TABLE_MADT *madt;
114 int baud_rate;
115
116 /*
117 * Setup serial console device
118 */
119 if (ACPI_SUCCESS(acpi_table_find(ACPI_SIG_SPCR, (void **)&spcr))) {
120
121 switch (spcr->BaudRate) {
122 case SPCR_BAUD_9600:
123 baud_rate = 9600;
124 break;
125 case SPCR_BAUD_19200:
126 baud_rate = 19200;
127 break;
128 case SPCR_BAUD_57600:
129 baud_rate = 57600;
130 break;
131 case SPCR_BAUD_115200:
132 case SPCR_BAUD_UNKNOWN:
133 default:
134 baud_rate = 115200;
135 break;
136 }
137
138 if (spcr->SerialPort.SpaceId == ACPI_ADR_SPACE_SYSTEM_MEMORY &&
139 spcr->SerialPort.Address != 0) {
140 switch (spcr->InterfaceType) {
141 #if NPLCOM > 0
142 case SPCR_INTERFACE_TYPE_PL011:
143 case SPCR_INTERFACE_TYPE_SBSA_32BIT:
144 case SPCR_INTERFACE_TYPE_SBSA_GENERIC:
145 plcom_console.pi_type = PLCOM_TYPE_PL011;
146 plcom_console.pi_iot = &arm_generic_bs_tag;
147 plcom_console.pi_iobase = spcr->SerialPort.Address;
148 plcom_console.pi_size = PL011COM_UART_SIZE;
149 if (spcr->InterfaceType == SPCR_INTERFACE_TYPE_SBSA_32BIT) {
150 plcom_console.pi_flags = PLC_FLAG_32BIT_ACCESS;
151 } else {
152 plcom_console.pi_flags = ACPI_ACCESS_BIT_WIDTH(spcr->SerialPort.AccessWidth) == 8 ?
153 0 : PLC_FLAG_32BIT_ACCESS;
154 }
155
156 plcomcnattach(&plcom_console, baud_rate, 0, TTYDEF_CFLAG, -1);
157 break;
158 #endif
159 #if NCOM > 0
160 case SPCR_INTERFACE_TYPE_BCM2835:
161 comcnattach(&arm_generic_a4x_bs_tag, spcr->SerialPort.Address + 0x40, baud_rate, -1,
162 COM_TYPE_BCMAUXUART, TTYDEF_CFLAG);
163 cn_set_magic("+++++");
164 break;
165 #endif
166 default:
167 printf("SPCR: kernel does not support interface type %#x\n", spcr->InterfaceType);
168 break;
169 }
170 }
171 acpi_table_unmap((ACPI_TABLE_HEADER *)spcr);
172 }
173
174 /*
175 * Initialize PSCI 0.2+ if implemented
176 */
177 if (ACPI_SUCCESS(acpi_table_find(ACPI_SIG_FADT, (void **)&fadt))) {
178 if (fadt->ArmBootFlags & ACPI_FADT_PSCI_COMPLIANT) {
179 if (fadt->ArmBootFlags & ACPI_FADT_PSCI_USE_HVC) {
180 psci_init(psci_call_hvc);
181 } else {
182 psci_init(psci_call_smc);
183 }
184 }
185 acpi_table_unmap((ACPI_TABLE_HEADER *)fadt);
186 }
187
188 /*
189 * Count CPUs
190 */
191 if (ACPI_SUCCESS(acpi_table_find(ACPI_SIG_MADT, (void **)&madt))) {
192 char *end = (char *)madt + madt->Header.Length;
193 char *where = (char *)madt + sizeof(ACPI_TABLE_MADT);
194 while (where < end) {
195 ACPI_SUBTABLE_HEADER *subtable = (ACPI_SUBTABLE_HEADER *)where;
196 if (subtable->Type == ACPI_MADT_TYPE_GENERIC_INTERRUPT)
197 arm_cpu_max++;
198 where += subtable->Length;
199 }
200 acpi_table_unmap((ACPI_TABLE_HEADER *)madt);
201 }
202 }
203
204 static void
205 acpi_platform_init_attach_args(struct fdt_attach_args *faa)
206 {
207 extern struct arm32_bus_dma_tag arm_generic_dma_tag;
208 extern struct bus_space arm_generic_bs_tag;
209 extern struct bus_space arm_generic_a4x_bs_tag;
210
211 faa->faa_bst = &arm_generic_bs_tag;
212 faa->faa_a4x_bst = &arm_generic_a4x_bs_tag;
213 faa->faa_dmat = &arm_generic_dma_tag;
214 }
215
216 static void
217 acpi_platform_early_putchar(char c)
218 {
219 }
220
221 static void
222 acpi_platform_device_register(device_t self, void *aux)
223 {
224 }
225
226 static void
227 acpi_platform_reset(void)
228 {
229 #ifdef EFI_RUNTIME
230 if (arm_efirt_reset(EFI_RESET_COLD) == 0)
231 return;
232 #endif
233 if (psci_available())
234 psci_system_reset();
235 }
236
237 static u_int
238 acpi_platform_uart_freq(void)
239 {
240 return 0;
241 }
242
243 static const struct arm_platform acpi_platform = {
244 .ap_devmap = acpi_platform_devmap,
245 .ap_bootstrap = acpi_platform_bootstrap,
246 .ap_startup = acpi_platform_startup,
247 .ap_init_attach_args = acpi_platform_init_attach_args,
248 .ap_early_putchar = acpi_platform_early_putchar,
249 .ap_device_register = acpi_platform_device_register,
250 .ap_reset = acpi_platform_reset,
251 .ap_delay = gtmr_delay,
252 .ap_uart_freq = acpi_platform_uart_freq,
253 };
254
255 ARM_PLATFORM(virt, "netbsd,generic-acpi", &acpi_platform);
256