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 *);
77 OF_init(int (*openfirm)(void *))
80 char mode[sizeof("true")];
82 openfirmware = openfirm;
84 if ((chosen = OF_finddevice("/chosen")) == -1)
86 if (OF_getprop(chosen, "memory", &memory, sizeof(memory)) == -1) {
87 memory = OF_open("/memory");
89 memory = OF_open("/memory@0");
93 if (OF_getprop(chosen, "mmu", &mmu, sizeof(mmu)) == -1)
97 * Check if we run in real mode. If so, we do not need to map
100 options = OF_finddevice("/options");
101 if (OF_getprop(options, "real-mode?", mode, sizeof(mode)) > 0 &&
102 strcmp(mode, "true") == 0)
110 /* Test to see if a service exists. */
126 args.service = (cell_t)name;
127 if (openfirmware(&args) == -1)
129 return (args.missing);
132 /* Return firmware millisecond count. */
142 (cell_t)"milliseconds",
152 * Device tree functions
155 /* Return the next sibling of this node or 0. */
157 OF_peer(phandle_t node)
172 if (openfirmware(&args) == -1)
177 /* Return the first child of this node or 0. */
179 OF_child(phandle_t node)
194 if (openfirmware(&args) == -1)
199 /* Return the parent of this node or 0. */
201 OF_parent(phandle_t node)
216 if (openfirmware(&args) == -1)
218 return (args.parent);
221 /* Return the package handle that corresponds to an instance handle. */
223 OF_instance_to_package(ihandle_t instance)
232 (cell_t)"instance-to-package",
237 args.instance = instance;
238 if (openfirmware(&args) == -1)
240 return (args.package);
243 /* Get the length of a property of a package. */
245 OF_getproplen(phandle_t package, const char *propname)
255 (cell_t)"getproplen",
260 args.package = package;
261 args.propname = (cell_t)propname;
262 if (openfirmware(&args) == -1)
264 return (args.proplen);
267 /* Get the value of a property of a package. */
269 OF_getprop(phandle_t package, const char *propname, void *buf, int buflen)
286 args.package = package;
287 args.propname = (cell_t)propname;
288 args.buf = (cell_t)buf;
289 args.buflen = buflen;
290 if (openfirmware(&args) == -1)
295 /* Get the next property of a package. */
297 OF_nextprop(phandle_t package, const char *previous, char *buf)
313 args.package = package;
314 args.previous = (cell_t)previous;
315 args.buf = (cell_t)buf;
316 if (openfirmware(&args) == -1)
321 /* Set the value of a property of a package. */
322 /* XXX Has a bug on FirePower */
324 OF_setprop(phandle_t package, const char *propname, void *buf, int len)
341 args.package = package;
342 args.propname = (cell_t)propname;
343 args.buf = (cell_t)buf;
345 if (openfirmware(&args) == -1)
350 /* Convert a device specifier to a fully qualified pathname. */
352 OF_canon(const char *device, char *buf, int len)
368 args.device = (cell_t)device;
369 args.buf = (cell_t)buf;
371 if (openfirmware(&args) == -1)
376 /* Return a package handle for the specified device. */
378 OF_finddevice(const char *device)
387 (cell_t)"finddevice",
392 args.device = (cell_t)device;
393 if (openfirmware(&args) == -1)
395 return (args.package);
398 /* Return the fully qualified pathname corresponding to an instance. */
400 OF_instance_to_path(ihandle_t instance, char *buf, int len)
411 (cell_t)"instance-to-path",
416 args.instance = instance;
417 args.buf = (cell_t)buf;
419 if (openfirmware(&args) == -1)
424 /* Return the fully qualified pathname corresponding to a package. */
426 OF_package_to_path(phandle_t package, char *buf, int len)
437 (cell_t)"package-to-path",
442 args.package = package;
443 args.buf = (cell_t)buf;
445 if (openfirmware(&args) == -1)
450 /* Call the method in the scope of a given instance. */
452 OF_call_method(char *method, ihandle_t instance, int nargs, int nreturns, ...)
461 cell_t args_n_results[12];
463 (cell_t)"call-method",
472 args.nargs = nargs + 2;
473 args.nreturns = nreturns + 1;
474 args.method = (cell_t)method;
475 args.instance = instance;
476 va_start(ap, nreturns);
477 for (cp = (cell_t *)(args.args_n_results + (n = nargs)); --n >= 0;)
478 *--cp = va_arg(ap, cell_t);
479 if (openfirmware(&args) == -1)
481 if (args.args_n_results[nargs])
482 return (args.args_n_results[nargs]);
483 for (cp = (cell_t *)(args.args_n_results + nargs + (n = args.nreturns));
485 *va_arg(ap, cell_t *) = *--cp;
491 * Device I/O functions
494 /* Open an instance for a device. */
496 OF_open(char *device)
510 args.device = (cell_t)device;
511 if (openfirmware(&args) == -1 || args.instance == 0) {
514 return (args.instance);
517 /* Close an instance. */
519 OF_close(ihandle_t instance)
531 args.instance = instance;
535 /* Read from an instance. */
537 OF_read(ihandle_t instance, void *addr, int len)
553 args.instance = instance;
554 args.addr = (cell_t)addr;
557 #if defined(OPENFIRM_DEBUG)
558 printf("OF_read: called with instance=%08x, addr=%p, len=%d\n",
559 args.instance, args.addr, args.len);
562 if (openfirmware(&args) == -1)
565 #if defined(OPENFIRM_DEBUG)
566 printf("OF_read: returning instance=%d, addr=%p, len=%d, actual=%d\n",
567 args.instance, args.addr, args.len, args.actual);
570 return (args.actual);
573 /* Write to an instance. */
575 OF_write(ihandle_t instance, void *addr, int len)
591 args.instance = instance;
592 args.addr = (cell_t)addr;
594 if (openfirmware(&args) == -1)
596 return (args.actual);
599 /* Seek to a position. */
601 OF_seek(ihandle_t instance, uint64_t pos)
617 args.instance = instance;
618 args.poshi = pos >> 32;
620 if (openfirmware(&args) == -1)
622 return (args.status);
627 OF_blocks(ihandle_t instance)
642 args.instance = instance;
643 if (openfirmware(&args) == -1)
644 return ((unsigned int)-1);
645 return (args.blocks);
650 OF_block_size(ihandle_t instance)
660 (cell_t)"block-size",
665 args.instance = instance;
666 if (openfirmware(&args) == -1)
675 /* Claim an area of memory. */
677 OF_claim(void *virt, u_int size, u_int align)
693 args.virt = (cell_t)virt;
696 if (openfirmware(&args) == -1)
698 return ((void *)args.baseaddr);
701 /* Release an area of memory. */
703 OF_release(void *virt, u_int size)
716 args.virt = (cell_t)virt;
722 * Control transfer functions
725 /* Reset the system and call "boot <bootspec>". */
727 OF_boot(char *bootspec)
739 args.bootspec = (cell_t)bootspec;
741 for (;;) /* just in case */
745 /* Suspend and drop back to the Open Firmware interface. */
758 /* We may come back. */
761 /* Shut down and drop back to the Open Firmware interface. */
774 for (;;) /* just in case */
792 /* Free <size> bytes starting at <virt>, then call <entry> with <arg>. */
795 OF_chain(void *virt, u_int size, void (*entry)(), void *arg, u_int len)
811 args.virt = (cell_t)virt;
813 args.entry = (cell_t)entry;
814 args.arg = (cell_t)arg;
820 OF_chain(void *virt, u_int size, void (*entry)(), void *arg, u_int len)
823 * This is a REALLY dirty hack till the firmware gets this going
827 OF_release(virt, size);
829 entry(0, 0, openfirmware, arg, len);