fpga-mgr.txt 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. FPGA Manager Core
  2. Alan Tull 2015
  3. Overview
  4. ========
  5. The FPGA manager core exports a set of functions for programming an FPGA with
  6. an image. The API is manufacturer agnostic. All manufacturer specifics are
  7. hidden away in a low level driver which registers a set of ops with the core.
  8. The FPGA image data itself is very manufacturer specific, but for our purposes
  9. it's just binary data. The FPGA manager core won't parse it.
  10. API Functions:
  11. ==============
  12. To program the FPGA from a file or from a buffer:
  13. -------------------------------------------------
  14. int fpga_mgr_buf_load(struct fpga_manager *mgr, u32 flags,
  15. const char *buf, size_t count);
  16. Load the FPGA from an image which exists as a buffer in memory.
  17. int fpga_mgr_firmware_load(struct fpga_manager *mgr, u32 flags,
  18. const char *image_name);
  19. Load the FPGA from an image which exists as a file. The image file must be on
  20. the firmware search path (see the firmware class documentation).
  21. For both these functions, flags == 0 for normal full reconfiguration or
  22. FPGA_MGR_PARTIAL_RECONFIG for partial reconfiguration. If successful, the FPGA
  23. ends up in operating mode. Return 0 on success or a negative error code.
  24. To get/put a reference to a FPGA manager:
  25. -----------------------------------------
  26. struct fpga_manager *of_fpga_mgr_get(struct device_node *node);
  27. void fpga_mgr_put(struct fpga_manager *mgr);
  28. Given a DT node, get an exclusive reference to a FPGA manager or release
  29. the reference.
  30. To register or unregister the low level FPGA-specific driver:
  31. -------------------------------------------------------------
  32. int fpga_mgr_register(struct device *dev, const char *name,
  33. const struct fpga_manager_ops *mops,
  34. void *priv);
  35. void fpga_mgr_unregister(struct device *dev);
  36. Use of these two functions is described below in "How To Support a new FPGA
  37. device."
  38. How to write an image buffer to a supported FPGA
  39. ================================================
  40. /* Include to get the API */
  41. #include <linux/fpga/fpga-mgr.h>
  42. /* device node that specifies the FPGA manager to use */
  43. struct device_node *mgr_node = ...
  44. /* FPGA image is in this buffer. count is size of the buffer. */
  45. char *buf = ...
  46. int count = ...
  47. /* flags indicates whether to do full or partial reconfiguration */
  48. int flags = 0;
  49. int ret;
  50. /* Get exclusive control of FPGA manager */
  51. struct fpga_manager *mgr = of_fpga_mgr_get(mgr_node);
  52. /* Load the buffer to the FPGA */
  53. ret = fpga_mgr_buf_load(mgr, flags, buf, count);
  54. /* Release the FPGA manager */
  55. fpga_mgr_put(mgr);
  56. How to write an image file to a supported FPGA
  57. ==============================================
  58. /* Include to get the API */
  59. #include <linux/fpga/fpga-mgr.h>
  60. /* device node that specifies the FPGA manager to use */
  61. struct device_node *mgr_node = ...
  62. /* FPGA image is in this file which is in the firmware search path */
  63. const char *path = "fpga-image-9.rbf"
  64. /* flags indicates whether to do full or partial reconfiguration */
  65. int flags = 0;
  66. int ret;
  67. /* Get exclusive control of FPGA manager */
  68. struct fpga_manager *mgr = of_fpga_mgr_get(mgr_node);
  69. /* Get the firmware image (path) and load it to the FPGA */
  70. ret = fpga_mgr_firmware_load(mgr, flags, path);
  71. /* Release the FPGA manager */
  72. fpga_mgr_put(mgr);
  73. How to support a new FPGA device
  74. ================================
  75. To add another FPGA manager, write a driver that implements a set of ops. The
  76. probe function calls fpga_mgr_register(), such as:
  77. static const struct fpga_manager_ops socfpga_fpga_ops = {
  78. .write_init = socfpga_fpga_ops_configure_init,
  79. .write = socfpga_fpga_ops_configure_write,
  80. .write_complete = socfpga_fpga_ops_configure_complete,
  81. .state = socfpga_fpga_ops_state,
  82. };
  83. static int socfpga_fpga_probe(struct platform_device *pdev)
  84. {
  85. struct device *dev = &pdev->dev;
  86. struct socfpga_fpga_priv *priv;
  87. int ret;
  88. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  89. if (!priv)
  90. return -ENOMEM;
  91. /* ... do ioremaps, get interrupts, etc. and save
  92. them in priv... */
  93. return fpga_mgr_register(dev, "Altera SOCFPGA FPGA Manager",
  94. &socfpga_fpga_ops, priv);
  95. }
  96. static int socfpga_fpga_remove(struct platform_device *pdev)
  97. {
  98. fpga_mgr_unregister(&pdev->dev);
  99. return 0;
  100. }
  101. The ops will implement whatever device specific register writes are needed to
  102. do the programming sequence for this particular FPGA. These ops return 0 for
  103. success or negative error codes otherwise.
  104. The programming sequence is:
  105. 1. .write_init
  106. 2. .write (may be called once or multiple times)
  107. 3. .write_complete
  108. The .write_init function will prepare the FPGA to receive the image data.
  109. The .write function writes a buffer to the FPGA. The buffer may be contain the
  110. whole FPGA image or may be a smaller chunk of an FPGA image. In the latter
  111. case, this function is called multiple times for successive chunks.
  112. The .write_complete function is called after all the image has been written
  113. to put the FPGA into operating mode.
  114. The ops include a .state function which will read the hardware FPGA manager and
  115. return a code of type enum fpga_mgr_states. It doesn't result in a change in
  116. hardware state.