interrupts.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. /*
  2. * (C) Copyright 2000-2002
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. *
  23. * Hacked for MPC8260 by Murray.Jensen@cmst.csiro.au, 22-Oct-00
  24. */
  25. #include <common.h>
  26. #include <watchdog.h>
  27. #include <command.h>
  28. #include <mpc8260.h>
  29. #include <mpc8260_irq.h>
  30. #include <asm/processor.h>
  31. /****************************************************************************/
  32. unsigned decrementer_count; /* count val for 1e6/HZ microseconds */
  33. struct irq_action {
  34. interrupt_handler_t *handler;
  35. void *arg;
  36. ulong count;
  37. };
  38. static struct irq_action irq_handlers[NR_IRQS];
  39. static ulong ppc_cached_irq_mask[NR_MASK_WORDS];
  40. /****************************************************************************/
  41. /* this section was ripped out of arch/ppc/kernel/ppc8260_pic.c in the */
  42. /* Linux/PPC 2.4.x source. There was no copyright notice in that file. */
  43. /* The 8260 internal interrupt controller. It is usually
  44. * the only interrupt controller.
  45. * There are two 32-bit registers (high/low) for up to 64
  46. * possible interrupts.
  47. *
  48. * Now, the fun starts.....Interrupt Numbers DO NOT MAP
  49. * in a simple arithmetic fashion to mask or pending registers.
  50. * That is, interrupt 4 does not map to bit position 4.
  51. * We create two tables, indexed by vector number, to indicate
  52. * which register to use and which bit in the register to use.
  53. */
  54. static u_char irq_to_siureg[] = {
  55. 1, 1, 1, 1, 1, 1, 1, 1,
  56. 1, 1, 1, 1, 1, 1, 1, 1,
  57. 0, 0, 0, 0, 0, 0, 0, 0,
  58. 0, 0, 0, 0, 0, 0, 0, 0,
  59. 1, 1, 1, 1, 1, 1, 1, 1,
  60. 1, 1, 1, 1, 1, 1, 1, 1,
  61. 0, 0, 0, 0, 0, 0, 0, 0,
  62. 0, 0, 0, 0, 0, 0, 0, 0
  63. };
  64. static u_char irq_to_siubit[] = {
  65. 31, 16, 17, 18, 19, 20, 21, 22,
  66. 23, 24, 25, 26, 27, 28, 29, 30,
  67. 29, 30, 16, 17, 18, 19, 20, 21,
  68. 22, 23, 24, 25, 26, 27, 28, 31,
  69. 0, 1, 2, 3, 4, 5, 6, 7,
  70. 8, 9, 10, 11, 12, 13, 14, 15,
  71. 15, 14, 13, 12, 11, 10, 9, 8,
  72. 7, 6, 5, 4, 3, 2, 1, 0
  73. };
  74. static void m8260_mask_irq (unsigned int irq_nr)
  75. {
  76. volatile immap_t *immr = (immap_t *) CFG_IMMR;
  77. int bit, word;
  78. volatile uint *simr;
  79. bit = irq_to_siubit[irq_nr];
  80. word = irq_to_siureg[irq_nr];
  81. simr = &(immr->im_intctl.ic_simrh);
  82. ppc_cached_irq_mask[word] &= ~(1 << (31 - bit));
  83. simr[word] = ppc_cached_irq_mask[word];
  84. }
  85. static void m8260_unmask_irq (unsigned int irq_nr)
  86. {
  87. volatile immap_t *immr = (immap_t *) CFG_IMMR;
  88. int bit, word;
  89. volatile uint *simr;
  90. bit = irq_to_siubit[irq_nr];
  91. word = irq_to_siureg[irq_nr];
  92. simr = &(immr->im_intctl.ic_simrh);
  93. ppc_cached_irq_mask[word] |= (1 << (31 - bit));
  94. simr[word] = ppc_cached_irq_mask[word];
  95. }
  96. static void m8260_mask_and_ack (unsigned int irq_nr)
  97. {
  98. volatile immap_t *immr = (immap_t *) CFG_IMMR;
  99. int bit, word;
  100. volatile uint *simr, *sipnr;
  101. bit = irq_to_siubit[irq_nr];
  102. word = irq_to_siureg[irq_nr];
  103. simr = &(immr->im_intctl.ic_simrh);
  104. sipnr = &(immr->im_intctl.ic_sipnrh);
  105. ppc_cached_irq_mask[word] &= ~(1 << (31 - bit));
  106. simr[word] = ppc_cached_irq_mask[word];
  107. sipnr[word] = 1 << (31 - bit);
  108. }
  109. static int m8260_get_irq (struct pt_regs *regs)
  110. {
  111. volatile immap_t *immr = (immap_t *) CFG_IMMR;
  112. int irq;
  113. unsigned long bits;
  114. /* For MPC8260, read the SIVEC register and shift the bits down
  115. * to get the irq number. */
  116. bits = immr->im_intctl.ic_sivec;
  117. irq = bits >> 26;
  118. return irq;
  119. }
  120. /* end of code ripped out of arch/ppc/kernel/ppc8260_pic.c */
  121. /****************************************************************************/
  122. static __inline__ unsigned long get_msr (void)
  123. {
  124. unsigned long msr;
  125. __asm__ __volatile__ ("mfmsr %0":"=r" (msr):);
  126. return msr;
  127. }
  128. static __inline__ void set_msr (unsigned long msr)
  129. {
  130. __asm__ __volatile__ ("mtmsr %0"::"r" (msr));
  131. }
  132. static __inline__ unsigned long get_dec (void)
  133. {
  134. unsigned long val;
  135. __asm__ __volatile__ ("mfdec %0":"=r" (val):);
  136. return val;
  137. }
  138. static __inline__ void set_dec (unsigned long val)
  139. {
  140. __asm__ __volatile__ ("mtdec %0"::"r" (val));
  141. }
  142. void enable_interrupts (void)
  143. {
  144. set_msr (get_msr () | MSR_EE);
  145. }
  146. /* returns flag if MSR_EE was set before */
  147. int disable_interrupts (void)
  148. {
  149. ulong msr = get_msr ();
  150. set_msr (msr & ~MSR_EE);
  151. return ((msr & MSR_EE) != 0);
  152. }
  153. /****************************************************************************/
  154. int interrupt_init (void)
  155. {
  156. DECLARE_GLOBAL_DATA_PTR;
  157. volatile immap_t *immr = (immap_t *) CFG_IMMR;
  158. decrementer_count = (gd->bus_clk / 4) / CFG_HZ;
  159. /* Initialize the default interrupt mapping priorities */
  160. immr->im_intctl.ic_sicr = 0;
  161. immr->im_intctl.ic_siprr = 0x05309770;
  162. immr->im_intctl.ic_scprrh = 0x05309770;
  163. immr->im_intctl.ic_scprrl = 0x05309770;
  164. /* disable all interrupts and clear all pending bits */
  165. immr->im_intctl.ic_simrh = ppc_cached_irq_mask[0] = 0;
  166. immr->im_intctl.ic_simrl = ppc_cached_irq_mask[1] = 0;
  167. immr->im_intctl.ic_sipnrh = 0xffffffff;
  168. immr->im_intctl.ic_sipnrl = 0xffffffff;
  169. set_dec (decrementer_count);
  170. set_msr (get_msr () | MSR_EE);
  171. return (0);
  172. }
  173. /****************************************************************************/
  174. /*
  175. * Handle external interrupts
  176. */
  177. void external_interrupt (struct pt_regs *regs)
  178. {
  179. int irq, unmask = 1;
  180. irq = m8260_get_irq (regs);
  181. m8260_mask_and_ack (irq);
  182. set_msr (get_msr () | MSR_EE);
  183. if (irq_handlers[irq].handler != NULL)
  184. (*irq_handlers[irq].handler) (irq_handlers[irq].arg);
  185. else {
  186. printf ("\nBogus External Interrupt IRQ %d\n", irq);
  187. /*
  188. * turn off the bogus interrupt, otherwise it
  189. * might repeat forever
  190. */
  191. unmask = 0;
  192. }
  193. if (unmask)
  194. m8260_unmask_irq (irq);
  195. }
  196. /****************************************************************************/
  197. /*
  198. * Install and free an interrupt handler.
  199. */
  200. void
  201. irq_install_handler (int irq, interrupt_handler_t * handler, void *arg)
  202. {
  203. if (irq < 0 || irq >= NR_IRQS) {
  204. printf ("irq_install_handler: bad irq number %d\n", irq);
  205. return;
  206. }
  207. if (irq_handlers[irq].handler != NULL)
  208. printf ("irq_install_handler: 0x%08lx replacing 0x%08lx\n",
  209. (ulong) handler, (ulong) irq_handlers[irq].handler);
  210. irq_handlers[irq].handler = handler;
  211. irq_handlers[irq].arg = arg;
  212. m8260_unmask_irq (irq);
  213. }
  214. void irq_free_handler (int irq)
  215. {
  216. if (irq < 0 || irq >= NR_IRQS) {
  217. printf ("irq_free_handler: bad irq number %d\n", irq);
  218. return;
  219. }
  220. m8260_mask_irq (irq);
  221. irq_handlers[irq].handler = NULL;
  222. irq_handlers[irq].arg = NULL;
  223. }
  224. /****************************************************************************/
  225. volatile ulong timestamp = 0;
  226. /*
  227. * timer_interrupt - gets called when the decrementer overflows,
  228. * with interrupts disabled.
  229. * Trivial implementation - no need to be really accurate.
  230. */
  231. void timer_interrupt (struct pt_regs *regs)
  232. {
  233. #if defined(CONFIG_WATCHDOG) || defined(CFG_HYMOD_DBLEDS)
  234. volatile immap_t *immr = (immap_t *) CFG_IMMR;
  235. #endif /* CONFIG_WATCHDOG */
  236. /* Restore Decrementer Count */
  237. set_dec (decrementer_count);
  238. timestamp++;
  239. #if defined(CONFIG_WATCHDOG) || \
  240. defined(CFG_CMA_LCD_HEARTBEAT) || \
  241. defined(CFG_HYMOD_DBLEDS)
  242. if ((timestamp % CFG_HZ) == 0) {
  243. #if defined(CFG_CMA_LCD_HEARTBEAT)
  244. extern void lcd_heartbeat (void);
  245. #endif /* CFG_CMA_LCD_HEARTBEAT */
  246. #if defined(CFG_HYMOD_DBLEDS)
  247. volatile iop8260_t *iop = &immr->im_ioport;
  248. static int shift = 0;
  249. #endif /* CFG_HYMOD_DBLEDS */
  250. #if defined(CFG_CMA_LCD_HEARTBEAT)
  251. lcd_heartbeat ();
  252. #endif /* CFG_CMA_LCD_HEARTBEAT */
  253. #if defined(CONFIG_WATCHDOG)
  254. reset_8260_watchdog (immr);
  255. #endif /* CONFIG_WATCHDOG */
  256. #if defined(CFG_HYMOD_DBLEDS)
  257. /* hymod daughter board LEDs */
  258. if (++shift > 3)
  259. shift = 0;
  260. iop->iop_pdatd =
  261. (iop->iop_pdatd & ~0x0f000000) | (1 << (24 + shift));
  262. #endif /* CFG_HYMOD_DBLEDS */
  263. }
  264. #endif /* CONFIG_WATCHDOG || CFG_CMA_LCD_HEARTBEAT */
  265. }
  266. /****************************************************************************/
  267. void reset_timer (void)
  268. {
  269. timestamp = 0;
  270. }
  271. ulong get_timer (ulong base)
  272. {
  273. return (timestamp - base);
  274. }
  275. void set_timer (ulong t)
  276. {
  277. timestamp = t;
  278. }
  279. /****************************************************************************/
  280. #if (CONFIG_COMMANDS & CFG_CMD_IRQ)
  281. /* ripped this out of ppc4xx/interrupts.c */
  282. /*******************************************************************************
  283. *
  284. * irqinfo - print information about PCI devices
  285. *
  286. */
  287. void
  288. do_irqinfo (cmd_tbl_t * cmdtp, bd_t * bd, int flag, int argc, char *argv[])
  289. {
  290. int irq, re_enable;
  291. re_enable = disable_interrupts ();
  292. printf ("\nInterrupt-Information:\n");
  293. printf ("Nr Routine Arg Count\n");
  294. for (irq = 0; irq < 32; irq++)
  295. if (irq_handlers[irq].handler != NULL)
  296. printf ("%02d %08lx %08lx %ld\n", irq,
  297. (ulong) irq_handlers[irq].handler,
  298. (ulong) irq_handlers[irq].arg,
  299. irq_handlers[irq].count);
  300. if (re_enable)
  301. enable_interrupts ();
  302. }
  303. #endif /* CONFIG_COMMANDS & CFG_CMD_IRQ */