Re: PCI Driver - Read/Write to Base Address



Thanks for your help.

Unfortunately, I get this message in the kernel log:
PCI: Unable to reserve mem region #2:4000@de000000

Is there a way to get around this error? Is it because of the
pci_request_region function?

In addition, I eventually want to take the reading configuration
register, reading PCI memory, and writing PCI memory code out of the
probe function and set each one as a function. After the device driver
is loaded, how do I call those functions from another C program?

gil_hamilton@xxxxxxxxxxx wrote:
elliotng.ee@xxxxxxxxx wrote:
I am trying to read and write to a PCI Base Address. Right now, I have:

static int rpm_probe(struct pci_dev *dev, const struct pci_device_id
*id)
{
pci_enable_device(dev);

u32 BaseAddress0;
pci_read_config_dword(dev, PCI_BASE_ADDRESS_0, &BaseAddress0);
printk(KERN_ALERT "PCI_BASE_ADDRESS_0: %x\n", BaseAddress0);
}


To read/write to that particular base address, do we just need to add
the following lines:
u32 BaseAddress0data;
pci_read_config_dword(dev, BaseAddress0, &BaseAddress0data);
pci_write_config_dword(dev, BaseAddress0, 0xFF000000);

or is there another pci function that I should use?

No, that's not correct, if I understand what you're doing.

First, you should get the base address value (for memory-mapped I/O
region 0) with:
int BarNumber = 0;
pci_request_region(dev, BarNumber, "MyDriverName");
BaseAddress0 = pci_resource_start(dev, BarNumber);
BarLen = pci_resource_len(dev, BarNumber);

This marks you as the owner of the region and returns to you its base
(physical) address and length.

Second, when you actually read or write the memory-mapped I/O in your
device, you don't use pci_read_config* / pci_write_config* -- those are
only used when you are reading / writing the device's *configuration
space registers*. Reading the memory-mapped I/O region is done
something like this:

char * Region0 = ioremap_nocache(BaseAddress0, BarLen);
Data = readl(Region0 + offsetToRead);
writel(DataToWrite, Region0 + offsetToRead);

ioremap_nocache creates a virtual-to-physical mapping in kernel address
space that you can use to access the device memory.

readl() and writel() are arch-specific macros that typically boil down
to something like this (however, you should still use them since there
may be slight variations between architectures):
unsigned long readl(void * addr) { return *((volatile unsigned long
*)addr)); }
void writel(unsigned long value, void * addr) { *((volatile unsigned
long *)addr) = value; }

You might want to check out the book "Linux Device Drivers, version 3"
which is available for free online. It has a chapter on PCI Drivers as
well as info on kernel memory management.

Also, check out the skeleton network driver in
/usr/src/linux/drivers/net/pci-skeleton.c for some sample code.

GH

.



Relevant Pages

  • Re: 2.6.19-rc6-mm1 -- sched-improve-migration-accuracy.patch slows boot
    ... Allocating PCI resources starting at 20000000 ... per task-struct memory footprint: 1200 bytes ... Linux Plug and Play Support v0.97 Adam Belay ... Freeing unused kernel memory: 156k freed ...
    (Linux-Kernel)
  • Re: PCI bus operations and driver design
    ... > device driver uses memory mapped IO to read/write to DPRAM on a PCI card. ... but on most platforms these failures are catastrophic and global and not ...
    (microsoft.public.development.device.drivers)
  • Kernel/Module OOPS do_softirq.
    ... My system configuration is a single board CompactPCI PIII and a PCI ... I am running kernel version 2.4.21 and I am ... Does anything in these 2 oops report look familar to anyone? ... Device Driver book by Rubini and Corbet and I thank them for getting me ...
    (comp.os.linux.development.system)
  • PCI bus operations and driver design
    ... I have been investigating an issue relating to our 'working' PCI ... device driver uses memory mapped IO to read/write to DPRAM on a PCI card. ... Coming from a background including lots of things *other* than writing ...
    (microsoft.public.development.device.drivers)
  • Re: Persuading PCI memory writes to write as a burst
    ... then writing a few resulting chunks of memory (each ... encourage PCI bridgeto do write combining. ... The PCI card has BAR0 declaring memory with prefetch (but, sadly, not fast ... posted writes in the same application + device driver setup. ...
    (microsoft.public.development.device.drivers)