interrupts.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /*
  2. * (C) Copyright 2002
  3. * John W. Linville <linville@tuxdriver.com>
  4. *
  5. * Copied and modified from original code by Josh Huber. Original
  6. * copyright notice preserved below.
  7. *
  8. * (C) Copyright 2001
  9. * Josh Huber <huber@mclx.com>, Mission Critical Linux, Inc.
  10. *
  11. * See file CREDITS for list of people who contributed to this
  12. * project.
  13. *
  14. * This program is free software; you can redistribute it and/or
  15. * modify it under the terms of the GNU General Public License as
  16. * published by the Free Software Foundation; either version 2 of
  17. * the License, or (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program; if not, write to the Free Software
  26. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  27. * MA 02111-1307 USA
  28. */
  29. /*
  30. * interrupts.c - just enough support for the decrementer/timer
  31. */
  32. #include <common.h>
  33. #include <asm/processor.h>
  34. #include <command.h>
  35. #include "i8259.h"
  36. #undef DEBUG
  37. #ifdef DEBUG
  38. #define PRINTF(fmt,args...) printf (fmt ,##args)
  39. #else
  40. #define PRINTF(fmt,args...)
  41. #endif
  42. #define NR_IRQS 16
  43. void irq_alloc_init(void);
  44. long irq_alloc(long wanted);
  45. /****************************************************************************/
  46. unsigned decrementer_count; /* count value for 1e6/HZ microseconds */
  47. struct irq_action {
  48. interrupt_handler_t *handler;
  49. void *arg;
  50. ulong count;
  51. };
  52. static struct irq_action irq_handlers[NR_IRQS];
  53. /****************************************************************************/
  54. static __inline__ unsigned long
  55. get_msr(void)
  56. {
  57. unsigned long msr;
  58. asm volatile("mfmsr %0" : "=r" (msr) :);
  59. return msr;
  60. }
  61. static __inline__ void
  62. set_msr(unsigned long msr)
  63. {
  64. asm volatile("mtmsr %0" : : "r" (msr));
  65. }
  66. static __inline__ unsigned long
  67. get_dec(void)
  68. {
  69. unsigned long val;
  70. asm volatile("mfdec %0" : "=r" (val) :);
  71. return val;
  72. }
  73. static __inline__ void
  74. set_dec(unsigned long val)
  75. {
  76. asm volatile("mtdec %0" : : "r" (val));
  77. }
  78. void
  79. enable_interrupts(void)
  80. {
  81. set_msr (get_msr() | MSR_EE);
  82. }
  83. /* returns flag if MSR_EE was set before */
  84. int
  85. disable_interrupts(void)
  86. {
  87. ulong msr;
  88. msr = get_msr();
  89. set_msr (msr & ~MSR_EE);
  90. return ((msr & MSR_EE) != 0);
  91. }
  92. /****************************************************************************/
  93. int interrupt_init (void)
  94. {
  95. extern void new_reset(void);
  96. extern void new_reset_end(void);
  97. #ifdef DEBUG
  98. puts("interrupt_init: setting decrementer_count\n");
  99. #endif
  100. decrementer_count = get_tbclk() / CONFIG_SYS_HZ;
  101. #ifdef DEBUG
  102. puts("interrupt_init: setting actual decremter\n");
  103. #endif
  104. set_dec (get_tbclk() / CONFIG_SYS_HZ);
  105. #ifdef DEBUG
  106. puts("interrupt_init: clearing external interrupt table\n");
  107. #endif
  108. /* clear external interrupt table here */
  109. memset(irq_handlers, 0, sizeof(irq_handlers));
  110. #ifdef DEBUG
  111. puts("interrupt_init: initializing interrupt controller\n");
  112. #endif
  113. i8259_init();
  114. #ifdef DEBUG
  115. puts("Copying reset trampoline\n");
  116. #endif
  117. /* WARNING: Assmues that the first megabyte is CACHEINHIBIT! */
  118. memcpy((void *)0x100, new_reset, new_reset_end - new_reset);
  119. #ifdef DEBUG
  120. PRINTF("interrupt_init: enabling interrupts (msr = %08x)\n",
  121. get_msr());
  122. #endif
  123. set_msr (get_msr() | MSR_EE);
  124. #ifdef DEBUG
  125. PRINTF("interrupt_init: done. (msr = %08x)\n", get_msr());
  126. #endif
  127. }
  128. /****************************************************************************/
  129. /*
  130. * Handle external interrupts
  131. */
  132. void
  133. external_interrupt(struct pt_regs *regs)
  134. {
  135. extern int i8259_irq(void);
  136. int irq, unmask = 1;
  137. irq = i8259_irq(); /*i8259_get_irq(regs); */
  138. /* printf("irq = %d, handler at %p ack=%d\n", irq, irq_handlers[irq].handler, *(volatile unsigned char *)0xFEF00000); */
  139. i8259_mask_and_ack(irq);
  140. if (irq_handlers[irq].handler != NULL)
  141. (*irq_handlers[irq].handler)(irq_handlers[irq].arg);
  142. else {
  143. PRINTF ("\nBogus External Interrupt IRQ %d\n", irq);
  144. /*
  145. * turn off the bogus interrupt, otherwise it
  146. * might repeat forever
  147. */
  148. unmask = 0;
  149. }
  150. if (unmask) i8259_unmask_irq(irq);
  151. }
  152. volatile ulong timestamp = 0;
  153. /*
  154. * timer_interrupt - gets called when the decrementer overflows,
  155. * with interrupts disabled.
  156. * Trivial implementation - no need to be really accurate.
  157. */
  158. void
  159. timer_interrupt(struct pt_regs *regs)
  160. {
  161. set_dec(decrementer_count);
  162. timestamp++;
  163. }
  164. /****************************************************************************/
  165. void
  166. reset_timer(void)
  167. {
  168. timestamp = 0;
  169. }
  170. ulong
  171. get_timer(ulong base)
  172. {
  173. return (timestamp - base);
  174. }
  175. void
  176. set_timer(ulong t)
  177. {
  178. timestamp = t;
  179. }
  180. /****************************************************************************/
  181. /*
  182. * Install and free a interrupt handler.
  183. */
  184. void
  185. irq_install_handler(int irq, interrupt_handler_t *handler, void *arg)
  186. {
  187. if (irq < 0 || irq >= NR_IRQS) {
  188. PRINTF("irq_install_handler: bad irq number %d\n", irq);
  189. return;
  190. }
  191. if (irq_handlers[irq].handler != NULL)
  192. PRINTF("irq_install_handler: 0x%08lx replacing 0x%08lx\n",
  193. (ulong)handler, (ulong)irq_handlers[irq].handler);
  194. irq_handlers[irq].handler = handler;
  195. irq_handlers[irq].arg = arg;
  196. i8259_unmask_irq(irq);
  197. }
  198. void
  199. irq_free_handler(int irq)
  200. {
  201. if (irq < 0 || irq >= NR_IRQS) {
  202. PRINTF("irq_free_handler: bad irq number %d\n", irq);
  203. return;
  204. }
  205. i8259_mask_irq(irq);
  206. irq_handlers[irq].handler = NULL;
  207. irq_handlers[irq].arg = NULL;
  208. }
  209. /****************************************************************************/
  210. void
  211. do_irqinfo(cmd_tbl_t *cmdtp, bd_t *bd, int flag, int argc, char *argv[])
  212. {
  213. puts("IRQ related functions are unimplemented currently.\n");
  214. }