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) {
84 memory = OF_open("/memory");
86 memory = OF_open("/memory@0");
90 if (OF_getprop(chosen, "mmu", &mmu, sizeof(mmu)) == -1)
98 /* Test to see if a service exists. */
114 args.service = (cell_t)name;
115 if (openfirmware(&args) == -1)
117 return (args.missing);
120 /* Return firmware millisecond count. */
130 (cell_t)"milliseconds",
140 * Device tree functions
143 /* Return the next sibling of this node or 0. */
145 OF_peer(phandle_t node)
160 if (openfirmware(&args) == -1)
165 /* Return the first child of this node or 0. */
167 OF_child(phandle_t node)
182 if (openfirmware(&args) == -1)
187 /* Return the parent of this node or 0. */
189 OF_parent(phandle_t node)
204 if (openfirmware(&args) == -1)
206 return (args.parent);
209 /* Return the package handle that corresponds to an instance handle. */
211 OF_instance_to_package(ihandle_t instance)
220 (cell_t)"instance-to-package",
225 args.instance = instance;
226 if (openfirmware(&args) == -1)
228 return (args.package);
231 /* Get the length of a property of a package. */
233 OF_getproplen(phandle_t package, char *propname)
243 (cell_t)"getproplen",
248 args.package = package;
249 args.propname = (cell_t)propname;
250 if (openfirmware(&args) == -1)
252 return (args.proplen);
255 /* Get the value of a property of a package. */
257 OF_getprop(phandle_t package, char *propname, void *buf, int buflen)
274 args.package = package;
275 args.propname = (cell_t)propname;
276 args.buf = (cell_t)buf;
277 args.buflen = buflen;
278 if (openfirmware(&args) == -1)
283 /* Get the next property of a package. */
285 OF_nextprop(phandle_t package, char *previous, char *buf)
301 args.package = package;
302 args.previous = (cell_t)previous;
303 args.buf = (cell_t)buf;
304 if (openfirmware(&args) == -1)
309 /* Set the value of a property of a package. */
310 /* XXX Has a bug on FirePower */
312 OF_setprop(phandle_t package, char *propname, void *buf, int len)
329 args.package = package;
330 args.propname = (cell_t)propname;
331 args.buf = (cell_t)buf;
333 if (openfirmware(&args) == -1)
338 /* Convert a device specifier to a fully qualified pathname. */
340 OF_canon(const char *device, char *buf, int len)
356 args.device = (cell_t)device;
357 args.buf = (cell_t)buf;
359 if (openfirmware(&args) == -1)
364 /* Return a package handle for the specified device. */
366 OF_finddevice(const char *device)
375 (cell_t)"finddevice",
380 args.device = (cell_t)device;
381 if (openfirmware(&args) == -1)
383 return (args.package);
386 /* Return the fully qualified pathname corresponding to an instance. */
388 OF_instance_to_path(ihandle_t instance, char *buf, int len)
399 (cell_t)"instance-to-path",
404 args.instance = instance;
405 args.buf = (cell_t)buf;
407 if (openfirmware(&args) == -1)
412 /* Return the fully qualified pathname corresponding to a package. */
414 OF_package_to_path(phandle_t package, char *buf, int len)
425 (cell_t)"package-to-path",
430 args.package = package;
431 args.buf = (cell_t)buf;
433 if (openfirmware(&args) == -1)
438 /* Call the method in the scope of a given instance. */
440 OF_call_method(char *method, ihandle_t instance, int nargs, int nreturns, ...)
449 cell_t args_n_results[12];
451 (cell_t)"call-method",
460 args.nargs = nargs + 2;
461 args.nreturns = nreturns + 1;
462 args.method = (cell_t)method;
463 args.instance = instance;
464 va_start(ap, nreturns);
465 for (cp = (cell_t *)(args.args_n_results + (n = nargs)); --n >= 0;)
466 *--cp = va_arg(ap, cell_t);
467 if (openfirmware(&args) == -1)
469 if (args.args_n_results[nargs])
470 return (args.args_n_results[nargs]);
471 for (cp = (cell_t *)(args.args_n_results + nargs + (n = args.nreturns));
473 *va_arg(ap, cell_t *) = *--cp;
479 * Device I/O functions
482 /* Open an instance for a device. */
484 OF_open(char *device)
498 args.device = (cell_t)device;
499 if (openfirmware(&args) == -1 || args.instance == 0) {
502 return (args.instance);
505 /* Close an instance. */
507 OF_close(ihandle_t instance)
519 args.instance = instance;
523 /* Read from an instance. */
525 OF_read(ihandle_t instance, void *addr, int len)
541 args.instance = instance;
542 args.addr = (cell_t)addr;
545 #if defined(OPENFIRM_DEBUG)
546 printf("OF_read: called with instance=%08x, addr=%p, len=%d\n",
547 args.instance, args.addr, args.len);
550 if (openfirmware(&args) == -1)
553 #if defined(OPENFIRM_DEBUG)
554 printf("OF_read: returning instance=%d, addr=%p, len=%d, actual=%d\n",
555 args.instance, args.addr, args.len, args.actual);
558 return (args.actual);
561 /* Write to an instance. */
563 OF_write(ihandle_t instance, void *addr, int len)
579 args.instance = instance;
580 args.addr = (cell_t)addr;
582 if (openfirmware(&args) == -1)
584 return (args.actual);
587 /* Seek to a position. */
589 OF_seek(ihandle_t instance, u_int64_t pos)
605 args.instance = instance;
606 args.poshi = pos >> 32;
608 if (openfirmware(&args) == -1)
610 return (args.status);
617 /* Claim an area of memory. */
619 OF_claim(void *virt, u_int size, u_int align)
635 args.virt = (cell_t)virt;
638 if (openfirmware(&args) == -1)
640 return ((void *)args.baseaddr);
643 /* Release an area of memory. */
645 OF_release(void *virt, u_int size)
658 args.virt = (cell_t)virt;
664 * Control transfer functions
667 /* Reset the system and call "boot <bootspec>". */
669 OF_boot(char *bootspec)
681 args.bootspec = (cell_t)bootspec;
683 for (;;) /* just in case */
687 /* Suspend and drop back to the Open Firmware interface. */
700 /* We may come back. */
703 /* Shut down and drop back to the Open Firmware interface. */
716 for (;;) /* just in case */
720 /* Free <size> bytes starting at <virt>, then call <entry> with <arg>. */
723 OF_chain(void *virt, u_int size, void (*entry)(), void *arg, u_int len)
739 args.virt = (cell_t)virt;
741 args.entry = (cell_t)entry;
742 args.arg = (cell_t)arg;
748 OF_chain(void *virt, u_int size, void (*entry)(), void *arg, u_int len)
751 * This is a REALLY dirty hack till the firmware gets this going
755 OF_release(virt, size);
757 entry(0, 0, openfirmware, arg, len);