2 * Altera Passive Serial SPI Driver
4 * Copyright (c) 2017 United Western Technologies, Corporation
6 * Joshua Clayton <stillcompiling@gmail.com>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms and conditions of the GNU General Public License,
10 * version 2, as published by the Free Software Foundation.
12 * Manage Altera FPGA firmware that is loaded over SPI using the passive
13 * serial configuration method.
14 * Firmware must be in binary "rbf" format.
15 * Works on Arria 10, Cyclone V and Stratix V. Should work on Cyclone series.
16 * May work on other Altera FPGAs.
19 #include <linux/bitrev.h>
20 #include <linux/delay.h>
21 #include <linux/fpga/fpga-mgr.h>
22 #include <linux/gpio/consumer.h>
23 #include <linux/module.h>
24 #include <linux/of_gpio.h>
25 #include <linux/of_device.h>
26 #include <linux/spi/spi.h>
27 #include <linux/sizes.h>
29 enum altera_ps_devtype
{
34 struct altera_ps_data
{
35 enum altera_ps_devtype devtype
;
36 int status_wait_min_us
;
37 int status_wait_max_us
;
42 struct altera_ps_conf
{
43 struct gpio_desc
*config
;
44 struct gpio_desc
*confd
;
45 struct gpio_desc
*status
;
46 struct spi_device
*spi
;
47 const struct altera_ps_data
*data
;
52 /* | Arria 10 | Cyclone5 | Stratix5 |
53 * t_CF2ST0 | [; 600] | [; 600] | [; 600] |ns
54 * t_CFG | [2;] | [2;] | [2;] |µs
55 * t_STATUS | [268; 3000] | [268; 1506] | [268; 1506] |µs
56 * t_CF2ST1 | [; 3000] | [; 1506] | [; 1506] |µs
57 * t_CF2CK | [3010;] | [1506;] | [1506;] |µs
58 * t_ST2CK | [10;] | [2;] | [2;] |µs
59 * t_CD2UM | [175; 830] | [175; 437] | [175; 437] |µs
61 static struct altera_ps_data c5_data
= {
62 /* these values for Cyclone5 are compatible with Stratix5 */
64 .status_wait_min_us
= 268,
65 .status_wait_max_us
= 1506,
70 static struct altera_ps_data a10_data
= {
72 .status_wait_min_us
= 268, /* min(t_STATUS) */
73 .status_wait_max_us
= 3000, /* max(t_CF2ST1) */
74 .t_cfg_us
= 2, /* max { min(t_CFG), max(tCF2ST0) } */
75 .t_st2ck_us
= 10, /* min(t_ST2CK) */
78 static const struct of_device_id of_ef_match
[] = {
79 { .compatible
= "altr,fpga-passive-serial", .data
= &c5_data
},
80 { .compatible
= "altr,fpga-arria10-passive-serial", .data
= &a10_data
},
83 MODULE_DEVICE_TABLE(of
, of_ef_match
);
85 static enum fpga_mgr_states
altera_ps_state(struct fpga_manager
*mgr
)
87 struct altera_ps_conf
*conf
= mgr
->priv
;
89 if (gpiod_get_value_cansleep(conf
->status
))
90 return FPGA_MGR_STATE_RESET
;
92 return FPGA_MGR_STATE_UNKNOWN
;
95 static inline void altera_ps_delay(int delay_us
)
98 usleep_range(delay_us
, delay_us
+ 5);
103 static int altera_ps_write_init(struct fpga_manager
*mgr
,
104 struct fpga_image_info
*info
,
105 const char *buf
, size_t count
)
107 struct altera_ps_conf
*conf
= mgr
->priv
;
111 conf
->info_flags
= info
->flags
;
113 if (info
->flags
& FPGA_MGR_PARTIAL_RECONFIG
) {
114 dev_err(&mgr
->dev
, "Partial reconfiguration not supported.\n");
118 gpiod_set_value_cansleep(conf
->config
, 1);
120 /* wait min reset pulse time */
121 altera_ps_delay(conf
->data
->t_cfg_us
);
123 if (!gpiod_get_value_cansleep(conf
->status
)) {
124 dev_err(&mgr
->dev
, "Status pin failed to show a reset\n");
128 gpiod_set_value_cansleep(conf
->config
, 0);
130 min
= conf
->data
->status_wait_min_us
;
131 max
= conf
->data
->status_wait_max_us
;
136 /* wait for max { max(t_STATUS), max(t_CF2ST1) } */
137 for (i
= 0; i
< waits
; i
++) {
138 usleep_range(min
, min
+ 10);
139 if (!gpiod_get_value_cansleep(conf
->status
)) {
140 /* wait for min(t_ST2CK)*/
141 altera_ps_delay(conf
->data
->t_st2ck_us
);
146 dev_err(&mgr
->dev
, "Status pin not ready.\n");
150 static void rev_buf(char *buf
, size_t len
)
152 u32
*fw32
= (u32
*)buf
;
153 size_t extra_bytes
= (len
& 0x03);
154 const u32
*fw_end
= (u32
*)(buf
+ len
- extra_bytes
);
156 /* set buffer to lsb first */
157 while (fw32
< fw_end
) {
158 *fw32
= bitrev8x4(*fw32
);
163 buf
= (char *)fw_end
;
164 while (extra_bytes
) {
165 *buf
= bitrev8(*buf
);
172 static int altera_ps_write(struct fpga_manager
*mgr
, const char *buf
,
175 struct altera_ps_conf
*conf
= mgr
->priv
;
176 const char *fw_data
= buf
;
177 const char *fw_data_end
= fw_data
+ count
;
179 while (fw_data
< fw_data_end
) {
181 size_t stride
= min_t(size_t, fw_data_end
- fw_data
, SZ_4K
);
183 if (!(conf
->info_flags
& FPGA_MGR_BITSTREAM_LSB_FIRST
))
184 rev_buf((char *)fw_data
, stride
);
186 ret
= spi_write(conf
->spi
, fw_data
, stride
);
188 dev_err(&mgr
->dev
, "spi error in firmware write: %d\n",
198 static int altera_ps_write_complete(struct fpga_manager
*mgr
,
199 struct fpga_image_info
*info
)
201 struct altera_ps_conf
*conf
= mgr
->priv
;
202 const char dummy
[] = {0};
205 if (gpiod_get_value_cansleep(conf
->status
)) {
206 dev_err(&mgr
->dev
, "Error during configuration.\n");
210 if (!IS_ERR(conf
->confd
)) {
211 if (!gpiod_get_raw_value_cansleep(conf
->confd
)) {
212 dev_err(&mgr
->dev
, "CONF_DONE is inactive!\n");
218 * After CONF_DONE goes high, send two additional falling edges on DCLK
219 * to begin initialization and enter user mode
221 ret
= spi_write(conf
->spi
, dummy
, 1);
223 dev_err(&mgr
->dev
, "spi error during end sequence: %d\n", ret
);
230 static const struct fpga_manager_ops altera_ps_ops
= {
231 .state
= altera_ps_state
,
232 .write_init
= altera_ps_write_init
,
233 .write
= altera_ps_write
,
234 .write_complete
= altera_ps_write_complete
,
237 static int altera_ps_probe(struct spi_device
*spi
)
239 struct altera_ps_conf
*conf
;
240 const struct of_device_id
*of_id
;
241 struct fpga_manager
*mgr
;
244 conf
= devm_kzalloc(&spi
->dev
, sizeof(*conf
), GFP_KERNEL
);
248 of_id
= of_match_device(of_ef_match
, &spi
->dev
);
252 conf
->data
= of_id
->data
;
254 conf
->config
= devm_gpiod_get(&spi
->dev
, "nconfig", GPIOD_OUT_LOW
);
255 if (IS_ERR(conf
->config
)) {
256 dev_err(&spi
->dev
, "Failed to get config gpio: %ld\n",
257 PTR_ERR(conf
->config
));
258 return PTR_ERR(conf
->config
);
261 conf
->status
= devm_gpiod_get(&spi
->dev
, "nstat", GPIOD_IN
);
262 if (IS_ERR(conf
->status
)) {
263 dev_err(&spi
->dev
, "Failed to get status gpio: %ld\n",
264 PTR_ERR(conf
->status
));
265 return PTR_ERR(conf
->status
);
268 conf
->confd
= devm_gpiod_get(&spi
->dev
, "confd", GPIOD_IN
);
269 if (IS_ERR(conf
->confd
)) {
270 dev_warn(&spi
->dev
, "Not using confd gpio: %ld\n",
271 PTR_ERR(conf
->confd
));
274 /* Register manager with unique name */
275 snprintf(conf
->mgr_name
, sizeof(conf
->mgr_name
), "%s %s",
276 dev_driver_string(&spi
->dev
), dev_name(&spi
->dev
));
278 mgr
= fpga_mgr_create(&spi
->dev
, conf
->mgr_name
,
279 &altera_ps_ops
, conf
);
283 spi_set_drvdata(spi
, mgr
);
285 ret
= fpga_mgr_register(mgr
);
292 static int altera_ps_remove(struct spi_device
*spi
)
294 struct fpga_manager
*mgr
= spi_get_drvdata(spi
);
296 fpga_mgr_unregister(mgr
);
301 static const struct spi_device_id altera_ps_spi_ids
[] = {
302 {"cyclone-ps-spi", 0},
305 MODULE_DEVICE_TABLE(spi
, altera_ps_spi_ids
);
307 static struct spi_driver altera_ps_driver
= {
309 .name
= "altera-ps-spi",
310 .owner
= THIS_MODULE
,
311 .of_match_table
= of_match_ptr(of_ef_match
),
313 .id_table
= altera_ps_spi_ids
,
314 .probe
= altera_ps_probe
,
315 .remove
= altera_ps_remove
,
318 module_spi_driver(altera_ps_driver
)
320 MODULE_LICENSE("GPL v2");
321 MODULE_AUTHOR("Joshua Clayton <stillcompiling@gmail.com>");
322 MODULE_DESCRIPTION("Module to load Altera FPGA firmware over SPI");