1 /*- 2 * Copyright (c) 2016 The FreeBSD Foundation 3 * 4 * This software was developed by Andrew Turner under sponsorship from 5 * the FreeBSD Foundation. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 #include "opt_acpi.h" 30 #include "opt_platform.h" 31 32 #include <sys/cdefs.h> 33 #include <sys/param.h> 34 #include <sys/bus.h> 35 #include <sys/kernel.h> 36 #include <sys/module.h> 37 #include <sys/systm.h> 38 39 #include <vm/vm.h> 40 #include <vm/pmap.h> 41 42 #include <machine/bus.h> 43 44 #include <dev/uart/uart.h> 45 #include <dev/uart/uart_bus.h> 46 #include <dev/uart/uart_cpu.h> 47 48 #ifdef DEV_ACPI 49 #include <contrib/dev/acpica/include/acpi.h> 50 #include <contrib/dev/acpica/include/accommon.h> 51 #include <contrib/dev/acpica/include/actables.h> 52 #include <dev/uart/uart_cpu_acpi.h> 53 #endif 54 55 #ifdef FDT 56 #include <dev/fdt/fdt_common.h> 57 #include <dev/ofw/ofw_bus.h> 58 #include <dev/ofw/ofw_bus_subr.h> 59 #include <dev/uart/uart_cpu_fdt.h> 60 #endif 61 62 /* 63 * UART console routines. 64 */ 65 extern struct bus_space memmap_bus; 66 bus_space_tag_t uart_bus_space_io; 67 bus_space_tag_t uart_bus_space_mem = &memmap_bus; 68 69 int 70 uart_cpu_eqres(struct uart_bas *b1, struct uart_bas *b2) 71 { 72 73 if (pmap_kextract(b1->bsh) == 0) 74 return (0); 75 if (pmap_kextract(b2->bsh) == 0) 76 return (0); 77 return ((pmap_kextract(b1->bsh) == pmap_kextract(b2->bsh)) ? 1 : 0); 78 } 79 80 #ifdef FDT 81 static int 82 uart_cpu_fdt_setup(struct uart_class *class, int devtype, struct uart_devinfo *di) 83 { 84 bus_space_handle_t bsh; 85 bus_space_tag_t bst; 86 u_int rclk, shift, iowidth; 87 int br, err; 88 89 err = uart_cpu_fdt_probe(&class, &bst, &bsh, &br, &rclk, 90 &shift, &iowidth, devtype); 91 if (err != 0) 92 return (err); 93 94 /* 95 * Finalize configuration. 96 */ 97 di->bas.chan = 0; 98 di->bas.regshft = shift; 99 di->bas.regiowidth = iowidth; 100 di->baudrate = br; 101 di->bas.rclk = rclk; 102 di->ops = uart_getops(class); 103 di->databits = 8; 104 di->stopbits = 1; 105 di->parity = UART_PARITY_NONE; 106 di->bas.bst = bst; 107 di->bas.bsh = bsh; 108 uart_bus_space_mem = di->bas.bst; 109 uart_bus_space_io = NULL; 110 111 return (0); 112 } 113 #endif 114 115 int 116 uart_cpu_getdev(int devtype, struct uart_devinfo *di) 117 { 118 struct uart_class *class; 119 int err; 120 121 /* Allow overriding ACPI/FDT using the environment. */ 122 class = &uart_ns8250_class; 123 err = uart_getenv(devtype, di, class); 124 if (err == 0) 125 return (0); 126 127 #ifdef DEV_ACPI 128 /* Check if SPCR can tell us what console to use. */ 129 if (uart_cpu_acpi_spcr(devtype, di) == 0) 130 return (0); 131 #endif 132 #ifdef FDT 133 if (uart_cpu_fdt_setup(class, devtype, di) == 0) 134 return (0); 135 #endif 136 137 return (ENXIO); 138 } 139