1 /* $NetBSD: fdtbus.c,v 1.48 2025/10/04 01:38:02 thorpej Exp $ */ 2 3 /*- 4 * Copyright (c) 2015 Jared D. McNeill <jmcneill (at) 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.48 2025/10/04 01:38:02 thorpej 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 if (faa != NULL) { 345 node->n_faa = *faa; 346 } else { 347 memset(&node->n_faa, 0, sizeof(node->n_faa)); 348 } 349 node->n_faa.faa_phandle = child; 350 node->n_faa.faa_name = node->n_name; 351 node->n_faa.faa_bst = fdt_get_bus_tag(node); 352 node->n_faa.faa_dmat = fdt_get_dma_tag(node); 353 354 fdt_add_node(node); 355 fdt_need_rescan = true; 356 } 357 358 static int 359 fdt_scan_submatch(device_t parent, cfdata_t cf, const int *locs, void *aux) 360 { 361 if (locs[FDTCF_PASS] != FDTCF_PASS_DEFAULT && 362 locs[FDTCF_PASS] != cf->cf_loc[FDTCF_PASS]) 363 return 0; 364 365 return config_stdsubmatch(parent, cf, locs, aux); 366 } 367 368 static void 369 fdt_scan_best(struct fdt_softc *sc, struct fdt_node *node) 370 { 371 struct fdt_attach_args faa; 372 cfdata_t cf, best_cf; 373 int match, best_match, best_pass; 374 375 best_cf = NULL; 376 best_match = 0; 377 best_pass = FDTCF_PASS_DEFAULT; 378 379 for (int pass = 0; pass <= FDTCF_PASS_DEFAULT; pass++) { 380 const int locs[FDTCF_NLOCS] = { 381 [FDTCF_PASS] = pass 382 }; 383 fdt_init_attach_args(&sc->sc_faa, node, true, &faa); 384 cf = config_search(node->n_bus, &faa, 385 CFARGS(.submatch = fdt_scan_submatch, 386 .iattr = "fdt", 387 .locators = locs)); 388 if (cf == NULL) 389 continue; 390 match = config_match(node->n_bus, cf, &faa); 391 if (match > best_match) { 392 best_match = match; 393 best_cf = cf; 394 best_pass = pass; 395 } 396 } 397 398 node->n_cf = best_cf; 399 node->n_cfpass = best_pass; 400 } 401 402 static void 403 fdt_scan(struct fdt_softc *sc, int pass) 404 { 405 struct fdt_node *node; 406 struct fdt_attach_args faa; 407 const int locs[FDTCF_NLOCS] = { 408 [FDTCF_PASS] = pass 409 }; 410 bool quiet = pass != FDTCF_PASS_DEFAULT; 411 412 TAILQ_FOREACH(node, &fdt_nodes, n_nodes) { 413 if (node->n_cfpass != pass || node->n_dev != NULL) 414 continue; 415 416 fdt_init_attach_args(&sc->sc_faa, node, quiet, &faa); 417 418 if (quiet && node->n_cf == NULL) { 419 /* 420 * No match for this device, skip it. 421 */ 422 continue; 423 } 424 425 /* 426 * Attach the device. 427 */ 428 fdt_pre_attach(node); 429 430 devhandle_t nodeh = device_handle(node->n_bus); 431 432 if (quiet) { 433 node->n_dev = config_attach(node->n_bus, node->n_cf, 434 &faa, fdtbus_print, 435 CFARGS(.locators = locs, 436 .devhandle = 437 devhandle_from_of(nodeh, 438 node->n_phandle))); 439 } else { 440 /* 441 * Default pass. 442 */ 443 node->n_dev = config_found(node->n_bus, &faa, 444 fdtbus_print, 445 CFARGS(.submatch = fdt_scan_submatch, 446 .iattr = "fdt", 447 .locators = locs, 448 .devhandle = 449 devhandle_from_of(nodeh, 450 node->n_phandle))); 451 } 452 453 if (node->n_dev != NULL) 454 fdt_post_attach(node); 455 } 456 } 457 458 static void 459 fdt_pre_attach(struct fdt_node *node) 460 { 461 const char *cfgname; 462 int error; 463 464 node->n_pinctrl_init = fdtbus_pinctrl_has_config(node->n_phandle, "init"); 465 466 cfgname = node->n_pinctrl_init ? "init" : "default"; 467 468 aprint_debug_dev(node->n_bus, "set %s config for %s\n", cfgname, node->n_name); 469 470 error = fdtbus_pinctrl_set_config(node->n_phandle, cfgname); 471 if (error != 0 && error != ENOENT) 472 aprint_debug_dev(node->n_bus, 473 "failed to set %s config on %s: %d\n", 474 cfgname, node->n_name, error); 475 476 fdtbus_powerdomain_enable(node->n_phandle); 477 } 478 479 static void 480 fdt_post_attach(struct fdt_node *node) 481 { 482 int error; 483 484 if (node->n_pinctrl_init) { 485 aprint_debug_dev(node->n_bus, "set default config for %s\n", node->n_name); 486 error = fdtbus_pinctrl_set_config(node->n_phandle, "default"); 487 if (error != 0 && error != ENOENT) 488 aprint_debug_dev(node->n_bus, 489 "failed to set default config on %s: %d\n", 490 node->n_name, error); 491 } 492 } 493 494 static void 495 fdt_add_node(struct fdt_node *new_node) 496 { 497 struct fdt_node *node; 498 499 TAILQ_FOREACH(node, &fdt_nodes, n_nodes) 500 if (node->n_order > new_node->n_order) { 501 TAILQ_INSERT_BEFORE(node, new_node, n_nodes); 502 return; 503 } 504 TAILQ_INSERT_TAIL(&fdt_nodes, new_node, n_nodes); 505 } 506 507 void 508 fdt_remove_byhandle(int phandle) 509 { 510 struct fdt_node *node; 511 512 TAILQ_FOREACH(node, &fdt_nodes, n_nodes) { 513 if (node->n_phandle == phandle) { 514 TAILQ_REMOVE(&fdt_nodes, node, n_nodes); 515 return; 516 } 517 } 518 } 519 520 void 521 fdt_remove_bycompat(const char *compatible[]) 522 { 523 struct fdt_node *node, *next; 524 525 TAILQ_FOREACH_SAFE(node, &fdt_nodes, n_nodes, next) { 526 if (of_compatible(node->n_phandle, compatible)) { 527 TAILQ_REMOVE(&fdt_nodes, node, n_nodes); 528 } 529 } 530 } 531 532 int 533 fdt_find_with_property(const char *prop, int *pindex) 534 { 535 struct fdt_node *node; 536 int index = 0; 537 538 TAILQ_FOREACH(node, &fdt_nodes, n_nodes) { 539 if (index++ < *pindex) 540 continue; 541 if (of_hasprop(node->n_phandle, prop)) { 542 *pindex = index; 543 return node->n_phandle; 544 } 545 } 546 547 return -1; 548 } 549 550 static u_int 551 fdt_get_order(int phandle) 552 { 553 u_int val = UINT_MAX; 554 int child; 555 556 of_getprop_uint32(phandle, "phandle", &val); 557 558 for (child = OF_child(phandle); child; child = OF_peer(child)) { 559 u_int child_val = fdt_get_order(child); 560 if (child_val < val) 561 val = child_val; 562 } 563 564 return val; 565 } 566 567 int 568 fdtbus_print(void *aux, const char *pnp) 569 { 570 const struct fdt_attach_args * const faa = aux; 571 char buf[FDT_MAX_PATH]; 572 const char *name = buf; 573 int len; 574 575 if (pnp && faa->faa_quiet) 576 return QUIET; 577 578 /* Skip "not configured" for nodes w/o compatible property */ 579 if (pnp && OF_getproplen(faa->faa_phandle, "compatible") <= 0) 580 return QUIET; 581 582 if (!fdtbus_get_path(faa->faa_phandle, buf, sizeof(buf))) 583 name = faa->faa_name; 584 585 if (pnp) { 586 aprint_normal("%s at %s", name, pnp); 587 const char *compat = fdt_getprop(fdtbus_get_data(), 588 fdtbus_phandle2offset(faa->faa_phandle), "compatible", 589 &len); 590 while (len > 0) { 591 aprint_debug(" <%s>", compat); 592 len -= (strlen(compat) + 1); 593 compat += (strlen(compat) + 1); 594 } 595 } else 596 aprint_debug(" (%s)", name); 597 598 return UNCONF; 599 } 600