2 comedi/drivers/cb_pcidas.c
4 Developed by Ivan Martinez and Frank Mori Hess, with valuable help from
5 David Schleef and the rest of the Comedi developers comunity.
7 Copyright (C) 2001-2003 Ivan Martinez <imr@oersted.dtu.dk>
8 Copyright (C) 2001,2002 Frank Mori Hess <fmhess@users.sourceforge.net>
10 COMEDI - Linux Control and Measurement Device Interface
11 Copyright (C) 1997-8 David A. Schleef <ds@schleef.org>
13 This program is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 2 of the License, or
16 (at your option) any later version.
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 ************************************************************************
31 Description: MeasurementComputing PCI-DAS series with the AMCC S5933 PCI controller
32 Author: Ivan Martinez <imr@oersted.dtu.dk>,
33 Frank Mori Hess <fmhess@users.sourceforge.net>
35 Devices: [Measurement Computing] PCI-DAS1602/16 (cb_pcidas),
36 PCI-DAS1602/16jr, PCI-DAS1602/12, PCI-DAS1200, PCI-DAS1200jr,
37 PCI-DAS1000, PCI-DAS1001, PCI_DAS1002
40 There are many reports of the driver being used with most of the
41 supported cards. Despite no detailed log is maintained, it can
42 be said that the driver is quite tested and stable.
44 The boards may be autocalibrated using the comedi_calibrate
47 Configuration options:
48 [0] - PCI bus of device (optional)
49 [1] - PCI slot of device (optional)
50 If bus/slot is not specified, the first supported
51 PCI device found will be used.
53 For commands, the scanned channels must be consecutive
54 (i.e. 4-5-6-7, 2-3-4,...), and must all have the same
61 analog triggering on 1602 series
64 #include "../comedidev.h"
65 #include <linux/delay.h>
66 #include <linux/interrupt.h>
70 #include "amcc_s5933.h"
71 #include "comedi_pci.h"
72 #include "comedi_fc.h"
74 #undef CB_PCIDAS_DEBUG /* disable debugging code */
75 /* #define CB_PCIDAS_DEBUG enable debugging code */
77 /* PCI vendor number of ComputerBoards/MeasurementComputing */
78 #define PCI_VENDOR_ID_CB 0x1307
79 #define TIMER_BASE 100 /* 10MHz master clock */
80 #define AI_BUFFER_SIZE 1024 /* maximum fifo size of any supported board */
81 #define AO_BUFFER_SIZE 1024 /* maximum fifo size of any supported board */
82 #define NUM_CHANNELS_8800 8
83 #define NUM_CHANNELS_7376 1
84 #define NUM_CHANNELS_8402 2
85 #define NUM_CHANNELS_DAC08 1
87 /* PCI-DAS base addresses */
89 /* indices of base address regions */
90 #define S5933_BADRINDEX 0
91 #define CONT_STAT_BADRINDEX 1
92 #define ADC_FIFO_BADRINDEX 2
93 #define PACER_BADRINDEX 3
94 #define AO_BADRINDEX 4
95 /* sizes of io regions */
96 #define CONT_STAT_SIZE 10
97 #define ADC_FIFO_SIZE 4
101 /* Control/Status registers */
102 #define INT_ADCFIFO 0 /* INTERRUPT / ADC FIFO register */
103 #define INT_EOS 0x1 /* interrupt end of scan */
104 #define INT_FHF 0x2 /* interrupt fifo half full */
105 #define INT_FNE 0x3 /* interrupt fifo not empty */
106 #define INT_MASK 0x3 /* mask of interrupt select bits */
107 #define INTE 0x4 /* interrupt enable */
108 #define DAHFIE 0x8 /* dac half full interrupt enable */
109 #define EOAIE 0x10 /* end of aquisition interrupt enable */
110 #define DAHFI 0x20 /* dac half full read status / write interrupt clear */
111 #define EOAI 0x40 /* read end of acq. interrupt status / write clear */
112 #define INT 0x80 /* read interrupt status / write clear */
113 #define EOBI 0x200 /* read end of burst interrupt status */
114 #define ADHFI 0x400 /* read half-full interrupt status */
115 #define ADNEI 0x800 /* read fifo not empty interrupt latch status */
116 #define ADNE 0x1000 /* read, fifo not empty (realtime, not latched) status */
117 #define DAEMIE 0x1000 /* write, dac empty interrupt enable */
118 #define LADFUL 0x2000 /* read fifo overflow / write clear */
119 #define DAEMI 0x4000 /* dac fifo empty interrupt status / write clear */
121 #define ADCMUX_CONT 2 /* ADC CHANNEL MUX AND CONTROL register */
122 #define BEGIN_SCAN(x) ((x) & 0xf)
123 #define END_SCAN(x) (((x) & 0xf) << 4)
124 #define GAIN_BITS(x) (((x) & 0x3) << 8)
125 #define UNIP 0x800 /* Analog front-end unipolar for range */
126 #define SE 0x400 /* Inputs in single-ended mode */
127 #define PACER_MASK 0x3000 /* pacer source bits */
128 #define PACER_INT 0x1000 /* internal pacer */
129 #define PACER_EXT_FALL 0x2000 /* external falling edge */
130 #define PACER_EXT_RISE 0x3000 /* external rising edge */
131 #define EOC 0x4000 /* adc not busy */
133 #define TRIG_CONTSTAT 4 /* TRIGGER CONTROL/STATUS register */
134 #define SW_TRIGGER 0x1 /* software start trigger */
135 #define EXT_TRIGGER 0x2 /* external start trigger */
136 #define ANALOG_TRIGGER 0x3 /* external analog trigger */
137 #define TRIGGER_MASK 0x3 /* mask of bits that determine start trigger */
138 #define TGEN 0x10 /* enable external start trigger */
139 #define BURSTE 0x20 /* burst mode enable */
140 #define XTRCL 0x80 /* clear external trigger */
142 #define CALIBRATION_REG 6 /* CALIBRATION register */
143 #define SELECT_8800_BIT 0x100 /* select 8800 caldac */
144 #define SELECT_TRIMPOT_BIT 0x200 /* select ad7376 trim pot */
145 #define SELECT_DAC08_BIT 0x400 /* select dac08 caldac */
146 #define CAL_SRC_BITS(x) (((x) & 0x7) << 11)
147 #define CAL_EN_BIT 0x4000 /* read calibration source instead of analog input channel 0 */
148 #define SERIAL_DATA_IN_BIT 0x8000 /* serial data stream going to 8800 and 7376 */
150 #define DAC_CSR 0x8 /* dac control and status register */
152 DACEN = 0x2, /* dac enable */
153 DAC_MODE_UPDATE_BOTH = 0x80, /* update both dacs when dac0 is written */
155 static inline unsigned int DAC_RANGE(unsigned int channel, unsigned int range)
157 return (range & 0x3) << (8 + 2 * (channel & 0x1));
160 static inline unsigned int DAC_RANGE_MASK(unsigned int channel)
162 return 0x3 << (8 + 2 * (channel & 0x1));
165 /* bits for 1602 series only */
166 enum dac_csr_bits_1602 {
167 DAC_EMPTY = 0x1, /* dac fifo empty, read, write clear */
168 DAC_START = 0x4, /* start/arm dac fifo operations */
169 DAC_PACER_MASK = 0x18, /* bits that set dac pacer source */
170 DAC_PACER_INT = 0x8, /* dac internal pacing */
171 DAC_PACER_EXT_FALL = 0x10, /* dac external pacing, falling edge */
172 DAC_PACER_EXT_RISE = 0x18, /* dac external pacing, rising edge */
174 static inline unsigned int DAC_CHAN_EN(unsigned int channel)
176 return 1 << (5 + (channel & 0x1)); /* enable channel 0 or 1 */
179 /* analog input fifo */
180 #define ADCDATA 0 /* ADC DATA register */
181 #define ADCFIFOCLR 2 /* ADC FIFO CLEAR */
183 /* pacer, counter, dio registers */
188 /* analog output registers for 100x, 1200 series */
189 static inline unsigned int DAC_DATA_REG(unsigned int channel)
191 return 2 * (channel & 0x1);
194 /* analog output registers for 1602 series*/
195 #define DACDATA 0 /* DAC DATA register */
196 #define DACFIFOCLR 2 /* DAC FIFO CLEAR */
198 /* bit in hexadecimal representation of range index that indicates unipolar input range */
199 #define IS_UNIPOLAR 0x4
200 /* analog input ranges for most boards */
201 static const struct comedi_lrange cb_pcidas_ranges = {
215 /* pci-das1001 input ranges */
216 static const struct comedi_lrange cb_pcidas_alt_ranges = {
230 /* analog output ranges */
231 static const struct comedi_lrange cb_pcidas_ao_ranges = {
246 struct cb_pcidas_board {
248 unsigned short device_id;
249 int ai_se_chans; /* Inputs in single-ended mode */
250 int ai_diff_chans; /* Inputs in differential mode */
251 int ai_bits; /* analog input resolution */
252 int ai_speed; /* fastest conversion period in ns */
253 int ao_nchan; /* number of analog out channels */
254 int has_ao_fifo; /* analog output has fifo */
255 int ao_scan_speed; /* analog output speed for 1602 series (for a scan, not conversion) */
256 int fifo_size; /* number of samples fifo can hold */
257 const struct comedi_lrange *ranges;
258 enum trimpot_model trimpot;
259 unsigned has_dac08:1;
262 static const struct cb_pcidas_board cb_pcidas_boards[] = {
264 .name = "pci-das1602/16",
272 .ao_scan_speed = 10000,
274 .ranges = &cb_pcidas_ranges,
279 .name = "pci-das1200",
288 .ranges = &cb_pcidas_ranges,
293 .name = "pci-das1602/12",
301 .ao_scan_speed = 4000,
303 .ranges = &cb_pcidas_ranges,
308 .name = "pci-das1200/jr",
317 .ranges = &cb_pcidas_ranges,
322 .name = "pci-das1602/16/jr",
331 .ranges = &cb_pcidas_ranges,
336 .name = "pci-das1000",
345 .ranges = &cb_pcidas_ranges,
350 .name = "pci-das1001",
359 .ranges = &cb_pcidas_alt_ranges,
364 .name = "pci-das1002",
373 .ranges = &cb_pcidas_ranges,
379 static DEFINE_PCI_DEVICE_TABLE(cb_pcidas_pci_table) = {
381 PCI_VENDOR_ID_CB, 0x0001, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0}, {
382 PCI_VENDOR_ID_CB, 0x000f, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0}, {
383 PCI_VENDOR_ID_CB, 0x0010, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0}, {
384 PCI_VENDOR_ID_CB, 0x0019, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0}, {
385 PCI_VENDOR_ID_CB, 0x001c, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0}, {
386 PCI_VENDOR_ID_CB, 0x004c, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0}, {
387 PCI_VENDOR_ID_CB, 0x001a, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0}, {
388 PCI_VENDOR_ID_CB, 0x001b, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0}, {
392 MODULE_DEVICE_TABLE(pci, cb_pcidas_pci_table);
395 * Useful for shorthand access to the particular board structure
397 #define thisboard ((const struct cb_pcidas_board *)dev->board_ptr)
399 /* this structure is for data unique to this hardware driver. If
400 several hardware drivers keep similar information in this structure,
401 feel free to suggest moving the variable to the struct comedi_device struct. */
402 struct cb_pcidas_private {
403 /* would be useful for a PCI device */
404 struct pci_dev *pci_dev;
406 unsigned long s5933_config;
407 unsigned long control_status;
408 unsigned long adc_fifo;
409 unsigned long pacer_counter_dio;
410 unsigned long ao_registers;
411 /* divisors of master clock for analog input pacing */
412 unsigned int divisor1;
413 unsigned int divisor2;
414 volatile unsigned int count; /* number of analog input samples remaining */
415 volatile unsigned int adc_fifo_bits; /* bits to write to interupt/adcfifo register */
416 volatile unsigned int s5933_intcsr_bits; /* bits to write to amcc s5933 interrupt control/status register */
417 volatile unsigned int ao_control_bits; /* bits to write to ao control and status register */
418 short ai_buffer[AI_BUFFER_SIZE];
419 short ao_buffer[AO_BUFFER_SIZE];
420 /* divisors of master clock for analog output pacing */
421 unsigned int ao_divisor1;
422 unsigned int ao_divisor2;
423 volatile unsigned int ao_count; /* number of analog output samples remaining */
424 int ao_value[2]; /* remember what the analog outputs are set to, to allow readback */
425 unsigned int caldac_value[NUM_CHANNELS_8800]; /* for readback of caldac */
426 unsigned int trimpot_value[NUM_CHANNELS_8402]; /* for readback of trimpot */
427 unsigned int dac08_value;
428 unsigned int calibration_source;
432 * most drivers define the following macro to make it easy to
433 * access the private structure.
435 #define devpriv ((struct cb_pcidas_private *)dev->private)
438 * The struct comedi_driver structure tells the Comedi core module
439 * which functions to call to configure/deconfigure (attach/detach)
440 * the board, and also about the kernel module that contains
443 static int cb_pcidas_attach(struct comedi_device *dev,
444 struct comedi_devconfig *it);
445 static int cb_pcidas_detach(struct comedi_device *dev);
446 static struct comedi_driver driver_cb_pcidas = {
447 .driver_name = "cb_pcidas",
448 .module = THIS_MODULE,
449 .attach = cb_pcidas_attach,
450 .detach = cb_pcidas_detach,
453 static int cb_pcidas_ai_rinsn(struct comedi_device *dev,
454 struct comedi_subdevice *s,
455 struct comedi_insn *insn, unsigned int *data);
456 static int ai_config_insn(struct comedi_device *dev, struct comedi_subdevice *s,
457 struct comedi_insn *insn, unsigned int *data);
458 static int cb_pcidas_ao_nofifo_winsn(struct comedi_device *dev,
459 struct comedi_subdevice *s,
460 struct comedi_insn *insn,
462 static int cb_pcidas_ao_fifo_winsn(struct comedi_device *dev,
463 struct comedi_subdevice *s,
464 struct comedi_insn *insn,
466 static int cb_pcidas_ao_readback_insn(struct comedi_device *dev,
467 struct comedi_subdevice *s,
468 struct comedi_insn *insn,
470 static int cb_pcidas_ai_cmd(struct comedi_device *dev,
471 struct comedi_subdevice *s);
472 static int cb_pcidas_ai_cmdtest(struct comedi_device *dev,
473 struct comedi_subdevice *s,
474 struct comedi_cmd *cmd);
475 static int cb_pcidas_ao_cmd(struct comedi_device *dev,
476 struct comedi_subdevice *s);
477 static int cb_pcidas_ao_inttrig(struct comedi_device *dev,
478 struct comedi_subdevice *subdev,
479 unsigned int trig_num);
480 static int cb_pcidas_ao_cmdtest(struct comedi_device *dev,
481 struct comedi_subdevice *s,
482 struct comedi_cmd *cmd);
483 static irqreturn_t cb_pcidas_interrupt(int irq, void *d);
484 static void handle_ao_interrupt(struct comedi_device *dev, unsigned int status);
485 static int cb_pcidas_cancel(struct comedi_device *dev,
486 struct comedi_subdevice *s);
487 static int cb_pcidas_ao_cancel(struct comedi_device *dev,
488 struct comedi_subdevice *s);
489 static void cb_pcidas_load_counters(struct comedi_device *dev, unsigned int *ns,
491 static int eeprom_read_insn(struct comedi_device *dev,
492 struct comedi_subdevice *s,
493 struct comedi_insn *insn, unsigned int *data);
494 static int caldac_read_insn(struct comedi_device *dev,
495 struct comedi_subdevice *s,
496 struct comedi_insn *insn, unsigned int *data);
497 static int caldac_write_insn(struct comedi_device *dev,
498 struct comedi_subdevice *s,
499 struct comedi_insn *insn, unsigned int *data);
500 static int trimpot_read_insn(struct comedi_device *dev,
501 struct comedi_subdevice *s,
502 struct comedi_insn *insn, unsigned int *data);
503 static int cb_pcidas_trimpot_write(struct comedi_device *dev,
504 unsigned int channel, unsigned int value);
505 static int trimpot_write_insn(struct comedi_device *dev,
506 struct comedi_subdevice *s,
507 struct comedi_insn *insn, unsigned int *data);
508 static int dac08_read_insn(struct comedi_device *dev,
509 struct comedi_subdevice *s, struct comedi_insn *insn,
511 static int dac08_write(struct comedi_device *dev, unsigned int value);
512 static int dac08_write_insn(struct comedi_device *dev,
513 struct comedi_subdevice *s,
514 struct comedi_insn *insn, unsigned int *data);
515 static int caldac_8800_write(struct comedi_device *dev, unsigned int address,
517 static int trimpot_7376_write(struct comedi_device *dev, uint8_t value);
518 static int trimpot_8402_write(struct comedi_device *dev, unsigned int channel,
520 static int nvram_read(struct comedi_device *dev, unsigned int address,
523 static inline unsigned int cal_enable_bits(struct comedi_device *dev)
525 return CAL_EN_BIT | CAL_SRC_BITS(devpriv->calibration_source);
529 * Attach is called by the Comedi core to configure the driver
530 * for a particular board.
532 static int cb_pcidas_attach(struct comedi_device *dev,
533 struct comedi_devconfig *it)
535 struct comedi_subdevice *s;
536 struct pci_dev *pcidev;
540 printk("comedi%d: cb_pcidas: ", dev->minor);
543 * Allocate the private structure area.
545 if (alloc_private(dev, sizeof(struct cb_pcidas_private)) < 0)
549 * Probe the device to determine what device in the series it is.
553 for (pcidev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, NULL);
555 pcidev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, pcidev)) {
556 /* is it not a computer boards card? */
557 if (pcidev->vendor != PCI_VENDOR_ID_CB)
559 /* loop through cards supported by this driver */
560 for (index = 0; index < ARRAY_SIZE(cb_pcidas_boards); index++) {
561 if (cb_pcidas_boards[index].device_id != pcidev->device)
563 /* was a particular bus/slot requested? */
564 if (it->options[0] || it->options[1]) {
565 /* are we on the wrong bus/slot? */
566 if (pcidev->bus->number != it->options[0] ||
567 PCI_SLOT(pcidev->devfn) != it->options[1]) {
571 devpriv->pci_dev = pcidev;
572 dev->board_ptr = cb_pcidas_boards + index;
577 printk("No supported ComputerBoards/MeasurementComputing card found on "
578 "requested position\n");
583 printk("Found %s on bus %i, slot %i\n", cb_pcidas_boards[index].name,
584 pcidev->bus->number, PCI_SLOT(pcidev->devfn));
587 * Enable PCI device and reserve I/O ports.
589 if (comedi_pci_enable(pcidev, "cb_pcidas")) {
590 printk(" Failed to enable PCI device and request regions\n");
594 * Initialize devpriv->control_status and devpriv->adc_fifo to point to
595 * their base address.
597 devpriv->s5933_config =
598 pci_resource_start(devpriv->pci_dev, S5933_BADRINDEX);
599 devpriv->control_status =
600 pci_resource_start(devpriv->pci_dev, CONT_STAT_BADRINDEX);
602 pci_resource_start(devpriv->pci_dev, ADC_FIFO_BADRINDEX);
603 devpriv->pacer_counter_dio =
604 pci_resource_start(devpriv->pci_dev, PACER_BADRINDEX);
605 if (thisboard->ao_nchan) {
606 devpriv->ao_registers =
607 pci_resource_start(devpriv->pci_dev, AO_BADRINDEX);
609 /* disable and clear interrupts on amcc s5933 */
610 outl(INTCSR_INBOX_INTR_STATUS,
611 devpriv->s5933_config + AMCC_OP_REG_INTCSR);
614 if (request_irq(devpriv->pci_dev->irq, cb_pcidas_interrupt,
615 IRQF_SHARED, "cb_pcidas", dev)) {
616 printk(" unable to allocate irq %d\n", devpriv->pci_dev->irq);
619 dev->irq = devpriv->pci_dev->irq;
621 /* Initialize dev->board_name */
622 dev->board_name = thisboard->name;
625 * Allocate the subdevice structures.
627 if (alloc_subdevices(dev, 7) < 0)
630 s = dev->subdevices + 0;
631 /* analog input subdevice */
632 dev->read_subdev = s;
633 s->type = COMEDI_SUBD_AI;
634 s->subdev_flags = SDF_READABLE | SDF_GROUND | SDF_DIFF | SDF_CMD_READ;
635 /* WARNING: Number of inputs in differential mode is ignored */
636 s->n_chan = thisboard->ai_se_chans;
637 s->len_chanlist = thisboard->ai_se_chans;
638 s->maxdata = (1 << thisboard->ai_bits) - 1;
639 s->range_table = thisboard->ranges;
640 s->insn_read = cb_pcidas_ai_rinsn;
641 s->insn_config = ai_config_insn;
642 s->do_cmd = cb_pcidas_ai_cmd;
643 s->do_cmdtest = cb_pcidas_ai_cmdtest;
644 s->cancel = cb_pcidas_cancel;
646 /* analog output subdevice */
647 s = dev->subdevices + 1;
648 if (thisboard->ao_nchan) {
649 s->type = COMEDI_SUBD_AO;
650 s->subdev_flags = SDF_READABLE | SDF_WRITABLE | SDF_GROUND;
651 s->n_chan = thisboard->ao_nchan;
652 /* analog out resolution is the same as analog input resolution, so use ai_bits */
653 s->maxdata = (1 << thisboard->ai_bits) - 1;
654 s->range_table = &cb_pcidas_ao_ranges;
655 s->insn_read = cb_pcidas_ao_readback_insn;
656 if (thisboard->has_ao_fifo) {
657 dev->write_subdev = s;
658 s->subdev_flags |= SDF_CMD_WRITE;
659 s->insn_write = cb_pcidas_ao_fifo_winsn;
660 s->do_cmdtest = cb_pcidas_ao_cmdtest;
661 s->do_cmd = cb_pcidas_ao_cmd;
662 s->cancel = cb_pcidas_ao_cancel;
664 s->insn_write = cb_pcidas_ao_nofifo_winsn;
667 s->type = COMEDI_SUBD_UNUSED;
671 s = dev->subdevices + 2;
672 subdev_8255_init(dev, s, NULL, devpriv->pacer_counter_dio + DIO_8255);
675 s = dev->subdevices + 3;
676 s->type = COMEDI_SUBD_MEMORY;
677 s->subdev_flags = SDF_READABLE | SDF_INTERNAL;
680 s->insn_read = eeprom_read_insn;
683 s = dev->subdevices + 4;
684 s->type = COMEDI_SUBD_CALIB;
685 s->subdev_flags = SDF_READABLE | SDF_WRITABLE | SDF_INTERNAL;
686 s->n_chan = NUM_CHANNELS_8800;
688 s->insn_read = caldac_read_insn;
689 s->insn_write = caldac_write_insn;
690 for (i = 0; i < s->n_chan; i++)
691 caldac_8800_write(dev, i, s->maxdata / 2);
693 /* trim potentiometer */
694 s = dev->subdevices + 5;
695 s->type = COMEDI_SUBD_CALIB;
696 s->subdev_flags = SDF_READABLE | SDF_WRITABLE | SDF_INTERNAL;
697 if (thisboard->trimpot == AD7376) {
698 s->n_chan = NUM_CHANNELS_7376;
701 s->n_chan = NUM_CHANNELS_8402;
704 s->insn_read = trimpot_read_insn;
705 s->insn_write = trimpot_write_insn;
706 for (i = 0; i < s->n_chan; i++)
707 cb_pcidas_trimpot_write(dev, i, s->maxdata / 2);
710 s = dev->subdevices + 6;
711 if (thisboard->has_dac08) {
712 s->type = COMEDI_SUBD_CALIB;
713 s->subdev_flags = SDF_READABLE | SDF_WRITABLE | SDF_INTERNAL;
714 s->n_chan = NUM_CHANNELS_DAC08;
715 s->insn_read = dac08_read_insn;
716 s->insn_write = dac08_write_insn;
718 dac08_write(dev, s->maxdata / 2);
720 s->type = COMEDI_SUBD_UNUSED;
722 /* make sure mailbox 4 is empty */
723 inl(devpriv->s5933_config + AMCC_OP_REG_IMB4);
724 /* Set bits to enable incoming mailbox interrupts on amcc s5933. */
725 devpriv->s5933_intcsr_bits =
726 INTCSR_INBOX_BYTE(3) | INTCSR_INBOX_SELECT(3) |
727 INTCSR_INBOX_FULL_INT;
728 /* clear and enable interrupt on amcc s5933 */
729 outl(devpriv->s5933_intcsr_bits | INTCSR_INBOX_INTR_STATUS,
730 devpriv->s5933_config + AMCC_OP_REG_INTCSR);
736 * cb_pcidas_detach is called to deconfigure a device. It should deallocate
738 * This function is also called when _attach() fails, so it should be
739 * careful not to release resources that were not necessarily
740 * allocated by _attach(). dev->private and dev->subdevices are
741 * deallocated automatically by the core.
743 static int cb_pcidas_detach(struct comedi_device *dev)
745 printk("comedi%d: cb_pcidas: remove\n", dev->minor);
748 if (devpriv->s5933_config) {
749 /* disable and clear interrupts on amcc s5933 */
750 outl(INTCSR_INBOX_INTR_STATUS,
751 devpriv->s5933_config + AMCC_OP_REG_INTCSR);
752 #ifdef CB_PCIDAS_DEBUG
753 printk("detaching, incsr is 0x%x\n",
754 inl(devpriv->s5933_config + AMCC_OP_REG_INTCSR));
759 free_irq(dev->irq, dev);
761 subdev_8255_cleanup(dev, dev->subdevices + 2);
762 if (devpriv && devpriv->pci_dev) {
763 if (devpriv->s5933_config) {
764 comedi_pci_disable(devpriv->pci_dev);
766 pci_dev_put(devpriv->pci_dev);
773 * "instructions" read/write data in "one-shot" or "software-triggered"
776 static int cb_pcidas_ai_rinsn(struct comedi_device *dev,
777 struct comedi_subdevice *s,
778 struct comedi_insn *insn, unsigned int *data)
782 static const int timeout = 10000;
784 /* enable calibration input if appropriate */
785 if (insn->chanspec & CR_ALT_SOURCE) {
786 outw(cal_enable_bits(dev),
787 devpriv->control_status + CALIBRATION_REG);
790 outw(0, devpriv->control_status + CALIBRATION_REG);
791 channel = CR_CHAN(insn->chanspec);
793 /* set mux limits and gain */
794 bits = BEGIN_SCAN(channel) |
795 END_SCAN(channel) | GAIN_BITS(CR_RANGE(insn->chanspec));
796 /* set unipolar/bipolar */
797 if (CR_RANGE(insn->chanspec) & IS_UNIPOLAR)
799 /* set singleended/differential */
800 if (CR_AREF(insn->chanspec) != AREF_DIFF)
802 outw(bits, devpriv->control_status + ADCMUX_CONT);
805 outw(0, devpriv->adc_fifo + ADCFIFOCLR);
807 /* convert n samples */
808 for (n = 0; n < insn->n; n++) {
809 /* trigger conversion */
810 outw(0, devpriv->adc_fifo + ADCDATA);
812 /* wait for conversion to end */
813 /* return -ETIMEDOUT if there is a timeout */
814 for (i = 0; i < timeout; i++) {
815 if (inw(devpriv->control_status + ADCMUX_CONT) & EOC)
822 data[n] = inw(devpriv->adc_fifo + ADCDATA);
825 /* return the number of samples read/written */
829 static int ai_config_calibration_source(struct comedi_device *dev,
832 static const int num_calibration_sources = 8;
833 unsigned int source = data[1];
835 if (source >= num_calibration_sources) {
836 printk("invalid calibration source: %i\n", source);
840 devpriv->calibration_source = source;
845 static int ai_config_insn(struct comedi_device *dev, struct comedi_subdevice *s,
846 struct comedi_insn *insn, unsigned int *data)
851 case INSN_CONFIG_ALT_SOURCE:
852 return ai_config_calibration_source(dev, data);
861 /* analog output insn for pcidas-1000 and 1200 series */
862 static int cb_pcidas_ao_nofifo_winsn(struct comedi_device *dev,
863 struct comedi_subdevice *s,
864 struct comedi_insn *insn,
870 /* set channel and range */
871 channel = CR_CHAN(insn->chanspec);
872 spin_lock_irqsave(&dev->spinlock, flags);
873 devpriv->ao_control_bits &=
874 ~DAC_MODE_UPDATE_BOTH & ~DAC_RANGE_MASK(channel);
875 devpriv->ao_control_bits |=
876 DACEN | DAC_RANGE(channel, CR_RANGE(insn->chanspec));
877 outw(devpriv->ao_control_bits, devpriv->control_status + DAC_CSR);
878 spin_unlock_irqrestore(&dev->spinlock, flags);
880 /* remember value for readback */
881 devpriv->ao_value[channel] = data[0];
883 outw(data[0], devpriv->ao_registers + DAC_DATA_REG(channel));
888 /* analog output insn for pcidas-1602 series */
889 static int cb_pcidas_ao_fifo_winsn(struct comedi_device *dev,
890 struct comedi_subdevice *s,
891 struct comedi_insn *insn, unsigned int *data)
897 outw(0, devpriv->ao_registers + DACFIFOCLR);
899 /* set channel and range */
900 channel = CR_CHAN(insn->chanspec);
901 spin_lock_irqsave(&dev->spinlock, flags);
902 devpriv->ao_control_bits &=
903 ~DAC_CHAN_EN(0) & ~DAC_CHAN_EN(1) & ~DAC_RANGE_MASK(channel) &
905 devpriv->ao_control_bits |=
906 DACEN | DAC_RANGE(channel,
908 chanspec)) | DAC_CHAN_EN(channel) |
910 outw(devpriv->ao_control_bits, devpriv->control_status + DAC_CSR);
911 spin_unlock_irqrestore(&dev->spinlock, flags);
913 /* remember value for readback */
914 devpriv->ao_value[channel] = data[0];
916 outw(data[0], devpriv->ao_registers + DACDATA);
921 /* analog output readback insn */
922 /* XXX loses track of analog output value back after an analog ouput command is executed */
923 static int cb_pcidas_ao_readback_insn(struct comedi_device *dev,
924 struct comedi_subdevice *s,
925 struct comedi_insn *insn,
928 data[0] = devpriv->ao_value[CR_CHAN(insn->chanspec)];
933 static int eeprom_read_insn(struct comedi_device *dev,
934 struct comedi_subdevice *s,
935 struct comedi_insn *insn, unsigned int *data)
940 retval = nvram_read(dev, CR_CHAN(insn->chanspec), &nvram_data);
944 data[0] = nvram_data;
949 static int caldac_write_insn(struct comedi_device *dev,
950 struct comedi_subdevice *s,
951 struct comedi_insn *insn, unsigned int *data)
953 const unsigned int channel = CR_CHAN(insn->chanspec);
955 return caldac_8800_write(dev, channel, data[0]);
958 static int caldac_read_insn(struct comedi_device *dev,
959 struct comedi_subdevice *s,
960 struct comedi_insn *insn, unsigned int *data)
962 data[0] = devpriv->caldac_value[CR_CHAN(insn->chanspec)];
967 /* 1602/16 pregain offset */
968 static int dac08_write(struct comedi_device *dev, unsigned int value)
970 if (devpriv->dac08_value == value)
973 devpriv->dac08_value = value;
975 outw(cal_enable_bits(dev) | (value & 0xff),
976 devpriv->control_status + CALIBRATION_REG);
978 outw(cal_enable_bits(dev) | SELECT_DAC08_BIT | (value & 0xff),
979 devpriv->control_status + CALIBRATION_REG);
981 outw(cal_enable_bits(dev) | (value & 0xff),
982 devpriv->control_status + CALIBRATION_REG);
988 static int dac08_write_insn(struct comedi_device *dev,
989 struct comedi_subdevice *s,
990 struct comedi_insn *insn, unsigned int *data)
992 return dac08_write(dev, data[0]);
995 static int dac08_read_insn(struct comedi_device *dev,
996 struct comedi_subdevice *s, struct comedi_insn *insn,
999 data[0] = devpriv->dac08_value;
1004 static int cb_pcidas_trimpot_write(struct comedi_device *dev,
1005 unsigned int channel, unsigned int value)
1007 if (devpriv->trimpot_value[channel] == value)
1010 devpriv->trimpot_value[channel] = value;
1011 switch (thisboard->trimpot) {
1013 trimpot_7376_write(dev, value);
1016 trimpot_8402_write(dev, channel, value);
1019 comedi_error(dev, "driver bug?");
1027 static int trimpot_write_insn(struct comedi_device *dev,
1028 struct comedi_subdevice *s,
1029 struct comedi_insn *insn, unsigned int *data)
1031 unsigned int channel = CR_CHAN(insn->chanspec);
1033 return cb_pcidas_trimpot_write(dev, channel, data[0]);
1036 static int trimpot_read_insn(struct comedi_device *dev,
1037 struct comedi_subdevice *s,
1038 struct comedi_insn *insn, unsigned int *data)
1040 unsigned int channel = CR_CHAN(insn->chanspec);
1042 data[0] = devpriv->trimpot_value[channel];
1047 static int cb_pcidas_ai_cmdtest(struct comedi_device *dev,
1048 struct comedi_subdevice *s,
1049 struct comedi_cmd *cmd)
1053 int i, gain, start_chan;
1055 /* cmdtest tests a particular command to see if it is valid.
1056 * Using the cmdtest ioctl, a user can create a valid cmd
1057 * and then have it executes by the cmd ioctl.
1059 * cmdtest returns 1,2,3,4 or 0, depending on which tests
1060 * the command passes. */
1062 /* step 1: make sure trigger sources are trivially valid */
1064 tmp = cmd->start_src;
1065 cmd->start_src &= TRIG_NOW | TRIG_EXT;
1066 if (!cmd->start_src || tmp != cmd->start_src)
1069 tmp = cmd->scan_begin_src;
1070 cmd->scan_begin_src &= TRIG_FOLLOW | TRIG_TIMER | TRIG_EXT;
1071 if (!cmd->scan_begin_src || tmp != cmd->scan_begin_src)
1074 tmp = cmd->convert_src;
1075 cmd->convert_src &= TRIG_TIMER | TRIG_NOW | TRIG_EXT;
1076 if (!cmd->convert_src || tmp != cmd->convert_src)
1079 tmp = cmd->scan_end_src;
1080 cmd->scan_end_src &= TRIG_COUNT;
1081 if (!cmd->scan_end_src || tmp != cmd->scan_end_src)
1084 tmp = cmd->stop_src;
1085 cmd->stop_src &= TRIG_COUNT | TRIG_NONE;
1086 if (!cmd->stop_src || tmp != cmd->stop_src)
1092 /* step 2: make sure trigger sources are unique and mutually compatible */
1094 if (cmd->start_src != TRIG_NOW && cmd->start_src != TRIG_EXT)
1096 if (cmd->scan_begin_src != TRIG_FOLLOW &&
1097 cmd->scan_begin_src != TRIG_TIMER &&
1098 cmd->scan_begin_src != TRIG_EXT)
1100 if (cmd->convert_src != TRIG_TIMER &&
1101 cmd->convert_src != TRIG_EXT && cmd->convert_src != TRIG_NOW)
1103 if (cmd->stop_src != TRIG_COUNT && cmd->stop_src != TRIG_NONE)
1106 /* make sure trigger sources are compatible with each other */
1107 if (cmd->scan_begin_src == TRIG_FOLLOW && cmd->convert_src == TRIG_NOW)
1109 if (cmd->scan_begin_src != TRIG_FOLLOW && cmd->convert_src != TRIG_NOW)
1111 if (cmd->start_src == TRIG_EXT &&
1112 (cmd->convert_src == TRIG_EXT || cmd->scan_begin_src == TRIG_EXT))
1118 /* step 3: make sure arguments are trivially compatible */
1120 if (cmd->start_arg != 0) {
1125 if (cmd->scan_begin_src == TRIG_TIMER) {
1126 if (cmd->scan_begin_arg <
1127 thisboard->ai_speed * cmd->chanlist_len) {
1128 cmd->scan_begin_arg =
1129 thisboard->ai_speed * cmd->chanlist_len;
1133 if (cmd->convert_src == TRIG_TIMER) {
1134 if (cmd->convert_arg < thisboard->ai_speed) {
1135 cmd->convert_arg = thisboard->ai_speed;
1140 if (cmd->scan_end_arg != cmd->chanlist_len) {
1141 cmd->scan_end_arg = cmd->chanlist_len;
1144 if (cmd->stop_src == TRIG_NONE) {
1146 if (cmd->stop_arg != 0) {
1155 /* step 4: fix up any arguments */
1157 if (cmd->scan_begin_src == TRIG_TIMER) {
1158 tmp = cmd->scan_begin_arg;
1159 i8253_cascade_ns_to_timer_2div(TIMER_BASE,
1160 &(devpriv->divisor1),
1161 &(devpriv->divisor2),
1162 &(cmd->scan_begin_arg),
1163 cmd->flags & TRIG_ROUND_MASK);
1164 if (tmp != cmd->scan_begin_arg)
1167 if (cmd->convert_src == TRIG_TIMER) {
1168 tmp = cmd->convert_arg;
1169 i8253_cascade_ns_to_timer_2div(TIMER_BASE,
1170 &(devpriv->divisor1),
1171 &(devpriv->divisor2),
1172 &(cmd->convert_arg),
1173 cmd->flags & TRIG_ROUND_MASK);
1174 if (tmp != cmd->convert_arg)
1181 /* check channel/gain list against card's limitations */
1182 if (cmd->chanlist) {
1183 gain = CR_RANGE(cmd->chanlist[0]);
1184 start_chan = CR_CHAN(cmd->chanlist[0]);
1185 for (i = 1; i < cmd->chanlist_len; i++) {
1186 if (CR_CHAN(cmd->chanlist[i]) !=
1187 (start_chan + i) % s->n_chan) {
1189 "entries in chanlist must be consecutive channels, counting upwards\n");
1192 if (CR_RANGE(cmd->chanlist[i]) != gain) {
1194 "entries in chanlist must all have the same gain\n");
1206 static int cb_pcidas_ai_cmd(struct comedi_device *dev,
1207 struct comedi_subdevice *s)
1209 struct comedi_async *async = s->async;
1210 struct comedi_cmd *cmd = &async->cmd;
1212 unsigned long flags;
1214 /* make sure CAL_EN_BIT is disabled */
1215 outw(0, devpriv->control_status + CALIBRATION_REG);
1216 /* initialize before settings pacer source and count values */
1217 outw(0, devpriv->control_status + TRIG_CONTSTAT);
1219 outw(0, devpriv->adc_fifo + ADCFIFOCLR);
1221 /* set mux limits, gain and pacer source */
1222 bits = BEGIN_SCAN(CR_CHAN(cmd->chanlist[0])) |
1223 END_SCAN(CR_CHAN(cmd->chanlist[cmd->chanlist_len - 1])) |
1224 GAIN_BITS(CR_RANGE(cmd->chanlist[0]));
1225 /* set unipolar/bipolar */
1226 if (CR_RANGE(cmd->chanlist[0]) & IS_UNIPOLAR)
1228 /* set singleended/differential */
1229 if (CR_AREF(cmd->chanlist[0]) != AREF_DIFF)
1231 /* set pacer source */
1232 if (cmd->convert_src == TRIG_EXT || cmd->scan_begin_src == TRIG_EXT)
1233 bits |= PACER_EXT_RISE;
1236 outw(bits, devpriv->control_status + ADCMUX_CONT);
1238 #ifdef CB_PCIDAS_DEBUG
1239 printk("comedi: sent 0x%x to adcmux control\n", bits);
1243 if (cmd->convert_src == TRIG_TIMER)
1244 cb_pcidas_load_counters(dev, &cmd->convert_arg,
1245 cmd->flags & TRIG_ROUND_MASK);
1246 else if (cmd->scan_begin_src == TRIG_TIMER)
1247 cb_pcidas_load_counters(dev, &cmd->scan_begin_arg,
1248 cmd->flags & TRIG_ROUND_MASK);
1250 /* set number of conversions */
1251 if (cmd->stop_src == TRIG_COUNT) {
1252 devpriv->count = cmd->chanlist_len * cmd->stop_arg;
1254 /* enable interrupts */
1255 spin_lock_irqsave(&dev->spinlock, flags);
1256 devpriv->adc_fifo_bits |= INTE;
1257 devpriv->adc_fifo_bits &= ~INT_MASK;
1258 if (cmd->flags & TRIG_WAKE_EOS) {
1259 if (cmd->convert_src == TRIG_NOW && cmd->chanlist_len > 1)
1260 devpriv->adc_fifo_bits |= INT_EOS; /* interrupt end of burst */
1262 devpriv->adc_fifo_bits |= INT_FNE; /* interrupt fifo not empty */
1264 devpriv->adc_fifo_bits |= INT_FHF; /* interrupt fifo half full */
1266 #ifdef CB_PCIDAS_DEBUG
1267 printk("comedi: adc_fifo_bits are 0x%x\n", devpriv->adc_fifo_bits);
1269 /* enable (and clear) interrupts */
1270 outw(devpriv->adc_fifo_bits | EOAI | INT | LADFUL,
1271 devpriv->control_status + INT_ADCFIFO);
1272 spin_unlock_irqrestore(&dev->spinlock, flags);
1274 /* set start trigger and burst mode */
1276 if (cmd->start_src == TRIG_NOW)
1278 else if (cmd->start_src == TRIG_EXT)
1279 bits |= EXT_TRIGGER | TGEN | XTRCL;
1281 comedi_error(dev, "bug!");
1284 if (cmd->convert_src == TRIG_NOW && cmd->chanlist_len > 1)
1286 outw(bits, devpriv->control_status + TRIG_CONTSTAT);
1287 #ifdef CB_PCIDAS_DEBUG
1288 printk("comedi: sent 0x%x to trig control\n", bits);
1294 static int cb_pcidas_ao_cmdtest(struct comedi_device *dev,
1295 struct comedi_subdevice *s,
1296 struct comedi_cmd *cmd)
1301 /* cmdtest tests a particular command to see if it is valid.
1302 * Using the cmdtest ioctl, a user can create a valid cmd
1303 * and then have it executes by the cmd ioctl.
1305 * cmdtest returns 1,2,3,4 or 0, depending on which tests
1306 * the command passes. */
1308 /* step 1: make sure trigger sources are trivially valid */
1310 tmp = cmd->start_src;
1311 cmd->start_src &= TRIG_INT;
1312 if (!cmd->start_src || tmp != cmd->start_src)
1315 tmp = cmd->scan_begin_src;
1316 cmd->scan_begin_src &= TRIG_TIMER | TRIG_EXT;
1317 if (!cmd->scan_begin_src || tmp != cmd->scan_begin_src)
1320 tmp = cmd->convert_src;
1321 cmd->convert_src &= TRIG_NOW;
1322 if (!cmd->convert_src || tmp != cmd->convert_src)
1325 tmp = cmd->scan_end_src;
1326 cmd->scan_end_src &= TRIG_COUNT;
1327 if (!cmd->scan_end_src || tmp != cmd->scan_end_src)
1330 tmp = cmd->stop_src;
1331 cmd->stop_src &= TRIG_COUNT | TRIG_NONE;
1332 if (!cmd->stop_src || tmp != cmd->stop_src)
1338 /* step 2: make sure trigger sources are unique and mutually compatible */
1340 if (cmd->scan_begin_src != TRIG_TIMER &&
1341 cmd->scan_begin_src != TRIG_EXT)
1343 if (cmd->stop_src != TRIG_COUNT && cmd->stop_src != TRIG_NONE)
1349 /* step 3: make sure arguments are trivially compatible */
1351 if (cmd->start_arg != 0) {
1356 if (cmd->scan_begin_src == TRIG_TIMER) {
1357 if (cmd->scan_begin_arg < thisboard->ao_scan_speed) {
1358 cmd->scan_begin_arg = thisboard->ao_scan_speed;
1363 if (cmd->scan_end_arg != cmd->chanlist_len) {
1364 cmd->scan_end_arg = cmd->chanlist_len;
1367 if (cmd->stop_src == TRIG_NONE) {
1369 if (cmd->stop_arg != 0) {
1378 /* step 4: fix up any arguments */
1380 if (cmd->scan_begin_src == TRIG_TIMER) {
1381 tmp = cmd->scan_begin_arg;
1382 i8253_cascade_ns_to_timer_2div(TIMER_BASE,
1383 &(devpriv->ao_divisor1),
1384 &(devpriv->ao_divisor2),
1385 &(cmd->scan_begin_arg),
1386 cmd->flags & TRIG_ROUND_MASK);
1387 if (tmp != cmd->scan_begin_arg)
1394 /* check channel/gain list against card's limitations */
1395 if (cmd->chanlist && cmd->chanlist_len > 1) {
1396 if (CR_CHAN(cmd->chanlist[0]) != 0 ||
1397 CR_CHAN(cmd->chanlist[1]) != 1) {
1399 "channels must be ordered channel 0, channel 1 in chanlist\n");
1410 static int cb_pcidas_ao_cmd(struct comedi_device *dev,
1411 struct comedi_subdevice *s)
1413 struct comedi_async *async = s->async;
1414 struct comedi_cmd *cmd = &async->cmd;
1416 unsigned long flags;
1418 /* set channel limits, gain */
1419 spin_lock_irqsave(&dev->spinlock, flags);
1420 for (i = 0; i < cmd->chanlist_len; i++) {
1421 /* enable channel */
1422 devpriv->ao_control_bits |=
1423 DAC_CHAN_EN(CR_CHAN(cmd->chanlist[i]));
1425 devpriv->ao_control_bits |= DAC_RANGE(CR_CHAN(cmd->chanlist[i]),
1430 /* disable analog out before settings pacer source and count values */
1431 outw(devpriv->ao_control_bits, devpriv->control_status + DAC_CSR);
1432 spin_unlock_irqrestore(&dev->spinlock, flags);
1435 outw(0, devpriv->ao_registers + DACFIFOCLR);
1438 if (cmd->scan_begin_src == TRIG_TIMER) {
1439 i8253_cascade_ns_to_timer_2div(TIMER_BASE,
1440 &(devpriv->ao_divisor1),
1441 &(devpriv->ao_divisor2),
1442 &(cmd->scan_begin_arg),
1445 /* Write the values of ctr1 and ctr2 into counters 1 and 2 */
1446 i8254_load(devpriv->pacer_counter_dio + DAC8254, 0, 1,
1447 devpriv->ao_divisor1, 2);
1448 i8254_load(devpriv->pacer_counter_dio + DAC8254, 0, 2,
1449 devpriv->ao_divisor2, 2);
1451 /* set number of conversions */
1452 if (cmd->stop_src == TRIG_COUNT) {
1453 devpriv->ao_count = cmd->chanlist_len * cmd->stop_arg;
1455 /* set pacer source */
1456 spin_lock_irqsave(&dev->spinlock, flags);
1457 switch (cmd->scan_begin_src) {
1459 devpriv->ao_control_bits |= DAC_PACER_INT;
1462 devpriv->ao_control_bits |= DAC_PACER_EXT_RISE;
1465 spin_unlock_irqrestore(&dev->spinlock, flags);
1466 comedi_error(dev, "error setting dac pacer source");
1470 spin_unlock_irqrestore(&dev->spinlock, flags);
1472 async->inttrig = cb_pcidas_ao_inttrig;
1477 static int cb_pcidas_ao_inttrig(struct comedi_device *dev,
1478 struct comedi_subdevice *s,
1479 unsigned int trig_num)
1481 unsigned int num_bytes, num_points = thisboard->fifo_size;
1482 struct comedi_async *async = s->async;
1483 struct comedi_cmd *cmd = &s->async->cmd;
1484 unsigned long flags;
1490 if (cmd->stop_src == TRIG_COUNT && devpriv->ao_count < num_points)
1491 num_points = devpriv->ao_count;
1493 num_bytes = cfc_read_array_from_buffer(s, devpriv->ao_buffer,
1494 num_points * sizeof(short));
1495 num_points = num_bytes / sizeof(short);
1497 if (cmd->stop_src == TRIG_COUNT) {
1498 devpriv->ao_count -= num_points;
1500 /* write data to board's fifo */
1501 outsw(devpriv->ao_registers + DACDATA, devpriv->ao_buffer, num_bytes);
1503 /* enable dac half-full and empty interrupts */
1504 spin_lock_irqsave(&dev->spinlock, flags);
1505 devpriv->adc_fifo_bits |= DAEMIE | DAHFIE;
1506 #ifdef CB_PCIDAS_DEBUG
1507 printk("comedi: adc_fifo_bits are 0x%x\n", devpriv->adc_fifo_bits);
1509 /* enable and clear interrupts */
1510 outw(devpriv->adc_fifo_bits | DAEMI | DAHFI,
1511 devpriv->control_status + INT_ADCFIFO);
1514 devpriv->ao_control_bits |= DAC_START | DACEN | DAC_EMPTY;
1515 outw(devpriv->ao_control_bits, devpriv->control_status + DAC_CSR);
1516 #ifdef CB_PCIDAS_DEBUG
1517 printk("comedi: sent 0x%x to dac control\n", devpriv->ao_control_bits);
1519 spin_unlock_irqrestore(&dev->spinlock, flags);
1521 async->inttrig = NULL;
1526 static irqreturn_t cb_pcidas_interrupt(int irq, void *d)
1528 struct comedi_device *dev = (struct comedi_device *)d;
1529 struct comedi_subdevice *s = dev->read_subdev;
1530 struct comedi_async *async;
1531 int status, s5933_status;
1532 int half_fifo = thisboard->fifo_size / 2;
1533 unsigned int num_samples, i;
1534 static const int timeout = 10000;
1535 unsigned long flags;
1537 if (dev->attached == 0) {
1544 s5933_status = inl(devpriv->s5933_config + AMCC_OP_REG_INTCSR);
1545 #ifdef CB_PCIDAS_DEBUG
1546 printk("intcsr 0x%x\n", s5933_status);
1547 printk("mbef 0x%x\n", inl(devpriv->s5933_config + AMCC_OP_REG_MBEF));
1550 if ((INTCSR_INTR_ASSERTED & s5933_status) == 0)
1553 /* make sure mailbox 4 is empty */
1554 inl_p(devpriv->s5933_config + AMCC_OP_REG_IMB4);
1555 /* clear interrupt on amcc s5933 */
1556 outl(devpriv->s5933_intcsr_bits | INTCSR_INBOX_INTR_STATUS,
1557 devpriv->s5933_config + AMCC_OP_REG_INTCSR);
1559 status = inw(devpriv->control_status + INT_ADCFIFO);
1560 #ifdef CB_PCIDAS_DEBUG
1561 if ((status & (INT | EOAI | LADFUL | DAHFI | DAEMI)) == 0) {
1562 comedi_error(dev, "spurious interrupt");
1566 /* check for analog output interrupt */
1567 if (status & (DAHFI | DAEMI)) {
1568 handle_ao_interrupt(dev, status);
1570 /* check for analog input interrupts */
1571 /* if fifo half-full */
1572 if (status & ADHFI) {
1574 num_samples = half_fifo;
1575 if (async->cmd.stop_src == TRIG_COUNT &&
1576 num_samples > devpriv->count) {
1577 num_samples = devpriv->count;
1579 insw(devpriv->adc_fifo + ADCDATA, devpriv->ai_buffer,
1581 cfc_write_array_to_buffer(s, devpriv->ai_buffer,
1582 num_samples * sizeof(short));
1583 devpriv->count -= num_samples;
1584 if (async->cmd.stop_src == TRIG_COUNT && devpriv->count == 0) {
1585 async->events |= COMEDI_CB_EOA;
1586 cb_pcidas_cancel(dev, s);
1588 /* clear half-full interrupt latch */
1589 spin_lock_irqsave(&dev->spinlock, flags);
1590 outw(devpriv->adc_fifo_bits | INT,
1591 devpriv->control_status + INT_ADCFIFO);
1592 spin_unlock_irqrestore(&dev->spinlock, flags);
1593 /* else if fifo not empty */
1594 } else if (status & (ADNEI | EOBI)) {
1595 for (i = 0; i < timeout; i++) {
1596 /* break if fifo is empty */
1597 if ((ADNE & inw(devpriv->control_status +
1600 cfc_write_to_buffer(s, inw(devpriv->adc_fifo));
1601 if (async->cmd.stop_src == TRIG_COUNT && --devpriv->count == 0) { /* end of acquisition */
1602 cb_pcidas_cancel(dev, s);
1603 async->events |= COMEDI_CB_EOA;
1607 /* clear not-empty interrupt latch */
1608 spin_lock_irqsave(&dev->spinlock, flags);
1609 outw(devpriv->adc_fifo_bits | INT,
1610 devpriv->control_status + INT_ADCFIFO);
1611 spin_unlock_irqrestore(&dev->spinlock, flags);
1612 } else if (status & EOAI) {
1614 "bug! encountered end of aquisition interrupt?");
1615 /* clear EOA interrupt latch */
1616 spin_lock_irqsave(&dev->spinlock, flags);
1617 outw(devpriv->adc_fifo_bits | EOAI,
1618 devpriv->control_status + INT_ADCFIFO);
1619 spin_unlock_irqrestore(&dev->spinlock, flags);
1621 /* check for fifo overflow */
1622 if (status & LADFUL) {
1623 comedi_error(dev, "fifo overflow");
1624 /* clear overflow interrupt latch */
1625 spin_lock_irqsave(&dev->spinlock, flags);
1626 outw(devpriv->adc_fifo_bits | LADFUL,
1627 devpriv->control_status + INT_ADCFIFO);
1628 spin_unlock_irqrestore(&dev->spinlock, flags);
1629 cb_pcidas_cancel(dev, s);
1630 async->events |= COMEDI_CB_EOA | COMEDI_CB_ERROR;
1633 comedi_event(dev, s);
1638 static void handle_ao_interrupt(struct comedi_device *dev, unsigned int status)
1640 struct comedi_subdevice *s = dev->write_subdev;
1641 struct comedi_async *async = s->async;
1642 struct comedi_cmd *cmd = &async->cmd;
1643 unsigned int half_fifo = thisboard->fifo_size / 2;
1644 unsigned int num_points;
1645 unsigned long flags;
1649 if (status & DAEMI) {
1650 /* clear dac empty interrupt latch */
1651 spin_lock_irqsave(&dev->spinlock, flags);
1652 outw(devpriv->adc_fifo_bits | DAEMI,
1653 devpriv->control_status + INT_ADCFIFO);
1654 spin_unlock_irqrestore(&dev->spinlock, flags);
1655 if (inw(devpriv->ao_registers + DAC_CSR) & DAC_EMPTY) {
1656 if (cmd->stop_src == TRIG_NONE ||
1657 (cmd->stop_src == TRIG_COUNT
1658 && devpriv->ao_count)) {
1659 comedi_error(dev, "dac fifo underflow");
1660 cb_pcidas_ao_cancel(dev, s);
1661 async->events |= COMEDI_CB_ERROR;
1663 async->events |= COMEDI_CB_EOA;
1665 } else if (status & DAHFI) {
1666 unsigned int num_bytes;
1668 /* figure out how many points we are writing to fifo */
1669 num_points = half_fifo;
1670 if (cmd->stop_src == TRIG_COUNT &&
1671 devpriv->ao_count < num_points)
1672 num_points = devpriv->ao_count;
1674 cfc_read_array_from_buffer(s, devpriv->ao_buffer,
1675 num_points * sizeof(short));
1676 num_points = num_bytes / sizeof(short);
1678 if (async->cmd.stop_src == TRIG_COUNT) {
1679 devpriv->ao_count -= num_points;
1681 /* write data to board's fifo */
1682 outsw(devpriv->ao_registers + DACDATA, devpriv->ao_buffer,
1684 /* clear half-full interrupt latch */
1685 spin_lock_irqsave(&dev->spinlock, flags);
1686 outw(devpriv->adc_fifo_bits | DAHFI,
1687 devpriv->control_status + INT_ADCFIFO);
1688 spin_unlock_irqrestore(&dev->spinlock, flags);
1691 comedi_event(dev, s);
1694 /* cancel analog input command */
1695 static int cb_pcidas_cancel(struct comedi_device *dev,
1696 struct comedi_subdevice *s)
1698 unsigned long flags;
1700 spin_lock_irqsave(&dev->spinlock, flags);
1701 /* disable interrupts */
1702 devpriv->adc_fifo_bits &= ~INTE & ~EOAIE;
1703 outw(devpriv->adc_fifo_bits, devpriv->control_status + INT_ADCFIFO);
1704 spin_unlock_irqrestore(&dev->spinlock, flags);
1706 /* disable start trigger source and burst mode */
1707 outw(0, devpriv->control_status + TRIG_CONTSTAT);
1708 /* software pacer source */
1709 outw(0, devpriv->control_status + ADCMUX_CONT);
1714 /* cancel analog output command */
1715 static int cb_pcidas_ao_cancel(struct comedi_device *dev,
1716 struct comedi_subdevice *s)
1718 unsigned long flags;
1720 spin_lock_irqsave(&dev->spinlock, flags);
1721 /* disable interrupts */
1722 devpriv->adc_fifo_bits &= ~DAHFIE & ~DAEMIE;
1723 outw(devpriv->adc_fifo_bits, devpriv->control_status + INT_ADCFIFO);
1725 /* disable output */
1726 devpriv->ao_control_bits &= ~DACEN & ~DAC_PACER_MASK;
1727 outw(devpriv->ao_control_bits, devpriv->control_status + DAC_CSR);
1728 spin_unlock_irqrestore(&dev->spinlock, flags);
1733 static void cb_pcidas_load_counters(struct comedi_device *dev, unsigned int *ns,
1736 i8253_cascade_ns_to_timer_2div(TIMER_BASE, &(devpriv->divisor1),
1737 &(devpriv->divisor2), ns,
1738 rounding_flags & TRIG_ROUND_MASK);
1740 /* Write the values of ctr1 and ctr2 into counters 1 and 2 */
1741 i8254_load(devpriv->pacer_counter_dio + ADC8254, 0, 1,
1742 devpriv->divisor1, 2);
1743 i8254_load(devpriv->pacer_counter_dio + ADC8254, 0, 2,
1744 devpriv->divisor2, 2);
1747 static void write_calibration_bitstream(struct comedi_device *dev,
1748 unsigned int register_bits,
1749 unsigned int bitstream,
1750 unsigned int bitstream_length)
1752 static const int write_delay = 1;
1755 for (bit = 1 << (bitstream_length - 1); bit; bit >>= 1) {
1756 if (bitstream & bit)
1757 register_bits |= SERIAL_DATA_IN_BIT;
1759 register_bits &= ~SERIAL_DATA_IN_BIT;
1760 udelay(write_delay);
1761 outw(register_bits, devpriv->control_status + CALIBRATION_REG);
1765 static int caldac_8800_write(struct comedi_device *dev, unsigned int address,
1768 static const int num_caldac_channels = 8;
1769 static const int bitstream_length = 11;
1770 unsigned int bitstream = ((address & 0x7) << 8) | value;
1771 static const int caldac_8800_udelay = 1;
1773 if (address >= num_caldac_channels) {
1774 comedi_error(dev, "illegal caldac channel");
1778 if (value == devpriv->caldac_value[address])
1781 devpriv->caldac_value[address] = value;
1783 write_calibration_bitstream(dev, cal_enable_bits(dev), bitstream,
1786 udelay(caldac_8800_udelay);
1787 outw(cal_enable_bits(dev) | SELECT_8800_BIT,
1788 devpriv->control_status + CALIBRATION_REG);
1789 udelay(caldac_8800_udelay);
1790 outw(cal_enable_bits(dev), devpriv->control_status + CALIBRATION_REG);
1795 static int trimpot_7376_write(struct comedi_device *dev, uint8_t value)
1797 static const int bitstream_length = 7;
1798 unsigned int bitstream = value & 0x7f;
1799 unsigned int register_bits;
1800 static const int ad7376_udelay = 1;
1802 register_bits = cal_enable_bits(dev) | SELECT_TRIMPOT_BIT;
1803 udelay(ad7376_udelay);
1804 outw(register_bits, devpriv->control_status + CALIBRATION_REG);
1806 write_calibration_bitstream(dev, register_bits, bitstream,
1809 udelay(ad7376_udelay);
1810 outw(cal_enable_bits(dev), devpriv->control_status + CALIBRATION_REG);
1817 * ch 1 : adc postgain offset */
1818 static int trimpot_8402_write(struct comedi_device *dev, unsigned int channel,
1821 static const int bitstream_length = 10;
1822 unsigned int bitstream = ((channel & 0x3) << 8) | (value & 0xff);
1823 unsigned int register_bits;
1824 static const int ad8402_udelay = 1;
1826 register_bits = cal_enable_bits(dev) | SELECT_TRIMPOT_BIT;
1827 udelay(ad8402_udelay);
1828 outw(register_bits, devpriv->control_status + CALIBRATION_REG);
1830 write_calibration_bitstream(dev, register_bits, bitstream,
1833 udelay(ad8402_udelay);
1834 outw(cal_enable_bits(dev), devpriv->control_status + CALIBRATION_REG);
1839 static int wait_for_nvram_ready(unsigned long s5933_base_addr)
1841 static const int timeout = 1000;
1844 for (i = 0; i < timeout; i++) {
1845 if ((inb(s5933_base_addr +
1846 AMCC_OP_REG_MCSR_NVCMD) & MCSR_NV_BUSY)
1854 static int nvram_read(struct comedi_device *dev, unsigned int address,
1857 unsigned long iobase = devpriv->s5933_config;
1859 if (wait_for_nvram_ready(iobase) < 0)
1862 outb(MCSR_NV_ENABLE | MCSR_NV_LOAD_LOW_ADDR,
1863 iobase + AMCC_OP_REG_MCSR_NVCMD);
1864 outb(address & 0xff, iobase + AMCC_OP_REG_MCSR_NVDATA);
1865 outb(MCSR_NV_ENABLE | MCSR_NV_LOAD_HIGH_ADDR,
1866 iobase + AMCC_OP_REG_MCSR_NVCMD);
1867 outb((address >> 8) & 0xff, iobase + AMCC_OP_REG_MCSR_NVDATA);
1868 outb(MCSR_NV_ENABLE | MCSR_NV_READ, iobase + AMCC_OP_REG_MCSR_NVCMD);
1870 if (wait_for_nvram_ready(iobase) < 0)
1873 *data = inb(iobase + AMCC_OP_REG_MCSR_NVDATA);
1879 * A convenient macro that defines init_module() and cleanup_module(),
1882 COMEDI_PCI_INITCLEANUP(driver_cb_pcidas, cb_pcidas_pci_table);