lattice-sysconfig-spi.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Lattice FPGA programming over slave SPI sysCONFIG interface.
  4. */
  5. #include <linux/of.h>
  6. #include <linux/spi/spi.h>
  7. #include "lattice-sysconfig.h"
  8. static const u32 ecp5_spi_max_speed_hz = 60000000;
  9. static int sysconfig_spi_cmd_transfer(struct sysconfig_priv *priv,
  10. const void *tx_buf, size_t tx_len,
  11. void *rx_buf, size_t rx_len)
  12. {
  13. struct spi_device *spi = to_spi_device(priv->dev);
  14. return spi_write_then_read(spi, tx_buf, tx_len, rx_buf, rx_len);
  15. }
  16. static int sysconfig_spi_bitstream_burst_init(struct sysconfig_priv *priv)
  17. {
  18. const u8 lsc_bitstream_burst[] = SYSCONFIG_LSC_BITSTREAM_BURST;
  19. struct spi_device *spi = to_spi_device(priv->dev);
  20. struct spi_transfer xfer = {};
  21. struct spi_message msg;
  22. size_t buf_len;
  23. void *buf;
  24. int ret;
  25. buf_len = sizeof(lsc_bitstream_burst);
  26. buf = kmemdup(lsc_bitstream_burst, buf_len, GFP_KERNEL);
  27. if (!buf)
  28. return -ENOMEM;
  29. xfer.len = buf_len;
  30. xfer.tx_buf = buf;
  31. xfer.cs_change = 1;
  32. spi_message_init_with_transfers(&msg, &xfer, 1);
  33. /*
  34. * Lock SPI bus for exclusive usage until FPGA programming is done.
  35. * SPI bus will be released in sysconfig_spi_bitstream_burst_complete().
  36. */
  37. spi_bus_lock(spi->controller);
  38. ret = spi_sync_locked(spi, &msg);
  39. if (ret)
  40. spi_bus_unlock(spi->controller);
  41. kfree(buf);
  42. return ret;
  43. }
  44. static int sysconfig_spi_bitstream_burst_write(struct sysconfig_priv *priv,
  45. const char *buf, size_t len)
  46. {
  47. struct spi_device *spi = to_spi_device(priv->dev);
  48. struct spi_transfer xfer = {
  49. .tx_buf = buf,
  50. .len = len,
  51. .cs_change = 1,
  52. };
  53. struct spi_message msg;
  54. spi_message_init_with_transfers(&msg, &xfer, 1);
  55. return spi_sync_locked(spi, &msg);
  56. }
  57. static int sysconfig_spi_bitstream_burst_complete(struct sysconfig_priv *priv)
  58. {
  59. struct spi_device *spi = to_spi_device(priv->dev);
  60. /* Bitstream burst write is done, release SPI bus */
  61. spi_bus_unlock(spi->controller);
  62. /* Toggle CS to finish bitstream write */
  63. return spi_write(spi, NULL, 0);
  64. }
  65. static int sysconfig_spi_probe(struct spi_device *spi)
  66. {
  67. const struct spi_device_id *dev_id;
  68. struct device *dev = &spi->dev;
  69. struct sysconfig_priv *priv;
  70. const u32 *spi_max_speed;
  71. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  72. if (!priv)
  73. return -ENOMEM;
  74. spi_max_speed = device_get_match_data(dev);
  75. if (!spi_max_speed) {
  76. dev_id = spi_get_device_id(spi);
  77. if (!dev_id)
  78. return -ENODEV;
  79. spi_max_speed = (const u32 *)dev_id->driver_data;
  80. }
  81. if (!spi_max_speed)
  82. return -EINVAL;
  83. if (spi->max_speed_hz > *spi_max_speed) {
  84. dev_err(dev, "SPI speed %u is too high, maximum speed is %u\n",
  85. spi->max_speed_hz, *spi_max_speed);
  86. return -EINVAL;
  87. }
  88. priv->dev = dev;
  89. priv->command_transfer = sysconfig_spi_cmd_transfer;
  90. priv->bitstream_burst_write_init = sysconfig_spi_bitstream_burst_init;
  91. priv->bitstream_burst_write = sysconfig_spi_bitstream_burst_write;
  92. priv->bitstream_burst_write_complete = sysconfig_spi_bitstream_burst_complete;
  93. return sysconfig_probe(priv);
  94. }
  95. static const struct spi_device_id sysconfig_spi_ids[] = {
  96. {
  97. .name = "sysconfig-ecp5",
  98. .driver_data = (kernel_ulong_t)&ecp5_spi_max_speed_hz,
  99. }, {},
  100. };
  101. MODULE_DEVICE_TABLE(spi, sysconfig_spi_ids);
  102. #if IS_ENABLED(CONFIG_OF)
  103. static const struct of_device_id sysconfig_of_ids[] = {
  104. {
  105. .compatible = "lattice,sysconfig-ecp5",
  106. .data = &ecp5_spi_max_speed_hz,
  107. }, {},
  108. };
  109. MODULE_DEVICE_TABLE(of, sysconfig_of_ids);
  110. #endif /* IS_ENABLED(CONFIG_OF) */
  111. static struct spi_driver lattice_sysconfig_driver = {
  112. .probe = sysconfig_spi_probe,
  113. .id_table = sysconfig_spi_ids,
  114. .driver = {
  115. .name = "lattice_sysconfig_spi_fpga_mgr",
  116. .of_match_table = of_match_ptr(sysconfig_of_ids),
  117. },
  118. };
  119. module_spi_driver(lattice_sysconfig_driver);
  120. MODULE_DESCRIPTION("Lattice sysCONFIG Slave SPI FPGA Manager");
  121. MODULE_LICENSE("GPL");