fpga-mgr.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * FPGA Framework
  4. *
  5. * Copyright (C) 2013-2016 Altera Corporation
  6. * Copyright (C) 2017 Intel Corporation
  7. */
  8. #ifndef _LINUX_FPGA_MGR_H
  9. #define _LINUX_FPGA_MGR_H
  10. #include <linux/mutex.h>
  11. #include <linux/platform_device.h>
  12. struct fpga_manager;
  13. struct sg_table;
  14. /**
  15. * enum fpga_mgr_states - fpga framework states
  16. * @FPGA_MGR_STATE_UNKNOWN: can't determine state
  17. * @FPGA_MGR_STATE_POWER_OFF: FPGA power is off
  18. * @FPGA_MGR_STATE_POWER_UP: FPGA reports power is up
  19. * @FPGA_MGR_STATE_RESET: FPGA in reset state
  20. * @FPGA_MGR_STATE_FIRMWARE_REQ: firmware request in progress
  21. * @FPGA_MGR_STATE_FIRMWARE_REQ_ERR: firmware request failed
  22. * @FPGA_MGR_STATE_WRITE_INIT: preparing FPGA for programming
  23. * @FPGA_MGR_STATE_WRITE_INIT_ERR: Error during WRITE_INIT stage
  24. * @FPGA_MGR_STATE_WRITE: writing image to FPGA
  25. * @FPGA_MGR_STATE_WRITE_ERR: Error while writing FPGA
  26. * @FPGA_MGR_STATE_WRITE_COMPLETE: Doing post programming steps
  27. * @FPGA_MGR_STATE_WRITE_COMPLETE_ERR: Error during WRITE_COMPLETE
  28. * @FPGA_MGR_STATE_OPERATING: FPGA is programmed and operating
  29. */
  30. enum fpga_mgr_states {
  31. /* default FPGA states */
  32. FPGA_MGR_STATE_UNKNOWN,
  33. FPGA_MGR_STATE_POWER_OFF,
  34. FPGA_MGR_STATE_POWER_UP,
  35. FPGA_MGR_STATE_RESET,
  36. /* getting an image for loading */
  37. FPGA_MGR_STATE_FIRMWARE_REQ,
  38. FPGA_MGR_STATE_FIRMWARE_REQ_ERR,
  39. /* write sequence: init, write, complete */
  40. FPGA_MGR_STATE_WRITE_INIT,
  41. FPGA_MGR_STATE_WRITE_INIT_ERR,
  42. FPGA_MGR_STATE_WRITE,
  43. FPGA_MGR_STATE_WRITE_ERR,
  44. FPGA_MGR_STATE_WRITE_COMPLETE,
  45. FPGA_MGR_STATE_WRITE_COMPLETE_ERR,
  46. /* fpga is programmed and operating */
  47. FPGA_MGR_STATE_OPERATING,
  48. };
  49. /*
  50. * FPGA Manager flags
  51. * FPGA_MGR_PARTIAL_RECONFIG: do partial reconfiguration if supported
  52. * FPGA_MGR_EXTERNAL_CONFIG: FPGA has been configured prior to Linux booting
  53. * FPGA_MGR_BITSTREAM_LSB_FIRST: SPI bitstream bit order is LSB first
  54. * FPGA_MGR_COMPRESSED_BITSTREAM: FPGA bitstream is compressed
  55. */
  56. #define FPGA_MGR_PARTIAL_RECONFIG BIT(0)
  57. #define FPGA_MGR_EXTERNAL_CONFIG BIT(1)
  58. #define FPGA_MGR_ENCRYPTED_BITSTREAM BIT(2)
  59. #define FPGA_MGR_BITSTREAM_LSB_FIRST BIT(3)
  60. #define FPGA_MGR_COMPRESSED_BITSTREAM BIT(4)
  61. /**
  62. * struct fpga_image_info - information specific to a FPGA image
  63. * @flags: boolean flags as defined above
  64. * @enable_timeout_us: maximum time to enable traffic through bridge (uSec)
  65. * @disable_timeout_us: maximum time to disable traffic through bridge (uSec)
  66. * @config_complete_timeout_us: maximum time for FPGA to switch to operating
  67. * status in the write_complete op.
  68. * @firmware_name: name of FPGA image firmware file
  69. * @sgt: scatter/gather table containing FPGA image
  70. * @buf: contiguous buffer containing FPGA image
  71. * @count: size of buf
  72. * @region_id: id of target region
  73. * @dev: device that owns this
  74. * @overlay: Device Tree overlay
  75. */
  76. struct fpga_image_info {
  77. u32 flags;
  78. u32 enable_timeout_us;
  79. u32 disable_timeout_us;
  80. u32 config_complete_timeout_us;
  81. char *firmware_name;
  82. struct sg_table *sgt;
  83. const char *buf;
  84. size_t count;
  85. int region_id;
  86. struct device *dev;
  87. #ifdef CONFIG_OF
  88. struct device_node *overlay;
  89. #endif
  90. };
  91. /**
  92. * struct fpga_manager_ops - ops for low level fpga manager drivers
  93. * @initial_header_size: Maximum number of bytes that should be passed into write_init
  94. * @state: returns an enum value of the FPGA's state
  95. * @status: returns status of the FPGA, including reconfiguration error code
  96. * @write_init: prepare the FPGA to receive confuration data
  97. * @write: write count bytes of configuration data to the FPGA
  98. * @write_sg: write the scatter list of configuration data to the FPGA
  99. * @write_complete: set FPGA to operating state after writing is done
  100. * @fpga_remove: optional: Set FPGA into a specific state during driver remove
  101. * @groups: optional attribute groups.
  102. *
  103. * fpga_manager_ops are the low level functions implemented by a specific
  104. * fpga manager driver. The optional ones are tested for NULL before being
  105. * called, so leaving them out is fine.
  106. */
  107. struct fpga_manager_ops {
  108. size_t initial_header_size;
  109. enum fpga_mgr_states (*state)(struct fpga_manager *mgr);
  110. u64 (*status)(struct fpga_manager *mgr);
  111. int (*write_init)(struct fpga_manager *mgr,
  112. struct fpga_image_info *info,
  113. const char *buf, size_t count);
  114. int (*write)(struct fpga_manager *mgr, const char *buf, size_t count);
  115. int (*write_sg)(struct fpga_manager *mgr, struct sg_table *sgt);
  116. int (*write_complete)(struct fpga_manager *mgr,
  117. struct fpga_image_info *info);
  118. void (*fpga_remove)(struct fpga_manager *mgr);
  119. const struct attribute_group **groups;
  120. };
  121. /* FPGA manager status: Partial/Full Reconfiguration errors */
  122. #define FPGA_MGR_STATUS_OPERATION_ERR BIT(0)
  123. #define FPGA_MGR_STATUS_CRC_ERR BIT(1)
  124. #define FPGA_MGR_STATUS_INCOMPATIBLE_IMAGE_ERR BIT(2)
  125. #define FPGA_MGR_STATUS_IP_PROTOCOL_ERR BIT(3)
  126. #define FPGA_MGR_STATUS_FIFO_OVERFLOW_ERR BIT(4)
  127. /**
  128. * struct fpga_manager - fpga manager structure
  129. * @name: name of low level fpga manager
  130. * @dev: fpga manager device
  131. * @ref_mutex: only allows one reference to fpga manager
  132. * @state: state of fpga manager
  133. * @mops: pointer to struct of fpga manager ops
  134. * @priv: low level driver private date
  135. */
  136. struct fpga_manager {
  137. const char *name;
  138. struct device dev;
  139. struct mutex ref_mutex;
  140. enum fpga_mgr_states state;
  141. const struct fpga_manager_ops *mops;
  142. void *priv;
  143. };
  144. #define to_fpga_manager(d) container_of(d, struct fpga_manager, dev)
  145. struct fpga_image_info *fpga_image_info_alloc(struct device *dev);
  146. void fpga_image_info_free(struct fpga_image_info *info);
  147. int fpga_mgr_load(struct fpga_manager *mgr, struct fpga_image_info *info);
  148. int fpga_mgr_lock(struct fpga_manager *mgr);
  149. void fpga_mgr_unlock(struct fpga_manager *mgr);
  150. struct fpga_manager *of_fpga_mgr_get(struct device_node *node);
  151. struct fpga_manager *fpga_mgr_get(struct device *dev);
  152. void fpga_mgr_put(struct fpga_manager *mgr);
  153. struct fpga_manager *fpga_mgr_create(struct device *dev, const char *name,
  154. const struct fpga_manager_ops *mops,
  155. void *priv);
  156. void fpga_mgr_free(struct fpga_manager *mgr);
  157. int fpga_mgr_register(struct fpga_manager *mgr);
  158. void fpga_mgr_unregister(struct fpga_manager *mgr);
  159. #endif /*_LINUX_FPGA_MGR_H */