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