altera.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * (C) Copyright 2003
  3. * Steven Scholz, imc Measurement & Control, steven.scholz@imc-berlin.de
  4. *
  5. * (C) Copyright 2002
  6. * Rich Ireland, Enterasys Networks, rireland@enterasys.com.
  7. *
  8. * SPDX-License-Identifier: GPL-2.0+
  9. */
  10. /*
  11. * Altera FPGA support
  12. */
  13. #include <common.h>
  14. #include <errno.h>
  15. #include <ACEX1K.h>
  16. #include <stratixII.h>
  17. /* Define FPGA_DEBUG to 1 to get debug printf's */
  18. #define FPGA_DEBUG 0
  19. static const struct altera_fpga {
  20. enum altera_family family;
  21. const char *name;
  22. int (*load)(Altera_desc *, const void *, size_t);
  23. int (*dump)(Altera_desc *, const void *, size_t);
  24. int (*info)(Altera_desc *);
  25. } altera_fpga[] = {
  26. #if defined(CONFIG_FPGA_ACEX1K)
  27. { Altera_ACEX1K, "ACEX1K", ACEX1K_load, ACEX1K_dump, ACEX1K_info },
  28. { Altera_CYC2, "ACEX1K", ACEX1K_load, ACEX1K_dump, ACEX1K_info },
  29. #elif defined(CONFIG_FPGA_CYCLON2)
  30. { Altera_ACEX1K, "CycloneII", CYC2_load, CYC2_dump, CYC2_info },
  31. { Altera_CYC2, "CycloneII", CYC2_load, CYC2_dump, CYC2_info },
  32. #endif
  33. #if defined(CONFIG_FPGA_STRATIX_II)
  34. { Altera_StratixII, "StratixII", StratixII_load,
  35. StratixII_dump, StratixII_info },
  36. #endif
  37. };
  38. static int altera_validate(Altera_desc *desc, const char *fn)
  39. {
  40. if (!desc) {
  41. printf("%s: NULL descriptor!\n", fn);
  42. return -EINVAL;
  43. }
  44. if ((desc->family < min_altera_type) ||
  45. (desc->family > max_altera_type)) {
  46. printf("%s: Invalid family type, %d\n", fn, desc->family);
  47. return -EINVAL;
  48. }
  49. if ((desc->iface < min_altera_iface_type) ||
  50. (desc->iface > max_altera_iface_type)) {
  51. printf("%s: Invalid Interface type, %d\n", fn, desc->iface);
  52. return -EINVAL;
  53. }
  54. if (!desc->size) {
  55. printf("%s: NULL part size\n", fn);
  56. return -EINVAL;
  57. }
  58. return 0;
  59. }
  60. static const struct altera_fpga *
  61. altera_desc_to_fpga(Altera_desc *desc, const char *fn)
  62. {
  63. int i;
  64. if (altera_validate(desc, fn)) {
  65. printf("%s: Invalid device descriptor\n", fn);
  66. return NULL;
  67. }
  68. for (i = 0; i < ARRAY_SIZE(altera_fpga); i++) {
  69. if (desc->family == altera_fpga[i].family)
  70. break;
  71. }
  72. if (i == ARRAY_SIZE(altera_fpga)) {
  73. printf("%s: Unsupported family type, %d\n", fn, desc->family);
  74. return NULL;
  75. }
  76. return &altera_fpga[i];
  77. }
  78. int altera_load(Altera_desc *desc, const void *buf, size_t bsize)
  79. {
  80. const struct altera_fpga *fpga = altera_desc_to_fpga(desc, __func__);
  81. if (!fpga)
  82. return FPGA_FAIL;
  83. debug_cond(FPGA_DEBUG, "%s: Launching the %s Loader...\n",
  84. __func__, fpga->name);
  85. if (fpga->load)
  86. return fpga->load(desc, buf, bsize);
  87. return 0;
  88. }
  89. int altera_dump(Altera_desc *desc, const void *buf, size_t bsize)
  90. {
  91. const struct altera_fpga *fpga = altera_desc_to_fpga(desc, __func__);
  92. if (!fpga)
  93. return FPGA_FAIL;
  94. debug_cond(FPGA_DEBUG, "%s: Launching the %s Reader...\n",
  95. __func__, fpga->name);
  96. if (fpga->dump)
  97. return fpga->dump(desc, buf, bsize);
  98. return 0;
  99. }
  100. int altera_info(Altera_desc *desc)
  101. {
  102. const struct altera_fpga *fpga = altera_desc_to_fpga(desc, __func__);
  103. if (!fpga)
  104. return FPGA_FAIL;
  105. printf("Family: \t%s\n", fpga->name);
  106. printf("Interface type:\t");
  107. switch (desc->iface) {
  108. case passive_serial:
  109. printf("Passive Serial (PS)\n");
  110. break;
  111. case passive_parallel_synchronous:
  112. printf("Passive Parallel Synchronous (PPS)\n");
  113. break;
  114. case passive_parallel_asynchronous:
  115. printf("Passive Parallel Asynchronous (PPA)\n");
  116. break;
  117. case passive_serial_asynchronous:
  118. printf("Passive Serial Asynchronous (PSA)\n");
  119. break;
  120. case altera_jtag_mode: /* Not used */
  121. printf("JTAG Mode\n");
  122. break;
  123. case fast_passive_parallel:
  124. printf("Fast Passive Parallel (FPP)\n");
  125. break;
  126. case fast_passive_parallel_security:
  127. printf("Fast Passive Parallel with Security (FPPS)\n");
  128. break;
  129. /* Add new interface types here */
  130. default:
  131. printf("Unsupported interface type, %d\n", desc->iface);
  132. }
  133. printf("Device Size: \t%zd bytes\n"
  134. "Cookie: \t0x%x (%d)\n",
  135. desc->size, desc->cookie, desc->cookie);
  136. if (desc->iface_fns) {
  137. printf("Device Function Table @ 0x%p\n", desc->iface_fns);
  138. if (fpga->info)
  139. fpga->info(desc);
  140. } else {
  141. printf("No Device Function Table.\n");
  142. }
  143. return FPGA_SUCCESS;
  144. }