xref: /netbsd-src/sys/dev/fdt/fdtbus.c (revision 82d56013d7b633d116a93943de88e08335357a7c)
1 /* $NetBSD: fdtbus.c,v 1.41 2021/04/24 23:36:53 thorpej Exp $ */
2 
3 /*-
4  * Copyright (c) 2015 Jared D. McNeill <jmcneill@invisible.ca>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: fdtbus.c,v 1.41 2021/04/24 23:36:53 thorpej Exp $");
31 
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/device.h>
35 #include <sys/kmem.h>
36 #include <sys/cpu.h>
37 
38 #include <sys/bus.h>
39 
40 #include <dev/ofw/openfirm.h>
41 
42 #include <dev/fdt/fdtvar.h>
43 
44 #include <libfdt.h>
45 
46 #include "locators.h"
47 
48 #define	FDT_MAX_PATH	256
49 
50 struct fdt_node {
51 	device_t	n_bus;
52 	device_t	n_dev;
53 	int		n_phandle;
54 	const char	*n_name;
55 	struct fdt_attach_args n_faa;
56 
57 	int		n_cfpass;
58 	cfdata_t	n_cf;
59 
60 	u_int		n_order;
61 
62 	bool		n_pinctrl_init;
63 
64 	TAILQ_ENTRY(fdt_node) n_nodes;
65 };
66 
67 static TAILQ_HEAD(, fdt_node) fdt_nodes =
68     TAILQ_HEAD_INITIALIZER(fdt_nodes);
69 static bool fdt_need_rescan = false;
70 
71 struct fdt_softc {
72 	device_t	sc_dev;
73 	int		sc_phandle;
74 	struct fdt_attach_args sc_faa;
75 };
76 
77 static int	fdt_match(device_t, cfdata_t, void *);
78 static void	fdt_attach(device_t, device_t, void *);
79 static int	fdt_rescan(device_t, const char *, const int *);
80 static void	fdt_childdet(device_t, device_t);
81 
82 static int	fdt_scan_submatch(device_t, cfdata_t, const int *, void *);
83 static void	fdt_scan_best(struct fdt_softc *, struct fdt_node *);
84 static void	fdt_scan(struct fdt_softc *, int);
85 static void	fdt_add_node(struct fdt_node *);
86 static u_int	fdt_get_order(int);
87 static void	fdt_pre_attach(struct fdt_node *);
88 static void	fdt_post_attach(struct fdt_node *);
89 
90 static const struct device_compatible_entry compat_data[] = {
91 	{ .compat = "simple-bus" },
92 	DEVICE_COMPAT_EOL
93 };
94 
95 CFATTACH_DECL2_NEW(simplebus, sizeof(struct fdt_softc),
96     fdt_match, fdt_attach, NULL, NULL, fdt_rescan, fdt_childdet);
97 
98 static int
99 fdt_match(device_t parent, cfdata_t cf, void *aux)
100 {
101 	const struct fdt_attach_args *faa = aux;
102 	const int phandle = faa->faa_phandle;
103 	int match;
104 
105 	/* Check compatible string */
106 	match = of_compatible_match(phandle, compat_data);
107 	if (match)
108 		return match;
109 
110 	/* Some nodes have no compatible string */
111 	if (!of_hasprop(phandle, "compatible")) {
112 		if (OF_finddevice("/clocks") == phandle)
113 			return 1;
114 		if (OF_finddevice("/chosen") == phandle)
115 			return 1;
116 	}
117 
118 	/* Always match the root node */
119 	return OF_finddevice("/") == phandle;
120 }
121 
122 static void
123 fdt_attach(device_t parent, device_t self, void *aux)
124 {
125 	struct fdt_softc *sc = device_private(self);
126 	const struct fdt_attach_args *faa = aux;
127 	const int phandle = faa->faa_phandle;
128 	const char *descr, *model;
129 
130 	sc->sc_dev = self;
131 	sc->sc_phandle = phandle;
132 	sc->sc_faa = *faa;
133 
134 	aprint_naive("\n");
135 
136 	descr = fdtbus_get_string(phandle, "model");
137 	if (descr)
138 		aprint_normal(": %s\n", descr);
139 	else
140 		aprint_normal("\n");
141 
142 	/* Find all child nodes */
143 	fdt_add_bus(self, phandle, &sc->sc_faa);
144 
145 	/* Only the root bus should scan for devices */
146 	if (OF_finddevice("/") != faa->faa_phandle)
147 		return;
148 
149 	/* Set hw.model if available */
150 	model = fdtbus_get_string(phandle, "compatible");
151 	if (model)
152 		cpu_setmodel("%s", model);
153 	else if (descr)
154 		cpu_setmodel("%s", descr);
155 
156 	/* Scan devices */
157 	fdt_rescan(self, NULL, NULL);
158 }
159 
160 static int
161 fdt_rescan(device_t self, const char *ifattr, const int *locs)
162 {
163 	struct fdt_softc *sc = device_private(self);
164 	struct fdt_node *node;
165 	int pass;
166 
167 	TAILQ_FOREACH(node, &fdt_nodes, n_nodes)
168 		fdt_scan_best(sc, node);
169 
170 	pass = 0;
171 	fdt_need_rescan = false;
172 	do {
173 		fdt_scan(sc, pass);
174 		if (fdt_need_rescan == true) {
175 			pass = 0;
176 			TAILQ_FOREACH(node, &fdt_nodes, n_nodes) {
177 				if (node->n_cfpass == -1)
178 					fdt_scan_best(sc, node);
179 			}
180 			fdt_need_rescan = false;
181 		} else {
182 			pass++;
183 		}
184 	} while (pass <= FDTCF_PASS_DEFAULT);
185 
186 	return 0;
187 }
188 
189 static void
190 fdt_childdet(device_t parent, device_t child)
191 {
192 	struct fdt_node *node;
193 
194 	TAILQ_FOREACH(node, &fdt_nodes, n_nodes)
195 		if (node->n_dev == child) {
196 			node->n_dev = NULL;
197 			break;
198 		}
199 }
200 
201 static void
202 fdt_init_attach_args(const struct fdt_attach_args *faa_tmpl, struct fdt_node *node,
203     bool quiet, struct fdt_attach_args *faa)
204 {
205 	*faa = *faa_tmpl;
206 	faa->faa_phandle = node->n_phandle;
207 	faa->faa_name = node->n_name;
208 	faa->faa_quiet = quiet;
209 	faa->faa_dmat = node->n_faa.faa_dmat;
210 }
211 
212 static bool
213 fdt_add_bus_stdmatch(void *arg, int child)
214 {
215 	return fdtbus_status_okay(child);
216 }
217 
218 void
219 fdt_add_bus(device_t bus, const int phandle, struct fdt_attach_args *faa)
220 {
221 	fdt_add_bus_match(bus, phandle, faa, fdt_add_bus_stdmatch, NULL);
222 }
223 
224 void
225 fdt_add_bus_match(device_t bus, const int phandle, struct fdt_attach_args *faa,
226     bool (*fn)(void *, int), void *fnarg)
227 {
228 	int child;
229 
230 	for (child = OF_child(phandle); child; child = OF_peer(child)) {
231 		if (fn && !fn(fnarg, child))
232 			continue;
233 
234 		fdt_add_child(bus, child, faa, fdt_get_order(child));
235 	}
236 }
237 
238 static int
239 fdt_dma_translate(int phandle, struct fdt_dma_range **ranges, u_int *nranges)
240 {
241 	const uint8_t *data;
242 	int len, n;
243 
244 	const int parent = OF_parent(phandle);
245 	if (parent == -1)
246 		return 1;	/* done searching */
247 
248 	data = fdtbus_get_prop(phandle, "dma-ranges", &len);
249 	if (data == NULL)
250 		return 1;	/* no dma-ranges property, stop searching */
251 
252 	if (len == 0)
253 		return 0;	/* dma-ranges property is empty, keep going */
254 
255 	const int addr_cells = fdtbus_get_addr_cells(phandle);
256 	const int size_cells = fdtbus_get_size_cells(phandle);
257 	const int paddr_cells = fdtbus_get_addr_cells(parent);
258 	if (addr_cells == -1 || size_cells == -1 || paddr_cells == -1)
259 		return 1;
260 
261 	const int entry_size = (addr_cells + paddr_cells + size_cells) * 4;
262 
263 	*nranges = len / entry_size;
264 	*ranges = kmem_alloc(sizeof(struct fdt_dma_range) * *nranges, KM_SLEEP);
265 	for (n = 0; len >= entry_size; n++, len -= entry_size) {
266 		const uint64_t cba = fdtbus_get_cells(data, addr_cells);
267 		data += addr_cells * 4;
268 		const uint64_t pba = fdtbus_get_cells(data, paddr_cells);
269 		data += paddr_cells * 4;
270 		const uint64_t cl = fdtbus_get_cells(data, size_cells);
271 		data += size_cells * 4;
272 
273 		(*ranges)[n].dr_sysbase = pba;
274 		(*ranges)[n].dr_busbase = cba;
275 		(*ranges)[n].dr_len = cl;
276 	}
277 
278 	return 1;
279 }
280 
281 static bus_dma_tag_t
282 fdt_get_dma_tag(struct fdt_node *node)
283 {
284 	struct fdt_dma_range *ranges = NULL;
285 	u_int nranges = 0;
286 	int parent;
287 
288 	parent = OF_parent(node->n_phandle);
289 	while (parent != -1) {
290 		if (fdt_dma_translate(parent, &ranges, &nranges) != 0)
291 			break;
292 		parent = OF_parent(parent);
293 	}
294 
295 	return fdtbus_dma_tag_create(node->n_phandle, ranges, nranges);
296 }
297 
298 void
299 fdt_add_child(device_t bus, const int child, struct fdt_attach_args *faa,
300     u_int order)
301 {
302 	struct fdt_node *node;
303 
304 	/* Add the node to our device list */
305 	node = kmem_zalloc(sizeof(*node), KM_SLEEP);
306 	node->n_bus = bus;
307 	node->n_dev = NULL;
308 	node->n_phandle = child;
309 	node->n_name = fdtbus_get_string(child, "name");
310 	node->n_cfpass = -1;
311 	node->n_cf = NULL;
312 	node->n_order = order;
313 	node->n_faa = *faa;
314 	node->n_faa.faa_phandle = child;
315 	node->n_faa.faa_name = node->n_name;
316 	node->n_faa.faa_dmat = fdt_get_dma_tag(node);
317 
318 	fdt_add_node(node);
319 	fdt_need_rescan = true;
320 }
321 
322 static int
323 fdt_scan_submatch(device_t parent, cfdata_t cf, const int *locs, void *aux)
324 {
325 	if (locs[FDTCF_PASS] != FDTCF_PASS_DEFAULT &&
326 	    locs[FDTCF_PASS] != cf->cf_loc[FDTCF_PASS])
327 		return 0;
328 
329 	return config_stdsubmatch(parent, cf, locs, aux);
330 }
331 
332 static void
333 fdt_scan_best(struct fdt_softc *sc, struct fdt_node *node)
334 {
335 	struct fdt_attach_args faa;
336 	cfdata_t cf, best_cf;
337 	int match, best_match, best_pass;
338 
339 	best_cf = NULL;
340 	best_match = 0;
341 	best_pass = FDTCF_PASS_DEFAULT;
342 
343 	for (int pass = 0; pass <= FDTCF_PASS_DEFAULT; pass++) {
344 		const int locs[FDTCF_NLOCS] = {
345 			[FDTCF_PASS] = pass
346 		};
347 		fdt_init_attach_args(&sc->sc_faa, node, true, &faa);
348 		cf = config_search(node->n_bus, &faa,
349 		    CFARG_SUBMATCH, fdt_scan_submatch,
350 		    CFARG_IATTR, "fdt",
351 		    CFARG_LOCATORS, locs,
352 		    CFARG_EOL);
353 		if (cf == NULL)
354 			continue;
355 		match = config_match(node->n_bus, cf, &faa);
356 		if (match > best_match) {
357 			best_match = match;
358 			best_cf = cf;
359 			best_pass = pass;
360 		}
361 	}
362 
363 	node->n_cf = best_cf;
364 	node->n_cfpass = best_pass;
365 }
366 
367 static void
368 fdt_scan(struct fdt_softc *sc, int pass)
369 {
370 	struct fdt_node *node;
371 	struct fdt_attach_args faa;
372 	const int locs[FDTCF_NLOCS] = {
373 		[FDTCF_PASS] = pass
374 	};
375 	bool quiet = pass != FDTCF_PASS_DEFAULT;
376 
377 	TAILQ_FOREACH(node, &fdt_nodes, n_nodes) {
378 		if (node->n_cfpass != pass || node->n_dev != NULL)
379 			continue;
380 
381 		fdt_init_attach_args(&sc->sc_faa, node, quiet, &faa);
382 
383 		if (quiet && node->n_cf == NULL) {
384 			/*
385 			 * No match for this device, skip it.
386 			 */
387 			continue;
388 		}
389 
390 		/*
391 		 * Attach the device.
392 		 */
393 		fdt_pre_attach(node);
394 
395 		if (quiet) {
396 			node->n_dev = config_attach(node->n_bus, node->n_cf,
397 			    &faa, fdtbus_print,
398 			    CFARG_LOCATORS, locs,
399 			    CFARG_DEVHANDLE, devhandle_from_of(node->n_phandle),
400 			    CFARG_EOL);
401 		} else {
402 			/*
403 			 * Default pass.
404 			 */
405 			node->n_dev = config_found(node->n_bus, &faa,
406 			    fdtbus_print,
407 			    CFARG_SUBMATCH, fdt_scan_submatch,
408 			    CFARG_IATTR, "fdt",
409 			    CFARG_LOCATORS, locs,
410 			    CFARG_DEVHANDLE, devhandle_from_of(node->n_phandle),
411 			    CFARG_EOL);
412 		}
413 
414 		if (node->n_dev != NULL)
415 			fdt_post_attach(node);
416 	}
417 }
418 
419 static void
420 fdt_pre_attach(struct fdt_node *node)
421 {
422 	const char *cfgname;
423 	int error;
424 
425 	node->n_pinctrl_init = fdtbus_pinctrl_has_config(node->n_phandle, "init");
426 
427 	cfgname = node->n_pinctrl_init ? "init" : "default";
428 
429 	aprint_debug_dev(node->n_bus, "set %s config for %s\n", cfgname, node->n_name);
430 
431 	error = fdtbus_pinctrl_set_config(node->n_phandle, cfgname);
432 	if (error != 0 && error != ENOENT)
433 		aprint_debug_dev(node->n_bus,
434 		    "failed to set %s config on %s: %d\n",
435 		    cfgname, node->n_name, error);
436 }
437 
438 static void
439 fdt_post_attach(struct fdt_node *node)
440 {
441 	char buf[FDT_MAX_PATH];
442 	prop_dictionary_t dict;
443 	int error;
444 
445 	dict = device_properties(node->n_dev);
446 	if (fdtbus_get_path(node->n_phandle, buf, sizeof(buf)))
447 		prop_dictionary_set_string(dict, "fdt-path", buf);
448 
449 	if (node->n_pinctrl_init) {
450 		aprint_debug_dev(node->n_bus, "set default config for %s\n", node->n_name);
451 		error = fdtbus_pinctrl_set_config(node->n_phandle, "default");
452 		if (error != 0 && error != ENOENT)
453 			aprint_debug_dev(node->n_bus,
454 			    "failed to set default config on %s: %d\n",
455 			    node->n_name, error);
456 	}
457 }
458 
459 static void
460 fdt_add_node(struct fdt_node *new_node)
461 {
462 	struct fdt_node *node;
463 
464 	TAILQ_FOREACH(node, &fdt_nodes, n_nodes)
465 		if (node->n_order > new_node->n_order) {
466 			TAILQ_INSERT_BEFORE(node, new_node, n_nodes);
467 			return;
468 		}
469 	TAILQ_INSERT_TAIL(&fdt_nodes, new_node, n_nodes);
470 }
471 
472 void
473 fdt_remove_byhandle(int phandle)
474 {
475 	struct fdt_node *node;
476 
477 	TAILQ_FOREACH(node, &fdt_nodes, n_nodes) {
478 		if (node->n_phandle == phandle) {
479 			TAILQ_REMOVE(&fdt_nodes, node, n_nodes);
480 			return;
481 		}
482 	}
483 }
484 
485 void
486 fdt_remove_bycompat(const char *compatible[])
487 {
488 	struct fdt_node *node, *next;
489 
490 	TAILQ_FOREACH_SAFE(node, &fdt_nodes, n_nodes, next) {
491 		if (of_compatible(node->n_phandle, compatible)) {
492 			TAILQ_REMOVE(&fdt_nodes, node, n_nodes);
493 		}
494 	}
495 }
496 
497 int
498 fdt_find_with_property(const char *prop, int *pindex)
499 {
500 	struct fdt_node *node;
501 	int index = 0;
502 
503 	TAILQ_FOREACH(node, &fdt_nodes, n_nodes) {
504 		if (index++ < *pindex)
505 			continue;
506 		if (of_hasprop(node->n_phandle, prop)) {
507 			*pindex = index;
508 			return node->n_phandle;
509 		}
510 	}
511 
512 	return -1;
513 }
514 
515 static u_int
516 fdt_get_order(int phandle)
517 {
518 	u_int val = UINT_MAX;
519 	int child;
520 
521 	of_getprop_uint32(phandle, "phandle", &val);
522 
523 	for (child = OF_child(phandle); child; child = OF_peer(child)) {
524 		u_int child_val = fdt_get_order(child);
525 		if (child_val < val)
526 			val = child_val;
527 	}
528 
529 	return val;
530 }
531 
532 int
533 fdtbus_print(void *aux, const char *pnp)
534 {
535 	const struct fdt_attach_args * const faa = aux;
536 	char buf[FDT_MAX_PATH];
537 	const char *name = buf;
538 	int len;
539 
540 	if (pnp && faa->faa_quiet)
541 		return QUIET;
542 
543 	/* Skip "not configured" for nodes w/o compatible property */
544 	if (pnp && OF_getproplen(faa->faa_phandle, "compatible") <= 0)
545 		return QUIET;
546 
547 	if (!fdtbus_get_path(faa->faa_phandle, buf, sizeof(buf)))
548 		name = faa->faa_name;
549 
550 	if (pnp) {
551 		aprint_normal("%s at %s", name, pnp);
552 		const char *compat = fdt_getprop(fdtbus_get_data(),
553 		    fdtbus_phandle2offset(faa->faa_phandle), "compatible",
554 		    &len);
555 		while (len > 0) {
556 			aprint_debug(" <%s>", compat);
557 			len -= (strlen(compat) + 1);
558 			compat += (strlen(compat) + 1);
559 		}
560 	} else
561 		aprint_debug(" (%s)", name);
562 
563 	return UNCONF;
564 }
565