altera-ps-spi.c 7.7 KB

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