zynqmp-fpga.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2019 Xilinx, Inc.
  4. */
  5. #include <linux/dma-mapping.h>
  6. #include <linux/fpga/fpga-mgr.h>
  7. #include <linux/io.h>
  8. #include <linux/kernel.h>
  9. #include <linux/module.h>
  10. #include <linux/of_address.h>
  11. #include <linux/string.h>
  12. #include <linux/firmware/xlnx-zynqmp.h>
  13. /* Constant Definitions */
  14. #define IXR_FPGA_DONE_MASK BIT(3)
  15. /**
  16. * struct zynqmp_fpga_priv - Private data structure
  17. * @dev: Device data structure
  18. * @flags: flags which is used to identify the bitfile type
  19. */
  20. struct zynqmp_fpga_priv {
  21. struct device *dev;
  22. u32 flags;
  23. };
  24. static int zynqmp_fpga_ops_write_init(struct fpga_manager *mgr,
  25. struct fpga_image_info *info,
  26. const char *buf, size_t size)
  27. {
  28. struct zynqmp_fpga_priv *priv;
  29. priv = mgr->priv;
  30. priv->flags = info->flags;
  31. return 0;
  32. }
  33. static int zynqmp_fpga_ops_write(struct fpga_manager *mgr,
  34. const char *buf, size_t size)
  35. {
  36. struct zynqmp_fpga_priv *priv;
  37. dma_addr_t dma_addr;
  38. u32 eemi_flags = 0;
  39. char *kbuf;
  40. int ret;
  41. priv = mgr->priv;
  42. kbuf = dma_alloc_coherent(priv->dev, size, &dma_addr, GFP_KERNEL);
  43. if (!kbuf)
  44. return -ENOMEM;
  45. memcpy(kbuf, buf, size);
  46. wmb(); /* ensure all writes are done before initiate FW call */
  47. if (priv->flags & FPGA_MGR_PARTIAL_RECONFIG)
  48. eemi_flags |= XILINX_ZYNQMP_PM_FPGA_PARTIAL;
  49. ret = zynqmp_pm_fpga_load(dma_addr, size, eemi_flags);
  50. dma_free_coherent(priv->dev, size, kbuf, dma_addr);
  51. return ret;
  52. }
  53. static enum fpga_mgr_states zynqmp_fpga_ops_state(struct fpga_manager *mgr)
  54. {
  55. u32 status = 0;
  56. zynqmp_pm_fpga_get_status(&status);
  57. if (status & IXR_FPGA_DONE_MASK)
  58. return FPGA_MGR_STATE_OPERATING;
  59. return FPGA_MGR_STATE_UNKNOWN;
  60. }
  61. static const struct fpga_manager_ops zynqmp_fpga_ops = {
  62. .state = zynqmp_fpga_ops_state,
  63. .write_init = zynqmp_fpga_ops_write_init,
  64. .write = zynqmp_fpga_ops_write,
  65. };
  66. static int zynqmp_fpga_probe(struct platform_device *pdev)
  67. {
  68. struct device *dev = &pdev->dev;
  69. struct zynqmp_fpga_priv *priv;
  70. struct fpga_manager *mgr;
  71. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  72. if (!priv)
  73. return -ENOMEM;
  74. priv->dev = dev;
  75. mgr = devm_fpga_mgr_register(dev, "Xilinx ZynqMP FPGA Manager",
  76. &zynqmp_fpga_ops, priv);
  77. return PTR_ERR_OR_ZERO(mgr);
  78. }
  79. #ifdef CONFIG_OF
  80. static const struct of_device_id zynqmp_fpga_of_match[] = {
  81. { .compatible = "xlnx,zynqmp-pcap-fpga", },
  82. {},
  83. };
  84. MODULE_DEVICE_TABLE(of, zynqmp_fpga_of_match);
  85. #endif
  86. static struct platform_driver zynqmp_fpga_driver = {
  87. .probe = zynqmp_fpga_probe,
  88. .driver = {
  89. .name = "zynqmp_fpga_manager",
  90. .of_match_table = of_match_ptr(zynqmp_fpga_of_match),
  91. },
  92. };
  93. module_platform_driver(zynqmp_fpga_driver);
  94. MODULE_AUTHOR("Nava kishore Manne <[email protected]>");
  95. MODULE_DESCRIPTION("Xilinx ZynqMp FPGA Manager");
  96. MODULE_LICENSE("GPL");