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; 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 DEFINE_MUTEX(of_set_property_mutex);
63 EXPORT_SYMBOL(of_set_property_mutex);
65 int of_set_property(struct device_node *dp, const char *name, void *val, int len)
67 struct property **prevp;
71 new_val = kmalloc(len, GFP_KERNEL);
75 memcpy(new_val, val, len);
79 write_lock(&devtree_lock);
80 prevp = &dp->properties;
82 struct property *prop = *prevp;
84 if (!strcasecmp(prop->name, name)) {
85 void *old_val = prop->value;
88 mutex_lock(&of_set_property_mutex);
89 ret = prom_setprop(dp->node, name, val, len);
90 mutex_unlock(&of_set_property_mutex);
94 prop->value = new_val;
97 if (OF_IS_DYNAMIC(prop))
100 OF_MARK_DYNAMIC(prop);
106 prevp = &(*prevp)->next;
108 write_unlock(&devtree_lock);
110 /* XXX Upate procfs if necessary... */
114 EXPORT_SYMBOL(of_set_property);
116 int of_find_in_proplist(const char *list, const char *match, int len)
121 if (!strcmp(list, match))
123 l = strlen(list) + 1;
129 EXPORT_SYMBOL(of_find_in_proplist);
131 static unsigned int prom_early_allocated __initdata;
133 static void * __init prom_early_alloc(unsigned long size)
135 unsigned long paddr = lmb_alloc(size, SMP_CACHE_BYTES);
139 prom_printf("prom_early_alloc(%lu) failed\n");
144 memset(ret, 0, size);
145 prom_early_allocated += size;
151 /* PSYCHO interrupt mapping support. */
152 #define PSYCHO_IMAP_A_SLOT0 0x0c00UL
153 #define PSYCHO_IMAP_B_SLOT0 0x0c20UL
154 static unsigned long psycho_pcislot_imap_offset(unsigned long ino)
156 unsigned int bus = (ino & 0x10) >> 4;
157 unsigned int slot = (ino & 0x0c) >> 2;
160 return PSYCHO_IMAP_A_SLOT0 + (slot * 8);
162 return PSYCHO_IMAP_B_SLOT0 + (slot * 8);
165 #define PSYCHO_OBIO_IMAP_BASE 0x1000UL
167 #define PSYCHO_ONBOARD_IRQ_BASE 0x20
168 #define psycho_onboard_imap_offset(__ino) \
169 (PSYCHO_OBIO_IMAP_BASE + (((__ino) & 0x1f) << 3))
171 #define PSYCHO_ICLR_A_SLOT0 0x1400UL
172 #define PSYCHO_ICLR_SCSI 0x1800UL
174 #define psycho_iclr_offset(ino) \
175 ((ino & 0x20) ? (PSYCHO_ICLR_SCSI + (((ino) & 0x1f) << 3)) : \
176 (PSYCHO_ICLR_A_SLOT0 + (((ino) & 0x1f)<<3)))
178 static unsigned int psycho_irq_build(struct device_node *dp,
182 unsigned long controller_regs = (unsigned long) _data;
183 unsigned long imap, iclr;
184 unsigned long imap_off, iclr_off;
188 if (ino < PSYCHO_ONBOARD_IRQ_BASE) {
190 imap_off = psycho_pcislot_imap_offset(ino);
193 imap_off = psycho_onboard_imap_offset(ino);
196 /* Now build the IRQ bucket. */
197 imap = controller_regs + imap_off;
199 iclr_off = psycho_iclr_offset(ino);
200 iclr = controller_regs + iclr_off;
202 if ((ino & 0x20) == 0)
203 inofixup = ino & 0x03;
205 return build_irq(inofixup, iclr, imap);
208 static void __init psycho_irq_trans_init(struct device_node *dp)
210 const struct linux_prom64_registers *regs;
212 dp->irq_trans = prom_early_alloc(sizeof(struct of_irq_controller));
213 dp->irq_trans->irq_build = psycho_irq_build;
215 regs = of_get_property(dp, "reg", NULL);
216 dp->irq_trans->data = (void *) regs[2].phys_addr;
219 #define sabre_read(__reg) \
221 __asm__ __volatile__("ldxa [%1] %2, %0" \
223 : "r" (__reg), "i" (ASI_PHYS_BYPASS_EC_E) \
228 struct sabre_irq_data {
229 unsigned long controller_regs;
230 unsigned int pci_first_busno;
232 #define SABRE_CONFIGSPACE 0x001000000UL
233 #define SABRE_WRSYNC 0x1c20UL
235 #define SABRE_CONFIG_BASE(CONFIG_SPACE) \
236 (CONFIG_SPACE | (1UL << 24))
237 #define SABRE_CONFIG_ENCODE(BUS, DEVFN, REG) \
238 (((unsigned long)(BUS) << 16) | \
239 ((unsigned long)(DEVFN) << 8) | \
240 ((unsigned long)(REG)))
242 /* When a device lives behind a bridge deeper in the PCI bus topology
243 * than APB, a special sequence must run to make sure all pending DMA
244 * transfers at the time of IRQ delivery are visible in the coherency
245 * domain by the cpu. This sequence is to perform a read on the far
246 * side of the non-APB bridge, then perform a read of Sabre's DMA
247 * write-sync register.
249 static void sabre_wsync_handler(unsigned int ino, void *_arg1, void *_arg2)
251 unsigned int phys_hi = (unsigned int) (unsigned long) _arg1;
252 struct sabre_irq_data *irq_data = _arg2;
253 unsigned long controller_regs = irq_data->controller_regs;
254 unsigned long sync_reg = controller_regs + SABRE_WRSYNC;
255 unsigned long config_space = controller_regs + SABRE_CONFIGSPACE;
256 unsigned int bus, devfn;
259 config_space = SABRE_CONFIG_BASE(config_space);
261 bus = (phys_hi >> 16) & 0xff;
262 devfn = (phys_hi >> 8) & 0xff;
264 config_space |= SABRE_CONFIG_ENCODE(bus, devfn, 0x00);
266 __asm__ __volatile__("membar #Sync\n\t"
267 "lduha [%1] %2, %0\n\t"
270 : "r" ((u16 *) config_space),
271 "i" (ASI_PHYS_BYPASS_EC_E_L)
274 sabre_read(sync_reg);
277 #define SABRE_IMAP_A_SLOT0 0x0c00UL
278 #define SABRE_IMAP_B_SLOT0 0x0c20UL
279 #define SABRE_ICLR_A_SLOT0 0x1400UL
280 #define SABRE_ICLR_B_SLOT0 0x1480UL
281 #define SABRE_ICLR_SCSI 0x1800UL
282 #define SABRE_ICLR_ETH 0x1808UL
283 #define SABRE_ICLR_BPP 0x1810UL
284 #define SABRE_ICLR_AU_REC 0x1818UL
285 #define SABRE_ICLR_AU_PLAY 0x1820UL
286 #define SABRE_ICLR_PFAIL 0x1828UL
287 #define SABRE_ICLR_KMS 0x1830UL
288 #define SABRE_ICLR_FLPY 0x1838UL
289 #define SABRE_ICLR_SHW 0x1840UL
290 #define SABRE_ICLR_KBD 0x1848UL
291 #define SABRE_ICLR_MS 0x1850UL
292 #define SABRE_ICLR_SER 0x1858UL
293 #define SABRE_ICLR_UE 0x1870UL
294 #define SABRE_ICLR_CE 0x1878UL
295 #define SABRE_ICLR_PCIERR 0x1880UL
297 static unsigned long sabre_pcislot_imap_offset(unsigned long ino)
299 unsigned int bus = (ino & 0x10) >> 4;
300 unsigned int slot = (ino & 0x0c) >> 2;
303 return SABRE_IMAP_A_SLOT0 + (slot * 8);
305 return SABRE_IMAP_B_SLOT0 + (slot * 8);
308 #define SABRE_OBIO_IMAP_BASE 0x1000UL
309 #define SABRE_ONBOARD_IRQ_BASE 0x20
310 #define sabre_onboard_imap_offset(__ino) \
311 (SABRE_OBIO_IMAP_BASE + (((__ino) & 0x1f) << 3))
313 #define sabre_iclr_offset(ino) \
314 ((ino & 0x20) ? (SABRE_ICLR_SCSI + (((ino) & 0x1f) << 3)) : \
315 (SABRE_ICLR_A_SLOT0 + (((ino) & 0x1f)<<3)))
317 static int sabre_device_needs_wsync(struct device_node *dp)
319 struct device_node *parent = dp->parent;
320 const char *parent_model, *parent_compat;
322 /* This traversal up towards the root is meant to
325 * 1) non-PCI bus sitting under PCI, such as 'ebus'
326 * 2) the PCI controller interrupts themselves, which
327 * will use the sabre_irq_build but do not need
328 * the DMA synchronization handling
331 if (!strcmp(parent->type, "pci"))
333 parent = parent->parent;
339 parent_model = of_get_property(parent,
342 (!strcmp(parent_model, "SUNW,sabre") ||
343 !strcmp(parent_model, "SUNW,simba")))
346 parent_compat = of_get_property(parent,
349 (!strcmp(parent_compat, "pci108e,a000") ||
350 !strcmp(parent_compat, "pci108e,a001")))
356 static unsigned int sabre_irq_build(struct device_node *dp,
360 struct sabre_irq_data *irq_data = _data;
361 unsigned long controller_regs = irq_data->controller_regs;
362 const struct linux_prom_pci_registers *regs;
363 unsigned long imap, iclr;
364 unsigned long imap_off, iclr_off;
369 if (ino < SABRE_ONBOARD_IRQ_BASE) {
371 imap_off = sabre_pcislot_imap_offset(ino);
374 imap_off = sabre_onboard_imap_offset(ino);
377 /* Now build the IRQ bucket. */
378 imap = controller_regs + imap_off;
380 iclr_off = sabre_iclr_offset(ino);
381 iclr = controller_regs + iclr_off;
383 if ((ino & 0x20) == 0)
384 inofixup = ino & 0x03;
386 virt_irq = build_irq(inofixup, iclr, imap);
388 /* If the parent device is a PCI<->PCI bridge other than
389 * APB, we have to install a pre-handler to ensure that
390 * all pending DMA is drained before the interrupt handler
393 regs = of_get_property(dp, "reg", NULL);
394 if (regs && sabre_device_needs_wsync(dp)) {
395 irq_install_pre_handler(virt_irq,
397 (void *) (long) regs->phys_hi,
404 static void __init sabre_irq_trans_init(struct device_node *dp)
406 const struct linux_prom64_registers *regs;
407 struct sabre_irq_data *irq_data;
410 dp->irq_trans = prom_early_alloc(sizeof(struct of_irq_controller));
411 dp->irq_trans->irq_build = sabre_irq_build;
413 irq_data = prom_early_alloc(sizeof(struct sabre_irq_data));
415 regs = of_get_property(dp, "reg", NULL);
416 irq_data->controller_regs = regs[0].phys_addr;
418 busrange = of_get_property(dp, "bus-range", NULL);
419 irq_data->pci_first_busno = busrange[0];
421 dp->irq_trans->data = irq_data;
424 /* SCHIZO interrupt mapping support. Unlike Psycho, for this controller the
425 * imap/iclr registers are per-PBM.
427 #define SCHIZO_IMAP_BASE 0x1000UL
428 #define SCHIZO_ICLR_BASE 0x1400UL
430 static unsigned long schizo_imap_offset(unsigned long ino)
432 return SCHIZO_IMAP_BASE + (ino * 8UL);
435 static unsigned long schizo_iclr_offset(unsigned long ino)
437 return SCHIZO_ICLR_BASE + (ino * 8UL);
440 static unsigned long schizo_ino_to_iclr(unsigned long pbm_regs,
444 return pbm_regs + schizo_iclr_offset(ino);
447 static unsigned long schizo_ino_to_imap(unsigned long pbm_regs,
450 return pbm_regs + schizo_imap_offset(ino);
453 #define schizo_read(__reg) \
455 __asm__ __volatile__("ldxa [%1] %2, %0" \
457 : "r" (__reg), "i" (ASI_PHYS_BYPASS_EC_E) \
461 #define schizo_write(__reg, __val) \
462 __asm__ __volatile__("stxa %0, [%1] %2" \
464 : "r" (__val), "r" (__reg), \
465 "i" (ASI_PHYS_BYPASS_EC_E) \
468 static void tomatillo_wsync_handler(unsigned int ino, void *_arg1, void *_arg2)
470 unsigned long sync_reg = (unsigned long) _arg2;
471 u64 mask = 1UL << (ino & IMAP_INO);
475 schizo_write(sync_reg, mask);
480 val = schizo_read(sync_reg);
485 printk("tomatillo_wsync_handler: DMA won't sync [%lx:%lx]\n",
490 static unsigned char cacheline[64]
491 __attribute__ ((aligned (64)));
493 __asm__ __volatile__("rd %%fprs, %0\n\t"
495 "wr %1, 0x0, %%fprs\n\t"
496 "stda %%f0, [%5] %6\n\t"
497 "wr %0, 0x0, %%fprs\n\t"
499 : "=&r" (mask), "=&r" (val)
500 : "0" (mask), "1" (val),
501 "i" (FPRS_FEF), "r" (&cacheline[0]),
502 "i" (ASI_BLK_COMMIT_P));
506 struct schizo_irq_data {
507 unsigned long pbm_regs;
508 unsigned long sync_reg;
513 static unsigned int schizo_irq_build(struct device_node *dp,
517 struct schizo_irq_data *irq_data = _data;
518 unsigned long pbm_regs = irq_data->pbm_regs;
519 unsigned long imap, iclr;
526 /* Now build the IRQ bucket. */
527 imap = schizo_ino_to_imap(pbm_regs, ino);
528 iclr = schizo_ino_to_iclr(pbm_regs, ino);
530 /* On Schizo, no inofixup occurs. This is because each
531 * INO has it's own IMAP register. On Psycho and Sabre
532 * there is only one IMAP register for each PCI slot even
533 * though four different INOs can be generated by each
536 * But, for JBUS variants (essentially, Tomatillo), we have
537 * to fixup the lowest bit of the interrupt group number.
541 is_tomatillo = (irq_data->sync_reg != 0UL);
544 if (irq_data->portid & 1)
545 ign_fixup = (1 << 6);
548 virt_irq = build_irq(ign_fixup, iclr, imap);
551 irq_install_pre_handler(virt_irq,
552 tomatillo_wsync_handler,
553 ((irq_data->chip_version <= 4) ?
554 (void *) 1 : (void *) 0),
555 (void *) irq_data->sync_reg);
561 static void __init __schizo_irq_trans_init(struct device_node *dp,
564 const struct linux_prom64_registers *regs;
565 struct schizo_irq_data *irq_data;
567 dp->irq_trans = prom_early_alloc(sizeof(struct of_irq_controller));
568 dp->irq_trans->irq_build = schizo_irq_build;
570 irq_data = prom_early_alloc(sizeof(struct schizo_irq_data));
572 regs = of_get_property(dp, "reg", NULL);
573 dp->irq_trans->data = irq_data;
575 irq_data->pbm_regs = regs[0].phys_addr;
577 irq_data->sync_reg = regs[3].phys_addr + 0x1a18UL;
579 irq_data->sync_reg = 0UL;
580 irq_data->portid = of_getintprop_default(dp, "portid", 0);
581 irq_data->chip_version = of_getintprop_default(dp, "version#", 0);
584 static void __init schizo_irq_trans_init(struct device_node *dp)
586 __schizo_irq_trans_init(dp, 0);
589 static void __init tomatillo_irq_trans_init(struct device_node *dp)
591 __schizo_irq_trans_init(dp, 1);
594 static unsigned int pci_sun4v_irq_build(struct device_node *dp,
598 u32 devhandle = (u32) (unsigned long) _data;
600 return sun4v_build_irq(devhandle, devino);
603 static void __init pci_sun4v_irq_trans_init(struct device_node *dp)
605 const struct linux_prom64_registers *regs;
607 dp->irq_trans = prom_early_alloc(sizeof(struct of_irq_controller));
608 dp->irq_trans->irq_build = pci_sun4v_irq_build;
610 regs = of_get_property(dp, "reg", NULL);
611 dp->irq_trans->data = (void *) (unsigned long)
612 ((regs->phys_addr >> 32UL) & 0x0fffffff);
615 struct fire_irq_data {
616 unsigned long pbm_regs;
620 #define FIRE_IMAP_BASE 0x001000
621 #define FIRE_ICLR_BASE 0x001400
623 static unsigned long fire_imap_offset(unsigned long ino)
625 return FIRE_IMAP_BASE + (ino * 8UL);
628 static unsigned long fire_iclr_offset(unsigned long ino)
630 return FIRE_ICLR_BASE + (ino * 8UL);
633 static unsigned long fire_ino_to_iclr(unsigned long pbm_regs,
636 return pbm_regs + fire_iclr_offset(ino);
639 static unsigned long fire_ino_to_imap(unsigned long pbm_regs,
642 return pbm_regs + fire_imap_offset(ino);
645 static unsigned int fire_irq_build(struct device_node *dp,
649 struct fire_irq_data *irq_data = _data;
650 unsigned long pbm_regs = irq_data->pbm_regs;
651 unsigned long imap, iclr;
652 unsigned long int_ctrlr;
656 /* Now build the IRQ bucket. */
657 imap = fire_ino_to_imap(pbm_regs, ino);
658 iclr = fire_ino_to_iclr(pbm_regs, ino);
660 /* Set the interrupt controller number. */
662 upa_writeq(int_ctrlr, imap);
664 /* The interrupt map registers do not have an INO field
665 * like other chips do. They return zero in the INO
666 * field, and the interrupt controller number is controlled
667 * in bits 6 to 9. So in order for build_irq() to get
668 * the INO right we pass it in as part of the fixup
669 * which will get added to the map register zero value
670 * read by build_irq().
672 ino |= (irq_data->portid << 6);
674 return build_irq(ino, iclr, imap);
677 static void __init fire_irq_trans_init(struct device_node *dp)
679 const struct linux_prom64_registers *regs;
680 struct fire_irq_data *irq_data;
682 dp->irq_trans = prom_early_alloc(sizeof(struct of_irq_controller));
683 dp->irq_trans->irq_build = fire_irq_build;
685 irq_data = prom_early_alloc(sizeof(struct fire_irq_data));
687 regs = of_get_property(dp, "reg", NULL);
688 dp->irq_trans->data = irq_data;
690 irq_data->pbm_regs = regs[0].phys_addr;
691 irq_data->portid = of_getintprop_default(dp, "portid", 0);
693 #endif /* CONFIG_PCI */
696 /* INO number to IMAP register offset for SYSIO external IRQ's.
697 * This should conform to both Sunfire/Wildfire server and Fusion
700 #define SYSIO_IMAP_SLOT0 0x2c00UL
701 #define SYSIO_IMAP_SLOT1 0x2c08UL
702 #define SYSIO_IMAP_SLOT2 0x2c10UL
703 #define SYSIO_IMAP_SLOT3 0x2c18UL
704 #define SYSIO_IMAP_SCSI 0x3000UL
705 #define SYSIO_IMAP_ETH 0x3008UL
706 #define SYSIO_IMAP_BPP 0x3010UL
707 #define SYSIO_IMAP_AUDIO 0x3018UL
708 #define SYSIO_IMAP_PFAIL 0x3020UL
709 #define SYSIO_IMAP_KMS 0x3028UL
710 #define SYSIO_IMAP_FLPY 0x3030UL
711 #define SYSIO_IMAP_SHW 0x3038UL
712 #define SYSIO_IMAP_KBD 0x3040UL
713 #define SYSIO_IMAP_MS 0x3048UL
714 #define SYSIO_IMAP_SER 0x3050UL
715 #define SYSIO_IMAP_TIM0 0x3060UL
716 #define SYSIO_IMAP_TIM1 0x3068UL
717 #define SYSIO_IMAP_UE 0x3070UL
718 #define SYSIO_IMAP_CE 0x3078UL
719 #define SYSIO_IMAP_SBERR 0x3080UL
720 #define SYSIO_IMAP_PMGMT 0x3088UL
721 #define SYSIO_IMAP_GFX 0x3090UL
722 #define SYSIO_IMAP_EUPA 0x3098UL
724 #define bogon ((unsigned long) -1)
725 static unsigned long sysio_irq_offsets[] = {
726 /* SBUS Slot 0 --> 3, level 1 --> 7 */
727 SYSIO_IMAP_SLOT0, SYSIO_IMAP_SLOT0, SYSIO_IMAP_SLOT0, SYSIO_IMAP_SLOT0,
728 SYSIO_IMAP_SLOT0, SYSIO_IMAP_SLOT0, SYSIO_IMAP_SLOT0, SYSIO_IMAP_SLOT0,
729 SYSIO_IMAP_SLOT1, SYSIO_IMAP_SLOT1, SYSIO_IMAP_SLOT1, SYSIO_IMAP_SLOT1,
730 SYSIO_IMAP_SLOT1, SYSIO_IMAP_SLOT1, SYSIO_IMAP_SLOT1, SYSIO_IMAP_SLOT1,
731 SYSIO_IMAP_SLOT2, SYSIO_IMAP_SLOT2, SYSIO_IMAP_SLOT2, SYSIO_IMAP_SLOT2,
732 SYSIO_IMAP_SLOT2, SYSIO_IMAP_SLOT2, SYSIO_IMAP_SLOT2, SYSIO_IMAP_SLOT2,
733 SYSIO_IMAP_SLOT3, SYSIO_IMAP_SLOT3, SYSIO_IMAP_SLOT3, SYSIO_IMAP_SLOT3,
734 SYSIO_IMAP_SLOT3, SYSIO_IMAP_SLOT3, SYSIO_IMAP_SLOT3, SYSIO_IMAP_SLOT3,
736 /* Onboard devices (not relevant/used on SunFire). */
767 #define NUM_SYSIO_OFFSETS ARRAY_SIZE(sysio_irq_offsets)
769 /* Convert Interrupt Mapping register pointer to associated
770 * Interrupt Clear register pointer, SYSIO specific version.
772 #define SYSIO_ICLR_UNUSED0 0x3400UL
773 #define SYSIO_ICLR_SLOT0 0x3408UL
774 #define SYSIO_ICLR_SLOT1 0x3448UL
775 #define SYSIO_ICLR_SLOT2 0x3488UL
776 #define SYSIO_ICLR_SLOT3 0x34c8UL
777 static unsigned long sysio_imap_to_iclr(unsigned long imap)
779 unsigned long diff = SYSIO_ICLR_UNUSED0 - SYSIO_IMAP_SLOT0;
783 static unsigned int sbus_of_build_irq(struct device_node *dp,
787 unsigned long reg_base = (unsigned long) _data;
788 const struct linux_prom_registers *regs;
789 unsigned long imap, iclr;
795 regs = of_get_property(dp, "reg", NULL);
797 sbus_slot = regs->which_io;
800 ino += (sbus_slot * 8);
802 imap = sysio_irq_offsets[ino];
803 if (imap == ((unsigned long)-1)) {
804 prom_printf("get_irq_translations: Bad SYSIO INO[%x]\n",
810 /* SYSIO inconsistency. For external SLOTS, we have to select
811 * the right ICLR register based upon the lower SBUS irq level
815 iclr = sysio_imap_to_iclr(imap);
817 sbus_level = ino & 0x7;
821 iclr = reg_base + SYSIO_ICLR_SLOT0;
824 iclr = reg_base + SYSIO_ICLR_SLOT1;
827 iclr = reg_base + SYSIO_ICLR_SLOT2;
831 iclr = reg_base + SYSIO_ICLR_SLOT3;
835 iclr += ((unsigned long)sbus_level - 1UL) * 8UL;
837 return build_irq(sbus_level, iclr, imap);
840 static void __init sbus_irq_trans_init(struct device_node *dp)
842 const struct linux_prom64_registers *regs;
844 dp->irq_trans = prom_early_alloc(sizeof(struct of_irq_controller));
845 dp->irq_trans->irq_build = sbus_of_build_irq;
847 regs = of_get_property(dp, "reg", NULL);
848 dp->irq_trans->data = (void *) (unsigned long) regs->phys_addr;
850 #endif /* CONFIG_SBUS */
853 static unsigned int central_build_irq(struct device_node *dp,
857 struct device_node *central_dp = _data;
858 struct of_device *central_op = of_find_device_by_node(central_dp);
859 struct resource *res;
860 unsigned long imap, iclr;
863 if (!strcmp(dp->name, "eeprom")) {
864 res = ¢ral_op->resource[5];
865 } else if (!strcmp(dp->name, "zs")) {
866 res = ¢ral_op->resource[4];
867 } else if (!strcmp(dp->name, "clock-board")) {
868 res = ¢ral_op->resource[3];
873 imap = res->start + 0x00UL;
874 iclr = res->start + 0x10UL;
876 /* Set the INO state to idle, and disable. */
880 tmp = upa_readl(imap);
882 upa_writel(tmp, imap);
884 return build_irq(0, iclr, imap);
887 static void __init central_irq_trans_init(struct device_node *dp)
889 dp->irq_trans = prom_early_alloc(sizeof(struct of_irq_controller));
890 dp->irq_trans->irq_build = central_build_irq;
892 dp->irq_trans->data = dp;
897 void (*init)(struct device_node *);
901 static struct irq_trans __initdata pci_irq_trans_table[] = {
902 { "SUNW,sabre", sabre_irq_trans_init },
903 { "pci108e,a000", sabre_irq_trans_init },
904 { "pci108e,a001", sabre_irq_trans_init },
905 { "SUNW,psycho", psycho_irq_trans_init },
906 { "pci108e,8000", psycho_irq_trans_init },
907 { "SUNW,schizo", schizo_irq_trans_init },
908 { "pci108e,8001", schizo_irq_trans_init },
909 { "SUNW,schizo+", schizo_irq_trans_init },
910 { "pci108e,8002", schizo_irq_trans_init },
911 { "SUNW,tomatillo", tomatillo_irq_trans_init },
912 { "pci108e,a801", tomatillo_irq_trans_init },
913 { "SUNW,sun4v-pci", pci_sun4v_irq_trans_init },
914 { "pciex108e,80f0", fire_irq_trans_init },
918 static unsigned int sun4v_vdev_irq_build(struct device_node *dp,
922 u32 devhandle = (u32) (unsigned long) _data;
924 return sun4v_build_irq(devhandle, devino);
927 static void __init sun4v_vdev_irq_trans_init(struct device_node *dp)
929 const struct linux_prom64_registers *regs;
931 dp->irq_trans = prom_early_alloc(sizeof(struct of_irq_controller));
932 dp->irq_trans->irq_build = sun4v_vdev_irq_build;
934 regs = of_get_property(dp, "reg", NULL);
935 dp->irq_trans->data = (void *) (unsigned long)
936 ((regs->phys_addr >> 32UL) & 0x0fffffff);
939 static void __init irq_trans_init(struct device_node *dp)
947 model = of_get_property(dp, "model", NULL);
949 model = of_get_property(dp, "compatible", NULL);
951 for (i = 0; i < ARRAY_SIZE(pci_irq_trans_table); i++) {
952 struct irq_trans *t = &pci_irq_trans_table[i];
954 if (!strcmp(model, t->name)) {
962 if (!strcmp(dp->name, "sbus") ||
963 !strcmp(dp->name, "sbi")) {
964 sbus_irq_trans_init(dp);
968 if (!strcmp(dp->name, "fhc") &&
969 !strcmp(dp->parent->name, "central")) {
970 central_irq_trans_init(dp);
973 if (!strcmp(dp->name, "virtual-devices") ||
974 !strcmp(dp->name, "niu")) {
975 sun4v_vdev_irq_trans_init(dp);
980 static int is_root_node(const struct device_node *dp)
985 return (dp->parent == NULL);
988 /* The following routines deal with the black magic of fully naming a
991 * Certain well known named nodes are just the simple name string.
993 * Actual devices have an address specifier appended to the base name
994 * string, like this "foo@addr". The "addr" can be in any number of
995 * formats, and the platform plus the type of the node determine the
996 * format and how it is constructed.
998 * For children of the ROOT node, the naming convention is fixed and
999 * determined by whether this is a sun4u or sun4v system.
1001 * For children of other nodes, it is bus type specific. So
1002 * we walk up the tree until we discover a "device_type" property
1003 * we recognize and we go from there.
1005 * As an example, the boot device on my workstation has a full path:
1007 * /pci@1e,600000/ide@d/disk@0,0:c
1009 static void __init sun4v_path_component(struct device_node *dp, char *tmp_buf)
1011 struct linux_prom64_registers *regs;
1012 struct property *rprop;
1013 u32 high_bits, low_bits, type;
1015 rprop = of_find_property(dp, "reg", NULL);
1019 regs = rprop->value;
1020 if (!is_root_node(dp->parent)) {
1021 sprintf(tmp_buf, "%s@%x,%x",
1023 (unsigned int) (regs->phys_addr >> 32UL),
1024 (unsigned int) (regs->phys_addr & 0xffffffffUL));
1028 type = regs->phys_addr >> 60UL;
1029 high_bits = (regs->phys_addr >> 32UL) & 0x0fffffffUL;
1030 low_bits = (regs->phys_addr & 0xffffffffUL);
1032 if (type == 0 || type == 8) {
1033 const char *prefix = (type == 0) ? "m" : "i";
1036 sprintf(tmp_buf, "%s@%s%x,%x",
1038 high_bits, low_bits);
1040 sprintf(tmp_buf, "%s@%s%x",
1044 } else if (type == 12) {
1045 sprintf(tmp_buf, "%s@%x",
1046 dp->name, high_bits);
1050 static void __init sun4u_path_component(struct device_node *dp, char *tmp_buf)
1052 struct linux_prom64_registers *regs;
1053 struct property *prop;
1055 prop = of_find_property(dp, "reg", NULL);
1060 if (!is_root_node(dp->parent)) {
1061 sprintf(tmp_buf, "%s@%x,%x",
1063 (unsigned int) (regs->phys_addr >> 32UL),
1064 (unsigned int) (regs->phys_addr & 0xffffffffUL));
1068 prop = of_find_property(dp, "upa-portid", NULL);
1070 prop = of_find_property(dp, "portid", NULL);
1072 unsigned long mask = 0xffffffffUL;
1074 if (tlb_type >= cheetah)
1077 sprintf(tmp_buf, "%s@%x,%x",
1079 *(u32 *)prop->value,
1080 (unsigned int) (regs->phys_addr & mask));
1084 /* "name@slot,offset" */
1085 static void __init sbus_path_component(struct device_node *dp, char *tmp_buf)
1087 struct linux_prom_registers *regs;
1088 struct property *prop;
1090 prop = of_find_property(dp, "reg", NULL);
1095 sprintf(tmp_buf, "%s@%x,%x",
1101 /* "name@devnum[,func]" */
1102 static void __init pci_path_component(struct device_node *dp, char *tmp_buf)
1104 struct linux_prom_pci_registers *regs;
1105 struct property *prop;
1108 prop = of_find_property(dp, "reg", NULL);
1113 devfn = (regs->phys_hi >> 8) & 0xff;
1115 sprintf(tmp_buf, "%s@%x,%x",
1120 sprintf(tmp_buf, "%s@%x",
1126 /* "name@UPA_PORTID,offset" */
1127 static void __init upa_path_component(struct device_node *dp, char *tmp_buf)
1129 struct linux_prom64_registers *regs;
1130 struct property *prop;
1132 prop = of_find_property(dp, "reg", NULL);
1138 prop = of_find_property(dp, "upa-portid", NULL);
1142 sprintf(tmp_buf, "%s@%x,%x",
1144 *(u32 *) prop->value,
1145 (unsigned int) (regs->phys_addr & 0xffffffffUL));
1149 static void __init vdev_path_component(struct device_node *dp, char *tmp_buf)
1151 struct property *prop;
1154 prop = of_find_property(dp, "reg", NULL);
1160 sprintf(tmp_buf, "%s@%x", dp->name, *regs);
1163 /* "name@addrhi,addrlo" */
1164 static void __init ebus_path_component(struct device_node *dp, char *tmp_buf)
1166 struct linux_prom64_registers *regs;
1167 struct property *prop;
1169 prop = of_find_property(dp, "reg", NULL);
1175 sprintf(tmp_buf, "%s@%x,%x",
1177 (unsigned int) (regs->phys_addr >> 32UL),
1178 (unsigned int) (regs->phys_addr & 0xffffffffUL));
1181 /* "name@bus,addr" */
1182 static void __init i2c_path_component(struct device_node *dp, char *tmp_buf)
1184 struct property *prop;
1187 prop = of_find_property(dp, "reg", NULL);
1193 /* This actually isn't right... should look at the #address-cells
1194 * property of the i2c bus node etc. etc.
1196 sprintf(tmp_buf, "%s@%x,%x",
1197 dp->name, regs[0], regs[1]);
1200 /* "name@reg0[,reg1]" */
1201 static void __init usb_path_component(struct device_node *dp, char *tmp_buf)
1203 struct property *prop;
1206 prop = of_find_property(dp, "reg", NULL);
1212 if (prop->length == sizeof(u32) || regs[1] == 1) {
1213 sprintf(tmp_buf, "%s@%x",
1216 sprintf(tmp_buf, "%s@%x,%x",
1217 dp->name, regs[0], regs[1]);
1221 /* "name@reg0reg1[,reg2reg3]" */
1222 static void __init ieee1394_path_component(struct device_node *dp, char *tmp_buf)
1224 struct property *prop;
1227 prop = of_find_property(dp, "reg", NULL);
1233 if (regs[2] || regs[3]) {
1234 sprintf(tmp_buf, "%s@%08x%08x,%04x%08x",
1235 dp->name, regs[0], regs[1], regs[2], regs[3]);
1237 sprintf(tmp_buf, "%s@%08x%08x",
1238 dp->name, regs[0], regs[1]);
1242 static void __init __build_path_component(struct device_node *dp, char *tmp_buf)
1244 struct device_node *parent = dp->parent;
1246 if (parent != NULL) {
1247 if (!strcmp(parent->type, "pci") ||
1248 !strcmp(parent->type, "pciex")) {
1249 pci_path_component(dp, tmp_buf);
1252 if (!strcmp(parent->type, "sbus")) {
1253 sbus_path_component(dp, tmp_buf);
1256 if (!strcmp(parent->type, "upa")) {
1257 upa_path_component(dp, tmp_buf);
1260 if (!strcmp(parent->type, "ebus")) {
1261 ebus_path_component(dp, tmp_buf);
1264 if (!strcmp(parent->name, "usb") ||
1265 !strcmp(parent->name, "hub")) {
1266 usb_path_component(dp, tmp_buf);
1269 if (!strcmp(parent->type, "i2c")) {
1270 i2c_path_component(dp, tmp_buf);
1273 if (!strcmp(parent->type, "firewire")) {
1274 ieee1394_path_component(dp, tmp_buf);
1277 if (!strcmp(parent->type, "virtual-devices")) {
1278 vdev_path_component(dp, tmp_buf);
1281 /* "isa" is handled with platform naming */
1284 /* Use platform naming convention. */
1285 if (tlb_type == hypervisor) {
1286 sun4v_path_component(dp, tmp_buf);
1289 sun4u_path_component(dp, tmp_buf);
1293 static char * __init build_path_component(struct device_node *dp)
1295 char tmp_buf[64], *n;
1298 __build_path_component(dp, tmp_buf);
1299 if (tmp_buf[0] == '\0')
1300 strcpy(tmp_buf, dp->name);
1302 n = prom_early_alloc(strlen(tmp_buf) + 1);
1308 static char * __init build_full_name(struct device_node *dp)
1310 int len, ourlen, plen;
1313 plen = strlen(dp->parent->full_name);
1314 ourlen = strlen(dp->path_component_name);
1315 len = ourlen + plen + 2;
1317 n = prom_early_alloc(len);
1318 strcpy(n, dp->parent->full_name);
1319 if (!is_root_node(dp->parent)) {
1320 strcpy(n + plen, "/");
1323 strcpy(n + plen, dp->path_component_name);
1328 static unsigned int unique_id;
1330 static struct property * __init build_one_prop(phandle node, char *prev, char *special_name, void *special_val, int special_len)
1332 static struct property *tmp = NULL;
1337 memset(p, 0, sizeof(*p) + 32);
1340 p = prom_early_alloc(sizeof(struct property) + 32);
1341 p->unique_id = unique_id++;
1344 p->name = (char *) (p + 1);
1346 strcpy(p->name, special_name);
1347 p->length = special_len;
1348 p->value = prom_early_alloc(special_len);
1349 memcpy(p->value, special_val, special_len);
1352 prom_firstprop(node, p->name);
1354 prom_nextprop(node, prev, p->name);
1356 if (strlen(p->name) == 0) {
1360 p->length = prom_getproplen(node, p->name);
1361 if (p->length <= 0) {
1364 p->value = prom_early_alloc(p->length + 1);
1365 prom_getproperty(node, p->name, p->value, p->length);
1366 ((unsigned char *)p->value)[p->length] = '\0';
1372 static struct property * __init build_prop_list(phandle node)
1374 struct property *head, *tail;
1376 head = tail = build_one_prop(node, NULL,
1377 ".node", &node, sizeof(node));
1379 tail->next = build_one_prop(node, NULL, NULL, NULL, 0);
1382 tail->next = build_one_prop(node, tail->name,
1390 static char * __init get_one_property(phandle node, const char *name)
1392 char *buf = "<NULL>";
1395 len = prom_getproplen(node, name);
1397 buf = prom_early_alloc(len);
1398 prom_getproperty(node, name, buf, len);
1404 static struct device_node * __init create_node(phandle node, struct device_node *parent)
1406 struct device_node *dp;
1411 dp = prom_early_alloc(sizeof(*dp));
1412 dp->unique_id = unique_id++;
1413 dp->parent = parent;
1415 kref_init(&dp->kref);
1417 dp->name = get_one_property(node, "name");
1418 dp->type = get_one_property(node, "device_type");
1421 dp->properties = build_prop_list(node);
1428 static struct device_node * __init build_tree(struct device_node *parent, phandle node, struct device_node ***nextp)
1430 struct device_node *ret = NULL, *prev_sibling = NULL;
1431 struct device_node *dp;
1434 dp = create_node(node, parent);
1439 prev_sibling->sibling = dp;
1446 *nextp = &dp->allnext;
1448 dp->path_component_name = build_path_component(dp);
1449 dp->full_name = build_full_name(dp);
1451 dp->child = build_tree(dp, prom_getchild(node), nextp);
1453 node = prom_getsibling(node);
1459 static const char *get_mid_prop(void)
1461 return (tlb_type == spitfire ? "upa-portid" : "portid");
1464 struct device_node *of_find_node_by_cpuid(int cpuid)
1466 struct device_node *dp;
1467 const char *mid_prop = get_mid_prop();
1469 for_each_node_by_type(dp, "cpu") {
1470 int id = of_getintprop_default(dp, mid_prop, -1);
1471 const char *this_mid_prop = mid_prop;
1474 this_mid_prop = "cpuid";
1475 id = of_getintprop_default(dp, this_mid_prop, -1);
1479 prom_printf("OF: Serious problem, cpu lacks "
1480 "%s property", this_mid_prop);
1489 static void __init of_fill_in_cpu_data(void)
1491 struct device_node *dp;
1492 const char *mid_prop = get_mid_prop();
1495 for_each_node_by_type(dp, "cpu") {
1496 int cpuid = of_getintprop_default(dp, mid_prop, -1);
1497 const char *this_mid_prop = mid_prop;
1498 struct device_node *portid_parent;
1501 portid_parent = NULL;
1503 this_mid_prop = "cpuid";
1504 cpuid = of_getintprop_default(dp, this_mid_prop, -1);
1510 portid_parent = portid_parent->parent;
1513 portid = of_getintprop_default(portid_parent,
1522 prom_printf("OF: Serious problem, cpu lacks "
1523 "%s property", this_mid_prop);
1530 if (cpuid >= NR_CPUS) {
1531 printk(KERN_WARNING "Ignoring CPU %d which is "
1532 ">= NR_CPUS (%d)\n",
1537 /* On uniprocessor we only want the values for the
1538 * real physical cpu the kernel booted onto, however
1539 * cpu_data() only has one entry at index 0.
1541 if (cpuid != real_hard_smp_processor_id())
1546 cpu_data(cpuid).clock_tick =
1547 of_getintprop_default(dp, "clock-frequency", 0);
1549 if (portid_parent) {
1550 cpu_data(cpuid).dcache_size =
1551 of_getintprop_default(dp, "l1-dcache-size",
1553 cpu_data(cpuid).dcache_line_size =
1554 of_getintprop_default(dp, "l1-dcache-line-size",
1556 cpu_data(cpuid).icache_size =
1557 of_getintprop_default(dp, "l1-icache-size",
1559 cpu_data(cpuid).icache_line_size =
1560 of_getintprop_default(dp, "l1-icache-line-size",
1562 cpu_data(cpuid).ecache_size =
1563 of_getintprop_default(dp, "l2-cache-size", 0);
1564 cpu_data(cpuid).ecache_line_size =
1565 of_getintprop_default(dp, "l2-cache-line-size", 0);
1566 if (!cpu_data(cpuid).ecache_size ||
1567 !cpu_data(cpuid).ecache_line_size) {
1568 cpu_data(cpuid).ecache_size =
1569 of_getintprop_default(portid_parent,
1572 cpu_data(cpuid).ecache_line_size =
1573 of_getintprop_default(portid_parent,
1574 "l2-cache-line-size", 64);
1577 cpu_data(cpuid).core_id = portid + 1;
1578 cpu_data(cpuid).proc_id = portid;
1580 sparc64_multi_core = 1;
1583 cpu_data(cpuid).dcache_size =
1584 of_getintprop_default(dp, "dcache-size", 16 * 1024);
1585 cpu_data(cpuid).dcache_line_size =
1586 of_getintprop_default(dp, "dcache-line-size", 32);
1588 cpu_data(cpuid).icache_size =
1589 of_getintprop_default(dp, "icache-size", 16 * 1024);
1590 cpu_data(cpuid).icache_line_size =
1591 of_getintprop_default(dp, "icache-line-size", 32);
1593 cpu_data(cpuid).ecache_size =
1594 of_getintprop_default(dp, "ecache-size",
1596 cpu_data(cpuid).ecache_line_size =
1597 of_getintprop_default(dp, "ecache-line-size", 64);
1599 cpu_data(cpuid).core_id = 0;
1600 cpu_data(cpuid).proc_id = -1;
1604 cpu_set(cpuid, cpu_present_map);
1605 cpu_set(cpuid, cpu_possible_map);
1609 smp_fill_in_sib_core_maps();
1612 struct device_node *of_console_device;
1613 EXPORT_SYMBOL(of_console_device);
1615 char *of_console_path;
1616 EXPORT_SYMBOL(of_console_path);
1618 char *of_console_options;
1619 EXPORT_SYMBOL(of_console_options);
1621 static void __init of_console_init(void)
1623 char *msg = "OF stdout device is: %s\n";
1624 struct device_node *dp;
1628 of_console_path = prom_early_alloc(256);
1629 if (prom_ihandle2path(prom_stdout, of_console_path, 256) < 0) {
1630 prom_printf("Cannot obtain path of stdout.\n");
1633 of_console_options = strrchr(of_console_path, ':');
1634 if (of_console_options) {
1635 of_console_options++;
1636 if (*of_console_options == '\0')
1637 of_console_options = NULL;
1640 node = prom_inst2pkg(prom_stdout);
1642 prom_printf("Cannot resolve stdout node from "
1643 "instance %08x.\n", prom_stdout);
1647 dp = of_find_node_by_phandle(node);
1648 type = of_get_property(dp, "device_type", NULL);
1650 prom_printf("Console stdout lacks device_type property.\n");
1654 if (strcmp(type, "display") && strcmp(type, "serial")) {
1655 prom_printf("Console device_type is neither display "
1660 of_console_device = dp;
1662 printk(msg, of_console_path);
1665 void __init prom_build_devicetree(void)
1667 struct device_node **nextp;
1669 allnodes = create_node(prom_root_node, NULL);
1670 allnodes->path_component_name = "";
1671 allnodes->full_name = "/";
1673 nextp = &allnodes->allnext;
1674 allnodes->child = build_tree(allnodes,
1675 prom_getchild(allnodes->node),
1679 printk("PROM: Built device tree with %u bytes of memory.\n",
1680 prom_early_allocated);
1682 if (tlb_type != hypervisor)
1683 of_fill_in_cpu_data();