#include #include #include #include #include #include #include #include #include #include #include #include #include /* #include 4/23/8 want to stop needing this */ #ifdef NEVER /* Need all features of vers. 4 */ #define AFE_VERS4 4 #define DRVPAD_MIN_AFE_VER AFE_VERS4 #endif #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 /* uint8_t drvPadAfeRegCache[32]; 4/23/8 moved to drvPadLan9118.h, instead of extern */ static DevBusMappedDev theAfe = 0; static int calTgl = 0; static int checkOverrun(int irq_pin) { /* If we need a soft trigger then things are more complicated: * We need to probe the status of the IRQ line because if we * raise the trigger while a acquisition is in progress * then we could lock out a trigger instead of generating one... */ if ( coldfEportEppdrGet(irq_pin) ) { /* no acquisition in progress, either a HW trigger * happening right now or this SW trigger will be * caught. */ } else { /* Ops. IRQ line was already low. In this case, * we have to check if this is a new interrupt * that just happened or if it was lost. */ if ( coldfEportFlagGet(irq_pin) ) { /* still pending just let them detect this interrupt */ } else { return -1; } } return 0; } /* Issue a software ADC trigger * RETURNS: 0 on success, nonzero if no trigger raised * (but a ongoing acq. is used with no IRQ pending). * */ int drvPadAdcTrigger(int trig_pin, int irq_pin) { int old_mask; int rval; old_mask = coldfEportIntDisable_inl(irq_pin); coldfEportEpdrSet(trig_pin, 1); rval = checkOverrun(irq_pin); if ( old_mask ) coldfEportIntEnable(irq_pin); return rval; } /* 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[6] = 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 */ checkOverrun(irq_pin); if ( old_mask ) coldfEportIntEnable(irq_pin); } #ifdef NEVER /* 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 drvPadCalToggle(int cs) { uint8_t buf[2]; int rval = -2, v; if ( !theAfe ) return rval; epicsMutexLock( theAfe->mutex ); if ( ! calTgl ) { rval = -1; goto bail; } 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, cs) ) { goto bail; } drvPadAfeRegCache[AFE_REG_CSR] = buf[1]; rval = v; bail: epicsMutexUnlock( theAfe->mutex ); return rval; } #endif #ifdef NEVER static int qspiReadPadReg(DevBusMappedPvt pvt, unsigned *pvalue, 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; } #endif #ifdef NEVER static int qspiWritePadReg(DevBusMappedPvt pvt, unsigned value, 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; } #endif #ifdef NEVER static DevBusMappedAccessRec qspiPadAccess = { qspiReadPadReg, qspiWritePadReg }; #endif #ifdef NEVER static int qspiWritePadCalm(DevBusMappedPvt pvt, unsigned value, 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, prec); } #endif #ifdef NEVER static DevBusMappedAccessRec qspiCalmAccess = { qspiReadPadReg, qspiWritePadCalm }; #endif #ifdef NEVER static int padSoftCalRd(DevBusMappedPvt pvt, unsigned *pvalue, dbCommon *prec) { *pvalue = 0; return -1; } #endif #ifdef NEVER static int padSoftCalWr(DevBusMappedPvt pvt, unsigned value, dbCommon *prec) { uint32_t cs = ((uint32_t)pvt->addr) >> 8; drvPadSoftCal(value, cs, TRIG_PIN, IRQ_PIN); return 0; } #endif #ifdef NEVER static DevBusMappedAccessRec softCalAccess = { padSoftCalRd, padSoftCalWr }; #endif #ifdef NEVER static long padAfeQspiReport(int level) { epicsPrintf("PAD QSPI Support for Communication with AFE\n"); return 0; } #endif #ifdef NEVER 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, AFE_CS) ) { return -1; } if ( (drvPadAfeRegCache[AFE_REG_VER] = buf[1]) < DRVPAD_MIN_AFE_VER ) { 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 *) (AFE_CS << 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; } #endif #ifdef NEVER /* 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; /* Always return 0 if no AFE hardware is present */ if ( !theAfe ) return 0; 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; } #endif #ifdef NEVER static struct { long number; DEVSUPFUN report; DEVSUPFUN init; } drvPadAfeQspi = { 2, padAfeQspiReport, padAfeQspiInit }; epicsExportAddress(drvet,drvPadAfeQspi); #endif