fpga_config.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*
  2. * (C) Copyright 2012
  3. * Valentin Lontgchamp, Keymile AG, valentin.longchamp@keymile.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., 51 Franklin Street, Fifth Floor, Boston,
  21. * MA 02110-1301 USA
  22. */
  23. #include <common.h>
  24. #include <i2c.h>
  25. #include <asm/errno.h>
  26. /* GPIO Pin from kirkwood connected to PROGRAM_B pin of the xilinx FPGA */
  27. #define KM_XLX_PROGRAM_B_PIN 39
  28. #define BOCO_ADDR 0x10
  29. #define ID_REG 0x00
  30. #define BOCO2_ID 0x5b
  31. static int check_boco2(void)
  32. {
  33. int ret;
  34. u8 id;
  35. ret = i2c_read(BOCO_ADDR, ID_REG, 1, &id, 1);
  36. if (ret) {
  37. printf("%s: error reading the BOCO id !!\n", __func__);
  38. return ret;
  39. }
  40. return (id == BOCO2_ID);
  41. }
  42. static int boco_clear_bits(u8 reg, u8 flags)
  43. {
  44. int ret;
  45. u8 regval;
  46. /* give access to the EEPROM from FPGA */
  47. ret = i2c_read(BOCO_ADDR, reg, 1, &regval, 1);
  48. if (ret) {
  49. printf("%s: error reading the BOCO @%#x !!\n",
  50. __func__, reg);
  51. return ret;
  52. }
  53. regval &= ~flags;
  54. ret = i2c_write(BOCO_ADDR, reg, 1, &regval, 1);
  55. if (ret) {
  56. printf("%s: error writing the BOCO @%#x !!\n",
  57. __func__, reg);
  58. return ret;
  59. }
  60. return 0;
  61. }
  62. static int boco_set_bits(u8 reg, u8 flags)
  63. {
  64. int ret;
  65. u8 regval;
  66. /* give access to the EEPROM from FPGA */
  67. ret = i2c_read(BOCO_ADDR, reg, 1, &regval, 1);
  68. if (ret) {
  69. printf("%s: error reading the BOCO @%#x !!\n",
  70. __func__, reg);
  71. return ret;
  72. }
  73. regval |= flags;
  74. ret = i2c_write(BOCO_ADDR, reg, 1, &regval, 1);
  75. if (ret) {
  76. printf("%s: error writing the BOCO @%#x !!\n",
  77. __func__, reg);
  78. return ret;
  79. }
  80. return 0;
  81. }
  82. #define SPI_REG 0x06
  83. #define CFG_EEPROM 0x02
  84. #define FPGA_PROG 0x04
  85. #define FPGA_INIT_B 0x10
  86. #define FPGA_DONE 0x20
  87. static int fpga_done(void)
  88. {
  89. int ret = 0;
  90. u8 regval;
  91. /* this is only supported with the boco2 design */
  92. if (!check_boco2())
  93. return 0;
  94. ret = i2c_read(BOCO_ADDR, SPI_REG, 1, &regval, 1);
  95. if (ret) {
  96. printf("%s: error reading the BOCO @%#x !!\n",
  97. __func__, SPI_REG);
  98. return 0;
  99. }
  100. return regval & FPGA_DONE ? 1 : 0;
  101. }
  102. int skip;
  103. int trigger_fpga_config(void)
  104. {
  105. int ret = 0;
  106. /* if the FPGA is already configured, we do not want to
  107. * reconfigure it */
  108. skip = 0;
  109. if (fpga_done()) {
  110. printf("PCIe FPGA config: skipped\n");
  111. skip = 1;
  112. return 0;
  113. }
  114. if (check_boco2()) {
  115. /* we have a BOCO2, this has to be triggered here */
  116. /* make sure the FPGA_can access the EEPROM */
  117. ret = boco_clear_bits(SPI_REG, CFG_EEPROM);
  118. if (ret)
  119. return ret;
  120. /* trigger the config start */
  121. ret = boco_clear_bits(SPI_REG, FPGA_PROG | FPGA_INIT_B);
  122. if (ret)
  123. return ret;
  124. /* small delay for the pulse */
  125. udelay(10);
  126. /* up signal for pulse end */
  127. ret = boco_set_bits(SPI_REG, FPGA_PROG);
  128. if (ret)
  129. return ret;
  130. /* finally, raise INIT_B to remove the config delay */
  131. ret = boco_set_bits(SPI_REG, FPGA_INIT_B);
  132. if (ret)
  133. return ret;
  134. } else {
  135. /* we do it the old way, with the gpio pin */
  136. kw_gpio_set_valid(KM_XLX_PROGRAM_B_PIN, 1);
  137. kw_gpio_direction_output(KM_XLX_PROGRAM_B_PIN, 0);
  138. /* small delay for the pulse */
  139. udelay(10);
  140. kw_gpio_direction_input(KM_XLX_PROGRAM_B_PIN);
  141. }
  142. return 0;
  143. }
  144. int wait_for_fpga_config(void)
  145. {
  146. int ret = 0;
  147. u8 spictrl;
  148. u32 timeout = 20000;
  149. if (skip)
  150. return 0;
  151. if (!check_boco2()) {
  152. /* we do not have BOCO2, this is not really used */
  153. return 0;
  154. }
  155. printf("PCIe FPGA config:");
  156. do {
  157. ret = i2c_read(BOCO_ADDR, SPI_REG, 1, &spictrl, 1);
  158. if (ret) {
  159. printf("%s: error reading the BOCO spictrl !!\n",
  160. __func__);
  161. return ret;
  162. }
  163. if (timeout-- == 0) {
  164. printf(" FPGA_DONE timeout\n");
  165. return -EFAULT;
  166. }
  167. udelay(10);
  168. } while (!(spictrl & FPGA_DONE));
  169. printf(" done\n");
  170. return 0;
  171. }
  172. #define PRST1 0x4
  173. #define PCIE_RST 0x10
  174. #define TRAFFIC_RST 0x04
  175. int fpga_reset(void)
  176. {
  177. int ret = 0;
  178. u8 resets;
  179. if (!check_boco2()) {
  180. /* we do not have BOCO2, this is not really used */
  181. return 0;
  182. }
  183. /* if we have skipped, we only want to reset the PCIe part */
  184. resets = skip ? PCIE_RST : PCIE_RST | TRAFFIC_RST;
  185. ret = boco_clear_bits(PRST1, resets);
  186. if (ret)
  187. return ret;
  188. /* small delay for the pulse */
  189. udelay(10);
  190. ret = boco_set_bits(PRST1, resets);
  191. if (ret)
  192. return ret;
  193. return 0;
  194. }
  195. /* the FPGA was configured, we configure the BOCO2 so that the EEPROM
  196. * is available from the Bobcat SPI bus */
  197. int toggle_eeprom_spi_bus(void)
  198. {
  199. int ret = 0;
  200. if (!check_boco2()) {
  201. /* we do not have BOCO2, this is not really used */
  202. return 0;
  203. }
  204. ret = boco_set_bits(SPI_REG, CFG_EEPROM);
  205. if (ret)
  206. return ret;
  207. return 0;
  208. }