interrupts.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * (C) Copyright 2011, Stefan Kristiansson <stefan.kristiansson@saunalahti.fi>
  3. * (C) Copyright 2011, Julius Baxter <julius@opencores.org>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  18. * MA 02111-1307 USA
  19. */
  20. #include <common.h>
  21. #include <asm/types.h>
  22. #include <asm/ptrace.h>
  23. #include <asm/system.h>
  24. #include <asm/openrisc_exc.h>
  25. struct irq_action {
  26. interrupt_handler_t *handler;
  27. void *arg;
  28. int count;
  29. };
  30. static struct irq_action handlers[32];
  31. void interrupt_handler(void)
  32. {
  33. int irq;
  34. while ((irq = ffs(mfspr(SPR_PICSR)))) {
  35. if (handlers[--irq].handler) {
  36. handlers[irq].handler(handlers[irq].arg);
  37. handlers[irq].count++;
  38. } else {
  39. /* disable the interrupt */
  40. mtspr(SPR_PICMR, mfspr(SPR_PICMR) & ~(1 << irq));
  41. printf("Unhandled interrupt: %d\n", irq);
  42. }
  43. /* clear the interrupt */
  44. mtspr(SPR_PICSR, mfspr(SPR_PICSR) & ~(1 << irq));
  45. }
  46. }
  47. int interrupt_init(void)
  48. {
  49. /* install handler for external interrupt exception */
  50. exception_install_handler(EXC_EXT_IRQ, interrupt_handler);
  51. /* Enable interrupts in supervisor register */
  52. mtspr(SPR_SR, mfspr(SPR_SR) | SPR_SR_IEE);
  53. return 0;
  54. }
  55. void enable_interrupts(void)
  56. {
  57. /* Set interrupt enable bit in supervisor register */
  58. mtspr(SPR_SR, mfspr(SPR_SR) | SPR_SR_IEE);
  59. /* Enable timer exception */
  60. mtspr(SPR_SR, mfspr(SPR_SR) | SPR_SR_TEE);
  61. }
  62. int disable_interrupts(void)
  63. {
  64. /* Clear interrupt enable bit in supervisor register */
  65. mtspr(SPR_SR, mfspr(SPR_SR) & ~SPR_SR_IEE);
  66. /* Disable timer exception */
  67. mtspr(SPR_SR, mfspr(SPR_SR) & ~SPR_SR_TEE);
  68. return 0;
  69. }
  70. void irq_install_handler(int irq, interrupt_handler_t *handler, void *arg)
  71. {
  72. if (irq < 0 || irq > 31)
  73. return;
  74. handlers[irq].handler = handler;
  75. handlers[irq].arg = arg;
  76. }
  77. void irq_free_handler(int irq)
  78. {
  79. if (irq < 0 || irq > 31)
  80. return;
  81. handlers[irq].handler = 0;
  82. handlers[irq].arg = 0;
  83. }
  84. #if defined(CONFIG_CMD_IRQ)
  85. int do_irqinfo(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  86. {
  87. int i;
  88. printf("\nInterrupt-Information:\n\n");
  89. printf("Nr Routine Arg Count\n");
  90. printf("-----------------------------\n");
  91. for (i = 0; i < 32; i++) {
  92. if (handlers[i].handler) {
  93. printf("%02d %08lx %08lx %d\n",
  94. i,
  95. (ulong)handlers[i].handler,
  96. (ulong)handlers[i].arg,
  97. handlers[i].count);
  98. }
  99. }
  100. printf("\n");
  101. return 0;
  102. }
  103. #endif