2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2009-2010 The FreeBSD Foundation
6 * This software was developed by Semihalf under sponsorship from
7 * the FreeBSD Foundation.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include "opt_platform.h"
36 #include <sys/param.h>
37 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/module.h>
42 #include <machine/bus.h>
44 #include <dev/fdt/fdt_common.h>
45 #include <dev/ofw/ofw_bus.h>
46 #include <dev/ofw/ofw_bus_subr.h>
47 #include <dev/uart/uart.h>
48 #include <dev/uart/uart_bus.h>
49 #include <dev/uart/uart_cpu.h>
50 #include <dev/uart/uart_cpu_fdt.h>
52 static int uart_fdt_probe(device_t);
54 static device_method_t uart_fdt_methods[] = {
55 /* Device interface */
56 DEVMETHOD(device_probe, uart_fdt_probe),
57 DEVMETHOD(device_attach, uart_bus_attach),
58 DEVMETHOD(device_detach, uart_bus_detach),
62 static driver_t uart_fdt_driver = {
65 sizeof(struct uart_softc),
69 uart_fdt_get_clock(phandle_t node, pcell_t *cell)
72 /* clock-frequency is a FreeBSD-only extension. */
73 if ((OF_getencprop(node, "clock-frequency", cell,
74 sizeof(*cell))) <= 0) {
75 /* Try to retrieve parent 'bus-frequency' */
76 /* XXX this should go to simple-bus fixup or so */
77 if ((OF_getencprop(OF_parent(node), "bus-frequency", cell,
86 uart_fdt_get_shift(phandle_t node, pcell_t *cell)
89 if ((OF_getencprop(node, "reg-shift", cell, sizeof(*cell))) <= 0)
95 uart_fdt_get_io_width(phandle_t node, pcell_t *cell)
98 if ((OF_getencprop(node, "reg-io-width", cell, sizeof(*cell))) <= 0)
104 uart_fdt_find_device(device_t dev)
106 struct ofw_compat_data **cd;
107 const struct ofw_compat_data *ocd;
109 SET_FOREACH(cd, uart_fdt_class_and_device_set) {
110 ocd = ofw_bus_search_compatible(dev, *cd);
111 if (ocd->ocd_data != 0)
112 return (ocd->ocd_data);
118 phandle_chosen_propdev(phandle_t chosen, const char *name, phandle_t *node)
123 if (OF_getprop(chosen, name, buf, sizeof(buf)) <= 0)
126 * stdout-path may have a ':' to separate the device from the
127 * connection settings. Split the string so we just pass the former
130 sep = strchr(buf, ':');
133 if ((*node = OF_finddevice(buf)) == -1)
139 static const struct ofw_compat_data *
140 uart_fdt_find_compatible(phandle_t node, const struct ofw_compat_data *cd)
142 const struct ofw_compat_data *ocd;
144 for (ocd = cd; ocd->ocd_str != NULL; ocd++) {
145 if (ofw_bus_node_is_compatible(node, ocd->ocd_str))
152 uart_fdt_find_by_node(phandle_t node, int class_list)
154 struct ofw_compat_data **cd;
155 const struct ofw_compat_data *ocd;
158 SET_FOREACH(cd, uart_fdt_class_set) {
159 ocd = uart_fdt_find_compatible(node, *cd);
160 if ((ocd != NULL) && (ocd->ocd_data != 0))
161 return (ocd->ocd_data);
164 SET_FOREACH(cd, uart_fdt_class_and_device_set) {
165 ocd = uart_fdt_find_compatible(node, *cd);
166 if ((ocd != NULL) && (ocd->ocd_data != 0))
167 return (ocd->ocd_data);
175 uart_cpu_fdt_probe(struct uart_class **classp, bus_space_tag_t *bst,
176 bus_space_handle_t *bsh, int *baud, u_int *rclk, u_int *shiftp,
177 u_int *iowidthp, const int devtype)
179 const char *propnames[] = {"stdout-path", "linux,stdout-path", "stdout",
180 "stdin-path", "stdin", NULL};
181 const char *propnames_dbgport[] = {"freebsd,debug-path", NULL};
183 struct uart_class *class;
184 phandle_t node, chosen;
185 pcell_t br, clk, shift, iowidth;
189 /* Has the user forced a specific device node? */
191 case UART_DEV_DBGPORT:
192 cp = kern_getenv("hw.fdt.dbgport");
193 name = propnames_dbgport;
195 case UART_DEV_CONSOLE:
196 cp = kern_getenv("hw.fdt.console");
205 * Retrieve a node from /chosen.
208 if ((chosen = OF_finddevice("/chosen")) != -1) {
209 for (; *name != NULL; name++) {
210 if (phandle_chosen_propdev(chosen, *name,
215 if (chosen == -1 || *name == NULL)
216 node = OF_finddevice("serial0"); /* Last ditch */
218 node = OF_finddevice(cp);
225 * Check old style of UART definition first. Unfortunately, the common
226 * FDT processing is not possible if we have clock, power domains and
229 class = (struct uart_class *)uart_fdt_find_by_node(node, 0);
231 if ((err = uart_fdt_get_clock(node, &clk)) != 0)
234 /* Check class only linker set */
236 (struct uart_class *)uart_fdt_find_by_node(node, 1);
243 * Retrieve serial attributes.
245 if (uart_fdt_get_shift(node, &shift) != 0)
246 shift = uart_getregshift(class);
248 if (uart_fdt_get_io_width(node, &iowidth) != 0)
249 iowidth = uart_getregiowidth(class);
251 if (OF_getencprop(node, "current-speed", &br, sizeof(br)) <= 0)
254 err = OF_decode_addr(node, 0, bst, bsh, NULL);
268 uart_fdt_probe(device_t dev)
270 struct uart_softc *sc;
272 pcell_t clock, shift, iowidth;
275 sc = device_get_softc(dev);
277 if (!ofw_bus_status_okay(dev))
280 sc->sc_class = (struct uart_class *)uart_fdt_find_device(dev);
281 if (sc->sc_class == NULL)
284 node = ofw_bus_get_node(dev);
286 if ((err = uart_fdt_get_clock(node, &clock)) != 0)
288 if (uart_fdt_get_shift(node, &shift) != 0)
289 shift = uart_getregshift(sc->sc_class);
290 if (uart_fdt_get_io_width(node, &iowidth) != 0)
291 iowidth = uart_getregiowidth(sc->sc_class);
293 return (uart_bus_probe(dev, (int)shift, (int)iowidth, (int)clock, 0, 0, 0));
296 DRIVER_MODULE(uart, simplebus, uart_fdt_driver, uart_devclass, 0, 0);
297 DRIVER_MODULE(uart, ofwbus, uart_fdt_driver, uart_devclass, 0, 0);