]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - arch/sparc/kernel/prom.c
Consolidate of_get_parent
[karo-tx-linux.git] / arch / sparc / kernel / prom.c
1 /*
2  * Procedures for creating, accessing and interpreting the device tree.
3  *
4  * Paul Mackerras       August 1996.
5  * Copyright (C) 1996-2005 Paul Mackerras.
6  * 
7  *  Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner.
8  *    {engebret|bergner}@us.ibm.com 
9  *
10  *  Adapted for sparc32 by David S. Miller davem@davemloft.net
11  *
12  *      This program is free software; you can redistribute it and/or
13  *      modify it under the terms of the GNU General Public License
14  *      as published by the Free Software Foundation; either version
15  *      2 of the License, or (at your option) any later version.
16  */
17
18 #include <linux/kernel.h>
19 #include <linux/types.h>
20 #include <linux/string.h>
21 #include <linux/mm.h>
22 #include <linux/bootmem.h>
23 #include <linux/module.h>
24
25 #include <asm/prom.h>
26 #include <asm/oplib.h>
27
28 static struct device_node *allnodes;
29
30 extern rwlock_t devtree_lock;   /* temporary while merging */
31
32 struct device_node *of_get_next_child(const struct device_node *node,
33         struct device_node *prev)
34 {
35         struct device_node *next;
36
37         next = prev ? prev->sibling : node->child;
38         for (; next != 0; next = next->sibling) {
39                 break;
40         }
41
42         return next;
43 }
44 EXPORT_SYMBOL(of_get_next_child);
45
46 struct device_node *of_find_node_by_path(const char *path)
47 {
48         struct device_node *np = allnodes;
49
50         for (; np != 0; np = np->allnext) {
51                 if (np->full_name != 0 && strcmp(np->full_name, path) == 0)
52                         break;
53         }
54
55         return np;
56 }
57 EXPORT_SYMBOL(of_find_node_by_path);
58
59 struct device_node *of_find_node_by_phandle(phandle handle)
60 {
61         struct device_node *np;
62
63         for (np = allnodes; np != 0; np = np->allnext)
64                 if (np->node == handle)
65                         break;
66
67         return np;
68 }
69 EXPORT_SYMBOL(of_find_node_by_phandle);
70
71 struct device_node *of_find_node_by_name(struct device_node *from,
72         const char *name)
73 {
74         struct device_node *np;
75
76         np = from ? from->allnext : allnodes;
77         for (; np != NULL; np = np->allnext)
78                 if (np->name != NULL && strcmp(np->name, name) == 0)
79                         break;
80
81         return np;
82 }
83 EXPORT_SYMBOL(of_find_node_by_name);
84
85 struct device_node *of_find_node_by_type(struct device_node *from,
86         const char *type)
87 {
88         struct device_node *np;
89
90         np = from ? from->allnext : allnodes;
91         for (; np != 0; np = np->allnext)
92                 if (np->type != 0 && strcmp(np->type, type) == 0)
93                         break;
94
95         return np;
96 }
97 EXPORT_SYMBOL(of_find_node_by_type);
98
99 struct device_node *of_find_compatible_node(struct device_node *from,
100         const char *type, const char *compatible)
101 {
102         struct device_node *np;
103
104         np = from ? from->allnext : allnodes;
105         for (; np != 0; np = np->allnext) {
106                 if (type != NULL
107                     && !(np->type != 0 && strcmp(np->type, type) == 0))
108                         continue;
109                 if (of_device_is_compatible(np, compatible))
110                         break;
111         }
112
113         return np;
114 }
115 EXPORT_SYMBOL(of_find_compatible_node);
116
117 int of_getintprop_default(struct device_node *np, const char *name, int def)
118 {
119         struct property *prop;
120         int len;
121
122         prop = of_find_property(np, name, &len);
123         if (!prop || len != 4)
124                 return def;
125
126         return *(int *) prop->value;
127 }
128 EXPORT_SYMBOL(of_getintprop_default);
129
130 int of_set_property(struct device_node *dp, const char *name, void *val, int len)
131 {
132         struct property **prevp;
133         void *new_val;
134         int err;
135
136         new_val = kmalloc(len, GFP_KERNEL);
137         if (!new_val)
138                 return -ENOMEM;
139
140         memcpy(new_val, val, len);
141
142         err = -ENODEV;
143
144         write_lock(&devtree_lock);
145         prevp = &dp->properties;
146         while (*prevp) {
147                 struct property *prop = *prevp;
148
149                 if (!strcasecmp(prop->name, name)) {
150                         void *old_val = prop->value;
151                         int ret;
152
153                         ret = prom_setprop(dp->node, (char *) name, val, len);
154                         err = -EINVAL;
155                         if (ret >= 0) {
156                                 prop->value = new_val;
157                                 prop->length = len;
158
159                                 if (OF_IS_DYNAMIC(prop))
160                                         kfree(old_val);
161
162                                 OF_MARK_DYNAMIC(prop);
163
164                                 err = 0;
165                         }
166                         break;
167                 }
168                 prevp = &(*prevp)->next;
169         }
170         write_unlock(&devtree_lock);
171
172         /* XXX Upate procfs if necessary... */
173
174         return err;
175 }
176 EXPORT_SYMBOL(of_set_property);
177
178 static unsigned int prom_early_allocated;
179
180 static void * __init prom_early_alloc(unsigned long size)
181 {
182         void *ret;
183
184         ret = __alloc_bootmem(size, SMP_CACHE_BYTES, 0UL);
185         if (ret != NULL)
186                 memset(ret, 0, size);
187
188         prom_early_allocated += size;
189
190         return ret;
191 }
192
193 static int is_root_node(const struct device_node *dp)
194 {
195         if (!dp)
196                 return 0;
197
198         return (dp->parent == NULL);
199 }
200
201 /* The following routines deal with the black magic of fully naming a
202  * node.
203  *
204  * Certain well known named nodes are just the simple name string.
205  *
206  * Actual devices have an address specifier appended to the base name
207  * string, like this "foo@addr".  The "addr" can be in any number of
208  * formats, and the platform plus the type of the node determine the
209  * format and how it is constructed.
210  *
211  * For children of the ROOT node, the naming convention is fixed and
212  * determined by whether this is a sun4u or sun4v system.
213  *
214  * For children of other nodes, it is bus type specific.  So
215  * we walk up the tree until we discover a "device_type" property
216  * we recognize and we go from there.
217  */
218 static void __init sparc32_path_component(struct device_node *dp, char *tmp_buf)
219 {
220         struct linux_prom_registers *regs;
221         struct property *rprop;
222
223         rprop = of_find_property(dp, "reg", NULL);
224         if (!rprop)
225                 return;
226
227         regs = rprop->value;
228         sprintf(tmp_buf, "%s@%x,%x",
229                 dp->name,
230                 regs->which_io, regs->phys_addr);
231 }
232
233 /* "name@slot,offset"  */
234 static void __init sbus_path_component(struct device_node *dp, char *tmp_buf)
235 {
236         struct linux_prom_registers *regs;
237         struct property *prop;
238
239         prop = of_find_property(dp, "reg", NULL);
240         if (!prop)
241                 return;
242
243         regs = prop->value;
244         sprintf(tmp_buf, "%s@%x,%x",
245                 dp->name,
246                 regs->which_io,
247                 regs->phys_addr);
248 }
249
250 /* "name@devnum[,func]" */
251 static void __init pci_path_component(struct device_node *dp, char *tmp_buf)
252 {
253         struct linux_prom_pci_registers *regs;
254         struct property *prop;
255         unsigned int devfn;
256
257         prop = of_find_property(dp, "reg", NULL);
258         if (!prop)
259                 return;
260
261         regs = prop->value;
262         devfn = (regs->phys_hi >> 8) & 0xff;
263         if (devfn & 0x07) {
264                 sprintf(tmp_buf, "%s@%x,%x",
265                         dp->name,
266                         devfn >> 3,
267                         devfn & 0x07);
268         } else {
269                 sprintf(tmp_buf, "%s@%x",
270                         dp->name,
271                         devfn >> 3);
272         }
273 }
274
275 /* "name@addrhi,addrlo" */
276 static void __init ebus_path_component(struct device_node *dp, char *tmp_buf)
277 {
278         struct linux_prom_registers *regs;
279         struct property *prop;
280
281         prop = of_find_property(dp, "reg", NULL);
282         if (!prop)
283                 return;
284
285         regs = prop->value;
286
287         sprintf(tmp_buf, "%s@%x,%x",
288                 dp->name,
289                 regs->which_io, regs->phys_addr);
290 }
291
292 static void __init __build_path_component(struct device_node *dp, char *tmp_buf)
293 {
294         struct device_node *parent = dp->parent;
295
296         if (parent != NULL) {
297                 if (!strcmp(parent->type, "pci") ||
298                     !strcmp(parent->type, "pciex"))
299                         return pci_path_component(dp, tmp_buf);
300                 if (!strcmp(parent->type, "sbus"))
301                         return sbus_path_component(dp, tmp_buf);
302                 if (!strcmp(parent->type, "ebus"))
303                         return ebus_path_component(dp, tmp_buf);
304
305                 /* "isa" is handled with platform naming */
306         }
307
308         /* Use platform naming convention.  */
309         return sparc32_path_component(dp, tmp_buf);
310 }
311
312 static char * __init build_path_component(struct device_node *dp)
313 {
314         char tmp_buf[64], *n;
315
316         tmp_buf[0] = '\0';
317         __build_path_component(dp, tmp_buf);
318         if (tmp_buf[0] == '\0')
319                 strcpy(tmp_buf, dp->name);
320
321         n = prom_early_alloc(strlen(tmp_buf) + 1);
322         strcpy(n, tmp_buf);
323
324         return n;
325 }
326
327 static char * __init build_full_name(struct device_node *dp)
328 {
329         int len, ourlen, plen;
330         char *n;
331
332         plen = strlen(dp->parent->full_name);
333         ourlen = strlen(dp->path_component_name);
334         len = ourlen + plen + 2;
335
336         n = prom_early_alloc(len);
337         strcpy(n, dp->parent->full_name);
338         if (!is_root_node(dp->parent)) {
339                 strcpy(n + plen, "/");
340                 plen++;
341         }
342         strcpy(n + plen, dp->path_component_name);
343
344         return n;
345 }
346
347 static unsigned int unique_id;
348
349 static struct property * __init build_one_prop(phandle node, char *prev, char *special_name, void *special_val, int special_len)
350 {
351         static struct property *tmp = NULL;
352         struct property *p;
353         int len;
354         const char *name;
355
356         if (tmp) {
357                 p = tmp;
358                 memset(p, 0, sizeof(*p) + 32);
359                 tmp = NULL;
360         } else {
361                 p = prom_early_alloc(sizeof(struct property) + 32);
362                 p->unique_id = unique_id++;
363         }
364
365         p->name = (char *) (p + 1);
366         if (special_name) {
367                 strcpy(p->name, special_name);
368                 p->length = special_len;
369                 p->value = prom_early_alloc(special_len);
370                 memcpy(p->value, special_val, special_len);
371         } else {
372                 if (prev == NULL) {
373                         name = prom_firstprop(node, NULL);
374                 } else {
375                         name = prom_nextprop(node, prev, NULL);
376                 }
377                 if (strlen(name) == 0) {
378                         tmp = p;
379                         return NULL;
380                 }
381                 strcpy(p->name, name);
382                 p->length = prom_getproplen(node, p->name);
383                 if (p->length <= 0) {
384                         p->length = 0;
385                 } else {
386                         p->value = prom_early_alloc(p->length + 1);
387                         len = prom_getproperty(node, p->name, p->value,
388                                                p->length);
389                         if (len <= 0)
390                                 p->length = 0;
391                         ((unsigned char *)p->value)[p->length] = '\0';
392                 }
393         }
394         return p;
395 }
396
397 static struct property * __init build_prop_list(phandle node)
398 {
399         struct property *head, *tail;
400
401         head = tail = build_one_prop(node, NULL,
402                                      ".node", &node, sizeof(node));
403
404         tail->next = build_one_prop(node, NULL, NULL, NULL, 0);
405         tail = tail->next;
406         while(tail) {
407                 tail->next = build_one_prop(node, tail->name,
408                                             NULL, NULL, 0);
409                 tail = tail->next;
410         }
411
412         return head;
413 }
414
415 static char * __init get_one_property(phandle node, char *name)
416 {
417         char *buf = "<NULL>";
418         int len;
419
420         len = prom_getproplen(node, name);
421         if (len > 0) {
422                 buf = prom_early_alloc(len);
423                 len = prom_getproperty(node, name, buf, len);
424         }
425
426         return buf;
427 }
428
429 static struct device_node * __init create_node(phandle node)
430 {
431         struct device_node *dp;
432
433         if (!node)
434                 return NULL;
435
436         dp = prom_early_alloc(sizeof(*dp));
437         dp->unique_id = unique_id++;
438
439         kref_init(&dp->kref);
440
441         dp->name = get_one_property(node, "name");
442         dp->type = get_one_property(node, "device_type");
443         dp->node = node;
444
445         /* Build interrupts later... */
446
447         dp->properties = build_prop_list(node);
448
449         return dp;
450 }
451
452 static struct device_node * __init build_tree(struct device_node *parent, phandle node, struct device_node ***nextp)
453 {
454         struct device_node *dp;
455
456         dp = create_node(node);
457         if (dp) {
458                 *(*nextp) = dp;
459                 *nextp = &dp->allnext;
460
461                 dp->parent = parent;
462                 dp->path_component_name = build_path_component(dp);
463                 dp->full_name = build_full_name(dp);
464
465                 dp->child = build_tree(dp, prom_getchild(node), nextp);
466
467                 dp->sibling = build_tree(parent, prom_getsibling(node), nextp);
468         }
469
470         return dp;
471 }
472
473 void __init prom_build_devicetree(void)
474 {
475         struct device_node **nextp;
476
477         allnodes = create_node(prom_root_node);
478         allnodes->path_component_name = "";
479         allnodes->full_name = "/";
480
481         nextp = &allnodes->allnext;
482         allnodes->child = build_tree(allnodes,
483                                      prom_getchild(allnodes->node),
484                                      &nextp);
485         printk("PROM: Built device tree with %u bytes of memory.\n",
486                prom_early_allocated);
487 }