1 /* $NetBSD: fdtbus.c,v 1.30 2019/10/01 23:32:52 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.30 2019/10/01 23:32:52 jmcneill Exp $"); 31 32 #include <sys/param.h> 33 #include <sys/systm.h> 34 #include <sys/device.h> 35 #include <sys/kmem.h> 36 37 #include <sys/bus.h> 38 39 #include <dev/ofw/openfirm.h> 40 41 #include <dev/fdt/fdtvar.h> 42 43 #include <libfdt.h> 44 45 #include "locators.h" 46 47 #define FDT_MAX_PATH 256 48 49 struct fdt_node { 50 device_t n_bus; 51 device_t n_dev; 52 int n_phandle; 53 const char *n_name; 54 struct fdt_attach_args n_faa; 55 cfdata_t n_cf; 56 57 u_int n_order; 58 59 bool n_pinctrl_init; 60 61 TAILQ_ENTRY(fdt_node) n_nodes; 62 }; 63 64 static TAILQ_HEAD(, fdt_node) fdt_nodes = 65 TAILQ_HEAD_INITIALIZER(fdt_nodes); 66 static bool fdt_need_rescan = false; 67 68 struct fdt_softc { 69 device_t sc_dev; 70 int sc_phandle; 71 struct fdt_attach_args sc_faa; 72 }; 73 74 static int fdt_match(device_t, cfdata_t, void *); 75 static void fdt_attach(device_t, device_t, void *); 76 static int fdt_rescan(device_t, const char *, const int *); 77 static void fdt_childdet(device_t, device_t); 78 79 static int fdt_scan_submatch(device_t, cfdata_t, const int *, void *); 80 static cfdata_t fdt_scan_best(struct fdt_softc *, struct fdt_node *); 81 static void fdt_scan(struct fdt_softc *, int); 82 static void fdt_add_node(struct fdt_node *); 83 static u_int fdt_get_order(int); 84 static void fdt_pre_attach(struct fdt_node *); 85 static void fdt_post_attach(struct fdt_node *); 86 87 static const char * const fdtbus_compatible[] = 88 { "simple-bus", NULL }; 89 90 CFATTACH_DECL2_NEW(simplebus, sizeof(struct fdt_softc), 91 fdt_match, fdt_attach, NULL, NULL, fdt_rescan, fdt_childdet); 92 93 static int 94 fdt_match(device_t parent, cfdata_t cf, void *aux) 95 { 96 const struct fdt_attach_args *faa = aux; 97 const int phandle = faa->faa_phandle; 98 int match; 99 100 /* Check compatible string */ 101 match = of_match_compatible(phandle, fdtbus_compatible); 102 if (match) 103 return match; 104 105 /* Some nodes have no compatible string */ 106 if (!of_hasprop(phandle, "compatible")) { 107 if (OF_finddevice("/clocks") == phandle) 108 return 1; 109 if (OF_finddevice("/chosen") == phandle) 110 return 1; 111 } 112 113 /* Always match the root node */ 114 return OF_finddevice("/") == phandle; 115 } 116 117 static void 118 fdt_attach(device_t parent, device_t self, void *aux) 119 { 120 struct fdt_softc *sc = device_private(self); 121 const struct fdt_attach_args *faa = aux; 122 const int phandle = faa->faa_phandle; 123 const char *descr; 124 125 sc->sc_dev = self; 126 sc->sc_phandle = phandle; 127 sc->sc_faa = *faa; 128 129 aprint_naive("\n"); 130 131 descr = fdtbus_get_string(phandle, "model"); 132 if (descr) 133 aprint_normal(": %s\n", descr); 134 else 135 aprint_normal("\n"); 136 137 /* Find all child nodes */ 138 fdt_add_bus(self, phandle, &sc->sc_faa); 139 140 /* Only the root bus should scan for devices */ 141 if (OF_finddevice("/") != faa->faa_phandle) 142 return; 143 144 /* Scan devices */ 145 fdt_rescan(self, NULL, NULL); 146 } 147 148 static int 149 fdt_rescan(device_t self, const char *ifattr, const int *locs) 150 { 151 struct fdt_softc *sc = device_private(self); 152 struct fdt_node *node; 153 int pass; 154 155 TAILQ_FOREACH(node, &fdt_nodes, n_nodes) 156 node->n_cf = fdt_scan_best(sc, node); 157 158 pass = 0; 159 fdt_need_rescan = false; 160 do { 161 fdt_scan(sc, pass); 162 if (fdt_need_rescan == true) { 163 pass = 0; 164 TAILQ_FOREACH(node, &fdt_nodes, n_nodes) { 165 if (node->n_dev == NULL) 166 node->n_cf = fdt_scan_best(sc, node); 167 } 168 fdt_need_rescan = false; 169 } else { 170 pass++; 171 } 172 } while (pass <= FDTCF_PASS_DEFAULT); 173 174 return 0; 175 } 176 177 static void 178 fdt_childdet(device_t parent, device_t child) 179 { 180 struct fdt_node *node; 181 182 TAILQ_FOREACH(node, &fdt_nodes, n_nodes) 183 if (node->n_dev == child) { 184 node->n_dev = NULL; 185 break; 186 } 187 } 188 189 static void 190 fdt_init_attach_args(const struct fdt_attach_args *faa_tmpl, struct fdt_node *node, 191 bool quiet, struct fdt_attach_args *faa) 192 { 193 *faa = *faa_tmpl; 194 faa->faa_phandle = node->n_phandle; 195 faa->faa_name = node->n_name; 196 faa->faa_quiet = quiet; 197 } 198 199 static bool 200 fdt_add_bus_stdmatch(void *arg, int child) 201 { 202 return fdtbus_status_okay(child); 203 } 204 205 void 206 fdt_add_bus(device_t bus, const int phandle, struct fdt_attach_args *faa) 207 { 208 fdt_add_bus_match(bus, phandle, faa, fdt_add_bus_stdmatch, NULL); 209 } 210 211 void 212 fdt_add_bus_match(device_t bus, const int phandle, struct fdt_attach_args *faa, 213 bool (*fn)(void *, int), void *fnarg) 214 { 215 int child; 216 217 for (child = OF_child(phandle); child; child = OF_peer(child)) { 218 if (fn && !fn(fnarg, child)) 219 continue; 220 221 fdt_add_child(bus, child, faa, fdt_get_order(child)); 222 } 223 } 224 225 void 226 fdt_add_child(device_t bus, const int child, struct fdt_attach_args *faa, 227 u_int order) 228 { 229 struct fdt_node *node; 230 231 /* Add the node to our device list */ 232 node = kmem_alloc(sizeof(*node), KM_SLEEP); 233 node->n_bus = bus; 234 node->n_dev = NULL; 235 node->n_phandle = child; 236 node->n_name = fdtbus_get_string(child, "name"); 237 node->n_order = order; 238 node->n_faa = *faa; 239 node->n_faa.faa_phandle = child; 240 node->n_faa.faa_name = node->n_name; 241 242 fdt_add_node(node); 243 fdt_need_rescan = true; 244 } 245 246 static int 247 fdt_scan_submatch(device_t parent, cfdata_t cf, const int *locs, void *aux) 248 { 249 if (locs[FDTCF_PASS] != FDTCF_PASS_DEFAULT && 250 locs[FDTCF_PASS] != cf->cf_loc[FDTCF_PASS]) 251 return 0; 252 253 return config_stdsubmatch(parent, cf, locs, aux); 254 } 255 256 static cfdata_t 257 fdt_scan_best(struct fdt_softc *sc, struct fdt_node *node) 258 { 259 struct fdt_attach_args faa; 260 cfdata_t cf, best_cf; 261 int match, best_match; 262 263 best_cf = NULL; 264 best_match = 0; 265 266 for (int pass = 0; pass <= FDTCF_PASS_DEFAULT; pass++) { 267 const int locs[FDTCF_NLOCS] = { 268 [FDTCF_PASS] = pass 269 }; 270 fdt_init_attach_args(&sc->sc_faa, node, true, &faa); 271 cf = config_search_loc(fdt_scan_submatch, node->n_bus, "fdt", locs, &faa); 272 if (cf == NULL) 273 continue; 274 match = config_match(node->n_bus, cf, &faa); 275 if (match > best_match) { 276 best_match = match; 277 best_cf = cf; 278 } 279 } 280 281 return best_cf; 282 } 283 284 static void 285 fdt_scan(struct fdt_softc *sc, int pass) 286 { 287 struct fdt_node *node; 288 struct fdt_attach_args faa; 289 const int locs[FDTCF_NLOCS] = { 290 [FDTCF_PASS] = pass 291 }; 292 bool quiet = pass != FDTCF_PASS_DEFAULT; 293 prop_dictionary_t dict; 294 char buf[FDT_MAX_PATH]; 295 296 TAILQ_FOREACH(node, &fdt_nodes, n_nodes) { 297 if (node->n_dev != NULL) 298 continue; 299 300 fdt_init_attach_args(&sc->sc_faa, node, quiet, &faa); 301 302 if (quiet) { 303 /* 304 * No match for this device, skip it. 305 */ 306 if (node->n_cf == NULL) 307 continue; 308 309 /* 310 * Make sure we don't attach before a better match in a later pass. 311 */ 312 cfdata_t cf_pass = 313 config_search_loc(fdt_scan_submatch, node->n_bus, "fdt", locs, &faa); 314 if (node->n_cf != cf_pass) 315 continue; 316 317 /* 318 * Attach the device. 319 */ 320 fdt_pre_attach(node); 321 node->n_dev = config_attach_loc(node->n_bus, cf_pass, locs, 322 &faa, fdtbus_print); 323 if (node->n_dev != NULL) 324 fdt_post_attach(node); 325 } else { 326 /* 327 * Default pass. 328 */ 329 fdt_pre_attach(node); 330 node->n_dev = config_found_sm_loc(node->n_bus, "fdt", locs, 331 &faa, fdtbus_print, fdt_scan_submatch); 332 if (node->n_dev != NULL) 333 fdt_post_attach(node); 334 } 335 336 if (node->n_dev) { 337 dict = device_properties(node->n_dev); 338 if (fdtbus_get_path(node->n_phandle, buf, sizeof(buf))) 339 prop_dictionary_set_cstring(dict, "fdt-path", buf); 340 } 341 } 342 } 343 344 static void 345 fdt_pre_attach(struct fdt_node *node) 346 { 347 const char *cfgname; 348 int error; 349 350 node->n_pinctrl_init = fdtbus_pinctrl_has_config(node->n_phandle, "init"); 351 352 cfgname = node->n_pinctrl_init ? "init" : "default"; 353 354 aprint_debug_dev(node->n_bus, "set %s config for %s\n", cfgname, node->n_name); 355 356 error = fdtbus_pinctrl_set_config(node->n_phandle, cfgname); 357 if (error != 0 && error != ENOENT) 358 aprint_debug_dev(node->n_bus, 359 "failed to set %s config on %s: %d\n", 360 cfgname, node->n_name, error); 361 } 362 363 static void 364 fdt_post_attach(struct fdt_node *node) 365 { 366 int error; 367 368 if (node->n_pinctrl_init) { 369 aprint_debug_dev(node->n_bus, "set default config for %s\n", node->n_name); 370 error = fdtbus_pinctrl_set_config(node->n_phandle, "default"); 371 if (error != 0 && error != ENOENT) 372 aprint_debug_dev(node->n_bus, 373 "failed to set default config on %s: %d\n", 374 node->n_name, error); 375 } 376 } 377 378 static void 379 fdt_add_node(struct fdt_node *new_node) 380 { 381 struct fdt_node *node; 382 383 TAILQ_FOREACH(node, &fdt_nodes, n_nodes) 384 if (node->n_order > new_node->n_order) { 385 TAILQ_INSERT_BEFORE(node, new_node, n_nodes); 386 return; 387 } 388 TAILQ_INSERT_TAIL(&fdt_nodes, new_node, n_nodes); 389 } 390 391 void 392 fdt_remove_byhandle(int phandle) 393 { 394 struct fdt_node *node; 395 396 TAILQ_FOREACH(node, &fdt_nodes, n_nodes) { 397 if (node->n_phandle == phandle) { 398 TAILQ_REMOVE(&fdt_nodes, node, n_nodes); 399 return; 400 } 401 } 402 } 403 404 void 405 fdt_remove_bycompat(const char *compatible[]) 406 { 407 struct fdt_node *node, *next; 408 409 TAILQ_FOREACH_SAFE(node, &fdt_nodes, n_nodes, next) { 410 if (of_match_compatible(node->n_phandle, compatible)) { 411 TAILQ_REMOVE(&fdt_nodes, node, n_nodes); 412 } 413 } 414 } 415 416 int 417 fdt_find_with_property(const char *prop, int *pindex) 418 { 419 struct fdt_node *node; 420 int index = 0; 421 422 TAILQ_FOREACH(node, &fdt_nodes, n_nodes) { 423 if (index++ < *pindex) 424 continue; 425 if (of_hasprop(node->n_phandle, prop)) { 426 *pindex = index; 427 return node->n_phandle; 428 } 429 } 430 431 return -1; 432 } 433 434 static u_int 435 fdt_get_order(int phandle) 436 { 437 u_int val = UINT_MAX; 438 int child; 439 440 of_getprop_uint32(phandle, "phandle", &val); 441 442 for (child = OF_child(phandle); child; child = OF_peer(child)) { 443 u_int child_val = fdt_get_order(child); 444 if (child_val < val) 445 val = child_val; 446 } 447 448 return val; 449 } 450 451 int 452 fdtbus_print(void *aux, const char *pnp) 453 { 454 const struct fdt_attach_args * const faa = aux; 455 char buf[FDT_MAX_PATH]; 456 const char *name = buf; 457 int len; 458 459 if (pnp && faa->faa_quiet) 460 return QUIET; 461 462 /* Skip "not configured" for nodes w/o compatible property */ 463 if (pnp && OF_getproplen(faa->faa_phandle, "compatible") <= 0) 464 return QUIET; 465 466 if (!fdtbus_get_path(faa->faa_phandle, buf, sizeof(buf))) 467 name = faa->faa_name; 468 469 if (pnp) { 470 aprint_normal("%s at %s", name, pnp); 471 const char *compat = fdt_getprop(fdtbus_get_data(), 472 fdtbus_phandle2offset(faa->faa_phandle), "compatible", 473 &len); 474 while (len > 0) { 475 aprint_debug(" <%s>", compat); 476 len -= (strlen(compat) + 1); 477 compat += (strlen(compat) + 1); 478 } 479 } else 480 aprint_debug(" (%s)", name); 481 482 return UNCONF; 483 } 484