interrupt.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /*
  2. * File : interrupt.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006-2015, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2011-01-13 weety first version
  23. * 2015-04-27 ArdaFu Port bsp from at91sam9260 to asm9260t
  24. */
  25. #include <rthw.h>
  26. #include "asm9260t.h"
  27. #include "interrupt.h"
  28. #define MAX_HANDLERS (64)
  29. extern rt_uint32_t rt_interrupt_nest;
  30. /* exception and interrupt handler table */
  31. struct rt_irq_desc irq_desc[MAX_HANDLERS];
  32. rt_uint32_t rt_interrupt_from_thread;
  33. rt_uint32_t rt_interrupt_to_thread;
  34. rt_uint32_t rt_thread_switch_interrupt_flag;
  35. /* --------------------------------------------------------------------
  36. * Interrupt initialization
  37. * -------------------------------------------------------------------- */
  38. /*
  39. * The default interrupt priority levels (0 = lowest, 3 = highest).
  40. */
  41. static rt_uint32_t default_irq_priority[MAX_HANDLERS/4] =
  42. {
  43. 0x00000000UL, /* INT3 - INT0 */
  44. 0x00000000UL, /* INT7 - INT4 */
  45. 0x00000000UL, /* INT11 - INT8 */
  46. 0x02000000UL, /* INT15 - INT12 */
  47. 0x02020202UL, /* INT19 - INT16 */
  48. 0x02020202UL, /* INT23 - INT20 */
  49. 0x00000002UL, /* INT27 - INT24 */
  50. 0x01010100UL, /* INT31 - INT28 */
  51. 0x00000001UL, /* INT35 - INT32 */
  52. 0x00000000UL, /* INT39 - INT36 */
  53. 0x00000000UL, /* INT43 - INT40 */
  54. 0x00000000UL, /* INT47 - INT44 */
  55. 0x00000000UL, /* INT51 - INT48 */
  56. 0x00000000UL, /* INT55 - INT52 */
  57. 0x00000000UL, /* INT59 - INT56 */
  58. 0x00000000UL, /* INT63 - INT60 */
  59. };
  60. void rt_hw_interrupt_mask(int irq);
  61. void rt_hw_interrupt_umask(int irq);
  62. rt_isr_handler_t rt_hw_interrupt_handle(rt_uint32_t vector, void *param)
  63. {
  64. rt_kprintf("UN-handled interrupt %d occurred!!!\n", vector);
  65. return RT_NULL;
  66. }
  67. /**
  68. * This function will initialize hardware interrupt
  69. */
  70. void rt_hw_interrupt_init(void)
  71. {
  72. register rt_uint32_t idx;
  73. /* Initialize the ICOLL interrupt controller */
  74. outl((1<<8), REG_SET(HW_AHBCLKCTRL1)); // Enable ICOLL clock
  75. outl((1<<8), REG_CLR(HW_PRESETCTRL1)); // Reset ICOLL start
  76. outl((1<<8), REG_SET(HW_PRESETCTRL1)); // Reset ICOLL stop
  77. for(idx = 0; idx < (MAX_HANDLERS/4); idx++)
  78. {
  79. rt_uint32_t reg = (HW_ICOLL_PRIORITY0 + 0x10*idx);
  80. outl(default_irq_priority[idx], REG_VAL(reg));
  81. }
  82. /* init exceptions table */
  83. for(idx=0; idx < MAX_HANDLERS; idx++)
  84. {
  85. irq_desc[idx].handler = (rt_isr_handler_t)rt_hw_interrupt_handle;
  86. irq_desc[idx].param = RT_NULL;
  87. #ifdef RT_USING_INTERRUPT_INFO
  88. rt_snprintf(irq_desc[idx].name, RT_NAME_MAX - 1, "default");
  89. irq_desc[idx].counter = 0;
  90. #endif
  91. }
  92. /* init interrupt nest, and context in thread sp */
  93. rt_interrupt_nest = 0;
  94. rt_interrupt_from_thread = 0;
  95. rt_interrupt_to_thread = 0;
  96. rt_thread_switch_interrupt_flag = 0;
  97. outl(0x00000000, REG_CLR(HW_ICOLL_VBASE)); //todo: fix this bug
  98. outl(0x00020000, REG_CLR(HW_ICOLL_CTRL)); // Clear CTRL REG
  99. outl(0x00050000, REG_SET(HW_ICOLL_CTRL));
  100. outl(0x00000004, HW_ICOLL_UNDEF_VECTOR);
  101. outl(~0UL, REG_CLR(HW_ICOLL_CLEAR0));
  102. outl(~0UL, REG_CLR(HW_ICOLL_CLEAR1));
  103. }
  104. /**
  105. * This function will mask a interrupt.
  106. * @param vector the interrupt number
  107. */
  108. void rt_hw_interrupt_mask(int irq)
  109. {
  110. rt_uint32_t reg = HW_ICOLL_PRIORITY0 + ((irq & 0x3CUL) << 2);
  111. rt_uint32_t bit = 4UL << ((irq & 3UL)<<3);
  112. outl(bit, REG_CLR(reg));
  113. }
  114. /**
  115. * This function will un-mask a interrupt.
  116. * @param vector the interrupt number
  117. */
  118. void rt_hw_interrupt_umask(int irq)
  119. {
  120. rt_uint32_t reg = HW_ICOLL_PRIORITY0 + ((irq & 0x3CUL) << 2);
  121. rt_uint32_t bit = 4UL << ((irq & 3UL)<<3);
  122. outl(bit, REG_SET(reg));
  123. }
  124. /**
  125. * This function will install a interrupt service routine to a interrupt.
  126. * @param vector the interrupt number
  127. * @param handler the interrupt service routine to be installed
  128. * @param param the interrupt service function parameter
  129. * @param name the interrupt name
  130. * @return old handler
  131. */
  132. rt_isr_handler_t rt_hw_interrupt_install(int vector, rt_isr_handler_t handler,
  133. void *param, const char *name)
  134. {
  135. rt_isr_handler_t old_handler = RT_NULL;
  136. if(vector < MAX_HANDLERS)
  137. {
  138. old_handler = irq_desc[vector].handler;
  139. if (handler != RT_NULL)
  140. {
  141. irq_desc[vector].handler = (rt_isr_handler_t)handler;
  142. irq_desc[vector].param = param;
  143. #ifdef RT_USING_INTERRUPT_INFO
  144. rt_snprintf(irq_desc[vector].name, RT_NAME_MAX - 1, "%s", name);
  145. irq_desc[vector].counter = 0;
  146. #endif
  147. }
  148. }
  149. return old_handler;
  150. }
  151. rt_uint32_t rt_hw_interrupt_get_active(rt_uint32_t fiq_irq)
  152. {
  153. //volatile rt_uint32_t irqstat;
  154. rt_uint32_t id;
  155. /* AIC need this dummy read */
  156. inl(HW_ICOLL_VECTOR);
  157. /* get irq number */
  158. id = inl(HW_ICOLL_STAT);
  159. /* clear pending register */
  160. //irqstat = inl(HW_ICOLL_VECTOR);
  161. return id;
  162. }
  163. void rt_hw_interrupt_ack(rt_uint32_t fiq_irq, rt_uint32_t id)
  164. {
  165. rt_uint32_t reg = HW_ICOLL_PRIORITY0 + ((id & 0x3CUL) << 2);
  166. rt_uint32_t level = 1UL << (0x3 & (inl(REG_VAL(reg)) >>((id & 3UL)<<3)));
  167. if(id & 0x20)
  168. outl((1UL<<(id&0x1F)), REG_SET(HW_ICOLL_CLEAR1));
  169. else
  170. outl((1UL<<id), REG_SET(HW_ICOLL_CLEAR0));
  171. outl(level, HW_ICOLL_LEVELACK);
  172. }
  173. #ifdef RT_USING_FINSH
  174. void list_irq(void)
  175. {
  176. int irq;
  177. rt_kprintf("number\tcount\tname\n");
  178. for (irq = 0; irq < MAX_HANDLERS; irq++)
  179. {
  180. if (rt_strncmp(irq_desc[irq].name, "default", sizeof("default")))
  181. {
  182. rt_kprintf("%02ld: %10ld %s\n",
  183. irq, irq_desc[irq].counter, irq_desc[irq].name);
  184. }
  185. }
  186. }
  187. #include <finsh.h>
  188. FINSH_FUNCTION_EXPORT(list_irq, list system irq);
  189. #endif