2 comedi/drivers/cb_pcidas64.c
3 This is a driver for the ComputerBoards/MeasurementComputing PCI-DAS
4 64xx, 60xx, and 4020 cards.
6 Author: Frank Mori Hess <fmhess@users.sourceforge.net>
7 Copyright (C) 2001, 2002 Frank Mori Hess
9 Thanks also go to the following people:
11 Steve Rosenbluth, for providing the source code for
12 his pci-das6402 driver, and source code for working QNX pci-6402
13 drivers by Greg Laird and Mariusz Bogacz. None of the code was
14 used directly here, but it was useful as an additional source of
15 documentation on how to program the boards.
17 John Sims, for much testing and feedback on pcidas-4020 support.
19 COMEDI - Linux Control and Measurement Device Interface
20 Copyright (C) 1997-8 David A. Schleef <ds@schleef.org>
22 This program is free software; you can redistribute it and/or modify
23 it under the terms of the GNU General Public License as published by
24 the Free Software Foundation; either version 2 of the License, or
25 (at your option) any later version.
27 This program is distributed in the hope that it will be useful,
28 but WITHOUT ANY WARRANTY; without even the implied warranty of
29 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 GNU General Public License for more details.
32 You should have received a copy of the GNU General Public License
33 along with this program; if not, write to the Free Software
34 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
36 ************************************************************************/
41 Description: MeasurementComputing PCI-DAS64xx, 60XX, and 4020 series with the PLX 9080 PCI controller
42 Author: Frank Mori Hess <fmhess@users.sourceforge.net>
45 Devices: [Measurement Computing] PCI-DAS6402/16 (cb_pcidas64),
46 PCI-DAS6402/12, PCI-DAS64/M1/16, PCI-DAS64/M2/16,
47 PCI-DAS64/M3/16, PCI-DAS6402/16/JR, PCI-DAS64/M1/16/JR,
48 PCI-DAS64/M2/16/JR, PCI-DAS64/M3/16/JR, PCI-DAS64/M1/14,
49 PCI-DAS64/M2/14, PCI-DAS64/M3/14, PCI-DAS6013, PCI-DAS6014,
50 PCI-DAS6023, PCI-DAS6025, PCI-DAS6030,
51 PCI-DAS6031, PCI-DAS6032, PCI-DAS6033, PCI-DAS6034,
52 PCI-DAS6035, PCI-DAS6036, PCI-DAS6040, PCI-DAS6052,
53 PCI-DAS6070, PCI-DAS6071, PCI-DAS4020/12
55 Configuration options:
56 [0] - PCI bus of device (optional)
57 [1] - PCI slot of device (optional)
59 These boards may be autocalibrated with the comedi_calibrate utility.
61 To select the bnc trigger input on the 4020 (instead of the dio input),
62 specify a nonzero channel in the chanspec. If you wish to use an external
63 master clock on the 4020, you may do so by setting the scan_begin_src
64 to TRIG_OTHER, and using an INSN_CONFIG_TIMER_1 configuration insn
65 to configure the divisor to use for the external clock.
67 Some devices are not identified because the PCI device IDs are not yet
68 known. If you have such a board, please file a bug report at
69 https://bugs.comedi.org.
76 make it return error if user attempts an ai command that uses the
77 external queue, and an ao command simultaneously
78 user counter subdevice
79 there are a number of boards this driver will support when they are
80 fully released, but does not yet since the pci device id numbers
81 are not yet available.
82 support prescaled 100khz clock for slow pacing (not available on 6000 series?)
83 make ao fifo size adjustable like ai fifo
86 #include "../comedidev.h"
87 #include <linux/delay.h>
88 #include <linux/interrupt.h>
89 #include <asm/system.h>
91 #include "comedi_pci.h"
95 #include "comedi_fc.h"
97 #undef PCIDAS64_DEBUG /* disable debugging code */
98 /* #define PCIDAS64_DEBUG enable debugging code */
100 #ifdef PCIDAS64_DEBUG
101 #define DEBUG_PRINT(format, args...) printk(format , ## args)
103 #define DEBUG_PRINT(format, args...)
106 #define TIMER_BASE 25 /* 40MHz master clock */
107 #define PRESCALED_TIMER_BASE 10000 /* 100kHz 'prescaled' clock for slow aquisition, maybe I'll support this someday */
108 #define DMA_BUFFER_SIZE 0x1000
110 #define PCI_VENDOR_ID_COMPUTERBOARDS 0x1307
112 /* maximum value that can be loaded into board's 24-bit counters*/
113 static const int max_counter_value = 0xffffff;
115 /* PCI-DAS64xxx base addresses */
117 /* indices of base address regions */
118 enum base_address_regions {
119 PLX9080_BADDRINDEX = 0,
121 DIO_COUNTER_BADDRINDEX = 3,
124 /* priv(dev)->main_iobase registers */
125 enum write_only_registers {
126 INTR_ENABLE_REG = 0x0, /* interrupt enable register */
127 HW_CONFIG_REG = 0x2, /* hardware config register */
129 DAQ_ATRIG_LOW_4020_REG = 0xc,
130 ADC_CONTROL0_REG = 0x10, /* adc control register 0 */
131 ADC_CONTROL1_REG = 0x12, /* adc control register 1 */
132 CALIBRATION_REG = 0x14,
133 ADC_SAMPLE_INTERVAL_LOWER_REG = 0x16, /* lower 16 bits of adc sample interval counter */
134 ADC_SAMPLE_INTERVAL_UPPER_REG = 0x18, /* upper 8 bits of adc sample interval counter */
135 ADC_DELAY_INTERVAL_LOWER_REG = 0x1a, /* lower 16 bits of delay interval counter */
136 ADC_DELAY_INTERVAL_UPPER_REG = 0x1c, /* upper 8 bits of delay interval counter */
137 ADC_COUNT_LOWER_REG = 0x1e, /* lower 16 bits of hardware conversion/scan counter */
138 ADC_COUNT_UPPER_REG = 0x20, /* upper 8 bits of hardware conversion/scan counter */
139 ADC_START_REG = 0x22, /* software trigger to start aquisition */
140 ADC_CONVERT_REG = 0x24, /* initiates single conversion */
141 ADC_QUEUE_CLEAR_REG = 0x26, /* clears adc queue */
142 ADC_QUEUE_LOAD_REG = 0x28, /* loads adc queue */
143 ADC_BUFFER_CLEAR_REG = 0x2a,
144 ADC_QUEUE_HIGH_REG = 0x2c, /* high channel for internal queue, use adc_chan_bits() inline above */
145 DAC_CONTROL0_REG = 0x50, /* dac control register 0 */
146 DAC_CONTROL1_REG = 0x52, /* dac control register 0 */
147 DAC_SAMPLE_INTERVAL_LOWER_REG = 0x54, /* lower 16 bits of dac sample interval counter */
148 DAC_SAMPLE_INTERVAL_UPPER_REG = 0x56, /* upper 8 bits of dac sample interval counter */
149 DAC_SELECT_REG = 0x60,
150 DAC_START_REG = 0x64,
151 DAC_BUFFER_CLEAR_REG = 0x66, /* clear dac buffer */
153 static inline unsigned int dac_convert_reg(unsigned int channel)
155 return 0x70 + (2 * (channel & 0x1));
158 static inline unsigned int dac_lsb_4020_reg(unsigned int channel)
160 return 0x70 + (4 * (channel & 0x1));
163 static inline unsigned int dac_msb_4020_reg(unsigned int channel)
165 return 0x72 + (4 * (channel & 0x1));
168 enum read_only_registers {
169 HW_STATUS_REG = 0x0, /* hardware status register, reading this apparently clears pending interrupts as well */
170 PIPE1_READ_REG = 0x4,
171 ADC_READ_PNTR_REG = 0x8,
172 LOWER_XFER_REG = 0x10,
173 ADC_WRITE_PNTR_REG = 0xc,
177 enum read_write_registers {
178 I8255_4020_REG = 0x48, /* 8255 offset, for 4020 only */
179 ADC_QUEUE_FIFO_REG = 0x100, /* external channel/gain queue, uses same bits as ADC_QUEUE_LOAD_REG */
180 ADC_FIFO_REG = 0x200, /* adc data fifo */
181 DAC_FIFO_REG = 0x300, /* dac data fifo, has weird interactions with external channel queue */
184 /* priv(dev)->dio_counter_iobase registers */
185 enum dio_counter_registers {
186 DIO_8255_OFFSET = 0x0,
189 DIO_DIRECTION_60XX_REG = 0x40,
190 DIO_DATA_60XX_REG = 0x48,
193 /* bit definitions for write-only registers */
195 enum intr_enable_contents {
196 ADC_INTR_SRC_MASK = 0x3, /* bits that set adc interrupt source */
197 ADC_INTR_QFULL_BITS = 0x0, /* interrupt fifo quater full */
198 ADC_INTR_EOC_BITS = 0x1, /* interrupt end of conversion */
199 ADC_INTR_EOSCAN_BITS = 0x2, /* interrupt end of scan */
200 ADC_INTR_EOSEQ_BITS = 0x3, /* interrupt end of sequence (probably wont use this it's pretty fancy) */
201 EN_ADC_INTR_SRC_BIT = 0x4, /* enable adc interrupt source */
202 EN_ADC_DONE_INTR_BIT = 0x8, /* enable adc aquisition done interrupt */
203 DAC_INTR_SRC_MASK = 0x30,
204 DAC_INTR_QEMPTY_BITS = 0x0,
205 DAC_INTR_HIGH_CHAN_BITS = 0x10,
206 EN_DAC_INTR_SRC_BIT = 0x40, /* enable dac interrupt source */
207 EN_DAC_DONE_INTR_BIT = 0x80,
208 EN_ADC_ACTIVE_INTR_BIT = 0x200, /* enable adc active interrupt */
209 EN_ADC_STOP_INTR_BIT = 0x400, /* enable adc stop trigger interrupt */
210 EN_DAC_ACTIVE_INTR_BIT = 0x800, /* enable dac active interrupt */
211 EN_DAC_UNDERRUN_BIT = 0x4000, /* enable dac underrun status bit */
212 EN_ADC_OVERRUN_BIT = 0x8000, /* enable adc overrun status bit */
215 enum hw_config_contents {
216 MASTER_CLOCK_4020_MASK = 0x3, /* bits that specify master clock source for 4020 */
217 INTERNAL_CLOCK_4020_BITS = 0x1, /* use 40 MHz internal master clock for 4020 */
218 BNC_CLOCK_4020_BITS = 0x2, /* use BNC input for master clock */
219 EXT_CLOCK_4020_BITS = 0x3, /* use dio input for master clock */
220 EXT_QUEUE_BIT = 0x200, /* use external channel/gain queue (more versatile than internal queue) */
221 SLOW_DAC_BIT = 0x400, /* use 225 nanosec strobe when loading dac instead of 50 nanosec */
222 HW_CONFIG_DUMMY_BITS = 0x2000, /* bit with unknown function yet given as default value in pci-das64 manual */
223 DMA_CH_SELECT_BIT = 0x8000, /* bit selects channels 1/0 for analog input/output, otherwise 0/1 */
224 FIFO_SIZE_REG = 0x4, /* allows adjustment of fifo sizes */
225 DAC_FIFO_SIZE_MASK = 0xff00, /* bits that set dac fifo size */
226 DAC_FIFO_BITS = 0xf800, /* 8k sample ao fifo */
228 #define DAC_FIFO_SIZE 0x2000
230 enum daq_atrig_low_4020_contents {
231 EXT_AGATE_BNC_BIT = 0x8000, /* use trig/ext clk bnc input for analog gate signal */
232 EXT_STOP_TRIG_BNC_BIT = 0x4000, /* use trig/ext clk bnc input for external stop trigger signal */
233 EXT_START_TRIG_BNC_BIT = 0x2000, /* use trig/ext clk bnc input for external start trigger signal */
235 static inline uint16_t analog_trig_low_threshold_bits(uint16_t threshold)
237 return threshold & 0xfff;
240 enum adc_control0_contents {
241 ADC_GATE_SRC_MASK = 0x3, /* bits that select gate */
242 ADC_SOFT_GATE_BITS = 0x1, /* software gate */
243 ADC_EXT_GATE_BITS = 0x2, /* external digital gate */
244 ADC_ANALOG_GATE_BITS = 0x3, /* analog level gate */
245 ADC_GATE_LEVEL_BIT = 0x4, /* level-sensitive gate (for digital) */
246 ADC_GATE_POLARITY_BIT = 0x8, /* gate active low */
247 ADC_START_TRIG_SOFT_BITS = 0x10,
248 ADC_START_TRIG_EXT_BITS = 0x20,
249 ADC_START_TRIG_ANALOG_BITS = 0x30,
250 ADC_START_TRIG_MASK = 0x30,
251 ADC_START_TRIG_FALLING_BIT = 0x40, /* trig 1 uses falling edge */
252 ADC_EXT_CONV_FALLING_BIT = 0x800, /* external pacing uses falling edge */
253 ADC_SAMPLE_COUNTER_EN_BIT = 0x1000, /* enable hardware scan counter */
254 ADC_DMA_DISABLE_BIT = 0x4000, /* disables dma */
255 ADC_ENABLE_BIT = 0x8000, /* master adc enable */
258 enum adc_control1_contents {
259 ADC_QUEUE_CONFIG_BIT = 0x1, /* should be set for boards with > 16 channels */
260 CONVERT_POLARITY_BIT = 0x10,
261 EOC_POLARITY_BIT = 0x20,
262 ADC_SW_GATE_BIT = 0x40, /* software gate of adc */
263 ADC_DITHER_BIT = 0x200, /* turn on extra noise for dithering */
264 RETRIGGER_BIT = 0x800,
265 ADC_LO_CHANNEL_4020_MASK = 0x300,
266 ADC_HI_CHANNEL_4020_MASK = 0xc00,
267 TWO_CHANNEL_4020_BITS = 0x1000, /* two channel mode for 4020 */
268 FOUR_CHANNEL_4020_BITS = 0x2000, /* four channel mode for 4020 */
269 CHANNEL_MODE_4020_MASK = 0x3000,
270 ADC_MODE_MASK = 0xf000,
272 static inline uint16_t adc_lo_chan_4020_bits(unsigned int channel)
274 return (channel & 0x3) << 8;
277 static inline uint16_t adc_hi_chan_4020_bits(unsigned int channel)
279 return (channel & 0x3) << 10;
282 static inline uint16_t adc_mode_bits(unsigned int mode)
284 return (mode & 0xf) << 12;
287 enum calibration_contents {
288 SELECT_8800_BIT = 0x1,
289 SELECT_8402_64XX_BIT = 0x2,
290 SELECT_1590_60XX_BIT = 0x2,
291 CAL_EN_64XX_BIT = 0x40, /* calibration enable for 64xx series */
292 SERIAL_DATA_IN_BIT = 0x80,
293 SERIAL_CLOCK_BIT = 0x100,
294 CAL_EN_60XX_BIT = 0x200, /* calibration enable for 60xx series */
295 CAL_GAIN_BIT = 0x800,
297 /* calibration sources for 6025 are:
307 static inline uint16_t adc_src_bits(unsigned int source)
309 return (source & 0xf) << 3;
312 static inline uint16_t adc_convert_chan_4020_bits(unsigned int channel)
314 return (channel & 0x3) << 8;
317 enum adc_queue_load_contents {
318 UNIP_BIT = 0x800, /* unipolar/bipolar bit */
319 ADC_SE_DIFF_BIT = 0x1000, /* single-ended/ differential bit */
320 ADC_COMMON_BIT = 0x2000, /* non-referenced single-ended (common-mode input) */
321 QUEUE_EOSEQ_BIT = 0x4000, /* queue end of sequence */
322 QUEUE_EOSCAN_BIT = 0x8000, /* queue end of scan */
324 static inline uint16_t adc_chan_bits(unsigned int channel)
326 return channel & 0x3f;
329 enum dac_control0_contents {
330 DAC_ENABLE_BIT = 0x8000, /* dac controller enable bit */
331 DAC_CYCLIC_STOP_BIT = 0x4000,
332 DAC_WAVEFORM_MODE_BIT = 0x100,
333 DAC_EXT_UPDATE_FALLING_BIT = 0x80,
334 DAC_EXT_UPDATE_ENABLE_BIT = 0x40,
335 WAVEFORM_TRIG_MASK = 0x30,
336 WAVEFORM_TRIG_DISABLED_BITS = 0x0,
337 WAVEFORM_TRIG_SOFT_BITS = 0x10,
338 WAVEFORM_TRIG_EXT_BITS = 0x20,
339 WAVEFORM_TRIG_ADC1_BITS = 0x30,
340 WAVEFORM_TRIG_FALLING_BIT = 0x8,
341 WAVEFORM_GATE_LEVEL_BIT = 0x4,
342 WAVEFORM_GATE_ENABLE_BIT = 0x2,
343 WAVEFORM_GATE_SELECT_BIT = 0x1,
346 enum dac_control1_contents {
347 DAC_WRITE_POLARITY_BIT = 0x800, /* board-dependent setting */
348 DAC1_EXT_REF_BIT = 0x200,
349 DAC0_EXT_REF_BIT = 0x100,
350 DAC_OUTPUT_ENABLE_BIT = 0x80, /* dac output enable bit */
351 DAC_UPDATE_POLARITY_BIT = 0x40, /* board-dependent setting */
352 DAC_SW_GATE_BIT = 0x20,
353 DAC1_UNIPOLAR_BIT = 0x8,
354 DAC0_UNIPOLAR_BIT = 0x2,
357 /* bit definitions for read-only registers */
358 enum hw_status_contents {
359 DAC_UNDERRUN_BIT = 0x1,
360 ADC_OVERRUN_BIT = 0x2,
361 DAC_ACTIVE_BIT = 0x4,
362 ADC_ACTIVE_BIT = 0x8,
363 DAC_INTR_PENDING_BIT = 0x10,
364 ADC_INTR_PENDING_BIT = 0x20,
367 EXT_INTR_PENDING_BIT = 0x100,
368 ADC_STOP_BIT = 0x200,
370 static inline uint16_t pipe_full_bits(uint16_t hw_status_bits)
372 return (hw_status_bits >> 10) & 0x3;
375 static inline unsigned int dma_chain_flag_bits(uint16_t prepost_bits)
377 return (prepost_bits >> 6) & 0x3;
380 static inline unsigned int adc_upper_read_ptr_code(uint16_t prepost_bits)
382 return (prepost_bits >> 12) & 0x3;
385 static inline unsigned int adc_upper_write_ptr_code(uint16_t prepost_bits)
387 return (prepost_bits >> 14) & 0x3;
390 /* I2C addresses for 4020 */
392 RANGE_CAL_I2C_ADDR = 0x20,
393 CALDAC0_I2C_ADDR = 0xc,
394 CALDAC1_I2C_ADDR = 0xd,
397 enum range_cal_i2c_contents {
398 ADC_SRC_4020_MASK = 0x70, /* bits that set what source the adc converter measures */
399 BNC_TRIG_THRESHOLD_0V_BIT = 0x80, /* make bnc trig/ext clock threshold 0V instead of 2.5V */
401 static inline uint8_t adc_src_4020_bits(unsigned int source)
403 return (source << 4) & ADC_SRC_4020_MASK;
406 static inline uint8_t attenuate_bit(unsigned int channel)
408 /* attenuate channel (+-5V input range) */
409 return 1 << (channel & 0x3);
412 /* analog input ranges for 64xx boards */
413 static const struct comedi_lrange ai_ranges_64xx = {
427 /* analog input ranges for 60xx boards */
428 static const struct comedi_lrange ai_ranges_60xx = {
438 /* analog input ranges for 6030, etc boards */
439 static const struct comedi_lrange ai_ranges_6030 = {
459 /* analog input ranges for 6052, etc boards */
460 static const struct comedi_lrange ai_ranges_6052 = {
481 /* analog input ranges for 4020 board */
482 static const struct comedi_lrange ai_ranges_4020 = {
490 /* analog output ranges */
491 static const struct comedi_lrange ao_ranges_64xx = {
501 static const int ao_range_code_64xx[] = {
508 static const struct comedi_lrange ao_ranges_60xx = {
515 static const int ao_range_code_60xx[] = {
519 static const struct comedi_lrange ao_ranges_6030 = {
527 static const int ao_range_code_6030[] = {
532 static const struct comedi_lrange ao_ranges_4020 = {
540 static const int ao_range_code_4020[] = {
545 enum register_layout {
551 struct hw_fifo_info {
552 unsigned int num_segments;
553 unsigned int max_segment_length;
554 unsigned int sample_packing_ratio;
555 uint16_t fifo_size_reg_mask;
558 struct pcidas64_board {
560 int device_id; /* pci device id */
561 int ai_se_chans; /* number of ai inputs in single-ended mode */
562 int ai_bits; /* analog input resolution */
563 int ai_speed; /* fastest conversion period in ns */
564 const struct comedi_lrange *ai_range_table;
565 int ao_nchan; /* number of analog out channels */
566 int ao_bits; /* analog output resolution */
567 int ao_scan_speed; /* analog output speed (for a scan, not conversion) */
568 const struct comedi_lrange *ao_range_table;
569 const int *ao_range_code;
570 const struct hw_fifo_info *const ai_fifo;
571 enum register_layout layout; /* different board families have slightly different registers */
575 static const struct hw_fifo_info ai_fifo_4020 = {
577 .max_segment_length = 0x8000,
578 .sample_packing_ratio = 2,
579 .fifo_size_reg_mask = 0x7f,
582 static const struct hw_fifo_info ai_fifo_64xx = {
584 .max_segment_length = 0x800,
585 .sample_packing_ratio = 1,
586 .fifo_size_reg_mask = 0x3f,
589 static const struct hw_fifo_info ai_fifo_60xx = {
591 .max_segment_length = 0x800,
592 .sample_packing_ratio = 1,
593 .fifo_size_reg_mask = 0x7f,
596 /* maximum number of dma transfers we will chain together into a ring
597 * (and the maximum number of dma buffers we maintain) */
598 #define MAX_AI_DMA_RING_COUNT (0x80000 / DMA_BUFFER_SIZE)
599 #define MIN_AI_DMA_RING_COUNT (0x10000 / DMA_BUFFER_SIZE)
600 #define AO_DMA_RING_COUNT (0x10000 / DMA_BUFFER_SIZE)
601 static inline unsigned int ai_dma_ring_count(struct pcidas64_board *board)
603 if (board->layout == LAYOUT_4020)
604 return MAX_AI_DMA_RING_COUNT;
606 return MIN_AI_DMA_RING_COUNT;
609 static const int bytes_in_sample = 2;
611 static const struct pcidas64_board pcidas64_boards[] = {
613 .name = "pci-das6402/16",
620 .ao_scan_speed = 10000,
621 .layout = LAYOUT_64XX,
622 .ai_range_table = &ai_ranges_64xx,
623 .ao_range_table = &ao_ranges_64xx,
624 .ao_range_code = ao_range_code_64xx,
625 .ai_fifo = &ai_fifo_64xx,
629 .name = "pci-das6402/12", /* XXX check */
636 .ao_scan_speed = 10000,
637 .layout = LAYOUT_64XX,
638 .ai_range_table = &ai_ranges_64xx,
639 .ao_range_table = &ao_ranges_64xx,
640 .ao_range_code = ao_range_code_64xx,
641 .ai_fifo = &ai_fifo_64xx,
645 .name = "pci-das64/m1/16",
652 .ao_scan_speed = 10000,
653 .layout = LAYOUT_64XX,
654 .ai_range_table = &ai_ranges_64xx,
655 .ao_range_table = &ao_ranges_64xx,
656 .ao_range_code = ao_range_code_64xx,
657 .ai_fifo = &ai_fifo_64xx,
661 .name = "pci-das64/m2/16",
668 .ao_scan_speed = 10000,
669 .layout = LAYOUT_64XX,
670 .ai_range_table = &ai_ranges_64xx,
671 .ao_range_table = &ao_ranges_64xx,
672 .ao_range_code = ao_range_code_64xx,
673 .ai_fifo = &ai_fifo_64xx,
677 .name = "pci-das64/m3/16",
684 .ao_scan_speed = 10000,
685 .layout = LAYOUT_64XX,
686 .ai_range_table = &ai_ranges_64xx,
687 .ao_range_table = &ao_ranges_64xx,
688 .ao_range_code = ao_range_code_64xx,
689 .ai_fifo = &ai_fifo_64xx,
693 .name = "pci-das6013",
700 .layout = LAYOUT_60XX,
701 .ai_range_table = &ai_ranges_60xx,
702 .ao_range_table = &ao_ranges_60xx,
703 .ao_range_code = ao_range_code_60xx,
704 .ai_fifo = &ai_fifo_60xx,
708 .name = "pci-das6014",
715 .ao_scan_speed = 100000,
716 .layout = LAYOUT_60XX,
717 .ai_range_table = &ai_ranges_60xx,
718 .ao_range_table = &ao_ranges_60xx,
719 .ao_range_code = ao_range_code_60xx,
720 .ai_fifo = &ai_fifo_60xx,
724 .name = "pci-das6023",
730 .ao_scan_speed = 100000,
731 .layout = LAYOUT_60XX,
732 .ai_range_table = &ai_ranges_60xx,
733 .ao_range_table = &ao_ranges_60xx,
734 .ao_range_code = ao_range_code_60xx,
735 .ai_fifo = &ai_fifo_60xx,
739 .name = "pci-das6025",
746 .ao_scan_speed = 100000,
747 .layout = LAYOUT_60XX,
748 .ai_range_table = &ai_ranges_60xx,
749 .ao_range_table = &ao_ranges_60xx,
750 .ao_range_code = ao_range_code_60xx,
751 .ai_fifo = &ai_fifo_60xx,
755 .name = "pci-das6030",
762 .ao_scan_speed = 10000,
763 .layout = LAYOUT_60XX,
764 .ai_range_table = &ai_ranges_6030,
765 .ao_range_table = &ao_ranges_6030,
766 .ao_range_code = ao_range_code_6030,
767 .ai_fifo = &ai_fifo_60xx,
771 .name = "pci-das6031",
778 .ao_scan_speed = 10000,
779 .layout = LAYOUT_60XX,
780 .ai_range_table = &ai_ranges_6030,
781 .ao_range_table = &ao_ranges_6030,
782 .ao_range_code = ao_range_code_6030,
783 .ai_fifo = &ai_fifo_60xx,
787 .name = "pci-das6032",
793 .layout = LAYOUT_60XX,
794 .ai_range_table = &ai_ranges_6030,
795 .ai_fifo = &ai_fifo_60xx,
799 .name = "pci-das6033",
805 .layout = LAYOUT_60XX,
806 .ai_range_table = &ai_ranges_6030,
807 .ai_fifo = &ai_fifo_60xx,
811 .name = "pci-das6034",
818 .layout = LAYOUT_60XX,
819 .ai_range_table = &ai_ranges_60xx,
820 .ai_fifo = &ai_fifo_60xx,
824 .name = "pci-das6035",
831 .ao_scan_speed = 100000,
832 .layout = LAYOUT_60XX,
833 .ai_range_table = &ai_ranges_60xx,
834 .ao_range_table = &ao_ranges_60xx,
835 .ao_range_code = ao_range_code_60xx,
836 .ai_fifo = &ai_fifo_60xx,
840 .name = "pci-das6036",
847 .ao_scan_speed = 100000,
848 .layout = LAYOUT_60XX,
849 .ai_range_table = &ai_ranges_60xx,
850 .ao_range_table = &ao_ranges_60xx,
851 .ao_range_code = ao_range_code_60xx,
852 .ai_fifo = &ai_fifo_60xx,
856 .name = "pci-das6040",
863 .ao_scan_speed = 1000,
864 .layout = LAYOUT_60XX,
865 .ai_range_table = &ai_ranges_6052,
866 .ao_range_table = &ao_ranges_6030,
867 .ao_range_code = ao_range_code_6030,
868 .ai_fifo = &ai_fifo_60xx,
872 .name = "pci-das6052",
879 .ao_scan_speed = 3333,
880 .layout = LAYOUT_60XX,
881 .ai_range_table = &ai_ranges_6052,
882 .ao_range_table = &ao_ranges_6030,
883 .ao_range_code = ao_range_code_6030,
884 .ai_fifo = &ai_fifo_60xx,
888 .name = "pci-das6070",
895 .ao_scan_speed = 1000,
896 .layout = LAYOUT_60XX,
897 .ai_range_table = &ai_ranges_6052,
898 .ao_range_table = &ao_ranges_6030,
899 .ao_range_code = ao_range_code_6030,
900 .ai_fifo = &ai_fifo_60xx,
904 .name = "pci-das6071",
911 .ao_scan_speed = 1000,
912 .layout = LAYOUT_60XX,
913 .ai_range_table = &ai_ranges_6052,
914 .ao_range_table = &ao_ranges_6030,
915 .ao_range_code = ao_range_code_6030,
916 .ai_fifo = &ai_fifo_60xx,
920 .name = "pci-das4020/12",
927 .ao_scan_speed = 0, /* no hardware pacing on ao */
928 .layout = LAYOUT_4020,
929 .ai_range_table = &ai_ranges_4020,
930 .ao_range_table = &ao_ranges_4020,
931 .ao_range_code = ao_range_code_4020,
932 .ai_fifo = &ai_fifo_4020,
937 .name = "pci-das6402/16/jr",
938 .device_id = 0 /* XXX, */
943 .ao_scan_speed = 10000,
944 .layout = LAYOUT_64XX,
945 .ai_range_table = &ai_ranges_64xx,
946 .ai_fifo = ai_fifo_64xx,
950 .name = "pci-das64/m1/16/jr",
951 .device_id = 0 /* XXX, */
956 .ao_scan_speed = 10000,
957 .layout = LAYOUT_64XX,
958 .ai_range_table = &ai_ranges_64xx,
959 .ai_fifo = ai_fifo_64xx,
963 .name = "pci-das64/m2/16/jr",
964 .device_id = 0 /* XXX, */
969 .ao_scan_speed = 10000,
970 .layout = LAYOUT_64XX,
971 .ai_range_table = &ai_ranges_64xx,
972 .ai_fifo = ai_fifo_64xx,
976 .name = "pci-das64/m3/16/jr",
977 .device_id = 0 /* XXX, */
982 .ao_scan_speed = 10000,
983 .layout = LAYOUT_64XX,
984 .ai_range_table = &ai_ranges_64xx,
985 .ai_fifo = ai_fifo_64xx,
989 .name = "pci-das64/m1/14",
990 .device_id = 0, /* XXX */
995 .ao_scan_speed = 10000,
996 .layout = LAYOUT_64XX,
997 .ai_range_table = &ai_ranges_64xx,
998 .ai_fifo = ai_fifo_64xx,
1002 .name = "pci-das64/m2/14",
1003 .device_id = 0, /* XXX */
1008 .ao_scan_speed = 10000,
1009 .layout = LAYOUT_64XX,
1010 .ai_range_table = &ai_ranges_64xx,
1011 .ai_fifo = ai_fifo_64xx,
1015 .name = "pci-das64/m3/14",
1016 .device_id = 0, /* XXX */
1021 .ao_scan_speed = 10000,
1022 .layout = LAYOUT_64XX,
1023 .ai_range_table = &ai_ranges_64xx,
1024 .ai_fifo = ai_fifo_64xx,
1030 static DEFINE_PCI_DEVICE_TABLE(pcidas64_pci_table) = {
1032 PCI_VENDOR_ID_COMPUTERBOARDS, 0x001d, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
1034 PCI_VENDOR_ID_COMPUTERBOARDS, 0x001e, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
1036 PCI_VENDOR_ID_COMPUTERBOARDS, 0x0035, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
1038 PCI_VENDOR_ID_COMPUTERBOARDS, 0x0036, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
1040 PCI_VENDOR_ID_COMPUTERBOARDS, 0x0037, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
1042 PCI_VENDOR_ID_COMPUTERBOARDS, 0x0052, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
1044 PCI_VENDOR_ID_COMPUTERBOARDS, 0x005d, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
1046 PCI_VENDOR_ID_COMPUTERBOARDS, 0x005e, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
1048 PCI_VENDOR_ID_COMPUTERBOARDS, 0x005f, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
1050 PCI_VENDOR_ID_COMPUTERBOARDS, 0x0061, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
1052 PCI_VENDOR_ID_COMPUTERBOARDS, 0x0062, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
1054 PCI_VENDOR_ID_COMPUTERBOARDS, 0x0063, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
1056 PCI_VENDOR_ID_COMPUTERBOARDS, 0x0064, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
1058 PCI_VENDOR_ID_COMPUTERBOARDS, 0x0066, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
1060 PCI_VENDOR_ID_COMPUTERBOARDS, 0x0067, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
1062 PCI_VENDOR_ID_COMPUTERBOARDS, 0x0068, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
1064 PCI_VENDOR_ID_COMPUTERBOARDS, 0x006f, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
1066 PCI_VENDOR_ID_COMPUTERBOARDS, 0x0078, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
1068 PCI_VENDOR_ID_COMPUTERBOARDS, 0x0079, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
1073 MODULE_DEVICE_TABLE(pci, pcidas64_pci_table);
1075 static inline struct pcidas64_board *board(const struct comedi_device *dev)
1077 return (struct pcidas64_board *)dev->board_ptr;
1080 static inline unsigned short se_diff_bit_6xxx(struct comedi_device *dev,
1081 int use_differential)
1083 if ((board(dev)->layout == LAYOUT_64XX && !use_differential) ||
1084 (board(dev)->layout == LAYOUT_60XX && use_differential))
1085 return ADC_SE_DIFF_BIT;
1090 struct ext_clock_info {
1091 unsigned int divisor; /* master clock divisor to use for scans with external master clock */
1092 unsigned int chanspec; /* chanspec for master clock input when used as scan begin src */
1095 /* this structure is for data unique to this hardware driver. */
1096 struct pcidas64_private {
1098 struct pci_dev *hw_dev; /* pointer to board's pci_dev struct */
1099 /* base addresses (physical) */
1100 resource_size_t plx9080_phys_iobase;
1101 resource_size_t main_phys_iobase;
1102 resource_size_t dio_counter_phys_iobase;
1103 /* base addresses (ioremapped) */
1104 void __iomem *plx9080_iobase;
1105 void __iomem *main_iobase;
1106 void __iomem *dio_counter_iobase;
1107 /* local address (used by dma controller) */
1108 uint32_t local0_iobase;
1109 uint32_t local1_iobase;
1110 volatile unsigned int ai_count; /* number of analog input samples remaining */
1111 uint16_t *ai_buffer[MAX_AI_DMA_RING_COUNT]; /* dma buffers for analog input */
1112 dma_addr_t ai_buffer_bus_addr[MAX_AI_DMA_RING_COUNT]; /* physical addresses of ai dma buffers */
1113 struct plx_dma_desc *ai_dma_desc; /* array of ai dma descriptors read by plx9080, allocated to get proper alignment */
1114 dma_addr_t ai_dma_desc_bus_addr; /* physical address of ai dma descriptor array */
1115 volatile unsigned int ai_dma_index; /* index of the ai dma descriptor/buffer that is currently being used */
1116 uint16_t *ao_buffer[AO_DMA_RING_COUNT]; /* dma buffers for analog output */
1117 dma_addr_t ao_buffer_bus_addr[AO_DMA_RING_COUNT]; /* physical addresses of ao dma buffers */
1118 struct plx_dma_desc *ao_dma_desc;
1119 dma_addr_t ao_dma_desc_bus_addr;
1120 volatile unsigned int ao_dma_index; /* keeps track of buffer where the next ao sample should go */
1121 volatile unsigned long ao_count; /* number of analog output samples remaining */
1122 volatile unsigned int ao_value[2]; /* remember what the analog outputs are set to, to allow readback */
1123 unsigned int hw_revision; /* stc chip hardware revision number */
1124 volatile unsigned int intr_enable_bits; /* last bits sent to INTR_ENABLE_REG register */
1125 volatile uint16_t adc_control1_bits; /* last bits sent to ADC_CONTROL1_REG register */
1126 volatile uint16_t fifo_size_bits; /* last bits sent to FIFO_SIZE_REG register */
1127 volatile uint16_t hw_config_bits; /* last bits sent to HW_CONFIG_REG register */
1128 volatile uint16_t dac_control1_bits;
1129 volatile uint32_t plx_control_bits; /* last bits written to plx9080 control register */
1130 volatile uint32_t plx_intcsr_bits; /* last bits written to plx interrupt control and status register */
1131 volatile int calibration_source; /* index of calibration source readable through ai ch0 */
1132 volatile uint8_t i2c_cal_range_bits; /* bits written to i2c calibration/range register */
1133 volatile unsigned int ext_trig_falling; /* configure digital triggers to trigger on falling edge */
1134 /* states of various devices stored to enable read-back */
1135 unsigned int ad8402_state[2];
1136 unsigned int caldac_state[8];
1137 volatile short ai_cmd_running;
1138 unsigned int ai_fifo_segment_length;
1139 struct ext_clock_info ext_clock;
1140 short ao_bounce_buffer[DAC_FIFO_SIZE];
1143 /* inline function that makes it easier to
1144 * access the private structure.
1146 static inline struct pcidas64_private *priv(struct comedi_device *dev)
1148 return dev->private;
1152 * The comedi_driver structure tells the Comedi core module
1153 * which functions to call to configure/deconfigure (attach/detach)
1154 * the board, and also about the kernel module that contains
1157 static int attach(struct comedi_device *dev, struct comedi_devconfig *it);
1158 static int detach(struct comedi_device *dev);
1159 static struct comedi_driver driver_cb_pcidas = {
1160 .driver_name = "cb_pcidas64",
1161 .module = THIS_MODULE,
1166 static int ai_rinsn(struct comedi_device *dev, struct comedi_subdevice *s,
1167 struct comedi_insn *insn, unsigned int *data);
1168 static int ai_config_insn(struct comedi_device *dev, struct comedi_subdevice *s,
1169 struct comedi_insn *insn, unsigned int *data);
1170 static int ao_winsn(struct comedi_device *dev, struct comedi_subdevice *s,
1171 struct comedi_insn *insn, unsigned int *data);
1172 static int ao_readback_insn(struct comedi_device *dev,
1173 struct comedi_subdevice *s,
1174 struct comedi_insn *insn, unsigned int *data);
1175 static int ai_cmd(struct comedi_device *dev, struct comedi_subdevice *s);
1176 static int ai_cmdtest(struct comedi_device *dev, struct comedi_subdevice *s,
1177 struct comedi_cmd *cmd);
1178 static int ao_cmd(struct comedi_device *dev, struct comedi_subdevice *s);
1179 static int ao_inttrig(struct comedi_device *dev,
1180 struct comedi_subdevice *subdev, unsigned int trig_num);
1181 static int ao_cmdtest(struct comedi_device *dev, struct comedi_subdevice *s,
1182 struct comedi_cmd *cmd);
1183 static irqreturn_t handle_interrupt(int irq, void *d);
1184 static int ai_cancel(struct comedi_device *dev, struct comedi_subdevice *s);
1185 static int ao_cancel(struct comedi_device *dev, struct comedi_subdevice *s);
1186 static int dio_callback(int dir, int port, int data, unsigned long arg);
1187 static int dio_callback_4020(int dir, int port, int data, unsigned long arg);
1188 static int di_rbits(struct comedi_device *dev, struct comedi_subdevice *s,
1189 struct comedi_insn *insn, unsigned int *data);
1190 static int do_wbits(struct comedi_device *dev, struct comedi_subdevice *s,
1191 struct comedi_insn *insn, unsigned int *data);
1192 static int dio_60xx_config_insn(struct comedi_device *dev,
1193 struct comedi_subdevice *s,
1194 struct comedi_insn *insn, unsigned int *data);
1195 static int dio_60xx_wbits(struct comedi_device *dev, struct comedi_subdevice *s,
1196 struct comedi_insn *insn, unsigned int *data);
1197 static int calib_read_insn(struct comedi_device *dev,
1198 struct comedi_subdevice *s, struct comedi_insn *insn,
1199 unsigned int *data);
1200 static int calib_write_insn(struct comedi_device *dev,
1201 struct comedi_subdevice *s,
1202 struct comedi_insn *insn, unsigned int *data);
1203 static int ad8402_read_insn(struct comedi_device *dev,
1204 struct comedi_subdevice *s,
1205 struct comedi_insn *insn, unsigned int *data);
1206 static void ad8402_write(struct comedi_device *dev, unsigned int channel,
1207 unsigned int value);
1208 static int ad8402_write_insn(struct comedi_device *dev,
1209 struct comedi_subdevice *s,
1210 struct comedi_insn *insn, unsigned int *data);
1211 static int eeprom_read_insn(struct comedi_device *dev,
1212 struct comedi_subdevice *s,
1213 struct comedi_insn *insn, unsigned int *data);
1214 static void check_adc_timing(struct comedi_device *dev, struct comedi_cmd *cmd);
1215 static unsigned int get_divisor(unsigned int ns, unsigned int flags);
1216 static void i2c_write(struct comedi_device *dev, unsigned int address,
1217 const uint8_t * data, unsigned int length);
1218 static void caldac_write(struct comedi_device *dev, unsigned int channel,
1219 unsigned int value);
1220 static int caldac_8800_write(struct comedi_device *dev, unsigned int address,
1222 /* static int dac_1590_write(struct comedi_device *dev, unsigned int dac_a, unsigned int dac_b); */
1223 static int caldac_i2c_write(struct comedi_device *dev,
1224 unsigned int caldac_channel, unsigned int value);
1225 static void abort_dma(struct comedi_device *dev, unsigned int channel);
1226 static void disable_plx_interrupts(struct comedi_device *dev);
1227 static int set_ai_fifo_size(struct comedi_device *dev,
1228 unsigned int num_samples);
1229 static unsigned int ai_fifo_size(struct comedi_device *dev);
1230 static int set_ai_fifo_segment_length(struct comedi_device *dev,
1231 unsigned int num_entries);
1232 static void disable_ai_pacing(struct comedi_device *dev);
1233 static void disable_ai_interrupts(struct comedi_device *dev);
1234 static void enable_ai_interrupts(struct comedi_device *dev,
1235 const struct comedi_cmd *cmd);
1236 static unsigned int get_ao_divisor(unsigned int ns, unsigned int flags);
1237 static void load_ao_dma(struct comedi_device *dev,
1238 const struct comedi_cmd *cmd);
1240 static int __devinit driver_cb_pcidas_pci_probe(struct pci_dev *dev,
1241 const struct pci_device_id *ent)
1243 return comedi_pci_auto_config(dev, driver_cb_pcidas.driver_name);
1246 static void __devexit driver_cb_pcidas_pci_remove(struct pci_dev *dev)
1248 comedi_pci_auto_unconfig(dev);
1251 static struct pci_driver driver_cb_pcidas_pci_driver = {
1252 .id_table = pcidas64_pci_table,
1253 .probe = &driver_cb_pcidas_pci_probe,
1254 .remove = __devexit_p(&driver_cb_pcidas_pci_remove)
1257 static int __init driver_cb_pcidas_init_module(void)
1261 retval = comedi_driver_register(&driver_cb_pcidas);
1265 driver_cb_pcidas_pci_driver.name = (char *)driver_cb_pcidas.driver_name;
1266 return pci_register_driver(&driver_cb_pcidas_pci_driver);
1269 static void __exit driver_cb_pcidas_cleanup_module(void)
1271 pci_unregister_driver(&driver_cb_pcidas_pci_driver);
1272 comedi_driver_unregister(&driver_cb_pcidas);
1275 module_init(driver_cb_pcidas_init_module);
1276 module_exit(driver_cb_pcidas_cleanup_module);
1278 static unsigned int ai_range_bits_6xxx(const struct comedi_device *dev,
1279 unsigned int range_index)
1281 const struct comedi_krange *range =
1282 &board(dev)->ai_range_table->range[range_index];
1283 unsigned int bits = 0;
1285 switch (range->max) {
1314 comedi_error(dev, "bug! in ai_range_bits_6xxx");
1317 if (range->min == 0)
1322 static unsigned int hw_revision(const struct comedi_device *dev,
1323 uint16_t hw_status_bits)
1325 if (board(dev)->layout == LAYOUT_4020)
1326 return (hw_status_bits >> 13) & 0x7;
1328 return (hw_status_bits >> 12) & 0xf;
1331 static void set_dac_range_bits(struct comedi_device *dev,
1332 volatile uint16_t * bits, unsigned int channel,
1335 unsigned int code = board(dev)->ao_range_code[range];
1338 comedi_error(dev, "bug! bad channel?");
1340 comedi_error(dev, "bug! bad range code?");
1342 *bits &= ~(0x3 << (2 * channel));
1343 *bits |= code << (2 * channel);
1346 static inline int ao_cmd_is_supported(const struct pcidas64_board *board)
1348 return board->ao_nchan && board->layout != LAYOUT_4020;
1351 /* initialize plx9080 chip */
1352 static void init_plx9080(struct comedi_device *dev)
1355 void __iomem *plx_iobase = priv(dev)->plx9080_iobase;
1357 priv(dev)->plx_control_bits =
1358 readl(priv(dev)->plx9080_iobase + PLX_CONTROL_REG);
1361 DEBUG_PRINT(" plx interrupt status 0x%x\n",
1362 readl(plx_iobase + PLX_INTRCS_REG));
1363 DEBUG_PRINT(" plx id bits 0x%x\n", readl(plx_iobase + PLX_ID_REG));
1364 DEBUG_PRINT(" plx control reg 0x%x\n", priv(dev)->plx_control_bits);
1365 DEBUG_PRINT(" plx mode/arbitration reg 0x%x\n",
1366 readl(plx_iobase + PLX_MARB_REG));
1367 DEBUG_PRINT(" plx region0 reg 0x%x\n",
1368 readl(plx_iobase + PLX_REGION0_REG));
1369 DEBUG_PRINT(" plx region1 reg 0x%x\n",
1370 readl(plx_iobase + PLX_REGION1_REG));
1372 DEBUG_PRINT(" plx revision 0x%x\n",
1373 readl(plx_iobase + PLX_REVISION_REG));
1374 DEBUG_PRINT(" plx dma channel 0 mode 0x%x\n",
1375 readl(plx_iobase + PLX_DMA0_MODE_REG));
1376 DEBUG_PRINT(" plx dma channel 1 mode 0x%x\n",
1377 readl(plx_iobase + PLX_DMA1_MODE_REG));
1378 DEBUG_PRINT(" plx dma channel 0 pci address 0x%x\n",
1379 readl(plx_iobase + PLX_DMA0_PCI_ADDRESS_REG));
1380 DEBUG_PRINT(" plx dma channel 0 local address 0x%x\n",
1381 readl(plx_iobase + PLX_DMA0_LOCAL_ADDRESS_REG));
1382 DEBUG_PRINT(" plx dma channel 0 transfer size 0x%x\n",
1383 readl(plx_iobase + PLX_DMA0_TRANSFER_SIZE_REG));
1384 DEBUG_PRINT(" plx dma channel 0 descriptor 0x%x\n",
1385 readl(plx_iobase + PLX_DMA0_DESCRIPTOR_REG));
1386 DEBUG_PRINT(" plx dma channel 0 command status 0x%x\n",
1387 readb(plx_iobase + PLX_DMA0_CS_REG));
1388 DEBUG_PRINT(" plx dma channel 0 threshold 0x%x\n",
1389 readl(plx_iobase + PLX_DMA0_THRESHOLD_REG));
1390 DEBUG_PRINT(" plx bigend 0x%x\n", readl(plx_iobase + PLX_BIGEND_REG));
1393 bits = BIGEND_DMA0 | BIGEND_DMA1;
1397 writel(bits, priv(dev)->plx9080_iobase + PLX_BIGEND_REG);
1399 disable_plx_interrupts(dev);
1404 /* configure dma0 mode */
1406 /* enable ready input, not sure if this is necessary */
1407 bits |= PLX_DMA_EN_READYIN_BIT;
1408 /* enable bterm, not sure if this is necessary */
1409 bits |= PLX_EN_BTERM_BIT;
1410 /* enable dma chaining */
1411 bits |= PLX_EN_CHAIN_BIT;
1412 /* enable interrupt on dma done (probably don't need this, since chain never finishes) */
1413 bits |= PLX_EN_DMA_DONE_INTR_BIT;
1414 /* don't increment local address during transfers (we are transferring from a fixed fifo register) */
1415 bits |= PLX_LOCAL_ADDR_CONST_BIT;
1416 /* route dma interrupt to pci bus */
1417 bits |= PLX_DMA_INTR_PCI_BIT;
1418 /* enable demand mode */
1419 bits |= PLX_DEMAND_MODE_BIT;
1420 /* enable local burst mode */
1421 bits |= PLX_DMA_LOCAL_BURST_EN_BIT;
1422 /* 4020 uses 32 bit dma */
1423 if (board(dev)->layout == LAYOUT_4020) {
1424 bits |= PLX_LOCAL_BUS_32_WIDE_BITS;
1425 } else { /* localspace0 bus is 16 bits wide */
1426 bits |= PLX_LOCAL_BUS_16_WIDE_BITS;
1428 writel(bits, plx_iobase + PLX_DMA1_MODE_REG);
1429 if (ao_cmd_is_supported(board(dev)))
1430 writel(bits, plx_iobase + PLX_DMA0_MODE_REG);
1432 /* enable interrupts on plx 9080 */
1433 priv(dev)->plx_intcsr_bits |=
1434 ICS_AERR | ICS_PERR | ICS_PIE | ICS_PLIE | ICS_PAIE | ICS_LIE |
1435 ICS_DMA0_E | ICS_DMA1_E;
1436 writel(priv(dev)->plx_intcsr_bits,
1437 priv(dev)->plx9080_iobase + PLX_INTRCS_REG);
1440 /* Allocate and initialize the subdevice structures.
1442 static int setup_subdevices(struct comedi_device *dev)
1444 struct comedi_subdevice *s;
1445 void __iomem *dio_8255_iobase;
1448 if (alloc_subdevices(dev, 10) < 0)
1451 s = dev->subdevices + 0;
1452 /* analog input subdevice */
1453 dev->read_subdev = s;
1454 s->type = COMEDI_SUBD_AI;
1455 s->subdev_flags = SDF_READABLE | SDF_GROUND | SDF_DITHER | SDF_CMD_READ;
1456 if (board(dev)->layout == LAYOUT_60XX)
1457 s->subdev_flags |= SDF_COMMON | SDF_DIFF;
1458 else if (board(dev)->layout == LAYOUT_64XX)
1459 s->subdev_flags |= SDF_DIFF;
1460 /* XXX Number of inputs in differential mode is ignored */
1461 s->n_chan = board(dev)->ai_se_chans;
1462 s->len_chanlist = 0x2000;
1463 s->maxdata = (1 << board(dev)->ai_bits) - 1;
1464 s->range_table = board(dev)->ai_range_table;
1465 s->insn_read = ai_rinsn;
1466 s->insn_config = ai_config_insn;
1468 s->do_cmdtest = ai_cmdtest;
1469 s->cancel = ai_cancel;
1470 if (board(dev)->layout == LAYOUT_4020) {
1472 /* set adc to read from inputs (not internal calibration sources) */
1473 priv(dev)->i2c_cal_range_bits = adc_src_4020_bits(4);
1474 /* set channels to +-5 volt input ranges */
1475 for (i = 0; i < s->n_chan; i++)
1476 priv(dev)->i2c_cal_range_bits |= attenuate_bit(i);
1477 data = priv(dev)->i2c_cal_range_bits;
1478 i2c_write(dev, RANGE_CAL_I2C_ADDR, &data, sizeof(data));
1481 /* analog output subdevice */
1482 s = dev->subdevices + 1;
1483 if (board(dev)->ao_nchan) {
1484 s->type = COMEDI_SUBD_AO;
1486 SDF_READABLE | SDF_WRITABLE | SDF_GROUND | SDF_CMD_WRITE;
1487 s->n_chan = board(dev)->ao_nchan;
1488 s->maxdata = (1 << board(dev)->ao_bits) - 1;
1489 s->range_table = board(dev)->ao_range_table;
1490 s->insn_read = ao_readback_insn;
1491 s->insn_write = ao_winsn;
1492 if (ao_cmd_is_supported(board(dev))) {
1493 dev->write_subdev = s;
1494 s->do_cmdtest = ao_cmdtest;
1496 s->len_chanlist = board(dev)->ao_nchan;
1497 s->cancel = ao_cancel;
1500 s->type = COMEDI_SUBD_UNUSED;
1504 s = dev->subdevices + 2;
1505 if (board(dev)->layout == LAYOUT_64XX) {
1506 s->type = COMEDI_SUBD_DI;
1507 s->subdev_flags = SDF_READABLE;
1510 s->range_table = &range_digital;
1511 s->insn_bits = di_rbits;
1513 s->type = COMEDI_SUBD_UNUSED;
1515 /* digital output */
1516 if (board(dev)->layout == LAYOUT_64XX) {
1517 s = dev->subdevices + 3;
1518 s->type = COMEDI_SUBD_DO;
1519 s->subdev_flags = SDF_WRITABLE | SDF_READABLE;
1522 s->range_table = &range_digital;
1523 s->insn_bits = do_wbits;
1525 s->type = COMEDI_SUBD_UNUSED;
1528 s = dev->subdevices + 4;
1529 if (board(dev)->has_8255) {
1530 if (board(dev)->layout == LAYOUT_4020) {
1532 priv(dev)->main_iobase + I8255_4020_REG;
1533 subdev_8255_init(dev, s, dio_callback_4020,
1534 (unsigned long)dio_8255_iobase);
1537 priv(dev)->dio_counter_iobase + DIO_8255_OFFSET;
1538 subdev_8255_init(dev, s, dio_callback,
1539 (unsigned long)dio_8255_iobase);
1542 s->type = COMEDI_SUBD_UNUSED;
1544 /* 8 channel dio for 60xx */
1545 s = dev->subdevices + 5;
1546 if (board(dev)->layout == LAYOUT_60XX) {
1547 s->type = COMEDI_SUBD_DIO;
1548 s->subdev_flags = SDF_WRITABLE | SDF_READABLE;
1551 s->range_table = &range_digital;
1552 s->insn_config = dio_60xx_config_insn;
1553 s->insn_bits = dio_60xx_wbits;
1555 s->type = COMEDI_SUBD_UNUSED;
1558 s = dev->subdevices + 6;
1559 s->type = COMEDI_SUBD_CALIB;
1560 s->subdev_flags = SDF_READABLE | SDF_WRITABLE | SDF_INTERNAL;
1562 if (board(dev)->layout == LAYOUT_4020)
1566 s->insn_read = calib_read_insn;
1567 s->insn_write = calib_write_insn;
1568 for (i = 0; i < s->n_chan; i++)
1569 caldac_write(dev, i, s->maxdata / 2);
1571 /* 2 channel ad8402 potentiometer */
1572 s = dev->subdevices + 7;
1573 if (board(dev)->layout == LAYOUT_64XX) {
1574 s->type = COMEDI_SUBD_CALIB;
1575 s->subdev_flags = SDF_READABLE | SDF_WRITABLE | SDF_INTERNAL;
1577 s->insn_read = ad8402_read_insn;
1578 s->insn_write = ad8402_write_insn;
1580 for (i = 0; i < s->n_chan; i++)
1581 ad8402_write(dev, i, s->maxdata / 2);
1583 s->type = COMEDI_SUBD_UNUSED;
1585 /* serial EEPROM, if present */
1586 s = dev->subdevices + 8;
1587 if (readl(priv(dev)->plx9080_iobase + PLX_CONTROL_REG) & CTL_EECHK) {
1588 s->type = COMEDI_SUBD_MEMORY;
1589 s->subdev_flags = SDF_READABLE | SDF_INTERNAL;
1591 s->maxdata = 0xffff;
1592 s->insn_read = eeprom_read_insn;
1594 s->type = COMEDI_SUBD_UNUSED;
1596 /* user counter subd XXX */
1597 s = dev->subdevices + 9;
1598 s->type = COMEDI_SUBD_UNUSED;
1603 static void disable_plx_interrupts(struct comedi_device *dev)
1605 priv(dev)->plx_intcsr_bits = 0;
1606 writel(priv(dev)->plx_intcsr_bits,
1607 priv(dev)->plx9080_iobase + PLX_INTRCS_REG);
1610 static void init_stc_registers(struct comedi_device *dev)
1613 unsigned long flags;
1615 spin_lock_irqsave(&dev->spinlock, flags);
1617 /* bit should be set for 6025, although docs say boards with <= 16 chans should be cleared XXX */
1619 priv(dev)->adc_control1_bits |= ADC_QUEUE_CONFIG_BIT;
1620 writew(priv(dev)->adc_control1_bits,
1621 priv(dev)->main_iobase + ADC_CONTROL1_REG);
1623 /* 6402/16 manual says this register must be initialized to 0xff? */
1624 writew(0xff, priv(dev)->main_iobase + ADC_SAMPLE_INTERVAL_UPPER_REG);
1626 bits = SLOW_DAC_BIT | DMA_CH_SELECT_BIT;
1627 if (board(dev)->layout == LAYOUT_4020)
1628 bits |= INTERNAL_CLOCK_4020_BITS;
1629 priv(dev)->hw_config_bits |= bits;
1630 writew(priv(dev)->hw_config_bits,
1631 priv(dev)->main_iobase + HW_CONFIG_REG);
1633 writew(0, priv(dev)->main_iobase + DAQ_SYNC_REG);
1634 writew(0, priv(dev)->main_iobase + CALIBRATION_REG);
1636 spin_unlock_irqrestore(&dev->spinlock, flags);
1638 /* set fifos to maximum size */
1639 priv(dev)->fifo_size_bits |= DAC_FIFO_BITS;
1640 set_ai_fifo_segment_length(dev,
1641 board(dev)->ai_fifo->max_segment_length);
1643 priv(dev)->dac_control1_bits = DAC_OUTPUT_ENABLE_BIT;
1644 priv(dev)->intr_enable_bits = /* EN_DAC_INTR_SRC_BIT | DAC_INTR_QEMPTY_BITS | */
1645 EN_DAC_DONE_INTR_BIT | EN_DAC_UNDERRUN_BIT;
1646 writew(priv(dev)->intr_enable_bits,
1647 priv(dev)->main_iobase + INTR_ENABLE_REG);
1649 disable_ai_pacing(dev);
1652 static int alloc_and_init_dma_members(struct comedi_device *dev)
1656 /* alocate pci dma buffers */
1657 for (i = 0; i < ai_dma_ring_count(board(dev)); i++) {
1658 priv(dev)->ai_buffer[i] =
1659 pci_alloc_consistent(priv(dev)->hw_dev, DMA_BUFFER_SIZE,
1660 &priv(dev)->ai_buffer_bus_addr[i]);
1661 if (priv(dev)->ai_buffer[i] == NULL)
1665 for (i = 0; i < AO_DMA_RING_COUNT; i++) {
1666 if (ao_cmd_is_supported(board(dev))) {
1667 priv(dev)->ao_buffer[i] =
1668 pci_alloc_consistent(priv(dev)->hw_dev,
1671 ao_buffer_bus_addr[i]);
1672 if (priv(dev)->ao_buffer[i] == NULL)
1677 /* allocate dma descriptors */
1678 priv(dev)->ai_dma_desc =
1679 pci_alloc_consistent(priv(dev)->hw_dev,
1680 sizeof(struct plx_dma_desc) *
1681 ai_dma_ring_count(board(dev)),
1682 &priv(dev)->ai_dma_desc_bus_addr);
1683 if (priv(dev)->ai_dma_desc == NULL)
1686 DEBUG_PRINT("ai dma descriptors start at bus addr 0x%x\n",
1687 priv(dev)->ai_dma_desc_bus_addr);
1688 if (ao_cmd_is_supported(board(dev))) {
1689 priv(dev)->ao_dma_desc =
1690 pci_alloc_consistent(priv(dev)->hw_dev,
1691 sizeof(struct plx_dma_desc) *
1693 &priv(dev)->ao_dma_desc_bus_addr);
1694 if (priv(dev)->ao_dma_desc == NULL)
1697 DEBUG_PRINT("ao dma descriptors start at bus addr 0x%x\n",
1698 priv(dev)->ao_dma_desc_bus_addr);
1700 /* initialize dma descriptors */
1701 for (i = 0; i < ai_dma_ring_count(board(dev)); i++) {
1702 priv(dev)->ai_dma_desc[i].pci_start_addr =
1703 cpu_to_le32(priv(dev)->ai_buffer_bus_addr[i]);
1704 if (board(dev)->layout == LAYOUT_4020)
1705 priv(dev)->ai_dma_desc[i].local_start_addr =
1706 cpu_to_le32(priv(dev)->local1_iobase +
1709 priv(dev)->ai_dma_desc[i].local_start_addr =
1710 cpu_to_le32(priv(dev)->local0_iobase +
1712 priv(dev)->ai_dma_desc[i].transfer_size = cpu_to_le32(0);
1713 priv(dev)->ai_dma_desc[i].next =
1714 cpu_to_le32((priv(dev)->ai_dma_desc_bus_addr + ((i +
1719 sizeof(priv(dev)->ai_dma_desc[0])) |
1720 PLX_DESC_IN_PCI_BIT | PLX_INTR_TERM_COUNT |
1721 PLX_XFER_LOCAL_TO_PCI);
1723 if (ao_cmd_is_supported(board(dev))) {
1724 for (i = 0; i < AO_DMA_RING_COUNT; i++) {
1725 priv(dev)->ao_dma_desc[i].pci_start_addr =
1726 cpu_to_le32(priv(dev)->ao_buffer_bus_addr[i]);
1727 priv(dev)->ao_dma_desc[i].local_start_addr =
1728 cpu_to_le32(priv(dev)->local0_iobase +
1730 priv(dev)->ao_dma_desc[i].transfer_size =
1732 priv(dev)->ao_dma_desc[i].next =
1733 cpu_to_le32((priv(dev)->ao_dma_desc_bus_addr +
1734 ((i + 1) % (AO_DMA_RING_COUNT)) *
1735 sizeof(priv(dev)->ao_dma_desc[0])) |
1736 PLX_DESC_IN_PCI_BIT |
1737 PLX_INTR_TERM_COUNT);
1743 static inline void warn_external_queue(struct comedi_device *dev)
1746 "AO command and AI external channel queue cannot be used simultaneously.");
1748 "Use internal AI channel queue (channels must be consecutive and use same range/aref)");
1752 * Attach is called by the Comedi core to configure the driver
1753 * for a particular board.
1755 static int attach(struct comedi_device *dev, struct comedi_devconfig *it)
1757 struct pci_dev *pcidev = NULL;
1759 uint32_t local_range, local_decode;
1762 printk("comedi%d: cb_pcidas64\n", dev->minor);
1765 * Allocate the private structure area.
1767 if (alloc_private(dev, sizeof(struct pcidas64_private)) < 0)
1771 * Probe the device to determine what device in the series it is.
1774 for_each_pci_dev(pcidev) {
1775 /* is it not a computer boards card? */
1776 if (pcidev->vendor != PCI_VENDOR_ID_COMPUTERBOARDS)
1778 /* loop through cards supported by this driver */
1779 for (index = 0; index < ARRAY_SIZE(pcidas64_boards); index++) {
1780 if (pcidas64_boards[index].device_id != pcidev->device)
1782 /* was a particular bus/slot requested? */
1783 if (it->options[0] || it->options[1]) {
1784 /* are we on the wrong bus/slot? */
1785 if (pcidev->bus->number != it->options[0] ||
1786 PCI_SLOT(pcidev->devfn) != it->options[1]) {
1790 priv(dev)->hw_dev = pcidev;
1791 dev->board_ptr = pcidas64_boards + index;
1798 if (dev->board_ptr == NULL) {
1800 ("No supported ComputerBoards/MeasurementComputing card found\n");
1804 printk("Found %s on bus %i, slot %i\n", board(dev)->name,
1805 pcidev->bus->number, PCI_SLOT(pcidev->devfn));
1807 if (comedi_pci_enable(pcidev, driver_cb_pcidas.driver_name)) {
1809 " failed to enable PCI device and request regions\n");
1812 pci_set_master(pcidev);
1814 /* Initialize dev->board_name */
1815 dev->board_name = board(dev)->name;
1817 priv(dev)->plx9080_phys_iobase =
1818 pci_resource_start(pcidev, PLX9080_BADDRINDEX);
1819 priv(dev)->main_phys_iobase =
1820 pci_resource_start(pcidev, MAIN_BADDRINDEX);
1821 priv(dev)->dio_counter_phys_iobase =
1822 pci_resource_start(pcidev, DIO_COUNTER_BADDRINDEX);
1824 /* remap, won't work with 2.0 kernels but who cares */
1825 priv(dev)->plx9080_iobase = ioremap(priv(dev)->plx9080_phys_iobase,
1826 pci_resource_len(pcidev,
1827 PLX9080_BADDRINDEX));
1828 priv(dev)->main_iobase =
1829 ioremap(priv(dev)->main_phys_iobase,
1830 pci_resource_len(pcidev, MAIN_BADDRINDEX));
1831 priv(dev)->dio_counter_iobase =
1832 ioremap(priv(dev)->dio_counter_phys_iobase,
1833 pci_resource_len(pcidev, DIO_COUNTER_BADDRINDEX));
1835 if (!priv(dev)->plx9080_iobase || !priv(dev)->main_iobase
1836 || !priv(dev)->dio_counter_iobase) {
1837 printk(" failed to remap io memory\n");
1841 DEBUG_PRINT(" plx9080 remapped to 0x%p\n", priv(dev)->plx9080_iobase);
1842 DEBUG_PRINT(" main remapped to 0x%p\n", priv(dev)->main_iobase);
1843 DEBUG_PRINT(" diocounter remapped to 0x%p\n",
1844 priv(dev)->dio_counter_iobase);
1846 /* figure out what local addresses are */
1848 readl(priv(dev)->plx9080_iobase + PLX_LAS0RNG_REG) & LRNG_MEM_MASK;
1850 readl(priv(dev)->plx9080_iobase +
1851 PLX_LAS0MAP_REG) & local_range & LMAP_MEM_MASK;
1852 priv(dev)->local0_iobase =
1853 ((uint32_t) priv(dev)->main_phys_iobase & ~local_range) |
1856 readl(priv(dev)->plx9080_iobase + PLX_LAS1RNG_REG) & LRNG_MEM_MASK;
1858 readl(priv(dev)->plx9080_iobase +
1859 PLX_LAS1MAP_REG) & local_range & LMAP_MEM_MASK;
1860 priv(dev)->local1_iobase =
1861 ((uint32_t) priv(dev)->dio_counter_phys_iobase & ~local_range) |
1864 DEBUG_PRINT(" local 0 io addr 0x%x\n", priv(dev)->local0_iobase);
1865 DEBUG_PRINT(" local 1 io addr 0x%x\n", priv(dev)->local1_iobase);
1867 retval = alloc_and_init_dma_members(dev);
1871 priv(dev)->hw_revision =
1872 hw_revision(dev, readw(priv(dev)->main_iobase + HW_STATUS_REG));
1873 printk(" stc hardware revision %i\n", priv(dev)->hw_revision);
1875 init_stc_registers(dev);
1877 if (request_irq(pcidev->irq, handle_interrupt, IRQF_SHARED,
1878 "cb_pcidas64", dev)) {
1879 printk(" unable to allocate irq %u\n", pcidev->irq);
1882 dev->irq = pcidev->irq;
1883 printk(" irq %u\n", dev->irq);
1885 retval = setup_subdevices(dev);
1894 * _detach is called to deconfigure a device. It should deallocate
1896 * This function is also called when _attach() fails, so it should be
1897 * careful not to release resources that were not necessarily
1898 * allocated by _attach(). dev->private and dev->subdevices are
1899 * deallocated automatically by the core.
1901 static int detach(struct comedi_device *dev)
1905 printk("comedi%d: cb_pcidas: remove\n", dev->minor);
1908 free_irq(dev->irq, dev);
1910 if (priv(dev)->hw_dev) {
1911 if (priv(dev)->plx9080_iobase) {
1912 disable_plx_interrupts(dev);
1913 iounmap(priv(dev)->plx9080_iobase);
1915 if (priv(dev)->main_iobase)
1916 iounmap(priv(dev)->main_iobase);
1917 if (priv(dev)->dio_counter_iobase)
1918 iounmap(priv(dev)->dio_counter_iobase);
1919 /* free pci dma buffers */
1920 for (i = 0; i < ai_dma_ring_count(board(dev)); i++) {
1921 if (priv(dev)->ai_buffer[i])
1922 pci_free_consistent(priv(dev)->hw_dev,
1927 (dev)->ai_buffer_bus_addr
1930 for (i = 0; i < AO_DMA_RING_COUNT; i++) {
1931 if (priv(dev)->ao_buffer[i])
1932 pci_free_consistent(priv(dev)->hw_dev,
1937 (dev)->ao_buffer_bus_addr
1940 /* free dma descriptors */
1941 if (priv(dev)->ai_dma_desc)
1942 pci_free_consistent(priv(dev)->hw_dev,
1943 sizeof(struct plx_dma_desc)
1945 ai_dma_ring_count(board
1947 priv(dev)->ai_dma_desc,
1949 ai_dma_desc_bus_addr);
1950 if (priv(dev)->ao_dma_desc)
1951 pci_free_consistent(priv(dev)->hw_dev,
1952 sizeof(struct plx_dma_desc)
1953 * AO_DMA_RING_COUNT,
1954 priv(dev)->ao_dma_desc,
1956 ao_dma_desc_bus_addr);
1957 if (priv(dev)->main_phys_iobase)
1958 comedi_pci_disable(priv(dev)->hw_dev);
1960 pci_dev_put(priv(dev)->hw_dev);
1963 if (dev->subdevices)
1964 subdev_8255_cleanup(dev, dev->subdevices + 4);
1969 static int ai_rinsn(struct comedi_device *dev, struct comedi_subdevice *s,
1970 struct comedi_insn *insn, unsigned int *data)
1972 unsigned int bits = 0, n, i;
1973 unsigned int channel, range, aref;
1974 unsigned long flags;
1975 static const int timeout = 100;
1977 DEBUG_PRINT("chanspec 0x%x\n", insn->chanspec);
1978 channel = CR_CHAN(insn->chanspec);
1979 range = CR_RANGE(insn->chanspec);
1980 aref = CR_AREF(insn->chanspec);
1982 /* disable card's analog input interrupt sources and pacing */
1983 /* 4020 generates dac done interrupts even though they are disabled */
1984 disable_ai_pacing(dev);
1986 spin_lock_irqsave(&dev->spinlock, flags);
1987 if (insn->chanspec & CR_ALT_FILTER)
1988 priv(dev)->adc_control1_bits |= ADC_DITHER_BIT;
1990 priv(dev)->adc_control1_bits &= ~ADC_DITHER_BIT;
1991 writew(priv(dev)->adc_control1_bits,
1992 priv(dev)->main_iobase + ADC_CONTROL1_REG);
1993 spin_unlock_irqrestore(&dev->spinlock, flags);
1995 if (board(dev)->layout != LAYOUT_4020) {
1996 /* use internal queue */
1997 priv(dev)->hw_config_bits &= ~EXT_QUEUE_BIT;
1998 writew(priv(dev)->hw_config_bits,
1999 priv(dev)->main_iobase + HW_CONFIG_REG);
2001 /* ALT_SOURCE is internal calibration reference */
2002 if (insn->chanspec & CR_ALT_SOURCE) {
2003 unsigned int cal_en_bit;
2005 DEBUG_PRINT("reading calibration source\n");
2006 if (board(dev)->layout == LAYOUT_60XX)
2007 cal_en_bit = CAL_EN_60XX_BIT;
2009 cal_en_bit = CAL_EN_64XX_BIT;
2010 /* select internal reference source to connect to channel 0 */
2012 adc_src_bits(priv(dev)->calibration_source),
2013 priv(dev)->main_iobase + CALIBRATION_REG);
2015 /* make sure internal calibration source is turned off */
2016 writew(0, priv(dev)->main_iobase + CALIBRATION_REG);
2018 /* load internal queue */
2021 bits |= ai_range_bits_6xxx(dev, CR_RANGE(insn->chanspec));
2022 /* set single-ended / differential */
2023 bits |= se_diff_bit_6xxx(dev, aref == AREF_DIFF);
2024 if (aref == AREF_COMMON)
2025 bits |= ADC_COMMON_BIT;
2026 bits |= adc_chan_bits(channel);
2027 /* set stop channel */
2028 writew(adc_chan_bits(channel),
2029 priv(dev)->main_iobase + ADC_QUEUE_HIGH_REG);
2030 /* set start channel, and rest of settings */
2031 writew(bits, priv(dev)->main_iobase + ADC_QUEUE_LOAD_REG);
2033 uint8_t old_cal_range_bits = priv(dev)->i2c_cal_range_bits;
2035 priv(dev)->i2c_cal_range_bits &= ~ADC_SRC_4020_MASK;
2036 if (insn->chanspec & CR_ALT_SOURCE) {
2037 DEBUG_PRINT("reading calibration source\n");
2038 priv(dev)->i2c_cal_range_bits |=
2039 adc_src_4020_bits(priv(dev)->calibration_source);
2040 } else { /* select BNC inputs */
2041 priv(dev)->i2c_cal_range_bits |= adc_src_4020_bits(4);
2045 priv(dev)->i2c_cal_range_bits |= attenuate_bit(channel);
2047 priv(dev)->i2c_cal_range_bits &=
2048 ~attenuate_bit(channel);
2049 /* update calibration/range i2c register only if necessary, as it is very slow */
2050 if (old_cal_range_bits != priv(dev)->i2c_cal_range_bits) {
2051 uint8_t i2c_data = priv(dev)->i2c_cal_range_bits;
2052 i2c_write(dev, RANGE_CAL_I2C_ADDR, &i2c_data,
2056 /* 4020 manual asks that sample interval register to be set before writing to convert register.
2057 * Using somewhat arbitrary setting of 4 master clock ticks = 0.1 usec */
2059 priv(dev)->main_iobase + ADC_SAMPLE_INTERVAL_UPPER_REG);
2061 priv(dev)->main_iobase + ADC_SAMPLE_INTERVAL_LOWER_REG);
2064 for (n = 0; n < insn->n; n++) {
2066 /* clear adc buffer (inside loop for 4020 sake) */
2067 writew(0, priv(dev)->main_iobase + ADC_BUFFER_CLEAR_REG);
2069 /* trigger conversion, bits sent only matter for 4020 */
2070 writew(adc_convert_chan_4020_bits(CR_CHAN(insn->chanspec)),
2071 priv(dev)->main_iobase + ADC_CONVERT_REG);
2074 for (i = 0; i < timeout; i++) {
2075 bits = readw(priv(dev)->main_iobase + HW_STATUS_REG);
2076 DEBUG_PRINT(" pipe bits 0x%x\n", pipe_full_bits(bits));
2077 if (board(dev)->layout == LAYOUT_4020) {
2078 if (readw(priv(dev)->main_iobase +
2079 ADC_WRITE_PNTR_REG))
2082 if (pipe_full_bits(bits))
2087 DEBUG_PRINT(" looped %i times waiting for data\n", i);
2089 comedi_error(dev, " analog input read insn timed out");
2090 printk(" status 0x%x\n", bits);
2093 if (board(dev)->layout == LAYOUT_4020)
2095 readl(priv(dev)->dio_counter_iobase +
2096 ADC_FIFO_REG) & 0xffff;
2099 readw(priv(dev)->main_iobase + PIPE1_READ_REG);
2105 static int ai_config_calibration_source(struct comedi_device *dev,
2108 unsigned int source = data[1];
2109 int num_calibration_sources;
2111 if (board(dev)->layout == LAYOUT_60XX)
2112 num_calibration_sources = 16;
2114 num_calibration_sources = 8;
2115 if (source >= num_calibration_sources) {
2116 printk("invalid calibration source: %i\n", source);
2120 DEBUG_PRINT("setting calibration source to %i\n", source);
2121 priv(dev)->calibration_source = source;
2126 static int ai_config_block_size(struct comedi_device *dev, unsigned int *data)
2129 const struct hw_fifo_info *const fifo = board(dev)->ai_fifo;
2130 unsigned int block_size, requested_block_size;
2133 requested_block_size = data[1];
2135 if (requested_block_size) {
2137 requested_block_size * fifo->num_segments / bytes_in_sample;
2139 retval = set_ai_fifo_size(dev, fifo_size);
2145 block_size = ai_fifo_size(dev) / fifo->num_segments * bytes_in_sample;
2147 data[1] = block_size;
2152 static int ai_config_master_clock_4020(struct comedi_device *dev,
2155 unsigned int divisor = data[4];
2164 case COMEDI_EV_SCAN_BEGIN:
2165 priv(dev)->ext_clock.divisor = divisor;
2166 priv(dev)->ext_clock.chanspec = data[2];
2175 return retval ? retval : 5;
2178 /* XXX could add support for 60xx series */
2179 static int ai_config_master_clock(struct comedi_device *dev, unsigned int *data)
2182 switch (board(dev)->layout) {
2184 return ai_config_master_clock_4020(dev, data);
2194 static int ai_config_insn(struct comedi_device *dev, struct comedi_subdevice *s,
2195 struct comedi_insn *insn, unsigned int *data)
2200 case INSN_CONFIG_ALT_SOURCE:
2201 return ai_config_calibration_source(dev, data);
2203 case INSN_CONFIG_BLOCK_SIZE:
2204 return ai_config_block_size(dev, data);
2206 case INSN_CONFIG_TIMER_1:
2207 return ai_config_master_clock(dev, data);
2216 static int ai_cmdtest(struct comedi_device *dev, struct comedi_subdevice *s,
2217 struct comedi_cmd *cmd)
2221 unsigned int tmp_arg, tmp_arg2;
2224 unsigned int triggers;
2226 /* step 1: make sure trigger sources are trivially valid */
2228 tmp = cmd->start_src;
2229 cmd->start_src &= TRIG_NOW | TRIG_EXT;
2230 if (!cmd->start_src || tmp != cmd->start_src)
2233 tmp = cmd->scan_begin_src;
2234 triggers = TRIG_TIMER;
2235 if (board(dev)->layout == LAYOUT_4020)
2236 triggers |= TRIG_OTHER;
2238 triggers |= TRIG_FOLLOW;
2239 cmd->scan_begin_src &= triggers;
2240 if (!cmd->scan_begin_src || tmp != cmd->scan_begin_src)
2243 tmp = cmd->convert_src;
2244 triggers = TRIG_TIMER;
2245 if (board(dev)->layout == LAYOUT_4020)
2246 triggers |= TRIG_NOW;
2248 triggers |= TRIG_EXT;
2249 cmd->convert_src &= triggers;
2250 if (!cmd->convert_src || tmp != cmd->convert_src)
2253 tmp = cmd->scan_end_src;
2254 cmd->scan_end_src &= TRIG_COUNT;
2255 if (!cmd->scan_end_src || tmp != cmd->scan_end_src)
2258 tmp = cmd->stop_src;
2259 cmd->stop_src &= TRIG_COUNT | TRIG_EXT | TRIG_NONE;
2260 if (!cmd->stop_src || tmp != cmd->stop_src)
2266 /* step 2: make sure trigger sources are unique and mutually compatible */
2268 /* uniqueness check */
2269 if (cmd->start_src != TRIG_NOW && cmd->start_src != TRIG_EXT)
2271 if (cmd->scan_begin_src != TRIG_TIMER &&
2272 cmd->scan_begin_src != TRIG_OTHER &&
2273 cmd->scan_begin_src != TRIG_FOLLOW)
2275 if (cmd->convert_src != TRIG_TIMER &&
2276 cmd->convert_src != TRIG_EXT && cmd->convert_src != TRIG_NOW)
2278 if (cmd->stop_src != TRIG_COUNT &&
2279 cmd->stop_src != TRIG_NONE && cmd->stop_src != TRIG_EXT)
2282 /* compatibility check */
2283 if (cmd->convert_src == TRIG_EXT && cmd->scan_begin_src == TRIG_TIMER)
2285 if (cmd->stop_src != TRIG_COUNT &&
2286 cmd->stop_src != TRIG_NONE && cmd->stop_src != TRIG_EXT)
2292 /* step 3: make sure arguments are trivially compatible */
2294 if (cmd->convert_src == TRIG_TIMER) {
2295 if (board(dev)->layout == LAYOUT_4020) {
2296 if (cmd->convert_arg) {
2297 cmd->convert_arg = 0;
2301 if (cmd->convert_arg < board(dev)->ai_speed) {
2302 cmd->convert_arg = board(dev)->ai_speed;
2305 if (cmd->scan_begin_src == TRIG_TIMER) {
2306 /* if scans are timed faster than conversion rate allows */
2307 if (cmd->convert_arg * cmd->chanlist_len >
2308 cmd->scan_begin_arg) {
2309 cmd->scan_begin_arg =
2318 if (!cmd->chanlist_len) {
2319 cmd->chanlist_len = 1;
2322 if (cmd->scan_end_arg != cmd->chanlist_len) {
2323 cmd->scan_end_arg = cmd->chanlist_len;
2327 switch (cmd->stop_src) {
2331 if (!cmd->stop_arg) {
2337 if (cmd->stop_arg != 0) {
2349 /* step 4: fix up any arguments */
2351 if (cmd->convert_src == TRIG_TIMER) {
2352 tmp_arg = cmd->convert_arg;
2353 tmp_arg2 = cmd->scan_begin_arg;
2354 check_adc_timing(dev, cmd);
2355 if (tmp_arg != cmd->convert_arg)
2357 if (tmp_arg2 != cmd->scan_begin_arg)
2364 /* make sure user is doesn't change analog reference mid chanlist */
2365 if (cmd->chanlist) {
2366 aref = CR_AREF(cmd->chanlist[0]);
2367 for (i = 1; i < cmd->chanlist_len; i++) {
2368 if (aref != CR_AREF(cmd->chanlist[i])) {
2370 "all elements in chanlist must use the same analog reference");
2375 /* check 4020 chanlist */
2376 if (board(dev)->layout == LAYOUT_4020) {
2377 unsigned int first_channel = CR_CHAN(cmd->chanlist[0]);
2378 for (i = 1; i < cmd->chanlist_len; i++) {
2379 if (CR_CHAN(cmd->chanlist[i]) !=
2380 first_channel + i) {
2382 "chanlist must use consecutive channels");
2387 if (cmd->chanlist_len == 3) {
2389 "chanlist cannot be 3 channels long, use 1, 2, or 4 channels");
2401 static int use_hw_sample_counter(struct comedi_cmd *cmd)
2403 /* disable for now until I work out a race */
2406 if (cmd->stop_src == TRIG_COUNT && cmd->stop_arg <= max_counter_value)
2412 static void setup_sample_counters(struct comedi_device *dev,
2413 struct comedi_cmd *cmd)
2415 if (cmd->stop_src == TRIG_COUNT) {
2416 /* set software count */
2417 priv(dev)->ai_count = cmd->stop_arg * cmd->chanlist_len;
2419 /* load hardware conversion counter */
2420 if (use_hw_sample_counter(cmd)) {
2421 writew(cmd->stop_arg & 0xffff,
2422 priv(dev)->main_iobase + ADC_COUNT_LOWER_REG);
2423 writew((cmd->stop_arg >> 16) & 0xff,
2424 priv(dev)->main_iobase + ADC_COUNT_UPPER_REG);
2426 writew(1, priv(dev)->main_iobase + ADC_COUNT_LOWER_REG);
2430 static inline unsigned int dma_transfer_size(struct comedi_device *dev)
2432 unsigned int num_samples;
2435 priv(dev)->ai_fifo_segment_length *
2436 board(dev)->ai_fifo->sample_packing_ratio;
2437 if (num_samples > DMA_BUFFER_SIZE / sizeof(uint16_t))
2438 num_samples = DMA_BUFFER_SIZE / sizeof(uint16_t);
2443 static void disable_ai_pacing(struct comedi_device *dev)
2445 unsigned long flags;
2447 disable_ai_interrupts(dev);
2449 spin_lock_irqsave(&dev->spinlock, flags);
2450 priv(dev)->adc_control1_bits &= ~ADC_SW_GATE_BIT;
2451 writew(priv(dev)->adc_control1_bits,
2452 priv(dev)->main_iobase + ADC_CONTROL1_REG);
2453 spin_unlock_irqrestore(&dev->spinlock, flags);
2455 /* disable pacing, triggering, etc */
2456 writew(ADC_DMA_DISABLE_BIT | ADC_SOFT_GATE_BITS | ADC_GATE_LEVEL_BIT,
2457 priv(dev)->main_iobase + ADC_CONTROL0_REG);
2460 static void disable_ai_interrupts(struct comedi_device *dev)
2462 unsigned long flags;
2464 spin_lock_irqsave(&dev->spinlock, flags);
2465 priv(dev)->intr_enable_bits &=
2466 ~EN_ADC_INTR_SRC_BIT & ~EN_ADC_DONE_INTR_BIT &
2467 ~EN_ADC_ACTIVE_INTR_BIT & ~EN_ADC_STOP_INTR_BIT &
2468 ~EN_ADC_OVERRUN_BIT & ~ADC_INTR_SRC_MASK;
2469 writew(priv(dev)->intr_enable_bits,
2470 priv(dev)->main_iobase + INTR_ENABLE_REG);
2471 spin_unlock_irqrestore(&dev->spinlock, flags);
2473 DEBUG_PRINT("intr enable bits 0x%x\n", priv(dev)->intr_enable_bits);
2476 static void enable_ai_interrupts(struct comedi_device *dev,
2477 const struct comedi_cmd *cmd)
2480 unsigned long flags;
2482 bits = EN_ADC_OVERRUN_BIT | EN_ADC_DONE_INTR_BIT |
2483 EN_ADC_ACTIVE_INTR_BIT | EN_ADC_STOP_INTR_BIT;
2484 /* Use pio transfer and interrupt on end of conversion if TRIG_WAKE_EOS flag is set. */
2485 if (cmd->flags & TRIG_WAKE_EOS) {
2486 /* 4020 doesn't support pio transfers except for fifo dregs */
2487 if (board(dev)->layout != LAYOUT_4020)
2488 bits |= ADC_INTR_EOSCAN_BITS | EN_ADC_INTR_SRC_BIT;
2490 spin_lock_irqsave(&dev->spinlock, flags);
2491 priv(dev)->intr_enable_bits |= bits;
2492 writew(priv(dev)->intr_enable_bits,
2493 priv(dev)->main_iobase + INTR_ENABLE_REG);
2494 DEBUG_PRINT("intr enable bits 0x%x\n", priv(dev)->intr_enable_bits);
2495 spin_unlock_irqrestore(&dev->spinlock, flags);
2498 static uint32_t ai_convert_counter_6xxx(const struct comedi_device *dev,
2499 const struct comedi_cmd *cmd)
2501 /* supposed to load counter with desired divisor minus 3 */
2502 return cmd->convert_arg / TIMER_BASE - 3;
2505 static uint32_t ai_scan_counter_6xxx(struct comedi_device *dev,
2506 struct comedi_cmd *cmd)
2509 /* figure out how long we need to delay at end of scan */
2510 switch (cmd->scan_begin_src) {
2512 count = (cmd->scan_begin_arg -
2513 (cmd->convert_arg * (cmd->chanlist_len - 1)))
2517 count = cmd->convert_arg / TIMER_BASE;
2526 static uint32_t ai_convert_counter_4020(struct comedi_device *dev,
2527 struct comedi_cmd *cmd)
2529 unsigned int divisor;
2531 switch (cmd->scan_begin_src) {
2533 divisor = cmd->scan_begin_arg / TIMER_BASE;
2536 divisor = priv(dev)->ext_clock.divisor;
2538 default: /* should never happen */
2539 comedi_error(dev, "bug! failed to set ai pacing!");
2544 /* supposed to load counter with desired divisor minus 2 for 4020 */
2548 static void select_master_clock_4020(struct comedi_device *dev,
2549 const struct comedi_cmd *cmd)
2551 /* select internal/external master clock */
2552 priv(dev)->hw_config_bits &= ~MASTER_CLOCK_4020_MASK;
2553 if (cmd->scan_begin_src == TRIG_OTHER) {
2554 int chanspec = priv(dev)->ext_clock.chanspec;
2556 if (CR_CHAN(chanspec))
2557 priv(dev)->hw_config_bits |= BNC_CLOCK_4020_BITS;
2559 priv(dev)->hw_config_bits |= EXT_CLOCK_4020_BITS;
2561 priv(dev)->hw_config_bits |= INTERNAL_CLOCK_4020_BITS;
2563 writew(priv(dev)->hw_config_bits,
2564 priv(dev)->main_iobase + HW_CONFIG_REG);
2567 static void select_master_clock(struct comedi_device *dev,
2568 const struct comedi_cmd *cmd)
2570 switch (board(dev)->layout) {
2572 select_master_clock_4020(dev, cmd);
2579 static inline void dma_start_sync(struct comedi_device *dev,
2580 unsigned int channel)
2582 unsigned long flags;
2584 /* spinlock for plx dma control/status reg */
2585 spin_lock_irqsave(&dev->spinlock, flags);
2587 writeb(PLX_DMA_EN_BIT | PLX_DMA_START_BIT |
2588 PLX_CLEAR_DMA_INTR_BIT,
2589 priv(dev)->plx9080_iobase + PLX_DMA1_CS_REG);
2591 writeb(PLX_DMA_EN_BIT | PLX_DMA_START_BIT |
2592 PLX_CLEAR_DMA_INTR_BIT,
2593 priv(dev)->plx9080_iobase + PLX_DMA0_CS_REG);
2594 spin_unlock_irqrestore(&dev->spinlock, flags);
2597 static void set_ai_pacing(struct comedi_device *dev, struct comedi_cmd *cmd)
2599 uint32_t convert_counter = 0, scan_counter = 0;
2601 check_adc_timing(dev, cmd);
2603 select_master_clock(dev, cmd);
2605 if (board(dev)->layout == LAYOUT_4020) {
2606 convert_counter = ai_convert_counter_4020(dev, cmd);
2608 convert_counter = ai_convert_counter_6xxx(dev, cmd);
2609 scan_counter = ai_scan_counter_6xxx(dev, cmd);
2612 /* load lower 16 bits of convert interval */
2613 writew(convert_counter & 0xffff,
2614 priv(dev)->main_iobase + ADC_SAMPLE_INTERVAL_LOWER_REG);
2615 DEBUG_PRINT("convert counter 0x%x\n", convert_counter);
2616 /* load upper 8 bits of convert interval */
2617 writew((convert_counter >> 16) & 0xff,
2618 priv(dev)->main_iobase + ADC_SAMPLE_INTERVAL_UPPER_REG);
2619 /* load lower 16 bits of scan delay */
2620 writew(scan_counter & 0xffff,
2621 priv(dev)->main_iobase + ADC_DELAY_INTERVAL_LOWER_REG);
2622 /* load upper 8 bits of scan delay */
2623 writew((scan_counter >> 16) & 0xff,
2624 priv(dev)->main_iobase + ADC_DELAY_INTERVAL_UPPER_REG);
2625 DEBUG_PRINT("scan counter 0x%x\n", scan_counter);
2628 static int use_internal_queue_6xxx(const struct comedi_cmd *cmd)
2631 for (i = 0; i + 1 < cmd->chanlist_len; i++) {
2632 if (CR_CHAN(cmd->chanlist[i + 1]) !=
2633 CR_CHAN(cmd->chanlist[i]) + 1)
2635 if (CR_RANGE(cmd->chanlist[i + 1]) !=
2636 CR_RANGE(cmd->chanlist[i]))
2638 if (CR_AREF(cmd->chanlist[i + 1]) != CR_AREF(cmd->chanlist[i]))
2644 static int setup_channel_queue(struct comedi_device *dev,
2645 const struct comedi_cmd *cmd)
2647 unsigned short bits;
2650 if (board(dev)->layout != LAYOUT_4020) {
2651 if (use_internal_queue_6xxx(cmd)) {
2652 priv(dev)->hw_config_bits &= ~EXT_QUEUE_BIT;
2653 writew(priv(dev)->hw_config_bits,
2654 priv(dev)->main_iobase + HW_CONFIG_REG);
2657 bits |= adc_chan_bits(CR_CHAN(cmd->chanlist[0]));
2659 bits |= ai_range_bits_6xxx(dev,
2660 CR_RANGE(cmd->chanlist[0]));
2661 /* set single-ended / differential */
2662 bits |= se_diff_bit_6xxx(dev,
2663 CR_AREF(cmd->chanlist[0]) ==
2665 if (CR_AREF(cmd->chanlist[0]) == AREF_COMMON)
2666 bits |= ADC_COMMON_BIT;
2667 /* set stop channel */
2668 writew(adc_chan_bits
2669 (CR_CHAN(cmd->chanlist[cmd->chanlist_len - 1])),
2670 priv(dev)->main_iobase + ADC_QUEUE_HIGH_REG);
2671 /* set start channel, and rest of settings */
2673 priv(dev)->main_iobase + ADC_QUEUE_LOAD_REG);
2675 /* use external queue */
2676 if (dev->write_subdev && dev->write_subdev->busy) {
2677 warn_external_queue(dev);
2680 priv(dev)->hw_config_bits |= EXT_QUEUE_BIT;
2681 writew(priv(dev)->hw_config_bits,
2682 priv(dev)->main_iobase + HW_CONFIG_REG);
2683 /* clear DAC buffer to prevent weird interactions */
2685 priv(dev)->main_iobase + DAC_BUFFER_CLEAR_REG);
2686 /* clear queue pointer */
2687 writew(0, priv(dev)->main_iobase + ADC_QUEUE_CLEAR_REG);
2688 /* load external queue */
2689 for (i = 0; i < cmd->chanlist_len; i++) {
2693 adc_chan_bits(CR_CHAN(cmd->chanlist[i]));
2695 bits |= ai_range_bits_6xxx(dev,
2699 /* set single-ended / differential */
2700 bits |= se_diff_bit_6xxx(dev,
2704 if (CR_AREF(cmd->chanlist[i]) == AREF_COMMON)
2705 bits |= ADC_COMMON_BIT;
2706 /* mark end of queue */
2707 if (i == cmd->chanlist_len - 1)
2708 bits |= QUEUE_EOSCAN_BIT |
2711 priv(dev)->main_iobase +
2712 ADC_QUEUE_FIFO_REG);
2714 ("wrote 0x%x to external channel queue\n",
2717 /* doing a queue clear is not specified in board docs,
2718 * but required for reliable operation */
2719 writew(0, priv(dev)->main_iobase + ADC_QUEUE_CLEAR_REG);
2720 /* prime queue holding register */
2721 writew(0, priv(dev)->main_iobase + ADC_QUEUE_LOAD_REG);
2724 unsigned short old_cal_range_bits =
2725 priv(dev)->i2c_cal_range_bits;
2727 priv(dev)->i2c_cal_range_bits &= ~ADC_SRC_4020_MASK;
2728 /* select BNC inputs */
2729 priv(dev)->i2c_cal_range_bits |= adc_src_4020_bits(4);
2731 for (i = 0; i < cmd->chanlist_len; i++) {
2732 unsigned int channel = CR_CHAN(cmd->chanlist[i]);
2733 unsigned int range = CR_RANGE(cmd->chanlist[i]);
2736 priv(dev)->i2c_cal_range_bits |=
2737 attenuate_bit(channel);
2739 priv(dev)->i2c_cal_range_bits &=
2740 ~attenuate_bit(channel);
2742 /* update calibration/range i2c register only if necessary, as it is very slow */
2743 if (old_cal_range_bits != priv(dev)->i2c_cal_range_bits) {
2744 uint8_t i2c_data = priv(dev)->i2c_cal_range_bits;
2745 i2c_write(dev, RANGE_CAL_I2C_ADDR, &i2c_data,
2752 static inline void load_first_dma_descriptor(struct comedi_device *dev,
2753 unsigned int dma_channel,
2754 unsigned int descriptor_bits)
2756 /* The transfer size, pci address, and local address registers
2757 * are supposedly unused during chained dma,
2758 * but I have found that left over values from last operation
2759 * occasionally cause problems with transfer of first dma
2760 * block. Initializing them to zero seems to fix the problem. */
2763 priv(dev)->plx9080_iobase + PLX_DMA1_TRANSFER_SIZE_REG);
2764 writel(0, priv(dev)->plx9080_iobase + PLX_DMA1_PCI_ADDRESS_REG);
2766 priv(dev)->plx9080_iobase + PLX_DMA1_LOCAL_ADDRESS_REG);
2767 writel(descriptor_bits,
2768 priv(dev)->plx9080_iobase + PLX_DMA1_DESCRIPTOR_REG);
2771 priv(dev)->plx9080_iobase + PLX_DMA0_TRANSFER_SIZE_REG);
2772 writel(0, priv(dev)->plx9080_iobase + PLX_DMA0_PCI_ADDRESS_REG);
2774 priv(dev)->plx9080_iobase + PLX_DMA0_LOCAL_ADDRESS_REG);
2775 writel(descriptor_bits,
2776 priv(dev)->plx9080_iobase + PLX_DMA0_DESCRIPTOR_REG);
2780 static int ai_cmd(struct comedi_device *dev, struct comedi_subdevice *s)
2782 struct comedi_async *async = s->async;
2783 struct comedi_cmd *cmd = &async->cmd;
2786 unsigned long flags;
2789 disable_ai_pacing(dev);
2792 retval = setup_channel_queue(dev, cmd);
2796 /* make sure internal calibration source is turned off */
2797 writew(0, priv(dev)->main_iobase + CALIBRATION_REG);
2799 set_ai_pacing(dev, cmd);
2801 setup_sample_counters(dev, cmd);
2803 enable_ai_interrupts(dev, cmd);
2805 spin_lock_irqsave(&dev->spinlock, flags);
2806 /* set mode, allow conversions through software gate */
2807 priv(dev)->adc_control1_bits |= ADC_SW_GATE_BIT;
2808 priv(dev)->adc_control1_bits &= ~ADC_DITHER_BIT;
2809 if (board(dev)->layout != LAYOUT_4020) {
2810 priv(dev)->adc_control1_bits &= ~ADC_MODE_MASK;
2811 if (cmd->convert_src == TRIG_EXT)
2812 priv(dev)->adc_control1_bits |= adc_mode_bits(13); /* good old mode 13 */
2814 priv(dev)->adc_control1_bits |= adc_mode_bits(8); /* mode 8. What else could you need? */
2816 priv(dev)->adc_control1_bits &= ~CHANNEL_MODE_4020_MASK;
2817 if (cmd->chanlist_len == 4)
2818 priv(dev)->adc_control1_bits |= FOUR_CHANNEL_4020_BITS;
2819 else if (cmd->chanlist_len == 2)
2820 priv(dev)->adc_control1_bits |= TWO_CHANNEL_4020_BITS;
2821 priv(dev)->adc_control1_bits &= ~ADC_LO_CHANNEL_4020_MASK;
2822 priv(dev)->adc_control1_bits |=
2823 adc_lo_chan_4020_bits(CR_CHAN(cmd->chanlist[0]));
2824 priv(dev)->adc_control1_bits &= ~ADC_HI_CHANNEL_4020_MASK;
2825 priv(dev)->adc_control1_bits |=
2826 adc_hi_chan_4020_bits(CR_CHAN
2828 chanlist[cmd->chanlist_len - 1]));
2830 writew(priv(dev)->adc_control1_bits,
2831 priv(dev)->main_iobase + ADC_CONTROL1_REG);
2832 DEBUG_PRINT("control1 bits 0x%x\n", priv(dev)->adc_control1_bits);
2833 spin_unlock_irqrestore(&dev->spinlock, flags);
2835 /* clear adc buffer */
2836 writew(0, priv(dev)->main_iobase + ADC_BUFFER_CLEAR_REG);
2838 if ((cmd->flags & TRIG_WAKE_EOS) == 0 ||
2839 board(dev)->layout == LAYOUT_4020) {
2840 priv(dev)->ai_dma_index = 0;
2842 /* set dma transfer size */
2843 for (i = 0; i < ai_dma_ring_count(board(dev)); i++)
2844 priv(dev)->ai_dma_desc[i].transfer_size =
2845 cpu_to_le32(dma_transfer_size(dev) *
2848 /* give location of first dma descriptor */
2849 load_first_dma_descriptor(dev, 1,
2850 priv(dev)->ai_dma_desc_bus_addr |
2851 PLX_DESC_IN_PCI_BIT |
2852 PLX_INTR_TERM_COUNT |
2853 PLX_XFER_LOCAL_TO_PCI);
2855 dma_start_sync(dev, 1);
2858 if (board(dev)->layout == LAYOUT_4020) {
2859 /* set source for external triggers */
2861 if (cmd->start_src == TRIG_EXT && CR_CHAN(cmd->start_arg))
2862 bits |= EXT_START_TRIG_BNC_BIT;
2863 if (cmd->stop_src == TRIG_EXT && CR_CHAN(cmd->stop_arg))
2864 bits |= EXT_STOP_TRIG_BNC_BIT;
2865 writew(bits, priv(dev)->main_iobase + DAQ_ATRIG_LOW_4020_REG);
2868 spin_lock_irqsave(&dev->spinlock, flags);
2870 /* enable pacing, triggering, etc */
2871 bits = ADC_ENABLE_BIT | ADC_SOFT_GATE_BITS | ADC_GATE_LEVEL_BIT;
2872 if (cmd->flags & TRIG_WAKE_EOS)
2873 bits |= ADC_DMA_DISABLE_BIT;
2874 /* set start trigger */
2875 if (cmd->start_src == TRIG_EXT) {
2876 bits |= ADC_START_TRIG_EXT_BITS;
2877 if (cmd->start_arg & CR_INVERT)
2878 bits |= ADC_START_TRIG_FALLING_BIT;
2879 } else if (cmd->start_src == TRIG_NOW)
2880 bits |= ADC_START_TRIG_SOFT_BITS;
2881 if (use_hw_sample_counter(cmd))
2882 bits |= ADC_SAMPLE_COUNTER_EN_BIT;
2883 writew(bits, priv(dev)->main_iobase + ADC_CONTROL0_REG);
2884 DEBUG_PRINT("control0 bits 0x%x\n", bits);
2886 priv(dev)->ai_cmd_running = 1;
2888 spin_unlock_irqrestore(&dev->spinlock, flags);
2890 /* start aquisition */
2891 if (cmd->start_src == TRIG_NOW) {
2892 writew(0, priv(dev)->main_iobase + ADC_START_REG);
2893 DEBUG_PRINT("soft trig\n");
2899 /* read num_samples from 16 bit wide ai fifo */
2900 static void pio_drain_ai_fifo_16(struct comedi_device *dev)
2902 struct comedi_subdevice *s = dev->read_subdev;
2903 struct comedi_async *async = s->async;
2904 struct comedi_cmd *cmd = &async->cmd;
2906 uint16_t prepost_bits;
2907 int read_segment, read_index, write_segment, write_index;
2911 /* get least significant 15 bits */
2913 readw(priv(dev)->main_iobase + ADC_READ_PNTR_REG) & 0x7fff;
2915 readw(priv(dev)->main_iobase + ADC_WRITE_PNTR_REG) & 0x7fff;
2916 /* Get most significant bits (grey code). Different boards use different code
2917 * so use a scheme that doesn't depend on encoding. This read must
2918 * occur after reading least significant 15 bits to avoid race
2919 * with fifo switching to next segment. */
2920 prepost_bits = readw(priv(dev)->main_iobase + PREPOST_REG);
2922 /* if read and write pointers are not on the same fifo segment, read to the
2923 * end of the read segment */
2924 read_segment = adc_upper_read_ptr_code(prepost_bits);
2925 write_segment = adc_upper_write_ptr_code(prepost_bits);
2927 DEBUG_PRINT(" rd seg %i, wrt seg %i, rd idx %i, wrt idx %i\n",
2928 read_segment, write_segment, read_index,
2931 if (read_segment != write_segment)
2933 priv(dev)->ai_fifo_segment_length - read_index;
2935 num_samples = write_index - read_index;
2937 if (cmd->stop_src == TRIG_COUNT) {
2938 if (priv(dev)->ai_count == 0)
2940 if (num_samples > priv(dev)->ai_count)
2941 num_samples = priv(dev)->ai_count;
2943 priv(dev)->ai_count -= num_samples;
2946 if (num_samples < 0) {
2947 printk(" cb_pcidas64: bug! num_samples < 0\n");
2951 DEBUG_PRINT(" read %i samples from fifo\n", num_samples);
2953 for (i = 0; i < num_samples; i++) {
2954 cfc_write_to_buffer(s,
2955 readw(priv(dev)->main_iobase +
2959 } while (read_segment != write_segment);
2962 /* Read from 32 bit wide ai fifo of 4020 - deal with insane grey coding of pointers.
2963 * The pci-4020 hardware only supports
2964 * dma transfers (it only supports the use of pio for draining the last remaining
2965 * points from the fifo when a data aquisition operation has completed).
2967 static void pio_drain_ai_fifo_32(struct comedi_device *dev)
2969 struct comedi_subdevice *s = dev->read_subdev;
2970 struct comedi_async *async = s->async;
2971 struct comedi_cmd *cmd = &async->cmd;
2973 unsigned int max_transfer = 100000;
2976 readw(priv(dev)->main_iobase + ADC_WRITE_PNTR_REG) & 0x7fff;
2978 readw(priv(dev)->main_iobase + ADC_READ_PNTR_REG) & 0x7fff;
2980 if (cmd->stop_src == TRIG_COUNT) {
2981 if (max_transfer > priv(dev)->ai_count)
2982 max_transfer = priv(dev)->ai_count;
2985 for (i = 0; read_code != write_code && i < max_transfer;) {
2986 fifo_data = readl(priv(dev)->dio_counter_iobase + ADC_FIFO_REG);
2987 cfc_write_to_buffer(s, fifo_data & 0xffff);
2989 if (i < max_transfer) {
2990 cfc_write_to_buffer(s, (fifo_data >> 16) & 0xffff);
2994 readw(priv(dev)->main_iobase + ADC_READ_PNTR_REG) & 0x7fff;
2996 priv(dev)->ai_count -= i;
3000 static void pio_drain_ai_fifo(struct comedi_device *dev)
3002 if (board(dev)->layout == LAYOUT_4020)
3003 pio_drain_ai_fifo_32(dev);
3005 pio_drain_ai_fifo_16(dev);
3008 static void drain_dma_buffers(struct comedi_device *dev, unsigned int channel)
3010 struct comedi_async *async = dev->read_subdev->async;
3011 uint32_t next_transfer_addr;
3013 int num_samples = 0;
3014 void __iomem *pci_addr_reg;
3018 priv(dev)->plx9080_iobase + PLX_DMA1_PCI_ADDRESS_REG;
3021 priv(dev)->plx9080_iobase + PLX_DMA0_PCI_ADDRESS_REG;
3023 /* loop until we have read all the full buffers */
3024 for (j = 0, next_transfer_addr = readl(pci_addr_reg);
3025 (next_transfer_addr <
3026 priv(dev)->ai_buffer_bus_addr[priv(dev)->ai_dma_index]
3027 || next_transfer_addr >=
3028 priv(dev)->ai_buffer_bus_addr[priv(dev)->ai_dma_index] +
3029 DMA_BUFFER_SIZE) && j < ai_dma_ring_count(board(dev)); j++) {
3030 /* transfer data from dma buffer to comedi buffer */
3031 num_samples = dma_transfer_size(dev);
3032 if (async->cmd.stop_src == TRIG_COUNT) {
3033 if (num_samples > priv(dev)->ai_count)
3034 num_samples = priv(dev)->ai_count;
3035 priv(dev)->ai_count -= num_samples;
3037 cfc_write_array_to_buffer(dev->read_subdev,
3038 priv(dev)->ai_buffer[priv(dev)->
3040 num_samples * sizeof(uint16_t));
3041 priv(dev)->ai_dma_index =
3042 (priv(dev)->ai_dma_index +
3043 1) % ai_dma_ring_count(board(dev));
3045 DEBUG_PRINT("next buffer addr 0x%lx\n",
3046 (unsigned long)priv(dev)->
3047 ai_buffer_bus_addr[priv(dev)->ai_dma_index]);
3048 DEBUG_PRINT("pci addr reg 0x%x\n", next_transfer_addr);
3050 /* XXX check for dma ring buffer overrun (use end-of-chain bit to mark last
3054 static void handle_ai_interrupt(struct comedi_device *dev,
3055 unsigned short status,
3056 unsigned int plx_status)
3058 struct comedi_subdevice *s = dev->read_subdev;
3059 struct comedi_async *async = s->async;
3060 struct comedi_cmd *cmd = &async->cmd;
3061 uint8_t dma1_status;
3062 unsigned long flags;
3064 /* check for fifo overrun */
3065 if (status & ADC_OVERRUN_BIT) {
3066 comedi_error(dev, "fifo overrun");
3067 async->events |= COMEDI_CB_EOA | COMEDI_CB_ERROR;
3069 /* spin lock makes sure noone else changes plx dma control reg */
3070 spin_lock_irqsave(&dev->spinlock, flags);
3071 dma1_status = readb(priv(dev)->plx9080_iobase + PLX_DMA1_CS_REG);
3072 if (plx_status & ICS_DMA1_A) { /* dma chan 1 interrupt */
3073 writeb((dma1_status & PLX_DMA_EN_BIT) | PLX_CLEAR_DMA_INTR_BIT,
3074 priv(dev)->plx9080_iobase + PLX_DMA1_CS_REG);
3075 DEBUG_PRINT("dma1 status 0x%x\n", dma1_status);
3077 if (dma1_status & PLX_DMA_EN_BIT)
3078 drain_dma_buffers(dev, 1);
3080 DEBUG_PRINT(" cleared dma ch1 interrupt\n");
3082 spin_unlock_irqrestore(&dev->spinlock, flags);
3084 if (status & ADC_DONE_BIT)
3085 DEBUG_PRINT("adc done interrupt\n");
3087 /* drain fifo with pio */
3088 if ((status & ADC_DONE_BIT) ||
3089 ((cmd->flags & TRIG_WAKE_EOS) &&
3090 (status & ADC_INTR_PENDING_BIT) &&
3091 (board(dev)->layout != LAYOUT_4020))) {
3092 DEBUG_PRINT("pio fifo drain\n");
3093 spin_lock_irqsave(&dev->spinlock, flags);
3094 if (priv(dev)->ai_cmd_running) {
3095 spin_unlock_irqrestore(&dev->spinlock, flags);
3096 pio_drain_ai_fifo(dev);
3098 spin_unlock_irqrestore(&dev->spinlock, flags);
3100 /* if we are have all the data, then quit */
3101 if ((cmd->stop_src == TRIG_COUNT && priv(dev)->ai_count <= 0) ||
3102 (cmd->stop_src == TRIG_EXT && (status & ADC_STOP_BIT))) {
3103 async->events |= COMEDI_CB_EOA;
3106 cfc_handle_events(dev, s);
3109 static inline unsigned int prev_ao_dma_index(struct comedi_device *dev)
3111 unsigned int buffer_index;
3113 if (priv(dev)->ao_dma_index == 0)
3114 buffer_index = AO_DMA_RING_COUNT - 1;
3116 buffer_index = priv(dev)->ao_dma_index - 1;
3117 return buffer_index;
3120 static int last_ao_dma_load_completed(struct comedi_device *dev)
3122 unsigned int buffer_index;
3123 unsigned int transfer_address;
3124 unsigned short dma_status;
3126 buffer_index = prev_ao_dma_index(dev);
3127 dma_status = readb(priv(dev)->plx9080_iobase + PLX_DMA0_CS_REG);
3128 if ((dma_status & PLX_DMA_DONE_BIT) == 0)
3132 readl(priv(dev)->plx9080_iobase + PLX_DMA0_PCI_ADDRESS_REG);
3133 if (transfer_address != priv(dev)->ao_buffer_bus_addr[buffer_index])
3139 static int ao_stopped_by_error(struct comedi_device *dev,
3140 const struct comedi_cmd *cmd)
3142 if (cmd->stop_src == TRIG_NONE)
3144 if (cmd->stop_src == TRIG_COUNT) {
3145 if (priv(dev)->ao_count)
3147 if (last_ao_dma_load_completed(dev) == 0)
3153 static inline int ao_dma_needs_restart(struct comedi_device *dev,
3154 unsigned short dma_status)
3156 if ((dma_status & PLX_DMA_DONE_BIT) == 0 ||
3157 (dma_status & PLX_DMA_EN_BIT) == 0)
3159 if (last_ao_dma_load_completed(dev))
3165 static void restart_ao_dma(struct comedi_device *dev)
3167 unsigned int dma_desc_bits;
3170 readl(priv(dev)->plx9080_iobase + PLX_DMA0_DESCRIPTOR_REG);
3171 dma_desc_bits &= ~PLX_END_OF_CHAIN_BIT;
3172 DEBUG_PRINT("restarting ao dma, descriptor reg 0x%x\n", dma_desc_bits);
3173 load_first_dma_descriptor(dev, 0, dma_desc_bits);
3175 dma_start_sync(dev, 0);
3178 static void handle_ao_interrupt(struct comedi_device *dev,
3179 unsigned short status, unsigned int plx_status)
3181 struct comedi_subdevice *s = dev->write_subdev;
3182 struct comedi_async *async;
3183 struct comedi_cmd *cmd;
3184 uint8_t dma0_status;
3185 unsigned long flags;
3187 /* board might not support ao, in which case write_subdev is NULL */
3193 /* spin lock makes sure noone else changes plx dma control reg */
3194 spin_lock_irqsave(&dev->spinlock, flags);
3195 dma0_status = readb(priv(dev)->plx9080_iobase + PLX_DMA0_CS_REG);
3196 if (plx_status & ICS_DMA0_A) { /* dma chan 0 interrupt */
3197 if ((dma0_status & PLX_DMA_EN_BIT)
3198 && !(dma0_status & PLX_DMA_DONE_BIT))
3199 writeb(PLX_DMA_EN_BIT | PLX_CLEAR_DMA_INTR_BIT,
3200 priv(dev)->plx9080_iobase + PLX_DMA0_CS_REG);
3202 writeb(PLX_CLEAR_DMA_INTR_BIT,
3203 priv(dev)->plx9080_iobase + PLX_DMA0_CS_REG);
3204 spin_unlock_irqrestore(&dev->spinlock, flags);
3205 DEBUG_PRINT("dma0 status 0x%x\n", dma0_status);
3206 if (dma0_status & PLX_DMA_EN_BIT) {
3207 load_ao_dma(dev, cmd);
3208 /* try to recover from dma end-of-chain event */
3209 if (ao_dma_needs_restart(dev, dma0_status))
3210 restart_ao_dma(dev);
3212 DEBUG_PRINT(" cleared dma ch0 interrupt\n");
3214 spin_unlock_irqrestore(&dev->spinlock, flags);
3216 if ((status & DAC_DONE_BIT)) {
3217 async->events |= COMEDI_CB_EOA;
3218 if (ao_stopped_by_error(dev, cmd))
3219 async->events |= COMEDI_CB_ERROR;
3220 DEBUG_PRINT("plx dma0 desc reg 0x%x\n",
3221 readl(priv(dev)->plx9080_iobase +
3222 PLX_DMA0_DESCRIPTOR_REG));
3223 DEBUG_PRINT("plx dma0 address reg 0x%x\n",
3224 readl(priv(dev)->plx9080_iobase +
3225 PLX_DMA0_PCI_ADDRESS_REG));
3227 cfc_handle_events(dev, s);
3230 static irqreturn_t handle_interrupt(int irq, void *d)
3232 struct comedi_device *dev = d;
3233 unsigned short status;
3234 uint32_t plx_status;
3237 plx_status = readl(priv(dev)->plx9080_iobase + PLX_INTRCS_REG);
3238 status = readw(priv(dev)->main_iobase + HW_STATUS_REG);
3240 DEBUG_PRINT("cb_pcidas64: hw status 0x%x ", status);
3241 DEBUG_PRINT("plx status 0x%x\n", plx_status);
3243 /* an interrupt before all the postconfig stuff gets done could
3244 * cause a NULL dereference if we continue through the
3245 * interrupt handler */
3246 if (dev->attached == 0) {
3247 DEBUG_PRINT("cb_pcidas64: premature interrupt, ignoring",
3251 handle_ai_interrupt(dev, status, plx_status);
3252 handle_ao_interrupt(dev, status, plx_status);
3254 /* clear possible plx9080 interrupt sources */
3255 if (plx_status & ICS_LDIA) { /* clear local doorbell interrupt */
3256 plx_bits = readl(priv(dev)->plx9080_iobase + PLX_DBR_OUT_REG);
3257 writel(plx_bits, priv(dev)->plx9080_iobase + PLX_DBR_OUT_REG);
3258 DEBUG_PRINT(" cleared local doorbell bits 0x%x\n", plx_bits);
3261 DEBUG_PRINT("exiting handler\n");
3266 static void abort_dma(struct comedi_device *dev, unsigned int channel)
3268 unsigned long flags;
3270 /* spinlock for plx dma control/status reg */
3271 spin_lock_irqsave(&dev->spinlock, flags);
3273 plx9080_abort_dma(priv(dev)->plx9080_iobase, channel);
3275 spin_unlock_irqrestore(&dev->spinlock, flags);
3278 static int ai_cancel(struct comedi_device *dev, struct comedi_subdevice *s)
3280 unsigned long flags;
3282 spin_lock_irqsave(&dev->spinlock, flags);
3283 if (priv(dev)->ai_cmd_running == 0) {
3284 spin_unlock_irqrestore(&dev->spinlock, flags);
3287 priv(dev)->ai_cmd_running = 0;
3288 spin_unlock_irqrestore(&dev->spinlock, flags);
3290 disable_ai_pacing(dev);
3294 DEBUG_PRINT("ai canceled\n");
3298 static int ao_winsn(struct comedi_device *dev, struct comedi_subdevice *s,
3299 struct comedi_insn *insn, unsigned int *data)
3301 int chan = CR_CHAN(insn->chanspec);
3302 int range = CR_RANGE(insn->chanspec);
3304 /* do some initializing */
3305 writew(0, priv(dev)->main_iobase + DAC_CONTROL0_REG);
3308 set_dac_range_bits(dev, &priv(dev)->dac_control1_bits, chan, range);
3309 writew(priv(dev)->dac_control1_bits,
3310 priv(dev)->main_iobase + DAC_CONTROL1_REG);
3312 /* write to channel */
3313 if (board(dev)->layout == LAYOUT_4020) {
3314 writew(data[0] & 0xff,
3315 priv(dev)->main_iobase + dac_lsb_4020_reg(chan));
3316 writew((data[0] >> 8) & 0xf,
3317 priv(dev)->main_iobase + dac_msb_4020_reg(chan));
3319 writew(data[0], priv(dev)->main_iobase + dac_convert_reg(chan));
3322 /* remember output value */
3323 priv(dev)->ao_value[chan] = data[0];
3328 static int ao_readback_insn(struct comedi_device *dev,
3329 struct comedi_subdevice *s,
3330 struct comedi_insn *insn, unsigned int *data)
3332 data[0] = priv(dev)->ao_value[CR_CHAN(insn->chanspec)];
3337 static void set_dac_control0_reg(struct comedi_device *dev,
3338 const struct comedi_cmd *cmd)
3340 unsigned int bits = DAC_ENABLE_BIT | WAVEFORM_GATE_LEVEL_BIT |
3341 WAVEFORM_GATE_ENABLE_BIT | WAVEFORM_GATE_SELECT_BIT;
3343 if (cmd->start_src == TRIG_EXT) {
3344 bits |= WAVEFORM_TRIG_EXT_BITS;
3345 if (cmd->start_arg & CR_INVERT)
3346 bits |= WAVEFORM_TRIG_FALLING_BIT;
3348 bits |= WAVEFORM_TRIG_SOFT_BITS;
3350 if (cmd->scan_begin_src == TRIG_EXT) {
3351 bits |= DAC_EXT_UPDATE_ENABLE_BIT;
3352 if (cmd->scan_begin_arg & CR_INVERT)
3353 bits |= DAC_EXT_UPDATE_FALLING_BIT;
3355 writew(bits, priv(dev)->main_iobase + DAC_CONTROL0_REG);
3358 static void set_dac_control1_reg(struct comedi_device *dev,
3359 const struct comedi_cmd *cmd)
3363 for (i = 0; i < cmd->chanlist_len; i++) {
3366 channel = CR_CHAN(cmd->chanlist[i]);
3367 range = CR_RANGE(cmd->chanlist[i]);
3368 set_dac_range_bits(dev, &priv(dev)->dac_control1_bits, channel,
3371 priv(dev)->dac_control1_bits |= DAC_SW_GATE_BIT;
3372 writew(priv(dev)->dac_control1_bits,
3373 priv(dev)->main_iobase + DAC_CONTROL1_REG);
3376 static void set_dac_select_reg(struct comedi_device *dev,
3377 const struct comedi_cmd *cmd)
3380 unsigned int first_channel, last_channel;
3382 first_channel = CR_CHAN(cmd->chanlist[0]);
3383 last_channel = CR_CHAN(cmd->chanlist[cmd->chanlist_len - 1]);
3384 if (last_channel < first_channel)
3385 comedi_error(dev, "bug! last ao channel < first ao channel");
3387 bits = (first_channel & 0x7) | (last_channel & 0x7) << 3;
3389 writew(bits, priv(dev)->main_iobase + DAC_SELECT_REG);
3392 static void set_dac_interval_regs(struct comedi_device *dev,
3393 const struct comedi_cmd *cmd)
3395 unsigned int divisor;
3397 if (cmd->scan_begin_src != TRIG_TIMER)
3400 divisor = get_ao_divisor(cmd->scan_begin_arg, cmd->flags);
3401 if (divisor > max_counter_value) {
3402 comedi_error(dev, "bug! ao divisor too big");
3403 divisor = max_counter_value;
3405 writew(divisor & 0xffff,
3406 priv(dev)->main_iobase + DAC_SAMPLE_INTERVAL_LOWER_REG);
3407 writew((divisor >> 16) & 0xff,
3408 priv(dev)->main_iobase + DAC_SAMPLE_INTERVAL_UPPER_REG);
3411 static unsigned int load_ao_dma_buffer(struct comedi_device *dev,
3412 const struct comedi_cmd *cmd)
3414 unsigned int num_bytes, buffer_index, prev_buffer_index;
3415 unsigned int next_bits;
3417 buffer_index = priv(dev)->ao_dma_index;
3418 prev_buffer_index = prev_ao_dma_index(dev);
3420 DEBUG_PRINT("attempting to load ao buffer %i (0x%x)\n", buffer_index,
3421 priv(dev)->ao_buffer_bus_addr[buffer_index]);
3423 num_bytes = comedi_buf_read_n_available(dev->write_subdev->async);
3424 if (num_bytes > DMA_BUFFER_SIZE)
3425 num_bytes = DMA_BUFFER_SIZE;
3426 if (cmd->stop_src == TRIG_COUNT && num_bytes > priv(dev)->ao_count)
3427 num_bytes = priv(dev)->ao_count;
3428 num_bytes -= num_bytes % bytes_in_sample;
3433 DEBUG_PRINT("loading %i bytes\n", num_bytes);
3435 num_bytes = cfc_read_array_from_buffer(dev->write_subdev,
3437 ao_buffer[buffer_index],
3439 priv(dev)->ao_dma_desc[buffer_index].transfer_size =
3440 cpu_to_le32(num_bytes);
3441 /* set end of chain bit so we catch underruns */
3442 next_bits = le32_to_cpu(priv(dev)->ao_dma_desc[buffer_index].next);
3443 next_bits |= PLX_END_OF_CHAIN_BIT;
3444 priv(dev)->ao_dma_desc[buffer_index].next = cpu_to_le32(next_bits);
3445 /* clear end of chain bit on previous buffer now that we have set it
3446 * for the last buffer */
3447 next_bits = le32_to_cpu(priv(dev)->ao_dma_desc[prev_buffer_index].next);
3448 next_bits &= ~PLX_END_OF_CHAIN_BIT;
3449 priv(dev)->ao_dma_desc[prev_buffer_index].next = cpu_to_le32(next_bits);
3451 priv(dev)->ao_dma_index = (buffer_index + 1) % AO_DMA_RING_COUNT;
3452 priv(dev)->ao_count -= num_bytes;
3457 static void load_ao_dma(struct comedi_device *dev, const struct comedi_cmd *cmd)
3459 unsigned int num_bytes;
3460 unsigned int next_transfer_addr;
3461 void __iomem *pci_addr_reg =
3462 priv(dev)->plx9080_iobase + PLX_DMA0_PCI_ADDRESS_REG;
3463 unsigned int buffer_index;
3466 buffer_index = priv(dev)->ao_dma_index;
3467 /* don't overwrite data that hasn't been transferred yet */
3468 next_transfer_addr = readl(pci_addr_reg);
3469 if (next_transfer_addr >=
3470 priv(dev)->ao_buffer_bus_addr[buffer_index]
3471 && next_transfer_addr <
3472 priv(dev)->ao_buffer_bus_addr[buffer_index] +
3475 num_bytes = load_ao_dma_buffer(dev, cmd);
3476 } while (num_bytes >= DMA_BUFFER_SIZE);
3479 static int prep_ao_dma(struct comedi_device *dev, const struct comedi_cmd *cmd)
3481 unsigned int num_bytes;
3484 /* clear queue pointer too, since external queue has
3485 * weird interactions with ao fifo */
3486 writew(0, priv(dev)->main_iobase + ADC_QUEUE_CLEAR_REG);
3487 writew(0, priv(dev)->main_iobase + DAC_BUFFER_CLEAR_REG);
3489 num_bytes = (DAC_FIFO_SIZE / 2) * bytes_in_sample;
3490 if (cmd->stop_src == TRIG_COUNT &&
3491 num_bytes / bytes_in_sample > priv(dev)->ao_count)
3492 num_bytes = priv(dev)->ao_count * bytes_in_sample;
3493 num_bytes = cfc_read_array_from_buffer(dev->write_subdev,
3494 priv(dev)->ao_bounce_buffer,
3496 for (i = 0; i < num_bytes / bytes_in_sample; i++) {
3497 writew(priv(dev)->ao_bounce_buffer[i],
3498 priv(dev)->main_iobase + DAC_FIFO_REG);
3500 priv(dev)->ao_count -= num_bytes / bytes_in_sample;
3501 if (cmd->stop_src == TRIG_COUNT && priv(dev)->ao_count == 0)
3503 num_bytes = load_ao_dma_buffer(dev, cmd);
3506 if (num_bytes >= DMA_BUFFER_SIZE) ;
3507 load_ao_dma(dev, cmd);
3509 dma_start_sync(dev, 0);
3514 static inline int external_ai_queue_in_use(struct comedi_device *dev)
3516 if (dev->read_subdev->busy)
3518 if (board(dev)->layout == LAYOUT_4020)
3520 else if (use_internal_queue_6xxx(&dev->read_subdev->async->cmd))
3525 static int ao_cmd(struct comedi_device *dev, struct comedi_subdevice *s)
3527 struct comedi_cmd *cmd = &s->async->cmd;
3529 if (external_ai_queue_in_use(dev)) {
3530 warn_external_queue(dev);
3533 /* disable analog output system during setup */
3534 writew(0x0, priv(dev)->main_iobase + DAC_CONTROL0_REG);
3536 priv(dev)->ao_dma_index = 0;
3537 priv(dev)->ao_count = cmd->stop_arg * cmd->chanlist_len;
3539 set_dac_select_reg(dev, cmd);
3540 set_dac_interval_regs(dev, cmd);
3541 load_first_dma_descriptor(dev, 0, priv(dev)->ao_dma_desc_bus_addr |
3542 PLX_DESC_IN_PCI_BIT | PLX_INTR_TERM_COUNT);
3544 set_dac_control1_reg(dev, cmd);
3545 s->async->inttrig = ao_inttrig;
3550 static int ao_inttrig(struct comedi_device *dev, struct comedi_subdevice *s,
3551 unsigned int trig_num)
3553 struct comedi_cmd *cmd = &s->async->cmd;
3559 retval = prep_ao_dma(dev, cmd);
3563 set_dac_control0_reg(dev, cmd);
3565 if (cmd->start_src == TRIG_INT)
3566 writew(0, priv(dev)->main_iobase + DAC_START_REG);
3568 s->async->inttrig = NULL;
3573 static int ao_cmdtest(struct comedi_device *dev, struct comedi_subdevice *s,
3574 struct comedi_cmd *cmd)
3578 unsigned int tmp_arg;
3581 /* step 1: make sure trigger sources are trivially valid */
3583 tmp = cmd->start_src;
3584 cmd->start_src &= TRIG_INT | TRIG_EXT;
3585 if (!cmd->start_src || tmp != cmd->start_src)
3588 tmp = cmd->scan_begin_src;
3589 cmd->scan_begin_src &= TRIG_TIMER | TRIG_EXT;
3590 if (!cmd->scan_begin_src || tmp != cmd->scan_begin_src)
3593 tmp = cmd->convert_src;
3594 cmd->convert_src &= TRIG_NOW;
3595 if (!cmd->convert_src || tmp != cmd->convert_src)
3598 tmp = cmd->scan_end_src;
3599 cmd->scan_end_src &= TRIG_COUNT;
3600 if (!cmd->scan_end_src || tmp != cmd->scan_end_src)
3603 tmp = cmd->stop_src;
3604 cmd->stop_src &= TRIG_NONE;
3605 if (!cmd->stop_src || tmp != cmd->stop_src)
3611 /* step 2: make sure trigger sources are unique and mutually compatible */
3613 /* uniqueness check */
3614 if (cmd->start_src != TRIG_INT && cmd->start_src != TRIG_EXT)
3616 if (cmd->scan_begin_src != TRIG_TIMER &&
3617 cmd->scan_begin_src != TRIG_EXT)
3620 /* compatibility check */
3621 if (cmd->convert_src == TRIG_EXT && cmd->scan_begin_src == TRIG_TIMER)
3623 if (cmd->stop_src != TRIG_COUNT &&
3624 cmd->stop_src != TRIG_NONE && cmd->stop_src != TRIG_EXT)
3630 /* step 3: make sure arguments are trivially compatible */
3632 if (cmd->scan_begin_src == TRIG_TIMER) {
3633 if (cmd->scan_begin_arg < board(dev)->ao_scan_speed) {
3634 cmd->scan_begin_arg = board(dev)->ao_scan_speed;
3637 if (get_ao_divisor(cmd->scan_begin_arg,
3638 cmd->flags) > max_counter_value) {
3639 cmd->scan_begin_arg =
3640 (max_counter_value + 2) * TIMER_BASE;
3645 if (!cmd->chanlist_len) {
3646 cmd->chanlist_len = 1;
3649 if (cmd->scan_end_arg != cmd->chanlist_len) {
3650 cmd->scan_end_arg = cmd->chanlist_len;
3657 /* step 4: fix up any arguments */
3659 if (cmd->scan_begin_src == TRIG_TIMER) {
3660 tmp_arg = cmd->scan_begin_arg;
3661 cmd->scan_begin_arg =
3662 get_divisor(cmd->scan_begin_arg, cmd->flags) * TIMER_BASE;
3663 if (tmp_arg != cmd->scan_begin_arg)
3670 if (cmd->chanlist) {
3671 unsigned int first_channel = CR_CHAN(cmd->chanlist[0]);
3672 for (i = 1; i < cmd->chanlist_len; i++) {
3673 if (CR_CHAN(cmd->chanlist[i]) != first_channel + i) {
3675 "chanlist must use consecutive channels");
3688 static int ao_cancel(struct comedi_device *dev, struct comedi_subdevice *s)
3690 writew(0x0, priv(dev)->main_iobase + DAC_CONTROL0_REG);
3695 static int dio_callback(int dir, int port, int data, unsigned long arg)
3697 void __iomem *iobase = (void __iomem *)arg;
3699 writeb(data, iobase + port);
3700 DEBUG_PRINT("wrote 0x%x to port %i\n", data, port);
3703 return readb(iobase + port);
3707 static int dio_callback_4020(int dir, int port, int data, unsigned long arg)
3709 void __iomem *iobase = (void __iomem *)arg;
3711 writew(data, iobase + 2 * port);
3714 return readw(iobase + 2 * port);
3718 static int di_rbits(struct comedi_device *dev, struct comedi_subdevice *s,
3719 struct comedi_insn *insn, unsigned int *data)
3723 bits = readb(priv(dev)->dio_counter_iobase + DI_REG);
3731 static int do_wbits(struct comedi_device *dev, struct comedi_subdevice *s,
3732 struct comedi_insn *insn, unsigned int *data)
3735 /* zero bits we are going to change */
3736 s->state &= ~data[0];
3738 s->state |= data[0] & data[1];
3740 writeb(s->state, priv(dev)->dio_counter_iobase + DO_REG);
3747 static int dio_60xx_config_insn(struct comedi_device *dev,
3748 struct comedi_subdevice *s,
3749 struct comedi_insn *insn, unsigned int *data)
3753 mask = 1 << CR_CHAN(insn->chanspec);
3756 case INSN_CONFIG_DIO_INPUT:
3757 s->io_bits &= ~mask;
3759 case INSN_CONFIG_DIO_OUTPUT:
3762 case INSN_CONFIG_DIO_QUERY:
3763 data[1] = (s->io_bits & mask) ? COMEDI_OUTPUT : COMEDI_INPUT;
3770 priv(dev)->dio_counter_iobase + DIO_DIRECTION_60XX_REG);
3775 static int dio_60xx_wbits(struct comedi_device *dev, struct comedi_subdevice *s,
3776 struct comedi_insn *insn, unsigned int *data)
3779 s->state &= ~data[0];
3780 s->state |= (data[0] & data[1]);
3782 priv(dev)->dio_counter_iobase + DIO_DATA_60XX_REG);
3785 data[1] = readb(priv(dev)->dio_counter_iobase + DIO_DATA_60XX_REG);
3790 static void caldac_write(struct comedi_device *dev, unsigned int channel,
3793 priv(dev)->caldac_state[channel] = value;
3795 switch (board(dev)->layout) {
3798 caldac_8800_write(dev, channel, value);
3801 caldac_i2c_write(dev, channel, value);
3808 static int calib_write_insn(struct comedi_device *dev,
3809 struct comedi_subdevice *s,
3810 struct comedi_insn *insn, unsigned int *data)
3812 int channel = CR_CHAN(insn->chanspec);
3814 /* return immediately if setting hasn't changed, since
3815 * programming these things is slow */
3816 if (priv(dev)->caldac_state[channel] == data[0])
3819 caldac_write(dev, channel, data[0]);
3824 static int calib_read_insn(struct comedi_device *dev,
3825 struct comedi_subdevice *s, struct comedi_insn *insn,
3828 unsigned int channel = CR_CHAN(insn->chanspec);
3830 data[0] = priv(dev)->caldac_state[channel];
3835 static void ad8402_write(struct comedi_device *dev, unsigned int channel,
3838 static const int bitstream_length = 10;
3839 unsigned int bit, register_bits;
3840 unsigned int bitstream = ((channel & 0x3) << 8) | (value & 0xff);
3841 static const int ad8402_udelay = 1;
3843 priv(dev)->ad8402_state[channel] = value;
3845 register_bits = SELECT_8402_64XX_BIT;
3846 udelay(ad8402_udelay);
3847 writew(register_bits, priv(dev)->main_iobase + CALIBRATION_REG);
3849 for (bit = 1 << (bitstream_length - 1); bit; bit >>= 1) {
3850 if (bitstream & bit)
3851 register_bits |= SERIAL_DATA_IN_BIT;
3853 register_bits &= ~SERIAL_DATA_IN_BIT;
3854 udelay(ad8402_udelay);
3855 writew(register_bits, priv(dev)->main_iobase + CALIBRATION_REG);
3856 udelay(ad8402_udelay);
3857 writew(register_bits | SERIAL_CLOCK_BIT,
3858 priv(dev)->main_iobase + CALIBRATION_REG);
3861 udelay(ad8402_udelay);
3862 writew(0, priv(dev)->main_iobase + CALIBRATION_REG);
3865 /* for pci-das6402/16, channel 0 is analog input gain and channel 1 is offset */
3866 static int ad8402_write_insn(struct comedi_device *dev,
3867 struct comedi_subdevice *s,
3868 struct comedi_insn *insn, unsigned int *data)
3870 int channel = CR_CHAN(insn->chanspec);
3872 /* return immediately if setting hasn't changed, since
3873 * programming these things is slow */
3874 if (priv(dev)->ad8402_state[channel] == data[0])
3877 priv(dev)->ad8402_state[channel] = data[0];
3879 ad8402_write(dev, channel, data[0]);
3884 static int ad8402_read_insn(struct comedi_device *dev,
3885 struct comedi_subdevice *s,
3886 struct comedi_insn *insn, unsigned int *data)
3888 unsigned int channel = CR_CHAN(insn->chanspec);
3890 data[0] = priv(dev)->ad8402_state[channel];
3895 static uint16_t read_eeprom(struct comedi_device *dev, uint8_t address)
3897 static const int bitstream_length = 11;
3898 static const int read_command = 0x6;
3899 unsigned int bitstream = (read_command << 8) | address;
3901 void __iomem * const plx_control_addr =
3902 priv(dev)->plx9080_iobase + PLX_CONTROL_REG;
3904 static const int value_length = 16;
3905 static const int eeprom_udelay = 1;
3907 udelay(eeprom_udelay);
3908 priv(dev)->plx_control_bits &= ~CTL_EE_CLK & ~CTL_EE_CS;
3909 /* make sure we don't send anything to the i2c bus on 4020 */
3910 priv(dev)->plx_control_bits |= CTL_USERO;
3911 writel(priv(dev)->plx_control_bits, plx_control_addr);
3912 /* activate serial eeprom */
3913 udelay(eeprom_udelay);
3914 priv(dev)->plx_control_bits |= CTL_EE_CS;
3915 writel(priv(dev)->plx_control_bits, plx_control_addr);
3917 /* write read command and desired memory address */
3918 for (bit = 1 << (bitstream_length - 1); bit; bit >>= 1) {
3919 /* set bit to be written */
3920 udelay(eeprom_udelay);
3921 if (bitstream & bit)
3922 priv(dev)->plx_control_bits |= CTL_EE_W;
3924 priv(dev)->plx_control_bits &= ~CTL_EE_W;
3925 writel(priv(dev)->plx_control_bits, plx_control_addr);
3927 udelay(eeprom_udelay);
3928 priv(dev)->plx_control_bits |= CTL_EE_CLK;
3929 writel(priv(dev)->plx_control_bits, plx_control_addr);
3930 udelay(eeprom_udelay);
3931 priv(dev)->plx_control_bits &= ~CTL_EE_CLK;
3932 writel(priv(dev)->plx_control_bits, plx_control_addr);
3934 /* read back value from eeprom memory location */
3936 for (bit = 1 << (value_length - 1); bit; bit >>= 1) {
3938 udelay(eeprom_udelay);
3939 priv(dev)->plx_control_bits |= CTL_EE_CLK;
3940 writel(priv(dev)->plx_control_bits, plx_control_addr);
3941 udelay(eeprom_udelay);
3942 priv(dev)->plx_control_bits &= ~CTL_EE_CLK;
3943 writel(priv(dev)->plx_control_bits, plx_control_addr);
3944 udelay(eeprom_udelay);
3945 if (readl(plx_control_addr) & CTL_EE_R)
3949 /* deactivate eeprom serial input */
3950 udelay(eeprom_udelay);
3951 priv(dev)->plx_control_bits &= ~CTL_EE_CS;
3952 writel(priv(dev)->plx_control_bits, plx_control_addr);
3957 static int eeprom_read_insn(struct comedi_device *dev,
3958 struct comedi_subdevice *s,
3959 struct comedi_insn *insn, unsigned int *data)
3961 data[0] = read_eeprom(dev, CR_CHAN(insn->chanspec));
3966 /* utility function that rounds desired timing to an achievable time, and
3967 * sets cmd members appropriately.
3968 * adc paces conversions from master clock by dividing by (x + 3) where x is 24 bit number
3970 static void check_adc_timing(struct comedi_device *dev, struct comedi_cmd *cmd)
3972 unsigned int convert_divisor = 0, scan_divisor;
3973 static const int min_convert_divisor = 3;
3974 static const int max_convert_divisor =
3975 max_counter_value + min_convert_divisor;
3976 static const int min_scan_divisor_4020 = 2;
3977 unsigned long long max_scan_divisor, min_scan_divisor;
3979 if (cmd->convert_src == TRIG_TIMER) {
3980 if (board(dev)->layout == LAYOUT_4020) {
3981 cmd->convert_arg = 0;
3984 get_divisor(cmd->convert_arg, cmd->flags);
3985 if (convert_divisor > max_convert_divisor)
3986 convert_divisor = max_convert_divisor;
3987 if (convert_divisor < min_convert_divisor)
3988 convert_divisor = min_convert_divisor;
3989 cmd->convert_arg = convert_divisor * TIMER_BASE;
3991 } else if (cmd->convert_src == TRIG_NOW)
3992 cmd->convert_arg = 0;
3994 if (cmd->scan_begin_src == TRIG_TIMER) {
3995 scan_divisor = get_divisor(cmd->scan_begin_arg, cmd->flags);
3996 if (cmd->convert_src == TRIG_TIMER) {
3997 /* XXX check for integer overflows */
3998 min_scan_divisor = convert_divisor * cmd->chanlist_len;
4000 (convert_divisor * cmd->chanlist_len - 1) +
4003 min_scan_divisor = min_scan_divisor_4020;
4004 max_scan_divisor = max_counter_value + min_scan_divisor;
4006 if (scan_divisor > max_scan_divisor)
4007 scan_divisor = max_scan_divisor;
4008 if (scan_divisor < min_scan_divisor)
4009 scan_divisor = min_scan_divisor;
4010 cmd->scan_begin_arg = scan_divisor * TIMER_BASE;
4016 /* Gets nearest achievable timing given master clock speed, does not
4017 * take into account possible minimum/maximum divisor values. Used
4018 * by other timing checking functions. */
4019 static unsigned int get_divisor(unsigned int ns, unsigned int flags)
4021 unsigned int divisor;
4023 switch (flags & TRIG_ROUND_MASK) {
4025 divisor = (ns + TIMER_BASE - 1) / TIMER_BASE;
4027 case TRIG_ROUND_DOWN:
4028 divisor = ns / TIMER_BASE;
4030 case TRIG_ROUND_NEAREST:
4032 divisor = (ns + TIMER_BASE / 2) / TIMER_BASE;
4038 static unsigned int get_ao_divisor(unsigned int ns, unsigned int flags)
4040 return get_divisor(ns, flags) - 2;
4043 /* adjusts the size of hardware fifo (which determines block size for dma xfers) */
4044 static int set_ai_fifo_size(struct comedi_device *dev, unsigned int num_samples)
4046 unsigned int num_fifo_entries;
4048 const struct hw_fifo_info *const fifo = board(dev)->ai_fifo;
4050 num_fifo_entries = num_samples / fifo->sample_packing_ratio;
4052 retval = set_ai_fifo_segment_length(dev,
4054 fifo->num_segments);
4058 num_samples = retval * fifo->num_segments * fifo->sample_packing_ratio;
4060 DEBUG_PRINT("set hardware fifo size to %i\n", num_samples);
4065 /* query length of fifo */
4066 static unsigned int ai_fifo_size(struct comedi_device *dev)
4068 return priv(dev)->ai_fifo_segment_length *
4069 board(dev)->ai_fifo->num_segments *
4070 board(dev)->ai_fifo->sample_packing_ratio;
4073 static int set_ai_fifo_segment_length(struct comedi_device *dev,
4074 unsigned int num_entries)
4076 static const int increment_size = 0x100;
4077 const struct hw_fifo_info *const fifo = board(dev)->ai_fifo;
4078 unsigned int num_increments;
4081 if (num_entries < increment_size)
4082 num_entries = increment_size;
4083 if (num_entries > fifo->max_segment_length)
4084 num_entries = fifo->max_segment_length;
4086 /* 1 == 256 entries, 2 == 512 entries, etc */
4087 num_increments = (num_entries + increment_size / 2) / increment_size;
4089 bits = (~(num_increments - 1)) & fifo->fifo_size_reg_mask;
4090 priv(dev)->fifo_size_bits &= ~fifo->fifo_size_reg_mask;
4091 priv(dev)->fifo_size_bits |= bits;
4092 writew(priv(dev)->fifo_size_bits,
4093 priv(dev)->main_iobase + FIFO_SIZE_REG);
4095 priv(dev)->ai_fifo_segment_length = num_increments * increment_size;
4097 DEBUG_PRINT("set hardware fifo segment length to %i\n",
4098 priv(dev)->ai_fifo_segment_length);
4100 return priv(dev)->ai_fifo_segment_length;
4103 /* pci-6025 8800 caldac:
4104 * address 0 == dac channel 0 offset
4105 * address 1 == dac channel 0 gain
4106 * address 2 == dac channel 1 offset
4107 * address 3 == dac channel 1 gain
4108 * address 4 == fine adc offset
4109 * address 5 == coarse adc offset
4110 * address 6 == coarse adc gain
4111 * address 7 == fine adc gain
4113 /* pci-6402/16 uses all 8 channels for dac:
4114 * address 0 == dac channel 0 fine gain
4115 * address 1 == dac channel 0 coarse gain
4116 * address 2 == dac channel 0 coarse offset
4117 * address 3 == dac channel 1 coarse offset
4118 * address 4 == dac channel 1 fine gain
4119 * address 5 == dac channel 1 coarse gain
4120 * address 6 == dac channel 0 fine offset
4121 * address 7 == dac channel 1 fine offset
4124 static int caldac_8800_write(struct comedi_device *dev, unsigned int address,
4127 static const int num_caldac_channels = 8;
4128 static const int bitstream_length = 11;
4129 unsigned int bitstream = ((address & 0x7) << 8) | value;
4130 unsigned int bit, register_bits;
4131 static const int caldac_8800_udelay = 1;
4133 if (address >= num_caldac_channels) {
4134 comedi_error(dev, "illegal caldac channel");
4137 for (bit = 1 << (bitstream_length - 1); bit; bit >>= 1) {
4139 if (bitstream & bit)
4140 register_bits |= SERIAL_DATA_IN_BIT;
4141 udelay(caldac_8800_udelay);
4142 writew(register_bits, priv(dev)->main_iobase + CALIBRATION_REG);
4143 register_bits |= SERIAL_CLOCK_BIT;
4144 udelay(caldac_8800_udelay);
4145 writew(register_bits, priv(dev)->main_iobase + CALIBRATION_REG);
4147 udelay(caldac_8800_udelay);
4148 writew(SELECT_8800_BIT, priv(dev)->main_iobase + CALIBRATION_REG);
4149 udelay(caldac_8800_udelay);
4150 writew(0, priv(dev)->main_iobase + CALIBRATION_REG);
4151 udelay(caldac_8800_udelay);
4156 static int caldac_i2c_write(struct comedi_device *dev,
4157 unsigned int caldac_channel, unsigned int value)
4159 uint8_t serial_bytes[3];
4162 /* manual has gain and offset bits switched */
4169 NOT_CLEAR_REGISTERS = 0x20,
4172 switch (caldac_channel) {
4173 case 0: /* chan 0 offset */
4174 i2c_addr = CALDAC0_I2C_ADDR;
4175 serial_bytes[0] = OFFSET_0_2;
4177 case 1: /* chan 1 offset */
4178 i2c_addr = CALDAC0_I2C_ADDR;
4179 serial_bytes[0] = OFFSET_1_3;
4181 case 2: /* chan 2 offset */
4182 i2c_addr = CALDAC1_I2C_ADDR;
4183 serial_bytes[0] = OFFSET_0_2;
4185 case 3: /* chan 3 offset */
4186 i2c_addr = CALDAC1_I2C_ADDR;
4187 serial_bytes[0] = OFFSET_1_3;
4189 case 4: /* chan 0 gain */
4190 i2c_addr = CALDAC0_I2C_ADDR;
4191 serial_bytes[0] = GAIN_0_2;
4193 case 5: /* chan 1 gain */
4194 i2c_addr = CALDAC0_I2C_ADDR;
4195 serial_bytes[0] = GAIN_1_3;
4197 case 6: /* chan 2 gain */
4198 i2c_addr = CALDAC1_I2C_ADDR;
4199 serial_bytes[0] = GAIN_0_2;
4201 case 7: /* chan 3 gain */
4202 i2c_addr = CALDAC1_I2C_ADDR;
4203 serial_bytes[0] = GAIN_1_3;
4206 comedi_error(dev, "invalid caldac channel\n");
4210 serial_bytes[1] = NOT_CLEAR_REGISTERS | ((value >> 8) & 0xf);
4211 serial_bytes[2] = value & 0xff;
4212 i2c_write(dev, i2c_addr, serial_bytes, 3);
4216 /* Their i2c requires a huge delay on setting clock or data high for some reason */
4217 static const int i2c_high_udelay = 1000;
4218 static const int i2c_low_udelay = 10;
4220 /* set i2c data line high or low */
4221 static void i2c_set_sda(struct comedi_device *dev, int state)
4223 static const int data_bit = CTL_EE_W;
4224 void __iomem *plx_control_addr = priv(dev)->plx9080_iobase +
4228 /* set data line high */
4229 priv(dev)->plx_control_bits &= ~data_bit;
4230 writel(priv(dev)->plx_control_bits, plx_control_addr);
4231 udelay(i2c_high_udelay);
4232 } else { /* set data line low */
4234 priv(dev)->plx_control_bits |= data_bit;
4235 writel(priv(dev)->plx_control_bits, plx_control_addr);
4236 udelay(i2c_low_udelay);
4240 /* set i2c clock line high or low */
4241 static void i2c_set_scl(struct comedi_device *dev, int state)
4243 static const int clock_bit = CTL_USERO;
4244 void __iomem *plx_control_addr = priv(dev)->plx9080_iobase +
4248 /* set clock line high */
4249 priv(dev)->plx_control_bits &= ~clock_bit;
4250 writel(priv(dev)->plx_control_bits, plx_control_addr);
4251 udelay(i2c_high_udelay);
4252 } else { /* set clock line low */
4254 priv(dev)->plx_control_bits |= clock_bit;
4255 writel(priv(dev)->plx_control_bits, plx_control_addr);
4256 udelay(i2c_low_udelay);
4260 static void i2c_write_byte(struct comedi_device *dev, uint8_t byte)
4263 unsigned int num_bits = 8;
4265 DEBUG_PRINT("writing to i2c byte 0x%x\n", byte);
4267 for (bit = 1 << (num_bits - 1); bit; bit >>= 1) {
4268 i2c_set_scl(dev, 0);
4270 i2c_set_sda(dev, 1);
4272 i2c_set_sda(dev, 0);
4273 i2c_set_scl(dev, 1);
4277 /* we can't really read the lines, so fake it */
4278 static int i2c_read_ack(struct comedi_device *dev)
4280 i2c_set_scl(dev, 0);
4281 i2c_set_sda(dev, 1);
4282 i2c_set_scl(dev, 1);
4284 return 0; /* return fake acknowledge bit */
4287 /* send start bit */
4288 static void i2c_start(struct comedi_device *dev)
4290 i2c_set_scl(dev, 1);
4291 i2c_set_sda(dev, 1);
4292 i2c_set_sda(dev, 0);
4296 static void i2c_stop(struct comedi_device *dev)
4298 i2c_set_scl(dev, 0);
4299 i2c_set_sda(dev, 0);
4300 i2c_set_scl(dev, 1);
4301 i2c_set_sda(dev, 1);
4304 static void i2c_write(struct comedi_device *dev, unsigned int address,
4305 const uint8_t * data, unsigned int length)
4309 static const int read_bit = 0x1;
4311 /* XXX need mutex to prevent simultaneous attempts to access eeprom and i2c bus */
4313 /* make sure we dont send anything to eeprom */
4314 priv(dev)->plx_control_bits &= ~CTL_EE_CS;
4319 /* send address and write bit */
4320 bitstream = (address << 1) & ~read_bit;
4321 i2c_write_byte(dev, bitstream);
4323 /* get acknowledge */
4324 if (i2c_read_ack(dev) != 0) {
4325 comedi_error(dev, "i2c write failed: no acknowledge");
4329 /* write data bytes */
4330 for (i = 0; i < length; i++) {
4331 i2c_write_byte(dev, data[i]);
4332 if (i2c_read_ack(dev) != 0) {
4333 comedi_error(dev, "i2c write failed: no acknowledge");
4341 MODULE_AUTHOR("Comedi http://www.comedi.org");
4342 MODULE_DESCRIPTION("Comedi low-level driver");
4343 MODULE_LICENSE("GPL");