3 * Data Translation DT3000 series driver
5 * COMEDI - Linux Control and Measurement Device Interface
6 * Copyright (C) 1999 David A. Schleef <ds@schleef.org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
21 * Description: Data Translation DT3000 series
22 * Devices: [Data Translation] DT3001 (dt3000), DT3001-PGL, DT3002, DT3003,
23 * DT3003-PGL, DT3004, DT3005, DT3004-200
25 * Updated: Mon, 14 Apr 2008 15:41:24 +0100
28 * Configuration Options: not applicable, uses PCI auto config
30 * There is code to support AI commands, but it may not work.
32 * AO commands are not supported.
36 * The DT3000 series is Data Translation's attempt to make a PCI
37 * data acquisition board. The design of this series is very nice,
38 * since each board has an on-board DSP (Texas Instruments TMS320C52).
39 * However, a few details are a little annoying. The boards lack
40 * bus-mastering DMA, which eliminates them from serious work.
41 * They also are not capable of autocalibration, which is a common
42 * feature in modern hardware. The default firmware is pretty bad,
43 * making it nearly impossible to write an RT compatible driver.
44 * It would make an interesting project to write a decent firmware
47 * Data Translation originally wanted an NDA for the documentation
48 * for the 3k series. However, if you ask nicely, they might send
49 * you the docs without one, also.
52 #include <linux/module.h>
53 #include <linux/delay.h>
54 #include <linux/interrupt.h>
56 #include "../comedi_pci.h"
59 * PCI BAR0 - dual-ported RAM location definitions (dev->mmio)
61 #define DPR_DAC_BUFFER (4 * 0x000)
62 #define DPR_ADC_BUFFER (4 * 0x800)
63 #define DPR_COMMAND (4 * 0xfd3)
64 #define DPR_SUBSYS (4 * 0xfd3)
65 #define DPR_SUBSYS_AI 0
66 #define DPR_SUBSYS_AO 1
67 #define DPR_SUBSYS_DIN 2
68 #define DPR_SUBSYS_DOUT 3
69 #define DPR_SUBSYS_MEM 4
70 #define DPR_SUBSYS_CT 5
71 #define DPR_ENCODE (4 * 0xfd4)
72 #define DPR_PARAMS(x) (4 * (0xfd5 + (x)))
73 #define DPR_TICK_REG_LO (4 * 0xff5)
74 #define DPR_TICK_REG_HI (4 * 0xff6)
75 #define DPR_DA_BUF_FRONT (4 * 0xff7)
76 #define DPR_DA_BUF_REAR (4 * 0xff8)
77 #define DPR_AD_BUF_FRONT (4 * 0xff9)
78 #define DPR_AD_BUF_REAR (4 * 0xffa)
79 #define DPR_INT_MASK (4 * 0xffb)
80 #define DPR_INTR_FLAG (4 * 0xffc)
81 #define DPR_INTR_CMDONE BIT(7)
82 #define DPR_INTR_CTDONE BIT(6)
83 #define DPR_INTR_DAHWERR BIT(5)
84 #define DPR_INTR_DASWERR BIT(4)
85 #define DPR_INTR_DAEMPTY BIT(3)
86 #define DPR_INTR_ADHWERR BIT(2)
87 #define DPR_INTR_ADSWERR BIT(1)
88 #define DPR_INTR_ADFULL BIT(0)
89 #define DPR_RESPONSE_MBX (4 * 0xffe)
90 #define DPR_CMD_MBX (4 * 0xfff)
91 #define DPR_CMD_COMPLETION(x) ((x) << 8)
92 #define DPR_CMD_NOTPROCESSED DPR_CMD_COMPLETION(0x00)
93 #define DPR_CMD_NOERROR DPR_CMD_COMPLETION(0x55)
94 #define DPR_CMD_ERROR DPR_CMD_COMPLETION(0xaa)
95 #define DPR_CMD_NOTSUPPORTED DPR_CMD_COMPLETION(0xff)
96 #define DPR_CMD_COMPLETION_MASK DPR_CMD_COMPLETION(0xff)
97 #define DPR_CMD(x) ((x) << 0)
98 #define DPR_CMD_GETBRDINFO DPR_CMD(0)
99 #define DPR_CMD_CONFIG DPR_CMD(1)
100 #define DPR_CMD_GETCONFIG DPR_CMD(2)
101 #define DPR_CMD_START DPR_CMD(3)
102 #define DPR_CMD_STOP DPR_CMD(4)
103 #define DPR_CMD_READSINGLE DPR_CMD(5)
104 #define DPR_CMD_WRITESINGLE DPR_CMD(6)
105 #define DPR_CMD_CALCCLOCK DPR_CMD(7)
106 #define DPR_CMD_READEVENTS DPR_CMD(8)
107 #define DPR_CMD_WRITECTCTRL DPR_CMD(16)
108 #define DPR_CMD_READCTCTRL DPR_CMD(17)
109 #define DPR_CMD_WRITECT DPR_CMD(18)
110 #define DPR_CMD_READCT DPR_CMD(19)
111 #define DPR_CMD_WRITEDATA DPR_CMD(32)
112 #define DPR_CMD_READDATA DPR_CMD(33)
113 #define DPR_CMD_WRITEIO DPR_CMD(34)
114 #define DPR_CMD_READIO DPR_CMD(35)
115 #define DPR_CMD_WRITECODE DPR_CMD(36)
116 #define DPR_CMD_READCODE DPR_CMD(37)
117 #define DPR_CMD_EXECUTE DPR_CMD(38)
118 #define DPR_CMD_HALT DPR_CMD(48)
119 #define DPR_CMD_MASK DPR_CMD(0xff)
121 #define DPR_PARAM5_AD_TRIG(x) (((x) & 0x7) << 2)
122 #define DPR_PARAM5_AD_TRIG_INT DPR_PARAM5_AD_TRIG(0)
123 #define DPR_PARAM5_AD_TRIG_EXT DPR_PARAM5_AD_TRIG(1)
124 #define DPR_PARAM5_AD_TRIG_INT_RETRIG DPR_PARAM5_AD_TRIG(2)
125 #define DPR_PARAM5_AD_TRIG_EXT_RETRIG DPR_PARAM5_AD_TRIG(3)
126 #define DPR_PARAM5_AD_TRIG_INT_RETRIG2 DPR_PARAM5_AD_TRIG(4)
128 #define DPR_PARAM6_AD_DIFF BIT(0)
130 #define DPR_AI_FIFO_DEPTH 2003
131 #define DPR_AO_FIFO_DEPTH 2048
133 #define DPR_EXTERNAL_CLOCK 1
134 #define DPR_RISING_EDGE 2
136 #define DPR_TMODE_MASK 0x1c
138 #define DPR_CMD_TIMEOUT 100
140 static const struct comedi_lrange range_dt3000_ai = {
149 static const struct comedi_lrange range_dt3000_ai_pgl = {
168 struct dt3k_boardtype {
172 const struct comedi_lrange *adrange;
173 unsigned int ai_is_16bit:1;
174 unsigned int has_ao:1;
177 static const struct dt3k_boardtype dt3k_boardtypes[] = {
181 .adrange = &range_dt3000_ai,
185 [BOARD_DT3001_PGL] = {
186 .name = "dt3001-pgl",
188 .adrange = &range_dt3000_ai_pgl,
195 .adrange = &range_dt3000_ai,
201 .adrange = &range_dt3000_ai,
205 [BOARD_DT3003_PGL] = {
206 .name = "dt3003-pgl",
208 .adrange = &range_dt3000_ai_pgl,
215 .adrange = &range_dt3000_ai,
221 .name = "dt3005", /* a.k.a. 3004-200 */
223 .adrange = &range_dt3000_ai,
230 struct dt3k_private {
232 unsigned int ai_front;
233 unsigned int ai_rear;
236 static void dt3k_send_cmd(struct comedi_device *dev, unsigned int cmd)
239 unsigned int status = 0;
241 writew(cmd, dev->mmio + DPR_CMD_MBX);
243 for (i = 0; i < DPR_CMD_TIMEOUT; i++) {
244 status = readw(dev->mmio + DPR_CMD_MBX);
245 status &= DPR_CMD_COMPLETION_MASK;
246 if (status != DPR_CMD_NOTPROCESSED)
251 if (status != DPR_CMD_NOERROR)
252 dev_dbg(dev->class_dev, "%s: timeout/error status=0x%04x\n",
256 static unsigned int dt3k_readsingle(struct comedi_device *dev,
257 unsigned int subsys, unsigned int chan,
260 writew(subsys, dev->mmio + DPR_SUBSYS);
262 writew(chan, dev->mmio + DPR_PARAMS(0));
263 writew(gain, dev->mmio + DPR_PARAMS(1));
265 dt3k_send_cmd(dev, DPR_CMD_READSINGLE);
267 return readw(dev->mmio + DPR_PARAMS(2));
270 static void dt3k_writesingle(struct comedi_device *dev, unsigned int subsys,
271 unsigned int chan, unsigned int data)
273 writew(subsys, dev->mmio + DPR_SUBSYS);
275 writew(chan, dev->mmio + DPR_PARAMS(0));
276 writew(0, dev->mmio + DPR_PARAMS(1));
277 writew(data, dev->mmio + DPR_PARAMS(2));
279 dt3k_send_cmd(dev, DPR_CMD_WRITESINGLE);
282 static void dt3k_ai_empty_fifo(struct comedi_device *dev,
283 struct comedi_subdevice *s)
285 struct dt3k_private *devpriv = dev->private;
292 front = readw(dev->mmio + DPR_AD_BUF_FRONT);
293 count = front - devpriv->ai_front;
295 count += DPR_AI_FIFO_DEPTH;
297 rear = devpriv->ai_rear;
299 for (i = 0; i < count; i++) {
300 data = readw(dev->mmio + DPR_ADC_BUFFER + rear);
301 comedi_buf_write_samples(s, &data, 1);
303 if (rear >= DPR_AI_FIFO_DEPTH)
307 devpriv->ai_rear = rear;
308 writew(rear, dev->mmio + DPR_AD_BUF_REAR);
311 static int dt3k_ai_cancel(struct comedi_device *dev,
312 struct comedi_subdevice *s)
314 writew(DPR_SUBSYS_AI, dev->mmio + DPR_SUBSYS);
315 dt3k_send_cmd(dev, DPR_CMD_STOP);
317 writew(0, dev->mmio + DPR_INT_MASK);
322 static int debug_n_ints;
324 /* FIXME! Assumes shared interrupt is for this card. */
325 /* What's this debug_n_ints stuff? Obviously needs some work... */
326 static irqreturn_t dt3k_interrupt(int irq, void *d)
328 struct comedi_device *dev = d;
329 struct comedi_subdevice *s = dev->read_subdev;
335 status = readw(dev->mmio + DPR_INTR_FLAG);
337 if (status & DPR_INTR_ADFULL)
338 dt3k_ai_empty_fifo(dev, s);
340 if (status & (DPR_INTR_ADSWERR | DPR_INTR_ADHWERR))
341 s->async->events |= COMEDI_CB_ERROR;
344 if (debug_n_ints >= 10)
345 s->async->events |= COMEDI_CB_EOA;
347 comedi_handle_events(dev, s);
351 static int dt3k_ns_to_timer(unsigned int timer_base, unsigned int *nanosec,
354 int divider, base, prescale;
356 /* This function needs improvment */
357 /* Don't know if divider==0 works. */
359 for (prescale = 0; prescale < 16; prescale++) {
360 base = timer_base * (prescale + 1);
361 switch (flags & CMDF_ROUND_MASK) {
362 case CMDF_ROUND_NEAREST:
364 divider = (*nanosec + base / 2) / base;
366 case CMDF_ROUND_DOWN:
367 divider = (*nanosec) / base;
370 divider = (*nanosec) / base;
373 if (divider < 65536) {
374 *nanosec = divider * base;
375 return (prescale << 16) | (divider);
380 base = timer_base * (1 << prescale);
382 *nanosec = divider * base;
383 return (prescale << 16) | (divider);
386 static int dt3k_ai_cmdtest(struct comedi_device *dev,
387 struct comedi_subdevice *s, struct comedi_cmd *cmd)
389 const struct dt3k_boardtype *board = dev->board_ptr;
393 /* Step 1 : check if triggers are trivially valid */
395 err |= comedi_check_trigger_src(&cmd->start_src, TRIG_NOW);
396 err |= comedi_check_trigger_src(&cmd->scan_begin_src, TRIG_TIMER);
397 err |= comedi_check_trigger_src(&cmd->convert_src, TRIG_TIMER);
398 err |= comedi_check_trigger_src(&cmd->scan_end_src, TRIG_COUNT);
399 err |= comedi_check_trigger_src(&cmd->stop_src, TRIG_COUNT);
404 /* Step 2a : make sure trigger sources are unique */
405 /* Step 2b : and mutually compatible */
407 /* Step 3: check if arguments are trivially valid */
409 err |= comedi_check_trigger_arg_is(&cmd->start_arg, 0);
411 if (cmd->scan_begin_src == TRIG_TIMER) {
412 err |= comedi_check_trigger_arg_min(&cmd->scan_begin_arg,
414 err |= comedi_check_trigger_arg_max(&cmd->scan_begin_arg,
418 if (cmd->convert_src == TRIG_TIMER) {
419 err |= comedi_check_trigger_arg_min(&cmd->convert_arg,
421 err |= comedi_check_trigger_arg_max(&cmd->convert_arg,
425 err |= comedi_check_trigger_arg_is(&cmd->scan_end_arg,
428 if (cmd->stop_src == TRIG_COUNT)
429 err |= comedi_check_trigger_arg_max(&cmd->stop_arg, 0x00ffffff);
431 err |= comedi_check_trigger_arg_is(&cmd->stop_arg, 0);
436 /* step 4: fix up any arguments */
438 if (cmd->scan_begin_src == TRIG_TIMER) {
439 arg = cmd->scan_begin_arg;
440 dt3k_ns_to_timer(100, &arg, cmd->flags);
441 err |= comedi_check_trigger_arg_is(&cmd->scan_begin_arg, arg);
444 if (cmd->convert_src == TRIG_TIMER) {
445 arg = cmd->convert_arg;
446 dt3k_ns_to_timer(50, &arg, cmd->flags);
447 err |= comedi_check_trigger_arg_is(&cmd->convert_arg, arg);
449 if (cmd->scan_begin_src == TRIG_TIMER) {
450 arg = cmd->convert_arg * cmd->scan_end_arg;
451 err |= comedi_check_trigger_arg_min(&cmd->
463 static int dt3k_ai_cmd(struct comedi_device *dev, struct comedi_subdevice *s)
465 struct comedi_cmd *cmd = &s->async->cmd;
467 unsigned int chan, range, aref;
468 unsigned int divider;
469 unsigned int tscandiv;
471 for (i = 0; i < cmd->chanlist_len; i++) {
472 chan = CR_CHAN(cmd->chanlist[i]);
473 range = CR_RANGE(cmd->chanlist[i]);
475 writew((range << 6) | chan, dev->mmio + DPR_ADC_BUFFER + i);
477 aref = CR_AREF(cmd->chanlist[0]);
479 writew(cmd->scan_end_arg, dev->mmio + DPR_PARAMS(0));
481 if (cmd->convert_src == TRIG_TIMER) {
482 divider = dt3k_ns_to_timer(50, &cmd->convert_arg, cmd->flags);
483 writew((divider >> 16), dev->mmio + DPR_PARAMS(1));
484 writew((divider & 0xffff), dev->mmio + DPR_PARAMS(2));
487 if (cmd->scan_begin_src == TRIG_TIMER) {
488 tscandiv = dt3k_ns_to_timer(100, &cmd->scan_begin_arg,
490 writew((tscandiv >> 16), dev->mmio + DPR_PARAMS(3));
491 writew((tscandiv & 0xffff), dev->mmio + DPR_PARAMS(4));
494 writew(DPR_PARAM5_AD_TRIG_INT_RETRIG, dev->mmio + DPR_PARAMS(5));
495 writew((aref == AREF_DIFF) ? DPR_PARAM6_AD_DIFF : 0,
496 dev->mmio + DPR_PARAMS(6));
498 writew(DPR_AI_FIFO_DEPTH / 2, dev->mmio + DPR_PARAMS(7));
500 writew(DPR_SUBSYS_AI, dev->mmio + DPR_SUBSYS);
501 dt3k_send_cmd(dev, DPR_CMD_CONFIG);
503 writew(DPR_INTR_ADFULL | DPR_INTR_ADSWERR | DPR_INTR_ADHWERR,
504 dev->mmio + DPR_INT_MASK);
508 writew(DPR_SUBSYS_AI, dev->mmio + DPR_SUBSYS);
509 dt3k_send_cmd(dev, DPR_CMD_START);
514 static int dt3k_ai_insn_read(struct comedi_device *dev,
515 struct comedi_subdevice *s,
516 struct comedi_insn *insn,
520 unsigned int chan, gain, aref;
522 chan = CR_CHAN(insn->chanspec);
523 gain = CR_RANGE(insn->chanspec);
524 /* XXX docs don't explain how to select aref */
525 aref = CR_AREF(insn->chanspec);
527 for (i = 0; i < insn->n; i++)
528 data[i] = dt3k_readsingle(dev, DPR_SUBSYS_AI, chan, gain);
533 static int dt3k_ao_insn_write(struct comedi_device *dev,
534 struct comedi_subdevice *s,
535 struct comedi_insn *insn,
538 unsigned int chan = CR_CHAN(insn->chanspec);
539 unsigned int val = s->readback[chan];
542 for (i = 0; i < insn->n; i++) {
544 dt3k_writesingle(dev, DPR_SUBSYS_AO, chan, val);
546 s->readback[chan] = val;
551 static void dt3k_dio_config(struct comedi_device *dev, int bits)
554 writew(DPR_SUBSYS_DOUT, dev->mmio + DPR_SUBSYS);
556 writew(bits, dev->mmio + DPR_PARAMS(0));
558 /* XXX write 0 to DPR_PARAMS(1) and DPR_PARAMS(2) ? */
560 dt3k_send_cmd(dev, DPR_CMD_CONFIG);
563 static int dt3k_dio_insn_config(struct comedi_device *dev,
564 struct comedi_subdevice *s,
565 struct comedi_insn *insn,
568 unsigned int chan = CR_CHAN(insn->chanspec);
577 ret = comedi_dio_insn_config(dev, s, insn, data, mask);
581 dt3k_dio_config(dev, (s->io_bits & 0x01) | ((s->io_bits & 0x10) >> 3));
586 static int dt3k_dio_insn_bits(struct comedi_device *dev,
587 struct comedi_subdevice *s,
588 struct comedi_insn *insn,
591 if (comedi_dio_update_state(s, data))
592 dt3k_writesingle(dev, DPR_SUBSYS_DOUT, 0, s->state);
594 data[1] = dt3k_readsingle(dev, DPR_SUBSYS_DIN, 0, 0);
599 static int dt3k_mem_insn_read(struct comedi_device *dev,
600 struct comedi_subdevice *s,
601 struct comedi_insn *insn,
604 unsigned int addr = CR_CHAN(insn->chanspec);
607 for (i = 0; i < insn->n; i++) {
608 writew(DPR_SUBSYS_MEM, dev->mmio + DPR_SUBSYS);
609 writew(addr, dev->mmio + DPR_PARAMS(0));
610 writew(1, dev->mmio + DPR_PARAMS(1));
612 dt3k_send_cmd(dev, DPR_CMD_READCODE);
614 data[i] = readw(dev->mmio + DPR_PARAMS(2));
620 static int dt3000_auto_attach(struct comedi_device *dev,
621 unsigned long context)
623 struct pci_dev *pcidev = comedi_to_pci_dev(dev);
624 const struct dt3k_boardtype *board = NULL;
625 struct dt3k_private *devpriv;
626 struct comedi_subdevice *s;
629 if (context < ARRAY_SIZE(dt3k_boardtypes))
630 board = &dt3k_boardtypes[context];
633 dev->board_ptr = board;
634 dev->board_name = board->name;
636 devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
640 ret = comedi_pci_enable(dev);
644 dev->mmio = pci_ioremap_bar(pcidev, 0);
649 ret = request_irq(pcidev->irq, dt3k_interrupt, IRQF_SHARED,
650 dev->board_name, dev);
652 dev->irq = pcidev->irq;
655 ret = comedi_alloc_subdevices(dev, 4);
659 /* Analog Input subdevice */
660 s = &dev->subdevices[0];
661 s->type = COMEDI_SUBD_AI;
662 s->subdev_flags = SDF_READABLE | SDF_GROUND | SDF_DIFF;
663 s->n_chan = board->adchan;
664 s->maxdata = board->ai_is_16bit ? 0xffff : 0x0fff;
665 s->range_table = &range_dt3000_ai; /* XXX */
666 s->insn_read = dt3k_ai_insn_read;
668 dev->read_subdev = s;
669 s->subdev_flags |= SDF_CMD_READ;
670 s->len_chanlist = 512;
671 s->do_cmd = dt3k_ai_cmd;
672 s->do_cmdtest = dt3k_ai_cmdtest;
673 s->cancel = dt3k_ai_cancel;
676 /* Analog Output subdevice */
677 s = &dev->subdevices[1];
679 s->type = COMEDI_SUBD_AO;
680 s->subdev_flags = SDF_WRITABLE;
683 s->range_table = &range_bipolar10;
684 s->insn_write = dt3k_ao_insn_write;
686 ret = comedi_alloc_subdev_readback(s);
691 s->type = COMEDI_SUBD_UNUSED;
694 /* Digital I/O subdevice */
695 s = &dev->subdevices[2];
696 s->type = COMEDI_SUBD_DIO;
697 s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
700 s->range_table = &range_digital;
701 s->insn_config = dt3k_dio_insn_config;
702 s->insn_bits = dt3k_dio_insn_bits;
704 /* Memory subdevice */
705 s = &dev->subdevices[3];
706 s->type = COMEDI_SUBD_MEMORY;
707 s->subdev_flags = SDF_READABLE;
710 s->range_table = &range_unknown;
711 s->insn_read = dt3k_mem_insn_read;
716 static struct comedi_driver dt3000_driver = {
717 .driver_name = "dt3000",
718 .module = THIS_MODULE,
719 .auto_attach = dt3000_auto_attach,
720 .detach = comedi_pci_detach,
723 static int dt3000_pci_probe(struct pci_dev *dev,
724 const struct pci_device_id *id)
726 return comedi_pci_auto_config(dev, &dt3000_driver, id->driver_data);
729 static const struct pci_device_id dt3000_pci_table[] = {
730 { PCI_VDEVICE(DT, 0x0022), BOARD_DT3001 },
731 { PCI_VDEVICE(DT, 0x0023), BOARD_DT3002 },
732 { PCI_VDEVICE(DT, 0x0024), BOARD_DT3003 },
733 { PCI_VDEVICE(DT, 0x0025), BOARD_DT3004 },
734 { PCI_VDEVICE(DT, 0x0026), BOARD_DT3005 },
735 { PCI_VDEVICE(DT, 0x0027), BOARD_DT3001_PGL },
736 { PCI_VDEVICE(DT, 0x0028), BOARD_DT3003_PGL },
739 MODULE_DEVICE_TABLE(pci, dt3000_pci_table);
741 static struct pci_driver dt3000_pci_driver = {
743 .id_table = dt3000_pci_table,
744 .probe = dt3000_pci_probe,
745 .remove = comedi_pci_auto_unconfig,
747 module_comedi_pci_driver(dt3000_driver, dt3000_pci_driver);
749 MODULE_AUTHOR("Comedi http://www.comedi.org");
750 MODULE_DESCRIPTION("Comedi driver for Data Translation DT3000 series boards");
751 MODULE_LICENSE("GPL");