1 // SPDX-License-Identifier: GPL-2.0+
3 * comedi/drivers/cb_pcidas64.c
4 * This is a driver for the ComputerBoards/MeasurementComputing PCI-DAS
5 * 64xx, 60xx, and 4020 cards.
7 * Author: Frank Mori Hess <fmhess@users.sourceforge.net>
8 * Copyright (C) 2001, 2002 Frank Mori Hess
10 * Thanks also go to the following people:
12 * Steve Rosenbluth, for providing the source code for
13 * his pci-das6402 driver, and source code for working QNX pci-6402
14 * drivers by Greg Laird and Mariusz Bogacz. None of the code was
15 * used directly here, but it was useful as an additional source of
16 * documentation on how to program the boards.
18 * John Sims, for much testing and feedback on pcidas-4020 support.
20 * COMEDI - Linux Control and Measurement Device Interface
21 * Copyright (C) 1997-8 David A. Schleef <ds@schleef.org>
26 * Description: MeasurementComputing PCI-DAS64xx, 60XX, and 4020 series
27 * with the PLX 9080 PCI controller
28 * Author: Frank Mori Hess <fmhess@users.sourceforge.net>
30 * Updated: Fri, 02 Nov 2012 18:58:55 +0000
31 * Devices: [Measurement Computing] PCI-DAS6402/16 (cb_pcidas64),
32 * PCI-DAS6402/12, PCI-DAS64/M1/16, PCI-DAS64/M2/16,
33 * PCI-DAS64/M3/16, PCI-DAS6402/16/JR, PCI-DAS64/M1/16/JR,
34 * PCI-DAS64/M2/16/JR, PCI-DAS64/M3/16/JR, PCI-DAS64/M1/14,
35 * PCI-DAS64/M2/14, PCI-DAS64/M3/14, PCI-DAS6013, PCI-DAS6014,
36 * PCI-DAS6023, PCI-DAS6025, PCI-DAS6030,
37 * PCI-DAS6031, PCI-DAS6032, PCI-DAS6033, PCI-DAS6034,
38 * PCI-DAS6035, PCI-DAS6036, PCI-DAS6040, PCI-DAS6052,
39 * PCI-DAS6070, PCI-DAS6071, PCI-DAS4020/12
41 * Configuration options:
44 * Manual attachment of PCI cards with the comedi_config utility is not
45 * supported by this driver; they are attached automatically.
47 * These boards may be autocalibrated with the comedi_calibrate utility.
49 * To select the bnc trigger input on the 4020 (instead of the dio input),
50 * specify a nonzero channel in the chanspec. If you wish to use an external
51 * master clock on the 4020, you may do so by setting the scan_begin_src
52 * to TRIG_OTHER, and using an INSN_CONFIG_TIMER_1 configuration insn
53 * to configure the divisor to use for the external clock.
55 * Some devices are not identified because the PCI device IDs are not yet
56 * known. If you have such a board, please let the maintainers know.
61 * make it return error if user attempts an ai command that uses the
62 * external queue, and an ao command simultaneously user counter subdevice
63 * there are a number of boards this driver will support when they are
64 * fully released, but does not yet since the pci device id numbers
65 * are not yet available.
67 * support prescaled 100khz clock for slow pacing (not available on 6000
70 * make ao fifo size adjustable like ai fifo
73 #include <linux/module.h>
74 #include <linux/delay.h>
75 #include <linux/interrupt.h>
77 #include "../comedi_pci.h"
82 #define TIMER_BASE 25 /* 40MHz master clock */
84 * 100kHz 'prescaled' clock for slow acquisition,
85 * maybe I'll support this someday
87 #define PRESCALED_TIMER_BASE 10000
88 #define DMA_BUFFER_SIZE 0x1000
89 #define DAC_FIFO_SIZE 0x2000
91 /* maximum value that can be loaded into board's 24-bit counters */
92 static const int max_counter_value = 0xffffff;
94 /* PCI-DAS64xxx base addresses */
96 /* devpriv->main_iobase registers */
97 enum write_only_registers {
98 INTR_ENABLE_REG = 0x0, /* interrupt enable register */
99 HW_CONFIG_REG = 0x2, /* hardware config register */
101 DAQ_ATRIG_LOW_4020_REG = 0xc,
102 ADC_CONTROL0_REG = 0x10, /* adc control register 0 */
103 ADC_CONTROL1_REG = 0x12, /* adc control register 1 */
104 CALIBRATION_REG = 0x14,
105 /* lower 16 bits of adc sample interval counter */
106 ADC_SAMPLE_INTERVAL_LOWER_REG = 0x16,
107 /* upper 8 bits of adc sample interval counter */
108 ADC_SAMPLE_INTERVAL_UPPER_REG = 0x18,
109 /* lower 16 bits of delay interval counter */
110 ADC_DELAY_INTERVAL_LOWER_REG = 0x1a,
111 /* upper 8 bits of delay interval counter */
112 ADC_DELAY_INTERVAL_UPPER_REG = 0x1c,
113 /* lower 16 bits of hardware conversion/scan counter */
114 ADC_COUNT_LOWER_REG = 0x1e,
115 /* upper 8 bits of hardware conversion/scan counter */
116 ADC_COUNT_UPPER_REG = 0x20,
117 ADC_START_REG = 0x22, /* software trigger to start acquisition */
118 ADC_CONVERT_REG = 0x24, /* initiates single conversion */
119 ADC_QUEUE_CLEAR_REG = 0x26, /* clears adc queue */
120 ADC_QUEUE_LOAD_REG = 0x28, /* loads adc queue */
121 ADC_BUFFER_CLEAR_REG = 0x2a,
122 /* high channel for internal queue, use adc_chan_bits() inline above */
123 ADC_QUEUE_HIGH_REG = 0x2c,
124 DAC_CONTROL0_REG = 0x50, /* dac control register 0 */
125 DAC_CONTROL1_REG = 0x52, /* dac control register 0 */
126 /* lower 16 bits of dac sample interval counter */
127 DAC_SAMPLE_INTERVAL_LOWER_REG = 0x54,
128 /* upper 8 bits of dac sample interval counter */
129 DAC_SAMPLE_INTERVAL_UPPER_REG = 0x56,
130 DAC_SELECT_REG = 0x60,
131 DAC_START_REG = 0x64,
132 DAC_BUFFER_CLEAR_REG = 0x66, /* clear dac buffer */
135 static inline unsigned int dac_convert_reg(unsigned int channel)
137 return 0x70 + (2 * (channel & 0x1));
140 static inline unsigned int dac_lsb_4020_reg(unsigned int channel)
142 return 0x70 + (4 * (channel & 0x1));
145 static inline unsigned int dac_msb_4020_reg(unsigned int channel)
147 return 0x72 + (4 * (channel & 0x1));
150 enum read_only_registers {
152 * hardware status register,
153 * reading this apparently clears pending interrupts as well
156 PIPE1_READ_REG = 0x4,
157 ADC_READ_PNTR_REG = 0x8,
158 LOWER_XFER_REG = 0x10,
159 ADC_WRITE_PNTR_REG = 0xc,
163 enum read_write_registers {
164 I8255_4020_REG = 0x48, /* 8255 offset, for 4020 only */
165 /* external channel/gain queue, uses same bits as ADC_QUEUE_LOAD_REG */
166 ADC_QUEUE_FIFO_REG = 0x100,
167 ADC_FIFO_REG = 0x200, /* adc data fifo */
168 /* dac data fifo, has weird interactions with external channel queue */
169 DAC_FIFO_REG = 0x300,
172 /* dev->mmio registers */
173 enum dio_counter_registers {
174 DIO_8255_OFFSET = 0x0,
177 DIO_DIRECTION_60XX_REG = 0x40,
178 DIO_DATA_60XX_REG = 0x48,
181 /* bit definitions for write-only registers */
183 enum intr_enable_contents {
184 ADC_INTR_SRC_MASK = 0x3, /* adc interrupt source mask */
185 ADC_INTR_QFULL_BITS = 0x0, /* interrupt fifo quarter full */
186 ADC_INTR_EOC_BITS = 0x1, /* interrupt end of conversion */
187 ADC_INTR_EOSCAN_BITS = 0x2, /* interrupt end of scan */
188 ADC_INTR_EOSEQ_BITS = 0x3, /* interrupt end of sequence mask */
189 EN_ADC_INTR_SRC_BIT = 0x4, /* enable adc interrupt source */
190 EN_ADC_DONE_INTR_BIT = 0x8, /* enable adc acquisition done intr */
191 DAC_INTR_SRC_MASK = 0x30,
192 DAC_INTR_QEMPTY_BITS = 0x0,
193 DAC_INTR_HIGH_CHAN_BITS = 0x10,
194 EN_DAC_INTR_SRC_BIT = 0x40, /* enable dac interrupt source */
195 EN_DAC_DONE_INTR_BIT = 0x80,
196 EN_ADC_ACTIVE_INTR_BIT = 0x200, /* enable adc active interrupt */
197 EN_ADC_STOP_INTR_BIT = 0x400, /* enable adc stop trigger interrupt */
198 EN_DAC_ACTIVE_INTR_BIT = 0x800, /* enable dac active interrupt */
199 EN_DAC_UNDERRUN_BIT = 0x4000, /* enable dac underrun status bit */
200 EN_ADC_OVERRUN_BIT = 0x8000, /* enable adc overrun status bit */
203 enum hw_config_contents {
204 MASTER_CLOCK_4020_MASK = 0x3, /* master clock source mask for 4020 */
205 INTERNAL_CLOCK_4020_BITS = 0x1, /* use 40 MHz internal master clock */
206 BNC_CLOCK_4020_BITS = 0x2, /* use BNC input for master clock */
207 EXT_CLOCK_4020_BITS = 0x3, /* use dio input for master clock */
208 EXT_QUEUE_BIT = 0x200, /* use external channel/gain queue */
209 /* use 225 nanosec strobe when loading dac instead of 50 nanosec */
210 SLOW_DAC_BIT = 0x400,
212 * bit with unknown function yet given as default value in pci-das64
215 HW_CONFIG_DUMMY_BITS = 0x2000,
216 /* bit selects channels 1/0 for analog input/output, otherwise 0/1 */
217 DMA_CH_SELECT_BIT = 0x8000,
218 FIFO_SIZE_REG = 0x4, /* allows adjustment of fifo sizes */
219 DAC_FIFO_SIZE_MASK = 0xff00, /* bits that set dac fifo size */
220 DAC_FIFO_BITS = 0xf800, /* 8k sample ao fifo */
223 enum daq_atrig_low_4020_contents {
224 /* use trig/ext clk bnc input for analog gate signal */
225 EXT_AGATE_BNC_BIT = 0x8000,
226 /* use trig/ext clk bnc input for external stop trigger signal */
227 EXT_STOP_TRIG_BNC_BIT = 0x4000,
228 /* use trig/ext clk bnc input for external start trigger signal */
229 EXT_START_TRIG_BNC_BIT = 0x2000,
232 static inline u16 analog_trig_low_threshold_bits(u16 threshold)
234 return threshold & 0xfff;
237 enum adc_control0_contents {
238 ADC_GATE_SRC_MASK = 0x3, /* bits that select gate */
239 ADC_SOFT_GATE_BITS = 0x1, /* software gate */
240 ADC_EXT_GATE_BITS = 0x2, /* external digital gate */
241 ADC_ANALOG_GATE_BITS = 0x3, /* analog level gate */
242 /* level-sensitive gate (for digital) */
243 ADC_GATE_LEVEL_BIT = 0x4,
244 ADC_GATE_POLARITY_BIT = 0x8, /* gate active low */
245 ADC_START_TRIG_SOFT_BITS = 0x10,
246 ADC_START_TRIG_EXT_BITS = 0x20,
247 ADC_START_TRIG_ANALOG_BITS = 0x30,
248 ADC_START_TRIG_MASK = 0x30,
249 ADC_START_TRIG_FALLING_BIT = 0x40, /* trig 1 uses falling edge */
250 /* external pacing uses falling edge */
251 ADC_EXT_CONV_FALLING_BIT = 0x800,
252 /* enable hardware scan counter */
253 ADC_SAMPLE_COUNTER_EN_BIT = 0x1000,
254 ADC_DMA_DISABLE_BIT = 0x4000, /* disables dma */
255 ADC_ENABLE_BIT = 0x8000, /* master adc enable */
258 enum adc_control1_contents {
259 /* should be set for boards with > 16 channels */
260 ADC_QUEUE_CONFIG_BIT = 0x1,
261 CONVERT_POLARITY_BIT = 0x10,
262 EOC_POLARITY_BIT = 0x20,
263 ADC_SW_GATE_BIT = 0x40, /* software gate of adc */
264 ADC_DITHER_BIT = 0x200, /* turn on extra noise for dithering */
265 RETRIGGER_BIT = 0x800,
266 ADC_LO_CHANNEL_4020_MASK = 0x300,
267 ADC_HI_CHANNEL_4020_MASK = 0xc00,
268 TWO_CHANNEL_4020_BITS = 0x1000, /* two channel mode for 4020 */
269 FOUR_CHANNEL_4020_BITS = 0x2000, /* four channel mode for 4020 */
270 CHANNEL_MODE_4020_MASK = 0x3000,
271 ADC_MODE_MASK = 0xf000,
274 static inline u16 adc_lo_chan_4020_bits(unsigned int channel)
276 return (channel & 0x3) << 8;
279 static inline u16 adc_hi_chan_4020_bits(unsigned int channel)
281 return (channel & 0x3) << 10;
284 static inline u16 adc_mode_bits(unsigned int mode)
286 return (mode & 0xf) << 12;
289 enum calibration_contents {
290 SELECT_8800_BIT = 0x1,
291 SELECT_8402_64XX_BIT = 0x2,
292 SELECT_1590_60XX_BIT = 0x2,
293 CAL_EN_64XX_BIT = 0x40, /* calibration enable for 64xx series */
294 SERIAL_DATA_IN_BIT = 0x80,
295 SERIAL_CLOCK_BIT = 0x100,
296 CAL_EN_60XX_BIT = 0x200, /* calibration enable for 60xx series */
297 CAL_GAIN_BIT = 0x800,
301 * calibration sources for 6025 are:
312 static inline u16 adc_src_bits(unsigned int source)
314 return (source & 0xf) << 3;
317 static inline u16 adc_convert_chan_4020_bits(unsigned int channel)
319 return (channel & 0x3) << 8;
322 enum adc_queue_load_contents {
323 UNIP_BIT = 0x800, /* unipolar/bipolar bit */
324 ADC_SE_DIFF_BIT = 0x1000, /* single-ended/ differential bit */
325 /* non-referenced single-ended (common-mode input) */
326 ADC_COMMON_BIT = 0x2000,
327 QUEUE_EOSEQ_BIT = 0x4000, /* queue end of sequence */
328 QUEUE_EOSCAN_BIT = 0x8000, /* queue end of scan */
331 static inline u16 adc_chan_bits(unsigned int channel)
333 return channel & 0x3f;
336 enum dac_control0_contents {
337 DAC_ENABLE_BIT = 0x8000, /* dac controller enable bit */
338 DAC_CYCLIC_STOP_BIT = 0x4000,
339 DAC_WAVEFORM_MODE_BIT = 0x100,
340 DAC_EXT_UPDATE_FALLING_BIT = 0x80,
341 DAC_EXT_UPDATE_ENABLE_BIT = 0x40,
342 WAVEFORM_TRIG_MASK = 0x30,
343 WAVEFORM_TRIG_DISABLED_BITS = 0x0,
344 WAVEFORM_TRIG_SOFT_BITS = 0x10,
345 WAVEFORM_TRIG_EXT_BITS = 0x20,
346 WAVEFORM_TRIG_ADC1_BITS = 0x30,
347 WAVEFORM_TRIG_FALLING_BIT = 0x8,
348 WAVEFORM_GATE_LEVEL_BIT = 0x4,
349 WAVEFORM_GATE_ENABLE_BIT = 0x2,
350 WAVEFORM_GATE_SELECT_BIT = 0x1,
353 enum dac_control1_contents {
354 DAC_WRITE_POLARITY_BIT = 0x800, /* board-dependent setting */
355 DAC1_EXT_REF_BIT = 0x200,
356 DAC0_EXT_REF_BIT = 0x100,
357 DAC_OUTPUT_ENABLE_BIT = 0x80, /* dac output enable bit */
358 DAC_UPDATE_POLARITY_BIT = 0x40, /* board-dependent setting */
359 DAC_SW_GATE_BIT = 0x20,
360 DAC1_UNIPOLAR_BIT = 0x8,
361 DAC0_UNIPOLAR_BIT = 0x2,
364 /* bit definitions for read-only registers */
365 enum hw_status_contents {
366 DAC_UNDERRUN_BIT = 0x1,
367 ADC_OVERRUN_BIT = 0x2,
368 DAC_ACTIVE_BIT = 0x4,
369 ADC_ACTIVE_BIT = 0x8,
370 DAC_INTR_PENDING_BIT = 0x10,
371 ADC_INTR_PENDING_BIT = 0x20,
374 EXT_INTR_PENDING_BIT = 0x100,
375 ADC_STOP_BIT = 0x200,
378 static inline u16 pipe_full_bits(u16 hw_status_bits)
380 return (hw_status_bits >> 10) & 0x3;
383 static inline unsigned int dma_chain_flag_bits(u16 prepost_bits)
385 return (prepost_bits >> 6) & 0x3;
388 static inline unsigned int adc_upper_read_ptr_code(u16 prepost_bits)
390 return (prepost_bits >> 12) & 0x3;
393 static inline unsigned int adc_upper_write_ptr_code(u16 prepost_bits)
395 return (prepost_bits >> 14) & 0x3;
398 /* I2C addresses for 4020 */
400 RANGE_CAL_I2C_ADDR = 0x20,
401 CALDAC0_I2C_ADDR = 0xc,
402 CALDAC1_I2C_ADDR = 0xd,
405 enum range_cal_i2c_contents {
406 /* bits that set what source the adc converter measures */
407 ADC_SRC_4020_MASK = 0x70,
408 /* make bnc trig/ext clock threshold 0V instead of 2.5V */
409 BNC_TRIG_THRESHOLD_0V_BIT = 0x80,
412 static inline u8 adc_src_4020_bits(unsigned int source)
414 return (source << 4) & ADC_SRC_4020_MASK;
417 static inline u8 attenuate_bit(unsigned int channel)
419 /* attenuate channel (+-5V input range) */
420 return 1 << (channel & 0x3);
423 /* analog input ranges for 64xx boards */
424 static const struct comedi_lrange ai_ranges_64xx = {
437 static const u8 ai_range_code_64xx[8] = {
438 0x0, 0x1, 0x2, 0x3, /* bipolar 10, 5, 2,5, 1.25 */
439 0x8, 0x9, 0xa, 0xb /* unipolar 10, 5, 2.5, 1.25 */
442 /* analog input ranges for 64-Mx boards */
443 static const struct comedi_lrange ai_ranges_64_mx = {
455 static const u8 ai_range_code_64_mx[7] = {
456 0x0, 0x1, 0x2, 0x3, /* bipolar 5, 2.5, 1.25, 0.625 */
457 0x9, 0xa, 0xb /* unipolar 5, 2.5, 1.25 */
460 /* analog input ranges for 60xx boards */
461 static const struct comedi_lrange ai_ranges_60xx = {
470 static const u8 ai_range_code_60xx[4] = {
471 0x0, 0x1, 0x4, 0x7 /* bipolar 10, 5, 0.5, 0.05 */
474 /* analog input ranges for 6030, etc boards */
475 static const struct comedi_lrange ai_ranges_6030 = {
494 static const u8 ai_range_code_6030[14] = {
495 0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, /* bip 10, 5, 2, 1, 0.5, 0.2, 0.1 */
496 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf /* uni 10, 5, 2, 1, 0.5, 0.2, 0.1 */
499 /* analog input ranges for 6052, etc boards */
500 static const struct comedi_lrange ai_ranges_6052 = {
520 static const u8 ai_range_code_6052[15] = {
521 0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, /* bipolar 10 ... 0.05 */
522 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf /* unipolar 10 ... 0.1 */
525 /* analog input ranges for 4020 board */
526 static const struct comedi_lrange ai_ranges_4020 = {
533 /* analog output ranges */
534 static const struct comedi_lrange ao_ranges_64xx = {
543 static const int ao_range_code_64xx[] = {
550 static const int ao_range_code_60xx[] = {
554 static const struct comedi_lrange ao_ranges_6030 = {
561 static const int ao_range_code_6030[] = {
566 static const struct comedi_lrange ao_ranges_4020 = {
573 static const int ao_range_code_4020[] = {
578 enum register_layout {
584 struct hw_fifo_info {
585 unsigned int num_segments;
586 unsigned int max_segment_length;
587 unsigned int sample_packing_ratio;
588 u16 fifo_size_reg_mask;
591 enum pcidas64_boardid {
594 BOARD_PCIDAS64_M1_16,
595 BOARD_PCIDAS64_M2_16,
596 BOARD_PCIDAS64_M3_16,
613 BOARD_PCIDAS6402_16_JR,
614 BOARD_PCIDAS64_M1_16_JR,
615 BOARD_PCIDAS64_M2_16_JR,
616 BOARD_PCIDAS64_M3_16_JR,
617 BOARD_PCIDAS64_M1_14,
618 BOARD_PCIDAS64_M2_14,
619 BOARD_PCIDAS64_M3_14,
622 struct pcidas64_board {
624 int ai_se_chans; /* number of ai inputs in single-ended mode */
625 int ai_bits; /* analog input resolution */
626 int ai_speed; /* fastest conversion period in ns */
627 const struct comedi_lrange *ai_range_table;
628 const u8 *ai_range_code;
629 int ao_nchan; /* number of analog out channels */
630 int ao_bits; /* analog output resolution */
631 int ao_scan_speed; /* analog output scan speed */
632 const struct comedi_lrange *ao_range_table;
633 const int *ao_range_code;
634 const struct hw_fifo_info *const ai_fifo;
635 /* different board families have slightly different registers */
636 enum register_layout layout;
640 static const struct hw_fifo_info ai_fifo_4020 = {
642 .max_segment_length = 0x8000,
643 .sample_packing_ratio = 2,
644 .fifo_size_reg_mask = 0x7f,
647 static const struct hw_fifo_info ai_fifo_64xx = {
649 .max_segment_length = 0x800,
650 .sample_packing_ratio = 1,
651 .fifo_size_reg_mask = 0x3f,
654 static const struct hw_fifo_info ai_fifo_60xx = {
656 .max_segment_length = 0x800,
657 .sample_packing_ratio = 1,
658 .fifo_size_reg_mask = 0x7f,
662 * maximum number of dma transfers we will chain together into a ring
663 * (and the maximum number of dma buffers we maintain)
665 #define MAX_AI_DMA_RING_COUNT (0x80000 / DMA_BUFFER_SIZE)
666 #define MIN_AI_DMA_RING_COUNT (0x10000 / DMA_BUFFER_SIZE)
667 #define AO_DMA_RING_COUNT (0x10000 / DMA_BUFFER_SIZE)
668 static inline unsigned int ai_dma_ring_count(const struct pcidas64_board *board)
670 if (board->layout == LAYOUT_4020)
671 return MAX_AI_DMA_RING_COUNT;
673 return MIN_AI_DMA_RING_COUNT;
676 static const int bytes_in_sample = 2;
678 static const struct pcidas64_board pcidas64_boards[] = {
679 [BOARD_PCIDAS6402_16] = {
680 .name = "pci-das6402/16",
686 .ao_scan_speed = 10000,
687 .layout = LAYOUT_64XX,
688 .ai_range_table = &ai_ranges_64xx,
689 .ai_range_code = ai_range_code_64xx,
690 .ao_range_table = &ao_ranges_64xx,
691 .ao_range_code = ao_range_code_64xx,
692 .ai_fifo = &ai_fifo_64xx,
695 [BOARD_PCIDAS6402_12] = {
696 .name = "pci-das6402/12", /* XXX check */
702 .ao_scan_speed = 10000,
703 .layout = LAYOUT_64XX,
704 .ai_range_table = &ai_ranges_64xx,
705 .ai_range_code = ai_range_code_64xx,
706 .ao_range_table = &ao_ranges_64xx,
707 .ao_range_code = ao_range_code_64xx,
708 .ai_fifo = &ai_fifo_64xx,
711 [BOARD_PCIDAS64_M1_16] = {
712 .name = "pci-das64/m1/16",
718 .ao_scan_speed = 10000,
719 .layout = LAYOUT_64XX,
720 .ai_range_table = &ai_ranges_64_mx,
721 .ai_range_code = ai_range_code_64_mx,
722 .ao_range_table = &ao_ranges_64xx,
723 .ao_range_code = ao_range_code_64xx,
724 .ai_fifo = &ai_fifo_64xx,
727 [BOARD_PCIDAS64_M2_16] = {
728 .name = "pci-das64/m2/16",
734 .ao_scan_speed = 10000,
735 .layout = LAYOUT_64XX,
736 .ai_range_table = &ai_ranges_64_mx,
737 .ai_range_code = ai_range_code_64_mx,
738 .ao_range_table = &ao_ranges_64xx,
739 .ao_range_code = ao_range_code_64xx,
740 .ai_fifo = &ai_fifo_64xx,
743 [BOARD_PCIDAS64_M3_16] = {
744 .name = "pci-das64/m3/16",
750 .ao_scan_speed = 10000,
751 .layout = LAYOUT_64XX,
752 .ai_range_table = &ai_ranges_64_mx,
753 .ai_range_code = ai_range_code_64_mx,
754 .ao_range_table = &ao_ranges_64xx,
755 .ao_range_code = ao_range_code_64xx,
756 .ai_fifo = &ai_fifo_64xx,
759 [BOARD_PCIDAS6013] = {
760 .name = "pci-das6013",
766 .layout = LAYOUT_60XX,
767 .ai_range_table = &ai_ranges_60xx,
768 .ai_range_code = ai_range_code_60xx,
769 .ao_range_table = &range_bipolar10,
770 .ao_range_code = ao_range_code_60xx,
771 .ai_fifo = &ai_fifo_60xx,
774 [BOARD_PCIDAS6014] = {
775 .name = "pci-das6014",
781 .ao_scan_speed = 100000,
782 .layout = LAYOUT_60XX,
783 .ai_range_table = &ai_ranges_60xx,
784 .ai_range_code = ai_range_code_60xx,
785 .ao_range_table = &range_bipolar10,
786 .ao_range_code = ao_range_code_60xx,
787 .ai_fifo = &ai_fifo_60xx,
790 [BOARD_PCIDAS6023] = {
791 .name = "pci-das6023",
796 .ao_scan_speed = 100000,
797 .layout = LAYOUT_60XX,
798 .ai_range_table = &ai_ranges_60xx,
799 .ai_range_code = ai_range_code_60xx,
800 .ao_range_table = &range_bipolar10,
801 .ao_range_code = ao_range_code_60xx,
802 .ai_fifo = &ai_fifo_60xx,
805 [BOARD_PCIDAS6025] = {
806 .name = "pci-das6025",
812 .ao_scan_speed = 100000,
813 .layout = LAYOUT_60XX,
814 .ai_range_table = &ai_ranges_60xx,
815 .ai_range_code = ai_range_code_60xx,
816 .ao_range_table = &range_bipolar10,
817 .ao_range_code = ao_range_code_60xx,
818 .ai_fifo = &ai_fifo_60xx,
821 [BOARD_PCIDAS6030] = {
822 .name = "pci-das6030",
828 .ao_scan_speed = 10000,
829 .layout = LAYOUT_60XX,
830 .ai_range_table = &ai_ranges_6030,
831 .ai_range_code = ai_range_code_6030,
832 .ao_range_table = &ao_ranges_6030,
833 .ao_range_code = ao_range_code_6030,
834 .ai_fifo = &ai_fifo_60xx,
837 [BOARD_PCIDAS6031] = {
838 .name = "pci-das6031",
844 .ao_scan_speed = 10000,
845 .layout = LAYOUT_60XX,
846 .ai_range_table = &ai_ranges_6030,
847 .ai_range_code = ai_range_code_6030,
848 .ao_range_table = &ao_ranges_6030,
849 .ao_range_code = ao_range_code_6030,
850 .ai_fifo = &ai_fifo_60xx,
853 [BOARD_PCIDAS6032] = {
854 .name = "pci-das6032",
859 .layout = LAYOUT_60XX,
860 .ai_range_table = &ai_ranges_6030,
861 .ai_range_code = ai_range_code_6030,
862 .ai_fifo = &ai_fifo_60xx,
865 [BOARD_PCIDAS6033] = {
866 .name = "pci-das6033",
871 .layout = LAYOUT_60XX,
872 .ai_range_table = &ai_ranges_6030,
873 .ai_range_code = ai_range_code_6030,
874 .ai_fifo = &ai_fifo_60xx,
877 [BOARD_PCIDAS6034] = {
878 .name = "pci-das6034",
884 .layout = LAYOUT_60XX,
885 .ai_range_table = &ai_ranges_60xx,
886 .ai_range_code = ai_range_code_60xx,
887 .ai_fifo = &ai_fifo_60xx,
890 [BOARD_PCIDAS6035] = {
891 .name = "pci-das6035",
897 .ao_scan_speed = 100000,
898 .layout = LAYOUT_60XX,
899 .ai_range_table = &ai_ranges_60xx,
900 .ai_range_code = ai_range_code_60xx,
901 .ao_range_table = &range_bipolar10,
902 .ao_range_code = ao_range_code_60xx,
903 .ai_fifo = &ai_fifo_60xx,
906 [BOARD_PCIDAS6036] = {
907 .name = "pci-das6036",
913 .ao_scan_speed = 100000,
914 .layout = LAYOUT_60XX,
915 .ai_range_table = &ai_ranges_60xx,
916 .ai_range_code = ai_range_code_60xx,
917 .ao_range_table = &range_bipolar10,
918 .ao_range_code = ao_range_code_60xx,
919 .ai_fifo = &ai_fifo_60xx,
922 [BOARD_PCIDAS6040] = {
923 .name = "pci-das6040",
929 .ao_scan_speed = 1000,
930 .layout = LAYOUT_60XX,
931 .ai_range_table = &ai_ranges_6052,
932 .ai_range_code = ai_range_code_6052,
933 .ao_range_table = &ao_ranges_6030,
934 .ao_range_code = ao_range_code_6030,
935 .ai_fifo = &ai_fifo_60xx,
938 [BOARD_PCIDAS6052] = {
939 .name = "pci-das6052",
945 .ao_scan_speed = 3333,
946 .layout = LAYOUT_60XX,
947 .ai_range_table = &ai_ranges_6052,
948 .ai_range_code = ai_range_code_6052,
949 .ao_range_table = &ao_ranges_6030,
950 .ao_range_code = ao_range_code_6030,
951 .ai_fifo = &ai_fifo_60xx,
954 [BOARD_PCIDAS6070] = {
955 .name = "pci-das6070",
961 .ao_scan_speed = 1000,
962 .layout = LAYOUT_60XX,
963 .ai_range_table = &ai_ranges_6052,
964 .ai_range_code = ai_range_code_6052,
965 .ao_range_table = &ao_ranges_6030,
966 .ao_range_code = ao_range_code_6030,
967 .ai_fifo = &ai_fifo_60xx,
970 [BOARD_PCIDAS6071] = {
971 .name = "pci-das6071",
977 .ao_scan_speed = 1000,
978 .layout = LAYOUT_60XX,
979 .ai_range_table = &ai_ranges_6052,
980 .ai_range_code = ai_range_code_6052,
981 .ao_range_table = &ao_ranges_6030,
982 .ao_range_code = ao_range_code_6030,
983 .ai_fifo = &ai_fifo_60xx,
986 [BOARD_PCIDAS4020_12] = {
987 .name = "pci-das4020/12",
993 .ao_scan_speed = 0, /* no hardware pacing on ao */
994 .layout = LAYOUT_4020,
995 .ai_range_table = &ai_ranges_4020,
996 .ao_range_table = &ao_ranges_4020,
997 .ao_range_code = ao_range_code_4020,
998 .ai_fifo = &ai_fifo_4020,
1002 /* The device id for these boards is unknown */
1004 [BOARD_PCIDAS6402_16_JR] = {
1005 .name = "pci-das6402/16/jr",
1010 .ao_scan_speed = 10000,
1011 .layout = LAYOUT_64XX,
1012 .ai_range_table = &ai_ranges_64xx,
1013 .ai_range_code = ai_range_code_64xx,
1014 .ai_fifo = ai_fifo_64xx,
1017 [BOARD_PCIDAS64_M1_16_JR] = {
1018 .name = "pci-das64/m1/16/jr",
1023 .ao_scan_speed = 10000,
1024 .layout = LAYOUT_64XX,
1025 .ai_range_table = &ai_ranges_64_mx,
1026 .ai_range_code = ai_range_code_64_mx,
1027 .ai_fifo = ai_fifo_64xx,
1030 [BOARD_PCIDAS64_M2_16_JR] = {
1031 .name = "pci-das64/m2/16/jr",
1036 .ao_scan_speed = 10000,
1037 .layout = LAYOUT_64XX,
1038 .ai_range_table = &ai_ranges_64_mx,
1039 .ai_range_code = ai_range_code_64_mx,
1040 .ai_fifo = ai_fifo_64xx,
1043 [BOARD_PCIDAS64_M3_16_JR] = {
1044 .name = "pci-das64/m3/16/jr",
1049 .ao_scan_speed = 10000,
1050 .layout = LAYOUT_64XX,
1051 .ai_range_table = &ai_ranges_64_mx,
1052 .ai_range_code = ai_range_code_64_mx,
1053 .ai_fifo = ai_fifo_64xx,
1056 [BOARD_PCIDAS64_M1_14] = {
1057 .name = "pci-das64/m1/14",
1062 .ao_scan_speed = 10000,
1063 .layout = LAYOUT_64XX,
1064 .ai_range_table = &ai_ranges_64_mx,
1065 .ai_range_code = ai_range_code_64_mx,
1066 .ai_fifo = ai_fifo_64xx,
1069 [BOARD_PCIDAS64_M2_14] = {
1070 .name = "pci-das64/m2/14",
1075 .ao_scan_speed = 10000,
1076 .layout = LAYOUT_64XX,
1077 .ai_range_table = &ai_ranges_64_mx,
1078 .ai_range_code = ai_range_code_64_mx,
1079 .ai_fifo = ai_fifo_64xx,
1082 [BOARD_PCIDAS64_M3_14] = {
1083 .name = "pci-das64/m3/14",
1088 .ao_scan_speed = 10000,
1089 .layout = LAYOUT_64XX,
1090 .ai_range_table = &ai_ranges_64_mx,
1091 .ai_range_code = ai_range_code_64_mx,
1092 .ai_fifo = ai_fifo_64xx,
1098 static inline unsigned short se_diff_bit_6xxx(struct comedi_device *dev,
1099 int use_differential)
1101 const struct pcidas64_board *board = dev->board_ptr;
1103 if ((board->layout == LAYOUT_64XX && !use_differential) ||
1104 (board->layout == LAYOUT_60XX && use_differential))
1105 return ADC_SE_DIFF_BIT;
1110 struct ext_clock_info {
1111 /* master clock divisor to use for scans with external master clock */
1112 unsigned int divisor;
1113 /* chanspec for master clock input when used as scan begin src */
1114 unsigned int chanspec;
1117 /* this structure is for data unique to this hardware driver. */
1118 struct pcidas64_private {
1119 /* base addresses (physical) */
1120 resource_size_t main_phys_iobase;
1121 resource_size_t dio_counter_phys_iobase;
1122 /* base addresses (ioremapped) */
1123 void __iomem *plx9080_iobase;
1124 void __iomem *main_iobase;
1125 /* local address (used by dma controller) */
1128 /* dma buffers for analog input */
1129 u16 *ai_buffer[MAX_AI_DMA_RING_COUNT];
1130 /* physical addresses of ai dma buffers */
1131 dma_addr_t ai_buffer_bus_addr[MAX_AI_DMA_RING_COUNT];
1133 * array of ai dma descriptors read by plx9080,
1134 * allocated to get proper alignment
1136 struct plx_dma_desc *ai_dma_desc;
1137 /* physical address of ai dma descriptor array */
1138 dma_addr_t ai_dma_desc_bus_addr;
1140 * index of the ai dma descriptor/buffer
1141 * that is currently being used
1143 unsigned int ai_dma_index;
1144 /* dma buffers for analog output */
1145 u16 *ao_buffer[AO_DMA_RING_COUNT];
1146 /* physical addresses of ao dma buffers */
1147 dma_addr_t ao_buffer_bus_addr[AO_DMA_RING_COUNT];
1148 struct plx_dma_desc *ao_dma_desc;
1149 dma_addr_t ao_dma_desc_bus_addr;
1150 /* keeps track of buffer where the next ao sample should go */
1151 unsigned int ao_dma_index;
1152 unsigned int hw_revision; /* stc chip hardware revision number */
1153 /* last bits sent to INTR_ENABLE_REG register */
1154 unsigned int intr_enable_bits;
1155 /* last bits sent to ADC_CONTROL1_REG register */
1156 u16 adc_control1_bits;
1157 /* last bits sent to FIFO_SIZE_REG register */
1159 /* last bits sent to HW_CONFIG_REG register */
1161 u16 dac_control1_bits;
1162 /* last bits written to plx9080 control register */
1163 u32 plx_control_bits;
1164 /* last bits written to plx interrupt control and status register */
1165 u32 plx_intcsr_bits;
1166 /* index of calibration source readable through ai ch0 */
1167 int calibration_source;
1168 /* bits written to i2c calibration/range register */
1169 u8 i2c_cal_range_bits;
1170 /* configure digital triggers to trigger on falling edge */
1171 unsigned int ext_trig_falling;
1172 short ai_cmd_running;
1173 unsigned int ai_fifo_segment_length;
1174 struct ext_clock_info ext_clock;
1175 unsigned short ao_bounce_buffer[DAC_FIFO_SIZE];
1178 static unsigned int ai_range_bits_6xxx(const struct comedi_device *dev,
1179 unsigned int range_index)
1181 const struct pcidas64_board *board = dev->board_ptr;
1183 return board->ai_range_code[range_index] << 8;
1186 static unsigned int hw_revision(const struct comedi_device *dev,
1189 const struct pcidas64_board *board = dev->board_ptr;
1191 if (board->layout == LAYOUT_4020)
1192 return (hw_status_bits >> 13) & 0x7;
1194 return (hw_status_bits >> 12) & 0xf;
1197 static void set_dac_range_bits(struct comedi_device *dev,
1198 u16 *bits, unsigned int channel,
1201 const struct pcidas64_board *board = dev->board_ptr;
1202 unsigned int code = board->ao_range_code[range];
1205 dev_err(dev->class_dev, "bug! bad channel?\n");
1207 dev_err(dev->class_dev, "bug! bad range code?\n");
1209 *bits &= ~(0x3 << (2 * channel));
1210 *bits |= code << (2 * channel);
1213 static inline int ao_cmd_is_supported(const struct pcidas64_board *board)
1215 return board->ao_nchan && board->layout != LAYOUT_4020;
1218 static void abort_dma(struct comedi_device *dev, unsigned int channel)
1220 struct pcidas64_private *devpriv = dev->private;
1221 unsigned long flags;
1223 /* spinlock for plx dma control/status reg */
1224 spin_lock_irqsave(&dev->spinlock, flags);
1226 plx9080_abort_dma(devpriv->plx9080_iobase, channel);
1228 spin_unlock_irqrestore(&dev->spinlock, flags);
1231 static void disable_plx_interrupts(struct comedi_device *dev)
1233 struct pcidas64_private *devpriv = dev->private;
1235 devpriv->plx_intcsr_bits = 0;
1236 writel(devpriv->plx_intcsr_bits,
1237 devpriv->plx9080_iobase + PLX_REG_INTCSR);
1240 static void disable_ai_interrupts(struct comedi_device *dev)
1242 struct pcidas64_private *devpriv = dev->private;
1243 unsigned long flags;
1245 spin_lock_irqsave(&dev->spinlock, flags);
1246 devpriv->intr_enable_bits &=
1247 ~EN_ADC_INTR_SRC_BIT & ~EN_ADC_DONE_INTR_BIT &
1248 ~EN_ADC_ACTIVE_INTR_BIT & ~EN_ADC_STOP_INTR_BIT &
1249 ~EN_ADC_OVERRUN_BIT & ~ADC_INTR_SRC_MASK;
1250 writew(devpriv->intr_enable_bits,
1251 devpriv->main_iobase + INTR_ENABLE_REG);
1252 spin_unlock_irqrestore(&dev->spinlock, flags);
1255 static void enable_ai_interrupts(struct comedi_device *dev,
1256 const struct comedi_cmd *cmd)
1258 const struct pcidas64_board *board = dev->board_ptr;
1259 struct pcidas64_private *devpriv = dev->private;
1261 unsigned long flags;
1263 bits = EN_ADC_OVERRUN_BIT | EN_ADC_DONE_INTR_BIT |
1264 EN_ADC_ACTIVE_INTR_BIT | EN_ADC_STOP_INTR_BIT;
1266 * Use pio transfer and interrupt on end of conversion
1267 * if CMDF_WAKE_EOS flag is set.
1269 if (cmd->flags & CMDF_WAKE_EOS) {
1270 /* 4020 doesn't support pio transfers except for fifo dregs */
1271 if (board->layout != LAYOUT_4020)
1272 bits |= ADC_INTR_EOSCAN_BITS | EN_ADC_INTR_SRC_BIT;
1274 spin_lock_irqsave(&dev->spinlock, flags);
1275 devpriv->intr_enable_bits |= bits;
1276 writew(devpriv->intr_enable_bits,
1277 devpriv->main_iobase + INTR_ENABLE_REG);
1278 spin_unlock_irqrestore(&dev->spinlock, flags);
1281 /* initialize plx9080 chip */
1282 static void init_plx9080(struct comedi_device *dev)
1284 const struct pcidas64_board *board = dev->board_ptr;
1285 struct pcidas64_private *devpriv = dev->private;
1287 void __iomem *plx_iobase = devpriv->plx9080_iobase;
1289 devpriv->plx_control_bits =
1290 readl(devpriv->plx9080_iobase + PLX_REG_CNTRL);
1293 bits = PLX_BIGEND_DMA0 | PLX_BIGEND_DMA1;
1297 writel(bits, devpriv->plx9080_iobase + PLX_REG_BIGEND);
1299 disable_plx_interrupts(dev);
1304 /* configure dma0 mode */
1306 /* enable ready input, not sure if this is necessary */
1307 bits |= PLX_DMAMODE_READYIEN;
1308 /* enable bterm, not sure if this is necessary */
1309 bits |= PLX_DMAMODE_BTERMIEN;
1310 /* enable dma chaining */
1311 bits |= PLX_DMAMODE_CHAINEN;
1313 * enable interrupt on dma done
1314 * (probably don't need this, since chain never finishes)
1316 bits |= PLX_DMAMODE_DONEIEN;
1318 * don't increment local address during transfers
1319 * (we are transferring from a fixed fifo register)
1321 bits |= PLX_DMAMODE_LACONST;
1322 /* route dma interrupt to pci bus */
1323 bits |= PLX_DMAMODE_INTRPCI;
1324 /* enable demand mode */
1325 bits |= PLX_DMAMODE_DEMAND;
1326 /* enable local burst mode */
1327 bits |= PLX_DMAMODE_BURSTEN;
1328 /* 4020 uses 32 bit dma */
1329 if (board->layout == LAYOUT_4020)
1330 bits |= PLX_DMAMODE_WIDTH_32;
1331 else /* localspace0 bus is 16 bits wide */
1332 bits |= PLX_DMAMODE_WIDTH_16;
1333 writel(bits, plx_iobase + PLX_REG_DMAMODE1);
1334 if (ao_cmd_is_supported(board))
1335 writel(bits, plx_iobase + PLX_REG_DMAMODE0);
1337 /* enable interrupts on plx 9080 */
1338 devpriv->plx_intcsr_bits |=
1339 PLX_INTCSR_LSEABORTEN | PLX_INTCSR_LSEPARITYEN | PLX_INTCSR_PIEN |
1340 PLX_INTCSR_PLIEN | PLX_INTCSR_PABORTIEN | PLX_INTCSR_LIOEN |
1341 PLX_INTCSR_DMA0IEN | PLX_INTCSR_DMA1IEN;
1342 writel(devpriv->plx_intcsr_bits,
1343 devpriv->plx9080_iobase + PLX_REG_INTCSR);
1346 static void disable_ai_pacing(struct comedi_device *dev)
1348 struct pcidas64_private *devpriv = dev->private;
1349 unsigned long flags;
1351 disable_ai_interrupts(dev);
1353 spin_lock_irqsave(&dev->spinlock, flags);
1354 devpriv->adc_control1_bits &= ~ADC_SW_GATE_BIT;
1355 writew(devpriv->adc_control1_bits,
1356 devpriv->main_iobase + ADC_CONTROL1_REG);
1357 spin_unlock_irqrestore(&dev->spinlock, flags);
1359 /* disable pacing, triggering, etc */
1360 writew(ADC_DMA_DISABLE_BIT | ADC_SOFT_GATE_BITS | ADC_GATE_LEVEL_BIT,
1361 devpriv->main_iobase + ADC_CONTROL0_REG);
1364 static int set_ai_fifo_segment_length(struct comedi_device *dev,
1365 unsigned int num_entries)
1367 const struct pcidas64_board *board = dev->board_ptr;
1368 struct pcidas64_private *devpriv = dev->private;
1369 static const int increment_size = 0x100;
1370 const struct hw_fifo_info *const fifo = board->ai_fifo;
1371 unsigned int num_increments;
1374 if (num_entries < increment_size)
1375 num_entries = increment_size;
1376 if (num_entries > fifo->max_segment_length)
1377 num_entries = fifo->max_segment_length;
1379 /* 1 == 256 entries, 2 == 512 entries, etc */
1380 num_increments = DIV_ROUND_CLOSEST(num_entries, increment_size);
1382 bits = (~(num_increments - 1)) & fifo->fifo_size_reg_mask;
1383 devpriv->fifo_size_bits &= ~fifo->fifo_size_reg_mask;
1384 devpriv->fifo_size_bits |= bits;
1385 writew(devpriv->fifo_size_bits,
1386 devpriv->main_iobase + FIFO_SIZE_REG);
1388 devpriv->ai_fifo_segment_length = num_increments * increment_size;
1390 return devpriv->ai_fifo_segment_length;
1394 * adjusts the size of hardware fifo (which determines block size for dma xfers)
1396 static int set_ai_fifo_size(struct comedi_device *dev, unsigned int num_samples)
1398 const struct pcidas64_board *board = dev->board_ptr;
1399 unsigned int num_fifo_entries;
1401 const struct hw_fifo_info *const fifo = board->ai_fifo;
1403 num_fifo_entries = num_samples / fifo->sample_packing_ratio;
1405 retval = set_ai_fifo_segment_length(dev,
1407 fifo->num_segments);
1411 return retval * fifo->num_segments * fifo->sample_packing_ratio;
1414 /* query length of fifo */
1415 static unsigned int ai_fifo_size(struct comedi_device *dev)
1417 const struct pcidas64_board *board = dev->board_ptr;
1418 struct pcidas64_private *devpriv = dev->private;
1420 return devpriv->ai_fifo_segment_length *
1421 board->ai_fifo->num_segments *
1422 board->ai_fifo->sample_packing_ratio;
1425 static void init_stc_registers(struct comedi_device *dev)
1427 const struct pcidas64_board *board = dev->board_ptr;
1428 struct pcidas64_private *devpriv = dev->private;
1430 unsigned long flags;
1432 spin_lock_irqsave(&dev->spinlock, flags);
1435 * bit should be set for 6025,
1436 * although docs say boards with <= 16 chans should be cleared XXX
1439 devpriv->adc_control1_bits |= ADC_QUEUE_CONFIG_BIT;
1440 writew(devpriv->adc_control1_bits,
1441 devpriv->main_iobase + ADC_CONTROL1_REG);
1443 /* 6402/16 manual says this register must be initialized to 0xff? */
1444 writew(0xff, devpriv->main_iobase + ADC_SAMPLE_INTERVAL_UPPER_REG);
1446 bits = SLOW_DAC_BIT | DMA_CH_SELECT_BIT;
1447 if (board->layout == LAYOUT_4020)
1448 bits |= INTERNAL_CLOCK_4020_BITS;
1449 devpriv->hw_config_bits |= bits;
1450 writew(devpriv->hw_config_bits,
1451 devpriv->main_iobase + HW_CONFIG_REG);
1453 writew(0, devpriv->main_iobase + DAQ_SYNC_REG);
1454 writew(0, devpriv->main_iobase + CALIBRATION_REG);
1456 spin_unlock_irqrestore(&dev->spinlock, flags);
1458 /* set fifos to maximum size */
1459 devpriv->fifo_size_bits |= DAC_FIFO_BITS;
1460 set_ai_fifo_segment_length(dev, board->ai_fifo->max_segment_length);
1462 devpriv->dac_control1_bits = DAC_OUTPUT_ENABLE_BIT;
1463 devpriv->intr_enable_bits =
1464 /* EN_DAC_INTR_SRC_BIT | DAC_INTR_QEMPTY_BITS | */
1465 EN_DAC_DONE_INTR_BIT | EN_DAC_UNDERRUN_BIT;
1466 writew(devpriv->intr_enable_bits,
1467 devpriv->main_iobase + INTR_ENABLE_REG);
1469 disable_ai_pacing(dev);
1472 static int alloc_and_init_dma_members(struct comedi_device *dev)
1474 const struct pcidas64_board *board = dev->board_ptr;
1475 struct pci_dev *pcidev = comedi_to_pci_dev(dev);
1476 struct pcidas64_private *devpriv = dev->private;
1479 /* allocate pci dma buffers */
1480 for (i = 0; i < ai_dma_ring_count(board); i++) {
1481 devpriv->ai_buffer[i] =
1482 dma_alloc_coherent(&pcidev->dev, DMA_BUFFER_SIZE,
1483 &devpriv->ai_buffer_bus_addr[i],
1485 if (!devpriv->ai_buffer[i])
1488 for (i = 0; i < AO_DMA_RING_COUNT; i++) {
1489 if (ao_cmd_is_supported(board)) {
1490 devpriv->ao_buffer[i] =
1491 dma_alloc_coherent(&pcidev->dev,
1493 &devpriv->ao_buffer_bus_addr[i],
1495 if (!devpriv->ao_buffer[i])
1499 /* allocate dma descriptors */
1500 devpriv->ai_dma_desc =
1501 dma_alloc_coherent(&pcidev->dev, sizeof(struct plx_dma_desc) *
1502 ai_dma_ring_count(board),
1503 &devpriv->ai_dma_desc_bus_addr, GFP_KERNEL);
1504 if (!devpriv->ai_dma_desc)
1507 if (ao_cmd_is_supported(board)) {
1508 devpriv->ao_dma_desc =
1509 dma_alloc_coherent(&pcidev->dev,
1510 sizeof(struct plx_dma_desc) *
1512 &devpriv->ao_dma_desc_bus_addr,
1514 if (!devpriv->ao_dma_desc)
1517 /* initialize dma descriptors */
1518 for (i = 0; i < ai_dma_ring_count(board); i++) {
1519 devpriv->ai_dma_desc[i].pci_start_addr =
1520 cpu_to_le32(devpriv->ai_buffer_bus_addr[i]);
1521 if (board->layout == LAYOUT_4020)
1522 devpriv->ai_dma_desc[i].local_start_addr =
1523 cpu_to_le32(devpriv->local1_iobase +
1526 devpriv->ai_dma_desc[i].local_start_addr =
1527 cpu_to_le32(devpriv->local0_iobase +
1529 devpriv->ai_dma_desc[i].transfer_size = cpu_to_le32(0);
1530 devpriv->ai_dma_desc[i].next =
1531 cpu_to_le32((devpriv->ai_dma_desc_bus_addr +
1532 ((i + 1) % ai_dma_ring_count(board)) *
1533 sizeof(devpriv->ai_dma_desc[0])) |
1534 PLX_DMADPR_DESCPCI | PLX_DMADPR_TCINTR |
1535 PLX_DMADPR_XFERL2P);
1537 if (ao_cmd_is_supported(board)) {
1538 for (i = 0; i < AO_DMA_RING_COUNT; i++) {
1539 devpriv->ao_dma_desc[i].pci_start_addr =
1540 cpu_to_le32(devpriv->ao_buffer_bus_addr[i]);
1541 devpriv->ao_dma_desc[i].local_start_addr =
1542 cpu_to_le32(devpriv->local0_iobase +
1544 devpriv->ao_dma_desc[i].transfer_size = cpu_to_le32(0);
1545 devpriv->ao_dma_desc[i].next =
1546 cpu_to_le32((devpriv->ao_dma_desc_bus_addr +
1547 ((i + 1) % (AO_DMA_RING_COUNT)) *
1548 sizeof(devpriv->ao_dma_desc[0])) |
1549 PLX_DMADPR_DESCPCI |
1556 static void cb_pcidas64_free_dma(struct comedi_device *dev)
1558 const struct pcidas64_board *board = dev->board_ptr;
1559 struct pci_dev *pcidev = comedi_to_pci_dev(dev);
1560 struct pcidas64_private *devpriv = dev->private;
1566 /* free pci dma buffers */
1567 for (i = 0; i < ai_dma_ring_count(board); i++) {
1568 if (devpriv->ai_buffer[i])
1569 dma_free_coherent(&pcidev->dev,
1571 devpriv->ai_buffer[i],
1572 devpriv->ai_buffer_bus_addr[i]);
1574 for (i = 0; i < AO_DMA_RING_COUNT; i++) {
1575 if (devpriv->ao_buffer[i])
1576 dma_free_coherent(&pcidev->dev,
1578 devpriv->ao_buffer[i],
1579 devpriv->ao_buffer_bus_addr[i]);
1581 /* free dma descriptors */
1582 if (devpriv->ai_dma_desc)
1583 dma_free_coherent(&pcidev->dev,
1584 sizeof(struct plx_dma_desc) *
1585 ai_dma_ring_count(board),
1586 devpriv->ai_dma_desc,
1587 devpriv->ai_dma_desc_bus_addr);
1588 if (devpriv->ao_dma_desc)
1589 dma_free_coherent(&pcidev->dev,
1590 sizeof(struct plx_dma_desc) *
1592 devpriv->ao_dma_desc,
1593 devpriv->ao_dma_desc_bus_addr);
1596 static inline void warn_external_queue(struct comedi_device *dev)
1598 dev_err(dev->class_dev,
1599 "AO command and AI external channel queue cannot be used simultaneously\n");
1600 dev_err(dev->class_dev,
1601 "Use internal AI channel queue (channels must be consecutive and use same range/aref)\n");
1605 * their i2c requires a huge delay on setting clock or data high for some reason
1607 static const int i2c_high_udelay = 1000;
1608 static const int i2c_low_udelay = 10;
1610 /* set i2c data line high or low */
1611 static void i2c_set_sda(struct comedi_device *dev, int state)
1613 struct pcidas64_private *devpriv = dev->private;
1614 static const int data_bit = PLX_CNTRL_EEWB;
1615 void __iomem *plx_control_addr = devpriv->plx9080_iobase +
1618 if (state) { /* set data line high */
1619 devpriv->plx_control_bits &= ~data_bit;
1620 writel(devpriv->plx_control_bits, plx_control_addr);
1621 udelay(i2c_high_udelay);
1622 } else { /* set data line low */
1623 devpriv->plx_control_bits |= data_bit;
1624 writel(devpriv->plx_control_bits, plx_control_addr);
1625 udelay(i2c_low_udelay);
1629 /* set i2c clock line high or low */
1630 static void i2c_set_scl(struct comedi_device *dev, int state)
1632 struct pcidas64_private *devpriv = dev->private;
1633 static const int clock_bit = PLX_CNTRL_USERO;
1634 void __iomem *plx_control_addr = devpriv->plx9080_iobase +
1637 if (state) { /* set clock line high */
1638 devpriv->plx_control_bits &= ~clock_bit;
1639 writel(devpriv->plx_control_bits, plx_control_addr);
1640 udelay(i2c_high_udelay);
1641 } else { /* set clock line low */
1642 devpriv->plx_control_bits |= clock_bit;
1643 writel(devpriv->plx_control_bits, plx_control_addr);
1644 udelay(i2c_low_udelay);
1648 static void i2c_write_byte(struct comedi_device *dev, u8 byte)
1651 unsigned int num_bits = 8;
1653 for (bit = 1 << (num_bits - 1); bit; bit >>= 1) {
1654 i2c_set_scl(dev, 0);
1656 i2c_set_sda(dev, 1);
1658 i2c_set_sda(dev, 0);
1659 i2c_set_scl(dev, 1);
1663 /* we can't really read the lines, so fake it */
1664 static int i2c_read_ack(struct comedi_device *dev)
1666 i2c_set_scl(dev, 0);
1667 i2c_set_sda(dev, 1);
1668 i2c_set_scl(dev, 1);
1670 return 0; /* return fake acknowledge bit */
1673 /* send start bit */
1674 static void i2c_start(struct comedi_device *dev)
1676 i2c_set_scl(dev, 1);
1677 i2c_set_sda(dev, 1);
1678 i2c_set_sda(dev, 0);
1682 static void i2c_stop(struct comedi_device *dev)
1684 i2c_set_scl(dev, 0);
1685 i2c_set_sda(dev, 0);
1686 i2c_set_scl(dev, 1);
1687 i2c_set_sda(dev, 1);
1690 static void i2c_write(struct comedi_device *dev, unsigned int address,
1691 const u8 *data, unsigned int length)
1693 struct pcidas64_private *devpriv = dev->private;
1696 static const int read_bit = 0x1;
1699 * XXX need mutex to prevent simultaneous attempts to access
1700 * eeprom and i2c bus
1703 /* make sure we don't send anything to eeprom */
1704 devpriv->plx_control_bits &= ~PLX_CNTRL_EECS;
1709 /* send address and write bit */
1710 bitstream = (address << 1) & ~read_bit;
1711 i2c_write_byte(dev, bitstream);
1713 /* get acknowledge */
1714 if (i2c_read_ack(dev) != 0) {
1715 dev_err(dev->class_dev, "failed: no acknowledge\n");
1719 /* write data bytes */
1720 for (i = 0; i < length; i++) {
1721 i2c_write_byte(dev, data[i]);
1722 if (i2c_read_ack(dev) != 0) {
1723 dev_err(dev->class_dev, "failed: no acknowledge\n");
1731 static int cb_pcidas64_ai_eoc(struct comedi_device *dev,
1732 struct comedi_subdevice *s,
1733 struct comedi_insn *insn,
1734 unsigned long context)
1736 const struct pcidas64_board *board = dev->board_ptr;
1737 struct pcidas64_private *devpriv = dev->private;
1738 unsigned int status;
1740 status = readw(devpriv->main_iobase + HW_STATUS_REG);
1741 if (board->layout == LAYOUT_4020) {
1742 status = readw(devpriv->main_iobase + ADC_WRITE_PNTR_REG);
1746 if (pipe_full_bits(status))
1752 static int ai_rinsn(struct comedi_device *dev, struct comedi_subdevice *s,
1753 struct comedi_insn *insn, unsigned int *data)
1755 const struct pcidas64_board *board = dev->board_ptr;
1756 struct pcidas64_private *devpriv = dev->private;
1757 unsigned int bits = 0, n;
1758 unsigned int channel, range, aref;
1759 unsigned long flags;
1762 channel = CR_CHAN(insn->chanspec);
1763 range = CR_RANGE(insn->chanspec);
1764 aref = CR_AREF(insn->chanspec);
1766 /* disable card's analog input interrupt sources and pacing */
1767 /* 4020 generates dac done interrupts even though they are disabled */
1768 disable_ai_pacing(dev);
1770 spin_lock_irqsave(&dev->spinlock, flags);
1771 if (insn->chanspec & CR_ALT_FILTER)
1772 devpriv->adc_control1_bits |= ADC_DITHER_BIT;
1774 devpriv->adc_control1_bits &= ~ADC_DITHER_BIT;
1775 writew(devpriv->adc_control1_bits,
1776 devpriv->main_iobase + ADC_CONTROL1_REG);
1777 spin_unlock_irqrestore(&dev->spinlock, flags);
1779 if (board->layout != LAYOUT_4020) {
1780 /* use internal queue */
1781 devpriv->hw_config_bits &= ~EXT_QUEUE_BIT;
1782 writew(devpriv->hw_config_bits,
1783 devpriv->main_iobase + HW_CONFIG_REG);
1785 /* ALT_SOURCE is internal calibration reference */
1786 if (insn->chanspec & CR_ALT_SOURCE) {
1787 unsigned int cal_en_bit;
1789 if (board->layout == LAYOUT_60XX)
1790 cal_en_bit = CAL_EN_60XX_BIT;
1792 cal_en_bit = CAL_EN_64XX_BIT;
1794 * select internal reference source to connect
1798 adc_src_bits(devpriv->calibration_source),
1799 devpriv->main_iobase + CALIBRATION_REG);
1802 * make sure internal calibration source
1805 writew(0, devpriv->main_iobase + CALIBRATION_REG);
1807 /* load internal queue */
1810 bits |= ai_range_bits_6xxx(dev, CR_RANGE(insn->chanspec));
1811 /* set single-ended / differential */
1812 bits |= se_diff_bit_6xxx(dev, aref == AREF_DIFF);
1813 if (aref == AREF_COMMON)
1814 bits |= ADC_COMMON_BIT;
1815 bits |= adc_chan_bits(channel);
1816 /* set stop channel */
1817 writew(adc_chan_bits(channel),
1818 devpriv->main_iobase + ADC_QUEUE_HIGH_REG);
1819 /* set start channel, and rest of settings */
1820 writew(bits, devpriv->main_iobase + ADC_QUEUE_LOAD_REG);
1822 u8 old_cal_range_bits = devpriv->i2c_cal_range_bits;
1824 devpriv->i2c_cal_range_bits &= ~ADC_SRC_4020_MASK;
1825 if (insn->chanspec & CR_ALT_SOURCE) {
1826 devpriv->i2c_cal_range_bits |=
1827 adc_src_4020_bits(devpriv->calibration_source);
1828 } else { /* select BNC inputs */
1829 devpriv->i2c_cal_range_bits |= adc_src_4020_bits(4);
1833 devpriv->i2c_cal_range_bits |= attenuate_bit(channel);
1835 devpriv->i2c_cal_range_bits &= ~attenuate_bit(channel);
1837 * update calibration/range i2c register only if necessary,
1838 * as it is very slow
1840 if (old_cal_range_bits != devpriv->i2c_cal_range_bits) {
1841 u8 i2c_data = devpriv->i2c_cal_range_bits;
1843 i2c_write(dev, RANGE_CAL_I2C_ADDR, &i2c_data,
1848 * 4020 manual asks that sample interval register to be set
1849 * before writing to convert register.
1850 * Using somewhat arbitrary setting of 4 master clock ticks
1853 writew(0, devpriv->main_iobase + ADC_SAMPLE_INTERVAL_UPPER_REG);
1854 writew(2, devpriv->main_iobase + ADC_SAMPLE_INTERVAL_LOWER_REG);
1857 for (n = 0; n < insn->n; n++) {
1858 /* clear adc buffer (inside loop for 4020 sake) */
1859 writew(0, devpriv->main_iobase + ADC_BUFFER_CLEAR_REG);
1861 /* trigger conversion, bits sent only matter for 4020 */
1862 writew(adc_convert_chan_4020_bits(CR_CHAN(insn->chanspec)),
1863 devpriv->main_iobase + ADC_CONVERT_REG);
1866 ret = comedi_timeout(dev, s, insn, cb_pcidas64_ai_eoc, 0);
1870 if (board->layout == LAYOUT_4020)
1871 data[n] = readl(dev->mmio + ADC_FIFO_REG) & 0xffff;
1873 data[n] = readw(devpriv->main_iobase + PIPE1_READ_REG);
1879 static int ai_config_calibration_source(struct comedi_device *dev,
1882 const struct pcidas64_board *board = dev->board_ptr;
1883 struct pcidas64_private *devpriv = dev->private;
1884 unsigned int source = data[1];
1885 int num_calibration_sources;
1887 if (board->layout == LAYOUT_60XX)
1888 num_calibration_sources = 16;
1890 num_calibration_sources = 8;
1891 if (source >= num_calibration_sources) {
1892 dev_dbg(dev->class_dev, "invalid calibration source: %i\n",
1897 devpriv->calibration_source = source;
1902 static int ai_config_block_size(struct comedi_device *dev, unsigned int *data)
1904 const struct pcidas64_board *board = dev->board_ptr;
1906 const struct hw_fifo_info *const fifo = board->ai_fifo;
1907 unsigned int block_size, requested_block_size;
1910 requested_block_size = data[1];
1912 if (requested_block_size) {
1913 fifo_size = requested_block_size * fifo->num_segments /
1916 retval = set_ai_fifo_size(dev, fifo_size);
1921 block_size = ai_fifo_size(dev) / fifo->num_segments * bytes_in_sample;
1923 data[1] = block_size;
1928 static int ai_config_master_clock_4020(struct comedi_device *dev,
1931 struct pcidas64_private *devpriv = dev->private;
1932 unsigned int divisor = data[4];
1941 case COMEDI_EV_SCAN_BEGIN:
1942 devpriv->ext_clock.divisor = divisor;
1943 devpriv->ext_clock.chanspec = data[2];
1951 return retval ? retval : 5;
1954 /* XXX could add support for 60xx series */
1955 static int ai_config_master_clock(struct comedi_device *dev, unsigned int *data)
1957 const struct pcidas64_board *board = dev->board_ptr;
1959 switch (board->layout) {
1961 return ai_config_master_clock_4020(dev, data);
1969 static int ai_config_insn(struct comedi_device *dev, struct comedi_subdevice *s,
1970 struct comedi_insn *insn, unsigned int *data)
1975 case INSN_CONFIG_ALT_SOURCE:
1976 return ai_config_calibration_source(dev, data);
1977 case INSN_CONFIG_BLOCK_SIZE:
1978 return ai_config_block_size(dev, data);
1979 case INSN_CONFIG_TIMER_1:
1980 return ai_config_master_clock(dev, data);
1988 * Gets nearest achievable timing given master clock speed, does not
1989 * take into account possible minimum/maximum divisor values. Used
1990 * by other timing checking functions.
1992 static unsigned int get_divisor(unsigned int ns, unsigned int flags)
1994 unsigned int divisor;
1996 switch (flags & CMDF_ROUND_MASK) {
1998 divisor = DIV_ROUND_UP(ns, TIMER_BASE);
2000 case CMDF_ROUND_DOWN:
2001 divisor = ns / TIMER_BASE;
2003 case CMDF_ROUND_NEAREST:
2005 divisor = DIV_ROUND_CLOSEST(ns, TIMER_BASE);
2012 * utility function that rounds desired timing to an achievable time, and
2013 * sets cmd members appropriately.
2014 * adc paces conversions from master clock by dividing by (x + 3) where x is
2017 static void check_adc_timing(struct comedi_device *dev, struct comedi_cmd *cmd)
2019 const struct pcidas64_board *board = dev->board_ptr;
2020 unsigned long long convert_divisor = 0;
2021 unsigned int scan_divisor;
2022 static const int min_convert_divisor = 3;
2023 static const int max_convert_divisor =
2024 max_counter_value + min_convert_divisor;
2025 static const int min_scan_divisor_4020 = 2;
2026 unsigned long long max_scan_divisor, min_scan_divisor;
2028 if (cmd->convert_src == TRIG_TIMER) {
2029 if (board->layout == LAYOUT_4020) {
2030 cmd->convert_arg = 0;
2032 convert_divisor = get_divisor(cmd->convert_arg,
2034 if (convert_divisor > max_convert_divisor)
2035 convert_divisor = max_convert_divisor;
2036 if (convert_divisor < min_convert_divisor)
2037 convert_divisor = min_convert_divisor;
2038 cmd->convert_arg = convert_divisor * TIMER_BASE;
2040 } else if (cmd->convert_src == TRIG_NOW) {
2041 cmd->convert_arg = 0;
2044 if (cmd->scan_begin_src == TRIG_TIMER) {
2045 scan_divisor = get_divisor(cmd->scan_begin_arg, cmd->flags);
2046 if (cmd->convert_src == TRIG_TIMER) {
2047 min_scan_divisor = convert_divisor * cmd->chanlist_len;
2049 (convert_divisor * cmd->chanlist_len - 1) +
2052 min_scan_divisor = min_scan_divisor_4020;
2053 max_scan_divisor = max_counter_value + min_scan_divisor;
2055 if (scan_divisor > max_scan_divisor)
2056 scan_divisor = max_scan_divisor;
2057 if (scan_divisor < min_scan_divisor)
2058 scan_divisor = min_scan_divisor;
2059 cmd->scan_begin_arg = scan_divisor * TIMER_BASE;
2063 static int cb_pcidas64_ai_check_chanlist(struct comedi_device *dev,
2064 struct comedi_subdevice *s,
2065 struct comedi_cmd *cmd)
2067 const struct pcidas64_board *board = dev->board_ptr;
2068 unsigned int aref0 = CR_AREF(cmd->chanlist[0]);
2071 for (i = 1; i < cmd->chanlist_len; i++) {
2072 unsigned int aref = CR_AREF(cmd->chanlist[i]);
2074 if (aref != aref0) {
2075 dev_dbg(dev->class_dev,
2076 "all elements in chanlist must use the same analog reference\n");
2081 if (board->layout == LAYOUT_4020) {
2082 unsigned int chan0 = CR_CHAN(cmd->chanlist[0]);
2084 for (i = 1; i < cmd->chanlist_len; i++) {
2085 unsigned int chan = CR_CHAN(cmd->chanlist[i]);
2087 if (chan != (chan0 + i)) {
2088 dev_dbg(dev->class_dev,
2089 "chanlist must use consecutive channels\n");
2093 if (cmd->chanlist_len == 3) {
2094 dev_dbg(dev->class_dev,
2095 "chanlist cannot be 3 channels long, use 1, 2, or 4 channels\n");
2103 static int ai_cmdtest(struct comedi_device *dev, struct comedi_subdevice *s,
2104 struct comedi_cmd *cmd)
2106 const struct pcidas64_board *board = dev->board_ptr;
2108 unsigned int tmp_arg, tmp_arg2;
2109 unsigned int triggers;
2111 /* Step 1 : check if triggers are trivially valid */
2113 err |= comedi_check_trigger_src(&cmd->start_src, TRIG_NOW | TRIG_EXT);
2115 triggers = TRIG_TIMER;
2116 if (board->layout == LAYOUT_4020)
2117 triggers |= TRIG_OTHER;
2119 triggers |= TRIG_FOLLOW;
2120 err |= comedi_check_trigger_src(&cmd->scan_begin_src, triggers);
2122 triggers = TRIG_TIMER;
2123 if (board->layout == LAYOUT_4020)
2124 triggers |= TRIG_NOW;
2126 triggers |= TRIG_EXT;
2127 err |= comedi_check_trigger_src(&cmd->convert_src, triggers);
2128 err |= comedi_check_trigger_src(&cmd->scan_end_src, TRIG_COUNT);
2129 err |= comedi_check_trigger_src(&cmd->stop_src,
2130 TRIG_COUNT | TRIG_EXT | TRIG_NONE);
2135 /* Step 2a : make sure trigger sources are unique */
2137 err |= comedi_check_trigger_is_unique(cmd->start_src);
2138 err |= comedi_check_trigger_is_unique(cmd->scan_begin_src);
2139 err |= comedi_check_trigger_is_unique(cmd->convert_src);
2140 err |= comedi_check_trigger_is_unique(cmd->stop_src);
2142 /* Step 2b : and mutually compatible */
2144 if (cmd->convert_src == TRIG_EXT && cmd->scan_begin_src == TRIG_TIMER)
2150 /* Step 3: check if arguments are trivially valid */
2152 switch (cmd->start_src) {
2154 err |= comedi_check_trigger_arg_is(&cmd->start_arg, 0);
2158 * start_arg is the CR_CHAN | CR_INVERT of the
2164 if (cmd->convert_src == TRIG_TIMER) {
2165 if (board->layout == LAYOUT_4020) {
2166 err |= comedi_check_trigger_arg_is(&cmd->convert_arg,
2169 err |= comedi_check_trigger_arg_min(&cmd->convert_arg,
2172 * if scans are timed faster than conversion rate
2175 if (cmd->scan_begin_src == TRIG_TIMER) {
2176 err |= comedi_check_trigger_arg_min(
2177 &cmd->scan_begin_arg,
2184 err |= comedi_check_trigger_arg_min(&cmd->chanlist_len, 1);
2185 err |= comedi_check_trigger_arg_is(&cmd->scan_end_arg,
2188 switch (cmd->stop_src) {
2192 err |= comedi_check_trigger_arg_min(&cmd->stop_arg, 1);
2195 err |= comedi_check_trigger_arg_is(&cmd->stop_arg, 0);
2204 /* step 4: fix up any arguments */
2206 if (cmd->convert_src == TRIG_TIMER) {
2207 tmp_arg = cmd->convert_arg;
2208 tmp_arg2 = cmd->scan_begin_arg;
2209 check_adc_timing(dev, cmd);
2210 if (tmp_arg != cmd->convert_arg)
2212 if (tmp_arg2 != cmd->scan_begin_arg)
2219 /* Step 5: check channel list if it exists */
2220 if (cmd->chanlist && cmd->chanlist_len > 0)
2221 err |= cb_pcidas64_ai_check_chanlist(dev, s, cmd);
2229 static int use_hw_sample_counter(struct comedi_cmd *cmd)
2231 /* disable for now until I work out a race */
2234 if (cmd->stop_src == TRIG_COUNT && cmd->stop_arg <= max_counter_value)
2240 static void setup_sample_counters(struct comedi_device *dev,
2241 struct comedi_cmd *cmd)
2243 struct pcidas64_private *devpriv = dev->private;
2245 /* load hardware conversion counter */
2246 if (use_hw_sample_counter(cmd)) {
2247 writew(cmd->stop_arg & 0xffff,
2248 devpriv->main_iobase + ADC_COUNT_LOWER_REG);
2249 writew((cmd->stop_arg >> 16) & 0xff,
2250 devpriv->main_iobase + ADC_COUNT_UPPER_REG);
2252 writew(1, devpriv->main_iobase + ADC_COUNT_LOWER_REG);
2256 static inline unsigned int dma_transfer_size(struct comedi_device *dev)
2258 const struct pcidas64_board *board = dev->board_ptr;
2259 struct pcidas64_private *devpriv = dev->private;
2260 unsigned int num_samples;
2262 num_samples = devpriv->ai_fifo_segment_length *
2263 board->ai_fifo->sample_packing_ratio;
2264 if (num_samples > DMA_BUFFER_SIZE / sizeof(u16))
2265 num_samples = DMA_BUFFER_SIZE / sizeof(u16);
2270 static u32 ai_convert_counter_6xxx(const struct comedi_device *dev,
2271 const struct comedi_cmd *cmd)
2273 /* supposed to load counter with desired divisor minus 3 */
2274 return cmd->convert_arg / TIMER_BASE - 3;
2277 static u32 ai_scan_counter_6xxx(struct comedi_device *dev,
2278 struct comedi_cmd *cmd)
2282 /* figure out how long we need to delay at end of scan */
2283 switch (cmd->scan_begin_src) {
2285 count = (cmd->scan_begin_arg -
2286 (cmd->convert_arg * (cmd->chanlist_len - 1))) /
2290 count = cmd->convert_arg / TIMER_BASE;
2298 static u32 ai_convert_counter_4020(struct comedi_device *dev,
2299 struct comedi_cmd *cmd)
2301 struct pcidas64_private *devpriv = dev->private;
2302 unsigned int divisor;
2304 switch (cmd->scan_begin_src) {
2306 divisor = cmd->scan_begin_arg / TIMER_BASE;
2309 divisor = devpriv->ext_clock.divisor;
2311 default: /* should never happen */
2312 dev_err(dev->class_dev, "bug! failed to set ai pacing!\n");
2317 /* supposed to load counter with desired divisor minus 2 for 4020 */
2321 static void select_master_clock_4020(struct comedi_device *dev,
2322 const struct comedi_cmd *cmd)
2324 struct pcidas64_private *devpriv = dev->private;
2326 /* select internal/external master clock */
2327 devpriv->hw_config_bits &= ~MASTER_CLOCK_4020_MASK;
2328 if (cmd->scan_begin_src == TRIG_OTHER) {
2329 int chanspec = devpriv->ext_clock.chanspec;
2331 if (CR_CHAN(chanspec))
2332 devpriv->hw_config_bits |= BNC_CLOCK_4020_BITS;
2334 devpriv->hw_config_bits |= EXT_CLOCK_4020_BITS;
2336 devpriv->hw_config_bits |= INTERNAL_CLOCK_4020_BITS;
2338 writew(devpriv->hw_config_bits,
2339 devpriv->main_iobase + HW_CONFIG_REG);
2342 static void select_master_clock(struct comedi_device *dev,
2343 const struct comedi_cmd *cmd)
2345 const struct pcidas64_board *board = dev->board_ptr;
2347 switch (board->layout) {
2349 select_master_clock_4020(dev, cmd);
2356 static inline void dma_start_sync(struct comedi_device *dev,
2357 unsigned int channel)
2359 struct pcidas64_private *devpriv = dev->private;
2360 unsigned long flags;
2362 /* spinlock for plx dma control/status reg */
2363 spin_lock_irqsave(&dev->spinlock, flags);
2364 writeb(PLX_DMACSR_ENABLE | PLX_DMACSR_START | PLX_DMACSR_CLEARINTR,
2365 devpriv->plx9080_iobase + PLX_REG_DMACSR(channel));
2366 spin_unlock_irqrestore(&dev->spinlock, flags);
2369 static void set_ai_pacing(struct comedi_device *dev, struct comedi_cmd *cmd)
2371 const struct pcidas64_board *board = dev->board_ptr;
2372 struct pcidas64_private *devpriv = dev->private;
2373 u32 convert_counter = 0, scan_counter = 0;
2375 check_adc_timing(dev, cmd);
2377 select_master_clock(dev, cmd);
2379 if (board->layout == LAYOUT_4020) {
2380 convert_counter = ai_convert_counter_4020(dev, cmd);
2382 convert_counter = ai_convert_counter_6xxx(dev, cmd);
2383 scan_counter = ai_scan_counter_6xxx(dev, cmd);
2386 /* load lower 16 bits of convert interval */
2387 writew(convert_counter & 0xffff,
2388 devpriv->main_iobase + ADC_SAMPLE_INTERVAL_LOWER_REG);
2389 /* load upper 8 bits of convert interval */
2390 writew((convert_counter >> 16) & 0xff,
2391 devpriv->main_iobase + ADC_SAMPLE_INTERVAL_UPPER_REG);
2392 /* load lower 16 bits of scan delay */
2393 writew(scan_counter & 0xffff,
2394 devpriv->main_iobase + ADC_DELAY_INTERVAL_LOWER_REG);
2395 /* load upper 8 bits of scan delay */
2396 writew((scan_counter >> 16) & 0xff,
2397 devpriv->main_iobase + ADC_DELAY_INTERVAL_UPPER_REG);
2400 static int use_internal_queue_6xxx(const struct comedi_cmd *cmd)
2404 for (i = 0; i + 1 < cmd->chanlist_len; i++) {
2405 if (CR_CHAN(cmd->chanlist[i + 1]) !=
2406 CR_CHAN(cmd->chanlist[i]) + 1)
2408 if (CR_RANGE(cmd->chanlist[i + 1]) !=
2409 CR_RANGE(cmd->chanlist[i]))
2411 if (CR_AREF(cmd->chanlist[i + 1]) != CR_AREF(cmd->chanlist[i]))
2417 static int setup_channel_queue(struct comedi_device *dev,
2418 const struct comedi_cmd *cmd)
2420 const struct pcidas64_board *board = dev->board_ptr;
2421 struct pcidas64_private *devpriv = dev->private;
2422 unsigned short bits;
2425 if (board->layout != LAYOUT_4020) {
2426 if (use_internal_queue_6xxx(cmd)) {
2427 devpriv->hw_config_bits &= ~EXT_QUEUE_BIT;
2428 writew(devpriv->hw_config_bits,
2429 devpriv->main_iobase + HW_CONFIG_REG);
2432 bits |= adc_chan_bits(CR_CHAN(cmd->chanlist[0]));
2434 bits |= ai_range_bits_6xxx(dev,
2435 CR_RANGE(cmd->chanlist[0]));
2436 /* set single-ended / differential */
2437 bits |= se_diff_bit_6xxx(dev,
2438 CR_AREF(cmd->chanlist[0]) ==
2440 if (CR_AREF(cmd->chanlist[0]) == AREF_COMMON)
2441 bits |= ADC_COMMON_BIT;
2442 /* set stop channel */
2443 writew(adc_chan_bits
2444 (CR_CHAN(cmd->chanlist[cmd->chanlist_len - 1])),
2445 devpriv->main_iobase + ADC_QUEUE_HIGH_REG);
2446 /* set start channel, and rest of settings */
2448 devpriv->main_iobase + ADC_QUEUE_LOAD_REG);
2450 /* use external queue */
2451 if (dev->write_subdev && dev->write_subdev->busy) {
2452 warn_external_queue(dev);
2455 devpriv->hw_config_bits |= EXT_QUEUE_BIT;
2456 writew(devpriv->hw_config_bits,
2457 devpriv->main_iobase + HW_CONFIG_REG);
2458 /* clear DAC buffer to prevent weird interactions */
2460 devpriv->main_iobase + DAC_BUFFER_CLEAR_REG);
2461 /* clear queue pointer */
2462 writew(0, devpriv->main_iobase + ADC_QUEUE_CLEAR_REG);
2463 /* load external queue */
2464 for (i = 0; i < cmd->chanlist_len; i++) {
2465 unsigned int chanspec = cmd->chanlist[i];
2466 int use_differential;
2470 bits |= adc_chan_bits(CR_CHAN(chanspec));
2472 bits |= ai_range_bits_6xxx(dev,
2473 CR_RANGE(chanspec));
2474 /* set single-ended / differential */
2475 use_differential = 0;
2476 if (CR_AREF(chanspec) == AREF_DIFF)
2477 use_differential = 1;
2478 bits |= se_diff_bit_6xxx(dev, use_differential);
2480 if (CR_AREF(cmd->chanlist[i]) == AREF_COMMON)
2481 bits |= ADC_COMMON_BIT;
2482 /* mark end of queue */
2483 if (i == cmd->chanlist_len - 1)
2484 bits |= QUEUE_EOSCAN_BIT |
2487 devpriv->main_iobase +
2488 ADC_QUEUE_FIFO_REG);
2491 * doing a queue clear is not specified in board docs,
2492 * but required for reliable operation
2494 writew(0, devpriv->main_iobase + ADC_QUEUE_CLEAR_REG);
2495 /* prime queue holding register */
2496 writew(0, devpriv->main_iobase + ADC_QUEUE_LOAD_REG);
2499 unsigned short old_cal_range_bits = devpriv->i2c_cal_range_bits;
2501 devpriv->i2c_cal_range_bits &= ~ADC_SRC_4020_MASK;
2502 /* select BNC inputs */
2503 devpriv->i2c_cal_range_bits |= adc_src_4020_bits(4);
2505 for (i = 0; i < cmd->chanlist_len; i++) {
2506 unsigned int channel = CR_CHAN(cmd->chanlist[i]);
2507 unsigned int range = CR_RANGE(cmd->chanlist[i]);
2510 devpriv->i2c_cal_range_bits |=
2511 attenuate_bit(channel);
2513 devpriv->i2c_cal_range_bits &=
2514 ~attenuate_bit(channel);
2517 * update calibration/range i2c register only if necessary,
2518 * as it is very slow
2520 if (old_cal_range_bits != devpriv->i2c_cal_range_bits) {
2521 u8 i2c_data = devpriv->i2c_cal_range_bits;
2523 i2c_write(dev, RANGE_CAL_I2C_ADDR, &i2c_data,
2530 static inline void load_first_dma_descriptor(struct comedi_device *dev,
2531 unsigned int dma_channel,
2532 unsigned int descriptor_bits)
2534 struct pcidas64_private *devpriv = dev->private;
2537 * The transfer size, pci address, and local address registers
2538 * are supposedly unused during chained dma,
2539 * but I have found that left over values from last operation
2540 * occasionally cause problems with transfer of first dma
2541 * block. Initializing them to zero seems to fix the problem.
2544 writel(0, devpriv->plx9080_iobase + PLX_REG_DMASIZ1);
2545 writel(0, devpriv->plx9080_iobase + PLX_REG_DMAPADR1);
2546 writel(0, devpriv->plx9080_iobase + PLX_REG_DMALADR1);
2547 writel(descriptor_bits,
2548 devpriv->plx9080_iobase + PLX_REG_DMADPR1);
2550 writel(0, devpriv->plx9080_iobase + PLX_REG_DMASIZ0);
2551 writel(0, devpriv->plx9080_iobase + PLX_REG_DMAPADR0);
2552 writel(0, devpriv->plx9080_iobase + PLX_REG_DMALADR0);
2553 writel(descriptor_bits,
2554 devpriv->plx9080_iobase + PLX_REG_DMADPR0);
2558 static int ai_cmd(struct comedi_device *dev, struct comedi_subdevice *s)
2560 const struct pcidas64_board *board = dev->board_ptr;
2561 struct pcidas64_private *devpriv = dev->private;
2562 struct comedi_async *async = s->async;
2563 struct comedi_cmd *cmd = &async->cmd;
2566 unsigned long flags;
2569 disable_ai_pacing(dev);
2572 retval = setup_channel_queue(dev, cmd);
2576 /* make sure internal calibration source is turned off */
2577 writew(0, devpriv->main_iobase + CALIBRATION_REG);
2579 set_ai_pacing(dev, cmd);
2581 setup_sample_counters(dev, cmd);
2583 enable_ai_interrupts(dev, cmd);
2585 spin_lock_irqsave(&dev->spinlock, flags);
2586 /* set mode, allow conversions through software gate */
2587 devpriv->adc_control1_bits |= ADC_SW_GATE_BIT;
2588 devpriv->adc_control1_bits &= ~ADC_DITHER_BIT;
2589 if (board->layout != LAYOUT_4020) {
2590 devpriv->adc_control1_bits &= ~ADC_MODE_MASK;
2591 if (cmd->convert_src == TRIG_EXT)
2592 /* good old mode 13 */
2593 devpriv->adc_control1_bits |= adc_mode_bits(13);
2595 /* mode 8. What else could you need? */
2596 devpriv->adc_control1_bits |= adc_mode_bits(8);
2598 devpriv->adc_control1_bits &= ~CHANNEL_MODE_4020_MASK;
2599 if (cmd->chanlist_len == 4)
2600 devpriv->adc_control1_bits |= FOUR_CHANNEL_4020_BITS;
2601 else if (cmd->chanlist_len == 2)
2602 devpriv->adc_control1_bits |= TWO_CHANNEL_4020_BITS;
2603 devpriv->adc_control1_bits &= ~ADC_LO_CHANNEL_4020_MASK;
2604 devpriv->adc_control1_bits |=
2605 adc_lo_chan_4020_bits(CR_CHAN(cmd->chanlist[0]));
2606 devpriv->adc_control1_bits &= ~ADC_HI_CHANNEL_4020_MASK;
2607 devpriv->adc_control1_bits |=
2608 adc_hi_chan_4020_bits(CR_CHAN(cmd->chanlist
2609 [cmd->chanlist_len - 1]));
2611 writew(devpriv->adc_control1_bits,
2612 devpriv->main_iobase + ADC_CONTROL1_REG);
2613 spin_unlock_irqrestore(&dev->spinlock, flags);
2615 /* clear adc buffer */
2616 writew(0, devpriv->main_iobase + ADC_BUFFER_CLEAR_REG);
2618 if ((cmd->flags & CMDF_WAKE_EOS) == 0 ||
2619 board->layout == LAYOUT_4020) {
2620 devpriv->ai_dma_index = 0;
2622 /* set dma transfer size */
2623 for (i = 0; i < ai_dma_ring_count(board); i++)
2624 devpriv->ai_dma_desc[i].transfer_size =
2625 cpu_to_le32(dma_transfer_size(dev) *
2628 /* give location of first dma descriptor */
2629 load_first_dma_descriptor(dev, 1,
2630 devpriv->ai_dma_desc_bus_addr |
2631 PLX_DMADPR_DESCPCI |
2633 PLX_DMADPR_XFERL2P);
2635 dma_start_sync(dev, 1);
2638 if (board->layout == LAYOUT_4020) {
2639 /* set source for external triggers */
2641 if (cmd->start_src == TRIG_EXT && CR_CHAN(cmd->start_arg))
2642 bits |= EXT_START_TRIG_BNC_BIT;
2643 if (cmd->stop_src == TRIG_EXT && CR_CHAN(cmd->stop_arg))
2644 bits |= EXT_STOP_TRIG_BNC_BIT;
2645 writew(bits, devpriv->main_iobase + DAQ_ATRIG_LOW_4020_REG);
2648 spin_lock_irqsave(&dev->spinlock, flags);
2650 /* enable pacing, triggering, etc */
2651 bits = ADC_ENABLE_BIT | ADC_SOFT_GATE_BITS | ADC_GATE_LEVEL_BIT;
2652 if (cmd->flags & CMDF_WAKE_EOS)
2653 bits |= ADC_DMA_DISABLE_BIT;
2654 /* set start trigger */
2655 if (cmd->start_src == TRIG_EXT) {
2656 bits |= ADC_START_TRIG_EXT_BITS;
2657 if (cmd->start_arg & CR_INVERT)
2658 bits |= ADC_START_TRIG_FALLING_BIT;
2659 } else if (cmd->start_src == TRIG_NOW) {
2660 bits |= ADC_START_TRIG_SOFT_BITS;
2662 if (use_hw_sample_counter(cmd))
2663 bits |= ADC_SAMPLE_COUNTER_EN_BIT;
2664 writew(bits, devpriv->main_iobase + ADC_CONTROL0_REG);
2666 devpriv->ai_cmd_running = 1;
2668 spin_unlock_irqrestore(&dev->spinlock, flags);
2670 /* start acquisition */
2671 if (cmd->start_src == TRIG_NOW)
2672 writew(0, devpriv->main_iobase + ADC_START_REG);
2677 /* read num_samples from 16 bit wide ai fifo */
2678 static void pio_drain_ai_fifo_16(struct comedi_device *dev)
2680 struct pcidas64_private *devpriv = dev->private;
2681 struct comedi_subdevice *s = dev->read_subdev;
2684 int read_segment, read_index, write_segment, write_index;
2688 /* get least significant 15 bits */
2689 read_index = readw(devpriv->main_iobase + ADC_READ_PNTR_REG) &
2691 write_index = readw(devpriv->main_iobase + ADC_WRITE_PNTR_REG) &
2694 * Get most significant bits (grey code).
2695 * Different boards use different code so use a scheme
2696 * that doesn't depend on encoding. This read must
2697 * occur after reading least significant 15 bits to avoid race
2698 * with fifo switching to next segment.
2700 prepost_bits = readw(devpriv->main_iobase + PREPOST_REG);
2703 * if read and write pointers are not on the same fifo segment,
2704 * read to the end of the read segment
2706 read_segment = adc_upper_read_ptr_code(prepost_bits);
2707 write_segment = adc_upper_write_ptr_code(prepost_bits);
2709 if (read_segment != write_segment)
2711 devpriv->ai_fifo_segment_length - read_index;
2713 num_samples = write_index - read_index;
2714 if (num_samples < 0) {
2715 dev_err(dev->class_dev,
2716 "cb_pcidas64: bug! num_samples < 0\n");
2720 num_samples = comedi_nsamples_left(s, num_samples);
2721 if (num_samples == 0)
2724 for (i = 0; i < num_samples; i++) {
2727 val = readw(devpriv->main_iobase + ADC_FIFO_REG);
2728 comedi_buf_write_samples(s, &val, 1);
2731 } while (read_segment != write_segment);
2735 * Read from 32 bit wide ai fifo of 4020 - deal with insane grey coding of
2736 * pointers. The pci-4020 hardware only supports dma transfers (it only
2737 * supports the use of pio for draining the last remaining points from the
2738 * fifo when a data acquisition operation has completed).
2740 static void pio_drain_ai_fifo_32(struct comedi_device *dev)
2742 struct pcidas64_private *devpriv = dev->private;
2743 struct comedi_subdevice *s = dev->read_subdev;
2744 unsigned int nsamples;
2748 readw(devpriv->main_iobase + ADC_WRITE_PNTR_REG) & 0x7fff;
2750 readw(devpriv->main_iobase + ADC_READ_PNTR_REG) & 0x7fff;
2752 nsamples = comedi_nsamples_left(s, 100000);
2753 for (i = 0; read_code != write_code && i < nsamples;) {
2756 fifo_data = readl(dev->mmio + ADC_FIFO_REG);
2757 val = fifo_data & 0xffff;
2758 comedi_buf_write_samples(s, &val, 1);
2761 val = (fifo_data >> 16) & 0xffff;
2762 comedi_buf_write_samples(s, &val, 1);
2765 read_code = readw(devpriv->main_iobase + ADC_READ_PNTR_REG) &
2771 static void pio_drain_ai_fifo(struct comedi_device *dev)
2773 const struct pcidas64_board *board = dev->board_ptr;
2775 if (board->layout == LAYOUT_4020)
2776 pio_drain_ai_fifo_32(dev);
2778 pio_drain_ai_fifo_16(dev);
2781 static void drain_dma_buffers(struct comedi_device *dev, unsigned int channel)
2783 const struct pcidas64_board *board = dev->board_ptr;
2784 struct pcidas64_private *devpriv = dev->private;
2785 struct comedi_subdevice *s = dev->read_subdev;
2786 u32 next_transfer_addr;
2788 int num_samples = 0;
2789 void __iomem *pci_addr_reg;
2791 pci_addr_reg = devpriv->plx9080_iobase + PLX_REG_DMAPADR(channel);
2793 /* loop until we have read all the full buffers */
2794 for (j = 0, next_transfer_addr = readl(pci_addr_reg);
2795 (next_transfer_addr <
2796 devpriv->ai_buffer_bus_addr[devpriv->ai_dma_index] ||
2797 next_transfer_addr >=
2798 devpriv->ai_buffer_bus_addr[devpriv->ai_dma_index] +
2799 DMA_BUFFER_SIZE) && j < ai_dma_ring_count(board); j++) {
2800 /* transfer data from dma buffer to comedi buffer */
2801 num_samples = comedi_nsamples_left(s, dma_transfer_size(dev));
2802 comedi_buf_write_samples(s,
2803 devpriv->ai_buffer[devpriv->ai_dma_index],
2805 devpriv->ai_dma_index = (devpriv->ai_dma_index + 1) %
2806 ai_dma_ring_count(board);
2809 * XXX check for dma ring buffer overrun
2810 * (use end-of-chain bit to mark last unused buffer)
2814 static void handle_ai_interrupt(struct comedi_device *dev,
2815 unsigned short status,
2816 unsigned int plx_status)
2818 const struct pcidas64_board *board = dev->board_ptr;
2819 struct pcidas64_private *devpriv = dev->private;
2820 struct comedi_subdevice *s = dev->read_subdev;
2821 struct comedi_async *async = s->async;
2822 struct comedi_cmd *cmd = &async->cmd;
2824 unsigned long flags;
2826 /* check for fifo overrun */
2827 if (status & ADC_OVERRUN_BIT) {
2828 dev_err(dev->class_dev, "fifo overrun\n");
2829 async->events |= COMEDI_CB_ERROR;
2831 /* spin lock makes sure no one else changes plx dma control reg */
2832 spin_lock_irqsave(&dev->spinlock, flags);
2833 dma1_status = readb(devpriv->plx9080_iobase + PLX_REG_DMACSR1);
2834 if (plx_status & PLX_INTCSR_DMA1IA) { /* dma chan 1 interrupt */
2835 writeb((dma1_status & PLX_DMACSR_ENABLE) | PLX_DMACSR_CLEARINTR,
2836 devpriv->plx9080_iobase + PLX_REG_DMACSR1);
2838 if (dma1_status & PLX_DMACSR_ENABLE)
2839 drain_dma_buffers(dev, 1);
2841 spin_unlock_irqrestore(&dev->spinlock, flags);
2843 /* drain fifo with pio */
2844 if ((status & ADC_DONE_BIT) ||
2845 ((cmd->flags & CMDF_WAKE_EOS) &&
2846 (status & ADC_INTR_PENDING_BIT) &&
2847 (board->layout != LAYOUT_4020))) {
2848 spin_lock_irqsave(&dev->spinlock, flags);
2849 if (devpriv->ai_cmd_running) {
2850 spin_unlock_irqrestore(&dev->spinlock, flags);
2851 pio_drain_ai_fifo(dev);
2853 spin_unlock_irqrestore(&dev->spinlock, flags);
2856 /* if we are have all the data, then quit */
2857 if ((cmd->stop_src == TRIG_COUNT &&
2858 async->scans_done >= cmd->stop_arg) ||
2859 (cmd->stop_src == TRIG_EXT && (status & ADC_STOP_BIT)))
2860 async->events |= COMEDI_CB_EOA;
2862 comedi_handle_events(dev, s);
2865 static inline unsigned int prev_ao_dma_index(struct comedi_device *dev)
2867 struct pcidas64_private *devpriv = dev->private;
2868 unsigned int buffer_index;
2870 if (devpriv->ao_dma_index == 0)
2871 buffer_index = AO_DMA_RING_COUNT - 1;
2873 buffer_index = devpriv->ao_dma_index - 1;
2874 return buffer_index;
2877 static int last_ao_dma_load_completed(struct comedi_device *dev)
2879 struct pcidas64_private *devpriv = dev->private;
2880 unsigned int buffer_index;
2881 unsigned int transfer_address;
2882 unsigned short dma_status;
2884 buffer_index = prev_ao_dma_index(dev);
2885 dma_status = readb(devpriv->plx9080_iobase + PLX_REG_DMACSR0);
2886 if ((dma_status & PLX_DMACSR_DONE) == 0)
2890 readl(devpriv->plx9080_iobase + PLX_REG_DMAPADR0);
2891 if (transfer_address != devpriv->ao_buffer_bus_addr[buffer_index])
2897 static inline int ao_dma_needs_restart(struct comedi_device *dev,
2898 unsigned short dma_status)
2900 if ((dma_status & PLX_DMACSR_DONE) == 0 ||
2901 (dma_status & PLX_DMACSR_ENABLE) == 0)
2903 if (last_ao_dma_load_completed(dev))
2909 static void restart_ao_dma(struct comedi_device *dev)
2911 struct pcidas64_private *devpriv = dev->private;
2912 unsigned int dma_desc_bits;
2914 dma_desc_bits = readl(devpriv->plx9080_iobase + PLX_REG_DMADPR0);
2915 dma_desc_bits &= ~PLX_DMADPR_CHAINEND;
2916 load_first_dma_descriptor(dev, 0, dma_desc_bits);
2918 dma_start_sync(dev, 0);
2921 static unsigned int cb_pcidas64_ao_fill_buffer(struct comedi_device *dev,
2922 struct comedi_subdevice *s,
2923 unsigned short *dest,
2924 unsigned int max_bytes)
2926 unsigned int nsamples = comedi_bytes_to_samples(s, max_bytes);
2927 unsigned int actual_bytes;
2929 nsamples = comedi_nsamples_left(s, nsamples);
2930 actual_bytes = comedi_buf_read_samples(s, dest, nsamples);
2932 return comedi_bytes_to_samples(s, actual_bytes);
2935 static unsigned int load_ao_dma_buffer(struct comedi_device *dev,
2936 const struct comedi_cmd *cmd)
2938 struct pcidas64_private *devpriv = dev->private;
2939 struct comedi_subdevice *s = dev->write_subdev;
2940 unsigned int buffer_index = devpriv->ao_dma_index;
2941 unsigned int prev_buffer_index = prev_ao_dma_index(dev);
2942 unsigned int nsamples;
2943 unsigned int nbytes;
2944 unsigned int next_bits;
2946 nsamples = cb_pcidas64_ao_fill_buffer(dev, s,
2947 devpriv->ao_buffer[buffer_index],
2952 nbytes = comedi_samples_to_bytes(s, nsamples);
2953 devpriv->ao_dma_desc[buffer_index].transfer_size = cpu_to_le32(nbytes);
2954 /* set end of chain bit so we catch underruns */
2955 next_bits = le32_to_cpu(devpriv->ao_dma_desc[buffer_index].next);
2956 next_bits |= PLX_DMADPR_CHAINEND;
2957 devpriv->ao_dma_desc[buffer_index].next = cpu_to_le32(next_bits);
2959 * clear end of chain bit on previous buffer now that we have set it
2960 * for the last buffer
2962 next_bits = le32_to_cpu(devpriv->ao_dma_desc[prev_buffer_index].next);
2963 next_bits &= ~PLX_DMADPR_CHAINEND;
2964 devpriv->ao_dma_desc[prev_buffer_index].next = cpu_to_le32(next_bits);
2966 devpriv->ao_dma_index = (buffer_index + 1) % AO_DMA_RING_COUNT;
2971 static void load_ao_dma(struct comedi_device *dev, const struct comedi_cmd *cmd)
2973 struct pcidas64_private *devpriv = dev->private;
2974 unsigned int num_bytes;
2975 unsigned int next_transfer_addr;
2976 void __iomem *pci_addr_reg = devpriv->plx9080_iobase + PLX_REG_DMAPADR0;
2977 unsigned int buffer_index;
2980 buffer_index = devpriv->ao_dma_index;
2981 /* don't overwrite data that hasn't been transferred yet */
2982 next_transfer_addr = readl(pci_addr_reg);
2983 if (next_transfer_addr >=
2984 devpriv->ao_buffer_bus_addr[buffer_index] &&
2985 next_transfer_addr <
2986 devpriv->ao_buffer_bus_addr[buffer_index] +
2989 num_bytes = load_ao_dma_buffer(dev, cmd);
2990 } while (num_bytes >= DMA_BUFFER_SIZE);
2993 static void handle_ao_interrupt(struct comedi_device *dev,
2994 unsigned short status, unsigned int plx_status)
2996 struct pcidas64_private *devpriv = dev->private;
2997 struct comedi_subdevice *s = dev->write_subdev;
2998 struct comedi_async *async;
2999 struct comedi_cmd *cmd;
3001 unsigned long flags;
3003 /* board might not support ao, in which case write_subdev is NULL */
3009 /* spin lock makes sure no one else changes plx dma control reg */
3010 spin_lock_irqsave(&dev->spinlock, flags);
3011 dma0_status = readb(devpriv->plx9080_iobase + PLX_REG_DMACSR0);
3012 if (plx_status & PLX_INTCSR_DMA0IA) { /* dma chan 0 interrupt */
3013 if ((dma0_status & PLX_DMACSR_ENABLE) &&
3014 !(dma0_status & PLX_DMACSR_DONE)) {
3015 writeb(PLX_DMACSR_ENABLE | PLX_DMACSR_CLEARINTR,
3016 devpriv->plx9080_iobase + PLX_REG_DMACSR0);
3018 writeb(PLX_DMACSR_CLEARINTR,
3019 devpriv->plx9080_iobase + PLX_REG_DMACSR0);
3021 spin_unlock_irqrestore(&dev->spinlock, flags);
3022 if (dma0_status & PLX_DMACSR_ENABLE) {
3023 load_ao_dma(dev, cmd);
3024 /* try to recover from dma end-of-chain event */
3025 if (ao_dma_needs_restart(dev, dma0_status))
3026 restart_ao_dma(dev);
3029 spin_unlock_irqrestore(&dev->spinlock, flags);
3032 if ((status & DAC_DONE_BIT)) {
3033 if ((cmd->stop_src == TRIG_COUNT &&
3034 async->scans_done >= cmd->stop_arg) ||
3035 last_ao_dma_load_completed(dev))
3036 async->events |= COMEDI_CB_EOA;
3038 async->events |= COMEDI_CB_ERROR;
3040 comedi_handle_events(dev, s);
3043 static irqreturn_t handle_interrupt(int irq, void *d)
3045 struct comedi_device *dev = d;
3046 struct pcidas64_private *devpriv = dev->private;
3047 unsigned short status;
3051 plx_status = readl(devpriv->plx9080_iobase + PLX_REG_INTCSR);
3052 status = readw(devpriv->main_iobase + HW_STATUS_REG);
3055 * an interrupt before all the postconfig stuff gets done could
3056 * cause a NULL dereference if we continue through the
3062 handle_ai_interrupt(dev, status, plx_status);
3063 handle_ao_interrupt(dev, status, plx_status);
3065 /* clear possible plx9080 interrupt sources */
3066 if (plx_status & PLX_INTCSR_LDBIA) {
3067 /* clear local doorbell interrupt */
3068 plx_bits = readl(devpriv->plx9080_iobase + PLX_REG_L2PDBELL);
3069 writel(plx_bits, devpriv->plx9080_iobase + PLX_REG_L2PDBELL);
3075 static int ai_cancel(struct comedi_device *dev, struct comedi_subdevice *s)
3077 struct pcidas64_private *devpriv = dev->private;
3078 unsigned long flags;
3080 spin_lock_irqsave(&dev->spinlock, flags);
3081 if (devpriv->ai_cmd_running == 0) {
3082 spin_unlock_irqrestore(&dev->spinlock, flags);
3085 devpriv->ai_cmd_running = 0;
3086 spin_unlock_irqrestore(&dev->spinlock, flags);
3088 disable_ai_pacing(dev);
3095 static int ao_winsn(struct comedi_device *dev, struct comedi_subdevice *s,
3096 struct comedi_insn *insn, unsigned int *data)
3098 const struct pcidas64_board *board = dev->board_ptr;
3099 struct pcidas64_private *devpriv = dev->private;
3100 int chan = CR_CHAN(insn->chanspec);
3101 int range = CR_RANGE(insn->chanspec);
3103 /* do some initializing */
3104 writew(0, devpriv->main_iobase + DAC_CONTROL0_REG);
3107 set_dac_range_bits(dev, &devpriv->dac_control1_bits, chan, range);
3108 writew(devpriv->dac_control1_bits,
3109 devpriv->main_iobase + DAC_CONTROL1_REG);
3111 /* write to channel */
3112 if (board->layout == LAYOUT_4020) {
3113 writew(data[0] & 0xff,
3114 devpriv->main_iobase + dac_lsb_4020_reg(chan));
3115 writew((data[0] >> 8) & 0xf,
3116 devpriv->main_iobase + dac_msb_4020_reg(chan));
3118 writew(data[0], devpriv->main_iobase + dac_convert_reg(chan));
3121 /* remember output value */
3122 s->readback[chan] = data[0];
3127 static void set_dac_control0_reg(struct comedi_device *dev,
3128 const struct comedi_cmd *cmd)
3130 struct pcidas64_private *devpriv = dev->private;
3131 unsigned int bits = DAC_ENABLE_BIT | WAVEFORM_GATE_LEVEL_BIT |
3132 WAVEFORM_GATE_ENABLE_BIT | WAVEFORM_GATE_SELECT_BIT;
3134 if (cmd->start_src == TRIG_EXT) {
3135 bits |= WAVEFORM_TRIG_EXT_BITS;
3136 if (cmd->start_arg & CR_INVERT)
3137 bits |= WAVEFORM_TRIG_FALLING_BIT;
3139 bits |= WAVEFORM_TRIG_SOFT_BITS;
3141 if (cmd->scan_begin_src == TRIG_EXT) {
3142 bits |= DAC_EXT_UPDATE_ENABLE_BIT;
3143 if (cmd->scan_begin_arg & CR_INVERT)
3144 bits |= DAC_EXT_UPDATE_FALLING_BIT;
3146 writew(bits, devpriv->main_iobase + DAC_CONTROL0_REG);
3149 static void set_dac_control1_reg(struct comedi_device *dev,
3150 const struct comedi_cmd *cmd)
3152 struct pcidas64_private *devpriv = dev->private;
3155 for (i = 0; i < cmd->chanlist_len; i++) {
3158 channel = CR_CHAN(cmd->chanlist[i]);
3159 range = CR_RANGE(cmd->chanlist[i]);
3160 set_dac_range_bits(dev, &devpriv->dac_control1_bits, channel,
3163 devpriv->dac_control1_bits |= DAC_SW_GATE_BIT;
3164 writew(devpriv->dac_control1_bits,
3165 devpriv->main_iobase + DAC_CONTROL1_REG);
3168 static void set_dac_select_reg(struct comedi_device *dev,
3169 const struct comedi_cmd *cmd)
3171 struct pcidas64_private *devpriv = dev->private;
3173 unsigned int first_channel, last_channel;
3175 first_channel = CR_CHAN(cmd->chanlist[0]);
3176 last_channel = CR_CHAN(cmd->chanlist[cmd->chanlist_len - 1]);
3177 if (last_channel < first_channel)
3178 dev_err(dev->class_dev,
3179 "bug! last ao channel < first ao channel\n");
3181 bits = (first_channel & 0x7) | (last_channel & 0x7) << 3;
3183 writew(bits, devpriv->main_iobase + DAC_SELECT_REG);
3186 static unsigned int get_ao_divisor(unsigned int ns, unsigned int flags)
3188 return get_divisor(ns, flags) - 2;
3191 static void set_dac_interval_regs(struct comedi_device *dev,
3192 const struct comedi_cmd *cmd)
3194 struct pcidas64_private *devpriv = dev->private;
3195 unsigned int divisor;
3197 if (cmd->scan_begin_src != TRIG_TIMER)
3200 divisor = get_ao_divisor(cmd->scan_begin_arg, cmd->flags);
3201 if (divisor > max_counter_value) {
3202 dev_err(dev->class_dev, "bug! ao divisor too big\n");
3203 divisor = max_counter_value;
3205 writew(divisor & 0xffff,
3206 devpriv->main_iobase + DAC_SAMPLE_INTERVAL_LOWER_REG);
3207 writew((divisor >> 16) & 0xff,
3208 devpriv->main_iobase + DAC_SAMPLE_INTERVAL_UPPER_REG);
3211 static int prep_ao_dma(struct comedi_device *dev, const struct comedi_cmd *cmd)
3213 struct pcidas64_private *devpriv = dev->private;
3214 struct comedi_subdevice *s = dev->write_subdev;
3215 unsigned int nsamples;
3216 unsigned int nbytes;
3220 * clear queue pointer too, since external queue has
3221 * weird interactions with ao fifo
3223 writew(0, devpriv->main_iobase + ADC_QUEUE_CLEAR_REG);
3224 writew(0, devpriv->main_iobase + DAC_BUFFER_CLEAR_REG);
3226 nsamples = cb_pcidas64_ao_fill_buffer(dev, s,
3227 devpriv->ao_bounce_buffer,
3232 for (i = 0; i < nsamples; i++) {
3233 writew(devpriv->ao_bounce_buffer[i],
3234 devpriv->main_iobase + DAC_FIFO_REG);
3237 if (cmd->stop_src == TRIG_COUNT &&
3238 s->async->scans_done >= cmd->stop_arg)
3241 nbytes = load_ao_dma_buffer(dev, cmd);
3244 load_ao_dma(dev, cmd);
3246 dma_start_sync(dev, 0);
3251 static inline int external_ai_queue_in_use(struct comedi_device *dev)
3253 const struct pcidas64_board *board = dev->board_ptr;
3255 if (!dev->read_subdev->busy)
3257 if (board->layout == LAYOUT_4020)
3259 else if (use_internal_queue_6xxx(&dev->read_subdev->async->cmd))
3264 static int ao_inttrig(struct comedi_device *dev, struct comedi_subdevice *s,
3265 unsigned int trig_num)
3267 struct pcidas64_private *devpriv = dev->private;
3268 struct comedi_cmd *cmd = &s->async->cmd;
3271 if (trig_num != cmd->start_arg)
3274 retval = prep_ao_dma(dev, cmd);
3278 set_dac_control0_reg(dev, cmd);
3280 if (cmd->start_src == TRIG_INT)
3281 writew(0, devpriv->main_iobase + DAC_START_REG);
3283 s->async->inttrig = NULL;
3288 static int ao_cmd(struct comedi_device *dev, struct comedi_subdevice *s)
3290 struct pcidas64_private *devpriv = dev->private;
3291 struct comedi_cmd *cmd = &s->async->cmd;
3293 if (external_ai_queue_in_use(dev)) {
3294 warn_external_queue(dev);
3297 /* disable analog output system during setup */
3298 writew(0x0, devpriv->main_iobase + DAC_CONTROL0_REG);
3300 devpriv->ao_dma_index = 0;
3302 set_dac_select_reg(dev, cmd);
3303 set_dac_interval_regs(dev, cmd);
3304 load_first_dma_descriptor(dev, 0, devpriv->ao_dma_desc_bus_addr |
3305 PLX_DMADPR_DESCPCI | PLX_DMADPR_TCINTR);
3307 set_dac_control1_reg(dev, cmd);
3308 s->async->inttrig = ao_inttrig;
3313 static int cb_pcidas64_ao_check_chanlist(struct comedi_device *dev,
3314 struct comedi_subdevice *s,
3315 struct comedi_cmd *cmd)
3317 unsigned int chan0 = CR_CHAN(cmd->chanlist[0]);
3320 for (i = 1; i < cmd->chanlist_len; i++) {
3321 unsigned int chan = CR_CHAN(cmd->chanlist[i]);
3323 if (chan != (chan0 + i)) {
3324 dev_dbg(dev->class_dev,
3325 "chanlist must use consecutive channels\n");
3333 static int ao_cmdtest(struct comedi_device *dev, struct comedi_subdevice *s,
3334 struct comedi_cmd *cmd)
3336 const struct pcidas64_board *board = dev->board_ptr;
3338 unsigned int tmp_arg;
3340 /* Step 1 : check if triggers are trivially valid */
3342 err |= comedi_check_trigger_src(&cmd->start_src, TRIG_INT | TRIG_EXT);
3343 err |= comedi_check_trigger_src(&cmd->scan_begin_src,
3344 TRIG_TIMER | TRIG_EXT);
3345 err |= comedi_check_trigger_src(&cmd->convert_src, TRIG_NOW);
3346 err |= comedi_check_trigger_src(&cmd->scan_end_src, TRIG_COUNT);
3347 err |= comedi_check_trigger_src(&cmd->stop_src, TRIG_NONE);
3352 /* Step 2a : make sure trigger sources are unique */
3354 err |= comedi_check_trigger_is_unique(cmd->start_src);
3355 err |= comedi_check_trigger_is_unique(cmd->scan_begin_src);
3357 /* Step 2b : and mutually compatible */
3359 if (cmd->convert_src == TRIG_EXT && cmd->scan_begin_src == TRIG_TIMER)
3361 if (cmd->stop_src != TRIG_COUNT &&
3362 cmd->stop_src != TRIG_NONE && cmd->stop_src != TRIG_EXT)
3368 /* Step 3: check if arguments are trivially valid */
3370 err |= comedi_check_trigger_arg_is(&cmd->start_arg, 0);
3372 if (cmd->scan_begin_src == TRIG_TIMER) {
3373 err |= comedi_check_trigger_arg_min(&cmd->scan_begin_arg,
3374 board->ao_scan_speed);
3375 if (get_ao_divisor(cmd->scan_begin_arg, cmd->flags) >
3376 max_counter_value) {
3377 cmd->scan_begin_arg = (max_counter_value + 2) *
3383 err |= comedi_check_trigger_arg_min(&cmd->chanlist_len, 1);
3384 err |= comedi_check_trigger_arg_is(&cmd->scan_end_arg,
3390 /* step 4: fix up any arguments */
3392 if (cmd->scan_begin_src == TRIG_TIMER) {
3393 tmp_arg = cmd->scan_begin_arg;
3394 cmd->scan_begin_arg = get_divisor(cmd->scan_begin_arg,
3395 cmd->flags) * TIMER_BASE;
3396 if (tmp_arg != cmd->scan_begin_arg)
3403 /* Step 5: check channel list if it exists */
3404 if (cmd->chanlist && cmd->chanlist_len > 0)
3405 err |= cb_pcidas64_ao_check_chanlist(dev, s, cmd);
3413 static int ao_cancel(struct comedi_device *dev, struct comedi_subdevice *s)
3415 struct pcidas64_private *devpriv = dev->private;
3417 writew(0x0, devpriv->main_iobase + DAC_CONTROL0_REG);
3422 static int dio_callback_4020(struct comedi_device *dev,
3423 int dir, int port, int data, unsigned long iobase)
3425 struct pcidas64_private *devpriv = dev->private;
3428 writew(data, devpriv->main_iobase + iobase + 2 * port);
3431 return readw(devpriv->main_iobase + iobase + 2 * port);
3434 static int di_rbits(struct comedi_device *dev, struct comedi_subdevice *s,
3435 struct comedi_insn *insn, unsigned int *data)
3439 bits = readb(dev->mmio + DI_REG);
3447 static int do_wbits(struct comedi_device *dev,
3448 struct comedi_subdevice *s,
3449 struct comedi_insn *insn,
3452 if (comedi_dio_update_state(s, data))
3453 writeb(s->state, dev->mmio + DO_REG);
3460 static int dio_60xx_config_insn(struct comedi_device *dev,
3461 struct comedi_subdevice *s,
3462 struct comedi_insn *insn,
3467 ret = comedi_dio_insn_config(dev, s, insn, data, 0);
3471 writeb(s->io_bits, dev->mmio + DIO_DIRECTION_60XX_REG);
3476 static int dio_60xx_wbits(struct comedi_device *dev,
3477 struct comedi_subdevice *s,
3478 struct comedi_insn *insn,
3481 if (comedi_dio_update_state(s, data))
3482 writeb(s->state, dev->mmio + DIO_DATA_60XX_REG);
3484 data[1] = readb(dev->mmio + DIO_DATA_60XX_REG);
3490 * pci-6025 8800 caldac:
3491 * address 0 == dac channel 0 offset
3492 * address 1 == dac channel 0 gain
3493 * address 2 == dac channel 1 offset
3494 * address 3 == dac channel 1 gain
3495 * address 4 == fine adc offset
3496 * address 5 == coarse adc offset
3497 * address 6 == coarse adc gain
3498 * address 7 == fine adc gain
3501 * pci-6402/16 uses all 8 channels for dac:
3502 * address 0 == dac channel 0 fine gain
3503 * address 1 == dac channel 0 coarse gain
3504 * address 2 == dac channel 0 coarse offset
3505 * address 3 == dac channel 1 coarse offset
3506 * address 4 == dac channel 1 fine gain
3507 * address 5 == dac channel 1 coarse gain
3508 * address 6 == dac channel 0 fine offset
3509 * address 7 == dac channel 1 fine offset
3512 static int caldac_8800_write(struct comedi_device *dev, unsigned int address,
3515 struct pcidas64_private *devpriv = dev->private;
3516 static const int num_caldac_channels = 8;
3517 static const int bitstream_length = 11;
3518 unsigned int bitstream = ((address & 0x7) << 8) | value;
3519 unsigned int bit, register_bits;
3520 static const int caldac_8800_udelay = 1;
3522 if (address >= num_caldac_channels) {
3523 dev_err(dev->class_dev, "illegal caldac channel\n");
3526 for (bit = 1 << (bitstream_length - 1); bit; bit >>= 1) {
3528 if (bitstream & bit)
3529 register_bits |= SERIAL_DATA_IN_BIT;
3530 udelay(caldac_8800_udelay);
3531 writew(register_bits, devpriv->main_iobase + CALIBRATION_REG);
3532 register_bits |= SERIAL_CLOCK_BIT;
3533 udelay(caldac_8800_udelay);
3534 writew(register_bits, devpriv->main_iobase + CALIBRATION_REG);
3536 udelay(caldac_8800_udelay);
3537 writew(SELECT_8800_BIT, devpriv->main_iobase + CALIBRATION_REG);
3538 udelay(caldac_8800_udelay);
3539 writew(0, devpriv->main_iobase + CALIBRATION_REG);
3540 udelay(caldac_8800_udelay);
3545 static int caldac_i2c_write(struct comedi_device *dev,
3546 unsigned int caldac_channel, unsigned int value)
3551 /* manual has gain and offset bits switched */
3558 NOT_CLEAR_REGISTERS = 0x20,
3561 switch (caldac_channel) {
3562 case 0: /* chan 0 offset */
3563 i2c_addr = CALDAC0_I2C_ADDR;
3564 serial_bytes[0] = OFFSET_0_2;
3566 case 1: /* chan 1 offset */
3567 i2c_addr = CALDAC0_I2C_ADDR;
3568 serial_bytes[0] = OFFSET_1_3;
3570 case 2: /* chan 2 offset */
3571 i2c_addr = CALDAC1_I2C_ADDR;
3572 serial_bytes[0] = OFFSET_0_2;
3574 case 3: /* chan 3 offset */
3575 i2c_addr = CALDAC1_I2C_ADDR;
3576 serial_bytes[0] = OFFSET_1_3;
3578 case 4: /* chan 0 gain */
3579 i2c_addr = CALDAC0_I2C_ADDR;
3580 serial_bytes[0] = GAIN_0_2;
3582 case 5: /* chan 1 gain */
3583 i2c_addr = CALDAC0_I2C_ADDR;
3584 serial_bytes[0] = GAIN_1_3;
3586 case 6: /* chan 2 gain */
3587 i2c_addr = CALDAC1_I2C_ADDR;
3588 serial_bytes[0] = GAIN_0_2;
3590 case 7: /* chan 3 gain */
3591 i2c_addr = CALDAC1_I2C_ADDR;
3592 serial_bytes[0] = GAIN_1_3;
3595 dev_err(dev->class_dev, "invalid caldac channel\n");
3598 serial_bytes[1] = NOT_CLEAR_REGISTERS | ((value >> 8) & 0xf);
3599 serial_bytes[2] = value & 0xff;
3600 i2c_write(dev, i2c_addr, serial_bytes, 3);
3604 static void caldac_write(struct comedi_device *dev, unsigned int channel,
3607 const struct pcidas64_board *board = dev->board_ptr;
3609 switch (board->layout) {
3612 caldac_8800_write(dev, channel, value);
3615 caldac_i2c_write(dev, channel, value);
3622 static int cb_pcidas64_calib_insn_write(struct comedi_device *dev,
3623 struct comedi_subdevice *s,
3624 struct comedi_insn *insn,
3627 unsigned int chan = CR_CHAN(insn->chanspec);
3630 * Programming the calib device is slow. Only write the
3631 * last data value if the value has changed.
3634 unsigned int val = data[insn->n - 1];
3636 if (s->readback[chan] != val) {
3637 caldac_write(dev, chan, val);
3638 s->readback[chan] = val;
3645 static void ad8402_write(struct comedi_device *dev, unsigned int channel,
3648 struct pcidas64_private *devpriv = dev->private;
3649 static const int bitstream_length = 10;
3650 unsigned int bit, register_bits;
3651 unsigned int bitstream = ((channel & 0x3) << 8) | (value & 0xff);
3652 static const int ad8402_udelay = 1;
3654 register_bits = SELECT_8402_64XX_BIT;
3655 udelay(ad8402_udelay);
3656 writew(register_bits, devpriv->main_iobase + CALIBRATION_REG);
3658 for (bit = 1 << (bitstream_length - 1); bit; bit >>= 1) {
3659 if (bitstream & bit)
3660 register_bits |= SERIAL_DATA_IN_BIT;
3662 register_bits &= ~SERIAL_DATA_IN_BIT;
3663 udelay(ad8402_udelay);
3664 writew(register_bits, devpriv->main_iobase + CALIBRATION_REG);
3665 udelay(ad8402_udelay);
3666 writew(register_bits | SERIAL_CLOCK_BIT,
3667 devpriv->main_iobase + CALIBRATION_REG);
3670 udelay(ad8402_udelay);
3671 writew(0, devpriv->main_iobase + CALIBRATION_REG);
3674 /* for pci-das6402/16, channel 0 is analog input gain and channel 1 is offset */
3675 static int cb_pcidas64_ad8402_insn_write(struct comedi_device *dev,
3676 struct comedi_subdevice *s,
3677 struct comedi_insn *insn,
3680 unsigned int chan = CR_CHAN(insn->chanspec);
3683 * Programming the calib device is slow. Only write the
3684 * last data value if the value has changed.
3687 unsigned int val = data[insn->n - 1];
3689 if (s->readback[chan] != val) {
3690 ad8402_write(dev, chan, val);
3691 s->readback[chan] = val;
3698 static u16 read_eeprom(struct comedi_device *dev, u8 address)
3700 struct pcidas64_private *devpriv = dev->private;
3701 static const int bitstream_length = 11;
3702 static const int read_command = 0x6;
3703 unsigned int bitstream = (read_command << 8) | address;
3705 void __iomem * const plx_control_addr =
3706 devpriv->plx9080_iobase + PLX_REG_CNTRL;
3708 static const int value_length = 16;
3709 static const int eeprom_udelay = 1;
3711 udelay(eeprom_udelay);
3712 devpriv->plx_control_bits &= ~PLX_CNTRL_EESK & ~PLX_CNTRL_EECS;
3713 /* make sure we don't send anything to the i2c bus on 4020 */
3714 devpriv->plx_control_bits |= PLX_CNTRL_USERO;
3715 writel(devpriv->plx_control_bits, plx_control_addr);
3716 /* activate serial eeprom */
3717 udelay(eeprom_udelay);
3718 devpriv->plx_control_bits |= PLX_CNTRL_EECS;
3719 writel(devpriv->plx_control_bits, plx_control_addr);
3721 /* write read command and desired memory address */
3722 for (bit = 1 << (bitstream_length - 1); bit; bit >>= 1) {
3723 /* set bit to be written */
3724 udelay(eeprom_udelay);
3725 if (bitstream & bit)
3726 devpriv->plx_control_bits |= PLX_CNTRL_EEWB;
3728 devpriv->plx_control_bits &= ~PLX_CNTRL_EEWB;
3729 writel(devpriv->plx_control_bits, plx_control_addr);
3731 udelay(eeprom_udelay);
3732 devpriv->plx_control_bits |= PLX_CNTRL_EESK;
3733 writel(devpriv->plx_control_bits, plx_control_addr);
3734 udelay(eeprom_udelay);
3735 devpriv->plx_control_bits &= ~PLX_CNTRL_EESK;
3736 writel(devpriv->plx_control_bits, plx_control_addr);
3738 /* read back value from eeprom memory location */
3740 for (bit = 1 << (value_length - 1); bit; bit >>= 1) {
3742 udelay(eeprom_udelay);
3743 devpriv->plx_control_bits |= PLX_CNTRL_EESK;
3744 writel(devpriv->plx_control_bits, plx_control_addr);
3745 udelay(eeprom_udelay);
3746 devpriv->plx_control_bits &= ~PLX_CNTRL_EESK;
3747 writel(devpriv->plx_control_bits, plx_control_addr);
3748 udelay(eeprom_udelay);
3749 if (readl(plx_control_addr) & PLX_CNTRL_EERB)
3753 /* deactivate eeprom serial input */
3754 udelay(eeprom_udelay);
3755 devpriv->plx_control_bits &= ~PLX_CNTRL_EECS;
3756 writel(devpriv->plx_control_bits, plx_control_addr);
3761 static int eeprom_read_insn(struct comedi_device *dev,
3762 struct comedi_subdevice *s,
3763 struct comedi_insn *insn, unsigned int *data)
3765 data[0] = read_eeprom(dev, CR_CHAN(insn->chanspec));
3770 /* Allocate and initialize the subdevice structures. */
3771 static int setup_subdevices(struct comedi_device *dev)
3773 const struct pcidas64_board *board = dev->board_ptr;
3774 struct pcidas64_private *devpriv = dev->private;
3775 struct comedi_subdevice *s;
3779 ret = comedi_alloc_subdevices(dev, 10);
3783 s = &dev->subdevices[0];
3784 /* analog input subdevice */
3785 dev->read_subdev = s;
3786 s->type = COMEDI_SUBD_AI;
3787 s->subdev_flags = SDF_READABLE | SDF_GROUND | SDF_DITHER | SDF_CMD_READ;
3788 if (board->layout == LAYOUT_60XX)
3789 s->subdev_flags |= SDF_COMMON | SDF_DIFF;
3790 else if (board->layout == LAYOUT_64XX)
3791 s->subdev_flags |= SDF_DIFF;
3792 /* XXX Number of inputs in differential mode is ignored */
3793 s->n_chan = board->ai_se_chans;
3794 s->len_chanlist = 0x2000;
3795 s->maxdata = (1 << board->ai_bits) - 1;
3796 s->range_table = board->ai_range_table;
3797 s->insn_read = ai_rinsn;
3798 s->insn_config = ai_config_insn;
3800 s->do_cmdtest = ai_cmdtest;
3801 s->cancel = ai_cancel;
3802 if (board->layout == LAYOUT_4020) {
3805 * set adc to read from inputs
3806 * (not internal calibration sources)
3808 devpriv->i2c_cal_range_bits = adc_src_4020_bits(4);
3809 /* set channels to +-5 volt input ranges */
3810 for (i = 0; i < s->n_chan; i++)
3811 devpriv->i2c_cal_range_bits |= attenuate_bit(i);
3812 data = devpriv->i2c_cal_range_bits;
3813 i2c_write(dev, RANGE_CAL_I2C_ADDR, &data, sizeof(data));
3816 /* analog output subdevice */
3817 s = &dev->subdevices[1];
3818 if (board->ao_nchan) {
3819 s->type = COMEDI_SUBD_AO;
3820 s->subdev_flags = SDF_READABLE | SDF_WRITABLE |
3821 SDF_GROUND | SDF_CMD_WRITE;
3822 s->n_chan = board->ao_nchan;
3823 s->maxdata = (1 << board->ao_bits) - 1;
3824 s->range_table = board->ao_range_table;
3825 s->insn_write = ao_winsn;
3827 ret = comedi_alloc_subdev_readback(s);
3831 if (ao_cmd_is_supported(board)) {
3832 dev->write_subdev = s;
3833 s->do_cmdtest = ao_cmdtest;
3835 s->len_chanlist = board->ao_nchan;
3836 s->cancel = ao_cancel;
3839 s->type = COMEDI_SUBD_UNUSED;
3843 s = &dev->subdevices[2];
3844 if (board->layout == LAYOUT_64XX) {
3845 s->type = COMEDI_SUBD_DI;
3846 s->subdev_flags = SDF_READABLE;
3849 s->range_table = &range_digital;
3850 s->insn_bits = di_rbits;
3852 s->type = COMEDI_SUBD_UNUSED;
3855 /* digital output */
3856 if (board->layout == LAYOUT_64XX) {
3857 s = &dev->subdevices[3];
3858 s->type = COMEDI_SUBD_DO;
3859 s->subdev_flags = SDF_WRITABLE;
3862 s->range_table = &range_digital;
3863 s->insn_bits = do_wbits;
3865 s->type = COMEDI_SUBD_UNUSED;
3869 s = &dev->subdevices[4];
3870 if (board->has_8255) {
3871 if (board->layout == LAYOUT_4020) {
3872 ret = subdev_8255_init(dev, s, dio_callback_4020,
3875 ret = subdev_8255_mm_init(dev, s, NULL,
3881 s->type = COMEDI_SUBD_UNUSED;
3884 /* 8 channel dio for 60xx */
3885 s = &dev->subdevices[5];
3886 if (board->layout == LAYOUT_60XX) {
3887 s->type = COMEDI_SUBD_DIO;
3888 s->subdev_flags = SDF_WRITABLE | SDF_READABLE;
3891 s->range_table = &range_digital;
3892 s->insn_config = dio_60xx_config_insn;
3893 s->insn_bits = dio_60xx_wbits;
3895 s->type = COMEDI_SUBD_UNUSED;
3899 s = &dev->subdevices[6];
3900 s->type = COMEDI_SUBD_CALIB;
3901 s->subdev_flags = SDF_READABLE | SDF_WRITABLE | SDF_INTERNAL;
3903 if (board->layout == LAYOUT_4020)
3907 s->insn_write = cb_pcidas64_calib_insn_write;
3909 ret = comedi_alloc_subdev_readback(s);
3913 for (i = 0; i < s->n_chan; i++) {
3914 caldac_write(dev, i, s->maxdata / 2);
3915 s->readback[i] = s->maxdata / 2;
3918 /* 2 channel ad8402 potentiometer */
3919 s = &dev->subdevices[7];
3920 if (board->layout == LAYOUT_64XX) {
3921 s->type = COMEDI_SUBD_CALIB;
3922 s->subdev_flags = SDF_READABLE | SDF_WRITABLE | SDF_INTERNAL;
3925 s->insn_write = cb_pcidas64_ad8402_insn_write;
3927 ret = comedi_alloc_subdev_readback(s);
3931 for (i = 0; i < s->n_chan; i++) {
3932 ad8402_write(dev, i, s->maxdata / 2);
3933 s->readback[i] = s->maxdata / 2;
3936 s->type = COMEDI_SUBD_UNUSED;
3939 /* serial EEPROM, if present */
3940 s = &dev->subdevices[8];
3941 if (readl(devpriv->plx9080_iobase + PLX_REG_CNTRL) &
3942 PLX_CNTRL_EEPRESENT) {
3943 s->type = COMEDI_SUBD_MEMORY;
3944 s->subdev_flags = SDF_READABLE | SDF_INTERNAL;
3946 s->maxdata = 0xffff;
3947 s->insn_read = eeprom_read_insn;
3949 s->type = COMEDI_SUBD_UNUSED;
3952 /* user counter subd XXX */
3953 s = &dev->subdevices[9];
3954 s->type = COMEDI_SUBD_UNUSED;
3959 static int auto_attach(struct comedi_device *dev,
3960 unsigned long context)
3962 struct pci_dev *pcidev = comedi_to_pci_dev(dev);
3963 const struct pcidas64_board *board = NULL;
3964 struct pcidas64_private *devpriv;
3965 u32 local_range, local_decode;
3968 if (context < ARRAY_SIZE(pcidas64_boards))
3969 board = &pcidas64_boards[context];
3972 dev->board_ptr = board;
3974 devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
3978 retval = comedi_pci_enable(dev);
3981 pci_set_master(pcidev);
3983 /* Initialize dev->board_name */
3984 dev->board_name = board->name;
3986 devpriv->main_phys_iobase = pci_resource_start(pcidev, 2);
3987 devpriv->dio_counter_phys_iobase = pci_resource_start(pcidev, 3);
3989 devpriv->plx9080_iobase = pci_ioremap_bar(pcidev, 0);
3990 devpriv->main_iobase = pci_ioremap_bar(pcidev, 2);
3991 dev->mmio = pci_ioremap_bar(pcidev, 3);
3993 if (!devpriv->plx9080_iobase || !devpriv->main_iobase || !dev->mmio) {
3994 dev_warn(dev->class_dev, "failed to remap io memory\n");
3998 /* figure out what local addresses are */
3999 local_range = readl(devpriv->plx9080_iobase + PLX_REG_LAS0RR) &
4001 local_decode = readl(devpriv->plx9080_iobase + PLX_REG_LAS0BA) &
4002 local_range & PLX_LASBA_MEM_MASK;
4003 devpriv->local0_iobase = ((u32)devpriv->main_phys_iobase &
4004 ~local_range) | local_decode;
4005 local_range = readl(devpriv->plx9080_iobase + PLX_REG_LAS1RR) &
4007 local_decode = readl(devpriv->plx9080_iobase + PLX_REG_LAS1BA) &
4008 local_range & PLX_LASBA_MEM_MASK;
4009 devpriv->local1_iobase = ((u32)devpriv->dio_counter_phys_iobase &
4010 ~local_range) | local_decode;
4012 retval = alloc_and_init_dma_members(dev);
4016 devpriv->hw_revision =
4017 hw_revision(dev, readw(devpriv->main_iobase + HW_STATUS_REG));
4018 dev_dbg(dev->class_dev, "stc hardware revision %i\n",
4019 devpriv->hw_revision);
4021 init_stc_registers(dev);
4023 retval = request_irq(pcidev->irq, handle_interrupt, IRQF_SHARED,
4024 dev->board_name, dev);
4026 dev_dbg(dev->class_dev, "unable to allocate irq %u\n",
4030 dev->irq = pcidev->irq;
4031 dev_dbg(dev->class_dev, "irq %u\n", dev->irq);
4033 retval = setup_subdevices(dev);
4040 static void detach(struct comedi_device *dev)
4042 struct pcidas64_private *devpriv = dev->private;
4045 free_irq(dev->irq, dev);
4047 if (devpriv->plx9080_iobase) {
4048 disable_plx_interrupts(dev);
4049 iounmap(devpriv->plx9080_iobase);
4051 if (devpriv->main_iobase)
4052 iounmap(devpriv->main_iobase);
4056 comedi_pci_disable(dev);
4057 cb_pcidas64_free_dma(dev);
4060 static struct comedi_driver cb_pcidas64_driver = {
4061 .driver_name = "cb_pcidas64",
4062 .module = THIS_MODULE,
4063 .auto_attach = auto_attach,
4067 static int cb_pcidas64_pci_probe(struct pci_dev *dev,
4068 const struct pci_device_id *id)
4070 return comedi_pci_auto_config(dev, &cb_pcidas64_driver,
4074 static const struct pci_device_id cb_pcidas64_pci_table[] = {
4075 { PCI_VDEVICE(CB, 0x001d), BOARD_PCIDAS6402_16 },
4076 { PCI_VDEVICE(CB, 0x001e), BOARD_PCIDAS6402_12 },
4077 { PCI_VDEVICE(CB, 0x0035), BOARD_PCIDAS64_M1_16 },
4078 { PCI_VDEVICE(CB, 0x0036), BOARD_PCIDAS64_M2_16 },
4079 { PCI_VDEVICE(CB, 0x0037), BOARD_PCIDAS64_M3_16 },
4080 { PCI_VDEVICE(CB, 0x0052), BOARD_PCIDAS4020_12 },
4081 { PCI_VDEVICE(CB, 0x005d), BOARD_PCIDAS6023 },
4082 { PCI_VDEVICE(CB, 0x005e), BOARD_PCIDAS6025 },
4083 { PCI_VDEVICE(CB, 0x005f), BOARD_PCIDAS6030 },
4084 { PCI_VDEVICE(CB, 0x0060), BOARD_PCIDAS6031 },
4085 { PCI_VDEVICE(CB, 0x0061), BOARD_PCIDAS6032 },
4086 { PCI_VDEVICE(CB, 0x0062), BOARD_PCIDAS6033 },
4087 { PCI_VDEVICE(CB, 0x0063), BOARD_PCIDAS6034 },
4088 { PCI_VDEVICE(CB, 0x0064), BOARD_PCIDAS6035 },
4089 { PCI_VDEVICE(CB, 0x0065), BOARD_PCIDAS6040 },
4090 { PCI_VDEVICE(CB, 0x0066), BOARD_PCIDAS6052 },
4091 { PCI_VDEVICE(CB, 0x0067), BOARD_PCIDAS6070 },
4092 { PCI_VDEVICE(CB, 0x0068), BOARD_PCIDAS6071 },
4093 { PCI_VDEVICE(CB, 0x006f), BOARD_PCIDAS6036 },
4094 { PCI_VDEVICE(CB, 0x0078), BOARD_PCIDAS6013 },
4095 { PCI_VDEVICE(CB, 0x0079), BOARD_PCIDAS6014 },
4098 MODULE_DEVICE_TABLE(pci, cb_pcidas64_pci_table);
4100 static struct pci_driver cb_pcidas64_pci_driver = {
4101 .name = "cb_pcidas64",
4102 .id_table = cb_pcidas64_pci_table,
4103 .probe = cb_pcidas64_pci_probe,
4104 .remove = comedi_pci_auto_unconfig,
4106 module_comedi_pci_driver(cb_pcidas64_driver, cb_pcidas64_pci_driver);
4108 MODULE_AUTHOR("Comedi http://www.comedi.org");
4109 MODULE_DESCRIPTION("Comedi low-level driver");
4110 MODULE_LICENSE("GPL");