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