i8259.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /*
  2. * This file is subject to the terms and conditions of the GNU General Public
  3. * License. See the file "COPYING" in the main directory of this archive
  4. * for more details.
  5. *
  6. * Code to handle x86 style IRQs plus some generic interrupt stuff.
  7. *
  8. * Copyright (C) 1992 Linus Torvalds
  9. * Copyright (C) 1994 - 2000 Ralf Baechle
  10. */
  11. #include <linux/delay.h>
  12. #include <linux/init.h>
  13. #include <linux/ioport.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/kernel.h>
  16. #include <linux/spinlock.h>
  17. #include <linux/sysdev.h>
  18. #include <asm/i8259.h>
  19. #include <asm/io.h>
  20. /*
  21. * This is the 'legacy' 8259A Programmable Interrupt Controller,
  22. * present in the majority of PC/AT boxes.
  23. * plus some generic x86 specific things if generic specifics makes
  24. * any sense at all.
  25. * this file should become arch/i386/kernel/irq.c when the old irq.c
  26. * moves to arch independent land
  27. */
  28. static int i8259A_auto_eoi;
  29. DEFINE_SPINLOCK(i8259A_lock);
  30. /* some platforms call this... */
  31. void mask_and_ack_8259A(unsigned int);
  32. static struct irq_chip i8259A_chip = {
  33. .name = "XT-PIC",
  34. .mask = disable_8259A_irq,
  35. .unmask = enable_8259A_irq,
  36. .mask_ack = mask_and_ack_8259A,
  37. };
  38. /*
  39. * 8259A PIC functions to handle ISA devices:
  40. */
  41. /*
  42. * This contains the irq mask for both 8259A irq controllers,
  43. */
  44. static unsigned int cached_irq_mask = 0xffff;
  45. #define cached_master_mask (cached_irq_mask)
  46. #define cached_slave_mask (cached_irq_mask >> 8)
  47. void disable_8259A_irq(unsigned int irq)
  48. {
  49. unsigned int mask = 1 << irq;
  50. unsigned long flags;
  51. spin_lock_irqsave(&i8259A_lock, flags);
  52. cached_irq_mask |= mask;
  53. if (irq & 8)
  54. outb(cached_slave_mask, PIC_SLAVE_IMR);
  55. else
  56. outb(cached_master_mask, PIC_MASTER_IMR);
  57. spin_unlock_irqrestore(&i8259A_lock, flags);
  58. }
  59. void enable_8259A_irq(unsigned int irq)
  60. {
  61. unsigned int mask = ~(1 << irq);
  62. unsigned long flags;
  63. spin_lock_irqsave(&i8259A_lock, flags);
  64. cached_irq_mask &= mask;
  65. if (irq & 8)
  66. outb(cached_slave_mask, PIC_SLAVE_IMR);
  67. else
  68. outb(cached_master_mask, PIC_MASTER_IMR);
  69. spin_unlock_irqrestore(&i8259A_lock, flags);
  70. }
  71. int i8259A_irq_pending(unsigned int irq)
  72. {
  73. unsigned int mask = 1 << irq;
  74. unsigned long flags;
  75. int ret;
  76. spin_lock_irqsave(&i8259A_lock, flags);
  77. if (irq < 8)
  78. ret = inb(PIC_MASTER_CMD) & mask;
  79. else
  80. ret = inb(PIC_SLAVE_CMD) & (mask >> 8);
  81. spin_unlock_irqrestore(&i8259A_lock, flags);
  82. return ret;
  83. }
  84. void make_8259A_irq(unsigned int irq)
  85. {
  86. disable_irq_nosync(irq);
  87. set_irq_chip_and_handler(irq, &i8259A_chip, handle_level_irq);
  88. enable_irq(irq);
  89. }
  90. /*
  91. * This function assumes to be called rarely. Switching between
  92. * 8259A registers is slow.
  93. * This has to be protected by the irq controller spinlock
  94. * before being called.
  95. */
  96. static inline int i8259A_irq_real(unsigned int irq)
  97. {
  98. int value;
  99. int irqmask = 1 << irq;
  100. if (irq < 8) {
  101. outb(0x0B,PIC_MASTER_CMD); /* ISR register */
  102. value = inb(PIC_MASTER_CMD) & irqmask;
  103. outb(0x0A,PIC_MASTER_CMD); /* back to the IRR register */
  104. return value;
  105. }
  106. outb(0x0B,PIC_SLAVE_CMD); /* ISR register */
  107. value = inb(PIC_SLAVE_CMD) & (irqmask >> 8);
  108. outb(0x0A,PIC_SLAVE_CMD); /* back to the IRR register */
  109. return value;
  110. }
  111. /*
  112. * Careful! The 8259A is a fragile beast, it pretty
  113. * much _has_ to be done exactly like this (mask it
  114. * first, _then_ send the EOI, and the order of EOI
  115. * to the two 8259s is important!
  116. */
  117. void mask_and_ack_8259A(unsigned int irq)
  118. {
  119. unsigned int irqmask = 1 << irq;
  120. unsigned long flags;
  121. spin_lock_irqsave(&i8259A_lock, flags);
  122. /*
  123. * Lightweight spurious IRQ detection. We do not want
  124. * to overdo spurious IRQ handling - it's usually a sign
  125. * of hardware problems, so we only do the checks we can
  126. * do without slowing down good hardware unnecessarily.
  127. *
  128. * Note that IRQ7 and IRQ15 (the two spurious IRQs
  129. * usually resulting from the 8259A-1|2 PICs) occur
  130. * even if the IRQ is masked in the 8259A. Thus we
  131. * can check spurious 8259A IRQs without doing the
  132. * quite slow i8259A_irq_real() call for every IRQ.
  133. * This does not cover 100% of spurious interrupts,
  134. * but should be enough to warn the user that there
  135. * is something bad going on ...
  136. */
  137. if (cached_irq_mask & irqmask)
  138. goto spurious_8259A_irq;
  139. cached_irq_mask |= irqmask;
  140. handle_real_irq:
  141. if (irq & 8) {
  142. inb(PIC_SLAVE_IMR); /* DUMMY - (do we need this?) */
  143. outb(cached_slave_mask, PIC_SLAVE_IMR);
  144. outb(0x60+(irq&7),PIC_SLAVE_CMD);/* 'Specific EOI' to slave */
  145. outb(0x60+PIC_CASCADE_IR,PIC_MASTER_CMD); /* 'Specific EOI' to master-IRQ2 */
  146. } else {
  147. inb(PIC_MASTER_IMR); /* DUMMY - (do we need this?) */
  148. outb(cached_master_mask, PIC_MASTER_IMR);
  149. outb(0x60+irq,PIC_MASTER_CMD); /* 'Specific EOI to master */
  150. }
  151. #ifdef CONFIG_MIPS_MT_SMTC
  152. if (irq_hwmask[irq] & ST0_IM)
  153. set_c0_status(irq_hwmask[irq] & ST0_IM);
  154. #endif /* CONFIG_MIPS_MT_SMTC */
  155. spin_unlock_irqrestore(&i8259A_lock, flags);
  156. return;
  157. spurious_8259A_irq:
  158. /*
  159. * this is the slow path - should happen rarely.
  160. */
  161. if (i8259A_irq_real(irq))
  162. /*
  163. * oops, the IRQ _is_ in service according to the
  164. * 8259A - not spurious, go handle it.
  165. */
  166. goto handle_real_irq;
  167. {
  168. static int spurious_irq_mask;
  169. /*
  170. * At this point we can be sure the IRQ is spurious,
  171. * lets ACK and report it. [once per IRQ]
  172. */
  173. if (!(spurious_irq_mask & irqmask)) {
  174. printk(KERN_DEBUG "spurious 8259A interrupt: IRQ%d.\n", irq);
  175. spurious_irq_mask |= irqmask;
  176. }
  177. atomic_inc(&irq_err_count);
  178. /*
  179. * Theoretically we do not have to handle this IRQ,
  180. * but in Linux this does not cause problems and is
  181. * simpler for us.
  182. */
  183. goto handle_real_irq;
  184. }
  185. }
  186. static int i8259A_resume(struct sys_device *dev)
  187. {
  188. init_8259A(i8259A_auto_eoi);
  189. return 0;
  190. }
  191. static int i8259A_shutdown(struct sys_device *dev)
  192. {
  193. /* Put the i8259A into a quiescent state that
  194. * the kernel initialization code can get it
  195. * out of.
  196. */
  197. outb(0xff, PIC_MASTER_IMR); /* mask all of 8259A-1 */
  198. outb(0xff, PIC_SLAVE_IMR); /* mask all of 8259A-1 */
  199. return 0;
  200. }
  201. static struct sysdev_class i8259_sysdev_class = {
  202. set_kset_name("i8259"),
  203. .resume = i8259A_resume,
  204. .shutdown = i8259A_shutdown,
  205. };
  206. static struct sys_device device_i8259A = {
  207. .id = 0,
  208. .cls = &i8259_sysdev_class,
  209. };
  210. static int __init i8259A_init_sysfs(void)
  211. {
  212. int error = sysdev_class_register(&i8259_sysdev_class);
  213. if (!error)
  214. error = sysdev_register(&device_i8259A);
  215. return error;
  216. }
  217. device_initcall(i8259A_init_sysfs);
  218. void __init init_8259A(int auto_eoi)
  219. {
  220. unsigned long flags;
  221. i8259A_auto_eoi = auto_eoi;
  222. spin_lock_irqsave(&i8259A_lock, flags);
  223. outb(0xff, PIC_MASTER_IMR); /* mask all of 8259A-1 */
  224. outb(0xff, PIC_SLAVE_IMR); /* mask all of 8259A-2 */
  225. /*
  226. * outb_p - this has to work on a wide range of PC hardware.
  227. */
  228. outb_p(0x11, PIC_MASTER_CMD); /* ICW1: select 8259A-1 init */
  229. outb_p(I8259A_IRQ_BASE + 0, PIC_MASTER_IMR); /* ICW2: 8259A-1 IR0 mapped to I8259A_IRQ_BASE + 0x00 */
  230. outb_p(1U << PIC_CASCADE_IR, PIC_MASTER_IMR); /* 8259A-1 (the master) has a slave on IR2 */
  231. if (auto_eoi) /* master does Auto EOI */
  232. outb_p(MASTER_ICW4_DEFAULT | PIC_ICW4_AEOI, PIC_MASTER_IMR);
  233. else /* master expects normal EOI */
  234. outb_p(MASTER_ICW4_DEFAULT, PIC_MASTER_IMR);
  235. outb_p(0x11, PIC_SLAVE_CMD); /* ICW1: select 8259A-2 init */
  236. outb_p(I8259A_IRQ_BASE + 8, PIC_SLAVE_IMR); /* ICW2: 8259A-2 IR0 mapped to I8259A_IRQ_BASE + 0x08 */
  237. outb_p(PIC_CASCADE_IR, PIC_SLAVE_IMR); /* 8259A-2 is a slave on master's IR2 */
  238. outb_p(SLAVE_ICW4_DEFAULT, PIC_SLAVE_IMR); /* (slave's support for AEOI in flat mode is to be investigated) */
  239. if (auto_eoi)
  240. /*
  241. * In AEOI mode we just have to mask the interrupt
  242. * when acking.
  243. */
  244. i8259A_chip.mask_ack = disable_8259A_irq;
  245. else
  246. i8259A_chip.mask_ack = mask_and_ack_8259A;
  247. udelay(100); /* wait for 8259A to initialize */
  248. outb(cached_master_mask, PIC_MASTER_IMR); /* restore master IRQ mask */
  249. outb(cached_slave_mask, PIC_SLAVE_IMR); /* restore slave IRQ mask */
  250. spin_unlock_irqrestore(&i8259A_lock, flags);
  251. }
  252. /*
  253. * IRQ2 is cascade interrupt to second interrupt controller
  254. */
  255. static struct irqaction irq2 = {
  256. no_action, 0, CPU_MASK_NONE, "cascade", NULL, NULL
  257. };
  258. static struct resource pic1_io_resource = {
  259. .name = "pic1",
  260. .start = PIC_MASTER_CMD,
  261. .end = PIC_MASTER_IMR,
  262. .flags = IORESOURCE_BUSY
  263. };
  264. static struct resource pic2_io_resource = {
  265. .name = "pic2",
  266. .start = PIC_SLAVE_CMD,
  267. .end = PIC_SLAVE_IMR,
  268. .flags = IORESOURCE_BUSY
  269. };
  270. /*
  271. * On systems with i8259-style interrupt controllers we assume for
  272. * driver compatibility reasons interrupts 0 - 15 to be the i8259
  273. * interrupts even if the hardware uses a different interrupt numbering.
  274. */
  275. void __init init_i8259_irqs (void)
  276. {
  277. int i;
  278. request_resource(&ioport_resource, &pic1_io_resource);
  279. request_resource(&ioport_resource, &pic2_io_resource);
  280. init_8259A(0);
  281. for (i = 0; i < 16; i++)
  282. set_irq_chip_and_handler(i, &i8259A_chip, handle_level_irq);
  283. setup_irq(PIC_CASCADE_IR, &irq2);
  284. }