sparc: Propagate SBUS iommu archdata into real of_device objects.
[safe/jmp/linux-2.6] / drivers / sbus / sbus.c
1 /* sbus.c: SBus support routines.
2  *
3  * Copyright (C) 1995, 2006 David S. Miller (davem@davemloft.net)
4  */
5
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>
11
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/irq.h>
18
19 static ssize_t
20 show_sbusobppath_attr(struct device * dev, struct device_attribute * attr, char * buf)
21 {
22         struct sbus_dev *sbus;
23
24         sbus = to_sbus_device(dev);
25
26         return snprintf (buf, PAGE_SIZE, "%s\n", sbus->ofdev.node->full_name);
27 }
28
29 static DEVICE_ATTR(obppath, S_IRUSR | S_IRGRP | S_IROTH, show_sbusobppath_attr, NULL);
30
31 struct sbus_bus *sbus_root;
32
33 static void __init fill_sbus_device_iommu(struct sbus_dev *sdev)
34 {
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;
38
39         sbus = sdev->bus;
40         bus_sd = &sbus->ofdev.dev.archdata;
41
42         sd = &sdev->ofdev.dev.archdata;
43         sd->iommu = bus_sd->iommu;
44         sd->stc = bus_sd->stc;
45
46         sd = &op->dev.archdata;
47         sd->iommu = bus_sd->iommu;
48         sd->stc = bus_sd->stc;
49 }
50
51 static void __init fill_sbus_device(struct device_node *dp, struct sbus_dev *sdev)
52 {
53         struct dev_archdata *sd;
54         unsigned long base;
55         const void *pval;
56         int len, err;
57
58         sdev->prom_node = dp->node;
59         strcpy(sdev->prom_name, dp->name);
60
61         pval = of_get_property(dp, "reg", &len);
62         sdev->num_registers = 0;
63         if (pval) {
64                 memcpy(sdev->reg_addrs, pval, len);
65
66                 sdev->num_registers =
67                         len / sizeof(struct linux_prom_registers);
68
69                 base = (unsigned long) sdev->reg_addrs[0].phys_addr;
70
71                 /* Compute the slot number. */
72                 if (base >= SUN_SBUS_BVADDR && sparc_cpu_model == sun4m)
73                         sdev->slot = sbus_dev_slot(base);
74                 else
75                         sdev->slot = sdev->reg_addrs[0].which_io;
76         }
77
78         pval = of_get_property(dp, "ranges", &len);
79         sdev->num_device_ranges = 0;
80         if (pval) {
81                 memcpy(sdev->device_ranges, pval, len);
82                 sdev->num_device_ranges =
83                         len / sizeof(struct linux_prom_ranges);
84         }
85
86         sbus_fill_device_irq(sdev);
87
88         sd = &sdev->ofdev.dev.archdata;
89         sd->prom_node = dp;
90         sd->op = &sdev->ofdev;
91
92         sdev->ofdev.node = dp;
93         if (sdev->parent)
94                 sdev->ofdev.dev.parent = &sdev->parent->ofdev.dev;
95         else
96                 sdev->ofdev.dev.parent = &sdev->bus->ofdev.dev;
97         sdev->ofdev.dev.bus = &sbus_bus_type;
98         dev_set_name(&sdev->ofdev.dev, "sbus[%08x]", dp->node);
99
100         if (of_device_register(&sdev->ofdev) != 0)
101                 printk(KERN_DEBUG "sbus: device registration error for %s!\n",
102                        dp->path_component_name);
103
104         /* WE HAVE BEEN INVADED BY ALIENS! */
105         err = sysfs_create_file(&sdev->ofdev.dev.kobj, &dev_attr_obppath.attr);
106
107         fill_sbus_device_iommu(sdev);
108 }
109
110 static void __init sbus_bus_ranges_init(struct device_node *dp, struct sbus_bus *sbus)
111 {
112         const void *pval;
113         int len;
114
115         pval = of_get_property(dp, "ranges", &len);
116         sbus->num_sbus_ranges = 0;
117         if (pval) {
118                 memcpy(sbus->sbus_ranges, pval, len);
119                 sbus->num_sbus_ranges =
120                         len / sizeof(struct linux_prom_ranges);
121
122                 sbus_arch_bus_ranges_init(dp->parent, sbus);
123         }
124 }
125
126 static void __init __apply_ranges_to_regs(struct linux_prom_ranges *ranges,
127                                           int num_ranges,
128                                           struct linux_prom_registers *regs,
129                                           int num_regs)
130 {
131         if (num_ranges) {
132                 int regnum;
133
134                 for (regnum = 0; regnum < num_regs; regnum++) {
135                         int rngnum;
136
137                         for (rngnum = 0; rngnum < num_ranges; rngnum++) {
138                                 if (regs[regnum].which_io == ranges[rngnum].ot_child_space)
139                                         break;
140                         }
141                         if (rngnum == num_ranges) {
142                                 /* We used to flag this as an error.  Actually
143                                  * some devices do not report the regs as we expect.
144                                  * For example, see SUNW,pln device.  In that case
145                                  * the reg property is in a format internal to that
146                                  * node, ie. it is not in the SBUS register space
147                                  * per se. -DaveM
148                                  */
149                                 return;
150                         }
151                         regs[regnum].which_io = ranges[rngnum].ot_parent_space;
152                         regs[regnum].phys_addr -= ranges[rngnum].ot_child_base;
153                         regs[regnum].phys_addr += ranges[rngnum].ot_parent_base;
154                 }
155         }
156 }
157
158 static void __init __fixup_regs_sdev(struct sbus_dev *sdev)
159 {
160         if (sdev->num_registers != 0) {
161                 struct sbus_dev *parent = sdev->parent;
162                 int i;
163
164                 while (parent != NULL) {
165                         __apply_ranges_to_regs(parent->device_ranges,
166                                                parent->num_device_ranges,
167                                                sdev->reg_addrs,
168                                                sdev->num_registers);
169
170                         parent = parent->parent;
171                 }
172
173                 __apply_ranges_to_regs(sdev->bus->sbus_ranges,
174                                        sdev->bus->num_sbus_ranges,
175                                        sdev->reg_addrs,
176                                        sdev->num_registers);
177
178                 for (i = 0; i < sdev->num_registers; i++) {
179                         struct resource *res = &sdev->resource[i];
180
181                         res->start = sdev->reg_addrs[i].phys_addr;
182                         res->end = (res->start +
183                                     (unsigned long)sdev->reg_addrs[i].reg_size - 1UL);
184                         res->flags = IORESOURCE_IO |
185                                 (sdev->reg_addrs[i].which_io & 0xff);
186                 }
187         }
188 }
189
190 static void __init sbus_fixup_all_regs(struct sbus_dev *first_sdev)
191 {
192         struct sbus_dev *sdev;
193
194         for (sdev = first_sdev; sdev; sdev = sdev->next) {
195                 if (sdev->child)
196                         sbus_fixup_all_regs(sdev->child);
197                 __fixup_regs_sdev(sdev);
198         }
199 }
200
201 /* We preserve the "probe order" of these bus and device lists to give
202  * the same ordering as the old code.
203  */
204 static void __init sbus_insert(struct sbus_bus *sbus, struct sbus_bus **root)
205 {
206         while (*root)
207                 root = &(*root)->next;
208         *root = sbus;
209         sbus->next = NULL;
210 }
211
212 static void __init sdev_insert(struct sbus_dev *sdev, struct sbus_dev **root)
213 {
214         while (*root)
215                 root = &(*root)->next;
216         *root = sdev;
217         sdev->next = NULL;
218 }
219
220 static void __init walk_children(struct device_node *dp, struct sbus_dev *parent, struct sbus_bus *sbus)
221 {
222         dp = dp->child;
223         while (dp) {
224                 struct sbus_dev *sdev;
225
226                 sdev = kzalloc(sizeof(struct sbus_dev), GFP_ATOMIC);
227                 if (sdev) {
228                         sdev_insert(sdev, &parent->child);
229
230                         sdev->bus = sbus;
231                         sdev->parent = parent;
232
233                         fill_sbus_device(dp, sdev);
234
235                         walk_children(dp, sdev, sbus);
236                 }
237                 dp = dp->sibling;
238         }
239 }
240
241 static void __init build_one_sbus(struct device_node *dp, int num_sbus)
242 {
243         struct sbus_bus *sbus;
244         unsigned int sbus_clock;
245         struct device_node *dev_dp;
246
247         sbus = kzalloc(sizeof(struct sbus_bus), GFP_ATOMIC);
248         if (!sbus)
249                 return;
250
251         sbus_insert(sbus, &sbus_root);
252         sbus->prom_node = dp->node;
253
254         sbus_setup_iommu(sbus, dp);
255
256         printk("sbus%d: ", num_sbus);
257
258         sbus_clock = of_getintprop_default(dp, "clock-frequency",
259                                            (25*1000*1000));
260         sbus->clock_freq = sbus_clock;
261
262         printk("Clock %d.%d MHz\n", (int) ((sbus_clock/1000)/1000),
263                (int) (((sbus_clock/1000)%1000 != 0) ? 
264                       (((sbus_clock/1000)%1000) + 1000) : 0));
265
266         strcpy(sbus->prom_name, dp->name);
267
268         sbus_setup_arch_props(sbus, dp);
269
270         sbus_bus_ranges_init(dp, sbus);
271
272         sbus->ofdev.node = dp;
273         sbus->ofdev.dev.parent = NULL;
274         sbus->ofdev.dev.bus = &sbus_bus_type;
275         dev_set_name(&sbus->ofdev.dev, "sbus%d", num_sbus);
276
277         if (of_device_register(&sbus->ofdev) != 0)
278                 printk(KERN_DEBUG "sbus: device registration error for %s!\n",
279                        dev_name(&sbus->ofdev.dev));
280
281         dev_dp = dp->child;
282         while (dev_dp) {
283                 struct sbus_dev *sdev;
284
285                 sdev = kzalloc(sizeof(struct sbus_dev), GFP_ATOMIC);
286                 if (sdev) {
287                         sdev_insert(sdev, &sbus->devices);
288
289                         sdev->bus = sbus;
290                         sdev->parent = NULL;
291                         sdev->ofdev.dev.archdata.iommu =
292                                 sbus->ofdev.dev.archdata.iommu;
293                         sdev->ofdev.dev.archdata.stc =
294                                 sbus->ofdev.dev.archdata.stc;
295
296                         fill_sbus_device(dev_dp, sdev);
297
298                         walk_children(dev_dp, sdev, sbus);
299                 }
300                 dev_dp = dev_dp->sibling;
301         }
302
303         sbus_fixup_all_regs(sbus->devices);
304 }
305
306 static int __init sbus_init(void)
307 {
308         struct device_node *dp;
309         const char *sbus_name = "sbus";
310         int num_sbus = 0;
311
312         if (sbus_arch_preinit())
313                 return 0;
314
315         if (sparc_cpu_model == sun4d)
316                 sbus_name = "sbi";
317
318         for_each_node_by_name(dp, sbus_name) {
319                 build_one_sbus(dp, num_sbus);
320                 num_sbus++;
321
322         }
323
324         sbus_arch_postinit();
325
326         return 0;
327 }
328
329 subsys_initcall(sbus_init);