[SPARC64]: Always register a PROM based early console.
[linux-2.6/kmemtrace.git] / arch / sparc64 / kernel / prom.c
blob68964ddcde1e53bd5ce77207815cfe0bbc2068f3
1 /*
2 * Procedures for creating, accessing and interpreting the device tree.
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
10 * Adapted for sparc64 by David S. Miller davem@davemloft.net
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.
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>
25 #include <asm/prom.h>
26 #include <asm/of_device.h>
27 #include <asm/oplib.h>
28 #include <asm/irq.h>
29 #include <asm/asi.h>
30 #include <asm/upa.h>
31 #include <asm/smp.h>
33 extern struct device_node *allnodes; /* temporary while merging */
35 extern rwlock_t devtree_lock; /* temporary while merging */
37 struct device_node *of_find_node_by_phandle(phandle handle)
39 struct device_node *np;
41 for (np = allnodes; np != 0; np = np->allnext)
42 if (np->node == handle)
43 break;
45 return np;
47 EXPORT_SYMBOL(of_find_node_by_phandle);
49 int of_getintprop_default(struct device_node *np, const char *name, int def)
51 struct property *prop;
52 int len;
54 prop = of_find_property(np, name, &len);
55 if (!prop || len != 4)
56 return def;
58 return *(int *) prop->value;
60 EXPORT_SYMBOL(of_getintprop_default);
62 int of_set_property(struct device_node *dp, const char *name, void *val, int len)
64 struct property **prevp;
65 void *new_val;
66 int err;
68 new_val = kmalloc(len, GFP_KERNEL);
69 if (!new_val)
70 return -ENOMEM;
72 memcpy(new_val, val, len);
74 err = -ENODEV;
76 write_lock(&devtree_lock);
77 prevp = &dp->properties;
78 while (*prevp) {
79 struct property *prop = *prevp;
81 if (!strcasecmp(prop->name, name)) {
82 void *old_val = prop->value;
83 int ret;
85 ret = prom_setprop(dp->node, name, val, len);
86 err = -EINVAL;
87 if (ret >= 0) {
88 prop->value = new_val;
89 prop->length = len;
91 if (OF_IS_DYNAMIC(prop))
92 kfree(old_val);
94 OF_MARK_DYNAMIC(prop);
96 err = 0;
98 break;
100 prevp = &(*prevp)->next;
102 write_unlock(&devtree_lock);
104 /* XXX Upate procfs if necessary... */
106 return err;
108 EXPORT_SYMBOL(of_set_property);
110 int of_find_in_proplist(const char *list, const char *match, int len)
112 while (len > 0) {
113 int l;
115 if (!strcmp(list, match))
116 return 1;
117 l = strlen(list) + 1;
118 list += l;
119 len -= l;
121 return 0;
123 EXPORT_SYMBOL(of_find_in_proplist);
125 static unsigned int prom_early_allocated;
127 static void * __init prom_early_alloc(unsigned long size)
129 void *ret;
131 ret = __alloc_bootmem(size, SMP_CACHE_BYTES, 0UL);
132 if (ret != NULL)
133 memset(ret, 0, size);
135 prom_early_allocated += size;
137 return ret;
140 #ifdef CONFIG_PCI
141 /* PSYCHO interrupt mapping support. */
142 #define PSYCHO_IMAP_A_SLOT0 0x0c00UL
143 #define PSYCHO_IMAP_B_SLOT0 0x0c20UL
144 static unsigned long psycho_pcislot_imap_offset(unsigned long ino)
146 unsigned int bus = (ino & 0x10) >> 4;
147 unsigned int slot = (ino & 0x0c) >> 2;
149 if (bus == 0)
150 return PSYCHO_IMAP_A_SLOT0 + (slot * 8);
151 else
152 return PSYCHO_IMAP_B_SLOT0 + (slot * 8);
155 #define PSYCHO_IMAP_SCSI 0x1000UL
156 #define PSYCHO_IMAP_ETH 0x1008UL
157 #define PSYCHO_IMAP_BPP 0x1010UL
158 #define PSYCHO_IMAP_AU_REC 0x1018UL
159 #define PSYCHO_IMAP_AU_PLAY 0x1020UL
160 #define PSYCHO_IMAP_PFAIL 0x1028UL
161 #define PSYCHO_IMAP_KMS 0x1030UL
162 #define PSYCHO_IMAP_FLPY 0x1038UL
163 #define PSYCHO_IMAP_SHW 0x1040UL
164 #define PSYCHO_IMAP_KBD 0x1048UL
165 #define PSYCHO_IMAP_MS 0x1050UL
166 #define PSYCHO_IMAP_SER 0x1058UL
167 #define PSYCHO_IMAP_TIM0 0x1060UL
168 #define PSYCHO_IMAP_TIM1 0x1068UL
169 #define PSYCHO_IMAP_UE 0x1070UL
170 #define PSYCHO_IMAP_CE 0x1078UL
171 #define PSYCHO_IMAP_A_ERR 0x1080UL
172 #define PSYCHO_IMAP_B_ERR 0x1088UL
173 #define PSYCHO_IMAP_PMGMT 0x1090UL
174 #define PSYCHO_IMAP_GFX 0x1098UL
175 #define PSYCHO_IMAP_EUPA 0x10a0UL
177 static unsigned long __psycho_onboard_imap_off[] = {
178 /*0x20*/ PSYCHO_IMAP_SCSI,
179 /*0x21*/ PSYCHO_IMAP_ETH,
180 /*0x22*/ PSYCHO_IMAP_BPP,
181 /*0x23*/ PSYCHO_IMAP_AU_REC,
182 /*0x24*/ PSYCHO_IMAP_AU_PLAY,
183 /*0x25*/ PSYCHO_IMAP_PFAIL,
184 /*0x26*/ PSYCHO_IMAP_KMS,
185 /*0x27*/ PSYCHO_IMAP_FLPY,
186 /*0x28*/ PSYCHO_IMAP_SHW,
187 /*0x29*/ PSYCHO_IMAP_KBD,
188 /*0x2a*/ PSYCHO_IMAP_MS,
189 /*0x2b*/ PSYCHO_IMAP_SER,
190 /*0x2c*/ PSYCHO_IMAP_TIM0,
191 /*0x2d*/ PSYCHO_IMAP_TIM1,
192 /*0x2e*/ PSYCHO_IMAP_UE,
193 /*0x2f*/ PSYCHO_IMAP_CE,
194 /*0x30*/ PSYCHO_IMAP_A_ERR,
195 /*0x31*/ PSYCHO_IMAP_B_ERR,
196 /*0x32*/ PSYCHO_IMAP_PMGMT,
197 /*0x33*/ PSYCHO_IMAP_GFX,
198 /*0x34*/ PSYCHO_IMAP_EUPA,
200 #define PSYCHO_ONBOARD_IRQ_BASE 0x20
201 #define PSYCHO_ONBOARD_IRQ_LAST 0x34
202 #define psycho_onboard_imap_offset(__ino) \
203 __psycho_onboard_imap_off[(__ino) - PSYCHO_ONBOARD_IRQ_BASE]
205 #define PSYCHO_ICLR_A_SLOT0 0x1400UL
206 #define PSYCHO_ICLR_SCSI 0x1800UL
208 #define psycho_iclr_offset(ino) \
209 ((ino & 0x20) ? (PSYCHO_ICLR_SCSI + (((ino) & 0x1f) << 3)) : \
210 (PSYCHO_ICLR_A_SLOT0 + (((ino) & 0x1f)<<3)))
212 static unsigned int psycho_irq_build(struct device_node *dp,
213 unsigned int ino,
214 void *_data)
216 unsigned long controller_regs = (unsigned long) _data;
217 unsigned long imap, iclr;
218 unsigned long imap_off, iclr_off;
219 int inofixup = 0;
221 ino &= 0x3f;
222 if (ino < PSYCHO_ONBOARD_IRQ_BASE) {
223 /* PCI slot */
224 imap_off = psycho_pcislot_imap_offset(ino);
225 } else {
226 /* Onboard device */
227 if (ino > PSYCHO_ONBOARD_IRQ_LAST) {
228 prom_printf("psycho_irq_build: Wacky INO [%x]\n", ino);
229 prom_halt();
231 imap_off = psycho_onboard_imap_offset(ino);
234 /* Now build the IRQ bucket. */
235 imap = controller_regs + imap_off;
237 iclr_off = psycho_iclr_offset(ino);
238 iclr = controller_regs + iclr_off;
240 if ((ino & 0x20) == 0)
241 inofixup = ino & 0x03;
243 return build_irq(inofixup, iclr, imap);
246 static void __init psycho_irq_trans_init(struct device_node *dp)
248 const struct linux_prom64_registers *regs;
250 dp->irq_trans = prom_early_alloc(sizeof(struct of_irq_controller));
251 dp->irq_trans->irq_build = psycho_irq_build;
253 regs = of_get_property(dp, "reg", NULL);
254 dp->irq_trans->data = (void *) regs[2].phys_addr;
257 #define sabre_read(__reg) \
258 ({ u64 __ret; \
259 __asm__ __volatile__("ldxa [%1] %2, %0" \
260 : "=r" (__ret) \
261 : "r" (__reg), "i" (ASI_PHYS_BYPASS_EC_E) \
262 : "memory"); \
263 __ret; \
266 struct sabre_irq_data {
267 unsigned long controller_regs;
268 unsigned int pci_first_busno;
270 #define SABRE_CONFIGSPACE 0x001000000UL
271 #define SABRE_WRSYNC 0x1c20UL
273 #define SABRE_CONFIG_BASE(CONFIG_SPACE) \
274 (CONFIG_SPACE | (1UL << 24))
275 #define SABRE_CONFIG_ENCODE(BUS, DEVFN, REG) \
276 (((unsigned long)(BUS) << 16) | \
277 ((unsigned long)(DEVFN) << 8) | \
278 ((unsigned long)(REG)))
280 /* When a device lives behind a bridge deeper in the PCI bus topology
281 * than APB, a special sequence must run to make sure all pending DMA
282 * transfers at the time of IRQ delivery are visible in the coherency
283 * domain by the cpu. This sequence is to perform a read on the far
284 * side of the non-APB bridge, then perform a read of Sabre's DMA
285 * write-sync register.
287 static void sabre_wsync_handler(unsigned int ino, void *_arg1, void *_arg2)
289 unsigned int phys_hi = (unsigned int) (unsigned long) _arg1;
290 struct sabre_irq_data *irq_data = _arg2;
291 unsigned long controller_regs = irq_data->controller_regs;
292 unsigned long sync_reg = controller_regs + SABRE_WRSYNC;
293 unsigned long config_space = controller_regs + SABRE_CONFIGSPACE;
294 unsigned int bus, devfn;
295 u16 _unused;
297 config_space = SABRE_CONFIG_BASE(config_space);
299 bus = (phys_hi >> 16) & 0xff;
300 devfn = (phys_hi >> 8) & 0xff;
302 config_space |= SABRE_CONFIG_ENCODE(bus, devfn, 0x00);
304 __asm__ __volatile__("membar #Sync\n\t"
305 "lduha [%1] %2, %0\n\t"
306 "membar #Sync"
307 : "=r" (_unused)
308 : "r" ((u16 *) config_space),
309 "i" (ASI_PHYS_BYPASS_EC_E_L)
310 : "memory");
312 sabre_read(sync_reg);
315 #define SABRE_IMAP_A_SLOT0 0x0c00UL
316 #define SABRE_IMAP_B_SLOT0 0x0c20UL
317 #define SABRE_IMAP_SCSI 0x1000UL
318 #define SABRE_IMAP_ETH 0x1008UL
319 #define SABRE_IMAP_BPP 0x1010UL
320 #define SABRE_IMAP_AU_REC 0x1018UL
321 #define SABRE_IMAP_AU_PLAY 0x1020UL
322 #define SABRE_IMAP_PFAIL 0x1028UL
323 #define SABRE_IMAP_KMS 0x1030UL
324 #define SABRE_IMAP_FLPY 0x1038UL
325 #define SABRE_IMAP_SHW 0x1040UL
326 #define SABRE_IMAP_KBD 0x1048UL
327 #define SABRE_IMAP_MS 0x1050UL
328 #define SABRE_IMAP_SER 0x1058UL
329 #define SABRE_IMAP_UE 0x1070UL
330 #define SABRE_IMAP_CE 0x1078UL
331 #define SABRE_IMAP_PCIERR 0x1080UL
332 #define SABRE_IMAP_GFX 0x1098UL
333 #define SABRE_IMAP_EUPA 0x10a0UL
334 #define SABRE_ICLR_A_SLOT0 0x1400UL
335 #define SABRE_ICLR_B_SLOT0 0x1480UL
336 #define SABRE_ICLR_SCSI 0x1800UL
337 #define SABRE_ICLR_ETH 0x1808UL
338 #define SABRE_ICLR_BPP 0x1810UL
339 #define SABRE_ICLR_AU_REC 0x1818UL
340 #define SABRE_ICLR_AU_PLAY 0x1820UL
341 #define SABRE_ICLR_PFAIL 0x1828UL
342 #define SABRE_ICLR_KMS 0x1830UL
343 #define SABRE_ICLR_FLPY 0x1838UL
344 #define SABRE_ICLR_SHW 0x1840UL
345 #define SABRE_ICLR_KBD 0x1848UL
346 #define SABRE_ICLR_MS 0x1850UL
347 #define SABRE_ICLR_SER 0x1858UL
348 #define SABRE_ICLR_UE 0x1870UL
349 #define SABRE_ICLR_CE 0x1878UL
350 #define SABRE_ICLR_PCIERR 0x1880UL
352 static unsigned long sabre_pcislot_imap_offset(unsigned long ino)
354 unsigned int bus = (ino & 0x10) >> 4;
355 unsigned int slot = (ino & 0x0c) >> 2;
357 if (bus == 0)
358 return SABRE_IMAP_A_SLOT0 + (slot * 8);
359 else
360 return SABRE_IMAP_B_SLOT0 + (slot * 8);
363 static unsigned long __sabre_onboard_imap_off[] = {
364 /*0x20*/ SABRE_IMAP_SCSI,
365 /*0x21*/ SABRE_IMAP_ETH,
366 /*0x22*/ SABRE_IMAP_BPP,
367 /*0x23*/ SABRE_IMAP_AU_REC,
368 /*0x24*/ SABRE_IMAP_AU_PLAY,
369 /*0x25*/ SABRE_IMAP_PFAIL,
370 /*0x26*/ SABRE_IMAP_KMS,
371 /*0x27*/ SABRE_IMAP_FLPY,
372 /*0x28*/ SABRE_IMAP_SHW,
373 /*0x29*/ SABRE_IMAP_KBD,
374 /*0x2a*/ SABRE_IMAP_MS,
375 /*0x2b*/ SABRE_IMAP_SER,
376 /*0x2c*/ 0 /* reserved */,
377 /*0x2d*/ 0 /* reserved */,
378 /*0x2e*/ SABRE_IMAP_UE,
379 /*0x2f*/ SABRE_IMAP_CE,
380 /*0x30*/ SABRE_IMAP_PCIERR,
381 /*0x31*/ 0 /* reserved */,
382 /*0x32*/ 0 /* reserved */,
383 /*0x33*/ SABRE_IMAP_GFX,
384 /*0x34*/ SABRE_IMAP_EUPA,
386 #define SABRE_ONBOARD_IRQ_BASE 0x20
387 #define SABRE_ONBOARD_IRQ_LAST 0x30
388 #define sabre_onboard_imap_offset(__ino) \
389 __sabre_onboard_imap_off[(__ino) - SABRE_ONBOARD_IRQ_BASE]
391 #define sabre_iclr_offset(ino) \
392 ((ino & 0x20) ? (SABRE_ICLR_SCSI + (((ino) & 0x1f) << 3)) : \
393 (SABRE_ICLR_A_SLOT0 + (((ino) & 0x1f)<<3)))
395 static int sabre_device_needs_wsync(struct device_node *dp)
397 struct device_node *parent = dp->parent;
398 const char *parent_model, *parent_compat;
400 /* This traversal up towards the root is meant to
401 * handle two cases:
403 * 1) non-PCI bus sitting under PCI, such as 'ebus'
404 * 2) the PCI controller interrupts themselves, which
405 * will use the sabre_irq_build but do not need
406 * the DMA synchronization handling
408 while (parent) {
409 if (!strcmp(parent->type, "pci"))
410 break;
411 parent = parent->parent;
414 if (!parent)
415 return 0;
417 parent_model = of_get_property(parent,
418 "model", NULL);
419 if (parent_model &&
420 (!strcmp(parent_model, "SUNW,sabre") ||
421 !strcmp(parent_model, "SUNW,simba")))
422 return 0;
424 parent_compat = of_get_property(parent,
425 "compatible", NULL);
426 if (parent_compat &&
427 (!strcmp(parent_compat, "pci108e,a000") ||
428 !strcmp(parent_compat, "pci108e,a001")))
429 return 0;
431 return 1;
434 static unsigned int sabre_irq_build(struct device_node *dp,
435 unsigned int ino,
436 void *_data)
438 struct sabre_irq_data *irq_data = _data;
439 unsigned long controller_regs = irq_data->controller_regs;
440 const struct linux_prom_pci_registers *regs;
441 unsigned long imap, iclr;
442 unsigned long imap_off, iclr_off;
443 int inofixup = 0;
444 int virt_irq;
446 ino &= 0x3f;
447 if (ino < SABRE_ONBOARD_IRQ_BASE) {
448 /* PCI slot */
449 imap_off = sabre_pcislot_imap_offset(ino);
450 } else {
451 /* onboard device */
452 if (ino > SABRE_ONBOARD_IRQ_LAST) {
453 prom_printf("sabre_irq_build: Wacky INO [%x]\n", ino);
454 prom_halt();
456 imap_off = sabre_onboard_imap_offset(ino);
459 /* Now build the IRQ bucket. */
460 imap = controller_regs + imap_off;
462 iclr_off = sabre_iclr_offset(ino);
463 iclr = controller_regs + iclr_off;
465 if ((ino & 0x20) == 0)
466 inofixup = ino & 0x03;
468 virt_irq = build_irq(inofixup, iclr, imap);
470 /* If the parent device is a PCI<->PCI bridge other than
471 * APB, we have to install a pre-handler to ensure that
472 * all pending DMA is drained before the interrupt handler
473 * is run.
475 regs = of_get_property(dp, "reg", NULL);
476 if (regs && sabre_device_needs_wsync(dp)) {
477 irq_install_pre_handler(virt_irq,
478 sabre_wsync_handler,
479 (void *) (long) regs->phys_hi,
480 (void *) irq_data);
483 return virt_irq;
486 static void __init sabre_irq_trans_init(struct device_node *dp)
488 const struct linux_prom64_registers *regs;
489 struct sabre_irq_data *irq_data;
490 const u32 *busrange;
492 dp->irq_trans = prom_early_alloc(sizeof(struct of_irq_controller));
493 dp->irq_trans->irq_build = sabre_irq_build;
495 irq_data = prom_early_alloc(sizeof(struct sabre_irq_data));
497 regs = of_get_property(dp, "reg", NULL);
498 irq_data->controller_regs = regs[0].phys_addr;
500 busrange = of_get_property(dp, "bus-range", NULL);
501 irq_data->pci_first_busno = busrange[0];
503 dp->irq_trans->data = irq_data;
506 /* SCHIZO interrupt mapping support. Unlike Psycho, for this controller the
507 * imap/iclr registers are per-PBM.
509 #define SCHIZO_IMAP_BASE 0x1000UL
510 #define SCHIZO_ICLR_BASE 0x1400UL
512 static unsigned long schizo_imap_offset(unsigned long ino)
514 return SCHIZO_IMAP_BASE + (ino * 8UL);
517 static unsigned long schizo_iclr_offset(unsigned long ino)
519 return SCHIZO_ICLR_BASE + (ino * 8UL);
522 static unsigned long schizo_ino_to_iclr(unsigned long pbm_regs,
523 unsigned int ino)
526 return pbm_regs + schizo_iclr_offset(ino);
529 static unsigned long schizo_ino_to_imap(unsigned long pbm_regs,
530 unsigned int ino)
532 return pbm_regs + schizo_imap_offset(ino);
535 #define schizo_read(__reg) \
536 ({ u64 __ret; \
537 __asm__ __volatile__("ldxa [%1] %2, %0" \
538 : "=r" (__ret) \
539 : "r" (__reg), "i" (ASI_PHYS_BYPASS_EC_E) \
540 : "memory"); \
541 __ret; \
543 #define schizo_write(__reg, __val) \
544 __asm__ __volatile__("stxa %0, [%1] %2" \
545 : /* no outputs */ \
546 : "r" (__val), "r" (__reg), \
547 "i" (ASI_PHYS_BYPASS_EC_E) \
548 : "memory")
550 static void tomatillo_wsync_handler(unsigned int ino, void *_arg1, void *_arg2)
552 unsigned long sync_reg = (unsigned long) _arg2;
553 u64 mask = 1UL << (ino & IMAP_INO);
554 u64 val;
555 int limit;
557 schizo_write(sync_reg, mask);
559 limit = 100000;
560 val = 0;
561 while (--limit) {
562 val = schizo_read(sync_reg);
563 if (!(val & mask))
564 break;
566 if (limit <= 0) {
567 printk("tomatillo_wsync_handler: DMA won't sync [%lx:%lx]\n",
568 val, mask);
571 if (_arg1) {
572 static unsigned char cacheline[64]
573 __attribute__ ((aligned (64)));
575 __asm__ __volatile__("rd %%fprs, %0\n\t"
576 "or %0, %4, %1\n\t"
577 "wr %1, 0x0, %%fprs\n\t"
578 "stda %%f0, [%5] %6\n\t"
579 "wr %0, 0x0, %%fprs\n\t"
580 "membar #Sync"
581 : "=&r" (mask), "=&r" (val)
582 : "0" (mask), "1" (val),
583 "i" (FPRS_FEF), "r" (&cacheline[0]),
584 "i" (ASI_BLK_COMMIT_P));
588 struct schizo_irq_data {
589 unsigned long pbm_regs;
590 unsigned long sync_reg;
591 u32 portid;
592 int chip_version;
595 static unsigned int schizo_irq_build(struct device_node *dp,
596 unsigned int ino,
597 void *_data)
599 struct schizo_irq_data *irq_data = _data;
600 unsigned long pbm_regs = irq_data->pbm_regs;
601 unsigned long imap, iclr;
602 int ign_fixup;
603 int virt_irq;
604 int is_tomatillo;
606 ino &= 0x3f;
608 /* Now build the IRQ bucket. */
609 imap = schizo_ino_to_imap(pbm_regs, ino);
610 iclr = schizo_ino_to_iclr(pbm_regs, ino);
612 /* On Schizo, no inofixup occurs. This is because each
613 * INO has it's own IMAP register. On Psycho and Sabre
614 * there is only one IMAP register for each PCI slot even
615 * though four different INOs can be generated by each
616 * PCI slot.
618 * But, for JBUS variants (essentially, Tomatillo), we have
619 * to fixup the lowest bit of the interrupt group number.
621 ign_fixup = 0;
623 is_tomatillo = (irq_data->sync_reg != 0UL);
625 if (is_tomatillo) {
626 if (irq_data->portid & 1)
627 ign_fixup = (1 << 6);
630 virt_irq = build_irq(ign_fixup, iclr, imap);
632 if (is_tomatillo) {
633 irq_install_pre_handler(virt_irq,
634 tomatillo_wsync_handler,
635 ((irq_data->chip_version <= 4) ?
636 (void *) 1 : (void *) 0),
637 (void *) irq_data->sync_reg);
640 return virt_irq;
643 static void __init __schizo_irq_trans_init(struct device_node *dp,
644 int is_tomatillo)
646 const struct linux_prom64_registers *regs;
647 struct schizo_irq_data *irq_data;
649 dp->irq_trans = prom_early_alloc(sizeof(struct of_irq_controller));
650 dp->irq_trans->irq_build = schizo_irq_build;
652 irq_data = prom_early_alloc(sizeof(struct schizo_irq_data));
654 regs = of_get_property(dp, "reg", NULL);
655 dp->irq_trans->data = irq_data;
657 irq_data->pbm_regs = regs[0].phys_addr;
658 if (is_tomatillo)
659 irq_data->sync_reg = regs[3].phys_addr + 0x1a18UL;
660 else
661 irq_data->sync_reg = 0UL;
662 irq_data->portid = of_getintprop_default(dp, "portid", 0);
663 irq_data->chip_version = of_getintprop_default(dp, "version#", 0);
666 static void __init schizo_irq_trans_init(struct device_node *dp)
668 __schizo_irq_trans_init(dp, 0);
671 static void __init tomatillo_irq_trans_init(struct device_node *dp)
673 __schizo_irq_trans_init(dp, 1);
676 static unsigned int pci_sun4v_irq_build(struct device_node *dp,
677 unsigned int devino,
678 void *_data)
680 u32 devhandle = (u32) (unsigned long) _data;
682 return sun4v_build_irq(devhandle, devino);
685 static void __init pci_sun4v_irq_trans_init(struct device_node *dp)
687 const struct linux_prom64_registers *regs;
689 dp->irq_trans = prom_early_alloc(sizeof(struct of_irq_controller));
690 dp->irq_trans->irq_build = pci_sun4v_irq_build;
692 regs = of_get_property(dp, "reg", NULL);
693 dp->irq_trans->data = (void *) (unsigned long)
694 ((regs->phys_addr >> 32UL) & 0x0fffffff);
697 struct fire_irq_data {
698 unsigned long pbm_regs;
699 u32 portid;
702 #define FIRE_IMAP_BASE 0x001000
703 #define FIRE_ICLR_BASE 0x001400
705 static unsigned long fire_imap_offset(unsigned long ino)
707 return FIRE_IMAP_BASE + (ino * 8UL);
710 static unsigned long fire_iclr_offset(unsigned long ino)
712 return FIRE_ICLR_BASE + (ino * 8UL);
715 static unsigned long fire_ino_to_iclr(unsigned long pbm_regs,
716 unsigned int ino)
718 return pbm_regs + fire_iclr_offset(ino);
721 static unsigned long fire_ino_to_imap(unsigned long pbm_regs,
722 unsigned int ino)
724 return pbm_regs + fire_imap_offset(ino);
727 static unsigned int fire_irq_build(struct device_node *dp,
728 unsigned int ino,
729 void *_data)
731 struct fire_irq_data *irq_data = _data;
732 unsigned long pbm_regs = irq_data->pbm_regs;
733 unsigned long imap, iclr;
734 unsigned long int_ctrlr;
736 ino &= 0x3f;
738 /* Now build the IRQ bucket. */
739 imap = fire_ino_to_imap(pbm_regs, ino);
740 iclr = fire_ino_to_iclr(pbm_regs, ino);
742 /* Set the interrupt controller number. */
743 int_ctrlr = 1 << 6;
744 upa_writeq(int_ctrlr, imap);
746 /* The interrupt map registers do not have an INO field
747 * like other chips do. They return zero in the INO
748 * field, and the interrupt controller number is controlled
749 * in bits 6 to 9. So in order for build_irq() to get
750 * the INO right we pass it in as part of the fixup
751 * which will get added to the map register zero value
752 * read by build_irq().
754 ino |= (irq_data->portid << 6);
755 ino -= int_ctrlr;
756 return build_irq(ino, iclr, imap);
759 static void __init fire_irq_trans_init(struct device_node *dp)
761 const struct linux_prom64_registers *regs;
762 struct fire_irq_data *irq_data;
764 dp->irq_trans = prom_early_alloc(sizeof(struct of_irq_controller));
765 dp->irq_trans->irq_build = fire_irq_build;
767 irq_data = prom_early_alloc(sizeof(struct fire_irq_data));
769 regs = of_get_property(dp, "reg", NULL);
770 dp->irq_trans->data = irq_data;
772 irq_data->pbm_regs = regs[0].phys_addr;
773 irq_data->portid = of_getintprop_default(dp, "portid", 0);
775 #endif /* CONFIG_PCI */
777 #ifdef CONFIG_SBUS
778 /* INO number to IMAP register offset for SYSIO external IRQ's.
779 * This should conform to both Sunfire/Wildfire server and Fusion
780 * desktop designs.
782 #define SYSIO_IMAP_SLOT0 0x2c00UL
783 #define SYSIO_IMAP_SLOT1 0x2c08UL
784 #define SYSIO_IMAP_SLOT2 0x2c10UL
785 #define SYSIO_IMAP_SLOT3 0x2c18UL
786 #define SYSIO_IMAP_SCSI 0x3000UL
787 #define SYSIO_IMAP_ETH 0x3008UL
788 #define SYSIO_IMAP_BPP 0x3010UL
789 #define SYSIO_IMAP_AUDIO 0x3018UL
790 #define SYSIO_IMAP_PFAIL 0x3020UL
791 #define SYSIO_IMAP_KMS 0x3028UL
792 #define SYSIO_IMAP_FLPY 0x3030UL
793 #define SYSIO_IMAP_SHW 0x3038UL
794 #define SYSIO_IMAP_KBD 0x3040UL
795 #define SYSIO_IMAP_MS 0x3048UL
796 #define SYSIO_IMAP_SER 0x3050UL
797 #define SYSIO_IMAP_TIM0 0x3060UL
798 #define SYSIO_IMAP_TIM1 0x3068UL
799 #define SYSIO_IMAP_UE 0x3070UL
800 #define SYSIO_IMAP_CE 0x3078UL
801 #define SYSIO_IMAP_SBERR 0x3080UL
802 #define SYSIO_IMAP_PMGMT 0x3088UL
803 #define SYSIO_IMAP_GFX 0x3090UL
804 #define SYSIO_IMAP_EUPA 0x3098UL
806 #define bogon ((unsigned long) -1)
807 static unsigned long sysio_irq_offsets[] = {
808 /* SBUS Slot 0 --> 3, level 1 --> 7 */
809 SYSIO_IMAP_SLOT0, SYSIO_IMAP_SLOT0, SYSIO_IMAP_SLOT0, SYSIO_IMAP_SLOT0,
810 SYSIO_IMAP_SLOT0, SYSIO_IMAP_SLOT0, SYSIO_IMAP_SLOT0, SYSIO_IMAP_SLOT0,
811 SYSIO_IMAP_SLOT1, SYSIO_IMAP_SLOT1, SYSIO_IMAP_SLOT1, SYSIO_IMAP_SLOT1,
812 SYSIO_IMAP_SLOT1, SYSIO_IMAP_SLOT1, SYSIO_IMAP_SLOT1, SYSIO_IMAP_SLOT1,
813 SYSIO_IMAP_SLOT2, SYSIO_IMAP_SLOT2, SYSIO_IMAP_SLOT2, SYSIO_IMAP_SLOT2,
814 SYSIO_IMAP_SLOT2, SYSIO_IMAP_SLOT2, SYSIO_IMAP_SLOT2, SYSIO_IMAP_SLOT2,
815 SYSIO_IMAP_SLOT3, SYSIO_IMAP_SLOT3, SYSIO_IMAP_SLOT3, SYSIO_IMAP_SLOT3,
816 SYSIO_IMAP_SLOT3, SYSIO_IMAP_SLOT3, SYSIO_IMAP_SLOT3, SYSIO_IMAP_SLOT3,
818 /* Onboard devices (not relevant/used on SunFire). */
819 SYSIO_IMAP_SCSI,
820 SYSIO_IMAP_ETH,
821 SYSIO_IMAP_BPP,
822 bogon,
823 SYSIO_IMAP_AUDIO,
824 SYSIO_IMAP_PFAIL,
825 bogon,
826 bogon,
827 SYSIO_IMAP_KMS,
828 SYSIO_IMAP_FLPY,
829 SYSIO_IMAP_SHW,
830 SYSIO_IMAP_KBD,
831 SYSIO_IMAP_MS,
832 SYSIO_IMAP_SER,
833 bogon,
834 bogon,
835 SYSIO_IMAP_TIM0,
836 SYSIO_IMAP_TIM1,
837 bogon,
838 bogon,
839 SYSIO_IMAP_UE,
840 SYSIO_IMAP_CE,
841 SYSIO_IMAP_SBERR,
842 SYSIO_IMAP_PMGMT,
843 SYSIO_IMAP_GFX,
844 SYSIO_IMAP_EUPA,
847 #undef bogon
849 #define NUM_SYSIO_OFFSETS ARRAY_SIZE(sysio_irq_offsets)
851 /* Convert Interrupt Mapping register pointer to associated
852 * Interrupt Clear register pointer, SYSIO specific version.
854 #define SYSIO_ICLR_UNUSED0 0x3400UL
855 #define SYSIO_ICLR_SLOT0 0x3408UL
856 #define SYSIO_ICLR_SLOT1 0x3448UL
857 #define SYSIO_ICLR_SLOT2 0x3488UL
858 #define SYSIO_ICLR_SLOT3 0x34c8UL
859 static unsigned long sysio_imap_to_iclr(unsigned long imap)
861 unsigned long diff = SYSIO_ICLR_UNUSED0 - SYSIO_IMAP_SLOT0;
862 return imap + diff;
865 static unsigned int sbus_of_build_irq(struct device_node *dp,
866 unsigned int ino,
867 void *_data)
869 unsigned long reg_base = (unsigned long) _data;
870 const struct linux_prom_registers *regs;
871 unsigned long imap, iclr;
872 int sbus_slot = 0;
873 int sbus_level = 0;
875 ino &= 0x3f;
877 regs = of_get_property(dp, "reg", NULL);
878 if (regs)
879 sbus_slot = regs->which_io;
881 if (ino < 0x20)
882 ino += (sbus_slot * 8);
884 imap = sysio_irq_offsets[ino];
885 if (imap == ((unsigned long)-1)) {
886 prom_printf("get_irq_translations: Bad SYSIO INO[%x]\n",
887 ino);
888 prom_halt();
890 imap += reg_base;
892 /* SYSIO inconsistency. For external SLOTS, we have to select
893 * the right ICLR register based upon the lower SBUS irq level
894 * bits.
896 if (ino >= 0x20) {
897 iclr = sysio_imap_to_iclr(imap);
898 } else {
899 sbus_level = ino & 0x7;
901 switch(sbus_slot) {
902 case 0:
903 iclr = reg_base + SYSIO_ICLR_SLOT0;
904 break;
905 case 1:
906 iclr = reg_base + SYSIO_ICLR_SLOT1;
907 break;
908 case 2:
909 iclr = reg_base + SYSIO_ICLR_SLOT2;
910 break;
911 default:
912 case 3:
913 iclr = reg_base + SYSIO_ICLR_SLOT3;
914 break;
917 iclr += ((unsigned long)sbus_level - 1UL) * 8UL;
919 return build_irq(sbus_level, iclr, imap);
922 static void __init sbus_irq_trans_init(struct device_node *dp)
924 const struct linux_prom64_registers *regs;
926 dp->irq_trans = prom_early_alloc(sizeof(struct of_irq_controller));
927 dp->irq_trans->irq_build = sbus_of_build_irq;
929 regs = of_get_property(dp, "reg", NULL);
930 dp->irq_trans->data = (void *) (unsigned long) regs->phys_addr;
932 #endif /* CONFIG_SBUS */
935 static unsigned int central_build_irq(struct device_node *dp,
936 unsigned int ino,
937 void *_data)
939 struct device_node *central_dp = _data;
940 struct of_device *central_op = of_find_device_by_node(central_dp);
941 struct resource *res;
942 unsigned long imap, iclr;
943 u32 tmp;
945 if (!strcmp(dp->name, "eeprom")) {
946 res = &central_op->resource[5];
947 } else if (!strcmp(dp->name, "zs")) {
948 res = &central_op->resource[4];
949 } else if (!strcmp(dp->name, "clock-board")) {
950 res = &central_op->resource[3];
951 } else {
952 return ino;
955 imap = res->start + 0x00UL;
956 iclr = res->start + 0x10UL;
958 /* Set the INO state to idle, and disable. */
959 upa_writel(0, iclr);
960 upa_readl(iclr);
962 tmp = upa_readl(imap);
963 tmp &= ~0x80000000;
964 upa_writel(tmp, imap);
966 return build_irq(0, iclr, imap);
969 static void __init central_irq_trans_init(struct device_node *dp)
971 dp->irq_trans = prom_early_alloc(sizeof(struct of_irq_controller));
972 dp->irq_trans->irq_build = central_build_irq;
974 dp->irq_trans->data = dp;
977 struct irq_trans {
978 const char *name;
979 void (*init)(struct device_node *);
982 #ifdef CONFIG_PCI
983 static struct irq_trans __initdata pci_irq_trans_table[] = {
984 { "SUNW,sabre", sabre_irq_trans_init },
985 { "pci108e,a000", sabre_irq_trans_init },
986 { "pci108e,a001", sabre_irq_trans_init },
987 { "SUNW,psycho", psycho_irq_trans_init },
988 { "pci108e,8000", psycho_irq_trans_init },
989 { "SUNW,schizo", schizo_irq_trans_init },
990 { "pci108e,8001", schizo_irq_trans_init },
991 { "SUNW,schizo+", schizo_irq_trans_init },
992 { "pci108e,8002", schizo_irq_trans_init },
993 { "SUNW,tomatillo", tomatillo_irq_trans_init },
994 { "pci108e,a801", tomatillo_irq_trans_init },
995 { "SUNW,sun4v-pci", pci_sun4v_irq_trans_init },
996 { "pciex108e,80f0", fire_irq_trans_init },
998 #endif
1000 static unsigned int sun4v_vdev_irq_build(struct device_node *dp,
1001 unsigned int devino,
1002 void *_data)
1004 u32 devhandle = (u32) (unsigned long) _data;
1006 return sun4v_build_irq(devhandle, devino);
1009 static void __init sun4v_vdev_irq_trans_init(struct device_node *dp)
1011 const struct linux_prom64_registers *regs;
1013 dp->irq_trans = prom_early_alloc(sizeof(struct of_irq_controller));
1014 dp->irq_trans->irq_build = sun4v_vdev_irq_build;
1016 regs = of_get_property(dp, "reg", NULL);
1017 dp->irq_trans->data = (void *) (unsigned long)
1018 ((regs->phys_addr >> 32UL) & 0x0fffffff);
1021 static void __init irq_trans_init(struct device_node *dp)
1023 #ifdef CONFIG_PCI
1024 const char *model;
1025 int i;
1026 #endif
1028 #ifdef CONFIG_PCI
1029 model = of_get_property(dp, "model", NULL);
1030 if (!model)
1031 model = of_get_property(dp, "compatible", NULL);
1032 if (model) {
1033 for (i = 0; i < ARRAY_SIZE(pci_irq_trans_table); i++) {
1034 struct irq_trans *t = &pci_irq_trans_table[i];
1036 if (!strcmp(model, t->name))
1037 return t->init(dp);
1040 #endif
1041 #ifdef CONFIG_SBUS
1042 if (!strcmp(dp->name, "sbus") ||
1043 !strcmp(dp->name, "sbi"))
1044 return sbus_irq_trans_init(dp);
1045 #endif
1046 if (!strcmp(dp->name, "fhc") &&
1047 !strcmp(dp->parent->name, "central"))
1048 return central_irq_trans_init(dp);
1049 if (!strcmp(dp->name, "virtual-devices") ||
1050 !strcmp(dp->name, "niu"))
1051 return sun4v_vdev_irq_trans_init(dp);
1054 static int is_root_node(const struct device_node *dp)
1056 if (!dp)
1057 return 0;
1059 return (dp->parent == NULL);
1062 /* The following routines deal with the black magic of fully naming a
1063 * node.
1065 * Certain well known named nodes are just the simple name string.
1067 * Actual devices have an address specifier appended to the base name
1068 * string, like this "foo@addr". The "addr" can be in any number of
1069 * formats, and the platform plus the type of the node determine the
1070 * format and how it is constructed.
1072 * For children of the ROOT node, the naming convention is fixed and
1073 * determined by whether this is a sun4u or sun4v system.
1075 * For children of other nodes, it is bus type specific. So
1076 * we walk up the tree until we discover a "device_type" property
1077 * we recognize and we go from there.
1079 * As an example, the boot device on my workstation has a full path:
1081 * /pci@1e,600000/ide@d/disk@0,0:c
1083 static void __init sun4v_path_component(struct device_node *dp, char *tmp_buf)
1085 struct linux_prom64_registers *regs;
1086 struct property *rprop;
1087 u32 high_bits, low_bits, type;
1089 rprop = of_find_property(dp, "reg", NULL);
1090 if (!rprop)
1091 return;
1093 regs = rprop->value;
1094 if (!is_root_node(dp->parent)) {
1095 sprintf(tmp_buf, "%s@%x,%x",
1096 dp->name,
1097 (unsigned int) (regs->phys_addr >> 32UL),
1098 (unsigned int) (regs->phys_addr & 0xffffffffUL));
1099 return;
1102 type = regs->phys_addr >> 60UL;
1103 high_bits = (regs->phys_addr >> 32UL) & 0x0fffffffUL;
1104 low_bits = (regs->phys_addr & 0xffffffffUL);
1106 if (type == 0 || type == 8) {
1107 const char *prefix = (type == 0) ? "m" : "i";
1109 if (low_bits)
1110 sprintf(tmp_buf, "%s@%s%x,%x",
1111 dp->name, prefix,
1112 high_bits, low_bits);
1113 else
1114 sprintf(tmp_buf, "%s@%s%x",
1115 dp->name,
1116 prefix,
1117 high_bits);
1118 } else if (type == 12) {
1119 sprintf(tmp_buf, "%s@%x",
1120 dp->name, high_bits);
1124 static void __init sun4u_path_component(struct device_node *dp, char *tmp_buf)
1126 struct linux_prom64_registers *regs;
1127 struct property *prop;
1129 prop = of_find_property(dp, "reg", NULL);
1130 if (!prop)
1131 return;
1133 regs = prop->value;
1134 if (!is_root_node(dp->parent)) {
1135 sprintf(tmp_buf, "%s@%x,%x",
1136 dp->name,
1137 (unsigned int) (regs->phys_addr >> 32UL),
1138 (unsigned int) (regs->phys_addr & 0xffffffffUL));
1139 return;
1142 prop = of_find_property(dp, "upa-portid", NULL);
1143 if (!prop)
1144 prop = of_find_property(dp, "portid", NULL);
1145 if (prop) {
1146 unsigned long mask = 0xffffffffUL;
1148 if (tlb_type >= cheetah)
1149 mask = 0x7fffff;
1151 sprintf(tmp_buf, "%s@%x,%x",
1152 dp->name,
1153 *(u32 *)prop->value,
1154 (unsigned int) (regs->phys_addr & mask));
1158 /* "name@slot,offset" */
1159 static void __init sbus_path_component(struct device_node *dp, char *tmp_buf)
1161 struct linux_prom_registers *regs;
1162 struct property *prop;
1164 prop = of_find_property(dp, "reg", NULL);
1165 if (!prop)
1166 return;
1168 regs = prop->value;
1169 sprintf(tmp_buf, "%s@%x,%x",
1170 dp->name,
1171 regs->which_io,
1172 regs->phys_addr);
1175 /* "name@devnum[,func]" */
1176 static void __init pci_path_component(struct device_node *dp, char *tmp_buf)
1178 struct linux_prom_pci_registers *regs;
1179 struct property *prop;
1180 unsigned int devfn;
1182 prop = of_find_property(dp, "reg", NULL);
1183 if (!prop)
1184 return;
1186 regs = prop->value;
1187 devfn = (regs->phys_hi >> 8) & 0xff;
1188 if (devfn & 0x07) {
1189 sprintf(tmp_buf, "%s@%x,%x",
1190 dp->name,
1191 devfn >> 3,
1192 devfn & 0x07);
1193 } else {
1194 sprintf(tmp_buf, "%s@%x",
1195 dp->name,
1196 devfn >> 3);
1200 /* "name@UPA_PORTID,offset" */
1201 static void __init upa_path_component(struct device_node *dp, char *tmp_buf)
1203 struct linux_prom64_registers *regs;
1204 struct property *prop;
1206 prop = of_find_property(dp, "reg", NULL);
1207 if (!prop)
1208 return;
1210 regs = prop->value;
1212 prop = of_find_property(dp, "upa-portid", NULL);
1213 if (!prop)
1214 return;
1216 sprintf(tmp_buf, "%s@%x,%x",
1217 dp->name,
1218 *(u32 *) prop->value,
1219 (unsigned int) (regs->phys_addr & 0xffffffffUL));
1222 /* "name@reg" */
1223 static void __init vdev_path_component(struct device_node *dp, char *tmp_buf)
1225 struct property *prop;
1226 u32 *regs;
1228 prop = of_find_property(dp, "reg", NULL);
1229 if (!prop)
1230 return;
1232 regs = prop->value;
1234 sprintf(tmp_buf, "%s@%x", dp->name, *regs);
1237 /* "name@addrhi,addrlo" */
1238 static void __init ebus_path_component(struct device_node *dp, char *tmp_buf)
1240 struct linux_prom64_registers *regs;
1241 struct property *prop;
1243 prop = of_find_property(dp, "reg", NULL);
1244 if (!prop)
1245 return;
1247 regs = prop->value;
1249 sprintf(tmp_buf, "%s@%x,%x",
1250 dp->name,
1251 (unsigned int) (regs->phys_addr >> 32UL),
1252 (unsigned int) (regs->phys_addr & 0xffffffffUL));
1255 /* "name@bus,addr" */
1256 static void __init i2c_path_component(struct device_node *dp, char *tmp_buf)
1258 struct property *prop;
1259 u32 *regs;
1261 prop = of_find_property(dp, "reg", NULL);
1262 if (!prop)
1263 return;
1265 regs = prop->value;
1267 /* This actually isn't right... should look at the #address-cells
1268 * property of the i2c bus node etc. etc.
1270 sprintf(tmp_buf, "%s@%x,%x",
1271 dp->name, regs[0], regs[1]);
1274 /* "name@reg0[,reg1]" */
1275 static void __init usb_path_component(struct device_node *dp, char *tmp_buf)
1277 struct property *prop;
1278 u32 *regs;
1280 prop = of_find_property(dp, "reg", NULL);
1281 if (!prop)
1282 return;
1284 regs = prop->value;
1286 if (prop->length == sizeof(u32) || regs[1] == 1) {
1287 sprintf(tmp_buf, "%s@%x",
1288 dp->name, regs[0]);
1289 } else {
1290 sprintf(tmp_buf, "%s@%x,%x",
1291 dp->name, regs[0], regs[1]);
1295 /* "name@reg0reg1[,reg2reg3]" */
1296 static void __init ieee1394_path_component(struct device_node *dp, char *tmp_buf)
1298 struct property *prop;
1299 u32 *regs;
1301 prop = of_find_property(dp, "reg", NULL);
1302 if (!prop)
1303 return;
1305 regs = prop->value;
1307 if (regs[2] || regs[3]) {
1308 sprintf(tmp_buf, "%s@%08x%08x,%04x%08x",
1309 dp->name, regs[0], regs[1], regs[2], regs[3]);
1310 } else {
1311 sprintf(tmp_buf, "%s@%08x%08x",
1312 dp->name, regs[0], regs[1]);
1316 static void __init __build_path_component(struct device_node *dp, char *tmp_buf)
1318 struct device_node *parent = dp->parent;
1320 if (parent != NULL) {
1321 if (!strcmp(parent->type, "pci") ||
1322 !strcmp(parent->type, "pciex"))
1323 return pci_path_component(dp, tmp_buf);
1324 if (!strcmp(parent->type, "sbus"))
1325 return sbus_path_component(dp, tmp_buf);
1326 if (!strcmp(parent->type, "upa"))
1327 return upa_path_component(dp, tmp_buf);
1328 if (!strcmp(parent->type, "ebus"))
1329 return ebus_path_component(dp, tmp_buf);
1330 if (!strcmp(parent->name, "usb") ||
1331 !strcmp(parent->name, "hub"))
1332 return usb_path_component(dp, tmp_buf);
1333 if (!strcmp(parent->type, "i2c"))
1334 return i2c_path_component(dp, tmp_buf);
1335 if (!strcmp(parent->type, "firewire"))
1336 return ieee1394_path_component(dp, tmp_buf);
1337 if (!strcmp(parent->type, "virtual-devices"))
1338 return vdev_path_component(dp, tmp_buf);
1340 /* "isa" is handled with platform naming */
1343 /* Use platform naming convention. */
1344 if (tlb_type == hypervisor)
1345 return sun4v_path_component(dp, tmp_buf);
1346 else
1347 return sun4u_path_component(dp, tmp_buf);
1350 static char * __init build_path_component(struct device_node *dp)
1352 char tmp_buf[64], *n;
1354 tmp_buf[0] = '\0';
1355 __build_path_component(dp, tmp_buf);
1356 if (tmp_buf[0] == '\0')
1357 strcpy(tmp_buf, dp->name);
1359 n = prom_early_alloc(strlen(tmp_buf) + 1);
1360 strcpy(n, tmp_buf);
1362 return n;
1365 static char * __init build_full_name(struct device_node *dp)
1367 int len, ourlen, plen;
1368 char *n;
1370 plen = strlen(dp->parent->full_name);
1371 ourlen = strlen(dp->path_component_name);
1372 len = ourlen + plen + 2;
1374 n = prom_early_alloc(len);
1375 strcpy(n, dp->parent->full_name);
1376 if (!is_root_node(dp->parent)) {
1377 strcpy(n + plen, "/");
1378 plen++;
1380 strcpy(n + plen, dp->path_component_name);
1382 return n;
1385 static unsigned int unique_id;
1387 static struct property * __init build_one_prop(phandle node, char *prev, char *special_name, void *special_val, int special_len)
1389 static struct property *tmp = NULL;
1390 struct property *p;
1392 if (tmp) {
1393 p = tmp;
1394 memset(p, 0, sizeof(*p) + 32);
1395 tmp = NULL;
1396 } else {
1397 p = prom_early_alloc(sizeof(struct property) + 32);
1398 p->unique_id = unique_id++;
1401 p->name = (char *) (p + 1);
1402 if (special_name) {
1403 strcpy(p->name, special_name);
1404 p->length = special_len;
1405 p->value = prom_early_alloc(special_len);
1406 memcpy(p->value, special_val, special_len);
1407 } else {
1408 if (prev == NULL) {
1409 prom_firstprop(node, p->name);
1410 } else {
1411 prom_nextprop(node, prev, p->name);
1413 if (strlen(p->name) == 0) {
1414 tmp = p;
1415 return NULL;
1417 p->length = prom_getproplen(node, p->name);
1418 if (p->length <= 0) {
1419 p->length = 0;
1420 } else {
1421 p->value = prom_early_alloc(p->length + 1);
1422 prom_getproperty(node, p->name, p->value, p->length);
1423 ((unsigned char *)p->value)[p->length] = '\0';
1426 return p;
1429 static struct property * __init build_prop_list(phandle node)
1431 struct property *head, *tail;
1433 head = tail = build_one_prop(node, NULL,
1434 ".node", &node, sizeof(node));
1436 tail->next = build_one_prop(node, NULL, NULL, NULL, 0);
1437 tail = tail->next;
1438 while(tail) {
1439 tail->next = build_one_prop(node, tail->name,
1440 NULL, NULL, 0);
1441 tail = tail->next;
1444 return head;
1447 static char * __init get_one_property(phandle node, const char *name)
1449 char *buf = "<NULL>";
1450 int len;
1452 len = prom_getproplen(node, name);
1453 if (len > 0) {
1454 buf = prom_early_alloc(len);
1455 prom_getproperty(node, name, buf, len);
1458 return buf;
1461 static struct device_node * __init create_node(phandle node, struct device_node *parent)
1463 struct device_node *dp;
1465 if (!node)
1466 return NULL;
1468 dp = prom_early_alloc(sizeof(*dp));
1469 dp->unique_id = unique_id++;
1470 dp->parent = parent;
1472 kref_init(&dp->kref);
1474 dp->name = get_one_property(node, "name");
1475 dp->type = get_one_property(node, "device_type");
1476 dp->node = node;
1478 dp->properties = build_prop_list(node);
1480 irq_trans_init(dp);
1482 return dp;
1485 static struct device_node * __init build_tree(struct device_node *parent, phandle node, struct device_node ***nextp)
1487 struct device_node *ret = NULL, *prev_sibling = NULL;
1488 struct device_node *dp;
1490 while (1) {
1491 dp = create_node(node, parent);
1492 if (!dp)
1493 break;
1495 if (prev_sibling)
1496 prev_sibling->sibling = dp;
1498 if (!ret)
1499 ret = dp;
1500 prev_sibling = dp;
1502 *(*nextp) = dp;
1503 *nextp = &dp->allnext;
1505 dp->path_component_name = build_path_component(dp);
1506 dp->full_name = build_full_name(dp);
1508 dp->child = build_tree(dp, prom_getchild(node), nextp);
1510 node = prom_getsibling(node);
1513 return ret;
1516 static const char *get_mid_prop(void)
1518 return (tlb_type == spitfire ? "upa-portid" : "portid");
1521 struct device_node *of_find_node_by_cpuid(int cpuid)
1523 struct device_node *dp;
1524 const char *mid_prop = get_mid_prop();
1526 for_each_node_by_type(dp, "cpu") {
1527 int id = of_getintprop_default(dp, mid_prop, -1);
1528 const char *this_mid_prop = mid_prop;
1530 if (id < 0) {
1531 this_mid_prop = "cpuid";
1532 id = of_getintprop_default(dp, this_mid_prop, -1);
1535 if (id < 0) {
1536 prom_printf("OF: Serious problem, cpu lacks "
1537 "%s property", this_mid_prop);
1538 prom_halt();
1540 if (cpuid == id)
1541 return dp;
1543 return NULL;
1546 static void __init of_fill_in_cpu_data(void)
1548 struct device_node *dp;
1549 const char *mid_prop = get_mid_prop();
1551 ncpus_probed = 0;
1552 for_each_node_by_type(dp, "cpu") {
1553 int cpuid = of_getintprop_default(dp, mid_prop, -1);
1554 const char *this_mid_prop = mid_prop;
1555 struct device_node *portid_parent;
1556 int portid = -1;
1558 portid_parent = NULL;
1559 if (cpuid < 0) {
1560 this_mid_prop = "cpuid";
1561 cpuid = of_getintprop_default(dp, this_mid_prop, -1);
1562 if (cpuid >= 0) {
1563 int limit = 2;
1565 portid_parent = dp;
1566 while (limit--) {
1567 portid_parent = portid_parent->parent;
1568 if (!portid_parent)
1569 break;
1570 portid = of_getintprop_default(portid_parent,
1571 "portid", -1);
1572 if (portid >= 0)
1573 break;
1578 if (cpuid < 0) {
1579 prom_printf("OF: Serious problem, cpu lacks "
1580 "%s property", this_mid_prop);
1581 prom_halt();
1584 ncpus_probed++;
1586 #ifdef CONFIG_SMP
1587 if (cpuid >= NR_CPUS) {
1588 printk(KERN_WARNING "Ignoring CPU %d which is "
1589 ">= NR_CPUS (%d)\n",
1590 cpuid, NR_CPUS);
1591 continue;
1593 #else
1594 /* On uniprocessor we only want the values for the
1595 * real physical cpu the kernel booted onto, however
1596 * cpu_data() only has one entry at index 0.
1598 if (cpuid != real_hard_smp_processor_id())
1599 continue;
1600 cpuid = 0;
1601 #endif
1603 cpu_data(cpuid).clock_tick =
1604 of_getintprop_default(dp, "clock-frequency", 0);
1606 if (portid_parent) {
1607 cpu_data(cpuid).dcache_size =
1608 of_getintprop_default(dp, "l1-dcache-size",
1609 16 * 1024);
1610 cpu_data(cpuid).dcache_line_size =
1611 of_getintprop_default(dp, "l1-dcache-line-size",
1612 32);
1613 cpu_data(cpuid).icache_size =
1614 of_getintprop_default(dp, "l1-icache-size",
1615 8 * 1024);
1616 cpu_data(cpuid).icache_line_size =
1617 of_getintprop_default(dp, "l1-icache-line-size",
1618 32);
1619 cpu_data(cpuid).ecache_size =
1620 of_getintprop_default(dp, "l2-cache-size", 0);
1621 cpu_data(cpuid).ecache_line_size =
1622 of_getintprop_default(dp, "l2-cache-line-size", 0);
1623 if (!cpu_data(cpuid).ecache_size ||
1624 !cpu_data(cpuid).ecache_line_size) {
1625 cpu_data(cpuid).ecache_size =
1626 of_getintprop_default(portid_parent,
1627 "l2-cache-size",
1628 (4 * 1024 * 1024));
1629 cpu_data(cpuid).ecache_line_size =
1630 of_getintprop_default(portid_parent,
1631 "l2-cache-line-size", 64);
1634 cpu_data(cpuid).core_id = portid + 1;
1635 cpu_data(cpuid).proc_id = portid;
1636 #ifdef CONFIG_SMP
1637 sparc64_multi_core = 1;
1638 #endif
1639 } else {
1640 cpu_data(cpuid).dcache_size =
1641 of_getintprop_default(dp, "dcache-size", 16 * 1024);
1642 cpu_data(cpuid).dcache_line_size =
1643 of_getintprop_default(dp, "dcache-line-size", 32);
1645 cpu_data(cpuid).icache_size =
1646 of_getintprop_default(dp, "icache-size", 16 * 1024);
1647 cpu_data(cpuid).icache_line_size =
1648 of_getintprop_default(dp, "icache-line-size", 32);
1650 cpu_data(cpuid).ecache_size =
1651 of_getintprop_default(dp, "ecache-size",
1652 (4 * 1024 * 1024));
1653 cpu_data(cpuid).ecache_line_size =
1654 of_getintprop_default(dp, "ecache-line-size", 64);
1656 cpu_data(cpuid).core_id = 0;
1657 cpu_data(cpuid).proc_id = -1;
1660 #ifdef CONFIG_SMP
1661 cpu_set(cpuid, cpu_present_map);
1662 cpu_set(cpuid, cpu_possible_map);
1663 #endif
1666 smp_fill_in_sib_core_maps();
1669 struct device_node *of_console_device;
1670 EXPORT_SYMBOL(of_console_device);
1672 char *of_console_path;
1673 EXPORT_SYMBOL(of_console_path);
1675 char *of_console_options;
1676 EXPORT_SYMBOL(of_console_options);
1678 static void __init of_console_init(void)
1680 char *msg = "OF stdout device is: %s\n";
1681 struct device_node *dp;
1682 const char *type;
1683 phandle node;
1685 of_console_path = prom_early_alloc(256);
1686 if (prom_ihandle2path(prom_stdout, of_console_path, 256) < 0) {
1687 prom_printf("Cannot obtain path of stdout.\n");
1688 prom_halt();
1690 of_console_options = strrchr(of_console_path, ':');
1691 if (of_console_options) {
1692 of_console_options++;
1693 if (*of_console_options == '\0')
1694 of_console_options = NULL;
1697 node = prom_inst2pkg(prom_stdout);
1698 if (!node) {
1699 prom_printf("Cannot resolve stdout node from "
1700 "instance %08x.\n", prom_stdout);
1701 prom_halt();
1704 dp = of_find_node_by_phandle(node);
1705 type = of_get_property(dp, "device_type", NULL);
1706 if (!type) {
1707 prom_printf("Console stdout lacks device_type property.\n");
1708 prom_halt();
1711 if (strcmp(type, "display") && strcmp(type, "serial")) {
1712 prom_printf("Console device_type is neither display "
1713 "nor serial.\n");
1714 prom_halt();
1717 of_console_device = dp;
1719 printk(msg, of_console_path);
1722 void __init prom_build_devicetree(void)
1724 struct device_node **nextp;
1726 allnodes = create_node(prom_root_node, NULL);
1727 allnodes->path_component_name = "";
1728 allnodes->full_name = "/";
1730 nextp = &allnodes->allnext;
1731 allnodes->child = build_tree(allnodes,
1732 prom_getchild(allnodes->node),
1733 &nextp);
1734 of_console_init();
1736 printk("PROM: Built device tree with %u bytes of memory.\n",
1737 prom_early_allocated);
1739 if (tlb_type != hypervisor)
1740 of_fill_in_cpu_data();