ofwbus.c revision 178367
1/*- 2 * Copyright 1998 Massachusetts Institute of Technology 3 * 4 * Permission to use, copy, modify, and distribute this software and 5 * its documentation for any purpose and without fee is hereby 6 * granted, provided that both the above copyright notice and this 7 * permission notice appear in all copies, that both the above 8 * copyright notice and this permission notice appear in all 9 * supporting documentation, and that the name of M.I.T. not be used 10 * in advertising or publicity pertaining to distribution of the 11 * software without specific, written prior permission. M.I.T. makes 12 * no representations about the suitability of this software for any 13 * purpose. It is provided "as is" without express or implied 14 * warranty. 15 * 16 * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''. M.I.T. DISCLAIMS 17 * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE, 18 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT 20 * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 23 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 25 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 26 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29/*- 30 * Copyright 2001 by Thomas Moestl <tmm@FreeBSD.org>. All rights reserved. 31 * 32 * Redistribution and use in source and binary forms, with or without 33 * modification, are permitted provided that the following conditions 34 * are met: 35 * 1. Redistributions of source code must retain the above copyright 36 * notice, this list of conditions and the following disclaimer. 37 * 2. Redistributions in binary form must reproduce the above copyright 38 * notice, this list of conditions and the following disclaimer in the 39 * documentation and/or other materials provided with the distribution. 40 * 41 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 51 * SUCH DAMAGE. 52 * 53 * from: FreeBSD: src/sys/i386/i386/nexus.c,v 1.43 2001/02/09 54 * 55 * $FreeBSD: head/sys/powerpc/aim/nexus.c 178367 2008-04-21 04:41:37Z marcel $ 56 */ 57#include "opt_psim.h" 58 59#include <sys/param.h> 60#include <sys/systm.h> 61#include <sys/module.h> 62#include <sys/bus.h> 63#include <sys/clock.h> 64#include <sys/cons.h> 65#include <sys/kernel.h> 66#include <sys/malloc.h> 67 68#include <dev/ofw/openfirm.h> 69 70#include <machine/bus.h> 71#include <machine/frame.h> 72#include <machine/intr_machdep.h> 73#include <machine/nexusvar.h> 74#include <machine/resource.h> 75 76#include <sys/rman.h> 77 78#include "clock_if.h" 79#include "ofw_bus_if.h" 80#include "pic_if.h" 81 82/* 83 * The nexus (which is a pseudo-bus actually) iterates over the nodes that 84 * exist in Open Firmware and adds them as devices to this bus so that drivers 85 * can be attached to them. 86 * 87 * Maybe this code should get into dev/ofw to some extent, as some of it should 88 * work for all Open Firmware based machines... 89 */ 90 91static MALLOC_DEFINE(M_NEXUS, "nexus", "nexus device information"); 92 93struct nexus_devinfo { 94 phandle_t ndi_node; 95 /* Some common properties. */ 96 const char *ndi_name; 97 const char *ndi_device_type; 98 const char *ndi_compatible; 99}; 100 101struct nexus_softc { 102 struct rman sc_rman; 103}; 104 105/* 106 * Device interface 107 */ 108static int nexus_probe(device_t); 109static int nexus_attach(device_t); 110 111/* 112 * Bus interface 113 */ 114static device_t nexus_add_child(device_t, int, const char *, int); 115static void nexus_probe_nomatch(device_t, device_t); 116static int nexus_read_ivar(device_t, device_t, int, uintptr_t *); 117static int nexus_write_ivar(device_t, device_t, int, uintptr_t); 118static int nexus_setup_intr(device_t, device_t, struct resource *, int, 119 driver_filter_t *, driver_intr_t *, void *, void **); 120static int nexus_teardown_intr(device_t, device_t, struct resource *, 121 void *); 122static struct resource *nexus_alloc_resource(device_t, device_t, int, int *, 123 u_long, u_long, u_long, u_int); 124static int nexus_activate_resource(device_t, device_t, int, int, 125 struct resource *); 126static int nexus_deactivate_resource(device_t, device_t, int, int, 127 struct resource *); 128static int nexus_release_resource(device_t, device_t, int, int, 129 struct resource *); 130 131/* 132 * OFW bus interface. 133 */ 134static phandle_t nexus_ofw_get_node(device_t, device_t); 135static const char *nexus_ofw_get_name(device_t, device_t); 136static const char *nexus_ofw_get_type(device_t, device_t); 137static const char *nexus_ofw_get_compat(device_t, device_t); 138 139/* 140 * Clock interface. 141 */ 142static int nexus_gettime(device_t, struct timespec *); 143static int nexus_settime(device_t, struct timespec *); 144 145/* 146 * Local routines 147 */ 148static device_t nexus_device_from_node(device_t, phandle_t); 149 150static device_method_t nexus_methods[] = { 151 /* Device interface */ 152 DEVMETHOD(device_probe, nexus_probe), 153 DEVMETHOD(device_attach, nexus_attach), 154 DEVMETHOD(device_detach, bus_generic_detach), 155 DEVMETHOD(device_shutdown, bus_generic_shutdown), 156 DEVMETHOD(device_suspend, bus_generic_suspend), 157 DEVMETHOD(device_resume, bus_generic_resume), 158 159 /* Bus interface. Resource management is business of the children... */ 160 DEVMETHOD(bus_add_child, nexus_add_child), 161 DEVMETHOD(bus_print_child, bus_generic_print_child), 162 DEVMETHOD(bus_probe_nomatch, nexus_probe_nomatch), 163 DEVMETHOD(bus_read_ivar, nexus_read_ivar), 164 DEVMETHOD(bus_write_ivar, nexus_write_ivar), 165 DEVMETHOD(bus_setup_intr, nexus_setup_intr), 166 DEVMETHOD(bus_teardown_intr, nexus_teardown_intr), 167 DEVMETHOD(bus_alloc_resource, nexus_alloc_resource), 168 DEVMETHOD(bus_activate_resource, nexus_activate_resource), 169 DEVMETHOD(bus_deactivate_resource, nexus_deactivate_resource), 170 DEVMETHOD(bus_release_resource, nexus_release_resource), 171 172 /* OFW bus interface */ 173 DEVMETHOD(ofw_bus_get_node, nexus_ofw_get_node), 174 DEVMETHOD(ofw_bus_get_name, nexus_ofw_get_name), 175 DEVMETHOD(ofw_bus_get_type, nexus_ofw_get_type), 176 DEVMETHOD(ofw_bus_get_compat, nexus_ofw_get_compat), 177 178 /* Clock interface */ 179 DEVMETHOD(clock_gettime, nexus_gettime), 180 DEVMETHOD(clock_settime, nexus_settime), 181 182 { 0, 0 } 183}; 184 185static driver_t nexus_driver = { 186 "nexus", 187 nexus_methods, 188 sizeof(struct nexus_softc), 189}; 190 191static devclass_t nexus_devclass; 192 193DRIVER_MODULE(nexus, root, nexus_driver, nexus_devclass, 0, 0); 194 195static int 196nexus_probe(device_t dev) 197{ 198 bus_generic_probe(dev); 199 device_set_desc(dev, "Open Firmware Nexus device"); 200 return (0); 201} 202 203static int 204nexus_attach(device_t dev) 205{ 206 phandle_t root; 207 phandle_t child; 208 struct nexus_softc *sc; 209 u_long start, end; 210 211 if ((root = OF_peer(0)) == -1) 212 panic("nexus_probe: OF_peer failed."); 213 214 sc = device_get_softc(dev); 215 216 start = 0; 217 end = INTR_VECTORS - 1; 218 219 sc->sc_rman.rm_start = start; 220 sc->sc_rman.rm_end = end; 221 sc->sc_rman.rm_type = RMAN_ARRAY; 222 sc->sc_rman.rm_descr = "Interrupt request lines"; 223 if (rman_init(&sc->sc_rman) || 224 rman_manage_region(&sc->sc_rman, start, end)) 225 panic("nexus_probe IRQ rman"); 226 227 /* 228 * Now walk the OFW tree to locate top-level devices 229 */ 230 for (child = OF_child(root); child != 0; child = OF_peer(child)) { 231 if (child == -1) 232 panic("nexus_probe(): OF_child failed."); 233 (void)nexus_device_from_node(dev, child); 234 235 } 236 237 clock_register(dev, 1000); 238 return (bus_generic_attach(dev)); 239} 240 241static void 242nexus_probe_nomatch(device_t dev, device_t child) 243{ 244 char *name, *type; 245 246 if (BUS_READ_IVAR(dev, child, NEXUS_IVAR_NAME, 247 (uintptr_t *)&name) != 0 || 248 BUS_READ_IVAR(dev, child, NEXUS_IVAR_DEVICE_TYPE, 249 (uintptr_t *)&type) != 0) 250 return; 251 252 if (type == NULL) 253 type = "(unknown)"; 254 255 if (bootverbose) 256 device_printf(dev, "<%s>, type %s (no driver attached)\n", 257 name, type); 258} 259 260static device_t 261nexus_add_child(device_t dev, int order, const char *name, int unit) 262{ 263 device_t child; 264 struct nexus_devinfo *dinfo; 265 266 child = device_add_child_ordered(dev, order, name, unit); 267 if (child == NULL) 268 return (NULL); 269 270 dinfo = malloc(sizeof(struct nexus_devinfo), M_NEXUS, M_NOWAIT|M_ZERO); 271 if (dinfo == NULL) 272 return (NULL); 273 274 dinfo->ndi_node = -1; 275 dinfo->ndi_name = name; 276 device_set_ivars(child, dinfo); 277 278 return (child); 279} 280 281 282static int 283nexus_read_ivar(device_t dev, device_t child, int which, uintptr_t *result) 284{ 285 struct nexus_devinfo *dinfo; 286 287 if ((dinfo = device_get_ivars(child)) == 0) 288 return (ENOENT); 289 switch (which) { 290 case NEXUS_IVAR_NODE: 291 *result = dinfo->ndi_node; 292 break; 293 case NEXUS_IVAR_NAME: 294 *result = (uintptr_t)dinfo->ndi_name; 295 break; 296 case NEXUS_IVAR_DEVICE_TYPE: 297 *result = (uintptr_t)dinfo->ndi_device_type; 298 break; 299 case NEXUS_IVAR_COMPATIBLE: 300 *result = (uintptr_t)dinfo->ndi_compatible; 301 break; 302 default: 303 return (ENOENT); 304 } 305 return 0; 306} 307 308static int 309nexus_write_ivar(device_t dev, device_t child, int which, uintptr_t value) 310{ 311 struct nexus_devinfo *dinfo; 312 313 if ((dinfo = device_get_ivars(child)) == 0) 314 return (ENOENT); 315 316 switch (which) { 317 case NEXUS_IVAR_NAME: 318 return (EINVAL); 319 320 /* Identified devices may want to set these */ 321 case NEXUS_IVAR_NODE: 322 dinfo->ndi_node = (phandle_t)value; 323 break; 324 325 case NEXUS_IVAR_DEVICE_TYPE: 326 dinfo->ndi_device_type = (char *)value; 327 break; 328 329 default: 330 return (ENOENT); 331 } 332 return 0; 333} 334 335static int 336nexus_setup_intr(device_t dev, device_t child, struct resource *res, int flags, 337 driver_filter_t *filter, driver_intr_t *ihand, void *arg, void **cookiep) 338{ 339 driver_t *driver; 340 int error; 341 342 /* somebody tried to setup an irq that failed to allocate! */ 343 if (res == NULL) 344 panic("nexus_setup_intr: NULL irq resource!"); 345 346 *cookiep = 0; 347 if ((rman_get_flags(res) & RF_SHAREABLE) == 0) 348 flags |= INTR_EXCL; 349 350 driver = device_get_driver(child); 351 352 /* 353 * We depend here on rman_activate_resource() being idempotent. 354 */ 355 error = rman_activate_resource(res); 356 if (error) 357 return (error); 358 359 error = powerpc_setup_intr(device_get_nameunit(child), 360 rman_get_start(res), filter, ihand, arg, flags, cookiep); 361 362 return (error); 363} 364 365static int 366nexus_teardown_intr(device_t dev, device_t child, struct resource *res, 367 void *cookie) 368{ 369 370 return (powerpc_teardown_intr(cookie)); 371} 372 373/* 374 * Allocate resources at the behest of a child. This only handles interrupts, 375 * since I/O resources are handled by child busses. 376 */ 377static struct resource * 378nexus_alloc_resource(device_t bus, device_t child, int type, int *rid, 379 u_long start, u_long end, u_long count, u_int flags) 380{ 381 struct nexus_softc *sc; 382 struct resource *rv; 383 384 if (type != SYS_RES_IRQ) { 385 device_printf(bus, "unknown resource request from %s\n", 386 device_get_nameunit(child)); 387 return (NULL); 388 } 389 390 if (count == 0 || start + count - 1 != end) { 391 device_printf(bus, "invalid IRQ allocation from %s\n", 392 device_get_nameunit(child)); 393 return (NULL); 394 } 395 396 sc = device_get_softc(bus); 397 398 rv = rman_reserve_resource(&sc->sc_rman, start, end, count, 399 flags, child); 400 if (rv == NULL) { 401 device_printf(bus, "IRQ allocation failed for %s\n", 402 device_get_nameunit(child)); 403 } else 404 rman_set_rid(rv, *rid); 405 406 return (rv); 407} 408 409static int 410nexus_activate_resource(device_t bus, device_t child, int type, int rid, 411 struct resource *res) 412{ 413 414 /* Not much to be done yet... */ 415 return (rman_activate_resource(res)); 416} 417 418static int 419nexus_deactivate_resource(device_t bus, device_t child, int type, int rid, 420 struct resource *res) 421{ 422 423 /* Not much to be done yet... */ 424 return (rman_deactivate_resource(res)); 425} 426 427static int 428nexus_release_resource(device_t bus, device_t child, int type, int rid, 429 struct resource *res) 430{ 431 432 if (type != SYS_RES_IRQ) { 433 device_printf(bus, "unknown resource request from %s\n", 434 device_get_nameunit(child)); 435 return (EINVAL); 436 } 437 438 return (rman_release_resource(res)); 439} 440 441static device_t 442nexus_device_from_node(device_t parent, phandle_t node) 443{ 444 device_t cdev; 445 struct nexus_devinfo *dinfo; 446 char *name, *type, *compatible; 447 448 OF_getprop_alloc(node, "name", 1, (void **)&name); 449 OF_getprop_alloc(node, "device_type", 1, (void **)&type); 450 OF_getprop_alloc(node, "compatible", 1, (void **)&compatible); 451 cdev = device_add_child(parent, NULL, -1); 452 if (cdev != NULL) { 453 dinfo = malloc(sizeof(*dinfo), M_NEXUS, M_WAITOK); 454 dinfo->ndi_node = node; 455 dinfo->ndi_name = name; 456 dinfo->ndi_device_type = type; 457 dinfo->ndi_compatible = compatible; 458 device_set_ivars(cdev, dinfo); 459 } else 460 free(name, M_OFWPROP); 461 462 return (cdev); 463} 464 465static const char * 466nexus_ofw_get_name(device_t bus, device_t dev) 467{ 468 struct nexus_devinfo *dinfo; 469 470 if ((dinfo = device_get_ivars(dev)) == NULL) 471 return (NULL); 472 473 return (dinfo->ndi_name); 474} 475 476static phandle_t 477nexus_ofw_get_node(device_t bus, device_t dev) 478{ 479 struct nexus_devinfo *dinfo; 480 481 if ((dinfo = device_get_ivars(dev)) == NULL) 482 return (0); 483 484 return (dinfo->ndi_node); 485} 486 487static const char * 488nexus_ofw_get_type(device_t bus, device_t dev) 489{ 490 struct nexus_devinfo *dinfo; 491 492 if ((dinfo = device_get_ivars(dev)) == NULL) 493 return (NULL); 494 495 return (dinfo->ndi_device_type); 496} 497 498static const char * 499nexus_ofw_get_compat(device_t bus, device_t dev) 500{ 501 struct nexus_devinfo *dinfo; 502 503 if ((dinfo = device_get_ivars(dev)) == NULL) 504 return (NULL); 505 506 return (dinfo->ndi_compatible); 507} 508 509#define DIFF19041970 2082844800 510 511static int 512nexus_gettime(device_t dev, struct timespec *ts) 513{ 514 char path[128]; 515 ihandle_t ih; 516 phandle_t ph; 517 u_int rtc; 518 519 ph = OF_finddevice("rtc"); 520 if (ph == -1) 521 return (ENOENT); 522 523 OF_package_to_path(ph, path, sizeof(path)); 524 ih = OF_open(path); 525 if (ih == -1) 526 return (ENXIO); 527 528 if (OF_call_method("read-rtc", ih, 0, 1, &rtc)) 529 return (EIO); 530 531 ts->tv_sec = rtc - DIFF19041970; 532 ts->tv_nsec = 0; 533 return (0); 534} 535 536static int 537nexus_settime(device_t dev, struct timespec *ts) 538{ 539 char path[128]; 540 ihandle_t ih; 541 phandle_t ph; 542 u_int rtc; 543 544 ph = OF_finddevice("rtc"); 545 if (ph == -1) 546 return (ENOENT); 547 548 OF_package_to_path(ph, path, sizeof(path)); 549 ih = OF_open(path); 550 if (ih == -1) 551 return (ENXIO); 552 553 rtc = ts->tv_sec + DIFF19041970; 554 return ((OF_call_method("write-rtc", ih, 1, 0, rtc) != 0) ? EIO : 0); 555} 556