Line data Source code
1 0 : /*
2 : * Copyright (c) 2021 Antmicro <www.antmicro.com>
3 : *
4 : * SPDX-License-Identifier: Apache-2.0
5 : */
6 :
7 : /**
8 : * @file
9 : * @ingroup fpga_interface
10 : * @brief Main header file for FPGA driver API.
11 : */
12 :
13 : #ifndef ZEPHYR_INCLUDE_DRIVERS_FPGA_H_
14 : #define ZEPHYR_INCLUDE_DRIVERS_FPGA_H_
15 :
16 : #include <errno.h>
17 :
18 : #include <zephyr/types.h>
19 : #include <zephyr/sys/util.h>
20 : #include <zephyr/device.h>
21 :
22 : #ifdef __cplusplus
23 : extern "C" {
24 : #endif
25 :
26 : /**
27 : * @brief Interfaces for Field-Programmable Gate Arrays (FPGA).
28 : * @defgroup fpga_interface FPGA
29 : * @since 2.7
30 : * @version 0.1.0
31 : * @ingroup io_interfaces
32 : * @{
33 : */
34 :
35 0 : enum FPGA_status {
36 : /* Inactive is when the FPGA cannot accept the bitstream
37 : * and will not be programmed correctly
38 : */
39 : FPGA_STATUS_INACTIVE,
40 : /* Active is when the FPGA can accept the bitstream and
41 : * can be programmed correctly
42 : */
43 : FPGA_STATUS_ACTIVE
44 : };
45 :
46 : typedef enum FPGA_status (*fpga_api_get_status)(const struct device *dev);
47 0 : typedef int (*fpga_api_load)(const struct device *dev, uint32_t *image_ptr,
48 : uint32_t img_size);
49 0 : typedef int (*fpga_api_reset)(const struct device *dev);
50 0 : typedef int (*fpga_api_on)(const struct device *dev);
51 0 : typedef int (*fpga_api_off)(const struct device *dev);
52 0 : typedef const char *(*fpga_api_get_info)(const struct device *dev);
53 :
54 0 : __subsystem struct fpga_driver_api {
55 0 : fpga_api_get_status get_status;
56 0 : fpga_api_reset reset;
57 0 : fpga_api_load load;
58 0 : fpga_api_on on;
59 0 : fpga_api_off off;
60 0 : fpga_api_get_info get_info;
61 : };
62 :
63 : /**
64 : * @brief Read the status of FPGA.
65 : *
66 : * @param dev FPGA device structure.
67 : *
68 : * @retval 0 if the FPGA is in INACTIVE state.
69 : * @retval 1 if the FPGA is in ACTIVE state.
70 : */
71 1 : static inline enum FPGA_status fpga_get_status(const struct device *dev)
72 : {
73 : const struct fpga_driver_api *api =
74 : (const struct fpga_driver_api *)dev->api;
75 :
76 : if (api->get_status == NULL) {
77 : /* assume it can never be reprogrammed if it
78 : * doesn't support the get_status callback
79 : */
80 : return FPGA_STATUS_INACTIVE;
81 : }
82 :
83 : return api->get_status(dev);
84 : }
85 :
86 : /**
87 : * @brief Reset the FPGA.
88 : *
89 : * @param dev FPGA device structure.
90 : *
91 : * @retval 0 if successful.
92 : * @retval Failed Otherwise.
93 : */
94 1 : static inline int fpga_reset(const struct device *dev)
95 : {
96 : const struct fpga_driver_api *api =
97 : (const struct fpga_driver_api *)dev->api;
98 :
99 : if (api->reset == NULL) {
100 : return -ENOTSUP;
101 : }
102 :
103 : return api->reset(dev);
104 : }
105 :
106 : /**
107 : * @brief Load the bitstream and program the FPGA
108 : *
109 : * @param dev FPGA device structure.
110 : * @param image_ptr Pointer to bitstream.
111 : * @param img_size Bitstream size in bytes.
112 : *
113 : * @retval 0 if successful.
114 : * @retval Failed Otherwise.
115 : */
116 1 : static inline int fpga_load(const struct device *dev, uint32_t *image_ptr,
117 : uint32_t img_size)
118 : {
119 : const struct fpga_driver_api *api =
120 : (const struct fpga_driver_api *)dev->api;
121 :
122 : if (api->load == NULL) {
123 : return -ENOTSUP;
124 : }
125 :
126 : return api->load(dev, image_ptr, img_size);
127 : }
128 :
129 : /**
130 : * @brief Turns on the FPGA.
131 : *
132 : * @param dev FPGA device structure.
133 : *
134 : * @retval 0 if successful.
135 : * @retval negative errno code on failure.
136 : */
137 1 : static inline int fpga_on(const struct device *dev)
138 : {
139 : const struct fpga_driver_api *api =
140 : (const struct fpga_driver_api *)dev->api;
141 :
142 : if (api->on == NULL) {
143 : return -ENOTSUP;
144 : }
145 :
146 : return api->on(dev);
147 : }
148 :
149 0 : #define FPGA_GET_INFO_DEFAULT "n/a"
150 :
151 : /**
152 : * @brief Returns information about the FPGA.
153 : *
154 : * @param dev FPGA device structure.
155 : *
156 : * @return String containing information.
157 : */
158 1 : static inline const char *fpga_get_info(const struct device *dev)
159 : {
160 : const struct fpga_driver_api *api =
161 : (const struct fpga_driver_api *)dev->api;
162 :
163 : if (api->get_info == NULL) {
164 : return FPGA_GET_INFO_DEFAULT;
165 : }
166 :
167 : return api->get_info(dev);
168 : }
169 :
170 : /**
171 : * @brief Turns off the FPGA.
172 : *
173 : * @param dev FPGA device structure.
174 : *
175 : * @retval 0 if successful.
176 : * @retval negative errno code on failure.
177 : */
178 1 : static inline int fpga_off(const struct device *dev)
179 : {
180 : const struct fpga_driver_api *api =
181 : (const struct fpga_driver_api *)dev->api;
182 :
183 : if (api->off == NULL) {
184 : return -ENOTSUP;
185 : }
186 :
187 : return api->off(dev);
188 : }
189 :
190 : /** @} */
191 :
192 : #ifdef __cplusplus
193 : }
194 : #endif
195 :
196 : #endif /* ZEPHYR_INCLUDE_DRIVERS_FPGA_H_ */
|