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 const struct ni_board_struct
*board
= comedi_board(dev
);
700 struct ni_private
*devpriv
= dev
->private;
702 if (board
->reg_type
& ni_reg_m_series_mask
)
704 switch (gpct_index
) {
707 devpriv
->stc_writew(dev
, G0_Gate_Second_Irq_Enable
,
708 Second_IRQ_A_Enable_Register
);
710 devpriv
->stc_writew(dev
, 0,
711 Second_IRQ_A_Enable_Register
);
716 devpriv
->stc_writew(dev
, G1_Gate_Second_Irq_Enable
,
717 Second_IRQ_B_Enable_Register
);
719 devpriv
->stc_writew(dev
, 0,
720 Second_IRQ_B_Enable_Register
);
730 static void ni_clear_ai_fifo(struct comedi_device
*dev
)
732 const struct ni_board_struct
*board
= comedi_board(dev
);
733 struct ni_private
*devpriv
= dev
->private;
735 if (board
->reg_type
== ni_reg_6143
) {
736 /* Flush the 6143 data FIFO */
737 ni_writel(0x10, AIFIFO_Control_6143
); /* Flush fifo */
738 ni_writel(0x00, AIFIFO_Control_6143
); /* Flush fifo */
739 while (ni_readl(AIFIFO_Status_6143
) & 0x10) ; /* Wait for complete */
741 devpriv
->stc_writew(dev
, 1, ADC_FIFO_Clear
);
742 if (board
->reg_type
== ni_reg_625x
) {
743 ni_writeb(0, M_Offset_Static_AI_Control(0));
744 ni_writeb(1, M_Offset_Static_AI_Control(0));
746 /* the NI example code does 3 convert pulses for 625x boards,
747 but that appears to be wrong in practice. */
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
);
752 devpriv
->stc_writew(dev
, AI_CONVERT_Pulse
,
753 AI_Command_1_Register
);
759 static void win_out2(struct comedi_device
*dev
, uint32_t data
, int reg
)
761 struct ni_private
*devpriv
= dev
->private;
763 devpriv
->stc_writew(dev
, data
>> 16, reg
);
764 devpriv
->stc_writew(dev
, data
& 0xffff, reg
+ 1);
767 static uint32_t win_in2(struct comedi_device
*dev
, int reg
)
769 struct ni_private
*devpriv
= dev
->private;
772 bits
= devpriv
->stc_readw(dev
, reg
) << 16;
773 bits
|= devpriv
->stc_readw(dev
, reg
+ 1);
777 #define ao_win_out(data, addr) ni_ao_win_outw(dev, data, addr)
778 static inline void ni_ao_win_outw(struct comedi_device
*dev
, uint16_t data
,
781 struct ni_private
*devpriv
= dev
->private;
784 spin_lock_irqsave(&devpriv
->window_lock
, flags
);
785 ni_writew(addr
, AO_Window_Address_611x
);
786 ni_writew(data
, AO_Window_Data_611x
);
787 spin_unlock_irqrestore(&devpriv
->window_lock
, flags
);
790 static inline void ni_ao_win_outl(struct comedi_device
*dev
, uint32_t data
,
793 struct ni_private
*devpriv
= dev
->private;
796 spin_lock_irqsave(&devpriv
->window_lock
, flags
);
797 ni_writew(addr
, AO_Window_Address_611x
);
798 ni_writel(data
, AO_Window_Data_611x
);
799 spin_unlock_irqrestore(&devpriv
->window_lock
, flags
);
802 static inline unsigned short ni_ao_win_inw(struct comedi_device
*dev
, int addr
)
804 struct ni_private
*devpriv
= dev
->private;
808 spin_lock_irqsave(&devpriv
->window_lock
, flags
);
809 ni_writew(addr
, AO_Window_Address_611x
);
810 data
= ni_readw(AO_Window_Data_611x
);
811 spin_unlock_irqrestore(&devpriv
->window_lock
, flags
);
815 /* ni_set_bits( ) allows different parts of the ni_mio_common driver to
816 * share registers (such as Interrupt_A_Register) without interfering with
819 * NOTE: the switch/case statements are optimized out for a constant argument
820 * so this is actually quite fast--- If you must wrap another function around this
821 * make it inline to avoid a large speed penalty.
823 * value should only be 1 or 0.
825 static inline void ni_set_bits(struct comedi_device
*dev
, int reg
,
826 unsigned bits
, unsigned value
)
834 ni_set_bitfield(dev
, reg
, bits
, bit_values
);
837 static irqreturn_t
ni_E_interrupt(int irq
, void *d
)
839 struct comedi_device
*dev
= d
;
840 struct ni_private
*devpriv
= dev
->private;
841 unsigned short a_status
;
842 unsigned short b_status
;
843 unsigned int ai_mite_status
= 0;
844 unsigned int ao_mite_status
= 0;
847 struct mite_struct
*mite
= devpriv
->mite
;
852 smp_mb(); /* make sure dev->attached is checked before handler does anything else. */
854 /* lock to avoid race with comedi_poll */
855 spin_lock_irqsave(&dev
->spinlock
, flags
);
856 a_status
= devpriv
->stc_readw(dev
, AI_Status_1_Register
);
857 b_status
= devpriv
->stc_readw(dev
, AO_Status_1_Register
);
860 unsigned long flags_too
;
862 spin_lock_irqsave(&devpriv
->mite_channel_lock
, flags_too
);
863 if (devpriv
->ai_mite_chan
) {
864 ai_mite_status
= mite_get_status(devpriv
->ai_mite_chan
);
865 if (ai_mite_status
& CHSR_LINKC
)
867 devpriv
->mite
->mite_io_addr
+
869 ai_mite_chan
->channel
));
871 if (devpriv
->ao_mite_chan
) {
872 ao_mite_status
= mite_get_status(devpriv
->ao_mite_chan
);
873 if (ao_mite_status
& CHSR_LINKC
)
877 ao_mite_chan
->channel
));
879 spin_unlock_irqrestore(&devpriv
->mite_channel_lock
, flags_too
);
882 ack_a_interrupt(dev
, a_status
);
883 ack_b_interrupt(dev
, b_status
);
884 if ((a_status
& Interrupt_A_St
) || (ai_mite_status
& CHSR_INT
))
885 handle_a_interrupt(dev
, a_status
, ai_mite_status
);
886 if ((b_status
& Interrupt_B_St
) || (ao_mite_status
& CHSR_INT
))
887 handle_b_interrupt(dev
, b_status
, ao_mite_status
);
888 handle_gpct_interrupt(dev
, 0);
889 handle_gpct_interrupt(dev
, 1);
890 handle_cdio_interrupt(dev
);
892 spin_unlock_irqrestore(&dev
->spinlock
, flags
);
897 static void ni_sync_ai_dma(struct comedi_device
*dev
)
899 struct ni_private
*devpriv
= dev
->private;
900 struct comedi_subdevice
*s
= &dev
->subdevices
[NI_AI_SUBDEV
];
903 spin_lock_irqsave(&devpriv
->mite_channel_lock
, flags
);
904 if (devpriv
->ai_mite_chan
)
905 mite_sync_input_dma(devpriv
->ai_mite_chan
, s
->async
);
906 spin_unlock_irqrestore(&devpriv
->mite_channel_lock
, flags
);
909 static void mite_handle_b_linkc(struct mite_struct
*mite
,
910 struct comedi_device
*dev
)
912 struct ni_private
*devpriv
= dev
->private;
913 struct comedi_subdevice
*s
= &dev
->subdevices
[NI_AO_SUBDEV
];
916 spin_lock_irqsave(&devpriv
->mite_channel_lock
, flags
);
917 if (devpriv
->ao_mite_chan
) {
918 mite_sync_output_dma(devpriv
->ao_mite_chan
, s
->async
);
920 spin_unlock_irqrestore(&devpriv
->mite_channel_lock
, flags
);
923 static int ni_ao_wait_for_dma_load(struct comedi_device
*dev
)
925 struct ni_private
*devpriv
= dev
->private;
926 static const int timeout
= 10000;
928 for (i
= 0; i
< timeout
; i
++) {
929 unsigned short b_status
;
931 b_status
= devpriv
->stc_readw(dev
, AO_Status_1_Register
);
932 if (b_status
& AO_FIFO_Half_Full_St
)
934 /* if we poll too often, the pci bus activity seems
935 to slow the dma transfer down */
939 comedi_error(dev
, "timed out waiting for dma load");
946 static void ni_handle_eos(struct comedi_device
*dev
, struct comedi_subdevice
*s
)
948 struct ni_private
*devpriv
= dev
->private;
950 if (devpriv
->aimode
== AIMODE_SCAN
) {
952 static const int timeout
= 10;
955 for (i
= 0; i
< timeout
; i
++) {
957 if ((s
->async
->events
& COMEDI_CB_EOS
))
962 ni_handle_fifo_dregs(dev
);
963 s
->async
->events
|= COMEDI_CB_EOS
;
966 /* handle special case of single scan using AI_End_On_End_Of_Scan */
967 if ((devpriv
->ai_cmd2
& AI_End_On_End_Of_Scan
)) {
968 shutdown_ai_command(dev
);
972 static void shutdown_ai_command(struct comedi_device
*dev
)
974 struct comedi_subdevice
*s
= &dev
->subdevices
[NI_AI_SUBDEV
];
977 ni_ai_drain_dma(dev
);
979 ni_handle_fifo_dregs(dev
);
980 get_last_sample_611x(dev
);
981 get_last_sample_6143(dev
);
983 s
->async
->events
|= COMEDI_CB_EOA
;
986 static void ni_event(struct comedi_device
*dev
, struct comedi_subdevice
*s
)
989 async
->events
& (COMEDI_CB_ERROR
| COMEDI_CB_OVERFLOW
|
998 case NI_GPCT0_SUBDEV
:
999 case NI_GPCT1_SUBDEV
:
1000 ni_gpct_cancel(dev
, s
);
1003 ni_cdio_cancel(dev
, s
);
1009 comedi_event(dev
, s
);
1012 static void handle_gpct_interrupt(struct comedi_device
*dev
,
1013 unsigned short counter_index
)
1016 struct ni_private
*devpriv
= dev
->private;
1017 struct comedi_subdevice
*s
;
1019 s
= &dev
->subdevices
[NI_GPCT_SUBDEV(counter_index
)];
1021 ni_tio_handle_interrupt(&devpriv
->counter_dev
->counters
[counter_index
],
1023 if (s
->async
->events
)
1028 static void ack_a_interrupt(struct comedi_device
*dev
, unsigned short a_status
)
1030 struct ni_private
*devpriv
= dev
->private;
1031 unsigned short ack
= 0;
1033 if (a_status
& AI_SC_TC_St
) {
1034 ack
|= AI_SC_TC_Interrupt_Ack
;
1036 if (a_status
& AI_START1_St
) {
1037 ack
|= AI_START1_Interrupt_Ack
;
1039 if (a_status
& AI_START_St
) {
1040 ack
|= AI_START_Interrupt_Ack
;
1042 if (a_status
& AI_STOP_St
) {
1043 /* not sure why we used to ack the START here also, instead of doing it independently. Frank Hess 2007-07-06 */
1044 ack
|= AI_STOP_Interrupt_Ack
/*| AI_START_Interrupt_Ack */ ;
1047 devpriv
->stc_writew(dev
, ack
, Interrupt_A_Ack_Register
);
1050 static void handle_a_interrupt(struct comedi_device
*dev
, unsigned short status
,
1051 unsigned ai_mite_status
)
1053 struct ni_private
*devpriv
= dev
->private;
1054 struct comedi_subdevice
*s
= &dev
->subdevices
[NI_AI_SUBDEV
];
1056 /* 67xx boards don't have ai subdevice, but their gpct0 might generate an a interrupt */
1057 if (s
->type
== COMEDI_SUBD_UNUSED
)
1060 #ifdef DEBUG_INTERRUPT
1062 ("ni_mio_common: interrupt: a_status=%04x ai_mite_status=%08x\n",
1063 status
, ai_mite_status
);
1064 ni_mio_print_status_a(status
);
1067 if (ai_mite_status
& CHSR_LINKC
) {
1068 ni_sync_ai_dma(dev
);
1071 if (ai_mite_status
& ~(CHSR_INT
| CHSR_LINKC
| CHSR_DONE
| CHSR_MRDY
|
1072 CHSR_DRDY
| CHSR_DRQ1
| CHSR_DRQ0
| CHSR_ERROR
|
1073 CHSR_SABORT
| CHSR_XFERR
| CHSR_LxERR_mask
)) {
1075 ("unknown mite interrupt, ack! (ai_mite_status=%08x)\n",
1077 /* mite_print_chsr(ai_mite_status); */
1078 s
->async
->events
|= COMEDI_CB_ERROR
| COMEDI_CB_EOA
;
1079 /* disable_irq(dev->irq); */
1083 /* test for all uncommon interrupt events at the same time */
1084 if (status
& (AI_Overrun_St
| AI_Overflow_St
| AI_SC_TC_Error_St
|
1085 AI_SC_TC_St
| AI_START1_St
)) {
1086 if (status
== 0xffff) {
1088 ("ni_mio_common: a_status=0xffff. Card removed?\n");
1089 /* we probably aren't even running a command now,
1090 * so it's a good idea to be careful. */
1091 if (comedi_is_subdevice_running(s
)) {
1093 COMEDI_CB_ERROR
| COMEDI_CB_EOA
;
1098 if (status
& (AI_Overrun_St
| AI_Overflow_St
|
1099 AI_SC_TC_Error_St
)) {
1100 printk("ni_mio_common: ai error a_status=%04x\n",
1102 ni_mio_print_status_a(status
);
1104 shutdown_ai_command(dev
);
1106 s
->async
->events
|= COMEDI_CB_ERROR
;
1107 if (status
& (AI_Overrun_St
| AI_Overflow_St
))
1108 s
->async
->events
|= COMEDI_CB_OVERFLOW
;
1114 if (status
& AI_SC_TC_St
) {
1115 #ifdef DEBUG_INTERRUPT
1116 printk("ni_mio_common: SC_TC interrupt\n");
1118 if (!devpriv
->ai_continuous
) {
1119 shutdown_ai_command(dev
);
1124 if (status
& AI_FIFO_Half_Full_St
) {
1126 static const int timeout
= 10;
1127 /* pcmcia cards (at least 6036) seem to stop producing interrupts if we
1128 *fail to get the fifo less than half full, so loop to be sure.*/
1129 for (i
= 0; i
< timeout
; ++i
) {
1130 ni_handle_fifo_half_full(dev
);
1131 if ((devpriv
->stc_readw(dev
,
1132 AI_Status_1_Register
) &
1133 AI_FIFO_Half_Full_St
) == 0)
1137 #endif /* !PCIDMA */
1139 if ((status
& AI_STOP_St
)) {
1140 ni_handle_eos(dev
, s
);
1145 #ifdef DEBUG_INTERRUPT
1146 status
= devpriv
->stc_readw(dev
, AI_Status_1_Register
);
1147 if (status
& Interrupt_A_St
) {
1149 ("handle_a_interrupt: didn't clear interrupt? status=0x%x\n",
1155 static void ack_b_interrupt(struct comedi_device
*dev
, unsigned short b_status
)
1157 struct ni_private
*devpriv
= dev
->private;
1158 unsigned short ack
= 0;
1160 if (b_status
& AO_BC_TC_St
) {
1161 ack
|= AO_BC_TC_Interrupt_Ack
;
1163 if (b_status
& AO_Overrun_St
) {
1164 ack
|= AO_Error_Interrupt_Ack
;
1166 if (b_status
& AO_START_St
) {
1167 ack
|= AO_START_Interrupt_Ack
;
1169 if (b_status
& AO_START1_St
) {
1170 ack
|= AO_START1_Interrupt_Ack
;
1172 if (b_status
& AO_UC_TC_St
) {
1173 ack
|= AO_UC_TC_Interrupt_Ack
;
1175 if (b_status
& AO_UI2_TC_St
) {
1176 ack
|= AO_UI2_TC_Interrupt_Ack
;
1178 if (b_status
& AO_UPDATE_St
) {
1179 ack
|= AO_UPDATE_Interrupt_Ack
;
1182 devpriv
->stc_writew(dev
, ack
, Interrupt_B_Ack_Register
);
1185 static void handle_b_interrupt(struct comedi_device
*dev
,
1186 unsigned short b_status
, unsigned ao_mite_status
)
1188 struct ni_private
*devpriv
= dev
->private;
1189 struct comedi_subdevice
*s
= &dev
->subdevices
[NI_AO_SUBDEV
];
1190 /* unsigned short ack=0; */
1192 #ifdef DEBUG_INTERRUPT
1193 printk("ni_mio_common: interrupt: b_status=%04x m1_status=%08x\n",
1194 b_status
, ao_mite_status
);
1195 ni_mio_print_status_b(b_status
);
1199 /* Currently, mite.c requires us to handle LINKC */
1200 if (ao_mite_status
& CHSR_LINKC
) {
1201 mite_handle_b_linkc(devpriv
->mite
, dev
);
1204 if (ao_mite_status
& ~(CHSR_INT
| CHSR_LINKC
| CHSR_DONE
| CHSR_MRDY
|
1205 CHSR_DRDY
| CHSR_DRQ1
| CHSR_DRQ0
| CHSR_ERROR
|
1206 CHSR_SABORT
| CHSR_XFERR
| CHSR_LxERR_mask
)) {
1208 ("unknown mite interrupt, ack! (ao_mite_status=%08x)\n",
1210 /* mite_print_chsr(ao_mite_status); */
1211 s
->async
->events
|= COMEDI_CB_EOA
| COMEDI_CB_ERROR
;
1215 if (b_status
== 0xffff)
1217 if (b_status
& AO_Overrun_St
) {
1219 ("ni_mio_common: AO FIFO underrun status=0x%04x status2=0x%04x\n",
1220 b_status
, devpriv
->stc_readw(dev
, AO_Status_2_Register
));
1221 s
->async
->events
|= COMEDI_CB_OVERFLOW
;
1224 if (b_status
& AO_BC_TC_St
) {
1226 ("ni_mio_common: AO BC_TC status=0x%04x status2=0x%04x\n",
1227 b_status
, devpriv
->stc_readw(dev
, AO_Status_2_Register
));
1228 s
->async
->events
|= COMEDI_CB_EOA
;
1231 if (b_status
& AO_FIFO_Request_St
) {
1234 ret
= ni_ao_fifo_half_empty(dev
, s
);
1236 printk("ni_mio_common: AO buffer underrun\n");
1237 ni_set_bits(dev
, Interrupt_B_Enable_Register
,
1238 AO_FIFO_Interrupt_Enable
|
1239 AO_Error_Interrupt_Enable
, 0);
1240 s
->async
->events
|= COMEDI_CB_OVERFLOW
;
1248 #ifdef DEBUG_STATUS_A
1249 static const char *const status_a_strings
[] = {
1250 "passthru0", "fifo", "G0_gate", "G0_TC",
1251 "stop", "start", "sc_tc", "start1",
1252 "start2", "sc_tc_error", "overflow", "overrun",
1253 "fifo_empty", "fifo_half_full", "fifo_full", "interrupt_a"
1256 static void ni_mio_print_status_a(int status
)
1260 printk("A status:");
1261 for (i
= 15; i
>= 0; i
--) {
1262 if (status
& (1 << i
)) {
1263 printk(" %s", status_a_strings
[i
]);
1270 #ifdef DEBUG_STATUS_B
1271 static const char *const status_b_strings
[] = {
1272 "passthru1", "fifo", "G1_gate", "G1_TC",
1273 "UI2_TC", "UPDATE", "UC_TC", "BC_TC",
1274 "start1", "overrun", "start", "bc_tc_error",
1275 "fifo_empty", "fifo_half_full", "fifo_full", "interrupt_b"
1278 static void ni_mio_print_status_b(int status
)
1282 printk("B status:");
1283 for (i
= 15; i
>= 0; i
--) {
1284 if (status
& (1 << i
)) {
1285 printk(" %s", status_b_strings
[i
]);
1294 static void ni_ao_fifo_load(struct comedi_device
*dev
,
1295 struct comedi_subdevice
*s
, int n
)
1297 const struct ni_board_struct
*board
= comedi_board(dev
);
1298 struct comedi_async
*async
= s
->async
;
1299 struct comedi_cmd
*cmd
= &async
->cmd
;
1307 chan
= async
->cur_chan
;
1308 for (i
= 0; i
< n
; i
++) {
1309 err
&= comedi_buf_get(async
, &d
);
1313 range
= CR_RANGE(cmd
->chanlist
[chan
]);
1315 if (board
->reg_type
& ni_reg_6xxx_mask
) {
1316 packed_data
= d
& 0xffff;
1317 /* 6711 only has 16 bit wide ao fifo */
1318 if (board
->reg_type
!= ni_reg_6711
) {
1319 err
&= comedi_buf_get(async
, &d
);
1324 packed_data
|= (d
<< 16) & 0xffff0000;
1326 ni_writel(packed_data
, DAC_FIFO_Data_611x
);
1328 ni_writew(d
, DAC_FIFO_Data
);
1331 chan
%= cmd
->chanlist_len
;
1333 async
->cur_chan
= chan
;
1335 async
->events
|= COMEDI_CB_OVERFLOW
;
1340 * There's a small problem if the FIFO gets really low and we
1341 * don't have the data to fill it. Basically, if after we fill
1342 * the FIFO with all the data available, the FIFO is _still_
1343 * less than half full, we never clear the interrupt. If the
1344 * IRQ is in edge mode, we never get another interrupt, because
1345 * this one wasn't cleared. If in level mode, we get flooded
1346 * with interrupts that we can't fulfill, because nothing ever
1347 * gets put into the buffer.
1349 * This kind of situation is recoverable, but it is easier to
1350 * just pretend we had a FIFO underrun, since there is a good
1351 * chance it will happen anyway. This is _not_ the case for
1352 * RT code, as RT code might purposely be running close to the
1353 * metal. Needs to be fixed eventually.
1355 static int ni_ao_fifo_half_empty(struct comedi_device
*dev
,
1356 struct comedi_subdevice
*s
)
1358 const struct ni_board_struct
*board
= comedi_board(dev
);
1361 n
= comedi_buf_read_n_available(s
->async
);
1363 s
->async
->events
|= COMEDI_CB_OVERFLOW
;
1368 if (n
> board
->ao_fifo_depth
/ 2)
1369 n
= board
->ao_fifo_depth
/ 2;
1371 ni_ao_fifo_load(dev
, s
, n
);
1373 s
->async
->events
|= COMEDI_CB_BLOCK
;
1378 static int ni_ao_prep_fifo(struct comedi_device
*dev
,
1379 struct comedi_subdevice
*s
)
1381 const struct ni_board_struct
*board
= comedi_board(dev
);
1382 struct ni_private
*devpriv
= dev
->private;
1386 devpriv
->stc_writew(dev
, 1, DAC_FIFO_Clear
);
1387 if (board
->reg_type
& ni_reg_6xxx_mask
)
1388 ni_ao_win_outl(dev
, 0x6, AO_FIFO_Offset_Load_611x
);
1390 /* load some data */
1391 n
= comedi_buf_read_n_available(s
->async
);
1396 if (n
> board
->ao_fifo_depth
)
1397 n
= board
->ao_fifo_depth
;
1399 ni_ao_fifo_load(dev
, s
, n
);
1404 static void ni_ai_fifo_read(struct comedi_device
*dev
,
1405 struct comedi_subdevice
*s
, int n
)
1407 const struct ni_board_struct
*board
= comedi_board(dev
);
1408 struct ni_private
*devpriv
= dev
->private;
1409 struct comedi_async
*async
= s
->async
;
1412 if (board
->reg_type
== ni_reg_611x
) {
1416 for (i
= 0; i
< n
/ 2; i
++) {
1417 dl
= ni_readl(ADC_FIFO_Data_611x
);
1418 /* This may get the hi/lo data in the wrong order */
1419 data
[0] = (dl
>> 16) & 0xffff;
1420 data
[1] = dl
& 0xffff;
1421 cfc_write_array_to_buffer(s
, data
, sizeof(data
));
1423 /* Check if there's a single sample stuck in the FIFO */
1425 dl
= ni_readl(ADC_FIFO_Data_611x
);
1426 data
[0] = dl
& 0xffff;
1427 cfc_write_to_buffer(s
, data
[0]);
1429 } else if (board
->reg_type
== ni_reg_6143
) {
1433 /* This just reads the FIFO assuming the data is present, no checks on the FIFO status are performed */
1434 for (i
= 0; i
< n
/ 2; i
++) {
1435 dl
= ni_readl(AIFIFO_Data_6143
);
1437 data
[0] = (dl
>> 16) & 0xffff;
1438 data
[1] = dl
& 0xffff;
1439 cfc_write_array_to_buffer(s
, data
, sizeof(data
));
1442 /* Assume there is a single sample stuck in the FIFO */
1443 ni_writel(0x01, AIFIFO_Control_6143
); /* Get stranded sample into FIFO */
1444 dl
= ni_readl(AIFIFO_Data_6143
);
1445 data
[0] = (dl
>> 16) & 0xffff;
1446 cfc_write_to_buffer(s
, data
[0]);
1449 if (n
> sizeof(devpriv
->ai_fifo_buffer
) /
1450 sizeof(devpriv
->ai_fifo_buffer
[0])) {
1451 comedi_error(dev
, "bug! ai_fifo_buffer too small");
1452 async
->events
|= COMEDI_CB_ERROR
;
1455 for (i
= 0; i
< n
; i
++) {
1456 devpriv
->ai_fifo_buffer
[i
] =
1457 ni_readw(ADC_FIFO_Data_Register
);
1459 cfc_write_array_to_buffer(s
, devpriv
->ai_fifo_buffer
,
1461 sizeof(devpriv
->ai_fifo_buffer
[0]));
1465 static void ni_handle_fifo_half_full(struct comedi_device
*dev
)
1467 const struct ni_board_struct
*board
= comedi_board(dev
);
1468 struct comedi_subdevice
*s
= &dev
->subdevices
[NI_AI_SUBDEV
];
1471 n
= board
->ai_fifo_depth
/ 2;
1473 ni_ai_fifo_read(dev
, s
, n
);
1478 static int ni_ai_drain_dma(struct comedi_device
*dev
)
1480 struct ni_private
*devpriv
= dev
->private;
1482 static const int timeout
= 10000;
1483 unsigned long flags
;
1486 spin_lock_irqsave(&devpriv
->mite_channel_lock
, flags
);
1487 if (devpriv
->ai_mite_chan
) {
1488 for (i
= 0; i
< timeout
; i
++) {
1489 if ((devpriv
->stc_readw(dev
,
1490 AI_Status_1_Register
) &
1492 && mite_bytes_in_transit(devpriv
->ai_mite_chan
) ==
1498 printk("ni_mio_common: wait for dma drain timed out\n");
1500 ("mite_bytes_in_transit=%i, AI_Status1_Register=0x%x\n",
1501 mite_bytes_in_transit(devpriv
->ai_mite_chan
),
1502 devpriv
->stc_readw(dev
, AI_Status_1_Register
));
1506 spin_unlock_irqrestore(&devpriv
->mite_channel_lock
, flags
);
1508 ni_sync_ai_dma(dev
);
1516 static void ni_handle_fifo_dregs(struct comedi_device
*dev
)
1518 const struct ni_board_struct
*board
= comedi_board(dev
);
1519 struct ni_private
*devpriv
= dev
->private;
1520 struct comedi_subdevice
*s
= &dev
->subdevices
[NI_AI_SUBDEV
];
1526 if (board
->reg_type
== ni_reg_611x
) {
1527 while ((devpriv
->stc_readw(dev
,
1528 AI_Status_1_Register
) &
1529 AI_FIFO_Empty_St
) == 0) {
1530 dl
= ni_readl(ADC_FIFO_Data_611x
);
1532 /* This may get the hi/lo data in the wrong order */
1533 data
[0] = (dl
>> 16);
1534 data
[1] = (dl
& 0xffff);
1535 cfc_write_array_to_buffer(s
, data
, sizeof(data
));
1537 } else if (board
->reg_type
== ni_reg_6143
) {
1539 while (ni_readl(AIFIFO_Status_6143
) & 0x04) {
1540 dl
= ni_readl(AIFIFO_Data_6143
);
1542 /* This may get the hi/lo data in the wrong order */
1543 data
[0] = (dl
>> 16);
1544 data
[1] = (dl
& 0xffff);
1545 cfc_write_array_to_buffer(s
, data
, sizeof(data
));
1548 /* Check if stranded sample is present */
1549 if (ni_readl(AIFIFO_Status_6143
) & 0x01) {
1550 ni_writel(0x01, AIFIFO_Control_6143
); /* Get stranded sample into FIFO */
1551 dl
= ni_readl(AIFIFO_Data_6143
);
1552 data
[0] = (dl
>> 16) & 0xffff;
1553 cfc_write_to_buffer(s
, data
[0]);
1558 devpriv
->stc_readw(dev
,
1559 AI_Status_1_Register
) & AI_FIFO_Empty_St
;
1560 while (fifo_empty
== 0) {
1563 sizeof(devpriv
->ai_fifo_buffer
) /
1564 sizeof(devpriv
->ai_fifo_buffer
[0]); i
++) {
1566 devpriv
->stc_readw(dev
,
1567 AI_Status_1_Register
) &
1571 devpriv
->ai_fifo_buffer
[i
] =
1572 ni_readw(ADC_FIFO_Data_Register
);
1574 cfc_write_array_to_buffer(s
, devpriv
->ai_fifo_buffer
,
1577 ai_fifo_buffer
[0]));
1582 static void get_last_sample_611x(struct comedi_device
*dev
)
1584 const struct ni_board_struct
*board
= comedi_board(dev
);
1585 struct ni_private
*devpriv __maybe_unused
= dev
->private;
1586 struct comedi_subdevice
*s
= &dev
->subdevices
[NI_AI_SUBDEV
];
1590 if (board
->reg_type
!= ni_reg_611x
)
1593 /* Check if there's a single sample stuck in the FIFO */
1594 if (ni_readb(XXX_Status
) & 0x80) {
1595 dl
= ni_readl(ADC_FIFO_Data_611x
);
1596 data
= (dl
& 0xffff);
1597 cfc_write_to_buffer(s
, data
);
1601 static void get_last_sample_6143(struct comedi_device
*dev
)
1603 const struct ni_board_struct
*board
= comedi_board(dev
);
1604 struct ni_private
*devpriv __maybe_unused
= dev
->private;
1605 struct comedi_subdevice
*s
= &dev
->subdevices
[NI_AI_SUBDEV
];
1609 if (board
->reg_type
!= ni_reg_6143
)
1612 /* Check if there's a single sample stuck in the FIFO */
1613 if (ni_readl(AIFIFO_Status_6143
) & 0x01) {
1614 ni_writel(0x01, AIFIFO_Control_6143
); /* Get stranded sample into FIFO */
1615 dl
= ni_readl(AIFIFO_Data_6143
);
1617 /* This may get the hi/lo data in the wrong order */
1618 data
= (dl
>> 16) & 0xffff;
1619 cfc_write_to_buffer(s
, data
);
1623 static void ni_ai_munge(struct comedi_device
*dev
, struct comedi_subdevice
*s
,
1624 void *data
, unsigned int num_bytes
,
1625 unsigned int chan_index
)
1627 struct ni_private
*devpriv
= dev
->private;
1628 struct comedi_async
*async
= s
->async
;
1630 unsigned int length
= num_bytes
/ bytes_per_sample(s
);
1631 short *array
= data
;
1632 unsigned int *larray
= data
;
1634 for (i
= 0; i
< length
; i
++) {
1636 if (s
->subdev_flags
& SDF_LSAMPL
)
1637 larray
[i
] = le32_to_cpu(larray
[i
]);
1639 array
[i
] = le16_to_cpu(array
[i
]);
1641 if (s
->subdev_flags
& SDF_LSAMPL
)
1642 larray
[i
] += devpriv
->ai_offset
[chan_index
];
1644 array
[i
] += devpriv
->ai_offset
[chan_index
];
1646 chan_index
%= async
->cmd
.chanlist_len
;
1652 static int ni_ai_setup_MITE_dma(struct comedi_device
*dev
)
1654 const struct ni_board_struct
*board
= comedi_board(dev
);
1655 struct ni_private
*devpriv
= dev
->private;
1656 struct comedi_subdevice
*s
= &dev
->subdevices
[NI_AI_SUBDEV
];
1658 unsigned long flags
;
1660 retval
= ni_request_ai_mite_channel(dev
);
1663 /* printk("comedi_debug: using mite channel %i for ai.\n", devpriv->ai_mite_chan->channel); */
1665 /* write alloc the entire buffer */
1666 comedi_buf_write_alloc(s
->async
, s
->async
->prealloc_bufsz
);
1668 spin_lock_irqsave(&devpriv
->mite_channel_lock
, flags
);
1669 if (devpriv
->ai_mite_chan
== NULL
) {
1670 spin_unlock_irqrestore(&devpriv
->mite_channel_lock
, flags
);
1674 switch (board
->reg_type
) {
1677 mite_prep_dma(devpriv
->ai_mite_chan
, 32, 16);
1680 mite_prep_dma(devpriv
->ai_mite_chan
, 32, 32);
1683 mite_prep_dma(devpriv
->ai_mite_chan
, 16, 16);
1687 mite_dma_arm(devpriv
->ai_mite_chan
);
1688 spin_unlock_irqrestore(&devpriv
->mite_channel_lock
, flags
);
1693 static int ni_ao_setup_MITE_dma(struct comedi_device
*dev
)
1695 const struct ni_board_struct
*board
= comedi_board(dev
);
1696 struct ni_private
*devpriv
= dev
->private;
1697 struct comedi_subdevice
*s
= &dev
->subdevices
[NI_AO_SUBDEV
];
1699 unsigned long flags
;
1701 retval
= ni_request_ao_mite_channel(dev
);
1705 /* read alloc the entire buffer */
1706 comedi_buf_read_alloc(s
->async
, s
->async
->prealloc_bufsz
);
1708 spin_lock_irqsave(&devpriv
->mite_channel_lock
, flags
);
1709 if (devpriv
->ao_mite_chan
) {
1710 if (board
->reg_type
& (ni_reg_611x
| ni_reg_6713
)) {
1711 mite_prep_dma(devpriv
->ao_mite_chan
, 32, 32);
1713 /* doing 32 instead of 16 bit wide transfers from memory
1714 makes the mite do 32 bit pci transfers, doubling pci bandwidth. */
1715 mite_prep_dma(devpriv
->ao_mite_chan
, 16, 32);
1717 mite_dma_arm(devpriv
->ao_mite_chan
);
1720 spin_unlock_irqrestore(&devpriv
->mite_channel_lock
, flags
);
1728 used for both cancel ioctl and board initialization
1730 this is pretty harsh for a cancel, but it works...
1733 static int ni_ai_reset(struct comedi_device
*dev
, struct comedi_subdevice
*s
)
1735 const struct ni_board_struct
*board
= comedi_board(dev
);
1736 struct ni_private
*devpriv
= dev
->private;
1738 ni_release_ai_mite_channel(dev
);
1739 /* ai configuration */
1740 devpriv
->stc_writew(dev
, AI_Configuration_Start
| AI_Reset
,
1741 Joint_Reset_Register
);
1743 ni_set_bits(dev
, Interrupt_A_Enable_Register
,
1744 AI_SC_TC_Interrupt_Enable
| AI_START1_Interrupt_Enable
|
1745 AI_START2_Interrupt_Enable
| AI_START_Interrupt_Enable
|
1746 AI_STOP_Interrupt_Enable
| AI_Error_Interrupt_Enable
|
1747 AI_FIFO_Interrupt_Enable
, 0);
1749 ni_clear_ai_fifo(dev
);
1751 if (board
->reg_type
!= ni_reg_6143
)
1752 ni_writeb(0, Misc_Command
);
1754 devpriv
->stc_writew(dev
, AI_Disarm
, AI_Command_1_Register
); /* reset pulses */
1755 devpriv
->stc_writew(dev
,
1756 AI_Start_Stop
| AI_Mode_1_Reserved
1757 /*| AI_Trigger_Once */ ,
1758 AI_Mode_1_Register
);
1759 devpriv
->stc_writew(dev
, 0x0000, AI_Mode_2_Register
);
1760 /* generate FIFO interrupts on non-empty */
1761 devpriv
->stc_writew(dev
, (0 << 6) | 0x0000, AI_Mode_3_Register
);
1762 if (board
->reg_type
== ni_reg_611x
) {
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_High
),
1774 AI_Output_Control_Register
);
1775 } else if (board
->reg_type
== ni_reg_6143
) {
1776 devpriv
->stc_writew(dev
, AI_SHIFTIN_Pulse_Width
|
1778 AI_LOCALMUX_CLK_Pulse_Width
,
1779 AI_Personal_Register
);
1780 devpriv
->stc_writew(dev
,
1781 AI_SCAN_IN_PROG_Output_Select(3) |
1782 AI_EXTMUX_CLK_Output_Select(0) |
1783 AI_LOCALMUX_CLK_Output_Select(2) |
1784 AI_SC_TC_Output_Select(3) |
1785 AI_CONVERT_Output_Select
1786 (AI_CONVERT_Output_Enable_Low
),
1787 AI_Output_Control_Register
);
1789 unsigned ai_output_control_bits
;
1790 devpriv
->stc_writew(dev
, AI_SHIFTIN_Pulse_Width
|
1792 AI_CONVERT_Pulse_Width
|
1793 AI_LOCALMUX_CLK_Pulse_Width
,
1794 AI_Personal_Register
);
1795 ai_output_control_bits
=
1796 AI_SCAN_IN_PROG_Output_Select(3) |
1797 AI_EXTMUX_CLK_Output_Select(0) |
1798 AI_LOCALMUX_CLK_Output_Select(2) |
1799 AI_SC_TC_Output_Select(3);
1800 if (board
->reg_type
== ni_reg_622x
)
1801 ai_output_control_bits
|=
1802 AI_CONVERT_Output_Select
1803 (AI_CONVERT_Output_Enable_High
);
1805 ai_output_control_bits
|=
1806 AI_CONVERT_Output_Select
1807 (AI_CONVERT_Output_Enable_Low
);
1808 devpriv
->stc_writew(dev
, ai_output_control_bits
,
1809 AI_Output_Control_Register
);
1811 /* the following registers should not be changed, because there
1812 * are no backup registers in devpriv. If you want to change
1813 * any of these, add a backup register and other appropriate code:
1814 * AI_Mode_1_Register
1815 * AI_Mode_3_Register
1816 * AI_Personal_Register
1817 * AI_Output_Control_Register
1819 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 */
1821 devpriv
->stc_writew(dev
, AI_Configuration_End
, Joint_Reset_Register
);
1826 static int ni_ai_poll(struct comedi_device
*dev
, struct comedi_subdevice
*s
)
1828 unsigned long flags
;
1831 /* lock to avoid race with interrupt handler */
1832 spin_lock_irqsave(&dev
->spinlock
, flags
);
1834 ni_handle_fifo_dregs(dev
);
1836 ni_sync_ai_dma(dev
);
1838 count
= s
->async
->buf_write_count
- s
->async
->buf_read_count
;
1839 spin_unlock_irqrestore(&dev
->spinlock
, flags
);
1844 static int ni_ai_insn_read(struct comedi_device
*dev
,
1845 struct comedi_subdevice
*s
, struct comedi_insn
*insn
,
1848 const struct ni_board_struct
*board
= comedi_board(dev
);
1849 struct ni_private
*devpriv
= dev
->private;
1851 const unsigned int mask
= (1 << board
->adbits
) - 1;
1856 ni_load_channelgain_list(dev
, 1, &insn
->chanspec
);
1858 ni_clear_ai_fifo(dev
);
1860 signbits
= devpriv
->ai_offset
[0];
1861 if (board
->reg_type
== ni_reg_611x
) {
1862 for (n
= 0; n
< num_adc_stages_611x
; n
++) {
1863 devpriv
->stc_writew(dev
, AI_CONVERT_Pulse
,
1864 AI_Command_1_Register
);
1867 for (n
= 0; n
< insn
->n
; n
++) {
1868 devpriv
->stc_writew(dev
, AI_CONVERT_Pulse
,
1869 AI_Command_1_Register
);
1870 /* The 611x has screwy 32-bit FIFOs. */
1872 for (i
= 0; i
< NI_TIMEOUT
; i
++) {
1873 if (ni_readb(XXX_Status
) & 0x80) {
1874 d
= (ni_readl(ADC_FIFO_Data_611x
) >> 16)
1878 if (!(devpriv
->stc_readw(dev
,
1879 AI_Status_1_Register
) &
1880 AI_FIFO_Empty_St
)) {
1881 d
= ni_readl(ADC_FIFO_Data_611x
) &
1886 if (i
== NI_TIMEOUT
) {
1888 ("ni_mio_common: timeout in 611x ni_ai_insn_read\n");
1894 } else if (board
->reg_type
== ni_reg_6143
) {
1895 for (n
= 0; n
< insn
->n
; n
++) {
1896 devpriv
->stc_writew(dev
, AI_CONVERT_Pulse
,
1897 AI_Command_1_Register
);
1899 /* The 6143 has 32-bit FIFOs. You need to strobe a bit to move a single 16bit stranded sample into the FIFO */
1901 for (i
= 0; i
< NI_TIMEOUT
; i
++) {
1902 if (ni_readl(AIFIFO_Status_6143
) & 0x01) {
1903 ni_writel(0x01, AIFIFO_Control_6143
); /* Get stranded sample into FIFO */
1904 dl
= ni_readl(AIFIFO_Data_6143
);
1908 if (i
== NI_TIMEOUT
) {
1910 ("ni_mio_common: timeout in 6143 ni_ai_insn_read\n");
1913 data
[n
] = (((dl
>> 16) & 0xFFFF) + signbits
) & 0xFFFF;
1916 for (n
= 0; n
< insn
->n
; n
++) {
1917 devpriv
->stc_writew(dev
, AI_CONVERT_Pulse
,
1918 AI_Command_1_Register
);
1919 for (i
= 0; i
< NI_TIMEOUT
; i
++) {
1920 if (!(devpriv
->stc_readw(dev
,
1921 AI_Status_1_Register
) &
1925 if (i
== NI_TIMEOUT
) {
1927 ("ni_mio_common: timeout in ni_ai_insn_read\n");
1930 if (board
->reg_type
& ni_reg_m_series_mask
) {
1932 ni_readl(M_Offset_AI_FIFO_Data
) & mask
;
1934 d
= ni_readw(ADC_FIFO_Data_Register
);
1935 d
+= signbits
; /* subtle: needs to be short addition */
1943 static void ni_prime_channelgain_list(struct comedi_device
*dev
)
1945 struct ni_private
*devpriv
= dev
->private;
1948 devpriv
->stc_writew(dev
, AI_CONVERT_Pulse
, AI_Command_1_Register
);
1949 for (i
= 0; i
< NI_TIMEOUT
; ++i
) {
1950 if (!(devpriv
->stc_readw(dev
,
1951 AI_Status_1_Register
) &
1952 AI_FIFO_Empty_St
)) {
1953 devpriv
->stc_writew(dev
, 1, ADC_FIFO_Clear
);
1958 printk("ni_mio_common: timeout loading channel/gain list\n");
1961 static void ni_m_series_load_channelgain_list(struct comedi_device
*dev
,
1962 unsigned int n_chan
,
1965 const struct ni_board_struct
*board
= comedi_board(dev
);
1966 struct ni_private
*devpriv
= dev
->private;
1967 unsigned int chan
, range
, aref
;
1970 unsigned int dither
;
1971 unsigned range_code
;
1973 devpriv
->stc_writew(dev
, 1, Configuration_Memory_Clear
);
1975 /* offset = 1 << (board->adbits - 1); */
1976 if ((list
[0] & CR_ALT_SOURCE
)) {
1977 unsigned bypass_bits
;
1978 chan
= CR_CHAN(list
[0]);
1979 range
= CR_RANGE(list
[0]);
1980 range_code
= ni_gainlkup
[board
->gainlkup
][range
];
1981 dither
= ((list
[0] & CR_ALT_FILTER
) != 0);
1982 bypass_bits
= MSeries_AI_Bypass_Config_FIFO_Bit
;
1983 bypass_bits
|= chan
;
1985 (devpriv
->ai_calib_source
) &
1986 (MSeries_AI_Bypass_Cal_Sel_Pos_Mask
|
1987 MSeries_AI_Bypass_Cal_Sel_Neg_Mask
|
1988 MSeries_AI_Bypass_Mode_Mux_Mask
|
1989 MSeries_AO_Bypass_AO_Cal_Sel_Mask
);
1990 bypass_bits
|= MSeries_AI_Bypass_Gain_Bits(range_code
);
1992 bypass_bits
|= MSeries_AI_Bypass_Dither_Bit
;
1993 /* don't use 2's complement encoding */
1994 bypass_bits
|= MSeries_AI_Bypass_Polarity_Bit
;
1995 ni_writel(bypass_bits
, M_Offset_AI_Config_FIFO_Bypass
);
1997 ni_writel(0, M_Offset_AI_Config_FIFO_Bypass
);
2000 for (i
= 0; i
< n_chan
; i
++) {
2001 unsigned config_bits
= 0;
2002 chan
= CR_CHAN(list
[i
]);
2003 aref
= CR_AREF(list
[i
]);
2004 range
= CR_RANGE(list
[i
]);
2005 dither
= ((list
[i
] & CR_ALT_FILTER
) != 0);
2007 range_code
= ni_gainlkup
[board
->gainlkup
][range
];
2008 devpriv
->ai_offset
[i
] = offset
;
2012 MSeries_AI_Config_Channel_Type_Differential_Bits
;
2016 MSeries_AI_Config_Channel_Type_Common_Ref_Bits
;
2020 MSeries_AI_Config_Channel_Type_Ground_Ref_Bits
;
2025 config_bits
|= MSeries_AI_Config_Channel_Bits(chan
);
2027 MSeries_AI_Config_Bank_Bits(board
->reg_type
, chan
);
2028 config_bits
|= MSeries_AI_Config_Gain_Bits(range_code
);
2029 if (i
== n_chan
- 1)
2030 config_bits
|= MSeries_AI_Config_Last_Channel_Bit
;
2032 config_bits
|= MSeries_AI_Config_Dither_Bit
;
2033 /* don't use 2's complement encoding */
2034 config_bits
|= MSeries_AI_Config_Polarity_Bit
;
2035 ni_writew(config_bits
, M_Offset_AI_Config_FIFO_Data
);
2037 ni_prime_channelgain_list(dev
);
2041 * Notes on the 6110 and 6111:
2042 * These boards a slightly different than the rest of the series, since
2043 * they have multiple A/D converters.
2044 * From the driver side, the configuration memory is a
2046 * Configuration Memory Low:
2048 * bit 8: unipolar/bipolar (should be 0 for bipolar)
2049 * bits 0-3: gain. This is 4 bits instead of 3 for the other boards
2050 * 1001 gain=0.1 (+/- 50)
2059 * Configuration Memory High:
2060 * bits 12-14: Channel Type
2061 * 001 for differential
2062 * 000 for calibration
2063 * bit 11: coupling (this is not currently handled)
2067 * valid channels are 0-3
2069 static void ni_load_channelgain_list(struct comedi_device
*dev
,
2070 unsigned int n_chan
, unsigned int *list
)
2072 const struct ni_board_struct
*board
= comedi_board(dev
);
2073 struct ni_private
*devpriv
= dev
->private;
2074 unsigned int chan
, range
, aref
;
2076 unsigned int hi
, lo
;
2078 unsigned int dither
;
2080 if (board
->reg_type
& ni_reg_m_series_mask
) {
2081 ni_m_series_load_channelgain_list(dev
, n_chan
, list
);
2084 if (n_chan
== 1 && (board
->reg_type
!= ni_reg_611x
)
2085 && (board
->reg_type
!= ni_reg_6143
)) {
2086 if (devpriv
->changain_state
2087 && devpriv
->changain_spec
== list
[0]) {
2091 devpriv
->changain_state
= 1;
2092 devpriv
->changain_spec
= list
[0];
2094 devpriv
->changain_state
= 0;
2097 devpriv
->stc_writew(dev
, 1, Configuration_Memory_Clear
);
2099 /* Set up Calibration mode if required */
2100 if (board
->reg_type
== ni_reg_6143
) {
2101 if ((list
[0] & CR_ALT_SOURCE
)
2102 && !devpriv
->ai_calib_source_enabled
) {
2103 /* Strobe Relay enable bit */
2104 ni_writew(devpriv
->ai_calib_source
|
2105 Calibration_Channel_6143_RelayOn
,
2106 Calibration_Channel_6143
);
2107 ni_writew(devpriv
->ai_calib_source
,
2108 Calibration_Channel_6143
);
2109 devpriv
->ai_calib_source_enabled
= 1;
2110 msleep_interruptible(100); /* Allow relays to change */
2111 } else if (!(list
[0] & CR_ALT_SOURCE
)
2112 && devpriv
->ai_calib_source_enabled
) {
2113 /* Strobe Relay disable bit */
2114 ni_writew(devpriv
->ai_calib_source
|
2115 Calibration_Channel_6143_RelayOff
,
2116 Calibration_Channel_6143
);
2117 ni_writew(devpriv
->ai_calib_source
,
2118 Calibration_Channel_6143
);
2119 devpriv
->ai_calib_source_enabled
= 0;
2120 msleep_interruptible(100); /* Allow relays to change */
2124 offset
= 1 << (board
->adbits
- 1);
2125 for (i
= 0; i
< n_chan
; i
++) {
2126 if ((board
->reg_type
!= ni_reg_6143
)
2127 && (list
[i
] & CR_ALT_SOURCE
)) {
2128 chan
= devpriv
->ai_calib_source
;
2130 chan
= CR_CHAN(list
[i
]);
2132 aref
= CR_AREF(list
[i
]);
2133 range
= CR_RANGE(list
[i
]);
2134 dither
= ((list
[i
] & CR_ALT_FILTER
) != 0);
2136 /* fix the external/internal range differences */
2137 range
= ni_gainlkup
[board
->gainlkup
][range
];
2138 if (board
->reg_type
== ni_reg_611x
)
2139 devpriv
->ai_offset
[i
] = offset
;
2141 devpriv
->ai_offset
[i
] = (range
& 0x100) ? 0 : offset
;
2144 if ((list
[i
] & CR_ALT_SOURCE
)) {
2145 if (board
->reg_type
== ni_reg_611x
)
2146 ni_writew(CR_CHAN(list
[i
]) & 0x0003,
2147 Calibration_Channel_Select_611x
);
2149 if (board
->reg_type
== ni_reg_611x
)
2151 else if (board
->reg_type
== ni_reg_6143
)
2155 hi
|= AI_DIFFERENTIAL
;
2167 hi
|= AI_CONFIG_CHANNEL(chan
);
2169 ni_writew(hi
, Configuration_Memory_High
);
2171 if (board
->reg_type
!= ni_reg_6143
) {
2173 if (i
== n_chan
- 1)
2174 lo
|= AI_LAST_CHANNEL
;
2178 ni_writew(lo
, Configuration_Memory_Low
);
2182 /* prime the channel/gain list */
2183 if ((board
->reg_type
!= ni_reg_611x
)
2184 && (board
->reg_type
!= ni_reg_6143
)) {
2185 ni_prime_channelgain_list(dev
);
2189 static int ni_ns_to_timer(const struct comedi_device
*dev
, unsigned nanosec
,
2192 struct ni_private
*devpriv
= dev
->private;
2195 switch (round_mode
) {
2196 case TRIG_ROUND_NEAREST
:
2198 divider
= (nanosec
+ devpriv
->clock_ns
/ 2) / devpriv
->clock_ns
;
2200 case TRIG_ROUND_DOWN
:
2201 divider
= (nanosec
) / devpriv
->clock_ns
;
2204 divider
= (nanosec
+ devpriv
->clock_ns
- 1) / devpriv
->clock_ns
;
2210 static unsigned ni_timer_to_ns(const struct comedi_device
*dev
, int timer
)
2212 struct ni_private
*devpriv
= dev
->private;
2214 return devpriv
->clock_ns
* (timer
+ 1);
2217 static unsigned ni_min_ai_scan_period_ns(struct comedi_device
*dev
,
2218 unsigned num_channels
)
2220 const struct ni_board_struct
*board
= comedi_board(dev
);
2222 switch (board
->reg_type
) {
2225 /* simultaneously-sampled inputs */
2226 return board
->ai_speed
;
2229 /* multiplexed inputs */
2232 return board
->ai_speed
* num_channels
;
2235 static int ni_ai_cmdtest(struct comedi_device
*dev
, struct comedi_subdevice
*s
,
2236 struct comedi_cmd
*cmd
)
2238 const struct ni_board_struct
*board
= comedi_board(dev
);
2239 struct ni_private
*devpriv
= dev
->private;
2242 unsigned int sources
;
2244 /* Step 1 : check if triggers are trivially valid */
2246 if ((cmd
->flags
& CMDF_WRITE
))
2247 cmd
->flags
&= ~CMDF_WRITE
;
2249 err
|= cfc_check_trigger_src(&cmd
->start_src
,
2250 TRIG_NOW
| TRIG_INT
| TRIG_EXT
);
2251 err
|= cfc_check_trigger_src(&cmd
->scan_begin_src
,
2252 TRIG_TIMER
| TRIG_EXT
);
2254 sources
= TRIG_TIMER
| TRIG_EXT
;
2255 if (board
->reg_type
== ni_reg_611x
||
2256 board
->reg_type
== ni_reg_6143
)
2257 sources
|= TRIG_NOW
;
2258 err
|= cfc_check_trigger_src(&cmd
->convert_src
, sources
);
2260 err
|= cfc_check_trigger_src(&cmd
->scan_end_src
, TRIG_COUNT
);
2261 err
|= cfc_check_trigger_src(&cmd
->stop_src
, TRIG_COUNT
| TRIG_NONE
);
2266 /* Step 2a : make sure trigger sources are unique */
2268 err
|= cfc_check_trigger_is_unique(cmd
->start_src
);
2269 err
|= cfc_check_trigger_is_unique(cmd
->scan_begin_src
);
2270 err
|= cfc_check_trigger_is_unique(cmd
->convert_src
);
2271 err
|= cfc_check_trigger_is_unique(cmd
->stop_src
);
2273 /* Step 2b : and mutually compatible */
2278 /* Step 3: check if arguments are trivially valid */
2280 if (cmd
->start_src
== TRIG_EXT
) {
2281 /* external trigger */
2282 unsigned int tmp
= CR_CHAN(cmd
->start_arg
);
2286 tmp
|= (cmd
->start_arg
& (CR_INVERT
| CR_EDGE
));
2287 err
|= cfc_check_trigger_arg_is(&cmd
->start_arg
, tmp
);
2289 /* true for both TRIG_NOW and TRIG_INT */
2290 err
|= cfc_check_trigger_arg_is(&cmd
->start_arg
, 0);
2293 if (cmd
->scan_begin_src
== TRIG_TIMER
) {
2294 err
|= cfc_check_trigger_arg_min(&cmd
->scan_begin_arg
,
2295 ni_min_ai_scan_period_ns(dev
, cmd
->chanlist_len
));
2296 err
|= cfc_check_trigger_arg_max(&cmd
->scan_begin_arg
,
2297 devpriv
->clock_ns
* 0xffffff);
2298 } else if (cmd
->scan_begin_src
== TRIG_EXT
) {
2299 /* external trigger */
2300 unsigned int tmp
= CR_CHAN(cmd
->scan_begin_arg
);
2304 tmp
|= (cmd
->scan_begin_arg
& (CR_INVERT
| CR_EDGE
));
2305 err
|= cfc_check_trigger_arg_is(&cmd
->scan_begin_arg
, tmp
);
2306 } else { /* TRIG_OTHER */
2307 err
|= cfc_check_trigger_arg_is(&cmd
->scan_begin_arg
, 0);
2310 if (cmd
->convert_src
== TRIG_TIMER
) {
2311 if ((board
->reg_type
== ni_reg_611x
)
2312 || (board
->reg_type
== ni_reg_6143
)) {
2313 err
|= cfc_check_trigger_arg_is(&cmd
->convert_arg
, 0);
2315 err
|= cfc_check_trigger_arg_min(&cmd
->convert_arg
,
2317 err
|= cfc_check_trigger_arg_max(&cmd
->convert_arg
,
2318 devpriv
->clock_ns
* 0xffff);
2320 } else if (cmd
->convert_src
== TRIG_EXT
) {
2321 /* external trigger */
2322 unsigned int tmp
= CR_CHAN(cmd
->convert_arg
);
2326 tmp
|= (cmd
->convert_arg
& (CR_ALT_FILTER
| CR_INVERT
));
2327 err
|= cfc_check_trigger_arg_is(&cmd
->convert_arg
, tmp
);
2328 } else if (cmd
->convert_src
== TRIG_NOW
) {
2329 err
|= cfc_check_trigger_arg_is(&cmd
->convert_arg
, 0);
2332 err
|= cfc_check_trigger_arg_is(&cmd
->scan_end_arg
, cmd
->chanlist_len
);
2334 if (cmd
->stop_src
== TRIG_COUNT
) {
2335 unsigned int max_count
= 0x01000000;
2337 if (board
->reg_type
== ni_reg_611x
)
2338 max_count
-= num_adc_stages_611x
;
2339 err
|= cfc_check_trigger_arg_max(&cmd
->stop_arg
, max_count
);
2340 err
|= cfc_check_trigger_arg_min(&cmd
->stop_arg
, 1);
2343 err
|= cfc_check_trigger_arg_is(&cmd
->stop_arg
, 0);
2349 /* step 4: fix up any arguments */
2351 if (cmd
->scan_begin_src
== TRIG_TIMER
) {
2352 tmp
= cmd
->scan_begin_arg
;
2353 cmd
->scan_begin_arg
=
2354 ni_timer_to_ns(dev
, ni_ns_to_timer(dev
,
2355 cmd
->scan_begin_arg
,
2359 if (tmp
!= cmd
->scan_begin_arg
)
2362 if (cmd
->convert_src
== TRIG_TIMER
) {
2363 if ((board
->reg_type
!= ni_reg_611x
)
2364 && (board
->reg_type
!= ni_reg_6143
)) {
2365 tmp
= cmd
->convert_arg
;
2367 ni_timer_to_ns(dev
, ni_ns_to_timer(dev
,
2372 if (tmp
!= cmd
->convert_arg
)
2374 if (cmd
->scan_begin_src
== TRIG_TIMER
&&
2375 cmd
->scan_begin_arg
<
2376 cmd
->convert_arg
* cmd
->scan_end_arg
) {
2377 cmd
->scan_begin_arg
=
2378 cmd
->convert_arg
* cmd
->scan_end_arg
;
2390 static int ni_ai_cmd(struct comedi_device
*dev
, struct comedi_subdevice
*s
)
2392 const struct ni_board_struct
*board
= comedi_board(dev
);
2393 struct ni_private
*devpriv
= dev
->private;
2394 const struct comedi_cmd
*cmd
= &s
->async
->cmd
;
2396 int mode1
= 0; /* mode1 is needed for both stop and convert */
2398 int start_stop_select
= 0;
2399 unsigned int stop_count
;
2400 int interrupt_a_enable
= 0;
2402 MDPRINTK("ni_ai_cmd\n");
2403 if (dev
->irq
== 0) {
2404 comedi_error(dev
, "cannot run command without an irq");
2407 ni_clear_ai_fifo(dev
);
2409 ni_load_channelgain_list(dev
, cmd
->chanlist_len
, cmd
->chanlist
);
2411 /* start configuration */
2412 devpriv
->stc_writew(dev
, AI_Configuration_Start
, Joint_Reset_Register
);
2414 /* disable analog triggering for now, since it
2415 * interferes with the use of pfi0 */
2416 devpriv
->an_trig_etc_reg
&= ~Analog_Trigger_Enable
;
2417 devpriv
->stc_writew(dev
, devpriv
->an_trig_etc_reg
,
2418 Analog_Trigger_Etc_Register
);
2420 switch (cmd
->start_src
) {
2423 devpriv
->stc_writew(dev
, AI_START2_Select(0) |
2424 AI_START1_Sync
| AI_START1_Edge
|
2425 AI_START1_Select(0),
2426 AI_Trigger_Select_Register
);
2430 int chan
= CR_CHAN(cmd
->start_arg
);
2431 unsigned int bits
= AI_START2_Select(0) |
2432 AI_START1_Sync
| AI_START1_Select(chan
+ 1);
2434 if (cmd
->start_arg
& CR_INVERT
)
2435 bits
|= AI_START1_Polarity
;
2436 if (cmd
->start_arg
& CR_EDGE
)
2437 bits
|= AI_START1_Edge
;
2438 devpriv
->stc_writew(dev
, bits
,
2439 AI_Trigger_Select_Register
);
2444 mode2
&= ~AI_Pre_Trigger
;
2445 mode2
&= ~AI_SC_Initial_Load_Source
;
2446 mode2
&= ~AI_SC_Reload_Mode
;
2447 devpriv
->stc_writew(dev
, mode2
, AI_Mode_2_Register
);
2449 if (cmd
->chanlist_len
== 1 || (board
->reg_type
== ni_reg_611x
)
2450 || (board
->reg_type
== ni_reg_6143
)) {
2451 start_stop_select
|= AI_STOP_Polarity
;
2452 start_stop_select
|= AI_STOP_Select(31); /* logic low */
2453 start_stop_select
|= AI_STOP_Sync
;
2455 start_stop_select
|= AI_STOP_Select(19); /* ai configuration memory */
2457 devpriv
->stc_writew(dev
, start_stop_select
,
2458 AI_START_STOP_Select_Register
);
2460 devpriv
->ai_cmd2
= 0;
2461 switch (cmd
->stop_src
) {
2463 stop_count
= cmd
->stop_arg
- 1;
2465 if (board
->reg_type
== ni_reg_611x
) {
2466 /* have to take 3 stage adc pipeline into account */
2467 stop_count
+= num_adc_stages_611x
;
2469 /* stage number of scans */
2470 devpriv
->stc_writel(dev
, stop_count
, AI_SC_Load_A_Registers
);
2472 mode1
|= AI_Start_Stop
| AI_Mode_1_Reserved
| AI_Trigger_Once
;
2473 devpriv
->stc_writew(dev
, mode1
, AI_Mode_1_Register
);
2474 /* load SC (Scan Count) */
2475 devpriv
->stc_writew(dev
, AI_SC_Load
, AI_Command_1_Register
);
2477 devpriv
->ai_continuous
= 0;
2478 if (stop_count
== 0) {
2479 devpriv
->ai_cmd2
|= AI_End_On_End_Of_Scan
;
2480 interrupt_a_enable
|= AI_STOP_Interrupt_Enable
;
2481 /* this is required to get the last sample for chanlist_len > 1, not sure why */
2482 if (cmd
->chanlist_len
> 1)
2483 start_stop_select
|=
2484 AI_STOP_Polarity
| AI_STOP_Edge
;
2488 /* stage number of scans */
2489 devpriv
->stc_writel(dev
, 0, AI_SC_Load_A_Registers
);
2491 mode1
|= AI_Start_Stop
| AI_Mode_1_Reserved
| AI_Continuous
;
2492 devpriv
->stc_writew(dev
, mode1
, AI_Mode_1_Register
);
2494 /* load SC (Scan Count) */
2495 devpriv
->stc_writew(dev
, AI_SC_Load
, AI_Command_1_Register
);
2497 devpriv
->ai_continuous
= 1;
2502 switch (cmd
->scan_begin_src
) {
2505 stop bits for non 611x boards
2506 AI_SI_Special_Trigger_Delay=0
2508 AI_START_STOP_Select_Register:
2509 AI_START_Polarity=0 (?) rising edge
2510 AI_START_Edge=1 edge triggered
2512 AI_START_Select=0 SI_TC
2513 AI_STOP_Polarity=0 rising edge
2514 AI_STOP_Edge=0 level
2516 AI_STOP_Select=19 external pin (configuration mem)
2518 start_stop_select
|= AI_START_Edge
| AI_START_Sync
;
2519 devpriv
->stc_writew(dev
, start_stop_select
,
2520 AI_START_STOP_Select_Register
);
2522 mode2
|= AI_SI_Reload_Mode(0);
2523 /* AI_SI_Initial_Load_Source=A */
2524 mode2
&= ~AI_SI_Initial_Load_Source
;
2525 /* mode2 |= AI_SC_Reload_Mode; */
2526 devpriv
->stc_writew(dev
, mode2
, AI_Mode_2_Register
);
2529 timer
= ni_ns_to_timer(dev
, cmd
->scan_begin_arg
,
2530 TRIG_ROUND_NEAREST
);
2531 devpriv
->stc_writel(dev
, timer
, AI_SI_Load_A_Registers
);
2532 devpriv
->stc_writew(dev
, AI_SI_Load
, AI_Command_1_Register
);
2535 if (cmd
->scan_begin_arg
& CR_EDGE
)
2536 start_stop_select
|= AI_START_Edge
;
2537 /* AI_START_Polarity==1 is falling edge */
2538 if (cmd
->scan_begin_arg
& CR_INVERT
)
2539 start_stop_select
|= AI_START_Polarity
;
2540 if (cmd
->scan_begin_src
!= cmd
->convert_src
||
2541 (cmd
->scan_begin_arg
& ~CR_EDGE
) !=
2542 (cmd
->convert_arg
& ~CR_EDGE
))
2543 start_stop_select
|= AI_START_Sync
;
2544 start_stop_select
|=
2545 AI_START_Select(1 + CR_CHAN(cmd
->scan_begin_arg
));
2546 devpriv
->stc_writew(dev
, start_stop_select
,
2547 AI_START_STOP_Select_Register
);
2551 switch (cmd
->convert_src
) {
2554 if (cmd
->convert_arg
== 0 || cmd
->convert_src
== TRIG_NOW
)
2557 timer
= ni_ns_to_timer(dev
, cmd
->convert_arg
,
2558 TRIG_ROUND_NEAREST
);
2559 devpriv
->stc_writew(dev
, 1, AI_SI2_Load_A_Register
); /* 0,0 does not work. */
2560 devpriv
->stc_writew(dev
, timer
, AI_SI2_Load_B_Register
);
2562 /* AI_SI2_Reload_Mode = alternate */
2563 /* AI_SI2_Initial_Load_Source = A */
2564 mode2
&= ~AI_SI2_Initial_Load_Source
;
2565 mode2
|= AI_SI2_Reload_Mode
;
2566 devpriv
->stc_writew(dev
, mode2
, AI_Mode_2_Register
);
2569 devpriv
->stc_writew(dev
, AI_SI2_Load
, AI_Command_1_Register
);
2571 mode2
|= AI_SI2_Reload_Mode
; /* alternate */
2572 mode2
|= AI_SI2_Initial_Load_Source
; /* B */
2574 devpriv
->stc_writew(dev
, mode2
, AI_Mode_2_Register
);
2577 mode1
|= AI_CONVERT_Source_Select(1 + cmd
->convert_arg
);
2578 if ((cmd
->convert_arg
& CR_INVERT
) == 0)
2579 mode1
|= AI_CONVERT_Source_Polarity
;
2580 devpriv
->stc_writew(dev
, mode1
, AI_Mode_1_Register
);
2582 mode2
|= AI_Start_Stop_Gate_Enable
| AI_SC_Gate_Enable
;
2583 devpriv
->stc_writew(dev
, mode2
, AI_Mode_2_Register
);
2590 /* interrupt on FIFO, errors, SC_TC */
2591 interrupt_a_enable
|= AI_Error_Interrupt_Enable
|
2592 AI_SC_TC_Interrupt_Enable
;
2595 interrupt_a_enable
|= AI_FIFO_Interrupt_Enable
;
2598 if (cmd
->flags
& TRIG_WAKE_EOS
2599 || (devpriv
->ai_cmd2
& AI_End_On_End_Of_Scan
)) {
2600 /* wake on end-of-scan */
2601 devpriv
->aimode
= AIMODE_SCAN
;
2603 devpriv
->aimode
= AIMODE_HALF_FULL
;
2606 switch (devpriv
->aimode
) {
2607 case AIMODE_HALF_FULL
:
2608 /*generate FIFO interrupts and DMA requests on half-full */
2610 devpriv
->stc_writew(dev
, AI_FIFO_Mode_HF_to_E
,
2611 AI_Mode_3_Register
);
2613 devpriv
->stc_writew(dev
, AI_FIFO_Mode_HF
,
2614 AI_Mode_3_Register
);
2618 /*generate FIFO interrupts on non-empty */
2619 devpriv
->stc_writew(dev
, AI_FIFO_Mode_NE
,
2620 AI_Mode_3_Register
);
2624 devpriv
->stc_writew(dev
, AI_FIFO_Mode_NE
,
2625 AI_Mode_3_Register
);
2627 devpriv
->stc_writew(dev
, AI_FIFO_Mode_HF
,
2628 AI_Mode_3_Register
);
2630 interrupt_a_enable
|= AI_STOP_Interrupt_Enable
;
2636 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 */
2638 ni_set_bits(dev
, Interrupt_A_Enable_Register
,
2639 interrupt_a_enable
, 1);
2641 MDPRINTK("Interrupt_A_Enable_Register = 0x%04x\n",
2642 devpriv
->int_a_enable_reg
);
2644 /* interrupt on nothing */
2645 ni_set_bits(dev
, Interrupt_A_Enable_Register
, ~0, 0);
2647 /* XXX start polling if necessary */
2648 MDPRINTK("interrupting on nothing\n");
2651 /* end configuration */
2652 devpriv
->stc_writew(dev
, AI_Configuration_End
, Joint_Reset_Register
);
2654 switch (cmd
->scan_begin_src
) {
2656 devpriv
->stc_writew(dev
,
2657 AI_SI2_Arm
| AI_SI_Arm
| AI_DIV_Arm
|
2658 AI_SC_Arm
, AI_Command_1_Register
);
2661 /* XXX AI_SI_Arm? */
2662 devpriv
->stc_writew(dev
,
2663 AI_SI2_Arm
| AI_SI_Arm
| AI_DIV_Arm
|
2664 AI_SC_Arm
, AI_Command_1_Register
);
2670 int retval
= ni_ai_setup_MITE_dma(dev
);
2674 /* mite_dump_regs(devpriv->mite); */
2677 switch (cmd
->start_src
) {
2679 /* AI_START1_Pulse */
2680 devpriv
->stc_writew(dev
, AI_START1_Pulse
| devpriv
->ai_cmd2
,
2681 AI_Command_2_Register
);
2682 s
->async
->inttrig
= NULL
;
2685 s
->async
->inttrig
= NULL
;
2688 s
->async
->inttrig
= &ni_ai_inttrig
;
2692 MDPRINTK("exit ni_ai_cmd\n");
2697 static int ni_ai_inttrig(struct comedi_device
*dev
, struct comedi_subdevice
*s
,
2698 unsigned int trignum
)
2700 struct ni_private
*devpriv
= dev
->private;
2705 devpriv
->stc_writew(dev
, AI_START1_Pulse
| devpriv
->ai_cmd2
,
2706 AI_Command_2_Register
);
2707 s
->async
->inttrig
= NULL
;
2712 static int ni_ai_config_analog_trig(struct comedi_device
*dev
,
2713 struct comedi_subdevice
*s
,
2714 struct comedi_insn
*insn
,
2715 unsigned int *data
);
2717 static int ni_ai_insn_config(struct comedi_device
*dev
,
2718 struct comedi_subdevice
*s
,
2719 struct comedi_insn
*insn
, unsigned int *data
)
2721 const struct ni_board_struct
*board
= comedi_board(dev
);
2722 struct ni_private
*devpriv
= dev
->private;
2728 case INSN_CONFIG_ANALOG_TRIG
:
2729 return ni_ai_config_analog_trig(dev
, s
, insn
, data
);
2730 case INSN_CONFIG_ALT_SOURCE
:
2731 if (board
->reg_type
& ni_reg_m_series_mask
) {
2732 if (data
[1] & ~(MSeries_AI_Bypass_Cal_Sel_Pos_Mask
|
2733 MSeries_AI_Bypass_Cal_Sel_Neg_Mask
|
2734 MSeries_AI_Bypass_Mode_Mux_Mask
|
2735 MSeries_AO_Bypass_AO_Cal_Sel_Mask
)) {
2738 devpriv
->ai_calib_source
= data
[1];
2739 } else if (board
->reg_type
== ni_reg_6143
) {
2740 unsigned int calib_source
;
2742 calib_source
= data
[1] & 0xf;
2744 if (calib_source
> 0xF)
2747 devpriv
->ai_calib_source
= calib_source
;
2748 ni_writew(calib_source
, Calibration_Channel_6143
);
2750 unsigned int calib_source
;
2751 unsigned int calib_source_adjust
;
2753 calib_source
= data
[1] & 0xf;
2754 calib_source_adjust
= (data
[1] >> 4) & 0xff;
2756 if (calib_source
>= 8)
2758 devpriv
->ai_calib_source
= calib_source
;
2759 if (board
->reg_type
== ni_reg_611x
) {
2760 ni_writeb(calib_source_adjust
,
2761 Cal_Gain_Select_611x
);
2772 static int ni_ai_config_analog_trig(struct comedi_device
*dev
,
2773 struct comedi_subdevice
*s
,
2774 struct comedi_insn
*insn
,
2777 const struct ni_board_struct
*board
= comedi_board(dev
);
2778 struct ni_private
*devpriv
= dev
->private;
2779 unsigned int a
, b
, modebits
;
2783 * data[2] is analog line
2784 * data[3] is set level
2785 * data[4] is reset level */
2786 if (!board
->has_analog_trig
)
2788 if ((data
[1] & 0xffff0000) != COMEDI_EV_SCAN_BEGIN
) {
2789 data
[1] &= (COMEDI_EV_SCAN_BEGIN
| 0xffff);
2792 if (data
[2] >= board
->n_adchan
) {
2793 data
[2] = board
->n_adchan
- 1;
2796 if (data
[3] > 255) { /* a */
2800 if (data
[4] > 255) { /* b */
2811 * high mode 00 00 01 10
2812 * low mode 00 00 10 01
2814 * hysteresis low mode 10 00 00 01
2815 * hysteresis high mode 01 00 00 10
2816 * middle mode 10 01 01 10
2821 modebits
= data
[1] & 0xff;
2822 if (modebits
& 0xf0) {
2823 /* two level mode */
2829 ((data
[1] & 0xf) << 4) | ((data
[1] & 0xf0) >> 4);
2831 devpriv
->atrig_low
= a
;
2832 devpriv
->atrig_high
= b
;
2834 case 0x81: /* low hysteresis mode */
2835 devpriv
->atrig_mode
= 6;
2837 case 0x42: /* high hysteresis mode */
2838 devpriv
->atrig_mode
= 3;
2840 case 0x96: /* middle window mode */
2841 devpriv
->atrig_mode
= 2;
2848 /* one level mode */
2854 case 0x06: /* high window mode */
2855 devpriv
->atrig_high
= a
;
2856 devpriv
->atrig_mode
= 0;
2858 case 0x09: /* low window mode */
2859 devpriv
->atrig_low
= a
;
2860 devpriv
->atrig_mode
= 1;
2872 /* munge data from unsigned to 2's complement for analog output bipolar modes */
2873 static void ni_ao_munge(struct comedi_device
*dev
, struct comedi_subdevice
*s
,
2874 void *data
, unsigned int num_bytes
,
2875 unsigned int chan_index
)
2877 const struct ni_board_struct
*board
= comedi_board(dev
);
2878 struct comedi_async
*async
= s
->async
;
2881 unsigned int offset
;
2882 unsigned int length
= num_bytes
/ sizeof(short);
2883 short *array
= data
;
2885 offset
= 1 << (board
->aobits
- 1);
2886 for (i
= 0; i
< length
; i
++) {
2887 range
= CR_RANGE(async
->cmd
.chanlist
[chan_index
]);
2888 if (board
->ao_unipolar
== 0 || (range
& 1) == 0)
2891 array
[i
] = cpu_to_le16(array
[i
]);
2894 chan_index
%= async
->cmd
.chanlist_len
;
2898 static int ni_m_series_ao_config_chanlist(struct comedi_device
*dev
,
2899 struct comedi_subdevice
*s
,
2900 unsigned int chanspec
[],
2901 unsigned int n_chans
, int timed
)
2903 const struct ni_board_struct
*board
= comedi_board(dev
);
2904 struct ni_private
*devpriv
= dev
->private;
2912 for (i
= 0; i
< board
->n_aochan
; ++i
) {
2913 devpriv
->ao_conf
[i
] &= ~MSeries_AO_Update_Timed_Bit
;
2914 ni_writeb(devpriv
->ao_conf
[i
],
2915 M_Offset_AO_Config_Bank(i
));
2916 ni_writeb(0xf, M_Offset_AO_Waveform_Order(i
));
2919 for (i
= 0; i
< n_chans
; i
++) {
2920 const struct comedi_krange
*krange
;
2921 chan
= CR_CHAN(chanspec
[i
]);
2922 range
= CR_RANGE(chanspec
[i
]);
2923 krange
= s
->range_table
->range
+ range
;
2926 switch (krange
->max
- krange
->min
) {
2928 conf
|= MSeries_AO_DAC_Reference_10V_Internal_Bits
;
2929 ni_writeb(0, M_Offset_AO_Reference_Attenuation(chan
));
2932 conf
|= MSeries_AO_DAC_Reference_5V_Internal_Bits
;
2933 ni_writeb(0, M_Offset_AO_Reference_Attenuation(chan
));
2936 conf
|= MSeries_AO_DAC_Reference_10V_Internal_Bits
;
2937 ni_writeb(MSeries_Attenuate_x5_Bit
,
2938 M_Offset_AO_Reference_Attenuation(chan
));
2941 conf
|= MSeries_AO_DAC_Reference_5V_Internal_Bits
;
2942 ni_writeb(MSeries_Attenuate_x5_Bit
,
2943 M_Offset_AO_Reference_Attenuation(chan
));
2946 printk("%s: bug! unhandled ao reference voltage\n",
2950 switch (krange
->max
+ krange
->min
) {
2952 conf
|= MSeries_AO_DAC_Offset_0V_Bits
;
2955 conf
|= MSeries_AO_DAC_Offset_5V_Bits
;
2958 printk("%s: bug! unhandled ao offset voltage\n",
2963 conf
|= MSeries_AO_Update_Timed_Bit
;
2964 ni_writeb(conf
, M_Offset_AO_Config_Bank(chan
));
2965 devpriv
->ao_conf
[chan
] = conf
;
2966 ni_writeb(i
, M_Offset_AO_Waveform_Order(chan
));
2971 static int ni_old_ao_config_chanlist(struct comedi_device
*dev
,
2972 struct comedi_subdevice
*s
,
2973 unsigned int chanspec
[],
2974 unsigned int n_chans
)
2976 const struct ni_board_struct
*board
= comedi_board(dev
);
2977 struct ni_private
*devpriv
= dev
->private;
2984 for (i
= 0; i
< n_chans
; i
++) {
2985 chan
= CR_CHAN(chanspec
[i
]);
2986 range
= CR_RANGE(chanspec
[i
]);
2987 conf
= AO_Channel(chan
);
2989 if (board
->ao_unipolar
) {
2990 if ((range
& 1) == 0) {
2992 invert
= (1 << (board
->aobits
- 1));
3000 invert
= (1 << (board
->aobits
- 1));
3003 /* not all boards can deglitch, but this shouldn't hurt */
3004 if (chanspec
[i
] & CR_DEGLITCH
)
3005 conf
|= AO_Deglitch
;
3007 /* analog reference */
3008 /* AREF_OTHER connects AO ground to AI ground, i think */
3009 conf
|= (CR_AREF(chanspec
[i
]) ==
3010 AREF_OTHER
) ? AO_Ground_Ref
: 0;
3012 ni_writew(conf
, AO_Configuration
);
3013 devpriv
->ao_conf
[chan
] = conf
;
3018 static int ni_ao_config_chanlist(struct comedi_device
*dev
,
3019 struct comedi_subdevice
*s
,
3020 unsigned int chanspec
[], unsigned int n_chans
,
3023 const struct ni_board_struct
*board
= comedi_board(dev
);
3025 if (board
->reg_type
& ni_reg_m_series_mask
)
3026 return ni_m_series_ao_config_chanlist(dev
, s
, chanspec
, n_chans
,
3029 return ni_old_ao_config_chanlist(dev
, s
, chanspec
, n_chans
);
3032 static int ni_ao_insn_read(struct comedi_device
*dev
,
3033 struct comedi_subdevice
*s
, struct comedi_insn
*insn
,
3036 struct ni_private
*devpriv
= dev
->private;
3038 data
[0] = devpriv
->ao
[CR_CHAN(insn
->chanspec
)];
3043 static int ni_ao_insn_write(struct comedi_device
*dev
,
3044 struct comedi_subdevice
*s
,
3045 struct comedi_insn
*insn
, unsigned int *data
)
3047 const struct ni_board_struct
*board
= comedi_board(dev
);
3048 struct ni_private
*devpriv
= dev
->private;
3049 unsigned int chan
= CR_CHAN(insn
->chanspec
);
3050 unsigned int invert
;
3052 invert
= ni_ao_config_chanlist(dev
, s
, &insn
->chanspec
, 1, 0);
3054 devpriv
->ao
[chan
] = data
[0];
3056 if (board
->reg_type
& ni_reg_m_series_mask
) {
3057 ni_writew(data
[0], M_Offset_DAC_Direct_Data(chan
));
3059 ni_writew(data
[0] ^ invert
,
3060 (chan
) ? DAC1_Direct_Data
: DAC0_Direct_Data
);
3065 static int ni_ao_insn_write_671x(struct comedi_device
*dev
,
3066 struct comedi_subdevice
*s
,
3067 struct comedi_insn
*insn
, unsigned int *data
)
3069 const struct ni_board_struct
*board
= comedi_board(dev
);
3070 struct ni_private
*devpriv
= dev
->private;
3071 unsigned int chan
= CR_CHAN(insn
->chanspec
);
3072 unsigned int invert
;
3074 ao_win_out(1 << chan
, AO_Immediate_671x
);
3075 invert
= 1 << (board
->aobits
- 1);
3077 ni_ao_config_chanlist(dev
, s
, &insn
->chanspec
, 1, 0);
3079 devpriv
->ao
[chan
] = data
[0];
3080 ao_win_out(data
[0] ^ invert
, DACx_Direct_Data_671x(chan
));
3085 static int ni_ao_insn_config(struct comedi_device
*dev
,
3086 struct comedi_subdevice
*s
,
3087 struct comedi_insn
*insn
, unsigned int *data
)
3089 const struct ni_board_struct
*board
= comedi_board(dev
);
3090 struct ni_private
*devpriv
= dev
->private;
3093 case INSN_CONFIG_GET_HARDWARE_BUFFER_SIZE
:
3096 data
[2] = 1 + board
->ao_fifo_depth
* sizeof(short);
3098 data
[2] += devpriv
->mite
->fifo_size
;
3115 static int ni_ao_inttrig(struct comedi_device
*dev
, struct comedi_subdevice
*s
,
3116 unsigned int trignum
)
3118 const struct ni_board_struct
*board __maybe_unused
= comedi_board(dev
);
3119 struct ni_private
*devpriv
= dev
->private;
3121 int interrupt_b_bits
;
3123 static const int timeout
= 1000;
3128 /* Null trig at beginning prevent ao start trigger from executing more than
3129 once per command (and doing things like trying to allocate the ao dma channel
3131 s
->async
->inttrig
= NULL
;
3133 ni_set_bits(dev
, Interrupt_B_Enable_Register
,
3134 AO_FIFO_Interrupt_Enable
| AO_Error_Interrupt_Enable
, 0);
3135 interrupt_b_bits
= AO_Error_Interrupt_Enable
;
3137 devpriv
->stc_writew(dev
, 1, DAC_FIFO_Clear
);
3138 if (board
->reg_type
& ni_reg_6xxx_mask
)
3139 ni_ao_win_outl(dev
, 0x6, AO_FIFO_Offset_Load_611x
);
3140 ret
= ni_ao_setup_MITE_dma(dev
);
3143 ret
= ni_ao_wait_for_dma_load(dev
);
3147 ret
= ni_ao_prep_fifo(dev
, s
);
3151 interrupt_b_bits
|= AO_FIFO_Interrupt_Enable
;
3154 devpriv
->stc_writew(dev
, devpriv
->ao_mode3
| AO_Not_An_UPDATE
,
3155 AO_Mode_3_Register
);
3156 devpriv
->stc_writew(dev
, devpriv
->ao_mode3
, AO_Mode_3_Register
);
3157 /* wait for DACs to be loaded */
3158 for (i
= 0; i
< timeout
; i
++) {
3160 if ((devpriv
->stc_readw(dev
,
3161 Joint_Status_2_Register
) &
3162 AO_TMRDACWRs_In_Progress_St
) == 0)
3167 "timed out waiting for AO_TMRDACWRs_In_Progress_St to clear");
3170 /* stc manual says we are need to clear error interrupt after AO_TMRDACWRs_In_Progress_St clears */
3171 devpriv
->stc_writew(dev
, AO_Error_Interrupt_Ack
,
3172 Interrupt_B_Ack_Register
);
3174 ni_set_bits(dev
, Interrupt_B_Enable_Register
, interrupt_b_bits
, 1);
3176 devpriv
->stc_writew(dev
,
3177 devpriv
->ao_cmd1
| AO_UI_Arm
| AO_UC_Arm
| AO_BC_Arm
3178 | AO_DAC1_Update_Mode
| AO_DAC0_Update_Mode
,
3179 AO_Command_1_Register
);
3181 devpriv
->stc_writew(dev
, devpriv
->ao_cmd2
| AO_START1_Pulse
,
3182 AO_Command_2_Register
);
3187 static int ni_ao_cmd(struct comedi_device
*dev
, struct comedi_subdevice
*s
)
3189 const struct ni_board_struct
*board
= comedi_board(dev
);
3190 struct ni_private
*devpriv
= dev
->private;
3191 const struct comedi_cmd
*cmd
= &s
->async
->cmd
;
3196 if (dev
->irq
== 0) {
3197 comedi_error(dev
, "cannot run command without an irq");
3201 devpriv
->stc_writew(dev
, AO_Configuration_Start
, Joint_Reset_Register
);
3203 devpriv
->stc_writew(dev
, AO_Disarm
, AO_Command_1_Register
);
3205 if (board
->reg_type
& ni_reg_6xxx_mask
) {
3206 ao_win_out(CLEAR_WG
, AO_Misc_611x
);
3209 for (i
= 0; i
< cmd
->chanlist_len
; i
++) {
3212 chan
= CR_CHAN(cmd
->chanlist
[i
]);
3214 ao_win_out(chan
, AO_Waveform_Generation_611x
);
3216 ao_win_out(bits
, AO_Timed_611x
);
3219 ni_ao_config_chanlist(dev
, s
, cmd
->chanlist
, cmd
->chanlist_len
, 1);
3221 if (cmd
->stop_src
== TRIG_NONE
) {
3222 devpriv
->ao_mode1
|= AO_Continuous
;
3223 devpriv
->ao_mode1
&= ~AO_Trigger_Once
;
3225 devpriv
->ao_mode1
&= ~AO_Continuous
;
3226 devpriv
->ao_mode1
|= AO_Trigger_Once
;
3228 devpriv
->stc_writew(dev
, devpriv
->ao_mode1
, AO_Mode_1_Register
);
3229 switch (cmd
->start_src
) {
3232 devpriv
->ao_trigger_select
&=
3233 ~(AO_START1_Polarity
| AO_START1_Select(-1));
3234 devpriv
->ao_trigger_select
|= AO_START1_Edge
| AO_START1_Sync
;
3235 devpriv
->stc_writew(dev
, devpriv
->ao_trigger_select
,
3236 AO_Trigger_Select_Register
);
3239 devpriv
->ao_trigger_select
=
3240 AO_START1_Select(CR_CHAN(cmd
->start_arg
) + 1);
3241 if (cmd
->start_arg
& CR_INVERT
)
3242 devpriv
->ao_trigger_select
|= AO_START1_Polarity
; /* 0=active high, 1=active low. see daq-stc 3-24 (p186) */
3243 if (cmd
->start_arg
& CR_EDGE
)
3244 devpriv
->ao_trigger_select
|= AO_START1_Edge
; /* 0=edge detection disabled, 1=enabled */
3245 devpriv
->stc_writew(dev
, devpriv
->ao_trigger_select
,
3246 AO_Trigger_Select_Register
);
3252 devpriv
->ao_mode3
&= ~AO_Trigger_Length
;
3253 devpriv
->stc_writew(dev
, devpriv
->ao_mode3
, AO_Mode_3_Register
);
3255 devpriv
->stc_writew(dev
, devpriv
->ao_mode1
, AO_Mode_1_Register
);
3256 devpriv
->ao_mode2
&= ~AO_BC_Initial_Load_Source
;
3257 devpriv
->stc_writew(dev
, devpriv
->ao_mode2
, AO_Mode_2_Register
);
3258 if (cmd
->stop_src
== TRIG_NONE
) {
3259 devpriv
->stc_writel(dev
, 0xffffff, AO_BC_Load_A_Register
);
3261 devpriv
->stc_writel(dev
, 0, AO_BC_Load_A_Register
);
3263 devpriv
->stc_writew(dev
, AO_BC_Load
, AO_Command_1_Register
);
3264 devpriv
->ao_mode2
&= ~AO_UC_Initial_Load_Source
;
3265 devpriv
->stc_writew(dev
, devpriv
->ao_mode2
, AO_Mode_2_Register
);
3266 switch (cmd
->stop_src
) {
3268 if (board
->reg_type
& ni_reg_m_series_mask
) {
3269 /* this is how the NI example code does it for m-series boards, verified correct with 6259 */
3270 devpriv
->stc_writel(dev
, cmd
->stop_arg
- 1,
3271 AO_UC_Load_A_Register
);
3272 devpriv
->stc_writew(dev
, AO_UC_Load
,
3273 AO_Command_1_Register
);
3275 devpriv
->stc_writel(dev
, cmd
->stop_arg
,
3276 AO_UC_Load_A_Register
);
3277 devpriv
->stc_writew(dev
, AO_UC_Load
,
3278 AO_Command_1_Register
);
3279 devpriv
->stc_writel(dev
, cmd
->stop_arg
- 1,
3280 AO_UC_Load_A_Register
);
3284 devpriv
->stc_writel(dev
, 0xffffff, AO_UC_Load_A_Register
);
3285 devpriv
->stc_writew(dev
, AO_UC_Load
, AO_Command_1_Register
);
3286 devpriv
->stc_writel(dev
, 0xffffff, AO_UC_Load_A_Register
);
3289 devpriv
->stc_writel(dev
, 0, AO_UC_Load_A_Register
);
3290 devpriv
->stc_writew(dev
, AO_UC_Load
, AO_Command_1_Register
);
3291 devpriv
->stc_writel(dev
, cmd
->stop_arg
, AO_UC_Load_A_Register
);
3294 devpriv
->ao_mode1
&=
3295 ~(AO_UI_Source_Select(0x1f) | AO_UI_Source_Polarity
|
3296 AO_UPDATE_Source_Select(0x1f) | AO_UPDATE_Source_Polarity
);
3297 switch (cmd
->scan_begin_src
) {
3299 devpriv
->ao_cmd2
&= ~AO_BC_Gate_Enable
;
3301 ni_ns_to_timer(dev
, cmd
->scan_begin_arg
,
3302 TRIG_ROUND_NEAREST
);
3303 devpriv
->stc_writel(dev
, 1, AO_UI_Load_A_Register
);
3304 devpriv
->stc_writew(dev
, AO_UI_Load
, AO_Command_1_Register
);
3305 devpriv
->stc_writel(dev
, trigvar
, AO_UI_Load_A_Register
);
3308 devpriv
->ao_mode1
|=
3309 AO_UPDATE_Source_Select(cmd
->scan_begin_arg
);
3310 if (cmd
->scan_begin_arg
& CR_INVERT
)
3311 devpriv
->ao_mode1
|= AO_UPDATE_Source_Polarity
;
3312 devpriv
->ao_cmd2
|= AO_BC_Gate_Enable
;
3318 devpriv
->stc_writew(dev
, devpriv
->ao_cmd2
, AO_Command_2_Register
);
3319 devpriv
->stc_writew(dev
, devpriv
->ao_mode1
, AO_Mode_1_Register
);
3320 devpriv
->ao_mode2
&=
3321 ~(AO_UI_Reload_Mode(3) | AO_UI_Initial_Load_Source
);
3322 devpriv
->stc_writew(dev
, devpriv
->ao_mode2
, AO_Mode_2_Register
);
3324 if (cmd
->scan_end_arg
> 1) {
3325 devpriv
->ao_mode1
|= AO_Multiple_Channels
;
3326 devpriv
->stc_writew(dev
,
3327 AO_Number_Of_Channels(cmd
->scan_end_arg
-
3329 AO_UPDATE_Output_Select
3330 (AO_Update_Output_High_Z
),
3331 AO_Output_Control_Register
);
3334 devpriv
->ao_mode1
&= ~AO_Multiple_Channels
;
3335 bits
= AO_UPDATE_Output_Select(AO_Update_Output_High_Z
);
3336 if (board
->reg_type
&
3337 (ni_reg_m_series_mask
| ni_reg_6xxx_mask
)) {
3338 bits
|= AO_Number_Of_Channels(0);
3341 AO_Number_Of_Channels(CR_CHAN(cmd
->chanlist
[0]));
3343 devpriv
->stc_writew(dev
, bits
, AO_Output_Control_Register
);
3345 devpriv
->stc_writew(dev
, devpriv
->ao_mode1
, AO_Mode_1_Register
);
3347 devpriv
->stc_writew(dev
, AO_DAC0_Update_Mode
| AO_DAC1_Update_Mode
,
3348 AO_Command_1_Register
);
3350 devpriv
->ao_mode3
|= AO_Stop_On_Overrun_Error
;
3351 devpriv
->stc_writew(dev
, devpriv
->ao_mode3
, AO_Mode_3_Register
);
3353 devpriv
->ao_mode2
&= ~AO_FIFO_Mode_Mask
;
3355 devpriv
->ao_mode2
|= AO_FIFO_Mode_HF_to_F
;
3357 devpriv
->ao_mode2
|= AO_FIFO_Mode_HF
;
3359 devpriv
->ao_mode2
&= ~AO_FIFO_Retransmit_Enable
;
3360 devpriv
->stc_writew(dev
, devpriv
->ao_mode2
, AO_Mode_2_Register
);
3362 bits
= AO_BC_Source_Select
| AO_UPDATE_Pulse_Width
|
3363 AO_TMRDACWR_Pulse_Width
;
3364 if (board
->ao_fifo_depth
)
3365 bits
|= AO_FIFO_Enable
;
3367 bits
|= AO_DMA_PIO_Control
;
3369 /* F Hess: windows driver does not set AO_Number_Of_DAC_Packages bit for 6281,
3370 verified with bus analyzer. */
3371 if (board
->reg_type
& ni_reg_m_series_mask
)
3372 bits
|= AO_Number_Of_DAC_Packages
;
3374 devpriv
->stc_writew(dev
, bits
, AO_Personal_Register
);
3375 /* enable sending of ao dma requests */
3376 devpriv
->stc_writew(dev
, AO_AOFREQ_Enable
, AO_Start_Select_Register
);
3378 devpriv
->stc_writew(dev
, AO_Configuration_End
, Joint_Reset_Register
);
3380 if (cmd
->stop_src
== TRIG_COUNT
) {
3381 devpriv
->stc_writew(dev
, AO_BC_TC_Interrupt_Ack
,
3382 Interrupt_B_Ack_Register
);
3383 ni_set_bits(dev
, Interrupt_B_Enable_Register
,
3384 AO_BC_TC_Interrupt_Enable
, 1);
3387 s
->async
->inttrig
= &ni_ao_inttrig
;
3392 static int ni_ao_cmdtest(struct comedi_device
*dev
, struct comedi_subdevice
*s
,
3393 struct comedi_cmd
*cmd
)
3395 const struct ni_board_struct
*board
= comedi_board(dev
);
3396 struct ni_private
*devpriv
= dev
->private;
3400 /* Step 1 : check if triggers are trivially valid */
3402 if ((cmd
->flags
& CMDF_WRITE
) == 0)
3403 cmd
->flags
|= CMDF_WRITE
;
3405 err
|= cfc_check_trigger_src(&cmd
->start_src
, TRIG_INT
| TRIG_EXT
);
3406 err
|= cfc_check_trigger_src(&cmd
->scan_begin_src
,
3407 TRIG_TIMER
| TRIG_EXT
);
3408 err
|= cfc_check_trigger_src(&cmd
->convert_src
, TRIG_NOW
);
3409 err
|= cfc_check_trigger_src(&cmd
->scan_end_src
, TRIG_COUNT
);
3410 err
|= cfc_check_trigger_src(&cmd
->stop_src
, TRIG_COUNT
| TRIG_NONE
);
3415 /* Step 2a : make sure trigger sources are unique */
3417 err
|= cfc_check_trigger_is_unique(cmd
->start_src
);
3418 err
|= cfc_check_trigger_is_unique(cmd
->scan_begin_src
);
3419 err
|= cfc_check_trigger_is_unique(cmd
->stop_src
);
3421 /* Step 2b : and mutually compatible */
3426 /* Step 3: check if arguments are trivially valid */
3428 if (cmd
->start_src
== TRIG_EXT
) {
3429 /* external trigger */
3430 unsigned int tmp
= CR_CHAN(cmd
->start_arg
);
3434 tmp
|= (cmd
->start_arg
& (CR_INVERT
| CR_EDGE
));
3435 err
|= cfc_check_trigger_arg_is(&cmd
->start_arg
, tmp
);
3437 /* true for both TRIG_NOW and TRIG_INT */
3438 err
|= cfc_check_trigger_arg_is(&cmd
->start_arg
, 0);
3441 if (cmd
->scan_begin_src
== TRIG_TIMER
) {
3442 err
|= cfc_check_trigger_arg_min(&cmd
->scan_begin_arg
,
3444 err
|= cfc_check_trigger_arg_max(&cmd
->scan_begin_arg
,
3445 devpriv
->clock_ns
* 0xffffff);
3448 err
|= cfc_check_trigger_arg_is(&cmd
->convert_arg
, 0);
3449 err
|= cfc_check_trigger_arg_is(&cmd
->scan_end_arg
, cmd
->chanlist_len
);
3451 if (cmd
->stop_src
== TRIG_COUNT
)
3452 err
|= cfc_check_trigger_arg_max(&cmd
->stop_arg
, 0x00ffffff);
3453 else /* TRIG_NONE */
3454 err
|= cfc_check_trigger_arg_is(&cmd
->stop_arg
, 0);
3459 /* step 4: fix up any arguments */
3460 if (cmd
->scan_begin_src
== TRIG_TIMER
) {
3461 tmp
= cmd
->scan_begin_arg
;
3462 cmd
->scan_begin_arg
=
3463 ni_timer_to_ns(dev
, ni_ns_to_timer(dev
,
3464 cmd
->scan_begin_arg
,
3468 if (tmp
!= cmd
->scan_begin_arg
)
3474 /* step 5: fix up chanlist */
3482 static int ni_ao_reset(struct comedi_device
*dev
, struct comedi_subdevice
*s
)
3484 const struct ni_board_struct
*board
= comedi_board(dev
);
3485 struct ni_private
*devpriv
= dev
->private;
3487 /* devpriv->ao0p=0x0000; */
3488 /* ni_writew(devpriv->ao0p,AO_Configuration); */
3490 /* devpriv->ao1p=AO_Channel(1); */
3491 /* ni_writew(devpriv->ao1p,AO_Configuration); */
3493 ni_release_ao_mite_channel(dev
);
3495 devpriv
->stc_writew(dev
, AO_Configuration_Start
, Joint_Reset_Register
);
3496 devpriv
->stc_writew(dev
, AO_Disarm
, AO_Command_1_Register
);
3497 ni_set_bits(dev
, Interrupt_B_Enable_Register
, ~0, 0);
3498 devpriv
->stc_writew(dev
, AO_BC_Source_Select
, AO_Personal_Register
);
3499 devpriv
->stc_writew(dev
, 0x3f98, Interrupt_B_Ack_Register
);
3500 devpriv
->stc_writew(dev
, AO_BC_Source_Select
| AO_UPDATE_Pulse_Width
|
3501 AO_TMRDACWR_Pulse_Width
, AO_Personal_Register
);
3502 devpriv
->stc_writew(dev
, 0, AO_Output_Control_Register
);
3503 devpriv
->stc_writew(dev
, 0, AO_Start_Select_Register
);
3504 devpriv
->ao_cmd1
= 0;
3505 devpriv
->stc_writew(dev
, devpriv
->ao_cmd1
, AO_Command_1_Register
);
3506 devpriv
->ao_cmd2
= 0;
3507 devpriv
->stc_writew(dev
, devpriv
->ao_cmd2
, AO_Command_2_Register
);
3508 devpriv
->ao_mode1
= 0;
3509 devpriv
->stc_writew(dev
, devpriv
->ao_mode1
, AO_Mode_1_Register
);
3510 devpriv
->ao_mode2
= 0;
3511 devpriv
->stc_writew(dev
, devpriv
->ao_mode2
, AO_Mode_2_Register
);
3512 if (board
->reg_type
& ni_reg_m_series_mask
)
3513 devpriv
->ao_mode3
= AO_Last_Gate_Disable
;
3515 devpriv
->ao_mode3
= 0;
3516 devpriv
->stc_writew(dev
, devpriv
->ao_mode3
, AO_Mode_3_Register
);
3517 devpriv
->ao_trigger_select
= 0;
3518 devpriv
->stc_writew(dev
, devpriv
->ao_trigger_select
,
3519 AO_Trigger_Select_Register
);
3520 if (board
->reg_type
& ni_reg_6xxx_mask
) {
3521 unsigned immediate_bits
= 0;
3523 for (i
= 0; i
< s
->n_chan
; ++i
) {
3524 immediate_bits
|= 1 << i
;
3526 ao_win_out(immediate_bits
, AO_Immediate_671x
);
3527 ao_win_out(CLEAR_WG
, AO_Misc_611x
);
3529 devpriv
->stc_writew(dev
, AO_Configuration_End
, Joint_Reset_Register
);
3536 static int ni_dio_insn_config(struct comedi_device
*dev
,
3537 struct comedi_subdevice
*s
,
3538 struct comedi_insn
*insn
, unsigned int *data
)
3540 struct ni_private
*devpriv
= dev
->private;
3543 printk("ni_dio_insn_config() chan=%d io=%d\n",
3544 CR_CHAN(insn
->chanspec
), data
[0]);
3547 case INSN_CONFIG_DIO_OUTPUT
:
3548 s
->io_bits
|= 1 << CR_CHAN(insn
->chanspec
);
3550 case INSN_CONFIG_DIO_INPUT
:
3551 s
->io_bits
&= ~(1 << CR_CHAN(insn
->chanspec
));
3553 case INSN_CONFIG_DIO_QUERY
:
3556 io_bits
& (1 << CR_CHAN(insn
->chanspec
))) ? COMEDI_OUTPUT
:
3564 devpriv
->dio_control
&= ~DIO_Pins_Dir_Mask
;
3565 devpriv
->dio_control
|= DIO_Pins_Dir(s
->io_bits
);
3566 devpriv
->stc_writew(dev
, devpriv
->dio_control
, DIO_Control_Register
);
3571 static int ni_dio_insn_bits(struct comedi_device
*dev
,
3572 struct comedi_subdevice
*s
,
3573 struct comedi_insn
*insn
, unsigned int *data
)
3575 struct ni_private
*devpriv
= dev
->private;
3578 printk("ni_dio_insn_bits() mask=0x%x bits=0x%x\n", data
[0], data
[1]);
3582 /* Perform check to make sure we're not using the
3583 serial part of the dio */
3584 if ((data
[0] & (DIO_SDIN
| DIO_SDOUT
))
3585 && devpriv
->serial_interval_ns
)
3588 s
->state
&= ~data
[0];
3589 s
->state
|= (data
[0] & data
[1]);
3590 devpriv
->dio_output
&= ~DIO_Parallel_Data_Mask
;
3591 devpriv
->dio_output
|= DIO_Parallel_Data_Out(s
->state
);
3592 devpriv
->stc_writew(dev
, devpriv
->dio_output
,
3593 DIO_Output_Register
);
3595 data
[1] = devpriv
->stc_readw(dev
, DIO_Parallel_Input_Register
);
3600 static int ni_m_series_dio_insn_config(struct comedi_device
*dev
,
3601 struct comedi_subdevice
*s
,
3602 struct comedi_insn
*insn
,
3605 struct ni_private
*devpriv __maybe_unused
= dev
->private;
3608 printk("ni_m_series_dio_insn_config() chan=%d io=%d\n",
3609 CR_CHAN(insn
->chanspec
), data
[0]);
3612 case INSN_CONFIG_DIO_OUTPUT
:
3613 s
->io_bits
|= 1 << CR_CHAN(insn
->chanspec
);
3615 case INSN_CONFIG_DIO_INPUT
:
3616 s
->io_bits
&= ~(1 << CR_CHAN(insn
->chanspec
));
3618 case INSN_CONFIG_DIO_QUERY
:
3621 io_bits
& (1 << CR_CHAN(insn
->chanspec
))) ? COMEDI_OUTPUT
:
3629 ni_writel(s
->io_bits
, M_Offset_DIO_Direction
);
3634 static int ni_m_series_dio_insn_bits(struct comedi_device
*dev
,
3635 struct comedi_subdevice
*s
,
3636 struct comedi_insn
*insn
,
3639 struct ni_private
*devpriv __maybe_unused
= dev
->private;
3642 printk("ni_m_series_dio_insn_bits() mask=0x%x bits=0x%x\n", data
[0],
3647 s
->state
&= ~data
[0];
3648 s
->state
|= (data
[0] & data
[1]);
3649 ni_writel(s
->state
, M_Offset_Static_Digital_Output
);
3651 data
[1] = ni_readl(M_Offset_Static_Digital_Input
);
3656 static int ni_cdio_cmdtest(struct comedi_device
*dev
,
3657 struct comedi_subdevice
*s
, struct comedi_cmd
*cmd
)
3663 /* Step 1 : check if triggers are trivially valid */
3665 err
|= cfc_check_trigger_src(&cmd
->start_src
, TRIG_INT
);
3666 err
|= cfc_check_trigger_src(&cmd
->scan_begin_src
, TRIG_EXT
);
3667 err
|= cfc_check_trigger_src(&cmd
->convert_src
, TRIG_NOW
);
3668 err
|= cfc_check_trigger_src(&cmd
->scan_end_src
, TRIG_COUNT
);
3669 err
|= cfc_check_trigger_src(&cmd
->stop_src
, TRIG_NONE
);
3674 /* Step 2a : make sure trigger sources are unique */
3675 /* Step 2b : and mutually compatible */
3680 /* Step 3: check if arguments are trivially valid */
3682 err
|= cfc_check_trigger_arg_is(&cmd
->start_arg
, 0);
3684 tmp
= cmd
->scan_begin_arg
;
3685 tmp
&= CR_PACK_FLAGS(CDO_Sample_Source_Select_Mask
, 0, 0, CR_INVERT
);
3686 if (tmp
!= cmd
->scan_begin_arg
)
3689 err
|= cfc_check_trigger_arg_is(&cmd
->convert_arg
, 0);
3690 err
|= cfc_check_trigger_arg_is(&cmd
->scan_end_arg
, cmd
->chanlist_len
);
3691 err
|= cfc_check_trigger_arg_is(&cmd
->stop_arg
, 0);
3696 /* step 4: fix up any arguments */
3701 /* step 5: check chanlist */
3703 for (i
= 0; i
< cmd
->chanlist_len
; ++i
) {
3704 if (cmd
->chanlist
[i
] != i
)
3714 static int ni_cdio_cmd(struct comedi_device
*dev
, struct comedi_subdevice
*s
)
3716 struct ni_private
*devpriv __maybe_unused
= dev
->private;
3717 const struct comedi_cmd
*cmd
= &s
->async
->cmd
;
3718 unsigned cdo_mode_bits
= CDO_FIFO_Mode_Bit
| CDO_Halt_On_Error_Bit
;
3721 ni_writel(CDO_Reset_Bit
, M_Offset_CDIO_Command
);
3722 switch (cmd
->scan_begin_src
) {
3725 CR_CHAN(cmd
->scan_begin_arg
) &
3726 CDO_Sample_Source_Select_Mask
;
3732 if (cmd
->scan_begin_arg
& CR_INVERT
)
3733 cdo_mode_bits
|= CDO_Polarity_Bit
;
3734 ni_writel(cdo_mode_bits
, M_Offset_CDO_Mode
);
3736 ni_writel(s
->state
, M_Offset_CDO_FIFO_Data
);
3737 ni_writel(CDO_SW_Update_Bit
, M_Offset_CDIO_Command
);
3738 ni_writel(s
->io_bits
, M_Offset_CDO_Mask_Enable
);
3741 "attempted to run digital output command with no lines configured as outputs");
3744 retval
= ni_request_cdo_mite_channel(dev
);
3748 s
->async
->inttrig
= &ni_cdo_inttrig
;
3752 static int ni_cdo_inttrig(struct comedi_device
*dev
, struct comedi_subdevice
*s
,
3753 unsigned int trignum
)
3756 struct ni_private
*devpriv
= dev
->private;
3757 unsigned long flags
;
3761 const unsigned timeout
= 1000;
3763 s
->async
->inttrig
= NULL
;
3765 /* read alloc the entire buffer */
3766 comedi_buf_read_alloc(s
->async
, s
->async
->prealloc_bufsz
);
3769 spin_lock_irqsave(&devpriv
->mite_channel_lock
, flags
);
3770 if (devpriv
->cdo_mite_chan
) {
3771 mite_prep_dma(devpriv
->cdo_mite_chan
, 32, 32);
3772 mite_dma_arm(devpriv
->cdo_mite_chan
);
3774 comedi_error(dev
, "BUG: no cdo mite channel?");
3777 spin_unlock_irqrestore(&devpriv
->mite_channel_lock
, flags
);
3782 * XXX not sure what interrupt C group does
3783 * ni_writeb(Interrupt_Group_C_Enable_Bit,
3784 * M_Offset_Interrupt_C_Enable); wait for dma to fill output fifo
3786 for (i
= 0; i
< timeout
; ++i
) {
3787 if (ni_readl(M_Offset_CDIO_Status
) & CDO_FIFO_Full_Bit
)
3792 comedi_error(dev
, "dma failed to fill cdo fifo!");
3793 ni_cdio_cancel(dev
, s
);
3796 ni_writel(CDO_Arm_Bit
| CDO_Error_Interrupt_Enable_Set_Bit
|
3797 CDO_Empty_FIFO_Interrupt_Enable_Set_Bit
,
3798 M_Offset_CDIO_Command
);
3802 static int ni_cdio_cancel(struct comedi_device
*dev
, struct comedi_subdevice
*s
)
3804 struct ni_private
*devpriv __maybe_unused
= dev
->private;
3806 ni_writel(CDO_Disarm_Bit
| CDO_Error_Interrupt_Enable_Clear_Bit
|
3807 CDO_Empty_FIFO_Interrupt_Enable_Clear_Bit
|
3808 CDO_FIFO_Request_Interrupt_Enable_Clear_Bit
,
3809 M_Offset_CDIO_Command
);
3811 * XXX not sure what interrupt C group does ni_writeb(0,
3812 * M_Offset_Interrupt_C_Enable);
3814 ni_writel(0, M_Offset_CDO_Mask_Enable
);
3815 ni_release_cdo_mite_channel(dev
);
3819 static void handle_cdio_interrupt(struct comedi_device
*dev
)
3821 const struct ni_board_struct
*board
= comedi_board(dev
);
3822 struct ni_private
*devpriv __maybe_unused
= dev
->private;
3823 unsigned cdio_status
;
3824 struct comedi_subdevice
*s
= &dev
->subdevices
[NI_DIO_SUBDEV
];
3826 unsigned long flags
;
3829 if ((board
->reg_type
& ni_reg_m_series_mask
) == 0) {
3833 spin_lock_irqsave(&devpriv
->mite_channel_lock
, flags
);
3834 if (devpriv
->cdo_mite_chan
) {
3835 unsigned cdo_mite_status
=
3836 mite_get_status(devpriv
->cdo_mite_chan
);
3837 if (cdo_mite_status
& CHSR_LINKC
) {
3839 devpriv
->mite
->mite_io_addr
+
3840 MITE_CHOR(devpriv
->cdo_mite_chan
->channel
));
3842 mite_sync_output_dma(devpriv
->cdo_mite_chan
, s
->async
);
3844 spin_unlock_irqrestore(&devpriv
->mite_channel_lock
, flags
);
3847 cdio_status
= ni_readl(M_Offset_CDIO_Status
);
3848 if (cdio_status
& (CDO_Overrun_Bit
| CDO_Underflow_Bit
)) {
3849 /* printk("cdio error: statux=0x%x\n", cdio_status); */
3850 ni_writel(CDO_Error_Interrupt_Confirm_Bit
, M_Offset_CDIO_Command
); /* XXX just guessing this is needed and does something useful */
3851 s
->async
->events
|= COMEDI_CB_OVERFLOW
;
3853 if (cdio_status
& CDO_FIFO_Empty_Bit
) {
3854 /* printk("cdio fifo empty\n"); */
3855 ni_writel(CDO_Empty_FIFO_Interrupt_Enable_Clear_Bit
,
3856 M_Offset_CDIO_Command
);
3857 /* s->async->events |= COMEDI_CB_EOA; */
3862 static int ni_serial_insn_config(struct comedi_device
*dev
,
3863 struct comedi_subdevice
*s
,
3864 struct comedi_insn
*insn
, unsigned int *data
)
3866 struct ni_private
*devpriv
= dev
->private;
3868 unsigned char byte_out
, byte_in
= 0;
3874 case INSN_CONFIG_SERIAL_CLOCK
:
3877 printk("SPI serial clock Config cd\n", data
[1]);
3879 devpriv
->serial_hw_mode
= 1;
3880 devpriv
->dio_control
|= DIO_HW_Serial_Enable
;
3882 if (data
[1] == SERIAL_DISABLED
) {
3883 devpriv
->serial_hw_mode
= 0;
3884 devpriv
->dio_control
&= ~(DIO_HW_Serial_Enable
|
3885 DIO_Software_Serial_Control
);
3886 data
[1] = SERIAL_DISABLED
;
3887 devpriv
->serial_interval_ns
= data
[1];
3888 } else if (data
[1] <= SERIAL_600NS
) {
3889 /* Warning: this clock speed is too fast to reliably
3891 devpriv
->dio_control
&= ~DIO_HW_Serial_Timebase
;
3892 devpriv
->clock_and_fout
|= Slow_Internal_Timebase
;
3893 devpriv
->clock_and_fout
&= ~DIO_Serial_Out_Divide_By_2
;
3894 data
[1] = SERIAL_600NS
;
3895 devpriv
->serial_interval_ns
= data
[1];
3896 } else if (data
[1] <= SERIAL_1_2US
) {
3897 devpriv
->dio_control
&= ~DIO_HW_Serial_Timebase
;
3898 devpriv
->clock_and_fout
|= Slow_Internal_Timebase
|
3899 DIO_Serial_Out_Divide_By_2
;
3900 data
[1] = SERIAL_1_2US
;
3901 devpriv
->serial_interval_ns
= data
[1];
3902 } else if (data
[1] <= SERIAL_10US
) {
3903 devpriv
->dio_control
|= DIO_HW_Serial_Timebase
;
3904 devpriv
->clock_and_fout
|= Slow_Internal_Timebase
|
3905 DIO_Serial_Out_Divide_By_2
;
3906 /* Note: DIO_Serial_Out_Divide_By_2 only affects
3907 600ns/1.2us. If you turn divide_by_2 off with the
3908 slow clock, you will still get 10us, except then
3909 all your delays are wrong. */
3910 data
[1] = SERIAL_10US
;
3911 devpriv
->serial_interval_ns
= data
[1];
3913 devpriv
->dio_control
&= ~(DIO_HW_Serial_Enable
|
3914 DIO_Software_Serial_Control
);
3915 devpriv
->serial_hw_mode
= 0;
3916 data
[1] = (data
[1] / 1000) * 1000;
3917 devpriv
->serial_interval_ns
= data
[1];
3920 devpriv
->stc_writew(dev
, devpriv
->dio_control
,
3921 DIO_Control_Register
);
3922 devpriv
->stc_writew(dev
, devpriv
->clock_and_fout
,
3923 Clock_and_FOUT_Register
);
3928 case INSN_CONFIG_BIDIRECTIONAL_DATA
:
3930 if (devpriv
->serial_interval_ns
== 0) {
3934 byte_out
= data
[1] & 0xFF;
3936 if (devpriv
->serial_hw_mode
) {
3937 err
= ni_serial_hw_readwrite8(dev
, s
, byte_out
,
3939 } else if (devpriv
->serial_interval_ns
> 0) {
3940 err
= ni_serial_sw_readwrite8(dev
, s
, byte_out
,
3943 printk("ni_serial_insn_config: serial disabled!\n");
3948 data
[1] = byte_in
& 0xFF;
3958 static int ni_serial_hw_readwrite8(struct comedi_device
*dev
,
3959 struct comedi_subdevice
*s
,
3960 unsigned char data_out
,
3961 unsigned char *data_in
)
3963 struct ni_private
*devpriv
= dev
->private;
3964 unsigned int status1
;
3965 int err
= 0, count
= 20;
3968 printk("ni_serial_hw_readwrite8: outputting 0x%x\n", data_out
);
3971 devpriv
->dio_output
&= ~DIO_Serial_Data_Mask
;
3972 devpriv
->dio_output
|= DIO_Serial_Data_Out(data_out
);
3973 devpriv
->stc_writew(dev
, devpriv
->dio_output
, DIO_Output_Register
);
3975 status1
= devpriv
->stc_readw(dev
, Joint_Status_1_Register
);
3976 if (status1
& DIO_Serial_IO_In_Progress_St
) {
3981 devpriv
->dio_control
|= DIO_HW_Serial_Start
;
3982 devpriv
->stc_writew(dev
, devpriv
->dio_control
, DIO_Control_Register
);
3983 devpriv
->dio_control
&= ~DIO_HW_Serial_Start
;
3985 /* Wait until STC says we're done, but don't loop infinitely. */
3987 devpriv
->stc_readw(dev
,
3988 Joint_Status_1_Register
)) &
3989 DIO_Serial_IO_In_Progress_St
) {
3990 /* Delay one bit per loop */
3991 udelay((devpriv
->serial_interval_ns
+ 999) / 1000);
3994 ("ni_serial_hw_readwrite8: SPI serial I/O didn't finish in time!\n");
4000 /* Delay for last bit. This delay is absolutely necessary, because
4001 DIO_Serial_IO_In_Progress_St goes high one bit too early. */
4002 udelay((devpriv
->serial_interval_ns
+ 999) / 1000);
4004 if (data_in
!= NULL
) {
4005 *data_in
= devpriv
->stc_readw(dev
, DIO_Serial_Input_Register
);
4007 printk("ni_serial_hw_readwrite8: inputted 0x%x\n", *data_in
);
4012 devpriv
->stc_writew(dev
, devpriv
->dio_control
, DIO_Control_Register
);
4017 static int ni_serial_sw_readwrite8(struct comedi_device
*dev
,
4018 struct comedi_subdevice
*s
,
4019 unsigned char data_out
,
4020 unsigned char *data_in
)
4022 struct ni_private
*devpriv
= dev
->private;
4023 unsigned char mask
, input
= 0;
4026 printk("ni_serial_sw_readwrite8: outputting 0x%x\n", data_out
);
4029 /* Wait for one bit before transfer */
4030 udelay((devpriv
->serial_interval_ns
+ 999) / 1000);
4032 for (mask
= 0x80; mask
; mask
>>= 1) {
4033 /* Output current bit; note that we cannot touch s->state
4034 because it is a per-subdevice field, and serial is
4035 a separate subdevice from DIO. */
4036 devpriv
->dio_output
&= ~DIO_SDOUT
;
4037 if (data_out
& mask
) {
4038 devpriv
->dio_output
|= DIO_SDOUT
;
4040 devpriv
->stc_writew(dev
, devpriv
->dio_output
,
4041 DIO_Output_Register
);
4043 /* Assert SDCLK (active low, inverted), wait for half of
4044 the delay, deassert SDCLK, and wait for the other half. */
4045 devpriv
->dio_control
|= DIO_Software_Serial_Control
;
4046 devpriv
->stc_writew(dev
, devpriv
->dio_control
,
4047 DIO_Control_Register
);
4049 udelay((devpriv
->serial_interval_ns
+ 999) / 2000);
4051 devpriv
->dio_control
&= ~DIO_Software_Serial_Control
;
4052 devpriv
->stc_writew(dev
, devpriv
->dio_control
,
4053 DIO_Control_Register
);
4055 udelay((devpriv
->serial_interval_ns
+ 999) / 2000);
4057 /* Input current bit */
4058 if (devpriv
->stc_readw(dev
,
4059 DIO_Parallel_Input_Register
) & DIO_SDIN
)
4061 /* printk("DIO_P_I_R: 0x%x\n", devpriv->stc_readw(dev, DIO_Parallel_Input_Register)); */
4066 printk("ni_serial_sw_readwrite8: inputted 0x%x\n", input
);
4074 static void mio_common_detach(struct comedi_device
*dev
)
4076 const struct ni_board_struct
*board
= comedi_board(dev
);
4077 struct ni_private
*devpriv
= dev
->private;
4078 struct comedi_subdevice
*s
;
4081 if (devpriv
->counter_dev
) {
4082 ni_gpct_device_destroy(devpriv
->counter_dev
);
4085 if (dev
->subdevices
&& board
->has_8255
) {
4086 s
= &dev
->subdevices
[NI_8255_DIO_SUBDEV
];
4087 subdev_8255_cleanup(dev
, s
);
4091 static void init_ao_67xx(struct comedi_device
*dev
, struct comedi_subdevice
*s
)
4095 for (i
= 0; i
< s
->n_chan
; i
++) {
4096 ni_ao_win_outw(dev
, AO_Channel(i
) | 0x0,
4097 AO_Configuration_2_67xx
);
4099 ao_win_out(0x0, AO_Later_Single_Point_Updates
);
4102 static unsigned ni_gpct_to_stc_register(enum ni_gpct_register reg
)
4104 unsigned stc_register
;
4106 case NITIO_G0_Autoincrement_Reg
:
4107 stc_register
= G_Autoincrement_Register(0);
4109 case NITIO_G1_Autoincrement_Reg
:
4110 stc_register
= G_Autoincrement_Register(1);
4112 case NITIO_G0_Command_Reg
:
4113 stc_register
= G_Command_Register(0);
4115 case NITIO_G1_Command_Reg
:
4116 stc_register
= G_Command_Register(1);
4118 case NITIO_G0_HW_Save_Reg
:
4119 stc_register
= G_HW_Save_Register(0);
4121 case NITIO_G1_HW_Save_Reg
:
4122 stc_register
= G_HW_Save_Register(1);
4124 case NITIO_G0_SW_Save_Reg
:
4125 stc_register
= G_Save_Register(0);
4127 case NITIO_G1_SW_Save_Reg
:
4128 stc_register
= G_Save_Register(1);
4130 case NITIO_G0_Mode_Reg
:
4131 stc_register
= G_Mode_Register(0);
4133 case NITIO_G1_Mode_Reg
:
4134 stc_register
= G_Mode_Register(1);
4136 case NITIO_G0_LoadA_Reg
:
4137 stc_register
= G_Load_A_Register(0);
4139 case NITIO_G1_LoadA_Reg
:
4140 stc_register
= G_Load_A_Register(1);
4142 case NITIO_G0_LoadB_Reg
:
4143 stc_register
= G_Load_B_Register(0);
4145 case NITIO_G1_LoadB_Reg
:
4146 stc_register
= G_Load_B_Register(1);
4148 case NITIO_G0_Input_Select_Reg
:
4149 stc_register
= G_Input_Select_Register(0);
4151 case NITIO_G1_Input_Select_Reg
:
4152 stc_register
= G_Input_Select_Register(1);
4154 case NITIO_G01_Status_Reg
:
4155 stc_register
= G_Status_Register
;
4157 case NITIO_G01_Joint_Reset_Reg
:
4158 stc_register
= Joint_Reset_Register
;
4160 case NITIO_G01_Joint_Status1_Reg
:
4161 stc_register
= Joint_Status_1_Register
;
4163 case NITIO_G01_Joint_Status2_Reg
:
4164 stc_register
= Joint_Status_2_Register
;
4166 case NITIO_G0_Interrupt_Acknowledge_Reg
:
4167 stc_register
= Interrupt_A_Ack_Register
;
4169 case NITIO_G1_Interrupt_Acknowledge_Reg
:
4170 stc_register
= Interrupt_B_Ack_Register
;
4172 case NITIO_G0_Status_Reg
:
4173 stc_register
= AI_Status_1_Register
;
4175 case NITIO_G1_Status_Reg
:
4176 stc_register
= AO_Status_1_Register
;
4178 case NITIO_G0_Interrupt_Enable_Reg
:
4179 stc_register
= Interrupt_A_Enable_Register
;
4181 case NITIO_G1_Interrupt_Enable_Reg
:
4182 stc_register
= Interrupt_B_Enable_Register
;
4185 printk("%s: unhandled register 0x%x in switch.\n",
4191 return stc_register
;
4194 static void ni_gpct_write_register(struct ni_gpct
*counter
, unsigned bits
,
4195 enum ni_gpct_register reg
)
4197 struct comedi_device
*dev
= counter
->counter_dev
->dev
;
4198 struct ni_private
*devpriv
= dev
->private;
4199 unsigned stc_register
;
4200 /* bits in the join reset register which are relevant to counters */
4201 static const unsigned gpct_joint_reset_mask
= G0_Reset
| G1_Reset
;
4202 static const unsigned gpct_interrupt_a_enable_mask
=
4203 G0_Gate_Interrupt_Enable
| G0_TC_Interrupt_Enable
;
4204 static const unsigned gpct_interrupt_b_enable_mask
=
4205 G1_Gate_Interrupt_Enable
| G1_TC_Interrupt_Enable
;
4208 /* m-series-only registers */
4209 case NITIO_G0_Counting_Mode_Reg
:
4210 ni_writew(bits
, M_Offset_G0_Counting_Mode
);
4212 case NITIO_G1_Counting_Mode_Reg
:
4213 ni_writew(bits
, M_Offset_G1_Counting_Mode
);
4215 case NITIO_G0_Second_Gate_Reg
:
4216 ni_writew(bits
, M_Offset_G0_Second_Gate
);
4218 case NITIO_G1_Second_Gate_Reg
:
4219 ni_writew(bits
, M_Offset_G1_Second_Gate
);
4221 case NITIO_G0_DMA_Config_Reg
:
4222 ni_writew(bits
, M_Offset_G0_DMA_Config
);
4224 case NITIO_G1_DMA_Config_Reg
:
4225 ni_writew(bits
, M_Offset_G1_DMA_Config
);
4227 case NITIO_G0_ABZ_Reg
:
4228 ni_writew(bits
, M_Offset_G0_MSeries_ABZ
);
4230 case NITIO_G1_ABZ_Reg
:
4231 ni_writew(bits
, M_Offset_G1_MSeries_ABZ
);
4234 /* 32 bit registers */
4235 case NITIO_G0_LoadA_Reg
:
4236 case NITIO_G1_LoadA_Reg
:
4237 case NITIO_G0_LoadB_Reg
:
4238 case NITIO_G1_LoadB_Reg
:
4239 stc_register
= ni_gpct_to_stc_register(reg
);
4240 devpriv
->stc_writel(dev
, bits
, stc_register
);
4243 /* 16 bit registers */
4244 case NITIO_G0_Interrupt_Enable_Reg
:
4245 BUG_ON(bits
& ~gpct_interrupt_a_enable_mask
);
4246 ni_set_bitfield(dev
, Interrupt_A_Enable_Register
,
4247 gpct_interrupt_a_enable_mask
, bits
);
4249 case NITIO_G1_Interrupt_Enable_Reg
:
4250 BUG_ON(bits
& ~gpct_interrupt_b_enable_mask
);
4251 ni_set_bitfield(dev
, Interrupt_B_Enable_Register
,
4252 gpct_interrupt_b_enable_mask
, bits
);
4254 case NITIO_G01_Joint_Reset_Reg
:
4255 BUG_ON(bits
& ~gpct_joint_reset_mask
);
4258 stc_register
= ni_gpct_to_stc_register(reg
);
4259 devpriv
->stc_writew(dev
, bits
, stc_register
);
4263 static unsigned ni_gpct_read_register(struct ni_gpct
*counter
,
4264 enum ni_gpct_register reg
)
4266 struct comedi_device
*dev
= counter
->counter_dev
->dev
;
4267 struct ni_private
*devpriv
= dev
->private;
4268 unsigned stc_register
;
4271 /* m-series only registers */
4272 case NITIO_G0_DMA_Status_Reg
:
4273 return ni_readw(M_Offset_G0_DMA_Status
);
4275 case NITIO_G1_DMA_Status_Reg
:
4276 return ni_readw(M_Offset_G1_DMA_Status
);
4279 /* 32 bit registers */
4280 case NITIO_G0_HW_Save_Reg
:
4281 case NITIO_G1_HW_Save_Reg
:
4282 case NITIO_G0_SW_Save_Reg
:
4283 case NITIO_G1_SW_Save_Reg
:
4284 stc_register
= ni_gpct_to_stc_register(reg
);
4285 return devpriv
->stc_readl(dev
, stc_register
);
4288 /* 16 bit registers */
4290 stc_register
= ni_gpct_to_stc_register(reg
);
4291 return devpriv
->stc_readw(dev
, stc_register
);
4297 static int ni_freq_out_insn_read(struct comedi_device
*dev
,
4298 struct comedi_subdevice
*s
,
4299 struct comedi_insn
*insn
, unsigned int *data
)
4301 struct ni_private
*devpriv
= dev
->private;
4303 data
[0] = devpriv
->clock_and_fout
& FOUT_Divider_mask
;
4307 static int ni_freq_out_insn_write(struct comedi_device
*dev
,
4308 struct comedi_subdevice
*s
,
4309 struct comedi_insn
*insn
, unsigned int *data
)
4311 struct ni_private
*devpriv
= dev
->private;
4313 devpriv
->clock_and_fout
&= ~FOUT_Enable
;
4314 devpriv
->stc_writew(dev
, devpriv
->clock_and_fout
,
4315 Clock_and_FOUT_Register
);
4316 devpriv
->clock_and_fout
&= ~FOUT_Divider_mask
;
4317 devpriv
->clock_and_fout
|= FOUT_Divider(data
[0]);
4318 devpriv
->clock_and_fout
|= FOUT_Enable
;
4319 devpriv
->stc_writew(dev
, devpriv
->clock_and_fout
,
4320 Clock_and_FOUT_Register
);
4324 static int ni_set_freq_out_clock(struct comedi_device
*dev
,
4325 unsigned int clock_source
)
4327 struct ni_private
*devpriv
= dev
->private;
4329 switch (clock_source
) {
4330 case NI_FREQ_OUT_TIMEBASE_1_DIV_2_CLOCK_SRC
:
4331 devpriv
->clock_and_fout
&= ~FOUT_Timebase_Select
;
4333 case NI_FREQ_OUT_TIMEBASE_2_CLOCK_SRC
:
4334 devpriv
->clock_and_fout
|= FOUT_Timebase_Select
;
4339 devpriv
->stc_writew(dev
, devpriv
->clock_and_fout
,
4340 Clock_and_FOUT_Register
);
4344 static void ni_get_freq_out_clock(struct comedi_device
*dev
,
4345 unsigned int *clock_source
,
4346 unsigned int *clock_period_ns
)
4348 struct ni_private
*devpriv
= dev
->private;
4350 if (devpriv
->clock_and_fout
& FOUT_Timebase_Select
) {
4351 *clock_source
= NI_FREQ_OUT_TIMEBASE_2_CLOCK_SRC
;
4352 *clock_period_ns
= TIMEBASE_2_NS
;
4354 *clock_source
= NI_FREQ_OUT_TIMEBASE_1_DIV_2_CLOCK_SRC
;
4355 *clock_period_ns
= TIMEBASE_1_NS
* 2;
4359 static int ni_freq_out_insn_config(struct comedi_device
*dev
,
4360 struct comedi_subdevice
*s
,
4361 struct comedi_insn
*insn
, unsigned int *data
)
4364 case INSN_CONFIG_SET_CLOCK_SRC
:
4365 return ni_set_freq_out_clock(dev
, data
[1]);
4367 case INSN_CONFIG_GET_CLOCK_SRC
:
4368 ni_get_freq_out_clock(dev
, &data
[1], &data
[2]);
4376 static int ni_alloc_private(struct comedi_device
*dev
)
4378 struct ni_private
*devpriv
;
4380 devpriv
= kzalloc(sizeof(*devpriv
), GFP_KERNEL
);
4383 dev
->private = devpriv
;
4385 spin_lock_init(&devpriv
->window_lock
);
4386 spin_lock_init(&devpriv
->soft_reg_copy_lock
);
4387 spin_lock_init(&devpriv
->mite_channel_lock
);
4392 static int ni_E_init(struct comedi_device
*dev
)
4394 const struct ni_board_struct
*board
= comedi_board(dev
);
4395 struct ni_private
*devpriv
= dev
->private;
4396 struct comedi_subdevice
*s
;
4398 enum ni_gpct_variant counter_variant
;
4401 if (board
->n_aochan
> MAX_N_AO_CHAN
) {
4402 printk("bug! n_aochan > MAX_N_AO_CHAN\n");
4406 ret
= comedi_alloc_subdevices(dev
, NI_NUM_SUBDEVICES
);
4410 /* analog input subdevice */
4412 s
= &dev
->subdevices
[NI_AI_SUBDEV
];
4413 dev
->read_subdev
= s
;
4414 if (board
->n_adchan
) {
4415 s
->type
= COMEDI_SUBD_AI
;
4417 SDF_READABLE
| SDF_DIFF
| SDF_DITHER
| SDF_CMD_READ
;
4418 if (board
->reg_type
!= ni_reg_611x
)
4419 s
->subdev_flags
|= SDF_GROUND
| SDF_COMMON
| SDF_OTHER
;
4420 if (board
->adbits
> 16)
4421 s
->subdev_flags
|= SDF_LSAMPL
;
4422 if (board
->reg_type
& ni_reg_m_series_mask
)
4423 s
->subdev_flags
|= SDF_SOFT_CALIBRATED
;
4424 s
->n_chan
= board
->n_adchan
;
4425 s
->len_chanlist
= 512;
4426 s
->maxdata
= (1 << board
->adbits
) - 1;
4427 s
->range_table
= ni_range_lkup
[board
->gainlkup
];
4428 s
->insn_read
= &ni_ai_insn_read
;
4429 s
->insn_config
= &ni_ai_insn_config
;
4430 s
->do_cmdtest
= &ni_ai_cmdtest
;
4431 s
->do_cmd
= &ni_ai_cmd
;
4432 s
->cancel
= &ni_ai_reset
;
4433 s
->poll
= &ni_ai_poll
;
4434 s
->munge
= &ni_ai_munge
;
4436 s
->async_dma_dir
= DMA_FROM_DEVICE
;
4439 s
->type
= COMEDI_SUBD_UNUSED
;
4442 /* analog output subdevice */
4444 s
= &dev
->subdevices
[NI_AO_SUBDEV
];
4445 if (board
->n_aochan
) {
4446 s
->type
= COMEDI_SUBD_AO
;
4447 s
->subdev_flags
= SDF_WRITABLE
| SDF_DEGLITCH
| SDF_GROUND
;
4448 if (board
->reg_type
& ni_reg_m_series_mask
)
4449 s
->subdev_flags
|= SDF_SOFT_CALIBRATED
;
4450 s
->n_chan
= board
->n_aochan
;
4451 s
->maxdata
= (1 << board
->aobits
) - 1;
4452 s
->range_table
= board
->ao_range_table
;
4453 s
->insn_read
= &ni_ao_insn_read
;
4454 if (board
->reg_type
& ni_reg_6xxx_mask
) {
4455 s
->insn_write
= &ni_ao_insn_write_671x
;
4457 s
->insn_write
= &ni_ao_insn_write
;
4459 s
->insn_config
= &ni_ao_insn_config
;
4461 if (board
->n_aochan
) {
4462 s
->async_dma_dir
= DMA_TO_DEVICE
;
4464 if (board
->ao_fifo_depth
) {
4466 dev
->write_subdev
= s
;
4467 s
->subdev_flags
|= SDF_CMD_WRITE
;
4468 s
->do_cmd
= &ni_ao_cmd
;
4469 s
->do_cmdtest
= &ni_ao_cmdtest
;
4470 s
->len_chanlist
= board
->n_aochan
;
4471 if ((board
->reg_type
& ni_reg_m_series_mask
) == 0)
4472 s
->munge
= ni_ao_munge
;
4474 s
->cancel
= &ni_ao_reset
;
4476 s
->type
= COMEDI_SUBD_UNUSED
;
4478 if ((board
->reg_type
& ni_reg_67xx_mask
))
4479 init_ao_67xx(dev
, s
);
4481 /* digital i/o subdevice */
4483 s
= &dev
->subdevices
[NI_DIO_SUBDEV
];
4484 s
->type
= COMEDI_SUBD_DIO
;
4485 s
->subdev_flags
= SDF_WRITABLE
| SDF_READABLE
;
4487 s
->io_bits
= 0; /* all bits input */
4488 s
->range_table
= &range_digital
;
4489 s
->n_chan
= board
->num_p0_dio_channels
;
4490 if (board
->reg_type
& ni_reg_m_series_mask
) {
4492 SDF_LSAMPL
| SDF_CMD_WRITE
/* | SDF_CMD_READ */ ;
4493 s
->insn_bits
= &ni_m_series_dio_insn_bits
;
4494 s
->insn_config
= &ni_m_series_dio_insn_config
;
4495 s
->do_cmd
= &ni_cdio_cmd
;
4496 s
->do_cmdtest
= &ni_cdio_cmdtest
;
4497 s
->cancel
= &ni_cdio_cancel
;
4498 s
->async_dma_dir
= DMA_BIDIRECTIONAL
;
4499 s
->len_chanlist
= s
->n_chan
;
4501 ni_writel(CDO_Reset_Bit
| CDI_Reset_Bit
, M_Offset_CDIO_Command
);
4502 ni_writel(s
->io_bits
, M_Offset_DIO_Direction
);
4504 s
->insn_bits
= &ni_dio_insn_bits
;
4505 s
->insn_config
= &ni_dio_insn_config
;
4506 devpriv
->dio_control
= DIO_Pins_Dir(s
->io_bits
);
4507 ni_writew(devpriv
->dio_control
, DIO_Control_Register
);
4511 s
= &dev
->subdevices
[NI_8255_DIO_SUBDEV
];
4512 if (board
->has_8255
) {
4513 subdev_8255_init(dev
, s
, ni_8255_callback
, (unsigned long)dev
);
4515 s
->type
= COMEDI_SUBD_UNUSED
;
4518 /* formerly general purpose counter/timer device, but no longer used */
4519 s
= &dev
->subdevices
[NI_UNUSED_SUBDEV
];
4520 s
->type
= COMEDI_SUBD_UNUSED
;
4522 /* calibration subdevice -- ai and ao */
4523 s
= &dev
->subdevices
[NI_CALIBRATION_SUBDEV
];
4524 s
->type
= COMEDI_SUBD_CALIB
;
4525 if (board
->reg_type
& ni_reg_m_series_mask
) {
4526 /* internal PWM analog output used for AI nonlinearity calibration */
4527 s
->subdev_flags
= SDF_INTERNAL
;
4528 s
->insn_config
= &ni_m_series_pwm_config
;
4531 ni_writel(0x0, M_Offset_Cal_PWM
);
4532 } else if (board
->reg_type
== ni_reg_6143
) {
4533 /* internal PWM analog output used for AI nonlinearity calibration */
4534 s
->subdev_flags
= SDF_INTERNAL
;
4535 s
->insn_config
= &ni_6143_pwm_config
;
4539 s
->subdev_flags
= SDF_WRITABLE
| SDF_INTERNAL
;
4540 s
->insn_read
= &ni_calib_insn_read
;
4541 s
->insn_write
= &ni_calib_insn_write
;
4542 caldac_setup(dev
, s
);
4546 s
= &dev
->subdevices
[NI_EEPROM_SUBDEV
];
4547 s
->type
= COMEDI_SUBD_MEMORY
;
4548 s
->subdev_flags
= SDF_READABLE
| SDF_INTERNAL
;
4550 if (board
->reg_type
& ni_reg_m_series_mask
) {
4551 s
->n_chan
= M_SERIES_EEPROM_SIZE
;
4552 s
->insn_read
= &ni_m_series_eeprom_insn_read
;
4555 s
->insn_read
= &ni_eeprom_insn_read
;
4559 s
= &dev
->subdevices
[NI_PFI_DIO_SUBDEV
];
4560 s
->type
= COMEDI_SUBD_DIO
;
4561 s
->subdev_flags
= SDF_READABLE
| SDF_WRITABLE
| SDF_INTERNAL
;
4562 if (board
->reg_type
& ni_reg_m_series_mask
) {
4565 ni_writew(s
->state
, M_Offset_PFI_DO
);
4566 for (i
= 0; i
< NUM_PFI_OUTPUT_SELECT_REGS
; ++i
) {
4567 ni_writew(devpriv
->pfi_output_select_reg
[i
],
4568 M_Offset_PFI_Output_Select(i
+ 1));
4574 if (board
->reg_type
& ni_reg_m_series_mask
) {
4575 s
->insn_bits
= &ni_pfi_insn_bits
;
4577 s
->insn_config
= &ni_pfi_insn_config
;
4578 ni_set_bits(dev
, IO_Bidirection_Pin_Register
, ~0, 0);
4580 /* cs5529 calibration adc */
4581 s
= &dev
->subdevices
[NI_CS5529_CALIBRATION_SUBDEV
];
4582 if (board
->reg_type
& ni_reg_67xx_mask
) {
4583 s
->type
= COMEDI_SUBD_AI
;
4584 s
->subdev_flags
= SDF_READABLE
| SDF_DIFF
| SDF_INTERNAL
;
4585 /* one channel for each analog output channel */
4586 s
->n_chan
= board
->n_aochan
;
4587 s
->maxdata
= (1 << 16) - 1;
4588 s
->range_table
= &range_unknown
; /* XXX */
4589 s
->insn_read
= cs5529_ai_insn_read
;
4590 s
->insn_config
= NULL
;
4593 s
->type
= COMEDI_SUBD_UNUSED
;
4597 s
= &dev
->subdevices
[NI_SERIAL_SUBDEV
];
4598 s
->type
= COMEDI_SUBD_SERIAL
;
4599 s
->subdev_flags
= SDF_READABLE
| SDF_WRITABLE
| SDF_INTERNAL
;
4602 s
->insn_config
= ni_serial_insn_config
;
4603 devpriv
->serial_interval_ns
= 0;
4604 devpriv
->serial_hw_mode
= 0;
4607 s
= &dev
->subdevices
[NI_RTSI_SUBDEV
];
4608 s
->type
= COMEDI_SUBD_DIO
;
4609 s
->subdev_flags
= SDF_READABLE
| SDF_WRITABLE
| SDF_INTERNAL
;
4612 s
->insn_bits
= ni_rtsi_insn_bits
;
4613 s
->insn_config
= ni_rtsi_insn_config
;
4616 if (board
->reg_type
& ni_reg_m_series_mask
) {
4617 counter_variant
= ni_gpct_variant_m_series
;
4619 counter_variant
= ni_gpct_variant_e_series
;
4621 devpriv
->counter_dev
= ni_gpct_device_construct(dev
,
4622 &ni_gpct_write_register
,
4623 &ni_gpct_read_register
,
4626 /* General purpose counters */
4627 for (j
= 0; j
< NUM_GPCT
; ++j
) {
4628 s
= &dev
->subdevices
[NI_GPCT_SUBDEV(j
)];
4629 s
->type
= COMEDI_SUBD_COUNTER
;
4631 SDF_READABLE
| SDF_WRITABLE
| SDF_LSAMPL
| SDF_CMD_READ
4632 /* | SDF_CMD_WRITE */ ;
4634 if (board
->reg_type
& ni_reg_m_series_mask
)
4635 s
->maxdata
= 0xffffffff;
4637 s
->maxdata
= 0xffffff;
4638 s
->insn_read
= &ni_gpct_insn_read
;
4639 s
->insn_write
= &ni_gpct_insn_write
;
4640 s
->insn_config
= &ni_gpct_insn_config
;
4641 s
->do_cmd
= &ni_gpct_cmd
;
4642 s
->len_chanlist
= 1;
4643 s
->do_cmdtest
= &ni_gpct_cmdtest
;
4644 s
->cancel
= &ni_gpct_cancel
;
4645 s
->async_dma_dir
= DMA_BIDIRECTIONAL
;
4646 s
->private = &devpriv
->counter_dev
->counters
[j
];
4648 devpriv
->counter_dev
->counters
[j
].chip_index
= 0;
4649 devpriv
->counter_dev
->counters
[j
].counter_index
= j
;
4650 ni_tio_init_counter(&devpriv
->counter_dev
->counters
[j
]);
4653 /* Frequency output */
4654 s
= &dev
->subdevices
[NI_FREQ_OUT_SUBDEV
];
4655 s
->type
= COMEDI_SUBD_COUNTER
;
4656 s
->subdev_flags
= SDF_READABLE
| SDF_WRITABLE
;
4659 s
->insn_read
= &ni_freq_out_insn_read
;
4660 s
->insn_write
= &ni_freq_out_insn_write
;
4661 s
->insn_config
= &ni_freq_out_insn_config
;
4663 /* ai configuration */
4664 s
= &dev
->subdevices
[NI_AI_SUBDEV
];
4665 ni_ai_reset(dev
, s
);
4666 if ((board
->reg_type
& ni_reg_6xxx_mask
) == 0) {
4667 /* BEAM is this needed for PCI-6143 ?? */
4668 devpriv
->clock_and_fout
=
4669 Slow_Internal_Time_Divide_By_2
|
4670 Slow_Internal_Timebase
|
4671 Clock_To_Board_Divide_By_2
|
4673 AI_Output_Divide_By_2
| AO_Output_Divide_By_2
;
4675 devpriv
->clock_and_fout
=
4676 Slow_Internal_Time_Divide_By_2
|
4677 Slow_Internal_Timebase
|
4678 Clock_To_Board_Divide_By_2
| Clock_To_Board
;
4680 devpriv
->stc_writew(dev
, devpriv
->clock_and_fout
,
4681 Clock_and_FOUT_Register
);
4683 /* analog output configuration */
4684 s
= &dev
->subdevices
[NI_AO_SUBDEV
];
4685 ni_ao_reset(dev
, s
);
4688 devpriv
->stc_writew(dev
,
4689 (IRQ_POLARITY
? Interrupt_Output_Polarity
:
4690 0) | (Interrupt_Output_On_3_Pins
& 0) |
4691 Interrupt_A_Enable
| Interrupt_B_Enable
|
4692 Interrupt_A_Output_Select(interrupt_pin
4694 Interrupt_B_Output_Select(interrupt_pin
4696 Interrupt_Control_Register
);
4700 ni_writeb(devpriv
->ai_ao_select_reg
, AI_AO_Select
);
4701 ni_writeb(devpriv
->g0_g1_select_reg
, G0_G1_Select
);
4703 if (board
->reg_type
& ni_reg_6xxx_mask
) {
4704 ni_writeb(0, Magic_611x
);
4705 } else if (board
->reg_type
& ni_reg_m_series_mask
) {
4707 for (channel
= 0; channel
< board
->n_aochan
; ++channel
) {
4708 ni_writeb(0xf, M_Offset_AO_Waveform_Order(channel
));
4710 M_Offset_AO_Reference_Attenuation(channel
));
4712 ni_writeb(0x0, M_Offset_AO_Calibration
);
4719 static int ni_8255_callback(int dir
, int port
, int data
, unsigned long arg
)
4721 struct comedi_device
*dev
= (struct comedi_device
*)arg
;
4722 struct ni_private
*devpriv __maybe_unused
= dev
->private;
4725 ni_writeb(data
, Port_A
+ 2 * port
);
4728 return ni_readb(Port_A
+ 2 * port
);
4733 presents the EEPROM as a subdevice
4736 static int ni_eeprom_insn_read(struct comedi_device
*dev
,
4737 struct comedi_subdevice
*s
,
4738 struct comedi_insn
*insn
, unsigned int *data
)
4740 data
[0] = ni_read_eeprom(dev
, CR_CHAN(insn
->chanspec
));
4746 reads bytes out of eeprom
4749 static int ni_read_eeprom(struct comedi_device
*dev
, int addr
)
4751 struct ni_private
*devpriv __maybe_unused
= dev
->private;
4755 bitstring
= 0x0300 | ((addr
& 0x100) << 3) | (addr
& 0xff);
4756 ni_writeb(0x04, Serial_Command
);
4757 for (bit
= 0x8000; bit
; bit
>>= 1) {
4758 ni_writeb(0x04 | ((bit
& bitstring
) ? 0x02 : 0),
4760 ni_writeb(0x05 | ((bit
& bitstring
) ? 0x02 : 0),
4764 for (bit
= 0x80; bit
; bit
>>= 1) {
4765 ni_writeb(0x04, Serial_Command
);
4766 ni_writeb(0x05, Serial_Command
);
4767 bitstring
|= ((ni_readb(XXX_Status
) & PROMOUT
) ? bit
: 0);
4769 ni_writeb(0x00, Serial_Command
);
4774 static int ni_m_series_eeprom_insn_read(struct comedi_device
*dev
,
4775 struct comedi_subdevice
*s
,
4776 struct comedi_insn
*insn
,
4779 struct ni_private
*devpriv
= dev
->private;
4781 data
[0] = devpriv
->eeprom_buffer
[CR_CHAN(insn
->chanspec
)];
4786 static int ni_get_pwm_config(struct comedi_device
*dev
, unsigned int *data
)
4788 struct ni_private
*devpriv
= dev
->private;
4790 data
[1] = devpriv
->pwm_up_count
* devpriv
->clock_ns
;
4791 data
[2] = devpriv
->pwm_down_count
* devpriv
->clock_ns
;
4795 static int ni_m_series_pwm_config(struct comedi_device
*dev
,
4796 struct comedi_subdevice
*s
,
4797 struct comedi_insn
*insn
, unsigned int *data
)
4799 struct ni_private
*devpriv
= dev
->private;
4800 unsigned up_count
, down_count
;
4803 case INSN_CONFIG_PWM_OUTPUT
:
4805 case TRIG_ROUND_NEAREST
:
4808 devpriv
->clock_ns
/ 2) / devpriv
->clock_ns
;
4810 case TRIG_ROUND_DOWN
:
4811 up_count
= data
[2] / devpriv
->clock_ns
;
4815 (data
[2] + devpriv
->clock_ns
-
4816 1) / devpriv
->clock_ns
;
4823 case TRIG_ROUND_NEAREST
:
4826 devpriv
->clock_ns
/ 2) / devpriv
->clock_ns
;
4828 case TRIG_ROUND_DOWN
:
4829 down_count
= data
[4] / devpriv
->clock_ns
;
4833 (data
[4] + devpriv
->clock_ns
-
4834 1) / devpriv
->clock_ns
;
4840 if (up_count
* devpriv
->clock_ns
!= data
[2] ||
4841 down_count
* devpriv
->clock_ns
!= data
[4]) {
4842 data
[2] = up_count
* devpriv
->clock_ns
;
4843 data
[4] = down_count
* devpriv
->clock_ns
;
4846 ni_writel(MSeries_Cal_PWM_High_Time_Bits(up_count
) |
4847 MSeries_Cal_PWM_Low_Time_Bits(down_count
),
4849 devpriv
->pwm_up_count
= up_count
;
4850 devpriv
->pwm_down_count
= down_count
;
4853 case INSN_CONFIG_GET_PWM_OUTPUT
:
4854 return ni_get_pwm_config(dev
, data
);
4863 static int ni_6143_pwm_config(struct comedi_device
*dev
,
4864 struct comedi_subdevice
*s
,
4865 struct comedi_insn
*insn
, unsigned int *data
)
4867 struct ni_private
*devpriv
= dev
->private;
4868 unsigned up_count
, down_count
;
4871 case INSN_CONFIG_PWM_OUTPUT
:
4873 case TRIG_ROUND_NEAREST
:
4876 devpriv
->clock_ns
/ 2) / devpriv
->clock_ns
;
4878 case TRIG_ROUND_DOWN
:
4879 up_count
= data
[2] / devpriv
->clock_ns
;
4883 (data
[2] + devpriv
->clock_ns
-
4884 1) / devpriv
->clock_ns
;
4891 case TRIG_ROUND_NEAREST
:
4894 devpriv
->clock_ns
/ 2) / devpriv
->clock_ns
;
4896 case TRIG_ROUND_DOWN
:
4897 down_count
= data
[4] / devpriv
->clock_ns
;
4901 (data
[4] + devpriv
->clock_ns
-
4902 1) / devpriv
->clock_ns
;
4908 if (up_count
* devpriv
->clock_ns
!= data
[2] ||
4909 down_count
* devpriv
->clock_ns
!= data
[4]) {
4910 data
[2] = up_count
* devpriv
->clock_ns
;
4911 data
[4] = down_count
* devpriv
->clock_ns
;
4914 ni_writel(up_count
, Calibration_HighTime_6143
);
4915 devpriv
->pwm_up_count
= up_count
;
4916 ni_writel(down_count
, Calibration_LowTime_6143
);
4917 devpriv
->pwm_down_count
= down_count
;
4920 case INSN_CONFIG_GET_PWM_OUTPUT
:
4921 return ni_get_pwm_config(dev
, data
);
4929 static void ni_write_caldac(struct comedi_device
*dev
, int addr
, int val
);
4931 calibration subdevice
4933 static int ni_calib_insn_write(struct comedi_device
*dev
,
4934 struct comedi_subdevice
*s
,
4935 struct comedi_insn
*insn
, unsigned int *data
)
4937 ni_write_caldac(dev
, CR_CHAN(insn
->chanspec
), data
[0]);
4942 static int ni_calib_insn_read(struct comedi_device
*dev
,
4943 struct comedi_subdevice
*s
,
4944 struct comedi_insn
*insn
, unsigned int *data
)
4946 struct ni_private
*devpriv
= dev
->private;
4948 data
[0] = devpriv
->caldacs
[CR_CHAN(insn
->chanspec
)];
4953 static int pack_mb88341(int addr
, int val
, int *bitstring
);
4954 static int pack_dac8800(int addr
, int val
, int *bitstring
);
4955 static int pack_dac8043(int addr
, int val
, int *bitstring
);
4956 static int pack_ad8522(int addr
, int val
, int *bitstring
);
4957 static int pack_ad8804(int addr
, int val
, int *bitstring
);
4958 static int pack_ad8842(int addr
, int val
, int *bitstring
);
4960 struct caldac_struct
{
4963 int (*packbits
) (int, int, int *);
4966 static struct caldac_struct caldacs
[] = {
4967 [mb88341
] = {12, 8, pack_mb88341
},
4968 [dac8800
] = {8, 8, pack_dac8800
},
4969 [dac8043
] = {1, 12, pack_dac8043
},
4970 [ad8522
] = {2, 12, pack_ad8522
},
4971 [ad8804
] = {12, 8, pack_ad8804
},
4972 [ad8842
] = {8, 8, pack_ad8842
},
4973 [ad8804_debug
] = {16, 8, pack_ad8804
},
4976 static void caldac_setup(struct comedi_device
*dev
, struct comedi_subdevice
*s
)
4978 const struct ni_board_struct
*board
= comedi_board(dev
);
4979 struct ni_private
*devpriv
= dev
->private;
4988 type
= board
->caldac
[0];
4989 if (type
== caldac_none
)
4991 n_bits
= caldacs
[type
].n_bits
;
4992 for (i
= 0; i
< 3; i
++) {
4993 type
= board
->caldac
[i
];
4994 if (type
== caldac_none
)
4996 if (caldacs
[type
].n_bits
!= n_bits
)
4998 n_chans
+= caldacs
[type
].n_chans
;
5001 s
->n_chan
= n_chans
;
5004 unsigned int *maxdata_list
;
5006 if (n_chans
> MAX_N_CALDACS
) {
5007 printk("BUG! MAX_N_CALDACS too small\n");
5009 s
->maxdata_list
= maxdata_list
= devpriv
->caldac_maxdata_list
;
5011 for (i
= 0; i
< n_dacs
; i
++) {
5012 type
= board
->caldac
[i
];
5013 for (j
= 0; j
< caldacs
[type
].n_chans
; j
++) {
5014 maxdata_list
[chan
] =
5015 (1 << caldacs
[type
].n_bits
) - 1;
5020 for (chan
= 0; chan
< s
->n_chan
; chan
++)
5021 ni_write_caldac(dev
, i
, s
->maxdata_list
[i
] / 2);
5023 type
= board
->caldac
[0];
5024 s
->maxdata
= (1 << caldacs
[type
].n_bits
) - 1;
5026 for (chan
= 0; chan
< s
->n_chan
; chan
++)
5027 ni_write_caldac(dev
, i
, s
->maxdata
/ 2);
5031 static void ni_write_caldac(struct comedi_device
*dev
, int addr
, int val
)
5033 const struct ni_board_struct
*board
= comedi_board(dev
);
5034 struct ni_private
*devpriv
= dev
->private;
5035 unsigned int loadbit
= 0, bits
= 0, bit
, bitstring
= 0;
5039 /* printk("ni_write_caldac: chan=%d val=%d\n",addr,val); */
5040 if (devpriv
->caldacs
[addr
] == val
)
5042 devpriv
->caldacs
[addr
] = val
;
5044 for (i
= 0; i
< 3; i
++) {
5045 type
= board
->caldac
[i
];
5046 if (type
== caldac_none
)
5048 if (addr
< caldacs
[type
].n_chans
) {
5049 bits
= caldacs
[type
].packbits(addr
, val
, &bitstring
);
5050 loadbit
= SerDacLd(i
);
5051 /* printk("caldac: using i=%d addr=%d %x\n",i,addr,bitstring); */
5054 addr
-= caldacs
[type
].n_chans
;
5057 for (bit
= 1 << (bits
- 1); bit
; bit
>>= 1) {
5058 ni_writeb(((bit
& bitstring
) ? 0x02 : 0), Serial_Command
);
5060 ni_writeb(1 | ((bit
& bitstring
) ? 0x02 : 0), Serial_Command
);
5063 ni_writeb(loadbit
, Serial_Command
);
5065 ni_writeb(0, Serial_Command
);
5068 static int pack_mb88341(int addr
, int val
, int *bitstring
)
5072 Note that address bits are reversed. Thanks to
5073 Ingo Keen for noticing this.
5075 Note also that the 88341 expects address values from
5076 1-12, whereas we use channel numbers 0-11. The NI
5077 docs use 1-12, also, so be careful here.
5080 *bitstring
= ((addr
& 0x1) << 11) |
5081 ((addr
& 0x2) << 9) |
5082 ((addr
& 0x4) << 7) | ((addr
& 0x8) << 5) | (val
& 0xff);
5086 static int pack_dac8800(int addr
, int val
, int *bitstring
)
5088 *bitstring
= ((addr
& 0x7) << 8) | (val
& 0xff);
5092 static int pack_dac8043(int addr
, int val
, int *bitstring
)
5094 *bitstring
= val
& 0xfff;
5098 static int pack_ad8522(int addr
, int val
, int *bitstring
)
5100 *bitstring
= (val
& 0xfff) | (addr
? 0xc000 : 0xa000);
5104 static int pack_ad8804(int addr
, int val
, int *bitstring
)
5106 *bitstring
= ((addr
& 0xf) << 8) | (val
& 0xff);
5110 static int pack_ad8842(int addr
, int val
, int *bitstring
)
5112 *bitstring
= ((addr
+ 1) << 8) | (val
& 0xff);
5118 * Read the GPCTs current value.
5120 static int GPCT_G_Watch(struct comedi_device
*dev
, int chan
)
5122 unsigned int hi1
, hi2
, lo
;
5124 devpriv
->gpct_command
[chan
] &= ~G_Save_Trace
;
5125 devpriv
->stc_writew(dev
, devpriv
->gpct_command
[chan
],
5126 G_Command_Register(chan
));
5128 devpriv
->gpct_command
[chan
] |= G_Save_Trace
;
5129 devpriv
->stc_writew(dev
, devpriv
->gpct_command
[chan
],
5130 G_Command_Register(chan
));
5132 /* This procedure is used because the two registers cannot
5133 * be read atomically. */
5135 hi1
= devpriv
->stc_readw(dev
, G_Save_Register_High(chan
));
5136 lo
= devpriv
->stc_readw(dev
, G_Save_Register_Low(chan
));
5137 hi2
= devpriv
->stc_readw(dev
, G_Save_Register_High(chan
));
5138 } while (hi1
!= hi2
);
5140 return (hi1
<< 16) | lo
;
5143 static void GPCT_Reset(struct comedi_device
*dev
, int chan
)
5145 int temp_ack_reg
= 0;
5147 /* printk("GPCT_Reset..."); */
5148 devpriv
->gpct_cur_operation
[chan
] = GPCT_RESET
;
5152 devpriv
->stc_writew(dev
, G0_Reset
, Joint_Reset_Register
);
5153 ni_set_bits(dev
, Interrupt_A_Enable_Register
,
5154 G0_TC_Interrupt_Enable
, 0);
5155 ni_set_bits(dev
, Interrupt_A_Enable_Register
,
5156 G0_Gate_Interrupt_Enable
, 0);
5157 temp_ack_reg
|= G0_Gate_Error_Confirm
;
5158 temp_ack_reg
|= G0_TC_Error_Confirm
;
5159 temp_ack_reg
|= G0_TC_Interrupt_Ack
;
5160 temp_ack_reg
|= G0_Gate_Interrupt_Ack
;
5161 devpriv
->stc_writew(dev
, temp_ack_reg
,
5162 Interrupt_A_Ack_Register
);
5164 /* problem...this interferes with the other ctr... */
5165 devpriv
->an_trig_etc_reg
|= GPFO_0_Output_Enable
;
5166 devpriv
->stc_writew(dev
, devpriv
->an_trig_etc_reg
,
5167 Analog_Trigger_Etc_Register
);
5170 devpriv
->stc_writew(dev
, G1_Reset
, Joint_Reset_Register
);
5171 ni_set_bits(dev
, Interrupt_B_Enable_Register
,
5172 G1_TC_Interrupt_Enable
, 0);
5173 ni_set_bits(dev
, Interrupt_B_Enable_Register
,
5174 G0_Gate_Interrupt_Enable
, 0);
5175 temp_ack_reg
|= G1_Gate_Error_Confirm
;
5176 temp_ack_reg
|= G1_TC_Error_Confirm
;
5177 temp_ack_reg
|= G1_TC_Interrupt_Ack
;
5178 temp_ack_reg
|= G1_Gate_Interrupt_Ack
;
5179 devpriv
->stc_writew(dev
, temp_ack_reg
,
5180 Interrupt_B_Ack_Register
);
5182 devpriv
->an_trig_etc_reg
|= GPFO_1_Output_Enable
;
5183 devpriv
->stc_writew(dev
, devpriv
->an_trig_etc_reg
,
5184 Analog_Trigger_Etc_Register
);
5188 devpriv
->gpct_mode
[chan
] = 0;
5189 devpriv
->gpct_input_select
[chan
] = 0;
5190 devpriv
->gpct_command
[chan
] = 0;
5192 devpriv
->gpct_command
[chan
] |= G_Synchronized_Gate
;
5194 devpriv
->stc_writew(dev
, devpriv
->gpct_mode
[chan
],
5195 G_Mode_Register(chan
));
5196 devpriv
->stc_writew(dev
, devpriv
->gpct_input_select
[chan
],
5197 G_Input_Select_Register(chan
));
5198 devpriv
->stc_writew(dev
, 0, G_Autoincrement_Register(chan
));
5200 /* printk("exit GPCT_Reset\n"); */
5205 static int ni_gpct_insn_config(struct comedi_device
*dev
,
5206 struct comedi_subdevice
*s
,
5207 struct comedi_insn
*insn
, unsigned int *data
)
5209 struct ni_gpct
*counter
= s
->private;
5210 return ni_tio_insn_config(counter
, insn
, data
);
5213 static int ni_gpct_insn_read(struct comedi_device
*dev
,
5214 struct comedi_subdevice
*s
,
5215 struct comedi_insn
*insn
, unsigned int *data
)
5217 struct ni_gpct
*counter
= s
->private;
5218 return ni_tio_rinsn(counter
, insn
, data
);
5221 static int ni_gpct_insn_write(struct comedi_device
*dev
,
5222 struct comedi_subdevice
*s
,
5223 struct comedi_insn
*insn
, unsigned int *data
)
5225 struct ni_gpct
*counter
= s
->private;
5226 return ni_tio_winsn(counter
, insn
, data
);
5229 static int ni_gpct_cmd(struct comedi_device
*dev
, struct comedi_subdevice
*s
)
5233 struct ni_gpct
*counter
= s
->private;
5234 /* const struct comedi_cmd *cmd = &s->async->cmd; */
5236 retval
= ni_request_gpct_mite_channel(dev
, counter
->counter_index
,
5240 "no dma channel available for use by counter");
5243 ni_tio_acknowledge_and_confirm(counter
, NULL
, NULL
, NULL
, NULL
);
5244 ni_e_series_enable_second_irq(dev
, counter
->counter_index
, 1);
5245 retval
= ni_tio_cmd(counter
, s
->async
);
5252 static int ni_gpct_cmdtest(struct comedi_device
*dev
,
5253 struct comedi_subdevice
*s
, struct comedi_cmd
*cmd
)
5256 struct ni_gpct
*counter
= s
->private;
5258 return ni_tio_cmdtest(counter
, cmd
);
5264 static int ni_gpct_cancel(struct comedi_device
*dev
, struct comedi_subdevice
*s
)
5267 struct ni_gpct
*counter
= s
->private;
5270 retval
= ni_tio_cancel(counter
);
5271 ni_e_series_enable_second_irq(dev
, counter
->counter_index
, 0);
5272 ni_release_gpct_mite_channel(dev
, counter
->counter_index
);
5281 * Programmable Function Inputs
5285 static int ni_m_series_set_pfi_routing(struct comedi_device
*dev
, unsigned chan
,
5288 struct ni_private
*devpriv
= dev
->private;
5289 unsigned pfi_reg_index
;
5290 unsigned array_offset
;
5292 if ((source
& 0x1f) != source
)
5294 pfi_reg_index
= 1 + chan
/ 3;
5295 array_offset
= pfi_reg_index
- 1;
5296 devpriv
->pfi_output_select_reg
[array_offset
] &=
5297 ~MSeries_PFI_Output_Select_Mask(chan
);
5298 devpriv
->pfi_output_select_reg
[array_offset
] |=
5299 MSeries_PFI_Output_Select_Bits(chan
, source
);
5300 ni_writew(devpriv
->pfi_output_select_reg
[array_offset
],
5301 M_Offset_PFI_Output_Select(pfi_reg_index
));
5305 static int ni_old_set_pfi_routing(struct comedi_device
*dev
, unsigned chan
,
5308 /* pre-m-series boards have fixed signals on pfi pins */
5309 if (source
!= ni_old_get_pfi_routing(dev
, chan
))
5314 static int ni_set_pfi_routing(struct comedi_device
*dev
, unsigned chan
,
5317 const struct ni_board_struct
*board
= comedi_board(dev
);
5319 if (board
->reg_type
& ni_reg_m_series_mask
)
5320 return ni_m_series_set_pfi_routing(dev
, chan
, source
);
5322 return ni_old_set_pfi_routing(dev
, chan
, source
);
5325 static unsigned ni_m_series_get_pfi_routing(struct comedi_device
*dev
,
5328 struct ni_private
*devpriv
= dev
->private;
5329 const unsigned array_offset
= chan
/ 3;
5331 return MSeries_PFI_Output_Select_Source(chan
,
5333 pfi_output_select_reg
5337 static unsigned ni_old_get_pfi_routing(struct comedi_device
*dev
, unsigned chan
)
5339 /* pre-m-series boards have fixed signals on pfi pins */
5342 return NI_PFI_OUTPUT_AI_START1
;
5345 return NI_PFI_OUTPUT_AI_START2
;
5348 return NI_PFI_OUTPUT_AI_CONVERT
;
5351 return NI_PFI_OUTPUT_G_SRC1
;
5354 return NI_PFI_OUTPUT_G_GATE1
;
5357 return NI_PFI_OUTPUT_AO_UPDATE_N
;
5360 return NI_PFI_OUTPUT_AO_START1
;
5363 return NI_PFI_OUTPUT_AI_START_PULSE
;
5366 return NI_PFI_OUTPUT_G_SRC0
;
5369 return NI_PFI_OUTPUT_G_GATE0
;
5372 printk("%s: bug, unhandled case in switch.\n", __func__
);
5378 static unsigned ni_get_pfi_routing(struct comedi_device
*dev
, unsigned chan
)
5380 const struct ni_board_struct
*board
= comedi_board(dev
);
5382 if (board
->reg_type
& ni_reg_m_series_mask
)
5383 return ni_m_series_get_pfi_routing(dev
, chan
);
5385 return ni_old_get_pfi_routing(dev
, chan
);
5388 static int ni_config_filter(struct comedi_device
*dev
, unsigned pfi_channel
,
5389 enum ni_pfi_filter_select filter
)
5391 const struct ni_board_struct
*board
= comedi_board(dev
);
5392 struct ni_private
*devpriv __maybe_unused
= dev
->private;
5395 if ((board
->reg_type
& ni_reg_m_series_mask
) == 0) {
5398 bits
= ni_readl(M_Offset_PFI_Filter
);
5399 bits
&= ~MSeries_PFI_Filter_Select_Mask(pfi_channel
);
5400 bits
|= MSeries_PFI_Filter_Select_Bits(pfi_channel
, filter
);
5401 ni_writel(bits
, M_Offset_PFI_Filter
);
5405 static int ni_pfi_insn_bits(struct comedi_device
*dev
,
5406 struct comedi_subdevice
*s
,
5407 struct comedi_insn
*insn
, unsigned int *data
)
5409 const struct ni_board_struct
*board
= comedi_board(dev
);
5410 struct ni_private
*devpriv __maybe_unused
= dev
->private;
5412 if ((board
->reg_type
& ni_reg_m_series_mask
) == 0) {
5416 s
->state
&= ~data
[0];
5417 s
->state
|= (data
[0] & data
[1]);
5418 ni_writew(s
->state
, M_Offset_PFI_DO
);
5420 data
[1] = ni_readw(M_Offset_PFI_DI
);
5424 static int ni_pfi_insn_config(struct comedi_device
*dev
,
5425 struct comedi_subdevice
*s
,
5426 struct comedi_insn
*insn
, unsigned int *data
)
5428 struct ni_private
*devpriv
= dev
->private;
5434 chan
= CR_CHAN(insn
->chanspec
);
5438 ni_set_bits(dev
, IO_Bidirection_Pin_Register
, 1 << chan
, 1);
5441 ni_set_bits(dev
, IO_Bidirection_Pin_Register
, 1 << chan
, 0);
5443 case INSN_CONFIG_DIO_QUERY
:
5445 (devpriv
->io_bidirection_pin_reg
& (1 << chan
)) ?
5446 COMEDI_OUTPUT
: COMEDI_INPUT
;
5449 case INSN_CONFIG_SET_ROUTING
:
5450 return ni_set_pfi_routing(dev
, chan
, data
[1]);
5452 case INSN_CONFIG_GET_ROUTING
:
5453 data
[1] = ni_get_pfi_routing(dev
, chan
);
5455 case INSN_CONFIG_FILTER
:
5456 return ni_config_filter(dev
, chan
, data
[1]);
5466 * NI RTSI Bus Functions
5469 static void ni_rtsi_init(struct comedi_device
*dev
)
5471 const struct ni_board_struct
*board
= comedi_board(dev
);
5472 struct ni_private
*devpriv
= dev
->private;
5474 /* Initialises the RTSI bus signal switch to a default state */
5476 /* Set clock mode to internal */
5477 devpriv
->clock_and_fout2
= MSeries_RTSI_10MHz_Bit
;
5478 if (ni_set_master_clock(dev
, NI_MIO_INTERNAL_CLOCK
, 0) < 0) {
5479 printk("ni_set_master_clock failed, bug?");
5481 /* default internal lines routing to RTSI bus lines */
5482 devpriv
->rtsi_trig_a_output_reg
=
5483 RTSI_Trig_Output_Bits(0,
5484 NI_RTSI_OUTPUT_ADR_START1
) |
5485 RTSI_Trig_Output_Bits(1,
5486 NI_RTSI_OUTPUT_ADR_START2
) |
5487 RTSI_Trig_Output_Bits(2,
5488 NI_RTSI_OUTPUT_SCLKG
) |
5489 RTSI_Trig_Output_Bits(3, NI_RTSI_OUTPUT_DACUPDN
);
5490 devpriv
->stc_writew(dev
, devpriv
->rtsi_trig_a_output_reg
,
5491 RTSI_Trig_A_Output_Register
);
5492 devpriv
->rtsi_trig_b_output_reg
=
5493 RTSI_Trig_Output_Bits(4,
5494 NI_RTSI_OUTPUT_DA_START1
) |
5495 RTSI_Trig_Output_Bits(5,
5496 NI_RTSI_OUTPUT_G_SRC0
) |
5497 RTSI_Trig_Output_Bits(6, NI_RTSI_OUTPUT_G_GATE0
);
5498 if (board
->reg_type
& ni_reg_m_series_mask
)
5499 devpriv
->rtsi_trig_b_output_reg
|=
5500 RTSI_Trig_Output_Bits(7, NI_RTSI_OUTPUT_RTSI_OSC
);
5501 devpriv
->stc_writew(dev
, devpriv
->rtsi_trig_b_output_reg
,
5502 RTSI_Trig_B_Output_Register
);
5505 * Sets the source and direction of the 4 on board lines
5506 * devpriv->stc_writew(dev, 0x0000, RTSI_Board_Register);
5510 static int ni_rtsi_insn_bits(struct comedi_device
*dev
,
5511 struct comedi_subdevice
*s
,
5512 struct comedi_insn
*insn
, unsigned int *data
)
5519 /* Find best multiplier/divider to try and get the PLL running at 80 MHz
5520 * given an arbitrary frequency input clock */
5521 static int ni_mseries_get_pll_parameters(unsigned reference_period_ns
,
5522 unsigned *freq_divider
,
5523 unsigned *freq_multiplier
,
5524 unsigned *actual_period_ns
)
5527 unsigned best_div
= 1;
5528 static const unsigned max_div
= 0x10;
5530 unsigned best_mult
= 1;
5531 static const unsigned max_mult
= 0x100;
5532 static const unsigned pico_per_nano
= 1000;
5534 const unsigned reference_picosec
= reference_period_ns
* pico_per_nano
;
5535 /* m-series wants the phased-locked loop to output 80MHz, which is divided by 4 to
5536 * 20 MHz for most timing clocks */
5537 static const unsigned target_picosec
= 12500;
5538 static const unsigned fudge_factor_80_to_20Mhz
= 4;
5539 int best_period_picosec
= 0;
5540 for (div
= 1; div
<= max_div
; ++div
) {
5541 for (mult
= 1; mult
<= max_mult
; ++mult
) {
5542 unsigned new_period_ps
=
5543 (reference_picosec
* div
) / mult
;
5544 if (abs(new_period_ps
- target_picosec
) <
5545 abs(best_period_picosec
- target_picosec
)) {
5546 best_period_picosec
= new_period_ps
;
5552 if (best_period_picosec
== 0) {
5553 printk("%s: bug, failed to find pll parameters\n", __func__
);
5556 *freq_divider
= best_div
;
5557 *freq_multiplier
= best_mult
;
5559 (best_period_picosec
* fudge_factor_80_to_20Mhz
+
5560 (pico_per_nano
/ 2)) / pico_per_nano
;
5564 static inline unsigned num_configurable_rtsi_channels(struct comedi_device
*dev
)
5566 const struct ni_board_struct
*board
= comedi_board(dev
);
5568 if (board
->reg_type
& ni_reg_m_series_mask
)
5574 static int ni_mseries_set_pll_master_clock(struct comedi_device
*dev
,
5575 unsigned source
, unsigned period_ns
)
5577 struct ni_private
*devpriv
= dev
->private;
5578 static const unsigned min_period_ns
= 50;
5579 static const unsigned max_period_ns
= 1000;
5580 static const unsigned timeout
= 1000;
5581 unsigned pll_control_bits
;
5582 unsigned freq_divider
;
5583 unsigned freq_multiplier
;
5587 if (source
== NI_MIO_PLL_PXI10_CLOCK
)
5589 /* these limits are somewhat arbitrary, but NI advertises 1 to 20MHz range so we'll use that */
5590 if (period_ns
< min_period_ns
|| period_ns
> max_period_ns
) {
5592 ("%s: you must specify an input clock frequency between %i and %i nanosec "
5593 "for the phased-lock loop.\n", __func__
,
5594 min_period_ns
, max_period_ns
);
5597 devpriv
->rtsi_trig_direction_reg
&= ~Use_RTSI_Clock_Bit
;
5598 devpriv
->stc_writew(dev
, devpriv
->rtsi_trig_direction_reg
,
5599 RTSI_Trig_Direction_Register
);
5601 MSeries_PLL_Enable_Bit
| MSeries_PLL_VCO_Mode_75_150MHz_Bits
;
5602 devpriv
->clock_and_fout2
|=
5603 MSeries_Timebase1_Select_Bit
| MSeries_Timebase3_Select_Bit
;
5604 devpriv
->clock_and_fout2
&= ~MSeries_PLL_In_Source_Select_Mask
;
5606 case NI_MIO_PLL_PXI_STAR_TRIGGER_CLOCK
:
5607 devpriv
->clock_and_fout2
|=
5608 MSeries_PLL_In_Source_Select_Star_Trigger_Bits
;
5609 retval
= ni_mseries_get_pll_parameters(period_ns
, &freq_divider
,
5611 &devpriv
->clock_ns
);
5615 case NI_MIO_PLL_PXI10_CLOCK
:
5616 /* pxi clock is 10MHz */
5617 devpriv
->clock_and_fout2
|=
5618 MSeries_PLL_In_Source_Select_PXI_Clock10
;
5619 retval
= ni_mseries_get_pll_parameters(period_ns
, &freq_divider
,
5621 &devpriv
->clock_ns
);
5627 unsigned rtsi_channel
;
5628 static const unsigned max_rtsi_channel
= 7;
5629 for (rtsi_channel
= 0; rtsi_channel
<= max_rtsi_channel
;
5632 NI_MIO_PLL_RTSI_CLOCK(rtsi_channel
)) {
5633 devpriv
->clock_and_fout2
|=
5634 MSeries_PLL_In_Source_Select_RTSI_Bits
5639 if (rtsi_channel
> max_rtsi_channel
)
5641 retval
= ni_mseries_get_pll_parameters(period_ns
,
5651 ni_writew(devpriv
->clock_and_fout2
, M_Offset_Clock_and_Fout2
);
5653 MSeries_PLL_Divisor_Bits(freq_divider
) |
5654 MSeries_PLL_Multiplier_Bits(freq_multiplier
);
5656 /* printk("using divider=%i, multiplier=%i for PLL. pll_control_bits = 0x%x\n",
5657 * freq_divider, freq_multiplier, pll_control_bits); */
5658 /* printk("clock_ns=%d\n", devpriv->clock_ns); */
5659 ni_writew(pll_control_bits
, M_Offset_PLL_Control
);
5660 devpriv
->clock_source
= source
;
5661 /* it seems to typically take a few hundred microseconds for PLL to lock */
5662 for (i
= 0; i
< timeout
; ++i
) {
5663 if (ni_readw(M_Offset_PLL_Status
) & MSeries_PLL_Locked_Bit
) {
5670 ("%s: timed out waiting for PLL to lock to reference clock source %i with period %i ns.\n",
5671 __func__
, source
, period_ns
);
5677 static int ni_set_master_clock(struct comedi_device
*dev
, unsigned source
,
5680 const struct ni_board_struct
*board
= comedi_board(dev
);
5681 struct ni_private
*devpriv
= dev
->private;
5683 if (source
== NI_MIO_INTERNAL_CLOCK
) {
5684 devpriv
->rtsi_trig_direction_reg
&= ~Use_RTSI_Clock_Bit
;
5685 devpriv
->stc_writew(dev
, devpriv
->rtsi_trig_direction_reg
,
5686 RTSI_Trig_Direction_Register
);
5687 devpriv
->clock_ns
= TIMEBASE_1_NS
;
5688 if (board
->reg_type
& ni_reg_m_series_mask
) {
5689 devpriv
->clock_and_fout2
&=
5690 ~(MSeries_Timebase1_Select_Bit
|
5691 MSeries_Timebase3_Select_Bit
);
5692 ni_writew(devpriv
->clock_and_fout2
,
5693 M_Offset_Clock_and_Fout2
);
5694 ni_writew(0, M_Offset_PLL_Control
);
5696 devpriv
->clock_source
= source
;
5698 if (board
->reg_type
& ni_reg_m_series_mask
) {
5699 return ni_mseries_set_pll_master_clock(dev
, source
,
5702 if (source
== NI_MIO_RTSI_CLOCK
) {
5703 devpriv
->rtsi_trig_direction_reg
|=
5705 devpriv
->stc_writew(dev
,
5707 rtsi_trig_direction_reg
,
5708 RTSI_Trig_Direction_Register
);
5709 if (period_ns
== 0) {
5711 ("%s: we don't handle an unspecified clock period correctly yet, returning error.\n",
5715 devpriv
->clock_ns
= period_ns
;
5717 devpriv
->clock_source
= source
;
5725 static int ni_valid_rtsi_output_source(struct comedi_device
*dev
, unsigned chan
,
5728 const struct ni_board_struct
*board
= comedi_board(dev
);
5730 if (chan
>= num_configurable_rtsi_channels(dev
)) {
5731 if (chan
== old_RTSI_clock_channel
) {
5732 if (source
== NI_RTSI_OUTPUT_RTSI_OSC
)
5736 ("%s: invalid source for channel=%i, channel %i is always the RTSI clock for pre-m-series boards.\n",
5737 __func__
, chan
, old_RTSI_clock_channel
);
5744 case NI_RTSI_OUTPUT_ADR_START1
:
5745 case NI_RTSI_OUTPUT_ADR_START2
:
5746 case NI_RTSI_OUTPUT_SCLKG
:
5747 case NI_RTSI_OUTPUT_DACUPDN
:
5748 case NI_RTSI_OUTPUT_DA_START1
:
5749 case NI_RTSI_OUTPUT_G_SRC0
:
5750 case NI_RTSI_OUTPUT_G_GATE0
:
5751 case NI_RTSI_OUTPUT_RGOUT0
:
5752 case NI_RTSI_OUTPUT_RTSI_BRD_0
:
5755 case NI_RTSI_OUTPUT_RTSI_OSC
:
5756 if (board
->reg_type
& ni_reg_m_series_mask
)
5767 static int ni_set_rtsi_routing(struct comedi_device
*dev
, unsigned chan
,
5770 struct ni_private
*devpriv
= dev
->private;
5772 if (ni_valid_rtsi_output_source(dev
, chan
, source
) == 0)
5775 devpriv
->rtsi_trig_a_output_reg
&= ~RTSI_Trig_Output_Mask(chan
);
5776 devpriv
->rtsi_trig_a_output_reg
|=
5777 RTSI_Trig_Output_Bits(chan
, source
);
5778 devpriv
->stc_writew(dev
, devpriv
->rtsi_trig_a_output_reg
,
5779 RTSI_Trig_A_Output_Register
);
5780 } else if (chan
< 8) {
5781 devpriv
->rtsi_trig_b_output_reg
&= ~RTSI_Trig_Output_Mask(chan
);
5782 devpriv
->rtsi_trig_b_output_reg
|=
5783 RTSI_Trig_Output_Bits(chan
, source
);
5784 devpriv
->stc_writew(dev
, devpriv
->rtsi_trig_b_output_reg
,
5785 RTSI_Trig_B_Output_Register
);
5790 static unsigned ni_get_rtsi_routing(struct comedi_device
*dev
, unsigned chan
)
5792 struct ni_private
*devpriv
= dev
->private;
5795 return RTSI_Trig_Output_Source(chan
,
5796 devpriv
->rtsi_trig_a_output_reg
);
5797 } else if (chan
< num_configurable_rtsi_channels(dev
)) {
5798 return RTSI_Trig_Output_Source(chan
,
5799 devpriv
->rtsi_trig_b_output_reg
);
5801 if (chan
== old_RTSI_clock_channel
)
5802 return NI_RTSI_OUTPUT_RTSI_OSC
;
5803 printk("%s: bug! should never get here?\n", __func__
);
5808 static int ni_rtsi_insn_config(struct comedi_device
*dev
,
5809 struct comedi_subdevice
*s
,
5810 struct comedi_insn
*insn
, unsigned int *data
)
5812 const struct ni_board_struct
*board
= comedi_board(dev
);
5813 struct ni_private
*devpriv
= dev
->private;
5814 unsigned int chan
= CR_CHAN(insn
->chanspec
);
5817 case INSN_CONFIG_DIO_OUTPUT
:
5818 if (chan
< num_configurable_rtsi_channels(dev
)) {
5819 devpriv
->rtsi_trig_direction_reg
|=
5820 RTSI_Output_Bit(chan
,
5821 (board
->reg_type
& ni_reg_m_series_mask
) != 0);
5822 } else if (chan
== old_RTSI_clock_channel
) {
5823 devpriv
->rtsi_trig_direction_reg
|=
5824 Drive_RTSI_Clock_Bit
;
5826 devpriv
->stc_writew(dev
, devpriv
->rtsi_trig_direction_reg
,
5827 RTSI_Trig_Direction_Register
);
5829 case INSN_CONFIG_DIO_INPUT
:
5830 if (chan
< num_configurable_rtsi_channels(dev
)) {
5831 devpriv
->rtsi_trig_direction_reg
&=
5832 ~RTSI_Output_Bit(chan
,
5833 (board
->reg_type
& ni_reg_m_series_mask
) != 0);
5834 } else if (chan
== old_RTSI_clock_channel
) {
5835 devpriv
->rtsi_trig_direction_reg
&=
5836 ~Drive_RTSI_Clock_Bit
;
5838 devpriv
->stc_writew(dev
, devpriv
->rtsi_trig_direction_reg
,
5839 RTSI_Trig_Direction_Register
);
5841 case INSN_CONFIG_DIO_QUERY
:
5842 if (chan
< num_configurable_rtsi_channels(dev
)) {
5844 (devpriv
->rtsi_trig_direction_reg
&
5845 RTSI_Output_Bit(chan
,
5846 (board
->reg_type
& ni_reg_m_series_mask
) != 0))
5847 ? INSN_CONFIG_DIO_OUTPUT
5848 : INSN_CONFIG_DIO_INPUT
;
5849 } else if (chan
== old_RTSI_clock_channel
) {
5851 (devpriv
->rtsi_trig_direction_reg
&
5852 Drive_RTSI_Clock_Bit
)
5853 ? INSN_CONFIG_DIO_OUTPUT
: INSN_CONFIG_DIO_INPUT
;
5857 case INSN_CONFIG_SET_CLOCK_SRC
:
5858 return ni_set_master_clock(dev
, data
[1], data
[2]);
5860 case INSN_CONFIG_GET_CLOCK_SRC
:
5861 data
[1] = devpriv
->clock_source
;
5862 data
[2] = devpriv
->clock_ns
;
5865 case INSN_CONFIG_SET_ROUTING
:
5866 return ni_set_rtsi_routing(dev
, chan
, data
[1]);
5868 case INSN_CONFIG_GET_ROUTING
:
5869 data
[1] = ni_get_rtsi_routing(dev
, chan
);
5879 static int cs5529_wait_for_idle(struct comedi_device
*dev
)
5881 unsigned short status
;
5882 const int timeout
= HZ
;
5885 for (i
= 0; i
< timeout
; i
++) {
5886 status
= ni_ao_win_inw(dev
, CAL_ADC_Status_67xx
);
5887 if ((status
& CSS_ADC_BUSY
) == 0) {
5890 set_current_state(TASK_INTERRUPTIBLE
);
5891 if (schedule_timeout(1)) {
5895 /* printk("looped %i times waiting for idle\n", i); */
5897 printk("%s: %s: timeout\n", __FILE__
, __func__
);
5903 static void cs5529_command(struct comedi_device
*dev
, unsigned short value
)
5905 static const int timeout
= 100;
5908 ni_ao_win_outw(dev
, value
, CAL_ADC_Command_67xx
);
5909 /* give time for command to start being serially clocked into cs5529.
5910 * this insures that the CSS_ADC_BUSY bit will get properly
5911 * set before we exit this function.
5913 for (i
= 0; i
< timeout
; i
++) {
5914 if ((ni_ao_win_inw(dev
, CAL_ADC_Status_67xx
) & CSS_ADC_BUSY
))
5918 /* printk("looped %i times writing command to cs5529\n", i); */
5920 comedi_error(dev
, "possible problem - never saw adc go busy?");
5924 /* write to cs5529 register */
5925 static void cs5529_config_write(struct comedi_device
*dev
, unsigned int value
,
5926 unsigned int reg_select_bits
)
5928 ni_ao_win_outw(dev
, ((value
>> 16) & 0xff),
5929 CAL_ADC_Config_Data_High_Word_67xx
);
5930 ni_ao_win_outw(dev
, (value
& 0xffff),
5931 CAL_ADC_Config_Data_Low_Word_67xx
);
5932 reg_select_bits
&= CSCMD_REGISTER_SELECT_MASK
;
5933 cs5529_command(dev
, CSCMD_COMMAND
| reg_select_bits
);
5934 if (cs5529_wait_for_idle(dev
))
5935 comedi_error(dev
, "time or signal in cs5529_config_write()");
5938 #ifdef NI_CS5529_DEBUG
5939 /* read from cs5529 register */
5940 static unsigned int cs5529_config_read(struct comedi_device
*dev
,
5941 unsigned int reg_select_bits
)
5945 reg_select_bits
&= CSCMD_REGISTER_SELECT_MASK
;
5946 cs5529_command(dev
, CSCMD_COMMAND
| CSCMD_READ
| reg_select_bits
);
5947 if (cs5529_wait_for_idle(dev
))
5948 comedi_error(dev
, "timeout or signal in cs5529_config_read()");
5949 value
= (ni_ao_win_inw(dev
,
5950 CAL_ADC_Config_Data_High_Word_67xx
) << 16) &
5952 value
|= ni_ao_win_inw(dev
, CAL_ADC_Config_Data_Low_Word_67xx
) & 0xffff;
5957 static int cs5529_do_conversion(struct comedi_device
*dev
, unsigned short *data
)
5960 unsigned short status
;
5962 cs5529_command(dev
, CSCMD_COMMAND
| CSCMD_SINGLE_CONVERSION
);
5963 retval
= cs5529_wait_for_idle(dev
);
5966 "timeout or signal in cs5529_do_conversion()");
5969 status
= ni_ao_win_inw(dev
, CAL_ADC_Status_67xx
);
5970 if (status
& CSS_OSC_DETECT
) {
5972 ("ni_mio_common: cs5529 conversion error, status CSS_OSC_DETECT\n");
5975 if (status
& CSS_OVERRANGE
) {
5977 ("ni_mio_common: cs5529 conversion error, overrange (ignoring)\n");
5980 *data
= ni_ao_win_inw(dev
, CAL_ADC_Data_67xx
);
5981 /* cs5529 returns 16 bit signed data in bipolar mode */
5987 static int cs5529_ai_insn_read(struct comedi_device
*dev
,
5988 struct comedi_subdevice
*s
,
5989 struct comedi_insn
*insn
, unsigned int *data
)
5992 unsigned short sample
;
5993 unsigned int channel_select
;
5994 const unsigned int INTERNAL_REF
= 0x1000;
5996 /* Set calibration adc source. Docs lie, reference select bits 8 to 11
5997 * do nothing. bit 12 seems to chooses internal reference voltage, bit
5998 * 13 causes the adc input to go overrange (maybe reads external reference?) */
5999 if (insn
->chanspec
& CR_ALT_SOURCE
)
6000 channel_select
= INTERNAL_REF
;
6002 channel_select
= CR_CHAN(insn
->chanspec
);
6003 ni_ao_win_outw(dev
, channel_select
, AO_Calibration_Channel_Select_67xx
);
6005 for (n
= 0; n
< insn
->n
; n
++) {
6006 retval
= cs5529_do_conversion(dev
, &sample
);
6014 static int init_cs5529(struct comedi_device
*dev
)
6016 unsigned int config_bits
=
6017 CSCFG_PORT_MODE
| CSCFG_WORD_RATE_2180_CYCLES
;
6020 /* do self-calibration */
6021 cs5529_config_write(dev
, config_bits
| CSCFG_SELF_CAL_OFFSET_GAIN
,
6022 CSCMD_CONFIG_REGISTER
);
6023 /* need to force a conversion for calibration to run */
6024 cs5529_do_conversion(dev
, NULL
);
6026 /* force gain calibration to 1 */
6027 cs5529_config_write(dev
, 0x400000, CSCMD_GAIN_REGISTER
);
6028 cs5529_config_write(dev
, config_bits
| CSCFG_SELF_CAL_OFFSET
,
6029 CSCMD_CONFIG_REGISTER
);
6030 if (cs5529_wait_for_idle(dev
))
6031 comedi_error(dev
, "timeout or signal in init_cs5529()\n");
6033 #ifdef NI_CS5529_DEBUG
6034 printk("config: 0x%x\n", cs5529_config_read(dev
,
6035 CSCMD_CONFIG_REGISTER
));
6036 printk("gain: 0x%x\n", cs5529_config_read(dev
, CSCMD_GAIN_REGISTER
));
6037 printk("offset: 0x%x\n", cs5529_config_read(dev
,
6038 CSCMD_OFFSET_REGISTER
));