blob: 9d3f9137a43ae1307ea698d81e13896af7504453 [file] [log] [blame]
Adrian Bunkb00dc832008-05-19 16:52:27 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * tree.c: Basic device tree traversal/scanning for the Linux
3 * prom library.
4 *
5 * Copyright (C) 1995 David S. Miller (davem@caip.rutgers.edu)
6 * Copyright (C) 1996,1997 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
7 */
8
9#include <linux/string.h>
10#include <linux/types.h>
11#include <linux/kernel.h>
12#include <linux/sched.h>
Sam Ravnborg917c3662009-01-08 16:58:20 -080013#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014
15#include <asm/openprom.h>
16#include <asm/oplib.h>
David S. Millerb3e13fb2007-07-12 15:55:55 -070017#include <asm/ldc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
David S. Miller25edd692010-08-23 23:10:57 -070019static int prom_node_to_node(const char *type, int node)
20{
21 unsigned long args[5];
22
23 args[0] = (unsigned long) type;
24 args[1] = 1;
25 args[2] = 1;
26 args[3] = (unsigned int) node;
27 args[4] = (unsigned long) -1;
28
29 p1275_cmd_direct(args);
30
31 return (int) args[4];
32}
33
Linus Torvalds1da177e2005-04-16 15:20:36 -070034/* Return the child of node 'node' or zero if no this node has no
35 * direct descendent.
36 */
David S. Millerd979f172007-10-27 00:13:04 -070037inline int __prom_getchild(int node)
Linus Torvalds1da177e2005-04-16 15:20:36 -070038{
David S. Miller25edd692010-08-23 23:10:57 -070039 return prom_node_to_node("child", node);
Linus Torvalds1da177e2005-04-16 15:20:36 -070040}
41
David S. Millerd979f172007-10-27 00:13:04 -070042inline int prom_getchild(int node)
Linus Torvalds1da177e2005-04-16 15:20:36 -070043{
44 int cnode;
45
David S. Miller25edd692010-08-23 23:10:57 -070046 if (node == -1)
47 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 cnode = __prom_getchild(node);
David S. Miller25edd692010-08-23 23:10:57 -070049 if (cnode == -1)
50 return 0;
51 return cnode;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052}
Sam Ravnborg917c3662009-01-08 16:58:20 -080053EXPORT_SYMBOL(prom_getchild);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
David S. Millerd979f172007-10-27 00:13:04 -070055inline int prom_getparent(int node)
Linus Torvalds1da177e2005-04-16 15:20:36 -070056{
57 int cnode;
58
David S. Miller25edd692010-08-23 23:10:57 -070059 if (node == -1)
60 return 0;
61 cnode = prom_node_to_node("parent", node);
62 if (cnode == -1)
63 return 0;
64 return cnode;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065}
66
67/* Return the next sibling of node 'node' or zero if no more siblings
68 * at this level of depth in the tree.
69 */
David S. Millerd979f172007-10-27 00:13:04 -070070inline int __prom_getsibling(int node)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071{
David S. Miller25edd692010-08-23 23:10:57 -070072 return prom_node_to_node(prom_peer_name, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073}
74
David S. Millerd979f172007-10-27 00:13:04 -070075inline int prom_getsibling(int node)
Linus Torvalds1da177e2005-04-16 15:20:36 -070076{
77 int sibnode;
78
David S. Millerd82ace72006-02-09 02:52:44 -080079 if (node == -1)
80 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 sibnode = __prom_getsibling(node);
David S. Millerd82ace72006-02-09 02:52:44 -080082 if (sibnode == -1)
83 return 0;
84
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 return sibnode;
86}
Sam Ravnborg917c3662009-01-08 16:58:20 -080087EXPORT_SYMBOL(prom_getsibling);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
89/* Return the length in bytes of property 'prop' at node 'node'.
90 * Return -1 on error.
91 */
David S. Millerd979f172007-10-27 00:13:04 -070092inline int prom_getproplen(int node, const char *prop)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093{
David S. Miller25edd692010-08-23 23:10:57 -070094 unsigned long args[6];
95
96 if (!node || !prop)
97 return -1;
98
99 args[0] = (unsigned long) "getproplen";
100 args[1] = 2;
101 args[2] = 1;
102 args[3] = (unsigned int) node;
103 args[4] = (unsigned long) prop;
104 args[5] = (unsigned long) -1;
105
106 p1275_cmd_direct(args);
107
108 return (int) args[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109}
Sam Ravnborg917c3662009-01-08 16:58:20 -0800110EXPORT_SYMBOL(prom_getproplen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
112/* Acquire a property 'prop' at node 'node' and place it in
113 * 'buffer' which has a size of 'bufsize'. If the acquisition
114 * was successful the length will be returned, else -1 is returned.
115 */
David S. Millerd979f172007-10-27 00:13:04 -0700116inline int prom_getproperty(int node, const char *prop,
117 char *buffer, int bufsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118{
David S. Miller25edd692010-08-23 23:10:57 -0700119 unsigned long args[8];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 int plen;
121
122 plen = prom_getproplen(node, prop);
David S. Miller25edd692010-08-23 23:10:57 -0700123 if ((plen > bufsize) || (plen == 0) || (plen == -1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 return -1;
David S. Miller25edd692010-08-23 23:10:57 -0700125
126 args[0] = (unsigned long) prom_getprop_name;
127 args[1] = 4;
128 args[2] = 1;
129 args[3] = (unsigned int) node;
130 args[4] = (unsigned long) prop;
131 args[5] = (unsigned long) buffer;
132 args[6] = bufsize;
133 args[7] = (unsigned long) -1;
134
135 p1275_cmd_direct(args);
136
137 return (int) args[7];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138}
Sam Ravnborg917c3662009-01-08 16:58:20 -0800139EXPORT_SYMBOL(prom_getproperty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
141/* Acquire an integer property and return its value. Returns -1
142 * on failure.
143 */
David S. Millerd979f172007-10-27 00:13:04 -0700144inline int prom_getint(int node, const char *prop)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145{
146 int intprop;
147
David S. Miller25edd692010-08-23 23:10:57 -0700148 if (prom_getproperty(node, prop, (char *) &intprop, sizeof(int)) != -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 return intprop;
150
151 return -1;
152}
Sam Ravnborg917c3662009-01-08 16:58:20 -0800153EXPORT_SYMBOL(prom_getint);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
155/* Acquire an integer property, upon error return the passed default
156 * integer.
157 */
158
David S. Millerd979f172007-10-27 00:13:04 -0700159int prom_getintdefault(int node, const char *property, int deflt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160{
161 int retval;
162
163 retval = prom_getint(node, property);
David S. Miller25edd692010-08-23 23:10:57 -0700164 if (retval == -1)
165 return deflt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
167 return retval;
168}
Sam Ravnborg917c3662009-01-08 16:58:20 -0800169EXPORT_SYMBOL(prom_getintdefault);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
171/* Acquire a boolean property, 1=TRUE 0=FALSE. */
David S. Millerd979f172007-10-27 00:13:04 -0700172int prom_getbool(int node, const char *prop)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173{
174 int retval;
175
176 retval = prom_getproplen(node, prop);
David S. Miller25edd692010-08-23 23:10:57 -0700177 if (retval == -1)
178 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 return 1;
180}
Sam Ravnborg917c3662009-01-08 16:58:20 -0800181EXPORT_SYMBOL(prom_getbool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
183/* Acquire a property whose value is a string, returns a null
184 * string on error. The char pointer is the user supplied string
185 * buffer.
186 */
David S. Millerd979f172007-10-27 00:13:04 -0700187void prom_getstring(int node, const char *prop, char *user_buf, int ubuf_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188{
189 int len;
190
191 len = prom_getproperty(node, prop, user_buf, ubuf_size);
David S. Miller25edd692010-08-23 23:10:57 -0700192 if (len != -1)
193 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 user_buf[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195}
Sam Ravnborg917c3662009-01-08 16:58:20 -0800196EXPORT_SYMBOL(prom_getstring);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
198/* Does the device at node 'node' have name 'name'?
199 * YES = 1 NO = 0
200 */
David S. Millerd979f172007-10-27 00:13:04 -0700201int prom_nodematch(int node, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202{
203 char namebuf[128];
204 prom_getproperty(node, "name", namebuf, sizeof(namebuf));
David S. Miller25edd692010-08-23 23:10:57 -0700205 if (strcmp(namebuf, name) == 0)
206 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 return 0;
208}
209
210/* Search siblings at 'node_start' for a node with name
211 * 'nodename'. Return node if successful, zero if not.
212 */
David S. Millerd979f172007-10-27 00:13:04 -0700213int prom_searchsiblings(int node_start, const char *nodename)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214{
215
216 int thisnode, error;
217 char promlib_buf[128];
218
219 for(thisnode = node_start; thisnode;
220 thisnode=prom_getsibling(thisnode)) {
221 error = prom_getproperty(thisnode, "name", promlib_buf,
222 sizeof(promlib_buf));
223 /* Should this ever happen? */
224 if(error == -1) continue;
225 if(strcmp(nodename, promlib_buf)==0) return thisnode;
226 }
227
228 return 0;
229}
Sam Ravnborg917c3662009-01-08 16:58:20 -0800230EXPORT_SYMBOL(prom_searchsiblings);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
David S. Miller25edd692010-08-23 23:10:57 -0700232static const char *prom_nextprop_name = "nextprop";
233
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234/* Return the first property type for node 'node'.
235 * buffer should be at least 32B in length
236 */
David S. Millerd979f172007-10-27 00:13:04 -0700237inline char *prom_firstprop(int node, char *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238{
David S. Miller25edd692010-08-23 23:10:57 -0700239 unsigned long args[7];
240
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 *buffer = 0;
David S. Miller25edd692010-08-23 23:10:57 -0700242 if (node == -1)
243 return buffer;
244
245 args[0] = (unsigned long) prom_nextprop_name;
246 args[1] = 3;
247 args[2] = 1;
248 args[3] = (unsigned int) node;
249 args[4] = 0;
250 args[5] = (unsigned long) buffer;
251 args[6] = (unsigned long) -1;
252
253 p1275_cmd_direct(args);
254
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 return buffer;
256}
Sam Ravnborg917c3662009-01-08 16:58:20 -0800257EXPORT_SYMBOL(prom_firstprop);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
259/* Return the property type string after property type 'oprop'
260 * at node 'node' . Returns NULL string if no more
261 * property types for this node.
262 */
David S. Millerd979f172007-10-27 00:13:04 -0700263inline char *prom_nextprop(int node, const char *oprop, char *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264{
David S. Miller25edd692010-08-23 23:10:57 -0700265 unsigned long args[7];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 char buf[32];
267
David S. Miller25edd692010-08-23 23:10:57 -0700268 if (node == -1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 *buffer = 0;
270 return buffer;
271 }
272 if (oprop == buffer) {
273 strcpy (buf, oprop);
274 oprop = buf;
275 }
David S. Miller25edd692010-08-23 23:10:57 -0700276
277 args[0] = (unsigned long) prom_nextprop_name;
278 args[1] = 3;
279 args[2] = 1;
280 args[3] = (unsigned int) node;
281 args[4] = (unsigned long) oprop;
282 args[5] = (unsigned long) buffer;
283 args[6] = (unsigned long) -1;
284
285 p1275_cmd_direct(args);
286
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 return buffer;
288}
Sam Ravnborg917c3662009-01-08 16:58:20 -0800289EXPORT_SYMBOL(prom_nextprop);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
291int
David S. Millerbff06d52005-09-22 20:11:33 -0700292prom_finddevice(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293{
David S. Miller25edd692010-08-23 23:10:57 -0700294 unsigned long args[5];
295
David S. Millerbff06d52005-09-22 20:11:33 -0700296 if (!name)
297 return 0;
David S. Miller25edd692010-08-23 23:10:57 -0700298 args[0] = (unsigned long) "finddevice";
299 args[1] = 1;
300 args[2] = 1;
301 args[3] = (unsigned long) name;
302 args[4] = (unsigned long) -1;
303
304 p1275_cmd_direct(args);
305
306 return (int) args[4];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307}
Sam Ravnborg917c3662009-01-08 16:58:20 -0800308EXPORT_SYMBOL(prom_finddevice);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
David S. Millerbff06d52005-09-22 20:11:33 -0700310int prom_node_has_property(int node, const char *prop)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311{
312 char buf [32];
313
314 *buf = 0;
315 do {
316 prom_nextprop(node, buf, buf);
David S. Miller25edd692010-08-23 23:10:57 -0700317 if (!strcmp(buf, prop))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 return 1;
319 } while (*buf);
320 return 0;
321}
Sam Ravnborg917c3662009-01-08 16:58:20 -0800322EXPORT_SYMBOL(prom_node_has_property);
323
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324/* Set property 'pname' at node 'node' to value 'value' which has a length
325 * of 'size' bytes. Return the number of bytes the prom accepted.
326 */
327int
David S. Millerbff06d52005-09-22 20:11:33 -0700328prom_setprop(int node, const char *pname, char *value, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329{
David S. Miller25edd692010-08-23 23:10:57 -0700330 unsigned long args[8];
331
David S. Millerb3e13fb2007-07-12 15:55:55 -0700332 if (size == 0)
333 return 0;
334 if ((pname == 0) || (value == 0))
335 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
David S. Millerb3e13fb2007-07-12 15:55:55 -0700337#ifdef CONFIG_SUN_LDOMS
338 if (ldom_domaining_enabled) {
339 ldom_set_var(pname, value);
340 return 0;
341 }
342#endif
David S. Miller25edd692010-08-23 23:10:57 -0700343 args[0] = (unsigned long) "setprop";
344 args[1] = 4;
345 args[2] = 1;
346 args[3] = (unsigned int) node;
347 args[4] = (unsigned long) pname;
348 args[5] = (unsigned long) value;
349 args[6] = size;
350 args[7] = (unsigned long) -1;
351
352 p1275_cmd_direct(args);
353
354 return (int) args[7];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355}
Sam Ravnborg917c3662009-01-08 16:58:20 -0800356EXPORT_SYMBOL(prom_setprop);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
David S. Millerd979f172007-10-27 00:13:04 -0700358inline int prom_inst2pkg(int inst)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359{
David S. Miller25edd692010-08-23 23:10:57 -0700360 unsigned long args[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 int node;
362
David S. Miller25edd692010-08-23 23:10:57 -0700363 args[0] = (unsigned long) "instance-to-package";
364 args[1] = 1;
365 args[2] = 1;
366 args[3] = (unsigned int) inst;
367 args[4] = (unsigned long) -1;
368
369 p1275_cmd_direct(args);
370
371 node = (int) args[4];
372 if (node == -1)
373 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 return node;
375}
376
377/* Return 'node' assigned to a particular prom 'path'
378 * FIXME: Should work for v0 as well
379 */
380int
David S. Millerbff06d52005-09-22 20:11:33 -0700381prom_pathtoinode(const char *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382{
383 int node, inst;
384
385 inst = prom_devopen (path);
David S. Miller25edd692010-08-23 23:10:57 -0700386 if (inst == 0)
387 return 0;
388 node = prom_inst2pkg(inst);
389 prom_devclose(inst);
390 if (node == -1)
391 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 return node;
393}
David S. Millerc73fcc82007-07-20 16:59:26 -0700394
395int prom_ihandle2path(int handle, char *buffer, int bufsize)
396{
David S. Miller25edd692010-08-23 23:10:57 -0700397 unsigned long args[7];
398
399 args[0] = (unsigned long) "instance-to-path";
400 args[1] = 3;
401 args[2] = 1;
402 args[3] = (unsigned int) handle;
403 args[4] = (unsigned long) buffer;
404 args[5] = bufsize;
405 args[6] = (unsigned long) -1;
406
407 p1275_cmd_direct(args);
408
409 return (int) args[6];
David S. Millerc73fcc82007-07-20 16:59:26 -0700410}