]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/i386/isa/loran.c
This commit was generated by cvs2svn to compensate for changes in r53796,
[FreeBSD/FreeBSD.git] / sys / i386 / isa / loran.c
1 /*
2  * ----------------------------------------------------------------------------
3  * "THE BEER-WARE LICENSE" (Revision 42):
4  * <phk@FreeBSD.org> wrote this file.  As long as you retain this notice you
5  * can do whatever you want with this stuff. If we meet some day, and you think
6  * this stuff is worth it, you can buy me a beer in return.   Poul-Henning Kamp
7  * ----------------------------------------------------------------------------
8  *
9  * $FreeBSD$
10  *
11  * This device-driver helps the userland controlprogram for a LORAN-C
12  * receiver avoid monopolizing the CPU.
13  *
14  * This is clearly a candidate for the "most weird hardware support in
15  * FreeBSD" prize.  At this time only two copies of the receiver are
16  * known to exist in the entire world.
17  *
18  * Details can be found at:
19  *     ftp://ftp.eecis.udel.edu/pub/ntp/loran.tar.Z
20  *
21  */
22
23 #ifdef KERNEL
24 #include <sys/param.h>
25 #include <sys/systm.h>
26 #include <sys/sysctl.h>
27 #include <sys/conf.h>
28 #include <sys/kernel.h>
29 #include <sys/uio.h>
30 #include <sys/malloc.h>
31
32 #include <i386/isa/isa_device.h>
33 #endif /* KERNEL */
34
35 typedef TAILQ_HEAD(, datapoint) dphead_t;
36
37 struct datapoint {
38         /* Fields used by kernel */
39         u_int64_t               scheduled;
40         u_int                   code;
41         u_int                   fri;
42         u_int                   agc;
43         u_int                   phase;
44         u_int                   width;
45         u_int                   par;
46         u_int                   isig;
47         u_int                   qsig;
48         u_int                   ssig;
49         u_int64_t               epoch;
50         TAILQ_ENTRY(datapoint)  list;
51         int                     vco;
52         int                     bounce;
53         pid_t                   pid;
54         struct timespec         when;
55
56         int                     priority;
57         dphead_t                *home;
58
59         /* Fields used only in userland */
60         void                    (*proc)(struct datapoint *);
61         void                    *ident;
62         int                     index;
63         char                    *name;
64
65
66         /* Fields used only in userland */
67         double                  ival;
68         double                  qval;
69         double                  sval;
70         double                  mval;
71
72 };
73
74 /*
75  * Mode register (PAR) hardware definitions
76  */
77     #define INTEG 0x03          /* integrator mask */
78         #define INTEG_1000us    0
79         #define INTEG_264us     1
80         #define INTEG_36us      2
81         #define INTEG_SHORT     3
82     #define GATE 0x0C           /* gate source mask */
83         #define GATE_OPEN       0x0
84         #define GATE_GRI        0x4
85         #define GATE_PCI        0x8
86         #define GATE_STB        0xc
87     #define MSB 0x10            /* load dac high-order bits */
88     #define IEN 0x20            /* enable interrupt bit */
89     #define EN5 0x40            /* enable counter 5 bit */
90     #define ENG 0x80            /* enable gri bit */
91
92 #define VCO_SHIFT 8             /* bits of fraction on VCO value */
93 #define VCO (2048 << VCO_SHIFT) /* initial vco dac (0 V)*/
94
95
96 #define PGUARD 990             /* program guard time (cycle) (990!) */
97
98 #ifdef KERNEL
99
100 #define NLORAN  10              /* Allow ten minor devices */
101
102 #define NDUMMY 4                /* How many idlers we want */
103
104 #define PORT 0x0300             /* controller port address */
105
106
107 #define GRI 800                 /* pulse-group gate (cycle) */
108
109 /*
110  * Analog/digital converter (ADC) hardware definitions
111  */
112 #define ADC PORT+2              /* adc buffer (r)/address (w) */
113 #define ADCGO PORT+3            /* adc status (r)/adc start (w) */
114     #define ADC_START 0x01      /* converter start bit (w) */
115     #define ADC_BUSY 0x01       /* converter busy bit (r) */
116     #define ADC_DONE 0x80       /* converter done bit (r) */
117     #define ADC_I 0             /* i channel (phase) */
118     #define ADC_Q 1             /* q channel (amplitude) */
119     #define ADC_S 2             /* s channel (agc) */
120
121 /*
122  * Digital/analog converter (DAC) hardware definitions
123  * Note: output voltage increases with value programmed; the buffer
124  * is loaded in two 8-bit bytes, the lsb 8 bits with the MSB bit off in
125  * the PAR register, the msb 4 bits with the MSB on.
126  */
127 #define DACA PORT+4             /* vco (dac a) buffer (w) */
128 #define DACB PORT+5             /* agc (dac b) buffer (w) */
129
130 #define LOAD_DAC(dac, val) if (0) { } else {                            \
131         par &= ~MSB; outb(PAR, par); outb((dac), (val) & 0xff);         \
132         par |=  MSB; outb(PAR, par); outb((dac), ((val) >> 8) & 0xff);  \
133         }
134
135 /*
136  * Pulse-code generator (CODE) hardware definitions
137  * Note: bits are shifted out from the lsb first
138  */
139 #define CODE PORT+6             /* pulse-code buffer (w) */
140     #define MPCA 0xCA           /* LORAN-C master pulse code group a */
141     #define MPCB 0x9F           /* LORAN-C master pulse code group b */
142     #define SPCA 0xF9           /* LORAN-C slave pulse code group a */
143     #define SPCB 0xAC           /* LORAN-C slave pulse code group b */
144
145 /*
146  * Mode register (PAR) hardware definitions
147  */
148 #define PAR PORT+7              /* parameter buffer (w) */
149
150 #define TGC PORT+0              /* stc control port (r/w) */
151 #define TGD PORT+1              /* stc data port (r/w) */
152
153 /*
154  * Timing generator (STC) hardware commands
155  */
156 /* argument sssss = counter numbers 5-1 */
157 #define TG_LOADDP 0x00             /* load data pointer */
158     /* argument ee = element (all groups except ggg = 000 or 111) */
159     #define MODEREG 0x00        /* mode register */
160     #define LOADREG 0x08        /* load register */
161     #define HOLDREG 0x10        /* hold register */
162     #define HOLDINC 0x18        /* hold register (hold cycle increm) */
163     /* argument ee = element (group ggg = 111) */
164     #define ALARM1 0x07         /* alarm register 1 */
165     #define ALARM2 0x0F         /* alarm register 2 */
166     #define MASTER 0x17         /* master mode register */
167     #define STATUS 0x1F         /* status register */
168 #define ARM 0x20                /* arm counters */
169 #define LOAD 0x40               /* load counters */
170 #define TG_LOADARM 0x60            /* load and arm counters */
171 #define DISSAVE 0x80            /* disarm and save counters */
172 #define TG_SAVE 0xA0               /* save counters */
173 #define DISARM 0xC0             /* disarm counters */
174 /* argument nnn = counter number */
175 #define SETTOG 0xE8             /* set toggle output HIGH for counter */
176 #define CLRTOG 0xE0             /* set toggle output LOW for counter */
177 #define STEP 0xF0               /* step counter */
178 /* argument eeggg, where ee = element, ggg - counter group */
179 /* no arguments */
180 #define ENABDPS 0xE0            /* enable data pointer sequencing */
181 #define ENABFOUT 0xE6           /* enable fout */
182 #define ENAB8 0xE7              /* enable 8-bit data bus */
183 #define DSABDPS 0xE8            /* disable data pointer sequencing */
184 #define ENAB16 0xEF             /* enable 16-bit data bus */
185 #define DSABFOUT 0xEE           /* disable fout */
186 #define ENABPFW 0xF8            /* enable prefetch for write */
187 #define DSABPFW 0xF9            /* disable prefetch for write */
188 #define TG_RESET 0xFF              /* master reset */
189
190 #define LOAD_9513(index, val) if (0) {} else {          \
191         outb(TGC, TG_LOADDP + (index));                 \
192         outb(TGD, (val) & 0xff);                                        \
193         outb(TGD, ((val) >> 8) & 0xff);                         \
194         }
195
196 #define NENV 40                 /* size of envelope filter */
197 #define CLOCK 50                /* clock period (clock) */
198 #define CYCLE 10                /* carrier period (us) */
199 #define PCX (NENV * CLOCK)      /* envelope gate (clock) */
200 #define STROBE 50               /* strobe gate (clock) */
201
202 /**********************************************************************/
203
204 extern struct cdevsw loran_cdevsw;
205
206 static dphead_t minors[NLORAN + 1], working;
207
208 static struct datapoint dummy[NDUMMY], *first, *second;
209
210 static u_int64_t ticker;
211
212 static u_char par;
213
214 static MALLOC_DEFINE(M_LORAN, "Loran", "Loran datapoints");
215
216 static int loranerror;
217 static char lorantext[160];
218
219 static u_int vco_is;
220 static u_int vco_should;
221 static u_int vco_want;
222 static u_int64_t vco_when;
223 static int64_t vco_error;
224
225 /**********************************************************************/
226
227 static  int             loranprobe (struct isa_device *dvp);
228 static  void            init_tgc (void);
229 static  int             loranattach (struct isa_device *isdp);
230 static  void            loranenqueue (struct datapoint *);
231 static  d_open_t        loranopen;
232 static  d_close_t       loranclose;
233 static  d_read_t        loranread;
234 static  d_write_t       loranwrite;
235 static  ointhand2_t     loranintr;
236 extern  struct timecounter loran_timecounter;
237
238 /**********************************************************************/
239
240 int
241 loranprobe(struct isa_device *dvp)
242 {
243         static int once;
244
245         if (!once++)
246                 cdevsw_add(&loran_cdevsw);
247         /* We need to be a "fast-intr" */
248         dvp->id_ri_flags |= RI_FAST;
249
250         dvp->id_iobase = PORT;
251         return (8);
252 }
253
254 static u_short tg_init[] = {            /* stc initialization vector    */
255         0x0562,      12,         13,    /* counter 1 (p0)  Mode J       */
256         0x0262,  PGUARD,        GRI,    /* counter 2 (gri) Mode J       */
257         0x8562,     PCX, 5000 - PCX,    /* counter 3 (pcx)              */
258         0xc562,       0,     STROBE,    /* counter 4 (stb) Mode L       */
259         0x052a,       0,          0     /* counter 5 (out)              */
260 };
261
262 static void
263 init_tgc(void)
264 {
265         int i;
266
267         /* Initialize the 9513A */
268         outb(TGC, TG_RESET);         outb(TGC, LOAD+0x1f); /* reset STC chip */
269         LOAD_9513(MASTER, 0x8af0);
270         outb(TGC, TG_LOADDP+1);
271         tg_init[4] = 7499 - GRI;
272         for (i = 0; i < 5*3; i++) {
273                 outb(TGD, tg_init[i]);
274                 outb(TGD, tg_init[i] >> 8);
275         }
276         outb(TGC, TG_LOADARM+0x1f);    /* let the good times roll */
277 }
278
279 int
280 loranattach(struct isa_device *isdp)
281 {
282         int i;
283
284         isdp->id_ointr = loranintr;
285
286         /* We need to be a "fast-intr" */
287         isdp->id_ri_flags |= RI_FAST;
288
289         printf("loran0: LORAN-C Receiver\n");
290
291         vco_want = vco_should = VCO;
292         vco_is = vco_should >> VCO_SHIFT;
293         LOAD_DAC(DACA, vco_is);
294          
295         init_tgc();
296
297         init_timecounter(&loran_timecounter);
298
299         TAILQ_INIT(&working);
300         for (i = 0; i < NLORAN + 1; i++) {
301                 TAILQ_INIT(&minors[i]);
302                 
303         }
304
305         for (i = 0; i < NDUMMY; i++) {
306                 dummy[i].agc = 4095;
307                 dummy[i].code = 0xac;
308                 dummy[i].fri = PGUARD;
309                 dummy[i].scheduled = PGUARD * 2 * i;
310                 dummy[i].phase = 50;
311                 dummy[i].width = 50;
312                 dummy[i].priority = NLORAN * 256;
313                 dummy[i].home = &minors[NLORAN];
314                 if (i == 0) 
315                         first = &dummy[i];
316                 else if (i == 1) 
317                         second = &dummy[i];
318                 else
319                         TAILQ_INSERT_TAIL(&working, &dummy[i], list);
320         }
321
322         inb(ADC);               /* Flush any old result */
323         outb(ADC, ADC_S);
324
325         par = ENG|IEN;
326         outb(PAR, par);
327
328         return (1);
329 }
330
331 static  int
332 loranopen (dev_t dev, int flags, int fmt, struct proc *p)
333 {
334         int idx;
335
336         idx = minor(dev);
337         if (idx >= NLORAN) 
338                 return (ENODEV);
339
340         return(0);
341 }
342
343 static  int
344 loranclose(dev_t dev, int flags, int fmt, struct proc *p)
345 {
346         return(0);
347 }
348
349 static  int
350 loranread(dev_t dev, struct uio * uio, int ioflag)
351 {
352         u_long ef;
353         struct datapoint *this;
354         int err, c;
355         int idx;
356
357         idx = minor(dev);
358         
359         if (loranerror) {
360                 printf("Loran0: %s", lorantext);
361                 loranerror = 0;
362                 return(EIO);
363         }
364         if (TAILQ_EMPTY(&minors[idx])) 
365                 tsleep ((caddr_t)&minors[idx], (PZERO + 8) |PCATCH, "loranrd", hz*2);
366         if (TAILQ_EMPTY(&minors[idx])) 
367                 return(0);
368         this = TAILQ_FIRST(&minors[idx]);
369         ef = read_eflags();
370         disable_intr();
371         TAILQ_REMOVE(&minors[idx], this, list);
372         write_eflags(ef);
373
374         c = imin(uio->uio_resid, (int)sizeof *this);
375         err = uiomove((caddr_t)this, c, uio);        
376         FREE(this, M_LORAN);
377         return(err);
378 }
379
380 static void
381 loranenqueue(struct datapoint *dp)
382 {
383         struct datapoint *dpp;
384
385         TAILQ_FOREACH(dpp, &working, list) {
386                 if (dpp->priority <= dp->priority)
387                         continue;
388                 TAILQ_INSERT_BEFORE(dpp, dp, list);
389                 return;
390         }
391         TAILQ_INSERT_TAIL(&working, dp, list);
392 }
393
394 static  int
395 loranwrite(dev_t dev, struct uio * uio, int ioflag)
396 {
397         u_long ef;
398         int err = 0, c;
399         struct datapoint *this;
400         int idx;
401         u_int64_t dt;
402         u_int64_t when;
403
404         idx = minor(dev);
405
406         MALLOC(this, struct datapoint *, sizeof *this, M_LORAN, M_WAITOK);
407         c = imin(uio->uio_resid, (int)sizeof *this);
408         err = uiomove((caddr_t)this, c, uio);        
409         if (err) {
410                 FREE(this, M_LORAN);
411                 return (err);
412         }
413         if (this->fri == 0) {
414                 FREE(this, M_LORAN);
415                 return (EINVAL);
416         }
417         this->par &= INTEG|GATE;
418         /* XXX more checks needed! */
419         this->home = &minors[idx];
420         this->priority &= 0xff;
421         this->priority += idx * 256;
422         this->bounce = 0;
423         when = second->scheduled + PGUARD;
424         if (when > this->scheduled) {
425                 dt = when - this->scheduled;
426                 dt -= dt % this->fri;
427                 this->scheduled += dt;
428         }
429         ef = read_eflags();
430         disable_intr();
431         loranenqueue(this);
432         write_eflags(ef);
433         if (this->vco >= 0)
434                 vco_want = this->vco;
435         return(err);
436 }
437
438 static void
439 loranintr(int unit)
440 {
441         u_long ef;
442         int status = 0, i;
443 #if 0
444         int count = 0;
445 #endif
446         int delay;
447         u_int64_t when;
448         struct timespec there, then;
449         struct datapoint *dp, *done;
450
451         ef = read_eflags();
452         disable_intr();
453
454         /*
455          * Pick up the measurement which just completed, and setup
456          * the next measurement.  We have 1100 microseconds for this
457          * of which some eaten by the A/D of the S channel and the 
458          * interrupt to get us here.
459          */
460
461         done = first;
462
463         nanotime(&there);
464         done->ssig = inb(ADC);
465
466         par &= ~(ENG | IEN);
467         outb(PAR, par);
468
469         outb(ADC, ADC_I);
470         outb(ADCGO, ADC_START);
471
472         /* Interlude: while we wait: setup the next measurement */
473                 LOAD_DAC(DACB, second->agc);
474                 outb(CODE, second->code);
475                 par &= ~(INTEG|GATE);
476                 par |= second->par;
477                 par |= ENG | IEN;
478
479         while (!(inb(ADCGO) & ADC_DONE))
480                 continue;
481         done->isig = inb(ADC);
482
483         outb(ADC, ADC_Q);
484         outb(ADCGO, ADC_START);
485         /* Interlude: while we wait: setup the next measurement */
486                 /*
487                  * We need to load this from the opposite register due to some 
488                  * weirdness which you can read about in in the 9513 manual on 
489                  * page 1-26 under "LOAD"
490                  */
491                 LOAD_9513(0x0c, second->phase);
492                 LOAD_9513(0x14, second->phase);
493                 outb(TGC, TG_LOADARM + 0x08);
494                 LOAD_9513(0x14, second->width);
495         while (!(inb(ADCGO) & ADC_DONE))
496                 continue;
497         done->qsig = inb(ADC);
498
499         outb(ADC, ADC_S);
500
501         outb(PAR, par);
502
503         /*
504          * End of VERY time critical stuff, we have 8 msec to find
505          * the next measurement and program the delay.
506          */
507         status = inb(TGC);
508         nanotime(&then);
509
510         first = second;
511         second = 0;
512         when = first->scheduled + PGUARD;
513         TAILQ_FOREACH(dp, &working, list) {
514                 while (dp->scheduled < when)
515                         dp->scheduled += dp->fri;
516                 if (second && dp->scheduled + PGUARD >= second->scheduled)
517                         continue;
518                 second = dp;
519         }
520
521         delay = (second->scheduled - first->scheduled) - GRI;
522
523         LOAD_9513(0x0a, delay);
524
525         /* Done, the rest is leisure work */
526
527         vco_error += ((vco_is << VCO_SHIFT) - vco_should) * 
528             (ticker - vco_when);
529         vco_should = vco_want;
530         i = vco_should >> VCO_SHIFT;
531         if (vco_error < 0)
532                 i++;
533         
534         if (vco_is != i) {
535                 LOAD_DAC(DACA, i);
536                 vco_is = i;
537         }
538         vco_when = ticker;
539
540         /* Check if we overran */
541         status &= 0x0c;
542 #if 0
543
544         if (status) {
545                 outb(TGC, TG_SAVE + 2);         /* save counter #2 */
546                 outb(TGC, TG_LOADDP + 0x12);    /* hold counter #2 */
547                 count = inb(TGD);
548                 count |= inb(TGD) << 8;
549                 LOAD_9513(0x12, GRI)
550         }
551 #endif
552
553         if (status) {
554                 printf( "Missed: %02x %d first:%p second:%p %.09ld\n",
555                     status, delay, first, second,
556                     then.tv_nsec - there.tv_nsec);
557                 first->bounce++;
558         }
559
560         TAILQ_REMOVE(&working, second, list);
561
562         if (done->bounce) {
563                 done->bounce = 0;
564                 loranenqueue(done);
565         } else {
566                 done->epoch = ticker;
567                 done->vco = vco_is;
568                 done->when = there;
569                 TAILQ_INSERT_TAIL(done->home, done, list);
570                 wakeup((caddr_t)done->home);
571         }
572
573         ticker = first->scheduled;
574
575         while ((dp = TAILQ_FIRST(&minors[NLORAN])) != NULL) {
576                 TAILQ_REMOVE(&minors[NLORAN], dp, list);
577                 TAILQ_INSERT_TAIL(&working, dp, list);
578         }
579
580         when = second->scheduled + PGUARD;
581
582         TAILQ_FOREACH(dp, &working, list) {
583                 while (dp->scheduled < when)
584                         dp->scheduled += dp->fri;
585         }
586         write_eflags(ef);
587 }
588
589 /**********************************************************************/
590
591 static unsigned
592 loran_get_timecount(struct timecounter *tc)
593 {
594         unsigned count;
595         u_long ef;
596
597         ef = read_eflags();
598         disable_intr();
599
600         outb(TGC, TG_SAVE + 0x10);      /* save counter #5 */
601         outb(TGC, TG_LOADDP +0x15);     /* hold counter #5 */
602         count = inb(TGD);
603         count |= inb(TGD) << 8;
604
605         write_eflags(ef);
606         return (count);
607 }
608
609 static struct timecounter loran_timecounter = {
610         loran_get_timecount,    /* get_timecount */
611         0,                      /* no pps_poll */
612         0xffff,                 /* counter_mask */
613         5000000,                /* frequency */
614         "loran"                 /* name */
615 };
616
617 SYSCTL_OPAQUE(_debug, OID_AUTO, loran_timecounter, CTLFLAG_RD, 
618         &loran_timecounter, sizeof(loran_timecounter), "S,timecounter", "");
619
620
621 /**********************************************************************/
622
623 struct  isa_driver lorandriver = {
624         loranprobe, loranattach, "loran"
625 };
626
627 #define CDEV_MAJOR 94
628 static struct cdevsw loran_cdevsw = {
629         /* open */      loranopen,
630         /* close */     loranclose,
631         /* read */      loranread,
632         /* write */     loranwrite,
633         /* ioctl */     noioctl,
634         /* poll */      nopoll,
635         /* mmap */      nommap,
636         /* strategy */  nostrategy,
637         /* name */      "loran",
638         /* maj */       CDEV_MAJOR,
639         /* dump */      nodump,
640         /* psize */     nopsize,
641         /* flags */     0,
642         /* bmaj */      -1
643 };
644
645 #endif /* KERNEL */