interrupt.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /*
  2. * File : interrupt.c
  3. * COPYRIGHT (C) 2008 - 2016, RT-Thread Development Team
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  18. *
  19. * Change Logs:
  20. * 2010-07-09 Bernard first version
  21. * 2013-03-29 aozima Modify the interrupt interface implementations.
  22. */
  23. #include <rtthread.h>
  24. #include <rthw.h>
  25. #include <board.h>
  26. #if defined(RT_USING_JZ4770) || defined(RT_USING_JZ4775) || defined(RT_USING_JZ_M150) || defined(RT_USING_JZ_X1000)
  27. #define INTERRUPTS_MAX 64
  28. #else
  29. #define INTERRUPTS_MAX 32
  30. #endif
  31. extern rt_uint32_t rt_interrupt_nest;
  32. rt_uint32_t rt_interrupt_from_thread, rt_interrupt_to_thread;
  33. rt_uint32_t rt_thread_switch_interrupt_flag;
  34. static struct rt_irq_desc isr_table[INTERRUPTS_MAX];
  35. /**
  36. * @addtogroup Ingenic
  37. */
  38. /*@{*/
  39. static void rt_hw_interrupt_handler(int vector, void *param)
  40. {
  41. rt_kprintf("Unhandled interrupt %d occured!!!\n", vector);
  42. }
  43. /**
  44. * This function will initialize hardware interrupt
  45. */
  46. void rt_hw_interrupt_init(void)
  47. {
  48. rt_int32_t idx;
  49. rt_memset(isr_table, 0x00, sizeof(isr_table));
  50. for (idx = 0; idx < INTERRUPTS_MAX; idx ++)
  51. {
  52. isr_table[idx].handler = rt_hw_interrupt_handler;
  53. }
  54. /* init interrupt nest, and context in thread sp */
  55. rt_interrupt_nest = 0;
  56. rt_interrupt_from_thread = 0;
  57. rt_interrupt_to_thread = 0;
  58. rt_thread_switch_interrupt_flag = 0;
  59. }
  60. /**
  61. * This function will mask a interrupt.
  62. * @param vector the interrupt number
  63. */
  64. void rt_hw_interrupt_mask(int vector)
  65. {
  66. /* mask interrupt */
  67. __intc_mask_irq(vector);
  68. }
  69. /**
  70. * This function will un-mask a interrupt.
  71. * @param vector the interrupt number
  72. */
  73. void rt_hw_interrupt_umask(int vector)
  74. {
  75. __intc_unmask_irq(vector);
  76. }
  77. /**
  78. * This function will install a interrupt service routine to a interrupt.
  79. * @param vector the interrupt number
  80. * @param handler the interrupt service routine to be installed
  81. * @param param the interrupt service function parameter
  82. * @param name the interrupt name
  83. * @return old handler
  84. */
  85. rt_isr_handler_t rt_hw_interrupt_install(int vector, rt_isr_handler_t handler,
  86. void *param, const char *name)
  87. {
  88. rt_isr_handler_t old_handler = RT_NULL;
  89. if(vector < INTERRUPTS_MAX)
  90. {
  91. old_handler = isr_table[vector].handler;
  92. #ifdef RT_USING_INTERRUPT_INFO
  93. rt_strncpy(isr_table[vector].name, name, RT_NAME_MAX);
  94. #endif /* RT_USING_INTERRUPT_INFO */
  95. isr_table[vector].handler = handler;
  96. isr_table[vector].param = param;
  97. }
  98. return old_handler;
  99. }
  100. #if defined(RT_USING_JZ4770) || defined(RT_USING_JZ4775) || defined(RT_USING_JZ_M150) || defined(RT_USING_JZ_X1000)
  101. /*
  102. * fls - find last bit set.
  103. * @word: The word to search
  104. *
  105. * This is defined the same way as ffs.
  106. * Note fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32.
  107. */
  108. rt_inline int fls(int x)
  109. {
  110. __asm__("clz %0, %1" : "=r" (x) : "r" (x));
  111. return 32 - x;
  112. }
  113. #endif
  114. #include <mipsregs.h>
  115. void rt_interrupt_dispatch(void *ptreg)
  116. {
  117. int i;
  118. void *param;
  119. rt_isr_handler_t irq_func;
  120. #if defined(RT_USING_JZ4770) || defined(RT_USING_JZ4775) || defined(RT_USING_JZ_M150) || defined(RT_USING_JZ_X1000)
  121. int irq = 0, group;
  122. rt_uint32_t intc_ipr0 = 0, intc_ipr1 = 0, vpu_pending = 0;
  123. rt_uint32_t c0_status, c0_cause;
  124. rt_uint32_t pending_im;
  125. /* check os timer */
  126. c0_status = read_c0_status();
  127. c0_cause = read_c0_cause();
  128. pending_im = (c0_cause & ST0_IM) & (c0_status & ST0_IM);
  129. if (pending_im & CAUSEF_IP3)
  130. {
  131. extern void rt_hw_ost_handler(void);
  132. rt_hw_ost_handler();
  133. return;
  134. }
  135. if (pending_im & CAUSEF_IP2)
  136. {
  137. intc_ipr0 = REG_INTC_IPR(0);
  138. intc_ipr1 = REG_INTC_IPR(1);
  139. if (intc_ipr0)
  140. {
  141. irq = fls(intc_ipr0) - 1;
  142. intc_ipr0 &= ~(1<<irq);
  143. }
  144. else if(intc_ipr1)
  145. {
  146. irq = fls(intc_ipr1) - 1;
  147. intc_ipr1 &= ~(1<<irq);
  148. irq += 32;
  149. }
  150. #ifndef RT_USING_JZ_X1000 /* X1000 has no VPU */
  151. else
  152. {
  153. __asm__ __volatile__ (
  154. "mfc0 %0, $13, 0 \n\t"
  155. "nop \n\t"
  156. :"=r"(vpu_pending)
  157. :);
  158. if(vpu_pending & 0x800)
  159. irq = IRQ_VPU;
  160. else
  161. return;
  162. }
  163. #endif
  164. if (irq >= INTERRUPTS_MAX)
  165. rt_kprintf("max interrupt, irq=%d\n", irq);
  166. /* do interrupt */
  167. irq_func = isr_table[irq].handler;
  168. param = isr_table[irq].param;
  169. (*irq_func)(irq, param);
  170. #ifdef RT_USING_INTERRUPT_INFO
  171. isr_table[i].counter++;
  172. #endif /* RT_USING_INTERRUPT_INFO */
  173. /* ack interrupt */
  174. __intc_ack_irq(irq);
  175. }
  176. if (pending_im & CAUSEF_IP0)
  177. rt_kprintf("CAUSEF_IP0\n");
  178. if (pending_im & CAUSEF_IP1)
  179. rt_kprintf("CAUSEF_IP1\n");
  180. if (pending_im & CAUSEF_IP4)
  181. rt_kprintf("CAUSEF_IP4\n");
  182. if (pending_im & CAUSEF_IP5)
  183. rt_kprintf("CAUSEF_IP5\n");
  184. if (pending_im & CAUSEF_IP6)
  185. rt_kprintf("CAUSEF_IP6\n");
  186. if (pending_im & CAUSEF_IP7)
  187. rt_kprintf("CAUSEF_IP7\n");
  188. #else
  189. static rt_uint32_t pending = 0;
  190. /* the hardware interrupt */
  191. pending |= REG_INTC_IPR;
  192. if (!pending) return;
  193. for (i = INTERRUPTS_MAX; i > 0; --i)
  194. {
  195. if ((pending & (1<<i)))
  196. {
  197. pending &= ~(1<<i);
  198. /* do interrupt */
  199. irq_func = isr_table[i].handler;
  200. param = isr_table[i].param;
  201. (*irq_func)(i, param);
  202. #ifdef RT_USING_INTERRUPT_INFO
  203. isr_table[i].counter++;
  204. #endif /* RT_USING_INTERRUPT_INFO */
  205. /* ack interrupt */
  206. __intc_ack_irq(i);
  207. }
  208. }
  209. #endif
  210. }
  211. /*@}*/