interrupt.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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, const 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. #ifdef RT_USING_INTERRUPT_INFO
  91. rt_strncpy(irq_handle_table[vector].name, name, RT_NAME_MAX);
  92. #endif /* RT_USING_INTERRUPT_INFO */
  93. irq_handle_table[vector].handler = handler;
  94. irq_handle_table[vector].param = param;
  95. }
  96. return old_handler;
  97. }
  98. void rt_interrupt_dispatch(void *ptreg)
  99. {
  100. int irq;
  101. void *param;
  102. rt_isr_handler_t irq_func;
  103. static rt_uint32_t status = 0;
  104. rt_uint32_t c0_status;
  105. rt_uint32_t c0_cause;
  106. volatile rt_uint32_t cause_im;
  107. volatile rt_uint32_t status_im;
  108. rt_uint32_t pending_im;
  109. /* check os timer */
  110. c0_status = read_c0_status();
  111. c0_cause = read_c0_cause();
  112. cause_im = c0_cause & ST0_IM;
  113. status_im = c0_status & ST0_IM;
  114. pending_im = cause_im & status_im;
  115. if (pending_im & CAUSEF_IP7)
  116. {
  117. rt_hw_timer_handler();
  118. }
  119. if (pending_im & CAUSEF_IP2)
  120. {
  121. /* the hardware interrupt */
  122. status = ls1b_hw0_icregs->int_isr;
  123. if (!status)
  124. return;
  125. for (irq = MAX_INTR; irq > 0; --irq)
  126. {
  127. if ((status & (1 << irq)))
  128. {
  129. status &= ~(1 << irq);
  130. irq_func = irq_handle_table[irq].handler;
  131. param = irq_handle_table[irq].param;
  132. /* do interrupt */
  133. irq_func(irq, param);
  134. #ifdef RT_USING_INTERRUPT_INFO
  135. irq_handle_table[irq].counter++;
  136. #endif /* RT_USING_INTERRUPT_INFO */
  137. /* ack interrupt */
  138. ls1b_hw0_icregs->int_clr |= (1 << irq);
  139. }
  140. }
  141. }
  142. else if (pending_im & CAUSEF_IP3)
  143. {
  144. rt_kprintf("%s %d\r\n", __FUNCTION__, __LINE__);
  145. }
  146. else if (pending_im & CAUSEF_IP4)
  147. {
  148. rt_kprintf("%s %d\r\n", __FUNCTION__, __LINE__);
  149. }
  150. else if (pending_im & CAUSEF_IP5)
  151. {
  152. rt_kprintf("%s %d\r\n", __FUNCTION__, __LINE__);
  153. }
  154. else if (pending_im & CAUSEF_IP6)
  155. {
  156. rt_kprintf("%s %d\r\n", __FUNCTION__, __LINE__);
  157. }
  158. }
  159. /*@}*/