interrupts.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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(CONFIG_SYS_NIOS_TMRBASE) && !defined(CONFIG_SYS_NIOS_TMRIRQ)
  38. #error CONFIG_SYS_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. nios_timer_t *tmr =(nios_timer_t *)CONFIG_SYS_NIOS_TMRBASE;
  52. /* From Embedded Peripherals Handbook:
  53. *
  54. * "When the hardware is configured with Writeable period
  55. * disabled, writing to one of the period_n registers causes
  56. * the counter to reset to the fixed Timeout Period specified
  57. * at system generation time."
  58. *
  59. * Here we force a reload to prevent early timeouts from
  60. * get_timer() when the interrupt period is greater than
  61. * than 1 msec.
  62. *
  63. * Simply write to periodl with its own value to force an
  64. * internal counter reload, THEN reset the timestamp.
  65. */
  66. writel (readl (&tmr->periodl), &tmr->periodl);
  67. timestamp = 0;
  68. /* From Embedded Peripherals Handbook:
  69. *
  70. * "Writing to one of the period_n registers stops the internal
  71. * counter, except when the hardware is configured with Start/Stop
  72. * control bits off. If Start/Stop control bits is off, writing
  73. * either register does not stop the counter."
  74. *
  75. * In order to accomodate either configuration, the control
  76. * register is re-written. If the counter is stopped, it will
  77. * be restarted. If it is running, the write is essentially
  78. * a nop.
  79. */
  80. writel (NIOS_TIMER_ITO | NIOS_TIMER_CONT | NIOS_TIMER_START,
  81. &tmr->control);
  82. }
  83. ulong get_timer (ulong base)
  84. {
  85. WATCHDOG_RESET ();
  86. return (timestamp - base);
  87. }
  88. void set_timer (ulong t)
  89. {
  90. timestamp = t;
  91. }
  92. /* The board must handle this interrupt if a timer is not
  93. * provided.
  94. */
  95. #if defined(CONFIG_SYS_NIOS_TMRBASE)
  96. void tmr_isr (void *arg)
  97. {
  98. nios_timer_t *tmr = (nios_timer_t *)arg;
  99. /* Interrupt is cleared by writing anything to the
  100. * status register.
  101. */
  102. writel (0, &tmr->status);
  103. timestamp += CONFIG_SYS_NIOS_TMRMS;
  104. #ifdef CONFIG_STATUS_LED
  105. status_led_tick(timestamp);
  106. #endif
  107. }
  108. static void tmr_init (void)
  109. {
  110. nios_timer_t *tmr =(nios_timer_t *)CONFIG_SYS_NIOS_TMRBASE;
  111. writel (0, &tmr->status);
  112. writel (0, &tmr->control);
  113. writel (NIOS_TIMER_STOP, &tmr->control);
  114. #if defined(CONFIG_SYS_NIOS_TMRCNT)
  115. writel (CONFIG_SYS_NIOS_TMRCNT & 0xffff, &tmr->periodl);
  116. writel ((CONFIG_SYS_NIOS_TMRCNT >> 16) & 0xffff, &tmr->periodh);
  117. #endif
  118. writel (NIOS_TIMER_ITO | NIOS_TIMER_CONT | NIOS_TIMER_START,
  119. &tmr->control);
  120. irq_install_handler (CONFIG_SYS_NIOS_TMRIRQ, tmr_isr, (void *)tmr);
  121. }
  122. #endif /* CONFIG_SYS_NIOS_TMRBASE */
  123. /*************************************************************************/
  124. int disable_interrupts (void)
  125. {
  126. int val = rdctl (CTL_STATUS);
  127. wrctl (CTL_STATUS, val & ~STATUS_IE);
  128. return (val & STATUS_IE);
  129. }
  130. void enable_interrupts( void )
  131. {
  132. int val = rdctl (CTL_STATUS);
  133. wrctl (CTL_STATUS, val | STATUS_IE);
  134. }
  135. void external_interrupt (struct pt_regs *regs)
  136. {
  137. unsigned irqs;
  138. struct irq_action *act;
  139. /* Evaluate only irqs that are both enabled AND pending */
  140. irqs = rdctl (CTL_IENABLE) & rdctl (CTL_IPENDING);
  141. act = vecs;
  142. /* Assume (as does the Nios2 HAL) that bit 0 is highest
  143. * priority. NOTE: There is ALWAYS a handler assigned
  144. * (the default if no other).
  145. */
  146. while (irqs) {
  147. if (irqs & 1) {
  148. act->handler (act->arg);
  149. act->count++;
  150. }
  151. irqs >>=1;
  152. act++;
  153. }
  154. }
  155. static void def_hdlr (void *arg)
  156. {
  157. unsigned irqs = rdctl (CTL_IENABLE);
  158. /* Disable the individual interrupt -- with gratuitous
  159. * warning.
  160. */
  161. irqs &= ~(1 << (int)arg);
  162. wrctl (CTL_IENABLE, irqs);
  163. printf ("WARNING: Disabling unhandled interrupt: %d\n",
  164. (int)arg);
  165. }
  166. /*************************************************************************/
  167. void irq_install_handler (int irq, interrupt_handler_t *hdlr, void *arg)
  168. {
  169. int flag;
  170. struct irq_action *act;
  171. unsigned ena = rdctl (CTL_IENABLE);
  172. if ((irq < 0) || (irq > 31))
  173. return;
  174. act = &vecs[irq];
  175. flag = disable_interrupts ();
  176. if (hdlr) {
  177. act->handler = hdlr;
  178. act->arg = arg;
  179. ena |= (1 << irq); /* enable */
  180. } else {
  181. act->handler = def_hdlr;
  182. act->arg = (void *)irq;
  183. ena &= ~(1 << irq); /* disable */
  184. }
  185. wrctl (CTL_IENABLE, ena);
  186. if (flag) enable_interrupts ();
  187. }
  188. int interrupt_init (void)
  189. {
  190. int i;
  191. /* Assign the default handler to all */
  192. for (i = 0; i < 32; i++) {
  193. vecs[i].handler = def_hdlr;
  194. vecs[i].arg = (void *)i;
  195. vecs[i].count = 0;
  196. }
  197. #if defined(CONFIG_SYS_NIOS_TMRBASE)
  198. tmr_init ();
  199. #endif
  200. enable_interrupts ();
  201. return (0);
  202. }
  203. /*************************************************************************/
  204. #if defined(CONFIG_CMD_IRQ)
  205. int do_irqinfo (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  206. {
  207. int i;
  208. struct irq_action *act = vecs;
  209. printf ("\nInterrupt-Information:\n\n");
  210. printf ("Nr Routine Arg Count\n");
  211. printf ("-----------------------------\n");
  212. for (i=0; i<32; i++) {
  213. if (act->handler != def_hdlr) {
  214. printf ("%02d %08lx %08lx %d\n",
  215. i,
  216. (ulong)act->handler,
  217. (ulong)act->arg,
  218. act->count);
  219. }
  220. act++;
  221. }
  222. printf ("\n");
  223. return (0);
  224. }
  225. #endif