You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

247 lines
6.7 KiB

  1. /***************************************************************************
  2. * Copyright (C) 2006 by Dominic Rath *
  3. * Dominic.Rath@gmx.de *
  4. * *
  5. * This program is free software; you can redistribute it and/or modify *
  6. * it under the terms of the GNU General Public License as published by *
  7. * the Free Software Foundation; either version 2 of the License, or *
  8. * (at your option) any later version. *
  9. * *
  10. * This program is distributed in the hope that it will be useful, *
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  13. * GNU General Public License for more details. *
  14. * *
  15. * You should have received a copy of the GNU General Public License *
  16. * along with this program; if not, write to the *
  17. * Free Software Foundation, Inc., *
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
  19. ***************************************************************************/
  20. #ifdef HAVE_CONFIG_H
  21. #include "config.h"
  22. #endif
  23. #include "virtex2.h"
  24. #include "xilinx_bit.h"
  25. #include "pld.h"
  26. static int virtex2_set_instr(struct jtag_tap *tap, uint32_t new_instr)
  27. {
  28. if (tap == NULL)
  29. return ERROR_FAIL;
  30. if (buf_get_u32(tap->cur_instr, 0, tap->ir_length) != new_instr) {
  31. struct scan_field field;
  32. field.num_bits = tap->ir_length;
  33. void *t = calloc(DIV_ROUND_UP(field.num_bits, 8), 1);
  34. field.out_value = t;
  35. buf_set_u32(t, 0, field.num_bits, new_instr);
  36. field.in_value = NULL;
  37. jtag_add_ir_scan(tap, &field, TAP_IDLE);
  38. free(t);
  39. }
  40. return ERROR_OK;
  41. }
  42. static int virtex2_send_32(struct pld_device *pld_device,
  43. int num_words, uint32_t *words)
  44. {
  45. struct virtex2_pld_device *virtex2_info = pld_device->driver_priv;
  46. struct scan_field scan_field;
  47. uint8_t *values;
  48. int i;
  49. values = malloc(num_words * 4);
  50. scan_field.num_bits = num_words * 32;
  51. scan_field.out_value = values;
  52. scan_field.in_value = NULL;
  53. for (i = 0; i < num_words; i++)
  54. buf_set_u32(values + 4 * i, 0, 32, flip_u32(*words++, 32));
  55. virtex2_set_instr(virtex2_info->tap, 0x5); /* CFG_IN */
  56. jtag_add_dr_scan(virtex2_info->tap, 1, &scan_field, TAP_DRPAUSE);
  57. free(values);
  58. return ERROR_OK;
  59. }
  60. static inline void virtexflip32(jtag_callback_data_t arg)
  61. {
  62. uint8_t *in = (uint8_t *)arg;
  63. *((uint32_t *)arg) = flip_u32(le_to_h_u32(in), 32);
  64. }
  65. static int virtex2_receive_32(struct pld_device *pld_device,
  66. int num_words, uint32_t *words)
  67. {
  68. struct virtex2_pld_device *virtex2_info = pld_device->driver_priv;
  69. struct scan_field scan_field;
  70. scan_field.num_bits = 32;
  71. scan_field.out_value = NULL;
  72. scan_field.in_value = NULL;
  73. virtex2_set_instr(virtex2_info->tap, 0x4); /* CFG_OUT */
  74. while (num_words--) {
  75. scan_field.in_value = (uint8_t *)words;
  76. jtag_add_dr_scan(virtex2_info->tap, 1, &scan_field, TAP_DRPAUSE);
  77. jtag_add_callback(virtexflip32, (jtag_callback_data_t)words);
  78. words++;
  79. }
  80. return ERROR_OK;
  81. }
  82. static int virtex2_read_stat(struct pld_device *pld_device, uint32_t *status)
  83. {
  84. uint32_t data[5];
  85. jtag_add_tlr();
  86. data[0] = 0xaa995566; /* synch word */
  87. data[1] = 0x2800E001; /* Type 1, read, address 7, 1 word */
  88. data[2] = 0x20000000; /* NOOP (Type 1, read, address 0, 0 words */
  89. data[3] = 0x20000000; /* NOOP */
  90. data[4] = 0x20000000; /* NOOP */
  91. virtex2_send_32(pld_device, 5, data);
  92. virtex2_receive_32(pld_device, 1, status);
  93. jtag_execute_queue();
  94. LOG_DEBUG("status: 0x%8.8" PRIx32 "", *status);
  95. return ERROR_OK;
  96. }
  97. static int virtex2_load(struct pld_device *pld_device, const char *filename)
  98. {
  99. struct virtex2_pld_device *virtex2_info = pld_device->driver_priv;
  100. struct xilinx_bit_file bit_file;
  101. int retval;
  102. unsigned int i;
  103. struct scan_field field;
  104. field.in_value = NULL;
  105. retval = xilinx_read_bit_file(&bit_file, filename);
  106. if (retval != ERROR_OK)
  107. return retval;
  108. virtex2_set_instr(virtex2_info->tap, 0xb); /* JPROG_B */
  109. jtag_execute_queue();
  110. jtag_add_sleep(1000);
  111. virtex2_set_instr(virtex2_info->tap, 0x5); /* CFG_IN */
  112. jtag_execute_queue();
  113. for (i = 0; i < bit_file.length; i++)
  114. bit_file.data[i] = flip_u32(bit_file.data[i], 8);
  115. field.num_bits = bit_file.length * 8;
  116. field.out_value = bit_file.data;
  117. jtag_add_dr_scan(virtex2_info->tap, 1, &field, TAP_DRPAUSE);
  118. jtag_execute_queue();
  119. jtag_add_tlr();
  120. virtex2_set_instr(virtex2_info->tap, 0xc); /* JSTART */
  121. jtag_add_runtest(13, TAP_IDLE);
  122. virtex2_set_instr(virtex2_info->tap, 0x3f); /* BYPASS */
  123. virtex2_set_instr(virtex2_info->tap, 0x3f); /* BYPASS */
  124. virtex2_set_instr(virtex2_info->tap, 0xc); /* JSTART */
  125. jtag_add_runtest(13, TAP_IDLE);
  126. virtex2_set_instr(virtex2_info->tap, 0x3f); /* BYPASS */
  127. jtag_execute_queue();
  128. return ERROR_OK;
  129. }
  130. COMMAND_HANDLER(virtex2_handle_read_stat_command)
  131. {
  132. struct pld_device *device;
  133. uint32_t status;
  134. if (CMD_ARGC < 1)
  135. return ERROR_COMMAND_SYNTAX_ERROR;
  136. unsigned dev_id;
  137. COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], dev_id);
  138. device = get_pld_device_by_num(dev_id);
  139. if (!device) {
  140. command_print(CMD_CTX, "pld device '#%s' is out of bounds", CMD_ARGV[0]);
  141. return ERROR_OK;
  142. }
  143. virtex2_read_stat(device, &status);
  144. command_print(CMD_CTX, "virtex2 status register: 0x%8.8" PRIx32 "", status);
  145. return ERROR_OK;
  146. }
  147. PLD_DEVICE_COMMAND_HANDLER(virtex2_pld_device_command)
  148. {
  149. struct jtag_tap *tap;
  150. struct virtex2_pld_device *virtex2_info;
  151. if (CMD_ARGC < 2)
  152. return ERROR_COMMAND_SYNTAX_ERROR;
  153. tap = jtag_tap_by_string(CMD_ARGV[1]);
  154. if (tap == NULL) {
  155. command_print(CMD_CTX, "Tap: %s does not exist", CMD_ARGV[1]);
  156. return ERROR_OK;
  157. }
  158. virtex2_info = malloc(sizeof(struct virtex2_pld_device));
  159. virtex2_info->tap = tap;
  160. pld->driver_priv = virtex2_info;
  161. return ERROR_OK;
  162. }
  163. static const struct command_registration virtex2_exec_command_handlers[] = {
  164. {
  165. .name = "read_stat",
  166. .mode = COMMAND_EXEC,
  167. .handler = virtex2_handle_read_stat_command,
  168. .help = "read status register",
  169. .usage = "pld_num",
  170. },
  171. COMMAND_REGISTRATION_DONE
  172. };
  173. static const struct command_registration virtex2_command_handler[] = {
  174. {
  175. .name = "virtex2",
  176. .mode = COMMAND_ANY,
  177. .help = "Virtex-II specific commands",
  178. .usage = "",
  179. .chain = virtex2_exec_command_handlers,
  180. },
  181. COMMAND_REGISTRATION_DONE
  182. };
  183. struct pld_driver virtex2_pld = {
  184. .name = "virtex2",
  185. .commands = virtex2_command_handler,
  186. .pld_device_command = &virtex2_pld_device_command,
  187. .load = &virtex2_load,
  188. };