blob: 824abbbd631e433e9d43c1778d6b8ff30c4e946d [file] [log] [blame]
Thomas Gleixner75a6faf2019-06-01 10:08:37 +02001// SPDX-License-Identifier: GPL-2.0-only
Anatolij Gustschin061c97d2017-03-23 19:34:26 -05002/*
Luca Ceresolid1ddca72020-06-11 23:11:41 +02003 * Xilinx Spartan6 and 7 Series Slave Serial SPI Driver
Anatolij Gustschin061c97d2017-03-23 19:34:26 -05004 *
5 * Copyright (C) 2017 DENX Software Engineering
6 *
7 * Anatolij Gustschin <agust@denx.de>
8 *
Anatolij Gustschin061c97d2017-03-23 19:34:26 -05009 * Manage Xilinx FPGA firmware that is loaded over SPI using
10 * the slave serial configuration interface.
11 */
12
13#include <linux/delay.h>
14#include <linux/device.h>
15#include <linux/fpga/fpga-mgr.h>
16#include <linux/gpio/consumer.h>
17#include <linux/module.h>
18#include <linux/mod_devicetable.h>
19#include <linux/of.h>
20#include <linux/spi/spi.h>
21#include <linux/sizes.h>
22
23struct xilinx_spi_conf {
24 struct spi_device *spi;
25 struct gpio_desc *prog_b;
Luca Ceresolidd2784c2020-06-22 15:37:23 +020026 struct gpio_desc *init_b;
Anatolij Gustschin061c97d2017-03-23 19:34:26 -050027 struct gpio_desc *done;
28};
29
Luca Ceresolieefe64f2020-08-30 18:38:49 +020030static int get_done_gpio(struct fpga_manager *mgr)
Anatolij Gustschin061c97d2017-03-23 19:34:26 -050031{
32 struct xilinx_spi_conf *conf = mgr->priv;
Luca Ceresolieefe64f2020-08-30 18:38:49 +020033 int ret;
Anatolij Gustschin061c97d2017-03-23 19:34:26 -050034
Luca Ceresolieefe64f2020-08-30 18:38:49 +020035 ret = gpiod_get_value(conf->done);
36
37 if (ret < 0)
38 dev_err(&mgr->dev, "Error reading DONE (%d)\n", ret);
39
40 return ret;
41}
42
43static enum fpga_mgr_states xilinx_spi_state(struct fpga_manager *mgr)
44{
45 if (!get_done_gpio(mgr))
Anatolij Gustschin061c97d2017-03-23 19:34:26 -050046 return FPGA_MGR_STATE_RESET;
47
48 return FPGA_MGR_STATE_UNKNOWN;
49}
50
Luca Ceresolidd2784c2020-06-22 15:37:23 +020051/**
52 * wait_for_init_b - wait for the INIT_B pin to have a given state, or wait
53 * a given delay if the pin is unavailable
54 *
55 * @mgr: The FPGA manager object
56 * @value: Value INIT_B to wait for (1 = asserted = low)
57 * @alt_udelay: Delay to wait if the INIT_B GPIO is not available
58 *
59 * Returns 0 when the INIT_B GPIO reached the given state or -ETIMEDOUT if
60 * too much time passed waiting for that. If no INIT_B GPIO is available
61 * then always return 0.
62 */
63static int wait_for_init_b(struct fpga_manager *mgr, int value,
64 unsigned long alt_udelay)
65{
66 struct xilinx_spi_conf *conf = mgr->priv;
67 unsigned long timeout = jiffies + msecs_to_jiffies(1000);
68
69 if (conf->init_b) {
70 while (time_before(jiffies, timeout)) {
Luca Ceresolieefe64f2020-08-30 18:38:49 +020071 int ret = gpiod_get_value(conf->init_b);
72
73 if (ret == value)
Luca Ceresolidd2784c2020-06-22 15:37:23 +020074 return 0;
Luca Ceresolieefe64f2020-08-30 18:38:49 +020075
76 if (ret < 0) {
77 dev_err(&mgr->dev, "Error reading INIT_B (%d)\n", ret);
78 return ret;
79 }
80
Luca Ceresolidd2784c2020-06-22 15:37:23 +020081 usleep_range(100, 400);
82 }
Luca Ceresolieefe64f2020-08-30 18:38:49 +020083
84 dev_err(&mgr->dev, "Timeout waiting for INIT_B to %s\n",
85 value ? "assert" : "deassert");
Luca Ceresolidd2784c2020-06-22 15:37:23 +020086 return -ETIMEDOUT;
87 }
88
89 udelay(alt_udelay);
90
91 return 0;
92}
93
Anatolij Gustschin061c97d2017-03-23 19:34:26 -050094static int xilinx_spi_write_init(struct fpga_manager *mgr,
95 struct fpga_image_info *info,
96 const char *buf, size_t count)
97{
98 struct xilinx_spi_conf *conf = mgr->priv;
Luca Ceresolidd2784c2020-06-22 15:37:23 +020099 int err;
Anatolij Gustschin061c97d2017-03-23 19:34:26 -0500100
101 if (info->flags & FPGA_MGR_PARTIAL_RECONFIG) {
Luca Ceresolia44ecdc2020-08-30 18:38:47 +0200102 dev_err(&mgr->dev, "Partial reconfiguration not supported\n");
Anatolij Gustschin061c97d2017-03-23 19:34:26 -0500103 return -EINVAL;
104 }
105
106 gpiod_set_value(conf->prog_b, 1);
107
Luca Ceresolidd2784c2020-06-22 15:37:23 +0200108 err = wait_for_init_b(mgr, 1, 1); /* min is 500 ns */
109 if (err) {
Luca Ceresolidd2784c2020-06-22 15:37:23 +0200110 gpiod_set_value(conf->prog_b, 0);
111 return err;
112 }
Anatolij Gustschin061c97d2017-03-23 19:34:26 -0500113
114 gpiod_set_value(conf->prog_b, 0);
115
Luca Ceresolidd2784c2020-06-22 15:37:23 +0200116 err = wait_for_init_b(mgr, 0, 0);
Luca Ceresolieefe64f2020-08-30 18:38:49 +0200117 if (err)
Luca Ceresolidd2784c2020-06-22 15:37:23 +0200118 return err;
Luca Ceresolidd2784c2020-06-22 15:37:23 +0200119
Luca Ceresolieefe64f2020-08-30 18:38:49 +0200120 if (get_done_gpio(mgr)) {
Anatolij Gustschin061c97d2017-03-23 19:34:26 -0500121 dev_err(&mgr->dev, "Unexpected DONE pin state...\n");
122 return -EIO;
123 }
124
125 /* program latency */
Luca Ceresoli23f872b2020-06-11 23:11:42 +0200126 usleep_range(7500, 7600);
Anatolij Gustschin061c97d2017-03-23 19:34:26 -0500127 return 0;
128}
129
130static int xilinx_spi_write(struct fpga_manager *mgr, const char *buf,
131 size_t count)
132{
133 struct xilinx_spi_conf *conf = mgr->priv;
134 const char *fw_data = buf;
135 const char *fw_data_end = fw_data + count;
136
137 while (fw_data < fw_data_end) {
138 size_t remaining, stride;
139 int ret;
140
141 remaining = fw_data_end - fw_data;
142 stride = min_t(size_t, remaining, SZ_4K);
143
144 ret = spi_write(conf->spi, fw_data, stride);
145 if (ret) {
146 dev_err(&mgr->dev, "SPI error in firmware write: %d\n",
147 ret);
148 return ret;
149 }
150 fw_data += stride;
151 }
152
153 return 0;
154}
155
156static int xilinx_spi_apply_cclk_cycles(struct xilinx_spi_conf *conf)
157{
158 struct spi_device *spi = conf->spi;
159 const u8 din_data[1] = { 0xff };
160 int ret;
161
162 ret = spi_write(conf->spi, din_data, sizeof(din_data));
163 if (ret)
164 dev_err(&spi->dev, "applying CCLK cycles failed: %d\n", ret);
165
166 return ret;
167}
168
169static int xilinx_spi_write_complete(struct fpga_manager *mgr,
170 struct fpga_image_info *info)
171{
172 struct xilinx_spi_conf *conf = mgr->priv;
Luca Ceresoli16b78562020-08-30 18:38:48 +0200173 unsigned long timeout = jiffies + usecs_to_jiffies(info->config_complete_timeout_us);
174 bool expired = false;
175 int done;
Anatolij Gustschin061c97d2017-03-23 19:34:26 -0500176 int ret;
177
Luca Ceresoli16b78562020-08-30 18:38:48 +0200178 /*
179 * This loop is carefully written such that if the driver is
180 * scheduled out for more than 'timeout', we still check for DONE
181 * before giving up and we apply 8 extra CCLK cycles in all cases.
182 */
183 while (!expired) {
184 expired = time_after(jiffies, timeout);
Anatolij Gustschin061c97d2017-03-23 19:34:26 -0500185
Luca Ceresoli16b78562020-08-30 18:38:48 +0200186 done = get_done_gpio(mgr);
187 if (done < 0)
188 return done;
Anatolij Gustschin061c97d2017-03-23 19:34:26 -0500189
190 ret = xilinx_spi_apply_cclk_cycles(conf);
191 if (ret)
192 return ret;
193
Luca Ceresoli16b78562020-08-30 18:38:48 +0200194 if (done)
195 return 0;
Anatolij Gustschin061c97d2017-03-23 19:34:26 -0500196 }
197
Luca Ceresoli4e772ab2020-08-30 18:38:50 +0200198 if (conf->init_b) {
199 ret = gpiod_get_value(conf->init_b);
200
201 if (ret < 0) {
202 dev_err(&mgr->dev, "Error reading INIT_B (%d)\n", ret);
203 return ret;
204 }
205
206 dev_err(&mgr->dev,
207 ret ? "CRC error or invalid device\n"
208 : "Missing sync word or incomplete bitstream\n");
209 } else {
210 dev_err(&mgr->dev, "Timeout after config data transfer\n");
211 }
212
Anatolij Gustschin061c97d2017-03-23 19:34:26 -0500213 return -ETIMEDOUT;
214}
215
216static const struct fpga_manager_ops xilinx_spi_ops = {
217 .state = xilinx_spi_state,
218 .write_init = xilinx_spi_write_init,
219 .write = xilinx_spi_write,
220 .write_complete = xilinx_spi_write_complete,
221};
222
223static int xilinx_spi_probe(struct spi_device *spi)
224{
225 struct xilinx_spi_conf *conf;
Alan Tull7085e2a2018-05-16 18:49:55 -0500226 struct fpga_manager *mgr;
Anatolij Gustschin061c97d2017-03-23 19:34:26 -0500227
228 conf = devm_kzalloc(&spi->dev, sizeof(*conf), GFP_KERNEL);
229 if (!conf)
230 return -ENOMEM;
231
232 conf->spi = spi;
233
234 /* PROGRAM_B is active low */
235 conf->prog_b = devm_gpiod_get(&spi->dev, "prog_b", GPIOD_OUT_LOW);
236 if (IS_ERR(conf->prog_b)) {
237 dev_err(&spi->dev, "Failed to get PROGRAM_B gpio: %ld\n",
238 PTR_ERR(conf->prog_b));
239 return PTR_ERR(conf->prog_b);
240 }
241
Luca Ceresolidd2784c2020-06-22 15:37:23 +0200242 conf->init_b = devm_gpiod_get_optional(&spi->dev, "init-b", GPIOD_IN);
243 if (IS_ERR(conf->init_b)) {
244 dev_err(&spi->dev, "Failed to get INIT_B gpio: %ld\n",
245 PTR_ERR(conf->init_b));
246 return PTR_ERR(conf->init_b);
247 }
248
Anatolij Gustschin061c97d2017-03-23 19:34:26 -0500249 conf->done = devm_gpiod_get(&spi->dev, "done", GPIOD_IN);
250 if (IS_ERR(conf->done)) {
251 dev_err(&spi->dev, "Failed to get DONE gpio: %ld\n",
252 PTR_ERR(conf->done));
253 return PTR_ERR(conf->done);
254 }
255
Alan Tull084181f2018-10-15 17:20:01 -0500256 mgr = devm_fpga_mgr_create(&spi->dev,
257 "Xilinx Slave Serial FPGA Manager",
258 &xilinx_spi_ops, conf);
Alan Tull7085e2a2018-05-16 18:49:55 -0500259 if (!mgr)
260 return -ENOMEM;
261
262 spi_set_drvdata(spi, mgr);
263
Alan Tull084181f2018-10-15 17:20:01 -0500264 return fpga_mgr_register(mgr);
Anatolij Gustschin061c97d2017-03-23 19:34:26 -0500265}
266
267static int xilinx_spi_remove(struct spi_device *spi)
268{
Alan Tull7085e2a2018-05-16 18:49:55 -0500269 struct fpga_manager *mgr = spi_get_drvdata(spi);
270
271 fpga_mgr_unregister(mgr);
Anatolij Gustschin061c97d2017-03-23 19:34:26 -0500272
273 return 0;
274}
275
276static const struct of_device_id xlnx_spi_of_match[] = {
277 { .compatible = "xlnx,fpga-slave-serial", },
278 {}
279};
280MODULE_DEVICE_TABLE(of, xlnx_spi_of_match);
281
282static struct spi_driver xilinx_slave_spi_driver = {
283 .driver = {
284 .name = "xlnx-slave-spi",
285 .of_match_table = of_match_ptr(xlnx_spi_of_match),
286 },
287 .probe = xilinx_spi_probe,
288 .remove = xilinx_spi_remove,
289};
290
291module_spi_driver(xilinx_slave_spi_driver)
292
293MODULE_LICENSE("GPL v2");
294MODULE_AUTHOR("Anatolij Gustschin <agust@denx.de>");
295MODULE_DESCRIPTION("Load Xilinx FPGA firmware over SPI");