lattice-ecp3-config.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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/init.h>
  15. #include <linux/spi/spi.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/delay.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 = dev_get_drvdata(&spi->dev);
  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->size == 0) {
  72. dev_err(&spi->dev, "Error: Firmware size is 0!\n");
  73. return;
  74. }
  75. /* Fill dummy data (24 stuffing bits for commands) */
  76. txbuf[1] = 0x00;
  77. txbuf[2] = 0x00;
  78. txbuf[3] = 0x00;
  79. /* Trying to speak with the FPGA via SPI... */
  80. txbuf[0] = FPGA_CMD_READ_ID;
  81. ret = spi_write_then_read(spi, txbuf, 8, rxbuf, rx_len);
  82. dev_dbg(&spi->dev, "FPGA JTAG ID=%08x\n", *(u32 *)&rxbuf[4]);
  83. jedec_id = *(u32 *)&rxbuf[4];
  84. for (i = 0; i < ARRAY_SIZE(ecp3_dev); i++) {
  85. if (jedec_id == ecp3_dev[i].jedec_id)
  86. break;
  87. }
  88. if (i == ARRAY_SIZE(ecp3_dev)) {
  89. dev_err(&spi->dev,
  90. "Error: No supported FPGA detected (JEDEC_ID=%08x)!\n",
  91. jedec_id);
  92. return;
  93. }
  94. dev_info(&spi->dev, "FPGA %s detected\n", ecp3_dev[i].name);
  95. txbuf[0] = FPGA_CMD_READ_STATUS;
  96. ret = spi_write_then_read(spi, txbuf, 8, rxbuf, rx_len);
  97. dev_dbg(&spi->dev, "FPGA Status=%08x\n", *(u32 *)&rxbuf[4]);
  98. buffer = kzalloc(fw->size + 8, GFP_KERNEL);
  99. if (!buffer) {
  100. dev_err(&spi->dev, "Error: Can't allocate memory!\n");
  101. return;
  102. }
  103. /*
  104. * Insert WRITE_INC command into stream (one SPI frame)
  105. */
  106. buffer[0] = FPGA_CMD_WRITE_INC;
  107. buffer[1] = 0xff;
  108. buffer[2] = 0xff;
  109. buffer[3] = 0xff;
  110. memcpy(buffer + 4, fw->data, fw->size);
  111. txbuf[0] = FPGA_CMD_REFRESH;
  112. ret = spi_write(spi, txbuf, 4);
  113. txbuf[0] = FPGA_CMD_WRITE_EN;
  114. ret = spi_write(spi, txbuf, 4);
  115. txbuf[0] = FPGA_CMD_CLEAR;
  116. ret = spi_write(spi, txbuf, 4);
  117. /*
  118. * Wait for FPGA memory to become cleared
  119. */
  120. for (i = 0; i < FPGA_CLEAR_LOOP_COUNT; i++) {
  121. txbuf[0] = FPGA_CMD_READ_STATUS;
  122. ret = spi_write_then_read(spi, txbuf, 8, rxbuf, rx_len);
  123. status = *(u32 *)&rxbuf[4];
  124. if (status == FPGA_STATUS_CLEARED)
  125. break;
  126. msleep(FPGA_CLEAR_MSLEEP);
  127. }
  128. if (i == FPGA_CLEAR_LOOP_COUNT) {
  129. dev_err(&spi->dev,
  130. "Error: Timeout waiting for FPGA to clear (status=%08x)!\n",
  131. status);
  132. kfree(buffer);
  133. return;
  134. }
  135. dev_info(&spi->dev, "Configuring the FPGA...\n");
  136. ret = spi_write(spi, buffer, fw->size + 8);
  137. txbuf[0] = FPGA_CMD_WRITE_DIS;
  138. ret = spi_write(spi, txbuf, 4);
  139. txbuf[0] = FPGA_CMD_READ_STATUS;
  140. ret = spi_write_then_read(spi, txbuf, 8, rxbuf, rx_len);
  141. dev_dbg(&spi->dev, "FPGA Status=%08x\n", *(u32 *)&rxbuf[4]);
  142. status = *(u32 *)&rxbuf[4];
  143. /* Check result */
  144. if (status & FPGA_STATUS_DONE)
  145. dev_info(&spi->dev, "FPGA succesfully configured!\n");
  146. else
  147. dev_info(&spi->dev, "FPGA not configured (DONE not set)\n");
  148. /*
  149. * Don't forget to release the firmware again
  150. */
  151. release_firmware(fw);
  152. kfree(buffer);
  153. complete(&data->fw_loaded);
  154. }
  155. static int lattice_ecp3_probe(struct spi_device *spi)
  156. {
  157. struct fpga_data *data;
  158. int err;
  159. data = devm_kzalloc(&spi->dev, sizeof(*data), GFP_KERNEL);
  160. if (!data) {
  161. dev_err(&spi->dev, "Memory allocation for fpga_data failed\n");
  162. return -ENOMEM;
  163. }
  164. spi_set_drvdata(spi, data);
  165. init_completion(&data->fw_loaded);
  166. err = request_firmware_nowait(THIS_MODULE, FW_ACTION_NOHOTPLUG,
  167. FIRMWARE_NAME, &spi->dev,
  168. GFP_KERNEL, spi, firmware_load);
  169. if (err) {
  170. dev_err(&spi->dev, "Firmware loading failed with %d!\n", err);
  171. return err;
  172. }
  173. dev_info(&spi->dev, "FPGA bitstream configuration driver registered\n");
  174. return 0;
  175. }
  176. static int lattice_ecp3_remove(struct spi_device *spi)
  177. {
  178. struct fpga_data *data = spi_get_drvdata(spi);
  179. wait_for_completion(&data->fw_loaded);
  180. return 0;
  181. }
  182. static const struct spi_device_id lattice_ecp3_id[] = {
  183. { "ecp3-17", 0 },
  184. { "ecp3-35", 0 },
  185. { }
  186. };
  187. MODULE_DEVICE_TABLE(spi, lattice_ecp3_id);
  188. static struct spi_driver lattice_ecp3_driver = {
  189. .driver = {
  190. .name = "lattice-ecp3",
  191. .owner = THIS_MODULE,
  192. },
  193. .probe = lattice_ecp3_probe,
  194. .remove = lattice_ecp3_remove,
  195. .id_table = lattice_ecp3_id,
  196. };
  197. module_spi_driver(lattice_ecp3_driver);
  198. MODULE_AUTHOR("Stefan Roese <sr@denx.de>");
  199. MODULE_DESCRIPTION("Lattice ECP3 FPGA configuration via SPI");
  200. MODULE_LICENSE("GPL");