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