zynqmp-fpga.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 ssize_t status_show(struct device *dev,
  62. struct device_attribute *attr, char *buf)
  63. {
  64. u32 status;
  65. int ret;
  66. ret = zynqmp_pm_fpga_get_config_status(&status);
  67. if (ret)
  68. return ret;
  69. return sysfs_emit(buf, "0x%x\n", status);
  70. }
  71. static DEVICE_ATTR_RO(status);
  72. static struct attribute *zynqmp_fpga_attrs[] = {
  73. &dev_attr_status.attr,
  74. NULL,
  75. };
  76. ATTRIBUTE_GROUPS(zynqmp_fpga);
  77. static const struct fpga_manager_ops zynqmp_fpga_ops = {
  78. .state = zynqmp_fpga_ops_state,
  79. .write_init = zynqmp_fpga_ops_write_init,
  80. .write = zynqmp_fpga_ops_write,
  81. };
  82. static int zynqmp_fpga_probe(struct platform_device *pdev)
  83. {
  84. struct device *dev = &pdev->dev;
  85. struct zynqmp_fpga_priv *priv;
  86. struct fpga_manager *mgr;
  87. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  88. if (!priv)
  89. return -ENOMEM;
  90. priv->dev = dev;
  91. mgr = devm_fpga_mgr_register(dev, "Xilinx ZynqMP FPGA Manager",
  92. &zynqmp_fpga_ops, priv);
  93. return PTR_ERR_OR_ZERO(mgr);
  94. }
  95. #ifdef CONFIG_OF
  96. static const struct of_device_id zynqmp_fpga_of_match[] = {
  97. { .compatible = "xlnx,zynqmp-pcap-fpga", },
  98. {},
  99. };
  100. MODULE_DEVICE_TABLE(of, zynqmp_fpga_of_match);
  101. #endif
  102. static struct platform_driver zynqmp_fpga_driver = {
  103. .probe = zynqmp_fpga_probe,
  104. .driver = {
  105. .name = "zynqmp_fpga_manager",
  106. .of_match_table = of_match_ptr(zynqmp_fpga_of_match),
  107. .dev_groups = zynqmp_fpga_groups,
  108. },
  109. };
  110. module_platform_driver(zynqmp_fpga_driver);
  111. MODULE_AUTHOR("Nava kishore Manne <navam@xilinx.com>");
  112. MODULE_DESCRIPTION("Xilinx ZynqMp FPGA Manager");
  113. MODULE_LICENSE("GPL");