1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
|
/*
* Public libusb3380 header file
* Copyright (c) 2017 Sergey Kostanbaev <sergey.kostanbaev@fairwaves.co>
* For more information, please visit: http://xtrx.io
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef LIBUSB3380_H
#define LIBUSB3380_H
#include <libusb-1.0/libusb.h>
#include <stdbool.h>
#ifdef __cplusplus
extern "C" {
#endif
struct libusb3380_context;
typedef struct libusb3380_context libusb3380_context_t;
enum libusb3380_context_vidpid {
LIBUSB3380_VID = 0x0525,
LIBUSB3380_PID = 0x3380,
};
int usb3380_context_init(libusb3380_context_t** octx);
int usb3380_context_init_ex(libusb3380_context_t** octx, libusb_device *dev, libusb_context *ctx);
void usb3380_context_free(libusb3380_context_t* ctx);
typedef enum libusb3380_loglevel {
USB3380_LOG_ERROR,
USB3380_LOG_WARNING,
USB3380_LOG_INFO,
USB3380_LOG_DEBUG,
USB3380_LOG_DUMP,
} libusb3380_loglevel_t;
typedef void (*usb3380_logfunc_t)(libusb3380_loglevel_t lvl,
void* obj,
const char* func,
const char* file,
int line,
const char* message, ...);
void usb3380_set_logfunc(usb3380_logfunc_t func, void *param);
void usb3380_set_loglevel(libusb3380_loglevel_t level);
typedef enum libusb3380_gpep {
LIBUSB3380_GPEP0,
LIBUSB3380_GPEP1,
LIBUSB3380_GPEP2,
LIBUSB3380_GPEP3,
LIBUSB3380_GPEP_COUNT,
} libusb3380_gpep_t;
typedef enum libusb3380_bar_size {
BAR_NONE = 0,
BAR_64K = 16,
BAR_128K = 17,
BAR_256K = 18,
BAR_512K = 19,
BAR_1M = 20,
BAR_2M = 21,
BAR_4M = 22,
BAR_8M = 23,
BAR_16M = 24,
BAR_32M = 25,
BAR_64M = 26,
BAR_128M = 27,
BAR_256M = 28,
BAR_512M = 29,
BAR_1G = 30,
BAR_2G = 31,
} libusb3380_bar_size_t;
typedef struct libusb3380_usbc_bar_ctrl {
uint32_t addr;
libusb3380_bar_size_t length;
/** Each quandrant of this region is mmaped to the specific GPEP index */
libusb3380_gpep_t qadrants_ep_map[4];
bool gpep_in_type[4];
unsigned flags;
} libusb3380_usbc_bar_ctrl_t;
enum libusb3380_pcie_rc_flags {
LIBUSB3380_EN_256B_PAYLOAD = 1,
};
typedef struct libusb3380_pcie_rc_cfg {
libusb3380_usbc_bar_ctrl_t bar2;
libusb3380_usbc_bar_ctrl_t bar3;
unsigned gpep_fifo_in_size[LIBUSB3380_GPEP_COUNT];
unsigned gpep_fifo_out_size[LIBUSB3380_GPEP_COUNT];
unsigned flags;
} libusb3380_pcie_rc_cfg_t;
void usb3380_init_rc_def(libusb3380_pcie_rc_cfg_t *cfg);
int usb3380_gpio_dir(libusb3380_context_t* ctx, uint8_t diroutbits);
int usb3380_gpio_out(libusb3380_context_t* ctx, uint8_t out);
int usb3380_gpio_in(libusb3380_context_t* ctx, uint8_t *in);
/**
* @brief usb3380_init_bridges Initialize internal bridges
* @param ctx
* @return
*/
int usb3380_init_root_complex(libusb3380_context_t* ctx,
const libusb3380_pcie_rc_cfg_t *cfg);
int usb3380_csr_pcie_cfg_read(libusb3380_context_t* ctx, uint32_t addr,
uint32_t* data);
int usb3380_csr_pcie_cfg_write(libusb3380_context_t* ctx, uint32_t addr,
uint32_t data);
int usb3380_csr_mm_cfg_read(libusb3380_context_t* ctx, uint32_t addr,
uint32_t* data);
int usb3380_csr_mm_cfg_write(libusb3380_context_t* ctx, uint32_t addr,
uint32_t data);
int usb3380_csr_mm_mcu_read(libusb3380_context_t* ctx, uint32_t addr,
uint32_t* data);
int usb3380_csr_mm_mcu_write(libusb3380_context_t* ctx, uint32_t addr,
uint32_t data);
int usb3380_csr_mm_mcu_copy(libusb3380_context_t* ctx, uint32_t addr,
const uint32_t* pdata, size_t dwords);
int usb3380_mcu_sem_acq(libusb3380_context_t* ctx, uint32_t* res);
int usb3380_mcu_sem_rel(libusb3380_context_t* ctx);
int usb3380_mcu_reset(libusb3380_context_t* ctx, int reset);
int usb3380_idxreg_read(libusb3380_context_t* ctx, uint16_t addr,
uint32_t* data);
int usb3380_idxreg_write(libusb3380_context_t* ctx, uint16_t addr,
uint32_t data);
#define MAKE_CFG(bus, dev, func, reg, type) \
(((type) << 24) | (((reg) & 0xFC) << 24) | (((dev) & 0x1f) << 11) | \
(((func) & 0x7) << 8) | ((bus) & 0xFF))
#define MAKE_CFG_1(bus, dev, func, reg) MAKE_CFG(bus, dev, func, reg, 1)
#define MAKE_CFG_0(bus, dev, func, reg) MAKE_CFG(bus, dev, func, reg, 0)
int usb3380_pci_cfg_read(libusb3380_context_t* ctx, uint32_t addr,
uint32_t* data);
int usb3380_pci_cfg_write(libusb3380_context_t* ctx, uint32_t addr,
uint32_t data);
struct libusb3380_pcidev;
typedef struct libusb3380_pcidev libusb3380_pcidev_t;
enum usb3380_init_flags {
NO_MSI = 1,
};
int usb3380_init_first_dev(libusb3380_context_t* ctx, unsigned flags,
libusb3380_pcidev_t **out);
uint16_t usb3380_pci_dev_did(libusb3380_pcidev_t* ctx);
uint16_t usb3380_pci_dev_vid(libusb3380_pcidev_t* ctx);
uint32_t usb3380_pci_dev_bar_addr(libusb3380_pcidev_t* ctx, unsigned bar);
uint32_t usb3380_pci_dev_bar_length(libusb3380_pcidev_t* ctx, unsigned bar);
int usb3380_pci_dev_mem_read32(libusb3380_context_t* ctx, uint32_t addr,
uint32_t* data);
int usb3380_pci_dev_mem_write32(libusb3380_context_t* ctx, uint32_t addr,
uint32_t data);
int usb3380_pci_dev_mem_write32_n(libusb3380_context_t* ctx, uint32_t addr,
const uint32_t* data, unsigned count_dw);
int usb3380_pci_dev_mem_read32_n(libusb3380_context_t* ctx, uint32_t addr,
uint32_t* data, unsigned count_dw);
int usb3380_pci_wait_interrupt(libusb3380_context_t *ctx, long timeoutms);
int usb3380_gpep_read(libusb3380_context_t* ctx, libusb3380_gpep_t ep,
uint8_t *data, int size, int* written, unsigned to);
int usb3380_gpep_write(libusb3380_context_t* ctx, libusb3380_gpep_t ep,
const uint8_t* data, int size, int* written,
unsigned to);
/* Async interfce */
enum {
LIBUSB3380_MAX_TLP_SIZE = 256,
};
typedef struct libusb3380_pciout_buf {
uint32_t ctrl;
uint32_t addr;
uint8_t data[LIBUSB3380_MAX_TLP_SIZE];
} libusb3380_pciout_buf_t;
typedef enum libusb3380_status {
DQS_SUCCESS = 0,
DQS_TIMEOUT = 1,
DQS_ABORT = 2,
DQS_PARTIAL = 3,
DQS_CANCELLED = 4,
} libusb3380_status_t;
typedef struct libusb3380_as_base {
/** number of bytes to IN or OUT (or MSI interrupt number awaiting) */
unsigned size;
/** number of miliseconds to the wait endpoint result */
unsigned timeout;
/** status of operation */
libusb3380_status_t status;
/** number of bytes INed or OUTed */
unsigned written;
} libusb3380_as_base_t;
typedef enum libusb3380_pci_tlp_type {
RC_PCIE_WRITE = 0,
RC_PCIE_READ = 1,
} libusb3380_pci_tlp_type_t;
/* awaiting root complex transaction result */
typedef struct libusb3380_qpci_master {
libusb3380_as_base_t base;
libusb3380_pci_tlp_type_t type;
union {
libusb3380_pciout_buf_t out;
uint32_t data[LIBUSB3380_MAX_TLP_SIZE / 4 + 2];
};
} libusb3380_qpci_master_t;
typedef struct libusb3380_qcsr {
libusb3380_as_base_t base;
bool read;
uint32_t data[2];
} libusb3380_qcsr_t;
struct libusb3380_qgpep;
typedef void (*on_gpep_cb_t)(const struct libusb3380_qgpep* gpep,
unsigned gpepno,
unsigned idx);
/* awaiting gpep data (device DMA in and out) */
typedef struct libusb3380_qgpep {
libusb3380_as_base_t base;
/** Data block to be IN or OUT */
uint8_t* pdata;
on_gpep_cb_t cb_done;
void* param;
} libusb3380_qgpep_t;
/* awaiting PCIe MSI interrupt */
typedef struct libusb3380_qmsi_int {
libusb3380_as_base_t base;
} libusb3380_qmsi_int_t;
typedef struct libusb3380_configuration {
unsigned gp_in_cnts[LIBUSB3380_GPEP_COUNT];
unsigned gp_out_cnts[LIBUSB3380_GPEP_COUNT];
} libusb3380_configuration_t;
struct libusb3380_async_manager;
int usb3380_async_start(struct libusb3380_pcidev *dev,
const struct libusb3380_configuration *configuration,
struct libusb3380_async_manager** out);
int usb3380_async_stop(struct libusb3380_async_manager* mgr);
int usb3380_async_pci_write32(struct libusb3380_async_manager* mgr,
uint32_t addr,
const uint32_t *data,
unsigned dw_count);
int usb3380_async_pci_read32(struct libusb3380_async_manager* mgr,
uint32_t addr,
uint32_t *data,
unsigned dw_count);
int usb3380_async_await_msi(struct libusb3380_async_manager* mgr,
unsigned num);
int usb3380_async_set_gpep_timeout(struct libusb3380_async_manager* mgr,
bool ep_in, libusb3380_gpep_t gpep,
unsigned idx,
unsigned to_ms);
/**
* @brief usb3380_async_gpep_out_post
* @param mgr
* @param data
* @param size
* @return 0 success, error otherwise
*
* Functions post data to send and return immediatly if sending queue isn't
* full, otherwise it bolcks untill we get some space in the queue
*/
int usb3380_async_gpep_out_post(struct libusb3380_async_manager* mgr,
libusb3380_gpep_t ep_no, unsigned idx,
const uint8_t* data, unsigned size,
on_gpep_cb_t cb, void* param);
int usb3380_async_gpep_in_post(struct libusb3380_async_manager* mgr,
libusb3380_gpep_t ep_no, unsigned idx,
uint8_t* data, unsigned size,
on_gpep_cb_t cb, void* param);
int usb3380_async_gpep_cancel(struct libusb3380_async_manager* mgr,
bool ep_in,
libusb3380_gpep_t ep_no,
unsigned idx);
typedef void (*on_msi_cb_t)(void* param,
int msinum,
bool timedout);
int usb3380_msi_in_post(struct libusb3380_async_manager* mgr,
unsigned timeoutms,
on_msi_cb_t cb,
void* param);
int usb3380_msi_in_cancel(struct libusb3380_async_manager* mgr);
#ifdef __cplusplus
};
#endif
#endif
|