interrupt.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. * File : interrupt.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2011, RT-Thread Development Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2010-10-15 Bernard first version
  13. * 2010-10-15 lgnq modified for LS1B
  14. * 2013-03-29 aozima Modify the interrupt interface implementations.
  15. */
  16. #include <rtthread.h>
  17. #include <rthw.h>
  18. #include "ls1b.h"
  19. #define MAX_INTR 32
  20. extern rt_uint32_t rt_interrupt_nest;
  21. rt_uint32_t rt_interrupt_from_thread;
  22. rt_uint32_t rt_interrupt_to_thread;
  23. rt_uint32_t rt_thread_switch_interrupt_flag;
  24. static struct rt_irq_desc irq_handle_table[MAX_INTR];
  25. void rt_interrupt_dispatch(void *ptreg);
  26. void rt_hw_timer_handler();
  27. static struct ls1b_intc_regs volatile *ls1b_hw0_icregs
  28. = (struct ls1b_intc_regs volatile *)(LS1B_INTREG_BASE);
  29. /**
  30. * @addtogroup Loongson LS1B
  31. */
  32. /*@{*/
  33. static void rt_hw_interrupt_handler(int vector, void *param)
  34. {
  35. rt_kprintf("Unhandled interrupt %d occured!!!\n", vector);
  36. }
  37. /**
  38. * This function will initialize hardware interrupt
  39. */
  40. void rt_hw_interrupt_init(void)
  41. {
  42. rt_int32_t idx;
  43. /* pci active low */
  44. ls1b_hw0_icregs->int_pol = -1; //must be done here 20110802 lgnq
  45. /* make all interrupts level triggered */
  46. (ls1b_hw0_icregs+0)->int_edge = 0x0000e000;
  47. /* mask all interrupts */
  48. (ls1b_hw0_icregs+0)->int_clr = 0xffffffff;
  49. rt_memset(irq_handle_table, 0x00, sizeof(irq_handle_table));
  50. for (idx = 0; idx < MAX_INTR; idx ++)
  51. {
  52. irq_handle_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. (ls1b_hw0_icregs+(vector>>5))->int_en &= ~(1 << (vector&0x1f));
  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. (ls1b_hw0_icregs+(vector>>5))->int_en |= (1 << (vector&0x1f));
  76. }
  77. /**
  78. * This function will install a interrupt service routine to a interrupt.
  79. * @param vector the interrupt number
  80. * @param new_handler the interrupt service routine to be installed
  81. * @param old_handler the old interrupt service routine
  82. */
  83. rt_isr_handler_t rt_hw_interrupt_install(int vector, rt_isr_handler_t handler,
  84. void *param, char *name)
  85. {
  86. rt_isr_handler_t old_handler = RT_NULL;
  87. if (vector >= 0 && vector < MAX_INTR)
  88. {
  89. old_handler = irq_handle_table[vector].handler;
  90. if (handler != RT_NULL)
  91. {
  92. #ifdef RT_USING_INTERRUPT_INFO
  93. rt_strncpy(irq_handle_table[vector].name, name, RT_NAME_MAX);
  94. #endif /* RT_USING_INTERRUPT_INFO */
  95. irq_handle_table[vector].handler = handler;
  96. irq_handle_table[vector].param = param;
  97. }
  98. }
  99. return old_handler;
  100. }
  101. void rt_interrupt_dispatch(void *ptreg)
  102. {
  103. int irq;
  104. void *param;
  105. rt_isr_handler_t irq_func;
  106. static rt_uint32_t status = 0;
  107. rt_uint32_t c0_status;
  108. rt_uint32_t c0_cause;
  109. volatile rt_uint32_t cause_im;
  110. volatile rt_uint32_t status_im;
  111. rt_uint32_t pending_im;
  112. /* check os timer */
  113. c0_status = read_c0_status();
  114. c0_cause = read_c0_cause();
  115. cause_im = c0_cause & ST0_IM;
  116. status_im = c0_status & ST0_IM;
  117. pending_im = cause_im & status_im;
  118. if (pending_im & CAUSEF_IP7)
  119. {
  120. rt_hw_timer_handler();
  121. }
  122. if (pending_im & CAUSEF_IP2)
  123. {
  124. /* the hardware interrupt */
  125. status = ls1b_hw0_icregs->int_isr;
  126. if (!status)
  127. return;
  128. for (irq = MAX_INTR; irq > 0; --irq)
  129. {
  130. if ((status & (1 << irq)))
  131. {
  132. status &= ~(1 << irq);
  133. irq_func = irq_handle_table[irq].handler;
  134. param = irq_handle_table[irq].param;
  135. /* do interrupt */
  136. (*irq_func)(irq, param);
  137. #ifdef RT_USING_INTERRUPT_INFO
  138. irq_handle_table[irq].counter++;
  139. #endif /* RT_USING_INTERRUPT_INFO */
  140. /* ack interrupt */
  141. ls1b_hw0_icregs->int_clr |= (1 << irq);
  142. }
  143. }
  144. }
  145. else if (pending_im & CAUSEF_IP3)
  146. {
  147. rt_kprintf("%s %d\r\n", __FUNCTION__, __LINE__);
  148. }
  149. else if (pending_im & CAUSEF_IP4)
  150. {
  151. rt_kprintf("%s %d\r\n", __FUNCTION__, __LINE__);
  152. }
  153. else if (pending_im & CAUSEF_IP5)
  154. {
  155. rt_kprintf("%s %d\r\n", __FUNCTION__, __LINE__);
  156. }
  157. else if (pending_im & CAUSEF_IP6)
  158. {
  159. rt_kprintf("%s %d\r\n", __FUNCTION__, __LINE__);
  160. }
  161. }
  162. /*@}*/