i8259.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * i8259 interrupt controller driver.
  4. */
  5. #undef DEBUG
  6. #include <linux/ioport.h>
  7. #include <linux/interrupt.h>
  8. #include <linux/irqdomain.h>
  9. #include <linux/kernel.h>
  10. #include <linux/delay.h>
  11. #include <asm/io.h>
  12. #include <asm/i8259.h>
  13. static volatile void __iomem *pci_intack; /* RO, gives us the irq vector */
  14. static unsigned char cached_8259[2] = { 0xff, 0xff };
  15. #define cached_A1 (cached_8259[0])
  16. #define cached_21 (cached_8259[1])
  17. static DEFINE_RAW_SPINLOCK(i8259_lock);
  18. static struct irq_domain *i8259_host;
  19. /*
  20. * Acknowledge the IRQ using either the PCI host bridge's interrupt
  21. * acknowledge feature or poll. How i8259_init() is called determines
  22. * which is called. It should be noted that polling is broken on some
  23. * IBM and Motorola PReP boxes so we must use the int-ack feature on them.
  24. */
  25. unsigned int i8259_irq(void)
  26. {
  27. int irq;
  28. int lock = 0;
  29. /* Either int-ack or poll for the IRQ */
  30. if (pci_intack)
  31. irq = readb(pci_intack);
  32. else {
  33. raw_spin_lock(&i8259_lock);
  34. lock = 1;
  35. /* Perform an interrupt acknowledge cycle on controller 1. */
  36. outb(0x0C, 0x20); /* prepare for poll */
  37. irq = inb(0x20) & 7;
  38. if (irq == 2 ) {
  39. /*
  40. * Interrupt is cascaded so perform interrupt
  41. * acknowledge on controller 2.
  42. */
  43. outb(0x0C, 0xA0); /* prepare for poll */
  44. irq = (inb(0xA0) & 7) + 8;
  45. }
  46. }
  47. if (irq == 7) {
  48. /*
  49. * This may be a spurious interrupt.
  50. *
  51. * Read the interrupt status register (ISR). If the most
  52. * significant bit is not set then there is no valid
  53. * interrupt.
  54. */
  55. if (!pci_intack)
  56. outb(0x0B, 0x20); /* ISR register */
  57. if(~inb(0x20) & 0x80)
  58. irq = 0;
  59. } else if (irq == 0xff)
  60. irq = 0;
  61. if (lock)
  62. raw_spin_unlock(&i8259_lock);
  63. return irq;
  64. }
  65. static void i8259_mask_and_ack_irq(struct irq_data *d)
  66. {
  67. unsigned long flags;
  68. raw_spin_lock_irqsave(&i8259_lock, flags);
  69. if (d->irq > 7) {
  70. cached_A1 |= 1 << (d->irq-8);
  71. inb(0xA1); /* DUMMY */
  72. outb(cached_A1, 0xA1);
  73. outb(0x20, 0xA0); /* Non-specific EOI */
  74. outb(0x20, 0x20); /* Non-specific EOI to cascade */
  75. } else {
  76. cached_21 |= 1 << d->irq;
  77. inb(0x21); /* DUMMY */
  78. outb(cached_21, 0x21);
  79. outb(0x20, 0x20); /* Non-specific EOI */
  80. }
  81. raw_spin_unlock_irqrestore(&i8259_lock, flags);
  82. }
  83. static void i8259_set_irq_mask(int irq_nr)
  84. {
  85. outb(cached_A1,0xA1);
  86. outb(cached_21,0x21);
  87. }
  88. static void i8259_mask_irq(struct irq_data *d)
  89. {
  90. unsigned long flags;
  91. pr_debug("i8259_mask_irq(%d)\n", d->irq);
  92. raw_spin_lock_irqsave(&i8259_lock, flags);
  93. if (d->irq < 8)
  94. cached_21 |= 1 << d->irq;
  95. else
  96. cached_A1 |= 1 << (d->irq-8);
  97. i8259_set_irq_mask(d->irq);
  98. raw_spin_unlock_irqrestore(&i8259_lock, flags);
  99. }
  100. static void i8259_unmask_irq(struct irq_data *d)
  101. {
  102. unsigned long flags;
  103. pr_debug("i8259_unmask_irq(%d)\n", d->irq);
  104. raw_spin_lock_irqsave(&i8259_lock, flags);
  105. if (d->irq < 8)
  106. cached_21 &= ~(1 << d->irq);
  107. else
  108. cached_A1 &= ~(1 << (d->irq-8));
  109. i8259_set_irq_mask(d->irq);
  110. raw_spin_unlock_irqrestore(&i8259_lock, flags);
  111. }
  112. static struct irq_chip i8259_pic = {
  113. .name = "i8259",
  114. .irq_mask = i8259_mask_irq,
  115. .irq_disable = i8259_mask_irq,
  116. .irq_unmask = i8259_unmask_irq,
  117. .irq_mask_ack = i8259_mask_and_ack_irq,
  118. };
  119. static struct resource pic1_iores = {
  120. .name = "8259 (master)",
  121. .start = 0x20,
  122. .end = 0x21,
  123. .flags = IORESOURCE_IO | IORESOURCE_BUSY,
  124. };
  125. static struct resource pic2_iores = {
  126. .name = "8259 (slave)",
  127. .start = 0xa0,
  128. .end = 0xa1,
  129. .flags = IORESOURCE_IO | IORESOURCE_BUSY,
  130. };
  131. static struct resource pic_edgectrl_iores = {
  132. .name = "8259 edge control",
  133. .start = 0x4d0,
  134. .end = 0x4d1,
  135. .flags = IORESOURCE_IO | IORESOURCE_BUSY,
  136. };
  137. static int i8259_host_match(struct irq_domain *h, struct device_node *node,
  138. enum irq_domain_bus_token bus_token)
  139. {
  140. struct device_node *of_node = irq_domain_get_of_node(h);
  141. return of_node == NULL || of_node == node;
  142. }
  143. static int i8259_host_map(struct irq_domain *h, unsigned int virq,
  144. irq_hw_number_t hw)
  145. {
  146. pr_debug("i8259_host_map(%d, 0x%lx)\n", virq, hw);
  147. /* We block the internal cascade */
  148. if (hw == 2)
  149. irq_set_status_flags(virq, IRQ_NOREQUEST);
  150. /* We use the level handler only for now, we might want to
  151. * be more cautious here but that works for now
  152. */
  153. irq_set_status_flags(virq, IRQ_LEVEL);
  154. irq_set_chip_and_handler(virq, &i8259_pic, handle_level_irq);
  155. return 0;
  156. }
  157. static int i8259_host_xlate(struct irq_domain *h, struct device_node *ct,
  158. const u32 *intspec, unsigned int intsize,
  159. irq_hw_number_t *out_hwirq, unsigned int *out_flags)
  160. {
  161. static unsigned char map_isa_senses[4] = {
  162. IRQ_TYPE_LEVEL_LOW,
  163. IRQ_TYPE_LEVEL_HIGH,
  164. IRQ_TYPE_EDGE_FALLING,
  165. IRQ_TYPE_EDGE_RISING,
  166. };
  167. *out_hwirq = intspec[0];
  168. if (intsize > 1 && intspec[1] < 4)
  169. *out_flags = map_isa_senses[intspec[1]];
  170. else
  171. *out_flags = IRQ_TYPE_NONE;
  172. return 0;
  173. }
  174. static const struct irq_domain_ops i8259_host_ops = {
  175. .match = i8259_host_match,
  176. .map = i8259_host_map,
  177. .xlate = i8259_host_xlate,
  178. };
  179. struct irq_domain *__init i8259_get_host(void)
  180. {
  181. return i8259_host;
  182. }
  183. /**
  184. * i8259_init - Initialize the legacy controller
  185. * @node: device node of the legacy PIC (can be NULL, but then, it will match
  186. * all interrupts, so beware)
  187. * @intack_addr: PCI interrupt acknowledge (real) address which will return
  188. * the active irq from the 8259
  189. */
  190. void i8259_init(struct device_node *node, unsigned long intack_addr)
  191. {
  192. unsigned long flags;
  193. /* initialize the controller */
  194. raw_spin_lock_irqsave(&i8259_lock, flags);
  195. /* Mask all first */
  196. outb(0xff, 0xA1);
  197. outb(0xff, 0x21);
  198. /* init master interrupt controller */
  199. outb(0x11, 0x20); /* Start init sequence */
  200. outb(0x00, 0x21); /* Vector base */
  201. outb(0x04, 0x21); /* edge triggered, Cascade (slave) on IRQ2 */
  202. outb(0x01, 0x21); /* Select 8086 mode */
  203. /* init slave interrupt controller */
  204. outb(0x11, 0xA0); /* Start init sequence */
  205. outb(0x08, 0xA1); /* Vector base */
  206. outb(0x02, 0xA1); /* edge triggered, Cascade (slave) on IRQ2 */
  207. outb(0x01, 0xA1); /* Select 8086 mode */
  208. /* That thing is slow */
  209. udelay(100);
  210. /* always read ISR */
  211. outb(0x0B, 0x20);
  212. outb(0x0B, 0xA0);
  213. /* Unmask the internal cascade */
  214. cached_21 &= ~(1 << 2);
  215. /* Set interrupt masks */
  216. outb(cached_A1, 0xA1);
  217. outb(cached_21, 0x21);
  218. raw_spin_unlock_irqrestore(&i8259_lock, flags);
  219. /* create a legacy host */
  220. i8259_host = irq_domain_add_legacy(node, NR_IRQS_LEGACY, 0, 0,
  221. &i8259_host_ops, NULL);
  222. if (i8259_host == NULL) {
  223. printk(KERN_ERR "i8259: failed to allocate irq host !\n");
  224. return;
  225. }
  226. /* reserve our resources */
  227. /* XXX should we continue doing that ? it seems to cause problems
  228. * with further requesting of PCI IO resources for that range...
  229. * need to look into it.
  230. */
  231. request_resource(&ioport_resource, &pic1_iores);
  232. request_resource(&ioport_resource, &pic2_iores);
  233. request_resource(&ioport_resource, &pic_edgectrl_iores);
  234. if (intack_addr != 0)
  235. pci_intack = ioremap(intack_addr, 1);
  236. printk(KERN_INFO "i8259 legacy interrupt controller initialized\n");
  237. }