[PATCH] pgdat allocation for new node add (generic alloc node_data)
[linux-2.6/x86.git] / drivers / sbus / sbus.c
blob387a6aa8c020cf8746c4f8a8807d803f3499930a
1 /* sbus.c: SBus support routines.
3 * Copyright (C) 1995, 2006 David S. Miller (davem@davemloft.net)
4 */
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>
13 #include <asm/sbus.h>
14 #include <asm/dma.h>
15 #include <asm/oplib.h>
16 #include <asm/prom.h>
17 #include <asm/of_device.h>
18 #include <asm/bpp.h>
19 #include <asm/irq.h>
21 struct sbus_bus *sbus_root;
23 static void __init fill_sbus_device(struct device_node *dp, struct sbus_dev *sdev)
25 unsigned long base;
26 void *pval;
27 int len;
29 sdev->prom_node = dp->node;
30 strcpy(sdev->prom_name, dp->name);
32 pval = of_get_property(dp, "reg", &len);
33 sdev->num_registers = 0;
34 if (pval) {
35 memcpy(sdev->reg_addrs, pval, len);
37 sdev->num_registers =
38 len / sizeof(struct linux_prom_registers);
40 base = (unsigned long) sdev->reg_addrs[0].phys_addr;
42 /* Compute the slot number. */
43 if (base >= SUN_SBUS_BVADDR && sparc_cpu_model == sun4m)
44 sdev->slot = sbus_dev_slot(base);
45 else
46 sdev->slot = sdev->reg_addrs[0].which_io;
49 pval = of_get_property(dp, "ranges", &len);
50 sdev->num_device_ranges = 0;
51 if (pval) {
52 memcpy(sdev->device_ranges, pval, len);
53 sdev->num_device_ranges =
54 len / sizeof(struct linux_prom_ranges);
57 sbus_fill_device_irq(sdev);
59 sdev->ofdev.node = dp;
60 if (sdev->parent)
61 sdev->ofdev.dev.parent = &sdev->parent->ofdev.dev;
62 else
63 sdev->ofdev.dev.parent = &sdev->bus->ofdev.dev;
64 sdev->ofdev.dev.bus = &sbus_bus_type;
65 strcpy(sdev->ofdev.dev.bus_id, dp->path_component_name);
67 if (of_device_register(&sdev->ofdev) != 0)
68 printk(KERN_DEBUG "sbus: device registration error for %s!\n",
69 sdev->ofdev.dev.bus_id);
72 static void __init sbus_bus_ranges_init(struct device_node *dp, struct sbus_bus *sbus)
74 void *pval;
75 int len;
77 pval = of_get_property(dp, "ranges", &len);
78 sbus->num_sbus_ranges = 0;
79 if (pval) {
80 memcpy(sbus->sbus_ranges, pval, len);
81 sbus->num_sbus_ranges =
82 len / sizeof(struct linux_prom_ranges);
84 sbus_arch_bus_ranges_init(dp->parent, sbus);
88 static void __init __apply_ranges_to_regs(struct linux_prom_ranges *ranges,
89 int num_ranges,
90 struct linux_prom_registers *regs,
91 int num_regs)
93 if (num_ranges) {
94 int regnum;
96 for (regnum = 0; regnum < num_regs; regnum++) {
97 int rngnum;
99 for (rngnum = 0; rngnum < num_ranges; rngnum++) {
100 if (regs[regnum].which_io == ranges[rngnum].ot_child_space)
101 break;
103 if (rngnum == num_ranges) {
104 /* We used to flag this as an error. Actually
105 * some devices do not report the regs as we expect.
106 * For example, see SUNW,pln device. In that case
107 * the reg property is in a format internal to that
108 * node, ie. it is not in the SBUS register space
109 * per se. -DaveM
111 return;
113 regs[regnum].which_io = ranges[rngnum].ot_parent_space;
114 regs[regnum].phys_addr -= ranges[rngnum].ot_child_base;
115 regs[regnum].phys_addr += ranges[rngnum].ot_parent_base;
120 static void __init __fixup_regs_sdev(struct sbus_dev *sdev)
122 if (sdev->num_registers != 0) {
123 struct sbus_dev *parent = sdev->parent;
124 int i;
126 while (parent != NULL) {
127 __apply_ranges_to_regs(parent->device_ranges,
128 parent->num_device_ranges,
129 sdev->reg_addrs,
130 sdev->num_registers);
132 parent = parent->parent;
135 __apply_ranges_to_regs(sdev->bus->sbus_ranges,
136 sdev->bus->num_sbus_ranges,
137 sdev->reg_addrs,
138 sdev->num_registers);
140 for (i = 0; i < sdev->num_registers; i++) {
141 struct resource *res = &sdev->resource[i];
143 res->start = sdev->reg_addrs[i].phys_addr;
144 res->end = (res->start +
145 (unsigned long)sdev->reg_addrs[i].reg_size - 1UL);
146 res->flags = IORESOURCE_IO |
147 (sdev->reg_addrs[i].which_io & 0xff);
152 static void __init sbus_fixup_all_regs(struct sbus_dev *first_sdev)
154 struct sbus_dev *sdev;
156 for (sdev = first_sdev; sdev; sdev = sdev->next) {
157 if (sdev->child)
158 sbus_fixup_all_regs(sdev->child);
159 __fixup_regs_sdev(sdev);
163 /* We preserve the "probe order" of these bus and device lists to give
164 * the same ordering as the old code.
166 static void __init sbus_insert(struct sbus_bus *sbus, struct sbus_bus **root)
168 while (*root)
169 root = &(*root)->next;
170 *root = sbus;
171 sbus->next = NULL;
174 static void __init sdev_insert(struct sbus_dev *sdev, struct sbus_dev **root)
176 while (*root)
177 root = &(*root)->next;
178 *root = sdev;
179 sdev->next = NULL;
182 static void __init walk_children(struct device_node *dp, struct sbus_dev *parent, struct sbus_bus *sbus)
184 dp = dp->child;
185 while (dp) {
186 struct sbus_dev *sdev;
188 sdev = kzalloc(sizeof(struct sbus_dev), GFP_ATOMIC);
189 if (sdev) {
190 sdev_insert(sdev, &parent->child);
192 sdev->bus = sbus;
193 sdev->parent = parent;
195 fill_sbus_device(dp, sdev);
197 walk_children(dp, sdev, sbus);
199 dp = dp->sibling;
203 static void __init build_one_sbus(struct device_node *dp, int num_sbus)
205 struct sbus_bus *sbus;
206 unsigned int sbus_clock;
207 struct device_node *dev_dp;
209 sbus = kzalloc(sizeof(struct sbus_bus), GFP_ATOMIC);
210 if (!sbus)
211 return;
213 sbus_insert(sbus, &sbus_root);
214 sbus->prom_node = dp->node;
216 sbus_setup_iommu(sbus, dp);
218 printk("sbus%d: ", num_sbus);
220 sbus_clock = of_getintprop_default(dp, "clock-frequency",
221 (25*1000*1000));
222 sbus->clock_freq = sbus_clock;
224 printk("Clock %d.%d MHz\n", (int) ((sbus_clock/1000)/1000),
225 (int) (((sbus_clock/1000)%1000 != 0) ?
226 (((sbus_clock/1000)%1000) + 1000) : 0));
228 strcpy(sbus->prom_name, dp->name);
230 sbus_setup_arch_props(sbus, dp);
232 sbus_bus_ranges_init(dp, sbus);
234 sbus->ofdev.node = dp;
235 sbus->ofdev.dev.parent = NULL;
236 sbus->ofdev.dev.bus = &sbus_bus_type;
237 strcpy(sbus->ofdev.dev.bus_id, dp->path_component_name);
239 if (of_device_register(&sbus->ofdev) != 0)
240 printk(KERN_DEBUG "sbus: device registration error for %s!\n",
241 sbus->ofdev.dev.bus_id);
243 dev_dp = dp->child;
244 while (dev_dp) {
245 struct sbus_dev *sdev;
247 sdev = kzalloc(sizeof(struct sbus_dev), GFP_ATOMIC);
248 if (sdev) {
249 sdev_insert(sdev, &sbus->devices);
251 sdev->bus = sbus;
252 sdev->parent = NULL;
253 fill_sbus_device(dev_dp, sdev);
255 walk_children(dev_dp, sdev, sbus);
257 dev_dp = dev_dp->sibling;
260 sbus_fixup_all_regs(sbus->devices);
262 dvma_init(sbus);
265 static int __init sbus_init(void)
267 struct device_node *dp;
268 const char *sbus_name = "sbus";
269 int num_sbus = 0;
271 if (sbus_arch_preinit())
272 return 0;
274 if (sparc_cpu_model == sun4d)
275 sbus_name = "sbi";
277 for_each_node_by_name(dp, sbus_name) {
278 build_one_sbus(dp, num_sbus);
279 num_sbus++;
283 sbus_arch_postinit();
285 return 0;
288 subsys_initcall(sbus_init);