lattice-ecp3-config.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /*
  2. * Copyright (C) 2012 Stefan Roese <sr@denx.de>
  3. *
  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.
  8. */
  9. #include <linux/device.h>
  10. #include <linux/firmware.h>
  11. #include <linux/module.h>
  12. #include <linux/errno.h>
  13. #include <linux/kernel.h>
  14. #include <linux/spi/spi.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/delay.h>
  17. #include <asm/unaligned.h>
  18. #define FIRMWARE_NAME "lattice-ecp3.bit"
  19. /*
  20. * The JTAG ID's of the supported FPGA's. The ID is 32bit wide
  21. * reversed as noted in the manual.
  22. */
  23. #define ID_ECP3_17 0xc2088080
  24. #define ID_ECP3_35 0xc2048080
  25. /* FPGA commands */
  26. #define FPGA_CMD_READ_ID 0x07 /* plus 24 bits */
  27. #define FPGA_CMD_READ_STATUS 0x09 /* plus 24 bits */
  28. #define FPGA_CMD_CLEAR 0x70
  29. #define FPGA_CMD_REFRESH 0x71
  30. #define FPGA_CMD_WRITE_EN 0x4a /* plus 2 bits */
  31. #define FPGA_CMD_WRITE_DIS 0x4f /* plus 8 bits */
  32. #define FPGA_CMD_WRITE_INC 0x41 /* plus 0 bits */
  33. /*
  34. * The status register is 32bit revered, DONE is bit 17 from the TN1222.pdf
  35. * (LatticeECP3 Slave SPI Port User's Guide)
  36. */
  37. #define FPGA_STATUS_DONE 0x00004000
  38. #define FPGA_STATUS_CLEARED 0x00010000
  39. #define FPGA_CLEAR_TIMEOUT 5000 /* max. 5000ms for FPGA clear */
  40. #define FPGA_CLEAR_MSLEEP 10
  41. #define FPGA_CLEAR_LOOP_COUNT (FPGA_CLEAR_TIMEOUT / FPGA_CLEAR_MSLEEP)
  42. struct fpga_data {
  43. struct completion fw_loaded;
  44. };
  45. struct ecp3_dev {
  46. u32 jedec_id;
  47. char *name;
  48. };
  49. static const struct ecp3_dev ecp3_dev[] = {
  50. {
  51. .jedec_id = ID_ECP3_17,
  52. .name = "Lattice ECP3-17",
  53. },
  54. {
  55. .jedec_id = ID_ECP3_35,
  56. .name = "Lattice ECP3-35",
  57. },
  58. };
  59. static void firmware_load(const struct firmware *fw, void *context)
  60. {
  61. struct spi_device *spi = (struct spi_device *)context;
  62. struct fpga_data *data = spi_get_drvdata(spi);
  63. u8 *buffer;
  64. int ret;
  65. u8 txbuf[8];
  66. u8 rxbuf[8];
  67. int rx_len = 8;
  68. int i;
  69. u32 jedec_id;
  70. u32 status;
  71. if (fw == NULL) {
  72. dev_err(&spi->dev, "Cannot load firmware, aborting\n");
  73. return;
  74. }
  75. if (fw->size == 0) {
  76. dev_err(&spi->dev, "Error: Firmware size is 0!\n");
  77. return;
  78. }
  79. /* Fill dummy data (24 stuffing bits for commands) */
  80. txbuf[1] = 0x00;
  81. txbuf[2] = 0x00;
  82. txbuf[3] = 0x00;
  83. /* Trying to speak with the FPGA via SPI... */
  84. txbuf[0] = FPGA_CMD_READ_ID;
  85. ret = spi_write_then_read(spi, txbuf, 8, rxbuf, rx_len);
  86. jedec_id = get_unaligned_be32(&rxbuf[4]);
  87. dev_dbg(&spi->dev, "FPGA JTAG ID=%08x\n", jedec_id);
  88. for (i = 0; i < ARRAY_SIZE(ecp3_dev); i++) {
  89. if (jedec_id == ecp3_dev[i].jedec_id)
  90. break;
  91. }
  92. if (i == ARRAY_SIZE(ecp3_dev)) {
  93. dev_err(&spi->dev,
  94. "Error: No supported FPGA detected (JEDEC_ID=%08x)!\n",
  95. jedec_id);
  96. return;
  97. }
  98. dev_info(&spi->dev, "FPGA %s detected\n", ecp3_dev[i].name);
  99. txbuf[0] = FPGA_CMD_READ_STATUS;
  100. ret = spi_write_then_read(spi, txbuf, 8, rxbuf, rx_len);
  101. status = get_unaligned_be32(&rxbuf[4]);
  102. dev_dbg(&spi->dev, "FPGA Status=%08x\n", status);
  103. buffer = kzalloc(fw->size + 8, GFP_KERNEL);
  104. if (!buffer) {
  105. dev_err(&spi->dev, "Error: Can't allocate memory!\n");
  106. return;
  107. }
  108. /*
  109. * Insert WRITE_INC command into stream (one SPI frame)
  110. */
  111. buffer[0] = FPGA_CMD_WRITE_INC;
  112. buffer[1] = 0xff;
  113. buffer[2] = 0xff;
  114. buffer[3] = 0xff;
  115. memcpy(buffer + 4, fw->data, fw->size);
  116. txbuf[0] = FPGA_CMD_REFRESH;
  117. ret = spi_write(spi, txbuf, 4);
  118. txbuf[0] = FPGA_CMD_WRITE_EN;
  119. ret = spi_write(spi, txbuf, 4);
  120. txbuf[0] = FPGA_CMD_CLEAR;
  121. ret = spi_write(spi, txbuf, 4);
  122. /*
  123. * Wait for FPGA memory to become cleared
  124. */
  125. for (i = 0; i < FPGA_CLEAR_LOOP_COUNT; i++) {
  126. txbuf[0] = FPGA_CMD_READ_STATUS;
  127. ret = spi_write_then_read(spi, txbuf, 8, rxbuf, rx_len);
  128. status = get_unaligned_be32(&rxbuf[4]);
  129. if (status == FPGA_STATUS_CLEARED)
  130. break;
  131. msleep(FPGA_CLEAR_MSLEEP);
  132. }
  133. if (i == FPGA_CLEAR_LOOP_COUNT) {
  134. dev_err(&spi->dev,
  135. "Error: Timeout waiting for FPGA to clear (status=%08x)!\n",
  136. status);
  137. kfree(buffer);
  138. return;
  139. }
  140. dev_info(&spi->dev, "Configuring the FPGA...\n");
  141. ret = spi_write(spi, buffer, fw->size + 8);
  142. txbuf[0] = FPGA_CMD_WRITE_DIS;
  143. ret = spi_write(spi, txbuf, 4);
  144. txbuf[0] = FPGA_CMD_READ_STATUS;
  145. ret = spi_write_then_read(spi, txbuf, 8, rxbuf, rx_len);
  146. status = get_unaligned_be32(&rxbuf[4]);
  147. dev_dbg(&spi->dev, "FPGA Status=%08x\n", status);
  148. /* Check result */
  149. if (status & FPGA_STATUS_DONE)
  150. dev_info(&spi->dev, "FPGA successfully configured!\n");
  151. else
  152. dev_info(&spi->dev, "FPGA not configured (DONE not set)\n");
  153. /*
  154. * Don't forget to release the firmware again
  155. */
  156. release_firmware(fw);
  157. kfree(buffer);
  158. complete(&data->fw_loaded);
  159. }
  160. static int lattice_ecp3_probe(struct spi_device *spi)
  161. {
  162. struct fpga_data *data;
  163. int err;
  164. data = devm_kzalloc(&spi->dev, sizeof(*data), GFP_KERNEL);
  165. if (!data) {
  166. dev_err(&spi->dev, "Memory allocation for fpga_data failed\n");
  167. return -ENOMEM;
  168. }
  169. spi_set_drvdata(spi, data);
  170. init_completion(&data->fw_loaded);
  171. err = request_firmware_nowait(THIS_MODULE, FW_ACTION_HOTPLUG,
  172. FIRMWARE_NAME, &spi->dev,
  173. GFP_KERNEL, spi, firmware_load);
  174. if (err) {
  175. dev_err(&spi->dev, "Firmware loading failed with %d!\n", err);
  176. return err;
  177. }
  178. dev_info(&spi->dev, "FPGA bitstream configuration driver registered\n");
  179. return 0;
  180. }
  181. static int lattice_ecp3_remove(struct spi_device *spi)
  182. {
  183. struct fpga_data *data = spi_get_drvdata(spi);
  184. wait_for_completion(&data->fw_loaded);
  185. return 0;
  186. }
  187. static const struct spi_device_id lattice_ecp3_id[] = {
  188. { "ecp3-17", 0 },
  189. { "ecp3-35", 0 },
  190. { }
  191. };
  192. MODULE_DEVICE_TABLE(spi, lattice_ecp3_id);
  193. static struct spi_driver lattice_ecp3_driver = {
  194. .driver = {
  195. .name = "lattice-ecp3",
  196. },
  197. .probe = lattice_ecp3_probe,
  198. .remove = lattice_ecp3_remove,
  199. .id_table = lattice_ecp3_id,
  200. };
  201. module_spi_driver(lattice_ecp3_driver);
  202. MODULE_AUTHOR("Stefan Roese <sr@denx.de>");
  203. MODULE_DESCRIPTION("Lattice ECP3 FPGA configuration via SPI");
  204. MODULE_LICENSE("GPL");
  205. MODULE_FIRMWARE(FIRMWARE_NAME);