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
50static int uart_fdt_probe(device_t);
51
52static 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
60static driver_t uart_fdt_driver = {
61	uart_driver_name,
62	uart_fdt_methods,
63	sizeof(struct uart_softc),
64};
65
66int
67uart_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
83int
84uart_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
92int
93uart_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
101static uintptr_t
102uart_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
115static int
116phandle_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
137static const struct ofw_compat_data *
138uart_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
149static uintptr_t
150uart_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
172int
173uart_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		if (uart_fdt_get_clock(node, &clk) != 0)
238			clk = 0;
239	}
240
241	/*
242	 * Retrieve serial attributes.
243	 */
244	if (uart_fdt_get_shift(node, &shift) != 0)
245		shift = uart_getregshift(class);
246
247	if (uart_fdt_get_io_width(node, &iowidth) != 0)
248		iowidth = uart_getregiowidth(class);
249
250	if (OF_getencprop(node, "current-speed", &br, sizeof(br)) <= 0)
251		br = 0;
252
253	err = OF_decode_addr(node, 0, bst, bsh, NULL);
254	if (err != 0)
255		return (err);
256
257	*classp = class;
258	*baud = br;
259	*rclk = clk;
260	*shiftp = shift;
261	*iowidthp = iowidth;
262
263	return (0);
264}
265
266static int
267uart_fdt_probe(device_t dev)
268{
269	struct uart_softc *sc;
270	phandle_t node;
271	pcell_t clock, shift, iowidth;
272	int err;
273
274	sc = device_get_softc(dev);
275
276	if (!ofw_bus_status_okay(dev))
277		return (ENXIO);
278
279	sc->sc_class = (struct uart_class *)uart_fdt_find_device(dev);
280	if (sc->sc_class == NULL)
281		return (ENXIO);
282
283	node = ofw_bus_get_node(dev);
284
285	if ((err = uart_fdt_get_clock(node, &clock)) != 0)
286		return (err);
287	if (uart_fdt_get_shift(node, &shift) != 0)
288		shift = uart_getregshift(sc->sc_class);
289	if (uart_fdt_get_io_width(node, &iowidth) != 0)
290		iowidth = uart_getregiowidth(sc->sc_class);
291
292	return (uart_bus_probe(dev, (int)shift, (int)iowidth, (int)clock, 0, 0, 0));
293}
294
295DRIVER_MODULE(uart, simplebus, uart_fdt_driver, 0, 0);
296DRIVER_MODULE(uart, ofwbus, uart_fdt_driver, 0, 0);
297