diff --git a/api/api.cpp b/api/api.cpp new file mode 100644 index 0000000..27df598 --- /dev/null +++ b/api/api.cpp @@ -0,0 +1,63 @@ +#include "api.h" +#include +#include +#include +#include +#include +#include +#include + +#include + +#include +/*************************************************************************************************/ +UltraSoundDevice::UltraSoundDevice() +{ + files[0] = open("/proc/usd_reg_0", O_RDWR, 0); + bars[0]= mmap(NULL, 64 * 1024, PROT_READ | PROT_WRITE, MAP_SHARED, files[0], 0); + + files[1] = open("/proc/usd_reg_1", O_RDWR, 0); + bars[1]= mmap(NULL, 128 * 1024 * 1024, PROT_READ | PROT_WRITE, MAP_SHARED, files[1], 0); +} + +/*************************************************************************************************/ +UltraSoundDevice::~UltraSoundDevice() +{ + munmap(bars[0], 64 * 1024); + munmap(bars[1], 128 * 1024 * 1024); + + for(int i = 0; i < BAR_NUM; i++) + { + close(files[i]); + } +} + +/*************************************************************************************************/ +uint8_t UltraSoundDevice::readByte(uint32_t address, uint32_t bar) +{ + uint8_t* ptr = (uint8_t*)bars[bar]; + + return ptr[address]; +} + +/*************************************************************************************************/ +uint32_t UltraSoundDevice::readWord(uint32_t address, uint32_t bar) +{ + uint32_t* ptr = (uint32_t*)bars[bar]; + + return ptr[address / 4]; +} + +/*************************************************************************************************/ +void UltraSoundDevice::writeByte(uint32_t address, uint32_t bar, uint8_t data) +{ + uint8_t* ptr = (uint8_t*)bars[bar]; + ptr[address] = data; +} + +/*************************************************************************************************/ +void UltraSoundDevice::writeWord(uint32_t address, uint32_t bar, uint32_t data) +{ + uint32_t* ptr = (uint32_t*)bars[bar]; + ptr[address / 4] = data; +} diff --git a/api/api.h b/api/api.h new file mode 100644 index 0000000..6bdadb2 --- /dev/null +++ b/api/api.h @@ -0,0 +1,21 @@ +#include "stdint.h" + +#define BAR_NUM 2 +#define DEV_NAME "usd_reg" + +class UltraSoundDevice +{ +private: + void* bars[BAR_NUM]; + int files[BAR_NUM]; + +public: + UltraSoundDevice(); + ~UltraSoundDevice(); + + uint8_t readByte(uint32_t address, uint32_t bar); + uint32_t readWord(uint32_t address, uint32_t bar); + + void writeByte(uint32_t address, uint32_t bar, uint8_t data); + void writeWord(uint32_t address, uint32_t bar, uint32_t data); +}; \ No newline at end of file diff --git a/usdpci.c b/usdpci.c index 5f71327..8a7e42a 100644 --- a/usdpci.c +++ b/usdpci.c @@ -109,7 +109,7 @@ static int __init pcie_init(void) bars[0].chrdev_fops.open = usd_open_0; bars[0].chrdev_fops.mmap = usd_mmap_0; bars[0].chrdev_fops.release = usd_close_0; - bars[0].bar_size = 4 * 1024; + bars[0].bar_size = 64 * 1024; bars[1].chrdev_fops.owner = THIS_MODULE; bars[1].chrdev_fops.open = usd_open_1; @@ -200,11 +200,15 @@ static int usd_mmap_0(struct file *filp, struct vm_area_struct *vma) unsigned long physical = bars[0].bar_addr + off; unsigned int intvsize = bars[0].bar_size - off; - // these are required for io maps, but is it okay for the buffer as well? vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot); vma->vm_flags |= VM_IO; + if ( vsize < intvsize ) intvsize = vsize; + + printk(KERN_ALERT "-----------------------------------\n"); + printk(KERN_ALERT "BAR0 reg 0 = 0x%x\n", intvsize); + remap_pfn_range(vma, vma->vm_start, physical>>PAGE_SHIFT, intvsize, vma->vm_page_prot); return 0; @@ -317,8 +321,19 @@ static int __init pcie_probe (struct pci_dev *dev, const struct pci_device_id *i pci_set_master(dev); + void * temp = kmalloc(1024 * 1024 * 4, GFP_KERNEL); + u64 phs = virt_to_phys(temp); + memset(temp, 0, 1024 * 1024 * 4); + int i = 0; + u32* counter = (u32*)temp; + for ( ; i < 256 * 1024; i++) + counter[i] = i; + bar0_data[0x82] = phs >> 32; + bar0_data[0x83] = phs; + bar0_data[0x2002] = 0x8000; + printk(KERN_ALERT "-----------------------------------\n"); - printk(KERN_ALERT "BAR0 reg 0 = 0x%x\n", *bar0_data); + printk(KERN_ALERT "BAR0 reg 0 = 0x%x\n", ioread32(bar0_data)); *bar1_data = 0x12345678; printk(KERN_ALERT "BAR1 reg 0 = 0x%x\n", ioread32(bar1_data)); printk(KERN_ALERT "-----------------------------------\n"); @@ -362,9 +377,9 @@ static void __exit pcie_exit(void) char name[16]; int i = 0; - for( ; i < TOTAL_BAR_NUM; i++) + for( ;i < TOTAL_BAR_NUM; i++) { - snprintf(name, 8, "%s%d", DEV_NAME, i); + sprintf(name, "%s_%d", DEV_NAME, i); remove_proc_entry(name, NULL); }