2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2016 The FreeBSD Foundation
5 * Copyright (c) 2019 Colin Percival
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
34 #include <sys/systm.h>
37 #include <machine/bus.h>
39 #include <dev/uart/uart.h>
40 #include <dev/uart/uart_bus.h>
41 #include <dev/uart/uart_cpu.h>
42 #include <dev/uart/uart_cpu_acpi.h>
44 #include <contrib/dev/acpica/include/acpi.h>
45 #include <contrib/dev/acpica/include/accommon.h>
46 #include <contrib/dev/acpica/include/actables.h>
48 static struct acpi_uart_compat_data *
49 uart_cpu_acpi_scan(uint8_t interface_type)
51 struct acpi_uart_compat_data **cd, *curcd;
54 SET_FOREACH(cd, uart_acpi_class_and_device_set) {
56 for (i = 0; curcd[i].cd_hid != NULL; i++) {
57 if (curcd[i].cd_port_subtype == interface_type)
62 SET_FOREACH(cd, uart_acpi_class_set) {
64 for (i = 0; curcd[i].cd_hid != NULL; i++) {
65 if (curcd[i].cd_port_subtype == interface_type)
74 uart_cpu_acpi_spcr(int devtype, struct uart_devinfo *di)
76 vm_paddr_t spcr_physaddr;
77 ACPI_TABLE_SPCR *spcr;
78 struct acpi_uart_compat_data *cd;
79 struct uart_class *class;
82 /* SPCR only tells us about consoles. */
83 if (devtype != UART_DEV_CONSOLE)
86 /* Look for the SPCR table. */
87 spcr_physaddr = acpi_find_table(ACPI_SIG_SPCR);
88 if (spcr_physaddr == 0)
90 spcr = acpi_map_table(spcr_physaddr, ACPI_SIG_SPCR);
92 printf("Unable to map the SPCR table!\n");
96 /* Search for information about this SPCR interface type. */
97 cd = uart_cpu_acpi_scan(spcr->InterfaceType);
100 class = cd->cd_class;
102 /* Fill in some fixed details. */
107 di->parity = UART_PARITY_NONE;
108 di->ops = uart_getops(class);
110 /* Fill in details from SPCR table. */
111 switch (spcr->SerialPort.SpaceId) {
113 di->bas.bst = uart_bus_space_mem;
116 di->bas.bst = uart_bus_space_io;
119 printf("UART in unrecognized address space: %d!\n",
120 (int)spcr->SerialPort.SpaceId);
123 if (spcr->SerialPort.AccessWidth == 0)
126 di->bas.regshft = spcr->SerialPort.AccessWidth - 1;
127 di->bas.regiowidth = spcr->SerialPort.BitWidth / 8;
128 switch (spcr->BaudRate) {
130 /* Special value; means "keep current value unchanged". */
137 di->baudrate = 19200;
140 di->baudrate = 57600;
143 di->baudrate = 115200;
146 printf("SPCR has reserved BaudRate value: %d!\n",
147 (int)spcr->BaudRate);
151 /* Apply device tweaks. */
152 if ((cd->cd_quirks & UART_F_IGNORE_SPCR_REGSHFT) ==
153 UART_F_IGNORE_SPCR_REGSHFT) {
154 di->bas.regshft = cd->cd_regshft;
157 /* Create a bus space handle. */
158 error = bus_space_map(di->bas.bst, spcr->SerialPort.Address,
159 uart_getrange(class), 0, &di->bas.bsh);
162 acpi_unmap_table(spcr);