2 comedi/drivers/ni_mio_common.c
3 Hardware driver for DAQ-STC based boards
5 COMEDI - Linux Control and Measurement Device Interface
6 Copyright (C) 1997-2001 David A. Schleef <ds@schleef.org>
7 Copyright (C) 2002-2006 Frank Mori Hess <fmhess@users.sourceforge.net>
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
21 This file is meant to be included by another file, e.g.,
22 ni_atmio.c or ni_pcimio.c.
24 Interrupt support originally added by Truxton Fulton
27 References (from ftp://ftp.natinst.com/support/manuals):
29 340747b.pdf AT-MIO E series Register Level Programmer Manual
30 341079b.pdf PCI E Series RLPM
31 340934b.pdf DAQ-STC reference manual
32 67xx and 611x registers (from ftp://ftp.ni.com/support/daq/mhddk/documentation/)
35 Other possibly relevant info:
37 320517c.pdf User manual (obsolete)
38 320517f.pdf User manual (new)
40 320906c.pdf maximum signal ratings
42 321791a.pdf discontinuation of at-mio-16e-10 rev. c
43 321808a.pdf about at-mio-16e-10 rev P
44 321837a.pdf discontinuation of at-mio-16de-10 rev d
45 321838a.pdf about at-mio-16de-10 rev N
49 - the interrupt routine needs to be cleaned up
51 2006-02-07: S-Series PCI-6143: Support has been added but is not
52 fully tested as yet. Terry Barnaby, BEAM Ltd.
55 /* #define DEBUG_INTERRUPT */
56 /* #define DEBUG_STATUS_A */
57 /* #define DEBUG_STATUS_B */
59 #include <linux/interrupt.h>
60 #include <linux/sched.h>
63 #include "comedi_fc.h"
66 #define MDPRINTK(format, args...)
70 #define NI_TIMEOUT 1000
71 static const unsigned old_RTSI_clock_channel
= 7;
73 /* Note: this table must match the ai_gain_* definitions */
74 static const short ni_gainlkup
[][16] = {
75 [ai_gain_16
] = {0, 1, 2, 3, 4, 5, 6, 7,
76 0x100, 0x101, 0x102, 0x103, 0x104, 0x105, 0x106, 0x107},
77 [ai_gain_8
] = {1, 2, 4, 7, 0x101, 0x102, 0x104, 0x107},
78 [ai_gain_14
] = {1, 2, 3, 4, 5, 6, 7,
79 0x101, 0x102, 0x103, 0x104, 0x105, 0x106, 0x107},
80 [ai_gain_4
] = {0, 1, 4, 7},
81 [ai_gain_611x
] = {0x00a, 0x00b, 0x001, 0x002,
82 0x003, 0x004, 0x005, 0x006},
83 [ai_gain_622x
] = {0, 1, 4, 5},
84 [ai_gain_628x
] = {1, 2, 3, 4, 5, 6, 7},
85 [ai_gain_6143
] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
88 static const struct comedi_lrange range_ni_E_ai
= { 16, {
108 static const struct comedi_lrange range_ni_E_ai_limited
= { 8, {
121 static const struct comedi_lrange range_ni_E_ai_limited14
= { 14, {
146 static const struct comedi_lrange range_ni_E_ai_bipolar4
= { 4, {
156 static const struct comedi_lrange range_ni_E_ai_611x
= { 8, {
168 static const struct comedi_lrange range_ni_M_ai_622x
= { 4, {
176 static const struct comedi_lrange range_ni_M_ai_628x
= { 7, {
187 static const struct comedi_lrange range_ni_E_ao_ext
= { 4, {
195 static const struct comedi_lrange
*const ni_range_lkup
[] = {
196 [ai_gain_16
] = &range_ni_E_ai
,
197 [ai_gain_8
] = &range_ni_E_ai_limited
,
198 [ai_gain_14
] = &range_ni_E_ai_limited14
,
199 [ai_gain_4
] = &range_ni_E_ai_bipolar4
,
200 [ai_gain_611x
] = &range_ni_E_ai_611x
,
201 [ai_gain_622x
] = &range_ni_M_ai_622x
,
202 [ai_gain_628x
] = &range_ni_M_ai_628x
,
203 [ai_gain_6143
] = &range_bipolar5
206 static int ni_dio_insn_config(struct comedi_device
*dev
,
207 struct comedi_subdevice
*s
,
208 struct comedi_insn
*insn
, unsigned int *data
);
209 static int ni_dio_insn_bits(struct comedi_device
*dev
,
210 struct comedi_subdevice
*s
,
211 struct comedi_insn
*insn
, unsigned int *data
);
212 static int ni_cdio_cmdtest(struct comedi_device
*dev
,
213 struct comedi_subdevice
*s
, struct comedi_cmd
*cmd
);
214 static int ni_cdio_cmd(struct comedi_device
*dev
, struct comedi_subdevice
*s
);
215 static int ni_cdio_cancel(struct comedi_device
*dev
,
216 struct comedi_subdevice
*s
);
217 static void handle_cdio_interrupt(struct comedi_device
*dev
);
218 static int ni_cdo_inttrig(struct comedi_device
*dev
, struct comedi_subdevice
*s
,
219 unsigned int trignum
);
221 static int ni_serial_insn_config(struct comedi_device
*dev
,
222 struct comedi_subdevice
*s
,
223 struct comedi_insn
*insn
, unsigned int *data
);
224 static int ni_serial_hw_readwrite8(struct comedi_device
*dev
,
225 struct comedi_subdevice
*s
,
226 unsigned char data_out
,
227 unsigned char *data_in
);
228 static int ni_serial_sw_readwrite8(struct comedi_device
*dev
,
229 struct comedi_subdevice
*s
,
230 unsigned char data_out
,
231 unsigned char *data_in
);
233 static int ni_calib_insn_read(struct comedi_device
*dev
,
234 struct comedi_subdevice
*s
,
235 struct comedi_insn
*insn
, unsigned int *data
);
236 static int ni_calib_insn_write(struct comedi_device
*dev
,
237 struct comedi_subdevice
*s
,
238 struct comedi_insn
*insn
, unsigned int *data
);
240 static int ni_eeprom_insn_read(struct comedi_device
*dev
,
241 struct comedi_subdevice
*s
,
242 struct comedi_insn
*insn
, unsigned int *data
);
243 static int ni_m_series_eeprom_insn_read(struct comedi_device
*dev
,
244 struct comedi_subdevice
*s
,
245 struct comedi_insn
*insn
,
248 static int ni_pfi_insn_bits(struct comedi_device
*dev
,
249 struct comedi_subdevice
*s
,
250 struct comedi_insn
*insn
, unsigned int *data
);
251 static int ni_pfi_insn_config(struct comedi_device
*dev
,
252 struct comedi_subdevice
*s
,
253 struct comedi_insn
*insn
, unsigned int *data
);
254 static unsigned ni_old_get_pfi_routing(struct comedi_device
*dev
,
257 static void ni_rtsi_init(struct comedi_device
*dev
);
258 static int ni_rtsi_insn_bits(struct comedi_device
*dev
,
259 struct comedi_subdevice
*s
,
260 struct comedi_insn
*insn
, unsigned int *data
);
261 static int ni_rtsi_insn_config(struct comedi_device
*dev
,
262 struct comedi_subdevice
*s
,
263 struct comedi_insn
*insn
, unsigned int *data
);
265 static void caldac_setup(struct comedi_device
*dev
, struct comedi_subdevice
*s
);
266 static int ni_read_eeprom(struct comedi_device
*dev
, int addr
);
268 #ifdef DEBUG_STATUS_A
269 static void ni_mio_print_status_a(int status
);
271 #define ni_mio_print_status_a(a)
273 #ifdef DEBUG_STATUS_B
274 static void ni_mio_print_status_b(int status
);
276 #define ni_mio_print_status_b(a)
279 static int ni_ai_reset(struct comedi_device
*dev
, struct comedi_subdevice
*s
);
281 static void ni_handle_fifo_half_full(struct comedi_device
*dev
);
282 static int ni_ao_fifo_half_empty(struct comedi_device
*dev
,
283 struct comedi_subdevice
*s
);
285 static void ni_handle_fifo_dregs(struct comedi_device
*dev
);
286 static int ni_ai_inttrig(struct comedi_device
*dev
, struct comedi_subdevice
*s
,
287 unsigned int trignum
);
288 static void ni_load_channelgain_list(struct comedi_device
*dev
,
289 unsigned int n_chan
, unsigned int *list
);
290 static void shutdown_ai_command(struct comedi_device
*dev
);
292 static int ni_ao_inttrig(struct comedi_device
*dev
, struct comedi_subdevice
*s
,
293 unsigned int trignum
);
295 static int ni_ao_reset(struct comedi_device
*dev
, struct comedi_subdevice
*s
);
297 static int ni_8255_callback(int dir
, int port
, int data
, unsigned long arg
);
299 static int ni_gpct_insn_write(struct comedi_device
*dev
,
300 struct comedi_subdevice
*s
,
301 struct comedi_insn
*insn
, unsigned int *data
);
302 static int ni_gpct_insn_read(struct comedi_device
*dev
,
303 struct comedi_subdevice
*s
,
304 struct comedi_insn
*insn
, unsigned int *data
);
305 static int ni_gpct_insn_config(struct comedi_device
*dev
,
306 struct comedi_subdevice
*s
,
307 struct comedi_insn
*insn
, unsigned int *data
);
308 static int ni_gpct_cmd(struct comedi_device
*dev
, struct comedi_subdevice
*s
);
309 static int ni_gpct_cmdtest(struct comedi_device
*dev
,
310 struct comedi_subdevice
*s
, struct comedi_cmd
*cmd
);
311 static int ni_gpct_cancel(struct comedi_device
*dev
,
312 struct comedi_subdevice
*s
);
313 static void handle_gpct_interrupt(struct comedi_device
*dev
,
314 unsigned short counter_index
);
316 static int init_cs5529(struct comedi_device
*dev
);
317 static int cs5529_do_conversion(struct comedi_device
*dev
,
318 unsigned short *data
);
319 static int cs5529_ai_insn_read(struct comedi_device
*dev
,
320 struct comedi_subdevice
*s
,
321 struct comedi_insn
*insn
, unsigned int *data
);
322 #ifdef NI_CS5529_DEBUG
323 static unsigned int cs5529_config_read(struct comedi_device
*dev
,
324 unsigned int reg_select_bits
);
326 static void cs5529_config_write(struct comedi_device
*dev
, unsigned int value
,
327 unsigned int reg_select_bits
);
329 static int ni_m_series_pwm_config(struct comedi_device
*dev
,
330 struct comedi_subdevice
*s
,
331 struct comedi_insn
*insn
, unsigned int *data
);
332 static int ni_6143_pwm_config(struct comedi_device
*dev
,
333 struct comedi_subdevice
*s
,
334 struct comedi_insn
*insn
, unsigned int *data
);
336 static int ni_set_master_clock(struct comedi_device
*dev
, unsigned source
,
338 static void ack_a_interrupt(struct comedi_device
*dev
, unsigned short a_status
);
339 static void ack_b_interrupt(struct comedi_device
*dev
, unsigned short b_status
);
343 AIMODE_HALF_FULL
= 1,
348 enum ni_common_subdevices
{
354 NI_CALIBRATION_SUBDEV
,
357 NI_CS5529_CALIBRATION_SUBDEV
,
365 static inline unsigned NI_GPCT_SUBDEV(unsigned counter_index
)
367 switch (counter_index
) {
369 return NI_GPCT0_SUBDEV
;
372 return NI_GPCT1_SUBDEV
;
378 return NI_GPCT0_SUBDEV
;
381 enum timebase_nanoseconds
{
383 TIMEBASE_2_NS
= 10000
386 #define SERIAL_DISABLED 0
387 #define SERIAL_600NS 600
388 #define SERIAL_1_2US 1200
389 #define SERIAL_10US 10000
391 static const int num_adc_stages_611x
= 3;
393 static void handle_a_interrupt(struct comedi_device
*dev
, unsigned short status
,
394 unsigned ai_mite_status
);
395 static void handle_b_interrupt(struct comedi_device
*dev
, unsigned short status
,
396 unsigned ao_mite_status
);
397 static void get_last_sample_611x(struct comedi_device
*dev
);
398 static void get_last_sample_6143(struct comedi_device
*dev
);
400 static inline void ni_set_bitfield(struct comedi_device
*dev
, int reg
,
401 unsigned bit_mask
, unsigned bit_values
)
403 struct ni_private
*devpriv
= dev
->private;
406 spin_lock_irqsave(&devpriv
->soft_reg_copy_lock
, flags
);
408 case Interrupt_A_Enable_Register
:
409 devpriv
->int_a_enable_reg
&= ~bit_mask
;
410 devpriv
->int_a_enable_reg
|= bit_values
& bit_mask
;
411 devpriv
->stc_writew(dev
, devpriv
->int_a_enable_reg
,
412 Interrupt_A_Enable_Register
);
414 case Interrupt_B_Enable_Register
:
415 devpriv
->int_b_enable_reg
&= ~bit_mask
;
416 devpriv
->int_b_enable_reg
|= bit_values
& bit_mask
;
417 devpriv
->stc_writew(dev
, devpriv
->int_b_enable_reg
,
418 Interrupt_B_Enable_Register
);
420 case IO_Bidirection_Pin_Register
:
421 devpriv
->io_bidirection_pin_reg
&= ~bit_mask
;
422 devpriv
->io_bidirection_pin_reg
|= bit_values
& bit_mask
;
423 devpriv
->stc_writew(dev
, devpriv
->io_bidirection_pin_reg
,
424 IO_Bidirection_Pin_Register
);
427 devpriv
->ai_ao_select_reg
&= ~bit_mask
;
428 devpriv
->ai_ao_select_reg
|= bit_values
& bit_mask
;
429 ni_writeb(devpriv
->ai_ao_select_reg
, AI_AO_Select
);
432 devpriv
->g0_g1_select_reg
&= ~bit_mask
;
433 devpriv
->g0_g1_select_reg
|= bit_values
& bit_mask
;
434 ni_writeb(devpriv
->g0_g1_select_reg
, G0_G1_Select
);
437 printk("Warning %s() called with invalid register\n", __func__
);
438 printk("reg is %d\n", reg
);
442 spin_unlock_irqrestore(&devpriv
->soft_reg_copy_lock
, flags
);
446 static int ni_ai_drain_dma(struct comedi_device
*dev
);
448 /* DMA channel setup */
450 /* negative channel means no channel */
451 static inline void ni_set_ai_dma_channel(struct comedi_device
*dev
, int channel
)
457 (ni_stc_dma_channel_select_bitfield(channel
) <<
458 AI_DMA_Select_Shift
) & AI_DMA_Select_Mask
;
462 ni_set_bitfield(dev
, AI_AO_Select
, AI_DMA_Select_Mask
, bitfield
);
465 /* negative channel means no channel */
466 static inline void ni_set_ao_dma_channel(struct comedi_device
*dev
, int channel
)
472 (ni_stc_dma_channel_select_bitfield(channel
) <<
473 AO_DMA_Select_Shift
) & AO_DMA_Select_Mask
;
477 ni_set_bitfield(dev
, AI_AO_Select
, AO_DMA_Select_Mask
, bitfield
);
480 /* negative mite_channel means no channel */
481 static inline void ni_set_gpct_dma_channel(struct comedi_device
*dev
,
487 if (mite_channel
>= 0) {
488 bitfield
= GPCT_DMA_Select_Bits(gpct_index
, mite_channel
);
492 ni_set_bitfield(dev
, G0_G1_Select
, GPCT_DMA_Select_Mask(gpct_index
),
496 /* negative mite_channel means no channel */
497 static inline void ni_set_cdo_dma_channel(struct comedi_device
*dev
,
500 struct ni_private
*devpriv
= dev
->private;
503 spin_lock_irqsave(&devpriv
->soft_reg_copy_lock
, flags
);
504 devpriv
->cdio_dma_select_reg
&= ~CDO_DMA_Select_Mask
;
505 if (mite_channel
>= 0) {
506 /*XXX just guessing ni_stc_dma_channel_select_bitfield() returns the right bits,
507 under the assumption the cdio dma selection works just like ai/ao/gpct.
508 Definitely works for dma channels 0 and 1. */
509 devpriv
->cdio_dma_select_reg
|=
510 (ni_stc_dma_channel_select_bitfield(mite_channel
) <<
511 CDO_DMA_Select_Shift
) & CDO_DMA_Select_Mask
;
513 ni_writeb(devpriv
->cdio_dma_select_reg
, M_Offset_CDIO_DMA_Select
);
515 spin_unlock_irqrestore(&devpriv
->soft_reg_copy_lock
, flags
);
518 static int ni_request_ai_mite_channel(struct comedi_device
*dev
)
520 struct ni_private
*devpriv
= dev
->private;
523 spin_lock_irqsave(&devpriv
->mite_channel_lock
, flags
);
524 BUG_ON(devpriv
->ai_mite_chan
);
525 devpriv
->ai_mite_chan
=
526 mite_request_channel(devpriv
->mite
, devpriv
->ai_mite_ring
);
527 if (devpriv
->ai_mite_chan
== NULL
) {
528 spin_unlock_irqrestore(&devpriv
->mite_channel_lock
, flags
);
530 "failed to reserve mite dma channel for analog input.");
533 devpriv
->ai_mite_chan
->dir
= COMEDI_INPUT
;
534 ni_set_ai_dma_channel(dev
, devpriv
->ai_mite_chan
->channel
);
535 spin_unlock_irqrestore(&devpriv
->mite_channel_lock
, flags
);
539 static int ni_request_ao_mite_channel(struct comedi_device
*dev
)
541 struct ni_private
*devpriv
= dev
->private;
544 spin_lock_irqsave(&devpriv
->mite_channel_lock
, flags
);
545 BUG_ON(devpriv
->ao_mite_chan
);
546 devpriv
->ao_mite_chan
=
547 mite_request_channel(devpriv
->mite
, devpriv
->ao_mite_ring
);
548 if (devpriv
->ao_mite_chan
== NULL
) {
549 spin_unlock_irqrestore(&devpriv
->mite_channel_lock
, flags
);
551 "failed to reserve mite dma channel for analog outut.");
554 devpriv
->ao_mite_chan
->dir
= COMEDI_OUTPUT
;
555 ni_set_ao_dma_channel(dev
, devpriv
->ao_mite_chan
->channel
);
556 spin_unlock_irqrestore(&devpriv
->mite_channel_lock
, flags
);
560 static int ni_request_gpct_mite_channel(struct comedi_device
*dev
,
562 enum comedi_io_direction direction
)
564 struct ni_private
*devpriv
= dev
->private;
566 struct mite_channel
*mite_chan
;
568 BUG_ON(gpct_index
>= NUM_GPCT
);
569 spin_lock_irqsave(&devpriv
->mite_channel_lock
, flags
);
570 BUG_ON(devpriv
->counter_dev
->counters
[gpct_index
].mite_chan
);
572 mite_request_channel(devpriv
->mite
,
573 devpriv
->gpct_mite_ring
[gpct_index
]);
574 if (mite_chan
== NULL
) {
575 spin_unlock_irqrestore(&devpriv
->mite_channel_lock
, flags
);
577 "failed to reserve mite dma channel for counter.");
580 mite_chan
->dir
= direction
;
581 ni_tio_set_mite_channel(&devpriv
->counter_dev
->counters
[gpct_index
],
583 ni_set_gpct_dma_channel(dev
, gpct_index
, mite_chan
->channel
);
584 spin_unlock_irqrestore(&devpriv
->mite_channel_lock
, flags
);
590 static int ni_request_cdo_mite_channel(struct comedi_device
*dev
)
593 struct ni_private
*devpriv
= dev
->private;
596 spin_lock_irqsave(&devpriv
->mite_channel_lock
, flags
);
597 BUG_ON(devpriv
->cdo_mite_chan
);
598 devpriv
->cdo_mite_chan
=
599 mite_request_channel(devpriv
->mite
, devpriv
->cdo_mite_ring
);
600 if (devpriv
->cdo_mite_chan
== NULL
) {
601 spin_unlock_irqrestore(&devpriv
->mite_channel_lock
, flags
);
603 "failed to reserve mite dma channel for correlated digital outut.");
606 devpriv
->cdo_mite_chan
->dir
= COMEDI_OUTPUT
;
607 ni_set_cdo_dma_channel(dev
, devpriv
->cdo_mite_chan
->channel
);
608 spin_unlock_irqrestore(&devpriv
->mite_channel_lock
, flags
);
613 static void ni_release_ai_mite_channel(struct comedi_device
*dev
)
616 struct ni_private
*devpriv
= dev
->private;
619 spin_lock_irqsave(&devpriv
->mite_channel_lock
, flags
);
620 if (devpriv
->ai_mite_chan
) {
621 ni_set_ai_dma_channel(dev
, -1);
622 mite_release_channel(devpriv
->ai_mite_chan
);
623 devpriv
->ai_mite_chan
= NULL
;
625 spin_unlock_irqrestore(&devpriv
->mite_channel_lock
, flags
);
629 static void ni_release_ao_mite_channel(struct comedi_device
*dev
)
632 struct ni_private
*devpriv
= dev
->private;
635 spin_lock_irqsave(&devpriv
->mite_channel_lock
, flags
);
636 if (devpriv
->ao_mite_chan
) {
637 ni_set_ao_dma_channel(dev
, -1);
638 mite_release_channel(devpriv
->ao_mite_chan
);
639 devpriv
->ao_mite_chan
= NULL
;
641 spin_unlock_irqrestore(&devpriv
->mite_channel_lock
, flags
);
646 static void ni_release_gpct_mite_channel(struct comedi_device
*dev
,
649 struct ni_private
*devpriv
= dev
->private;
652 BUG_ON(gpct_index
>= NUM_GPCT
);
653 spin_lock_irqsave(&devpriv
->mite_channel_lock
, flags
);
654 if (devpriv
->counter_dev
->counters
[gpct_index
].mite_chan
) {
655 struct mite_channel
*mite_chan
=
656 devpriv
->counter_dev
->counters
[gpct_index
].mite_chan
;
658 ni_set_gpct_dma_channel(dev
, gpct_index
, -1);
659 ni_tio_set_mite_channel(&devpriv
->
660 counter_dev
->counters
[gpct_index
],
662 mite_release_channel(mite_chan
);
664 spin_unlock_irqrestore(&devpriv
->mite_channel_lock
, flags
);
668 static void ni_release_cdo_mite_channel(struct comedi_device
*dev
)
671 struct ni_private
*devpriv
= dev
->private;
674 spin_lock_irqsave(&devpriv
->mite_channel_lock
, flags
);
675 if (devpriv
->cdo_mite_chan
) {
676 ni_set_cdo_dma_channel(dev
, -1);
677 mite_release_channel(devpriv
->cdo_mite_chan
);
678 devpriv
->cdo_mite_chan
= NULL
;
680 spin_unlock_irqrestore(&devpriv
->mite_channel_lock
, flags
);
684 /* e-series boards use the second irq signals to generate dma requests for their counters */
686 static void ni_e_series_enable_second_irq(struct comedi_device
*dev
,
687 unsigned gpct_index
, short enable
)
689 const struct ni_board_struct
*board
= comedi_board(dev
);
690 struct ni_private
*devpriv
= dev
->private;
692 if (board
->reg_type
& ni_reg_m_series_mask
)
694 switch (gpct_index
) {
697 devpriv
->stc_writew(dev
, G0_Gate_Second_Irq_Enable
,
698 Second_IRQ_A_Enable_Register
);
700 devpriv
->stc_writew(dev
, 0,
701 Second_IRQ_A_Enable_Register
);
706 devpriv
->stc_writew(dev
, G1_Gate_Second_Irq_Enable
,
707 Second_IRQ_B_Enable_Register
);
709 devpriv
->stc_writew(dev
, 0,
710 Second_IRQ_B_Enable_Register
);
720 static void ni_clear_ai_fifo(struct comedi_device
*dev
)
722 const struct ni_board_struct
*board
= comedi_board(dev
);
723 struct ni_private
*devpriv
= dev
->private;
725 if (board
->reg_type
== ni_reg_6143
) {
726 /* Flush the 6143 data FIFO */
727 ni_writel(0x10, AIFIFO_Control_6143
); /* Flush fifo */
728 ni_writel(0x00, AIFIFO_Control_6143
); /* Flush fifo */
729 while (ni_readl(AIFIFO_Status_6143
) & 0x10) ; /* Wait for complete */
731 devpriv
->stc_writew(dev
, 1, ADC_FIFO_Clear
);
732 if (board
->reg_type
== ni_reg_625x
) {
733 ni_writeb(0, M_Offset_Static_AI_Control(0));
734 ni_writeb(1, M_Offset_Static_AI_Control(0));
736 /* the NI example code does 3 convert pulses for 625x boards,
737 but that appears to be wrong in practice. */
738 devpriv
->stc_writew(dev
, AI_CONVERT_Pulse
,
739 AI_Command_1_Register
);
740 devpriv
->stc_writew(dev
, AI_CONVERT_Pulse
,
741 AI_Command_1_Register
);
742 devpriv
->stc_writew(dev
, AI_CONVERT_Pulse
,
743 AI_Command_1_Register
);
749 static void win_out2(struct comedi_device
*dev
, uint32_t data
, int reg
)
751 struct ni_private
*devpriv
= dev
->private;
753 devpriv
->stc_writew(dev
, data
>> 16, reg
);
754 devpriv
->stc_writew(dev
, data
& 0xffff, reg
+ 1);
757 static uint32_t win_in2(struct comedi_device
*dev
, int reg
)
759 struct ni_private
*devpriv
= dev
->private;
762 bits
= devpriv
->stc_readw(dev
, reg
) << 16;
763 bits
|= devpriv
->stc_readw(dev
, reg
+ 1);
767 #define ao_win_out(data, addr) ni_ao_win_outw(dev, data, addr)
768 static inline void ni_ao_win_outw(struct comedi_device
*dev
, uint16_t data
,
771 struct ni_private
*devpriv
= dev
->private;
774 spin_lock_irqsave(&devpriv
->window_lock
, flags
);
775 ni_writew(addr
, AO_Window_Address_611x
);
776 ni_writew(data
, AO_Window_Data_611x
);
777 spin_unlock_irqrestore(&devpriv
->window_lock
, flags
);
780 static inline void ni_ao_win_outl(struct comedi_device
*dev
, uint32_t data
,
783 struct ni_private
*devpriv
= dev
->private;
786 spin_lock_irqsave(&devpriv
->window_lock
, flags
);
787 ni_writew(addr
, AO_Window_Address_611x
);
788 ni_writel(data
, AO_Window_Data_611x
);
789 spin_unlock_irqrestore(&devpriv
->window_lock
, flags
);
792 static inline unsigned short ni_ao_win_inw(struct comedi_device
*dev
, int addr
)
794 struct ni_private
*devpriv
= dev
->private;
798 spin_lock_irqsave(&devpriv
->window_lock
, flags
);
799 ni_writew(addr
, AO_Window_Address_611x
);
800 data
= ni_readw(AO_Window_Data_611x
);
801 spin_unlock_irqrestore(&devpriv
->window_lock
, flags
);
805 /* ni_set_bits( ) allows different parts of the ni_mio_common driver to
806 * share registers (such as Interrupt_A_Register) without interfering with
809 * NOTE: the switch/case statements are optimized out for a constant argument
810 * so this is actually quite fast--- If you must wrap another function around this
811 * make it inline to avoid a large speed penalty.
813 * value should only be 1 or 0.
815 static inline void ni_set_bits(struct comedi_device
*dev
, int reg
,
816 unsigned bits
, unsigned value
)
824 ni_set_bitfield(dev
, reg
, bits
, bit_values
);
827 static irqreturn_t
ni_E_interrupt(int irq
, void *d
)
829 struct comedi_device
*dev
= d
;
830 struct ni_private
*devpriv
= dev
->private;
831 unsigned short a_status
;
832 unsigned short b_status
;
833 unsigned int ai_mite_status
= 0;
834 unsigned int ao_mite_status
= 0;
837 struct mite_struct
*mite
= devpriv
->mite
;
842 smp_mb(); /* make sure dev->attached is checked before handler does anything else. */
844 /* lock to avoid race with comedi_poll */
845 spin_lock_irqsave(&dev
->spinlock
, flags
);
846 a_status
= devpriv
->stc_readw(dev
, AI_Status_1_Register
);
847 b_status
= devpriv
->stc_readw(dev
, AO_Status_1_Register
);
850 unsigned long flags_too
;
852 spin_lock_irqsave(&devpriv
->mite_channel_lock
, flags_too
);
853 if (devpriv
->ai_mite_chan
) {
854 ai_mite_status
= mite_get_status(devpriv
->ai_mite_chan
);
855 if (ai_mite_status
& CHSR_LINKC
)
857 devpriv
->mite
->mite_io_addr
+
859 ai_mite_chan
->channel
));
861 if (devpriv
->ao_mite_chan
) {
862 ao_mite_status
= mite_get_status(devpriv
->ao_mite_chan
);
863 if (ao_mite_status
& CHSR_LINKC
)
867 ao_mite_chan
->channel
));
869 spin_unlock_irqrestore(&devpriv
->mite_channel_lock
, flags_too
);
872 ack_a_interrupt(dev
, a_status
);
873 ack_b_interrupt(dev
, b_status
);
874 if ((a_status
& Interrupt_A_St
) || (ai_mite_status
& CHSR_INT
))
875 handle_a_interrupt(dev
, a_status
, ai_mite_status
);
876 if ((b_status
& Interrupt_B_St
) || (ao_mite_status
& CHSR_INT
))
877 handle_b_interrupt(dev
, b_status
, ao_mite_status
);
878 handle_gpct_interrupt(dev
, 0);
879 handle_gpct_interrupt(dev
, 1);
880 handle_cdio_interrupt(dev
);
882 spin_unlock_irqrestore(&dev
->spinlock
, flags
);
887 static void ni_sync_ai_dma(struct comedi_device
*dev
)
889 struct ni_private
*devpriv
= dev
->private;
890 struct comedi_subdevice
*s
= &dev
->subdevices
[NI_AI_SUBDEV
];
893 spin_lock_irqsave(&devpriv
->mite_channel_lock
, flags
);
894 if (devpriv
->ai_mite_chan
)
895 mite_sync_input_dma(devpriv
->ai_mite_chan
, s
->async
);
896 spin_unlock_irqrestore(&devpriv
->mite_channel_lock
, flags
);
899 static void mite_handle_b_linkc(struct mite_struct
*mite
,
900 struct comedi_device
*dev
)
902 struct ni_private
*devpriv
= dev
->private;
903 struct comedi_subdevice
*s
= &dev
->subdevices
[NI_AO_SUBDEV
];
906 spin_lock_irqsave(&devpriv
->mite_channel_lock
, flags
);
907 if (devpriv
->ao_mite_chan
) {
908 mite_sync_output_dma(devpriv
->ao_mite_chan
, s
->async
);
910 spin_unlock_irqrestore(&devpriv
->mite_channel_lock
, flags
);
913 static int ni_ao_wait_for_dma_load(struct comedi_device
*dev
)
915 struct ni_private
*devpriv
= dev
->private;
916 static const int timeout
= 10000;
918 for (i
= 0; i
< timeout
; i
++) {
919 unsigned short b_status
;
921 b_status
= devpriv
->stc_readw(dev
, AO_Status_1_Register
);
922 if (b_status
& AO_FIFO_Half_Full_St
)
924 /* if we poll too often, the pci bus activity seems
925 to slow the dma transfer down */
929 comedi_error(dev
, "timed out waiting for dma load");
936 static void ni_handle_eos(struct comedi_device
*dev
, struct comedi_subdevice
*s
)
938 struct ni_private
*devpriv
= dev
->private;
940 if (devpriv
->aimode
== AIMODE_SCAN
) {
942 static const int timeout
= 10;
945 for (i
= 0; i
< timeout
; i
++) {
947 if ((s
->async
->events
& COMEDI_CB_EOS
))
952 ni_handle_fifo_dregs(dev
);
953 s
->async
->events
|= COMEDI_CB_EOS
;
956 /* handle special case of single scan using AI_End_On_End_Of_Scan */
957 if ((devpriv
->ai_cmd2
& AI_End_On_End_Of_Scan
)) {
958 shutdown_ai_command(dev
);
962 static void shutdown_ai_command(struct comedi_device
*dev
)
964 struct comedi_subdevice
*s
= &dev
->subdevices
[NI_AI_SUBDEV
];
967 ni_ai_drain_dma(dev
);
969 ni_handle_fifo_dregs(dev
);
970 get_last_sample_611x(dev
);
971 get_last_sample_6143(dev
);
973 s
->async
->events
|= COMEDI_CB_EOA
;
976 static void ni_event(struct comedi_device
*dev
, struct comedi_subdevice
*s
)
979 async
->events
& (COMEDI_CB_ERROR
| COMEDI_CB_OVERFLOW
|
988 case NI_GPCT0_SUBDEV
:
989 case NI_GPCT1_SUBDEV
:
990 ni_gpct_cancel(dev
, s
);
993 ni_cdio_cancel(dev
, s
);
999 comedi_event(dev
, s
);
1002 static void handle_gpct_interrupt(struct comedi_device
*dev
,
1003 unsigned short counter_index
)
1006 struct ni_private
*devpriv
= dev
->private;
1007 struct comedi_subdevice
*s
;
1009 s
= &dev
->subdevices
[NI_GPCT_SUBDEV(counter_index
)];
1011 ni_tio_handle_interrupt(&devpriv
->counter_dev
->counters
[counter_index
],
1013 if (s
->async
->events
)
1018 static void ack_a_interrupt(struct comedi_device
*dev
, unsigned short a_status
)
1020 struct ni_private
*devpriv
= dev
->private;
1021 unsigned short ack
= 0;
1023 if (a_status
& AI_SC_TC_St
) {
1024 ack
|= AI_SC_TC_Interrupt_Ack
;
1026 if (a_status
& AI_START1_St
) {
1027 ack
|= AI_START1_Interrupt_Ack
;
1029 if (a_status
& AI_START_St
) {
1030 ack
|= AI_START_Interrupt_Ack
;
1032 if (a_status
& AI_STOP_St
) {
1033 /* not sure why we used to ack the START here also, instead of doing it independently. Frank Hess 2007-07-06 */
1034 ack
|= AI_STOP_Interrupt_Ack
/*| AI_START_Interrupt_Ack */ ;
1037 devpriv
->stc_writew(dev
, ack
, Interrupt_A_Ack_Register
);
1040 static void handle_a_interrupt(struct comedi_device
*dev
, unsigned short status
,
1041 unsigned ai_mite_status
)
1043 struct ni_private
*devpriv
= dev
->private;
1044 struct comedi_subdevice
*s
= &dev
->subdevices
[NI_AI_SUBDEV
];
1046 /* 67xx boards don't have ai subdevice, but their gpct0 might generate an a interrupt */
1047 if (s
->type
== COMEDI_SUBD_UNUSED
)
1050 #ifdef DEBUG_INTERRUPT
1052 ("ni_mio_common: interrupt: a_status=%04x ai_mite_status=%08x\n",
1053 status
, ai_mite_status
);
1054 ni_mio_print_status_a(status
);
1057 if (ai_mite_status
& CHSR_LINKC
) {
1058 ni_sync_ai_dma(dev
);
1061 if (ai_mite_status
& ~(CHSR_INT
| CHSR_LINKC
| CHSR_DONE
| CHSR_MRDY
|
1062 CHSR_DRDY
| CHSR_DRQ1
| CHSR_DRQ0
| CHSR_ERROR
|
1063 CHSR_SABORT
| CHSR_XFERR
| CHSR_LxERR_mask
)) {
1065 ("unknown mite interrupt, ack! (ai_mite_status=%08x)\n",
1067 /* mite_print_chsr(ai_mite_status); */
1068 s
->async
->events
|= COMEDI_CB_ERROR
| COMEDI_CB_EOA
;
1069 /* disable_irq(dev->irq); */
1073 /* test for all uncommon interrupt events at the same time */
1074 if (status
& (AI_Overrun_St
| AI_Overflow_St
| AI_SC_TC_Error_St
|
1075 AI_SC_TC_St
| AI_START1_St
)) {
1076 if (status
== 0xffff) {
1078 ("ni_mio_common: a_status=0xffff. Card removed?\n");
1079 /* we probably aren't even running a command now,
1080 * so it's a good idea to be careful. */
1081 if (comedi_is_subdevice_running(s
)) {
1083 COMEDI_CB_ERROR
| COMEDI_CB_EOA
;
1088 if (status
& (AI_Overrun_St
| AI_Overflow_St
|
1089 AI_SC_TC_Error_St
)) {
1090 printk("ni_mio_common: ai error a_status=%04x\n",
1092 ni_mio_print_status_a(status
);
1094 shutdown_ai_command(dev
);
1096 s
->async
->events
|= COMEDI_CB_ERROR
;
1097 if (status
& (AI_Overrun_St
| AI_Overflow_St
))
1098 s
->async
->events
|= COMEDI_CB_OVERFLOW
;
1104 if (status
& AI_SC_TC_St
) {
1105 #ifdef DEBUG_INTERRUPT
1106 printk("ni_mio_common: SC_TC interrupt\n");
1108 if (!devpriv
->ai_continuous
) {
1109 shutdown_ai_command(dev
);
1114 if (status
& AI_FIFO_Half_Full_St
) {
1116 static const int timeout
= 10;
1117 /* pcmcia cards (at least 6036) seem to stop producing interrupts if we
1118 *fail to get the fifo less than half full, so loop to be sure.*/
1119 for (i
= 0; i
< timeout
; ++i
) {
1120 ni_handle_fifo_half_full(dev
);
1121 if ((devpriv
->stc_readw(dev
,
1122 AI_Status_1_Register
) &
1123 AI_FIFO_Half_Full_St
) == 0)
1127 #endif /* !PCIDMA */
1129 if ((status
& AI_STOP_St
)) {
1130 ni_handle_eos(dev
, s
);
1135 #ifdef DEBUG_INTERRUPT
1136 status
= devpriv
->stc_readw(dev
, AI_Status_1_Register
);
1137 if (status
& Interrupt_A_St
) {
1139 ("handle_a_interrupt: didn't clear interrupt? status=0x%x\n",
1145 static void ack_b_interrupt(struct comedi_device
*dev
, unsigned short b_status
)
1147 struct ni_private
*devpriv
= dev
->private;
1148 unsigned short ack
= 0;
1150 if (b_status
& AO_BC_TC_St
) {
1151 ack
|= AO_BC_TC_Interrupt_Ack
;
1153 if (b_status
& AO_Overrun_St
) {
1154 ack
|= AO_Error_Interrupt_Ack
;
1156 if (b_status
& AO_START_St
) {
1157 ack
|= AO_START_Interrupt_Ack
;
1159 if (b_status
& AO_START1_St
) {
1160 ack
|= AO_START1_Interrupt_Ack
;
1162 if (b_status
& AO_UC_TC_St
) {
1163 ack
|= AO_UC_TC_Interrupt_Ack
;
1165 if (b_status
& AO_UI2_TC_St
) {
1166 ack
|= AO_UI2_TC_Interrupt_Ack
;
1168 if (b_status
& AO_UPDATE_St
) {
1169 ack
|= AO_UPDATE_Interrupt_Ack
;
1172 devpriv
->stc_writew(dev
, ack
, Interrupt_B_Ack_Register
);
1175 static void handle_b_interrupt(struct comedi_device
*dev
,
1176 unsigned short b_status
, unsigned ao_mite_status
)
1178 struct ni_private
*devpriv
= dev
->private;
1179 struct comedi_subdevice
*s
= &dev
->subdevices
[NI_AO_SUBDEV
];
1180 /* unsigned short ack=0; */
1182 #ifdef DEBUG_INTERRUPT
1183 printk("ni_mio_common: interrupt: b_status=%04x m1_status=%08x\n",
1184 b_status
, ao_mite_status
);
1185 ni_mio_print_status_b(b_status
);
1189 /* Currently, mite.c requires us to handle LINKC */
1190 if (ao_mite_status
& CHSR_LINKC
) {
1191 mite_handle_b_linkc(devpriv
->mite
, dev
);
1194 if (ao_mite_status
& ~(CHSR_INT
| CHSR_LINKC
| CHSR_DONE
| CHSR_MRDY
|
1195 CHSR_DRDY
| CHSR_DRQ1
| CHSR_DRQ0
| CHSR_ERROR
|
1196 CHSR_SABORT
| CHSR_XFERR
| CHSR_LxERR_mask
)) {
1198 ("unknown mite interrupt, ack! (ao_mite_status=%08x)\n",
1200 /* mite_print_chsr(ao_mite_status); */
1201 s
->async
->events
|= COMEDI_CB_EOA
| COMEDI_CB_ERROR
;
1205 if (b_status
== 0xffff)
1207 if (b_status
& AO_Overrun_St
) {
1209 ("ni_mio_common: AO FIFO underrun status=0x%04x status2=0x%04x\n",
1210 b_status
, devpriv
->stc_readw(dev
, AO_Status_2_Register
));
1211 s
->async
->events
|= COMEDI_CB_OVERFLOW
;
1214 if (b_status
& AO_BC_TC_St
) {
1216 ("ni_mio_common: AO BC_TC status=0x%04x status2=0x%04x\n",
1217 b_status
, devpriv
->stc_readw(dev
, AO_Status_2_Register
));
1218 s
->async
->events
|= COMEDI_CB_EOA
;
1221 if (b_status
& AO_FIFO_Request_St
) {
1224 ret
= ni_ao_fifo_half_empty(dev
, s
);
1226 printk("ni_mio_common: AO buffer underrun\n");
1227 ni_set_bits(dev
, Interrupt_B_Enable_Register
,
1228 AO_FIFO_Interrupt_Enable
|
1229 AO_Error_Interrupt_Enable
, 0);
1230 s
->async
->events
|= COMEDI_CB_OVERFLOW
;
1238 #ifdef DEBUG_STATUS_A
1239 static const char *const status_a_strings
[] = {
1240 "passthru0", "fifo", "G0_gate", "G0_TC",
1241 "stop", "start", "sc_tc", "start1",
1242 "start2", "sc_tc_error", "overflow", "overrun",
1243 "fifo_empty", "fifo_half_full", "fifo_full", "interrupt_a"
1246 static void ni_mio_print_status_a(int status
)
1250 printk("A status:");
1251 for (i
= 15; i
>= 0; i
--) {
1252 if (status
& (1 << i
)) {
1253 printk(" %s", status_a_strings
[i
]);
1260 #ifdef DEBUG_STATUS_B
1261 static const char *const status_b_strings
[] = {
1262 "passthru1", "fifo", "G1_gate", "G1_TC",
1263 "UI2_TC", "UPDATE", "UC_TC", "BC_TC",
1264 "start1", "overrun", "start", "bc_tc_error",
1265 "fifo_empty", "fifo_half_full", "fifo_full", "interrupt_b"
1268 static void ni_mio_print_status_b(int status
)
1272 printk("B status:");
1273 for (i
= 15; i
>= 0; i
--) {
1274 if (status
& (1 << i
)) {
1275 printk(" %s", status_b_strings
[i
]);
1284 static void ni_ao_fifo_load(struct comedi_device
*dev
,
1285 struct comedi_subdevice
*s
, int n
)
1287 const struct ni_board_struct
*board
= comedi_board(dev
);
1288 struct comedi_async
*async
= s
->async
;
1289 struct comedi_cmd
*cmd
= &async
->cmd
;
1297 chan
= async
->cur_chan
;
1298 for (i
= 0; i
< n
; i
++) {
1299 err
&= comedi_buf_get(async
, &d
);
1303 range
= CR_RANGE(cmd
->chanlist
[chan
]);
1305 if (board
->reg_type
& ni_reg_6xxx_mask
) {
1306 packed_data
= d
& 0xffff;
1307 /* 6711 only has 16 bit wide ao fifo */
1308 if (board
->reg_type
!= ni_reg_6711
) {
1309 err
&= comedi_buf_get(async
, &d
);
1314 packed_data
|= (d
<< 16) & 0xffff0000;
1316 ni_writel(packed_data
, DAC_FIFO_Data_611x
);
1318 ni_writew(d
, DAC_FIFO_Data
);
1321 chan
%= cmd
->chanlist_len
;
1323 async
->cur_chan
= chan
;
1325 async
->events
|= COMEDI_CB_OVERFLOW
;
1330 * There's a small problem if the FIFO gets really low and we
1331 * don't have the data to fill it. Basically, if after we fill
1332 * the FIFO with all the data available, the FIFO is _still_
1333 * less than half full, we never clear the interrupt. If the
1334 * IRQ is in edge mode, we never get another interrupt, because
1335 * this one wasn't cleared. If in level mode, we get flooded
1336 * with interrupts that we can't fulfill, because nothing ever
1337 * gets put into the buffer.
1339 * This kind of situation is recoverable, but it is easier to
1340 * just pretend we had a FIFO underrun, since there is a good
1341 * chance it will happen anyway. This is _not_ the case for
1342 * RT code, as RT code might purposely be running close to the
1343 * metal. Needs to be fixed eventually.
1345 static int ni_ao_fifo_half_empty(struct comedi_device
*dev
,
1346 struct comedi_subdevice
*s
)
1348 const struct ni_board_struct
*board
= comedi_board(dev
);
1351 n
= comedi_buf_read_n_available(s
->async
);
1353 s
->async
->events
|= COMEDI_CB_OVERFLOW
;
1358 if (n
> board
->ao_fifo_depth
/ 2)
1359 n
= board
->ao_fifo_depth
/ 2;
1361 ni_ao_fifo_load(dev
, s
, n
);
1363 s
->async
->events
|= COMEDI_CB_BLOCK
;
1368 static int ni_ao_prep_fifo(struct comedi_device
*dev
,
1369 struct comedi_subdevice
*s
)
1371 const struct ni_board_struct
*board
= comedi_board(dev
);
1372 struct ni_private
*devpriv
= dev
->private;
1376 devpriv
->stc_writew(dev
, 1, DAC_FIFO_Clear
);
1377 if (board
->reg_type
& ni_reg_6xxx_mask
)
1378 ni_ao_win_outl(dev
, 0x6, AO_FIFO_Offset_Load_611x
);
1380 /* load some data */
1381 n
= comedi_buf_read_n_available(s
->async
);
1386 if (n
> board
->ao_fifo_depth
)
1387 n
= board
->ao_fifo_depth
;
1389 ni_ao_fifo_load(dev
, s
, n
);
1394 static void ni_ai_fifo_read(struct comedi_device
*dev
,
1395 struct comedi_subdevice
*s
, int n
)
1397 const struct ni_board_struct
*board
= comedi_board(dev
);
1398 struct ni_private
*devpriv
= dev
->private;
1399 struct comedi_async
*async
= s
->async
;
1402 if (board
->reg_type
== ni_reg_611x
) {
1406 for (i
= 0; i
< n
/ 2; i
++) {
1407 dl
= ni_readl(ADC_FIFO_Data_611x
);
1408 /* This may get the hi/lo data in the wrong order */
1409 data
[0] = (dl
>> 16) & 0xffff;
1410 data
[1] = dl
& 0xffff;
1411 cfc_write_array_to_buffer(s
, data
, sizeof(data
));
1413 /* Check if there's a single sample stuck in the FIFO */
1415 dl
= ni_readl(ADC_FIFO_Data_611x
);
1416 data
[0] = dl
& 0xffff;
1417 cfc_write_to_buffer(s
, data
[0]);
1419 } else if (board
->reg_type
== ni_reg_6143
) {
1423 /* This just reads the FIFO assuming the data is present, no checks on the FIFO status are performed */
1424 for (i
= 0; i
< n
/ 2; i
++) {
1425 dl
= ni_readl(AIFIFO_Data_6143
);
1427 data
[0] = (dl
>> 16) & 0xffff;
1428 data
[1] = dl
& 0xffff;
1429 cfc_write_array_to_buffer(s
, data
, sizeof(data
));
1432 /* Assume there is a single sample stuck in the FIFO */
1433 ni_writel(0x01, AIFIFO_Control_6143
); /* Get stranded sample into FIFO */
1434 dl
= ni_readl(AIFIFO_Data_6143
);
1435 data
[0] = (dl
>> 16) & 0xffff;
1436 cfc_write_to_buffer(s
, data
[0]);
1439 if (n
> sizeof(devpriv
->ai_fifo_buffer
) /
1440 sizeof(devpriv
->ai_fifo_buffer
[0])) {
1441 comedi_error(dev
, "bug! ai_fifo_buffer too small");
1442 async
->events
|= COMEDI_CB_ERROR
;
1445 for (i
= 0; i
< n
; i
++) {
1446 devpriv
->ai_fifo_buffer
[i
] =
1447 ni_readw(ADC_FIFO_Data_Register
);
1449 cfc_write_array_to_buffer(s
, devpriv
->ai_fifo_buffer
,
1451 sizeof(devpriv
->ai_fifo_buffer
[0]));
1455 static void ni_handle_fifo_half_full(struct comedi_device
*dev
)
1457 const struct ni_board_struct
*board
= comedi_board(dev
);
1458 struct comedi_subdevice
*s
= &dev
->subdevices
[NI_AI_SUBDEV
];
1461 n
= board
->ai_fifo_depth
/ 2;
1463 ni_ai_fifo_read(dev
, s
, n
);
1468 static int ni_ai_drain_dma(struct comedi_device
*dev
)
1470 struct ni_private
*devpriv
= dev
->private;
1472 static const int timeout
= 10000;
1473 unsigned long flags
;
1476 spin_lock_irqsave(&devpriv
->mite_channel_lock
, flags
);
1477 if (devpriv
->ai_mite_chan
) {
1478 for (i
= 0; i
< timeout
; i
++) {
1479 if ((devpriv
->stc_readw(dev
,
1480 AI_Status_1_Register
) &
1482 && mite_bytes_in_transit(devpriv
->ai_mite_chan
) ==
1488 printk("ni_mio_common: wait for dma drain timed out\n");
1490 ("mite_bytes_in_transit=%i, AI_Status1_Register=0x%x\n",
1491 mite_bytes_in_transit(devpriv
->ai_mite_chan
),
1492 devpriv
->stc_readw(dev
, AI_Status_1_Register
));
1496 spin_unlock_irqrestore(&devpriv
->mite_channel_lock
, flags
);
1498 ni_sync_ai_dma(dev
);
1506 static void ni_handle_fifo_dregs(struct comedi_device
*dev
)
1508 const struct ni_board_struct
*board
= comedi_board(dev
);
1509 struct ni_private
*devpriv
= dev
->private;
1510 struct comedi_subdevice
*s
= &dev
->subdevices
[NI_AI_SUBDEV
];
1516 if (board
->reg_type
== ni_reg_611x
) {
1517 while ((devpriv
->stc_readw(dev
,
1518 AI_Status_1_Register
) &
1519 AI_FIFO_Empty_St
) == 0) {
1520 dl
= ni_readl(ADC_FIFO_Data_611x
);
1522 /* This may get the hi/lo data in the wrong order */
1523 data
[0] = (dl
>> 16);
1524 data
[1] = (dl
& 0xffff);
1525 cfc_write_array_to_buffer(s
, data
, sizeof(data
));
1527 } else if (board
->reg_type
== ni_reg_6143
) {
1529 while (ni_readl(AIFIFO_Status_6143
) & 0x04) {
1530 dl
= ni_readl(AIFIFO_Data_6143
);
1532 /* This may get the hi/lo data in the wrong order */
1533 data
[0] = (dl
>> 16);
1534 data
[1] = (dl
& 0xffff);
1535 cfc_write_array_to_buffer(s
, data
, sizeof(data
));
1538 /* Check if stranded sample is present */
1539 if (ni_readl(AIFIFO_Status_6143
) & 0x01) {
1540 ni_writel(0x01, AIFIFO_Control_6143
); /* Get stranded sample into FIFO */
1541 dl
= ni_readl(AIFIFO_Data_6143
);
1542 data
[0] = (dl
>> 16) & 0xffff;
1543 cfc_write_to_buffer(s
, data
[0]);
1548 devpriv
->stc_readw(dev
,
1549 AI_Status_1_Register
) & AI_FIFO_Empty_St
;
1550 while (fifo_empty
== 0) {
1553 sizeof(devpriv
->ai_fifo_buffer
) /
1554 sizeof(devpriv
->ai_fifo_buffer
[0]); i
++) {
1556 devpriv
->stc_readw(dev
,
1557 AI_Status_1_Register
) &
1561 devpriv
->ai_fifo_buffer
[i
] =
1562 ni_readw(ADC_FIFO_Data_Register
);
1564 cfc_write_array_to_buffer(s
, devpriv
->ai_fifo_buffer
,
1567 ai_fifo_buffer
[0]));
1572 static void get_last_sample_611x(struct comedi_device
*dev
)
1574 const struct ni_board_struct
*board
= comedi_board(dev
);
1575 struct ni_private
*devpriv __maybe_unused
= dev
->private;
1576 struct comedi_subdevice
*s
= &dev
->subdevices
[NI_AI_SUBDEV
];
1580 if (board
->reg_type
!= ni_reg_611x
)
1583 /* Check if there's a single sample stuck in the FIFO */
1584 if (ni_readb(XXX_Status
) & 0x80) {
1585 dl
= ni_readl(ADC_FIFO_Data_611x
);
1586 data
= (dl
& 0xffff);
1587 cfc_write_to_buffer(s
, data
);
1591 static void get_last_sample_6143(struct comedi_device
*dev
)
1593 const struct ni_board_struct
*board
= comedi_board(dev
);
1594 struct ni_private
*devpriv __maybe_unused
= dev
->private;
1595 struct comedi_subdevice
*s
= &dev
->subdevices
[NI_AI_SUBDEV
];
1599 if (board
->reg_type
!= ni_reg_6143
)
1602 /* Check if there's a single sample stuck in the FIFO */
1603 if (ni_readl(AIFIFO_Status_6143
) & 0x01) {
1604 ni_writel(0x01, AIFIFO_Control_6143
); /* Get stranded sample into FIFO */
1605 dl
= ni_readl(AIFIFO_Data_6143
);
1607 /* This may get the hi/lo data in the wrong order */
1608 data
= (dl
>> 16) & 0xffff;
1609 cfc_write_to_buffer(s
, data
);
1613 static void ni_ai_munge(struct comedi_device
*dev
, struct comedi_subdevice
*s
,
1614 void *data
, unsigned int num_bytes
,
1615 unsigned int chan_index
)
1617 struct ni_private
*devpriv
= dev
->private;
1618 struct comedi_async
*async
= s
->async
;
1620 unsigned int length
= num_bytes
/ bytes_per_sample(s
);
1621 short *array
= data
;
1622 unsigned int *larray
= data
;
1624 for (i
= 0; i
< length
; i
++) {
1626 if (s
->subdev_flags
& SDF_LSAMPL
)
1627 larray
[i
] = le32_to_cpu(larray
[i
]);
1629 array
[i
] = le16_to_cpu(array
[i
]);
1631 if (s
->subdev_flags
& SDF_LSAMPL
)
1632 larray
[i
] += devpriv
->ai_offset
[chan_index
];
1634 array
[i
] += devpriv
->ai_offset
[chan_index
];
1636 chan_index
%= async
->cmd
.chanlist_len
;
1642 static int ni_ai_setup_MITE_dma(struct comedi_device
*dev
)
1644 const struct ni_board_struct
*board
= comedi_board(dev
);
1645 struct ni_private
*devpriv
= dev
->private;
1646 struct comedi_subdevice
*s
= &dev
->subdevices
[NI_AI_SUBDEV
];
1648 unsigned long flags
;
1650 retval
= ni_request_ai_mite_channel(dev
);
1653 /* printk("comedi_debug: using mite channel %i for ai.\n", devpriv->ai_mite_chan->channel); */
1655 /* write alloc the entire buffer */
1656 comedi_buf_write_alloc(s
->async
, s
->async
->prealloc_bufsz
);
1658 spin_lock_irqsave(&devpriv
->mite_channel_lock
, flags
);
1659 if (devpriv
->ai_mite_chan
== NULL
) {
1660 spin_unlock_irqrestore(&devpriv
->mite_channel_lock
, flags
);
1664 switch (board
->reg_type
) {
1667 mite_prep_dma(devpriv
->ai_mite_chan
, 32, 16);
1670 mite_prep_dma(devpriv
->ai_mite_chan
, 32, 32);
1673 mite_prep_dma(devpriv
->ai_mite_chan
, 16, 16);
1677 mite_dma_arm(devpriv
->ai_mite_chan
);
1678 spin_unlock_irqrestore(&devpriv
->mite_channel_lock
, flags
);
1683 static int ni_ao_setup_MITE_dma(struct comedi_device
*dev
)
1685 const struct ni_board_struct
*board
= comedi_board(dev
);
1686 struct ni_private
*devpriv
= dev
->private;
1687 struct comedi_subdevice
*s
= &dev
->subdevices
[NI_AO_SUBDEV
];
1689 unsigned long flags
;
1691 retval
= ni_request_ao_mite_channel(dev
);
1695 /* read alloc the entire buffer */
1696 comedi_buf_read_alloc(s
->async
, s
->async
->prealloc_bufsz
);
1698 spin_lock_irqsave(&devpriv
->mite_channel_lock
, flags
);
1699 if (devpriv
->ao_mite_chan
) {
1700 if (board
->reg_type
& (ni_reg_611x
| ni_reg_6713
)) {
1701 mite_prep_dma(devpriv
->ao_mite_chan
, 32, 32);
1703 /* doing 32 instead of 16 bit wide transfers from memory
1704 makes the mite do 32 bit pci transfers, doubling pci bandwidth. */
1705 mite_prep_dma(devpriv
->ao_mite_chan
, 16, 32);
1707 mite_dma_arm(devpriv
->ao_mite_chan
);
1710 spin_unlock_irqrestore(&devpriv
->mite_channel_lock
, flags
);
1718 used for both cancel ioctl and board initialization
1720 this is pretty harsh for a cancel, but it works...
1723 static int ni_ai_reset(struct comedi_device
*dev
, struct comedi_subdevice
*s
)
1725 const struct ni_board_struct
*board
= comedi_board(dev
);
1726 struct ni_private
*devpriv
= dev
->private;
1728 ni_release_ai_mite_channel(dev
);
1729 /* ai configuration */
1730 devpriv
->stc_writew(dev
, AI_Configuration_Start
| AI_Reset
,
1731 Joint_Reset_Register
);
1733 ni_set_bits(dev
, Interrupt_A_Enable_Register
,
1734 AI_SC_TC_Interrupt_Enable
| AI_START1_Interrupt_Enable
|
1735 AI_START2_Interrupt_Enable
| AI_START_Interrupt_Enable
|
1736 AI_STOP_Interrupt_Enable
| AI_Error_Interrupt_Enable
|
1737 AI_FIFO_Interrupt_Enable
, 0);
1739 ni_clear_ai_fifo(dev
);
1741 if (board
->reg_type
!= ni_reg_6143
)
1742 ni_writeb(0, Misc_Command
);
1744 devpriv
->stc_writew(dev
, AI_Disarm
, AI_Command_1_Register
); /* reset pulses */
1745 devpriv
->stc_writew(dev
,
1746 AI_Start_Stop
| AI_Mode_1_Reserved
1747 /*| AI_Trigger_Once */ ,
1748 AI_Mode_1_Register
);
1749 devpriv
->stc_writew(dev
, 0x0000, AI_Mode_2_Register
);
1750 /* generate FIFO interrupts on non-empty */
1751 devpriv
->stc_writew(dev
, (0 << 6) | 0x0000, AI_Mode_3_Register
);
1752 if (board
->reg_type
== ni_reg_611x
) {
1753 devpriv
->stc_writew(dev
, AI_SHIFTIN_Pulse_Width
|
1755 AI_LOCALMUX_CLK_Pulse_Width
,
1756 AI_Personal_Register
);
1757 devpriv
->stc_writew(dev
,
1758 AI_SCAN_IN_PROG_Output_Select(3) |
1759 AI_EXTMUX_CLK_Output_Select(0) |
1760 AI_LOCALMUX_CLK_Output_Select(2) |
1761 AI_SC_TC_Output_Select(3) |
1762 AI_CONVERT_Output_Select
1763 (AI_CONVERT_Output_Enable_High
),
1764 AI_Output_Control_Register
);
1765 } else if (board
->reg_type
== ni_reg_6143
) {
1766 devpriv
->stc_writew(dev
, AI_SHIFTIN_Pulse_Width
|
1768 AI_LOCALMUX_CLK_Pulse_Width
,
1769 AI_Personal_Register
);
1770 devpriv
->stc_writew(dev
,
1771 AI_SCAN_IN_PROG_Output_Select(3) |
1772 AI_EXTMUX_CLK_Output_Select(0) |
1773 AI_LOCALMUX_CLK_Output_Select(2) |
1774 AI_SC_TC_Output_Select(3) |
1775 AI_CONVERT_Output_Select
1776 (AI_CONVERT_Output_Enable_Low
),
1777 AI_Output_Control_Register
);
1779 unsigned ai_output_control_bits
;
1780 devpriv
->stc_writew(dev
, AI_SHIFTIN_Pulse_Width
|
1782 AI_CONVERT_Pulse_Width
|
1783 AI_LOCALMUX_CLK_Pulse_Width
,
1784 AI_Personal_Register
);
1785 ai_output_control_bits
=
1786 AI_SCAN_IN_PROG_Output_Select(3) |
1787 AI_EXTMUX_CLK_Output_Select(0) |
1788 AI_LOCALMUX_CLK_Output_Select(2) |
1789 AI_SC_TC_Output_Select(3);
1790 if (board
->reg_type
== ni_reg_622x
)
1791 ai_output_control_bits
|=
1792 AI_CONVERT_Output_Select
1793 (AI_CONVERT_Output_Enable_High
);
1795 ai_output_control_bits
|=
1796 AI_CONVERT_Output_Select
1797 (AI_CONVERT_Output_Enable_Low
);
1798 devpriv
->stc_writew(dev
, ai_output_control_bits
,
1799 AI_Output_Control_Register
);
1801 /* the following registers should not be changed, because there
1802 * are no backup registers in devpriv. If you want to change
1803 * any of these, add a backup register and other appropriate code:
1804 * AI_Mode_1_Register
1805 * AI_Mode_3_Register
1806 * AI_Personal_Register
1807 * AI_Output_Control_Register
1809 devpriv
->stc_writew(dev
, AI_SC_TC_Error_Confirm
| AI_START_Interrupt_Ack
| AI_START2_Interrupt_Ack
| AI_START1_Interrupt_Ack
| AI_SC_TC_Interrupt_Ack
| AI_Error_Interrupt_Ack
| AI_STOP_Interrupt_Ack
, Interrupt_A_Ack_Register
); /* clear interrupts */
1811 devpriv
->stc_writew(dev
, AI_Configuration_End
, Joint_Reset_Register
);
1816 static int ni_ai_poll(struct comedi_device
*dev
, struct comedi_subdevice
*s
)
1818 unsigned long flags
;
1821 /* lock to avoid race with interrupt handler */
1822 spin_lock_irqsave(&dev
->spinlock
, flags
);
1824 ni_handle_fifo_dregs(dev
);
1826 ni_sync_ai_dma(dev
);
1828 count
= s
->async
->buf_write_count
- s
->async
->buf_read_count
;
1829 spin_unlock_irqrestore(&dev
->spinlock
, flags
);
1834 static int ni_ai_insn_read(struct comedi_device
*dev
,
1835 struct comedi_subdevice
*s
, struct comedi_insn
*insn
,
1838 const struct ni_board_struct
*board
= comedi_board(dev
);
1839 struct ni_private
*devpriv
= dev
->private;
1841 const unsigned int mask
= (1 << board
->adbits
) - 1;
1846 ni_load_channelgain_list(dev
, 1, &insn
->chanspec
);
1848 ni_clear_ai_fifo(dev
);
1850 signbits
= devpriv
->ai_offset
[0];
1851 if (board
->reg_type
== ni_reg_611x
) {
1852 for (n
= 0; n
< num_adc_stages_611x
; n
++) {
1853 devpriv
->stc_writew(dev
, AI_CONVERT_Pulse
,
1854 AI_Command_1_Register
);
1857 for (n
= 0; n
< insn
->n
; n
++) {
1858 devpriv
->stc_writew(dev
, AI_CONVERT_Pulse
,
1859 AI_Command_1_Register
);
1860 /* The 611x has screwy 32-bit FIFOs. */
1862 for (i
= 0; i
< NI_TIMEOUT
; i
++) {
1863 if (ni_readb(XXX_Status
) & 0x80) {
1864 d
= (ni_readl(ADC_FIFO_Data_611x
) >> 16)
1868 if (!(devpriv
->stc_readw(dev
,
1869 AI_Status_1_Register
) &
1870 AI_FIFO_Empty_St
)) {
1871 d
= ni_readl(ADC_FIFO_Data_611x
) &
1876 if (i
== NI_TIMEOUT
) {
1878 ("ni_mio_common: timeout in 611x ni_ai_insn_read\n");
1884 } else if (board
->reg_type
== ni_reg_6143
) {
1885 for (n
= 0; n
< insn
->n
; n
++) {
1886 devpriv
->stc_writew(dev
, AI_CONVERT_Pulse
,
1887 AI_Command_1_Register
);
1889 /* The 6143 has 32-bit FIFOs. You need to strobe a bit to move a single 16bit stranded sample into the FIFO */
1891 for (i
= 0; i
< NI_TIMEOUT
; i
++) {
1892 if (ni_readl(AIFIFO_Status_6143
) & 0x01) {
1893 ni_writel(0x01, AIFIFO_Control_6143
); /* Get stranded sample into FIFO */
1894 dl
= ni_readl(AIFIFO_Data_6143
);
1898 if (i
== NI_TIMEOUT
) {
1900 ("ni_mio_common: timeout in 6143 ni_ai_insn_read\n");
1903 data
[n
] = (((dl
>> 16) & 0xFFFF) + signbits
) & 0xFFFF;
1906 for (n
= 0; n
< insn
->n
; n
++) {
1907 devpriv
->stc_writew(dev
, AI_CONVERT_Pulse
,
1908 AI_Command_1_Register
);
1909 for (i
= 0; i
< NI_TIMEOUT
; i
++) {
1910 if (!(devpriv
->stc_readw(dev
,
1911 AI_Status_1_Register
) &
1915 if (i
== NI_TIMEOUT
) {
1917 ("ni_mio_common: timeout in ni_ai_insn_read\n");
1920 if (board
->reg_type
& ni_reg_m_series_mask
) {
1922 ni_readl(M_Offset_AI_FIFO_Data
) & mask
;
1924 d
= ni_readw(ADC_FIFO_Data_Register
);
1925 d
+= signbits
; /* subtle: needs to be short addition */
1933 static void ni_prime_channelgain_list(struct comedi_device
*dev
)
1935 struct ni_private
*devpriv
= dev
->private;
1938 devpriv
->stc_writew(dev
, AI_CONVERT_Pulse
, AI_Command_1_Register
);
1939 for (i
= 0; i
< NI_TIMEOUT
; ++i
) {
1940 if (!(devpriv
->stc_readw(dev
,
1941 AI_Status_1_Register
) &
1942 AI_FIFO_Empty_St
)) {
1943 devpriv
->stc_writew(dev
, 1, ADC_FIFO_Clear
);
1948 printk("ni_mio_common: timeout loading channel/gain list\n");
1951 static void ni_m_series_load_channelgain_list(struct comedi_device
*dev
,
1952 unsigned int n_chan
,
1955 const struct ni_board_struct
*board
= comedi_board(dev
);
1956 struct ni_private
*devpriv
= dev
->private;
1957 unsigned int chan
, range
, aref
;
1960 unsigned int dither
;
1961 unsigned range_code
;
1963 devpriv
->stc_writew(dev
, 1, Configuration_Memory_Clear
);
1965 /* offset = 1 << (board->adbits - 1); */
1966 if ((list
[0] & CR_ALT_SOURCE
)) {
1967 unsigned bypass_bits
;
1968 chan
= CR_CHAN(list
[0]);
1969 range
= CR_RANGE(list
[0]);
1970 range_code
= ni_gainlkup
[board
->gainlkup
][range
];
1971 dither
= ((list
[0] & CR_ALT_FILTER
) != 0);
1972 bypass_bits
= MSeries_AI_Bypass_Config_FIFO_Bit
;
1973 bypass_bits
|= chan
;
1975 (devpriv
->ai_calib_source
) &
1976 (MSeries_AI_Bypass_Cal_Sel_Pos_Mask
|
1977 MSeries_AI_Bypass_Cal_Sel_Neg_Mask
|
1978 MSeries_AI_Bypass_Mode_Mux_Mask
|
1979 MSeries_AO_Bypass_AO_Cal_Sel_Mask
);
1980 bypass_bits
|= MSeries_AI_Bypass_Gain_Bits(range_code
);
1982 bypass_bits
|= MSeries_AI_Bypass_Dither_Bit
;
1983 /* don't use 2's complement encoding */
1984 bypass_bits
|= MSeries_AI_Bypass_Polarity_Bit
;
1985 ni_writel(bypass_bits
, M_Offset_AI_Config_FIFO_Bypass
);
1987 ni_writel(0, M_Offset_AI_Config_FIFO_Bypass
);
1990 for (i
= 0; i
< n_chan
; i
++) {
1991 unsigned config_bits
= 0;
1992 chan
= CR_CHAN(list
[i
]);
1993 aref
= CR_AREF(list
[i
]);
1994 range
= CR_RANGE(list
[i
]);
1995 dither
= ((list
[i
] & CR_ALT_FILTER
) != 0);
1997 range_code
= ni_gainlkup
[board
->gainlkup
][range
];
1998 devpriv
->ai_offset
[i
] = offset
;
2002 MSeries_AI_Config_Channel_Type_Differential_Bits
;
2006 MSeries_AI_Config_Channel_Type_Common_Ref_Bits
;
2010 MSeries_AI_Config_Channel_Type_Ground_Ref_Bits
;
2015 config_bits
|= MSeries_AI_Config_Channel_Bits(chan
);
2017 MSeries_AI_Config_Bank_Bits(board
->reg_type
, chan
);
2018 config_bits
|= MSeries_AI_Config_Gain_Bits(range_code
);
2019 if (i
== n_chan
- 1)
2020 config_bits
|= MSeries_AI_Config_Last_Channel_Bit
;
2022 config_bits
|= MSeries_AI_Config_Dither_Bit
;
2023 /* don't use 2's complement encoding */
2024 config_bits
|= MSeries_AI_Config_Polarity_Bit
;
2025 ni_writew(config_bits
, M_Offset_AI_Config_FIFO_Data
);
2027 ni_prime_channelgain_list(dev
);
2031 * Notes on the 6110 and 6111:
2032 * These boards a slightly different than the rest of the series, since
2033 * they have multiple A/D converters.
2034 * From the driver side, the configuration memory is a
2036 * Configuration Memory Low:
2038 * bit 8: unipolar/bipolar (should be 0 for bipolar)
2039 * bits 0-3: gain. This is 4 bits instead of 3 for the other boards
2040 * 1001 gain=0.1 (+/- 50)
2049 * Configuration Memory High:
2050 * bits 12-14: Channel Type
2051 * 001 for differential
2052 * 000 for calibration
2053 * bit 11: coupling (this is not currently handled)
2057 * valid channels are 0-3
2059 static void ni_load_channelgain_list(struct comedi_device
*dev
,
2060 unsigned int n_chan
, unsigned int *list
)
2062 const struct ni_board_struct
*board
= comedi_board(dev
);
2063 struct ni_private
*devpriv
= dev
->private;
2064 unsigned int chan
, range
, aref
;
2066 unsigned int hi
, lo
;
2068 unsigned int dither
;
2070 if (board
->reg_type
& ni_reg_m_series_mask
) {
2071 ni_m_series_load_channelgain_list(dev
, n_chan
, list
);
2074 if (n_chan
== 1 && (board
->reg_type
!= ni_reg_611x
)
2075 && (board
->reg_type
!= ni_reg_6143
)) {
2076 if (devpriv
->changain_state
2077 && devpriv
->changain_spec
== list
[0]) {
2081 devpriv
->changain_state
= 1;
2082 devpriv
->changain_spec
= list
[0];
2084 devpriv
->changain_state
= 0;
2087 devpriv
->stc_writew(dev
, 1, Configuration_Memory_Clear
);
2089 /* Set up Calibration mode if required */
2090 if (board
->reg_type
== ni_reg_6143
) {
2091 if ((list
[0] & CR_ALT_SOURCE
)
2092 && !devpriv
->ai_calib_source_enabled
) {
2093 /* Strobe Relay enable bit */
2094 ni_writew(devpriv
->ai_calib_source
|
2095 Calibration_Channel_6143_RelayOn
,
2096 Calibration_Channel_6143
);
2097 ni_writew(devpriv
->ai_calib_source
,
2098 Calibration_Channel_6143
);
2099 devpriv
->ai_calib_source_enabled
= 1;
2100 msleep_interruptible(100); /* Allow relays to change */
2101 } else if (!(list
[0] & CR_ALT_SOURCE
)
2102 && devpriv
->ai_calib_source_enabled
) {
2103 /* Strobe Relay disable bit */
2104 ni_writew(devpriv
->ai_calib_source
|
2105 Calibration_Channel_6143_RelayOff
,
2106 Calibration_Channel_6143
);
2107 ni_writew(devpriv
->ai_calib_source
,
2108 Calibration_Channel_6143
);
2109 devpriv
->ai_calib_source_enabled
= 0;
2110 msleep_interruptible(100); /* Allow relays to change */
2114 offset
= 1 << (board
->adbits
- 1);
2115 for (i
= 0; i
< n_chan
; i
++) {
2116 if ((board
->reg_type
!= ni_reg_6143
)
2117 && (list
[i
] & CR_ALT_SOURCE
)) {
2118 chan
= devpriv
->ai_calib_source
;
2120 chan
= CR_CHAN(list
[i
]);
2122 aref
= CR_AREF(list
[i
]);
2123 range
= CR_RANGE(list
[i
]);
2124 dither
= ((list
[i
] & CR_ALT_FILTER
) != 0);
2126 /* fix the external/internal range differences */
2127 range
= ni_gainlkup
[board
->gainlkup
][range
];
2128 if (board
->reg_type
== ni_reg_611x
)
2129 devpriv
->ai_offset
[i
] = offset
;
2131 devpriv
->ai_offset
[i
] = (range
& 0x100) ? 0 : offset
;
2134 if ((list
[i
] & CR_ALT_SOURCE
)) {
2135 if (board
->reg_type
== ni_reg_611x
)
2136 ni_writew(CR_CHAN(list
[i
]) & 0x0003,
2137 Calibration_Channel_Select_611x
);
2139 if (board
->reg_type
== ni_reg_611x
)
2141 else if (board
->reg_type
== ni_reg_6143
)
2145 hi
|= AI_DIFFERENTIAL
;
2157 hi
|= AI_CONFIG_CHANNEL(chan
);
2159 ni_writew(hi
, Configuration_Memory_High
);
2161 if (board
->reg_type
!= ni_reg_6143
) {
2163 if (i
== n_chan
- 1)
2164 lo
|= AI_LAST_CHANNEL
;
2168 ni_writew(lo
, Configuration_Memory_Low
);
2172 /* prime the channel/gain list */
2173 if ((board
->reg_type
!= ni_reg_611x
)
2174 && (board
->reg_type
!= ni_reg_6143
)) {
2175 ni_prime_channelgain_list(dev
);
2179 static int ni_ns_to_timer(const struct comedi_device
*dev
, unsigned nanosec
,
2182 struct ni_private
*devpriv
= dev
->private;
2185 switch (round_mode
) {
2186 case TRIG_ROUND_NEAREST
:
2188 divider
= (nanosec
+ devpriv
->clock_ns
/ 2) / devpriv
->clock_ns
;
2190 case TRIG_ROUND_DOWN
:
2191 divider
= (nanosec
) / devpriv
->clock_ns
;
2194 divider
= (nanosec
+ devpriv
->clock_ns
- 1) / devpriv
->clock_ns
;
2200 static unsigned ni_timer_to_ns(const struct comedi_device
*dev
, int timer
)
2202 struct ni_private
*devpriv
= dev
->private;
2204 return devpriv
->clock_ns
* (timer
+ 1);
2207 static unsigned ni_min_ai_scan_period_ns(struct comedi_device
*dev
,
2208 unsigned num_channels
)
2210 const struct ni_board_struct
*board
= comedi_board(dev
);
2212 switch (board
->reg_type
) {
2215 /* simultaneously-sampled inputs */
2216 return board
->ai_speed
;
2219 /* multiplexed inputs */
2222 return board
->ai_speed
* num_channels
;
2225 static int ni_ai_cmdtest(struct comedi_device
*dev
, struct comedi_subdevice
*s
,
2226 struct comedi_cmd
*cmd
)
2228 const struct ni_board_struct
*board
= comedi_board(dev
);
2229 struct ni_private
*devpriv
= dev
->private;
2232 unsigned int sources
;
2234 /* Step 1 : check if triggers are trivially valid */
2236 if ((cmd
->flags
& CMDF_WRITE
))
2237 cmd
->flags
&= ~CMDF_WRITE
;
2239 err
|= cfc_check_trigger_src(&cmd
->start_src
,
2240 TRIG_NOW
| TRIG_INT
| TRIG_EXT
);
2241 err
|= cfc_check_trigger_src(&cmd
->scan_begin_src
,
2242 TRIG_TIMER
| TRIG_EXT
);
2244 sources
= TRIG_TIMER
| TRIG_EXT
;
2245 if (board
->reg_type
== ni_reg_611x
||
2246 board
->reg_type
== ni_reg_6143
)
2247 sources
|= TRIG_NOW
;
2248 err
|= cfc_check_trigger_src(&cmd
->convert_src
, sources
);
2250 err
|= cfc_check_trigger_src(&cmd
->scan_end_src
, TRIG_COUNT
);
2251 err
|= cfc_check_trigger_src(&cmd
->stop_src
, TRIG_COUNT
| TRIG_NONE
);
2256 /* Step 2a : make sure trigger sources are unique */
2258 err
|= cfc_check_trigger_is_unique(cmd
->start_src
);
2259 err
|= cfc_check_trigger_is_unique(cmd
->scan_begin_src
);
2260 err
|= cfc_check_trigger_is_unique(cmd
->convert_src
);
2261 err
|= cfc_check_trigger_is_unique(cmd
->stop_src
);
2263 /* Step 2b : and mutually compatible */
2268 /* Step 3: check if arguments are trivially valid */
2270 if (cmd
->start_src
== TRIG_EXT
) {
2271 /* external trigger */
2272 unsigned int tmp
= CR_CHAN(cmd
->start_arg
);
2276 tmp
|= (cmd
->start_arg
& (CR_INVERT
| CR_EDGE
));
2277 err
|= cfc_check_trigger_arg_is(&cmd
->start_arg
, tmp
);
2279 /* true for both TRIG_NOW and TRIG_INT */
2280 err
|= cfc_check_trigger_arg_is(&cmd
->start_arg
, 0);
2283 if (cmd
->scan_begin_src
== TRIG_TIMER
) {
2284 err
|= cfc_check_trigger_arg_min(&cmd
->scan_begin_arg
,
2285 ni_min_ai_scan_period_ns(dev
, cmd
->chanlist_len
));
2286 err
|= cfc_check_trigger_arg_max(&cmd
->scan_begin_arg
,
2287 devpriv
->clock_ns
* 0xffffff);
2288 } else if (cmd
->scan_begin_src
== TRIG_EXT
) {
2289 /* external trigger */
2290 unsigned int tmp
= CR_CHAN(cmd
->scan_begin_arg
);
2294 tmp
|= (cmd
->scan_begin_arg
& (CR_INVERT
| CR_EDGE
));
2295 err
|= cfc_check_trigger_arg_is(&cmd
->scan_begin_arg
, tmp
);
2296 } else { /* TRIG_OTHER */
2297 err
|= cfc_check_trigger_arg_is(&cmd
->scan_begin_arg
, 0);
2300 if (cmd
->convert_src
== TRIG_TIMER
) {
2301 if ((board
->reg_type
== ni_reg_611x
)
2302 || (board
->reg_type
== ni_reg_6143
)) {
2303 err
|= cfc_check_trigger_arg_is(&cmd
->convert_arg
, 0);
2305 err
|= cfc_check_trigger_arg_min(&cmd
->convert_arg
,
2307 err
|= cfc_check_trigger_arg_max(&cmd
->convert_arg
,
2308 devpriv
->clock_ns
* 0xffff);
2310 } else if (cmd
->convert_src
== TRIG_EXT
) {
2311 /* external trigger */
2312 unsigned int tmp
= CR_CHAN(cmd
->convert_arg
);
2316 tmp
|= (cmd
->convert_arg
& (CR_ALT_FILTER
| CR_INVERT
));
2317 err
|= cfc_check_trigger_arg_is(&cmd
->convert_arg
, tmp
);
2318 } else if (cmd
->convert_src
== TRIG_NOW
) {
2319 err
|= cfc_check_trigger_arg_is(&cmd
->convert_arg
, 0);
2322 err
|= cfc_check_trigger_arg_is(&cmd
->scan_end_arg
, cmd
->chanlist_len
);
2324 if (cmd
->stop_src
== TRIG_COUNT
) {
2325 unsigned int max_count
= 0x01000000;
2327 if (board
->reg_type
== ni_reg_611x
)
2328 max_count
-= num_adc_stages_611x
;
2329 err
|= cfc_check_trigger_arg_max(&cmd
->stop_arg
, max_count
);
2330 err
|= cfc_check_trigger_arg_min(&cmd
->stop_arg
, 1);
2333 err
|= cfc_check_trigger_arg_is(&cmd
->stop_arg
, 0);
2339 /* step 4: fix up any arguments */
2341 if (cmd
->scan_begin_src
== TRIG_TIMER
) {
2342 tmp
= cmd
->scan_begin_arg
;
2343 cmd
->scan_begin_arg
=
2344 ni_timer_to_ns(dev
, ni_ns_to_timer(dev
,
2345 cmd
->scan_begin_arg
,
2349 if (tmp
!= cmd
->scan_begin_arg
)
2352 if (cmd
->convert_src
== TRIG_TIMER
) {
2353 if ((board
->reg_type
!= ni_reg_611x
)
2354 && (board
->reg_type
!= ni_reg_6143
)) {
2355 tmp
= cmd
->convert_arg
;
2357 ni_timer_to_ns(dev
, ni_ns_to_timer(dev
,
2362 if (tmp
!= cmd
->convert_arg
)
2364 if (cmd
->scan_begin_src
== TRIG_TIMER
&&
2365 cmd
->scan_begin_arg
<
2366 cmd
->convert_arg
* cmd
->scan_end_arg
) {
2367 cmd
->scan_begin_arg
=
2368 cmd
->convert_arg
* cmd
->scan_end_arg
;
2380 static int ni_ai_cmd(struct comedi_device
*dev
, struct comedi_subdevice
*s
)
2382 const struct ni_board_struct
*board
= comedi_board(dev
);
2383 struct ni_private
*devpriv
= dev
->private;
2384 const struct comedi_cmd
*cmd
= &s
->async
->cmd
;
2386 int mode1
= 0; /* mode1 is needed for both stop and convert */
2388 int start_stop_select
= 0;
2389 unsigned int stop_count
;
2390 int interrupt_a_enable
= 0;
2392 MDPRINTK("ni_ai_cmd\n");
2393 if (dev
->irq
== 0) {
2394 comedi_error(dev
, "cannot run command without an irq");
2397 ni_clear_ai_fifo(dev
);
2399 ni_load_channelgain_list(dev
, cmd
->chanlist_len
, cmd
->chanlist
);
2401 /* start configuration */
2402 devpriv
->stc_writew(dev
, AI_Configuration_Start
, Joint_Reset_Register
);
2404 /* disable analog triggering for now, since it
2405 * interferes with the use of pfi0 */
2406 devpriv
->an_trig_etc_reg
&= ~Analog_Trigger_Enable
;
2407 devpriv
->stc_writew(dev
, devpriv
->an_trig_etc_reg
,
2408 Analog_Trigger_Etc_Register
);
2410 switch (cmd
->start_src
) {
2413 devpriv
->stc_writew(dev
, AI_START2_Select(0) |
2414 AI_START1_Sync
| AI_START1_Edge
|
2415 AI_START1_Select(0),
2416 AI_Trigger_Select_Register
);
2420 int chan
= CR_CHAN(cmd
->start_arg
);
2421 unsigned int bits
= AI_START2_Select(0) |
2422 AI_START1_Sync
| AI_START1_Select(chan
+ 1);
2424 if (cmd
->start_arg
& CR_INVERT
)
2425 bits
|= AI_START1_Polarity
;
2426 if (cmd
->start_arg
& CR_EDGE
)
2427 bits
|= AI_START1_Edge
;
2428 devpriv
->stc_writew(dev
, bits
,
2429 AI_Trigger_Select_Register
);
2434 mode2
&= ~AI_Pre_Trigger
;
2435 mode2
&= ~AI_SC_Initial_Load_Source
;
2436 mode2
&= ~AI_SC_Reload_Mode
;
2437 devpriv
->stc_writew(dev
, mode2
, AI_Mode_2_Register
);
2439 if (cmd
->chanlist_len
== 1 || (board
->reg_type
== ni_reg_611x
)
2440 || (board
->reg_type
== ni_reg_6143
)) {
2441 start_stop_select
|= AI_STOP_Polarity
;
2442 start_stop_select
|= AI_STOP_Select(31); /* logic low */
2443 start_stop_select
|= AI_STOP_Sync
;
2445 start_stop_select
|= AI_STOP_Select(19); /* ai configuration memory */
2447 devpriv
->stc_writew(dev
, start_stop_select
,
2448 AI_START_STOP_Select_Register
);
2450 devpriv
->ai_cmd2
= 0;
2451 switch (cmd
->stop_src
) {
2453 stop_count
= cmd
->stop_arg
- 1;
2455 if (board
->reg_type
== ni_reg_611x
) {
2456 /* have to take 3 stage adc pipeline into account */
2457 stop_count
+= num_adc_stages_611x
;
2459 /* stage number of scans */
2460 devpriv
->stc_writel(dev
, stop_count
, AI_SC_Load_A_Registers
);
2462 mode1
|= AI_Start_Stop
| AI_Mode_1_Reserved
| AI_Trigger_Once
;
2463 devpriv
->stc_writew(dev
, mode1
, AI_Mode_1_Register
);
2464 /* load SC (Scan Count) */
2465 devpriv
->stc_writew(dev
, AI_SC_Load
, AI_Command_1_Register
);
2467 devpriv
->ai_continuous
= 0;
2468 if (stop_count
== 0) {
2469 devpriv
->ai_cmd2
|= AI_End_On_End_Of_Scan
;
2470 interrupt_a_enable
|= AI_STOP_Interrupt_Enable
;
2471 /* this is required to get the last sample for chanlist_len > 1, not sure why */
2472 if (cmd
->chanlist_len
> 1)
2473 start_stop_select
|=
2474 AI_STOP_Polarity
| AI_STOP_Edge
;
2478 /* stage number of scans */
2479 devpriv
->stc_writel(dev
, 0, AI_SC_Load_A_Registers
);
2481 mode1
|= AI_Start_Stop
| AI_Mode_1_Reserved
| AI_Continuous
;
2482 devpriv
->stc_writew(dev
, mode1
, AI_Mode_1_Register
);
2484 /* load SC (Scan Count) */
2485 devpriv
->stc_writew(dev
, AI_SC_Load
, AI_Command_1_Register
);
2487 devpriv
->ai_continuous
= 1;
2492 switch (cmd
->scan_begin_src
) {
2495 stop bits for non 611x boards
2496 AI_SI_Special_Trigger_Delay=0
2498 AI_START_STOP_Select_Register:
2499 AI_START_Polarity=0 (?) rising edge
2500 AI_START_Edge=1 edge triggered
2502 AI_START_Select=0 SI_TC
2503 AI_STOP_Polarity=0 rising edge
2504 AI_STOP_Edge=0 level
2506 AI_STOP_Select=19 external pin (configuration mem)
2508 start_stop_select
|= AI_START_Edge
| AI_START_Sync
;
2509 devpriv
->stc_writew(dev
, start_stop_select
,
2510 AI_START_STOP_Select_Register
);
2512 mode2
|= AI_SI_Reload_Mode(0);
2513 /* AI_SI_Initial_Load_Source=A */
2514 mode2
&= ~AI_SI_Initial_Load_Source
;
2515 /* mode2 |= AI_SC_Reload_Mode; */
2516 devpriv
->stc_writew(dev
, mode2
, AI_Mode_2_Register
);
2519 timer
= ni_ns_to_timer(dev
, cmd
->scan_begin_arg
,
2520 TRIG_ROUND_NEAREST
);
2521 devpriv
->stc_writel(dev
, timer
, AI_SI_Load_A_Registers
);
2522 devpriv
->stc_writew(dev
, AI_SI_Load
, AI_Command_1_Register
);
2525 if (cmd
->scan_begin_arg
& CR_EDGE
)
2526 start_stop_select
|= AI_START_Edge
;
2527 /* AI_START_Polarity==1 is falling edge */
2528 if (cmd
->scan_begin_arg
& CR_INVERT
)
2529 start_stop_select
|= AI_START_Polarity
;
2530 if (cmd
->scan_begin_src
!= cmd
->convert_src
||
2531 (cmd
->scan_begin_arg
& ~CR_EDGE
) !=
2532 (cmd
->convert_arg
& ~CR_EDGE
))
2533 start_stop_select
|= AI_START_Sync
;
2534 start_stop_select
|=
2535 AI_START_Select(1 + CR_CHAN(cmd
->scan_begin_arg
));
2536 devpriv
->stc_writew(dev
, start_stop_select
,
2537 AI_START_STOP_Select_Register
);
2541 switch (cmd
->convert_src
) {
2544 if (cmd
->convert_arg
== 0 || cmd
->convert_src
== TRIG_NOW
)
2547 timer
= ni_ns_to_timer(dev
, cmd
->convert_arg
,
2548 TRIG_ROUND_NEAREST
);
2549 devpriv
->stc_writew(dev
, 1, AI_SI2_Load_A_Register
); /* 0,0 does not work. */
2550 devpriv
->stc_writew(dev
, timer
, AI_SI2_Load_B_Register
);
2552 /* AI_SI2_Reload_Mode = alternate */
2553 /* AI_SI2_Initial_Load_Source = A */
2554 mode2
&= ~AI_SI2_Initial_Load_Source
;
2555 mode2
|= AI_SI2_Reload_Mode
;
2556 devpriv
->stc_writew(dev
, mode2
, AI_Mode_2_Register
);
2559 devpriv
->stc_writew(dev
, AI_SI2_Load
, AI_Command_1_Register
);
2561 mode2
|= AI_SI2_Reload_Mode
; /* alternate */
2562 mode2
|= AI_SI2_Initial_Load_Source
; /* B */
2564 devpriv
->stc_writew(dev
, mode2
, AI_Mode_2_Register
);
2567 mode1
|= AI_CONVERT_Source_Select(1 + cmd
->convert_arg
);
2568 if ((cmd
->convert_arg
& CR_INVERT
) == 0)
2569 mode1
|= AI_CONVERT_Source_Polarity
;
2570 devpriv
->stc_writew(dev
, mode1
, AI_Mode_1_Register
);
2572 mode2
|= AI_Start_Stop_Gate_Enable
| AI_SC_Gate_Enable
;
2573 devpriv
->stc_writew(dev
, mode2
, AI_Mode_2_Register
);
2580 /* interrupt on FIFO, errors, SC_TC */
2581 interrupt_a_enable
|= AI_Error_Interrupt_Enable
|
2582 AI_SC_TC_Interrupt_Enable
;
2585 interrupt_a_enable
|= AI_FIFO_Interrupt_Enable
;
2588 if (cmd
->flags
& TRIG_WAKE_EOS
2589 || (devpriv
->ai_cmd2
& AI_End_On_End_Of_Scan
)) {
2590 /* wake on end-of-scan */
2591 devpriv
->aimode
= AIMODE_SCAN
;
2593 devpriv
->aimode
= AIMODE_HALF_FULL
;
2596 switch (devpriv
->aimode
) {
2597 case AIMODE_HALF_FULL
:
2598 /*generate FIFO interrupts and DMA requests on half-full */
2600 devpriv
->stc_writew(dev
, AI_FIFO_Mode_HF_to_E
,
2601 AI_Mode_3_Register
);
2603 devpriv
->stc_writew(dev
, AI_FIFO_Mode_HF
,
2604 AI_Mode_3_Register
);
2608 /*generate FIFO interrupts on non-empty */
2609 devpriv
->stc_writew(dev
, AI_FIFO_Mode_NE
,
2610 AI_Mode_3_Register
);
2614 devpriv
->stc_writew(dev
, AI_FIFO_Mode_NE
,
2615 AI_Mode_3_Register
);
2617 devpriv
->stc_writew(dev
, AI_FIFO_Mode_HF
,
2618 AI_Mode_3_Register
);
2620 interrupt_a_enable
|= AI_STOP_Interrupt_Enable
;
2626 devpriv
->stc_writew(dev
, AI_Error_Interrupt_Ack
| AI_STOP_Interrupt_Ack
| AI_START_Interrupt_Ack
| AI_START2_Interrupt_Ack
| AI_START1_Interrupt_Ack
| AI_SC_TC_Interrupt_Ack
| AI_SC_TC_Error_Confirm
, Interrupt_A_Ack_Register
); /* clear interrupts */
2628 ni_set_bits(dev
, Interrupt_A_Enable_Register
,
2629 interrupt_a_enable
, 1);
2631 MDPRINTK("Interrupt_A_Enable_Register = 0x%04x\n",
2632 devpriv
->int_a_enable_reg
);
2634 /* interrupt on nothing */
2635 ni_set_bits(dev
, Interrupt_A_Enable_Register
, ~0, 0);
2637 /* XXX start polling if necessary */
2638 MDPRINTK("interrupting on nothing\n");
2641 /* end configuration */
2642 devpriv
->stc_writew(dev
, AI_Configuration_End
, Joint_Reset_Register
);
2644 switch (cmd
->scan_begin_src
) {
2646 devpriv
->stc_writew(dev
,
2647 AI_SI2_Arm
| AI_SI_Arm
| AI_DIV_Arm
|
2648 AI_SC_Arm
, AI_Command_1_Register
);
2651 /* XXX AI_SI_Arm? */
2652 devpriv
->stc_writew(dev
,
2653 AI_SI2_Arm
| AI_SI_Arm
| AI_DIV_Arm
|
2654 AI_SC_Arm
, AI_Command_1_Register
);
2660 int retval
= ni_ai_setup_MITE_dma(dev
);
2664 /* mite_dump_regs(devpriv->mite); */
2667 switch (cmd
->start_src
) {
2669 /* AI_START1_Pulse */
2670 devpriv
->stc_writew(dev
, AI_START1_Pulse
| devpriv
->ai_cmd2
,
2671 AI_Command_2_Register
);
2672 s
->async
->inttrig
= NULL
;
2675 s
->async
->inttrig
= NULL
;
2678 s
->async
->inttrig
= &ni_ai_inttrig
;
2682 MDPRINTK("exit ni_ai_cmd\n");
2687 static int ni_ai_inttrig(struct comedi_device
*dev
, struct comedi_subdevice
*s
,
2688 unsigned int trignum
)
2690 struct ni_private
*devpriv
= dev
->private;
2695 devpriv
->stc_writew(dev
, AI_START1_Pulse
| devpriv
->ai_cmd2
,
2696 AI_Command_2_Register
);
2697 s
->async
->inttrig
= NULL
;
2702 static int ni_ai_config_analog_trig(struct comedi_device
*dev
,
2703 struct comedi_subdevice
*s
,
2704 struct comedi_insn
*insn
,
2705 unsigned int *data
);
2707 static int ni_ai_insn_config(struct comedi_device
*dev
,
2708 struct comedi_subdevice
*s
,
2709 struct comedi_insn
*insn
, unsigned int *data
)
2711 const struct ni_board_struct
*board
= comedi_board(dev
);
2712 struct ni_private
*devpriv
= dev
->private;
2718 case INSN_CONFIG_ANALOG_TRIG
:
2719 return ni_ai_config_analog_trig(dev
, s
, insn
, data
);
2720 case INSN_CONFIG_ALT_SOURCE
:
2721 if (board
->reg_type
& ni_reg_m_series_mask
) {
2722 if (data
[1] & ~(MSeries_AI_Bypass_Cal_Sel_Pos_Mask
|
2723 MSeries_AI_Bypass_Cal_Sel_Neg_Mask
|
2724 MSeries_AI_Bypass_Mode_Mux_Mask
|
2725 MSeries_AO_Bypass_AO_Cal_Sel_Mask
)) {
2728 devpriv
->ai_calib_source
= data
[1];
2729 } else if (board
->reg_type
== ni_reg_6143
) {
2730 unsigned int calib_source
;
2732 calib_source
= data
[1] & 0xf;
2734 if (calib_source
> 0xF)
2737 devpriv
->ai_calib_source
= calib_source
;
2738 ni_writew(calib_source
, Calibration_Channel_6143
);
2740 unsigned int calib_source
;
2741 unsigned int calib_source_adjust
;
2743 calib_source
= data
[1] & 0xf;
2744 calib_source_adjust
= (data
[1] >> 4) & 0xff;
2746 if (calib_source
>= 8)
2748 devpriv
->ai_calib_source
= calib_source
;
2749 if (board
->reg_type
== ni_reg_611x
) {
2750 ni_writeb(calib_source_adjust
,
2751 Cal_Gain_Select_611x
);
2762 static int ni_ai_config_analog_trig(struct comedi_device
*dev
,
2763 struct comedi_subdevice
*s
,
2764 struct comedi_insn
*insn
,
2767 const struct ni_board_struct
*board
= comedi_board(dev
);
2768 struct ni_private
*devpriv
= dev
->private;
2769 unsigned int a
, b
, modebits
;
2773 * data[2] is analog line
2774 * data[3] is set level
2775 * data[4] is reset level */
2776 if (!board
->has_analog_trig
)
2778 if ((data
[1] & 0xffff0000) != COMEDI_EV_SCAN_BEGIN
) {
2779 data
[1] &= (COMEDI_EV_SCAN_BEGIN
| 0xffff);
2782 if (data
[2] >= board
->n_adchan
) {
2783 data
[2] = board
->n_adchan
- 1;
2786 if (data
[3] > 255) { /* a */
2790 if (data
[4] > 255) { /* b */
2801 * high mode 00 00 01 10
2802 * low mode 00 00 10 01
2804 * hysteresis low mode 10 00 00 01
2805 * hysteresis high mode 01 00 00 10
2806 * middle mode 10 01 01 10
2811 modebits
= data
[1] & 0xff;
2812 if (modebits
& 0xf0) {
2813 /* two level mode */
2819 ((data
[1] & 0xf) << 4) | ((data
[1] & 0xf0) >> 4);
2821 devpriv
->atrig_low
= a
;
2822 devpriv
->atrig_high
= b
;
2824 case 0x81: /* low hysteresis mode */
2825 devpriv
->atrig_mode
= 6;
2827 case 0x42: /* high hysteresis mode */
2828 devpriv
->atrig_mode
= 3;
2830 case 0x96: /* middle window mode */
2831 devpriv
->atrig_mode
= 2;
2838 /* one level mode */
2844 case 0x06: /* high window mode */
2845 devpriv
->atrig_high
= a
;
2846 devpriv
->atrig_mode
= 0;
2848 case 0x09: /* low window mode */
2849 devpriv
->atrig_low
= a
;
2850 devpriv
->atrig_mode
= 1;
2862 /* munge data from unsigned to 2's complement for analog output bipolar modes */
2863 static void ni_ao_munge(struct comedi_device
*dev
, struct comedi_subdevice
*s
,
2864 void *data
, unsigned int num_bytes
,
2865 unsigned int chan_index
)
2867 const struct ni_board_struct
*board
= comedi_board(dev
);
2868 struct comedi_async
*async
= s
->async
;
2871 unsigned int offset
;
2872 unsigned int length
= num_bytes
/ sizeof(short);
2873 short *array
= data
;
2875 offset
= 1 << (board
->aobits
- 1);
2876 for (i
= 0; i
< length
; i
++) {
2877 range
= CR_RANGE(async
->cmd
.chanlist
[chan_index
]);
2878 if (board
->ao_unipolar
== 0 || (range
& 1) == 0)
2881 array
[i
] = cpu_to_le16(array
[i
]);
2884 chan_index
%= async
->cmd
.chanlist_len
;
2888 static int ni_m_series_ao_config_chanlist(struct comedi_device
*dev
,
2889 struct comedi_subdevice
*s
,
2890 unsigned int chanspec
[],
2891 unsigned int n_chans
, int timed
)
2893 const struct ni_board_struct
*board
= comedi_board(dev
);
2894 struct ni_private
*devpriv
= dev
->private;
2902 for (i
= 0; i
< board
->n_aochan
; ++i
) {
2903 devpriv
->ao_conf
[i
] &= ~MSeries_AO_Update_Timed_Bit
;
2904 ni_writeb(devpriv
->ao_conf
[i
],
2905 M_Offset_AO_Config_Bank(i
));
2906 ni_writeb(0xf, M_Offset_AO_Waveform_Order(i
));
2909 for (i
= 0; i
< n_chans
; i
++) {
2910 const struct comedi_krange
*krange
;
2911 chan
= CR_CHAN(chanspec
[i
]);
2912 range
= CR_RANGE(chanspec
[i
]);
2913 krange
= s
->range_table
->range
+ range
;
2916 switch (krange
->max
- krange
->min
) {
2918 conf
|= MSeries_AO_DAC_Reference_10V_Internal_Bits
;
2919 ni_writeb(0, M_Offset_AO_Reference_Attenuation(chan
));
2922 conf
|= MSeries_AO_DAC_Reference_5V_Internal_Bits
;
2923 ni_writeb(0, M_Offset_AO_Reference_Attenuation(chan
));
2926 conf
|= MSeries_AO_DAC_Reference_10V_Internal_Bits
;
2927 ni_writeb(MSeries_Attenuate_x5_Bit
,
2928 M_Offset_AO_Reference_Attenuation(chan
));
2931 conf
|= MSeries_AO_DAC_Reference_5V_Internal_Bits
;
2932 ni_writeb(MSeries_Attenuate_x5_Bit
,
2933 M_Offset_AO_Reference_Attenuation(chan
));
2936 printk("%s: bug! unhandled ao reference voltage\n",
2940 switch (krange
->max
+ krange
->min
) {
2942 conf
|= MSeries_AO_DAC_Offset_0V_Bits
;
2945 conf
|= MSeries_AO_DAC_Offset_5V_Bits
;
2948 printk("%s: bug! unhandled ao offset voltage\n",
2953 conf
|= MSeries_AO_Update_Timed_Bit
;
2954 ni_writeb(conf
, M_Offset_AO_Config_Bank(chan
));
2955 devpriv
->ao_conf
[chan
] = conf
;
2956 ni_writeb(i
, M_Offset_AO_Waveform_Order(chan
));
2961 static int ni_old_ao_config_chanlist(struct comedi_device
*dev
,
2962 struct comedi_subdevice
*s
,
2963 unsigned int chanspec
[],
2964 unsigned int n_chans
)
2966 const struct ni_board_struct
*board
= comedi_board(dev
);
2967 struct ni_private
*devpriv
= dev
->private;
2974 for (i
= 0; i
< n_chans
; i
++) {
2975 chan
= CR_CHAN(chanspec
[i
]);
2976 range
= CR_RANGE(chanspec
[i
]);
2977 conf
= AO_Channel(chan
);
2979 if (board
->ao_unipolar
) {
2980 if ((range
& 1) == 0) {
2982 invert
= (1 << (board
->aobits
- 1));
2990 invert
= (1 << (board
->aobits
- 1));
2993 /* not all boards can deglitch, but this shouldn't hurt */
2994 if (chanspec
[i
] & CR_DEGLITCH
)
2995 conf
|= AO_Deglitch
;
2997 /* analog reference */
2998 /* AREF_OTHER connects AO ground to AI ground, i think */
2999 conf
|= (CR_AREF(chanspec
[i
]) ==
3000 AREF_OTHER
) ? AO_Ground_Ref
: 0;
3002 ni_writew(conf
, AO_Configuration
);
3003 devpriv
->ao_conf
[chan
] = conf
;
3008 static int ni_ao_config_chanlist(struct comedi_device
*dev
,
3009 struct comedi_subdevice
*s
,
3010 unsigned int chanspec
[], unsigned int n_chans
,
3013 const struct ni_board_struct
*board
= comedi_board(dev
);
3015 if (board
->reg_type
& ni_reg_m_series_mask
)
3016 return ni_m_series_ao_config_chanlist(dev
, s
, chanspec
, n_chans
,
3019 return ni_old_ao_config_chanlist(dev
, s
, chanspec
, n_chans
);
3022 static int ni_ao_insn_read(struct comedi_device
*dev
,
3023 struct comedi_subdevice
*s
, struct comedi_insn
*insn
,
3026 struct ni_private
*devpriv
= dev
->private;
3028 data
[0] = devpriv
->ao
[CR_CHAN(insn
->chanspec
)];
3033 static int ni_ao_insn_write(struct comedi_device
*dev
,
3034 struct comedi_subdevice
*s
,
3035 struct comedi_insn
*insn
, unsigned int *data
)
3037 const struct ni_board_struct
*board
= comedi_board(dev
);
3038 struct ni_private
*devpriv
= dev
->private;
3039 unsigned int chan
= CR_CHAN(insn
->chanspec
);
3040 unsigned int invert
;
3042 invert
= ni_ao_config_chanlist(dev
, s
, &insn
->chanspec
, 1, 0);
3044 devpriv
->ao
[chan
] = data
[0];
3046 if (board
->reg_type
& ni_reg_m_series_mask
) {
3047 ni_writew(data
[0], M_Offset_DAC_Direct_Data(chan
));
3049 ni_writew(data
[0] ^ invert
,
3050 (chan
) ? DAC1_Direct_Data
: DAC0_Direct_Data
);
3055 static int ni_ao_insn_write_671x(struct comedi_device
*dev
,
3056 struct comedi_subdevice
*s
,
3057 struct comedi_insn
*insn
, unsigned int *data
)
3059 const struct ni_board_struct
*board
= comedi_board(dev
);
3060 struct ni_private
*devpriv
= dev
->private;
3061 unsigned int chan
= CR_CHAN(insn
->chanspec
);
3062 unsigned int invert
;
3064 ao_win_out(1 << chan
, AO_Immediate_671x
);
3065 invert
= 1 << (board
->aobits
- 1);
3067 ni_ao_config_chanlist(dev
, s
, &insn
->chanspec
, 1, 0);
3069 devpriv
->ao
[chan
] = data
[0];
3070 ao_win_out(data
[0] ^ invert
, DACx_Direct_Data_671x(chan
));
3075 static int ni_ao_insn_config(struct comedi_device
*dev
,
3076 struct comedi_subdevice
*s
,
3077 struct comedi_insn
*insn
, unsigned int *data
)
3079 const struct ni_board_struct
*board
= comedi_board(dev
);
3080 struct ni_private
*devpriv
= dev
->private;
3083 case INSN_CONFIG_GET_HARDWARE_BUFFER_SIZE
:
3086 data
[2] = 1 + board
->ao_fifo_depth
* sizeof(short);
3088 data
[2] += devpriv
->mite
->fifo_size
;
3105 static int ni_ao_inttrig(struct comedi_device
*dev
, struct comedi_subdevice
*s
,
3106 unsigned int trignum
)
3108 const struct ni_board_struct
*board __maybe_unused
= comedi_board(dev
);
3109 struct ni_private
*devpriv
= dev
->private;
3111 int interrupt_b_bits
;
3113 static const int timeout
= 1000;
3118 /* Null trig at beginning prevent ao start trigger from executing more than
3119 once per command (and doing things like trying to allocate the ao dma channel
3121 s
->async
->inttrig
= NULL
;
3123 ni_set_bits(dev
, Interrupt_B_Enable_Register
,
3124 AO_FIFO_Interrupt_Enable
| AO_Error_Interrupt_Enable
, 0);
3125 interrupt_b_bits
= AO_Error_Interrupt_Enable
;
3127 devpriv
->stc_writew(dev
, 1, DAC_FIFO_Clear
);
3128 if (board
->reg_type
& ni_reg_6xxx_mask
)
3129 ni_ao_win_outl(dev
, 0x6, AO_FIFO_Offset_Load_611x
);
3130 ret
= ni_ao_setup_MITE_dma(dev
);
3133 ret
= ni_ao_wait_for_dma_load(dev
);
3137 ret
= ni_ao_prep_fifo(dev
, s
);
3141 interrupt_b_bits
|= AO_FIFO_Interrupt_Enable
;
3144 devpriv
->stc_writew(dev
, devpriv
->ao_mode3
| AO_Not_An_UPDATE
,
3145 AO_Mode_3_Register
);
3146 devpriv
->stc_writew(dev
, devpriv
->ao_mode3
, AO_Mode_3_Register
);
3147 /* wait for DACs to be loaded */
3148 for (i
= 0; i
< timeout
; i
++) {
3150 if ((devpriv
->stc_readw(dev
,
3151 Joint_Status_2_Register
) &
3152 AO_TMRDACWRs_In_Progress_St
) == 0)
3157 "timed out waiting for AO_TMRDACWRs_In_Progress_St to clear");
3160 /* stc manual says we are need to clear error interrupt after AO_TMRDACWRs_In_Progress_St clears */
3161 devpriv
->stc_writew(dev
, AO_Error_Interrupt_Ack
,
3162 Interrupt_B_Ack_Register
);
3164 ni_set_bits(dev
, Interrupt_B_Enable_Register
, interrupt_b_bits
, 1);
3166 devpriv
->stc_writew(dev
,
3167 devpriv
->ao_cmd1
| AO_UI_Arm
| AO_UC_Arm
| AO_BC_Arm
3168 | AO_DAC1_Update_Mode
| AO_DAC0_Update_Mode
,
3169 AO_Command_1_Register
);
3171 devpriv
->stc_writew(dev
, devpriv
->ao_cmd2
| AO_START1_Pulse
,
3172 AO_Command_2_Register
);
3177 static int ni_ao_cmd(struct comedi_device
*dev
, struct comedi_subdevice
*s
)
3179 const struct ni_board_struct
*board
= comedi_board(dev
);
3180 struct ni_private
*devpriv
= dev
->private;
3181 const struct comedi_cmd
*cmd
= &s
->async
->cmd
;
3186 if (dev
->irq
== 0) {
3187 comedi_error(dev
, "cannot run command without an irq");
3191 devpriv
->stc_writew(dev
, AO_Configuration_Start
, Joint_Reset_Register
);
3193 devpriv
->stc_writew(dev
, AO_Disarm
, AO_Command_1_Register
);
3195 if (board
->reg_type
& ni_reg_6xxx_mask
) {
3196 ao_win_out(CLEAR_WG
, AO_Misc_611x
);
3199 for (i
= 0; i
< cmd
->chanlist_len
; i
++) {
3202 chan
= CR_CHAN(cmd
->chanlist
[i
]);
3204 ao_win_out(chan
, AO_Waveform_Generation_611x
);
3206 ao_win_out(bits
, AO_Timed_611x
);
3209 ni_ao_config_chanlist(dev
, s
, cmd
->chanlist
, cmd
->chanlist_len
, 1);
3211 if (cmd
->stop_src
== TRIG_NONE
) {
3212 devpriv
->ao_mode1
|= AO_Continuous
;
3213 devpriv
->ao_mode1
&= ~AO_Trigger_Once
;
3215 devpriv
->ao_mode1
&= ~AO_Continuous
;
3216 devpriv
->ao_mode1
|= AO_Trigger_Once
;
3218 devpriv
->stc_writew(dev
, devpriv
->ao_mode1
, AO_Mode_1_Register
);
3219 switch (cmd
->start_src
) {
3222 devpriv
->ao_trigger_select
&=
3223 ~(AO_START1_Polarity
| AO_START1_Select(-1));
3224 devpriv
->ao_trigger_select
|= AO_START1_Edge
| AO_START1_Sync
;
3225 devpriv
->stc_writew(dev
, devpriv
->ao_trigger_select
,
3226 AO_Trigger_Select_Register
);
3229 devpriv
->ao_trigger_select
=
3230 AO_START1_Select(CR_CHAN(cmd
->start_arg
) + 1);
3231 if (cmd
->start_arg
& CR_INVERT
)
3232 devpriv
->ao_trigger_select
|= AO_START1_Polarity
; /* 0=active high, 1=active low. see daq-stc 3-24 (p186) */
3233 if (cmd
->start_arg
& CR_EDGE
)
3234 devpriv
->ao_trigger_select
|= AO_START1_Edge
; /* 0=edge detection disabled, 1=enabled */
3235 devpriv
->stc_writew(dev
, devpriv
->ao_trigger_select
,
3236 AO_Trigger_Select_Register
);
3242 devpriv
->ao_mode3
&= ~AO_Trigger_Length
;
3243 devpriv
->stc_writew(dev
, devpriv
->ao_mode3
, AO_Mode_3_Register
);
3245 devpriv
->stc_writew(dev
, devpriv
->ao_mode1
, AO_Mode_1_Register
);
3246 devpriv
->ao_mode2
&= ~AO_BC_Initial_Load_Source
;
3247 devpriv
->stc_writew(dev
, devpriv
->ao_mode2
, AO_Mode_2_Register
);
3248 if (cmd
->stop_src
== TRIG_NONE
) {
3249 devpriv
->stc_writel(dev
, 0xffffff, AO_BC_Load_A_Register
);
3251 devpriv
->stc_writel(dev
, 0, AO_BC_Load_A_Register
);
3253 devpriv
->stc_writew(dev
, AO_BC_Load
, AO_Command_1_Register
);
3254 devpriv
->ao_mode2
&= ~AO_UC_Initial_Load_Source
;
3255 devpriv
->stc_writew(dev
, devpriv
->ao_mode2
, AO_Mode_2_Register
);
3256 switch (cmd
->stop_src
) {
3258 if (board
->reg_type
& ni_reg_m_series_mask
) {
3259 /* this is how the NI example code does it for m-series boards, verified correct with 6259 */
3260 devpriv
->stc_writel(dev
, cmd
->stop_arg
- 1,
3261 AO_UC_Load_A_Register
);
3262 devpriv
->stc_writew(dev
, AO_UC_Load
,
3263 AO_Command_1_Register
);
3265 devpriv
->stc_writel(dev
, cmd
->stop_arg
,
3266 AO_UC_Load_A_Register
);
3267 devpriv
->stc_writew(dev
, AO_UC_Load
,
3268 AO_Command_1_Register
);
3269 devpriv
->stc_writel(dev
, cmd
->stop_arg
- 1,
3270 AO_UC_Load_A_Register
);
3274 devpriv
->stc_writel(dev
, 0xffffff, AO_UC_Load_A_Register
);
3275 devpriv
->stc_writew(dev
, AO_UC_Load
, AO_Command_1_Register
);
3276 devpriv
->stc_writel(dev
, 0xffffff, AO_UC_Load_A_Register
);
3279 devpriv
->stc_writel(dev
, 0, AO_UC_Load_A_Register
);
3280 devpriv
->stc_writew(dev
, AO_UC_Load
, AO_Command_1_Register
);
3281 devpriv
->stc_writel(dev
, cmd
->stop_arg
, AO_UC_Load_A_Register
);
3284 devpriv
->ao_mode1
&=
3285 ~(AO_UI_Source_Select(0x1f) | AO_UI_Source_Polarity
|
3286 AO_UPDATE_Source_Select(0x1f) | AO_UPDATE_Source_Polarity
);
3287 switch (cmd
->scan_begin_src
) {
3289 devpriv
->ao_cmd2
&= ~AO_BC_Gate_Enable
;
3291 ni_ns_to_timer(dev
, cmd
->scan_begin_arg
,
3292 TRIG_ROUND_NEAREST
);
3293 devpriv
->stc_writel(dev
, 1, AO_UI_Load_A_Register
);
3294 devpriv
->stc_writew(dev
, AO_UI_Load
, AO_Command_1_Register
);
3295 devpriv
->stc_writel(dev
, trigvar
, AO_UI_Load_A_Register
);
3298 devpriv
->ao_mode1
|=
3299 AO_UPDATE_Source_Select(cmd
->scan_begin_arg
);
3300 if (cmd
->scan_begin_arg
& CR_INVERT
)
3301 devpriv
->ao_mode1
|= AO_UPDATE_Source_Polarity
;
3302 devpriv
->ao_cmd2
|= AO_BC_Gate_Enable
;
3308 devpriv
->stc_writew(dev
, devpriv
->ao_cmd2
, AO_Command_2_Register
);
3309 devpriv
->stc_writew(dev
, devpriv
->ao_mode1
, AO_Mode_1_Register
);
3310 devpriv
->ao_mode2
&=
3311 ~(AO_UI_Reload_Mode(3) | AO_UI_Initial_Load_Source
);
3312 devpriv
->stc_writew(dev
, devpriv
->ao_mode2
, AO_Mode_2_Register
);
3314 if (cmd
->scan_end_arg
> 1) {
3315 devpriv
->ao_mode1
|= AO_Multiple_Channels
;
3316 devpriv
->stc_writew(dev
,
3317 AO_Number_Of_Channels(cmd
->scan_end_arg
-
3319 AO_UPDATE_Output_Select
3320 (AO_Update_Output_High_Z
),
3321 AO_Output_Control_Register
);
3324 devpriv
->ao_mode1
&= ~AO_Multiple_Channels
;
3325 bits
= AO_UPDATE_Output_Select(AO_Update_Output_High_Z
);
3326 if (board
->reg_type
&
3327 (ni_reg_m_series_mask
| ni_reg_6xxx_mask
)) {
3328 bits
|= AO_Number_Of_Channels(0);
3331 AO_Number_Of_Channels(CR_CHAN(cmd
->chanlist
[0]));
3333 devpriv
->stc_writew(dev
, bits
, AO_Output_Control_Register
);
3335 devpriv
->stc_writew(dev
, devpriv
->ao_mode1
, AO_Mode_1_Register
);
3337 devpriv
->stc_writew(dev
, AO_DAC0_Update_Mode
| AO_DAC1_Update_Mode
,
3338 AO_Command_1_Register
);
3340 devpriv
->ao_mode3
|= AO_Stop_On_Overrun_Error
;
3341 devpriv
->stc_writew(dev
, devpriv
->ao_mode3
, AO_Mode_3_Register
);
3343 devpriv
->ao_mode2
&= ~AO_FIFO_Mode_Mask
;
3345 devpriv
->ao_mode2
|= AO_FIFO_Mode_HF_to_F
;
3347 devpriv
->ao_mode2
|= AO_FIFO_Mode_HF
;
3349 devpriv
->ao_mode2
&= ~AO_FIFO_Retransmit_Enable
;
3350 devpriv
->stc_writew(dev
, devpriv
->ao_mode2
, AO_Mode_2_Register
);
3352 bits
= AO_BC_Source_Select
| AO_UPDATE_Pulse_Width
|
3353 AO_TMRDACWR_Pulse_Width
;
3354 if (board
->ao_fifo_depth
)
3355 bits
|= AO_FIFO_Enable
;
3357 bits
|= AO_DMA_PIO_Control
;
3359 /* F Hess: windows driver does not set AO_Number_Of_DAC_Packages bit for 6281,
3360 verified with bus analyzer. */
3361 if (board
->reg_type
& ni_reg_m_series_mask
)
3362 bits
|= AO_Number_Of_DAC_Packages
;
3364 devpriv
->stc_writew(dev
, bits
, AO_Personal_Register
);
3365 /* enable sending of ao dma requests */
3366 devpriv
->stc_writew(dev
, AO_AOFREQ_Enable
, AO_Start_Select_Register
);
3368 devpriv
->stc_writew(dev
, AO_Configuration_End
, Joint_Reset_Register
);
3370 if (cmd
->stop_src
== TRIG_COUNT
) {
3371 devpriv
->stc_writew(dev
, AO_BC_TC_Interrupt_Ack
,
3372 Interrupt_B_Ack_Register
);
3373 ni_set_bits(dev
, Interrupt_B_Enable_Register
,
3374 AO_BC_TC_Interrupt_Enable
, 1);
3377 s
->async
->inttrig
= &ni_ao_inttrig
;
3382 static int ni_ao_cmdtest(struct comedi_device
*dev
, struct comedi_subdevice
*s
,
3383 struct comedi_cmd
*cmd
)
3385 const struct ni_board_struct
*board
= comedi_board(dev
);
3386 struct ni_private
*devpriv
= dev
->private;
3390 /* Step 1 : check if triggers are trivially valid */
3392 if ((cmd
->flags
& CMDF_WRITE
) == 0)
3393 cmd
->flags
|= CMDF_WRITE
;
3395 err
|= cfc_check_trigger_src(&cmd
->start_src
, TRIG_INT
| TRIG_EXT
);
3396 err
|= cfc_check_trigger_src(&cmd
->scan_begin_src
,
3397 TRIG_TIMER
| TRIG_EXT
);
3398 err
|= cfc_check_trigger_src(&cmd
->convert_src
, TRIG_NOW
);
3399 err
|= cfc_check_trigger_src(&cmd
->scan_end_src
, TRIG_COUNT
);
3400 err
|= cfc_check_trigger_src(&cmd
->stop_src
, TRIG_COUNT
| TRIG_NONE
);
3405 /* Step 2a : make sure trigger sources are unique */
3407 err
|= cfc_check_trigger_is_unique(cmd
->start_src
);
3408 err
|= cfc_check_trigger_is_unique(cmd
->scan_begin_src
);
3409 err
|= cfc_check_trigger_is_unique(cmd
->stop_src
);
3411 /* Step 2b : and mutually compatible */
3416 /* Step 3: check if arguments are trivially valid */
3418 if (cmd
->start_src
== TRIG_EXT
) {
3419 /* external trigger */
3420 unsigned int tmp
= CR_CHAN(cmd
->start_arg
);
3424 tmp
|= (cmd
->start_arg
& (CR_INVERT
| CR_EDGE
));
3425 err
|= cfc_check_trigger_arg_is(&cmd
->start_arg
, tmp
);
3427 /* true for both TRIG_NOW and TRIG_INT */
3428 err
|= cfc_check_trigger_arg_is(&cmd
->start_arg
, 0);
3431 if (cmd
->scan_begin_src
== TRIG_TIMER
) {
3432 err
|= cfc_check_trigger_arg_min(&cmd
->scan_begin_arg
,
3434 err
|= cfc_check_trigger_arg_max(&cmd
->scan_begin_arg
,
3435 devpriv
->clock_ns
* 0xffffff);
3438 err
|= cfc_check_trigger_arg_is(&cmd
->convert_arg
, 0);
3439 err
|= cfc_check_trigger_arg_is(&cmd
->scan_end_arg
, cmd
->chanlist_len
);
3441 if (cmd
->stop_src
== TRIG_COUNT
)
3442 err
|= cfc_check_trigger_arg_max(&cmd
->stop_arg
, 0x00ffffff);
3443 else /* TRIG_NONE */
3444 err
|= cfc_check_trigger_arg_is(&cmd
->stop_arg
, 0);
3449 /* step 4: fix up any arguments */
3450 if (cmd
->scan_begin_src
== TRIG_TIMER
) {
3451 tmp
= cmd
->scan_begin_arg
;
3452 cmd
->scan_begin_arg
=
3453 ni_timer_to_ns(dev
, ni_ns_to_timer(dev
,
3454 cmd
->scan_begin_arg
,
3458 if (tmp
!= cmd
->scan_begin_arg
)
3464 /* step 5: fix up chanlist */
3472 static int ni_ao_reset(struct comedi_device
*dev
, struct comedi_subdevice
*s
)
3474 const struct ni_board_struct
*board
= comedi_board(dev
);
3475 struct ni_private
*devpriv
= dev
->private;
3477 /* devpriv->ao0p=0x0000; */
3478 /* ni_writew(devpriv->ao0p,AO_Configuration); */
3480 /* devpriv->ao1p=AO_Channel(1); */
3481 /* ni_writew(devpriv->ao1p,AO_Configuration); */
3483 ni_release_ao_mite_channel(dev
);
3485 devpriv
->stc_writew(dev
, AO_Configuration_Start
, Joint_Reset_Register
);
3486 devpriv
->stc_writew(dev
, AO_Disarm
, AO_Command_1_Register
);
3487 ni_set_bits(dev
, Interrupt_B_Enable_Register
, ~0, 0);
3488 devpriv
->stc_writew(dev
, AO_BC_Source_Select
, AO_Personal_Register
);
3489 devpriv
->stc_writew(dev
, 0x3f98, Interrupt_B_Ack_Register
);
3490 devpriv
->stc_writew(dev
, AO_BC_Source_Select
| AO_UPDATE_Pulse_Width
|
3491 AO_TMRDACWR_Pulse_Width
, AO_Personal_Register
);
3492 devpriv
->stc_writew(dev
, 0, AO_Output_Control_Register
);
3493 devpriv
->stc_writew(dev
, 0, AO_Start_Select_Register
);
3494 devpriv
->ao_cmd1
= 0;
3495 devpriv
->stc_writew(dev
, devpriv
->ao_cmd1
, AO_Command_1_Register
);
3496 devpriv
->ao_cmd2
= 0;
3497 devpriv
->stc_writew(dev
, devpriv
->ao_cmd2
, AO_Command_2_Register
);
3498 devpriv
->ao_mode1
= 0;
3499 devpriv
->stc_writew(dev
, devpriv
->ao_mode1
, AO_Mode_1_Register
);
3500 devpriv
->ao_mode2
= 0;
3501 devpriv
->stc_writew(dev
, devpriv
->ao_mode2
, AO_Mode_2_Register
);
3502 if (board
->reg_type
& ni_reg_m_series_mask
)
3503 devpriv
->ao_mode3
= AO_Last_Gate_Disable
;
3505 devpriv
->ao_mode3
= 0;
3506 devpriv
->stc_writew(dev
, devpriv
->ao_mode3
, AO_Mode_3_Register
);
3507 devpriv
->ao_trigger_select
= 0;
3508 devpriv
->stc_writew(dev
, devpriv
->ao_trigger_select
,
3509 AO_Trigger_Select_Register
);
3510 if (board
->reg_type
& ni_reg_6xxx_mask
) {
3511 unsigned immediate_bits
= 0;
3513 for (i
= 0; i
< s
->n_chan
; ++i
) {
3514 immediate_bits
|= 1 << i
;
3516 ao_win_out(immediate_bits
, AO_Immediate_671x
);
3517 ao_win_out(CLEAR_WG
, AO_Misc_611x
);
3519 devpriv
->stc_writew(dev
, AO_Configuration_End
, Joint_Reset_Register
);
3526 static int ni_dio_insn_config(struct comedi_device
*dev
,
3527 struct comedi_subdevice
*s
,
3528 struct comedi_insn
*insn
, unsigned int *data
)
3530 struct ni_private
*devpriv
= dev
->private;
3533 printk("ni_dio_insn_config() chan=%d io=%d\n",
3534 CR_CHAN(insn
->chanspec
), data
[0]);
3537 case INSN_CONFIG_DIO_OUTPUT
:
3538 s
->io_bits
|= 1 << CR_CHAN(insn
->chanspec
);
3540 case INSN_CONFIG_DIO_INPUT
:
3541 s
->io_bits
&= ~(1 << CR_CHAN(insn
->chanspec
));
3543 case INSN_CONFIG_DIO_QUERY
:
3546 io_bits
& (1 << CR_CHAN(insn
->chanspec
))) ? COMEDI_OUTPUT
:
3554 devpriv
->dio_control
&= ~DIO_Pins_Dir_Mask
;
3555 devpriv
->dio_control
|= DIO_Pins_Dir(s
->io_bits
);
3556 devpriv
->stc_writew(dev
, devpriv
->dio_control
, DIO_Control_Register
);
3561 static int ni_dio_insn_bits(struct comedi_device
*dev
,
3562 struct comedi_subdevice
*s
,
3563 struct comedi_insn
*insn
, unsigned int *data
)
3565 struct ni_private
*devpriv
= dev
->private;
3568 printk("ni_dio_insn_bits() mask=0x%x bits=0x%x\n", data
[0], data
[1]);
3572 /* Perform check to make sure we're not using the
3573 serial part of the dio */
3574 if ((data
[0] & (DIO_SDIN
| DIO_SDOUT
))
3575 && devpriv
->serial_interval_ns
)
3578 s
->state
&= ~data
[0];
3579 s
->state
|= (data
[0] & data
[1]);
3580 devpriv
->dio_output
&= ~DIO_Parallel_Data_Mask
;
3581 devpriv
->dio_output
|= DIO_Parallel_Data_Out(s
->state
);
3582 devpriv
->stc_writew(dev
, devpriv
->dio_output
,
3583 DIO_Output_Register
);
3585 data
[1] = devpriv
->stc_readw(dev
, DIO_Parallel_Input_Register
);
3590 static int ni_m_series_dio_insn_config(struct comedi_device
*dev
,
3591 struct comedi_subdevice
*s
,
3592 struct comedi_insn
*insn
,
3595 struct ni_private
*devpriv __maybe_unused
= dev
->private;
3598 printk("ni_m_series_dio_insn_config() chan=%d io=%d\n",
3599 CR_CHAN(insn
->chanspec
), data
[0]);
3602 case INSN_CONFIG_DIO_OUTPUT
:
3603 s
->io_bits
|= 1 << CR_CHAN(insn
->chanspec
);
3605 case INSN_CONFIG_DIO_INPUT
:
3606 s
->io_bits
&= ~(1 << CR_CHAN(insn
->chanspec
));
3608 case INSN_CONFIG_DIO_QUERY
:
3611 io_bits
& (1 << CR_CHAN(insn
->chanspec
))) ? COMEDI_OUTPUT
:
3619 ni_writel(s
->io_bits
, M_Offset_DIO_Direction
);
3624 static int ni_m_series_dio_insn_bits(struct comedi_device
*dev
,
3625 struct comedi_subdevice
*s
,
3626 struct comedi_insn
*insn
,
3629 struct ni_private
*devpriv __maybe_unused
= dev
->private;
3632 printk("ni_m_series_dio_insn_bits() mask=0x%x bits=0x%x\n", data
[0],
3637 s
->state
&= ~data
[0];
3638 s
->state
|= (data
[0] & data
[1]);
3639 ni_writel(s
->state
, M_Offset_Static_Digital_Output
);
3641 data
[1] = ni_readl(M_Offset_Static_Digital_Input
);
3646 static int ni_cdio_cmdtest(struct comedi_device
*dev
,
3647 struct comedi_subdevice
*s
, struct comedi_cmd
*cmd
)
3653 /* Step 1 : check if triggers are trivially valid */
3655 err
|= cfc_check_trigger_src(&cmd
->start_src
, TRIG_INT
);
3656 err
|= cfc_check_trigger_src(&cmd
->scan_begin_src
, TRIG_EXT
);
3657 err
|= cfc_check_trigger_src(&cmd
->convert_src
, TRIG_NOW
);
3658 err
|= cfc_check_trigger_src(&cmd
->scan_end_src
, TRIG_COUNT
);
3659 err
|= cfc_check_trigger_src(&cmd
->stop_src
, TRIG_NONE
);
3664 /* Step 2a : make sure trigger sources are unique */
3665 /* Step 2b : and mutually compatible */
3670 /* Step 3: check if arguments are trivially valid */
3672 err
|= cfc_check_trigger_arg_is(&cmd
->start_arg
, 0);
3674 tmp
= cmd
->scan_begin_arg
;
3675 tmp
&= CR_PACK_FLAGS(CDO_Sample_Source_Select_Mask
, 0, 0, CR_INVERT
);
3676 if (tmp
!= cmd
->scan_begin_arg
)
3679 err
|= cfc_check_trigger_arg_is(&cmd
->convert_arg
, 0);
3680 err
|= cfc_check_trigger_arg_is(&cmd
->scan_end_arg
, cmd
->chanlist_len
);
3681 err
|= cfc_check_trigger_arg_is(&cmd
->stop_arg
, 0);
3686 /* step 4: fix up any arguments */
3691 /* step 5: check chanlist */
3693 for (i
= 0; i
< cmd
->chanlist_len
; ++i
) {
3694 if (cmd
->chanlist
[i
] != i
)
3704 static int ni_cdio_cmd(struct comedi_device
*dev
, struct comedi_subdevice
*s
)
3706 struct ni_private
*devpriv __maybe_unused
= dev
->private;
3707 const struct comedi_cmd
*cmd
= &s
->async
->cmd
;
3708 unsigned cdo_mode_bits
= CDO_FIFO_Mode_Bit
| CDO_Halt_On_Error_Bit
;
3711 ni_writel(CDO_Reset_Bit
, M_Offset_CDIO_Command
);
3712 switch (cmd
->scan_begin_src
) {
3715 CR_CHAN(cmd
->scan_begin_arg
) &
3716 CDO_Sample_Source_Select_Mask
;
3722 if (cmd
->scan_begin_arg
& CR_INVERT
)
3723 cdo_mode_bits
|= CDO_Polarity_Bit
;
3724 ni_writel(cdo_mode_bits
, M_Offset_CDO_Mode
);
3726 ni_writel(s
->state
, M_Offset_CDO_FIFO_Data
);
3727 ni_writel(CDO_SW_Update_Bit
, M_Offset_CDIO_Command
);
3728 ni_writel(s
->io_bits
, M_Offset_CDO_Mask_Enable
);
3731 "attempted to run digital output command with no lines configured as outputs");
3734 retval
= ni_request_cdo_mite_channel(dev
);
3738 s
->async
->inttrig
= &ni_cdo_inttrig
;
3742 static int ni_cdo_inttrig(struct comedi_device
*dev
, struct comedi_subdevice
*s
,
3743 unsigned int trignum
)
3746 struct ni_private
*devpriv
= dev
->private;
3747 unsigned long flags
;
3751 const unsigned timeout
= 1000;
3753 s
->async
->inttrig
= NULL
;
3755 /* read alloc the entire buffer */
3756 comedi_buf_read_alloc(s
->async
, s
->async
->prealloc_bufsz
);
3759 spin_lock_irqsave(&devpriv
->mite_channel_lock
, flags
);
3760 if (devpriv
->cdo_mite_chan
) {
3761 mite_prep_dma(devpriv
->cdo_mite_chan
, 32, 32);
3762 mite_dma_arm(devpriv
->cdo_mite_chan
);
3764 comedi_error(dev
, "BUG: no cdo mite channel?");
3767 spin_unlock_irqrestore(&devpriv
->mite_channel_lock
, flags
);
3772 * XXX not sure what interrupt C group does
3773 * ni_writeb(Interrupt_Group_C_Enable_Bit,
3774 * M_Offset_Interrupt_C_Enable); wait for dma to fill output fifo
3776 for (i
= 0; i
< timeout
; ++i
) {
3777 if (ni_readl(M_Offset_CDIO_Status
) & CDO_FIFO_Full_Bit
)
3782 comedi_error(dev
, "dma failed to fill cdo fifo!");
3783 ni_cdio_cancel(dev
, s
);
3786 ni_writel(CDO_Arm_Bit
| CDO_Error_Interrupt_Enable_Set_Bit
|
3787 CDO_Empty_FIFO_Interrupt_Enable_Set_Bit
,
3788 M_Offset_CDIO_Command
);
3792 static int ni_cdio_cancel(struct comedi_device
*dev
, struct comedi_subdevice
*s
)
3794 struct ni_private
*devpriv __maybe_unused
= dev
->private;
3796 ni_writel(CDO_Disarm_Bit
| CDO_Error_Interrupt_Enable_Clear_Bit
|
3797 CDO_Empty_FIFO_Interrupt_Enable_Clear_Bit
|
3798 CDO_FIFO_Request_Interrupt_Enable_Clear_Bit
,
3799 M_Offset_CDIO_Command
);
3801 * XXX not sure what interrupt C group does ni_writeb(0,
3802 * M_Offset_Interrupt_C_Enable);
3804 ni_writel(0, M_Offset_CDO_Mask_Enable
);
3805 ni_release_cdo_mite_channel(dev
);
3809 static void handle_cdio_interrupt(struct comedi_device
*dev
)
3811 const struct ni_board_struct
*board
= comedi_board(dev
);
3812 struct ni_private
*devpriv __maybe_unused
= dev
->private;
3813 unsigned cdio_status
;
3814 struct comedi_subdevice
*s
= &dev
->subdevices
[NI_DIO_SUBDEV
];
3816 unsigned long flags
;
3819 if ((board
->reg_type
& ni_reg_m_series_mask
) == 0) {
3823 spin_lock_irqsave(&devpriv
->mite_channel_lock
, flags
);
3824 if (devpriv
->cdo_mite_chan
) {
3825 unsigned cdo_mite_status
=
3826 mite_get_status(devpriv
->cdo_mite_chan
);
3827 if (cdo_mite_status
& CHSR_LINKC
) {
3829 devpriv
->mite
->mite_io_addr
+
3830 MITE_CHOR(devpriv
->cdo_mite_chan
->channel
));
3832 mite_sync_output_dma(devpriv
->cdo_mite_chan
, s
->async
);
3834 spin_unlock_irqrestore(&devpriv
->mite_channel_lock
, flags
);
3837 cdio_status
= ni_readl(M_Offset_CDIO_Status
);
3838 if (cdio_status
& (CDO_Overrun_Bit
| CDO_Underflow_Bit
)) {
3839 /* printk("cdio error: statux=0x%x\n", cdio_status); */
3840 ni_writel(CDO_Error_Interrupt_Confirm_Bit
, M_Offset_CDIO_Command
); /* XXX just guessing this is needed and does something useful */
3841 s
->async
->events
|= COMEDI_CB_OVERFLOW
;
3843 if (cdio_status
& CDO_FIFO_Empty_Bit
) {
3844 /* printk("cdio fifo empty\n"); */
3845 ni_writel(CDO_Empty_FIFO_Interrupt_Enable_Clear_Bit
,
3846 M_Offset_CDIO_Command
);
3847 /* s->async->events |= COMEDI_CB_EOA; */
3852 static int ni_serial_insn_config(struct comedi_device
*dev
,
3853 struct comedi_subdevice
*s
,
3854 struct comedi_insn
*insn
, unsigned int *data
)
3856 struct ni_private
*devpriv
= dev
->private;
3858 unsigned char byte_out
, byte_in
= 0;
3864 case INSN_CONFIG_SERIAL_CLOCK
:
3867 printk("SPI serial clock Config cd\n", data
[1]);
3869 devpriv
->serial_hw_mode
= 1;
3870 devpriv
->dio_control
|= DIO_HW_Serial_Enable
;
3872 if (data
[1] == SERIAL_DISABLED
) {
3873 devpriv
->serial_hw_mode
= 0;
3874 devpriv
->dio_control
&= ~(DIO_HW_Serial_Enable
|
3875 DIO_Software_Serial_Control
);
3876 data
[1] = SERIAL_DISABLED
;
3877 devpriv
->serial_interval_ns
= data
[1];
3878 } else if (data
[1] <= SERIAL_600NS
) {
3879 /* Warning: this clock speed is too fast to reliably
3881 devpriv
->dio_control
&= ~DIO_HW_Serial_Timebase
;
3882 devpriv
->clock_and_fout
|= Slow_Internal_Timebase
;
3883 devpriv
->clock_and_fout
&= ~DIO_Serial_Out_Divide_By_2
;
3884 data
[1] = SERIAL_600NS
;
3885 devpriv
->serial_interval_ns
= data
[1];
3886 } else if (data
[1] <= SERIAL_1_2US
) {
3887 devpriv
->dio_control
&= ~DIO_HW_Serial_Timebase
;
3888 devpriv
->clock_and_fout
|= Slow_Internal_Timebase
|
3889 DIO_Serial_Out_Divide_By_2
;
3890 data
[1] = SERIAL_1_2US
;
3891 devpriv
->serial_interval_ns
= data
[1];
3892 } else if (data
[1] <= SERIAL_10US
) {
3893 devpriv
->dio_control
|= DIO_HW_Serial_Timebase
;
3894 devpriv
->clock_and_fout
|= Slow_Internal_Timebase
|
3895 DIO_Serial_Out_Divide_By_2
;
3896 /* Note: DIO_Serial_Out_Divide_By_2 only affects
3897 600ns/1.2us. If you turn divide_by_2 off with the
3898 slow clock, you will still get 10us, except then
3899 all your delays are wrong. */
3900 data
[1] = SERIAL_10US
;
3901 devpriv
->serial_interval_ns
= data
[1];
3903 devpriv
->dio_control
&= ~(DIO_HW_Serial_Enable
|
3904 DIO_Software_Serial_Control
);
3905 devpriv
->serial_hw_mode
= 0;
3906 data
[1] = (data
[1] / 1000) * 1000;
3907 devpriv
->serial_interval_ns
= data
[1];
3910 devpriv
->stc_writew(dev
, devpriv
->dio_control
,
3911 DIO_Control_Register
);
3912 devpriv
->stc_writew(dev
, devpriv
->clock_and_fout
,
3913 Clock_and_FOUT_Register
);
3918 case INSN_CONFIG_BIDIRECTIONAL_DATA
:
3920 if (devpriv
->serial_interval_ns
== 0) {
3924 byte_out
= data
[1] & 0xFF;
3926 if (devpriv
->serial_hw_mode
) {
3927 err
= ni_serial_hw_readwrite8(dev
, s
, byte_out
,
3929 } else if (devpriv
->serial_interval_ns
> 0) {
3930 err
= ni_serial_sw_readwrite8(dev
, s
, byte_out
,
3933 printk("ni_serial_insn_config: serial disabled!\n");
3938 data
[1] = byte_in
& 0xFF;
3948 static int ni_serial_hw_readwrite8(struct comedi_device
*dev
,
3949 struct comedi_subdevice
*s
,
3950 unsigned char data_out
,
3951 unsigned char *data_in
)
3953 struct ni_private
*devpriv
= dev
->private;
3954 unsigned int status1
;
3955 int err
= 0, count
= 20;
3958 printk("ni_serial_hw_readwrite8: outputting 0x%x\n", data_out
);
3961 devpriv
->dio_output
&= ~DIO_Serial_Data_Mask
;
3962 devpriv
->dio_output
|= DIO_Serial_Data_Out(data_out
);
3963 devpriv
->stc_writew(dev
, devpriv
->dio_output
, DIO_Output_Register
);
3965 status1
= devpriv
->stc_readw(dev
, Joint_Status_1_Register
);
3966 if (status1
& DIO_Serial_IO_In_Progress_St
) {
3971 devpriv
->dio_control
|= DIO_HW_Serial_Start
;
3972 devpriv
->stc_writew(dev
, devpriv
->dio_control
, DIO_Control_Register
);
3973 devpriv
->dio_control
&= ~DIO_HW_Serial_Start
;
3975 /* Wait until STC says we're done, but don't loop infinitely. */
3977 devpriv
->stc_readw(dev
,
3978 Joint_Status_1_Register
)) &
3979 DIO_Serial_IO_In_Progress_St
) {
3980 /* Delay one bit per loop */
3981 udelay((devpriv
->serial_interval_ns
+ 999) / 1000);
3984 ("ni_serial_hw_readwrite8: SPI serial I/O didn't finish in time!\n");
3990 /* Delay for last bit. This delay is absolutely necessary, because
3991 DIO_Serial_IO_In_Progress_St goes high one bit too early. */
3992 udelay((devpriv
->serial_interval_ns
+ 999) / 1000);
3994 if (data_in
!= NULL
) {
3995 *data_in
= devpriv
->stc_readw(dev
, DIO_Serial_Input_Register
);
3997 printk("ni_serial_hw_readwrite8: inputted 0x%x\n", *data_in
);
4002 devpriv
->stc_writew(dev
, devpriv
->dio_control
, DIO_Control_Register
);
4007 static int ni_serial_sw_readwrite8(struct comedi_device
*dev
,
4008 struct comedi_subdevice
*s
,
4009 unsigned char data_out
,
4010 unsigned char *data_in
)
4012 struct ni_private
*devpriv
= dev
->private;
4013 unsigned char mask
, input
= 0;
4016 printk("ni_serial_sw_readwrite8: outputting 0x%x\n", data_out
);
4019 /* Wait for one bit before transfer */
4020 udelay((devpriv
->serial_interval_ns
+ 999) / 1000);
4022 for (mask
= 0x80; mask
; mask
>>= 1) {
4023 /* Output current bit; note that we cannot touch s->state
4024 because it is a per-subdevice field, and serial is
4025 a separate subdevice from DIO. */
4026 devpriv
->dio_output
&= ~DIO_SDOUT
;
4027 if (data_out
& mask
) {
4028 devpriv
->dio_output
|= DIO_SDOUT
;
4030 devpriv
->stc_writew(dev
, devpriv
->dio_output
,
4031 DIO_Output_Register
);
4033 /* Assert SDCLK (active low, inverted), wait for half of
4034 the delay, deassert SDCLK, and wait for the other half. */
4035 devpriv
->dio_control
|= DIO_Software_Serial_Control
;
4036 devpriv
->stc_writew(dev
, devpriv
->dio_control
,
4037 DIO_Control_Register
);
4039 udelay((devpriv
->serial_interval_ns
+ 999) / 2000);
4041 devpriv
->dio_control
&= ~DIO_Software_Serial_Control
;
4042 devpriv
->stc_writew(dev
, devpriv
->dio_control
,
4043 DIO_Control_Register
);
4045 udelay((devpriv
->serial_interval_ns
+ 999) / 2000);
4047 /* Input current bit */
4048 if (devpriv
->stc_readw(dev
,
4049 DIO_Parallel_Input_Register
) & DIO_SDIN
)
4051 /* printk("DIO_P_I_R: 0x%x\n", devpriv->stc_readw(dev, DIO_Parallel_Input_Register)); */
4056 printk("ni_serial_sw_readwrite8: inputted 0x%x\n", input
);
4064 static void mio_common_detach(struct comedi_device
*dev
)
4066 struct ni_private
*devpriv
= dev
->private;
4069 if (devpriv
->counter_dev
) {
4070 ni_gpct_device_destroy(devpriv
->counter_dev
);
4073 comedi_spriv_free(dev
, NI_8255_DIO_SUBDEV
);
4076 static void init_ao_67xx(struct comedi_device
*dev
, struct comedi_subdevice
*s
)
4080 for (i
= 0; i
< s
->n_chan
; i
++) {
4081 ni_ao_win_outw(dev
, AO_Channel(i
) | 0x0,
4082 AO_Configuration_2_67xx
);
4084 ao_win_out(0x0, AO_Later_Single_Point_Updates
);
4087 static unsigned ni_gpct_to_stc_register(enum ni_gpct_register reg
)
4089 unsigned stc_register
;
4091 case NITIO_G0_Autoincrement_Reg
:
4092 stc_register
= G_Autoincrement_Register(0);
4094 case NITIO_G1_Autoincrement_Reg
:
4095 stc_register
= G_Autoincrement_Register(1);
4097 case NITIO_G0_Command_Reg
:
4098 stc_register
= G_Command_Register(0);
4100 case NITIO_G1_Command_Reg
:
4101 stc_register
= G_Command_Register(1);
4103 case NITIO_G0_HW_Save_Reg
:
4104 stc_register
= G_HW_Save_Register(0);
4106 case NITIO_G1_HW_Save_Reg
:
4107 stc_register
= G_HW_Save_Register(1);
4109 case NITIO_G0_SW_Save_Reg
:
4110 stc_register
= G_Save_Register(0);
4112 case NITIO_G1_SW_Save_Reg
:
4113 stc_register
= G_Save_Register(1);
4115 case NITIO_G0_Mode_Reg
:
4116 stc_register
= G_Mode_Register(0);
4118 case NITIO_G1_Mode_Reg
:
4119 stc_register
= G_Mode_Register(1);
4121 case NITIO_G0_LoadA_Reg
:
4122 stc_register
= G_Load_A_Register(0);
4124 case NITIO_G1_LoadA_Reg
:
4125 stc_register
= G_Load_A_Register(1);
4127 case NITIO_G0_LoadB_Reg
:
4128 stc_register
= G_Load_B_Register(0);
4130 case NITIO_G1_LoadB_Reg
:
4131 stc_register
= G_Load_B_Register(1);
4133 case NITIO_G0_Input_Select_Reg
:
4134 stc_register
= G_Input_Select_Register(0);
4136 case NITIO_G1_Input_Select_Reg
:
4137 stc_register
= G_Input_Select_Register(1);
4139 case NITIO_G01_Status_Reg
:
4140 stc_register
= G_Status_Register
;
4142 case NITIO_G01_Joint_Reset_Reg
:
4143 stc_register
= Joint_Reset_Register
;
4145 case NITIO_G01_Joint_Status1_Reg
:
4146 stc_register
= Joint_Status_1_Register
;
4148 case NITIO_G01_Joint_Status2_Reg
:
4149 stc_register
= Joint_Status_2_Register
;
4151 case NITIO_G0_Interrupt_Acknowledge_Reg
:
4152 stc_register
= Interrupt_A_Ack_Register
;
4154 case NITIO_G1_Interrupt_Acknowledge_Reg
:
4155 stc_register
= Interrupt_B_Ack_Register
;
4157 case NITIO_G0_Status_Reg
:
4158 stc_register
= AI_Status_1_Register
;
4160 case NITIO_G1_Status_Reg
:
4161 stc_register
= AO_Status_1_Register
;
4163 case NITIO_G0_Interrupt_Enable_Reg
:
4164 stc_register
= Interrupt_A_Enable_Register
;
4166 case NITIO_G1_Interrupt_Enable_Reg
:
4167 stc_register
= Interrupt_B_Enable_Register
;
4170 printk("%s: unhandled register 0x%x in switch.\n",
4176 return stc_register
;
4179 static void ni_gpct_write_register(struct ni_gpct
*counter
, unsigned bits
,
4180 enum ni_gpct_register reg
)
4182 struct comedi_device
*dev
= counter
->counter_dev
->dev
;
4183 struct ni_private
*devpriv
= dev
->private;
4184 unsigned stc_register
;
4185 /* bits in the join reset register which are relevant to counters */
4186 static const unsigned gpct_joint_reset_mask
= G0_Reset
| G1_Reset
;
4187 static const unsigned gpct_interrupt_a_enable_mask
=
4188 G0_Gate_Interrupt_Enable
| G0_TC_Interrupt_Enable
;
4189 static const unsigned gpct_interrupt_b_enable_mask
=
4190 G1_Gate_Interrupt_Enable
| G1_TC_Interrupt_Enable
;
4193 /* m-series-only registers */
4194 case NITIO_G0_Counting_Mode_Reg
:
4195 ni_writew(bits
, M_Offset_G0_Counting_Mode
);
4197 case NITIO_G1_Counting_Mode_Reg
:
4198 ni_writew(bits
, M_Offset_G1_Counting_Mode
);
4200 case NITIO_G0_Second_Gate_Reg
:
4201 ni_writew(bits
, M_Offset_G0_Second_Gate
);
4203 case NITIO_G1_Second_Gate_Reg
:
4204 ni_writew(bits
, M_Offset_G1_Second_Gate
);
4206 case NITIO_G0_DMA_Config_Reg
:
4207 ni_writew(bits
, M_Offset_G0_DMA_Config
);
4209 case NITIO_G1_DMA_Config_Reg
:
4210 ni_writew(bits
, M_Offset_G1_DMA_Config
);
4212 case NITIO_G0_ABZ_Reg
:
4213 ni_writew(bits
, M_Offset_G0_MSeries_ABZ
);
4215 case NITIO_G1_ABZ_Reg
:
4216 ni_writew(bits
, M_Offset_G1_MSeries_ABZ
);
4219 /* 32 bit registers */
4220 case NITIO_G0_LoadA_Reg
:
4221 case NITIO_G1_LoadA_Reg
:
4222 case NITIO_G0_LoadB_Reg
:
4223 case NITIO_G1_LoadB_Reg
:
4224 stc_register
= ni_gpct_to_stc_register(reg
);
4225 devpriv
->stc_writel(dev
, bits
, stc_register
);
4228 /* 16 bit registers */
4229 case NITIO_G0_Interrupt_Enable_Reg
:
4230 BUG_ON(bits
& ~gpct_interrupt_a_enable_mask
);
4231 ni_set_bitfield(dev
, Interrupt_A_Enable_Register
,
4232 gpct_interrupt_a_enable_mask
, bits
);
4234 case NITIO_G1_Interrupt_Enable_Reg
:
4235 BUG_ON(bits
& ~gpct_interrupt_b_enable_mask
);
4236 ni_set_bitfield(dev
, Interrupt_B_Enable_Register
,
4237 gpct_interrupt_b_enable_mask
, bits
);
4239 case NITIO_G01_Joint_Reset_Reg
:
4240 BUG_ON(bits
& ~gpct_joint_reset_mask
);
4243 stc_register
= ni_gpct_to_stc_register(reg
);
4244 devpriv
->stc_writew(dev
, bits
, stc_register
);
4248 static unsigned ni_gpct_read_register(struct ni_gpct
*counter
,
4249 enum ni_gpct_register reg
)
4251 struct comedi_device
*dev
= counter
->counter_dev
->dev
;
4252 struct ni_private
*devpriv
= dev
->private;
4253 unsigned stc_register
;
4256 /* m-series only registers */
4257 case NITIO_G0_DMA_Status_Reg
:
4258 return ni_readw(M_Offset_G0_DMA_Status
);
4260 case NITIO_G1_DMA_Status_Reg
:
4261 return ni_readw(M_Offset_G1_DMA_Status
);
4264 /* 32 bit registers */
4265 case NITIO_G0_HW_Save_Reg
:
4266 case NITIO_G1_HW_Save_Reg
:
4267 case NITIO_G0_SW_Save_Reg
:
4268 case NITIO_G1_SW_Save_Reg
:
4269 stc_register
= ni_gpct_to_stc_register(reg
);
4270 return devpriv
->stc_readl(dev
, stc_register
);
4273 /* 16 bit registers */
4275 stc_register
= ni_gpct_to_stc_register(reg
);
4276 return devpriv
->stc_readw(dev
, stc_register
);
4282 static int ni_freq_out_insn_read(struct comedi_device
*dev
,
4283 struct comedi_subdevice
*s
,
4284 struct comedi_insn
*insn
, unsigned int *data
)
4286 struct ni_private
*devpriv
= dev
->private;
4288 data
[0] = devpriv
->clock_and_fout
& FOUT_Divider_mask
;
4292 static int ni_freq_out_insn_write(struct comedi_device
*dev
,
4293 struct comedi_subdevice
*s
,
4294 struct comedi_insn
*insn
, unsigned int *data
)
4296 struct ni_private
*devpriv
= dev
->private;
4298 devpriv
->clock_and_fout
&= ~FOUT_Enable
;
4299 devpriv
->stc_writew(dev
, devpriv
->clock_and_fout
,
4300 Clock_and_FOUT_Register
);
4301 devpriv
->clock_and_fout
&= ~FOUT_Divider_mask
;
4302 devpriv
->clock_and_fout
|= FOUT_Divider(data
[0]);
4303 devpriv
->clock_and_fout
|= FOUT_Enable
;
4304 devpriv
->stc_writew(dev
, devpriv
->clock_and_fout
,
4305 Clock_and_FOUT_Register
);
4309 static int ni_set_freq_out_clock(struct comedi_device
*dev
,
4310 unsigned int clock_source
)
4312 struct ni_private
*devpriv
= dev
->private;
4314 switch (clock_source
) {
4315 case NI_FREQ_OUT_TIMEBASE_1_DIV_2_CLOCK_SRC
:
4316 devpriv
->clock_and_fout
&= ~FOUT_Timebase_Select
;
4318 case NI_FREQ_OUT_TIMEBASE_2_CLOCK_SRC
:
4319 devpriv
->clock_and_fout
|= FOUT_Timebase_Select
;
4324 devpriv
->stc_writew(dev
, devpriv
->clock_and_fout
,
4325 Clock_and_FOUT_Register
);
4329 static void ni_get_freq_out_clock(struct comedi_device
*dev
,
4330 unsigned int *clock_source
,
4331 unsigned int *clock_period_ns
)
4333 struct ni_private
*devpriv
= dev
->private;
4335 if (devpriv
->clock_and_fout
& FOUT_Timebase_Select
) {
4336 *clock_source
= NI_FREQ_OUT_TIMEBASE_2_CLOCK_SRC
;
4337 *clock_period_ns
= TIMEBASE_2_NS
;
4339 *clock_source
= NI_FREQ_OUT_TIMEBASE_1_DIV_2_CLOCK_SRC
;
4340 *clock_period_ns
= TIMEBASE_1_NS
* 2;
4344 static int ni_freq_out_insn_config(struct comedi_device
*dev
,
4345 struct comedi_subdevice
*s
,
4346 struct comedi_insn
*insn
, unsigned int *data
)
4349 case INSN_CONFIG_SET_CLOCK_SRC
:
4350 return ni_set_freq_out_clock(dev
, data
[1]);
4352 case INSN_CONFIG_GET_CLOCK_SRC
:
4353 ni_get_freq_out_clock(dev
, &data
[1], &data
[2]);
4361 static int ni_alloc_private(struct comedi_device
*dev
)
4363 struct ni_private
*devpriv
;
4365 devpriv
= kzalloc(sizeof(*devpriv
), GFP_KERNEL
);
4368 dev
->private = devpriv
;
4370 spin_lock_init(&devpriv
->window_lock
);
4371 spin_lock_init(&devpriv
->soft_reg_copy_lock
);
4372 spin_lock_init(&devpriv
->mite_channel_lock
);
4377 static int ni_E_init(struct comedi_device
*dev
)
4379 const struct ni_board_struct
*board
= comedi_board(dev
);
4380 struct ni_private
*devpriv
= dev
->private;
4381 struct comedi_subdevice
*s
;
4383 enum ni_gpct_variant counter_variant
;
4386 if (board
->n_aochan
> MAX_N_AO_CHAN
) {
4387 printk("bug! n_aochan > MAX_N_AO_CHAN\n");
4391 ret
= comedi_alloc_subdevices(dev
, NI_NUM_SUBDEVICES
);
4395 /* analog input subdevice */
4397 s
= &dev
->subdevices
[NI_AI_SUBDEV
];
4398 dev
->read_subdev
= s
;
4399 if (board
->n_adchan
) {
4400 s
->type
= COMEDI_SUBD_AI
;
4402 SDF_READABLE
| SDF_DIFF
| SDF_DITHER
| SDF_CMD_READ
;
4403 if (board
->reg_type
!= ni_reg_611x
)
4404 s
->subdev_flags
|= SDF_GROUND
| SDF_COMMON
| SDF_OTHER
;
4405 if (board
->adbits
> 16)
4406 s
->subdev_flags
|= SDF_LSAMPL
;
4407 if (board
->reg_type
& ni_reg_m_series_mask
)
4408 s
->subdev_flags
|= SDF_SOFT_CALIBRATED
;
4409 s
->n_chan
= board
->n_adchan
;
4410 s
->len_chanlist
= 512;
4411 s
->maxdata
= (1 << board
->adbits
) - 1;
4412 s
->range_table
= ni_range_lkup
[board
->gainlkup
];
4413 s
->insn_read
= &ni_ai_insn_read
;
4414 s
->insn_config
= &ni_ai_insn_config
;
4415 s
->do_cmdtest
= &ni_ai_cmdtest
;
4416 s
->do_cmd
= &ni_ai_cmd
;
4417 s
->cancel
= &ni_ai_reset
;
4418 s
->poll
= &ni_ai_poll
;
4419 s
->munge
= &ni_ai_munge
;
4421 s
->async_dma_dir
= DMA_FROM_DEVICE
;
4424 s
->type
= COMEDI_SUBD_UNUSED
;
4427 /* analog output subdevice */
4429 s
= &dev
->subdevices
[NI_AO_SUBDEV
];
4430 if (board
->n_aochan
) {
4431 s
->type
= COMEDI_SUBD_AO
;
4432 s
->subdev_flags
= SDF_WRITABLE
| SDF_DEGLITCH
| SDF_GROUND
;
4433 if (board
->reg_type
& ni_reg_m_series_mask
)
4434 s
->subdev_flags
|= SDF_SOFT_CALIBRATED
;
4435 s
->n_chan
= board
->n_aochan
;
4436 s
->maxdata
= (1 << board
->aobits
) - 1;
4437 s
->range_table
= board
->ao_range_table
;
4438 s
->insn_read
= &ni_ao_insn_read
;
4439 if (board
->reg_type
& ni_reg_6xxx_mask
) {
4440 s
->insn_write
= &ni_ao_insn_write_671x
;
4442 s
->insn_write
= &ni_ao_insn_write
;
4444 s
->insn_config
= &ni_ao_insn_config
;
4446 if (board
->n_aochan
) {
4447 s
->async_dma_dir
= DMA_TO_DEVICE
;
4449 if (board
->ao_fifo_depth
) {
4451 dev
->write_subdev
= s
;
4452 s
->subdev_flags
|= SDF_CMD_WRITE
;
4453 s
->do_cmd
= &ni_ao_cmd
;
4454 s
->do_cmdtest
= &ni_ao_cmdtest
;
4455 s
->len_chanlist
= board
->n_aochan
;
4456 if ((board
->reg_type
& ni_reg_m_series_mask
) == 0)
4457 s
->munge
= ni_ao_munge
;
4459 s
->cancel
= &ni_ao_reset
;
4461 s
->type
= COMEDI_SUBD_UNUSED
;
4463 if ((board
->reg_type
& ni_reg_67xx_mask
))
4464 init_ao_67xx(dev
, s
);
4466 /* digital i/o subdevice */
4468 s
= &dev
->subdevices
[NI_DIO_SUBDEV
];
4469 s
->type
= COMEDI_SUBD_DIO
;
4470 s
->subdev_flags
= SDF_WRITABLE
| SDF_READABLE
;
4472 s
->io_bits
= 0; /* all bits input */
4473 s
->range_table
= &range_digital
;
4474 s
->n_chan
= board
->num_p0_dio_channels
;
4475 if (board
->reg_type
& ni_reg_m_series_mask
) {
4477 SDF_LSAMPL
| SDF_CMD_WRITE
/* | SDF_CMD_READ */ ;
4478 s
->insn_bits
= &ni_m_series_dio_insn_bits
;
4479 s
->insn_config
= &ni_m_series_dio_insn_config
;
4480 s
->do_cmd
= &ni_cdio_cmd
;
4481 s
->do_cmdtest
= &ni_cdio_cmdtest
;
4482 s
->cancel
= &ni_cdio_cancel
;
4483 s
->async_dma_dir
= DMA_BIDIRECTIONAL
;
4484 s
->len_chanlist
= s
->n_chan
;
4486 ni_writel(CDO_Reset_Bit
| CDI_Reset_Bit
, M_Offset_CDIO_Command
);
4487 ni_writel(s
->io_bits
, M_Offset_DIO_Direction
);
4489 s
->insn_bits
= &ni_dio_insn_bits
;
4490 s
->insn_config
= &ni_dio_insn_config
;
4491 devpriv
->dio_control
= DIO_Pins_Dir(s
->io_bits
);
4492 ni_writew(devpriv
->dio_control
, DIO_Control_Register
);
4496 s
= &dev
->subdevices
[NI_8255_DIO_SUBDEV
];
4497 if (board
->has_8255
) {
4498 subdev_8255_init(dev
, s
, ni_8255_callback
, (unsigned long)dev
);
4500 s
->type
= COMEDI_SUBD_UNUSED
;
4503 /* formerly general purpose counter/timer device, but no longer used */
4504 s
= &dev
->subdevices
[NI_UNUSED_SUBDEV
];
4505 s
->type
= COMEDI_SUBD_UNUSED
;
4507 /* calibration subdevice -- ai and ao */
4508 s
= &dev
->subdevices
[NI_CALIBRATION_SUBDEV
];
4509 s
->type
= COMEDI_SUBD_CALIB
;
4510 if (board
->reg_type
& ni_reg_m_series_mask
) {
4511 /* internal PWM analog output used for AI nonlinearity calibration */
4512 s
->subdev_flags
= SDF_INTERNAL
;
4513 s
->insn_config
= &ni_m_series_pwm_config
;
4516 ni_writel(0x0, M_Offset_Cal_PWM
);
4517 } else if (board
->reg_type
== ni_reg_6143
) {
4518 /* internal PWM analog output used for AI nonlinearity calibration */
4519 s
->subdev_flags
= SDF_INTERNAL
;
4520 s
->insn_config
= &ni_6143_pwm_config
;
4524 s
->subdev_flags
= SDF_WRITABLE
| SDF_INTERNAL
;
4525 s
->insn_read
= &ni_calib_insn_read
;
4526 s
->insn_write
= &ni_calib_insn_write
;
4527 caldac_setup(dev
, s
);
4531 s
= &dev
->subdevices
[NI_EEPROM_SUBDEV
];
4532 s
->type
= COMEDI_SUBD_MEMORY
;
4533 s
->subdev_flags
= SDF_READABLE
| SDF_INTERNAL
;
4535 if (board
->reg_type
& ni_reg_m_series_mask
) {
4536 s
->n_chan
= M_SERIES_EEPROM_SIZE
;
4537 s
->insn_read
= &ni_m_series_eeprom_insn_read
;
4540 s
->insn_read
= &ni_eeprom_insn_read
;
4544 s
= &dev
->subdevices
[NI_PFI_DIO_SUBDEV
];
4545 s
->type
= COMEDI_SUBD_DIO
;
4546 s
->subdev_flags
= SDF_READABLE
| SDF_WRITABLE
| SDF_INTERNAL
;
4547 if (board
->reg_type
& ni_reg_m_series_mask
) {
4550 ni_writew(s
->state
, M_Offset_PFI_DO
);
4551 for (i
= 0; i
< NUM_PFI_OUTPUT_SELECT_REGS
; ++i
) {
4552 ni_writew(devpriv
->pfi_output_select_reg
[i
],
4553 M_Offset_PFI_Output_Select(i
+ 1));
4559 if (board
->reg_type
& ni_reg_m_series_mask
) {
4560 s
->insn_bits
= &ni_pfi_insn_bits
;
4562 s
->insn_config
= &ni_pfi_insn_config
;
4563 ni_set_bits(dev
, IO_Bidirection_Pin_Register
, ~0, 0);
4565 /* cs5529 calibration adc */
4566 s
= &dev
->subdevices
[NI_CS5529_CALIBRATION_SUBDEV
];
4567 if (board
->reg_type
& ni_reg_67xx_mask
) {
4568 s
->type
= COMEDI_SUBD_AI
;
4569 s
->subdev_flags
= SDF_READABLE
| SDF_DIFF
| SDF_INTERNAL
;
4570 /* one channel for each analog output channel */
4571 s
->n_chan
= board
->n_aochan
;
4572 s
->maxdata
= (1 << 16) - 1;
4573 s
->range_table
= &range_unknown
; /* XXX */
4574 s
->insn_read
= cs5529_ai_insn_read
;
4575 s
->insn_config
= NULL
;
4578 s
->type
= COMEDI_SUBD_UNUSED
;
4582 s
= &dev
->subdevices
[NI_SERIAL_SUBDEV
];
4583 s
->type
= COMEDI_SUBD_SERIAL
;
4584 s
->subdev_flags
= SDF_READABLE
| SDF_WRITABLE
| SDF_INTERNAL
;
4587 s
->insn_config
= ni_serial_insn_config
;
4588 devpriv
->serial_interval_ns
= 0;
4589 devpriv
->serial_hw_mode
= 0;
4592 s
= &dev
->subdevices
[NI_RTSI_SUBDEV
];
4593 s
->type
= COMEDI_SUBD_DIO
;
4594 s
->subdev_flags
= SDF_READABLE
| SDF_WRITABLE
| SDF_INTERNAL
;
4597 s
->insn_bits
= ni_rtsi_insn_bits
;
4598 s
->insn_config
= ni_rtsi_insn_config
;
4601 if (board
->reg_type
& ni_reg_m_series_mask
) {
4602 counter_variant
= ni_gpct_variant_m_series
;
4604 counter_variant
= ni_gpct_variant_e_series
;
4606 devpriv
->counter_dev
= ni_gpct_device_construct(dev
,
4607 &ni_gpct_write_register
,
4608 &ni_gpct_read_register
,
4611 /* General purpose counters */
4612 for (j
= 0; j
< NUM_GPCT
; ++j
) {
4613 s
= &dev
->subdevices
[NI_GPCT_SUBDEV(j
)];
4614 s
->type
= COMEDI_SUBD_COUNTER
;
4616 SDF_READABLE
| SDF_WRITABLE
| SDF_LSAMPL
| SDF_CMD_READ
4617 /* | SDF_CMD_WRITE */ ;
4619 if (board
->reg_type
& ni_reg_m_series_mask
)
4620 s
->maxdata
= 0xffffffff;
4622 s
->maxdata
= 0xffffff;
4623 s
->insn_read
= &ni_gpct_insn_read
;
4624 s
->insn_write
= &ni_gpct_insn_write
;
4625 s
->insn_config
= &ni_gpct_insn_config
;
4626 s
->do_cmd
= &ni_gpct_cmd
;
4627 s
->len_chanlist
= 1;
4628 s
->do_cmdtest
= &ni_gpct_cmdtest
;
4629 s
->cancel
= &ni_gpct_cancel
;
4630 s
->async_dma_dir
= DMA_BIDIRECTIONAL
;
4631 s
->private = &devpriv
->counter_dev
->counters
[j
];
4633 devpriv
->counter_dev
->counters
[j
].chip_index
= 0;
4634 devpriv
->counter_dev
->counters
[j
].counter_index
= j
;
4635 ni_tio_init_counter(&devpriv
->counter_dev
->counters
[j
]);
4638 /* Frequency output */
4639 s
= &dev
->subdevices
[NI_FREQ_OUT_SUBDEV
];
4640 s
->type
= COMEDI_SUBD_COUNTER
;
4641 s
->subdev_flags
= SDF_READABLE
| SDF_WRITABLE
;
4644 s
->insn_read
= &ni_freq_out_insn_read
;
4645 s
->insn_write
= &ni_freq_out_insn_write
;
4646 s
->insn_config
= &ni_freq_out_insn_config
;
4648 /* ai configuration */
4649 s
= &dev
->subdevices
[NI_AI_SUBDEV
];
4650 ni_ai_reset(dev
, s
);
4651 if ((board
->reg_type
& ni_reg_6xxx_mask
) == 0) {
4652 /* BEAM is this needed for PCI-6143 ?? */
4653 devpriv
->clock_and_fout
=
4654 Slow_Internal_Time_Divide_By_2
|
4655 Slow_Internal_Timebase
|
4656 Clock_To_Board_Divide_By_2
|
4658 AI_Output_Divide_By_2
| AO_Output_Divide_By_2
;
4660 devpriv
->clock_and_fout
=
4661 Slow_Internal_Time_Divide_By_2
|
4662 Slow_Internal_Timebase
|
4663 Clock_To_Board_Divide_By_2
| Clock_To_Board
;
4665 devpriv
->stc_writew(dev
, devpriv
->clock_and_fout
,
4666 Clock_and_FOUT_Register
);
4668 /* analog output configuration */
4669 s
= &dev
->subdevices
[NI_AO_SUBDEV
];
4670 ni_ao_reset(dev
, s
);
4673 devpriv
->stc_writew(dev
,
4674 (IRQ_POLARITY
? Interrupt_Output_Polarity
:
4675 0) | (Interrupt_Output_On_3_Pins
& 0) |
4676 Interrupt_A_Enable
| Interrupt_B_Enable
|
4677 Interrupt_A_Output_Select(interrupt_pin
4679 Interrupt_B_Output_Select(interrupt_pin
4681 Interrupt_Control_Register
);
4685 ni_writeb(devpriv
->ai_ao_select_reg
, AI_AO_Select
);
4686 ni_writeb(devpriv
->g0_g1_select_reg
, G0_G1_Select
);
4688 if (board
->reg_type
& ni_reg_6xxx_mask
) {
4689 ni_writeb(0, Magic_611x
);
4690 } else if (board
->reg_type
& ni_reg_m_series_mask
) {
4692 for (channel
= 0; channel
< board
->n_aochan
; ++channel
) {
4693 ni_writeb(0xf, M_Offset_AO_Waveform_Order(channel
));
4695 M_Offset_AO_Reference_Attenuation(channel
));
4697 ni_writeb(0x0, M_Offset_AO_Calibration
);
4704 static int ni_8255_callback(int dir
, int port
, int data
, unsigned long arg
)
4706 struct comedi_device
*dev
= (struct comedi_device
*)arg
;
4707 struct ni_private
*devpriv __maybe_unused
= dev
->private;
4710 ni_writeb(data
, Port_A
+ 2 * port
);
4713 return ni_readb(Port_A
+ 2 * port
);
4718 presents the EEPROM as a subdevice
4721 static int ni_eeprom_insn_read(struct comedi_device
*dev
,
4722 struct comedi_subdevice
*s
,
4723 struct comedi_insn
*insn
, unsigned int *data
)
4725 data
[0] = ni_read_eeprom(dev
, CR_CHAN(insn
->chanspec
));
4731 reads bytes out of eeprom
4734 static int ni_read_eeprom(struct comedi_device
*dev
, int addr
)
4736 struct ni_private
*devpriv __maybe_unused
= dev
->private;
4740 bitstring
= 0x0300 | ((addr
& 0x100) << 3) | (addr
& 0xff);
4741 ni_writeb(0x04, Serial_Command
);
4742 for (bit
= 0x8000; bit
; bit
>>= 1) {
4743 ni_writeb(0x04 | ((bit
& bitstring
) ? 0x02 : 0),
4745 ni_writeb(0x05 | ((bit
& bitstring
) ? 0x02 : 0),
4749 for (bit
= 0x80; bit
; bit
>>= 1) {
4750 ni_writeb(0x04, Serial_Command
);
4751 ni_writeb(0x05, Serial_Command
);
4752 bitstring
|= ((ni_readb(XXX_Status
) & PROMOUT
) ? bit
: 0);
4754 ni_writeb(0x00, Serial_Command
);
4759 static int ni_m_series_eeprom_insn_read(struct comedi_device
*dev
,
4760 struct comedi_subdevice
*s
,
4761 struct comedi_insn
*insn
,
4764 struct ni_private
*devpriv
= dev
->private;
4766 data
[0] = devpriv
->eeprom_buffer
[CR_CHAN(insn
->chanspec
)];
4771 static int ni_get_pwm_config(struct comedi_device
*dev
, unsigned int *data
)
4773 struct ni_private
*devpriv
= dev
->private;
4775 data
[1] = devpriv
->pwm_up_count
* devpriv
->clock_ns
;
4776 data
[2] = devpriv
->pwm_down_count
* devpriv
->clock_ns
;
4780 static int ni_m_series_pwm_config(struct comedi_device
*dev
,
4781 struct comedi_subdevice
*s
,
4782 struct comedi_insn
*insn
, unsigned int *data
)
4784 struct ni_private
*devpriv
= dev
->private;
4785 unsigned up_count
, down_count
;
4788 case INSN_CONFIG_PWM_OUTPUT
:
4790 case TRIG_ROUND_NEAREST
:
4793 devpriv
->clock_ns
/ 2) / devpriv
->clock_ns
;
4795 case TRIG_ROUND_DOWN
:
4796 up_count
= data
[2] / devpriv
->clock_ns
;
4800 (data
[2] + devpriv
->clock_ns
-
4801 1) / devpriv
->clock_ns
;
4808 case TRIG_ROUND_NEAREST
:
4811 devpriv
->clock_ns
/ 2) / devpriv
->clock_ns
;
4813 case TRIG_ROUND_DOWN
:
4814 down_count
= data
[4] / devpriv
->clock_ns
;
4818 (data
[4] + devpriv
->clock_ns
-
4819 1) / devpriv
->clock_ns
;
4825 if (up_count
* devpriv
->clock_ns
!= data
[2] ||
4826 down_count
* devpriv
->clock_ns
!= data
[4]) {
4827 data
[2] = up_count
* devpriv
->clock_ns
;
4828 data
[4] = down_count
* devpriv
->clock_ns
;
4831 ni_writel(MSeries_Cal_PWM_High_Time_Bits(up_count
) |
4832 MSeries_Cal_PWM_Low_Time_Bits(down_count
),
4834 devpriv
->pwm_up_count
= up_count
;
4835 devpriv
->pwm_down_count
= down_count
;
4838 case INSN_CONFIG_GET_PWM_OUTPUT
:
4839 return ni_get_pwm_config(dev
, data
);
4848 static int ni_6143_pwm_config(struct comedi_device
*dev
,
4849 struct comedi_subdevice
*s
,
4850 struct comedi_insn
*insn
, unsigned int *data
)
4852 struct ni_private
*devpriv
= dev
->private;
4853 unsigned up_count
, down_count
;
4856 case INSN_CONFIG_PWM_OUTPUT
:
4858 case TRIG_ROUND_NEAREST
:
4861 devpriv
->clock_ns
/ 2) / devpriv
->clock_ns
;
4863 case TRIG_ROUND_DOWN
:
4864 up_count
= data
[2] / devpriv
->clock_ns
;
4868 (data
[2] + devpriv
->clock_ns
-
4869 1) / devpriv
->clock_ns
;
4876 case TRIG_ROUND_NEAREST
:
4879 devpriv
->clock_ns
/ 2) / devpriv
->clock_ns
;
4881 case TRIG_ROUND_DOWN
:
4882 down_count
= data
[4] / devpriv
->clock_ns
;
4886 (data
[4] + devpriv
->clock_ns
-
4887 1) / devpriv
->clock_ns
;
4893 if (up_count
* devpriv
->clock_ns
!= data
[2] ||
4894 down_count
* devpriv
->clock_ns
!= data
[4]) {
4895 data
[2] = up_count
* devpriv
->clock_ns
;
4896 data
[4] = down_count
* devpriv
->clock_ns
;
4899 ni_writel(up_count
, Calibration_HighTime_6143
);
4900 devpriv
->pwm_up_count
= up_count
;
4901 ni_writel(down_count
, Calibration_LowTime_6143
);
4902 devpriv
->pwm_down_count
= down_count
;
4905 case INSN_CONFIG_GET_PWM_OUTPUT
:
4906 return ni_get_pwm_config(dev
, data
);
4914 static void ni_write_caldac(struct comedi_device
*dev
, int addr
, int val
);
4916 calibration subdevice
4918 static int ni_calib_insn_write(struct comedi_device
*dev
,
4919 struct comedi_subdevice
*s
,
4920 struct comedi_insn
*insn
, unsigned int *data
)
4922 ni_write_caldac(dev
, CR_CHAN(insn
->chanspec
), data
[0]);
4927 static int ni_calib_insn_read(struct comedi_device
*dev
,
4928 struct comedi_subdevice
*s
,
4929 struct comedi_insn
*insn
, unsigned int *data
)
4931 struct ni_private
*devpriv
= dev
->private;
4933 data
[0] = devpriv
->caldacs
[CR_CHAN(insn
->chanspec
)];
4938 static int pack_mb88341(int addr
, int val
, int *bitstring
);
4939 static int pack_dac8800(int addr
, int val
, int *bitstring
);
4940 static int pack_dac8043(int addr
, int val
, int *bitstring
);
4941 static int pack_ad8522(int addr
, int val
, int *bitstring
);
4942 static int pack_ad8804(int addr
, int val
, int *bitstring
);
4943 static int pack_ad8842(int addr
, int val
, int *bitstring
);
4945 struct caldac_struct
{
4948 int (*packbits
) (int, int, int *);
4951 static struct caldac_struct caldacs
[] = {
4952 [mb88341
] = {12, 8, pack_mb88341
},
4953 [dac8800
] = {8, 8, pack_dac8800
},
4954 [dac8043
] = {1, 12, pack_dac8043
},
4955 [ad8522
] = {2, 12, pack_ad8522
},
4956 [ad8804
] = {12, 8, pack_ad8804
},
4957 [ad8842
] = {8, 8, pack_ad8842
},
4958 [ad8804_debug
] = {16, 8, pack_ad8804
},
4961 static void caldac_setup(struct comedi_device
*dev
, struct comedi_subdevice
*s
)
4963 const struct ni_board_struct
*board
= comedi_board(dev
);
4964 struct ni_private
*devpriv
= dev
->private;
4973 type
= board
->caldac
[0];
4974 if (type
== caldac_none
)
4976 n_bits
= caldacs
[type
].n_bits
;
4977 for (i
= 0; i
< 3; i
++) {
4978 type
= board
->caldac
[i
];
4979 if (type
== caldac_none
)
4981 if (caldacs
[type
].n_bits
!= n_bits
)
4983 n_chans
+= caldacs
[type
].n_chans
;
4986 s
->n_chan
= n_chans
;
4989 unsigned int *maxdata_list
;
4991 if (n_chans
> MAX_N_CALDACS
) {
4992 printk("BUG! MAX_N_CALDACS too small\n");
4994 s
->maxdata_list
= maxdata_list
= devpriv
->caldac_maxdata_list
;
4996 for (i
= 0; i
< n_dacs
; i
++) {
4997 type
= board
->caldac
[i
];
4998 for (j
= 0; j
< caldacs
[type
].n_chans
; j
++) {
4999 maxdata_list
[chan
] =
5000 (1 << caldacs
[type
].n_bits
) - 1;
5005 for (chan
= 0; chan
< s
->n_chan
; chan
++)
5006 ni_write_caldac(dev
, i
, s
->maxdata_list
[i
] / 2);
5008 type
= board
->caldac
[0];
5009 s
->maxdata
= (1 << caldacs
[type
].n_bits
) - 1;
5011 for (chan
= 0; chan
< s
->n_chan
; chan
++)
5012 ni_write_caldac(dev
, i
, s
->maxdata
/ 2);
5016 static void ni_write_caldac(struct comedi_device
*dev
, int addr
, int val
)
5018 const struct ni_board_struct
*board
= comedi_board(dev
);
5019 struct ni_private
*devpriv
= dev
->private;
5020 unsigned int loadbit
= 0, bits
= 0, bit
, bitstring
= 0;
5024 /* printk("ni_write_caldac: chan=%d val=%d\n",addr,val); */
5025 if (devpriv
->caldacs
[addr
] == val
)
5027 devpriv
->caldacs
[addr
] = val
;
5029 for (i
= 0; i
< 3; i
++) {
5030 type
= board
->caldac
[i
];
5031 if (type
== caldac_none
)
5033 if (addr
< caldacs
[type
].n_chans
) {
5034 bits
= caldacs
[type
].packbits(addr
, val
, &bitstring
);
5035 loadbit
= SerDacLd(i
);
5036 /* printk("caldac: using i=%d addr=%d %x\n",i,addr,bitstring); */
5039 addr
-= caldacs
[type
].n_chans
;
5042 for (bit
= 1 << (bits
- 1); bit
; bit
>>= 1) {
5043 ni_writeb(((bit
& bitstring
) ? 0x02 : 0), Serial_Command
);
5045 ni_writeb(1 | ((bit
& bitstring
) ? 0x02 : 0), Serial_Command
);
5048 ni_writeb(loadbit
, Serial_Command
);
5050 ni_writeb(0, Serial_Command
);
5053 static int pack_mb88341(int addr
, int val
, int *bitstring
)
5057 Note that address bits are reversed. Thanks to
5058 Ingo Keen for noticing this.
5060 Note also that the 88341 expects address values from
5061 1-12, whereas we use channel numbers 0-11. The NI
5062 docs use 1-12, also, so be careful here.
5065 *bitstring
= ((addr
& 0x1) << 11) |
5066 ((addr
& 0x2) << 9) |
5067 ((addr
& 0x4) << 7) | ((addr
& 0x8) << 5) | (val
& 0xff);
5071 static int pack_dac8800(int addr
, int val
, int *bitstring
)
5073 *bitstring
= ((addr
& 0x7) << 8) | (val
& 0xff);
5077 static int pack_dac8043(int addr
, int val
, int *bitstring
)
5079 *bitstring
= val
& 0xfff;
5083 static int pack_ad8522(int addr
, int val
, int *bitstring
)
5085 *bitstring
= (val
& 0xfff) | (addr
? 0xc000 : 0xa000);
5089 static int pack_ad8804(int addr
, int val
, int *bitstring
)
5091 *bitstring
= ((addr
& 0xf) << 8) | (val
& 0xff);
5095 static int pack_ad8842(int addr
, int val
, int *bitstring
)
5097 *bitstring
= ((addr
+ 1) << 8) | (val
& 0xff);
5103 * Read the GPCTs current value.
5105 static int GPCT_G_Watch(struct comedi_device
*dev
, int chan
)
5107 unsigned int hi1
, hi2
, lo
;
5109 devpriv
->gpct_command
[chan
] &= ~G_Save_Trace
;
5110 devpriv
->stc_writew(dev
, devpriv
->gpct_command
[chan
],
5111 G_Command_Register(chan
));
5113 devpriv
->gpct_command
[chan
] |= G_Save_Trace
;
5114 devpriv
->stc_writew(dev
, devpriv
->gpct_command
[chan
],
5115 G_Command_Register(chan
));
5117 /* This procedure is used because the two registers cannot
5118 * be read atomically. */
5120 hi1
= devpriv
->stc_readw(dev
, G_Save_Register_High(chan
));
5121 lo
= devpriv
->stc_readw(dev
, G_Save_Register_Low(chan
));
5122 hi2
= devpriv
->stc_readw(dev
, G_Save_Register_High(chan
));
5123 } while (hi1
!= hi2
);
5125 return (hi1
<< 16) | lo
;
5128 static void GPCT_Reset(struct comedi_device
*dev
, int chan
)
5130 int temp_ack_reg
= 0;
5132 /* printk("GPCT_Reset..."); */
5133 devpriv
->gpct_cur_operation
[chan
] = GPCT_RESET
;
5137 devpriv
->stc_writew(dev
, G0_Reset
, Joint_Reset_Register
);
5138 ni_set_bits(dev
, Interrupt_A_Enable_Register
,
5139 G0_TC_Interrupt_Enable
, 0);
5140 ni_set_bits(dev
, Interrupt_A_Enable_Register
,
5141 G0_Gate_Interrupt_Enable
, 0);
5142 temp_ack_reg
|= G0_Gate_Error_Confirm
;
5143 temp_ack_reg
|= G0_TC_Error_Confirm
;
5144 temp_ack_reg
|= G0_TC_Interrupt_Ack
;
5145 temp_ack_reg
|= G0_Gate_Interrupt_Ack
;
5146 devpriv
->stc_writew(dev
, temp_ack_reg
,
5147 Interrupt_A_Ack_Register
);
5149 /* problem...this interferes with the other ctr... */
5150 devpriv
->an_trig_etc_reg
|= GPFO_0_Output_Enable
;
5151 devpriv
->stc_writew(dev
, devpriv
->an_trig_etc_reg
,
5152 Analog_Trigger_Etc_Register
);
5155 devpriv
->stc_writew(dev
, G1_Reset
, Joint_Reset_Register
);
5156 ni_set_bits(dev
, Interrupt_B_Enable_Register
,
5157 G1_TC_Interrupt_Enable
, 0);
5158 ni_set_bits(dev
, Interrupt_B_Enable_Register
,
5159 G0_Gate_Interrupt_Enable
, 0);
5160 temp_ack_reg
|= G1_Gate_Error_Confirm
;
5161 temp_ack_reg
|= G1_TC_Error_Confirm
;
5162 temp_ack_reg
|= G1_TC_Interrupt_Ack
;
5163 temp_ack_reg
|= G1_Gate_Interrupt_Ack
;
5164 devpriv
->stc_writew(dev
, temp_ack_reg
,
5165 Interrupt_B_Ack_Register
);
5167 devpriv
->an_trig_etc_reg
|= GPFO_1_Output_Enable
;
5168 devpriv
->stc_writew(dev
, devpriv
->an_trig_etc_reg
,
5169 Analog_Trigger_Etc_Register
);
5173 devpriv
->gpct_mode
[chan
] = 0;
5174 devpriv
->gpct_input_select
[chan
] = 0;
5175 devpriv
->gpct_command
[chan
] = 0;
5177 devpriv
->gpct_command
[chan
] |= G_Synchronized_Gate
;
5179 devpriv
->stc_writew(dev
, devpriv
->gpct_mode
[chan
],
5180 G_Mode_Register(chan
));
5181 devpriv
->stc_writew(dev
, devpriv
->gpct_input_select
[chan
],
5182 G_Input_Select_Register(chan
));
5183 devpriv
->stc_writew(dev
, 0, G_Autoincrement_Register(chan
));
5185 /* printk("exit GPCT_Reset\n"); */
5190 static int ni_gpct_insn_config(struct comedi_device
*dev
,
5191 struct comedi_subdevice
*s
,
5192 struct comedi_insn
*insn
, unsigned int *data
)
5194 struct ni_gpct
*counter
= s
->private;
5195 return ni_tio_insn_config(counter
, insn
, data
);
5198 static int ni_gpct_insn_read(struct comedi_device
*dev
,
5199 struct comedi_subdevice
*s
,
5200 struct comedi_insn
*insn
, unsigned int *data
)
5202 struct ni_gpct
*counter
= s
->private;
5203 return ni_tio_rinsn(counter
, insn
, data
);
5206 static int ni_gpct_insn_write(struct comedi_device
*dev
,
5207 struct comedi_subdevice
*s
,
5208 struct comedi_insn
*insn
, unsigned int *data
)
5210 struct ni_gpct
*counter
= s
->private;
5211 return ni_tio_winsn(counter
, insn
, data
);
5214 static int ni_gpct_cmd(struct comedi_device
*dev
, struct comedi_subdevice
*s
)
5218 struct ni_gpct
*counter
= s
->private;
5219 /* const struct comedi_cmd *cmd = &s->async->cmd; */
5221 retval
= ni_request_gpct_mite_channel(dev
, counter
->counter_index
,
5225 "no dma channel available for use by counter");
5228 ni_tio_acknowledge_and_confirm(counter
, NULL
, NULL
, NULL
, NULL
);
5229 ni_e_series_enable_second_irq(dev
, counter
->counter_index
, 1);
5230 retval
= ni_tio_cmd(counter
, s
->async
);
5237 static int ni_gpct_cmdtest(struct comedi_device
*dev
,
5238 struct comedi_subdevice
*s
, struct comedi_cmd
*cmd
)
5241 struct ni_gpct
*counter
= s
->private;
5243 return ni_tio_cmdtest(counter
, cmd
);
5249 static int ni_gpct_cancel(struct comedi_device
*dev
, struct comedi_subdevice
*s
)
5252 struct ni_gpct
*counter
= s
->private;
5255 retval
= ni_tio_cancel(counter
);
5256 ni_e_series_enable_second_irq(dev
, counter
->counter_index
, 0);
5257 ni_release_gpct_mite_channel(dev
, counter
->counter_index
);
5266 * Programmable Function Inputs
5270 static int ni_m_series_set_pfi_routing(struct comedi_device
*dev
, unsigned chan
,
5273 struct ni_private
*devpriv
= dev
->private;
5274 unsigned pfi_reg_index
;
5275 unsigned array_offset
;
5277 if ((source
& 0x1f) != source
)
5279 pfi_reg_index
= 1 + chan
/ 3;
5280 array_offset
= pfi_reg_index
- 1;
5281 devpriv
->pfi_output_select_reg
[array_offset
] &=
5282 ~MSeries_PFI_Output_Select_Mask(chan
);
5283 devpriv
->pfi_output_select_reg
[array_offset
] |=
5284 MSeries_PFI_Output_Select_Bits(chan
, source
);
5285 ni_writew(devpriv
->pfi_output_select_reg
[array_offset
],
5286 M_Offset_PFI_Output_Select(pfi_reg_index
));
5290 static int ni_old_set_pfi_routing(struct comedi_device
*dev
, unsigned chan
,
5293 /* pre-m-series boards have fixed signals on pfi pins */
5294 if (source
!= ni_old_get_pfi_routing(dev
, chan
))
5299 static int ni_set_pfi_routing(struct comedi_device
*dev
, unsigned chan
,
5302 const struct ni_board_struct
*board
= comedi_board(dev
);
5304 if (board
->reg_type
& ni_reg_m_series_mask
)
5305 return ni_m_series_set_pfi_routing(dev
, chan
, source
);
5307 return ni_old_set_pfi_routing(dev
, chan
, source
);
5310 static unsigned ni_m_series_get_pfi_routing(struct comedi_device
*dev
,
5313 struct ni_private
*devpriv
= dev
->private;
5314 const unsigned array_offset
= chan
/ 3;
5316 return MSeries_PFI_Output_Select_Source(chan
,
5318 pfi_output_select_reg
5322 static unsigned ni_old_get_pfi_routing(struct comedi_device
*dev
, unsigned chan
)
5324 /* pre-m-series boards have fixed signals on pfi pins */
5327 return NI_PFI_OUTPUT_AI_START1
;
5330 return NI_PFI_OUTPUT_AI_START2
;
5333 return NI_PFI_OUTPUT_AI_CONVERT
;
5336 return NI_PFI_OUTPUT_G_SRC1
;
5339 return NI_PFI_OUTPUT_G_GATE1
;
5342 return NI_PFI_OUTPUT_AO_UPDATE_N
;
5345 return NI_PFI_OUTPUT_AO_START1
;
5348 return NI_PFI_OUTPUT_AI_START_PULSE
;
5351 return NI_PFI_OUTPUT_G_SRC0
;
5354 return NI_PFI_OUTPUT_G_GATE0
;
5357 printk("%s: bug, unhandled case in switch.\n", __func__
);
5363 static unsigned ni_get_pfi_routing(struct comedi_device
*dev
, unsigned chan
)
5365 const struct ni_board_struct
*board
= comedi_board(dev
);
5367 if (board
->reg_type
& ni_reg_m_series_mask
)
5368 return ni_m_series_get_pfi_routing(dev
, chan
);
5370 return ni_old_get_pfi_routing(dev
, chan
);
5373 static int ni_config_filter(struct comedi_device
*dev
, unsigned pfi_channel
,
5374 enum ni_pfi_filter_select filter
)
5376 const struct ni_board_struct
*board
= comedi_board(dev
);
5377 struct ni_private
*devpriv __maybe_unused
= dev
->private;
5380 if ((board
->reg_type
& ni_reg_m_series_mask
) == 0) {
5383 bits
= ni_readl(M_Offset_PFI_Filter
);
5384 bits
&= ~MSeries_PFI_Filter_Select_Mask(pfi_channel
);
5385 bits
|= MSeries_PFI_Filter_Select_Bits(pfi_channel
, filter
);
5386 ni_writel(bits
, M_Offset_PFI_Filter
);
5390 static int ni_pfi_insn_bits(struct comedi_device
*dev
,
5391 struct comedi_subdevice
*s
,
5392 struct comedi_insn
*insn
, unsigned int *data
)
5394 const struct ni_board_struct
*board
= comedi_board(dev
);
5395 struct ni_private
*devpriv __maybe_unused
= dev
->private;
5397 if ((board
->reg_type
& ni_reg_m_series_mask
) == 0) {
5401 s
->state
&= ~data
[0];
5402 s
->state
|= (data
[0] & data
[1]);
5403 ni_writew(s
->state
, M_Offset_PFI_DO
);
5405 data
[1] = ni_readw(M_Offset_PFI_DI
);
5409 static int ni_pfi_insn_config(struct comedi_device
*dev
,
5410 struct comedi_subdevice
*s
,
5411 struct comedi_insn
*insn
, unsigned int *data
)
5413 struct ni_private
*devpriv
= dev
->private;
5419 chan
= CR_CHAN(insn
->chanspec
);
5423 ni_set_bits(dev
, IO_Bidirection_Pin_Register
, 1 << chan
, 1);
5426 ni_set_bits(dev
, IO_Bidirection_Pin_Register
, 1 << chan
, 0);
5428 case INSN_CONFIG_DIO_QUERY
:
5430 (devpriv
->io_bidirection_pin_reg
& (1 << chan
)) ?
5431 COMEDI_OUTPUT
: COMEDI_INPUT
;
5434 case INSN_CONFIG_SET_ROUTING
:
5435 return ni_set_pfi_routing(dev
, chan
, data
[1]);
5437 case INSN_CONFIG_GET_ROUTING
:
5438 data
[1] = ni_get_pfi_routing(dev
, chan
);
5440 case INSN_CONFIG_FILTER
:
5441 return ni_config_filter(dev
, chan
, data
[1]);
5451 * NI RTSI Bus Functions
5454 static void ni_rtsi_init(struct comedi_device
*dev
)
5456 const struct ni_board_struct
*board
= comedi_board(dev
);
5457 struct ni_private
*devpriv
= dev
->private;
5459 /* Initialises the RTSI bus signal switch to a default state */
5461 /* Set clock mode to internal */
5462 devpriv
->clock_and_fout2
= MSeries_RTSI_10MHz_Bit
;
5463 if (ni_set_master_clock(dev
, NI_MIO_INTERNAL_CLOCK
, 0) < 0) {
5464 printk("ni_set_master_clock failed, bug?");
5466 /* default internal lines routing to RTSI bus lines */
5467 devpriv
->rtsi_trig_a_output_reg
=
5468 RTSI_Trig_Output_Bits(0,
5469 NI_RTSI_OUTPUT_ADR_START1
) |
5470 RTSI_Trig_Output_Bits(1,
5471 NI_RTSI_OUTPUT_ADR_START2
) |
5472 RTSI_Trig_Output_Bits(2,
5473 NI_RTSI_OUTPUT_SCLKG
) |
5474 RTSI_Trig_Output_Bits(3, NI_RTSI_OUTPUT_DACUPDN
);
5475 devpriv
->stc_writew(dev
, devpriv
->rtsi_trig_a_output_reg
,
5476 RTSI_Trig_A_Output_Register
);
5477 devpriv
->rtsi_trig_b_output_reg
=
5478 RTSI_Trig_Output_Bits(4,
5479 NI_RTSI_OUTPUT_DA_START1
) |
5480 RTSI_Trig_Output_Bits(5,
5481 NI_RTSI_OUTPUT_G_SRC0
) |
5482 RTSI_Trig_Output_Bits(6, NI_RTSI_OUTPUT_G_GATE0
);
5483 if (board
->reg_type
& ni_reg_m_series_mask
)
5484 devpriv
->rtsi_trig_b_output_reg
|=
5485 RTSI_Trig_Output_Bits(7, NI_RTSI_OUTPUT_RTSI_OSC
);
5486 devpriv
->stc_writew(dev
, devpriv
->rtsi_trig_b_output_reg
,
5487 RTSI_Trig_B_Output_Register
);
5490 * Sets the source and direction of the 4 on board lines
5491 * devpriv->stc_writew(dev, 0x0000, RTSI_Board_Register);
5495 static int ni_rtsi_insn_bits(struct comedi_device
*dev
,
5496 struct comedi_subdevice
*s
,
5497 struct comedi_insn
*insn
, unsigned int *data
)
5504 /* Find best multiplier/divider to try and get the PLL running at 80 MHz
5505 * given an arbitrary frequency input clock */
5506 static int ni_mseries_get_pll_parameters(unsigned reference_period_ns
,
5507 unsigned *freq_divider
,
5508 unsigned *freq_multiplier
,
5509 unsigned *actual_period_ns
)
5512 unsigned best_div
= 1;
5513 static const unsigned max_div
= 0x10;
5515 unsigned best_mult
= 1;
5516 static const unsigned max_mult
= 0x100;
5517 static const unsigned pico_per_nano
= 1000;
5519 const unsigned reference_picosec
= reference_period_ns
* pico_per_nano
;
5520 /* m-series wants the phased-locked loop to output 80MHz, which is divided by 4 to
5521 * 20 MHz for most timing clocks */
5522 static const unsigned target_picosec
= 12500;
5523 static const unsigned fudge_factor_80_to_20Mhz
= 4;
5524 int best_period_picosec
= 0;
5525 for (div
= 1; div
<= max_div
; ++div
) {
5526 for (mult
= 1; mult
<= max_mult
; ++mult
) {
5527 unsigned new_period_ps
=
5528 (reference_picosec
* div
) / mult
;
5529 if (abs(new_period_ps
- target_picosec
) <
5530 abs(best_period_picosec
- target_picosec
)) {
5531 best_period_picosec
= new_period_ps
;
5537 if (best_period_picosec
== 0) {
5538 printk("%s: bug, failed to find pll parameters\n", __func__
);
5541 *freq_divider
= best_div
;
5542 *freq_multiplier
= best_mult
;
5544 (best_period_picosec
* fudge_factor_80_to_20Mhz
+
5545 (pico_per_nano
/ 2)) / pico_per_nano
;
5549 static inline unsigned num_configurable_rtsi_channels(struct comedi_device
*dev
)
5551 const struct ni_board_struct
*board
= comedi_board(dev
);
5553 if (board
->reg_type
& ni_reg_m_series_mask
)
5559 static int ni_mseries_set_pll_master_clock(struct comedi_device
*dev
,
5560 unsigned source
, unsigned period_ns
)
5562 struct ni_private
*devpriv
= dev
->private;
5563 static const unsigned min_period_ns
= 50;
5564 static const unsigned max_period_ns
= 1000;
5565 static const unsigned timeout
= 1000;
5566 unsigned pll_control_bits
;
5567 unsigned freq_divider
;
5568 unsigned freq_multiplier
;
5572 if (source
== NI_MIO_PLL_PXI10_CLOCK
)
5574 /* these limits are somewhat arbitrary, but NI advertises 1 to 20MHz range so we'll use that */
5575 if (period_ns
< min_period_ns
|| period_ns
> max_period_ns
) {
5577 ("%s: you must specify an input clock frequency between %i and %i nanosec "
5578 "for the phased-lock loop.\n", __func__
,
5579 min_period_ns
, max_period_ns
);
5582 devpriv
->rtsi_trig_direction_reg
&= ~Use_RTSI_Clock_Bit
;
5583 devpriv
->stc_writew(dev
, devpriv
->rtsi_trig_direction_reg
,
5584 RTSI_Trig_Direction_Register
);
5586 MSeries_PLL_Enable_Bit
| MSeries_PLL_VCO_Mode_75_150MHz_Bits
;
5587 devpriv
->clock_and_fout2
|=
5588 MSeries_Timebase1_Select_Bit
| MSeries_Timebase3_Select_Bit
;
5589 devpriv
->clock_and_fout2
&= ~MSeries_PLL_In_Source_Select_Mask
;
5591 case NI_MIO_PLL_PXI_STAR_TRIGGER_CLOCK
:
5592 devpriv
->clock_and_fout2
|=
5593 MSeries_PLL_In_Source_Select_Star_Trigger_Bits
;
5594 retval
= ni_mseries_get_pll_parameters(period_ns
, &freq_divider
,
5596 &devpriv
->clock_ns
);
5600 case NI_MIO_PLL_PXI10_CLOCK
:
5601 /* pxi clock is 10MHz */
5602 devpriv
->clock_and_fout2
|=
5603 MSeries_PLL_In_Source_Select_PXI_Clock10
;
5604 retval
= ni_mseries_get_pll_parameters(period_ns
, &freq_divider
,
5606 &devpriv
->clock_ns
);
5612 unsigned rtsi_channel
;
5613 static const unsigned max_rtsi_channel
= 7;
5614 for (rtsi_channel
= 0; rtsi_channel
<= max_rtsi_channel
;
5617 NI_MIO_PLL_RTSI_CLOCK(rtsi_channel
)) {
5618 devpriv
->clock_and_fout2
|=
5619 MSeries_PLL_In_Source_Select_RTSI_Bits
5624 if (rtsi_channel
> max_rtsi_channel
)
5626 retval
= ni_mseries_get_pll_parameters(period_ns
,
5636 ni_writew(devpriv
->clock_and_fout2
, M_Offset_Clock_and_Fout2
);
5638 MSeries_PLL_Divisor_Bits(freq_divider
) |
5639 MSeries_PLL_Multiplier_Bits(freq_multiplier
);
5641 /* printk("using divider=%i, multiplier=%i for PLL. pll_control_bits = 0x%x\n",
5642 * freq_divider, freq_multiplier, pll_control_bits); */
5643 /* printk("clock_ns=%d\n", devpriv->clock_ns); */
5644 ni_writew(pll_control_bits
, M_Offset_PLL_Control
);
5645 devpriv
->clock_source
= source
;
5646 /* it seems to typically take a few hundred microseconds for PLL to lock */
5647 for (i
= 0; i
< timeout
; ++i
) {
5648 if (ni_readw(M_Offset_PLL_Status
) & MSeries_PLL_Locked_Bit
) {
5655 ("%s: timed out waiting for PLL to lock to reference clock source %i with period %i ns.\n",
5656 __func__
, source
, period_ns
);
5662 static int ni_set_master_clock(struct comedi_device
*dev
, unsigned source
,
5665 const struct ni_board_struct
*board
= comedi_board(dev
);
5666 struct ni_private
*devpriv
= dev
->private;
5668 if (source
== NI_MIO_INTERNAL_CLOCK
) {
5669 devpriv
->rtsi_trig_direction_reg
&= ~Use_RTSI_Clock_Bit
;
5670 devpriv
->stc_writew(dev
, devpriv
->rtsi_trig_direction_reg
,
5671 RTSI_Trig_Direction_Register
);
5672 devpriv
->clock_ns
= TIMEBASE_1_NS
;
5673 if (board
->reg_type
& ni_reg_m_series_mask
) {
5674 devpriv
->clock_and_fout2
&=
5675 ~(MSeries_Timebase1_Select_Bit
|
5676 MSeries_Timebase3_Select_Bit
);
5677 ni_writew(devpriv
->clock_and_fout2
,
5678 M_Offset_Clock_and_Fout2
);
5679 ni_writew(0, M_Offset_PLL_Control
);
5681 devpriv
->clock_source
= source
;
5683 if (board
->reg_type
& ni_reg_m_series_mask
) {
5684 return ni_mseries_set_pll_master_clock(dev
, source
,
5687 if (source
== NI_MIO_RTSI_CLOCK
) {
5688 devpriv
->rtsi_trig_direction_reg
|=
5690 devpriv
->stc_writew(dev
,
5692 rtsi_trig_direction_reg
,
5693 RTSI_Trig_Direction_Register
);
5694 if (period_ns
== 0) {
5696 ("%s: we don't handle an unspecified clock period correctly yet, returning error.\n",
5700 devpriv
->clock_ns
= period_ns
;
5702 devpriv
->clock_source
= source
;
5710 static int ni_valid_rtsi_output_source(struct comedi_device
*dev
, unsigned chan
,
5713 const struct ni_board_struct
*board
= comedi_board(dev
);
5715 if (chan
>= num_configurable_rtsi_channels(dev
)) {
5716 if (chan
== old_RTSI_clock_channel
) {
5717 if (source
== NI_RTSI_OUTPUT_RTSI_OSC
)
5721 ("%s: invalid source for channel=%i, channel %i is always the RTSI clock for pre-m-series boards.\n",
5722 __func__
, chan
, old_RTSI_clock_channel
);
5729 case NI_RTSI_OUTPUT_ADR_START1
:
5730 case NI_RTSI_OUTPUT_ADR_START2
:
5731 case NI_RTSI_OUTPUT_SCLKG
:
5732 case NI_RTSI_OUTPUT_DACUPDN
:
5733 case NI_RTSI_OUTPUT_DA_START1
:
5734 case NI_RTSI_OUTPUT_G_SRC0
:
5735 case NI_RTSI_OUTPUT_G_GATE0
:
5736 case NI_RTSI_OUTPUT_RGOUT0
:
5737 case NI_RTSI_OUTPUT_RTSI_BRD_0
:
5740 case NI_RTSI_OUTPUT_RTSI_OSC
:
5741 if (board
->reg_type
& ni_reg_m_series_mask
)
5752 static int ni_set_rtsi_routing(struct comedi_device
*dev
, unsigned chan
,
5755 struct ni_private
*devpriv
= dev
->private;
5757 if (ni_valid_rtsi_output_source(dev
, chan
, source
) == 0)
5760 devpriv
->rtsi_trig_a_output_reg
&= ~RTSI_Trig_Output_Mask(chan
);
5761 devpriv
->rtsi_trig_a_output_reg
|=
5762 RTSI_Trig_Output_Bits(chan
, source
);
5763 devpriv
->stc_writew(dev
, devpriv
->rtsi_trig_a_output_reg
,
5764 RTSI_Trig_A_Output_Register
);
5765 } else if (chan
< 8) {
5766 devpriv
->rtsi_trig_b_output_reg
&= ~RTSI_Trig_Output_Mask(chan
);
5767 devpriv
->rtsi_trig_b_output_reg
|=
5768 RTSI_Trig_Output_Bits(chan
, source
);
5769 devpriv
->stc_writew(dev
, devpriv
->rtsi_trig_b_output_reg
,
5770 RTSI_Trig_B_Output_Register
);
5775 static unsigned ni_get_rtsi_routing(struct comedi_device
*dev
, unsigned chan
)
5777 struct ni_private
*devpriv
= dev
->private;
5780 return RTSI_Trig_Output_Source(chan
,
5781 devpriv
->rtsi_trig_a_output_reg
);
5782 } else if (chan
< num_configurable_rtsi_channels(dev
)) {
5783 return RTSI_Trig_Output_Source(chan
,
5784 devpriv
->rtsi_trig_b_output_reg
);
5786 if (chan
== old_RTSI_clock_channel
)
5787 return NI_RTSI_OUTPUT_RTSI_OSC
;
5788 printk("%s: bug! should never get here?\n", __func__
);
5793 static int ni_rtsi_insn_config(struct comedi_device
*dev
,
5794 struct comedi_subdevice
*s
,
5795 struct comedi_insn
*insn
, unsigned int *data
)
5797 const struct ni_board_struct
*board
= comedi_board(dev
);
5798 struct ni_private
*devpriv
= dev
->private;
5799 unsigned int chan
= CR_CHAN(insn
->chanspec
);
5802 case INSN_CONFIG_DIO_OUTPUT
:
5803 if (chan
< num_configurable_rtsi_channels(dev
)) {
5804 devpriv
->rtsi_trig_direction_reg
|=
5805 RTSI_Output_Bit(chan
,
5806 (board
->reg_type
& ni_reg_m_series_mask
) != 0);
5807 } else if (chan
== old_RTSI_clock_channel
) {
5808 devpriv
->rtsi_trig_direction_reg
|=
5809 Drive_RTSI_Clock_Bit
;
5811 devpriv
->stc_writew(dev
, devpriv
->rtsi_trig_direction_reg
,
5812 RTSI_Trig_Direction_Register
);
5814 case INSN_CONFIG_DIO_INPUT
:
5815 if (chan
< num_configurable_rtsi_channels(dev
)) {
5816 devpriv
->rtsi_trig_direction_reg
&=
5817 ~RTSI_Output_Bit(chan
,
5818 (board
->reg_type
& ni_reg_m_series_mask
) != 0);
5819 } else if (chan
== old_RTSI_clock_channel
) {
5820 devpriv
->rtsi_trig_direction_reg
&=
5821 ~Drive_RTSI_Clock_Bit
;
5823 devpriv
->stc_writew(dev
, devpriv
->rtsi_trig_direction_reg
,
5824 RTSI_Trig_Direction_Register
);
5826 case INSN_CONFIG_DIO_QUERY
:
5827 if (chan
< num_configurable_rtsi_channels(dev
)) {
5829 (devpriv
->rtsi_trig_direction_reg
&
5830 RTSI_Output_Bit(chan
,
5831 (board
->reg_type
& ni_reg_m_series_mask
) != 0))
5832 ? INSN_CONFIG_DIO_OUTPUT
5833 : INSN_CONFIG_DIO_INPUT
;
5834 } else if (chan
== old_RTSI_clock_channel
) {
5836 (devpriv
->rtsi_trig_direction_reg
&
5837 Drive_RTSI_Clock_Bit
)
5838 ? INSN_CONFIG_DIO_OUTPUT
: INSN_CONFIG_DIO_INPUT
;
5842 case INSN_CONFIG_SET_CLOCK_SRC
:
5843 return ni_set_master_clock(dev
, data
[1], data
[2]);
5845 case INSN_CONFIG_GET_CLOCK_SRC
:
5846 data
[1] = devpriv
->clock_source
;
5847 data
[2] = devpriv
->clock_ns
;
5850 case INSN_CONFIG_SET_ROUTING
:
5851 return ni_set_rtsi_routing(dev
, chan
, data
[1]);
5853 case INSN_CONFIG_GET_ROUTING
:
5854 data
[1] = ni_get_rtsi_routing(dev
, chan
);
5864 static int cs5529_wait_for_idle(struct comedi_device
*dev
)
5866 unsigned short status
;
5867 const int timeout
= HZ
;
5870 for (i
= 0; i
< timeout
; i
++) {
5871 status
= ni_ao_win_inw(dev
, CAL_ADC_Status_67xx
);
5872 if ((status
& CSS_ADC_BUSY
) == 0) {
5875 set_current_state(TASK_INTERRUPTIBLE
);
5876 if (schedule_timeout(1)) {
5880 /* printk("looped %i times waiting for idle\n", i); */
5882 printk("%s: %s: timeout\n", __FILE__
, __func__
);
5888 static void cs5529_command(struct comedi_device
*dev
, unsigned short value
)
5890 static const int timeout
= 100;
5893 ni_ao_win_outw(dev
, value
, CAL_ADC_Command_67xx
);
5894 /* give time for command to start being serially clocked into cs5529.
5895 * this insures that the CSS_ADC_BUSY bit will get properly
5896 * set before we exit this function.
5898 for (i
= 0; i
< timeout
; i
++) {
5899 if ((ni_ao_win_inw(dev
, CAL_ADC_Status_67xx
) & CSS_ADC_BUSY
))
5903 /* printk("looped %i times writing command to cs5529\n", i); */
5905 comedi_error(dev
, "possible problem - never saw adc go busy?");
5909 /* write to cs5529 register */
5910 static void cs5529_config_write(struct comedi_device
*dev
, unsigned int value
,
5911 unsigned int reg_select_bits
)
5913 ni_ao_win_outw(dev
, ((value
>> 16) & 0xff),
5914 CAL_ADC_Config_Data_High_Word_67xx
);
5915 ni_ao_win_outw(dev
, (value
& 0xffff),
5916 CAL_ADC_Config_Data_Low_Word_67xx
);
5917 reg_select_bits
&= CSCMD_REGISTER_SELECT_MASK
;
5918 cs5529_command(dev
, CSCMD_COMMAND
| reg_select_bits
);
5919 if (cs5529_wait_for_idle(dev
))
5920 comedi_error(dev
, "time or signal in cs5529_config_write()");
5923 #ifdef NI_CS5529_DEBUG
5924 /* read from cs5529 register */
5925 static unsigned int cs5529_config_read(struct comedi_device
*dev
,
5926 unsigned int reg_select_bits
)
5930 reg_select_bits
&= CSCMD_REGISTER_SELECT_MASK
;
5931 cs5529_command(dev
, CSCMD_COMMAND
| CSCMD_READ
| reg_select_bits
);
5932 if (cs5529_wait_for_idle(dev
))
5933 comedi_error(dev
, "timeout or signal in cs5529_config_read()");
5934 value
= (ni_ao_win_inw(dev
,
5935 CAL_ADC_Config_Data_High_Word_67xx
) << 16) &
5937 value
|= ni_ao_win_inw(dev
, CAL_ADC_Config_Data_Low_Word_67xx
) & 0xffff;
5942 static int cs5529_do_conversion(struct comedi_device
*dev
, unsigned short *data
)
5945 unsigned short status
;
5947 cs5529_command(dev
, CSCMD_COMMAND
| CSCMD_SINGLE_CONVERSION
);
5948 retval
= cs5529_wait_for_idle(dev
);
5951 "timeout or signal in cs5529_do_conversion()");
5954 status
= ni_ao_win_inw(dev
, CAL_ADC_Status_67xx
);
5955 if (status
& CSS_OSC_DETECT
) {
5957 ("ni_mio_common: cs5529 conversion error, status CSS_OSC_DETECT\n");
5960 if (status
& CSS_OVERRANGE
) {
5962 ("ni_mio_common: cs5529 conversion error, overrange (ignoring)\n");
5965 *data
= ni_ao_win_inw(dev
, CAL_ADC_Data_67xx
);
5966 /* cs5529 returns 16 bit signed data in bipolar mode */
5972 static int cs5529_ai_insn_read(struct comedi_device
*dev
,
5973 struct comedi_subdevice
*s
,
5974 struct comedi_insn
*insn
, unsigned int *data
)
5977 unsigned short sample
;
5978 unsigned int channel_select
;
5979 const unsigned int INTERNAL_REF
= 0x1000;
5981 /* Set calibration adc source. Docs lie, reference select bits 8 to 11
5982 * do nothing. bit 12 seems to chooses internal reference voltage, bit
5983 * 13 causes the adc input to go overrange (maybe reads external reference?) */
5984 if (insn
->chanspec
& CR_ALT_SOURCE
)
5985 channel_select
= INTERNAL_REF
;
5987 channel_select
= CR_CHAN(insn
->chanspec
);
5988 ni_ao_win_outw(dev
, channel_select
, AO_Calibration_Channel_Select_67xx
);
5990 for (n
= 0; n
< insn
->n
; n
++) {
5991 retval
= cs5529_do_conversion(dev
, &sample
);
5999 static int init_cs5529(struct comedi_device
*dev
)
6001 unsigned int config_bits
=
6002 CSCFG_PORT_MODE
| CSCFG_WORD_RATE_2180_CYCLES
;
6005 /* do self-calibration */
6006 cs5529_config_write(dev
, config_bits
| CSCFG_SELF_CAL_OFFSET_GAIN
,
6007 CSCMD_CONFIG_REGISTER
);
6008 /* need to force a conversion for calibration to run */
6009 cs5529_do_conversion(dev
, NULL
);
6011 /* force gain calibration to 1 */
6012 cs5529_config_write(dev
, 0x400000, CSCMD_GAIN_REGISTER
);
6013 cs5529_config_write(dev
, config_bits
| CSCFG_SELF_CAL_OFFSET
,
6014 CSCMD_CONFIG_REGISTER
);
6015 if (cs5529_wait_for_idle(dev
))
6016 comedi_error(dev
, "timeout or signal in init_cs5529()\n");
6018 #ifdef NI_CS5529_DEBUG
6019 printk("config: 0x%x\n", cs5529_config_read(dev
,
6020 CSCMD_CONFIG_REGISTER
));
6021 printk("gain: 0x%x\n", cs5529_config_read(dev
, CSCMD_GAIN_REGISTER
));
6022 printk("offset: 0x%x\n", cs5529_config_read(dev
,
6023 CSCMD_OFFSET_REGISTER
));