ice40-spi.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. * FPGA Manager Driver for Lattice iCE40.
  3. *
  4. * Copyright (c) 2016 Joel Holdsworth
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; version 2 of the License.
  9. *
  10. * This driver adds support to the FPGA manager for configuring the SRAM of
  11. * Lattice iCE40 FPGAs through slave SPI.
  12. */
  13. #include <linux/fpga/fpga-mgr.h>
  14. #include <linux/gpio/consumer.h>
  15. #include <linux/module.h>
  16. #include <linux/of_gpio.h>
  17. #include <linux/spi/spi.h>
  18. #include <linux/stringify.h>
  19. #define ICE40_SPI_MAX_SPEED 25000000 /* Hz */
  20. #define ICE40_SPI_MIN_SPEED 1000000 /* Hz */
  21. #define ICE40_SPI_RESET_DELAY 1 /* us (>200ns) */
  22. #define ICE40_SPI_HOUSEKEEPING_DELAY 1200 /* us */
  23. #define ICE40_SPI_NUM_ACTIVATION_BYTES DIV_ROUND_UP(49, 8)
  24. struct ice40_fpga_priv {
  25. struct spi_device *dev;
  26. struct gpio_desc *reset;
  27. struct gpio_desc *cdone;
  28. };
  29. static enum fpga_mgr_states ice40_fpga_ops_state(struct fpga_manager *mgr)
  30. {
  31. struct ice40_fpga_priv *priv = mgr->priv;
  32. return gpiod_get_value(priv->cdone) ? FPGA_MGR_STATE_OPERATING :
  33. FPGA_MGR_STATE_UNKNOWN;
  34. }
  35. static int ice40_fpga_ops_write_init(struct fpga_manager *mgr,
  36. struct fpga_image_info *info,
  37. const char *buf, size_t count)
  38. {
  39. struct ice40_fpga_priv *priv = mgr->priv;
  40. struct spi_device *dev = priv->dev;
  41. struct spi_message message;
  42. struct spi_transfer assert_cs_then_reset_delay = {
  43. .cs_change = 1,
  44. .delay_usecs = ICE40_SPI_RESET_DELAY
  45. };
  46. struct spi_transfer housekeeping_delay_then_release_cs = {
  47. .delay_usecs = ICE40_SPI_HOUSEKEEPING_DELAY
  48. };
  49. int ret;
  50. if ((info->flags & FPGA_MGR_PARTIAL_RECONFIG)) {
  51. dev_err(&dev->dev,
  52. "Partial reconfiguration is not supported\n");
  53. return -ENOTSUPP;
  54. }
  55. /* Lock the bus, assert CRESET_B and SS_B and delay >200ns */
  56. spi_bus_lock(dev->master);
  57. gpiod_set_value(priv->reset, 1);
  58. spi_message_init(&message);
  59. spi_message_add_tail(&assert_cs_then_reset_delay, &message);
  60. ret = spi_sync_locked(dev, &message);
  61. /* Come out of reset */
  62. gpiod_set_value(priv->reset, 0);
  63. /* Abort if the chip-select failed */
  64. if (ret)
  65. goto fail;
  66. /* Check CDONE is de-asserted i.e. the FPGA is reset */
  67. if (gpiod_get_value(priv->cdone)) {
  68. dev_err(&dev->dev, "Device reset failed, CDONE is asserted\n");
  69. ret = -EIO;
  70. goto fail;
  71. }
  72. /* Wait for the housekeeping to complete, and release SS_B */
  73. spi_message_init(&message);
  74. spi_message_add_tail(&housekeeping_delay_then_release_cs, &message);
  75. ret = spi_sync_locked(dev, &message);
  76. fail:
  77. spi_bus_unlock(dev->master);
  78. return ret;
  79. }
  80. static int ice40_fpga_ops_write(struct fpga_manager *mgr,
  81. const char *buf, size_t count)
  82. {
  83. struct ice40_fpga_priv *priv = mgr->priv;
  84. return spi_write(priv->dev, buf, count);
  85. }
  86. static int ice40_fpga_ops_write_complete(struct fpga_manager *mgr,
  87. struct fpga_image_info *info)
  88. {
  89. struct ice40_fpga_priv *priv = mgr->priv;
  90. struct spi_device *dev = priv->dev;
  91. const u8 padding[ICE40_SPI_NUM_ACTIVATION_BYTES] = {0};
  92. /* Check CDONE is asserted */
  93. if (!gpiod_get_value(priv->cdone)) {
  94. dev_err(&dev->dev,
  95. "CDONE was not asserted after firmware transfer\n");
  96. return -EIO;
  97. }
  98. /* Send of zero-padding to activate the firmware */
  99. return spi_write(dev, padding, sizeof(padding));
  100. }
  101. static const struct fpga_manager_ops ice40_fpga_ops = {
  102. .state = ice40_fpga_ops_state,
  103. .write_init = ice40_fpga_ops_write_init,
  104. .write = ice40_fpga_ops_write,
  105. .write_complete = ice40_fpga_ops_write_complete,
  106. };
  107. static int ice40_fpga_probe(struct spi_device *spi)
  108. {
  109. struct device *dev = &spi->dev;
  110. struct ice40_fpga_priv *priv;
  111. int ret;
  112. priv = devm_kzalloc(&spi->dev, sizeof(*priv), GFP_KERNEL);
  113. if (!priv)
  114. return -ENOMEM;
  115. priv->dev = spi;
  116. /* Check board setup data. */
  117. if (spi->max_speed_hz > ICE40_SPI_MAX_SPEED) {
  118. dev_err(dev, "SPI speed is too high, maximum speed is "
  119. __stringify(ICE40_SPI_MAX_SPEED) "\n");
  120. return -EINVAL;
  121. }
  122. if (spi->max_speed_hz < ICE40_SPI_MIN_SPEED) {
  123. dev_err(dev, "SPI speed is too low, minimum speed is "
  124. __stringify(ICE40_SPI_MIN_SPEED) "\n");
  125. return -EINVAL;
  126. }
  127. if (spi->mode & SPI_CPHA) {
  128. dev_err(dev, "Bad SPI mode, CPHA not supported\n");
  129. return -EINVAL;
  130. }
  131. /* Set up the GPIOs */
  132. priv->cdone = devm_gpiod_get(dev, "cdone", GPIOD_IN);
  133. if (IS_ERR(priv->cdone)) {
  134. ret = PTR_ERR(priv->cdone);
  135. dev_err(dev, "Failed to get CDONE GPIO: %d\n", ret);
  136. return ret;
  137. }
  138. priv->reset = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
  139. if (IS_ERR(priv->reset)) {
  140. ret = PTR_ERR(priv->reset);
  141. dev_err(dev, "Failed to get CRESET_B GPIO: %d\n", ret);
  142. return ret;
  143. }
  144. /* Register with the FPGA manager */
  145. return fpga_mgr_register(dev, "Lattice iCE40 FPGA Manager",
  146. &ice40_fpga_ops, priv);
  147. }
  148. static int ice40_fpga_remove(struct spi_device *spi)
  149. {
  150. fpga_mgr_unregister(&spi->dev);
  151. return 0;
  152. }
  153. static const struct of_device_id ice40_fpga_of_match[] = {
  154. { .compatible = "lattice,ice40-fpga-mgr", },
  155. {},
  156. };
  157. MODULE_DEVICE_TABLE(of, ice40_fpga_of_match);
  158. static struct spi_driver ice40_fpga_driver = {
  159. .probe = ice40_fpga_probe,
  160. .remove = ice40_fpga_remove,
  161. .driver = {
  162. .name = "ice40spi",
  163. .of_match_table = of_match_ptr(ice40_fpga_of_match),
  164. },
  165. };
  166. module_spi_driver(ice40_fpga_driver);
  167. MODULE_AUTHOR("Joel Holdsworth <joel@airwebreathe.org.uk>");
  168. MODULE_DESCRIPTION("Lattice iCE40 FPGA Manager");
  169. MODULE_LICENSE("GPL v2");