xref: /freebsd/sys/dev/uart/uart_cpu_powerpc.c (revision f6a3b357e9be4c6423c85eff9a847163a0d307c8)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2006 Marcel Moolenaar
5  * All rights reserved.
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  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <vm/vm.h>
35 #include <vm/pmap.h>
36 
37 #include <machine/bus.h>
38 #include <machine/ofw_machdep.h>
39 
40 #include <dev/ofw/ofw_bus_subr.h>
41 #include <dev/ofw/openfirm.h>
42 #include <dev/uart/uart.h>
43 #include <dev/uart/uart_cpu.h>
44 
45 bus_space_tag_t uart_bus_space_io = &bs_le_tag;
46 bus_space_tag_t uart_bus_space_mem = &bs_le_tag;
47 
48 int
49 uart_cpu_eqres(struct uart_bas *b1, struct uart_bas *b2)
50 {
51 
52 	return ((pmap_kextract(b1->bsh) == pmap_kextract(b2->bsh)) ? 1 : 0);
53 }
54 
55 static int
56 ofw_get_uart_console(phandle_t opts, phandle_t *result, const char *inputdev,
57     const char *outputdev)
58 {
59 	char buf[64];
60 	phandle_t input;
61 
62 	if (OF_getprop(opts, inputdev, buf, sizeof(buf)) == -1)
63 		return (ENXIO);
64 	input = OF_finddevice(buf);
65 	if (input == -1)
66 		return (ENXIO);
67 
68 	if (outputdev != NULL) {
69 		if (OF_getprop(opts, outputdev, buf, sizeof(buf)) == -1)
70 			return (ENXIO);
71 		if (OF_finddevice(buf) != input)
72 			return (ENXIO);
73 	}
74 
75 	*result = input;
76 	return (0);
77 }
78 
79 static int
80 ofw_get_console_phandle_path(phandle_t node, phandle_t *result,
81     const char *prop)
82 {
83 	union {
84 		char buf[64];
85 		phandle_t ref;
86 	} field;
87 	phandle_t output;
88 	ssize_t size;
89 
90 	size = OF_getproplen(node, prop);
91 	if (size == -1)
92 		return (ENXIO);
93 	OF_getprop(node, prop, &field, sizeof(field));
94 
95 	/* This property might be either a ihandle or path. Hooray. */
96 
97 	output = -1;
98 	if (field.buf[size - 1] == 0)
99 		output = OF_finddevice(field.buf);
100 	if (output == -1 && size == 4)
101 		output = OF_instance_to_package(field.ref);
102 
103 	if (output != -1) {
104 		*result = output;
105 		return (0);
106 	}
107 
108 	return (ENXIO);
109 }
110 
111 int
112 uart_cpu_getdev(int devtype, struct uart_devinfo *di)
113 {
114 	char buf[64];
115 	struct uart_class *class;
116 	phandle_t input, opts, chosen;
117 	int error;
118 
119 	opts = OF_finddevice("/options");
120 	chosen = OF_finddevice("/chosen");
121 	switch (devtype) {
122 	case UART_DEV_CONSOLE:
123 		error = ENXIO;
124 		if (chosen != -1 && error != 0)
125 			error = ofw_get_uart_console(chosen, &input,
126 			    "stdout-path", NULL);
127 		if (chosen != -1 && error != 0)
128 			error = ofw_get_uart_console(chosen, &input,
129 			    "linux,stdout-path", NULL);
130 		if (chosen != -1 && error != 0)
131 			error = ofw_get_console_phandle_path(chosen, &input,
132 			    "stdout");
133 		if (chosen != -1 && error != 0)
134 			error = ofw_get_uart_console(chosen, &input,
135 			    "stdin-path", NULL);
136 		if (chosen != -1 && error != 0)
137 			error = ofw_get_console_phandle_path(chosen, &input,
138 			    "stdin");
139 		if (opts != -1 && error != 0)
140 			error = ofw_get_uart_console(opts, &input,
141 			    "input-device", "output-device");
142 		if (opts != -1 && error != 0)
143 			error = ofw_get_uart_console(opts, &input,
144 			    "input-device-1", "output-device-1");
145 		if (error != 0) {
146 			input = OF_finddevice("serial0"); /* Last ditch */
147 			if (input == -1)
148 				error = (ENXIO);
149 		}
150 
151 		if (error != 0)
152 			return (error);
153 		break;
154 	case UART_DEV_DBGPORT:
155 		if (!getenv_string("hw.uart.dbgport", buf, sizeof(buf)))
156 			return (ENXIO);
157 		input = OF_finddevice(buf);
158 		if (input == -1)
159 			return (ENXIO);
160 		break;
161 	default:
162 		return (EINVAL);
163 	}
164 
165 	if (OF_getprop(input, "device_type", buf, sizeof(buf)) == -1)
166 		return (ENXIO);
167 	if (strcmp(buf, "serial") != 0)
168 		return (ENXIO);
169 
170 	if (ofw_bus_node_is_compatible(input, "chrp,es")) {
171 		class = &uart_z8530_class;
172 		di->bas.regshft = 4;
173 		di->bas.chan = 1;
174 	} else if (ofw_bus_node_is_compatible(input,"ns16550") ||
175 	    ofw_bus_node_is_compatible(input,"ns8250")) {
176 		class = &uart_ns8250_class;
177 		di->bas.regshft = 0;
178 		di->bas.chan = 0;
179 	} else
180 		return (ENXIO);
181 
182 	if (class == NULL)
183 		return (ENXIO);
184 
185 	error = OF_decode_addr(input, 0, &di->bas.bst, &di->bas.bsh, NULL);
186 	if (error)
187 		return (error);
188 
189 	di->ops = uart_getops(class);
190 
191 	if (OF_getprop(input, "clock-frequency", &di->bas.rclk,
192 	    sizeof(di->bas.rclk)) == -1)
193 		di->bas.rclk = 230400;
194 	if (OF_getprop(input, "current-speed", &di->baudrate,
195 	    sizeof(di->baudrate)) == -1)
196 		di->baudrate = 0;
197 	OF_getprop(input, "reg-shift", &di->bas.regshft,
198 	    sizeof(di->bas.regshft));
199 
200 	di->databits = 8;
201 	di->stopbits = 1;
202 	di->parity = UART_PARITY_NONE;
203 	return (0);
204 }
205 
206