Xilinx FPGA PCIE Linux驱动程序.docx_第1页
Xilinx FPGA PCIE Linux驱动程序.docx_第2页
Xilinx FPGA PCIE Linux驱动程序.docx_第3页
Xilinx FPGA PCIE Linux驱动程序.docx_第4页
Xilinx FPGA PCIE Linux驱动程序.docx_第5页
已阅读5页,还剩12页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

/ (c) Copyright 2009 聳2009 Xilinx, Inc. All rights reserved./ This file contains confidential and proprietary information/ of Xilinx, Inc. and is protected under U.S. and/ international copyright and other intellectual property/ laws./ DISCLAIMER/ This disclaimer is not a license and does not grant any/ rights to the materials distributed herewith. Except as/ otherwise provided in a valid license issued to you by/ Xilinx, and to the maximum extent permitted by applicable/ law: (1) THESE MATERIALS ARE MADE AVAILABLE AS IS AND/ WITH ALL FAULTS, AND XILINX HEREBY DISCLAIMS ALL WARRANTIES/ AND CONDITIONS, EXPRESS, IMPLIED, OR STATUTORY, INCLUDING/ BUT NOT LIMITED TO WARRANTIES OF MERCHANTABILITY, NON-/ INFRINGEMENT, OR FITNESS FOR ANY PARTICULAR PURPOSE; and/ (2) Xilinx shall not be liable (whether in contract or tort,/ including negligence, or under any other theory of/ liability) for any loss or damage of any kind or nature/ related to, arising under or in connection with these/ materials, including for any direct, or any indirect,/ special, incidental, or consequential loss or damage/ (including loss of data, profits, goodwill, or any type of/ loss or damage suffered as a result of any action brought/ by a third party) even if such damage or loss was/ reasonably foreseeable or Xilinx had been advised of the/ possibility of the same./ CRITICAL APPLICATIONS/ Xilinx products are not designed or intended to be fail-/ safe, or for use in any application requiring fail-safe/ performance, such as life-support or safety devices or/ systems, Class III medical devices, nuclear facilities,/ applications related to the deployment of airbags, or any/ other applications that could lead to death, personal/ injury, or severe property or environmental damage/ (individually and collectively, Critical/ Applications). Customer assumes the sole risk and/ liability of any use of Xilinx products in Critical/ Applications, subject only to applicable laws and/ regulations governing limitations on product liability./ THIS COPYRIGHT NOTICE AND DISCLAIMER MUST BE RETAINED AS/ PART OF THIS FILE AT ALL TIMES./-/- Filename: xbmd.c/-/- Description: XBMD device driver. /- /- XBMD is an example Red Hat device driver which exercises XBMD design/- Device driver has been tested on Red Hat Fedora FC9 2.6.15./-#include #include #include #include #include /#include /#include #include /* copy_to_user */#include xbmd.h/ semaphoresenum SEM_READ, SEM_WRITE, SEM_WRITEREG, SEM_READREG, SEM_WAITFOR, SEM_DMA, NUM_SEMS;#define SUCCESS 0#define CRIT_ERR -1/ Debug - define will output more info#define Verbose 1/ Max DMA Buffer Size#define BUF_SIZE (4096 * 1024)enum INITCARD, / 0 INITRST, DISPREGS, RDDCSR, RDDDMACR, RDWDMATLPA, / 5 RDWDMATLPS, RDWDMATLPC, RDWDMATLPP, RDRDMATLPP, RDRDMATLPA, / 10 RDRDMATLPS, RDRDMATLPC, RDWDMAPERF, RDRDMAPERF, RDRDMASTAT, / 15 RDNRDCOMP, RDRCOMPDSIZE, RDDLWSTAT, RDDLTRSSTAT, RDDMISCCONT, / 20 RDDMISCONT, RDDLNKC, DFCCTL, DFCPINFO, DFCNPINFO, / 25 DFCINFO, RDCFGREG, WRCFGREG, RDBMDREG, WRBMDREG, / 30 WRDDMACR, WRWDMATLPS, WRWDMATLPC, WRWDMATLPP, WRRDMATLPS, WRRDMATLPC, WRRDMATLPP, WRDMISCCONT, WRDDLNKC, NUMCOMMANDS;/semaphoresstruct semaphore gSemNUM_SEMS;MODULE_LICENSE(Dual BSD/GPL);/ Defines the Vendor ID. Must be changed if core generated did not set the Vendor ID to the same value#define PCI_VENDOR_ID_XILINX 0x10ee/ Defines the Device ID. Must be changed if core generated did not set the Device ID to the same value#define PCI_DEVICE_ID_XILINX_PCIE 0x0007/ Defining#define XBMD_REGISTER_SIZE (4*8) / There are eight registers, and each is 4 bytes wide.#define HAVE_REGION 0x01 / I/O Memory region#define HAVE_IRQ 0x02 / Interupt/Status Flags: / 1 = Resouce successfully acquired/ 0 = Resource not acquired. #define HAVE_REGION 0x01 / I/O Memory region#define HAVE_IRQ 0x02 / Interupt#define HAVE_KREG 0x04 / Kernel registrationint gDrvrMajor = 241; / Major number not dynamic.unsigned int gStatFlags = 0x00; / Status flags used for cleanup.unsigned long gBaseHdwr; / Base register address (Hardware address)unsigned long gBaseLen; / Base register address Lengthvoid *gBaseVirt = NULL; / Base register address (Virtual address, for I/O).char gDrvrName= xbmd; / Name of driver in proc.struct pci_dev *gDev = NULL; / PCI device gIrq; / IRQ assigned by PCI system.char *gBufferUnaligned = NULL; / Pointer to Unaligned DMA buffer.char *gReadBuffer = NULL; / Pointer to dword aligned DMA buffer.char *gWriteBuffer = NULL; / Pointer to dword aligned DMA buffer.dma_addr_t gReadHWAddr;dma_addr_t gWriteHWAddr;unsigned long SA_SHIRQ = 0;unsigned long SA_SAMPLE_RANDOM = 0;int pos;/ Struct Used for Writing CFG Register. Holds value and register to be writtentypedef struct cfgwrite int reg; int value; cfgwr;/ Struct Used for Writing BMD Register. Holds value and register to be writtentypedef struct bmdwrite int reg; int value; bmdwr;/-/ Prototypes/-void XPCIe_IRQHandler (int irq, void *dev_id, struct pt_regs *regs);u32 XPCIe_ReadReg (u32 dw_offset);void XPCIe_WriteReg (u32 dw_offset, u32 val);void XPCIe_InitCard (void);void XPCIe_InitiatorReset (void);u32 XPCIe_ReadCfgReg (u32 byte);u32 XPCIe_WriteCfgReg (u32 byte, u32 value);/-/ Name: XPCIe_Open/ Description: Book keeping routine invoked each time the device is opened. / Arguments: inode :/ filp :/ Returns: 0 on success, error code on failure./ Modification log:/ Date Who Description/-int XPCIe_Open(struct inode *inode, struct file *filp) printk(KERN_INFO%s: Open: module openedn,gDrvrName); return SUCCESS;/-/ Name: XPCIe_Release/ Description: Book keeping routine invoked each time the device is closed. / Arguments: inode :/ filp :/ Returns: 0 on success, error code on failure./ Modification log:/ Date Who Description/-int XPCIe_Release(struct inode *inode, struct file *filp) printk(KERN_INFO%s: Release: module releasedn,gDrvrName); return(SUCCESS);/-/ Name: XPCIe_Write/ Description: This routine is invoked from user space to write data to/ the PCIe device. / Arguments: filp : file pointer to opened device./ buf : pointer to location in users space, where data is to/ be acquired./ count : Amount of data in bytes user wishes to send./ Returns: SUCCESS = Success/ CRIT_ERR = Critical failure/ Modification log:/ Date Who Description/-ssize_t XPCIe_Write(struct file *filp, const char *buf, size_t count, loff_t *f_pos) int ret = SUCCESS; memcpy(char *)gWriteBuffer, buf, count); printk(KERN_INFO%s: XPCIe_Write: %d bytes have been written.n, gDrvrName, count); memcpy(char *)gReadBuffer, buf, count); printk(KERN_INFO%s: XPCIe_Write: %d bytes have been written.n, gDrvrName, count); return (ret);/-/ Name: XPCIe_Read/ Description: This routine is invoked from user space to read data from/ the PCIe device. *NOTE: This routine returns the entire/ buffer, (BUF_SIZE), count is ignored!. The user App must/ do any needed processing on the buffer./ Arguments: filp : file pointer to opened device./ buf : pointer to location in users space, where data is to/ be placed./ count : Amount of data in bytes user wishes to read./ Returns: SUCCESS = Success/ CRIT_ERR = Critical failure/ Modification log:/ Date Who Description/-ssize_t XPCIe_Read(struct file *filp, char *buf, size_t count, loff_t *f_pos) memcpy(buf, (char *)gWriteBuffer, count); printk(KERN_INFO%s: XPCIe_Read: %d bytes have been read.n, gDrvrName, count); return (0);/-/ Name: XPCIe_Ioctl/ Description: This routine is invoked from user space to configure the / running driver. / Arguments: inode : / filp : File pointer to opened device./ cmd : Ioctl command to execute./ arg : Argument to Ioctl command./ Returns: 0 on success, error code on failure./ Modification log:/ Date Who Description/-int XPCIe_Ioctl(struct inode *inode, struct file *filp, unsigned int cmd, unsigned long arg) u32 regx; int ret = SUCCESS; /*寄存器相关说明在xapp1052.pdf的附录里*/ switch (cmd) case INITCARD: / Initailizes XBMD application XPCIe_InitCard(); break;case INITRST: / Resets XBMD applications XPCIe_InitiatorReset(); break;case DISPREGS: break;case RDDCSR: / Read: Device Control Status Register regx = XPCIe_ReadReg(0); *(u32 *)arg) = regx; break;case RDDDMACR: / Read: DMA Control Status Register regx = XPCIe_ReadReg(1); *(u32 *)arg) = regx; break;case RDWDMATLPA: / Read: Write DMA TLP Address Register regx = XPCIe_ReadReg(2); *(u32 *)arg) = regx; break;case RDWDMATLPS: / Read: Write DMA TLP Size Register regx = XPCIe_ReadReg(3); *(u32 *)arg) = regx; break;case RDWDMATLPC: / Read: Write DMA TLP Count Register regx = XPCIe_ReadReg(4); *(u32 *)arg) = regx; break;case RDWDMATLPP: / Read: Write DMA TLP Pattern Register regx = XPCIe_ReadReg(5); *(u32 *)arg) = regx; break;case RDRDMATLPP: / Read: Read DMA TLP Pattern Register regx = XPCIe_ReadReg(6); *(u32 *)arg) = regx; break;case RDRDMATLPA: / Read: Read DMA TLP Address Register regx = XPCIe_ReadReg(7); *(u32 *)arg) = regx; break;case RDRDMATLPS: / Read: Read DMA TLP Size Register regx = XPCIe_ReadReg(8); *(u32 *)arg) = regx; break;case RDRDMATLPC: / Read: Read DMA TLP Count Register regx = XPCIe_ReadReg(9); *(u32 *)arg) = regx; break;case RDWDMAPERF: / Read: Write DMA Performance Register regx = XPCIe_ReadReg(10); *(u32 *)arg) = regx; break;case RDRDMAPERF: / Read: Read DMA Performance Register regx = XPCIe_ReadReg(11); *(u32 *)arg) = regx; break;case RDRDMASTAT: / Read: Read DMA Status Register regx = XPCIe_ReadReg(12); *(u32 *)arg) = regx; break;case RDNRDCOMP: / Read: Number of Read Completion w/ Data Register regx = XPCIe_ReadReg(13); *(u32 *)arg) = regx; break;case RDRCOMPDSIZE: / Read: Read Completion Size Register regx = XPCIe_ReadReg(14); *(u32 *)arg) = regx; break;case RDDLWSTAT: / Read: Device Link Width Status Register regx = XPCIe_ReadReg(15); *(u32 *)arg) = regx; break;case RDDLTRSSTAT: / Read: Device Link Transaction Size Status Register regx = XPCIe_ReadReg(16); *(u32 *)arg) = regx; break;case RDDMISCCONT: / Read: Device Miscellaneous Control Register regx = XPCIe_ReadReg(17); *(u32 *)arg) = regx; break;case RDDMISCONT: / Read: Device MSI Control regx = XPCIe_ReadReg(18); *(u32 *)arg) = regx; break;case RDDLNKC: / Read: Device Directed Link Change Register regx = XPCIe_ReadReg(19); *(u32 *)arg) = regx; break;case DFCCTL: / Read: Device FC Control Register regx = XPCIe_ReadReg(20); *(u32 *)arg) = regx; break;case DFCPINFO: / Read: Device FC Posted Information regx = XPCIe_ReadReg(21); *(u32 *)arg) = regx; break;case DFCNPINFO: / Read: Device FC Non Posted Information regx = XPCIe_ReadReg(22); *(u32 *)arg) = regx; break;case DFCINFO: / Read: Device FC Completion Information regx = XPCIe_ReadReg(23); *(u32 *)arg) = regx; break;case WRDDMACR: / Write: DMA Control Status Register XPCIe_WriteReg(1, arg); break;case WRWDMATLPS: / Write: Write DMA TLP Size Register XPCIe_WriteReg(3, arg); break;case WRWDMATLPC: / Write: Write DMA TLP Count Register XPCIe_WriteReg(4, arg); break;case WRWDMATLPP: / Write: Write DMA TLP Pattern Register XPCIe_WriteReg(5, arg); break;case WRRDMATLPS: / Write: Read DMA TLP Size Register XPCIe_WriteReg(8, arg); break;case WRRDMATLPC: / Write: Read DMA TLP Count Register XPCIe_WriteReg(9, arg); break;case WRRDMATLPP: / Write: Read DMA TLP Pattern Register XPCIe_WriteReg(6, arg); break;case WRDMISCCONT: / Write: Device Miscellaneous Control Register XPCIe_WriteReg(18, arg); break;case WRDDLNKC: / Write: Device Directed Link Change Register XPCIe_WriteReg(19, arg); break;case RDBMDREG: / Read: Any XBMD Reg. Added generic functionality so all register can be read regx = XPCIe_ReadReg(*(u32 *)arg); *(u32 *)arg) = regx; break; case RDCFGREG: / Read: Any CFG Reg. Added generic functionality so all register can be read regx = XPCIe_ReadCfgReg(*(u32 *)arg); *(u32 *)arg) = regx; break;case WRBMDREG: / Write: Any BMD Reg. Added generic functionality so all register can be read XPCIe_WriteReg(*(bmdwr *)arg).reg,(*(bmdwr *)arg).value); printk(KERN_WARNING%d: Write Register.n, (*(bmdwr *)arg).reg); printk(KERN_WARNING%d: Write Valuen, (*(bmdwr *)arg).value); break;case WRCFGREG: / Write: Any CFG Reg. Added generic functionality so all register can be read regx = XPCIe_WriteCfgReg(*(cfgwr *)arg).reg,(*(cfgwr *)arg).value); printk(KERN_WARNING%d: Write Register.n, (*(cfgwr *)arg).reg); printk(KERN_WARNING%d: Write Valuen, (*(cfgwr *)arg).value); break;default: break; return ret;/ Aliasing write, read, ioctl, etc.struct file_operations XPCIe_Intf = read: XPCIe_Read,write: XPCIe_Write,ioctl: XPCIe_Ioctl,open: XPCIe_Open,release: XPCIe_Release,;static int XPCIe_init(void) / Find the Xilinx EP device. The device is found by matching device and vendor IDs which is defined / at the top of this fil

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论