i8259.c 2.6 KB

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