added mtd driver
[linux-2.4.git] / drivers / gsc / gsc.c
1 /*
2  * Interrupt management for most GSC and related devices.
3  *
4  * (c) Copyright 1999 Alex deVries for The Puffin Group
5  * (c) Copyright 1999 Grant Grundler for Hewlett-Packard
6  * (c) Copyright 1999 Matthew Wilcox
7  * (c) Copyright 2000 Helge Deller
8  * (c) Copyright 2001 Matthew Wilcox for Hewlett-Packard
9  *
10  *      This program is free software; you can redistribute it and/or modify
11  *      it under the terms of the GNU General Public License as published by
12  *      the Free Software Foundation; either version 2 of the License, or
13  *      (at your option) any later version.
14  */
15
16 #include <linux/bitops.h>
17 #include <linux/config.h>
18 #include <linux/errno.h>
19 #include <linux/init.h>
20 #include <linux/interrupt.h>
21 #include <linux/ioport.h>
22 #include <linux/slab.h>
23 #include <linux/types.h>
24
25 #include <asm/gsc.h>
26 #include <asm/hardware.h>
27 #include <asm/io.h>
28 #include <asm/irq.h>
29
30 #include "busdevice.h"
31
32 #undef DEBUG
33
34 #ifdef DEBUG
35 #define DEBPRINTK printk
36 #else
37 #define DEBPRINTK(x,...)
38 #endif
39
40 int gsc_alloc_irq(struct gsc_irq *i)
41 {
42         int irq = txn_alloc_irq();
43         if (irq < 0) {
44                 printk("cannot get irq\n");
45                 return irq;
46         }
47
48         i->txn_addr = txn_alloc_addr(irq);
49         i->txn_data = txn_alloc_data(irq, GSC_EIM_WIDTH);
50         i->irq = irq;
51
52         return irq;
53 }
54
55
56 int gsc_claim_irq(struct gsc_irq *i, int irq)
57 {
58         int c = irq;
59
60         irq += IRQ_FROM_REGION(CPU_IRQ_REGION); /* virtualize the IRQ first */
61
62         irq = txn_claim_irq(irq);
63         if (irq < 0) {
64                 printk("cannot claim irq %d\n", c);
65                 return irq;
66         }
67
68         i->txn_addr = txn_alloc_addr(irq);
69         i->txn_data = txn_alloc_data(irq, GSC_EIM_WIDTH);
70         i->irq = irq;
71
72         return irq;
73 }
74
75
76 /* IRQ bits must be numbered from Most Significant Bit */
77 #define GSC_FIX_IRQ(x)  (31-(x))
78 #define GSC_MASK_IRQ(x) (1<<(GSC_FIX_IRQ(x)))
79
80 /* Common interrupt demultiplexer used by Asp, Lasi & Wax.  */
81 void busdev_barked(int busdev_irq, void *dev, struct pt_regs *regs)
82 {
83         unsigned long irq;
84         struct busdevice *busdev = (struct busdevice *) dev;
85
86         /* 
87             Don't need to protect OFFSET_IRR with spinlock since this is
88             the only place it's touched.
89             Protect busdev_region by disabling this region's interrupts,
90             modifying the region, and then re-enabling the region.
91         */
92
93         irq = gsc_readl(busdev->hpa+OFFSET_IRR);
94         if (irq == 0) {
95                 printk(KERN_ERR "%s: barking without apparent reason.\n", busdev->name);
96         } else {
97                 DEBPRINTK ("%s (0x%x) barked, mask=0x%x, irq=%d\n", 
98                     busdev->name, busdev->busdev_region->data.irqbase, 
99                     irq, GSC_FIX_IRQ(ffs(irq))+1 );
100
101                 do_irq_mask(irq, busdev->busdev_region, regs);
102         }
103 }
104
105 static void
106 busdev_disable_irq(void *irq_dev, int irq)
107 {
108         /* Disable the IRQ line by clearing the bit in the IMR */
109         u32 imr = gsc_readl(BUSDEV_DEV(irq_dev)->hpa+OFFSET_IMR);
110         imr &= ~(GSC_MASK_IRQ(irq));
111
112         DEBPRINTK( KERN_WARNING "%s(%p, %d) %s: IMR 0x%x\n", 
113                     __FUNCTION__, irq_dev, irq, BUSDEV_DEV(irq_dev)->name, imr);
114
115         gsc_writel(imr, BUSDEV_DEV(irq_dev)->hpa+OFFSET_IMR);
116 }
117
118
119 static void
120 busdev_enable_irq(void *irq_dev, int irq)
121 {
122         /* Enable the IRQ line by setting the bit in the IMR */
123         unsigned long addr = BUSDEV_DEV(irq_dev)->hpa + OFFSET_IMR;
124         u32 imr = gsc_readl(addr);
125         imr |= GSC_MASK_IRQ(irq);
126
127         DEBPRINTK (KERN_WARNING "%s(%p, %d) %s: IMR 0x%x\n", 
128                     __FUNCTION__, irq_dev, irq, BUSDEV_DEV(irq_dev)->name, imr);
129
130         gsc_writel(imr, addr);
131 //      gsc_writel(~0L, addr);
132
133 /* FIXME: read IPR to make sure the IRQ isn't already pending.
134 **   If so, we need to read IRR and manually call do_irq_mask().
135 **   This code should be shared with busdev_unmask_irq().
136 */
137 }
138
139 static void
140 busdev_mask_irq(void *irq_dev, int irq)
141 {
142 /* FIXME: Clear the IMR bit in busdev for that IRQ */
143 }
144
145 static void
146 busdev_unmask_irq(void *irq_dev, int irq)
147 {
148 /* FIXME: Read IPR. Set the IMR bit in busdev for that IRQ.
149    call do_irq_mask() if IPR is non-zero
150 */
151 }
152
153 struct irq_region_ops busdev_irq_ops = {
154         disable_irq:    busdev_disable_irq,
155         enable_irq:     busdev_enable_irq,
156         mask_irq:       busdev_mask_irq,
157         unmask_irq:     busdev_unmask_irq
158 };
159
160
161 int gsc_common_irqsetup(struct parisc_device *parent, struct busdevice *busdev)
162 {
163         struct resource *res;
164
165         busdev->gsc = parent;
166
167         /* the IRQs we simulate */
168         busdev->busdev_region = alloc_irq_region(32, &busdev_irq_ops,
169                                                  busdev->name, busdev);
170         if (!busdev->busdev_region)
171                 return -ENOMEM;
172
173         /* allocate resource region */
174         res = request_mem_region(busdev->hpa, 0x100000, busdev->name);
175         if (res) {
176                 res->flags = IORESOURCE_MEM;    /* do not mark it busy ! */
177         }
178
179 #if 0
180         printk(KERN_WARNING "%s IRQ %d EIM 0x%x", busdev->name,
181                         busdev->parent_irq, busdev->eim);
182         if (gsc_readl(busdev->hpa + OFFSET_IMR))
183                 printk("  IMR is non-zero! (0x%x)",
184                                 gsc_readl(busdev->hpa + OFFSET_IMR));
185         printk("\n");
186 #endif
187
188         return 0;
189 }
190
191 extern struct parisc_driver lasi_driver;
192 extern struct parisc_driver asp_driver;
193 extern struct parisc_driver wax_driver;
194
195 void __init gsc_init(void)
196 {
197 #ifdef CONFIG_GSC_LASI
198         register_parisc_driver(&lasi_driver);
199         register_parisc_driver(&asp_driver);
200 #endif
201 #ifdef CONFIG_GSC_WAX
202         register_parisc_driver(&wax_driver);
203 #endif
204 }