How to read write PCI BAR memory address

1,517 views
Skip to first unread message

Amarjeet Sharma

unread,
Apr 24, 2015, 1:39:04 AM4/24/15
to inside...@googlegroups.com

My PCI device information is as follows:

Print Begin Configuration space...
BAR 0 start address: f6000000
BAR 0 end address: f6ffffff
BAR 0 flags: 140204
BAR 0 IO / MEM:  MEM
BAR 0 PREFETCH NON PREFETCH: NON-PREFETCH
BAR 1 start address: 0
BAR 1 end address: 0
BAR 1 flags: 0
BAR 1 IO / MEM:  MEM
BAR 1 PREFETCH NON PREFETCH: NON-PREFETCH
BAR 2 start address: f0000000
BAR 2 end address: f3ffffff
BAR 2 flags: 14220c
BAR 2 IO / MEM:  MEM
BAR 2 PREFETCH NON PREFETCH: PREFETCH

I want to write a byte and read it back to BAR2 for a test simple . Is it possible to write a byte to BAR2 start address.

Anil Kumar Pugalia

unread,
Apr 24, 2015, 3:25:43 AM4/24/15
to inside...@googlegroups.com
Yes. Do an ioremap, and then use iowrite8.

Regards
Anil
Passion: http://sysplay.in (Playing with Systems)
--
You received this message because you are subscribed to the Google Groups "SysPlay's Inside Linux" group.
To unsubscribe from this group and stop receiving emails from it, send an email to inside_linux...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Amarjeet Sharma

unread,
Apr 24, 2015, 4:58:25 AM4/24/15
to inside...@googlegroups.com
Sir !!!! 
I have the following code.The address range for BAR2 is shown in my previous mail.
Can you please provide me with pseudo changes.

#define MY_BYTE 0xAA

static int expt_probe(struct pci_dev *dev, const struct pci_device_id *id)
{
uint32_t addr;
printk("PCI probe\n");
struct dev_priv *dpv = &_pvt; // Should be converted to kmalloc for multi-card support
int retval;

retval = pci_enable_device(dev);
if (retval)
{
printk(KERN_ERR "Unable to enable this PCI device\n");
return retval;
}
else
{
printk(KERN_INFO "PCI device enabled\n");
}

display_pci_config_space(dev);

retval = pci_request_regions(dev, "expt_pci");
if (retval)
{
printk(KERN_ERR "Unable to acquire regions of this PCI device\n");
pci_disable_device(dev);
return retval;
}
else
{
printk(KERN_INFO "PCI device regions acquired\n");
}

if ((dpv->reg_base = ioremap(pci_resource_start(dev, 2), pci_resource_len(dev, 2))) == NULL)
{
printk(KERN_ERR "Unable to map registers of this PCI device\n");
pci_release_regions(dev);
pci_disable_device(dev);
return -ENODEV;
}
printk(KERN_INFO "Register Base: %p\n", dpv->reg_base);

printk(KERN_INFO "IRQ: %u\n", dev->irq);

pci_set_drvdata(dev, dpv);

#ifdef ENABLE_FILE_OPS
retval = char_register_dev(dpv);
if (retval)
{
/* Something prevented us from registering this driver */
printk(KERN_ERR "Unable to register the character vertical\n");
pci_set_drvdata(dev, NULL);
iounmap(dpv->reg_base);
pci_release_regions(dev);
pci_disable_device(dev);
return retval;
}
else
{
printk(KERN_INFO "Character vertical registered\n");
}
#endif

printk(KERN_INFO "PCI device registered\n");

printk(KERN_INFO "Expt: In write\n");
writel(MY_BYTE, dpv->reg_base);
printk(KERN_INFO "Expt: In read\n");
addr = readl(dpv->reg_base );
printk("Value read is %x\n",addr);

return 0;
}

Anil Kumar Pugalia

unread,
Apr 26, 2015, 10:13:23 AM4/26/15
to inside...@googlegroups.com
If you just want to read / write into the BAR2, you don't need any of the following long code - just ioremap the bus address, and then ioread8/iowrite8 on the virtual address returned, as I mentioned earlier - that's all


Regards
Anil
Passion: http://sysplay.in (Playing with Systems)

Anil Kumar Pugalia

unread,
Apr 29, 2015, 4:25:38 AM4/29/15
to Inside Linux
For just a quick hack, you may include <asm/io.h> and put the following code in your constructor/init function:

void __iomem *reg_base;

if ((reg_base = ioremap(0xf0000000, 0x4)) == NULL)
{
    printk(KERN_ERR "Unable to map registers of this PCI device\n");
    return -ENODEV;
}

printk(KERN_INFO "First 4 bytes: %08X\n", ioread32(reg_base));

iounmap(reg_base);

return 0;


Regards
Anil
Passion: http://sysplay.in (Playing with Systems)
Amarjeet Sharma wrote on Monday 27 April 2015 05:47 PM:
Sir,

I try'ed but could not achieve it.
Please can you give me the pseudo code for the first/last time.
I know i should not ask you but please.

All the info you already have if anything required please do tell me.

Regards

Reply all
Reply to author
Forward
0 new messages