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