lattice-ecp3-config.c 5.8 KB

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