interrupts.c 4.4 KB

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