interrupts.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*
  2. * (C) Copyright 2000-2002
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * (C) Copyright 2003, 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 <nios.h>
  27. #include <nios-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. /****************************************************************************/
  36. struct irq_action {
  37. interrupt_handler_t *handler;
  38. void *arg;
  39. int count;
  40. };
  41. static struct irq_action irq_vecs[64];
  42. /*************************************************************************/
  43. volatile ulong timestamp = 0;
  44. void reset_timer (void)
  45. {
  46. timestamp = 0;
  47. }
  48. ulong get_timer (ulong base)
  49. {
  50. WATCHDOG_RESET ();
  51. return (timestamp - base);
  52. }
  53. void set_timer (ulong t)
  54. {
  55. timestamp = t;
  56. }
  57. /* The board must handle this interrupt if a timer is not
  58. * provided.
  59. */
  60. #if defined(CFG_NIOS_TMRBASE)
  61. void timer_interrupt (struct pt_regs *regs)
  62. {
  63. /* Interrupt is cleared by writing anything to the
  64. * status register.
  65. */
  66. nios_timer_t *tmr = (nios_timer_t *)CFG_NIOS_TMRBASE;
  67. tmr->status = 0;
  68. timestamp += CFG_NIOS_TMRMS;
  69. #ifdef CONFIG_STATUS_LED
  70. status_led_tick(timestamp);
  71. #endif
  72. }
  73. #endif
  74. /*************************************************************************/
  75. int disable_interrupts (void)
  76. {
  77. int val = 0;
  78. /* Writing anything to CLR_IE disables interrupts */
  79. val = rdctl (CTL_STATUS);
  80. wrctl (CTL_CLR_IE, 0);
  81. return (val & STATUS_IE);
  82. }
  83. void enable_interrupts( void )
  84. {
  85. /* Writing anything SET_IE enables interrupts */
  86. wrctl (CTL_SET_IE, 0);
  87. }
  88. void external_interrupt (struct pt_regs *regs)
  89. {
  90. unsigned vec;
  91. vec = (regs->status & STATUS_IPRI) >> 9; /* ipri */
  92. irq_vecs[vec].count++;
  93. if (irq_vecs[vec].handler != NULL) {
  94. (*irq_vecs[vec].handler)(irq_vecs[vec].arg);
  95. } else {
  96. /* A sad side-effect of masking a bogus interrupt is
  97. * that lower priority interrupts will also be disabled.
  98. * This is probably not what we want ... so hang insted.
  99. */
  100. printf ("Unhandled interrupt: 0x%x\n", vec);
  101. disable_interrupts ();
  102. hang ();
  103. }
  104. }
  105. /*************************************************************************/
  106. int interrupt_init (void)
  107. {
  108. int vec;
  109. #if defined(CFG_NIOS_TMRBASE)
  110. nios_timer_t *tmr = (nios_timer_t *)CFG_NIOS_TMRBASE;
  111. tmr->control &= ~NIOS_TIMER_ITO;
  112. tmr->control |= NIOS_TIMER_STOP;
  113. #if defined(CFG_NIOS_TMRCNT)
  114. tmr->periodl = CFG_NIOS_TMRCNT & 0xffff;
  115. tmr->periodh = (CFG_NIOS_TMRCNT >> 16) & 0xffff;
  116. #endif
  117. #endif
  118. for (vec=0; vec<64; vec++ ) {
  119. irq_vecs[vec].handler = NULL;
  120. irq_vecs[vec].arg = NULL;
  121. irq_vecs[vec].count = 0;
  122. }
  123. /* Need timus interruptus -- start the lopri timer */
  124. #if defined(CFG_NIOS_TMRBASE)
  125. tmr->control |= ( NIOS_TIMER_ITO |
  126. NIOS_TIMER_CONT |
  127. NIOS_TIMER_START );
  128. ipri (CFG_NIOS_TMRIRQ + 1);
  129. #endif
  130. enable_interrupts ();
  131. return (0);
  132. }
  133. void irq_install_handler (int vec, interrupt_handler_t *handler, void *arg)
  134. {
  135. struct irq_action *irqa = irq_vecs;
  136. int i = vec;
  137. int flag;
  138. if (irqa[i].handler != NULL) {
  139. printf ("Interrupt vector %d: handler 0x%x "
  140. "replacing 0x%x\n",
  141. vec, (uint)handler, (uint)irqa[i].handler);
  142. }
  143. flag = disable_interrupts ();
  144. irqa[i].handler = handler;
  145. irqa[i].arg = arg;
  146. if (flag )
  147. enable_interrupts ();
  148. }
  149. /*************************************************************************/
  150. #if (CONFIG_COMMANDS & CFG_CMD_IRQ)
  151. int do_irqinfo (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  152. {
  153. int vec;
  154. printf ("\nInterrupt-Information:\n");
  155. printf ("Nr Routine Arg Count\n");
  156. for (vec=0; vec<64; vec++) {
  157. if (irq_vecs[vec].handler != NULL) {
  158. printf ("%02d %08lx %08lx %d\n",
  159. vec,
  160. (ulong)irq_vecs[vec].handler<<1,
  161. (ulong)irq_vecs[vec].arg,
  162. irq_vecs[vec].count);
  163. }
  164. }
  165. return (0);
  166. }
  167. #endif /* CONFIG_COMMANDS & CFG_CMD_IRQ */