第一次写驱动,测试read 、write都不好使
我鲁班无敌 2013-10-21 05:24:36 #include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/miscdevice.h>
#include <linux/delay.h>
#include <asm/irq.h>
//#include <mach/hardware.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/gpio.h>
//#include <plat/gpio-cfg.h>
//#include <mach/regs-gpio.h
#include <linux/types.h>
#include <linux/errno.h>
#include <linux/mm.h>
#include <linux/sched.h>
#include <linux/init.h>
#include <asm/io.h>
#include <asm/system.h>
#include <asm/uaccess.h>
#define FPGA_PHY_START 0x38000000
#define FPGA_PHY_SIZE 2*1024*1024 // 2M
#define FPGA_MAJOR 250
#define FPGA_IO_ADDR 0x0000 //¿¿¿¿
#define DEVICE_NAME "fpga"
static void __iomem *fpga_base;
static ssize_t s3c6410_fpga_read(struct file *filp, char *buf, size_t len, loff_t *off)
{
unsigned long p = *off;
unsigned int count = len;
int ret = 0;
if(count > 0 && count < FPGA_PHY_SIZE)
{
iowrite16(FPGA_IO_ADDR,fpga_base+0x04);
if(copy_to_user(buf,(void *)(fpga_base+p),count))
{
ret = -EFAULT;
}
else
{
*off += count;
ret - count;
}
}
return ret;
}
static ssize_t s3c6410_fpga_write(struct file *filp, char *buf, size_t len, loff_t *off)
{
unsigned long p = *off;
unsigned int count = len;
int ret = 0;
if(count > 0 && count < FPGA_PHY_SIZE)
{
iowrite16(FPGA_IO_ADDR,fpga_base+0x04);
if(copy_from_user(fpga_base+p,buf,count))
{
ret = -EFAULT;
}
else
{
*off += count;
ret - count;
}
}
return ret;
}
static loff_t s3c6410_fpga_llseek(struct file *filp, loff_t offset, int whence)
{
loff_t newpos;
switch(whence) {
case 0: /* SEEK_SET */
newpos = offset;
break;
case 1: /* SEEK_CUR */
newpos = filp->f_pos + offset;
break;
case 2: /* SEEK_END */
newpos = FPGA_PHY_SIZE -1 + offset;
break;
default: /* can't happen */
return -EINVAL;
}
if ((newpos<0) || (newpos>FPGA_PHY_SIZE))
return -EINVAL;
filp->f_pos = newpos;
return newpos;
}
static struct file_operations s3c6410_fpga_fops =
{
.owner = THIS_MODULE,
.read = s3c6410_fpga_read,
.write = s3c6410_fpga_write,
// .open = s3c6410_fpga_open,
.llseek = s3c6410_fpga_llseek,
};
static struct cdev cdev_fpga;
static int __init s3c6410_fpga_init(void)
{ int result;
dev_t devno = MKDEV(FPGA_MAJOR,0);
struct class *fpga_class;
result = register_chrdev_region(devno,1,DEVICE_NAME)
if(result)
{
printk(KERN_NOTICE "Error %d register fpga",result);
return result;
}
cdev_init(&cdev_fpga,&s3c6410_fpga_fops);
result = cdev_add(&cdev_fpga,devno,1);
if(result)
{
printk(KERN_NOTICE "Error %d adding irda",result);
return result;
}
fpga_class = class_create(THIS_MODULE, "fpga_class");
device_create(fpga_class, NULL, devno, "fpga","fpga%d", 0);
fpga_base = ioremap(FPGA_PHY_START,FPGA_PHY_SIZE);
printk ("\n@@@@@@@@@@@@@@@@@@@@@@@@@@\n");
printk (DEVICE_NAME"\tinitialized\n");
printk ("\n@@@@@@@@@@@@@@@@@@@@@@@@@@\n");
return 0;
}
static void __exit s3c6410_fpga_exit(void)
{
cdev_del(&cdev_fpga);
unregister_chrdev_region(MKDEV(FPGA_MAJOR,0),1);
}
module_init(s3c6410_fpga_init);
module_exit(s3c6410_fpga_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("FORLINX Inc.");
这个FPGA要先写一个地址 然后写数据、