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/param.h>
62 #include <sys/kernel.h>
63 #include <sys/malloc.h>
64 #include <sys/systm.h>
66 #include <machine/stdarg.h>
68 #include <dev/ofw/ofwvar.h>
69 #include <dev/ofw/openfirm.h>
73 static void ofw_std_init(ofw_t ofw, void *openfirm);
74 static int ofw_std_test(ofw_t ofw, const char *name);
75 static int ofw_std_interpret(ofw_t ofw, const char *cmd, int nreturns,
76 unsigned long *returns);
77 static phandle_t ofw_std_peer(ofw_t ofw, phandle_t node);
78 static phandle_t ofw_std_child(ofw_t ofw, phandle_t node);
79 static phandle_t ofw_std_parent(ofw_t ofw, phandle_t node);
80 static phandle_t ofw_std_instance_to_package(ofw_t ofw, ihandle_t instance);
81 static ssize_t ofw_std_getproplen(ofw_t ofw, phandle_t package,
82 const char *propname);
83 static ssize_t ofw_std_getprop(ofw_t ofw, phandle_t package,
84 const char *propname, void *buf, size_t buflen);
85 static int ofw_std_nextprop(ofw_t ofw, phandle_t package, const char *previous,
87 static int ofw_std_setprop(ofw_t ofw, phandle_t package, const char *propname,
88 const void *buf, size_t len);
89 static ssize_t ofw_std_canon(ofw_t ofw, const char *device, char *buf,
91 static phandle_t ofw_std_finddevice(ofw_t ofw, const char *device);
92 static ssize_t ofw_std_instance_to_path(ofw_t ofw, ihandle_t instance,
93 char *buf, size_t len);
94 static ssize_t ofw_std_package_to_path(ofw_t ofw, phandle_t package, char *buf,
96 static int ofw_std_call_method(ofw_t ofw, ihandle_t instance,
97 const char *method, int nargs, int nreturns,
98 unsigned long *args_and_returns);
99 static ihandle_t ofw_std_open(ofw_t ofw, const char *device);
100 static void ofw_std_close(ofw_t ofw, ihandle_t instance);
101 static ssize_t ofw_std_read(ofw_t ofw, ihandle_t instance, void *addr,
103 static ssize_t ofw_std_write(ofw_t ofw, ihandle_t instance, const void *addr,
105 static int ofw_std_seek(ofw_t ofw, ihandle_t instance, uint64_t pos);
106 static caddr_t ofw_std_claim(ofw_t ofw, void *virt, size_t size, u_int align);
107 static void ofw_std_release(ofw_t ofw, void *virt, size_t size);
108 static void ofw_std_enter(ofw_t ofw);
109 static void ofw_std_exit(ofw_t ofw);
111 static ofw_method_t ofw_std_methods[] = {
112 OFWMETHOD(ofw_init, ofw_std_init),
113 OFWMETHOD(ofw_peer, ofw_std_peer),
114 OFWMETHOD(ofw_child, ofw_std_child),
115 OFWMETHOD(ofw_parent, ofw_std_parent),
116 OFWMETHOD(ofw_instance_to_package, ofw_std_instance_to_package),
117 OFWMETHOD(ofw_getproplen, ofw_std_getproplen),
118 OFWMETHOD(ofw_getprop, ofw_std_getprop),
119 OFWMETHOD(ofw_nextprop, ofw_std_nextprop),
120 OFWMETHOD(ofw_setprop, ofw_std_setprop),
121 OFWMETHOD(ofw_canon, ofw_std_canon),
122 OFWMETHOD(ofw_finddevice, ofw_std_finddevice),
123 OFWMETHOD(ofw_instance_to_path, ofw_std_instance_to_path),
124 OFWMETHOD(ofw_package_to_path, ofw_std_package_to_path),
126 OFWMETHOD(ofw_test, ofw_std_test),
127 OFWMETHOD(ofw_call_method, ofw_std_call_method),
128 OFWMETHOD(ofw_interpret, ofw_std_interpret),
129 OFWMETHOD(ofw_open, ofw_std_open),
130 OFWMETHOD(ofw_close, ofw_std_close),
131 OFWMETHOD(ofw_read, ofw_std_read),
132 OFWMETHOD(ofw_write, ofw_std_write),
133 OFWMETHOD(ofw_seek, ofw_std_seek),
134 OFWMETHOD(ofw_claim, ofw_std_claim),
135 OFWMETHOD(ofw_release, ofw_std_release),
136 OFWMETHOD(ofw_enter, ofw_std_enter),
137 OFWMETHOD(ofw_exit, ofw_std_exit),
142 static ofw_def_t ofw_std = {
149 static int (*openfirmware)(void *);
154 ofw_std_init(ofw_t ofw, void *openfirm)
157 openfirmware = (int (*)(void *))openfirm;
164 /* Test to see if a service exists. */
166 ofw_std_test(ofw_t ofw, const char *name)
180 args.service = (cell_t)name;
181 if (openfirmware(&args) == -1)
183 return (args.missing);
187 ofw_std_interpret(ofw_t ofw, const char *cmd, int nreturns,
188 unsigned long *returns)
202 args.nreturns = ++nreturns;
203 args.slot[i++] = (cell_t)cmd;
204 if (openfirmware(&args) == -1)
206 status = args.slot[i++];
207 while (i < 1 + nreturns)
208 returns[j++] = args.slot[i++];
213 * Device tree functions
216 /* Return the next sibling of this node or 0. */
218 ofw_std_peer(ofw_t ofw, phandle_t node)
233 if (openfirmware(&args) == -1)
238 /* Return the first child of this node or 0. */
240 ofw_std_child(ofw_t ofw, phandle_t node)
255 if (openfirmware(&args) == -1)
260 /* Return the parent of this node or 0. */
262 ofw_std_parent(ofw_t ofw, phandle_t node)
277 if (openfirmware(&args) == -1)
279 return (args.parent);
282 /* Return the package handle that corresponds to an instance handle. */
284 ofw_std_instance_to_package(ofw_t ofw, ihandle_t instance)
293 (cell_t)"instance-to-package",
298 args.instance = instance;
299 if (openfirmware(&args) == -1)
301 return (args.package);
304 /* Get the length of a property of a package. */
306 ofw_std_getproplen(ofw_t ofw, phandle_t package, const char *propname)
316 (cell_t)"getproplen",
321 args.package = package;
322 args.propname = (cell_t)propname;
323 if (openfirmware(&args) == -1)
325 return (args.proplen);
328 /* Get the value of a property of a package. */
330 ofw_std_getprop(ofw_t ofw, phandle_t package, const char *propname, void *buf,
348 args.package = package;
349 args.propname = (cell_t)propname;
350 args.buf = (cell_t)buf;
351 args.buflen = buflen;
352 if (openfirmware(&args) == -1)
357 /* Get the next property of a package. */
359 ofw_std_nextprop(ofw_t ofw, phandle_t package, const char *previous, char *buf,
376 args.package = package;
377 args.previous = (cell_t)previous;
378 args.buf = (cell_t)buf;
379 if (openfirmware(&args) == -1)
384 /* Set the value of a property of a package. */
385 /* XXX Has a bug on FirePower */
387 ofw_std_setprop(ofw_t ofw, phandle_t package, const char *propname,
388 const void *buf, size_t len)
405 args.package = package;
406 args.propname = (cell_t)propname;
407 args.buf = (cell_t)buf;
409 if (openfirmware(&args) == -1)
414 /* Convert a device specifier to a fully qualified pathname. */
416 ofw_std_canon(ofw_t ofw, const char *device, char *buf, size_t len)
432 args.device = (cell_t)device;
433 args.buf = (cell_t)buf;
435 if (openfirmware(&args) == -1)
440 /* Return a package handle for the specified device. */
442 ofw_std_finddevice(ofw_t ofw, const char *device)
451 (cell_t)"finddevice",
456 args.device = (cell_t)device;
457 if (openfirmware(&args) == -1)
459 return (args.package);
462 /* Return the fully qualified pathname corresponding to an instance. */
464 ofw_std_instance_to_path(ofw_t ofw, ihandle_t instance, char *buf, size_t len)
475 (cell_t)"instance-to-path",
480 args.instance = instance;
481 args.buf = (cell_t)buf;
483 if (openfirmware(&args) == -1)
488 /* Return the fully qualified pathname corresponding to a package. */
490 ofw_std_package_to_path(ofw_t ofw, phandle_t package, char *buf, size_t len)
501 (cell_t)"package-to-path",
506 args.package = package;
507 args.buf = (cell_t)buf;
509 if (openfirmware(&args) == -1)
514 /* Call the method in the scope of a given instance. */
516 ofw_std_call_method(ofw_t ofw, ihandle_t instance, const char *method,
517 int nargs, int nreturns, unsigned long *args_and_returns)
525 cell_t args_n_results[12];
527 (cell_t)"call-method",
537 args.nargs = nargs + 2;
538 args.nreturns = nreturns + 1;
539 args.method = (cell_t)method;
540 args.instance = instance;
542 ap = args_and_returns;
543 for (cp = args.args_n_results + (n = nargs); --n >= 0;)
545 if (openfirmware(&args) == -1)
547 if (args.args_n_results[nargs])
548 return (args.args_n_results[nargs]);
549 for (cp = args.args_n_results + nargs + (n = args.nreturns); --n > 0;)
555 * Device I/O functions
558 /* Open an instance for a device. */
560 ofw_std_open(ofw_t ofw, const char *device)
574 args.device = (cell_t)device;
575 if (openfirmware(&args) == -1 || args.instance == 0)
577 return (args.instance);
580 /* Close an instance. */
582 ofw_std_close(ofw_t ofw, ihandle_t instance)
594 args.instance = instance;
598 /* Read from an instance. */
600 ofw_std_read(ofw_t ofw, ihandle_t instance, void *addr, size_t len)
616 args.instance = instance;
617 args.addr = (cell_t)addr;
619 if (openfirmware(&args) == -1)
622 return (args.actual);
625 /* Write to an instance. */
627 ofw_std_write(ofw_t ofw, ihandle_t instance, const void *addr, size_t len)
643 args.instance = instance;
644 args.addr = (cell_t)addr;
646 if (openfirmware(&args) == -1)
648 return (args.actual);
651 /* Seek to a position. */
653 ofw_std_seek(ofw_t ofw, ihandle_t instance, uint64_t pos)
669 args.instance = instance;
670 args.poshi = pos >> 32;
672 if (openfirmware(&args) == -1)
674 return (args.status);
681 /* Claim an area of memory. */
683 ofw_std_claim(ofw_t ofw, void *virt, size_t size, u_int align)
699 args.virt = (cell_t)virt;
702 if (openfirmware(&args) == -1)
704 return ((void *)args.baseaddr);
707 /* Release an area of memory. */
709 ofw_std_release(ofw_t ofw, void *virt, size_t size)
722 args.virt = (cell_t)virt;
728 * Control transfer functions
731 /* Suspend and drop back to the Open Firmware interface. */
733 ofw_std_enter(ofw_t ofw)
744 /* We may come back. */
747 /* Shut down and drop back to the Open Firmware interface. */
749 ofw_std_exit(ofw_t ofw)
760 for (;;) /* just in case */