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