1 /* $NetBSD: Locore.c,v 1.7 2000/08/20 07:04:59 tsubai Exp $ */
4 * Copyright (C) 1995, 1996 Wolfgang Solfrank.
5 * Copyright (C) 1995, 1996 TooLs GmbH.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by TooLs GmbH.
19 * 4. The name of TooLs GmbH may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
28 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 * Copyright (C) 2000 Benno Rice.
35 * All rights reserved.
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
40 * 1. Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in the
44 * documentation and/or other materials provided with the distribution.
46 * THIS SOFTWARE IS PROVIDED BY Benno Rice ``AS IS'' AND ANY EXPRESS OR
47 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
48 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
49 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
50 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
51 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
52 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
53 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
54 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
55 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
58 #include <sys/cdefs.h>
59 __FBSDID("$FreeBSD$");
61 #include <machine/stdarg.h>
67 int (*openfirmware)(void *);
76 OF_init(int (*openfirm)(void *))
79 openfirmware = openfirm;
81 if ((chosen = OF_finddevice("/chosen")) == -1)
83 if (OF_getprop(chosen, "memory", &memory, sizeof(memory)) == -1)
85 if (OF_getprop(chosen, "mmu", &mmu, sizeof(mmu)) == -1)
93 /* Test to see if a service exists. */
109 args.service = (cell_t)name;
110 if (openfirmware(&args) == -1)
112 return (args.missing);
115 /* Return firmware millisecond count. */
125 (cell_t)"milliseconds",
135 * Device tree functions
138 /* Return the next sibling of this node or 0. */
140 OF_peer(phandle_t node)
155 if (openfirmware(&args) == -1)
160 /* Return the first child of this node or 0. */
162 OF_child(phandle_t node)
177 if (openfirmware(&args) == -1)
182 /* Return the parent of this node or 0. */
184 OF_parent(phandle_t node)
199 if (openfirmware(&args) == -1)
201 return (args.parent);
204 /* Return the package handle that corresponds to an instance handle. */
206 OF_instance_to_package(ihandle_t instance)
215 (cell_t)"instance-to-package",
220 args.instance = instance;
221 if (openfirmware(&args) == -1)
223 return (args.package);
226 /* Get the length of a property of a package. */
228 OF_getproplen(phandle_t package, char *propname)
238 (cell_t)"getproplen",
243 args.package = package;
244 args.propname = (cell_t)propname;
245 if (openfirmware(&args) == -1)
247 return (args.proplen);
250 /* Get the value of a property of a package. */
252 OF_getprop(phandle_t package, char *propname, void *buf, int buflen)
269 args.package = package;
270 args.propname = (cell_t)propname;
271 args.buf = (cell_t)buf;
272 args.buflen = buflen;
273 if (openfirmware(&args) == -1)
278 /* Get the next property of a package. */
280 OF_nextprop(phandle_t package, char *previous, char *buf)
296 args.package = package;
297 args.previous = (cell_t)previous;
298 args.buf = (cell_t)buf;
299 if (openfirmware(&args) == -1)
304 /* Set the value of a property of a package. */
305 /* XXX Has a bug on FirePower */
307 OF_setprop(phandle_t package, char *propname, void *buf, int len)
324 args.package = package;
325 args.propname = (cell_t)propname;
326 args.buf = (cell_t)buf;
328 if (openfirmware(&args) == -1)
333 /* Convert a device specifier to a fully qualified pathname. */
335 OF_canon(const char *device, char *buf, int len)
351 args.device = (cell_t)device;
352 args.buf = (cell_t)buf;
354 if (openfirmware(&args) == -1)
359 /* Return a package handle for the specified device. */
361 OF_finddevice(const char *device)
370 (cell_t)"finddevice",
375 args.device = (cell_t)device;
376 if (openfirmware(&args) == -1)
378 return (args.package);
381 /* Return the fully qualified pathname corresponding to an instance. */
383 OF_instance_to_path(ihandle_t instance, char *buf, int len)
394 (cell_t)"instance-to-path",
399 args.instance = instance;
400 args.buf = (cell_t)buf;
402 if (openfirmware(&args) == -1)
407 /* Return the fully qualified pathname corresponding to a package. */
409 OF_package_to_path(phandle_t package, char *buf, int len)
420 (cell_t)"package-to-path",
425 args.package = package;
426 args.buf = (cell_t)buf;
428 if (openfirmware(&args) == -1)
433 /* Call the method in the scope of a given instance. */
435 OF_call_method(char *method, ihandle_t instance, int nargs, int nreturns, ...)
444 cell_t args_n_results[12];
446 (cell_t)"call-method",
455 args.nargs = nargs + 2;
456 args.nreturns = nreturns + 1;
457 args.method = (cell_t)method;
458 args.instance = instance;
459 va_start(ap, nreturns);
460 for (cp = (cell_t *)(args.args_n_results + (n = nargs)); --n >= 0;)
461 *--cp = va_arg(ap, cell_t);
462 if (openfirmware(&args) == -1)
464 if (args.args_n_results[nargs])
465 return (args.args_n_results[nargs]);
466 for (cp = (cell_t *)(args.args_n_results + nargs + (n = args.nreturns));
468 *va_arg(ap, cell_t *) = *--cp;
474 * Device I/O functions
477 /* Open an instance for a device. */
479 OF_open(char *device)
493 args.device = (cell_t)device;
494 if (openfirmware(&args) == -1 || args.instance == 0) {
497 return (args.instance);
500 /* Close an instance. */
502 OF_close(ihandle_t instance)
514 args.instance = instance;
518 /* Read from an instance. */
520 OF_read(ihandle_t instance, void *addr, int len)
536 args.instance = instance;
537 args.addr = (cell_t)addr;
540 #if defined(OPENFIRM_DEBUG)
541 printf("OF_read: called with instance=%08x, addr=%p, len=%d\n",
542 args.instance, args.addr, args.len);
545 if (openfirmware(&args) == -1)
548 #if defined(OPENFIRM_DEBUG)
549 printf("OF_read: returning instance=%d, addr=%p, len=%d, actual=%d\n",
550 args.instance, args.addr, args.len, args.actual);
553 return (args.actual);
556 /* Write to an instance. */
558 OF_write(ihandle_t instance, void *addr, int len)
574 args.instance = instance;
575 args.addr = (cell_t)addr;
577 if (openfirmware(&args) == -1)
579 return (args.actual);
582 /* Seek to a position. */
584 OF_seek(ihandle_t instance, u_int64_t pos)
600 args.instance = instance;
601 args.poshi = pos >> 32;
603 if (openfirmware(&args) == -1)
605 return (args.status);
612 /* Claim an area of memory. */
614 OF_claim(void *virt, u_int size, u_int align)
630 args.virt = (cell_t)virt;
633 if (openfirmware(&args) == -1)
635 return ((void *)args.baseaddr);
638 /* Release an area of memory. */
640 OF_release(void *virt, u_int size)
653 args.virt = (cell_t)virt;
659 * Control transfer functions
662 /* Reset the system and call "boot <bootspec>". */
664 OF_boot(char *bootspec)
676 args.bootspec = (cell_t)bootspec;
678 for (;;) /* just in case */
682 /* Suspend and drop back to the Open Firmware interface. */
695 /* We may come back. */
698 /* Shut down and drop back to the Open Firmware interface. */
711 for (;;) /* just in case */
715 /* Free <size> bytes starting at <virt>, then call <entry> with <arg>. */
718 OF_chain(void *virt, u_int size, void (*entry)(), void *arg, u_int len)
734 args.virt = (cell_t)virt;
736 args.entry = (cell_t)entry;
737 args.arg = (cell_t)arg;
743 OF_chain(void *virt, u_int size, void (*entry)(), void *arg, u_int len)
746 * This is a REALLY dirty hack till the firmware gets this going
750 OF_release(virt, size);
752 entry(0, 0, openfirmware, arg, len);