mirror of
https://e.coding.net/weidongshan/linux/doc_and_source_for_drivers.git
synced 2025-12-02 12:51:16 +08:00
08_Interrupt完结
This commit is contained in:
@@ -256,7 +256,7 @@ GICD_ISPENDRn有多个寄存器,每个寄存器中每一位对应一个GIC中
|
||||
* 编译:
|
||||
|
||||
* 在Ubuntu上
|
||||
* 修改`003_virtual_int_controller_legacy`中的Makefile,指定内核路径`KERN_DIR`,在执行`make`命令即可。
|
||||
* 修改`04_virtual_int_controller_legacy_ok`中的Makefile,指定内核路径`KERN_DIR`,在执行`make`命令即可。
|
||||
|
||||
* 安装:
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
## 层级中断控制器驱动程序编写与实验
|
||||
## 层级中断控制器驱动程序编写
|
||||
|
||||
参考资料:
|
||||
|
||||
@@ -20,11 +20,11 @@
|
||||
```shell
|
||||
doc_and_source_for_drivers\
|
||||
IMX6ULL\source\08_Interrupt\
|
||||
06_virtual_int_controller_hierarchy_ok
|
||||
06_virtual_int_controller_hierarchy
|
||||
|
||||
doc_and_source_for_drivers\
|
||||
STM32MP157\source\A7\08_Interrupt\
|
||||
06_virtual_int_controller_hierarchy_ok
|
||||
06_virtual_int_controller_hierarchy
|
||||
```
|
||||
|
||||
|
||||
@@ -124,5 +124,15 @@
|
||||
|
||||
会涉及2个驱动程序:虚拟的中断控制器驱动程序,按键驱动程序,以及对应的设备树。
|
||||
|
||||
虚拟的中断控制器驱动程序中,涉及2个递归处理。
|
||||
|
||||
#### 3.1 alloc的递归处理
|
||||
|
||||

|
||||
|
||||
|
||||
|
||||
#### 3.2 irq_chip的递归处理
|
||||
|
||||

|
||||
|
||||
BIN
IMX6ULL/doc_pic/08_Interrupt/18_层级中断控制器驱动程序编写.tif
Normal file
BIN
IMX6ULL/doc_pic/08_Interrupt/18_层级中断控制器驱动程序编写.tif
Normal file
Binary file not shown.
299
IMX6ULL/doc_pic/08_Interrupt/19_层级中断控制器驱动程序上机实验.md
Normal file
299
IMX6ULL/doc_pic/08_Interrupt/19_层级中断控制器驱动程序上机实验.md
Normal file
@@ -0,0 +1,299 @@
|
||||
## 层级中断控制器驱动程序上机实验
|
||||
|
||||
参考资料:
|
||||
|
||||
* [linux kernel的中断子系统之(七):GIC代码分析](http://www.wowotech.net/irq_subsystem/gic_driver.html)
|
||||
|
||||
* Linux 4.9.88内核源码
|
||||
|
||||
* `Linux-4.9.88\drivers\gpio\gpio-mxc.c`
|
||||
* `Linux-4.9.88\arch\arm\boot\dts\imx6ull.dtsi`
|
||||
|
||||
* Linux 5.4内核源码
|
||||
|
||||
* `Linux-5.4\drivers\pinctrl\stm32\pinctrl-stm32mp157.c`
|
||||
* `Linux-5.4\drivers\irqchip\irq-stm32-exti.c`
|
||||
* `Linux-5.4\arch\arm\boot\dts\stm32mp151.dtsi`
|
||||
|
||||
* 芯片手册
|
||||
|
||||
* IMX6ULL: imx6ullrm.pdf
|
||||
* STM32MP157: DM00327659.pdf
|
||||
|
||||
* 本节视频源码在GIT仓库里
|
||||
|
||||
```shell
|
||||
doc_and_source_for_drivers\
|
||||
IMX6ULL\source\08_Interrupt\
|
||||
07_virtual_int_controller_hierarchy_ok
|
||||
|
||||
doc_and_source_for_drivers\
|
||||
STM32MP157\source\A7\08_Interrupt\
|
||||
07_virtual_int_controller_hierarchy_ok
|
||||
```
|
||||
|
||||
|
||||
|
||||
### 1. 确定中断号n
|
||||
|
||||

|
||||
|
||||
查看芯片手册,选择一个保留的、未使用的GIC SPI中断即可。
|
||||
|
||||
#### 1.1 IMX6ULL
|
||||
|
||||
看芯片手册第3章:
|
||||
|
||||

|
||||
|
||||
看上图,选择122号中断,它是SPI里的122号中断,GIC里的编号是(32+122)=154。
|
||||
|
||||
|
||||
|
||||
#### 1.2 STM32MP157
|
||||
|
||||
看芯片手册第21.2节:
|
||||
|
||||

|
||||
|
||||
看上图,选择210号中断,它是SPI里的210号中断,GIC里的编号是(32+210)=242。
|
||||
|
||||
### 2. 怎么触发中断
|
||||
|
||||
可以通过devmem命令直接写GIC的PENDING寄存区。
|
||||
|
||||

|
||||
|
||||
GICD_ISPENDRn有多个寄存器,每个寄存器中每一位对应一个GIC中断,写入1就可以触发该中断。
|
||||
|
||||
写哪一个GICD_ISPENDRn寄存器?写哪一位?使用下列公式来确定:
|
||||
|
||||

|
||||
|
||||
查看内核设备树文件imx6ull.dtsi、stm32mp151.dtsi,可以知道:
|
||||
|
||||
* IMX6ULL的GIC Distributor 地址是:0x00a01000
|
||||

|
||||
* STM32MP157的GIC Distributor 地址是:0xa0021000
|
||||

|
||||
|
||||
| 芯片 | SPI中断号 | GIC中断号 | n,bit | GICD_ISPENDRn地址 | 命令 |
|
||||
| ---------- | --------- | --------- | ----- | ----------------- | ----------------------------- |
|
||||
| IMX6LLL | 122 | 154 | 4,26 | 0xa01210 | devmem 0xa01210 32 0x4000000 |
|
||||
| | 123 | 155 | 4,27 | 0xa01210 | devmem 0xa01210 32 0x8000000 |
|
||||
| | 124 | 156 | 4,28 | 0xa01210 | devmem 0xa01210 32 0x10000000 |
|
||||
| | 125 | 157 | 4,29 | 0xa01210 | devmem 0xa01210 32 0x20000000 |
|
||||
| STM32MP157 | 210 | 242 | 7,18 | 0xa002121c | devmem 0xa002121c 32 0x40000 |
|
||||
| | 211 | 243 | 7,19 | 0xa002121c | devmem 0xa002121c 32 0x80000 |
|
||||
| | 212 | 244 | 7,20 | 0xa002121c | devmem 0xa002121c 32 0x100000 |
|
||||
| | 213 | 245 | 7,21 | 0xa002121c | devmem 0xa002121c 32 0x200000 |
|
||||
|
||||
|
||||
|
||||
### 3. 上机实验
|
||||
|
||||
#### 3.1 设置工具链
|
||||
|
||||
##### 1. STM32MP157
|
||||
|
||||
```shell
|
||||
export ARCH=arm
|
||||
export CROSS_COMPILE=arm-buildroot-linux-gnueabihf-
|
||||
export PATH=$PATH:/home/book/100ask_stm32mp157_pro-sdk/ToolChain/arm-buildroot-linux-gnueabihf_sdk-buildroot/bin
|
||||
```
|
||||
|
||||
##### 2. IMX6ULL
|
||||
|
||||
```shell
|
||||
export ARCH=arm
|
||||
export CROSS_COMPILE=arm-linux-gnueabihf-
|
||||
export PATH=$PATH:/home/book/100ask_imx6ull-sdk/ToolChain/gcc-linaro-6.2.1-2016.11-x86_64_arm-linux-gnueabihf/bin
|
||||
```
|
||||
|
||||
|
||||
|
||||
#### 3.2 编译、替换设备树
|
||||
|
||||
##### 1. STM32MP157
|
||||
|
||||
* 修改`arch/arm/boot/dts/stm32mp157c-100ask-512d-lcd-v1.dts`,添加如下代码:
|
||||
|
||||
```shell
|
||||
/ {
|
||||
virtual_intc: virtual_intc_100ask {
|
||||
compatible = "100ask,virtual_intc";
|
||||
|
||||
interrupt-controller;
|
||||
#interrupt-cells = <2>;
|
||||
|
||||
interrupt-parent = <&intc>;
|
||||
//upper_hwirq_base = <122>; // imx6ull
|
||||
upper_hwirq_base = <210>; // stm32mp157
|
||||
};
|
||||
|
||||
gpio_keys_100ask {
|
||||
compatible = "100ask,gpio_key";
|
||||
interrupt-parent = <&virtual_intc>;
|
||||
interrupts = <0 IRQ_TYPE_LEVEL_HIGH>,
|
||||
<1 IRQ_TYPE_LEVEL_HIGH>,
|
||||
<2 IRQ_TYPE_LEVEL_HIGH>,
|
||||
<3 IRQ_TYPE_LEVEL_HIGH>;
|
||||
};
|
||||
};
|
||||
```
|
||||
|
||||
|
||||
|
||||
* 编译设备树:
|
||||
在Ubuntu的STM32MP157内核目录下执行如下命令,
|
||||
得到设备树文件:`arch/arm/boot/dts/stm32mp157c-100ask-512d-lcd-v1.dtb`
|
||||
|
||||
```shell
|
||||
make dtbs
|
||||
```
|
||||
|
||||
* 复制到NFS目录:
|
||||
|
||||
```shell
|
||||
$ cp arch/arm/boot/dts/stm32mp157c-100ask-512d-lcd-v1.dtb ~/nfs_rootfs/
|
||||
```
|
||||
|
||||
* 开发板上挂载NFS文件系统
|
||||
|
||||
* vmware使用NAT(假设windowsIP为192.168.1.100)
|
||||
|
||||
```shell
|
||||
[root@100ask:~]# mount -t nfs -o nolock,vers=3,port=2049,mountport=9999
|
||||
192.168.1.100:/home/book/nfs_rootfs /mnt
|
||||
```
|
||||
|
||||
* vmware使用桥接,或者不使用vmware而是直接使用服务器:假设Ubuntu IP为192.168.1.137
|
||||
|
||||
```shell
|
||||
[root@100ask:~]# mount -t nfs -o nolock,vers=3 192.168.1.137:/home/book/nfs_rootfs /mnt
|
||||
```
|
||||
|
||||
* 更新设备树
|
||||
|
||||
```shell
|
||||
[root@100ask:~]# mount /dev/mmcblk2p2 /boot
|
||||
[root@100ask:~]# cp /mnt/stm32mp157c-100ask-512d-lcd-v1.dtb /boot
|
||||
[root@100ask:~]# sync
|
||||
```
|
||||
|
||||
* 重启开发板
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
##### 2. IMX6ULL
|
||||
|
||||
* 修改`arch/arm/boot/dts/100ask_imx6ull-14x14.dts`,添加如下代码:
|
||||
|
||||
```shell
|
||||
/ {
|
||||
virtual_intc: virtual_intc_100ask {
|
||||
compatible = "100ask,virtual_intc";
|
||||
|
||||
interrupt-controller;
|
||||
#interrupt-cells = <2>;
|
||||
|
||||
interrupt-parent = <&intc>;
|
||||
upper_hwirq_base = <122>; // imx6ull
|
||||
//upper_hwirq_base = <210>; // stm32mp157
|
||||
};
|
||||
|
||||
gpio_keys_100ask {
|
||||
compatible = "100ask,gpio_key";
|
||||
interrupt-parent = <&virtual_intc>;
|
||||
interrupts = <0 IRQ_TYPE_LEVEL_HIGH>,
|
||||
<1 IRQ_TYPE_LEVEL_HIGH>,
|
||||
<2 IRQ_TYPE_LEVEL_HIGH>,
|
||||
<3 IRQ_TYPE_LEVEL_HIGH>;
|
||||
};
|
||||
|
||||
};
|
||||
```
|
||||
|
||||
|
||||
|
||||
* 编译设备树:
|
||||
在Ubuntu的IMX6ULL内核目录下执行如下命令,
|
||||
得到设备树文件:`arch/arm/boot/dts/100ask_imx6ull-14x14.dtb`
|
||||
|
||||
```shell
|
||||
make dtbs
|
||||
```
|
||||
|
||||
* 复制到NFS目录:
|
||||
|
||||
```shell
|
||||
$ cp arch/arm/boot/dts/100ask_imx6ull-14x14.dtb ~/nfs_rootfs/
|
||||
```
|
||||
|
||||
* 开发板上挂载NFS文件系统
|
||||
|
||||
* vmware使用NAT(假设windowsIP为192.168.1.100)
|
||||
|
||||
```shell
|
||||
[root@100ask:~]# mount -t nfs -o nolock,vers=3,port=2049,mountport=9999
|
||||
192.168.1.100:/home/book/nfs_rootfs /mnt
|
||||
```
|
||||
|
||||
* vmware使用桥接,或者不使用vmware而是直接使用服务器:假设Ubuntu IP为192.168.1.137
|
||||
|
||||
```shell
|
||||
[root@100ask:~]# mount -t nfs -o nolock,vers=3 192.168.1.137:/home/book/nfs_rootfs /mnt
|
||||
```
|
||||
|
||||
* 更新设备树
|
||||
|
||||
```shell
|
||||
[root@100ask:~]# cp /mnt/100ask_imx6ull-14x14.dtb /boot
|
||||
[root@100ask:~]# sync
|
||||
```
|
||||
|
||||
* 重启开发板
|
||||
|
||||
|
||||
|
||||
#### 3.3 编译、安装驱动程序
|
||||
|
||||
* 编译:
|
||||
|
||||
* 在Ubuntu上
|
||||
* 修改`07_virtual_int_controller_hierarchy_ok`中的Makefile,指定内核路径`KERN_DIR`,在执行`make`命令即可。
|
||||
|
||||
* 安装:
|
||||
|
||||
* 在开发板上
|
||||
|
||||
* 挂载NFS,复制文件,insmod,类似如下命令:
|
||||
|
||||
```shell
|
||||
mount -t nfs -o nolock,vers=3 192.168.1.137:/home/book/nfs_rootfs /mnt
|
||||
// 对于IMX6ULL,想看到驱动打印信息,需要先执行
|
||||
echo "7 4 1 7" > /proc/sys/kernel/printk
|
||||
|
||||
insmod -f /mnt/virtual_int_controller.ko
|
||||
// 安装virtual_int_controller之后即可进入/sys/kernel/irq目录查看分配的中断号
|
||||
|
||||
insmod -f /mnt/gpio_key_drv.ko
|
||||
cat /proc/interrupts
|
||||
|
||||
// 触发中断
|
||||
// 对于IMX6ULL
|
||||
devmem 0xa01210 32 0x4000000
|
||||
devmem 0xa01210 32 0x8000000
|
||||
devmem 0xa01210 32 0x10000000
|
||||
devmem 0xa01210 32 0x20000000
|
||||
|
||||
// 对于stm32mp157
|
||||
devmem 0xa002121c 32 0x40000
|
||||
devmem 0xa002121c 32 0x80000
|
||||
devmem 0xa002121c 32 0x100000 // 它不能触发中断,可能是被占用了
|
||||
devmem 0xa002121c 32 0x200000
|
||||
```
|
||||
|
||||
* 观察内核打印的信息
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 28 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 27 KiB |
@@ -0,0 +1,21 @@
|
||||
|
||||
# 1. 使用不同的开发板内核时, 一定要修改KERN_DIR
|
||||
# 2. KERN_DIR中的内核要事先配置、编译, 为了能编译内核, 要先设置下列环境变量:
|
||||
# 2.1 ARCH, 比如: export ARCH=arm64
|
||||
# 2.2 CROSS_COMPILE, 比如: export CROSS_COMPILE=aarch64-linux-gnu-
|
||||
# 2.3 PATH, 比如: export PATH=$PATH:/home/book/100ask_roc-rk3399-pc/ToolChain-6.3.1/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin
|
||||
# 注意: 不同的开发板不同的编译器上述3个环境变量不一定相同,
|
||||
# 请参考各开发板的高级用户使用手册
|
||||
|
||||
KERN_DIR = /home/book/100ask_imx6ull-sdk/Linux-4.9.88
|
||||
|
||||
all:
|
||||
make -C $(KERN_DIR) M=`pwd` modules
|
||||
|
||||
clean:
|
||||
make -C $(KERN_DIR) M=`pwd` modules clean
|
||||
rm -rf modules.order
|
||||
|
||||
obj-m += virtual_int_controller.o
|
||||
obj-m += gpio_key_drv.o
|
||||
|
||||
@@ -0,0 +1,125 @@
|
||||
#include <linux/module.h>
|
||||
|
||||
#include <linux/fs.h>
|
||||
#include <linux/errno.h>
|
||||
#include <linux/miscdevice.h>
|
||||
#include <linux/kernel.h>
|
||||
#include <linux/major.h>
|
||||
#include <linux/mutex.h>
|
||||
#include <linux/proc_fs.h>
|
||||
#include <linux/seq_file.h>
|
||||
#include <linux/stat.h>
|
||||
#include <linux/init.h>
|
||||
#include <linux/device.h>
|
||||
#include <linux/tty.h>
|
||||
#include <linux/kmod.h>
|
||||
#include <linux/gfp.h>
|
||||
#include <linux/gpio/consumer.h>
|
||||
#include <linux/platform_device.h>
|
||||
#include <linux/of_gpio.h>
|
||||
#include <linux/of_irq.h>
|
||||
#include <linux/interrupt.h>
|
||||
#include <linux/irq.h>
|
||||
#include <linux/slab.h>
|
||||
|
||||
|
||||
struct gpio_key{
|
||||
char name[100];
|
||||
int irq;
|
||||
int cnt;
|
||||
} ;
|
||||
|
||||
static struct gpio_key gpio_keys_100ask[100];
|
||||
|
||||
static irqreturn_t gpio_key_isr(int irq, void *dev_id)
|
||||
{
|
||||
struct gpio_key *gpio_key = dev_id;
|
||||
|
||||
|
||||
printk("gpio_key_isr %s cnt %d\n", gpio_key->name, gpio_key->cnt++);
|
||||
|
||||
return IRQ_HANDLED;
|
||||
}
|
||||
|
||||
/* 1. 从platform_device获得GPIO
|
||||
* 2. gpio=>irq
|
||||
* 3. request_irq
|
||||
*/
|
||||
static int gpio_key_probe(struct platform_device *pdev)
|
||||
{
|
||||
int err;
|
||||
int i = 0;
|
||||
int irq;
|
||||
|
||||
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
|
||||
|
||||
|
||||
while (1)
|
||||
{
|
||||
irq = platform_get_irq(pdev, i);
|
||||
if (irq <= 0)
|
||||
break;
|
||||
gpio_keys_100ask[i].irq = irq;
|
||||
sprintf(gpio_keys_100ask[i].name, "100as_virtual_key%d", i);
|
||||
|
||||
err = devm_request_irq(&pdev->dev, gpio_keys_100ask[i].irq, gpio_key_isr, IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING, gpio_keys_100ask[i].name, &gpio_keys_100ask[i]);
|
||||
printk("devm_request_irq %d for %s, err = %d\n", irq, gpio_keys_100ask[i].name, err);
|
||||
i++;
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
}
|
||||
|
||||
static int gpio_key_remove(struct platform_device *pdev)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static const struct of_device_id ask100_keys[] = {
|
||||
{ .compatible = "100ask,gpio_key" },
|
||||
{ },
|
||||
};
|
||||
|
||||
/* 1. 定义platform_driver */
|
||||
static struct platform_driver gpio_keys_driver = {
|
||||
.probe = gpio_key_probe,
|
||||
.remove = gpio_key_remove,
|
||||
.driver = {
|
||||
.name = "100ask_gpio_key",
|
||||
.of_match_table = ask100_keys,
|
||||
},
|
||||
};
|
||||
|
||||
/* 2. 在入口函数注册platform_driver */
|
||||
static int __init gpio_key_init(void)
|
||||
{
|
||||
int err;
|
||||
|
||||
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
|
||||
|
||||
err = platform_driver_register(&gpio_keys_driver);
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
/* 3. 有入口函数就应该有出口函数:卸载驱动程序时,就会去调用这个出口函数
|
||||
* 卸载platform_driver
|
||||
*/
|
||||
static void __exit gpio_key_exit(void)
|
||||
{
|
||||
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
|
||||
|
||||
platform_driver_unregister(&gpio_keys_driver);
|
||||
}
|
||||
|
||||
|
||||
/* 7. 其他完善:提供设备信息,自动创建设备节点 */
|
||||
|
||||
module_init(gpio_key_init);
|
||||
module_exit(gpio_key_exit);
|
||||
|
||||
MODULE_LICENSE("GPL");
|
||||
|
||||
|
||||
@@ -0,0 +1,163 @@
|
||||
#include <linux/kernel.h>
|
||||
#include <linux/module.h>
|
||||
#include <linux/clk.h>
|
||||
#include <linux/err.h>
|
||||
#include <linux/init.h>
|
||||
#include <linux/interrupt.h>
|
||||
#include <linux/io.h>
|
||||
#include <linux/random.h>
|
||||
#include <linux/irq.h>
|
||||
#include <linux/irqdomain.h>
|
||||
#include <linux/irqchip/chained_irq.h>
|
||||
#include <linux/platform_device.h>
|
||||
#include <linux/pm_runtime.h>
|
||||
#include <linux/slab.h>
|
||||
#include <linux/gpio/driver.h>
|
||||
/* FIXME: for gpio_get_value() replace this with direct register read */
|
||||
#include <linux/gpio.h>
|
||||
#include <linux/of.h>
|
||||
#include <linux/of_device.h>
|
||||
#include <linux/bug.h>
|
||||
|
||||
|
||||
static struct irq_domain *virtual_intc_domain;
|
||||
static u32 upper_hwirq_base;
|
||||
|
||||
static int virtual_intc_domain_translate(struct irq_domain *d,
|
||||
struct irq_fwspec *fwspec,
|
||||
unsigned long *hwirq,
|
||||
unsigned int *type)
|
||||
{
|
||||
if (is_of_node(fwspec->fwnode)) {
|
||||
if (fwspec->param_count != 2)
|
||||
return -EINVAL;
|
||||
|
||||
*hwirq = fwspec->param[0];
|
||||
*type = fwspec->param[1];
|
||||
return 0;
|
||||
}
|
||||
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
static void virtual_intc_irq_unmask(struct irq_data *d)
|
||||
{
|
||||
printk("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);
|
||||
irq_chip_unmask_parent(d);
|
||||
}
|
||||
|
||||
static void virtual_intc_irq_mask(struct irq_data *d)
|
||||
{
|
||||
printk("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);
|
||||
irq_chip_mask_parent(d);
|
||||
}
|
||||
|
||||
static struct irq_chip virtual_intc_chip = {
|
||||
.name = "virtual_intc",
|
||||
.irq_mask = virtual_intc_irq_mask,
|
||||
.irq_unmask = virtual_intc_irq_unmask,
|
||||
};
|
||||
|
||||
|
||||
static int virtual_intc_domain_alloc(struct irq_domain *domain,
|
||||
unsigned int irq,
|
||||
unsigned int nr_irqs, void *data)
|
||||
{
|
||||
struct irq_fwspec *fwspec = data;
|
||||
struct irq_fwspec parent_fwspec;
|
||||
irq_hw_number_t hwirq;
|
||||
int i;
|
||||
|
||||
/* 设置irq_desc[irq] */
|
||||
|
||||
/* 1. 设置irq_desc[irq].irq_data, 里面含有virtual_intc irq_chip */
|
||||
hwirq = fwspec->param[0];
|
||||
for (i = 0; i < nr_irqs; i++)
|
||||
irq_domain_set_hwirq_and_chip(domain, irq + i, hwirq + i,
|
||||
&virtual_intc_chip, NULL);
|
||||
|
||||
/* 2. 设置irq_desc[irq].handle_irq, 来自GIC */
|
||||
parent_fwspec.fwnode = domain->parent->fwnode;
|
||||
parent_fwspec.param_count = 3;
|
||||
parent_fwspec.param[0] = GIC_SPI;
|
||||
parent_fwspec.param[1] = fwspec->param[0] + upper_hwirq_base;
|
||||
parent_fwspec.param[2] = fwspec->param[1];
|
||||
|
||||
return irq_domain_alloc_irqs_parent(domain, irq, nr_irqs,
|
||||
&parent_fwspec);
|
||||
}
|
||||
|
||||
static const struct irq_domain_ops virtual_intc_domain_ops = {
|
||||
.translate = virtual_intc_domain_translate,
|
||||
.alloc = virtual_intc_domain_alloc,
|
||||
};
|
||||
|
||||
static int virtual_intc_probe(struct platform_device *pdev)
|
||||
{
|
||||
|
||||
struct irq_domain *parent_domain;
|
||||
struct device_node *parent;
|
||||
int err;
|
||||
|
||||
|
||||
printk("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);
|
||||
err = of_property_read_u32(pdev->dev.of_node, "upper_hwirq_base", &upper_hwirq_base);
|
||||
|
||||
parent = of_irq_find_parent(pdev->dev.of_node);
|
||||
parent_domain = irq_find_host(parent);
|
||||
|
||||
|
||||
/* 分配/设置/注册irq_domain */
|
||||
virtual_intc_domain = irq_domain_add_hierarchy(parent_domain, 0, 4,
|
||||
pdev->dev.of_node, &virtual_intc_domain_ops,
|
||||
NULL);
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
static int virtual_intc_remove(struct platform_device *pdev)
|
||||
{
|
||||
printk("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static const struct of_device_id virtual_intc_of_match[] = {
|
||||
{ .compatible = "100ask,virtual_intc", },
|
||||
{ },
|
||||
};
|
||||
|
||||
|
||||
static struct platform_driver virtual_intc_driver = {
|
||||
.probe = virtual_intc_probe,
|
||||
.remove = virtual_intc_remove,
|
||||
.driver = {
|
||||
.name = "100ask_virtual_intc",
|
||||
.of_match_table = of_match_ptr(virtual_intc_of_match),
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/* 1. 入口函数 */
|
||||
static int __init virtual_intc_init(void)
|
||||
{
|
||||
printk("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);
|
||||
/* 1.1 注册一个platform_driver */
|
||||
return platform_driver_register(&virtual_intc_driver);
|
||||
}
|
||||
|
||||
|
||||
/* 2. 出口函数 */
|
||||
static void __exit virtual_intc_exit(void)
|
||||
{
|
||||
printk("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);
|
||||
/* 2.1 反注册platform_driver */
|
||||
platform_driver_unregister(&virtual_intc_driver);
|
||||
}
|
||||
|
||||
module_init(virtual_intc_init);
|
||||
module_exit(virtual_intc_exit);
|
||||
|
||||
MODULE_LICENSE("GPL");
|
||||
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
|
||||
#define m 123
|
||||
|
||||
/{
|
||||
virtual_intc: virtual_intc_100ask {
|
||||
compatible = "100ask,virtual_intc";
|
||||
|
||||
interrupt-controller;
|
||||
#interrupt-cells = <2>;
|
||||
|
||||
interrupt-parent = <&intc>;
|
||||
upper_hwirq_base = <m>;
|
||||
};
|
||||
|
||||
gpio_keys_100ask {
|
||||
compatible = "100ask,gpio_key";
|
||||
interrupt-parent = <&virtual_intc>;
|
||||
interrupts = <0 IRQ_TYPE_LEVEL_HIGH>,
|
||||
<1 IRQ_TYPE_LEVEL_HIGH>,
|
||||
<2 IRQ_TYPE_LEVEL_HIGH>,
|
||||
<3 IRQ_TYPE_LEVEL_HIGH>;
|
||||
};
|
||||
|
||||
};
|
||||
@@ -0,0 +1,21 @@
|
||||
|
||||
# 1. 使用不同的开发板内核时, 一定要修改KERN_DIR
|
||||
# 2. KERN_DIR中的内核要事先配置、编译, 为了能编译内核, 要先设置下列环境变量:
|
||||
# 2.1 ARCH, 比如: export ARCH=arm64
|
||||
# 2.2 CROSS_COMPILE, 比如: export CROSS_COMPILE=aarch64-linux-gnu-
|
||||
# 2.3 PATH, 比如: export PATH=$PATH:/home/book/100ask_roc-rk3399-pc/ToolChain-6.3.1/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin
|
||||
# 注意: 不同的开发板不同的编译器上述3个环境变量不一定相同,
|
||||
# 请参考各开发板的高级用户使用手册
|
||||
|
||||
KERN_DIR = /home/book/100ask_imx6ull-sdk/Linux-4.9.88
|
||||
|
||||
all:
|
||||
make -C $(KERN_DIR) M=`pwd` modules
|
||||
|
||||
clean:
|
||||
make -C $(KERN_DIR) M=`pwd` modules clean
|
||||
rm -rf modules.order
|
||||
|
||||
obj-m += virtual_int_controller.o
|
||||
obj-m += gpio_key_drv.o
|
||||
|
||||
@@ -0,0 +1,125 @@
|
||||
#include <linux/module.h>
|
||||
|
||||
#include <linux/fs.h>
|
||||
#include <linux/errno.h>
|
||||
#include <linux/miscdevice.h>
|
||||
#include <linux/kernel.h>
|
||||
#include <linux/major.h>
|
||||
#include <linux/mutex.h>
|
||||
#include <linux/proc_fs.h>
|
||||
#include <linux/seq_file.h>
|
||||
#include <linux/stat.h>
|
||||
#include <linux/init.h>
|
||||
#include <linux/device.h>
|
||||
#include <linux/tty.h>
|
||||
#include <linux/kmod.h>
|
||||
#include <linux/gfp.h>
|
||||
#include <linux/gpio/consumer.h>
|
||||
#include <linux/platform_device.h>
|
||||
#include <linux/of_gpio.h>
|
||||
#include <linux/of_irq.h>
|
||||
#include <linux/interrupt.h>
|
||||
#include <linux/irq.h>
|
||||
#include <linux/slab.h>
|
||||
|
||||
|
||||
struct gpio_key{
|
||||
char name[100];
|
||||
int irq;
|
||||
int cnt;
|
||||
} ;
|
||||
|
||||
static struct gpio_key gpio_keys_100ask[100];
|
||||
|
||||
static irqreturn_t gpio_key_isr(int irq, void *dev_id)
|
||||
{
|
||||
struct gpio_key *gpio_key = dev_id;
|
||||
|
||||
|
||||
printk("gpio_key_isr %s cnt %d\n", gpio_key->name, gpio_key->cnt++);
|
||||
|
||||
return IRQ_HANDLED;
|
||||
}
|
||||
|
||||
/* 1. 从platform_device获得GPIO
|
||||
* 2. gpio=>irq
|
||||
* 3. request_irq
|
||||
*/
|
||||
static int gpio_key_probe(struct platform_device *pdev)
|
||||
{
|
||||
int err;
|
||||
int i = 0;
|
||||
int irq;
|
||||
|
||||
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
|
||||
|
||||
|
||||
while (1)
|
||||
{
|
||||
irq = platform_get_irq(pdev, i);
|
||||
if (irq <= 0)
|
||||
break;
|
||||
gpio_keys_100ask[i].irq = irq;
|
||||
sprintf(gpio_keys_100ask[i].name, "100as_virtual_key%d", i);
|
||||
|
||||
err = devm_request_irq(&pdev->dev, gpio_keys_100ask[i].irq, gpio_key_isr, IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING, gpio_keys_100ask[i].name, &gpio_keys_100ask[i]);
|
||||
printk("devm_request_irq %d for %s, err = %d\n", irq, gpio_keys_100ask[i].name, err);
|
||||
i++;
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
}
|
||||
|
||||
static int gpio_key_remove(struct platform_device *pdev)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static const struct of_device_id ask100_keys[] = {
|
||||
{ .compatible = "100ask,gpio_key" },
|
||||
{ },
|
||||
};
|
||||
|
||||
/* 1. 定义platform_driver */
|
||||
static struct platform_driver gpio_keys_driver = {
|
||||
.probe = gpio_key_probe,
|
||||
.remove = gpio_key_remove,
|
||||
.driver = {
|
||||
.name = "100ask_gpio_key",
|
||||
.of_match_table = ask100_keys,
|
||||
},
|
||||
};
|
||||
|
||||
/* 2. 在入口函数注册platform_driver */
|
||||
static int __init gpio_key_init(void)
|
||||
{
|
||||
int err;
|
||||
|
||||
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
|
||||
|
||||
err = platform_driver_register(&gpio_keys_driver);
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
/* 3. 有入口函数就应该有出口函数:卸载驱动程序时,就会去调用这个出口函数
|
||||
* 卸载platform_driver
|
||||
*/
|
||||
static void __exit gpio_key_exit(void)
|
||||
{
|
||||
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
|
||||
|
||||
platform_driver_unregister(&gpio_keys_driver);
|
||||
}
|
||||
|
||||
|
||||
/* 7. 其他完善:提供设备信息,自动创建设备节点 */
|
||||
|
||||
module_init(gpio_key_init);
|
||||
module_exit(gpio_key_exit);
|
||||
|
||||
MODULE_LICENSE("GPL");
|
||||
|
||||
|
||||
@@ -0,0 +1,171 @@
|
||||
#include <linux/kernel.h>
|
||||
#include <linux/module.h>
|
||||
#include <linux/clk.h>
|
||||
#include <linux/err.h>
|
||||
#include <linux/init.h>
|
||||
#include <linux/interrupt.h>
|
||||
#include <linux/io.h>
|
||||
#include <linux/random.h>
|
||||
#include <linux/irq.h>
|
||||
#include <linux/irqdomain.h>
|
||||
#include <linux/irqchip/chained_irq.h>
|
||||
#include <linux/platform_device.h>
|
||||
#include <linux/pm_runtime.h>
|
||||
#include <linux/slab.h>
|
||||
#include <linux/gpio/driver.h>
|
||||
/* FIXME: for gpio_get_value() replace this with direct register read */
|
||||
#include <linux/gpio.h>
|
||||
#include <linux/of.h>
|
||||
#include <linux/of_irq.h>
|
||||
#include <linux/of_device.h>
|
||||
#include <linux/bug.h>
|
||||
|
||||
|
||||
static struct irq_domain *virtual_intc_domain;
|
||||
static u32 upper_hwirq_base;
|
||||
|
||||
static int virtual_intc_domain_translate(struct irq_domain *d,
|
||||
struct irq_fwspec *fwspec,
|
||||
unsigned long *hwirq,
|
||||
unsigned int *type)
|
||||
{
|
||||
if (is_of_node(fwspec->fwnode)) {
|
||||
if (fwspec->param_count != 2)
|
||||
return -EINVAL;
|
||||
|
||||
*hwirq = fwspec->param[0];
|
||||
*type = fwspec->param[1];
|
||||
return 0;
|
||||
}
|
||||
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
static void virtual_intc_irq_unmask(struct irq_data *d)
|
||||
{
|
||||
printk("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);
|
||||
irq_chip_unmask_parent(d);
|
||||
}
|
||||
|
||||
static void virtual_intc_irq_mask(struct irq_data *d)
|
||||
{
|
||||
printk("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);
|
||||
irq_chip_mask_parent(d);
|
||||
}
|
||||
|
||||
static void virtual_intc_irq_eoi(struct irq_data *d)
|
||||
{
|
||||
printk("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);
|
||||
irq_chip_eoi_parent(d);
|
||||
}
|
||||
|
||||
static struct irq_chip virtual_intc_chip = {
|
||||
.name = "virtual_intc",
|
||||
.irq_mask = virtual_intc_irq_mask,
|
||||
.irq_unmask = virtual_intc_irq_unmask,
|
||||
.irq_eoi = virtual_intc_irq_eoi,
|
||||
};
|
||||
|
||||
|
||||
static int virtual_intc_domain_alloc(struct irq_domain *domain,
|
||||
unsigned int irq,
|
||||
unsigned int nr_irqs, void *data)
|
||||
{
|
||||
struct irq_fwspec *fwspec = data;
|
||||
struct irq_fwspec parent_fwspec;
|
||||
irq_hw_number_t hwirq;
|
||||
int i;
|
||||
|
||||
/* 设置irq_desc[irq] */
|
||||
|
||||
/* 1. 设置irq_desc[irq].irq_data, 里面含有virtual_intc irq_chip */
|
||||
hwirq = fwspec->param[0];
|
||||
for (i = 0; i < nr_irqs; i++)
|
||||
irq_domain_set_hwirq_and_chip(domain, irq + i, hwirq + i,
|
||||
&virtual_intc_chip, NULL);
|
||||
|
||||
/* 2. 设置irq_desc[irq].handle_irq, 来自GIC */
|
||||
parent_fwspec.fwnode = domain->parent->fwnode;
|
||||
parent_fwspec.param_count = 3;
|
||||
parent_fwspec.param[0] = 0; //GIC_SPI;
|
||||
parent_fwspec.param[1] = fwspec->param[0] + upper_hwirq_base;
|
||||
parent_fwspec.param[2] = fwspec->param[1];
|
||||
|
||||
return irq_domain_alloc_irqs_parent(domain, irq, nr_irqs,
|
||||
&parent_fwspec);
|
||||
}
|
||||
|
||||
static const struct irq_domain_ops virtual_intc_domain_ops = {
|
||||
.translate = virtual_intc_domain_translate,
|
||||
.alloc = virtual_intc_domain_alloc,
|
||||
};
|
||||
|
||||
static int virtual_intc_probe(struct platform_device *pdev)
|
||||
{
|
||||
|
||||
struct irq_domain *parent_domain;
|
||||
struct device_node *parent;
|
||||
int err;
|
||||
|
||||
|
||||
printk("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);
|
||||
err = of_property_read_u32(pdev->dev.of_node, "upper_hwirq_base", &upper_hwirq_base);
|
||||
|
||||
parent = of_irq_find_parent(pdev->dev.of_node);
|
||||
parent_domain = irq_find_host(parent);
|
||||
|
||||
|
||||
/* 分配/设置/注册irq_domain */
|
||||
virtual_intc_domain = irq_domain_add_hierarchy(parent_domain, 0, 4,
|
||||
pdev->dev.of_node, &virtual_intc_domain_ops,
|
||||
NULL);
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
static int virtual_intc_remove(struct platform_device *pdev)
|
||||
{
|
||||
printk("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static const struct of_device_id virtual_intc_of_match[] = {
|
||||
{ .compatible = "100ask,virtual_intc", },
|
||||
{ },
|
||||
};
|
||||
|
||||
|
||||
static struct platform_driver virtual_intc_driver = {
|
||||
.probe = virtual_intc_probe,
|
||||
.remove = virtual_intc_remove,
|
||||
.driver = {
|
||||
.name = "100ask_virtual_intc",
|
||||
.of_match_table = of_match_ptr(virtual_intc_of_match),
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/* 1. 入口函数 */
|
||||
static int __init virtual_intc_init(void)
|
||||
{
|
||||
printk("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);
|
||||
/* 1.1 注册一个platform_driver */
|
||||
return platform_driver_register(&virtual_intc_driver);
|
||||
}
|
||||
|
||||
|
||||
/* 2. 出口函数 */
|
||||
static void __exit virtual_intc_exit(void)
|
||||
{
|
||||
printk("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);
|
||||
/* 2.1 反注册platform_driver */
|
||||
platform_driver_unregister(&virtual_intc_driver);
|
||||
}
|
||||
|
||||
module_init(virtual_intc_init);
|
||||
module_exit(virtual_intc_exit);
|
||||
|
||||
MODULE_LICENSE("GPL");
|
||||
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
|
||||
#define m 123
|
||||
|
||||
/{
|
||||
virtual_intc: virtual_intc_100ask {
|
||||
compatible = "100ask,virtual_intc";
|
||||
|
||||
interrupt-controller;
|
||||
#interrupt-cells = <2>;
|
||||
|
||||
interrupt-parent = <&intc>;
|
||||
upper_hwirq_base = <122>; // imx6ull
|
||||
//upper_hwirq_base = <210>; // stm32mp157
|
||||
};
|
||||
|
||||
gpio_keys_100ask {
|
||||
compatible = "100ask,gpio_key";
|
||||
interrupt-parent = <&virtual_intc>;
|
||||
interrupts = <0 IRQ_TYPE_LEVEL_HIGH>,
|
||||
<1 IRQ_TYPE_LEVEL_HIGH>,
|
||||
<2 IRQ_TYPE_LEVEL_HIGH>,
|
||||
<3 IRQ_TYPE_LEVEL_HIGH>;
|
||||
};
|
||||
|
||||
};
|
||||
@@ -303,6 +303,8 @@ git clone https://e.coding.net/weidongshan/linux/doc_and_source_for_drivers.git
|
||||
|
||||
```shell
|
||||
17_链式中断控制器驱动程序编写_linear方式
|
||||
18_层级中断控制器驱动程序编写
|
||||
19_层级中断控制器驱动程序上机实验
|
||||
```
|
||||
|
||||
|
||||
|
||||
@@ -256,7 +256,7 @@ GICD_ISPENDRn有多个寄存器,每个寄存器中每一位对应一个GIC中
|
||||
* 编译:
|
||||
|
||||
* 在Ubuntu上
|
||||
* 修改`003_virtual_int_controller_legacy`中的Makefile,指定内核路径`KERN_DIR`,在执行`make`命令即可。
|
||||
* 修改`04_virtual_int_controller_legacy_ok`中的Makefile,指定内核路径`KERN_DIR`,在执行`make`命令即可。
|
||||
|
||||
* 安装:
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
## 层级中断控制器驱动程序编写与实验
|
||||
## 层级中断控制器驱动程序编写
|
||||
|
||||
参考资料:
|
||||
|
||||
@@ -20,11 +20,11 @@
|
||||
```shell
|
||||
doc_and_source_for_drivers\
|
||||
IMX6ULL\source\08_Interrupt\
|
||||
06_virtual_int_controller_hierarchy_ok
|
||||
06_virtual_int_controller_hierarchy
|
||||
|
||||
doc_and_source_for_drivers\
|
||||
STM32MP157\source\A7\08_Interrupt\
|
||||
06_virtual_int_controller_hierarchy_ok
|
||||
06_virtual_int_controller_hierarchy
|
||||
```
|
||||
|
||||
|
||||
@@ -124,5 +124,15 @@
|
||||
|
||||
会涉及2个驱动程序:虚拟的中断控制器驱动程序,按键驱动程序,以及对应的设备树。
|
||||
|
||||
虚拟的中断控制器驱动程序中,涉及2个递归处理。
|
||||
|
||||
#### 3.1 alloc的递归处理
|
||||
|
||||

|
||||
|
||||
|
||||
|
||||
#### 3.2 irq_chip的递归处理
|
||||
|
||||

|
||||
|
||||
BIN
STM32MP157/doc_pic/08_Interrupt/18_层级中断控制器驱动程序编写.tif
Normal file
BIN
STM32MP157/doc_pic/08_Interrupt/18_层级中断控制器驱动程序编写.tif
Normal file
Binary file not shown.
299
STM32MP157/doc_pic/08_Interrupt/19_层级中断控制器驱动程序上机实验.md
Normal file
299
STM32MP157/doc_pic/08_Interrupt/19_层级中断控制器驱动程序上机实验.md
Normal file
@@ -0,0 +1,299 @@
|
||||
## 层级中断控制器驱动程序上机实验
|
||||
|
||||
参考资料:
|
||||
|
||||
* [linux kernel的中断子系统之(七):GIC代码分析](http://www.wowotech.net/irq_subsystem/gic_driver.html)
|
||||
|
||||
* Linux 4.9.88内核源码
|
||||
|
||||
* `Linux-4.9.88\drivers\gpio\gpio-mxc.c`
|
||||
* `Linux-4.9.88\arch\arm\boot\dts\imx6ull.dtsi`
|
||||
|
||||
* Linux 5.4内核源码
|
||||
|
||||
* `Linux-5.4\drivers\pinctrl\stm32\pinctrl-stm32mp157.c`
|
||||
* `Linux-5.4\drivers\irqchip\irq-stm32-exti.c`
|
||||
* `Linux-5.4\arch\arm\boot\dts\stm32mp151.dtsi`
|
||||
|
||||
* 芯片手册
|
||||
|
||||
* IMX6ULL: imx6ullrm.pdf
|
||||
* STM32MP157: DM00327659.pdf
|
||||
|
||||
* 本节视频源码在GIT仓库里
|
||||
|
||||
```shell
|
||||
doc_and_source_for_drivers\
|
||||
IMX6ULL\source\08_Interrupt\
|
||||
07_virtual_int_controller_hierarchy_ok
|
||||
|
||||
doc_and_source_for_drivers\
|
||||
STM32MP157\source\A7\08_Interrupt\
|
||||
07_virtual_int_controller_hierarchy_ok
|
||||
```
|
||||
|
||||
|
||||
|
||||
### 1. 确定中断号n
|
||||
|
||||

|
||||
|
||||
查看芯片手册,选择一个保留的、未使用的GIC SPI中断即可。
|
||||
|
||||
#### 1.1 IMX6ULL
|
||||
|
||||
看芯片手册第3章:
|
||||
|
||||

|
||||
|
||||
看上图,选择122号中断,它是SPI里的122号中断,GIC里的编号是(32+122)=154。
|
||||
|
||||
|
||||
|
||||
#### 1.2 STM32MP157
|
||||
|
||||
看芯片手册第21.2节:
|
||||
|
||||

|
||||
|
||||
看上图,选择210号中断,它是SPI里的210号中断,GIC里的编号是(32+210)=242。
|
||||
|
||||
### 2. 怎么触发中断
|
||||
|
||||
可以通过devmem命令直接写GIC的PENDING寄存区。
|
||||
|
||||

|
||||
|
||||
GICD_ISPENDRn有多个寄存器,每个寄存器中每一位对应一个GIC中断,写入1就可以触发该中断。
|
||||
|
||||
写哪一个GICD_ISPENDRn寄存器?写哪一位?使用下列公式来确定:
|
||||
|
||||

|
||||
|
||||
查看内核设备树文件imx6ull.dtsi、stm32mp151.dtsi,可以知道:
|
||||
|
||||
* IMX6ULL的GIC Distributor 地址是:0x00a01000
|
||||

|
||||
* STM32MP157的GIC Distributor 地址是:0xa0021000
|
||||

|
||||
|
||||
| 芯片 | SPI中断号 | GIC中断号 | n,bit | GICD_ISPENDRn地址 | 命令 |
|
||||
| ---------- | --------- | --------- | ----- | ----------------- | ----------------------------- |
|
||||
| IMX6LLL | 122 | 154 | 4,26 | 0xa01210 | devmem 0xa01210 32 0x4000000 |
|
||||
| | 123 | 155 | 4,27 | 0xa01210 | devmem 0xa01210 32 0x8000000 |
|
||||
| | 124 | 156 | 4,28 | 0xa01210 | devmem 0xa01210 32 0x10000000 |
|
||||
| | 125 | 157 | 4,29 | 0xa01210 | devmem 0xa01210 32 0x20000000 |
|
||||
| STM32MP157 | 210 | 242 | 7,18 | 0xa002121c | devmem 0xa002121c 32 0x40000 |
|
||||
| | 211 | 243 | 7,19 | 0xa002121c | devmem 0xa002121c 32 0x80000 |
|
||||
| | 212 | 244 | 7,20 | 0xa002121c | devmem 0xa002121c 32 0x100000 |
|
||||
| | 213 | 245 | 7,21 | 0xa002121c | devmem 0xa002121c 32 0x200000 |
|
||||
|
||||
|
||||
|
||||
### 3. 上机实验
|
||||
|
||||
#### 3.1 设置工具链
|
||||
|
||||
##### 1. STM32MP157
|
||||
|
||||
```shell
|
||||
export ARCH=arm
|
||||
export CROSS_COMPILE=arm-buildroot-linux-gnueabihf-
|
||||
export PATH=$PATH:/home/book/100ask_stm32mp157_pro-sdk/ToolChain/arm-buildroot-linux-gnueabihf_sdk-buildroot/bin
|
||||
```
|
||||
|
||||
##### 2. IMX6ULL
|
||||
|
||||
```shell
|
||||
export ARCH=arm
|
||||
export CROSS_COMPILE=arm-linux-gnueabihf-
|
||||
export PATH=$PATH:/home/book/100ask_imx6ull-sdk/ToolChain/gcc-linaro-6.2.1-2016.11-x86_64_arm-linux-gnueabihf/bin
|
||||
```
|
||||
|
||||
|
||||
|
||||
#### 3.2 编译、替换设备树
|
||||
|
||||
##### 1. STM32MP157
|
||||
|
||||
* 修改`arch/arm/boot/dts/stm32mp157c-100ask-512d-lcd-v1.dts`,添加如下代码:
|
||||
|
||||
```shell
|
||||
/ {
|
||||
virtual_intc: virtual_intc_100ask {
|
||||
compatible = "100ask,virtual_intc";
|
||||
|
||||
interrupt-controller;
|
||||
#interrupt-cells = <2>;
|
||||
|
||||
interrupt-parent = <&intc>;
|
||||
//upper_hwirq_base = <122>; // imx6ull
|
||||
upper_hwirq_base = <210>; // stm32mp157
|
||||
};
|
||||
|
||||
gpio_keys_100ask {
|
||||
compatible = "100ask,gpio_key";
|
||||
interrupt-parent = <&virtual_intc>;
|
||||
interrupts = <0 IRQ_TYPE_LEVEL_HIGH>,
|
||||
<1 IRQ_TYPE_LEVEL_HIGH>,
|
||||
<2 IRQ_TYPE_LEVEL_HIGH>,
|
||||
<3 IRQ_TYPE_LEVEL_HIGH>;
|
||||
};
|
||||
};
|
||||
```
|
||||
|
||||
|
||||
|
||||
* 编译设备树:
|
||||
在Ubuntu的STM32MP157内核目录下执行如下命令,
|
||||
得到设备树文件:`arch/arm/boot/dts/stm32mp157c-100ask-512d-lcd-v1.dtb`
|
||||
|
||||
```shell
|
||||
make dtbs
|
||||
```
|
||||
|
||||
* 复制到NFS目录:
|
||||
|
||||
```shell
|
||||
$ cp arch/arm/boot/dts/stm32mp157c-100ask-512d-lcd-v1.dtb ~/nfs_rootfs/
|
||||
```
|
||||
|
||||
* 开发板上挂载NFS文件系统
|
||||
|
||||
* vmware使用NAT(假设windowsIP为192.168.1.100)
|
||||
|
||||
```shell
|
||||
[root@100ask:~]# mount -t nfs -o nolock,vers=3,port=2049,mountport=9999
|
||||
192.168.1.100:/home/book/nfs_rootfs /mnt
|
||||
```
|
||||
|
||||
* vmware使用桥接,或者不使用vmware而是直接使用服务器:假设Ubuntu IP为192.168.1.137
|
||||
|
||||
```shell
|
||||
[root@100ask:~]# mount -t nfs -o nolock,vers=3 192.168.1.137:/home/book/nfs_rootfs /mnt
|
||||
```
|
||||
|
||||
* 更新设备树
|
||||
|
||||
```shell
|
||||
[root@100ask:~]# mount /dev/mmcblk2p2 /boot
|
||||
[root@100ask:~]# cp /mnt/stm32mp157c-100ask-512d-lcd-v1.dtb /boot
|
||||
[root@100ask:~]# sync
|
||||
```
|
||||
|
||||
* 重启开发板
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
##### 2. IMX6ULL
|
||||
|
||||
* 修改`arch/arm/boot/dts/100ask_imx6ull-14x14.dts`,添加如下代码:
|
||||
|
||||
```shell
|
||||
/ {
|
||||
virtual_intc: virtual_intc_100ask {
|
||||
compatible = "100ask,virtual_intc";
|
||||
|
||||
interrupt-controller;
|
||||
#interrupt-cells = <2>;
|
||||
|
||||
interrupt-parent = <&intc>;
|
||||
upper_hwirq_base = <122>; // imx6ull
|
||||
//upper_hwirq_base = <210>; // stm32mp157
|
||||
};
|
||||
|
||||
gpio_keys_100ask {
|
||||
compatible = "100ask,gpio_key";
|
||||
interrupt-parent = <&virtual_intc>;
|
||||
interrupts = <0 IRQ_TYPE_LEVEL_HIGH>,
|
||||
<1 IRQ_TYPE_LEVEL_HIGH>,
|
||||
<2 IRQ_TYPE_LEVEL_HIGH>,
|
||||
<3 IRQ_TYPE_LEVEL_HIGH>;
|
||||
};
|
||||
|
||||
};
|
||||
```
|
||||
|
||||
|
||||
|
||||
* 编译设备树:
|
||||
在Ubuntu的IMX6ULL内核目录下执行如下命令,
|
||||
得到设备树文件:`arch/arm/boot/dts/100ask_imx6ull-14x14.dtb`
|
||||
|
||||
```shell
|
||||
make dtbs
|
||||
```
|
||||
|
||||
* 复制到NFS目录:
|
||||
|
||||
```shell
|
||||
$ cp arch/arm/boot/dts/100ask_imx6ull-14x14.dtb ~/nfs_rootfs/
|
||||
```
|
||||
|
||||
* 开发板上挂载NFS文件系统
|
||||
|
||||
* vmware使用NAT(假设windowsIP为192.168.1.100)
|
||||
|
||||
```shell
|
||||
[root@100ask:~]# mount -t nfs -o nolock,vers=3,port=2049,mountport=9999
|
||||
192.168.1.100:/home/book/nfs_rootfs /mnt
|
||||
```
|
||||
|
||||
* vmware使用桥接,或者不使用vmware而是直接使用服务器:假设Ubuntu IP为192.168.1.137
|
||||
|
||||
```shell
|
||||
[root@100ask:~]# mount -t nfs -o nolock,vers=3 192.168.1.137:/home/book/nfs_rootfs /mnt
|
||||
```
|
||||
|
||||
* 更新设备树
|
||||
|
||||
```shell
|
||||
[root@100ask:~]# cp /mnt/100ask_imx6ull-14x14.dtb /boot
|
||||
[root@100ask:~]# sync
|
||||
```
|
||||
|
||||
* 重启开发板
|
||||
|
||||
|
||||
|
||||
#### 3.3 编译、安装驱动程序
|
||||
|
||||
* 编译:
|
||||
|
||||
* 在Ubuntu上
|
||||
* 修改`07_virtual_int_controller_hierarchy_ok`中的Makefile,指定内核路径`KERN_DIR`,在执行`make`命令即可。
|
||||
|
||||
* 安装:
|
||||
|
||||
* 在开发板上
|
||||
|
||||
* 挂载NFS,复制文件,insmod,类似如下命令:
|
||||
|
||||
```shell
|
||||
mount -t nfs -o nolock,vers=3 192.168.1.137:/home/book/nfs_rootfs /mnt
|
||||
// 对于IMX6ULL,想看到驱动打印信息,需要先执行
|
||||
echo "7 4 1 7" > /proc/sys/kernel/printk
|
||||
|
||||
insmod -f /mnt/virtual_int_controller.ko
|
||||
// 安装virtual_int_controller之后即可进入/sys/kernel/irq目录查看分配的中断号
|
||||
|
||||
insmod -f /mnt/gpio_key_drv.ko
|
||||
cat /proc/interrupts
|
||||
|
||||
// 触发中断
|
||||
// 对于IMX6ULL
|
||||
devmem 0xa01210 32 0x4000000
|
||||
devmem 0xa01210 32 0x8000000
|
||||
devmem 0xa01210 32 0x10000000
|
||||
devmem 0xa01210 32 0x20000000
|
||||
|
||||
// 对于stm32mp157
|
||||
devmem 0xa002121c 32 0x40000
|
||||
devmem 0xa002121c 32 0x80000
|
||||
devmem 0xa002121c 32 0x100000 // 它不能触发中断,可能是被占用了
|
||||
devmem 0xa002121c 32 0x200000
|
||||
```
|
||||
|
||||
* 观察内核打印的信息
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 28 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 27 KiB |
@@ -0,0 +1,21 @@
|
||||
|
||||
# 1. 使用不同的开发板内核时, 一定要修改KERN_DIR
|
||||
# 2. KERN_DIR中的内核要事先配置、编译, 为了能编译内核, 要先设置下列环境变量:
|
||||
# 2.1 ARCH, 比如: export ARCH=arm64
|
||||
# 2.2 CROSS_COMPILE, 比如: export CROSS_COMPILE=aarch64-linux-gnu-
|
||||
# 2.3 PATH, 比如: export PATH=$PATH:/home/book/100ask_roc-rk3399-pc/ToolChain-6.3.1/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin
|
||||
# 注意: 不同的开发板不同的编译器上述3个环境变量不一定相同,
|
||||
# 请参考各开发板的高级用户使用手册
|
||||
|
||||
KERN_DIR = /home/book/100ask_stm32mp157_pro-sdk/Linux-5.4
|
||||
|
||||
all:
|
||||
make -C $(KERN_DIR) M=`pwd` modules
|
||||
|
||||
clean:
|
||||
make -C $(KERN_DIR) M=`pwd` modules clean
|
||||
rm -rf modules.order
|
||||
|
||||
obj-m += virtual_int_controller.o
|
||||
obj-m += gpio_key_drv.o
|
||||
|
||||
@@ -0,0 +1,125 @@
|
||||
#include <linux/module.h>
|
||||
|
||||
#include <linux/fs.h>
|
||||
#include <linux/errno.h>
|
||||
#include <linux/miscdevice.h>
|
||||
#include <linux/kernel.h>
|
||||
#include <linux/major.h>
|
||||
#include <linux/mutex.h>
|
||||
#include <linux/proc_fs.h>
|
||||
#include <linux/seq_file.h>
|
||||
#include <linux/stat.h>
|
||||
#include <linux/init.h>
|
||||
#include <linux/device.h>
|
||||
#include <linux/tty.h>
|
||||
#include <linux/kmod.h>
|
||||
#include <linux/gfp.h>
|
||||
#include <linux/gpio/consumer.h>
|
||||
#include <linux/platform_device.h>
|
||||
#include <linux/of_gpio.h>
|
||||
#include <linux/of_irq.h>
|
||||
#include <linux/interrupt.h>
|
||||
#include <linux/irq.h>
|
||||
#include <linux/slab.h>
|
||||
|
||||
|
||||
struct gpio_key{
|
||||
char name[100];
|
||||
int irq;
|
||||
int cnt;
|
||||
} ;
|
||||
|
||||
static struct gpio_key gpio_keys_100ask[100];
|
||||
|
||||
static irqreturn_t gpio_key_isr(int irq, void *dev_id)
|
||||
{
|
||||
struct gpio_key *gpio_key = dev_id;
|
||||
|
||||
|
||||
printk("gpio_key_isr %s cnt %d\n", gpio_key->name, gpio_key->cnt++);
|
||||
|
||||
return IRQ_HANDLED;
|
||||
}
|
||||
|
||||
/* 1. 从platform_device获得GPIO
|
||||
* 2. gpio=>irq
|
||||
* 3. request_irq
|
||||
*/
|
||||
static int gpio_key_probe(struct platform_device *pdev)
|
||||
{
|
||||
int err;
|
||||
int i = 0;
|
||||
int irq;
|
||||
|
||||
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
|
||||
|
||||
|
||||
while (1)
|
||||
{
|
||||
irq = platform_get_irq(pdev, i);
|
||||
if (irq <= 0)
|
||||
break;
|
||||
gpio_keys_100ask[i].irq = irq;
|
||||
sprintf(gpio_keys_100ask[i].name, "100as_virtual_key%d", i);
|
||||
|
||||
err = devm_request_irq(&pdev->dev, gpio_keys_100ask[i].irq, gpio_key_isr, IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING, gpio_keys_100ask[i].name, &gpio_keys_100ask[i]);
|
||||
printk("devm_request_irq %d for %s, err = %d\n", irq, gpio_keys_100ask[i].name, err);
|
||||
i++;
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
}
|
||||
|
||||
static int gpio_key_remove(struct platform_device *pdev)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static const struct of_device_id ask100_keys[] = {
|
||||
{ .compatible = "100ask,gpio_key" },
|
||||
{ },
|
||||
};
|
||||
|
||||
/* 1. 定义platform_driver */
|
||||
static struct platform_driver gpio_keys_driver = {
|
||||
.probe = gpio_key_probe,
|
||||
.remove = gpio_key_remove,
|
||||
.driver = {
|
||||
.name = "100ask_gpio_key",
|
||||
.of_match_table = ask100_keys,
|
||||
},
|
||||
};
|
||||
|
||||
/* 2. 在入口函数注册platform_driver */
|
||||
static int __init gpio_key_init(void)
|
||||
{
|
||||
int err;
|
||||
|
||||
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
|
||||
|
||||
err = platform_driver_register(&gpio_keys_driver);
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
/* 3. 有入口函数就应该有出口函数:卸载驱动程序时,就会去调用这个出口函数
|
||||
* 卸载platform_driver
|
||||
*/
|
||||
static void __exit gpio_key_exit(void)
|
||||
{
|
||||
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
|
||||
|
||||
platform_driver_unregister(&gpio_keys_driver);
|
||||
}
|
||||
|
||||
|
||||
/* 7. 其他完善:提供设备信息,自动创建设备节点 */
|
||||
|
||||
module_init(gpio_key_init);
|
||||
module_exit(gpio_key_exit);
|
||||
|
||||
MODULE_LICENSE("GPL");
|
||||
|
||||
|
||||
@@ -0,0 +1,163 @@
|
||||
#include <linux/kernel.h>
|
||||
#include <linux/module.h>
|
||||
#include <linux/clk.h>
|
||||
#include <linux/err.h>
|
||||
#include <linux/init.h>
|
||||
#include <linux/interrupt.h>
|
||||
#include <linux/io.h>
|
||||
#include <linux/random.h>
|
||||
#include <linux/irq.h>
|
||||
#include <linux/irqdomain.h>
|
||||
#include <linux/irqchip/chained_irq.h>
|
||||
#include <linux/platform_device.h>
|
||||
#include <linux/pm_runtime.h>
|
||||
#include <linux/slab.h>
|
||||
#include <linux/gpio/driver.h>
|
||||
/* FIXME: for gpio_get_value() replace this with direct register read */
|
||||
#include <linux/gpio.h>
|
||||
#include <linux/of.h>
|
||||
#include <linux/of_device.h>
|
||||
#include <linux/bug.h>
|
||||
|
||||
|
||||
static struct irq_domain *virtual_intc_domain;
|
||||
static u32 upper_hwirq_base;
|
||||
|
||||
static int virtual_intc_domain_translate(struct irq_domain *d,
|
||||
struct irq_fwspec *fwspec,
|
||||
unsigned long *hwirq,
|
||||
unsigned int *type)
|
||||
{
|
||||
if (is_of_node(fwspec->fwnode)) {
|
||||
if (fwspec->param_count != 2)
|
||||
return -EINVAL;
|
||||
|
||||
*hwirq = fwspec->param[0];
|
||||
*type = fwspec->param[1];
|
||||
return 0;
|
||||
}
|
||||
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
static void virtual_intc_irq_unmask(struct irq_data *d)
|
||||
{
|
||||
printk("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);
|
||||
irq_chip_unmask_parent(d);
|
||||
}
|
||||
|
||||
static void virtual_intc_irq_mask(struct irq_data *d)
|
||||
{
|
||||
printk("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);
|
||||
irq_chip_mask_parent(d);
|
||||
}
|
||||
|
||||
static struct irq_chip virtual_intc_chip = {
|
||||
.name = "virtual_intc",
|
||||
.irq_mask = virtual_intc_irq_mask,
|
||||
.irq_unmask = virtual_intc_irq_unmask,
|
||||
};
|
||||
|
||||
|
||||
static int virtual_intc_domain_alloc(struct irq_domain *domain,
|
||||
unsigned int irq,
|
||||
unsigned int nr_irqs, void *data)
|
||||
{
|
||||
struct irq_fwspec *fwspec = data;
|
||||
struct irq_fwspec parent_fwspec;
|
||||
irq_hw_number_t hwirq;
|
||||
int i;
|
||||
|
||||
/* 设置irq_desc[irq] */
|
||||
|
||||
/* 1. 设置irq_desc[irq].irq_data, 里面含有virtual_intc irq_chip */
|
||||
hwirq = fwspec->param[0];
|
||||
for (i = 0; i < nr_irqs; i++)
|
||||
irq_domain_set_hwirq_and_chip(domain, irq + i, hwirq + i,
|
||||
&virtual_intc_chip, NULL);
|
||||
|
||||
/* 2. 设置irq_desc[irq].handle_irq, 来自GIC */
|
||||
parent_fwspec.fwnode = domain->parent->fwnode;
|
||||
parent_fwspec.param_count = 3;
|
||||
parent_fwspec.param[0] = GIC_SPI;
|
||||
parent_fwspec.param[1] = fwspec->param[0] + upper_hwirq_base;
|
||||
parent_fwspec.param[2] = fwspec->param[1];
|
||||
|
||||
return irq_domain_alloc_irqs_parent(domain, irq, nr_irqs,
|
||||
&parent_fwspec);
|
||||
}
|
||||
|
||||
static const struct irq_domain_ops virtual_intc_domain_ops = {
|
||||
.translate = virtual_intc_domain_translate,
|
||||
.alloc = virtual_intc_domain_alloc,
|
||||
};
|
||||
|
||||
static int virtual_intc_probe(struct platform_device *pdev)
|
||||
{
|
||||
|
||||
struct irq_domain *parent_domain;
|
||||
struct device_node *parent;
|
||||
int err;
|
||||
|
||||
|
||||
printk("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);
|
||||
err = of_property_read_u32(pdev->dev.of_node, "upper_hwirq_base", &upper_hwirq_base);
|
||||
|
||||
parent = of_irq_find_parent(pdev->dev.of_node);
|
||||
parent_domain = irq_find_host(parent);
|
||||
|
||||
|
||||
/* 分配/设置/注册irq_domain */
|
||||
virtual_intc_domain = irq_domain_add_hierarchy(parent_domain, 0, 4,
|
||||
pdev->dev.of_node, &virtual_intc_domain_ops,
|
||||
NULL);
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
static int virtual_intc_remove(struct platform_device *pdev)
|
||||
{
|
||||
printk("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static const struct of_device_id virtual_intc_of_match[] = {
|
||||
{ .compatible = "100ask,virtual_intc", },
|
||||
{ },
|
||||
};
|
||||
|
||||
|
||||
static struct platform_driver virtual_intc_driver = {
|
||||
.probe = virtual_intc_probe,
|
||||
.remove = virtual_intc_remove,
|
||||
.driver = {
|
||||
.name = "100ask_virtual_intc",
|
||||
.of_match_table = of_match_ptr(virtual_intc_of_match),
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/* 1. 入口函数 */
|
||||
static int __init virtual_intc_init(void)
|
||||
{
|
||||
printk("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);
|
||||
/* 1.1 注册一个platform_driver */
|
||||
return platform_driver_register(&virtual_intc_driver);
|
||||
}
|
||||
|
||||
|
||||
/* 2. 出口函数 */
|
||||
static void __exit virtual_intc_exit(void)
|
||||
{
|
||||
printk("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);
|
||||
/* 2.1 反注册platform_driver */
|
||||
platform_driver_unregister(&virtual_intc_driver);
|
||||
}
|
||||
|
||||
module_init(virtual_intc_init);
|
||||
module_exit(virtual_intc_exit);
|
||||
|
||||
MODULE_LICENSE("GPL");
|
||||
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
|
||||
#define m 123
|
||||
|
||||
/{
|
||||
virtual_intc: virtual_intc_100ask {
|
||||
compatible = "100ask,virtual_intc";
|
||||
|
||||
interrupt-controller;
|
||||
#interrupt-cells = <2>;
|
||||
|
||||
interrupt-parent = <&intc>;
|
||||
upper_hwirq_base = <m>;
|
||||
};
|
||||
|
||||
gpio_keys_100ask {
|
||||
compatible = "100ask,gpio_key";
|
||||
interrupt-parent = <&virtual_intc>;
|
||||
interrupts = <0 IRQ_TYPE_LEVEL_HIGH>,
|
||||
<1 IRQ_TYPE_LEVEL_HIGH>,
|
||||
<2 IRQ_TYPE_LEVEL_HIGH>,
|
||||
<3 IRQ_TYPE_LEVEL_HIGH>;
|
||||
};
|
||||
|
||||
};
|
||||
@@ -0,0 +1,21 @@
|
||||
|
||||
# 1. 使用不同的开发板内核时, 一定要修改KERN_DIR
|
||||
# 2. KERN_DIR中的内核要事先配置、编译, 为了能编译内核, 要先设置下列环境变量:
|
||||
# 2.1 ARCH, 比如: export ARCH=arm64
|
||||
# 2.2 CROSS_COMPILE, 比如: export CROSS_COMPILE=aarch64-linux-gnu-
|
||||
# 2.3 PATH, 比如: export PATH=$PATH:/home/book/100ask_roc-rk3399-pc/ToolChain-6.3.1/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin
|
||||
# 注意: 不同的开发板不同的编译器上述3个环境变量不一定相同,
|
||||
# 请参考各开发板的高级用户使用手册
|
||||
|
||||
KERN_DIR = /home/book/100ask_stm32mp157_pro-sdk/Linux-5.4
|
||||
|
||||
all:
|
||||
make -C $(KERN_DIR) M=`pwd` modules
|
||||
|
||||
clean:
|
||||
make -C $(KERN_DIR) M=`pwd` modules clean
|
||||
rm -rf modules.order
|
||||
|
||||
obj-m += virtual_int_controller.o
|
||||
obj-m += gpio_key_drv.o
|
||||
|
||||
@@ -0,0 +1,125 @@
|
||||
#include <linux/module.h>
|
||||
|
||||
#include <linux/fs.h>
|
||||
#include <linux/errno.h>
|
||||
#include <linux/miscdevice.h>
|
||||
#include <linux/kernel.h>
|
||||
#include <linux/major.h>
|
||||
#include <linux/mutex.h>
|
||||
#include <linux/proc_fs.h>
|
||||
#include <linux/seq_file.h>
|
||||
#include <linux/stat.h>
|
||||
#include <linux/init.h>
|
||||
#include <linux/device.h>
|
||||
#include <linux/tty.h>
|
||||
#include <linux/kmod.h>
|
||||
#include <linux/gfp.h>
|
||||
#include <linux/gpio/consumer.h>
|
||||
#include <linux/platform_device.h>
|
||||
#include <linux/of_gpio.h>
|
||||
#include <linux/of_irq.h>
|
||||
#include <linux/interrupt.h>
|
||||
#include <linux/irq.h>
|
||||
#include <linux/slab.h>
|
||||
|
||||
|
||||
struct gpio_key{
|
||||
char name[100];
|
||||
int irq;
|
||||
int cnt;
|
||||
} ;
|
||||
|
||||
static struct gpio_key gpio_keys_100ask[100];
|
||||
|
||||
static irqreturn_t gpio_key_isr(int irq, void *dev_id)
|
||||
{
|
||||
struct gpio_key *gpio_key = dev_id;
|
||||
|
||||
|
||||
printk("gpio_key_isr %s cnt %d\n", gpio_key->name, gpio_key->cnt++);
|
||||
|
||||
return IRQ_HANDLED;
|
||||
}
|
||||
|
||||
/* 1. 从platform_device获得GPIO
|
||||
* 2. gpio=>irq
|
||||
* 3. request_irq
|
||||
*/
|
||||
static int gpio_key_probe(struct platform_device *pdev)
|
||||
{
|
||||
int err;
|
||||
int i = 0;
|
||||
int irq;
|
||||
|
||||
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
|
||||
|
||||
|
||||
while (1)
|
||||
{
|
||||
irq = platform_get_irq(pdev, i);
|
||||
if (irq <= 0)
|
||||
break;
|
||||
gpio_keys_100ask[i].irq = irq;
|
||||
sprintf(gpio_keys_100ask[i].name, "100as_virtual_key%d", i);
|
||||
|
||||
err = devm_request_irq(&pdev->dev, gpio_keys_100ask[i].irq, gpio_key_isr, IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING, gpio_keys_100ask[i].name, &gpio_keys_100ask[i]);
|
||||
printk("devm_request_irq %d for %s, err = %d\n", irq, gpio_keys_100ask[i].name, err);
|
||||
i++;
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
}
|
||||
|
||||
static int gpio_key_remove(struct platform_device *pdev)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static const struct of_device_id ask100_keys[] = {
|
||||
{ .compatible = "100ask,gpio_key" },
|
||||
{ },
|
||||
};
|
||||
|
||||
/* 1. 定义platform_driver */
|
||||
static struct platform_driver gpio_keys_driver = {
|
||||
.probe = gpio_key_probe,
|
||||
.remove = gpio_key_remove,
|
||||
.driver = {
|
||||
.name = "100ask_gpio_key",
|
||||
.of_match_table = ask100_keys,
|
||||
},
|
||||
};
|
||||
|
||||
/* 2. 在入口函数注册platform_driver */
|
||||
static int __init gpio_key_init(void)
|
||||
{
|
||||
int err;
|
||||
|
||||
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
|
||||
|
||||
err = platform_driver_register(&gpio_keys_driver);
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
/* 3. 有入口函数就应该有出口函数:卸载驱动程序时,就会去调用这个出口函数
|
||||
* 卸载platform_driver
|
||||
*/
|
||||
static void __exit gpio_key_exit(void)
|
||||
{
|
||||
printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
|
||||
|
||||
platform_driver_unregister(&gpio_keys_driver);
|
||||
}
|
||||
|
||||
|
||||
/* 7. 其他完善:提供设备信息,自动创建设备节点 */
|
||||
|
||||
module_init(gpio_key_init);
|
||||
module_exit(gpio_key_exit);
|
||||
|
||||
MODULE_LICENSE("GPL");
|
||||
|
||||
|
||||
@@ -0,0 +1,171 @@
|
||||
#include <linux/kernel.h>
|
||||
#include <linux/module.h>
|
||||
#include <linux/clk.h>
|
||||
#include <linux/err.h>
|
||||
#include <linux/init.h>
|
||||
#include <linux/interrupt.h>
|
||||
#include <linux/io.h>
|
||||
#include <linux/random.h>
|
||||
#include <linux/irq.h>
|
||||
#include <linux/irqdomain.h>
|
||||
#include <linux/irqchip/chained_irq.h>
|
||||
#include <linux/platform_device.h>
|
||||
#include <linux/pm_runtime.h>
|
||||
#include <linux/slab.h>
|
||||
#include <linux/gpio/driver.h>
|
||||
/* FIXME: for gpio_get_value() replace this with direct register read */
|
||||
#include <linux/gpio.h>
|
||||
#include <linux/of.h>
|
||||
#include <linux/of_irq.h>
|
||||
#include <linux/of_device.h>
|
||||
#include <linux/bug.h>
|
||||
|
||||
|
||||
static struct irq_domain *virtual_intc_domain;
|
||||
static u32 upper_hwirq_base;
|
||||
|
||||
static int virtual_intc_domain_translate(struct irq_domain *d,
|
||||
struct irq_fwspec *fwspec,
|
||||
unsigned long *hwirq,
|
||||
unsigned int *type)
|
||||
{
|
||||
if (is_of_node(fwspec->fwnode)) {
|
||||
if (fwspec->param_count != 2)
|
||||
return -EINVAL;
|
||||
|
||||
*hwirq = fwspec->param[0];
|
||||
*type = fwspec->param[1];
|
||||
return 0;
|
||||
}
|
||||
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
static void virtual_intc_irq_unmask(struct irq_data *d)
|
||||
{
|
||||
printk("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);
|
||||
irq_chip_unmask_parent(d);
|
||||
}
|
||||
|
||||
static void virtual_intc_irq_mask(struct irq_data *d)
|
||||
{
|
||||
printk("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);
|
||||
irq_chip_mask_parent(d);
|
||||
}
|
||||
|
||||
static void virtual_intc_irq_eoi(struct irq_data *d)
|
||||
{
|
||||
printk("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);
|
||||
irq_chip_eoi_parent(d);
|
||||
}
|
||||
|
||||
static struct irq_chip virtual_intc_chip = {
|
||||
.name = "virtual_intc",
|
||||
.irq_mask = virtual_intc_irq_mask,
|
||||
.irq_unmask = virtual_intc_irq_unmask,
|
||||
.irq_eoi = virtual_intc_irq_eoi,
|
||||
};
|
||||
|
||||
|
||||
static int virtual_intc_domain_alloc(struct irq_domain *domain,
|
||||
unsigned int irq,
|
||||
unsigned int nr_irqs, void *data)
|
||||
{
|
||||
struct irq_fwspec *fwspec = data;
|
||||
struct irq_fwspec parent_fwspec;
|
||||
irq_hw_number_t hwirq;
|
||||
int i;
|
||||
|
||||
/* 设置irq_desc[irq] */
|
||||
|
||||
/* 1. 设置irq_desc[irq].irq_data, 里面含有virtual_intc irq_chip */
|
||||
hwirq = fwspec->param[0];
|
||||
for (i = 0; i < nr_irqs; i++)
|
||||
irq_domain_set_hwirq_and_chip(domain, irq + i, hwirq + i,
|
||||
&virtual_intc_chip, NULL);
|
||||
|
||||
/* 2. 设置irq_desc[irq].handle_irq, 来自GIC */
|
||||
parent_fwspec.fwnode = domain->parent->fwnode;
|
||||
parent_fwspec.param_count = 3;
|
||||
parent_fwspec.param[0] = 0; //GIC_SPI;
|
||||
parent_fwspec.param[1] = fwspec->param[0] + upper_hwirq_base;
|
||||
parent_fwspec.param[2] = fwspec->param[1];
|
||||
|
||||
return irq_domain_alloc_irqs_parent(domain, irq, nr_irqs,
|
||||
&parent_fwspec);
|
||||
}
|
||||
|
||||
static const struct irq_domain_ops virtual_intc_domain_ops = {
|
||||
.translate = virtual_intc_domain_translate,
|
||||
.alloc = virtual_intc_domain_alloc,
|
||||
};
|
||||
|
||||
static int virtual_intc_probe(struct platform_device *pdev)
|
||||
{
|
||||
|
||||
struct irq_domain *parent_domain;
|
||||
struct device_node *parent;
|
||||
int err;
|
||||
|
||||
|
||||
printk("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);
|
||||
err = of_property_read_u32(pdev->dev.of_node, "upper_hwirq_base", &upper_hwirq_base);
|
||||
|
||||
parent = of_irq_find_parent(pdev->dev.of_node);
|
||||
parent_domain = irq_find_host(parent);
|
||||
|
||||
|
||||
/* 分配/设置/注册irq_domain */
|
||||
virtual_intc_domain = irq_domain_add_hierarchy(parent_domain, 0, 4,
|
||||
pdev->dev.of_node, &virtual_intc_domain_ops,
|
||||
NULL);
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
static int virtual_intc_remove(struct platform_device *pdev)
|
||||
{
|
||||
printk("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static const struct of_device_id virtual_intc_of_match[] = {
|
||||
{ .compatible = "100ask,virtual_intc", },
|
||||
{ },
|
||||
};
|
||||
|
||||
|
||||
static struct platform_driver virtual_intc_driver = {
|
||||
.probe = virtual_intc_probe,
|
||||
.remove = virtual_intc_remove,
|
||||
.driver = {
|
||||
.name = "100ask_virtual_intc",
|
||||
.of_match_table = of_match_ptr(virtual_intc_of_match),
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/* 1. 入口函数 */
|
||||
static int __init virtual_intc_init(void)
|
||||
{
|
||||
printk("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);
|
||||
/* 1.1 注册一个platform_driver */
|
||||
return platform_driver_register(&virtual_intc_driver);
|
||||
}
|
||||
|
||||
|
||||
/* 2. 出口函数 */
|
||||
static void __exit virtual_intc_exit(void)
|
||||
{
|
||||
printk("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);
|
||||
/* 2.1 反注册platform_driver */
|
||||
platform_driver_unregister(&virtual_intc_driver);
|
||||
}
|
||||
|
||||
module_init(virtual_intc_init);
|
||||
module_exit(virtual_intc_exit);
|
||||
|
||||
MODULE_LICENSE("GPL");
|
||||
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
|
||||
#define m 123
|
||||
|
||||
/{
|
||||
virtual_intc: virtual_intc_100ask {
|
||||
compatible = "100ask,virtual_intc";
|
||||
|
||||
interrupt-controller;
|
||||
#interrupt-cells = <2>;
|
||||
|
||||
interrupt-parent = <&intc>;
|
||||
//upper_hwirq_base = <122>; // imx6ull
|
||||
upper_hwirq_base = <210>; // stm32mp157
|
||||
};
|
||||
|
||||
gpio_keys_100ask {
|
||||
compatible = "100ask,gpio_key";
|
||||
interrupt-parent = <&virtual_intc>;
|
||||
interrupts = <0 IRQ_TYPE_LEVEL_HIGH>,
|
||||
<1 IRQ_TYPE_LEVEL_HIGH>,
|
||||
<2 IRQ_TYPE_LEVEL_HIGH>,
|
||||
<3 IRQ_TYPE_LEVEL_HIGH>;
|
||||
};
|
||||
|
||||
};
|
||||
Reference in New Issue
Block a user