#include #include #include #include #include #include #include #include #include #include #include #include #include #include /* Need all features of vers. 4 */ #define AFE_VERS4 4 #define DRVPAD_MIN_AFE_VER AFE_VERS4 #define BAUDRATE 10000000 #undef DEBUG /* pull CS line 0 low, others high */ #define PAD_CS 0xe /* CS3 is re-used to notify us of a CAL trigger */ #define CALT_FLG_BIT MCF5282_GPIO_PORTx6 /* We set CS2 (reprogrammed as GPIO) to clear the CALTRIG notify */ #define CALT_RST_BIT MCF5282_GPIO_PORTx5 #define AFE_REG_CSR 0 #define AFE_REG_CALA 1 #define AFE_REG_ATT1 2 #define AFE_REG_ATT2 3 #define AFE_REG_LMT 4 #define AFE_REG_VER 5 #define AFE_REG_CALT 6 #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_CSR_CALM_GET(x) ((x)&3) uint8_t drvPadAfeRegCache[32]; static DevBusMappedDev theAfe = 0; static int calTgl = 0; volatile int drvPadAfeSimMode = 0; int drvPadAfeCS = AFE_CS_DFLT; /* Firmware version 4 has a bug. The cal trigger seems * to be only raised at the time the next QSPI transaction * occurs. * To work around this bug we immediately read the (bogus) * value back... */ static inline int do_calt(int cs) { uint8_t msg[4] = {0x86, 0x00, 0xc6, 0x00}; return coldfQspiWrite(msg,sizeof(msg),cs); } /* Issue a soft calibration cycle; * - send CAL trigger on QSPI * - delay for 'dely' loops (depends * on processor) * - send ADC trigger * * *** All of this runs with INTERRUPTS DISABLED *** */ void drvPadSoftCal(int dely, int cs, int trig_pin, int irq_pin) { unsigned flags; int old_mask; if ( dely<=0 ) dely = 1; old_mask = coldfEportIntDisable_inl(irq_pin); rtems_interrupt_disable(flags); /* clear pending CALT flag */ drvPadAfeGotCalt(1); drvPadAfeRegCache[AFE_REG_CALT] = 0; /* start cal pulse; it is OK to run this * with interrupts disabled (albeit a little long). * * If the task blocks waiting for the QSPI * then it is put to sleep (with the current IRQ mask). * Once it runs again then it runs through * w/o interruption. * * It would be more real-time friendly if * we would just hold the QSPI mutex and * only disable interrupts just before * starting the transfer [or even better: if we * could use a hardware timer to trigger the * PAD instead of busywaiting]. */ do_calt(cs); do { asm volatile(""); } while (--dely > 0 ); coldfEportEpdrSet(trig_pin,1); rtems_interrupt_enable(flags); /* theoretically, a missed interrupt cannot occur here */ drvPadCheckOverrun(irq_pin); if ( old_mask ) coldfEportIntEnable(irq_pin); } /* Toggle calibrator from GRN <-> RED * RETURNS: new state, -1 if not in soft toggle mode, * -2 if old state was not GRN nor RED or for other errors. */ int drvPadAfeCalToggle() { uint8_t buf[2]; int rval = -2, v; if ( !theAfe || drvPadAfeSimMode ) { /* If there is no AFE we fake the toggle mode */ v = ( AFE_REG_CSR_CALM_GET(drvPadAfeRegCache[AFE_REG_CSR]) ); switch ( v ) { default: return rval; case AFE_REG_CSR_CALM_RED: v = AFE_REG_CSR_CALM_GRN; break; case AFE_REG_CSR_CALM_GRN: v = AFE_REG_CSR_CALM_RED; break; } drvPadAfeRegCache[AFE_REG_CSR] = (drvPadAfeRegCache[AFE_REG_CSR] & ~AFE_REG_CSR_CALM_GET(-1)) | v; return v; } if ( ! calTgl ) { return -1; } epicsMutexLock( theAfe->mutex ); switch ( AFE_REG_CSR_CALM_GET(drvPadAfeRegCache[AFE_REG_CSR]) ) { default: goto bail; case AFE_REG_CSR_CALM_RED: v = AFE_REG_CSR_CALM_GRN; break; case AFE_REG_CSR_CALM_GRN: v = AFE_REG_CSR_CALM_RED; break; } buf[0] = 0x80 + AFE_REG_CSR; buf[1] = (drvPadAfeRegCache[AFE_REG_CSR] & ~AFE_REG_CSR_CALM_GET(-1)) | v; if ( 2 != coldfQspiWrite(buf, 2, drvPadAfeCS) ) { goto bail; } drvPadAfeRegCache[AFE_REG_CSR] = buf[1]; rval = v; bail: epicsMutexUnlock( theAfe->mutex ); return rval; } static int qspiReadPadReg(DevBusMappedPvt pvt, unsigned *pvalue, int idx, dbCommon *prec) { uint8_t buf[2]; uint32_t cs = ((uint32_t)pvt->addr) >> 8; uint8_t a = ((uint32_t)pvt->addr & 0x3f); buf[0] = 0xc0 + a; #ifdef DEBUG printf("qspiReadPadReg; writing (CS 0x%x) 0x%02x 0x%02x\n", cs, buf[0], buf[1]); #endif if ( 2 != coldfQspiWriteRead(buf, buf, 2, cs) ) return -1; #ifdef DEBUG printf("qspiReadPadReg; got 0x%02x\n", buf[1]); #endif *pvalue = buf[1]; if ( a < sizeof(drvPadAfeRegCache)/sizeof(drvPadAfeRegCache[0]) ) drvPadAfeRegCache[a] = buf[1]; return 0; } static int qspiWritePadReg(DevBusMappedPvt pvt, unsigned value, int idx, dbCommon *prec) { uint8_t buf[2]; uint32_t cs = ((uint32_t)pvt->addr) >> 8; uint8_t a = ((uint32_t)pvt->addr & 0x3f); buf[0] = 0x80 + a; buf[1] = value; #ifdef DEBUG printf("qspiWritePadReg; writing (value = 0x%x, cs 0x%x) 0x%02x 0x%02x\n", value, cs, buf[0], buf[1]); #endif if ( AFE_REG_CALT == a ) { /* handle soft-trigger firmware bug */ if ( 4 != do_calt(cs) ) return -1; } else { if ( 2 != coldfQspiWrite(buf, 2, cs) ) return -1; } #ifdef DEBUG buf[0] |= 0x40; coldfQspiWriteRead(buf, buf, 2, cs); printf("qspiWritePadReg; register contents are now 0x%02x\n",buf[1]); #endif if ( a < sizeof(drvPadAfeRegCache)/sizeof(drvPadAfeRegCache[0]) ) drvPadAfeRegCache[a] = buf[1]; return 0; } static DevBusMappedAccessRec qspiPadAccess = { qspiReadPadReg, qspiWritePadReg }; static int qspiWritePadCalm(DevBusMappedPvt pvt, unsigned value, int idx, dbCommon *prec) { /* extract 'soft toggle' information via an ugly hack * (we shouldn't have to know what record type 'prec' is * at the driver level!) */ calTgl = (((mbboRecord *)prec)->val > 3); return qspiWritePadReg(pvt, value, idx, prec); } static DevBusMappedAccessRec qspiCalmAccess = { qspiReadPadReg, qspiWritePadCalm }; static int padSoftCalRd(DevBusMappedPvt pvt, unsigned *pvalue, int idx, dbCommon *prec) { *pvalue = 0; return -1; } static int padSoftCalWr(DevBusMappedPvt pvt, unsigned value, int idx, dbCommon *prec) { uint32_t cs = ((uint32_t)pvt->addr) >> 8; drvPadSoftCal(value, cs, drvPadTrigPin, drvPadIrqPin); return 0; } static DevBusMappedAccessRec softCalAccess = { padSoftCalRd, padSoftCalWr }; static long padAfeQspiReport(int level) { epicsPrintf("PAD QSPI Support for Communication with AFE\n"); return 0; } static long padAfeQspiInit(void) { long rval; uint8_t buf[2] = { 0xc0, 00 }; int key; /* Initialize + setup QSPI driver */ if ( ( rval = coldfQspiInit() ) ) return rval; if ( ( rval = coldfQspiSetup(0, BAUDRATE, 0, 0, DRV5282_QSPI_SETUP_CS_ACTVLOW) ) ) { return rval; } /* Check for version >= 4 */ buf[0] += AFE_REG_VER; if ( 2 != coldfQspiWriteRead(buf, buf, 2, drvPadAfeCS) ) { return -1; } drvPadAfeRegCache[AFE_REG_VER] = buf[1]; if ( buf[1] < DRVPAD_MIN_AFE_VER || 0xff == buf[1] ) { epicsPrintf("Warning: AFE Firmware too old or no AFE present -- has %i but I need at least %i\n", buf[1], DRVPAD_MIN_AFE_VER); epicsPrintf(" ==> AFE DISABLED\n"); return -1; } if ( ! (theAfe = devBusMappedRegister("AFE",(volatile void *) (drvPadAfeCS << 8))) ) { epicsPrintf("FATAL ERROR: Unable to register AFE with bus mapped devsup\n"); return -1; } if ( (rval = devBusMappedRegisterIO("qspiPadAfe",&qspiPadAccess)) ) return rval; if ( (rval = devBusMappedRegisterIO("softCalAfe",&softCalAccess)) ) return rval; if ( (rval = devBusMappedRegisterIO("softTogAfe",&qspiCalmAccess)) ) return rval; if ( drvPadAfeRegCache[AFE_REG_VER] >= AFE_VERS4 ) { /* Upper 2 CS lines are handshake of CAL trigger detection */ key = epicsInterruptLock(); MCF5282_GPIO_PQSPAR &= ~(MCF5282_GPIO_PQSPAR_PQSPA6 | MCF5282_GPIO_PQSPAR_PQSPA5); MCF5282_GPIO_DDRQS &= ~(CALT_FLG_BIT); MCF5282_GPIO_DDRQS |= CALT_RST_BIT; MCF5282_GPIO_PORTQS &= ~(CALT_RST_BIT); epicsInterruptUnlock(key); } return 0; } /* Check if a CAL trigger has arrived; if 'do_reset' is nonzero then * the flag is reset */ int drvPadAfeGotCalt(int do_reset) { int key, rval; if ( !theAfe || drvPadAfeSimMode ) { return -1; } key = epicsInterruptLock(); if ( (rval = (MCF5282_GPIO_PORTQSP & CALT_FLG_BIT)) && do_reset ) { MCF5282_GPIO_PORTQS |= CALT_RST_BIT; MCF5282_GPIO_PORTQS &= ~(CALT_RST_BIT); } epicsInterruptUnlock(key); return rval; } int drvPadAfeGetCalm() { return AFE_REG_CSR_CALM_GET(drvPadAfeRegCache[AFE_REG_CSR]); } static struct { long number; DEVSUPFUN report; DEVSUPFUN init; } drvPadAfeQspi = { 2, padAfeQspiReport, padAfeQspiInit }; epicsExportAddress(drvet,drvPadAfeQspi);