aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid S. Miller <davem@sunset.davemloft.net>2006-06-23 18:53:31 -0400
committerDavid S. Miller <davem@sunset.davemloft.net>2006-06-24 02:15:45 -0400
commit942a6bdd1c4d2419a42be77ba1c964e4ba8dae9e (patch)
treeb1a432a83aff7b88d301d4932ff75e5078b8c347
parenta2bd4fd17926d715a470fbe0ebe05128ba410984 (diff)
[SPARC]: Port sparc64 in-kernel device tree code to sparc32.
Signed-off-by: David S. Miller <davem@davemloft.net>
-rw-r--r--arch/sparc/kernel/Makefile2
-rw-r--r--arch/sparc/kernel/ebus.c164
-rw-r--r--arch/sparc/kernel/pcic.c3
-rw-r--r--arch/sparc/kernel/prom.c474
-rw-r--r--arch/sparc/mm/init.c2
-rw-r--r--drivers/net/sunhme.c1
-rw-r--r--include/asm-sparc/ebus.h12
-rw-r--r--include/asm-sparc/pbm.h3
-rw-r--r--include/asm-sparc/prom.h98
9 files changed, 668 insertions, 91 deletions
diff --git a/arch/sparc/kernel/Makefile b/arch/sparc/kernel/Makefile
index 1b83e21841b5..93e41d0dd8da 100644
--- a/arch/sparc/kernel/Makefile
+++ b/arch/sparc/kernel/Makefile
@@ -12,7 +12,7 @@ obj-y := entry.o wof.o wuf.o etrap.o rtrap.o traps.o $(IRQ_OBJS) \
12 sys_sparc.o sunos_asm.o systbls.o \ 12 sys_sparc.o sunos_asm.o systbls.o \
13 time.o windows.o cpu.o devices.o sclow.o \ 13 time.o windows.o cpu.o devices.o sclow.o \
14 tadpole.o tick14.o ptrace.o sys_solaris.o \ 14 tadpole.o tick14.o ptrace.o sys_solaris.o \
15 unaligned.o muldiv.o semaphore.o 15 unaligned.o muldiv.o semaphore.o prom.o
16 16
17obj-$(CONFIG_PCI) += pcic.o 17obj-$(CONFIG_PCI) += pcic.o
18obj-$(CONFIG_SUN4) += sun4setup.o 18obj-$(CONFIG_SUN4) += sun4setup.o
diff --git a/arch/sparc/kernel/ebus.c b/arch/sparc/kernel/ebus.c
index 5c3529ceb5d6..9d461da76399 100644
--- a/arch/sparc/kernel/ebus.c
+++ b/arch/sparc/kernel/ebus.c
@@ -20,6 +20,7 @@
20#include <asm/ebus.h> 20#include <asm/ebus.h>
21#include <asm/io.h> 21#include <asm/io.h>
22#include <asm/oplib.h> 22#include <asm/oplib.h>
23#include <asm/prom.h>
23#include <asm/bpp.h> 24#include <asm/bpp.h>
24 25
25struct linux_ebus *ebus_chain = NULL; 26struct linux_ebus *ebus_chain = NULL;
@@ -83,79 +84,81 @@ int __init ebus_blacklist_irq(char *name)
83 return 0; 84 return 0;
84} 85}
85 86
86void __init fill_ebus_child(int node, struct linux_prom_registers *preg, 87void __init fill_ebus_child(struct device_node *dp,
87 struct linux_ebus_child *dev) 88 struct linux_ebus_child *dev)
88{ 89{
89 int regs[PROMREG_MAX]; 90 int *regs;
90 int irqs[PROMREG_MAX]; 91 int *irqs;
91 char lbuf[128];
92 int i, len; 92 int i, len;
93 93
94 dev->prom_node = node; 94 dev->prom_node = dp;
95 prom_getstring(node, "name", lbuf, sizeof(lbuf)); 95 regs = of_get_property(dp, "reg", &len);
96 strcpy(dev->prom_name, lbuf); 96 if (!regs)
97 97 len = 0;
98 len = prom_getproperty(node, "reg", (void *)regs, sizeof(regs));
99 if (len == -1) len = 0;
100 dev->num_addrs = len / sizeof(regs[0]); 98 dev->num_addrs = len / sizeof(regs[0]);
101 99
102 for (i = 0; i < dev->num_addrs; i++) { 100 for (i = 0; i < dev->num_addrs; i++) {
103 if (regs[i] >= dev->parent->num_addrs) { 101 if (regs[i] >= dev->parent->num_addrs) {
104 prom_printf("UGH: property for %s was %d, need < %d\n", 102 prom_printf("UGH: property for %s was %d, need < %d\n",
105 dev->prom_name, len, dev->parent->num_addrs); 103 dev->prom_node->name, len,
104 dev->parent->num_addrs);
106 panic(__FUNCTION__); 105 panic(__FUNCTION__);
107 } 106 }
108 dev->resource[i].start = dev->parent->resource[regs[i]].start; /* XXX resource */ 107
108 /* XXX resource */
109 dev->resource[i].start =
110 dev->parent->resource[regs[i]].start;
109 } 111 }
110 112
111 for (i = 0; i < PROMINTR_MAX; i++) 113 for (i = 0; i < PROMINTR_MAX; i++)
112 dev->irqs[i] = PCI_IRQ_NONE; 114 dev->irqs[i] = PCI_IRQ_NONE;
113 115
114 if ((dev->irqs[0] = ebus_blacklist_irq(dev->prom_name)) != 0) { 116 if ((dev->irqs[0] = ebus_blacklist_irq(dev->prom_node->name)) != 0) {
115 dev->num_irqs = 1; 117 dev->num_irqs = 1;
116 } else if ((len = prom_getproperty(node, "interrupts",
117 (char *)&irqs, sizeof(irqs))) == -1 || len == 0) {
118 dev->num_irqs = 0;
119 dev->irqs[0] = 0;
120 if (dev->parent->num_irqs != 0) {
121 dev->num_irqs = 1;
122 dev->irqs[0] = dev->parent->irqs[0];
123/* P3 */ /* printk("EBUS: dev %s irq %d from parent\n", dev->prom_name, dev->irqs[0]); */
124 }
125 } else { 118 } else {
126 dev->num_irqs = len / sizeof(irqs[0]); 119 irqs = of_get_property(dp, "interrupts", &len);
127 if (irqs[0] == 0 || irqs[0] >= 8) { 120 if (!irqs) {
128 /*
129 * XXX Zero is a valid pin number...
130 * This works as long as Ebus is not wired to INTA#.
131 */
132 printk("EBUS: %s got bad irq %d from PROM\n",
133 dev->prom_name, irqs[0]);
134 dev->num_irqs = 0; 121 dev->num_irqs = 0;
135 dev->irqs[0] = 0; 122 dev->irqs[0] = 0;
123 if (dev->parent->num_irqs != 0) {
124 dev->num_irqs = 1;
125 dev->irqs[0] = dev->parent->irqs[0];
126 }
136 } else { 127 } else {
137 dev->irqs[0] = pcic_pin_to_irq(irqs[0], dev->prom_name); 128 dev->num_irqs = len / sizeof(irqs[0]);
129 if (irqs[0] == 0 || irqs[0] >= 8) {
130 /*
131 * XXX Zero is a valid pin number...
132 * This works as long as Ebus is not wired
133 * to INTA#.
134 */
135 printk("EBUS: %s got bad irq %d from PROM\n",
136 dev->prom_node->name, irqs[0]);
137 dev->num_irqs = 0;
138 dev->irqs[0] = 0;
139 } else {
140 dev->irqs[0] =
141 pcic_pin_to_irq(irqs[0],
142 dev->prom_node->name);
143 }
138 } 144 }
139 } 145 }
140} 146}
141 147
142void __init fill_ebus_device(int node, struct linux_ebus_device *dev) 148void __init fill_ebus_device(struct device_node *dp, struct linux_ebus_device *dev)
143{ 149{
144 struct linux_prom_registers regs[PROMREG_MAX]; 150 struct linux_prom_registers *regs;
145 struct linux_ebus_child *child; 151 struct linux_ebus_child *child;
146 int irqs[PROMINTR_MAX]; 152 int *irqs;
147 char lbuf[128];
148 int i, n, len; 153 int i, n, len;
149 unsigned long baseaddr; 154 unsigned long baseaddr;
150 155
151 dev->prom_node = node; 156 dev->prom_node = dp;
152 prom_getstring(node, "name", lbuf, sizeof(lbuf));
153 strcpy(dev->prom_name, lbuf);
154 157
155 len = prom_getproperty(node, "reg", (void *)regs, sizeof(regs)); 158 regs = of_get_property(dp, "reg", &len);
156 if (len % sizeof(struct linux_prom_registers)) { 159 if (len % sizeof(struct linux_prom_registers)) {
157 prom_printf("UGH: proplen for %s was %d, need multiple of %d\n", 160 prom_printf("UGH: proplen for %s was %d, need multiple of %d\n",
158 dev->prom_name, len, 161 dev->prom_node->name, len,
159 (int)sizeof(struct linux_prom_registers)); 162 (int)sizeof(struct linux_prom_registers));
160 panic(__FUNCTION__); 163 panic(__FUNCTION__);
161 } 164 }
@@ -197,7 +200,7 @@ void __init fill_ebus_device(int node, struct linux_ebus_device *dev)
197 if ((baseaddr = (unsigned long) ioremap(baseaddr, 200 if ((baseaddr = (unsigned long) ioremap(baseaddr,
198 regs[i].reg_size)) == 0) { 201 regs[i].reg_size)) == 0) {
199 panic("ebus: unable to remap dev %s", 202 panic("ebus: unable to remap dev %s",
200 dev->prom_name); 203 dev->prom_node->name);
201 } 204 }
202 } 205 }
203 dev->resource[i].start = baseaddr; /* XXX Unaligned */ 206 dev->resource[i].start = baseaddr; /* XXX Unaligned */
@@ -206,29 +209,33 @@ void __init fill_ebus_device(int node, struct linux_ebus_device *dev)
206 for (i = 0; i < PROMINTR_MAX; i++) 209 for (i = 0; i < PROMINTR_MAX; i++)
207 dev->irqs[i] = PCI_IRQ_NONE; 210 dev->irqs[i] = PCI_IRQ_NONE;
208 211
209 if ((dev->irqs[0] = ebus_blacklist_irq(dev->prom_name)) != 0) { 212 if ((dev->irqs[0] = ebus_blacklist_irq(dev->prom_node->name)) != 0) {
210 dev->num_irqs = 1; 213 dev->num_irqs = 1;
211 } else if ((len = prom_getproperty(node, "interrupts",
212 (char *)&irqs, sizeof(irqs))) == -1 || len == 0) {
213 dev->num_irqs = 0;
214 if ((dev->irqs[0] = dev->bus->self->irq) != 0) {
215 dev->num_irqs = 1;
216/* P3 */ /* printk("EBUS: child %s irq %d from parent\n", dev->prom_name, dev->irqs[0]); */
217 }
218 } else { 214 } else {
219 dev->num_irqs = 1; /* dev->num_irqs = len / sizeof(irqs[0]); */ 215 irqs = of_get_property(dp, "interrupts", &len);
220 if (irqs[0] == 0 || irqs[0] >= 8) { 216 if (!irqs) {
221 /* See above for the parent. XXX */
222 printk("EBUS: %s got bad irq %d from PROM\n",
223 dev->prom_name, irqs[0]);
224 dev->num_irqs = 0; 217 dev->num_irqs = 0;
225 dev->irqs[0] = 0; 218 if ((dev->irqs[0] = dev->bus->self->irq) != 0) {
219 dev->num_irqs = 1;
220/* P3 */ /* printk("EBUS: child %s irq %d from parent\n", dev->prom_name, dev->irqs[0]); */
221 }
226 } else { 222 } else {
227 dev->irqs[0] = pcic_pin_to_irq(irqs[0], dev->prom_name); 223 dev->num_irqs = 1; /* dev->num_irqs = len / sizeof(irqs[0]); */
224 if (irqs[0] == 0 || irqs[0] >= 8) {
225 /* See above for the parent. XXX */
226 printk("EBUS: %s got bad irq %d from PROM\n",
227 dev->prom_node->name, irqs[0]);
228 dev->num_irqs = 0;
229 dev->irqs[0] = 0;
230 } else {
231 dev->irqs[0] =
232 pcic_pin_to_irq(irqs[0],
233 dev->prom_node->name);
234 }
228 } 235 }
229 } 236 }
230 237
231 if ((node = prom_getchild(node))) { 238 if ((dp = dp->child) != NULL) {
232 dev->children = (struct linux_ebus_child *) 239 dev->children = (struct linux_ebus_child *)
233 ebus_alloc(sizeof(struct linux_ebus_child)); 240 ebus_alloc(sizeof(struct linux_ebus_child));
234 241
@@ -236,9 +243,9 @@ void __init fill_ebus_device(int node, struct linux_ebus_device *dev)
236 child->next = NULL; 243 child->next = NULL;
237 child->parent = dev; 244 child->parent = dev;
238 child->bus = dev->bus; 245 child->bus = dev->bus;
239 fill_ebus_child(node, &regs[0], child); 246 fill_ebus_child(dp, child);
240 247
241 while ((node = prom_getsibling(node)) != 0) { 248 while ((dp = dp->sibling) != NULL) {
242 child->next = (struct linux_ebus_child *) 249 child->next = (struct linux_ebus_child *)
243 ebus_alloc(sizeof(struct linux_ebus_child)); 250 ebus_alloc(sizeof(struct linux_ebus_child));
244 251
@@ -246,51 +253,49 @@ void __init fill_ebus_device(int node, struct linux_ebus_device *dev)
246 child->next = NULL; 253 child->next = NULL;
247 child->parent = dev; 254 child->parent = dev;
248 child->bus = dev->bus; 255 child->bus = dev->bus;
249 fill_ebus_child(node, &regs[0], child); 256 fill_ebus_child(dp, child);
250 } 257 }
251 } 258 }
252} 259}
253 260
254void __init ebus_init(void) 261void __init ebus_init(void)
255{ 262{
256 struct linux_prom_pci_registers regs[PROMREG_MAX]; 263 struct linux_prom_pci_registers *regs;
257 struct linux_pbm_info *pbm; 264 struct linux_pbm_info *pbm;
258 struct linux_ebus_device *dev; 265 struct linux_ebus_device *dev;
259 struct linux_ebus *ebus; 266 struct linux_ebus *ebus;
260 struct ebus_system_entry *sp; 267 struct ebus_system_entry *sp;
261 struct pci_dev *pdev; 268 struct pci_dev *pdev;
262 struct pcidev_cookie *cookie; 269 struct pcidev_cookie *cookie;
263 char lbuf[128]; 270 struct device_node *dp;
264 unsigned long addr, *base; 271 unsigned long addr, *base;
265 unsigned short pci_command; 272 unsigned short pci_command;
266 int nd, len, ebusnd; 273 int len, reg, nreg;
267 int reg, nreg;
268 int num_ebus = 0; 274 int num_ebus = 0;
269 275
270 prom_getstring(prom_root_node, "name", lbuf, sizeof(lbuf)); 276 dp = of_find_node_by_path("/");
271 for (sp = ebus_blacklist; sp->esname != NULL; sp++) { 277 for (sp = ebus_blacklist; sp->esname != NULL; sp++) {
272 if (strcmp(lbuf, sp->esname) == 0) { 278 if (strcmp(dp->name, sp->esname) == 0) {
273 ebus_blackp = sp->ipt; 279 ebus_blackp = sp->ipt;
274 break; 280 break;
275 } 281 }
276 } 282 }
277 283
278 pdev = pci_get_device(PCI_VENDOR_ID_SUN, PCI_DEVICE_ID_SUN_EBUS, NULL); 284 pdev = pci_get_device(PCI_VENDOR_ID_SUN, PCI_DEVICE_ID_SUN_EBUS, NULL);
279 if (!pdev) { 285 if (!pdev)
280 return; 286 return;
281 } 287
282 cookie = pdev->sysdata; 288 cookie = pdev->sysdata;
283 ebusnd = cookie->prom_node; 289 dp = cookie->prom_node;
284 290
285 ebus_chain = ebus = (struct linux_ebus *) 291 ebus_chain = ebus = (struct linux_ebus *)
286 ebus_alloc(sizeof(struct linux_ebus)); 292 ebus_alloc(sizeof(struct linux_ebus));
287 ebus->next = NULL; 293 ebus->next = NULL;
288 294
289 while (ebusnd) { 295 while (dp) {
296 struct device_node *nd;
290 297
291 prom_getstring(ebusnd, "name", lbuf, sizeof(lbuf)); 298 ebus->prom_node = dp;
292 ebus->prom_node = ebusnd;
293 strcpy(ebus->prom_name, lbuf);
294 ebus->self = pdev; 299 ebus->self = pdev;
295 ebus->parent = pbm = cookie->pbm; 300 ebus->parent = pbm = cookie->pbm;
296 301
@@ -299,9 +304,8 @@ void __init ebus_init(void)
299 pci_command |= PCI_COMMAND_MASTER; 304 pci_command |= PCI_COMMAND_MASTER;
300 pci_write_config_word(pdev, PCI_COMMAND, pci_command); 305 pci_write_config_word(pdev, PCI_COMMAND, pci_command);
301 306
302 len = prom_getproperty(ebusnd, "reg", (void *)regs, 307 regs = of_get_property(dp, "reg", &len);
303 sizeof(regs)); 308 if (!regs) {
304 if (len == 0 || len == -1) {
305 prom_printf("%s: can't find reg property\n", 309 prom_printf("%s: can't find reg property\n",
306 __FUNCTION__); 310 __FUNCTION__);
307 prom_halt(); 311 prom_halt();
@@ -317,7 +321,7 @@ void __init ebus_init(void)
317 *base++ = addr; 321 *base++ = addr;
318 } 322 }
319 323
320 nd = prom_getchild(ebusnd); 324 nd = dp->child;
321 if (!nd) 325 if (!nd)
322 goto next_ebus; 326 goto next_ebus;
323 327
@@ -330,7 +334,7 @@ void __init ebus_init(void)
330 dev->bus = ebus; 334 dev->bus = ebus;
331 fill_ebus_device(nd, dev); 335 fill_ebus_device(nd, dev);
332 336
333 while ((nd = prom_getsibling(nd)) != 0) { 337 while ((nd = nd->sibling) != NULL) {
334 dev->next = (struct linux_ebus_device *) 338 dev->next = (struct linux_ebus_device *)
335 ebus_alloc(sizeof(struct linux_ebus_device)); 339 ebus_alloc(sizeof(struct linux_ebus_device));
336 340
@@ -348,7 +352,7 @@ void __init ebus_init(void)
348 break; 352 break;
349 353
350 cookie = pdev->sysdata; 354 cookie = pdev->sysdata;
351 ebusnd = cookie->prom_node; 355 dp = cookie->prom_node;
352 356
353 ebus->next = (struct linux_ebus *) 357 ebus->next = (struct linux_ebus *)
354 ebus_alloc(sizeof(struct linux_ebus)); 358 ebus_alloc(sizeof(struct linux_ebus));
diff --git a/arch/sparc/kernel/pcic.c b/arch/sparc/kernel/pcic.c
index bcdf5ad0f035..bcfdddd0418a 100644
--- a/arch/sparc/kernel/pcic.c
+++ b/arch/sparc/kernel/pcic.c
@@ -31,6 +31,7 @@
31 31
32#include <asm/irq.h> 32#include <asm/irq.h>
33#include <asm/oplib.h> 33#include <asm/oplib.h>
34#include <asm/prom.h>
34#include <asm/pcic.h> 35#include <asm/pcic.h>
35#include <asm/timer.h> 36#include <asm/timer.h>
36#include <asm/uaccess.h> 37#include <asm/uaccess.h>
@@ -665,7 +666,7 @@ void __init pcibios_fixup_bus(struct pci_bus *bus)
665 /* cookies */ 666 /* cookies */
666 pcp = pci_devcookie_alloc(); 667 pcp = pci_devcookie_alloc();
667 pcp->pbm = &pcic->pbm; 668 pcp->pbm = &pcic->pbm;
668 pcp->prom_node = node; 669 pcp->prom_node = of_find_node_by_phandle(node);
669 dev->sysdata = pcp; 670 dev->sysdata = pcp;
670 671
671 /* fixing I/O to look like memory */ 672 /* fixing I/O to look like memory */
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
28static struct device_node *allnodes;
29
30int 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}
48EXPORT_SYMBOL(of_device_is_compatible);
49
50struct 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}
61EXPORT_SYMBOL(of_get_parent);
62
63struct 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}
75EXPORT_SYMBOL(of_get_next_child);
76
77struct 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}
88EXPORT_SYMBOL(of_find_node_by_path);
89
90struct 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}
100EXPORT_SYMBOL(of_find_node_by_phandle);
101
102struct 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}
114EXPORT_SYMBOL(of_find_node_by_name);
115
116struct 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}
128EXPORT_SYMBOL(of_find_node_by_type);
129
130struct 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}
146EXPORT_SYMBOL(of_find_compatible_node);
147
148struct 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}
162EXPORT_SYMBOL(of_find_property);
163
164/*
165 * Find a property with a given name for a given node
166 * and return the value.
167 */
168void *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}
173EXPORT_SYMBOL(of_get_property);
174
175int 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}
186EXPORT_SYMBOL(of_getintprop_default);
187
188static unsigned int prom_early_allocated;
189
190static 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
203static 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 */
228static 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" */
244static 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]" */
261static 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" */
286static 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
302static 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
322static 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
337static 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
357static 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
390static 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
403static 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
417static 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
439static 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
460void __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}
diff --git a/arch/sparc/mm/init.c b/arch/sparc/mm/init.c
index 898669732466..cfa7d3456634 100644
--- a/arch/sparc/mm/init.c
+++ b/arch/sparc/mm/init.c
@@ -31,6 +31,7 @@
31#include <asm/vaddrs.h> 31#include <asm/vaddrs.h>
32#include <asm/pgalloc.h> /* bug in asm-generic/tlb.h: check_pgt_cache */ 32#include <asm/pgalloc.h> /* bug in asm-generic/tlb.h: check_pgt_cache */
33#include <asm/tlb.h> 33#include <asm/tlb.h>
34#include <asm/prom.h>
34 35
35DEFINE_PER_CPU(struct mmu_gather, mmu_gathers); 36DEFINE_PER_CPU(struct mmu_gather, mmu_gathers);
36 37
@@ -349,6 +350,7 @@ void __init paging_init(void)
349 protection_map[14] = PAGE_SHARED; 350 protection_map[14] = PAGE_SHARED;
350 protection_map[15] = PAGE_SHARED; 351 protection_map[15] = PAGE_SHARED;
351 btfixup(); 352 btfixup();
353 prom_build_devicetree();
352 device_scan(); 354 device_scan();
353} 355}
354 356
diff --git a/drivers/net/sunhme.c b/drivers/net/sunhme.c
index b0d452733c9b..cfec9e2c0932 100644
--- a/drivers/net/sunhme.c
+++ b/drivers/net/sunhme.c
@@ -45,6 +45,7 @@
45#include <asm/sbus.h> 45#include <asm/sbus.h>
46#include <asm/openprom.h> 46#include <asm/openprom.h>
47#include <asm/oplib.h> 47#include <asm/oplib.h>
48#include <asm/prom.h>
48#include <asm/auxio.h> 49#include <asm/auxio.h>
49#ifndef __sparc_v9__ 50#ifndef __sparc_v9__
50#include <asm/io-unit.h> 51#include <asm/io-unit.h>
diff --git a/include/asm-sparc/ebus.h b/include/asm-sparc/ebus.h
index 2d6a997c5b0c..0dc3c01b4b1f 100644
--- a/include/asm-sparc/ebus.h
+++ b/include/asm-sparc/ebus.h
@@ -13,13 +13,13 @@
13#include <linux/ioport.h> 13#include <linux/ioport.h>
14#endif 14#endif
15#include <asm/oplib.h> 15#include <asm/oplib.h>
16#include <asm/prom.h>
16 17
17struct linux_ebus_child { 18struct linux_ebus_child {
18 struct linux_ebus_child *next; 19 struct linux_ebus_child *next;
19 struct linux_ebus_device *parent; 20 struct linux_ebus_device *parent;
20 struct linux_ebus *bus; 21 struct linux_ebus *bus;
21 int prom_node; 22 struct device_node *prom_node;
22 char prom_name[64];
23 struct resource resource[PROMREG_MAX]; 23 struct resource resource[PROMREG_MAX];
24 int num_addrs; 24 int num_addrs;
25 unsigned int irqs[PROMINTR_MAX]; 25 unsigned int irqs[PROMINTR_MAX];
@@ -30,8 +30,7 @@ struct linux_ebus_device {
30 struct linux_ebus_device *next; 30 struct linux_ebus_device *next;
31 struct linux_ebus_child *children; 31 struct linux_ebus_child *children;
32 struct linux_ebus *bus; 32 struct linux_ebus *bus;
33 int prom_node; 33 struct device_node *prom_node;
34 char prom_name[64];
35 struct resource resource[PROMREG_MAX]; 34 struct resource resource[PROMREG_MAX];
36 int num_addrs; 35 int num_addrs;
37 unsigned int irqs[PROMINTR_MAX]; 36 unsigned int irqs[PROMINTR_MAX];
@@ -43,10 +42,7 @@ struct linux_ebus {
43 struct linux_ebus_device *devices; 42 struct linux_ebus_device *devices;
44 struct linux_pbm_info *parent; 43 struct linux_pbm_info *parent;
45 struct pci_dev *self; 44 struct pci_dev *self;
46 int prom_node; 45 struct device_node *prom_node;
47 char prom_name[64];
48 struct linux_prom_ebus_ranges ebus_ranges[PROMREG_MAX];
49 int num_ebus_ranges;
50}; 46};
51 47
52struct linux_ebus_dma { 48struct linux_ebus_dma {
diff --git a/include/asm-sparc/pbm.h b/include/asm-sparc/pbm.h
index 0aba3a82c2eb..fedd9c6e875c 100644
--- a/include/asm-sparc/pbm.h
+++ b/include/asm-sparc/pbm.h
@@ -22,6 +22,7 @@
22 22
23#include <linux/pci.h> 23#include <linux/pci.h>
24#include <asm/oplib.h> 24#include <asm/oplib.h>
25#include <asm/prom.h>
25 26
26struct linux_pbm_info { 27struct linux_pbm_info {
27 int prom_node; 28 int prom_node;
@@ -40,7 +41,7 @@ struct linux_pbm_info {
40 */ 41 */
41struct pcidev_cookie { 42struct pcidev_cookie {
42 struct linux_pbm_info *pbm; 43 struct linux_pbm_info *pbm;
43 int prom_node; 44 struct device_node *prom_node;
44}; 45};
45 46
46#endif /* !(__SPARC_PBM_H) */ 47#endif /* !(__SPARC_PBM_H) */
diff --git a/include/asm-sparc/prom.h b/include/asm-sparc/prom.h
new file mode 100644
index 000000000000..c5e3d26eabd3
--- /dev/null
+++ b/include/asm-sparc/prom.h
@@ -0,0 +1,98 @@
1#ifndef _SPARC_PROM_H
2#define _SPARC_PROM_H
3#ifdef __KERNEL__
4
5
6/*
7 * Definitions for talking to the Open Firmware PROM on
8 * Power Macintosh computers.
9 *
10 * Copyright (C) 1996-2005 Paul Mackerras.
11 *
12 * Updates for PPC64 by Peter Bergner & David Engebretsen, IBM Corp.
13 * Updates for SPARC32 by David S. Miller
14 *
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version
18 * 2 of the License, or (at your option) any later version.
19 */
20
21#include <linux/types.h>
22#include <linux/proc_fs.h>
23#include <asm/atomic.h>
24
25typedef u32 phandle;
26typedef u32 ihandle;
27
28struct interrupt_info {
29 int line;
30 int sense; /* +ve/-ve logic, edge or level, etc. */
31};
32
33struct property {
34 char *name;
35 int length;
36 void *value;
37 struct property *next;
38};
39
40struct device_node {
41 char *name;
42 char *type;
43 phandle node;
44 phandle linux_phandle;
45 int n_intrs;
46 struct interrupt_info *intrs;
47 char *path_component_name;
48 char *full_name;
49
50 struct property *properties;
51 struct property *deadprops; /* removed properties */
52 struct device_node *parent;
53 struct device_node *child;
54 struct device_node *sibling;
55 struct device_node *next; /* next device of same type */
56 struct device_node *allnext; /* next in list of all nodes */
57 struct proc_dir_entry *pde; /* this node's proc directory */
58 struct kref kref;
59 unsigned long _flags;
60 void *data;
61};
62
63static inline void set_node_proc_entry(struct device_node *dn, struct proc_dir_entry *de)
64{
65 dn->pde = de;
66}
67
68extern struct device_node *of_find_node_by_name(struct device_node *from,
69 const char *name);
70#define for_each_node_by_name(dn, name) \
71 for (dn = of_find_node_by_name(NULL, name); dn; \
72 dn = of_find_node_by_name(dn, name))
73extern struct device_node *of_find_node_by_type(struct device_node *from,
74 const char *type);
75#define for_each_node_by_type(dn, type) \
76 for (dn = of_find_node_by_type(NULL, type); dn; \
77 dn = of_find_node_by_type(dn, type))
78extern struct device_node *of_find_compatible_node(struct device_node *from,
79 const char *type, const char *compat);
80extern struct device_node *of_find_node_by_path(const char *path);
81extern struct device_node *of_find_node_by_phandle(phandle handle);
82extern struct device_node *of_get_parent(const struct device_node *node);
83extern struct device_node *of_get_next_child(const struct device_node *node,
84 struct device_node *prev);
85extern struct property *of_find_property(struct device_node *np,
86 const char *name,
87 int *lenp);
88extern int of_device_is_compatible(struct device_node *device, const char *);
89extern void *of_get_property(struct device_node *node, const char *name,
90 int *lenp);
91extern int of_getintprop_default(struct device_node *np,
92 const char *name,
93 int def);
94
95extern void prom_build_devicetree(void);
96
97#endif /* __KERNEL__ */
98#endif /* _SPARC_PROM_H */