ih_at91sys.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*
  2. * Copyright (C) 2007 by egnite Software GmbH. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of the copyright holders nor the names of
  14. * contributors may be used to endorse or promote products derived
  15. * from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  20. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  21. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  22. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  23. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  24. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  25. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  26. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  27. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  28. * SUCH DAMAGE.
  29. *
  30. * For additional information see http://www.ethernut.de/
  31. *
  32. */
  33. /*
  34. * $Log$
  35. * Revision 1.2 2008/08/11 06:59:11 haraldkipp
  36. * BSD types replaced by stdint types (feature request #1282721).
  37. *
  38. * Revision 1.1 2007/02/15 16:11:14 haraldkipp
  39. * Support for system controller interrupts added.
  40. *
  41. */
  42. #include <arch/arm.h>
  43. #include <dev/irqreg.h>
  44. #ifndef NUT_IRQPRI_SYS
  45. #define NUT_IRQPRI_SYS 0
  46. #endif
  47. SYSIRQ_HANDLER syssig_DBGU;
  48. SYSIRQ_HANDLER syssig_MC;
  49. SYSIRQ_HANDLER syssig_PIT;
  50. SYSIRQ_HANDLER syssig_PMC;
  51. SYSIRQ_HANDLER syssig_RSTC;
  52. SYSIRQ_HANDLER syssig_RTT;
  53. SYSIRQ_HANDLER syssig_WDT;
  54. static int SystemIrqCtl(int cmd, void *param);
  55. IRQ_HANDLER sig_SYS = {
  56. #ifdef NUT_PERFMON
  57. 0, /* Interrupt counter, ir_count. */
  58. #endif
  59. NULL, /* Passed argument, ir_arg. */
  60. NULL, /* Handler subroutine, ir_handler. */
  61. SystemIrqCtl /* Interrupt control, ir_ctl. */
  62. };
  63. /*!
  64. * \brief System interrupt entry.
  65. */
  66. static void SystemIrqEntry(void) NUT_NAKED_FUNC;
  67. void SystemIrqEntry(void)
  68. {
  69. IRQ_ENTRY();
  70. #ifdef NUT_PERFMON
  71. sig_SYS.ir_count++;
  72. #endif
  73. if (syssig_DBGU.sir_enabled && syssig_DBGU.sir_handler) {
  74. /* Call debug unit interrupt handler. */
  75. (syssig_DBGU.sir_handler) (syssig_DBGU.sir_arg);
  76. }
  77. if (syssig_MC.sir_enabled && syssig_MC.sir_handler) {
  78. /* Call memory controller interrupt handler. */
  79. (syssig_MC.sir_handler) (syssig_MC.sir_arg);
  80. }
  81. if (syssig_PMC.sir_enabled && syssig_PMC.sir_handler) {
  82. /* Call power management controller interrupt handler. */
  83. (syssig_PMC.sir_handler) (syssig_PMC.sir_arg);
  84. }
  85. if (syssig_RSTC.sir_enabled && syssig_RSTC.sir_handler) {
  86. /* Call reset controller interrupt handler. */
  87. (syssig_RSTC.sir_handler) (syssig_RSTC.sir_arg);
  88. }
  89. if (syssig_RTT.sir_enabled && syssig_RTT.sir_handler) {
  90. /* Call real-time timer interrupt handler. */
  91. (syssig_RTT.sir_handler) (syssig_RTT.sir_arg);
  92. }
  93. if (syssig_WDT.sir_enabled && syssig_WDT.sir_handler) {
  94. /* Call watchdog timer interrupt handler. */
  95. (syssig_WDT.sir_handler) (syssig_WDT.sir_arg);
  96. }
  97. if ((inr(PIT_MR) & PIT_PITIEN) != 0 && (inr(PIT_SR) & PIT_PITS) != 0) {
  98. if (syssig_PIT.sir_handler) {
  99. /* Call periodic interval timer interrupt handler. */
  100. (syssig_PIT.sir_handler) (syssig_PIT.sir_arg);
  101. }
  102. inr(PIT_PIVR);
  103. }
  104. IRQ_EXIT();
  105. }
  106. /*!
  107. * \brief Timer/Counter 0 interrupt control.
  108. *
  109. * \param cmd Control command.
  110. * - NUT_IRQCTL_INIT Initialize and disable interrupt.
  111. * - NUT_IRQCTL_STATUS Query interrupt status.
  112. * - NUT_IRQCTL_ENABLE Enable interrupt.
  113. * - NUT_IRQCTL_DISABLE Disable interrupt.
  114. * - NUT_IRQCTL_GETPRIO Query interrupt priority.
  115. * - NUT_IRQCTL_SETPRIO Set interrupt priority.
  116. * - NUT_IRQCTL_GETCOUNT Query and clear interrupt counter.
  117. * \param param Pointer to optional parameter.
  118. *
  119. * \return 0 on success, -1 otherwise.
  120. */
  121. static int SystemIrqCtl(int cmd, void *param)
  122. {
  123. int rc = 0;
  124. unsigned int *ival = (unsigned int *)param;
  125. int_fast8_t enabled = inr(AIC_IMR) & _BV(SYSC_ID);
  126. /* Disable interrupt. */
  127. if (enabled) {
  128. outr(AIC_IDCR, _BV(SYSC_ID));
  129. }
  130. switch(cmd) {
  131. case NUT_IRQCTL_INIT:
  132. /* Set the vector. */
  133. outr(AIC_SVR(SYSC_ID), (unsigned int)SystemIrqEntry);
  134. /* Initialize to level triggered with defined priority. */
  135. outr(AIC_SMR(SYSC_ID), AIC_SRCTYPE_INT_LEVEL_SENSITIVE | NUT_IRQPRI_SYS);
  136. /* Clear interrupt */
  137. outr(AIC_ICCR, _BV(SYSC_ID));
  138. break;
  139. case NUT_IRQCTL_STATUS:
  140. if (enabled) {
  141. *ival |= 1;
  142. }
  143. else {
  144. *ival &= ~1;
  145. }
  146. break;
  147. case NUT_IRQCTL_ENABLE:
  148. enabled = 1;
  149. break;
  150. case NUT_IRQCTL_DISABLE:
  151. enabled = 0;
  152. break;
  153. case NUT_IRQCTL_GETPRIO:
  154. *ival = inr(AIC_SMR(SYSC_ID)) & AIC_PRIOR;
  155. break;
  156. case NUT_IRQCTL_SETPRIO:
  157. outr(AIC_SMR(SYSC_ID), (inr(AIC_SMR(SYSC_ID)) & ~AIC_PRIOR) | *ival);
  158. break;
  159. #ifdef NUT_PERFMON
  160. case NUT_IRQCTL_GETCOUNT:
  161. *ival = (unsigned int)sig_SYS.ir_count;
  162. sig_SYS.ir_count = 0;
  163. break;
  164. #endif
  165. default:
  166. rc = -1;
  167. break;
  168. }
  169. /* Enable interrupt. */
  170. if (enabled) {
  171. outr(AIC_IECR, _BV(SYSC_ID));
  172. }
  173. return rc;
  174. }
  175. /*!
  176. * \brief Enable a specified system interrupt.
  177. *
  178. * \param sysirq Interrupt to enable.
  179. *
  180. * \return 0 on success, -1 otherwise.
  181. */
  182. int NutSysIrqEnable(SYSIRQ_HANDLER * sysirq)
  183. {
  184. sysirq->sir_enabled = 1;
  185. return SystemIrqCtl(NUT_IRQCTL_ENABLE, NULL);
  186. }
  187. /*!
  188. * \brief Disable a specified system interrupt.
  189. *
  190. * \param sysirq Interrupt to disable.
  191. *
  192. * \return 0 on success, -1 otherwise.
  193. */
  194. int NutSysIrqDisable(SYSIRQ_HANDLER * sysirq)
  195. {
  196. sysirq->sir_enabled = 0;
  197. return 0;
  198. }
  199. /*!
  200. * \brief Register a system interrupt handler.
  201. *
  202. * This call will also disable any previously registered handler.
  203. *
  204. * If enabled with NutSysIrqEnable(), the registered handler will be
  205. * called on every system interrupt. The handler must check, if the
  206. * interrupt was triggered by the corresponding device.
  207. *
  208. * This function is typically called by device drivers, but applications
  209. * may also implement their local interrupt handlers.
  210. *
  211. * \param irq Interrupt to be associated with this handler.
  212. * \param handler This routine will be called by Nut/OS, when the
  213. * specified interrupt occurs.
  214. * \param arg Argument to be passed to the interrupt handler.
  215. *
  216. * \return 0 on success, -1 otherwise.
  217. */
  218. int NutRegisterSysIrqHandler(SYSIRQ_HANDLER * sysirq, void (*handler) (void *), void *arg)
  219. {
  220. int rc;
  221. /* Disable any previously registered handler. */
  222. NutSysIrqDisable(sysirq);
  223. /* Initialize this interrupt. */
  224. rc = SystemIrqCtl(NUT_IRQCTL_INIT, NULL);
  225. if (rc == 0) {
  226. /* Set the interrupt handler. */
  227. sysirq->sir_arg = arg;
  228. sysirq->sir_handler = handler;
  229. }
  230. return rc;
  231. }