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