Merge ../linux-2.6-watchdog-mm
[linux-drm-fsl-dcu.git] / arch / i386 / mach-visws / visws_apic.c
1 /*
2  *      linux/arch/i386/mach-visws/visws_apic.c
3  *
4  *      Copyright (C) 1999 Bent Hagemark, Ingo Molnar
5  *
6  *  SGI Visual Workstation interrupt controller
7  *
8  *  The Cobalt system ASIC in the Visual Workstation contains a "Cobalt" APIC
9  *  which serves as the main interrupt controller in the system.  Non-legacy
10  *  hardware in the system uses this controller directly.  Legacy devices
11  *  are connected to the PIIX4 which in turn has its 8259(s) connected to
12  *  a of the Cobalt APIC entry.
13  *
14  *  09/02/2000 - Updated for 2.4 by jbarnes@sgi.com
15  *
16  *  25/11/2002 - Updated for 2.5 by Andrey Panin <pazke@orbita1.ru>
17  */
18
19 #include <linux/kernel_stat.h>
20 #include <linux/interrupt.h>
21 #include <linux/smp_lock.h>
22 #include <linux/init.h>
23
24 #include <asm/io.h>
25 #include <asm/apic.h>
26 #include <asm/i8259.h>
27
28 #include "cobalt.h"
29 #include "irq_vectors.h"
30
31
32 static DEFINE_SPINLOCK(cobalt_lock);
33
34 /*
35  * Set the given Cobalt APIC Redirection Table entry to point
36  * to the given IDT vector/index.
37  */
38 static inline void co_apic_set(int entry, int irq)
39 {
40         co_apic_write(CO_APIC_LO(entry), CO_APIC_LEVEL | (irq + FIRST_EXTERNAL_VECTOR));
41         co_apic_write(CO_APIC_HI(entry), 0);
42 }
43
44 /*
45  * Cobalt (IO)-APIC functions to handle PCI devices.
46  */
47 static inline int co_apic_ide0_hack(void)
48 {
49         extern char visws_board_type;
50         extern char visws_board_rev;
51
52         if (visws_board_type == VISWS_320 && visws_board_rev == 5)
53                 return 5;
54         return CO_APIC_IDE0;
55 }
56
57 static int is_co_apic(unsigned int irq)
58 {
59         if (IS_CO_APIC(irq))
60                 return CO_APIC(irq);
61
62         switch (irq) {
63                 case 0: return CO_APIC_CPU;
64                 case CO_IRQ_IDE0: return co_apic_ide0_hack();
65                 case CO_IRQ_IDE1: return CO_APIC_IDE1;
66                 default: return -1;
67         }
68 }
69
70
71 /*
72  * This is the SGI Cobalt (IO-)APIC:
73  */
74
75 static void enable_cobalt_irq(unsigned int irq)
76 {
77         co_apic_set(is_co_apic(irq), irq);
78 }
79
80 static void disable_cobalt_irq(unsigned int irq)
81 {
82         int entry = is_co_apic(irq);
83
84         co_apic_write(CO_APIC_LO(entry), CO_APIC_MASK);
85         co_apic_read(CO_APIC_LO(entry));
86 }
87
88 /*
89  * "irq" really just serves to identify the device.  Here is where we
90  * map this to the Cobalt APIC entry where it's physically wired.
91  * This is called via request_irq -> setup_irq -> irq_desc->startup()
92  */
93 static unsigned int startup_cobalt_irq(unsigned int irq)
94 {
95         unsigned long flags;
96
97         spin_lock_irqsave(&cobalt_lock, flags);
98         if ((irq_desc[irq].status & (IRQ_DISABLED | IRQ_INPROGRESS | IRQ_WAITING)))
99                 irq_desc[irq].status &= ~(IRQ_DISABLED | IRQ_INPROGRESS | IRQ_WAITING);
100         enable_cobalt_irq(irq);
101         spin_unlock_irqrestore(&cobalt_lock, flags);
102         return 0;
103 }
104
105 static void ack_cobalt_irq(unsigned int irq)
106 {
107         unsigned long flags;
108
109         spin_lock_irqsave(&cobalt_lock, flags);
110         disable_cobalt_irq(irq);
111         apic_write(APIC_EOI, APIC_EIO_ACK);
112         spin_unlock_irqrestore(&cobalt_lock, flags);
113 }
114
115 static void end_cobalt_irq(unsigned int irq)
116 {
117         unsigned long flags;
118
119         spin_lock_irqsave(&cobalt_lock, flags);
120         if (!(irq_desc[irq].status & (IRQ_DISABLED | IRQ_INPROGRESS)))
121                 enable_cobalt_irq(irq);
122         spin_unlock_irqrestore(&cobalt_lock, flags);
123 }
124
125 static struct irq_chip cobalt_irq_type = {
126         .typename =     "Cobalt-APIC",
127         .startup =      startup_cobalt_irq,
128         .shutdown =     disable_cobalt_irq,
129         .enable =       enable_cobalt_irq,
130         .disable =      disable_cobalt_irq,
131         .ack =          ack_cobalt_irq,
132         .end =          end_cobalt_irq,
133 };
134
135
136 /*
137  * This is the PIIX4-based 8259 that is wired up indirectly to Cobalt
138  * -- not the manner expected by the code in i8259.c.
139  *
140  * there is a 'master' physical interrupt source that gets sent to
141  * the CPU. But in the chipset there are various 'virtual' interrupts
142  * waiting to be handled. We represent this to Linux through a 'master'
143  * interrupt controller type, and through a special virtual interrupt-
144  * controller. Device drivers only see the virtual interrupt sources.
145  */
146 static unsigned int startup_piix4_master_irq(unsigned int irq)
147 {
148         init_8259A(0);
149
150         return startup_cobalt_irq(irq);
151 }
152
153 static void end_piix4_master_irq(unsigned int irq)
154 {
155         unsigned long flags;
156
157         spin_lock_irqsave(&cobalt_lock, flags);
158         enable_cobalt_irq(irq);
159         spin_unlock_irqrestore(&cobalt_lock, flags);
160 }
161
162 static struct irq_chip piix4_master_irq_type = {
163         .typename =     "PIIX4-master",
164         .startup =      startup_piix4_master_irq,
165         .ack =          ack_cobalt_irq,
166         .end =          end_piix4_master_irq,
167 };
168
169
170 static struct irq_chip piix4_virtual_irq_type = {
171         .typename =     "PIIX4-virtual",
172         .shutdown =     disable_8259A_irq,
173         .enable =       enable_8259A_irq,
174         .disable =      disable_8259A_irq,
175 };
176
177
178 /*
179  * PIIX4-8259 master/virtual functions to handle interrupt requests
180  * from legacy devices: floppy, parallel, serial, rtc.
181  *
182  * None of these get Cobalt APIC entries, neither do they have IDT
183  * entries. These interrupts are purely virtual and distributed from
184  * the 'master' interrupt source: CO_IRQ_8259.
185  *
186  * When the 8259 interrupts its handler figures out which of these
187  * devices is interrupting and dispatches to its handler.
188  *
189  * CAREFUL: devices see the 'virtual' interrupt only. Thus disable/
190  * enable_irq gets the right irq. This 'master' irq is never directly
191  * manipulated by any driver.
192  */
193 static irqreturn_t piix4_master_intr(int irq, void *dev_id)
194 {
195         int realirq;
196         irq_desc_t *desc;
197         unsigned long flags;
198
199         spin_lock_irqsave(&i8259A_lock, flags);
200
201         /* Find out what's interrupting in the PIIX4 master 8259 */
202         outb(0x0c, 0x20);               /* OCW3 Poll command */
203         realirq = inb(0x20);
204
205         /*
206          * Bit 7 == 0 means invalid/spurious
207          */
208         if (unlikely(!(realirq & 0x80)))
209                 goto out_unlock;
210
211         realirq &= 7;
212
213         if (unlikely(realirq == 2)) {
214                 outb(0x0c, 0xa0);
215                 realirq = inb(0xa0);
216
217                 if (unlikely(!(realirq & 0x80)))
218                         goto out_unlock;
219
220                 realirq = (realirq & 7) + 8;
221         }
222
223         /* mask and ack interrupt */
224         cached_irq_mask |= 1 << realirq;
225         if (unlikely(realirq > 7)) {
226                 inb(0xa1);
227                 outb(cached_slave_mask, 0xa1);
228                 outb(0x60 + (realirq & 7), 0xa0);
229                 outb(0x60 + 2, 0x20);
230         } else {
231                 inb(0x21);
232                 outb(cached_master_mask, 0x21);
233                 outb(0x60 + realirq, 0x20);
234         }
235
236         spin_unlock_irqrestore(&i8259A_lock, flags);
237
238         desc = irq_desc + realirq;
239
240         /*
241          * handle this 'virtual interrupt' as a Cobalt one now.
242          */
243         kstat_cpu(smp_processor_id()).irqs[realirq]++;
244
245         if (likely(desc->action != NULL))
246                 handle_IRQ_event(realirq, desc->action);
247
248         if (!(desc->status & IRQ_DISABLED))
249                 enable_8259A_irq(realirq);
250
251         return IRQ_HANDLED;
252
253 out_unlock:
254         spin_unlock_irqrestore(&i8259A_lock, flags);
255         return IRQ_NONE;
256 }
257
258 static struct irqaction master_action = {
259         .handler =      piix4_master_intr,
260         .name =         "PIIX4-8259",
261 };
262
263 static struct irqaction cascade_action = {
264         .handler =      no_action,
265         .name =         "cascade",
266 };
267
268
269 void init_VISWS_APIC_irqs(void)
270 {
271         int i;
272
273         for (i = 0; i < CO_IRQ_APIC0 + CO_APIC_LAST + 1; i++) {
274                 irq_desc[i].status = IRQ_DISABLED;
275                 irq_desc[i].action = 0;
276                 irq_desc[i].depth = 1;
277
278                 if (i == 0) {
279                         irq_desc[i].chip = &cobalt_irq_type;
280                 }
281                 else if (i == CO_IRQ_IDE0) {
282                         irq_desc[i].chip = &cobalt_irq_type;
283                 }
284                 else if (i == CO_IRQ_IDE1) {
285                         irq_desc[i].chip = &cobalt_irq_type;
286                 }
287                 else if (i == CO_IRQ_8259) {
288                         irq_desc[i].chip = &piix4_master_irq_type;
289                 }
290                 else if (i < CO_IRQ_APIC0) {
291                         irq_desc[i].chip = &piix4_virtual_irq_type;
292                 }
293                 else if (IS_CO_APIC(i)) {
294                         irq_desc[i].chip = &cobalt_irq_type;
295                 }
296         }
297
298         setup_irq(CO_IRQ_8259, &master_action);
299         setup_irq(2, &cascade_action);
300 }