S3C2440之中断寄存器及中断过程

一、S3C2440中断硬件框架

irq_platform

​ S3C2440一共有60个中断源,其中有15个子中断源,它们与SUBSRCPND寄存器中的每一位相对应,其他45个中断源与SRCPND中的每一位相对应。要注意的是EINT47对应的是同一位SRCPND[4],而EINT823对应的也是SRCPND[5]一位。

​ irq中断号:arch/arm/mach-s3c24xx/include/mach/irqs.h

中断寄存器

​ 中断分两大类:外部中断和内部中断。

1.1 外部中断寄存器

​ 24个外部中断占用GPF0-GPF7(EINT0-EINT7),GPG0-GPG15(EINT8-EINT23)。用这些脚做中断输入,则必须配置引脚为中断,并且不要上拉。具体可参考datesheet数据手册。

寄存器 介绍
EXTINT0-EXTINT2 分别设置EINT0—EINT7、EINT8—EINT15、EINT16—EINT23的触发方式(高电平触发、低电平触发、下降沿触发、上升沿触发)。
EINTFLT0-EINTFLT3 控制滤波时钟和滤波宽度。
EINTPEND 这个是中断挂起寄存器,清除时要写1,后面还有几个是写1清除。当一个外部中断(EINT4-EINT23)发生后,那么相应的位会被置1。为什么没有EINT0-EINT3,因为它们分别由SRCPND寄存器的后4位控制。
EINTMASK 这个简单,是屏蔽中断用的,也就是说位为1时,此次中断无效。

1.2 内部中断寄存器

​ 内部中断有8个寄存器。

寄存器 介绍
SUBSRCPND 当一个中断发生后,那么相应的位会被置1,表示一个中断发生了。
INTSUBMSK 与上一个是一样的,中断屏蔽寄存器。
SRCPND 当一个中断发生后,那么相应的位会被置1,表示一个或一类中断发生了。
INTMSK 用来屏蔽SRCPND寄存器所标识的中断。但只能屏蔽IRQ中断,不能屏蔽FIQ中断。
INTMOD 当INTMOD中某位被设置为1时,它对应的中断被设为FIQ,CPU将进入快速中断模式。
PRIORITY 用于设置IRQ中断的优先级。具体使用方法可参考芯片手册。
INTPND 中断优先级仲裁器选出优先级最高中断后,这个中断在INTPND寄存器中的相应位被置1,随后,CPU进入中断模式处理它。同一时间内,此寄存器只有一位被置1。
INTOFFSET 用来表示INTPND寄存器中哪位被置1了,即记录INTPND中位[x]为1的位x的值。清除INTPND、SRCPND时自动清除。

1.3 各寄存器关系:

130303pgvj737c8brzkogn

130306dp7jb9buzpem9jwj

​ 图1 各中断寄存器间的关系

2 中断过程

2.1 内部中断过程

​ a 如果是不带子中断的内部中断:发生后SRCPND相应位置1,如果没有被INTMSK屏蔽,那么等待进一步处理。

​ b 如果是带子中断的内部中断 :发生后SUBSRCPND相应位置1,如果没有被INTSUBMSK屏蔽,那么SRCPND相应位置1, 等待进一步处理,几个SUBSRCPND可能对应同一个SRCPND,对应表如下:

130308twla77lyeae0al3l

2.2 外部中断过程

​ a 如果是外部中断:EINT0-EINT3发生后SRCPND相应位置1,如果没有被INTMSK屏蔽,那么等待进一步处理。EINT4-EINT23发生后EINTPEND相应位置1,如果没有被EINTMASK屏蔽,那么SRCPND相应位EINT4-7 或EINT8-23置1,如果没有被INTMSK屏蔽,等待进一步处理,几个EINTPEND对应同一个SRCPND,对应表如下:

130311a61gyz1kqmtb1au0

​ 三种中断都等待进一步处理了。接下来从SRCPND往下看,看INTMSK。如果中断被屏蔽了,就不用说了(注意:快中断也能被屏蔽)。如果没有被屏蔽,那么会进一步到INTMOD。如果是快中断,那么直接出来,进入FIQ(即CPU进入快中断模式处理)。如果是普通中断,那么SRCPND可以有多为置1(FIQ只能有一个),这时就会经过PRIORITY选出一个优先级高的,然后把根据选出的中断把INTPND相应位置1(注意:只能选出一个),进入IRQ,让CPU处理。

2.3 中断的开启

​ a. 如果是不带子中断的内部中断,只需设置INTMSK,让它不屏蔽中断就可以了。

​ b. 如果是带子中断的内部中断,需设置INTSUBMSK和INTMSK,让它们不屏蔽中断就可以了。

​ c. 如果是外部中断,对于EINT8-23需要设置EINTMASK和INTMSK。对于EINT0-EINT3只需设置INTMSK。

2.4 中断的清除

​ a. 如果是不带子中断的内部中断,只需清除SRCPND,注意清除需位置1。

​ b. 如果是带子中断的内部中断,需清除SRCPND和SUBSRCPND,注意先清除SUBSRCPND,再清除SRCPND。因为,如果你先清除SRCPND的话,然后在清除SUBSRCPND的过程中,SRCPND会以为又有中断发生,又会置1。也就是说一次中断会响应两次。所以必须先掐断源头。

​ c. 如果是外部中断,对于EINT8-23需要清除EINTPEND和SRCPND(同样注意顺序)。对于EINT0-EINT3只需清除SRCPND。

二、中断软件平台

中断入口

​ linux swi 举例:arch/arm/kernel/entry-armv.S

1. 中断向量表定义位置,本文不分析 arch/arm/kernel/entry-common.S 
1. 软中断真正入口地址 arch/arm/kernel/Calls.S     
  1. 调用函数声明 arch/arm/kernel/entry-header.S //部分定义

S3C6410中断以及外部中断 S3C6410的中断主要改进是. 增加中断向量控制器,这样在S3C2440里需要用软件来跳转的中断处理机制,在S3C6410完全由硬件来跳转。你只要把ISR地址是存在连续向量寄存器空间,而不是象S3C2440自行分配空间自行管理。

​ 参考: ARM Linux系统调用详细分析 ARM Linux 系统调用过程 在ARM Linux内核中增加一个新的系统调用

entry-armv.S

1
2
3
4
5
6
.macro	irq_handler
#ifdef CONFIG_GENERIC_IRQ_MULTI_HANDLER
ldr r1, =handle_arch_irq
mov r0, sp
badr lr, 9997f
ldr pc, [r1]
中断平台

entry-armv.S

1
2
3
4
5
6
7
8
9
10
11
12
13
/*
* Interrupt handling. Preserves r7, r8, r9
*/
.macro irq_handler
#ifdef CONFIG_MULTI_IRQ_HANDLER
ldr r5, =handle_arch_irq
mov r0, sp
ldr r5, [r5]
adr lr, BSYM(9997f)
teq r5, #0
movne pc, r5
#endif
arch_irq_handler_default

2种: arch_irq_handler_default、handle_arch_irq 最终都调用到了desc->handle_irq(desc)

(1)arch_irq_handler_default:

1
2
3
4
5
6
arch_irq_handler_default
asm_do_IRQ()
handle_IRQ()
__handle_domain_irq()
generic_handle_irq()
调用用户request_irq注册的中断处理函数

(2)handle_arch_irq :

1
2
3
4
5
6
7
handle_arch_irq
set_handle_irq
gic_handle_irq
handle_domain_irq()
__handle_domain_irq()
generic_handle_irq()
generic_handle_irq_desc()
1
2
3
4
5
6
7
s3c2410_init_irq
s3c24xx_init_intc
// 设置handle_arch_irq, 即中断处理的C语言总入口函数
set_handle_irq(s3c24xx_handle_irq);

s3c24xx_handle_irq
s3c24xx_handle_intc

s3c24xx_handle_irq

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/*
* Array holding pointers to the global controller structs
* [0] ... main_intc
* [1] ... sub_intc
* [2] ... main_intc2 on s3c2416
*/
static struct s3c_irq_intc *s3c_intc[3];


这里有子中断的概念在硬件上,初始化如下
s3c2410_init_irq
s3c_intc[0] = s3c24xx_init_intc(NULL, &init_s3c2410base[0], NULL,0x4a000000);
// s3c24xx_init_intc 的第三个参数是 parent 也就是表示 s3c_intc[1] 是 s3c_intc[0] 的一个子控制器
s3c_intc[1] = s3c24xx_init_intc(NULL, &init_s3c2410subint[0],s3c_intc[0], 0x4a000018);
// s3c24xx_init_intc 里面会设置如下结构体
struct s3c_irq_intc {
void __iomem *reg_pending;
void __iomem *reg_intpnd;
void __iomem *reg_mask;
struct irq_domain *domain;
struct s3c_irq_intc *parent;
struct s3c_irq_data *irqs;
};


s3c24xx_handle_intc
// 1. 读取 reg_intpnd 这个应该就是顶级中断控制器的 pend 标志
readl_relaxed
// 2. 计算出硬件中断号
irq_domain_get_of_node
offset = readl_relaxed(intc->reg_intpnd + 4);
offset = __ffs(pnd);

handle_domain_irq(domain,hwirq,regs)
__handle_domain_irq
// 找到虚拟中断号
irq = irq_find_mapping(domain, hwirq);
// 根据虚拟中断号,找到描述符所在,执行 irq_flow_handler_t handle_irq
generic_handle_irq
// 这里就是找到中断描述符的结构
desc = irq_to_desc(irq)
// 执行 irq_flow_handler_t handle_irq
generic_handle_irq_desc((desc))

//先设置好linear_revmap,也就是一般就是硬件中断号与虚拟中断号
中断注册
1
2
3
4
5
//新的domain注册irq
irq_domain_add_legacy

//老的request_irq
request_irq(info->irq, uio_interrupt,info->irq_flags, info->name, idev);
中断应用

gpio request_irq

OnionIoT/gpio-irq: GPIO IRQ support for AR9331 SoC and OpenWRT

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
/*
* GPIO IRQ handler for AR9331
*
* Copyright (C) 2013-2015 Gerhard Bertelsmann <info@gerhard-bertelsmann.de>
* Copyright (C) 2015 Dmitriy Zherebkov <dzh@black-swift.com>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 as published
* by the Free Software Foundation.
*/

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/gpio.h>
#include <linux/interrupt.h>

#include <asm/siginfo.h>
#include <linux/rcupdate.h>
#include <linux/sched.h>
#include <linux/debugfs.h>
#include <linux/uaccess.h>

//#define DEBUG_OUT

#ifdef DEBUG_OUT
#define debug(fmt,args...) printk (KERN_INFO fmt ,##args)
#else
#define debug(fmt,args...)
#endif /* DEBUG_OUT */

//#define SIG_GPIO_IRQ (SIGRTMIN+10) // SIGRTMIN is different in Kernel and User modes
#define SIG_GPIO_IRQ 42 // So we have to hardcode this value

////////////////////////////////////////////////////////////////////////////////////////////

#define DRV_NAME "GPIO IRQ handler"
#define FILE_NAME "gpio-irq"

////////////////////////////////////////////////////////////////////////////////////////////

#define MAX_PROCESSES 10

typedef struct
{
int gpio;
int irq;
int last_value;

pid_t processes[MAX_PROCESSES];
} _gpio_handler;

#define TOTAL_GPIO 50 // arbitrary number, but enough for may embedded SoCs (MT7688 has 0..46)
static _gpio_handler all_handlers[TOTAL_GPIO];
static struct dentry* in_file;

////////////////////////////////////////////////////////////////////////////////////////////
static int is_space(char symbol)
{
return (symbol == ' ') || (symbol == '\t');
}

////////////////////////////////////////////////////////////////////////////////////////////
static int is_digit(char symbol)
{
return (symbol >= '0') && (symbol <= '9');
}

////////////////////////////////////////////////////////////////////////////////////////////
static int is_eol(char symbol)
{
return (symbol == '\n') || (symbol == '\r');
}

////////////////////////////////////////////////////////////////////////////////////////////
static irqreturn_t gpio_edge_interrupt(int irq, void* dev_id)
{
_gpio_handler* handler=(_gpio_handler*)dev_id;

if(handler && (handler->irq == irq))
{
int val=0;

debug("Got _handler!\n");

val = gpio_get_value(handler->gpio)!=0;

if(val != handler->last_value)
{
struct siginfo info;
struct task_struct* ts=NULL;

int i=0;

handler->last_value=val;
debug("IRQ %d event (GPIO %d) - new value is %d!\n", irq, handler->gpio, val);

/* send the signal */
memset(&info, 0, sizeof(struct siginfo));
info.si_signo = SIG_GPIO_IRQ;
info.si_code = SI_QUEUE; // this is bit of a trickery: SI_QUEUE is normally used by sigqueue from user space,
// and kernel space should use SI_KERNEL. But if SI_KERNEL is used the real_time data
// is not delivered to the user space signal handler function.

for(i=0; i < MAX_PROCESSES; ++i)
{
pid_t pid=handler->processes[i];
if(pid == 0) break;
info.si_int=(handler->gpio << 24) | (val & 1);
rcu_read_lock();
ts=pid_task(find_vpid(pid), PIDTYPE_PID);
rcu_read_unlock();
if(ts == NULL)
{
debug("PID %u is not found, remove it please.\n",pid);
}
else
{
debug("Sending signal to PID %u.\n",pid);
send_sig_info(SIG_GPIO_IRQ, &info, ts); //send the signal
}
}
}
}
else
{
debug("IRQ %d event - no handlers found!\n",irq);
}

return(IRQ_HANDLED);
}

////////////////////////////////////////////////////////////////////////////////////////////
static int add_irq(int gpio,void* data)
{
if(gpio_request(gpio, DRV_NAME) >= 0)
{
int irq_number=gpio_to_irq(gpio);

if(irq_number >= 0)
{
int err = request_irq(irq_number, gpio_edge_interrupt, IRQ_TYPE_EDGE_BOTH, "gpio_irq_handler", data);

if(!err)
{
debug("Got IRQ %d for GPIO %d\n", irq_number, gpio);
return irq_number;
}
else
{
debug("GPIO IRQ handler: trouble requesting IRQ %d error %d\n",irq_number, err);
}
}
else
{
debug("Can't map GPIO %d to IRQ : error %d\n",gpio, irq_number);
}
}
else
{
debug("Can't get GPIO %d\n", gpio);
}

return -1;
}

////////////////////////////////////////////////////////////////////////////////////////////
static void free_handler(int gpio)
{
if((gpio >= 0) && (gpio < TOTAL_GPIO))
{
_gpio_handler* handler=&all_handlers[gpio];

if(handler->gpio == gpio)
{
int i=0;

if(handler->irq >= 0)
{
free_irq(handler->irq, (void*)handler);
handler->irq=-1;
}

gpio_free(gpio);
handler->gpio=-1;

for(; i < MAX_PROCESSES; ++i)
{
handler->processes[i]=0;
}
}
}
}

////////////////////////////////////////////////////////////////////////////////////////////
static void remove_handler(int gpio,pid_t pid)
{
if((gpio >= 0) && (gpio < TOTAL_GPIO))
{
_gpio_handler* handler=&all_handlers[gpio];

if(handler->gpio == gpio)
{
int i=0;
for(i=0; i < MAX_PROCESSES; ++i)
{
if(handler->processes[i] == pid)
{
for(++i; i < MAX_PROCESSES; ++i)
{
handler->processes[i-1]=handler->processes[i];
}
handler->processes[i-1]=0;
return;
}
}

debug("Handler for GPIO %d to PID %u is not found.\n", gpio, pid);
}
}
}

////////////////////////////////////////////////////////////////////////////////////////////
static int add_handler(int gpio, pid_t pid)
{
if((gpio >= 0) && (gpio < TOTAL_GPIO))
{
_gpio_handler* handler=&all_handlers[gpio];
int p=0;

if(handler->gpio != gpio)
{
int irq=add_irq(gpio, handler);

if(irq < 0)
{
free_handler(gpio);
return -1;
}

handler->gpio=gpio;
handler->irq=irq;
handler->last_value=-1;
}

while((handler->processes[p] > 0) && (handler->processes[p] != pid)) ++p;

if(p < MAX_PROCESSES)
{
handler->processes[p]=pid;
return handler->irq;
}
else
{
debug("Can't add handler: %d processes already handle GPIO %d.\n", p, gpio);
}
}

return -1;
}

////////////////////////////////////////////////////////////////////////////////////////////
static ssize_t run_command(struct file *file, const char __user *buf,
size_t count, loff_t *ppos)
{
char buffer[512];
char line[20];
char* in_pos=NULL;
char* end=NULL;
char* out_pos=NULL;

int add=0;
int gpio=-1;
int pid=0;

if(count > 512)
return -EINVAL; // file is too big

copy_from_user(buffer, buf, count);
buffer[count]=0;

debug("Command is found (%u bytes length):\n%s\n",count,buffer);

in_pos=buffer;
end=in_pos+count-1;

while(in_pos < end)
{
add=0;
gpio=-1;
pid=0;

while((in_pos < end) && is_space(*in_pos)) ++in_pos; // skip whitespace
if(in_pos >= end) break;

if(*in_pos == '+')
{
add=1;
}
else if(*in_pos == '-')
{
add=0;
}
else if(*in_pos == '?')
{
// just print all handlers
int i,j;

for(i=0; i < TOTAL_GPIO; ++i)
{
if(all_handlers[i].gpio != -1)
{
printk(KERN_INFO "GPIO %d (IRQ %d): ",all_handlers[i].gpio,all_handlers[i].irq);

for(j=0; j < MAX_PROCESSES; ++j)
{
if(all_handlers[i].processes[j] != 0)
{
printk("%u ",all_handlers[i].processes[j]);
}
else
{
break;
}
}

printk("\n");
}
}

return count;
}
else
{
printk(KERN_INFO "Wrong command '%c'.\n", *in_pos);
break;
}
++in_pos;
while((in_pos < end) && is_space(*in_pos)) ++in_pos; // skip whitespace

out_pos=line;
while((in_pos < end) && is_digit(*in_pos)) *out_pos++=*in_pos++;
*out_pos=0;

if(is_digit(line[0]))
{
sscanf(line, "%d", &gpio);
}
else
{
printk(KERN_INFO "Can't read GPIO number.\n");
break;
}

while((in_pos < end) && is_space(*in_pos)) ++in_pos; // skip whitespace

out_pos=line;
while((in_pos < end) && is_digit(*in_pos)) *out_pos++=*in_pos++;
*out_pos=0;

if(is_digit(line[0]))
{
sscanf(line, "%u", &pid);
}
else
{
if(add)
{
printk(KERN_INFO "Can't read PID.\n");
break;
}
}

if(add)
{
debug("Trying to add handler for GPIO %d to PID %u.\n",gpio,pid);
add_handler(gpio,pid);
}
else
{
if(pid)
{
_gpio_handler* handler=&all_handlers[gpio];

debug("Trying to remove handler for GPIO %d to PID %u.\n",gpio,pid);
remove_handler(gpio,pid);

if(handler->processes[0] == 0)
{
debug("It was the last handler. Let's free IRQ %d.\n",handler->irq);
free_handler(gpio);
}
}
else
{
debug("Trying to remove all handlers for GPIO %d.\n",gpio);
free_handler(gpio);
}
}

while((in_pos < end) && (is_space(*in_pos) || is_eol(*in_pos))) ++in_pos; // next line
}

return count;
}

////////////////////////////////////////////////////////////////////////////////////////////
static const struct file_operations irq_fops = {
// .read = show_handlers,
.write = run_command,
};

////////////////////////////////////////////////////////////////////////////////////////////
static int __init mymodule_init(void)
{
int i=0,j=0;

for(i=0; i < TOTAL_GPIO; ++i)
{
all_handlers[i].gpio=-1;
all_handlers[i].irq=-1;
all_handlers[i].gpio=-1;

for(j=0; j < MAX_PROCESSES; ++j)
{
all_handlers[i].processes[j]=0;
}
}
in_file=debugfs_create_file(FILE_NAME, 0666, NULL, NULL, &irq_fops);
debug("Waiting for commands in file /sys/kernel/debug/" FILE_NAME ".\n");
return 0;
}

////////////////////////////////////////////////////////////////////////////////////////////
static void __exit mymodule_exit(void)
{
int i=0;

for(; i < TOTAL_GPIO; ++i)
{
free_handler(i);
}

debugfs_remove(in_file);

return;
}

////////////////////////////////////////////////////////////////////////////////////////////
module_init(mymodule_init);
module_exit(mymodule_exit);
////////////////////////////////////////////////////////////////////////////////////////////
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Dmitriy Zherebkov (Black Swift team)");
////////////////////////////////////////////////////////////////////////////////////////////

设备树中的中断

https://blog.csdn.net/weixin_33796205/article/details/93873014


S3C2440之中断寄存器及中断过程
http://witbit.cn/EMBEDDED/mini2440/S3C2440之中断寄存器及中断过程.html
作者
朝彻
发布于
2025年2月13日
许可协议