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 *);
75 OF_init(int (*openfirm)(void *))
79 openfirmware = openfirm;
81 chosen = OF_finddevice("/chosen");
82 OF_getprop(chosen, "memory", &memory, sizeof(memory));
84 panic("failed to get memory ihandle");
85 OF_getprop(chosen, "mmu", &mmu, sizeof(mmu));
87 panic("failed to get mmu ihandle");
93 static phandle_t chosen;
98 if ((chosen = OF_finddevice("/chosen")) == -1)
108 /* Test to see if a service exists. */
126 args.service = (cell_t)name;
127 if (openfirmware(&args) == -1)
129 return (int)args.missing;
132 /* Return firmware millisecond count. */
142 (cell_t)"milliseconds",
153 * Device tree functions
156 /* Return the next sibling of this node or 0. */
158 OF_peer(phandle_t node)
174 args.node = (u_int)node;
175 if (openfirmware(&args) == -1)
177 return (phandle_t)args.next;
180 /* Return the first child of this node or 0. */
182 OF_child(phandle_t node)
198 args.node = (u_int)node;
199 if (openfirmware(&args) == -1)
201 return (phandle_t)args.child;
204 /* Return the parent of this node or 0. */
206 OF_parent(phandle_t node)
222 args.node = (u_int)node;
223 if (openfirmware(&args) == -1)
225 return (phandle_t)args.parent;
228 /* Return the package handle that corresponds to an instance handle. */
230 OF_instance_to_package(ihandle_t instance)
239 (cell_t)"instance-to-package",
246 args.instance = (u_int)instance;
247 if (openfirmware(&args) == -1)
249 return (phandle_t)args.package;
252 /* Get the length of a property of a package. */
254 OF_getproplen(phandle_t package, char *propname)
264 (cell_t)"getproplen",
272 args.package = (u_int)package;
273 args.propname = (cell_t)propname;
274 if (openfirmware(&args) == -1)
276 return (int)args.proplen;
279 /* Get the value of a property of a package. */
281 OF_getprop(phandle_t package, char *propname, void *buf, int buflen)
303 args.package = (u_int)package;
304 args.propname = (cell_t)propname;
305 args.buf = (cell_t)buf;
306 args.buflen = (u_int)buflen;
307 if (openfirmware(&args) == -1)
309 return (int)args.size;
312 /* Get the next property of a package. */
314 OF_nextprop(phandle_t package, char *previous, char *buf)
334 args.package = (u_int)package;
335 args.previous = (cell_t)previous;
336 args.buf = (cell_t)buf;
337 if (openfirmware(&args) == -1)
339 return (int)args.flag;
342 /* Set the value of a property of a package. */
343 /* XXX Has a bug on FirePower */
345 OF_setprop(phandle_t package, char *propname, void *buf, int len)
367 args.package = (u_int)package;
368 args.propname = (cell_t)propname;
369 args.buf = (cell_t)buf;
370 args.len = (u_int)len;
371 if (openfirmware(&args) == -1)
373 return (int)args.size;
376 /* Convert a device specifier to a fully qualified pathname. */
378 OF_canon(const char *device, char *buf, int len)
398 args.device = (cell_t)device;
399 args.buf = (cell_t)buf;
400 args.len = (cell_t)len;
401 if (openfirmware(&args) == -1)
403 return (int)args.size;
406 /* Return a package handle for the specified device. */
408 OF_finddevice(const char *device)
417 (cell_t)"finddevice",
424 args.device = (cell_t)device;
425 if (openfirmware(&args) == -1)
428 return (phandle_t)args.package;
431 /* Return the fully qualified pathname corresponding to an instance. */
433 OF_instance_to_path(ihandle_t instance, char *buf, int len)
444 (cell_t)"instance-to-path",
453 args.instance = (u_int)instance;
454 args.buf = (cell_t)buf;
455 args.len = (u_int)len;
456 if (openfirmware(&args) == -1)
458 return (int)args.size;
461 /* Return the fully qualified pathname corresponding to a package. */
463 OF_package_to_path(phandle_t package, char *buf, int len)
474 (cell_t)"package-to-path",
483 args.package = (u_int)package;
484 args.buf = (cell_t)buf;
485 args.len = (u_int)len;
486 if (openfirmware(&args) == -1)
488 return (int)args.size;
491 /* Call the method in the scope of a given instance. */
493 OF_call_method(char *method, ihandle_t instance, int nargs, int nreturns, ...)
502 cell_t args_n_results[12];
504 (cell_t)"call-method",
509 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
515 args.nargs = nargs + 2;
516 args.nreturns = nreturns + 1;
517 args.method = (cell_t)method;
518 args.instance = (u_int)instance;
519 va_start(ap, nreturns);
520 for (ip = (int *)(args.args_n_results + (n = nargs)); --n >= 0;)
521 *--ip = va_arg(ap, int);
523 if (openfirmware(&args) == -1)
525 if (args.args_n_results[nargs])
526 return (int)args.args_n_results[nargs];
527 for (ip = (int *)(args.args_n_results + nargs + (n = args.nreturns));
529 *va_arg(ap, int *) = *--ip;
535 * Device I/O functions.
538 /* Open an instance for a device. */
540 OF_open(char *device)
556 args.device = (cell_t)device;
557 if (openfirmware(&args) == -1 || args.instance == 0) {
560 return (ihandle_t)args.instance;
563 /* Close an instance. */
565 OF_close(ihandle_t instance)
579 args.instance = (u_int)instance;
583 /* Read from an instance. */
585 OF_read(ihandle_t instance, void *addr, int len)
605 args.instance = (u_int)instance;
606 args.addr = (cell_t)addr;
607 args.len = (u_int)len;
609 #if defined(OPENFIRM_DEBUG)
610 printf("OF_read: called with instance=%08x, addr=%p, len=%d\n",
611 args.instance, args.addr, args.len);
614 if (openfirmware(&args) == -1)
617 #if defined(OPENFIRM_DEBUG)
618 printf("OF_read: returning instance=%d, addr=%p, len=%d, actual=%d\n",
619 args.instance, args.addr, args.len, args.actual);
622 return (int)args.actual;
625 /* Write to an instance. */
627 OF_write(ihandle_t instance, void *addr, int len)
647 args.instance = (u_int)instance;
648 args.addr = (cell_t)addr;
649 args.len = (u_int)len;
650 if (openfirmware(&args) == -1)
652 return (int)args.actual;
655 /* Seek to a position. */
657 OF_seek(ihandle_t instance, u_int64_t pos)
677 args.instance = (u_int)instance;
678 args.poshi = pos >> 32;
680 if (openfirmware(&args) == -1)
682 return (int)args.status;
689 /* Claim an area of memory. */
691 OF_claim(void *virt, u_int size, u_int align)
711 args.virt = (cell_t)virt;
714 if (openfirmware(&args) == -1)
716 return (void *)args.baseaddr;
719 /* Allocate an area of physical memory */
721 OF_claim_virt(vm_offset_t virt, size_t size, int align)
735 (cell_t)"call-method",
752 if (openfirmware(&args) == -1)
753 return (vm_offset_t)-1;
755 return (vm_offset_t)args.ret;
758 /* Allocate an area of physical memory */
760 OF_alloc_phys(size_t size, int align)
774 (cell_t)"call-method",
786 args.ihandle = memory;
790 if (openfirmware(&args) == -1)
793 return (void *)(args.phys_hi << 32 | args.phys_low);
796 /* Release an area of memory. */
798 OF_release(void *virt, u_int size)
814 args.virt = (cell_t)virt;
819 /* Release an area of physical memory. */
821 OF_release_phys(vm_offset_t phys, u_int size)
833 (cell_t)"call-method",
843 args.ihandle = memory;
844 args.phys_hi = (u_int32_t)(phys >> 32);
845 args.phys_lo = (u_int32_t)phys;
851 * Control transfer functions.
854 /* Reset the system and call "boot <bootspec>". */
856 OF_boot(char *bootspec)
870 args.bootspec = (cell_t)bootspec;
872 for (;;); /* just in case */
875 /* Suspend and drop back to the Open Firmware interface. */
892 /* Shut down and drop back to the Open Firmware interface. */
907 for (;;); /* just in case */
910 /* Free <size> bytes starting at <virt>, then call <entry> with <arg>. */
913 OF_chain(void *virt, u_int size, void (*entry)(), void *arg, u_int len)
935 args.virt = (cell_t)virt;
937 args.entry = (cell_t)entry;
938 args.arg = (cell_t)arg;
944 OF_chain(void *virt, u_int size, void (*entry)(), void *arg, u_int len)
947 * This is a REALLY dirty hack till the firmware gets this going
951 OF_release(virt, size);
954 entry(0, 0, openfirmware, arg, len);