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.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 This file is meant to be included by another file, e.g.,
27 ni_atmio.c or ni_pcimio.c.
29 Interrupt support originally added by Truxton Fulton
32 References (from ftp://ftp.natinst.com/support/manuals):
34 340747b.pdf AT-MIO E series Register Level Programmer Manual
35 341079b.pdf PCI E Series RLPM
36 340934b.pdf DAQ-STC reference manual
37 67xx and 611x registers (from ftp://ftp.ni.com/support/daq/mhddk/documentation/)
40 Other possibly relevant info:
42 320517c.pdf User manual (obsolete)
43 320517f.pdf User manual (new)
45 320906c.pdf maximum signal ratings
47 321791a.pdf discontinuation of at-mio-16e-10 rev. c
48 321808a.pdf about at-mio-16e-10 rev P
49 321837a.pdf discontinuation of at-mio-16de-10 rev d
50 321838a.pdf about at-mio-16de-10 rev N
54 - the interrupt routine needs to be cleaned up
56 2006-02-07: S-Series PCI-6143: Support has been added but is not
57 fully tested as yet. Terry Barnaby, BEAM Ltd.
60 /* #define DEBUG_INTERRUPT */
61 /* #define DEBUG_STATUS_A */
62 /* #define DEBUG_STATUS_B */
64 #include <linux/interrupt.h>
65 #include <linux/sched.h>
68 #include "comedi_fc.h"
71 #define MDPRINTK(format, args...)
75 #define NI_TIMEOUT 1000
76 static const unsigned old_RTSI_clock_channel
= 7;
78 /* Note: this table must match the ai_gain_* definitions */
79 static const short ni_gainlkup
[][16] = {
80 [ai_gain_16
] = {0, 1, 2, 3, 4, 5, 6, 7,
81 0x100, 0x101, 0x102, 0x103, 0x104, 0x105, 0x106, 0x107},
82 [ai_gain_8
] = {1, 2, 4, 7, 0x101, 0x102, 0x104, 0x107},
83 [ai_gain_14
] = {1, 2, 3, 4, 5, 6, 7,
84 0x101, 0x102, 0x103, 0x104, 0x105, 0x106, 0x107},
85 [ai_gain_4
] = {0, 1, 4, 7},
86 [ai_gain_611x
] = {0x00a, 0x00b, 0x001, 0x002,
87 0x003, 0x004, 0x005, 0x006},
88 [ai_gain_622x
] = {0, 1, 4, 5},
89 [ai_gain_628x
] = {1, 2, 3, 4, 5, 6, 7},
90 [ai_gain_6143
] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
93 static const struct comedi_lrange range_ni_E_ai
= { 16, {
113 static const struct comedi_lrange range_ni_E_ai_limited
= { 8, {
126 static const struct comedi_lrange range_ni_E_ai_limited14
= { 14, {
151 static const struct comedi_lrange range_ni_E_ai_bipolar4
= { 4, {
161 static const struct comedi_lrange range_ni_E_ai_611x
= { 8, {
173 static const struct comedi_lrange range_ni_M_ai_622x
= { 4, {
181 static const struct comedi_lrange range_ni_M_ai_628x
= { 7, {
192 static const struct comedi_lrange range_ni_S_ai_6143
= { 1, {
197 static const struct comedi_lrange range_ni_E_ao_ext
= { 4, {
205 static const struct comedi_lrange
*const ni_range_lkup
[] = {
206 [ai_gain_16
] = &range_ni_E_ai
,
207 [ai_gain_8
] = &range_ni_E_ai_limited
,
208 [ai_gain_14
] = &range_ni_E_ai_limited14
,
209 [ai_gain_4
] = &range_ni_E_ai_bipolar4
,
210 [ai_gain_611x
] = &range_ni_E_ai_611x
,
211 [ai_gain_622x
] = &range_ni_M_ai_622x
,
212 [ai_gain_628x
] = &range_ni_M_ai_628x
,
213 [ai_gain_6143
] = &range_ni_S_ai_6143
216 static int ni_dio_insn_config(struct comedi_device
*dev
,
217 struct comedi_subdevice
*s
,
218 struct comedi_insn
*insn
, unsigned int *data
);
219 static int ni_dio_insn_bits(struct comedi_device
*dev
,
220 struct comedi_subdevice
*s
,
221 struct comedi_insn
*insn
, unsigned int *data
);
222 static int ni_cdio_cmdtest(struct comedi_device
*dev
,
223 struct comedi_subdevice
*s
, struct comedi_cmd
*cmd
);
224 static int ni_cdio_cmd(struct comedi_device
*dev
, struct comedi_subdevice
*s
);
225 static int ni_cdio_cancel(struct comedi_device
*dev
,
226 struct comedi_subdevice
*s
);
227 static void handle_cdio_interrupt(struct comedi_device
*dev
);
228 static int ni_cdo_inttrig(struct comedi_device
*dev
, struct comedi_subdevice
*s
,
229 unsigned int trignum
);
231 static int ni_serial_insn_config(struct comedi_device
*dev
,
232 struct comedi_subdevice
*s
,
233 struct comedi_insn
*insn
, unsigned int *data
);
234 static int ni_serial_hw_readwrite8(struct comedi_device
*dev
,
235 struct comedi_subdevice
*s
,
236 unsigned char data_out
,
237 unsigned char *data_in
);
238 static int ni_serial_sw_readwrite8(struct comedi_device
*dev
,
239 struct comedi_subdevice
*s
,
240 unsigned char data_out
,
241 unsigned char *data_in
);
243 static int ni_calib_insn_read(struct comedi_device
*dev
,
244 struct comedi_subdevice
*s
,
245 struct comedi_insn
*insn
, unsigned int *data
);
246 static int ni_calib_insn_write(struct comedi_device
*dev
,
247 struct comedi_subdevice
*s
,
248 struct comedi_insn
*insn
, unsigned int *data
);
250 static int ni_eeprom_insn_read(struct comedi_device
*dev
,
251 struct comedi_subdevice
*s
,
252 struct comedi_insn
*insn
, unsigned int *data
);
253 static int ni_m_series_eeprom_insn_read(struct comedi_device
*dev
,
254 struct comedi_subdevice
*s
,
255 struct comedi_insn
*insn
,
258 static int ni_pfi_insn_bits(struct comedi_device
*dev
,
259 struct comedi_subdevice
*s
,
260 struct comedi_insn
*insn
, unsigned int *data
);
261 static int ni_pfi_insn_config(struct comedi_device
*dev
,
262 struct comedi_subdevice
*s
,
263 struct comedi_insn
*insn
, unsigned int *data
);
264 static unsigned ni_old_get_pfi_routing(struct comedi_device
*dev
,
267 static void ni_rtsi_init(struct comedi_device
*dev
);
268 static int ni_rtsi_insn_bits(struct comedi_device
*dev
,
269 struct comedi_subdevice
*s
,
270 struct comedi_insn
*insn
, unsigned int *data
);
271 static int ni_rtsi_insn_config(struct comedi_device
*dev
,
272 struct comedi_subdevice
*s
,
273 struct comedi_insn
*insn
, unsigned int *data
);
275 static void caldac_setup(struct comedi_device
*dev
, struct comedi_subdevice
*s
);
276 static int ni_read_eeprom(struct comedi_device
*dev
, int addr
);
278 #ifdef DEBUG_STATUS_A
279 static void ni_mio_print_status_a(int status
);
281 #define ni_mio_print_status_a(a)
283 #ifdef DEBUG_STATUS_B
284 static void ni_mio_print_status_b(int status
);
286 #define ni_mio_print_status_b(a)
289 static int ni_ai_reset(struct comedi_device
*dev
, struct comedi_subdevice
*s
);
291 static void ni_handle_fifo_half_full(struct comedi_device
*dev
);
292 static int ni_ao_fifo_half_empty(struct comedi_device
*dev
,
293 struct comedi_subdevice
*s
);
295 static void ni_handle_fifo_dregs(struct comedi_device
*dev
);
296 static int ni_ai_inttrig(struct comedi_device
*dev
, struct comedi_subdevice
*s
,
297 unsigned int trignum
);
298 static void ni_load_channelgain_list(struct comedi_device
*dev
,
299 unsigned int n_chan
, unsigned int *list
);
300 static void shutdown_ai_command(struct comedi_device
*dev
);
302 static int ni_ao_inttrig(struct comedi_device
*dev
, struct comedi_subdevice
*s
,
303 unsigned int trignum
);
305 static int ni_ao_reset(struct comedi_device
*dev
, struct comedi_subdevice
*s
);
307 static int ni_8255_callback(int dir
, int port
, int data
, unsigned long arg
);
309 static int ni_gpct_insn_write(struct comedi_device
*dev
,
310 struct comedi_subdevice
*s
,
311 struct comedi_insn
*insn
, unsigned int *data
);
312 static int ni_gpct_insn_read(struct comedi_device
*dev
,
313 struct comedi_subdevice
*s
,
314 struct comedi_insn
*insn
, unsigned int *data
);
315 static int ni_gpct_insn_config(struct comedi_device
*dev
,
316 struct comedi_subdevice
*s
,
317 struct comedi_insn
*insn
, unsigned int *data
);
318 static int ni_gpct_cmd(struct comedi_device
*dev
, struct comedi_subdevice
*s
);
319 static int ni_gpct_cmdtest(struct comedi_device
*dev
,
320 struct comedi_subdevice
*s
, struct comedi_cmd
*cmd
);
321 static int ni_gpct_cancel(struct comedi_device
*dev
,
322 struct comedi_subdevice
*s
);
323 static void handle_gpct_interrupt(struct comedi_device
*dev
,
324 unsigned short counter_index
);
326 static int init_cs5529(struct comedi_device
*dev
);
327 static int cs5529_do_conversion(struct comedi_device
*dev
,
328 unsigned short *data
);
329 static int cs5529_ai_insn_read(struct comedi_device
*dev
,
330 struct comedi_subdevice
*s
,
331 struct comedi_insn
*insn
, unsigned int *data
);
332 #ifdef NI_CS5529_DEBUG
333 static unsigned int cs5529_config_read(struct comedi_device
*dev
,
334 unsigned int reg_select_bits
);
336 static void cs5529_config_write(struct comedi_device
*dev
, unsigned int value
,
337 unsigned int reg_select_bits
);
339 static int ni_m_series_pwm_config(struct comedi_device
*dev
,
340 struct comedi_subdevice
*s
,
341 struct comedi_insn
*insn
, unsigned int *data
);
342 static int ni_6143_pwm_config(struct comedi_device
*dev
,
343 struct comedi_subdevice
*s
,
344 struct comedi_insn
*insn
, unsigned int *data
);
346 static int ni_set_master_clock(struct comedi_device
*dev
, unsigned source
,
348 static void ack_a_interrupt(struct comedi_device
*dev
, unsigned short a_status
);
349 static void ack_b_interrupt(struct comedi_device
*dev
, unsigned short b_status
);
353 AIMODE_HALF_FULL
= 1,
358 enum ni_common_subdevices
{
364 NI_CALIBRATION_SUBDEV
,
367 NI_CS5529_CALIBRATION_SUBDEV
,
375 static inline unsigned NI_GPCT_SUBDEV(unsigned counter_index
)
377 switch (counter_index
) {
379 return NI_GPCT0_SUBDEV
;
382 return NI_GPCT1_SUBDEV
;
388 return NI_GPCT0_SUBDEV
;
391 enum timebase_nanoseconds
{
393 TIMEBASE_2_NS
= 10000
396 #define SERIAL_DISABLED 0
397 #define SERIAL_600NS 600
398 #define SERIAL_1_2US 1200
399 #define SERIAL_10US 10000
401 static const int num_adc_stages_611x
= 3;
403 static void handle_a_interrupt(struct comedi_device
*dev
, unsigned short status
,
404 unsigned ai_mite_status
);
405 static void handle_b_interrupt(struct comedi_device
*dev
, unsigned short status
,
406 unsigned ao_mite_status
);
407 static void get_last_sample_611x(struct comedi_device
*dev
);
408 static void get_last_sample_6143(struct comedi_device
*dev
);
410 static inline void ni_set_bitfield(struct comedi_device
*dev
, int reg
,
411 unsigned bit_mask
, unsigned bit_values
)
413 struct ni_private
*devpriv
= dev
->private;
416 spin_lock_irqsave(&devpriv
->soft_reg_copy_lock
, flags
);
418 case Interrupt_A_Enable_Register
:
419 devpriv
->int_a_enable_reg
&= ~bit_mask
;
420 devpriv
->int_a_enable_reg
|= bit_values
& bit_mask
;
421 devpriv
->stc_writew(dev
, devpriv
->int_a_enable_reg
,
422 Interrupt_A_Enable_Register
);
424 case Interrupt_B_Enable_Register
:
425 devpriv
->int_b_enable_reg
&= ~bit_mask
;
426 devpriv
->int_b_enable_reg
|= bit_values
& bit_mask
;
427 devpriv
->stc_writew(dev
, devpriv
->int_b_enable_reg
,
428 Interrupt_B_Enable_Register
);
430 case IO_Bidirection_Pin_Register
:
431 devpriv
->io_bidirection_pin_reg
&= ~bit_mask
;
432 devpriv
->io_bidirection_pin_reg
|= bit_values
& bit_mask
;
433 devpriv
->stc_writew(dev
, devpriv
->io_bidirection_pin_reg
,
434 IO_Bidirection_Pin_Register
);
437 devpriv
->ai_ao_select_reg
&= ~bit_mask
;
438 devpriv
->ai_ao_select_reg
|= bit_values
& bit_mask
;
439 ni_writeb(devpriv
->ai_ao_select_reg
, AI_AO_Select
);
442 devpriv
->g0_g1_select_reg
&= ~bit_mask
;
443 devpriv
->g0_g1_select_reg
|= bit_values
& bit_mask
;
444 ni_writeb(devpriv
->g0_g1_select_reg
, G0_G1_Select
);
447 printk("Warning %s() called with invalid register\n", __func__
);
448 printk("reg is %d\n", reg
);
452 spin_unlock_irqrestore(&devpriv
->soft_reg_copy_lock
, flags
);
456 static int ni_ai_drain_dma(struct comedi_device
*dev
);
458 /* DMA channel setup */
460 /* negative channel means no channel */
461 static inline void ni_set_ai_dma_channel(struct comedi_device
*dev
, int channel
)
467 (ni_stc_dma_channel_select_bitfield(channel
) <<
468 AI_DMA_Select_Shift
) & AI_DMA_Select_Mask
;
472 ni_set_bitfield(dev
, AI_AO_Select
, AI_DMA_Select_Mask
, bitfield
);
475 /* negative channel means no channel */
476 static inline void ni_set_ao_dma_channel(struct comedi_device
*dev
, int channel
)
482 (ni_stc_dma_channel_select_bitfield(channel
) <<
483 AO_DMA_Select_Shift
) & AO_DMA_Select_Mask
;
487 ni_set_bitfield(dev
, AI_AO_Select
, AO_DMA_Select_Mask
, bitfield
);
490 /* negative mite_channel means no channel */
491 static inline void ni_set_gpct_dma_channel(struct comedi_device
*dev
,
497 if (mite_channel
>= 0) {
498 bitfield
= GPCT_DMA_Select_Bits(gpct_index
, mite_channel
);
502 ni_set_bitfield(dev
, G0_G1_Select
, GPCT_DMA_Select_Mask(gpct_index
),
506 /* negative mite_channel means no channel */
507 static inline void ni_set_cdo_dma_channel(struct comedi_device
*dev
,
510 struct ni_private
*devpriv
= dev
->private;
513 spin_lock_irqsave(&devpriv
->soft_reg_copy_lock
, flags
);
514 devpriv
->cdio_dma_select_reg
&= ~CDO_DMA_Select_Mask
;
515 if (mite_channel
>= 0) {
516 /*XXX just guessing ni_stc_dma_channel_select_bitfield() returns the right bits,
517 under the assumption the cdio dma selection works just like ai/ao/gpct.
518 Definitely works for dma channels 0 and 1. */
519 devpriv
->cdio_dma_select_reg
|=
520 (ni_stc_dma_channel_select_bitfield(mite_channel
) <<
521 CDO_DMA_Select_Shift
) & CDO_DMA_Select_Mask
;
523 ni_writeb(devpriv
->cdio_dma_select_reg
, M_Offset_CDIO_DMA_Select
);
525 spin_unlock_irqrestore(&devpriv
->soft_reg_copy_lock
, flags
);
528 static int ni_request_ai_mite_channel(struct comedi_device
*dev
)
530 struct ni_private
*devpriv
= dev
->private;
533 spin_lock_irqsave(&devpriv
->mite_channel_lock
, flags
);
534 BUG_ON(devpriv
->ai_mite_chan
);
535 devpriv
->ai_mite_chan
=
536 mite_request_channel(devpriv
->mite
, devpriv
->ai_mite_ring
);
537 if (devpriv
->ai_mite_chan
== NULL
) {
538 spin_unlock_irqrestore(&devpriv
->mite_channel_lock
, flags
);
540 "failed to reserve mite dma channel for analog input.");
543 devpriv
->ai_mite_chan
->dir
= COMEDI_INPUT
;
544 ni_set_ai_dma_channel(dev
, devpriv
->ai_mite_chan
->channel
);
545 spin_unlock_irqrestore(&devpriv
->mite_channel_lock
, flags
);
549 static int ni_request_ao_mite_channel(struct comedi_device
*dev
)
551 struct ni_private
*devpriv
= dev
->private;
554 spin_lock_irqsave(&devpriv
->mite_channel_lock
, flags
);
555 BUG_ON(devpriv
->ao_mite_chan
);
556 devpriv
->ao_mite_chan
=
557 mite_request_channel(devpriv
->mite
, devpriv
->ao_mite_ring
);
558 if (devpriv
->ao_mite_chan
== NULL
) {
559 spin_unlock_irqrestore(&devpriv
->mite_channel_lock
, flags
);
561 "failed to reserve mite dma channel for analog outut.");
564 devpriv
->ao_mite_chan
->dir
= COMEDI_OUTPUT
;
565 ni_set_ao_dma_channel(dev
, devpriv
->ao_mite_chan
->channel
);
566 spin_unlock_irqrestore(&devpriv
->mite_channel_lock
, flags
);
570 static int ni_request_gpct_mite_channel(struct comedi_device
*dev
,
572 enum comedi_io_direction direction
)
574 struct ni_private
*devpriv
= dev
->private;
576 struct mite_channel
*mite_chan
;
578 BUG_ON(gpct_index
>= NUM_GPCT
);
579 spin_lock_irqsave(&devpriv
->mite_channel_lock
, flags
);
580 BUG_ON(devpriv
->counter_dev
->counters
[gpct_index
].mite_chan
);
582 mite_request_channel(devpriv
->mite
,
583 devpriv
->gpct_mite_ring
[gpct_index
]);
584 if (mite_chan
== NULL
) {
585 spin_unlock_irqrestore(&devpriv
->mite_channel_lock
, flags
);
587 "failed to reserve mite dma channel for counter.");
590 mite_chan
->dir
= direction
;
591 ni_tio_set_mite_channel(&devpriv
->counter_dev
->counters
[gpct_index
],
593 ni_set_gpct_dma_channel(dev
, gpct_index
, mite_chan
->channel
);
594 spin_unlock_irqrestore(&devpriv
->mite_channel_lock
, flags
);
600 static int ni_request_cdo_mite_channel(struct comedi_device
*dev
)
603 struct ni_private
*devpriv
= dev
->private;
606 spin_lock_irqsave(&devpriv
->mite_channel_lock
, flags
);
607 BUG_ON(devpriv
->cdo_mite_chan
);
608 devpriv
->cdo_mite_chan
=
609 mite_request_channel(devpriv
->mite
, devpriv
->cdo_mite_ring
);
610 if (devpriv
->cdo_mite_chan
== NULL
) {
611 spin_unlock_irqrestore(&devpriv
->mite_channel_lock
, flags
);
613 "failed to reserve mite dma channel for correlated digital outut.");
616 devpriv
->cdo_mite_chan
->dir
= COMEDI_OUTPUT
;
617 ni_set_cdo_dma_channel(dev
, devpriv
->cdo_mite_chan
->channel
);
618 spin_unlock_irqrestore(&devpriv
->mite_channel_lock
, flags
);
623 static void ni_release_ai_mite_channel(struct comedi_device
*dev
)
626 struct ni_private
*devpriv
= dev
->private;
629 spin_lock_irqsave(&devpriv
->mite_channel_lock
, flags
);
630 if (devpriv
->ai_mite_chan
) {
631 ni_set_ai_dma_channel(dev
, -1);
632 mite_release_channel(devpriv
->ai_mite_chan
);
633 devpriv
->ai_mite_chan
= NULL
;
635 spin_unlock_irqrestore(&devpriv
->mite_channel_lock
, flags
);
639 static void ni_release_ao_mite_channel(struct comedi_device
*dev
)
642 struct ni_private
*devpriv
= dev
->private;
645 spin_lock_irqsave(&devpriv
->mite_channel_lock
, flags
);
646 if (devpriv
->ao_mite_chan
) {
647 ni_set_ao_dma_channel(dev
, -1);
648 mite_release_channel(devpriv
->ao_mite_chan
);
649 devpriv
->ao_mite_chan
= NULL
;
651 spin_unlock_irqrestore(&devpriv
->mite_channel_lock
, flags
);
656 static void ni_release_gpct_mite_channel(struct comedi_device
*dev
,
659 struct ni_private
*devpriv
= dev
->private;
662 BUG_ON(gpct_index
>= NUM_GPCT
);
663 spin_lock_irqsave(&devpriv
->mite_channel_lock
, flags
);
664 if (devpriv
->counter_dev
->counters
[gpct_index
].mite_chan
) {
665 struct mite_channel
*mite_chan
=
666 devpriv
->counter_dev
->counters
[gpct_index
].mite_chan
;
668 ni_set_gpct_dma_channel(dev
, gpct_index
, -1);
669 ni_tio_set_mite_channel(&devpriv
->
670 counter_dev
->counters
[gpct_index
],
672 mite_release_channel(mite_chan
);
674 spin_unlock_irqrestore(&devpriv
->mite_channel_lock
, flags
);
678 static void ni_release_cdo_mite_channel(struct comedi_device
*dev
)
681 struct ni_private
*devpriv
= dev
->private;
684 spin_lock_irqsave(&devpriv
->mite_channel_lock
, flags
);
685 if (devpriv
->cdo_mite_chan
) {
686 ni_set_cdo_dma_channel(dev
, -1);
687 mite_release_channel(devpriv
->cdo_mite_chan
);
688 devpriv
->cdo_mite_chan
= NULL
;
690 spin_unlock_irqrestore(&devpriv
->mite_channel_lock
, flags
);
694 /* e-series boards use the second irq signals to generate dma requests for their counters */
696 static void ni_e_series_enable_second_irq(struct comedi_device
*dev
,
697 unsigned gpct_index
, short enable
)
699 struct ni_private
*devpriv
= dev
->private;
701 if (boardtype
.reg_type
& ni_reg_m_series_mask
)
703 switch (gpct_index
) {
706 devpriv
->stc_writew(dev
, G0_Gate_Second_Irq_Enable
,
707 Second_IRQ_A_Enable_Register
);
709 devpriv
->stc_writew(dev
, 0,
710 Second_IRQ_A_Enable_Register
);
715 devpriv
->stc_writew(dev
, G1_Gate_Second_Irq_Enable
,
716 Second_IRQ_B_Enable_Register
);
718 devpriv
->stc_writew(dev
, 0,
719 Second_IRQ_B_Enable_Register
);
729 static void ni_clear_ai_fifo(struct comedi_device
*dev
)
731 struct ni_private
*devpriv
= dev
->private;
733 if (boardtype
.reg_type
== ni_reg_6143
) {
734 /* Flush the 6143 data FIFO */
735 ni_writel(0x10, AIFIFO_Control_6143
); /* Flush fifo */
736 ni_writel(0x00, AIFIFO_Control_6143
); /* Flush fifo */
737 while (ni_readl(AIFIFO_Status_6143
) & 0x10) ; /* Wait for complete */
739 devpriv
->stc_writew(dev
, 1, ADC_FIFO_Clear
);
740 if (boardtype
.reg_type
== ni_reg_625x
) {
741 ni_writeb(0, M_Offset_Static_AI_Control(0));
742 ni_writeb(1, M_Offset_Static_AI_Control(0));
744 /* the NI example code does 3 convert pulses for 625x boards,
745 but that appears to be wrong in practice. */
746 devpriv
->stc_writew(dev
, AI_CONVERT_Pulse
,
747 AI_Command_1_Register
);
748 devpriv
->stc_writew(dev
, AI_CONVERT_Pulse
,
749 AI_Command_1_Register
);
750 devpriv
->stc_writew(dev
, AI_CONVERT_Pulse
,
751 AI_Command_1_Register
);
757 static void win_out2(struct comedi_device
*dev
, uint32_t data
, int reg
)
759 struct ni_private
*devpriv
= dev
->private;
761 devpriv
->stc_writew(dev
, data
>> 16, reg
);
762 devpriv
->stc_writew(dev
, data
& 0xffff, reg
+ 1);
765 static uint32_t win_in2(struct comedi_device
*dev
, int reg
)
767 struct ni_private
*devpriv
= dev
->private;
770 bits
= devpriv
->stc_readw(dev
, reg
) << 16;
771 bits
|= devpriv
->stc_readw(dev
, reg
+ 1);
775 #define ao_win_out(data, addr) ni_ao_win_outw(dev, data, addr)
776 static inline void ni_ao_win_outw(struct comedi_device
*dev
, uint16_t data
,
779 struct ni_private
*devpriv
= dev
->private;
782 spin_lock_irqsave(&devpriv
->window_lock
, flags
);
783 ni_writew(addr
, AO_Window_Address_611x
);
784 ni_writew(data
, AO_Window_Data_611x
);
785 spin_unlock_irqrestore(&devpriv
->window_lock
, flags
);
788 static inline void ni_ao_win_outl(struct comedi_device
*dev
, uint32_t data
,
791 struct ni_private
*devpriv
= dev
->private;
794 spin_lock_irqsave(&devpriv
->window_lock
, flags
);
795 ni_writew(addr
, AO_Window_Address_611x
);
796 ni_writel(data
, AO_Window_Data_611x
);
797 spin_unlock_irqrestore(&devpriv
->window_lock
, flags
);
800 static inline unsigned short ni_ao_win_inw(struct comedi_device
*dev
, int addr
)
802 struct ni_private
*devpriv
= dev
->private;
806 spin_lock_irqsave(&devpriv
->window_lock
, flags
);
807 ni_writew(addr
, AO_Window_Address_611x
);
808 data
= ni_readw(AO_Window_Data_611x
);
809 spin_unlock_irqrestore(&devpriv
->window_lock
, flags
);
813 /* ni_set_bits( ) allows different parts of the ni_mio_common driver to
814 * share registers (such as Interrupt_A_Register) without interfering with
817 * NOTE: the switch/case statements are optimized out for a constant argument
818 * so this is actually quite fast--- If you must wrap another function around this
819 * make it inline to avoid a large speed penalty.
821 * value should only be 1 or 0.
823 static inline void ni_set_bits(struct comedi_device
*dev
, int reg
,
824 unsigned bits
, unsigned value
)
832 ni_set_bitfield(dev
, reg
, bits
, bit_values
);
835 static irqreturn_t
ni_E_interrupt(int irq
, void *d
)
837 struct comedi_device
*dev
= d
;
838 struct ni_private
*devpriv
= dev
->private;
839 unsigned short a_status
;
840 unsigned short b_status
;
841 unsigned int ai_mite_status
= 0;
842 unsigned int ao_mite_status
= 0;
845 struct mite_struct
*mite
= devpriv
->mite
;
848 if (dev
->attached
== 0)
850 smp_mb(); /* make sure dev->attached is checked before handler does anything else. */
852 /* lock to avoid race with comedi_poll */
853 spin_lock_irqsave(&dev
->spinlock
, flags
);
854 a_status
= devpriv
->stc_readw(dev
, AI_Status_1_Register
);
855 b_status
= devpriv
->stc_readw(dev
, AO_Status_1_Register
);
858 unsigned long flags_too
;
860 spin_lock_irqsave(&devpriv
->mite_channel_lock
, flags_too
);
861 if (devpriv
->ai_mite_chan
) {
862 ai_mite_status
= mite_get_status(devpriv
->ai_mite_chan
);
863 if (ai_mite_status
& CHSR_LINKC
)
865 devpriv
->mite
->mite_io_addr
+
867 ai_mite_chan
->channel
));
869 if (devpriv
->ao_mite_chan
) {
870 ao_mite_status
= mite_get_status(devpriv
->ao_mite_chan
);
871 if (ao_mite_status
& CHSR_LINKC
)
875 ao_mite_chan
->channel
));
877 spin_unlock_irqrestore(&devpriv
->mite_channel_lock
, flags_too
);
880 ack_a_interrupt(dev
, a_status
);
881 ack_b_interrupt(dev
, b_status
);
882 if ((a_status
& Interrupt_A_St
) || (ai_mite_status
& CHSR_INT
))
883 handle_a_interrupt(dev
, a_status
, ai_mite_status
);
884 if ((b_status
& Interrupt_B_St
) || (ao_mite_status
& CHSR_INT
))
885 handle_b_interrupt(dev
, b_status
, ao_mite_status
);
886 handle_gpct_interrupt(dev
, 0);
887 handle_gpct_interrupt(dev
, 1);
888 handle_cdio_interrupt(dev
);
890 spin_unlock_irqrestore(&dev
->spinlock
, flags
);
895 static void ni_sync_ai_dma(struct comedi_device
*dev
)
897 struct ni_private
*devpriv
= dev
->private;
898 struct comedi_subdevice
*s
= &dev
->subdevices
[NI_AI_SUBDEV
];
901 spin_lock_irqsave(&devpriv
->mite_channel_lock
, flags
);
902 if (devpriv
->ai_mite_chan
)
903 mite_sync_input_dma(devpriv
->ai_mite_chan
, s
->async
);
904 spin_unlock_irqrestore(&devpriv
->mite_channel_lock
, flags
);
907 static void mite_handle_b_linkc(struct mite_struct
*mite
,
908 struct comedi_device
*dev
)
910 struct ni_private
*devpriv
= dev
->private;
911 struct comedi_subdevice
*s
= &dev
->subdevices
[NI_AO_SUBDEV
];
914 spin_lock_irqsave(&devpriv
->mite_channel_lock
, flags
);
915 if (devpriv
->ao_mite_chan
) {
916 mite_sync_output_dma(devpriv
->ao_mite_chan
, s
->async
);
918 spin_unlock_irqrestore(&devpriv
->mite_channel_lock
, flags
);
921 static int ni_ao_wait_for_dma_load(struct comedi_device
*dev
)
923 struct ni_private
*devpriv
= dev
->private;
924 static const int timeout
= 10000;
926 for (i
= 0; i
< timeout
; i
++) {
927 unsigned short b_status
;
929 b_status
= devpriv
->stc_readw(dev
, AO_Status_1_Register
);
930 if (b_status
& AO_FIFO_Half_Full_St
)
932 /* if we poll too often, the pci bus activity seems
933 to slow the dma transfer down */
937 comedi_error(dev
, "timed out waiting for dma load");
944 static void ni_handle_eos(struct comedi_device
*dev
, struct comedi_subdevice
*s
)
946 struct ni_private
*devpriv
= dev
->private;
948 if (devpriv
->aimode
== AIMODE_SCAN
) {
950 static const int timeout
= 10;
953 for (i
= 0; i
< timeout
; i
++) {
955 if ((s
->async
->events
& COMEDI_CB_EOS
))
960 ni_handle_fifo_dregs(dev
);
961 s
->async
->events
|= COMEDI_CB_EOS
;
964 /* handle special case of single scan using AI_End_On_End_Of_Scan */
965 if ((devpriv
->ai_cmd2
& AI_End_On_End_Of_Scan
)) {
966 shutdown_ai_command(dev
);
970 static void shutdown_ai_command(struct comedi_device
*dev
)
972 struct comedi_subdevice
*s
= &dev
->subdevices
[NI_AI_SUBDEV
];
975 ni_ai_drain_dma(dev
);
977 ni_handle_fifo_dregs(dev
);
978 get_last_sample_611x(dev
);
979 get_last_sample_6143(dev
);
981 s
->async
->events
|= COMEDI_CB_EOA
;
984 static void ni_event(struct comedi_device
*dev
, struct comedi_subdevice
*s
)
987 async
->events
& (COMEDI_CB_ERROR
| COMEDI_CB_OVERFLOW
|
989 switch (s
- dev
->subdevices
) {
996 case NI_GPCT0_SUBDEV
:
997 case NI_GPCT1_SUBDEV
:
998 ni_gpct_cancel(dev
, s
);
1001 ni_cdio_cancel(dev
, s
);
1007 comedi_event(dev
, s
);
1010 static void handle_gpct_interrupt(struct comedi_device
*dev
,
1011 unsigned short counter_index
)
1014 struct ni_private
*devpriv
= dev
->private;
1015 struct comedi_subdevice
*s
;
1017 s
= &dev
->subdevices
[NI_GPCT_SUBDEV(counter_index
)];
1019 ni_tio_handle_interrupt(&devpriv
->counter_dev
->counters
[counter_index
],
1021 if (s
->async
->events
)
1026 static void ack_a_interrupt(struct comedi_device
*dev
, unsigned short a_status
)
1028 struct ni_private
*devpriv
= dev
->private;
1029 unsigned short ack
= 0;
1031 if (a_status
& AI_SC_TC_St
) {
1032 ack
|= AI_SC_TC_Interrupt_Ack
;
1034 if (a_status
& AI_START1_St
) {
1035 ack
|= AI_START1_Interrupt_Ack
;
1037 if (a_status
& AI_START_St
) {
1038 ack
|= AI_START_Interrupt_Ack
;
1040 if (a_status
& AI_STOP_St
) {
1041 /* not sure why we used to ack the START here also, instead of doing it independently. Frank Hess 2007-07-06 */
1042 ack
|= AI_STOP_Interrupt_Ack
/*| AI_START_Interrupt_Ack */ ;
1045 devpriv
->stc_writew(dev
, ack
, Interrupt_A_Ack_Register
);
1048 static void handle_a_interrupt(struct comedi_device
*dev
, unsigned short status
,
1049 unsigned ai_mite_status
)
1051 struct ni_private
*devpriv
= dev
->private;
1052 struct comedi_subdevice
*s
= &dev
->subdevices
[NI_AI_SUBDEV
];
1054 /* 67xx boards don't have ai subdevice, but their gpct0 might generate an a interrupt */
1055 if (s
->type
== COMEDI_SUBD_UNUSED
)
1058 #ifdef DEBUG_INTERRUPT
1060 ("ni_mio_common: interrupt: a_status=%04x ai_mite_status=%08x\n",
1061 status
, ai_mite_status
);
1062 ni_mio_print_status_a(status
);
1065 if (ai_mite_status
& CHSR_LINKC
) {
1066 ni_sync_ai_dma(dev
);
1069 if (ai_mite_status
& ~(CHSR_INT
| CHSR_LINKC
| CHSR_DONE
| CHSR_MRDY
|
1070 CHSR_DRDY
| CHSR_DRQ1
| CHSR_DRQ0
| CHSR_ERROR
|
1071 CHSR_SABORT
| CHSR_XFERR
| CHSR_LxERR_mask
)) {
1073 ("unknown mite interrupt, ack! (ai_mite_status=%08x)\n",
1075 /* mite_print_chsr(ai_mite_status); */
1076 s
->async
->events
|= COMEDI_CB_ERROR
| COMEDI_CB_EOA
;
1077 /* disable_irq(dev->irq); */
1081 /* test for all uncommon interrupt events at the same time */
1082 if (status
& (AI_Overrun_St
| AI_Overflow_St
| AI_SC_TC_Error_St
|
1083 AI_SC_TC_St
| AI_START1_St
)) {
1084 if (status
== 0xffff) {
1086 ("ni_mio_common: a_status=0xffff. Card removed?\n");
1087 /* we probably aren't even running a command now,
1088 * so it's a good idea to be careful. */
1089 if (comedi_get_subdevice_runflags(s
) & SRF_RUNNING
) {
1091 COMEDI_CB_ERROR
| COMEDI_CB_EOA
;
1096 if (status
& (AI_Overrun_St
| AI_Overflow_St
|
1097 AI_SC_TC_Error_St
)) {
1098 printk("ni_mio_common: ai error a_status=%04x\n",
1100 ni_mio_print_status_a(status
);
1102 shutdown_ai_command(dev
);
1104 s
->async
->events
|= COMEDI_CB_ERROR
;
1105 if (status
& (AI_Overrun_St
| AI_Overflow_St
))
1106 s
->async
->events
|= COMEDI_CB_OVERFLOW
;
1112 if (status
& AI_SC_TC_St
) {
1113 #ifdef DEBUG_INTERRUPT
1114 printk("ni_mio_common: SC_TC interrupt\n");
1116 if (!devpriv
->ai_continuous
) {
1117 shutdown_ai_command(dev
);
1122 if (status
& AI_FIFO_Half_Full_St
) {
1124 static const int timeout
= 10;
1125 /* pcmcia cards (at least 6036) seem to stop producing interrupts if we
1126 *fail to get the fifo less than half full, so loop to be sure.*/
1127 for (i
= 0; i
< timeout
; ++i
) {
1128 ni_handle_fifo_half_full(dev
);
1129 if ((devpriv
->stc_readw(dev
,
1130 AI_Status_1_Register
) &
1131 AI_FIFO_Half_Full_St
) == 0)
1135 #endif /* !PCIDMA */
1137 if ((status
& AI_STOP_St
)) {
1138 ni_handle_eos(dev
, s
);
1143 #ifdef DEBUG_INTERRUPT
1144 status
= devpriv
->stc_readw(dev
, AI_Status_1_Register
);
1145 if (status
& Interrupt_A_St
) {
1147 ("handle_a_interrupt: didn't clear interrupt? status=0x%x\n",
1153 static void ack_b_interrupt(struct comedi_device
*dev
, unsigned short b_status
)
1155 struct ni_private
*devpriv
= dev
->private;
1156 unsigned short ack
= 0;
1158 if (b_status
& AO_BC_TC_St
) {
1159 ack
|= AO_BC_TC_Interrupt_Ack
;
1161 if (b_status
& AO_Overrun_St
) {
1162 ack
|= AO_Error_Interrupt_Ack
;
1164 if (b_status
& AO_START_St
) {
1165 ack
|= AO_START_Interrupt_Ack
;
1167 if (b_status
& AO_START1_St
) {
1168 ack
|= AO_START1_Interrupt_Ack
;
1170 if (b_status
& AO_UC_TC_St
) {
1171 ack
|= AO_UC_TC_Interrupt_Ack
;
1173 if (b_status
& AO_UI2_TC_St
) {
1174 ack
|= AO_UI2_TC_Interrupt_Ack
;
1176 if (b_status
& AO_UPDATE_St
) {
1177 ack
|= AO_UPDATE_Interrupt_Ack
;
1180 devpriv
->stc_writew(dev
, ack
, Interrupt_B_Ack_Register
);
1183 static void handle_b_interrupt(struct comedi_device
*dev
,
1184 unsigned short b_status
, unsigned ao_mite_status
)
1186 struct ni_private
*devpriv
= dev
->private;
1187 struct comedi_subdevice
*s
= &dev
->subdevices
[NI_AO_SUBDEV
];
1188 /* unsigned short ack=0; */
1190 #ifdef DEBUG_INTERRUPT
1191 printk("ni_mio_common: interrupt: b_status=%04x m1_status=%08x\n",
1192 b_status
, ao_mite_status
);
1193 ni_mio_print_status_b(b_status
);
1197 /* Currently, mite.c requires us to handle LINKC */
1198 if (ao_mite_status
& CHSR_LINKC
) {
1199 mite_handle_b_linkc(devpriv
->mite
, dev
);
1202 if (ao_mite_status
& ~(CHSR_INT
| CHSR_LINKC
| CHSR_DONE
| CHSR_MRDY
|
1203 CHSR_DRDY
| CHSR_DRQ1
| CHSR_DRQ0
| CHSR_ERROR
|
1204 CHSR_SABORT
| CHSR_XFERR
| CHSR_LxERR_mask
)) {
1206 ("unknown mite interrupt, ack! (ao_mite_status=%08x)\n",
1208 /* mite_print_chsr(ao_mite_status); */
1209 s
->async
->events
|= COMEDI_CB_EOA
| COMEDI_CB_ERROR
;
1213 if (b_status
== 0xffff)
1215 if (b_status
& AO_Overrun_St
) {
1217 ("ni_mio_common: AO FIFO underrun status=0x%04x status2=0x%04x\n",
1218 b_status
, devpriv
->stc_readw(dev
, AO_Status_2_Register
));
1219 s
->async
->events
|= COMEDI_CB_OVERFLOW
;
1222 if (b_status
& AO_BC_TC_St
) {
1224 ("ni_mio_common: AO BC_TC status=0x%04x status2=0x%04x\n",
1225 b_status
, devpriv
->stc_readw(dev
, AO_Status_2_Register
));
1226 s
->async
->events
|= COMEDI_CB_EOA
;
1229 if (b_status
& AO_FIFO_Request_St
) {
1232 ret
= ni_ao_fifo_half_empty(dev
, s
);
1234 printk("ni_mio_common: AO buffer underrun\n");
1235 ni_set_bits(dev
, Interrupt_B_Enable_Register
,
1236 AO_FIFO_Interrupt_Enable
|
1237 AO_Error_Interrupt_Enable
, 0);
1238 s
->async
->events
|= COMEDI_CB_OVERFLOW
;
1246 #ifdef DEBUG_STATUS_A
1247 static const char *const status_a_strings
[] = {
1248 "passthru0", "fifo", "G0_gate", "G0_TC",
1249 "stop", "start", "sc_tc", "start1",
1250 "start2", "sc_tc_error", "overflow", "overrun",
1251 "fifo_empty", "fifo_half_full", "fifo_full", "interrupt_a"
1254 static void ni_mio_print_status_a(int status
)
1258 printk("A status:");
1259 for (i
= 15; i
>= 0; i
--) {
1260 if (status
& (1 << i
)) {
1261 printk(" %s", status_a_strings
[i
]);
1268 #ifdef DEBUG_STATUS_B
1269 static const char *const status_b_strings
[] = {
1270 "passthru1", "fifo", "G1_gate", "G1_TC",
1271 "UI2_TC", "UPDATE", "UC_TC", "BC_TC",
1272 "start1", "overrun", "start", "bc_tc_error",
1273 "fifo_empty", "fifo_half_full", "fifo_full", "interrupt_b"
1276 static void ni_mio_print_status_b(int status
)
1280 printk("B status:");
1281 for (i
= 15; i
>= 0; i
--) {
1282 if (status
& (1 << i
)) {
1283 printk(" %s", status_b_strings
[i
]);
1292 static void ni_ao_fifo_load(struct comedi_device
*dev
,
1293 struct comedi_subdevice
*s
, int n
)
1295 struct comedi_async
*async
= s
->async
;
1296 struct comedi_cmd
*cmd
= &async
->cmd
;
1304 chan
= async
->cur_chan
;
1305 for (i
= 0; i
< n
; i
++) {
1306 err
&= comedi_buf_get(async
, &d
);
1310 range
= CR_RANGE(cmd
->chanlist
[chan
]);
1312 if (boardtype
.reg_type
& ni_reg_6xxx_mask
) {
1313 packed_data
= d
& 0xffff;
1314 /* 6711 only has 16 bit wide ao fifo */
1315 if (boardtype
.reg_type
!= ni_reg_6711
) {
1316 err
&= comedi_buf_get(async
, &d
);
1321 packed_data
|= (d
<< 16) & 0xffff0000;
1323 ni_writel(packed_data
, DAC_FIFO_Data_611x
);
1325 ni_writew(d
, DAC_FIFO_Data
);
1328 chan
%= cmd
->chanlist_len
;
1330 async
->cur_chan
= chan
;
1332 async
->events
|= COMEDI_CB_OVERFLOW
;
1337 * There's a small problem if the FIFO gets really low and we
1338 * don't have the data to fill it. Basically, if after we fill
1339 * the FIFO with all the data available, the FIFO is _still_
1340 * less than half full, we never clear the interrupt. If the
1341 * IRQ is in edge mode, we never get another interrupt, because
1342 * this one wasn't cleared. If in level mode, we get flooded
1343 * with interrupts that we can't fulfill, because nothing ever
1344 * gets put into the buffer.
1346 * This kind of situation is recoverable, but it is easier to
1347 * just pretend we had a FIFO underrun, since there is a good
1348 * chance it will happen anyway. This is _not_ the case for
1349 * RT code, as RT code might purposely be running close to the
1350 * metal. Needs to be fixed eventually.
1352 static int ni_ao_fifo_half_empty(struct comedi_device
*dev
,
1353 struct comedi_subdevice
*s
)
1357 n
= comedi_buf_read_n_available(s
->async
);
1359 s
->async
->events
|= COMEDI_CB_OVERFLOW
;
1364 if (n
> boardtype
.ao_fifo_depth
/ 2)
1365 n
= boardtype
.ao_fifo_depth
/ 2;
1367 ni_ao_fifo_load(dev
, s
, n
);
1369 s
->async
->events
|= COMEDI_CB_BLOCK
;
1374 static int ni_ao_prep_fifo(struct comedi_device
*dev
,
1375 struct comedi_subdevice
*s
)
1377 struct ni_private
*devpriv
= dev
->private;
1381 devpriv
->stc_writew(dev
, 1, DAC_FIFO_Clear
);
1382 if (boardtype
.reg_type
& ni_reg_6xxx_mask
)
1383 ni_ao_win_outl(dev
, 0x6, AO_FIFO_Offset_Load_611x
);
1385 /* load some data */
1386 n
= comedi_buf_read_n_available(s
->async
);
1391 if (n
> boardtype
.ao_fifo_depth
)
1392 n
= boardtype
.ao_fifo_depth
;
1394 ni_ao_fifo_load(dev
, s
, n
);
1399 static void ni_ai_fifo_read(struct comedi_device
*dev
,
1400 struct comedi_subdevice
*s
, int n
)
1402 struct ni_private
*devpriv
= dev
->private;
1403 struct comedi_async
*async
= s
->async
;
1406 if (boardtype
.reg_type
== ni_reg_611x
) {
1410 for (i
= 0; i
< n
/ 2; i
++) {
1411 dl
= ni_readl(ADC_FIFO_Data_611x
);
1412 /* This may get the hi/lo data in the wrong order */
1413 data
[0] = (dl
>> 16) & 0xffff;
1414 data
[1] = dl
& 0xffff;
1415 cfc_write_array_to_buffer(s
, data
, sizeof(data
));
1417 /* Check if there's a single sample stuck in the FIFO */
1419 dl
= ni_readl(ADC_FIFO_Data_611x
);
1420 data
[0] = dl
& 0xffff;
1421 cfc_write_to_buffer(s
, data
[0]);
1423 } else if (boardtype
.reg_type
== ni_reg_6143
) {
1427 /* This just reads the FIFO assuming the data is present, no checks on the FIFO status are performed */
1428 for (i
= 0; i
< n
/ 2; i
++) {
1429 dl
= ni_readl(AIFIFO_Data_6143
);
1431 data
[0] = (dl
>> 16) & 0xffff;
1432 data
[1] = dl
& 0xffff;
1433 cfc_write_array_to_buffer(s
, data
, sizeof(data
));
1436 /* Assume there is a single sample stuck in the FIFO */
1437 ni_writel(0x01, AIFIFO_Control_6143
); /* Get stranded sample into FIFO */
1438 dl
= ni_readl(AIFIFO_Data_6143
);
1439 data
[0] = (dl
>> 16) & 0xffff;
1440 cfc_write_to_buffer(s
, data
[0]);
1443 if (n
> sizeof(devpriv
->ai_fifo_buffer
) /
1444 sizeof(devpriv
->ai_fifo_buffer
[0])) {
1445 comedi_error(dev
, "bug! ai_fifo_buffer too small");
1446 async
->events
|= COMEDI_CB_ERROR
;
1449 for (i
= 0; i
< n
; i
++) {
1450 devpriv
->ai_fifo_buffer
[i
] =
1451 ni_readw(ADC_FIFO_Data_Register
);
1453 cfc_write_array_to_buffer(s
, devpriv
->ai_fifo_buffer
,
1455 sizeof(devpriv
->ai_fifo_buffer
[0]));
1459 static void ni_handle_fifo_half_full(struct comedi_device
*dev
)
1462 struct comedi_subdevice
*s
= &dev
->subdevices
[NI_AI_SUBDEV
];
1464 n
= boardtype
.ai_fifo_depth
/ 2;
1466 ni_ai_fifo_read(dev
, s
, n
);
1471 static int ni_ai_drain_dma(struct comedi_device
*dev
)
1473 struct ni_private
*devpriv
= dev
->private;
1475 static const int timeout
= 10000;
1476 unsigned long flags
;
1479 spin_lock_irqsave(&devpriv
->mite_channel_lock
, flags
);
1480 if (devpriv
->ai_mite_chan
) {
1481 for (i
= 0; i
< timeout
; i
++) {
1482 if ((devpriv
->stc_readw(dev
,
1483 AI_Status_1_Register
) &
1485 && mite_bytes_in_transit(devpriv
->ai_mite_chan
) ==
1491 printk("ni_mio_common: wait for dma drain timed out\n");
1493 ("mite_bytes_in_transit=%i, AI_Status1_Register=0x%x\n",
1494 mite_bytes_in_transit(devpriv
->ai_mite_chan
),
1495 devpriv
->stc_readw(dev
, AI_Status_1_Register
));
1499 spin_unlock_irqrestore(&devpriv
->mite_channel_lock
, flags
);
1501 ni_sync_ai_dma(dev
);
1509 static void ni_handle_fifo_dregs(struct comedi_device
*dev
)
1511 struct ni_private
*devpriv
= dev
->private;
1512 struct comedi_subdevice
*s
= &dev
->subdevices
[NI_AI_SUBDEV
];
1518 if (boardtype
.reg_type
== ni_reg_611x
) {
1519 while ((devpriv
->stc_readw(dev
,
1520 AI_Status_1_Register
) &
1521 AI_FIFO_Empty_St
) == 0) {
1522 dl
= ni_readl(ADC_FIFO_Data_611x
);
1524 /* This may get the hi/lo data in the wrong order */
1525 data
[0] = (dl
>> 16);
1526 data
[1] = (dl
& 0xffff);
1527 cfc_write_array_to_buffer(s
, data
, sizeof(data
));
1529 } else if (boardtype
.reg_type
== ni_reg_6143
) {
1531 while (ni_readl(AIFIFO_Status_6143
) & 0x04) {
1532 dl
= ni_readl(AIFIFO_Data_6143
);
1534 /* This may get the hi/lo data in the wrong order */
1535 data
[0] = (dl
>> 16);
1536 data
[1] = (dl
& 0xffff);
1537 cfc_write_array_to_buffer(s
, data
, sizeof(data
));
1540 /* Check if stranded sample is present */
1541 if (ni_readl(AIFIFO_Status_6143
) & 0x01) {
1542 ni_writel(0x01, AIFIFO_Control_6143
); /* Get stranded sample into FIFO */
1543 dl
= ni_readl(AIFIFO_Data_6143
);
1544 data
[0] = (dl
>> 16) & 0xffff;
1545 cfc_write_to_buffer(s
, data
[0]);
1550 devpriv
->stc_readw(dev
,
1551 AI_Status_1_Register
) & AI_FIFO_Empty_St
;
1552 while (fifo_empty
== 0) {
1555 sizeof(devpriv
->ai_fifo_buffer
) /
1556 sizeof(devpriv
->ai_fifo_buffer
[0]); i
++) {
1558 devpriv
->stc_readw(dev
,
1559 AI_Status_1_Register
) &
1563 devpriv
->ai_fifo_buffer
[i
] =
1564 ni_readw(ADC_FIFO_Data_Register
);
1566 cfc_write_array_to_buffer(s
, devpriv
->ai_fifo_buffer
,
1569 ai_fifo_buffer
[0]));
1574 static void get_last_sample_611x(struct comedi_device
*dev
)
1576 struct ni_private
*devpriv __maybe_unused
= dev
->private;
1577 struct comedi_subdevice
*s
= &dev
->subdevices
[NI_AI_SUBDEV
];
1581 if (boardtype
.reg_type
!= ni_reg_611x
)
1584 /* Check if there's a single sample stuck in the FIFO */
1585 if (ni_readb(XXX_Status
) & 0x80) {
1586 dl
= ni_readl(ADC_FIFO_Data_611x
);
1587 data
= (dl
& 0xffff);
1588 cfc_write_to_buffer(s
, data
);
1592 static void get_last_sample_6143(struct comedi_device
*dev
)
1594 struct ni_private
*devpriv __maybe_unused
= dev
->private;
1595 struct comedi_subdevice
*s
= &dev
->subdevices
[NI_AI_SUBDEV
];
1599 if (boardtype
.reg_type
!= ni_reg_6143
)
1602 /* Check if there's a single sample stuck in the FIFO */
1603 if (ni_readl(AIFIFO_Status_6143
) & 0x01) {
1604 ni_writel(0x01, AIFIFO_Control_6143
); /* Get stranded sample into FIFO */
1605 dl
= ni_readl(AIFIFO_Data_6143
);
1607 /* This may get the hi/lo data in the wrong order */
1608 data
= (dl
>> 16) & 0xffff;
1609 cfc_write_to_buffer(s
, data
);
1613 static void ni_ai_munge(struct comedi_device
*dev
, struct comedi_subdevice
*s
,
1614 void *data
, unsigned int num_bytes
,
1615 unsigned int chan_index
)
1617 struct ni_private
*devpriv
= dev
->private;
1618 struct comedi_async
*async
= s
->async
;
1620 unsigned int length
= num_bytes
/ bytes_per_sample(s
);
1621 short *array
= data
;
1622 unsigned int *larray
= data
;
1624 for (i
= 0; i
< length
; i
++) {
1626 if (s
->subdev_flags
& SDF_LSAMPL
)
1627 larray
[i
] = le32_to_cpu(larray
[i
]);
1629 array
[i
] = le16_to_cpu(array
[i
]);
1631 if (s
->subdev_flags
& SDF_LSAMPL
)
1632 larray
[i
] += devpriv
->ai_offset
[chan_index
];
1634 array
[i
] += devpriv
->ai_offset
[chan_index
];
1636 chan_index
%= async
->cmd
.chanlist_len
;
1642 static int ni_ai_setup_MITE_dma(struct comedi_device
*dev
)
1644 struct ni_private
*devpriv
= dev
->private;
1645 struct comedi_subdevice
*s
= &dev
->subdevices
[NI_AI_SUBDEV
];
1647 unsigned long flags
;
1649 retval
= ni_request_ai_mite_channel(dev
);
1652 /* printk("comedi_debug: using mite channel %i for ai.\n", devpriv->ai_mite_chan->channel); */
1654 /* write alloc the entire buffer */
1655 comedi_buf_write_alloc(s
->async
, s
->async
->prealloc_bufsz
);
1657 spin_lock_irqsave(&devpriv
->mite_channel_lock
, flags
);
1658 if (devpriv
->ai_mite_chan
== NULL
) {
1659 spin_unlock_irqrestore(&devpriv
->mite_channel_lock
, flags
);
1663 switch (boardtype
.reg_type
) {
1666 mite_prep_dma(devpriv
->ai_mite_chan
, 32, 16);
1669 mite_prep_dma(devpriv
->ai_mite_chan
, 32, 32);
1672 mite_prep_dma(devpriv
->ai_mite_chan
, 16, 16);
1676 mite_dma_arm(devpriv
->ai_mite_chan
);
1677 spin_unlock_irqrestore(&devpriv
->mite_channel_lock
, flags
);
1682 static int ni_ao_setup_MITE_dma(struct comedi_device
*dev
)
1684 struct ni_private
*devpriv
= dev
->private;
1685 struct comedi_subdevice
*s
= &dev
->subdevices
[NI_AO_SUBDEV
];
1687 unsigned long flags
;
1689 retval
= ni_request_ao_mite_channel(dev
);
1693 /* read alloc the entire buffer */
1694 comedi_buf_read_alloc(s
->async
, s
->async
->prealloc_bufsz
);
1696 spin_lock_irqsave(&devpriv
->mite_channel_lock
, flags
);
1697 if (devpriv
->ao_mite_chan
) {
1698 if (boardtype
.reg_type
& (ni_reg_611x
| ni_reg_6713
)) {
1699 mite_prep_dma(devpriv
->ao_mite_chan
, 32, 32);
1701 /* doing 32 instead of 16 bit wide transfers from memory
1702 makes the mite do 32 bit pci transfers, doubling pci bandwidth. */
1703 mite_prep_dma(devpriv
->ao_mite_chan
, 16, 32);
1705 mite_dma_arm(devpriv
->ao_mite_chan
);
1708 spin_unlock_irqrestore(&devpriv
->mite_channel_lock
, flags
);
1716 used for both cancel ioctl and board initialization
1718 this is pretty harsh for a cancel, but it works...
1721 static int ni_ai_reset(struct comedi_device
*dev
, struct comedi_subdevice
*s
)
1723 struct ni_private
*devpriv
= dev
->private;
1725 ni_release_ai_mite_channel(dev
);
1726 /* ai configuration */
1727 devpriv
->stc_writew(dev
, AI_Configuration_Start
| AI_Reset
,
1728 Joint_Reset_Register
);
1730 ni_set_bits(dev
, Interrupt_A_Enable_Register
,
1731 AI_SC_TC_Interrupt_Enable
| AI_START1_Interrupt_Enable
|
1732 AI_START2_Interrupt_Enable
| AI_START_Interrupt_Enable
|
1733 AI_STOP_Interrupt_Enable
| AI_Error_Interrupt_Enable
|
1734 AI_FIFO_Interrupt_Enable
, 0);
1736 ni_clear_ai_fifo(dev
);
1738 if (boardtype
.reg_type
!= ni_reg_6143
)
1739 ni_writeb(0, Misc_Command
);
1741 devpriv
->stc_writew(dev
, AI_Disarm
, AI_Command_1_Register
); /* reset pulses */
1742 devpriv
->stc_writew(dev
,
1743 AI_Start_Stop
| AI_Mode_1_Reserved
1744 /*| AI_Trigger_Once */ ,
1745 AI_Mode_1_Register
);
1746 devpriv
->stc_writew(dev
, 0x0000, AI_Mode_2_Register
);
1747 /* generate FIFO interrupts on non-empty */
1748 devpriv
->stc_writew(dev
, (0 << 6) | 0x0000, AI_Mode_3_Register
);
1749 if (boardtype
.reg_type
== ni_reg_611x
) {
1750 devpriv
->stc_writew(dev
, AI_SHIFTIN_Pulse_Width
|
1752 AI_LOCALMUX_CLK_Pulse_Width
,
1753 AI_Personal_Register
);
1754 devpriv
->stc_writew(dev
,
1755 AI_SCAN_IN_PROG_Output_Select(3) |
1756 AI_EXTMUX_CLK_Output_Select(0) |
1757 AI_LOCALMUX_CLK_Output_Select(2) |
1758 AI_SC_TC_Output_Select(3) |
1759 AI_CONVERT_Output_Select
1760 (AI_CONVERT_Output_Enable_High
),
1761 AI_Output_Control_Register
);
1762 } else if (boardtype
.reg_type
== ni_reg_6143
) {
1763 devpriv
->stc_writew(dev
, AI_SHIFTIN_Pulse_Width
|
1765 AI_LOCALMUX_CLK_Pulse_Width
,
1766 AI_Personal_Register
);
1767 devpriv
->stc_writew(dev
,
1768 AI_SCAN_IN_PROG_Output_Select(3) |
1769 AI_EXTMUX_CLK_Output_Select(0) |
1770 AI_LOCALMUX_CLK_Output_Select(2) |
1771 AI_SC_TC_Output_Select(3) |
1772 AI_CONVERT_Output_Select
1773 (AI_CONVERT_Output_Enable_Low
),
1774 AI_Output_Control_Register
);
1776 unsigned ai_output_control_bits
;
1777 devpriv
->stc_writew(dev
, AI_SHIFTIN_Pulse_Width
|
1779 AI_CONVERT_Pulse_Width
|
1780 AI_LOCALMUX_CLK_Pulse_Width
,
1781 AI_Personal_Register
);
1782 ai_output_control_bits
=
1783 AI_SCAN_IN_PROG_Output_Select(3) |
1784 AI_EXTMUX_CLK_Output_Select(0) |
1785 AI_LOCALMUX_CLK_Output_Select(2) |
1786 AI_SC_TC_Output_Select(3);
1787 if (boardtype
.reg_type
== ni_reg_622x
)
1788 ai_output_control_bits
|=
1789 AI_CONVERT_Output_Select
1790 (AI_CONVERT_Output_Enable_High
);
1792 ai_output_control_bits
|=
1793 AI_CONVERT_Output_Select
1794 (AI_CONVERT_Output_Enable_Low
);
1795 devpriv
->stc_writew(dev
, ai_output_control_bits
,
1796 AI_Output_Control_Register
);
1798 /* the following registers should not be changed, because there
1799 * are no backup registers in devpriv. If you want to change
1800 * any of these, add a backup register and other appropriate code:
1801 * AI_Mode_1_Register
1802 * AI_Mode_3_Register
1803 * AI_Personal_Register
1804 * AI_Output_Control_Register
1806 devpriv
->stc_writew(dev
, AI_SC_TC_Error_Confirm
| AI_START_Interrupt_Ack
| AI_START2_Interrupt_Ack
| AI_START1_Interrupt_Ack
| AI_SC_TC_Interrupt_Ack
| AI_Error_Interrupt_Ack
| AI_STOP_Interrupt_Ack
, Interrupt_A_Ack_Register
); /* clear interrupts */
1808 devpriv
->stc_writew(dev
, AI_Configuration_End
, Joint_Reset_Register
);
1813 static int ni_ai_poll(struct comedi_device
*dev
, struct comedi_subdevice
*s
)
1815 unsigned long flags
;
1818 /* lock to avoid race with interrupt handler */
1819 spin_lock_irqsave(&dev
->spinlock
, flags
);
1821 ni_handle_fifo_dregs(dev
);
1823 ni_sync_ai_dma(dev
);
1825 count
= s
->async
->buf_write_count
- s
->async
->buf_read_count
;
1826 spin_unlock_irqrestore(&dev
->spinlock
, flags
);
1831 static int ni_ai_insn_read(struct comedi_device
*dev
,
1832 struct comedi_subdevice
*s
, struct comedi_insn
*insn
,
1835 struct ni_private
*devpriv
= dev
->private;
1837 const unsigned int mask
= (1 << boardtype
.adbits
) - 1;
1842 ni_load_channelgain_list(dev
, 1, &insn
->chanspec
);
1844 ni_clear_ai_fifo(dev
);
1846 signbits
= devpriv
->ai_offset
[0];
1847 if (boardtype
.reg_type
== ni_reg_611x
) {
1848 for (n
= 0; n
< num_adc_stages_611x
; n
++) {
1849 devpriv
->stc_writew(dev
, AI_CONVERT_Pulse
,
1850 AI_Command_1_Register
);
1853 for (n
= 0; n
< insn
->n
; n
++) {
1854 devpriv
->stc_writew(dev
, AI_CONVERT_Pulse
,
1855 AI_Command_1_Register
);
1856 /* The 611x has screwy 32-bit FIFOs. */
1858 for (i
= 0; i
< NI_TIMEOUT
; i
++) {
1859 if (ni_readb(XXX_Status
) & 0x80) {
1860 d
= (ni_readl(ADC_FIFO_Data_611x
) >> 16)
1864 if (!(devpriv
->stc_readw(dev
,
1865 AI_Status_1_Register
) &
1866 AI_FIFO_Empty_St
)) {
1867 d
= ni_readl(ADC_FIFO_Data_611x
) &
1872 if (i
== NI_TIMEOUT
) {
1874 ("ni_mio_common: timeout in 611x ni_ai_insn_read\n");
1880 } else if (boardtype
.reg_type
== ni_reg_6143
) {
1881 for (n
= 0; n
< insn
->n
; n
++) {
1882 devpriv
->stc_writew(dev
, AI_CONVERT_Pulse
,
1883 AI_Command_1_Register
);
1885 /* The 6143 has 32-bit FIFOs. You need to strobe a bit to move a single 16bit stranded sample into the FIFO */
1887 for (i
= 0; i
< NI_TIMEOUT
; i
++) {
1888 if (ni_readl(AIFIFO_Status_6143
) & 0x01) {
1889 ni_writel(0x01, AIFIFO_Control_6143
); /* Get stranded sample into FIFO */
1890 dl
= ni_readl(AIFIFO_Data_6143
);
1894 if (i
== NI_TIMEOUT
) {
1896 ("ni_mio_common: timeout in 6143 ni_ai_insn_read\n");
1899 data
[n
] = (((dl
>> 16) & 0xFFFF) + signbits
) & 0xFFFF;
1902 for (n
= 0; n
< insn
->n
; n
++) {
1903 devpriv
->stc_writew(dev
, AI_CONVERT_Pulse
,
1904 AI_Command_1_Register
);
1905 for (i
= 0; i
< NI_TIMEOUT
; i
++) {
1906 if (!(devpriv
->stc_readw(dev
,
1907 AI_Status_1_Register
) &
1911 if (i
== NI_TIMEOUT
) {
1913 ("ni_mio_common: timeout in ni_ai_insn_read\n");
1916 if (boardtype
.reg_type
& ni_reg_m_series_mask
) {
1918 ni_readl(M_Offset_AI_FIFO_Data
) & mask
;
1920 d
= ni_readw(ADC_FIFO_Data_Register
);
1921 d
+= signbits
; /* subtle: needs to be short addition */
1929 static void ni_prime_channelgain_list(struct comedi_device
*dev
)
1931 struct ni_private
*devpriv
= dev
->private;
1934 devpriv
->stc_writew(dev
, AI_CONVERT_Pulse
, AI_Command_1_Register
);
1935 for (i
= 0; i
< NI_TIMEOUT
; ++i
) {
1936 if (!(devpriv
->stc_readw(dev
,
1937 AI_Status_1_Register
) &
1938 AI_FIFO_Empty_St
)) {
1939 devpriv
->stc_writew(dev
, 1, ADC_FIFO_Clear
);
1944 printk("ni_mio_common: timeout loading channel/gain list\n");
1947 static void ni_m_series_load_channelgain_list(struct comedi_device
*dev
,
1948 unsigned int n_chan
,
1951 struct ni_private
*devpriv
= dev
->private;
1952 unsigned int chan
, range
, aref
;
1955 unsigned int dither
;
1956 unsigned range_code
;
1958 devpriv
->stc_writew(dev
, 1, Configuration_Memory_Clear
);
1960 /* offset = 1 << (boardtype.adbits - 1); */
1961 if ((list
[0] & CR_ALT_SOURCE
)) {
1962 unsigned bypass_bits
;
1963 chan
= CR_CHAN(list
[0]);
1964 range
= CR_RANGE(list
[0]);
1965 range_code
= ni_gainlkup
[boardtype
.gainlkup
][range
];
1966 dither
= ((list
[0] & CR_ALT_FILTER
) != 0);
1967 bypass_bits
= MSeries_AI_Bypass_Config_FIFO_Bit
;
1968 bypass_bits
|= chan
;
1970 (devpriv
->ai_calib_source
) &
1971 (MSeries_AI_Bypass_Cal_Sel_Pos_Mask
|
1972 MSeries_AI_Bypass_Cal_Sel_Neg_Mask
|
1973 MSeries_AI_Bypass_Mode_Mux_Mask
|
1974 MSeries_AO_Bypass_AO_Cal_Sel_Mask
);
1975 bypass_bits
|= MSeries_AI_Bypass_Gain_Bits(range_code
);
1977 bypass_bits
|= MSeries_AI_Bypass_Dither_Bit
;
1978 /* don't use 2's complement encoding */
1979 bypass_bits
|= MSeries_AI_Bypass_Polarity_Bit
;
1980 ni_writel(bypass_bits
, M_Offset_AI_Config_FIFO_Bypass
);
1982 ni_writel(0, M_Offset_AI_Config_FIFO_Bypass
);
1985 for (i
= 0; i
< n_chan
; i
++) {
1986 unsigned config_bits
= 0;
1987 chan
= CR_CHAN(list
[i
]);
1988 aref
= CR_AREF(list
[i
]);
1989 range
= CR_RANGE(list
[i
]);
1990 dither
= ((list
[i
] & CR_ALT_FILTER
) != 0);
1992 range_code
= ni_gainlkup
[boardtype
.gainlkup
][range
];
1993 devpriv
->ai_offset
[i
] = offset
;
1997 MSeries_AI_Config_Channel_Type_Differential_Bits
;
2001 MSeries_AI_Config_Channel_Type_Common_Ref_Bits
;
2005 MSeries_AI_Config_Channel_Type_Ground_Ref_Bits
;
2010 config_bits
|= MSeries_AI_Config_Channel_Bits(chan
);
2012 MSeries_AI_Config_Bank_Bits(boardtype
.reg_type
, chan
);
2013 config_bits
|= MSeries_AI_Config_Gain_Bits(range_code
);
2014 if (i
== n_chan
- 1)
2015 config_bits
|= MSeries_AI_Config_Last_Channel_Bit
;
2017 config_bits
|= MSeries_AI_Config_Dither_Bit
;
2018 /* don't use 2's complement encoding */
2019 config_bits
|= MSeries_AI_Config_Polarity_Bit
;
2020 ni_writew(config_bits
, M_Offset_AI_Config_FIFO_Data
);
2022 ni_prime_channelgain_list(dev
);
2026 * Notes on the 6110 and 6111:
2027 * These boards a slightly different than the rest of the series, since
2028 * they have multiple A/D converters.
2029 * From the driver side, the configuration memory is a
2031 * Configuration Memory Low:
2033 * bit 8: unipolar/bipolar (should be 0 for bipolar)
2034 * bits 0-3: gain. This is 4 bits instead of 3 for the other boards
2035 * 1001 gain=0.1 (+/- 50)
2044 * Configuration Memory High:
2045 * bits 12-14: Channel Type
2046 * 001 for differential
2047 * 000 for calibration
2048 * bit 11: coupling (this is not currently handled)
2052 * valid channels are 0-3
2054 static void ni_load_channelgain_list(struct comedi_device
*dev
,
2055 unsigned int n_chan
, unsigned int *list
)
2057 struct ni_private
*devpriv
= dev
->private;
2058 unsigned int chan
, range
, aref
;
2060 unsigned int hi
, lo
;
2062 unsigned int dither
;
2064 if (boardtype
.reg_type
& ni_reg_m_series_mask
) {
2065 ni_m_series_load_channelgain_list(dev
, n_chan
, list
);
2068 if (n_chan
== 1 && (boardtype
.reg_type
!= ni_reg_611x
)
2069 && (boardtype
.reg_type
!= ni_reg_6143
)) {
2070 if (devpriv
->changain_state
2071 && devpriv
->changain_spec
== list
[0]) {
2075 devpriv
->changain_state
= 1;
2076 devpriv
->changain_spec
= list
[0];
2078 devpriv
->changain_state
= 0;
2081 devpriv
->stc_writew(dev
, 1, Configuration_Memory_Clear
);
2083 /* Set up Calibration mode if required */
2084 if (boardtype
.reg_type
== ni_reg_6143
) {
2085 if ((list
[0] & CR_ALT_SOURCE
)
2086 && !devpriv
->ai_calib_source_enabled
) {
2087 /* Strobe Relay enable bit */
2088 ni_writew(devpriv
->ai_calib_source
|
2089 Calibration_Channel_6143_RelayOn
,
2090 Calibration_Channel_6143
);
2091 ni_writew(devpriv
->ai_calib_source
,
2092 Calibration_Channel_6143
);
2093 devpriv
->ai_calib_source_enabled
= 1;
2094 msleep_interruptible(100); /* Allow relays to change */
2095 } else if (!(list
[0] & CR_ALT_SOURCE
)
2096 && devpriv
->ai_calib_source_enabled
) {
2097 /* Strobe Relay disable bit */
2098 ni_writew(devpriv
->ai_calib_source
|
2099 Calibration_Channel_6143_RelayOff
,
2100 Calibration_Channel_6143
);
2101 ni_writew(devpriv
->ai_calib_source
,
2102 Calibration_Channel_6143
);
2103 devpriv
->ai_calib_source_enabled
= 0;
2104 msleep_interruptible(100); /* Allow relays to change */
2108 offset
= 1 << (boardtype
.adbits
- 1);
2109 for (i
= 0; i
< n_chan
; i
++) {
2110 if ((boardtype
.reg_type
!= ni_reg_6143
)
2111 && (list
[i
] & CR_ALT_SOURCE
)) {
2112 chan
= devpriv
->ai_calib_source
;
2114 chan
= CR_CHAN(list
[i
]);
2116 aref
= CR_AREF(list
[i
]);
2117 range
= CR_RANGE(list
[i
]);
2118 dither
= ((list
[i
] & CR_ALT_FILTER
) != 0);
2120 /* fix the external/internal range differences */
2121 range
= ni_gainlkup
[boardtype
.gainlkup
][range
];
2122 if (boardtype
.reg_type
== ni_reg_611x
)
2123 devpriv
->ai_offset
[i
] = offset
;
2125 devpriv
->ai_offset
[i
] = (range
& 0x100) ? 0 : offset
;
2128 if ((list
[i
] & CR_ALT_SOURCE
)) {
2129 if (boardtype
.reg_type
== ni_reg_611x
)
2130 ni_writew(CR_CHAN(list
[i
]) & 0x0003,
2131 Calibration_Channel_Select_611x
);
2133 if (boardtype
.reg_type
== ni_reg_611x
)
2135 else if (boardtype
.reg_type
== ni_reg_6143
)
2139 hi
|= AI_DIFFERENTIAL
;
2151 hi
|= AI_CONFIG_CHANNEL(chan
);
2153 ni_writew(hi
, Configuration_Memory_High
);
2155 if (boardtype
.reg_type
!= ni_reg_6143
) {
2157 if (i
== n_chan
- 1)
2158 lo
|= AI_LAST_CHANNEL
;
2162 ni_writew(lo
, Configuration_Memory_Low
);
2166 /* prime the channel/gain list */
2167 if ((boardtype
.reg_type
!= ni_reg_611x
)
2168 && (boardtype
.reg_type
!= ni_reg_6143
)) {
2169 ni_prime_channelgain_list(dev
);
2173 static int ni_ns_to_timer(const struct comedi_device
*dev
, unsigned nanosec
,
2176 struct ni_private
*devpriv
= dev
->private;
2179 switch (round_mode
) {
2180 case TRIG_ROUND_NEAREST
:
2182 divider
= (nanosec
+ devpriv
->clock_ns
/ 2) / devpriv
->clock_ns
;
2184 case TRIG_ROUND_DOWN
:
2185 divider
= (nanosec
) / devpriv
->clock_ns
;
2188 divider
= (nanosec
+ devpriv
->clock_ns
- 1) / devpriv
->clock_ns
;
2194 static unsigned ni_timer_to_ns(const struct comedi_device
*dev
, int timer
)
2196 struct ni_private
*devpriv
= dev
->private;
2198 return devpriv
->clock_ns
* (timer
+ 1);
2201 static unsigned ni_min_ai_scan_period_ns(struct comedi_device
*dev
,
2202 unsigned num_channels
)
2204 switch (boardtype
.reg_type
) {
2207 /* simultaneously-sampled inputs */
2208 return boardtype
.ai_speed
;
2211 /* multiplexed inputs */
2214 return boardtype
.ai_speed
* num_channels
;
2217 static int ni_ai_cmdtest(struct comedi_device
*dev
, struct comedi_subdevice
*s
,
2218 struct comedi_cmd
*cmd
)
2220 struct ni_private
*devpriv
= dev
->private;
2223 unsigned int sources
;
2225 /* Step 1 : check if triggers are trivially valid */
2227 if ((cmd
->flags
& CMDF_WRITE
))
2228 cmd
->flags
&= ~CMDF_WRITE
;
2230 err
|= cfc_check_trigger_src(&cmd
->start_src
,
2231 TRIG_NOW
| TRIG_INT
| TRIG_EXT
);
2232 err
|= cfc_check_trigger_src(&cmd
->scan_begin_src
,
2233 TRIG_TIMER
| TRIG_EXT
);
2235 sources
= TRIG_TIMER
| TRIG_EXT
;
2236 if (boardtype
.reg_type
== ni_reg_611x
||
2237 boardtype
.reg_type
== ni_reg_6143
)
2238 sources
|= TRIG_NOW
;
2239 err
|= cfc_check_trigger_src(&cmd
->convert_src
, sources
);
2241 err
|= cfc_check_trigger_src(&cmd
->scan_end_src
, TRIG_COUNT
);
2242 err
|= cfc_check_trigger_src(&cmd
->stop_src
, TRIG_COUNT
| TRIG_NONE
);
2247 /* Step 2a : make sure trigger sources are unique */
2249 err
|= cfc_check_trigger_is_unique(cmd
->start_src
);
2250 err
|= cfc_check_trigger_is_unique(cmd
->scan_begin_src
);
2251 err
|= cfc_check_trigger_is_unique(cmd
->convert_src
);
2252 err
|= cfc_check_trigger_is_unique(cmd
->stop_src
);
2254 /* Step 2b : and mutually compatible */
2259 /* step 3: make sure arguments are trivially compatible */
2261 if (cmd
->start_src
== TRIG_EXT
) {
2262 /* external trigger */
2263 unsigned int tmp
= CR_CHAN(cmd
->start_arg
);
2267 tmp
|= (cmd
->start_arg
& (CR_INVERT
| CR_EDGE
));
2268 if (cmd
->start_arg
!= tmp
) {
2269 cmd
->start_arg
= tmp
;
2273 if (cmd
->start_arg
!= 0) {
2274 /* true for both TRIG_NOW and TRIG_INT */
2279 if (cmd
->scan_begin_src
== TRIG_TIMER
) {
2280 if (cmd
->scan_begin_arg
< ni_min_ai_scan_period_ns(dev
,
2284 cmd
->scan_begin_arg
=
2285 ni_min_ai_scan_period_ns(dev
, cmd
->chanlist_len
);
2288 if (cmd
->scan_begin_arg
> devpriv
->clock_ns
* 0xffffff) {
2289 cmd
->scan_begin_arg
= devpriv
->clock_ns
* 0xffffff;
2292 } else if (cmd
->scan_begin_src
== TRIG_EXT
) {
2293 /* external trigger */
2294 unsigned int tmp
= CR_CHAN(cmd
->scan_begin_arg
);
2298 tmp
|= (cmd
->scan_begin_arg
& (CR_INVERT
| CR_EDGE
));
2299 if (cmd
->scan_begin_arg
!= tmp
) {
2300 cmd
->scan_begin_arg
= tmp
;
2303 } else { /* TRIG_OTHER */
2304 if (cmd
->scan_begin_arg
) {
2305 cmd
->scan_begin_arg
= 0;
2309 if (cmd
->convert_src
== TRIG_TIMER
) {
2310 if ((boardtype
.reg_type
== ni_reg_611x
)
2311 || (boardtype
.reg_type
== ni_reg_6143
)) {
2312 if (cmd
->convert_arg
!= 0) {
2313 cmd
->convert_arg
= 0;
2317 if (cmd
->convert_arg
< boardtype
.ai_speed
) {
2318 cmd
->convert_arg
= boardtype
.ai_speed
;
2321 if (cmd
->convert_arg
> devpriv
->clock_ns
* 0xffff) {
2322 cmd
->convert_arg
= devpriv
->clock_ns
* 0xffff;
2326 } else if (cmd
->convert_src
== TRIG_EXT
) {
2327 /* external trigger */
2328 unsigned int tmp
= CR_CHAN(cmd
->convert_arg
);
2332 tmp
|= (cmd
->convert_arg
& (CR_ALT_FILTER
| CR_INVERT
));
2333 if (cmd
->convert_arg
!= tmp
) {
2334 cmd
->convert_arg
= tmp
;
2337 } else if (cmd
->convert_src
== TRIG_NOW
) {
2338 if (cmd
->convert_arg
!= 0) {
2339 cmd
->convert_arg
= 0;
2344 if (cmd
->scan_end_arg
!= cmd
->chanlist_len
) {
2345 cmd
->scan_end_arg
= cmd
->chanlist_len
;
2348 if (cmd
->stop_src
== TRIG_COUNT
) {
2349 unsigned int max_count
= 0x01000000;
2351 if (boardtype
.reg_type
== ni_reg_611x
)
2352 max_count
-= num_adc_stages_611x
;
2353 if (cmd
->stop_arg
> max_count
) {
2354 cmd
->stop_arg
= max_count
;
2357 if (cmd
->stop_arg
< 1) {
2363 if (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 ((boardtype
.reg_type
!= ni_reg_611x
)
2387 && (boardtype
.reg_type
!= ni_reg_6143
)) {
2388 tmp
= cmd
->convert_arg
;
2390 ni_timer_to_ns(dev
, ni_ns_to_timer(dev
,
2395 if (tmp
!= cmd
->convert_arg
)
2397 if (cmd
->scan_begin_src
== TRIG_TIMER
&&
2398 cmd
->scan_begin_arg
<
2399 cmd
->convert_arg
* cmd
->scan_end_arg
) {
2400 cmd
->scan_begin_arg
=
2401 cmd
->convert_arg
* cmd
->scan_end_arg
;
2413 static int ni_ai_cmd(struct comedi_device
*dev
, struct comedi_subdevice
*s
)
2415 struct ni_private
*devpriv
= dev
->private;
2416 const struct comedi_cmd
*cmd
= &s
->async
->cmd
;
2418 int mode1
= 0; /* mode1 is needed for both stop and convert */
2420 int start_stop_select
= 0;
2421 unsigned int stop_count
;
2422 int interrupt_a_enable
= 0;
2424 MDPRINTK("ni_ai_cmd\n");
2425 if (dev
->irq
== 0) {
2426 comedi_error(dev
, "cannot run command without an irq");
2429 ni_clear_ai_fifo(dev
);
2431 ni_load_channelgain_list(dev
, cmd
->chanlist_len
, cmd
->chanlist
);
2433 /* start configuration */
2434 devpriv
->stc_writew(dev
, AI_Configuration_Start
, Joint_Reset_Register
);
2436 /* disable analog triggering for now, since it
2437 * interferes with the use of pfi0 */
2438 devpriv
->an_trig_etc_reg
&= ~Analog_Trigger_Enable
;
2439 devpriv
->stc_writew(dev
, devpriv
->an_trig_etc_reg
,
2440 Analog_Trigger_Etc_Register
);
2442 switch (cmd
->start_src
) {
2445 devpriv
->stc_writew(dev
, AI_START2_Select(0) |
2446 AI_START1_Sync
| AI_START1_Edge
|
2447 AI_START1_Select(0),
2448 AI_Trigger_Select_Register
);
2452 int chan
= CR_CHAN(cmd
->start_arg
);
2453 unsigned int bits
= AI_START2_Select(0) |
2454 AI_START1_Sync
| AI_START1_Select(chan
+ 1);
2456 if (cmd
->start_arg
& CR_INVERT
)
2457 bits
|= AI_START1_Polarity
;
2458 if (cmd
->start_arg
& CR_EDGE
)
2459 bits
|= AI_START1_Edge
;
2460 devpriv
->stc_writew(dev
, bits
,
2461 AI_Trigger_Select_Register
);
2466 mode2
&= ~AI_Pre_Trigger
;
2467 mode2
&= ~AI_SC_Initial_Load_Source
;
2468 mode2
&= ~AI_SC_Reload_Mode
;
2469 devpriv
->stc_writew(dev
, mode2
, AI_Mode_2_Register
);
2471 if (cmd
->chanlist_len
== 1 || (boardtype
.reg_type
== ni_reg_611x
)
2472 || (boardtype
.reg_type
== ni_reg_6143
)) {
2473 start_stop_select
|= AI_STOP_Polarity
;
2474 start_stop_select
|= AI_STOP_Select(31); /* logic low */
2475 start_stop_select
|= AI_STOP_Sync
;
2477 start_stop_select
|= AI_STOP_Select(19); /* ai configuration memory */
2479 devpriv
->stc_writew(dev
, start_stop_select
,
2480 AI_START_STOP_Select_Register
);
2482 devpriv
->ai_cmd2
= 0;
2483 switch (cmd
->stop_src
) {
2485 stop_count
= cmd
->stop_arg
- 1;
2487 if (boardtype
.reg_type
== ni_reg_611x
) {
2488 /* have to take 3 stage adc pipeline into account */
2489 stop_count
+= num_adc_stages_611x
;
2491 /* stage number of scans */
2492 devpriv
->stc_writel(dev
, stop_count
, AI_SC_Load_A_Registers
);
2494 mode1
|= AI_Start_Stop
| AI_Mode_1_Reserved
| AI_Trigger_Once
;
2495 devpriv
->stc_writew(dev
, mode1
, AI_Mode_1_Register
);
2496 /* load SC (Scan Count) */
2497 devpriv
->stc_writew(dev
, AI_SC_Load
, AI_Command_1_Register
);
2499 devpriv
->ai_continuous
= 0;
2500 if (stop_count
== 0) {
2501 devpriv
->ai_cmd2
|= AI_End_On_End_Of_Scan
;
2502 interrupt_a_enable
|= AI_STOP_Interrupt_Enable
;
2503 /* this is required to get the last sample for chanlist_len > 1, not sure why */
2504 if (cmd
->chanlist_len
> 1)
2505 start_stop_select
|=
2506 AI_STOP_Polarity
| AI_STOP_Edge
;
2510 /* stage number of scans */
2511 devpriv
->stc_writel(dev
, 0, AI_SC_Load_A_Registers
);
2513 mode1
|= AI_Start_Stop
| AI_Mode_1_Reserved
| AI_Continuous
;
2514 devpriv
->stc_writew(dev
, mode1
, AI_Mode_1_Register
);
2516 /* load SC (Scan Count) */
2517 devpriv
->stc_writew(dev
, AI_SC_Load
, AI_Command_1_Register
);
2519 devpriv
->ai_continuous
= 1;
2524 switch (cmd
->scan_begin_src
) {
2527 stop bits for non 611x boards
2528 AI_SI_Special_Trigger_Delay=0
2530 AI_START_STOP_Select_Register:
2531 AI_START_Polarity=0 (?) rising edge
2532 AI_START_Edge=1 edge triggered
2534 AI_START_Select=0 SI_TC
2535 AI_STOP_Polarity=0 rising edge
2536 AI_STOP_Edge=0 level
2538 AI_STOP_Select=19 external pin (configuration mem)
2540 start_stop_select
|= AI_START_Edge
| AI_START_Sync
;
2541 devpriv
->stc_writew(dev
, start_stop_select
,
2542 AI_START_STOP_Select_Register
);
2544 mode2
|= AI_SI_Reload_Mode(0);
2545 /* AI_SI_Initial_Load_Source=A */
2546 mode2
&= ~AI_SI_Initial_Load_Source
;
2547 /* mode2 |= AI_SC_Reload_Mode; */
2548 devpriv
->stc_writew(dev
, mode2
, AI_Mode_2_Register
);
2551 timer
= ni_ns_to_timer(dev
, cmd
->scan_begin_arg
,
2552 TRIG_ROUND_NEAREST
);
2553 devpriv
->stc_writel(dev
, timer
, AI_SI_Load_A_Registers
);
2554 devpriv
->stc_writew(dev
, AI_SI_Load
, AI_Command_1_Register
);
2557 if (cmd
->scan_begin_arg
& CR_EDGE
)
2558 start_stop_select
|= AI_START_Edge
;
2559 /* AI_START_Polarity==1 is falling edge */
2560 if (cmd
->scan_begin_arg
& CR_INVERT
)
2561 start_stop_select
|= AI_START_Polarity
;
2562 if (cmd
->scan_begin_src
!= cmd
->convert_src
||
2563 (cmd
->scan_begin_arg
& ~CR_EDGE
) !=
2564 (cmd
->convert_arg
& ~CR_EDGE
))
2565 start_stop_select
|= AI_START_Sync
;
2566 start_stop_select
|=
2567 AI_START_Select(1 + CR_CHAN(cmd
->scan_begin_arg
));
2568 devpriv
->stc_writew(dev
, start_stop_select
,
2569 AI_START_STOP_Select_Register
);
2573 switch (cmd
->convert_src
) {
2576 if (cmd
->convert_arg
== 0 || cmd
->convert_src
== TRIG_NOW
)
2579 timer
= ni_ns_to_timer(dev
, cmd
->convert_arg
,
2580 TRIG_ROUND_NEAREST
);
2581 devpriv
->stc_writew(dev
, 1, AI_SI2_Load_A_Register
); /* 0,0 does not work. */
2582 devpriv
->stc_writew(dev
, timer
, AI_SI2_Load_B_Register
);
2584 /* AI_SI2_Reload_Mode = alternate */
2585 /* AI_SI2_Initial_Load_Source = A */
2586 mode2
&= ~AI_SI2_Initial_Load_Source
;
2587 mode2
|= AI_SI2_Reload_Mode
;
2588 devpriv
->stc_writew(dev
, mode2
, AI_Mode_2_Register
);
2591 devpriv
->stc_writew(dev
, AI_SI2_Load
, AI_Command_1_Register
);
2593 mode2
|= AI_SI2_Reload_Mode
; /* alternate */
2594 mode2
|= AI_SI2_Initial_Load_Source
; /* B */
2596 devpriv
->stc_writew(dev
, mode2
, AI_Mode_2_Register
);
2599 mode1
|= AI_CONVERT_Source_Select(1 + cmd
->convert_arg
);
2600 if ((cmd
->convert_arg
& CR_INVERT
) == 0)
2601 mode1
|= AI_CONVERT_Source_Polarity
;
2602 devpriv
->stc_writew(dev
, mode1
, AI_Mode_1_Register
);
2604 mode2
|= AI_Start_Stop_Gate_Enable
| AI_SC_Gate_Enable
;
2605 devpriv
->stc_writew(dev
, mode2
, AI_Mode_2_Register
);
2612 /* interrupt on FIFO, errors, SC_TC */
2613 interrupt_a_enable
|= AI_Error_Interrupt_Enable
|
2614 AI_SC_TC_Interrupt_Enable
;
2617 interrupt_a_enable
|= AI_FIFO_Interrupt_Enable
;
2620 if (cmd
->flags
& TRIG_WAKE_EOS
2621 || (devpriv
->ai_cmd2
& AI_End_On_End_Of_Scan
)) {
2622 /* wake on end-of-scan */
2623 devpriv
->aimode
= AIMODE_SCAN
;
2625 devpriv
->aimode
= AIMODE_HALF_FULL
;
2628 switch (devpriv
->aimode
) {
2629 case AIMODE_HALF_FULL
:
2630 /*generate FIFO interrupts and DMA requests on half-full */
2632 devpriv
->stc_writew(dev
, AI_FIFO_Mode_HF_to_E
,
2633 AI_Mode_3_Register
);
2635 devpriv
->stc_writew(dev
, AI_FIFO_Mode_HF
,
2636 AI_Mode_3_Register
);
2640 /*generate FIFO interrupts on non-empty */
2641 devpriv
->stc_writew(dev
, AI_FIFO_Mode_NE
,
2642 AI_Mode_3_Register
);
2646 devpriv
->stc_writew(dev
, AI_FIFO_Mode_NE
,
2647 AI_Mode_3_Register
);
2649 devpriv
->stc_writew(dev
, AI_FIFO_Mode_HF
,
2650 AI_Mode_3_Register
);
2652 interrupt_a_enable
|= AI_STOP_Interrupt_Enable
;
2658 devpriv
->stc_writew(dev
, AI_Error_Interrupt_Ack
| AI_STOP_Interrupt_Ack
| AI_START_Interrupt_Ack
| AI_START2_Interrupt_Ack
| AI_START1_Interrupt_Ack
| AI_SC_TC_Interrupt_Ack
| AI_SC_TC_Error_Confirm
, Interrupt_A_Ack_Register
); /* clear interrupts */
2660 ni_set_bits(dev
, Interrupt_A_Enable_Register
,
2661 interrupt_a_enable
, 1);
2663 MDPRINTK("Interrupt_A_Enable_Register = 0x%04x\n",
2664 devpriv
->int_a_enable_reg
);
2666 /* interrupt on nothing */
2667 ni_set_bits(dev
, Interrupt_A_Enable_Register
, ~0, 0);
2669 /* XXX start polling if necessary */
2670 MDPRINTK("interrupting on nothing\n");
2673 /* end configuration */
2674 devpriv
->stc_writew(dev
, AI_Configuration_End
, Joint_Reset_Register
);
2676 switch (cmd
->scan_begin_src
) {
2678 devpriv
->stc_writew(dev
,
2679 AI_SI2_Arm
| AI_SI_Arm
| AI_DIV_Arm
|
2680 AI_SC_Arm
, AI_Command_1_Register
);
2683 /* XXX AI_SI_Arm? */
2684 devpriv
->stc_writew(dev
,
2685 AI_SI2_Arm
| AI_SI_Arm
| AI_DIV_Arm
|
2686 AI_SC_Arm
, AI_Command_1_Register
);
2692 int retval
= ni_ai_setup_MITE_dma(dev
);
2696 /* mite_dump_regs(devpriv->mite); */
2699 switch (cmd
->start_src
) {
2701 /* AI_START1_Pulse */
2702 devpriv
->stc_writew(dev
, AI_START1_Pulse
| devpriv
->ai_cmd2
,
2703 AI_Command_2_Register
);
2704 s
->async
->inttrig
= NULL
;
2707 s
->async
->inttrig
= NULL
;
2710 s
->async
->inttrig
= &ni_ai_inttrig
;
2714 MDPRINTK("exit ni_ai_cmd\n");
2719 static int ni_ai_inttrig(struct comedi_device
*dev
, struct comedi_subdevice
*s
,
2720 unsigned int trignum
)
2722 struct ni_private
*devpriv
= dev
->private;
2727 devpriv
->stc_writew(dev
, AI_START1_Pulse
| devpriv
->ai_cmd2
,
2728 AI_Command_2_Register
);
2729 s
->async
->inttrig
= NULL
;
2734 static int ni_ai_config_analog_trig(struct comedi_device
*dev
,
2735 struct comedi_subdevice
*s
,
2736 struct comedi_insn
*insn
,
2737 unsigned int *data
);
2739 static int ni_ai_insn_config(struct comedi_device
*dev
,
2740 struct comedi_subdevice
*s
,
2741 struct comedi_insn
*insn
, unsigned int *data
)
2743 struct ni_private
*devpriv
= dev
->private;
2749 case INSN_CONFIG_ANALOG_TRIG
:
2750 return ni_ai_config_analog_trig(dev
, s
, insn
, data
);
2751 case INSN_CONFIG_ALT_SOURCE
:
2752 if (boardtype
.reg_type
& ni_reg_m_series_mask
) {
2753 if (data
[1] & ~(MSeries_AI_Bypass_Cal_Sel_Pos_Mask
|
2754 MSeries_AI_Bypass_Cal_Sel_Neg_Mask
|
2755 MSeries_AI_Bypass_Mode_Mux_Mask
|
2756 MSeries_AO_Bypass_AO_Cal_Sel_Mask
)) {
2759 devpriv
->ai_calib_source
= data
[1];
2760 } else if (boardtype
.reg_type
== ni_reg_6143
) {
2761 unsigned int calib_source
;
2763 calib_source
= data
[1] & 0xf;
2765 if (calib_source
> 0xF)
2768 devpriv
->ai_calib_source
= calib_source
;
2769 ni_writew(calib_source
, Calibration_Channel_6143
);
2771 unsigned int calib_source
;
2772 unsigned int calib_source_adjust
;
2774 calib_source
= data
[1] & 0xf;
2775 calib_source_adjust
= (data
[1] >> 4) & 0xff;
2777 if (calib_source
>= 8)
2779 devpriv
->ai_calib_source
= calib_source
;
2780 if (boardtype
.reg_type
== ni_reg_611x
) {
2781 ni_writeb(calib_source_adjust
,
2782 Cal_Gain_Select_611x
);
2793 static int ni_ai_config_analog_trig(struct comedi_device
*dev
,
2794 struct comedi_subdevice
*s
,
2795 struct comedi_insn
*insn
,
2798 struct ni_private
*devpriv
= dev
->private;
2799 unsigned int a
, b
, modebits
;
2803 * data[2] is analog line
2804 * data[3] is set level
2805 * data[4] is reset level */
2806 if (!boardtype
.has_analog_trig
)
2808 if ((data
[1] & 0xffff0000) != COMEDI_EV_SCAN_BEGIN
) {
2809 data
[1] &= (COMEDI_EV_SCAN_BEGIN
| 0xffff);
2812 if (data
[2] >= boardtype
.n_adchan
) {
2813 data
[2] = boardtype
.n_adchan
- 1;
2816 if (data
[3] > 255) { /* a */
2820 if (data
[4] > 255) { /* b */
2831 * high mode 00 00 01 10
2832 * low mode 00 00 10 01
2834 * hysteresis low mode 10 00 00 01
2835 * hysteresis high mode 01 00 00 10
2836 * middle mode 10 01 01 10
2841 modebits
= data
[1] & 0xff;
2842 if (modebits
& 0xf0) {
2843 /* two level mode */
2849 ((data
[1] & 0xf) << 4) | ((data
[1] & 0xf0) >> 4);
2851 devpriv
->atrig_low
= a
;
2852 devpriv
->atrig_high
= b
;
2854 case 0x81: /* low hysteresis mode */
2855 devpriv
->atrig_mode
= 6;
2857 case 0x42: /* high hysteresis mode */
2858 devpriv
->atrig_mode
= 3;
2860 case 0x96: /* middle window mode */
2861 devpriv
->atrig_mode
= 2;
2868 /* one level mode */
2874 case 0x06: /* high window mode */
2875 devpriv
->atrig_high
= a
;
2876 devpriv
->atrig_mode
= 0;
2878 case 0x09: /* low window mode */
2879 devpriv
->atrig_low
= a
;
2880 devpriv
->atrig_mode
= 1;
2892 /* munge data from unsigned to 2's complement for analog output bipolar modes */
2893 static void ni_ao_munge(struct comedi_device
*dev
, struct comedi_subdevice
*s
,
2894 void *data
, unsigned int num_bytes
,
2895 unsigned int chan_index
)
2897 struct comedi_async
*async
= s
->async
;
2900 unsigned int offset
;
2901 unsigned int length
= num_bytes
/ sizeof(short);
2902 short *array
= data
;
2904 offset
= 1 << (boardtype
.aobits
- 1);
2905 for (i
= 0; i
< length
; i
++) {
2906 range
= CR_RANGE(async
->cmd
.chanlist
[chan_index
]);
2907 if (boardtype
.ao_unipolar
== 0 || (range
& 1) == 0)
2910 array
[i
] = cpu_to_le16(array
[i
]);
2913 chan_index
%= async
->cmd
.chanlist_len
;
2917 static int ni_m_series_ao_config_chanlist(struct comedi_device
*dev
,
2918 struct comedi_subdevice
*s
,
2919 unsigned int chanspec
[],
2920 unsigned int n_chans
, int timed
)
2922 struct ni_private
*devpriv
= dev
->private;
2930 for (i
= 0; i
< boardtype
.n_aochan
; ++i
) {
2931 devpriv
->ao_conf
[i
] &= ~MSeries_AO_Update_Timed_Bit
;
2932 ni_writeb(devpriv
->ao_conf
[i
],
2933 M_Offset_AO_Config_Bank(i
));
2934 ni_writeb(0xf, M_Offset_AO_Waveform_Order(i
));
2937 for (i
= 0; i
< n_chans
; i
++) {
2938 const struct comedi_krange
*krange
;
2939 chan
= CR_CHAN(chanspec
[i
]);
2940 range
= CR_RANGE(chanspec
[i
]);
2941 krange
= s
->range_table
->range
+ range
;
2944 switch (krange
->max
- krange
->min
) {
2946 conf
|= MSeries_AO_DAC_Reference_10V_Internal_Bits
;
2947 ni_writeb(0, M_Offset_AO_Reference_Attenuation(chan
));
2950 conf
|= MSeries_AO_DAC_Reference_5V_Internal_Bits
;
2951 ni_writeb(0, M_Offset_AO_Reference_Attenuation(chan
));
2954 conf
|= MSeries_AO_DAC_Reference_10V_Internal_Bits
;
2955 ni_writeb(MSeries_Attenuate_x5_Bit
,
2956 M_Offset_AO_Reference_Attenuation(chan
));
2959 conf
|= MSeries_AO_DAC_Reference_5V_Internal_Bits
;
2960 ni_writeb(MSeries_Attenuate_x5_Bit
,
2961 M_Offset_AO_Reference_Attenuation(chan
));
2964 printk("%s: bug! unhandled ao reference voltage\n",
2968 switch (krange
->max
+ krange
->min
) {
2970 conf
|= MSeries_AO_DAC_Offset_0V_Bits
;
2973 conf
|= MSeries_AO_DAC_Offset_5V_Bits
;
2976 printk("%s: bug! unhandled ao offset voltage\n",
2981 conf
|= MSeries_AO_Update_Timed_Bit
;
2982 ni_writeb(conf
, M_Offset_AO_Config_Bank(chan
));
2983 devpriv
->ao_conf
[chan
] = conf
;
2984 ni_writeb(i
, M_Offset_AO_Waveform_Order(chan
));
2989 static int ni_old_ao_config_chanlist(struct comedi_device
*dev
,
2990 struct comedi_subdevice
*s
,
2991 unsigned int chanspec
[],
2992 unsigned int n_chans
)
2994 struct ni_private
*devpriv
= dev
->private;
3001 for (i
= 0; i
< n_chans
; i
++) {
3002 chan
= CR_CHAN(chanspec
[i
]);
3003 range
= CR_RANGE(chanspec
[i
]);
3004 conf
= AO_Channel(chan
);
3006 if (boardtype
.ao_unipolar
) {
3007 if ((range
& 1) == 0) {
3009 invert
= (1 << (boardtype
.aobits
- 1));
3017 invert
= (1 << (boardtype
.aobits
- 1));
3020 /* not all boards can deglitch, but this shouldn't hurt */
3021 if (chanspec
[i
] & CR_DEGLITCH
)
3022 conf
|= AO_Deglitch
;
3024 /* analog reference */
3025 /* AREF_OTHER connects AO ground to AI ground, i think */
3026 conf
|= (CR_AREF(chanspec
[i
]) ==
3027 AREF_OTHER
) ? AO_Ground_Ref
: 0;
3029 ni_writew(conf
, AO_Configuration
);
3030 devpriv
->ao_conf
[chan
] = conf
;
3035 static int ni_ao_config_chanlist(struct comedi_device
*dev
,
3036 struct comedi_subdevice
*s
,
3037 unsigned int chanspec
[], unsigned int n_chans
,
3040 if (boardtype
.reg_type
& ni_reg_m_series_mask
)
3041 return ni_m_series_ao_config_chanlist(dev
, s
, chanspec
, n_chans
,
3044 return ni_old_ao_config_chanlist(dev
, s
, chanspec
, n_chans
);
3047 static int ni_ao_insn_read(struct comedi_device
*dev
,
3048 struct comedi_subdevice
*s
, struct comedi_insn
*insn
,
3051 struct ni_private
*devpriv
= dev
->private;
3053 data
[0] = devpriv
->ao
[CR_CHAN(insn
->chanspec
)];
3058 static int ni_ao_insn_write(struct comedi_device
*dev
,
3059 struct comedi_subdevice
*s
,
3060 struct comedi_insn
*insn
, unsigned int *data
)
3062 struct ni_private
*devpriv
= dev
->private;
3063 unsigned int chan
= CR_CHAN(insn
->chanspec
);
3064 unsigned int invert
;
3066 invert
= ni_ao_config_chanlist(dev
, s
, &insn
->chanspec
, 1, 0);
3068 devpriv
->ao
[chan
] = data
[0];
3070 if (boardtype
.reg_type
& ni_reg_m_series_mask
) {
3071 ni_writew(data
[0], M_Offset_DAC_Direct_Data(chan
));
3073 ni_writew(data
[0] ^ invert
,
3074 (chan
) ? DAC1_Direct_Data
: DAC0_Direct_Data
);
3079 static int ni_ao_insn_write_671x(struct comedi_device
*dev
,
3080 struct comedi_subdevice
*s
,
3081 struct comedi_insn
*insn
, unsigned int *data
)
3083 struct ni_private
*devpriv
= dev
->private;
3084 unsigned int chan
= CR_CHAN(insn
->chanspec
);
3085 unsigned int invert
;
3087 ao_win_out(1 << chan
, AO_Immediate_671x
);
3088 invert
= 1 << (boardtype
.aobits
- 1);
3090 ni_ao_config_chanlist(dev
, s
, &insn
->chanspec
, 1, 0);
3092 devpriv
->ao
[chan
] = data
[0];
3093 ao_win_out(data
[0] ^ invert
, DACx_Direct_Data_671x(chan
));
3098 static int ni_ao_insn_config(struct comedi_device
*dev
,
3099 struct comedi_subdevice
*s
,
3100 struct comedi_insn
*insn
, unsigned int *data
)
3102 struct ni_private
*devpriv
= dev
->private;
3105 case INSN_CONFIG_GET_HARDWARE_BUFFER_SIZE
:
3108 data
[2] = 1 + boardtype
.ao_fifo_depth
* sizeof(short);
3110 data
[2] += devpriv
->mite
->fifo_size
;
3127 static int ni_ao_inttrig(struct comedi_device
*dev
, struct comedi_subdevice
*s
,
3128 unsigned int trignum
)
3130 struct ni_private
*devpriv
= dev
->private;
3132 int interrupt_b_bits
;
3134 static const int timeout
= 1000;
3139 /* Null trig at beginning prevent ao start trigger from executing more than
3140 once per command (and doing things like trying to allocate the ao dma channel
3142 s
->async
->inttrig
= NULL
;
3144 ni_set_bits(dev
, Interrupt_B_Enable_Register
,
3145 AO_FIFO_Interrupt_Enable
| AO_Error_Interrupt_Enable
, 0);
3146 interrupt_b_bits
= AO_Error_Interrupt_Enable
;
3148 devpriv
->stc_writew(dev
, 1, DAC_FIFO_Clear
);
3149 if (boardtype
.reg_type
& ni_reg_6xxx_mask
)
3150 ni_ao_win_outl(dev
, 0x6, AO_FIFO_Offset_Load_611x
);
3151 ret
= ni_ao_setup_MITE_dma(dev
);
3154 ret
= ni_ao_wait_for_dma_load(dev
);
3158 ret
= ni_ao_prep_fifo(dev
, s
);
3162 interrupt_b_bits
|= AO_FIFO_Interrupt_Enable
;
3165 devpriv
->stc_writew(dev
, devpriv
->ao_mode3
| AO_Not_An_UPDATE
,
3166 AO_Mode_3_Register
);
3167 devpriv
->stc_writew(dev
, devpriv
->ao_mode3
, AO_Mode_3_Register
);
3168 /* wait for DACs to be loaded */
3169 for (i
= 0; i
< timeout
; i
++) {
3171 if ((devpriv
->stc_readw(dev
,
3172 Joint_Status_2_Register
) &
3173 AO_TMRDACWRs_In_Progress_St
) == 0)
3178 "timed out waiting for AO_TMRDACWRs_In_Progress_St to clear");
3181 /* stc manual says we are need to clear error interrupt after AO_TMRDACWRs_In_Progress_St clears */
3182 devpriv
->stc_writew(dev
, AO_Error_Interrupt_Ack
,
3183 Interrupt_B_Ack_Register
);
3185 ni_set_bits(dev
, Interrupt_B_Enable_Register
, interrupt_b_bits
, 1);
3187 devpriv
->stc_writew(dev
,
3188 devpriv
->ao_cmd1
| AO_UI_Arm
| AO_UC_Arm
| AO_BC_Arm
3189 | AO_DAC1_Update_Mode
| AO_DAC0_Update_Mode
,
3190 AO_Command_1_Register
);
3192 devpriv
->stc_writew(dev
, devpriv
->ao_cmd2
| AO_START1_Pulse
,
3193 AO_Command_2_Register
);
3198 static int ni_ao_cmd(struct comedi_device
*dev
, struct comedi_subdevice
*s
)
3200 struct ni_private
*devpriv
= dev
->private;
3201 const struct comedi_cmd
*cmd
= &s
->async
->cmd
;
3206 if (dev
->irq
== 0) {
3207 comedi_error(dev
, "cannot run command without an irq");
3211 devpriv
->stc_writew(dev
, AO_Configuration_Start
, Joint_Reset_Register
);
3213 devpriv
->stc_writew(dev
, AO_Disarm
, AO_Command_1_Register
);
3215 if (boardtype
.reg_type
& ni_reg_6xxx_mask
) {
3216 ao_win_out(CLEAR_WG
, AO_Misc_611x
);
3219 for (i
= 0; i
< cmd
->chanlist_len
; i
++) {
3222 chan
= CR_CHAN(cmd
->chanlist
[i
]);
3224 ao_win_out(chan
, AO_Waveform_Generation_611x
);
3226 ao_win_out(bits
, AO_Timed_611x
);
3229 ni_ao_config_chanlist(dev
, s
, cmd
->chanlist
, cmd
->chanlist_len
, 1);
3231 if (cmd
->stop_src
== TRIG_NONE
) {
3232 devpriv
->ao_mode1
|= AO_Continuous
;
3233 devpriv
->ao_mode1
&= ~AO_Trigger_Once
;
3235 devpriv
->ao_mode1
&= ~AO_Continuous
;
3236 devpriv
->ao_mode1
|= AO_Trigger_Once
;
3238 devpriv
->stc_writew(dev
, devpriv
->ao_mode1
, AO_Mode_1_Register
);
3239 switch (cmd
->start_src
) {
3242 devpriv
->ao_trigger_select
&=
3243 ~(AO_START1_Polarity
| AO_START1_Select(-1));
3244 devpriv
->ao_trigger_select
|= AO_START1_Edge
| AO_START1_Sync
;
3245 devpriv
->stc_writew(dev
, devpriv
->ao_trigger_select
,
3246 AO_Trigger_Select_Register
);
3249 devpriv
->ao_trigger_select
=
3250 AO_START1_Select(CR_CHAN(cmd
->start_arg
) + 1);
3251 if (cmd
->start_arg
& CR_INVERT
)
3252 devpriv
->ao_trigger_select
|= AO_START1_Polarity
; /* 0=active high, 1=active low. see daq-stc 3-24 (p186) */
3253 if (cmd
->start_arg
& CR_EDGE
)
3254 devpriv
->ao_trigger_select
|= AO_START1_Edge
; /* 0=edge detection disabled, 1=enabled */
3255 devpriv
->stc_writew(dev
, devpriv
->ao_trigger_select
,
3256 AO_Trigger_Select_Register
);
3262 devpriv
->ao_mode3
&= ~AO_Trigger_Length
;
3263 devpriv
->stc_writew(dev
, devpriv
->ao_mode3
, AO_Mode_3_Register
);
3265 devpriv
->stc_writew(dev
, devpriv
->ao_mode1
, AO_Mode_1_Register
);
3266 devpriv
->ao_mode2
&= ~AO_BC_Initial_Load_Source
;
3267 devpriv
->stc_writew(dev
, devpriv
->ao_mode2
, AO_Mode_2_Register
);
3268 if (cmd
->stop_src
== TRIG_NONE
) {
3269 devpriv
->stc_writel(dev
, 0xffffff, AO_BC_Load_A_Register
);
3271 devpriv
->stc_writel(dev
, 0, AO_BC_Load_A_Register
);
3273 devpriv
->stc_writew(dev
, AO_BC_Load
, AO_Command_1_Register
);
3274 devpriv
->ao_mode2
&= ~AO_UC_Initial_Load_Source
;
3275 devpriv
->stc_writew(dev
, devpriv
->ao_mode2
, AO_Mode_2_Register
);
3276 switch (cmd
->stop_src
) {
3278 if (boardtype
.reg_type
& ni_reg_m_series_mask
) {
3279 /* this is how the NI example code does it for m-series boards, verified correct with 6259 */
3280 devpriv
->stc_writel(dev
, cmd
->stop_arg
- 1,
3281 AO_UC_Load_A_Register
);
3282 devpriv
->stc_writew(dev
, AO_UC_Load
,
3283 AO_Command_1_Register
);
3285 devpriv
->stc_writel(dev
, cmd
->stop_arg
,
3286 AO_UC_Load_A_Register
);
3287 devpriv
->stc_writew(dev
, AO_UC_Load
,
3288 AO_Command_1_Register
);
3289 devpriv
->stc_writel(dev
, cmd
->stop_arg
- 1,
3290 AO_UC_Load_A_Register
);
3294 devpriv
->stc_writel(dev
, 0xffffff, AO_UC_Load_A_Register
);
3295 devpriv
->stc_writew(dev
, AO_UC_Load
, AO_Command_1_Register
);
3296 devpriv
->stc_writel(dev
, 0xffffff, AO_UC_Load_A_Register
);
3299 devpriv
->stc_writel(dev
, 0, AO_UC_Load_A_Register
);
3300 devpriv
->stc_writew(dev
, AO_UC_Load
, AO_Command_1_Register
);
3301 devpriv
->stc_writel(dev
, cmd
->stop_arg
, AO_UC_Load_A_Register
);
3304 devpriv
->ao_mode1
&=
3305 ~(AO_UI_Source_Select(0x1f) | AO_UI_Source_Polarity
|
3306 AO_UPDATE_Source_Select(0x1f) | AO_UPDATE_Source_Polarity
);
3307 switch (cmd
->scan_begin_src
) {
3309 devpriv
->ao_cmd2
&= ~AO_BC_Gate_Enable
;
3311 ni_ns_to_timer(dev
, cmd
->scan_begin_arg
,
3312 TRIG_ROUND_NEAREST
);
3313 devpriv
->stc_writel(dev
, 1, AO_UI_Load_A_Register
);
3314 devpriv
->stc_writew(dev
, AO_UI_Load
, AO_Command_1_Register
);
3315 devpriv
->stc_writel(dev
, trigvar
, AO_UI_Load_A_Register
);
3318 devpriv
->ao_mode1
|=
3319 AO_UPDATE_Source_Select(cmd
->scan_begin_arg
);
3320 if (cmd
->scan_begin_arg
& CR_INVERT
)
3321 devpriv
->ao_mode1
|= AO_UPDATE_Source_Polarity
;
3322 devpriv
->ao_cmd2
|= AO_BC_Gate_Enable
;
3328 devpriv
->stc_writew(dev
, devpriv
->ao_cmd2
, AO_Command_2_Register
);
3329 devpriv
->stc_writew(dev
, devpriv
->ao_mode1
, AO_Mode_1_Register
);
3330 devpriv
->ao_mode2
&=
3331 ~(AO_UI_Reload_Mode(3) | AO_UI_Initial_Load_Source
);
3332 devpriv
->stc_writew(dev
, devpriv
->ao_mode2
, AO_Mode_2_Register
);
3334 if (cmd
->scan_end_arg
> 1) {
3335 devpriv
->ao_mode1
|= AO_Multiple_Channels
;
3336 devpriv
->stc_writew(dev
,
3337 AO_Number_Of_Channels(cmd
->scan_end_arg
-
3339 AO_UPDATE_Output_Select
3340 (AO_Update_Output_High_Z
),
3341 AO_Output_Control_Register
);
3344 devpriv
->ao_mode1
&= ~AO_Multiple_Channels
;
3345 bits
= AO_UPDATE_Output_Select(AO_Update_Output_High_Z
);
3347 reg_type
& (ni_reg_m_series_mask
| ni_reg_6xxx_mask
)) {
3348 bits
|= AO_Number_Of_Channels(0);
3351 AO_Number_Of_Channels(CR_CHAN(cmd
->chanlist
[0]));
3353 devpriv
->stc_writew(dev
, bits
, AO_Output_Control_Register
);
3355 devpriv
->stc_writew(dev
, devpriv
->ao_mode1
, AO_Mode_1_Register
);
3357 devpriv
->stc_writew(dev
, AO_DAC0_Update_Mode
| AO_DAC1_Update_Mode
,
3358 AO_Command_1_Register
);
3360 devpriv
->ao_mode3
|= AO_Stop_On_Overrun_Error
;
3361 devpriv
->stc_writew(dev
, devpriv
->ao_mode3
, AO_Mode_3_Register
);
3363 devpriv
->ao_mode2
&= ~AO_FIFO_Mode_Mask
;
3365 devpriv
->ao_mode2
|= AO_FIFO_Mode_HF_to_F
;
3367 devpriv
->ao_mode2
|= AO_FIFO_Mode_HF
;
3369 devpriv
->ao_mode2
&= ~AO_FIFO_Retransmit_Enable
;
3370 devpriv
->stc_writew(dev
, devpriv
->ao_mode2
, AO_Mode_2_Register
);
3372 bits
= AO_BC_Source_Select
| AO_UPDATE_Pulse_Width
|
3373 AO_TMRDACWR_Pulse_Width
;
3374 if (boardtype
.ao_fifo_depth
)
3375 bits
|= AO_FIFO_Enable
;
3377 bits
|= AO_DMA_PIO_Control
;
3379 /* F Hess: windows driver does not set AO_Number_Of_DAC_Packages bit for 6281,
3380 verified with bus analyzer. */
3381 if (boardtype
.reg_type
& ni_reg_m_series_mask
)
3382 bits
|= AO_Number_Of_DAC_Packages
;
3384 devpriv
->stc_writew(dev
, bits
, AO_Personal_Register
);
3385 /* enable sending of ao dma requests */
3386 devpriv
->stc_writew(dev
, AO_AOFREQ_Enable
, AO_Start_Select_Register
);
3388 devpriv
->stc_writew(dev
, AO_Configuration_End
, Joint_Reset_Register
);
3390 if (cmd
->stop_src
== TRIG_COUNT
) {
3391 devpriv
->stc_writew(dev
, AO_BC_TC_Interrupt_Ack
,
3392 Interrupt_B_Ack_Register
);
3393 ni_set_bits(dev
, Interrupt_B_Enable_Register
,
3394 AO_BC_TC_Interrupt_Enable
, 1);
3397 s
->async
->inttrig
= &ni_ao_inttrig
;
3402 static int ni_ao_cmdtest(struct comedi_device
*dev
, struct comedi_subdevice
*s
,
3403 struct comedi_cmd
*cmd
)
3405 struct ni_private
*devpriv
= dev
->private;
3409 /* Step 1 : check if triggers are trivially valid */
3411 if ((cmd
->flags
& CMDF_WRITE
) == 0)
3412 cmd
->flags
|= CMDF_WRITE
;
3414 err
|= cfc_check_trigger_src(&cmd
->start_src
, TRIG_INT
| TRIG_EXT
);
3415 err
|= cfc_check_trigger_src(&cmd
->scan_begin_src
,
3416 TRIG_TIMER
| TRIG_EXT
);
3417 err
|= cfc_check_trigger_src(&cmd
->convert_src
, TRIG_NOW
);
3418 err
|= cfc_check_trigger_src(&cmd
->scan_end_src
, TRIG_COUNT
);
3419 err
|= cfc_check_trigger_src(&cmd
->stop_src
, TRIG_COUNT
| TRIG_NONE
);
3424 /* Step 2a : make sure trigger sources are unique */
3426 err
|= cfc_check_trigger_is_unique(cmd
->start_src
);
3427 err
|= cfc_check_trigger_is_unique(cmd
->scan_begin_src
);
3428 err
|= cfc_check_trigger_is_unique(cmd
->stop_src
);
3430 /* Step 2b : and mutually compatible */
3435 /* step 3: make sure arguments are trivially compatible */
3437 if (cmd
->start_src
== TRIG_EXT
) {
3438 /* external trigger */
3439 unsigned int tmp
= CR_CHAN(cmd
->start_arg
);
3443 tmp
|= (cmd
->start_arg
& (CR_INVERT
| CR_EDGE
));
3444 if (cmd
->start_arg
!= tmp
) {
3445 cmd
->start_arg
= tmp
;
3449 if (cmd
->start_arg
!= 0) {
3450 /* true for both TRIG_NOW and TRIG_INT */
3455 if (cmd
->scan_begin_src
== TRIG_TIMER
) {
3456 if (cmd
->scan_begin_arg
< boardtype
.ao_speed
) {
3457 cmd
->scan_begin_arg
= boardtype
.ao_speed
;
3460 if (cmd
->scan_begin_arg
> devpriv
->clock_ns
* 0xffffff) { /* XXX check */
3461 cmd
->scan_begin_arg
= devpriv
->clock_ns
* 0xffffff;
3465 if (cmd
->convert_arg
!= 0) {
3466 cmd
->convert_arg
= 0;
3469 if (cmd
->scan_end_arg
!= cmd
->chanlist_len
) {
3470 cmd
->scan_end_arg
= cmd
->chanlist_len
;
3473 if (cmd
->stop_src
== TRIG_COUNT
) { /* XXX check */
3474 if (cmd
->stop_arg
> 0x00ffffff) {
3475 cmd
->stop_arg
= 0x00ffffff;
3480 if (cmd
->stop_arg
!= 0) {
3489 /* step 4: fix up any arguments */
3490 if (cmd
->scan_begin_src
== TRIG_TIMER
) {
3491 tmp
= cmd
->scan_begin_arg
;
3492 cmd
->scan_begin_arg
=
3493 ni_timer_to_ns(dev
, ni_ns_to_timer(dev
,
3494 cmd
->scan_begin_arg
,
3498 if (tmp
!= cmd
->scan_begin_arg
)
3504 /* step 5: fix up chanlist */
3512 static int ni_ao_reset(struct comedi_device
*dev
, struct comedi_subdevice
*s
)
3514 struct ni_private
*devpriv
= dev
->private;
3516 /* devpriv->ao0p=0x0000; */
3517 /* ni_writew(devpriv->ao0p,AO_Configuration); */
3519 /* devpriv->ao1p=AO_Channel(1); */
3520 /* ni_writew(devpriv->ao1p,AO_Configuration); */
3522 ni_release_ao_mite_channel(dev
);
3524 devpriv
->stc_writew(dev
, AO_Configuration_Start
, Joint_Reset_Register
);
3525 devpriv
->stc_writew(dev
, AO_Disarm
, AO_Command_1_Register
);
3526 ni_set_bits(dev
, Interrupt_B_Enable_Register
, ~0, 0);
3527 devpriv
->stc_writew(dev
, AO_BC_Source_Select
, AO_Personal_Register
);
3528 devpriv
->stc_writew(dev
, 0x3f98, Interrupt_B_Ack_Register
);
3529 devpriv
->stc_writew(dev
, AO_BC_Source_Select
| AO_UPDATE_Pulse_Width
|
3530 AO_TMRDACWR_Pulse_Width
, AO_Personal_Register
);
3531 devpriv
->stc_writew(dev
, 0, AO_Output_Control_Register
);
3532 devpriv
->stc_writew(dev
, 0, AO_Start_Select_Register
);
3533 devpriv
->ao_cmd1
= 0;
3534 devpriv
->stc_writew(dev
, devpriv
->ao_cmd1
, AO_Command_1_Register
);
3535 devpriv
->ao_cmd2
= 0;
3536 devpriv
->stc_writew(dev
, devpriv
->ao_cmd2
, AO_Command_2_Register
);
3537 devpriv
->ao_mode1
= 0;
3538 devpriv
->stc_writew(dev
, devpriv
->ao_mode1
, AO_Mode_1_Register
);
3539 devpriv
->ao_mode2
= 0;
3540 devpriv
->stc_writew(dev
, devpriv
->ao_mode2
, AO_Mode_2_Register
);
3541 if (boardtype
.reg_type
& ni_reg_m_series_mask
)
3542 devpriv
->ao_mode3
= AO_Last_Gate_Disable
;
3544 devpriv
->ao_mode3
= 0;
3545 devpriv
->stc_writew(dev
, devpriv
->ao_mode3
, AO_Mode_3_Register
);
3546 devpriv
->ao_trigger_select
= 0;
3547 devpriv
->stc_writew(dev
, devpriv
->ao_trigger_select
,
3548 AO_Trigger_Select_Register
);
3549 if (boardtype
.reg_type
& ni_reg_6xxx_mask
) {
3550 unsigned immediate_bits
= 0;
3552 for (i
= 0; i
< s
->n_chan
; ++i
) {
3553 immediate_bits
|= 1 << i
;
3555 ao_win_out(immediate_bits
, AO_Immediate_671x
);
3556 ao_win_out(CLEAR_WG
, AO_Misc_611x
);
3558 devpriv
->stc_writew(dev
, AO_Configuration_End
, Joint_Reset_Register
);
3565 static int ni_dio_insn_config(struct comedi_device
*dev
,
3566 struct comedi_subdevice
*s
,
3567 struct comedi_insn
*insn
, unsigned int *data
)
3569 struct ni_private
*devpriv
= dev
->private;
3572 printk("ni_dio_insn_config() chan=%d io=%d\n",
3573 CR_CHAN(insn
->chanspec
), data
[0]);
3576 case INSN_CONFIG_DIO_OUTPUT
:
3577 s
->io_bits
|= 1 << CR_CHAN(insn
->chanspec
);
3579 case INSN_CONFIG_DIO_INPUT
:
3580 s
->io_bits
&= ~(1 << CR_CHAN(insn
->chanspec
));
3582 case INSN_CONFIG_DIO_QUERY
:
3585 io_bits
& (1 << CR_CHAN(insn
->chanspec
))) ? COMEDI_OUTPUT
:
3593 devpriv
->dio_control
&= ~DIO_Pins_Dir_Mask
;
3594 devpriv
->dio_control
|= DIO_Pins_Dir(s
->io_bits
);
3595 devpriv
->stc_writew(dev
, devpriv
->dio_control
, DIO_Control_Register
);
3600 static int ni_dio_insn_bits(struct comedi_device
*dev
,
3601 struct comedi_subdevice
*s
,
3602 struct comedi_insn
*insn
, unsigned int *data
)
3604 struct ni_private
*devpriv
= dev
->private;
3607 printk("ni_dio_insn_bits() mask=0x%x bits=0x%x\n", data
[0], data
[1]);
3611 /* Perform check to make sure we're not using the
3612 serial part of the dio */
3613 if ((data
[0] & (DIO_SDIN
| DIO_SDOUT
))
3614 && devpriv
->serial_interval_ns
)
3617 s
->state
&= ~data
[0];
3618 s
->state
|= (data
[0] & data
[1]);
3619 devpriv
->dio_output
&= ~DIO_Parallel_Data_Mask
;
3620 devpriv
->dio_output
|= DIO_Parallel_Data_Out(s
->state
);
3621 devpriv
->stc_writew(dev
, devpriv
->dio_output
,
3622 DIO_Output_Register
);
3624 data
[1] = devpriv
->stc_readw(dev
, DIO_Parallel_Input_Register
);
3629 static int ni_m_series_dio_insn_config(struct comedi_device
*dev
,
3630 struct comedi_subdevice
*s
,
3631 struct comedi_insn
*insn
,
3634 struct ni_private
*devpriv __maybe_unused
= dev
->private;
3637 printk("ni_m_series_dio_insn_config() chan=%d io=%d\n",
3638 CR_CHAN(insn
->chanspec
), data
[0]);
3641 case INSN_CONFIG_DIO_OUTPUT
:
3642 s
->io_bits
|= 1 << CR_CHAN(insn
->chanspec
);
3644 case INSN_CONFIG_DIO_INPUT
:
3645 s
->io_bits
&= ~(1 << CR_CHAN(insn
->chanspec
));
3647 case INSN_CONFIG_DIO_QUERY
:
3650 io_bits
& (1 << CR_CHAN(insn
->chanspec
))) ? COMEDI_OUTPUT
:
3658 ni_writel(s
->io_bits
, M_Offset_DIO_Direction
);
3663 static int ni_m_series_dio_insn_bits(struct comedi_device
*dev
,
3664 struct comedi_subdevice
*s
,
3665 struct comedi_insn
*insn
,
3668 struct ni_private
*devpriv __maybe_unused
= dev
->private;
3671 printk("ni_m_series_dio_insn_bits() mask=0x%x bits=0x%x\n", data
[0],
3676 s
->state
&= ~data
[0];
3677 s
->state
|= (data
[0] & data
[1]);
3678 ni_writel(s
->state
, M_Offset_Static_Digital_Output
);
3680 data
[1] = ni_readl(M_Offset_Static_Digital_Input
);
3685 static int ni_cdio_cmdtest(struct comedi_device
*dev
,
3686 struct comedi_subdevice
*s
, struct comedi_cmd
*cmd
)
3692 /* Step 1 : check if triggers are trivially valid */
3694 err
|= cfc_check_trigger_src(&cmd
->start_src
, TRIG_INT
);
3695 err
|= cfc_check_trigger_src(&cmd
->scan_begin_src
, TRIG_EXT
);
3696 err
|= cfc_check_trigger_src(&cmd
->convert_src
, TRIG_NOW
);
3697 err
|= cfc_check_trigger_src(&cmd
->scan_end_src
, TRIG_COUNT
);
3698 err
|= cfc_check_trigger_src(&cmd
->stop_src
, TRIG_NONE
);
3703 /* Step 2a : make sure trigger sources are unique */
3704 /* Step 2b : and mutually compatible */
3709 /* step 3: make sure arguments are trivially compatible */
3710 if (cmd
->start_src
== TRIG_INT
) {
3711 if (cmd
->start_arg
!= 0) {
3716 if (cmd
->scan_begin_src
== TRIG_EXT
) {
3717 tmp
= cmd
->scan_begin_arg
;
3718 tmp
&= CR_PACK_FLAGS(CDO_Sample_Source_Select_Mask
, 0, 0,
3720 if (tmp
!= cmd
->scan_begin_arg
) {
3724 if (cmd
->convert_src
== TRIG_NOW
) {
3725 if (cmd
->convert_arg
) {
3726 cmd
->convert_arg
= 0;
3731 if (cmd
->scan_end_arg
!= cmd
->chanlist_len
) {
3732 cmd
->scan_end_arg
= cmd
->chanlist_len
;
3736 if (cmd
->stop_src
== TRIG_NONE
) {
3737 if (cmd
->stop_arg
!= 0) {
3746 /* step 4: fix up any arguments */
3751 /* step 5: check chanlist */
3753 for (i
= 0; i
< cmd
->chanlist_len
; ++i
) {
3754 if (cmd
->chanlist
[i
] != i
)
3764 static int ni_cdio_cmd(struct comedi_device
*dev
, struct comedi_subdevice
*s
)
3766 struct ni_private
*devpriv __maybe_unused
= dev
->private;
3767 const struct comedi_cmd
*cmd
= &s
->async
->cmd
;
3768 unsigned cdo_mode_bits
= CDO_FIFO_Mode_Bit
| CDO_Halt_On_Error_Bit
;
3771 ni_writel(CDO_Reset_Bit
, M_Offset_CDIO_Command
);
3772 switch (cmd
->scan_begin_src
) {
3775 CR_CHAN(cmd
->scan_begin_arg
) &
3776 CDO_Sample_Source_Select_Mask
;
3782 if (cmd
->scan_begin_arg
& CR_INVERT
)
3783 cdo_mode_bits
|= CDO_Polarity_Bit
;
3784 ni_writel(cdo_mode_bits
, M_Offset_CDO_Mode
);
3786 ni_writel(s
->state
, M_Offset_CDO_FIFO_Data
);
3787 ni_writel(CDO_SW_Update_Bit
, M_Offset_CDIO_Command
);
3788 ni_writel(s
->io_bits
, M_Offset_CDO_Mask_Enable
);
3791 "attempted to run digital output command with no lines configured as outputs");
3794 retval
= ni_request_cdo_mite_channel(dev
);
3798 s
->async
->inttrig
= &ni_cdo_inttrig
;
3802 static int ni_cdo_inttrig(struct comedi_device
*dev
, struct comedi_subdevice
*s
,
3803 unsigned int trignum
)
3806 struct ni_private
*devpriv
= dev
->private;
3807 unsigned long flags
;
3811 const unsigned timeout
= 1000;
3813 s
->async
->inttrig
= NULL
;
3815 /* read alloc the entire buffer */
3816 comedi_buf_read_alloc(s
->async
, s
->async
->prealloc_bufsz
);
3819 spin_lock_irqsave(&devpriv
->mite_channel_lock
, flags
);
3820 if (devpriv
->cdo_mite_chan
) {
3821 mite_prep_dma(devpriv
->cdo_mite_chan
, 32, 32);
3822 mite_dma_arm(devpriv
->cdo_mite_chan
);
3824 comedi_error(dev
, "BUG: no cdo mite channel?");
3827 spin_unlock_irqrestore(&devpriv
->mite_channel_lock
, flags
);
3832 * XXX not sure what interrupt C group does
3833 * ni_writeb(Interrupt_Group_C_Enable_Bit,
3834 * M_Offset_Interrupt_C_Enable); wait for dma to fill output fifo
3836 for (i
= 0; i
< timeout
; ++i
) {
3837 if (ni_readl(M_Offset_CDIO_Status
) & CDO_FIFO_Full_Bit
)
3842 comedi_error(dev
, "dma failed to fill cdo fifo!");
3843 ni_cdio_cancel(dev
, s
);
3846 ni_writel(CDO_Arm_Bit
| CDO_Error_Interrupt_Enable_Set_Bit
|
3847 CDO_Empty_FIFO_Interrupt_Enable_Set_Bit
,
3848 M_Offset_CDIO_Command
);
3852 static int ni_cdio_cancel(struct comedi_device
*dev
, struct comedi_subdevice
*s
)
3854 struct ni_private
*devpriv __maybe_unused
= dev
->private;
3856 ni_writel(CDO_Disarm_Bit
| CDO_Error_Interrupt_Enable_Clear_Bit
|
3857 CDO_Empty_FIFO_Interrupt_Enable_Clear_Bit
|
3858 CDO_FIFO_Request_Interrupt_Enable_Clear_Bit
,
3859 M_Offset_CDIO_Command
);
3861 * XXX not sure what interrupt C group does ni_writeb(0,
3862 * M_Offset_Interrupt_C_Enable);
3864 ni_writel(0, M_Offset_CDO_Mask_Enable
);
3865 ni_release_cdo_mite_channel(dev
);
3869 static void handle_cdio_interrupt(struct comedi_device
*dev
)
3871 struct ni_private
*devpriv __maybe_unused
= dev
->private;
3872 unsigned cdio_status
;
3873 struct comedi_subdevice
*s
= &dev
->subdevices
[NI_DIO_SUBDEV
];
3875 unsigned long flags
;
3878 if ((boardtype
.reg_type
& ni_reg_m_series_mask
) == 0) {
3882 spin_lock_irqsave(&devpriv
->mite_channel_lock
, flags
);
3883 if (devpriv
->cdo_mite_chan
) {
3884 unsigned cdo_mite_status
=
3885 mite_get_status(devpriv
->cdo_mite_chan
);
3886 if (cdo_mite_status
& CHSR_LINKC
) {
3888 devpriv
->mite
->mite_io_addr
+
3889 MITE_CHOR(devpriv
->cdo_mite_chan
->channel
));
3891 mite_sync_output_dma(devpriv
->cdo_mite_chan
, s
->async
);
3893 spin_unlock_irqrestore(&devpriv
->mite_channel_lock
, flags
);
3896 cdio_status
= ni_readl(M_Offset_CDIO_Status
);
3897 if (cdio_status
& (CDO_Overrun_Bit
| CDO_Underflow_Bit
)) {
3898 /* printk("cdio error: statux=0x%x\n", cdio_status); */
3899 ni_writel(CDO_Error_Interrupt_Confirm_Bit
, M_Offset_CDIO_Command
); /* XXX just guessing this is needed and does something useful */
3900 s
->async
->events
|= COMEDI_CB_OVERFLOW
;
3902 if (cdio_status
& CDO_FIFO_Empty_Bit
) {
3903 /* printk("cdio fifo empty\n"); */
3904 ni_writel(CDO_Empty_FIFO_Interrupt_Enable_Clear_Bit
,
3905 M_Offset_CDIO_Command
);
3906 /* s->async->events |= COMEDI_CB_EOA; */
3911 static int ni_serial_insn_config(struct comedi_device
*dev
,
3912 struct comedi_subdevice
*s
,
3913 struct comedi_insn
*insn
, unsigned int *data
)
3915 struct ni_private
*devpriv
= dev
->private;
3917 unsigned char byte_out
, byte_in
= 0;
3923 case INSN_CONFIG_SERIAL_CLOCK
:
3926 printk("SPI serial clock Config cd\n", data
[1]);
3928 devpriv
->serial_hw_mode
= 1;
3929 devpriv
->dio_control
|= DIO_HW_Serial_Enable
;
3931 if (data
[1] == SERIAL_DISABLED
) {
3932 devpriv
->serial_hw_mode
= 0;
3933 devpriv
->dio_control
&= ~(DIO_HW_Serial_Enable
|
3934 DIO_Software_Serial_Control
);
3935 data
[1] = SERIAL_DISABLED
;
3936 devpriv
->serial_interval_ns
= data
[1];
3937 } else if (data
[1] <= SERIAL_600NS
) {
3938 /* Warning: this clock speed is too fast to reliably
3940 devpriv
->dio_control
&= ~DIO_HW_Serial_Timebase
;
3941 devpriv
->clock_and_fout
|= Slow_Internal_Timebase
;
3942 devpriv
->clock_and_fout
&= ~DIO_Serial_Out_Divide_By_2
;
3943 data
[1] = SERIAL_600NS
;
3944 devpriv
->serial_interval_ns
= data
[1];
3945 } else if (data
[1] <= SERIAL_1_2US
) {
3946 devpriv
->dio_control
&= ~DIO_HW_Serial_Timebase
;
3947 devpriv
->clock_and_fout
|= Slow_Internal_Timebase
|
3948 DIO_Serial_Out_Divide_By_2
;
3949 data
[1] = SERIAL_1_2US
;
3950 devpriv
->serial_interval_ns
= data
[1];
3951 } else if (data
[1] <= SERIAL_10US
) {
3952 devpriv
->dio_control
|= DIO_HW_Serial_Timebase
;
3953 devpriv
->clock_and_fout
|= Slow_Internal_Timebase
|
3954 DIO_Serial_Out_Divide_By_2
;
3955 /* Note: DIO_Serial_Out_Divide_By_2 only affects
3956 600ns/1.2us. If you turn divide_by_2 off with the
3957 slow clock, you will still get 10us, except then
3958 all your delays are wrong. */
3959 data
[1] = SERIAL_10US
;
3960 devpriv
->serial_interval_ns
= data
[1];
3962 devpriv
->dio_control
&= ~(DIO_HW_Serial_Enable
|
3963 DIO_Software_Serial_Control
);
3964 devpriv
->serial_hw_mode
= 0;
3965 data
[1] = (data
[1] / 1000) * 1000;
3966 devpriv
->serial_interval_ns
= data
[1];
3969 devpriv
->stc_writew(dev
, devpriv
->dio_control
,
3970 DIO_Control_Register
);
3971 devpriv
->stc_writew(dev
, devpriv
->clock_and_fout
,
3972 Clock_and_FOUT_Register
);
3977 case INSN_CONFIG_BIDIRECTIONAL_DATA
:
3979 if (devpriv
->serial_interval_ns
== 0) {
3983 byte_out
= data
[1] & 0xFF;
3985 if (devpriv
->serial_hw_mode
) {
3986 err
= ni_serial_hw_readwrite8(dev
, s
, byte_out
,
3988 } else if (devpriv
->serial_interval_ns
> 0) {
3989 err
= ni_serial_sw_readwrite8(dev
, s
, byte_out
,
3992 printk("ni_serial_insn_config: serial disabled!\n");
3997 data
[1] = byte_in
& 0xFF;
4007 static int ni_serial_hw_readwrite8(struct comedi_device
*dev
,
4008 struct comedi_subdevice
*s
,
4009 unsigned char data_out
,
4010 unsigned char *data_in
)
4012 struct ni_private
*devpriv
= dev
->private;
4013 unsigned int status1
;
4014 int err
= 0, count
= 20;
4017 printk("ni_serial_hw_readwrite8: outputting 0x%x\n", data_out
);
4020 devpriv
->dio_output
&= ~DIO_Serial_Data_Mask
;
4021 devpriv
->dio_output
|= DIO_Serial_Data_Out(data_out
);
4022 devpriv
->stc_writew(dev
, devpriv
->dio_output
, DIO_Output_Register
);
4024 status1
= devpriv
->stc_readw(dev
, Joint_Status_1_Register
);
4025 if (status1
& DIO_Serial_IO_In_Progress_St
) {
4030 devpriv
->dio_control
|= DIO_HW_Serial_Start
;
4031 devpriv
->stc_writew(dev
, devpriv
->dio_control
, DIO_Control_Register
);
4032 devpriv
->dio_control
&= ~DIO_HW_Serial_Start
;
4034 /* Wait until STC says we're done, but don't loop infinitely. */
4036 devpriv
->stc_readw(dev
,
4037 Joint_Status_1_Register
)) &
4038 DIO_Serial_IO_In_Progress_St
) {
4039 /* Delay one bit per loop */
4040 udelay((devpriv
->serial_interval_ns
+ 999) / 1000);
4043 ("ni_serial_hw_readwrite8: SPI serial I/O didn't finish in time!\n");
4049 /* Delay for last bit. This delay is absolutely necessary, because
4050 DIO_Serial_IO_In_Progress_St goes high one bit too early. */
4051 udelay((devpriv
->serial_interval_ns
+ 999) / 1000);
4053 if (data_in
!= NULL
) {
4054 *data_in
= devpriv
->stc_readw(dev
, DIO_Serial_Input_Register
);
4056 printk("ni_serial_hw_readwrite8: inputted 0x%x\n", *data_in
);
4061 devpriv
->stc_writew(dev
, devpriv
->dio_control
, DIO_Control_Register
);
4066 static int ni_serial_sw_readwrite8(struct comedi_device
*dev
,
4067 struct comedi_subdevice
*s
,
4068 unsigned char data_out
,
4069 unsigned char *data_in
)
4071 struct ni_private
*devpriv
= dev
->private;
4072 unsigned char mask
, input
= 0;
4075 printk("ni_serial_sw_readwrite8: outputting 0x%x\n", data_out
);
4078 /* Wait for one bit before transfer */
4079 udelay((devpriv
->serial_interval_ns
+ 999) / 1000);
4081 for (mask
= 0x80; mask
; mask
>>= 1) {
4082 /* Output current bit; note that we cannot touch s->state
4083 because it is a per-subdevice field, and serial is
4084 a separate subdevice from DIO. */
4085 devpriv
->dio_output
&= ~DIO_SDOUT
;
4086 if (data_out
& mask
) {
4087 devpriv
->dio_output
|= DIO_SDOUT
;
4089 devpriv
->stc_writew(dev
, devpriv
->dio_output
,
4090 DIO_Output_Register
);
4092 /* Assert SDCLK (active low, inverted), wait for half of
4093 the delay, deassert SDCLK, and wait for the other half. */
4094 devpriv
->dio_control
|= DIO_Software_Serial_Control
;
4095 devpriv
->stc_writew(dev
, devpriv
->dio_control
,
4096 DIO_Control_Register
);
4098 udelay((devpriv
->serial_interval_ns
+ 999) / 2000);
4100 devpriv
->dio_control
&= ~DIO_Software_Serial_Control
;
4101 devpriv
->stc_writew(dev
, devpriv
->dio_control
,
4102 DIO_Control_Register
);
4104 udelay((devpriv
->serial_interval_ns
+ 999) / 2000);
4106 /* Input current bit */
4107 if (devpriv
->stc_readw(dev
,
4108 DIO_Parallel_Input_Register
) & DIO_SDIN
)
4110 /* printk("DIO_P_I_R: 0x%x\n", devpriv->stc_readw(dev, DIO_Parallel_Input_Register)); */
4115 printk("ni_serial_sw_readwrite8: inputted 0x%x\n", input
);
4123 static void mio_common_detach(struct comedi_device
*dev
)
4125 struct ni_private
*devpriv
= dev
->private;
4126 struct comedi_subdevice
*s
;
4129 if (devpriv
->counter_dev
) {
4130 ni_gpct_device_destroy(devpriv
->counter_dev
);
4133 if (dev
->subdevices
&& boardtype
.has_8255
) {
4134 s
= &dev
->subdevices
[NI_8255_DIO_SUBDEV
];
4135 subdev_8255_cleanup(dev
, s
);
4139 static void init_ao_67xx(struct comedi_device
*dev
, struct comedi_subdevice
*s
)
4143 for (i
= 0; i
< s
->n_chan
; i
++) {
4144 ni_ao_win_outw(dev
, AO_Channel(i
) | 0x0,
4145 AO_Configuration_2_67xx
);
4147 ao_win_out(0x0, AO_Later_Single_Point_Updates
);
4150 static unsigned ni_gpct_to_stc_register(enum ni_gpct_register reg
)
4152 unsigned stc_register
;
4154 case NITIO_G0_Autoincrement_Reg
:
4155 stc_register
= G_Autoincrement_Register(0);
4157 case NITIO_G1_Autoincrement_Reg
:
4158 stc_register
= G_Autoincrement_Register(1);
4160 case NITIO_G0_Command_Reg
:
4161 stc_register
= G_Command_Register(0);
4163 case NITIO_G1_Command_Reg
:
4164 stc_register
= G_Command_Register(1);
4166 case NITIO_G0_HW_Save_Reg
:
4167 stc_register
= G_HW_Save_Register(0);
4169 case NITIO_G1_HW_Save_Reg
:
4170 stc_register
= G_HW_Save_Register(1);
4172 case NITIO_G0_SW_Save_Reg
:
4173 stc_register
= G_Save_Register(0);
4175 case NITIO_G1_SW_Save_Reg
:
4176 stc_register
= G_Save_Register(1);
4178 case NITIO_G0_Mode_Reg
:
4179 stc_register
= G_Mode_Register(0);
4181 case NITIO_G1_Mode_Reg
:
4182 stc_register
= G_Mode_Register(1);
4184 case NITIO_G0_LoadA_Reg
:
4185 stc_register
= G_Load_A_Register(0);
4187 case NITIO_G1_LoadA_Reg
:
4188 stc_register
= G_Load_A_Register(1);
4190 case NITIO_G0_LoadB_Reg
:
4191 stc_register
= G_Load_B_Register(0);
4193 case NITIO_G1_LoadB_Reg
:
4194 stc_register
= G_Load_B_Register(1);
4196 case NITIO_G0_Input_Select_Reg
:
4197 stc_register
= G_Input_Select_Register(0);
4199 case NITIO_G1_Input_Select_Reg
:
4200 stc_register
= G_Input_Select_Register(1);
4202 case NITIO_G01_Status_Reg
:
4203 stc_register
= G_Status_Register
;
4205 case NITIO_G01_Joint_Reset_Reg
:
4206 stc_register
= Joint_Reset_Register
;
4208 case NITIO_G01_Joint_Status1_Reg
:
4209 stc_register
= Joint_Status_1_Register
;
4211 case NITIO_G01_Joint_Status2_Reg
:
4212 stc_register
= Joint_Status_2_Register
;
4214 case NITIO_G0_Interrupt_Acknowledge_Reg
:
4215 stc_register
= Interrupt_A_Ack_Register
;
4217 case NITIO_G1_Interrupt_Acknowledge_Reg
:
4218 stc_register
= Interrupt_B_Ack_Register
;
4220 case NITIO_G0_Status_Reg
:
4221 stc_register
= AI_Status_1_Register
;
4223 case NITIO_G1_Status_Reg
:
4224 stc_register
= AO_Status_1_Register
;
4226 case NITIO_G0_Interrupt_Enable_Reg
:
4227 stc_register
= Interrupt_A_Enable_Register
;
4229 case NITIO_G1_Interrupt_Enable_Reg
:
4230 stc_register
= Interrupt_B_Enable_Register
;
4233 printk("%s: unhandled register 0x%x in switch.\n",
4239 return stc_register
;
4242 static void ni_gpct_write_register(struct ni_gpct
*counter
, unsigned bits
,
4243 enum ni_gpct_register reg
)
4245 struct comedi_device
*dev
= counter
->counter_dev
->dev
;
4246 struct ni_private
*devpriv
= dev
->private;
4247 unsigned stc_register
;
4248 /* bits in the join reset register which are relevant to counters */
4249 static const unsigned gpct_joint_reset_mask
= G0_Reset
| G1_Reset
;
4250 static const unsigned gpct_interrupt_a_enable_mask
=
4251 G0_Gate_Interrupt_Enable
| G0_TC_Interrupt_Enable
;
4252 static const unsigned gpct_interrupt_b_enable_mask
=
4253 G1_Gate_Interrupt_Enable
| G1_TC_Interrupt_Enable
;
4256 /* m-series-only registers */
4257 case NITIO_G0_Counting_Mode_Reg
:
4258 ni_writew(bits
, M_Offset_G0_Counting_Mode
);
4260 case NITIO_G1_Counting_Mode_Reg
:
4261 ni_writew(bits
, M_Offset_G1_Counting_Mode
);
4263 case NITIO_G0_Second_Gate_Reg
:
4264 ni_writew(bits
, M_Offset_G0_Second_Gate
);
4266 case NITIO_G1_Second_Gate_Reg
:
4267 ni_writew(bits
, M_Offset_G1_Second_Gate
);
4269 case NITIO_G0_DMA_Config_Reg
:
4270 ni_writew(bits
, M_Offset_G0_DMA_Config
);
4272 case NITIO_G1_DMA_Config_Reg
:
4273 ni_writew(bits
, M_Offset_G1_DMA_Config
);
4275 case NITIO_G0_ABZ_Reg
:
4276 ni_writew(bits
, M_Offset_G0_MSeries_ABZ
);
4278 case NITIO_G1_ABZ_Reg
:
4279 ni_writew(bits
, M_Offset_G1_MSeries_ABZ
);
4282 /* 32 bit registers */
4283 case NITIO_G0_LoadA_Reg
:
4284 case NITIO_G1_LoadA_Reg
:
4285 case NITIO_G0_LoadB_Reg
:
4286 case NITIO_G1_LoadB_Reg
:
4287 stc_register
= ni_gpct_to_stc_register(reg
);
4288 devpriv
->stc_writel(dev
, bits
, stc_register
);
4291 /* 16 bit registers */
4292 case NITIO_G0_Interrupt_Enable_Reg
:
4293 BUG_ON(bits
& ~gpct_interrupt_a_enable_mask
);
4294 ni_set_bitfield(dev
, Interrupt_A_Enable_Register
,
4295 gpct_interrupt_a_enable_mask
, bits
);
4297 case NITIO_G1_Interrupt_Enable_Reg
:
4298 BUG_ON(bits
& ~gpct_interrupt_b_enable_mask
);
4299 ni_set_bitfield(dev
, Interrupt_B_Enable_Register
,
4300 gpct_interrupt_b_enable_mask
, bits
);
4302 case NITIO_G01_Joint_Reset_Reg
:
4303 BUG_ON(bits
& ~gpct_joint_reset_mask
);
4306 stc_register
= ni_gpct_to_stc_register(reg
);
4307 devpriv
->stc_writew(dev
, bits
, stc_register
);
4311 static unsigned ni_gpct_read_register(struct ni_gpct
*counter
,
4312 enum ni_gpct_register reg
)
4314 struct comedi_device
*dev
= counter
->counter_dev
->dev
;
4315 struct ni_private
*devpriv
= dev
->private;
4316 unsigned stc_register
;
4319 /* m-series only registers */
4320 case NITIO_G0_DMA_Status_Reg
:
4321 return ni_readw(M_Offset_G0_DMA_Status
);
4323 case NITIO_G1_DMA_Status_Reg
:
4324 return ni_readw(M_Offset_G1_DMA_Status
);
4327 /* 32 bit registers */
4328 case NITIO_G0_HW_Save_Reg
:
4329 case NITIO_G1_HW_Save_Reg
:
4330 case NITIO_G0_SW_Save_Reg
:
4331 case NITIO_G1_SW_Save_Reg
:
4332 stc_register
= ni_gpct_to_stc_register(reg
);
4333 return devpriv
->stc_readl(dev
, stc_register
);
4336 /* 16 bit registers */
4338 stc_register
= ni_gpct_to_stc_register(reg
);
4339 return devpriv
->stc_readw(dev
, stc_register
);
4345 static int ni_freq_out_insn_read(struct comedi_device
*dev
,
4346 struct comedi_subdevice
*s
,
4347 struct comedi_insn
*insn
, unsigned int *data
)
4349 struct ni_private
*devpriv
= dev
->private;
4351 data
[0] = devpriv
->clock_and_fout
& FOUT_Divider_mask
;
4355 static int ni_freq_out_insn_write(struct comedi_device
*dev
,
4356 struct comedi_subdevice
*s
,
4357 struct comedi_insn
*insn
, unsigned int *data
)
4359 struct ni_private
*devpriv
= dev
->private;
4361 devpriv
->clock_and_fout
&= ~FOUT_Enable
;
4362 devpriv
->stc_writew(dev
, devpriv
->clock_and_fout
,
4363 Clock_and_FOUT_Register
);
4364 devpriv
->clock_and_fout
&= ~FOUT_Divider_mask
;
4365 devpriv
->clock_and_fout
|= FOUT_Divider(data
[0]);
4366 devpriv
->clock_and_fout
|= FOUT_Enable
;
4367 devpriv
->stc_writew(dev
, devpriv
->clock_and_fout
,
4368 Clock_and_FOUT_Register
);
4372 static int ni_set_freq_out_clock(struct comedi_device
*dev
,
4373 unsigned int clock_source
)
4375 struct ni_private
*devpriv
= dev
->private;
4377 switch (clock_source
) {
4378 case NI_FREQ_OUT_TIMEBASE_1_DIV_2_CLOCK_SRC
:
4379 devpriv
->clock_and_fout
&= ~FOUT_Timebase_Select
;
4381 case NI_FREQ_OUT_TIMEBASE_2_CLOCK_SRC
:
4382 devpriv
->clock_and_fout
|= FOUT_Timebase_Select
;
4387 devpriv
->stc_writew(dev
, devpriv
->clock_and_fout
,
4388 Clock_and_FOUT_Register
);
4392 static void ni_get_freq_out_clock(struct comedi_device
*dev
,
4393 unsigned int *clock_source
,
4394 unsigned int *clock_period_ns
)
4396 struct ni_private
*devpriv
= dev
->private;
4398 if (devpriv
->clock_and_fout
& FOUT_Timebase_Select
) {
4399 *clock_source
= NI_FREQ_OUT_TIMEBASE_2_CLOCK_SRC
;
4400 *clock_period_ns
= TIMEBASE_2_NS
;
4402 *clock_source
= NI_FREQ_OUT_TIMEBASE_1_DIV_2_CLOCK_SRC
;
4403 *clock_period_ns
= TIMEBASE_1_NS
* 2;
4407 static int ni_freq_out_insn_config(struct comedi_device
*dev
,
4408 struct comedi_subdevice
*s
,
4409 struct comedi_insn
*insn
, unsigned int *data
)
4412 case INSN_CONFIG_SET_CLOCK_SRC
:
4413 return ni_set_freq_out_clock(dev
, data
[1]);
4415 case INSN_CONFIG_GET_CLOCK_SRC
:
4416 ni_get_freq_out_clock(dev
, &data
[1], &data
[2]);
4424 static int ni_alloc_private(struct comedi_device
*dev
)
4426 struct ni_private
*devpriv
;
4428 devpriv
= kzalloc(sizeof(*devpriv
), GFP_KERNEL
);
4431 dev
->private = devpriv
;
4433 spin_lock_init(&devpriv
->window_lock
);
4434 spin_lock_init(&devpriv
->soft_reg_copy_lock
);
4435 spin_lock_init(&devpriv
->mite_channel_lock
);
4440 static int ni_E_init(struct comedi_device
*dev
)
4442 struct ni_private
*devpriv
= dev
->private;
4443 struct comedi_subdevice
*s
;
4445 enum ni_gpct_variant counter_variant
;
4448 if (boardtype
.n_aochan
> MAX_N_AO_CHAN
) {
4449 printk("bug! boardtype.n_aochan > MAX_N_AO_CHAN\n");
4453 ret
= comedi_alloc_subdevices(dev
, NI_NUM_SUBDEVICES
);
4457 /* analog input subdevice */
4459 s
= &dev
->subdevices
[NI_AI_SUBDEV
];
4460 dev
->read_subdev
= s
;
4461 if (boardtype
.n_adchan
) {
4462 s
->type
= COMEDI_SUBD_AI
;
4464 SDF_READABLE
| SDF_DIFF
| SDF_DITHER
| SDF_CMD_READ
;
4465 if (boardtype
.reg_type
!= ni_reg_611x
)
4466 s
->subdev_flags
|= SDF_GROUND
| SDF_COMMON
| SDF_OTHER
;
4467 if (boardtype
.adbits
> 16)
4468 s
->subdev_flags
|= SDF_LSAMPL
;
4469 if (boardtype
.reg_type
& ni_reg_m_series_mask
)
4470 s
->subdev_flags
|= SDF_SOFT_CALIBRATED
;
4471 s
->n_chan
= boardtype
.n_adchan
;
4472 s
->len_chanlist
= 512;
4473 s
->maxdata
= (1 << boardtype
.adbits
) - 1;
4474 s
->range_table
= ni_range_lkup
[boardtype
.gainlkup
];
4475 s
->insn_read
= &ni_ai_insn_read
;
4476 s
->insn_config
= &ni_ai_insn_config
;
4477 s
->do_cmdtest
= &ni_ai_cmdtest
;
4478 s
->do_cmd
= &ni_ai_cmd
;
4479 s
->cancel
= &ni_ai_reset
;
4480 s
->poll
= &ni_ai_poll
;
4481 s
->munge
= &ni_ai_munge
;
4483 s
->async_dma_dir
= DMA_FROM_DEVICE
;
4486 s
->type
= COMEDI_SUBD_UNUSED
;
4489 /* analog output subdevice */
4491 s
= &dev
->subdevices
[NI_AO_SUBDEV
];
4492 if (boardtype
.n_aochan
) {
4493 s
->type
= COMEDI_SUBD_AO
;
4494 s
->subdev_flags
= SDF_WRITABLE
| SDF_DEGLITCH
| SDF_GROUND
;
4495 if (boardtype
.reg_type
& ni_reg_m_series_mask
)
4496 s
->subdev_flags
|= SDF_SOFT_CALIBRATED
;
4497 s
->n_chan
= boardtype
.n_aochan
;
4498 s
->maxdata
= (1 << boardtype
.aobits
) - 1;
4499 s
->range_table
= boardtype
.ao_range_table
;
4500 s
->insn_read
= &ni_ao_insn_read
;
4501 if (boardtype
.reg_type
& ni_reg_6xxx_mask
) {
4502 s
->insn_write
= &ni_ao_insn_write_671x
;
4504 s
->insn_write
= &ni_ao_insn_write
;
4506 s
->insn_config
= &ni_ao_insn_config
;
4508 if (boardtype
.n_aochan
) {
4509 s
->async_dma_dir
= DMA_TO_DEVICE
;
4511 if (boardtype
.ao_fifo_depth
) {
4513 dev
->write_subdev
= s
;
4514 s
->subdev_flags
|= SDF_CMD_WRITE
;
4515 s
->do_cmd
= &ni_ao_cmd
;
4516 s
->do_cmdtest
= &ni_ao_cmdtest
;
4517 s
->len_chanlist
= boardtype
.n_aochan
;
4518 if ((boardtype
.reg_type
& ni_reg_m_series_mask
) == 0)
4519 s
->munge
= ni_ao_munge
;
4521 s
->cancel
= &ni_ao_reset
;
4523 s
->type
= COMEDI_SUBD_UNUSED
;
4525 if ((boardtype
.reg_type
& ni_reg_67xx_mask
))
4526 init_ao_67xx(dev
, s
);
4528 /* digital i/o subdevice */
4530 s
= &dev
->subdevices
[NI_DIO_SUBDEV
];
4531 s
->type
= COMEDI_SUBD_DIO
;
4532 s
->subdev_flags
= SDF_WRITABLE
| SDF_READABLE
;
4534 s
->io_bits
= 0; /* all bits input */
4535 s
->range_table
= &range_digital
;
4536 s
->n_chan
= boardtype
.num_p0_dio_channels
;
4537 if (boardtype
.reg_type
& ni_reg_m_series_mask
) {
4539 SDF_LSAMPL
| SDF_CMD_WRITE
/* | SDF_CMD_READ */ ;
4540 s
->insn_bits
= &ni_m_series_dio_insn_bits
;
4541 s
->insn_config
= &ni_m_series_dio_insn_config
;
4542 s
->do_cmd
= &ni_cdio_cmd
;
4543 s
->do_cmdtest
= &ni_cdio_cmdtest
;
4544 s
->cancel
= &ni_cdio_cancel
;
4545 s
->async_dma_dir
= DMA_BIDIRECTIONAL
;
4546 s
->len_chanlist
= s
->n_chan
;
4548 ni_writel(CDO_Reset_Bit
| CDI_Reset_Bit
, M_Offset_CDIO_Command
);
4549 ni_writel(s
->io_bits
, M_Offset_DIO_Direction
);
4551 s
->insn_bits
= &ni_dio_insn_bits
;
4552 s
->insn_config
= &ni_dio_insn_config
;
4553 devpriv
->dio_control
= DIO_Pins_Dir(s
->io_bits
);
4554 ni_writew(devpriv
->dio_control
, DIO_Control_Register
);
4558 s
= &dev
->subdevices
[NI_8255_DIO_SUBDEV
];
4559 if (boardtype
.has_8255
) {
4560 subdev_8255_init(dev
, s
, ni_8255_callback
, (unsigned long)dev
);
4562 s
->type
= COMEDI_SUBD_UNUSED
;
4565 /* formerly general purpose counter/timer device, but no longer used */
4566 s
= &dev
->subdevices
[NI_UNUSED_SUBDEV
];
4567 s
->type
= COMEDI_SUBD_UNUSED
;
4569 /* calibration subdevice -- ai and ao */
4570 s
= &dev
->subdevices
[NI_CALIBRATION_SUBDEV
];
4571 s
->type
= COMEDI_SUBD_CALIB
;
4572 if (boardtype
.reg_type
& ni_reg_m_series_mask
) {
4573 /* internal PWM analog output used for AI nonlinearity calibration */
4574 s
->subdev_flags
= SDF_INTERNAL
;
4575 s
->insn_config
= &ni_m_series_pwm_config
;
4578 ni_writel(0x0, M_Offset_Cal_PWM
);
4579 } else if (boardtype
.reg_type
== ni_reg_6143
) {
4580 /* internal PWM analog output used for AI nonlinearity calibration */
4581 s
->subdev_flags
= SDF_INTERNAL
;
4582 s
->insn_config
= &ni_6143_pwm_config
;
4586 s
->subdev_flags
= SDF_WRITABLE
| SDF_INTERNAL
;
4587 s
->insn_read
= &ni_calib_insn_read
;
4588 s
->insn_write
= &ni_calib_insn_write
;
4589 caldac_setup(dev
, s
);
4593 s
= &dev
->subdevices
[NI_EEPROM_SUBDEV
];
4594 s
->type
= COMEDI_SUBD_MEMORY
;
4595 s
->subdev_flags
= SDF_READABLE
| SDF_INTERNAL
;
4597 if (boardtype
.reg_type
& ni_reg_m_series_mask
) {
4598 s
->n_chan
= M_SERIES_EEPROM_SIZE
;
4599 s
->insn_read
= &ni_m_series_eeprom_insn_read
;
4602 s
->insn_read
= &ni_eeprom_insn_read
;
4606 s
= &dev
->subdevices
[NI_PFI_DIO_SUBDEV
];
4607 s
->type
= COMEDI_SUBD_DIO
;
4608 s
->subdev_flags
= SDF_READABLE
| SDF_WRITABLE
| SDF_INTERNAL
;
4609 if (boardtype
.reg_type
& ni_reg_m_series_mask
) {
4612 ni_writew(s
->state
, M_Offset_PFI_DO
);
4613 for (i
= 0; i
< NUM_PFI_OUTPUT_SELECT_REGS
; ++i
) {
4614 ni_writew(devpriv
->pfi_output_select_reg
[i
],
4615 M_Offset_PFI_Output_Select(i
+ 1));
4621 if (boardtype
.reg_type
& ni_reg_m_series_mask
) {
4622 s
->insn_bits
= &ni_pfi_insn_bits
;
4624 s
->insn_config
= &ni_pfi_insn_config
;
4625 ni_set_bits(dev
, IO_Bidirection_Pin_Register
, ~0, 0);
4627 /* cs5529 calibration adc */
4628 s
= &dev
->subdevices
[NI_CS5529_CALIBRATION_SUBDEV
];
4629 if (boardtype
.reg_type
& ni_reg_67xx_mask
) {
4630 s
->type
= COMEDI_SUBD_AI
;
4631 s
->subdev_flags
= SDF_READABLE
| SDF_DIFF
| SDF_INTERNAL
;
4632 /* one channel for each analog output channel */
4633 s
->n_chan
= boardtype
.n_aochan
;
4634 s
->maxdata
= (1 << 16) - 1;
4635 s
->range_table
= &range_unknown
; /* XXX */
4636 s
->insn_read
= cs5529_ai_insn_read
;
4637 s
->insn_config
= NULL
;
4640 s
->type
= COMEDI_SUBD_UNUSED
;
4644 s
= &dev
->subdevices
[NI_SERIAL_SUBDEV
];
4645 s
->type
= COMEDI_SUBD_SERIAL
;
4646 s
->subdev_flags
= SDF_READABLE
| SDF_WRITABLE
| SDF_INTERNAL
;
4649 s
->insn_config
= ni_serial_insn_config
;
4650 devpriv
->serial_interval_ns
= 0;
4651 devpriv
->serial_hw_mode
= 0;
4654 s
= &dev
->subdevices
[NI_RTSI_SUBDEV
];
4655 s
->type
= COMEDI_SUBD_DIO
;
4656 s
->subdev_flags
= SDF_READABLE
| SDF_WRITABLE
| SDF_INTERNAL
;
4659 s
->insn_bits
= ni_rtsi_insn_bits
;
4660 s
->insn_config
= ni_rtsi_insn_config
;
4663 if (boardtype
.reg_type
& ni_reg_m_series_mask
) {
4664 counter_variant
= ni_gpct_variant_m_series
;
4666 counter_variant
= ni_gpct_variant_e_series
;
4668 devpriv
->counter_dev
= ni_gpct_device_construct(dev
,
4669 &ni_gpct_write_register
,
4670 &ni_gpct_read_register
,
4673 /* General purpose counters */
4674 for (j
= 0; j
< NUM_GPCT
; ++j
) {
4675 s
= &dev
->subdevices
[NI_GPCT_SUBDEV(j
)];
4676 s
->type
= COMEDI_SUBD_COUNTER
;
4678 SDF_READABLE
| SDF_WRITABLE
| SDF_LSAMPL
| SDF_CMD_READ
4679 /* | SDF_CMD_WRITE */ ;
4681 if (boardtype
.reg_type
& ni_reg_m_series_mask
)
4682 s
->maxdata
= 0xffffffff;
4684 s
->maxdata
= 0xffffff;
4685 s
->insn_read
= &ni_gpct_insn_read
;
4686 s
->insn_write
= &ni_gpct_insn_write
;
4687 s
->insn_config
= &ni_gpct_insn_config
;
4688 s
->do_cmd
= &ni_gpct_cmd
;
4689 s
->len_chanlist
= 1;
4690 s
->do_cmdtest
= &ni_gpct_cmdtest
;
4691 s
->cancel
= &ni_gpct_cancel
;
4692 s
->async_dma_dir
= DMA_BIDIRECTIONAL
;
4693 s
->private = &devpriv
->counter_dev
->counters
[j
];
4695 devpriv
->counter_dev
->counters
[j
].chip_index
= 0;
4696 devpriv
->counter_dev
->counters
[j
].counter_index
= j
;
4697 ni_tio_init_counter(&devpriv
->counter_dev
->counters
[j
]);
4700 /* Frequency output */
4701 s
= &dev
->subdevices
[NI_FREQ_OUT_SUBDEV
];
4702 s
->type
= COMEDI_SUBD_COUNTER
;
4703 s
->subdev_flags
= SDF_READABLE
| SDF_WRITABLE
;
4706 s
->insn_read
= &ni_freq_out_insn_read
;
4707 s
->insn_write
= &ni_freq_out_insn_write
;
4708 s
->insn_config
= &ni_freq_out_insn_config
;
4710 /* ai configuration */
4711 s
= &dev
->subdevices
[NI_AI_SUBDEV
];
4712 ni_ai_reset(dev
, s
);
4713 if ((boardtype
.reg_type
& ni_reg_6xxx_mask
) == 0) {
4714 /* BEAM is this needed for PCI-6143 ?? */
4715 devpriv
->clock_and_fout
=
4716 Slow_Internal_Time_Divide_By_2
|
4717 Slow_Internal_Timebase
|
4718 Clock_To_Board_Divide_By_2
|
4720 AI_Output_Divide_By_2
| AO_Output_Divide_By_2
;
4722 devpriv
->clock_and_fout
=
4723 Slow_Internal_Time_Divide_By_2
|
4724 Slow_Internal_Timebase
|
4725 Clock_To_Board_Divide_By_2
| Clock_To_Board
;
4727 devpriv
->stc_writew(dev
, devpriv
->clock_and_fout
,
4728 Clock_and_FOUT_Register
);
4730 /* analog output configuration */
4731 s
= &dev
->subdevices
[NI_AO_SUBDEV
];
4732 ni_ao_reset(dev
, s
);
4735 devpriv
->stc_writew(dev
,
4736 (IRQ_POLARITY
? Interrupt_Output_Polarity
:
4737 0) | (Interrupt_Output_On_3_Pins
& 0) |
4738 Interrupt_A_Enable
| Interrupt_B_Enable
|
4739 Interrupt_A_Output_Select(interrupt_pin
4741 Interrupt_B_Output_Select(interrupt_pin
4743 Interrupt_Control_Register
);
4747 ni_writeb(devpriv
->ai_ao_select_reg
, AI_AO_Select
);
4748 ni_writeb(devpriv
->g0_g1_select_reg
, G0_G1_Select
);
4750 if (boardtype
.reg_type
& ni_reg_6xxx_mask
) {
4751 ni_writeb(0, Magic_611x
);
4752 } else if (boardtype
.reg_type
& ni_reg_m_series_mask
) {
4754 for (channel
= 0; channel
< boardtype
.n_aochan
; ++channel
) {
4755 ni_writeb(0xf, M_Offset_AO_Waveform_Order(channel
));
4757 M_Offset_AO_Reference_Attenuation(channel
));
4759 ni_writeb(0x0, M_Offset_AO_Calibration
);
4766 static int ni_8255_callback(int dir
, int port
, int data
, unsigned long arg
)
4768 struct comedi_device
*dev
= (struct comedi_device
*)arg
;
4769 struct ni_private
*devpriv __maybe_unused
= dev
->private;
4772 ni_writeb(data
, Port_A
+ 2 * port
);
4775 return ni_readb(Port_A
+ 2 * port
);
4780 presents the EEPROM as a subdevice
4783 static int ni_eeprom_insn_read(struct comedi_device
*dev
,
4784 struct comedi_subdevice
*s
,
4785 struct comedi_insn
*insn
, unsigned int *data
)
4787 data
[0] = ni_read_eeprom(dev
, CR_CHAN(insn
->chanspec
));
4793 reads bytes out of eeprom
4796 static int ni_read_eeprom(struct comedi_device
*dev
, int addr
)
4798 struct ni_private
*devpriv __maybe_unused
= dev
->private;
4802 bitstring
= 0x0300 | ((addr
& 0x100) << 3) | (addr
& 0xff);
4803 ni_writeb(0x04, Serial_Command
);
4804 for (bit
= 0x8000; bit
; bit
>>= 1) {
4805 ni_writeb(0x04 | ((bit
& bitstring
) ? 0x02 : 0),
4807 ni_writeb(0x05 | ((bit
& bitstring
) ? 0x02 : 0),
4811 for (bit
= 0x80; bit
; bit
>>= 1) {
4812 ni_writeb(0x04, Serial_Command
);
4813 ni_writeb(0x05, Serial_Command
);
4814 bitstring
|= ((ni_readb(XXX_Status
) & PROMOUT
) ? bit
: 0);
4816 ni_writeb(0x00, Serial_Command
);
4821 static int ni_m_series_eeprom_insn_read(struct comedi_device
*dev
,
4822 struct comedi_subdevice
*s
,
4823 struct comedi_insn
*insn
,
4826 struct ni_private
*devpriv
= dev
->private;
4828 data
[0] = devpriv
->eeprom_buffer
[CR_CHAN(insn
->chanspec
)];
4833 static int ni_get_pwm_config(struct comedi_device
*dev
, unsigned int *data
)
4835 struct ni_private
*devpriv
= dev
->private;
4837 data
[1] = devpriv
->pwm_up_count
* devpriv
->clock_ns
;
4838 data
[2] = devpriv
->pwm_down_count
* devpriv
->clock_ns
;
4842 static int ni_m_series_pwm_config(struct comedi_device
*dev
,
4843 struct comedi_subdevice
*s
,
4844 struct comedi_insn
*insn
, unsigned int *data
)
4846 struct ni_private
*devpriv
= dev
->private;
4847 unsigned up_count
, down_count
;
4850 case INSN_CONFIG_PWM_OUTPUT
:
4852 case TRIG_ROUND_NEAREST
:
4855 devpriv
->clock_ns
/ 2) / devpriv
->clock_ns
;
4857 case TRIG_ROUND_DOWN
:
4858 up_count
= data
[2] / devpriv
->clock_ns
;
4862 (data
[2] + devpriv
->clock_ns
-
4863 1) / devpriv
->clock_ns
;
4870 case TRIG_ROUND_NEAREST
:
4873 devpriv
->clock_ns
/ 2) / devpriv
->clock_ns
;
4875 case TRIG_ROUND_DOWN
:
4876 down_count
= data
[4] / devpriv
->clock_ns
;
4880 (data
[4] + devpriv
->clock_ns
-
4881 1) / devpriv
->clock_ns
;
4887 if (up_count
* devpriv
->clock_ns
!= data
[2] ||
4888 down_count
* devpriv
->clock_ns
!= data
[4]) {
4889 data
[2] = up_count
* devpriv
->clock_ns
;
4890 data
[4] = down_count
* devpriv
->clock_ns
;
4893 ni_writel(MSeries_Cal_PWM_High_Time_Bits(up_count
) |
4894 MSeries_Cal_PWM_Low_Time_Bits(down_count
),
4896 devpriv
->pwm_up_count
= up_count
;
4897 devpriv
->pwm_down_count
= down_count
;
4900 case INSN_CONFIG_GET_PWM_OUTPUT
:
4901 return ni_get_pwm_config(dev
, data
);
4910 static int ni_6143_pwm_config(struct comedi_device
*dev
,
4911 struct comedi_subdevice
*s
,
4912 struct comedi_insn
*insn
, unsigned int *data
)
4914 struct ni_private
*devpriv
= dev
->private;
4915 unsigned up_count
, down_count
;
4918 case INSN_CONFIG_PWM_OUTPUT
:
4920 case TRIG_ROUND_NEAREST
:
4923 devpriv
->clock_ns
/ 2) / devpriv
->clock_ns
;
4925 case TRIG_ROUND_DOWN
:
4926 up_count
= data
[2] / devpriv
->clock_ns
;
4930 (data
[2] + devpriv
->clock_ns
-
4931 1) / devpriv
->clock_ns
;
4938 case TRIG_ROUND_NEAREST
:
4941 devpriv
->clock_ns
/ 2) / devpriv
->clock_ns
;
4943 case TRIG_ROUND_DOWN
:
4944 down_count
= data
[4] / devpriv
->clock_ns
;
4948 (data
[4] + devpriv
->clock_ns
-
4949 1) / devpriv
->clock_ns
;
4955 if (up_count
* devpriv
->clock_ns
!= data
[2] ||
4956 down_count
* devpriv
->clock_ns
!= data
[4]) {
4957 data
[2] = up_count
* devpriv
->clock_ns
;
4958 data
[4] = down_count
* devpriv
->clock_ns
;
4961 ni_writel(up_count
, Calibration_HighTime_6143
);
4962 devpriv
->pwm_up_count
= up_count
;
4963 ni_writel(down_count
, Calibration_LowTime_6143
);
4964 devpriv
->pwm_down_count
= down_count
;
4967 case INSN_CONFIG_GET_PWM_OUTPUT
:
4968 return ni_get_pwm_config(dev
, data
);
4976 static void ni_write_caldac(struct comedi_device
*dev
, int addr
, int val
);
4978 calibration subdevice
4980 static int ni_calib_insn_write(struct comedi_device
*dev
,
4981 struct comedi_subdevice
*s
,
4982 struct comedi_insn
*insn
, unsigned int *data
)
4984 ni_write_caldac(dev
, CR_CHAN(insn
->chanspec
), data
[0]);
4989 static int ni_calib_insn_read(struct comedi_device
*dev
,
4990 struct comedi_subdevice
*s
,
4991 struct comedi_insn
*insn
, unsigned int *data
)
4993 struct ni_private
*devpriv
= dev
->private;
4995 data
[0] = devpriv
->caldacs
[CR_CHAN(insn
->chanspec
)];
5000 static int pack_mb88341(int addr
, int val
, int *bitstring
);
5001 static int pack_dac8800(int addr
, int val
, int *bitstring
);
5002 static int pack_dac8043(int addr
, int val
, int *bitstring
);
5003 static int pack_ad8522(int addr
, int val
, int *bitstring
);
5004 static int pack_ad8804(int addr
, int val
, int *bitstring
);
5005 static int pack_ad8842(int addr
, int val
, int *bitstring
);
5007 struct caldac_struct
{
5010 int (*packbits
) (int, int, int *);
5013 static struct caldac_struct caldacs
[] = {
5014 [mb88341
] = {12, 8, pack_mb88341
},
5015 [dac8800
] = {8, 8, pack_dac8800
},
5016 [dac8043
] = {1, 12, pack_dac8043
},
5017 [ad8522
] = {2, 12, pack_ad8522
},
5018 [ad8804
] = {12, 8, pack_ad8804
},
5019 [ad8842
] = {8, 8, pack_ad8842
},
5020 [ad8804_debug
] = {16, 8, pack_ad8804
},
5023 static void caldac_setup(struct comedi_device
*dev
, struct comedi_subdevice
*s
)
5025 struct ni_private
*devpriv
= dev
->private;
5034 type
= boardtype
.caldac
[0];
5035 if (type
== caldac_none
)
5037 n_bits
= caldacs
[type
].n_bits
;
5038 for (i
= 0; i
< 3; i
++) {
5039 type
= boardtype
.caldac
[i
];
5040 if (type
== caldac_none
)
5042 if (caldacs
[type
].n_bits
!= n_bits
)
5044 n_chans
+= caldacs
[type
].n_chans
;
5047 s
->n_chan
= n_chans
;
5050 unsigned int *maxdata_list
;
5052 if (n_chans
> MAX_N_CALDACS
) {
5053 printk("BUG! MAX_N_CALDACS too small\n");
5055 s
->maxdata_list
= maxdata_list
= devpriv
->caldac_maxdata_list
;
5057 for (i
= 0; i
< n_dacs
; i
++) {
5058 type
= boardtype
.caldac
[i
];
5059 for (j
= 0; j
< caldacs
[type
].n_chans
; j
++) {
5060 maxdata_list
[chan
] =
5061 (1 << caldacs
[type
].n_bits
) - 1;
5066 for (chan
= 0; chan
< s
->n_chan
; chan
++)
5067 ni_write_caldac(dev
, i
, s
->maxdata_list
[i
] / 2);
5069 type
= boardtype
.caldac
[0];
5070 s
->maxdata
= (1 << caldacs
[type
].n_bits
) - 1;
5072 for (chan
= 0; chan
< s
->n_chan
; chan
++)
5073 ni_write_caldac(dev
, i
, s
->maxdata
/ 2);
5077 static void ni_write_caldac(struct comedi_device
*dev
, int addr
, int val
)
5079 struct ni_private
*devpriv
= dev
->private;
5080 unsigned int loadbit
= 0, bits
= 0, bit
, bitstring
= 0;
5084 /* printk("ni_write_caldac: chan=%d val=%d\n",addr,val); */
5085 if (devpriv
->caldacs
[addr
] == val
)
5087 devpriv
->caldacs
[addr
] = val
;
5089 for (i
= 0; i
< 3; i
++) {
5090 type
= boardtype
.caldac
[i
];
5091 if (type
== caldac_none
)
5093 if (addr
< caldacs
[type
].n_chans
) {
5094 bits
= caldacs
[type
].packbits(addr
, val
, &bitstring
);
5095 loadbit
= SerDacLd(i
);
5096 /* printk("caldac: using i=%d addr=%d %x\n",i,addr,bitstring); */
5099 addr
-= caldacs
[type
].n_chans
;
5102 for (bit
= 1 << (bits
- 1); bit
; bit
>>= 1) {
5103 ni_writeb(((bit
& bitstring
) ? 0x02 : 0), Serial_Command
);
5105 ni_writeb(1 | ((bit
& bitstring
) ? 0x02 : 0), Serial_Command
);
5108 ni_writeb(loadbit
, Serial_Command
);
5110 ni_writeb(0, Serial_Command
);
5113 static int pack_mb88341(int addr
, int val
, int *bitstring
)
5117 Note that address bits are reversed. Thanks to
5118 Ingo Keen for noticing this.
5120 Note also that the 88341 expects address values from
5121 1-12, whereas we use channel numbers 0-11. The NI
5122 docs use 1-12, also, so be careful here.
5125 *bitstring
= ((addr
& 0x1) << 11) |
5126 ((addr
& 0x2) << 9) |
5127 ((addr
& 0x4) << 7) | ((addr
& 0x8) << 5) | (val
& 0xff);
5131 static int pack_dac8800(int addr
, int val
, int *bitstring
)
5133 *bitstring
= ((addr
& 0x7) << 8) | (val
& 0xff);
5137 static int pack_dac8043(int addr
, int val
, int *bitstring
)
5139 *bitstring
= val
& 0xfff;
5143 static int pack_ad8522(int addr
, int val
, int *bitstring
)
5145 *bitstring
= (val
& 0xfff) | (addr
? 0xc000 : 0xa000);
5149 static int pack_ad8804(int addr
, int val
, int *bitstring
)
5151 *bitstring
= ((addr
& 0xf) << 8) | (val
& 0xff);
5155 static int pack_ad8842(int addr
, int val
, int *bitstring
)
5157 *bitstring
= ((addr
+ 1) << 8) | (val
& 0xff);
5163 * Read the GPCTs current value.
5165 static int GPCT_G_Watch(struct comedi_device
*dev
, int chan
)
5167 unsigned int hi1
, hi2
, lo
;
5169 devpriv
->gpct_command
[chan
] &= ~G_Save_Trace
;
5170 devpriv
->stc_writew(dev
, devpriv
->gpct_command
[chan
],
5171 G_Command_Register(chan
));
5173 devpriv
->gpct_command
[chan
] |= G_Save_Trace
;
5174 devpriv
->stc_writew(dev
, devpriv
->gpct_command
[chan
],
5175 G_Command_Register(chan
));
5177 /* This procedure is used because the two registers cannot
5178 * be read atomically. */
5180 hi1
= devpriv
->stc_readw(dev
, G_Save_Register_High(chan
));
5181 lo
= devpriv
->stc_readw(dev
, G_Save_Register_Low(chan
));
5182 hi2
= devpriv
->stc_readw(dev
, G_Save_Register_High(chan
));
5183 } while (hi1
!= hi2
);
5185 return (hi1
<< 16) | lo
;
5188 static void GPCT_Reset(struct comedi_device
*dev
, int chan
)
5190 int temp_ack_reg
= 0;
5192 /* printk("GPCT_Reset..."); */
5193 devpriv
->gpct_cur_operation
[chan
] = GPCT_RESET
;
5197 devpriv
->stc_writew(dev
, G0_Reset
, Joint_Reset_Register
);
5198 ni_set_bits(dev
, Interrupt_A_Enable_Register
,
5199 G0_TC_Interrupt_Enable
, 0);
5200 ni_set_bits(dev
, Interrupt_A_Enable_Register
,
5201 G0_Gate_Interrupt_Enable
, 0);
5202 temp_ack_reg
|= G0_Gate_Error_Confirm
;
5203 temp_ack_reg
|= G0_TC_Error_Confirm
;
5204 temp_ack_reg
|= G0_TC_Interrupt_Ack
;
5205 temp_ack_reg
|= G0_Gate_Interrupt_Ack
;
5206 devpriv
->stc_writew(dev
, temp_ack_reg
,
5207 Interrupt_A_Ack_Register
);
5209 /* problem...this interferes with the other ctr... */
5210 devpriv
->an_trig_etc_reg
|= GPFO_0_Output_Enable
;
5211 devpriv
->stc_writew(dev
, devpriv
->an_trig_etc_reg
,
5212 Analog_Trigger_Etc_Register
);
5215 devpriv
->stc_writew(dev
, G1_Reset
, Joint_Reset_Register
);
5216 ni_set_bits(dev
, Interrupt_B_Enable_Register
,
5217 G1_TC_Interrupt_Enable
, 0);
5218 ni_set_bits(dev
, Interrupt_B_Enable_Register
,
5219 G0_Gate_Interrupt_Enable
, 0);
5220 temp_ack_reg
|= G1_Gate_Error_Confirm
;
5221 temp_ack_reg
|= G1_TC_Error_Confirm
;
5222 temp_ack_reg
|= G1_TC_Interrupt_Ack
;
5223 temp_ack_reg
|= G1_Gate_Interrupt_Ack
;
5224 devpriv
->stc_writew(dev
, temp_ack_reg
,
5225 Interrupt_B_Ack_Register
);
5227 devpriv
->an_trig_etc_reg
|= GPFO_1_Output_Enable
;
5228 devpriv
->stc_writew(dev
, devpriv
->an_trig_etc_reg
,
5229 Analog_Trigger_Etc_Register
);
5233 devpriv
->gpct_mode
[chan
] = 0;
5234 devpriv
->gpct_input_select
[chan
] = 0;
5235 devpriv
->gpct_command
[chan
] = 0;
5237 devpriv
->gpct_command
[chan
] |= G_Synchronized_Gate
;
5239 devpriv
->stc_writew(dev
, devpriv
->gpct_mode
[chan
],
5240 G_Mode_Register(chan
));
5241 devpriv
->stc_writew(dev
, devpriv
->gpct_input_select
[chan
],
5242 G_Input_Select_Register(chan
));
5243 devpriv
->stc_writew(dev
, 0, G_Autoincrement_Register(chan
));
5245 /* printk("exit GPCT_Reset\n"); */
5250 static int ni_gpct_insn_config(struct comedi_device
*dev
,
5251 struct comedi_subdevice
*s
,
5252 struct comedi_insn
*insn
, unsigned int *data
)
5254 struct ni_gpct
*counter
= s
->private;
5255 return ni_tio_insn_config(counter
, insn
, data
);
5258 static int ni_gpct_insn_read(struct comedi_device
*dev
,
5259 struct comedi_subdevice
*s
,
5260 struct comedi_insn
*insn
, unsigned int *data
)
5262 struct ni_gpct
*counter
= s
->private;
5263 return ni_tio_rinsn(counter
, insn
, data
);
5266 static int ni_gpct_insn_write(struct comedi_device
*dev
,
5267 struct comedi_subdevice
*s
,
5268 struct comedi_insn
*insn
, unsigned int *data
)
5270 struct ni_gpct
*counter
= s
->private;
5271 return ni_tio_winsn(counter
, insn
, data
);
5274 static int ni_gpct_cmd(struct comedi_device
*dev
, struct comedi_subdevice
*s
)
5278 struct ni_gpct
*counter
= s
->private;
5279 /* const struct comedi_cmd *cmd = &s->async->cmd; */
5281 retval
= ni_request_gpct_mite_channel(dev
, counter
->counter_index
,
5285 "no dma channel available for use by counter");
5288 ni_tio_acknowledge_and_confirm(counter
, NULL
, NULL
, NULL
, NULL
);
5289 ni_e_series_enable_second_irq(dev
, counter
->counter_index
, 1);
5290 retval
= ni_tio_cmd(counter
, s
->async
);
5297 static int ni_gpct_cmdtest(struct comedi_device
*dev
,
5298 struct comedi_subdevice
*s
, struct comedi_cmd
*cmd
)
5301 struct ni_gpct
*counter
= s
->private;
5303 return ni_tio_cmdtest(counter
, cmd
);
5309 static int ni_gpct_cancel(struct comedi_device
*dev
, struct comedi_subdevice
*s
)
5312 struct ni_gpct
*counter
= s
->private;
5315 retval
= ni_tio_cancel(counter
);
5316 ni_e_series_enable_second_irq(dev
, counter
->counter_index
, 0);
5317 ni_release_gpct_mite_channel(dev
, counter
->counter_index
);
5326 * Programmable Function Inputs
5330 static int ni_m_series_set_pfi_routing(struct comedi_device
*dev
, unsigned chan
,
5333 struct ni_private
*devpriv
= dev
->private;
5334 unsigned pfi_reg_index
;
5335 unsigned array_offset
;
5337 if ((source
& 0x1f) != source
)
5339 pfi_reg_index
= 1 + chan
/ 3;
5340 array_offset
= pfi_reg_index
- 1;
5341 devpriv
->pfi_output_select_reg
[array_offset
] &=
5342 ~MSeries_PFI_Output_Select_Mask(chan
);
5343 devpriv
->pfi_output_select_reg
[array_offset
] |=
5344 MSeries_PFI_Output_Select_Bits(chan
, source
);
5345 ni_writew(devpriv
->pfi_output_select_reg
[array_offset
],
5346 M_Offset_PFI_Output_Select(pfi_reg_index
));
5350 static int ni_old_set_pfi_routing(struct comedi_device
*dev
, unsigned chan
,
5353 /* pre-m-series boards have fixed signals on pfi pins */
5354 if (source
!= ni_old_get_pfi_routing(dev
, chan
))
5359 static int ni_set_pfi_routing(struct comedi_device
*dev
, unsigned chan
,
5362 if (boardtype
.reg_type
& ni_reg_m_series_mask
)
5363 return ni_m_series_set_pfi_routing(dev
, chan
, source
);
5365 return ni_old_set_pfi_routing(dev
, chan
, source
);
5368 static unsigned ni_m_series_get_pfi_routing(struct comedi_device
*dev
,
5371 struct ni_private
*devpriv
= dev
->private;
5372 const unsigned array_offset
= chan
/ 3;
5374 return MSeries_PFI_Output_Select_Source(chan
,
5376 pfi_output_select_reg
5380 static unsigned ni_old_get_pfi_routing(struct comedi_device
*dev
, unsigned chan
)
5382 /* pre-m-series boards have fixed signals on pfi pins */
5385 return NI_PFI_OUTPUT_AI_START1
;
5388 return NI_PFI_OUTPUT_AI_START2
;
5391 return NI_PFI_OUTPUT_AI_CONVERT
;
5394 return NI_PFI_OUTPUT_G_SRC1
;
5397 return NI_PFI_OUTPUT_G_GATE1
;
5400 return NI_PFI_OUTPUT_AO_UPDATE_N
;
5403 return NI_PFI_OUTPUT_AO_START1
;
5406 return NI_PFI_OUTPUT_AI_START_PULSE
;
5409 return NI_PFI_OUTPUT_G_SRC0
;
5412 return NI_PFI_OUTPUT_G_GATE0
;
5415 printk("%s: bug, unhandled case in switch.\n", __func__
);
5421 static unsigned ni_get_pfi_routing(struct comedi_device
*dev
, unsigned chan
)
5423 if (boardtype
.reg_type
& ni_reg_m_series_mask
)
5424 return ni_m_series_get_pfi_routing(dev
, chan
);
5426 return ni_old_get_pfi_routing(dev
, chan
);
5429 static int ni_config_filter(struct comedi_device
*dev
, unsigned pfi_channel
,
5430 enum ni_pfi_filter_select filter
)
5432 struct ni_private
*devpriv __maybe_unused
= dev
->private;
5435 if ((boardtype
.reg_type
& ni_reg_m_series_mask
) == 0) {
5438 bits
= ni_readl(M_Offset_PFI_Filter
);
5439 bits
&= ~MSeries_PFI_Filter_Select_Mask(pfi_channel
);
5440 bits
|= MSeries_PFI_Filter_Select_Bits(pfi_channel
, filter
);
5441 ni_writel(bits
, M_Offset_PFI_Filter
);
5445 static int ni_pfi_insn_bits(struct comedi_device
*dev
,
5446 struct comedi_subdevice
*s
,
5447 struct comedi_insn
*insn
, unsigned int *data
)
5449 struct ni_private
*devpriv __maybe_unused
= dev
->private;
5451 if ((boardtype
.reg_type
& ni_reg_m_series_mask
) == 0) {
5455 s
->state
&= ~data
[0];
5456 s
->state
|= (data
[0] & data
[1]);
5457 ni_writew(s
->state
, M_Offset_PFI_DO
);
5459 data
[1] = ni_readw(M_Offset_PFI_DI
);
5463 static int ni_pfi_insn_config(struct comedi_device
*dev
,
5464 struct comedi_subdevice
*s
,
5465 struct comedi_insn
*insn
, unsigned int *data
)
5467 struct ni_private
*devpriv
= dev
->private;
5473 chan
= CR_CHAN(insn
->chanspec
);
5477 ni_set_bits(dev
, IO_Bidirection_Pin_Register
, 1 << chan
, 1);
5480 ni_set_bits(dev
, IO_Bidirection_Pin_Register
, 1 << chan
, 0);
5482 case INSN_CONFIG_DIO_QUERY
:
5484 (devpriv
->io_bidirection_pin_reg
& (1 << chan
)) ?
5485 COMEDI_OUTPUT
: COMEDI_INPUT
;
5488 case INSN_CONFIG_SET_ROUTING
:
5489 return ni_set_pfi_routing(dev
, chan
, data
[1]);
5491 case INSN_CONFIG_GET_ROUTING
:
5492 data
[1] = ni_get_pfi_routing(dev
, chan
);
5494 case INSN_CONFIG_FILTER
:
5495 return ni_config_filter(dev
, chan
, data
[1]);
5505 * NI RTSI Bus Functions
5508 static void ni_rtsi_init(struct comedi_device
*dev
)
5510 struct ni_private
*devpriv
= dev
->private;
5512 /* Initialises the RTSI bus signal switch to a default state */
5514 /* Set clock mode to internal */
5515 devpriv
->clock_and_fout2
= MSeries_RTSI_10MHz_Bit
;
5516 if (ni_set_master_clock(dev
, NI_MIO_INTERNAL_CLOCK
, 0) < 0) {
5517 printk("ni_set_master_clock failed, bug?");
5519 /* default internal lines routing to RTSI bus lines */
5520 devpriv
->rtsi_trig_a_output_reg
=
5521 RTSI_Trig_Output_Bits(0,
5522 NI_RTSI_OUTPUT_ADR_START1
) |
5523 RTSI_Trig_Output_Bits(1,
5524 NI_RTSI_OUTPUT_ADR_START2
) |
5525 RTSI_Trig_Output_Bits(2,
5526 NI_RTSI_OUTPUT_SCLKG
) |
5527 RTSI_Trig_Output_Bits(3, NI_RTSI_OUTPUT_DACUPDN
);
5528 devpriv
->stc_writew(dev
, devpriv
->rtsi_trig_a_output_reg
,
5529 RTSI_Trig_A_Output_Register
);
5530 devpriv
->rtsi_trig_b_output_reg
=
5531 RTSI_Trig_Output_Bits(4,
5532 NI_RTSI_OUTPUT_DA_START1
) |
5533 RTSI_Trig_Output_Bits(5,
5534 NI_RTSI_OUTPUT_G_SRC0
) |
5535 RTSI_Trig_Output_Bits(6, NI_RTSI_OUTPUT_G_GATE0
);
5536 if (boardtype
.reg_type
& ni_reg_m_series_mask
)
5537 devpriv
->rtsi_trig_b_output_reg
|=
5538 RTSI_Trig_Output_Bits(7, NI_RTSI_OUTPUT_RTSI_OSC
);
5539 devpriv
->stc_writew(dev
, devpriv
->rtsi_trig_b_output_reg
,
5540 RTSI_Trig_B_Output_Register
);
5543 * Sets the source and direction of the 4 on board lines
5544 * devpriv->stc_writew(dev, 0x0000, RTSI_Board_Register);
5548 static int ni_rtsi_insn_bits(struct comedi_device
*dev
,
5549 struct comedi_subdevice
*s
,
5550 struct comedi_insn
*insn
, unsigned int *data
)
5557 /* Find best multiplier/divider to try and get the PLL running at 80 MHz
5558 * given an arbitrary frequency input clock */
5559 static int ni_mseries_get_pll_parameters(unsigned reference_period_ns
,
5560 unsigned *freq_divider
,
5561 unsigned *freq_multiplier
,
5562 unsigned *actual_period_ns
)
5565 unsigned best_div
= 1;
5566 static const unsigned max_div
= 0x10;
5568 unsigned best_mult
= 1;
5569 static const unsigned max_mult
= 0x100;
5570 static const unsigned pico_per_nano
= 1000;
5572 const unsigned reference_picosec
= reference_period_ns
* pico_per_nano
;
5573 /* m-series wants the phased-locked loop to output 80MHz, which is divided by 4 to
5574 * 20 MHz for most timing clocks */
5575 static const unsigned target_picosec
= 12500;
5576 static const unsigned fudge_factor_80_to_20Mhz
= 4;
5577 int best_period_picosec
= 0;
5578 for (div
= 1; div
<= max_div
; ++div
) {
5579 for (mult
= 1; mult
<= max_mult
; ++mult
) {
5580 unsigned new_period_ps
=
5581 (reference_picosec
* div
) / mult
;
5582 if (abs(new_period_ps
- target_picosec
) <
5583 abs(best_period_picosec
- target_picosec
)) {
5584 best_period_picosec
= new_period_ps
;
5590 if (best_period_picosec
== 0) {
5591 printk("%s: bug, failed to find pll parameters\n", __func__
);
5594 *freq_divider
= best_div
;
5595 *freq_multiplier
= best_mult
;
5597 (best_period_picosec
* fudge_factor_80_to_20Mhz
+
5598 (pico_per_nano
/ 2)) / pico_per_nano
;
5602 static inline unsigned num_configurable_rtsi_channels(struct comedi_device
*dev
)
5604 if (boardtype
.reg_type
& ni_reg_m_series_mask
)
5610 static int ni_mseries_set_pll_master_clock(struct comedi_device
*dev
,
5611 unsigned source
, unsigned period_ns
)
5613 struct ni_private
*devpriv
= dev
->private;
5614 static const unsigned min_period_ns
= 50;
5615 static const unsigned max_period_ns
= 1000;
5616 static const unsigned timeout
= 1000;
5617 unsigned pll_control_bits
;
5618 unsigned freq_divider
;
5619 unsigned freq_multiplier
;
5623 if (source
== NI_MIO_PLL_PXI10_CLOCK
)
5625 /* these limits are somewhat arbitrary, but NI advertises 1 to 20MHz range so we'll use that */
5626 if (period_ns
< min_period_ns
|| period_ns
> max_period_ns
) {
5628 ("%s: you must specify an input clock frequency between %i and %i nanosec "
5629 "for the phased-lock loop.\n", __func__
,
5630 min_period_ns
, max_period_ns
);
5633 devpriv
->rtsi_trig_direction_reg
&= ~Use_RTSI_Clock_Bit
;
5634 devpriv
->stc_writew(dev
, devpriv
->rtsi_trig_direction_reg
,
5635 RTSI_Trig_Direction_Register
);
5637 MSeries_PLL_Enable_Bit
| MSeries_PLL_VCO_Mode_75_150MHz_Bits
;
5638 devpriv
->clock_and_fout2
|=
5639 MSeries_Timebase1_Select_Bit
| MSeries_Timebase3_Select_Bit
;
5640 devpriv
->clock_and_fout2
&= ~MSeries_PLL_In_Source_Select_Mask
;
5642 case NI_MIO_PLL_PXI_STAR_TRIGGER_CLOCK
:
5643 devpriv
->clock_and_fout2
|=
5644 MSeries_PLL_In_Source_Select_Star_Trigger_Bits
;
5645 retval
= ni_mseries_get_pll_parameters(period_ns
, &freq_divider
,
5647 &devpriv
->clock_ns
);
5651 case NI_MIO_PLL_PXI10_CLOCK
:
5652 /* pxi clock is 10MHz */
5653 devpriv
->clock_and_fout2
|=
5654 MSeries_PLL_In_Source_Select_PXI_Clock10
;
5655 retval
= ni_mseries_get_pll_parameters(period_ns
, &freq_divider
,
5657 &devpriv
->clock_ns
);
5663 unsigned rtsi_channel
;
5664 static const unsigned max_rtsi_channel
= 7;
5665 for (rtsi_channel
= 0; rtsi_channel
<= max_rtsi_channel
;
5668 NI_MIO_PLL_RTSI_CLOCK(rtsi_channel
)) {
5669 devpriv
->clock_and_fout2
|=
5670 MSeries_PLL_In_Source_Select_RTSI_Bits
5675 if (rtsi_channel
> max_rtsi_channel
)
5677 retval
= ni_mseries_get_pll_parameters(period_ns
,
5687 ni_writew(devpriv
->clock_and_fout2
, M_Offset_Clock_and_Fout2
);
5689 MSeries_PLL_Divisor_Bits(freq_divider
) |
5690 MSeries_PLL_Multiplier_Bits(freq_multiplier
);
5692 /* printk("using divider=%i, multiplier=%i for PLL. pll_control_bits = 0x%x\n",
5693 * freq_divider, freq_multiplier, pll_control_bits); */
5694 /* printk("clock_ns=%d\n", devpriv->clock_ns); */
5695 ni_writew(pll_control_bits
, M_Offset_PLL_Control
);
5696 devpriv
->clock_source
= source
;
5697 /* it seems to typically take a few hundred microseconds for PLL to lock */
5698 for (i
= 0; i
< timeout
; ++i
) {
5699 if (ni_readw(M_Offset_PLL_Status
) & MSeries_PLL_Locked_Bit
) {
5706 ("%s: timed out waiting for PLL to lock to reference clock source %i with period %i ns.\n",
5707 __func__
, source
, period_ns
);
5713 static int ni_set_master_clock(struct comedi_device
*dev
, unsigned source
,
5716 struct ni_private
*devpriv
= dev
->private;
5718 if (source
== NI_MIO_INTERNAL_CLOCK
) {
5719 devpriv
->rtsi_trig_direction_reg
&= ~Use_RTSI_Clock_Bit
;
5720 devpriv
->stc_writew(dev
, devpriv
->rtsi_trig_direction_reg
,
5721 RTSI_Trig_Direction_Register
);
5722 devpriv
->clock_ns
= TIMEBASE_1_NS
;
5723 if (boardtype
.reg_type
& ni_reg_m_series_mask
) {
5724 devpriv
->clock_and_fout2
&=
5725 ~(MSeries_Timebase1_Select_Bit
|
5726 MSeries_Timebase3_Select_Bit
);
5727 ni_writew(devpriv
->clock_and_fout2
,
5728 M_Offset_Clock_and_Fout2
);
5729 ni_writew(0, M_Offset_PLL_Control
);
5731 devpriv
->clock_source
= source
;
5733 if (boardtype
.reg_type
& ni_reg_m_series_mask
) {
5734 return ni_mseries_set_pll_master_clock(dev
, source
,
5737 if (source
== NI_MIO_RTSI_CLOCK
) {
5738 devpriv
->rtsi_trig_direction_reg
|=
5740 devpriv
->stc_writew(dev
,
5742 rtsi_trig_direction_reg
,
5743 RTSI_Trig_Direction_Register
);
5744 if (period_ns
== 0) {
5746 ("%s: we don't handle an unspecified clock period correctly yet, returning error.\n",
5750 devpriv
->clock_ns
= period_ns
;
5752 devpriv
->clock_source
= source
;
5760 static int ni_valid_rtsi_output_source(struct comedi_device
*dev
, unsigned chan
,
5763 if (chan
>= num_configurable_rtsi_channels(dev
)) {
5764 if (chan
== old_RTSI_clock_channel
) {
5765 if (source
== NI_RTSI_OUTPUT_RTSI_OSC
)
5769 ("%s: invalid source for channel=%i, channel %i is always the RTSI clock for pre-m-series boards.\n",
5770 __func__
, chan
, old_RTSI_clock_channel
);
5777 case NI_RTSI_OUTPUT_ADR_START1
:
5778 case NI_RTSI_OUTPUT_ADR_START2
:
5779 case NI_RTSI_OUTPUT_SCLKG
:
5780 case NI_RTSI_OUTPUT_DACUPDN
:
5781 case NI_RTSI_OUTPUT_DA_START1
:
5782 case NI_RTSI_OUTPUT_G_SRC0
:
5783 case NI_RTSI_OUTPUT_G_GATE0
:
5784 case NI_RTSI_OUTPUT_RGOUT0
:
5785 case NI_RTSI_OUTPUT_RTSI_BRD_0
:
5788 case NI_RTSI_OUTPUT_RTSI_OSC
:
5789 if (boardtype
.reg_type
& ni_reg_m_series_mask
)
5800 static int ni_set_rtsi_routing(struct comedi_device
*dev
, unsigned chan
,
5803 struct ni_private
*devpriv
= dev
->private;
5805 if (ni_valid_rtsi_output_source(dev
, chan
, source
) == 0)
5808 devpriv
->rtsi_trig_a_output_reg
&= ~RTSI_Trig_Output_Mask(chan
);
5809 devpriv
->rtsi_trig_a_output_reg
|=
5810 RTSI_Trig_Output_Bits(chan
, source
);
5811 devpriv
->stc_writew(dev
, devpriv
->rtsi_trig_a_output_reg
,
5812 RTSI_Trig_A_Output_Register
);
5813 } else if (chan
< 8) {
5814 devpriv
->rtsi_trig_b_output_reg
&= ~RTSI_Trig_Output_Mask(chan
);
5815 devpriv
->rtsi_trig_b_output_reg
|=
5816 RTSI_Trig_Output_Bits(chan
, source
);
5817 devpriv
->stc_writew(dev
, devpriv
->rtsi_trig_b_output_reg
,
5818 RTSI_Trig_B_Output_Register
);
5823 static unsigned ni_get_rtsi_routing(struct comedi_device
*dev
, unsigned chan
)
5825 struct ni_private
*devpriv
= dev
->private;
5828 return RTSI_Trig_Output_Source(chan
,
5829 devpriv
->rtsi_trig_a_output_reg
);
5830 } else if (chan
< num_configurable_rtsi_channels(dev
)) {
5831 return RTSI_Trig_Output_Source(chan
,
5832 devpriv
->rtsi_trig_b_output_reg
);
5834 if (chan
== old_RTSI_clock_channel
)
5835 return NI_RTSI_OUTPUT_RTSI_OSC
;
5836 printk("%s: bug! should never get here?\n", __func__
);
5841 static int ni_rtsi_insn_config(struct comedi_device
*dev
,
5842 struct comedi_subdevice
*s
,
5843 struct comedi_insn
*insn
, unsigned int *data
)
5845 struct ni_private
*devpriv
= dev
->private;
5846 unsigned int chan
= CR_CHAN(insn
->chanspec
);
5849 case INSN_CONFIG_DIO_OUTPUT
:
5850 if (chan
< num_configurable_rtsi_channels(dev
)) {
5851 devpriv
->rtsi_trig_direction_reg
|=
5852 RTSI_Output_Bit(chan
,
5854 reg_type
& ni_reg_m_series_mask
) !=
5856 } else if (chan
== old_RTSI_clock_channel
) {
5857 devpriv
->rtsi_trig_direction_reg
|=
5858 Drive_RTSI_Clock_Bit
;
5860 devpriv
->stc_writew(dev
, devpriv
->rtsi_trig_direction_reg
,
5861 RTSI_Trig_Direction_Register
);
5863 case INSN_CONFIG_DIO_INPUT
:
5864 if (chan
< num_configurable_rtsi_channels(dev
)) {
5865 devpriv
->rtsi_trig_direction_reg
&=
5866 ~RTSI_Output_Bit(chan
,
5868 reg_type
& ni_reg_m_series_mask
)
5870 } else if (chan
== old_RTSI_clock_channel
) {
5871 devpriv
->rtsi_trig_direction_reg
&=
5872 ~Drive_RTSI_Clock_Bit
;
5874 devpriv
->stc_writew(dev
, devpriv
->rtsi_trig_direction_reg
,
5875 RTSI_Trig_Direction_Register
);
5877 case INSN_CONFIG_DIO_QUERY
:
5878 if (chan
< num_configurable_rtsi_channels(dev
)) {
5880 (devpriv
->rtsi_trig_direction_reg
&
5881 RTSI_Output_Bit(chan
,
5882 (boardtype
.reg_type
&
5883 ni_reg_m_series_mask
)
5884 != 0)) ? INSN_CONFIG_DIO_OUTPUT
:
5885 INSN_CONFIG_DIO_INPUT
;
5886 } else if (chan
== old_RTSI_clock_channel
) {
5888 (devpriv
->rtsi_trig_direction_reg
&
5889 Drive_RTSI_Clock_Bit
)
5890 ? INSN_CONFIG_DIO_OUTPUT
: INSN_CONFIG_DIO_INPUT
;
5894 case INSN_CONFIG_SET_CLOCK_SRC
:
5895 return ni_set_master_clock(dev
, data
[1], data
[2]);
5897 case INSN_CONFIG_GET_CLOCK_SRC
:
5898 data
[1] = devpriv
->clock_source
;
5899 data
[2] = devpriv
->clock_ns
;
5902 case INSN_CONFIG_SET_ROUTING
:
5903 return ni_set_rtsi_routing(dev
, chan
, data
[1]);
5905 case INSN_CONFIG_GET_ROUTING
:
5906 data
[1] = ni_get_rtsi_routing(dev
, chan
);
5916 static int cs5529_wait_for_idle(struct comedi_device
*dev
)
5918 unsigned short status
;
5919 const int timeout
= HZ
;
5922 for (i
= 0; i
< timeout
; i
++) {
5923 status
= ni_ao_win_inw(dev
, CAL_ADC_Status_67xx
);
5924 if ((status
& CSS_ADC_BUSY
) == 0) {
5927 set_current_state(TASK_INTERRUPTIBLE
);
5928 if (schedule_timeout(1)) {
5932 /* printk("looped %i times waiting for idle\n", i); */
5934 printk("%s: %s: timeout\n", __FILE__
, __func__
);
5940 static void cs5529_command(struct comedi_device
*dev
, unsigned short value
)
5942 static const int timeout
= 100;
5945 ni_ao_win_outw(dev
, value
, CAL_ADC_Command_67xx
);
5946 /* give time for command to start being serially clocked into cs5529.
5947 * this insures that the CSS_ADC_BUSY bit will get properly
5948 * set before we exit this function.
5950 for (i
= 0; i
< timeout
; i
++) {
5951 if ((ni_ao_win_inw(dev
, CAL_ADC_Status_67xx
) & CSS_ADC_BUSY
))
5955 /* printk("looped %i times writing command to cs5529\n", i); */
5957 comedi_error(dev
, "possible problem - never saw adc go busy?");
5961 /* write to cs5529 register */
5962 static void cs5529_config_write(struct comedi_device
*dev
, unsigned int value
,
5963 unsigned int reg_select_bits
)
5965 ni_ao_win_outw(dev
, ((value
>> 16) & 0xff),
5966 CAL_ADC_Config_Data_High_Word_67xx
);
5967 ni_ao_win_outw(dev
, (value
& 0xffff),
5968 CAL_ADC_Config_Data_Low_Word_67xx
);
5969 reg_select_bits
&= CSCMD_REGISTER_SELECT_MASK
;
5970 cs5529_command(dev
, CSCMD_COMMAND
| reg_select_bits
);
5971 if (cs5529_wait_for_idle(dev
))
5972 comedi_error(dev
, "time or signal in cs5529_config_write()");
5975 #ifdef NI_CS5529_DEBUG
5976 /* read from cs5529 register */
5977 static unsigned int cs5529_config_read(struct comedi_device
*dev
,
5978 unsigned int reg_select_bits
)
5982 reg_select_bits
&= CSCMD_REGISTER_SELECT_MASK
;
5983 cs5529_command(dev
, CSCMD_COMMAND
| CSCMD_READ
| reg_select_bits
);
5984 if (cs5529_wait_for_idle(dev
))
5985 comedi_error(dev
, "timeout or signal in cs5529_config_read()");
5986 value
= (ni_ao_win_inw(dev
,
5987 CAL_ADC_Config_Data_High_Word_67xx
) << 16) &
5989 value
|= ni_ao_win_inw(dev
, CAL_ADC_Config_Data_Low_Word_67xx
) & 0xffff;
5994 static int cs5529_do_conversion(struct comedi_device
*dev
, unsigned short *data
)
5997 unsigned short status
;
5999 cs5529_command(dev
, CSCMD_COMMAND
| CSCMD_SINGLE_CONVERSION
);
6000 retval
= cs5529_wait_for_idle(dev
);
6003 "timeout or signal in cs5529_do_conversion()");
6006 status
= ni_ao_win_inw(dev
, CAL_ADC_Status_67xx
);
6007 if (status
& CSS_OSC_DETECT
) {
6009 ("ni_mio_common: cs5529 conversion error, status CSS_OSC_DETECT\n");
6012 if (status
& CSS_OVERRANGE
) {
6014 ("ni_mio_common: cs5529 conversion error, overrange (ignoring)\n");
6017 *data
= ni_ao_win_inw(dev
, CAL_ADC_Data_67xx
);
6018 /* cs5529 returns 16 bit signed data in bipolar mode */
6024 static int cs5529_ai_insn_read(struct comedi_device
*dev
,
6025 struct comedi_subdevice
*s
,
6026 struct comedi_insn
*insn
, unsigned int *data
)
6029 unsigned short sample
;
6030 unsigned int channel_select
;
6031 const unsigned int INTERNAL_REF
= 0x1000;
6033 /* Set calibration adc source. Docs lie, reference select bits 8 to 11
6034 * do nothing. bit 12 seems to chooses internal reference voltage, bit
6035 * 13 causes the adc input to go overrange (maybe reads external reference?) */
6036 if (insn
->chanspec
& CR_ALT_SOURCE
)
6037 channel_select
= INTERNAL_REF
;
6039 channel_select
= CR_CHAN(insn
->chanspec
);
6040 ni_ao_win_outw(dev
, channel_select
, AO_Calibration_Channel_Select_67xx
);
6042 for (n
= 0; n
< insn
->n
; n
++) {
6043 retval
= cs5529_do_conversion(dev
, &sample
);
6051 static int init_cs5529(struct comedi_device
*dev
)
6053 unsigned int config_bits
=
6054 CSCFG_PORT_MODE
| CSCFG_WORD_RATE_2180_CYCLES
;
6057 /* do self-calibration */
6058 cs5529_config_write(dev
, config_bits
| CSCFG_SELF_CAL_OFFSET_GAIN
,
6059 CSCMD_CONFIG_REGISTER
);
6060 /* need to force a conversion for calibration to run */
6061 cs5529_do_conversion(dev
, NULL
);
6063 /* force gain calibration to 1 */
6064 cs5529_config_write(dev
, 0x400000, CSCMD_GAIN_REGISTER
);
6065 cs5529_config_write(dev
, config_bits
| CSCFG_SELF_CAL_OFFSET
,
6066 CSCMD_CONFIG_REGISTER
);
6067 if (cs5529_wait_for_idle(dev
))
6068 comedi_error(dev
, "timeout or signal in init_cs5529()\n");
6070 #ifdef NI_CS5529_DEBUG
6071 printk("config: 0x%x\n", cs5529_config_read(dev
,
6072 CSCMD_CONFIG_REGISTER
));
6073 printk("gain: 0x%x\n", cs5529_config_read(dev
, CSCMD_GAIN_REGISTER
));
6074 printk("offset: 0x%x\n", cs5529_config_read(dev
,
6075 CSCMD_OFFSET_REGISTER
));