1 /* sbus.c: SBus support routines.
3 * Copyright (C) 1995, 2006 David S. Miller (davem@davemloft.net)
6 #include <linux/kernel.h>
7 #include <linux/slab.h>
8 #include <linux/config.h>
9 #include <linux/init.h>
10 #include <linux/pci.h>
12 #include <asm/system.h>
15 #include <asm/oplib.h>
19 struct sbus_bus *sbus_root;
22 extern int pcic_present(void);
25 static void __init fill_sbus_device(int prom_node, struct sbus_dev *sdev)
27 unsigned long address, base;
30 sdev->prom_node = prom_node;
31 prom_getstring(prom_node, "name",
32 sdev->prom_name, sizeof(sdev->prom_name));
33 address = prom_getint(prom_node, "address");
34 len = prom_getproperty(prom_node, "reg",
35 (char *) sdev->reg_addrs,
36 sizeof(sdev->reg_addrs));
37 sdev->num_registers = 0;
40 len / sizeof(struct linux_prom_registers);
41 sdev->ranges_applied = 0;
43 base = (unsigned long) sdev->reg_addrs[0].phys_addr;
45 /* Compute the slot number. */
46 if (base >= SUN_SBUS_BVADDR && sparc_cpu_model == sun4m)
47 sdev->slot = sbus_dev_slot(base);
49 sdev->slot = sdev->reg_addrs[0].which_io;
52 len = prom_getproperty(prom_node, "ranges",
53 (char *)sdev->device_ranges,
54 sizeof(sdev->device_ranges));
55 sdev->num_device_ranges = 0;
57 sdev->num_device_ranges =
58 len / sizeof(struct linux_prom_ranges);
60 sbus_fill_device_irq(sdev);
63 /* This routine gets called from whoever needs the sbus first, to scan
64 * the SBus device tree. Currently it just prints out the devices
65 * found on the bus and builds trees of SBUS structs and attached
69 extern void iommu_init(int iommu_node, struct sbus_bus *sbus);
70 extern void iounit_init(int sbi_node, int iounit_node, struct sbus_bus *sbus);
72 #ifdef CONFIG_SUN_AUXIO
73 extern void auxio_probe(void);
76 static void __init sbus_do_child_siblings(int start_node,
77 struct sbus_dev *child,
78 struct sbus_dev *parent,
79 struct sbus_bus *sbus)
81 struct sbus_dev *this_dev = child;
82 int this_node = start_node;
84 /* Child already filled in, just need to traverse siblings. */
86 child->parent = parent;
87 while((this_node = prom_getsibling(this_node)) != 0) {
88 this_dev->next = kmalloc(sizeof(struct sbus_dev), GFP_ATOMIC);
89 this_dev = this_dev->next;
90 this_dev->next = NULL;
91 this_dev->parent = parent;
94 fill_sbus_device(this_node, this_dev);
96 if(prom_getchild(this_node)) {
97 this_dev->child = kmalloc(sizeof(struct sbus_dev),
99 this_dev->child->bus = sbus;
100 this_dev->child->next = NULL;
101 fill_sbus_device(prom_getchild(this_node), this_dev->child);
102 sbus_do_child_siblings(prom_getchild(this_node),
103 this_dev->child, this_dev, sbus);
105 this_dev->child = NULL;
111 * XXX This functions appears to be a distorted version of
112 * prom_sbus_ranges_init(), with all sun4d stuff cut away.
113 * Ask DaveM what is going on here, how is sun4d supposed to work... XXX
115 /* added back sun4d patch from Thomas Bogendoerfer - should be OK (crn) */
117 static void __init sbus_bus_ranges_init(int parent_node, struct sbus_bus *sbus)
121 len = prom_getproperty(sbus->prom_node, "ranges",
122 (char *) sbus->sbus_ranges,
123 sizeof(sbus->sbus_ranges));
124 if (len == -1 || len == 0) {
125 sbus->num_sbus_ranges = 0;
128 sbus->num_sbus_ranges = len / sizeof(struct linux_prom_ranges);
129 #ifdef CONFIG_SPARC32
130 if (sparc_cpu_model == sun4d) {
131 struct linux_prom_ranges iounit_ranges[PROMREG_MAX];
132 int num_iounit_ranges;
134 len = prom_getproperty(parent_node, "ranges",
135 (char *) iounit_ranges,
136 sizeof (iounit_ranges));
138 num_iounit_ranges = (len/sizeof(struct linux_prom_ranges));
139 prom_adjust_ranges (sbus->sbus_ranges, sbus->num_sbus_ranges, iounit_ranges, num_iounit_ranges);
145 static void __init __apply_ranges_to_regs(struct linux_prom_ranges *ranges,
147 struct linux_prom_registers *regs,
153 for (regnum = 0; regnum < num_regs; regnum++) {
156 for (rngnum = 0; rngnum < num_ranges; rngnum++) {
157 if (regs[regnum].which_io == ranges[rngnum].ot_child_space)
160 if (rngnum == num_ranges) {
161 /* We used to flag this as an error. Actually
162 * some devices do not report the regs as we expect.
163 * For example, see SUNW,pln device. In that case
164 * the reg property is in a format internal to that
165 * node, ie. it is not in the SBUS register space
170 regs[regnum].which_io = ranges[rngnum].ot_parent_space;
171 regs[regnum].phys_addr -= ranges[rngnum].ot_child_base;
172 regs[regnum].phys_addr += ranges[rngnum].ot_parent_base;
177 static void __init __fixup_regs_sdev(struct sbus_dev *sdev)
179 if (sdev->num_registers != 0) {
180 struct sbus_dev *parent = sdev->parent;
183 while (parent != NULL) {
184 __apply_ranges_to_regs(parent->device_ranges,
185 parent->num_device_ranges,
187 sdev->num_registers);
189 parent = parent->parent;
192 __apply_ranges_to_regs(sdev->bus->sbus_ranges,
193 sdev->bus->num_sbus_ranges,
195 sdev->num_registers);
197 for (i = 0; i < sdev->num_registers; i++) {
198 struct resource *res = &sdev->resource[i];
200 res->start = sdev->reg_addrs[i].phys_addr;
201 res->end = (res->start +
202 (unsigned long)sdev->reg_addrs[i].reg_size - 1UL);
203 res->flags = IORESOURCE_IO |
204 (sdev->reg_addrs[i].which_io & 0xff);
209 static void __init sbus_fixup_all_regs(struct sbus_dev *first_sdev)
211 struct sbus_dev *sdev;
213 for (sdev = first_sdev; sdev; sdev = sdev->next) {
215 sbus_fixup_all_regs(sdev->child);
216 __fixup_regs_sdev(sdev);
220 extern void register_proc_sparc_ioport(void);
221 extern void firetruck_init(void);
224 extern void sun4_dvma_init(void);
227 static int __init sbus_init(void)
229 int nd, this_sbus, sbus_devs, topnd, iommund;
230 unsigned int sbus_clock;
231 struct sbus_bus *sbus;
232 struct sbus_dev *this_dev;
233 int num_sbus = 0; /* How many did we find? */
235 #ifdef CONFIG_SPARC32
236 register_proc_sparc_ioport();
244 topnd = prom_getchild(prom_root_node);
246 /* Finding the first sbus is a special case... */
248 if(sparc_cpu_model == sun4u) {
249 nd = prom_searchsiblings(topnd, "sbus");
252 if (!pcic_present()) {
253 prom_printf("Neither SBUS nor PCI found.\n");
256 #ifdef CONFIG_SPARC64
262 prom_printf("YEEE, UltraSparc sbus not found\n");
266 } else if(sparc_cpu_model == sun4d) {
267 if((iommund = prom_searchsiblings(topnd, "io-unit")) == 0 ||
268 (nd = prom_getchild(iommund)) == 0 ||
269 (nd = prom_searchsiblings(nd, "sbi")) == 0) {
270 panic("sbi not found");
272 } else if((nd = prom_searchsiblings(topnd, "sbus")) == 0) {
273 if((iommund = prom_searchsiblings(topnd, "iommu")) == 0 ||
274 (nd = prom_getchild(iommund)) == 0 ||
275 (nd = prom_searchsiblings(nd, "sbus")) == 0) {
277 if (!pcic_present()) {
278 prom_printf("Neither SBUS nor PCI found.\n");
283 /* No reason to run further - the data access trap will occur. */
284 panic("sbus not found");
289 /* Ok, we've found the first one, allocate first SBus struct
290 * and place in chain.
292 sbus = sbus_root = kmalloc(sizeof(struct sbus_bus), GFP_ATOMIC);
294 sbus->prom_node = nd;
297 if(iommund && sparc_cpu_model != sun4u && sparc_cpu_model != sun4d)
298 iommu_init(iommund, sbus);
300 /* Loop until we find no more SBUS's */
302 #ifdef CONFIG_SPARC64
303 /* IOMMU hides inside SBUS/SYSIO prom node on Ultra. */
304 if(sparc_cpu_model == sun4u) {
305 extern void sbus_iommu_init(int prom_node, struct sbus_bus *sbus);
307 sbus_iommu_init(this_sbus, sbus);
309 #endif /* CONFIG_SPARC64 */
311 #ifdef CONFIG_SPARC32
312 if (sparc_cpu_model == sun4d)
313 iounit_init(this_sbus, iommund, sbus);
314 #endif /* CONFIG_SPARC32 */
315 printk("sbus%d: ", num_sbus);
316 sbus_clock = prom_getint(this_sbus, "clock-frequency");
318 sbus_clock = (25*1000*1000);
319 printk("Clock %d.%d MHz\n", (int) ((sbus_clock/1000)/1000),
320 (int) (((sbus_clock/1000)%1000 != 0) ?
321 (((sbus_clock/1000)%1000) + 1000) : 0));
323 prom_getstring(this_sbus, "name",
324 sbus->prom_name, sizeof(sbus->prom_name));
325 sbus->clock_freq = sbus_clock;
326 #ifdef CONFIG_SPARC32
327 if (sparc_cpu_model == sun4d) {
328 sbus->devid = prom_getint(iommund, "device-id");
329 sbus->board = prom_getint(iommund, "board#");
333 sbus_bus_ranges_init(iommund, sbus);
335 sbus_devs = prom_getchild(this_sbus);
337 sbus->devices = NULL;
341 sbus->devices = kmalloc(sizeof(struct sbus_dev), GFP_ATOMIC);
343 this_dev = sbus->devices;
344 this_dev->next = NULL;
346 this_dev->bus = sbus;
347 this_dev->parent = NULL;
348 fill_sbus_device(sbus_devs, this_dev);
350 /* Should we traverse for children? */
351 if(prom_getchild(sbus_devs)) {
352 /* Allocate device node */
353 this_dev->child = kmalloc(sizeof(struct sbus_dev),
356 this_dev->child->bus = sbus;
357 this_dev->child->next = NULL;
358 fill_sbus_device(prom_getchild(sbus_devs),
360 sbus_do_child_siblings(prom_getchild(sbus_devs),
365 this_dev->child = NULL;
368 while((sbus_devs = prom_getsibling(sbus_devs)) != 0) {
369 /* Allocate device node */
370 this_dev->next = kmalloc(sizeof(struct sbus_dev),
372 this_dev = this_dev->next;
373 this_dev->next = NULL;
376 this_dev->bus = sbus;
377 this_dev->parent = NULL;
378 fill_sbus_device(sbus_devs, this_dev);
380 /* Is there a child node hanging off of us? */
381 if(prom_getchild(sbus_devs)) {
382 /* Get new device struct */
383 this_dev->child = kmalloc(sizeof(struct sbus_dev),
386 this_dev->child->bus = sbus;
387 this_dev->child->next = NULL;
388 fill_sbus_device(prom_getchild(sbus_devs),
390 sbus_do_child_siblings(prom_getchild(sbus_devs),
395 this_dev->child = NULL;
399 /* Walk all devices and apply parent ranges. */
400 sbus_fixup_all_regs(sbus->devices);
405 if(sparc_cpu_model == sun4u) {
406 this_sbus = prom_getsibling(this_sbus);
409 this_sbus = prom_searchsiblings(this_sbus, "sbus");
410 } else if(sparc_cpu_model == sun4d) {
411 iommund = prom_getsibling(iommund);
414 iommund = prom_searchsiblings(iommund, "io-unit");
417 this_sbus = prom_searchsiblings(prom_getchild(iommund), "sbi");
419 this_sbus = prom_getsibling(this_sbus);
422 this_sbus = prom_searchsiblings(this_sbus, "sbus");
425 sbus->next = kmalloc(sizeof(struct sbus_bus), GFP_ATOMIC);
428 sbus->prom_node = this_sbus;
432 } /* while(this_sbus) */
434 if (sparc_cpu_model == sun4d) {
435 extern void sun4d_init_sbi_irq(void);
436 sun4d_init_sbi_irq();
439 #ifdef CONFIG_SPARC64
440 if (sparc_cpu_model == sun4u) {
444 #ifdef CONFIG_SUN_AUXIO
445 if (sparc_cpu_model == sun4u)
448 #ifdef CONFIG_SPARC64
449 if (sparc_cpu_model == sun4u) {
450 extern void clock_probe(void);
459 subsys_initcall(sbus_init);