1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2019 Xilinx, Inc.
6 #include <linux/dma-mapping.h>
7 #include <linux/fpga/fpga-mgr.h>
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <linux/of_address.h>
12 #include <linux/string.h>
13 #include <linux/firmware/xlnx-zynqmp.h>
15 /* Constant Definitions */
16 #define IXR_FPGA_DONE_MASK BIT(3)
19 * struct zynqmp_fpga_priv - Private data structure
20 * @dev: Device data structure
21 * @flags: flags which is used to identify the bitfile type
23 struct zynqmp_fpga_priv {
28 static int zynqmp_fpga_ops_write_init(struct fpga_manager *mgr,
29 struct fpga_image_info *info,
30 const char *buf, size_t size)
32 struct zynqmp_fpga_priv *priv;
35 priv->flags = info->flags;
40 static int zynqmp_fpga_ops_write(struct fpga_manager *mgr,
41 const char *buf, size_t size)
43 const struct zynqmp_eemi_ops *eemi_ops = zynqmp_pm_get_eemi_ops();
44 struct zynqmp_fpga_priv *priv;
50 if (IS_ERR_OR_NULL(eemi_ops) || !eemi_ops->fpga_load)
55 kbuf = dma_alloc_coherent(priv->dev, size, &dma_addr, GFP_KERNEL);
59 memcpy(kbuf, buf, size);
61 wmb(); /* ensure all writes are done before initiate FW call */
63 if (priv->flags & FPGA_MGR_PARTIAL_RECONFIG)
64 eemi_flags |= XILINX_ZYNQMP_PM_FPGA_PARTIAL;
66 ret = eemi_ops->fpga_load(dma_addr, size, eemi_flags);
68 dma_free_coherent(priv->dev, size, kbuf, dma_addr);
73 static int zynqmp_fpga_ops_write_complete(struct fpga_manager *mgr,
74 struct fpga_image_info *info)
79 static enum fpga_mgr_states zynqmp_fpga_ops_state(struct fpga_manager *mgr)
81 const struct zynqmp_eemi_ops *eemi_ops = zynqmp_pm_get_eemi_ops();
84 if (IS_ERR_OR_NULL(eemi_ops) || !eemi_ops->fpga_get_status)
85 return FPGA_MGR_STATE_UNKNOWN;
87 eemi_ops->fpga_get_status(&status);
88 if (status & IXR_FPGA_DONE_MASK)
89 return FPGA_MGR_STATE_OPERATING;
91 return FPGA_MGR_STATE_UNKNOWN;
94 static const struct fpga_manager_ops zynqmp_fpga_ops = {
95 .state = zynqmp_fpga_ops_state,
96 .write_init = zynqmp_fpga_ops_write_init,
97 .write = zynqmp_fpga_ops_write,
98 .write_complete = zynqmp_fpga_ops_write_complete,
101 static int zynqmp_fpga_probe(struct platform_device *pdev)
103 struct device *dev = &pdev->dev;
104 struct zynqmp_fpga_priv *priv;
105 struct fpga_manager *mgr;
108 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
114 mgr = devm_fpga_mgr_create(dev, "Xilinx ZynqMP FPGA Manager",
115 &zynqmp_fpga_ops, priv);
119 platform_set_drvdata(pdev, mgr);
121 ret = fpga_mgr_register(mgr);
123 dev_err(dev, "unable to register FPGA manager");
130 static int zynqmp_fpga_remove(struct platform_device *pdev)
132 struct fpga_manager *mgr = platform_get_drvdata(pdev);
134 fpga_mgr_unregister(mgr);
139 static const struct of_device_id zynqmp_fpga_of_match[] = {
140 { .compatible = "xlnx,zynqmp-pcap-fpga", },
144 MODULE_DEVICE_TABLE(of, zynqmp_fpga_of_match);
146 static struct platform_driver zynqmp_fpga_driver = {
147 .probe = zynqmp_fpga_probe,
148 .remove = zynqmp_fpga_remove,
150 .name = "zynqmp_fpga_manager",
151 .of_match_table = of_match_ptr(zynqmp_fpga_of_match),
155 module_platform_driver(zynqmp_fpga_driver);
158 MODULE_DESCRIPTION("Xilinx ZynqMp FPGA Manager");
159 MODULE_LICENSE("GPL");