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