interrupts.c 2.9 KB

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