Chinaunix首页 | 论坛 | 博客
  • 博客访问: 160386
  • 博文数量: 119
  • 博客积分: 2862
  • 博客等级: 上尉
  • 技术积分: 945
  • 用 户 组: 普通用户
  • 注册时间: 2010-10-20 09:46
文章分类

全部博文(119)

文章存档

2013年(2)

2012年(61)

2011年(22)

2010年(34)

分类:

2012-11-11 18:22:49

底层硬件操作方法

每一条中断线都有一个底层硬件操作函数集struct irq_chip 。大多数控制方法都是重复的 ,基本上只要有中断响应 、 中断屏蔽 、 中断开启 、 中断触发类型设置等方法就可以满足要求了。其他各种方法基本上和这些相同。

这些操作方法的实现在文件linux/arch/arm/plat-s3c24xx/irq.c中。

例如外部中断 IRQ_EINT0 ~ IRQ_EINT3都用以下操作函数集:

static struct irq_chip s3c_irq_eint0t4 = {
 .name  = "s3c-ext0",
 .ack  = s3c_irq_ack,
 .mask  = s3c_irq_mask,
 .unmask  = s3c_irq_unmask,
 .set_wake = s3c_irq_wake,
 .set_type = s3c_irqext_type,
};

/******************** 中断响应******************************/

static inline void
s3c_irq_ack(unsigned int irqno)
{
 unsigned long bitval = 1UL << (irqno - IRQ_EINT0);

 __raw_writel(bitval, S3C2410_SRCPND);
 __raw_writel(bitval, S3C2410_INTPND);
}

/******************** 中断屏蔽******************************/

static void
s3c_irq_mask(unsigned int irqno)
{
 unsigned long mask;

 irqno -= IRQ_EINT0;

 mask = __raw_readl(S3C2410_INTMSK);
 mask |= 1UL << irqno;
 __raw_writel(mask, S3C2410_INTMSK);
}

/******************** 中断开启******************************/

static void
s3c_irq_unmask(unsigned int irqno)
{
 unsigned long mask;

 if (irqno != IRQ_TIMER4 && irqno != IRQ_EINT8t23)
  irqdbf2("s3c_irq_unmask %d\n", irqno);

 irqno -= IRQ_EINT0;

 mask = __raw_readl(S3C2410_INTMSK);
 mask &= ~(1UL << irqno);
 __raw_writel(mask, S3C2410_INTMSK);
}

 /******************** 中断触发类型设置******************************/

 int
s3c_irqext_type(unsigned int irq, unsigned int type)
{
 void __iomem *extint_reg;
 void __iomem *gpcon_reg;
 unsigned long gpcon_offset, extint_offset;
 unsigned long newvalue = 0, value;

 if ((irq >= IRQ_EINT0) && (irq <= IRQ_EINT3))
 {
  gpcon_reg = S3C2410_GPFCON;
  extint_reg = S3C24XX_EXTINT0;
  gpcon_offset = (irq - IRQ_EINT0) * 2;
  extint_offset = (irq - IRQ_EINT0) * 4;
 }
。。。。。。

 __raw_writel(value, gpcon_reg);  //将对应管脚配置成中断功能


 switch (type)
 {
  case IRQ_TYPE_NONE:
   printk(KERN_WARNING "No edge setting!\n");
   break;

  case IRQ_TYPE_EDGE_RISING:
   newvalue = S3C2410_EXTINT_RISEEDGE;
   break;

  case IRQ_TYPE_EDGE_FALLING:
   newvalue = S3C2410_EXTINT_FALLEDGE;
   break;

  case IRQ_TYPE_EDGE_BOTH:
   newvalue = S3C2410_EXTINT_BOTHEDGE;
   break;

  case IRQ_TYPE_LEVEL_LOW:
   newvalue = S3C2410_EXTINT_LOWLEV;
   break;

  case IRQ_TYPE_LEVEL_HIGH:
   newvalue = S3C2410_EXTINT_HILEV;
   break;

  default:
   printk(KERN_ERR "No such irq type %d", type);
   return -1;
 }

 value = __raw_readl(extint_reg);  
 value = (value & ~(7 << extint_offset)) | (newvalue << extint_offset);
 __raw_writel(value, extint_reg); //设置中断的触发方式。

 return 0;
}

中断申请函数

//中断申请函数request_irq()只是函数request_threaded_irq()的包装而已

点击(此处)折叠或打开

  1. request_irq(unsigned int irq, irq_handler_t handler, unsigned long flags,
  2.      const char *name, void *dev)
  3. {
  4.  return request_threaded_irq(irq, handler, NULL, flags, name, dev);
  5. }

  6.  

  7. int request_threaded_irq(unsigned int irq, irq_handler_t handler,
  8.     irq_handler_t thread_fn, unsigned long irqflags,
  9.     const char *devname, void *dev_id)
  10. {
  11.  struct irqaction *action;
  12.  struct irq_desc *desc;
  13.  int retval;

  14. //中断类型标识IRQF_SHARED和IRQF_DISABLED不应当被同时设置。
  15.  if ((irqflags & (IRQF_SHARED|IRQF_DISABLED)) ==
  16.      (IRQF_SHARED|IRQF_DISABLED)) {
  17.   pr_warning(
  18.     "IRQ %d/%s: IRQF_DISABLED is not guaranteed on shared IRQs\n",
  19.    irq, devname);
  20.  }

  21. #ifdef CONFIG_LOCKDEP
  22.  /*
  23.   * Lockdep wants atomic interrupt handlers:
  24.   */
  25.  irqflags |= IRQF_DISABLED;
  26. #endif
  27.  /*
  28.   * Sanity-check: shared interrupts must pass in a real dev-ID,
  29.   * otherwise we'll have trouble later trying to figure out
  30.   * which interrupt is which (messes up the interrupt freeing
  31.   * logic etc).
  32.   */
  33.  if ((irqflags & IRQF_SHARED) && !dev_id)
  34.   return -EINVAL;

  35.  desc = irq_to_desc(irq); //根据中断号获取中断线描述符结构体
  36.  if (!desc)
  37.   return -EINVAL;

  38.  if (desc->status & IRQ_NOREQUEST)
  39.   return -EINVAL;
  40.  if (!handler)
  41.   return -EINVAL;

  42. //分配一个中断服务例程结构体action并初始化它的各字段。

  43.  action = kzalloc(sizeof(struct irqaction), GFP_KERNEL);
  44.  if (!action)
  45.   return -ENOMEM;

  46.  action->handler = handler;
  47.  action->thread_fn = thread_fn; //为NULL
  48.  action->flags = irqflags;
  49.  action->name = devname;

  50.  //对于共享中断 , 此特定值用来区分各中断,以便从共享中断线的诸多中断处理程序中删除指定的那一个。
  51.  action->dev_id = dev_id;

  52. //将该例程添加到单向链表desc->action上,并启动该例程。

  53.  retval = __setup_irq(irq, desc, action);
  54.  if (retval)
  55.   kfree(action);

  56. 。。。。。。
  57.  return retval;
  58. }

  59.  

  60. static int
  61. __setup_irq(unsigned int irq, struct irq_desc *desc, struct irqaction *new)
  62. {
  63.  struct irqaction *old, **old_ptr;
  64.  const char *old_name = NULL;
  65.  unsigned long flags;
  66.  int shared = 0;
  67.  int ret;

  68. 。。。。。。


  69.  old_ptr = &desc->action; //获取中断处理例程单向链表上的第一个例程,如果它为NULL说明该中断线是第一次被触发。
  70.  old = *old_ptr;
  71.  if (old) { //


  72. 。。。。。。

  73. //如果该中断线上存在中断服务例程则让old_ptr指向该例程链表的尾部,以便加入新的服务例程
  74.   do {
  75.    old_ptr = &old->next;
  76.    old = *old_ptr;
  77.   } while (old);
  78.   shared = 1;
  79.  }

  80.  if (!shared) {

  81. //将操作函数集desc->chip的一些未设置的字段设为默认值。
  82.   irq_chip_set_defaults(desc->chip);

  83.   init_waitqueue_head(&desc->wait_for_threads);

  84. /*

  85. 函数__irq_set_trigger()的主要工作是调用函数chip->set_type(irq, flags);设置外部中断的触发方式。

  86. 中断触发方式在文件linux\include\irq.h中定义

  87. #define IRQ_TYPE_NONE 0x00000000 /* Default, unspecified type */
  88. #define IRQ_TYPE_EDGE_RISING 0x00000001 /* Edge rising type */
  89. #define IRQ_TYPE_EDGE_FALLING 0x00000002 /* Edge falling type */
  90. #define IRQ_TYPE_EDGE_BOTH (IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING)
  91. #define IRQ_TYPE_LEVEL_HIGH 0x00000004 /* Level high type */
  92. #define IRQ_TYPE_LEVEL_LOW 0x00000008 /* Level low type */
  93. #define IRQ_TYPE_SENSE_MASK 0x0000000f /* Mask of the above */

  94. IRQF_TRIGGER_MASK在文件interrupt.h中定义

  95. #define IRQF_TRIGGER_NONE 0x00000000
  96. #define IRQF_TRIGGER_RISING 0x00000001
  97. #define IRQF_TRIGGER_FALLING 0x00000002
  98. #define IRQF_TRIGGER_HIGH 0x00000004
  99. #define IRQF_TRIGGER_LOW 0x00000008
  100. #define IRQF_TRIGGER_MASK (IRQF_TRIGGER_HIGH | IRQF_TRIGGER_LOW | \
  101.      IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING)


  102. 可以看出只要外部中断设置了触发方式函数__irq_set_trigger()就会执行。

  103. */
  104.   if (new->flags & IRQF_TRIGGER_MASK)
  105.    ret = __irq_set_trigger(desc, irq, new->flags & IRQF_TRIGGER_MASK);

  106.    if (ret)
  107.     goto out_thread;
  108.   } else
  109.    compat_irq_chip_set_default_handler(desc);

  110. // desc->status的标志IRQ_NOAUTOEN 在中断初始化函数 s3c24xx_init_irq()中调用函数set_irq_flags()设置。

  111.   if (!(desc->status & IRQ_NOAUTOEN)) {
  112.    desc->depth = 0;
  113.    desc->status &= ~IRQ_DISABLED;
  114.    desc->chip->startup(irq); //开启中断线
  115.   } else
  116.    desc->depth = 1;

  117. 。。。。。。
  118.   }

  119.  *old_ptr = new; //将新加入的中断处理例程添加到链表中

  120. 。。。。。。

  121. //在proc文件系统中创建目录。

  122.  new->irq = irq;
  123.  register_irq_proc(irq, desc);
  124.  new->dir = NULL;
  125.  register_handler_proc(irq, new);

  126. 。。。。。。
  127.  return ret;
  128. }

 

中断卸载函数free_irq().。

如果指定的中断线不是共享的 , 那么 , 该函数删除处理程序的同时将禁用这条中断线 。 如果
中断线是共享的,则仅删除 dev_id 所对应的处理程序,而这条中断线本身只有在删除了最
后一个处理程序时才会被禁用。由此可以看出为什么惟一的 dev_ id 如此重要。对于共享的
中断线,需要一个惟一的信息来区分其上面的多个处理程序,并让 free_irq() 仅仅删除指定
的处理程序。如果 dev_id 非空,它都必须与需要删除的处理程序相匹配。非共享中断,该
域可以为空,但需要和注册时使用的指针一致。

 

static struct irqaction *__free_irq(unsigned int irq, void *dev_id)
{
 struct irq_desc *desc = irq_to_desc(irq);
 struct irqaction *action, **action_ptr;
 struct task_struct *irqthread;
 unsigned long flags;

 

 if (!desc)
  return NULL;


 action_ptr = &desc->action;
 for (;;) {
  action = *action_ptr;

/*

根据 dev_id在中断处理例程单项链表中找出一个要卸载的中断例程。

在单项链表中如果卸载了中间的一个还得将前一个和后一个连接起来。在这里用了一个小技巧。

指针**action_ptr中存储了两个对象,一个是&action->next,另一个是action->next。

比如链表中有三个action,我们要卸载的是第二个,此时指针action_ptr中存放的是第一个action中的成员next的地址&action->next。

*action_ptr存放的是第一个action的action->next所指向的对象,即第二个action。此时的action->next即是第三个action。

只要*action_ptr = action->next;就将第一个action的action->next指向了第三个action。

*/

  if (action->dev_id == dev_id)
   break;
  action_ptr = &action->next;
 }

*action_ptr = action->next;

。。。。。。
 if (!desc->action) { // 无其他中断使用该中断线则禁止
  desc->status |= IRQ_DISABLED;
  if (desc->chip->shutdown)
   desc->chip->shutdown(irq);
  else
   desc->chip->disable(irq);
 }

。。。。。。。


 return action; //返回中断处理例程结构体
}
//在函数free_irq中将函数__free_irq返回的中断处理例程结构体释放掉。

void free_irq(unsigned int irq, void *dev_id)
{
 kfree(__free_irq(irq, dev_id));
}

阅读(614) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~