1 /* $NetBSD: fdtbus.c,v 1.35 2020/09/20 11:25:36 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.35 2020/09/20 11:25:36 jmcneill 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 char * const fdtbus_compatible[] = 91 { "simple-bus", NULL }; 92 93 CFATTACH_DECL2_NEW(simplebus, sizeof(struct fdt_softc), 94 fdt_match, fdt_attach, NULL, NULL, fdt_rescan, fdt_childdet); 95 96 static int 97 fdt_match(device_t parent, cfdata_t cf, void *aux) 98 { 99 const struct fdt_attach_args *faa = aux; 100 const int phandle = faa->faa_phandle; 101 int match; 102 103 /* Check compatible string */ 104 match = of_match_compatible(phandle, fdtbus_compatible); 105 if (match) 106 return match; 107 108 /* Some nodes have no compatible string */ 109 if (!of_hasprop(phandle, "compatible")) { 110 if (OF_finddevice("/clocks") == phandle) 111 return 1; 112 if (OF_finddevice("/chosen") == phandle) 113 return 1; 114 } 115 116 /* Always match the root node */ 117 return OF_finddevice("/") == phandle; 118 } 119 120 static void 121 fdt_attach(device_t parent, device_t self, void *aux) 122 { 123 struct fdt_softc *sc = device_private(self); 124 const struct fdt_attach_args *faa = aux; 125 const int phandle = faa->faa_phandle; 126 const char *descr, *model; 127 128 sc->sc_dev = self; 129 sc->sc_phandle = phandle; 130 sc->sc_faa = *faa; 131 132 aprint_naive("\n"); 133 134 descr = fdtbus_get_string(phandle, "model"); 135 if (descr) 136 aprint_normal(": %s\n", descr); 137 else 138 aprint_normal("\n"); 139 140 /* Find all child nodes */ 141 fdt_add_bus(self, phandle, &sc->sc_faa); 142 143 /* Only the root bus should scan for devices */ 144 if (OF_finddevice("/") != faa->faa_phandle) 145 return; 146 147 /* Set hw.model if available */ 148 model = fdtbus_get_string(phandle, "compatible"); 149 if (model) 150 cpu_setmodel("%s", model); 151 else if (descr) 152 cpu_setmodel("%s", descr); 153 154 /* Scan devices */ 155 fdt_rescan(self, NULL, NULL); 156 } 157 158 static int 159 fdt_rescan(device_t self, const char *ifattr, const int *locs) 160 { 161 struct fdt_softc *sc = device_private(self); 162 struct fdt_node *node; 163 int pass; 164 165 TAILQ_FOREACH(node, &fdt_nodes, n_nodes) 166 fdt_scan_best(sc, node); 167 168 pass = 0; 169 fdt_need_rescan = false; 170 do { 171 fdt_scan(sc, pass); 172 if (fdt_need_rescan == true) { 173 pass = 0; 174 TAILQ_FOREACH(node, &fdt_nodes, n_nodes) { 175 if (node->n_cfpass == -1) 176 fdt_scan_best(sc, node); 177 } 178 fdt_need_rescan = false; 179 } else { 180 pass++; 181 } 182 } while (pass <= FDTCF_PASS_DEFAULT); 183 184 return 0; 185 } 186 187 static void 188 fdt_childdet(device_t parent, device_t child) 189 { 190 struct fdt_node *node; 191 192 TAILQ_FOREACH(node, &fdt_nodes, n_nodes) 193 if (node->n_dev == child) { 194 node->n_dev = NULL; 195 break; 196 } 197 } 198 199 static void 200 fdt_init_attach_args(const struct fdt_attach_args *faa_tmpl, struct fdt_node *node, 201 bool quiet, struct fdt_attach_args *faa) 202 { 203 *faa = *faa_tmpl; 204 faa->faa_phandle = node->n_phandle; 205 faa->faa_name = node->n_name; 206 faa->faa_quiet = quiet; 207 faa->faa_dmat = node->n_faa.faa_dmat; 208 } 209 210 static bool 211 fdt_add_bus_stdmatch(void *arg, int child) 212 { 213 return fdtbus_status_okay(child); 214 } 215 216 void 217 fdt_add_bus(device_t bus, const int phandle, struct fdt_attach_args *faa) 218 { 219 fdt_add_bus_match(bus, phandle, faa, fdt_add_bus_stdmatch, NULL); 220 } 221 222 void 223 fdt_add_bus_match(device_t bus, const int phandle, struct fdt_attach_args *faa, 224 bool (*fn)(void *, int), void *fnarg) 225 { 226 int child; 227 228 for (child = OF_child(phandle); child; child = OF_peer(child)) { 229 if (fn && !fn(fnarg, child)) 230 continue; 231 232 fdt_add_child(bus, child, faa, fdt_get_order(child)); 233 } 234 } 235 236 static int 237 fdt_dma_translate(int phandle, struct fdt_dma_range **ranges, u_int *nranges) 238 { 239 const uint8_t *data; 240 int len, n; 241 242 const int parent = OF_parent(phandle); 243 if (parent == -1) 244 return 1; /* done searching */ 245 246 data = fdtbus_get_prop(phandle, "dma-ranges", &len); 247 if (data == NULL) 248 return 1; /* no dma-ranges property, stop searching */ 249 250 if (len == 0) 251 return 0; /* dma-ranges property is empty, keep going */ 252 253 const int addr_cells = fdtbus_get_addr_cells(phandle); 254 const int size_cells = fdtbus_get_size_cells(phandle); 255 const int paddr_cells = fdtbus_get_addr_cells(parent); 256 if (addr_cells == -1 || size_cells == -1 || paddr_cells == -1) 257 return 1; 258 259 const int entry_size = (addr_cells + paddr_cells + size_cells) * 4; 260 261 *nranges = len / entry_size; 262 *ranges = kmem_alloc(sizeof(struct fdt_dma_range) * *nranges, KM_SLEEP); 263 for (n = 0; len >= entry_size; n++, len -= entry_size) { 264 const uint64_t cba = fdtbus_get_cells(data, addr_cells); 265 data += addr_cells * 4; 266 const uint64_t pba = fdtbus_get_cells(data, paddr_cells); 267 data += paddr_cells * 4; 268 const uint64_t cl = fdtbus_get_cells(data, size_cells); 269 data += size_cells * 4; 270 271 (*ranges)[n].dr_sysbase = pba; 272 (*ranges)[n].dr_busbase = cba; 273 (*ranges)[n].dr_len = cl; 274 } 275 276 return 1; 277 } 278 279 static bus_dma_tag_t 280 fdt_get_dma_tag(struct fdt_node *node) 281 { 282 struct fdt_dma_range *ranges = NULL; 283 u_int nranges = 0; 284 int parent; 285 286 parent = OF_parent(node->n_phandle); 287 while (parent != -1) { 288 if (fdt_dma_translate(parent, &ranges, &nranges) != 0) 289 break; 290 parent = OF_parent(parent); 291 } 292 293 return fdtbus_dma_tag_create(node->n_phandle, ranges, nranges); 294 } 295 296 void 297 fdt_add_child(device_t bus, const int child, struct fdt_attach_args *faa, 298 u_int order) 299 { 300 struct fdt_node *node; 301 302 /* Add the node to our device list */ 303 node = kmem_zalloc(sizeof(*node), KM_SLEEP); 304 node->n_bus = bus; 305 node->n_dev = NULL; 306 node->n_phandle = child; 307 node->n_name = fdtbus_get_string(child, "name"); 308 node->n_cfpass = -1; 309 node->n_cf = NULL; 310 node->n_order = order; 311 node->n_faa = *faa; 312 node->n_faa.faa_phandle = child; 313 node->n_faa.faa_name = node->n_name; 314 node->n_faa.faa_dmat = fdt_get_dma_tag(node); 315 316 fdt_add_node(node); 317 fdt_need_rescan = true; 318 } 319 320 static int 321 fdt_scan_submatch(device_t parent, cfdata_t cf, const int *locs, void *aux) 322 { 323 if (locs[FDTCF_PASS] != FDTCF_PASS_DEFAULT && 324 locs[FDTCF_PASS] != cf->cf_loc[FDTCF_PASS]) 325 return 0; 326 327 return config_stdsubmatch(parent, cf, locs, aux); 328 } 329 330 static void 331 fdt_scan_best(struct fdt_softc *sc, struct fdt_node *node) 332 { 333 struct fdt_attach_args faa; 334 cfdata_t cf, best_cf; 335 int match, best_match, best_pass; 336 337 best_cf = NULL; 338 best_match = 0; 339 best_pass = FDTCF_PASS_DEFAULT; 340 341 for (int pass = 0; pass <= FDTCF_PASS_DEFAULT; pass++) { 342 const int locs[FDTCF_NLOCS] = { 343 [FDTCF_PASS] = pass 344 }; 345 fdt_init_attach_args(&sc->sc_faa, node, true, &faa); 346 cf = config_search_loc(fdt_scan_submatch, node->n_bus, "fdt", locs, &faa); 347 if (cf == NULL) 348 continue; 349 match = config_match(node->n_bus, cf, &faa); 350 if (match > best_match) { 351 best_match = match; 352 best_cf = cf; 353 best_pass = pass; 354 } 355 } 356 357 node->n_cf = best_cf; 358 node->n_cfpass = best_pass; 359 } 360 361 static void 362 fdt_scan(struct fdt_softc *sc, int pass) 363 { 364 struct fdt_node *node; 365 struct fdt_attach_args faa; 366 const int locs[FDTCF_NLOCS] = { 367 [FDTCF_PASS] = pass 368 }; 369 bool quiet = pass != FDTCF_PASS_DEFAULT; 370 371 TAILQ_FOREACH(node, &fdt_nodes, n_nodes) { 372 if (node->n_cfpass != pass || node->n_dev != NULL) 373 continue; 374 375 fdt_init_attach_args(&sc->sc_faa, node, quiet, &faa); 376 377 if (quiet && node->n_cf == NULL) { 378 /* 379 * No match for this device, skip it. 380 */ 381 continue; 382 } 383 384 /* 385 * Attach the device. 386 */ 387 fdt_pre_attach(node); 388 389 if (quiet) { 390 node->n_dev = config_attach_loc(node->n_bus, node->n_cf, locs, 391 &faa, fdtbus_print); 392 } else { 393 /* 394 * Default pass. 395 */ 396 node->n_dev = config_found_sm_loc(node->n_bus, "fdt", locs, 397 &faa, fdtbus_print, fdt_scan_submatch); 398 } 399 400 if (node->n_dev != NULL) 401 fdt_post_attach(node); 402 } 403 } 404 405 static void 406 fdt_pre_attach(struct fdt_node *node) 407 { 408 const char *cfgname; 409 int error; 410 411 node->n_pinctrl_init = fdtbus_pinctrl_has_config(node->n_phandle, "init"); 412 413 cfgname = node->n_pinctrl_init ? "init" : "default"; 414 415 aprint_debug_dev(node->n_bus, "set %s config for %s\n", cfgname, node->n_name); 416 417 error = fdtbus_pinctrl_set_config(node->n_phandle, cfgname); 418 if (error != 0 && error != ENOENT) 419 aprint_debug_dev(node->n_bus, 420 "failed to set %s config on %s: %d\n", 421 cfgname, node->n_name, error); 422 } 423 424 static void 425 fdt_post_attach(struct fdt_node *node) 426 { 427 char buf[FDT_MAX_PATH]; 428 prop_dictionary_t dict; 429 int error; 430 431 dict = device_properties(node->n_dev); 432 if (fdtbus_get_path(node->n_phandle, buf, sizeof(buf))) 433 prop_dictionary_set_string(dict, "fdt-path", buf); 434 435 if (node->n_pinctrl_init) { 436 aprint_debug_dev(node->n_bus, "set default config for %s\n", node->n_name); 437 error = fdtbus_pinctrl_set_config(node->n_phandle, "default"); 438 if (error != 0 && error != ENOENT) 439 aprint_debug_dev(node->n_bus, 440 "failed to set default config on %s: %d\n", 441 node->n_name, error); 442 } 443 } 444 445 static void 446 fdt_add_node(struct fdt_node *new_node) 447 { 448 struct fdt_node *node; 449 450 TAILQ_FOREACH(node, &fdt_nodes, n_nodes) 451 if (node->n_order > new_node->n_order) { 452 TAILQ_INSERT_BEFORE(node, new_node, n_nodes); 453 return; 454 } 455 TAILQ_INSERT_TAIL(&fdt_nodes, new_node, n_nodes); 456 } 457 458 void 459 fdt_remove_byhandle(int phandle) 460 { 461 struct fdt_node *node; 462 463 TAILQ_FOREACH(node, &fdt_nodes, n_nodes) { 464 if (node->n_phandle == phandle) { 465 TAILQ_REMOVE(&fdt_nodes, node, n_nodes); 466 return; 467 } 468 } 469 } 470 471 void 472 fdt_remove_bycompat(const char *compatible[]) 473 { 474 struct fdt_node *node, *next; 475 476 TAILQ_FOREACH_SAFE(node, &fdt_nodes, n_nodes, next) { 477 if (of_match_compatible(node->n_phandle, compatible)) { 478 TAILQ_REMOVE(&fdt_nodes, node, n_nodes); 479 } 480 } 481 } 482 483 int 484 fdt_find_with_property(const char *prop, int *pindex) 485 { 486 struct fdt_node *node; 487 int index = 0; 488 489 TAILQ_FOREACH(node, &fdt_nodes, n_nodes) { 490 if (index++ < *pindex) 491 continue; 492 if (of_hasprop(node->n_phandle, prop)) { 493 *pindex = index; 494 return node->n_phandle; 495 } 496 } 497 498 return -1; 499 } 500 501 static u_int 502 fdt_get_order(int phandle) 503 { 504 u_int val = UINT_MAX; 505 int child; 506 507 of_getprop_uint32(phandle, "phandle", &val); 508 509 for (child = OF_child(phandle); child; child = OF_peer(child)) { 510 u_int child_val = fdt_get_order(child); 511 if (child_val < val) 512 val = child_val; 513 } 514 515 return val; 516 } 517 518 int 519 fdtbus_print(void *aux, const char *pnp) 520 { 521 const struct fdt_attach_args * const faa = aux; 522 char buf[FDT_MAX_PATH]; 523 const char *name = buf; 524 int len; 525 526 if (pnp && faa->faa_quiet) 527 return QUIET; 528 529 /* Skip "not configured" for nodes w/o compatible property */ 530 if (pnp && OF_getproplen(faa->faa_phandle, "compatible") <= 0) 531 return QUIET; 532 533 if (!fdtbus_get_path(faa->faa_phandle, buf, sizeof(buf))) 534 name = faa->faa_name; 535 536 if (pnp) { 537 aprint_normal("%s at %s", name, pnp); 538 const char *compat = fdt_getprop(fdtbus_get_data(), 539 fdtbus_phandle2offset(faa->faa_phandle), "compatible", 540 &len); 541 while (len > 0) { 542 aprint_debug(" <%s>", compat); 543 len -= (strlen(compat) + 1); 544 compat += (strlen(compat) + 1); 545 } 546 } else 547 aprint_debug(" (%s)", name); 548 549 return UNCONF; 550 } 551