cmd_fpga.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * (C) Copyright 2000, 2001
  3. * Rich Ireland, Enterasys Networks, rireland@enterasys.com.
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. *
  23. */
  24. /*
  25. * FPGA support
  26. */
  27. #include <common.h>
  28. #include <command.h>
  29. #if (CONFIG_COMMANDS & CFG_CMD_NET)
  30. #include <net.h>
  31. #endif
  32. #include <fpga.h>
  33. #if 0
  34. #define FPGA_DEBUG
  35. #endif
  36. #ifdef FPGA_DEBUG
  37. #define PRINTF(fmt,args...) printf (fmt ,##args)
  38. #else
  39. #define PRINTF(fmt,args...)
  40. #endif
  41. #if defined (CONFIG_FPGA) && ( CONFIG_COMMANDS & CFG_CMD_FPGA )
  42. /* Local functions */
  43. static void fpga_usage (cmd_tbl_t * cmdtp);
  44. static int fpga_get_op (char *opstr);
  45. /* Local defines */
  46. #define FPGA_NONE -1
  47. #define FPGA_INFO 0
  48. #define FPGA_LOAD 1
  49. #define FPGA_DUMP 3
  50. /* ------------------------------------------------------------------------- */
  51. /* command form:
  52. * fpga <op> <device number> <data addr> <datasize>
  53. * where op is 'load', 'dump', or 'info'
  54. * If there is no device number field, the fpga environment variable is used.
  55. * If there is no data addr field, the fpgadata environment variable is used.
  56. * The info command requires no data address field.
  57. */
  58. int do_fpga (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
  59. {
  60. int op, dev = FPGA_INVALID_DEVICE;
  61. size_t data_size = 0;
  62. void *fpga_data = NULL;
  63. char *devstr = getenv ("fpga");
  64. char *datastr = getenv ("fpgadata");
  65. int rc = FPGA_FAIL;
  66. if (devstr)
  67. dev = (int) simple_strtoul (devstr, NULL, 16);
  68. if (datastr)
  69. fpga_data = (void *) simple_strtoul (datastr, NULL, 16);
  70. switch (argc) {
  71. case 5: /* fpga <op> <dev> <data> <datasize> */
  72. data_size = simple_strtoul (argv[4], NULL, 16);
  73. case 4: /* fpga <op> <dev> <data> */
  74. fpga_data = (void *) simple_strtoul (argv[3], NULL, 16);
  75. PRINTF (__FUNCTION__ ": fpga_data = 0x%x\n",
  76. (uint) fpga_data);
  77. case 3: /* fpga <op> <dev | data addr> */
  78. dev = (int) simple_strtoul (argv[2], NULL, 16);
  79. PRINTF (__FUNCTION__ ": device = %d\n", dev);
  80. /* FIXME - this is a really weak test */
  81. if ((argc == 3) && (dev > fpga_count ())) { /* must be buffer ptr */
  82. PRINTF (__FUNCTION__
  83. ": Assuming buffer pointer in arg 3\n");
  84. fpga_data = (void *) dev;
  85. PRINTF (__FUNCTION__ ": fpga_data = 0x%x\n",
  86. (uint) fpga_data);
  87. dev = FPGA_INVALID_DEVICE; /* reset device num */
  88. }
  89. case 2: /* fpga <op> */
  90. op = (int) fpga_get_op (argv[1]);
  91. break;
  92. default:
  93. PRINTF (__FUNCTION__ ": Too many or too few args (%d)\n",
  94. argc);
  95. op = FPGA_NONE; /* force usage display */
  96. break;
  97. }
  98. switch (op) {
  99. case FPGA_NONE:
  100. fpga_usage (cmdtp);
  101. break;
  102. case FPGA_INFO:
  103. rc = fpga_info (dev);
  104. break;
  105. case FPGA_LOAD:
  106. rc = fpga_load (dev, fpga_data, data_size);
  107. break;
  108. case FPGA_DUMP:
  109. rc = fpga_dump (dev, fpga_data, data_size);
  110. break;
  111. default:
  112. printf ("Unknown operation.\n");
  113. fpga_usage (cmdtp);
  114. break;
  115. }
  116. return (rc);
  117. }
  118. static void fpga_usage (cmd_tbl_t * cmdtp)
  119. {
  120. printf ("Usage:\n%s\n", cmdtp->usage);
  121. }
  122. /*
  123. * Map op to supported operations. We don't use a table since we
  124. * would just have to relocate it from flash anyway.
  125. */
  126. static int fpga_get_op (char *opstr)
  127. {
  128. int op = FPGA_NONE;
  129. if (!strcmp ("info", opstr)) {
  130. op = FPGA_INFO;
  131. } else if (!strcmp ("load", opstr)) {
  132. op = FPGA_LOAD;
  133. } else if (!strcmp ("dump", opstr)) {
  134. op = FPGA_DUMP;
  135. }
  136. if (op == FPGA_NONE) {
  137. printf ("Unknown fpga operation \"%s\"\n", opstr);
  138. }
  139. return op;
  140. }
  141. U_BOOT_CMD (fpga, 6, 1, do_fpga,
  142. "fpga - loadable FPGA image support\n",
  143. "fpga [operation type] [device number] [image address] [image size]\n"
  144. "fpga operations:\n"
  145. "\tinfo\tlist known device information.\n"
  146. "\tload\tLoad device from memory buffer.\n"
  147. "\tdump\tLoad device to memory buffer.\n");
  148. #endif /* CONFIG_FPGA && CONFIG_COMMANDS & CFG_CMD_FPGA */