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 <sys/param.h>
62 #include <sys/kernel.h>
63 #include <sys/malloc.h>
64 #include <sys/systm.h>
66 #include <machine/stdarg.h>
68 #include <dev/ofw/openfirm.h>
70 MALLOC_DEFINE(M_OFWPROP, "openfirm", "Open Firmware properties");
72 static ihandle_t stdout;
77 OF_init(int (*openfirm)(void *))
81 set_openfirm_callback(openfirm);
82 if ((chosen = OF_finddevice("/chosen")) == -1)
84 if (OF_getprop(chosen, "stdout", &stdout, sizeof(stdout)) == -1)
89 OF_printf(const char *fmt, ...)
95 vsprintf(buf, fmt, va);
96 OF_write(stdout, buf, strlen(buf));
104 /* Test to see if a service exists. */
120 args.service = (cell_t)name;
121 if (openfirmware(&args) == -1)
123 return (args.missing);
127 OF_interpret(char *cmd, int nreturns, ...)
142 va_start(ap, nreturns);
143 args.nreturns = ++nreturns;
144 args.slot[i++] = (cell_t)cmd;
146 args.slot[i++] = va_arg(ap, cell_t);
147 if (openfirmware(&args) == -1) {
151 status = args.slot[i++];
152 while (i < 1 + nreturns)
153 *va_arg(ap, cell_t *) = args.slot[i++];
158 /* Return firmware millisecond count. */
168 (cell_t)"milliseconds",
178 * Device tree functions
181 /* Return the next sibling of this node or 0. */
183 OF_peer(phandle_t node)
198 if (openfirmware(&args) == -1)
203 /* Return the first child of this node or 0. */
205 OF_child(phandle_t node)
220 if (openfirmware(&args) == -1)
225 /* Return the parent of this node or 0. */
227 OF_parent(phandle_t node)
242 if (openfirmware(&args) == -1)
244 return (args.parent);
247 /* Return the package handle that corresponds to an instance handle. */
249 OF_instance_to_package(ihandle_t instance)
258 (cell_t)"instance-to-package",
263 args.instance = instance;
264 if (openfirmware(&args) == -1)
266 return (args.package);
269 /* Get the length of a property of a package. */
271 OF_getproplen(phandle_t package, char *propname)
281 (cell_t)"getproplen",
286 args.package = package;
287 args.propname = (cell_t)propname;
288 if (openfirmware(&args) == -1)
290 return (args.proplen);
293 /* Get the value of a property of a package. */
295 OF_getprop(phandle_t package, char *propname, void *buf, int buflen)
312 args.package = package;
313 args.propname = (cell_t)propname;
314 args.buf = (cell_t)buf;
315 args.buflen = buflen;
316 if (openfirmware(&args) == -1)
322 * Store the value of a property of a package into newly allocated memory
323 * (using the M_OFWPROP malloc pool and M_WAITOK). elsz is the size of a
324 * single element, the number of elements is return in number.
327 OF_getprop_alloc(phandle_t package, char *propname, int elsz, void **buf)
332 if ((len = OF_getproplen(package, propname)) == -1 ||
336 *buf = malloc(len, M_OFWPROP, M_WAITOK);
337 if (OF_getprop(package, propname, *buf, len) == -1) {
338 free(*buf, M_OFWPROP);
345 /* Get the next property of a package. */
347 OF_nextprop(phandle_t package, char *previous, char *buf)
363 args.package = package;
364 args.previous = (cell_t)previous;
365 args.buf = (cell_t)buf;
366 if (openfirmware(&args) == -1)
371 /* Set the value of a property of a package. */
372 /* XXX Has a bug on FirePower */
374 OF_setprop(phandle_t package, char *propname, void *buf, int len)
391 args.package = package;
392 args.propname = (cell_t)propname;
393 args.buf = (cell_t)buf;
395 if (openfirmware(&args) == -1)
400 /* Convert a device specifier to a fully qualified pathname. */
402 OF_canon(const char *device, char *buf, int len)
418 args.device = (cell_t)device;
419 args.buf = (cell_t)buf;
421 if (openfirmware(&args) == -1)
426 /* Return a package handle for the specified device. */
428 OF_finddevice(const char *device)
437 (cell_t)"finddevice",
442 args.device = (cell_t)device;
443 if (openfirmware(&args) == -1)
445 return (args.package);
448 /* Return the fully qualified pathname corresponding to an instance. */
450 OF_instance_to_path(ihandle_t instance, char *buf, int len)
461 (cell_t)"instance-to-path",
466 args.instance = instance;
467 args.buf = (cell_t)buf;
469 if (openfirmware(&args) == -1)
474 /* Return the fully qualified pathname corresponding to a package. */
476 OF_package_to_path(phandle_t package, char *buf, int len)
487 (cell_t)"package-to-path",
492 args.package = package;
493 args.buf = (cell_t)buf;
495 if (openfirmware(&args) == -1)
500 /* Call the method in the scope of a given instance. */
502 OF_call_method(char *method, ihandle_t instance, int nargs, int nreturns, ...)
511 cell_t args_n_results[12];
513 (cell_t)"call-method",
522 args.nargs = nargs + 2;
523 args.nreturns = nreturns + 1;
524 args.method = (cell_t)method;
525 args.instance = instance;
526 va_start(ap, nreturns);
527 for (cp = args.args_n_results + (n = nargs); --n >= 0;)
528 *--cp = va_arg(ap, cell_t);
529 if (openfirmware(&args) == -1)
531 if (args.args_n_results[nargs])
532 return (args.args_n_results[nargs]);
533 for (cp = args.args_n_results + nargs + (n = args.nreturns); --n > 0;)
534 *va_arg(ap, cell_t *) = *--cp;
540 * Device I/O functions
543 /* Open an instance for a device. */
545 OF_open(char *device)
559 args.device = (cell_t)device;
560 if (openfirmware(&args) == -1 || args.instance == 0) {
563 return (args.instance);
566 /* Close an instance. */
568 OF_close(ihandle_t instance)
580 args.instance = instance;
584 /* Read from an instance. */
586 OF_read(ihandle_t instance, void *addr, int len)
602 args.instance = instance;
603 args.addr = (cell_t)addr;
605 if (openfirmware(&args) == -1)
608 return (args.actual);
611 /* Write to an instance. */
613 OF_write(ihandle_t instance, void *addr, int len)
629 args.instance = instance;
630 args.addr = (cell_t)addr;
632 if (openfirmware(&args) == -1)
634 return (args.actual);
637 /* Seek to a position. */
639 OF_seek(ihandle_t instance, u_int64_t pos)
655 args.instance = instance;
656 args.poshi = pos >> 32;
658 if (openfirmware(&args) == -1)
660 return (args.status);
667 /* Claim an area of memory. */
669 OF_claim(void *virt, u_int size, u_int align)
685 args.virt = (cell_t)virt;
688 if (openfirmware(&args) == -1)
690 return ((void *)args.baseaddr);
693 /* Release an area of memory. */
695 OF_release(void *virt, u_int size)
708 args.virt = (cell_t)virt;
714 * Control transfer functions
717 /* Reset the system and call "boot <bootspec>". */
719 OF_boot(char *bootspec)
731 args.bootspec = (cell_t)bootspec;
733 for (;;) /* just in case */
737 /* Suspend and drop back to the Open Firmware interface. */
750 /* We may come back. */
753 /* Shut down and drop back to the Open Firmware interface. */
766 for (;;) /* just in case */
770 /* Free <size> bytes starting at <virt>, then call <entry> with <arg>. */
773 OF_chain(void *virt, u_int size, void (*entry)(), void *arg, u_int len)
789 args.virt = (cell_t)virt;
791 args.entry = (cell_t)entry;
792 args.arg = (cell_t)arg;
798 OF_chain(void *virt, u_int size,
799 void (*entry)(void *, u_int, void *, void *, u_int), void *arg, u_int len)
802 * This is a REALLY dirty hack till the firmware gets this going
806 OF_release(virt, size);
808 entry(0, 0, openfirmware, arg, len);