interrupts.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2000-2002
  4. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  5. *
  6. * (C) Copyright 2004, Psyent Corporation <www.psyent.com>
  7. * Scott McNutt <smcnutt@psyent.com>
  8. */
  9. #include <common.h>
  10. #include <command.h>
  11. #include <asm/nios2.h>
  12. #include <asm/types.h>
  13. #include <asm/io.h>
  14. #include <asm/ptrace.h>
  15. /*************************************************************************/
  16. struct irq_action {
  17. interrupt_handler_t *handler;
  18. void *arg;
  19. int count;
  20. };
  21. static struct irq_action vecs[32];
  22. int disable_interrupts (void)
  23. {
  24. int val = rdctl (CTL_STATUS);
  25. wrctl (CTL_STATUS, val & ~STATUS_IE);
  26. return (val & STATUS_IE);
  27. }
  28. void enable_interrupts( void )
  29. {
  30. int val = rdctl (CTL_STATUS);
  31. wrctl (CTL_STATUS, val | STATUS_IE);
  32. }
  33. void external_interrupt (struct pt_regs *regs)
  34. {
  35. unsigned irqs;
  36. struct irq_action *act;
  37. /* Evaluate only irqs that are both enabled AND pending */
  38. irqs = rdctl (CTL_IENABLE) & rdctl (CTL_IPENDING);
  39. act = vecs;
  40. /* Assume (as does the Nios2 HAL) that bit 0 is highest
  41. * priority. NOTE: There is ALWAYS a handler assigned
  42. * (the default if no other).
  43. */
  44. while (irqs) {
  45. if (irqs & 1) {
  46. act->handler (act->arg);
  47. act->count++;
  48. }
  49. irqs >>=1;
  50. act++;
  51. }
  52. }
  53. static void def_hdlr (void *arg)
  54. {
  55. unsigned irqs = rdctl (CTL_IENABLE);
  56. /* Disable the individual interrupt -- with gratuitous
  57. * warning.
  58. */
  59. irqs &= ~(1 << (int)arg);
  60. wrctl (CTL_IENABLE, irqs);
  61. printf ("WARNING: Disabling unhandled interrupt: %d\n",
  62. (int)arg);
  63. }
  64. /*************************************************************************/
  65. void irq_install_handler (int irq, interrupt_handler_t *hdlr, void *arg)
  66. {
  67. int flag;
  68. struct irq_action *act;
  69. unsigned ena = rdctl (CTL_IENABLE);
  70. if ((irq < 0) || (irq > 31))
  71. return;
  72. act = &vecs[irq];
  73. flag = disable_interrupts ();
  74. if (hdlr) {
  75. act->handler = hdlr;
  76. act->arg = arg;
  77. ena |= (1 << irq); /* enable */
  78. } else {
  79. act->handler = def_hdlr;
  80. act->arg = (void *)irq;
  81. ena &= ~(1 << irq); /* disable */
  82. }
  83. wrctl (CTL_IENABLE, ena);
  84. if (flag) enable_interrupts ();
  85. }
  86. int interrupt_init (void)
  87. {
  88. int i;
  89. /* Assign the default handler to all */
  90. for (i = 0; i < 32; i++) {
  91. vecs[i].handler = def_hdlr;
  92. vecs[i].arg = (void *)i;
  93. vecs[i].count = 0;
  94. }
  95. enable_interrupts ();
  96. return (0);
  97. }
  98. /*************************************************************************/
  99. #if defined(CONFIG_CMD_IRQ)
  100. int do_irqinfo (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  101. {
  102. int i;
  103. struct irq_action *act = vecs;
  104. printf ("\nInterrupt-Information:\n\n");
  105. printf ("Nr Routine Arg Count\n");
  106. printf ("-----------------------------\n");
  107. for (i=0; i<32; i++) {
  108. if (act->handler != def_hdlr) {
  109. printf ("%02d %08lx %08lx %d\n",
  110. i,
  111. (ulong)act->handler,
  112. (ulong)act->arg,
  113. act->count);
  114. }
  115. act++;
  116. }
  117. printf ("\n");
  118. return (0);
  119. }
  120. #endif