[SPARC64]: Convert sparc64 PCI layer to in-kernel device tree.
[linux-block.git] / arch / sparc64 / kernel / prom.c
CommitLineData
372b07bb
DM
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 sparc64 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>
de8d28b1 23#include <linux/module.h>
372b07bb
DM
24
25#include <asm/prom.h>
26#include <asm/oplib.h>
27
28static struct device_node *allnodes;
29
30struct device_node *of_get_parent(const struct device_node *node)
31{
32 struct device_node *np;
33
34 if (!node)
35 return NULL;
36
37 np = node->parent;
38
39 return np;
40}
41
42struct device_node *of_get_next_child(const struct device_node *node,
43 struct device_node *prev)
44{
45 struct device_node *next;
46
47 next = prev ? prev->sibling : node->child;
48 for (; next != 0; next = next->sibling) {
49 break;
50 }
51
52 return next;
53}
54
55struct device_node *of_find_node_by_path(const char *path)
56{
57 struct device_node *np = allnodes;
58
59 for (; np != 0; np = np->allnext) {
60 if (np->full_name != 0 && strcmp(np->full_name, path) == 0)
61 break;
62 }
63
64 return np;
65}
66
de8d28b1
DM
67struct device_node *of_find_node_by_phandle(phandle handle)
68{
69 struct device_node *np;
70
71 for (np = allnodes; np != 0; np = np->allnext)
72 if (np->node == handle)
73 break;
74
75 return np;
76}
77
aaf7cec2
DM
78struct device_node *of_find_node_by_name(struct device_node *from,
79 const char *name)
80{
81 struct device_node *np;
82
83 np = from ? from->allnext : allnodes;
84 for (; np != NULL; np = np->allnext)
85 if (np->name != NULL && strcmp(np->name, name) == 0)
86 break;
87
88 return np;
89}
90
91struct device_node *of_find_node_by_type(struct device_node *from,
92 const char *type)
93{
94 struct device_node *np;
95
96 np = from ? from->allnext : allnodes;
97 for (; np != 0; np = np->allnext)
98 if (np->type != 0 && strcmp(np->type, type) == 0)
99 break;
100
101 return np;
102}
103
372b07bb
DM
104struct property *of_find_property(struct device_node *np, const char *name,
105 int *lenp)
106{
107 struct property *pp;
108
109 for (pp = np->properties; pp != 0; pp = pp->next) {
110 if (strcmp(pp->name, name) == 0) {
111 if (lenp != 0)
112 *lenp = pp->length;
113 break;
114 }
115 }
116 return pp;
117}
de8d28b1
DM
118EXPORT_SYMBOL(of_find_property);
119
120/*
121 * Find a property with a given name for a given node
122 * and return the value.
123 */
124void *of_get_property(struct device_node *np, const char *name, int *lenp)
125{
126 struct property *pp = of_find_property(np,name,lenp);
127 return pp ? pp->value : NULL;
128}
129EXPORT_SYMBOL(of_get_property);
372b07bb 130
6d307724
DM
131int of_getintprop_default(struct device_node *np, const char *name, int def)
132{
133 struct property *prop;
134 int len;
135
136 prop = of_find_property(np, name, &len);
137 if (!prop || len != 4)
138 return def;
139
140 return *(int *) prop->value;
141}
de8d28b1 142EXPORT_SYMBOL(of_getintprop_default);
6d307724 143
372b07bb
DM
144static unsigned int prom_early_allocated;
145
146static void * __init prom_early_alloc(unsigned long size)
147{
148 void *ret;
149
150 ret = __alloc_bootmem(size, SMP_CACHE_BYTES, 0UL);
151 if (ret != NULL)
152 memset(ret, 0, size);
153
154 prom_early_allocated += size;
155
156 return ret;
157}
158
159static int is_root_node(const struct device_node *dp)
160{
161 if (!dp)
162 return 0;
163
164 return (dp->parent == NULL);
165}
166
167/* The following routines deal with the black magic of fully naming a
168 * node.
169 *
170 * Certain well known named nodes are just the simple name string.
171 *
172 * Actual devices have an address specifier appended to the base name
173 * string, like this "foo@addr". The "addr" can be in any number of
174 * formats, and the platform plus the type of the node determine the
175 * format and how it is constructed.
176 *
177 * For children of the ROOT node, the naming convention is fixed and
178 * determined by whether this is a sun4u or sun4v system.
179 *
180 * For children of other nodes, it is bus type specific. So
181 * we walk up the tree until we discover a "device_type" property
182 * we recognize and we go from there.
183 *
184 * As an example, the boot device on my workstation has a full path:
185 *
186 * /pci@1e,600000/ide@d/disk@0,0:c
187 */
188static void __init sun4v_path_component(struct device_node *dp, char *tmp_buf)
189{
190 struct linux_prom64_registers *regs;
191 struct property *rprop;
192 u32 high_bits, low_bits, type;
193
194 rprop = of_find_property(dp, "reg", NULL);
195 if (!rprop)
196 return;
197
198 regs = rprop->value;
199 if (!is_root_node(dp->parent)) {
200 sprintf(tmp_buf, "%s@%x,%x",
201 dp->name,
202 (unsigned int) (regs->phys_addr >> 32UL),
203 (unsigned int) (regs->phys_addr & 0xffffffffUL));
204 return;
205 }
206
207 type = regs->phys_addr >> 60UL;
208 high_bits = (regs->phys_addr >> 32UL) & 0x0fffffffUL;
209 low_bits = (regs->phys_addr & 0xffffffffUL);
210
211 if (type == 0 || type == 8) {
212 const char *prefix = (type == 0) ? "m" : "i";
213
214 if (low_bits)
215 sprintf(tmp_buf, "%s@%s%x,%x",
216 dp->name, prefix,
217 high_bits, low_bits);
218 else
219 sprintf(tmp_buf, "%s@%s%x",
220 dp->name,
221 prefix,
222 high_bits);
223 } else if (type == 12) {
224 sprintf(tmp_buf, "%s@%x",
225 dp->name, high_bits);
226 }
227}
228
229static void __init sun4u_path_component(struct device_node *dp, char *tmp_buf)
230{
231 struct linux_prom64_registers *regs;
232 struct property *prop;
233
234 prop = of_find_property(dp, "reg", NULL);
235 if (!prop)
236 return;
237
238 regs = prop->value;
239 if (!is_root_node(dp->parent)) {
240 sprintf(tmp_buf, "%s@%x,%x",
241 dp->name,
242 (unsigned int) (regs->phys_addr >> 32UL),
243 (unsigned int) (regs->phys_addr & 0xffffffffUL));
244 return;
245 }
246
247 prop = of_find_property(dp, "upa-portid", NULL);
248 if (!prop)
249 prop = of_find_property(dp, "portid", NULL);
250 if (prop) {
251 unsigned long mask = 0xffffffffUL;
252
253 if (tlb_type >= cheetah)
254 mask = 0x7fffff;
255
256 sprintf(tmp_buf, "%s@%x,%x",
257 dp->name,
258 *(u32 *)prop->value,
259 (unsigned int) (regs->phys_addr & mask));
260 }
261}
262
263/* "name@slot,offset" */
264static void __init sbus_path_component(struct device_node *dp, char *tmp_buf)
265{
266 struct linux_prom_registers *regs;
267 struct property *prop;
268
269 prop = of_find_property(dp, "reg", NULL);
270 if (!prop)
271 return;
272
273 regs = prop->value;
274 sprintf(tmp_buf, "%s@%x,%x",
275 dp->name,
276 regs->which_io,
277 regs->phys_addr);
278}
279
280/* "name@devnum[,func]" */
281static void __init pci_path_component(struct device_node *dp, char *tmp_buf)
282{
283 struct linux_prom_pci_registers *regs;
284 struct property *prop;
285 unsigned int devfn;
286
287 prop = of_find_property(dp, "reg", NULL);
288 if (!prop)
289 return;
290
291 regs = prop->value;
292 devfn = (regs->phys_hi >> 8) & 0xff;
293 if (devfn & 0x07) {
294 sprintf(tmp_buf, "%s@%x,%x",
295 dp->name,
296 devfn >> 3,
297 devfn & 0x07);
298 } else {
299 sprintf(tmp_buf, "%s@%x",
300 dp->name,
301 devfn >> 3);
302 }
303}
304
305/* "name@UPA_PORTID,offset" */
306static void __init upa_path_component(struct device_node *dp, char *tmp_buf)
307{
308 struct linux_prom64_registers *regs;
309 struct property *prop;
310
311 prop = of_find_property(dp, "reg", NULL);
312 if (!prop)
313 return;
314
315 regs = prop->value;
316
317 prop = of_find_property(dp, "upa-portid", NULL);
318 if (!prop)
319 return;
320
321 sprintf(tmp_buf, "%s@%x,%x",
322 dp->name,
323 *(u32 *) prop->value,
324 (unsigned int) (regs->phys_addr & 0xffffffffUL));
325}
326
327/* "name@reg" */
328static void __init vdev_path_component(struct device_node *dp, char *tmp_buf)
329{
330 struct property *prop;
331 u32 *regs;
332
333 prop = of_find_property(dp, "reg", NULL);
334 if (!prop)
335 return;
336
337 regs = prop->value;
338
339 sprintf(tmp_buf, "%s@%x", dp->name, *regs);
340}
341
342/* "name@addrhi,addrlo" */
343static void __init ebus_path_component(struct device_node *dp, char *tmp_buf)
344{
345 struct linux_prom64_registers *regs;
346 struct property *prop;
347
348 prop = of_find_property(dp, "reg", NULL);
349 if (!prop)
350 return;
351
352 regs = prop->value;
353
354 sprintf(tmp_buf, "%s@%x,%x",
355 dp->name,
356 (unsigned int) (regs->phys_addr >> 32UL),
357 (unsigned int) (regs->phys_addr & 0xffffffffUL));
358}
359
360/* "name@bus,addr" */
361static void __init i2c_path_component(struct device_node *dp, char *tmp_buf)
362{
363 struct property *prop;
364 u32 *regs;
365
366 prop = of_find_property(dp, "reg", NULL);
367 if (!prop)
368 return;
369
370 regs = prop->value;
371
372 /* This actually isn't right... should look at the #address-cells
373 * property of the i2c bus node etc. etc.
374 */
375 sprintf(tmp_buf, "%s@%x,%x",
376 dp->name, regs[0], regs[1]);
377}
378
379/* "name@reg0[,reg1]" */
380static void __init usb_path_component(struct device_node *dp, char *tmp_buf)
381{
382 struct property *prop;
383 u32 *regs;
384
385 prop = of_find_property(dp, "reg", NULL);
386 if (!prop)
387 return;
388
389 regs = prop->value;
390
391 if (prop->length == sizeof(u32) || regs[1] == 1) {
392 sprintf(tmp_buf, "%s@%x",
393 dp->name, regs[0]);
394 } else {
395 sprintf(tmp_buf, "%s@%x,%x",
396 dp->name, regs[0], regs[1]);
397 }
398}
399
400/* "name@reg0reg1[,reg2reg3]" */
401static void __init ieee1394_path_component(struct device_node *dp, char *tmp_buf)
402{
403 struct property *prop;
404 u32 *regs;
405
406 prop = of_find_property(dp, "reg", NULL);
407 if (!prop)
408 return;
409
410 regs = prop->value;
411
412 if (regs[2] || regs[3]) {
413 sprintf(tmp_buf, "%s@%08x%08x,%04x%08x",
414 dp->name, regs[0], regs[1], regs[2], regs[3]);
415 } else {
416 sprintf(tmp_buf, "%s@%08x%08x",
417 dp->name, regs[0], regs[1]);
418 }
419}
420
421static void __init __build_path_component(struct device_node *dp, char *tmp_buf)
422{
423 struct device_node *parent = dp->parent;
424
425 if (parent != NULL) {
426 if (!strcmp(parent->type, "pci") ||
427 !strcmp(parent->type, "pciex"))
428 return pci_path_component(dp, tmp_buf);
429 if (!strcmp(parent->type, "sbus"))
430 return sbus_path_component(dp, tmp_buf);
431 if (!strcmp(parent->type, "upa"))
432 return upa_path_component(dp, tmp_buf);
433 if (!strcmp(parent->type, "ebus"))
434 return ebus_path_component(dp, tmp_buf);
435 if (!strcmp(parent->name, "usb") ||
436 !strcmp(parent->name, "hub"))
437 return usb_path_component(dp, tmp_buf);
438 if (!strcmp(parent->type, "i2c"))
439 return i2c_path_component(dp, tmp_buf);
440 if (!strcmp(parent->type, "firewire"))
441 return ieee1394_path_component(dp, tmp_buf);
442 if (!strcmp(parent->type, "virtual-devices"))
443 return vdev_path_component(dp, tmp_buf);
444
445 /* "isa" is handled with platform naming */
446 }
447
448 /* Use platform naming convention. */
449 if (tlb_type == hypervisor)
450 return sun4v_path_component(dp, tmp_buf);
451 else
452 return sun4u_path_component(dp, tmp_buf);
453}
454
455static char * __init build_path_component(struct device_node *dp)
456{
457 char tmp_buf[64], *n;
458
459 tmp_buf[0] = '\0';
460 __build_path_component(dp, tmp_buf);
461 if (tmp_buf[0] == '\0')
462 strcpy(tmp_buf, dp->name);
463
464 n = prom_early_alloc(strlen(tmp_buf) + 1);
465 strcpy(n, tmp_buf);
466
467 return n;
468}
469
470static char * __init build_full_name(struct device_node *dp)
471{
472 int len, ourlen, plen;
473 char *n;
474
475 plen = strlen(dp->parent->full_name);
476 ourlen = strlen(dp->path_component_name);
477 len = ourlen + plen + 2;
478
479 n = prom_early_alloc(len);
480 strcpy(n, dp->parent->full_name);
481 if (!is_root_node(dp->parent)) {
482 strcpy(n + plen, "/");
483 plen++;
484 }
485 strcpy(n + plen, dp->path_component_name);
486
487 return n;
488}
489
490static struct property * __init build_one_prop(phandle node, char *prev)
491{
492 static struct property *tmp = NULL;
493 struct property *p;
494
495 if (tmp) {
496 p = tmp;
497 memset(p, 0, sizeof(*p) + 32);
498 tmp = NULL;
499 } else
500 p = prom_early_alloc(sizeof(struct property) + 32);
501
502 p->name = (char *) (p + 1);
503 if (prev == NULL) {
504 prom_firstprop(node, p->name);
505 } else {
506 prom_nextprop(node, prev, p->name);
507 }
508 if (strlen(p->name) == 0) {
509 tmp = p;
510 return NULL;
511 }
512 p->length = prom_getproplen(node, p->name);
513 if (p->length <= 0) {
514 p->length = 0;
515 } else {
516 p->value = prom_early_alloc(p->length);
517 prom_getproperty(node, p->name, p->value, p->length);
518 }
519 return p;
520}
521
522static struct property * __init build_prop_list(phandle node)
523{
524 struct property *head, *tail;
525
526 head = tail = build_one_prop(node, NULL);
527 while(tail) {
528 tail->next = build_one_prop(node, tail->name);
529 tail = tail->next;
530 }
531
532 return head;
533}
534
535static char * __init get_one_property(phandle node, const char *name)
536{
537 char *buf = "<NULL>";
538 int len;
539
540 len = prom_getproplen(node, name);
541 if (len > 0) {
542 buf = prom_early_alloc(len);
543 prom_getproperty(node, name, buf, len);
544 }
545
546 return buf;
547}
548
549static struct device_node * __init create_node(phandle node)
550{
551 struct device_node *dp;
552
553 if (!node)
554 return NULL;
555
556 dp = prom_early_alloc(sizeof(*dp));
557
558 kref_init(&dp->kref);
559
560 dp->name = get_one_property(node, "name");
561 dp->type = get_one_property(node, "device_type");
562 dp->node = node;
563
564 /* Build interrupts later... */
565
566 dp->properties = build_prop_list(node);
567
568 return dp;
569}
570
571static struct device_node * __init build_tree(struct device_node *parent, phandle node, struct device_node ***nextp)
572{
573 struct device_node *dp;
574
575 dp = create_node(node);
576 if (dp) {
577 *(*nextp) = dp;
578 *nextp = &dp->allnext;
579
580 dp->parent = parent;
581 dp->path_component_name = build_path_component(dp);
582 dp->full_name = build_full_name(dp);
583
584 dp->child = build_tree(dp, prom_getchild(node), nextp);
585
586 dp->sibling = build_tree(parent, prom_getsibling(node), nextp);
587 }
588
589 return dp;
590}
591
592void __init prom_build_devicetree(void)
593{
594 struct device_node **nextp;
595
596 allnodes = create_node(prom_root_node);
597 allnodes->path_component_name = "";
598 allnodes->full_name = "/";
599
600 nextp = &allnodes->allnext;
601 allnodes->child = build_tree(allnodes,
602 prom_getchild(allnodes->node),
603 &nextp);
604 printk("PROM: Built device tree with %u bytes of memory.\n",
605 prom_early_allocated);
606}