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