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.
 
 
 
 
 
 

219 lines
5.5 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. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  19. ***************************************************************************/
  20. #ifdef HAVE_CONFIG_H
  21. #include "config.h"
  22. #endif
  23. #include "pld.h"
  24. #include "jtag.h"
  25. #include "command.h"
  26. #include "log.h"
  27. #include "time_support.h"
  28. #include <stdlib.h>
  29. #include <unistd.h>
  30. #include <sys/types.h>
  31. #include <sys/stat.h>
  32. #include <fcntl.h>
  33. #include <string.h>
  34. #include <sys/time.h>
  35. #include <time.h>
  36. /* pld drivers
  37. */
  38. extern pld_driver_t virtex2_pld;
  39. pld_driver_t *pld_drivers[] =
  40. {
  41. &virtex2_pld,
  42. NULL,
  43. };
  44. pld_device_t *pld_devices;
  45. static command_t *pld_cmd;
  46. int handle_pld_devices_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  47. int handle_pld_device_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  48. int handle_pld_load_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  49. int pld_init(struct command_context_s *cmd_ctx)
  50. {
  51. if (pld_devices)
  52. {
  53. register_command(cmd_ctx, pld_cmd, "devices", handle_pld_devices_command, COMMAND_EXEC,
  54. "list configured pld devices");
  55. register_command(cmd_ctx, pld_cmd, "load", handle_pld_load_command, COMMAND_EXEC,
  56. "load configuration <file> into programmable logic device");
  57. }
  58. return ERROR_OK;
  59. }
  60. pld_device_t *get_pld_device_by_num(int num)
  61. {
  62. pld_device_t *p;
  63. int i = 0;
  64. for (p = pld_devices; p; p = p->next)
  65. {
  66. if (i++ == num)
  67. {
  68. return p;
  69. }
  70. }
  71. return NULL;
  72. }
  73. /* pld device <driver> [driver_options ...]
  74. */
  75. int handle_pld_device_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  76. {
  77. int i;
  78. int found = 0;
  79. if (argc < 1)
  80. {
  81. WARNING("incomplete 'pld bank' configuration");
  82. return ERROR_OK;
  83. }
  84. for (i = 0; pld_drivers[i]; i++)
  85. {
  86. if (strcmp(args[0], pld_drivers[i]->name) == 0)
  87. {
  88. pld_device_t *p, *c;
  89. /* register pld specific commands */
  90. if (pld_drivers[i]->register_commands(cmd_ctx) != ERROR_OK)
  91. {
  92. ERROR("couldn't register '%s' commands", args[0]);
  93. exit(-1);
  94. }
  95. c = malloc(sizeof(pld_device_t));
  96. c->driver = pld_drivers[i];
  97. c->next = NULL;
  98. if (pld_drivers[i]->pld_device_command(cmd_ctx, cmd, args, argc, c) != ERROR_OK)
  99. {
  100. ERROR("'%s' driver rejected pld device", args[0]);
  101. free(c);
  102. return ERROR_OK;
  103. }
  104. /* put pld device in linked list */
  105. if (pld_devices)
  106. {
  107. /* find last pld device */
  108. for (p = pld_devices; p && p->next; p = p->next);
  109. if (p)
  110. p->next = c;
  111. }
  112. else
  113. {
  114. pld_devices = c;
  115. }
  116. found = 1;
  117. }
  118. }
  119. /* no matching pld driver found */
  120. if (!found)
  121. {
  122. ERROR("pld driver '%s' not found", args[0]);
  123. exit(-1);
  124. }
  125. return ERROR_OK;
  126. }
  127. int handle_pld_devices_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  128. {
  129. pld_device_t *p;
  130. int i = 0;
  131. if (!pld_devices)
  132. {
  133. command_print(cmd_ctx, "no pld devices configured");
  134. return ERROR_OK;
  135. }
  136. for (p = pld_devices; p; p = p->next)
  137. {
  138. command_print(cmd_ctx, "#%i: %s", i++, p->driver->name);
  139. }
  140. return ERROR_OK;
  141. }
  142. int handle_pld_load_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  143. {
  144. int retval;
  145. struct timeval start, end, duration;
  146. pld_device_t *p;
  147. gettimeofday(&start, NULL);
  148. if (argc < 2)
  149. {
  150. command_print(cmd_ctx, "usage: pld load <device#> <file>");
  151. return ERROR_OK;
  152. }
  153. p = get_pld_device_by_num(strtoul(args[0], NULL, 0));
  154. if (!p)
  155. {
  156. command_print(cmd_ctx, "pld device '#%s' is out of bounds", args[0]);
  157. return ERROR_OK;
  158. }
  159. if ((retval = p->driver->load(p, args[1])) != ERROR_OK)
  160. {
  161. command_print(cmd_ctx, "failed loading file %s to pld device %i",
  162. args[1], strtoul(args[0], NULL, 0));
  163. switch (retval)
  164. {
  165. }
  166. }
  167. else
  168. {
  169. gettimeofday(&end, NULL);
  170. timeval_subtract(&duration, &end, &start);
  171. command_print(cmd_ctx, "loaded file %s to pld device %i in %is %ius",
  172. args[1], strtoul(args[0], NULL, 0), duration.tv_sec, duration.tv_usec);
  173. }
  174. return ERROR_OK;
  175. }
  176. int pld_register_commands(struct command_context_s *cmd_ctx)
  177. {
  178. pld_cmd = register_command(cmd_ctx, NULL, "pld", NULL, COMMAND_ANY, "programmable logic device commands");
  179. register_command(cmd_ctx, pld_cmd, "device", handle_pld_device_command, COMMAND_CONFIG, NULL);
  180. return ERROR_OK;
  181. }