i8259.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:5k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /*
  2.  * c 2001 PPC64 Team, IBM Corp
  3.  *
  4.  * This program is free software; you can redistribute it and/or
  5.  * modify it under the terms of the GNU General Public License
  6.  * as published by the Free Software Foundation; either version
  7.  * 2 of the License, or (at your option) any later version.
  8.  */
  9. #include <linux/stddef.h>
  10. #include <linux/init.h>
  11. #include <linux/sched.h>
  12. #include <linux/signal.h>
  13. #include <linux/cache.h>
  14. #include <asm/io.h>
  15. #include "i8259.h"
  16. #include <linux/irq.h>
  17. #include <asm/ppcdebug.h>
  18. unsigned char cached_8259[2] = { 0xff, 0xff };
  19. #define cached_A1 (cached_8259[0])
  20. #define cached_21 (cached_8259[1])
  21. static spinlock_t i8259_lock __cacheline_aligned_in_smp = SPIN_LOCK_UNLOCKED;
  22. int i8259_pic_irq_offset;
  23. int i8259_irq(int cpu)
  24. {
  25. int irq;
  26. spin_lock/*_irqsave*/(&i8259_lock/*, flags*/);
  27.         /*
  28.          * Perform an interrupt acknowledge cycle on controller 1
  29.          */                                                             
  30.         outb(0x0C, 0x20);
  31.         irq = inb(0x20) & 7;                                   
  32.         if (irq == 2)                                                     
  33.         {                                                                   
  34.                 /*                                     
  35.                  * Interrupt is cascaded so perform interrupt
  36.                  * acknowledge on controller 2
  37.                  */
  38.                 outb(0x0C, 0xA0);                      
  39.                 irq = (inb(0xA0) & 7) + 8;
  40.         }
  41.         else if (irq==7)                                
  42.         {
  43.                 /*                               
  44.                  * This may be a spurious interrupt
  45.                  *                         
  46.                  * Read the interrupt status register. If the most
  47.                  * significant bit is not set then there is no valid
  48.  * interrupt
  49.  */
  50. outb(0x0b, 0x20);
  51. if(~inb(0x20)&0x80) {
  52. spin_unlock/*_irqrestore*/(&i8259_lock/*, flags*/);
  53. return -1;
  54. }
  55. }
  56. spin_unlock/*_irqrestore*/(&i8259_lock/*, flags*/);
  57. return irq;
  58. }
  59. static void i8259_mask_and_ack_irq(unsigned int irq_nr)
  60. {
  61. unsigned long flags;
  62. spin_lock_irqsave(&i8259_lock, flags);
  63.         if ( irq_nr >= i8259_pic_irq_offset )
  64.                 irq_nr -= i8259_pic_irq_offset;
  65.         if (irq_nr > 7) {                                                   
  66.                 cached_A1 |= 1 << (irq_nr-8);                                   
  67.                 inb(0xA1);      /* DUMMY */                                     
  68.                 outb(cached_A1,0xA1);                                           
  69.                 outb(0x20,0xA0);        /* Non-specific EOI */             
  70.                 outb(0x20,0x20);        /* Non-specific EOI to cascade */
  71.         } else {                                                            
  72.                 cached_21 |= 1 << irq_nr;                                   
  73.                 inb(0x21);      /* DUMMY */                                 
  74.                 outb(cached_21,0x21);
  75.                 outb(0x20,0x20);        /* Non-specific EOI */                 
  76.         }                                                                
  77. spin_unlock_irqrestore(&i8259_lock, flags);
  78. }
  79. static void i8259_set_irq_mask(int irq_nr)
  80. {
  81.         outb(cached_A1,0xA1);
  82.         outb(cached_21,0x21);
  83. }
  84. static void i8259_mask_irq(unsigned int irq_nr)
  85. {
  86. unsigned long flags;
  87. spin_lock_irqsave(&i8259_lock, flags);
  88.         if ( irq_nr >= i8259_pic_irq_offset )
  89.                 irq_nr -= i8259_pic_irq_offset;
  90.         if ( irq_nr < 8 )
  91.                 cached_21 |= 1 << irq_nr;
  92.         else
  93.                 cached_A1 |= 1 << (irq_nr-8);
  94.         i8259_set_irq_mask(irq_nr);
  95. spin_unlock_irqrestore(&i8259_lock, flags);
  96. }
  97. static void i8259_unmask_irq(unsigned int irq_nr)
  98. {
  99. unsigned long flags;
  100. spin_lock_irqsave(&i8259_lock, flags);
  101.         if ( irq_nr >= i8259_pic_irq_offset )
  102.                 irq_nr -= i8259_pic_irq_offset;
  103.         if ( irq_nr < 8 )
  104.                 cached_21 &= ~(1 << irq_nr);
  105.         else
  106.                 cached_A1 &= ~(1 << (irq_nr-8));
  107.         i8259_set_irq_mask(irq_nr);
  108. spin_unlock_irqrestore(&i8259_lock, flags);
  109. }
  110. static void i8259_end_irq(unsigned int irq)
  111. {
  112. if (!(irq_desc[irq].status & (IRQ_DISABLED|IRQ_INPROGRESS)))
  113. i8259_unmask_irq(irq);
  114. }
  115. struct hw_interrupt_type i8259_pic = {
  116.         " i8259    ",
  117.         NULL,
  118.         NULL,
  119.         i8259_unmask_irq,
  120.         i8259_mask_irq,
  121.         i8259_mask_and_ack_irq,
  122.         i8259_end_irq,
  123.         NULL
  124. };
  125. void __init i8259_init(void)
  126. {
  127. unsigned long flags;
  128. spin_lock_irqsave(&i8259_lock, flags);
  129.         /* init master interrupt controller */
  130.         outb(0x11, 0x20); /* Start init sequence */
  131.         outb(0x00, 0x21); /* Vector base */
  132.         outb(0x04, 0x21); /* edge tiggered, Cascade (slave) on IRQ2 */
  133.         outb(0x01, 0x21); /* Select 8086 mode */
  134.         outb(0xFF, 0x21); /* Mask all */
  135.         /* init slave interrupt controller */
  136.         outb(0x11, 0xA0); /* Start init sequence */
  137.         outb(0x08, 0xA1); /* Vector base */
  138.         outb(0x02, 0xA1); /* edge triggered, Cascade (slave) on IRQ2 */
  139.         outb(0x01, 0xA1); /* Select 8086 mode */
  140.         outb(0xFF, 0xA1); /* Mask all */
  141.         outb(cached_A1, 0xA1);
  142.         outb(cached_21, 0x21);
  143. spin_unlock_irqrestore(&i8259_lock, flags);
  144.         request_irq( i8259_pic_irq_offset + 2, no_action, SA_INTERRUPT,
  145.                      "82c59 secondary cascade", NULL );
  146.         
  147. }