interrupts.c 5.1 KB

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