2 comedi/drivers/das16m1.c
4 Author: Frank Mori Hess, based on code from the das16
6 Copyright (C) 2001 Frank Mori Hess <fmhess@users.sourceforge.net>
8 COMEDI - Linux Control and Measurement Device Interface
9 Copyright (C) 2000 David A. Schleef <ds@schleef.org>
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 ************************************************************************
29 Description: CIO-DAS16/M1
30 Author: Frank Mori Hess <fmhess@users.sourceforge.net>
31 Devices: [Measurement Computing] CIO-DAS16/M1 (cio-das16/m1)
34 This driver supports a single board - the CIO-DAS16/M1.
35 As far as I know, there are no other boards that have
36 the same register layout. Even the CIO-DAS16/M1/16 is
37 significantly different.
39 I was _barely_ able to reach the full 1 MHz capability
40 of this board, using a hard real-time interrupt
41 (set the TRIG_RT flag in your struct comedi_cmd and use
42 rtlinux or RTAI). The board can't do dma, so the bottleneck is
43 pulling the data across the ISA bus. I timed the interrupt
44 handler, and it took my computer ~470 microseconds to pull 512
45 samples from the board. So at 1 Mhz sampling rate,
46 expect your CPU to be spending almost all of its
47 time in the interrupt handler.
49 This board has some unusual restrictions for its channel/gain list. If the
50 list has 2 or more channels in it, then two conditions must be satisfied:
51 (1) - even/odd channels must appear at even/odd indices in the list
52 (2) - the list must have an even number of entries.
56 [1] - irq (optional, but you probably want it)
58 irq can be omitted, although the cmd interface will not work without it.
61 #include <linux/ioport.h>
62 #include <linux/interrupt.h>
63 #include "../comedidev.h"
67 #include "comedi_fc.h"
69 #define DAS16M1_SIZE 16
70 #define DAS16M1_SIZE2 8
72 #define DAS16M1_XTAL 100 /* 10 MHz master clock */
74 #define FIFO_SIZE 1024 /* 1024 sample fifo */
81 0 a/d bits 0-3, mux start 12 bit
82 1 a/d bits 4-11 unused
85 4 unused clear interrupt
87 6 channel/gain queue address
88 7 channel/gain queue data
96 #define DAS16M1_AI 0 /* 16-bit wide register */
97 #define AI_CHAN(x) ((x) & 0xf)
99 #define EXT_TRIG_BIT 0x1
102 #define DAS16M1_DIO 3
103 #define DAS16M1_CLEAR_INTR 4
104 #define DAS16M1_INTR_CONTROL 5
105 #define EXT_PACER 0x2
106 #define INT_PACER 0x3
107 #define PACER_MASK 0x3
109 #define DAS16M1_QUEUE_ADDR 6
110 #define DAS16M1_QUEUE_DATA 7
111 #define Q_CHAN(x) ((x) & 0x7)
112 #define Q_RANGE(x) (((x) & 0xf) << 4)
113 #define UNIPOLAR 0x40
114 #define DAS16M1_8254_FIRST 0x8
115 #define DAS16M1_8254_FIRST_CNTRL 0xb
116 #define TOTAL_CLEAR 0x30
117 #define DAS16M1_8254_SECOND 0xc
118 #define DAS16M1_82C55 0x400
119 #define DAS16M1_8254_THIRD 0x404
121 static const struct comedi_lrange range_das16m1 = { 9,
135 static int das16m1_do_wbits(struct comedi_device *dev, struct comedi_subdevice *s,
136 struct comedi_insn *insn, unsigned int *data);
137 static int das16m1_di_rbits(struct comedi_device *dev, struct comedi_subdevice *s,
138 struct comedi_insn *insn, unsigned int *data);
139 static int das16m1_ai_rinsn(struct comedi_device *dev, struct comedi_subdevice *s,
140 struct comedi_insn *insn, unsigned int *data);
142 static int das16m1_cmd_test(struct comedi_device *dev, struct comedi_subdevice *s,
143 struct comedi_cmd *cmd);
144 static int das16m1_cmd_exec(struct comedi_device *dev, struct comedi_subdevice *s);
145 static int das16m1_cancel(struct comedi_device *dev, struct comedi_subdevice *s);
147 static int das16m1_poll(struct comedi_device *dev, struct comedi_subdevice *s);
148 static irqreturn_t das16m1_interrupt(int irq, void *d);
149 static void das16m1_handler(struct comedi_device *dev, unsigned int status);
151 static unsigned int das16m1_set_pacer(struct comedi_device *dev, unsigned int ns,
154 static int das16m1_irq_bits(unsigned int irq);
156 struct das16m1_board {
158 unsigned int ai_speed;
161 static const struct das16m1_board das16m1_boards[] = {
163 .name = "cio-das16/m1", /* CIO-DAS16_M1.pdf */
164 .ai_speed = 1000, /* 1MHz max speed */
168 static int das16m1_attach(struct comedi_device *dev, struct comedi_devconfig *it);
169 static int das16m1_detach(struct comedi_device *dev);
170 static struct comedi_driver driver_das16m1 = {
171 .driver_name = "das16m1",
172 .module = THIS_MODULE,
173 .attach = das16m1_attach,
174 .detach = das16m1_detach,
175 .board_name = &das16m1_boards[0].name,
176 .num_names = ARRAY_SIZE(das16m1_boards),
177 .offset = sizeof(das16m1_boards[0]),
180 struct das16m1_private_struct {
181 unsigned int control_state;
182 volatile unsigned int adc_count; /* number of samples completed */
183 /* initial value in lower half of hardware conversion counter,
184 * needed to keep track of whether new count has been loaded into
185 * counter yet (loaded by first sample conversion) */
186 u16 initial_hw_count;
187 short ai_buffer[FIFO_SIZE];
188 unsigned int do_bits; /* saves status of digital output bits */
189 unsigned int divisor1; /* divides master clock to obtain conversion speed */
190 unsigned int divisor2; /* divides master clock to obtain conversion speed */
192 #define devpriv ((struct das16m1_private_struct *)(dev->private))
193 #define thisboard ((const struct das16m1_board *)(dev->board_ptr))
195 COMEDI_INITCLEANUP(driver_das16m1);
197 static inline short munge_sample(short data)
199 return (data >> 4) & 0xfff;
202 static int das16m1_cmd_test(struct comedi_device *dev, struct comedi_subdevice *s,
203 struct comedi_cmd *cmd)
205 unsigned int err = 0, tmp, i;
207 /* make sure triggers are valid */
208 tmp = cmd->start_src;
209 cmd->start_src &= TRIG_NOW | TRIG_EXT;
210 if (!cmd->start_src || tmp != cmd->start_src)
213 tmp = cmd->scan_begin_src;
214 cmd->scan_begin_src &= TRIG_FOLLOW;
215 if (!cmd->scan_begin_src || tmp != cmd->scan_begin_src)
218 tmp = cmd->convert_src;
219 cmd->convert_src &= TRIG_TIMER | TRIG_EXT;
220 if (!cmd->convert_src || tmp != cmd->convert_src)
223 tmp = cmd->scan_end_src;
224 cmd->scan_end_src &= TRIG_COUNT;
225 if (!cmd->scan_end_src || tmp != cmd->scan_end_src)
229 cmd->stop_src &= TRIG_COUNT | TRIG_NONE;
230 if (!cmd->stop_src || tmp != cmd->stop_src)
236 /* step 2: make sure trigger sources are unique and mutually compatible */
237 if (cmd->stop_src != TRIG_COUNT && cmd->stop_src != TRIG_NONE)
239 if (cmd->start_src != TRIG_NOW && cmd->start_src != TRIG_EXT)
241 if (cmd->convert_src != TRIG_TIMER && cmd->convert_src != TRIG_EXT)
247 /* step 3: make sure arguments are trivially compatible */
248 if (cmd->start_arg != 0) {
253 if (cmd->scan_begin_src == TRIG_FOLLOW) {
254 /* internal trigger */
255 if (cmd->scan_begin_arg != 0) {
256 cmd->scan_begin_arg = 0;
261 if (cmd->convert_src == TRIG_TIMER) {
262 if (cmd->convert_arg < thisboard->ai_speed) {
263 cmd->convert_arg = thisboard->ai_speed;
268 if (cmd->scan_end_arg != cmd->chanlist_len) {
269 cmd->scan_end_arg = cmd->chanlist_len;
273 if (cmd->stop_src == TRIG_COUNT) {
274 /* any count is allowed */
277 if (cmd->stop_arg != 0) {
286 /* step 4: fix up arguments */
288 if (cmd->convert_src == TRIG_TIMER) {
289 tmp = cmd->convert_arg;
290 /* calculate counter values that give desired timing */
291 i8253_cascade_ns_to_timer_2div(DAS16M1_XTAL,
292 &(devpriv->divisor1), &(devpriv->divisor2),
293 &(cmd->convert_arg), cmd->flags & TRIG_ROUND_MASK);
294 if (tmp != cmd->convert_arg)
301 /* check chanlist against board's peculiarities */
302 if (cmd->chanlist && cmd->chanlist_len > 1) {
303 for (i = 0; i < cmd->chanlist_len; i++) {
304 /* even/odd channels must go into even/odd queue addresses */
305 if ((i % 2) != (CR_CHAN(cmd->chanlist[i]) % 2)) {
306 comedi_error(dev, "bad chanlist:\n"
307 " even/odd channels must go have even/odd chanlist indices");
311 if ((cmd->chanlist_len % 2) != 0) {
313 "chanlist must be of even length or length 1");
324 static int das16m1_cmd_exec(struct comedi_device *dev, struct comedi_subdevice *s)
326 struct comedi_async *async = s->async;
327 struct comedi_cmd *cmd = &async->cmd;
328 unsigned int byte, i;
331 comedi_error(dev, "irq required to execute comedi_cmd");
335 /* disable interrupts and internal pacer */
336 devpriv->control_state &= ~INTE & ~PACER_MASK;
337 outb(devpriv->control_state, dev->iobase + DAS16M1_INTR_CONTROL);
339 /* set software count */
340 devpriv->adc_count = 0;
341 /* Initialize lower half of hardware counter, used to determine how
342 * many samples are in fifo. Value doesn't actually load into counter
343 * until counter's next clock (the next a/d conversion) */
344 i8254_load(dev->iobase + DAS16M1_8254_FIRST, 0, 1, 0, 2);
345 /* remember current reading of counter so we know when counter has
346 * actually been loaded */
347 devpriv->initial_hw_count =
348 i8254_read(dev->iobase + DAS16M1_8254_FIRST, 0, 1);
349 /* setup channel/gain queue */
350 for (i = 0; i < cmd->chanlist_len; i++) {
351 outb(i, dev->iobase + DAS16M1_QUEUE_ADDR);
352 byte = Q_CHAN(CR_CHAN(cmd->
353 chanlist[i])) | Q_RANGE(CR_RANGE(cmd->
355 outb(byte, dev->iobase + DAS16M1_QUEUE_DATA);
358 /* set counter mode and counts */
360 das16m1_set_pacer(dev, cmd->convert_arg,
361 cmd->flags & TRIG_ROUND_MASK);
363 /* set control & status register */
365 /* if we are using external start trigger (also board dislikes having
366 * both start and conversion triggers external simultaneously) */
367 if (cmd->start_src == TRIG_EXT && cmd->convert_src != TRIG_EXT) {
368 byte |= EXT_TRIG_BIT;
370 outb(byte, dev->iobase + DAS16M1_CS);
371 /* clear interrupt bit */
372 outb(0, dev->iobase + DAS16M1_CLEAR_INTR);
374 /* enable interrupts and internal pacer */
375 devpriv->control_state &= ~PACER_MASK;
376 if (cmd->convert_src == TRIG_TIMER) {
377 devpriv->control_state |= INT_PACER;
379 devpriv->control_state |= EXT_PACER;
381 devpriv->control_state |= INTE;
382 outb(devpriv->control_state, dev->iobase + DAS16M1_INTR_CONTROL);
387 static int das16m1_cancel(struct comedi_device *dev, struct comedi_subdevice *s)
389 devpriv->control_state &= ~INTE & ~PACER_MASK;
390 outb(devpriv->control_state, dev->iobase + DAS16M1_INTR_CONTROL);
395 static int das16m1_ai_rinsn(struct comedi_device *dev, struct comedi_subdevice *s,
396 struct comedi_insn *insn, unsigned int *data)
400 const int timeout = 1000;
402 /* disable interrupts and internal pacer */
403 devpriv->control_state &= ~INTE & ~PACER_MASK;
404 outb(devpriv->control_state, dev->iobase + DAS16M1_INTR_CONTROL);
406 /* setup channel/gain queue */
407 outb(0, dev->iobase + DAS16M1_QUEUE_ADDR);
408 byte = Q_CHAN(CR_CHAN(insn->chanspec)) | Q_RANGE(CR_RANGE(insn->
410 outb(byte, dev->iobase + DAS16M1_QUEUE_DATA);
412 for (n = 0; n < insn->n; n++) {
413 /* clear IRQDATA bit */
414 outb(0, dev->iobase + DAS16M1_CLEAR_INTR);
415 /* trigger conversion */
416 outb(0, dev->iobase);
418 for (i = 0; i < timeout; i++) {
419 if (inb(dev->iobase + DAS16M1_CS) & IRQDATA)
423 comedi_error(dev, "timeout");
426 data[n] = munge_sample(inw(dev->iobase));
432 static int das16m1_di_rbits(struct comedi_device *dev, struct comedi_subdevice *s,
433 struct comedi_insn *insn, unsigned int *data)
437 bits = inb(dev->iobase + DAS16M1_DIO) & 0xf;
444 static int das16m1_do_wbits(struct comedi_device *dev, struct comedi_subdevice *s,
445 struct comedi_insn *insn, unsigned int *data)
449 /* only set bits that have been masked */
451 wbits = devpriv->do_bits;
452 /* zero bits that have been masked */
454 /* set masked bits */
455 wbits |= data[0] & data[1];
456 devpriv->do_bits = wbits;
459 outb(devpriv->do_bits, dev->iobase + DAS16M1_DIO);
464 static int das16m1_poll(struct comedi_device *dev, struct comedi_subdevice *s)
469 /* prevent race with interrupt handler */
470 spin_lock_irqsave(&dev->spinlock, flags);
471 status = inb(dev->iobase + DAS16M1_CS);
472 das16m1_handler(dev, status);
473 spin_unlock_irqrestore(&dev->spinlock, flags);
475 return s->async->buf_write_count - s->async->buf_read_count;
478 static irqreturn_t das16m1_interrupt(int irq, void *d)
481 struct comedi_device *dev = d;
483 if (dev->attached == 0) {
484 comedi_error(dev, "premature interrupt");
487 /* prevent race with comedi_poll() */
488 spin_lock(&dev->spinlock);
490 status = inb(dev->iobase + DAS16M1_CS);
492 if ((status & (IRQDATA | OVRUN)) == 0) {
493 comedi_error(dev, "spurious interrupt");
494 spin_unlock(&dev->spinlock);
498 das16m1_handler(dev, status);
500 /* clear interrupt */
501 outb(0, dev->iobase + DAS16M1_CLEAR_INTR);
503 spin_unlock(&dev->spinlock);
507 static void munge_sample_array(short *array, unsigned int num_elements)
511 for (i = 0; i < num_elements; i++) {
512 array[i] = munge_sample(array[i]);
516 static void das16m1_handler(struct comedi_device *dev, unsigned int status)
518 struct comedi_subdevice *s;
519 struct comedi_async *async;
520 struct comedi_cmd *cmd;
524 s = dev->read_subdev;
529 /* figure out how many samples are in fifo */
530 hw_counter = i8254_read(dev->iobase + DAS16M1_8254_FIRST, 0, 1);
531 /* make sure hardware counter reading is not bogus due to initial value
532 * not having been loaded yet */
533 if (devpriv->adc_count == 0 && hw_counter == devpriv->initial_hw_count) {
536 /* The calculation of num_samples looks odd, but it uses the following facts.
537 * 16 bit hardware counter is initialized with value of zero (which really
538 * means 0x1000). The counter decrements by one on each conversion
539 * (when the counter decrements from zero it goes to 0xffff). num_samples
540 * is a 16 bit variable, so it will roll over in a similar fashion to the
541 * hardware counter. Work it out, and this is what you get. */
542 num_samples = -hw_counter - devpriv->adc_count;
544 /* check if we only need some of the points */
545 if (cmd->stop_src == TRIG_COUNT) {
546 if (num_samples > cmd->stop_arg * cmd->chanlist_len)
547 num_samples = cmd->stop_arg * cmd->chanlist_len;
549 /* make sure we dont try to get too many points if fifo has overrun */
550 if (num_samples > FIFO_SIZE)
551 num_samples = FIFO_SIZE;
552 insw(dev->iobase, devpriv->ai_buffer, num_samples);
553 munge_sample_array(devpriv->ai_buffer, num_samples);
554 cfc_write_array_to_buffer(s, devpriv->ai_buffer,
555 num_samples * sizeof(short));
556 devpriv->adc_count += num_samples;
558 if (cmd->stop_src == TRIG_COUNT) {
559 if (devpriv->adc_count >= cmd->stop_arg * cmd->chanlist_len) { /* end of acquisition */
560 das16m1_cancel(dev, s);
561 async->events |= COMEDI_CB_EOA;
565 /* this probably won't catch overruns since the card doesn't generate
566 * overrun interrupts, but we might as well try */
567 if (status & OVRUN) {
568 das16m1_cancel(dev, s);
569 async->events |= COMEDI_CB_EOA | COMEDI_CB_ERROR;
570 comedi_error(dev, "fifo overflow");
573 comedi_event(dev, s);
577 /* This function takes a time in nanoseconds and sets the *
578 * 2 pacer clocks to the closest frequency possible. It also *
579 * returns the actual sampling period. */
580 static unsigned int das16m1_set_pacer(struct comedi_device *dev, unsigned int ns,
583 i8253_cascade_ns_to_timer_2div(DAS16M1_XTAL, &(devpriv->divisor1),
584 &(devpriv->divisor2), &ns, rounding_flags & TRIG_ROUND_MASK);
586 /* Write the values of ctr1 and ctr2 into counters 1 and 2 */
587 i8254_load(dev->iobase + DAS16M1_8254_SECOND, 0, 1, devpriv->divisor1,
589 i8254_load(dev->iobase + DAS16M1_8254_SECOND, 0, 2, devpriv->divisor2,
595 static int das16m1_irq_bits(unsigned int irq)
637 static int das16m1_attach(struct comedi_device *dev, struct comedi_devconfig *it)
639 struct comedi_subdevice *s;
642 unsigned long iobase;
644 iobase = it->options[0];
646 printk("comedi%d: das16m1:", dev->minor);
648 ret = alloc_private(dev, sizeof(struct das16m1_private_struct));
652 dev->board_name = thisboard->name;
654 printk(" io 0x%lx-0x%lx 0x%lx-0x%lx",
655 iobase, iobase + DAS16M1_SIZE,
656 iobase + DAS16M1_82C55, iobase + DAS16M1_82C55 + DAS16M1_SIZE2);
657 if (!request_region(iobase, DAS16M1_SIZE, driver_das16m1.driver_name)) {
658 printk(" I/O port conflict\n");
661 if (!request_region(iobase + DAS16M1_82C55, DAS16M1_SIZE2,
662 driver_das16m1.driver_name)) {
663 release_region(iobase, DAS16M1_SIZE);
664 printk(" I/O port conflict\n");
667 dev->iobase = iobase;
669 /* now for the irq */
670 irq = it->options[1];
671 /* make sure it is valid */
672 if (das16m1_irq_bits(irq) >= 0) {
673 ret = request_irq(irq, das16m1_interrupt, 0,
674 driver_das16m1.driver_name, dev);
676 printk(", irq unavailable\n");
680 printk(", irq %u\n", irq);
681 } else if (irq == 0) {
682 printk(", no irq\n");
684 printk(", invalid irq\n"
685 " valid irqs are 2, 3, 5, 7, 10, 11, 12, or 15\n");
689 ret = alloc_subdevices(dev, 4);
693 s = dev->subdevices + 0;
694 dev->read_subdev = s;
696 s->type = COMEDI_SUBD_AI;
697 s->subdev_flags = SDF_READABLE | SDF_CMD_READ;
699 s->subdev_flags = SDF_DIFF;
700 s->len_chanlist = 256;
701 s->maxdata = (1 << 12) - 1;
702 s->range_table = &range_das16m1;
703 s->insn_read = das16m1_ai_rinsn;
704 s->do_cmdtest = das16m1_cmd_test;
705 s->do_cmd = das16m1_cmd_exec;
706 s->cancel = das16m1_cancel;
707 s->poll = das16m1_poll;
709 s = dev->subdevices + 1;
711 s->type = COMEDI_SUBD_DI;
712 s->subdev_flags = SDF_READABLE;
715 s->range_table = &range_digital;
716 s->insn_bits = das16m1_di_rbits;
718 s = dev->subdevices + 2;
720 s->type = COMEDI_SUBD_DO;
721 s->subdev_flags = SDF_WRITABLE | SDF_READABLE;
724 s->range_table = &range_digital;
725 s->insn_bits = das16m1_do_wbits;
727 s = dev->subdevices + 3;
729 subdev_8255_init(dev, s, NULL, dev->iobase + DAS16M1_82C55);
731 /* disable upper half of hardware conversion counter so it doesn't mess with us */
732 outb(TOTAL_CLEAR, dev->iobase + DAS16M1_8254_FIRST_CNTRL);
734 /* initialize digital output lines */
735 outb(devpriv->do_bits, dev->iobase + DAS16M1_DIO);
737 /* set the interrupt level */
739 devpriv->control_state = das16m1_irq_bits(dev->irq);
741 devpriv->control_state = 0;
742 outb(devpriv->control_state, dev->iobase + DAS16M1_INTR_CONTROL);
747 static int das16m1_detach(struct comedi_device *dev)
749 printk("comedi%d: das16m1: remove\n", dev->minor);
751 /* das16m1_reset(dev); */
754 subdev_8255_cleanup(dev, dev->subdevices + 3);
757 free_irq(dev->irq, dev);
760 release_region(dev->iobase, DAS16M1_SIZE);
761 release_region(dev->iobase + DAS16M1_82C55, DAS16M1_SIZE2);