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>
46 #include <linux/comedi/comedi_8255.h>
56 #define NI_TIMEOUT 1000
58 /* Note: this table must match the ai_gain_* definitions */
59 static const short ni_gainlkup[][16] = {
60 [ai_gain_16] = {0, 1, 2, 3, 4, 5, 6, 7,
61 0x100, 0x101, 0x102, 0x103, 0x104, 0x105, 0x106, 0x107},
62 [ai_gain_8] = {1, 2, 4, 7, 0x101, 0x102, 0x104, 0x107},
63 [ai_gain_14] = {1, 2, 3, 4, 5, 6, 7,
64 0x101, 0x102, 0x103, 0x104, 0x105, 0x106, 0x107},
65 [ai_gain_4] = {0, 1, 4, 7},
66 [ai_gain_611x] = {0x00a, 0x00b, 0x001, 0x002,
67 0x003, 0x004, 0x005, 0x006},
68 [ai_gain_622x] = {0, 1, 4, 5},
69 [ai_gain_628x] = {1, 2, 3, 4, 5, 6, 7},
70 [ai_gain_6143] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
73 static const struct comedi_lrange range_ni_E_ai = {
94 static const struct comedi_lrange range_ni_E_ai_limited = {
107 static const struct comedi_lrange range_ni_E_ai_limited14 = {
126 static const struct comedi_lrange range_ni_E_ai_bipolar4 = {
135 static const struct comedi_lrange range_ni_E_ai_611x = {
148 static const struct comedi_lrange range_ni_M_ai_622x = {
157 static const struct comedi_lrange range_ni_M_ai_628x = {
169 static const struct comedi_lrange range_ni_E_ao_ext = {
178 static const struct comedi_lrange *const ni_range_lkup[] = {
179 [ai_gain_16] = &range_ni_E_ai,
180 [ai_gain_8] = &range_ni_E_ai_limited,
181 [ai_gain_14] = &range_ni_E_ai_limited14,
182 [ai_gain_4] = &range_ni_E_ai_bipolar4,
183 [ai_gain_611x] = &range_ni_E_ai_611x,
184 [ai_gain_622x] = &range_ni_M_ai_622x,
185 [ai_gain_628x] = &range_ni_M_ai_628x,
186 [ai_gain_6143] = &range_bipolar5
191 AIMODE_HALF_FULL = 1,
196 enum ni_common_subdevices {
202 NI_CALIBRATION_SUBDEV,
205 NI_CS5529_CALIBRATION_SUBDEV,
214 #define NI_GPCT_SUBDEV(x) (NI_GPCT0_SUBDEV + (x))
216 enum timebase_nanoseconds {
218 TIMEBASE_2_NS = 10000
221 #define SERIAL_DISABLED 0
222 #define SERIAL_600NS 600
223 #define SERIAL_1_2US 1200
224 #define SERIAL_10US 10000
226 static const int num_adc_stages_611x = 3;
230 static void ni_writel(struct comedi_device *dev, unsigned int data, int reg)
232 writel(data, dev->mmio + reg);
235 static void ni_writew(struct comedi_device *dev, unsigned int data, int reg)
237 writew(data, dev->mmio + reg);
240 static void ni_writeb(struct comedi_device *dev, unsigned int data, int reg)
242 writeb(data, dev->mmio + reg);
245 static unsigned int ni_readl(struct comedi_device *dev, int reg)
247 return readl(dev->mmio + reg);
250 static unsigned int ni_readw(struct comedi_device *dev, int reg)
252 return readw(dev->mmio + reg);
255 static unsigned int ni_readb(struct comedi_device *dev, int reg)
257 return readb(dev->mmio + reg);
262 static void ni_writel(struct comedi_device *dev, unsigned int data, int reg)
264 outl(data, dev->iobase + reg);
267 static void ni_writew(struct comedi_device *dev, unsigned int data, int reg)
269 outw(data, dev->iobase + reg);
272 static void ni_writeb(struct comedi_device *dev, unsigned int data, int reg)
274 outb(data, dev->iobase + reg);
277 static unsigned int ni_readl(struct comedi_device *dev, int reg)
279 return inl(dev->iobase + reg);
282 static unsigned int ni_readw(struct comedi_device *dev, int reg)
284 return inw(dev->iobase + reg);
287 static unsigned int ni_readb(struct comedi_device *dev, int reg)
289 return inb(dev->iobase + reg);
295 * We automatically take advantage of STC registers that can be
296 * read/written directly in the I/O space of the board.
298 * The AT-MIO and DAQCard devices map the low 8 STC registers to
301 * Most PCIMIO devices also map the low 8 STC registers but the
302 * 611x devices map the read registers to iobase+(addr-1)*2.
303 * For now non-windowed STC access is disabled if a PCIMIO device
304 * is detected (devpriv->mite has been initialized).
306 * The M series devices do not used windowed registers for the
307 * STC registers. The functions below handle the mapping of the
308 * windowed STC registers to the m series register offsets.
312 unsigned int mio_reg;
316 static const struct mio_regmap m_series_stc_write_regmap[] = {
317 [NISTC_INTA_ACK_REG] = { 0x104, 2 },
318 [NISTC_INTB_ACK_REG] = { 0x106, 2 },
319 [NISTC_AI_CMD2_REG] = { 0x108, 2 },
320 [NISTC_AO_CMD2_REG] = { 0x10a, 2 },
321 [NISTC_G0_CMD_REG] = { 0x10c, 2 },
322 [NISTC_G1_CMD_REG] = { 0x10e, 2 },
323 [NISTC_AI_CMD1_REG] = { 0x110, 2 },
324 [NISTC_AO_CMD1_REG] = { 0x112, 2 },
326 * NISTC_DIO_OUT_REG maps to:
327 * { NI_M_DIO_REG, 4 } and { NI_M_SCXI_SER_DO_REG, 1 }
329 [NISTC_DIO_OUT_REG] = { 0, 0 }, /* DOES NOT MAP CLEANLY */
330 [NISTC_DIO_CTRL_REG] = { 0, 0 }, /* DOES NOT MAP CLEANLY */
331 [NISTC_AI_MODE1_REG] = { 0x118, 2 },
332 [NISTC_AI_MODE2_REG] = { 0x11a, 2 },
333 [NISTC_AI_SI_LOADA_REG] = { 0x11c, 4 },
334 [NISTC_AI_SI_LOADB_REG] = { 0x120, 4 },
335 [NISTC_AI_SC_LOADA_REG] = { 0x124, 4 },
336 [NISTC_AI_SC_LOADB_REG] = { 0x128, 4 },
337 [NISTC_AI_SI2_LOADA_REG] = { 0x12c, 4 },
338 [NISTC_AI_SI2_LOADB_REG] = { 0x130, 4 },
339 [NISTC_G0_MODE_REG] = { 0x134, 2 },
340 [NISTC_G1_MODE_REG] = { 0x136, 2 },
341 [NISTC_G0_LOADA_REG] = { 0x138, 4 },
342 [NISTC_G0_LOADB_REG] = { 0x13c, 4 },
343 [NISTC_G1_LOADA_REG] = { 0x140, 4 },
344 [NISTC_G1_LOADB_REG] = { 0x144, 4 },
345 [NISTC_G0_INPUT_SEL_REG] = { 0x148, 2 },
346 [NISTC_G1_INPUT_SEL_REG] = { 0x14a, 2 },
347 [NISTC_AO_MODE1_REG] = { 0x14c, 2 },
348 [NISTC_AO_MODE2_REG] = { 0x14e, 2 },
349 [NISTC_AO_UI_LOADA_REG] = { 0x150, 4 },
350 [NISTC_AO_UI_LOADB_REG] = { 0x154, 4 },
351 [NISTC_AO_BC_LOADA_REG] = { 0x158, 4 },
352 [NISTC_AO_BC_LOADB_REG] = { 0x15c, 4 },
353 [NISTC_AO_UC_LOADA_REG] = { 0x160, 4 },
354 [NISTC_AO_UC_LOADB_REG] = { 0x164, 4 },
355 [NISTC_CLK_FOUT_REG] = { 0x170, 2 },
356 [NISTC_IO_BIDIR_PIN_REG] = { 0x172, 2 },
357 [NISTC_RTSI_TRIG_DIR_REG] = { 0x174, 2 },
358 [NISTC_INT_CTRL_REG] = { 0x176, 2 },
359 [NISTC_AI_OUT_CTRL_REG] = { 0x178, 2 },
360 [NISTC_ATRIG_ETC_REG] = { 0x17a, 2 },
361 [NISTC_AI_START_STOP_REG] = { 0x17c, 2 },
362 [NISTC_AI_TRIG_SEL_REG] = { 0x17e, 2 },
363 [NISTC_AI_DIV_LOADA_REG] = { 0x180, 4 },
364 [NISTC_AO_START_SEL_REG] = { 0x184, 2 },
365 [NISTC_AO_TRIG_SEL_REG] = { 0x186, 2 },
366 [NISTC_G0_AUTOINC_REG] = { 0x188, 2 },
367 [NISTC_G1_AUTOINC_REG] = { 0x18a, 2 },
368 [NISTC_AO_MODE3_REG] = { 0x18c, 2 },
369 [NISTC_RESET_REG] = { 0x190, 2 },
370 [NISTC_INTA_ENA_REG] = { 0x192, 2 },
371 [NISTC_INTA2_ENA_REG] = { 0, 0 }, /* E-Series only */
372 [NISTC_INTB_ENA_REG] = { 0x196, 2 },
373 [NISTC_INTB2_ENA_REG] = { 0, 0 }, /* E-Series only */
374 [NISTC_AI_PERSONAL_REG] = { 0x19a, 2 },
375 [NISTC_AO_PERSONAL_REG] = { 0x19c, 2 },
376 [NISTC_RTSI_TRIGA_OUT_REG] = { 0x19e, 2 },
377 [NISTC_RTSI_TRIGB_OUT_REG] = { 0x1a0, 2 },
378 /* doc for following line: mhddk/nimseries/ChipObjects/tMSeries.h */
379 [NISTC_RTSI_BOARD_REG] = { 0x1a2, 2 },
380 [NISTC_CFG_MEM_CLR_REG] = { 0x1a4, 2 },
381 [NISTC_ADC_FIFO_CLR_REG] = { 0x1a6, 2 },
382 [NISTC_DAC_FIFO_CLR_REG] = { 0x1a8, 2 },
383 [NISTC_AO_OUT_CTRL_REG] = { 0x1ac, 2 },
384 [NISTC_AI_MODE3_REG] = { 0x1ae, 2 },
387 static void m_series_stc_write(struct comedi_device *dev,
388 unsigned int data, unsigned int reg)
390 const struct mio_regmap *regmap;
392 if (reg < ARRAY_SIZE(m_series_stc_write_regmap)) {
393 regmap = &m_series_stc_write_regmap[reg];
395 dev_warn(dev->class_dev, "%s: unhandled register=0x%x\n",
400 switch (regmap->size) {
402 ni_writel(dev, data, regmap->mio_reg);
405 ni_writew(dev, data, regmap->mio_reg);
408 dev_warn(dev->class_dev, "%s: unmapped register=0x%x\n",
414 static const struct mio_regmap m_series_stc_read_regmap[] = {
415 [NISTC_AI_STATUS1_REG] = { 0x104, 2 },
416 [NISTC_AO_STATUS1_REG] = { 0x106, 2 },
417 [NISTC_G01_STATUS_REG] = { 0x108, 2 },
418 [NISTC_AI_STATUS2_REG] = { 0, 0 }, /* Unknown */
419 [NISTC_AO_STATUS2_REG] = { 0x10c, 2 },
420 [NISTC_DIO_IN_REG] = { 0, 0 }, /* Unknown */
421 [NISTC_G0_HW_SAVE_REG] = { 0x110, 4 },
422 [NISTC_G1_HW_SAVE_REG] = { 0x114, 4 },
423 [NISTC_G0_SAVE_REG] = { 0x118, 4 },
424 [NISTC_G1_SAVE_REG] = { 0x11c, 4 },
425 [NISTC_AO_UI_SAVE_REG] = { 0x120, 4 },
426 [NISTC_AO_BC_SAVE_REG] = { 0x124, 4 },
427 [NISTC_AO_UC_SAVE_REG] = { 0x128, 4 },
428 [NISTC_STATUS1_REG] = { 0x136, 2 },
429 [NISTC_DIO_SERIAL_IN_REG] = { 0x009, 1 },
430 [NISTC_STATUS2_REG] = { 0x13a, 2 },
431 [NISTC_AI_SI_SAVE_REG] = { 0x180, 4 },
432 [NISTC_AI_SC_SAVE_REG] = { 0x184, 4 },
435 static unsigned int m_series_stc_read(struct comedi_device *dev,
438 const struct mio_regmap *regmap;
440 if (reg < ARRAY_SIZE(m_series_stc_read_regmap)) {
441 regmap = &m_series_stc_read_regmap[reg];
443 dev_warn(dev->class_dev, "%s: unhandled register=0x%x\n",
448 switch (regmap->size) {
450 return ni_readl(dev, regmap->mio_reg);
452 return ni_readw(dev, regmap->mio_reg);
454 return ni_readb(dev, regmap->mio_reg);
456 dev_warn(dev->class_dev, "%s: unmapped register=0x%x\n",
462 static void ni_stc_writew(struct comedi_device *dev,
463 unsigned int data, int reg)
465 struct ni_private *devpriv = dev->private;
468 if (devpriv->is_m_series) {
469 m_series_stc_write(dev, data, reg);
471 spin_lock_irqsave(&devpriv->window_lock, flags);
472 if (!devpriv->mite && reg < 8) {
473 ni_writew(dev, data, reg * 2);
475 ni_writew(dev, reg, NI_E_STC_WINDOW_ADDR_REG);
476 ni_writew(dev, data, NI_E_STC_WINDOW_DATA_REG);
478 spin_unlock_irqrestore(&devpriv->window_lock, flags);
482 static void ni_stc_writel(struct comedi_device *dev,
483 unsigned int data, int reg)
485 struct ni_private *devpriv = dev->private;
487 if (devpriv->is_m_series) {
488 m_series_stc_write(dev, data, reg);
490 ni_stc_writew(dev, data >> 16, reg);
491 ni_stc_writew(dev, data & 0xffff, reg + 1);
495 static unsigned int ni_stc_readw(struct comedi_device *dev, int reg)
497 struct ni_private *devpriv = dev->private;
501 if (devpriv->is_m_series) {
502 val = m_series_stc_read(dev, reg);
504 spin_lock_irqsave(&devpriv->window_lock, flags);
505 if (!devpriv->mite && reg < 8) {
506 val = ni_readw(dev, reg * 2);
508 ni_writew(dev, reg, NI_E_STC_WINDOW_ADDR_REG);
509 val = ni_readw(dev, NI_E_STC_WINDOW_DATA_REG);
511 spin_unlock_irqrestore(&devpriv->window_lock, flags);
516 static unsigned int ni_stc_readl(struct comedi_device *dev, int reg)
518 struct ni_private *devpriv = dev->private;
521 if (devpriv->is_m_series) {
522 val = m_series_stc_read(dev, reg);
524 val = ni_stc_readw(dev, reg) << 16;
525 val |= ni_stc_readw(dev, reg + 1);
530 static inline void ni_set_bitfield(struct comedi_device *dev, int reg,
531 unsigned int bit_mask,
532 unsigned int bit_values)
534 struct ni_private *devpriv = dev->private;
537 spin_lock_irqsave(&devpriv->soft_reg_copy_lock, flags);
539 case NISTC_INTA_ENA_REG:
540 devpriv->int_a_enable_reg &= ~bit_mask;
541 devpriv->int_a_enable_reg |= bit_values & bit_mask;
542 ni_stc_writew(dev, devpriv->int_a_enable_reg, reg);
544 case NISTC_INTB_ENA_REG:
545 devpriv->int_b_enable_reg &= ~bit_mask;
546 devpriv->int_b_enable_reg |= bit_values & bit_mask;
547 ni_stc_writew(dev, devpriv->int_b_enable_reg, reg);
549 case NISTC_IO_BIDIR_PIN_REG:
550 devpriv->io_bidirection_pin_reg &= ~bit_mask;
551 devpriv->io_bidirection_pin_reg |= bit_values & bit_mask;
552 ni_stc_writew(dev, devpriv->io_bidirection_pin_reg, reg);
554 case NI_E_DMA_AI_AO_SEL_REG:
555 devpriv->ai_ao_select_reg &= ~bit_mask;
556 devpriv->ai_ao_select_reg |= bit_values & bit_mask;
557 ni_writeb(dev, devpriv->ai_ao_select_reg, reg);
559 case NI_E_DMA_G0_G1_SEL_REG:
560 devpriv->g0_g1_select_reg &= ~bit_mask;
561 devpriv->g0_g1_select_reg |= bit_values & bit_mask;
562 ni_writeb(dev, devpriv->g0_g1_select_reg, reg);
564 case NI_M_CDIO_DMA_SEL_REG:
565 devpriv->cdio_dma_select_reg &= ~bit_mask;
566 devpriv->cdio_dma_select_reg |= bit_values & bit_mask;
567 ni_writeb(dev, devpriv->cdio_dma_select_reg, reg);
570 dev_err(dev->class_dev, "called with invalid register %d\n",
574 spin_unlock_irqrestore(&devpriv->soft_reg_copy_lock, flags);
579 /* selects the MITE channel to use for DMA */
580 #define NI_STC_DMA_CHAN_SEL(x) (((x) < 4) ? BIT(x) : \
582 ((x) == 5) ? 0x5 : 0x0)
584 /* DMA channel setup */
585 static int ni_request_ai_mite_channel(struct comedi_device *dev)
587 struct ni_private *devpriv = dev->private;
588 struct mite_channel *mite_chan;
592 spin_lock_irqsave(&devpriv->mite_channel_lock, flags);
593 mite_chan = mite_request_channel(devpriv->mite, devpriv->ai_mite_ring);
595 spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
596 dev_err(dev->class_dev,
597 "failed to reserve mite dma channel for analog input\n");
600 mite_chan->dir = COMEDI_INPUT;
601 devpriv->ai_mite_chan = mite_chan;
603 bits = NI_STC_DMA_CHAN_SEL(mite_chan->channel);
604 ni_set_bitfield(dev, NI_E_DMA_AI_AO_SEL_REG,
605 NI_E_DMA_AI_SEL_MASK, NI_E_DMA_AI_SEL(bits));
607 spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
611 static int ni_request_ao_mite_channel(struct comedi_device *dev)
613 struct ni_private *devpriv = dev->private;
614 struct mite_channel *mite_chan;
618 spin_lock_irqsave(&devpriv->mite_channel_lock, flags);
619 mite_chan = mite_request_channel(devpriv->mite, devpriv->ao_mite_ring);
621 spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
622 dev_err(dev->class_dev,
623 "failed to reserve mite dma channel for analog output\n");
626 mite_chan->dir = COMEDI_OUTPUT;
627 devpriv->ao_mite_chan = mite_chan;
629 bits = NI_STC_DMA_CHAN_SEL(mite_chan->channel);
630 ni_set_bitfield(dev, NI_E_DMA_AI_AO_SEL_REG,
631 NI_E_DMA_AO_SEL_MASK, NI_E_DMA_AO_SEL(bits));
633 spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
637 static int ni_request_gpct_mite_channel(struct comedi_device *dev,
638 unsigned int gpct_index,
639 enum comedi_io_direction direction)
641 struct ni_private *devpriv = dev->private;
642 struct ni_gpct *counter = &devpriv->counter_dev->counters[gpct_index];
643 struct mite_channel *mite_chan;
647 spin_lock_irqsave(&devpriv->mite_channel_lock, flags);
648 mite_chan = mite_request_channel(devpriv->mite,
649 devpriv->gpct_mite_ring[gpct_index]);
651 spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
652 dev_err(dev->class_dev,
653 "failed to reserve mite dma channel for counter\n");
656 mite_chan->dir = direction;
657 ni_tio_set_mite_channel(counter, mite_chan);
659 bits = NI_STC_DMA_CHAN_SEL(mite_chan->channel);
660 ni_set_bitfield(dev, NI_E_DMA_G0_G1_SEL_REG,
661 NI_E_DMA_G0_G1_SEL_MASK(gpct_index),
662 NI_E_DMA_G0_G1_SEL(gpct_index, bits));
664 spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
668 static int ni_request_cdo_mite_channel(struct comedi_device *dev)
670 struct ni_private *devpriv = dev->private;
671 struct mite_channel *mite_chan;
675 spin_lock_irqsave(&devpriv->mite_channel_lock, flags);
676 mite_chan = mite_request_channel(devpriv->mite, devpriv->cdo_mite_ring);
678 spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
679 dev_err(dev->class_dev,
680 "failed to reserve mite dma channel for correlated digital output\n");
683 mite_chan->dir = COMEDI_OUTPUT;
684 devpriv->cdo_mite_chan = mite_chan;
687 * XXX just guessing NI_STC_DMA_CHAN_SEL()
688 * returns the right bits, under the assumption the cdio dma
689 * selection works just like ai/ao/gpct.
690 * Definitely works for dma channels 0 and 1.
692 bits = NI_STC_DMA_CHAN_SEL(mite_chan->channel);
693 ni_set_bitfield(dev, NI_M_CDIO_DMA_SEL_REG,
694 NI_M_CDIO_DMA_SEL_CDO_MASK,
695 NI_M_CDIO_DMA_SEL_CDO(bits));
697 spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
702 static void ni_release_ai_mite_channel(struct comedi_device *dev)
705 struct ni_private *devpriv = dev->private;
708 spin_lock_irqsave(&devpriv->mite_channel_lock, flags);
709 if (devpriv->ai_mite_chan) {
710 ni_set_bitfield(dev, NI_E_DMA_AI_AO_SEL_REG,
711 NI_E_DMA_AI_SEL_MASK, 0);
712 mite_release_channel(devpriv->ai_mite_chan);
713 devpriv->ai_mite_chan = NULL;
715 spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
719 static void ni_release_ao_mite_channel(struct comedi_device *dev)
722 struct ni_private *devpriv = dev->private;
725 spin_lock_irqsave(&devpriv->mite_channel_lock, flags);
726 if (devpriv->ao_mite_chan) {
727 ni_set_bitfield(dev, NI_E_DMA_AI_AO_SEL_REG,
728 NI_E_DMA_AO_SEL_MASK, 0);
729 mite_release_channel(devpriv->ao_mite_chan);
730 devpriv->ao_mite_chan = NULL;
732 spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
737 static void ni_release_gpct_mite_channel(struct comedi_device *dev,
738 unsigned int gpct_index)
740 struct ni_private *devpriv = dev->private;
743 spin_lock_irqsave(&devpriv->mite_channel_lock, flags);
744 if (devpriv->counter_dev->counters[gpct_index].mite_chan) {
745 struct mite_channel *mite_chan =
746 devpriv->counter_dev->counters[gpct_index].mite_chan;
748 ni_set_bitfield(dev, NI_E_DMA_G0_G1_SEL_REG,
749 NI_E_DMA_G0_G1_SEL_MASK(gpct_index), 0);
750 ni_tio_set_mite_channel(&devpriv->counter_dev->counters[gpct_index],
752 mite_release_channel(mite_chan);
754 spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
757 static void ni_release_cdo_mite_channel(struct comedi_device *dev)
759 struct ni_private *devpriv = dev->private;
762 spin_lock_irqsave(&devpriv->mite_channel_lock, flags);
763 if (devpriv->cdo_mite_chan) {
764 ni_set_bitfield(dev, NI_M_CDIO_DMA_SEL_REG,
765 NI_M_CDIO_DMA_SEL_CDO_MASK, 0);
766 mite_release_channel(devpriv->cdo_mite_chan);
767 devpriv->cdo_mite_chan = NULL;
769 spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
772 static void ni_e_series_enable_second_irq(struct comedi_device *dev,
773 unsigned int gpct_index, short enable)
775 struct ni_private *devpriv = dev->private;
776 unsigned int val = 0;
779 if (devpriv->is_m_series || gpct_index > 1)
783 * e-series boards use the second irq signals to generate
784 * dma requests for their counters
786 if (gpct_index == 0) {
787 reg = NISTC_INTA2_ENA_REG;
789 val = NISTC_INTA_ENA_G0_GATE;
791 reg = NISTC_INTB2_ENA_REG;
793 val = NISTC_INTB_ENA_G1_GATE;
795 ni_stc_writew(dev, val, reg);
799 static void ni_clear_ai_fifo(struct comedi_device *dev)
801 struct ni_private *devpriv = dev->private;
802 static const int timeout = 10000;
805 if (devpriv->is_6143) {
806 /* Flush the 6143 data FIFO */
807 ni_writel(dev, 0x10, NI6143_AI_FIFO_CTRL_REG);
808 ni_writel(dev, 0x00, NI6143_AI_FIFO_CTRL_REG);
809 /* Wait for complete */
810 for (i = 0; i < timeout; i++) {
811 if (!(ni_readl(dev, NI6143_AI_FIFO_STATUS_REG) & 0x10))
816 dev_err(dev->class_dev, "FIFO flush timeout\n");
818 ni_stc_writew(dev, 1, NISTC_ADC_FIFO_CLR_REG);
819 if (devpriv->is_625x) {
820 ni_writeb(dev, 0, NI_M_STATIC_AI_CTRL_REG(0));
821 ni_writeb(dev, 1, NI_M_STATIC_AI_CTRL_REG(0));
824 * The NI example code does 3 convert pulses for 625x
825 * boards, But that appears to be wrong in practice.
827 ni_stc_writew(dev, NISTC_AI_CMD1_CONVERT_PULSE,
829 ni_stc_writew(dev, NISTC_AI_CMD1_CONVERT_PULSE,
831 ni_stc_writew(dev, NISTC_AI_CMD1_CONVERT_PULSE,
838 static inline void ni_ao_win_outw(struct comedi_device *dev,
839 unsigned int data, int addr)
841 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 ni_writew(dev, data, NI611X_AO_WINDOW_DATA_REG);
847 spin_unlock_irqrestore(&devpriv->window_lock, flags);
850 static inline void ni_ao_win_outl(struct comedi_device *dev,
851 unsigned int data, int addr)
853 struct ni_private *devpriv = dev->private;
856 spin_lock_irqsave(&devpriv->window_lock, flags);
857 ni_writew(dev, addr, NI611X_AO_WINDOW_ADDR_REG);
858 ni_writel(dev, data, NI611X_AO_WINDOW_DATA_REG);
859 spin_unlock_irqrestore(&devpriv->window_lock, flags);
862 static inline unsigned short ni_ao_win_inw(struct comedi_device *dev, int addr)
864 struct ni_private *devpriv = dev->private;
868 spin_lock_irqsave(&devpriv->window_lock, flags);
869 ni_writew(dev, addr, NI611X_AO_WINDOW_ADDR_REG);
870 data = ni_readw(dev, NI611X_AO_WINDOW_DATA_REG);
871 spin_unlock_irqrestore(&devpriv->window_lock, flags);
876 * ni_set_bits( ) allows different parts of the ni_mio_common driver to
877 * share registers (such as Interrupt_A_Register) without interfering with
880 * NOTE: the switch/case statements are optimized out for a constant argument
881 * so this is actually quite fast--- If you must wrap another function around
882 * this make it inline to avoid a large speed penalty.
884 * value should only be 1 or 0.
886 static inline void ni_set_bits(struct comedi_device *dev, int reg,
887 unsigned int bits, unsigned int value)
889 unsigned int bit_values;
895 ni_set_bitfield(dev, reg, bits, bit_values);
899 static void ni_sync_ai_dma(struct comedi_device *dev)
901 struct ni_private *devpriv = dev->private;
902 struct comedi_subdevice *s = dev->read_subdev;
905 spin_lock_irqsave(&devpriv->mite_channel_lock, flags);
906 if (devpriv->ai_mite_chan)
907 mite_sync_dma(devpriv->ai_mite_chan, s);
908 spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
911 static int ni_ai_drain_dma(struct comedi_device *dev)
913 struct ni_private *devpriv = dev->private;
915 static const int timeout = 10000;
919 spin_lock_irqsave(&devpriv->mite_channel_lock, flags);
920 if (devpriv->ai_mite_chan) {
921 for (i = 0; i < timeout; i++) {
922 if ((ni_stc_readw(dev, NISTC_AI_STATUS1_REG) &
923 NISTC_AI_STATUS1_FIFO_E) &&
924 mite_bytes_in_transit(devpriv->ai_mite_chan) == 0)
929 dev_err(dev->class_dev, "timed out\n");
930 dev_err(dev->class_dev,
931 "mite_bytes_in_transit=%i, AI_Status1_Register=0x%x\n",
932 mite_bytes_in_transit(devpriv->ai_mite_chan),
933 ni_stc_readw(dev, NISTC_AI_STATUS1_REG));
937 spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
944 static int ni_ao_wait_for_dma_load(struct comedi_device *dev)
946 static const int timeout = 10000;
949 for (i = 0; i < timeout; i++) {
950 unsigned short b_status;
952 b_status = ni_stc_readw(dev, NISTC_AO_STATUS1_REG);
953 if (b_status & NISTC_AO_STATUS1_FIFO_HF)
956 * If we poll too often, the pci bus activity seems
957 * to slow the dma transfer down.
959 usleep_range(10, 100);
962 dev_err(dev->class_dev, "timed out waiting for dma load\n");
971 static void ni_ao_fifo_load(struct comedi_device *dev,
972 struct comedi_subdevice *s, int n)
974 struct ni_private *devpriv = dev->private;
977 unsigned int packed_data;
979 for (i = 0; i < n; i++) {
980 comedi_buf_read_samples(s, &d, 1);
982 if (devpriv->is_6xxx) {
983 packed_data = d & 0xffff;
984 /* 6711 only has 16 bit wide ao fifo */
985 if (!devpriv->is_6711) {
986 comedi_buf_read_samples(s, &d, 1);
988 packed_data |= (d << 16) & 0xffff0000;
990 ni_writel(dev, packed_data, NI611X_AO_FIFO_DATA_REG);
992 ni_writew(dev, d, NI_E_AO_FIFO_DATA_REG);
998 * There's a small problem if the FIFO gets really low and we
999 * don't have the data to fill it. Basically, if after we fill
1000 * the FIFO with all the data available, the FIFO is _still_
1001 * less than half full, we never clear the interrupt. If the
1002 * IRQ is in edge mode, we never get another interrupt, because
1003 * this one wasn't cleared. If in level mode, we get flooded
1004 * with interrupts that we can't fulfill, because nothing ever
1005 * gets put into the buffer.
1007 * This kind of situation is recoverable, but it is easier to
1008 * just pretend we had a FIFO underrun, since there is a good
1009 * chance it will happen anyway. This is _not_ the case for
1010 * RT code, as RT code might purposely be running close to the
1011 * metal. Needs to be fixed eventually.
1013 static int ni_ao_fifo_half_empty(struct comedi_device *dev,
1014 struct comedi_subdevice *s)
1016 const struct ni_board_struct *board = dev->board_ptr;
1017 unsigned int nbytes;
1018 unsigned int nsamples;
1020 nbytes = comedi_buf_read_n_available(s);
1022 s->async->events |= COMEDI_CB_OVERFLOW;
1026 nsamples = comedi_bytes_to_samples(s, nbytes);
1027 if (nsamples > board->ao_fifo_depth / 2)
1028 nsamples = board->ao_fifo_depth / 2;
1030 ni_ao_fifo_load(dev, s, nsamples);
1035 static int ni_ao_prep_fifo(struct comedi_device *dev,
1036 struct comedi_subdevice *s)
1038 const struct ni_board_struct *board = dev->board_ptr;
1039 struct ni_private *devpriv = dev->private;
1040 unsigned int nbytes;
1041 unsigned int nsamples;
1044 ni_stc_writew(dev, 1, NISTC_DAC_FIFO_CLR_REG);
1045 if (devpriv->is_6xxx)
1046 ni_ao_win_outl(dev, 0x6, NI611X_AO_FIFO_OFFSET_LOAD_REG);
1048 /* load some data */
1049 nbytes = comedi_buf_read_n_available(s);
1053 nsamples = comedi_bytes_to_samples(s, nbytes);
1054 if (nsamples > board->ao_fifo_depth)
1055 nsamples = board->ao_fifo_depth;
1057 ni_ao_fifo_load(dev, s, nsamples);
1062 static void ni_ai_fifo_read(struct comedi_device *dev,
1063 struct comedi_subdevice *s, int n)
1065 struct ni_private *devpriv = dev->private;
1066 struct comedi_async *async = s->async;
1068 unsigned short data;
1071 if (devpriv->is_611x) {
1072 for (i = 0; i < n / 2; i++) {
1073 dl = ni_readl(dev, NI611X_AI_FIFO_DATA_REG);
1074 /* This may get the hi/lo data in the wrong order */
1075 data = (dl >> 16) & 0xffff;
1076 comedi_buf_write_samples(s, &data, 1);
1078 comedi_buf_write_samples(s, &data, 1);
1080 /* Check if there's a single sample stuck in the FIFO */
1082 dl = ni_readl(dev, NI611X_AI_FIFO_DATA_REG);
1084 comedi_buf_write_samples(s, &data, 1);
1086 } else if (devpriv->is_6143) {
1088 * This just reads the FIFO assuming the data is present,
1089 * no checks on the FIFO status are performed.
1091 for (i = 0; i < n / 2; i++) {
1092 dl = ni_readl(dev, NI6143_AI_FIFO_DATA_REG);
1094 data = (dl >> 16) & 0xffff;
1095 comedi_buf_write_samples(s, &data, 1);
1097 comedi_buf_write_samples(s, &data, 1);
1100 /* Assume there is a single sample stuck in the FIFO */
1101 /* Get stranded sample into FIFO */
1102 ni_writel(dev, 0x01, NI6143_AI_FIFO_CTRL_REG);
1103 dl = ni_readl(dev, NI6143_AI_FIFO_DATA_REG);
1104 data = (dl >> 16) & 0xffff;
1105 comedi_buf_write_samples(s, &data, 1);
1108 if (n > ARRAY_SIZE(devpriv->ai_fifo_buffer)) {
1109 dev_err(dev->class_dev,
1110 "bug! ai_fifo_buffer too small\n");
1111 async->events |= COMEDI_CB_ERROR;
1114 for (i = 0; i < n; i++) {
1115 devpriv->ai_fifo_buffer[i] =
1116 ni_readw(dev, NI_E_AI_FIFO_DATA_REG);
1118 comedi_buf_write_samples(s, devpriv->ai_fifo_buffer, n);
1122 static void ni_handle_fifo_half_full(struct comedi_device *dev)
1124 const struct ni_board_struct *board = dev->board_ptr;
1125 struct comedi_subdevice *s = dev->read_subdev;
1128 n = board->ai_fifo_depth / 2;
1130 ni_ai_fifo_read(dev, s, n);
1134 /* Empties the AI fifo */
1135 static void ni_handle_fifo_dregs(struct comedi_device *dev)
1137 struct ni_private *devpriv = dev->private;
1138 struct comedi_subdevice *s = dev->read_subdev;
1140 unsigned short data;
1143 if (devpriv->is_611x) {
1144 while ((ni_stc_readw(dev, NISTC_AI_STATUS1_REG) &
1145 NISTC_AI_STATUS1_FIFO_E) == 0) {
1146 dl = ni_readl(dev, NI611X_AI_FIFO_DATA_REG);
1148 /* This may get the hi/lo data in the wrong order */
1150 comedi_buf_write_samples(s, &data, 1);
1152 comedi_buf_write_samples(s, &data, 1);
1154 } else if (devpriv->is_6143) {
1156 while (ni_readl(dev, NI6143_AI_FIFO_STATUS_REG) & 0x04) {
1157 dl = ni_readl(dev, NI6143_AI_FIFO_DATA_REG);
1159 /* This may get the hi/lo data in the wrong order */
1161 comedi_buf_write_samples(s, &data, 1);
1163 comedi_buf_write_samples(s, &data, 1);
1166 /* Check if stranded sample is present */
1167 if (ni_readl(dev, NI6143_AI_FIFO_STATUS_REG) & 0x01) {
1168 /* Get stranded sample into FIFO */
1169 ni_writel(dev, 0x01, NI6143_AI_FIFO_CTRL_REG);
1170 dl = ni_readl(dev, NI6143_AI_FIFO_DATA_REG);
1171 data = (dl >> 16) & 0xffff;
1172 comedi_buf_write_samples(s, &data, 1);
1176 unsigned short fe; /* fifo empty */
1178 fe = ni_stc_readw(dev, NISTC_AI_STATUS1_REG) &
1179 NISTC_AI_STATUS1_FIFO_E;
1182 i < ARRAY_SIZE(devpriv->ai_fifo_buffer); i++) {
1183 fe = ni_stc_readw(dev, NISTC_AI_STATUS1_REG) &
1184 NISTC_AI_STATUS1_FIFO_E;
1187 devpriv->ai_fifo_buffer[i] =
1188 ni_readw(dev, NI_E_AI_FIFO_DATA_REG);
1190 comedi_buf_write_samples(s, devpriv->ai_fifo_buffer, i);
1195 static void get_last_sample_611x(struct comedi_device *dev)
1197 struct ni_private *devpriv = dev->private;
1198 struct comedi_subdevice *s = dev->read_subdev;
1199 unsigned short data;
1202 if (!devpriv->is_611x)
1205 /* Check if there's a single sample stuck in the FIFO */
1206 if (ni_readb(dev, NI_E_STATUS_REG) & 0x80) {
1207 dl = ni_readl(dev, NI611X_AI_FIFO_DATA_REG);
1209 comedi_buf_write_samples(s, &data, 1);
1213 static void get_last_sample_6143(struct comedi_device *dev)
1215 struct ni_private *devpriv = dev->private;
1216 struct comedi_subdevice *s = dev->read_subdev;
1217 unsigned short data;
1220 if (!devpriv->is_6143)
1223 /* Check if there's a single sample stuck in the FIFO */
1224 if (ni_readl(dev, NI6143_AI_FIFO_STATUS_REG) & 0x01) {
1225 /* Get stranded sample into FIFO */
1226 ni_writel(dev, 0x01, NI6143_AI_FIFO_CTRL_REG);
1227 dl = ni_readl(dev, NI6143_AI_FIFO_DATA_REG);
1229 /* This may get the hi/lo data in the wrong order */
1230 data = (dl >> 16) & 0xffff;
1231 comedi_buf_write_samples(s, &data, 1);
1235 static void shutdown_ai_command(struct comedi_device *dev)
1237 struct comedi_subdevice *s = dev->read_subdev;
1240 ni_ai_drain_dma(dev);
1242 ni_handle_fifo_dregs(dev);
1243 get_last_sample_611x(dev);
1244 get_last_sample_6143(dev);
1246 s->async->events |= COMEDI_CB_EOA;
1249 static void ni_handle_eos(struct comedi_device *dev, struct comedi_subdevice *s)
1251 struct ni_private *devpriv = dev->private;
1253 if (devpriv->aimode == AIMODE_SCAN) {
1255 static const int timeout = 10;
1258 for (i = 0; i < timeout; i++) {
1259 ni_sync_ai_dma(dev);
1260 if ((s->async->events & COMEDI_CB_EOS))
1265 ni_handle_fifo_dregs(dev);
1266 s->async->events |= COMEDI_CB_EOS;
1269 /* handle special case of single scan */
1270 if (devpriv->ai_cmd2 & NISTC_AI_CMD2_END_ON_EOS)
1271 shutdown_ai_command(dev);
1274 static void handle_gpct_interrupt(struct comedi_device *dev,
1275 unsigned short counter_index)
1278 struct ni_private *devpriv = dev->private;
1279 struct comedi_subdevice *s;
1281 s = &dev->subdevices[NI_GPCT_SUBDEV(counter_index)];
1283 ni_tio_handle_interrupt(&devpriv->counter_dev->counters[counter_index],
1285 comedi_handle_events(dev, s);
1289 static void ack_a_interrupt(struct comedi_device *dev, unsigned short a_status)
1291 unsigned short ack = 0;
1293 if (a_status & NISTC_AI_STATUS1_SC_TC)
1294 ack |= NISTC_INTA_ACK_AI_SC_TC;
1295 if (a_status & NISTC_AI_STATUS1_START1)
1296 ack |= NISTC_INTA_ACK_AI_START1;
1297 if (a_status & NISTC_AI_STATUS1_START)
1298 ack |= NISTC_INTA_ACK_AI_START;
1299 if (a_status & NISTC_AI_STATUS1_STOP)
1300 ack |= NISTC_INTA_ACK_AI_STOP;
1301 if (a_status & NISTC_AI_STATUS1_OVER)
1302 ack |= NISTC_INTA_ACK_AI_ERR;
1304 ni_stc_writew(dev, ack, NISTC_INTA_ACK_REG);
1307 static void handle_a_interrupt(struct comedi_device *dev,
1308 struct comedi_subdevice *s,
1309 unsigned short status)
1311 struct comedi_cmd *cmd = &s->async->cmd;
1313 /* test for all uncommon interrupt events at the same time */
1314 if (status & (NISTC_AI_STATUS1_ERR |
1315 NISTC_AI_STATUS1_SC_TC | NISTC_AI_STATUS1_START1)) {
1316 if (status == 0xffff) {
1317 dev_err(dev->class_dev, "Card removed?\n");
1319 * We probably aren't even running a command now,
1320 * so it's a good idea to be careful.
1322 if (comedi_is_subdevice_running(s))
1323 s->async->events |= COMEDI_CB_ERROR;
1326 if (status & NISTC_AI_STATUS1_ERR) {
1327 dev_err(dev->class_dev, "ai error a_status=%04x\n",
1330 shutdown_ai_command(dev);
1332 s->async->events |= COMEDI_CB_ERROR;
1333 if (status & NISTC_AI_STATUS1_OVER)
1334 s->async->events |= COMEDI_CB_OVERFLOW;
1337 if (status & NISTC_AI_STATUS1_SC_TC) {
1338 if (cmd->stop_src == TRIG_COUNT)
1339 shutdown_ai_command(dev);
1343 if (status & NISTC_AI_STATUS1_FIFO_HF) {
1345 static const int timeout = 10;
1347 * PCMCIA cards (at least 6036) seem to stop producing
1348 * interrupts if we fail to get the fifo less than half
1349 * full, so loop to be sure.
1351 for (i = 0; i < timeout; ++i) {
1352 ni_handle_fifo_half_full(dev);
1353 if ((ni_stc_readw(dev, NISTC_AI_STATUS1_REG) &
1354 NISTC_AI_STATUS1_FIFO_HF) == 0)
1358 #endif /* !PCIDMA */
1360 if (status & NISTC_AI_STATUS1_STOP)
1361 ni_handle_eos(dev, s);
1364 static void ack_b_interrupt(struct comedi_device *dev, unsigned short b_status)
1366 unsigned short ack = 0;
1368 if (b_status & NISTC_AO_STATUS1_BC_TC)
1369 ack |= NISTC_INTB_ACK_AO_BC_TC;
1370 if (b_status & NISTC_AO_STATUS1_OVERRUN)
1371 ack |= NISTC_INTB_ACK_AO_ERR;
1372 if (b_status & NISTC_AO_STATUS1_START)
1373 ack |= NISTC_INTB_ACK_AO_START;
1374 if (b_status & NISTC_AO_STATUS1_START1)
1375 ack |= NISTC_INTB_ACK_AO_START1;
1376 if (b_status & NISTC_AO_STATUS1_UC_TC)
1377 ack |= NISTC_INTB_ACK_AO_UC_TC;
1378 if (b_status & NISTC_AO_STATUS1_UI2_TC)
1379 ack |= NISTC_INTB_ACK_AO_UI2_TC;
1380 if (b_status & NISTC_AO_STATUS1_UPDATE)
1381 ack |= NISTC_INTB_ACK_AO_UPDATE;
1383 ni_stc_writew(dev, ack, NISTC_INTB_ACK_REG);
1386 static void handle_b_interrupt(struct comedi_device *dev,
1387 struct comedi_subdevice *s,
1388 unsigned short b_status)
1390 if (b_status == 0xffff)
1392 if (b_status & NISTC_AO_STATUS1_OVERRUN) {
1393 dev_err(dev->class_dev,
1394 "AO FIFO underrun status=0x%04x status2=0x%04x\n",
1395 b_status, ni_stc_readw(dev, NISTC_AO_STATUS2_REG));
1396 s->async->events |= COMEDI_CB_OVERFLOW;
1399 if (s->async->cmd.stop_src != TRIG_NONE &&
1400 b_status & NISTC_AO_STATUS1_BC_TC)
1401 s->async->events |= COMEDI_CB_EOA;
1404 if (b_status & NISTC_AO_STATUS1_FIFO_REQ) {
1407 ret = ni_ao_fifo_half_empty(dev, s);
1409 dev_err(dev->class_dev, "AO buffer underrun\n");
1410 ni_set_bits(dev, NISTC_INTB_ENA_REG,
1411 NISTC_INTB_ENA_AO_FIFO |
1412 NISTC_INTB_ENA_AO_ERR, 0);
1413 s->async->events |= COMEDI_CB_OVERFLOW;
1419 static void ni_ai_munge(struct comedi_device *dev, struct comedi_subdevice *s,
1420 void *data, unsigned int num_bytes,
1421 unsigned int chan_index)
1423 struct ni_private *devpriv = dev->private;
1424 struct comedi_async *async = s->async;
1425 struct comedi_cmd *cmd = &async->cmd;
1426 unsigned int nsamples = comedi_bytes_to_samples(s, num_bytes);
1427 unsigned short *array = data;
1428 unsigned int *larray = data;
1431 __le16 *barray = data;
1432 __le32 *blarray = data;
1435 for (i = 0; i < nsamples; i++) {
1437 if (s->subdev_flags & SDF_LSAMPL)
1438 larray[i] = le32_to_cpu(blarray[i]);
1440 array[i] = le16_to_cpu(barray[i]);
1442 if (s->subdev_flags & SDF_LSAMPL)
1443 larray[i] += devpriv->ai_offset[chan_index];
1445 array[i] += devpriv->ai_offset[chan_index];
1447 chan_index %= cmd->chanlist_len;
1453 static int ni_ai_setup_MITE_dma(struct comedi_device *dev)
1455 struct ni_private *devpriv = dev->private;
1456 struct comedi_subdevice *s = dev->read_subdev;
1458 unsigned long flags;
1460 retval = ni_request_ai_mite_channel(dev);
1464 /* write alloc the entire buffer */
1465 comedi_buf_write_alloc(s, s->async->prealloc_bufsz);
1467 spin_lock_irqsave(&devpriv->mite_channel_lock, flags);
1468 if (!devpriv->ai_mite_chan) {
1469 spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
1473 if (devpriv->is_611x || devpriv->is_6143)
1474 mite_prep_dma(devpriv->ai_mite_chan, 32, 16);
1475 else if (devpriv->is_628x)
1476 mite_prep_dma(devpriv->ai_mite_chan, 32, 32);
1478 mite_prep_dma(devpriv->ai_mite_chan, 16, 16);
1481 mite_dma_arm(devpriv->ai_mite_chan);
1482 spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
1487 static int ni_ao_setup_MITE_dma(struct comedi_device *dev)
1489 struct ni_private *devpriv = dev->private;
1490 struct comedi_subdevice *s = dev->write_subdev;
1492 unsigned long flags;
1494 retval = ni_request_ao_mite_channel(dev);
1498 /* read alloc the entire buffer */
1499 comedi_buf_read_alloc(s, s->async->prealloc_bufsz);
1501 spin_lock_irqsave(&devpriv->mite_channel_lock, flags);
1502 if (devpriv->ao_mite_chan) {
1503 if (devpriv->is_611x || devpriv->is_6713) {
1504 mite_prep_dma(devpriv->ao_mite_chan, 32, 32);
1507 * Doing 32 instead of 16 bit wide transfers from
1508 * memory makes the mite do 32 bit pci transfers,
1509 * doubling pci bandwidth.
1511 mite_prep_dma(devpriv->ao_mite_chan, 16, 32);
1513 mite_dma_arm(devpriv->ao_mite_chan);
1517 spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
1525 * used for both cancel ioctl and board initialization
1527 * this is pretty harsh for a cancel, but it works...
1529 static int ni_ai_reset(struct comedi_device *dev, struct comedi_subdevice *s)
1531 struct ni_private *devpriv = dev->private;
1532 unsigned int ai_personal;
1533 unsigned int ai_out_ctrl;
1535 ni_release_ai_mite_channel(dev);
1536 /* ai configuration */
1537 ni_stc_writew(dev, NISTC_RESET_AI_CFG_START | NISTC_RESET_AI,
1540 ni_set_bits(dev, NISTC_INTA_ENA_REG, NISTC_INTA_ENA_AI_MASK, 0);
1542 ni_clear_ai_fifo(dev);
1544 if (!devpriv->is_6143)
1545 ni_writeb(dev, NI_E_MISC_CMD_EXT_ATRIG, NI_E_MISC_CMD_REG);
1547 ni_stc_writew(dev, NISTC_AI_CMD1_DISARM, NISTC_AI_CMD1_REG);
1548 ni_stc_writew(dev, NISTC_AI_MODE1_START_STOP |
1550 /*| NISTC_AI_MODE1_TRIGGER_ONCE */,
1551 NISTC_AI_MODE1_REG);
1552 ni_stc_writew(dev, 0, NISTC_AI_MODE2_REG);
1553 /* generate FIFO interrupts on non-empty */
1554 ni_stc_writew(dev, NISTC_AI_MODE3_FIFO_MODE_NE,
1555 NISTC_AI_MODE3_REG);
1557 ai_personal = NISTC_AI_PERSONAL_SHIFTIN_PW |
1558 NISTC_AI_PERSONAL_SOC_POLARITY |
1559 NISTC_AI_PERSONAL_LOCALMUX_CLK_PW;
1560 ai_out_ctrl = NISTC_AI_OUT_CTRL_SCAN_IN_PROG_SEL(3) |
1561 NISTC_AI_OUT_CTRL_EXTMUX_CLK_SEL(0) |
1562 NISTC_AI_OUT_CTRL_LOCALMUX_CLK_SEL(2) |
1563 NISTC_AI_OUT_CTRL_SC_TC_SEL(3);
1564 if (devpriv->is_611x) {
1565 ai_out_ctrl |= NISTC_AI_OUT_CTRL_CONVERT_HIGH;
1566 } else if (devpriv->is_6143) {
1567 ai_out_ctrl |= NISTC_AI_OUT_CTRL_CONVERT_LOW;
1569 ai_personal |= NISTC_AI_PERSONAL_CONVERT_PW;
1570 if (devpriv->is_622x)
1571 ai_out_ctrl |= NISTC_AI_OUT_CTRL_CONVERT_HIGH;
1573 ai_out_ctrl |= NISTC_AI_OUT_CTRL_CONVERT_LOW;
1575 ni_stc_writew(dev, ai_personal, NISTC_AI_PERSONAL_REG);
1576 ni_stc_writew(dev, ai_out_ctrl, NISTC_AI_OUT_CTRL_REG);
1578 /* the following registers should not be changed, because there
1579 * are no backup registers in devpriv. If you want to change
1580 * any of these, add a backup register and other appropriate code:
1581 * NISTC_AI_MODE1_REG
1582 * NISTC_AI_MODE3_REG
1583 * NISTC_AI_PERSONAL_REG
1584 * NISTC_AI_OUT_CTRL_REG
1587 /* clear interrupts */
1588 ni_stc_writew(dev, NISTC_INTA_ACK_AI_ALL, NISTC_INTA_ACK_REG);
1590 ni_stc_writew(dev, NISTC_RESET_AI_CFG_END, NISTC_RESET_REG);
1595 static int ni_ai_poll(struct comedi_device *dev, struct comedi_subdevice *s)
1597 unsigned long flags;
1600 /* lock to avoid race with interrupt handler */
1601 spin_lock_irqsave(&dev->spinlock, flags);
1603 ni_handle_fifo_dregs(dev);
1605 ni_sync_ai_dma(dev);
1607 count = comedi_buf_n_bytes_ready(s);
1608 spin_unlock_irqrestore(&dev->spinlock, flags);
1613 static void ni_prime_channelgain_list(struct comedi_device *dev)
1617 ni_stc_writew(dev, NISTC_AI_CMD1_CONVERT_PULSE, NISTC_AI_CMD1_REG);
1618 for (i = 0; i < NI_TIMEOUT; ++i) {
1619 if (!(ni_stc_readw(dev, NISTC_AI_STATUS1_REG) &
1620 NISTC_AI_STATUS1_FIFO_E)) {
1621 ni_stc_writew(dev, 1, NISTC_ADC_FIFO_CLR_REG);
1626 dev_err(dev->class_dev, "timeout loading channel/gain list\n");
1629 static void ni_m_series_load_channelgain_list(struct comedi_device *dev,
1630 unsigned int n_chan,
1633 const struct ni_board_struct *board = dev->board_ptr;
1634 struct ni_private *devpriv = dev->private;
1635 unsigned int chan, range, aref;
1637 unsigned int dither;
1638 unsigned int range_code;
1640 ni_stc_writew(dev, 1, NISTC_CFG_MEM_CLR_REG);
1642 if ((list[0] & CR_ALT_SOURCE)) {
1643 unsigned int bypass_bits;
1645 chan = CR_CHAN(list[0]);
1646 range = CR_RANGE(list[0]);
1647 range_code = ni_gainlkup[board->gainlkup][range];
1648 dither = (list[0] & CR_ALT_FILTER) != 0;
1649 bypass_bits = NI_M_CFG_BYPASS_FIFO |
1650 NI_M_CFG_BYPASS_AI_CHAN(chan) |
1651 NI_M_CFG_BYPASS_AI_GAIN(range_code) |
1652 devpriv->ai_calib_source;
1654 bypass_bits |= NI_M_CFG_BYPASS_AI_DITHER;
1655 /* don't use 2's complement encoding */
1656 bypass_bits |= NI_M_CFG_BYPASS_AI_POLARITY;
1657 ni_writel(dev, bypass_bits, NI_M_CFG_BYPASS_FIFO_REG);
1659 ni_writel(dev, 0, NI_M_CFG_BYPASS_FIFO_REG);
1661 for (i = 0; i < n_chan; i++) {
1662 unsigned int config_bits = 0;
1664 chan = CR_CHAN(list[i]);
1665 aref = CR_AREF(list[i]);
1666 range = CR_RANGE(list[i]);
1667 dither = (list[i] & CR_ALT_FILTER) != 0;
1669 range_code = ni_gainlkup[board->gainlkup][range];
1670 devpriv->ai_offset[i] = 0;
1673 config_bits |= NI_M_AI_CFG_CHAN_TYPE_DIFF;
1676 config_bits |= NI_M_AI_CFG_CHAN_TYPE_COMMON;
1679 config_bits |= NI_M_AI_CFG_CHAN_TYPE_GROUND;
1684 config_bits |= NI_M_AI_CFG_CHAN_SEL(chan);
1685 config_bits |= NI_M_AI_CFG_BANK_SEL(chan);
1686 config_bits |= NI_M_AI_CFG_GAIN(range_code);
1687 if (i == n_chan - 1)
1688 config_bits |= NI_M_AI_CFG_LAST_CHAN;
1690 config_bits |= NI_M_AI_CFG_DITHER;
1691 /* don't use 2's complement encoding */
1692 config_bits |= NI_M_AI_CFG_POLARITY;
1693 ni_writew(dev, config_bits, NI_M_AI_CFG_FIFO_DATA_REG);
1695 ni_prime_channelgain_list(dev);
1699 * Notes on the 6110 and 6111:
1700 * These boards a slightly different than the rest of the series, since
1701 * they have multiple A/D converters.
1702 * From the driver side, the configuration memory is a
1704 * Configuration Memory Low:
1706 * bit 8: unipolar/bipolar (should be 0 for bipolar)
1707 * bits 0-3: gain. This is 4 bits instead of 3 for the other boards
1708 * 1001 gain=0.1 (+/- 50)
1717 * Configuration Memory High:
1718 * bits 12-14: Channel Type
1719 * 001 for differential
1720 * 000 for calibration
1721 * bit 11: coupling (this is not currently handled)
1725 * valid channels are 0-3
1727 static void ni_load_channelgain_list(struct comedi_device *dev,
1728 struct comedi_subdevice *s,
1729 unsigned int n_chan, unsigned int *list)
1731 const struct ni_board_struct *board = dev->board_ptr;
1732 struct ni_private *devpriv = dev->private;
1733 unsigned int offset = (s->maxdata + 1) >> 1;
1734 unsigned int chan, range, aref;
1736 unsigned int hi, lo;
1737 unsigned int dither;
1739 if (devpriv->is_m_series) {
1740 ni_m_series_load_channelgain_list(dev, n_chan, list);
1743 if (n_chan == 1 && !devpriv->is_611x && !devpriv->is_6143) {
1744 if (devpriv->changain_state &&
1745 devpriv->changain_spec == list[0]) {
1749 devpriv->changain_state = 1;
1750 devpriv->changain_spec = list[0];
1752 devpriv->changain_state = 0;
1755 ni_stc_writew(dev, 1, NISTC_CFG_MEM_CLR_REG);
1757 /* Set up Calibration mode if required */
1758 if (devpriv->is_6143) {
1759 if ((list[0] & CR_ALT_SOURCE) &&
1760 !devpriv->ai_calib_source_enabled) {
1761 /* Strobe Relay enable bit */
1762 ni_writew(dev, devpriv->ai_calib_source |
1763 NI6143_CALIB_CHAN_RELAY_ON,
1764 NI6143_CALIB_CHAN_REG);
1765 ni_writew(dev, devpriv->ai_calib_source,
1766 NI6143_CALIB_CHAN_REG);
1767 devpriv->ai_calib_source_enabled = 1;
1768 /* Allow relays to change */
1769 msleep_interruptible(100);
1770 } else if (!(list[0] & CR_ALT_SOURCE) &&
1771 devpriv->ai_calib_source_enabled) {
1772 /* Strobe Relay disable bit */
1773 ni_writew(dev, devpriv->ai_calib_source |
1774 NI6143_CALIB_CHAN_RELAY_OFF,
1775 NI6143_CALIB_CHAN_REG);
1776 ni_writew(dev, devpriv->ai_calib_source,
1777 NI6143_CALIB_CHAN_REG);
1778 devpriv->ai_calib_source_enabled = 0;
1779 /* Allow relays to change */
1780 msleep_interruptible(100);
1784 for (i = 0; i < n_chan; i++) {
1785 if (!devpriv->is_6143 && (list[i] & CR_ALT_SOURCE))
1786 chan = devpriv->ai_calib_source;
1788 chan = CR_CHAN(list[i]);
1789 aref = CR_AREF(list[i]);
1790 range = CR_RANGE(list[i]);
1791 dither = (list[i] & CR_ALT_FILTER) != 0;
1793 /* fix the external/internal range differences */
1794 range = ni_gainlkup[board->gainlkup][range];
1795 if (devpriv->is_611x)
1796 devpriv->ai_offset[i] = offset;
1798 devpriv->ai_offset[i] = (range & 0x100) ? 0 : offset;
1801 if ((list[i] & CR_ALT_SOURCE)) {
1802 if (devpriv->is_611x)
1803 ni_writew(dev, CR_CHAN(list[i]) & 0x0003,
1804 NI611X_CALIB_CHAN_SEL_REG);
1806 if (devpriv->is_611x)
1808 else if (devpriv->is_6143)
1812 hi |= NI_E_AI_CFG_HI_TYPE_DIFF;
1815 hi |= NI_E_AI_CFG_HI_TYPE_COMMON;
1818 hi |= NI_E_AI_CFG_HI_TYPE_GROUND;
1824 hi |= NI_E_AI_CFG_HI_CHAN(chan);
1826 ni_writew(dev, hi, NI_E_AI_CFG_HI_REG);
1828 if (!devpriv->is_6143) {
1829 lo = NI_E_AI_CFG_LO_GAIN(range);
1831 if (i == n_chan - 1)
1832 lo |= NI_E_AI_CFG_LO_LAST_CHAN;
1834 lo |= NI_E_AI_CFG_LO_DITHER;
1836 ni_writew(dev, lo, NI_E_AI_CFG_LO_REG);
1840 /* prime the channel/gain list */
1841 if (!devpriv->is_611x && !devpriv->is_6143)
1842 ni_prime_channelgain_list(dev);
1845 static int ni_ai_insn_read(struct comedi_device *dev,
1846 struct comedi_subdevice *s,
1847 struct comedi_insn *insn,
1850 struct ni_private *devpriv = dev->private;
1851 unsigned int mask = s->maxdata;
1853 unsigned int signbits;
1856 ni_load_channelgain_list(dev, s, 1, &insn->chanspec);
1858 ni_clear_ai_fifo(dev);
1860 signbits = devpriv->ai_offset[0];
1861 if (devpriv->is_611x) {
1862 for (n = 0; n < num_adc_stages_611x; n++) {
1863 ni_stc_writew(dev, NISTC_AI_CMD1_CONVERT_PULSE,
1867 for (n = 0; n < insn->n; n++) {
1868 ni_stc_writew(dev, NISTC_AI_CMD1_CONVERT_PULSE,
1870 /* The 611x has screwy 32-bit FIFOs. */
1872 for (i = 0; i < NI_TIMEOUT; i++) {
1873 if (ni_readb(dev, NI_E_STATUS_REG) & 0x80) {
1875 NI611X_AI_FIFO_DATA_REG);
1880 if (!(ni_stc_readw(dev, NISTC_AI_STATUS1_REG) &
1881 NISTC_AI_STATUS1_FIFO_E)) {
1883 NI611X_AI_FIFO_DATA_REG);
1888 if (i == NI_TIMEOUT) {
1889 dev_err(dev->class_dev, "timeout\n");
1893 data[n] = d & 0xffff;
1895 } else if (devpriv->is_6143) {
1896 for (n = 0; n < insn->n; n++) {
1897 ni_stc_writew(dev, NISTC_AI_CMD1_CONVERT_PULSE,
1901 * The 6143 has 32-bit FIFOs. You need to strobe a
1902 * bit to move a single 16bit stranded sample into
1906 for (i = 0; i < NI_TIMEOUT; i++) {
1907 if (ni_readl(dev, NI6143_AI_FIFO_STATUS_REG) &
1909 /* Get stranded sample into FIFO */
1910 ni_writel(dev, 0x01,
1911 NI6143_AI_FIFO_CTRL_REG);
1913 NI6143_AI_FIFO_DATA_REG);
1917 if (i == NI_TIMEOUT) {
1918 dev_err(dev->class_dev, "timeout\n");
1921 data[n] = (((d >> 16) & 0xFFFF) + signbits) & 0xFFFF;
1924 for (n = 0; n < insn->n; n++) {
1925 ni_stc_writew(dev, NISTC_AI_CMD1_CONVERT_PULSE,
1927 for (i = 0; i < NI_TIMEOUT; i++) {
1928 if (!(ni_stc_readw(dev, NISTC_AI_STATUS1_REG) &
1929 NISTC_AI_STATUS1_FIFO_E))
1932 if (i == NI_TIMEOUT) {
1933 dev_err(dev->class_dev, "timeout\n");
1936 if (devpriv->is_m_series) {
1937 d = ni_readl(dev, NI_M_AI_FIFO_DATA_REG);
1941 d = ni_readw(dev, NI_E_AI_FIFO_DATA_REG);
1943 data[n] = d & 0xffff;
1950 static int ni_ns_to_timer(const struct comedi_device *dev,
1951 unsigned int nanosec, unsigned int flags)
1953 struct ni_private *devpriv = dev->private;
1956 switch (flags & CMDF_ROUND_MASK) {
1957 case CMDF_ROUND_NEAREST:
1959 divider = DIV_ROUND_CLOSEST(nanosec, devpriv->clock_ns);
1961 case CMDF_ROUND_DOWN:
1962 divider = (nanosec) / devpriv->clock_ns;
1965 divider = DIV_ROUND_UP(nanosec, devpriv->clock_ns);
1971 static unsigned int ni_timer_to_ns(const struct comedi_device *dev, int timer)
1973 struct ni_private *devpriv = dev->private;
1975 return devpriv->clock_ns * (timer + 1);
1978 static void ni_cmd_set_mite_transfer(struct mite_ring *ring,
1979 struct comedi_subdevice *sdev,
1980 const struct comedi_cmd *cmd,
1981 unsigned int max_count)
1984 unsigned int nbytes = max_count;
1986 if (cmd->stop_arg > 0 && cmd->stop_arg < max_count)
1987 nbytes = cmd->stop_arg;
1988 nbytes *= comedi_bytes_per_scan(sdev);
1990 if (nbytes > sdev->async->prealloc_bufsz) {
1991 if (cmd->stop_arg > 0)
1992 dev_err(sdev->device->class_dev,
1993 "%s: tried exact data transfer limits greater than buffer size\n",
1997 * we can only transfer up to the size of the buffer. In this
1998 * case, the user is expected to continue to write into the
1999 * comedi buffer (already implemented as a ring buffer).
2001 nbytes = sdev->async->prealloc_bufsz;
2004 mite_init_ring_descriptors(ring, sdev, nbytes);
2006 dev_err(sdev->device->class_dev,
2007 "%s: exact data transfer limits not implemented yet without DMA\n",
2012 static unsigned int ni_min_ai_scan_period_ns(struct comedi_device *dev,
2013 unsigned int num_channels)
2015 const struct ni_board_struct *board = dev->board_ptr;
2016 struct ni_private *devpriv = dev->private;
2018 /* simultaneously-sampled inputs */
2019 if (devpriv->is_611x || devpriv->is_6143)
2020 return board->ai_speed;
2022 /* multiplexed inputs */
2023 return board->ai_speed * num_channels;
2026 static int ni_ai_cmdtest(struct comedi_device *dev, struct comedi_subdevice *s,
2027 struct comedi_cmd *cmd)
2029 const struct ni_board_struct *board = dev->board_ptr;
2030 struct ni_private *devpriv = dev->private;
2032 unsigned int sources;
2034 /* Step 1 : check if triggers are trivially valid */
2036 err |= comedi_check_trigger_src(&cmd->start_src,
2037 TRIG_NOW | TRIG_INT | TRIG_EXT);
2038 err |= comedi_check_trigger_src(&cmd->scan_begin_src,
2039 TRIG_TIMER | TRIG_EXT);
2041 sources = TRIG_TIMER | TRIG_EXT;
2042 if (devpriv->is_611x || devpriv->is_6143)
2043 sources |= TRIG_NOW;
2044 err |= comedi_check_trigger_src(&cmd->convert_src, sources);
2046 err |= comedi_check_trigger_src(&cmd->scan_end_src, TRIG_COUNT);
2047 err |= comedi_check_trigger_src(&cmd->stop_src, TRIG_COUNT | TRIG_NONE);
2052 /* Step 2a : make sure trigger sources are unique */
2054 err |= comedi_check_trigger_is_unique(cmd->start_src);
2055 err |= comedi_check_trigger_is_unique(cmd->scan_begin_src);
2056 err |= comedi_check_trigger_is_unique(cmd->convert_src);
2057 err |= comedi_check_trigger_is_unique(cmd->stop_src);
2059 /* Step 2b : and mutually compatible */
2064 /* Step 3: check if arguments are trivially valid */
2066 switch (cmd->start_src) {
2069 err |= comedi_check_trigger_arg_is(&cmd->start_arg, 0);
2072 err |= ni_check_trigger_arg_roffs(CR_CHAN(cmd->start_arg),
2074 &devpriv->routing_tables, 1);
2078 if (cmd->scan_begin_src == TRIG_TIMER) {
2079 err |= comedi_check_trigger_arg_min(&cmd->scan_begin_arg,
2080 ni_min_ai_scan_period_ns(dev, cmd->chanlist_len));
2081 err |= comedi_check_trigger_arg_max(&cmd->scan_begin_arg,
2084 } else if (cmd->scan_begin_src == TRIG_EXT) {
2085 /* external trigger */
2086 err |= ni_check_trigger_arg_roffs(CR_CHAN(cmd->scan_begin_arg),
2088 &devpriv->routing_tables, 1);
2089 } else { /* TRIG_OTHER */
2090 err |= comedi_check_trigger_arg_is(&cmd->scan_begin_arg, 0);
2093 if (cmd->convert_src == TRIG_TIMER) {
2094 if (devpriv->is_611x || devpriv->is_6143) {
2095 err |= comedi_check_trigger_arg_is(&cmd->convert_arg,
2098 err |= comedi_check_trigger_arg_min(&cmd->convert_arg,
2100 err |= comedi_check_trigger_arg_max(&cmd->convert_arg,
2104 } else if (cmd->convert_src == TRIG_EXT) {
2105 /* external trigger */
2106 err |= ni_check_trigger_arg_roffs(CR_CHAN(cmd->convert_arg),
2108 &devpriv->routing_tables, 1);
2109 } else if (cmd->convert_src == TRIG_NOW) {
2110 err |= comedi_check_trigger_arg_is(&cmd->convert_arg, 0);
2113 err |= comedi_check_trigger_arg_is(&cmd->scan_end_arg,
2116 if (cmd->stop_src == TRIG_COUNT) {
2117 unsigned int max_count = 0x01000000;
2119 if (devpriv->is_611x)
2120 max_count -= num_adc_stages_611x;
2121 err |= comedi_check_trigger_arg_max(&cmd->stop_arg, max_count);
2122 err |= comedi_check_trigger_arg_min(&cmd->stop_arg, 1);
2125 err |= comedi_check_trigger_arg_is(&cmd->stop_arg, 0);
2131 /* step 4: fix up any arguments */
2133 if (cmd->scan_begin_src == TRIG_TIMER) {
2134 unsigned int tmp = cmd->scan_begin_arg;
2136 cmd->scan_begin_arg =
2137 ni_timer_to_ns(dev, ni_ns_to_timer(dev,
2138 cmd->scan_begin_arg,
2140 if (tmp != cmd->scan_begin_arg)
2143 if (cmd->convert_src == TRIG_TIMER) {
2144 if (!devpriv->is_611x && !devpriv->is_6143) {
2145 unsigned int tmp = cmd->convert_arg;
2148 ni_timer_to_ns(dev, ni_ns_to_timer(dev,
2151 if (tmp != cmd->convert_arg)
2153 if (cmd->scan_begin_src == TRIG_TIMER &&
2154 cmd->scan_begin_arg <
2155 cmd->convert_arg * cmd->scan_end_arg) {
2156 cmd->scan_begin_arg =
2157 cmd->convert_arg * cmd->scan_end_arg;
2169 static int ni_ai_inttrig(struct comedi_device *dev,
2170 struct comedi_subdevice *s,
2171 unsigned int trig_num)
2173 struct ni_private *devpriv = dev->private;
2174 struct comedi_cmd *cmd = &s->async->cmd;
2176 if (trig_num != cmd->start_arg)
2179 ni_stc_writew(dev, NISTC_AI_CMD2_START1_PULSE | devpriv->ai_cmd2,
2181 s->async->inttrig = NULL;
2186 static int ni_ai_cmd(struct comedi_device *dev, struct comedi_subdevice *s)
2188 struct ni_private *devpriv = dev->private;
2189 const struct comedi_cmd *cmd = &s->async->cmd;
2191 int mode1 = 0; /* mode1 is needed for both stop and convert */
2193 int start_stop_select = 0;
2194 unsigned int stop_count;
2195 int interrupt_a_enable = 0;
2196 unsigned int ai_trig;
2198 if (dev->irq == 0) {
2199 dev_err(dev->class_dev, "cannot run command without an irq\n");
2202 ni_clear_ai_fifo(dev);
2204 ni_load_channelgain_list(dev, s, cmd->chanlist_len, cmd->chanlist);
2206 /* start configuration */
2207 ni_stc_writew(dev, NISTC_RESET_AI_CFG_START, NISTC_RESET_REG);
2210 * Disable analog triggering for now, since it interferes
2211 * with the use of pfi0.
2213 devpriv->an_trig_etc_reg &= ~NISTC_ATRIG_ETC_ENA;
2214 ni_stc_writew(dev, devpriv->an_trig_etc_reg, NISTC_ATRIG_ETC_REG);
2216 ai_trig = NISTC_AI_TRIG_START2_SEL(0) | NISTC_AI_TRIG_START1_SYNC;
2217 switch (cmd->start_src) {
2220 ai_trig |= NISTC_AI_TRIG_START1_EDGE |
2221 NISTC_AI_TRIG_START1_SEL(0);
2224 ai_trig |= NISTC_AI_TRIG_START1_SEL(
2225 ni_get_reg_value_roffs(
2226 CR_CHAN(cmd->start_arg),
2228 &devpriv->routing_tables, 1));
2230 if (cmd->start_arg & CR_INVERT)
2231 ai_trig |= NISTC_AI_TRIG_START1_POLARITY;
2232 if (cmd->start_arg & CR_EDGE)
2233 ai_trig |= NISTC_AI_TRIG_START1_EDGE;
2236 ni_stc_writew(dev, ai_trig, NISTC_AI_TRIG_SEL_REG);
2238 mode2 &= ~NISTC_AI_MODE2_PRE_TRIGGER;
2239 mode2 &= ~NISTC_AI_MODE2_SC_INIT_LOAD_SRC;
2240 mode2 &= ~NISTC_AI_MODE2_SC_RELOAD_MODE;
2241 ni_stc_writew(dev, mode2, NISTC_AI_MODE2_REG);
2243 if (cmd->chanlist_len == 1 || devpriv->is_611x || devpriv->is_6143) {
2245 start_stop_select |= NISTC_AI_STOP_POLARITY |
2246 NISTC_AI_STOP_SEL(31) |
2249 /* ai configuration memory */
2250 start_stop_select |= NISTC_AI_STOP_SEL(19);
2252 ni_stc_writew(dev, start_stop_select, NISTC_AI_START_STOP_REG);
2254 devpriv->ai_cmd2 = 0;
2255 switch (cmd->stop_src) {
2257 stop_count = cmd->stop_arg - 1;
2259 if (devpriv->is_611x) {
2260 /* have to take 3 stage adc pipeline into account */
2261 stop_count += num_adc_stages_611x;
2263 /* stage number of scans */
2264 ni_stc_writel(dev, stop_count, NISTC_AI_SC_LOADA_REG);
2266 mode1 |= NISTC_AI_MODE1_START_STOP |
2267 NISTC_AI_MODE1_RSVD |
2268 NISTC_AI_MODE1_TRIGGER_ONCE;
2269 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);
2273 if (stop_count == 0) {
2274 devpriv->ai_cmd2 |= NISTC_AI_CMD2_END_ON_EOS;
2275 interrupt_a_enable |= NISTC_INTA_ENA_AI_STOP;
2277 * This is required to get the last sample for
2278 * chanlist_len > 1, not sure why.
2280 if (cmd->chanlist_len > 1)
2281 start_stop_select |= NISTC_AI_STOP_POLARITY |
2286 /* stage number of scans */
2287 ni_stc_writel(dev, 0, NISTC_AI_SC_LOADA_REG);
2289 mode1 |= NISTC_AI_MODE1_START_STOP |
2290 NISTC_AI_MODE1_RSVD |
2291 NISTC_AI_MODE1_CONTINUOUS;
2292 ni_stc_writew(dev, mode1, NISTC_AI_MODE1_REG);
2294 /* load SC (Scan Count) */
2295 ni_stc_writew(dev, NISTC_AI_CMD1_SC_LOAD, NISTC_AI_CMD1_REG);
2299 switch (cmd->scan_begin_src) {
2302 * stop bits for non 611x boards
2303 * NISTC_AI_MODE3_SI_TRIG_DELAY=0
2304 * NISTC_AI_MODE2_PRE_TRIGGER=0
2305 * NISTC_AI_START_STOP_REG:
2306 * NISTC_AI_START_POLARITY=0 (?) rising edge
2307 * NISTC_AI_START_EDGE=1 edge triggered
2308 * NISTC_AI_START_SYNC=1 (?)
2309 * NISTC_AI_START_SEL=0 SI_TC
2310 * NISTC_AI_STOP_POLARITY=0 rising edge
2311 * NISTC_AI_STOP_EDGE=0 level
2312 * NISTC_AI_STOP_SYNC=1
2313 * NISTC_AI_STOP_SEL=19 external pin (configuration mem)
2315 start_stop_select |= NISTC_AI_START_EDGE | NISTC_AI_START_SYNC;
2316 ni_stc_writew(dev, start_stop_select, NISTC_AI_START_STOP_REG);
2318 mode2 &= ~NISTC_AI_MODE2_SI_INIT_LOAD_SRC; /* A */
2319 mode2 |= NISTC_AI_MODE2_SI_RELOAD_MODE(0);
2320 /* mode2 |= NISTC_AI_MODE2_SC_RELOAD_MODE; */
2321 ni_stc_writew(dev, mode2, NISTC_AI_MODE2_REG);
2324 timer = ni_ns_to_timer(dev, cmd->scan_begin_arg,
2325 CMDF_ROUND_NEAREST);
2326 ni_stc_writel(dev, timer, NISTC_AI_SI_LOADA_REG);
2327 ni_stc_writew(dev, NISTC_AI_CMD1_SI_LOAD, NISTC_AI_CMD1_REG);
2330 if (cmd->scan_begin_arg & CR_EDGE)
2331 start_stop_select |= NISTC_AI_START_EDGE;
2332 if (cmd->scan_begin_arg & CR_INVERT) /* falling edge */
2333 start_stop_select |= NISTC_AI_START_POLARITY;
2334 if (cmd->scan_begin_src != cmd->convert_src ||
2335 (cmd->scan_begin_arg & ~CR_EDGE) !=
2336 (cmd->convert_arg & ~CR_EDGE))
2337 start_stop_select |= NISTC_AI_START_SYNC;
2339 start_stop_select |= NISTC_AI_START_SEL(
2340 ni_get_reg_value_roffs(
2341 CR_CHAN(cmd->scan_begin_arg),
2343 &devpriv->routing_tables, 1));
2344 ni_stc_writew(dev, start_stop_select, NISTC_AI_START_STOP_REG);
2348 switch (cmd->convert_src) {
2351 if (cmd->convert_arg == 0 || cmd->convert_src == TRIG_NOW)
2354 timer = ni_ns_to_timer(dev, cmd->convert_arg,
2355 CMDF_ROUND_NEAREST);
2356 /* 0,0 does not work */
2357 ni_stc_writew(dev, 1, NISTC_AI_SI2_LOADA_REG);
2358 ni_stc_writew(dev, timer, NISTC_AI_SI2_LOADB_REG);
2360 mode2 &= ~NISTC_AI_MODE2_SI2_INIT_LOAD_SRC; /* A */
2361 mode2 |= NISTC_AI_MODE2_SI2_RELOAD_MODE; /* alternate */
2362 ni_stc_writew(dev, mode2, NISTC_AI_MODE2_REG);
2364 ni_stc_writew(dev, NISTC_AI_CMD1_SI2_LOAD, NISTC_AI_CMD1_REG);
2366 mode2 |= NISTC_AI_MODE2_SI2_INIT_LOAD_SRC; /* B */
2367 mode2 |= NISTC_AI_MODE2_SI2_RELOAD_MODE; /* alternate */
2368 ni_stc_writew(dev, mode2, NISTC_AI_MODE2_REG);
2371 mode1 |= NISTC_AI_MODE1_CONVERT_SRC(
2372 ni_get_reg_value_roffs(
2373 CR_CHAN(cmd->convert_arg),
2375 &devpriv->routing_tables, 1));
2376 if ((cmd->convert_arg & CR_INVERT) == 0)
2377 mode1 |= NISTC_AI_MODE1_CONVERT_POLARITY;
2378 ni_stc_writew(dev, mode1, NISTC_AI_MODE1_REG);
2380 mode2 |= NISTC_AI_MODE2_SC_GATE_ENA |
2381 NISTC_AI_MODE2_START_STOP_GATE_ENA;
2382 ni_stc_writew(dev, mode2, NISTC_AI_MODE2_REG);
2388 /* interrupt on FIFO, errors, SC_TC */
2389 interrupt_a_enable |= NISTC_INTA_ENA_AI_ERR |
2390 NISTC_INTA_ENA_AI_SC_TC;
2393 interrupt_a_enable |= NISTC_INTA_ENA_AI_FIFO;
2396 if ((cmd->flags & CMDF_WAKE_EOS) ||
2397 (devpriv->ai_cmd2 & NISTC_AI_CMD2_END_ON_EOS)) {
2398 /* wake on end-of-scan */
2399 devpriv->aimode = AIMODE_SCAN;
2401 devpriv->aimode = AIMODE_HALF_FULL;
2404 switch (devpriv->aimode) {
2405 case AIMODE_HALF_FULL:
2406 /* FIFO interrupts and DMA requests on half-full */
2408 ni_stc_writew(dev, NISTC_AI_MODE3_FIFO_MODE_HF_E,
2409 NISTC_AI_MODE3_REG);
2411 ni_stc_writew(dev, NISTC_AI_MODE3_FIFO_MODE_HF,
2412 NISTC_AI_MODE3_REG);
2416 /* generate FIFO interrupts on non-empty */
2417 ni_stc_writew(dev, NISTC_AI_MODE3_FIFO_MODE_NE,
2418 NISTC_AI_MODE3_REG);
2422 ni_stc_writew(dev, NISTC_AI_MODE3_FIFO_MODE_NE,
2423 NISTC_AI_MODE3_REG);
2425 ni_stc_writew(dev, NISTC_AI_MODE3_FIFO_MODE_HF,
2426 NISTC_AI_MODE3_REG);
2428 interrupt_a_enable |= NISTC_INTA_ENA_AI_STOP;
2434 /* clear interrupts */
2435 ni_stc_writew(dev, NISTC_INTA_ACK_AI_ALL, NISTC_INTA_ACK_REG);
2437 ni_set_bits(dev, NISTC_INTA_ENA_REG, interrupt_a_enable, 1);
2439 /* interrupt on nothing */
2440 ni_set_bits(dev, NISTC_INTA_ENA_REG, ~0, 0);
2442 /* XXX start polling if necessary */
2445 /* end configuration */
2446 ni_stc_writew(dev, NISTC_RESET_AI_CFG_END, NISTC_RESET_REG);
2448 switch (cmd->scan_begin_src) {
2450 ni_stc_writew(dev, NISTC_AI_CMD1_SI2_ARM |
2451 NISTC_AI_CMD1_SI_ARM |
2452 NISTC_AI_CMD1_DIV_ARM |
2453 NISTC_AI_CMD1_SC_ARM,
2457 ni_stc_writew(dev, NISTC_AI_CMD1_SI2_ARM |
2458 NISTC_AI_CMD1_SI_ARM | /* XXX ? */
2459 NISTC_AI_CMD1_DIV_ARM |
2460 NISTC_AI_CMD1_SC_ARM,
2467 int retval = ni_ai_setup_MITE_dma(dev);
2474 if (cmd->start_src == TRIG_NOW) {
2475 ni_stc_writew(dev, NISTC_AI_CMD2_START1_PULSE |
2478 s->async->inttrig = NULL;
2479 } else if (cmd->start_src == TRIG_EXT) {
2480 s->async->inttrig = NULL;
2481 } else { /* TRIG_INT */
2482 s->async->inttrig = ni_ai_inttrig;
2488 static int ni_ai_insn_config(struct comedi_device *dev,
2489 struct comedi_subdevice *s,
2490 struct comedi_insn *insn, unsigned int *data)
2492 const struct ni_board_struct *board = dev->board_ptr;
2493 struct ni_private *devpriv = dev->private;
2499 case INSN_CONFIG_ALT_SOURCE:
2500 if (devpriv->is_m_series) {
2501 if (data[1] & ~NI_M_CFG_BYPASS_AI_CAL_MASK)
2503 devpriv->ai_calib_source = data[1];
2504 } else if (devpriv->is_6143) {
2505 unsigned int calib_source;
2507 calib_source = data[1] & 0xf;
2509 devpriv->ai_calib_source = calib_source;
2510 ni_writew(dev, calib_source, NI6143_CALIB_CHAN_REG);
2512 unsigned int calib_source;
2513 unsigned int calib_source_adjust;
2515 calib_source = data[1] & 0xf;
2516 calib_source_adjust = (data[1] >> 4) & 0xff;
2518 if (calib_source >= 8)
2520 devpriv->ai_calib_source = calib_source;
2521 if (devpriv->is_611x) {
2522 ni_writeb(dev, calib_source_adjust,
2523 NI611X_CAL_GAIN_SEL_REG);
2527 case INSN_CONFIG_GET_CMD_TIMING_CONSTRAINTS:
2528 /* we don't care about actual channels */
2529 /* data[3] : chanlist_len */
2530 data[1] = ni_min_ai_scan_period_ns(dev, data[3]);
2531 if (devpriv->is_611x || devpriv->is_6143)
2532 data[2] = 0; /* simultaneous output */
2534 data[2] = board->ai_speed;
2543 static void ni_ao_munge(struct comedi_device *dev, struct comedi_subdevice *s,
2544 void *data, unsigned int num_bytes,
2545 unsigned int chan_index)
2547 struct comedi_cmd *cmd = &s->async->cmd;
2548 unsigned int nsamples = comedi_bytes_to_samples(s, num_bytes);
2549 unsigned short *array = data;
2552 __le16 buf, *barray = data;
2555 for (i = 0; i < nsamples; i++) {
2556 unsigned int range = CR_RANGE(cmd->chanlist[chan_index]);
2557 unsigned short val = array[i];
2560 * Munge data from unsigned to two's complement for
2563 if (comedi_range_is_bipolar(s, range))
2564 val = comedi_offset_munge(s, val);
2566 buf = cpu_to_le16(val);
2572 chan_index %= cmd->chanlist_len;
2576 static int ni_m_series_ao_config_chanlist(struct comedi_device *dev,
2577 struct comedi_subdevice *s,
2578 unsigned int chanspec[],
2579 unsigned int n_chans, int timed)
2581 struct ni_private *devpriv = dev->private;
2589 for (i = 0; i < s->n_chan; ++i) {
2590 devpriv->ao_conf[i] &= ~NI_M_AO_CFG_BANK_UPDATE_TIMED;
2591 ni_writeb(dev, devpriv->ao_conf[i],
2592 NI_M_AO_CFG_BANK_REG(i));
2593 ni_writeb(dev, 0xf, NI_M_AO_WAVEFORM_ORDER_REG(i));
2596 for (i = 0; i < n_chans; i++) {
2597 const struct comedi_krange *krange;
2599 chan = CR_CHAN(chanspec[i]);
2600 range = CR_RANGE(chanspec[i]);
2601 krange = s->range_table->range + range;
2604 switch (krange->max - krange->min) {
2606 conf |= NI_M_AO_CFG_BANK_REF_INT_10V;
2607 ni_writeb(dev, 0, NI_M_AO_REF_ATTENUATION_REG(chan));
2610 conf |= NI_M_AO_CFG_BANK_REF_INT_5V;
2611 ni_writeb(dev, 0, NI_M_AO_REF_ATTENUATION_REG(chan));
2614 conf |= NI_M_AO_CFG_BANK_REF_INT_10V;
2615 ni_writeb(dev, NI_M_AO_REF_ATTENUATION_X5,
2616 NI_M_AO_REF_ATTENUATION_REG(chan));
2619 conf |= NI_M_AO_CFG_BANK_REF_INT_5V;
2620 ni_writeb(dev, NI_M_AO_REF_ATTENUATION_X5,
2621 NI_M_AO_REF_ATTENUATION_REG(chan));
2624 dev_err(dev->class_dev,
2625 "bug! unhandled ao reference voltage\n");
2628 switch (krange->max + krange->min) {
2630 conf |= NI_M_AO_CFG_BANK_OFFSET_0V;
2633 conf |= NI_M_AO_CFG_BANK_OFFSET_5V;
2636 dev_err(dev->class_dev,
2637 "bug! unhandled ao offset voltage\n");
2641 conf |= NI_M_AO_CFG_BANK_UPDATE_TIMED;
2642 ni_writeb(dev, conf, NI_M_AO_CFG_BANK_REG(chan));
2643 devpriv->ao_conf[chan] = conf;
2644 ni_writeb(dev, i, NI_M_AO_WAVEFORM_ORDER_REG(chan));
2649 static int ni_old_ao_config_chanlist(struct comedi_device *dev,
2650 struct comedi_subdevice *s,
2651 unsigned int chanspec[],
2652 unsigned int n_chans)
2654 struct ni_private *devpriv = dev->private;
2661 for (i = 0; i < n_chans; i++) {
2662 chan = CR_CHAN(chanspec[i]);
2663 range = CR_RANGE(chanspec[i]);
2664 conf = NI_E_AO_DACSEL(chan);
2666 if (comedi_range_is_bipolar(s, range)) {
2667 conf |= NI_E_AO_CFG_BIP;
2668 invert = (s->maxdata + 1) >> 1;
2672 if (comedi_range_is_external(s, range))
2673 conf |= NI_E_AO_EXT_REF;
2675 /* not all boards can deglitch, but this shouldn't hurt */
2676 if (chanspec[i] & CR_DEGLITCH)
2677 conf |= NI_E_AO_DEGLITCH;
2679 /* analog reference */
2680 /* AREF_OTHER connects AO ground to AI ground, i think */
2681 if (CR_AREF(chanspec[i]) == AREF_OTHER)
2682 conf |= NI_E_AO_GROUND_REF;
2684 ni_writew(dev, conf, NI_E_AO_CFG_REG);
2685 devpriv->ao_conf[chan] = conf;
2690 static int ni_ao_config_chanlist(struct comedi_device *dev,
2691 struct comedi_subdevice *s,
2692 unsigned int chanspec[], unsigned int n_chans,
2695 struct ni_private *devpriv = dev->private;
2697 if (devpriv->is_m_series)
2698 return ni_m_series_ao_config_chanlist(dev, s, chanspec, n_chans,
2701 return ni_old_ao_config_chanlist(dev, s, chanspec, n_chans);
2704 static int ni_ao_insn_write(struct comedi_device *dev,
2705 struct comedi_subdevice *s,
2706 struct comedi_insn *insn,
2709 struct ni_private *devpriv = dev->private;
2710 unsigned int chan = CR_CHAN(insn->chanspec);
2711 unsigned int range = CR_RANGE(insn->chanspec);
2715 if (devpriv->is_6xxx) {
2716 ni_ao_win_outw(dev, 1 << chan, NI671X_AO_IMMEDIATE_REG);
2718 reg = NI671X_DAC_DIRECT_DATA_REG(chan);
2719 } else if (devpriv->is_m_series) {
2720 reg = NI_M_DAC_DIRECT_DATA_REG(chan);
2722 reg = NI_E_DAC_DIRECT_DATA_REG(chan);
2725 ni_ao_config_chanlist(dev, s, &insn->chanspec, 1, 0);
2727 for (i = 0; i < insn->n; i++) {
2728 unsigned int val = data[i];
2730 s->readback[chan] = val;
2732 if (devpriv->is_6xxx) {
2734 * 6xxx boards have bipolar outputs, munge the
2735 * unsigned comedi values to 2's complement
2737 val = comedi_offset_munge(s, val);
2739 ni_ao_win_outw(dev, val, reg);
2740 } else if (devpriv->is_m_series) {
2742 * M-series boards use offset binary values for
2743 * bipolar and uinpolar outputs
2745 ni_writew(dev, val, reg);
2748 * Non-M series boards need two's complement values
2749 * for bipolar ranges.
2751 if (comedi_range_is_bipolar(s, range))
2752 val = comedi_offset_munge(s, val);
2754 ni_writew(dev, val, reg);
2762 * Arms the AO device in preparation for a trigger event.
2763 * This function also allocates and prepares a DMA channel (or FIFO if DMA is
2764 * not used). As a part of this preparation, this function preloads the DAC
2765 * registers with the first values of the output stream. This ensures that the
2766 * first clock cycle after the trigger can be used for output.
2768 * Note that this function _must_ happen after a user has written data to the
2769 * output buffers via either mmap or write(fileno,...).
2771 static int ni_ao_arm(struct comedi_device *dev,
2772 struct comedi_subdevice *s)
2774 struct ni_private *devpriv = dev->private;
2776 int interrupt_b_bits;
2778 static const int timeout = 1000;
2781 * Prevent ao from doing things like trying to allocate the ao dma
2782 * channel multiple times.
2784 if (!devpriv->ao_needs_arming) {
2785 dev_dbg(dev->class_dev, "%s: device does not need arming!\n",
2790 devpriv->ao_needs_arming = 0;
2792 ni_set_bits(dev, NISTC_INTB_ENA_REG,
2793 NISTC_INTB_ENA_AO_FIFO | NISTC_INTB_ENA_AO_ERR, 0);
2794 interrupt_b_bits = NISTC_INTB_ENA_AO_ERR;
2796 ni_stc_writew(dev, 1, NISTC_DAC_FIFO_CLR_REG);
2797 if (devpriv->is_6xxx)
2798 ni_ao_win_outl(dev, 0x6, NI611X_AO_FIFO_OFFSET_LOAD_REG);
2799 ret = ni_ao_setup_MITE_dma(dev);
2802 ret = ni_ao_wait_for_dma_load(dev);
2806 ret = ni_ao_prep_fifo(dev, s);
2810 interrupt_b_bits |= NISTC_INTB_ENA_AO_FIFO;
2813 ni_stc_writew(dev, devpriv->ao_mode3 | NISTC_AO_MODE3_NOT_AN_UPDATE,
2814 NISTC_AO_MODE3_REG);
2815 ni_stc_writew(dev, devpriv->ao_mode3, NISTC_AO_MODE3_REG);
2816 /* wait for DACs to be loaded */
2817 for (i = 0; i < timeout; i++) {
2819 if ((ni_stc_readw(dev, NISTC_STATUS2_REG) &
2820 NISTC_STATUS2_AO_TMRDACWRS_IN_PROGRESS) == 0)
2824 dev_err(dev->class_dev,
2825 "timed out waiting for AO_TMRDACWRs_In_Progress_St to clear\n");
2829 * stc manual says we are need to clear error interrupt after
2830 * AO_TMRDACWRs_In_Progress_St clears
2832 ni_stc_writew(dev, NISTC_INTB_ACK_AO_ERR, NISTC_INTB_ACK_REG);
2834 ni_set_bits(dev, NISTC_INTB_ENA_REG, interrupt_b_bits, 1);
2836 ni_stc_writew(dev, NISTC_AO_CMD1_UI_ARM |
2837 NISTC_AO_CMD1_UC_ARM |
2838 NISTC_AO_CMD1_BC_ARM |
2845 static int ni_ao_insn_config(struct comedi_device *dev,
2846 struct comedi_subdevice *s,
2847 struct comedi_insn *insn, unsigned int *data)
2849 const struct ni_board_struct *board = dev->board_ptr;
2850 struct ni_private *devpriv = dev->private;
2851 unsigned int nbytes;
2854 case INSN_CONFIG_GET_HARDWARE_BUFFER_SIZE:
2857 nbytes = comedi_samples_to_bytes(s,
2858 board->ao_fifo_depth);
2859 data[2] = 1 + nbytes;
2861 data[2] += devpriv->mite->fifo_size;
2870 case INSN_CONFIG_ARM:
2871 return ni_ao_arm(dev, s);
2872 case INSN_CONFIG_GET_CMD_TIMING_CONSTRAINTS:
2873 /* we don't care about actual channels */
2874 /* data[3] : chanlist_len */
2875 data[1] = board->ao_speed * data[3];
2885 static int ni_ao_inttrig(struct comedi_device *dev,
2886 struct comedi_subdevice *s,
2887 unsigned int trig_num)
2889 struct ni_private *devpriv = dev->private;
2890 struct comedi_cmd *cmd = &s->async->cmd;
2894 * Require trig_num == cmd->start_arg when cmd->start_src == TRIG_INT.
2895 * For backwards compatibility, also allow trig_num == 0 when
2896 * cmd->start_src != TRIG_INT (i.e. when cmd->start_src == TRIG_EXT);
2897 * in that case, the internal trigger is being used as a pre-trigger
2898 * before the external trigger.
2900 if (!(trig_num == cmd->start_arg ||
2901 (trig_num == 0 && cmd->start_src != TRIG_INT)))
2905 * Null trig at beginning prevent ao start trigger from executing more
2906 * than once per command.
2908 s->async->inttrig = NULL;
2910 if (devpriv->ao_needs_arming) {
2911 /* only arm this device if it still needs arming */
2912 ret = ni_ao_arm(dev, s);
2917 ni_stc_writew(dev, NISTC_AO_CMD2_START1_PULSE | devpriv->ao_cmd2,
2925 * Organized similar to NI-STC and MHDDK examples.
2926 * ni_ao_cmd is broken out into configuration sub-routines for clarity.
2929 static void ni_ao_cmd_personalize(struct comedi_device *dev,
2930 const struct comedi_cmd *cmd)
2932 const struct ni_board_struct *board = dev->board_ptr;
2935 ni_stc_writew(dev, NISTC_RESET_AO_CFG_START, NISTC_RESET_REG);
2938 /* fast CPU interface--only eseries */
2939 /* ((slow CPU interface) ? 0 : AO_Fast_CPU) | */
2940 NISTC_AO_PERSONAL_BC_SRC_SEL |
2941 0 /* (use_original_pulse ? 0 : NISTC_AO_PERSONAL_UPDATE_TIMEBASE) */ |
2943 * FIXME: start setting following bit when appropriate. Need to
2944 * determine whether board is E4 or E1.
2946 * if board is E4 or E1
2947 * Set bit "NISTC_AO_PERSONAL_UPDATE_PW" to 0
2951 NISTC_AO_PERSONAL_UPDATE_PW |
2952 /* FIXME: when should we set following bit to zero? */
2953 NISTC_AO_PERSONAL_TMRDACWR_PW |
2954 (board->ao_fifo_depth ?
2955 NISTC_AO_PERSONAL_FIFO_ENA : NISTC_AO_PERSONAL_DMA_PIO_CTRL)
2960 * add something like ".has_individual_dacs = 0" to ni_board_struct
2961 * since, as F Hess pointed out, not all in m series have singles. not
2962 * sure if e-series all have duals...
2966 * F Hess: windows driver does not set NISTC_AO_PERSONAL_NUM_DAC bit for
2967 * 6281, verified with bus analyzer.
2969 if (devpriv->is_m_series)
2970 bits |= NISTC_AO_PERSONAL_NUM_DAC;
2972 ni_stc_writew(dev, bits, NISTC_AO_PERSONAL_REG);
2974 ni_stc_writew(dev, NISTC_RESET_AO_CFG_END, NISTC_RESET_REG);
2977 static void ni_ao_cmd_set_trigger(struct comedi_device *dev,
2978 const struct comedi_cmd *cmd)
2980 struct ni_private *devpriv = dev->private;
2981 unsigned int trigsel;
2983 ni_stc_writew(dev, NISTC_RESET_AO_CFG_START, NISTC_RESET_REG);
2986 if (cmd->stop_src == TRIG_NONE) {
2987 devpriv->ao_mode1 |= NISTC_AO_MODE1_CONTINUOUS;
2988 devpriv->ao_mode1 &= ~NISTC_AO_MODE1_TRIGGER_ONCE;
2990 devpriv->ao_mode1 &= ~NISTC_AO_MODE1_CONTINUOUS;
2991 devpriv->ao_mode1 |= NISTC_AO_MODE1_TRIGGER_ONCE;
2993 ni_stc_writew(dev, devpriv->ao_mode1, NISTC_AO_MODE1_REG);
2995 if (cmd->start_src == TRIG_INT) {
2996 trigsel = NISTC_AO_TRIG_START1_EDGE |
2997 NISTC_AO_TRIG_START1_SYNC;
2998 } else { /* TRIG_EXT */
2999 trigsel = NISTC_AO_TRIG_START1_SEL(
3000 ni_get_reg_value_roffs(
3001 CR_CHAN(cmd->start_arg),
3003 &devpriv->routing_tables, 1));
3005 /* 0=active high, 1=active low. see daq-stc 3-24 (p186) */
3006 if (cmd->start_arg & CR_INVERT)
3007 trigsel |= NISTC_AO_TRIG_START1_POLARITY;
3008 /* 0=edge detection disabled, 1=enabled */
3009 if (cmd->start_arg & CR_EDGE)
3010 trigsel |= NISTC_AO_TRIG_START1_EDGE;
3012 ni_stc_writew(dev, trigsel, NISTC_AO_TRIG_SEL_REG);
3014 /* AO_Delayed_START1 = 0, we do not support delayed start...yet */
3017 /* select DA_START1 as PFI6/AO_START1 when configured as an output */
3018 devpriv->ao_mode3 &= ~NISTC_AO_MODE3_TRIG_LEN;
3019 ni_stc_writew(dev, devpriv->ao_mode3, NISTC_AO_MODE3_REG);
3021 ni_stc_writew(dev, NISTC_RESET_AO_CFG_END, NISTC_RESET_REG);
3024 static void ni_ao_cmd_set_counters(struct comedi_device *dev,
3025 const struct comedi_cmd *cmd)
3027 struct ni_private *devpriv = dev->private;
3028 /* Not supporting 'waveform staging' or 'local buffer with pauses' */
3030 ni_stc_writew(dev, NISTC_RESET_AO_CFG_START, NISTC_RESET_REG);
3032 * This relies on ao_mode1/(Trigger_Once | Continuous) being set in
3033 * set_trigger above. It is unclear whether we really need to re-write
3034 * this register with these values. The mhddk examples for e-series
3035 * show writing this in both places, but the examples for m-series show
3036 * a single write in the set_counters function (here).
3038 ni_stc_writew(dev, devpriv->ao_mode1, NISTC_AO_MODE1_REG);
3040 /* sync (upload number of buffer iterations -1) */
3041 /* indicate that we want to use BC_Load_A_Register as the source */
3042 devpriv->ao_mode2 &= ~NISTC_AO_MODE2_BC_INIT_LOAD_SRC;
3043 ni_stc_writew(dev, devpriv->ao_mode2, NISTC_AO_MODE2_REG);
3046 * if the BC_TC interrupt is still issued in spite of UC, BC, UI
3047 * ignoring BC_TC, then we will need to find a way to ignore that
3048 * interrupt in continuous mode.
3050 ni_stc_writel(dev, 0, NISTC_AO_BC_LOADA_REG); /* iter once */
3052 /* sync (issue command to load number of buffer iterations -1) */
3053 ni_stc_writew(dev, NISTC_AO_CMD1_BC_LOAD, NISTC_AO_CMD1_REG);
3055 /* sync (upload number of updates in buffer) */
3056 /* indicate that we want to use UC_Load_A_Register as the source */
3057 devpriv->ao_mode2 &= ~NISTC_AO_MODE2_UC_INIT_LOAD_SRC;
3058 ni_stc_writew(dev, devpriv->ao_mode2, NISTC_AO_MODE2_REG);
3061 * if a user specifies '0', this automatically assumes the entire 24bit
3062 * address space is available for the (multiple iterations of single
3063 * buffer) MISB. Otherwise, stop_arg specifies the MISB length that
3064 * will be used, regardless of whether we are in continuous mode or not.
3065 * In continuous mode, the output will just iterate indefinitely over
3069 unsigned int stop_arg = cmd->stop_arg > 0 ?
3070 (cmd->stop_arg & 0xffffff) : 0xffffff;
3072 if (devpriv->is_m_series) {
3074 * this is how the NI example code does it for m-series
3075 * boards, verified correct with 6259
3077 ni_stc_writel(dev, stop_arg - 1, NISTC_AO_UC_LOADA_REG);
3079 /* sync (issue cmd to load number of updates in MISB) */
3080 ni_stc_writew(dev, NISTC_AO_CMD1_UC_LOAD,
3083 ni_stc_writel(dev, stop_arg, NISTC_AO_UC_LOADA_REG);
3085 /* sync (issue cmd to load number of updates in MISB) */
3086 ni_stc_writew(dev, NISTC_AO_CMD1_UC_LOAD,
3090 * sync (upload number of updates-1 in MISB)
3093 ni_stc_writel(dev, stop_arg - 1, NISTC_AO_UC_LOADA_REG);
3097 ni_stc_writew(dev, NISTC_RESET_AO_CFG_END, NISTC_RESET_REG);
3100 static void ni_ao_cmd_set_update(struct comedi_device *dev,
3101 const struct comedi_cmd *cmd)
3103 struct ni_private *devpriv = dev->private;
3105 ni_stc_writew(dev, NISTC_RESET_AO_CFG_START, NISTC_RESET_REG);
3108 * zero out these bit fields to be set below. Does an ao-reset do this
3111 devpriv->ao_mode1 &= ~(NISTC_AO_MODE1_UI_SRC_MASK |
3112 NISTC_AO_MODE1_UI_SRC_POLARITY |
3113 NISTC_AO_MODE1_UPDATE_SRC_MASK |
3114 NISTC_AO_MODE1_UPDATE_SRC_POLARITY);
3116 if (cmd->scan_begin_src == TRIG_TIMER) {
3117 unsigned int trigvar;
3119 devpriv->ao_cmd2 &= ~NISTC_AO_CMD2_BC_GATE_ENA;
3122 * NOTE: there are several other ways of configuring internal
3123 * updates, but we'll only support one for now: using
3124 * AO_IN_TIMEBASE, w/o waveform staging, w/o a delay between
3125 * START1 and first update, and also w/o local buffer mode w/
3130 * This is already done above:
3131 * devpriv->ao_mode1 &= ~(
3132 * // set UPDATE_Source to UI_TC:
3133 * NISTC_AO_MODE1_UPDATE_SRC_MASK |
3134 * // set UPDATE_Source_Polarity to rising (required?)
3135 * NISTC_AO_MODE1_UPDATE_SRC_POLARITY |
3136 * // set UI_Source to AO_IN_TIMEBASE1:
3137 * NISTC_AO_MODE1_UI_SRC_MASK |
3138 * // set UI_Source_Polarity to rising (required?)
3139 * NISTC_AO_MODE1_UI_SRC_POLARITY
3144 * TODO: use ao_ui_clock_source to allow all possible signals
3145 * to be routed to UI_Source_Select. See tSTC.h for
3146 * eseries/ni67xx and tMSeries.h for mseries.
3149 trigvar = ni_ns_to_timer(dev, cmd->scan_begin_arg,
3150 CMDF_ROUND_NEAREST);
3153 * Wait N TB3 ticks after the start trigger before
3154 * clocking (N must be >=2).
3156 /* following line: 2-1 per STC */
3157 ni_stc_writel(dev, 1, NISTC_AO_UI_LOADA_REG);
3158 ni_stc_writew(dev, NISTC_AO_CMD1_UI_LOAD, NISTC_AO_CMD1_REG);
3159 ni_stc_writel(dev, trigvar, NISTC_AO_UI_LOADA_REG);
3160 } else { /* TRIG_EXT */
3161 /* FIXME: assert scan_begin_arg != 0, ret failure otherwise */
3162 devpriv->ao_cmd2 |= NISTC_AO_CMD2_BC_GATE_ENA;
3163 devpriv->ao_mode1 |= NISTC_AO_MODE1_UPDATE_SRC(
3165 CR_CHAN(cmd->scan_begin_arg),
3167 &devpriv->routing_tables));
3168 if (cmd->scan_begin_arg & CR_INVERT)
3169 devpriv->ao_mode1 |= NISTC_AO_MODE1_UPDATE_SRC_POLARITY;
3172 ni_stc_writew(dev, devpriv->ao_cmd2, NISTC_AO_CMD2_REG);
3173 ni_stc_writew(dev, devpriv->ao_mode1, NISTC_AO_MODE1_REG);
3174 devpriv->ao_mode2 &= ~(NISTC_AO_MODE2_UI_RELOAD_MODE(3) |
3175 NISTC_AO_MODE2_UI_INIT_LOAD_SRC);
3176 ni_stc_writew(dev, devpriv->ao_mode2, NISTC_AO_MODE2_REG);
3178 /* Configure DAQ-STC for Timed update mode */
3179 devpriv->ao_cmd1 |= NISTC_AO_CMD1_DAC1_UPDATE_MODE |
3180 NISTC_AO_CMD1_DAC0_UPDATE_MODE;
3181 /* We are not using UPDATE2-->don't have to set DACx_Source_Select */
3182 ni_stc_writew(dev, devpriv->ao_cmd1, NISTC_AO_CMD1_REG);
3184 ni_stc_writew(dev, NISTC_RESET_AO_CFG_END, NISTC_RESET_REG);
3187 static void ni_ao_cmd_set_channels(struct comedi_device *dev,
3188 struct comedi_subdevice *s)
3190 struct ni_private *devpriv = dev->private;
3191 const struct comedi_cmd *cmd = &s->async->cmd;
3192 unsigned int bits = 0;
3194 ni_stc_writew(dev, NISTC_RESET_AO_CFG_START, NISTC_RESET_REG);
3196 if (devpriv->is_6xxx) {
3200 for (i = 0; i < cmd->chanlist_len; ++i) {
3201 int chan = CR_CHAN(cmd->chanlist[i]);
3204 ni_ao_win_outw(dev, chan, NI611X_AO_WAVEFORM_GEN_REG);
3206 ni_ao_win_outw(dev, bits, NI611X_AO_TIMED_REG);
3209 ni_ao_config_chanlist(dev, s, cmd->chanlist, cmd->chanlist_len, 1);
3211 if (cmd->scan_end_arg > 1) {
3212 devpriv->ao_mode1 |= NISTC_AO_MODE1_MULTI_CHAN;
3213 bits = NISTC_AO_OUT_CTRL_CHANS(cmd->scan_end_arg - 1)
3214 | NISTC_AO_OUT_CTRL_UPDATE_SEL_HIGHZ;
3217 devpriv->ao_mode1 &= ~NISTC_AO_MODE1_MULTI_CHAN;
3218 bits = NISTC_AO_OUT_CTRL_UPDATE_SEL_HIGHZ;
3219 if (devpriv->is_m_series | devpriv->is_6xxx)
3220 bits |= NISTC_AO_OUT_CTRL_CHANS(0);
3222 bits |= NISTC_AO_OUT_CTRL_CHANS(
3223 CR_CHAN(cmd->chanlist[0]));
3226 ni_stc_writew(dev, devpriv->ao_mode1, NISTC_AO_MODE1_REG);
3227 ni_stc_writew(dev, bits, NISTC_AO_OUT_CTRL_REG);
3229 ni_stc_writew(dev, NISTC_RESET_AO_CFG_END, NISTC_RESET_REG);
3232 static void ni_ao_cmd_set_stop_conditions(struct comedi_device *dev,
3233 const struct comedi_cmd *cmd)
3235 struct ni_private *devpriv = dev->private;
3237 ni_stc_writew(dev, NISTC_RESET_AO_CFG_START, NISTC_RESET_REG);
3239 devpriv->ao_mode3 |= NISTC_AO_MODE3_STOP_ON_OVERRUN_ERR;
3240 ni_stc_writew(dev, devpriv->ao_mode3, NISTC_AO_MODE3_REG);
3243 * Since we are not supporting waveform staging, we ignore these errors:
3244 * NISTC_AO_MODE3_STOP_ON_BC_TC_ERR,
3245 * NISTC_AO_MODE3_STOP_ON_BC_TC_TRIG_ERR
3248 ni_stc_writew(dev, NISTC_RESET_AO_CFG_END, NISTC_RESET_REG);
3251 static void ni_ao_cmd_set_fifo_mode(struct comedi_device *dev)
3253 struct ni_private *devpriv = dev->private;
3255 ni_stc_writew(dev, NISTC_RESET_AO_CFG_START, NISTC_RESET_REG);
3257 devpriv->ao_mode2 &= ~NISTC_AO_MODE2_FIFO_MODE_MASK;
3259 devpriv->ao_mode2 |= NISTC_AO_MODE2_FIFO_MODE_HF_F;
3261 devpriv->ao_mode2 |= NISTC_AO_MODE2_FIFO_MODE_HF;
3263 /* NOTE: this is where use_onboard_memory=True would be implemented */
3264 devpriv->ao_mode2 &= ~NISTC_AO_MODE2_FIFO_REXMIT_ENA;
3265 ni_stc_writew(dev, devpriv->ao_mode2, NISTC_AO_MODE2_REG);
3267 /* enable sending of ao fifo requests (dma request) */
3268 ni_stc_writew(dev, NISTC_AO_START_AOFREQ_ENA, NISTC_AO_START_SEL_REG);
3270 ni_stc_writew(dev, NISTC_RESET_AO_CFG_END, NISTC_RESET_REG);
3272 /* we are not supporting boards with virtual fifos */
3275 static void ni_ao_cmd_set_interrupts(struct comedi_device *dev,
3276 struct comedi_subdevice *s)
3278 if (s->async->cmd.stop_src == TRIG_COUNT)
3279 ni_set_bits(dev, NISTC_INTB_ENA_REG,
3280 NISTC_INTB_ENA_AO_BC_TC, 1);
3282 s->async->inttrig = ni_ao_inttrig;
3285 static int ni_ao_cmd(struct comedi_device *dev, struct comedi_subdevice *s)
3287 struct ni_private *devpriv = dev->private;
3288 const struct comedi_cmd *cmd = &s->async->cmd;
3290 if (dev->irq == 0) {
3291 dev_err(dev->class_dev, "cannot run command without an irq");
3295 /* ni_ao_reset should have already been done */
3296 ni_ao_cmd_personalize(dev, cmd);
3297 /* clearing fifo and preload happens elsewhere */
3299 ni_ao_cmd_set_trigger(dev, cmd);
3300 ni_ao_cmd_set_counters(dev, cmd);
3301 ni_ao_cmd_set_update(dev, cmd);
3302 ni_ao_cmd_set_channels(dev, s);
3303 ni_ao_cmd_set_stop_conditions(dev, cmd);
3304 ni_ao_cmd_set_fifo_mode(dev);
3305 ni_cmd_set_mite_transfer(devpriv->ao_mite_ring, s, cmd, 0x00ffffff);
3306 ni_ao_cmd_set_interrupts(dev, s);
3309 * arm(ing) must happen later so that DMA can be setup and DACs
3310 * preloaded with the actual output buffer before starting.
3312 * start(ing) must happen _after_ arming is completed. Starting can be
3313 * done either via ni_ao_inttrig, or via an external trigger.
3315 * **Currently, ni_ao_inttrig will automatically attempt a call to
3316 * ni_ao_arm if the device still needs arming at that point. This
3317 * allows backwards compatibility.
3319 devpriv->ao_needs_arming = 1;
3325 static int ni_ao_cmdtest(struct comedi_device *dev, struct comedi_subdevice *s,
3326 struct comedi_cmd *cmd)
3328 const struct ni_board_struct *board = dev->board_ptr;
3329 struct ni_private *devpriv = dev->private;
3333 /* Step 1 : check if triggers are trivially valid */
3335 err |= comedi_check_trigger_src(&cmd->start_src, TRIG_INT | TRIG_EXT);
3336 err |= comedi_check_trigger_src(&cmd->scan_begin_src,
3337 TRIG_TIMER | TRIG_EXT);
3338 err |= comedi_check_trigger_src(&cmd->convert_src, TRIG_NOW);
3339 err |= comedi_check_trigger_src(&cmd->scan_end_src, TRIG_COUNT);
3340 err |= comedi_check_trigger_src(&cmd->stop_src, TRIG_COUNT | TRIG_NONE);
3345 /* Step 2a : make sure trigger sources are unique */
3347 err |= comedi_check_trigger_is_unique(cmd->start_src);
3348 err |= comedi_check_trigger_is_unique(cmd->scan_begin_src);
3349 err |= comedi_check_trigger_is_unique(cmd->stop_src);
3351 /* Step 2b : and mutually compatible */
3356 /* Step 3: check if arguments are trivially valid */
3358 switch (cmd->start_src) {
3360 err |= comedi_check_trigger_arg_is(&cmd->start_arg, 0);
3363 err |= ni_check_trigger_arg_roffs(CR_CHAN(cmd->start_arg),
3365 &devpriv->routing_tables, 1);
3369 if (cmd->scan_begin_src == TRIG_TIMER) {
3370 err |= comedi_check_trigger_arg_min(&cmd->scan_begin_arg,
3372 err |= comedi_check_trigger_arg_max(&cmd->scan_begin_arg,
3375 } else { /* TRIG_EXT */
3376 err |= ni_check_trigger_arg(CR_CHAN(cmd->scan_begin_arg),
3378 &devpriv->routing_tables);
3381 err |= comedi_check_trigger_arg_is(&cmd->convert_arg, 0);
3382 err |= comedi_check_trigger_arg_is(&cmd->scan_end_arg,
3384 err |= comedi_check_trigger_arg_max(&cmd->stop_arg, 0x00ffffff);
3389 /* step 4: fix up any arguments */
3390 if (cmd->scan_begin_src == TRIG_TIMER) {
3391 tmp = cmd->scan_begin_arg;
3392 cmd->scan_begin_arg =
3393 ni_timer_to_ns(dev, ni_ns_to_timer(dev,
3394 cmd->scan_begin_arg,
3396 if (tmp != cmd->scan_begin_arg)
3405 static int ni_ao_reset(struct comedi_device *dev, struct comedi_subdevice *s)
3407 /* See 3.6.1.2 "Resetting", of DAQ-STC Technical Reference Manual */
3410 * In the following, the "--sync" comments are meant to denote
3411 * asynchronous boundaries for setting the registers as described in the
3412 * DAQ-STC mostly in the order also described in the DAQ-STC.
3415 struct ni_private *devpriv = dev->private;
3417 ni_release_ao_mite_channel(dev);
3419 /* --sync (reset AO) */
3420 if (devpriv->is_m_series)
3421 /* following example in mhddk for m-series */
3422 ni_stc_writew(dev, NISTC_RESET_AO, NISTC_RESET_REG);
3424 /*--sync (start config) */
3425 ni_stc_writew(dev, NISTC_RESET_AO_CFG_START, NISTC_RESET_REG);
3427 /*--sync (Disarm) */
3428 ni_stc_writew(dev, NISTC_AO_CMD1_DISARM, NISTC_AO_CMD1_REG);
3432 * (clear bunch of registers--mseries mhddk examples do not include
3435 devpriv->ao_cmd1 = 0;
3436 devpriv->ao_cmd2 = 0;
3437 devpriv->ao_mode1 = 0;
3438 devpriv->ao_mode2 = 0;
3439 if (devpriv->is_m_series)
3440 devpriv->ao_mode3 = NISTC_AO_MODE3_LAST_GATE_DISABLE;
3442 devpriv->ao_mode3 = 0;
3444 ni_stc_writew(dev, 0, NISTC_AO_PERSONAL_REG);
3445 ni_stc_writew(dev, 0, NISTC_AO_CMD1_REG);
3446 ni_stc_writew(dev, 0, NISTC_AO_CMD2_REG);
3447 ni_stc_writew(dev, 0, NISTC_AO_MODE1_REG);
3448 ni_stc_writew(dev, 0, NISTC_AO_MODE2_REG);
3449 ni_stc_writew(dev, 0, NISTC_AO_OUT_CTRL_REG);
3450 ni_stc_writew(dev, devpriv->ao_mode3, NISTC_AO_MODE3_REG);
3451 ni_stc_writew(dev, 0, NISTC_AO_START_SEL_REG);
3452 ni_stc_writew(dev, 0, NISTC_AO_TRIG_SEL_REG);
3454 /*--sync (disable interrupts) */
3455 ni_set_bits(dev, NISTC_INTB_ENA_REG, ~0, 0);
3458 ni_stc_writew(dev, NISTC_AO_PERSONAL_BC_SRC_SEL, NISTC_AO_PERSONAL_REG);
3459 ni_stc_writew(dev, NISTC_INTB_ACK_AO_ALL, NISTC_INTB_ACK_REG);
3461 /*--not in DAQ-STC. which doc? */
3462 if (devpriv->is_6xxx) {
3463 ni_ao_win_outw(dev, (1u << s->n_chan) - 1u,
3464 NI671X_AO_IMMEDIATE_REG);
3465 ni_ao_win_outw(dev, NI611X_AO_MISC_CLEAR_WG,
3466 NI611X_AO_MISC_REG);
3468 ni_stc_writew(dev, NISTC_RESET_AO_CFG_END, NISTC_RESET_REG);
3476 static int ni_dio_insn_config(struct comedi_device *dev,
3477 struct comedi_subdevice *s,
3478 struct comedi_insn *insn,
3481 struct ni_private *devpriv = dev->private;
3484 ret = comedi_dio_insn_config(dev, s, insn, data, 0);
3488 devpriv->dio_control &= ~NISTC_DIO_CTRL_DIR_MASK;
3489 devpriv->dio_control |= NISTC_DIO_CTRL_DIR(s->io_bits);
3490 ni_stc_writew(dev, devpriv->dio_control, NISTC_DIO_CTRL_REG);
3495 static int ni_dio_insn_bits(struct comedi_device *dev,
3496 struct comedi_subdevice *s,
3497 struct comedi_insn *insn,
3500 struct ni_private *devpriv = dev->private;
3502 /* Make sure we're not using the serial part of the dio */
3503 if ((data[0] & (NISTC_DIO_SDIN | NISTC_DIO_SDOUT)) &&
3504 devpriv->serial_interval_ns)
3507 if (comedi_dio_update_state(s, data)) {
3508 devpriv->dio_output &= ~NISTC_DIO_OUT_PARALLEL_MASK;
3509 devpriv->dio_output |= NISTC_DIO_OUT_PARALLEL(s->state);
3510 ni_stc_writew(dev, devpriv->dio_output, NISTC_DIO_OUT_REG);
3513 data[1] = ni_stc_readw(dev, NISTC_DIO_IN_REG);
3519 static int ni_m_series_dio_insn_config(struct comedi_device *dev,
3520 struct comedi_subdevice *s,
3521 struct comedi_insn *insn,
3526 if (data[0] == INSN_CONFIG_GET_CMD_TIMING_CONSTRAINTS) {
3527 const struct ni_board_struct *board = dev->board_ptr;
3529 /* we don't care about actual channels */
3530 data[1] = board->dio_speed;
3535 ret = comedi_dio_insn_config(dev, s, insn, data, 0);
3539 ni_writel(dev, s->io_bits, NI_M_DIO_DIR_REG);
3544 static int ni_m_series_dio_insn_bits(struct comedi_device *dev,
3545 struct comedi_subdevice *s,
3546 struct comedi_insn *insn,
3549 if (comedi_dio_update_state(s, data))
3550 ni_writel(dev, s->state, NI_M_DIO_REG);
3552 data[1] = ni_readl(dev, NI_M_DIO_REG);
3557 static int ni_cdio_check_chanlist(struct comedi_device *dev,
3558 struct comedi_subdevice *s,
3559 struct comedi_cmd *cmd)
3563 for (i = 0; i < cmd->chanlist_len; ++i) {
3564 unsigned int chan = CR_CHAN(cmd->chanlist[i]);
3573 static int ni_cdio_cmdtest(struct comedi_device *dev,
3574 struct comedi_subdevice *s, struct comedi_cmd *cmd)
3576 struct ni_private *devpriv = dev->private;
3577 unsigned int bytes_per_scan;
3580 /* Step 1 : check if triggers are trivially valid */
3582 err |= comedi_check_trigger_src(&cmd->start_src, TRIG_INT);
3583 err |= comedi_check_trigger_src(&cmd->scan_begin_src, TRIG_EXT);
3584 err |= comedi_check_trigger_src(&cmd->convert_src, TRIG_NOW);
3585 err |= comedi_check_trigger_src(&cmd->scan_end_src, TRIG_COUNT);
3586 err |= comedi_check_trigger_src(&cmd->stop_src, TRIG_NONE);
3591 /* Step 2a : make sure trigger sources are unique */
3592 /* Step 2b : and mutually compatible */
3594 /* Step 3: check if arguments are trivially valid */
3596 err |= comedi_check_trigger_arg_is(&cmd->start_arg, 0);
3599 * Although NI_D[IO]_SampleClock are the same, perhaps we should still,
3600 * for completeness, test whether the cmd is output or input?
3602 err |= ni_check_trigger_arg(CR_CHAN(cmd->scan_begin_arg),
3604 &devpriv->routing_tables);
3605 if (CR_RANGE(cmd->scan_begin_arg) != 0 ||
3606 CR_AREF(cmd->scan_begin_arg) != 0)
3609 err |= comedi_check_trigger_arg_is(&cmd->convert_arg, 0);
3610 err |= comedi_check_trigger_arg_is(&cmd->scan_end_arg,
3612 bytes_per_scan = comedi_bytes_per_scan_cmd(s, cmd);
3613 if (bytes_per_scan) {
3614 err |= comedi_check_trigger_arg_max(&cmd->stop_arg,
3615 s->async->prealloc_bufsz /
3622 /* Step 4: fix up any arguments */
3624 /* Step 5: check channel list if it exists */
3626 if (cmd->chanlist && cmd->chanlist_len > 0)
3627 err |= ni_cdio_check_chanlist(dev, s, cmd);
3635 static int ni_cdo_inttrig(struct comedi_device *dev,
3636 struct comedi_subdevice *s,
3637 unsigned int trig_num)
3639 struct comedi_cmd *cmd = &s->async->cmd;
3640 const unsigned int timeout = 1000;
3643 struct ni_private *devpriv = dev->private;
3644 unsigned long flags;
3646 if (trig_num != cmd->start_arg)
3649 s->async->inttrig = NULL;
3651 /* read alloc the entire buffer */
3652 comedi_buf_read_alloc(s, s->async->prealloc_bufsz);
3654 spin_lock_irqsave(&devpriv->mite_channel_lock, flags);
3655 if (devpriv->cdo_mite_chan) {
3656 mite_prep_dma(devpriv->cdo_mite_chan, 32, 32);
3657 mite_dma_arm(devpriv->cdo_mite_chan);
3659 dev_err(dev->class_dev, "BUG: no cdo mite channel?\n");
3662 spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
3667 * XXX not sure what interrupt C group does
3668 * wait for dma to fill output fifo
3669 * ni_writeb(dev, NI_M_INTC_ENA, NI_M_INTC_ENA_REG);
3671 for (i = 0; i < timeout; ++i) {
3672 if (ni_readl(dev, NI_M_CDIO_STATUS_REG) &
3673 NI_M_CDIO_STATUS_CDO_FIFO_FULL)
3675 usleep_range(10, 100);
3678 dev_err(dev->class_dev, "dma failed to fill cdo fifo!\n");
3682 ni_writel(dev, NI_M_CDO_CMD_ARM |
3683 NI_M_CDO_CMD_ERR_INT_ENA_SET |
3684 NI_M_CDO_CMD_F_E_INT_ENA_SET,
3689 static int ni_cdio_cmd(struct comedi_device *dev, struct comedi_subdevice *s)
3691 struct ni_private *devpriv = dev->private;
3692 const struct comedi_cmd *cmd = &s->async->cmd;
3693 unsigned int cdo_mode_bits;
3696 ni_writel(dev, NI_M_CDO_CMD_RESET, NI_M_CDIO_CMD_REG);
3698 * Although NI_D[IO]_SampleClock are the same, perhaps we should still,
3699 * for completeness, test whether the cmd is output or input(?)
3701 cdo_mode_bits = NI_M_CDO_MODE_FIFO_MODE |
3702 NI_M_CDO_MODE_HALT_ON_ERROR |
3703 NI_M_CDO_MODE_SAMPLE_SRC(
3705 CR_CHAN(cmd->scan_begin_arg),
3707 &devpriv->routing_tables));
3708 if (cmd->scan_begin_arg & CR_INVERT)
3709 cdo_mode_bits |= NI_M_CDO_MODE_POLARITY;
3710 ni_writel(dev, cdo_mode_bits, NI_M_CDO_MODE_REG);
3712 ni_writel(dev, s->state, NI_M_CDO_FIFO_DATA_REG);
3713 ni_writel(dev, NI_M_CDO_CMD_SW_UPDATE, NI_M_CDIO_CMD_REG);
3714 ni_writel(dev, s->io_bits, NI_M_CDO_MASK_ENA_REG);
3716 dev_err(dev->class_dev,
3717 "attempted to run digital output command with no lines configured as outputs\n");
3720 retval = ni_request_cdo_mite_channel(dev);
3724 ni_cmd_set_mite_transfer(devpriv->cdo_mite_ring, s, cmd,
3725 s->async->prealloc_bufsz /
3726 comedi_bytes_per_scan(s));
3728 s->async->inttrig = ni_cdo_inttrig;
3733 static int ni_cdio_cancel(struct comedi_device *dev, struct comedi_subdevice *s)
3735 ni_writel(dev, NI_M_CDO_CMD_DISARM |
3736 NI_M_CDO_CMD_ERR_INT_ENA_CLR |
3737 NI_M_CDO_CMD_F_E_INT_ENA_CLR |
3738 NI_M_CDO_CMD_F_REQ_INT_ENA_CLR,
3741 * XXX not sure what interrupt C group does
3742 * ni_writeb(dev, 0, NI_M_INTC_ENA_REG);
3744 ni_writel(dev, 0, NI_M_CDO_MASK_ENA_REG);
3745 ni_release_cdo_mite_channel(dev);
3749 static void handle_cdio_interrupt(struct comedi_device *dev)
3751 struct ni_private *devpriv = dev->private;
3752 unsigned int cdio_status;
3753 struct comedi_subdevice *s = &dev->subdevices[NI_DIO_SUBDEV];
3754 unsigned long flags;
3756 spin_lock_irqsave(&devpriv->mite_channel_lock, flags);
3757 if (devpriv->cdo_mite_chan)
3758 mite_ack_linkc(devpriv->cdo_mite_chan, s, true);
3759 spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
3761 cdio_status = ni_readl(dev, NI_M_CDIO_STATUS_REG);
3762 if (cdio_status & NI_M_CDIO_STATUS_CDO_ERROR) {
3763 /* XXX just guessing this is needed and does something useful */
3764 ni_writel(dev, NI_M_CDO_CMD_ERR_INT_CONFIRM,
3766 s->async->events |= COMEDI_CB_OVERFLOW;
3768 if (cdio_status & NI_M_CDIO_STATUS_CDO_FIFO_EMPTY) {
3769 ni_writel(dev, NI_M_CDO_CMD_F_E_INT_ENA_CLR,
3771 /* s->async->events |= COMEDI_CB_EOA; */
3773 comedi_handle_events(dev, s);
3777 static int ni_serial_hw_readwrite8(struct comedi_device *dev,
3778 struct comedi_subdevice *s,
3779 unsigned char data_out,
3780 unsigned char *data_in)
3782 struct ni_private *devpriv = dev->private;
3783 unsigned int status1;
3784 int err = 0, count = 20;
3786 devpriv->dio_output &= ~NISTC_DIO_OUT_SERIAL_MASK;
3787 devpriv->dio_output |= NISTC_DIO_OUT_SERIAL(data_out);
3788 ni_stc_writew(dev, devpriv->dio_output, NISTC_DIO_OUT_REG);
3790 status1 = ni_stc_readw(dev, NISTC_STATUS1_REG);
3791 if (status1 & NISTC_STATUS1_SERIO_IN_PROG) {
3796 devpriv->dio_control |= NISTC_DIO_CTRL_HW_SER_START;
3797 ni_stc_writew(dev, devpriv->dio_control, NISTC_DIO_CTRL_REG);
3798 devpriv->dio_control &= ~NISTC_DIO_CTRL_HW_SER_START;
3800 /* Wait until STC says we're done, but don't loop infinitely. */
3801 while ((status1 = ni_stc_readw(dev, NISTC_STATUS1_REG)) &
3802 NISTC_STATUS1_SERIO_IN_PROG) {
3803 /* Delay one bit per loop */
3804 udelay((devpriv->serial_interval_ns + 999) / 1000);
3806 dev_err(dev->class_dev,
3807 "SPI serial I/O didn't finish in time!\n");
3814 * Delay for last bit. This delay is absolutely necessary, because
3815 * NISTC_STATUS1_SERIO_IN_PROG goes high one bit too early.
3817 udelay((devpriv->serial_interval_ns + 999) / 1000);
3820 *data_in = ni_stc_readw(dev, NISTC_DIO_SERIAL_IN_REG);
3823 ni_stc_writew(dev, devpriv->dio_control, NISTC_DIO_CTRL_REG);
3828 static int ni_serial_sw_readwrite8(struct comedi_device *dev,
3829 struct comedi_subdevice *s,
3830 unsigned char data_out,
3831 unsigned char *data_in)
3833 struct ni_private *devpriv = dev->private;
3834 unsigned char mask, input = 0;
3836 /* Wait for one bit before transfer */
3837 udelay((devpriv->serial_interval_ns + 999) / 1000);
3839 for (mask = 0x80; mask; mask >>= 1) {
3841 * Output current bit; note that we cannot touch s->state
3842 * because it is a per-subdevice field, and serial is
3843 * a separate subdevice from DIO.
3845 devpriv->dio_output &= ~NISTC_DIO_SDOUT;
3846 if (data_out & mask)
3847 devpriv->dio_output |= NISTC_DIO_SDOUT;
3848 ni_stc_writew(dev, devpriv->dio_output, NISTC_DIO_OUT_REG);
3851 * Assert SDCLK (active low, inverted), wait for half of
3852 * the delay, deassert SDCLK, and wait for the other half.
3854 devpriv->dio_control |= NISTC_DIO_SDCLK;
3855 ni_stc_writew(dev, devpriv->dio_control, NISTC_DIO_CTRL_REG);
3857 udelay((devpriv->serial_interval_ns + 999) / 2000);
3859 devpriv->dio_control &= ~NISTC_DIO_SDCLK;
3860 ni_stc_writew(dev, devpriv->dio_control, NISTC_DIO_CTRL_REG);
3862 udelay((devpriv->serial_interval_ns + 999) / 2000);
3864 /* Input current bit */
3865 if (ni_stc_readw(dev, NISTC_DIO_IN_REG) & NISTC_DIO_SDIN)
3875 static int ni_serial_insn_config(struct comedi_device *dev,
3876 struct comedi_subdevice *s,
3877 struct comedi_insn *insn,
3880 struct ni_private *devpriv = dev->private;
3881 unsigned int clk_fout = devpriv->clock_and_fout;
3883 unsigned char byte_out, byte_in = 0;
3889 case INSN_CONFIG_SERIAL_CLOCK:
3890 devpriv->serial_hw_mode = 1;
3891 devpriv->dio_control |= NISTC_DIO_CTRL_HW_SER_ENA;
3893 if (data[1] == SERIAL_DISABLED) {
3894 devpriv->serial_hw_mode = 0;
3895 devpriv->dio_control &= ~(NISTC_DIO_CTRL_HW_SER_ENA |
3897 data[1] = SERIAL_DISABLED;
3898 devpriv->serial_interval_ns = data[1];
3899 } else if (data[1] <= SERIAL_600NS) {
3901 * Warning: this clock speed is too fast to reliably
3904 devpriv->dio_control &= ~NISTC_DIO_CTRL_HW_SER_TIMEBASE;
3905 clk_fout |= NISTC_CLK_FOUT_SLOW_TIMEBASE;
3906 clk_fout &= ~NISTC_CLK_FOUT_DIO_SER_OUT_DIV2;
3907 data[1] = SERIAL_600NS;
3908 devpriv->serial_interval_ns = data[1];
3909 } else if (data[1] <= SERIAL_1_2US) {
3910 devpriv->dio_control &= ~NISTC_DIO_CTRL_HW_SER_TIMEBASE;
3911 clk_fout |= NISTC_CLK_FOUT_SLOW_TIMEBASE |
3912 NISTC_CLK_FOUT_DIO_SER_OUT_DIV2;
3913 data[1] = SERIAL_1_2US;
3914 devpriv->serial_interval_ns = data[1];
3915 } else if (data[1] <= SERIAL_10US) {
3916 devpriv->dio_control |= NISTC_DIO_CTRL_HW_SER_TIMEBASE;
3917 clk_fout |= NISTC_CLK_FOUT_SLOW_TIMEBASE |
3918 NISTC_CLK_FOUT_DIO_SER_OUT_DIV2;
3920 * Note: NISTC_CLK_FOUT_DIO_SER_OUT_DIV2 only affects
3921 * 600ns/1.2us. If you turn divide_by_2 off with the
3922 * slow clock, you will still get 10us, except then
3923 * all your delays are wrong.
3925 data[1] = SERIAL_10US;
3926 devpriv->serial_interval_ns = data[1];
3928 devpriv->dio_control &= ~(NISTC_DIO_CTRL_HW_SER_ENA |
3930 devpriv->serial_hw_mode = 0;
3931 data[1] = (data[1] / 1000) * 1000;
3932 devpriv->serial_interval_ns = data[1];
3934 devpriv->clock_and_fout = clk_fout;
3936 ni_stc_writew(dev, devpriv->dio_control, NISTC_DIO_CTRL_REG);
3937 ni_stc_writew(dev, devpriv->clock_and_fout, NISTC_CLK_FOUT_REG);
3940 case INSN_CONFIG_BIDIRECTIONAL_DATA:
3942 if (devpriv->serial_interval_ns == 0)
3945 byte_out = data[1] & 0xFF;
3947 if (devpriv->serial_hw_mode) {
3948 err = ni_serial_hw_readwrite8(dev, s, byte_out,
3950 } else if (devpriv->serial_interval_ns > 0) {
3951 err = ni_serial_sw_readwrite8(dev, s, byte_out,
3954 dev_err(dev->class_dev, "serial disabled!\n");
3959 data[1] = byte_in & 0xFF;
3968 static void init_ao_67xx(struct comedi_device *dev, struct comedi_subdevice *s)
3972 for (i = 0; i < s->n_chan; i++) {
3973 ni_ao_win_outw(dev, NI_E_AO_DACSEL(i) | 0x0,
3974 NI67XX_AO_CFG2_REG);
3976 ni_ao_win_outw(dev, 0x0, NI67XX_AO_SP_UPDATES_REG);
3979 static const struct mio_regmap ni_gpct_to_stc_regmap[] = {
3980 [NITIO_G0_AUTO_INC] = { NISTC_G0_AUTOINC_REG, 2 },
3981 [NITIO_G1_AUTO_INC] = { NISTC_G1_AUTOINC_REG, 2 },
3982 [NITIO_G0_CMD] = { NISTC_G0_CMD_REG, 2 },
3983 [NITIO_G1_CMD] = { NISTC_G1_CMD_REG, 2 },
3984 [NITIO_G0_HW_SAVE] = { NISTC_G0_HW_SAVE_REG, 4 },
3985 [NITIO_G1_HW_SAVE] = { NISTC_G1_HW_SAVE_REG, 4 },
3986 [NITIO_G0_SW_SAVE] = { NISTC_G0_SAVE_REG, 4 },
3987 [NITIO_G1_SW_SAVE] = { NISTC_G1_SAVE_REG, 4 },
3988 [NITIO_G0_MODE] = { NISTC_G0_MODE_REG, 2 },
3989 [NITIO_G1_MODE] = { NISTC_G1_MODE_REG, 2 },
3990 [NITIO_G0_LOADA] = { NISTC_G0_LOADA_REG, 4 },
3991 [NITIO_G1_LOADA] = { NISTC_G1_LOADA_REG, 4 },
3992 [NITIO_G0_LOADB] = { NISTC_G0_LOADB_REG, 4 },
3993 [NITIO_G1_LOADB] = { NISTC_G1_LOADB_REG, 4 },
3994 [NITIO_G0_INPUT_SEL] = { NISTC_G0_INPUT_SEL_REG, 2 },
3995 [NITIO_G1_INPUT_SEL] = { NISTC_G1_INPUT_SEL_REG, 2 },
3996 [NITIO_G0_CNT_MODE] = { 0x1b0, 2 }, /* M-Series only */
3997 [NITIO_G1_CNT_MODE] = { 0x1b2, 2 }, /* M-Series only */
3998 [NITIO_G0_GATE2] = { 0x1b4, 2 }, /* M-Series only */
3999 [NITIO_G1_GATE2] = { 0x1b6, 2 }, /* M-Series only */
4000 [NITIO_G01_STATUS] = { NISTC_G01_STATUS_REG, 2 },
4001 [NITIO_G01_RESET] = { NISTC_RESET_REG, 2 },
4002 [NITIO_G01_STATUS1] = { NISTC_STATUS1_REG, 2 },
4003 [NITIO_G01_STATUS2] = { NISTC_STATUS2_REG, 2 },
4004 [NITIO_G0_DMA_CFG] = { 0x1b8, 2 }, /* M-Series only */
4005 [NITIO_G1_DMA_CFG] = { 0x1ba, 2 }, /* M-Series only */
4006 [NITIO_G0_DMA_STATUS] = { 0x1b8, 2 }, /* M-Series only */
4007 [NITIO_G1_DMA_STATUS] = { 0x1ba, 2 }, /* M-Series only */
4008 [NITIO_G0_ABZ] = { 0x1c0, 2 }, /* M-Series only */
4009 [NITIO_G1_ABZ] = { 0x1c2, 2 }, /* M-Series only */
4010 [NITIO_G0_INT_ACK] = { NISTC_INTA_ACK_REG, 2 },
4011 [NITIO_G1_INT_ACK] = { NISTC_INTB_ACK_REG, 2 },
4012 [NITIO_G0_STATUS] = { NISTC_AI_STATUS1_REG, 2 },
4013 [NITIO_G1_STATUS] = { NISTC_AO_STATUS1_REG, 2 },
4014 [NITIO_G0_INT_ENA] = { NISTC_INTA_ENA_REG, 2 },
4015 [NITIO_G1_INT_ENA] = { NISTC_INTB_ENA_REG, 2 },
4018 static unsigned int ni_gpct_to_stc_register(struct comedi_device *dev,
4019 enum ni_gpct_register reg)
4021 const struct mio_regmap *regmap;
4023 if (reg < ARRAY_SIZE(ni_gpct_to_stc_regmap)) {
4024 regmap = &ni_gpct_to_stc_regmap[reg];
4026 dev_warn(dev->class_dev, "%s: unhandled register=0x%x\n",
4031 return regmap->mio_reg;
4034 static void ni_gpct_write_register(struct ni_gpct *counter, unsigned int bits,
4035 enum ni_gpct_register reg)
4037 struct comedi_device *dev = counter->counter_dev->dev;
4038 unsigned int stc_register = ni_gpct_to_stc_register(dev, reg);
4040 if (stc_register == 0)
4044 /* m-series only registers */
4045 case NITIO_G0_CNT_MODE:
4046 case NITIO_G1_CNT_MODE:
4047 case NITIO_G0_GATE2:
4048 case NITIO_G1_GATE2:
4049 case NITIO_G0_DMA_CFG:
4050 case NITIO_G1_DMA_CFG:
4053 ni_writew(dev, bits, stc_register);
4056 /* 32 bit registers */
4057 case NITIO_G0_LOADA:
4058 case NITIO_G1_LOADA:
4059 case NITIO_G0_LOADB:
4060 case NITIO_G1_LOADB:
4061 ni_stc_writel(dev, bits, stc_register);
4064 /* 16 bit registers */
4065 case NITIO_G0_INT_ENA:
4066 ni_set_bitfield(dev, stc_register,
4067 NISTC_INTA_ENA_G0_GATE | NISTC_INTA_ENA_G0_TC,
4070 case NITIO_G1_INT_ENA:
4071 ni_set_bitfield(dev, stc_register,
4072 NISTC_INTB_ENA_G1_GATE | NISTC_INTB_ENA_G1_TC,
4076 ni_stc_writew(dev, bits, stc_register);
4080 static unsigned int ni_gpct_read_register(struct ni_gpct *counter,
4081 enum ni_gpct_register reg)
4083 struct comedi_device *dev = counter->counter_dev->dev;
4084 unsigned int stc_register = ni_gpct_to_stc_register(dev, reg);
4086 if (stc_register == 0)
4090 /* m-series only registers */
4091 case NITIO_G0_DMA_STATUS:
4092 case NITIO_G1_DMA_STATUS:
4093 return ni_readw(dev, stc_register);
4095 /* 32 bit registers */
4096 case NITIO_G0_HW_SAVE:
4097 case NITIO_G1_HW_SAVE:
4098 case NITIO_G0_SW_SAVE:
4099 case NITIO_G1_SW_SAVE:
4100 return ni_stc_readl(dev, stc_register);
4102 /* 16 bit registers */
4104 return ni_stc_readw(dev, stc_register);
4108 static int ni_freq_out_insn_read(struct comedi_device *dev,
4109 struct comedi_subdevice *s,
4110 struct comedi_insn *insn,
4113 struct ni_private *devpriv = dev->private;
4114 unsigned int val = NISTC_CLK_FOUT_TO_DIVIDER(devpriv->clock_and_fout);
4117 for (i = 0; i < insn->n; i++)
4123 static int ni_freq_out_insn_write(struct comedi_device *dev,
4124 struct comedi_subdevice *s,
4125 struct comedi_insn *insn,
4128 struct ni_private *devpriv = dev->private;
4131 unsigned int val = data[insn->n - 1];
4133 devpriv->clock_and_fout &= ~NISTC_CLK_FOUT_ENA;
4134 ni_stc_writew(dev, devpriv->clock_and_fout, NISTC_CLK_FOUT_REG);
4135 devpriv->clock_and_fout &= ~NISTC_CLK_FOUT_DIVIDER_MASK;
4137 /* use the last data value to set the fout divider */
4138 devpriv->clock_and_fout |= NISTC_CLK_FOUT_DIVIDER(val);
4140 devpriv->clock_and_fout |= NISTC_CLK_FOUT_ENA;
4141 ni_stc_writew(dev, devpriv->clock_and_fout, NISTC_CLK_FOUT_REG);
4146 static int ni_freq_out_insn_config(struct comedi_device *dev,
4147 struct comedi_subdevice *s,
4148 struct comedi_insn *insn,
4151 struct ni_private *devpriv = dev->private;
4154 case INSN_CONFIG_SET_CLOCK_SRC:
4156 case NI_FREQ_OUT_TIMEBASE_1_DIV_2_CLOCK_SRC:
4157 devpriv->clock_and_fout &= ~NISTC_CLK_FOUT_TIMEBASE_SEL;
4159 case NI_FREQ_OUT_TIMEBASE_2_CLOCK_SRC:
4160 devpriv->clock_and_fout |= NISTC_CLK_FOUT_TIMEBASE_SEL;
4165 ni_stc_writew(dev, devpriv->clock_and_fout, NISTC_CLK_FOUT_REG);
4167 case INSN_CONFIG_GET_CLOCK_SRC:
4168 if (devpriv->clock_and_fout & NISTC_CLK_FOUT_TIMEBASE_SEL) {
4169 data[1] = NI_FREQ_OUT_TIMEBASE_2_CLOCK_SRC;
4170 data[2] = TIMEBASE_2_NS;
4172 data[1] = NI_FREQ_OUT_TIMEBASE_1_DIV_2_CLOCK_SRC;
4173 data[2] = TIMEBASE_1_NS * 2;
4182 static int ni_8255_callback(struct comedi_device *dev,
4183 int dir, int port, int data, unsigned long iobase)
4186 ni_writeb(dev, data, iobase + 2 * port);
4190 return ni_readb(dev, iobase + 2 * port);
4193 static int ni_get_pwm_config(struct comedi_device *dev, unsigned int *data)
4195 struct ni_private *devpriv = dev->private;
4197 data[1] = devpriv->pwm_up_count * devpriv->clock_ns;
4198 data[2] = devpriv->pwm_down_count * devpriv->clock_ns;
4202 static int ni_m_series_pwm_config(struct comedi_device *dev,
4203 struct comedi_subdevice *s,
4204 struct comedi_insn *insn,
4207 struct ni_private *devpriv = dev->private;
4208 unsigned int up_count, down_count;
4211 case INSN_CONFIG_PWM_OUTPUT:
4213 case CMDF_ROUND_NEAREST:
4214 up_count = DIV_ROUND_CLOSEST(data[2],
4217 case CMDF_ROUND_DOWN:
4218 up_count = data[2] / devpriv->clock_ns;
4222 DIV_ROUND_UP(data[2], devpriv->clock_ns);
4228 case CMDF_ROUND_NEAREST:
4229 down_count = DIV_ROUND_CLOSEST(data[4],
4232 case CMDF_ROUND_DOWN:
4233 down_count = data[4] / devpriv->clock_ns;
4237 DIV_ROUND_UP(data[4], devpriv->clock_ns);
4242 if (up_count * devpriv->clock_ns != data[2] ||
4243 down_count * devpriv->clock_ns != data[4]) {
4244 data[2] = up_count * devpriv->clock_ns;
4245 data[4] = down_count * devpriv->clock_ns;
4248 ni_writel(dev, NI_M_CAL_PWM_HIGH_TIME(up_count) |
4249 NI_M_CAL_PWM_LOW_TIME(down_count),
4251 devpriv->pwm_up_count = up_count;
4252 devpriv->pwm_down_count = down_count;
4254 case INSN_CONFIG_GET_PWM_OUTPUT:
4255 return ni_get_pwm_config(dev, data);
4262 static int ni_6143_pwm_config(struct comedi_device *dev,
4263 struct comedi_subdevice *s,
4264 struct comedi_insn *insn,
4267 struct ni_private *devpriv = dev->private;
4268 unsigned int up_count, down_count;
4271 case INSN_CONFIG_PWM_OUTPUT:
4273 case CMDF_ROUND_NEAREST:
4274 up_count = DIV_ROUND_CLOSEST(data[2],
4277 case CMDF_ROUND_DOWN:
4278 up_count = data[2] / devpriv->clock_ns;
4282 DIV_ROUND_UP(data[2], devpriv->clock_ns);
4288 case CMDF_ROUND_NEAREST:
4289 down_count = DIV_ROUND_CLOSEST(data[4],
4292 case CMDF_ROUND_DOWN:
4293 down_count = data[4] / devpriv->clock_ns;
4297 DIV_ROUND_UP(data[4], devpriv->clock_ns);
4302 if (up_count * devpriv->clock_ns != data[2] ||
4303 down_count * devpriv->clock_ns != data[4]) {
4304 data[2] = up_count * devpriv->clock_ns;
4305 data[4] = down_count * devpriv->clock_ns;
4308 ni_writel(dev, up_count, NI6143_CALIB_HI_TIME_REG);
4309 devpriv->pwm_up_count = up_count;
4310 ni_writel(dev, down_count, NI6143_CALIB_LO_TIME_REG);
4311 devpriv->pwm_down_count = down_count;
4313 case INSN_CONFIG_GET_PWM_OUTPUT:
4314 return ni_get_pwm_config(dev, data);
4321 static int pack_mb88341(int addr, int val, int *bitstring)
4325 * Note that address bits are reversed. Thanks to
4326 * Ingo Keen for noticing this.
4328 * Note also that the 88341 expects address values from
4329 * 1-12, whereas we use channel numbers 0-11. The NI
4330 * docs use 1-12, also, so be careful here.
4333 *bitstring = ((addr & 0x1) << 11) |
4334 ((addr & 0x2) << 9) |
4335 ((addr & 0x4) << 7) | ((addr & 0x8) << 5) | (val & 0xff);
4339 static int pack_dac8800(int addr, int val, int *bitstring)
4341 *bitstring = ((addr & 0x7) << 8) | (val & 0xff);
4345 static int pack_dac8043(int addr, int val, int *bitstring)
4347 *bitstring = val & 0xfff;
4351 static int pack_ad8522(int addr, int val, int *bitstring)
4353 *bitstring = (val & 0xfff) | (addr ? 0xc000 : 0xa000);
4357 static int pack_ad8804(int addr, int val, int *bitstring)
4359 *bitstring = ((addr & 0xf) << 8) | (val & 0xff);
4363 static int pack_ad8842(int addr, int val, int *bitstring)
4365 *bitstring = ((addr + 1) << 8) | (val & 0xff);
4369 struct caldac_struct {
4372 int (*packbits)(int address, int value, int *bitstring);
4375 static struct caldac_struct caldacs[] = {
4376 [mb88341] = {12, 8, pack_mb88341},
4377 [dac8800] = {8, 8, pack_dac8800},
4378 [dac8043] = {1, 12, pack_dac8043},
4379 [ad8522] = {2, 12, pack_ad8522},
4380 [ad8804] = {12, 8, pack_ad8804},
4381 [ad8842] = {8, 8, pack_ad8842},
4382 [ad8804_debug] = {16, 8, pack_ad8804},
4385 static void ni_write_caldac(struct comedi_device *dev, int addr, int val)
4387 const struct ni_board_struct *board = dev->board_ptr;
4388 struct ni_private *devpriv = dev->private;
4389 unsigned int loadbit = 0, bits = 0, bit, bitstring = 0;
4394 if (devpriv->caldacs[addr] == val)
4396 devpriv->caldacs[addr] = val;
4398 for (i = 0; i < 3; i++) {
4399 type = board->caldac[i];
4400 if (type == caldac_none)
4402 if (addr < caldacs[type].n_chans) {
4403 bits = caldacs[type].packbits(addr, val, &bitstring);
4404 loadbit = NI_E_SERIAL_CMD_DAC_LD(i);
4407 addr -= caldacs[type].n_chans;
4410 /* bits will be 0 if there is no caldac for the given addr */
4414 for (bit = 1 << (bits - 1); bit; bit >>= 1) {
4415 cmd = (bit & bitstring) ? NI_E_SERIAL_CMD_SDATA : 0;
4416 ni_writeb(dev, cmd, NI_E_SERIAL_CMD_REG);
4418 ni_writeb(dev, NI_E_SERIAL_CMD_SCLK | cmd, NI_E_SERIAL_CMD_REG);
4421 ni_writeb(dev, loadbit, NI_E_SERIAL_CMD_REG);
4423 ni_writeb(dev, 0, NI_E_SERIAL_CMD_REG);
4426 static int ni_calib_insn_write(struct comedi_device *dev,
4427 struct comedi_subdevice *s,
4428 struct comedi_insn *insn,
4432 /* only bother writing the last sample to the channel */
4433 ni_write_caldac(dev, CR_CHAN(insn->chanspec),
4440 static int ni_calib_insn_read(struct comedi_device *dev,
4441 struct comedi_subdevice *s,
4442 struct comedi_insn *insn,
4445 struct ni_private *devpriv = dev->private;
4448 for (i = 0; i < insn->n; i++)
4449 data[0] = devpriv->caldacs[CR_CHAN(insn->chanspec)];
4454 static void caldac_setup(struct comedi_device *dev, struct comedi_subdevice *s)
4456 const struct ni_board_struct *board = dev->board_ptr;
4457 struct ni_private *devpriv = dev->private;
4466 type = board->caldac[0];
4467 if (type == caldac_none)
4469 n_bits = caldacs[type].n_bits;
4470 for (i = 0; i < 3; i++) {
4471 type = board->caldac[i];
4472 if (type == caldac_none)
4474 if (caldacs[type].n_bits != n_bits)
4476 n_chans += caldacs[type].n_chans;
4479 s->n_chan = n_chans;
4482 unsigned int *maxdata_list = devpriv->caldac_maxdata_list;
4484 if (n_chans > MAX_N_CALDACS)
4485 dev_err(dev->class_dev,
4486 "BUG! MAX_N_CALDACS too small\n");
4487 s->maxdata_list = maxdata_list;
4489 for (i = 0; i < n_dacs; i++) {
4490 type = board->caldac[i];
4491 for (j = 0; j < caldacs[type].n_chans; j++) {
4492 maxdata_list[chan] =
4493 (1 << caldacs[type].n_bits) - 1;
4498 for (chan = 0; chan < s->n_chan; chan++)
4499 ni_write_caldac(dev, i, s->maxdata_list[i] / 2);
4501 type = board->caldac[0];
4502 s->maxdata = (1 << caldacs[type].n_bits) - 1;
4504 for (chan = 0; chan < s->n_chan; chan++)
4505 ni_write_caldac(dev, i, s->maxdata / 2);
4509 static int ni_read_eeprom(struct comedi_device *dev, int addr)
4511 unsigned int cmd = NI_E_SERIAL_CMD_EEPROM_CS;
4515 bitstring = 0x0300 | ((addr & 0x100) << 3) | (addr & 0xff);
4516 ni_writeb(dev, cmd, NI_E_SERIAL_CMD_REG);
4517 for (bit = 0x8000; bit; bit >>= 1) {
4518 if (bit & bitstring)
4519 cmd |= NI_E_SERIAL_CMD_SDATA;
4521 cmd &= ~NI_E_SERIAL_CMD_SDATA;
4523 ni_writeb(dev, cmd, NI_E_SERIAL_CMD_REG);
4524 ni_writeb(dev, NI_E_SERIAL_CMD_SCLK | cmd, NI_E_SERIAL_CMD_REG);
4526 cmd = NI_E_SERIAL_CMD_EEPROM_CS;
4528 for (bit = 0x80; bit; bit >>= 1) {
4529 ni_writeb(dev, cmd, NI_E_SERIAL_CMD_REG);
4530 ni_writeb(dev, NI_E_SERIAL_CMD_SCLK | cmd, NI_E_SERIAL_CMD_REG);
4531 if (ni_readb(dev, NI_E_STATUS_REG) & NI_E_STATUS_PROMOUT)
4534 ni_writeb(dev, 0, NI_E_SERIAL_CMD_REG);
4539 static int ni_eeprom_insn_read(struct comedi_device *dev,
4540 struct comedi_subdevice *s,
4541 struct comedi_insn *insn,
4548 val = ni_read_eeprom(dev, CR_CHAN(insn->chanspec));
4549 for (i = 0; i < insn->n; i++)
4555 static int ni_m_series_eeprom_insn_read(struct comedi_device *dev,
4556 struct comedi_subdevice *s,
4557 struct comedi_insn *insn,
4560 struct ni_private *devpriv = dev->private;
4563 for (i = 0; i < insn->n; i++)
4564 data[i] = devpriv->eeprom_buffer[CR_CHAN(insn->chanspec)];
4569 static unsigned int ni_old_get_pfi_routing(struct comedi_device *dev,
4572 /* pre-m-series boards have fixed signals on pfi pins */
4575 return NI_PFI_OUTPUT_AI_START1;
4577 return NI_PFI_OUTPUT_AI_START2;
4579 return NI_PFI_OUTPUT_AI_CONVERT;
4581 return NI_PFI_OUTPUT_G_SRC1;
4583 return NI_PFI_OUTPUT_G_GATE1;
4585 return NI_PFI_OUTPUT_AO_UPDATE_N;
4587 return NI_PFI_OUTPUT_AO_START1;
4589 return NI_PFI_OUTPUT_AI_START_PULSE;
4591 return NI_PFI_OUTPUT_G_SRC0;
4593 return NI_PFI_OUTPUT_G_GATE0;
4595 dev_err(dev->class_dev, "bug, unhandled case in switch.\n");
4601 static int ni_old_set_pfi_routing(struct comedi_device *dev,
4602 unsigned int chan, unsigned int source)
4604 /* pre-m-series boards have fixed signals on pfi pins */
4605 if (source != ni_old_get_pfi_routing(dev, chan))
4610 static unsigned int ni_m_series_get_pfi_routing(struct comedi_device *dev,
4613 struct ni_private *devpriv = dev->private;
4614 const unsigned int array_offset = chan / 3;
4616 return NI_M_PFI_OUT_SEL_TO_SRC(chan,
4617 devpriv->pfi_output_select_reg[array_offset]);
4620 static int ni_m_series_set_pfi_routing(struct comedi_device *dev,
4621 unsigned int chan, unsigned int source)
4623 struct ni_private *devpriv = dev->private;
4624 unsigned int index = chan / 3;
4625 unsigned short val = devpriv->pfi_output_select_reg[index];
4627 if ((source & 0x1f) != source)
4630 val &= ~NI_M_PFI_OUT_SEL_MASK(chan);
4631 val |= NI_M_PFI_OUT_SEL(chan, source);
4632 ni_writew(dev, val, NI_M_PFI_OUT_SEL_REG(index));
4633 devpriv->pfi_output_select_reg[index] = val;
4638 static unsigned int ni_get_pfi_routing(struct comedi_device *dev,
4641 struct ni_private *devpriv = dev->private;
4643 if (chan >= NI_PFI(0)) {
4644 /* allow new and old names of pfi channels to work. */
4647 return (devpriv->is_m_series)
4648 ? ni_m_series_get_pfi_routing(dev, chan)
4649 : ni_old_get_pfi_routing(dev, chan);
4652 /* Sets the output mux for the specified PFI channel. */
4653 static int ni_set_pfi_routing(struct comedi_device *dev,
4654 unsigned int chan, unsigned int source)
4656 struct ni_private *devpriv = dev->private;
4658 if (chan >= NI_PFI(0)) {
4659 /* allow new and old names of pfi channels to work. */
4662 return (devpriv->is_m_series)
4663 ? ni_m_series_set_pfi_routing(dev, chan, source)
4664 : ni_old_set_pfi_routing(dev, chan, source);
4667 static int ni_config_pfi_filter(struct comedi_device *dev,
4669 enum ni_pfi_filter_select filter)
4671 struct ni_private *devpriv = dev->private;
4674 if (!devpriv->is_m_series)
4677 if (chan >= NI_PFI(0)) {
4678 /* allow new and old names of pfi channels to work. */
4682 bits = ni_readl(dev, NI_M_PFI_FILTER_REG);
4683 bits &= ~NI_M_PFI_FILTER_SEL_MASK(chan);
4684 bits |= NI_M_PFI_FILTER_SEL(chan, filter);
4685 ni_writel(dev, bits, NI_M_PFI_FILTER_REG);
4689 static void ni_set_pfi_direction(struct comedi_device *dev, int chan,
4690 unsigned int direction)
4692 if (chan >= NI_PFI(0)) {
4693 /* allow new and old names of pfi channels to work. */
4696 direction = (direction == COMEDI_OUTPUT) ? 1u : 0u;
4697 ni_set_bits(dev, NISTC_IO_BIDIR_PIN_REG, 1 << chan, direction);
4700 static int ni_get_pfi_direction(struct comedi_device *dev, int chan)
4702 struct ni_private *devpriv = dev->private;
4704 if (chan >= NI_PFI(0)) {
4705 /* allow new and old names of pfi channels to work. */
4708 return devpriv->io_bidirection_pin_reg & (1 << chan) ?
4709 COMEDI_OUTPUT : COMEDI_INPUT;
4712 static int ni_pfi_insn_config(struct comedi_device *dev,
4713 struct comedi_subdevice *s,
4714 struct comedi_insn *insn,
4722 chan = CR_CHAN(insn->chanspec);
4727 ni_set_pfi_direction(dev, chan, data[0]);
4729 case INSN_CONFIG_DIO_QUERY:
4730 data[1] = ni_get_pfi_direction(dev, chan);
4732 case INSN_CONFIG_SET_ROUTING:
4733 return ni_set_pfi_routing(dev, chan, data[1]);
4734 case INSN_CONFIG_GET_ROUTING:
4735 data[1] = ni_get_pfi_routing(dev, chan);
4737 case INSN_CONFIG_FILTER:
4738 return ni_config_pfi_filter(dev, chan, data[1]);
4745 static int ni_pfi_insn_bits(struct comedi_device *dev,
4746 struct comedi_subdevice *s,
4747 struct comedi_insn *insn,
4750 struct ni_private *devpriv = dev->private;
4752 if (!devpriv->is_m_series)
4755 if (comedi_dio_update_state(s, data))
4756 ni_writew(dev, s->state, NI_M_PFI_DO_REG);
4758 data[1] = ni_readw(dev, NI_M_PFI_DI_REG);
4763 static int cs5529_wait_for_idle(struct comedi_device *dev)
4765 unsigned short status;
4766 const int timeout = HZ;
4769 for (i = 0; i < timeout; i++) {
4770 status = ni_ao_win_inw(dev, NI67XX_CAL_STATUS_REG);
4771 if ((status & NI67XX_CAL_STATUS_BUSY) == 0)
4773 set_current_state(TASK_INTERRUPTIBLE);
4774 if (schedule_timeout(1))
4778 dev_err(dev->class_dev, "timeout\n");
4784 static void cs5529_command(struct comedi_device *dev, unsigned short value)
4786 static const int timeout = 100;
4789 ni_ao_win_outw(dev, value, NI67XX_CAL_CMD_REG);
4790 /* give time for command to start being serially clocked into cs5529.
4791 * this insures that the NI67XX_CAL_STATUS_BUSY bit will get properly
4792 * set before we exit this function.
4794 for (i = 0; i < timeout; i++) {
4795 if (ni_ao_win_inw(dev, NI67XX_CAL_STATUS_REG) &
4796 NI67XX_CAL_STATUS_BUSY)
4801 dev_err(dev->class_dev,
4802 "possible problem - never saw adc go busy?\n");
4805 static int cs5529_do_conversion(struct comedi_device *dev,
4806 unsigned short *data)
4809 unsigned short status;
4811 cs5529_command(dev, CS5529_CMD_CB | CS5529_CMD_SINGLE_CONV);
4812 retval = cs5529_wait_for_idle(dev);
4814 dev_err(dev->class_dev,
4815 "timeout or signal in %s()\n", __func__);
4818 status = ni_ao_win_inw(dev, NI67XX_CAL_STATUS_REG);
4819 if (status & NI67XX_CAL_STATUS_OSC_DETECT) {
4820 dev_err(dev->class_dev,
4821 "cs5529 conversion error, status CSS_OSC_DETECT\n");
4824 if (status & NI67XX_CAL_STATUS_OVERRANGE) {
4825 dev_err(dev->class_dev,
4826 "cs5529 conversion error, overrange (ignoring)\n");
4829 *data = ni_ao_win_inw(dev, NI67XX_CAL_DATA_REG);
4830 /* cs5529 returns 16 bit signed data in bipolar mode */
4836 static int cs5529_ai_insn_read(struct comedi_device *dev,
4837 struct comedi_subdevice *s,
4838 struct comedi_insn *insn,
4842 unsigned short sample;
4843 unsigned int channel_select;
4844 const unsigned int INTERNAL_REF = 0x1000;
4847 * Set calibration adc source. Docs lie, reference select bits 8 to 11
4848 * do nothing. bit 12 seems to chooses internal reference voltage, bit
4849 * 13 causes the adc input to go overrange (maybe reads external
4852 if (insn->chanspec & CR_ALT_SOURCE)
4853 channel_select = INTERNAL_REF;
4855 channel_select = CR_CHAN(insn->chanspec);
4856 ni_ao_win_outw(dev, channel_select, NI67XX_AO_CAL_CHAN_SEL_REG);
4858 for (n = 0; n < insn->n; n++) {
4859 retval = cs5529_do_conversion(dev, &sample);
4867 static void cs5529_config_write(struct comedi_device *dev, unsigned int value,
4868 unsigned int reg_select_bits)
4870 ni_ao_win_outw(dev, (value >> 16) & 0xff, NI67XX_CAL_CFG_HI_REG);
4871 ni_ao_win_outw(dev, value & 0xffff, NI67XX_CAL_CFG_LO_REG);
4872 reg_select_bits &= CS5529_CMD_REG_MASK;
4873 cs5529_command(dev, CS5529_CMD_CB | reg_select_bits);
4874 if (cs5529_wait_for_idle(dev))
4875 dev_err(dev->class_dev,
4876 "timeout or signal in %s\n", __func__);
4879 static int init_cs5529(struct comedi_device *dev)
4881 unsigned int config_bits = CS5529_CFG_PORT_FLAG |
4882 CS5529_CFG_WORD_RATE_2180;
4885 /* do self-calibration */
4886 cs5529_config_write(dev, config_bits | CS5529_CFG_CALIB_BOTH_SELF,
4888 /* need to force a conversion for calibration to run */
4889 cs5529_do_conversion(dev, NULL);
4891 /* force gain calibration to 1 */
4892 cs5529_config_write(dev, 0x400000, CS5529_GAIN_REG);
4893 cs5529_config_write(dev, config_bits | CS5529_CFG_CALIB_OFFSET_SELF,
4895 if (cs5529_wait_for_idle(dev))
4896 dev_err(dev->class_dev,
4897 "timeout or signal in %s\n", __func__);
4903 * Find best multiplier/divider to try and get the PLL running at 80 MHz
4904 * given an arbitrary frequency input clock.
4906 static int ni_mseries_get_pll_parameters(unsigned int reference_period_ns,
4907 unsigned int *freq_divider,
4908 unsigned int *freq_multiplier,
4909 unsigned int *actual_period_ns)
4912 unsigned int best_div = 1;
4914 unsigned int best_mult = 1;
4915 static const unsigned int pico_per_nano = 1000;
4916 const unsigned int reference_picosec = reference_period_ns *
4919 * m-series wants the phased-locked loop to output 80MHz, which is
4920 * divided by 4 to 20 MHz for most timing clocks
4922 static const unsigned int target_picosec = 12500;
4923 int best_period_picosec = 0;
4925 for (div = 1; div <= NI_M_PLL_MAX_DIVISOR; ++div) {
4926 for (mult = 1; mult <= NI_M_PLL_MAX_MULTIPLIER; ++mult) {
4927 unsigned int new_period_ps =
4928 (reference_picosec * div) / mult;
4929 if (abs(new_period_ps - target_picosec) <
4930 abs(best_period_picosec - target_picosec)) {
4931 best_period_picosec = new_period_ps;
4937 if (best_period_picosec == 0)
4940 *freq_divider = best_div;
4941 *freq_multiplier = best_mult;
4942 /* return the actual period (* fudge factor for 80 to 20 MHz) */
4943 *actual_period_ns = DIV_ROUND_CLOSEST(best_period_picosec * 4,
4948 static int ni_mseries_set_pll_master_clock(struct comedi_device *dev,
4949 unsigned int source,
4950 unsigned int period_ns)
4952 struct ni_private *devpriv = dev->private;
4953 static const unsigned int min_period_ns = 50;
4954 static const unsigned int max_period_ns = 1000;
4955 static const unsigned int timeout = 1000;
4956 unsigned int pll_control_bits;
4957 unsigned int freq_divider;
4958 unsigned int freq_multiplier;
4963 if (source == NI_MIO_PLL_PXI10_CLOCK)
4966 * These limits are somewhat arbitrary, but NI advertises 1 to 20MHz
4967 * range so we'll use that.
4969 if (period_ns < min_period_ns || period_ns > max_period_ns) {
4970 dev_err(dev->class_dev,
4971 "%s: you must specify an input clock frequency between %i and %i nanosec for the phased-lock loop\n",
4972 __func__, min_period_ns, max_period_ns);
4975 devpriv->rtsi_trig_direction_reg &= ~NISTC_RTSI_TRIG_USE_CLK;
4976 ni_stc_writew(dev, devpriv->rtsi_trig_direction_reg,
4977 NISTC_RTSI_TRIG_DIR_REG);
4978 pll_control_bits = NI_M_PLL_CTRL_ENA | NI_M_PLL_CTRL_VCO_MODE_75_150MHZ;
4979 devpriv->clock_and_fout2 |= NI_M_CLK_FOUT2_TIMEBASE1_PLL |
4980 NI_M_CLK_FOUT2_TIMEBASE3_PLL;
4981 devpriv->clock_and_fout2 &= ~NI_M_CLK_FOUT2_PLL_SRC_MASK;
4983 case NI_MIO_PLL_PXI_STAR_TRIGGER_CLOCK:
4984 devpriv->clock_and_fout2 |= NI_M_CLK_FOUT2_PLL_SRC_STAR;
4986 case NI_MIO_PLL_PXI10_CLOCK:
4987 /* pxi clock is 10MHz */
4988 devpriv->clock_and_fout2 |= NI_M_CLK_FOUT2_PLL_SRC_PXI10;
4991 for (rtsi = 0; rtsi <= NI_M_MAX_RTSI_CHAN; ++rtsi) {
4992 if (source == NI_MIO_PLL_RTSI_CLOCK(rtsi)) {
4993 devpriv->clock_and_fout2 |=
4994 NI_M_CLK_FOUT2_PLL_SRC_RTSI(rtsi);
4998 if (rtsi > NI_M_MAX_RTSI_CHAN)
5002 retval = ni_mseries_get_pll_parameters(period_ns,
5005 &devpriv->clock_ns);
5007 dev_err(dev->class_dev,
5008 "bug, failed to find pll parameters\n");
5012 ni_writew(dev, devpriv->clock_and_fout2, NI_M_CLK_FOUT2_REG);
5013 pll_control_bits |= NI_M_PLL_CTRL_DIVISOR(freq_divider) |
5014 NI_M_PLL_CTRL_MULTIPLIER(freq_multiplier);
5016 ni_writew(dev, pll_control_bits, NI_M_PLL_CTRL_REG);
5017 devpriv->clock_source = source;
5018 /* it takes a few hundred microseconds for PLL to lock */
5019 for (i = 0; i < timeout; ++i) {
5020 if (ni_readw(dev, NI_M_PLL_STATUS_REG) & NI_M_PLL_STATUS_LOCKED)
5025 dev_err(dev->class_dev,
5026 "%s: timed out waiting for PLL to lock to reference clock source %i with period %i ns\n",
5027 __func__, source, period_ns);
5033 static int ni_set_master_clock(struct comedi_device *dev,
5034 unsigned int source, unsigned int period_ns)
5036 struct ni_private *devpriv = dev->private;
5038 if (source == NI_MIO_INTERNAL_CLOCK) {
5039 devpriv->rtsi_trig_direction_reg &= ~NISTC_RTSI_TRIG_USE_CLK;
5040 ni_stc_writew(dev, devpriv->rtsi_trig_direction_reg,
5041 NISTC_RTSI_TRIG_DIR_REG);
5042 devpriv->clock_ns = TIMEBASE_1_NS;
5043 if (devpriv->is_m_series) {
5044 devpriv->clock_and_fout2 &=
5045 ~(NI_M_CLK_FOUT2_TIMEBASE1_PLL |
5046 NI_M_CLK_FOUT2_TIMEBASE3_PLL);
5047 ni_writew(dev, devpriv->clock_and_fout2,
5048 NI_M_CLK_FOUT2_REG);
5049 ni_writew(dev, 0, NI_M_PLL_CTRL_REG);
5051 devpriv->clock_source = source;
5053 if (devpriv->is_m_series) {
5054 return ni_mseries_set_pll_master_clock(dev, source,
5057 if (source == NI_MIO_RTSI_CLOCK) {
5058 devpriv->rtsi_trig_direction_reg |=
5059 NISTC_RTSI_TRIG_USE_CLK;
5061 devpriv->rtsi_trig_direction_reg,
5062 NISTC_RTSI_TRIG_DIR_REG);
5063 if (period_ns == 0) {
5064 dev_err(dev->class_dev,
5065 "we don't handle an unspecified clock period correctly yet, returning error\n");
5068 devpriv->clock_ns = period_ns;
5069 devpriv->clock_source = source;
5078 static int ni_valid_rtsi_output_source(struct comedi_device *dev,
5079 unsigned int chan, unsigned int source)
5081 struct ni_private *devpriv = dev->private;
5083 if (chan >= NISTC_RTSI_TRIG_NUM_CHAN(devpriv->is_m_series)) {
5084 if (chan == NISTC_RTSI_TRIG_OLD_CLK_CHAN) {
5085 if (source == NI_RTSI_OUTPUT_RTSI_OSC)
5088 dev_err(dev->class_dev,
5089 "%s: invalid source for channel=%i, channel %i is always the RTSI clock for pre-m-series boards\n",
5090 __func__, chan, NISTC_RTSI_TRIG_OLD_CLK_CHAN);
5096 case NI_RTSI_OUTPUT_ADR_START1:
5097 case NI_RTSI_OUTPUT_ADR_START2:
5098 case NI_RTSI_OUTPUT_SCLKG:
5099 case NI_RTSI_OUTPUT_DACUPDN:
5100 case NI_RTSI_OUTPUT_DA_START1:
5101 case NI_RTSI_OUTPUT_G_SRC0:
5102 case NI_RTSI_OUTPUT_G_GATE0:
5103 case NI_RTSI_OUTPUT_RGOUT0:
5104 case NI_RTSI_OUTPUT_RTSI_BRD(0):
5105 case NI_RTSI_OUTPUT_RTSI_BRD(1):
5106 case NI_RTSI_OUTPUT_RTSI_BRD(2):
5107 case NI_RTSI_OUTPUT_RTSI_BRD(3):
5109 case NI_RTSI_OUTPUT_RTSI_OSC:
5110 return (devpriv->is_m_series) ? 1 : 0;
5116 static int ni_set_rtsi_routing(struct comedi_device *dev,
5117 unsigned int chan, unsigned int src)
5119 struct ni_private *devpriv = dev->private;
5121 if (chan >= TRIGGER_LINE(0))
5122 /* allow new and old names of rtsi channels to work. */
5123 chan -= TRIGGER_LINE(0);
5125 if (ni_valid_rtsi_output_source(dev, chan, src) == 0)
5128 devpriv->rtsi_trig_a_output_reg &= ~NISTC_RTSI_TRIG_MASK(chan);
5129 devpriv->rtsi_trig_a_output_reg |= NISTC_RTSI_TRIG(chan, src);
5130 ni_stc_writew(dev, devpriv->rtsi_trig_a_output_reg,
5131 NISTC_RTSI_TRIGA_OUT_REG);
5132 } else if (chan < NISTC_RTSI_TRIG_NUM_CHAN(devpriv->is_m_series)) {
5133 devpriv->rtsi_trig_b_output_reg &= ~NISTC_RTSI_TRIG_MASK(chan);
5134 devpriv->rtsi_trig_b_output_reg |= NISTC_RTSI_TRIG(chan, src);
5135 ni_stc_writew(dev, devpriv->rtsi_trig_b_output_reg,
5136 NISTC_RTSI_TRIGB_OUT_REG);
5137 } else if (chan != NISTC_RTSI_TRIG_OLD_CLK_CHAN) {
5138 /* probably should never reach this, since the
5139 * ni_valid_rtsi_output_source above errors out if chan is too
5142 dev_err(dev->class_dev, "%s: unknown rtsi channel\n", __func__);
5148 static unsigned int ni_get_rtsi_routing(struct comedi_device *dev,
5151 struct ni_private *devpriv = dev->private;
5153 if (chan >= TRIGGER_LINE(0))
5154 /* allow new and old names of rtsi channels to work. */
5155 chan -= TRIGGER_LINE(0);
5158 return NISTC_RTSI_TRIG_TO_SRC(chan,
5159 devpriv->rtsi_trig_a_output_reg);
5160 } else if (chan < NISTC_RTSI_TRIG_NUM_CHAN(devpriv->is_m_series)) {
5161 return NISTC_RTSI_TRIG_TO_SRC(chan,
5162 devpriv->rtsi_trig_b_output_reg);
5163 } else if (chan == NISTC_RTSI_TRIG_OLD_CLK_CHAN) {
5164 return NI_RTSI_OUTPUT_RTSI_OSC;
5167 dev_err(dev->class_dev, "%s: unknown rtsi channel\n", __func__);
5171 static void ni_set_rtsi_direction(struct comedi_device *dev, int chan,
5172 unsigned int direction)
5174 struct ni_private *devpriv = dev->private;
5175 unsigned int max_chan = NISTC_RTSI_TRIG_NUM_CHAN(devpriv->is_m_series);
5177 if (chan >= TRIGGER_LINE(0))
5178 /* allow new and old names of rtsi channels to work. */
5179 chan -= TRIGGER_LINE(0);
5181 if (direction == COMEDI_OUTPUT) {
5182 if (chan < max_chan) {
5183 devpriv->rtsi_trig_direction_reg |=
5184 NISTC_RTSI_TRIG_DIR(chan, devpriv->is_m_series);
5185 } else if (chan == NISTC_RTSI_TRIG_OLD_CLK_CHAN) {
5186 devpriv->rtsi_trig_direction_reg |=
5187 NISTC_RTSI_TRIG_DRV_CLK;
5190 if (chan < max_chan) {
5191 devpriv->rtsi_trig_direction_reg &=
5192 ~NISTC_RTSI_TRIG_DIR(chan, devpriv->is_m_series);
5193 } else if (chan == NISTC_RTSI_TRIG_OLD_CLK_CHAN) {
5194 devpriv->rtsi_trig_direction_reg &=
5195 ~NISTC_RTSI_TRIG_DRV_CLK;
5198 ni_stc_writew(dev, devpriv->rtsi_trig_direction_reg,
5199 NISTC_RTSI_TRIG_DIR_REG);
5202 static int ni_get_rtsi_direction(struct comedi_device *dev, int chan)
5204 struct ni_private *devpriv = dev->private;
5205 unsigned int max_chan = NISTC_RTSI_TRIG_NUM_CHAN(devpriv->is_m_series);
5207 if (chan >= TRIGGER_LINE(0))
5208 /* allow new and old names of rtsi channels to work. */
5209 chan -= TRIGGER_LINE(0);
5211 if (chan < max_chan) {
5212 return (devpriv->rtsi_trig_direction_reg &
5213 NISTC_RTSI_TRIG_DIR(chan, devpriv->is_m_series))
5214 ? COMEDI_OUTPUT : COMEDI_INPUT;
5215 } else if (chan == NISTC_RTSI_TRIG_OLD_CLK_CHAN) {
5216 return (devpriv->rtsi_trig_direction_reg &
5217 NISTC_RTSI_TRIG_DRV_CLK)
5218 ? COMEDI_OUTPUT : COMEDI_INPUT;
5223 static int ni_rtsi_insn_config(struct comedi_device *dev,
5224 struct comedi_subdevice *s,
5225 struct comedi_insn *insn,
5228 struct ni_private *devpriv = dev->private;
5229 unsigned int chan = CR_CHAN(insn->chanspec);
5234 ni_set_rtsi_direction(dev, chan, data[0]);
5236 case INSN_CONFIG_DIO_QUERY: {
5237 int ret = ni_get_rtsi_direction(dev, chan);
5244 case INSN_CONFIG_SET_CLOCK_SRC:
5245 return ni_set_master_clock(dev, data[1], data[2]);
5246 case INSN_CONFIG_GET_CLOCK_SRC:
5247 data[1] = devpriv->clock_source;
5248 data[2] = devpriv->clock_ns;
5250 case INSN_CONFIG_SET_ROUTING:
5251 return ni_set_rtsi_routing(dev, chan, data[1]);
5252 case INSN_CONFIG_GET_ROUTING: {
5253 int ret = ni_get_rtsi_routing(dev, chan);
5266 static int ni_rtsi_insn_bits(struct comedi_device *dev,
5267 struct comedi_subdevice *s,
5268 struct comedi_insn *insn,
5277 * Default routing for RTSI trigger lines.
5279 * These values are used here in the init function, as well as in the
5280 * disconnect_route function, after a RTSI route has been disconnected.
5282 static const int default_rtsi_routing[] = {
5283 [0] = NI_RTSI_OUTPUT_ADR_START1,
5284 [1] = NI_RTSI_OUTPUT_ADR_START2,
5285 [2] = NI_RTSI_OUTPUT_SCLKG,
5286 [3] = NI_RTSI_OUTPUT_DACUPDN,
5287 [4] = NI_RTSI_OUTPUT_DA_START1,
5288 [5] = NI_RTSI_OUTPUT_G_SRC0,
5289 [6] = NI_RTSI_OUTPUT_G_GATE0,
5290 [7] = NI_RTSI_OUTPUT_RTSI_OSC,
5294 * Route signals through RGOUT0 terminal.
5295 * @reg: raw register value of RGOUT0 bits (only bit0 is important).
5296 * @dev: comedi device handle.
5298 static void set_rgout0_reg(int reg, struct comedi_device *dev)
5300 struct ni_private *devpriv = dev->private;
5302 if (devpriv->is_m_series) {
5303 devpriv->rtsi_trig_direction_reg &=
5304 ~NISTC_RTSI_TRIG_DIR_SUB_SEL1;
5305 devpriv->rtsi_trig_direction_reg |=
5306 (reg << NISTC_RTSI_TRIG_DIR_SUB_SEL1_SHIFT) &
5307 NISTC_RTSI_TRIG_DIR_SUB_SEL1;
5308 ni_stc_writew(dev, devpriv->rtsi_trig_direction_reg,
5309 NISTC_RTSI_TRIG_DIR_REG);
5311 devpriv->rtsi_trig_b_output_reg &= ~NISTC_RTSI_TRIGB_SUB_SEL1;
5312 devpriv->rtsi_trig_b_output_reg |=
5313 (reg << NISTC_RTSI_TRIGB_SUB_SEL1_SHIFT) &
5314 NISTC_RTSI_TRIGB_SUB_SEL1;
5315 ni_stc_writew(dev, devpriv->rtsi_trig_b_output_reg,
5316 NISTC_RTSI_TRIGB_OUT_REG);
5320 static int get_rgout0_reg(struct comedi_device *dev)
5322 struct ni_private *devpriv = dev->private;
5325 if (devpriv->is_m_series)
5326 reg = (devpriv->rtsi_trig_direction_reg &
5327 NISTC_RTSI_TRIG_DIR_SUB_SEL1)
5328 >> NISTC_RTSI_TRIG_DIR_SUB_SEL1_SHIFT;
5330 reg = (devpriv->rtsi_trig_b_output_reg &
5331 NISTC_RTSI_TRIGB_SUB_SEL1)
5332 >> NISTC_RTSI_TRIGB_SUB_SEL1_SHIFT;
5336 static inline int get_rgout0_src(struct comedi_device *dev)
5338 struct ni_private *devpriv = dev->private;
5339 int reg = get_rgout0_reg(dev);
5341 return ni_find_route_source(reg, NI_RGOUT0, &devpriv->routing_tables);
5345 * Route signals through RGOUT0 terminal and increment the RGOUT0 use for this
5347 * @src: device-global signal name
5348 * @dev: comedi device handle
5350 * Return: -EINVAL if the source is not valid to route to RGOUT0;
5351 * -EBUSY if the RGOUT0 is already used;
5354 static int incr_rgout0_src_use(int src, struct comedi_device *dev)
5356 struct ni_private *devpriv = dev->private;
5357 s8 reg = ni_lookup_route_register(CR_CHAN(src), NI_RGOUT0,
5358 &devpriv->routing_tables);
5363 if (devpriv->rgout0_usage > 0 && get_rgout0_reg(dev) != reg)
5366 ++devpriv->rgout0_usage;
5367 set_rgout0_reg(reg, dev);
5372 * Unroute signals through RGOUT0 terminal and deccrement the RGOUT0 use for
5373 * this particular source. This function does not actually unroute anything
5374 * with respect to RGOUT0. It does, on the other hand, decrement the usage
5375 * counter for the current src->RGOUT0 mapping.
5377 * Return: -EINVAL if the source is not already routed to RGOUT0 (or usage is
5378 * already at zero); 0 if successful.
5380 static int decr_rgout0_src_use(int src, struct comedi_device *dev)
5382 struct ni_private *devpriv = dev->private;
5383 s8 reg = ni_lookup_route_register(CR_CHAN(src), NI_RGOUT0,
5384 &devpriv->routing_tables);
5386 if (devpriv->rgout0_usage > 0 && get_rgout0_reg(dev) == reg) {
5387 --devpriv->rgout0_usage;
5388 if (!devpriv->rgout0_usage)
5389 set_rgout0_reg(0, dev); /* ok default? */
5396 * Route signals through given NI_RTSI_BRD mux.
5397 * @i: index of mux to route
5398 * @reg: raw register value of RTSI_BRD bits
5399 * @dev: comedi device handle
5401 static void set_ith_rtsi_brd_reg(int i, int reg, struct comedi_device *dev)
5403 struct ni_private *devpriv = dev->private;
5404 int reg_i_sz = 3; /* value for e-series */
5408 if (devpriv->is_m_series)
5410 reg_i_mask = ~((~0) << reg_i_sz);
5411 reg_i_shift = i * reg_i_sz;
5413 /* clear out the current reg_i for ith brd */
5414 devpriv->rtsi_shared_mux_reg &= ~(reg_i_mask << reg_i_shift);
5415 /* (softcopy) write the new reg_i for ith brd */
5416 devpriv->rtsi_shared_mux_reg |= (reg & reg_i_mask) << reg_i_shift;
5417 /* (hardcopy) write the new reg_i for ith brd */
5418 ni_stc_writew(dev, devpriv->rtsi_shared_mux_reg, NISTC_RTSI_BOARD_REG);
5421 static int get_ith_rtsi_brd_reg(int i, struct comedi_device *dev)
5423 struct ni_private *devpriv = dev->private;
5424 int reg_i_sz = 3; /* value for e-series */
5428 if (devpriv->is_m_series)
5430 reg_i_mask = ~((~0) << reg_i_sz);
5431 reg_i_shift = i * reg_i_sz;
5433 return (devpriv->rtsi_shared_mux_reg >> reg_i_shift) & reg_i_mask;
5436 static inline int get_rtsi_brd_src(int brd, struct comedi_device *dev)
5438 struct ni_private *devpriv = dev->private;
5439 int brd_index = brd;
5442 if (brd >= NI_RTSI_BRD(0))
5443 brd_index = brd - NI_RTSI_BRD(0);
5445 brd = NI_RTSI_BRD(brd);
5448 * brd : device-global name
5449 * brd_index : index number of RTSI_BRD mux
5452 reg = get_ith_rtsi_brd_reg(brd_index, dev);
5454 return ni_find_route_source(reg, brd, &devpriv->routing_tables);
5458 * Route signals through NI_RTSI_BRD mux and increment the use counter for this
5461 * Return: -EINVAL if the source is not valid to route to NI_RTSI_BRD(i);
5462 * -EBUSY if all NI_RTSI_BRD muxes are already used;
5463 * NI_RTSI_BRD(i) of allocated ith mux if successful.
5465 static int incr_rtsi_brd_src_use(int src, struct comedi_device *dev)
5467 struct ni_private *devpriv = dev->private;
5468 int first_available = -1;
5473 /* first look for a mux that is already configured to provide src */
5474 for (i = 0; i < NUM_RTSI_SHARED_MUXS; ++i) {
5475 reg = ni_lookup_route_register(CR_CHAN(src), NI_RTSI_BRD(i),
5476 &devpriv->routing_tables);
5479 continue; /* invalid route */
5481 if (!devpriv->rtsi_shared_mux_usage[i]) {
5482 if (first_available < 0)
5483 /* found the first unused, but usable mux */
5484 first_available = i;
5487 * we've seen at least one possible route, so change the
5488 * final error to -EBUSY in case there are no muxes
5493 if (get_ith_rtsi_brd_reg(i, dev) == reg) {
5495 * we've found a mux that is already being used
5496 * to provide the requested signal. Reuse it.
5503 if (first_available < 0)
5506 /* we did not find a mux to reuse, but there is at least one usable */
5507 i = first_available;
5510 ++devpriv->rtsi_shared_mux_usage[i];
5511 set_ith_rtsi_brd_reg(i, reg, dev);
5512 return NI_RTSI_BRD(i);
5516 * Unroute signals through NI_RTSI_BRD mux and decrement the user counter for
5517 * this particular route.
5519 * Return: -EINVAL if the source is not already routed to rtsi_brd(i) (or usage
5520 * is already at zero); 0 if successful.
5522 static int decr_rtsi_brd_src_use(int src, int rtsi_brd,
5523 struct comedi_device *dev)
5525 struct ni_private *devpriv = dev->private;
5526 s8 reg = ni_lookup_route_register(CR_CHAN(src), rtsi_brd,
5527 &devpriv->routing_tables);
5528 const int i = rtsi_brd - NI_RTSI_BRD(0);
5530 if (devpriv->rtsi_shared_mux_usage[i] > 0 &&
5531 get_ith_rtsi_brd_reg(i, dev) == reg) {
5532 --devpriv->rtsi_shared_mux_usage[i];
5533 if (!devpriv->rtsi_shared_mux_usage[i])
5534 set_ith_rtsi_brd_reg(i, 0, dev); /* ok default? */
5541 static void ni_rtsi_init(struct comedi_device *dev)
5543 struct ni_private *devpriv = dev->private;
5546 /* Initialises the RTSI bus signal switch to a default state */
5549 * Use 10MHz instead of 20MHz for RTSI clock frequency. Appears
5550 * to have no effect, at least on pxi-6281, which always uses
5551 * 20MHz rtsi clock frequency
5553 devpriv->clock_and_fout2 = NI_M_CLK_FOUT2_RTSI_10MHZ;
5554 /* Set clock mode to internal */
5555 if (ni_set_master_clock(dev, NI_MIO_INTERNAL_CLOCK, 0) < 0)
5556 dev_err(dev->class_dev, "ni_set_master_clock failed, bug?\n");
5558 /* default internal lines routing to RTSI bus lines */
5559 for (i = 0; i < 8; ++i) {
5560 ni_set_rtsi_direction(dev, i, COMEDI_INPUT);
5561 ni_set_rtsi_routing(dev, i, default_rtsi_routing[i]);
5565 * Sets the source and direction of the 4 on board lines.
5566 * This configures all board lines to be:
5568 * 1) inputs (not sure what "output" would mean)
5569 * 2) copying TRIGGER_LINE(0) (or RTSI0) output
5571 * copying NI_PFI(0) output
5573 devpriv->rtsi_shared_mux_reg = 0;
5574 for (i = 0; i < 4; ++i)
5575 set_ith_rtsi_brd_reg(i, 0, dev);
5576 memset(devpriv->rtsi_shared_mux_usage, 0,
5577 sizeof(devpriv->rtsi_shared_mux_usage));
5579 /* initialize rgout0 pin as unused. */
5580 devpriv->rgout0_usage = 0;
5581 set_rgout0_reg(0, dev);
5584 /* Get route of GPFO_i/CtrOut pins */
5585 static inline int ni_get_gout_routing(unsigned int dest,
5586 struct comedi_device *dev)
5588 struct ni_private *devpriv = dev->private;
5589 unsigned int reg = devpriv->an_trig_etc_reg;
5593 if (reg & NISTC_ATRIG_ETC_GPFO_0_ENA)
5594 return NISTC_ATRIG_ETC_GPFO_0_SEL_TO_SRC(reg);
5597 if (reg & NISTC_ATRIG_ETC_GPFO_1_ENA)
5598 return NISTC_ATRIG_ETC_GPFO_1_SEL_TO_SRC(reg);
5605 /* Set route of GPFO_i/CtrOut pins */
5606 static inline int ni_disable_gout_routing(unsigned int dest,
5607 struct comedi_device *dev)
5609 struct ni_private *devpriv = dev->private;
5613 devpriv->an_trig_etc_reg &= ~NISTC_ATRIG_ETC_GPFO_0_ENA;
5616 devpriv->an_trig_etc_reg &= ~NISTC_ATRIG_ETC_GPFO_1_ENA;
5622 ni_stc_writew(dev, devpriv->an_trig_etc_reg, NISTC_ATRIG_ETC_REG);
5626 /* Set route of GPFO_i/CtrOut pins */
5627 static inline int ni_set_gout_routing(unsigned int src, unsigned int dest,
5628 struct comedi_device *dev)
5630 struct ni_private *devpriv = dev->private;
5635 devpriv->an_trig_etc_reg &= ~NISTC_ATRIG_ETC_GPFO_0_SEL(-1);
5637 devpriv->an_trig_etc_reg |= NISTC_ATRIG_ETC_GPFO_0_ENA
5638 | NISTC_ATRIG_ETC_GPFO_0_SEL(src);
5642 devpriv->an_trig_etc_reg &= ~NISTC_ATRIG_ETC_GPFO_1_SEL;
5643 src = src ? NISTC_ATRIG_ETC_GPFO_1_SEL : 0;
5645 devpriv->an_trig_etc_reg |= NISTC_ATRIG_ETC_GPFO_1_ENA | src;
5651 ni_stc_writew(dev, devpriv->an_trig_etc_reg, NISTC_ATRIG_ETC_REG);
5656 * Retrieves the current source of the output selector for the given
5657 * destination. If the terminal for the destination is not already configured
5658 * as an output, this function returns -EINVAL as error.
5660 * Return: the register value of the destination output selector;
5661 * -EINVAL if terminal is not configured for output.
5663 static int get_output_select_source(int dest, struct comedi_device *dev)
5665 struct ni_private *devpriv = dev->private;
5668 if (channel_is_pfi(dest)) {
5669 if (ni_get_pfi_direction(dev, dest) == COMEDI_OUTPUT)
5670 reg = ni_get_pfi_routing(dev, dest);
5671 } else if (channel_is_rtsi(dest)) {
5672 if (ni_get_rtsi_direction(dev, dest) == COMEDI_OUTPUT) {
5673 reg = ni_get_rtsi_routing(dev, dest);
5675 if (reg == NI_RTSI_OUTPUT_RGOUT0) {
5676 dest = NI_RGOUT0; /* prepare for lookup below */
5677 reg = get_rgout0_reg(dev);
5678 } else if (reg >= NI_RTSI_OUTPUT_RTSI_BRD(0) &&
5679 reg <= NI_RTSI_OUTPUT_RTSI_BRD(3)) {
5680 const int i = reg - NI_RTSI_OUTPUT_RTSI_BRD(0);
5682 dest = NI_RTSI_BRD(i); /* prepare for lookup */
5683 reg = get_ith_rtsi_brd_reg(i, dev);
5686 } else if (dest >= NI_CtrOut(0) && dest <= NI_CtrOut(-1)) {
5688 * not handled by ni_tio. Only available for GPFO registers in
5691 dest -= NI_CtrOut(0);
5693 /* there are only two g_out outputs. */
5695 reg = ni_get_gout_routing(dest, dev);
5696 } else if (channel_is_ctr(dest)) {
5697 reg = ni_tio_get_routing(devpriv->counter_dev, dest);
5699 dev_dbg(dev->class_dev, "%s: unhandled destination (%d) queried\n",
5704 return ni_find_route_source(CR_CHAN(reg), dest,
5705 &devpriv->routing_tables);
5712 * Return: -1 if not connectible;
5713 * 0 if connectible and not connected;
5714 * 1 if connectible and connected.
5716 static int test_route(unsigned int src, unsigned int dest,
5717 struct comedi_device *dev)
5719 struct ni_private *devpriv = dev->private;
5720 s8 reg = ni_route_to_register(CR_CHAN(src), dest,
5721 &devpriv->routing_tables);
5725 if (get_output_select_source(dest, dev) != CR_CHAN(src))
5730 /* Connect the actual route. */
5731 static int connect_route(unsigned int src, unsigned int dest,
5732 struct comedi_device *dev)
5734 struct ni_private *devpriv = dev->private;
5735 s8 reg = ni_route_to_register(CR_CHAN(src), dest,
5736 &devpriv->routing_tables);
5740 /* route is not valid */
5743 current_src = get_output_select_source(dest, dev);
5744 if (current_src == CR_CHAN(src))
5746 if (current_src >= 0)
5747 /* destination mux is already busy. complain, don't overwrite */
5750 /* The route is valid and available. Now connect... */
5751 if (channel_is_pfi(dest)) {
5752 /* set routing source, then open output */
5753 ni_set_pfi_routing(dev, dest, reg);
5754 ni_set_pfi_direction(dev, dest, COMEDI_OUTPUT);
5755 } else if (channel_is_rtsi(dest)) {
5756 if (reg == NI_RTSI_OUTPUT_RGOUT0) {
5757 int ret = incr_rgout0_src_use(src, dev);
5761 } else if (ni_rtsi_route_requires_mux(reg)) {
5762 /* Attempt to allocate and route (src->brd) */
5763 int brd = incr_rtsi_brd_src_use(src, dev);
5768 /* Now lookup the register value for (brd->dest) */
5769 reg = ni_lookup_route_register(
5770 brd, dest, &devpriv->routing_tables);
5773 ni_set_rtsi_direction(dev, dest, COMEDI_OUTPUT);
5774 ni_set_rtsi_routing(dev, dest, reg);
5775 } else if (dest >= NI_CtrOut(0) && dest <= NI_CtrOut(-1)) {
5777 * not handled by ni_tio. Only available for GPFO registers in
5780 dest -= NI_CtrOut(0);
5782 /* there are only two g_out outputs. */
5784 if (ni_set_gout_routing(src, dest, dev))
5786 } else if (channel_is_ctr(dest)) {
5788 * we are adding back the channel modifier info to set
5789 * invert/edge info passed by the user
5791 ni_tio_set_routing(devpriv->counter_dev, dest,
5792 reg | (src & ~CR_CHAN(-1)));
5799 static int disconnect_route(unsigned int src, unsigned int dest,
5800 struct comedi_device *dev)
5802 struct ni_private *devpriv = dev->private;
5803 s8 reg = ni_route_to_register(CR_CHAN(src), dest,
5804 &devpriv->routing_tables);
5807 /* route is not valid */
5809 if (get_output_select_source(dest, dev) != src)
5810 /* cannot disconnect something not connected */
5813 /* The route is valid and is connected. Now disconnect... */
5814 if (channel_is_pfi(dest)) {
5815 /* set the pfi to high impedance, and disconnect */
5816 ni_set_pfi_direction(dev, dest, COMEDI_INPUT);
5817 ni_set_pfi_routing(dev, dest, NI_PFI_OUTPUT_PFI_DEFAULT);
5818 } else if (channel_is_rtsi(dest)) {
5819 if (reg == NI_RTSI_OUTPUT_RGOUT0) {
5820 int ret = decr_rgout0_src_use(src, dev);
5824 } else if (ni_rtsi_route_requires_mux(reg)) {
5825 /* find which RTSI_BRD line is source for rtsi pin */
5826 int brd = ni_find_route_source(
5827 ni_get_rtsi_routing(dev, dest), dest,
5828 &devpriv->routing_tables);
5833 /* decrement/disconnect RTSI_BRD line from source */
5834 decr_rtsi_brd_src_use(src, brd, dev);
5837 /* set rtsi output selector to default state */
5838 reg = default_rtsi_routing[dest - TRIGGER_LINE(0)];
5839 ni_set_rtsi_direction(dev, dest, COMEDI_INPUT);
5840 ni_set_rtsi_routing(dev, dest, reg);
5841 } else if (dest >= NI_CtrOut(0) && dest <= NI_CtrOut(-1)) {
5843 * not handled by ni_tio. Only available for GPFO registers in
5846 dest -= NI_CtrOut(0);
5848 /* there are only two g_out outputs. */
5850 reg = ni_disable_gout_routing(dest, dev);
5851 } else if (channel_is_ctr(dest)) {
5852 ni_tio_unset_routing(devpriv->counter_dev, dest);
5859 static int ni_global_insn_config(struct comedi_device *dev,
5860 struct comedi_insn *insn,
5864 case INSN_DEVICE_CONFIG_TEST_ROUTE:
5865 data[0] = test_route(data[1], data[2], dev);
5867 case INSN_DEVICE_CONFIG_CONNECT_ROUTE:
5868 return connect_route(data[1], data[2], dev);
5869 case INSN_DEVICE_CONFIG_DISCONNECT_ROUTE:
5870 return disconnect_route(data[1], data[2], dev);
5872 * This case is already handled one level up.
5873 * case INSN_DEVICE_CONFIG_GET_ROUTES:
5882 static int ni_gpct_cmd(struct comedi_device *dev, struct comedi_subdevice *s)
5884 struct ni_gpct *counter = s->private;
5887 retval = ni_request_gpct_mite_channel(dev, counter->counter_index,
5890 dev_err(dev->class_dev,
5891 "no dma channel available for use by counter\n");
5894 ni_tio_acknowledge(counter);
5895 ni_e_series_enable_second_irq(dev, counter->counter_index, 1);
5897 return ni_tio_cmd(dev, s);
5900 static int ni_gpct_cancel(struct comedi_device *dev, struct comedi_subdevice *s)
5902 struct ni_gpct *counter = s->private;
5905 retval = ni_tio_cancel(counter);
5906 ni_e_series_enable_second_irq(dev, counter->counter_index, 0);
5907 ni_release_gpct_mite_channel(dev, counter->counter_index);
5912 static irqreturn_t ni_E_interrupt(int irq, void *d)
5914 struct comedi_device *dev = d;
5915 struct comedi_subdevice *s_ai = dev->read_subdev;
5916 struct comedi_subdevice *s_ao = dev->write_subdev;
5917 unsigned short a_status;
5918 unsigned short b_status;
5919 unsigned long flags;
5921 struct ni_private *devpriv = dev->private;
5926 smp_mb(); /* make sure dev->attached is checked */
5928 /* lock to avoid race with comedi_poll */
5929 spin_lock_irqsave(&dev->spinlock, flags);
5930 a_status = ni_stc_readw(dev, NISTC_AI_STATUS1_REG);
5931 b_status = ni_stc_readw(dev, NISTC_AO_STATUS1_REG);
5933 if (devpriv->mite) {
5934 unsigned long flags_too;
5936 spin_lock_irqsave(&devpriv->mite_channel_lock, flags_too);
5937 if (s_ai && devpriv->ai_mite_chan)
5938 mite_ack_linkc(devpriv->ai_mite_chan, s_ai, false);
5939 if (s_ao && devpriv->ao_mite_chan)
5940 mite_ack_linkc(devpriv->ao_mite_chan, s_ao, false);
5941 spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags_too);
5944 ack_a_interrupt(dev, a_status);
5945 ack_b_interrupt(dev, b_status);
5947 if (a_status & NISTC_AI_STATUS1_INTA)
5948 handle_a_interrupt(dev, s_ai, a_status);
5949 /* handle any interrupt or dma events */
5950 comedi_handle_events(dev, s_ai);
5953 if (b_status & NISTC_AO_STATUS1_INTB)
5954 handle_b_interrupt(dev, s_ao, b_status);
5955 /* handle any interrupt or dma events */
5956 comedi_handle_events(dev, s_ao);
5958 handle_gpct_interrupt(dev, 0);
5959 handle_gpct_interrupt(dev, 1);
5961 if (devpriv->is_m_series)
5962 handle_cdio_interrupt(dev);
5965 spin_unlock_irqrestore(&dev->spinlock, flags);
5969 static int ni_alloc_private(struct comedi_device *dev)
5971 struct ni_private *devpriv;
5973 devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
5977 spin_lock_init(&devpriv->window_lock);
5978 spin_lock_init(&devpriv->soft_reg_copy_lock);
5979 spin_lock_init(&devpriv->mite_channel_lock);
5984 static unsigned int _ni_get_valid_routes(struct comedi_device *dev,
5985 unsigned int n_pairs,
5986 unsigned int *pair_data)
5988 struct ni_private *devpriv = dev->private;
5990 return ni_get_valid_routes(&devpriv->routing_tables, n_pairs,
5994 static int ni_E_init(struct comedi_device *dev,
5995 unsigned int interrupt_pin, unsigned int irq_polarity)
5997 const struct ni_board_struct *board = dev->board_ptr;
5998 struct ni_private *devpriv = dev->private;
5999 struct comedi_subdevice *s;
6002 const char *dev_family = devpriv->is_m_series ? "ni_mseries"
6004 if (!IS_PCIMIO != !dev->mmio) {
6005 dev_err(dev->class_dev,
6006 "%s: bug! %s device not supported.\n",
6007 KBUILD_MODNAME, board->name);
6011 /* prepare the device for globally-named routes. */
6012 if (ni_assign_device_routes(dev_family, board->name,
6013 board->alt_route_name,
6014 &devpriv->routing_tables) < 0) {
6015 dev_warn(dev->class_dev, "%s: %s device has no signal routing table.\n",
6016 __func__, board->name);
6017 dev_warn(dev->class_dev, "%s: High level NI signal names will not be available for this %s board.\n",
6018 __func__, board->name);
6021 * only(?) assign insn_device_config if we have global names for
6024 dev->insn_device_config = ni_global_insn_config;
6025 dev->get_valid_routes = _ni_get_valid_routes;
6028 if (board->n_aochan > MAX_N_AO_CHAN) {
6029 dev_err(dev->class_dev, "bug! n_aochan > MAX_N_AO_CHAN\n");
6033 /* initialize clock dividers */
6034 devpriv->clock_and_fout = NISTC_CLK_FOUT_SLOW_DIV2 |
6035 NISTC_CLK_FOUT_SLOW_TIMEBASE |
6036 NISTC_CLK_FOUT_TO_BOARD_DIV2 |
6037 NISTC_CLK_FOUT_TO_BOARD;
6038 if (!devpriv->is_6xxx) {
6039 /* BEAM is this needed for PCI-6143 ?? */
6040 devpriv->clock_and_fout |= (NISTC_CLK_FOUT_AI_OUT_DIV2 |
6041 NISTC_CLK_FOUT_AO_OUT_DIV2);
6043 ni_stc_writew(dev, devpriv->clock_and_fout, NISTC_CLK_FOUT_REG);
6045 ret = comedi_alloc_subdevices(dev, NI_NUM_SUBDEVICES);
6049 /* Analog Input subdevice */
6050 s = &dev->subdevices[NI_AI_SUBDEV];
6051 if (board->n_adchan) {
6052 s->type = COMEDI_SUBD_AI;
6053 s->subdev_flags = SDF_READABLE | SDF_DIFF | SDF_DITHER;
6054 if (!devpriv->is_611x)
6055 s->subdev_flags |= SDF_GROUND | SDF_COMMON | SDF_OTHER;
6056 if (board->ai_maxdata > 0xffff)
6057 s->subdev_flags |= SDF_LSAMPL;
6058 if (devpriv->is_m_series)
6059 s->subdev_flags |= SDF_SOFT_CALIBRATED;
6060 s->n_chan = board->n_adchan;
6061 s->maxdata = board->ai_maxdata;
6062 s->range_table = ni_range_lkup[board->gainlkup];
6063 s->insn_read = ni_ai_insn_read;
6064 s->insn_config = ni_ai_insn_config;
6066 dev->read_subdev = s;
6067 s->subdev_flags |= SDF_CMD_READ;
6068 s->len_chanlist = 512;
6069 s->do_cmdtest = ni_ai_cmdtest;
6070 s->do_cmd = ni_ai_cmd;
6071 s->cancel = ni_ai_reset;
6072 s->poll = ni_ai_poll;
6073 s->munge = ni_ai_munge;
6076 s->async_dma_dir = DMA_FROM_DEVICE;
6079 /* reset the analog input configuration */
6080 ni_ai_reset(dev, s);
6082 s->type = COMEDI_SUBD_UNUSED;
6085 /* Analog Output subdevice */
6086 s = &dev->subdevices[NI_AO_SUBDEV];
6087 if (board->n_aochan) {
6088 s->type = COMEDI_SUBD_AO;
6089 s->subdev_flags = SDF_WRITABLE | SDF_DEGLITCH | SDF_GROUND;
6090 if (devpriv->is_m_series)
6091 s->subdev_flags |= SDF_SOFT_CALIBRATED;
6092 s->n_chan = board->n_aochan;
6093 s->maxdata = board->ao_maxdata;
6094 s->range_table = board->ao_range_table;
6095 s->insn_config = ni_ao_insn_config;
6096 s->insn_write = ni_ao_insn_write;
6098 ret = comedi_alloc_subdev_readback(s);
6103 * Along with the IRQ we need either a FIFO or DMA for
6104 * async command support.
6106 if (dev->irq && (board->ao_fifo_depth || devpriv->mite)) {
6107 dev->write_subdev = s;
6108 s->subdev_flags |= SDF_CMD_WRITE;
6109 s->len_chanlist = s->n_chan;
6110 s->do_cmdtest = ni_ao_cmdtest;
6111 s->do_cmd = ni_ao_cmd;
6112 s->cancel = ni_ao_reset;
6113 if (!devpriv->is_m_series)
6114 s->munge = ni_ao_munge;
6117 s->async_dma_dir = DMA_TO_DEVICE;
6120 if (devpriv->is_67xx)
6121 init_ao_67xx(dev, s);
6123 /* reset the analog output configuration */
6124 ni_ao_reset(dev, s);
6126 s->type = COMEDI_SUBD_UNUSED;
6129 /* Digital I/O subdevice */
6130 s = &dev->subdevices[NI_DIO_SUBDEV];
6131 s->type = COMEDI_SUBD_DIO;
6132 s->subdev_flags = SDF_WRITABLE | SDF_READABLE;
6133 s->n_chan = board->has_32dio_chan ? 32 : 8;
6135 s->range_table = &range_digital;
6136 if (devpriv->is_m_series) {
6138 s->subdev_flags |= SDF_LSAMPL;
6139 s->insn_bits = ni_m_series_dio_insn_bits;
6140 s->insn_config = ni_m_series_dio_insn_config;
6142 s->subdev_flags |= SDF_CMD_WRITE /* | SDF_CMD_READ */;
6143 s->len_chanlist = s->n_chan;
6144 s->do_cmdtest = ni_cdio_cmdtest;
6145 s->do_cmd = ni_cdio_cmd;
6146 s->cancel = ni_cdio_cancel;
6148 /* M-series boards use DMA */
6149 s->async_dma_dir = DMA_BIDIRECTIONAL;
6152 /* reset DIO and set all channels to inputs */
6153 ni_writel(dev, NI_M_CDO_CMD_RESET |
6156 ni_writel(dev, s->io_bits, NI_M_DIO_DIR_REG);
6159 s->insn_bits = ni_dio_insn_bits;
6160 s->insn_config = ni_dio_insn_config;
6162 /* set all channels to inputs */
6163 devpriv->dio_control = NISTC_DIO_CTRL_DIR(s->io_bits);
6164 ni_writew(dev, devpriv->dio_control, NISTC_DIO_CTRL_REG);
6168 s = &dev->subdevices[NI_8255_DIO_SUBDEV];
6169 if (board->has_8255) {
6170 ret = subdev_8255_cb_init(dev, s, ni_8255_callback,
6175 s->type = COMEDI_SUBD_UNUSED;
6178 /* formerly general purpose counter/timer device, but no longer used */
6179 s = &dev->subdevices[NI_UNUSED_SUBDEV];
6180 s->type = COMEDI_SUBD_UNUSED;
6182 /* Calibration subdevice */
6183 s = &dev->subdevices[NI_CALIBRATION_SUBDEV];
6184 s->type = COMEDI_SUBD_CALIB;
6185 s->subdev_flags = SDF_INTERNAL;
6188 if (devpriv->is_m_series) {
6189 /* internal PWM output used for AI nonlinearity calibration */
6190 s->insn_config = ni_m_series_pwm_config;
6192 ni_writel(dev, 0x0, NI_M_CAL_PWM_REG);
6193 } else if (devpriv->is_6143) {
6194 /* internal PWM output used for AI nonlinearity calibration */
6195 s->insn_config = ni_6143_pwm_config;
6197 s->subdev_flags |= SDF_WRITABLE;
6198 s->insn_read = ni_calib_insn_read;
6199 s->insn_write = ni_calib_insn_write;
6201 /* setup the caldacs and find the real n_chan and maxdata */
6202 caldac_setup(dev, s);
6205 /* EEPROM subdevice */
6206 s = &dev->subdevices[NI_EEPROM_SUBDEV];
6207 s->type = COMEDI_SUBD_MEMORY;
6208 s->subdev_flags = SDF_READABLE | SDF_INTERNAL;
6210 if (devpriv->is_m_series) {
6211 s->n_chan = M_SERIES_EEPROM_SIZE;
6212 s->insn_read = ni_m_series_eeprom_insn_read;
6215 s->insn_read = ni_eeprom_insn_read;
6218 /* Digital I/O (PFI) subdevice */
6219 s = &dev->subdevices[NI_PFI_DIO_SUBDEV];
6220 s->type = COMEDI_SUBD_DIO;
6222 if (devpriv->is_m_series) {
6224 s->insn_bits = ni_pfi_insn_bits;
6225 s->subdev_flags = SDF_READABLE | SDF_WRITABLE | SDF_INTERNAL;
6227 ni_writew(dev, s->state, NI_M_PFI_DO_REG);
6228 for (i = 0; i < NUM_PFI_OUTPUT_SELECT_REGS; ++i) {
6229 ni_writew(dev, devpriv->pfi_output_select_reg[i],
6230 NI_M_PFI_OUT_SEL_REG(i));
6234 s->subdev_flags = SDF_INTERNAL;
6236 s->insn_config = ni_pfi_insn_config;
6238 ni_set_bits(dev, NISTC_IO_BIDIR_PIN_REG, ~0, 0);
6240 /* cs5529 calibration adc */
6241 s = &dev->subdevices[NI_CS5529_CALIBRATION_SUBDEV];
6242 if (devpriv->is_67xx) {
6243 s->type = COMEDI_SUBD_AI;
6244 s->subdev_flags = SDF_READABLE | SDF_DIFF | SDF_INTERNAL;
6245 /* one channel for each analog output channel */
6246 s->n_chan = board->n_aochan;
6247 s->maxdata = BIT(16) - 1;
6248 s->range_table = &range_unknown; /* XXX */
6249 s->insn_read = cs5529_ai_insn_read;
6250 s->insn_config = NULL;
6253 s->type = COMEDI_SUBD_UNUSED;
6257 s = &dev->subdevices[NI_SERIAL_SUBDEV];
6258 s->type = COMEDI_SUBD_SERIAL;
6259 s->subdev_flags = SDF_READABLE | SDF_WRITABLE | SDF_INTERNAL;
6262 s->insn_config = ni_serial_insn_config;
6263 devpriv->serial_interval_ns = 0;
6264 devpriv->serial_hw_mode = 0;
6267 s = &dev->subdevices[NI_RTSI_SUBDEV];
6268 s->type = COMEDI_SUBD_DIO;
6269 s->subdev_flags = SDF_READABLE | SDF_WRITABLE | SDF_INTERNAL;
6272 s->insn_bits = ni_rtsi_insn_bits;
6273 s->insn_config = ni_rtsi_insn_config;
6276 /* allocate and initialize the gpct counter device */
6277 devpriv->counter_dev = ni_gpct_device_construct(dev,
6278 ni_gpct_write_register,
6279 ni_gpct_read_register,
6280 (devpriv->is_m_series)
6281 ? ni_gpct_variant_m_series
6282 : ni_gpct_variant_e_series,
6285 &devpriv->routing_tables);
6286 if (!devpriv->counter_dev)
6289 /* Counter (gpct) subdevices */
6290 for (i = 0; i < NUM_GPCT; ++i) {
6291 struct ni_gpct *gpct = &devpriv->counter_dev->counters[i];
6293 /* setup and initialize the counter */
6294 ni_tio_init_counter(gpct);
6296 s = &dev->subdevices[NI_GPCT_SUBDEV(i)];
6297 s->type = COMEDI_SUBD_COUNTER;
6298 s->subdev_flags = SDF_READABLE | SDF_WRITABLE | SDF_LSAMPL;
6300 s->maxdata = (devpriv->is_m_series) ? 0xffffffff
6302 s->insn_read = ni_tio_insn_read;
6303 s->insn_write = ni_tio_insn_write;
6304 s->insn_config = ni_tio_insn_config;
6306 if (dev->irq && devpriv->mite) {
6307 s->subdev_flags |= SDF_CMD_READ /* | SDF_CMD_WRITE */;
6308 s->len_chanlist = 1;
6309 s->do_cmdtest = ni_tio_cmdtest;
6310 s->do_cmd = ni_gpct_cmd;
6311 s->cancel = ni_gpct_cancel;
6313 s->async_dma_dir = DMA_BIDIRECTIONAL;
6319 /* Initialize GPFO_{0,1} to produce output of counters */
6320 ni_set_gout_routing(0, 0, dev); /* output of counter 0; DAQ STC, p338 */
6321 ni_set_gout_routing(0, 1, dev); /* output of counter 1; DAQ STC, p338 */
6323 /* Frequency output subdevice */
6324 s = &dev->subdevices[NI_FREQ_OUT_SUBDEV];
6325 s->type = COMEDI_SUBD_COUNTER;
6326 s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
6329 s->insn_read = ni_freq_out_insn_read;
6330 s->insn_write = ni_freq_out_insn_write;
6331 s->insn_config = ni_freq_out_insn_config;
6335 (irq_polarity ? NISTC_INT_CTRL_INT_POL : 0) |
6336 (NISTC_INT_CTRL_3PIN_INT & 0) |
6337 NISTC_INT_CTRL_INTA_ENA |
6338 NISTC_INT_CTRL_INTB_ENA |
6339 NISTC_INT_CTRL_INTA_SEL(interrupt_pin) |
6340 NISTC_INT_CTRL_INTB_SEL(interrupt_pin),
6341 NISTC_INT_CTRL_REG);
6345 ni_writeb(dev, devpriv->ai_ao_select_reg, NI_E_DMA_AI_AO_SEL_REG);
6346 ni_writeb(dev, devpriv->g0_g1_select_reg, NI_E_DMA_G0_G1_SEL_REG);
6348 if (devpriv->is_6xxx) {
6349 ni_writeb(dev, 0, NI611X_MAGIC_REG);
6350 } else if (devpriv->is_m_series) {
6353 for (channel = 0; channel < board->n_aochan; ++channel) {
6355 NI_M_AO_WAVEFORM_ORDER_REG(channel));
6357 NI_M_AO_REF_ATTENUATION_REG(channel));
6359 ni_writeb(dev, 0x0, NI_M_AO_CALIB_REG);
6365 static void mio_common_detach(struct comedi_device *dev)
6367 struct ni_private *devpriv = dev->private;
6370 ni_gpct_device_destroy(devpriv->counter_dev);