1 /*!***************************************************************************
3 *! FILE NAME : vertex.c
5 *! DESCRIPTION: Implements an interface towards virtex FPGA (mounted on one of our
6 *! evaluation boards) from userspace using ioctl()'s
8 *! The FPGA can be programmed by copying the bit-file to /dev/fpga.
10 *! cat fpga.bit > /dev/fpga
12 *! Kernel log should look like:
13 *! 69900 bytes written
14 *! FPGA-configuration completed, no errors
16 *! Number of bytes written depends on the FPGA
18 *! From Xilinx data sheet:
20 *! XCV100 781,216 bits
21 *! XCV800 4,715,616 bits
23 *! Accepted file type is the design.bit generated by Alliance
25 *! This software just sends the bitfile into the device without
26 *! checking device type etc.
28 *! Sync-header 0xff 0xff 0xff 0xff defines the start for the
29 *! byte stream, everything from that position is written to the FPGA.
32 *! Jul 19 2002 Stefan Lundberg Initial version.
34 *! Revision 1.2 2003/02/24 07:50:30 fredriko
35 *! Bugfixes and cleanups.
37 *! Revision 1.1 2002/06/25 09:58:58 stefanl
38 *! New FPGA driver for Platoon
41 *! ---------------------------------------------------------------------------
43 *! (C) Copyright 2002 Axis Communications AB, LUND, SWEDEN
45 *!***************************************************************************/
46 /* $Id: virtex.c,v 1.2 2003/02/24 07:50:30 fredriko Exp $ */
47 /****************** INCLUDE FILES SECTION ***********************************/
49 #include <linux/module.h>
50 #include <linux/sched.h>
51 #include <linux/slab.h>
52 #include <linux/errno.h>
53 #include <linux/major.h>
54 #include <linux/kernel.h>
56 #include <linux/string.h>
57 #include <linux/init.h>
58 #include <linux/config.h>
60 #include <linux/hdreg.h>
63 #include <asm/etraxvirtex.h>
65 #include <asm/system.h>
66 #include <asm/svinto.h>
67 #include <asm/uaccess.h>
69 #include <asm/delay.h>
73 /******************* VIRTEX DEFINITION SECTION **************************/
75 #define VIRTEX_DEBUG(x)
77 #define VIRTEX_MAJOR 246 /* EXPERIMENTAL */
79 static const char virtex_name[] = "virtex";
81 /****************** FUNCTION DEFINITION SECTION *************************/
86 // Read register interface for FPGA programming:
88 // FPGA_DONE is connected to S1CD_N G28
89 // FPGA_INIT_N is connected to S1IO_N G27
92 // Write register interface for FPGA programming:
94 // Bit: 15 14 13 12 9 8 7 0
95 // ____________________________________________________
96 // | fpga_write | program | cclk | reserved | cs | data |
97 // |____________|_________|______|__________|____|______|
101 // csp0 = cs_fpga1 = FPGA programming interface
102 // csr0 = cs_fpga2 = register interface towards FPGA construction
104 static volatile short *port_csp0_word_addr;
105 static volatile short *port_csr0_word_addr;
107 static volatile unsigned char open_count;
108 static volatile unsigned char bytes_written;
109 static volatile unsigned long bytes_written_cnt;
110 static volatile unsigned char sync_found;
111 static volatile unsigned char sync_count;
112 static volatile unsigned char sync_ff_count;
114 #define WRITE_FPGA_PROG_REG(data) *port_csp0_word_addr=(data)
115 #define SET_PROGRAM_BIT(data) (data)|(1<<14)
117 #define SET_WRITE_BIT(data) (data)|(1<<15)
118 #define CLR_WRITE_BIT(data) (data)&(~(1<<15))
120 #define SET_CS_BIT(data) (data)|(1<<8)
121 #define CLR_CS_BIT(data) (data)&(~(1<<8))
123 #define SET_CCLK_BIT(data) (data)|(1<<13)
124 #define CLR_CCLK_BIT(data) (data)&(~(1<<13))
126 // Bit in read port G (always inputs)
127 #define READ_INIT (*R_PORT_G_DATA)&(1<<27)
128 #define READ_DONE (*R_PORT_G_DATA)&(1<<28)
131 void start_virtex_program(void)
133 volatile unsigned int i=0;
134 unsigned short reg_data=0;
136 printk("Start writing to FPGA\n");
137 reg_data = SET_CS_BIT(reg_data); // FPGA unselected, PROGRAM bit not set
138 WRITE_FPGA_PROG_REG(reg_data);
140 for(i=0;i<10;i++) { } // at least 300 ns loop
142 reg_data = SET_PROGRAM_BIT(reg_data);
143 WRITE_FPGA_PROG_REG(reg_data);
145 for(i=0;i<10;i++) { } // at least 300 ns loop
147 while(!READ_INIT); // Wait for init
149 reg_data = SET_WRITE_BIT(reg_data);
150 WRITE_FPGA_PROG_REG(reg_data);
152 reg_data = CLR_CS_BIT(reg_data); // FPGA selected, CS is active low
153 WRITE_FPGA_PROG_REG(reg_data);
157 // According to datasheet, bytes should be reversed, it is unknown to me why.
158 unsigned char bit_reverse(unsigned char data)
160 unsigned char in=data;
161 unsigned short out=0;
176 void virtex_program(char* ptr,size_t count)
181 unsigned short reg_data=0;
191 data=(unsigned char)*p++;
194 if(sync_count>=256) {
195 printk("Sync not found, aborting\n");
204 if(sync_ff_count==4) {
206 VIRTEX_DEBUG(printk("Sync found at offset %d\n",sync_count));
215 if(bytes_written==0) {
216 start_virtex_program();
220 reg_data = SET_PROGRAM_BIT(reg_data);
221 reg_data = SET_WRITE_BIT(reg_data);
222 reg_data = CLR_CS_BIT(reg_data);
228 data=(unsigned char)*p++;
229 data=bit_reverse(data);
233 if(tmp_cnt<=32 || c<=32 ) {
234 printk("0x%x ",data);
236 if(tmp_cnt==32 || c==0 ) {
241 reg_data = CLR_CCLK_BIT(reg_data);
242 WRITE_FPGA_PROG_REG(reg_data|(data&0xff));
243 reg_data = SET_CCLK_BIT(reg_data);
244 WRITE_FPGA_PROG_REG(reg_data|(data&0xff));
245 reg_data = CLR_CCLK_BIT(reg_data);
246 WRITE_FPGA_PROG_REG(reg_data|(data&0xff));
254 void stop_virtex_program(void)
256 unsigned short reg_data=0;
258 reg_data = SET_PROGRAM_BIT(reg_data);
259 reg_data = SET_WRITE_BIT(reg_data);
260 reg_data = CLR_CCLK_BIT(reg_data);
262 reg_data = SET_CS_BIT(reg_data); // release CS
263 WRITE_FPGA_PROG_REG(reg_data);
265 reg_data = CLR_WRITE_BIT(reg_data); // release write, important to do!
266 WRITE_FPGA_PROG_REG(reg_data);
267 printk("%d bytes written\n",bytes_written_cnt);
269 printk("FPGA-configuration completed, no errors\n");
271 printk("Error, FPGA-configuration failed\n");
277 virtex_open(struct inode *inode, struct file *filp)
279 port_csp0_word_addr = port_csp0_addr;
281 printk("FPGA Device busy, aborting\n");
294 virtex_release(struct inode *inode, struct file *filp)
297 if(bytes_written!=0)stop_virtex_program();
301 // FPGA programming interface
303 static ssize_t virtex_write(struct file * file, const char * buf,
304 size_t count, loff_t *ppos)
307 VIRTEX_DEBUG(printk("Write FPGA count %d\n", count));
309 ptr=kmalloc(count, GFP_KERNEL);
311 printk("FPGA device, kernel malloc failed (%d) bytes\n",count);
314 if(copy_from_user(ptr, buf, count)) {
315 printk("copy_from_user failed\n");
320 virtex_program(ptr,count);
326 /* Main device API. ioctl's to write or read to/from registers.
329 int virtex_writereg(unsigned short theReg, unsigned short theValue)
331 port_csr0_word_addr[theReg]=theValue;
335 unsigned short virtex_readreg(unsigned short theReg)
337 return(port_csr0_word_addr[theReg]);
342 virtex_ioctl(struct inode *inode, struct file *file,
343 unsigned int cmd, unsigned long arg)
345 if(_IOC_TYPE(cmd) != ETRAXVIRTEX_FPGA_IOCTYPE) {
349 switch (_IOC_NR(cmd)) {
350 case VIRTEX_FPGA_WRITEREG:
351 /* write to an FPGA register */
352 VIRTEX_DEBUG(printk("virtex wr 0x%x = 0x%x\n",
353 VIRTEX_FPGA_ARGREG(arg),
354 VIRTEX_FPGA_ARGVALUE(arg)));
356 return virtex_writereg(VIRTEX_FPGA_ARGREG(arg),
357 VIRTEX_FPGA_ARGVALUE(arg));
358 case VIRTEX_FPGA_READREG:
361 /* read from an FPGA register */
362 VIRTEX_DEBUG(printk("virtex rd 0x%x ",
363 VIRTEX_FPGA_ARGREG(arg)));
364 val = virtex_readreg(VIRTEX_FPGA_ARGREG(arg));
365 VIRTEX_DEBUG(printk("= 0x%x\n", val));
375 static struct file_operations virtex_fops = {
380 release: virtex_release,
388 /* register char device */
389 res = register_chrdev(VIRTEX_MAJOR, virtex_name, &virtex_fops);
391 printk(KERN_ERR "virtex: couldn't get a major number.\n");
395 port_csr0_word_addr = (volatile unsigned short *)
396 ioremap((unsigned long)(MEM_CSR0_START |
397 MEM_NON_CACHEABLE), 16);
399 // see ~/platoon/rel2/platoon/os/linux/arch/cris/mm/init.c
400 // port_csp0_addr = (volatile unsigned long *)
401 // ioremap((unsigned long)(MEM_CSP0_START |
402 // MEM_NON_CACHEABLE), 16);
406 printk("VIRTEX(TM) FPGA driver v1.0, (c) 2002 Axis Communications AB\n");
411 /* this makes sure that virtex_init is called during boot */
413 module_init(virtex_init);
415 /****************** END OF FILE virtex.c ********************************/