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