xref: /netbsd-src/sys/dev/fdt/fdtbus.c (revision 53b02e147d4ed531c0d2a5ca9b3e8026ba3e99b5)
1 /* $NetBSD: fdtbus.c,v 1.44 2021/11/07 17:12:15 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.44 2021/11/07 17:12:15 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 	{ .compat = "simple-pm-bus" },
93 	DEVICE_COMPAT_EOL
94 };
95 
96 CFATTACH_DECL2_NEW(simplebus, sizeof(struct fdt_softc),
97     fdt_match, fdt_attach, NULL, NULL, fdt_rescan, fdt_childdet);
98 
99 static int
100 fdt_match(device_t parent, cfdata_t cf, void *aux)
101 {
102 	const struct fdt_attach_args *faa = aux;
103 	const int phandle = faa->faa_phandle;
104 	int match;
105 
106 	/* Check compatible string */
107 	match = of_compatible_match(phandle, compat_data);
108 	if (match)
109 		return match;
110 
111 	/* Some nodes have no compatible string */
112 	if (!of_hasprop(phandle, "compatible")) {
113 		if (OF_finddevice("/clocks") == phandle)
114 			return 1;
115 		if (OF_finddevice("/chosen") == phandle)
116 			return 1;
117 	}
118 
119 	/* Always match the root node */
120 	return OF_finddevice("/") == phandle;
121 }
122 
123 static void
124 fdt_attach(device_t parent, device_t self, void *aux)
125 {
126 	struct fdt_softc *sc = device_private(self);
127 	const struct fdt_attach_args *faa = aux;
128 	const int phandle = faa->faa_phandle;
129 	const char *descr, *model;
130 
131 	sc->sc_dev = self;
132 	sc->sc_phandle = phandle;
133 	sc->sc_faa = *faa;
134 
135 	aprint_naive("\n");
136 
137 	descr = fdtbus_get_string(phandle, "model");
138 	if (descr)
139 		aprint_normal(": %s\n", descr);
140 	else
141 		aprint_normal("\n");
142 
143 	/* Find all child nodes */
144 	fdt_add_bus(self, phandle, &sc->sc_faa);
145 
146 	/* Only the root bus should scan for devices */
147 	if (OF_finddevice("/") != faa->faa_phandle)
148 		return;
149 
150 	/* Set hw.model if available */
151 	model = fdtbus_get_string(phandle, "compatible");
152 	if (model)
153 		cpu_setmodel("%s", model);
154 	else if (descr)
155 		cpu_setmodel("%s", descr);
156 
157 	/* Scan devices */
158 	fdt_rescan(self, NULL, NULL);
159 }
160 
161 static int
162 fdt_rescan(device_t self, const char *ifattr, const int *locs)
163 {
164 	struct fdt_softc *sc = device_private(self);
165 	struct fdt_node *node;
166 	int pass;
167 
168 	TAILQ_FOREACH(node, &fdt_nodes, n_nodes)
169 		fdt_scan_best(sc, node);
170 
171 	pass = 0;
172 	fdt_need_rescan = false;
173 	do {
174 		fdt_scan(sc, pass);
175 		if (fdt_need_rescan == true) {
176 			pass = 0;
177 			TAILQ_FOREACH(node, &fdt_nodes, n_nodes) {
178 				if (node->n_cfpass == -1)
179 					fdt_scan_best(sc, node);
180 			}
181 			fdt_need_rescan = false;
182 		} else {
183 			pass++;
184 		}
185 	} while (pass <= FDTCF_PASS_DEFAULT);
186 
187 	return 0;
188 }
189 
190 static void
191 fdt_childdet(device_t parent, device_t child)
192 {
193 	struct fdt_node *node;
194 
195 	TAILQ_FOREACH(node, &fdt_nodes, n_nodes)
196 		if (node->n_dev == child) {
197 			node->n_dev = NULL;
198 			break;
199 		}
200 }
201 
202 static void
203 fdt_init_attach_args(const struct fdt_attach_args *faa_tmpl, struct fdt_node *node,
204     bool quiet, struct fdt_attach_args *faa)
205 {
206 	*faa = *faa_tmpl;
207 	faa->faa_phandle = node->n_phandle;
208 	faa->faa_name = node->n_name;
209 	faa->faa_quiet = quiet;
210 	faa->faa_bst = node->n_faa.faa_bst;
211 	faa->faa_dmat = fdtbus_iommu_map(node->n_phandle, 0,
212 	   node->n_faa.faa_dmat);
213 }
214 
215 static bool
216 fdt_add_bus_stdmatch(void *arg, int child)
217 {
218 	return fdtbus_status_okay(child);
219 }
220 
221 void
222 fdt_add_bus(device_t bus, const int phandle, struct fdt_attach_args *faa)
223 {
224 	fdt_add_bus_match(bus, phandle, faa, fdt_add_bus_stdmatch, NULL);
225 }
226 
227 void
228 fdt_add_bus_match(device_t bus, const int phandle, struct fdt_attach_args *faa,
229     bool (*fn)(void *, int), void *fnarg)
230 {
231 	int child;
232 
233 	for (child = OF_child(phandle); child; child = OF_peer(child)) {
234 		if (fn && !fn(fnarg, child))
235 			continue;
236 
237 		fdt_add_child(bus, child, faa, fdt_get_order(child));
238 	}
239 }
240 
241 static int
242 fdt_dma_translate(int phandle, struct fdt_dma_range **ranges, u_int *nranges)
243 {
244 	const uint8_t *data;
245 	int len, n;
246 
247 	const int parent = OF_parent(phandle);
248 	if (parent == -1)
249 		return 1;	/* done searching */
250 
251 	data = fdtbus_get_prop(phandle, "dma-ranges", &len);
252 	if (data == NULL)
253 		return 1;	/* no dma-ranges property, stop searching */
254 
255 	if (len == 0)
256 		return 0;	/* dma-ranges property is empty, keep going */
257 
258 	const int addr_cells = fdtbus_get_addr_cells(phandle);
259 	const int size_cells = fdtbus_get_size_cells(phandle);
260 	const int paddr_cells = fdtbus_get_addr_cells(parent);
261 	if (addr_cells == -1 || size_cells == -1 || paddr_cells == -1)
262 		return 1;
263 
264 	const int entry_size = (addr_cells + paddr_cells + size_cells) * 4;
265 
266 	*nranges = len / entry_size;
267 	*ranges = kmem_alloc(sizeof(struct fdt_dma_range) * *nranges, KM_SLEEP);
268 	for (n = 0; len >= entry_size; n++, len -= entry_size) {
269 		const uint64_t cba = fdtbus_get_cells(data, addr_cells);
270 		data += addr_cells * 4;
271 		const uint64_t pba = fdtbus_get_cells(data, paddr_cells);
272 		data += paddr_cells * 4;
273 		const uint64_t cl = fdtbus_get_cells(data, size_cells);
274 		data += size_cells * 4;
275 
276 		(*ranges)[n].dr_sysbase = pba;
277 		(*ranges)[n].dr_busbase = cba;
278 		(*ranges)[n].dr_len = cl;
279 	}
280 
281 	return 1;
282 }
283 
284 static bus_dma_tag_t
285 fdt_get_dma_tag(struct fdt_node *node)
286 {
287 	struct fdt_dma_range *ranges = NULL;
288 	u_int nranges = 0;
289 	int parent;
290 
291 	parent = OF_parent(node->n_phandle);
292 	while (parent != -1) {
293 		if (fdt_dma_translate(parent, &ranges, &nranges) != 0)
294 			break;
295 		parent = OF_parent(parent);
296 	}
297 
298 	return fdtbus_dma_tag_create(node->n_phandle, ranges, nranges);
299 }
300 
301 static uint32_t
302 fdt_bus_flags(int phandle, uint32_t *flags)
303 {
304 	if (of_hasprop(phandle, "nonposted-mmio")) {
305 		*flags |= FDT_BUS_SPACE_FLAG_NONPOSTED_MMIO;
306 		return 1;
307 	}
308 
309 	return 0;
310 }
311 
312 static bus_space_tag_t
313 fdt_get_bus_tag(struct fdt_node *node)
314 {
315 	uint32_t flags = 0;
316 	int parent;
317 
318 	parent = OF_parent(node->n_phandle);
319 	while (parent != -1) {
320 		if (fdt_bus_flags(parent, &flags) != 0) {
321 			break;
322 		}
323 		parent = OF_parent(parent);
324 	}
325 
326 	return fdtbus_bus_tag_create(node->n_phandle, flags);
327 }
328 
329 void
330 fdt_add_child(device_t bus, const int child, struct fdt_attach_args *faa,
331     u_int order)
332 {
333 	struct fdt_node *node;
334 
335 	/* Add the node to our device list */
336 	node = kmem_zalloc(sizeof(*node), KM_SLEEP);
337 	node->n_bus = bus;
338 	node->n_dev = NULL;
339 	node->n_phandle = child;
340 	node->n_name = fdtbus_get_string(child, "name");
341 	node->n_cfpass = -1;
342 	node->n_cf = NULL;
343 	node->n_order = order;
344 	node->n_faa = *faa;
345 	node->n_faa.faa_phandle = child;
346 	node->n_faa.faa_name = node->n_name;
347 	node->n_faa.faa_bst = fdt_get_bus_tag(node);
348 	node->n_faa.faa_dmat = fdt_get_dma_tag(node);
349 
350 	fdt_add_node(node);
351 	fdt_need_rescan = true;
352 }
353 
354 static int
355 fdt_scan_submatch(device_t parent, cfdata_t cf, const int *locs, void *aux)
356 {
357 	if (locs[FDTCF_PASS] != FDTCF_PASS_DEFAULT &&
358 	    locs[FDTCF_PASS] != cf->cf_loc[FDTCF_PASS])
359 		return 0;
360 
361 	return config_stdsubmatch(parent, cf, locs, aux);
362 }
363 
364 static void
365 fdt_scan_best(struct fdt_softc *sc, struct fdt_node *node)
366 {
367 	struct fdt_attach_args faa;
368 	cfdata_t cf, best_cf;
369 	int match, best_match, best_pass;
370 
371 	best_cf = NULL;
372 	best_match = 0;
373 	best_pass = FDTCF_PASS_DEFAULT;
374 
375 	for (int pass = 0; pass <= FDTCF_PASS_DEFAULT; pass++) {
376 		const int locs[FDTCF_NLOCS] = {
377 			[FDTCF_PASS] = pass
378 		};
379 		fdt_init_attach_args(&sc->sc_faa, node, true, &faa);
380 		cf = config_search(node->n_bus, &faa,
381 		    CFARGS(.submatch = fdt_scan_submatch,
382 			   .iattr = "fdt",
383 			   .locators = locs));
384 		if (cf == NULL)
385 			continue;
386 		match = config_match(node->n_bus, cf, &faa);
387 		if (match > best_match) {
388 			best_match = match;
389 			best_cf = cf;
390 			best_pass = pass;
391 		}
392 	}
393 
394 	node->n_cf = best_cf;
395 	node->n_cfpass = best_pass;
396 }
397 
398 static void
399 fdt_scan(struct fdt_softc *sc, int pass)
400 {
401 	struct fdt_node *node;
402 	struct fdt_attach_args faa;
403 	const int locs[FDTCF_NLOCS] = {
404 		[FDTCF_PASS] = pass
405 	};
406 	bool quiet = pass != FDTCF_PASS_DEFAULT;
407 
408 	TAILQ_FOREACH(node, &fdt_nodes, n_nodes) {
409 		if (node->n_cfpass != pass || node->n_dev != NULL)
410 			continue;
411 
412 		fdt_init_attach_args(&sc->sc_faa, node, quiet, &faa);
413 
414 		if (quiet && node->n_cf == NULL) {
415 			/*
416 			 * No match for this device, skip it.
417 			 */
418 			continue;
419 		}
420 
421 		/*
422 		 * Attach the device.
423 		 */
424 		fdt_pre_attach(node);
425 
426 		if (quiet) {
427 			node->n_dev = config_attach(node->n_bus, node->n_cf,
428 			    &faa, fdtbus_print,
429 			    CFARGS(.locators = locs,
430 				   .devhandle =
431 				       devhandle_from_of(node->n_phandle)));
432 		} else {
433 			/*
434 			 * Default pass.
435 			 */
436 			node->n_dev = config_found(node->n_bus, &faa,
437 			    fdtbus_print,
438 			    CFARGS(.submatch = fdt_scan_submatch,
439 				   .iattr = "fdt",
440 				   .locators = locs,
441 				   .devhandle =
442 				       devhandle_from_of(node->n_phandle)));
443 		}
444 
445 		if (node->n_dev != NULL)
446 			fdt_post_attach(node);
447 	}
448 }
449 
450 static void
451 fdt_pre_attach(struct fdt_node *node)
452 {
453 	const char *cfgname;
454 	int error;
455 
456 	node->n_pinctrl_init = fdtbus_pinctrl_has_config(node->n_phandle, "init");
457 
458 	cfgname = node->n_pinctrl_init ? "init" : "default";
459 
460 	aprint_debug_dev(node->n_bus, "set %s config for %s\n", cfgname, node->n_name);
461 
462 	error = fdtbus_pinctrl_set_config(node->n_phandle, cfgname);
463 	if (error != 0 && error != ENOENT)
464 		aprint_debug_dev(node->n_bus,
465 		    "failed to set %s config on %s: %d\n",
466 		    cfgname, node->n_name, error);
467 }
468 
469 static void
470 fdt_post_attach(struct fdt_node *node)
471 {
472 	char buf[FDT_MAX_PATH];
473 	prop_dictionary_t dict;
474 	int error;
475 
476 	dict = device_properties(node->n_dev);
477 	if (fdtbus_get_path(node->n_phandle, buf, sizeof(buf)))
478 		prop_dictionary_set_string(dict, "fdt-path", buf);
479 
480 	if (node->n_pinctrl_init) {
481 		aprint_debug_dev(node->n_bus, "set default config for %s\n", node->n_name);
482 		error = fdtbus_pinctrl_set_config(node->n_phandle, "default");
483 		if (error != 0 && error != ENOENT)
484 			aprint_debug_dev(node->n_bus,
485 			    "failed to set default config on %s: %d\n",
486 			    node->n_name, error);
487 	}
488 }
489 
490 static void
491 fdt_add_node(struct fdt_node *new_node)
492 {
493 	struct fdt_node *node;
494 
495 	TAILQ_FOREACH(node, &fdt_nodes, n_nodes)
496 		if (node->n_order > new_node->n_order) {
497 			TAILQ_INSERT_BEFORE(node, new_node, n_nodes);
498 			return;
499 		}
500 	TAILQ_INSERT_TAIL(&fdt_nodes, new_node, n_nodes);
501 }
502 
503 void
504 fdt_remove_byhandle(int phandle)
505 {
506 	struct fdt_node *node;
507 
508 	TAILQ_FOREACH(node, &fdt_nodes, n_nodes) {
509 		if (node->n_phandle == phandle) {
510 			TAILQ_REMOVE(&fdt_nodes, node, n_nodes);
511 			return;
512 		}
513 	}
514 }
515 
516 void
517 fdt_remove_bycompat(const char *compatible[])
518 {
519 	struct fdt_node *node, *next;
520 
521 	TAILQ_FOREACH_SAFE(node, &fdt_nodes, n_nodes, next) {
522 		if (of_compatible(node->n_phandle, compatible)) {
523 			TAILQ_REMOVE(&fdt_nodes, node, n_nodes);
524 		}
525 	}
526 }
527 
528 int
529 fdt_find_with_property(const char *prop, int *pindex)
530 {
531 	struct fdt_node *node;
532 	int index = 0;
533 
534 	TAILQ_FOREACH(node, &fdt_nodes, n_nodes) {
535 		if (index++ < *pindex)
536 			continue;
537 		if (of_hasprop(node->n_phandle, prop)) {
538 			*pindex = index;
539 			return node->n_phandle;
540 		}
541 	}
542 
543 	return -1;
544 }
545 
546 static u_int
547 fdt_get_order(int phandle)
548 {
549 	u_int val = UINT_MAX;
550 	int child;
551 
552 	of_getprop_uint32(phandle, "phandle", &val);
553 
554 	for (child = OF_child(phandle); child; child = OF_peer(child)) {
555 		u_int child_val = fdt_get_order(child);
556 		if (child_val < val)
557 			val = child_val;
558 	}
559 
560 	return val;
561 }
562 
563 int
564 fdtbus_print(void *aux, const char *pnp)
565 {
566 	const struct fdt_attach_args * const faa = aux;
567 	char buf[FDT_MAX_PATH];
568 	const char *name = buf;
569 	int len;
570 
571 	if (pnp && faa->faa_quiet)
572 		return QUIET;
573 
574 	/* Skip "not configured" for nodes w/o compatible property */
575 	if (pnp && OF_getproplen(faa->faa_phandle, "compatible") <= 0)
576 		return QUIET;
577 
578 	if (!fdtbus_get_path(faa->faa_phandle, buf, sizeof(buf)))
579 		name = faa->faa_name;
580 
581 	if (pnp) {
582 		aprint_normal("%s at %s", name, pnp);
583 		const char *compat = fdt_getprop(fdtbus_get_data(),
584 		    fdtbus_phandle2offset(faa->faa_phandle), "compatible",
585 		    &len);
586 		while (len > 0) {
587 			aprint_debug(" <%s>", compat);
588 			len -= (strlen(compat) + 1);
589 			compat += (strlen(compat) + 1);
590 		}
591 	} else
592 		aprint_debug(" (%s)", name);
593 
594 	return UNCONF;
595 }
596