1 // SPDX-License-Identifier: GPL-2.0
3 * Lattice FPGA sysCONFIG interface functions independent of port type.
6 #include <linux/delay.h>
7 #include <linux/fpga/fpga-mgr.h>
8 #include <linux/gpio/consumer.h>
9 #include <linux/iopoll.h>
11 #include "lattice-sysconfig.h"
13 static int sysconfig_cmd_write(struct sysconfig_priv
*priv
, const void *buf
,
16 return priv
->command_transfer(priv
, buf
, buf_len
, NULL
, 0);
19 static int sysconfig_cmd_read(struct sysconfig_priv
*priv
, const void *tx_buf
,
20 size_t tx_len
, void *rx_buf
, size_t rx_len
)
22 return priv
->command_transfer(priv
, tx_buf
, tx_len
, rx_buf
, rx_len
);
25 static int sysconfig_read_busy(struct sysconfig_priv
*priv
)
27 const u8 lsc_check_busy
[] = SYSCONFIG_LSC_CHECK_BUSY
;
31 ret
= sysconfig_cmd_read(priv
, lsc_check_busy
, sizeof(lsc_check_busy
),
37 static int sysconfig_poll_busy(struct sysconfig_priv
*priv
)
41 ret
= read_poll_timeout(sysconfig_read_busy
, busy
, busy
<= 0,
42 SYSCONFIG_POLL_INTERVAL_US
,
43 SYSCONFIG_POLL_BUSY_TIMEOUT_US
, false, priv
);
48 static int sysconfig_read_status(struct sysconfig_priv
*priv
, u32
*status
)
50 const u8 lsc_read_status
[] = SYSCONFIG_LSC_READ_STATUS
;
54 ret
= sysconfig_cmd_read(priv
, lsc_read_status
, sizeof(lsc_read_status
),
55 &device_status
, sizeof(device_status
));
59 *status
= be32_to_cpu(device_status
);
64 static int sysconfig_poll_status(struct sysconfig_priv
*priv
, u32
*status
)
66 int ret
= sysconfig_poll_busy(priv
);
71 return sysconfig_read_status(priv
, status
);
74 static int sysconfig_poll_gpio(struct gpio_desc
*gpio
, bool is_active
)
78 ret
= read_poll_timeout(gpiod_get_value
, val
,
79 val
< 0 || !!val
== is_active
,
80 SYSCONFIG_POLL_INTERVAL_US
,
81 SYSCONFIG_POLL_GPIO_TIMEOUT_US
, false, gpio
);
89 static int sysconfig_gpio_refresh(struct sysconfig_priv
*priv
)
91 struct gpio_desc
*program
= priv
->program
;
92 struct gpio_desc
*init
= priv
->init
;
93 struct gpio_desc
*done
= priv
->done
;
97 gpiod_set_value(program
, 1);
99 ret
= sysconfig_poll_gpio(init
, true);
101 ret
= sysconfig_poll_gpio(done
, false);
106 /* Enter program mode */
107 gpiod_set_value(program
, 0);
109 return sysconfig_poll_gpio(init
, false);
112 static int sysconfig_lsc_refresh(struct sysconfig_priv
*priv
)
114 static const u8 lsc_refresh
[] = SYSCONFIG_LSC_REFRESH
;
117 ret
= sysconfig_cmd_write(priv
, lsc_refresh
, sizeof(lsc_refresh
));
121 usleep_range(4000, 8000);
126 static int sysconfig_refresh(struct sysconfig_priv
*priv
)
128 struct gpio_desc
*program
= priv
->program
;
129 struct gpio_desc
*init
= priv
->init
;
130 struct gpio_desc
*done
= priv
->done
;
132 if (program
&& init
&& done
)
133 return sysconfig_gpio_refresh(priv
);
135 return sysconfig_lsc_refresh(priv
);
138 static int sysconfig_isc_enable(struct sysconfig_priv
*priv
)
140 u8 isc_enable
[] = SYSCONFIG_ISC_ENABLE
;
144 ret
= sysconfig_cmd_write(priv
, isc_enable
, sizeof(isc_enable
));
148 ret
= sysconfig_poll_status(priv
, &status
);
152 if (status
& SYSCONFIG_STATUS_FAIL
)
158 static int sysconfig_isc_erase(struct sysconfig_priv
*priv
)
160 u8 isc_erase
[] = SYSCONFIG_ISC_ERASE
;
164 ret
= sysconfig_cmd_write(priv
, isc_erase
, sizeof(isc_erase
));
168 ret
= sysconfig_poll_status(priv
, &status
);
172 if (status
& SYSCONFIG_STATUS_FAIL
)
178 static int sysconfig_isc_init(struct sysconfig_priv
*priv
)
180 int ret
= sysconfig_isc_enable(priv
);
185 return sysconfig_isc_erase(priv
);
188 static int sysconfig_lsc_init_addr(struct sysconfig_priv
*priv
)
190 const u8 lsc_init_addr
[] = SYSCONFIG_LSC_INIT_ADDR
;
192 return sysconfig_cmd_write(priv
, lsc_init_addr
, sizeof(lsc_init_addr
));
195 static int sysconfig_burst_write_init(struct sysconfig_priv
*priv
)
197 return priv
->bitstream_burst_write_init(priv
);
200 static int sysconfig_burst_write_complete(struct sysconfig_priv
*priv
)
202 return priv
->bitstream_burst_write_complete(priv
);
205 static int sysconfig_bitstream_burst_write(struct sysconfig_priv
*priv
,
206 const char *buf
, size_t count
)
208 int ret
= priv
->bitstream_burst_write(priv
, buf
, count
);
211 sysconfig_burst_write_complete(priv
);
216 static int sysconfig_isc_disable(struct sysconfig_priv
*priv
)
218 const u8 isc_disable
[] = SYSCONFIG_ISC_DISABLE
;
220 return sysconfig_cmd_write(priv
, isc_disable
, sizeof(isc_disable
));
223 static void sysconfig_cleanup(struct sysconfig_priv
*priv
)
225 sysconfig_isc_erase(priv
);
226 sysconfig_refresh(priv
);
229 static int sysconfig_isc_finish(struct sysconfig_priv
*priv
)
231 struct gpio_desc
*done_gpio
= priv
->done
;
236 ret
= sysconfig_isc_disable(priv
);
240 return sysconfig_poll_gpio(done_gpio
, true);
243 ret
= sysconfig_poll_status(priv
, &status
);
247 if ((status
& SYSCONFIG_STATUS_DONE
) &&
248 !(status
& SYSCONFIG_STATUS_BUSY
) &&
249 !(status
& SYSCONFIG_STATUS_ERR
))
250 return sysconfig_isc_disable(priv
);
255 static enum fpga_mgr_states
sysconfig_ops_state(struct fpga_manager
*mgr
)
257 struct sysconfig_priv
*priv
= mgr
->priv
;
258 struct gpio_desc
*done
= priv
->done
;
262 if (done
&& (gpiod_get_value(done
) > 0))
263 return FPGA_MGR_STATE_OPERATING
;
265 ret
= sysconfig_read_status(priv
, &status
);
266 if (!ret
&& (status
& SYSCONFIG_STATUS_DONE
))
267 return FPGA_MGR_STATE_OPERATING
;
269 return FPGA_MGR_STATE_UNKNOWN
;
272 static int sysconfig_ops_write_init(struct fpga_manager
*mgr
,
273 struct fpga_image_info
*info
,
274 const char *buf
, size_t count
)
276 struct sysconfig_priv
*priv
= mgr
->priv
;
277 struct device
*dev
= &mgr
->dev
;
280 if (info
->flags
& FPGA_MGR_PARTIAL_RECONFIG
) {
281 dev_err(dev
, "Partial reconfiguration is not supported\n");
285 /* Enter program mode */
286 ret
= sysconfig_refresh(priv
);
288 dev_err(dev
, "Failed to go to program mode\n");
293 ret
= sysconfig_isc_init(priv
);
295 dev_err(dev
, "Failed to go to ISC mode\n");
299 /* Initialize the Address Shift Register */
300 ret
= sysconfig_lsc_init_addr(priv
);
303 "Failed to initialize the Address Shift Register\n");
307 /* Prepare for bitstream burst write */
308 ret
= sysconfig_burst_write_init(priv
);
310 dev_err(dev
, "Failed to prepare for bitstream burst write\n");
315 static int sysconfig_ops_write(struct fpga_manager
*mgr
, const char *buf
,
318 return sysconfig_bitstream_burst_write(mgr
->priv
, buf
, count
);
321 static int sysconfig_ops_write_complete(struct fpga_manager
*mgr
,
322 struct fpga_image_info
*info
)
324 struct sysconfig_priv
*priv
= mgr
->priv
;
325 struct device
*dev
= &mgr
->dev
;
328 ret
= sysconfig_burst_write_complete(priv
);
330 ret
= sysconfig_poll_busy(priv
);
333 dev_err(dev
, "Error while waiting bitstream write to finish\n");
337 ret
= sysconfig_isc_finish(priv
);
341 sysconfig_cleanup(priv
);
346 static const struct fpga_manager_ops sysconfig_fpga_mgr_ops
= {
347 .state
= sysconfig_ops_state
,
348 .write_init
= sysconfig_ops_write_init
,
349 .write
= sysconfig_ops_write
,
350 .write_complete
= sysconfig_ops_write_complete
,
353 int sysconfig_probe(struct sysconfig_priv
*priv
)
355 struct gpio_desc
*program
, *init
, *done
;
356 struct device
*dev
= priv
->dev
;
357 struct fpga_manager
*mgr
;
362 if (!priv
->command_transfer
||
363 !priv
->bitstream_burst_write_init
||
364 !priv
->bitstream_burst_write
||
365 !priv
->bitstream_burst_write_complete
) {
366 dev_err(dev
, "Essential callback is missing\n");
370 program
= devm_gpiod_get_optional(dev
, "program", GPIOD_OUT_LOW
);
372 return dev_err_probe(dev
, PTR_ERR(program
),
373 "Failed to get PROGRAM GPIO\n");
375 init
= devm_gpiod_get_optional(dev
, "init", GPIOD_IN
);
377 return dev_err_probe(dev
, PTR_ERR(init
),
378 "Failed to get INIT GPIO\n");
380 done
= devm_gpiod_get_optional(dev
, "done", GPIOD_IN
);
382 return dev_err_probe(dev
, PTR_ERR(done
),
383 "Failed to get DONE GPIO\n");
385 priv
->program
= program
;
389 mgr
= devm_fpga_mgr_register(dev
, "Lattice sysCONFIG FPGA Manager",
390 &sysconfig_fpga_mgr_ops
, priv
);
392 return PTR_ERR_OR_ZERO(mgr
);
394 EXPORT_SYMBOL(sysconfig_probe
);
396 MODULE_DESCRIPTION("Lattice sysCONFIG FPGA Manager Core");
397 MODULE_LICENSE("GPL");