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/init.h>
9 #include <linux/device.h>
10 #include <linux/of_device.h>
12 #include <asm/system.h>
15 #include <asm/oplib.h>
20 show_sbusobppath_attr(struct device * dev, struct device_attribute * attr, char * buf)
22 struct sbus_dev *sbus;
24 sbus = to_sbus_device(dev);
26 return snprintf (buf, PAGE_SIZE, "%s\n", sbus->ofdev.node->full_name);
29 static DEVICE_ATTR(obppath, S_IRUSR | S_IRGRP | S_IROTH, show_sbusobppath_attr, NULL);
31 struct sbus_bus *sbus_root;
33 static void __init fill_sbus_device_iommu(struct sbus_dev *sdev)
35 struct of_device *op = of_find_device_by_node(sdev->ofdev.node);
36 struct dev_archdata *sd, *bus_sd;
37 struct sbus_bus *sbus;
40 bus_sd = &sbus->ofdev.dev.archdata;
42 sd = &sdev->ofdev.dev.archdata;
43 sd->iommu = bus_sd->iommu;
44 sd->stc = bus_sd->stc;
46 sd = &op->dev.archdata;
47 sd->iommu = bus_sd->iommu;
48 sd->stc = bus_sd->stc;
51 static void __init fill_sbus_device(struct device_node *dp, struct sbus_dev *sdev)
53 struct dev_archdata *sd;
58 sdev->prom_node = dp->node;
59 strcpy(sdev->prom_name, dp->name);
61 pval = of_get_property(dp, "reg", &len);
62 sdev->num_registers = 0;
64 memcpy(sdev->reg_addrs, pval, len);
67 len / sizeof(struct linux_prom_registers);
69 base = (unsigned long) sdev->reg_addrs[0].phys_addr;
71 /* Compute the slot number. */
72 if (base >= SUN_SBUS_BVADDR && sparc_cpu_model == sun4m)
73 sdev->slot = sbus_dev_slot(base);
75 sdev->slot = sdev->reg_addrs[0].which_io;
78 pval = of_get_property(dp, "ranges", &len);
79 sdev->num_device_ranges = 0;
81 memcpy(sdev->device_ranges, pval, len);
82 sdev->num_device_ranges =
83 len / sizeof(struct linux_prom_ranges);
86 sd = &sdev->ofdev.dev.archdata;
88 sd->op = &sdev->ofdev;
90 sdev->ofdev.node = dp;
92 sdev->ofdev.dev.parent = &sdev->parent->ofdev.dev;
94 sdev->ofdev.dev.parent = &sdev->bus->ofdev.dev;
95 sdev->ofdev.dev.bus = &sbus_bus_type;
96 dev_set_name(&sdev->ofdev.dev, "sbus[%08x]", dp->node);
98 if (of_device_register(&sdev->ofdev) != 0)
99 printk(KERN_DEBUG "sbus: device registration error for %s!\n",
100 dp->path_component_name);
102 /* WE HAVE BEEN INVADED BY ALIENS! */
103 err = sysfs_create_file(&sdev->ofdev.dev.kobj, &dev_attr_obppath.attr);
105 fill_sbus_device_iommu(sdev);
108 static void __init sbus_bus_ranges_init(struct device_node *dp, struct sbus_bus *sbus)
113 pval = of_get_property(dp, "ranges", &len);
114 sbus->num_sbus_ranges = 0;
116 memcpy(sbus->sbus_ranges, pval, len);
117 sbus->num_sbus_ranges =
118 len / sizeof(struct linux_prom_ranges);
120 sbus_arch_bus_ranges_init(dp->parent, sbus);
124 static void __init __apply_ranges_to_regs(struct linux_prom_ranges *ranges,
126 struct linux_prom_registers *regs,
132 for (regnum = 0; regnum < num_regs; regnum++) {
135 for (rngnum = 0; rngnum < num_ranges; rngnum++) {
136 if (regs[regnum].which_io == ranges[rngnum].ot_child_space)
139 if (rngnum == num_ranges) {
140 /* We used to flag this as an error. Actually
141 * some devices do not report the regs as we expect.
142 * For example, see SUNW,pln device. In that case
143 * the reg property is in a format internal to that
144 * node, ie. it is not in the SBUS register space
149 regs[regnum].which_io = ranges[rngnum].ot_parent_space;
150 regs[regnum].phys_addr -= ranges[rngnum].ot_child_base;
151 regs[regnum].phys_addr += ranges[rngnum].ot_parent_base;
156 static void __init __fixup_regs_sdev(struct sbus_dev *sdev)
158 if (sdev->num_registers != 0) {
159 struct sbus_dev *parent = sdev->parent;
162 while (parent != NULL) {
163 __apply_ranges_to_regs(parent->device_ranges,
164 parent->num_device_ranges,
166 sdev->num_registers);
168 parent = parent->parent;
171 __apply_ranges_to_regs(sdev->bus->sbus_ranges,
172 sdev->bus->num_sbus_ranges,
174 sdev->num_registers);
176 for (i = 0; i < sdev->num_registers; i++) {
177 struct resource *res = &sdev->resource[i];
179 res->start = sdev->reg_addrs[i].phys_addr;
180 res->end = (res->start +
181 (unsigned long)sdev->reg_addrs[i].reg_size - 1UL);
182 res->flags = IORESOURCE_IO |
183 (sdev->reg_addrs[i].which_io & 0xff);
188 static void __init sbus_fixup_all_regs(struct sbus_dev *first_sdev)
190 struct sbus_dev *sdev;
192 for (sdev = first_sdev; sdev; sdev = sdev->next) {
194 sbus_fixup_all_regs(sdev->child);
195 __fixup_regs_sdev(sdev);
199 /* We preserve the "probe order" of these bus and device lists to give
200 * the same ordering as the old code.
202 static void __init sbus_insert(struct sbus_bus *sbus, struct sbus_bus **root)
205 root = &(*root)->next;
210 static void __init sdev_insert(struct sbus_dev *sdev, struct sbus_dev **root)
213 root = &(*root)->next;
218 static void __init walk_children(struct device_node *dp, struct sbus_dev *parent, struct sbus_bus *sbus)
222 struct sbus_dev *sdev;
224 sdev = kzalloc(sizeof(struct sbus_dev), GFP_ATOMIC);
226 sdev_insert(sdev, &parent->child);
229 sdev->parent = parent;
231 fill_sbus_device(dp, sdev);
233 walk_children(dp, sdev, sbus);
239 static void __init build_one_sbus(struct device_node *dp, int num_sbus)
241 struct sbus_bus *sbus;
242 unsigned int sbus_clock;
243 struct device_node *dev_dp;
245 sbus = kzalloc(sizeof(struct sbus_bus), GFP_ATOMIC);
249 sbus_insert(sbus, &sbus_root);
250 sbus->prom_node = dp->node;
252 sbus_setup_iommu(sbus, dp);
254 printk("sbus%d: ", num_sbus);
256 sbus_clock = of_getintprop_default(dp, "clock-frequency",
258 sbus->clock_freq = sbus_clock;
260 printk("Clock %d.%d MHz\n", (int) ((sbus_clock/1000)/1000),
261 (int) (((sbus_clock/1000)%1000 != 0) ?
262 (((sbus_clock/1000)%1000) + 1000) : 0));
264 strcpy(sbus->prom_name, dp->name);
266 sbus_bus_ranges_init(dp, sbus);
268 sbus->ofdev.node = dp;
269 sbus->ofdev.dev.parent = NULL;
270 sbus->ofdev.dev.bus = &sbus_bus_type;
271 dev_set_name(&sbus->ofdev.dev, "sbus%d", num_sbus);
273 if (of_device_register(&sbus->ofdev) != 0)
274 printk(KERN_DEBUG "sbus: device registration error for %s!\n",
275 dev_name(&sbus->ofdev.dev));
279 struct sbus_dev *sdev;
281 sdev = kzalloc(sizeof(struct sbus_dev), GFP_ATOMIC);
283 sdev_insert(sdev, &sbus->devices);
287 sdev->ofdev.dev.archdata.iommu =
288 sbus->ofdev.dev.archdata.iommu;
289 sdev->ofdev.dev.archdata.stc =
290 sbus->ofdev.dev.archdata.stc;
292 fill_sbus_device(dev_dp, sdev);
294 walk_children(dev_dp, sdev, sbus);
296 dev_dp = dev_dp->sibling;
299 sbus_fixup_all_regs(sbus->devices);
302 static int __init sbus_init(void)
304 struct device_node *dp;
305 const char *sbus_name = "sbus";
308 if (sbus_arch_preinit())
311 if (sparc_cpu_model == sun4d)
314 for_each_node_by_name(dp, sbus_name) {
315 build_one_sbus(dp, num_sbus);
320 sbus_arch_postinit();
325 subsys_initcall(sbus_init);