diff --git a/sys/dev/uart/uart_cpu_acpi.c b/sys/dev/uart/uart_cpu_acpi.c index 09f69e951e81..9c9ffc1e3194 100644 --- a/sys/dev/uart/uart_cpu_acpi.c +++ b/sys/dev/uart/uart_cpu_acpi.c @@ -1,215 +1,221 @@ /*- * SPDX-License-Identifier: BSD-2-Clause * * Copyright (c) 2016 The FreeBSD Foundation * Copyright (c) 2019 Colin Percival * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include #include #include #include #include #include #include #include #include #include #include #include static struct acpi_uart_compat_data * uart_cpu_acpi_scan(uint8_t interface_type) { struct acpi_uart_compat_data **cd, *curcd; int i; SET_FOREACH(cd, uart_acpi_class_and_device_set) { curcd = *cd; for (i = 0; curcd[i].cd_hid != NULL; i++) { if (curcd[i].cd_port_subtype == interface_type) return (&curcd[i]); } } SET_FOREACH(cd, uart_acpi_class_set) { curcd = *cd; for (i = 0; curcd[i].cd_hid != NULL; i++) { if (curcd[i].cd_port_subtype == interface_type) return (&curcd[i]); } } return (NULL); } static int uart_cpu_acpi_init_devinfo(struct uart_devinfo *di, struct uart_class *class, ACPI_GENERIC_ADDRESS *addr) { /* Fill in some fixed details. */ di->bas.chan = 0; di->bas.rclk = 0; di->databits = 8; di->stopbits = 1; di->parity = UART_PARITY_NONE; di->ops = uart_getops(class); /* Fill in details from SPCR table. */ switch (addr->SpaceId) { case 0: di->bas.bst = uart_bus_space_mem; break; case 1: di->bas.bst = uart_bus_space_io; break; default: printf("UART in unrecognized address space: %d!\n", (int)addr->SpaceId); return (ENXIO); } switch (addr->AccessWidth) { case 0: /* EFI_ACPI_6_0_UNDEFINED */ /* FALLTHROUGH */ case 1: /* EFI_ACPI_6_0_BYTE */ di->bas.regiowidth = 1; break; case 2: /* EFI_ACPI_6_0_WORD */ di->bas.regiowidth = 2; break; case 3: /* EFI_ACPI_6_0_DWORD */ di->bas.regiowidth = 4; break; case 4: /* EFI_ACPI_6_0_QWORD */ di->bas.regiowidth = 8; break; default: printf("UART unsupported access width: %d!\n", (int)addr->AccessWidth); return (ENXIO); } switch (addr->BitWidth) { case 0: /* FALLTHROUGH */ case 8: di->bas.regshft = 0; break; case 16: di->bas.regshft = 1; break; case 32: di->bas.regshft = 2; break; case 64: di->bas.regshft = 3; break; default: printf("UART unsupported bit width: %d!\n", (int)addr->BitWidth); return (ENXIO); } return (0); } -int +static int uart_cpu_acpi_spcr(int devtype, struct uart_devinfo *di) { vm_paddr_t spcr_physaddr; ACPI_TABLE_SPCR *spcr; struct acpi_uart_compat_data *cd; struct uart_class *class; int error = ENXIO; - /* SPCR only tells us about consoles. */ - if (devtype != UART_DEV_CONSOLE) - return (error); - /* Look for the SPCR table. */ spcr_physaddr = acpi_find_table(ACPI_SIG_SPCR); if (spcr_physaddr == 0) return (error); spcr = acpi_map_table(spcr_physaddr, ACPI_SIG_SPCR); if (spcr == NULL) { printf("Unable to map the SPCR table!\n"); return (error); } /* Search for information about this SPCR interface type. */ cd = uart_cpu_acpi_scan(spcr->InterfaceType); if (cd == NULL) goto out; class = cd->cd_class; error = uart_cpu_acpi_init_devinfo(di, class, &spcr->SerialPort); if (error != 0) goto out; switch (spcr->BaudRate) { case 0: /* Special value; means "keep current value unchanged". */ di->baudrate = 0; break; case 3: di->baudrate = 9600; break; case 4: di->baudrate = 19200; break; case 6: di->baudrate = 57600; break; case 7: di->baudrate = 115200; break; default: printf("SPCR has reserved BaudRate value: %d!\n", (int)spcr->BaudRate); goto out; } if (spcr->PciVendorId != PCIV_INVALID && spcr->PciDeviceId != PCIV_INVALID) { di->pci_info.vendor = spcr->PciVendorId; di->pci_info.device = spcr->PciDeviceId; } /* Apply device tweaks. */ if ((cd->cd_quirks & UART_F_IGNORE_SPCR_REGSHFT) == UART_F_IGNORE_SPCR_REGSHFT) { di->bas.regshft = cd->cd_regshft; } /* Create a bus space handle. */ error = bus_space_map(di->bas.bst, spcr->SerialPort.Address, uart_getrange(class), 0, &di->bas.bsh); out: acpi_unmap_table(spcr); return (error); } + +int +uart_cpu_acpi_setup(int devtype, struct uart_devinfo *di) +{ + switch(devtype) { + case UART_DEV_CONSOLE: + return (uart_cpu_acpi_spcr(devtype, di)); + } + return (ENXIO); +} diff --git a/sys/dev/uart/uart_cpu_acpi.h b/sys/dev/uart/uart_cpu_acpi.h index 816644eb08ba..94329e1f1349 100644 --- a/sys/dev/uart/uart_cpu_acpi.h +++ b/sys/dev/uart/uart_cpu_acpi.h @@ -1,70 +1,70 @@ /*- * Copyright (c) 2015 Michal Meloun * Copyright (c) 2016 The FreeBSD Foundation * All rights reserved. * * This software was developed by Andrew Turner under * sponsorship from the FreeBSD Foundation. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #ifndef _DEV_UART_CPU_ACPI_H_ #define _DEV_UART_CPU_ACPI_H_ #include struct uart_class; struct acpi_uart_compat_data { const char *cd_hid; struct uart_class *cd_class; uint16_t cd_port_subtype; int cd_regshft; int cd_regiowidth; int cd_rclk; int cd_quirks; const char *cd_desc; }; /* * If your UART driver implements only uart_class and uses uart_cpu_acpi.c * for device instantiation, then use UART_ACPI_CLASS_AND_DEVICE for its * declaration */ SET_DECLARE(uart_acpi_class_and_device_set, struct acpi_uart_compat_data); #define UART_ACPI_CLASS_AND_DEVICE(data) \ DATA_SET(uart_acpi_class_and_device_set, data) /* * If your UART driver implements uart_class and custom device layer, * then use UART_ACPI_CLASS for its declaration */ SET_DECLARE(uart_acpi_class_set, struct acpi_uart_compat_data); #define UART_ACPI_CLASS(data) \ DATA_SET(uart_acpi_class_set, data) -/* Try to initialize UART device from SPCR data. */ -int uart_cpu_acpi_spcr(int devtype, struct uart_devinfo *di); +/* Try to initialize UART device from ACPI tables */ +int uart_cpu_acpi_setup(int devtype, struct uart_devinfo *di); #endif /* _DEV_UART_CPU_ACPI_H_ */ diff --git a/sys/dev/uart/uart_cpu_arm64.c b/sys/dev/uart/uart_cpu_arm64.c index 51b195783636..80e67e65697c 100644 --- a/sys/dev/uart/uart_cpu_arm64.c +++ b/sys/dev/uart/uart_cpu_arm64.c @@ -1,137 +1,137 @@ /*- * Copyright (c) 2016 The FreeBSD Foundation * * This software was developed by Andrew Turner under sponsorship from * the FreeBSD Foundation. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #include "opt_acpi.h" #include "opt_platform.h" #include #include #include #include #include #include #include #include #include #include #include #ifdef DEV_ACPI #include #include #include #include #endif #ifdef FDT #include #include #include #include #endif /* * UART console routines. */ extern struct bus_space memmap_bus; bus_space_tag_t uart_bus_space_io; bus_space_tag_t uart_bus_space_mem = &memmap_bus; int uart_cpu_eqres(struct uart_bas *b1, struct uart_bas *b2) { if (pmap_kextract(b1->bsh) == 0) return (0); if (pmap_kextract(b2->bsh) == 0) return (0); return ((pmap_kextract(b1->bsh) == pmap_kextract(b2->bsh)) ? 1 : 0); } #ifdef FDT static int uart_cpu_fdt_setup(struct uart_class *class, int devtype, struct uart_devinfo *di) { bus_space_handle_t bsh; bus_space_tag_t bst; u_int rclk, shift, iowidth; int br, err; err = uart_cpu_fdt_probe(&class, &bst, &bsh, &br, &rclk, &shift, &iowidth, devtype); if (err != 0) return (err); /* * Finalize configuration. */ di->bas.chan = 0; di->bas.regshft = shift; di->bas.regiowidth = iowidth; di->baudrate = br; di->bas.rclk = rclk; di->ops = uart_getops(class); di->databits = 8; di->stopbits = 1; di->parity = UART_PARITY_NONE; di->bas.bst = bst; di->bas.bsh = bsh; uart_bus_space_mem = di->bas.bst; uart_bus_space_io = NULL; return (0); } #endif int uart_cpu_getdev(int devtype, struct uart_devinfo *di) { struct uart_class *class; int err; /* Allow overriding ACPI/FDT using the environment. */ class = &uart_ns8250_class; err = uart_getenv(devtype, di, class); if (err == 0) return (0); #ifdef DEV_ACPI /* Check if SPCR can tell us what console to use. */ - if (uart_cpu_acpi_spcr(devtype, di) == 0) + if (uart_cpu_acpi_setup(devtype, di) == 0) return (0); #endif #ifdef FDT if (uart_cpu_fdt_setup(class, devtype, di) == 0) return (0); #endif return (ENXIO); } diff --git a/sys/dev/uart/uart_cpu_x86.c b/sys/dev/uart/uart_cpu_x86.c index 63f68245f589..d152d5059786 100644 --- a/sys/dev/uart/uart_cpu_x86.c +++ b/sys/dev/uart/uart_cpu_x86.c @@ -1,121 +1,121 @@ /*- * SPDX-License-Identifier: BSD-2-Clause * * Copyright (c) 2003, 2004 Marcel Moolenaar * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include "opt_acpi.h" #include #include #include #include #include #include #include bus_space_tag_t uart_bus_space_io = X86_BUS_SPACE_IO; bus_space_tag_t uart_bus_space_mem = X86_BUS_SPACE_MEM; extern int late_console; int uart_cpu_eqres(struct uart_bas *b1, struct uart_bas *b2) { return ((b1->bsh == b2->bsh && b1->bst == b2->bst) ? 1 : 0); } int uart_cpu_getdev(int devtype, struct uart_devinfo *di) { struct uart_class *class; unsigned int i, ivar; class = &uart_ns8250_class; if (class == NULL) return (ENXIO); /* Check the environment. */ if (uart_getenv(devtype, di, class) == 0) return (0); #ifdef DEV_ACPI /* * Check if SPCR can tell us what console to use. If running with * !late_console, we haven't set up our own page tables yet, so we * can't map ACPI tables to look at them. */ - if (late_console && uart_cpu_acpi_spcr(devtype, di) == 0) + if (late_console && uart_cpu_acpi_setup(devtype, di) == 0) return (0); #endif /* * Scan the hints. We only try units 0 to 3 (inclusive). This * covers the ISA legacy where 4 UARTs had their resources * predefined. */ for (i = 0; i < 4; i++) { if (resource_int_value("uart", i, "flags", &ivar)) continue; if (devtype == UART_DEV_CONSOLE && !UART_FLAGS_CONSOLE(ivar)) continue; if (devtype == UART_DEV_DBGPORT && !UART_FLAGS_DBGPORT(ivar)) continue; /* * We have a possible device. Make sure it's enabled and * that we have an I/O port. */ if (resource_int_value("uart", i, "disabled", &ivar) == 0 && ivar != 0) continue; if (resource_int_value("uart", i, "port", &ivar) != 0 || ivar == 0) continue; /* * Got it. Fill in the instance and return it. We only have * ns8250 and successors on i386. */ di->ops = uart_getops(class); di->bas.chan = 0; di->bas.bst = uart_bus_space_io; if (bus_space_map(di->bas.bst, ivar, uart_getrange(class), 0, &di->bas.bsh) != 0) continue; di->bas.regshft = 0; di->bas.rclk = 0; if (resource_int_value("uart", i, "baud", &ivar) != 0) ivar = 0; di->baudrate = ivar; di->databits = 8; di->stopbits = 1; di->parity = UART_PARITY_NONE; return (0); } return (ENXIO); }