fpga.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * (C) Copyright 2002
  3. * Rich Ireland, Enterasys Networks, rireland@enterasys.com.
  4. * Keith Outwater, keith_outwater@mvis.com.
  5. *
  6. * (C) Copyright 2008
  7. * Andre Schwarz, Matrix Vision GmbH, andre.schwarz@matrix-vision.de
  8. *
  9. * SPDX-License-Identifier: GPL-2.0+
  10. */
  11. #include <common.h>
  12. #include <ACEX1K.h>
  13. #include <command.h>
  14. #include "fpga.h"
  15. #include "mvblm7.h"
  16. #ifdef FPGA_DEBUG
  17. #define fpga_debug(fmt, args...) printf("%s: "fmt, __func__, ##args)
  18. #else
  19. #define fpga_debug(fmt, args...)
  20. #endif
  21. Altera_CYC2_Passive_Serial_fns altera_fns = {
  22. fpga_null_fn,
  23. fpga_config_fn,
  24. fpga_status_fn,
  25. fpga_done_fn,
  26. fpga_wr_fn,
  27. fpga_null_fn,
  28. fpga_null_fn,
  29. };
  30. Altera_desc cyclone2 = {
  31. Altera_CYC2,
  32. passive_serial,
  33. Altera_EP2C20_SIZE,
  34. (void *) &altera_fns,
  35. NULL,
  36. 0
  37. };
  38. DECLARE_GLOBAL_DATA_PTR;
  39. int mvblm7_init_fpga(void)
  40. {
  41. fpga_debug("Initialize FPGA interface\n");
  42. fpga_init();
  43. fpga_add(fpga_altera, &cyclone2);
  44. fpga_config_fn(0, 1, 0);
  45. udelay(60);
  46. return 1;
  47. }
  48. int fpga_null_fn(int cookie)
  49. {
  50. return 0;
  51. }
  52. int fpga_config_fn(int assert, int flush, int cookie)
  53. {
  54. volatile immap_t *im = (volatile immap_t *)CONFIG_SYS_IMMR;
  55. volatile gpio83xx_t *gpio = (volatile gpio83xx_t *)&im->gpio[0];
  56. u32 dvo = gpio->dat;
  57. fpga_debug("SET config : %s\n", assert ? "low" : "high");
  58. if (assert)
  59. dvo |= FPGA_CONFIG;
  60. else
  61. dvo &= ~FPGA_CONFIG;
  62. if (flush)
  63. gpio->dat = dvo;
  64. return assert;
  65. }
  66. int fpga_done_fn(int cookie)
  67. {
  68. volatile immap_t *im = (volatile immap_t *)CONFIG_SYS_IMMR;
  69. volatile gpio83xx_t *gpio = (volatile gpio83xx_t *)&im->gpio[0];
  70. int result = 0;
  71. udelay(10);
  72. fpga_debug("CONF_DONE check ... ");
  73. if (gpio->dat & FPGA_CONF_DONE) {
  74. fpga_debug("high\n");
  75. result = 1;
  76. } else
  77. fpga_debug("low\n");
  78. return result;
  79. }
  80. int fpga_status_fn(int cookie)
  81. {
  82. volatile immap_t *im = (volatile immap_t *)CONFIG_SYS_IMMR;
  83. volatile gpio83xx_t *gpio = (volatile gpio83xx_t *)&im->gpio[0];
  84. int result = 0;
  85. fpga_debug("STATUS check ... ");
  86. if (gpio->dat & FPGA_STATUS) {
  87. fpga_debug("high\n");
  88. result = 1;
  89. } else
  90. fpga_debug("low\n");
  91. return result;
  92. }
  93. int fpga_clk_fn(int assert_clk, int flush, int cookie)
  94. {
  95. volatile immap_t *im = (volatile immap_t *)CONFIG_SYS_IMMR;
  96. volatile gpio83xx_t *gpio = (volatile gpio83xx_t *)&im->gpio[0];
  97. u32 dvo = gpio->dat;
  98. fpga_debug("CLOCK %s\n", assert_clk ? "high" : "low");
  99. if (assert_clk)
  100. dvo |= FPGA_CCLK;
  101. else
  102. dvo &= ~FPGA_CCLK;
  103. if (flush)
  104. gpio->dat = dvo;
  105. return assert_clk;
  106. }
  107. static inline int _write_fpga(u8 val, int dump)
  108. {
  109. volatile immap_t *im = (volatile immap_t *)CONFIG_SYS_IMMR;
  110. volatile gpio83xx_t *gpio = (volatile gpio83xx_t *)&im->gpio[0];
  111. int i;
  112. u32 dvo = gpio->dat;
  113. if (dump)
  114. fpga_debug(" %02x -> ", val);
  115. for (i = 0; i < 8; i++) {
  116. dvo &= ~FPGA_CCLK;
  117. gpio->dat = dvo;
  118. dvo &= ~FPGA_DIN;
  119. if (dump)
  120. fpga_debug("%d ", val&1);
  121. if (val & 1)
  122. dvo |= FPGA_DIN;
  123. gpio->dat = dvo;
  124. dvo |= FPGA_CCLK;
  125. gpio->dat = dvo;
  126. val >>= 1;
  127. }
  128. if (dump)
  129. fpga_debug("\n");
  130. return 0;
  131. }
  132. int fpga_wr_fn(const void *buf, size_t len, int flush, int cookie)
  133. {
  134. unsigned char *data = (unsigned char *) buf;
  135. int i;
  136. fpga_debug("fpga_wr: buf %p / size %d\n", buf, len);
  137. for (i = 0; i < len; i++)
  138. _write_fpga(data[i], 0);
  139. fpga_debug("\n");
  140. return FPGA_SUCCESS;
  141. }