[ARM] 3646/1: ep93xx: instantiate ep93xx-ohci platform device
[linux-2.6/openmoko-kernel/knife-kernel.git] / arch / sparc64 / prom / tree.c
blob49075abd7cbc2d08394f47df7e71e906ff976fed
1 /* $Id: tree.c,v 1.10 1998/01/10 22:39:00 ecd Exp $
2 * tree.c: Basic device tree traversal/scanning for the Linux
3 * prom library.
5 * Copyright (C) 1995 David S. Miller (davem@caip.rutgers.edu)
6 * Copyright (C) 1996,1997 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
7 */
9 #include <linux/string.h>
10 #include <linux/types.h>
11 #include <linux/kernel.h>
12 #include <linux/sched.h>
14 #include <asm/openprom.h>
15 #include <asm/oplib.h>
17 /* Return the child of node 'node' or zero if no this node has no
18 * direct descendent.
20 __inline__ int
21 __prom_getchild(int node)
23 return p1275_cmd ("child", P1275_INOUT(1, 1), node);
26 __inline__ int
27 prom_getchild(int node)
29 int cnode;
31 if(node == -1) return 0;
32 cnode = __prom_getchild(node);
33 if(cnode == -1) return 0;
34 return (int)cnode;
37 __inline__ int
38 prom_getparent(int node)
40 int cnode;
42 if(node == -1) return 0;
43 cnode = p1275_cmd ("parent", P1275_INOUT(1, 1), node);
44 if(cnode == -1) return 0;
45 return (int)cnode;
48 /* Return the next sibling of node 'node' or zero if no more siblings
49 * at this level of depth in the tree.
51 __inline__ int
52 __prom_getsibling(int node)
54 return p1275_cmd(prom_peer_name, P1275_INOUT(1, 1), node);
57 __inline__ int
58 prom_getsibling(int node)
60 int sibnode;
62 if (node == -1)
63 return 0;
64 sibnode = __prom_getsibling(node);
65 if (sibnode == -1)
66 return 0;
68 return sibnode;
71 /* Return the length in bytes of property 'prop' at node 'node'.
72 * Return -1 on error.
74 __inline__ int
75 prom_getproplen(int node, const char *prop)
77 if((!node) || (!prop)) return -1;
78 return p1275_cmd ("getproplen",
79 P1275_ARG(1,P1275_ARG_IN_STRING)|
80 P1275_INOUT(2, 1),
81 node, prop);
84 /* Acquire a property 'prop' at node 'node' and place it in
85 * 'buffer' which has a size of 'bufsize'. If the acquisition
86 * was successful the length will be returned, else -1 is returned.
88 __inline__ int
89 prom_getproperty(int node, const char *prop, char *buffer, int bufsize)
91 int plen;
93 plen = prom_getproplen(node, prop);
94 if ((plen > bufsize) || (plen == 0) || (plen == -1)) {
95 return -1;
96 } else {
97 /* Ok, things seem all right. */
98 return p1275_cmd(prom_getprop_name,
99 P1275_ARG(1,P1275_ARG_IN_STRING)|
100 P1275_ARG(2,P1275_ARG_OUT_BUF)|
101 P1275_INOUT(4, 1),
102 node, prop, buffer, P1275_SIZE(plen));
106 /* Acquire an integer property and return its value. Returns -1
107 * on failure.
109 __inline__ int
110 prom_getint(int node, const char *prop)
112 int intprop;
114 if(prom_getproperty(node, prop, (char *) &intprop, sizeof(int)) != -1)
115 return intprop;
117 return -1;
120 /* Acquire an integer property, upon error return the passed default
121 * integer.
125 prom_getintdefault(int node, const char *property, int deflt)
127 int retval;
129 retval = prom_getint(node, property);
130 if(retval == -1) return deflt;
132 return retval;
135 /* Acquire a boolean property, 1=TRUE 0=FALSE. */
137 prom_getbool(int node, const char *prop)
139 int retval;
141 retval = prom_getproplen(node, prop);
142 if(retval == -1) return 0;
143 return 1;
146 /* Acquire a property whose value is a string, returns a null
147 * string on error. The char pointer is the user supplied string
148 * buffer.
150 void
151 prom_getstring(int node, const char *prop, char *user_buf, int ubuf_size)
153 int len;
155 len = prom_getproperty(node, prop, user_buf, ubuf_size);
156 if(len != -1) return;
157 user_buf[0] = 0;
158 return;
162 /* Does the device at node 'node' have name 'name'?
163 * YES = 1 NO = 0
166 prom_nodematch(int node, const char *name)
168 char namebuf[128];
169 prom_getproperty(node, "name", namebuf, sizeof(namebuf));
170 if(strcmp(namebuf, name) == 0) return 1;
171 return 0;
174 /* Search siblings at 'node_start' for a node with name
175 * 'nodename'. Return node if successful, zero if not.
178 prom_searchsiblings(int node_start, const char *nodename)
181 int thisnode, error;
182 char promlib_buf[128];
184 for(thisnode = node_start; thisnode;
185 thisnode=prom_getsibling(thisnode)) {
186 error = prom_getproperty(thisnode, "name", promlib_buf,
187 sizeof(promlib_buf));
188 /* Should this ever happen? */
189 if(error == -1) continue;
190 if(strcmp(nodename, promlib_buf)==0) return thisnode;
193 return 0;
196 /* Gets name in the {name@x,yyyyy|name (if no reg)} form */
197 int
198 prom_getname (int node, char *buffer, int len)
200 int i, sbus = 0;
201 int pci = 0, ebus = 0, ide = 0;
202 struct linux_prom_registers *reg;
203 struct linux_prom64_registers reg64[PROMREG_MAX];
205 for (sbus = prom_getparent (node); sbus; sbus = prom_getparent (sbus)) {
206 i = prom_getproperty (sbus, "name", buffer, len);
207 if (i > 0) {
208 buffer [i] = 0;
209 if (!strcmp (buffer, "sbus"))
210 goto getit;
213 if ((pci = prom_getparent (node))) {
214 i = prom_getproperty (pci, "name", buffer, len);
215 if (i > 0) {
216 buffer [i] = 0;
217 if (!strcmp (buffer, "pci"))
218 goto getit;
220 pci = 0;
222 if ((ebus = prom_getparent (node))) {
223 i = prom_getproperty (ebus, "name", buffer, len);
224 if (i > 0) {
225 buffer[i] = 0;
226 if (!strcmp (buffer, "ebus"))
227 goto getit;
229 ebus = 0;
231 if ((ide = prom_getparent (node))) {
232 i = prom_getproperty (ide, "name", buffer, len);
233 if (i > 0) {
234 buffer [i] = 0;
235 if (!strcmp (buffer, "ide"))
236 goto getit;
238 ide = 0;
240 getit:
241 i = prom_getproperty (node, "name", buffer, len);
242 if (i <= 0) {
243 buffer [0] = 0;
244 return -1;
246 buffer [i] = 0;
247 len -= i;
248 i = prom_getproperty (node, "reg", (char *)reg64, sizeof (reg64));
249 if (i <= 0) return 0;
250 if (len < 16) return -1;
251 buffer = strchr (buffer, 0);
252 if (sbus) {
253 reg = (struct linux_prom_registers *)reg64;
254 sprintf (buffer, "@%x,%x", reg[0].which_io, (uint)reg[0].phys_addr);
255 } else if (pci) {
256 int dev, fn;
257 reg = (struct linux_prom_registers *)reg64;
258 fn = (reg[0].which_io >> 8) & 0x07;
259 dev = (reg[0].which_io >> 11) & 0x1f;
260 if (fn)
261 sprintf (buffer, "@%x,%x", dev, fn);
262 else
263 sprintf (buffer, "@%x", dev);
264 } else if (ebus) {
265 reg = (struct linux_prom_registers *)reg64;
266 sprintf (buffer, "@%x,%x", reg[0].which_io, reg[0].phys_addr);
267 } else if (ide) {
268 reg = (struct linux_prom_registers *)reg64;
269 sprintf (buffer, "@%x,%x", reg[0].which_io, reg[0].phys_addr);
270 } else if (i == 4) { /* Happens on 8042's children on Ultra/PCI. */
271 reg = (struct linux_prom_registers *)reg64;
272 sprintf (buffer, "@%x", reg[0].which_io);
273 } else {
274 sprintf (buffer, "@%x,%x",
275 (unsigned int)(reg64[0].phys_addr >> 36),
276 (unsigned int)(reg64[0].phys_addr));
278 return 0;
281 /* Return the first property type for node 'node'.
282 * buffer should be at least 32B in length
284 __inline__ char *
285 prom_firstprop(int node, char *buffer)
287 *buffer = 0;
288 if(node == -1) return buffer;
289 p1275_cmd ("nextprop", P1275_ARG(2,P1275_ARG_OUT_32B)|
290 P1275_INOUT(3, 0),
291 node, (char *) 0x0, buffer);
292 return buffer;
295 /* Return the property type string after property type 'oprop'
296 * at node 'node' . Returns NULL string if no more
297 * property types for this node.
299 __inline__ char *
300 prom_nextprop(int node, const char *oprop, char *buffer)
302 char buf[32];
304 if(node == -1) {
305 *buffer = 0;
306 return buffer;
308 if (oprop == buffer) {
309 strcpy (buf, oprop);
310 oprop = buf;
312 p1275_cmd ("nextprop", P1275_ARG(1,P1275_ARG_IN_STRING)|
313 P1275_ARG(2,P1275_ARG_OUT_32B)|
314 P1275_INOUT(3, 0),
315 node, oprop, buffer);
316 return buffer;
320 prom_finddevice(const char *name)
322 if (!name)
323 return 0;
324 return p1275_cmd(prom_finddev_name,
325 P1275_ARG(0,P1275_ARG_IN_STRING)|
326 P1275_INOUT(1, 1),
327 name);
330 int prom_node_has_property(int node, const char *prop)
332 char buf [32];
334 *buf = 0;
335 do {
336 prom_nextprop(node, buf, buf);
337 if(!strcmp(buf, prop))
338 return 1;
339 } while (*buf);
340 return 0;
343 /* Set property 'pname' at node 'node' to value 'value' which has a length
344 * of 'size' bytes. Return the number of bytes the prom accepted.
347 prom_setprop(int node, const char *pname, char *value, int size)
349 if(size == 0) return 0;
350 if((pname == 0) || (value == 0)) return 0;
352 return p1275_cmd ("setprop", P1275_ARG(1,P1275_ARG_IN_STRING)|
353 P1275_ARG(2,P1275_ARG_IN_BUF)|
354 P1275_INOUT(4, 1),
355 node, pname, value, P1275_SIZE(size));
358 __inline__ int
359 prom_inst2pkg(int inst)
361 int node;
363 node = p1275_cmd ("instance-to-package", P1275_INOUT(1, 1), inst);
364 if (node == -1) return 0;
365 return node;
368 /* Return 'node' assigned to a particular prom 'path'
369 * FIXME: Should work for v0 as well
372 prom_pathtoinode(const char *path)
374 int node, inst;
376 inst = prom_devopen (path);
377 if (inst == 0) return 0;
378 node = prom_inst2pkg (inst);
379 prom_devclose (inst);
380 if (node == -1) return 0;
381 return node;