pcat_interrupts.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 i8259_init(void)
  24. {
  25. u8 i;
  26. /* Mask all interrupts */
  27. outb(0xff, MASTER_PIC + IMR);
  28. outb(0xff, SLAVE_PIC + IMR);
  29. /* Master PIC */
  30. /* Place master PIC interrupts at INT20 */
  31. /* ICW3, One slave PIC is present */
  32. outb(ICW1_SEL|ICW1_EICW4, MASTER_PIC + ICW1);
  33. outb(0x20, MASTER_PIC + ICW2);
  34. outb(IR2, MASTER_PIC + ICW3);
  35. outb(ICW4_PM, MASTER_PIC + ICW4);
  36. for (i = 0; i < 8; i++)
  37. outb(OCW2_SEOI | i, MASTER_PIC + OCW2);
  38. /* Slave PIC */
  39. /* Place slave PIC interrupts at INT28 */
  40. /* Slave ID */
  41. outb(ICW1_SEL|ICW1_EICW4, SLAVE_PIC + ICW1);
  42. outb(0x28, SLAVE_PIC + ICW2);
  43. outb(0x02, SLAVE_PIC + ICW3);
  44. outb(ICW4_PM, SLAVE_PIC + ICW4);
  45. for (i = 0; i < 8; i++)
  46. outb(OCW2_SEOI | i, SLAVE_PIC + OCW2);
  47. /*
  48. * Enable cascaded interrupts by unmasking the cascade IRQ pin of
  49. * the master PIC
  50. */
  51. unmask_irq(2);
  52. /* Interrupt 9 should be level triggered (SCI). The OS might do this */
  53. configure_irq_trigger(9, true);
  54. return 0;
  55. }
  56. void mask_irq(int irq)
  57. {
  58. int imr_port;
  59. if (irq >= CONFIG_SYS_NUM_IRQS)
  60. return;
  61. if (irq > 7)
  62. imr_port = SLAVE_PIC + IMR;
  63. else
  64. imr_port = MASTER_PIC + IMR;
  65. outb(inb(imr_port) | (1 << (irq & 7)), imr_port);
  66. }
  67. void unmask_irq(int irq)
  68. {
  69. int imr_port;
  70. if (irq >= CONFIG_SYS_NUM_IRQS)
  71. return;
  72. if (irq > 7)
  73. imr_port = SLAVE_PIC + IMR;
  74. else
  75. imr_port = MASTER_PIC + IMR;
  76. outb(inb(imr_port) & ~(1 << (irq & 7)), imr_port);
  77. }
  78. void specific_eoi(int irq)
  79. {
  80. if (irq >= CONFIG_SYS_NUM_IRQS)
  81. return;
  82. if (irq > 7) {
  83. /*
  84. * IRQ is on the slave - Issue a corresponding EOI to the
  85. * slave PIC and an EOI for IRQ2 (the cascade interrupt)
  86. * on the master PIC
  87. */
  88. outb(OCW2_SEOI | (irq & 7), SLAVE_PIC + OCW2);
  89. irq = SEOI_IR2;
  90. }
  91. outb(OCW2_SEOI | irq, MASTER_PIC + OCW2);
  92. }
  93. #define ELCR1 0x4d0
  94. #define ELCR2 0x4d1
  95. void configure_irq_trigger(int int_num, bool is_level_triggered)
  96. {
  97. u16 int_bits = inb(ELCR1) | (((u16)inb(ELCR2)) << 8);
  98. debug("%s: current interrupts are 0x%x\n", __func__, int_bits);
  99. if (is_level_triggered)
  100. int_bits |= (1 << int_num);
  101. else
  102. int_bits &= ~(1 << int_num);
  103. /* Write new values */
  104. debug("%s: try to set interrupts 0x%x\n", __func__, int_bits);
  105. outb((u8)(int_bits & 0xff), ELCR1);
  106. outb((u8)(int_bits >> 8), ELCR2);
  107. #ifdef PARANOID_IRQ_TRIGGERS
  108. /*
  109. * Try reading back the new values. This seems like an error but is
  110. * not
  111. */
  112. if (inb(ELCR1) != (int_bits & 0xff)) {
  113. printf("%s: lower order bits are wrong: want 0x%x, got 0x%x\n",
  114. __func__, (int_bits & 0xff), inb(ELCR1));
  115. }
  116. if (inb(ELCR2) != (int_bits >> 8)) {
  117. printf("%s: higher order bits are wrong: want 0x%x, got 0x%x\n",
  118. __func__, (int_bits>>8), inb(ELCR2));
  119. }
  120. #endif
  121. }