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 #include <sys/endian.h>
61 #include <machine/stdarg.h>
67 int (*openfirmware)(void *);
74 #define IN(x) htobe32((cell_t)x)
75 #define OUT(x) be32toh(x)
76 #define SETUP(a, b, c, d) \
78 a.nargs = IN( (c) ); \
79 a.nreturns = IN( (d) );
84 OF_init(int (*openfirm)(void *))
87 char mode[sizeof("true")];
89 openfirmware = openfirm;
91 if ((chosen = OF_finddevice("/chosen")) == -1)
93 if (OF_getprop(chosen, "memory", &memory, sizeof(memory)) == -1) {
94 memory = OF_open("/memory");
96 memory = OF_open("/memory@0");
100 if (OF_getprop(chosen, "mmu", &mmu, sizeof(mmu)) == -1)
104 * Check if we run in real mode. If so, we do not need to map
107 options = OF_finddevice("/options");
108 if (OF_getprop(options, "real-mode?", mode, sizeof(mode)) > 0 &&
109 strcmp(mode, "true") == 0)
117 /* Test to see if a service exists. */
128 SETUP(args, "test", 1, 1);
130 args.service = IN(name);
131 if (openfirmware(&args) == -1)
133 return (OUT(args.missing));
136 /* Return firmware millisecond count. */
146 SETUP(args, "milliseconds", 0, 1);
149 return (OUT(args.ms));
153 * Device tree functions
156 /* Return the next sibling of this node or 0. */
158 OF_peer(phandle_t node)
167 SETUP(args, "peer", 1, 1);
170 if (openfirmware(&args) == -1)
175 /* Return the first child of this node or 0. */
177 OF_child(phandle_t node)
186 SETUP(args, "child", 1, 1);
189 if (openfirmware(&args) == -1)
194 /* Return the parent of this node or 0. */
196 OF_parent(phandle_t node)
205 SETUP(args, "parent", 1, 1);
208 if (openfirmware(&args) == -1)
210 return (args.parent);
213 /* Return the package handle that corresponds to an instance handle. */
215 OF_instance_to_package(ihandle_t instance)
224 SETUP(args, "instance-to-package", 1, 1);
226 args.instance = instance;
227 if (openfirmware(&args) == -1)
229 return (args.package);
232 /* Get the length of a property of a package. */
234 OF_getproplen(phandle_t package, const char *propname)
244 SETUP(args, "getproplen", 2, 1);
246 args.package = package;
247 args.propname = IN(propname);
248 if (openfirmware(&args) == -1)
250 return (OUT(args.proplen));
253 /* Get the value of a property of a package. */
255 OF_getprop(phandle_t package, const char *propname, void *buf, int buflen)
267 SETUP(args, "getprop", 4, 1);
269 args.package = package;
270 args.propname = IN(propname);
272 args.buflen = IN(buflen);
273 if (openfirmware(&args) == -1)
275 return (OUT(args.size));
278 /* Decode a binary property from a package. */
280 OF_getencprop(phandle_t package, const char *propname, cell_t *buf, int buflen)
283 retval = OF_getprop(package, propname, buf, buflen);
287 for (i = 0; i < buflen/4; i++)
288 buf[i] = be32toh((uint32_t)buf[i]);
293 /* Get the next property of a package. */
295 OF_nextprop(phandle_t package, const char *previous, char *buf)
306 SETUP(args, "nextprop", 3, 1);
308 args.package = package;
309 args.previous = IN(previous);
311 if (openfirmware(&args) == -1)
313 return (OUT(args.flag));
316 /* Set the value of a property of a package. */
317 /* XXX Has a bug on FirePower */
319 OF_setprop(phandle_t package, const char *propname, void *buf, int len)
331 SETUP(args, "setprop", 4, 1);
333 args.package = package;
334 args.propname = IN(propname);
337 if (openfirmware(&args) == -1)
339 return (OUT(args.size));
342 /* Convert a device specifier to a fully qualified pathname. */
344 OF_canon(const char *device, char *buf, int len)
355 SETUP(args, "canon", 3, 1);
357 args.device = IN(device);
360 if (openfirmware(&args) == -1)
362 return (OUT(args.size));
365 /* Return a package handle for the specified device. */
367 OF_finddevice(const char *device)
376 SETUP(args, "finddevice", 1, 1);
378 args.device = IN(device);
379 if (openfirmware(&args) == -1)
381 return (args.package);
384 /* Return the fully qualified pathname corresponding to an instance. */
386 OF_instance_to_path(ihandle_t instance, char *buf, int len)
397 SETUP(args, "instance-to-path", 3, 1);
399 args.instance = instance;
402 if (openfirmware(&args) == -1)
404 return (OUT(args.size));
407 /* Return the fully qualified pathname corresponding to a package. */
409 OF_package_to_path(phandle_t package, char *buf, int len)
420 SETUP(args, "package-to-path", 3, 1);
422 args.package = package;
425 if (openfirmware(&args) == -1)
427 return (OUT(args.size));
430 /* Call the method in the scope of a given instance. */
432 OF_call_method(char *method, ihandle_t instance, int nargs, int nreturns, ...)
441 cell_t args_n_results[12];
443 SETUP(args, "call-method", nargs + 2, nreturns + 1);
449 args.method = IN(method);
450 args.instance = instance;
451 va_start(ap, nreturns);
452 for (cp = (cell_t *)(args.args_n_results + (n = nargs)); --n >= 0;)
453 *--cp = IN(va_arg(ap, cell_t));
454 if (openfirmware(&args) == -1)
456 if (args.args_n_results[nargs])
457 return (OUT(args.args_n_results[nargs]));
458 /* XXX what if ihandles or phandles are returned */
459 for (cp = (cell_t *)(args.args_n_results + nargs +
460 (n = be32toh(args.nreturns))); --n > 0;)
461 *va_arg(ap, cell_t *) = OUT(*--cp);
467 * Device I/O functions
470 /* Open an instance for a device. */
472 OF_open(char *device)
481 SETUP(args, "open", 1, 1);
483 args.device = IN(device);
484 if (openfirmware(&args) == -1 || args.instance == 0) {
487 return (args.instance);
490 /* Close an instance. */
492 OF_close(ihandle_t instance)
500 SETUP(args, "close", 1, 0);
502 args.instance = instance;
506 /* Read from an instance. */
508 OF_read(ihandle_t instance, void *addr, int len)
519 SETUP(args, "read", 3, 1);
521 args.instance = instance;
522 args.addr = IN(addr);
525 #if defined(OPENFIRM_DEBUG)
526 printf("OF_read: called with instance=%08x, addr=%p, len=%d\n",
527 instance, addr, len);
530 if (openfirmware(&args) == -1)
533 #if defined(OPENFIRM_DEBUG)
534 printf("OF_read: returning instance=%d, addr=%p, len=%d, actual=%d\n",
535 args.instance, OUT(args.addr), OUT(args.len), OUT(args.actual));
538 return (OUT(args.actual));
541 /* Write to an instance. */
543 OF_write(ihandle_t instance, void *addr, int len)
554 SETUP(args, "write", 3, 1);
556 args.instance = instance;
557 args.addr = IN(addr);
559 if (openfirmware(&args) == -1)
561 return (OUT(args.actual));
564 /* Seek to a position. */
566 OF_seek(ihandle_t instance, uint64_t pos)
577 SETUP(args, "seek", 3, 1);
579 args.instance = instance;
580 args.poshi = IN(((uint64_t)pos >> 32));
581 args.poslo = IN(pos);
582 if (openfirmware(&args) == -1)
584 return (OUT(args.status));
589 OF_blocks(ihandle_t instance)
599 SETUP(args, "#blocks", 2, 1);
601 args.instance = instance;
602 if (openfirmware(&args) == -1)
603 return ((unsigned int)-1);
604 return (OUT(args.blocks));
609 OF_block_size(ihandle_t instance)
619 SETUP(args, "block-size", 2, 1);
621 args.instance = instance;
622 if (openfirmware(&args) == -1)
624 return (OUT(args.size));
631 /* Claim an area of memory. */
633 OF_claim(void *virt, u_int size, u_int align)
644 SETUP(args, "claim", 3, 1);
646 args.virt = IN(virt);
647 args.size = IN(size);
648 args.align = IN(align);
649 if (openfirmware(&args) == -1)
651 return ((void *)OUT(args.baseaddr));
654 /* Release an area of memory. */
656 OF_release(void *virt, u_int size)
665 SETUP(args, "release", 2, 0);
667 args.virt = IN(virt);
668 args.size = IN(size);
673 * Control transfer functions
676 /* Reset the system and call "boot <bootspec>". */
678 OF_boot(char *bootspec)
686 SETUP(args, "boot", 1, 0);
688 args.bootspec = IN(bootspec);
690 for (;;) /* just in case */
694 /* Suspend and drop back to the Open Firmware interface. */
703 SETUP(args, "enter", 0, 0);
706 /* We may come back. */
709 /* Shut down and drop back to the Open Firmware interface. */
718 SETUP(args, "exit", 0, 0);
721 for (;;) /* just in case */
733 SETUP(args, "quiesce", 0, 0);
738 /* Free <size> bytes starting at <virt>, then call <entry> with <arg>. */
741 OF_chain(void *virt, u_int size, void (*entry)(), void *arg, u_int len)
753 SETUP(args, "chain", 5, 0);
755 args.virt = IN(virt);
756 args.size = IN(size);
757 args.entry = IN(entry);
764 OF_chain(void *virt, u_int size, void (*entry)(), void *arg, u_int len)
767 * This is a REALLY dirty hack till the firmware gets this going
771 OF_release(virt, size);
773 ((int (*)(u_long, u_long, u_long, void *, u_long))entry)
774 (0, 0, (u_long)openfirmware, arg, len);