Merge tag 'v4.0-rc1' into locking/core, to refresh the tree before merging new changes
[firefly-linux-kernel-4.4.55.git] / drivers / staging / gs_fpgaboot / gs_fpgaboot.c
1 /*
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.
6  *
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.
11  *
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.
15  */
16
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>
25 #include <linux/fs.h>
26 #include <linux/platform_device.h>
27 #include <linux/of.h>
28 #include <linux/delay.h>
29 #include <linux/io.h>
30 #include <linux/firmware.h>
31
32 #include "gs_fpgaboot.h"
33 #include "io.h"
34
35 #define DEVICE_NAME "device"
36 #define CLASS_NAME  "fpgaboot"
37
38 static uint8_t bits_magic[] = {
39         0x0, 0x9, 0xf, 0xf0, 0xf, 0xf0,
40         0xf, 0xf0, 0xf, 0xf0, 0x0, 0x0, 0x1};
41
42 /* fake device for request_firmware */
43 static struct platform_device   *firmware_pdev;
44
45 static char     *file = "xlinx_fpga_firmware.bit";
46 module_param(file, charp, S_IRUGO);
47 MODULE_PARM_DESC(file, "Xilinx FPGA firmware file.");
48
49 static void read_bitstream(char *bitdata, char *buf, int *offset, int rdsize)
50 {
51         memcpy(buf, bitdata + *offset, rdsize);
52         *offset += rdsize;
53 }
54
55 static void readinfo_bitstream(char *bitdata, char *buf, int *offset)
56 {
57         char tbuf[64];
58         int32_t len;
59
60         /* read section char */
61         read_bitstream(bitdata, tbuf, offset, 1);
62
63         /* read length */
64         read_bitstream(bitdata, tbuf, offset, 2);
65
66         len = tbuf[0] << 8 | tbuf[1];
67
68         read_bitstream(bitdata, buf, offset, len);
69         buf[len] = '\0';
70 }
71
72 /*
73  * read bitdata length
74  */
75 static int readlength_bitstream(char *bitdata, int *lendata, int *offset)
76 {
77         char tbuf[64];
78
79         /* read section char */
80         read_bitstream(bitdata, tbuf, offset, 1);
81
82         /* make sure it is section 'e' */
83         if (tbuf[0] != 'e') {
84                 pr_err("error: length section is not 'e', but %c\n", tbuf[0]);
85                 return -1;
86         }
87
88         /* read 4bytes length */
89         read_bitstream(bitdata, tbuf, offset, 4);
90
91         *lendata = tbuf[0] << 24 | tbuf[1] << 16 |
92                 tbuf[2] << 8 | tbuf[3];
93
94         return 0;
95 }
96
97
98 /*
99  * read first 13 bytes to check bitstream magic number
100  */
101 static int readmagic_bitstream(char *bitdata, int *offset)
102 {
103         char buf[13];
104         int r;
105
106         read_bitstream(bitdata, buf, offset, 13);
107         r = memcmp(buf, bits_magic, 13);
108         if (r) {
109                 pr_err("error: corrupted header");
110                 return -1;
111         }
112         pr_info("bitstream file magic number Ok\n");
113
114         *offset = 13;   /* magic length */
115
116         return 0;
117 }
118
119 /*
120  * NOTE: supports only bitstream format
121  */
122 static enum fmt_image get_imageformat(struct fpgaimage *fimage)
123 {
124         return f_bit;
125 }
126
127 static void gs_print_header(struct fpgaimage *fimage)
128 {
129         pr_info("file: %s\n", fimage->filename);
130         pr_info("part: %s\n", fimage->part);
131         pr_info("date: %s\n", fimage->date);
132         pr_info("time: %s\n", fimage->time);
133         pr_info("lendata: %d\n", fimage->lendata);
134 }
135
136 static void gs_read_bitstream(struct fpgaimage *fimage)
137 {
138         char *bitdata;
139         int offset;
140
141         offset = 0;
142         bitdata = (char *)fimage->fw_entry->data;
143
144         readmagic_bitstream(bitdata, &offset);
145         readinfo_bitstream(bitdata, fimage->filename, &offset);
146         readinfo_bitstream(bitdata, fimage->part, &offset);
147         readinfo_bitstream(bitdata, fimage->date, &offset);
148         readinfo_bitstream(bitdata, fimage->time, &offset);
149         readlength_bitstream(bitdata, &fimage->lendata, &offset);
150
151         fimage->fpgadata = bitdata + offset;
152 }
153
154 static int gs_read_image(struct fpgaimage *fimage)
155 {
156         int img_fmt;
157
158         img_fmt = get_imageformat(fimage);
159
160         switch (img_fmt) {
161         case f_bit:
162                 pr_info("image is bitstream format\n");
163                 gs_read_bitstream(fimage);
164                 break;
165         default:
166                 pr_err("unsupported fpga image format\n");
167                 return -1;
168         }
169
170         gs_print_header(fimage);
171
172         return 0;
173 }
174
175 static int gs_load_image(struct fpgaimage *fimage, char *fw_file)
176 {
177         int err;
178
179         pr_info("load fpgaimage %s\n", fw_file);
180
181         err = request_firmware(&fimage->fw_entry, fw_file, &firmware_pdev->dev);
182         if (err != 0) {
183                 pr_err("firmware %s is missing, cannot continue.\n", fw_file);
184                 return err;
185         }
186
187         return 0;
188 }
189
190 static int gs_download_image(struct fpgaimage *fimage, enum wbus bus_bytes)
191 {
192         char *bitdata;
193         int size, i, cnt;
194
195         cnt = 0;
196         bitdata = (char *)fimage->fpgadata;
197         size = fimage->lendata;
198
199 #ifdef DEBUG_FPGA
200         print_hex_dump_bytes("bitfile sample: ", DUMP_PREFIX_OFFSET,
201                              bitdata, 0x100);
202 #endif /* DEBUG_FPGA */
203         if (!xl_supported_prog_bus_width(bus_bytes)) {
204                 pr_err("unsupported program bus width %d\n",
205                                 bus_bytes);
206                 return -1;
207         }
208
209         /* Bring csi_b, rdwr_b Low and program_b High */
210         xl_program_b(1);
211         xl_rdwr_b(0);
212         xl_csi_b(0);
213
214         /* Configuration reset */
215         xl_program_b(0);
216         msleep(20);
217         xl_program_b(1);
218
219         /* Wait for Device Initialization */
220         while (xl_get_init_b() == 0)
221                 ;
222
223         pr_info("device init done\n");
224
225         for (i = 0; i < size; i += bus_bytes)
226                 xl_shift_bytes_out(bus_bytes, bitdata+i);
227
228         pr_info("program done\n");
229
230         /* Check INIT_B */
231         if (xl_get_init_b() == 0) {
232                 pr_err("init_b 0\n");
233                 return -1;
234         }
235
236         while (xl_get_done_b() == 0) {
237                 if (cnt++ > MAX_WAIT_DONE) {
238                         pr_err("init_B %d\n", xl_get_init_b());
239                         break;
240                 }
241         }
242
243         if (cnt > MAX_WAIT_DONE) {
244                 pr_err("fpga download fail\n");
245                 return -1;
246         }
247
248         pr_info("download fpgaimage\n");
249
250         /* Compensate for Special Startup Conditions */
251         xl_shift_cclk(8);
252
253         return 0;
254 }
255
256 static int gs_release_image(struct fpgaimage *fimage)
257 {
258         release_firmware(fimage->fw_entry);
259         pr_info("release fpgaimage\n");
260
261         return 0;
262 }
263
264 /*
265  * NOTE: supports systemmap parallel programming
266  */
267 static int gs_set_download_method(struct fpgaimage *fimage)
268 {
269         pr_info("set program method\n");
270
271         fimage->dmethod = m_systemmap;
272
273         pr_info("systemmap program method\n");
274
275         return 0;
276 }
277
278 static int init_driver(void)
279 {
280         firmware_pdev = platform_device_register_simple("fpgaboot", -1,
281                                                          NULL, 0);
282         return PTR_ERR_OR_ZERO(firmware_pdev);
283 }
284
285 static void finish_driver(void)
286 {
287         platform_device_unregister(firmware_pdev);
288 }
289
290 static int gs_fpgaboot(void)
291 {
292         int err;
293         struct fpgaimage        *fimage;
294
295         fimage = kmalloc(sizeof(struct fpgaimage), GFP_KERNEL);
296         if (!fimage)
297                 return -ENOMEM;
298
299         err = gs_load_image(fimage, file);
300         if (err) {
301                 pr_err("gs_load_image error\n");
302                 goto err_out1;
303         }
304
305         err = gs_read_image(fimage);
306         if (err) {
307                 pr_err("gs_read_image error\n");
308                 goto err_out2;
309         }
310
311         err = gs_set_download_method(fimage);
312         if (err) {
313                 pr_err("gs_set_download_method error\n");
314                 goto err_out2;
315         }
316
317         err = gs_download_image(fimage, bus_2byte);
318         if (err) {
319                 pr_err("gs_download_image error\n");
320                 goto err_out2;
321         }
322
323         err = gs_release_image(fimage);
324         if (err) {
325                 pr_err("gs_release_image error\n");
326                 goto err_out1;
327         }
328
329         kfree(fimage);
330         return 0;
331
332 err_out2:
333         err = gs_release_image(fimage);
334         if (err)
335                 pr_err("gs_release_image error\n");
336 err_out1:
337         kfree(fimage);
338
339         return -1;
340
341 }
342
343 static int __init gs_fpgaboot_init(void)
344 {
345         int err;
346
347         pr_info("FPGA DOWNLOAD --->\n");
348
349         pr_info("FPGA image file name: %s\n", file);
350
351         err = init_driver();
352         if (err) {
353                 pr_err("FPGA DRIVER INIT FAIL!!\n");
354                 return err;
355         }
356
357         err = xl_init_io();
358         if (err) {
359                 pr_err("GPIO INIT FAIL!!\n");
360                 goto errout;
361         }
362
363         err = gs_fpgaboot();
364         if (err) {
365                 pr_err("FPGA DOWNLOAD FAIL!!\n");
366                 goto errout;
367         }
368
369         pr_info("FPGA DOWNLOAD DONE <---\n");
370
371         return 0;
372
373 errout:
374         finish_driver();
375
376         return err;
377 }
378
379 static void __exit gs_fpgaboot_exit(void)
380 {
381         finish_driver();
382         pr_info("FPGA image download module removed\n");
383 }
384
385 module_init(gs_fpgaboot_init);
386 module_exit(gs_fpgaboot_exit);
387
388 MODULE_AUTHOR("Insop Song");
389 MODULE_DESCRIPTION("Xlinix FPGA firmware download");
390 MODULE_LICENSE("GPL");