interrupts.c 9.6 KB

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