cmd_fpga.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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
  59. do_fpga (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  60. {
  61. int op, dev = FPGA_INVALID_DEVICE;
  62. size_t data_size = 0;
  63. void *fpga_data = NULL;
  64. char *devstr = getenv("fpga");
  65. char *datastr = getenv("fpgadata");
  66. if ( devstr ) dev = (int)simple_strtoul( devstr, NULL, 16 );
  67. if ( datastr ) fpga_data = (void *)simple_strtoul( datastr, NULL, 16 );
  68. switch ( argc )
  69. {
  70. case 5: /* fpga <op> <dev> <data> <datasize> */
  71. data_size = simple_strtoul( argv[4], NULL, 16 );
  72. case 4: /* fpga <op> <dev> <data> */
  73. fpga_data = (void *)simple_strtoul( argv[3], NULL, 16 );
  74. PRINTF(__FUNCTION__": fpga_data = 0x%x\n", (uint)fpga_data );
  75. case 3: /* fpga <op> <dev | data addr> */
  76. dev = (int)simple_strtoul( argv[2], NULL, 16 );
  77. PRINTF(__FUNCTION__": device = %d\n", dev );
  78. /* FIXME - this is a really weak test */
  79. if (( argc == 3 ) && ( dev > fpga_count() )) { /* must be buffer ptr */
  80. PRINTF(__FUNCTION__": Assuming buffer pointer in arg 3\n");
  81. fpga_data = (void *)dev;
  82. PRINTF(__FUNCTION__": fpga_data = 0x%x\n", (uint)fpga_data );
  83. dev = FPGA_INVALID_DEVICE; /* reset device num */
  84. }
  85. case 2: /* fpga <op> */
  86. op = (int)fpga_get_op( argv[1] );
  87. break;
  88. default:
  89. PRINTF(__FUNCTION__": Too many or too few args (%d)\n", argc );
  90. op = FPGA_NONE; /* force usage display */
  91. break;
  92. }
  93. switch ( op ) {
  94. case FPGA_NONE:
  95. fpga_usage( cmdtp );
  96. break;
  97. case FPGA_INFO:
  98. fpga_info( dev );
  99. break;
  100. case FPGA_LOAD:
  101. fpga_load( dev, fpga_data, data_size );
  102. break;
  103. case FPGA_DUMP:
  104. fpga_dump( dev, fpga_data, data_size );
  105. break;
  106. default:
  107. printf( "Unknown operation.\n" );
  108. fpga_usage( cmdtp );
  109. break;
  110. }
  111. return 0;
  112. }
  113. static void fpga_usage ( cmd_tbl_t *cmdtp )
  114. {
  115. printf( "Usage:\n%s\n", cmdtp->usage );
  116. }
  117. /*
  118. * Map op to supported operations. We don't use a table since we
  119. * would just have to relocate it from flash anyway.
  120. */
  121. static int fpga_get_op( char *opstr )
  122. {
  123. int op = FPGA_NONE;
  124. if (!strcmp ("info", opstr)) {
  125. op = FPGA_INFO;
  126. }
  127. else if (!strcmp ("load", opstr)) {
  128. op = FPGA_LOAD;
  129. }
  130. else if (!strcmp ("dump", opstr)) {
  131. op = FPGA_DUMP;
  132. }
  133. if ( op == FPGA_NONE ) {
  134. printf ("Unknown fpga operation \"%s\"\n", opstr);
  135. }
  136. return op;
  137. }
  138. U_BOOT_CMD(
  139. fpga, 6, 1, do_fpga,
  140. "fpga - loadable FPGA image support\n",
  141. "fpga [operation type] [device number] [image address] [image size]\n"
  142. "fpga operations:\n"
  143. "\tinfo\tlist known device information.\n"
  144. "\tload\tLoad device from memory buffer.\n"
  145. "\tdump\tLoad device to memory buffer.\n"
  146. );
  147. #endif /* CONFIG_FPGA && CONFIG_COMMANDS & CFG_CMD_FPGA */