Home | History | Annotate | Line # | Download | only in fdt
fdtbus.c revision 1.7
      1  1.7  jmcneill /* $NetBSD: fdtbus.c,v 1.7 2017/04/15 00:34:29 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.7  jmcneill __KERNEL_RCSID(0, "$NetBSD: fdtbus.c,v 1.7 2017/04/15 00:34:29 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.1  jmcneill 
     37  1.1  jmcneill #include <sys/bus.h>
     38  1.1  jmcneill 
     39  1.1  jmcneill #include <dev/ofw/openfirm.h>
     40  1.1  jmcneill 
     41  1.1  jmcneill #include <dev/fdt/fdtvar.h>
     42  1.1  jmcneill 
     43  1.4  jmcneill #define	FDT_MAX_PATH	256
     44  1.4  jmcneill 
     45  1.7  jmcneill struct fdt_node {
     46  1.7  jmcneill 	device_t	n_dev;
     47  1.7  jmcneill 	int		n_phandle;
     48  1.7  jmcneill 	char		*n_name;
     49  1.7  jmcneill 
     50  1.7  jmcneill 	TAILQ_ENTRY(fdt_node) n_nodes;
     51  1.7  jmcneill };
     52  1.7  jmcneill 
     53  1.7  jmcneill struct fdt_softc {
     54  1.7  jmcneill 	device_t	sc_dev;
     55  1.7  jmcneill 	int		sc_phandle;
     56  1.7  jmcneill 	struct fdt_attach_args sc_faa;
     57  1.7  jmcneill 
     58  1.7  jmcneill 	TAILQ_HEAD(, fdt_node) sc_nodes;
     59  1.7  jmcneill };
     60  1.7  jmcneill 
     61  1.1  jmcneill static int	fdt_match(device_t, cfdata_t, void *);
     62  1.1  jmcneill static void	fdt_attach(device_t, device_t, void *);
     63  1.7  jmcneill static void	fdt_scan(struct fdt_softc *, const char *, bool);
     64  1.7  jmcneill 
     65  1.7  jmcneill static const char * const fdtbus_compatible[] =
     66  1.7  jmcneill     { "simple-bus", NULL };
     67  1.1  jmcneill 
     68  1.7  jmcneill CFATTACH_DECL_NEW(fdt, sizeof(struct fdt_softc),
     69  1.1  jmcneill     fdt_match, fdt_attach, NULL, NULL);
     70  1.1  jmcneill 
     71  1.1  jmcneill static int	fdt_print(void *, const char *);
     72  1.1  jmcneill 
     73  1.1  jmcneill static int
     74  1.1  jmcneill fdt_match(device_t parent, cfdata_t cf, void *aux)
     75  1.1  jmcneill {
     76  1.1  jmcneill 	const struct fdt_attach_args *faa = aux;
     77  1.6  jmcneill 	int match;
     78  1.1  jmcneill 
     79  1.1  jmcneill 	if (!OF_child(faa->faa_phandle))
     80  1.1  jmcneill 		return 0;
     81  1.1  jmcneill 
     82  1.7  jmcneill 	match = of_match_compatible(faa->faa_phandle, fdtbus_compatible);
     83  1.6  jmcneill 	if (match)
     84  1.6  jmcneill 		return match;
     85  1.6  jmcneill 
     86  1.6  jmcneill 	return OF_finddevice("/") == faa->faa_phandle;
     87  1.1  jmcneill }
     88  1.1  jmcneill 
     89  1.1  jmcneill static void
     90  1.1  jmcneill fdt_attach(device_t parent, device_t self, void *aux)
     91  1.1  jmcneill {
     92  1.7  jmcneill 	struct fdt_softc *sc = device_private(self);
     93  1.1  jmcneill 	const struct fdt_attach_args *faa = aux;
     94  1.1  jmcneill 	const int phandle = faa->faa_phandle;
     95  1.7  jmcneill 	struct fdt_node *node;
     96  1.7  jmcneill 	char *model, *name, *status;
     97  1.7  jmcneill 	int len, n, child;
     98  1.7  jmcneill 
     99  1.7  jmcneill 	sc->sc_dev = self;
    100  1.7  jmcneill 	sc->sc_phandle = faa->faa_phandle;
    101  1.7  jmcneill 	sc->sc_faa = *faa;
    102  1.7  jmcneill 	TAILQ_INIT(&sc->sc_nodes);
    103  1.1  jmcneill 
    104  1.1  jmcneill 	aprint_naive("\n");
    105  1.1  jmcneill 	len = OF_getproplen(phandle, "model");
    106  1.1  jmcneill 	if (len > 0) {
    107  1.1  jmcneill 		model = kmem_zalloc(len, KM_SLEEP);
    108  1.1  jmcneill 		if (OF_getprop(phandle, "model", model, len) == len) {
    109  1.1  jmcneill 			aprint_normal(": %s\n", model);
    110  1.1  jmcneill 		} else {
    111  1.1  jmcneill 			aprint_normal("\n");
    112  1.1  jmcneill 		}
    113  1.1  jmcneill 		kmem_free(model, len);
    114  1.1  jmcneill 	} else {
    115  1.1  jmcneill 		aprint_normal("\n");
    116  1.1  jmcneill 	}
    117  1.1  jmcneill 
    118  1.1  jmcneill 	for (child = OF_child(phandle); child; child = OF_peer(child)) {
    119  1.1  jmcneill 		/* If there is a "status" property, make sure it is "okay" */
    120  1.1  jmcneill 		len = OF_getproplen(child, "status");
    121  1.1  jmcneill 		if (len > 0) {
    122  1.1  jmcneill 			status = kmem_zalloc(len, KM_SLEEP);
    123  1.3     skrll 			int alen __diagused = OF_getprop(child, "status", status, len);
    124  1.1  jmcneill 			KASSERT(alen == len);
    125  1.2  jmcneill 			const bool okay_p = strcmp(status, "okay") == 0 ||
    126  1.2  jmcneill 					    strcmp(status, "ok") == 0;
    127  1.1  jmcneill 			kmem_free(status, len);
    128  1.1  jmcneill 			if (!okay_p) {
    129  1.1  jmcneill 				continue;
    130  1.1  jmcneill 			}
    131  1.1  jmcneill 		}
    132  1.1  jmcneill 
    133  1.1  jmcneill 		len = OF_getproplen(child, "name");
    134  1.1  jmcneill 		if (len <= 0) {
    135  1.1  jmcneill 			continue;
    136  1.1  jmcneill 		}
    137  1.1  jmcneill 		name = kmem_zalloc(len, KM_SLEEP);
    138  1.7  jmcneill 		if (OF_getprop(child, "name", name, len) != len) {
    139  1.7  jmcneill 			continue;
    140  1.1  jmcneill 		}
    141  1.7  jmcneill 
    142  1.7  jmcneill 		/* Add the node to our device list */
    143  1.7  jmcneill 		node = kmem_alloc(sizeof(*node), KM_SLEEP);
    144  1.7  jmcneill 		node->n_dev = NULL;
    145  1.7  jmcneill 		node->n_phandle = child;
    146  1.7  jmcneill 		node->n_name = name;
    147  1.7  jmcneill 		TAILQ_INSERT_TAIL(&sc->sc_nodes, node, n_nodes);
    148  1.1  jmcneill 	}
    149  1.7  jmcneill 
    150  1.7  jmcneill 	/* Scan and attach all known busses in the tree. */
    151  1.7  jmcneill 	for (n = 0; fdtbus_compatible[n] != NULL; n++)
    152  1.7  jmcneill 		fdt_scan(sc, fdtbus_compatible[n], true);
    153  1.7  jmcneill 
    154  1.7  jmcneill 	/* Only the root bus should scan for devices */
    155  1.7  jmcneill 	if (OF_finddevice("/") != faa->faa_phandle)
    156  1.7  jmcneill 		return;
    157  1.7  jmcneill 
    158  1.7  jmcneill 	/* Scan the tree for "early init" devices */
    159  1.7  jmcneill 	for (n = 0; n < faa->faa_ninit; n++)
    160  1.7  jmcneill 		fdt_scan(sc, faa->faa_init[n], false);
    161  1.7  jmcneill 
    162  1.7  jmcneill 	/* Finally, scan the tree for all other devices */
    163  1.7  jmcneill 	fdt_scan(sc, NULL, false);
    164  1.1  jmcneill }
    165  1.1  jmcneill 
    166  1.7  jmcneill static void
    167  1.7  jmcneill fdt_scan(struct fdt_softc *sc, const char *compat, bool bus)
    168  1.1  jmcneill {
    169  1.7  jmcneill 	struct fdt_node *node;
    170  1.7  jmcneill 	struct fdt_attach_args faa;
    171  1.7  jmcneill 	cfdata_t cf;
    172  1.7  jmcneill 
    173  1.7  jmcneill 	TAILQ_FOREACH(node, &sc->sc_nodes, n_nodes) {
    174  1.7  jmcneill 		if (node->n_dev != NULL) {
    175  1.7  jmcneill 			/*
    176  1.7  jmcneill 			 * Child is already attached. If it is a bus,
    177  1.7  jmcneill 			 * recursively scan.
    178  1.7  jmcneill 			 */
    179  1.7  jmcneill 			if (device_is_a(node->n_dev, "fdt"))
    180  1.7  jmcneill 				fdt_scan(device_private(node->n_dev), compat,
    181  1.7  jmcneill 				    bus);
    182  1.7  jmcneill 			continue;
    183  1.7  jmcneill 		}
    184  1.7  jmcneill 
    185  1.7  jmcneill 		faa = sc->sc_faa;
    186  1.7  jmcneill 		faa.faa_phandle = node->n_phandle;
    187  1.7  jmcneill 		faa.faa_name = node->n_name;
    188  1.7  jmcneill 
    189  1.7  jmcneill 		/*
    190  1.7  jmcneill 		 * Only attach busses to nodes where this driver is the best
    191  1.7  jmcneill 		 * match.
    192  1.7  jmcneill 		 */
    193  1.7  jmcneill 		if (compat && bus) {
    194  1.7  jmcneill 			cf = config_search_loc(NULL, sc->sc_dev, NULL, NULL,
    195  1.7  jmcneill 			    &faa);
    196  1.7  jmcneill 			if (cf == NULL || strcmp(cf->cf_name, "fdt") != 0)
    197  1.7  jmcneill 				continue;
    198  1.7  jmcneill 		}
    199  1.1  jmcneill 
    200  1.7  jmcneill 		/*
    201  1.7  jmcneill 		 * Attach the child device.
    202  1.7  jmcneill 		 */
    203  1.7  jmcneill 		const char * const compats[] = { compat, NULL };
    204  1.7  jmcneill 		if (compat == NULL ||
    205  1.7  jmcneill 		    of_match_compatible(node->n_phandle, compats)) {
    206  1.7  jmcneill 			node->n_dev = config_found(sc->sc_dev, &faa, fdt_print);
    207  1.7  jmcneill 		}
    208  1.1  jmcneill 	}
    209  1.1  jmcneill }
    210  1.1  jmcneill 
    211  1.1  jmcneill static int
    212  1.1  jmcneill fdt_print(void *aux, const char *pnp)
    213  1.1  jmcneill {
    214  1.1  jmcneill 	const struct fdt_attach_args * const faa = aux;
    215  1.4  jmcneill 	char buf[FDT_MAX_PATH];
    216  1.4  jmcneill 	const char *name = buf;
    217  1.1  jmcneill 
    218  1.5  jmcneill 	/* Skip "not configured" for nodes w/o compatible property */
    219  1.5  jmcneill 	if (OF_getproplen(faa->faa_phandle, "compatible") <= 0)
    220  1.5  jmcneill 		return QUIET;
    221  1.5  jmcneill 
    222  1.1  jmcneill 	if (pnp) {
    223  1.4  jmcneill 		if (!fdtbus_get_path(faa->faa_phandle, buf, sizeof(buf)))
    224  1.4  jmcneill 			name = faa->faa_name;
    225  1.4  jmcneill 		aprint_normal("%s at %s", name, pnp);
    226  1.1  jmcneill 	}
    227  1.1  jmcneill 
    228  1.1  jmcneill 	return UNCONF;
    229  1.1  jmcneill }
    230