1 /* refclock_bancomm.c - clock driver for the Datum/Bancomm bc635VME
2 * Time and Frequency Processor. It requires the BANCOMM bc635VME/
3 * bc350VXI Time and Frequency Processor Module Driver for SunOS4.x
4 * and SunOS5.x UNIX Systems. It has been tested on a UltraSparc
5 * IIi-cEngine running Solaris 2.6.
7 * Author(s): Ganesh Ramasivan & Gary Cliff, Computing Devices Canada,
12 * Note(s): The refclock type has been defined as 16.
14 * This program has been modelled after the Bancomm driver
15 * originally written by R. Schmidt of Time Service, U.S.
16 * Naval Observatory for a HP-UX machine. Since the original
17 * authors no longer plan to maintain this code, all
18 * references to the HP-UX vme2 driver subsystem bave been
19 * removed. Functions vme_report_event(), vme_receive(),
20 * vme_control() and vme_buginfo() have been deleted because
21 * they are no longer being used.
23 * The time on the bc635 TFP must be set to GMT due to the
24 * fact that NTP makes use of GMT for all its calculations.
26 * Installation of the Datum/Bancomm driver creates the
27 * device file /dev/btfp0
30 * Modified to add support for Symmetricom bc637PCI-U Time &
31 * Frequency Processor.
32 * Card bus type (VME/VXI or PCI) and environment are specified via the
33 * "mode" keyword on the server command in ntp.conf.
34 * server 127.127.16.u prefer mode m (...)
35 * Modes currently supported are
36 * 1 : FreeBSD PCI 635/637.
37 * 2 : Linux or Windows PCI 635/637.
38 * not specified, or other number:
39 * : Assumed to be VME/VXI legacy Bancomm card on Solaris.
40 * Linux and Windows platforms require Symmetricoms' proprietary driver
42 * Tested on FreeBSD 5.3 with a 637 card.
49 #if defined(REFCLOCK) && defined(CLOCK_BANC)
53 #include "ntp_refclock.h"
54 #include "ntp_unixtime.h"
55 #include "ntp_stdlib.h"
62 struct btfp_time /* Structure for reading 5 time words */
63 /* in one ioctl(2) operation. */
65 unsigned short btfp_time[5]; /* Time words 0,1,2,3, and 4. (16bit)*/
67 /* SunOS5 ioctl commands definitions.*/
68 #define BTFPIOC ( 'b'<< 8 )
69 #define IOCIO( l, n ) ( BTFPIOC | n )
70 #define IOCIOR( l, n, s ) ( BTFPIOC | n )
71 #define IOCIORN( l, n, s ) ( BTFPIOC | n )
72 #define IOCIOWN( l, n, s ) ( BTFPIOC | n )
74 /***** Simple ioctl commands *****/
75 #define RUNLOCK IOCIOR(b, 19, int ) /* Release Capture Lockout */
76 #define RCR0 IOCIOR(b, 22, int ) /* Read control register zero.*/
77 #define WCR0 IOCIOWN(b, 23, int) /* Write control register zero*/
78 /***** Compound ioctl commands *****/
80 /* Read all 5 time words in one call. */
81 #define READTIME IOCIORN(b, 32, sizeof( struct btfp_time ))
83 #if defined(__FreeBSD__)
85 #define READTIME _IOR('u', 5, struct btfp_time )
88 #define VMEFD "/dev/btfp0"
90 struct vmedate { /* structure returned by get_vmetime.c */
97 unsigned short status;
100 /* END OF STUFF FROM RES */
101 typedef void *SYMMT_PCI_HANDLE;
104 * VME interface parameters.
106 #define VMEPRECISION (-21) /* precision assumed (1 us) */
107 #define USNOREFID "BTFP" /* or whatever */
108 #define VMEREFID "BTFP" /* reference id */
109 #define VMEDESCRIPTION "Bancomm bc635 TFP" /* who we are */
110 #define VMEHSREFID 0x7f7f1000 /* 127.127.16.00 refid hi strata */
111 /* clock type 16 is used here */
112 #define GMT 0 /* hour offset from Greenwich */
115 * Imported from ntp_timer module
117 extern u_long current_time; /* current time(s) */
120 * Imported from ntpd module
122 extern volatile int debug; /* global debug flag */
125 * VME unit control structure.
126 * Changes made to vmeunit structure. Most members are now available in the
127 * new refclockproc structure in ntp_refclock.h - 07/99 - Ganesh Ramasivan
130 struct vmedate vmedata; /* data returned from vme read */
131 u_long lasttime; /* last time clock heard from */
135 * Function prototypes
137 static int vme_start (int, struct peer *);
138 static void vme_shutdown (int, struct peer *);
139 static void vme_receive (struct recvbuf *);
140 static void vme_poll (int unit, struct peer *);
141 struct vmedate *get_datumtime(struct vmedate *);
142 void tvme_fill(struct vmedate *, uint32_t btm[2]);
144 * Define the bc*() functions as weak so we can compile/link without them.
145 * Only clients with the card will have the proprietary vendor device driver
146 * and interface library needed for use on Linux/Windows platforms.
148 extern uint32_t __attribute__ ((weak)) bcReadBinTime(SYMMT_PCI_HANDLE, uint32_t *, uint32_t*, uint8_t*);
149 extern SYMMT_PCI_HANDLE __attribute__ ((weak)) bcStartPci(void);
150 extern void __attribute__ ((weak)) bcStopPci(SYMMT_PCI_HANDLE);
155 struct refclock refclock_bancomm = {
156 vme_start, /* start up driver */
157 vme_shutdown, /* shut down driver */
158 vme_poll, /* transmit poll message */
159 noentry, /* not used (old vme_control) */
160 noentry, /* initialize driver */
161 noentry, /* not used (old vme_buginfo) */
162 NOFLAGS /* not used */
165 int fd_vme; /* file descriptor for ioctls */
167 int tfp_type; /* mode selector, indicate platform and driver interface */
168 SYMMT_PCI_HANDLE stfp_handle;
172 * vme_start - open the VME device and initialize data for processing
180 register struct vmeunit *vme;
181 struct refclockproc *pp;
185 tfp_type = (int)(peer->ttl);
190 stfp_handle = bcStartPci(); /* init the card in lin/win */
200 printf("Opening DATUM VME DEVICE \n");
202 if ( (fd_vme = open(VMEFD, O_RDWR)) < 0) {
203 msyslog(LOG_ERR, "vme_start: failed open of %s: %m", vmedev);
211 /* Release capture lockout in case it was set before. */
212 if( ioctl( fd_vme, RUNLOCK, &dummy ) )
213 msyslog(LOG_ERR, "vme_start: RUNLOCK failed %m");
215 regvalue = 0; /* More esoteric stuff to do... */
216 if( ioctl( fd_vme, WCR0, ®value ) )
217 msyslog(LOG_ERR, "vme_start: WCR0 failed %m");
223 * Allocate unit structure
225 vme = (struct vmeunit *)emalloc(sizeof(struct vmeunit));
226 bzero((char *)vme, sizeof(struct vmeunit));
230 * Set up the structures
233 pp->unitptr = (caddr_t) vme;
234 pp->timestarted = current_time;
236 pp->io.clock_recv = vme_receive;
237 pp->io.srcclock = (caddr_t)peer;
242 * All done. Initialize a few random peer variables, then
243 * return success. Note that root delay and root dispersion are
244 * always zero for this clock.
246 peer->precision = VMEPRECISION;
247 memcpy(&pp->refid, USNOREFID,4);
253 * vme_shutdown - shut down a VME clock
261 register struct vmeunit *vme;
262 struct refclockproc *pp;
265 * Tell the I/O module to turn us off. We're history.
268 vme = (struct vmeunit *)pp->unitptr;
269 io_closeclock(&pp->io);
272 if (tfp_type == 2) bcStopPci(stfp_handle);
277 * vme_receive - receive data from the VME device.
279 * Note: This interface would be interrupt-driven. We don't use that
280 * now, but include a dummy routine for possible future adventures.
284 struct recvbuf *rbufp
291 * vme_poll - called by the transmit procedure
299 struct vmedate *tptr;
301 struct refclockproc *pp;
306 vme = (struct vmeunit *)pp->unitptr; /* Here is the structure */
308 tptr = &vme->vmedata;
309 if ((tptr = get_datumtime(tptr)) == NULL ) {
310 refclock_report(peer, CEVNT_BADREPLY);
314 get_systime(&pp->lastrec);
316 vme->lasttime = current_time;
319 * Get VME time and convert to timestamp format.
320 * The year must come from the system clock.
324 tadr = gmtime(&tloc);
325 tptr->year = (unsigned short)(tadr->tm_year + 1900);
327 sprintf(pp->a_lastcode,
328 "%3.3d %2.2d:%2.2d:%2.2d.%.6ld %1d",
336 pp->lencode = (u_short) strlen(pp->a_lastcode);
340 pp->minute = tptr->mn;
341 pp->second = tptr->sec;
342 pp->nsec = tptr->frac;
346 printf("pp: %3d %02d:%02d:%02d.%06ld %1x\n",
347 pp->day, pp->hour, pp->minute, pp->second,
348 pp->nsec, tptr->status);
350 if (tptr->status ) { /* Status 0 is locked to ref., 1 is not */
351 refclock_report(peer, CEVNT_BADREPLY);
356 * Now, compute the reference time value. Use the heavy
357 * machinery for the seconds and the millisecond field for the
358 * fraction when present. If an error in conversion to internal
359 * format is found, the program declares bad data and exits.
360 * Note that this code does not yet know how to do the years and
361 * relies on the clock-calendar chip for sanity.
363 if (!refclock_process(pp)) {
364 refclock_report(peer, CEVNT_BADTIME);
367 pp->lastref = pp->lastrec;
368 refclock_receive(peer);
369 record_clock_stats(&peer->srcadr, pp->a_lastcode);
373 get_datumtime(struct vmedate *time_vme)
376 struct btfp_time vts;
380 if ( time_vme == (struct vmedate *)NULL) {
381 time_vme = (struct vmedate *)malloc(sizeof(struct vmedate ));
385 case 1: /* BSD, PCI, 2 32bit time words */
386 if (ioctl(fd_vme, READTIME, &btm)) {
387 msyslog(LOG_ERR, "get_bc63x error: %m");
390 tvme_fill(time_vme, btm);
393 case 2: /* Linux/Windows, PCI, 2 32bit time words */
394 if (bcReadBinTime(stfp_handle, &btm[1], &btm[0], &dmy) == 0) {
395 msyslog(LOG_ERR, "get_datumtime error: %m");
398 tvme_fill(time_vme, btm);
401 default: /* legacy bancomm card */
403 if (ioctl(fd_vme, READTIME, &vts)) {
404 msyslog(LOG_ERR, "get_datumtime error: %m");
408 sprintf(cbuf,"%3.3x", ((vts.btfp_time[ 0 ] & 0x000f) <<8) +
409 ((vts.btfp_time[ 1 ] & 0xff00) >> 8));
410 time_vme->day = (unsigned short)atoi(cbuf);
413 sprintf(cbuf,"%2.2x", vts.btfp_time[ 1 ] & 0x00ff);
415 time_vme->hr = (unsigned short)atoi(cbuf);
418 sprintf(cbuf,"%2.2x", (vts.btfp_time[ 2 ] & 0xff00) >>8);
419 time_vme->mn = (unsigned short)atoi(cbuf);
422 sprintf(cbuf,"%2.2x", vts.btfp_time[ 2 ] & 0x00ff);
423 time_vme->sec = (unsigned short)atoi(cbuf);
425 /* Get microseconds. Yes, we ignore the 0.1 microsecond digit so
426 we can use the TVTOTSF function later on...*/
428 sprintf(cbuf,"%4.4x%2.2x", vts.btfp_time[ 3 ],
429 vts.btfp_time[ 4 ]>>8);
431 time_vme->frac = (u_long) atoi(cbuf);
434 time_vme->status = (vts.btfp_time[0] & 0x0010) >>4;
439 if (time_vme->status)
440 return ((void *)NULL);
444 /* Assign values to time_vme struct. Mostly for readability */
446 tvme_fill(struct vmedate *time_vme, uint32_t btm[2])
451 dmaj = btm[1]; /* syntax sugar */
454 gmtime_r(&dmaj, &maj);
455 time_vme->day = maj.tm_yday+1;
456 time_vme->hr = maj.tm_hour;
457 time_vme->mn = maj.tm_min;
458 time_vme->sec = maj.tm_sec;
459 time_vme->frac = (dmin & 0x000fffff) * 1000;
460 time_vme->frac += ((dmin & 0x00f00000) >> 20) * 100;
461 time_vme->status = (dmin & 0x01000000) >> 24;
466 int refclock_bancomm_bs;
467 #endif /* REFCLOCK */