#ifndef AFE_DEV_SUP_H #define AFE_DEV_SUP_H /* Common support used for BPM Analog-Front-End devices * (except for PAD AFEs--that code still resides in padBpmApp) * * Contains devBusMapped access methods for reading/writing * AFE registers. Methods use callbacks to call the * correct driver routines for the AFE. * * To use this support, driver must: * - #include afeDevSup.h * - Define AfeDevSupCb callback structure * and attach to afeData->cb */ #include #include #ifdef __cplusplus extern "C" { #endif /*---copied from drvPadBpm.h---*/ #define AFE_REG_CSR 0 #define AFE_REG_CALA 1 #define AFE_REG_ATT1 2 #define AFE_REG_ATT2 3 #define AFE_REG_VER 5 #define AFE_REG_T2AMP 0x10 /* trig -> amp on */ /* registers are listed in order of time-sequence not in numerical order */ #define AFE_REG_AMP2RF1 0x11 /* amp on -> RF pulse 1 */ #define AFE_REG_RFWIDTH 0x13 /* RF pulse width */ #define AFE_REG_OFFTIME 0x14 /* pulse OFF -> change of control switches */ #define AFE_REG_RF12RF2 0x12 /* delay to pulse 2 */ #define AFE_REG_TRIG2BEAM 0x15 #define AFE_REG_RF2RED 0x16 #define AFE_REG_RF2GRN 0x17 #define AFE_REG_CSR_CALM_GET(x) ((x)&3) #define AFE_REG_CSR_CALM_RED 0 #define AFE_REG_CSR_CALM_GRN 1 #define AFE_REG_CSR_CLKS (1<<0) /* Overloaded -- ouch :-( */ /*---end copied---*/ #define AFE_REG_CSR_ONLN (1<<4) /* Always 1 when AFE on */ #define AFE_REG_CSR_CNFG (1<<5) /* Set to 1 by FW when powered on; SW sets to 0 to indicate AFE is configured */ #define AFE_REG_CSR_CALT (1<<6) /* Set to 1 by FW when cal trigger was seen; SW sets to 0 to acknowledge */ /* During AFE power-up, the FPGA bits are high for ~700 ms, returning * a reply message of 0xffff. Also observed unpredictable bits set * during power-down. To avoid responding to a transient bad read * when checking AFE online and configured do the following: * When checking online, also check that (unused) MSB is not set. * When checking configured, verify that message header is * what is expected. (The latter is done in the routines and not * in AFE_NOT_CNFG macro because the message header is different * for read and write.) */ #define AFE_IS_ONLN(x) (((x) & AFE_REG_CSR_ONLN) && !((x) & (1<<7))) /* True if AFE online */ #define AFE_NOT_CNFG(x) (((x) & AFE_REG_CSR_CNFG)) /* True if AFE not configured */ /* Calibration modes */ #define AFE_CALM_RED 0 #define AFE_CALM_GRN 1 #define AFE_CALM_NONE 3 #define AFE_CALM_TOGGLE 4 #define AFE_MSG_READ 0xc0 /* Read message header */ #define AFE_MSG_WRITE 0x80 /* Write (and write/read) message header */ #define AFE_MSG_HEADER(x) ((x)<<8) /* Shift message header to first byte */ #define AFE_MSG_DATA(x) ((x)&0xFF) /* Extract data byte from message */ #define AFE_BASE 1 /* devBusMapped error during record processing if base address = 0 */ /* * I/O method to be used by AFE device support for communication with AFE. * The I/O (usually QSPI) must implement the following semantics: * - 'data_in' may be AFE_DEV_SUP_IO_WRONLY (NULL) in which case * 'data_out' is written out to QSPI (but the method doesn't have * to wait for completion). * - 'data_out' may be AFE_DEV_SUP_IO_RDONLY (-1) in which case the method * waits for an outstanding transaction to complete and then returns * the value last read from QSPI. * These semantics allow some work to be interleaved with a QSPI transaction: * * afe_write_read(dev, data_out, AFE_DEV_SUP_IO_WRONLY) * // qspi has started is working * do_some_work(); * are_write_read(dev, AFE_DEV_SUP_IO_RDONLY, &data_in) * // wait (if necessary) and return result of broken-up transaction. */ #define AFE_DEV_SUP_IO_RDONLY (-1) #define AFE_DEV_SUP_IO_WRONLY ((uint16_t*) 0) typedef int (*AfeWriteReadFunc)(const void *device, int data_out, uint16_t *data_in); typedef struct AfeDevSupIoRec_ { AfeWriteReadFunc afe_write_read; } AfeDevSupIoRec, *AfeDevSupIo; typedef struct AfeDataRec_ *AfeData; typedef enum { AfeCaltErr = -1, AfeCaltNone = 0, AfeCaltRed = 1, AfeCaltGrn = 2 } AfeGotCalt; /* NOTE: Any of the 'ops' may be NULL */ typedef struct AfeOpsRec_ { void (*lock)(AfeData afe); void (*unlock)(AfeData afe); /* op which toggles CAL mode via QSPI if the AFE is in 'soft-toggle' mode. * NOTE: caller MUST lock/unlock afe during execution of this callback. */ void (*cal_flip)(AfeData afe); /* op to find out if a CAL trigger has occurred. * * NOTE: this operation may not actually do a QSPI transaction but just * determine CALT from the data received during the last transaction. * * This operation exists for efficiency reasons and -- when used -- * must be executed after cal_toggle_cb() without any interfering * QSPI transaction. * The idea is that cal_toggle_cb() may start a QSPI transaction; * while it is in progress the caller may perform some work and * eventually obtain the result of the outstanding transaction. * The afe lock must be held across *both* operations: * * lock( afe ); * cal_toggle_cb( afe ); * // do some work here * gotCalt = got_calt_cb( afe ); * unlock( afe ); * * RETURNS: 'AfeCaltErr' status if packet should be dropped. * If a CAL trigger was seen then the return value reflects the * 'color' of the calibration cycle. */ AfeGotCalt (*got_calt)(AfeData afe); int (*recover)(AfeData afe); } AfeOpsRec, *AfeOps; typedef unsigned AfeFeatures; /* Must AND features with ATTx_MSK, then test for any ATTx feature, e.g: * switch ( (features & AFE_FEAT_ATT1_MSK) ) { * case AFE_FEAT_ATT1_0_15: * case AFE_FEAT_ATT1_0_31: * } * NOTE: for now, the attenuator range features are 1 bit but that may * change in the future. The described approach ensures that code * remains working. */ #define AFE_FEAT_ATT1_MSK (1<<0) #define AFE_FEAT_ATT1_0_15 (0<<0) #define AFE_FEAT_ATT1_0_31 (1<<0) #define AFE_FEAT_ATT2_MSK (1<<1) #define AFE_FEAT_ATT2_0_15 (0<<1) #define AFE_FEAT_ATT2_0_31 (1<<1) #define AFE_FEAT_CALM_QSPI (1<<8) /* CALM must/can be switched via QSPI */ #define AFE_FEAT_CALTIM_INT (1<<9) /* AFE has internal timing for calibration * (regs TRIG2BEAM, RF2RED, RF2GRN) */ #define AFE_FEAT_CSR_CLKS (1<<10) /* Clock source in CSR :-( */ #define AFE_FEAT_PWR_INDEP (1<<11) /* AFE is housed separately from its software controller * It can be power-cycled separately and thus we must * be able to detect an outage and recover */ /* AfeCacheMsk must hold (at least) as many bits as there are cached registers */ typedef uint32_t AfeCacheMsk; typedef struct AfeDataRec_ { const void *io_dev; /* Device handle for IO callback */ AfeDevSupIoRec cb; unsigned calMode; /* Cal mode set by CALM record */ uint8_t regCache[32]; /* Cached AFE register contents */ AfeCacheMsk regValid,regSave; void *ops_priv; /* private data for implementor of ops */ AfeOps ops; unsigned onln; /* strobe flag for online checker */ AfeFeatures features; } AfeDataRec; AfeData afeDataCreate(AfeWriteReadFunc f, const void *io_device); int afeGetFeatures(unsigned fw_vers, AfeFeatures *f_p); typedef AfeData (*AfeAttachFunc)(unsigned instance, AfeWriteReadFunc f, const void *io_device); int afeAttachRegister(const char *name, AfeAttachFunc f); #define AFE_ATTACH_REGISTER(x) afeAttachRegister(#x, x) AfeAttachFunc afeAttachFind(const char *name); static inline int afeDevSupWriteRead(AfeData afe, uint16_t data_out, uint16_t *data_in) { return afe->cb.afe_write_read( afe->io_dev, data_out, data_in ); } static inline int afeDevSupWrite(AfeData afe, uint16_t data_out) { return afe->cb.afe_write_read( afe->io_dev, data_out, 0 ); } static inline int afeDevSupRead(AfeData afe, uint16_t *data_in) { return afe->cb.afe_write_read( afe->io_dev, AFE_DEV_SUP_IO_RDONLY, data_in ); } static inline void afeLock(AfeData afe) { afe->ops->lock( afe ); } static inline void afeUnlock(AfeData afe) { afe->ops->unlock( afe ); } static inline int afeRecover(AfeData afe) { return afe->ops->recover ? afe->ops->recover( afe ) : 0; } #endif