import of upstream 2.4.34.4 from kernel.org
[linux-2.4.git] / arch / mips / ddb5xxx / ddb5074 / nile4_pic.c
1 /*
2  *  arch/mips/ddb5476/nile4.c --
3  *      low-level PIC code for NEC Vrc-5476 (Nile 4)
4  *
5  *  Copyright (C) 2000 Geert Uytterhoeven <geert@sonycom.com>
6  *                     Sony Software Development Center Europe (SDCE), Brussels
7  *
8  *  Copyright 2001 MontaVista Software Inc.
9  *  Author: jsun@mvista.com or jsun@junsun.net
10  *
11  */
12 #include <linux/kernel.h>
13 #include <linux/types.h>
14 #include <linux/interrupt.h>
15 #include <linux/ioport.h>
16
17 #include <asm/addrspace.h>
18
19 #include <asm/ddb5xxx/ddb5xxx.h>
20
21 static int irq_base;
22
23 /*
24  *  Interrupt Programming
25  */
26 void nile4_map_irq(int nile4_irq, int cpu_irq)
27 {
28         u32 offset, t;
29
30         offset = DDB_INTCTRL;
31         if (nile4_irq >= 8) {
32                 offset += 4;
33                 nile4_irq -= 8;
34         }
35         t = ddb_in32(offset);
36         t &= ~(7 << (nile4_irq * 4));
37         t |= cpu_irq << (nile4_irq * 4);
38         ddb_out32(offset, t);
39 }
40
41 void nile4_map_irq_all(int cpu_irq)
42 {
43         u32 all, t;
44
45         all = cpu_irq;
46         all |= all << 4;
47         all |= all << 8;
48         all |= all << 16;
49         t = ddb_in32(DDB_INTCTRL);
50         t &= 0x88888888;
51         t |= all;
52         ddb_out32(DDB_INTCTRL, t);
53         t = ddb_in32(DDB_INTCTRL + 4);
54         t &= 0x88888888;
55         t |= all;
56         ddb_out32(DDB_INTCTRL + 4, t);
57 }
58
59 void nile4_enable_irq(unsigned int nile4_irq)
60 {
61         u32 offset, t;
62
63         nile4_irq-=irq_base;
64
65         ddb5074_led_hex(8);
66
67         offset = DDB_INTCTRL;
68         if (nile4_irq >= 8) {
69                 offset += 4;
70                 nile4_irq -= 8;
71         }
72         ddb5074_led_hex(9);
73         t = ddb_in32(offset);
74         ddb5074_led_hex(0xa);
75         t |= 8 << (nile4_irq * 4);
76         ddb_out32(offset, t);
77         ddb5074_led_hex(0xb);
78 }
79
80 void nile4_disable_irq(unsigned int nile4_irq)
81 {
82         u32 offset, t;
83
84         nile4_irq-=irq_base;
85
86         offset = DDB_INTCTRL;
87         if (nile4_irq >= 8) {
88                 offset += 4;
89                 nile4_irq -= 8;
90         }
91         t = ddb_in32(offset);
92         t &= ~(8 << (nile4_irq * 4));
93         ddb_out32(offset, t);
94 }
95
96 void nile4_disable_irq_all(void)
97 {
98         ddb_out32(DDB_INTCTRL, 0);
99         ddb_out32(DDB_INTCTRL + 4, 0);
100 }
101
102 u16 nile4_get_irq_stat(int cpu_irq)
103 {
104         return ddb_in16(DDB_INTSTAT0 + cpu_irq * 2);
105 }
106
107 void nile4_enable_irq_output(int cpu_irq)
108 {
109         u32 t;
110
111         t = ddb_in32(DDB_INTSTAT1 + 4);
112         t |= 1 << (16 + cpu_irq);
113         ddb_out32(DDB_INTSTAT1, t);
114 }
115
116 void nile4_disable_irq_output(int cpu_irq)
117 {
118         u32 t;
119
120         t = ddb_in32(DDB_INTSTAT1 + 4);
121         t &= ~(1 << (16 + cpu_irq));
122         ddb_out32(DDB_INTSTAT1, t);
123 }
124
125 void nile4_set_pci_irq_polarity(int pci_irq, int high)
126 {
127         u32 t;
128
129         t = ddb_in32(DDB_INTPPES);
130         if (high)
131                 t &= ~(1 << (pci_irq * 2));
132         else
133                 t |= 1 << (pci_irq * 2);
134         ddb_out32(DDB_INTPPES, t);
135 }
136
137 void nile4_set_pci_irq_level_or_edge(int pci_irq, int level)
138 {
139         u32 t;
140
141         t = ddb_in32(DDB_INTPPES);
142         if (level)
143                 t |= 2 << (pci_irq * 2);
144         else
145                 t &= ~(2 << (pci_irq * 2));
146         ddb_out32(DDB_INTPPES, t);
147 }
148
149 void nile4_clear_irq(int nile4_irq)
150 {
151         nile4_irq-=irq_base;
152         ddb_out32(DDB_INTCLR, 1 << nile4_irq);
153 }
154
155 void nile4_clear_irq_mask(u32 mask)
156 {
157         ddb_out32(DDB_INTCLR, mask);
158 }
159
160 u8 nile4_i8259_iack(void)
161 {
162         u8 irq;
163         u32 reg;
164
165         /* Set window 0 for interrupt acknowledge */
166         reg = ddb_in32(DDB_PCIINIT0);
167
168         ddb_set_pmr(DDB_PCIINIT0, DDB_PCICMD_IACK, 0, DDB_PCI_ACCESS_32);
169         irq = *(volatile u8 *) KSEG1ADDR(DDB_PCI_IACK_BASE);
170         /* restore window 0 for PCI I/O space */
171         // ddb_set_pmr(DDB_PCIINIT0, DDB_PCICMD_IO, 0, DDB_PCI_ACCESS_32);
172         ddb_out32(DDB_PCIINIT0, reg);
173
174         /* i8269.c set the base vector to be 0x0 */
175         return irq ;
176 }
177
178 static unsigned int nile4_irq_startup(unsigned int irq) {
179
180         nile4_enable_irq(irq);
181         return 0;
182
183 }
184
185 static void nile4_ack_irq(unsigned int irq) {
186
187     ddb5074_led_hex(4);
188
189         nile4_clear_irq(irq);
190     ddb5074_led_hex(2);
191         nile4_disable_irq(irq);
192
193     ddb5074_led_hex(0);
194 }
195
196 static void nile4_irq_end(unsigned int irq) {
197
198         ddb5074_led_hex(3);
199         if(!(irq_desc[irq].status & (IRQ_DISABLED | IRQ_INPROGRESS))) {
200         ddb5074_led_hex(5);
201                 nile4_enable_irq(irq);
202         ddb5074_led_hex(7);
203         }
204
205         ddb5074_led_hex(1);
206 }
207
208 #define nile4_irq_shutdown nile4_disable_irq
209
210 static hw_irq_controller nile4_irq_controller = {
211     "nile4",
212     nile4_irq_startup,
213     nile4_irq_shutdown,
214     nile4_enable_irq,
215     nile4_disable_irq,
216     nile4_ack_irq,
217     nile4_irq_end,
218     NULL
219 };
220
221 void nile4_irq_setup(u32 base) {
222
223         int i;
224         extern irq_desc_t irq_desc[];
225
226         irq_base=base;
227
228         /* Map all interrupts to CPU int #0 */
229         nile4_map_irq_all(0);
230
231         /* PCI INTA#-E# must be level triggered */
232         nile4_set_pci_irq_level_or_edge(0, 1);
233         nile4_set_pci_irq_level_or_edge(1, 1);
234         nile4_set_pci_irq_level_or_edge(2, 1);
235         nile4_set_pci_irq_level_or_edge(3, 1);
236         nile4_set_pci_irq_level_or_edge(4, 1);
237
238         /* PCI INTA#-D# must be active low, INTE# must be active high */
239         nile4_set_pci_irq_polarity(0, 0);
240         nile4_set_pci_irq_polarity(1, 0);
241         nile4_set_pci_irq_polarity(2, 0);
242         nile4_set_pci_irq_polarity(3, 0);
243         nile4_set_pci_irq_polarity(4, 1);
244
245
246         for (i = 0; i < 16; i++) {
247                 nile4_clear_irq(i);
248                 nile4_disable_irq(i);
249         }
250
251         /* Enable CPU int #0 */
252         nile4_enable_irq_output(0);
253
254         for (i= base; i< base + NUM_NILE4_INTERRUPTS; i++) {
255                 irq_desc[i].status = IRQ_DISABLED;
256                 irq_desc[i].action = NULL;
257                 irq_desc[i].depth = 1;
258                 irq_desc[i].handler = &nile4_irq_controller;
259         }
260
261 }
262
263 #if defined(CONFIG_RUNTIME_DEBUG)
264 void nile4_dump_irq_status(void)
265 {
266         printk(KERN_DEBUG "
267                CPUSTAT = %p:%p\n", (void *) ddb_in32(DDB_CPUSTAT + 4),
268                (void *) ddb_in32(DDB_CPUSTAT));
269         printk(KERN_DEBUG "
270                INTCTRL = %p:%p\n", (void *) ddb_in32(DDB_INTCTRL + 4),
271                (void *) ddb_in32(DDB_INTCTRL));
272         printk(KERN_DEBUG
273                "INTSTAT0 = %p:%p\n",
274                (void *) ddb_in32(DDB_INTSTAT0 + 4),
275                (void *) ddb_in32(DDB_INTSTAT0));
276         printk(KERN_DEBUG
277                "INTSTAT1 = %p:%p\n",
278                (void *) ddb_in32(DDB_INTSTAT1 + 4),
279                (void *) ddb_in32(DDB_INTSTAT1));
280         printk(KERN_DEBUG
281                "INTCLR = %p:%p\n", (void *) ddb_in32(DDB_INTCLR + 4),
282                (void *) ddb_in32(DDB_INTCLR));
283         printk(KERN_DEBUG
284                "INTPPES = %p:%p\n", (void *) ddb_in32(DDB_INTPPES + 4),
285                (void *) ddb_in32(DDB_INTPPES));
286 }
287
288 #endif