altera-ps-spi.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Altera Passive Serial SPI Driver
  4. *
  5. * Copyright (c) 2017 United Western Technologies, Corporation
  6. *
  7. * Joshua Clayton <stillcompiling@gmail.com>
  8. *
  9. * Manage Altera FPGA firmware that is loaded over SPI using the passive
  10. * serial configuration method.
  11. * Firmware must be in binary "rbf" format.
  12. * Works on Arria 10, Cyclone V and Stratix V. Should work on Cyclone series.
  13. * May work on other Altera FPGAs.
  14. */
  15. #include <linux/bitrev.h>
  16. #include <linux/delay.h>
  17. #include <linux/fpga/fpga-mgr.h>
  18. #include <linux/gpio/consumer.h>
  19. #include <linux/module.h>
  20. #include <linux/of.h>
  21. #include <linux/spi/spi.h>
  22. #include <linux/sizes.h>
  23. enum altera_ps_devtype {
  24. CYCLONE5,
  25. ARRIA10,
  26. };
  27. struct altera_ps_data {
  28. enum altera_ps_devtype devtype;
  29. int status_wait_min_us;
  30. int status_wait_max_us;
  31. int t_cfg_us;
  32. int t_st2ck_us;
  33. };
  34. struct altera_ps_conf {
  35. struct gpio_desc *config;
  36. struct gpio_desc *confd;
  37. struct gpio_desc *status;
  38. struct spi_device *spi;
  39. const struct altera_ps_data *data;
  40. u32 info_flags;
  41. char mgr_name[64];
  42. };
  43. /* | Arria 10 | Cyclone5 | Stratix5 |
  44. * t_CF2ST0 | [; 600] | [; 600] | [; 600] |ns
  45. * t_CFG | [2;] | [2;] | [2;] |µs
  46. * t_STATUS | [268; 3000] | [268; 1506] | [268; 1506] |µs
  47. * t_CF2ST1 | [; 3000] | [; 1506] | [; 1506] |µs
  48. * t_CF2CK | [3010;] | [1506;] | [1506;] |µs
  49. * t_ST2CK | [10;] | [2;] | [2;] |µs
  50. * t_CD2UM | [175; 830] | [175; 437] | [175; 437] |µs
  51. */
  52. static struct altera_ps_data c5_data = {
  53. /* these values for Cyclone5 are compatible with Stratix5 */
  54. .devtype = CYCLONE5,
  55. .status_wait_min_us = 268,
  56. .status_wait_max_us = 1506,
  57. .t_cfg_us = 2,
  58. .t_st2ck_us = 2,
  59. };
  60. static struct altera_ps_data a10_data = {
  61. .devtype = ARRIA10,
  62. .status_wait_min_us = 268, /* min(t_STATUS) */
  63. .status_wait_max_us = 3000, /* max(t_CF2ST1) */
  64. .t_cfg_us = 2, /* max { min(t_CFG), max(tCF2ST0) } */
  65. .t_st2ck_us = 10, /* min(t_ST2CK) */
  66. };
  67. static const struct of_device_id of_ef_match[] = {
  68. { .compatible = "altr,fpga-passive-serial", .data = &c5_data },
  69. { .compatible = "altr,fpga-arria10-passive-serial", .data = &a10_data },
  70. {}
  71. };
  72. MODULE_DEVICE_TABLE(of, of_ef_match);
  73. static enum fpga_mgr_states altera_ps_state(struct fpga_manager *mgr)
  74. {
  75. struct altera_ps_conf *conf = mgr->priv;
  76. if (gpiod_get_value_cansleep(conf->status))
  77. return FPGA_MGR_STATE_RESET;
  78. return FPGA_MGR_STATE_UNKNOWN;
  79. }
  80. static inline void altera_ps_delay(int delay_us)
  81. {
  82. if (delay_us > 10)
  83. usleep_range(delay_us, delay_us + 5);
  84. else
  85. udelay(delay_us);
  86. }
  87. static int altera_ps_write_init(struct fpga_manager *mgr,
  88. struct fpga_image_info *info,
  89. const char *buf, size_t count)
  90. {
  91. struct altera_ps_conf *conf = mgr->priv;
  92. int min, max, waits;
  93. int i;
  94. conf->info_flags = info->flags;
  95. if (info->flags & FPGA_MGR_PARTIAL_RECONFIG) {
  96. dev_err(&mgr->dev, "Partial reconfiguration not supported.\n");
  97. return -EINVAL;
  98. }
  99. gpiod_set_value_cansleep(conf->config, 1);
  100. /* wait min reset pulse time */
  101. altera_ps_delay(conf->data->t_cfg_us);
  102. if (!gpiod_get_value_cansleep(conf->status)) {
  103. dev_err(&mgr->dev, "Status pin failed to show a reset\n");
  104. return -EIO;
  105. }
  106. gpiod_set_value_cansleep(conf->config, 0);
  107. min = conf->data->status_wait_min_us;
  108. max = conf->data->status_wait_max_us;
  109. waits = max / min;
  110. if (max % min)
  111. waits++;
  112. /* wait for max { max(t_STATUS), max(t_CF2ST1) } */
  113. for (i = 0; i < waits; i++) {
  114. usleep_range(min, min + 10);
  115. if (!gpiod_get_value_cansleep(conf->status)) {
  116. /* wait for min(t_ST2CK)*/
  117. altera_ps_delay(conf->data->t_st2ck_us);
  118. return 0;
  119. }
  120. }
  121. dev_err(&mgr->dev, "Status pin not ready.\n");
  122. return -EIO;
  123. }
  124. static void rev_buf(char *buf, size_t len)
  125. {
  126. u32 *fw32 = (u32 *)buf;
  127. size_t extra_bytes = (len & 0x03);
  128. const u32 *fw_end = (u32 *)(buf + len - extra_bytes);
  129. /* set buffer to lsb first */
  130. while (fw32 < fw_end) {
  131. *fw32 = bitrev8x4(*fw32);
  132. fw32++;
  133. }
  134. if (extra_bytes) {
  135. buf = (char *)fw_end;
  136. while (extra_bytes) {
  137. *buf = bitrev8(*buf);
  138. buf++;
  139. extra_bytes--;
  140. }
  141. }
  142. }
  143. static int altera_ps_write(struct fpga_manager *mgr, const char *buf,
  144. size_t count)
  145. {
  146. struct altera_ps_conf *conf = mgr->priv;
  147. const char *fw_data = buf;
  148. const char *fw_data_end = fw_data + count;
  149. while (fw_data < fw_data_end) {
  150. int ret;
  151. size_t stride = min_t(size_t, fw_data_end - fw_data, SZ_4K);
  152. if (!(conf->info_flags & FPGA_MGR_BITSTREAM_LSB_FIRST))
  153. rev_buf((char *)fw_data, stride);
  154. ret = spi_write(conf->spi, fw_data, stride);
  155. if (ret) {
  156. dev_err(&mgr->dev, "spi error in firmware write: %d\n",
  157. ret);
  158. return ret;
  159. }
  160. fw_data += stride;
  161. }
  162. return 0;
  163. }
  164. static int altera_ps_write_complete(struct fpga_manager *mgr,
  165. struct fpga_image_info *info)
  166. {
  167. struct altera_ps_conf *conf = mgr->priv;
  168. static const char dummy[] = {0};
  169. int ret;
  170. if (gpiod_get_value_cansleep(conf->status)) {
  171. dev_err(&mgr->dev, "Error during configuration.\n");
  172. return -EIO;
  173. }
  174. if (conf->confd) {
  175. if (!gpiod_get_raw_value_cansleep(conf->confd)) {
  176. dev_err(&mgr->dev, "CONF_DONE is inactive!\n");
  177. return -EIO;
  178. }
  179. }
  180. /*
  181. * After CONF_DONE goes high, send two additional falling edges on DCLK
  182. * to begin initialization and enter user mode
  183. */
  184. ret = spi_write(conf->spi, dummy, 1);
  185. if (ret) {
  186. dev_err(&mgr->dev, "spi error during end sequence: %d\n", ret);
  187. return ret;
  188. }
  189. return 0;
  190. }
  191. static const struct fpga_manager_ops altera_ps_ops = {
  192. .state = altera_ps_state,
  193. .write_init = altera_ps_write_init,
  194. .write = altera_ps_write,
  195. .write_complete = altera_ps_write_complete,
  196. };
  197. static int altera_ps_probe(struct spi_device *spi)
  198. {
  199. struct altera_ps_conf *conf;
  200. struct fpga_manager *mgr;
  201. conf = devm_kzalloc(&spi->dev, sizeof(*conf), GFP_KERNEL);
  202. if (!conf)
  203. return -ENOMEM;
  204. conf->data = spi_get_device_match_data(spi);
  205. conf->spi = spi;
  206. conf->config = devm_gpiod_get(&spi->dev, "nconfig", GPIOD_OUT_LOW);
  207. if (IS_ERR(conf->config)) {
  208. dev_err(&spi->dev, "Failed to get config gpio: %ld\n",
  209. PTR_ERR(conf->config));
  210. return PTR_ERR(conf->config);
  211. }
  212. conf->status = devm_gpiod_get(&spi->dev, "nstat", GPIOD_IN);
  213. if (IS_ERR(conf->status)) {
  214. dev_err(&spi->dev, "Failed to get status gpio: %ld\n",
  215. PTR_ERR(conf->status));
  216. return PTR_ERR(conf->status);
  217. }
  218. conf->confd = devm_gpiod_get_optional(&spi->dev, "confd", GPIOD_IN);
  219. if (IS_ERR(conf->confd)) {
  220. dev_err(&spi->dev, "Failed to get confd gpio: %ld\n",
  221. PTR_ERR(conf->confd));
  222. return PTR_ERR(conf->confd);
  223. } else if (!conf->confd) {
  224. dev_warn(&spi->dev, "Not using confd gpio");
  225. }
  226. /* Register manager with unique name */
  227. snprintf(conf->mgr_name, sizeof(conf->mgr_name), "%s %s",
  228. dev_driver_string(&spi->dev), dev_name(&spi->dev));
  229. mgr = devm_fpga_mgr_register(&spi->dev, conf->mgr_name,
  230. &altera_ps_ops, conf);
  231. return PTR_ERR_OR_ZERO(mgr);
  232. }
  233. static const struct spi_device_id altera_ps_spi_ids[] = {
  234. { "cyclone-ps-spi", (uintptr_t)&c5_data },
  235. { "fpga-passive-serial", (uintptr_t)&c5_data },
  236. { "fpga-arria10-passive-serial", (uintptr_t)&a10_data },
  237. {}
  238. };
  239. MODULE_DEVICE_TABLE(spi, altera_ps_spi_ids);
  240. static struct spi_driver altera_ps_driver = {
  241. .driver = {
  242. .name = "altera-ps-spi",
  243. .of_match_table = of_ef_match,
  244. },
  245. .id_table = altera_ps_spi_ids,
  246. .probe = altera_ps_probe,
  247. };
  248. module_spi_driver(altera_ps_driver)
  249. MODULE_LICENSE("GPL v2");
  250. MODULE_AUTHOR("Joshua Clayton <stillcompiling@gmail.com>");
  251. MODULE_DESCRIPTION("Module to load Altera FPGA firmware over SPI");