1 /*
2  * Interrupt handle for GS232
3  *
4  * Copyright (c) 2006-2021, RT-Thread Development Team
5  *
6  * SPDX-License-Identifier: Apache-2.0
7  *
8  * Change Logs:
9  * Date           Author       Notes
10  * 2010-10-15     Bernard      first version
11  * 2010-10-15     lgnq         modified for LS1B
12  * 2013-03-29     aozima       Modify the interrupt interface implementations.
13  * 2015-07-06     chinesebear  modified for loongson 1c
14  * 2019-12-04     Jiaxun Yang  Generialize
15  */
16 
17 #include <rtthread.h>
18 #include <rthw.h>
19 #include "gs232.h"
20 
21 
22 #define MAX_INTR            (GS232_NR_IRQS)
23 
24 static struct rt_irq_desc irq_handle_table[MAX_INTR];
25 void rt_hw_timer_handler();
26 
27 static struct gs232_intc_regs volatile *gs232_hw0_icregs
28 = (struct gs232_intc_regs volatile *)(INTC_BASE);
29 
30 /**
31  * @addtogroup Loongson GS232
32  */
33 
34 /*@{*/
35 
rt_hw_interrupt_handler(int vector,void * param)36 static void rt_hw_interrupt_handler(int vector, void *param)
37 {
38     rt_kprintf("Unhandled interrupt %d occured!!!\n", vector);
39 }
40 
41 /**
42  * This function will initialize hardware interrupt
43  */
rt_hw_interrupt_init(void)44 void rt_hw_interrupt_init(void)
45 {
46     rt_int32_t idx;
47     rt_int32_t i;
48     rt_uint32_t c0_status = 0;
49 
50     for (i=0; i < GS232_INTC_CELLS; i++)
51     {
52         /* Disable */
53         (gs232_hw0_icregs+i)->int_en = 0x0;
54         /* Trigger active low */
55         (gs232_hw0_icregs+i)->int_pol = -1;    /* Must be done here */
56         /* Make all interrupts level triggered */
57         (gs232_hw0_icregs+i)->int_edge = 0x00000000;
58         /* Mask all interrupts */
59         (gs232_hw0_icregs+i)->int_clr = 0xffffffff;
60         mips_unmask_cpu_irq(i + 2);
61     }
62 
63     rt_memset(irq_handle_table, 0x00, sizeof(irq_handle_table));
64     for (idx = 0; idx < MAX_INTR; idx ++)
65     {
66         irq_handle_table[idx].handler = rt_hw_interrupt_handler;
67     }
68 }
69 
70 /**
71  * This function will mask a interrupt.
72  * @param vector the interrupt number
73  */
rt_hw_interrupt_mask(int vector)74 void rt_hw_interrupt_mask(int vector)
75 {
76     /* mask interrupt */
77     (gs232_hw0_icregs+(vector>>5))->int_en &= ~(1 << (vector&0x1f));
78 }
79 
80 /**
81  * This function will un-mask a interrupt.
82  * @param vector the interrupt number
83  */
rt_hw_interrupt_umask(int vector)84 void rt_hw_interrupt_umask(int vector)
85 {
86     (gs232_hw0_icregs+(vector>>5))->int_en |= (1 << (vector&0x1f));
87 }
88 
89 /**
90  * This function will install a interrupt service routine to a interrupt.
91  * @param vector the interrupt number
92  * @param new_handler the interrupt service routine to be installed
93  * @param old_handler the old interrupt service routine
94  */
rt_hw_interrupt_install(int vector,rt_isr_handler_t handler,void * param,const char * name)95 rt_isr_handler_t rt_hw_interrupt_install(int vector, rt_isr_handler_t handler,
96                                          void *param, const char *name)
97 {
98     rt_isr_handler_t old_handler = RT_NULL;
99 
100     if (vector >= 0 && vector < MAX_INTR)
101     {
102         old_handler = irq_handle_table[vector].handler;
103 
104 #ifdef RT_USING_INTERRUPT_INFO
105         rt_strncpy(irq_handle_table[vector].name, name, RT_NAME_MAX);
106 #endif /* RT_USING_INTERRUPT_INFO */
107         irq_handle_table[vector].handler = handler;
108         irq_handle_table[vector].param = param;
109     }
110 
111     return old_handler;
112 }
113 
114 
115 /**
116  * Call ISR
117  * @IRQn ID of IRQ
118  */
gs232_do_IRQ(int IRQn)119 void gs232_do_IRQ(int IRQn)
120 {
121     rt_isr_handler_t irq_func;
122     void *param;
123 
124     irq_func = irq_handle_table[IRQn].handler;
125     param    = irq_handle_table[IRQn].param;
126 
127     irq_func(IRQn, param);
128 
129 #ifdef RT_USING_INTERRUPT_INFO
130     irq_handle_table[IRQn].counter++;
131 #endif
132 
133     return ;
134 }
135 
136 
rt_do_mips_cpu_irq(rt_uint32_t ip)137 void rt_do_mips_cpu_irq(rt_uint32_t ip)
138 {
139     rt_uint32_t intstatus, irq, n;
140 
141     if (ip == 7) {
142         rt_hw_timer_handler();
143     } else {
144         n = ip - 2;
145         /* Receive interrupt signal, compute the irq */
146         intstatus = (gs232_hw0_icregs+n)->int_isr & (gs232_hw0_icregs+n)->int_en;
147         if (0 == intstatus)
148             return ;
149 
150         irq = __rt_ffs(intstatus) - 1;
151         gs232_do_IRQ((n<<5) + irq);
152 
153         /* ack interrupt */
154         (gs232_hw0_icregs+n)->int_clr |= (1 << irq);
155     }
156 }
157 
158 
159 /*@}*/
160 
161 
162