1 // SPDX-License-Identifier: GPL-2.0+
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>
11 * This file is meant to be included by another file, e.g.,
12 * ni_atmio.c or ni_pcimio.c.
14 * Interrupt support originally added by Truxton Fulton <trux@truxton.com>
16 * References (ftp://ftp.natinst.com/support/manuals):
17 * 340747b.pdf AT-MIO E series Register Level Programmer Manual
18 * 341079b.pdf PCI E Series RLPM
19 * 340934b.pdf DAQ-STC reference manual
21 * 67xx and 611x registers (ftp://ftp.ni.com/support/daq/mhddk/documentation/)
25 * Other possibly relevant info:
26 * 320517c.pdf User manual (obsolete)
27 * 320517f.pdf User manual (new)
29 * 320906c.pdf maximum signal ratings
30 * 321066a.pdf about 16x
31 * 321791a.pdf discontinuation of at-mio-16e-10 rev. c
32 * 321808a.pdf about at-mio-16e-10 rev P
33 * 321837a.pdf discontinuation of at-mio-16de-10 rev d
34 * 321838a.pdf about at-mio-16de-10 rev N
37 * - the interrupt routine needs to be cleaned up
39 * 2006-02-07: S-Series PCI-6143: Support has been added but is not
40 * fully tested as yet. Terry Barnaby, BEAM Ltd.
43 #include <linux/interrupt.h>
44 #include <linux/sched.h>
45 #include <linux/delay.h>
50 #define NI_TIMEOUT 1000
52 /* Note: this table must match the ai_gain_* definitions */
53 static const short ni_gainlkup[][16] = {
54 [ai_gain_16] = {0, 1, 2, 3, 4, 5, 6, 7,
55 0x100, 0x101, 0x102, 0x103, 0x104, 0x105, 0x106, 0x107},
56 [ai_gain_8] = {1, 2, 4, 7, 0x101, 0x102, 0x104, 0x107},
57 [ai_gain_14] = {1, 2, 3, 4, 5, 6, 7,
58 0x101, 0x102, 0x103, 0x104, 0x105, 0x106, 0x107},
59 [ai_gain_4] = {0, 1, 4, 7},
60 [ai_gain_611x] = {0x00a, 0x00b, 0x001, 0x002,
61 0x003, 0x004, 0x005, 0x006},
62 [ai_gain_622x] = {0, 1, 4, 5},
63 [ai_gain_628x] = {1, 2, 3, 4, 5, 6, 7},
64 [ai_gain_6143] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
67 static const struct comedi_lrange range_ni_E_ai = {
88 static const struct comedi_lrange range_ni_E_ai_limited = {
101 static const struct comedi_lrange range_ni_E_ai_limited14 = {
120 static const struct comedi_lrange range_ni_E_ai_bipolar4 = {
129 static const struct comedi_lrange range_ni_E_ai_611x = {
142 static const struct comedi_lrange range_ni_M_ai_622x = {
151 static const struct comedi_lrange range_ni_M_ai_628x = {
163 static const struct comedi_lrange range_ni_E_ao_ext = {
172 static const struct comedi_lrange *const ni_range_lkup[] = {
173 [ai_gain_16] = &range_ni_E_ai,
174 [ai_gain_8] = &range_ni_E_ai_limited,
175 [ai_gain_14] = &range_ni_E_ai_limited14,
176 [ai_gain_4] = &range_ni_E_ai_bipolar4,
177 [ai_gain_611x] = &range_ni_E_ai_611x,
178 [ai_gain_622x] = &range_ni_M_ai_622x,
179 [ai_gain_628x] = &range_ni_M_ai_628x,
180 [ai_gain_6143] = &range_bipolar5
185 AIMODE_HALF_FULL = 1,
190 enum ni_common_subdevices {
196 NI_CALIBRATION_SUBDEV,
199 NI_CS5529_CALIBRATION_SUBDEV,
208 #define NI_GPCT_SUBDEV(x) (NI_GPCT0_SUBDEV + (x))
210 enum timebase_nanoseconds {
212 TIMEBASE_2_NS = 10000
215 #define SERIAL_DISABLED 0
216 #define SERIAL_600NS 600
217 #define SERIAL_1_2US 1200
218 #define SERIAL_10US 10000
220 static const int num_adc_stages_611x = 3;
222 static void ni_writel(struct comedi_device *dev, unsigned int data, int reg)
225 writel(data, dev->mmio + reg);
227 outl(data, dev->iobase + reg);
230 static void ni_writew(struct comedi_device *dev, unsigned int data, int reg)
233 writew(data, dev->mmio + reg);
235 outw(data, dev->iobase + reg);
238 static void ni_writeb(struct comedi_device *dev, unsigned int data, int reg)
241 writeb(data, dev->mmio + reg);
243 outb(data, dev->iobase + reg);
246 static unsigned int ni_readl(struct comedi_device *dev, int reg)
249 return readl(dev->mmio + reg);
251 return inl(dev->iobase + reg);
254 static unsigned int ni_readw(struct comedi_device *dev, int reg)
257 return readw(dev->mmio + reg);
259 return inw(dev->iobase + reg);
262 static unsigned int ni_readb(struct comedi_device *dev, int reg)
265 return readb(dev->mmio + reg);
267 return inb(dev->iobase + reg);
271 * We automatically take advantage of STC registers that can be
272 * read/written directly in the I/O space of the board.
274 * The AT-MIO and DAQCard devices map the low 8 STC registers to
277 * Most PCIMIO devices also map the low 8 STC registers but the
278 * 611x devices map the read registers to iobase+(addr-1)*2.
279 * For now non-windowed STC access is disabled if a PCIMIO device
280 * is detected (devpriv->mite has been initialized).
282 * The M series devices do not used windowed registers for the
283 * STC registers. The functions below handle the mapping of the
284 * windowed STC registers to the m series register offsets.
288 unsigned int mio_reg;
292 static const struct mio_regmap m_series_stc_write_regmap[] = {
293 [NISTC_INTA_ACK_REG] = { 0x104, 2 },
294 [NISTC_INTB_ACK_REG] = { 0x106, 2 },
295 [NISTC_AI_CMD2_REG] = { 0x108, 2 },
296 [NISTC_AO_CMD2_REG] = { 0x10a, 2 },
297 [NISTC_G0_CMD_REG] = { 0x10c, 2 },
298 [NISTC_G1_CMD_REG] = { 0x10e, 2 },
299 [NISTC_AI_CMD1_REG] = { 0x110, 2 },
300 [NISTC_AO_CMD1_REG] = { 0x112, 2 },
302 * NISTC_DIO_OUT_REG maps to:
303 * { NI_M_DIO_REG, 4 } and { NI_M_SCXI_SER_DO_REG, 1 }
305 [NISTC_DIO_OUT_REG] = { 0, 0 }, /* DOES NOT MAP CLEANLY */
306 [NISTC_DIO_CTRL_REG] = { 0, 0 }, /* DOES NOT MAP CLEANLY */
307 [NISTC_AI_MODE1_REG] = { 0x118, 2 },
308 [NISTC_AI_MODE2_REG] = { 0x11a, 2 },
309 [NISTC_AI_SI_LOADA_REG] = { 0x11c, 4 },
310 [NISTC_AI_SI_LOADB_REG] = { 0x120, 4 },
311 [NISTC_AI_SC_LOADA_REG] = { 0x124, 4 },
312 [NISTC_AI_SC_LOADB_REG] = { 0x128, 4 },
313 [NISTC_AI_SI2_LOADA_REG] = { 0x12c, 4 },
314 [NISTC_AI_SI2_LOADB_REG] = { 0x130, 4 },
315 [NISTC_G0_MODE_REG] = { 0x134, 2 },
316 [NISTC_G1_MODE_REG] = { 0x136, 2 },
317 [NISTC_G0_LOADA_REG] = { 0x138, 4 },
318 [NISTC_G0_LOADB_REG] = { 0x13c, 4 },
319 [NISTC_G1_LOADA_REG] = { 0x140, 4 },
320 [NISTC_G1_LOADB_REG] = { 0x144, 4 },
321 [NISTC_G0_INPUT_SEL_REG] = { 0x148, 2 },
322 [NISTC_G1_INPUT_SEL_REG] = { 0x14a, 2 },
323 [NISTC_AO_MODE1_REG] = { 0x14c, 2 },
324 [NISTC_AO_MODE2_REG] = { 0x14e, 2 },
325 [NISTC_AO_UI_LOADA_REG] = { 0x150, 4 },
326 [NISTC_AO_UI_LOADB_REG] = { 0x154, 4 },
327 [NISTC_AO_BC_LOADA_REG] = { 0x158, 4 },
328 [NISTC_AO_BC_LOADB_REG] = { 0x15c, 4 },
329 [NISTC_AO_UC_LOADA_REG] = { 0x160, 4 },
330 [NISTC_AO_UC_LOADB_REG] = { 0x164, 4 },
331 [NISTC_CLK_FOUT_REG] = { 0x170, 2 },
332 [NISTC_IO_BIDIR_PIN_REG] = { 0x172, 2 },
333 [NISTC_RTSI_TRIG_DIR_REG] = { 0x174, 2 },
334 [NISTC_INT_CTRL_REG] = { 0x176, 2 },
335 [NISTC_AI_OUT_CTRL_REG] = { 0x178, 2 },
336 [NISTC_ATRIG_ETC_REG] = { 0x17a, 2 },
337 [NISTC_AI_START_STOP_REG] = { 0x17c, 2 },
338 [NISTC_AI_TRIG_SEL_REG] = { 0x17e, 2 },
339 [NISTC_AI_DIV_LOADA_REG] = { 0x180, 4 },
340 [NISTC_AO_START_SEL_REG] = { 0x184, 2 },
341 [NISTC_AO_TRIG_SEL_REG] = { 0x186, 2 },
342 [NISTC_G0_AUTOINC_REG] = { 0x188, 2 },
343 [NISTC_G1_AUTOINC_REG] = { 0x18a, 2 },
344 [NISTC_AO_MODE3_REG] = { 0x18c, 2 },
345 [NISTC_RESET_REG] = { 0x190, 2 },
346 [NISTC_INTA_ENA_REG] = { 0x192, 2 },
347 [NISTC_INTA2_ENA_REG] = { 0, 0 }, /* E-Series only */
348 [NISTC_INTB_ENA_REG] = { 0x196, 2 },
349 [NISTC_INTB2_ENA_REG] = { 0, 0 }, /* E-Series only */
350 [NISTC_AI_PERSONAL_REG] = { 0x19a, 2 },
351 [NISTC_AO_PERSONAL_REG] = { 0x19c, 2 },
352 [NISTC_RTSI_TRIGA_OUT_REG] = { 0x19e, 2 },
353 [NISTC_RTSI_TRIGB_OUT_REG] = { 0x1a0, 2 },
354 /* doc for following line: mhddk/nimseries/ChipObjects/tMSeries.h */
355 [NISTC_RTSI_BOARD_REG] = { 0x1a2, 2 },
356 [NISTC_CFG_MEM_CLR_REG] = { 0x1a4, 2 },
357 [NISTC_ADC_FIFO_CLR_REG] = { 0x1a6, 2 },
358 [NISTC_DAC_FIFO_CLR_REG] = { 0x1a8, 2 },
359 [NISTC_AO_OUT_CTRL_REG] = { 0x1ac, 2 },
360 [NISTC_AI_MODE3_REG] = { 0x1ae, 2 },
363 static void m_series_stc_write(struct comedi_device *dev,
364 unsigned int data, unsigned int reg)
366 const struct mio_regmap *regmap;
368 if (reg < ARRAY_SIZE(m_series_stc_write_regmap)) {
369 regmap = &m_series_stc_write_regmap[reg];
371 dev_warn(dev->class_dev, "%s: unhandled register=0x%x\n",
376 switch (regmap->size) {
378 ni_writel(dev, data, regmap->mio_reg);
381 ni_writew(dev, data, regmap->mio_reg);
384 dev_warn(dev->class_dev, "%s: unmapped register=0x%x\n",
390 static const struct mio_regmap m_series_stc_read_regmap[] = {
391 [NISTC_AI_STATUS1_REG] = { 0x104, 2 },
392 [NISTC_AO_STATUS1_REG] = { 0x106, 2 },
393 [NISTC_G01_STATUS_REG] = { 0x108, 2 },
394 [NISTC_AI_STATUS2_REG] = { 0, 0 }, /* Unknown */
395 [NISTC_AO_STATUS2_REG] = { 0x10c, 2 },
396 [NISTC_DIO_IN_REG] = { 0, 0 }, /* Unknown */
397 [NISTC_G0_HW_SAVE_REG] = { 0x110, 4 },
398 [NISTC_G1_HW_SAVE_REG] = { 0x114, 4 },
399 [NISTC_G0_SAVE_REG] = { 0x118, 4 },
400 [NISTC_G1_SAVE_REG] = { 0x11c, 4 },
401 [NISTC_AO_UI_SAVE_REG] = { 0x120, 4 },
402 [NISTC_AO_BC_SAVE_REG] = { 0x124, 4 },
403 [NISTC_AO_UC_SAVE_REG] = { 0x128, 4 },
404 [NISTC_STATUS1_REG] = { 0x136, 2 },
405 [NISTC_DIO_SERIAL_IN_REG] = { 0x009, 1 },
406 [NISTC_STATUS2_REG] = { 0x13a, 2 },
407 [NISTC_AI_SI_SAVE_REG] = { 0x180, 4 },
408 [NISTC_AI_SC_SAVE_REG] = { 0x184, 4 },
411 static unsigned int m_series_stc_read(struct comedi_device *dev,
414 const struct mio_regmap *regmap;
416 if (reg < ARRAY_SIZE(m_series_stc_read_regmap)) {
417 regmap = &m_series_stc_read_regmap[reg];
419 dev_warn(dev->class_dev, "%s: unhandled register=0x%x\n",
424 switch (regmap->size) {
426 return ni_readl(dev, regmap->mio_reg);
428 return ni_readw(dev, regmap->mio_reg);
430 return ni_readb(dev, regmap->mio_reg);
432 dev_warn(dev->class_dev, "%s: unmapped register=0x%x\n",
438 static void ni_stc_writew(struct comedi_device *dev,
439 unsigned int data, int reg)
441 struct ni_private *devpriv = dev->private;
444 if (devpriv->is_m_series) {
445 m_series_stc_write(dev, data, reg);
447 spin_lock_irqsave(&devpriv->window_lock, flags);
448 if (!devpriv->mite && reg < 8) {
449 ni_writew(dev, data, reg * 2);
451 ni_writew(dev, reg, NI_E_STC_WINDOW_ADDR_REG);
452 ni_writew(dev, data, NI_E_STC_WINDOW_DATA_REG);
454 spin_unlock_irqrestore(&devpriv->window_lock, flags);
458 static void ni_stc_writel(struct comedi_device *dev,
459 unsigned int data, int reg)
461 struct ni_private *devpriv = dev->private;
463 if (devpriv->is_m_series) {
464 m_series_stc_write(dev, data, reg);
466 ni_stc_writew(dev, data >> 16, reg);
467 ni_stc_writew(dev, data & 0xffff, reg + 1);
471 static unsigned int ni_stc_readw(struct comedi_device *dev, int reg)
473 struct ni_private *devpriv = dev->private;
477 if (devpriv->is_m_series) {
478 val = m_series_stc_read(dev, reg);
480 spin_lock_irqsave(&devpriv->window_lock, flags);
481 if (!devpriv->mite && reg < 8) {
482 val = ni_readw(dev, reg * 2);
484 ni_writew(dev, reg, NI_E_STC_WINDOW_ADDR_REG);
485 val = ni_readw(dev, NI_E_STC_WINDOW_DATA_REG);
487 spin_unlock_irqrestore(&devpriv->window_lock, flags);
492 static unsigned int ni_stc_readl(struct comedi_device *dev, int reg)
494 struct ni_private *devpriv = dev->private;
497 if (devpriv->is_m_series) {
498 val = m_series_stc_read(dev, reg);
500 val = ni_stc_readw(dev, reg) << 16;
501 val |= ni_stc_readw(dev, reg + 1);
506 static inline void ni_set_bitfield(struct comedi_device *dev, int reg,
507 unsigned int bit_mask,
508 unsigned int bit_values)
510 struct ni_private *devpriv = dev->private;
513 spin_lock_irqsave(&devpriv->soft_reg_copy_lock, flags);
515 case NISTC_INTA_ENA_REG:
516 devpriv->int_a_enable_reg &= ~bit_mask;
517 devpriv->int_a_enable_reg |= bit_values & bit_mask;
518 ni_stc_writew(dev, devpriv->int_a_enable_reg, reg);
520 case NISTC_INTB_ENA_REG:
521 devpriv->int_b_enable_reg &= ~bit_mask;
522 devpriv->int_b_enable_reg |= bit_values & bit_mask;
523 ni_stc_writew(dev, devpriv->int_b_enable_reg, reg);
525 case NISTC_IO_BIDIR_PIN_REG:
526 devpriv->io_bidirection_pin_reg &= ~bit_mask;
527 devpriv->io_bidirection_pin_reg |= bit_values & bit_mask;
528 ni_stc_writew(dev, devpriv->io_bidirection_pin_reg, reg);
530 case NI_E_DMA_AI_AO_SEL_REG:
531 devpriv->ai_ao_select_reg &= ~bit_mask;
532 devpriv->ai_ao_select_reg |= bit_values & bit_mask;
533 ni_writeb(dev, devpriv->ai_ao_select_reg, reg);
535 case NI_E_DMA_G0_G1_SEL_REG:
536 devpriv->g0_g1_select_reg &= ~bit_mask;
537 devpriv->g0_g1_select_reg |= bit_values & bit_mask;
538 ni_writeb(dev, devpriv->g0_g1_select_reg, reg);
540 case NI_M_CDIO_DMA_SEL_REG:
541 devpriv->cdio_dma_select_reg &= ~bit_mask;
542 devpriv->cdio_dma_select_reg |= bit_values & bit_mask;
543 ni_writeb(dev, devpriv->cdio_dma_select_reg, reg);
546 dev_err(dev->class_dev, "called with invalid register %d\n",
550 spin_unlock_irqrestore(&devpriv->soft_reg_copy_lock, flags);
555 /* selects the MITE channel to use for DMA */
556 #define NI_STC_DMA_CHAN_SEL(x) (((x) < 4) ? BIT(x) : \
558 ((x) == 5) ? 0x5 : 0x0)
560 /* DMA channel setup */
561 static int ni_request_ai_mite_channel(struct comedi_device *dev)
563 struct ni_private *devpriv = dev->private;
564 struct mite_channel *mite_chan;
568 spin_lock_irqsave(&devpriv->mite_channel_lock, flags);
569 mite_chan = mite_request_channel(devpriv->mite, devpriv->ai_mite_ring);
571 spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
572 dev_err(dev->class_dev,
573 "failed to reserve mite dma channel for analog input\n");
576 mite_chan->dir = COMEDI_INPUT;
577 devpriv->ai_mite_chan = mite_chan;
579 bits = NI_STC_DMA_CHAN_SEL(mite_chan->channel);
580 ni_set_bitfield(dev, NI_E_DMA_AI_AO_SEL_REG,
581 NI_E_DMA_AI_SEL_MASK, NI_E_DMA_AI_SEL(bits));
583 spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
587 static int ni_request_ao_mite_channel(struct comedi_device *dev)
589 struct ni_private *devpriv = dev->private;
590 struct mite_channel *mite_chan;
594 spin_lock_irqsave(&devpriv->mite_channel_lock, flags);
595 mite_chan = mite_request_channel(devpriv->mite, devpriv->ao_mite_ring);
597 spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
598 dev_err(dev->class_dev,
599 "failed to reserve mite dma channel for analog output\n");
602 mite_chan->dir = COMEDI_OUTPUT;
603 devpriv->ao_mite_chan = mite_chan;
605 bits = NI_STC_DMA_CHAN_SEL(mite_chan->channel);
606 ni_set_bitfield(dev, NI_E_DMA_AI_AO_SEL_REG,
607 NI_E_DMA_AO_SEL_MASK, NI_E_DMA_AO_SEL(bits));
609 spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
613 static int ni_request_gpct_mite_channel(struct comedi_device *dev,
614 unsigned int gpct_index,
615 enum comedi_io_direction direction)
617 struct ni_private *devpriv = dev->private;
618 struct ni_gpct *counter = &devpriv->counter_dev->counters[gpct_index];
619 struct mite_channel *mite_chan;
623 spin_lock_irqsave(&devpriv->mite_channel_lock, flags);
624 mite_chan = mite_request_channel(devpriv->mite,
625 devpriv->gpct_mite_ring[gpct_index]);
627 spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
628 dev_err(dev->class_dev,
629 "failed to reserve mite dma channel for counter\n");
632 mite_chan->dir = direction;
633 ni_tio_set_mite_channel(counter, mite_chan);
635 bits = NI_STC_DMA_CHAN_SEL(mite_chan->channel);
636 ni_set_bitfield(dev, NI_E_DMA_G0_G1_SEL_REG,
637 NI_E_DMA_G0_G1_SEL_MASK(gpct_index),
638 NI_E_DMA_G0_G1_SEL(gpct_index, bits));
640 spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
644 static int ni_request_cdo_mite_channel(struct comedi_device *dev)
646 struct ni_private *devpriv = dev->private;
647 struct mite_channel *mite_chan;
651 spin_lock_irqsave(&devpriv->mite_channel_lock, flags);
652 mite_chan = mite_request_channel(devpriv->mite, devpriv->cdo_mite_ring);
654 spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
655 dev_err(dev->class_dev,
656 "failed to reserve mite dma channel for correlated digital output\n");
659 mite_chan->dir = COMEDI_OUTPUT;
660 devpriv->cdo_mite_chan = mite_chan;
663 * XXX just guessing NI_STC_DMA_CHAN_SEL()
664 * returns the right bits, under the assumption the cdio dma
665 * selection works just like ai/ao/gpct.
666 * Definitely works for dma channels 0 and 1.
668 bits = NI_STC_DMA_CHAN_SEL(mite_chan->channel);
669 ni_set_bitfield(dev, NI_M_CDIO_DMA_SEL_REG,
670 NI_M_CDIO_DMA_SEL_CDO_MASK,
671 NI_M_CDIO_DMA_SEL_CDO(bits));
673 spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
678 static void ni_release_ai_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->ai_mite_chan) {
686 ni_set_bitfield(dev, NI_E_DMA_AI_AO_SEL_REG,
687 NI_E_DMA_AI_SEL_MASK, 0);
688 mite_release_channel(devpriv->ai_mite_chan);
689 devpriv->ai_mite_chan = NULL;
691 spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
695 static void ni_release_ao_mite_channel(struct comedi_device *dev)
698 struct ni_private *devpriv = dev->private;
701 spin_lock_irqsave(&devpriv->mite_channel_lock, flags);
702 if (devpriv->ao_mite_chan) {
703 ni_set_bitfield(dev, NI_E_DMA_AI_AO_SEL_REG,
704 NI_E_DMA_AO_SEL_MASK, 0);
705 mite_release_channel(devpriv->ao_mite_chan);
706 devpriv->ao_mite_chan = NULL;
708 spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
713 static void ni_release_gpct_mite_channel(struct comedi_device *dev,
714 unsigned int gpct_index)
716 struct ni_private *devpriv = dev->private;
719 spin_lock_irqsave(&devpriv->mite_channel_lock, flags);
720 if (devpriv->counter_dev->counters[gpct_index].mite_chan) {
721 struct mite_channel *mite_chan =
722 devpriv->counter_dev->counters[gpct_index].mite_chan;
724 ni_set_bitfield(dev, NI_E_DMA_G0_G1_SEL_REG,
725 NI_E_DMA_G0_G1_SEL_MASK(gpct_index), 0);
726 ni_tio_set_mite_channel(&devpriv->counter_dev->counters[gpct_index],
728 mite_release_channel(mite_chan);
730 spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
733 static void ni_release_cdo_mite_channel(struct comedi_device *dev)
735 struct ni_private *devpriv = dev->private;
738 spin_lock_irqsave(&devpriv->mite_channel_lock, flags);
739 if (devpriv->cdo_mite_chan) {
740 ni_set_bitfield(dev, NI_M_CDIO_DMA_SEL_REG,
741 NI_M_CDIO_DMA_SEL_CDO_MASK, 0);
742 mite_release_channel(devpriv->cdo_mite_chan);
743 devpriv->cdo_mite_chan = NULL;
745 spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
748 static void ni_e_series_enable_second_irq(struct comedi_device *dev,
749 unsigned int gpct_index, short enable)
751 struct ni_private *devpriv = dev->private;
752 unsigned int val = 0;
755 if (devpriv->is_m_series || gpct_index > 1)
759 * e-series boards use the second irq signals to generate
760 * dma requests for their counters
762 if (gpct_index == 0) {
763 reg = NISTC_INTA2_ENA_REG;
765 val = NISTC_INTA_ENA_G0_GATE;
767 reg = NISTC_INTB2_ENA_REG;
769 val = NISTC_INTB_ENA_G1_GATE;
771 ni_stc_writew(dev, val, reg);
775 static void ni_clear_ai_fifo(struct comedi_device *dev)
777 struct ni_private *devpriv = dev->private;
778 static const int timeout = 10000;
781 if (devpriv->is_6143) {
782 /* Flush the 6143 data FIFO */
783 ni_writel(dev, 0x10, NI6143_AI_FIFO_CTRL_REG);
784 ni_writel(dev, 0x00, NI6143_AI_FIFO_CTRL_REG);
785 /* Wait for complete */
786 for (i = 0; i < timeout; i++) {
787 if (!(ni_readl(dev, NI6143_AI_FIFO_STATUS_REG) & 0x10))
792 dev_err(dev->class_dev, "FIFO flush timeout\n");
794 ni_stc_writew(dev, 1, NISTC_ADC_FIFO_CLR_REG);
795 if (devpriv->is_625x) {
796 ni_writeb(dev, 0, NI_M_STATIC_AI_CTRL_REG(0));
797 ni_writeb(dev, 1, NI_M_STATIC_AI_CTRL_REG(0));
800 * The NI example code does 3 convert pulses for 625x
801 * boards, But that appears to be wrong in practice.
803 ni_stc_writew(dev, NISTC_AI_CMD1_CONVERT_PULSE,
805 ni_stc_writew(dev, NISTC_AI_CMD1_CONVERT_PULSE,
807 ni_stc_writew(dev, NISTC_AI_CMD1_CONVERT_PULSE,
814 static inline void ni_ao_win_outw(struct comedi_device *dev,
815 unsigned int data, int addr)
817 struct ni_private *devpriv = dev->private;
820 spin_lock_irqsave(&devpriv->window_lock, flags);
821 ni_writew(dev, addr, NI611X_AO_WINDOW_ADDR_REG);
822 ni_writew(dev, data, NI611X_AO_WINDOW_DATA_REG);
823 spin_unlock_irqrestore(&devpriv->window_lock, flags);
826 static inline void ni_ao_win_outl(struct comedi_device *dev,
827 unsigned int data, int addr)
829 struct ni_private *devpriv = dev->private;
832 spin_lock_irqsave(&devpriv->window_lock, flags);
833 ni_writew(dev, addr, NI611X_AO_WINDOW_ADDR_REG);
834 ni_writel(dev, data, NI611X_AO_WINDOW_DATA_REG);
835 spin_unlock_irqrestore(&devpriv->window_lock, flags);
838 static inline unsigned short ni_ao_win_inw(struct comedi_device *dev, int addr)
840 struct ni_private *devpriv = dev->private;
844 spin_lock_irqsave(&devpriv->window_lock, flags);
845 ni_writew(dev, addr, NI611X_AO_WINDOW_ADDR_REG);
846 data = ni_readw(dev, NI611X_AO_WINDOW_DATA_REG);
847 spin_unlock_irqrestore(&devpriv->window_lock, flags);
852 * ni_set_bits( ) allows different parts of the ni_mio_common driver to
853 * share registers (such as Interrupt_A_Register) without interfering with
856 * NOTE: the switch/case statements are optimized out for a constant argument
857 * so this is actually quite fast--- If you must wrap another function around
858 * this make it inline to avoid a large speed penalty.
860 * value should only be 1 or 0.
862 static inline void ni_set_bits(struct comedi_device *dev, int reg,
863 unsigned int bits, unsigned int value)
865 unsigned int bit_values;
871 ni_set_bitfield(dev, reg, bits, bit_values);
875 static void ni_sync_ai_dma(struct comedi_device *dev)
877 struct ni_private *devpriv = dev->private;
878 struct comedi_subdevice *s = dev->read_subdev;
881 spin_lock_irqsave(&devpriv->mite_channel_lock, flags);
882 if (devpriv->ai_mite_chan)
883 mite_sync_dma(devpriv->ai_mite_chan, s);
884 spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
887 static int ni_ai_drain_dma(struct comedi_device *dev)
889 struct ni_private *devpriv = dev->private;
891 static const int timeout = 10000;
895 spin_lock_irqsave(&devpriv->mite_channel_lock, flags);
896 if (devpriv->ai_mite_chan) {
897 for (i = 0; i < timeout; i++) {
898 if ((ni_stc_readw(dev, NISTC_AI_STATUS1_REG) &
899 NISTC_AI_STATUS1_FIFO_E) &&
900 mite_bytes_in_transit(devpriv->ai_mite_chan) == 0)
905 dev_err(dev->class_dev, "timed out\n");
906 dev_err(dev->class_dev,
907 "mite_bytes_in_transit=%i, AI_Status1_Register=0x%x\n",
908 mite_bytes_in_transit(devpriv->ai_mite_chan),
909 ni_stc_readw(dev, NISTC_AI_STATUS1_REG));
913 spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
920 static int ni_ao_wait_for_dma_load(struct comedi_device *dev)
922 static const int timeout = 10000;
925 for (i = 0; i < timeout; i++) {
926 unsigned short b_status;
928 b_status = ni_stc_readw(dev, NISTC_AO_STATUS1_REG);
929 if (b_status & NISTC_AO_STATUS1_FIFO_HF)
932 * If we poll too often, the pci bus activity seems
933 * to slow the dma transfer down.
935 usleep_range(10, 100);
938 dev_err(dev->class_dev, "timed out waiting for dma load\n");
947 static void ni_ao_fifo_load(struct comedi_device *dev,
948 struct comedi_subdevice *s, int n)
950 struct ni_private *devpriv = dev->private;
953 unsigned int packed_data;
955 for (i = 0; i < n; i++) {
956 comedi_buf_read_samples(s, &d, 1);
958 if (devpriv->is_6xxx) {
959 packed_data = d & 0xffff;
960 /* 6711 only has 16 bit wide ao fifo */
961 if (!devpriv->is_6711) {
962 comedi_buf_read_samples(s, &d, 1);
964 packed_data |= (d << 16) & 0xffff0000;
966 ni_writel(dev, packed_data, NI611X_AO_FIFO_DATA_REG);
968 ni_writew(dev, d, NI_E_AO_FIFO_DATA_REG);
974 * There's a small problem if the FIFO gets really low and we
975 * don't have the data to fill it. Basically, if after we fill
976 * the FIFO with all the data available, the FIFO is _still_
977 * less than half full, we never clear the interrupt. If the
978 * IRQ is in edge mode, we never get another interrupt, because
979 * this one wasn't cleared. If in level mode, we get flooded
980 * with interrupts that we can't fulfill, because nothing ever
981 * gets put into the buffer.
983 * This kind of situation is recoverable, but it is easier to
984 * just pretend we had a FIFO underrun, since there is a good
985 * chance it will happen anyway. This is _not_ the case for
986 * RT code, as RT code might purposely be running close to the
987 * metal. Needs to be fixed eventually.
989 static int ni_ao_fifo_half_empty(struct comedi_device *dev,
990 struct comedi_subdevice *s)
992 const struct ni_board_struct *board = dev->board_ptr;
994 unsigned int nsamples;
996 nbytes = comedi_buf_read_n_available(s);
998 s->async->events |= COMEDI_CB_OVERFLOW;
1002 nsamples = comedi_bytes_to_samples(s, nbytes);
1003 if (nsamples > board->ao_fifo_depth / 2)
1004 nsamples = board->ao_fifo_depth / 2;
1006 ni_ao_fifo_load(dev, s, nsamples);
1011 static int ni_ao_prep_fifo(struct comedi_device *dev,
1012 struct comedi_subdevice *s)
1014 const struct ni_board_struct *board = dev->board_ptr;
1015 struct ni_private *devpriv = dev->private;
1016 unsigned int nbytes;
1017 unsigned int nsamples;
1020 ni_stc_writew(dev, 1, NISTC_DAC_FIFO_CLR_REG);
1021 if (devpriv->is_6xxx)
1022 ni_ao_win_outl(dev, 0x6, NI611X_AO_FIFO_OFFSET_LOAD_REG);
1024 /* load some data */
1025 nbytes = comedi_buf_read_n_available(s);
1029 nsamples = comedi_bytes_to_samples(s, nbytes);
1030 if (nsamples > board->ao_fifo_depth)
1031 nsamples = board->ao_fifo_depth;
1033 ni_ao_fifo_load(dev, s, nsamples);
1038 static void ni_ai_fifo_read(struct comedi_device *dev,
1039 struct comedi_subdevice *s, int n)
1041 struct ni_private *devpriv = dev->private;
1042 struct comedi_async *async = s->async;
1044 unsigned short data;
1047 if (devpriv->is_611x) {
1048 for (i = 0; i < n / 2; i++) {
1049 dl = ni_readl(dev, NI611X_AI_FIFO_DATA_REG);
1050 /* This may get the hi/lo data in the wrong order */
1051 data = (dl >> 16) & 0xffff;
1052 comedi_buf_write_samples(s, &data, 1);
1054 comedi_buf_write_samples(s, &data, 1);
1056 /* Check if there's a single sample stuck in the FIFO */
1058 dl = ni_readl(dev, NI611X_AI_FIFO_DATA_REG);
1060 comedi_buf_write_samples(s, &data, 1);
1062 } else if (devpriv->is_6143) {
1064 * This just reads the FIFO assuming the data is present,
1065 * no checks on the FIFO status are performed.
1067 for (i = 0; i < n / 2; i++) {
1068 dl = ni_readl(dev, NI6143_AI_FIFO_DATA_REG);
1070 data = (dl >> 16) & 0xffff;
1071 comedi_buf_write_samples(s, &data, 1);
1073 comedi_buf_write_samples(s, &data, 1);
1076 /* Assume there is a single sample stuck in the FIFO */
1077 /* Get stranded sample into FIFO */
1078 ni_writel(dev, 0x01, NI6143_AI_FIFO_CTRL_REG);
1079 dl = ni_readl(dev, NI6143_AI_FIFO_DATA_REG);
1080 data = (dl >> 16) & 0xffff;
1081 comedi_buf_write_samples(s, &data, 1);
1084 if (n > ARRAY_SIZE(devpriv->ai_fifo_buffer)) {
1085 dev_err(dev->class_dev,
1086 "bug! ai_fifo_buffer too small\n");
1087 async->events |= COMEDI_CB_ERROR;
1090 for (i = 0; i < n; i++) {
1091 devpriv->ai_fifo_buffer[i] =
1092 ni_readw(dev, NI_E_AI_FIFO_DATA_REG);
1094 comedi_buf_write_samples(s, devpriv->ai_fifo_buffer, n);
1098 static void ni_handle_fifo_half_full(struct comedi_device *dev)
1100 const struct ni_board_struct *board = dev->board_ptr;
1101 struct comedi_subdevice *s = dev->read_subdev;
1104 n = board->ai_fifo_depth / 2;
1106 ni_ai_fifo_read(dev, s, n);
1110 /* Empties the AI fifo */
1111 static void ni_handle_fifo_dregs(struct comedi_device *dev)
1113 struct ni_private *devpriv = dev->private;
1114 struct comedi_subdevice *s = dev->read_subdev;
1116 unsigned short data;
1119 if (devpriv->is_611x) {
1120 while ((ni_stc_readw(dev, NISTC_AI_STATUS1_REG) &
1121 NISTC_AI_STATUS1_FIFO_E) == 0) {
1122 dl = ni_readl(dev, NI611X_AI_FIFO_DATA_REG);
1124 /* This may get the hi/lo data in the wrong order */
1126 comedi_buf_write_samples(s, &data, 1);
1128 comedi_buf_write_samples(s, &data, 1);
1130 } else if (devpriv->is_6143) {
1132 while (ni_readl(dev, NI6143_AI_FIFO_STATUS_REG) & 0x04) {
1133 dl = ni_readl(dev, NI6143_AI_FIFO_DATA_REG);
1135 /* This may get the hi/lo data in the wrong order */
1137 comedi_buf_write_samples(s, &data, 1);
1139 comedi_buf_write_samples(s, &data, 1);
1142 /* Check if stranded sample is present */
1143 if (ni_readl(dev, NI6143_AI_FIFO_STATUS_REG) & 0x01) {
1144 /* Get stranded sample into FIFO */
1145 ni_writel(dev, 0x01, NI6143_AI_FIFO_CTRL_REG);
1146 dl = ni_readl(dev, NI6143_AI_FIFO_DATA_REG);
1147 data = (dl >> 16) & 0xffff;
1148 comedi_buf_write_samples(s, &data, 1);
1152 unsigned short fe; /* fifo empty */
1154 fe = ni_stc_readw(dev, NISTC_AI_STATUS1_REG) &
1155 NISTC_AI_STATUS1_FIFO_E;
1158 i < ARRAY_SIZE(devpriv->ai_fifo_buffer); i++) {
1159 fe = ni_stc_readw(dev, NISTC_AI_STATUS1_REG) &
1160 NISTC_AI_STATUS1_FIFO_E;
1163 devpriv->ai_fifo_buffer[i] =
1164 ni_readw(dev, NI_E_AI_FIFO_DATA_REG);
1166 comedi_buf_write_samples(s, devpriv->ai_fifo_buffer, i);
1171 static void get_last_sample_611x(struct comedi_device *dev)
1173 struct ni_private *devpriv = dev->private;
1174 struct comedi_subdevice *s = dev->read_subdev;
1175 unsigned short data;
1178 if (!devpriv->is_611x)
1181 /* Check if there's a single sample stuck in the FIFO */
1182 if (ni_readb(dev, NI_E_STATUS_REG) & 0x80) {
1183 dl = ni_readl(dev, NI611X_AI_FIFO_DATA_REG);
1185 comedi_buf_write_samples(s, &data, 1);
1189 static void get_last_sample_6143(struct comedi_device *dev)
1191 struct ni_private *devpriv = dev->private;
1192 struct comedi_subdevice *s = dev->read_subdev;
1193 unsigned short data;
1196 if (!devpriv->is_6143)
1199 /* Check if there's a single sample stuck in the FIFO */
1200 if (ni_readl(dev, NI6143_AI_FIFO_STATUS_REG) & 0x01) {
1201 /* Get stranded sample into FIFO */
1202 ni_writel(dev, 0x01, NI6143_AI_FIFO_CTRL_REG);
1203 dl = ni_readl(dev, NI6143_AI_FIFO_DATA_REG);
1205 /* This may get the hi/lo data in the wrong order */
1206 data = (dl >> 16) & 0xffff;
1207 comedi_buf_write_samples(s, &data, 1);
1211 static void shutdown_ai_command(struct comedi_device *dev)
1213 struct comedi_subdevice *s = dev->read_subdev;
1216 ni_ai_drain_dma(dev);
1218 ni_handle_fifo_dregs(dev);
1219 get_last_sample_611x(dev);
1220 get_last_sample_6143(dev);
1222 s->async->events |= COMEDI_CB_EOA;
1225 static void ni_handle_eos(struct comedi_device *dev, struct comedi_subdevice *s)
1227 struct ni_private *devpriv = dev->private;
1229 if (devpriv->aimode == AIMODE_SCAN) {
1231 static const int timeout = 10;
1234 for (i = 0; i < timeout; i++) {
1235 ni_sync_ai_dma(dev);
1236 if ((s->async->events & COMEDI_CB_EOS))
1241 ni_handle_fifo_dregs(dev);
1242 s->async->events |= COMEDI_CB_EOS;
1245 /* handle special case of single scan */
1246 if (devpriv->ai_cmd2 & NISTC_AI_CMD2_END_ON_EOS)
1247 shutdown_ai_command(dev);
1250 static void handle_gpct_interrupt(struct comedi_device *dev,
1251 unsigned short counter_index)
1254 struct ni_private *devpriv = dev->private;
1255 struct comedi_subdevice *s;
1257 s = &dev->subdevices[NI_GPCT_SUBDEV(counter_index)];
1259 ni_tio_handle_interrupt(&devpriv->counter_dev->counters[counter_index],
1261 comedi_handle_events(dev, s);
1265 static void ack_a_interrupt(struct comedi_device *dev, unsigned short a_status)
1267 unsigned short ack = 0;
1269 if (a_status & NISTC_AI_STATUS1_SC_TC)
1270 ack |= NISTC_INTA_ACK_AI_SC_TC;
1271 if (a_status & NISTC_AI_STATUS1_START1)
1272 ack |= NISTC_INTA_ACK_AI_START1;
1273 if (a_status & NISTC_AI_STATUS1_START)
1274 ack |= NISTC_INTA_ACK_AI_START;
1275 if (a_status & NISTC_AI_STATUS1_STOP)
1276 ack |= NISTC_INTA_ACK_AI_STOP;
1277 if (a_status & NISTC_AI_STATUS1_OVER)
1278 ack |= NISTC_INTA_ACK_AI_ERR;
1280 ni_stc_writew(dev, ack, NISTC_INTA_ACK_REG);
1283 static void handle_a_interrupt(struct comedi_device *dev,
1284 struct comedi_subdevice *s,
1285 unsigned short status)
1287 struct comedi_cmd *cmd = &s->async->cmd;
1289 /* test for all uncommon interrupt events at the same time */
1290 if (status & (NISTC_AI_STATUS1_ERR |
1291 NISTC_AI_STATUS1_SC_TC | NISTC_AI_STATUS1_START1)) {
1292 if (status == 0xffff) {
1293 dev_err(dev->class_dev, "Card removed?\n");
1295 * We probably aren't even running a command now,
1296 * so it's a good idea to be careful.
1298 if (comedi_is_subdevice_running(s))
1299 s->async->events |= COMEDI_CB_ERROR;
1302 if (status & NISTC_AI_STATUS1_ERR) {
1303 dev_err(dev->class_dev, "ai error a_status=%04x\n",
1306 shutdown_ai_command(dev);
1308 s->async->events |= COMEDI_CB_ERROR;
1309 if (status & NISTC_AI_STATUS1_OVER)
1310 s->async->events |= COMEDI_CB_OVERFLOW;
1313 if (status & NISTC_AI_STATUS1_SC_TC) {
1314 if (cmd->stop_src == TRIG_COUNT)
1315 shutdown_ai_command(dev);
1319 if (status & NISTC_AI_STATUS1_FIFO_HF) {
1321 static const int timeout = 10;
1323 * PCMCIA cards (at least 6036) seem to stop producing
1324 * interrupts if we fail to get the fifo less than half
1325 * full, so loop to be sure.
1327 for (i = 0; i < timeout; ++i) {
1328 ni_handle_fifo_half_full(dev);
1329 if ((ni_stc_readw(dev, NISTC_AI_STATUS1_REG) &
1330 NISTC_AI_STATUS1_FIFO_HF) == 0)
1334 #endif /* !PCIDMA */
1336 if (status & NISTC_AI_STATUS1_STOP)
1337 ni_handle_eos(dev, s);
1340 static void ack_b_interrupt(struct comedi_device *dev, unsigned short b_status)
1342 unsigned short ack = 0;
1344 if (b_status & NISTC_AO_STATUS1_BC_TC)
1345 ack |= NISTC_INTB_ACK_AO_BC_TC;
1346 if (b_status & NISTC_AO_STATUS1_OVERRUN)
1347 ack |= NISTC_INTB_ACK_AO_ERR;
1348 if (b_status & NISTC_AO_STATUS1_START)
1349 ack |= NISTC_INTB_ACK_AO_START;
1350 if (b_status & NISTC_AO_STATUS1_START1)
1351 ack |= NISTC_INTB_ACK_AO_START1;
1352 if (b_status & NISTC_AO_STATUS1_UC_TC)
1353 ack |= NISTC_INTB_ACK_AO_UC_TC;
1354 if (b_status & NISTC_AO_STATUS1_UI2_TC)
1355 ack |= NISTC_INTB_ACK_AO_UI2_TC;
1356 if (b_status & NISTC_AO_STATUS1_UPDATE)
1357 ack |= NISTC_INTB_ACK_AO_UPDATE;
1359 ni_stc_writew(dev, ack, NISTC_INTB_ACK_REG);
1362 static void handle_b_interrupt(struct comedi_device *dev,
1363 struct comedi_subdevice *s,
1364 unsigned short b_status)
1366 if (b_status == 0xffff)
1368 if (b_status & NISTC_AO_STATUS1_OVERRUN) {
1369 dev_err(dev->class_dev,
1370 "AO FIFO underrun status=0x%04x status2=0x%04x\n",
1371 b_status, ni_stc_readw(dev, NISTC_AO_STATUS2_REG));
1372 s->async->events |= COMEDI_CB_OVERFLOW;
1375 if (s->async->cmd.stop_src != TRIG_NONE &&
1376 b_status & NISTC_AO_STATUS1_BC_TC)
1377 s->async->events |= COMEDI_CB_EOA;
1380 if (b_status & NISTC_AO_STATUS1_FIFO_REQ) {
1383 ret = ni_ao_fifo_half_empty(dev, s);
1385 dev_err(dev->class_dev, "AO buffer underrun\n");
1386 ni_set_bits(dev, NISTC_INTB_ENA_REG,
1387 NISTC_INTB_ENA_AO_FIFO |
1388 NISTC_INTB_ENA_AO_ERR, 0);
1389 s->async->events |= COMEDI_CB_OVERFLOW;
1395 static void ni_ai_munge(struct comedi_device *dev, struct comedi_subdevice *s,
1396 void *data, unsigned int num_bytes,
1397 unsigned int chan_index)
1399 struct ni_private *devpriv = dev->private;
1400 struct comedi_async *async = s->async;
1401 struct comedi_cmd *cmd = &async->cmd;
1402 unsigned int nsamples = comedi_bytes_to_samples(s, num_bytes);
1403 unsigned short *array = data;
1404 unsigned int *larray = data;
1407 __le16 *barray = data;
1408 __le32 *blarray = data;
1411 for (i = 0; i < nsamples; i++) {
1413 if (s->subdev_flags & SDF_LSAMPL)
1414 larray[i] = le32_to_cpu(blarray[i]);
1416 array[i] = le16_to_cpu(barray[i]);
1418 if (s->subdev_flags & SDF_LSAMPL)
1419 larray[i] += devpriv->ai_offset[chan_index];
1421 array[i] += devpriv->ai_offset[chan_index];
1423 chan_index %= cmd->chanlist_len;
1429 static int ni_ai_setup_MITE_dma(struct comedi_device *dev)
1431 struct ni_private *devpriv = dev->private;
1432 struct comedi_subdevice *s = dev->read_subdev;
1434 unsigned long flags;
1436 retval = ni_request_ai_mite_channel(dev);
1440 /* write alloc the entire buffer */
1441 comedi_buf_write_alloc(s, s->async->prealloc_bufsz);
1443 spin_lock_irqsave(&devpriv->mite_channel_lock, flags);
1444 if (!devpriv->ai_mite_chan) {
1445 spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
1449 if (devpriv->is_611x || devpriv->is_6143)
1450 mite_prep_dma(devpriv->ai_mite_chan, 32, 16);
1451 else if (devpriv->is_628x)
1452 mite_prep_dma(devpriv->ai_mite_chan, 32, 32);
1454 mite_prep_dma(devpriv->ai_mite_chan, 16, 16);
1457 mite_dma_arm(devpriv->ai_mite_chan);
1458 spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
1463 static int ni_ao_setup_MITE_dma(struct comedi_device *dev)
1465 struct ni_private *devpriv = dev->private;
1466 struct comedi_subdevice *s = dev->write_subdev;
1468 unsigned long flags;
1470 retval = ni_request_ao_mite_channel(dev);
1474 /* read alloc the entire buffer */
1475 comedi_buf_read_alloc(s, s->async->prealloc_bufsz);
1477 spin_lock_irqsave(&devpriv->mite_channel_lock, flags);
1478 if (devpriv->ao_mite_chan) {
1479 if (devpriv->is_611x || devpriv->is_6713) {
1480 mite_prep_dma(devpriv->ao_mite_chan, 32, 32);
1483 * Doing 32 instead of 16 bit wide transfers from
1484 * memory makes the mite do 32 bit pci transfers,
1485 * doubling pci bandwidth.
1487 mite_prep_dma(devpriv->ao_mite_chan, 16, 32);
1489 mite_dma_arm(devpriv->ao_mite_chan);
1493 spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
1501 * used for both cancel ioctl and board initialization
1503 * this is pretty harsh for a cancel, but it works...
1505 static int ni_ai_reset(struct comedi_device *dev, struct comedi_subdevice *s)
1507 struct ni_private *devpriv = dev->private;
1508 unsigned int ai_personal;
1509 unsigned int ai_out_ctrl;
1511 ni_release_ai_mite_channel(dev);
1512 /* ai configuration */
1513 ni_stc_writew(dev, NISTC_RESET_AI_CFG_START | NISTC_RESET_AI,
1516 ni_set_bits(dev, NISTC_INTA_ENA_REG, NISTC_INTA_ENA_AI_MASK, 0);
1518 ni_clear_ai_fifo(dev);
1520 if (!devpriv->is_6143)
1521 ni_writeb(dev, NI_E_MISC_CMD_EXT_ATRIG, NI_E_MISC_CMD_REG);
1523 ni_stc_writew(dev, NISTC_AI_CMD1_DISARM, NISTC_AI_CMD1_REG);
1524 ni_stc_writew(dev, NISTC_AI_MODE1_START_STOP |
1526 /*| NISTC_AI_MODE1_TRIGGER_ONCE */,
1527 NISTC_AI_MODE1_REG);
1528 ni_stc_writew(dev, 0, NISTC_AI_MODE2_REG);
1529 /* generate FIFO interrupts on non-empty */
1530 ni_stc_writew(dev, NISTC_AI_MODE3_FIFO_MODE_NE,
1531 NISTC_AI_MODE3_REG);
1533 ai_personal = NISTC_AI_PERSONAL_SHIFTIN_PW |
1534 NISTC_AI_PERSONAL_SOC_POLARITY |
1535 NISTC_AI_PERSONAL_LOCALMUX_CLK_PW;
1536 ai_out_ctrl = NISTC_AI_OUT_CTRL_SCAN_IN_PROG_SEL(3) |
1537 NISTC_AI_OUT_CTRL_EXTMUX_CLK_SEL(0) |
1538 NISTC_AI_OUT_CTRL_LOCALMUX_CLK_SEL(2) |
1539 NISTC_AI_OUT_CTRL_SC_TC_SEL(3);
1540 if (devpriv->is_611x) {
1541 ai_out_ctrl |= NISTC_AI_OUT_CTRL_CONVERT_HIGH;
1542 } else if (devpriv->is_6143) {
1543 ai_out_ctrl |= NISTC_AI_OUT_CTRL_CONVERT_LOW;
1545 ai_personal |= NISTC_AI_PERSONAL_CONVERT_PW;
1546 if (devpriv->is_622x)
1547 ai_out_ctrl |= NISTC_AI_OUT_CTRL_CONVERT_HIGH;
1549 ai_out_ctrl |= NISTC_AI_OUT_CTRL_CONVERT_LOW;
1551 ni_stc_writew(dev, ai_personal, NISTC_AI_PERSONAL_REG);
1552 ni_stc_writew(dev, ai_out_ctrl, NISTC_AI_OUT_CTRL_REG);
1554 /* the following registers should not be changed, because there
1555 * are no backup registers in devpriv. If you want to change
1556 * any of these, add a backup register and other appropriate code:
1557 * NISTC_AI_MODE1_REG
1558 * NISTC_AI_MODE3_REG
1559 * NISTC_AI_PERSONAL_REG
1560 * NISTC_AI_OUT_CTRL_REG
1563 /* clear interrupts */
1564 ni_stc_writew(dev, NISTC_INTA_ACK_AI_ALL, NISTC_INTA_ACK_REG);
1566 ni_stc_writew(dev, NISTC_RESET_AI_CFG_END, NISTC_RESET_REG);
1571 static int ni_ai_poll(struct comedi_device *dev, struct comedi_subdevice *s)
1573 unsigned long flags;
1576 /* lock to avoid race with interrupt handler */
1577 spin_lock_irqsave(&dev->spinlock, flags);
1579 ni_handle_fifo_dregs(dev);
1581 ni_sync_ai_dma(dev);
1583 count = comedi_buf_n_bytes_ready(s);
1584 spin_unlock_irqrestore(&dev->spinlock, flags);
1589 static void ni_prime_channelgain_list(struct comedi_device *dev)
1593 ni_stc_writew(dev, NISTC_AI_CMD1_CONVERT_PULSE, NISTC_AI_CMD1_REG);
1594 for (i = 0; i < NI_TIMEOUT; ++i) {
1595 if (!(ni_stc_readw(dev, NISTC_AI_STATUS1_REG) &
1596 NISTC_AI_STATUS1_FIFO_E)) {
1597 ni_stc_writew(dev, 1, NISTC_ADC_FIFO_CLR_REG);
1602 dev_err(dev->class_dev, "timeout loading channel/gain list\n");
1605 static void ni_m_series_load_channelgain_list(struct comedi_device *dev,
1606 unsigned int n_chan,
1609 const struct ni_board_struct *board = dev->board_ptr;
1610 struct ni_private *devpriv = dev->private;
1611 unsigned int chan, range, aref;
1613 unsigned int dither;
1614 unsigned int range_code;
1616 ni_stc_writew(dev, 1, NISTC_CFG_MEM_CLR_REG);
1618 if ((list[0] & CR_ALT_SOURCE)) {
1619 unsigned int bypass_bits;
1621 chan = CR_CHAN(list[0]);
1622 range = CR_RANGE(list[0]);
1623 range_code = ni_gainlkup[board->gainlkup][range];
1624 dither = (list[0] & CR_ALT_FILTER) != 0;
1625 bypass_bits = NI_M_CFG_BYPASS_FIFO |
1626 NI_M_CFG_BYPASS_AI_CHAN(chan) |
1627 NI_M_CFG_BYPASS_AI_GAIN(range_code) |
1628 devpriv->ai_calib_source;
1630 bypass_bits |= NI_M_CFG_BYPASS_AI_DITHER;
1631 /* don't use 2's complement encoding */
1632 bypass_bits |= NI_M_CFG_BYPASS_AI_POLARITY;
1633 ni_writel(dev, bypass_bits, NI_M_CFG_BYPASS_FIFO_REG);
1635 ni_writel(dev, 0, NI_M_CFG_BYPASS_FIFO_REG);
1637 for (i = 0; i < n_chan; i++) {
1638 unsigned int config_bits = 0;
1640 chan = CR_CHAN(list[i]);
1641 aref = CR_AREF(list[i]);
1642 range = CR_RANGE(list[i]);
1643 dither = (list[i] & CR_ALT_FILTER) != 0;
1645 range_code = ni_gainlkup[board->gainlkup][range];
1646 devpriv->ai_offset[i] = 0;
1649 config_bits |= NI_M_AI_CFG_CHAN_TYPE_DIFF;
1652 config_bits |= NI_M_AI_CFG_CHAN_TYPE_COMMON;
1655 config_bits |= NI_M_AI_CFG_CHAN_TYPE_GROUND;
1660 config_bits |= NI_M_AI_CFG_CHAN_SEL(chan);
1661 config_bits |= NI_M_AI_CFG_BANK_SEL(chan);
1662 config_bits |= NI_M_AI_CFG_GAIN(range_code);
1663 if (i == n_chan - 1)
1664 config_bits |= NI_M_AI_CFG_LAST_CHAN;
1666 config_bits |= NI_M_AI_CFG_DITHER;
1667 /* don't use 2's complement encoding */
1668 config_bits |= NI_M_AI_CFG_POLARITY;
1669 ni_writew(dev, config_bits, NI_M_AI_CFG_FIFO_DATA_REG);
1671 ni_prime_channelgain_list(dev);
1675 * Notes on the 6110 and 6111:
1676 * These boards a slightly different than the rest of the series, since
1677 * they have multiple A/D converters.
1678 * From the driver side, the configuration memory is a
1680 * Configuration Memory Low:
1682 * bit 8: unipolar/bipolar (should be 0 for bipolar)
1683 * bits 0-3: gain. This is 4 bits instead of 3 for the other boards
1684 * 1001 gain=0.1 (+/- 50)
1693 * Configuration Memory High:
1694 * bits 12-14: Channel Type
1695 * 001 for differential
1696 * 000 for calibration
1697 * bit 11: coupling (this is not currently handled)
1701 * valid channels are 0-3
1703 static void ni_load_channelgain_list(struct comedi_device *dev,
1704 struct comedi_subdevice *s,
1705 unsigned int n_chan, unsigned int *list)
1707 const struct ni_board_struct *board = dev->board_ptr;
1708 struct ni_private *devpriv = dev->private;
1709 unsigned int offset = (s->maxdata + 1) >> 1;
1710 unsigned int chan, range, aref;
1712 unsigned int hi, lo;
1713 unsigned int dither;
1715 if (devpriv->is_m_series) {
1716 ni_m_series_load_channelgain_list(dev, n_chan, list);
1719 if (n_chan == 1 && !devpriv->is_611x && !devpriv->is_6143) {
1720 if (devpriv->changain_state &&
1721 devpriv->changain_spec == list[0]) {
1725 devpriv->changain_state = 1;
1726 devpriv->changain_spec = list[0];
1728 devpriv->changain_state = 0;
1731 ni_stc_writew(dev, 1, NISTC_CFG_MEM_CLR_REG);
1733 /* Set up Calibration mode if required */
1734 if (devpriv->is_6143) {
1735 if ((list[0] & CR_ALT_SOURCE) &&
1736 !devpriv->ai_calib_source_enabled) {
1737 /* Strobe Relay enable bit */
1738 ni_writew(dev, devpriv->ai_calib_source |
1739 NI6143_CALIB_CHAN_RELAY_ON,
1740 NI6143_CALIB_CHAN_REG);
1741 ni_writew(dev, devpriv->ai_calib_source,
1742 NI6143_CALIB_CHAN_REG);
1743 devpriv->ai_calib_source_enabled = 1;
1744 /* Allow relays to change */
1745 msleep_interruptible(100);
1746 } else if (!(list[0] & CR_ALT_SOURCE) &&
1747 devpriv->ai_calib_source_enabled) {
1748 /* Strobe Relay disable bit */
1749 ni_writew(dev, devpriv->ai_calib_source |
1750 NI6143_CALIB_CHAN_RELAY_OFF,
1751 NI6143_CALIB_CHAN_REG);
1752 ni_writew(dev, devpriv->ai_calib_source,
1753 NI6143_CALIB_CHAN_REG);
1754 devpriv->ai_calib_source_enabled = 0;
1755 /* Allow relays to change */
1756 msleep_interruptible(100);
1760 for (i = 0; i < n_chan; i++) {
1761 if (!devpriv->is_6143 && (list[i] & CR_ALT_SOURCE))
1762 chan = devpriv->ai_calib_source;
1764 chan = CR_CHAN(list[i]);
1765 aref = CR_AREF(list[i]);
1766 range = CR_RANGE(list[i]);
1767 dither = (list[i] & CR_ALT_FILTER) != 0;
1769 /* fix the external/internal range differences */
1770 range = ni_gainlkup[board->gainlkup][range];
1771 if (devpriv->is_611x)
1772 devpriv->ai_offset[i] = offset;
1774 devpriv->ai_offset[i] = (range & 0x100) ? 0 : offset;
1777 if ((list[i] & CR_ALT_SOURCE)) {
1778 if (devpriv->is_611x)
1779 ni_writew(dev, CR_CHAN(list[i]) & 0x0003,
1780 NI611X_CALIB_CHAN_SEL_REG);
1782 if (devpriv->is_611x)
1784 else if (devpriv->is_6143)
1788 hi |= NI_E_AI_CFG_HI_TYPE_DIFF;
1791 hi |= NI_E_AI_CFG_HI_TYPE_COMMON;
1794 hi |= NI_E_AI_CFG_HI_TYPE_GROUND;
1800 hi |= NI_E_AI_CFG_HI_CHAN(chan);
1802 ni_writew(dev, hi, NI_E_AI_CFG_HI_REG);
1804 if (!devpriv->is_6143) {
1805 lo = NI_E_AI_CFG_LO_GAIN(range);
1807 if (i == n_chan - 1)
1808 lo |= NI_E_AI_CFG_LO_LAST_CHAN;
1810 lo |= NI_E_AI_CFG_LO_DITHER;
1812 ni_writew(dev, lo, NI_E_AI_CFG_LO_REG);
1816 /* prime the channel/gain list */
1817 if (!devpriv->is_611x && !devpriv->is_6143)
1818 ni_prime_channelgain_list(dev);
1821 static int ni_ai_insn_read(struct comedi_device *dev,
1822 struct comedi_subdevice *s,
1823 struct comedi_insn *insn,
1826 struct ni_private *devpriv = dev->private;
1827 unsigned int mask = s->maxdata;
1829 unsigned int signbits;
1832 ni_load_channelgain_list(dev, s, 1, &insn->chanspec);
1834 ni_clear_ai_fifo(dev);
1836 signbits = devpriv->ai_offset[0];
1837 if (devpriv->is_611x) {
1838 for (n = 0; n < num_adc_stages_611x; n++) {
1839 ni_stc_writew(dev, NISTC_AI_CMD1_CONVERT_PULSE,
1843 for (n = 0; n < insn->n; n++) {
1844 ni_stc_writew(dev, NISTC_AI_CMD1_CONVERT_PULSE,
1846 /* The 611x has screwy 32-bit FIFOs. */
1848 for (i = 0; i < NI_TIMEOUT; i++) {
1849 if (ni_readb(dev, NI_E_STATUS_REG) & 0x80) {
1851 NI611X_AI_FIFO_DATA_REG);
1856 if (!(ni_stc_readw(dev, NISTC_AI_STATUS1_REG) &
1857 NISTC_AI_STATUS1_FIFO_E)) {
1859 NI611X_AI_FIFO_DATA_REG);
1864 if (i == NI_TIMEOUT) {
1865 dev_err(dev->class_dev, "timeout\n");
1869 data[n] = d & 0xffff;
1871 } else if (devpriv->is_6143) {
1872 for (n = 0; n < insn->n; n++) {
1873 ni_stc_writew(dev, NISTC_AI_CMD1_CONVERT_PULSE,
1877 * The 6143 has 32-bit FIFOs. You need to strobe a
1878 * bit to move a single 16bit stranded sample into
1882 for (i = 0; i < NI_TIMEOUT; i++) {
1883 if (ni_readl(dev, NI6143_AI_FIFO_STATUS_REG) &
1885 /* Get stranded sample into FIFO */
1886 ni_writel(dev, 0x01,
1887 NI6143_AI_FIFO_CTRL_REG);
1889 NI6143_AI_FIFO_DATA_REG);
1893 if (i == NI_TIMEOUT) {
1894 dev_err(dev->class_dev, "timeout\n");
1897 data[n] = (((d >> 16) & 0xFFFF) + signbits) & 0xFFFF;
1900 for (n = 0; n < insn->n; n++) {
1901 ni_stc_writew(dev, NISTC_AI_CMD1_CONVERT_PULSE,
1903 for (i = 0; i < NI_TIMEOUT; i++) {
1904 if (!(ni_stc_readw(dev, NISTC_AI_STATUS1_REG) &
1905 NISTC_AI_STATUS1_FIFO_E))
1908 if (i == NI_TIMEOUT) {
1909 dev_err(dev->class_dev, "timeout\n");
1912 if (devpriv->is_m_series) {
1913 d = ni_readl(dev, NI_M_AI_FIFO_DATA_REG);
1917 d = ni_readw(dev, NI_E_AI_FIFO_DATA_REG);
1919 data[n] = d & 0xffff;
1926 static int ni_ns_to_timer(const struct comedi_device *dev,
1927 unsigned int nanosec, unsigned int flags)
1929 struct ni_private *devpriv = dev->private;
1932 switch (flags & CMDF_ROUND_MASK) {
1933 case CMDF_ROUND_NEAREST:
1935 divider = DIV_ROUND_CLOSEST(nanosec, devpriv->clock_ns);
1937 case CMDF_ROUND_DOWN:
1938 divider = (nanosec) / devpriv->clock_ns;
1941 divider = DIV_ROUND_UP(nanosec, devpriv->clock_ns);
1947 static unsigned int ni_timer_to_ns(const struct comedi_device *dev, int timer)
1949 struct ni_private *devpriv = dev->private;
1951 return devpriv->clock_ns * (timer + 1);
1954 static void ni_cmd_set_mite_transfer(struct mite_ring *ring,
1955 struct comedi_subdevice *sdev,
1956 const struct comedi_cmd *cmd,
1957 unsigned int max_count)
1960 unsigned int nbytes = max_count;
1962 if (cmd->stop_arg > 0 && cmd->stop_arg < max_count)
1963 nbytes = cmd->stop_arg;
1964 nbytes *= comedi_bytes_per_scan(sdev);
1966 if (nbytes > sdev->async->prealloc_bufsz) {
1967 if (cmd->stop_arg > 0)
1968 dev_err(sdev->device->class_dev,
1969 "%s: tried exact data transfer limits greater than buffer size\n",
1973 * we can only transfer up to the size of the buffer. In this
1974 * case, the user is expected to continue to write into the
1975 * comedi buffer (already implemented as a ring buffer).
1977 nbytes = sdev->async->prealloc_bufsz;
1980 mite_init_ring_descriptors(ring, sdev, nbytes);
1982 dev_err(sdev->device->class_dev,
1983 "%s: exact data transfer limits not implemented yet without DMA\n",
1988 static unsigned int ni_min_ai_scan_period_ns(struct comedi_device *dev,
1989 unsigned int num_channels)
1991 const struct ni_board_struct *board = dev->board_ptr;
1992 struct ni_private *devpriv = dev->private;
1994 /* simultaneously-sampled inputs */
1995 if (devpriv->is_611x || devpriv->is_6143)
1996 return board->ai_speed;
1998 /* multiplexed inputs */
1999 return board->ai_speed * num_channels;
2002 static int ni_ai_cmdtest(struct comedi_device *dev, struct comedi_subdevice *s,
2003 struct comedi_cmd *cmd)
2005 const struct ni_board_struct *board = dev->board_ptr;
2006 struct ni_private *devpriv = dev->private;
2008 unsigned int sources;
2010 /* Step 1 : check if triggers are trivially valid */
2012 err |= comedi_check_trigger_src(&cmd->start_src,
2013 TRIG_NOW | TRIG_INT | TRIG_EXT);
2014 err |= comedi_check_trigger_src(&cmd->scan_begin_src,
2015 TRIG_TIMER | TRIG_EXT);
2017 sources = TRIG_TIMER | TRIG_EXT;
2018 if (devpriv->is_611x || devpriv->is_6143)
2019 sources |= TRIG_NOW;
2020 err |= comedi_check_trigger_src(&cmd->convert_src, sources);
2022 err |= comedi_check_trigger_src(&cmd->scan_end_src, TRIG_COUNT);
2023 err |= comedi_check_trigger_src(&cmd->stop_src, TRIG_COUNT | TRIG_NONE);
2028 /* Step 2a : make sure trigger sources are unique */
2030 err |= comedi_check_trigger_is_unique(cmd->start_src);
2031 err |= comedi_check_trigger_is_unique(cmd->scan_begin_src);
2032 err |= comedi_check_trigger_is_unique(cmd->convert_src);
2033 err |= comedi_check_trigger_is_unique(cmd->stop_src);
2035 /* Step 2b : and mutually compatible */
2040 /* Step 3: check if arguments are trivially valid */
2042 switch (cmd->start_src) {
2045 err |= comedi_check_trigger_arg_is(&cmd->start_arg, 0);
2048 err |= ni_check_trigger_arg_roffs(CR_CHAN(cmd->start_arg),
2050 &devpriv->routing_tables, 1);
2054 if (cmd->scan_begin_src == TRIG_TIMER) {
2055 err |= comedi_check_trigger_arg_min(&cmd->scan_begin_arg,
2056 ni_min_ai_scan_period_ns(dev, cmd->chanlist_len));
2057 err |= comedi_check_trigger_arg_max(&cmd->scan_begin_arg,
2060 } else if (cmd->scan_begin_src == TRIG_EXT) {
2061 /* external trigger */
2062 err |= ni_check_trigger_arg_roffs(CR_CHAN(cmd->scan_begin_arg),
2064 &devpriv->routing_tables, 1);
2065 } else { /* TRIG_OTHER */
2066 err |= comedi_check_trigger_arg_is(&cmd->scan_begin_arg, 0);
2069 if (cmd->convert_src == TRIG_TIMER) {
2070 if (devpriv->is_611x || devpriv->is_6143) {
2071 err |= comedi_check_trigger_arg_is(&cmd->convert_arg,
2074 err |= comedi_check_trigger_arg_min(&cmd->convert_arg,
2076 err |= comedi_check_trigger_arg_max(&cmd->convert_arg,
2080 } else if (cmd->convert_src == TRIG_EXT) {
2081 /* external trigger */
2082 err |= ni_check_trigger_arg_roffs(CR_CHAN(cmd->convert_arg),
2084 &devpriv->routing_tables, 1);
2085 } else if (cmd->convert_src == TRIG_NOW) {
2086 err |= comedi_check_trigger_arg_is(&cmd->convert_arg, 0);
2089 err |= comedi_check_trigger_arg_is(&cmd->scan_end_arg,
2092 if (cmd->stop_src == TRIG_COUNT) {
2093 unsigned int max_count = 0x01000000;
2095 if (devpriv->is_611x)
2096 max_count -= num_adc_stages_611x;
2097 err |= comedi_check_trigger_arg_max(&cmd->stop_arg, max_count);
2098 err |= comedi_check_trigger_arg_min(&cmd->stop_arg, 1);
2101 err |= comedi_check_trigger_arg_is(&cmd->stop_arg, 0);
2107 /* step 4: fix up any arguments */
2109 if (cmd->scan_begin_src == TRIG_TIMER) {
2110 unsigned int tmp = cmd->scan_begin_arg;
2112 cmd->scan_begin_arg =
2113 ni_timer_to_ns(dev, ni_ns_to_timer(dev,
2114 cmd->scan_begin_arg,
2116 if (tmp != cmd->scan_begin_arg)
2119 if (cmd->convert_src == TRIG_TIMER) {
2120 if (!devpriv->is_611x && !devpriv->is_6143) {
2121 unsigned int tmp = cmd->convert_arg;
2124 ni_timer_to_ns(dev, ni_ns_to_timer(dev,
2127 if (tmp != cmd->convert_arg)
2129 if (cmd->scan_begin_src == TRIG_TIMER &&
2130 cmd->scan_begin_arg <
2131 cmd->convert_arg * cmd->scan_end_arg) {
2132 cmd->scan_begin_arg =
2133 cmd->convert_arg * cmd->scan_end_arg;
2145 static int ni_ai_inttrig(struct comedi_device *dev,
2146 struct comedi_subdevice *s,
2147 unsigned int trig_num)
2149 struct ni_private *devpriv = dev->private;
2150 struct comedi_cmd *cmd = &s->async->cmd;
2152 if (trig_num != cmd->start_arg)
2155 ni_stc_writew(dev, NISTC_AI_CMD2_START1_PULSE | devpriv->ai_cmd2,
2157 s->async->inttrig = NULL;
2162 static int ni_ai_cmd(struct comedi_device *dev, struct comedi_subdevice *s)
2164 struct ni_private *devpriv = dev->private;
2165 const struct comedi_cmd *cmd = &s->async->cmd;
2167 int mode1 = 0; /* mode1 is needed for both stop and convert */
2169 int start_stop_select = 0;
2170 unsigned int stop_count;
2171 int interrupt_a_enable = 0;
2172 unsigned int ai_trig;
2174 if (dev->irq == 0) {
2175 dev_err(dev->class_dev, "cannot run command without an irq\n");
2178 ni_clear_ai_fifo(dev);
2180 ni_load_channelgain_list(dev, s, cmd->chanlist_len, cmd->chanlist);
2182 /* start configuration */
2183 ni_stc_writew(dev, NISTC_RESET_AI_CFG_START, NISTC_RESET_REG);
2186 * Disable analog triggering for now, since it interferes
2187 * with the use of pfi0.
2189 devpriv->an_trig_etc_reg &= ~NISTC_ATRIG_ETC_ENA;
2190 ni_stc_writew(dev, devpriv->an_trig_etc_reg, NISTC_ATRIG_ETC_REG);
2192 ai_trig = NISTC_AI_TRIG_START2_SEL(0) | NISTC_AI_TRIG_START1_SYNC;
2193 switch (cmd->start_src) {
2196 ai_trig |= NISTC_AI_TRIG_START1_EDGE |
2197 NISTC_AI_TRIG_START1_SEL(0);
2200 ai_trig |= NISTC_AI_TRIG_START1_SEL(
2201 ni_get_reg_value_roffs(
2202 CR_CHAN(cmd->start_arg),
2204 &devpriv->routing_tables, 1));
2206 if (cmd->start_arg & CR_INVERT)
2207 ai_trig |= NISTC_AI_TRIG_START1_POLARITY;
2208 if (cmd->start_arg & CR_EDGE)
2209 ai_trig |= NISTC_AI_TRIG_START1_EDGE;
2212 ni_stc_writew(dev, ai_trig, NISTC_AI_TRIG_SEL_REG);
2214 mode2 &= ~NISTC_AI_MODE2_PRE_TRIGGER;
2215 mode2 &= ~NISTC_AI_MODE2_SC_INIT_LOAD_SRC;
2216 mode2 &= ~NISTC_AI_MODE2_SC_RELOAD_MODE;
2217 ni_stc_writew(dev, mode2, NISTC_AI_MODE2_REG);
2219 if (cmd->chanlist_len == 1 || devpriv->is_611x || devpriv->is_6143) {
2221 start_stop_select |= NISTC_AI_STOP_POLARITY |
2222 NISTC_AI_STOP_SEL(31) |
2225 /* ai configuration memory */
2226 start_stop_select |= NISTC_AI_STOP_SEL(19);
2228 ni_stc_writew(dev, start_stop_select, NISTC_AI_START_STOP_REG);
2230 devpriv->ai_cmd2 = 0;
2231 switch (cmd->stop_src) {
2233 stop_count = cmd->stop_arg - 1;
2235 if (devpriv->is_611x) {
2236 /* have to take 3 stage adc pipeline into account */
2237 stop_count += num_adc_stages_611x;
2239 /* stage number of scans */
2240 ni_stc_writel(dev, stop_count, NISTC_AI_SC_LOADA_REG);
2242 mode1 |= NISTC_AI_MODE1_START_STOP |
2243 NISTC_AI_MODE1_RSVD |
2244 NISTC_AI_MODE1_TRIGGER_ONCE;
2245 ni_stc_writew(dev, mode1, NISTC_AI_MODE1_REG);
2246 /* load SC (Scan Count) */
2247 ni_stc_writew(dev, NISTC_AI_CMD1_SC_LOAD, NISTC_AI_CMD1_REG);
2249 if (stop_count == 0) {
2250 devpriv->ai_cmd2 |= NISTC_AI_CMD2_END_ON_EOS;
2251 interrupt_a_enable |= NISTC_INTA_ENA_AI_STOP;
2253 * This is required to get the last sample for
2254 * chanlist_len > 1, not sure why.
2256 if (cmd->chanlist_len > 1)
2257 start_stop_select |= NISTC_AI_STOP_POLARITY |
2262 /* stage number of scans */
2263 ni_stc_writel(dev, 0, NISTC_AI_SC_LOADA_REG);
2265 mode1 |= NISTC_AI_MODE1_START_STOP |
2266 NISTC_AI_MODE1_RSVD |
2267 NISTC_AI_MODE1_CONTINUOUS;
2268 ni_stc_writew(dev, mode1, NISTC_AI_MODE1_REG);
2270 /* load SC (Scan Count) */
2271 ni_stc_writew(dev, NISTC_AI_CMD1_SC_LOAD, NISTC_AI_CMD1_REG);
2275 switch (cmd->scan_begin_src) {
2278 * stop bits for non 611x boards
2279 * NISTC_AI_MODE3_SI_TRIG_DELAY=0
2280 * NISTC_AI_MODE2_PRE_TRIGGER=0
2281 * NISTC_AI_START_STOP_REG:
2282 * NISTC_AI_START_POLARITY=0 (?) rising edge
2283 * NISTC_AI_START_EDGE=1 edge triggered
2284 * NISTC_AI_START_SYNC=1 (?)
2285 * NISTC_AI_START_SEL=0 SI_TC
2286 * NISTC_AI_STOP_POLARITY=0 rising edge
2287 * NISTC_AI_STOP_EDGE=0 level
2288 * NISTC_AI_STOP_SYNC=1
2289 * NISTC_AI_STOP_SEL=19 external pin (configuration mem)
2291 start_stop_select |= NISTC_AI_START_EDGE | NISTC_AI_START_SYNC;
2292 ni_stc_writew(dev, start_stop_select, NISTC_AI_START_STOP_REG);
2294 mode2 &= ~NISTC_AI_MODE2_SI_INIT_LOAD_SRC; /* A */
2295 mode2 |= NISTC_AI_MODE2_SI_RELOAD_MODE(0);
2296 /* mode2 |= NISTC_AI_MODE2_SC_RELOAD_MODE; */
2297 ni_stc_writew(dev, mode2, NISTC_AI_MODE2_REG);
2300 timer = ni_ns_to_timer(dev, cmd->scan_begin_arg,
2301 CMDF_ROUND_NEAREST);
2302 ni_stc_writel(dev, timer, NISTC_AI_SI_LOADA_REG);
2303 ni_stc_writew(dev, NISTC_AI_CMD1_SI_LOAD, NISTC_AI_CMD1_REG);
2306 if (cmd->scan_begin_arg & CR_EDGE)
2307 start_stop_select |= NISTC_AI_START_EDGE;
2308 if (cmd->scan_begin_arg & CR_INVERT) /* falling edge */
2309 start_stop_select |= NISTC_AI_START_POLARITY;
2310 if (cmd->scan_begin_src != cmd->convert_src ||
2311 (cmd->scan_begin_arg & ~CR_EDGE) !=
2312 (cmd->convert_arg & ~CR_EDGE))
2313 start_stop_select |= NISTC_AI_START_SYNC;
2315 start_stop_select |= NISTC_AI_START_SEL(
2316 ni_get_reg_value_roffs(
2317 CR_CHAN(cmd->scan_begin_arg),
2319 &devpriv->routing_tables, 1));
2320 ni_stc_writew(dev, start_stop_select, NISTC_AI_START_STOP_REG);
2324 switch (cmd->convert_src) {
2327 if (cmd->convert_arg == 0 || cmd->convert_src == TRIG_NOW)
2330 timer = ni_ns_to_timer(dev, cmd->convert_arg,
2331 CMDF_ROUND_NEAREST);
2332 /* 0,0 does not work */
2333 ni_stc_writew(dev, 1, NISTC_AI_SI2_LOADA_REG);
2334 ni_stc_writew(dev, timer, NISTC_AI_SI2_LOADB_REG);
2336 mode2 &= ~NISTC_AI_MODE2_SI2_INIT_LOAD_SRC; /* A */
2337 mode2 |= NISTC_AI_MODE2_SI2_RELOAD_MODE; /* alternate */
2338 ni_stc_writew(dev, mode2, NISTC_AI_MODE2_REG);
2340 ni_stc_writew(dev, NISTC_AI_CMD1_SI2_LOAD, NISTC_AI_CMD1_REG);
2342 mode2 |= NISTC_AI_MODE2_SI2_INIT_LOAD_SRC; /* B */
2343 mode2 |= NISTC_AI_MODE2_SI2_RELOAD_MODE; /* alternate */
2344 ni_stc_writew(dev, mode2, NISTC_AI_MODE2_REG);
2347 mode1 |= NISTC_AI_MODE1_CONVERT_SRC(
2348 ni_get_reg_value_roffs(
2349 CR_CHAN(cmd->convert_arg),
2351 &devpriv->routing_tables, 1));
2352 if ((cmd->convert_arg & CR_INVERT) == 0)
2353 mode1 |= NISTC_AI_MODE1_CONVERT_POLARITY;
2354 ni_stc_writew(dev, mode1, NISTC_AI_MODE1_REG);
2356 mode2 |= NISTC_AI_MODE2_SC_GATE_ENA |
2357 NISTC_AI_MODE2_START_STOP_GATE_ENA;
2358 ni_stc_writew(dev, mode2, NISTC_AI_MODE2_REG);
2364 /* interrupt on FIFO, errors, SC_TC */
2365 interrupt_a_enable |= NISTC_INTA_ENA_AI_ERR |
2366 NISTC_INTA_ENA_AI_SC_TC;
2369 interrupt_a_enable |= NISTC_INTA_ENA_AI_FIFO;
2372 if ((cmd->flags & CMDF_WAKE_EOS) ||
2373 (devpriv->ai_cmd2 & NISTC_AI_CMD2_END_ON_EOS)) {
2374 /* wake on end-of-scan */
2375 devpriv->aimode = AIMODE_SCAN;
2377 devpriv->aimode = AIMODE_HALF_FULL;
2380 switch (devpriv->aimode) {
2381 case AIMODE_HALF_FULL:
2382 /* FIFO interrupts and DMA requests on half-full */
2384 ni_stc_writew(dev, NISTC_AI_MODE3_FIFO_MODE_HF_E,
2385 NISTC_AI_MODE3_REG);
2387 ni_stc_writew(dev, NISTC_AI_MODE3_FIFO_MODE_HF,
2388 NISTC_AI_MODE3_REG);
2392 /* generate FIFO interrupts on non-empty */
2393 ni_stc_writew(dev, NISTC_AI_MODE3_FIFO_MODE_NE,
2394 NISTC_AI_MODE3_REG);
2398 ni_stc_writew(dev, NISTC_AI_MODE3_FIFO_MODE_NE,
2399 NISTC_AI_MODE3_REG);
2401 ni_stc_writew(dev, NISTC_AI_MODE3_FIFO_MODE_HF,
2402 NISTC_AI_MODE3_REG);
2404 interrupt_a_enable |= NISTC_INTA_ENA_AI_STOP;
2410 /* clear interrupts */
2411 ni_stc_writew(dev, NISTC_INTA_ACK_AI_ALL, NISTC_INTA_ACK_REG);
2413 ni_set_bits(dev, NISTC_INTA_ENA_REG, interrupt_a_enable, 1);
2415 /* interrupt on nothing */
2416 ni_set_bits(dev, NISTC_INTA_ENA_REG, ~0, 0);
2418 /* XXX start polling if necessary */
2421 /* end configuration */
2422 ni_stc_writew(dev, NISTC_RESET_AI_CFG_END, NISTC_RESET_REG);
2424 switch (cmd->scan_begin_src) {
2426 ni_stc_writew(dev, NISTC_AI_CMD1_SI2_ARM |
2427 NISTC_AI_CMD1_SI_ARM |
2428 NISTC_AI_CMD1_DIV_ARM |
2429 NISTC_AI_CMD1_SC_ARM,
2433 ni_stc_writew(dev, NISTC_AI_CMD1_SI2_ARM |
2434 NISTC_AI_CMD1_SI_ARM | /* XXX ? */
2435 NISTC_AI_CMD1_DIV_ARM |
2436 NISTC_AI_CMD1_SC_ARM,
2443 int retval = ni_ai_setup_MITE_dma(dev);
2450 if (cmd->start_src == TRIG_NOW) {
2451 ni_stc_writew(dev, NISTC_AI_CMD2_START1_PULSE |
2454 s->async->inttrig = NULL;
2455 } else if (cmd->start_src == TRIG_EXT) {
2456 s->async->inttrig = NULL;
2457 } else { /* TRIG_INT */
2458 s->async->inttrig = ni_ai_inttrig;
2464 static int ni_ai_insn_config(struct comedi_device *dev,
2465 struct comedi_subdevice *s,
2466 struct comedi_insn *insn, unsigned int *data)
2468 const struct ni_board_struct *board = dev->board_ptr;
2469 struct ni_private *devpriv = dev->private;
2475 case INSN_CONFIG_ALT_SOURCE:
2476 if (devpriv->is_m_series) {
2477 if (data[1] & ~NI_M_CFG_BYPASS_AI_CAL_MASK)
2479 devpriv->ai_calib_source = data[1];
2480 } else if (devpriv->is_6143) {
2481 unsigned int calib_source;
2483 calib_source = data[1] & 0xf;
2485 devpriv->ai_calib_source = calib_source;
2486 ni_writew(dev, calib_source, NI6143_CALIB_CHAN_REG);
2488 unsigned int calib_source;
2489 unsigned int calib_source_adjust;
2491 calib_source = data[1] & 0xf;
2492 calib_source_adjust = (data[1] >> 4) & 0xff;
2494 if (calib_source >= 8)
2496 devpriv->ai_calib_source = calib_source;
2497 if (devpriv->is_611x) {
2498 ni_writeb(dev, calib_source_adjust,
2499 NI611X_CAL_GAIN_SEL_REG);
2503 case INSN_CONFIG_GET_CMD_TIMING_CONSTRAINTS:
2504 /* we don't care about actual channels */
2505 /* data[3] : chanlist_len */
2506 data[1] = ni_min_ai_scan_period_ns(dev, data[3]);
2507 if (devpriv->is_611x || devpriv->is_6143)
2508 data[2] = 0; /* simultaneous output */
2510 data[2] = board->ai_speed;
2519 static void ni_ao_munge(struct comedi_device *dev, struct comedi_subdevice *s,
2520 void *data, unsigned int num_bytes,
2521 unsigned int chan_index)
2523 struct comedi_cmd *cmd = &s->async->cmd;
2524 unsigned int nsamples = comedi_bytes_to_samples(s, num_bytes);
2525 unsigned short *array = data;
2528 __le16 buf, *barray = data;
2531 for (i = 0; i < nsamples; i++) {
2532 unsigned int range = CR_RANGE(cmd->chanlist[chan_index]);
2533 unsigned short val = array[i];
2536 * Munge data from unsigned to two's complement for
2539 if (comedi_range_is_bipolar(s, range))
2540 val = comedi_offset_munge(s, val);
2542 buf = cpu_to_le16(val);
2548 chan_index %= cmd->chanlist_len;
2552 static int ni_m_series_ao_config_chanlist(struct comedi_device *dev,
2553 struct comedi_subdevice *s,
2554 unsigned int chanspec[],
2555 unsigned int n_chans, int timed)
2557 struct ni_private *devpriv = dev->private;
2565 for (i = 0; i < s->n_chan; ++i) {
2566 devpriv->ao_conf[i] &= ~NI_M_AO_CFG_BANK_UPDATE_TIMED;
2567 ni_writeb(dev, devpriv->ao_conf[i],
2568 NI_M_AO_CFG_BANK_REG(i));
2569 ni_writeb(dev, 0xf, NI_M_AO_WAVEFORM_ORDER_REG(i));
2572 for (i = 0; i < n_chans; i++) {
2573 const struct comedi_krange *krange;
2575 chan = CR_CHAN(chanspec[i]);
2576 range = CR_RANGE(chanspec[i]);
2577 krange = s->range_table->range + range;
2580 switch (krange->max - krange->min) {
2582 conf |= NI_M_AO_CFG_BANK_REF_INT_10V;
2583 ni_writeb(dev, 0, NI_M_AO_REF_ATTENUATION_REG(chan));
2586 conf |= NI_M_AO_CFG_BANK_REF_INT_5V;
2587 ni_writeb(dev, 0, NI_M_AO_REF_ATTENUATION_REG(chan));
2590 conf |= NI_M_AO_CFG_BANK_REF_INT_10V;
2591 ni_writeb(dev, NI_M_AO_REF_ATTENUATION_X5,
2592 NI_M_AO_REF_ATTENUATION_REG(chan));
2595 conf |= NI_M_AO_CFG_BANK_REF_INT_5V;
2596 ni_writeb(dev, NI_M_AO_REF_ATTENUATION_X5,
2597 NI_M_AO_REF_ATTENUATION_REG(chan));
2600 dev_err(dev->class_dev,
2601 "bug! unhandled ao reference voltage\n");
2604 switch (krange->max + krange->min) {
2606 conf |= NI_M_AO_CFG_BANK_OFFSET_0V;
2609 conf |= NI_M_AO_CFG_BANK_OFFSET_5V;
2612 dev_err(dev->class_dev,
2613 "bug! unhandled ao offset voltage\n");
2617 conf |= NI_M_AO_CFG_BANK_UPDATE_TIMED;
2618 ni_writeb(dev, conf, NI_M_AO_CFG_BANK_REG(chan));
2619 devpriv->ao_conf[chan] = conf;
2620 ni_writeb(dev, i, NI_M_AO_WAVEFORM_ORDER_REG(chan));
2625 static int ni_old_ao_config_chanlist(struct comedi_device *dev,
2626 struct comedi_subdevice *s,
2627 unsigned int chanspec[],
2628 unsigned int n_chans)
2630 struct ni_private *devpriv = dev->private;
2637 for (i = 0; i < n_chans; i++) {
2638 chan = CR_CHAN(chanspec[i]);
2639 range = CR_RANGE(chanspec[i]);
2640 conf = NI_E_AO_DACSEL(chan);
2642 if (comedi_range_is_bipolar(s, range)) {
2643 conf |= NI_E_AO_CFG_BIP;
2644 invert = (s->maxdata + 1) >> 1;
2648 if (comedi_range_is_external(s, range))
2649 conf |= NI_E_AO_EXT_REF;
2651 /* not all boards can deglitch, but this shouldn't hurt */
2652 if (chanspec[i] & CR_DEGLITCH)
2653 conf |= NI_E_AO_DEGLITCH;
2655 /* analog reference */
2656 /* AREF_OTHER connects AO ground to AI ground, i think */
2657 if (CR_AREF(chanspec[i]) == AREF_OTHER)
2658 conf |= NI_E_AO_GROUND_REF;
2660 ni_writew(dev, conf, NI_E_AO_CFG_REG);
2661 devpriv->ao_conf[chan] = conf;
2666 static int ni_ao_config_chanlist(struct comedi_device *dev,
2667 struct comedi_subdevice *s,
2668 unsigned int chanspec[], unsigned int n_chans,
2671 struct ni_private *devpriv = dev->private;
2673 if (devpriv->is_m_series)
2674 return ni_m_series_ao_config_chanlist(dev, s, chanspec, n_chans,
2677 return ni_old_ao_config_chanlist(dev, s, chanspec, n_chans);
2680 static int ni_ao_insn_write(struct comedi_device *dev,
2681 struct comedi_subdevice *s,
2682 struct comedi_insn *insn,
2685 struct ni_private *devpriv = dev->private;
2686 unsigned int chan = CR_CHAN(insn->chanspec);
2687 unsigned int range = CR_RANGE(insn->chanspec);
2691 if (devpriv->is_6xxx) {
2692 ni_ao_win_outw(dev, 1 << chan, NI671X_AO_IMMEDIATE_REG);
2694 reg = NI671X_DAC_DIRECT_DATA_REG(chan);
2695 } else if (devpriv->is_m_series) {
2696 reg = NI_M_DAC_DIRECT_DATA_REG(chan);
2698 reg = NI_E_DAC_DIRECT_DATA_REG(chan);
2701 ni_ao_config_chanlist(dev, s, &insn->chanspec, 1, 0);
2703 for (i = 0; i < insn->n; i++) {
2704 unsigned int val = data[i];
2706 s->readback[chan] = val;
2708 if (devpriv->is_6xxx) {
2710 * 6xxx boards have bipolar outputs, munge the
2711 * unsigned comedi values to 2's complement
2713 val = comedi_offset_munge(s, val);
2715 ni_ao_win_outw(dev, val, reg);
2716 } else if (devpriv->is_m_series) {
2718 * M-series boards use offset binary values for
2719 * bipolar and uinpolar outputs
2721 ni_writew(dev, val, reg);
2724 * Non-M series boards need two's complement values
2725 * for bipolar ranges.
2727 if (comedi_range_is_bipolar(s, range))
2728 val = comedi_offset_munge(s, val);
2730 ni_writew(dev, val, reg);
2738 * Arms the AO device in preparation for a trigger event.
2739 * This function also allocates and prepares a DMA channel (or FIFO if DMA is
2740 * not used). As a part of this preparation, this function preloads the DAC
2741 * registers with the first values of the output stream. This ensures that the
2742 * first clock cycle after the trigger can be used for output.
2744 * Note that this function _must_ happen after a user has written data to the
2745 * output buffers via either mmap or write(fileno,...).
2747 static int ni_ao_arm(struct comedi_device *dev,
2748 struct comedi_subdevice *s)
2750 struct ni_private *devpriv = dev->private;
2752 int interrupt_b_bits;
2754 static const int timeout = 1000;
2757 * Prevent ao from doing things like trying to allocate the ao dma
2758 * channel multiple times.
2760 if (!devpriv->ao_needs_arming) {
2761 dev_dbg(dev->class_dev, "%s: device does not need arming!\n",
2766 devpriv->ao_needs_arming = 0;
2768 ni_set_bits(dev, NISTC_INTB_ENA_REG,
2769 NISTC_INTB_ENA_AO_FIFO | NISTC_INTB_ENA_AO_ERR, 0);
2770 interrupt_b_bits = NISTC_INTB_ENA_AO_ERR;
2772 ni_stc_writew(dev, 1, NISTC_DAC_FIFO_CLR_REG);
2773 if (devpriv->is_6xxx)
2774 ni_ao_win_outl(dev, 0x6, NI611X_AO_FIFO_OFFSET_LOAD_REG);
2775 ret = ni_ao_setup_MITE_dma(dev);
2778 ret = ni_ao_wait_for_dma_load(dev);
2782 ret = ni_ao_prep_fifo(dev, s);
2786 interrupt_b_bits |= NISTC_INTB_ENA_AO_FIFO;
2789 ni_stc_writew(dev, devpriv->ao_mode3 | NISTC_AO_MODE3_NOT_AN_UPDATE,
2790 NISTC_AO_MODE3_REG);
2791 ni_stc_writew(dev, devpriv->ao_mode3, NISTC_AO_MODE3_REG);
2792 /* wait for DACs to be loaded */
2793 for (i = 0; i < timeout; i++) {
2795 if ((ni_stc_readw(dev, NISTC_STATUS2_REG) &
2796 NISTC_STATUS2_AO_TMRDACWRS_IN_PROGRESS) == 0)
2800 dev_err(dev->class_dev,
2801 "timed out waiting for AO_TMRDACWRs_In_Progress_St to clear\n");
2805 * stc manual says we are need to clear error interrupt after
2806 * AO_TMRDACWRs_In_Progress_St clears
2808 ni_stc_writew(dev, NISTC_INTB_ACK_AO_ERR, NISTC_INTB_ACK_REG);
2810 ni_set_bits(dev, NISTC_INTB_ENA_REG, interrupt_b_bits, 1);
2812 ni_stc_writew(dev, NISTC_AO_CMD1_UI_ARM |
2813 NISTC_AO_CMD1_UC_ARM |
2814 NISTC_AO_CMD1_BC_ARM |
2821 static int ni_ao_insn_config(struct comedi_device *dev,
2822 struct comedi_subdevice *s,
2823 struct comedi_insn *insn, unsigned int *data)
2825 const struct ni_board_struct *board = dev->board_ptr;
2826 struct ni_private *devpriv = dev->private;
2827 unsigned int nbytes;
2830 case INSN_CONFIG_GET_HARDWARE_BUFFER_SIZE:
2833 nbytes = comedi_samples_to_bytes(s,
2834 board->ao_fifo_depth);
2835 data[2] = 1 + nbytes;
2837 data[2] += devpriv->mite->fifo_size;
2846 case INSN_CONFIG_ARM:
2847 return ni_ao_arm(dev, s);
2848 case INSN_CONFIG_GET_CMD_TIMING_CONSTRAINTS:
2849 /* we don't care about actual channels */
2850 /* data[3] : chanlist_len */
2851 data[1] = board->ao_speed * data[3];
2861 static int ni_ao_inttrig(struct comedi_device *dev,
2862 struct comedi_subdevice *s,
2863 unsigned int trig_num)
2865 struct ni_private *devpriv = dev->private;
2866 struct comedi_cmd *cmd = &s->async->cmd;
2870 * Require trig_num == cmd->start_arg when cmd->start_src == TRIG_INT.
2871 * For backwards compatibility, also allow trig_num == 0 when
2872 * cmd->start_src != TRIG_INT (i.e. when cmd->start_src == TRIG_EXT);
2873 * in that case, the internal trigger is being used as a pre-trigger
2874 * before the external trigger.
2876 if (!(trig_num == cmd->start_arg ||
2877 (trig_num == 0 && cmd->start_src != TRIG_INT)))
2881 * Null trig at beginning prevent ao start trigger from executing more
2882 * than once per command.
2884 s->async->inttrig = NULL;
2886 if (devpriv->ao_needs_arming) {
2887 /* only arm this device if it still needs arming */
2888 ret = ni_ao_arm(dev, s);
2893 ni_stc_writew(dev, NISTC_AO_CMD2_START1_PULSE | devpriv->ao_cmd2,
2901 * Organized similar to NI-STC and MHDDK examples.
2902 * ni_ao_cmd is broken out into configuration sub-routines for clarity.
2905 static void ni_ao_cmd_personalize(struct comedi_device *dev,
2906 const struct comedi_cmd *cmd)
2908 const struct ni_board_struct *board = dev->board_ptr;
2911 ni_stc_writew(dev, NISTC_RESET_AO_CFG_START, NISTC_RESET_REG);
2914 /* fast CPU interface--only eseries */
2915 /* ((slow CPU interface) ? 0 : AO_Fast_CPU) | */
2916 NISTC_AO_PERSONAL_BC_SRC_SEL |
2917 0 /* (use_original_pulse ? 0 : NISTC_AO_PERSONAL_UPDATE_TIMEBASE) */ |
2919 * FIXME: start setting following bit when appropriate. Need to
2920 * determine whether board is E4 or E1.
2922 * if board is E4 or E1
2923 * Set bit "NISTC_AO_PERSONAL_UPDATE_PW" to 0
2927 NISTC_AO_PERSONAL_UPDATE_PW |
2928 /* FIXME: when should we set following bit to zero? */
2929 NISTC_AO_PERSONAL_TMRDACWR_PW |
2930 (board->ao_fifo_depth ?
2931 NISTC_AO_PERSONAL_FIFO_ENA : NISTC_AO_PERSONAL_DMA_PIO_CTRL)
2936 * add something like ".has_individual_dacs = 0" to ni_board_struct
2937 * since, as F Hess pointed out, not all in m series have singles. not
2938 * sure if e-series all have duals...
2942 * F Hess: windows driver does not set NISTC_AO_PERSONAL_NUM_DAC bit for
2943 * 6281, verified with bus analyzer.
2945 if (devpriv->is_m_series)
2946 bits |= NISTC_AO_PERSONAL_NUM_DAC;
2948 ni_stc_writew(dev, bits, NISTC_AO_PERSONAL_REG);
2950 ni_stc_writew(dev, NISTC_RESET_AO_CFG_END, NISTC_RESET_REG);
2953 static void ni_ao_cmd_set_trigger(struct comedi_device *dev,
2954 const struct comedi_cmd *cmd)
2956 struct ni_private *devpriv = dev->private;
2957 unsigned int trigsel;
2959 ni_stc_writew(dev, NISTC_RESET_AO_CFG_START, NISTC_RESET_REG);
2962 if (cmd->stop_src == TRIG_NONE) {
2963 devpriv->ao_mode1 |= NISTC_AO_MODE1_CONTINUOUS;
2964 devpriv->ao_mode1 &= ~NISTC_AO_MODE1_TRIGGER_ONCE;
2966 devpriv->ao_mode1 &= ~NISTC_AO_MODE1_CONTINUOUS;
2967 devpriv->ao_mode1 |= NISTC_AO_MODE1_TRIGGER_ONCE;
2969 ni_stc_writew(dev, devpriv->ao_mode1, NISTC_AO_MODE1_REG);
2971 if (cmd->start_src == TRIG_INT) {
2972 trigsel = NISTC_AO_TRIG_START1_EDGE |
2973 NISTC_AO_TRIG_START1_SYNC;
2974 } else { /* TRIG_EXT */
2975 trigsel = NISTC_AO_TRIG_START1_SEL(
2976 ni_get_reg_value_roffs(
2977 CR_CHAN(cmd->start_arg),
2979 &devpriv->routing_tables, 1));
2981 /* 0=active high, 1=active low. see daq-stc 3-24 (p186) */
2982 if (cmd->start_arg & CR_INVERT)
2983 trigsel |= NISTC_AO_TRIG_START1_POLARITY;
2984 /* 0=edge detection disabled, 1=enabled */
2985 if (cmd->start_arg & CR_EDGE)
2986 trigsel |= NISTC_AO_TRIG_START1_EDGE;
2988 ni_stc_writew(dev, trigsel, NISTC_AO_TRIG_SEL_REG);
2990 /* AO_Delayed_START1 = 0, we do not support delayed start...yet */
2993 /* select DA_START1 as PFI6/AO_START1 when configured as an output */
2994 devpriv->ao_mode3 &= ~NISTC_AO_MODE3_TRIG_LEN;
2995 ni_stc_writew(dev, devpriv->ao_mode3, NISTC_AO_MODE3_REG);
2997 ni_stc_writew(dev, NISTC_RESET_AO_CFG_END, NISTC_RESET_REG);
3000 static void ni_ao_cmd_set_counters(struct comedi_device *dev,
3001 const struct comedi_cmd *cmd)
3003 struct ni_private *devpriv = dev->private;
3004 /* Not supporting 'waveform staging' or 'local buffer with pauses' */
3006 ni_stc_writew(dev, NISTC_RESET_AO_CFG_START, NISTC_RESET_REG);
3008 * This relies on ao_mode1/(Trigger_Once | Continuous) being set in
3009 * set_trigger above. It is unclear whether we really need to re-write
3010 * this register with these values. The mhddk examples for e-series
3011 * show writing this in both places, but the examples for m-series show
3012 * a single write in the set_counters function (here).
3014 ni_stc_writew(dev, devpriv->ao_mode1, NISTC_AO_MODE1_REG);
3016 /* sync (upload number of buffer iterations -1) */
3017 /* indicate that we want to use BC_Load_A_Register as the source */
3018 devpriv->ao_mode2 &= ~NISTC_AO_MODE2_BC_INIT_LOAD_SRC;
3019 ni_stc_writew(dev, devpriv->ao_mode2, NISTC_AO_MODE2_REG);
3022 * if the BC_TC interrupt is still issued in spite of UC, BC, UI
3023 * ignoring BC_TC, then we will need to find a way to ignore that
3024 * interrupt in continuous mode.
3026 ni_stc_writel(dev, 0, NISTC_AO_BC_LOADA_REG); /* iter once */
3028 /* sync (issue command to load number of buffer iterations -1) */
3029 ni_stc_writew(dev, NISTC_AO_CMD1_BC_LOAD, NISTC_AO_CMD1_REG);
3031 /* sync (upload number of updates in buffer) */
3032 /* indicate that we want to use UC_Load_A_Register as the source */
3033 devpriv->ao_mode2 &= ~NISTC_AO_MODE2_UC_INIT_LOAD_SRC;
3034 ni_stc_writew(dev, devpriv->ao_mode2, NISTC_AO_MODE2_REG);
3037 * if a user specifies '0', this automatically assumes the entire 24bit
3038 * address space is available for the (multiple iterations of single
3039 * buffer) MISB. Otherwise, stop_arg specifies the MISB length that
3040 * will be used, regardless of whether we are in continuous mode or not.
3041 * In continuous mode, the output will just iterate indefinitely over
3045 unsigned int stop_arg = cmd->stop_arg > 0 ?
3046 (cmd->stop_arg & 0xffffff) : 0xffffff;
3048 if (devpriv->is_m_series) {
3050 * this is how the NI example code does it for m-series
3051 * boards, verified correct with 6259
3053 ni_stc_writel(dev, stop_arg - 1, NISTC_AO_UC_LOADA_REG);
3055 /* sync (issue cmd to load number of updates in MISB) */
3056 ni_stc_writew(dev, NISTC_AO_CMD1_UC_LOAD,
3059 ni_stc_writel(dev, stop_arg, NISTC_AO_UC_LOADA_REG);
3061 /* sync (issue cmd to load number of updates in MISB) */
3062 ni_stc_writew(dev, NISTC_AO_CMD1_UC_LOAD,
3066 * sync (upload number of updates-1 in MISB)
3069 ni_stc_writel(dev, stop_arg - 1, NISTC_AO_UC_LOADA_REG);
3073 ni_stc_writew(dev, NISTC_RESET_AO_CFG_END, NISTC_RESET_REG);
3076 static void ni_ao_cmd_set_update(struct comedi_device *dev,
3077 const struct comedi_cmd *cmd)
3079 struct ni_private *devpriv = dev->private;
3081 ni_stc_writew(dev, NISTC_RESET_AO_CFG_START, NISTC_RESET_REG);
3084 * zero out these bit fields to be set below. Does an ao-reset do this
3087 devpriv->ao_mode1 &= ~(NISTC_AO_MODE1_UI_SRC_MASK |
3088 NISTC_AO_MODE1_UI_SRC_POLARITY |
3089 NISTC_AO_MODE1_UPDATE_SRC_MASK |
3090 NISTC_AO_MODE1_UPDATE_SRC_POLARITY);
3092 if (cmd->scan_begin_src == TRIG_TIMER) {
3093 unsigned int trigvar;
3095 devpriv->ao_cmd2 &= ~NISTC_AO_CMD2_BC_GATE_ENA;
3098 * NOTE: there are several other ways of configuring internal
3099 * updates, but we'll only support one for now: using
3100 * AO_IN_TIMEBASE, w/o waveform staging, w/o a delay between
3101 * START1 and first update, and also w/o local buffer mode w/
3106 * This is already done above:
3107 * devpriv->ao_mode1 &= ~(
3108 * // set UPDATE_Source to UI_TC:
3109 * NISTC_AO_MODE1_UPDATE_SRC_MASK |
3110 * // set UPDATE_Source_Polarity to rising (required?)
3111 * NISTC_AO_MODE1_UPDATE_SRC_POLARITY |
3112 * // set UI_Source to AO_IN_TIMEBASE1:
3113 * NISTC_AO_MODE1_UI_SRC_MASK |
3114 * // set UI_Source_Polarity to rising (required?)
3115 * NISTC_AO_MODE1_UI_SRC_POLARITY
3120 * TODO: use ao_ui_clock_source to allow all possible signals
3121 * to be routed to UI_Source_Select. See tSTC.h for
3122 * eseries/ni67xx and tMSeries.h for mseries.
3125 trigvar = ni_ns_to_timer(dev, cmd->scan_begin_arg,
3126 CMDF_ROUND_NEAREST);
3129 * Wait N TB3 ticks after the start trigger before
3130 * clocking (N must be >=2).
3132 /* following line: 2-1 per STC */
3133 ni_stc_writel(dev, 1, NISTC_AO_UI_LOADA_REG);
3134 ni_stc_writew(dev, NISTC_AO_CMD1_UI_LOAD, NISTC_AO_CMD1_REG);
3135 ni_stc_writel(dev, trigvar, NISTC_AO_UI_LOADA_REG);
3136 } else { /* TRIG_EXT */
3137 /* FIXME: assert scan_begin_arg != 0, ret failure otherwise */
3138 devpriv->ao_cmd2 |= NISTC_AO_CMD2_BC_GATE_ENA;
3139 devpriv->ao_mode1 |= NISTC_AO_MODE1_UPDATE_SRC(
3141 CR_CHAN(cmd->scan_begin_arg),
3143 &devpriv->routing_tables));
3144 if (cmd->scan_begin_arg & CR_INVERT)
3145 devpriv->ao_mode1 |= NISTC_AO_MODE1_UPDATE_SRC_POLARITY;
3148 ni_stc_writew(dev, devpriv->ao_cmd2, NISTC_AO_CMD2_REG);
3149 ni_stc_writew(dev, devpriv->ao_mode1, NISTC_AO_MODE1_REG);
3150 devpriv->ao_mode2 &= ~(NISTC_AO_MODE2_UI_RELOAD_MODE(3) |
3151 NISTC_AO_MODE2_UI_INIT_LOAD_SRC);
3152 ni_stc_writew(dev, devpriv->ao_mode2, NISTC_AO_MODE2_REG);
3154 /* Configure DAQ-STC for Timed update mode */
3155 devpriv->ao_cmd1 |= NISTC_AO_CMD1_DAC1_UPDATE_MODE |
3156 NISTC_AO_CMD1_DAC0_UPDATE_MODE;
3157 /* We are not using UPDATE2-->don't have to set DACx_Source_Select */
3158 ni_stc_writew(dev, devpriv->ao_cmd1, NISTC_AO_CMD1_REG);
3160 ni_stc_writew(dev, NISTC_RESET_AO_CFG_END, NISTC_RESET_REG);
3163 static void ni_ao_cmd_set_channels(struct comedi_device *dev,
3164 struct comedi_subdevice *s)
3166 struct ni_private *devpriv = dev->private;
3167 const struct comedi_cmd *cmd = &s->async->cmd;
3168 unsigned int bits = 0;
3170 ni_stc_writew(dev, NISTC_RESET_AO_CFG_START, NISTC_RESET_REG);
3172 if (devpriv->is_6xxx) {
3176 for (i = 0; i < cmd->chanlist_len; ++i) {
3177 int chan = CR_CHAN(cmd->chanlist[i]);
3180 ni_ao_win_outw(dev, chan, NI611X_AO_WAVEFORM_GEN_REG);
3182 ni_ao_win_outw(dev, bits, NI611X_AO_TIMED_REG);
3185 ni_ao_config_chanlist(dev, s, cmd->chanlist, cmd->chanlist_len, 1);
3187 if (cmd->scan_end_arg > 1) {
3188 devpriv->ao_mode1 |= NISTC_AO_MODE1_MULTI_CHAN;
3189 bits = NISTC_AO_OUT_CTRL_CHANS(cmd->scan_end_arg - 1)
3190 | NISTC_AO_OUT_CTRL_UPDATE_SEL_HIGHZ;
3193 devpriv->ao_mode1 &= ~NISTC_AO_MODE1_MULTI_CHAN;
3194 bits = NISTC_AO_OUT_CTRL_UPDATE_SEL_HIGHZ;
3195 if (devpriv->is_m_series | devpriv->is_6xxx)
3196 bits |= NISTC_AO_OUT_CTRL_CHANS(0);
3198 bits |= NISTC_AO_OUT_CTRL_CHANS(
3199 CR_CHAN(cmd->chanlist[0]));
3202 ni_stc_writew(dev, devpriv->ao_mode1, NISTC_AO_MODE1_REG);
3203 ni_stc_writew(dev, bits, NISTC_AO_OUT_CTRL_REG);
3205 ni_stc_writew(dev, NISTC_RESET_AO_CFG_END, NISTC_RESET_REG);
3208 static void ni_ao_cmd_set_stop_conditions(struct comedi_device *dev,
3209 const struct comedi_cmd *cmd)
3211 struct ni_private *devpriv = dev->private;
3213 ni_stc_writew(dev, NISTC_RESET_AO_CFG_START, NISTC_RESET_REG);
3215 devpriv->ao_mode3 |= NISTC_AO_MODE3_STOP_ON_OVERRUN_ERR;
3216 ni_stc_writew(dev, devpriv->ao_mode3, NISTC_AO_MODE3_REG);
3219 * Since we are not supporting waveform staging, we ignore these errors:
3220 * NISTC_AO_MODE3_STOP_ON_BC_TC_ERR,
3221 * NISTC_AO_MODE3_STOP_ON_BC_TC_TRIG_ERR
3224 ni_stc_writew(dev, NISTC_RESET_AO_CFG_END, NISTC_RESET_REG);
3227 static void ni_ao_cmd_set_fifo_mode(struct comedi_device *dev)
3229 struct ni_private *devpriv = dev->private;
3231 ni_stc_writew(dev, NISTC_RESET_AO_CFG_START, NISTC_RESET_REG);
3233 devpriv->ao_mode2 &= ~NISTC_AO_MODE2_FIFO_MODE_MASK;
3235 devpriv->ao_mode2 |= NISTC_AO_MODE2_FIFO_MODE_HF_F;
3237 devpriv->ao_mode2 |= NISTC_AO_MODE2_FIFO_MODE_HF;
3239 /* NOTE: this is where use_onboard_memory=True would be implemented */
3240 devpriv->ao_mode2 &= ~NISTC_AO_MODE2_FIFO_REXMIT_ENA;
3241 ni_stc_writew(dev, devpriv->ao_mode2, NISTC_AO_MODE2_REG);
3243 /* enable sending of ao fifo requests (dma request) */
3244 ni_stc_writew(dev, NISTC_AO_START_AOFREQ_ENA, NISTC_AO_START_SEL_REG);
3246 ni_stc_writew(dev, NISTC_RESET_AO_CFG_END, NISTC_RESET_REG);
3248 /* we are not supporting boards with virtual fifos */
3251 static void ni_ao_cmd_set_interrupts(struct comedi_device *dev,
3252 struct comedi_subdevice *s)
3254 if (s->async->cmd.stop_src == TRIG_COUNT)
3255 ni_set_bits(dev, NISTC_INTB_ENA_REG,
3256 NISTC_INTB_ENA_AO_BC_TC, 1);
3258 s->async->inttrig = ni_ao_inttrig;
3261 static int ni_ao_cmd(struct comedi_device *dev, struct comedi_subdevice *s)
3263 struct ni_private *devpriv = dev->private;
3264 const struct comedi_cmd *cmd = &s->async->cmd;
3266 if (dev->irq == 0) {
3267 dev_err(dev->class_dev, "cannot run command without an irq");
3271 /* ni_ao_reset should have already been done */
3272 ni_ao_cmd_personalize(dev, cmd);
3273 /* clearing fifo and preload happens elsewhere */
3275 ni_ao_cmd_set_trigger(dev, cmd);
3276 ni_ao_cmd_set_counters(dev, cmd);
3277 ni_ao_cmd_set_update(dev, cmd);
3278 ni_ao_cmd_set_channels(dev, s);
3279 ni_ao_cmd_set_stop_conditions(dev, cmd);
3280 ni_ao_cmd_set_fifo_mode(dev);
3281 ni_cmd_set_mite_transfer(devpriv->ao_mite_ring, s, cmd, 0x00ffffff);
3282 ni_ao_cmd_set_interrupts(dev, s);
3285 * arm(ing) must happen later so that DMA can be setup and DACs
3286 * preloaded with the actual output buffer before starting.
3288 * start(ing) must happen _after_ arming is completed. Starting can be
3289 * done either via ni_ao_inttrig, or via an external trigger.
3291 * **Currently, ni_ao_inttrig will automatically attempt a call to
3292 * ni_ao_arm if the device still needs arming at that point. This
3293 * allows backwards compatibility.
3295 devpriv->ao_needs_arming = 1;
3301 static int ni_ao_cmdtest(struct comedi_device *dev, struct comedi_subdevice *s,
3302 struct comedi_cmd *cmd)
3304 const struct ni_board_struct *board = dev->board_ptr;
3305 struct ni_private *devpriv = dev->private;
3309 /* Step 1 : check if triggers are trivially valid */
3311 err |= comedi_check_trigger_src(&cmd->start_src, TRIG_INT | TRIG_EXT);
3312 err |= comedi_check_trigger_src(&cmd->scan_begin_src,
3313 TRIG_TIMER | TRIG_EXT);
3314 err |= comedi_check_trigger_src(&cmd->convert_src, TRIG_NOW);
3315 err |= comedi_check_trigger_src(&cmd->scan_end_src, TRIG_COUNT);
3316 err |= comedi_check_trigger_src(&cmd->stop_src, TRIG_COUNT | TRIG_NONE);
3321 /* Step 2a : make sure trigger sources are unique */
3323 err |= comedi_check_trigger_is_unique(cmd->start_src);
3324 err |= comedi_check_trigger_is_unique(cmd->scan_begin_src);
3325 err |= comedi_check_trigger_is_unique(cmd->stop_src);
3327 /* Step 2b : and mutually compatible */
3332 /* Step 3: check if arguments are trivially valid */
3334 switch (cmd->start_src) {
3336 err |= comedi_check_trigger_arg_is(&cmd->start_arg, 0);
3339 err |= ni_check_trigger_arg_roffs(CR_CHAN(cmd->start_arg),
3341 &devpriv->routing_tables, 1);
3345 if (cmd->scan_begin_src == TRIG_TIMER) {
3346 err |= comedi_check_trigger_arg_min(&cmd->scan_begin_arg,
3348 err |= comedi_check_trigger_arg_max(&cmd->scan_begin_arg,
3351 } else { /* TRIG_EXT */
3352 err |= ni_check_trigger_arg(CR_CHAN(cmd->scan_begin_arg),
3354 &devpriv->routing_tables);
3357 err |= comedi_check_trigger_arg_is(&cmd->convert_arg, 0);
3358 err |= comedi_check_trigger_arg_is(&cmd->scan_end_arg,
3360 err |= comedi_check_trigger_arg_max(&cmd->stop_arg, 0x00ffffff);
3365 /* step 4: fix up any arguments */
3366 if (cmd->scan_begin_src == TRIG_TIMER) {
3367 tmp = cmd->scan_begin_arg;
3368 cmd->scan_begin_arg =
3369 ni_timer_to_ns(dev, ni_ns_to_timer(dev,
3370 cmd->scan_begin_arg,
3372 if (tmp != cmd->scan_begin_arg)
3381 static int ni_ao_reset(struct comedi_device *dev, struct comedi_subdevice *s)
3383 /* See 3.6.1.2 "Resetting", of DAQ-STC Technical Reference Manual */
3386 * In the following, the "--sync" comments are meant to denote
3387 * asynchronous boundaries for setting the registers as described in the
3388 * DAQ-STC mostly in the order also described in the DAQ-STC.
3391 struct ni_private *devpriv = dev->private;
3393 ni_release_ao_mite_channel(dev);
3395 /* --sync (reset AO) */
3396 if (devpriv->is_m_series)
3397 /* following example in mhddk for m-series */
3398 ni_stc_writew(dev, NISTC_RESET_AO, NISTC_RESET_REG);
3400 /*--sync (start config) */
3401 ni_stc_writew(dev, NISTC_RESET_AO_CFG_START, NISTC_RESET_REG);
3403 /*--sync (Disarm) */
3404 ni_stc_writew(dev, NISTC_AO_CMD1_DISARM, NISTC_AO_CMD1_REG);
3408 * (clear bunch of registers--mseries mhddk examples do not include
3411 devpriv->ao_cmd1 = 0;
3412 devpriv->ao_cmd2 = 0;
3413 devpriv->ao_mode1 = 0;
3414 devpriv->ao_mode2 = 0;
3415 if (devpriv->is_m_series)
3416 devpriv->ao_mode3 = NISTC_AO_MODE3_LAST_GATE_DISABLE;
3418 devpriv->ao_mode3 = 0;
3420 ni_stc_writew(dev, 0, NISTC_AO_PERSONAL_REG);
3421 ni_stc_writew(dev, 0, NISTC_AO_CMD1_REG);
3422 ni_stc_writew(dev, 0, NISTC_AO_CMD2_REG);
3423 ni_stc_writew(dev, 0, NISTC_AO_MODE1_REG);
3424 ni_stc_writew(dev, 0, NISTC_AO_MODE2_REG);
3425 ni_stc_writew(dev, 0, NISTC_AO_OUT_CTRL_REG);
3426 ni_stc_writew(dev, devpriv->ao_mode3, NISTC_AO_MODE3_REG);
3427 ni_stc_writew(dev, 0, NISTC_AO_START_SEL_REG);
3428 ni_stc_writew(dev, 0, NISTC_AO_TRIG_SEL_REG);
3430 /*--sync (disable interrupts) */
3431 ni_set_bits(dev, NISTC_INTB_ENA_REG, ~0, 0);
3434 ni_stc_writew(dev, NISTC_AO_PERSONAL_BC_SRC_SEL, NISTC_AO_PERSONAL_REG);
3435 ni_stc_writew(dev, NISTC_INTB_ACK_AO_ALL, NISTC_INTB_ACK_REG);
3437 /*--not in DAQ-STC. which doc? */
3438 if (devpriv->is_6xxx) {
3439 ni_ao_win_outw(dev, (1u << s->n_chan) - 1u,
3440 NI671X_AO_IMMEDIATE_REG);
3441 ni_ao_win_outw(dev, NI611X_AO_MISC_CLEAR_WG,
3442 NI611X_AO_MISC_REG);
3444 ni_stc_writew(dev, NISTC_RESET_AO_CFG_END, NISTC_RESET_REG);
3452 static int ni_dio_insn_config(struct comedi_device *dev,
3453 struct comedi_subdevice *s,
3454 struct comedi_insn *insn,
3457 struct ni_private *devpriv = dev->private;
3460 ret = comedi_dio_insn_config(dev, s, insn, data, 0);
3464 devpriv->dio_control &= ~NISTC_DIO_CTRL_DIR_MASK;
3465 devpriv->dio_control |= NISTC_DIO_CTRL_DIR(s->io_bits);
3466 ni_stc_writew(dev, devpriv->dio_control, NISTC_DIO_CTRL_REG);
3471 static int ni_dio_insn_bits(struct comedi_device *dev,
3472 struct comedi_subdevice *s,
3473 struct comedi_insn *insn,
3476 struct ni_private *devpriv = dev->private;
3478 /* Make sure we're not using the serial part of the dio */
3479 if ((data[0] & (NISTC_DIO_SDIN | NISTC_DIO_SDOUT)) &&
3480 devpriv->serial_interval_ns)
3483 if (comedi_dio_update_state(s, data)) {
3484 devpriv->dio_output &= ~NISTC_DIO_OUT_PARALLEL_MASK;
3485 devpriv->dio_output |= NISTC_DIO_OUT_PARALLEL(s->state);
3486 ni_stc_writew(dev, devpriv->dio_output, NISTC_DIO_OUT_REG);
3489 data[1] = ni_stc_readw(dev, NISTC_DIO_IN_REG);
3495 static int ni_m_series_dio_insn_config(struct comedi_device *dev,
3496 struct comedi_subdevice *s,
3497 struct comedi_insn *insn,
3502 if (data[0] == INSN_CONFIG_GET_CMD_TIMING_CONSTRAINTS) {
3503 const struct ni_board_struct *board = dev->board_ptr;
3505 /* we don't care about actual channels */
3506 data[1] = board->dio_speed;
3511 ret = comedi_dio_insn_config(dev, s, insn, data, 0);
3515 ni_writel(dev, s->io_bits, NI_M_DIO_DIR_REG);
3520 static int ni_m_series_dio_insn_bits(struct comedi_device *dev,
3521 struct comedi_subdevice *s,
3522 struct comedi_insn *insn,
3525 if (comedi_dio_update_state(s, data))
3526 ni_writel(dev, s->state, NI_M_DIO_REG);
3528 data[1] = ni_readl(dev, NI_M_DIO_REG);
3533 static int ni_cdio_check_chanlist(struct comedi_device *dev,
3534 struct comedi_subdevice *s,
3535 struct comedi_cmd *cmd)
3539 for (i = 0; i < cmd->chanlist_len; ++i) {
3540 unsigned int chan = CR_CHAN(cmd->chanlist[i]);
3549 static int ni_cdio_cmdtest(struct comedi_device *dev,
3550 struct comedi_subdevice *s, struct comedi_cmd *cmd)
3552 struct ni_private *devpriv = dev->private;
3553 unsigned int bytes_per_scan;
3556 /* Step 1 : check if triggers are trivially valid */
3558 err |= comedi_check_trigger_src(&cmd->start_src, TRIG_INT);
3559 err |= comedi_check_trigger_src(&cmd->scan_begin_src, TRIG_EXT);
3560 err |= comedi_check_trigger_src(&cmd->convert_src, TRIG_NOW);
3561 err |= comedi_check_trigger_src(&cmd->scan_end_src, TRIG_COUNT);
3562 err |= comedi_check_trigger_src(&cmd->stop_src, TRIG_NONE);
3567 /* Step 2a : make sure trigger sources are unique */
3568 /* Step 2b : and mutually compatible */
3570 /* Step 3: check if arguments are trivially valid */
3572 err |= comedi_check_trigger_arg_is(&cmd->start_arg, 0);
3575 * Although NI_D[IO]_SampleClock are the same, perhaps we should still,
3576 * for completeness, test whether the cmd is output or input?
3578 err |= ni_check_trigger_arg(CR_CHAN(cmd->scan_begin_arg),
3580 &devpriv->routing_tables);
3581 if (CR_RANGE(cmd->scan_begin_arg) != 0 ||
3582 CR_AREF(cmd->scan_begin_arg) != 0)
3585 err |= comedi_check_trigger_arg_is(&cmd->convert_arg, 0);
3586 err |= comedi_check_trigger_arg_is(&cmd->scan_end_arg,
3588 bytes_per_scan = comedi_bytes_per_scan_cmd(s, cmd);
3589 if (bytes_per_scan) {
3590 err |= comedi_check_trigger_arg_max(&cmd->stop_arg,
3591 s->async->prealloc_bufsz /
3598 /* Step 4: fix up any arguments */
3600 /* Step 5: check channel list if it exists */
3602 if (cmd->chanlist && cmd->chanlist_len > 0)
3603 err |= ni_cdio_check_chanlist(dev, s, cmd);
3611 static int ni_cdo_inttrig(struct comedi_device *dev,
3612 struct comedi_subdevice *s,
3613 unsigned int trig_num)
3615 struct comedi_cmd *cmd = &s->async->cmd;
3616 const unsigned int timeout = 1000;
3619 struct ni_private *devpriv = dev->private;
3620 unsigned long flags;
3622 if (trig_num != cmd->start_arg)
3625 s->async->inttrig = NULL;
3627 /* read alloc the entire buffer */
3628 comedi_buf_read_alloc(s, s->async->prealloc_bufsz);
3630 spin_lock_irqsave(&devpriv->mite_channel_lock, flags);
3631 if (devpriv->cdo_mite_chan) {
3632 mite_prep_dma(devpriv->cdo_mite_chan, 32, 32);
3633 mite_dma_arm(devpriv->cdo_mite_chan);
3635 dev_err(dev->class_dev, "BUG: no cdo mite channel?\n");
3638 spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
3643 * XXX not sure what interrupt C group does
3644 * wait for dma to fill output fifo
3645 * ni_writeb(dev, NI_M_INTC_ENA, NI_M_INTC_ENA_REG);
3647 for (i = 0; i < timeout; ++i) {
3648 if (ni_readl(dev, NI_M_CDIO_STATUS_REG) &
3649 NI_M_CDIO_STATUS_CDO_FIFO_FULL)
3651 usleep_range(10, 100);
3654 dev_err(dev->class_dev, "dma failed to fill cdo fifo!\n");
3658 ni_writel(dev, NI_M_CDO_CMD_ARM |
3659 NI_M_CDO_CMD_ERR_INT_ENA_SET |
3660 NI_M_CDO_CMD_F_E_INT_ENA_SET,
3665 static int ni_cdio_cmd(struct comedi_device *dev, struct comedi_subdevice *s)
3667 struct ni_private *devpriv = dev->private;
3668 const struct comedi_cmd *cmd = &s->async->cmd;
3669 unsigned int cdo_mode_bits;
3672 ni_writel(dev, NI_M_CDO_CMD_RESET, NI_M_CDIO_CMD_REG);
3674 * Although NI_D[IO]_SampleClock are the same, perhaps we should still,
3675 * for completeness, test whether the cmd is output or input(?)
3677 cdo_mode_bits = NI_M_CDO_MODE_FIFO_MODE |
3678 NI_M_CDO_MODE_HALT_ON_ERROR |
3679 NI_M_CDO_MODE_SAMPLE_SRC(
3681 CR_CHAN(cmd->scan_begin_arg),
3683 &devpriv->routing_tables));
3684 if (cmd->scan_begin_arg & CR_INVERT)
3685 cdo_mode_bits |= NI_M_CDO_MODE_POLARITY;
3686 ni_writel(dev, cdo_mode_bits, NI_M_CDO_MODE_REG);
3688 ni_writel(dev, s->state, NI_M_CDO_FIFO_DATA_REG);
3689 ni_writel(dev, NI_M_CDO_CMD_SW_UPDATE, NI_M_CDIO_CMD_REG);
3690 ni_writel(dev, s->io_bits, NI_M_CDO_MASK_ENA_REG);
3692 dev_err(dev->class_dev,
3693 "attempted to run digital output command with no lines configured as outputs\n");
3696 retval = ni_request_cdo_mite_channel(dev);
3700 ni_cmd_set_mite_transfer(devpriv->cdo_mite_ring, s, cmd,
3701 s->async->prealloc_bufsz /
3702 comedi_bytes_per_scan(s));
3704 s->async->inttrig = ni_cdo_inttrig;
3709 static int ni_cdio_cancel(struct comedi_device *dev, struct comedi_subdevice *s)
3711 ni_writel(dev, NI_M_CDO_CMD_DISARM |
3712 NI_M_CDO_CMD_ERR_INT_ENA_CLR |
3713 NI_M_CDO_CMD_F_E_INT_ENA_CLR |
3714 NI_M_CDO_CMD_F_REQ_INT_ENA_CLR,
3717 * XXX not sure what interrupt C group does
3718 * ni_writeb(dev, 0, NI_M_INTC_ENA_REG);
3720 ni_writel(dev, 0, NI_M_CDO_MASK_ENA_REG);
3721 ni_release_cdo_mite_channel(dev);
3725 static void handle_cdio_interrupt(struct comedi_device *dev)
3727 struct ni_private *devpriv = dev->private;
3728 unsigned int cdio_status;
3729 struct comedi_subdevice *s = &dev->subdevices[NI_DIO_SUBDEV];
3730 unsigned long flags;
3732 spin_lock_irqsave(&devpriv->mite_channel_lock, flags);
3733 if (devpriv->cdo_mite_chan)
3734 mite_ack_linkc(devpriv->cdo_mite_chan, s, true);
3735 spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
3737 cdio_status = ni_readl(dev, NI_M_CDIO_STATUS_REG);
3738 if (cdio_status & NI_M_CDIO_STATUS_CDO_ERROR) {
3739 /* XXX just guessing this is needed and does something useful */
3740 ni_writel(dev, NI_M_CDO_CMD_ERR_INT_CONFIRM,
3742 s->async->events |= COMEDI_CB_OVERFLOW;
3744 if (cdio_status & NI_M_CDIO_STATUS_CDO_FIFO_EMPTY) {
3745 ni_writel(dev, NI_M_CDO_CMD_F_E_INT_ENA_CLR,
3747 /* s->async->events |= COMEDI_CB_EOA; */
3749 comedi_handle_events(dev, s);
3753 static int ni_serial_hw_readwrite8(struct comedi_device *dev,
3754 struct comedi_subdevice *s,
3755 unsigned char data_out,
3756 unsigned char *data_in)
3758 struct ni_private *devpriv = dev->private;
3759 unsigned int status1;
3760 int err = 0, count = 20;
3762 devpriv->dio_output &= ~NISTC_DIO_OUT_SERIAL_MASK;
3763 devpriv->dio_output |= NISTC_DIO_OUT_SERIAL(data_out);
3764 ni_stc_writew(dev, devpriv->dio_output, NISTC_DIO_OUT_REG);
3766 status1 = ni_stc_readw(dev, NISTC_STATUS1_REG);
3767 if (status1 & NISTC_STATUS1_SERIO_IN_PROG) {
3772 devpriv->dio_control |= NISTC_DIO_CTRL_HW_SER_START;
3773 ni_stc_writew(dev, devpriv->dio_control, NISTC_DIO_CTRL_REG);
3774 devpriv->dio_control &= ~NISTC_DIO_CTRL_HW_SER_START;
3776 /* Wait until STC says we're done, but don't loop infinitely. */
3777 while ((status1 = ni_stc_readw(dev, NISTC_STATUS1_REG)) &
3778 NISTC_STATUS1_SERIO_IN_PROG) {
3779 /* Delay one bit per loop */
3780 udelay((devpriv->serial_interval_ns + 999) / 1000);
3782 dev_err(dev->class_dev,
3783 "SPI serial I/O didn't finish in time!\n");
3790 * Delay for last bit. This delay is absolutely necessary, because
3791 * NISTC_STATUS1_SERIO_IN_PROG goes high one bit too early.
3793 udelay((devpriv->serial_interval_ns + 999) / 1000);
3796 *data_in = ni_stc_readw(dev, NISTC_DIO_SERIAL_IN_REG);
3799 ni_stc_writew(dev, devpriv->dio_control, NISTC_DIO_CTRL_REG);
3804 static int ni_serial_sw_readwrite8(struct comedi_device *dev,
3805 struct comedi_subdevice *s,
3806 unsigned char data_out,
3807 unsigned char *data_in)
3809 struct ni_private *devpriv = dev->private;
3810 unsigned char mask, input = 0;
3812 /* Wait for one bit before transfer */
3813 udelay((devpriv->serial_interval_ns + 999) / 1000);
3815 for (mask = 0x80; mask; mask >>= 1) {
3817 * Output current bit; note that we cannot touch s->state
3818 * because it is a per-subdevice field, and serial is
3819 * a separate subdevice from DIO.
3821 devpriv->dio_output &= ~NISTC_DIO_SDOUT;
3822 if (data_out & mask)
3823 devpriv->dio_output |= NISTC_DIO_SDOUT;
3824 ni_stc_writew(dev, devpriv->dio_output, NISTC_DIO_OUT_REG);
3827 * Assert SDCLK (active low, inverted), wait for half of
3828 * the delay, deassert SDCLK, and wait for the other half.
3830 devpriv->dio_control |= NISTC_DIO_SDCLK;
3831 ni_stc_writew(dev, devpriv->dio_control, NISTC_DIO_CTRL_REG);
3833 udelay((devpriv->serial_interval_ns + 999) / 2000);
3835 devpriv->dio_control &= ~NISTC_DIO_SDCLK;
3836 ni_stc_writew(dev, devpriv->dio_control, NISTC_DIO_CTRL_REG);
3838 udelay((devpriv->serial_interval_ns + 999) / 2000);
3840 /* Input current bit */
3841 if (ni_stc_readw(dev, NISTC_DIO_IN_REG) & NISTC_DIO_SDIN)
3851 static int ni_serial_insn_config(struct comedi_device *dev,
3852 struct comedi_subdevice *s,
3853 struct comedi_insn *insn,
3856 struct ni_private *devpriv = dev->private;
3857 unsigned int clk_fout = devpriv->clock_and_fout;
3859 unsigned char byte_out, byte_in = 0;
3865 case INSN_CONFIG_SERIAL_CLOCK:
3866 devpriv->serial_hw_mode = 1;
3867 devpriv->dio_control |= NISTC_DIO_CTRL_HW_SER_ENA;
3869 if (data[1] == SERIAL_DISABLED) {
3870 devpriv->serial_hw_mode = 0;
3871 devpriv->dio_control &= ~(NISTC_DIO_CTRL_HW_SER_ENA |
3873 data[1] = SERIAL_DISABLED;
3874 devpriv->serial_interval_ns = data[1];
3875 } else if (data[1] <= SERIAL_600NS) {
3877 * Warning: this clock speed is too fast to reliably
3880 devpriv->dio_control &= ~NISTC_DIO_CTRL_HW_SER_TIMEBASE;
3881 clk_fout |= NISTC_CLK_FOUT_SLOW_TIMEBASE;
3882 clk_fout &= ~NISTC_CLK_FOUT_DIO_SER_OUT_DIV2;
3883 data[1] = SERIAL_600NS;
3884 devpriv->serial_interval_ns = data[1];
3885 } else if (data[1] <= SERIAL_1_2US) {
3886 devpriv->dio_control &= ~NISTC_DIO_CTRL_HW_SER_TIMEBASE;
3887 clk_fout |= NISTC_CLK_FOUT_SLOW_TIMEBASE |
3888 NISTC_CLK_FOUT_DIO_SER_OUT_DIV2;
3889 data[1] = SERIAL_1_2US;
3890 devpriv->serial_interval_ns = data[1];
3891 } else if (data[1] <= SERIAL_10US) {
3892 devpriv->dio_control |= NISTC_DIO_CTRL_HW_SER_TIMEBASE;
3893 clk_fout |= NISTC_CLK_FOUT_SLOW_TIMEBASE |
3894 NISTC_CLK_FOUT_DIO_SER_OUT_DIV2;
3896 * Note: NISTC_CLK_FOUT_DIO_SER_OUT_DIV2 only affects
3897 * 600ns/1.2us. If you turn divide_by_2 off with the
3898 * slow clock, you will still get 10us, except then
3899 * all your delays are wrong.
3901 data[1] = SERIAL_10US;
3902 devpriv->serial_interval_ns = data[1];
3904 devpriv->dio_control &= ~(NISTC_DIO_CTRL_HW_SER_ENA |
3906 devpriv->serial_hw_mode = 0;
3907 data[1] = (data[1] / 1000) * 1000;
3908 devpriv->serial_interval_ns = data[1];
3910 devpriv->clock_and_fout = clk_fout;
3912 ni_stc_writew(dev, devpriv->dio_control, NISTC_DIO_CTRL_REG);
3913 ni_stc_writew(dev, devpriv->clock_and_fout, NISTC_CLK_FOUT_REG);
3916 case INSN_CONFIG_BIDIRECTIONAL_DATA:
3918 if (devpriv->serial_interval_ns == 0)
3921 byte_out = data[1] & 0xFF;
3923 if (devpriv->serial_hw_mode) {
3924 err = ni_serial_hw_readwrite8(dev, s, byte_out,
3926 } else if (devpriv->serial_interval_ns > 0) {
3927 err = ni_serial_sw_readwrite8(dev, s, byte_out,
3930 dev_err(dev->class_dev, "serial disabled!\n");
3935 data[1] = byte_in & 0xFF;
3944 static void init_ao_67xx(struct comedi_device *dev, struct comedi_subdevice *s)
3948 for (i = 0; i < s->n_chan; i++) {
3949 ni_ao_win_outw(dev, NI_E_AO_DACSEL(i) | 0x0,
3950 NI67XX_AO_CFG2_REG);
3952 ni_ao_win_outw(dev, 0x0, NI67XX_AO_SP_UPDATES_REG);
3955 static const struct mio_regmap ni_gpct_to_stc_regmap[] = {
3956 [NITIO_G0_AUTO_INC] = { NISTC_G0_AUTOINC_REG, 2 },
3957 [NITIO_G1_AUTO_INC] = { NISTC_G1_AUTOINC_REG, 2 },
3958 [NITIO_G0_CMD] = { NISTC_G0_CMD_REG, 2 },
3959 [NITIO_G1_CMD] = { NISTC_G1_CMD_REG, 2 },
3960 [NITIO_G0_HW_SAVE] = { NISTC_G0_HW_SAVE_REG, 4 },
3961 [NITIO_G1_HW_SAVE] = { NISTC_G1_HW_SAVE_REG, 4 },
3962 [NITIO_G0_SW_SAVE] = { NISTC_G0_SAVE_REG, 4 },
3963 [NITIO_G1_SW_SAVE] = { NISTC_G1_SAVE_REG, 4 },
3964 [NITIO_G0_MODE] = { NISTC_G0_MODE_REG, 2 },
3965 [NITIO_G1_MODE] = { NISTC_G1_MODE_REG, 2 },
3966 [NITIO_G0_LOADA] = { NISTC_G0_LOADA_REG, 4 },
3967 [NITIO_G1_LOADA] = { NISTC_G1_LOADA_REG, 4 },
3968 [NITIO_G0_LOADB] = { NISTC_G0_LOADB_REG, 4 },
3969 [NITIO_G1_LOADB] = { NISTC_G1_LOADB_REG, 4 },
3970 [NITIO_G0_INPUT_SEL] = { NISTC_G0_INPUT_SEL_REG, 2 },
3971 [NITIO_G1_INPUT_SEL] = { NISTC_G1_INPUT_SEL_REG, 2 },
3972 [NITIO_G0_CNT_MODE] = { 0x1b0, 2 }, /* M-Series only */
3973 [NITIO_G1_CNT_MODE] = { 0x1b2, 2 }, /* M-Series only */
3974 [NITIO_G0_GATE2] = { 0x1b4, 2 }, /* M-Series only */
3975 [NITIO_G1_GATE2] = { 0x1b6, 2 }, /* M-Series only */
3976 [NITIO_G01_STATUS] = { NISTC_G01_STATUS_REG, 2 },
3977 [NITIO_G01_RESET] = { NISTC_RESET_REG, 2 },
3978 [NITIO_G01_STATUS1] = { NISTC_STATUS1_REG, 2 },
3979 [NITIO_G01_STATUS2] = { NISTC_STATUS2_REG, 2 },
3980 [NITIO_G0_DMA_CFG] = { 0x1b8, 2 }, /* M-Series only */
3981 [NITIO_G1_DMA_CFG] = { 0x1ba, 2 }, /* M-Series only */
3982 [NITIO_G0_DMA_STATUS] = { 0x1b8, 2 }, /* M-Series only */
3983 [NITIO_G1_DMA_STATUS] = { 0x1ba, 2 }, /* M-Series only */
3984 [NITIO_G0_ABZ] = { 0x1c0, 2 }, /* M-Series only */
3985 [NITIO_G1_ABZ] = { 0x1c2, 2 }, /* M-Series only */
3986 [NITIO_G0_INT_ACK] = { NISTC_INTA_ACK_REG, 2 },
3987 [NITIO_G1_INT_ACK] = { NISTC_INTB_ACK_REG, 2 },
3988 [NITIO_G0_STATUS] = { NISTC_AI_STATUS1_REG, 2 },
3989 [NITIO_G1_STATUS] = { NISTC_AO_STATUS1_REG, 2 },
3990 [NITIO_G0_INT_ENA] = { NISTC_INTA_ENA_REG, 2 },
3991 [NITIO_G1_INT_ENA] = { NISTC_INTB_ENA_REG, 2 },
3994 static unsigned int ni_gpct_to_stc_register(struct comedi_device *dev,
3995 enum ni_gpct_register reg)
3997 const struct mio_regmap *regmap;
3999 if (reg < ARRAY_SIZE(ni_gpct_to_stc_regmap)) {
4000 regmap = &ni_gpct_to_stc_regmap[reg];
4002 dev_warn(dev->class_dev, "%s: unhandled register=0x%x\n",
4007 return regmap->mio_reg;
4010 static void ni_gpct_write_register(struct ni_gpct *counter, unsigned int bits,
4011 enum ni_gpct_register reg)
4013 struct comedi_device *dev = counter->counter_dev->dev;
4014 unsigned int stc_register = ni_gpct_to_stc_register(dev, reg);
4016 if (stc_register == 0)
4020 /* m-series only registers */
4021 case NITIO_G0_CNT_MODE:
4022 case NITIO_G1_CNT_MODE:
4023 case NITIO_G0_GATE2:
4024 case NITIO_G1_GATE2:
4025 case NITIO_G0_DMA_CFG:
4026 case NITIO_G1_DMA_CFG:
4029 ni_writew(dev, bits, stc_register);
4032 /* 32 bit registers */
4033 case NITIO_G0_LOADA:
4034 case NITIO_G1_LOADA:
4035 case NITIO_G0_LOADB:
4036 case NITIO_G1_LOADB:
4037 ni_stc_writel(dev, bits, stc_register);
4040 /* 16 bit registers */
4041 case NITIO_G0_INT_ENA:
4042 ni_set_bitfield(dev, stc_register,
4043 NISTC_INTA_ENA_G0_GATE | NISTC_INTA_ENA_G0_TC,
4046 case NITIO_G1_INT_ENA:
4047 ni_set_bitfield(dev, stc_register,
4048 NISTC_INTB_ENA_G1_GATE | NISTC_INTB_ENA_G1_TC,
4052 ni_stc_writew(dev, bits, stc_register);
4056 static unsigned int ni_gpct_read_register(struct ni_gpct *counter,
4057 enum ni_gpct_register reg)
4059 struct comedi_device *dev = counter->counter_dev->dev;
4060 unsigned int stc_register = ni_gpct_to_stc_register(dev, reg);
4062 if (stc_register == 0)
4066 /* m-series only registers */
4067 case NITIO_G0_DMA_STATUS:
4068 case NITIO_G1_DMA_STATUS:
4069 return ni_readw(dev, stc_register);
4071 /* 32 bit registers */
4072 case NITIO_G0_HW_SAVE:
4073 case NITIO_G1_HW_SAVE:
4074 case NITIO_G0_SW_SAVE:
4075 case NITIO_G1_SW_SAVE:
4076 return ni_stc_readl(dev, stc_register);
4078 /* 16 bit registers */
4080 return ni_stc_readw(dev, stc_register);
4084 static int ni_freq_out_insn_read(struct comedi_device *dev,
4085 struct comedi_subdevice *s,
4086 struct comedi_insn *insn,
4089 struct ni_private *devpriv = dev->private;
4090 unsigned int val = NISTC_CLK_FOUT_TO_DIVIDER(devpriv->clock_and_fout);
4093 for (i = 0; i < insn->n; i++)
4099 static int ni_freq_out_insn_write(struct comedi_device *dev,
4100 struct comedi_subdevice *s,
4101 struct comedi_insn *insn,
4104 struct ni_private *devpriv = dev->private;
4107 unsigned int val = data[insn->n - 1];
4109 devpriv->clock_and_fout &= ~NISTC_CLK_FOUT_ENA;
4110 ni_stc_writew(dev, devpriv->clock_and_fout, NISTC_CLK_FOUT_REG);
4111 devpriv->clock_and_fout &= ~NISTC_CLK_FOUT_DIVIDER_MASK;
4113 /* use the last data value to set the fout divider */
4114 devpriv->clock_and_fout |= NISTC_CLK_FOUT_DIVIDER(val);
4116 devpriv->clock_and_fout |= NISTC_CLK_FOUT_ENA;
4117 ni_stc_writew(dev, devpriv->clock_and_fout, NISTC_CLK_FOUT_REG);
4122 static int ni_freq_out_insn_config(struct comedi_device *dev,
4123 struct comedi_subdevice *s,
4124 struct comedi_insn *insn,
4127 struct ni_private *devpriv = dev->private;
4130 case INSN_CONFIG_SET_CLOCK_SRC:
4132 case NI_FREQ_OUT_TIMEBASE_1_DIV_2_CLOCK_SRC:
4133 devpriv->clock_and_fout &= ~NISTC_CLK_FOUT_TIMEBASE_SEL;
4135 case NI_FREQ_OUT_TIMEBASE_2_CLOCK_SRC:
4136 devpriv->clock_and_fout |= NISTC_CLK_FOUT_TIMEBASE_SEL;
4141 ni_stc_writew(dev, devpriv->clock_and_fout, NISTC_CLK_FOUT_REG);
4143 case INSN_CONFIG_GET_CLOCK_SRC:
4144 if (devpriv->clock_and_fout & NISTC_CLK_FOUT_TIMEBASE_SEL) {
4145 data[1] = NI_FREQ_OUT_TIMEBASE_2_CLOCK_SRC;
4146 data[2] = TIMEBASE_2_NS;
4148 data[1] = NI_FREQ_OUT_TIMEBASE_1_DIV_2_CLOCK_SRC;
4149 data[2] = TIMEBASE_1_NS * 2;
4158 static int ni_8255_callback(struct comedi_device *dev,
4159 int dir, int port, int data, unsigned long iobase)
4162 ni_writeb(dev, data, iobase + 2 * port);
4166 return ni_readb(dev, iobase + 2 * port);
4169 static int ni_get_pwm_config(struct comedi_device *dev, unsigned int *data)
4171 struct ni_private *devpriv = dev->private;
4173 data[1] = devpriv->pwm_up_count * devpriv->clock_ns;
4174 data[2] = devpriv->pwm_down_count * devpriv->clock_ns;
4178 static int ni_m_series_pwm_config(struct comedi_device *dev,
4179 struct comedi_subdevice *s,
4180 struct comedi_insn *insn,
4183 struct ni_private *devpriv = dev->private;
4184 unsigned int up_count, down_count;
4187 case INSN_CONFIG_PWM_OUTPUT:
4189 case CMDF_ROUND_NEAREST:
4190 up_count = DIV_ROUND_CLOSEST(data[2],
4193 case CMDF_ROUND_DOWN:
4194 up_count = data[2] / devpriv->clock_ns;
4198 DIV_ROUND_UP(data[2], devpriv->clock_ns);
4204 case CMDF_ROUND_NEAREST:
4205 down_count = DIV_ROUND_CLOSEST(data[4],
4208 case CMDF_ROUND_DOWN:
4209 down_count = data[4] / devpriv->clock_ns;
4213 DIV_ROUND_UP(data[4], devpriv->clock_ns);
4218 if (up_count * devpriv->clock_ns != data[2] ||
4219 down_count * devpriv->clock_ns != data[4]) {
4220 data[2] = up_count * devpriv->clock_ns;
4221 data[4] = down_count * devpriv->clock_ns;
4224 ni_writel(dev, NI_M_CAL_PWM_HIGH_TIME(up_count) |
4225 NI_M_CAL_PWM_LOW_TIME(down_count),
4227 devpriv->pwm_up_count = up_count;
4228 devpriv->pwm_down_count = down_count;
4230 case INSN_CONFIG_GET_PWM_OUTPUT:
4231 return ni_get_pwm_config(dev, data);
4238 static int ni_6143_pwm_config(struct comedi_device *dev,
4239 struct comedi_subdevice *s,
4240 struct comedi_insn *insn,
4243 struct ni_private *devpriv = dev->private;
4244 unsigned int up_count, down_count;
4247 case INSN_CONFIG_PWM_OUTPUT:
4249 case CMDF_ROUND_NEAREST:
4250 up_count = DIV_ROUND_CLOSEST(data[2],
4253 case CMDF_ROUND_DOWN:
4254 up_count = data[2] / devpriv->clock_ns;
4258 DIV_ROUND_UP(data[2], devpriv->clock_ns);
4264 case CMDF_ROUND_NEAREST:
4265 down_count = DIV_ROUND_CLOSEST(data[4],
4268 case CMDF_ROUND_DOWN:
4269 down_count = data[4] / devpriv->clock_ns;
4273 DIV_ROUND_UP(data[4], devpriv->clock_ns);
4278 if (up_count * devpriv->clock_ns != data[2] ||
4279 down_count * devpriv->clock_ns != data[4]) {
4280 data[2] = up_count * devpriv->clock_ns;
4281 data[4] = down_count * devpriv->clock_ns;
4284 ni_writel(dev, up_count, NI6143_CALIB_HI_TIME_REG);
4285 devpriv->pwm_up_count = up_count;
4286 ni_writel(dev, down_count, NI6143_CALIB_LO_TIME_REG);
4287 devpriv->pwm_down_count = down_count;
4289 case INSN_CONFIG_GET_PWM_OUTPUT:
4290 return ni_get_pwm_config(dev, data);
4297 static int pack_mb88341(int addr, int val, int *bitstring)
4301 * Note that address bits are reversed. Thanks to
4302 * Ingo Keen for noticing this.
4304 * Note also that the 88341 expects address values from
4305 * 1-12, whereas we use channel numbers 0-11. The NI
4306 * docs use 1-12, also, so be careful here.
4309 *bitstring = ((addr & 0x1) << 11) |
4310 ((addr & 0x2) << 9) |
4311 ((addr & 0x4) << 7) | ((addr & 0x8) << 5) | (val & 0xff);
4315 static int pack_dac8800(int addr, int val, int *bitstring)
4317 *bitstring = ((addr & 0x7) << 8) | (val & 0xff);
4321 static int pack_dac8043(int addr, int val, int *bitstring)
4323 *bitstring = val & 0xfff;
4327 static int pack_ad8522(int addr, int val, int *bitstring)
4329 *bitstring = (val & 0xfff) | (addr ? 0xc000 : 0xa000);
4333 static int pack_ad8804(int addr, int val, int *bitstring)
4335 *bitstring = ((addr & 0xf) << 8) | (val & 0xff);
4339 static int pack_ad8842(int addr, int val, int *bitstring)
4341 *bitstring = ((addr + 1) << 8) | (val & 0xff);
4345 struct caldac_struct {
4348 int (*packbits)(int address, int value, int *bitstring);
4351 static struct caldac_struct caldacs[] = {
4352 [mb88341] = {12, 8, pack_mb88341},
4353 [dac8800] = {8, 8, pack_dac8800},
4354 [dac8043] = {1, 12, pack_dac8043},
4355 [ad8522] = {2, 12, pack_ad8522},
4356 [ad8804] = {12, 8, pack_ad8804},
4357 [ad8842] = {8, 8, pack_ad8842},
4358 [ad8804_debug] = {16, 8, pack_ad8804},
4361 static void ni_write_caldac(struct comedi_device *dev, int addr, int val)
4363 const struct ni_board_struct *board = dev->board_ptr;
4364 struct ni_private *devpriv = dev->private;
4365 unsigned int loadbit = 0, bits = 0, bit, bitstring = 0;
4370 if (devpriv->caldacs[addr] == val)
4372 devpriv->caldacs[addr] = val;
4374 for (i = 0; i < 3; i++) {
4375 type = board->caldac[i];
4376 if (type == caldac_none)
4378 if (addr < caldacs[type].n_chans) {
4379 bits = caldacs[type].packbits(addr, val, &bitstring);
4380 loadbit = NI_E_SERIAL_CMD_DAC_LD(i);
4383 addr -= caldacs[type].n_chans;
4386 /* bits will be 0 if there is no caldac for the given addr */
4390 for (bit = 1 << (bits - 1); bit; bit >>= 1) {
4391 cmd = (bit & bitstring) ? NI_E_SERIAL_CMD_SDATA : 0;
4392 ni_writeb(dev, cmd, NI_E_SERIAL_CMD_REG);
4394 ni_writeb(dev, NI_E_SERIAL_CMD_SCLK | cmd, NI_E_SERIAL_CMD_REG);
4397 ni_writeb(dev, loadbit, NI_E_SERIAL_CMD_REG);
4399 ni_writeb(dev, 0, NI_E_SERIAL_CMD_REG);
4402 static int ni_calib_insn_write(struct comedi_device *dev,
4403 struct comedi_subdevice *s,
4404 struct comedi_insn *insn,
4408 /* only bother writing the last sample to the channel */
4409 ni_write_caldac(dev, CR_CHAN(insn->chanspec),
4416 static int ni_calib_insn_read(struct comedi_device *dev,
4417 struct comedi_subdevice *s,
4418 struct comedi_insn *insn,
4421 struct ni_private *devpriv = dev->private;
4424 for (i = 0; i < insn->n; i++)
4425 data[0] = devpriv->caldacs[CR_CHAN(insn->chanspec)];
4430 static void caldac_setup(struct comedi_device *dev, struct comedi_subdevice *s)
4432 const struct ni_board_struct *board = dev->board_ptr;
4433 struct ni_private *devpriv = dev->private;
4442 type = board->caldac[0];
4443 if (type == caldac_none)
4445 n_bits = caldacs[type].n_bits;
4446 for (i = 0; i < 3; i++) {
4447 type = board->caldac[i];
4448 if (type == caldac_none)
4450 if (caldacs[type].n_bits != n_bits)
4452 n_chans += caldacs[type].n_chans;
4455 s->n_chan = n_chans;
4458 unsigned int *maxdata_list = devpriv->caldac_maxdata_list;
4460 if (n_chans > MAX_N_CALDACS)
4461 dev_err(dev->class_dev,
4462 "BUG! MAX_N_CALDACS too small\n");
4463 s->maxdata_list = maxdata_list;
4465 for (i = 0; i < n_dacs; i++) {
4466 type = board->caldac[i];
4467 for (j = 0; j < caldacs[type].n_chans; j++) {
4468 maxdata_list[chan] =
4469 (1 << caldacs[type].n_bits) - 1;
4474 for (chan = 0; chan < s->n_chan; chan++)
4475 ni_write_caldac(dev, i, s->maxdata_list[i] / 2);
4477 type = board->caldac[0];
4478 s->maxdata = (1 << caldacs[type].n_bits) - 1;
4480 for (chan = 0; chan < s->n_chan; chan++)
4481 ni_write_caldac(dev, i, s->maxdata / 2);
4485 static int ni_read_eeprom(struct comedi_device *dev, int addr)
4487 unsigned int cmd = NI_E_SERIAL_CMD_EEPROM_CS;
4491 bitstring = 0x0300 | ((addr & 0x100) << 3) | (addr & 0xff);
4492 ni_writeb(dev, cmd, NI_E_SERIAL_CMD_REG);
4493 for (bit = 0x8000; bit; bit >>= 1) {
4494 if (bit & bitstring)
4495 cmd |= NI_E_SERIAL_CMD_SDATA;
4497 cmd &= ~NI_E_SERIAL_CMD_SDATA;
4499 ni_writeb(dev, cmd, NI_E_SERIAL_CMD_REG);
4500 ni_writeb(dev, NI_E_SERIAL_CMD_SCLK | cmd, NI_E_SERIAL_CMD_REG);
4502 cmd = NI_E_SERIAL_CMD_EEPROM_CS;
4504 for (bit = 0x80; bit; bit >>= 1) {
4505 ni_writeb(dev, cmd, NI_E_SERIAL_CMD_REG);
4506 ni_writeb(dev, NI_E_SERIAL_CMD_SCLK | cmd, NI_E_SERIAL_CMD_REG);
4507 if (ni_readb(dev, NI_E_STATUS_REG) & NI_E_STATUS_PROMOUT)
4510 ni_writeb(dev, 0, NI_E_SERIAL_CMD_REG);
4515 static int ni_eeprom_insn_read(struct comedi_device *dev,
4516 struct comedi_subdevice *s,
4517 struct comedi_insn *insn,
4524 val = ni_read_eeprom(dev, CR_CHAN(insn->chanspec));
4525 for (i = 0; i < insn->n; i++)
4531 static int ni_m_series_eeprom_insn_read(struct comedi_device *dev,
4532 struct comedi_subdevice *s,
4533 struct comedi_insn *insn,
4536 struct ni_private *devpriv = dev->private;
4539 for (i = 0; i < insn->n; i++)
4540 data[i] = devpriv->eeprom_buffer[CR_CHAN(insn->chanspec)];
4545 static unsigned int ni_old_get_pfi_routing(struct comedi_device *dev,
4548 /* pre-m-series boards have fixed signals on pfi pins */
4551 return NI_PFI_OUTPUT_AI_START1;
4553 return NI_PFI_OUTPUT_AI_START2;
4555 return NI_PFI_OUTPUT_AI_CONVERT;
4557 return NI_PFI_OUTPUT_G_SRC1;
4559 return NI_PFI_OUTPUT_G_GATE1;
4561 return NI_PFI_OUTPUT_AO_UPDATE_N;
4563 return NI_PFI_OUTPUT_AO_START1;
4565 return NI_PFI_OUTPUT_AI_START_PULSE;
4567 return NI_PFI_OUTPUT_G_SRC0;
4569 return NI_PFI_OUTPUT_G_GATE0;
4571 dev_err(dev->class_dev, "bug, unhandled case in switch.\n");
4577 static int ni_old_set_pfi_routing(struct comedi_device *dev,
4578 unsigned int chan, unsigned int source)
4580 /* pre-m-series boards have fixed signals on pfi pins */
4581 if (source != ni_old_get_pfi_routing(dev, chan))
4586 static unsigned int ni_m_series_get_pfi_routing(struct comedi_device *dev,
4589 struct ni_private *devpriv = dev->private;
4590 const unsigned int array_offset = chan / 3;
4592 return NI_M_PFI_OUT_SEL_TO_SRC(chan,
4593 devpriv->pfi_output_select_reg[array_offset]);
4596 static int ni_m_series_set_pfi_routing(struct comedi_device *dev,
4597 unsigned int chan, unsigned int source)
4599 struct ni_private *devpriv = dev->private;
4600 unsigned int index = chan / 3;
4601 unsigned short val = devpriv->pfi_output_select_reg[index];
4603 if ((source & 0x1f) != source)
4606 val &= ~NI_M_PFI_OUT_SEL_MASK(chan);
4607 val |= NI_M_PFI_OUT_SEL(chan, source);
4608 ni_writew(dev, val, NI_M_PFI_OUT_SEL_REG(index));
4609 devpriv->pfi_output_select_reg[index] = val;
4614 static unsigned int ni_get_pfi_routing(struct comedi_device *dev,
4617 struct ni_private *devpriv = dev->private;
4619 if (chan >= NI_PFI(0)) {
4620 /* allow new and old names of pfi channels to work. */
4623 return (devpriv->is_m_series)
4624 ? ni_m_series_get_pfi_routing(dev, chan)
4625 : ni_old_get_pfi_routing(dev, chan);
4628 /* Sets the output mux for the specified PFI channel. */
4629 static int ni_set_pfi_routing(struct comedi_device *dev,
4630 unsigned int chan, unsigned int source)
4632 struct ni_private *devpriv = dev->private;
4634 if (chan >= NI_PFI(0)) {
4635 /* allow new and old names of pfi channels to work. */
4638 return (devpriv->is_m_series)
4639 ? ni_m_series_set_pfi_routing(dev, chan, source)
4640 : ni_old_set_pfi_routing(dev, chan, source);
4643 static int ni_config_pfi_filter(struct comedi_device *dev,
4645 enum ni_pfi_filter_select filter)
4647 struct ni_private *devpriv = dev->private;
4650 if (!devpriv->is_m_series)
4653 if (chan >= NI_PFI(0)) {
4654 /* allow new and old names of pfi channels to work. */
4658 bits = ni_readl(dev, NI_M_PFI_FILTER_REG);
4659 bits &= ~NI_M_PFI_FILTER_SEL_MASK(chan);
4660 bits |= NI_M_PFI_FILTER_SEL(chan, filter);
4661 ni_writel(dev, bits, NI_M_PFI_FILTER_REG);
4665 static void ni_set_pfi_direction(struct comedi_device *dev, int chan,
4666 unsigned int direction)
4668 if (chan >= NI_PFI(0)) {
4669 /* allow new and old names of pfi channels to work. */
4672 direction = (direction == COMEDI_OUTPUT) ? 1u : 0u;
4673 ni_set_bits(dev, NISTC_IO_BIDIR_PIN_REG, 1 << chan, direction);
4676 static int ni_get_pfi_direction(struct comedi_device *dev, int chan)
4678 struct ni_private *devpriv = dev->private;
4680 if (chan >= NI_PFI(0)) {
4681 /* allow new and old names of pfi channels to work. */
4684 return devpriv->io_bidirection_pin_reg & (1 << chan) ?
4685 COMEDI_OUTPUT : COMEDI_INPUT;
4688 static int ni_pfi_insn_config(struct comedi_device *dev,
4689 struct comedi_subdevice *s,
4690 struct comedi_insn *insn,
4698 chan = CR_CHAN(insn->chanspec);
4703 ni_set_pfi_direction(dev, chan, data[0]);
4705 case INSN_CONFIG_DIO_QUERY:
4706 data[1] = ni_get_pfi_direction(dev, chan);
4708 case INSN_CONFIG_SET_ROUTING:
4709 return ni_set_pfi_routing(dev, chan, data[1]);
4710 case INSN_CONFIG_GET_ROUTING:
4711 data[1] = ni_get_pfi_routing(dev, chan);
4713 case INSN_CONFIG_FILTER:
4714 return ni_config_pfi_filter(dev, chan, data[1]);
4721 static int ni_pfi_insn_bits(struct comedi_device *dev,
4722 struct comedi_subdevice *s,
4723 struct comedi_insn *insn,
4726 struct ni_private *devpriv = dev->private;
4728 if (!devpriv->is_m_series)
4731 if (comedi_dio_update_state(s, data))
4732 ni_writew(dev, s->state, NI_M_PFI_DO_REG);
4734 data[1] = ni_readw(dev, NI_M_PFI_DI_REG);
4739 static int cs5529_wait_for_idle(struct comedi_device *dev)
4741 unsigned short status;
4742 const int timeout = HZ;
4745 for (i = 0; i < timeout; i++) {
4746 status = ni_ao_win_inw(dev, NI67XX_CAL_STATUS_REG);
4747 if ((status & NI67XX_CAL_STATUS_BUSY) == 0)
4749 set_current_state(TASK_INTERRUPTIBLE);
4750 if (schedule_timeout(1))
4754 dev_err(dev->class_dev, "timeout\n");
4760 static void cs5529_command(struct comedi_device *dev, unsigned short value)
4762 static const int timeout = 100;
4765 ni_ao_win_outw(dev, value, NI67XX_CAL_CMD_REG);
4766 /* give time for command to start being serially clocked into cs5529.
4767 * this insures that the NI67XX_CAL_STATUS_BUSY bit will get properly
4768 * set before we exit this function.
4770 for (i = 0; i < timeout; i++) {
4771 if (ni_ao_win_inw(dev, NI67XX_CAL_STATUS_REG) &
4772 NI67XX_CAL_STATUS_BUSY)
4777 dev_err(dev->class_dev,
4778 "possible problem - never saw adc go busy?\n");
4781 static int cs5529_do_conversion(struct comedi_device *dev,
4782 unsigned short *data)
4785 unsigned short status;
4787 cs5529_command(dev, CS5529_CMD_CB | CS5529_CMD_SINGLE_CONV);
4788 retval = cs5529_wait_for_idle(dev);
4790 dev_err(dev->class_dev,
4791 "timeout or signal in %s()\n", __func__);
4794 status = ni_ao_win_inw(dev, NI67XX_CAL_STATUS_REG);
4795 if (status & NI67XX_CAL_STATUS_OSC_DETECT) {
4796 dev_err(dev->class_dev,
4797 "cs5529 conversion error, status CSS_OSC_DETECT\n");
4800 if (status & NI67XX_CAL_STATUS_OVERRANGE) {
4801 dev_err(dev->class_dev,
4802 "cs5529 conversion error, overrange (ignoring)\n");
4805 *data = ni_ao_win_inw(dev, NI67XX_CAL_DATA_REG);
4806 /* cs5529 returns 16 bit signed data in bipolar mode */
4812 static int cs5529_ai_insn_read(struct comedi_device *dev,
4813 struct comedi_subdevice *s,
4814 struct comedi_insn *insn,
4818 unsigned short sample;
4819 unsigned int channel_select;
4820 const unsigned int INTERNAL_REF = 0x1000;
4823 * Set calibration adc source. Docs lie, reference select bits 8 to 11
4824 * do nothing. bit 12 seems to chooses internal reference voltage, bit
4825 * 13 causes the adc input to go overrange (maybe reads external
4828 if (insn->chanspec & CR_ALT_SOURCE)
4829 channel_select = INTERNAL_REF;
4831 channel_select = CR_CHAN(insn->chanspec);
4832 ni_ao_win_outw(dev, channel_select, NI67XX_AO_CAL_CHAN_SEL_REG);
4834 for (n = 0; n < insn->n; n++) {
4835 retval = cs5529_do_conversion(dev, &sample);
4843 static void cs5529_config_write(struct comedi_device *dev, unsigned int value,
4844 unsigned int reg_select_bits)
4846 ni_ao_win_outw(dev, (value >> 16) & 0xff, NI67XX_CAL_CFG_HI_REG);
4847 ni_ao_win_outw(dev, value & 0xffff, NI67XX_CAL_CFG_LO_REG);
4848 reg_select_bits &= CS5529_CMD_REG_MASK;
4849 cs5529_command(dev, CS5529_CMD_CB | reg_select_bits);
4850 if (cs5529_wait_for_idle(dev))
4851 dev_err(dev->class_dev,
4852 "timeout or signal in %s\n", __func__);
4855 static int init_cs5529(struct comedi_device *dev)
4857 unsigned int config_bits = CS5529_CFG_PORT_FLAG |
4858 CS5529_CFG_WORD_RATE_2180;
4861 /* do self-calibration */
4862 cs5529_config_write(dev, config_bits | CS5529_CFG_CALIB_BOTH_SELF,
4864 /* need to force a conversion for calibration to run */
4865 cs5529_do_conversion(dev, NULL);
4867 /* force gain calibration to 1 */
4868 cs5529_config_write(dev, 0x400000, CS5529_GAIN_REG);
4869 cs5529_config_write(dev, config_bits | CS5529_CFG_CALIB_OFFSET_SELF,
4871 if (cs5529_wait_for_idle(dev))
4872 dev_err(dev->class_dev,
4873 "timeout or signal in %s\n", __func__);
4879 * Find best multiplier/divider to try and get the PLL running at 80 MHz
4880 * given an arbitrary frequency input clock.
4882 static int ni_mseries_get_pll_parameters(unsigned int reference_period_ns,
4883 unsigned int *freq_divider,
4884 unsigned int *freq_multiplier,
4885 unsigned int *actual_period_ns)
4888 unsigned int best_div = 1;
4890 unsigned int best_mult = 1;
4891 static const unsigned int pico_per_nano = 1000;
4892 const unsigned int reference_picosec = reference_period_ns *
4895 * m-series wants the phased-locked loop to output 80MHz, which is
4896 * divided by 4 to 20 MHz for most timing clocks
4898 static const unsigned int target_picosec = 12500;
4899 int best_period_picosec = 0;
4901 for (div = 1; div <= NI_M_PLL_MAX_DIVISOR; ++div) {
4902 for (mult = 1; mult <= NI_M_PLL_MAX_MULTIPLIER; ++mult) {
4903 unsigned int new_period_ps =
4904 (reference_picosec * div) / mult;
4905 if (abs(new_period_ps - target_picosec) <
4906 abs(best_period_picosec - target_picosec)) {
4907 best_period_picosec = new_period_ps;
4913 if (best_period_picosec == 0)
4916 *freq_divider = best_div;
4917 *freq_multiplier = best_mult;
4918 /* return the actual period (* fudge factor for 80 to 20 MHz) */
4919 *actual_period_ns = DIV_ROUND_CLOSEST(best_period_picosec * 4,
4924 static int ni_mseries_set_pll_master_clock(struct comedi_device *dev,
4925 unsigned int source,
4926 unsigned int period_ns)
4928 struct ni_private *devpriv = dev->private;
4929 static const unsigned int min_period_ns = 50;
4930 static const unsigned int max_period_ns = 1000;
4931 static const unsigned int timeout = 1000;
4932 unsigned int pll_control_bits;
4933 unsigned int freq_divider;
4934 unsigned int freq_multiplier;
4939 if (source == NI_MIO_PLL_PXI10_CLOCK)
4942 * These limits are somewhat arbitrary, but NI advertises 1 to 20MHz
4943 * range so we'll use that.
4945 if (period_ns < min_period_ns || period_ns > max_period_ns) {
4946 dev_err(dev->class_dev,
4947 "%s: you must specify an input clock frequency between %i and %i nanosec for the phased-lock loop\n",
4948 __func__, min_period_ns, max_period_ns);
4951 devpriv->rtsi_trig_direction_reg &= ~NISTC_RTSI_TRIG_USE_CLK;
4952 ni_stc_writew(dev, devpriv->rtsi_trig_direction_reg,
4953 NISTC_RTSI_TRIG_DIR_REG);
4954 pll_control_bits = NI_M_PLL_CTRL_ENA | NI_M_PLL_CTRL_VCO_MODE_75_150MHZ;
4955 devpriv->clock_and_fout2 |= NI_M_CLK_FOUT2_TIMEBASE1_PLL |
4956 NI_M_CLK_FOUT2_TIMEBASE3_PLL;
4957 devpriv->clock_and_fout2 &= ~NI_M_CLK_FOUT2_PLL_SRC_MASK;
4959 case NI_MIO_PLL_PXI_STAR_TRIGGER_CLOCK:
4960 devpriv->clock_and_fout2 |= NI_M_CLK_FOUT2_PLL_SRC_STAR;
4962 case NI_MIO_PLL_PXI10_CLOCK:
4963 /* pxi clock is 10MHz */
4964 devpriv->clock_and_fout2 |= NI_M_CLK_FOUT2_PLL_SRC_PXI10;
4967 for (rtsi = 0; rtsi <= NI_M_MAX_RTSI_CHAN; ++rtsi) {
4968 if (source == NI_MIO_PLL_RTSI_CLOCK(rtsi)) {
4969 devpriv->clock_and_fout2 |=
4970 NI_M_CLK_FOUT2_PLL_SRC_RTSI(rtsi);
4974 if (rtsi > NI_M_MAX_RTSI_CHAN)
4978 retval = ni_mseries_get_pll_parameters(period_ns,
4981 &devpriv->clock_ns);
4983 dev_err(dev->class_dev,
4984 "bug, failed to find pll parameters\n");
4988 ni_writew(dev, devpriv->clock_and_fout2, NI_M_CLK_FOUT2_REG);
4989 pll_control_bits |= NI_M_PLL_CTRL_DIVISOR(freq_divider) |
4990 NI_M_PLL_CTRL_MULTIPLIER(freq_multiplier);
4992 ni_writew(dev, pll_control_bits, NI_M_PLL_CTRL_REG);
4993 devpriv->clock_source = source;
4994 /* it takes a few hundred microseconds for PLL to lock */
4995 for (i = 0; i < timeout; ++i) {
4996 if (ni_readw(dev, NI_M_PLL_STATUS_REG) & NI_M_PLL_STATUS_LOCKED)
5001 dev_err(dev->class_dev,
5002 "%s: timed out waiting for PLL to lock to reference clock source %i with period %i ns\n",
5003 __func__, source, period_ns);
5009 static int ni_set_master_clock(struct comedi_device *dev,
5010 unsigned int source, unsigned int period_ns)
5012 struct ni_private *devpriv = dev->private;
5014 if (source == NI_MIO_INTERNAL_CLOCK) {
5015 devpriv->rtsi_trig_direction_reg &= ~NISTC_RTSI_TRIG_USE_CLK;
5016 ni_stc_writew(dev, devpriv->rtsi_trig_direction_reg,
5017 NISTC_RTSI_TRIG_DIR_REG);
5018 devpriv->clock_ns = TIMEBASE_1_NS;
5019 if (devpriv->is_m_series) {
5020 devpriv->clock_and_fout2 &=
5021 ~(NI_M_CLK_FOUT2_TIMEBASE1_PLL |
5022 NI_M_CLK_FOUT2_TIMEBASE3_PLL);
5023 ni_writew(dev, devpriv->clock_and_fout2,
5024 NI_M_CLK_FOUT2_REG);
5025 ni_writew(dev, 0, NI_M_PLL_CTRL_REG);
5027 devpriv->clock_source = source;
5029 if (devpriv->is_m_series) {
5030 return ni_mseries_set_pll_master_clock(dev, source,
5033 if (source == NI_MIO_RTSI_CLOCK) {
5034 devpriv->rtsi_trig_direction_reg |=
5035 NISTC_RTSI_TRIG_USE_CLK;
5037 devpriv->rtsi_trig_direction_reg,
5038 NISTC_RTSI_TRIG_DIR_REG);
5039 if (period_ns == 0) {
5040 dev_err(dev->class_dev,
5041 "we don't handle an unspecified clock period correctly yet, returning error\n");
5044 devpriv->clock_ns = period_ns;
5045 devpriv->clock_source = source;
5054 static int ni_valid_rtsi_output_source(struct comedi_device *dev,
5055 unsigned int chan, unsigned int source)
5057 struct ni_private *devpriv = dev->private;
5059 if (chan >= NISTC_RTSI_TRIG_NUM_CHAN(devpriv->is_m_series)) {
5060 if (chan == NISTC_RTSI_TRIG_OLD_CLK_CHAN) {
5061 if (source == NI_RTSI_OUTPUT_RTSI_OSC)
5064 dev_err(dev->class_dev,
5065 "%s: invalid source for channel=%i, channel %i is always the RTSI clock for pre-m-series boards\n",
5066 __func__, chan, NISTC_RTSI_TRIG_OLD_CLK_CHAN);
5072 case NI_RTSI_OUTPUT_ADR_START1:
5073 case NI_RTSI_OUTPUT_ADR_START2:
5074 case NI_RTSI_OUTPUT_SCLKG:
5075 case NI_RTSI_OUTPUT_DACUPDN:
5076 case NI_RTSI_OUTPUT_DA_START1:
5077 case NI_RTSI_OUTPUT_G_SRC0:
5078 case NI_RTSI_OUTPUT_G_GATE0:
5079 case NI_RTSI_OUTPUT_RGOUT0:
5080 case NI_RTSI_OUTPUT_RTSI_BRD(0):
5081 case NI_RTSI_OUTPUT_RTSI_BRD(1):
5082 case NI_RTSI_OUTPUT_RTSI_BRD(2):
5083 case NI_RTSI_OUTPUT_RTSI_BRD(3):
5085 case NI_RTSI_OUTPUT_RTSI_OSC:
5086 return (devpriv->is_m_series) ? 1 : 0;
5092 static int ni_set_rtsi_routing(struct comedi_device *dev,
5093 unsigned int chan, unsigned int src)
5095 struct ni_private *devpriv = dev->private;
5097 if (chan >= TRIGGER_LINE(0))
5098 /* allow new and old names of rtsi channels to work. */
5099 chan -= TRIGGER_LINE(0);
5101 if (ni_valid_rtsi_output_source(dev, chan, src) == 0)
5104 devpriv->rtsi_trig_a_output_reg &= ~NISTC_RTSI_TRIG_MASK(chan);
5105 devpriv->rtsi_trig_a_output_reg |= NISTC_RTSI_TRIG(chan, src);
5106 ni_stc_writew(dev, devpriv->rtsi_trig_a_output_reg,
5107 NISTC_RTSI_TRIGA_OUT_REG);
5108 } else if (chan < NISTC_RTSI_TRIG_NUM_CHAN(devpriv->is_m_series)) {
5109 devpriv->rtsi_trig_b_output_reg &= ~NISTC_RTSI_TRIG_MASK(chan);
5110 devpriv->rtsi_trig_b_output_reg |= NISTC_RTSI_TRIG(chan, src);
5111 ni_stc_writew(dev, devpriv->rtsi_trig_b_output_reg,
5112 NISTC_RTSI_TRIGB_OUT_REG);
5113 } else if (chan != NISTC_RTSI_TRIG_OLD_CLK_CHAN) {
5114 /* probably should never reach this, since the
5115 * ni_valid_rtsi_output_source above errors out if chan is too
5118 dev_err(dev->class_dev, "%s: unknown rtsi channel\n", __func__);
5124 static unsigned int ni_get_rtsi_routing(struct comedi_device *dev,
5127 struct ni_private *devpriv = dev->private;
5129 if (chan >= TRIGGER_LINE(0))
5130 /* allow new and old names of rtsi channels to work. */
5131 chan -= TRIGGER_LINE(0);
5134 return NISTC_RTSI_TRIG_TO_SRC(chan,
5135 devpriv->rtsi_trig_a_output_reg);
5136 } else if (chan < NISTC_RTSI_TRIG_NUM_CHAN(devpriv->is_m_series)) {
5137 return NISTC_RTSI_TRIG_TO_SRC(chan,
5138 devpriv->rtsi_trig_b_output_reg);
5139 } else if (chan == NISTC_RTSI_TRIG_OLD_CLK_CHAN) {
5140 return NI_RTSI_OUTPUT_RTSI_OSC;
5143 dev_err(dev->class_dev, "%s: unknown rtsi channel\n", __func__);
5147 static void ni_set_rtsi_direction(struct comedi_device *dev, int chan,
5148 unsigned int direction)
5150 struct ni_private *devpriv = dev->private;
5151 unsigned int max_chan = NISTC_RTSI_TRIG_NUM_CHAN(devpriv->is_m_series);
5153 if (chan >= TRIGGER_LINE(0))
5154 /* allow new and old names of rtsi channels to work. */
5155 chan -= TRIGGER_LINE(0);
5157 if (direction == COMEDI_OUTPUT) {
5158 if (chan < max_chan) {
5159 devpriv->rtsi_trig_direction_reg |=
5160 NISTC_RTSI_TRIG_DIR(chan, devpriv->is_m_series);
5161 } else if (chan == NISTC_RTSI_TRIG_OLD_CLK_CHAN) {
5162 devpriv->rtsi_trig_direction_reg |=
5163 NISTC_RTSI_TRIG_DRV_CLK;
5166 if (chan < max_chan) {
5167 devpriv->rtsi_trig_direction_reg &=
5168 ~NISTC_RTSI_TRIG_DIR(chan, devpriv->is_m_series);
5169 } else if (chan == NISTC_RTSI_TRIG_OLD_CLK_CHAN) {
5170 devpriv->rtsi_trig_direction_reg &=
5171 ~NISTC_RTSI_TRIG_DRV_CLK;
5174 ni_stc_writew(dev, devpriv->rtsi_trig_direction_reg,
5175 NISTC_RTSI_TRIG_DIR_REG);
5178 static int ni_get_rtsi_direction(struct comedi_device *dev, int chan)
5180 struct ni_private *devpriv = dev->private;
5181 unsigned int max_chan = NISTC_RTSI_TRIG_NUM_CHAN(devpriv->is_m_series);
5183 if (chan >= TRIGGER_LINE(0))
5184 /* allow new and old names of rtsi channels to work. */
5185 chan -= TRIGGER_LINE(0);
5187 if (chan < max_chan) {
5188 return (devpriv->rtsi_trig_direction_reg &
5189 NISTC_RTSI_TRIG_DIR(chan, devpriv->is_m_series))
5190 ? COMEDI_OUTPUT : COMEDI_INPUT;
5191 } else if (chan == NISTC_RTSI_TRIG_OLD_CLK_CHAN) {
5192 return (devpriv->rtsi_trig_direction_reg &
5193 NISTC_RTSI_TRIG_DRV_CLK)
5194 ? COMEDI_OUTPUT : COMEDI_INPUT;
5199 static int ni_rtsi_insn_config(struct comedi_device *dev,
5200 struct comedi_subdevice *s,
5201 struct comedi_insn *insn,
5204 struct ni_private *devpriv = dev->private;
5205 unsigned int chan = CR_CHAN(insn->chanspec);
5210 ni_set_rtsi_direction(dev, chan, data[0]);
5212 case INSN_CONFIG_DIO_QUERY: {
5213 int ret = ni_get_rtsi_direction(dev, chan);
5220 case INSN_CONFIG_SET_CLOCK_SRC:
5221 return ni_set_master_clock(dev, data[1], data[2]);
5222 case INSN_CONFIG_GET_CLOCK_SRC:
5223 data[1] = devpriv->clock_source;
5224 data[2] = devpriv->clock_ns;
5226 case INSN_CONFIG_SET_ROUTING:
5227 return ni_set_rtsi_routing(dev, chan, data[1]);
5228 case INSN_CONFIG_GET_ROUTING: {
5229 int ret = ni_get_rtsi_routing(dev, chan);
5242 static int ni_rtsi_insn_bits(struct comedi_device *dev,
5243 struct comedi_subdevice *s,
5244 struct comedi_insn *insn,
5253 * Default routing for RTSI trigger lines.
5255 * These values are used here in the init function, as well as in the
5256 * disconnect_route function, after a RTSI route has been disconnected.
5258 static const int default_rtsi_routing[] = {
5259 [0] = NI_RTSI_OUTPUT_ADR_START1,
5260 [1] = NI_RTSI_OUTPUT_ADR_START2,
5261 [2] = NI_RTSI_OUTPUT_SCLKG,
5262 [3] = NI_RTSI_OUTPUT_DACUPDN,
5263 [4] = NI_RTSI_OUTPUT_DA_START1,
5264 [5] = NI_RTSI_OUTPUT_G_SRC0,
5265 [6] = NI_RTSI_OUTPUT_G_GATE0,
5266 [7] = NI_RTSI_OUTPUT_RTSI_OSC,
5270 * Route signals through RGOUT0 terminal.
5271 * @reg: raw register value of RGOUT0 bits (only bit0 is important).
5272 * @dev: comedi device handle.
5274 static void set_rgout0_reg(int reg, struct comedi_device *dev)
5276 struct ni_private *devpriv = dev->private;
5278 if (devpriv->is_m_series) {
5279 devpriv->rtsi_trig_direction_reg &=
5280 ~NISTC_RTSI_TRIG_DIR_SUB_SEL1;
5281 devpriv->rtsi_trig_direction_reg |=
5282 (reg << NISTC_RTSI_TRIG_DIR_SUB_SEL1_SHIFT) &
5283 NISTC_RTSI_TRIG_DIR_SUB_SEL1;
5284 ni_stc_writew(dev, devpriv->rtsi_trig_direction_reg,
5285 NISTC_RTSI_TRIG_DIR_REG);
5287 devpriv->rtsi_trig_b_output_reg &= ~NISTC_RTSI_TRIGB_SUB_SEL1;
5288 devpriv->rtsi_trig_b_output_reg |=
5289 (reg << NISTC_RTSI_TRIGB_SUB_SEL1_SHIFT) &
5290 NISTC_RTSI_TRIGB_SUB_SEL1;
5291 ni_stc_writew(dev, devpriv->rtsi_trig_b_output_reg,
5292 NISTC_RTSI_TRIGB_OUT_REG);
5296 static int get_rgout0_reg(struct comedi_device *dev)
5298 struct ni_private *devpriv = dev->private;
5301 if (devpriv->is_m_series)
5302 reg = (devpriv->rtsi_trig_direction_reg &
5303 NISTC_RTSI_TRIG_DIR_SUB_SEL1)
5304 >> NISTC_RTSI_TRIG_DIR_SUB_SEL1_SHIFT;
5306 reg = (devpriv->rtsi_trig_b_output_reg &
5307 NISTC_RTSI_TRIGB_SUB_SEL1)
5308 >> NISTC_RTSI_TRIGB_SUB_SEL1_SHIFT;
5312 static inline int get_rgout0_src(struct comedi_device *dev)
5314 struct ni_private *devpriv = dev->private;
5315 int reg = get_rgout0_reg(dev);
5317 return ni_find_route_source(reg, NI_RGOUT0, &devpriv->routing_tables);
5321 * Route signals through RGOUT0 terminal and increment the RGOUT0 use for this
5323 * @src: device-global signal name
5324 * @dev: comedi device handle
5326 * Return: -EINVAL if the source is not valid to route to RGOUT0;
5327 * -EBUSY if the RGOUT0 is already used;
5330 static int incr_rgout0_src_use(int src, struct comedi_device *dev)
5332 struct ni_private *devpriv = dev->private;
5333 s8 reg = ni_lookup_route_register(CR_CHAN(src), NI_RGOUT0,
5334 &devpriv->routing_tables);
5339 if (devpriv->rgout0_usage > 0 && get_rgout0_reg(dev) != reg)
5342 ++devpriv->rgout0_usage;
5343 set_rgout0_reg(reg, dev);
5348 * Unroute signals through RGOUT0 terminal and deccrement the RGOUT0 use for
5349 * this particular source. This function does not actually unroute anything
5350 * with respect to RGOUT0. It does, on the other hand, decrement the usage
5351 * counter for the current src->RGOUT0 mapping.
5353 * Return: -EINVAL if the source is not already routed to RGOUT0 (or usage is
5354 * already at zero); 0 if successful.
5356 static int decr_rgout0_src_use(int src, struct comedi_device *dev)
5358 struct ni_private *devpriv = dev->private;
5359 s8 reg = ni_lookup_route_register(CR_CHAN(src), NI_RGOUT0,
5360 &devpriv->routing_tables);
5362 if (devpriv->rgout0_usage > 0 && get_rgout0_reg(dev) == reg) {
5363 --devpriv->rgout0_usage;
5364 if (!devpriv->rgout0_usage)
5365 set_rgout0_reg(0, dev); /* ok default? */
5372 * Route signals through given NI_RTSI_BRD mux.
5373 * @i: index of mux to route
5374 * @reg: raw register value of RTSI_BRD bits
5375 * @dev: comedi device handle
5377 static void set_ith_rtsi_brd_reg(int i, int reg, struct comedi_device *dev)
5379 struct ni_private *devpriv = dev->private;
5380 int reg_i_sz = 3; /* value for e-series */
5384 if (devpriv->is_m_series)
5386 reg_i_mask = ~((~0) << reg_i_sz);
5387 reg_i_shift = i * reg_i_sz;
5389 /* clear out the current reg_i for ith brd */
5390 devpriv->rtsi_shared_mux_reg &= ~(reg_i_mask << reg_i_shift);
5391 /* (softcopy) write the new reg_i for ith brd */
5392 devpriv->rtsi_shared_mux_reg |= (reg & reg_i_mask) << reg_i_shift;
5393 /* (hardcopy) write the new reg_i for ith brd */
5394 ni_stc_writew(dev, devpriv->rtsi_shared_mux_reg, NISTC_RTSI_BOARD_REG);
5397 static int get_ith_rtsi_brd_reg(int i, struct comedi_device *dev)
5399 struct ni_private *devpriv = dev->private;
5400 int reg_i_sz = 3; /* value for e-series */
5404 if (devpriv->is_m_series)
5406 reg_i_mask = ~((~0) << reg_i_sz);
5407 reg_i_shift = i * reg_i_sz;
5409 return (devpriv->rtsi_shared_mux_reg >> reg_i_shift) & reg_i_mask;
5412 static inline int get_rtsi_brd_src(int brd, struct comedi_device *dev)
5414 struct ni_private *devpriv = dev->private;
5415 int brd_index = brd;
5418 if (brd >= NI_RTSI_BRD(0))
5419 brd_index = brd - NI_RTSI_BRD(0);
5421 brd = NI_RTSI_BRD(brd);
5424 * brd : device-global name
5425 * brd_index : index number of RTSI_BRD mux
5428 reg = get_ith_rtsi_brd_reg(brd_index, dev);
5430 return ni_find_route_source(reg, brd, &devpriv->routing_tables);
5434 * Route signals through NI_RTSI_BRD mux and increment the use counter for this
5437 * Return: -EINVAL if the source is not valid to route to NI_RTSI_BRD(i);
5438 * -EBUSY if all NI_RTSI_BRD muxes are already used;
5439 * NI_RTSI_BRD(i) of allocated ith mux if successful.
5441 static int incr_rtsi_brd_src_use(int src, struct comedi_device *dev)
5443 struct ni_private *devpriv = dev->private;
5444 int first_available = -1;
5449 /* first look for a mux that is already configured to provide src */
5450 for (i = 0; i < NUM_RTSI_SHARED_MUXS; ++i) {
5451 reg = ni_lookup_route_register(CR_CHAN(src), NI_RTSI_BRD(i),
5452 &devpriv->routing_tables);
5455 continue; /* invalid route */
5457 if (!devpriv->rtsi_shared_mux_usage[i]) {
5458 if (first_available < 0)
5459 /* found the first unused, but usable mux */
5460 first_available = i;
5463 * we've seen at least one possible route, so change the
5464 * final error to -EBUSY in case there are no muxes
5469 if (get_ith_rtsi_brd_reg(i, dev) == reg) {
5471 * we've found a mux that is already being used
5472 * to provide the requested signal. Reuse it.
5479 if (first_available < 0)
5482 /* we did not find a mux to reuse, but there is at least one usable */
5483 i = first_available;
5486 ++devpriv->rtsi_shared_mux_usage[i];
5487 set_ith_rtsi_brd_reg(i, reg, dev);
5488 return NI_RTSI_BRD(i);
5492 * Unroute signals through NI_RTSI_BRD mux and decrement the user counter for
5493 * this particular route.
5495 * Return: -EINVAL if the source is not already routed to rtsi_brd(i) (or usage
5496 * is already at zero); 0 if successful.
5498 static int decr_rtsi_brd_src_use(int src, int rtsi_brd,
5499 struct comedi_device *dev)
5501 struct ni_private *devpriv = dev->private;
5502 s8 reg = ni_lookup_route_register(CR_CHAN(src), rtsi_brd,
5503 &devpriv->routing_tables);
5504 const int i = rtsi_brd - NI_RTSI_BRD(0);
5506 if (devpriv->rtsi_shared_mux_usage[i] > 0 &&
5507 get_ith_rtsi_brd_reg(i, dev) == reg) {
5508 --devpriv->rtsi_shared_mux_usage[i];
5509 if (!devpriv->rtsi_shared_mux_usage[i])
5510 set_ith_rtsi_brd_reg(i, 0, dev); /* ok default? */
5517 static void ni_rtsi_init(struct comedi_device *dev)
5519 struct ni_private *devpriv = dev->private;
5522 /* Initialises the RTSI bus signal switch to a default state */
5525 * Use 10MHz instead of 20MHz for RTSI clock frequency. Appears
5526 * to have no effect, at least on pxi-6281, which always uses
5527 * 20MHz rtsi clock frequency
5529 devpriv->clock_and_fout2 = NI_M_CLK_FOUT2_RTSI_10MHZ;
5530 /* Set clock mode to internal */
5531 if (ni_set_master_clock(dev, NI_MIO_INTERNAL_CLOCK, 0) < 0)
5532 dev_err(dev->class_dev, "ni_set_master_clock failed, bug?\n");
5534 /* default internal lines routing to RTSI bus lines */
5535 for (i = 0; i < 8; ++i) {
5536 ni_set_rtsi_direction(dev, i, COMEDI_INPUT);
5537 ni_set_rtsi_routing(dev, i, default_rtsi_routing[i]);
5541 * Sets the source and direction of the 4 on board lines.
5542 * This configures all board lines to be:
5544 * 1) inputs (not sure what "output" would mean)
5545 * 2) copying TRIGGER_LINE(0) (or RTSI0) output
5547 * copying NI_PFI(0) output
5549 devpriv->rtsi_shared_mux_reg = 0;
5550 for (i = 0; i < 4; ++i)
5551 set_ith_rtsi_brd_reg(i, 0, dev);
5552 memset(devpriv->rtsi_shared_mux_usage, 0,
5553 sizeof(devpriv->rtsi_shared_mux_usage));
5555 /* initialize rgout0 pin as unused. */
5556 devpriv->rgout0_usage = 0;
5557 set_rgout0_reg(0, dev);
5560 /* Get route of GPFO_i/CtrOut pins */
5561 static inline int ni_get_gout_routing(unsigned int dest,
5562 struct comedi_device *dev)
5564 struct ni_private *devpriv = dev->private;
5565 unsigned int reg = devpriv->an_trig_etc_reg;
5569 if (reg & NISTC_ATRIG_ETC_GPFO_0_ENA)
5570 return NISTC_ATRIG_ETC_GPFO_0_SEL_TO_SRC(reg);
5573 if (reg & NISTC_ATRIG_ETC_GPFO_1_ENA)
5574 return NISTC_ATRIG_ETC_GPFO_1_SEL_TO_SRC(reg);
5581 /* Set route of GPFO_i/CtrOut pins */
5582 static inline int ni_disable_gout_routing(unsigned int dest,
5583 struct comedi_device *dev)
5585 struct ni_private *devpriv = dev->private;
5589 devpriv->an_trig_etc_reg &= ~NISTC_ATRIG_ETC_GPFO_0_ENA;
5592 devpriv->an_trig_etc_reg &= ~NISTC_ATRIG_ETC_GPFO_1_ENA;
5598 ni_stc_writew(dev, devpriv->an_trig_etc_reg, NISTC_ATRIG_ETC_REG);
5602 /* Set route of GPFO_i/CtrOut pins */
5603 static inline int ni_set_gout_routing(unsigned int src, unsigned int dest,
5604 struct comedi_device *dev)
5606 struct ni_private *devpriv = dev->private;
5611 devpriv->an_trig_etc_reg &= ~NISTC_ATRIG_ETC_GPFO_0_SEL(-1);
5613 devpriv->an_trig_etc_reg |= NISTC_ATRIG_ETC_GPFO_0_ENA
5614 | NISTC_ATRIG_ETC_GPFO_0_SEL(src);
5618 devpriv->an_trig_etc_reg &= ~NISTC_ATRIG_ETC_GPFO_1_SEL;
5619 src = src ? NISTC_ATRIG_ETC_GPFO_1_SEL : 0;
5621 devpriv->an_trig_etc_reg |= NISTC_ATRIG_ETC_GPFO_1_ENA | src;
5627 ni_stc_writew(dev, devpriv->an_trig_etc_reg, NISTC_ATRIG_ETC_REG);
5632 * Retrieves the current source of the output selector for the given
5633 * destination. If the terminal for the destination is not already configured
5634 * as an output, this function returns -EINVAL as error.
5636 * Return: the register value of the destination output selector;
5637 * -EINVAL if terminal is not configured for output.
5639 static int get_output_select_source(int dest, struct comedi_device *dev)
5641 struct ni_private *devpriv = dev->private;
5644 if (channel_is_pfi(dest)) {
5645 if (ni_get_pfi_direction(dev, dest) == COMEDI_OUTPUT)
5646 reg = ni_get_pfi_routing(dev, dest);
5647 } else if (channel_is_rtsi(dest)) {
5648 if (ni_get_rtsi_direction(dev, dest) == COMEDI_OUTPUT) {
5649 reg = ni_get_rtsi_routing(dev, dest);
5651 if (reg == NI_RTSI_OUTPUT_RGOUT0) {
5652 dest = NI_RGOUT0; /* prepare for lookup below */
5653 reg = get_rgout0_reg(dev);
5654 } else if (reg >= NI_RTSI_OUTPUT_RTSI_BRD(0) &&
5655 reg <= NI_RTSI_OUTPUT_RTSI_BRD(3)) {
5656 const int i = reg - NI_RTSI_OUTPUT_RTSI_BRD(0);
5658 dest = NI_RTSI_BRD(i); /* prepare for lookup */
5659 reg = get_ith_rtsi_brd_reg(i, dev);
5662 } else if (dest >= NI_CtrOut(0) && dest <= NI_CtrOut(-1)) {
5664 * not handled by ni_tio. Only available for GPFO registers in
5667 dest -= NI_CtrOut(0);
5669 /* there are only two g_out outputs. */
5671 reg = ni_get_gout_routing(dest, dev);
5672 } else if (channel_is_ctr(dest)) {
5673 reg = ni_tio_get_routing(devpriv->counter_dev, dest);
5675 dev_dbg(dev->class_dev, "%s: unhandled destination (%d) queried\n",
5680 return ni_find_route_source(CR_CHAN(reg), dest,
5681 &devpriv->routing_tables);
5688 * Return: -1 if not connectible;
5689 * 0 if connectible and not connected;
5690 * 1 if connectible and connected.
5692 static int test_route(unsigned int src, unsigned int dest,
5693 struct comedi_device *dev)
5695 struct ni_private *devpriv = dev->private;
5696 s8 reg = ni_route_to_register(CR_CHAN(src), dest,
5697 &devpriv->routing_tables);
5701 if (get_output_select_source(dest, dev) != CR_CHAN(src))
5706 /* Connect the actual route. */
5707 static int connect_route(unsigned int src, unsigned int dest,
5708 struct comedi_device *dev)
5710 struct ni_private *devpriv = dev->private;
5711 s8 reg = ni_route_to_register(CR_CHAN(src), dest,
5712 &devpriv->routing_tables);
5716 /* route is not valid */
5719 current_src = get_output_select_source(dest, dev);
5720 if (current_src == CR_CHAN(src))
5722 if (current_src >= 0)
5723 /* destination mux is already busy. complain, don't overwrite */
5726 /* The route is valid and available. Now connect... */
5727 if (channel_is_pfi(dest)) {
5728 /* set routing source, then open output */
5729 ni_set_pfi_routing(dev, dest, reg);
5730 ni_set_pfi_direction(dev, dest, COMEDI_OUTPUT);
5731 } else if (channel_is_rtsi(dest)) {
5732 if (reg == NI_RTSI_OUTPUT_RGOUT0) {
5733 int ret = incr_rgout0_src_use(src, dev);
5737 } else if (ni_rtsi_route_requires_mux(reg)) {
5738 /* Attempt to allocate and route (src->brd) */
5739 int brd = incr_rtsi_brd_src_use(src, dev);
5744 /* Now lookup the register value for (brd->dest) */
5745 reg = ni_lookup_route_register(
5746 brd, dest, &devpriv->routing_tables);
5749 ni_set_rtsi_direction(dev, dest, COMEDI_OUTPUT);
5750 ni_set_rtsi_routing(dev, dest, reg);
5751 } else if (dest >= NI_CtrOut(0) && dest <= NI_CtrOut(-1)) {
5753 * not handled by ni_tio. Only available for GPFO registers in
5756 dest -= NI_CtrOut(0);
5758 /* there are only two g_out outputs. */
5760 if (ni_set_gout_routing(src, dest, dev))
5762 } else if (channel_is_ctr(dest)) {
5764 * we are adding back the channel modifier info to set
5765 * invert/edge info passed by the user
5767 ni_tio_set_routing(devpriv->counter_dev, dest,
5768 reg | (src & ~CR_CHAN(-1)));
5775 static int disconnect_route(unsigned int src, unsigned int dest,
5776 struct comedi_device *dev)
5778 struct ni_private *devpriv = dev->private;
5779 s8 reg = ni_route_to_register(CR_CHAN(src), dest,
5780 &devpriv->routing_tables);
5783 /* route is not valid */
5785 if (get_output_select_source(dest, dev) != src)
5786 /* cannot disconnect something not connected */
5789 /* The route is valid and is connected. Now disconnect... */
5790 if (channel_is_pfi(dest)) {
5791 /* set the pfi to high impedance, and disconnect */
5792 ni_set_pfi_direction(dev, dest, COMEDI_INPUT);
5793 ni_set_pfi_routing(dev, dest, NI_PFI_OUTPUT_PFI_DEFAULT);
5794 } else if (channel_is_rtsi(dest)) {
5795 if (reg == NI_RTSI_OUTPUT_RGOUT0) {
5796 int ret = decr_rgout0_src_use(src, dev);
5800 } else if (ni_rtsi_route_requires_mux(reg)) {
5801 /* find which RTSI_BRD line is source for rtsi pin */
5802 int brd = ni_find_route_source(
5803 ni_get_rtsi_routing(dev, dest), dest,
5804 &devpriv->routing_tables);
5809 /* decrement/disconnect RTSI_BRD line from source */
5810 decr_rtsi_brd_src_use(src, brd, dev);
5813 /* set rtsi output selector to default state */
5814 reg = default_rtsi_routing[dest - TRIGGER_LINE(0)];
5815 ni_set_rtsi_direction(dev, dest, COMEDI_INPUT);
5816 ni_set_rtsi_routing(dev, dest, reg);
5817 } else if (dest >= NI_CtrOut(0) && dest <= NI_CtrOut(-1)) {
5819 * not handled by ni_tio. Only available for GPFO registers in
5822 dest -= NI_CtrOut(0);
5824 /* there are only two g_out outputs. */
5826 reg = ni_disable_gout_routing(dest, dev);
5827 } else if (channel_is_ctr(dest)) {
5828 ni_tio_unset_routing(devpriv->counter_dev, dest);
5835 static int ni_global_insn_config(struct comedi_device *dev,
5836 struct comedi_insn *insn,
5840 case INSN_DEVICE_CONFIG_TEST_ROUTE:
5841 data[0] = test_route(data[1], data[2], dev);
5843 case INSN_DEVICE_CONFIG_CONNECT_ROUTE:
5844 return connect_route(data[1], data[2], dev);
5845 case INSN_DEVICE_CONFIG_DISCONNECT_ROUTE:
5846 return disconnect_route(data[1], data[2], dev);
5848 * This case is already handled one level up.
5849 * case INSN_DEVICE_CONFIG_GET_ROUTES:
5858 static int ni_gpct_cmd(struct comedi_device *dev, struct comedi_subdevice *s)
5860 struct ni_gpct *counter = s->private;
5863 retval = ni_request_gpct_mite_channel(dev, counter->counter_index,
5866 dev_err(dev->class_dev,
5867 "no dma channel available for use by counter\n");
5870 ni_tio_acknowledge(counter);
5871 ni_e_series_enable_second_irq(dev, counter->counter_index, 1);
5873 return ni_tio_cmd(dev, s);
5876 static int ni_gpct_cancel(struct comedi_device *dev, struct comedi_subdevice *s)
5878 struct ni_gpct *counter = s->private;
5881 retval = ni_tio_cancel(counter);
5882 ni_e_series_enable_second_irq(dev, counter->counter_index, 0);
5883 ni_release_gpct_mite_channel(dev, counter->counter_index);
5888 static irqreturn_t ni_E_interrupt(int irq, void *d)
5890 struct comedi_device *dev = d;
5891 struct comedi_subdevice *s_ai = dev->read_subdev;
5892 struct comedi_subdevice *s_ao = dev->write_subdev;
5893 unsigned short a_status;
5894 unsigned short b_status;
5895 unsigned long flags;
5897 struct ni_private *devpriv = dev->private;
5902 smp_mb(); /* make sure dev->attached is checked */
5904 /* lock to avoid race with comedi_poll */
5905 spin_lock_irqsave(&dev->spinlock, flags);
5906 a_status = ni_stc_readw(dev, NISTC_AI_STATUS1_REG);
5907 b_status = ni_stc_readw(dev, NISTC_AO_STATUS1_REG);
5909 if (devpriv->mite) {
5910 unsigned long flags_too;
5912 spin_lock_irqsave(&devpriv->mite_channel_lock, flags_too);
5913 if (s_ai && devpriv->ai_mite_chan)
5914 mite_ack_linkc(devpriv->ai_mite_chan, s_ai, false);
5915 if (s_ao && devpriv->ao_mite_chan)
5916 mite_ack_linkc(devpriv->ao_mite_chan, s_ao, false);
5917 spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags_too);
5920 ack_a_interrupt(dev, a_status);
5921 ack_b_interrupt(dev, b_status);
5923 if (a_status & NISTC_AI_STATUS1_INTA)
5924 handle_a_interrupt(dev, s_ai, a_status);
5925 /* handle any interrupt or dma events */
5926 comedi_handle_events(dev, s_ai);
5929 if (b_status & NISTC_AO_STATUS1_INTB)
5930 handle_b_interrupt(dev, s_ao, b_status);
5931 /* handle any interrupt or dma events */
5932 comedi_handle_events(dev, s_ao);
5934 handle_gpct_interrupt(dev, 0);
5935 handle_gpct_interrupt(dev, 1);
5937 if (devpriv->is_m_series)
5938 handle_cdio_interrupt(dev);
5941 spin_unlock_irqrestore(&dev->spinlock, flags);
5945 static int ni_alloc_private(struct comedi_device *dev)
5947 struct ni_private *devpriv;
5949 devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
5953 spin_lock_init(&devpriv->window_lock);
5954 spin_lock_init(&devpriv->soft_reg_copy_lock);
5955 spin_lock_init(&devpriv->mite_channel_lock);
5960 static unsigned int _ni_get_valid_routes(struct comedi_device *dev,
5961 unsigned int n_pairs,
5962 unsigned int *pair_data)
5964 struct ni_private *devpriv = dev->private;
5966 return ni_get_valid_routes(&devpriv->routing_tables, n_pairs,
5970 static int ni_E_init(struct comedi_device *dev,
5971 unsigned int interrupt_pin, unsigned int irq_polarity)
5973 const struct ni_board_struct *board = dev->board_ptr;
5974 struct ni_private *devpriv = dev->private;
5975 struct comedi_subdevice *s;
5978 const char *dev_family = devpriv->is_m_series ? "ni_mseries"
5981 /* prepare the device for globally-named routes. */
5982 if (ni_assign_device_routes(dev_family, board->name,
5983 board->alt_route_name,
5984 &devpriv->routing_tables) < 0) {
5985 dev_warn(dev->class_dev, "%s: %s device has no signal routing table.\n",
5986 __func__, board->name);
5987 dev_warn(dev->class_dev, "%s: High level NI signal names will not be available for this %s board.\n",
5988 __func__, board->name);
5991 * only(?) assign insn_device_config if we have global names for
5994 dev->insn_device_config = ni_global_insn_config;
5995 dev->get_valid_routes = _ni_get_valid_routes;
5998 if (board->n_aochan > MAX_N_AO_CHAN) {
5999 dev_err(dev->class_dev, "bug! n_aochan > MAX_N_AO_CHAN\n");
6003 /* initialize clock dividers */
6004 devpriv->clock_and_fout = NISTC_CLK_FOUT_SLOW_DIV2 |
6005 NISTC_CLK_FOUT_SLOW_TIMEBASE |
6006 NISTC_CLK_FOUT_TO_BOARD_DIV2 |
6007 NISTC_CLK_FOUT_TO_BOARD;
6008 if (!devpriv->is_6xxx) {
6009 /* BEAM is this needed for PCI-6143 ?? */
6010 devpriv->clock_and_fout |= (NISTC_CLK_FOUT_AI_OUT_DIV2 |
6011 NISTC_CLK_FOUT_AO_OUT_DIV2);
6013 ni_stc_writew(dev, devpriv->clock_and_fout, NISTC_CLK_FOUT_REG);
6015 ret = comedi_alloc_subdevices(dev, NI_NUM_SUBDEVICES);
6019 /* Analog Input subdevice */
6020 s = &dev->subdevices[NI_AI_SUBDEV];
6021 if (board->n_adchan) {
6022 s->type = COMEDI_SUBD_AI;
6023 s->subdev_flags = SDF_READABLE | SDF_DIFF | SDF_DITHER;
6024 if (!devpriv->is_611x)
6025 s->subdev_flags |= SDF_GROUND | SDF_COMMON | SDF_OTHER;
6026 if (board->ai_maxdata > 0xffff)
6027 s->subdev_flags |= SDF_LSAMPL;
6028 if (devpriv->is_m_series)
6029 s->subdev_flags |= SDF_SOFT_CALIBRATED;
6030 s->n_chan = board->n_adchan;
6031 s->maxdata = board->ai_maxdata;
6032 s->range_table = ni_range_lkup[board->gainlkup];
6033 s->insn_read = ni_ai_insn_read;
6034 s->insn_config = ni_ai_insn_config;
6036 dev->read_subdev = s;
6037 s->subdev_flags |= SDF_CMD_READ;
6038 s->len_chanlist = 512;
6039 s->do_cmdtest = ni_ai_cmdtest;
6040 s->do_cmd = ni_ai_cmd;
6041 s->cancel = ni_ai_reset;
6042 s->poll = ni_ai_poll;
6043 s->munge = ni_ai_munge;
6046 s->async_dma_dir = DMA_FROM_DEVICE;
6049 /* reset the analog input configuration */
6050 ni_ai_reset(dev, s);
6052 s->type = COMEDI_SUBD_UNUSED;
6055 /* Analog Output subdevice */
6056 s = &dev->subdevices[NI_AO_SUBDEV];
6057 if (board->n_aochan) {
6058 s->type = COMEDI_SUBD_AO;
6059 s->subdev_flags = SDF_WRITABLE | SDF_DEGLITCH | SDF_GROUND;
6060 if (devpriv->is_m_series)
6061 s->subdev_flags |= SDF_SOFT_CALIBRATED;
6062 s->n_chan = board->n_aochan;
6063 s->maxdata = board->ao_maxdata;
6064 s->range_table = board->ao_range_table;
6065 s->insn_config = ni_ao_insn_config;
6066 s->insn_write = ni_ao_insn_write;
6068 ret = comedi_alloc_subdev_readback(s);
6073 * Along with the IRQ we need either a FIFO or DMA for
6074 * async command support.
6076 if (dev->irq && (board->ao_fifo_depth || devpriv->mite)) {
6077 dev->write_subdev = s;
6078 s->subdev_flags |= SDF_CMD_WRITE;
6079 s->len_chanlist = s->n_chan;
6080 s->do_cmdtest = ni_ao_cmdtest;
6081 s->do_cmd = ni_ao_cmd;
6082 s->cancel = ni_ao_reset;
6083 if (!devpriv->is_m_series)
6084 s->munge = ni_ao_munge;
6087 s->async_dma_dir = DMA_TO_DEVICE;
6090 if (devpriv->is_67xx)
6091 init_ao_67xx(dev, s);
6093 /* reset the analog output configuration */
6094 ni_ao_reset(dev, s);
6096 s->type = COMEDI_SUBD_UNUSED;
6099 /* Digital I/O subdevice */
6100 s = &dev->subdevices[NI_DIO_SUBDEV];
6101 s->type = COMEDI_SUBD_DIO;
6102 s->subdev_flags = SDF_WRITABLE | SDF_READABLE;
6103 s->n_chan = board->has_32dio_chan ? 32 : 8;
6105 s->range_table = &range_digital;
6106 if (devpriv->is_m_series) {
6108 s->subdev_flags |= SDF_LSAMPL;
6109 s->insn_bits = ni_m_series_dio_insn_bits;
6110 s->insn_config = ni_m_series_dio_insn_config;
6112 s->subdev_flags |= SDF_CMD_WRITE /* | SDF_CMD_READ */;
6113 s->len_chanlist = s->n_chan;
6114 s->do_cmdtest = ni_cdio_cmdtest;
6115 s->do_cmd = ni_cdio_cmd;
6116 s->cancel = ni_cdio_cancel;
6118 /* M-series boards use DMA */
6119 s->async_dma_dir = DMA_BIDIRECTIONAL;
6122 /* reset DIO and set all channels to inputs */
6123 ni_writel(dev, NI_M_CDO_CMD_RESET |
6126 ni_writel(dev, s->io_bits, NI_M_DIO_DIR_REG);
6129 s->insn_bits = ni_dio_insn_bits;
6130 s->insn_config = ni_dio_insn_config;
6132 /* set all channels to inputs */
6133 devpriv->dio_control = NISTC_DIO_CTRL_DIR(s->io_bits);
6134 ni_writew(dev, devpriv->dio_control, NISTC_DIO_CTRL_REG);
6138 s = &dev->subdevices[NI_8255_DIO_SUBDEV];
6139 if (board->has_8255) {
6140 ret = subdev_8255_init(dev, s, ni_8255_callback,
6145 s->type = COMEDI_SUBD_UNUSED;
6148 /* formerly general purpose counter/timer device, but no longer used */
6149 s = &dev->subdevices[NI_UNUSED_SUBDEV];
6150 s->type = COMEDI_SUBD_UNUSED;
6152 /* Calibration subdevice */
6153 s = &dev->subdevices[NI_CALIBRATION_SUBDEV];
6154 s->type = COMEDI_SUBD_CALIB;
6155 s->subdev_flags = SDF_INTERNAL;
6158 if (devpriv->is_m_series) {
6159 /* internal PWM output used for AI nonlinearity calibration */
6160 s->insn_config = ni_m_series_pwm_config;
6162 ni_writel(dev, 0x0, NI_M_CAL_PWM_REG);
6163 } else if (devpriv->is_6143) {
6164 /* internal PWM output used for AI nonlinearity calibration */
6165 s->insn_config = ni_6143_pwm_config;
6167 s->subdev_flags |= SDF_WRITABLE;
6168 s->insn_read = ni_calib_insn_read;
6169 s->insn_write = ni_calib_insn_write;
6171 /* setup the caldacs and find the real n_chan and maxdata */
6172 caldac_setup(dev, s);
6175 /* EEPROM subdevice */
6176 s = &dev->subdevices[NI_EEPROM_SUBDEV];
6177 s->type = COMEDI_SUBD_MEMORY;
6178 s->subdev_flags = SDF_READABLE | SDF_INTERNAL;
6180 if (devpriv->is_m_series) {
6181 s->n_chan = M_SERIES_EEPROM_SIZE;
6182 s->insn_read = ni_m_series_eeprom_insn_read;
6185 s->insn_read = ni_eeprom_insn_read;
6188 /* Digital I/O (PFI) subdevice */
6189 s = &dev->subdevices[NI_PFI_DIO_SUBDEV];
6190 s->type = COMEDI_SUBD_DIO;
6192 if (devpriv->is_m_series) {
6194 s->insn_bits = ni_pfi_insn_bits;
6195 s->subdev_flags = SDF_READABLE | SDF_WRITABLE | SDF_INTERNAL;
6197 ni_writew(dev, s->state, NI_M_PFI_DO_REG);
6198 for (i = 0; i < NUM_PFI_OUTPUT_SELECT_REGS; ++i) {
6199 ni_writew(dev, devpriv->pfi_output_select_reg[i],
6200 NI_M_PFI_OUT_SEL_REG(i));
6204 s->subdev_flags = SDF_INTERNAL;
6206 s->insn_config = ni_pfi_insn_config;
6208 ni_set_bits(dev, NISTC_IO_BIDIR_PIN_REG, ~0, 0);
6210 /* cs5529 calibration adc */
6211 s = &dev->subdevices[NI_CS5529_CALIBRATION_SUBDEV];
6212 if (devpriv->is_67xx) {
6213 s->type = COMEDI_SUBD_AI;
6214 s->subdev_flags = SDF_READABLE | SDF_DIFF | SDF_INTERNAL;
6215 /* one channel for each analog output channel */
6216 s->n_chan = board->n_aochan;
6217 s->maxdata = BIT(16) - 1;
6218 s->range_table = &range_unknown; /* XXX */
6219 s->insn_read = cs5529_ai_insn_read;
6220 s->insn_config = NULL;
6223 s->type = COMEDI_SUBD_UNUSED;
6227 s = &dev->subdevices[NI_SERIAL_SUBDEV];
6228 s->type = COMEDI_SUBD_SERIAL;
6229 s->subdev_flags = SDF_READABLE | SDF_WRITABLE | SDF_INTERNAL;
6232 s->insn_config = ni_serial_insn_config;
6233 devpriv->serial_interval_ns = 0;
6234 devpriv->serial_hw_mode = 0;
6237 s = &dev->subdevices[NI_RTSI_SUBDEV];
6238 s->type = COMEDI_SUBD_DIO;
6239 s->subdev_flags = SDF_READABLE | SDF_WRITABLE | SDF_INTERNAL;
6242 s->insn_bits = ni_rtsi_insn_bits;
6243 s->insn_config = ni_rtsi_insn_config;
6246 /* allocate and initialize the gpct counter device */
6247 devpriv->counter_dev = ni_gpct_device_construct(dev,
6248 ni_gpct_write_register,
6249 ni_gpct_read_register,
6250 (devpriv->is_m_series)
6251 ? ni_gpct_variant_m_series
6252 : ni_gpct_variant_e_series,
6255 &devpriv->routing_tables);
6256 if (!devpriv->counter_dev)
6259 /* Counter (gpct) subdevices */
6260 for (i = 0; i < NUM_GPCT; ++i) {
6261 struct ni_gpct *gpct = &devpriv->counter_dev->counters[i];
6263 /* setup and initialize the counter */
6264 ni_tio_init_counter(gpct);
6266 s = &dev->subdevices[NI_GPCT_SUBDEV(i)];
6267 s->type = COMEDI_SUBD_COUNTER;
6268 s->subdev_flags = SDF_READABLE | SDF_WRITABLE | SDF_LSAMPL;
6270 s->maxdata = (devpriv->is_m_series) ? 0xffffffff
6272 s->insn_read = ni_tio_insn_read;
6273 s->insn_write = ni_tio_insn_write;
6274 s->insn_config = ni_tio_insn_config;
6276 if (dev->irq && devpriv->mite) {
6277 s->subdev_flags |= SDF_CMD_READ /* | SDF_CMD_WRITE */;
6278 s->len_chanlist = 1;
6279 s->do_cmdtest = ni_tio_cmdtest;
6280 s->do_cmd = ni_gpct_cmd;
6281 s->cancel = ni_gpct_cancel;
6283 s->async_dma_dir = DMA_BIDIRECTIONAL;
6289 /* Initialize GPFO_{0,1} to produce output of counters */
6290 ni_set_gout_routing(0, 0, dev); /* output of counter 0; DAQ STC, p338 */
6291 ni_set_gout_routing(0, 1, dev); /* output of counter 1; DAQ STC, p338 */
6293 /* Frequency output subdevice */
6294 s = &dev->subdevices[NI_FREQ_OUT_SUBDEV];
6295 s->type = COMEDI_SUBD_COUNTER;
6296 s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
6299 s->insn_read = ni_freq_out_insn_read;
6300 s->insn_write = ni_freq_out_insn_write;
6301 s->insn_config = ni_freq_out_insn_config;
6305 (irq_polarity ? NISTC_INT_CTRL_INT_POL : 0) |
6306 (NISTC_INT_CTRL_3PIN_INT & 0) |
6307 NISTC_INT_CTRL_INTA_ENA |
6308 NISTC_INT_CTRL_INTB_ENA |
6309 NISTC_INT_CTRL_INTA_SEL(interrupt_pin) |
6310 NISTC_INT_CTRL_INTB_SEL(interrupt_pin),
6311 NISTC_INT_CTRL_REG);
6315 ni_writeb(dev, devpriv->ai_ao_select_reg, NI_E_DMA_AI_AO_SEL_REG);
6316 ni_writeb(dev, devpriv->g0_g1_select_reg, NI_E_DMA_G0_G1_SEL_REG);
6318 if (devpriv->is_6xxx) {
6319 ni_writeb(dev, 0, NI611X_MAGIC_REG);
6320 } else if (devpriv->is_m_series) {
6323 for (channel = 0; channel < board->n_aochan; ++channel) {
6325 NI_M_AO_WAVEFORM_ORDER_REG(channel));
6327 NI_M_AO_REF_ATTENUATION_REG(channel));
6329 ni_writeb(dev, 0x0, NI_M_AO_CALIB_REG);
6335 static void mio_common_detach(struct comedi_device *dev)
6337 struct ni_private *devpriv = dev->private;
6340 ni_gpct_device_destroy(devpriv->counter_dev);