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