VServer 1.9.2 (patch-2.6.8.1-vs1.9.2.diff)
[linux-2.6.git] / drivers / sbus / sbus.c
1 /* $Id: sbus.c,v 1.100 2002/01/24 15:36:24 davem Exp $
2  * sbus.c:  SBus support routines.
3  *
4  * Copyright (C) 1995 David S. Miller (davem@caip.rutgers.edu)
5  */
6
7 #include <linux/kernel.h>
8 #include <linux/slab.h>
9 #include <linux/config.h>
10 #include <linux/init.h>
11 #include <linux/pci.h>
12
13 #include <asm/system.h>
14 #include <asm/sbus.h>
15 #include <asm/dma.h>
16 #include <asm/oplib.h>
17 #include <asm/bpp.h>
18 #include <asm/irq.h>
19
20 struct sbus_bus *sbus_root = NULL;
21
22 static struct linux_prom_irqs irqs[PROMINTR_MAX] __initdata = { { 0 } };
23 #ifdef CONFIG_SPARC32
24 static int interrupts[PROMINTR_MAX] __initdata = { 0 };
25 #endif
26
27 #ifdef CONFIG_PCI
28 extern int pcic_present(void);
29 #endif
30
31 /* Perhaps when I figure out more about the iommu we'll put a
32  * device registration routine here that probe_sbus() calls to
33  * setup the iommu for each Sbus.
34  */
35
36 /* We call this for each SBus device, and fill the structure based
37  * upon the prom device tree.  We return the start of memory after
38  * the things we have allocated.
39  */
40
41 /* #define DEBUG_FILL */
42
43 static void __init fill_sbus_device(int prom_node, struct sbus_dev *sdev)
44 {
45         unsigned long address, base;
46         int len;
47
48         sdev->prom_node = prom_node;
49         prom_getstring(prom_node, "name",
50                        sdev->prom_name, sizeof(sdev->prom_name));
51         address = prom_getint(prom_node, "address");
52         len = prom_getproperty(prom_node, "reg",
53                                (char *) sdev->reg_addrs,
54                                sizeof(sdev->reg_addrs));
55         if (len == -1) {
56                 sdev->num_registers = 0;
57                 goto no_regs;
58         }
59
60         if (len % sizeof(struct linux_prom_registers)) {
61                 prom_printf("fill_sbus_device: proplen for regs of %s "
62                             " was %d, need multiple of %d\n",
63                             sdev->prom_name, len,
64                             (int) sizeof(struct linux_prom_registers));
65                 prom_halt();
66         }
67         if (len > (sizeof(struct linux_prom_registers) * PROMREG_MAX)) {
68                 prom_printf("fill_sbus_device: Too many register properties "
69                             "for device %s, len=%d\n",
70                             sdev->prom_name, len);
71                 prom_halt();
72         }
73         sdev->num_registers = len / sizeof(struct linux_prom_registers);
74         sdev->ranges_applied = 0;
75
76         base = (unsigned long) sdev->reg_addrs[0].phys_addr;
77
78         /* Compute the slot number. */
79         if (base >= SUN_SBUS_BVADDR && sparc_cpu_model == sun4m) {
80                 sdev->slot = sbus_dev_slot(base);
81         } else {
82                 sdev->slot = sdev->reg_addrs[0].which_io;
83         }
84
85 no_regs:
86         len = prom_getproperty(prom_node, "ranges",
87                                (char *)sdev->device_ranges,
88                                sizeof(sdev->device_ranges));
89         if (len == -1) {
90                 sdev->num_device_ranges = 0;
91                 goto no_ranges;
92         }
93         if (len % sizeof(struct linux_prom_ranges)) {
94                 prom_printf("fill_sbus_device: proplen for ranges of %s "
95                             " was %d, need multiple of %d\n",
96                             sdev->prom_name, len,
97                             (int) sizeof(struct linux_prom_ranges));
98                 prom_halt();
99         }
100         if (len > (sizeof(struct linux_prom_ranges) * PROMREG_MAX)) {
101                 prom_printf("fill_sbus_device: Too many range properties "
102                             "for device %s, len=%d\n",
103                             sdev->prom_name, len);
104                 prom_halt();
105         }
106         sdev->num_device_ranges =
107                 len / sizeof(struct linux_prom_ranges);
108
109 no_ranges:
110         /* XXX Unfortunately, IRQ issues are very arch specific.
111          * XXX Pull this crud out into an arch specific area
112          * XXX at some point. -DaveM
113          */
114 #ifdef CONFIG_SPARC64
115         len = prom_getproperty(prom_node, "interrupts",
116                                (char *) irqs, sizeof(irqs));
117         if (len == -1 || len == 0) {
118                 sdev->irqs[0] = 0;
119                 sdev->num_irqs = 0;
120         } else {
121                 unsigned int pri = irqs[0].pri;
122
123                 sdev->num_irqs = 1;
124                 if (pri < 0x20)
125                         pri += sdev->slot * 8;
126
127                 sdev->irqs[0] = sbus_build_irq(sdev->bus, pri);
128         }
129 #endif /* CONFIG_SPARC64 */
130
131 #ifdef CONFIG_SPARC32
132         len = prom_getproperty(prom_node, "intr",
133                                (char *)irqs, sizeof(irqs));
134         if (len != -1) {
135                 sdev->num_irqs = len / 8;
136                 if (sdev->num_irqs == 0) {
137                         sdev->irqs[0] = 0;
138                 } else if (sparc_cpu_model == sun4d) {
139                         extern unsigned int sun4d_build_irq(struct sbus_dev *sdev, int irq);
140
141                         for (len = 0; len < sdev->num_irqs; len++)
142                                 sdev->irqs[len] = sun4d_build_irq(sdev, irqs[len].pri);
143                 } else {
144                         for (len = 0; len < sdev->num_irqs; len++)
145                                 sdev->irqs[len] = irqs[len].pri;
146                 }
147         } else {
148                 /* No "intr" node found-- check for "interrupts" node.
149                  * This node contains SBus interrupt levels, not IPLs
150                  * as in "intr", and no vector values.  We convert 
151                  * SBus interrupt levels to PILs (platform specific).
152                  */
153                 len = prom_getproperty(prom_node, "interrupts", 
154                                         (char *)interrupts, sizeof(interrupts));
155                 if (len == -1) {
156                         sdev->irqs[0] = 0;
157                         sdev->num_irqs = 0;
158                 } else {
159                         sdev->num_irqs = len / sizeof(int);
160                         for (len = 0; len < sdev->num_irqs; len++) {
161                                 sdev->irqs[len] = sbint_to_irq(sdev, interrupts[len]);
162                         }
163                 }
164         } 
165 #endif /* CONFIG_SPARC32 */
166 }
167
168 /* This routine gets called from whoever needs the sbus first, to scan
169  * the SBus device tree.  Currently it just prints out the devices
170  * found on the bus and builds trees of SBUS structs and attached
171  * devices.
172  */
173
174 extern void iommu_init(int iommu_node, struct sbus_bus *sbus);
175 extern void iounit_init(int sbi_node, int iounit_node, struct sbus_bus *sbus);
176 void sun4_init(void);
177 #ifdef CONFIG_SUN_AUXIO
178 extern void auxio_probe(void);
179 #endif
180
181 static void __init sbus_do_child_siblings(int start_node,
182                                           struct sbus_dev *child,
183                                           struct sbus_dev *parent,
184                                           struct sbus_bus *sbus)
185 {
186         struct sbus_dev *this_dev = child;
187         int this_node = start_node;
188
189         /* Child already filled in, just need to traverse siblings. */
190         child->child = NULL;
191         child->parent = parent;
192         while((this_node = prom_getsibling(this_node)) != 0) {
193                 this_dev->next = kmalloc(sizeof(struct sbus_dev), GFP_ATOMIC);
194                 this_dev = this_dev->next;
195                 this_dev->next = NULL;
196                 this_dev->parent = parent;
197
198                 this_dev->bus = sbus;
199                 fill_sbus_device(this_node, this_dev);
200
201                 if(prom_getchild(this_node)) {
202                         this_dev->child = kmalloc(sizeof(struct sbus_dev),
203                                                   GFP_ATOMIC);
204                         this_dev->child->bus = sbus;
205                         this_dev->child->next = NULL;
206                         fill_sbus_device(prom_getchild(this_node), this_dev->child);
207                         sbus_do_child_siblings(prom_getchild(this_node),
208                                                this_dev->child, this_dev, sbus);
209                 } else {
210                         this_dev->child = NULL;
211                 }
212         }
213 }
214
215 /*
216  * XXX This functions appears to be a distorted version of
217  * prom_sbus_ranges_init(), with all sun4d stuff cut away.
218  * Ask DaveM what is going on here, how is sun4d supposed to work... XXX
219  */
220 static void __init sbus_bus_ranges_init(int parent_node, struct sbus_bus *sbus)
221 {
222         int len;
223
224         len = prom_getproperty(sbus->prom_node, "ranges",
225                                (char *) sbus->sbus_ranges,
226                                sizeof(sbus->sbus_ranges));
227         if (len == -1 || len == 0) {
228                 sbus->num_sbus_ranges = 0;
229                 return;
230         }
231         sbus->num_sbus_ranges = len / sizeof(struct linux_prom_ranges);
232 }
233
234 static void __init __apply_ranges_to_regs(struct linux_prom_ranges *ranges,
235                                           int num_ranges,
236                                           struct linux_prom_registers *regs,
237                                           int num_regs)
238 {
239         if (num_ranges) {
240                 int regnum;
241
242                 for (regnum = 0; regnum < num_regs; regnum++) {
243                         int rngnum;
244
245                         for (rngnum = 0; rngnum < num_ranges; rngnum++) {
246                                 if (regs[regnum].which_io == ranges[rngnum].ot_child_space)
247                                         break;
248                         }
249                         if (rngnum == num_ranges) {
250                                 /* We used to flag this as an error.  Actually
251                                  * some devices do not report the regs as we expect.
252                                  * For example, see SUNW,pln device.  In that case
253                                  * the reg property is in a format internal to that
254                                  * node, ie. it is not in the SBUS register space
255                                  * per se. -DaveM
256                                  */
257                                 return;
258                         }
259                         regs[regnum].which_io = ranges[rngnum].ot_parent_space;
260                         regs[regnum].phys_addr -= ranges[rngnum].ot_child_base;
261                         regs[regnum].phys_addr += ranges[rngnum].ot_parent_base;
262                 }
263         }
264 }
265
266 static void __init __fixup_regs_sdev(struct sbus_dev *sdev)
267 {
268         if (sdev->num_registers != 0) {
269                 struct sbus_dev *parent = sdev->parent;
270                 int i;
271
272                 while (parent != NULL) {
273                         __apply_ranges_to_regs(parent->device_ranges,
274                                                parent->num_device_ranges,
275                                                sdev->reg_addrs,
276                                                sdev->num_registers);
277
278                         parent = parent->parent;
279                 }
280
281                 __apply_ranges_to_regs(sdev->bus->sbus_ranges,
282                                        sdev->bus->num_sbus_ranges,
283                                        sdev->reg_addrs,
284                                        sdev->num_registers);
285
286                 for (i = 0; i < sdev->num_registers; i++) {
287                         struct resource *res = &sdev->resource[i];
288
289                         res->start = sdev->reg_addrs[i].phys_addr;
290                         res->end = (res->start +
291                                     (unsigned long)sdev->reg_addrs[i].reg_size - 1UL);
292                         res->flags = IORESOURCE_IO |
293                                 (sdev->reg_addrs[i].which_io & 0xff);
294                 }
295         }
296 }
297
298 static void __init sbus_fixup_all_regs(struct sbus_dev *first_sdev)
299 {
300         struct sbus_dev *sdev;
301
302         for (sdev = first_sdev; sdev; sdev = sdev->next) {
303                 if (sdev->child)
304                         sbus_fixup_all_regs(sdev->child);
305                 __fixup_regs_sdev(sdev);
306         }
307 }
308
309 extern void register_proc_sparc_ioport(void);
310 extern void firetruck_init(void);
311
312 #ifdef CONFIG_SUN4
313 extern void sun4_dvma_init(void);
314 #endif
315
316 static int __init sbus_init(void)
317 {
318         int nd, this_sbus, sbus_devs, topnd, iommund;
319         unsigned int sbus_clock;
320         struct sbus_bus *sbus;
321         struct sbus_dev *this_dev;
322         int num_sbus = 0;  /* How many did we find? */
323
324 #ifdef CONFIG_SPARC32
325         register_proc_sparc_ioport();
326 #endif
327
328 #ifdef CONFIG_SUN4
329         sun4_dvma_init();
330         return 0;
331 #endif
332
333         topnd = prom_getchild(prom_root_node);
334         
335         /* Finding the first sbus is a special case... */
336         iommund = 0;
337         if(sparc_cpu_model == sun4u) {
338                 nd = prom_searchsiblings(topnd, "sbus");
339                 if(nd == 0) {
340 #ifdef CONFIG_PCI
341                         if (!pcic_present()) {
342                                 prom_printf("Neither SBUS nor PCI found.\n");
343                                 prom_halt();
344                         } else {
345 #ifdef CONFIG_SPARC64
346                                 firetruck_init();
347 #endif
348                         }
349                         return 0;
350 #else
351                         prom_printf("YEEE, UltraSparc sbus not found\n");
352                         prom_halt();
353 #endif
354                 }
355         } else if(sparc_cpu_model == sun4d) {
356                 if((iommund = prom_searchsiblings(topnd, "io-unit")) == 0 ||
357                    (nd = prom_getchild(iommund)) == 0 ||
358                    (nd = prom_searchsiblings(nd, "sbi")) == 0) {
359                         panic("sbi not found");
360                 }
361         } else if((nd = prom_searchsiblings(topnd, "sbus")) == 0) {
362                 if((iommund = prom_searchsiblings(topnd, "iommu")) == 0 ||
363                    (nd = prom_getchild(iommund)) == 0 ||
364                    (nd = prom_searchsiblings(nd, "sbus")) == 0) {
365 #ifdef CONFIG_PCI
366                         if (!pcic_present()) {
367                                 prom_printf("Neither SBUS nor PCI found.\n");
368                                 prom_halt();
369                         }
370                         return 0;
371 #else
372                         /* No reason to run further - the data access trap will occur. */
373                         panic("sbus not found");
374 #endif
375                 }
376         }
377
378         /* Ok, we've found the first one, allocate first SBus struct
379          * and place in chain.
380          */
381         sbus = sbus_root = kmalloc(sizeof(struct sbus_bus), GFP_ATOMIC);
382         sbus->next = NULL;
383         sbus->prom_node = nd;
384         this_sbus = nd;
385
386         if(iommund && sparc_cpu_model != sun4u && sparc_cpu_model != sun4d)
387                 iommu_init(iommund, sbus);
388
389         /* Loop until we find no more SBUS's */
390         while(this_sbus) {
391 #ifdef CONFIG_SPARC64
392                 /* IOMMU hides inside SBUS/SYSIO prom node on Ultra. */
393                 if(sparc_cpu_model == sun4u) {
394                         extern void sbus_iommu_init(int prom_node, struct sbus_bus *sbus);
395
396                         sbus_iommu_init(this_sbus, sbus);
397                 }
398 #endif /* CONFIG_SPARC64 */
399
400 #ifdef CONFIG_SPARC32
401                 if (sparc_cpu_model == sun4d)
402                         iounit_init(this_sbus, iommund, sbus);
403 #endif /* CONFIG_SPARC32 */
404                 printk("sbus%d: ", num_sbus);
405                 sbus_clock = prom_getint(this_sbus, "clock-frequency");
406                 if(sbus_clock == -1)
407                         sbus_clock = (25*1000*1000);
408                 printk("Clock %d.%d MHz\n", (int) ((sbus_clock/1000)/1000),
409                        (int) (((sbus_clock/1000)%1000 != 0) ? 
410                               (((sbus_clock/1000)%1000) + 1000) : 0));
411
412                 prom_getstring(this_sbus, "name",
413                                sbus->prom_name, sizeof(sbus->prom_name));
414                 sbus->clock_freq = sbus_clock;
415 #ifdef CONFIG_SPARC32
416                 if (sparc_cpu_model == sun4d) {
417                         sbus->devid = prom_getint(iommund, "device-id");
418                         sbus->board = prom_getint(iommund, "board#");
419                 }
420 #endif
421                 
422                 sbus_bus_ranges_init(iommund, sbus);
423
424                 sbus_devs = prom_getchild(this_sbus);
425                 if (!sbus_devs) {
426                         sbus->devices = NULL;
427                         goto next_bus;
428                 }
429
430                 sbus->devices = kmalloc(sizeof(struct sbus_dev), GFP_ATOMIC);
431
432                 this_dev = sbus->devices;
433                 this_dev->next = NULL;
434
435                 this_dev->bus = sbus;
436                 this_dev->parent = NULL;
437                 fill_sbus_device(sbus_devs, this_dev);
438
439                 /* Should we traverse for children? */
440                 if(prom_getchild(sbus_devs)) {
441                         /* Allocate device node */
442                         this_dev->child = kmalloc(sizeof(struct sbus_dev),
443                                                   GFP_ATOMIC);
444                         /* Fill it */
445                         this_dev->child->bus = sbus;
446                         this_dev->child->next = NULL;
447                         fill_sbus_device(prom_getchild(sbus_devs),
448                                          this_dev->child);
449                         sbus_do_child_siblings(prom_getchild(sbus_devs),
450                                                this_dev->child,
451                                                this_dev,
452                                                sbus);
453                 } else {
454                         this_dev->child = NULL;
455                 }
456
457                 while((sbus_devs = prom_getsibling(sbus_devs)) != 0) {
458                         /* Allocate device node */
459                         this_dev->next = kmalloc(sizeof(struct sbus_dev),
460                                                  GFP_ATOMIC);
461                         this_dev = this_dev->next;
462                         this_dev->next = NULL;
463
464                         /* Fill it */
465                         this_dev->bus = sbus;
466                         this_dev->parent = NULL;
467                         fill_sbus_device(sbus_devs, this_dev);
468
469                         /* Is there a child node hanging off of us? */
470                         if(prom_getchild(sbus_devs)) {
471                                 /* Get new device struct */
472                                 this_dev->child = kmalloc(sizeof(struct sbus_dev),
473                                                           GFP_ATOMIC);
474                                 /* Fill it */
475                                 this_dev->child->bus = sbus;
476                                 this_dev->child->next = NULL;
477                                 fill_sbus_device(prom_getchild(sbus_devs),
478                                                  this_dev->child);
479                                 sbus_do_child_siblings(prom_getchild(sbus_devs),
480                                                        this_dev->child,
481                                                        this_dev,
482                                                        sbus);
483                         } else {
484                                 this_dev->child = NULL;
485                         }
486                 }
487
488                 /* Walk all devices and apply parent ranges. */
489                 sbus_fixup_all_regs(sbus->devices);
490
491                 dvma_init(sbus);
492         next_bus:
493                 num_sbus++;
494                 if(sparc_cpu_model == sun4u) {
495                         this_sbus = prom_getsibling(this_sbus);
496                         if(!this_sbus)
497                                 break;
498                         this_sbus = prom_searchsiblings(this_sbus, "sbus");
499                 } else if(sparc_cpu_model == sun4d) {
500                         iommund = prom_getsibling(iommund);
501                         if(!iommund)
502                                 break;
503                         iommund = prom_searchsiblings(iommund, "io-unit");
504                         if(!iommund)
505                                 break;
506                         this_sbus = prom_searchsiblings(prom_getchild(iommund), "sbi");
507                 } else {
508                         this_sbus = prom_getsibling(this_sbus);
509                         if(!this_sbus)
510                                 break;
511                         this_sbus = prom_searchsiblings(this_sbus, "sbus");
512                 }
513                 if(this_sbus) {
514                         sbus->next = kmalloc(sizeof(struct sbus_bus), GFP_ATOMIC);
515                         sbus = sbus->next;
516                         sbus->next = NULL;
517                         sbus->prom_node = this_sbus;
518                 } else {
519                         break;
520                 }
521         } /* while(this_sbus) */
522
523         if (sparc_cpu_model == sun4d) {
524                 extern void sun4d_init_sbi_irq(void);
525                 sun4d_init_sbi_irq();
526         }
527         
528 #ifdef CONFIG_SPARC64
529         if (sparc_cpu_model == sun4u) {
530                 firetruck_init();
531         }
532 #endif
533 #ifdef CONFIG_SUN_AUXIO
534         if (sparc_cpu_model == sun4u)
535                 auxio_probe ();
536 #endif
537 #ifdef CONFIG_SPARC64
538         if (sparc_cpu_model == sun4u) {
539                 extern void clock_probe(void);
540
541                 clock_probe();
542         }
543 #endif
544
545         return 0;
546 }
547
548 subsys_initcall(sbus_init);