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