interrupts.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. /*
  89. * This function is derived from Blackfin code (read timebase as long long).
  90. * On Nios2 it just returns the timer value.
  91. */
  92. unsigned long long get_ticks(void)
  93. {
  94. return get_timer(0);
  95. }
  96. /*
  97. * This function is derived from Blackfin code.
  98. * On Nios2 it returns the number of timer ticks per second.
  99. */
  100. ulong get_tbclk(void)
  101. {
  102. ulong tbclk;
  103. tbclk = CONFIG_SYS_HZ;
  104. return tbclk;
  105. }
  106. /* The board must handle this interrupt if a timer is not
  107. * provided.
  108. */
  109. #if defined(CONFIG_SYS_NIOS_TMRBASE)
  110. void tmr_isr (void *arg)
  111. {
  112. nios_timer_t *tmr = (nios_timer_t *)arg;
  113. /* Interrupt is cleared by writing anything to the
  114. * status register.
  115. */
  116. writel (0, &tmr->status);
  117. timestamp += CONFIG_SYS_NIOS_TMRMS;
  118. #ifdef CONFIG_STATUS_LED
  119. status_led_tick(timestamp);
  120. #endif
  121. }
  122. static void tmr_init (void)
  123. {
  124. nios_timer_t *tmr =(nios_timer_t *)CONFIG_SYS_NIOS_TMRBASE;
  125. writel (0, &tmr->status);
  126. writel (0, &tmr->control);
  127. writel (NIOS_TIMER_STOP, &tmr->control);
  128. #if defined(CONFIG_SYS_NIOS_TMRCNT)
  129. writel (CONFIG_SYS_NIOS_TMRCNT & 0xffff, &tmr->periodl);
  130. writel ((CONFIG_SYS_NIOS_TMRCNT >> 16) & 0xffff, &tmr->periodh);
  131. #endif
  132. writel (NIOS_TIMER_ITO | NIOS_TIMER_CONT | NIOS_TIMER_START,
  133. &tmr->control);
  134. irq_install_handler (CONFIG_SYS_NIOS_TMRIRQ, tmr_isr, (void *)tmr);
  135. }
  136. #endif /* CONFIG_SYS_NIOS_TMRBASE */
  137. /*************************************************************************/
  138. int disable_interrupts (void)
  139. {
  140. int val = rdctl (CTL_STATUS);
  141. wrctl (CTL_STATUS, val & ~STATUS_IE);
  142. return (val & STATUS_IE);
  143. }
  144. void enable_interrupts( void )
  145. {
  146. int val = rdctl (CTL_STATUS);
  147. wrctl (CTL_STATUS, val | STATUS_IE);
  148. }
  149. void external_interrupt (struct pt_regs *regs)
  150. {
  151. unsigned irqs;
  152. struct irq_action *act;
  153. /* Evaluate only irqs that are both enabled AND pending */
  154. irqs = rdctl (CTL_IENABLE) & rdctl (CTL_IPENDING);
  155. act = vecs;
  156. /* Assume (as does the Nios2 HAL) that bit 0 is highest
  157. * priority. NOTE: There is ALWAYS a handler assigned
  158. * (the default if no other).
  159. */
  160. while (irqs) {
  161. if (irqs & 1) {
  162. act->handler (act->arg);
  163. act->count++;
  164. }
  165. irqs >>=1;
  166. act++;
  167. }
  168. }
  169. static void def_hdlr (void *arg)
  170. {
  171. unsigned irqs = rdctl (CTL_IENABLE);
  172. /* Disable the individual interrupt -- with gratuitous
  173. * warning.
  174. */
  175. irqs &= ~(1 << (int)arg);
  176. wrctl (CTL_IENABLE, irqs);
  177. printf ("WARNING: Disabling unhandled interrupt: %d\n",
  178. (int)arg);
  179. }
  180. /*************************************************************************/
  181. void irq_install_handler (int irq, interrupt_handler_t *hdlr, void *arg)
  182. {
  183. int flag;
  184. struct irq_action *act;
  185. unsigned ena = rdctl (CTL_IENABLE);
  186. if ((irq < 0) || (irq > 31))
  187. return;
  188. act = &vecs[irq];
  189. flag = disable_interrupts ();
  190. if (hdlr) {
  191. act->handler = hdlr;
  192. act->arg = arg;
  193. ena |= (1 << irq); /* enable */
  194. } else {
  195. act->handler = def_hdlr;
  196. act->arg = (void *)irq;
  197. ena &= ~(1 << irq); /* disable */
  198. }
  199. wrctl (CTL_IENABLE, ena);
  200. if (flag) enable_interrupts ();
  201. }
  202. int interrupt_init (void)
  203. {
  204. int i;
  205. /* Assign the default handler to all */
  206. for (i = 0; i < 32; i++) {
  207. vecs[i].handler = def_hdlr;
  208. vecs[i].arg = (void *)i;
  209. vecs[i].count = 0;
  210. }
  211. #if defined(CONFIG_SYS_NIOS_TMRBASE)
  212. tmr_init ();
  213. #endif
  214. enable_interrupts ();
  215. return (0);
  216. }
  217. /*************************************************************************/
  218. #if defined(CONFIG_CMD_IRQ)
  219. int do_irqinfo (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  220. {
  221. int i;
  222. struct irq_action *act = vecs;
  223. printf ("\nInterrupt-Information:\n\n");
  224. printf ("Nr Routine Arg Count\n");
  225. printf ("-----------------------------\n");
  226. for (i=0; i<32; i++) {
  227. if (act->handler != def_hdlr) {
  228. printf ("%02d %08lx %08lx %d\n",
  229. i,
  230. (ulong)act->handler,
  231. (ulong)act->arg,
  232. act->count);
  233. }
  234. act++;
  235. }
  236. printf ("\n");
  237. return (0);
  238. }
  239. #endif