[SBUS]: Rewrite and plug into of_device framework.

I severely apologize, I was still learning how to program
in C when I wrote this stuff 10 years ago...

Signed-off-by: David S. Miller <davem@davemloft.net>
diff --git a/drivers/sbus/sbus.c b/drivers/sbus/sbus.c
index 6e9b260..387a6aa 100644
--- a/drivers/sbus/sbus.c
+++ b/drivers/sbus/sbus.c
@@ -13,32 +13,29 @@
 #include <asm/sbus.h>
 #include <asm/dma.h>
 #include <asm/oplib.h>
+#include <asm/prom.h>
+#include <asm/of_device.h>
 #include <asm/bpp.h>
 #include <asm/irq.h>
 
 struct sbus_bus *sbus_root;
 
-#ifdef CONFIG_PCI
-extern int pcic_present(void);
-#endif
-
-static void __init fill_sbus_device(int prom_node, struct sbus_dev *sdev)
+static void __init fill_sbus_device(struct device_node *dp, struct sbus_dev *sdev)
 {
-	unsigned long address, base;
+	unsigned long base;
+	void *pval;
 	int len;
 
-	sdev->prom_node = prom_node;
-	prom_getstring(prom_node, "name",
-		       sdev->prom_name, sizeof(sdev->prom_name));
-	address = prom_getint(prom_node, "address");
-	len = prom_getproperty(prom_node, "reg",
-			       (char *) sdev->reg_addrs,
-			       sizeof(sdev->reg_addrs));
+	sdev->prom_node = dp->node;
+	strcpy(sdev->prom_name, dp->name);
+
+	pval = of_get_property(dp, "reg", &len);
 	sdev->num_registers = 0;
-	if (len != -1) {
+	if (pval) {
+		memcpy(sdev->reg_addrs, pval, len);
+
 		sdev->num_registers =
 			len / sizeof(struct linux_prom_registers);
-		sdev->ranges_applied = 0;
 
 		base = (unsigned long) sdev->reg_addrs[0].phys_addr;
 
@@ -49,97 +46,43 @@
 			sdev->slot = sdev->reg_addrs[0].which_io;
 	}
 
-	len = prom_getproperty(prom_node, "ranges",
-			       (char *)sdev->device_ranges,
-			       sizeof(sdev->device_ranges));
+	pval = of_get_property(dp, "ranges", &len);
 	sdev->num_device_ranges = 0;
-	if (len != -1)
+	if (pval) {
+		memcpy(sdev->device_ranges, pval, len);
 		sdev->num_device_ranges =
 			len / sizeof(struct linux_prom_ranges);
+	}
 
 	sbus_fill_device_irq(sdev);
+
+	sdev->ofdev.node = dp;
+	if (sdev->parent)
+		sdev->ofdev.dev.parent = &sdev->parent->ofdev.dev;
+	else
+		sdev->ofdev.dev.parent = &sdev->bus->ofdev.dev;
+	sdev->ofdev.dev.bus = &sbus_bus_type;
+	strcpy(sdev->ofdev.dev.bus_id, dp->path_component_name);
+
+	if (of_device_register(&sdev->ofdev) != 0)
+		printk(KERN_DEBUG "sbus: device registration error for %s!\n",
+		       sdev->ofdev.dev.bus_id);
 }
 
-/* This routine gets called from whoever needs the sbus first, to scan
- * the SBus device tree.  Currently it just prints out the devices
- * found on the bus and builds trees of SBUS structs and attached
- * devices.
- */
-
-extern void iommu_init(int iommu_node, struct sbus_bus *sbus);
-extern void iounit_init(int sbi_node, int iounit_node, struct sbus_bus *sbus);
-void sun4_init(void);
-#ifdef CONFIG_SUN_AUXIO
-extern void auxio_probe(void);
-#endif
-
-static void __init sbus_do_child_siblings(int start_node,
-					  struct sbus_dev *child,
-					  struct sbus_dev *parent,
-					  struct sbus_bus *sbus)
+static void __init sbus_bus_ranges_init(struct device_node *dp, struct sbus_bus *sbus)
 {
-	struct sbus_dev *this_dev = child;
-	int this_node = start_node;
-
-	/* Child already filled in, just need to traverse siblings. */
-	child->child = NULL;
-	child->parent = parent;
-	while((this_node = prom_getsibling(this_node)) != 0) {
-		this_dev->next = kmalloc(sizeof(struct sbus_dev), GFP_ATOMIC);
-		this_dev = this_dev->next;
-		this_dev->next = NULL;
-		this_dev->parent = parent;
-
-		this_dev->bus = sbus;
-		fill_sbus_device(this_node, this_dev);
-
-		if(prom_getchild(this_node)) {
-			this_dev->child = kmalloc(sizeof(struct sbus_dev),
-						  GFP_ATOMIC);
-			this_dev->child->bus = sbus;
-			this_dev->child->next = NULL;
-			fill_sbus_device(prom_getchild(this_node), this_dev->child);
-			sbus_do_child_siblings(prom_getchild(this_node),
-					       this_dev->child, this_dev, sbus);
-		} else {
-			this_dev->child = NULL;
-		}
-	}
-}
-
-/*
- * XXX This functions appears to be a distorted version of
- * prom_sbus_ranges_init(), with all sun4d stuff cut away.
- * Ask DaveM what is going on here, how is sun4d supposed to work... XXX
- */
-/* added back sun4d patch from Thomas Bogendoerfer - should be OK (crn) */
-
-static void __init sbus_bus_ranges_init(int parent_node, struct sbus_bus *sbus)
-{
+	void *pval;
 	int len;
 
-	len = prom_getproperty(sbus->prom_node, "ranges",
-			       (char *) sbus->sbus_ranges,
-			       sizeof(sbus->sbus_ranges));
-	if (len == -1 || len == 0) {
-		sbus->num_sbus_ranges = 0;
-		return;
-	}
-	sbus->num_sbus_ranges = len / sizeof(struct linux_prom_ranges);
-#ifdef CONFIG_SPARC32
-	if (sparc_cpu_model == sun4d) {
-		struct linux_prom_ranges iounit_ranges[PROMREG_MAX];
-		int num_iounit_ranges;
+	pval = of_get_property(dp, "ranges", &len);
+	sbus->num_sbus_ranges = 0;
+	if (pval) {
+		memcpy(sbus->sbus_ranges, pval, len);
+		sbus->num_sbus_ranges =
+			len / sizeof(struct linux_prom_ranges);
 
-		len = prom_getproperty(parent_node, "ranges",
-				       (char *) iounit_ranges,
-				       sizeof (iounit_ranges));
-		if (len != -1) {
-			num_iounit_ranges = (len/sizeof(struct linux_prom_ranges));
-			prom_adjust_ranges (sbus->sbus_ranges, sbus->num_sbus_ranges, iounit_ranges, num_iounit_ranges);
-		}
+		sbus_arch_bus_ranges_init(dp->parent, sbus);
 	}
-#endif
 }
 
 static void __init __apply_ranges_to_regs(struct linux_prom_ranges *ranges,
@@ -217,241 +160,127 @@
 	}
 }
 
-extern void register_proc_sparc_ioport(void);
-extern void firetruck_init(void);
+/* We preserve the "probe order" of these bus and device lists to give
+ * the same ordering as the old code.
+ */
+static void __init sbus_insert(struct sbus_bus *sbus, struct sbus_bus **root)
+{
+	while (*root)
+		root = &(*root)->next;
+	*root = sbus;
+	sbus->next = NULL;
+}
 
-#ifdef CONFIG_SUN4
-extern void sun4_dvma_init(void);
-#endif
+static void __init sdev_insert(struct sbus_dev *sdev, struct sbus_dev **root)
+{
+	while (*root)
+		root = &(*root)->next;
+	*root = sdev;
+	sdev->next = NULL;
+}
+
+static void __init walk_children(struct device_node *dp, struct sbus_dev *parent, struct sbus_bus *sbus)
+{
+	dp = dp->child;
+	while (dp) {
+		struct sbus_dev *sdev;
+
+		sdev = kzalloc(sizeof(struct sbus_dev), GFP_ATOMIC);
+		if (sdev) {
+			sdev_insert(sdev, &parent->child);
+
+			sdev->bus = sbus;
+			sdev->parent = parent;
+
+			fill_sbus_device(dp, sdev);
+
+			walk_children(dp, sdev, sbus);
+		}
+		dp = dp->sibling;
+	}
+}
+
+static void __init build_one_sbus(struct device_node *dp, int num_sbus)
+{
+	struct sbus_bus *sbus;
+	unsigned int sbus_clock;
+	struct device_node *dev_dp;
+
+	sbus = kzalloc(sizeof(struct sbus_bus), GFP_ATOMIC);
+	if (!sbus)
+		return;
+
+	sbus_insert(sbus, &sbus_root);
+	sbus->prom_node = dp->node;
+
+	sbus_setup_iommu(sbus, dp);
+
+	printk("sbus%d: ", num_sbus);
+
+	sbus_clock = of_getintprop_default(dp, "clock-frequency",
+					   (25*1000*1000));
+	sbus->clock_freq = sbus_clock;
+
+	printk("Clock %d.%d MHz\n", (int) ((sbus_clock/1000)/1000),
+	       (int) (((sbus_clock/1000)%1000 != 0) ? 
+		      (((sbus_clock/1000)%1000) + 1000) : 0));
+
+	strcpy(sbus->prom_name, dp->name);
+
+	sbus_setup_arch_props(sbus, dp);
+
+	sbus_bus_ranges_init(dp, sbus);
+
+	sbus->ofdev.node = dp;
+	sbus->ofdev.dev.parent = NULL;
+	sbus->ofdev.dev.bus = &sbus_bus_type;
+	strcpy(sbus->ofdev.dev.bus_id, dp->path_component_name);
+
+	if (of_device_register(&sbus->ofdev) != 0)
+		printk(KERN_DEBUG "sbus: device registration error for %s!\n",
+		       sbus->ofdev.dev.bus_id);
+
+	dev_dp = dp->child;
+	while (dev_dp) {
+		struct sbus_dev *sdev;
+
+		sdev = kzalloc(sizeof(struct sbus_dev), GFP_ATOMIC);
+		if (sdev) {
+			sdev_insert(sdev, &sbus->devices);
+
+			sdev->bus = sbus;
+			sdev->parent = NULL;
+			fill_sbus_device(dev_dp, sdev);
+
+			walk_children(dev_dp, sdev, sbus);
+		}
+		dev_dp = dev_dp->sibling;
+	}
+
+	sbus_fixup_all_regs(sbus->devices);
+
+	dvma_init(sbus);
+}
 
 static int __init sbus_init(void)
 {
-	int nd, this_sbus, sbus_devs, topnd, iommund;
-	unsigned int sbus_clock;
-	struct sbus_bus *sbus;
-	struct sbus_dev *this_dev;
-	int num_sbus = 0;  /* How many did we find? */
+	struct device_node *dp;
+	const char *sbus_name = "sbus";
+	int num_sbus = 0;
 
-#ifdef CONFIG_SPARC32
-	register_proc_sparc_ioport();
-#endif
+	if (sbus_arch_preinit())
+		return 0;
 
-#ifdef CONFIG_SUN4
-	sun4_dvma_init();
-	return 0;
-#endif
+	if (sparc_cpu_model == sun4d)
+		sbus_name = "sbi";
 
-	topnd = prom_getchild(prom_root_node);
-	
-	/* Finding the first sbus is a special case... */
-	iommund = 0;
-	if(sparc_cpu_model == sun4u) {
-		nd = prom_searchsiblings(topnd, "sbus");
-		if(nd == 0) {
-#ifdef CONFIG_PCI
-			if (!pcic_present()) {
-				prom_printf("Neither SBUS nor PCI found.\n");
-				prom_halt();
-			} else {
-#ifdef CONFIG_SPARC64
-				firetruck_init();
-#endif
-			}
-			return 0;
-#else
-			prom_printf("YEEE, UltraSparc sbus not found\n");
-			prom_halt();
-#endif
-		}
-	} else if(sparc_cpu_model == sun4d) {
-		if((iommund = prom_searchsiblings(topnd, "io-unit")) == 0 ||
-		   (nd = prom_getchild(iommund)) == 0 ||
-		   (nd = prom_searchsiblings(nd, "sbi")) == 0) {
-		   	panic("sbi not found");
-		}
-	} else if((nd = prom_searchsiblings(topnd, "sbus")) == 0) {
-		if((iommund = prom_searchsiblings(topnd, "iommu")) == 0 ||
-		   (nd = prom_getchild(iommund)) == 0 ||
-		   (nd = prom_searchsiblings(nd, "sbus")) == 0) {
-#ifdef CONFIG_PCI
-                        if (!pcic_present()) {
-                                prom_printf("Neither SBUS nor PCI found.\n");
-                                prom_halt();
-                        }
-                        return 0;
-#else
-			/* No reason to run further - the data access trap will occur. */
-			panic("sbus not found");
-#endif
-		}
-	}
-
-	/* Ok, we've found the first one, allocate first SBus struct
-	 * and place in chain.
-	 */
-	sbus = sbus_root = kmalloc(sizeof(struct sbus_bus), GFP_ATOMIC);
-	sbus->next = NULL;
-	sbus->prom_node = nd;
-	this_sbus = nd;
-
-	if(iommund && sparc_cpu_model != sun4u && sparc_cpu_model != sun4d)
-		iommu_init(iommund, sbus);
-
-	/* Loop until we find no more SBUS's */
-	while(this_sbus) {
-#ifdef CONFIG_SPARC64
-		/* IOMMU hides inside SBUS/SYSIO prom node on Ultra. */
-		if(sparc_cpu_model == sun4u) {
-			extern void sbus_iommu_init(int prom_node, struct sbus_bus *sbus);
-
-			sbus_iommu_init(this_sbus, sbus);
-		}
-#endif /* CONFIG_SPARC64 */
-
-#ifdef CONFIG_SPARC32
-		if (sparc_cpu_model == sun4d)
-			iounit_init(this_sbus, iommund, sbus);
-#endif /* CONFIG_SPARC32 */
-		printk("sbus%d: ", num_sbus);
-		sbus_clock = prom_getint(this_sbus, "clock-frequency");
-		if(sbus_clock == -1)
-			sbus_clock = (25*1000*1000);
-		printk("Clock %d.%d MHz\n", (int) ((sbus_clock/1000)/1000),
-		       (int) (((sbus_clock/1000)%1000 != 0) ? 
-			      (((sbus_clock/1000)%1000) + 1000) : 0));
-
-		prom_getstring(this_sbus, "name",
-			       sbus->prom_name, sizeof(sbus->prom_name));
-		sbus->clock_freq = sbus_clock;
-#ifdef CONFIG_SPARC32
-		if (sparc_cpu_model == sun4d) {
-			sbus->devid = prom_getint(iommund, "device-id");
-			sbus->board = prom_getint(iommund, "board#");
-		}
-#endif
-		
-		sbus_bus_ranges_init(iommund, sbus);
-
-		sbus_devs = prom_getchild(this_sbus);
-		if (!sbus_devs) {
-			sbus->devices = NULL;
-			goto next_bus;
-		}
-
-		sbus->devices = kmalloc(sizeof(struct sbus_dev), GFP_ATOMIC);
-
-		this_dev = sbus->devices;
-		this_dev->next = NULL;
-
-		this_dev->bus = sbus;
-		this_dev->parent = NULL;
-		fill_sbus_device(sbus_devs, this_dev);
-
-		/* Should we traverse for children? */
-		if(prom_getchild(sbus_devs)) {
-			/* Allocate device node */
-			this_dev->child = kmalloc(sizeof(struct sbus_dev),
-						  GFP_ATOMIC);
-			/* Fill it */
-			this_dev->child->bus = sbus;
-			this_dev->child->next = NULL;
-			fill_sbus_device(prom_getchild(sbus_devs),
-					 this_dev->child);
-			sbus_do_child_siblings(prom_getchild(sbus_devs),
-					       this_dev->child,
-					       this_dev,
-					       sbus);
-		} else {
-			this_dev->child = NULL;
-		}
-
-		while((sbus_devs = prom_getsibling(sbus_devs)) != 0) {
-			/* Allocate device node */
-			this_dev->next = kmalloc(sizeof(struct sbus_dev),
-						 GFP_ATOMIC);
-			this_dev = this_dev->next;
-			this_dev->next = NULL;
-
-			/* Fill it */
-			this_dev->bus = sbus;
-			this_dev->parent = NULL;
-			fill_sbus_device(sbus_devs, this_dev);
-
-			/* Is there a child node hanging off of us? */
-			if(prom_getchild(sbus_devs)) {
-				/* Get new device struct */
-				this_dev->child = kmalloc(sizeof(struct sbus_dev),
-							  GFP_ATOMIC);
-				/* Fill it */
-				this_dev->child->bus = sbus;
-				this_dev->child->next = NULL;
-				fill_sbus_device(prom_getchild(sbus_devs),
-						 this_dev->child);
-				sbus_do_child_siblings(prom_getchild(sbus_devs),
-						       this_dev->child,
-						       this_dev,
-						       sbus);
-			} else {
-				this_dev->child = NULL;
-			}
-		}
-
-		/* Walk all devices and apply parent ranges. */
-		sbus_fixup_all_regs(sbus->devices);
-
-		dvma_init(sbus);
-	next_bus:
+	for_each_node_by_name(dp, sbus_name) {
+		build_one_sbus(dp, num_sbus);
 		num_sbus++;
-		if(sparc_cpu_model == sun4u) {
-			this_sbus = prom_getsibling(this_sbus);
-			if(!this_sbus)
-				break;
-			this_sbus = prom_searchsiblings(this_sbus, "sbus");
-		} else if(sparc_cpu_model == sun4d) {
-			iommund = prom_getsibling(iommund);
-			if(!iommund)
-				break;
-			iommund = prom_searchsiblings(iommund, "io-unit");
-			if(!iommund)
-				break;
-			this_sbus = prom_searchsiblings(prom_getchild(iommund), "sbi");
-		} else {
-			this_sbus = prom_getsibling(this_sbus);
-			if(!this_sbus)
-				break;
-			this_sbus = prom_searchsiblings(this_sbus, "sbus");
-		}
-		if(this_sbus) {
-			sbus->next = kmalloc(sizeof(struct sbus_bus), GFP_ATOMIC);
-			sbus = sbus->next;
-			sbus->next = NULL;
-			sbus->prom_node = this_sbus;
-		} else {
-			break;
-		}
-	} /* while(this_sbus) */
 
-	if (sparc_cpu_model == sun4d) {
-		extern void sun4d_init_sbi_irq(void);
-		sun4d_init_sbi_irq();
 	}
-	
-#ifdef CONFIG_SPARC64
-	if (sparc_cpu_model == sun4u) {
-		firetruck_init();
-	}
-#endif
-#ifdef CONFIG_SUN_AUXIO
-	if (sparc_cpu_model == sun4u)
-		auxio_probe ();
-#endif
-#ifdef CONFIG_SPARC64
-	if (sparc_cpu_model == sun4u) {
-		extern void clock_probe(void);
 
-		clock_probe();
-	}
-#endif
+	sbus_arch_postinit();
 
 	return 0;
 }