1: //=========================摰儔include瑼?define瑼?======================
2: #define SOCLE_FPGA_MAJOR 0 // 0: dynamic major number
3: #define SOCLE_FPGA_MINOR 0 //甈∠楊??
4: #define FPGA_START 0x1CB20000
5: #define SIZE_OF_DEVICE (0x4000*32)
6:
7: #include<linux/sched.h>
8: #include <linux/kernel.h>
9: #include <linux/fs.h> //摰儔file蝯?
10: #include <linux/types.h>
11: #include <linux/module.h>
12: #include <linux/cdev.h>
13: #include <linux/device.h>
14: #include <linux/ioport.h>
15: #include <asm/uaccess.h> //copy from/to user
16: #include <linux/ioctl.h>
17: #include <asm/io.h>
18: #include <asm/system.h>
19: #include <asm/arch/regs-pwmt.h>
20: #include <asm/arch/hardware.h>
21:
22: //=======================================================================
23:
24: //============================摰儔?航炊閮憿舐內?澆?========================
25: //#define SOCLE_FPGA_DEBUG
26: #ifdef SOCLE_FPGA_DEBUG
27: #define FPGA_DBG(fmt, args...) printk(KERN_DEBUG "SOCLE_FPGA: " fmt, ## args)
28: #else
29: #define FPGA_DBG(fmt, args...)
30: #endif
31: //=======================================================================
32:
33: //===============================摰儔鋆蔭蝺刻?=============================
34: static int socle_FPGA_major = SOCLE_FPGA_MAJOR; //銝餌楊??
35: static int socle_FPGA_minor = SOCLE_FPGA_MINOR; //甈∠楊??
36: module_param(socle_FPGA_major, int, 644);
37: module_param(socle_FPGA_minor, int, 644);
38: //=======================================================================
39:
40: //==========================摰儔??蝵桃?蝯??==========================
41: static struct socle_FPGA_Driver {
42: int busy;
43: dev_t FPGA_devt;
44: struct class *class;
45: struct class_device *class_dev;
46: struct cdev *cdev;
47: } socle_FPGA;
48: //=======================================================================
49:
50: static ssize_t socle_FPGA_read (struct file *fd, char __user *buf, size_t size, loff_t *f_pos);
51: static ssize_t socle_FPGA_write (struct file *fd, const char __user *buf, size_t size, loff_t *f_pos);
52: static int socle_FPGA_ioctl (struct inode *inodep, struct file *filep, unsigned int cmd, unsigned long arg);
53: static int socle_FPGA_open (struct inode *inodep, struct file *filep);
54: static int socle_FPGA_release (struct inode *inodep, struct file *filep);
55: u32 val; //?脣?霈撖怎???
56: volatile unsigned long *FPGA_wSTART;
57:
58: //===========================撽?蝔?????==============================
59: static struct file_operations socle_FPGA_fops = {
60: .owner = THIS_MODULE, //摰儔??linux/module.h>
61: .llseek = no_llseek, //銝祕雿lseek
62: .read = socle_FPGA_read, //敺ernel-sapce?酌ser-space
63: .write = socle_FPGA_write, //敺ser-space?逃ernel-space
64: .ioctl = socle_FPGA_ioctl, //鋆蔭撠惇?誘
65: .open = socle_FPGA_open, //??獢?
66: .release = socle_FPGA_release //?鋆蔭瑼?
67: };
68: //=======================================================================
69:
70: //=========================Application霈????========================
71: static int socle_FPGA_read(struct file *fd, char __user *buf, size_t size, loff_t *f_pos)
72: {
73: val=ioread32(FPGA_wSTART);
74: mb();
75: copy_to_user(buf, &val,size);
76: return 0;
77: }
78: //=======================================================================
79:
80: //=========================Application摮鞈?=========================
81: static int socle_FPGA_write(struct file *fd, const char __user *buf, size_t size, loff_t *f_pos)
82: {
83: copy_from_user(&val, buf, size);
84: iowrite32(val,FPGA_wSTART);
85: mb();
86: return 0;
87: }
88: //=======================================================================
89:
90:
91: //============================摰儔撠惇?誘===============================
92: static int socle_FPGA_ioctl(struct inode *inodep, struct file *filep, unsigned int cmd, unsigned long arg)
93: {
94:
95: return 0;
96: }
97: //=======================================================================
98:
99: //===================?箏?蝥?璆剝脰?敹???????=========================
100: //ex:瑼X鋆蔭?寞??隤扎璅?蝵桀?憪???逆_op??蝑?
101: static int socle_FPGA_open(struct inode *inodep, struct file *filep)
102: {
103:
104: FPGA_DBG("socle_FPGA_open\n");
105:
106: return nonseekable_open(inodep,filep);
107: }
108: //=======================================================================
109:
110:
111: //================?鋆蔭瑼??逵ile撠◤???鋡怨孛??===================
112: //ex:?open?脣??審ilp->private_data?镼?
113: static int socle_FPGA_release(struct inode *inodep, struct file *filep)
114: {
115:
116: FPGA_DBG("socle_FPGA_release\n");
117: return 0;
118: }
119: //=======================================================================
120:
121:
122: //=====================??cdev_init()???迨蝯?=======================
123: static int socle_FPGA_setup_cdev(struct socle_FPGA_Driver *p_FPGA)
124: {
125: int ret, err;
126:
127: p_FPGA->FPGA_devt = MKDEV(socle_FPGA_major, socle_FPGA_minor);
128:
129: if (socle_FPGA_major) {
130: ret = register_chrdev_region(p_FPGA->FPGA_devt, 1, "FPGA-Driver");
131: } else {
132: ret = alloc_chrdev_region(&p_FPGA->FPGA_devt, socle_FPGA_minor, 1, "FPGA-Driver");
133: socle_FPGA_major = MAJOR(p_FPGA->FPGA_devt);
134: socle_FPGA_minor = MINOR(p_FPGA->FPGA_devt);
135: }
136: if (ret < 0)
137: return ret;
138:
139: p_FPGA->class = class_create(THIS_MODULE, "socle-FPGA-Driver");
140: if (IS_ERR(p_FPGA->class)) {
141: printk("socle_FPGA_setup_cdev: Can't create FPGA Driver class!\n");
142: ret = PTR_ERR(p_FPGA->class);
143: goto error1;
144: }
145: p_FPGA->cdev = cdev_alloc();
146: if (NULL == p_FPGA->cdev) {
147: printk("socle_FPGA_setup_cdev: Can't alloc FPGA Driver cdev!\n");
148: ret = -ENOMEM;
149: goto error2;
150: }
151:
152: p_FPGA->cdev->owner = THIS_MODULE;
153: p_FPGA->cdev->ops = &socle_FPGA_fops;
154:
155: err = cdev_add(p_FPGA->cdev, p_FPGA->FPGA_devt, 1);
156: if (err) {
157: printk("socle_FPGA_setup_cdev: Can't add FPGA cdev to system!\n");
158: ret = -EAGAIN;
159: goto error2;
160: }
161:
162: p_FPGA->class_dev = class_device_create(p_FPGA->class, NULL, p_FPGA->FPGA_devt, NULL, "FPGA-Driver");
163: if (IS_ERR(p_FPGA->class_dev)) {
164: printk("socle_FPGA_setup_cdev: Can't create FPGA class_dev to system!\n");
165: ret = PTR_ERR(p_FPGA->class_dev);
166: goto error3;
167: }
168: printk("FPGA-Driver_class_dev info: FPGA-Driver (%d:%d)\n", MAJOR(p_FPGA->FPGA_devt), MINOR(p_FPGA->FPGA_devt));
169:
170: //=================request_mem_region==========================
171: if(!request_mem_region(FPGA_START,SIZE_OF_DEVICE*4,"socle_FPGA"))
172: {
173: printk("error:Request_mem_region\n");
174: return -ENODEV;
175: }
176: //========ioremap_nocache==========
177: FPGA_wSTART=(unsigned long *) ioremap_nocache(FPGA_START,SIZE_OF_DEVICE*4);
178:
179:
180: printk("FPGA> socle_FPGA_ioport_write : %p\n",FPGA_wSTART);
181: //=============================================================================
182:
183:
184: return 0;
185:
186: error3:
187: cdev_del(p_FPGA->cdev);
188: error2:
189: class_destroy(p_FPGA->class);
190: error1:
191: unregister_chrdev_region(p_FPGA->FPGA_devt, 1);
192: return ret;
193: }
194: //=======================================================================
195:
196: //============================頛?詨??銵?=============================
197: static int __init socle_FPGA_init(void)
198: {
199: int ret = 0;
200:
201: socle_FPGA_setup_cdev(&socle_FPGA); //_init()???ocle_FPGA_setup_cdev
202:
203: return ret;
204: }
205: //=======================================================================
206:
207: //===========================?頂蝯梯酉瘨酉??==============================
208: static void socle_FPGA_remove_cdev(struct socle_FPGA_Driver *p_FPGA)
209: {
210: class_device_unregister(p_FPGA->class_dev);
211: cdev_del(p_FPGA->cdev);
212: class_destroy(p_FPGA->class);
213: unregister_chrdev_region(p_FPGA->FPGA_devt, 1);
214: //========ioremap_nocache==========
215: iounmap((void *)FPGA_wSTART);
216:
217: release_mem_region(FPGA_START,SIZE_OF_DEVICE*4);
218: }
219: //=======================================================================
220:
221: //=========================蝯?撽?蝔?撌乩?===============================
222: static void __exit socle_FPGA_cleanup(void)
223: {
224: socle_FPGA_remove_cdev(&socle_FPGA);
225:
226: }
227: //=======================================================================
228:
229:
230: module_init(socle_FPGA_init);
231: module_exit(socle_FPGA_cleanup);
232:
233: MODULE_DESCRIPTION("Socle FPGA Driver");
234: MODULE_LICENSE("GPL");