3 #include <system_def.h>
10 #include "appl_version.h"
11 #include "appl_fpga.h"
13 #define SWAB32(x) ((x >> 24) | ((x & 0x00FF0000) >> 8) | ((x & 0x0000FF00) << 8) | (x << 24))
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;
23 volatile struct irc_register *fpga_irc[] =
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
35 volatile uint8_t *fpga_irc_state[] =
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)
47 volatile uint8_t *fpga_irc_reset = (volatile uint8_t *)(FPGA_IRC_RESET);
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;
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;
58 volatile uint32_t *fpga_bus_meas_read2 = (volatile uint32_t *)0x80007FF8;
59 volatile uint32_t *fpga_bus_meas_write2 = (volatile uint32_t *)0x80007FFC;
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;
69 /* BUS measurement - values (shifting all bits) */
70 #define MEAS_VAL1 0xAAAAAAAA
71 #define MEAS_VAL2 0x55555555
75 /* Initialze EMC for FPGA */
79 * CS polarity: LOW (ATTENTION: Must match FPGA setup)
80 * Byte line state: Reads are only 32 bits
83 * Write protection: disabled
85 LPC_EMC->StaticConfig0 = 0x00000002;
87 /* Delays - not measured at this point
88 * We're running on 72 MHz, FPGA bus is running on 100 MHz async.
91 * Turnaround: 2 cycles (cca. 28 ns)
93 LPC_EMC->StaticWaitRd0 = 0x1F;
94 LPC_EMC->StaticWaitWr0 = 0x1F;
95 LPC_EMC->StaticWaitTurn0 = 0x01;
97 /* Shift addresses by 2 (32-bit bus) */
98 LPC_SC->SCS &= 0xFFFFFFFE;
100 printf("EMC for FPGA initialized!\n");
103 int fpga_tumbl_set_reset(int reset)
106 *fpga_tumbl_control |= FPGA_TUMBL_CONTROL_REG_RESET_BIT;
108 *fpga_tumbl_control &= ~FPGA_TUMBL_CONTROL_REG_RESET_BIT;
112 int fpga_tumbl_set_halt(int halt)
115 *fpga_tumbl_control |= FPGA_TUMBL_CONTROL_REG_HALT_BIT;
117 *fpga_tumbl_control &= ~FPGA_TUMBL_CONTROL_REG_HALT_BIT;
122 int fpga_tumbl_set_trace(int trace)
125 *fpga_tumbl_control |= FPGA_TUMBL_CONTROL_REG_TRACE_BIT;
127 *fpga_tumbl_control &= ~FPGA_TUMBL_CONTROL_REG_TRACE_BIT;
132 int fpga_tumbl_kick_trace()
136 *fpga_tumbl_trace_kick = 1;
139 /* Make sure it's processed */
140 for (i = 0; i < 32; i++)
144 printf("Tumbl PC: 0x%08X\n", (unsigned int) *fpga_tumbl_pc);
148 void fpga_tumbl_write(unsigned int offset, unsigned char *ptr, int len)
151 unsigned int *iptr = (unsigned int *)ptr;
153 for (i = 0; i < len / 4; i++)
154 fpga_tumbl_base[(offset / 4) + i] = SWAB32(iptr[i]);
158 * Bus measurement - functions can be called via USB interface
160 * 1) Measurement read
161 * 2) Measurement write
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)
168 /* Cannot be on stack due to memory barrier for gcc */
169 static uint32_t a, b;
171 int fpga_measure_bus_read()
175 /* Set the delays are set to highest (default) value */
176 LPC_EMC->StaticWaitRd0 = 0x1F;
178 while (LPC_EMC->StaticWaitRd0 >= 0)
180 for (i = 0; i < 1024; i++)
182 /* Reset the values */
187 /* Read the values several times - so there are two flips at least
188 * NOTE: SDRAM reads / writes may occur in between!
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;
201 if (a != MEAS_VAL1 || b != MEAS_VAL2)
203 if (LPC_EMC->StaticWaitRd0 == 0x1F)
205 printf("ERROR: FPGA bus is not working properly!\n");
210 LPC_EMC->StaticWaitRd0++;
211 printf("FPGA bus: StaticWaitRd0 set to 0x%02X\n", (unsigned int) LPC_EMC->StaticWaitRd0);
217 /* We're good, lower it */
218 if (LPC_EMC->StaticWaitRd0 == 0)
220 printf("FPGA bus: StaticWaitRd0 set to 0x%02X\n", (unsigned int) LPC_EMC->StaticWaitRd0);
224 LPC_EMC->StaticWaitRd0--;
230 int fpga_measure_bus_write()
234 /* Set the delays are set to highest (default) value */
235 LPC_EMC->StaticWaitWr0 = 0x1F;
237 while (LPC_EMC->StaticWaitWr0 >= 0)
239 for (i = 0; i < 1024; i++)
241 /* Make sure there is nothing other going on */
243 *fpga_bus_meas_write1 = 0x00000000;
244 *fpga_bus_meas_write2 = 0x00000000;
249 /* Write the values several times - so there are two flips at least
250 * NOTE: SDRAM reads / writes may occur in between!
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;
261 * Strongly ordered memory
262 * GCC is blocked by volatilness
265 a = *fpga_bus_meas_write1;
266 b = *fpga_bus_meas_write2;
270 if (a != MEAS_VAL1 || b != MEAS_VAL2)
272 if (LPC_EMC->StaticWaitWr0 == 0x1F)
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);
280 LPC_EMC->StaticWaitWr0++;
281 printf("FPGA bus: StaticWaitWr0 set to 0x%02X\n", (unsigned int) LPC_EMC->StaticWaitWr0);
287 /* We're good, lower it */
288 if (LPC_EMC->StaticWaitWr0 == 0)
290 printf("FPGA bus: StaticWaitWr0 set to 0x%02X\n", (unsigned int) LPC_EMC->StaticWaitWr0);
294 LPC_EMC->StaticWaitWr0--;
300 void fpga_set_reconfiguration_lock(int lock)
302 fpga_reconfiguration_locked = lock;
305 int fpga_get_reconfiguration_lock()
307 return fpga_reconfiguration_locked;
317 if (fpga_configured && fpga_reconfiguration_locked)
318 return FPGA_CONF_ERR_RECONF_LOCKED;
320 if (fpga_reconfiguaration_initiated != NULL)
321 fpga_reconfiguaration_initiated();
323 /* Make sure INIT_B is set as input */
324 hal_gpio_direction_input(XC_INIT_PIN);
326 /* PROGRAM_B to low */
327 hal_gpio_set_value(XC_PROGRAM_PIN, 0);
329 /* SUSPEND to low (permamently) */
330 hal_gpio_set_value(XC_SUSPEND_PIN, 0);
332 /* Wait some cycles (minimum: 500 ns) */
333 for (i = 0; i < 4096; i++)
336 /* PROGRAM_B to high */
337 hal_gpio_set_value(XC_PROGRAM_PIN, 1);
339 /* Wait for INIT_B to be high */
342 while (!hal_gpio_get_value(XC_INIT_PIN))
346 hal_gpio_set_value(XC_SUSPEND_PIN, 1);
347 return FPGA_CONF_ERR_RESET_FAIL;
353 /* Use highest EMC delays */
354 LPC_EMC->StaticWaitRd0 = 0x1F;
355 LPC_EMC->StaticWaitWr0 = 0x1F;
357 /* Assert RWDR to WRITE */
358 hal_gpio_set_value(XC_RDWR_PIN, 0);
360 /* Send bin file (NOTE: Bits must be reversed!) via EMC)
365 * 3) send configuration data
368 * INIT_B is LOW in case of a failure
369 * DONE is HIGH in case of a success
371 * When DONE is HIGH, deassert RWDR and do GPIO reconfiguration:
373 * GPIOs need to be reconfigured:
375 * INIT_B - used as reset, triggered LOW right after startup,
376 * change from input to output (OUTPUT DRAIN)
380 magic = (char *)FPGA_CONFIGURATION_FILE_ADDRESS;
382 if (magic[0] != 'F' || magic[1] != 'P' || magic[2] != 'G' || magic[3] != 'A')
384 hal_gpio_set_value(XC_SUSPEND_PIN, 1);
388 size = (*(uint32_t *)(FPGA_CONFIGURATION_FILE_ADDRESS + 4)) >> 1;
389 data = (uint16_t *)(FPGA_CONFIGURATION_FILE_ADDRESS + 4 + sizeof(uint32_t));
391 /* Periodically check for failure */
397 *fpga_configure_line = *data;
402 if (!hal_gpio_get_value(XC_INIT_PIN))
404 hal_gpio_set_value(XC_SUSPEND_PIN, 1);
405 return FPGA_CONF_ERR_WRITE_ERR;
416 /* We're done, deassert RDWR */
417 hal_gpio_set_value(XC_RDWR_PIN, 1);
419 while (!hal_gpio_get_value(XC_DONE_PIN))
421 if (!hal_gpio_get_value(XC_INIT_PIN))
423 hal_gpio_set_value(XC_SUSPEND_PIN, 1);
424 return FPGA_CONF_ERR_CRC_ERR;
428 /* Issue startup clocks with data all 1s (at least 8 recommended) */
429 for (i = 0; i < 16; i++)
430 *fpga_configure_line = 0xFFFF;
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);
435 /* Hold it for some time */
436 for (i = 0; i < 128; i++)
439 /* Use EMC delays obtained through measurement */
440 LPC_EMC->StaticWaitRd0 = 0x07;
441 LPC_EMC->StaticWaitWr0 = 0x03;
444 hal_gpio_direction_output(XC_INIT_PIN, 1);
446 /* Give it some time */
447 for (i = 0; i < 1024; i++)
451 printf("FPGA configured!\n");
453 if (fpga_reconfiguaration_finished != NULL)
454 fpga_reconfiguaration_finished();
456 return FPGA_CONF_SUCESS;