2c57ced5c68c14d19d9a63ff7b4d1b9879c58171
[firefly-linux-kernel-4.4.55.git] / arch / mips / tx4927 / common / tx4927_irq.c
1 /*
2  * Common tx4927 irq handler
3  *
4  * Author: MontaVista Software, Inc.
5  *         source@mvista.com
6  *
7  *  under the terms of the GNU General Public License as published by the
8  *  Free Software Foundation; either version 2 of the License, or (at your
9  *  option) any later version.
10  *
11  *  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
12  *  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
13  *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
14  *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
15  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
16  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
17  *  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
18  *  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
19  *  TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
20  *  USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
21  *
22  *  You should have received a copy of the GNU General Public License along
23  *  with this program; if not, write to the Free Software Foundation, Inc.,
24  *  675 Mass Ave, Cambridge, MA 02139, USA.
25  */
26 #include <linux/errno.h>
27 #include <linux/init.h>
28 #include <linux/kernel_stat.h>
29 #include <linux/module.h>
30 #include <linux/signal.h>
31 #include <linux/sched.h>
32 #include <linux/types.h>
33 #include <linux/interrupt.h>
34 #include <linux/ioport.h>
35 #include <linux/timex.h>
36 #include <linux/slab.h>
37 #include <linux/random.h>
38 #include <linux/irq.h>
39 #include <linux/bitops.h>
40 #include <asm/bootinfo.h>
41 #include <asm/io.h>
42 #include <asm/irq.h>
43 #include <asm/mipsregs.h>
44 #include <asm/system.h>
45 #include <asm/tx4927/tx4927.h>
46
47 /*
48  * DEBUG
49  */
50
51 #undef TX4927_IRQ_DEBUG
52
53 #ifdef TX4927_IRQ_DEBUG
54 #define TX4927_IRQ_NONE        0x00000000
55
56 #define TX4927_IRQ_INFO        ( 1 <<  0 )
57 #define TX4927_IRQ_WARN        ( 1 <<  1 )
58 #define TX4927_IRQ_EROR        ( 1 <<  2 )
59
60 #define TX4927_IRQ_INIT        ( 1 <<  5 )
61 #define TX4927_IRQ_NEST1       ( 1 <<  6 )
62 #define TX4927_IRQ_NEST2       ( 1 <<  7 )
63 #define TX4927_IRQ_NEST3       ( 1 <<  8 )
64 #define TX4927_IRQ_NEST4       ( 1 <<  9 )
65
66 #define TX4927_IRQ_CP0_INIT     ( 1 << 10 )
67 #define TX4927_IRQ_CP0_ENABLE   ( 1 << 13 )
68 #define TX4927_IRQ_CP0_DISABLE  ( 1 << 14 )
69 #define TX4927_IRQ_CP0_ENDIRQ   ( 1 << 16 )
70
71 #define TX4927_IRQ_PIC_INIT     ( 1 << 20 )
72 #define TX4927_IRQ_PIC_ENABLE   ( 1 << 23 )
73 #define TX4927_IRQ_PIC_DISABLE  ( 1 << 24 )
74 #define TX4927_IRQ_PIC_ENDIRQ   ( 1 << 26 )
75
76 #define TX4927_IRQ_ALL         0xffffffff
77 #endif
78
79 #ifdef TX4927_IRQ_DEBUG
80 static const u32 tx4927_irq_debug_flag = (TX4927_IRQ_NONE
81                                           | TX4927_IRQ_INFO
82                                           | TX4927_IRQ_WARN | TX4927_IRQ_EROR
83 //                                       | TX4927_IRQ_CP0_INIT
84 //                                       | TX4927_IRQ_CP0_ENABLE
85 //                                       | TX4927_IRQ_CP0_DISABLE
86 //                                       | TX4927_IRQ_CP0_ENDIRQ
87 //                                       | TX4927_IRQ_PIC_INIT
88 //                                       | TX4927_IRQ_PIC_ENABLE
89 //                                       | TX4927_IRQ_PIC_DISABLE
90 //                                       | TX4927_IRQ_PIC_ENDIRQ
91 //                                       | TX4927_IRQ_INIT
92 //                                       | TX4927_IRQ_NEST1
93 //                                       | TX4927_IRQ_NEST2
94 //                                       | TX4927_IRQ_NEST3
95 //                                       | TX4927_IRQ_NEST4
96     );
97 #endif
98
99 #ifdef TX4927_IRQ_DEBUG
100 #define TX4927_IRQ_DPRINTK(flag,str...) \
101         if ( (tx4927_irq_debug_flag) & (flag) ) \
102         { \
103            char tmp[100]; \
104            sprintf( tmp, str ); \
105            printk( "%s(%s:%u)::%s", __FUNCTION__, __FILE__, __LINE__, tmp ); \
106         }
107 #else
108 #define TX4927_IRQ_DPRINTK(flag,str...)
109 #endif
110
111 /*
112  * Forwad definitions for all pic's
113  */
114
115 static void tx4927_irq_cp0_enable(unsigned int irq);
116 static void tx4927_irq_cp0_disable(unsigned int irq);
117 static void tx4927_irq_cp0_end(unsigned int irq);
118
119 static void tx4927_irq_pic_enable(unsigned int irq);
120 static void tx4927_irq_pic_disable(unsigned int irq);
121 static void tx4927_irq_pic_end(unsigned int irq);
122
123 /*
124  * Kernel structs for all pic's
125  */
126
127 #define TX4927_CP0_NAME "TX4927-CP0"
128 static struct irq_chip tx4927_irq_cp0_type = {
129         .typename       = TX4927_CP0_NAME,
130         .ack            = tx4927_irq_cp0_disable,
131         .mask           = tx4927_irq_cp0_disable,
132         .mask_ack       = tx4927_irq_cp0_disable,
133         .unmask         = tx4927_irq_cp0_enable,
134         .end            = tx4927_irq_cp0_end,
135 };
136
137 #define TX4927_PIC_NAME "TX4927-PIC"
138 static struct irq_chip tx4927_irq_pic_type = {
139         .typename       = TX4927_PIC_NAME,
140         .ack            = tx4927_irq_pic_disable,
141         .mask           = tx4927_irq_pic_disable,
142         .mask_ack       = tx4927_irq_pic_disable,
143         .unmask         = tx4927_irq_pic_enable,
144         .end            = tx4927_irq_pic_end,
145 };
146
147 #define TX4927_PIC_ACTION(s) { no_action, 0, CPU_MASK_NONE, s, NULL, NULL }
148 static struct irqaction tx4927_irq_pic_action =
149 TX4927_PIC_ACTION(TX4927_PIC_NAME);
150
151 #define CCP0_STATUS 12
152 #define CCP0_CAUSE 13
153
154 /*
155  * Functions for cp0
156  */
157
158 #define tx4927_irq_cp0_mask(irq) ( 1 << ( irq-TX4927_IRQ_CP0_BEG+8 ) )
159
160 static void
161 tx4927_irq_cp0_modify(unsigned cp0_reg, unsigned clr_bits, unsigned set_bits)
162 {
163         unsigned long val = 0;
164
165         switch (cp0_reg) {
166         case CCP0_STATUS:
167                 val = read_c0_status();
168                 break;
169
170         case CCP0_CAUSE:
171                 val = read_c0_cause();
172                 break;
173
174         }
175
176         val &= (~clr_bits);
177         val |= (set_bits);
178
179         switch (cp0_reg) {
180         case CCP0_STATUS:{
181                         write_c0_status(val);
182                         break;
183                 }
184         case CCP0_CAUSE:{
185                         write_c0_cause(val);
186                         break;
187                 }
188         }
189 }
190
191 static void __init tx4927_irq_cp0_init(void)
192 {
193         int i;
194
195         TX4927_IRQ_DPRINTK(TX4927_IRQ_CP0_INIT, "beg=%d end=%d\n",
196                            TX4927_IRQ_CP0_BEG, TX4927_IRQ_CP0_END);
197
198         for (i = TX4927_IRQ_CP0_BEG; i <= TX4927_IRQ_CP0_END; i++)
199                 set_irq_chip(i, &tx4927_irq_cp0_type);
200 }
201
202 static void tx4927_irq_cp0_enable(unsigned int irq)
203 {
204         TX4927_IRQ_DPRINTK(TX4927_IRQ_CP0_ENABLE, "irq=%d \n", irq);
205
206         tx4927_irq_cp0_modify(CCP0_STATUS, 0, tx4927_irq_cp0_mask(irq));
207 }
208
209 static void tx4927_irq_cp0_disable(unsigned int irq)
210 {
211         TX4927_IRQ_DPRINTK(TX4927_IRQ_CP0_DISABLE, "irq=%d \n", irq);
212
213         tx4927_irq_cp0_modify(CCP0_STATUS, tx4927_irq_cp0_mask(irq), 0);
214 }
215
216 static void tx4927_irq_cp0_end(unsigned int irq)
217 {
218         TX4927_IRQ_DPRINTK(TX4927_IRQ_CP0_ENDIRQ, "irq=%d \n", irq);
219
220         if (!(irq_desc[irq].status & (IRQ_DISABLED | IRQ_INPROGRESS))) {
221                 tx4927_irq_cp0_enable(irq);
222         }
223 }
224
225 /*
226  * Functions for pic
227  */
228 u32 tx4927_irq_pic_addr(int irq)
229 {
230         /* MVMCP -- need to formulize this */
231         irq -= TX4927_IRQ_PIC_BEG;
232         switch (irq) {
233         case 17:
234         case 16:
235         case 1:
236         case 0:
237                 return (0xff1ff610);
238
239         case 19:
240         case 18:
241         case 3:
242         case 2:
243                 return (0xff1ff614);
244
245         case 21:
246         case 20:
247         case 5:
248         case 4:
249                 return (0xff1ff618);
250
251         case 23:
252         case 22:
253         case 7:
254         case 6:
255                 return (0xff1ff61c);
256
257         case 25:
258         case 24:
259         case 9:
260         case 8:
261                 return (0xff1ff620);
262
263         case 27:
264         case 26:
265         case 11:
266         case 10:
267                 return (0xff1ff624);
268
269         case 29:
270         case 28:
271         case 13:
272         case 12:
273                 return (0xff1ff628);
274
275         case 31:
276         case 30:
277         case 15:
278         case 14:
279                 return (0xff1ff62c);
280
281         }
282         return (0);
283 }
284
285 u32 tx4927_irq_pic_mask(int irq)
286 {
287         /* MVMCP -- need to formulize this */
288         irq -= TX4927_IRQ_PIC_BEG;
289         switch (irq) {
290         case 31:
291         case 29:
292         case 27:
293         case 25:
294         case 23:
295         case 21:
296         case 19:
297         case 17:{
298                         return (0x07000000);
299                 }
300         case 30:
301         case 28:
302         case 26:
303         case 24:
304         case 22:
305         case 20:
306         case 18:
307         case 16:{
308                         return (0x00070000);
309                 }
310         case 15:
311         case 13:
312         case 11:
313         case 9:
314         case 7:
315         case 5:
316         case 3:
317         case 1:{
318                         return (0x00000700);
319                 }
320         case 14:
321         case 12:
322         case 10:
323         case 8:
324         case 6:
325         case 4:
326         case 2:
327         case 0:{
328                         return (0x00000007);
329                 }
330         }
331         return (0x00000000);
332 }
333
334 static void tx4927_irq_pic_modify(unsigned pic_reg, unsigned clr_bits,
335         unsigned set_bits)
336 {
337         unsigned long val = 0;
338
339         val = TX4927_RD(pic_reg);
340         val &= (~clr_bits);
341         val |= (set_bits);
342         TX4927_WR(pic_reg, val);
343 }
344
345 static void __init tx4927_irq_pic_init(void)
346 {
347         int i;
348
349         TX4927_IRQ_DPRINTK(TX4927_IRQ_PIC_INIT, "beg=%d end=%d\n",
350                            TX4927_IRQ_PIC_BEG, TX4927_IRQ_PIC_END);
351
352         for (i = TX4927_IRQ_PIC_BEG; i <= TX4927_IRQ_PIC_END; i++)
353                 set_irq_chip(i, &tx4927_irq_pic_type);
354
355         setup_irq(TX4927_IRQ_NEST_PIC_ON_CP0, &tx4927_irq_pic_action);
356
357         TX4927_WR(0xff1ff640, 0x6);     /* irq level mask -- only accept hightest */
358         TX4927_WR(0xff1ff600, TX4927_RD(0xff1ff600) | 0x1);     /* irq enable */
359 }
360
361 static void tx4927_irq_pic_enable(unsigned int irq)
362 {
363         TX4927_IRQ_DPRINTK(TX4927_IRQ_PIC_ENABLE, "irq=%d\n", irq);
364
365         tx4927_irq_pic_modify(tx4927_irq_pic_addr(irq), 0,
366                               tx4927_irq_pic_mask(irq));
367 }
368
369 static void tx4927_irq_pic_disable(unsigned int irq)
370 {
371         TX4927_IRQ_DPRINTK(TX4927_IRQ_PIC_DISABLE, "irq=%d\n", irq);
372
373         tx4927_irq_pic_modify(tx4927_irq_pic_addr(irq),
374                               tx4927_irq_pic_mask(irq), 0);
375 }
376
377 static void tx4927_irq_pic_end(unsigned int irq)
378 {
379         TX4927_IRQ_DPRINTK(TX4927_IRQ_PIC_ENDIRQ, "irq=%d\n", irq);
380
381         if (!(irq_desc[irq].status & (IRQ_DISABLED | IRQ_INPROGRESS))) {
382                 tx4927_irq_pic_enable(irq);
383         }
384 }
385
386 /*
387  * Main init functions
388  */
389 void __init tx4927_irq_init(void)
390 {
391         TX4927_IRQ_DPRINTK(TX4927_IRQ_INIT, "-\n");
392
393         TX4927_IRQ_DPRINTK(TX4927_IRQ_INIT, "=Calling tx4927_irq_cp0_init()\n");
394         tx4927_irq_cp0_init();
395
396         TX4927_IRQ_DPRINTK(TX4927_IRQ_INIT, "=Calling tx4927_irq_pic_init()\n");
397         tx4927_irq_pic_init();
398
399         TX4927_IRQ_DPRINTK(TX4927_IRQ_INIT, "+\n");
400 }
401
402 static int tx4927_irq_nested(void)
403 {
404         int sw_irq = 0;
405         u32 level2;
406
407         TX4927_IRQ_DPRINTK(TX4927_IRQ_NEST1, "-\n");
408
409         level2 = TX4927_RD(0xff1ff6a0);
410         TX4927_IRQ_DPRINTK(TX4927_IRQ_NEST2, "=level2a=0x%x\n", level2);
411
412         if ((level2 & 0x10000) == 0) {
413                 level2 &= 0x1f;
414                 TX4927_IRQ_DPRINTK(TX4927_IRQ_NEST3, "=level2b=0x%x\n", level2);
415
416                 sw_irq = TX4927_IRQ_PIC_BEG + level2;
417                 TX4927_IRQ_DPRINTK(TX4927_IRQ_NEST3, "=sw_irq=%d\n", sw_irq);
418
419                 if (sw_irq == 27) {
420                         TX4927_IRQ_DPRINTK(TX4927_IRQ_NEST4, "=irq-%d\n",
421                                            sw_irq);
422
423 #ifdef CONFIG_TOSHIBA_RBTX4927
424                         {
425                                 sw_irq = toshiba_rbtx4927_irq_nested(sw_irq);
426                         }
427 #endif
428
429                         TX4927_IRQ_DPRINTK(TX4927_IRQ_NEST4, "=irq+%d\n",
430                                            sw_irq);
431                 }
432         }
433
434         TX4927_IRQ_DPRINTK(TX4927_IRQ_NEST2, "=sw_irq=%d\n", sw_irq);
435
436         TX4927_IRQ_DPRINTK(TX4927_IRQ_NEST1, "+\n");
437
438         return (sw_irq);
439 }
440
441 asmlinkage void plat_irq_dispatch(void)
442 {
443         unsigned int pending = read_c0_status() & read_c0_cause();
444
445         if (pending & STATUSF_IP7)                      /* cpu timer */
446                 do_IRQ(TX4927_IRQ_CPU_TIMER);
447         else if (pending & STATUSF_IP2) {               /* tx4927 pic */
448                 unsigned int irq = tx4927_irq_nested();
449
450                 if (unlikely(irq == 0)) {
451                         spurious_interrupt();
452                         return;
453                 }
454                 do_IRQ(irq);
455         } else if (pending & STATUSF_IP0)               /* user line 0 */
456                 do_IRQ(TX4927_IRQ_USER0);
457         else if (pending & STATUSF_IP1)                 /* user line 1 */
458                 do_IRQ(TX4927_IRQ_USER1);
459         else
460                 spurious_interrupt();
461 }