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 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
37 * Copyright (C) 2000 Benno Rice.
38 * All rights reserved.
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
43 * 1. Redistributions of source code must retain the above copyright
44 * notice, this list of conditions and the following disclaimer.
45 * 2. Redistributions in binary form must reproduce the above copyright
46 * notice, this list of conditions and the following disclaimer in the
47 * documentation and/or other materials provided with the distribution.
49 * THIS SOFTWARE IS PROVIDED BY Benno Rice ``AS IS'' AND ANY EXPRESS OR
50 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
51 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
52 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
53 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
54 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
55 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
56 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
57 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
58 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
62 #include <sys/param.h>
63 #include <sys/kernel.h>
64 #include <sys/malloc.h>
65 #include <sys/systm.h>
67 #include <machine/stdarg.h>
69 #include <dev/ofw/openfirm.h>
71 MALLOC_DEFINE(M_OFWPROP, "openfirm", "Open Firmware properties");
73 static ihandle_t stdout;
78 OF_init(int (*openfirm)(void *))
82 set_openfirm_callback(openfirm);
83 if ((chosen = OF_finddevice("/chosen")) == -1)
85 OF_getprop(chosen, "stdout", &stdout, sizeof(stdout));
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)
127 OF_interpret(char *cmd, int nreturns, ...)
142 args.nreturns = ++nreturns;
143 args.slot[i++] = (cell_t)cmd;
144 va_start(ap, nreturns);
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)
197 args.node = (cell_t)node;
198 if (openfirmware(&args) == -1)
203 /* Return the first child of this node or 0. */
205 OF_child(phandle_t node)
219 args.node = (cell_t)node;
220 if (openfirmware(&args) == -1)
225 /* Return the parent of this node or 0. */
227 OF_parent(phandle_t node)
241 args.node = (cell_t)node;
242 if (openfirmware(&args) == -1)
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 = (cell_t)instance;
264 if (openfirmware(&args) == -1)
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 = (cell_t)package;
287 args.propname = (cell_t)propname;
288 if (openfirmware(&args) == -1)
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 = (cell_t)package;
313 args.propname = (cell_t)propname;
314 args.buf = (cell_t)buf;
315 args.buflen = (cell_t)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 = (cell_t)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 = (cell_t)package;
392 args.propname = (cell_t)propname;
393 args.buf = (cell_t)buf;
394 args.len = (cell_t)len;
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;
420 args.len = (cell_t)len;
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)
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 = (cell_t)instance;
467 args.buf = (cell_t)buf;
468 args.len = (cell_t)len;
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 = (cell_t)package;
493 args.buf = (cell_t)buf;
494 args.len = (cell_t)len;
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 = (cell_t)nargs + 2;
523 args.nreturns = (cell_t)nreturns + 1;
524 args.method = (cell_t)method;
525 args.instance = (cell_t)instance;
526 va_start(ap, nreturns);
527 for (ip = args.args_n_results + (n = (cell_t)nargs); --n >= 0;)
528 *--ip = va_arg(ap, cell_t);
530 if (openfirmware(&args) == -1)
532 if (args.args_n_results[nargs])
533 return args.args_n_results[nargs];
534 for (ip = args.args_n_results + nargs + (n = args.nreturns); --n > 0;)
535 *va_arg(ap, cell_t *) = *--ip;
541 * Device I/O functions.
544 /* Open an instance for a device. */
546 OF_open(char *device)
560 args.device = (cell_t)device;
561 if (openfirmware(&args) == -1 || args.instance == 0) {
564 return args.instance;
567 /* Close an instance. */
569 OF_close(ihandle_t instance)
582 args.instance = (cell_t)instance;
586 /* Read from an instance. */
588 OF_read(ihandle_t instance, void *addr, int len)
604 args.instance = (cell_t)instance;
605 args.addr = (cell_t)addr;
606 args.len = (cell_t)len;
607 if (openfirmware(&args) == -1)
613 /* Write to an instance. */
615 OF_write(ihandle_t instance, void *addr, int len)
631 args.instance = (cell_t)instance;
632 args.addr = (cell_t)addr;
633 args.len = (cell_t)len;
634 if (openfirmware(&args) == -1)
639 /* Seek to a position. */
641 OF_seek(ihandle_t instance, u_int64_t pos)
657 args.instance = (cell_t)instance;
658 args.poshi = (cell_t)(pos >> 32);
659 args.poslo = (cell_t)pos;
660 if (openfirmware(&args) == -1)
669 /* Claim an area of memory. */
671 OF_claim(void *virt, u_int size, u_int align)
687 args.virt = (cell_t)virt;
688 args.size = (cell_t)size;
689 args.align = (cell_t)align;
690 if (openfirmware(&args) == -1)
692 return (void *)args.baseaddr;
695 /* Release an area of memory. */
697 OF_release(void *virt, u_int size)
711 args.virt = (cell_t)virt;
712 args.size = (cell_t)size;
717 * Control transfer functions.
720 /* Reset the system and call "boot <bootspec>". */
722 OF_boot(char *bootspec)
735 args.bootspec = (cell_t)bootspec;
737 for (;;); /* just in case */
740 /* Suspend and drop back to the Open Firmware interface. */
755 return; /* We may come back. */
758 /* Shut down and drop back to the Open Firmware interface. */
773 for (;;); /* just in case */
776 /* Free <size> bytes starting at <virt>, then call <entry> with <arg>. */
779 OF_chain(void *virt, u_int size, void (*entry)(), void *arg, u_int len)
796 args.virt = (cell_t)virt;
797 args.size = (cell_t)size;
798 args.entry = (cell_t)entry;
799 args.arg = (cell_t)arg;
800 args.len = (cell_t)len;
805 OF_chain(void *virt, u_int size,
806 void (*entry)(void *, u_int, void *, void *, u_int), void *arg, u_int len)
809 * This is a REALLY dirty hack till the firmware gets this going
812 OF_release(virt, size);
814 entry(0, 0, openfirmware, arg, len);