2 * Copyright (C) 2012 Stefan Roese <sr@denx.de>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
10 #include <linux/device.h>
11 #include <linux/firmware.h>
12 #include <linux/module.h>
13 #include <linux/errno.h>
14 #include <linux/kernel.h>
15 #include <linux/spi/spi.h>
16 #include <linux/platform_device.h>
17 #include <linux/delay.h>
18 #include <asm/unaligned.h>
20 #define FIRMWARE_NAME "lattice-ecp3.bit"
23 * The JTAG ID's of the supported FPGA's. The ID is 32bit wide
24 * reversed as noted in the manual.
26 #define ID_ECP3_17 0xc2088080
27 #define ID_ECP3_35 0xc2048080
30 #define FPGA_CMD_READ_ID 0x07 /* plus 24 bits */
31 #define FPGA_CMD_READ_STATUS 0x09 /* plus 24 bits */
32 #define FPGA_CMD_CLEAR 0x70
33 #define FPGA_CMD_REFRESH 0x71
34 #define FPGA_CMD_WRITE_EN 0x4a /* plus 2 bits */
35 #define FPGA_CMD_WRITE_DIS 0x4f /* plus 8 bits */
36 #define FPGA_CMD_WRITE_INC 0x41 /* plus 0 bits */
39 * The status register is 32bit revered, DONE is bit 17 from the TN1222.pdf
40 * (LatticeECP3 Slave SPI Port User's Guide)
42 #define FPGA_STATUS_DONE 0x00004000
43 #define FPGA_STATUS_CLEARED 0x00010000
45 #define FPGA_CLEAR_TIMEOUT 5000 /* max. 5000ms for FPGA clear */
46 #define FPGA_CLEAR_MSLEEP 10
47 #define FPGA_CLEAR_LOOP_COUNT (FPGA_CLEAR_TIMEOUT / FPGA_CLEAR_MSLEEP)
50 struct completion fw_loaded
;
58 static const struct ecp3_dev ecp3_dev
[] = {
60 .jedec_id
= ID_ECP3_17
,
61 .name
= "Lattice ECP3-17",
64 .jedec_id
= ID_ECP3_35
,
65 .name
= "Lattice ECP3-35",
69 static void firmware_load(const struct firmware
*fw
, void *context
)
71 struct spi_device
*spi
= (struct spi_device
*)context
;
72 struct fpga_data
*data
= spi_get_drvdata(spi
);
83 dev_err(&spi
->dev
, "Cannot load firmware, aborting\n");
88 dev_err(&spi
->dev
, "Error: Firmware size is 0!\n");
92 /* Fill dummy data (24 stuffing bits for commands) */
97 /* Trying to speak with the FPGA via SPI... */
98 txbuf
[0] = FPGA_CMD_READ_ID
;
99 ret
= spi_write_then_read(spi
, txbuf
, 8, rxbuf
, rx_len
);
100 jedec_id
= get_unaligned_be32(&rxbuf
[4]);
101 dev_dbg(&spi
->dev
, "FPGA JTAG ID=%08x\n", jedec_id
);
103 for (i
= 0; i
< ARRAY_SIZE(ecp3_dev
); i
++) {
104 if (jedec_id
== ecp3_dev
[i
].jedec_id
)
107 if (i
== ARRAY_SIZE(ecp3_dev
)) {
109 "Error: No supported FPGA detected (JEDEC_ID=%08x)!\n",
114 dev_info(&spi
->dev
, "FPGA %s detected\n", ecp3_dev
[i
].name
);
116 txbuf
[0] = FPGA_CMD_READ_STATUS
;
117 ret
= spi_write_then_read(spi
, txbuf
, 8, rxbuf
, rx_len
);
118 status
= get_unaligned_be32(&rxbuf
[4]);
119 dev_dbg(&spi
->dev
, "FPGA Status=%08x\n", status
);
121 buffer
= kzalloc(fw
->size
+ 8, GFP_KERNEL
);
123 dev_err(&spi
->dev
, "Error: Can't allocate memory!\n");
128 * Insert WRITE_INC command into stream (one SPI frame)
130 buffer
[0] = FPGA_CMD_WRITE_INC
;
134 memcpy(buffer
+ 4, fw
->data
, fw
->size
);
136 txbuf
[0] = FPGA_CMD_REFRESH
;
137 ret
= spi_write(spi
, txbuf
, 4);
139 txbuf
[0] = FPGA_CMD_WRITE_EN
;
140 ret
= spi_write(spi
, txbuf
, 4);
142 txbuf
[0] = FPGA_CMD_CLEAR
;
143 ret
= spi_write(spi
, txbuf
, 4);
146 * Wait for FPGA memory to become cleared
148 for (i
= 0; i
< FPGA_CLEAR_LOOP_COUNT
; i
++) {
149 txbuf
[0] = FPGA_CMD_READ_STATUS
;
150 ret
= spi_write_then_read(spi
, txbuf
, 8, rxbuf
, rx_len
);
151 status
= get_unaligned_be32(&rxbuf
[4]);
152 if (status
== FPGA_STATUS_CLEARED
)
155 msleep(FPGA_CLEAR_MSLEEP
);
158 if (i
== FPGA_CLEAR_LOOP_COUNT
) {
160 "Error: Timeout waiting for FPGA to clear (status=%08x)!\n",
166 dev_info(&spi
->dev
, "Configuring the FPGA...\n");
167 ret
= spi_write(spi
, buffer
, fw
->size
+ 8);
169 txbuf
[0] = FPGA_CMD_WRITE_DIS
;
170 ret
= spi_write(spi
, txbuf
, 4);
172 txbuf
[0] = FPGA_CMD_READ_STATUS
;
173 ret
= spi_write_then_read(spi
, txbuf
, 8, rxbuf
, rx_len
);
174 status
= get_unaligned_be32(&rxbuf
[4]);
175 dev_dbg(&spi
->dev
, "FPGA Status=%08x\n", status
);
178 if (status
& FPGA_STATUS_DONE
)
179 dev_info(&spi
->dev
, "FPGA successfully configured!\n");
181 dev_info(&spi
->dev
, "FPGA not configured (DONE not set)\n");
184 * Don't forget to release the firmware again
186 release_firmware(fw
);
190 complete(&data
->fw_loaded
);
193 static int lattice_ecp3_probe(struct spi_device
*spi
)
195 struct fpga_data
*data
;
198 data
= devm_kzalloc(&spi
->dev
, sizeof(*data
), GFP_KERNEL
);
200 dev_err(&spi
->dev
, "Memory allocation for fpga_data failed\n");
203 spi_set_drvdata(spi
, data
);
205 init_completion(&data
->fw_loaded
);
206 err
= request_firmware_nowait(THIS_MODULE
, FW_ACTION_HOTPLUG
,
207 FIRMWARE_NAME
, &spi
->dev
,
208 GFP_KERNEL
, spi
, firmware_load
);
210 dev_err(&spi
->dev
, "Firmware loading failed with %d!\n", err
);
214 dev_info(&spi
->dev
, "FPGA bitstream configuration driver registered\n");
219 static int lattice_ecp3_remove(struct spi_device
*spi
)
221 struct fpga_data
*data
= spi_get_drvdata(spi
);
223 wait_for_completion(&data
->fw_loaded
);
228 static const struct spi_device_id lattice_ecp3_id
[] = {
233 MODULE_DEVICE_TABLE(spi
, lattice_ecp3_id
);
235 static struct spi_driver lattice_ecp3_driver
= {
237 .name
= "lattice-ecp3",
238 .owner
= THIS_MODULE
,
240 .probe
= lattice_ecp3_probe
,
241 .remove
= lattice_ecp3_remove
,
242 .id_table
= lattice_ecp3_id
,
245 module_spi_driver(lattice_ecp3_driver
);
247 MODULE_AUTHOR("Stefan Roese <sr@denx.de>");
248 MODULE_DESCRIPTION("Lattice ECP3 FPGA configuration via SPI");
249 MODULE_LICENSE("GPL");
250 MODULE_FIRMWARE(FIRMWARE_NAME
);