2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 #include <linux/kernel.h>
18 #include <linux/init.h>
19 #include <linux/module.h>
20 #include <linux/moduleparam.h>
21 #include <linux/types.h>
22 #include <linux/device.h>
23 #include <linux/string.h>
24 #include <linux/slab.h>
26 #include <linux/platform_device.h>
28 #include <linux/delay.h>
30 #include <linux/firmware.h>
32 #include "gs_fpgaboot.h"
35 #define DEVICE_NAME "device"
36 #define CLASS_NAME "fpgaboot"
38 static uint8_t bits_magic[] = {
39 0x0, 0x9, 0xf, 0xf0, 0xf, 0xf0,
40 0xf, 0xf0, 0xf, 0xf0, 0x0, 0x0, 0x1};
42 /* fake device for request_firmware */
43 static struct platform_device *firmware_pdev;
45 static char *file = "xlinx_fpga_firmware.bit";
46 module_param(file, charp, S_IRUGO);
47 MODULE_PARM_DESC(file, "Xilinx FPGA firmware file.");
50 static void datadump(char *msg, void *m, int n)
55 pr_info("=== %s ===\n", msg);
59 for (i = 0; i < n; i++) {
61 pr_info(KERN_INFO "\n 0x%4x: ", i);
63 pr_info("%02X ", c[i]);
68 #endif /* DEBUG_FPGA */
70 static void read_bitstream(char *bitdata, char *buf, int *offset, int rdsize)
72 memcpy(buf, bitdata + *offset, rdsize);
76 static void readinfo_bitstream(char *bitdata, char *buf, int *offset)
81 /* read section char */
82 read_bitstream(bitdata, tbuf, offset, 1);
85 read_bitstream(bitdata, tbuf, offset, 2);
87 len = tbuf[0] << 8 | tbuf[1];
89 read_bitstream(bitdata, buf, offset, len);
96 static int readlength_bitstream(char *bitdata, int *lendata, int *offset)
100 /* read section char */
101 read_bitstream(bitdata, tbuf, offset, 1);
103 /* make sure it is section 'e' */
104 if (tbuf[0] != 'e') {
105 pr_err("error: length section is not 'e', but %c\n", tbuf[0]);
109 /* read 4bytes length */
110 read_bitstream(bitdata, tbuf, offset, 4);
112 *lendata = tbuf[0] << 24 | tbuf[1] << 16 |
113 tbuf[2] << 8 | tbuf[3];
120 * read first 13 bytes to check bitstream magic number
122 static int readmagic_bitstream(char *bitdata, int *offset)
127 read_bitstream(bitdata, buf, offset, 13);
128 r = memcmp(buf, bits_magic, 13);
130 pr_err("error: corrupted header");
133 pr_info("bitstream file magic number Ok\n");
135 *offset = 13; /* magic length */
141 * NOTE: supports only bitstream format
143 static enum fmt_image get_imageformat(struct fpgaimage *fimage)
148 static void gs_print_header(struct fpgaimage *fimage)
150 pr_info("file: %s\n", fimage->filename);
151 pr_info("part: %s\n", fimage->part);
152 pr_info("date: %s\n", fimage->date);
153 pr_info("time: %s\n", fimage->time);
154 pr_info("lendata: %d\n", fimage->lendata);
157 static void gs_read_bitstream(struct fpgaimage *fimage)
164 bitdata = (char *)fimage->fw_entry->data;
165 size = fimage->fw_entry->size;
167 readmagic_bitstream(bitdata, &offset);
168 readinfo_bitstream(bitdata, fimage->filename, &offset);
169 readinfo_bitstream(bitdata, fimage->part, &offset);
170 readinfo_bitstream(bitdata, fimage->date, &offset);
171 readinfo_bitstream(bitdata, fimage->time, &offset);
172 readlength_bitstream(bitdata, &fimage->lendata, &offset);
174 fimage->fpgadata = bitdata + offset;
177 static int gs_read_image(struct fpgaimage *fimage)
181 img_fmt = get_imageformat(fimage);
185 pr_info("image is bitstream format\n");
186 gs_read_bitstream(fimage);
189 pr_err("unsupported fpga image format\n");
193 gs_print_header(fimage);
198 static int gs_load_image(struct fpgaimage *fimage, char *file)
202 pr_info("load fpgaimage %s\n", file);
204 err = request_firmware(&fimage->fw_entry, file, &firmware_pdev->dev);
206 pr_err("firmware %s is missing, cannot continue.\n", file);
213 static int gs_download_image(struct fpgaimage *fimage, enum wbus bus_bytes)
219 bitdata = (char *)fimage->fpgadata;
220 size = fimage->lendata;
223 datadump("bitfile sample", bitdata, 0x100);
224 #endif /* DEBUG_FPGA */
226 if (!xl_supported_prog_bus_width(bus_bytes)) {
227 pr_err("unsupported program bus width %d\n",
232 /* Bring csi_b, rdwr_b Low and program_b High */
237 /* Configuration reset */
242 /* Wait for Device Initialization */
243 while (xl_get_init_b() == 0)
246 pr_info("device init done\n");
248 for (i = 0; i < size; i += bus_bytes)
249 xl_shift_bytes_out(bus_bytes, bitdata+i);
251 pr_info("program done\n");
254 if (xl_get_init_b() == 0) {
255 pr_err("init_b 0\n");
259 while (xl_get_done_b() == 0) {
260 if (cnt++ > MAX_WAIT_DONE) {
261 pr_err("init_B %d\n", xl_get_init_b());
266 if (cnt > MAX_WAIT_DONE) {
267 pr_err("fpga download fail\n");
271 pr_info("download fpgaimage\n");
273 /* Compensate for Special Startup Conditions */
279 static int gs_release_image(struct fpgaimage *fimage)
281 release_firmware(fimage->fw_entry);
282 pr_info("release fpgaimage\n");
288 * NOTE: supports systemmap parallel programming
290 static int gs_set_download_method(struct fpgaimage *fimage)
292 pr_info("set program method\n");
294 fimage->dmethod = m_systemmap;
296 pr_info("systemmap program method\n");
301 static int init_driver(void)
303 firmware_pdev = platform_device_register_simple("fpgaboot", -1,
305 return PTR_ERR_OR_ZERO(firmware_pdev);
308 static void finish_driver(void)
310 platform_device_unregister(firmware_pdev);
313 static int gs_fpgaboot(void)
316 struct fpgaimage *fimage;
318 fimage = kmalloc(sizeof(struct fpgaimage), GFP_KERNEL);
319 if (fimage == NULL) {
320 pr_err("No memory is available\n");
324 err = gs_load_image(fimage, file);
326 pr_err("gs_load_image error\n");
330 err = gs_read_image(fimage);
332 pr_err("gs_read_image error\n");
336 err = gs_set_download_method(fimage);
338 pr_err("gs_set_download_method error\n");
342 err = gs_download_image(fimage, bus_2byte);
344 pr_err("gs_download_image error\n");
348 err = gs_release_image(fimage);
350 pr_err("gs_release_image error\n");
358 err = gs_release_image(fimage);
360 pr_err("gs_release_image error\n");
369 static int __init gs_fpgaboot_init(void)
375 pr_info("FPGA DOWNLOAD --->\n");
377 pr_info("FPGA image file name: %s\n", file);
381 pr_err("FPGA DRIVER INIT FAIL!!\n");
387 pr_err("GPIO INIT FAIL!!\n");
394 pr_err("FPGA DOWNLOAD FAIL!!\n");
399 pr_info("FPGA DOWNLOAD DONE <---\n");
410 static void __exit gs_fpgaboot_exit(void)
413 pr_info("FPGA image download module removed\n");
416 module_init(gs_fpgaboot_init);
417 module_exit(gs_fpgaboot_exit);
419 MODULE_AUTHOR("Insop Song");
420 MODULE_DESCRIPTION("Xlinix FPGA firmware download");
421 MODULE_LICENSE("GPL");