]>
Commit | Line | Data |
---|---|---|
9bdf6bab AS |
1 | /* prom_common.c: OF device tree support common code. |
2 | * | |
3 | * Paul Mackerras August 1996. | |
4 | * Copyright (C) 1996-2005 Paul Mackerras. | |
5 | * | |
6 | * Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner. | |
7 | * {engebret|bergner}@us.ibm.com | |
8 | * | |
9 | * Adapted for sparc by David S. Miller davem@davemloft.net | |
10 | * | |
11 | * This program is free software; you can redistribute it and/or | |
12 | * modify it under the terms of the GNU General Public License | |
13 | * as published by the Free Software Foundation; either version | |
14 | * 2 of the License, or (at your option) any later version. | |
15 | */ | |
16 | ||
17 | #include <linux/kernel.h> | |
18 | #include <linux/module.h> | |
19 | #include <linux/errno.h> | |
20 | #include <linux/mutex.h> | |
21 | #include <linux/slab.h> | |
22 | #include <linux/of.h> | |
23 | #include <asm/prom.h> | |
24 | #include <asm/oplib.h> | |
25 | #include <asm/leon.h> | |
26 | ||
27 | void (*prom_build_more)(struct device_node *dp, struct device_node ***nextp); | |
28 | ||
29 | unsigned int prom_unique_id; | |
30 | ||
31 | static struct property * __init build_one_prop(phandle node, char *prev, | |
32 | char *special_name, | |
33 | void *special_val, | |
34 | int special_len) | |
35 | { | |
36 | static struct property *tmp = NULL; | |
37 | struct property *p; | |
38 | const char *name; | |
39 | ||
40 | if (tmp) { | |
41 | p = tmp; | |
42 | memset(p, 0, sizeof(*p) + 32); | |
43 | tmp = NULL; | |
44 | } else { | |
45 | p = prom_early_alloc(sizeof(struct property) + 32); | |
46 | p->unique_id = prom_unique_id++; | |
47 | } | |
48 | ||
49 | p->name = (char *) (p + 1); | |
50 | if (special_name) { | |
51 | strcpy(p->name, special_name); | |
52 | p->length = special_len; | |
53 | p->value = prom_early_alloc(special_len); | |
54 | memcpy(p->value, special_val, special_len); | |
55 | } else { | |
56 | if (prev == NULL) { | |
57 | name = prom_firstprop(node, p->name); | |
58 | } else { | |
59 | name = prom_nextprop(node, prev, p->name); | |
60 | } | |
61 | ||
62 | if (!name || strlen(name) == 0) { | |
63 | tmp = p; | |
64 | return NULL; | |
65 | } | |
66 | #ifdef CONFIG_SPARC32 | |
67 | strcpy(p->name, name); | |
68 | #endif | |
69 | p->length = prom_getproplen(node, p->name); | |
70 | if (p->length <= 0) { | |
71 | p->length = 0; | |
72 | } else { | |
73 | int len; | |
74 | ||
75 | p->value = prom_early_alloc(p->length + 1); | |
76 | len = prom_getproperty(node, p->name, p->value, | |
77 | p->length); | |
78 | if (len <= 0) | |
79 | p->length = 0; | |
80 | ((unsigned char *)p->value)[p->length] = '\0'; | |
81 | } | |
82 | } | |
83 | return p; | |
84 | } | |
85 | ||
86 | static struct property * __init build_prop_list(phandle node) | |
87 | { | |
88 | struct property *head, *tail; | |
89 | ||
90 | head = tail = build_one_prop(node, NULL, | |
91 | ".node", &node, sizeof(node)); | |
92 | ||
93 | tail->next = build_one_prop(node, NULL, NULL, NULL, 0); | |
94 | tail = tail->next; | |
95 | while(tail) { | |
96 | tail->next = build_one_prop(node, tail->name, | |
97 | NULL, NULL, 0); | |
98 | tail = tail->next; | |
99 | } | |
100 | ||
101 | return head; | |
102 | } | |
103 | ||
104 | static char * __init get_one_property(phandle node, const char *name) | |
105 | { | |
106 | char *buf = "<NULL>"; | |
107 | int len; | |
108 | ||
109 | len = prom_getproplen(node, name); | |
110 | if (len > 0) { | |
111 | buf = prom_early_alloc(len); | |
112 | len = prom_getproperty(node, name, buf, len); | |
113 | } | |
114 | ||
115 | return buf; | |
116 | } | |
117 | ||
118 | static struct device_node * __init prom_create_node(phandle node, | |
119 | struct device_node *parent) | |
120 | { | |
121 | struct device_node *dp; | |
122 | ||
123 | if (!node) | |
124 | return NULL; | |
125 | ||
126 | dp = prom_early_alloc(sizeof(*dp)); | |
127 | dp->unique_id = prom_unique_id++; | |
128 | dp->parent = parent; | |
129 | ||
130 | kref_init(&dp->kref); | |
131 | ||
132 | dp->name = get_one_property(node, "name"); | |
133 | dp->type = get_one_property(node, "device_type"); | |
134 | dp->phandle = node; | |
135 | ||
136 | dp->properties = build_prop_list(node); | |
137 | ||
138 | irq_trans_init(dp); | |
139 | ||
140 | return dp; | |
141 | } | |
142 | ||
143 | char * __init build_full_name(struct device_node *dp) | |
144 | { | |
145 | int len, ourlen, plen; | |
146 | char *n; | |
147 | ||
148 | plen = strlen(dp->parent->full_name); | |
149 | ourlen = strlen(dp->path_component_name); | |
150 | len = ourlen + plen + 2; | |
151 | ||
152 | n = prom_early_alloc(len); | |
153 | strcpy(n, dp->parent->full_name); | |
154 | if (!of_node_is_root(dp->parent)) { | |
155 | strcpy(n + plen, "/"); | |
156 | plen++; | |
157 | } | |
158 | strcpy(n + plen, dp->path_component_name); | |
159 | ||
160 | return n; | |
161 | } | |
162 | ||
163 | static struct device_node * __init prom_build_tree(struct device_node *parent, | |
164 | phandle node, | |
165 | struct device_node ***nextp) | |
166 | { | |
167 | struct device_node *ret = NULL, *prev_sibling = NULL; | |
168 | struct device_node *dp; | |
169 | ||
170 | while (1) { | |
171 | dp = prom_create_node(node, parent); | |
172 | if (!dp) | |
173 | break; | |
174 | ||
175 | if (prev_sibling) | |
176 | prev_sibling->sibling = dp; | |
177 | ||
178 | if (!ret) | |
179 | ret = dp; | |
180 | prev_sibling = dp; | |
181 | ||
182 | *(*nextp) = dp; | |
183 | *nextp = &dp->allnext; | |
184 | ||
185 | dp->path_component_name = build_path_component(dp); | |
186 | dp->full_name = build_full_name(dp); | |
187 | ||
188 | dp->child = prom_build_tree(dp, prom_getchild(node), nextp); | |
189 | ||
190 | if (prom_build_more) | |
191 | prom_build_more(dp, nextp); | |
192 | ||
193 | node = prom_getsibling(node); | |
194 | } | |
195 | ||
196 | return ret; | |
197 | } | |
198 | ||
199 | void __init prom_build_devicetree(void) | |
200 | { | |
201 | struct device_node **nextp; | |
202 | ||
203 | allnodes = prom_create_node(prom_root_node, NULL); | |
204 | allnodes->path_component_name = ""; | |
205 | allnodes->full_name = "/"; | |
206 | ||
207 | nextp = &allnodes->allnext; | |
208 | allnodes->child = prom_build_tree(allnodes, | |
209 | prom_getchild(allnodes->phandle), | |
210 | &nextp); | |
211 | of_console_init(); | |
212 | ||
213 | printk("PROM: Built device tree with %u bytes of memory.\n", | |
214 | prom_early_allocated); | |
215 | } |