fdtbus.c revision 1.11 1 1.11 jmcneill /* $NetBSD: fdtbus.c,v 1.11 2017/04/29 12:38:26 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.11 jmcneill __KERNEL_RCSID(0, "$NetBSD: fdtbus.c,v 1.11 2017/04/29 12:38:26 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.9 jmcneill #include "locators.h"
44 1.9 jmcneill
45 1.4 jmcneill #define FDT_MAX_PATH 256
46 1.4 jmcneill
47 1.7 jmcneill struct fdt_node {
48 1.8 jmcneill device_t n_bus;
49 1.7 jmcneill device_t n_dev;
50 1.7 jmcneill int n_phandle;
51 1.7 jmcneill char *n_name;
52 1.7 jmcneill
53 1.8 jmcneill u_int n_order;
54 1.8 jmcneill
55 1.7 jmcneill TAILQ_ENTRY(fdt_node) n_nodes;
56 1.7 jmcneill };
57 1.7 jmcneill
58 1.8 jmcneill static TAILQ_HEAD(, fdt_node) fdt_nodes =
59 1.8 jmcneill TAILQ_HEAD_INITIALIZER(fdt_nodes);
60 1.8 jmcneill
61 1.7 jmcneill struct fdt_softc {
62 1.7 jmcneill device_t sc_dev;
63 1.7 jmcneill int sc_phandle;
64 1.7 jmcneill struct fdt_attach_args sc_faa;
65 1.7 jmcneill };
66 1.7 jmcneill
67 1.1 jmcneill static int fdt_match(device_t, cfdata_t, void *);
68 1.1 jmcneill static void fdt_attach(device_t, device_t, void *);
69 1.9 jmcneill static int fdt_scan_submatch(device_t, cfdata_t, const int *, void *);
70 1.8 jmcneill static void fdt_scan_bus(struct fdt_softc *);
71 1.9 jmcneill static void fdt_scan(struct fdt_softc *, int);
72 1.8 jmcneill static void fdt_add_node(struct fdt_node *);
73 1.8 jmcneill static u_int fdt_get_order(int);
74 1.8 jmcneill
75 1.8 jmcneill static int fdt_print(void *, const char *);
76 1.7 jmcneill
77 1.7 jmcneill static const char * const fdtbus_compatible[] =
78 1.7 jmcneill { "simple-bus", NULL };
79 1.1 jmcneill
80 1.7 jmcneill CFATTACH_DECL_NEW(fdt, sizeof(struct fdt_softc),
81 1.1 jmcneill fdt_match, fdt_attach, NULL, NULL);
82 1.1 jmcneill
83 1.1 jmcneill static int
84 1.1 jmcneill fdt_match(device_t parent, cfdata_t cf, void *aux)
85 1.1 jmcneill {
86 1.1 jmcneill const struct fdt_attach_args *faa = aux;
87 1.6 jmcneill int match;
88 1.1 jmcneill
89 1.7 jmcneill match = of_match_compatible(faa->faa_phandle, fdtbus_compatible);
90 1.6 jmcneill if (match)
91 1.6 jmcneill return match;
92 1.6 jmcneill
93 1.6 jmcneill return OF_finddevice("/") == faa->faa_phandle;
94 1.1 jmcneill }
95 1.1 jmcneill
96 1.1 jmcneill static void
97 1.1 jmcneill fdt_attach(device_t parent, device_t self, void *aux)
98 1.1 jmcneill {
99 1.7 jmcneill struct fdt_softc *sc = device_private(self);
100 1.1 jmcneill const struct fdt_attach_args *faa = aux;
101 1.1 jmcneill const int phandle = faa->faa_phandle;
102 1.7 jmcneill struct fdt_node *node;
103 1.11 jmcneill char *model, *name;
104 1.8 jmcneill int len, child;
105 1.7 jmcneill
106 1.7 jmcneill sc->sc_dev = self;
107 1.8 jmcneill sc->sc_phandle = phandle;
108 1.7 jmcneill sc->sc_faa = *faa;
109 1.1 jmcneill
110 1.1 jmcneill aprint_naive("\n");
111 1.1 jmcneill len = OF_getproplen(phandle, "model");
112 1.1 jmcneill if (len > 0) {
113 1.1 jmcneill model = kmem_zalloc(len, KM_SLEEP);
114 1.1 jmcneill if (OF_getprop(phandle, "model", model, len) == len) {
115 1.1 jmcneill aprint_normal(": %s\n", model);
116 1.1 jmcneill } else {
117 1.1 jmcneill aprint_normal("\n");
118 1.1 jmcneill }
119 1.1 jmcneill kmem_free(model, len);
120 1.1 jmcneill } else {
121 1.1 jmcneill aprint_normal("\n");
122 1.1 jmcneill }
123 1.1 jmcneill
124 1.1 jmcneill for (child = OF_child(phandle); child; child = OF_peer(child)) {
125 1.11 jmcneill if (!fdtbus_status_okay(child))
126 1.11 jmcneill continue;
127 1.1 jmcneill
128 1.1 jmcneill len = OF_getproplen(child, "name");
129 1.11 jmcneill if (len <= 0)
130 1.1 jmcneill continue;
131 1.11 jmcneill
132 1.1 jmcneill name = kmem_zalloc(len, KM_SLEEP);
133 1.11 jmcneill if (OF_getprop(child, "name", name, len) != len)
134 1.7 jmcneill continue;
135 1.7 jmcneill
136 1.7 jmcneill /* Add the node to our device list */
137 1.7 jmcneill node = kmem_alloc(sizeof(*node), KM_SLEEP);
138 1.8 jmcneill node->n_bus = self;
139 1.7 jmcneill node->n_dev = NULL;
140 1.7 jmcneill node->n_phandle = child;
141 1.7 jmcneill node->n_name = name;
142 1.8 jmcneill node->n_order = fdt_get_order(node->n_phandle);
143 1.8 jmcneill fdt_add_node(node);
144 1.1 jmcneill }
145 1.7 jmcneill
146 1.7 jmcneill /* Scan and attach all known busses in the tree. */
147 1.8 jmcneill fdt_scan_bus(sc);
148 1.7 jmcneill
149 1.7 jmcneill /* Only the root bus should scan for devices */
150 1.7 jmcneill if (OF_finddevice("/") != faa->faa_phandle)
151 1.7 jmcneill return;
152 1.7 jmcneill
153 1.9 jmcneill aprint_debug_dev(sc->sc_dev, " order phandle bus path\n");
154 1.9 jmcneill aprint_debug_dev(sc->sc_dev, " ===== ======= === ====\n");
155 1.9 jmcneill TAILQ_FOREACH(node, &fdt_nodes, n_nodes) {
156 1.9 jmcneill char buf[FDT_MAX_PATH];
157 1.9 jmcneill const char *path = buf;
158 1.9 jmcneill if (!fdtbus_get_path(node->n_phandle, buf, sizeof(buf)))
159 1.9 jmcneill path = node->n_name;
160 1.9 jmcneill aprint_debug_dev(sc->sc_dev, " %04x 0x%04x %s %s\n",
161 1.9 jmcneill node->n_order & 0xffff, node->n_phandle,
162 1.9 jmcneill device_xname(node->n_bus), path);
163 1.9 jmcneill }
164 1.9 jmcneill
165 1.8 jmcneill /* Scan devices */
166 1.9 jmcneill for (int pass = 1; pass <= FDTCF_PASS_DEFAULT; pass++)
167 1.9 jmcneill fdt_scan(sc, pass);
168 1.8 jmcneill }
169 1.7 jmcneill
170 1.8 jmcneill static void
171 1.8 jmcneill fdt_init_attach_args(struct fdt_softc *sc, struct fdt_node *node,
172 1.9 jmcneill bool quiet, struct fdt_attach_args *faa)
173 1.8 jmcneill {
174 1.8 jmcneill *faa = sc->sc_faa;
175 1.8 jmcneill faa->faa_phandle = node->n_phandle;
176 1.8 jmcneill faa->faa_name = node->n_name;
177 1.9 jmcneill faa->faa_quiet = quiet;
178 1.1 jmcneill }
179 1.1 jmcneill
180 1.7 jmcneill static void
181 1.8 jmcneill fdt_scan_bus(struct fdt_softc *sc)
182 1.1 jmcneill {
183 1.7 jmcneill struct fdt_node *node;
184 1.7 jmcneill struct fdt_attach_args faa;
185 1.7 jmcneill cfdata_t cf;
186 1.7 jmcneill
187 1.8 jmcneill TAILQ_FOREACH(node, &fdt_nodes, n_nodes) {
188 1.8 jmcneill if (node->n_bus != sc->sc_dev)
189 1.8 jmcneill continue;
190 1.8 jmcneill if (node->n_dev != NULL)
191 1.7 jmcneill continue;
192 1.7 jmcneill
193 1.9 jmcneill fdt_init_attach_args(sc, node, true, &faa);
194 1.7 jmcneill
195 1.7 jmcneill /*
196 1.7 jmcneill * Only attach busses to nodes where this driver is the best
197 1.7 jmcneill * match.
198 1.7 jmcneill */
199 1.8 jmcneill cf = config_search_loc(NULL, node->n_bus, NULL, NULL, &faa);
200 1.8 jmcneill if (cf == NULL || strcmp(cf->cf_name, "fdt") != 0)
201 1.8 jmcneill continue;
202 1.8 jmcneill
203 1.8 jmcneill /*
204 1.8 jmcneill * Attach the bus.
205 1.8 jmcneill */
206 1.8 jmcneill node->n_dev = config_found(node->n_bus, &faa, fdt_print);
207 1.8 jmcneill }
208 1.8 jmcneill }
209 1.8 jmcneill
210 1.9 jmcneill static int
211 1.9 jmcneill fdt_scan_submatch(device_t parent, cfdata_t cf, const int *locs, void *aux)
212 1.9 jmcneill {
213 1.9 jmcneill if (locs[FDTCF_PASS] != FDTCF_PASS_DEFAULT &&
214 1.9 jmcneill locs[FDTCF_PASS] != cf->cf_loc[FDTCF_PASS])
215 1.9 jmcneill return 0;
216 1.9 jmcneill
217 1.9 jmcneill return config_stdsubmatch(parent, cf, locs, aux);
218 1.9 jmcneill }
219 1.9 jmcneill
220 1.8 jmcneill static void
221 1.9 jmcneill fdt_scan(struct fdt_softc *sc, int pass)
222 1.8 jmcneill {
223 1.8 jmcneill struct fdt_node *node;
224 1.8 jmcneill struct fdt_attach_args faa;
225 1.9 jmcneill const int locs[FDTCF_NLOCS] = {
226 1.9 jmcneill [FDTCF_PASS] = pass
227 1.9 jmcneill };
228 1.9 jmcneill bool quiet = pass != FDTCF_PASS_DEFAULT;
229 1.8 jmcneill
230 1.8 jmcneill TAILQ_FOREACH(node, &fdt_nodes, n_nodes) {
231 1.8 jmcneill if (node->n_dev != NULL)
232 1.8 jmcneill continue;
233 1.8 jmcneill
234 1.9 jmcneill fdt_init_attach_args(sc, node, quiet, &faa);
235 1.1 jmcneill
236 1.7 jmcneill /*
237 1.8 jmcneill * Attach the device.
238 1.7 jmcneill */
239 1.9 jmcneill node->n_dev = config_found_sm_loc(node->n_bus, "fdt", locs,
240 1.9 jmcneill &faa, fdt_print, fdt_scan_submatch);
241 1.8 jmcneill }
242 1.8 jmcneill }
243 1.8 jmcneill
244 1.8 jmcneill static void
245 1.8 jmcneill fdt_add_node(struct fdt_node *new_node)
246 1.8 jmcneill {
247 1.8 jmcneill struct fdt_node *node;
248 1.8 jmcneill
249 1.8 jmcneill TAILQ_FOREACH(node, &fdt_nodes, n_nodes)
250 1.8 jmcneill if (node->n_order > new_node->n_order) {
251 1.8 jmcneill TAILQ_INSERT_BEFORE(node, new_node, n_nodes);
252 1.8 jmcneill return;
253 1.7 jmcneill }
254 1.8 jmcneill TAILQ_INSERT_TAIL(&fdt_nodes, new_node, n_nodes);
255 1.8 jmcneill }
256 1.8 jmcneill
257 1.8 jmcneill static u_int
258 1.8 jmcneill fdt_get_order(int phandle)
259 1.8 jmcneill {
260 1.8 jmcneill u_int val = UINT_MAX;
261 1.8 jmcneill int child;
262 1.8 jmcneill
263 1.8 jmcneill of_getprop_uint32(phandle, "phandle", &val);
264 1.8 jmcneill
265 1.8 jmcneill for (child = OF_child(phandle); child; child = OF_peer(child)) {
266 1.8 jmcneill u_int child_val = fdt_get_order(child);
267 1.8 jmcneill if (child_val < val)
268 1.8 jmcneill val = child_val;
269 1.1 jmcneill }
270 1.8 jmcneill
271 1.8 jmcneill return val;
272 1.1 jmcneill }
273 1.1 jmcneill
274 1.1 jmcneill static int
275 1.1 jmcneill fdt_print(void *aux, const char *pnp)
276 1.1 jmcneill {
277 1.1 jmcneill const struct fdt_attach_args * const faa = aux;
278 1.4 jmcneill char buf[FDT_MAX_PATH];
279 1.4 jmcneill const char *name = buf;
280 1.1 jmcneill
281 1.10 jmcneill if (pnp && faa->faa_quiet)
282 1.9 jmcneill return QUIET;
283 1.9 jmcneill
284 1.5 jmcneill /* Skip "not configured" for nodes w/o compatible property */
285 1.10 jmcneill if (pnp && OF_getproplen(faa->faa_phandle, "compatible") <= 0)
286 1.5 jmcneill return QUIET;
287 1.5 jmcneill
288 1.10 jmcneill if (!fdtbus_get_path(faa->faa_phandle, buf, sizeof(buf)))
289 1.10 jmcneill name = faa->faa_name;
290 1.10 jmcneill
291 1.10 jmcneill if (pnp)
292 1.4 jmcneill aprint_normal("%s at %s", name, pnp);
293 1.10 jmcneill else
294 1.10 jmcneill aprint_debug(" (%s)", name);
295 1.1 jmcneill
296 1.1 jmcneill return UNCONF;
297 1.1 jmcneill }
298