fpga.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * Copyright (C) 2016 Stefan Roese <sr@denx.de>
  3. *
  4. * SPDX-License-Identifier: GPL-2.0+
  5. */
  6. #include <common.h>
  7. #include <altera.h>
  8. #include <errno.h>
  9. #include <asm/gpio.h>
  10. #include <asm/io.h>
  11. #include <asm/arch/cpu.h>
  12. #include <asm/arch/soc.h>
  13. #include <asm/arch-mvebu/spi.h>
  14. #include "theadorable.h"
  15. /*
  16. * FPGA programming support
  17. */
  18. static int fpga_pre_fn(int cookie)
  19. {
  20. int gpio_config = COOKIE2CONFIG(cookie);
  21. int gpio_done = COOKIE2DONE(cookie);
  22. int ret;
  23. debug("%s (%d): cookie=%08x gpio_config=%d gpio_done=%d\n",
  24. __func__, __LINE__, cookie, gpio_config, gpio_done);
  25. /* Configure config pin */
  26. /* Set to output */
  27. ret = gpio_request(gpio_config, "CONFIG");
  28. if (ret < 0)
  29. return ret;
  30. gpio_direction_output(gpio_config, 1);
  31. /* Configure done pin */
  32. /* Set to input */
  33. ret = gpio_request(gpio_done, "DONE");
  34. if (ret < 0)
  35. return ret;
  36. gpio_direction_input(gpio_done);
  37. return 0;
  38. }
  39. static int fpga_config_fn(int assert, int flush, int cookie)
  40. {
  41. int gpio_config = COOKIE2CONFIG(cookie);
  42. debug("%s (%d): cookie=%08x gpio_config=%d\n",
  43. __func__, __LINE__, cookie, gpio_config);
  44. if (assert)
  45. gpio_set_value(gpio_config, 1);
  46. else
  47. gpio_set_value(gpio_config, 0);
  48. return 0;
  49. }
  50. static int fpga_write_fn(const void *buf, size_t len, int flush, int cookie)
  51. {
  52. int spi_bus = COOKIE2SPI_BUS(cookie);
  53. int spi_dev = COOKIE2SPI_DEV(cookie);
  54. struct kwspi_registers *reg;
  55. u32 control_reg;
  56. u32 config_reg;
  57. void *dst;
  58. /*
  59. * Write data to FPGA attached to SPI bus via SPI direct write.
  60. * This results in the fastest and easiest way to program the
  61. * bitstream into the FPGA.
  62. */
  63. debug("%s (%d): cookie=%08x spi_bus=%d spi_dev=%d\n",
  64. __func__, __LINE__, cookie, spi_bus, spi_dev);
  65. if (spi_bus == 0) {
  66. reg = (struct kwspi_registers *)MVEBU_REGISTER(0x10600);
  67. dst = (void *)SPI_BUS0_DEV1_BASE;
  68. } else {
  69. reg = (struct kwspi_registers *)MVEBU_REGISTER(0x10680);
  70. dst = (void *)SPI_BUS1_DEV2_BASE;
  71. }
  72. /* Configure SPI controller for direct access mode */
  73. control_reg = readl(&reg->ctrl);
  74. config_reg = readl(&reg->cfg);
  75. writel(0x00000214, &reg->cfg); /* 27MHz clock */
  76. writel(0x00000000, &reg->dw_cfg); /* don't de-asset CS */
  77. writel(KWSPI_CSN_ACT, &reg->ctrl); /* activate CS */
  78. /* Copy data to the SPI direct mapped window */
  79. memcpy(dst, buf, len);
  80. /* Restore original register values */
  81. writel(control_reg, &reg->ctrl);
  82. writel(config_reg, &reg->cfg);
  83. return 0;
  84. }
  85. /* Returns the state of CONF_DONE Pin */
  86. static int fpga_done_fn(int cookie)
  87. {
  88. int gpio_done = COOKIE2DONE(cookie);
  89. unsigned long ts;
  90. debug("%s (%d): cookie=%08x gpio_done=%d\n",
  91. __func__, __LINE__, cookie, gpio_done);
  92. ts = get_timer(0);
  93. do {
  94. if (gpio_get_value(gpio_done))
  95. return 0;
  96. } while (get_timer(ts) < 1000);
  97. /* timeout so return error */
  98. return -ENODEV;
  99. }
  100. static altera_board_specific_func stratixv_fns = {
  101. .pre = fpga_pre_fn,
  102. .config = fpga_config_fn,
  103. .write = fpga_write_fn,
  104. .done = fpga_done_fn,
  105. };
  106. static Altera_desc altera_fpga[] = {
  107. {
  108. /* Family */
  109. Altera_StratixV,
  110. /* Interface type */
  111. passive_serial,
  112. /* No limitation as additional data will be ignored */
  113. -1,
  114. /* Device function table */
  115. (void *)&stratixv_fns,
  116. /* Base interface address specified in driver */
  117. NULL,
  118. /* Cookie implementation */
  119. /*
  120. * In this 32bit word the following information is coded:
  121. * Bit 31 ... Bit 0
  122. * SPI-Bus | SPI-Dev | Config-Pin | Done-Pin
  123. */
  124. FPGA_COOKIE(0, 1, 26, 7)
  125. },
  126. {
  127. /* Family */
  128. Altera_StratixV,
  129. /* Interface type */
  130. passive_serial,
  131. /* No limitation as additional data will be ignored */
  132. -1,
  133. /* Device function table */
  134. (void *)&stratixv_fns,
  135. /* Base interface address specified in driver */
  136. NULL,
  137. /* Cookie implementation */
  138. /*
  139. * In this 32bit word the following information is coded:
  140. * Bit 31 ... Bit 0
  141. * SPI-Bus | SPI-Dev | Config-Pin | Done-Pin
  142. */
  143. FPGA_COOKIE(1, 2, 29, 9)
  144. },
  145. };
  146. /* Add device descriptor to FPGA device table */
  147. void board_fpga_add(void)
  148. {
  149. int i;
  150. fpga_init();
  151. for (i = 0; i < ARRAY_SIZE(altera_fpga); i++)
  152. fpga_add(fpga_altera, &altera_fpga[i]);
  153. }