xref: /linux/drivers/fpga/xilinx-selectmap.c (revision 20bed0d6ff62d985fe8222958825504b8aad4e88)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Xilinx Spartan6 and 7 Series SelectMAP interface driver
4  *
5  * (C) 2024 Charles Perry <charles.perry@savoirfairelinux.com>
6  *
7  * Manage Xilinx FPGA firmware loaded over the SelectMAP configuration
8  * interface.
9  */
10 
11 #include "xilinx-core.h"
12 
13 #include <linux/gpio/consumer.h>
14 #include <linux/io.h>
15 #include <linux/module.h>
16 #include <linux/of.h>
17 #include <linux/platform_device.h>
18 
19 struct xilinx_selectmap_conf {
20 	struct xilinx_fpga_core core;
21 	void __iomem *base;
22 	struct gpio_desc *csi_b;
23 	struct gpio_desc *rdwr_b;
24 };
25 
26 #define to_xilinx_selectmap_conf(obj) \
27 	container_of(obj, struct xilinx_selectmap_conf, core)
28 
29 static int xilinx_selectmap_write(struct xilinx_fpga_core *core,
30 				  const char *buf, size_t count)
31 {
32 	struct xilinx_selectmap_conf *conf = to_xilinx_selectmap_conf(core);
33 	size_t i;
34 
35 	/*
36 	 * Select this device and put its SelectMAP port into write mode.
37 	 *
38 	 * RDWR_B is set before CSI_B. UG470 describes in chapter
39 	 * "SelectMAP ABORT" that changing RDWR_B while the device is
40 	 * selected aborts the configuration on the next CCLK.
41 	 */
42 	gpiod_set_value(conf->rdwr_b, 1);
43 	gpiod_set_value(conf->csi_b, 1);
44 
45 	for (i = 0; i < count; ++i)
46 		writeb(buf[i], conf->base);
47 
48 	gpiod_set_value(conf->csi_b, 0);
49 	gpiod_set_value(conf->rdwr_b, 0);
50 
51 	return 0;
52 }
53 
54 static int xilinx_selectmap_probe(struct platform_device *pdev)
55 {
56 	struct xilinx_selectmap_conf *conf;
57 	void __iomem *base;
58 
59 	conf = devm_kzalloc(&pdev->dev, sizeof(*conf), GFP_KERNEL);
60 	if (!conf)
61 		return -ENOMEM;
62 
63 	conf->core.dev = &pdev->dev;
64 	conf->core.write = xilinx_selectmap_write;
65 
66 	base = devm_platform_get_and_ioremap_resource(pdev, 0, NULL);
67 	if (IS_ERR(base))
68 		return dev_err_probe(&pdev->dev, PTR_ERR(base),
69 				     "ioremap error\n");
70 	conf->base = base;
71 
72 	/*
73 	 * Request both signals deasserted, so a device sharing the SelectMAP
74 	 * bus with others stays off that bus until its bitstream is written.
75 	 *
76 	 * The value in the gpiod flags is logical, gpiolib drives the line
77 	 * high for GPIOD_OUT_LOW when the firmware describes it active low.
78 	 */
79 	conf->csi_b = devm_gpiod_get_optional(&pdev->dev, "csi",
80 					      GPIOD_OUT_LOW);
81 	if (IS_ERR(conf->csi_b))
82 		return dev_err_probe(&pdev->dev, PTR_ERR(conf->csi_b),
83 				     "Failed to get CSI_B gpio\n");
84 
85 	conf->rdwr_b = devm_gpiod_get_optional(&pdev->dev, "rdwr",
86 					       GPIOD_OUT_LOW);
87 	if (IS_ERR(conf->rdwr_b))
88 		return dev_err_probe(&pdev->dev, PTR_ERR(conf->rdwr_b),
89 				     "Failed to get RDWR_B gpio\n");
90 
91 	return xilinx_core_probe(&conf->core);
92 }
93 
94 static const struct of_device_id xlnx_selectmap_of_match[] = {
95 	{ .compatible = "xlnx,fpga-xc7s-selectmap", }, // Spartan-7
96 	{ .compatible = "xlnx,fpga-xc7a-selectmap", }, // Artix-7
97 	{ .compatible = "xlnx,fpga-xc7k-selectmap", }, // Kintex-7
98 	{ .compatible = "xlnx,fpga-xc7v-selectmap", }, // Virtex-7
99 	{},
100 };
101 MODULE_DEVICE_TABLE(of, xlnx_selectmap_of_match);
102 
103 static struct platform_driver xilinx_selectmap_driver = {
104 	.driver = {
105 		.name = "xilinx-selectmap",
106 		.of_match_table = xlnx_selectmap_of_match,
107 	},
108 	.probe  = xilinx_selectmap_probe,
109 };
110 
111 module_platform_driver(xilinx_selectmap_driver);
112 
113 MODULE_LICENSE("GPL");
114 MODULE_AUTHOR("Charles Perry <charles.perry@savoirfairelinux.com>");
115 MODULE_DESCRIPTION("Load Xilinx FPGA firmware over SelectMap");
116