xilinx-spi.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Xilinx Spartan6 and 7 Series Slave Serial SPI Driver
  4. *
  5. * Copyright (C) 2017 DENX Software Engineering
  6. *
  7. * Anatolij Gustschin <agust@denx.de>
  8. *
  9. * Manage Xilinx FPGA firmware that is loaded over SPI using
  10. * the slave serial configuration interface.
  11. */
  12. #include "xilinx-core.h"
  13. #include <linux/module.h>
  14. #include <linux/mod_devicetable.h>
  15. #include <linux/of.h>
  16. #include <linux/spi/spi.h>
  17. static int xilinx_spi_write(struct xilinx_fpga_core *core, const char *buf,
  18. size_t count)
  19. {
  20. struct spi_device *spi = to_spi_device(core->dev);
  21. const char *fw_data = buf;
  22. const char *fw_data_end = fw_data + count;
  23. while (fw_data < fw_data_end) {
  24. size_t remaining, stride;
  25. int ret;
  26. remaining = fw_data_end - fw_data;
  27. stride = min_t(size_t, remaining, SZ_4K);
  28. ret = spi_write(spi, fw_data, stride);
  29. if (ret) {
  30. dev_err(core->dev, "SPI error in firmware write: %d\n",
  31. ret);
  32. return ret;
  33. }
  34. fw_data += stride;
  35. }
  36. return 0;
  37. }
  38. static int xilinx_spi_probe(struct spi_device *spi)
  39. {
  40. struct xilinx_fpga_core *core;
  41. core = devm_kzalloc(&spi->dev, sizeof(*core), GFP_KERNEL);
  42. if (!core)
  43. return -ENOMEM;
  44. core->dev = &spi->dev;
  45. core->write = xilinx_spi_write;
  46. return xilinx_core_probe(core);
  47. }
  48. #ifdef CONFIG_OF
  49. static const struct of_device_id xlnx_spi_of_match[] = {
  50. {
  51. .compatible = "xlnx,fpga-slave-serial",
  52. },
  53. {}
  54. };
  55. MODULE_DEVICE_TABLE(of, xlnx_spi_of_match);
  56. #endif
  57. static struct spi_driver xilinx_slave_spi_driver = {
  58. .driver = {
  59. .name = "xlnx-slave-spi",
  60. .of_match_table = of_match_ptr(xlnx_spi_of_match),
  61. },
  62. .probe = xilinx_spi_probe,
  63. };
  64. module_spi_driver(xilinx_slave_spi_driver)
  65. MODULE_LICENSE("GPL v2");
  66. MODULE_AUTHOR("Anatolij Gustschin <agust@denx.de>");
  67. MODULE_DESCRIPTION("Load Xilinx FPGA firmware over SPI");