xref: /freebsd/sys/dev/uart/uart_cpu_arm64.c (revision 19fae0f66023a97a9b464b3beeeabb2081f575b3)
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 __FBSDID("$FreeBSD$");
34 
35 #include <sys/param.h>
36 #include <sys/bus.h>
37 #include <sys/kernel.h>
38 #include <sys/module.h>
39 #include <sys/systm.h>
40 
41 #include <vm/vm.h>
42 #include <vm/pmap.h>
43 
44 #include <machine/bus.h>
45 
46 #include <dev/uart/uart.h>
47 #include <dev/uart/uart_bus.h>
48 #include <dev/uart/uart_cpu.h>
49 
50 #ifdef DEV_ACPI
51 #include <contrib/dev/acpica/include/acpi.h>
52 #include <contrib/dev/acpica/include/accommon.h>
53 #include <contrib/dev/acpica/include/actables.h>
54 #include <dev/uart/uart_cpu_acpi.h>
55 #endif
56 
57 #ifdef FDT
58 #include <dev/fdt/fdt_common.h>
59 #include <dev/ofw/ofw_bus.h>
60 #include <dev/ofw/ofw_bus_subr.h>
61 #include <dev/uart/uart_cpu_fdt.h>
62 #endif
63 
64 /*
65  * UART console routines.
66  */
67 extern struct bus_space memmap_bus;
68 bus_space_tag_t uart_bus_space_io;
69 bus_space_tag_t uart_bus_space_mem = &memmap_bus;
70 
71 int
72 uart_cpu_eqres(struct uart_bas *b1, struct uart_bas *b2)
73 {
74 
75 	if (pmap_kextract(b1->bsh) == 0)
76 		return (0);
77 	if (pmap_kextract(b2->bsh) == 0)
78 		return (0);
79 	return ((pmap_kextract(b1->bsh) == pmap_kextract(b2->bsh)) ? 1 : 0);
80 }
81 
82 #ifdef FDT
83 static int
84 uart_cpu_fdt_setup(struct uart_class *class, int devtype, struct uart_devinfo *di)
85 {
86 	bus_space_handle_t bsh;
87 	bus_space_tag_t bst;
88 	u_int rclk, shift, iowidth;
89 	int br, err;
90 
91 	err = uart_cpu_fdt_probe(&class, &bst, &bsh, &br, &rclk,
92 	    &shift, &iowidth, devtype);
93 	if (err != 0)
94 		return (err);
95 
96 	/*
97 	 * Finalize configuration.
98 	 */
99 	di->bas.chan = 0;
100 	di->bas.regshft = shift;
101 	di->bas.regiowidth = iowidth;
102 	di->baudrate = br;
103 	di->bas.rclk = rclk;
104 	di->ops = uart_getops(class);
105 	di->databits = 8;
106 	di->stopbits = 1;
107 	di->parity = UART_PARITY_NONE;
108 	di->bas.bst = bst;
109 	di->bas.bsh = bsh;
110 	uart_bus_space_mem = di->bas.bst;
111 	uart_bus_space_io = NULL;
112 
113 	return (0);
114 }
115 #endif
116 
117 int
118 uart_cpu_getdev(int devtype, struct uart_devinfo *di)
119 {
120 	struct uart_class *class;
121 	int err;
122 
123 	/* Allow overriding ACPI/FDT using the environment. */
124 	class = &uart_ns8250_class;
125 	err = uart_getenv(devtype, di, class);
126 	if (err == 0)
127 		return (0);
128 
129 #ifdef DEV_ACPI
130 	/* Check if SPCR can tell us what console to use. */
131 	if (uart_cpu_acpi_spcr(devtype, di) == 0)
132 		return (0);
133 #endif
134 #ifdef FDT
135 	if (uart_cpu_fdt_setup(class, devtype, di) == 0)
136 		return (0);
137 #endif
138 
139 	return (ENXIO);
140 }
141