xref: /freebsd/sys/dev/uart/uart_bus_fdt.c (revision 12be6f12e87abc3e1edd3cc6cdc20334bdd63380)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2009-2010 The FreeBSD Foundation
5  *
6  * This software was developed by Semihalf under sponsorship from
7  * the FreeBSD Foundation.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30 
31 #include <sys/cdefs.h>
32 #include "opt_platform.h"
33 
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/bus.h>
37 #include <sys/kernel.h>
38 #include <sys/module.h>
39 
40 #include <machine/bus.h>
41 
42 #include <dev/fdt/fdt_common.h>
43 #include <dev/ofw/ofw_bus.h>
44 #include <dev/ofw/ofw_bus_subr.h>
45 #include <dev/uart/uart.h>
46 #include <dev/uart/uart_bus.h>
47 #include <dev/uart/uart_cpu.h>
48 #include <dev/uart/uart_cpu_fdt.h>
49 
50 static int uart_fdt_probe(device_t);
51 
52 static device_method_t uart_fdt_methods[] = {
53 	/* Device interface */
54 	DEVMETHOD(device_probe,		uart_fdt_probe),
55 	DEVMETHOD(device_attach,	uart_bus_attach),
56 	DEVMETHOD(device_detach,	uart_bus_detach),
57 	{ 0, 0 }
58 };
59 
60 static driver_t uart_fdt_driver = {
61 	uart_driver_name,
62 	uart_fdt_methods,
63 	sizeof(struct uart_softc),
64 };
65 
66 int
67 uart_fdt_get_clock(phandle_t node, pcell_t *cell)
68 {
69 
70 	/* clock-frequency is a FreeBSD-only extension. */
71 	if ((OF_getencprop(node, "clock-frequency", cell,
72 	    sizeof(*cell))) <= 0) {
73 		/* Try to retrieve parent 'bus-frequency' */
74 		/* XXX this should go to simple-bus fixup or so */
75 		if ((OF_getencprop(OF_parent(node), "bus-frequency", cell,
76 		    sizeof(*cell))) <= 0)
77 			*cell = 0;
78 	}
79 
80 	return (0);
81 }
82 
83 int
84 uart_fdt_get_shift(phandle_t node, pcell_t *cell)
85 {
86 
87 	if ((OF_getencprop(node, "reg-shift", cell, sizeof(*cell))) <= 0)
88 		return (-1);
89 	return (0);
90 }
91 
92 int
93 uart_fdt_get_io_width(phandle_t node, pcell_t *cell)
94 {
95 
96 	if ((OF_getencprop(node, "reg-io-width", cell, sizeof(*cell))) <= 0)
97 		return (-1);
98 	return (0);
99 }
100 
101 static uintptr_t
102 uart_fdt_find_device(device_t dev)
103 {
104 	struct ofw_compat_data **cd;
105 	const struct ofw_compat_data *ocd;
106 
107 	SET_FOREACH(cd, uart_fdt_class_and_device_set) {
108 		ocd = ofw_bus_search_compatible(dev, *cd);
109 		if (ocd->ocd_data != 0)
110 			return (ocd->ocd_data);
111 	}
112 	return (0);
113 }
114 
115 static int
116 phandle_chosen_propdev(phandle_t chosen, const char *name, phandle_t *node)
117 {
118 	char buf[64];
119 	char *sep;
120 
121 	if (OF_getprop(chosen, name, buf, sizeof(buf)) <= 0)
122 		return (ENXIO);
123 	/*
124 	 * stdout-path may have a ':' to separate the device from the
125 	 * connection settings. Split the string so we just pass the former
126 	 * to OF_finddevice.
127 	 */
128 	sep = strchr(buf, ':');
129 	if (sep != NULL)
130 		*sep = '\0';
131 	if ((*node = OF_finddevice(buf)) == -1)
132 		return (ENXIO);
133 
134 	return (0);
135 }
136 
137 static const struct ofw_compat_data *
138 uart_fdt_find_compatible(phandle_t node, const struct ofw_compat_data *cd)
139 {
140 	const struct ofw_compat_data *ocd;
141 
142 	for (ocd = cd; ocd->ocd_str != NULL; ocd++) {
143 		if (ofw_bus_node_is_compatible(node, ocd->ocd_str))
144 			return (ocd);
145 	}
146 	return (NULL);
147 }
148 
149 static uintptr_t
150 uart_fdt_find_by_node(phandle_t node, int class_list)
151 {
152 	struct ofw_compat_data **cd;
153 	const struct ofw_compat_data *ocd;
154 
155 	if (class_list) {
156 		SET_FOREACH(cd, uart_fdt_class_set) {
157 			ocd = uart_fdt_find_compatible(node, *cd);
158 			if ((ocd != NULL) && (ocd->ocd_data != 0))
159 				return (ocd->ocd_data);
160 		}
161 	} else {
162 		SET_FOREACH(cd, uart_fdt_class_and_device_set) {
163 			ocd = uart_fdt_find_compatible(node, *cd);
164 			if ((ocd != NULL) && (ocd->ocd_data != 0))
165 				return (ocd->ocd_data);
166 		}
167 	}
168 
169 	return (0);
170 }
171 
172 int
173 uart_cpu_fdt_probe(struct uart_class **classp, bus_space_tag_t *bst,
174     bus_space_handle_t *bsh, int *baud, u_int *rclk, u_int *shiftp,
175     u_int *iowidthp, const int devtype)
176 {
177 	const char *propnames[] = {"stdout-path", "linux,stdout-path", "stdout",
178 	    "stdin-path", "stdin", NULL};
179 	const char *propnames_dbgport[] = {"freebsd,debug-path", NULL};
180 	const char **name;
181 	struct uart_class *class;
182 	phandle_t node, chosen;
183 	pcell_t br, clk, shift, iowidth;
184 	char *cp = NULL;
185 	int err;
186 
187 	/* Has the user forced a specific device node? */
188 	switch (devtype) {
189 	case UART_DEV_DBGPORT:
190 		cp = kern_getenv("hw.fdt.dbgport");
191 		name = propnames_dbgport;
192 		break;
193 	case UART_DEV_CONSOLE:
194 		cp = kern_getenv("hw.fdt.console");
195 		name = propnames;
196 		break;
197 	default:
198 		return (ENXIO);
199 	}
200 
201 	if (cp == NULL) {
202 		/*
203 		 * Retrieve a node from /chosen.
204 		 */
205 		node = -1;
206 		if ((chosen = OF_finddevice("/chosen")) != -1) {
207 			for (; *name != NULL; name++) {
208 				if (phandle_chosen_propdev(chosen, *name,
209 				    &node) == 0)
210 					break;
211 			}
212 		}
213 		if (chosen == -1 || *name == NULL)
214 			node = OF_finddevice("serial0"); /* Last ditch */
215 	} else {
216 		node = OF_finddevice(cp);
217 	}
218 
219 	if (node == -1)
220 		return (ENXIO);
221 
222 	/*
223 	 * Check old style of UART definition first. Unfortunately, the common
224 	 * FDT processing is not possible if we have clock, power domains and
225 	 * pinmux stuff.
226 	 */
227 	class = (struct uart_class *)uart_fdt_find_by_node(node, 0);
228 	if (class != NULL) {
229 		if ((err = uart_fdt_get_clock(node, &clk)) != 0)
230 			return (err);
231 	} else {
232 		/* Check class only linker set */
233 		class =
234 		    (struct uart_class *)uart_fdt_find_by_node(node, 1);
235 		if (class == NULL)
236 			return (ENXIO);
237 		clk = 0;
238 	}
239 
240 	/*
241 	 * Retrieve serial attributes.
242 	 */
243 	if (uart_fdt_get_shift(node, &shift) != 0)
244 		shift = uart_getregshift(class);
245 
246 	if (uart_fdt_get_io_width(node, &iowidth) != 0)
247 		iowidth = uart_getregiowidth(class);
248 
249 	if (OF_getencprop(node, "current-speed", &br, sizeof(br)) <= 0)
250 		br = 0;
251 
252 	err = OF_decode_addr(node, 0, bst, bsh, NULL);
253 	if (err != 0)
254 		return (err);
255 
256 	*classp = class;
257 	*baud = br;
258 	*rclk = clk;
259 	*shiftp = shift;
260 	*iowidthp = iowidth;
261 
262 	return (0);
263 }
264 
265 static int
266 uart_fdt_probe(device_t dev)
267 {
268 	struct uart_softc *sc;
269 	phandle_t node;
270 	pcell_t clock, shift, iowidth;
271 	int err;
272 
273 	sc = device_get_softc(dev);
274 
275 	if (!ofw_bus_status_okay(dev))
276 		return (ENXIO);
277 
278 	sc->sc_class = (struct uart_class *)uart_fdt_find_device(dev);
279 	if (sc->sc_class == NULL)
280 		return (ENXIO);
281 
282 	node = ofw_bus_get_node(dev);
283 
284 	if ((err = uart_fdt_get_clock(node, &clock)) != 0)
285 		return (err);
286 	if (uart_fdt_get_shift(node, &shift) != 0)
287 		shift = uart_getregshift(sc->sc_class);
288 	if (uart_fdt_get_io_width(node, &iowidth) != 0)
289 		iowidth = uart_getregiowidth(sc->sc_class);
290 
291 	return (uart_bus_probe(dev, (int)shift, (int)iowidth, (int)clock, 0, 0, 0));
292 }
293 
294 DRIVER_MODULE(uart, simplebus, uart_fdt_driver, 0, 0);
295 DRIVER_MODULE(uart, ofwbus, uart_fdt_driver, 0, 0);
296