xref: /linux/drivers/fpga/xilinx-spi.c (revision dba0bfded433d412506eb674d6af77cdca82e259)
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 
13 #include "xilinx-core.h"
14 
15 #include <linux/module.h>
16 #include <linux/of.h>
17 #include <linux/spi/spi.h>
18 
19 static int xilinx_spi_write(struct xilinx_fpga_core *core, const char *buf,
20 			    size_t count)
21 {
22 	struct spi_device *spi = to_spi_device(core->dev);
23 	const char *fw_data = buf;
24 	const char *fw_data_end = fw_data + count;
25 
26 	while (fw_data < fw_data_end) {
27 		size_t remaining, stride;
28 		int ret;
29 
30 		remaining = fw_data_end - fw_data;
31 		stride = min_t(size_t, remaining, SZ_4K);
32 
33 		ret = spi_write(spi, fw_data, stride);
34 		if (ret) {
35 			dev_err(core->dev, "SPI error in firmware write: %d\n",
36 				ret);
37 			return ret;
38 		}
39 		fw_data += stride;
40 	}
41 
42 	return 0;
43 }
44 
45 static int xilinx_spi_probe(struct spi_device *spi)
46 {
47 	struct xilinx_fpga_core *core;
48 
49 	core = devm_kzalloc(&spi->dev, sizeof(*core), GFP_KERNEL);
50 	if (!core)
51 		return -ENOMEM;
52 
53 	core->dev = &spi->dev;
54 	core->write = xilinx_spi_write;
55 
56 	return xilinx_core_probe(core);
57 }
58 
59 static const struct spi_device_id xilinx_spi_ids[] = {
60 	{ "fpga-slave-serial" },
61 	{ },
62 };
63 MODULE_DEVICE_TABLE(spi, xilinx_spi_ids);
64 
65 #ifdef CONFIG_OF
66 static const struct of_device_id xlnx_spi_of_match[] = {
67 	{
68 		.compatible = "xlnx,fpga-slave-serial",
69 	},
70 	{}
71 };
72 MODULE_DEVICE_TABLE(of, xlnx_spi_of_match);
73 #endif
74 
75 static struct spi_driver xilinx_slave_spi_driver = {
76 	.driver = {
77 		.name = "xlnx-slave-spi",
78 		.of_match_table = of_match_ptr(xlnx_spi_of_match),
79 	},
80 	.probe = xilinx_spi_probe,
81 	.id_table = xilinx_spi_ids,
82 };
83 
84 module_spi_driver(xilinx_slave_spi_driver)
85 
86 MODULE_LICENSE("GPL v2");
87 MODULE_AUTHOR("Anatolij Gustschin <agust@denx.de>");
88 MODULE_DESCRIPTION("Load Xilinx FPGA firmware over SPI");
89