1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
|
// SPDX-License-Identifier: GPL-2.0
/*
* Lattice FPGA sysCONFIG interface functions independent of port type.
*/
#include <linux/delay.h>
#include <linux/fpga/fpga-mgr.h>
#include <linux/gpio/consumer.h>
#include <linux/iopoll.h>
#include "lattice-sysconfig.h"
static int sysconfig_cmd_write(struct sysconfig_priv *priv, const void *buf,
size_t buf_len)
{
return priv->command_transfer(priv, buf, buf_len, NULL, 0);
}
static int sysconfig_cmd_read(struct sysconfig_priv *priv, const void *tx_buf,
size_t tx_len, void *rx_buf, size_t rx_len)
{
return priv->command_transfer(priv, tx_buf, tx_len, rx_buf, rx_len);
}
static int sysconfig_read_busy(struct sysconfig_priv *priv)
{
const u8 lsc_check_busy[] = SYSCONFIG_LSC_CHECK_BUSY;
u8 busy;
int ret;
ret = sysconfig_cmd_read(priv, lsc_check_busy, sizeof(lsc_check_busy),
&busy, sizeof(busy));
return ret ? : busy;
}
static int sysconfig_poll_busy(struct sysconfig_priv *priv)
{
int ret, busy;
ret = read_poll_timeout(sysconfig_read_busy, busy, busy <= 0,
SYSCONFIG_POLL_INTERVAL_US,
SYSCONFIG_POLL_BUSY_TIMEOUT_US, false, priv);
return ret ? : busy;
}
static int sysconfig_read_status(struct sysconfig_priv *priv, u32 *status)
{
const u8 lsc_read_status[] = SYSCONFIG_LSC_READ_STATUS;
__be32 device_status;
int ret;
ret = sysconfig_cmd_read(priv, lsc_read_status, sizeof(lsc_read_status),
&device_status, sizeof(device_status));
if (ret)
return ret;
*status = be32_to_cpu(device_status);
return 0;
}
static int sysconfig_poll_status(struct sysconfig_priv *priv, u32 *status)
{
int ret = sysconfig_poll_busy(priv);
if (ret)
return ret;
return sysconfig_read_status(priv, status);
}
static int sysconfig_poll_gpio(struct gpio_desc *gpio, bool is_active)
{
int ret, val;
ret = read_poll_timeout(gpiod_get_value, val,
val < 0 || !!val == is_active,
SYSCONFIG_POLL_INTERVAL_US,
SYSCONFIG_POLL_GPIO_TIMEOUT_US, false, gpio);
if (val < 0)
return val;
return ret;
}
static int sysconfig_gpio_refresh(struct sysconfig_priv *priv)
{
struct gpio_desc *program = priv->program;
struct gpio_desc *init = priv->init;
struct gpio_desc *done = priv->done;
int ret;
/* Enter init mode */
gpiod_set_value(program, 1);
ret = sysconfig_poll_gpio(init, true);
if (!ret)
ret = sysconfig_poll_gpio(done, false);
if (ret)
return ret;
/* Enter program mode */
gpiod_set_value(program, 0);
return sysconfig_poll_gpio(init, false);
}
static int sysconfig_lsc_refresh(struct sysconfig_priv *priv)
{
static const u8 lsc_refresh[] = SYSCONFIG_LSC_REFRESH;
int ret;
ret = sysconfig_cmd_write(priv, lsc_refresh, sizeof(lsc_refresh));
if (ret)
return ret;
usleep_range(4000, 8000);
return 0;
}
static int sysconfig_refresh(struct sysconfig_priv *priv)
{
struct gpio_desc *program = priv->program;
struct gpio_desc *init = priv->init;
struct gpio_desc *done = priv->done;
if (program && init && done)
return sysconfig_gpio_refresh(priv);
return sysconfig_lsc_refresh(priv);
}
static int sysconfig_isc_enable(struct sysconfig_priv *priv)
{
u8 isc_enable[] = SYSCONFIG_ISC_ENABLE;
u32 status;
int ret;
ret = sysconfig_cmd_write(priv, isc_enable, sizeof(isc_enable));
if (ret)
return ret;
ret = sysconfig_poll_status(priv, &status);
if (ret)
return ret;
if (status & SYSCONFIG_STATUS_FAIL)
return -EFAULT;
return 0;
}
static int sysconfig_isc_erase(struct sysconfig_priv *priv)
{
u8 isc_erase[] = SYSCONFIG_ISC_ERASE;
u32 status;
int ret;
ret = sysconfig_cmd_write(priv, isc_erase, sizeof(isc_erase));
if (ret)
return ret;
ret = sysconfig_poll_status(priv, &status);
if (ret)
return ret;
if (status & SYSCONFIG_STATUS_FAIL)
return -EFAULT;
return 0;
}
static int sysconfig_isc_init(struct sysconfig_priv *priv)
{
int ret = sysconfig_isc_enable(priv);
if (ret)
return ret;
return sysconfig_isc_erase(priv);
}
static int sysconfig_lsc_init_addr(struct sysconfig_priv *priv)
{
const u8 lsc_init_addr[] = SYSCONFIG_LSC_INIT_ADDR;
return sysconfig_cmd_write(priv, lsc_init_addr, sizeof(lsc_init_addr));
}
static int sysconfig_burst_write_init(struct sysconfig_priv *priv)
{
return priv->bitstream_burst_write_init(priv);
}
static int sysconfig_burst_write_complete(struct sysconfig_priv *priv)
{
return priv->bitstream_burst_write_complete(priv);
}
static int sysconfig_bitstream_burst_write(struct sysconfig_priv *priv,
const char *buf, size_t count)
{
int ret = priv->bitstream_burst_write(priv, buf, count);
if (ret)
sysconfig_burst_write_complete(priv);
return ret;
}
static int sysconfig_isc_disable(struct sysconfig_priv *priv)
{
const u8 isc_disable[] = SYSCONFIG_ISC_DISABLE;
return sysconfig_cmd_write(priv, isc_disable, sizeof(isc_disable));
}
static void sysconfig_cleanup(struct sysconfig_priv *priv)
{
sysconfig_isc_erase(priv);
sysconfig_refresh(priv);
}
static int sysconfig_isc_finish(struct sysconfig_priv *priv)
{
struct gpio_desc *done_gpio = priv->done;
u32 status;
int ret;
if (done_gpio) {
ret = sysconfig_isc_disable(priv);
if (ret)
return ret;
return sysconfig_poll_gpio(done_gpio, true);
}
ret = sysconfig_poll_status(priv, &status);
if (ret)
return ret;
if ((status & SYSCONFIG_STATUS_DONE) &&
!(status & SYSCONFIG_STATUS_BUSY) &&
!(status & SYSCONFIG_STATUS_ERR))
return sysconfig_isc_disable(priv);
return -EFAULT;
}
static enum fpga_mgr_states sysconfig_ops_state(struct fpga_manager *mgr)
{
struct sysconfig_priv *priv = mgr->priv;
struct gpio_desc *done = priv->done;
u32 status;
int ret;
if (done && (gpiod_get_value(done) > 0))
return FPGA_MGR_STATE_OPERATING;
ret = sysconfig_read_status(priv, &status);
if (!ret && (status & SYSCONFIG_STATUS_DONE))
return FPGA_MGR_STATE_OPERATING;
return FPGA_MGR_STATE_UNKNOWN;
}
static int sysconfig_ops_write_init(struct fpga_manager *mgr,
struct fpga_image_info *info,
const char *buf, size_t count)
{
struct sysconfig_priv *priv = mgr->priv;
struct device *dev = &mgr->dev;
int ret;
if (info->flags & FPGA_MGR_PARTIAL_RECONFIG) {
dev_err(dev, "Partial reconfiguration is not supported\n");
return -EOPNOTSUPP;
}
/* Enter program mode */
ret = sysconfig_refresh(priv);
if (ret) {
dev_err(dev, "Failed to go to program mode\n");
return ret;
}
/* Enter ISC mode */
ret = sysconfig_isc_init(priv);
if (ret) {
dev_err(dev, "Failed to go to ISC mode\n");
return ret;
}
/* Initialize the Address Shift Register */
ret = sysconfig_lsc_init_addr(priv);
if (ret) {
dev_err(dev,
"Failed to initialize the Address Shift Register\n");
return ret;
}
/* Prepare for bitstream burst write */
ret = sysconfig_burst_write_init(priv);
if (ret)
dev_err(dev, "Failed to prepare for bitstream burst write\n");
return ret;
}
static int sysconfig_ops_write(struct fpga_manager *mgr, const char *buf,
size_t count)
{
return sysconfig_bitstream_burst_write(mgr->priv, buf, count);
}
static int sysconfig_ops_write_complete(struct fpga_manager *mgr,
struct fpga_image_info *info)
{
struct sysconfig_priv *priv = mgr->priv;
struct device *dev = &mgr->dev;
int ret;
ret = sysconfig_burst_write_complete(priv);
if (!ret)
ret = sysconfig_poll_busy(priv);
if (ret) {
dev_err(dev, "Error while waiting bitstream write to finish\n");
goto fail;
}
ret = sysconfig_isc_finish(priv);
fail:
if (ret)
sysconfig_cleanup(priv);
return ret;
}
static const struct fpga_manager_ops sysconfig_fpga_mgr_ops = {
.state = sysconfig_ops_state,
.write_init = sysconfig_ops_write_init,
.write = sysconfig_ops_write,
.write_complete = sysconfig_ops_write_complete,
};
int sysconfig_probe(struct sysconfig_priv *priv)
{
struct gpio_desc *program, *init, *done;
struct device *dev = priv->dev;
struct fpga_manager *mgr;
if (!dev)
return -ENODEV;
if (!priv->command_transfer ||
!priv->bitstream_burst_write_init ||
!priv->bitstream_burst_write ||
!priv->bitstream_burst_write_complete) {
dev_err(dev, "Essential callback is missing\n");
return -EINVAL;
}
program = devm_gpiod_get_optional(dev, "program", GPIOD_OUT_LOW);
if (IS_ERR(program))
return dev_err_probe(dev, PTR_ERR(program),
"Failed to get PROGRAM GPIO\n");
init = devm_gpiod_get_optional(dev, "init", GPIOD_IN);
if (IS_ERR(init))
return dev_err_probe(dev, PTR_ERR(init),
"Failed to get INIT GPIO\n");
done = devm_gpiod_get_optional(dev, "done", GPIOD_IN);
if (IS_ERR(done))
return dev_err_probe(dev, PTR_ERR(done),
"Failed to get DONE GPIO\n");
priv->program = program;
priv->init = init;
priv->done = done;
mgr = devm_fpga_mgr_register(dev, "Lattice sysCONFIG FPGA Manager",
&sysconfig_fpga_mgr_ops, priv);
return PTR_ERR_OR_ZERO(mgr);
}
EXPORT_SYMBOL(sysconfig_probe);
MODULE_DESCRIPTION("Lattice sysCONFIG FPGA Manager Core");
MODULE_LICENSE("GPL");
|