Kernel driver, ioremap necessary in MMU-less system?

561 views Asked by At

So, I am a total newbie when it comes to kernel drivers and have a question regarding ioremap function.

I am writing a driver for accessing some registers defined in a custom VHDL-module on a SoC with a ARM Cortex-M3 and FPGA fabric. Looking at examples I figured I should use ioremap, but since the Cortex-M3 does not have a MMU, I don't really see the point, as per the following example:

/* Physical addresses */
static u32* rcu_trig_recv_physaddr = ((u32 *) 0x50040000);
static int  rcu_trig_recv_size     = 0x10; // size of 16 for testing 
/* Virtual addresses */
static u32* rcu_trig_recv_virtbase = NULL;
/*removed code not relevant for the question*/
static int __init rcumodule_init(void)
{
    int iResult = 0; // holding result of operations
    u32 buffer;     

    // Register the driver
    iResult = register_chrdev(rcuc_majorID, "rcuc", &rcuc_fops);
    if (iResult < 0) {
        printk(KERN_INFO "module init: can't register driver\n");
    }
    else{
        printk(KERN_INFO "module init: success!\n");
    }

    // Map physical address to virtual address
    if(rcu_trig_recv_size){
        rcu_trig_recv_virtbase = (u32*) ioremap_nocache( (u32 *)rcu_trig_recv_physaddr, rcu_trig_recv_size );
        printk("Remapped TRGRECV from 0x%p to 0x%p\n", rcu_trig_recv_physaddr, rcu_trig_recv_virtbase);
    }
    // try to read some stuff, expecting 0x17240f09  
    buffer = readl(rcu_trig_recv_virtbase);
    printk("read %lx, at 0x%p\n", buffer, rcu_trig_recv_virtbase);

    return iResult;
}

This then return, when I insmod the driver:

# insmod trigger.ko 
module init: success!
Remapped TRGRECV from 0x50040000 to 0x50040000
read 17240f09, at 0x50040000

According to this, I would just be better off reading the physical address instead. Or is that a bad idea and I should be messing with my registers in a better way?

1

There are 1 answers

0
skrrgwasme On BEST ANSWER

It's possible that you can get away with this if you know your code will never need to be used on another device, but you're much safer sticking with using ioremap(). Basing your code around obtaining and using the pointers provided by memory-mapped IO will make your code more portable and maintainable than utilizing hard-coded physical addresses.

Even if you don't plan on taking this code to a different device, using physical addresses could potentially break your code when simply upgrading to a newer chip in the same line.