interrupts.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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/io.h>
  29. #include <asm/ptrace.h>
  30. #include <common.h>
  31. #include <command.h>
  32. #include <watchdog.h>
  33. #ifdef CONFIG_STATUS_LED
  34. #include <status_led.h>
  35. #endif
  36. #if defined(CFG_NIOS_TMRBASE) && !defined(CFG_NIOS_TMRIRQ)
  37. #error CFG_NIOS_TMRIRQ not defined (see documentation)
  38. #endif
  39. /****************************************************************************/
  40. struct irq_action {
  41. interrupt_handler_t *handler;
  42. void *arg;
  43. int count;
  44. };
  45. static struct irq_action vecs[32];
  46. /*************************************************************************/
  47. volatile ulong timestamp = 0;
  48. void reset_timer (void)
  49. {
  50. timestamp = 0;
  51. }
  52. ulong get_timer (ulong base)
  53. {
  54. WATCHDOG_RESET ();
  55. return (timestamp - base);
  56. }
  57. void set_timer (ulong t)
  58. {
  59. timestamp = t;
  60. }
  61. /* The board must handle this interrupt if a timer is not
  62. * provided.
  63. */
  64. #if defined(CFG_NIOS_TMRBASE)
  65. void tmr_isr (void *arg)
  66. {
  67. nios_timer_t *tmr = (nios_timer_t *)arg;
  68. /* Interrupt is cleared by writing anything to the
  69. * status register.
  70. */
  71. writel (&tmr->status, 0);
  72. timestamp += CFG_NIOS_TMRMS;
  73. #ifdef CONFIG_STATUS_LED
  74. status_led_tick(timestamp);
  75. #endif
  76. }
  77. static void tmr_init (void)
  78. {
  79. nios_timer_t *tmr =(nios_timer_t *)CFG_NIOS_TMRBASE;
  80. writel (&tmr->status, 0);
  81. writel (&tmr->control, 0);
  82. writel (&tmr->control, NIOS_TIMER_STOP);
  83. #if defined(CFG_NIOS_TMRCNT)
  84. writel (&tmr->periodl, CFG_NIOS_TMRCNT & 0xffff);
  85. writel (&tmr->periodh, (CFG_NIOS_TMRCNT >> 16) & 0xffff);
  86. #endif
  87. writel (&tmr->control, NIOS_TIMER_ITO | NIOS_TIMER_CONT |
  88. NIOS_TIMER_START );
  89. irq_install_handler (CFG_NIOS_TMRIRQ, tmr_isr, (void *)tmr);
  90. }
  91. #endif /* CFG_NIOS_TMRBASE */
  92. /*************************************************************************/
  93. int disable_interrupts (void)
  94. {
  95. int val = rdctl (CTL_STATUS);
  96. wrctl (CTL_STATUS, val & ~STATUS_IE);
  97. return (val & STATUS_IE);
  98. }
  99. void enable_interrupts( void )
  100. {
  101. int val = rdctl (CTL_STATUS);
  102. wrctl (CTL_STATUS, val | STATUS_IE);
  103. }
  104. void external_interrupt (struct pt_regs *regs)
  105. {
  106. unsigned irqs;
  107. struct irq_action *act;
  108. /* Evaluate only irqs that are both enabled AND pending */
  109. irqs = rdctl (CTL_IENABLE) & rdctl (CTL_IPENDING);
  110. act = vecs;
  111. /* Assume (as does the Nios2 HAL) that bit 0 is highest
  112. * priority. NOTE: There is ALWAYS a handler assigned
  113. * (the default if no other).
  114. */
  115. while (irqs) {
  116. if (irqs & 1) {
  117. act->handler (act->arg);
  118. act->count++;
  119. }
  120. irqs >>=1;
  121. act++;
  122. }
  123. }
  124. static void def_hdlr (void *arg)
  125. {
  126. unsigned irqs = rdctl (CTL_IENABLE);
  127. /* Disable the individual interrupt -- with gratuitous
  128. * warning.
  129. */
  130. irqs &= ~(1 << (int)arg);
  131. wrctl (CTL_IENABLE, irqs);
  132. printf ("WARNING: Disabling unhandled interrupt: %d\n",
  133. (int)arg);
  134. }
  135. /*************************************************************************/
  136. void irq_install_handler (int irq, interrupt_handler_t *hdlr, void *arg)
  137. {
  138. int flag;
  139. struct irq_action *act;
  140. unsigned ena = rdctl (CTL_IENABLE);
  141. if ((irq < 0) || (irq > 31))
  142. return;
  143. act = &vecs[irq];
  144. flag = disable_interrupts ();
  145. if (hdlr) {
  146. act->handler = hdlr;
  147. act->arg = arg;
  148. ena |= (1 << irq); /* enable */
  149. } else {
  150. act->handler = def_hdlr;
  151. act->arg = (void *)irq;
  152. ena &= ~(1 << irq); /* disable */
  153. }
  154. wrctl (CTL_IENABLE, ena);
  155. if (flag) enable_interrupts ();
  156. }
  157. int interrupt_init (void)
  158. {
  159. int i;
  160. /* Assign the default handler to all */
  161. for (i = 0; i < 32; i++) {
  162. vecs[i].handler = def_hdlr;
  163. vecs[i].arg = (void *)i;
  164. vecs[i].count = 0;
  165. }
  166. #if defined(CFG_NIOS_TMRBASE)
  167. tmr_init ();
  168. #endif
  169. enable_interrupts ();
  170. return (0);
  171. }
  172. /*************************************************************************/
  173. #if (CONFIG_COMMANDS & CFG_CMD_IRQ)
  174. int do_irqinfo (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  175. {
  176. int i;
  177. struct irq_action *act = vecs;
  178. printf ("\nInterrupt-Information:\n\n");
  179. printf ("Nr Routine Arg Count\n");
  180. printf ("-----------------------------\n");
  181. for (i=0; i<32; i++) {
  182. if (act->handler != def_hdlr) {
  183. printf ("%02d %08lx %08lx %d\n",
  184. i,
  185. (ulong)act->handler,
  186. (ulong)act->arg,
  187. act->count);
  188. }
  189. act++;
  190. }
  191. printf ("\n");
  192. return (0);
  193. }
  194. #endif /* CONFIG_COMMANDS & CFG_CMD_IRQ */