added mtd driver
[linux-2.4.git] / drivers / sbus / sbus.c
1 /* $Id: sbus.c,v 1.95.2.3 2002/01/05 01:12:31 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 __sparc_v9__
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 #else
130         len = prom_getproperty(prom_node, "intr",
131                                (char *)irqs, sizeof(irqs));
132         if (len != -1) {
133                 sdev->num_irqs = len / 8;
134                 if (sdev->num_irqs == 0) {
135                         sdev->irqs[0] = 0;
136                 } else if (sparc_cpu_model == sun4d) {
137                         extern unsigned int sun4d_build_irq(struct sbus_dev *sdev, int irq);
138
139                         for (len = 0; len < sdev->num_irqs; len++)
140                                 sdev->irqs[len] = sun4d_build_irq(sdev, irqs[len].pri);
141                 } else {
142                         for (len = 0; len < sdev->num_irqs; len++)
143                                 sdev->irqs[len] = irqs[len].pri;
144                 }
145         } else {
146                 /* No "intr" node found-- check for "interrupts" node.
147                  * This node contains SBus interrupt levels, not IPLs
148                  * as in "intr", and no vector values.  We convert 
149                  * SBus interrupt levels to PILs (platform specific).
150                  */
151                 len = prom_getproperty(prom_node, "interrupts", 
152                                         (char *)interrupts, sizeof(interrupts));
153                 if (len == -1) {
154                         sdev->irqs[0] = 0;
155                         sdev->num_irqs = 0;
156                 } else {
157                         sdev->num_irqs = len / sizeof(int);
158                         for (len = 0; len < sdev->num_irqs; len++) {
159                                 sdev->irqs[len] = sbint_to_irq(sdev, interrupts[len]);
160                         }
161                 }
162         } 
163 #endif /* !__sparc_v9__ */
164 }
165
166 /* This routine gets called from whoever needs the sbus first, to scan
167  * the SBus device tree.  Currently it just prints out the devices
168  * found on the bus and builds trees of SBUS structs and attached
169  * devices.
170  */
171
172 extern void iommu_init(int iommu_node, struct sbus_bus *sbus);
173 extern void iounit_init(int sbi_node, int iounit_node, struct sbus_bus *sbus);
174 void sun4_init(void);
175 #ifdef CONFIG_SUN_AUXIO
176 extern void auxio_probe(void);
177 #endif
178
179 static void __init sbus_do_child_siblings(int start_node,
180                                           struct sbus_dev *child,
181                                           struct sbus_dev *parent,
182                                           struct sbus_bus *sbus)
183 {
184         struct sbus_dev *this_dev = child;
185         int this_node = start_node;
186
187         /* Child already filled in, just need to traverse siblings. */
188         child->child = NULL;
189         child->parent = parent;
190         while((this_node = prom_getsibling(this_node)) != 0) {
191                 this_dev->next = kmalloc(sizeof(struct sbus_dev), GFP_ATOMIC);
192                 this_dev = this_dev->next;
193                 this_dev->next = 0;
194                 this_dev->parent = parent;
195
196                 this_dev->bus = sbus;
197                 fill_sbus_device(this_node, this_dev);
198
199                 if(prom_getchild(this_node)) {
200                         this_dev->child = kmalloc(sizeof(struct sbus_dev),
201                                                   GFP_ATOMIC);
202                         this_dev->child->bus = sbus;
203                         this_dev->child->next = 0;
204                         fill_sbus_device(prom_getchild(this_node), this_dev->child);
205                         sbus_do_child_siblings(prom_getchild(this_node),
206                                                this_dev->child, this_dev, sbus);
207                 } else {
208                         this_dev->child = NULL;
209                 }
210         }
211 }
212
213 /*
214  * XXX This functions appears to be a distorted version of
215  * prom_sbus_ranges_init(), with all sun4d stuff cut away.
216  * Ask DaveM what is going on here, how is sun4d supposed to work... XXX
217  */
218 /* added back sun4d patch from Thomas Bogendoerfer - should be OK (crn) */
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 #ifdef CONFIG_SPARC32
233         if (sparc_cpu_model == sun4d) {
234                 struct linux_prom_ranges iounit_ranges[PROMREG_MAX];
235                 int num_iounit_ranges;
236
237                 len = prom_getproperty(parent_node, "ranges",
238                                        (char *) iounit_ranges,
239                                        sizeof (iounit_ranges));
240                 if (len != -1) {
241                         num_iounit_ranges = (len/sizeof(struct linux_prom_ranges));
242                         prom_adjust_ranges (sbus->sbus_ranges, sbus->num_sbus_ranges, iounit_ranges, num_iounit_ranges);
243                 }
244         }
245 #endif
246 }
247
248 static void __init __apply_ranges_to_regs(struct linux_prom_ranges *ranges,
249                                           int num_ranges,
250                                           struct linux_prom_registers *regs,
251                                           int num_regs)
252 {
253         if (num_ranges) {
254                 int regnum;
255
256                 for (regnum = 0; regnum < num_regs; regnum++) {
257                         int rngnum;
258
259                         for (rngnum = 0; rngnum < num_ranges; rngnum++) {
260                                 if (regs[regnum].which_io == ranges[rngnum].ot_child_space)
261                                         break;
262                         }
263                         if (rngnum == num_ranges) {
264                                 /* We used to flag this as an error.  Actually
265                                  * some devices do not report the regs as we expect.
266                                  * For example, see SUNW,pln device.  In that case
267                                  * the reg property is in a format internal to that
268                                  * node, ie. it is not in the SBUS register space
269                                  * per se. -DaveM
270                                  */
271                                 return;
272                         }
273                         regs[regnum].which_io = ranges[rngnum].ot_parent_space;
274                         regs[regnum].phys_addr -= ranges[rngnum].ot_child_base;
275                         regs[regnum].phys_addr += ranges[rngnum].ot_parent_base;
276                 }
277         }
278 }
279
280 static void __init __fixup_regs_sdev(struct sbus_dev *sdev)
281 {
282         if (sdev->num_registers != 0) {
283                 struct sbus_dev *parent = sdev->parent;
284                 int i;
285
286                 while (parent != NULL) {
287                         __apply_ranges_to_regs(parent->device_ranges,
288                                                parent->num_device_ranges,
289                                                sdev->reg_addrs,
290                                                sdev->num_registers);
291
292                         parent = parent->parent;
293                 }
294
295                 __apply_ranges_to_regs(sdev->bus->sbus_ranges,
296                                        sdev->bus->num_sbus_ranges,
297                                        sdev->reg_addrs,
298                                        sdev->num_registers);
299
300                 for (i = 0; i < sdev->num_registers; i++) {
301                         struct resource *res = &sdev->resource[i];
302
303                         res->start = sdev->reg_addrs[i].phys_addr;
304                         res->end = (res->start +
305                                     (unsigned long)sdev->reg_addrs[i].reg_size - 1UL);
306                         res->flags = IORESOURCE_IO |
307                                 (sdev->reg_addrs[i].which_io & 0xff);
308                 }
309         }
310 }
311
312 static void __init sbus_fixup_all_regs(struct sbus_dev *first_sdev)
313 {
314         struct sbus_dev *sdev;
315
316         for (sdev = first_sdev; sdev; sdev = sdev->next) {
317                 if (sdev->child)
318                         sbus_fixup_all_regs(sdev->child);
319                 __fixup_regs_sdev(sdev);
320         }
321 }
322
323 extern void register_proc_sparc_ioport(void);
324 extern void firetruck_init(void);
325 extern void rs_init(void);
326
327 void __init sbus_init(void)
328 {
329         int nd, this_sbus, sbus_devs, topnd, iommund;
330         unsigned int sbus_clock;
331         struct sbus_bus *sbus;
332         struct sbus_dev *this_dev;
333         int num_sbus = 0;  /* How many did we find? */
334
335 #ifndef __sparc_v9__
336         register_proc_sparc_ioport();
337 #endif
338
339 #ifdef CONFIG_SUN4
340         return sun4_dvma_init();
341 #endif
342
343         topnd = prom_getchild(prom_root_node);
344         
345         /* Finding the first sbus is a special case... */
346         iommund = 0;
347         if(sparc_cpu_model == sun4u) {
348                 nd = prom_searchsiblings(topnd, "sbus");
349                 if(nd == 0) {
350 #ifdef CONFIG_PCI
351                         if (!pcic_present()) {  
352                                 prom_printf("Neither SBUS nor PCI found.\n");
353                                 prom_halt();
354                         } else {
355 #ifdef __sparc_v9__
356                                 firetruck_init();
357 #endif
358                         }
359                         return;
360 #else
361                         prom_printf("YEEE, UltraSparc sbus not found\n");
362                         prom_halt();
363 #endif
364                 }
365         } else if(sparc_cpu_model == sun4d) {
366                 if((iommund = prom_searchsiblings(topnd, "io-unit")) == 0 ||
367                    (nd = prom_getchild(iommund)) == 0 ||
368                    (nd = prom_searchsiblings(nd, "sbi")) == 0) {
369                         panic("sbi not found");
370                 }
371         } else if((nd = prom_searchsiblings(topnd, "sbus")) == 0) {
372                 if((iommund = prom_searchsiblings(topnd, "iommu")) == 0 ||
373                    (nd = prom_getchild(iommund)) == 0 ||
374                    (nd = prom_searchsiblings(nd, "sbus")) == 0) {
375 #ifdef CONFIG_PCI
376                         if (!pcic_present()) {       
377                                 prom_printf("Neither SBUS nor PCI found.\n");
378                                 prom_halt();
379                         }
380                         return;
381 #else
382                         /* No reason to run further - the data access trap will occur. */
383                         panic("sbus not found");
384 #endif
385                 }
386         }
387
388         /* Ok, we've found the first one, allocate first SBus struct
389          * and place in chain.
390          */
391         sbus = sbus_root = kmalloc(sizeof(struct sbus_bus), GFP_ATOMIC);
392         sbus->next = NULL;
393         sbus->prom_node = nd;
394         this_sbus = nd;
395
396         if(iommund && sparc_cpu_model != sun4u && sparc_cpu_model != sun4d)
397                 iommu_init(iommund, sbus);
398
399         /* Loop until we find no more SBUS's */
400         while(this_sbus) {
401 #ifdef __sparc_v9__                                               
402                 /* IOMMU hides inside SBUS/SYSIO prom node on Ultra. */
403                 if(sparc_cpu_model == sun4u) {
404                         extern void sbus_iommu_init(int prom_node, struct sbus_bus *sbus);
405
406                         sbus_iommu_init(this_sbus, sbus);
407                 }
408 #endif
409 #ifndef __sparc_v9__                                              
410                 if (sparc_cpu_model == sun4d)
411                         iounit_init(this_sbus, iommund, sbus);
412 #endif                                             
413                 printk("sbus%d: ", num_sbus);
414                 sbus_clock = prom_getint(this_sbus, "clock-frequency");
415                 if(sbus_clock == -1)
416                         sbus_clock = (25*1000*1000);
417                 printk("Clock %d.%d MHz\n", (int) ((sbus_clock/1000)/1000),
418                        (int) (((sbus_clock/1000)%1000 != 0) ? 
419                               (((sbus_clock/1000)%1000) + 1000) : 0));
420
421                 prom_getstring(this_sbus, "name",
422                                sbus->prom_name, sizeof(sbus->prom_name));
423                 sbus->clock_freq = sbus_clock;
424 #ifndef __sparc_v9__            
425                 if (sparc_cpu_model == sun4d) {
426                         sbus->devid = prom_getint(iommund, "device-id");
427                         sbus->board = prom_getint(iommund, "board#");
428                 }
429 #endif
430                 
431                 sbus_bus_ranges_init(iommund, sbus);
432
433                 sbus_devs = prom_getchild(this_sbus);
434                 if (!sbus_devs) {
435                         sbus->devices = NULL;
436                         goto next_bus;
437                 }
438
439                 sbus->devices = kmalloc(sizeof(struct sbus_dev), GFP_ATOMIC);
440
441                 this_dev = sbus->devices;
442                 this_dev->next = NULL;
443
444                 this_dev->bus = sbus;
445                 this_dev->parent = NULL;
446                 fill_sbus_device(sbus_devs, this_dev);
447
448                 /* Should we traverse for children? */
449                 if(prom_getchild(sbus_devs)) {
450                         /* Allocate device node */
451                         this_dev->child = kmalloc(sizeof(struct sbus_dev),
452                                                   GFP_ATOMIC);
453                         /* Fill it */
454                         this_dev->child->bus = sbus;
455                         this_dev->child->next = 0;
456                         fill_sbus_device(prom_getchild(sbus_devs),
457                                          this_dev->child);
458                         sbus_do_child_siblings(prom_getchild(sbus_devs),
459                                                this_dev->child,
460                                                this_dev,
461                                                sbus);
462                 } else {
463                         this_dev->child = NULL;
464                 }
465
466                 while((sbus_devs = prom_getsibling(sbus_devs)) != 0) {
467                         /* Allocate device node */
468                         this_dev->next = kmalloc(sizeof(struct sbus_dev),
469                                                  GFP_ATOMIC);
470                         this_dev = this_dev->next;
471                         this_dev->next = NULL;
472
473                         /* Fill it */
474                         this_dev->bus = sbus;
475                         this_dev->parent = NULL;
476                         fill_sbus_device(sbus_devs, this_dev);
477
478                         /* Is there a child node hanging off of us? */
479                         if(prom_getchild(sbus_devs)) {
480                                 /* Get new device struct */
481                                 this_dev->child = kmalloc(sizeof(struct sbus_dev),
482                                                           GFP_ATOMIC);
483                                 /* Fill it */
484                                 this_dev->child->bus = sbus;
485                                 this_dev->child->next = 0;
486                                 fill_sbus_device(prom_getchild(sbus_devs),
487                                                  this_dev->child);
488                                 sbus_do_child_siblings(prom_getchild(sbus_devs),
489                                                        this_dev->child,
490                                                        this_dev,
491                                                        sbus);
492                         } else {
493                                 this_dev->child = NULL;
494                         }
495                 }
496
497                 /* Walk all devices and apply parent ranges. */
498                 sbus_fixup_all_regs(sbus->devices);
499
500                 dvma_init(sbus);
501         next_bus:
502                 num_sbus++;
503                 if(sparc_cpu_model == sun4u) {
504                         this_sbus = prom_getsibling(this_sbus);
505                         if(!this_sbus)
506                                 break;
507                         this_sbus = prom_searchsiblings(this_sbus, "sbus");
508                 } else if(sparc_cpu_model == sun4d) {
509                         iommund = prom_getsibling(iommund);
510                         if(!iommund)
511                                 break;
512                         iommund = prom_searchsiblings(iommund, "io-unit");
513                         if(!iommund)
514                                 break;
515                         this_sbus = prom_searchsiblings(prom_getchild(iommund), "sbi");
516                 } else {
517                         this_sbus = prom_getsibling(this_sbus);
518                         if(!this_sbus)
519                                 break;
520                         this_sbus = prom_searchsiblings(this_sbus, "sbus");
521                 }
522                 if(this_sbus) {
523                         sbus->next = kmalloc(sizeof(struct sbus_bus), GFP_ATOMIC);
524                         sbus = sbus->next;
525                         sbus->next = NULL;
526                         sbus->prom_node = this_sbus;
527                 } else {
528                         break;
529                 }
530         } /* while(this_sbus) */
531
532         if (sparc_cpu_model == sun4d) {
533                 extern void sun4d_init_sbi_irq(void);
534                 sun4d_init_sbi_irq();
535         }
536         
537         rs_init();
538
539 #ifdef __sparc_v9__
540         if (sparc_cpu_model == sun4u) {
541                 firetruck_init();
542         }
543 #endif
544 #ifdef CONFIG_SUN_AUXIO
545         if (sparc_cpu_model == sun4u)
546                 auxio_probe ();
547 #endif
548 #ifdef __sparc_v9__
549         if (sparc_cpu_model == sun4u) {
550                 extern void clock_probe(void);
551
552                 clock_probe();
553         }
554 #endif
555 }