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 <sys/endian.h>
63 #include <machine/stdarg.h>
69 int (*openfirmware)(void *);
76 #define IN(x) htobe32((cell_t)x)
77 #define OUT(x) be32toh(x)
78 #define SETUP(a, b, c, d) \
80 a.nargs = IN( (c) ); \
81 a.nreturns = IN( (d) );
86 OF_init(int (*openfirm)(void *))
89 char mode[sizeof("true")];
91 openfirmware = openfirm;
93 if ((chosen = OF_finddevice("/chosen")) == -1)
95 if (OF_getprop(chosen, "memory", &memory, sizeof(memory)) == -1) {
96 memory = OF_open("/memory");
98 memory = OF_open("/memory@0");
102 if (OF_getprop(chosen, "mmu", &mmu, sizeof(mmu)) == -1)
106 * Check if we run in real mode. If so, we do not need to map
109 options = OF_finddevice("/options");
110 if (OF_getprop(options, "real-mode?", mode, sizeof(mode)) > 0 &&
111 strcmp(mode, "true") == 0)
119 /* Test to see if a service exists. */
130 SETUP(args, "test", 1, 1);
132 args.service = IN(name);
133 if (openfirmware(&args) == -1)
135 return (OUT(args.missing));
138 /* Return firmware millisecond count. */
148 SETUP(args, "milliseconds", 0, 1);
151 return (OUT(args.ms));
155 * Device tree functions
158 /* Return the next sibling of this node or 0. */
160 OF_peer(phandle_t node)
169 SETUP(args, "peer", 1, 1);
172 if (openfirmware(&args) == -1)
177 /* Return the first child of this node or 0. */
179 OF_child(phandle_t node)
188 SETUP(args, "child", 1, 1);
191 if (openfirmware(&args) == -1)
196 /* Return the parent of this node or 0. */
198 OF_parent(phandle_t node)
207 SETUP(args, "parent", 1, 1);
210 if (openfirmware(&args) == -1)
212 return (args.parent);
215 /* Return the package handle that corresponds to an instance handle. */
217 OF_instance_to_package(ihandle_t instance)
226 SETUP(args, "instance-to-package", 1, 1);
228 args.instance = instance;
229 if (openfirmware(&args) == -1)
231 return (args.package);
234 /* Get the length of a property of a package. */
236 OF_getproplen(phandle_t package, const char *propname)
246 SETUP(args, "getproplen", 2, 1);
248 args.package = package;
249 args.propname = IN(propname);
250 if (openfirmware(&args) == -1)
252 return (OUT(args.proplen));
255 /* Get the value of a property of a package. */
257 OF_getprop(phandle_t package, const char *propname, void *buf, int buflen)
269 SETUP(args, "getprop", 4, 1);
271 args.package = package;
272 args.propname = IN(propname);
274 args.buflen = IN(buflen);
275 if (openfirmware(&args) == -1)
277 return (OUT(args.size));
280 /* Decode a binary property from a package. */
282 OF_getencprop(phandle_t package, const char *propname, cell_t *buf, int buflen)
285 retval = OF_getprop(package, propname, buf, buflen);
289 for (i = 0; i < buflen/4; i++)
290 buf[i] = be32toh((uint32_t)buf[i]);
295 /* Get the next property of a package. */
297 OF_nextprop(phandle_t package, const char *previous, char *buf)
308 SETUP(args, "nextprop", 3, 1);
310 args.package = package;
311 args.previous = IN(previous);
313 if (openfirmware(&args) == -1)
315 return (OUT(args.flag));
318 /* Set the value of a property of a package. */
319 /* XXX Has a bug on FirePower */
321 OF_setprop(phandle_t package, const char *propname, void *buf, int len)
333 SETUP(args, "setprop", 4, 1);
335 args.package = package;
336 args.propname = IN(propname);
339 if (openfirmware(&args) == -1)
341 return (OUT(args.size));
344 /* Convert a device specifier to a fully qualified pathname. */
346 OF_canon(const char *device, char *buf, int len)
357 SETUP(args, "canon", 3, 1);
359 args.device = IN(device);
362 if (openfirmware(&args) == -1)
364 return (OUT(args.size));
367 /* Return a package handle for the specified device. */
369 OF_finddevice(const char *device)
378 SETUP(args, "finddevice", 1, 1);
380 args.device = IN(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 SETUP(args, "instance-to-path", 3, 1);
401 args.instance = instance;
404 if (openfirmware(&args) == -1)
406 return (OUT(args.size));
409 /* Return the fully qualified pathname corresponding to a package. */
411 OF_package_to_path(phandle_t package, char *buf, int len)
422 SETUP(args, "package-to-path", 3, 1);
424 args.package = package;
427 if (openfirmware(&args) == -1)
429 return (OUT(args.size));
432 /* Call the method in the scope of a given instance. */
434 OF_call_method(char *method, ihandle_t instance, int nargs, int nreturns, ...)
443 cell_t args_n_results[12];
445 SETUP(args, "call-method", nargs + 2, nreturns + 1);
451 args.method = IN(method);
452 args.instance = instance;
453 va_start(ap, nreturns);
454 for (cp = (cell_t *)(args.args_n_results + (n = nargs)); --n >= 0;)
455 *--cp = IN(va_arg(ap, cell_t));
456 if (openfirmware(&args) == -1)
458 if (args.args_n_results[nargs])
459 return (OUT(args.args_n_results[nargs]));
460 /* XXX what if ihandles or phandles are returned */
461 for (cp = (cell_t *)(args.args_n_results + nargs +
462 (n = be32toh(args.nreturns))); --n > 0;)
463 *va_arg(ap, cell_t *) = OUT(*--cp);
469 * Device I/O functions
472 /* Open an instance for a device. */
474 OF_open(char *device)
483 SETUP(args, "open", 1, 1);
485 args.device = IN(device);
486 if (openfirmware(&args) == -1 || args.instance == 0) {
489 return (args.instance);
492 /* Close an instance. */
494 OF_close(ihandle_t instance)
502 SETUP(args, "close", 1, 0);
504 args.instance = instance;
508 /* Read from an instance. */
510 OF_read(ihandle_t instance, void *addr, int len)
521 SETUP(args, "read", 3, 1);
523 args.instance = instance;
524 args.addr = IN(addr);
527 #if defined(OPENFIRM_DEBUG)
528 printf("OF_read: called with instance=%08x, addr=%p, len=%d\n",
529 instance, addr, len);
532 if (openfirmware(&args) == -1)
535 #if defined(OPENFIRM_DEBUG)
536 printf("OF_read: returning instance=%d, addr=%p, len=%d, actual=%d\n",
537 args.instance, OUT(args.addr), OUT(args.len), OUT(args.actual));
540 return (OUT(args.actual));
543 /* Write to an instance. */
545 OF_write(ihandle_t instance, void *addr, int len)
556 SETUP(args, "write", 3, 1);
558 args.instance = instance;
559 args.addr = IN(addr);
561 if (openfirmware(&args) == -1)
563 return (OUT(args.actual));
566 /* Seek to a position. */
568 OF_seek(ihandle_t instance, uint64_t pos)
579 SETUP(args, "seek", 3, 1);
581 args.instance = instance;
582 args.poshi = IN(((uint64_t)pos >> 32));
583 args.poslo = IN(pos);
584 if (openfirmware(&args) == -1)
586 return (OUT(args.status));
591 OF_blocks(ihandle_t instance)
601 SETUP(args, "#blocks", 2, 1);
603 args.instance = instance;
604 if (openfirmware(&args) == -1)
605 return ((unsigned int)-1);
606 return (OUT(args.blocks));
611 OF_block_size(ihandle_t instance)
621 SETUP(args, "block-size", 2, 1);
623 args.instance = instance;
624 if (openfirmware(&args) == -1)
626 return (OUT(args.size));
633 /* Claim an area of memory. */
635 OF_claim(void *virt, u_int size, u_int align)
646 SETUP(args, "claim", 3, 1);
648 args.virt = IN(virt);
649 args.size = IN(size);
650 args.align = IN(align);
651 if (openfirmware(&args) == -1)
653 return ((void *)OUT(args.baseaddr));
656 /* Release an area of memory. */
658 OF_release(void *virt, u_int size)
667 SETUP(args, "release", 2, 0);
669 args.virt = IN(virt);
670 args.size = IN(size);
675 * Control transfer functions
678 /* Reset the system and call "boot <bootspec>". */
680 OF_boot(char *bootspec)
688 SETUP(args, "boot", 1, 0);
690 args.bootspec = IN(bootspec);
692 for (;;) /* just in case */
696 /* Suspend and drop back to the Open Firmware interface. */
705 SETUP(args, "enter", 0, 0);
708 /* We may come back. */
711 /* Shut down and drop back to the Open Firmware interface. */
720 SETUP(args, "exit", 0, 0);
723 for (;;) /* just in case */
735 SETUP(args, "quiesce", 0, 0);
740 /* Free <size> bytes starting at <virt>, then call <entry> with <arg>. */
743 OF_chain(void *virt, u_int size, void (*entry)(), void *arg, u_int len)
755 SETUP(args, "chain", 5, 0);
757 args.virt = IN(virt);
758 args.size = IN(size);
759 args.entry = IN(entry);
766 OF_chain(void *virt, u_int size, void (*entry)(), void *arg, u_int len)
769 * This is a REALLY dirty hack till the firmware gets this going
773 OF_release(virt, size);
775 ((int (*)(u_long, u_long, u_long, void *, u_long))entry)
776 (0, 0, (u_long)openfirmware, arg, len);