lattice-ecp3-config.c 5.7 KB

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