pcat_interrupts.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. * (C) Copyright 2009
  3. * Graeme Russ, <graeme.russ@gmail.com>
  4. *
  5. * (C) Copyright 2002
  6. * Daniel Engström, Omicron Ceti AB, <daniel@omicron.se>
  7. *
  8. * SPDX-License-Identifier: GPL-2.0+
  9. */
  10. /*
  11. * This file provides the interrupt handling functionality for systems
  12. * based on the standard PC/AT architecture using two cascaded i8259
  13. * Programmable Interrupt Controllers.
  14. */
  15. #include <common.h>
  16. #include <asm/io.h>
  17. #include <asm/i8259.h>
  18. #include <asm/ibmpc.h>
  19. #include <asm/interrupt.h>
  20. #if CONFIG_SYS_NUM_IRQS != 16
  21. #error "CONFIG_SYS_NUM_IRQS must equal 16 if CONFIG_SYS_NUM_IRQS is defined"
  22. #endif
  23. int interrupt_init(void)
  24. {
  25. u8 i;
  26. disable_interrupts();
  27. /* Mask all interrupts */
  28. outb(0xff, MASTER_PIC + IMR);
  29. outb(0xff, SLAVE_PIC + IMR);
  30. /* Master PIC */
  31. /* Place master PIC interrupts at INT20 */
  32. /* ICW3, One slave PIC is present */
  33. outb(ICW1_SEL|ICW1_EICW4, MASTER_PIC + ICW1);
  34. outb(0x20, MASTER_PIC + ICW2);
  35. outb(IR2, MASTER_PIC + ICW3);
  36. outb(ICW4_PM, MASTER_PIC + ICW4);
  37. for (i = 0; i < 8; i++)
  38. outb(OCW2_SEOI | i, MASTER_PIC + OCW2);
  39. /* Slave PIC */
  40. /* Place slave PIC interrupts at INT28 */
  41. /* Slave ID */
  42. outb(ICW1_SEL|ICW1_EICW4, SLAVE_PIC + ICW1);
  43. outb(0x28, SLAVE_PIC + ICW2);
  44. outb(0x02, SLAVE_PIC + ICW3);
  45. outb(ICW4_PM, SLAVE_PIC + ICW4);
  46. for (i = 0; i < 8; i++)
  47. outb(OCW2_SEOI | i, SLAVE_PIC + OCW2);
  48. /*
  49. * Enable cascaded interrupts by unmasking the cascade IRQ pin of
  50. * the master PIC
  51. */
  52. unmask_irq(2);
  53. /* Interrupt 9 should be level triggered (SCI). The OS might do this */
  54. configure_irq_trigger(9, true);
  55. enable_interrupts();
  56. return 0;
  57. }
  58. void mask_irq(int irq)
  59. {
  60. int imr_port;
  61. if (irq >= CONFIG_SYS_NUM_IRQS)
  62. return;
  63. if (irq > 7)
  64. imr_port = SLAVE_PIC + IMR;
  65. else
  66. imr_port = MASTER_PIC + IMR;
  67. outb(inb(imr_port) | (1 << (irq & 7)), imr_port);
  68. }
  69. void unmask_irq(int irq)
  70. {
  71. int imr_port;
  72. if (irq >= CONFIG_SYS_NUM_IRQS)
  73. return;
  74. if (irq > 7)
  75. imr_port = SLAVE_PIC + IMR;
  76. else
  77. imr_port = MASTER_PIC + IMR;
  78. outb(inb(imr_port) & ~(1 << (irq & 7)), imr_port);
  79. }
  80. void specific_eoi(int irq)
  81. {
  82. if (irq >= CONFIG_SYS_NUM_IRQS)
  83. return;
  84. if (irq > 7) {
  85. /*
  86. * IRQ is on the slave - Issue a corresponding EOI to the
  87. * slave PIC and an EOI for IRQ2 (the cascade interrupt)
  88. * on the master PIC
  89. */
  90. outb(OCW2_SEOI | (irq & 7), SLAVE_PIC + OCW2);
  91. irq = SEOI_IR2;
  92. }
  93. outb(OCW2_SEOI | irq, MASTER_PIC + OCW2);
  94. }
  95. #define ELCR1 0x4d0
  96. #define ELCR2 0x4d1
  97. void configure_irq_trigger(int int_num, bool is_level_triggered)
  98. {
  99. u16 int_bits = inb(ELCR1) | (((u16)inb(ELCR2)) << 8);
  100. debug("%s: current interrupts are 0x%x\n", __func__, int_bits);
  101. if (is_level_triggered)
  102. int_bits |= (1 << int_num);
  103. else
  104. int_bits &= ~(1 << int_num);
  105. /* Write new values */
  106. debug("%s: try to set interrupts 0x%x\n", __func__, int_bits);
  107. outb((u8)(int_bits & 0xff), ELCR1);
  108. outb((u8)(int_bits >> 8), ELCR2);
  109. #ifdef PARANOID_IRQ_TRIGGERS
  110. /*
  111. * Try reading back the new values. This seems like an error but is
  112. * not
  113. */
  114. if (inb(ELCR1) != (int_bits & 0xff)) {
  115. printf("%s: lower order bits are wrong: want 0x%x, got 0x%x\n",
  116. __func__, (int_bits & 0xff), inb(ELCR1));
  117. }
  118. if (inb(ELCR2) != (int_bits >> 8)) {
  119. printf("%s: higher order bits are wrong: want 0x%x, got 0x%x\n",
  120. __func__, (int_bits>>8), inb(ELCR2));
  121. }
  122. #endif
  123. }