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");
94 /* Test to see if a service exists. */
112 args.service = (cell_t)name;
113 if (openfirmware(&args) == -1)
115 return (int)args.missing;
118 /* Return firmware millisecond count. */
128 (cell_t)"milliseconds",
139 * Device tree functions
142 /* Return the next sibling of this node or 0. */
144 OF_peer(phandle_t node)
160 args.node = (u_int)node;
161 if (openfirmware(&args) == -1)
163 return (phandle_t)args.next;
166 /* Return the first child of this node or 0. */
168 OF_child(phandle_t node)
184 args.node = (u_int)node;
185 if (openfirmware(&args) == -1)
187 return (phandle_t)args.child;
190 /* Return the parent of this node or 0. */
192 OF_parent(phandle_t node)
208 args.node = (u_int)node;
209 if (openfirmware(&args) == -1)
211 return (phandle_t)args.parent;
214 /* Return the package handle that corresponds to an instance handle. */
216 OF_instance_to_package(ihandle_t instance)
225 (cell_t)"instance-to-package",
232 args.instance = (u_int)instance;
233 if (openfirmware(&args) == -1)
235 return (phandle_t)args.package;
238 /* Get the length of a property of a package. */
240 OF_getproplen(phandle_t package, char *propname)
250 (cell_t)"getproplen",
258 args.package = (u_int)package;
259 args.propname = (cell_t)propname;
260 if (openfirmware(&args) == -1)
262 return (int)args.proplen;
265 /* Get the value of a property of a package. */
267 OF_getprop(phandle_t package, char *propname, void *buf, int buflen)
289 args.package = (u_int)package;
290 args.propname = (cell_t)propname;
291 args.buf = (cell_t)buf;
292 args.buflen = (u_int)buflen;
293 if (openfirmware(&args) == -1)
295 return (int)args.size;
298 /* Get the next property of a package. */
300 OF_nextprop(phandle_t package, char *previous, char *buf)
320 args.package = (u_int)package;
321 args.previous = (cell_t)previous;
322 args.buf = (cell_t)buf;
323 if (openfirmware(&args) == -1)
325 return (int)args.flag;
328 /* Set the value of a property of a package. */
329 /* XXX Has a bug on FirePower */
331 OF_setprop(phandle_t package, char *propname, void *buf, int len)
353 args.package = (u_int)package;
354 args.propname = (cell_t)propname;
355 args.buf = (cell_t)buf;
356 args.len = (u_int)len;
357 if (openfirmware(&args) == -1)
359 return (int)args.size;
362 /* Convert a device specifier to a fully qualified pathname. */
364 OF_canon(const char *device, char *buf, int len)
384 args.device = (cell_t)device;
385 args.buf = (cell_t)buf;
386 args.len = (cell_t)len;
387 if (openfirmware(&args) == -1)
389 return (int)args.size;
392 /* Return a package handle for the specified device. */
394 OF_finddevice(const char *device)
403 (cell_t)"finddevice",
410 args.device = (cell_t)device;
411 if (openfirmware(&args) == -1)
414 return (phandle_t)args.package;
417 /* Return the fully qualified pathname corresponding to an instance. */
419 OF_instance_to_path(ihandle_t instance, char *buf, int len)
430 (cell_t)"instance-to-path",
439 args.instance = (u_int)instance;
440 args.buf = (cell_t)buf;
441 args.len = (u_int)len;
442 if (openfirmware(&args) == -1)
444 return (int)args.size;
447 /* Return the fully qualified pathname corresponding to a package. */
449 OF_package_to_path(phandle_t package, char *buf, int len)
460 (cell_t)"package-to-path",
469 args.package = (u_int)package;
470 args.buf = (cell_t)buf;
471 args.len = (u_int)len;
472 if (openfirmware(&args) == -1)
474 return (int)args.size;
477 /* Call the method in the scope of a given instance. */
479 OF_call_method(char *method, ihandle_t instance, int nargs, int nreturns, ...)
488 cell_t args_n_results[12];
490 (cell_t)"call-method",
495 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
501 args.nargs = nargs + 2;
502 args.nreturns = nreturns + 1;
503 args.method = (cell_t)method;
504 args.instance = (u_int)instance;
505 va_start(ap, nreturns);
506 for (ip = (int *)(args.args_n_results + (n = nargs)); --n >= 0;)
507 *--ip = va_arg(ap, int);
509 if (openfirmware(&args) == -1)
511 if (args.args_n_results[nargs])
512 return (int)args.args_n_results[nargs];
513 for (ip = (int *)(args.args_n_results + nargs + (n = args.nreturns));
515 *va_arg(ap, int *) = *--ip;
521 * Device I/O functions.
524 /* Open an instance for a device. */
526 OF_open(char *device)
542 args.device = (cell_t)device;
543 if (openfirmware(&args) == -1 || args.instance == 0) {
546 return (ihandle_t)args.instance;
549 /* Close an instance. */
551 OF_close(ihandle_t instance)
565 args.instance = (u_int)instance;
569 /* Read from an instance. */
571 OF_read(ihandle_t instance, void *addr, int len)
591 args.instance = (u_int)instance;
592 args.addr = (cell_t)addr;
593 args.len = (u_int)len;
595 #if defined(OPENFIRM_DEBUG)
596 printf("OF_read: called with instance=%08x, addr=%p, len=%d\n",
597 args.instance, args.addr, args.len);
600 if (openfirmware(&args) == -1)
603 #if defined(OPENFIRM_DEBUG)
604 printf("OF_read: returning instance=%d, addr=%p, len=%d, actual=%d\n",
605 args.instance, args.addr, args.len, args.actual);
608 return (int)args.actual;
611 /* Write to an instance. */
613 OF_write(ihandle_t instance, void *addr, int len)
633 args.instance = (u_int)instance;
634 args.addr = (cell_t)addr;
635 args.len = (u_int)len;
636 if (openfirmware(&args) == -1)
638 return (int)args.actual;
641 /* Seek to a position. */
643 OF_seek(ihandle_t instance, u_int64_t pos)
663 args.instance = (u_int)instance;
664 args.poshi = pos >> 32;
666 if (openfirmware(&args) == -1)
668 return (int)args.status;
675 /* Claim an area of memory. */
677 OF_claim(void *virt, u_int size, u_int align)
697 args.virt = (cell_t)virt;
700 if (openfirmware(&args) == -1)
702 return (void *)args.baseaddr;
705 /* Allocate an area of physical memory */
707 OF_claim_virt(vm_offset_t virt, size_t size, int align)
721 (cell_t)"call-method",
738 if (openfirmware(&args) == -1)
739 return (vm_offset_t)-1;
741 return (vm_offset_t)args.ret;
744 /* Allocate an area of physical memory */
746 OF_alloc_phys(size_t size, int align)
760 (cell_t)"call-method",
772 args.ihandle = memory;
776 if (openfirmware(&args) == -1)
779 return (void *)(args.phys_hi << 32 | args.phys_low);
782 /* Release an area of memory. */
784 OF_release(void *virt, u_int size)
800 args.virt = (cell_t)virt;
805 /* Release an area of physical memory. */
807 OF_release_phys(vm_offset_t phys, u_int size)
819 (cell_t)"call-method",
829 args.ihandle = memory;
830 args.phys_hi = (u_int32_t)(phys >> 32);
831 args.phys_lo = (u_int32_t)phys;
837 * Control transfer functions.
840 /* Reset the system and call "boot <bootspec>". */
842 OF_boot(char *bootspec)
856 args.bootspec = (cell_t)bootspec;
858 for (;;); /* just in case */
861 /* Suspend and drop back to the Open Firmware interface. */
878 /* Shut down and drop back to the Open Firmware interface. */
893 for (;;); /* just in case */
896 /* Free <size> bytes starting at <virt>, then call <entry> with <arg>. */
899 OF_chain(void *virt, u_int size, void (*entry)(), void *arg, u_int len)
921 args.virt = (cell_t)virt;
923 args.entry = (cell_t)entry;
924 args.arg = (cell_t)arg;
930 OF_chain(void *virt, u_int size, void (*entry)(), void *arg, u_int len)
933 * This is a REALLY dirty hack till the firmware gets this going
937 OF_release(virt, size);
940 entry(0, 0, openfirmware, arg, len);