interrupts.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. #if defined(CFG_NIOS_TMRCNT)
  112. tmr->periodl = CFG_NIOS_TMRCNT & 0xffff;
  113. tmr->periodh = (CFG_NIOS_TMRCNT >> 16) & 0xffff;
  114. #endif
  115. #endif
  116. for (vec=0; vec<64; vec++ ) {
  117. irq_vecs[vec].handler = NULL;
  118. irq_vecs[vec].arg = NULL;
  119. irq_vecs[vec].count = 0;
  120. }
  121. /* Need timus interruptus -- start the lopri timer */
  122. #if defined(CFG_NIOS_TMRBASE)
  123. tmr->control |= ( NIOS_TIMER_ITO |
  124. NIOS_TIMER_CONT |
  125. NIOS_TIMER_START );
  126. ipri (CFG_NIOS_TMRIRQ + 1);
  127. #endif
  128. enable_interrupts ();
  129. return (0);
  130. }
  131. void irq_install_handler (int vec, interrupt_handler_t *handler, void *arg)
  132. {
  133. struct irq_action *irqa = irq_vecs;
  134. int i = vec;
  135. int flag;
  136. if (irqa[i].handler != NULL) {
  137. printf ("Interrupt vector %d: handler 0x%x "
  138. "replacing 0x%x\n",
  139. vec, (uint)handler, (uint)irqa[i].handler);
  140. }
  141. flag = disable_interrupts ();
  142. irqa[i].handler = handler;
  143. irqa[i].arg = arg;
  144. if (flag )
  145. enable_interrupts ();
  146. }
  147. /*************************************************************************/
  148. #if (CONFIG_COMMANDS & CFG_CMD_IRQ)
  149. int do_irqinfo (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  150. {
  151. int vec;
  152. printf ("\nInterrupt-Information:\n");
  153. printf ("Nr Routine Arg CouIt's ok to cnt\n");
  154. for (vec=0; vec<64; vec++) {
  155. if (irq_vecs[vec].handler != NULL) {
  156. printf ("%02d %08lx %08lx %d\n",
  157. vec,
  158. (ulong)irq_vecs[vec].handler<<1,
  159. (ulong)irq_vecs[vec].arg,
  160. irq_vecs[vec].count);
  161. }
  162. }
  163. return (0);
  164. }
  165. #endif /* CONFIG_COMMANDS & CFG_CMD_IRQ */