Implementing PCIe Linux device driver (want to access my card registers from kernel driver)

Ian Vaughan picture Ian Vaughan · Mar 3, 2011 · Viewed 8.2k times · Source

I'm writing a device driver to access the memory in a FPGA on a PCIe card.
The card boots and is probed/found :-

/proc/iomem

80000000-840fffff : PCI Bus #03
  80000000-83ffffff : 0000:03:00.0
  84000000-840fffff : 0000:03:00.0

So reading ldd/etc I coded up a call to request_mem_region at the 80000000, and requested a pointer to it via ioremap_nocache

1) Do I need to request_mem_region as well as a ioremap_nocache, cant I use just the latter?

/proc/iomem After insmod my device driver :-

80000000-840fffff : PCI Bus #03
  80000000-83ffffff : 0000:03:00.0
    80000000-8003ffff : fp2
  84000000-840fffff : 0000:03:00.0

2) Doesnt look quite right to me...?

Anyway, reads don't work (its not coded like below, it has checks etc):-

#define BAR_ADDR 0x80000000
void *base = ioremap_nocache(BAR_ADDR, 0x40000);
void *address = base + KNOWN_REG_LOCATION;
int data = ioread32(address);
printk("fp2: address:0x%08x, data:0x%08x\n", address, data);

Outputs :-

address:0xfd500000, data:0xffffffff

I can read the x80000000+KNOWN_REG_LOCATION from mmap userspace.

3) I've tried __raw_readl/readl with no luck as well.

4) Can I just read at the currently mapped address x80000000?

Answer

Will Tate picture Will Tate · Mar 4, 2011

Ian,

I wrote a PCI driver for a device (full source). The mapping of the register space should be the same though. Here is how I do it.

dm7820_device->pci[region].virt_addr = ioremap_nocache(address, length);
if (dm7820_device->pci[region].virt_addr == NULL) {
    printk(KERN_ERR "%s: ERROR: BAR%u remapping FAILED\n",
        &((dm7820_device->device_name)[0]), region);
    dm7820_release_resources();
    return -ENOMEM;
}

if (request_mem_region(address, length, &((dm7820_device->device_name)[0])) == NULL) {
    printk(KERN_ERR "%s: ERROR: I/O memory range %#lx-%#lx allocation FAILED\n",
        &((dm7820_device->device_name)[0]), address, (address + length - 1));
    dm7820_release_resources();
    return -EBUSY;
}

The address and length values are returned from pci_resource_start() and pci_resource_length() calls.

Then you can access it using ioread32() using dm7820_device->pci[region].virt_addr + <register offset>

Let me know if you have any questions.