[SBUS]: Start cleaning up generic sbus support layer.
[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/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/bpp.h>
17 #include <asm/irq.h>
18
19 struct sbus_bus *sbus_root;
20
21 #ifdef CONFIG_PCI
22 extern int pcic_present(void);
23 #endif
24
25 static void __init fill_sbus_device(int prom_node, struct sbus_dev *sdev)
26 {
27         unsigned long address, base;
28         int len;
29
30         sdev->prom_node = prom_node;
31         prom_getstring(prom_node, "name",
32                        sdev->prom_name, sizeof(sdev->prom_name));
33         address = prom_getint(prom_node, "address");
34         len = prom_getproperty(prom_node, "reg",
35                                (char *) sdev->reg_addrs,
36                                sizeof(sdev->reg_addrs));
37         sdev->num_registers = 0;
38         if (len != -1) {
39                 sdev->num_registers =
40                         len / sizeof(struct linux_prom_registers);
41                 sdev->ranges_applied = 0;
42
43                 base = (unsigned long) sdev->reg_addrs[0].phys_addr;
44
45                 /* Compute the slot number. */
46                 if (base >= SUN_SBUS_BVADDR && sparc_cpu_model == sun4m)
47                         sdev->slot = sbus_dev_slot(base);
48                 else
49                         sdev->slot = sdev->reg_addrs[0].which_io;
50         }
51
52         len = prom_getproperty(prom_node, "ranges",
53                                (char *)sdev->device_ranges,
54                                sizeof(sdev->device_ranges));
55         sdev->num_device_ranges = 0;
56         if (len != -1)
57                 sdev->num_device_ranges =
58                         len / sizeof(struct linux_prom_ranges);
59
60         sbus_fill_device_irq(sdev);
61 }
62
63 /* This routine gets called from whoever needs the sbus first, to scan
64  * the SBus device tree.  Currently it just prints out the devices
65  * found on the bus and builds trees of SBUS structs and attached
66  * devices.
67  */
68
69 extern void iommu_init(int iommu_node, struct sbus_bus *sbus);
70 extern void iounit_init(int sbi_node, int iounit_node, struct sbus_bus *sbus);
71 void sun4_init(void);
72 #ifdef CONFIG_SUN_AUXIO
73 extern void auxio_probe(void);
74 #endif
75
76 static void __init sbus_do_child_siblings(int start_node,
77                                           struct sbus_dev *child,
78                                           struct sbus_dev *parent,
79                                           struct sbus_bus *sbus)
80 {
81         struct sbus_dev *this_dev = child;
82         int this_node = start_node;
83
84         /* Child already filled in, just need to traverse siblings. */
85         child->child = NULL;
86         child->parent = parent;
87         while((this_node = prom_getsibling(this_node)) != 0) {
88                 this_dev->next = kmalloc(sizeof(struct sbus_dev), GFP_ATOMIC);
89                 this_dev = this_dev->next;
90                 this_dev->next = NULL;
91                 this_dev->parent = parent;
92
93                 this_dev->bus = sbus;
94                 fill_sbus_device(this_node, this_dev);
95
96                 if(prom_getchild(this_node)) {
97                         this_dev->child = kmalloc(sizeof(struct sbus_dev),
98                                                   GFP_ATOMIC);
99                         this_dev->child->bus = sbus;
100                         this_dev->child->next = NULL;
101                         fill_sbus_device(prom_getchild(this_node), this_dev->child);
102                         sbus_do_child_siblings(prom_getchild(this_node),
103                                                this_dev->child, this_dev, sbus);
104                 } else {
105                         this_dev->child = NULL;
106                 }
107         }
108 }
109
110 /*
111  * XXX This functions appears to be a distorted version of
112  * prom_sbus_ranges_init(), with all sun4d stuff cut away.
113  * Ask DaveM what is going on here, how is sun4d supposed to work... XXX
114  */
115 /* added back sun4d patch from Thomas Bogendoerfer - should be OK (crn) */
116
117 static void __init sbus_bus_ranges_init(int parent_node, struct sbus_bus *sbus)
118 {
119         int len;
120
121         len = prom_getproperty(sbus->prom_node, "ranges",
122                                (char *) sbus->sbus_ranges,
123                                sizeof(sbus->sbus_ranges));
124         if (len == -1 || len == 0) {
125                 sbus->num_sbus_ranges = 0;
126                 return;
127         }
128         sbus->num_sbus_ranges = len / sizeof(struct linux_prom_ranges);
129 #ifdef CONFIG_SPARC32
130         if (sparc_cpu_model == sun4d) {
131                 struct linux_prom_ranges iounit_ranges[PROMREG_MAX];
132                 int num_iounit_ranges;
133
134                 len = prom_getproperty(parent_node, "ranges",
135                                        (char *) iounit_ranges,
136                                        sizeof (iounit_ranges));
137                 if (len != -1) {
138                         num_iounit_ranges = (len/sizeof(struct linux_prom_ranges));
139                         prom_adjust_ranges (sbus->sbus_ranges, sbus->num_sbus_ranges, iounit_ranges, num_iounit_ranges);
140                 }
141         }
142 #endif
143 }
144
145 static void __init __apply_ranges_to_regs(struct linux_prom_ranges *ranges,
146                                           int num_ranges,
147                                           struct linux_prom_registers *regs,
148                                           int num_regs)
149 {
150         if (num_ranges) {
151                 int regnum;
152
153                 for (regnum = 0; regnum < num_regs; regnum++) {
154                         int rngnum;
155
156                         for (rngnum = 0; rngnum < num_ranges; rngnum++) {
157                                 if (regs[regnum].which_io == ranges[rngnum].ot_child_space)
158                                         break;
159                         }
160                         if (rngnum == num_ranges) {
161                                 /* We used to flag this as an error.  Actually
162                                  * some devices do not report the regs as we expect.
163                                  * For example, see SUNW,pln device.  In that case
164                                  * the reg property is in a format internal to that
165                                  * node, ie. it is not in the SBUS register space
166                                  * per se. -DaveM
167                                  */
168                                 return;
169                         }
170                         regs[regnum].which_io = ranges[rngnum].ot_parent_space;
171                         regs[regnum].phys_addr -= ranges[rngnum].ot_child_base;
172                         regs[regnum].phys_addr += ranges[rngnum].ot_parent_base;
173                 }
174         }
175 }
176
177 static void __init __fixup_regs_sdev(struct sbus_dev *sdev)
178 {
179         if (sdev->num_registers != 0) {
180                 struct sbus_dev *parent = sdev->parent;
181                 int i;
182
183                 while (parent != NULL) {
184                         __apply_ranges_to_regs(parent->device_ranges,
185                                                parent->num_device_ranges,
186                                                sdev->reg_addrs,
187                                                sdev->num_registers);
188
189                         parent = parent->parent;
190                 }
191
192                 __apply_ranges_to_regs(sdev->bus->sbus_ranges,
193                                        sdev->bus->num_sbus_ranges,
194                                        sdev->reg_addrs,
195                                        sdev->num_registers);
196
197                 for (i = 0; i < sdev->num_registers; i++) {
198                         struct resource *res = &sdev->resource[i];
199
200                         res->start = sdev->reg_addrs[i].phys_addr;
201                         res->end = (res->start +
202                                     (unsigned long)sdev->reg_addrs[i].reg_size - 1UL);
203                         res->flags = IORESOURCE_IO |
204                                 (sdev->reg_addrs[i].which_io & 0xff);
205                 }
206         }
207 }
208
209 static void __init sbus_fixup_all_regs(struct sbus_dev *first_sdev)
210 {
211         struct sbus_dev *sdev;
212
213         for (sdev = first_sdev; sdev; sdev = sdev->next) {
214                 if (sdev->child)
215                         sbus_fixup_all_regs(sdev->child);
216                 __fixup_regs_sdev(sdev);
217         }
218 }
219
220 extern void register_proc_sparc_ioport(void);
221 extern void firetruck_init(void);
222
223 #ifdef CONFIG_SUN4
224 extern void sun4_dvma_init(void);
225 #endif
226
227 static int __init sbus_init(void)
228 {
229         int nd, this_sbus, sbus_devs, topnd, iommund;
230         unsigned int sbus_clock;
231         struct sbus_bus *sbus;
232         struct sbus_dev *this_dev;
233         int num_sbus = 0;  /* How many did we find? */
234
235 #ifdef CONFIG_SPARC32
236         register_proc_sparc_ioport();
237 #endif
238
239 #ifdef CONFIG_SUN4
240         sun4_dvma_init();
241         return 0;
242 #endif
243
244         topnd = prom_getchild(prom_root_node);
245         
246         /* Finding the first sbus is a special case... */
247         iommund = 0;
248         if(sparc_cpu_model == sun4u) {
249                 nd = prom_searchsiblings(topnd, "sbus");
250                 if(nd == 0) {
251 #ifdef CONFIG_PCI
252                         if (!pcic_present()) {
253                                 prom_printf("Neither SBUS nor PCI found.\n");
254                                 prom_halt();
255                         } else {
256 #ifdef CONFIG_SPARC64
257                                 firetruck_init();
258 #endif
259                         }
260                         return 0;
261 #else
262                         prom_printf("YEEE, UltraSparc sbus not found\n");
263                         prom_halt();
264 #endif
265                 }
266         } else if(sparc_cpu_model == sun4d) {
267                 if((iommund = prom_searchsiblings(topnd, "io-unit")) == 0 ||
268                    (nd = prom_getchild(iommund)) == 0 ||
269                    (nd = prom_searchsiblings(nd, "sbi")) == 0) {
270                         panic("sbi not found");
271                 }
272         } else if((nd = prom_searchsiblings(topnd, "sbus")) == 0) {
273                 if((iommund = prom_searchsiblings(topnd, "iommu")) == 0 ||
274                    (nd = prom_getchild(iommund)) == 0 ||
275                    (nd = prom_searchsiblings(nd, "sbus")) == 0) {
276 #ifdef CONFIG_PCI
277                         if (!pcic_present()) {
278                                 prom_printf("Neither SBUS nor PCI found.\n");
279                                 prom_halt();
280                         }
281                         return 0;
282 #else
283                         /* No reason to run further - the data access trap will occur. */
284                         panic("sbus not found");
285 #endif
286                 }
287         }
288
289         /* Ok, we've found the first one, allocate first SBus struct
290          * and place in chain.
291          */
292         sbus = sbus_root = kmalloc(sizeof(struct sbus_bus), GFP_ATOMIC);
293         sbus->next = NULL;
294         sbus->prom_node = nd;
295         this_sbus = nd;
296
297         if(iommund && sparc_cpu_model != sun4u && sparc_cpu_model != sun4d)
298                 iommu_init(iommund, sbus);
299
300         /* Loop until we find no more SBUS's */
301         while(this_sbus) {
302 #ifdef CONFIG_SPARC64
303                 /* IOMMU hides inside SBUS/SYSIO prom node on Ultra. */
304                 if(sparc_cpu_model == sun4u) {
305                         extern void sbus_iommu_init(int prom_node, struct sbus_bus *sbus);
306
307                         sbus_iommu_init(this_sbus, sbus);
308                 }
309 #endif /* CONFIG_SPARC64 */
310
311 #ifdef CONFIG_SPARC32
312                 if (sparc_cpu_model == sun4d)
313                         iounit_init(this_sbus, iommund, sbus);
314 #endif /* CONFIG_SPARC32 */
315                 printk("sbus%d: ", num_sbus);
316                 sbus_clock = prom_getint(this_sbus, "clock-frequency");
317                 if(sbus_clock == -1)
318                         sbus_clock = (25*1000*1000);
319                 printk("Clock %d.%d MHz\n", (int) ((sbus_clock/1000)/1000),
320                        (int) (((sbus_clock/1000)%1000 != 0) ? 
321                               (((sbus_clock/1000)%1000) + 1000) : 0));
322
323                 prom_getstring(this_sbus, "name",
324                                sbus->prom_name, sizeof(sbus->prom_name));
325                 sbus->clock_freq = sbus_clock;
326 #ifdef CONFIG_SPARC32
327                 if (sparc_cpu_model == sun4d) {
328                         sbus->devid = prom_getint(iommund, "device-id");
329                         sbus->board = prom_getint(iommund, "board#");
330                 }
331 #endif
332                 
333                 sbus_bus_ranges_init(iommund, sbus);
334
335                 sbus_devs = prom_getchild(this_sbus);
336                 if (!sbus_devs) {
337                         sbus->devices = NULL;
338                         goto next_bus;
339                 }
340
341                 sbus->devices = kmalloc(sizeof(struct sbus_dev), GFP_ATOMIC);
342
343                 this_dev = sbus->devices;
344                 this_dev->next = NULL;
345
346                 this_dev->bus = sbus;
347                 this_dev->parent = NULL;
348                 fill_sbus_device(sbus_devs, this_dev);
349
350                 /* Should we traverse for children? */
351                 if(prom_getchild(sbus_devs)) {
352                         /* Allocate device node */
353                         this_dev->child = kmalloc(sizeof(struct sbus_dev),
354                                                   GFP_ATOMIC);
355                         /* Fill it */
356                         this_dev->child->bus = sbus;
357                         this_dev->child->next = NULL;
358                         fill_sbus_device(prom_getchild(sbus_devs),
359                                          this_dev->child);
360                         sbus_do_child_siblings(prom_getchild(sbus_devs),
361                                                this_dev->child,
362                                                this_dev,
363                                                sbus);
364                 } else {
365                         this_dev->child = NULL;
366                 }
367
368                 while((sbus_devs = prom_getsibling(sbus_devs)) != 0) {
369                         /* Allocate device node */
370                         this_dev->next = kmalloc(sizeof(struct sbus_dev),
371                                                  GFP_ATOMIC);
372                         this_dev = this_dev->next;
373                         this_dev->next = NULL;
374
375                         /* Fill it */
376                         this_dev->bus = sbus;
377                         this_dev->parent = NULL;
378                         fill_sbus_device(sbus_devs, this_dev);
379
380                         /* Is there a child node hanging off of us? */
381                         if(prom_getchild(sbus_devs)) {
382                                 /* Get new device struct */
383                                 this_dev->child = kmalloc(sizeof(struct sbus_dev),
384                                                           GFP_ATOMIC);
385                                 /* Fill it */
386                                 this_dev->child->bus = sbus;
387                                 this_dev->child->next = NULL;
388                                 fill_sbus_device(prom_getchild(sbus_devs),
389                                                  this_dev->child);
390                                 sbus_do_child_siblings(prom_getchild(sbus_devs),
391                                                        this_dev->child,
392                                                        this_dev,
393                                                        sbus);
394                         } else {
395                                 this_dev->child = NULL;
396                         }
397                 }
398
399                 /* Walk all devices and apply parent ranges. */
400                 sbus_fixup_all_regs(sbus->devices);
401
402                 dvma_init(sbus);
403         next_bus:
404                 num_sbus++;
405                 if(sparc_cpu_model == sun4u) {
406                         this_sbus = prom_getsibling(this_sbus);
407                         if(!this_sbus)
408                                 break;
409                         this_sbus = prom_searchsiblings(this_sbus, "sbus");
410                 } else if(sparc_cpu_model == sun4d) {
411                         iommund = prom_getsibling(iommund);
412                         if(!iommund)
413                                 break;
414                         iommund = prom_searchsiblings(iommund, "io-unit");
415                         if(!iommund)
416                                 break;
417                         this_sbus = prom_searchsiblings(prom_getchild(iommund), "sbi");
418                 } else {
419                         this_sbus = prom_getsibling(this_sbus);
420                         if(!this_sbus)
421                                 break;
422                         this_sbus = prom_searchsiblings(this_sbus, "sbus");
423                 }
424                 if(this_sbus) {
425                         sbus->next = kmalloc(sizeof(struct sbus_bus), GFP_ATOMIC);
426                         sbus = sbus->next;
427                         sbus->next = NULL;
428                         sbus->prom_node = this_sbus;
429                 } else {
430                         break;
431                 }
432         } /* while(this_sbus) */
433
434         if (sparc_cpu_model == sun4d) {
435                 extern void sun4d_init_sbi_irq(void);
436                 sun4d_init_sbi_irq();
437         }
438         
439 #ifdef CONFIG_SPARC64
440         if (sparc_cpu_model == sun4u) {
441                 firetruck_init();
442         }
443 #endif
444 #ifdef CONFIG_SUN_AUXIO
445         if (sparc_cpu_model == sun4u)
446                 auxio_probe ();
447 #endif
448 #ifdef CONFIG_SPARC64
449         if (sparc_cpu_model == sun4u) {
450                 extern void clock_probe(void);
451
452                 clock_probe();
453         }
454 #endif
455
456         return 0;
457 }
458
459 subsys_initcall(sbus_init);