Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[powerpc.git] / 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/config.h>
9 #include <linux/init.h>
10 #include <linux/pci.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/of_device.h>
18 #include <asm/bpp.h>
19 #include <asm/irq.h>
20
21 struct sbus_bus *sbus_root;
22
23 static void __init fill_sbus_device(struct device_node *dp, struct sbus_dev *sdev)
24 {
25         unsigned long base;
26         void *pval;
27         int len;
28
29         sdev->prom_node = dp->node;
30         strcpy(sdev->prom_name, dp->name);
31
32         pval = of_get_property(dp, "reg", &len);
33         sdev->num_registers = 0;
34         if (pval) {
35                 memcpy(sdev->reg_addrs, pval, len);
36
37                 sdev->num_registers =
38                         len / sizeof(struct linux_prom_registers);
39
40                 base = (unsigned long) sdev->reg_addrs[0].phys_addr;
41
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;
47         }
48
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);
55         }
56
57         sbus_fill_device_irq(sdev);
58
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);
66
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);
70 }
71
72 static void __init sbus_bus_ranges_init(struct device_node *dp, struct sbus_bus *sbus)
73 {
74         void *pval;
75         int len;
76
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);
83
84                 sbus_arch_bus_ranges_init(dp->parent, sbus);
85         }
86 }
87
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)
92 {
93         if (num_ranges) {
94                 int regnum;
95
96                 for (regnum = 0; regnum < num_regs; regnum++) {
97                         int rngnum;
98
99                         for (rngnum = 0; rngnum < num_ranges; rngnum++) {
100                                 if (regs[regnum].which_io == ranges[rngnum].ot_child_space)
101                                         break;
102                         }
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
110                                  */
111                                 return;
112                         }
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;
116                 }
117         }
118 }
119
120 static void __init __fixup_regs_sdev(struct sbus_dev *sdev)
121 {
122         if (sdev->num_registers != 0) {
123                 struct sbus_dev *parent = sdev->parent;
124                 int i;
125
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);
131
132                         parent = parent->parent;
133                 }
134
135                 __apply_ranges_to_regs(sdev->bus->sbus_ranges,
136                                        sdev->bus->num_sbus_ranges,
137                                        sdev->reg_addrs,
138                                        sdev->num_registers);
139
140                 for (i = 0; i < sdev->num_registers; i++) {
141                         struct resource *res = &sdev->resource[i];
142
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);
148                 }
149         }
150 }
151
152 static void __init sbus_fixup_all_regs(struct sbus_dev *first_sdev)
153 {
154         struct sbus_dev *sdev;
155
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);
160         }
161 }
162
163 /* We preserve the "probe order" of these bus and device lists to give
164  * the same ordering as the old code.
165  */
166 static void __init sbus_insert(struct sbus_bus *sbus, struct sbus_bus **root)
167 {
168         while (*root)
169                 root = &(*root)->next;
170         *root = sbus;
171         sbus->next = NULL;
172 }
173
174 static void __init sdev_insert(struct sbus_dev *sdev, struct sbus_dev **root)
175 {
176         while (*root)
177                 root = &(*root)->next;
178         *root = sdev;
179         sdev->next = NULL;
180 }
181
182 static void __init walk_children(struct device_node *dp, struct sbus_dev *parent, struct sbus_bus *sbus)
183 {
184         dp = dp->child;
185         while (dp) {
186                 struct sbus_dev *sdev;
187
188                 sdev = kzalloc(sizeof(struct sbus_dev), GFP_ATOMIC);
189                 if (sdev) {
190                         sdev_insert(sdev, &parent->child);
191
192                         sdev->bus = sbus;
193                         sdev->parent = parent;
194
195                         fill_sbus_device(dp, sdev);
196
197                         walk_children(dp, sdev, sbus);
198                 }
199                 dp = dp->sibling;
200         }
201 }
202
203 static void __init build_one_sbus(struct device_node *dp, int num_sbus)
204 {
205         struct sbus_bus *sbus;
206         unsigned int sbus_clock;
207         struct device_node *dev_dp;
208
209         sbus = kzalloc(sizeof(struct sbus_bus), GFP_ATOMIC);
210         if (!sbus)
211                 return;
212
213         sbus_insert(sbus, &sbus_root);
214         sbus->prom_node = dp->node;
215
216         sbus_setup_iommu(sbus, dp);
217
218         printk("sbus%d: ", num_sbus);
219
220         sbus_clock = of_getintprop_default(dp, "clock-frequency",
221                                            (25*1000*1000));
222         sbus->clock_freq = sbus_clock;
223
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));
227
228         strcpy(sbus->prom_name, dp->name);
229
230         sbus_setup_arch_props(sbus, dp);
231
232         sbus_bus_ranges_init(dp, sbus);
233
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);
238
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);
242
243         dev_dp = dp->child;
244         while (dev_dp) {
245                 struct sbus_dev *sdev;
246
247                 sdev = kzalloc(sizeof(struct sbus_dev), GFP_ATOMIC);
248                 if (sdev) {
249                         sdev_insert(sdev, &sbus->devices);
250
251                         sdev->bus = sbus;
252                         sdev->parent = NULL;
253                         fill_sbus_device(dev_dp, sdev);
254
255                         walk_children(dev_dp, sdev, sbus);
256                 }
257                 dev_dp = dev_dp->sibling;
258         }
259
260         sbus_fixup_all_regs(sbus->devices);
261
262         dvma_init(sbus);
263 }
264
265 static int __init sbus_init(void)
266 {
267         struct device_node *dp;
268         const char *sbus_name = "sbus";
269         int num_sbus = 0;
270
271         if (sbus_arch_preinit())
272                 return 0;
273
274         if (sparc_cpu_model == sun4d)
275                 sbus_name = "sbi";
276
277         for_each_node_by_name(dp, sbus_name) {
278                 build_one_sbus(dp, num_sbus);
279                 num_sbus++;
280
281         }
282
283         sbus_arch_postinit();
284
285         return 0;
286 }
287
288 subsys_initcall(sbus_init);