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
uart_fdt_get_clock(phandle_t node,pcell_t * cell)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
uart_fdt_get_shift(phandle_t node,pcell_t * cell)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
uart_fdt_get_io_width(phandle_t node,pcell_t * cell)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
uart_fdt_find_device(device_t dev)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
phandle_chosen_propdev(phandle_t chosen,const char * name,phandle_t * node)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 *
uart_fdt_find_compatible(phandle_t node,const struct ofw_compat_data * cd)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
uart_fdt_find_by_node(phandle_t node,int class_list)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
uart_cpu_fdt_probe(struct uart_class ** classp,bus_space_tag_t * bst,bus_space_handle_t * bsh,int * baud,u_int * rclk,u_int * shiftp,u_int * iowidthp)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)
179 {
180 const char *propnames[] = {"stdout-path", "linux,stdout-path", "stdout",
181 "stdin-path", "stdin", NULL};
182 const char **name;
183 struct uart_class *class;
184 phandle_t node, chosen;
185 pcell_t br, clk, shift, iowidth;
186 char *cp;
187 int err;
188
189 /* Has the user forced a specific device node? */
190 cp = kern_getenv("hw.fdt.console");
191 if (cp == NULL) {
192 /*
193 * Retrieve /chosen/std{in,out}.
194 */
195 node = -1;
196 if ((chosen = OF_finddevice("/chosen")) != -1) {
197 for (name = propnames; *name != NULL; name++) {
198 if (phandle_chosen_propdev(chosen, *name,
199 &node) == 0)
200 break;
201 }
202 }
203 if (chosen == -1 || *name == NULL)
204 node = OF_finddevice("serial0"); /* Last ditch */
205 } else {
206 node = OF_finddevice(cp);
207 }
208
209 if (node == -1)
210 return (ENXIO);
211
212 /*
213 * Check old style of UART definition first. Unfortunately, the common
214 * FDT processing is not possible if we have clock, power domains and
215 * pinmux stuff.
216 */
217 class = (struct uart_class *)uart_fdt_find_by_node(node, 0);
218 if (class != NULL) {
219 if ((err = uart_fdt_get_clock(node, &clk)) != 0)
220 return (err);
221 } else {
222 /* Check class only linker set */
223 class =
224 (struct uart_class *)uart_fdt_find_by_node(node, 1);
225 if (class == NULL)
226 return (ENXIO);
227 clk = 0;
228 }
229
230 /*
231 * Retrieve serial attributes.
232 */
233 if (uart_fdt_get_shift(node, &shift) != 0)
234 shift = uart_getregshift(class);
235
236 if (uart_fdt_get_io_width(node, &iowidth) != 0)
237 iowidth = uart_getregiowidth(class);
238
239 if (OF_getencprop(node, "current-speed", &br, sizeof(br)) <= 0)
240 br = 0;
241
242 err = OF_decode_addr(node, 0, bst, bsh, NULL);
243 if (err != 0)
244 return (err);
245
246 *classp = class;
247 *baud = br;
248 *rclk = clk;
249 *shiftp = shift;
250 *iowidthp = iowidth;
251
252 return (0);
253 }
254
255 static int
uart_fdt_probe(device_t dev)256 uart_fdt_probe(device_t dev)
257 {
258 struct uart_softc *sc;
259 phandle_t node;
260 pcell_t clock, shift, iowidth;
261 int err;
262
263 sc = device_get_softc(dev);
264
265 if (!ofw_bus_status_okay(dev))
266 return (ENXIO);
267
268 sc->sc_class = (struct uart_class *)uart_fdt_find_device(dev);
269 if (sc->sc_class == NULL)
270 return (ENXIO);
271
272 node = ofw_bus_get_node(dev);
273
274 if ((err = uart_fdt_get_clock(node, &clock)) != 0)
275 return (err);
276 if (uart_fdt_get_shift(node, &shift) != 0)
277 shift = uart_getregshift(sc->sc_class);
278 if (uart_fdt_get_io_width(node, &iowidth) != 0)
279 iowidth = uart_getregiowidth(sc->sc_class);
280
281 return (uart_bus_probe(dev, (int)shift, (int)iowidth, (int)clock, 0, 0, 0));
282 }
283
284 DRIVER_MODULE(uart, simplebus, uart_fdt_driver, uart_devclass, 0, 0);
285 DRIVER_MODULE(uart, ofwbus, uart_fdt_driver, uart_devclass, 0, 0);
286