简体   繁体   English

无法读写(内存映射)硬件寄存器

[英]Can't read and write (memory mapped) hardware register

I am working on an LPC3131 (arm9 from NXP) and i am trying to make my very first driver in the linux kernel, but i have some trouble. 我正在研究LPC3131(来自NXP的arm9),并且试图在Linux内核中制作我的第一个驱动程序,但是我遇到了一些麻烦。

Indeed, i want to read/write value in the microcontroller register, and like i found there 确实,我想在微控制器寄存器中读取/写入值,就像我在那里找到的一样

Reading from 16-bit hardware registers 从16位硬件寄存器读取

i do the following thing in the init function of my driver 我在驱动程序的init函数中执行以下操作

static int parr_init(void)
{
        int result;
        dev_t my_dev;
        my_cdev = cdev_alloc();
        cdev_init(my_cdev, &parr_port_fops);

        printk(KERN_ALERT "parr_driver : Hello world \n");

        result = alloc_chrdev_region(&my_dev, 0, 0, "parr_driver");

        if(result < 0)
        {
                printk(KERN_WARNING "parr_driver : can't get a major number \n");
        }
        else
        {
                printk(KERN_ALERT "parr_driver : major number : %i \n", MAJOR(my_dev));
                printk(KERN_ALERT "parr_driver : minor number : %i \n", MINOR(my_dev));
        }

        result = cdev_add(my_cdev, my_dev, 1);

        if(result < 0)
        {
                printk(KERN_ALERT "parr_driver : failed to register driver \n");
        }
        else
        {
                printk(KERN_ALERT "parr_driver : driver is now ready to be use \n");
        }
                                uint32_t volatile* reg = (uint32_t volatile *) 0x13009000;

                                printk("reg adress %p \n", reg);

                                printk("reg value  %X \n", *reg);
        return 0;
}

The first printk works fine, it print the adress of my register. 第一个printk可以正常工作,它可以打印我的寄存器的地址。 But the second doesn't give the expected result, i got a segmentation fault. 但是第二个没有给出预期的结果,我遇到了分割错误。 You can find my console log below : 您可以在下面找到我的控制台日志:

/ # insmod home/parr_driver.ko 
parr_driver : Hello world 
parr_driver : major number : 254 
parr_driver : minor number : 0 
parr_driver : driver is now ready to be use 
reg adress 13009000 
Unable to handle kernel paging request at virtual address 13009000
pgd = c1798000
[13009000] *pgd=00000000
Internal error: Oops: 5 [#1]
last sysfs file: /sys/devices/platform/pnx-i2c.1/i2c-1/i2c-dev/i2c-1/dev
Modules linked in: parr_driver(+)
CPU: 0    Not tainted  (2.6.33.7 #125)
PC is at parr_init+0xa4/0xec [parr_driver]
LR is at parr_init+0xa4/0xec [parr_driver]
pc : [<bf0000f0>]    lr : [<bf0000f0>]    psr: 60000013
sp : c1763f48  ip : c075ffe4  fp : 00000002
r10: bf00004c  r9 : 00000000  r8 : c0659f68
r7 : 00131fd8  r6 : c0827fcc  r5 : bf00033c  r4 : 13009000
r3 : 60000013  r2 : c081d184  r1 : 000044c0  r0 : 00000018
Flags: nZCv  IRQs on  FIQs on  Mode SVC_32  ISA ARM  Segment user
Control: 0005317f  Table: 31798000  DAC: 00000015
Process insmod (pid: 260, stack limit = 0xc1762270)
Stack: (0xc1763f48 to 0xc1764000)
3f40:                   00000000 0fe00000 00000000 c0659350 00000000 00000000
3f60: 00000e19 bf00033c 00000000 bf00033c 00000000 00131fd8 c0659f68 c1762000
3f80: 00000000 c069581c c1c1aec0 c1768ec0 00000003 00000000 befc0e78 00000000
3fa0: 00000080 c0659dc0 00000000 befc0e78 00131fd8 00000e19 00102520 00000000
3fc0: 00000000 befc0e78 00000000 00000080 befc0e78 00102520 00000000 00000002
3fe0: 00000069 befc07e4 00015608 00009434 60000010 00131fd8 30880031 30880431
[<bf0000f0>] (parr_init+0xa4/0xec [parr_driver]) from [<c0659350>] (do_one_initcall+0x58/0x1a4)
[<c0659350>] (do_one_initcall+0x58/0x1a4) from [<c069581c>] (sys_init_module+0xc0/0x1f0)
[<c069581c>] (sys_init_module+0xc0/0x1f0) from [<c0659dc0>] (ret_fast_syscall+0x0/0x28)
Code: eb59cf48 e1a01004 e59f0040 eb59cf45 (e5941000) 
---[ end trace e5bb78b790365d97 ]---
Segmentation fault

What's wrong with my code ? 我的代码有什么问题?


As adviced in the comment below, i tryed to use the ioremap instead of direct read. 如以下评论中所建议,我尝试使用ioremap代替直接读取。

here is my new code 这是我的新代码

    if(request_mem_region(0x13009000, 4, "parr_driver") != NULL)
    {
            map = ioremap(0x13009000, 4);

            if(map != NULL)
            {
                    printk("reg value  %X \n", ioread32(map));

                    iowrite32(0x0000FFFF, map);

                    printk("reg value  %X \n", ioread32(map));
            }
            else
            {
                    printk("ioremap FAIL \n");
            }
    }

the console log 控制台日志

parr_driver : minor number : 0 
parr_driver : driver is now ready to be use 
reg value  DFB 
reg value  DFB 

It's doesn't crash anymore now, read seems to work, but i can't still write to my hardware register (DFB the second time instead of FFFF) 它现在不再崩溃,读取似乎可以工作,但是我仍然无法写入我的硬件寄存器(第二次使用DFB而不是FFFF)

Any idea ? 任何想法 ?

As Chris Stratton mensioned it, it was a problem of accessing. 克里斯·斯特拉顿(Chris Stratton)提到时,这是访问的问题。 Actually, on kernel boot, the register clock was disable, that's why i could not write it. 实际上,在内核启动时,寄存器时钟被禁用,这就是为什么我无法编写它的原因。

Thanks a lot for your help. 非常感谢你的帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM