interrupt.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /*
  2. * File : interrupt.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006, 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. * 2010-11-13 weety first version
  23. */
  24. #include <rtthread.h>
  25. #include <rthw.h>
  26. #include "dm36x.h"
  27. #define MAX_HANDLERS 64
  28. extern rt_uint32_t rt_interrupt_nest;
  29. struct rt_irq_desc irq_desc[MAX_HANDLERS];
  30. /* exception and interrupt handler table */
  31. rt_uint32_t rt_interrupt_from_thread, rt_interrupt_to_thread;
  32. rt_uint32_t rt_thread_switch_interrupt_flag;
  33. #define IRQ_BIT(irq) ((irq) & 0x1f)
  34. #define FIQ_REG0_OFFSET 0x0000
  35. #define FIQ_REG1_OFFSET 0x0004
  36. #define IRQ_REG0_OFFSET 0x0008
  37. #define IRQ_REG1_OFFSET 0x000C
  38. #define IRQ_ENT_REG0_OFFSET 0x0018
  39. #define IRQ_ENT_REG1_OFFSET 0x001C
  40. #define IRQ_INCTL_REG_OFFSET 0x0020
  41. #define IRQ_EABASE_REG_OFFSET 0x0024
  42. #define IRQ_INTPRI0_REG_OFFSET 0x0030
  43. #define IRQ_INTPRI7_REG_OFFSET 0x004C
  44. /* FIQ are pri 0-1; otherwise 2-7, with 7 lowest priority */
  45. static const rt_uint8_t dm365_default_priorities[DAVINCI_N_AINTC_IRQ] = {
  46. [IRQ_DM3XX_VPSSINT0] = 2,
  47. [IRQ_DM3XX_VPSSINT1] = 6,
  48. [IRQ_DM3XX_VPSSINT2] = 6,
  49. [IRQ_DM3XX_VPSSINT3] = 6,
  50. [IRQ_DM3XX_VPSSINT4] = 6,
  51. [IRQ_DM3XX_VPSSINT5] = 6,
  52. [IRQ_DM3XX_VPSSINT6] = 6,
  53. [IRQ_DM3XX_VPSSINT7] = 7,
  54. [IRQ_DM3XX_VPSSINT8] = 6,
  55. [IRQ_ASQINT] = 6,
  56. [IRQ_DM365_IMXINT0] = 6,
  57. [IRQ_DM3XX_IMCOPINT] = 6,
  58. [IRQ_USBINT] = 4,
  59. [IRQ_DM3XX_RTOINT] = 4,
  60. [IRQ_DM3XX_TINT5] = 7,
  61. [IRQ_DM3XX_TINT6] = 7,
  62. [IRQ_CCINT0] = 5, /* dma */
  63. [IRQ_DM3XX_SPINT1_0] = 5, /* dma */
  64. [IRQ_DM3XX_SPINT1_1] = 5, /* dma */
  65. [IRQ_DM3XX_SPINT2_0] = 5, /* dma */
  66. [IRQ_DM365_PSCINT] = 7,
  67. [IRQ_DM3XX_SPINT2_1] = 7,
  68. [IRQ_DM3XX_TINT7] = 4,
  69. [IRQ_DM3XX_SDIOINT0] = 7,
  70. [IRQ_DM365_MBXINT] = 7,
  71. [IRQ_DM365_MBRINT] = 7,
  72. [IRQ_DM3XX_MMCINT0] = 7,
  73. [IRQ_DM3XX_MMCINT1] = 7,
  74. [IRQ_DM3XX_PWMINT3] = 7,
  75. [IRQ_DM365_DDRINT] = 7,
  76. [IRQ_DM365_AEMIFINT] = 7,
  77. [IRQ_DM3XX_SDIOINT1] = 4,
  78. [IRQ_DM365_TINT0] = 2, /* clockevent */
  79. [IRQ_DM365_TINT1] = 2, /* clocksource */
  80. [IRQ_DM365_TINT2] = 7, /* DSP timer */
  81. [IRQ_DM365_TINT3] = 7, /* system tick */
  82. [IRQ_PWMINT0] = 7,
  83. [IRQ_PWMINT1] = 7,
  84. [IRQ_DM365_PWMINT2] = 7,
  85. [IRQ_DM365_IICINT] = 3,
  86. [IRQ_UARTINT0] = 3,
  87. [IRQ_UARTINT1] = 3,
  88. [IRQ_DM3XX_SPINT0_0] = 3,
  89. [IRQ_DM3XX_SPINT0_1] = 3,
  90. [IRQ_DM3XX_GPIO0] = 3,
  91. [IRQ_DM3XX_GPIO1] = 7,
  92. [IRQ_DM3XX_GPIO2] = 4,
  93. [IRQ_DM3XX_GPIO3] = 4,
  94. [IRQ_DM3XX_GPIO4] = 7,
  95. [IRQ_DM3XX_GPIO5] = 7,
  96. [IRQ_DM3XX_GPIO6] = 7,
  97. [IRQ_DM3XX_GPIO7] = 7,
  98. [IRQ_DM3XX_GPIO8] = 7,
  99. [IRQ_DM3XX_GPIO9] = 7,
  100. [IRQ_DM365_GPIO10] = 7,
  101. [IRQ_DM365_GPIO11] = 7,
  102. [IRQ_DM365_GPIO12] = 7,
  103. [IRQ_DM365_GPIO13] = 7,
  104. [IRQ_DM365_GPIO14] = 7,
  105. [IRQ_DM365_GPIO15] = 7,
  106. [IRQ_DM365_KEYINT] = 7,
  107. [IRQ_DM365_COMMTX] = 7,
  108. [IRQ_DM365_COMMRX] = 7,
  109. [IRQ_EMUINT] = 7,
  110. };
  111. static inline unsigned int davinci_irq_readl(int offset)
  112. {
  113. return davinci_readl(DAVINCI_ARM_INTC_BASE + offset);
  114. }
  115. static inline void davinci_irq_writel(unsigned long value, int offset)
  116. {
  117. davinci_writel(value, DAVINCI_ARM_INTC_BASE + offset);
  118. }
  119. /**
  120. * @addtogroup DM36X
  121. */
  122. /*@{*/
  123. rt_isr_handler_t rt_hw_interrupt_handle(int vector, void *param)
  124. {
  125. rt_kprintf("Unhandled interrupt %d occured!!!\n", vector);
  126. return RT_NULL;
  127. }
  128. /**
  129. * This function will initialize hardware interrupt
  130. */
  131. void rt_hw_interrupt_init(void)
  132. {
  133. int i;
  134. register rt_uint32_t idx;
  135. const rt_uint8_t *priority;
  136. priority = dm365_default_priorities;
  137. /* Clear all interrupt requests */
  138. davinci_irq_writel(~0x0, FIQ_REG0_OFFSET);
  139. davinci_irq_writel(~0x0, FIQ_REG1_OFFSET);
  140. davinci_irq_writel(~0x0, IRQ_REG0_OFFSET);
  141. davinci_irq_writel(~0x0, IRQ_REG1_OFFSET);
  142. /* Disable all interrupts */
  143. davinci_irq_writel(0x0, IRQ_ENT_REG0_OFFSET);
  144. davinci_irq_writel(0x0, IRQ_ENT_REG1_OFFSET);
  145. /* Interrupts disabled immediately, IRQ entry reflects all */
  146. davinci_irq_writel(0x0, IRQ_INCTL_REG_OFFSET);
  147. /* we don't use the hardware vector table, just its entry addresses */
  148. davinci_irq_writel(0, IRQ_EABASE_REG_OFFSET);
  149. /* Clear all interrupt requests */
  150. davinci_irq_writel(~0x0, FIQ_REG0_OFFSET);
  151. davinci_irq_writel(~0x0, FIQ_REG1_OFFSET);
  152. davinci_irq_writel(~0x0, IRQ_REG0_OFFSET);
  153. davinci_irq_writel(~0x0, IRQ_REG1_OFFSET);
  154. for (i = IRQ_INTPRI0_REG_OFFSET; i <= IRQ_INTPRI7_REG_OFFSET; i += 4) {
  155. unsigned j;
  156. rt_uint32_t pri;
  157. for (j = 0, pri = 0; j < 32; j += 4, priority++)
  158. pri |= (*priority & 0x07) << j;
  159. davinci_irq_writel(pri, i);
  160. }
  161. /* init exceptions table */
  162. for(idx=0; idx < MAX_HANDLERS; idx++)
  163. {
  164. irq_desc[idx].handler = (rt_isr_handler_t)rt_hw_interrupt_handle;
  165. irq_desc[idx].param = RT_NULL;
  166. #ifdef RT_USING_INTERRUPT_INFO
  167. rt_snprintf(irq_desc[idx].name, RT_NAME_MAX - 1, "default");
  168. irq_desc[idx].counter = 0;
  169. #endif
  170. }
  171. /* init interrupt nest, and context in thread sp */
  172. rt_interrupt_nest = 0;
  173. rt_interrupt_from_thread = 0;
  174. rt_interrupt_to_thread = 0;
  175. rt_thread_switch_interrupt_flag = 0;
  176. }
  177. /**
  178. * This function will mask a interrupt.
  179. * @param vector the interrupt number
  180. */
  181. void rt_hw_interrupt_mask(int irq)
  182. {
  183. unsigned int mask;
  184. rt_uint32_t l;
  185. mask = 1 << IRQ_BIT(irq);
  186. if (irq > 31) {
  187. l = davinci_irq_readl(IRQ_ENT_REG1_OFFSET);
  188. l &= ~mask;
  189. davinci_irq_writel(l, IRQ_ENT_REG1_OFFSET);
  190. } else {
  191. l = davinci_irq_readl(IRQ_ENT_REG0_OFFSET);
  192. l &= ~mask;
  193. davinci_irq_writel(l, IRQ_ENT_REG0_OFFSET);
  194. }
  195. }
  196. /**
  197. * This function will un-mask a interrupt.
  198. * @param vector the interrupt number
  199. */
  200. void rt_hw_interrupt_umask(int irq)
  201. {
  202. unsigned int mask;
  203. rt_uint32_t l;
  204. mask = 1 << IRQ_BIT(irq);
  205. if (irq > 31) {
  206. l = davinci_irq_readl(IRQ_ENT_REG1_OFFSET);
  207. l |= mask;
  208. davinci_irq_writel(l, IRQ_ENT_REG1_OFFSET);
  209. } else {
  210. l = davinci_irq_readl(IRQ_ENT_REG0_OFFSET);
  211. l |= mask;
  212. davinci_irq_writel(l, IRQ_ENT_REG0_OFFSET);
  213. }
  214. }
  215. /**
  216. * This function will install a interrupt service routine to a interrupt.
  217. * @param vector the interrupt number
  218. * @param handler the interrupt service routine to be installed
  219. * @param param the interrupt service function parameter
  220. * @param name the interrupt name
  221. * @return old handler
  222. */
  223. rt_isr_handler_t rt_hw_interrupt_install(int vector, rt_isr_handler_t handler,
  224. void *param, char *name)
  225. {
  226. rt_isr_handler_t old_handler = RT_NULL;
  227. if(vector < MAX_HANDLERS)
  228. {
  229. old_handler = irq_desc[vector].handler;
  230. if (handler != RT_NULL)
  231. {
  232. irq_desc[vector].handler = (rt_isr_handler_t)handler;
  233. irq_desc[vector].param = param;
  234. #ifdef RT_USING_INTERRUPT_INFO
  235. rt_snprintf(irq_desc[vector].name, RT_NAME_MAX - 1, "%s", name);
  236. irq_desc[vector].counter = 0;
  237. #endif
  238. }
  239. }
  240. return old_handler;
  241. }
  242. #ifdef RT_USING_FINSH
  243. #ifdef RT_USING_INTERRUPT_INFO
  244. void list_irq(void)
  245. {
  246. int irq;
  247. rt_kprintf("number\tcount\tname\n");
  248. for (irq = 0; irq < MAX_HANDLERS; irq++)
  249. {
  250. if (rt_strncmp(irq_desc[irq].name, "default", sizeof("default")))
  251. {
  252. rt_kprintf("%02ld: %10ld %s\n", irq, irq_desc[irq].counter, irq_desc[irq].name);
  253. }
  254. }
  255. }
  256. #include <finsh.h>
  257. FINSH_FUNCTION_EXPORT(list_irq, list system irq);
  258. #ifdef FINSH_USING_MSH
  259. int cmd_list_irq(int argc, char** argv)
  260. {
  261. list_irq();
  262. return 0;
  263. }
  264. FINSH_FUNCTION_EXPORT_ALIAS(cmd_list_irq, __cmd_list_irq, list system irq.);
  265. #endif
  266. #endif
  267. #endif
  268. /*@}*/