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

时间:2012-04-25 19:06:37

标签: c memory embedded-linux

我正在研究LPC3131(来自NXP的arm9),我正在尝试在linux内核中创建我的第一个驱动程序,但是我遇到了一些麻烦。

的确,我想在微控制器寄存器中读取/写入值,就像我在那里找到的那​​样

Reading from 16-bit hardware registers

我在我的驱动程序的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;
}

第一个printk工作正常,它打印我的注册地址。 但第二个没有给出预期的结果,我得到了一个分段错误。 您可以在下面找到我的控制台日志:

/ # 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

我的代码出了什么问题?


正如下面评论中所建议的那样,我尝试使用ioremap而不是直接阅读。

这是我的新代码

    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");
            }
    }

控制台日志

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

它现在不再崩溃,读取似乎工作,但我仍然不能写入我的硬件寄存器(DFB第二次而不是FFFF)

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

正如Chris Stratton所说,这是一个访问问题。实际上,在内核启动时,寄存器时钟被禁用,这就是我无法写入的原因。

非常感谢你的帮助。