2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2009-2010 The FreeBSD Foundation
7 * This software was developed by Semihalf under sponsorship from
8 * the FreeBSD Foundation.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include "opt_platform.h"
37 #include <sys/param.h>
38 #include <sys/systm.h>
40 #include <sys/kernel.h>
41 #include <sys/module.h>
43 #include <machine/bus.h>
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>
53 static int uart_fdt_probe(device_t);
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),
63 static driver_t uart_fdt_driver = {
66 sizeof(struct uart_softc),
70 uart_fdt_get_clock(phandle_t node, pcell_t *cell)
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,
87 uart_fdt_get_shift(phandle_t node, pcell_t *cell)
90 if ((OF_getencprop(node, "reg-shift", cell, sizeof(*cell))) <= 0)
96 uart_fdt_get_io_width(phandle_t node, pcell_t *cell)
99 if ((OF_getencprop(node, "reg-io-width", cell, sizeof(*cell))) <= 0)
105 uart_fdt_find_device(device_t dev)
107 struct ofw_compat_data **cd;
108 const struct ofw_compat_data *ocd;
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);
119 phandle_chosen_propdev(phandle_t chosen, const char *name, phandle_t *node)
124 if (OF_getprop(chosen, name, buf, sizeof(buf)) <= 0)
127 * stdout-path may have a ':' to separate the device from the
128 * connection settings. Split the string so we just pass the former
131 sep = strchr(buf, ':');
134 if ((*node = OF_finddevice(buf)) == -1)
140 static const struct ofw_compat_data *
141 uart_fdt_find_compatible(phandle_t node, const struct ofw_compat_data *cd)
143 const struct ofw_compat_data *ocd;
145 for (ocd = cd; ocd->ocd_str != NULL; ocd++) {
146 if (ofw_bus_node_is_compatible(node, ocd->ocd_str))
153 uart_fdt_find_by_node(phandle_t node, int class_list)
155 struct ofw_compat_data **cd;
156 const struct ofw_compat_data *ocd;
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);
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);
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)
180 const char *propnames[] = {"stdout-path", "linux,stdout-path", "stdout",
181 "stdin-path", "stdin", NULL};
182 const char *propnames_dbgport[] = {"freebsd,debug-path", NULL};
184 struct uart_class *class;
185 phandle_t node, chosen;
186 pcell_t br, clk, shift, iowidth;
190 /* Has the user forced a specific device node? */
192 case UART_DEV_DBGPORT:
193 cp = kern_getenv("hw.fdt.dbgport");
194 name = propnames_dbgport;
196 case UART_DEV_CONSOLE:
197 cp = kern_getenv("hw.fdt.console");
206 * Retrieve a node from /chosen.
209 if ((chosen = OF_finddevice("/chosen")) != -1) {
210 for (; *name != NULL; name++) {
211 if (phandle_chosen_propdev(chosen, *name,
216 if (chosen == -1 || *name == NULL)
217 node = OF_finddevice("serial0"); /* Last ditch */
219 node = OF_finddevice(cp);
226 * Check old style of UART definition first. Unfortunately, the common
227 * FDT processing is not possible if we have clock, power domains and
230 class = (struct uart_class *)uart_fdt_find_by_node(node, 0);
232 if ((err = uart_fdt_get_clock(node, &clk)) != 0)
235 /* Check class only linker set */
237 (struct uart_class *)uart_fdt_find_by_node(node, 1);
244 * Retrieve serial attributes.
246 if (uart_fdt_get_shift(node, &shift) != 0)
247 shift = uart_getregshift(class);
249 if (uart_fdt_get_io_width(node, &iowidth) != 0)
250 iowidth = uart_getregiowidth(class);
252 if (OF_getencprop(node, "current-speed", &br, sizeof(br)) <= 0)
255 err = OF_decode_addr(node, 0, bst, bsh, NULL);
269 uart_fdt_probe(device_t dev)
271 struct uart_softc *sc;
273 pcell_t clock, shift, iowidth;
276 sc = device_get_softc(dev);
278 if (!ofw_bus_status_okay(dev))
281 sc->sc_class = (struct uart_class *)uart_fdt_find_device(dev);
282 if (sc->sc_class == NULL)
285 node = ofw_bus_get_node(dev);
287 if ((err = uart_fdt_get_clock(node, &clock)) != 0)
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);
294 return (uart_bus_probe(dev, (int)shift, (int)iowidth, (int)clock, 0, 0, 0));
297 DRIVER_MODULE(uart, simplebus, uart_fdt_driver, uart_devclass, 0, 0);
298 DRIVER_MODULE(uart, ofwbus, uart_fdt_driver, uart_devclass, 0, 0);