2 * Procedures for creating, accessing and interpreting the device tree.
4 * Paul Mackerras August 1996.
5 * Copyright (C) 1996-2005 Paul Mackerras.
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>
22 #include <linux/module.h>
23 #include <linux/lmb.h>
24 #include <linux/of_device.h>
27 #include <asm/oplib.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)
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;
54 prop = of_find_property(np, name, &len);
55 if (!prop || len != 4)
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;
68 new_val = kmalloc(len, GFP_KERNEL);
72 memcpy(new_val, val, len);
76 write_lock(&devtree_lock);
77 prevp = &dp->properties;
79 struct property *prop = *prevp;
81 if (!strcasecmp(prop->name, name)) {
82 void *old_val = prop->value;
85 ret = prom_setprop(dp->node, name, val, len);
88 prop->value = new_val;
91 if (OF_IS_DYNAMIC(prop))
94 OF_MARK_DYNAMIC(prop);
100 prevp = &(*prevp)->next;
102 write_unlock(&devtree_lock);
104 /* XXX Upate procfs if necessary... */
108 EXPORT_SYMBOL(of_set_property);
110 int of_find_in_proplist(const char *list, const char *match, int len)
115 if (!strcmp(list, match))
117 l = strlen(list) + 1;
123 EXPORT_SYMBOL(of_find_in_proplist);
125 static unsigned int prom_early_allocated __initdata;
127 static void * __init prom_early_alloc(unsigned long size)
129 unsigned long paddr = lmb_alloc(size, SMP_CACHE_BYTES);
133 prom_printf("prom_early_alloc(%lu) failed\n");
138 memset(ret, 0, size);
139 prom_early_allocated += size;
145 /* PSYCHO interrupt mapping support. */
146 #define PSYCHO_IMAP_A_SLOT0 0x0c00UL
147 #define PSYCHO_IMAP_B_SLOT0 0x0c20UL
148 static unsigned long psycho_pcislot_imap_offset(unsigned long ino)
150 unsigned int bus = (ino & 0x10) >> 4;
151 unsigned int slot = (ino & 0x0c) >> 2;
154 return PSYCHO_IMAP_A_SLOT0 + (slot * 8);
156 return PSYCHO_IMAP_B_SLOT0 + (slot * 8);
159 #define PSYCHO_OBIO_IMAP_BASE 0x1000UL
161 #define PSYCHO_ONBOARD_IRQ_BASE 0x20
162 #define psycho_onboard_imap_offset(__ino) \
163 (PSYCHO_OBIO_IMAP_BASE + (((__ino) & 0x1f) << 3))
165 #define PSYCHO_ICLR_A_SLOT0 0x1400UL
166 #define PSYCHO_ICLR_SCSI 0x1800UL
168 #define psycho_iclr_offset(ino) \
169 ((ino & 0x20) ? (PSYCHO_ICLR_SCSI + (((ino) & 0x1f) << 3)) : \
170 (PSYCHO_ICLR_A_SLOT0 + (((ino) & 0x1f)<<3)))
172 static unsigned int psycho_irq_build(struct device_node *dp,
176 unsigned long controller_regs = (unsigned long) _data;
177 unsigned long imap, iclr;
178 unsigned long imap_off, iclr_off;
182 if (ino < PSYCHO_ONBOARD_IRQ_BASE) {
184 imap_off = psycho_pcislot_imap_offset(ino);
187 imap_off = psycho_onboard_imap_offset(ino);
190 /* Now build the IRQ bucket. */
191 imap = controller_regs + imap_off;
193 iclr_off = psycho_iclr_offset(ino);
194 iclr = controller_regs + iclr_off;
196 if ((ino & 0x20) == 0)
197 inofixup = ino & 0x03;
199 return build_irq(inofixup, iclr, imap);
202 static void __init psycho_irq_trans_init(struct device_node *dp)
204 const struct linux_prom64_registers *regs;
206 dp->irq_trans = prom_early_alloc(sizeof(struct of_irq_controller));
207 dp->irq_trans->irq_build = psycho_irq_build;
209 regs = of_get_property(dp, "reg", NULL);
210 dp->irq_trans->data = (void *) regs[2].phys_addr;
213 #define sabre_read(__reg) \
215 __asm__ __volatile__("ldxa [%1] %2, %0" \
217 : "r" (__reg), "i" (ASI_PHYS_BYPASS_EC_E) \
222 struct sabre_irq_data {
223 unsigned long controller_regs;
224 unsigned int pci_first_busno;
226 #define SABRE_CONFIGSPACE 0x001000000UL
227 #define SABRE_WRSYNC 0x1c20UL
229 #define SABRE_CONFIG_BASE(CONFIG_SPACE) \
230 (CONFIG_SPACE | (1UL << 24))
231 #define SABRE_CONFIG_ENCODE(BUS, DEVFN, REG) \
232 (((unsigned long)(BUS) << 16) | \
233 ((unsigned long)(DEVFN) << 8) | \
234 ((unsigned long)(REG)))
236 /* When a device lives behind a bridge deeper in the PCI bus topology
237 * than APB, a special sequence must run to make sure all pending DMA
238 * transfers at the time of IRQ delivery are visible in the coherency
239 * domain by the cpu. This sequence is to perform a read on the far
240 * side of the non-APB bridge, then perform a read of Sabre's DMA
241 * write-sync register.
243 static void sabre_wsync_handler(unsigned int ino, void *_arg1, void *_arg2)
245 unsigned int phys_hi = (unsigned int) (unsigned long) _arg1;
246 struct sabre_irq_data *irq_data = _arg2;
247 unsigned long controller_regs = irq_data->controller_regs;
248 unsigned long sync_reg = controller_regs + SABRE_WRSYNC;
249 unsigned long config_space = controller_regs + SABRE_CONFIGSPACE;
250 unsigned int bus, devfn;
253 config_space = SABRE_CONFIG_BASE(config_space);
255 bus = (phys_hi >> 16) & 0xff;
256 devfn = (phys_hi >> 8) & 0xff;
258 config_space |= SABRE_CONFIG_ENCODE(bus, devfn, 0x00);
260 __asm__ __volatile__("membar #Sync\n\t"
261 "lduha [%1] %2, %0\n\t"
264 : "r" ((u16 *) config_space),
265 "i" (ASI_PHYS_BYPASS_EC_E_L)
268 sabre_read(sync_reg);
271 #define SABRE_IMAP_A_SLOT0 0x0c00UL
272 #define SABRE_IMAP_B_SLOT0 0x0c20UL
273 #define SABRE_ICLR_A_SLOT0 0x1400UL
274 #define SABRE_ICLR_B_SLOT0 0x1480UL
275 #define SABRE_ICLR_SCSI 0x1800UL
276 #define SABRE_ICLR_ETH 0x1808UL
277 #define SABRE_ICLR_BPP 0x1810UL
278 #define SABRE_ICLR_AU_REC 0x1818UL
279 #define SABRE_ICLR_AU_PLAY 0x1820UL
280 #define SABRE_ICLR_PFAIL 0x1828UL
281 #define SABRE_ICLR_KMS 0x1830UL
282 #define SABRE_ICLR_FLPY 0x1838UL
283 #define SABRE_ICLR_SHW 0x1840UL
284 #define SABRE_ICLR_KBD 0x1848UL
285 #define SABRE_ICLR_MS 0x1850UL
286 #define SABRE_ICLR_SER 0x1858UL
287 #define SABRE_ICLR_UE 0x1870UL
288 #define SABRE_ICLR_CE 0x1878UL
289 #define SABRE_ICLR_PCIERR 0x1880UL
291 static unsigned long sabre_pcislot_imap_offset(unsigned long ino)
293 unsigned int bus = (ino & 0x10) >> 4;
294 unsigned int slot = (ino & 0x0c) >> 2;
297 return SABRE_IMAP_A_SLOT0 + (slot * 8);
299 return SABRE_IMAP_B_SLOT0 + (slot * 8);
302 #define SABRE_OBIO_IMAP_BASE 0x1000UL
303 #define SABRE_ONBOARD_IRQ_BASE 0x20
304 #define sabre_onboard_imap_offset(__ino) \
305 (SABRE_OBIO_IMAP_BASE + (((__ino) & 0x1f) << 3))
307 #define sabre_iclr_offset(ino) \
308 ((ino & 0x20) ? (SABRE_ICLR_SCSI + (((ino) & 0x1f) << 3)) : \
309 (SABRE_ICLR_A_SLOT0 + (((ino) & 0x1f)<<3)))
311 static int sabre_device_needs_wsync(struct device_node *dp)
313 struct device_node *parent = dp->parent;
314 const char *parent_model, *parent_compat;
316 /* This traversal up towards the root is meant to
319 * 1) non-PCI bus sitting under PCI, such as 'ebus'
320 * 2) the PCI controller interrupts themselves, which
321 * will use the sabre_irq_build but do not need
322 * the DMA synchronization handling
325 if (!strcmp(parent->type, "pci"))
327 parent = parent->parent;
333 parent_model = of_get_property(parent,
336 (!strcmp(parent_model, "SUNW,sabre") ||
337 !strcmp(parent_model, "SUNW,simba")))
340 parent_compat = of_get_property(parent,
343 (!strcmp(parent_compat, "pci108e,a000") ||
344 !strcmp(parent_compat, "pci108e,a001")))
350 static unsigned int sabre_irq_build(struct device_node *dp,
354 struct sabre_irq_data *irq_data = _data;
355 unsigned long controller_regs = irq_data->controller_regs;
356 const struct linux_prom_pci_registers *regs;
357 unsigned long imap, iclr;
358 unsigned long imap_off, iclr_off;
363 if (ino < SABRE_ONBOARD_IRQ_BASE) {
365 imap_off = sabre_pcislot_imap_offset(ino);
368 imap_off = sabre_onboard_imap_offset(ino);
371 /* Now build the IRQ bucket. */
372 imap = controller_regs + imap_off;
374 iclr_off = sabre_iclr_offset(ino);
375 iclr = controller_regs + iclr_off;
377 if ((ino & 0x20) == 0)
378 inofixup = ino & 0x03;
380 virt_irq = build_irq(inofixup, iclr, imap);
382 /* If the parent device is a PCI<->PCI bridge other than
383 * APB, we have to install a pre-handler to ensure that
384 * all pending DMA is drained before the interrupt handler
387 regs = of_get_property(dp, "reg", NULL);
388 if (regs && sabre_device_needs_wsync(dp)) {
389 irq_install_pre_handler(virt_irq,
391 (void *) (long) regs->phys_hi,
398 static void __init sabre_irq_trans_init(struct device_node *dp)
400 const struct linux_prom64_registers *regs;
401 struct sabre_irq_data *irq_data;
404 dp->irq_trans = prom_early_alloc(sizeof(struct of_irq_controller));
405 dp->irq_trans->irq_build = sabre_irq_build;
407 irq_data = prom_early_alloc(sizeof(struct sabre_irq_data));
409 regs = of_get_property(dp, "reg", NULL);
410 irq_data->controller_regs = regs[0].phys_addr;
412 busrange = of_get_property(dp, "bus-range", NULL);
413 irq_data->pci_first_busno = busrange[0];
415 dp->irq_trans->data = irq_data;
418 /* SCHIZO interrupt mapping support. Unlike Psycho, for this controller the
419 * imap/iclr registers are per-PBM.
421 #define SCHIZO_IMAP_BASE 0x1000UL
422 #define SCHIZO_ICLR_BASE 0x1400UL
424 static unsigned long schizo_imap_offset(unsigned long ino)
426 return SCHIZO_IMAP_BASE + (ino * 8UL);
429 static unsigned long schizo_iclr_offset(unsigned long ino)
431 return SCHIZO_ICLR_BASE + (ino * 8UL);
434 static unsigned long schizo_ino_to_iclr(unsigned long pbm_regs,
438 return pbm_regs + schizo_iclr_offset(ino);
441 static unsigned long schizo_ino_to_imap(unsigned long pbm_regs,
444 return pbm_regs + schizo_imap_offset(ino);
447 #define schizo_read(__reg) \
449 __asm__ __volatile__("ldxa [%1] %2, %0" \
451 : "r" (__reg), "i" (ASI_PHYS_BYPASS_EC_E) \
455 #define schizo_write(__reg, __val) \
456 __asm__ __volatile__("stxa %0, [%1] %2" \
458 : "r" (__val), "r" (__reg), \
459 "i" (ASI_PHYS_BYPASS_EC_E) \
462 static void tomatillo_wsync_handler(unsigned int ino, void *_arg1, void *_arg2)
464 unsigned long sync_reg = (unsigned long) _arg2;
465 u64 mask = 1UL << (ino & IMAP_INO);
469 schizo_write(sync_reg, mask);
474 val = schizo_read(sync_reg);
479 printk("tomatillo_wsync_handler: DMA won't sync [%lx:%lx]\n",
484 static unsigned char cacheline[64]
485 __attribute__ ((aligned (64)));
487 __asm__ __volatile__("rd %%fprs, %0\n\t"
489 "wr %1, 0x0, %%fprs\n\t"
490 "stda %%f0, [%5] %6\n\t"
491 "wr %0, 0x0, %%fprs\n\t"
493 : "=&r" (mask), "=&r" (val)
494 : "0" (mask), "1" (val),
495 "i" (FPRS_FEF), "r" (&cacheline[0]),
496 "i" (ASI_BLK_COMMIT_P));
500 struct schizo_irq_data {
501 unsigned long pbm_regs;
502 unsigned long sync_reg;
507 static unsigned int schizo_irq_build(struct device_node *dp,
511 struct schizo_irq_data *irq_data = _data;
512 unsigned long pbm_regs = irq_data->pbm_regs;
513 unsigned long imap, iclr;
520 /* Now build the IRQ bucket. */
521 imap = schizo_ino_to_imap(pbm_regs, ino);
522 iclr = schizo_ino_to_iclr(pbm_regs, ino);
524 /* On Schizo, no inofixup occurs. This is because each
525 * INO has it's own IMAP register. On Psycho and Sabre
526 * there is only one IMAP register for each PCI slot even
527 * though four different INOs can be generated by each
530 * But, for JBUS variants (essentially, Tomatillo), we have
531 * to fixup the lowest bit of the interrupt group number.
535 is_tomatillo = (irq_data->sync_reg != 0UL);
538 if (irq_data->portid & 1)
539 ign_fixup = (1 << 6);
542 virt_irq = build_irq(ign_fixup, iclr, imap);
545 irq_install_pre_handler(virt_irq,
546 tomatillo_wsync_handler,
547 ((irq_data->chip_version <= 4) ?
548 (void *) 1 : (void *) 0),
549 (void *) irq_data->sync_reg);
555 static void __init __schizo_irq_trans_init(struct device_node *dp,
558 const struct linux_prom64_registers *regs;
559 struct schizo_irq_data *irq_data;
561 dp->irq_trans = prom_early_alloc(sizeof(struct of_irq_controller));
562 dp->irq_trans->irq_build = schizo_irq_build;
564 irq_data = prom_early_alloc(sizeof(struct schizo_irq_data));
566 regs = of_get_property(dp, "reg", NULL);
567 dp->irq_trans->data = irq_data;
569 irq_data->pbm_regs = regs[0].phys_addr;
571 irq_data->sync_reg = regs[3].phys_addr + 0x1a18UL;
573 irq_data->sync_reg = 0UL;
574 irq_data->portid = of_getintprop_default(dp, "portid", 0);
575 irq_data->chip_version = of_getintprop_default(dp, "version#", 0);
578 static void __init schizo_irq_trans_init(struct device_node *dp)
580 __schizo_irq_trans_init(dp, 0);
583 static void __init tomatillo_irq_trans_init(struct device_node *dp)
585 __schizo_irq_trans_init(dp, 1);
588 static unsigned int pci_sun4v_irq_build(struct device_node *dp,
592 u32 devhandle = (u32) (unsigned long) _data;
594 return sun4v_build_irq(devhandle, devino);
597 static void __init pci_sun4v_irq_trans_init(struct device_node *dp)
599 const struct linux_prom64_registers *regs;
601 dp->irq_trans = prom_early_alloc(sizeof(struct of_irq_controller));
602 dp->irq_trans->irq_build = pci_sun4v_irq_build;
604 regs = of_get_property(dp, "reg", NULL);
605 dp->irq_trans->data = (void *) (unsigned long)
606 ((regs->phys_addr >> 32UL) & 0x0fffffff);
609 struct fire_irq_data {
610 unsigned long pbm_regs;
614 #define FIRE_IMAP_BASE 0x001000
615 #define FIRE_ICLR_BASE 0x001400
617 static unsigned long fire_imap_offset(unsigned long ino)
619 return FIRE_IMAP_BASE + (ino * 8UL);
622 static unsigned long fire_iclr_offset(unsigned long ino)
624 return FIRE_ICLR_BASE + (ino * 8UL);
627 static unsigned long fire_ino_to_iclr(unsigned long pbm_regs,
630 return pbm_regs + fire_iclr_offset(ino);
633 static unsigned long fire_ino_to_imap(unsigned long pbm_regs,
636 return pbm_regs + fire_imap_offset(ino);
639 static unsigned int fire_irq_build(struct device_node *dp,
643 struct fire_irq_data *irq_data = _data;
644 unsigned long pbm_regs = irq_data->pbm_regs;
645 unsigned long imap, iclr;
646 unsigned long int_ctrlr;
650 /* Now build the IRQ bucket. */
651 imap = fire_ino_to_imap(pbm_regs, ino);
652 iclr = fire_ino_to_iclr(pbm_regs, ino);
654 /* Set the interrupt controller number. */
656 upa_writeq(int_ctrlr, imap);
658 /* The interrupt map registers do not have an INO field
659 * like other chips do. They return zero in the INO
660 * field, and the interrupt controller number is controlled
661 * in bits 6 to 9. So in order for build_irq() to get
662 * the INO right we pass it in as part of the fixup
663 * which will get added to the map register zero value
664 * read by build_irq().
666 ino |= (irq_data->portid << 6);
668 return build_irq(ino, iclr, imap);
671 static void __init fire_irq_trans_init(struct device_node *dp)
673 const struct linux_prom64_registers *regs;
674 struct fire_irq_data *irq_data;
676 dp->irq_trans = prom_early_alloc(sizeof(struct of_irq_controller));
677 dp->irq_trans->irq_build = fire_irq_build;
679 irq_data = prom_early_alloc(sizeof(struct fire_irq_data));
681 regs = of_get_property(dp, "reg", NULL);
682 dp->irq_trans->data = irq_data;
684 irq_data->pbm_regs = regs[0].phys_addr;
685 irq_data->portid = of_getintprop_default(dp, "portid", 0);
687 #endif /* CONFIG_PCI */
690 /* INO number to IMAP register offset for SYSIO external IRQ's.
691 * This should conform to both Sunfire/Wildfire server and Fusion
694 #define SYSIO_IMAP_SLOT0 0x2c00UL
695 #define SYSIO_IMAP_SLOT1 0x2c08UL
696 #define SYSIO_IMAP_SLOT2 0x2c10UL
697 #define SYSIO_IMAP_SLOT3 0x2c18UL
698 #define SYSIO_IMAP_SCSI 0x3000UL
699 #define SYSIO_IMAP_ETH 0x3008UL
700 #define SYSIO_IMAP_BPP 0x3010UL
701 #define SYSIO_IMAP_AUDIO 0x3018UL
702 #define SYSIO_IMAP_PFAIL 0x3020UL
703 #define SYSIO_IMAP_KMS 0x3028UL
704 #define SYSIO_IMAP_FLPY 0x3030UL
705 #define SYSIO_IMAP_SHW 0x3038UL
706 #define SYSIO_IMAP_KBD 0x3040UL
707 #define SYSIO_IMAP_MS 0x3048UL
708 #define SYSIO_IMAP_SER 0x3050UL
709 #define SYSIO_IMAP_TIM0 0x3060UL
710 #define SYSIO_IMAP_TIM1 0x3068UL
711 #define SYSIO_IMAP_UE 0x3070UL
712 #define SYSIO_IMAP_CE 0x3078UL
713 #define SYSIO_IMAP_SBERR 0x3080UL
714 #define SYSIO_IMAP_PMGMT 0x3088UL
715 #define SYSIO_IMAP_GFX 0x3090UL
716 #define SYSIO_IMAP_EUPA 0x3098UL
718 #define bogon ((unsigned long) -1)
719 static unsigned long sysio_irq_offsets[] = {
720 /* SBUS Slot 0 --> 3, level 1 --> 7 */
721 SYSIO_IMAP_SLOT0, SYSIO_IMAP_SLOT0, SYSIO_IMAP_SLOT0, SYSIO_IMAP_SLOT0,
722 SYSIO_IMAP_SLOT0, SYSIO_IMAP_SLOT0, SYSIO_IMAP_SLOT0, SYSIO_IMAP_SLOT0,
723 SYSIO_IMAP_SLOT1, SYSIO_IMAP_SLOT1, SYSIO_IMAP_SLOT1, SYSIO_IMAP_SLOT1,
724 SYSIO_IMAP_SLOT1, SYSIO_IMAP_SLOT1, SYSIO_IMAP_SLOT1, SYSIO_IMAP_SLOT1,
725 SYSIO_IMAP_SLOT2, SYSIO_IMAP_SLOT2, SYSIO_IMAP_SLOT2, SYSIO_IMAP_SLOT2,
726 SYSIO_IMAP_SLOT2, SYSIO_IMAP_SLOT2, SYSIO_IMAP_SLOT2, SYSIO_IMAP_SLOT2,
727 SYSIO_IMAP_SLOT3, SYSIO_IMAP_SLOT3, SYSIO_IMAP_SLOT3, SYSIO_IMAP_SLOT3,
728 SYSIO_IMAP_SLOT3, SYSIO_IMAP_SLOT3, SYSIO_IMAP_SLOT3, SYSIO_IMAP_SLOT3,
730 /* Onboard devices (not relevant/used on SunFire). */
761 #define NUM_SYSIO_OFFSETS ARRAY_SIZE(sysio_irq_offsets)
763 /* Convert Interrupt Mapping register pointer to associated
764 * Interrupt Clear register pointer, SYSIO specific version.
766 #define SYSIO_ICLR_UNUSED0 0x3400UL
767 #define SYSIO_ICLR_SLOT0 0x3408UL
768 #define SYSIO_ICLR_SLOT1 0x3448UL
769 #define SYSIO_ICLR_SLOT2 0x3488UL
770 #define SYSIO_ICLR_SLOT3 0x34c8UL
771 static unsigned long sysio_imap_to_iclr(unsigned long imap)
773 unsigned long diff = SYSIO_ICLR_UNUSED0 - SYSIO_IMAP_SLOT0;
777 static unsigned int sbus_of_build_irq(struct device_node *dp,
781 unsigned long reg_base = (unsigned long) _data;
782 const struct linux_prom_registers *regs;
783 unsigned long imap, iclr;
789 regs = of_get_property(dp, "reg", NULL);
791 sbus_slot = regs->which_io;
794 ino += (sbus_slot * 8);
796 imap = sysio_irq_offsets[ino];
797 if (imap == ((unsigned long)-1)) {
798 prom_printf("get_irq_translations: Bad SYSIO INO[%x]\n",
804 /* SYSIO inconsistency. For external SLOTS, we have to select
805 * the right ICLR register based upon the lower SBUS irq level
809 iclr = sysio_imap_to_iclr(imap);
811 sbus_level = ino & 0x7;
815 iclr = reg_base + SYSIO_ICLR_SLOT0;
818 iclr = reg_base + SYSIO_ICLR_SLOT1;
821 iclr = reg_base + SYSIO_ICLR_SLOT2;
825 iclr = reg_base + SYSIO_ICLR_SLOT3;
829 iclr += ((unsigned long)sbus_level - 1UL) * 8UL;
831 return build_irq(sbus_level, iclr, imap);
834 static void __init sbus_irq_trans_init(struct device_node *dp)
836 const struct linux_prom64_registers *regs;
838 dp->irq_trans = prom_early_alloc(sizeof(struct of_irq_controller));
839 dp->irq_trans->irq_build = sbus_of_build_irq;
841 regs = of_get_property(dp, "reg", NULL);
842 dp->irq_trans->data = (void *) (unsigned long) regs->phys_addr;
844 #endif /* CONFIG_SBUS */
847 static unsigned int central_build_irq(struct device_node *dp,
851 struct device_node *central_dp = _data;
852 struct of_device *central_op = of_find_device_by_node(central_dp);
853 struct resource *res;
854 unsigned long imap, iclr;
857 if (!strcmp(dp->name, "eeprom")) {
858 res = ¢ral_op->resource[5];
859 } else if (!strcmp(dp->name, "zs")) {
860 res = ¢ral_op->resource[4];
861 } else if (!strcmp(dp->name, "clock-board")) {
862 res = ¢ral_op->resource[3];
867 imap = res->start + 0x00UL;
868 iclr = res->start + 0x10UL;
870 /* Set the INO state to idle, and disable. */
874 tmp = upa_readl(imap);
876 upa_writel(tmp, imap);
878 return build_irq(0, iclr, imap);
881 static void __init central_irq_trans_init(struct device_node *dp)
883 dp->irq_trans = prom_early_alloc(sizeof(struct of_irq_controller));
884 dp->irq_trans->irq_build = central_build_irq;
886 dp->irq_trans->data = dp;
891 void (*init)(struct device_node *);
895 static struct irq_trans __initdata pci_irq_trans_table[] = {
896 { "SUNW,sabre", sabre_irq_trans_init },
897 { "pci108e,a000", sabre_irq_trans_init },
898 { "pci108e,a001", sabre_irq_trans_init },
899 { "SUNW,psycho", psycho_irq_trans_init },
900 { "pci108e,8000", psycho_irq_trans_init },
901 { "SUNW,schizo", schizo_irq_trans_init },
902 { "pci108e,8001", schizo_irq_trans_init },
903 { "SUNW,schizo+", schizo_irq_trans_init },
904 { "pci108e,8002", schizo_irq_trans_init },
905 { "SUNW,tomatillo", tomatillo_irq_trans_init },
906 { "pci108e,a801", tomatillo_irq_trans_init },
907 { "SUNW,sun4v-pci", pci_sun4v_irq_trans_init },
908 { "pciex108e,80f0", fire_irq_trans_init },
912 static unsigned int sun4v_vdev_irq_build(struct device_node *dp,
916 u32 devhandle = (u32) (unsigned long) _data;
918 return sun4v_build_irq(devhandle, devino);
921 static void __init sun4v_vdev_irq_trans_init(struct device_node *dp)
923 const struct linux_prom64_registers *regs;
925 dp->irq_trans = prom_early_alloc(sizeof(struct of_irq_controller));
926 dp->irq_trans->irq_build = sun4v_vdev_irq_build;
928 regs = of_get_property(dp, "reg", NULL);
929 dp->irq_trans->data = (void *) (unsigned long)
930 ((regs->phys_addr >> 32UL) & 0x0fffffff);
933 static void __init irq_trans_init(struct device_node *dp)
941 model = of_get_property(dp, "model", NULL);
943 model = of_get_property(dp, "compatible", NULL);
945 for (i = 0; i < ARRAY_SIZE(pci_irq_trans_table); i++) {
946 struct irq_trans *t = &pci_irq_trans_table[i];
948 if (!strcmp(model, t->name))
954 if (!strcmp(dp->name, "sbus") ||
955 !strcmp(dp->name, "sbi"))
956 return sbus_irq_trans_init(dp);
958 if (!strcmp(dp->name, "fhc") &&
959 !strcmp(dp->parent->name, "central"))
960 return central_irq_trans_init(dp);
961 if (!strcmp(dp->name, "virtual-devices") ||
962 !strcmp(dp->name, "niu"))
963 return sun4v_vdev_irq_trans_init(dp);
966 static int is_root_node(const struct device_node *dp)
971 return (dp->parent == NULL);
974 /* The following routines deal with the black magic of fully naming a
977 * Certain well known named nodes are just the simple name string.
979 * Actual devices have an address specifier appended to the base name
980 * string, like this "foo@addr". The "addr" can be in any number of
981 * formats, and the platform plus the type of the node determine the
982 * format and how it is constructed.
984 * For children of the ROOT node, the naming convention is fixed and
985 * determined by whether this is a sun4u or sun4v system.
987 * For children of other nodes, it is bus type specific. So
988 * we walk up the tree until we discover a "device_type" property
989 * we recognize and we go from there.
991 * As an example, the boot device on my workstation has a full path:
993 * /pci@1e,600000/ide@d/disk@0,0:c
995 static void __init sun4v_path_component(struct device_node *dp, char *tmp_buf)
997 struct linux_prom64_registers *regs;
998 struct property *rprop;
999 u32 high_bits, low_bits, type;
1001 rprop = of_find_property(dp, "reg", NULL);
1005 regs = rprop->value;
1006 if (!is_root_node(dp->parent)) {
1007 sprintf(tmp_buf, "%s@%x,%x",
1009 (unsigned int) (regs->phys_addr >> 32UL),
1010 (unsigned int) (regs->phys_addr & 0xffffffffUL));
1014 type = regs->phys_addr >> 60UL;
1015 high_bits = (regs->phys_addr >> 32UL) & 0x0fffffffUL;
1016 low_bits = (regs->phys_addr & 0xffffffffUL);
1018 if (type == 0 || type == 8) {
1019 const char *prefix = (type == 0) ? "m" : "i";
1022 sprintf(tmp_buf, "%s@%s%x,%x",
1024 high_bits, low_bits);
1026 sprintf(tmp_buf, "%s@%s%x",
1030 } else if (type == 12) {
1031 sprintf(tmp_buf, "%s@%x",
1032 dp->name, high_bits);
1036 static void __init sun4u_path_component(struct device_node *dp, char *tmp_buf)
1038 struct linux_prom64_registers *regs;
1039 struct property *prop;
1041 prop = of_find_property(dp, "reg", NULL);
1046 if (!is_root_node(dp->parent)) {
1047 sprintf(tmp_buf, "%s@%x,%x",
1049 (unsigned int) (regs->phys_addr >> 32UL),
1050 (unsigned int) (regs->phys_addr & 0xffffffffUL));
1054 prop = of_find_property(dp, "upa-portid", NULL);
1056 prop = of_find_property(dp, "portid", NULL);
1058 unsigned long mask = 0xffffffffUL;
1060 if (tlb_type >= cheetah)
1063 sprintf(tmp_buf, "%s@%x,%x",
1065 *(u32 *)prop->value,
1066 (unsigned int) (regs->phys_addr & mask));
1070 /* "name@slot,offset" */
1071 static void __init sbus_path_component(struct device_node *dp, char *tmp_buf)
1073 struct linux_prom_registers *regs;
1074 struct property *prop;
1076 prop = of_find_property(dp, "reg", NULL);
1081 sprintf(tmp_buf, "%s@%x,%x",
1087 /* "name@devnum[,func]" */
1088 static void __init pci_path_component(struct device_node *dp, char *tmp_buf)
1090 struct linux_prom_pci_registers *regs;
1091 struct property *prop;
1094 prop = of_find_property(dp, "reg", NULL);
1099 devfn = (regs->phys_hi >> 8) & 0xff;
1101 sprintf(tmp_buf, "%s@%x,%x",
1106 sprintf(tmp_buf, "%s@%x",
1112 /* "name@UPA_PORTID,offset" */
1113 static void __init upa_path_component(struct device_node *dp, char *tmp_buf)
1115 struct linux_prom64_registers *regs;
1116 struct property *prop;
1118 prop = of_find_property(dp, "reg", NULL);
1124 prop = of_find_property(dp, "upa-portid", NULL);
1128 sprintf(tmp_buf, "%s@%x,%x",
1130 *(u32 *) prop->value,
1131 (unsigned int) (regs->phys_addr & 0xffffffffUL));
1135 static void __init vdev_path_component(struct device_node *dp, char *tmp_buf)
1137 struct property *prop;
1140 prop = of_find_property(dp, "reg", NULL);
1146 sprintf(tmp_buf, "%s@%x", dp->name, *regs);
1149 /* "name@addrhi,addrlo" */
1150 static void __init ebus_path_component(struct device_node *dp, char *tmp_buf)
1152 struct linux_prom64_registers *regs;
1153 struct property *prop;
1155 prop = of_find_property(dp, "reg", NULL);
1161 sprintf(tmp_buf, "%s@%x,%x",
1163 (unsigned int) (regs->phys_addr >> 32UL),
1164 (unsigned int) (regs->phys_addr & 0xffffffffUL));
1167 /* "name@bus,addr" */
1168 static void __init i2c_path_component(struct device_node *dp, char *tmp_buf)
1170 struct property *prop;
1173 prop = of_find_property(dp, "reg", NULL);
1179 /* This actually isn't right... should look at the #address-cells
1180 * property of the i2c bus node etc. etc.
1182 sprintf(tmp_buf, "%s@%x,%x",
1183 dp->name, regs[0], regs[1]);
1186 /* "name@reg0[,reg1]" */
1187 static void __init usb_path_component(struct device_node *dp, char *tmp_buf)
1189 struct property *prop;
1192 prop = of_find_property(dp, "reg", NULL);
1198 if (prop->length == sizeof(u32) || regs[1] == 1) {
1199 sprintf(tmp_buf, "%s@%x",
1202 sprintf(tmp_buf, "%s@%x,%x",
1203 dp->name, regs[0], regs[1]);
1207 /* "name@reg0reg1[,reg2reg3]" */
1208 static void __init ieee1394_path_component(struct device_node *dp, char *tmp_buf)
1210 struct property *prop;
1213 prop = of_find_property(dp, "reg", NULL);
1219 if (regs[2] || regs[3]) {
1220 sprintf(tmp_buf, "%s@%08x%08x,%04x%08x",
1221 dp->name, regs[0], regs[1], regs[2], regs[3]);
1223 sprintf(tmp_buf, "%s@%08x%08x",
1224 dp->name, regs[0], regs[1]);
1228 static void __init __build_path_component(struct device_node *dp, char *tmp_buf)
1230 struct device_node *parent = dp->parent;
1232 if (parent != NULL) {
1233 if (!strcmp(parent->type, "pci") ||
1234 !strcmp(parent->type, "pciex"))
1235 return pci_path_component(dp, tmp_buf);
1236 if (!strcmp(parent->type, "sbus"))
1237 return sbus_path_component(dp, tmp_buf);
1238 if (!strcmp(parent->type, "upa"))
1239 return upa_path_component(dp, tmp_buf);
1240 if (!strcmp(parent->type, "ebus"))
1241 return ebus_path_component(dp, tmp_buf);
1242 if (!strcmp(parent->name, "usb") ||
1243 !strcmp(parent->name, "hub"))
1244 return usb_path_component(dp, tmp_buf);
1245 if (!strcmp(parent->type, "i2c"))
1246 return i2c_path_component(dp, tmp_buf);
1247 if (!strcmp(parent->type, "firewire"))
1248 return ieee1394_path_component(dp, tmp_buf);
1249 if (!strcmp(parent->type, "virtual-devices"))
1250 return vdev_path_component(dp, tmp_buf);
1252 /* "isa" is handled with platform naming */
1255 /* Use platform naming convention. */
1256 if (tlb_type == hypervisor)
1257 return sun4v_path_component(dp, tmp_buf);
1259 return sun4u_path_component(dp, tmp_buf);
1262 static char * __init build_path_component(struct device_node *dp)
1264 char tmp_buf[64], *n;
1267 __build_path_component(dp, tmp_buf);
1268 if (tmp_buf[0] == '\0')
1269 strcpy(tmp_buf, dp->name);
1271 n = prom_early_alloc(strlen(tmp_buf) + 1);
1277 static char * __init build_full_name(struct device_node *dp)
1279 int len, ourlen, plen;
1282 plen = strlen(dp->parent->full_name);
1283 ourlen = strlen(dp->path_component_name);
1284 len = ourlen + plen + 2;
1286 n = prom_early_alloc(len);
1287 strcpy(n, dp->parent->full_name);
1288 if (!is_root_node(dp->parent)) {
1289 strcpy(n + plen, "/");
1292 strcpy(n + plen, dp->path_component_name);
1297 static unsigned int unique_id;
1299 static struct property * __init build_one_prop(phandle node, char *prev, char *special_name, void *special_val, int special_len)
1301 static struct property *tmp = NULL;
1306 memset(p, 0, sizeof(*p) + 32);
1309 p = prom_early_alloc(sizeof(struct property) + 32);
1310 p->unique_id = unique_id++;
1313 p->name = (char *) (p + 1);
1315 strcpy(p->name, special_name);
1316 p->length = special_len;
1317 p->value = prom_early_alloc(special_len);
1318 memcpy(p->value, special_val, special_len);
1321 prom_firstprop(node, p->name);
1323 prom_nextprop(node, prev, p->name);
1325 if (strlen(p->name) == 0) {
1329 p->length = prom_getproplen(node, p->name);
1330 if (p->length <= 0) {
1333 p->value = prom_early_alloc(p->length + 1);
1334 prom_getproperty(node, p->name, p->value, p->length);
1335 ((unsigned char *)p->value)[p->length] = '\0';
1341 static struct property * __init build_prop_list(phandle node)
1343 struct property *head, *tail;
1345 head = tail = build_one_prop(node, NULL,
1346 ".node", &node, sizeof(node));
1348 tail->next = build_one_prop(node, NULL, NULL, NULL, 0);
1351 tail->next = build_one_prop(node, tail->name,
1359 static char * __init get_one_property(phandle node, const char *name)
1361 char *buf = "<NULL>";
1364 len = prom_getproplen(node, name);
1366 buf = prom_early_alloc(len);
1367 prom_getproperty(node, name, buf, len);
1373 static struct device_node * __init create_node(phandle node, struct device_node *parent)
1375 struct device_node *dp;
1380 dp = prom_early_alloc(sizeof(*dp));
1381 dp->unique_id = unique_id++;
1382 dp->parent = parent;
1384 kref_init(&dp->kref);
1386 dp->name = get_one_property(node, "name");
1387 dp->type = get_one_property(node, "device_type");
1390 dp->properties = build_prop_list(node);
1397 static struct device_node * __init build_tree(struct device_node *parent, phandle node, struct device_node ***nextp)
1399 struct device_node *ret = NULL, *prev_sibling = NULL;
1400 struct device_node *dp;
1403 dp = create_node(node, parent);
1408 prev_sibling->sibling = dp;
1415 *nextp = &dp->allnext;
1417 dp->path_component_name = build_path_component(dp);
1418 dp->full_name = build_full_name(dp);
1420 dp->child = build_tree(dp, prom_getchild(node), nextp);
1422 node = prom_getsibling(node);
1428 static const char *get_mid_prop(void)
1430 return (tlb_type == spitfire ? "upa-portid" : "portid");
1433 struct device_node *of_find_node_by_cpuid(int cpuid)
1435 struct device_node *dp;
1436 const char *mid_prop = get_mid_prop();
1438 for_each_node_by_type(dp, "cpu") {
1439 int id = of_getintprop_default(dp, mid_prop, -1);
1440 const char *this_mid_prop = mid_prop;
1443 this_mid_prop = "cpuid";
1444 id = of_getintprop_default(dp, this_mid_prop, -1);
1448 prom_printf("OF: Serious problem, cpu lacks "
1449 "%s property", this_mid_prop);
1458 static void __init of_fill_in_cpu_data(void)
1460 struct device_node *dp;
1461 const char *mid_prop = get_mid_prop();
1464 for_each_node_by_type(dp, "cpu") {
1465 int cpuid = of_getintprop_default(dp, mid_prop, -1);
1466 const char *this_mid_prop = mid_prop;
1467 struct device_node *portid_parent;
1470 portid_parent = NULL;
1472 this_mid_prop = "cpuid";
1473 cpuid = of_getintprop_default(dp, this_mid_prop, -1);
1479 portid_parent = portid_parent->parent;
1482 portid = of_getintprop_default(portid_parent,
1491 prom_printf("OF: Serious problem, cpu lacks "
1492 "%s property", this_mid_prop);
1499 if (cpuid >= NR_CPUS) {
1500 printk(KERN_WARNING "Ignoring CPU %d which is "
1501 ">= NR_CPUS (%d)\n",
1506 /* On uniprocessor we only want the values for the
1507 * real physical cpu the kernel booted onto, however
1508 * cpu_data() only has one entry at index 0.
1510 if (cpuid != real_hard_smp_processor_id())
1515 cpu_data(cpuid).clock_tick =
1516 of_getintprop_default(dp, "clock-frequency", 0);
1518 if (portid_parent) {
1519 cpu_data(cpuid).dcache_size =
1520 of_getintprop_default(dp, "l1-dcache-size",
1522 cpu_data(cpuid).dcache_line_size =
1523 of_getintprop_default(dp, "l1-dcache-line-size",
1525 cpu_data(cpuid).icache_size =
1526 of_getintprop_default(dp, "l1-icache-size",
1528 cpu_data(cpuid).icache_line_size =
1529 of_getintprop_default(dp, "l1-icache-line-size",
1531 cpu_data(cpuid).ecache_size =
1532 of_getintprop_default(dp, "l2-cache-size", 0);
1533 cpu_data(cpuid).ecache_line_size =
1534 of_getintprop_default(dp, "l2-cache-line-size", 0);
1535 if (!cpu_data(cpuid).ecache_size ||
1536 !cpu_data(cpuid).ecache_line_size) {
1537 cpu_data(cpuid).ecache_size =
1538 of_getintprop_default(portid_parent,
1541 cpu_data(cpuid).ecache_line_size =
1542 of_getintprop_default(portid_parent,
1543 "l2-cache-line-size", 64);
1546 cpu_data(cpuid).core_id = portid + 1;
1547 cpu_data(cpuid).proc_id = portid;
1549 sparc64_multi_core = 1;
1552 cpu_data(cpuid).dcache_size =
1553 of_getintprop_default(dp, "dcache-size", 16 * 1024);
1554 cpu_data(cpuid).dcache_line_size =
1555 of_getintprop_default(dp, "dcache-line-size", 32);
1557 cpu_data(cpuid).icache_size =
1558 of_getintprop_default(dp, "icache-size", 16 * 1024);
1559 cpu_data(cpuid).icache_line_size =
1560 of_getintprop_default(dp, "icache-line-size", 32);
1562 cpu_data(cpuid).ecache_size =
1563 of_getintprop_default(dp, "ecache-size",
1565 cpu_data(cpuid).ecache_line_size =
1566 of_getintprop_default(dp, "ecache-line-size", 64);
1568 cpu_data(cpuid).core_id = 0;
1569 cpu_data(cpuid).proc_id = -1;
1573 cpu_set(cpuid, cpu_present_map);
1574 cpu_set(cpuid, cpu_possible_map);
1578 smp_fill_in_sib_core_maps();
1581 struct device_node *of_console_device;
1582 EXPORT_SYMBOL(of_console_device);
1584 char *of_console_path;
1585 EXPORT_SYMBOL(of_console_path);
1587 char *of_console_options;
1588 EXPORT_SYMBOL(of_console_options);
1590 static void __init of_console_init(void)
1592 char *msg = "OF stdout device is: %s\n";
1593 struct device_node *dp;
1597 of_console_path = prom_early_alloc(256);
1598 if (prom_ihandle2path(prom_stdout, of_console_path, 256) < 0) {
1599 prom_printf("Cannot obtain path of stdout.\n");
1602 of_console_options = strrchr(of_console_path, ':');
1603 if (of_console_options) {
1604 of_console_options++;
1605 if (*of_console_options == '\0')
1606 of_console_options = NULL;
1609 node = prom_inst2pkg(prom_stdout);
1611 prom_printf("Cannot resolve stdout node from "
1612 "instance %08x.\n", prom_stdout);
1616 dp = of_find_node_by_phandle(node);
1617 type = of_get_property(dp, "device_type", NULL);
1619 prom_printf("Console stdout lacks device_type property.\n");
1623 if (strcmp(type, "display") && strcmp(type, "serial")) {
1624 prom_printf("Console device_type is neither display "
1629 of_console_device = dp;
1631 printk(msg, of_console_path);
1634 void __init prom_build_devicetree(void)
1636 struct device_node **nextp;
1638 allnodes = create_node(prom_root_node, NULL);
1639 allnodes->path_component_name = "";
1640 allnodes->full_name = "/";
1642 nextp = &allnodes->allnext;
1643 allnodes->child = build_tree(allnodes,
1644 prom_getchild(allnodes->node),
1648 printk("PROM: Built device tree with %u bytes of memory.\n",
1649 prom_early_allocated);
1651 if (tlb_type != hypervisor)
1652 of_fill_in_cpu_data();