xref: /netbsd-src/sys/dev/fdt/fdtbus.c (revision 122b5006ee1bd67145794b4cde92f4fe4781a5ec)
1 /* $NetBSD: fdtbus.c,v 1.43 2021/09/06 14:03:18 jmcneill 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.43 2021/09/06 14:03:18 jmcneill 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_bst = node->n_faa.faa_bst;
210 	faa->faa_dmat = fdtbus_iommu_map(node->n_phandle, 0,
211 	   node->n_faa.faa_dmat);
212 }
213 
214 static bool
215 fdt_add_bus_stdmatch(void *arg, int child)
216 {
217 	return fdtbus_status_okay(child);
218 }
219 
220 void
221 fdt_add_bus(device_t bus, const int phandle, struct fdt_attach_args *faa)
222 {
223 	fdt_add_bus_match(bus, phandle, faa, fdt_add_bus_stdmatch, NULL);
224 }
225 
226 void
227 fdt_add_bus_match(device_t bus, const int phandle, struct fdt_attach_args *faa,
228     bool (*fn)(void *, int), void *fnarg)
229 {
230 	int child;
231 
232 	for (child = OF_child(phandle); child; child = OF_peer(child)) {
233 		if (fn && !fn(fnarg, child))
234 			continue;
235 
236 		fdt_add_child(bus, child, faa, fdt_get_order(child));
237 	}
238 }
239 
240 static int
241 fdt_dma_translate(int phandle, struct fdt_dma_range **ranges, u_int *nranges)
242 {
243 	const uint8_t *data;
244 	int len, n;
245 
246 	const int parent = OF_parent(phandle);
247 	if (parent == -1)
248 		return 1;	/* done searching */
249 
250 	data = fdtbus_get_prop(phandle, "dma-ranges", &len);
251 	if (data == NULL)
252 		return 1;	/* no dma-ranges property, stop searching */
253 
254 	if (len == 0)
255 		return 0;	/* dma-ranges property is empty, keep going */
256 
257 	const int addr_cells = fdtbus_get_addr_cells(phandle);
258 	const int size_cells = fdtbus_get_size_cells(phandle);
259 	const int paddr_cells = fdtbus_get_addr_cells(parent);
260 	if (addr_cells == -1 || size_cells == -1 || paddr_cells == -1)
261 		return 1;
262 
263 	const int entry_size = (addr_cells + paddr_cells + size_cells) * 4;
264 
265 	*nranges = len / entry_size;
266 	*ranges = kmem_alloc(sizeof(struct fdt_dma_range) * *nranges, KM_SLEEP);
267 	for (n = 0; len >= entry_size; n++, len -= entry_size) {
268 		const uint64_t cba = fdtbus_get_cells(data, addr_cells);
269 		data += addr_cells * 4;
270 		const uint64_t pba = fdtbus_get_cells(data, paddr_cells);
271 		data += paddr_cells * 4;
272 		const uint64_t cl = fdtbus_get_cells(data, size_cells);
273 		data += size_cells * 4;
274 
275 		(*ranges)[n].dr_sysbase = pba;
276 		(*ranges)[n].dr_busbase = cba;
277 		(*ranges)[n].dr_len = cl;
278 	}
279 
280 	return 1;
281 }
282 
283 static bus_dma_tag_t
284 fdt_get_dma_tag(struct fdt_node *node)
285 {
286 	struct fdt_dma_range *ranges = NULL;
287 	u_int nranges = 0;
288 	int parent;
289 
290 	parent = OF_parent(node->n_phandle);
291 	while (parent != -1) {
292 		if (fdt_dma_translate(parent, &ranges, &nranges) != 0)
293 			break;
294 		parent = OF_parent(parent);
295 	}
296 
297 	return fdtbus_dma_tag_create(node->n_phandle, ranges, nranges);
298 }
299 
300 static uint32_t
301 fdt_bus_flags(int phandle, uint32_t *flags)
302 {
303 	if (of_hasprop(phandle, "nonposted-mmio")) {
304 		*flags |= FDT_BUS_SPACE_FLAG_NONPOSTED_MMIO;
305 		return 1;
306 	}
307 
308 	return 0;
309 }
310 
311 static bus_space_tag_t
312 fdt_get_bus_tag(struct fdt_node *node)
313 {
314 	uint32_t flags = 0;
315 	int parent;
316 
317 	parent = OF_parent(node->n_phandle);
318 	while (parent != -1) {
319 		if (fdt_bus_flags(parent, &flags) != 0) {
320 			break;
321 		}
322 		parent = OF_parent(parent);
323 	}
324 
325 	return fdtbus_bus_tag_create(node->n_phandle, flags);
326 }
327 
328 void
329 fdt_add_child(device_t bus, const int child, struct fdt_attach_args *faa,
330     u_int order)
331 {
332 	struct fdt_node *node;
333 
334 	/* Add the node to our device list */
335 	node = kmem_zalloc(sizeof(*node), KM_SLEEP);
336 	node->n_bus = bus;
337 	node->n_dev = NULL;
338 	node->n_phandle = child;
339 	node->n_name = fdtbus_get_string(child, "name");
340 	node->n_cfpass = -1;
341 	node->n_cf = NULL;
342 	node->n_order = order;
343 	node->n_faa = *faa;
344 	node->n_faa.faa_phandle = child;
345 	node->n_faa.faa_name = node->n_name;
346 	node->n_faa.faa_bst = fdt_get_bus_tag(node);
347 	node->n_faa.faa_dmat = fdt_get_dma_tag(node);
348 
349 	fdt_add_node(node);
350 	fdt_need_rescan = true;
351 }
352 
353 static int
354 fdt_scan_submatch(device_t parent, cfdata_t cf, const int *locs, void *aux)
355 {
356 	if (locs[FDTCF_PASS] != FDTCF_PASS_DEFAULT &&
357 	    locs[FDTCF_PASS] != cf->cf_loc[FDTCF_PASS])
358 		return 0;
359 
360 	return config_stdsubmatch(parent, cf, locs, aux);
361 }
362 
363 static void
364 fdt_scan_best(struct fdt_softc *sc, struct fdt_node *node)
365 {
366 	struct fdt_attach_args faa;
367 	cfdata_t cf, best_cf;
368 	int match, best_match, best_pass;
369 
370 	best_cf = NULL;
371 	best_match = 0;
372 	best_pass = FDTCF_PASS_DEFAULT;
373 
374 	for (int pass = 0; pass <= FDTCF_PASS_DEFAULT; pass++) {
375 		const int locs[FDTCF_NLOCS] = {
376 			[FDTCF_PASS] = pass
377 		};
378 		fdt_init_attach_args(&sc->sc_faa, node, true, &faa);
379 		cf = config_search(node->n_bus, &faa,
380 		    CFARGS(.submatch = fdt_scan_submatch,
381 			   .iattr = "fdt",
382 			   .locators = locs));
383 		if (cf == NULL)
384 			continue;
385 		match = config_match(node->n_bus, cf, &faa);
386 		if (match > best_match) {
387 			best_match = match;
388 			best_cf = cf;
389 			best_pass = pass;
390 		}
391 	}
392 
393 	node->n_cf = best_cf;
394 	node->n_cfpass = best_pass;
395 }
396 
397 static void
398 fdt_scan(struct fdt_softc *sc, int pass)
399 {
400 	struct fdt_node *node;
401 	struct fdt_attach_args faa;
402 	const int locs[FDTCF_NLOCS] = {
403 		[FDTCF_PASS] = pass
404 	};
405 	bool quiet = pass != FDTCF_PASS_DEFAULT;
406 
407 	TAILQ_FOREACH(node, &fdt_nodes, n_nodes) {
408 		if (node->n_cfpass != pass || node->n_dev != NULL)
409 			continue;
410 
411 		fdt_init_attach_args(&sc->sc_faa, node, quiet, &faa);
412 
413 		if (quiet && node->n_cf == NULL) {
414 			/*
415 			 * No match for this device, skip it.
416 			 */
417 			continue;
418 		}
419 
420 		/*
421 		 * Attach the device.
422 		 */
423 		fdt_pre_attach(node);
424 
425 		if (quiet) {
426 			node->n_dev = config_attach(node->n_bus, node->n_cf,
427 			    &faa, fdtbus_print,
428 			    CFARGS(.locators = locs,
429 				   .devhandle =
430 				       devhandle_from_of(node->n_phandle)));
431 		} else {
432 			/*
433 			 * Default pass.
434 			 */
435 			node->n_dev = config_found(node->n_bus, &faa,
436 			    fdtbus_print,
437 			    CFARGS(.submatch = fdt_scan_submatch,
438 				   .iattr = "fdt",
439 				   .locators = locs,
440 				   .devhandle =
441 				       devhandle_from_of(node->n_phandle)));
442 		}
443 
444 		if (node->n_dev != NULL)
445 			fdt_post_attach(node);
446 	}
447 }
448 
449 static void
450 fdt_pre_attach(struct fdt_node *node)
451 {
452 	const char *cfgname;
453 	int error;
454 
455 	node->n_pinctrl_init = fdtbus_pinctrl_has_config(node->n_phandle, "init");
456 
457 	cfgname = node->n_pinctrl_init ? "init" : "default";
458 
459 	aprint_debug_dev(node->n_bus, "set %s config for %s\n", cfgname, node->n_name);
460 
461 	error = fdtbus_pinctrl_set_config(node->n_phandle, cfgname);
462 	if (error != 0 && error != ENOENT)
463 		aprint_debug_dev(node->n_bus,
464 		    "failed to set %s config on %s: %d\n",
465 		    cfgname, node->n_name, error);
466 }
467 
468 static void
469 fdt_post_attach(struct fdt_node *node)
470 {
471 	char buf[FDT_MAX_PATH];
472 	prop_dictionary_t dict;
473 	int error;
474 
475 	dict = device_properties(node->n_dev);
476 	if (fdtbus_get_path(node->n_phandle, buf, sizeof(buf)))
477 		prop_dictionary_set_string(dict, "fdt-path", buf);
478 
479 	if (node->n_pinctrl_init) {
480 		aprint_debug_dev(node->n_bus, "set default config for %s\n", node->n_name);
481 		error = fdtbus_pinctrl_set_config(node->n_phandle, "default");
482 		if (error != 0 && error != ENOENT)
483 			aprint_debug_dev(node->n_bus,
484 			    "failed to set default config on %s: %d\n",
485 			    node->n_name, error);
486 	}
487 }
488 
489 static void
490 fdt_add_node(struct fdt_node *new_node)
491 {
492 	struct fdt_node *node;
493 
494 	TAILQ_FOREACH(node, &fdt_nodes, n_nodes)
495 		if (node->n_order > new_node->n_order) {
496 			TAILQ_INSERT_BEFORE(node, new_node, n_nodes);
497 			return;
498 		}
499 	TAILQ_INSERT_TAIL(&fdt_nodes, new_node, n_nodes);
500 }
501 
502 void
503 fdt_remove_byhandle(int phandle)
504 {
505 	struct fdt_node *node;
506 
507 	TAILQ_FOREACH(node, &fdt_nodes, n_nodes) {
508 		if (node->n_phandle == phandle) {
509 			TAILQ_REMOVE(&fdt_nodes, node, n_nodes);
510 			return;
511 		}
512 	}
513 }
514 
515 void
516 fdt_remove_bycompat(const char *compatible[])
517 {
518 	struct fdt_node *node, *next;
519 
520 	TAILQ_FOREACH_SAFE(node, &fdt_nodes, n_nodes, next) {
521 		if (of_compatible(node->n_phandle, compatible)) {
522 			TAILQ_REMOVE(&fdt_nodes, node, n_nodes);
523 		}
524 	}
525 }
526 
527 int
528 fdt_find_with_property(const char *prop, int *pindex)
529 {
530 	struct fdt_node *node;
531 	int index = 0;
532 
533 	TAILQ_FOREACH(node, &fdt_nodes, n_nodes) {
534 		if (index++ < *pindex)
535 			continue;
536 		if (of_hasprop(node->n_phandle, prop)) {
537 			*pindex = index;
538 			return node->n_phandle;
539 		}
540 	}
541 
542 	return -1;
543 }
544 
545 static u_int
546 fdt_get_order(int phandle)
547 {
548 	u_int val = UINT_MAX;
549 	int child;
550 
551 	of_getprop_uint32(phandle, "phandle", &val);
552 
553 	for (child = OF_child(phandle); child; child = OF_peer(child)) {
554 		u_int child_val = fdt_get_order(child);
555 		if (child_val < val)
556 			val = child_val;
557 	}
558 
559 	return val;
560 }
561 
562 int
563 fdtbus_print(void *aux, const char *pnp)
564 {
565 	const struct fdt_attach_args * const faa = aux;
566 	char buf[FDT_MAX_PATH];
567 	const char *name = buf;
568 	int len;
569 
570 	if (pnp && faa->faa_quiet)
571 		return QUIET;
572 
573 	/* Skip "not configured" for nodes w/o compatible property */
574 	if (pnp && OF_getproplen(faa->faa_phandle, "compatible") <= 0)
575 		return QUIET;
576 
577 	if (!fdtbus_get_path(faa->faa_phandle, buf, sizeof(buf)))
578 		name = faa->faa_name;
579 
580 	if (pnp) {
581 		aprint_normal("%s at %s", name, pnp);
582 		const char *compat = fdt_getprop(fdtbus_get_data(),
583 		    fdtbus_phandle2offset(faa->faa_phandle), "compatible",
584 		    &len);
585 		while (len > 0) {
586 			aprint_debug(" <%s>", compat);
587 			len -= (strlen(compat) + 1);
588 			compat += (strlen(compat) + 1);
589 		}
590 	} else
591 		aprint_debug(" (%s)", name);
592 
593 	return UNCONF;
594 }
595