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