xref: /freebsd/sys/dev/uart/uart_cpu_arm64.c (revision 5c8e8e82aeaf3aa788acdd6cfca30ef09094230d)
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 int
83 uart_cpu_getdev(int devtype, struct uart_devinfo *di)
84 {
85 	struct uart_class *class;
86 	bus_space_handle_t bsh;
87 	bus_space_tag_t bst;
88 	u_int rclk, shift, iowidth;
89 	int br, err;
90 
91 	/* Allow overriding the FDT using the environment. */
92 	class = &uart_ns8250_class;
93 	err = uart_getenv(devtype, di, class);
94 	if (err == 0)
95 		return (0);
96 
97 #ifdef DEV_ACPI
98 	/* Check if SPCR can tell us what console to use. */
99 	if (uart_cpu_acpi_spcr(devtype, di) == 0)
100 		return (0);
101 #endif
102 	err = ENXIO;
103 #ifdef FDT
104 	if (err != 0) {
105 		err = uart_cpu_fdt_probe(&class, &bst, &bsh, &br, &rclk,
106 		    &shift, &iowidth, devtype);
107 	}
108 #endif
109 	if (err != 0)
110 		return (err);
111 
112 	/*
113 	 * Finalize configuration.
114 	 */
115 	di->bas.chan = 0;
116 	di->bas.regshft = shift;
117 	di->bas.regiowidth = iowidth;
118 	di->baudrate = br;
119 	di->bas.rclk = rclk;
120 	di->ops = uart_getops(class);
121 	di->databits = 8;
122 	di->stopbits = 1;
123 	di->parity = UART_PARITY_NONE;
124 	di->bas.bst = bst;
125 	di->bas.bsh = bsh;
126 	uart_bus_space_mem = di->bas.bst;
127 	uart_bus_space_io = NULL;
128 
129 	return (0);
130 }
131