interrupts.c 4.3 KB

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