import of ftp.dlink.com/GPL/DSMG-600_reB/ppclinux.tar.gz
[linux-2.4.21-pre4.git] / drivers / gsc / dino.c
1 /*
2 **      DINO manager
3 **
4 **      (c) Copyright 1999 Red Hat Software
5 **      (c) Copyright 1999 SuSE GmbH
6 **      (c) Copyright 1999,2000 Hewlett-Packard Company
7 **      (c) Copyright 2000 Grant Grundler
8 **
9 **      This program is free software; you can redistribute it and/or modify
10 **      it under the terms of the GNU General Public License as published by
11 **      the Free Software Foundation; either version 2 of the License, or
12 **      (at your option) any later version.
13 **
14 **      This module provides access to Dino PCI bus (config/IOport spaces)
15 **      and helps manage Dino IRQ lines.
16 **
17 **      Dino interrupt handling is a bit complicated.
18 **      Dino always writes to the broadcast EIR via irr0 for now.
19 **      (BIG WARNING: using broadcast EIR is a really bad thing for SMP!)
20 **      Only one processor interrupt is used for the 11 IRQ line 
21 **      inputs to dino.
22 **
23 **      The different between Built-in Dino and Card-Mode
24 **      dino is in chip initialization and pci device initialization.
25 **
26 **      Linux drivers can only use Card-Mode Dino if pci devices I/O port
27 **      BARs are configured and used by the driver. Programming MMIO address 
28 **      requires substantial knowledge of available Host I/O address ranges
29 **      is currently not supported.  Port/Config accessor functions are the
30 **      same. "BIOS" differences are handled within the existing routines.
31 */
32
33 /*      Changes :
34 **      2001-06-14 : Clement Moyroud (moyroudc@esiee.fr)
35 **              - added support for the integrated RS232.       
36 */
37
38 /*
39 ** TODO: create a virtual address for each Dino HPA.
40 **       GSC code might be able to do this since IODC data tells us
41 **       how many pages are used. PCI subsystem could (must?) do this
42 **       for PCI drivers devices which implement/use MMIO registers.
43 */
44
45 #include <linux/config.h>
46 #include <linux/delay.h>
47 #include <linux/types.h>
48 #include <linux/kernel.h>
49 #include <linux/pci.h>
50 #include <linux/init.h>
51 #include <linux/ioport.h>
52 #include <linux/slab.h>
53 #include <linux/interrupt.h>    /* for struct irqaction */
54 #include <linux/spinlock.h>     /* for spinlock_t and prototypes */
55
56 #include <asm/pdc.h>
57 #include <asm/page.h>
58 #include <asm/system.h>
59 #include <asm/io.h>
60 #include <asm/hardware.h>
61
62 #include <asm/irq.h>            /* for "gsc" irq functions */
63 #include <asm/gsc.h>
64
65 #undef DINO_DEBUG
66
67 #ifdef DINO_DEBUG
68 #define DBG(x...) printk(x)
69 #else
70 #define DBG(x...)
71 #endif
72
73 /*
74 ** Config accessor functions only pass in the 8-bit bus number
75 ** and not the 8-bit "PCI Segment" number. Each Dino will be
76 ** assigned a PCI bus number based on "when" it's discovered.
77 **
78 ** The "secondary" bus number is set to this before calling
79 ** pci_scan_bus(). If any PPB's are present, the scan will
80 ** discover them and update the "secondary" and "subordinate"
81 ** fields in Dino's pci_bus structure.
82 **
83 ** Changes in the configuration *will* result in a different
84 ** bus number for each dino.
85 */
86
87 #define is_card_dino(id) ((id)->hw_type == HPHW_A_DMA)
88
89 #define DINO_IAR0               0x004
90 #define DINO_IODC_ADDR          0x008
91 #define DINO_IODC_DATA_0        0x008
92 #define DINO_IODC_DATA_1        0x008
93 #define DINO_IRR0               0x00C
94 #define DINO_IAR1               0x010
95 #define DINO_IRR1               0x014
96 #define DINO_IMR                0x018
97 #define DINO_IPR                0x01C
98 #define DINO_TOC_ADDR           0x020
99 #define DINO_ICR                0x024
100 #define DINO_ILR                0x028
101 #define DINO_IO_COMMAND         0x030
102 #define DINO_IO_STATUS          0x034
103 #define DINO_IO_CONTROL         0x038
104 #define DINO_IO_GSC_ERR_RESP    0x040
105 #define DINO_IO_ERR_INFO        0x044
106 #define DINO_IO_PCI_ERR_RESP    0x048
107 #define DINO_IO_FBB_EN          0x05c
108 #define DINO_IO_ADDR_EN         0x060
109 #define DINO_PCI_ADDR           0x064
110 #define DINO_CONFIG_DATA        0x068
111 #define DINO_IO_DATA            0x06c
112 #define DINO_MEM_DATA           0x070   /* Dino 3.x only */
113 #define DINO_GSC2X_CONFIG       0x7b4
114 #define DINO_GMASK              0x800
115 #define DINO_PAMR               0x804
116 #define DINO_PAPR               0x808
117 #define DINO_DAMODE             0x80c
118 #define DINO_PCICMD             0x810
119 #define DINO_PCISTS             0x814
120 #define DINO_MLTIM              0x81c
121 #define DINO_BRDG_FEAT          0x820
122 #define DINO_PCIROR             0x824
123 #define DINO_PCIWOR             0x828
124 #define DINO_TLTIM              0x830
125
126 #define DINO_IRQS 11            /* bits 0-10 are architected */
127 #define DINO_IRR_MASK   0x5ff   /* only 10 bits are implemented */
128
129 #define DINO_MASK_IRQ(x)        (1<<(x))
130
131 #define PCIINTA   0x001
132 #define PCIINTB   0x002
133 #define PCIINTC   0x004
134 #define PCIINTD   0x008
135 #define PCIINTE   0x010
136 #define PCIINTF   0x020
137 #define GSCEXTINT 0x040
138 /* #define xxx       0x080 - bit 7 is "default" */
139 /* #define xxx    0x100 - bit 8 not used */
140 /* #define xxx    0x200 - bit 9 not used */
141 #define RS232INT  0x400
142
143 struct dino_device
144 {
145         struct pci_hba_data     hba;    /* 'C' inheritance - must be first */
146         spinlock_t              dinosaur_pen;
147         unsigned long           txn_addr; /* EIR addr to generate interrupt */ 
148         u32                     txn_data; /* EIR data assign to each dino */ 
149         int                     irq;      /* Virtual IRQ dino uses */
150         struct irq_region       *dino_region;  /* region for this Dino */
151
152         u32                     imr; /* IRQ's which are enabled */ 
153 #ifdef DINO_DEBUG
154         unsigned int            dino_irr0; /* save most recent IRQ line stat */ 
155 #endif
156 };
157
158 /* Looks nice and keeps the compiler happy */
159 #define DINO_DEV(d) ((struct dino_device *) d)
160
161
162
163 /***********************************************
164 **
165 ** Dino Configuration Space Accessor Functions
166 **
167 ************************************************/
168
169 #define le8_to_cpu(x)   (x)
170 #define cpu_to_le8(x)   (x)
171
172 #define DINO_CFG_TOK(bus,dfn,pos) ((u32) ((bus)<<16 | (dfn)<<8 | (pos)))
173
174 #define DINO_CFG_RD(type, size, mask) \
175 static int dino_cfg_read##size (struct pci_dev *dev, int pos, u##size *data) \
176 { \
177         struct dino_device *d = DINO_DEV(dev->sysdata); \
178         u32 local_bus = (dev->bus->parent == NULL) ? 0 : dev->bus->secondary; \
179         u32 v = DINO_CFG_TOK(local_bus, dev->devfn, (pos&~3)); \
180         unsigned long flags; \
181         spin_lock_irqsave(&d->dinosaur_pen, flags); \
182         /* tell HW which CFG address */ \
183         gsc_writel(v, d->hba.base_addr + DINO_PCI_ADDR); \
184         /* generate cfg read cycle */ \
185         *data = le##size##_to_cpu(gsc_read##type(d->hba.base_addr+DINO_CONFIG_DATA+(pos&mask))); \
186         spin_unlock_irqrestore(&d->dinosaur_pen, flags); \
187         return 0; \
188 }
189
190 DINO_CFG_RD(b,  8, 3)
191 DINO_CFG_RD(w, 16, 2)
192 DINO_CFG_RD(l, 32, 0)
193
194
195 /*
196 ** Dino address stepping "feature":
197 ** When address stepping, Dino attempts to drive the bus one cycle too soon
198 ** even though the type of cycle (config vs. MMIO) might be different. 
199 ** The read of Ven/Prod ID is harmless and avoids Dino's address stepping.
200 */
201 #define DINO_CFG_WR(type, size, mask) \
202 static int dino_cfg_write##size (struct pci_dev *dev, int pos, u##size data) \
203 { \
204         struct dino_device *d = DINO_DEV(dev->sysdata); \
205         u32 local_bus = (dev->bus->parent == NULL) ? 0 : dev->bus->secondary; \
206         u32 v = DINO_CFG_TOK(local_bus, dev->devfn, (pos&~3)); \
207         unsigned long flags; \
208         spin_lock_irqsave(&d->dinosaur_pen, flags); \
209         /* avoid address stepping feature */ \
210         gsc_writel(v & 0xffffff00, d->hba.base_addr + DINO_PCI_ADDR); \
211         (volatile int) gsc_readl(d->hba.base_addr + DINO_CONFIG_DATA); \
212         /* tell HW which CFG address */ \
213         gsc_writel(v, d->hba.base_addr + DINO_PCI_ADDR); \
214         /* generate cfg read cycle */ \
215         gsc_write##type(cpu_to_le##size(data), d->hba.base_addr+DINO_CONFIG_DATA+(pos&mask)); \
216         spin_unlock_irqrestore(&d->dinosaur_pen, flags); \
217         return 0; \
218 }
219
220 DINO_CFG_WR(b,  8, 3)
221 DINO_CFG_WR(w, 16, 2)
222 DINO_CFG_WR(l, 32, 0)
223
224 static struct pci_ops dino_cfg_ops = {
225         read_byte:      dino_cfg_read8,
226         read_word:      dino_cfg_read16,
227         read_dword:     dino_cfg_read32,
228         write_byte:     dino_cfg_write8,
229         write_word:     dino_cfg_write16,
230         write_dword:    dino_cfg_write32
231 };
232
233
234
235 /*******************************************************
236 **
237 ** Dino "I/O Port" Space Accessor Functions
238 **
239 ** Many PCI devices don't require use of I/O port space (eg Tulip,
240 ** NCR720) since they export the same registers to both MMIO and
241 ** I/O port space.  Performance is going to stink if drivers use
242 ** I/O port instead of MMIO.
243 **
244 ********************************************************/
245
246
247 #define DINO_PORT_IN(type, size, mask) \
248 static u##size dino_in##size (struct pci_hba_data *d, u16 addr) \
249 { \
250         u##size v; \
251         unsigned long flags; \
252         spin_lock_irqsave(&(DINO_DEV(d)->dinosaur_pen), flags); \
253         /* tell HW which IO Port address */ \
254         gsc_writel((u32) addr & ~3, d->base_addr + DINO_PCI_ADDR); \
255         /* generate I/O PORT read cycle */ \
256         v = gsc_read##type(d->base_addr+DINO_IO_DATA+(addr&mask)); \
257         spin_unlock_irqrestore(&(DINO_DEV(d)->dinosaur_pen), flags); \
258         return le##size##_to_cpu(v); \
259 }
260
261 DINO_PORT_IN(b,  8, 3)
262 DINO_PORT_IN(w, 16, 2)
263 DINO_PORT_IN(l, 32, 0)
264
265 #define DINO_PORT_OUT(type, size, mask) \
266 static void dino_out##size (struct pci_hba_data *d, u16 addr, u##size val) \
267 { \
268         unsigned long flags; \
269         spin_lock_irqsave(&(DINO_DEV(d)->dinosaur_pen), flags); \
270         /* tell HW which CFG address */ \
271         gsc_writel((u32) addr, d->base_addr + DINO_PCI_ADDR); \
272         /* generate cfg write cycle */ \
273         gsc_write##type(cpu_to_le##size(val), d->base_addr+DINO_IO_DATA+(addr&mask)); \
274         spin_unlock_irqrestore(&(DINO_DEV(d)->dinosaur_pen), flags); \
275 }
276
277 DINO_PORT_OUT(b,  8, 3)
278 DINO_PORT_OUT(w, 16, 2)
279 DINO_PORT_OUT(l, 32, 0)
280
281 struct pci_port_ops dino_port_ops = {
282         inb:    dino_in8,
283         inw:    dino_in16,
284         inl:    dino_in32,
285         outb:   dino_out8,
286         outw:   dino_out16,
287         outl:   dino_out32
288 };
289
290 static void
291 dino_mask_irq(void *irq_dev, int irq)
292 {
293         struct dino_device *dino_dev = DINO_DEV(irq_dev);
294
295         DBG(KERN_WARNING "%s(0x%p, %d)\n", __FUNCTION__, irq_dev, irq);
296
297         if (NULL == irq_dev || irq > DINO_IRQS || irq < 0) {
298                 printk(KERN_WARNING "%s(0x%lx, %d) - not a dino irq?\n",
299                         __FUNCTION__, (long) irq_dev, irq);
300                 BUG();
301         } else {
302                 /*
303                 ** Clear the matching bit in the IMR register
304                 */
305                 dino_dev->imr &= ~(DINO_MASK_IRQ(irq));
306                 gsc_writel(dino_dev->imr, dino_dev->hba.base_addr+DINO_IMR);
307         }
308 }
309
310
311 static void
312 dino_unmask_irq(void *irq_dev, int irq)
313 {
314         struct dino_device *dino_dev = DINO_DEV(irq_dev);
315         u32 tmp;
316
317         DBG(KERN_WARNING "%s(0x%p, %d)\n", __FUNCTION__, irq_dev, irq);
318
319         if (NULL == irq_dev || irq > DINO_IRQS) {
320                 printk(KERN_WARNING "%s(): %d not a dino irq?\n",
321                                 __FUNCTION__, irq);
322                 BUG();
323                 return;
324         }
325
326         /* set the matching bit in the IMR register */
327         dino_dev->imr |= DINO_MASK_IRQ(irq);          /* used in dino_isr() */
328         gsc_writel( dino_dev->imr, dino_dev->hba.base_addr+DINO_IMR);
329
330         /* Emulate "Level Triggered" Interrupt
331         ** Basically, a driver is blowing it if the IRQ line is asserted
332         ** while the IRQ is disabled.  But tulip.c seems to do that....
333         ** Give 'em a kluge award and a nice round of applause!
334         **
335         ** The gsc_write will generate an interrupt which invokes dino_isr().
336         ** dino_isr() will read IPR and find nothing. But then catch this
337         ** when it also checks ILR.
338         */
339         tmp = gsc_readl(dino_dev->hba.base_addr+DINO_ILR);
340         if (tmp & DINO_MASK_IRQ(irq)) {
341                 DBG(KERN_WARNING "%s(): IRQ asserted! (ILR 0x%x)\n",
342                                 __FUNCTION__, tmp);
343                 gsc_writel(dino_dev->txn_data, dino_dev->txn_addr);
344         }
345 }
346
347
348
349 static void
350 dino_enable_irq(void *irq_dev, int irq)
351 {
352         struct dino_device *dino_dev = DINO_DEV(irq_dev);
353
354         /*
355         ** clear pending IRQ bits
356         **
357         ** This does NOT change ILR state!
358         ** See comments in dino_unmask_irq() for ILR usage.
359         */
360         gsc_readl(dino_dev->hba.base_addr+DINO_IPR);
361
362         dino_unmask_irq(irq_dev, irq);
363 }
364
365
366 static struct irq_region_ops dino_irq_ops = {
367         disable_irq:    dino_mask_irq,  /* ??? */
368         enable_irq:     dino_enable_irq, 
369         mask_irq:       dino_mask_irq,
370         unmask_irq:     dino_unmask_irq
371 };
372
373
374 /*
375  * Handle a Processor interrupt generated by Dino.
376  *
377  * ilr_loop counter is a kluge to prevent a "stuck" IRQ line from
378  * wedging the CPU. Could be removed or made optional at some point.
379  */
380 static void
381 dino_isr(int irq, void *intr_dev, struct pt_regs *regs)
382 {
383         struct dino_device *dino_dev = DINO_DEV(intr_dev);
384         u32 mask;
385         int ilr_loop = 100;
386         extern void do_irq(struct irqaction *a, int i, struct pt_regs *p);
387
388
389         /* read and acknowledge pending interrupts */
390 #ifdef DINO_DEBUG
391         dino_dev->dino_irr0 =
392 #endif
393         mask = gsc_readl(dino_dev->hba.base_addr+DINO_IRR0) & DINO_IRR_MASK;
394
395 ilr_again:
396         while (mask)
397         {
398                 int irq;
399
400                 /*
401                  * Perform a binary search on set bits.
402                  * `Less than Fatal' and PS2 interupts aren't supported.
403                  */
404                 if (mask & 0xf) {
405                         if (mask & 0x3) {
406                                 irq = (mask & 0x1) ? 0 : 1; /* PCI INT A, B */
407                         } else {
408                                 irq = (mask & 0x4) ? 2 : 3; /* PCI INT C, D */
409                         }
410                 } else {
411                         if (mask & 0x30) {
412                                 irq = (mask & 0x10) ? 4 : 5; /* PCI INT E, F */
413                         } else {
414                                 irq = (mask & 0x40) ? 6 : 10; /* GSC, RS232 */
415                         }
416                 }
417
418                 mask &= ~(1<<irq);
419
420                 DBG(KERN_WARNING "%s(%x, %p) mask %0x\n",
421                         __FUNCTION__, irq, intr_dev, mask);
422                 do_irq(&dino_dev->dino_region->action[irq],
423                         dino_dev->dino_region->data.irqbase + irq,
424                         regs);
425
426         }
427
428         /* Support for level triggered IRQ lines.
429         ** 
430         ** Dropping this support would make this routine *much* faster.
431         ** But since PCI requires level triggered IRQ line to share lines...
432         ** device drivers may assume lines are level triggered (and not
433         ** edge triggered like EISA/ISA can be).
434         */
435         mask = gsc_readl(dino_dev->hba.base_addr+DINO_ILR) & dino_dev->imr;
436         if (mask) {
437                 if (--ilr_loop > 0)
438                         goto ilr_again;
439                 printk("Dino %lx: IRQ base %d, stuck IRQ lines? 0x%x\n", dino_dev->hba.base_addr, dino_dev->dino_region->data.irqbase, mask);
440         }
441 }
442
443 static int dino_choose_irq(struct parisc_device *dev)
444 {
445         int irq = -1;
446
447         switch (dev->id.sversion) {
448                 case 0x00084:   irq =  8; break; /* PS/2 */
449                 case 0x0008c:   irq = 10; break; /* RS232 */
450                 case 0x00096:   irq =  8; break; /* PS/2 */
451         }
452
453         return irq;
454 }
455
456 static void __init
457 dino_bios_init(void)
458 {
459         DBG("dino_bios_init\n");
460 }
461
462 /*
463  * dino_card_setup - Set up the memory space for a Dino in card mode.
464  * @bus: the bus under this dino
465  *
466  * Claim an 8MB chunk of unused IO space and call the generic PCI routines
467  * to set up the addresses of the devices on this bus.
468  */
469 #define _8MB 0x00800000UL
470 static void __init
471 dino_card_setup(struct pci_bus *bus, unsigned long base_addr)
472 {
473         int i;
474         struct dino_device *dino_dev = DINO_DEV(bus->sysdata);
475         struct resource *res;
476
477         res = &dino_dev->hba.lmmio_space;
478         res->flags = IORESOURCE_MEM;
479
480         if (ccio_allocate_resource(dino_dev->hba.dev, res, _8MB,
481                                 (unsigned long) 0xfffffffff0000000UL | _8MB,
482                                 0xffffffffffffffffUL &~ _8MB, _8MB,
483                                 NULL, NULL) < 0) {
484                 printk(KERN_WARNING "Dino: Failed to allocate memory region\n");
485                 return;
486         }
487         bus->resource[1] = res;
488         bus->resource[0] = &(dino_dev->hba.io_space);
489
490         /* Now tell dino what range it has */
491         for (i = 1; i < 31; i++) {
492                 if (res->start == (0xfffffffff0000000UL | i * _8MB))
493                         break;
494         }
495         gsc_writel(1 << i, base_addr + DINO_IO_ADDR_EN);
496
497         pcibios_assign_unassigned_resources(bus);
498 }
499
500 static void __init
501 dino_card_fixup(struct pci_dev *dev)
502 {
503         u8 irq_pin;
504
505         /*
506         ** REVISIT: card-mode PCI-PCI expansion chassis do exist.
507         **         Not sure they were ever productized.
508         **         Die here since we'll die later in dino_inb() anyway.
509         */
510         if ((dev->class >> 8) == PCI_CLASS_BRIDGE_PCI) {
511                 panic("Card-Mode Dino: PCI-PCI Bridge not supported\n");
512         }
513
514         /*
515         ** Set Latency Timer to 0xff (not a shared bus)
516         ** Set CACHELINE_SIZE.
517         */
518         dino_cfg_write16(dev, PCI_CACHE_LINE_SIZE, 0xff00 | L1_CACHE_BYTES/4); 
519
520         /*
521         ** Program INT_LINE for card-mode devices.
522         ** The cards are hardwired according to this algorithm.
523         ** And it doesn't matter if PPB's are present or not since
524         ** the IRQ lines bypass the PPB.
525         **
526         ** "-1" converts INTA-D (1-4) to PCIINTA-D (0-3) range.
527         ** The additional "-1" adjusts for skewing the IRQ<->slot.
528         */
529         dino_cfg_read8(dev, PCI_INTERRUPT_PIN, &irq_pin); 
530         dev->irq = (irq_pin + PCI_SLOT(dev->devfn) - 1) % 4 ;
531
532         /* Shouldn't really need to do this but it's in case someone tries
533         ** to bypass PCI services and look at the card themselves.
534         */
535         dino_cfg_write8(dev, PCI_INTERRUPT_LINE, dev->irq); 
536 }
537
538
539 static void __init
540 dino_fixup_bus(struct pci_bus *bus)
541 {
542         struct list_head *ln;
543         struct pci_dev *dev;
544         struct dino_device *dino_dev = DINO_DEV(bus->sysdata);
545         int port_base = HBA_PORT_BASE(dino_dev->hba.hba_num);
546
547         DBG(KERN_WARNING "%s(0x%p) bus %d sysdata 0x%p\n",
548                         __FUNCTION__, bus, bus->secondary, bus->sysdata);
549
550         /* Firmware doesn't set up card-mode dino, so we have to */
551         if (is_card_dino(&dino_dev->hba.dev->id))
552                 dino_card_setup(bus, dino_dev->hba.base_addr);
553
554         /* If this is a PCI-PCI Bridge, read the window registers etc */
555         if (bus->self)
556                 pci_read_bridge_bases(bus);
557
558         list_for_each(ln, &bus->devices) {
559                 int i;
560
561                 dev = pci_dev_b(ln);
562                 if (is_card_dino(&dino_dev->hba.dev->id))
563                         dino_card_fixup(dev);
564
565                 /*
566                 ** P2PB's only have 2 BARs, no IRQs.
567                 ** I'd like to just ignore them for now.
568                 */
569                 if ((dev->class >> 8) == PCI_CLASS_BRIDGE_PCI)
570                         continue;
571
572                 /* Adjust the I/O Port space addresses */
573                 for (i = 0; i < PCI_NUM_RESOURCES; i++) {
574                         struct resource *res = &dev->resource[i];
575                         if (res->flags & IORESOURCE_IO) {
576                                 res->start |= port_base;
577                                 res->end |= port_base;
578                         }
579 #ifdef __LP64__
580                         /* Sign Extend MMIO addresses */
581                         else if (res->flags & IORESOURCE_MEM) {
582                                 res->start |= 0xffffffff00000000UL;
583                                 res->end   |= 0xffffffff00000000UL;
584                         }
585 #endif
586                 }
587
588                 /* Adjust INT_LINE for that busses region */
589                 dev->irq = dino_dev->dino_region->data.irqbase + dev->irq;
590         }
591 }
592
593
594 struct pci_bios_ops dino_bios_ops = {
595         dino_bios_init,
596         dino_fixup_bus  /* void dino_fixup_bus(struct pci_bus *bus) */
597 };
598
599
600 /*
601  *      Initialise a DINO controller chip
602  */
603 static void __init
604 dino_card_init(struct dino_device *dino_dev)
605 {
606         u32 brdg_feat = 0x00784e05;
607
608         gsc_writel(0x00000000, dino_dev->hba.base_addr+DINO_GMASK);
609         gsc_writel(0x00000001, dino_dev->hba.base_addr+DINO_IO_FBB_EN);
610         gsc_writel(0x00000000, dino_dev->hba.base_addr+DINO_ICR);
611
612 #if 1
613 /* REVISIT - should be a runtime check (eg if (CPU_IS_PCX_L) ...) */
614         /*
615         ** PCX-L processors don't support XQL like Dino wants it.
616         ** PCX-L2 ignore XQL signal and it doesn't matter.
617         */
618         brdg_feat &= ~0x4;      /* UXQL */
619 #endif
620         gsc_writel( brdg_feat, dino_dev->hba.base_addr+DINO_BRDG_FEAT);
621
622         /*
623         ** Don't enable address decoding until we know which I/O range
624         ** currently is available from the host. Only affects MMIO
625         ** and not I/O port space.
626         */
627         gsc_writel(0x00000000, dino_dev->hba.base_addr+DINO_IO_ADDR_EN);
628
629         gsc_writel(0x00000000, dino_dev->hba.base_addr+DINO_DAMODE);
630         gsc_writel(0x00222222, dino_dev->hba.base_addr+DINO_PCIROR);
631         gsc_writel(0x00222222, dino_dev->hba.base_addr+DINO_PCIWOR);
632
633         gsc_writel(0x00000040, dino_dev->hba.base_addr+DINO_MLTIM);
634         gsc_writel(0x00000080, dino_dev->hba.base_addr+DINO_IO_CONTROL);
635         gsc_writel(0x0000008c, dino_dev->hba.base_addr+DINO_TLTIM);
636
637         /* Disable PAMR before writing PAPR */
638         gsc_writel(0x0000007e, dino_dev->hba.base_addr+DINO_PAMR);
639         gsc_writel(0x0000007f, dino_dev->hba.base_addr+DINO_PAPR);
640         gsc_writel(0x00000000, dino_dev->hba.base_addr+DINO_PAMR);
641
642         /*
643         ** Dino ERS encourages enabling FBB (0x6f).
644         ** We can't until we know *all* devices below us can support it.
645         ** (Something in device configuration header tells us).
646         */
647         gsc_writel(0x0000004f, dino_dev->hba.base_addr+DINO_PCICMD);
648
649         /* Somewhere, the PCI spec says give devices 1 second
650         ** to recover from the #RESET being de-asserted.
651         ** Experience shows most devices only need 10ms.
652         ** This short-cut speeds up booting significantly.
653         */
654         mdelay(pci_post_reset_delay);
655 }
656
657 static int __init
658 dino_bridge_init(struct dino_device *dino_dev, const char *name)
659 {
660         unsigned long io_addr, bpos;
661         int result;
662         struct resource *res;
663         /*
664          * Decoding IO_ADDR_EN only works for Built-in Dino
665          * since PDC has already initialized this.
666          */
667
668         io_addr = gsc_readl(dino_dev->hba.base_addr + DINO_IO_ADDR_EN);
669         if (io_addr == 0) {
670                 printk(KERN_WARNING "%s: No PCI devices enabled.\n", name);
671                 return -ENODEV;
672         }
673
674         for (bpos = 0; (io_addr & (1 << bpos)) == 0; bpos++)
675                 ;
676
677         res = &dino_dev->hba.lmmio_space;
678         res->flags = IORESOURCE_MEM;
679
680         res->start = (unsigned long)(signed int)(0xf0000000 | (bpos << 23));
681         res->end = res->start + 8 * 1024 * 1024 - 1;
682
683         result = ccio_request_resource(dino_dev->hba.dev, res);
684         if (result < 0) {
685                 printk(KERN_ERR "%s: failed to claim PCI Bus address space!\n", name);
686                 return result;
687         }
688
689         return 0;
690 }
691
692 static int __init dino_common_init(struct parisc_device *dev,
693                 struct dino_device *dino_dev, const char *name)
694 {
695         int status;
696         u32 eim;
697         struct gsc_irq gsc_irq;
698         struct resource *res;
699
700         pcibios_register_hba(&dino_dev->hba);
701
702         pci_bios = &dino_bios_ops;   /* used by pci_scan_bus() */
703         pci_port = &dino_port_ops;
704
705         /*
706         ** Note: SMP systems can make use of IRR1/IAR1 registers
707         **   But it won't buy much performance except in very
708         **   specific applications/configurations. Note Dino
709         **   still only has 11 IRQ input lines - just map some of them
710         **   to a different processor.
711         */
712         dino_dev->irq = gsc_alloc_irq(&gsc_irq);
713         dino_dev->txn_addr = gsc_irq.txn_addr;
714         dino_dev->txn_data = gsc_irq.txn_data;
715         eim = ((u32) gsc_irq.txn_addr) | gsc_irq.txn_data;
716
717         /* 
718         ** Dino needs a PA "IRQ" to get a processor's attention.
719         ** arch/parisc/kernel/irq.c returns an EIRR bit.
720         */
721         if (dino_dev->irq < 0) {
722                 printk(KERN_WARNING "%s: gsc_alloc_irq() failed\n", name);
723                 return 1;
724         }
725
726         status = request_irq(dino_dev->irq, dino_isr, 0, name, dino_dev);
727         if (status) {
728                 printk(KERN_WARNING "%s: request_irq() failed with %d\n", 
729                         name, status);
730                 return 1;
731         }
732
733         /*
734         ** Tell generic interrupt support we have 11 bits which need
735         ** be checked in the interrupt handler.
736         */
737         dino_dev->dino_region = alloc_irq_region(DINO_IRQS, &dino_irq_ops,
738                                                 name, dino_dev);
739
740         if (NULL == dino_dev->dino_region) {
741                 printk(KERN_WARNING "%s: alloc_irq_region() failed\n", name);
742                 return 1;
743         }
744
745         /* Support the serial port which is sometimes attached on built-in
746          * Dino / Cujo chips.
747          */
748
749         fixup_child_irqs(dev, dino_dev->dino_region->data.irqbase,
750                         dino_choose_irq);
751
752         /*
753         ** This enables DINO to generate interrupts when it sees
754         ** any of it's inputs *change*. Just asserting an IRQ
755         ** before it's enabled (ie unmasked) isn't good enough.
756         */
757         gsc_writel(eim, dino_dev->hba.base_addr+DINO_IAR0);
758
759         /*
760         ** Some platforms don't clear Dino's IRR0 register at boot time.
761         ** Reading will clear it now.
762         */
763         gsc_readl(dino_dev->hba.base_addr+DINO_IRR0);
764
765         /* allocate I/O Port resource region */
766         res = &dino_dev->hba.io_space;
767         if (dev->id.hversion == 0x680 || is_card_dino(&dev->id)) {
768                 res->name = "Dino I/O Port";
769                 dino_dev->hba.lmmio_space.name = "Dino LMMIO";
770         } else {
771                 res->name = "Cujo I/O Port";
772                 dino_dev->hba.lmmio_space.name = "Cujo LMMIO";
773         }
774         res->start = HBA_PORT_BASE(dino_dev->hba.hba_num);
775         res->end = res->start + (HBA_PORT_SPACE_SIZE - 1);
776         res->flags = IORESOURCE_IO; /* do not mark it busy ! */
777         if (request_resource(&ioport_resource, res) < 0) {
778                 printk(KERN_ERR "%s: request I/O Port region failed 0x%lx/%lx (hpa 0x%lx)\n",
779                                 name, res->start, res->end, dino_dev->hba.base_addr);
780                 return 1;
781         }
782
783         return 0;
784 }
785
786 #define CUJO_RAVEN_ADDR         0xfffffffff1000000UL
787 #define CUJO_FIREHAWK_ADDR      0xfffffffff1604000UL
788 #define CUJO_RAVEN_BADPAGE      0x01003000UL
789 #define CUJO_FIREHAWK_BADPAGE   0x01607000UL
790
791 static const char *dino_vers[] = {
792         "2.0",
793         "2.1",
794         "3.0",
795         "3.1"
796 };
797
798 static const char *cujo_vers[] = {
799         "1.0",
800         "2.0"
801 };
802
803 void ccio_cujo20_fixup(struct parisc_device *dev, u32 iovp);
804
805 /*
806 ** Determine if dino should claim this chip (return 0) or not (return 1).
807 ** If so, initialize the chip appropriately (card-mode vs bridge mode).
808 ** Much of the initialization is common though.
809 */
810 static int __init
811 dino_driver_callback(struct parisc_device *dev)
812 {
813         struct dino_device *dino_dev;   // Dino specific control struct
814         const char *version = "unknown";
815         const char *name = "Dino";
816         int is_cujo = 0;
817
818         if (is_card_dino(&dev->id)) {
819                 version = "3.x (card mode)";
820         } else {
821                 if(dev->id.hversion == 0x680) {
822                         if (dev->id.hversion_rev < 4) {
823                                 version = dino_vers[dev->id.hversion_rev];
824                         }
825                 } else {
826                         name = "Cujo";
827                         is_cujo = 1;
828                         if (dev->id.hversion_rev < 2) {
829                                 version = cujo_vers[dev->id.hversion_rev];
830                         }
831                 }
832         }
833
834         printk("%s version %s found at 0x%lx\n", name, version, dev->hpa);
835
836         if (!request_mem_region(dev->hpa, PAGE_SIZE, name)) {
837                 printk(KERN_ERR "DINO: Hey! Someone took my MMIO space (0x%ld)!\n",
838                         dev->hpa);
839                 return 1;
840         }
841
842         /* Check for bugs */
843         if (is_cujo && dev->id.hversion_rev == 1) {
844 #ifdef CONFIG_IOMMU_CCIO
845                 printk(KERN_WARNING "Enabling Cujo 2.0 bug workaround\n");
846                 if (dev->hpa == CUJO_RAVEN_ADDR) {
847                         ccio_cujo20_fixup(dev->parent, CUJO_RAVEN_BADPAGE);
848                 } else if (dev->hpa == CUJO_FIREHAWK_ADDR) {
849                         ccio_cujo20_fixup(dev->parent, CUJO_FIREHAWK_BADPAGE);
850                 } else {
851                         printk("Don't recognise Cujo at address 0x%lx, not enabling workaround\n", dev->hpa);
852                 }
853 #endif
854         } else if (!is_cujo && !is_card_dino(&dev->id) &&
855                         dev->id.hversion_rev < 3) {
856                 printk(KERN_WARNING
857 "The GSCtoPCI (Dino hrev %d) bus converter found may exhibit\n"
858 "data corruption.  See Service Note Numbers: A4190A-01, A4191A-01.\n"
859 "Systems shipped after Aug 20, 1997 will not exhibit this problem.\n"
860 "Models affected: C180, C160, C160L, B160L, and B132L workstations.\n\n",
861                         dev->id.hversion_rev);
862 /* REVISIT: why are C200/C240 listed in the README table but not
863 **   "Models affected"? Could be an omission in the original literature.
864 */
865         }
866
867         dino_dev = kmalloc(sizeof(struct dino_device), GFP_KERNEL);
868         if (!dino_dev) {
869                 printk("dino_init_chip - couldn't alloc dino_device\n");
870                 return 1;
871         }
872
873         memset(dino_dev, 0, sizeof(struct dino_device));
874
875         dino_dev->hba.dev = dev;
876         dino_dev->hba.base_addr = dev->hpa;  /* faster access */
877         dino_dev->hba.lmmio_space_offset = 0;   /* CPU addrs == bus addrs */
878         dino_dev->dinosaur_pen = SPIN_LOCK_UNLOCKED;
879         dino_dev->hba.iommu = ccio_get_iommu(dev);
880
881         if (is_card_dino(&dev->id)) {
882                 dino_card_init(dino_dev);
883         } else {
884                 dino_bridge_init(dino_dev, name);
885         }
886
887         if (dino_common_init(dev, dino_dev, name))
888                 return 1;
889
890         /*
891         ** It's not used to avoid chicken/egg problems
892         ** with configuration accessor functions.
893         */
894         dino_dev->hba.hba_bus = pci_scan_bus(dino_dev->hba.hba_num,
895                         &dino_cfg_ops, dino_dev);
896
897         return 0;
898 }
899
900 /*
901  * Normally, we would just test sversion.  But the Elroy PCI adapter has
902  * the same sversion as Dino, so we have to check hversion as well.
903  * Unfortunately, the J2240 PDC reports the wrong hversion for the first
904  * Dino, so we have to test for Dino, Cujo and Dino-in-a-J2240.
905  */
906 static struct parisc_device_id dino_tbl[] = {
907         { HPHW_A_DMA, HVERSION_REV_ANY_ID, 0x004, 0x0009D }, /* Card-mode Dino. */
908         { HPHW_A_DMA, HVERSION_REV_ANY_ID, 0x444, 0x08080 }, /* Same card in a 715.  Bug? */
909         { HPHW_BRIDGE, HVERSION_REV_ANY_ID, 0x680, 0xa }, /* Bridge-mode Dino */
910         { HPHW_BRIDGE, HVERSION_REV_ANY_ID, 0x682, 0xa }, /* Bridge-mode Cujo */
911         { HPHW_BRIDGE, HVERSION_REV_ANY_ID, 0x05d, 0xa }, /* Dino in a J2240 */
912         { 0, }
913 };
914
915 static struct parisc_driver dino_driver = {
916         name:           "Dino",
917         id_table:       dino_tbl,
918         probe:          dino_driver_callback,
919 };
920
921 /*
922  * One time initialization to let the world know Dino is here.
923  * This is the only routine which is NOT static.
924  * Must be called exactly once before pci_init().
925  */
926 int __init dino_init(void)
927 {
928         register_parisc_driver(&dino_driver);
929         return 0;
930 }
931