1 /* $Id: sbus.c,v 1.95.2.3 2002/01/05 01:12:31 davem Exp $
2 * sbus.c: SBus support routines.
4 * Copyright (C) 1995 David S. Miller (davem@caip.rutgers.edu)
7 #include <linux/kernel.h>
8 #include <linux/slab.h>
9 #include <linux/config.h>
10 #include <linux/init.h>
11 #include <linux/pci.h>
13 #include <asm/system.h>
16 #include <asm/oplib.h>
20 struct sbus_bus *sbus_root = NULL;
22 static struct linux_prom_irqs irqs[PROMINTR_MAX] __initdata = { { 0 } };
24 static int interrupts[PROMINTR_MAX] __initdata = { 0 };
28 extern int pcic_present(void);
31 /* Perhaps when I figure out more about the iommu we'll put a
32 * device registration routine here that probe_sbus() calls to
33 * setup the iommu for each Sbus.
36 /* We call this for each SBus device, and fill the structure based
37 * upon the prom device tree. We return the start of memory after
38 * the things we have allocated.
41 /* #define DEBUG_FILL */
43 static void __init fill_sbus_device(int prom_node, struct sbus_dev *sdev)
45 unsigned long address, base;
48 sdev->prom_node = prom_node;
49 prom_getstring(prom_node, "name",
50 sdev->prom_name, sizeof(sdev->prom_name));
51 address = prom_getint(prom_node, "address");
52 len = prom_getproperty(prom_node, "reg",
53 (char *) sdev->reg_addrs,
54 sizeof(sdev->reg_addrs));
56 sdev->num_registers = 0;
60 if (len % sizeof(struct linux_prom_registers)) {
61 prom_printf("fill_sbus_device: proplen for regs of %s "
62 " was %d, need multiple of %d\n",
64 (int) sizeof(struct linux_prom_registers));
67 if (len > (sizeof(struct linux_prom_registers) * PROMREG_MAX)) {
68 prom_printf("fill_sbus_device: Too many register properties "
69 "for device %s, len=%d\n",
70 sdev->prom_name, len);
73 sdev->num_registers = len / sizeof(struct linux_prom_registers);
74 sdev->ranges_applied = 0;
76 base = (unsigned long) sdev->reg_addrs[0].phys_addr;
78 /* Compute the slot number. */
79 if (base >= SUN_SBUS_BVADDR && sparc_cpu_model == sun4m) {
80 sdev->slot = sbus_dev_slot(base);
82 sdev->slot = sdev->reg_addrs[0].which_io;
86 len = prom_getproperty(prom_node, "ranges",
87 (char *)sdev->device_ranges,
88 sizeof(sdev->device_ranges));
90 sdev->num_device_ranges = 0;
93 if (len % sizeof(struct linux_prom_ranges)) {
94 prom_printf("fill_sbus_device: proplen for ranges of %s "
95 " was %d, need multiple of %d\n",
97 (int) sizeof(struct linux_prom_ranges));
100 if (len > (sizeof(struct linux_prom_ranges) * PROMREG_MAX)) {
101 prom_printf("fill_sbus_device: Too many range properties "
102 "for device %s, len=%d\n",
103 sdev->prom_name, len);
106 sdev->num_device_ranges =
107 len / sizeof(struct linux_prom_ranges);
110 /* XXX Unfortunately, IRQ issues are very arch specific.
111 * XXX Pull this crud out into an arch specific area
112 * XXX at some point. -DaveM
115 len = prom_getproperty(prom_node, "interrupts",
116 (char *) irqs, sizeof(irqs));
117 if (len == -1 || len == 0) {
121 unsigned int pri = irqs[0].pri;
125 pri += sdev->slot * 8;
127 sdev->irqs[0] = sbus_build_irq(sdev->bus, pri);
130 len = prom_getproperty(prom_node, "intr",
131 (char *)irqs, sizeof(irqs));
133 sdev->num_irqs = len / 8;
134 if (sdev->num_irqs == 0) {
136 } else if (sparc_cpu_model == sun4d) {
137 extern unsigned int sun4d_build_irq(struct sbus_dev *sdev, int irq);
139 for (len = 0; len < sdev->num_irqs; len++)
140 sdev->irqs[len] = sun4d_build_irq(sdev, irqs[len].pri);
142 for (len = 0; len < sdev->num_irqs; len++)
143 sdev->irqs[len] = irqs[len].pri;
146 /* No "intr" node found-- check for "interrupts" node.
147 * This node contains SBus interrupt levels, not IPLs
148 * as in "intr", and no vector values. We convert
149 * SBus interrupt levels to PILs (platform specific).
151 len = prom_getproperty(prom_node, "interrupts",
152 (char *)interrupts, sizeof(interrupts));
157 sdev->num_irqs = len / sizeof(int);
158 for (len = 0; len < sdev->num_irqs; len++) {
159 sdev->irqs[len] = sbint_to_irq(sdev, interrupts[len]);
163 #endif /* !__sparc_v9__ */
166 /* This routine gets called from whoever needs the sbus first, to scan
167 * the SBus device tree. Currently it just prints out the devices
168 * found on the bus and builds trees of SBUS structs and attached
172 extern void iommu_init(int iommu_node, struct sbus_bus *sbus);
173 extern void iounit_init(int sbi_node, int iounit_node, struct sbus_bus *sbus);
174 void sun4_init(void);
175 #ifdef CONFIG_SUN_AUXIO
176 extern void auxio_probe(void);
179 static void __init sbus_do_child_siblings(int start_node,
180 struct sbus_dev *child,
181 struct sbus_dev *parent,
182 struct sbus_bus *sbus)
184 struct sbus_dev *this_dev = child;
185 int this_node = start_node;
187 /* Child already filled in, just need to traverse siblings. */
189 child->parent = parent;
190 while((this_node = prom_getsibling(this_node)) != 0) {
191 this_dev->next = kmalloc(sizeof(struct sbus_dev), GFP_ATOMIC);
192 this_dev = this_dev->next;
194 this_dev->parent = parent;
196 this_dev->bus = sbus;
197 fill_sbus_device(this_node, this_dev);
199 if(prom_getchild(this_node)) {
200 this_dev->child = kmalloc(sizeof(struct sbus_dev),
202 this_dev->child->bus = sbus;
203 this_dev->child->next = 0;
204 fill_sbus_device(prom_getchild(this_node), this_dev->child);
205 sbus_do_child_siblings(prom_getchild(this_node),
206 this_dev->child, this_dev, sbus);
208 this_dev->child = NULL;
214 * XXX This functions appears to be a distorted version of
215 * prom_sbus_ranges_init(), with all sun4d stuff cut away.
216 * Ask DaveM what is going on here, how is sun4d supposed to work... XXX
218 /* added back sun4d patch from Thomas Bogendoerfer - should be OK (crn) */
220 static void __init sbus_bus_ranges_init(int parent_node, struct sbus_bus *sbus)
224 len = prom_getproperty(sbus->prom_node, "ranges",
225 (char *) sbus->sbus_ranges,
226 sizeof(sbus->sbus_ranges));
227 if (len == -1 || len == 0) {
228 sbus->num_sbus_ranges = 0;
231 sbus->num_sbus_ranges = len / sizeof(struct linux_prom_ranges);
232 #ifdef CONFIG_SPARC32
233 if (sparc_cpu_model == sun4d) {
234 struct linux_prom_ranges iounit_ranges[PROMREG_MAX];
235 int num_iounit_ranges;
237 len = prom_getproperty(parent_node, "ranges",
238 (char *) iounit_ranges,
239 sizeof (iounit_ranges));
241 num_iounit_ranges = (len/sizeof(struct linux_prom_ranges));
242 prom_adjust_ranges (sbus->sbus_ranges, sbus->num_sbus_ranges, iounit_ranges, num_iounit_ranges);
248 static void __init __apply_ranges_to_regs(struct linux_prom_ranges *ranges,
250 struct linux_prom_registers *regs,
256 for (regnum = 0; regnum < num_regs; regnum++) {
259 for (rngnum = 0; rngnum < num_ranges; rngnum++) {
260 if (regs[regnum].which_io == ranges[rngnum].ot_child_space)
263 if (rngnum == num_ranges) {
264 /* We used to flag this as an error. Actually
265 * some devices do not report the regs as we expect.
266 * For example, see SUNW,pln device. In that case
267 * the reg property is in a format internal to that
268 * node, ie. it is not in the SBUS register space
273 regs[regnum].which_io = ranges[rngnum].ot_parent_space;
274 regs[regnum].phys_addr -= ranges[rngnum].ot_child_base;
275 regs[regnum].phys_addr += ranges[rngnum].ot_parent_base;
280 static void __init __fixup_regs_sdev(struct sbus_dev *sdev)
282 if (sdev->num_registers != 0) {
283 struct sbus_dev *parent = sdev->parent;
286 while (parent != NULL) {
287 __apply_ranges_to_regs(parent->device_ranges,
288 parent->num_device_ranges,
290 sdev->num_registers);
292 parent = parent->parent;
295 __apply_ranges_to_regs(sdev->bus->sbus_ranges,
296 sdev->bus->num_sbus_ranges,
298 sdev->num_registers);
300 for (i = 0; i < sdev->num_registers; i++) {
301 struct resource *res = &sdev->resource[i];
303 res->start = sdev->reg_addrs[i].phys_addr;
304 res->end = (res->start +
305 (unsigned long)sdev->reg_addrs[i].reg_size - 1UL);
306 res->flags = IORESOURCE_IO |
307 (sdev->reg_addrs[i].which_io & 0xff);
312 static void __init sbus_fixup_all_regs(struct sbus_dev *first_sdev)
314 struct sbus_dev *sdev;
316 for (sdev = first_sdev; sdev; sdev = sdev->next) {
318 sbus_fixup_all_regs(sdev->child);
319 __fixup_regs_sdev(sdev);
323 extern void register_proc_sparc_ioport(void);
324 extern void firetruck_init(void);
325 extern void rs_init(void);
327 void __init sbus_init(void)
329 int nd, this_sbus, sbus_devs, topnd, iommund;
330 unsigned int sbus_clock;
331 struct sbus_bus *sbus;
332 struct sbus_dev *this_dev;
333 int num_sbus = 0; /* How many did we find? */
336 register_proc_sparc_ioport();
340 return sun4_dvma_init();
343 topnd = prom_getchild(prom_root_node);
345 /* Finding the first sbus is a special case... */
347 if(sparc_cpu_model == sun4u) {
348 nd = prom_searchsiblings(topnd, "sbus");
351 if (!pcic_present()) {
352 prom_printf("Neither SBUS nor PCI found.\n");
361 prom_printf("YEEE, UltraSparc sbus not found\n");
365 } else if(sparc_cpu_model == sun4d) {
366 if((iommund = prom_searchsiblings(topnd, "io-unit")) == 0 ||
367 (nd = prom_getchild(iommund)) == 0 ||
368 (nd = prom_searchsiblings(nd, "sbi")) == 0) {
369 panic("sbi not found");
371 } else if((nd = prom_searchsiblings(topnd, "sbus")) == 0) {
372 if((iommund = prom_searchsiblings(topnd, "iommu")) == 0 ||
373 (nd = prom_getchild(iommund)) == 0 ||
374 (nd = prom_searchsiblings(nd, "sbus")) == 0) {
376 if (!pcic_present()) {
377 prom_printf("Neither SBUS nor PCI found.\n");
382 /* No reason to run further - the data access trap will occur. */
383 panic("sbus not found");
388 /* Ok, we've found the first one, allocate first SBus struct
389 * and place in chain.
391 sbus = sbus_root = kmalloc(sizeof(struct sbus_bus), GFP_ATOMIC);
393 sbus->prom_node = nd;
396 if(iommund && sparc_cpu_model != sun4u && sparc_cpu_model != sun4d)
397 iommu_init(iommund, sbus);
399 /* Loop until we find no more SBUS's */
402 /* IOMMU hides inside SBUS/SYSIO prom node on Ultra. */
403 if(sparc_cpu_model == sun4u) {
404 extern void sbus_iommu_init(int prom_node, struct sbus_bus *sbus);
406 sbus_iommu_init(this_sbus, sbus);
410 if (sparc_cpu_model == sun4d)
411 iounit_init(this_sbus, iommund, sbus);
413 printk("sbus%d: ", num_sbus);
414 sbus_clock = prom_getint(this_sbus, "clock-frequency");
416 sbus_clock = (25*1000*1000);
417 printk("Clock %d.%d MHz\n", (int) ((sbus_clock/1000)/1000),
418 (int) (((sbus_clock/1000)%1000 != 0) ?
419 (((sbus_clock/1000)%1000) + 1000) : 0));
421 prom_getstring(this_sbus, "name",
422 sbus->prom_name, sizeof(sbus->prom_name));
423 sbus->clock_freq = sbus_clock;
425 if (sparc_cpu_model == sun4d) {
426 sbus->devid = prom_getint(iommund, "device-id");
427 sbus->board = prom_getint(iommund, "board#");
431 sbus_bus_ranges_init(iommund, sbus);
433 sbus_devs = prom_getchild(this_sbus);
435 sbus->devices = NULL;
439 sbus->devices = kmalloc(sizeof(struct sbus_dev), GFP_ATOMIC);
441 this_dev = sbus->devices;
442 this_dev->next = NULL;
444 this_dev->bus = sbus;
445 this_dev->parent = NULL;
446 fill_sbus_device(sbus_devs, this_dev);
448 /* Should we traverse for children? */
449 if(prom_getchild(sbus_devs)) {
450 /* Allocate device node */
451 this_dev->child = kmalloc(sizeof(struct sbus_dev),
454 this_dev->child->bus = sbus;
455 this_dev->child->next = 0;
456 fill_sbus_device(prom_getchild(sbus_devs),
458 sbus_do_child_siblings(prom_getchild(sbus_devs),
463 this_dev->child = NULL;
466 while((sbus_devs = prom_getsibling(sbus_devs)) != 0) {
467 /* Allocate device node */
468 this_dev->next = kmalloc(sizeof(struct sbus_dev),
470 this_dev = this_dev->next;
471 this_dev->next = NULL;
474 this_dev->bus = sbus;
475 this_dev->parent = NULL;
476 fill_sbus_device(sbus_devs, this_dev);
478 /* Is there a child node hanging off of us? */
479 if(prom_getchild(sbus_devs)) {
480 /* Get new device struct */
481 this_dev->child = kmalloc(sizeof(struct sbus_dev),
484 this_dev->child->bus = sbus;
485 this_dev->child->next = 0;
486 fill_sbus_device(prom_getchild(sbus_devs),
488 sbus_do_child_siblings(prom_getchild(sbus_devs),
493 this_dev->child = NULL;
497 /* Walk all devices and apply parent ranges. */
498 sbus_fixup_all_regs(sbus->devices);
503 if(sparc_cpu_model == sun4u) {
504 this_sbus = prom_getsibling(this_sbus);
507 this_sbus = prom_searchsiblings(this_sbus, "sbus");
508 } else if(sparc_cpu_model == sun4d) {
509 iommund = prom_getsibling(iommund);
512 iommund = prom_searchsiblings(iommund, "io-unit");
515 this_sbus = prom_searchsiblings(prom_getchild(iommund), "sbi");
517 this_sbus = prom_getsibling(this_sbus);
520 this_sbus = prom_searchsiblings(this_sbus, "sbus");
523 sbus->next = kmalloc(sizeof(struct sbus_bus), GFP_ATOMIC);
526 sbus->prom_node = this_sbus;
530 } /* while(this_sbus) */
532 if (sparc_cpu_model == sun4d) {
533 extern void sun4d_init_sbi_irq(void);
534 sun4d_init_sbi_irq();
540 if (sparc_cpu_model == sun4u) {
544 #ifdef CONFIG_SUN_AUXIO
545 if (sparc_cpu_model == sun4u)
549 if (sparc_cpu_model == sun4u) {
550 extern void clock_probe(void);