interrupts.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*
  2. * (C) Copyright 2000-2002
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * (C) Copyright 2004, Psyent Corporation <www.psyent.com>
  6. * Scott McNutt <smcnutt@psyent.com>
  7. *
  8. * See file CREDITS for list of people who contributed to this
  9. * project.
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License as
  13. * published by the Free Software Foundation; either version 2 of
  14. * the License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  24. * MA 02111-1307 USA
  25. */
  26. #include <nios2.h>
  27. #include <nios2-io.h>
  28. #include <asm/ptrace.h>
  29. #include <common.h>
  30. #include <command.h>
  31. #include <watchdog.h>
  32. #ifdef CONFIG_STATUS_LED
  33. #include <status_led.h>
  34. #endif
  35. #if defined(CFG_NIOS_TMRBASE) && !defined(CFG_NIOS_TMRIRQ)
  36. #error CFG_NIOS_TMRIRQ not defined (see documentation)
  37. #endif
  38. /****************************************************************************/
  39. struct irq_action {
  40. interrupt_handler_t *handler;
  41. void *arg;
  42. int count;
  43. };
  44. static struct irq_action vecs[32];
  45. /*************************************************************************/
  46. volatile ulong timestamp = 0;
  47. void reset_timer (void)
  48. {
  49. timestamp = 0;
  50. }
  51. ulong get_timer (ulong base)
  52. {
  53. WATCHDOG_RESET ();
  54. return (timestamp - base);
  55. }
  56. void set_timer (ulong t)
  57. {
  58. timestamp = t;
  59. }
  60. /* The board must handle this interrupt if a timer is not
  61. * provided.
  62. */
  63. #if defined(CFG_NIOS_TMRBASE)
  64. void tmr_isr (void *arg)
  65. {
  66. nios_timer_t *tmr = (nios_timer_t *)arg;
  67. /* Interrupt is cleared by writing anything to the
  68. * status register.
  69. */
  70. tmr->status = 0;
  71. timestamp += CFG_NIOS_TMRMS;
  72. #ifdef CONFIG_STATUS_LED
  73. status_led_tick(timestamp);
  74. #endif
  75. }
  76. static void tmr_init (void)
  77. {
  78. nios_timer_t *tmr =(nios_timer_t *)CACHE_BYPASS(CFG_NIOS_TMRBASE);
  79. tmr->control &= ~(NIOS_TIMER_START | NIOS_TIMER_ITO);
  80. tmr->control |= NIOS_TIMER_STOP;
  81. #if defined(CFG_NIOS_TMRCNT)
  82. tmr->periodl = CFG_NIOS_TMRCNT & 0xffff;
  83. tmr->periodh = (CFG_NIOS_TMRCNT >> 16) & 0xffff;
  84. #endif
  85. tmr->control |= ( NIOS_TIMER_ITO |
  86. NIOS_TIMER_CONT |
  87. NIOS_TIMER_START );
  88. irq_install_handler (CFG_NIOS_TMRIRQ, tmr_isr, (void *)tmr);
  89. }
  90. #endif /* CFG_NIOS_TMRBASE */
  91. /*************************************************************************/
  92. int disable_interrupts (void)
  93. {
  94. int val = rdctl (CTL_STATUS);
  95. wrctl (CTL_STATUS, val & ~STATUS_IE);
  96. return (val & STATUS_IE);
  97. }
  98. void enable_interrupts( void )
  99. {
  100. int val = rdctl (CTL_STATUS);
  101. wrctl (CTL_STATUS, val | STATUS_IE);
  102. }
  103. void external_interrupt (struct pt_regs *regs)
  104. {
  105. unsigned irqs;
  106. struct irq_action *act;
  107. /* Evaluate only irqs that are both enabled AND pending */
  108. irqs = rdctl (CTL_IENABLE) & rdctl (CTL_IPENDING);
  109. act = vecs;
  110. /* Assume (as does the Nios2 HAL) that bit 0 is highest
  111. * priority. NOTE: There is ALWAYS a handler assigned
  112. * (the default if no other).
  113. */
  114. while (irqs) {
  115. if (irqs & 1) {
  116. act->handler (act->arg);
  117. act->count++;
  118. }
  119. irqs >>=1;
  120. act++;
  121. }
  122. }
  123. static void def_hdlr (void *arg)
  124. {
  125. unsigned irqs = rdctl (CTL_IENABLE);
  126. /* Disable the individual interrupt -- with gratuitous
  127. * warning.
  128. */
  129. irqs &= ~(1 << (int)arg);
  130. wrctl (CTL_IENABLE, irqs);
  131. printf ("WARNING: Disabling unhandled interrupt: %d\n",
  132. (int)arg);
  133. }
  134. /*************************************************************************/
  135. void irq_install_handler (int irq, interrupt_handler_t *hdlr, void *arg)
  136. {
  137. int flag;
  138. struct irq_action *act;
  139. unsigned ena = rdctl (CTL_IENABLE);
  140. if ((irq < 0) || (irq > 31))
  141. return;
  142. act = &vecs[irq];
  143. flag = disable_interrupts ();
  144. if (hdlr) {
  145. act->handler = hdlr;
  146. act->arg = arg;
  147. ena |= (1 << irq); /* enable */
  148. } else {
  149. act->handler = def_hdlr;
  150. act->arg = (void *)irq;
  151. ena &= ~(1 << irq); /* disable */
  152. }
  153. wrctl (CTL_IENABLE, ena);
  154. if (flag) enable_interrupts ();
  155. }
  156. int interrupt_init (void)
  157. {
  158. int i;
  159. /* Assign the default handler to all */
  160. for (i = 0; i < 32; i++) {
  161. vecs[i].handler = def_hdlr;
  162. vecs[i].arg = (void *)i;
  163. vecs[i].count = 0;
  164. }
  165. #if defined(CFG_NIOS_TMRBASE)
  166. tmr_init ();
  167. #endif
  168. enable_interrupts ();
  169. return (0);
  170. }
  171. /*************************************************************************/
  172. #if (CONFIG_COMMANDS & CFG_CMD_IRQ)
  173. int do_irqinfo (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  174. {
  175. int i;
  176. struct irq_action *act = vecs;
  177. printf ("\nInterrupt-Information:\n\n");
  178. printf ("Nr Routine Arg Count\n");
  179. printf ("-----------------------------\n");
  180. for (i=0; i<32; i++) {
  181. if (act->handler != def_hdlr) {
  182. printf ("%02d %08lx %08lx %d\n",
  183. i,
  184. (ulong)act->handler,
  185. (ulong)act->arg,
  186. act->count);
  187. }
  188. act++;
  189. }
  190. printf ("\n");
  191. return (0);
  192. }
  193. #endif /* CONFIG_COMMANDS & CFG_CMD_IRQ */