]> rtime.felk.cvut.cz Git - fpga/lx-cpu1/lx-rocon.git/blob - sw/app/rocon/appl_fpga.c
Fix typo in IRC5 register address base
[fpga/lx-cpu1/lx-rocon.git] / sw / app / rocon / appl_fpga.c
1 #include <types.h>
2 #include <cpu_def.h>
3 #include <system_def.h>
4 #include <string.h>
5 #include <stdio.h>
6 #include <endian.h>
7 #include <hal_gpio.h>
8
9 #include "appl_defs.h"
10 #include "appl_version.h"
11 #include "appl_fpga.h"
12
13 #define SWAB32(x) ((x >> 24) | ((x & 0x00FF0000) >> 8) | ((x & 0x0000FF00) << 8) | (x << 24))
14
15 /* Registers in FPGA */
16 volatile uint32_t *fpga_tumbl_control = (volatile uint32_t *)FPGA_TUMBL_CONTROL_REG;
17 volatile uint32_t *fpga_tumbl_trace_kick = (volatile uint32_t *)FPGA_TUMBL_TRACE_KICK_REG;
18 volatile uint32_t *fpga_tumbl_pc = (volatile uint32_t *)FPGA_TUMBL_PC;
19 volatile uint32_t *fpga_tumbl_base = (volatile uint32_t *)FPGA_TUMBL_IMEM_BASE;
20 volatile uint32_t *fpga_tumbl_imem = (volatile uint32_t *)FPGA_TUMBL_IMEM_BASE;
21 volatile uint32_t *fpga_tumbl_dmem = (volatile uint32_t *)FPGA_TUMBL_DMEM_BASE;
22
23 volatile struct irc_register *fpga_irc[] = 
24 {
25   (volatile struct irc_register *)FPGA_IRC0_BASE,
26   (volatile struct irc_register *)FPGA_IRC1_BASE,
27   (volatile struct irc_register *)FPGA_IRC2_BASE,
28   (volatile struct irc_register *)FPGA_IRC3_BASE,
29   (volatile struct irc_register *)FPGA_IRC4_BASE,
30   (volatile struct irc_register *)FPGA_IRC5_BASE,
31   (volatile struct irc_register *)FPGA_IRC6_BASE,
32   (volatile struct irc_register *)FPGA_IRC7_BASE
33 };
34
35 volatile uint8_t *fpga_irc_state[] = 
36 {
37   (volatile uint8_t *)(FPGA_IRC0_BASE + 0x40),
38   (volatile uint8_t *)(FPGA_IRC1_BASE + 0x40),
39   (volatile uint8_t *)(FPGA_IRC2_BASE + 0x40),
40   (volatile uint8_t *)(FPGA_IRC3_BASE + 0x40),
41   (volatile uint8_t *)(FPGA_IRC4_BASE + 0x40),
42   (volatile uint8_t *)(FPGA_IRC5_BASE + 0x40),
43   (volatile uint8_t *)(FPGA_IRC6_BASE + 0x40),
44   (volatile uint8_t *)(FPGA_IRC7_BASE + 0x40)
45 };
46
47 volatile uint8_t *fpga_irc_reset  = (volatile uint8_t *)(FPGA_IRC_RESET);
48
49 /* Variables for configuration */
50 volatile uint16_t *fpga_configure_line = (volatile uint16_t *)0x80007FF0;
51 int fpga_configured = 0;
52 int fpga_reconfiguration_locked = 1;
53
54 /* BUS measurement - registers to measure the delay necessary for reading and writing */
55 volatile uint32_t *fpga_bus_meas_read1  = (volatile uint32_t *)0x80007FF0;
56 volatile uint32_t *fpga_bus_meas_write1 = (volatile uint32_t *)0x80007FF4;
57
58 volatile uint32_t *fpga_bus_meas_read2  = (volatile uint32_t *)0x80007FF8;
59 volatile uint32_t *fpga_bus_meas_write2 = (volatile uint32_t *)0x80007FFC;
60
61 /* LX Master */
62 volatile uint32_t *fpga_lx_master_transmitter_base = (volatile uint32_t *)FPGA_LX_MASTER_TRANSMITTER_BASE;
63 volatile uint32_t *fpga_lx_master_transmitter_reg = (volatile uint32_t *)FPGA_LX_MASTER_TRANSMITTER_REG;
64 volatile uint32_t *fpga_lx_master_receiver_base = (volatile uint32_t *)FPGA_LX_MASTER_RECEIVER_BASE;
65 volatile uint32_t *fpga_lx_master_receiver_reg = (volatile uint32_t *)FPGA_LX_MASTER_RECEIVER_REG;
66 volatile uint32_t *fpga_lx_master_reset = (volatile uint32_t *)FPGA_LX_MASTER_RESET;
67 volatile uint32_t *fpga_lx_master_conf  = (volatile uint32_t *)FPGA_CONFIGURATION_FILE_ADDRESS;
68
69 /* BUS measurement - values (shifting all bits) */
70 #define MEAS_VAL1 0xAAAAAAAA
71 #define MEAS_VAL2 0x55555555
72
73 void fpga_init()
74 {
75   /* Initialze EMC for FPGA */
76
77   /* Settings:
78    * 32 bus width
79    * CS polarity: LOW (ATTENTION: Must match FPGA setup)
80    * Byte line state: Reads are only 32 bits
81    * Extended wait: off
82    * Buffer: disabled
83    * Write protection: disabled
84    */
85   LPC_EMC->StaticConfig0 = 0x00000002;
86
87   /* Delays - not measured at this point
88    * We're running on 72 MHz, FPGA bus is running on 100 MHz async.
89    * Read: 32 cycles
90    * Write: 33 cycles
91    * Turnaround: 2 cycles (cca. 28 ns)
92    */
93   LPC_EMC->StaticWaitRd0 = 0x1F;
94   LPC_EMC->StaticWaitWr0 = 0x1F;
95   LPC_EMC->StaticWaitTurn0 = 0x01;
96
97   /* Shift addresses by 2 (32-bit bus) */
98   LPC_SC->SCS &= 0xFFFFFFFE;
99
100   printf("EMC for FPGA initialized!\n");
101 }
102
103 int fpga_tumbl_set_reset(int reset)
104 {   
105   if (reset)
106     *fpga_tumbl_control |= FPGA_TUMBL_CONTROL_REG_RESET_BIT;
107   else
108     *fpga_tumbl_control &= ~FPGA_TUMBL_CONTROL_REG_RESET_BIT;
109   return 0;
110 }
111
112 int fpga_tumbl_set_halt(int halt)
113 {
114   if (halt)
115     *fpga_tumbl_control |= FPGA_TUMBL_CONTROL_REG_HALT_BIT; 
116   else
117     *fpga_tumbl_control &= ~FPGA_TUMBL_CONTROL_REG_HALT_BIT;
118   
119   return 0;
120 }
121
122 int fpga_tumbl_set_trace(int trace)
123 {  
124   if (trace)
125     *fpga_tumbl_control |= FPGA_TUMBL_CONTROL_REG_TRACE_BIT; 
126   else
127     *fpga_tumbl_control &= ~FPGA_TUMBL_CONTROL_REG_TRACE_BIT;
128   
129   return 0;
130 }
131
132 int fpga_tumbl_kick_trace()
133 {
134   int i;
135   
136   *fpga_tumbl_trace_kick = 1;
137   __memory_barrier();
138
139   /* Make sure it's processed */
140   for (i = 0; i < 32; i++)
141   {}
142   
143   __memory_barrier();
144   printf("Tumbl PC: 0x%08X\n", (unsigned int) *fpga_tumbl_pc);
145   return 0;
146 }
147
148 void fpga_tumbl_write(unsigned int offset, unsigned char *ptr, int len)
149 {
150   int i;
151   unsigned int *iptr = (unsigned int *)ptr;
152   
153   for (i = 0; i < len / 4; i++)
154     fpga_tumbl_base[(offset / 4) + i] = SWAB32(iptr[i]);
155 }
156
157 /* 
158  * Bus measurement - functions can be called via USB interface
159  * Proper usage:
160  * 1) Measurement read
161  * 2) Measurement write
162  * 3) Set turnaround
163  *      bus is not pipelined, therefore
164  *      just necessary delay for I/O to enter
165  *      high impedance state (synchronous clocking => only 1 cycle necessary)
166  */
167
168 /* Cannot be on stack due to memory barrier for gcc */
169 static uint32_t a, b;
170
171 int fpga_measure_bus_read()
172 {
173   int i;
174   
175   /* Set the delays are set to highest (default) value */
176   LPC_EMC->StaticWaitRd0 = 0x1F;
177   
178   while (LPC_EMC->StaticWaitRd0 >= 0)
179   {
180     for (i = 0; i < 1024; i++)
181     {
182       /* Reset the values */
183       __memory_barrier();
184       a = 0xFFFFFFFF;
185       b = 0xFFFFFFFF;
186       __memory_barrier();
187       /* Read the values several times - so there are two flips at least
188        * NOTE: SDRAM reads / writes may occur in between!
189        */
190       a = *fpga_bus_meas_read1;
191       b = *fpga_bus_meas_read2;
192       a = *fpga_bus_meas_read1;
193       b = *fpga_bus_meas_read2;
194       a = *fpga_bus_meas_read1;
195       b = *fpga_bus_meas_read2;
196       a = *fpga_bus_meas_read1;
197       b = *fpga_bus_meas_read2;
198       __memory_barrier();
199       
200       /* Verify */
201       if (a != MEAS_VAL1 || b != MEAS_VAL2)
202       {
203         if (LPC_EMC->StaticWaitRd0 == 0x1F)
204         {
205           printf("ERROR: FPGA bus is not working properly!\n"); 
206           return 1;
207         }
208         else
209         {
210           LPC_EMC->StaticWaitRd0++;
211           printf("FPGA bus: StaticWaitRd0 set to 0x%02X\n", (unsigned int) LPC_EMC->StaticWaitRd0);
212           return 0;
213         }
214       }
215     }
216     
217     /* We're good, lower it */
218     if (LPC_EMC->StaticWaitRd0 == 0)
219     {
220       printf("FPGA bus: StaticWaitRd0 set to 0x%02X\n", (unsigned int) LPC_EMC->StaticWaitRd0);
221       break;
222     }
223     else
224       LPC_EMC->StaticWaitRd0--;
225   }
226
227   return 0;
228 }
229
230 int fpga_measure_bus_write()
231 {
232   int i;
233   
234   /* Set the delays are set to highest (default) value */
235   LPC_EMC->StaticWaitWr0 = 0x1F;
236
237   while (LPC_EMC->StaticWaitWr0 >= 0)
238   {
239     for (i = 0; i < 1024; i++)
240     {
241       /* Make sure there is nothing other going on */
242       __memory_barrier();
243       *fpga_bus_meas_write1 = 0x00000000;
244       *fpga_bus_meas_write2 = 0x00000000;
245       __memory_barrier();
246       a = 0xFFFFFFFF;
247       b = 0xFFFFFFFF;
248       __memory_barrier();
249       /* Write the values several times - so there are two flips at least
250        * NOTE: SDRAM reads / writes may occur in between!
251        */
252       *fpga_bus_meas_write1 = MEAS_VAL1;
253       *fpga_bus_meas_write2 = MEAS_VAL2;
254       *fpga_bus_meas_write1 = MEAS_VAL1;
255       *fpga_bus_meas_write2 = MEAS_VAL2;
256       *fpga_bus_meas_write1 = MEAS_VAL1;
257       *fpga_bus_meas_write2 = MEAS_VAL2;
258       *fpga_bus_meas_write1 = MEAS_VAL1;
259       *fpga_bus_meas_write2 = MEAS_VAL2;
260       /* 
261        * Strongly ordered memory
262        * GCC is blocked by volatilness
263        */
264       __memory_barrier();
265       a = *fpga_bus_meas_write1;
266       b = *fpga_bus_meas_write2;
267       __memory_barrier();
268       
269       /* Verify */
270       if (a != MEAS_VAL1 || b != MEAS_VAL2)
271       {
272         if (LPC_EMC->StaticWaitWr0 == 0x1F)
273         {
274           printf("ERROR: FPGA bus is not working properly!\n");
275           printf("a = 0x%08X, b = 0x%08X\n", (unsigned int) a, (unsigned int) b);
276           return 1;
277         }
278         else
279         {
280           LPC_EMC->StaticWaitWr0++;
281           printf("FPGA bus: StaticWaitWr0 set to 0x%02X\n", (unsigned int) LPC_EMC->StaticWaitWr0);
282           return 0;
283         }
284       }
285     }
286     
287     /* We're good, lower it */
288     if (LPC_EMC->StaticWaitWr0 == 0)
289     {
290       printf("FPGA bus: StaticWaitWr0 set to 0x%02X\n", (unsigned int) LPC_EMC->StaticWaitWr0);
291       break;
292     }
293     else
294       LPC_EMC->StaticWaitWr0--;
295   }
296
297   return 0;
298 }
299
300 void fpga_set_reconfiguration_lock(int lock)
301 {
302   fpga_reconfiguration_locked = lock;
303 }
304
305 int fpga_get_reconfiguration_lock()
306 {
307   return fpga_reconfiguration_locked;
308 }
309
310 int fpga_configure()
311 {
312   int i, j;
313   uint16_t *data;
314   char *magic;
315   uint32_t size;
316
317   if (fpga_configured && fpga_reconfiguration_locked)
318     return FPGA_CONF_ERR_RECONF_LOCKED;
319
320   if (fpga_reconfiguaration_initiated != NULL)
321     fpga_reconfiguaration_initiated();
322
323   /* Make sure INIT_B is set as input */
324   hal_gpio_direction_input(XC_INIT_PIN);
325
326   /* PROGRAM_B to low */
327   hal_gpio_set_value(XC_PROGRAM_PIN, 0);
328
329   /* SUSPEND to low (permamently) */
330   hal_gpio_set_value(XC_SUSPEND_PIN, 0);
331
332   /* Wait some cycles (minimum: 500 ns) */
333   for (i = 0; i < 4096; i++)
334     {}
335
336   /* PROGRAM_B to high */
337   hal_gpio_set_value(XC_PROGRAM_PIN, 1);
338
339   /* Wait for INIT_B to be high */
340   j = 0;
341
342   while (!hal_gpio_get_value(XC_INIT_PIN))
343   {
344     if (j >= 4096)
345     {
346       hal_gpio_set_value(XC_SUSPEND_PIN, 1);
347       return FPGA_CONF_ERR_RESET_FAIL;
348     }
349
350     j++;
351   }
352   
353   /* Use highest EMC delays */
354   LPC_EMC->StaticWaitRd0 = 0x1F;
355   LPC_EMC->StaticWaitWr0 = 0x1F;
356
357   /* Assert RWDR to WRITE */
358   hal_gpio_set_value(XC_RDWR_PIN, 0);
359
360   /* Send bin file (NOTE: Bits must be reversed!) via EMC)
361    *
362    * Embedded steps:
363    * 1) sync
364    * 2) device ID check
365    * 3) send configuration data
366    * 4) crc check
367    *
368    * INIT_B is LOW in case of a failure
369    * DONE is HIGH in case of a success
370    *
371    * When DONE is HIGH, deassert RWDR and do GPIO reconfiguration:
372    *
373    * GPIOs need to be reconfigured:
374    *
375    * INIT_B - used as reset, triggered LOW right after startup,
376    *          change from input to output (OUTPUT DRAIN)
377    */
378
379   /* Get size */
380   magic = (char *)FPGA_CONFIGURATION_FILE_ADDRESS;
381
382   if (magic[0] != 'F' || magic[1] != 'P' || magic[2] != 'G' || magic[3] != 'A')
383   {
384     hal_gpio_set_value(XC_SUSPEND_PIN, 1);
385     return 1;
386   }
387
388   size = (*(uint32_t *)(FPGA_CONFIGURATION_FILE_ADDRESS + 4)) >> 1;
389   data = (uint16_t *)(FPGA_CONFIGURATION_FILE_ADDRESS + 4 + sizeof(uint32_t));
390
391   /* Periodically check for failure */
392   i = 0;
393   j = 0;
394
395   while (i < size)
396   {
397     *fpga_configure_line = *data;
398
399     if (j >= 128)
400     {
401       /* Check state */
402       if (!hal_gpio_get_value(XC_INIT_PIN))
403       {
404         hal_gpio_set_value(XC_SUSPEND_PIN, 1);
405         return FPGA_CONF_ERR_WRITE_ERR;
406       }
407
408       j = 0;
409     }
410
411     j++;
412     i++;
413     data++;
414   }
415
416   /* We're done, deassert RDWR */
417   hal_gpio_set_value(XC_RDWR_PIN, 1);
418
419   while (!hal_gpio_get_value(XC_DONE_PIN))
420   {
421     if (!hal_gpio_get_value(XC_INIT_PIN))
422     {
423       hal_gpio_set_value(XC_SUSPEND_PIN, 1);
424       return FPGA_CONF_ERR_CRC_ERR;
425     }
426   }
427
428   /* Issue startup clocks with data all 1s (at least 8 recommended) */
429   for (i = 0; i < 16; i++)
430     *fpga_configure_line = 0xFFFF;
431
432   /* In our design, INIT_B is used as reset, convert it to output, and trigger it */
433   hal_gpio_direction_output(XC_INIT_PIN, 0);
434
435   /* Hold it for some time */
436   for (i = 0; i < 128; i++)
437     {}
438     
439   /* Use EMC delays obtained through measurement */
440   LPC_EMC->StaticWaitRd0 = 0x07;
441   LPC_EMC->StaticWaitWr0 = 0x03;
442
443   /* Lift the reset */
444   hal_gpio_direction_output(XC_INIT_PIN, 1);
445   
446    /* Give it some time */
447   for (i = 0; i < 1024; i++)
448     {}
449   
450   fpga_configured = 1;
451   printf("FPGA configured!\n");
452
453   if (fpga_reconfiguaration_finished != NULL)
454     fpga_reconfiguaration_finished();
455
456   return FPGA_CONF_SUCESS;
457 }