gs_fpgaboot.c 7.7 KB

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