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 #include <linux/interrupt.h>
56 #include <linux/sched.h>
57 #include <linux/delay.h>
60 #include "comedi_fc.h"
63 #define NI_TIMEOUT 1000
64 static const unsigned old_RTSI_clock_channel
= 7;
66 /* Note: this table must match the ai_gain_* definitions */
67 static const short ni_gainlkup
[][16] = {
68 [ai_gain_16
] = {0, 1, 2, 3, 4, 5, 6, 7,
69 0x100, 0x101, 0x102, 0x103, 0x104, 0x105, 0x106, 0x107},
70 [ai_gain_8
] = {1, 2, 4, 7, 0x101, 0x102, 0x104, 0x107},
71 [ai_gain_14
] = {1, 2, 3, 4, 5, 6, 7,
72 0x101, 0x102, 0x103, 0x104, 0x105, 0x106, 0x107},
73 [ai_gain_4
] = {0, 1, 4, 7},
74 [ai_gain_611x
] = {0x00a, 0x00b, 0x001, 0x002,
75 0x003, 0x004, 0x005, 0x006},
76 [ai_gain_622x
] = {0, 1, 4, 5},
77 [ai_gain_628x
] = {1, 2, 3, 4, 5, 6, 7},
78 [ai_gain_6143
] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
81 static const struct comedi_lrange range_ni_E_ai
= {
102 static const struct comedi_lrange range_ni_E_ai_limited
= {
115 static const struct comedi_lrange range_ni_E_ai_limited14
= {
134 static const struct comedi_lrange range_ni_E_ai_bipolar4
= {
143 static const struct comedi_lrange range_ni_E_ai_611x
= {
156 static const struct comedi_lrange range_ni_M_ai_622x
= {
165 static const struct comedi_lrange range_ni_M_ai_628x
= {
177 static const struct comedi_lrange range_ni_E_ao_ext
= {
186 static const struct comedi_lrange
*const ni_range_lkup
[] = {
187 [ai_gain_16
] = &range_ni_E_ai
,
188 [ai_gain_8
] = &range_ni_E_ai_limited
,
189 [ai_gain_14
] = &range_ni_E_ai_limited14
,
190 [ai_gain_4
] = &range_ni_E_ai_bipolar4
,
191 [ai_gain_611x
] = &range_ni_E_ai_611x
,
192 [ai_gain_622x
] = &range_ni_M_ai_622x
,
193 [ai_gain_628x
] = &range_ni_M_ai_628x
,
194 [ai_gain_6143
] = &range_bipolar5
199 AIMODE_HALF_FULL
= 1,
204 enum ni_common_subdevices
{
210 NI_CALIBRATION_SUBDEV
,
213 NI_CS5529_CALIBRATION_SUBDEV
,
221 static inline unsigned NI_GPCT_SUBDEV(unsigned counter_index
)
223 switch (counter_index
) {
225 return NI_GPCT0_SUBDEV
;
228 return NI_GPCT1_SUBDEV
;
234 return NI_GPCT0_SUBDEV
;
237 enum timebase_nanoseconds
{
239 TIMEBASE_2_NS
= 10000
242 #define SERIAL_DISABLED 0
243 #define SERIAL_600NS 600
244 #define SERIAL_1_2US 1200
245 #define SERIAL_10US 10000
247 static const int num_adc_stages_611x
= 3;
249 static void ni_writel(struct comedi_device
*dev
, uint32_t data
, int reg
)
251 struct ni_private
*devpriv
= dev
->private;
254 writel(data
, devpriv
->mite
->daq_io_addr
+ reg
);
256 outl(data
, dev
->iobase
+ reg
);
259 static void ni_writew(struct comedi_device
*dev
, uint16_t data
, int reg
)
261 struct ni_private
*devpriv
= dev
->private;
264 writew(data
, devpriv
->mite
->daq_io_addr
+ reg
);
266 outw(data
, dev
->iobase
+ reg
);
269 static void ni_writeb(struct comedi_device
*dev
, uint8_t data
, int reg
)
271 struct ni_private
*devpriv
= dev
->private;
274 writeb(data
, devpriv
->mite
->daq_io_addr
+ reg
);
276 outb(data
, dev
->iobase
+ reg
);
279 static uint32_t ni_readl(struct comedi_device
*dev
, int reg
)
281 struct ni_private
*devpriv
= dev
->private;
284 return readl(devpriv
->mite
->daq_io_addr
+ reg
);
286 return inl(dev
->iobase
+ reg
);
289 static uint16_t ni_readw(struct comedi_device
*dev
, int reg
)
291 struct ni_private
*devpriv
= dev
->private;
294 return readw(devpriv
->mite
->daq_io_addr
+ reg
);
296 return inw(dev
->iobase
+ reg
);
299 static uint8_t ni_readb(struct comedi_device
*dev
, int reg
)
301 struct ni_private
*devpriv
= dev
->private;
304 return readb(devpriv
->mite
->daq_io_addr
+ reg
);
306 return inb(dev
->iobase
+ reg
);
310 * We automatically take advantage of STC registers that can be
311 * read/written directly in the I/O space of the board.
313 * The AT-MIO and DAQCard devices map the low 8 STC registers to
316 * Most PCIMIO devices also map the low 8 STC registers but the
317 * 611x devices map the read registers to iobase+(addr-1)*2.
318 * For now non-windowed STC access is disabled if a PCIMIO device
319 * is detected (devpriv->mite has been initialized).
321 * The M series devices do not used windowed registers for the
322 * STC registers. The functions below handle the mapping of the
323 * windowed STC registers to the m series register offsets.
326 static void m_series_stc_writel(struct comedi_device
*dev
,
327 uint32_t data
, int reg
)
332 case AI_SC_Load_A_Registers
:
333 offset
= M_Offset_AI_SC_Load_A
;
335 case AI_SI_Load_A_Registers
:
336 offset
= M_Offset_AI_SI_Load_A
;
338 case AO_BC_Load_A_Register
:
339 offset
= M_Offset_AO_BC_Load_A
;
341 case AO_UC_Load_A_Register
:
342 offset
= M_Offset_AO_UC_Load_A
;
344 case AO_UI_Load_A_Register
:
345 offset
= M_Offset_AO_UI_Load_A
;
347 case G_Load_A_Register(0):
348 offset
= M_Offset_G0_Load_A
;
350 case G_Load_A_Register(1):
351 offset
= M_Offset_G1_Load_A
;
353 case G_Load_B_Register(0):
354 offset
= M_Offset_G0_Load_B
;
356 case G_Load_B_Register(1):
357 offset
= M_Offset_G1_Load_B
;
360 dev_warn(dev
->class_dev
,
361 "%s: bug! unhandled register=0x%x in switch\n",
365 ni_writel(dev
, data
, offset
);
368 static void m_series_stc_writew(struct comedi_device
*dev
,
369 uint16_t data
, int reg
)
375 offset
= M_Offset_AI_FIFO_Clear
;
377 case AI_Command_1_Register
:
378 offset
= M_Offset_AI_Command_1
;
380 case AI_Command_2_Register
:
381 offset
= M_Offset_AI_Command_2
;
383 case AI_Mode_1_Register
:
384 offset
= M_Offset_AI_Mode_1
;
386 case AI_Mode_2_Register
:
387 offset
= M_Offset_AI_Mode_2
;
389 case AI_Mode_3_Register
:
390 offset
= M_Offset_AI_Mode_3
;
392 case AI_Output_Control_Register
:
393 offset
= M_Offset_AI_Output_Control
;
395 case AI_Personal_Register
:
396 offset
= M_Offset_AI_Personal
;
398 case AI_SI2_Load_A_Register
:
399 /* this is a 32 bit register on m series boards */
400 ni_writel(dev
, data
, M_Offset_AI_SI2_Load_A
);
402 case AI_SI2_Load_B_Register
:
403 /* this is a 32 bit register on m series boards */
404 ni_writel(dev
, data
, M_Offset_AI_SI2_Load_B
);
406 case AI_START_STOP_Select_Register
:
407 offset
= M_Offset_AI_START_STOP_Select
;
409 case AI_Trigger_Select_Register
:
410 offset
= M_Offset_AI_Trigger_Select
;
412 case Analog_Trigger_Etc_Register
:
413 offset
= M_Offset_Analog_Trigger_Etc
;
415 case AO_Command_1_Register
:
416 offset
= M_Offset_AO_Command_1
;
418 case AO_Command_2_Register
:
419 offset
= M_Offset_AO_Command_2
;
421 case AO_Mode_1_Register
:
422 offset
= M_Offset_AO_Mode_1
;
424 case AO_Mode_2_Register
:
425 offset
= M_Offset_AO_Mode_2
;
427 case AO_Mode_3_Register
:
428 offset
= M_Offset_AO_Mode_3
;
430 case AO_Output_Control_Register
:
431 offset
= M_Offset_AO_Output_Control
;
433 case AO_Personal_Register
:
434 offset
= M_Offset_AO_Personal
;
436 case AO_Start_Select_Register
:
437 offset
= M_Offset_AO_Start_Select
;
439 case AO_Trigger_Select_Register
:
440 offset
= M_Offset_AO_Trigger_Select
;
442 case Clock_and_FOUT_Register
:
443 offset
= M_Offset_Clock_and_FOUT
;
445 case Configuration_Memory_Clear
:
446 offset
= M_Offset_Configuration_Memory_Clear
;
449 offset
= M_Offset_AO_FIFO_Clear
;
451 case DIO_Control_Register
:
452 dev_dbg(dev
->class_dev
,
453 "%s: FIXME: register 0x%x does not map cleanly on to m-series boards\n",
456 case G_Autoincrement_Register(0):
457 offset
= M_Offset_G0_Autoincrement
;
459 case G_Autoincrement_Register(1):
460 offset
= M_Offset_G1_Autoincrement
;
462 case G_Command_Register(0):
463 offset
= M_Offset_G0_Command
;
465 case G_Command_Register(1):
466 offset
= M_Offset_G1_Command
;
468 case G_Input_Select_Register(0):
469 offset
= M_Offset_G0_Input_Select
;
471 case G_Input_Select_Register(1):
472 offset
= M_Offset_G1_Input_Select
;
474 case G_Mode_Register(0):
475 offset
= M_Offset_G0_Mode
;
477 case G_Mode_Register(1):
478 offset
= M_Offset_G1_Mode
;
480 case Interrupt_A_Ack_Register
:
481 offset
= M_Offset_Interrupt_A_Ack
;
483 case Interrupt_A_Enable_Register
:
484 offset
= M_Offset_Interrupt_A_Enable
;
486 case Interrupt_B_Ack_Register
:
487 offset
= M_Offset_Interrupt_B_Ack
;
489 case Interrupt_B_Enable_Register
:
490 offset
= M_Offset_Interrupt_B_Enable
;
492 case Interrupt_Control_Register
:
493 offset
= M_Offset_Interrupt_Control
;
495 case IO_Bidirection_Pin_Register
:
496 offset
= M_Offset_IO_Bidirection_Pin
;
498 case Joint_Reset_Register
:
499 offset
= M_Offset_Joint_Reset
;
501 case RTSI_Trig_A_Output_Register
:
502 offset
= M_Offset_RTSI_Trig_A_Output
;
504 case RTSI_Trig_B_Output_Register
:
505 offset
= M_Offset_RTSI_Trig_B_Output
;
507 case RTSI_Trig_Direction_Register
:
508 offset
= M_Offset_RTSI_Trig_Direction
;
511 * FIXME: DIO_Output_Register (16 bit reg) is replaced by
512 * M_Offset_Static_Digital_Output (32 bit) and
513 * M_Offset_SCXI_Serial_Data_Out (8 bit)
516 dev_warn(dev
->class_dev
,
517 "%s: bug! unhandled register=0x%x in switch\n",
521 ni_writew(dev
, data
, offset
);
524 static uint32_t m_series_stc_readl(struct comedi_device
*dev
, int reg
)
529 case G_HW_Save_Register(0):
530 offset
= M_Offset_G0_HW_Save
;
532 case G_HW_Save_Register(1):
533 offset
= M_Offset_G1_HW_Save
;
535 case G_Save_Register(0):
536 offset
= M_Offset_G0_Save
;
538 case G_Save_Register(1):
539 offset
= M_Offset_G1_Save
;
542 dev_warn(dev
->class_dev
,
543 "%s: bug! unhandled register=0x%x in switch\n",
547 return ni_readl(dev
, offset
);
550 static uint16_t m_series_stc_readw(struct comedi_device
*dev
, int reg
)
555 case AI_Status_1_Register
:
556 offset
= M_Offset_AI_Status_1
;
558 case AO_Status_1_Register
:
559 offset
= M_Offset_AO_Status_1
;
561 case AO_Status_2_Register
:
562 offset
= M_Offset_AO_Status_2
;
564 case DIO_Serial_Input_Register
:
565 return ni_readb(dev
, M_Offset_SCXI_Serial_Data_In
);
566 case Joint_Status_1_Register
:
567 offset
= M_Offset_Joint_Status_1
;
569 case Joint_Status_2_Register
:
570 offset
= M_Offset_Joint_Status_2
;
572 case G_Status_Register
:
573 offset
= M_Offset_G01_Status
;
576 dev_warn(dev
->class_dev
,
577 "%s: bug! unhandled register=0x%x in switch\n",
581 return ni_readw(dev
, offset
);
584 static void ni_stc_writew(struct comedi_device
*dev
, uint16_t data
, int reg
)
586 struct ni_private
*devpriv
= dev
->private;
589 if (devpriv
->is_m_series
) {
590 m_series_stc_writew(dev
, data
, reg
);
592 spin_lock_irqsave(&devpriv
->window_lock
, flags
);
593 if (!devpriv
->mite
&& reg
< 8) {
594 ni_writew(dev
, data
, reg
* 2);
596 ni_writew(dev
, reg
, Window_Address
);
597 ni_writew(dev
, data
, Window_Data
);
599 spin_unlock_irqrestore(&devpriv
->window_lock
, flags
);
603 static void ni_stc_writel(struct comedi_device
*dev
, uint32_t data
, int reg
)
605 struct ni_private
*devpriv
= dev
->private;
607 if (devpriv
->is_m_series
) {
608 m_series_stc_writel(dev
, data
, reg
);
610 ni_stc_writew(dev
, data
>> 16, reg
);
611 ni_stc_writew(dev
, data
& 0xffff, reg
+ 1);
615 static uint16_t ni_stc_readw(struct comedi_device
*dev
, int reg
)
617 struct ni_private
*devpriv
= dev
->private;
621 if (devpriv
->is_m_series
) {
622 val
= m_series_stc_readw(dev
, reg
);
624 spin_lock_irqsave(&devpriv
->window_lock
, flags
);
625 if (!devpriv
->mite
&& reg
< 8) {
626 val
= ni_readw(dev
, reg
* 2);
628 ni_writew(dev
, reg
, Window_Address
);
629 val
= ni_readw(dev
, Window_Data
);
631 spin_unlock_irqrestore(&devpriv
->window_lock
, flags
);
636 static uint32_t ni_stc_readl(struct comedi_device
*dev
, int reg
)
638 struct ni_private
*devpriv
= dev
->private;
641 if (devpriv
->is_m_series
) {
642 val
= m_series_stc_readl(dev
, reg
);
644 val
= ni_stc_readw(dev
, reg
) << 16;
645 val
|= ni_stc_readw(dev
, reg
+ 1);
650 static inline void ni_set_bitfield(struct comedi_device
*dev
, int reg
,
651 unsigned bit_mask
, unsigned bit_values
)
653 struct ni_private
*devpriv
= dev
->private;
656 spin_lock_irqsave(&devpriv
->soft_reg_copy_lock
, flags
);
658 case Interrupt_A_Enable_Register
:
659 devpriv
->int_a_enable_reg
&= ~bit_mask
;
660 devpriv
->int_a_enable_reg
|= bit_values
& bit_mask
;
661 ni_stc_writew(dev
, devpriv
->int_a_enable_reg
,
662 Interrupt_A_Enable_Register
);
664 case Interrupt_B_Enable_Register
:
665 devpriv
->int_b_enable_reg
&= ~bit_mask
;
666 devpriv
->int_b_enable_reg
|= bit_values
& bit_mask
;
667 ni_stc_writew(dev
, devpriv
->int_b_enable_reg
,
668 Interrupt_B_Enable_Register
);
670 case IO_Bidirection_Pin_Register
:
671 devpriv
->io_bidirection_pin_reg
&= ~bit_mask
;
672 devpriv
->io_bidirection_pin_reg
|= bit_values
& bit_mask
;
673 ni_stc_writew(dev
, devpriv
->io_bidirection_pin_reg
,
674 IO_Bidirection_Pin_Register
);
677 devpriv
->ai_ao_select_reg
&= ~bit_mask
;
678 devpriv
->ai_ao_select_reg
|= bit_values
& bit_mask
;
679 ni_writeb(dev
, devpriv
->ai_ao_select_reg
, AI_AO_Select
);
682 devpriv
->g0_g1_select_reg
&= ~bit_mask
;
683 devpriv
->g0_g1_select_reg
|= bit_values
& bit_mask
;
684 ni_writeb(dev
, devpriv
->g0_g1_select_reg
, G0_G1_Select
);
687 printk("Warning %s() called with invalid register\n", __func__
);
688 printk("reg is %d\n", reg
);
692 spin_unlock_irqrestore(&devpriv
->soft_reg_copy_lock
, flags
);
696 /* DMA channel setup */
698 /* negative channel means no channel */
699 static inline void ni_set_ai_dma_channel(struct comedi_device
*dev
, int channel
)
705 (ni_stc_dma_channel_select_bitfield(channel
) <<
706 AI_DMA_Select_Shift
) & AI_DMA_Select_Mask
;
710 ni_set_bitfield(dev
, AI_AO_Select
, AI_DMA_Select_Mask
, bitfield
);
713 /* negative channel means no channel */
714 static inline void ni_set_ao_dma_channel(struct comedi_device
*dev
, int channel
)
720 (ni_stc_dma_channel_select_bitfield(channel
) <<
721 AO_DMA_Select_Shift
) & AO_DMA_Select_Mask
;
725 ni_set_bitfield(dev
, AI_AO_Select
, AO_DMA_Select_Mask
, bitfield
);
728 /* negative mite_channel means no channel */
729 static inline void ni_set_gpct_dma_channel(struct comedi_device
*dev
,
735 if (mite_channel
>= 0)
736 bitfield
= GPCT_DMA_Select_Bits(gpct_index
, mite_channel
);
739 ni_set_bitfield(dev
, G0_G1_Select
, GPCT_DMA_Select_Mask(gpct_index
),
743 /* negative mite_channel means no channel */
744 static inline void ni_set_cdo_dma_channel(struct comedi_device
*dev
,
747 struct ni_private
*devpriv
= dev
->private;
750 spin_lock_irqsave(&devpriv
->soft_reg_copy_lock
, flags
);
751 devpriv
->cdio_dma_select_reg
&= ~CDO_DMA_Select_Mask
;
752 if (mite_channel
>= 0) {
753 /*XXX just guessing ni_stc_dma_channel_select_bitfield() returns the right bits,
754 under the assumption the cdio dma selection works just like ai/ao/gpct.
755 Definitely works for dma channels 0 and 1. */
756 devpriv
->cdio_dma_select_reg
|=
757 (ni_stc_dma_channel_select_bitfield(mite_channel
) <<
758 CDO_DMA_Select_Shift
) & CDO_DMA_Select_Mask
;
760 ni_writeb(dev
, devpriv
->cdio_dma_select_reg
, M_Offset_CDIO_DMA_Select
);
762 spin_unlock_irqrestore(&devpriv
->soft_reg_copy_lock
, flags
);
765 static int ni_request_ai_mite_channel(struct comedi_device
*dev
)
767 struct ni_private
*devpriv
= dev
->private;
770 spin_lock_irqsave(&devpriv
->mite_channel_lock
, flags
);
771 BUG_ON(devpriv
->ai_mite_chan
);
772 devpriv
->ai_mite_chan
=
773 mite_request_channel(devpriv
->mite
, devpriv
->ai_mite_ring
);
774 if (devpriv
->ai_mite_chan
== NULL
) {
775 spin_unlock_irqrestore(&devpriv
->mite_channel_lock
, flags
);
777 "failed to reserve mite dma channel for analog input.");
780 devpriv
->ai_mite_chan
->dir
= COMEDI_INPUT
;
781 ni_set_ai_dma_channel(dev
, devpriv
->ai_mite_chan
->channel
);
782 spin_unlock_irqrestore(&devpriv
->mite_channel_lock
, flags
);
786 static int ni_request_ao_mite_channel(struct comedi_device
*dev
)
788 struct ni_private
*devpriv
= dev
->private;
791 spin_lock_irqsave(&devpriv
->mite_channel_lock
, flags
);
792 BUG_ON(devpriv
->ao_mite_chan
);
793 devpriv
->ao_mite_chan
=
794 mite_request_channel(devpriv
->mite
, devpriv
->ao_mite_ring
);
795 if (devpriv
->ao_mite_chan
== NULL
) {
796 spin_unlock_irqrestore(&devpriv
->mite_channel_lock
, flags
);
798 "failed to reserve mite dma channel for analog outut.");
801 devpriv
->ao_mite_chan
->dir
= COMEDI_OUTPUT
;
802 ni_set_ao_dma_channel(dev
, devpriv
->ao_mite_chan
->channel
);
803 spin_unlock_irqrestore(&devpriv
->mite_channel_lock
, flags
);
807 static int ni_request_gpct_mite_channel(struct comedi_device
*dev
,
809 enum comedi_io_direction direction
)
811 struct ni_private
*devpriv
= dev
->private;
813 struct mite_channel
*mite_chan
;
815 BUG_ON(gpct_index
>= NUM_GPCT
);
816 spin_lock_irqsave(&devpriv
->mite_channel_lock
, flags
);
817 BUG_ON(devpriv
->counter_dev
->counters
[gpct_index
].mite_chan
);
819 mite_request_channel(devpriv
->mite
,
820 devpriv
->gpct_mite_ring
[gpct_index
]);
821 if (mite_chan
== NULL
) {
822 spin_unlock_irqrestore(&devpriv
->mite_channel_lock
, flags
);
824 "failed to reserve mite dma channel for counter.");
827 mite_chan
->dir
= direction
;
828 ni_tio_set_mite_channel(&devpriv
->counter_dev
->counters
[gpct_index
],
830 ni_set_gpct_dma_channel(dev
, gpct_index
, mite_chan
->channel
);
831 spin_unlock_irqrestore(&devpriv
->mite_channel_lock
, flags
);
837 static int ni_request_cdo_mite_channel(struct comedi_device
*dev
)
840 struct ni_private
*devpriv
= dev
->private;
843 spin_lock_irqsave(&devpriv
->mite_channel_lock
, flags
);
844 BUG_ON(devpriv
->cdo_mite_chan
);
845 devpriv
->cdo_mite_chan
=
846 mite_request_channel(devpriv
->mite
, devpriv
->cdo_mite_ring
);
847 if (devpriv
->cdo_mite_chan
== NULL
) {
848 spin_unlock_irqrestore(&devpriv
->mite_channel_lock
, flags
);
850 "failed to reserve mite dma channel for correlated digital outut.");
853 devpriv
->cdo_mite_chan
->dir
= COMEDI_OUTPUT
;
854 ni_set_cdo_dma_channel(dev
, devpriv
->cdo_mite_chan
->channel
);
855 spin_unlock_irqrestore(&devpriv
->mite_channel_lock
, flags
);
860 static void ni_release_ai_mite_channel(struct comedi_device
*dev
)
863 struct ni_private
*devpriv
= dev
->private;
866 spin_lock_irqsave(&devpriv
->mite_channel_lock
, flags
);
867 if (devpriv
->ai_mite_chan
) {
868 ni_set_ai_dma_channel(dev
, -1);
869 mite_release_channel(devpriv
->ai_mite_chan
);
870 devpriv
->ai_mite_chan
= NULL
;
872 spin_unlock_irqrestore(&devpriv
->mite_channel_lock
, flags
);
876 static void ni_release_ao_mite_channel(struct comedi_device
*dev
)
879 struct ni_private
*devpriv
= dev
->private;
882 spin_lock_irqsave(&devpriv
->mite_channel_lock
, flags
);
883 if (devpriv
->ao_mite_chan
) {
884 ni_set_ao_dma_channel(dev
, -1);
885 mite_release_channel(devpriv
->ao_mite_chan
);
886 devpriv
->ao_mite_chan
= NULL
;
888 spin_unlock_irqrestore(&devpriv
->mite_channel_lock
, flags
);
893 static void ni_release_gpct_mite_channel(struct comedi_device
*dev
,
896 struct ni_private
*devpriv
= dev
->private;
899 BUG_ON(gpct_index
>= NUM_GPCT
);
900 spin_lock_irqsave(&devpriv
->mite_channel_lock
, flags
);
901 if (devpriv
->counter_dev
->counters
[gpct_index
].mite_chan
) {
902 struct mite_channel
*mite_chan
=
903 devpriv
->counter_dev
->counters
[gpct_index
].mite_chan
;
905 ni_set_gpct_dma_channel(dev
, gpct_index
, -1);
906 ni_tio_set_mite_channel(&devpriv
->
907 counter_dev
->counters
[gpct_index
],
909 mite_release_channel(mite_chan
);
911 spin_unlock_irqrestore(&devpriv
->mite_channel_lock
, flags
);
915 static void ni_release_cdo_mite_channel(struct comedi_device
*dev
)
918 struct ni_private
*devpriv
= dev
->private;
921 spin_lock_irqsave(&devpriv
->mite_channel_lock
, flags
);
922 if (devpriv
->cdo_mite_chan
) {
923 ni_set_cdo_dma_channel(dev
, -1);
924 mite_release_channel(devpriv
->cdo_mite_chan
);
925 devpriv
->cdo_mite_chan
= NULL
;
927 spin_unlock_irqrestore(&devpriv
->mite_channel_lock
, flags
);
932 static void ni_e_series_enable_second_irq(struct comedi_device
*dev
,
933 unsigned gpct_index
, short enable
)
935 struct ni_private
*devpriv
= dev
->private;
939 if (devpriv
->is_m_series
|| gpct_index
> 1)
943 * e-series boards use the second irq signals to generate
944 * dma requests for their counters
946 if (gpct_index
== 0) {
947 reg
= Second_IRQ_A_Enable_Register
;
949 val
= G0_Gate_Second_Irq_Enable
;
951 reg
= Second_IRQ_B_Enable_Register
;
953 val
= G1_Gate_Second_Irq_Enable
;
955 ni_stc_writew(dev
, val
, reg
);
959 static void ni_clear_ai_fifo(struct comedi_device
*dev
)
961 struct ni_private
*devpriv
= dev
->private;
962 static const int timeout
= 10000;
965 if (devpriv
->is_6143
) {
966 /* Flush the 6143 data FIFO */
967 ni_writel(dev
, 0x10, AIFIFO_Control_6143
);
968 ni_writel(dev
, 0x00, AIFIFO_Control_6143
);
969 /* Wait for complete */
970 for (i
= 0; i
< timeout
; i
++) {
971 if (!(ni_readl(dev
, AIFIFO_Status_6143
) & 0x10))
976 comedi_error(dev
, "FIFO flush timeout.");
979 ni_stc_writew(dev
, 1, ADC_FIFO_Clear
);
980 if (devpriv
->is_625x
) {
981 ni_writeb(dev
, 0, M_Offset_Static_AI_Control(0));
982 ni_writeb(dev
, 1, M_Offset_Static_AI_Control(0));
984 /* the NI example code does 3 convert pulses for 625x boards,
985 but that appears to be wrong in practice. */
986 ni_stc_writew(dev
, AI_CONVERT_Pulse
,
987 AI_Command_1_Register
);
988 ni_stc_writew(dev
, AI_CONVERT_Pulse
,
989 AI_Command_1_Register
);
990 ni_stc_writew(dev
, AI_CONVERT_Pulse
,
991 AI_Command_1_Register
);
997 static inline void ni_ao_win_outw(struct comedi_device
*dev
, uint16_t data
,
1000 struct ni_private
*devpriv
= dev
->private;
1001 unsigned long flags
;
1003 spin_lock_irqsave(&devpriv
->window_lock
, flags
);
1004 ni_writew(dev
, addr
, AO_Window_Address_611x
);
1005 ni_writew(dev
, data
, AO_Window_Data_611x
);
1006 spin_unlock_irqrestore(&devpriv
->window_lock
, flags
);
1009 static inline void ni_ao_win_outl(struct comedi_device
*dev
, uint32_t data
,
1012 struct ni_private
*devpriv
= dev
->private;
1013 unsigned long flags
;
1015 spin_lock_irqsave(&devpriv
->window_lock
, flags
);
1016 ni_writew(dev
, addr
, AO_Window_Address_611x
);
1017 ni_writel(dev
, data
, AO_Window_Data_611x
);
1018 spin_unlock_irqrestore(&devpriv
->window_lock
, flags
);
1021 static inline unsigned short ni_ao_win_inw(struct comedi_device
*dev
, int addr
)
1023 struct ni_private
*devpriv
= dev
->private;
1024 unsigned long flags
;
1025 unsigned short data
;
1027 spin_lock_irqsave(&devpriv
->window_lock
, flags
);
1028 ni_writew(dev
, addr
, AO_Window_Address_611x
);
1029 data
= ni_readw(dev
, AO_Window_Data_611x
);
1030 spin_unlock_irqrestore(&devpriv
->window_lock
, flags
);
1034 /* ni_set_bits( ) allows different parts of the ni_mio_common driver to
1035 * share registers (such as Interrupt_A_Register) without interfering with
1038 * NOTE: the switch/case statements are optimized out for a constant argument
1039 * so this is actually quite fast--- If you must wrap another function around this
1040 * make it inline to avoid a large speed penalty.
1042 * value should only be 1 or 0.
1044 static inline void ni_set_bits(struct comedi_device
*dev
, int reg
,
1045 unsigned bits
, unsigned value
)
1047 unsigned bit_values
;
1053 ni_set_bitfield(dev
, reg
, bits
, bit_values
);
1057 static void ni_sync_ai_dma(struct comedi_device
*dev
)
1059 struct ni_private
*devpriv
= dev
->private;
1060 struct comedi_subdevice
*s
= dev
->read_subdev
;
1061 unsigned long flags
;
1063 spin_lock_irqsave(&devpriv
->mite_channel_lock
, flags
);
1064 if (devpriv
->ai_mite_chan
)
1065 mite_sync_input_dma(devpriv
->ai_mite_chan
, s
);
1066 spin_unlock_irqrestore(&devpriv
->mite_channel_lock
, flags
);
1069 static int ni_ai_drain_dma(struct comedi_device
*dev
)
1071 struct ni_private
*devpriv
= dev
->private;
1073 static const int timeout
= 10000;
1074 unsigned long flags
;
1077 spin_lock_irqsave(&devpriv
->mite_channel_lock
, flags
);
1078 if (devpriv
->ai_mite_chan
) {
1079 for (i
= 0; i
< timeout
; i
++) {
1080 if ((ni_stc_readw(dev
, AI_Status_1_Register
) &
1082 && mite_bytes_in_transit(devpriv
->ai_mite_chan
) ==
1088 printk("ni_mio_common: wait for dma drain timed out\n");
1090 ("mite_bytes_in_transit=%i, AI_Status1_Register=0x%x\n",
1091 mite_bytes_in_transit(devpriv
->ai_mite_chan
),
1092 ni_stc_readw(dev
, AI_Status_1_Register
));
1096 spin_unlock_irqrestore(&devpriv
->mite_channel_lock
, flags
);
1098 ni_sync_ai_dma(dev
);
1103 static void mite_handle_b_linkc(struct mite_struct
*mite
,
1104 struct comedi_device
*dev
)
1106 struct ni_private
*devpriv
= dev
->private;
1107 struct comedi_subdevice
*s
= dev
->write_subdev
;
1108 unsigned long flags
;
1110 spin_lock_irqsave(&devpriv
->mite_channel_lock
, flags
);
1111 if (devpriv
->ao_mite_chan
)
1112 mite_sync_output_dma(devpriv
->ao_mite_chan
, s
);
1113 spin_unlock_irqrestore(&devpriv
->mite_channel_lock
, flags
);
1116 static int ni_ao_wait_for_dma_load(struct comedi_device
*dev
)
1118 static const int timeout
= 10000;
1120 for (i
= 0; i
< timeout
; i
++) {
1121 unsigned short b_status
;
1123 b_status
= ni_stc_readw(dev
, AO_Status_1_Register
);
1124 if (b_status
& AO_FIFO_Half_Full_St
)
1126 /* if we poll too often, the pci bus activity seems
1127 to slow the dma transfer down */
1131 comedi_error(dev
, "timed out waiting for dma load");
1140 static void ni_ao_fifo_load(struct comedi_device
*dev
,
1141 struct comedi_subdevice
*s
, int n
)
1143 struct ni_private
*devpriv
= dev
->private;
1144 struct comedi_async
*async
= s
->async
;
1145 struct comedi_cmd
*cmd
= &async
->cmd
;
1153 chan
= async
->cur_chan
;
1154 for (i
= 0; i
< n
; i
++) {
1155 err
&= comedi_buf_get(s
, &d
);
1159 range
= CR_RANGE(cmd
->chanlist
[chan
]);
1161 if (devpriv
->is_6xxx
) {
1162 packed_data
= d
& 0xffff;
1163 /* 6711 only has 16 bit wide ao fifo */
1164 if (!devpriv
->is_6711
) {
1165 err
&= comedi_buf_get(s
, &d
);
1170 packed_data
|= (d
<< 16) & 0xffff0000;
1172 ni_writel(dev
, packed_data
, DAC_FIFO_Data_611x
);
1174 ni_writew(dev
, d
, DAC_FIFO_Data
);
1177 chan
%= cmd
->chanlist_len
;
1179 async
->cur_chan
= chan
;
1181 async
->events
|= COMEDI_CB_OVERFLOW
;
1185 * There's a small problem if the FIFO gets really low and we
1186 * don't have the data to fill it. Basically, if after we fill
1187 * the FIFO with all the data available, the FIFO is _still_
1188 * less than half full, we never clear the interrupt. If the
1189 * IRQ is in edge mode, we never get another interrupt, because
1190 * this one wasn't cleared. If in level mode, we get flooded
1191 * with interrupts that we can't fulfill, because nothing ever
1192 * gets put into the buffer.
1194 * This kind of situation is recoverable, but it is easier to
1195 * just pretend we had a FIFO underrun, since there is a good
1196 * chance it will happen anyway. This is _not_ the case for
1197 * RT code, as RT code might purposely be running close to the
1198 * metal. Needs to be fixed eventually.
1200 static int ni_ao_fifo_half_empty(struct comedi_device
*dev
,
1201 struct comedi_subdevice
*s
)
1203 const struct ni_board_struct
*board
= comedi_board(dev
);
1206 n
= comedi_buf_read_n_available(s
);
1208 s
->async
->events
|= COMEDI_CB_OVERFLOW
;
1213 if (n
> board
->ao_fifo_depth
/ 2)
1214 n
= board
->ao_fifo_depth
/ 2;
1216 ni_ao_fifo_load(dev
, s
, n
);
1218 s
->async
->events
|= COMEDI_CB_BLOCK
;
1223 static int ni_ao_prep_fifo(struct comedi_device
*dev
,
1224 struct comedi_subdevice
*s
)
1226 const struct ni_board_struct
*board
= comedi_board(dev
);
1227 struct ni_private
*devpriv
= dev
->private;
1231 ni_stc_writew(dev
, 1, DAC_FIFO_Clear
);
1232 if (devpriv
->is_6xxx
)
1233 ni_ao_win_outl(dev
, 0x6, AO_FIFO_Offset_Load_611x
);
1235 /* load some data */
1236 n
= comedi_buf_read_n_available(s
);
1241 if (n
> board
->ao_fifo_depth
)
1242 n
= board
->ao_fifo_depth
;
1244 ni_ao_fifo_load(dev
, s
, n
);
1249 static void ni_ai_fifo_read(struct comedi_device
*dev
,
1250 struct comedi_subdevice
*s
, int n
)
1252 struct ni_private
*devpriv
= dev
->private;
1253 struct comedi_async
*async
= s
->async
;
1256 if (devpriv
->is_611x
) {
1257 unsigned short data
[2];
1260 for (i
= 0; i
< n
/ 2; i
++) {
1261 dl
= ni_readl(dev
, ADC_FIFO_Data_611x
);
1262 /* This may get the hi/lo data in the wrong order */
1263 data
[0] = (dl
>> 16) & 0xffff;
1264 data
[1] = dl
& 0xffff;
1265 cfc_write_array_to_buffer(s
, data
, sizeof(data
));
1267 /* Check if there's a single sample stuck in the FIFO */
1269 dl
= ni_readl(dev
, ADC_FIFO_Data_611x
);
1270 data
[0] = dl
& 0xffff;
1271 cfc_write_to_buffer(s
, data
[0]);
1273 } else if (devpriv
->is_6143
) {
1274 unsigned short data
[2];
1277 /* This just reads the FIFO assuming the data is present, no checks on the FIFO status are performed */
1278 for (i
= 0; i
< n
/ 2; i
++) {
1279 dl
= ni_readl(dev
, AIFIFO_Data_6143
);
1281 data
[0] = (dl
>> 16) & 0xffff;
1282 data
[1] = dl
& 0xffff;
1283 cfc_write_array_to_buffer(s
, data
, sizeof(data
));
1286 /* Assume there is a single sample stuck in the FIFO */
1287 /* Get stranded sample into FIFO */
1288 ni_writel(dev
, 0x01, AIFIFO_Control_6143
);
1289 dl
= ni_readl(dev
, AIFIFO_Data_6143
);
1290 data
[0] = (dl
>> 16) & 0xffff;
1291 cfc_write_to_buffer(s
, data
[0]);
1294 if (n
> sizeof(devpriv
->ai_fifo_buffer
) /
1295 sizeof(devpriv
->ai_fifo_buffer
[0])) {
1296 comedi_error(dev
, "bug! ai_fifo_buffer too small");
1297 async
->events
|= COMEDI_CB_ERROR
;
1300 for (i
= 0; i
< n
; i
++) {
1301 devpriv
->ai_fifo_buffer
[i
] =
1302 ni_readw(dev
, ADC_FIFO_Data_Register
);
1304 cfc_write_array_to_buffer(s
, devpriv
->ai_fifo_buffer
,
1306 sizeof(devpriv
->ai_fifo_buffer
[0]));
1310 static void ni_handle_fifo_half_full(struct comedi_device
*dev
)
1312 const struct ni_board_struct
*board
= comedi_board(dev
);
1313 struct comedi_subdevice
*s
= dev
->read_subdev
;
1316 n
= board
->ai_fifo_depth
/ 2;
1318 ni_ai_fifo_read(dev
, s
, n
);
1325 static void ni_handle_fifo_dregs(struct comedi_device
*dev
)
1327 struct ni_private
*devpriv
= dev
->private;
1328 struct comedi_subdevice
*s
= dev
->read_subdev
;
1329 unsigned short data
[2];
1331 unsigned short fifo_empty
;
1334 if (devpriv
->is_611x
) {
1335 while ((ni_stc_readw(dev
, AI_Status_1_Register
) &
1336 AI_FIFO_Empty_St
) == 0) {
1337 dl
= ni_readl(dev
, ADC_FIFO_Data_611x
);
1339 /* This may get the hi/lo data in the wrong order */
1340 data
[0] = (dl
>> 16);
1341 data
[1] = (dl
& 0xffff);
1342 cfc_write_array_to_buffer(s
, data
, sizeof(data
));
1344 } else if (devpriv
->is_6143
) {
1346 while (ni_readl(dev
, AIFIFO_Status_6143
) & 0x04) {
1347 dl
= ni_readl(dev
, AIFIFO_Data_6143
);
1349 /* This may get the hi/lo data in the wrong order */
1350 data
[0] = (dl
>> 16);
1351 data
[1] = (dl
& 0xffff);
1352 cfc_write_array_to_buffer(s
, data
, sizeof(data
));
1355 /* Check if stranded sample is present */
1356 if (ni_readl(dev
, AIFIFO_Status_6143
) & 0x01) {
1357 /* Get stranded sample into FIFO */
1358 ni_writel(dev
, 0x01, AIFIFO_Control_6143
);
1359 dl
= ni_readl(dev
, AIFIFO_Data_6143
);
1360 data
[0] = (dl
>> 16) & 0xffff;
1361 cfc_write_to_buffer(s
, data
[0]);
1365 fifo_empty
= ni_stc_readw(dev
, AI_Status_1_Register
) &
1367 while (fifo_empty
== 0) {
1370 sizeof(devpriv
->ai_fifo_buffer
) /
1371 sizeof(devpriv
->ai_fifo_buffer
[0]); i
++) {
1372 fifo_empty
= ni_stc_readw(dev
,
1373 AI_Status_1_Register
) &
1377 devpriv
->ai_fifo_buffer
[i
] =
1378 ni_readw(dev
, ADC_FIFO_Data_Register
);
1380 cfc_write_array_to_buffer(s
, devpriv
->ai_fifo_buffer
,
1383 ai_fifo_buffer
[0]));
1388 static void get_last_sample_611x(struct comedi_device
*dev
)
1390 struct ni_private
*devpriv
= dev
->private;
1391 struct comedi_subdevice
*s
= dev
->read_subdev
;
1392 unsigned short data
;
1395 if (!devpriv
->is_611x
)
1398 /* Check if there's a single sample stuck in the FIFO */
1399 if (ni_readb(dev
, XXX_Status
) & 0x80) {
1400 dl
= ni_readl(dev
, ADC_FIFO_Data_611x
);
1401 data
= (dl
& 0xffff);
1402 cfc_write_to_buffer(s
, data
);
1406 static void get_last_sample_6143(struct comedi_device
*dev
)
1408 struct ni_private
*devpriv
= dev
->private;
1409 struct comedi_subdevice
*s
= dev
->read_subdev
;
1410 unsigned short data
;
1413 if (!devpriv
->is_6143
)
1416 /* Check if there's a single sample stuck in the FIFO */
1417 if (ni_readl(dev
, AIFIFO_Status_6143
) & 0x01) {
1418 /* Get stranded sample into FIFO */
1419 ni_writel(dev
, 0x01, AIFIFO_Control_6143
);
1420 dl
= ni_readl(dev
, AIFIFO_Data_6143
);
1422 /* This may get the hi/lo data in the wrong order */
1423 data
= (dl
>> 16) & 0xffff;
1424 cfc_write_to_buffer(s
, data
);
1428 static void shutdown_ai_command(struct comedi_device
*dev
)
1430 struct comedi_subdevice
*s
= dev
->read_subdev
;
1433 ni_ai_drain_dma(dev
);
1435 ni_handle_fifo_dregs(dev
);
1436 get_last_sample_611x(dev
);
1437 get_last_sample_6143(dev
);
1439 s
->async
->events
|= COMEDI_CB_EOA
;
1442 static void ni_handle_eos(struct comedi_device
*dev
, struct comedi_subdevice
*s
)
1444 struct ni_private
*devpriv
= dev
->private;
1446 if (devpriv
->aimode
== AIMODE_SCAN
) {
1448 static const int timeout
= 10;
1451 for (i
= 0; i
< timeout
; i
++) {
1452 ni_sync_ai_dma(dev
);
1453 if ((s
->async
->events
& COMEDI_CB_EOS
))
1458 ni_handle_fifo_dregs(dev
);
1459 s
->async
->events
|= COMEDI_CB_EOS
;
1462 /* handle special case of single scan using AI_End_On_End_Of_Scan */
1463 if ((devpriv
->ai_cmd2
& AI_End_On_End_Of_Scan
))
1464 shutdown_ai_command(dev
);
1467 static void handle_gpct_interrupt(struct comedi_device
*dev
,
1468 unsigned short counter_index
)
1471 struct ni_private
*devpriv
= dev
->private;
1472 struct comedi_subdevice
*s
;
1474 s
= &dev
->subdevices
[NI_GPCT_SUBDEV(counter_index
)];
1476 ni_tio_handle_interrupt(&devpriv
->counter_dev
->counters
[counter_index
],
1478 cfc_handle_events(dev
, s
);
1482 static void ack_a_interrupt(struct comedi_device
*dev
, unsigned short a_status
)
1484 unsigned short ack
= 0;
1486 if (a_status
& AI_SC_TC_St
)
1487 ack
|= AI_SC_TC_Interrupt_Ack
;
1488 if (a_status
& AI_START1_St
)
1489 ack
|= AI_START1_Interrupt_Ack
;
1490 if (a_status
& AI_START_St
)
1491 ack
|= AI_START_Interrupt_Ack
;
1492 if (a_status
& AI_STOP_St
)
1493 /* not sure why we used to ack the START here also, instead of doing it independently. Frank Hess 2007-07-06 */
1494 ack
|= AI_STOP_Interrupt_Ack
/*| AI_START_Interrupt_Ack */;
1496 ni_stc_writew(dev
, ack
, Interrupt_A_Ack_Register
);
1499 static void handle_a_interrupt(struct comedi_device
*dev
, unsigned short status
,
1500 unsigned ai_mite_status
)
1502 struct comedi_subdevice
*s
= dev
->read_subdev
;
1503 struct comedi_cmd
*cmd
= &s
->async
->cmd
;
1505 /* 67xx boards don't have ai subdevice, but their gpct0 might generate an a interrupt */
1506 if (s
->type
== COMEDI_SUBD_UNUSED
)
1510 if (ai_mite_status
& CHSR_LINKC
)
1511 ni_sync_ai_dma(dev
);
1513 if (ai_mite_status
& ~(CHSR_INT
| CHSR_LINKC
| CHSR_DONE
| CHSR_MRDY
|
1514 CHSR_DRDY
| CHSR_DRQ1
| CHSR_DRQ0
| CHSR_ERROR
|
1515 CHSR_SABORT
| CHSR_XFERR
| CHSR_LxERR_mask
)) {
1517 ("unknown mite interrupt, ack! (ai_mite_status=%08x)\n",
1519 s
->async
->events
|= COMEDI_CB_ERROR
| COMEDI_CB_EOA
;
1520 /* disable_irq(dev->irq); */
1524 /* test for all uncommon interrupt events at the same time */
1525 if (status
& (AI_Overrun_St
| AI_Overflow_St
| AI_SC_TC_Error_St
|
1526 AI_SC_TC_St
| AI_START1_St
)) {
1527 if (status
== 0xffff) {
1529 ("ni_mio_common: a_status=0xffff. Card removed?\n");
1530 /* we probably aren't even running a command now,
1531 * so it's a good idea to be careful. */
1532 if (comedi_is_subdevice_running(s
)) {
1534 COMEDI_CB_ERROR
| COMEDI_CB_EOA
;
1535 cfc_handle_events(dev
, s
);
1539 if (status
& (AI_Overrun_St
| AI_Overflow_St
|
1540 AI_SC_TC_Error_St
)) {
1541 printk("ni_mio_common: ai error a_status=%04x\n",
1544 shutdown_ai_command(dev
);
1546 s
->async
->events
|= COMEDI_CB_ERROR
;
1547 if (status
& (AI_Overrun_St
| AI_Overflow_St
))
1548 s
->async
->events
|= COMEDI_CB_OVERFLOW
;
1550 cfc_handle_events(dev
, s
);
1553 if (status
& AI_SC_TC_St
) {
1554 if (cmd
->stop_src
== TRIG_COUNT
)
1555 shutdown_ai_command(dev
);
1559 if (status
& AI_FIFO_Half_Full_St
) {
1561 static const int timeout
= 10;
1562 /* pcmcia cards (at least 6036) seem to stop producing interrupts if we
1563 *fail to get the fifo less than half full, so loop to be sure.*/
1564 for (i
= 0; i
< timeout
; ++i
) {
1565 ni_handle_fifo_half_full(dev
);
1566 if ((ni_stc_readw(dev
, AI_Status_1_Register
) &
1567 AI_FIFO_Half_Full_St
) == 0)
1571 #endif /* !PCIDMA */
1573 if ((status
& AI_STOP_St
))
1574 ni_handle_eos(dev
, s
);
1576 cfc_handle_events(dev
, s
);
1579 static void ack_b_interrupt(struct comedi_device
*dev
, unsigned short b_status
)
1581 unsigned short ack
= 0;
1583 if (b_status
& AO_BC_TC_St
)
1584 ack
|= AO_BC_TC_Interrupt_Ack
;
1585 if (b_status
& AO_Overrun_St
)
1586 ack
|= AO_Error_Interrupt_Ack
;
1587 if (b_status
& AO_START_St
)
1588 ack
|= AO_START_Interrupt_Ack
;
1589 if (b_status
& AO_START1_St
)
1590 ack
|= AO_START1_Interrupt_Ack
;
1591 if (b_status
& AO_UC_TC_St
)
1592 ack
|= AO_UC_TC_Interrupt_Ack
;
1593 if (b_status
& AO_UI2_TC_St
)
1594 ack
|= AO_UI2_TC_Interrupt_Ack
;
1595 if (b_status
& AO_UPDATE_St
)
1596 ack
|= AO_UPDATE_Interrupt_Ack
;
1598 ni_stc_writew(dev
, ack
, Interrupt_B_Ack_Register
);
1601 static void handle_b_interrupt(struct comedi_device
*dev
,
1602 unsigned short b_status
, unsigned ao_mite_status
)
1604 struct comedi_subdevice
*s
= dev
->write_subdev
;
1605 /* unsigned short ack=0; */
1608 /* Currently, mite.c requires us to handle LINKC */
1609 if (ao_mite_status
& CHSR_LINKC
) {
1610 struct ni_private
*devpriv
= dev
->private;
1612 mite_handle_b_linkc(devpriv
->mite
, dev
);
1615 if (ao_mite_status
& ~(CHSR_INT
| CHSR_LINKC
| CHSR_DONE
| CHSR_MRDY
|
1616 CHSR_DRDY
| CHSR_DRQ1
| CHSR_DRQ0
| CHSR_ERROR
|
1617 CHSR_SABORT
| CHSR_XFERR
| CHSR_LxERR_mask
)) {
1619 ("unknown mite interrupt, ack! (ao_mite_status=%08x)\n",
1621 s
->async
->events
|= COMEDI_CB_EOA
| COMEDI_CB_ERROR
;
1625 if (b_status
== 0xffff)
1627 if (b_status
& AO_Overrun_St
) {
1629 ("ni_mio_common: AO FIFO underrun status=0x%04x status2=0x%04x\n",
1630 b_status
, ni_stc_readw(dev
, AO_Status_2_Register
));
1631 s
->async
->events
|= COMEDI_CB_OVERFLOW
;
1634 if (b_status
& AO_BC_TC_St
)
1635 s
->async
->events
|= COMEDI_CB_EOA
;
1638 if (b_status
& AO_FIFO_Request_St
) {
1641 ret
= ni_ao_fifo_half_empty(dev
, s
);
1643 printk("ni_mio_common: AO buffer underrun\n");
1644 ni_set_bits(dev
, Interrupt_B_Enable_Register
,
1645 AO_FIFO_Interrupt_Enable
|
1646 AO_Error_Interrupt_Enable
, 0);
1647 s
->async
->events
|= COMEDI_CB_OVERFLOW
;
1652 cfc_handle_events(dev
, s
);
1655 static void ni_ai_munge(struct comedi_device
*dev
, struct comedi_subdevice
*s
,
1656 void *data
, unsigned int num_bytes
,
1657 unsigned int chan_index
)
1659 struct ni_private
*devpriv
= dev
->private;
1660 struct comedi_async
*async
= s
->async
;
1661 struct comedi_cmd
*cmd
= &async
->cmd
;
1662 unsigned int length
= num_bytes
/ bytes_per_sample(s
);
1663 unsigned short *array
= data
;
1664 unsigned int *larray
= data
;
1667 for (i
= 0; i
< length
; i
++) {
1669 if (s
->subdev_flags
& SDF_LSAMPL
)
1670 larray
[i
] = le32_to_cpu(larray
[i
]);
1672 array
[i
] = le16_to_cpu(array
[i
]);
1674 if (s
->subdev_flags
& SDF_LSAMPL
)
1675 larray
[i
] += devpriv
->ai_offset
[chan_index
];
1677 array
[i
] += devpriv
->ai_offset
[chan_index
];
1679 chan_index
%= cmd
->chanlist_len
;
1685 static int ni_ai_setup_MITE_dma(struct comedi_device
*dev
)
1687 struct ni_private
*devpriv
= dev
->private;
1688 struct comedi_subdevice
*s
= dev
->read_subdev
;
1690 unsigned long flags
;
1692 retval
= ni_request_ai_mite_channel(dev
);
1695 /* printk("comedi_debug: using mite channel %i for ai.\n", devpriv->ai_mite_chan->channel); */
1697 /* write alloc the entire buffer */
1698 comedi_buf_write_alloc(s
, s
->async
->prealloc_bufsz
);
1700 spin_lock_irqsave(&devpriv
->mite_channel_lock
, flags
);
1701 if (devpriv
->ai_mite_chan
== NULL
) {
1702 spin_unlock_irqrestore(&devpriv
->mite_channel_lock
, flags
);
1706 if (devpriv
->is_611x
|| devpriv
->is_6143
)
1707 mite_prep_dma(devpriv
->ai_mite_chan
, 32, 16);
1708 else if (devpriv
->is_628x
)
1709 mite_prep_dma(devpriv
->ai_mite_chan
, 32, 32);
1711 mite_prep_dma(devpriv
->ai_mite_chan
, 16, 16);
1714 mite_dma_arm(devpriv
->ai_mite_chan
);
1715 spin_unlock_irqrestore(&devpriv
->mite_channel_lock
, flags
);
1720 static int ni_ao_setup_MITE_dma(struct comedi_device
*dev
)
1722 struct ni_private
*devpriv
= dev
->private;
1723 struct comedi_subdevice
*s
= dev
->write_subdev
;
1725 unsigned long flags
;
1727 retval
= ni_request_ao_mite_channel(dev
);
1731 /* read alloc the entire buffer */
1732 comedi_buf_read_alloc(s
, s
->async
->prealloc_bufsz
);
1734 spin_lock_irqsave(&devpriv
->mite_channel_lock
, flags
);
1735 if (devpriv
->ao_mite_chan
) {
1736 if (devpriv
->is_611x
|| devpriv
->is_6713
) {
1737 mite_prep_dma(devpriv
->ao_mite_chan
, 32, 32);
1739 /* doing 32 instead of 16 bit wide transfers from memory
1740 makes the mite do 32 bit pci transfers, doubling pci bandwidth. */
1741 mite_prep_dma(devpriv
->ao_mite_chan
, 16, 32);
1743 mite_dma_arm(devpriv
->ao_mite_chan
);
1746 spin_unlock_irqrestore(&devpriv
->mite_channel_lock
, flags
);
1754 used for both cancel ioctl and board initialization
1756 this is pretty harsh for a cancel, but it works...
1759 static int ni_ai_reset(struct comedi_device
*dev
, struct comedi_subdevice
*s
)
1761 struct ni_private
*devpriv
= dev
->private;
1763 ni_release_ai_mite_channel(dev
);
1764 /* ai configuration */
1765 ni_stc_writew(dev
, AI_Configuration_Start
| AI_Reset
,
1766 Joint_Reset_Register
);
1768 ni_set_bits(dev
, Interrupt_A_Enable_Register
,
1769 AI_SC_TC_Interrupt_Enable
| AI_START1_Interrupt_Enable
|
1770 AI_START2_Interrupt_Enable
| AI_START_Interrupt_Enable
|
1771 AI_STOP_Interrupt_Enable
| AI_Error_Interrupt_Enable
|
1772 AI_FIFO_Interrupt_Enable
, 0);
1774 ni_clear_ai_fifo(dev
);
1776 if (!devpriv
->is_6143
)
1777 ni_writeb(dev
, 0, Misc_Command
);
1779 ni_stc_writew(dev
, AI_Disarm
, AI_Command_1_Register
); /* reset pulses */
1780 ni_stc_writew(dev
, AI_Start_Stop
| AI_Mode_1_Reserved
1781 /*| AI_Trigger_Once */,
1782 AI_Mode_1_Register
);
1783 ni_stc_writew(dev
, 0x0000, AI_Mode_2_Register
);
1784 /* generate FIFO interrupts on non-empty */
1785 ni_stc_writew(dev
, (0 << 6) | 0x0000, AI_Mode_3_Register
);
1786 if (devpriv
->is_611x
) {
1788 AI_SHIFTIN_Pulse_Width
|
1790 AI_LOCALMUX_CLK_Pulse_Width
,
1791 AI_Personal_Register
);
1793 AI_SCAN_IN_PROG_Output_Select(3) |
1794 AI_EXTMUX_CLK_Output_Select(0) |
1795 AI_LOCALMUX_CLK_Output_Select(2) |
1796 AI_SC_TC_Output_Select(3) |
1797 AI_CONVERT_Output_Select
1798 (AI_CONVERT_Output_Enable_High
),
1799 AI_Output_Control_Register
);
1800 } else if (devpriv
->is_6143
) {
1801 ni_stc_writew(dev
, AI_SHIFTIN_Pulse_Width
|
1803 AI_LOCALMUX_CLK_Pulse_Width
,
1804 AI_Personal_Register
);
1806 AI_SCAN_IN_PROG_Output_Select(3) |
1807 AI_EXTMUX_CLK_Output_Select(0) |
1808 AI_LOCALMUX_CLK_Output_Select(2) |
1809 AI_SC_TC_Output_Select(3) |
1810 AI_CONVERT_Output_Select
1811 (AI_CONVERT_Output_Enable_Low
),
1812 AI_Output_Control_Register
);
1814 unsigned ai_output_control_bits
;
1816 AI_SHIFTIN_Pulse_Width
|
1818 AI_CONVERT_Pulse_Width
|
1819 AI_LOCALMUX_CLK_Pulse_Width
,
1820 AI_Personal_Register
);
1821 ai_output_control_bits
=
1822 AI_SCAN_IN_PROG_Output_Select(3) |
1823 AI_EXTMUX_CLK_Output_Select(0) |
1824 AI_LOCALMUX_CLK_Output_Select(2) |
1825 AI_SC_TC_Output_Select(3);
1826 if (devpriv
->is_622x
)
1827 ai_output_control_bits
|=
1828 AI_CONVERT_Output_Select
1829 (AI_CONVERT_Output_Enable_High
);
1831 ai_output_control_bits
|=
1832 AI_CONVERT_Output_Select
1833 (AI_CONVERT_Output_Enable_Low
);
1834 ni_stc_writew(dev
, ai_output_control_bits
,
1835 AI_Output_Control_Register
);
1837 /* the following registers should not be changed, because there
1838 * are no backup registers in devpriv. If you want to change
1839 * any of these, add a backup register and other appropriate code:
1840 * AI_Mode_1_Register
1841 * AI_Mode_3_Register
1842 * AI_Personal_Register
1843 * AI_Output_Control_Register
1846 AI_SC_TC_Error_Confirm
|
1847 AI_START_Interrupt_Ack
|
1848 AI_START2_Interrupt_Ack
|
1849 AI_START1_Interrupt_Ack
|
1850 AI_SC_TC_Interrupt_Ack
|
1851 AI_Error_Interrupt_Ack
|
1852 AI_STOP_Interrupt_Ack
,
1853 Interrupt_A_Ack_Register
); /* clear interrupts */
1855 ni_stc_writew(dev
, AI_Configuration_End
, Joint_Reset_Register
);
1860 static int ni_ai_poll(struct comedi_device
*dev
, struct comedi_subdevice
*s
)
1862 unsigned long flags
;
1865 /* lock to avoid race with interrupt handler */
1866 spin_lock_irqsave(&dev
->spinlock
, flags
);
1868 ni_handle_fifo_dregs(dev
);
1870 ni_sync_ai_dma(dev
);
1872 count
= comedi_buf_n_bytes_ready(s
);
1873 spin_unlock_irqrestore(&dev
->spinlock
, flags
);
1878 static void ni_prime_channelgain_list(struct comedi_device
*dev
)
1882 ni_stc_writew(dev
, AI_CONVERT_Pulse
, AI_Command_1_Register
);
1883 for (i
= 0; i
< NI_TIMEOUT
; ++i
) {
1884 if (!(ni_stc_readw(dev
, AI_Status_1_Register
) &
1885 AI_FIFO_Empty_St
)) {
1886 ni_stc_writew(dev
, 1, ADC_FIFO_Clear
);
1891 printk("ni_mio_common: timeout loading channel/gain list\n");
1894 static void ni_m_series_load_channelgain_list(struct comedi_device
*dev
,
1895 unsigned int n_chan
,
1898 const struct ni_board_struct
*board
= comedi_board(dev
);
1899 struct ni_private
*devpriv
= dev
->private;
1900 unsigned int chan
, range
, aref
;
1902 unsigned int dither
;
1903 unsigned range_code
;
1905 ni_stc_writew(dev
, 1, Configuration_Memory_Clear
);
1907 if ((list
[0] & CR_ALT_SOURCE
)) {
1908 unsigned bypass_bits
;
1909 chan
= CR_CHAN(list
[0]);
1910 range
= CR_RANGE(list
[0]);
1911 range_code
= ni_gainlkup
[board
->gainlkup
][range
];
1912 dither
= ((list
[0] & CR_ALT_FILTER
) != 0);
1913 bypass_bits
= MSeries_AI_Bypass_Config_FIFO_Bit
;
1914 bypass_bits
|= chan
;
1916 (devpriv
->ai_calib_source
) &
1917 (MSeries_AI_Bypass_Cal_Sel_Pos_Mask
|
1918 MSeries_AI_Bypass_Cal_Sel_Neg_Mask
|
1919 MSeries_AI_Bypass_Mode_Mux_Mask
|
1920 MSeries_AO_Bypass_AO_Cal_Sel_Mask
);
1921 bypass_bits
|= MSeries_AI_Bypass_Gain_Bits(range_code
);
1923 bypass_bits
|= MSeries_AI_Bypass_Dither_Bit
;
1924 /* don't use 2's complement encoding */
1925 bypass_bits
|= MSeries_AI_Bypass_Polarity_Bit
;
1926 ni_writel(dev
, bypass_bits
, M_Offset_AI_Config_FIFO_Bypass
);
1928 ni_writel(dev
, 0, M_Offset_AI_Config_FIFO_Bypass
);
1930 for (i
= 0; i
< n_chan
; i
++) {
1931 unsigned config_bits
= 0;
1932 chan
= CR_CHAN(list
[i
]);
1933 aref
= CR_AREF(list
[i
]);
1934 range
= CR_RANGE(list
[i
]);
1935 dither
= ((list
[i
] & CR_ALT_FILTER
) != 0);
1937 range_code
= ni_gainlkup
[board
->gainlkup
][range
];
1938 devpriv
->ai_offset
[i
] = 0;
1942 MSeries_AI_Config_Channel_Type_Differential_Bits
;
1946 MSeries_AI_Config_Channel_Type_Common_Ref_Bits
;
1950 MSeries_AI_Config_Channel_Type_Ground_Ref_Bits
;
1955 config_bits
|= MSeries_AI_Config_Channel_Bits(chan
);
1957 MSeries_AI_Config_Bank_Bits(board
->reg_type
, chan
);
1958 config_bits
|= MSeries_AI_Config_Gain_Bits(range_code
);
1959 if (i
== n_chan
- 1)
1960 config_bits
|= MSeries_AI_Config_Last_Channel_Bit
;
1962 config_bits
|= MSeries_AI_Config_Dither_Bit
;
1963 /* don't use 2's complement encoding */
1964 config_bits
|= MSeries_AI_Config_Polarity_Bit
;
1965 ni_writew(dev
, config_bits
, M_Offset_AI_Config_FIFO_Data
);
1967 ni_prime_channelgain_list(dev
);
1971 * Notes on the 6110 and 6111:
1972 * These boards a slightly different than the rest of the series, since
1973 * they have multiple A/D converters.
1974 * From the driver side, the configuration memory is a
1976 * Configuration Memory Low:
1978 * bit 8: unipolar/bipolar (should be 0 for bipolar)
1979 * bits 0-3: gain. This is 4 bits instead of 3 for the other boards
1980 * 1001 gain=0.1 (+/- 50)
1989 * Configuration Memory High:
1990 * bits 12-14: Channel Type
1991 * 001 for differential
1992 * 000 for calibration
1993 * bit 11: coupling (this is not currently handled)
1997 * valid channels are 0-3
1999 static void ni_load_channelgain_list(struct comedi_device
*dev
,
2000 struct comedi_subdevice
*s
,
2001 unsigned int n_chan
, unsigned int *list
)
2003 const struct ni_board_struct
*board
= comedi_board(dev
);
2004 struct ni_private
*devpriv
= dev
->private;
2005 unsigned int offset
= (s
->maxdata
+ 1) >> 1;
2006 unsigned int chan
, range
, aref
;
2008 unsigned int hi
, lo
;
2009 unsigned int dither
;
2011 if (devpriv
->is_m_series
) {
2012 ni_m_series_load_channelgain_list(dev
, n_chan
, list
);
2015 if (n_chan
== 1 && !devpriv
->is_611x
&& !devpriv
->is_6143
) {
2016 if (devpriv
->changain_state
2017 && devpriv
->changain_spec
== list
[0]) {
2021 devpriv
->changain_state
= 1;
2022 devpriv
->changain_spec
= list
[0];
2024 devpriv
->changain_state
= 0;
2027 ni_stc_writew(dev
, 1, Configuration_Memory_Clear
);
2029 /* Set up Calibration mode if required */
2030 if (devpriv
->is_6143
) {
2031 if ((list
[0] & CR_ALT_SOURCE
)
2032 && !devpriv
->ai_calib_source_enabled
) {
2033 /* Strobe Relay enable bit */
2034 ni_writew(dev
, devpriv
->ai_calib_source
|
2035 Calibration_Channel_6143_RelayOn
,
2036 Calibration_Channel_6143
);
2037 ni_writew(dev
, devpriv
->ai_calib_source
,
2038 Calibration_Channel_6143
);
2039 devpriv
->ai_calib_source_enabled
= 1;
2040 msleep_interruptible(100); /* Allow relays to change */
2041 } else if (!(list
[0] & CR_ALT_SOURCE
)
2042 && devpriv
->ai_calib_source_enabled
) {
2043 /* Strobe Relay disable bit */
2044 ni_writew(dev
, devpriv
->ai_calib_source
|
2045 Calibration_Channel_6143_RelayOff
,
2046 Calibration_Channel_6143
);
2047 ni_writew(dev
, devpriv
->ai_calib_source
,
2048 Calibration_Channel_6143
);
2049 devpriv
->ai_calib_source_enabled
= 0;
2050 msleep_interruptible(100); /* Allow relays to change */
2054 for (i
= 0; i
< n_chan
; i
++) {
2055 if (!devpriv
->is_6143
&& (list
[i
] & CR_ALT_SOURCE
)) {
2056 chan
= devpriv
->ai_calib_source
;
2058 chan
= CR_CHAN(list
[i
]);
2060 aref
= CR_AREF(list
[i
]);
2061 range
= CR_RANGE(list
[i
]);
2062 dither
= ((list
[i
] & CR_ALT_FILTER
) != 0);
2064 /* fix the external/internal range differences */
2065 range
= ni_gainlkup
[board
->gainlkup
][range
];
2066 if (devpriv
->is_611x
)
2067 devpriv
->ai_offset
[i
] = offset
;
2069 devpriv
->ai_offset
[i
] = (range
& 0x100) ? 0 : offset
;
2072 if ((list
[i
] & CR_ALT_SOURCE
)) {
2073 if (devpriv
->is_611x
)
2074 ni_writew(dev
, CR_CHAN(list
[i
]) & 0x0003,
2075 Calibration_Channel_Select_611x
);
2077 if (devpriv
->is_611x
)
2079 else if (devpriv
->is_6143
)
2083 hi
|= AI_DIFFERENTIAL
;
2095 hi
|= AI_CONFIG_CHANNEL(chan
);
2097 ni_writew(dev
, hi
, Configuration_Memory_High
);
2099 if (!devpriv
->is_6143
) {
2101 if (i
== n_chan
- 1)
2102 lo
|= AI_LAST_CHANNEL
;
2106 ni_writew(dev
, lo
, Configuration_Memory_Low
);
2110 /* prime the channel/gain list */
2111 if (!devpriv
->is_611x
&& !devpriv
->is_6143
)
2112 ni_prime_channelgain_list(dev
);
2115 static int ni_ai_insn_read(struct comedi_device
*dev
,
2116 struct comedi_subdevice
*s
,
2117 struct comedi_insn
*insn
,
2120 struct ni_private
*devpriv
= dev
->private;
2121 unsigned int mask
= (s
->maxdata
+ 1) >> 1;
2127 ni_load_channelgain_list(dev
, s
, 1, &insn
->chanspec
);
2129 ni_clear_ai_fifo(dev
);
2131 signbits
= devpriv
->ai_offset
[0];
2132 if (devpriv
->is_611x
) {
2133 for (n
= 0; n
< num_adc_stages_611x
; n
++) {
2134 ni_stc_writew(dev
, AI_CONVERT_Pulse
,
2135 AI_Command_1_Register
);
2138 for (n
= 0; n
< insn
->n
; n
++) {
2139 ni_stc_writew(dev
, AI_CONVERT_Pulse
,
2140 AI_Command_1_Register
);
2141 /* The 611x has screwy 32-bit FIFOs. */
2143 for (i
= 0; i
< NI_TIMEOUT
; i
++) {
2144 if (ni_readb(dev
, XXX_Status
) & 0x80) {
2145 d
= ni_readl(dev
, ADC_FIFO_Data_611x
);
2150 if (!(ni_stc_readw(dev
, AI_Status_1_Register
) &
2151 AI_FIFO_Empty_St
)) {
2152 d
= ni_readl(dev
, ADC_FIFO_Data_611x
);
2157 if (i
== NI_TIMEOUT
) {
2159 ("ni_mio_common: timeout in 611x ni_ai_insn_read\n");
2165 } else if (devpriv
->is_6143
) {
2166 for (n
= 0; n
< insn
->n
; n
++) {
2167 ni_stc_writew(dev
, AI_CONVERT_Pulse
,
2168 AI_Command_1_Register
);
2170 /* The 6143 has 32-bit FIFOs. You need to strobe a bit to move a single 16bit stranded sample into the FIFO */
2172 for (i
= 0; i
< NI_TIMEOUT
; i
++) {
2173 if (ni_readl(dev
, AIFIFO_Status_6143
) & 0x01) {
2174 /* Get stranded sample into FIFO */
2175 ni_writel(dev
, 0x01,
2176 AIFIFO_Control_6143
);
2177 dl
= ni_readl(dev
, AIFIFO_Data_6143
);
2181 if (i
== NI_TIMEOUT
) {
2183 ("ni_mio_common: timeout in 6143 ni_ai_insn_read\n");
2186 data
[n
] = (((dl
>> 16) & 0xFFFF) + signbits
) & 0xFFFF;
2189 for (n
= 0; n
< insn
->n
; n
++) {
2190 ni_stc_writew(dev
, AI_CONVERT_Pulse
,
2191 AI_Command_1_Register
);
2192 for (i
= 0; i
< NI_TIMEOUT
; i
++) {
2193 if (!(ni_stc_readw(dev
, AI_Status_1_Register
) &
2197 if (i
== NI_TIMEOUT
) {
2199 ("ni_mio_common: timeout in ni_ai_insn_read\n");
2202 if (devpriv
->is_m_series
) {
2203 dl
= ni_readl(dev
, M_Offset_AI_FIFO_Data
);
2207 d
= ni_readw(dev
, ADC_FIFO_Data_Register
);
2208 d
+= signbits
; /* subtle: needs to be short addition */
2216 static int ni_ns_to_timer(const struct comedi_device
*dev
, unsigned nanosec
,
2219 struct ni_private
*devpriv
= dev
->private;
2222 switch (round_mode
) {
2223 case TRIG_ROUND_NEAREST
:
2225 divider
= (nanosec
+ devpriv
->clock_ns
/ 2) / devpriv
->clock_ns
;
2227 case TRIG_ROUND_DOWN
:
2228 divider
= (nanosec
) / devpriv
->clock_ns
;
2231 divider
= (nanosec
+ devpriv
->clock_ns
- 1) / devpriv
->clock_ns
;
2237 static unsigned ni_timer_to_ns(const struct comedi_device
*dev
, int timer
)
2239 struct ni_private
*devpriv
= dev
->private;
2241 return devpriv
->clock_ns
* (timer
+ 1);
2244 static unsigned ni_min_ai_scan_period_ns(struct comedi_device
*dev
,
2245 unsigned num_channels
)
2247 const struct ni_board_struct
*board
= comedi_board(dev
);
2248 struct ni_private
*devpriv
= dev
->private;
2250 /* simultaneously-sampled inputs */
2251 if (devpriv
->is_611x
|| devpriv
->is_6143
)
2252 return board
->ai_speed
;
2254 /* multiplexed inputs */
2255 return board
->ai_speed
* num_channels
;
2258 static int ni_ai_cmdtest(struct comedi_device
*dev
, struct comedi_subdevice
*s
,
2259 struct comedi_cmd
*cmd
)
2261 const struct ni_board_struct
*board
= comedi_board(dev
);
2262 struct ni_private
*devpriv
= dev
->private;
2265 unsigned int sources
;
2267 /* Step 1 : check if triggers are trivially valid */
2269 if ((cmd
->flags
& CMDF_WRITE
))
2270 cmd
->flags
&= ~CMDF_WRITE
;
2272 err
|= cfc_check_trigger_src(&cmd
->start_src
,
2273 TRIG_NOW
| TRIG_INT
| TRIG_EXT
);
2274 err
|= cfc_check_trigger_src(&cmd
->scan_begin_src
,
2275 TRIG_TIMER
| TRIG_EXT
);
2277 sources
= TRIG_TIMER
| TRIG_EXT
;
2278 if (devpriv
->is_611x
|| devpriv
->is_6143
)
2279 sources
|= TRIG_NOW
;
2280 err
|= cfc_check_trigger_src(&cmd
->convert_src
, sources
);
2282 err
|= cfc_check_trigger_src(&cmd
->scan_end_src
, TRIG_COUNT
);
2283 err
|= cfc_check_trigger_src(&cmd
->stop_src
, TRIG_COUNT
| TRIG_NONE
);
2288 /* Step 2a : make sure trigger sources are unique */
2290 err
|= cfc_check_trigger_is_unique(cmd
->start_src
);
2291 err
|= cfc_check_trigger_is_unique(cmd
->scan_begin_src
);
2292 err
|= cfc_check_trigger_is_unique(cmd
->convert_src
);
2293 err
|= cfc_check_trigger_is_unique(cmd
->stop_src
);
2295 /* Step 2b : and mutually compatible */
2300 /* Step 3: check if arguments are trivially valid */
2302 switch (cmd
->start_src
) {
2305 err
|= cfc_check_trigger_arg_is(&cmd
->start_arg
, 0);
2308 tmp
= CR_CHAN(cmd
->start_arg
);
2312 tmp
|= (cmd
->start_arg
& (CR_INVERT
| CR_EDGE
));
2313 err
|= cfc_check_trigger_arg_is(&cmd
->start_arg
, tmp
);
2317 if (cmd
->scan_begin_src
== TRIG_TIMER
) {
2318 err
|= cfc_check_trigger_arg_min(&cmd
->scan_begin_arg
,
2319 ni_min_ai_scan_period_ns(dev
, cmd
->chanlist_len
));
2320 err
|= cfc_check_trigger_arg_max(&cmd
->scan_begin_arg
,
2321 devpriv
->clock_ns
* 0xffffff);
2322 } else if (cmd
->scan_begin_src
== TRIG_EXT
) {
2323 /* external trigger */
2324 unsigned int tmp
= CR_CHAN(cmd
->scan_begin_arg
);
2328 tmp
|= (cmd
->scan_begin_arg
& (CR_INVERT
| CR_EDGE
));
2329 err
|= cfc_check_trigger_arg_is(&cmd
->scan_begin_arg
, tmp
);
2330 } else { /* TRIG_OTHER */
2331 err
|= cfc_check_trigger_arg_is(&cmd
->scan_begin_arg
, 0);
2334 if (cmd
->convert_src
== TRIG_TIMER
) {
2335 if (devpriv
->is_611x
|| devpriv
->is_6143
) {
2336 err
|= cfc_check_trigger_arg_is(&cmd
->convert_arg
, 0);
2338 err
|= cfc_check_trigger_arg_min(&cmd
->convert_arg
,
2340 err
|= cfc_check_trigger_arg_max(&cmd
->convert_arg
,
2341 devpriv
->clock_ns
* 0xffff);
2343 } else if (cmd
->convert_src
== TRIG_EXT
) {
2344 /* external trigger */
2345 unsigned int tmp
= CR_CHAN(cmd
->convert_arg
);
2349 tmp
|= (cmd
->convert_arg
& (CR_ALT_FILTER
| CR_INVERT
));
2350 err
|= cfc_check_trigger_arg_is(&cmd
->convert_arg
, tmp
);
2351 } else if (cmd
->convert_src
== TRIG_NOW
) {
2352 err
|= cfc_check_trigger_arg_is(&cmd
->convert_arg
, 0);
2355 err
|= cfc_check_trigger_arg_is(&cmd
->scan_end_arg
, cmd
->chanlist_len
);
2357 if (cmd
->stop_src
== TRIG_COUNT
) {
2358 unsigned int max_count
= 0x01000000;
2360 if (devpriv
->is_611x
)
2361 max_count
-= num_adc_stages_611x
;
2362 err
|= cfc_check_trigger_arg_max(&cmd
->stop_arg
, max_count
);
2363 err
|= cfc_check_trigger_arg_min(&cmd
->stop_arg
, 1);
2366 err
|= cfc_check_trigger_arg_is(&cmd
->stop_arg
, 0);
2372 /* step 4: fix up any arguments */
2374 if (cmd
->scan_begin_src
== TRIG_TIMER
) {
2375 tmp
= cmd
->scan_begin_arg
;
2376 cmd
->scan_begin_arg
=
2377 ni_timer_to_ns(dev
, ni_ns_to_timer(dev
,
2378 cmd
->scan_begin_arg
,
2382 if (tmp
!= cmd
->scan_begin_arg
)
2385 if (cmd
->convert_src
== TRIG_TIMER
) {
2386 if (!devpriv
->is_611x
&& !devpriv
->is_6143
) {
2387 tmp
= cmd
->convert_arg
;
2389 ni_timer_to_ns(dev
, ni_ns_to_timer(dev
,
2394 if (tmp
!= cmd
->convert_arg
)
2396 if (cmd
->scan_begin_src
== TRIG_TIMER
&&
2397 cmd
->scan_begin_arg
<
2398 cmd
->convert_arg
* cmd
->scan_end_arg
) {
2399 cmd
->scan_begin_arg
=
2400 cmd
->convert_arg
* cmd
->scan_end_arg
;
2412 static int ni_ai_inttrig(struct comedi_device
*dev
,
2413 struct comedi_subdevice
*s
,
2414 unsigned int trig_num
)
2416 struct ni_private
*devpriv
= dev
->private;
2417 struct comedi_cmd
*cmd
= &s
->async
->cmd
;
2419 if (trig_num
!= cmd
->start_arg
)
2422 ni_stc_writew(dev
, AI_START1_Pulse
| devpriv
->ai_cmd2
,
2423 AI_Command_2_Register
);
2424 s
->async
->inttrig
= NULL
;
2429 static int ni_ai_cmd(struct comedi_device
*dev
, struct comedi_subdevice
*s
)
2431 struct ni_private
*devpriv
= dev
->private;
2432 const struct comedi_cmd
*cmd
= &s
->async
->cmd
;
2434 int mode1
= 0; /* mode1 is needed for both stop and convert */
2436 int start_stop_select
= 0;
2437 unsigned int stop_count
;
2438 int interrupt_a_enable
= 0;
2440 if (dev
->irq
== 0) {
2441 comedi_error(dev
, "cannot run command without an irq");
2444 ni_clear_ai_fifo(dev
);
2446 ni_load_channelgain_list(dev
, s
, cmd
->chanlist_len
, cmd
->chanlist
);
2448 /* start configuration */
2449 ni_stc_writew(dev
, AI_Configuration_Start
, Joint_Reset_Register
);
2451 /* disable analog triggering for now, since it
2452 * interferes with the use of pfi0 */
2453 devpriv
->an_trig_etc_reg
&= ~Analog_Trigger_Enable
;
2454 ni_stc_writew(dev
, devpriv
->an_trig_etc_reg
,
2455 Analog_Trigger_Etc_Register
);
2457 switch (cmd
->start_src
) {
2461 AI_START2_Select(0) |
2462 AI_START1_Sync
| AI_START1_Edge
|
2463 AI_START1_Select(0),
2464 AI_Trigger_Select_Register
);
2468 int chan
= CR_CHAN(cmd
->start_arg
);
2469 unsigned int bits
= AI_START2_Select(0) |
2470 AI_START1_Sync
| AI_START1_Select(chan
+ 1);
2472 if (cmd
->start_arg
& CR_INVERT
)
2473 bits
|= AI_START1_Polarity
;
2474 if (cmd
->start_arg
& CR_EDGE
)
2475 bits
|= AI_START1_Edge
;
2476 ni_stc_writew(dev
, bits
, AI_Trigger_Select_Register
);
2481 mode2
&= ~AI_Pre_Trigger
;
2482 mode2
&= ~AI_SC_Initial_Load_Source
;
2483 mode2
&= ~AI_SC_Reload_Mode
;
2484 ni_stc_writew(dev
, mode2
, AI_Mode_2_Register
);
2486 if (cmd
->chanlist_len
== 1 || devpriv
->is_611x
|| devpriv
->is_6143
) {
2487 start_stop_select
|= AI_STOP_Polarity
;
2488 start_stop_select
|= AI_STOP_Select(31); /* logic low */
2489 start_stop_select
|= AI_STOP_Sync
;
2491 start_stop_select
|= AI_STOP_Select(19); /* ai configuration memory */
2493 ni_stc_writew(dev
, start_stop_select
, AI_START_STOP_Select_Register
);
2495 devpriv
->ai_cmd2
= 0;
2496 switch (cmd
->stop_src
) {
2498 stop_count
= cmd
->stop_arg
- 1;
2500 if (devpriv
->is_611x
) {
2501 /* have to take 3 stage adc pipeline into account */
2502 stop_count
+= num_adc_stages_611x
;
2504 /* stage number of scans */
2505 ni_stc_writel(dev
, stop_count
, AI_SC_Load_A_Registers
);
2507 mode1
|= AI_Start_Stop
| AI_Mode_1_Reserved
| AI_Trigger_Once
;
2508 ni_stc_writew(dev
, mode1
, AI_Mode_1_Register
);
2509 /* load SC (Scan Count) */
2510 ni_stc_writew(dev
, AI_SC_Load
, AI_Command_1_Register
);
2512 if (stop_count
== 0) {
2513 devpriv
->ai_cmd2
|= AI_End_On_End_Of_Scan
;
2514 interrupt_a_enable
|= AI_STOP_Interrupt_Enable
;
2515 /* this is required to get the last sample for chanlist_len > 1, not sure why */
2516 if (cmd
->chanlist_len
> 1)
2517 start_stop_select
|=
2518 AI_STOP_Polarity
| AI_STOP_Edge
;
2522 /* stage number of scans */
2523 ni_stc_writel(dev
, 0, AI_SC_Load_A_Registers
);
2525 mode1
|= AI_Start_Stop
| AI_Mode_1_Reserved
| AI_Continuous
;
2526 ni_stc_writew(dev
, mode1
, AI_Mode_1_Register
);
2528 /* load SC (Scan Count) */
2529 ni_stc_writew(dev
, AI_SC_Load
, AI_Command_1_Register
);
2533 switch (cmd
->scan_begin_src
) {
2536 stop bits for non 611x boards
2537 AI_SI_Special_Trigger_Delay=0
2539 AI_START_STOP_Select_Register:
2540 AI_START_Polarity=0 (?) rising edge
2541 AI_START_Edge=1 edge triggered
2543 AI_START_Select=0 SI_TC
2544 AI_STOP_Polarity=0 rising edge
2545 AI_STOP_Edge=0 level
2547 AI_STOP_Select=19 external pin (configuration mem)
2549 start_stop_select
|= AI_START_Edge
| AI_START_Sync
;
2550 ni_stc_writew(dev
, start_stop_select
,
2551 AI_START_STOP_Select_Register
);
2553 mode2
|= AI_SI_Reload_Mode(0);
2554 /* AI_SI_Initial_Load_Source=A */
2555 mode2
&= ~AI_SI_Initial_Load_Source
;
2556 /* mode2 |= AI_SC_Reload_Mode; */
2557 ni_stc_writew(dev
, mode2
, AI_Mode_2_Register
);
2560 timer
= ni_ns_to_timer(dev
, cmd
->scan_begin_arg
,
2561 TRIG_ROUND_NEAREST
);
2562 ni_stc_writel(dev
, timer
, AI_SI_Load_A_Registers
);
2563 ni_stc_writew(dev
, AI_SI_Load
, AI_Command_1_Register
);
2566 if (cmd
->scan_begin_arg
& CR_EDGE
)
2567 start_stop_select
|= AI_START_Edge
;
2568 /* AI_START_Polarity==1 is falling edge */
2569 if (cmd
->scan_begin_arg
& CR_INVERT
)
2570 start_stop_select
|= AI_START_Polarity
;
2571 if (cmd
->scan_begin_src
!= cmd
->convert_src
||
2572 (cmd
->scan_begin_arg
& ~CR_EDGE
) !=
2573 (cmd
->convert_arg
& ~CR_EDGE
))
2574 start_stop_select
|= AI_START_Sync
;
2575 start_stop_select
|=
2576 AI_START_Select(1 + CR_CHAN(cmd
->scan_begin_arg
));
2577 ni_stc_writew(dev
, start_stop_select
,
2578 AI_START_STOP_Select_Register
);
2582 switch (cmd
->convert_src
) {
2585 if (cmd
->convert_arg
== 0 || cmd
->convert_src
== TRIG_NOW
)
2588 timer
= ni_ns_to_timer(dev
, cmd
->convert_arg
,
2589 TRIG_ROUND_NEAREST
);
2590 /* 0,0 does not work */
2591 ni_stc_writew(dev
, 1, AI_SI2_Load_A_Register
);
2592 ni_stc_writew(dev
, timer
, AI_SI2_Load_B_Register
);
2594 /* AI_SI2_Reload_Mode = alternate */
2595 /* AI_SI2_Initial_Load_Source = A */
2596 mode2
&= ~AI_SI2_Initial_Load_Source
;
2597 mode2
|= AI_SI2_Reload_Mode
;
2598 ni_stc_writew(dev
, mode2
, AI_Mode_2_Register
);
2601 ni_stc_writew(dev
, AI_SI2_Load
, AI_Command_1_Register
);
2603 mode2
|= AI_SI2_Reload_Mode
; /* alternate */
2604 mode2
|= AI_SI2_Initial_Load_Source
; /* B */
2606 ni_stc_writew(dev
, mode2
, AI_Mode_2_Register
);
2609 mode1
|= AI_CONVERT_Source_Select(1 + cmd
->convert_arg
);
2610 if ((cmd
->convert_arg
& CR_INVERT
) == 0)
2611 mode1
|= AI_CONVERT_Source_Polarity
;
2612 ni_stc_writew(dev
, mode1
, AI_Mode_1_Register
);
2614 mode2
|= AI_Start_Stop_Gate_Enable
| AI_SC_Gate_Enable
;
2615 ni_stc_writew(dev
, mode2
, AI_Mode_2_Register
);
2622 /* interrupt on FIFO, errors, SC_TC */
2623 interrupt_a_enable
|= AI_Error_Interrupt_Enable
|
2624 AI_SC_TC_Interrupt_Enable
;
2627 interrupt_a_enable
|= AI_FIFO_Interrupt_Enable
;
2630 if (cmd
->flags
& TRIG_WAKE_EOS
2631 || (devpriv
->ai_cmd2
& AI_End_On_End_Of_Scan
)) {
2632 /* wake on end-of-scan */
2633 devpriv
->aimode
= AIMODE_SCAN
;
2635 devpriv
->aimode
= AIMODE_HALF_FULL
;
2638 switch (devpriv
->aimode
) {
2639 case AIMODE_HALF_FULL
:
2640 /*generate FIFO interrupts and DMA requests on half-full */
2642 ni_stc_writew(dev
, AI_FIFO_Mode_HF_to_E
,
2643 AI_Mode_3_Register
);
2645 ni_stc_writew(dev
, AI_FIFO_Mode_HF
,
2646 AI_Mode_3_Register
);
2650 /*generate FIFO interrupts on non-empty */
2651 ni_stc_writew(dev
, AI_FIFO_Mode_NE
,
2652 AI_Mode_3_Register
);
2656 ni_stc_writew(dev
, AI_FIFO_Mode_NE
,
2657 AI_Mode_3_Register
);
2659 ni_stc_writew(dev
, AI_FIFO_Mode_HF
,
2660 AI_Mode_3_Register
);
2662 interrupt_a_enable
|= AI_STOP_Interrupt_Enable
;
2668 /* clear interrupts */
2670 AI_Error_Interrupt_Ack
|
2671 AI_STOP_Interrupt_Ack
|
2672 AI_START_Interrupt_Ack
|
2673 AI_START2_Interrupt_Ack
|
2674 AI_START1_Interrupt_Ack
|
2675 AI_SC_TC_Interrupt_Ack
|
2676 AI_SC_TC_Error_Confirm
,
2677 Interrupt_A_Ack_Register
);
2679 ni_set_bits(dev
, Interrupt_A_Enable_Register
,
2680 interrupt_a_enable
, 1);
2682 /* interrupt on nothing */
2683 ni_set_bits(dev
, Interrupt_A_Enable_Register
, ~0, 0);
2685 /* XXX start polling if necessary */
2688 /* end configuration */
2689 ni_stc_writew(dev
, AI_Configuration_End
, Joint_Reset_Register
);
2691 switch (cmd
->scan_begin_src
) {
2694 AI_SI2_Arm
| AI_SI_Arm
| AI_DIV_Arm
| AI_SC_Arm
,
2695 AI_Command_1_Register
);
2698 /* XXX AI_SI_Arm? */
2700 AI_SI2_Arm
| AI_SI_Arm
| AI_DIV_Arm
| AI_SC_Arm
,
2701 AI_Command_1_Register
);
2707 int retval
= ni_ai_setup_MITE_dma(dev
);
2713 if (cmd
->start_src
== TRIG_NOW
) {
2714 /* AI_START1_Pulse */
2715 ni_stc_writew(dev
, AI_START1_Pulse
| devpriv
->ai_cmd2
,
2716 AI_Command_2_Register
);
2717 s
->async
->inttrig
= NULL
;
2718 } else if (cmd
->start_src
== TRIG_EXT
) {
2719 s
->async
->inttrig
= NULL
;
2720 } else { /* TRIG_INT */
2721 s
->async
->inttrig
= ni_ai_inttrig
;
2727 static int ni_ai_insn_config(struct comedi_device
*dev
,
2728 struct comedi_subdevice
*s
,
2729 struct comedi_insn
*insn
, unsigned int *data
)
2731 struct ni_private
*devpriv
= dev
->private;
2737 case INSN_CONFIG_ALT_SOURCE
:
2738 if (devpriv
->is_m_series
) {
2739 if (data
[1] & ~(MSeries_AI_Bypass_Cal_Sel_Pos_Mask
|
2740 MSeries_AI_Bypass_Cal_Sel_Neg_Mask
|
2741 MSeries_AI_Bypass_Mode_Mux_Mask
|
2742 MSeries_AO_Bypass_AO_Cal_Sel_Mask
)) {
2745 devpriv
->ai_calib_source
= data
[1];
2746 } else if (devpriv
->is_6143
) {
2747 unsigned int calib_source
;
2749 calib_source
= data
[1] & 0xf;
2751 if (calib_source
> 0xF)
2754 devpriv
->ai_calib_source
= calib_source
;
2755 ni_writew(dev
, calib_source
, Calibration_Channel_6143
);
2757 unsigned int calib_source
;
2758 unsigned int calib_source_adjust
;
2760 calib_source
= data
[1] & 0xf;
2761 calib_source_adjust
= (data
[1] >> 4) & 0xff;
2763 if (calib_source
>= 8)
2765 devpriv
->ai_calib_source
= calib_source
;
2766 if (devpriv
->is_611x
) {
2767 ni_writeb(dev
, calib_source_adjust
,
2768 Cal_Gain_Select_611x
);
2779 static void ni_ao_munge(struct comedi_device
*dev
, struct comedi_subdevice
*s
,
2780 void *data
, unsigned int num_bytes
,
2781 unsigned int chan_index
)
2783 struct comedi_cmd
*cmd
= &s
->async
->cmd
;
2784 unsigned int length
= num_bytes
/ bytes_per_sample(s
);
2785 unsigned short *array
= data
;
2788 for (i
= 0; i
< length
; i
++) {
2789 unsigned int range
= CR_RANGE(cmd
->chanlist
[chan_index
]);
2790 unsigned short val
= array
[i
];
2793 * Munge data from unsigned to two's complement for
2796 if (comedi_range_is_bipolar(s
, range
))
2797 val
= comedi_offset_munge(s
, val
);
2799 val
= cpu_to_le16(val
);
2804 chan_index
%= cmd
->chanlist_len
;
2808 static int ni_m_series_ao_config_chanlist(struct comedi_device
*dev
,
2809 struct comedi_subdevice
*s
,
2810 unsigned int chanspec
[],
2811 unsigned int n_chans
, int timed
)
2813 struct ni_private
*devpriv
= dev
->private;
2821 for (i
= 0; i
< s
->n_chan
; ++i
) {
2822 devpriv
->ao_conf
[i
] &= ~MSeries_AO_Update_Timed_Bit
;
2823 ni_writeb(dev
, devpriv
->ao_conf
[i
],
2824 M_Offset_AO_Config_Bank(i
));
2825 ni_writeb(dev
, 0xf, M_Offset_AO_Waveform_Order(i
));
2828 for (i
= 0; i
< n_chans
; i
++) {
2829 const struct comedi_krange
*krange
;
2830 chan
= CR_CHAN(chanspec
[i
]);
2831 range
= CR_RANGE(chanspec
[i
]);
2832 krange
= s
->range_table
->range
+ range
;
2835 switch (krange
->max
- krange
->min
) {
2837 conf
|= MSeries_AO_DAC_Reference_10V_Internal_Bits
;
2839 M_Offset_AO_Reference_Attenuation(chan
));
2842 conf
|= MSeries_AO_DAC_Reference_5V_Internal_Bits
;
2844 M_Offset_AO_Reference_Attenuation(chan
));
2847 conf
|= MSeries_AO_DAC_Reference_10V_Internal_Bits
;
2848 ni_writeb(dev
, MSeries_Attenuate_x5_Bit
,
2849 M_Offset_AO_Reference_Attenuation(chan
));
2852 conf
|= MSeries_AO_DAC_Reference_5V_Internal_Bits
;
2853 ni_writeb(dev
, MSeries_Attenuate_x5_Bit
,
2854 M_Offset_AO_Reference_Attenuation(chan
));
2857 printk("%s: bug! unhandled ao reference voltage\n",
2861 switch (krange
->max
+ krange
->min
) {
2863 conf
|= MSeries_AO_DAC_Offset_0V_Bits
;
2866 conf
|= MSeries_AO_DAC_Offset_5V_Bits
;
2869 printk("%s: bug! unhandled ao offset voltage\n",
2874 conf
|= MSeries_AO_Update_Timed_Bit
;
2875 ni_writeb(dev
, conf
, M_Offset_AO_Config_Bank(chan
));
2876 devpriv
->ao_conf
[chan
] = conf
;
2877 ni_writeb(dev
, i
, M_Offset_AO_Waveform_Order(chan
));
2882 static int ni_old_ao_config_chanlist(struct comedi_device
*dev
,
2883 struct comedi_subdevice
*s
,
2884 unsigned int chanspec
[],
2885 unsigned int n_chans
)
2887 struct ni_private
*devpriv
= dev
->private;
2894 for (i
= 0; i
< n_chans
; i
++) {
2895 chan
= CR_CHAN(chanspec
[i
]);
2896 range
= CR_RANGE(chanspec
[i
]);
2897 conf
= AO_Channel(chan
);
2899 if (comedi_range_is_bipolar(s
, range
)) {
2901 invert
= (s
->maxdata
+ 1) >> 1;
2905 if (comedi_range_is_external(s
, range
))
2908 /* not all boards can deglitch, but this shouldn't hurt */
2909 if (chanspec
[i
] & CR_DEGLITCH
)
2910 conf
|= AO_Deglitch
;
2912 /* analog reference */
2913 /* AREF_OTHER connects AO ground to AI ground, i think */
2914 conf
|= (CR_AREF(chanspec
[i
]) ==
2915 AREF_OTHER
) ? AO_Ground_Ref
: 0;
2917 ni_writew(dev
, conf
, AO_Configuration
);
2918 devpriv
->ao_conf
[chan
] = conf
;
2923 static int ni_ao_config_chanlist(struct comedi_device
*dev
,
2924 struct comedi_subdevice
*s
,
2925 unsigned int chanspec
[], unsigned int n_chans
,
2928 struct ni_private
*devpriv
= dev
->private;
2930 if (devpriv
->is_m_series
)
2931 return ni_m_series_ao_config_chanlist(dev
, s
, chanspec
, n_chans
,
2934 return ni_old_ao_config_chanlist(dev
, s
, chanspec
, n_chans
);
2937 static int ni_ao_insn_read(struct comedi_device
*dev
,
2938 struct comedi_subdevice
*s
,
2939 struct comedi_insn
*insn
,
2942 struct ni_private
*devpriv
= dev
->private;
2943 unsigned int chan
= CR_CHAN(insn
->chanspec
);
2946 for (i
= 0; i
< insn
->n
; i
++)
2947 data
[i
] = devpriv
->ao
[chan
];
2952 static int ni_ao_insn_write(struct comedi_device
*dev
,
2953 struct comedi_subdevice
*s
,
2954 struct comedi_insn
*insn
,
2957 struct ni_private
*devpriv
= dev
->private;
2958 unsigned int chan
= CR_CHAN(insn
->chanspec
);
2959 unsigned int range
= CR_RANGE(insn
->chanspec
);
2963 if (devpriv
->is_6xxx
) {
2964 ni_ao_win_outw(dev
, 1 << chan
, AO_Immediate_671x
);
2966 reg
= DACx_Direct_Data_671x(chan
);
2967 } else if (devpriv
->is_m_series
) {
2968 reg
= M_Offset_DAC_Direct_Data(chan
);
2970 reg
= (chan
) ? DAC1_Direct_Data
: DAC0_Direct_Data
;
2973 ni_ao_config_chanlist(dev
, s
, &insn
->chanspec
, 1, 0);
2975 for (i
= 0; i
< insn
->n
; i
++) {
2976 unsigned int val
= data
[i
];
2978 devpriv
->ao
[chan
] = val
;
2980 if (devpriv
->is_6xxx
) {
2982 * 6xxx boards have bipolar outputs, munge the
2983 * unsigned comedi values to 2's complement
2985 val
= comedi_offset_munge(s
, val
);
2987 ni_ao_win_outw(dev
, val
, reg
);
2988 } else if (devpriv
->is_m_series
) {
2990 * M-series boards use offset binary values for
2991 * bipolar and uinpolar outputs
2993 ni_writew(dev
, val
, reg
);
2996 * Non-M series boards need two's complement values
2997 * for bipolar ranges.
2999 if (comedi_range_is_bipolar(s
, range
))
3000 val
= comedi_offset_munge(s
, val
);
3002 ni_writew(dev
, val
, reg
);
3009 static int ni_ao_insn_config(struct comedi_device
*dev
,
3010 struct comedi_subdevice
*s
,
3011 struct comedi_insn
*insn
, unsigned int *data
)
3013 const struct ni_board_struct
*board
= comedi_board(dev
);
3014 struct ni_private
*devpriv
= dev
->private;
3017 case INSN_CONFIG_GET_HARDWARE_BUFFER_SIZE
:
3020 data
[2] = 1 + board
->ao_fifo_depth
* sizeof(short);
3022 data
[2] += devpriv
->mite
->fifo_size
;
3039 static int ni_ao_inttrig(struct comedi_device
*dev
,
3040 struct comedi_subdevice
*s
,
3041 unsigned int trig_num
)
3043 struct ni_private
*devpriv
= dev
->private;
3044 struct comedi_cmd
*cmd
= &s
->async
->cmd
;
3046 int interrupt_b_bits
;
3048 static const int timeout
= 1000;
3050 if (trig_num
!= cmd
->start_arg
)
3053 /* Null trig at beginning prevent ao start trigger from executing more than
3054 once per command (and doing things like trying to allocate the ao dma channel
3056 s
->async
->inttrig
= NULL
;
3058 ni_set_bits(dev
, Interrupt_B_Enable_Register
,
3059 AO_FIFO_Interrupt_Enable
| AO_Error_Interrupt_Enable
, 0);
3060 interrupt_b_bits
= AO_Error_Interrupt_Enable
;
3062 ni_stc_writew(dev
, 1, DAC_FIFO_Clear
);
3063 if (devpriv
->is_6xxx
)
3064 ni_ao_win_outl(dev
, 0x6, AO_FIFO_Offset_Load_611x
);
3065 ret
= ni_ao_setup_MITE_dma(dev
);
3068 ret
= ni_ao_wait_for_dma_load(dev
);
3072 ret
= ni_ao_prep_fifo(dev
, s
);
3076 interrupt_b_bits
|= AO_FIFO_Interrupt_Enable
;
3079 ni_stc_writew(dev
, devpriv
->ao_mode3
| AO_Not_An_UPDATE
,
3080 AO_Mode_3_Register
);
3081 ni_stc_writew(dev
, devpriv
->ao_mode3
, AO_Mode_3_Register
);
3082 /* wait for DACs to be loaded */
3083 for (i
= 0; i
< timeout
; i
++) {
3085 if ((ni_stc_readw(dev
, Joint_Status_2_Register
) &
3086 AO_TMRDACWRs_In_Progress_St
) == 0)
3091 "timed out waiting for AO_TMRDACWRs_In_Progress_St to clear");
3095 * stc manual says we are need to clear error interrupt after
3096 * AO_TMRDACWRs_In_Progress_St clears
3098 ni_stc_writew(dev
, AO_Error_Interrupt_Ack
, Interrupt_B_Ack_Register
);
3100 ni_set_bits(dev
, Interrupt_B_Enable_Register
, interrupt_b_bits
, 1);
3102 ni_stc_writew(dev
, devpriv
->ao_cmd1
|
3103 AO_UI_Arm
| AO_UC_Arm
| AO_BC_Arm
|
3104 AO_DAC1_Update_Mode
| AO_DAC0_Update_Mode
,
3105 AO_Command_1_Register
);
3107 ni_stc_writew(dev
, devpriv
->ao_cmd2
| AO_START1_Pulse
,
3108 AO_Command_2_Register
);
3113 static int ni_ao_cmd(struct comedi_device
*dev
, struct comedi_subdevice
*s
)
3115 const struct ni_board_struct
*board
= comedi_board(dev
);
3116 struct ni_private
*devpriv
= dev
->private;
3117 const struct comedi_cmd
*cmd
= &s
->async
->cmd
;
3122 if (dev
->irq
== 0) {
3123 comedi_error(dev
, "cannot run command without an irq");
3127 ni_stc_writew(dev
, AO_Configuration_Start
, Joint_Reset_Register
);
3129 ni_stc_writew(dev
, AO_Disarm
, AO_Command_1_Register
);
3131 if (devpriv
->is_6xxx
) {
3132 ni_ao_win_outw(dev
, CLEAR_WG
, AO_Misc_611x
);
3135 for (i
= 0; i
< cmd
->chanlist_len
; i
++) {
3138 chan
= CR_CHAN(cmd
->chanlist
[i
]);
3140 ni_ao_win_outw(dev
, chan
, AO_Waveform_Generation_611x
);
3142 ni_ao_win_outw(dev
, bits
, AO_Timed_611x
);
3145 ni_ao_config_chanlist(dev
, s
, cmd
->chanlist
, cmd
->chanlist_len
, 1);
3147 if (cmd
->stop_src
== TRIG_NONE
) {
3148 devpriv
->ao_mode1
|= AO_Continuous
;
3149 devpriv
->ao_mode1
&= ~AO_Trigger_Once
;
3151 devpriv
->ao_mode1
&= ~AO_Continuous
;
3152 devpriv
->ao_mode1
|= AO_Trigger_Once
;
3154 ni_stc_writew(dev
, devpriv
->ao_mode1
, AO_Mode_1_Register
);
3155 switch (cmd
->start_src
) {
3158 devpriv
->ao_trigger_select
&=
3159 ~(AO_START1_Polarity
| AO_START1_Select(-1));
3160 devpriv
->ao_trigger_select
|= AO_START1_Edge
| AO_START1_Sync
;
3161 ni_stc_writew(dev
, devpriv
->ao_trigger_select
,
3162 AO_Trigger_Select_Register
);
3165 devpriv
->ao_trigger_select
=
3166 AO_START1_Select(CR_CHAN(cmd
->start_arg
) + 1);
3167 if (cmd
->start_arg
& CR_INVERT
)
3168 devpriv
->ao_trigger_select
|= AO_START1_Polarity
; /* 0=active high, 1=active low. see daq-stc 3-24 (p186) */
3169 if (cmd
->start_arg
& CR_EDGE
)
3170 devpriv
->ao_trigger_select
|= AO_START1_Edge
; /* 0=edge detection disabled, 1=enabled */
3171 ni_stc_writew(dev
, devpriv
->ao_trigger_select
,
3172 AO_Trigger_Select_Register
);
3178 devpriv
->ao_mode3
&= ~AO_Trigger_Length
;
3179 ni_stc_writew(dev
, devpriv
->ao_mode3
, AO_Mode_3_Register
);
3181 ni_stc_writew(dev
, devpriv
->ao_mode1
, AO_Mode_1_Register
);
3182 devpriv
->ao_mode2
&= ~AO_BC_Initial_Load_Source
;
3183 ni_stc_writew(dev
, devpriv
->ao_mode2
, AO_Mode_2_Register
);
3184 if (cmd
->stop_src
== TRIG_NONE
)
3185 ni_stc_writel(dev
, 0xffffff, AO_BC_Load_A_Register
);
3187 ni_stc_writel(dev
, 0, AO_BC_Load_A_Register
);
3188 ni_stc_writew(dev
, AO_BC_Load
, AO_Command_1_Register
);
3189 devpriv
->ao_mode2
&= ~AO_UC_Initial_Load_Source
;
3190 ni_stc_writew(dev
, devpriv
->ao_mode2
, AO_Mode_2_Register
);
3191 switch (cmd
->stop_src
) {
3193 if (devpriv
->is_m_series
) {
3194 /* this is how the NI example code does it for m-series boards, verified correct with 6259 */
3195 ni_stc_writel(dev
, cmd
->stop_arg
- 1,
3196 AO_UC_Load_A_Register
);
3197 ni_stc_writew(dev
, AO_UC_Load
, AO_Command_1_Register
);
3199 ni_stc_writel(dev
, cmd
->stop_arg
,
3200 AO_UC_Load_A_Register
);
3201 ni_stc_writew(dev
, AO_UC_Load
, AO_Command_1_Register
);
3202 ni_stc_writel(dev
, cmd
->stop_arg
- 1,
3203 AO_UC_Load_A_Register
);
3207 ni_stc_writel(dev
, 0xffffff, AO_UC_Load_A_Register
);
3208 ni_stc_writew(dev
, AO_UC_Load
, AO_Command_1_Register
);
3209 ni_stc_writel(dev
, 0xffffff, AO_UC_Load_A_Register
);
3212 ni_stc_writel(dev
, 0, AO_UC_Load_A_Register
);
3213 ni_stc_writew(dev
, AO_UC_Load
, AO_Command_1_Register
);
3214 ni_stc_writel(dev
, cmd
->stop_arg
, AO_UC_Load_A_Register
);
3217 devpriv
->ao_mode1
&=
3218 ~(AO_UI_Source_Select(0x1f) | AO_UI_Source_Polarity
|
3219 AO_UPDATE_Source_Select(0x1f) | AO_UPDATE_Source_Polarity
);
3220 switch (cmd
->scan_begin_src
) {
3222 devpriv
->ao_cmd2
&= ~AO_BC_Gate_Enable
;
3224 ni_ns_to_timer(dev
, cmd
->scan_begin_arg
,
3225 TRIG_ROUND_NEAREST
);
3226 ni_stc_writel(dev
, 1, AO_UI_Load_A_Register
);
3227 ni_stc_writew(dev
, AO_UI_Load
, AO_Command_1_Register
);
3228 ni_stc_writel(dev
, trigvar
, AO_UI_Load_A_Register
);
3231 devpriv
->ao_mode1
|=
3232 AO_UPDATE_Source_Select(cmd
->scan_begin_arg
);
3233 if (cmd
->scan_begin_arg
& CR_INVERT
)
3234 devpriv
->ao_mode1
|= AO_UPDATE_Source_Polarity
;
3235 devpriv
->ao_cmd2
|= AO_BC_Gate_Enable
;
3241 ni_stc_writew(dev
, devpriv
->ao_cmd2
, AO_Command_2_Register
);
3242 ni_stc_writew(dev
, devpriv
->ao_mode1
, AO_Mode_1_Register
);
3243 devpriv
->ao_mode2
&=
3244 ~(AO_UI_Reload_Mode(3) | AO_UI_Initial_Load_Source
);
3245 ni_stc_writew(dev
, devpriv
->ao_mode2
, AO_Mode_2_Register
);
3247 if (cmd
->scan_end_arg
> 1) {
3248 devpriv
->ao_mode1
|= AO_Multiple_Channels
;
3250 AO_Number_Of_Channels(cmd
->scan_end_arg
- 1) |
3251 AO_UPDATE_Output_Select(AO_Update_Output_High_Z
),
3252 AO_Output_Control_Register
);
3255 devpriv
->ao_mode1
&= ~AO_Multiple_Channels
;
3256 bits
= AO_UPDATE_Output_Select(AO_Update_Output_High_Z
);
3257 if (devpriv
->is_m_series
|| devpriv
->is_6xxx
) {
3258 bits
|= AO_Number_Of_Channels(0);
3261 AO_Number_Of_Channels(CR_CHAN(cmd
->chanlist
[0]));
3263 ni_stc_writew(dev
, bits
, AO_Output_Control_Register
);
3265 ni_stc_writew(dev
, devpriv
->ao_mode1
, AO_Mode_1_Register
);
3267 ni_stc_writew(dev
, AO_DAC0_Update_Mode
| AO_DAC1_Update_Mode
,
3268 AO_Command_1_Register
);
3270 devpriv
->ao_mode3
|= AO_Stop_On_Overrun_Error
;
3271 ni_stc_writew(dev
, devpriv
->ao_mode3
, AO_Mode_3_Register
);
3273 devpriv
->ao_mode2
&= ~AO_FIFO_Mode_Mask
;
3275 devpriv
->ao_mode2
|= AO_FIFO_Mode_HF_to_F
;
3277 devpriv
->ao_mode2
|= AO_FIFO_Mode_HF
;
3279 devpriv
->ao_mode2
&= ~AO_FIFO_Retransmit_Enable
;
3280 ni_stc_writew(dev
, devpriv
->ao_mode2
, AO_Mode_2_Register
);
3282 bits
= AO_BC_Source_Select
| AO_UPDATE_Pulse_Width
|
3283 AO_TMRDACWR_Pulse_Width
;
3284 if (board
->ao_fifo_depth
)
3285 bits
|= AO_FIFO_Enable
;
3287 bits
|= AO_DMA_PIO_Control
;
3289 /* F Hess: windows driver does not set AO_Number_Of_DAC_Packages bit for 6281,
3290 verified with bus analyzer. */
3291 if (devpriv
->is_m_series
)
3292 bits
|= AO_Number_Of_DAC_Packages
;
3294 ni_stc_writew(dev
, bits
, AO_Personal_Register
);
3295 /* enable sending of ao dma requests */
3296 ni_stc_writew(dev
, AO_AOFREQ_Enable
, AO_Start_Select_Register
);
3298 ni_stc_writew(dev
, AO_Configuration_End
, Joint_Reset_Register
);
3300 if (cmd
->stop_src
== TRIG_COUNT
) {
3301 ni_stc_writew(dev
, AO_BC_TC_Interrupt_Ack
,
3302 Interrupt_B_Ack_Register
);
3303 ni_set_bits(dev
, Interrupt_B_Enable_Register
,
3304 AO_BC_TC_Interrupt_Enable
, 1);
3307 s
->async
->inttrig
= ni_ao_inttrig
;
3312 static int ni_ao_cmdtest(struct comedi_device
*dev
, struct comedi_subdevice
*s
,
3313 struct comedi_cmd
*cmd
)
3315 const struct ni_board_struct
*board
= comedi_board(dev
);
3316 struct ni_private
*devpriv
= dev
->private;
3320 /* Step 1 : check if triggers are trivially valid */
3322 if ((cmd
->flags
& CMDF_WRITE
) == 0)
3323 cmd
->flags
|= CMDF_WRITE
;
3325 err
|= cfc_check_trigger_src(&cmd
->start_src
, TRIG_INT
| TRIG_EXT
);
3326 err
|= cfc_check_trigger_src(&cmd
->scan_begin_src
,
3327 TRIG_TIMER
| TRIG_EXT
);
3328 err
|= cfc_check_trigger_src(&cmd
->convert_src
, TRIG_NOW
);
3329 err
|= cfc_check_trigger_src(&cmd
->scan_end_src
, TRIG_COUNT
);
3330 err
|= cfc_check_trigger_src(&cmd
->stop_src
, TRIG_COUNT
| TRIG_NONE
);
3335 /* Step 2a : make sure trigger sources are unique */
3337 err
|= cfc_check_trigger_is_unique(cmd
->start_src
);
3338 err
|= cfc_check_trigger_is_unique(cmd
->scan_begin_src
);
3339 err
|= cfc_check_trigger_is_unique(cmd
->stop_src
);
3341 /* Step 2b : and mutually compatible */
3346 /* Step 3: check if arguments are trivially valid */
3348 switch (cmd
->start_src
) {
3350 err
|= cfc_check_trigger_arg_is(&cmd
->start_arg
, 0);
3353 tmp
= CR_CHAN(cmd
->start_arg
);
3357 tmp
|= (cmd
->start_arg
& (CR_INVERT
| CR_EDGE
));
3358 err
|= cfc_check_trigger_arg_is(&cmd
->start_arg
, tmp
);
3362 if (cmd
->scan_begin_src
== TRIG_TIMER
) {
3363 err
|= cfc_check_trigger_arg_min(&cmd
->scan_begin_arg
,
3365 err
|= cfc_check_trigger_arg_max(&cmd
->scan_begin_arg
,
3366 devpriv
->clock_ns
* 0xffffff);
3369 err
|= cfc_check_trigger_arg_is(&cmd
->convert_arg
, 0);
3370 err
|= cfc_check_trigger_arg_is(&cmd
->scan_end_arg
, cmd
->chanlist_len
);
3372 if (cmd
->stop_src
== TRIG_COUNT
)
3373 err
|= cfc_check_trigger_arg_max(&cmd
->stop_arg
, 0x00ffffff);
3374 else /* TRIG_NONE */
3375 err
|= cfc_check_trigger_arg_is(&cmd
->stop_arg
, 0);
3380 /* step 4: fix up any arguments */
3381 if (cmd
->scan_begin_src
== TRIG_TIMER
) {
3382 tmp
= cmd
->scan_begin_arg
;
3383 cmd
->scan_begin_arg
=
3384 ni_timer_to_ns(dev
, ni_ns_to_timer(dev
,
3385 cmd
->scan_begin_arg
,
3389 if (tmp
!= cmd
->scan_begin_arg
)
3398 static int ni_ao_reset(struct comedi_device
*dev
, struct comedi_subdevice
*s
)
3400 struct ni_private
*devpriv
= dev
->private;
3402 ni_release_ao_mite_channel(dev
);
3404 ni_stc_writew(dev
, AO_Configuration_Start
, Joint_Reset_Register
);
3405 ni_stc_writew(dev
, AO_Disarm
, AO_Command_1_Register
);
3406 ni_set_bits(dev
, Interrupt_B_Enable_Register
, ~0, 0);
3407 ni_stc_writew(dev
, AO_BC_Source_Select
, AO_Personal_Register
);
3408 ni_stc_writew(dev
, 0x3f98, Interrupt_B_Ack_Register
);
3409 ni_stc_writew(dev
, AO_BC_Source_Select
| AO_UPDATE_Pulse_Width
|
3410 AO_TMRDACWR_Pulse_Width
, AO_Personal_Register
);
3411 ni_stc_writew(dev
, 0, AO_Output_Control_Register
);
3412 ni_stc_writew(dev
, 0, AO_Start_Select_Register
);
3413 devpriv
->ao_cmd1
= 0;
3414 ni_stc_writew(dev
, devpriv
->ao_cmd1
, AO_Command_1_Register
);
3415 devpriv
->ao_cmd2
= 0;
3416 ni_stc_writew(dev
, devpriv
->ao_cmd2
, AO_Command_2_Register
);
3417 devpriv
->ao_mode1
= 0;
3418 ni_stc_writew(dev
, devpriv
->ao_mode1
, AO_Mode_1_Register
);
3419 devpriv
->ao_mode2
= 0;
3420 ni_stc_writew(dev
, devpriv
->ao_mode2
, AO_Mode_2_Register
);
3421 if (devpriv
->is_m_series
)
3422 devpriv
->ao_mode3
= AO_Last_Gate_Disable
;
3424 devpriv
->ao_mode3
= 0;
3425 ni_stc_writew(dev
, devpriv
->ao_mode3
, AO_Mode_3_Register
);
3426 devpriv
->ao_trigger_select
= 0;
3427 ni_stc_writew(dev
, devpriv
->ao_trigger_select
,
3428 AO_Trigger_Select_Register
);
3429 if (devpriv
->is_6xxx
) {
3430 unsigned immediate_bits
= 0;
3432 for (i
= 0; i
< s
->n_chan
; ++i
)
3433 immediate_bits
|= 1 << i
;
3434 ni_ao_win_outw(dev
, immediate_bits
, AO_Immediate_671x
);
3435 ni_ao_win_outw(dev
, CLEAR_WG
, AO_Misc_611x
);
3437 ni_stc_writew(dev
, AO_Configuration_End
, Joint_Reset_Register
);
3444 static int ni_dio_insn_config(struct comedi_device
*dev
,
3445 struct comedi_subdevice
*s
,
3446 struct comedi_insn
*insn
,
3449 struct ni_private
*devpriv
= dev
->private;
3452 ret
= comedi_dio_insn_config(dev
, s
, insn
, data
, 0);
3456 devpriv
->dio_control
&= ~DIO_Pins_Dir_Mask
;
3457 devpriv
->dio_control
|= DIO_Pins_Dir(s
->io_bits
);
3458 ni_stc_writew(dev
, devpriv
->dio_control
, DIO_Control_Register
);
3463 static int ni_dio_insn_bits(struct comedi_device
*dev
,
3464 struct comedi_subdevice
*s
,
3465 struct comedi_insn
*insn
,
3468 struct ni_private
*devpriv
= dev
->private;
3470 /* Make sure we're not using the serial part of the dio */
3471 if ((data
[0] & (DIO_SDIN
| DIO_SDOUT
)) && devpriv
->serial_interval_ns
)
3474 if (comedi_dio_update_state(s
, data
)) {
3475 devpriv
->dio_output
&= ~DIO_Parallel_Data_Mask
;
3476 devpriv
->dio_output
|= DIO_Parallel_Data_Out(s
->state
);
3477 ni_stc_writew(dev
, devpriv
->dio_output
, DIO_Output_Register
);
3480 data
[1] = ni_stc_readw(dev
, DIO_Parallel_Input_Register
);
3485 static int ni_m_series_dio_insn_config(struct comedi_device
*dev
,
3486 struct comedi_subdevice
*s
,
3487 struct comedi_insn
*insn
,
3492 ret
= comedi_dio_insn_config(dev
, s
, insn
, data
, 0);
3496 ni_writel(dev
, s
->io_bits
, M_Offset_DIO_Direction
);
3501 static int ni_m_series_dio_insn_bits(struct comedi_device
*dev
,
3502 struct comedi_subdevice
*s
,
3503 struct comedi_insn
*insn
,
3506 if (comedi_dio_update_state(s
, data
))
3507 ni_writel(dev
, s
->state
, M_Offset_Static_Digital_Output
);
3509 data
[1] = ni_readl(dev
, M_Offset_Static_Digital_Input
);
3514 static int ni_cdio_check_chanlist(struct comedi_device
*dev
,
3515 struct comedi_subdevice
*s
,
3516 struct comedi_cmd
*cmd
)
3520 for (i
= 0; i
< cmd
->chanlist_len
; ++i
) {
3521 unsigned int chan
= CR_CHAN(cmd
->chanlist
[i
]);
3530 static int ni_cdio_cmdtest(struct comedi_device
*dev
,
3531 struct comedi_subdevice
*s
, struct comedi_cmd
*cmd
)
3536 /* Step 1 : check if triggers are trivially valid */
3538 err
|= cfc_check_trigger_src(&cmd
->start_src
, TRIG_INT
);
3539 err
|= cfc_check_trigger_src(&cmd
->scan_begin_src
, TRIG_EXT
);
3540 err
|= cfc_check_trigger_src(&cmd
->convert_src
, TRIG_NOW
);
3541 err
|= cfc_check_trigger_src(&cmd
->scan_end_src
, TRIG_COUNT
);
3542 err
|= cfc_check_trigger_src(&cmd
->stop_src
, TRIG_NONE
);
3547 /* Step 2a : make sure trigger sources are unique */
3548 /* Step 2b : and mutually compatible */
3553 /* Step 3: check if arguments are trivially valid */
3555 err
|= cfc_check_trigger_arg_is(&cmd
->start_arg
, 0);
3557 tmp
= cmd
->scan_begin_arg
;
3558 tmp
&= CR_PACK_FLAGS(CDO_Sample_Source_Select_Mask
, 0, 0, CR_INVERT
);
3559 if (tmp
!= cmd
->scan_begin_arg
)
3562 err
|= cfc_check_trigger_arg_is(&cmd
->convert_arg
, 0);
3563 err
|= cfc_check_trigger_arg_is(&cmd
->scan_end_arg
, cmd
->chanlist_len
);
3564 err
|= cfc_check_trigger_arg_is(&cmd
->stop_arg
, 0);
3569 /* step 4: fix up any arguments */
3574 /* Step 5: check channel list if it exists */
3575 if (cmd
->chanlist
&& cmd
->chanlist_len
> 0)
3576 err
|= ni_cdio_check_chanlist(dev
, s
, cmd
);
3584 static int ni_cdo_inttrig(struct comedi_device
*dev
,
3585 struct comedi_subdevice
*s
,
3586 unsigned int trig_num
)
3588 struct comedi_cmd
*cmd
= &s
->async
->cmd
;
3589 const unsigned timeout
= 1000;
3593 struct ni_private
*devpriv
= dev
->private;
3594 unsigned long flags
;
3597 if (trig_num
!= cmd
->start_arg
)
3600 s
->async
->inttrig
= NULL
;
3602 /* read alloc the entire buffer */
3603 comedi_buf_read_alloc(s
, s
->async
->prealloc_bufsz
);
3606 spin_lock_irqsave(&devpriv
->mite_channel_lock
, flags
);
3607 if (devpriv
->cdo_mite_chan
) {
3608 mite_prep_dma(devpriv
->cdo_mite_chan
, 32, 32);
3609 mite_dma_arm(devpriv
->cdo_mite_chan
);
3611 comedi_error(dev
, "BUG: no cdo mite channel?");
3614 spin_unlock_irqrestore(&devpriv
->mite_channel_lock
, flags
);
3619 * XXX not sure what interrupt C group does
3620 * ni_writeb(dev, Interrupt_Group_C_Enable_Bit,
3621 * M_Offset_Interrupt_C_Enable); wait for dma to fill output fifo
3623 for (i
= 0; i
< timeout
; ++i
) {
3624 if (ni_readl(dev
, M_Offset_CDIO_Status
) & CDO_FIFO_Full_Bit
)
3629 comedi_error(dev
, "dma failed to fill cdo fifo!");
3633 ni_writel(dev
, CDO_Arm_Bit
| CDO_Error_Interrupt_Enable_Set_Bit
|
3634 CDO_Empty_FIFO_Interrupt_Enable_Set_Bit
,
3635 M_Offset_CDIO_Command
);
3639 static int ni_cdio_cmd(struct comedi_device
*dev
, struct comedi_subdevice
*s
)
3641 const struct comedi_cmd
*cmd
= &s
->async
->cmd
;
3642 unsigned cdo_mode_bits
= CDO_FIFO_Mode_Bit
| CDO_Halt_On_Error_Bit
;
3645 ni_writel(dev
, CDO_Reset_Bit
, M_Offset_CDIO_Command
);
3646 switch (cmd
->scan_begin_src
) {
3649 CR_CHAN(cmd
->scan_begin_arg
) &
3650 CDO_Sample_Source_Select_Mask
;
3656 if (cmd
->scan_begin_arg
& CR_INVERT
)
3657 cdo_mode_bits
|= CDO_Polarity_Bit
;
3658 ni_writel(dev
, cdo_mode_bits
, M_Offset_CDO_Mode
);
3660 ni_writel(dev
, s
->state
, M_Offset_CDO_FIFO_Data
);
3661 ni_writel(dev
, CDO_SW_Update_Bit
, M_Offset_CDIO_Command
);
3662 ni_writel(dev
, s
->io_bits
, M_Offset_CDO_Mask_Enable
);
3665 "attempted to run digital output command with no lines configured as outputs");
3668 retval
= ni_request_cdo_mite_channel(dev
);
3672 s
->async
->inttrig
= ni_cdo_inttrig
;
3677 static int ni_cdio_cancel(struct comedi_device
*dev
, struct comedi_subdevice
*s
)
3679 ni_writel(dev
, CDO_Disarm_Bit
| CDO_Error_Interrupt_Enable_Clear_Bit
|
3680 CDO_Empty_FIFO_Interrupt_Enable_Clear_Bit
|
3681 CDO_FIFO_Request_Interrupt_Enable_Clear_Bit
,
3682 M_Offset_CDIO_Command
);
3684 * XXX not sure what interrupt C group does ni_writeb(dev, 0,
3685 * M_Offset_Interrupt_C_Enable);
3687 ni_writel(dev
, 0, M_Offset_CDO_Mask_Enable
);
3688 ni_release_cdo_mite_channel(dev
);
3692 static void handle_cdio_interrupt(struct comedi_device
*dev
)
3694 struct ni_private
*devpriv
= dev
->private;
3695 unsigned cdio_status
;
3696 struct comedi_subdevice
*s
= &dev
->subdevices
[NI_DIO_SUBDEV
];
3698 unsigned long flags
;
3701 if (!devpriv
->is_m_series
)
3704 spin_lock_irqsave(&devpriv
->mite_channel_lock
, flags
);
3705 if (devpriv
->cdo_mite_chan
) {
3706 unsigned cdo_mite_status
=
3707 mite_get_status(devpriv
->cdo_mite_chan
);
3708 if (cdo_mite_status
& CHSR_LINKC
) {
3710 devpriv
->mite
->mite_io_addr
+
3711 MITE_CHOR(devpriv
->cdo_mite_chan
->channel
));
3713 mite_sync_output_dma(devpriv
->cdo_mite_chan
, s
);
3715 spin_unlock_irqrestore(&devpriv
->mite_channel_lock
, flags
);
3718 cdio_status
= ni_readl(dev
, M_Offset_CDIO_Status
);
3719 if (cdio_status
& (CDO_Overrun_Bit
| CDO_Underflow_Bit
)) {
3720 /* printk("cdio error: statux=0x%x\n", cdio_status); */
3721 /* XXX just guessing this is needed and does something useful */
3722 ni_writel(dev
, CDO_Error_Interrupt_Confirm_Bit
,
3723 M_Offset_CDIO_Command
);
3724 s
->async
->events
|= COMEDI_CB_OVERFLOW
;
3726 if (cdio_status
& CDO_FIFO_Empty_Bit
) {
3727 /* printk("cdio fifo empty\n"); */
3728 ni_writel(dev
, CDO_Empty_FIFO_Interrupt_Enable_Clear_Bit
,
3729 M_Offset_CDIO_Command
);
3730 /* s->async->events |= COMEDI_CB_EOA; */
3732 cfc_handle_events(dev
, s
);
3735 static int ni_serial_hw_readwrite8(struct comedi_device
*dev
,
3736 struct comedi_subdevice
*s
,
3737 unsigned char data_out
,
3738 unsigned char *data_in
)
3740 struct ni_private
*devpriv
= dev
->private;
3741 unsigned int status1
;
3742 int err
= 0, count
= 20;
3744 devpriv
->dio_output
&= ~DIO_Serial_Data_Mask
;
3745 devpriv
->dio_output
|= DIO_Serial_Data_Out(data_out
);
3746 ni_stc_writew(dev
, devpriv
->dio_output
, DIO_Output_Register
);
3748 status1
= ni_stc_readw(dev
, Joint_Status_1_Register
);
3749 if (status1
& DIO_Serial_IO_In_Progress_St
) {
3754 devpriv
->dio_control
|= DIO_HW_Serial_Start
;
3755 ni_stc_writew(dev
, devpriv
->dio_control
, DIO_Control_Register
);
3756 devpriv
->dio_control
&= ~DIO_HW_Serial_Start
;
3758 /* Wait until STC says we're done, but don't loop infinitely. */
3759 while ((status1
= ni_stc_readw(dev
, Joint_Status_1_Register
)) &
3760 DIO_Serial_IO_In_Progress_St
) {
3761 /* Delay one bit per loop */
3762 udelay((devpriv
->serial_interval_ns
+ 999) / 1000);
3765 ("ni_serial_hw_readwrite8: SPI serial I/O didn't finish in time!\n");
3771 /* Delay for last bit. This delay is absolutely necessary, because
3772 DIO_Serial_IO_In_Progress_St goes high one bit too early. */
3773 udelay((devpriv
->serial_interval_ns
+ 999) / 1000);
3775 if (data_in
!= NULL
)
3776 *data_in
= ni_stc_readw(dev
, DIO_Serial_Input_Register
);
3779 ni_stc_writew(dev
, devpriv
->dio_control
, DIO_Control_Register
);
3784 static int ni_serial_sw_readwrite8(struct comedi_device
*dev
,
3785 struct comedi_subdevice
*s
,
3786 unsigned char data_out
,
3787 unsigned char *data_in
)
3789 struct ni_private
*devpriv
= dev
->private;
3790 unsigned char mask
, input
= 0;
3792 /* Wait for one bit before transfer */
3793 udelay((devpriv
->serial_interval_ns
+ 999) / 1000);
3795 for (mask
= 0x80; mask
; mask
>>= 1) {
3796 /* Output current bit; note that we cannot touch s->state
3797 because it is a per-subdevice field, and serial is
3798 a separate subdevice from DIO. */
3799 devpriv
->dio_output
&= ~DIO_SDOUT
;
3800 if (data_out
& mask
)
3801 devpriv
->dio_output
|= DIO_SDOUT
;
3802 ni_stc_writew(dev
, devpriv
->dio_output
, DIO_Output_Register
);
3804 /* Assert SDCLK (active low, inverted), wait for half of
3805 the delay, deassert SDCLK, and wait for the other half. */
3806 devpriv
->dio_control
|= DIO_Software_Serial_Control
;
3807 ni_stc_writew(dev
, devpriv
->dio_control
, DIO_Control_Register
);
3809 udelay((devpriv
->serial_interval_ns
+ 999) / 2000);
3811 devpriv
->dio_control
&= ~DIO_Software_Serial_Control
;
3812 ni_stc_writew(dev
, devpriv
->dio_control
, DIO_Control_Register
);
3814 udelay((devpriv
->serial_interval_ns
+ 999) / 2000);
3816 /* Input current bit */
3817 if (ni_stc_readw(dev
, DIO_Parallel_Input_Register
) & DIO_SDIN
) {
3818 /* printk("DIO_P_I_R: 0x%x\n", ni_stc_readw(dev, DIO_Parallel_Input_Register)); */
3829 static int ni_serial_insn_config(struct comedi_device
*dev
,
3830 struct comedi_subdevice
*s
,
3831 struct comedi_insn
*insn
,
3834 struct ni_private
*devpriv
= dev
->private;
3836 unsigned char byte_out
, byte_in
= 0;
3842 case INSN_CONFIG_SERIAL_CLOCK
:
3843 devpriv
->serial_hw_mode
= 1;
3844 devpriv
->dio_control
|= DIO_HW_Serial_Enable
;
3846 if (data
[1] == SERIAL_DISABLED
) {
3847 devpriv
->serial_hw_mode
= 0;
3848 devpriv
->dio_control
&= ~(DIO_HW_Serial_Enable
|
3849 DIO_Software_Serial_Control
);
3850 data
[1] = SERIAL_DISABLED
;
3851 devpriv
->serial_interval_ns
= data
[1];
3852 } else if (data
[1] <= SERIAL_600NS
) {
3853 /* Warning: this clock speed is too fast to reliably
3855 devpriv
->dio_control
&= ~DIO_HW_Serial_Timebase
;
3856 devpriv
->clock_and_fout
|= Slow_Internal_Timebase
;
3857 devpriv
->clock_and_fout
&= ~DIO_Serial_Out_Divide_By_2
;
3858 data
[1] = SERIAL_600NS
;
3859 devpriv
->serial_interval_ns
= data
[1];
3860 } else if (data
[1] <= SERIAL_1_2US
) {
3861 devpriv
->dio_control
&= ~DIO_HW_Serial_Timebase
;
3862 devpriv
->clock_and_fout
|= Slow_Internal_Timebase
|
3863 DIO_Serial_Out_Divide_By_2
;
3864 data
[1] = SERIAL_1_2US
;
3865 devpriv
->serial_interval_ns
= data
[1];
3866 } else if (data
[1] <= SERIAL_10US
) {
3867 devpriv
->dio_control
|= DIO_HW_Serial_Timebase
;
3868 devpriv
->clock_and_fout
|= Slow_Internal_Timebase
|
3869 DIO_Serial_Out_Divide_By_2
;
3870 /* Note: DIO_Serial_Out_Divide_By_2 only affects
3871 600ns/1.2us. If you turn divide_by_2 off with the
3872 slow clock, you will still get 10us, except then
3873 all your delays are wrong. */
3874 data
[1] = SERIAL_10US
;
3875 devpriv
->serial_interval_ns
= data
[1];
3877 devpriv
->dio_control
&= ~(DIO_HW_Serial_Enable
|
3878 DIO_Software_Serial_Control
);
3879 devpriv
->serial_hw_mode
= 0;
3880 data
[1] = (data
[1] / 1000) * 1000;
3881 devpriv
->serial_interval_ns
= data
[1];
3884 ni_stc_writew(dev
, devpriv
->dio_control
, DIO_Control_Register
);
3885 ni_stc_writew(dev
, devpriv
->clock_and_fout
,
3886 Clock_and_FOUT_Register
);
3891 case INSN_CONFIG_BIDIRECTIONAL_DATA
:
3893 if (devpriv
->serial_interval_ns
== 0)
3896 byte_out
= data
[1] & 0xFF;
3898 if (devpriv
->serial_hw_mode
) {
3899 err
= ni_serial_hw_readwrite8(dev
, s
, byte_out
,
3901 } else if (devpriv
->serial_interval_ns
> 0) {
3902 err
= ni_serial_sw_readwrite8(dev
, s
, byte_out
,
3905 printk("ni_serial_insn_config: serial disabled!\n");
3910 data
[1] = byte_in
& 0xFF;
3920 static void init_ao_67xx(struct comedi_device
*dev
, struct comedi_subdevice
*s
)
3924 for (i
= 0; i
< s
->n_chan
; i
++) {
3925 ni_ao_win_outw(dev
, AO_Channel(i
) | 0x0,
3926 AO_Configuration_2_67xx
);
3928 ni_ao_win_outw(dev
, 0x0, AO_Later_Single_Point_Updates
);
3931 static unsigned ni_gpct_to_stc_register(enum ni_gpct_register reg
)
3933 unsigned stc_register
;
3935 case NITIO_G0_AUTO_INC
:
3936 stc_register
= G_Autoincrement_Register(0);
3938 case NITIO_G1_AUTO_INC
:
3939 stc_register
= G_Autoincrement_Register(1);
3942 stc_register
= G_Command_Register(0);
3945 stc_register
= G_Command_Register(1);
3947 case NITIO_G0_HW_SAVE
:
3948 stc_register
= G_HW_Save_Register(0);
3950 case NITIO_G1_HW_SAVE
:
3951 stc_register
= G_HW_Save_Register(1);
3953 case NITIO_G0_SW_SAVE
:
3954 stc_register
= G_Save_Register(0);
3956 case NITIO_G1_SW_SAVE
:
3957 stc_register
= G_Save_Register(1);
3960 stc_register
= G_Mode_Register(0);
3963 stc_register
= G_Mode_Register(1);
3965 case NITIO_G0_LOADA
:
3966 stc_register
= G_Load_A_Register(0);
3968 case NITIO_G1_LOADA
:
3969 stc_register
= G_Load_A_Register(1);
3971 case NITIO_G0_LOADB
:
3972 stc_register
= G_Load_B_Register(0);
3974 case NITIO_G1_LOADB
:
3975 stc_register
= G_Load_B_Register(1);
3977 case NITIO_G0_INPUT_SEL
:
3978 stc_register
= G_Input_Select_Register(0);
3980 case NITIO_G1_INPUT_SEL
:
3981 stc_register
= G_Input_Select_Register(1);
3983 case NITIO_G01_STATUS
:
3984 stc_register
= G_Status_Register
;
3986 case NITIO_G01_RESET
:
3987 stc_register
= Joint_Reset_Register
;
3989 case NITIO_G01_STATUS1
:
3990 stc_register
= Joint_Status_1_Register
;
3992 case NITIO_G01_STATUS2
:
3993 stc_register
= Joint_Status_2_Register
;
3995 case NITIO_G0_INT_ACK
:
3996 stc_register
= Interrupt_A_Ack_Register
;
3998 case NITIO_G1_INT_ACK
:
3999 stc_register
= Interrupt_B_Ack_Register
;
4001 case NITIO_G0_STATUS
:
4002 stc_register
= AI_Status_1_Register
;
4004 case NITIO_G1_STATUS
:
4005 stc_register
= AO_Status_1_Register
;
4007 case NITIO_G0_INT_ENA
:
4008 stc_register
= Interrupt_A_Enable_Register
;
4010 case NITIO_G1_INT_ENA
:
4011 stc_register
= Interrupt_B_Enable_Register
;
4014 printk("%s: unhandled register 0x%x in switch.\n",
4020 return stc_register
;
4023 static void ni_gpct_write_register(struct ni_gpct
*counter
, unsigned bits
,
4024 enum ni_gpct_register reg
)
4026 struct comedi_device
*dev
= counter
->counter_dev
->dev
;
4027 unsigned stc_register
;
4028 /* bits in the join reset register which are relevant to counters */
4029 static const unsigned gpct_joint_reset_mask
= G0_Reset
| G1_Reset
;
4030 static const unsigned gpct_interrupt_a_enable_mask
=
4031 G0_Gate_Interrupt_Enable
| G0_TC_Interrupt_Enable
;
4032 static const unsigned gpct_interrupt_b_enable_mask
=
4033 G1_Gate_Interrupt_Enable
| G1_TC_Interrupt_Enable
;
4036 /* m-series-only registers */
4037 case NITIO_G0_CNT_MODE
:
4038 ni_writew(dev
, bits
, M_Offset_G0_Counting_Mode
);
4040 case NITIO_G1_CNT_MODE
:
4041 ni_writew(dev
, bits
, M_Offset_G1_Counting_Mode
);
4043 case NITIO_G0_GATE2
:
4044 ni_writew(dev
, bits
, M_Offset_G0_Second_Gate
);
4046 case NITIO_G1_GATE2
:
4047 ni_writew(dev
, bits
, M_Offset_G1_Second_Gate
);
4049 case NITIO_G0_DMA_CFG
:
4050 ni_writew(dev
, bits
, M_Offset_G0_DMA_Config
);
4052 case NITIO_G1_DMA_CFG
:
4053 ni_writew(dev
, bits
, M_Offset_G1_DMA_Config
);
4056 ni_writew(dev
, bits
, M_Offset_G0_MSeries_ABZ
);
4059 ni_writew(dev
, bits
, M_Offset_G1_MSeries_ABZ
);
4062 /* 32 bit registers */
4063 case NITIO_G0_LOADA
:
4064 case NITIO_G1_LOADA
:
4065 case NITIO_G0_LOADB
:
4066 case NITIO_G1_LOADB
:
4067 stc_register
= ni_gpct_to_stc_register(reg
);
4068 ni_stc_writel(dev
, bits
, stc_register
);
4071 /* 16 bit registers */
4072 case NITIO_G0_INT_ENA
:
4073 BUG_ON(bits
& ~gpct_interrupt_a_enable_mask
);
4074 ni_set_bitfield(dev
, Interrupt_A_Enable_Register
,
4075 gpct_interrupt_a_enable_mask
, bits
);
4077 case NITIO_G1_INT_ENA
:
4078 BUG_ON(bits
& ~gpct_interrupt_b_enable_mask
);
4079 ni_set_bitfield(dev
, Interrupt_B_Enable_Register
,
4080 gpct_interrupt_b_enable_mask
, bits
);
4082 case NITIO_G01_RESET
:
4083 BUG_ON(bits
& ~gpct_joint_reset_mask
);
4086 stc_register
= ni_gpct_to_stc_register(reg
);
4087 ni_stc_writew(dev
, bits
, stc_register
);
4091 static unsigned ni_gpct_read_register(struct ni_gpct
*counter
,
4092 enum ni_gpct_register reg
)
4094 struct comedi_device
*dev
= counter
->counter_dev
->dev
;
4095 unsigned stc_register
;
4098 /* m-series only registers */
4099 case NITIO_G0_DMA_STATUS
:
4100 return ni_readw(dev
, M_Offset_G0_DMA_Status
);
4101 case NITIO_G1_DMA_STATUS
:
4102 return ni_readw(dev
, M_Offset_G1_DMA_Status
);
4104 /* 32 bit registers */
4105 case NITIO_G0_HW_SAVE
:
4106 case NITIO_G1_HW_SAVE
:
4107 case NITIO_G0_SW_SAVE
:
4108 case NITIO_G1_SW_SAVE
:
4109 stc_register
= ni_gpct_to_stc_register(reg
);
4110 return ni_stc_readl(dev
, stc_register
);
4112 /* 16 bit registers */
4114 stc_register
= ni_gpct_to_stc_register(reg
);
4115 return ni_stc_readw(dev
, stc_register
);
4121 static int ni_freq_out_insn_read(struct comedi_device
*dev
,
4122 struct comedi_subdevice
*s
,
4123 struct comedi_insn
*insn
,
4126 struct ni_private
*devpriv
= dev
->private;
4127 unsigned int val
= devpriv
->clock_and_fout
& FOUT_Divider_mask
;
4130 for (i
= 0; i
< insn
->n
; i
++)
4136 static int ni_freq_out_insn_write(struct comedi_device
*dev
,
4137 struct comedi_subdevice
*s
,
4138 struct comedi_insn
*insn
,
4141 struct ni_private
*devpriv
= dev
->private;
4144 devpriv
->clock_and_fout
&= ~FOUT_Enable
;
4145 ni_stc_writew(dev
, devpriv
->clock_and_fout
,
4146 Clock_and_FOUT_Register
);
4147 devpriv
->clock_and_fout
&= ~FOUT_Divider_mask
;
4149 /* use the last data value to set the fout divider */
4150 devpriv
->clock_and_fout
|= FOUT_Divider(data
[insn
->n
- 1]);
4152 devpriv
->clock_and_fout
|= FOUT_Enable
;
4153 ni_stc_writew(dev
, devpriv
->clock_and_fout
,
4154 Clock_and_FOUT_Register
);
4159 static int ni_freq_out_insn_config(struct comedi_device
*dev
,
4160 struct comedi_subdevice
*s
,
4161 struct comedi_insn
*insn
,
4164 struct ni_private
*devpriv
= dev
->private;
4167 case INSN_CONFIG_SET_CLOCK_SRC
:
4169 case NI_FREQ_OUT_TIMEBASE_1_DIV_2_CLOCK_SRC
:
4170 devpriv
->clock_and_fout
&= ~FOUT_Timebase_Select
;
4172 case NI_FREQ_OUT_TIMEBASE_2_CLOCK_SRC
:
4173 devpriv
->clock_and_fout
|= FOUT_Timebase_Select
;
4178 ni_stc_writew(dev
, devpriv
->clock_and_fout
,
4179 Clock_and_FOUT_Register
);
4181 case INSN_CONFIG_GET_CLOCK_SRC
:
4182 if (devpriv
->clock_and_fout
& FOUT_Timebase_Select
) {
4183 data
[1] = NI_FREQ_OUT_TIMEBASE_2_CLOCK_SRC
;
4184 data
[2] = TIMEBASE_2_NS
;
4186 data
[1] = NI_FREQ_OUT_TIMEBASE_1_DIV_2_CLOCK_SRC
;
4187 data
[2] = TIMEBASE_1_NS
* 2;
4196 static int ni_8255_callback(int dir
, int port
, int data
, unsigned long arg
)
4198 struct comedi_device
*dev
= (struct comedi_device
*)arg
;
4201 ni_writeb(dev
, data
, Port_A
+ 2 * port
);
4204 return ni_readb(dev
, Port_A
+ 2 * port
);
4208 static int ni_get_pwm_config(struct comedi_device
*dev
, unsigned int *data
)
4210 struct ni_private
*devpriv
= dev
->private;
4212 data
[1] = devpriv
->pwm_up_count
* devpriv
->clock_ns
;
4213 data
[2] = devpriv
->pwm_down_count
* devpriv
->clock_ns
;
4217 static int ni_m_series_pwm_config(struct comedi_device
*dev
,
4218 struct comedi_subdevice
*s
,
4219 struct comedi_insn
*insn
,
4222 struct ni_private
*devpriv
= dev
->private;
4223 unsigned up_count
, down_count
;
4226 case INSN_CONFIG_PWM_OUTPUT
:
4228 case TRIG_ROUND_NEAREST
:
4231 devpriv
->clock_ns
/ 2) / devpriv
->clock_ns
;
4233 case TRIG_ROUND_DOWN
:
4234 up_count
= data
[2] / devpriv
->clock_ns
;
4238 (data
[2] + devpriv
->clock_ns
-
4239 1) / devpriv
->clock_ns
;
4246 case TRIG_ROUND_NEAREST
:
4249 devpriv
->clock_ns
/ 2) / devpriv
->clock_ns
;
4251 case TRIG_ROUND_DOWN
:
4252 down_count
= data
[4] / devpriv
->clock_ns
;
4256 (data
[4] + devpriv
->clock_ns
-
4257 1) / devpriv
->clock_ns
;
4263 if (up_count
* devpriv
->clock_ns
!= data
[2] ||
4264 down_count
* devpriv
->clock_ns
!= data
[4]) {
4265 data
[2] = up_count
* devpriv
->clock_ns
;
4266 data
[4] = down_count
* devpriv
->clock_ns
;
4269 ni_writel(dev
, MSeries_Cal_PWM_High_Time_Bits(up_count
) |
4270 MSeries_Cal_PWM_Low_Time_Bits(down_count
),
4272 devpriv
->pwm_up_count
= up_count
;
4273 devpriv
->pwm_down_count
= down_count
;
4276 case INSN_CONFIG_GET_PWM_OUTPUT
:
4277 return ni_get_pwm_config(dev
, data
);
4286 static int ni_6143_pwm_config(struct comedi_device
*dev
,
4287 struct comedi_subdevice
*s
,
4288 struct comedi_insn
*insn
,
4291 struct ni_private
*devpriv
= dev
->private;
4292 unsigned up_count
, down_count
;
4295 case INSN_CONFIG_PWM_OUTPUT
:
4297 case TRIG_ROUND_NEAREST
:
4300 devpriv
->clock_ns
/ 2) / devpriv
->clock_ns
;
4302 case TRIG_ROUND_DOWN
:
4303 up_count
= data
[2] / devpriv
->clock_ns
;
4307 (data
[2] + devpriv
->clock_ns
-
4308 1) / devpriv
->clock_ns
;
4315 case TRIG_ROUND_NEAREST
:
4318 devpriv
->clock_ns
/ 2) / devpriv
->clock_ns
;
4320 case TRIG_ROUND_DOWN
:
4321 down_count
= data
[4] / devpriv
->clock_ns
;
4325 (data
[4] + devpriv
->clock_ns
-
4326 1) / devpriv
->clock_ns
;
4332 if (up_count
* devpriv
->clock_ns
!= data
[2] ||
4333 down_count
* devpriv
->clock_ns
!= data
[4]) {
4334 data
[2] = up_count
* devpriv
->clock_ns
;
4335 data
[4] = down_count
* devpriv
->clock_ns
;
4338 ni_writel(dev
, up_count
, Calibration_HighTime_6143
);
4339 devpriv
->pwm_up_count
= up_count
;
4340 ni_writel(dev
, down_count
, Calibration_LowTime_6143
);
4341 devpriv
->pwm_down_count
= down_count
;
4344 case INSN_CONFIG_GET_PWM_OUTPUT
:
4345 return ni_get_pwm_config(dev
, data
);
4353 static int pack_mb88341(int addr
, int val
, int *bitstring
)
4357 Note that address bits are reversed. Thanks to
4358 Ingo Keen for noticing this.
4360 Note also that the 88341 expects address values from
4361 1-12, whereas we use channel numbers 0-11. The NI
4362 docs use 1-12, also, so be careful here.
4365 *bitstring
= ((addr
& 0x1) << 11) |
4366 ((addr
& 0x2) << 9) |
4367 ((addr
& 0x4) << 7) | ((addr
& 0x8) << 5) | (val
& 0xff);
4371 static int pack_dac8800(int addr
, int val
, int *bitstring
)
4373 *bitstring
= ((addr
& 0x7) << 8) | (val
& 0xff);
4377 static int pack_dac8043(int addr
, int val
, int *bitstring
)
4379 *bitstring
= val
& 0xfff;
4383 static int pack_ad8522(int addr
, int val
, int *bitstring
)
4385 *bitstring
= (val
& 0xfff) | (addr
? 0xc000 : 0xa000);
4389 static int pack_ad8804(int addr
, int val
, int *bitstring
)
4391 *bitstring
= ((addr
& 0xf) << 8) | (val
& 0xff);
4395 static int pack_ad8842(int addr
, int val
, int *bitstring
)
4397 *bitstring
= ((addr
+ 1) << 8) | (val
& 0xff);
4401 struct caldac_struct
{
4404 int (*packbits
)(int, int, int *);
4407 static struct caldac_struct caldacs
[] = {
4408 [mb88341
] = {12, 8, pack_mb88341
},
4409 [dac8800
] = {8, 8, pack_dac8800
},
4410 [dac8043
] = {1, 12, pack_dac8043
},
4411 [ad8522
] = {2, 12, pack_ad8522
},
4412 [ad8804
] = {12, 8, pack_ad8804
},
4413 [ad8842
] = {8, 8, pack_ad8842
},
4414 [ad8804_debug
] = {16, 8, pack_ad8804
},
4417 static void ni_write_caldac(struct comedi_device
*dev
, int addr
, int val
)
4419 const struct ni_board_struct
*board
= comedi_board(dev
);
4420 struct ni_private
*devpriv
= dev
->private;
4421 unsigned int loadbit
= 0, bits
= 0, bit
, bitstring
= 0;
4425 /* printk("ni_write_caldac: chan=%d val=%d\n",addr,val); */
4426 if (devpriv
->caldacs
[addr
] == val
)
4428 devpriv
->caldacs
[addr
] = val
;
4430 for (i
= 0; i
< 3; i
++) {
4431 type
= board
->caldac
[i
];
4432 if (type
== caldac_none
)
4434 if (addr
< caldacs
[type
].n_chans
) {
4435 bits
= caldacs
[type
].packbits(addr
, val
, &bitstring
);
4436 loadbit
= SerDacLd(i
);
4437 /* printk("caldac: using i=%d addr=%d %x\n",i,addr,bitstring); */
4440 addr
-= caldacs
[type
].n_chans
;
4443 for (bit
= 1 << (bits
- 1); bit
; bit
>>= 1) {
4444 ni_writeb(dev
, ((bit
& bitstring
) ? 0x02 : 0), Serial_Command
);
4446 ni_writeb(dev
, 1 | ((bit
& bitstring
) ? 0x02 : 0),
4450 ni_writeb(dev
, loadbit
, Serial_Command
);
4452 ni_writeb(dev
, 0, Serial_Command
);
4455 static int ni_calib_insn_write(struct comedi_device
*dev
,
4456 struct comedi_subdevice
*s
,
4457 struct comedi_insn
*insn
,
4460 ni_write_caldac(dev
, CR_CHAN(insn
->chanspec
), data
[0]);
4465 static int ni_calib_insn_read(struct comedi_device
*dev
,
4466 struct comedi_subdevice
*s
,
4467 struct comedi_insn
*insn
,
4470 struct ni_private
*devpriv
= dev
->private;
4472 data
[0] = devpriv
->caldacs
[CR_CHAN(insn
->chanspec
)];
4477 static void caldac_setup(struct comedi_device
*dev
, struct comedi_subdevice
*s
)
4479 const struct ni_board_struct
*board
= comedi_board(dev
);
4480 struct ni_private
*devpriv
= dev
->private;
4489 type
= board
->caldac
[0];
4490 if (type
== caldac_none
)
4492 n_bits
= caldacs
[type
].n_bits
;
4493 for (i
= 0; i
< 3; i
++) {
4494 type
= board
->caldac
[i
];
4495 if (type
== caldac_none
)
4497 if (caldacs
[type
].n_bits
!= n_bits
)
4499 n_chans
+= caldacs
[type
].n_chans
;
4502 s
->n_chan
= n_chans
;
4505 unsigned int *maxdata_list
;
4507 if (n_chans
> MAX_N_CALDACS
)
4508 printk("BUG! MAX_N_CALDACS too small\n");
4509 s
->maxdata_list
= maxdata_list
= devpriv
->caldac_maxdata_list
;
4511 for (i
= 0; i
< n_dacs
; i
++) {
4512 type
= board
->caldac
[i
];
4513 for (j
= 0; j
< caldacs
[type
].n_chans
; j
++) {
4514 maxdata_list
[chan
] =
4515 (1 << caldacs
[type
].n_bits
) - 1;
4520 for (chan
= 0; chan
< s
->n_chan
; chan
++)
4521 ni_write_caldac(dev
, i
, s
->maxdata_list
[i
] / 2);
4523 type
= board
->caldac
[0];
4524 s
->maxdata
= (1 << caldacs
[type
].n_bits
) - 1;
4526 for (chan
= 0; chan
< s
->n_chan
; chan
++)
4527 ni_write_caldac(dev
, i
, s
->maxdata
/ 2);
4531 static int ni_read_eeprom(struct comedi_device
*dev
, int addr
)
4536 bitstring
= 0x0300 | ((addr
& 0x100) << 3) | (addr
& 0xff);
4537 ni_writeb(dev
, 0x04, Serial_Command
);
4538 for (bit
= 0x8000; bit
; bit
>>= 1) {
4539 ni_writeb(dev
, 0x04 | ((bit
& bitstring
) ? 0x02 : 0),
4541 ni_writeb(dev
, 0x05 | ((bit
& bitstring
) ? 0x02 : 0),
4545 for (bit
= 0x80; bit
; bit
>>= 1) {
4546 ni_writeb(dev
, 0x04, Serial_Command
);
4547 ni_writeb(dev
, 0x05, Serial_Command
);
4548 bitstring
|= ((ni_readb(dev
, XXX_Status
) & PROMOUT
) ? bit
: 0);
4550 ni_writeb(dev
, 0x00, Serial_Command
);
4555 static int ni_eeprom_insn_read(struct comedi_device
*dev
,
4556 struct comedi_subdevice
*s
,
4557 struct comedi_insn
*insn
,
4560 data
[0] = ni_read_eeprom(dev
, CR_CHAN(insn
->chanspec
));
4565 static int ni_m_series_eeprom_insn_read(struct comedi_device
*dev
,
4566 struct comedi_subdevice
*s
,
4567 struct comedi_insn
*insn
,
4570 struct ni_private
*devpriv
= dev
->private;
4572 data
[0] = devpriv
->eeprom_buffer
[CR_CHAN(insn
->chanspec
)];
4577 static unsigned ni_old_get_pfi_routing(struct comedi_device
*dev
,
4580 /* pre-m-series boards have fixed signals on pfi pins */
4583 return NI_PFI_OUTPUT_AI_START1
;
4586 return NI_PFI_OUTPUT_AI_START2
;
4589 return NI_PFI_OUTPUT_AI_CONVERT
;
4592 return NI_PFI_OUTPUT_G_SRC1
;
4595 return NI_PFI_OUTPUT_G_GATE1
;
4598 return NI_PFI_OUTPUT_AO_UPDATE_N
;
4601 return NI_PFI_OUTPUT_AO_START1
;
4604 return NI_PFI_OUTPUT_AI_START_PULSE
;
4607 return NI_PFI_OUTPUT_G_SRC0
;
4610 return NI_PFI_OUTPUT_G_GATE0
;
4613 printk("%s: bug, unhandled case in switch.\n", __func__
);
4619 static int ni_old_set_pfi_routing(struct comedi_device
*dev
,
4620 unsigned chan
, unsigned source
)
4622 /* pre-m-series boards have fixed signals on pfi pins */
4623 if (source
!= ni_old_get_pfi_routing(dev
, chan
))
4628 static unsigned ni_m_series_get_pfi_routing(struct comedi_device
*dev
,
4631 struct ni_private
*devpriv
= dev
->private;
4632 const unsigned array_offset
= chan
/ 3;
4634 return MSeries_PFI_Output_Select_Source(chan
,
4635 devpriv
->pfi_output_select_reg
[array_offset
]);
4638 static int ni_m_series_set_pfi_routing(struct comedi_device
*dev
,
4639 unsigned chan
, unsigned source
)
4641 struct ni_private
*devpriv
= dev
->private;
4642 unsigned pfi_reg_index
;
4643 unsigned array_offset
;
4645 if ((source
& 0x1f) != source
)
4647 pfi_reg_index
= 1 + chan
/ 3;
4648 array_offset
= pfi_reg_index
- 1;
4649 devpriv
->pfi_output_select_reg
[array_offset
] &=
4650 ~MSeries_PFI_Output_Select_Mask(chan
);
4651 devpriv
->pfi_output_select_reg
[array_offset
] |=
4652 MSeries_PFI_Output_Select_Bits(chan
, source
);
4653 ni_writew(dev
, devpriv
->pfi_output_select_reg
[array_offset
],
4654 M_Offset_PFI_Output_Select(pfi_reg_index
));
4658 static unsigned ni_get_pfi_routing(struct comedi_device
*dev
, unsigned chan
)
4660 struct ni_private
*devpriv
= dev
->private;
4662 if (devpriv
->is_m_series
)
4663 return ni_m_series_get_pfi_routing(dev
, chan
);
4665 return ni_old_get_pfi_routing(dev
, chan
);
4668 static int ni_set_pfi_routing(struct comedi_device
*dev
, unsigned chan
,
4671 struct ni_private
*devpriv
= dev
->private;
4673 if (devpriv
->is_m_series
)
4674 return ni_m_series_set_pfi_routing(dev
, chan
, source
);
4676 return ni_old_set_pfi_routing(dev
, chan
, source
);
4679 static int ni_config_filter(struct comedi_device
*dev
,
4680 unsigned pfi_channel
,
4681 enum ni_pfi_filter_select filter
)
4683 struct ni_private
*devpriv
= dev
->private;
4686 if (!devpriv
->is_m_series
)
4689 bits
= ni_readl(dev
, M_Offset_PFI_Filter
);
4690 bits
&= ~MSeries_PFI_Filter_Select_Mask(pfi_channel
);
4691 bits
|= MSeries_PFI_Filter_Select_Bits(pfi_channel
, filter
);
4692 ni_writel(dev
, bits
, M_Offset_PFI_Filter
);
4696 static int ni_pfi_insn_config(struct comedi_device
*dev
,
4697 struct comedi_subdevice
*s
,
4698 struct comedi_insn
*insn
,
4701 struct ni_private
*devpriv
= dev
->private;
4707 chan
= CR_CHAN(insn
->chanspec
);
4711 ni_set_bits(dev
, IO_Bidirection_Pin_Register
, 1 << chan
, 1);
4714 ni_set_bits(dev
, IO_Bidirection_Pin_Register
, 1 << chan
, 0);
4716 case INSN_CONFIG_DIO_QUERY
:
4718 (devpriv
->io_bidirection_pin_reg
& (1 << chan
)) ?
4719 COMEDI_OUTPUT
: COMEDI_INPUT
;
4722 case INSN_CONFIG_SET_ROUTING
:
4723 return ni_set_pfi_routing(dev
, chan
, data
[1]);
4725 case INSN_CONFIG_GET_ROUTING
:
4726 data
[1] = ni_get_pfi_routing(dev
, chan
);
4728 case INSN_CONFIG_FILTER
:
4729 return ni_config_filter(dev
, chan
, data
[1]);
4737 static int ni_pfi_insn_bits(struct comedi_device
*dev
,
4738 struct comedi_subdevice
*s
,
4739 struct comedi_insn
*insn
,
4742 struct ni_private
*devpriv
= dev
->private;
4744 if (!devpriv
->is_m_series
)
4747 if (comedi_dio_update_state(s
, data
))
4748 ni_writew(dev
, s
->state
, M_Offset_PFI_DO
);
4750 data
[1] = ni_readw(dev
, M_Offset_PFI_DI
);
4755 static int cs5529_wait_for_idle(struct comedi_device
*dev
)
4757 unsigned short status
;
4758 const int timeout
= HZ
;
4761 for (i
= 0; i
< timeout
; i
++) {
4762 status
= ni_ao_win_inw(dev
, CAL_ADC_Status_67xx
);
4763 if ((status
& CSS_ADC_BUSY
) == 0)
4765 set_current_state(TASK_INTERRUPTIBLE
);
4766 if (schedule_timeout(1))
4769 /* printk("looped %i times waiting for idle\n", i); */
4771 printk("%s: %s: timeout\n", __FILE__
, __func__
);
4777 static void cs5529_command(struct comedi_device
*dev
, unsigned short value
)
4779 static const int timeout
= 100;
4782 ni_ao_win_outw(dev
, value
, CAL_ADC_Command_67xx
);
4783 /* give time for command to start being serially clocked into cs5529.
4784 * this insures that the CSS_ADC_BUSY bit will get properly
4785 * set before we exit this function.
4787 for (i
= 0; i
< timeout
; i
++) {
4788 if ((ni_ao_win_inw(dev
, CAL_ADC_Status_67xx
) & CSS_ADC_BUSY
))
4792 /* printk("looped %i times writing command to cs5529\n", i); */
4794 comedi_error(dev
, "possible problem - never saw adc go busy?");
4797 static int cs5529_do_conversion(struct comedi_device
*dev
,
4798 unsigned short *data
)
4801 unsigned short status
;
4803 cs5529_command(dev
, CSCMD_COMMAND
| CSCMD_SINGLE_CONVERSION
);
4804 retval
= cs5529_wait_for_idle(dev
);
4807 "timeout or signal in cs5529_do_conversion()");
4810 status
= ni_ao_win_inw(dev
, CAL_ADC_Status_67xx
);
4811 if (status
& CSS_OSC_DETECT
) {
4813 ("ni_mio_common: cs5529 conversion error, status CSS_OSC_DETECT\n");
4816 if (status
& CSS_OVERRANGE
) {
4818 ("ni_mio_common: cs5529 conversion error, overrange (ignoring)\n");
4821 *data
= ni_ao_win_inw(dev
, CAL_ADC_Data_67xx
);
4822 /* cs5529 returns 16 bit signed data in bipolar mode */
4828 static int cs5529_ai_insn_read(struct comedi_device
*dev
,
4829 struct comedi_subdevice
*s
,
4830 struct comedi_insn
*insn
,
4834 unsigned short sample
;
4835 unsigned int channel_select
;
4836 const unsigned int INTERNAL_REF
= 0x1000;
4838 /* Set calibration adc source. Docs lie, reference select bits 8 to 11
4839 * do nothing. bit 12 seems to chooses internal reference voltage, bit
4840 * 13 causes the adc input to go overrange (maybe reads external reference?) */
4841 if (insn
->chanspec
& CR_ALT_SOURCE
)
4842 channel_select
= INTERNAL_REF
;
4844 channel_select
= CR_CHAN(insn
->chanspec
);
4845 ni_ao_win_outw(dev
, channel_select
, AO_Calibration_Channel_Select_67xx
);
4847 for (n
= 0; n
< insn
->n
; n
++) {
4848 retval
= cs5529_do_conversion(dev
, &sample
);
4856 static void cs5529_config_write(struct comedi_device
*dev
, unsigned int value
,
4857 unsigned int reg_select_bits
)
4859 ni_ao_win_outw(dev
, ((value
>> 16) & 0xff),
4860 CAL_ADC_Config_Data_High_Word_67xx
);
4861 ni_ao_win_outw(dev
, (value
& 0xffff),
4862 CAL_ADC_Config_Data_Low_Word_67xx
);
4863 reg_select_bits
&= CSCMD_REGISTER_SELECT_MASK
;
4864 cs5529_command(dev
, CSCMD_COMMAND
| reg_select_bits
);
4865 if (cs5529_wait_for_idle(dev
))
4866 comedi_error(dev
, "time or signal in cs5529_config_write()");
4869 static int init_cs5529(struct comedi_device
*dev
)
4871 unsigned int config_bits
=
4872 CSCFG_PORT_MODE
| CSCFG_WORD_RATE_2180_CYCLES
;
4875 /* do self-calibration */
4876 cs5529_config_write(dev
, config_bits
| CSCFG_SELF_CAL_OFFSET_GAIN
,
4877 CSCMD_CONFIG_REGISTER
);
4878 /* need to force a conversion for calibration to run */
4879 cs5529_do_conversion(dev
, NULL
);
4881 /* force gain calibration to 1 */
4882 cs5529_config_write(dev
, 0x400000, CSCMD_GAIN_REGISTER
);
4883 cs5529_config_write(dev
, config_bits
| CSCFG_SELF_CAL_OFFSET
,
4884 CSCMD_CONFIG_REGISTER
);
4885 if (cs5529_wait_for_idle(dev
))
4886 comedi_error(dev
, "timeout or signal in init_cs5529()\n");
4892 * Find best multiplier/divider to try and get the PLL running at 80 MHz
4893 * given an arbitrary frequency input clock.
4895 static int ni_mseries_get_pll_parameters(unsigned reference_period_ns
,
4896 unsigned *freq_divider
,
4897 unsigned *freq_multiplier
,
4898 unsigned *actual_period_ns
)
4901 unsigned best_div
= 1;
4902 static const unsigned max_div
= 0x10;
4904 unsigned best_mult
= 1;
4905 static const unsigned max_mult
= 0x100;
4906 static const unsigned pico_per_nano
= 1000;
4908 const unsigned reference_picosec
= reference_period_ns
* pico_per_nano
;
4909 /* m-series wants the phased-locked loop to output 80MHz, which is divided by 4 to
4910 * 20 MHz for most timing clocks */
4911 static const unsigned target_picosec
= 12500;
4912 static const unsigned fudge_factor_80_to_20Mhz
= 4;
4913 int best_period_picosec
= 0;
4914 for (div
= 1; div
<= max_div
; ++div
) {
4915 for (mult
= 1; mult
<= max_mult
; ++mult
) {
4916 unsigned new_period_ps
=
4917 (reference_picosec
* div
) / mult
;
4918 if (abs(new_period_ps
- target_picosec
) <
4919 abs(best_period_picosec
- target_picosec
)) {
4920 best_period_picosec
= new_period_ps
;
4926 if (best_period_picosec
== 0) {
4927 printk("%s: bug, failed to find pll parameters\n", __func__
);
4930 *freq_divider
= best_div
;
4931 *freq_multiplier
= best_mult
;
4933 (best_period_picosec
* fudge_factor_80_to_20Mhz
+
4934 (pico_per_nano
/ 2)) / pico_per_nano
;
4938 static int ni_mseries_set_pll_master_clock(struct comedi_device
*dev
,
4939 unsigned source
, unsigned period_ns
)
4941 struct ni_private
*devpriv
= dev
->private;
4942 static const unsigned min_period_ns
= 50;
4943 static const unsigned max_period_ns
= 1000;
4944 static const unsigned timeout
= 1000;
4945 unsigned pll_control_bits
;
4946 unsigned freq_divider
;
4947 unsigned freq_multiplier
;
4951 if (source
== NI_MIO_PLL_PXI10_CLOCK
)
4953 /* these limits are somewhat arbitrary, but NI advertises 1 to 20MHz range so we'll use that */
4954 if (period_ns
< min_period_ns
|| period_ns
> max_period_ns
) {
4956 ("%s: you must specify an input clock frequency between %i and %i nanosec "
4957 "for the phased-lock loop.\n", __func__
,
4958 min_period_ns
, max_period_ns
);
4961 devpriv
->rtsi_trig_direction_reg
&= ~Use_RTSI_Clock_Bit
;
4962 ni_stc_writew(dev
, devpriv
->rtsi_trig_direction_reg
,
4963 RTSI_Trig_Direction_Register
);
4965 MSeries_PLL_Enable_Bit
| MSeries_PLL_VCO_Mode_75_150MHz_Bits
;
4966 devpriv
->clock_and_fout2
|=
4967 MSeries_Timebase1_Select_Bit
| MSeries_Timebase3_Select_Bit
;
4968 devpriv
->clock_and_fout2
&= ~MSeries_PLL_In_Source_Select_Mask
;
4970 case NI_MIO_PLL_PXI_STAR_TRIGGER_CLOCK
:
4971 devpriv
->clock_and_fout2
|=
4972 MSeries_PLL_In_Source_Select_Star_Trigger_Bits
;
4973 retval
= ni_mseries_get_pll_parameters(period_ns
, &freq_divider
,
4975 &devpriv
->clock_ns
);
4979 case NI_MIO_PLL_PXI10_CLOCK
:
4980 /* pxi clock is 10MHz */
4981 devpriv
->clock_and_fout2
|=
4982 MSeries_PLL_In_Source_Select_PXI_Clock10
;
4983 retval
= ni_mseries_get_pll_parameters(period_ns
, &freq_divider
,
4985 &devpriv
->clock_ns
);
4991 unsigned rtsi_channel
;
4992 static const unsigned max_rtsi_channel
= 7;
4993 for (rtsi_channel
= 0; rtsi_channel
<= max_rtsi_channel
;
4996 NI_MIO_PLL_RTSI_CLOCK(rtsi_channel
)) {
4997 devpriv
->clock_and_fout2
|=
4998 MSeries_PLL_In_Source_Select_RTSI_Bits
5003 if (rtsi_channel
> max_rtsi_channel
)
5005 retval
= ni_mseries_get_pll_parameters(period_ns
,
5015 ni_writew(dev
, devpriv
->clock_and_fout2
, M_Offset_Clock_and_Fout2
);
5017 MSeries_PLL_Divisor_Bits(freq_divider
) |
5018 MSeries_PLL_Multiplier_Bits(freq_multiplier
);
5020 /* printk("using divider=%i, multiplier=%i for PLL. pll_control_bits = 0x%x\n",
5021 * freq_divider, freq_multiplier, pll_control_bits); */
5022 /* printk("clock_ns=%d\n", devpriv->clock_ns); */
5023 ni_writew(dev
, pll_control_bits
, M_Offset_PLL_Control
);
5024 devpriv
->clock_source
= source
;
5025 /* it seems to typically take a few hundred microseconds for PLL to lock */
5026 for (i
= 0; i
< timeout
; ++i
) {
5027 if (ni_readw(dev
, M_Offset_PLL_Status
) & MSeries_PLL_Locked_Bit
)
5033 ("%s: timed out waiting for PLL to lock to reference clock source %i with period %i ns.\n",
5034 __func__
, source
, period_ns
);
5040 static int ni_set_master_clock(struct comedi_device
*dev
,
5041 unsigned source
, unsigned period_ns
)
5043 struct ni_private
*devpriv
= dev
->private;
5045 if (source
== NI_MIO_INTERNAL_CLOCK
) {
5046 devpriv
->rtsi_trig_direction_reg
&= ~Use_RTSI_Clock_Bit
;
5047 ni_stc_writew(dev
, devpriv
->rtsi_trig_direction_reg
,
5048 RTSI_Trig_Direction_Register
);
5049 devpriv
->clock_ns
= TIMEBASE_1_NS
;
5050 if (devpriv
->is_m_series
) {
5051 devpriv
->clock_and_fout2
&=
5052 ~(MSeries_Timebase1_Select_Bit
|
5053 MSeries_Timebase3_Select_Bit
);
5054 ni_writew(dev
, devpriv
->clock_and_fout2
,
5055 M_Offset_Clock_and_Fout2
);
5056 ni_writew(dev
, 0, M_Offset_PLL_Control
);
5058 devpriv
->clock_source
= source
;
5060 if (devpriv
->is_m_series
) {
5061 return ni_mseries_set_pll_master_clock(dev
, source
,
5064 if (source
== NI_MIO_RTSI_CLOCK
) {
5065 devpriv
->rtsi_trig_direction_reg
|=
5068 devpriv
->rtsi_trig_direction_reg
,
5069 RTSI_Trig_Direction_Register
);
5070 if (period_ns
== 0) {
5072 ("%s: we don't handle an unspecified clock period correctly yet, returning error.\n",
5076 devpriv
->clock_ns
= period_ns
;
5078 devpriv
->clock_source
= source
;
5086 static unsigned num_configurable_rtsi_channels(struct comedi_device
*dev
)
5088 struct ni_private
*devpriv
= dev
->private;
5090 if (devpriv
->is_m_series
)
5096 static int ni_valid_rtsi_output_source(struct comedi_device
*dev
,
5097 unsigned chan
, unsigned source
)
5099 struct ni_private
*devpriv
= dev
->private;
5101 if (chan
>= num_configurable_rtsi_channels(dev
)) {
5102 if (chan
== old_RTSI_clock_channel
) {
5103 if (source
== NI_RTSI_OUTPUT_RTSI_OSC
)
5107 ("%s: invalid source for channel=%i, channel %i is always the RTSI clock for pre-m-series boards.\n",
5108 __func__
, chan
, old_RTSI_clock_channel
);
5115 case NI_RTSI_OUTPUT_ADR_START1
:
5116 case NI_RTSI_OUTPUT_ADR_START2
:
5117 case NI_RTSI_OUTPUT_SCLKG
:
5118 case NI_RTSI_OUTPUT_DACUPDN
:
5119 case NI_RTSI_OUTPUT_DA_START1
:
5120 case NI_RTSI_OUTPUT_G_SRC0
:
5121 case NI_RTSI_OUTPUT_G_GATE0
:
5122 case NI_RTSI_OUTPUT_RGOUT0
:
5123 case NI_RTSI_OUTPUT_RTSI_BRD_0
:
5126 case NI_RTSI_OUTPUT_RTSI_OSC
:
5127 if (devpriv
->is_m_series
)
5138 static int ni_set_rtsi_routing(struct comedi_device
*dev
,
5139 unsigned chan
, unsigned source
)
5141 struct ni_private
*devpriv
= dev
->private;
5143 if (ni_valid_rtsi_output_source(dev
, chan
, source
) == 0)
5146 devpriv
->rtsi_trig_a_output_reg
&= ~RTSI_Trig_Output_Mask(chan
);
5147 devpriv
->rtsi_trig_a_output_reg
|=
5148 RTSI_Trig_Output_Bits(chan
, source
);
5149 ni_stc_writew(dev
, devpriv
->rtsi_trig_a_output_reg
,
5150 RTSI_Trig_A_Output_Register
);
5151 } else if (chan
< 8) {
5152 devpriv
->rtsi_trig_b_output_reg
&= ~RTSI_Trig_Output_Mask(chan
);
5153 devpriv
->rtsi_trig_b_output_reg
|=
5154 RTSI_Trig_Output_Bits(chan
, source
);
5155 ni_stc_writew(dev
, devpriv
->rtsi_trig_b_output_reg
,
5156 RTSI_Trig_B_Output_Register
);
5161 static unsigned ni_get_rtsi_routing(struct comedi_device
*dev
, unsigned chan
)
5163 struct ni_private
*devpriv
= dev
->private;
5166 return RTSI_Trig_Output_Source(chan
,
5167 devpriv
->rtsi_trig_a_output_reg
);
5168 } else if (chan
< num_configurable_rtsi_channels(dev
)) {
5169 return RTSI_Trig_Output_Source(chan
,
5170 devpriv
->rtsi_trig_b_output_reg
);
5172 if (chan
== old_RTSI_clock_channel
)
5173 return NI_RTSI_OUTPUT_RTSI_OSC
;
5174 printk("%s: bug! should never get here?\n", __func__
);
5179 static int ni_rtsi_insn_config(struct comedi_device
*dev
,
5180 struct comedi_subdevice
*s
,
5181 struct comedi_insn
*insn
,
5184 struct ni_private
*devpriv
= dev
->private;
5185 unsigned int chan
= CR_CHAN(insn
->chanspec
);
5188 case INSN_CONFIG_DIO_OUTPUT
:
5189 if (chan
< num_configurable_rtsi_channels(dev
)) {
5190 devpriv
->rtsi_trig_direction_reg
|=
5191 RTSI_Output_Bit(chan
, devpriv
->is_m_series
);
5192 } else if (chan
== old_RTSI_clock_channel
) {
5193 devpriv
->rtsi_trig_direction_reg
|=
5194 Drive_RTSI_Clock_Bit
;
5196 ni_stc_writew(dev
, devpriv
->rtsi_trig_direction_reg
,
5197 RTSI_Trig_Direction_Register
);
5199 case INSN_CONFIG_DIO_INPUT
:
5200 if (chan
< num_configurable_rtsi_channels(dev
)) {
5201 devpriv
->rtsi_trig_direction_reg
&=
5202 ~RTSI_Output_Bit(chan
, devpriv
->is_m_series
);
5203 } else if (chan
== old_RTSI_clock_channel
) {
5204 devpriv
->rtsi_trig_direction_reg
&=
5205 ~Drive_RTSI_Clock_Bit
;
5207 ni_stc_writew(dev
, devpriv
->rtsi_trig_direction_reg
,
5208 RTSI_Trig_Direction_Register
);
5210 case INSN_CONFIG_DIO_QUERY
:
5211 if (chan
< num_configurable_rtsi_channels(dev
)) {
5213 (devpriv
->rtsi_trig_direction_reg
&
5214 RTSI_Output_Bit(chan
, devpriv
->is_m_series
))
5215 ? INSN_CONFIG_DIO_OUTPUT
5216 : INSN_CONFIG_DIO_INPUT
;
5217 } else if (chan
== old_RTSI_clock_channel
) {
5219 (devpriv
->rtsi_trig_direction_reg
&
5220 Drive_RTSI_Clock_Bit
)
5221 ? INSN_CONFIG_DIO_OUTPUT
: INSN_CONFIG_DIO_INPUT
;
5225 case INSN_CONFIG_SET_CLOCK_SRC
:
5226 return ni_set_master_clock(dev
, data
[1], data
[2]);
5228 case INSN_CONFIG_GET_CLOCK_SRC
:
5229 data
[1] = devpriv
->clock_source
;
5230 data
[2] = devpriv
->clock_ns
;
5233 case INSN_CONFIG_SET_ROUTING
:
5234 return ni_set_rtsi_routing(dev
, chan
, data
[1]);
5236 case INSN_CONFIG_GET_ROUTING
:
5237 data
[1] = ni_get_rtsi_routing(dev
, chan
);
5247 static int ni_rtsi_insn_bits(struct comedi_device
*dev
,
5248 struct comedi_subdevice
*s
,
5249 struct comedi_insn
*insn
,
5257 static void ni_rtsi_init(struct comedi_device
*dev
)
5259 struct ni_private
*devpriv
= dev
->private;
5261 /* Initialises the RTSI bus signal switch to a default state */
5263 /* Set clock mode to internal */
5264 devpriv
->clock_and_fout2
= MSeries_RTSI_10MHz_Bit
;
5265 if (ni_set_master_clock(dev
, NI_MIO_INTERNAL_CLOCK
, 0) < 0)
5266 printk("ni_set_master_clock failed, bug?");
5267 /* default internal lines routing to RTSI bus lines */
5268 devpriv
->rtsi_trig_a_output_reg
=
5269 RTSI_Trig_Output_Bits(0,
5270 NI_RTSI_OUTPUT_ADR_START1
) |
5271 RTSI_Trig_Output_Bits(1,
5272 NI_RTSI_OUTPUT_ADR_START2
) |
5273 RTSI_Trig_Output_Bits(2,
5274 NI_RTSI_OUTPUT_SCLKG
) |
5275 RTSI_Trig_Output_Bits(3, NI_RTSI_OUTPUT_DACUPDN
);
5276 ni_stc_writew(dev
, devpriv
->rtsi_trig_a_output_reg
,
5277 RTSI_Trig_A_Output_Register
);
5278 devpriv
->rtsi_trig_b_output_reg
=
5279 RTSI_Trig_Output_Bits(4,
5280 NI_RTSI_OUTPUT_DA_START1
) |
5281 RTSI_Trig_Output_Bits(5,
5282 NI_RTSI_OUTPUT_G_SRC0
) |
5283 RTSI_Trig_Output_Bits(6, NI_RTSI_OUTPUT_G_GATE0
);
5284 if (devpriv
->is_m_series
)
5285 devpriv
->rtsi_trig_b_output_reg
|=
5286 RTSI_Trig_Output_Bits(7, NI_RTSI_OUTPUT_RTSI_OSC
);
5287 ni_stc_writew(dev
, devpriv
->rtsi_trig_b_output_reg
,
5288 RTSI_Trig_B_Output_Register
);
5291 * Sets the source and direction of the 4 on board lines
5292 * ni_stc_writew(dev, 0x0000, RTSI_Board_Register);
5297 static int ni_gpct_cmd(struct comedi_device
*dev
, struct comedi_subdevice
*s
)
5299 struct ni_gpct
*counter
= s
->private;
5302 retval
= ni_request_gpct_mite_channel(dev
, counter
->counter_index
,
5306 "no dma channel available for use by counter");
5309 ni_tio_acknowledge_and_confirm(counter
, NULL
, NULL
, NULL
, NULL
);
5310 ni_e_series_enable_second_irq(dev
, counter
->counter_index
, 1);
5312 return ni_tio_cmd(dev
, s
);
5315 static int ni_gpct_cancel(struct comedi_device
*dev
, struct comedi_subdevice
*s
)
5317 struct ni_gpct
*counter
= s
->private;
5320 retval
= ni_tio_cancel(counter
);
5321 ni_e_series_enable_second_irq(dev
, counter
->counter_index
, 0);
5322 ni_release_gpct_mite_channel(dev
, counter
->counter_index
);
5329 * Read the GPCTs current value.
5331 static int GPCT_G_Watch(struct comedi_device
*dev
, int chan
)
5333 unsigned int hi1
, hi2
, lo
;
5335 devpriv
->gpct_command
[chan
] &= ~G_Save_Trace
;
5336 ni_stc_writew(dev
, devpriv
->gpct_command
[chan
],
5337 G_Command_Register(chan
));
5339 devpriv
->gpct_command
[chan
] |= G_Save_Trace
;
5340 ni_stc_writew(dev
, devpriv
->gpct_command
[chan
],
5341 G_Command_Register(chan
));
5343 /* This procedure is used because the two registers cannot
5344 * be read atomically. */
5346 hi1
= ni_stc_readw(dev
, G_Save_Register_High(chan
));
5347 lo
= ni_stc_readw(dev
, G_Save_Register_Low(chan
));
5348 hi2
= ni_stc_readw(dev
, G_Save_Register_High(chan
));
5349 } while (hi1
!= hi2
);
5351 return (hi1
<< 16) | lo
;
5354 static void GPCT_Reset(struct comedi_device
*dev
, int chan
)
5356 int temp_ack_reg
= 0;
5358 /* printk("GPCT_Reset..."); */
5359 devpriv
->gpct_cur_operation
[chan
] = GPCT_RESET
;
5363 ni_stc_writew(dev
, G0_Reset
, Joint_Reset_Register
);
5364 ni_set_bits(dev
, Interrupt_A_Enable_Register
,
5365 G0_TC_Interrupt_Enable
, 0);
5366 ni_set_bits(dev
, Interrupt_A_Enable_Register
,
5367 G0_Gate_Interrupt_Enable
, 0);
5368 temp_ack_reg
|= G0_Gate_Error_Confirm
;
5369 temp_ack_reg
|= G0_TC_Error_Confirm
;
5370 temp_ack_reg
|= G0_TC_Interrupt_Ack
;
5371 temp_ack_reg
|= G0_Gate_Interrupt_Ack
;
5372 ni_stc_writew(dev
, temp_ack_reg
, Interrupt_A_Ack_Register
);
5374 /* problem...this interferes with the other ctr... */
5375 devpriv
->an_trig_etc_reg
|= GPFO_0_Output_Enable
;
5376 ni_stc_writew(dev
, devpriv
->an_trig_etc_reg
,
5377 Analog_Trigger_Etc_Register
);
5380 ni_stc_writew(dev
, G1_Reset
, Joint_Reset_Register
);
5381 ni_set_bits(dev
, Interrupt_B_Enable_Register
,
5382 G1_TC_Interrupt_Enable
, 0);
5383 ni_set_bits(dev
, Interrupt_B_Enable_Register
,
5384 G0_Gate_Interrupt_Enable
, 0);
5385 temp_ack_reg
|= G1_Gate_Error_Confirm
;
5386 temp_ack_reg
|= G1_TC_Error_Confirm
;
5387 temp_ack_reg
|= G1_TC_Interrupt_Ack
;
5388 temp_ack_reg
|= G1_Gate_Interrupt_Ack
;
5389 ni_stc_writew(dev
, temp_ack_reg
, Interrupt_B_Ack_Register
);
5391 devpriv
->an_trig_etc_reg
|= GPFO_1_Output_Enable
;
5392 ni_stc_writew(dev
, devpriv
->an_trig_etc_reg
,
5393 Analog_Trigger_Etc_Register
);
5397 devpriv
->gpct_mode
[chan
] = 0;
5398 devpriv
->gpct_input_select
[chan
] = 0;
5399 devpriv
->gpct_command
[chan
] = 0;
5401 devpriv
->gpct_command
[chan
] |= G_Synchronized_Gate
;
5403 ni_stc_writew(dev
, devpriv
->gpct_mode
[chan
], G_Mode_Register(chan
));
5404 ni_stc_writew(dev
, devpriv
->gpct_input_select
[chan
],
5405 G_Input_Select_Register(chan
));
5406 ni_stc_writew(dev
, 0, G_Autoincrement_Register(chan
));
5408 /* printk("exit GPCT_Reset\n"); */
5412 static irqreturn_t
ni_E_interrupt(int irq
, void *d
)
5414 struct comedi_device
*dev
= d
;
5415 unsigned short a_status
;
5416 unsigned short b_status
;
5417 unsigned int ai_mite_status
= 0;
5418 unsigned int ao_mite_status
= 0;
5419 unsigned long flags
;
5421 struct ni_private
*devpriv
= dev
->private;
5422 struct mite_struct
*mite
= devpriv
->mite
;
5427 smp_mb(); /* make sure dev->attached is checked before handler does anything else. */
5429 /* lock to avoid race with comedi_poll */
5430 spin_lock_irqsave(&dev
->spinlock
, flags
);
5431 a_status
= ni_stc_readw(dev
, AI_Status_1_Register
);
5432 b_status
= ni_stc_readw(dev
, AO_Status_1_Register
);
5435 struct ni_private
*devpriv
= dev
->private;
5436 unsigned long flags_too
;
5438 spin_lock_irqsave(&devpriv
->mite_channel_lock
, flags_too
);
5439 if (devpriv
->ai_mite_chan
) {
5440 ai_mite_status
= mite_get_status(devpriv
->ai_mite_chan
);
5441 if (ai_mite_status
& CHSR_LINKC
)
5443 devpriv
->mite
->mite_io_addr
+
5445 ai_mite_chan
->channel
));
5447 if (devpriv
->ao_mite_chan
) {
5448 ao_mite_status
= mite_get_status(devpriv
->ao_mite_chan
);
5449 if (ao_mite_status
& CHSR_LINKC
)
5451 mite
->mite_io_addr
+
5453 ao_mite_chan
->channel
));
5455 spin_unlock_irqrestore(&devpriv
->mite_channel_lock
, flags_too
);
5458 ack_a_interrupt(dev
, a_status
);
5459 ack_b_interrupt(dev
, b_status
);
5460 if ((a_status
& Interrupt_A_St
) || (ai_mite_status
& CHSR_INT
))
5461 handle_a_interrupt(dev
, a_status
, ai_mite_status
);
5462 if ((b_status
& Interrupt_B_St
) || (ao_mite_status
& CHSR_INT
))
5463 handle_b_interrupt(dev
, b_status
, ao_mite_status
);
5464 handle_gpct_interrupt(dev
, 0);
5465 handle_gpct_interrupt(dev
, 1);
5466 handle_cdio_interrupt(dev
);
5468 spin_unlock_irqrestore(&dev
->spinlock
, flags
);
5472 static int ni_alloc_private(struct comedi_device
*dev
)
5474 struct ni_private
*devpriv
;
5476 devpriv
= comedi_alloc_devpriv(dev
, sizeof(*devpriv
));
5480 spin_lock_init(&devpriv
->window_lock
);
5481 spin_lock_init(&devpriv
->soft_reg_copy_lock
);
5482 spin_lock_init(&devpriv
->mite_channel_lock
);
5487 static int ni_E_init(struct comedi_device
*dev
,
5488 unsigned interrupt_pin
, unsigned irq_polarity
)
5490 const struct ni_board_struct
*board
= comedi_board(dev
);
5491 struct ni_private
*devpriv
= dev
->private;
5492 struct comedi_subdevice
*s
;
5496 if (board
->n_aochan
> MAX_N_AO_CHAN
) {
5497 printk("bug! n_aochan > MAX_N_AO_CHAN\n");
5501 /* initialize clock dividers */
5502 devpriv
->clock_and_fout
= Slow_Internal_Time_Divide_By_2
|
5503 Slow_Internal_Timebase
|
5504 Clock_To_Board_Divide_By_2
|
5506 if (!devpriv
->is_6xxx
) {
5507 /* BEAM is this needed for PCI-6143 ?? */
5508 devpriv
->clock_and_fout
|= (AI_Output_Divide_By_2
|
5509 AO_Output_Divide_By_2
);
5511 ni_stc_writew(dev
, devpriv
->clock_and_fout
, Clock_and_FOUT_Register
);
5513 ret
= comedi_alloc_subdevices(dev
, NI_NUM_SUBDEVICES
);
5517 /* Analog Input subdevice */
5518 s
= &dev
->subdevices
[NI_AI_SUBDEV
];
5519 if (board
->n_adchan
) {
5520 s
->type
= COMEDI_SUBD_AI
;
5521 s
->subdev_flags
= SDF_READABLE
| SDF_DIFF
| SDF_DITHER
;
5522 if (!devpriv
->is_611x
)
5523 s
->subdev_flags
|= SDF_GROUND
| SDF_COMMON
| SDF_OTHER
;
5524 if (board
->ai_maxdata
> 0xffff)
5525 s
->subdev_flags
|= SDF_LSAMPL
;
5526 if (devpriv
->is_m_series
)
5527 s
->subdev_flags
|= SDF_SOFT_CALIBRATED
;
5528 s
->n_chan
= board
->n_adchan
;
5529 s
->maxdata
= board
->ai_maxdata
;
5530 s
->range_table
= ni_range_lkup
[board
->gainlkup
];
5531 s
->insn_read
= ni_ai_insn_read
;
5532 s
->insn_config
= ni_ai_insn_config
;
5534 dev
->read_subdev
= s
;
5535 s
->subdev_flags
|= SDF_CMD_READ
;
5536 s
->len_chanlist
= 512;
5537 s
->do_cmdtest
= ni_ai_cmdtest
;
5538 s
->do_cmd
= ni_ai_cmd
;
5539 s
->cancel
= ni_ai_reset
;
5540 s
->poll
= ni_ai_poll
;
5541 s
->munge
= ni_ai_munge
;
5544 s
->async_dma_dir
= DMA_FROM_DEVICE
;
5547 /* reset the analog input configuration */
5548 ni_ai_reset(dev
, s
);
5550 s
->type
= COMEDI_SUBD_UNUSED
;
5553 /* Analog Output subdevice */
5554 s
= &dev
->subdevices
[NI_AO_SUBDEV
];
5555 if (board
->n_aochan
) {
5556 s
->type
= COMEDI_SUBD_AO
;
5557 s
->subdev_flags
= SDF_WRITABLE
| SDF_DEGLITCH
| SDF_GROUND
;
5558 if (devpriv
->is_m_series
)
5559 s
->subdev_flags
|= SDF_SOFT_CALIBRATED
;
5560 s
->n_chan
= board
->n_aochan
;
5561 s
->maxdata
= board
->ao_maxdata
;
5562 s
->range_table
= board
->ao_range_table
;
5563 s
->insn_read
= ni_ao_insn_read
;
5564 s
->insn_write
= ni_ao_insn_write
;
5565 s
->insn_config
= ni_ao_insn_config
;
5568 * Along with the IRQ we need either a FIFO or DMA for
5569 * async command support.
5571 if (dev
->irq
&& (board
->ao_fifo_depth
|| devpriv
->mite
)) {
5572 dev
->write_subdev
= s
;
5573 s
->subdev_flags
|= SDF_CMD_WRITE
;
5574 s
->len_chanlist
= s
->n_chan
;
5575 s
->do_cmdtest
= ni_ao_cmdtest
;
5576 s
->do_cmd
= ni_ao_cmd
;
5577 s
->cancel
= ni_ao_reset
;
5578 if (!devpriv
->is_m_series
)
5579 s
->munge
= ni_ao_munge
;
5582 s
->async_dma_dir
= DMA_TO_DEVICE
;
5585 if (devpriv
->is_67xx
)
5586 init_ao_67xx(dev
, s
);
5588 /* reset the analog output configuration */
5589 ni_ao_reset(dev
, s
);
5591 s
->type
= COMEDI_SUBD_UNUSED
;
5594 /* Digital I/O subdevice */
5595 s
= &dev
->subdevices
[NI_DIO_SUBDEV
];
5596 s
->type
= COMEDI_SUBD_DIO
;
5597 s
->subdev_flags
= SDF_WRITABLE
| SDF_READABLE
;
5598 s
->n_chan
= board
->has_32dio_chan
? 32 : 8;
5600 s
->range_table
= &range_digital
;
5601 if (devpriv
->is_m_series
) {
5602 s
->subdev_flags
|= SDF_LSAMPL
;
5603 s
->insn_bits
= ni_m_series_dio_insn_bits
;
5604 s
->insn_config
= ni_m_series_dio_insn_config
;
5606 s
->subdev_flags
|= SDF_CMD_WRITE
/* | SDF_CMD_READ */;
5607 s
->len_chanlist
= s
->n_chan
;
5608 s
->do_cmdtest
= ni_cdio_cmdtest
;
5609 s
->do_cmd
= ni_cdio_cmd
;
5610 s
->cancel
= ni_cdio_cancel
;
5612 /* M-series boards use DMA */
5613 s
->async_dma_dir
= DMA_BIDIRECTIONAL
;
5616 /* reset DIO and set all channels to inputs */
5617 ni_writel(dev
, CDO_Reset_Bit
| CDI_Reset_Bit
,
5618 M_Offset_CDIO_Command
);
5619 ni_writel(dev
, s
->io_bits
, M_Offset_DIO_Direction
);
5621 s
->insn_bits
= ni_dio_insn_bits
;
5622 s
->insn_config
= ni_dio_insn_config
;
5624 /* set all channels to inputs */
5625 devpriv
->dio_control
= DIO_Pins_Dir(s
->io_bits
);
5626 ni_writew(dev
, devpriv
->dio_control
, DIO_Control_Register
);
5630 s
= &dev
->subdevices
[NI_8255_DIO_SUBDEV
];
5631 if (board
->has_8255
) {
5632 ret
= subdev_8255_init(dev
, s
, ni_8255_callback
,
5633 (unsigned long)dev
);
5637 s
->type
= COMEDI_SUBD_UNUSED
;
5640 /* formerly general purpose counter/timer device, but no longer used */
5641 s
= &dev
->subdevices
[NI_UNUSED_SUBDEV
];
5642 s
->type
= COMEDI_SUBD_UNUSED
;
5644 /* Calibration subdevice */
5645 s
= &dev
->subdevices
[NI_CALIBRATION_SUBDEV
];
5646 s
->type
= COMEDI_SUBD_CALIB
;
5647 s
->subdev_flags
= SDF_INTERNAL
;
5650 if (devpriv
->is_m_series
) {
5651 /* internal PWM output used for AI nonlinearity calibration */
5652 s
->insn_config
= ni_m_series_pwm_config
;
5654 ni_writel(dev
, 0x0, M_Offset_Cal_PWM
);
5655 } else if (devpriv
->is_6143
) {
5656 /* internal PWM output used for AI nonlinearity calibration */
5657 s
->insn_config
= ni_6143_pwm_config
;
5659 s
->subdev_flags
|= SDF_WRITABLE
;
5660 s
->insn_read
= ni_calib_insn_read
;
5661 s
->insn_write
= ni_calib_insn_write
;
5663 /* setup the caldacs and find the real n_chan and maxdata */
5664 caldac_setup(dev
, s
);
5667 /* EEPROM subdevice */
5668 s
= &dev
->subdevices
[NI_EEPROM_SUBDEV
];
5669 s
->type
= COMEDI_SUBD_MEMORY
;
5670 s
->subdev_flags
= SDF_READABLE
| SDF_INTERNAL
;
5672 if (devpriv
->is_m_series
) {
5673 s
->n_chan
= M_SERIES_EEPROM_SIZE
;
5674 s
->insn_read
= ni_m_series_eeprom_insn_read
;
5677 s
->insn_read
= ni_eeprom_insn_read
;
5680 /* Digital I/O (PFI) subdevice */
5681 s
= &dev
->subdevices
[NI_PFI_DIO_SUBDEV
];
5682 s
->type
= COMEDI_SUBD_DIO
;
5683 s
->subdev_flags
= SDF_READABLE
| SDF_WRITABLE
| SDF_INTERNAL
;
5685 if (devpriv
->is_m_series
) {
5687 s
->insn_bits
= ni_pfi_insn_bits
;
5689 ni_writew(dev
, s
->state
, M_Offset_PFI_DO
);
5690 for (i
= 0; i
< NUM_PFI_OUTPUT_SELECT_REGS
; ++i
) {
5691 ni_writew(dev
, devpriv
->pfi_output_select_reg
[i
],
5692 M_Offset_PFI_Output_Select(i
+ 1));
5697 s
->insn_config
= ni_pfi_insn_config
;
5699 ni_set_bits(dev
, IO_Bidirection_Pin_Register
, ~0, 0);
5701 /* cs5529 calibration adc */
5702 s
= &dev
->subdevices
[NI_CS5529_CALIBRATION_SUBDEV
];
5703 if (devpriv
->is_67xx
) {
5704 s
->type
= COMEDI_SUBD_AI
;
5705 s
->subdev_flags
= SDF_READABLE
| SDF_DIFF
| SDF_INTERNAL
;
5706 /* one channel for each analog output channel */
5707 s
->n_chan
= board
->n_aochan
;
5708 s
->maxdata
= (1 << 16) - 1;
5709 s
->range_table
= &range_unknown
; /* XXX */
5710 s
->insn_read
= cs5529_ai_insn_read
;
5711 s
->insn_config
= NULL
;
5714 s
->type
= COMEDI_SUBD_UNUSED
;
5718 s
= &dev
->subdevices
[NI_SERIAL_SUBDEV
];
5719 s
->type
= COMEDI_SUBD_SERIAL
;
5720 s
->subdev_flags
= SDF_READABLE
| SDF_WRITABLE
| SDF_INTERNAL
;
5723 s
->insn_config
= ni_serial_insn_config
;
5724 devpriv
->serial_interval_ns
= 0;
5725 devpriv
->serial_hw_mode
= 0;
5728 s
= &dev
->subdevices
[NI_RTSI_SUBDEV
];
5729 s
->type
= COMEDI_SUBD_DIO
;
5730 s
->subdev_flags
= SDF_READABLE
| SDF_WRITABLE
| SDF_INTERNAL
;
5733 s
->insn_bits
= ni_rtsi_insn_bits
;
5734 s
->insn_config
= ni_rtsi_insn_config
;
5737 /* allocate and initialize the gpct counter device */
5738 devpriv
->counter_dev
= ni_gpct_device_construct(dev
,
5739 ni_gpct_write_register
,
5740 ni_gpct_read_register
,
5741 (devpriv
->is_m_series
)
5742 ? ni_gpct_variant_m_series
5743 : ni_gpct_variant_e_series
,
5745 if (!devpriv
->counter_dev
)
5748 /* Counter (gpct) subdevices */
5749 for (i
= 0; i
< NUM_GPCT
; ++i
) {
5750 struct ni_gpct
*gpct
= &devpriv
->counter_dev
->counters
[i
];
5752 /* setup and initialize the counter */
5753 gpct
->chip_index
= 0;
5754 gpct
->counter_index
= i
;
5755 ni_tio_init_counter(gpct
);
5757 s
= &dev
->subdevices
[NI_GPCT_SUBDEV(i
)];
5758 s
->type
= COMEDI_SUBD_COUNTER
;
5759 s
->subdev_flags
= SDF_READABLE
| SDF_WRITABLE
| SDF_LSAMPL
;
5761 s
->maxdata
= (devpriv
->is_m_series
) ? 0xffffffff
5763 s
->insn_read
= ni_tio_insn_read
;
5764 s
->insn_write
= ni_tio_insn_read
;
5765 s
->insn_config
= ni_tio_insn_config
;
5767 if (dev
->irq
&& devpriv
->mite
) {
5768 s
->subdev_flags
|= SDF_CMD_READ
/* | SDF_CMD_WRITE */;
5769 s
->len_chanlist
= 1;
5770 s
->do_cmdtest
= ni_tio_cmdtest
;
5771 s
->do_cmd
= ni_gpct_cmd
;
5772 s
->cancel
= ni_gpct_cancel
;
5774 s
->async_dma_dir
= DMA_BIDIRECTIONAL
;
5780 /* Frequency output subdevice */
5781 s
= &dev
->subdevices
[NI_FREQ_OUT_SUBDEV
];
5782 s
->type
= COMEDI_SUBD_COUNTER
;
5783 s
->subdev_flags
= SDF_READABLE
| SDF_WRITABLE
;
5786 s
->insn_read
= ni_freq_out_insn_read
;
5787 s
->insn_write
= ni_freq_out_insn_write
;
5788 s
->insn_config
= ni_freq_out_insn_config
;
5792 (irq_polarity
? Interrupt_Output_Polarity
: 0) |
5793 (Interrupt_Output_On_3_Pins
& 0) |
5794 Interrupt_A_Enable
| Interrupt_B_Enable
|
5795 Interrupt_A_Output_Select(interrupt_pin
) |
5796 Interrupt_B_Output_Select(interrupt_pin
),
5797 Interrupt_Control_Register
);
5801 ni_writeb(dev
, devpriv
->ai_ao_select_reg
, AI_AO_Select
);
5802 ni_writeb(dev
, devpriv
->g0_g1_select_reg
, G0_G1_Select
);
5804 if (devpriv
->is_6xxx
) {
5805 ni_writeb(dev
, 0, Magic_611x
);
5806 } else if (devpriv
->is_m_series
) {
5808 for (channel
= 0; channel
< board
->n_aochan
; ++channel
) {
5810 M_Offset_AO_Waveform_Order(channel
));
5812 M_Offset_AO_Reference_Attenuation(channel
));
5814 ni_writeb(dev
, 0x0, M_Offset_AO_Calibration
);
5820 static void mio_common_detach(struct comedi_device
*dev
)
5822 struct ni_private
*devpriv
= dev
->private;
5825 if (devpriv
->counter_dev
)
5826 ni_gpct_device_destroy(devpriv
->counter_dev
);