2 comedi/drivers/ni_mio_common.c
3 Hardware driver for DAQ-STC based boards
5 COMEDI - Linux Control and Measurement Device Interface
6 Copyright (C) 1997-2001 David A. Schleef <ds@schleef.org>
7 Copyright (C) 2002-2006 Frank Mori Hess <fmhess@users.sourceforge.net>
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
21 This file is meant to be included by another file, e.g.,
22 ni_atmio.c or ni_pcimio.c.
24 Interrupt support originally added by Truxton Fulton
27 References (from ftp://ftp.natinst.com/support/manuals):
29 340747b.pdf AT-MIO E series Register Level Programmer Manual
30 341079b.pdf PCI E Series RLPM
31 340934b.pdf DAQ-STC reference manual
32 67xx and 611x registers (from ftp://ftp.ni.com/support/daq/mhddk/documentation/)
35 Other possibly relevant info:
37 320517c.pdf User manual (obsolete)
38 320517f.pdf User manual (new)
40 320906c.pdf maximum signal ratings
42 321791a.pdf discontinuation of at-mio-16e-10 rev. c
43 321808a.pdf about at-mio-16e-10 rev P
44 321837a.pdf discontinuation of at-mio-16de-10 rev d
45 321838a.pdf about at-mio-16de-10 rev N
49 - the interrupt routine needs to be cleaned up
51 2006-02-07: S-Series PCI-6143: Support has been added but is not
52 fully tested as yet. Terry Barnaby, BEAM Ltd.
55 #include <linux/interrupt.h>
56 #include <linux/sched.h>
57 #include <linux/delay.h>
62 #define NI_TIMEOUT 1000
64 /* Note: this table must match the ai_gain_* definitions */
65 static const short ni_gainlkup[][16] = {
66 [ai_gain_16] = {0, 1, 2, 3, 4, 5, 6, 7,
67 0x100, 0x101, 0x102, 0x103, 0x104, 0x105, 0x106, 0x107},
68 [ai_gain_8] = {1, 2, 4, 7, 0x101, 0x102, 0x104, 0x107},
69 [ai_gain_14] = {1, 2, 3, 4, 5, 6, 7,
70 0x101, 0x102, 0x103, 0x104, 0x105, 0x106, 0x107},
71 [ai_gain_4] = {0, 1, 4, 7},
72 [ai_gain_611x] = {0x00a, 0x00b, 0x001, 0x002,
73 0x003, 0x004, 0x005, 0x006},
74 [ai_gain_622x] = {0, 1, 4, 5},
75 [ai_gain_628x] = {1, 2, 3, 4, 5, 6, 7},
76 [ai_gain_6143] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
79 static const struct comedi_lrange range_ni_E_ai = {
100 static const struct comedi_lrange range_ni_E_ai_limited = {
113 static const struct comedi_lrange range_ni_E_ai_limited14 = {
132 static const struct comedi_lrange range_ni_E_ai_bipolar4 = {
141 static const struct comedi_lrange range_ni_E_ai_611x = {
154 static const struct comedi_lrange range_ni_M_ai_622x = {
163 static const struct comedi_lrange range_ni_M_ai_628x = {
175 static const struct comedi_lrange range_ni_E_ao_ext = {
184 static const struct comedi_lrange *const ni_range_lkup[] = {
185 [ai_gain_16] = &range_ni_E_ai,
186 [ai_gain_8] = &range_ni_E_ai_limited,
187 [ai_gain_14] = &range_ni_E_ai_limited14,
188 [ai_gain_4] = &range_ni_E_ai_bipolar4,
189 [ai_gain_611x] = &range_ni_E_ai_611x,
190 [ai_gain_622x] = &range_ni_M_ai_622x,
191 [ai_gain_628x] = &range_ni_M_ai_628x,
192 [ai_gain_6143] = &range_bipolar5
197 AIMODE_HALF_FULL = 1,
202 enum ni_common_subdevices {
208 NI_CALIBRATION_SUBDEV,
211 NI_CS5529_CALIBRATION_SUBDEV,
219 static inline unsigned NI_GPCT_SUBDEV(unsigned counter_index)
221 switch (counter_index) {
223 return NI_GPCT0_SUBDEV;
225 return NI_GPCT1_SUBDEV;
230 return NI_GPCT0_SUBDEV;
233 enum timebase_nanoseconds {
235 TIMEBASE_2_NS = 10000
238 #define SERIAL_DISABLED 0
239 #define SERIAL_600NS 600
240 #define SERIAL_1_2US 1200
241 #define SERIAL_10US 10000
243 static const int num_adc_stages_611x = 3;
245 static void ni_writel(struct comedi_device *dev, uint32_t data, int reg)
248 writel(data, dev->mmio + reg);
250 outl(data, dev->iobase + reg);
253 static void ni_writew(struct comedi_device *dev, uint16_t data, int reg)
256 writew(data, dev->mmio + reg);
258 outw(data, dev->iobase + reg);
261 static void ni_writeb(struct comedi_device *dev, uint8_t data, int reg)
264 writeb(data, dev->mmio + reg);
266 outb(data, dev->iobase + reg);
269 static uint32_t ni_readl(struct comedi_device *dev, int reg)
272 return readl(dev->mmio + reg);
274 return inl(dev->iobase + reg);
277 static uint16_t ni_readw(struct comedi_device *dev, int reg)
280 return readw(dev->mmio + reg);
282 return inw(dev->iobase + reg);
285 static uint8_t ni_readb(struct comedi_device *dev, int reg)
288 return readb(dev->mmio + reg);
290 return inb(dev->iobase + reg);
294 * We automatically take advantage of STC registers that can be
295 * read/written directly in the I/O space of the board.
297 * The AT-MIO and DAQCard devices map the low 8 STC registers to
300 * Most PCIMIO devices also map the low 8 STC registers but the
301 * 611x devices map the read registers to iobase+(addr-1)*2.
302 * For now non-windowed STC access is disabled if a PCIMIO device
303 * is detected (devpriv->mite has been initialized).
305 * The M series devices do not used windowed registers for the
306 * STC registers. The functions below handle the mapping of the
307 * windowed STC registers to the m series register offsets.
311 unsigned int mio_reg;
315 static const struct mio_regmap m_series_stc_write_regmap[] = {
316 [NISTC_INTA_ACK_REG] = { 0x104, 2 },
317 [NISTC_INTB_ACK_REG] = { 0x106, 2 },
318 [NISTC_AI_CMD2_REG] = { 0x108, 2 },
319 [NISTC_AO_CMD2_REG] = { 0x10a, 2 },
320 [NISTC_G0_CMD_REG] = { 0x10c, 2 },
321 [NISTC_G1_CMD_REG] = { 0x10e, 2 },
322 [NISTC_AI_CMD1_REG] = { 0x110, 2 },
323 [NISTC_AO_CMD1_REG] = { 0x112, 2 },
325 * NISTC_DIO_OUT_REG maps to:
326 * { NI_M_DIO_REG, 4 } and { NI_M_SCXI_SER_DO_REG, 1 }
328 [NISTC_DIO_OUT_REG] = { 0, 0 }, /* DOES NOT MAP CLEANLY */
329 [NISTC_DIO_CTRL_REG] = { 0, 0 }, /* DOES NOT MAP CLEANLY */
330 [NISTC_AI_MODE1_REG] = { 0x118, 2 },
331 [NISTC_AI_MODE2_REG] = { 0x11a, 2 },
332 [NISTC_AI_SI_LOADA_REG] = { 0x11c, 4 },
333 [NISTC_AI_SI_LOADB_REG] = { 0x120, 4 },
334 [NISTC_AI_SC_LOADA_REG] = { 0x124, 4 },
335 [NISTC_AI_SC_LOADB_REG] = { 0x128, 4 },
336 [NISTC_AI_SI2_LOADA_REG] = { 0x12c, 4 },
337 [NISTC_AI_SI2_LOADB_REG] = { 0x130, 4 },
338 [NISTC_G0_MODE_REG] = { 0x134, 2 },
339 [NISTC_G1_MODE_REG] = { 0x136, 2 },
340 [NISTC_G0_LOADA_REG] = { 0x138, 4 },
341 [NISTC_G0_LOADB_REG] = { 0x13c, 4 },
342 [NISTC_G1_LOADA_REG] = { 0x140, 4 },
343 [NISTC_G1_LOADB_REG] = { 0x144, 4 },
344 [NISTC_G0_INPUT_SEL_REG] = { 0x148, 2 },
345 [NISTC_G1_INPUT_SEL_REG] = { 0x14a, 2 },
346 [NISTC_AO_MODE1_REG] = { 0x14c, 2 },
347 [NISTC_AO_MODE2_REG] = { 0x14e, 2 },
348 [NISTC_AO_UI_LOADA_REG] = { 0x150, 4 },
349 [NISTC_AO_UI_LOADB_REG] = { 0x154, 4 },
350 [NISTC_AO_BC_LOADA_REG] = { 0x158, 4 },
351 [NISTC_AO_BC_LOADB_REG] = { 0x15c, 4 },
352 [NISTC_AO_UC_LOADA_REG] = { 0x160, 4 },
353 [NISTC_AO_UC_LOADB_REG] = { 0x164, 4 },
354 [NISTC_CLK_FOUT_REG] = { 0x170, 2 },
355 [NISTC_IO_BIDIR_PIN_REG] = { 0x172, 2 },
356 [NISTC_RTSI_TRIG_DIR_REG] = { 0x174, 2 },
357 [NISTC_INT_CTRL_REG] = { 0x176, 2 },
358 [NISTC_AI_OUT_CTRL_REG] = { 0x178, 2 },
359 [NISTC_ATRIG_ETC_REG] = { 0x17a, 2 },
360 [NISTC_AI_START_STOP_REG] = { 0x17c, 2 },
361 [NISTC_AI_TRIG_SEL_REG] = { 0x17e, 2 },
362 [NISTC_AI_DIV_LOADA_REG] = { 0x180, 4 },
363 [NISTC_AO_START_SEL_REG] = { 0x184, 2 },
364 [NISTC_AO_TRIG_SEL_REG] = { 0x186, 2 },
365 [NISTC_G0_AUTOINC_REG] = { 0x188, 2 },
366 [NISTC_G1_AUTOINC_REG] = { 0x18a, 2 },
367 [NISTC_AO_MODE3_REG] = { 0x18c, 2 },
368 [NISTC_RESET_REG] = { 0x190, 2 },
369 [NISTC_INTA_ENA_REG] = { 0x192, 2 },
370 [NISTC_INTA2_ENA_REG] = { 0, 0 }, /* E-Series only */
371 [NISTC_INTB_ENA_REG] = { 0x196, 2 },
372 [NISTC_INTB2_ENA_REG] = { 0, 0 }, /* E-Series only */
373 [NISTC_AI_PERSONAL_REG] = { 0x19a, 2 },
374 [NISTC_AO_PERSONAL_REG] = { 0x19c, 2 },
375 [NISTC_RTSI_TRIGA_OUT_REG] = { 0x19e, 2 },
376 [NISTC_RTSI_TRIGB_OUT_REG] = { 0x1a0, 2 },
377 [NISTC_RTSI_BOARD_REG] = { 0, 0 }, /* Unknown */
378 [NISTC_CFG_MEM_CLR_REG] = { 0x1a4, 2 },
379 [NISTC_ADC_FIFO_CLR_REG] = { 0x1a6, 2 },
380 [NISTC_DAC_FIFO_CLR_REG] = { 0x1a8, 2 },
381 [NISTC_AO_OUT_CTRL_REG] = { 0x1ac, 2 },
382 [NISTC_AI_MODE3_REG] = { 0x1ae, 2 },
385 static void m_series_stc_write(struct comedi_device *dev,
386 unsigned int data, unsigned int reg)
388 const struct mio_regmap *regmap;
390 if (reg < ARRAY_SIZE(m_series_stc_write_regmap)) {
391 regmap = &m_series_stc_write_regmap[reg];
393 dev_warn(dev->class_dev, "%s: unhandled register=0x%x\n",
398 switch (regmap->size) {
400 ni_writel(dev, data, regmap->mio_reg);
403 ni_writew(dev, data, regmap->mio_reg);
406 dev_warn(dev->class_dev, "%s: unmapped register=0x%x\n",
412 static const struct mio_regmap m_series_stc_read_regmap[] = {
413 [NISTC_AI_STATUS1_REG] = { 0x104, 2 },
414 [NISTC_AO_STATUS1_REG] = { 0x106, 2 },
415 [NISTC_G01_STATUS_REG] = { 0x108, 2 },
416 [NISTC_AI_STATUS2_REG] = { 0, 0 }, /* Unknown */
417 [NISTC_AO_STATUS2_REG] = { 0x10c, 2 },
418 [NISTC_DIO_IN_REG] = { 0, 0 }, /* Unknown */
419 [NISTC_G0_HW_SAVE_REG] = { 0x110, 4 },
420 [NISTC_G1_HW_SAVE_REG] = { 0x114, 4 },
421 [NISTC_G0_SAVE_REG] = { 0x118, 4 },
422 [NISTC_G1_SAVE_REG] = { 0x11c, 4 },
423 [NISTC_AO_UI_SAVE_REG] = { 0x120, 4 },
424 [NISTC_AO_BC_SAVE_REG] = { 0x124, 4 },
425 [NISTC_AO_UC_SAVE_REG] = { 0x128, 4 },
426 [NISTC_STATUS1_REG] = { 0x136, 2 },
427 [NISTC_DIO_SERIAL_IN_REG] = { 0x009, 1 },
428 [NISTC_STATUS2_REG] = { 0x13a, 2 },
429 [NISTC_AI_SI_SAVE_REG] = { 0x180, 4 },
430 [NISTC_AI_SC_SAVE_REG] = { 0x184, 4 },
433 static unsigned int m_series_stc_read(struct comedi_device *dev,
436 const struct mio_regmap *regmap;
438 if (reg < ARRAY_SIZE(m_series_stc_read_regmap)) {
439 regmap = &m_series_stc_read_regmap[reg];
441 dev_warn(dev->class_dev, "%s: unhandled register=0x%x\n",
446 switch (regmap->size) {
448 return ni_readl(dev, regmap->mio_reg);
450 return ni_readw(dev, regmap->mio_reg);
452 return ni_readb(dev, regmap->mio_reg);
454 dev_warn(dev->class_dev, "%s: unmapped register=0x%x\n",
460 static void ni_stc_writew(struct comedi_device *dev, uint16_t data, int reg)
462 struct ni_private *devpriv = dev->private;
465 if (devpriv->is_m_series) {
466 m_series_stc_write(dev, data, reg);
468 spin_lock_irqsave(&devpriv->window_lock, flags);
469 if (!devpriv->mite && reg < 8) {
470 ni_writew(dev, data, reg * 2);
472 ni_writew(dev, reg, NI_E_STC_WINDOW_ADDR_REG);
473 ni_writew(dev, data, NI_E_STC_WINDOW_DATA_REG);
475 spin_unlock_irqrestore(&devpriv->window_lock, flags);
479 static void ni_stc_writel(struct comedi_device *dev, uint32_t data, int reg)
481 struct ni_private *devpriv = dev->private;
483 if (devpriv->is_m_series) {
484 m_series_stc_write(dev, data, reg);
486 ni_stc_writew(dev, data >> 16, reg);
487 ni_stc_writew(dev, data & 0xffff, reg + 1);
491 static uint16_t ni_stc_readw(struct comedi_device *dev, int reg)
493 struct ni_private *devpriv = dev->private;
497 if (devpriv->is_m_series) {
498 val = m_series_stc_read(dev, reg);
500 spin_lock_irqsave(&devpriv->window_lock, flags);
501 if (!devpriv->mite && reg < 8) {
502 val = ni_readw(dev, reg * 2);
504 ni_writew(dev, reg, NI_E_STC_WINDOW_ADDR_REG);
505 val = ni_readw(dev, NI_E_STC_WINDOW_DATA_REG);
507 spin_unlock_irqrestore(&devpriv->window_lock, flags);
512 static uint32_t ni_stc_readl(struct comedi_device *dev, int reg)
514 struct ni_private *devpriv = dev->private;
517 if (devpriv->is_m_series) {
518 val = m_series_stc_read(dev, reg);
520 val = ni_stc_readw(dev, reg) << 16;
521 val |= ni_stc_readw(dev, reg + 1);
526 static inline void ni_set_bitfield(struct comedi_device *dev, int reg,
527 unsigned bit_mask, unsigned bit_values)
529 struct ni_private *devpriv = dev->private;
532 spin_lock_irqsave(&devpriv->soft_reg_copy_lock, flags);
534 case NISTC_INTA_ENA_REG:
535 devpriv->int_a_enable_reg &= ~bit_mask;
536 devpriv->int_a_enable_reg |= bit_values & bit_mask;
537 ni_stc_writew(dev, devpriv->int_a_enable_reg, reg);
539 case NISTC_INTB_ENA_REG:
540 devpriv->int_b_enable_reg &= ~bit_mask;
541 devpriv->int_b_enable_reg |= bit_values & bit_mask;
542 ni_stc_writew(dev, devpriv->int_b_enable_reg, reg);
544 case NISTC_IO_BIDIR_PIN_REG:
545 devpriv->io_bidirection_pin_reg &= ~bit_mask;
546 devpriv->io_bidirection_pin_reg |= bit_values & bit_mask;
547 ni_stc_writew(dev, devpriv->io_bidirection_pin_reg, reg);
549 case NI_E_DMA_AI_AO_SEL_REG:
550 devpriv->ai_ao_select_reg &= ~bit_mask;
551 devpriv->ai_ao_select_reg |= bit_values & bit_mask;
552 ni_writeb(dev, devpriv->ai_ao_select_reg, reg);
554 case NI_E_DMA_G0_G1_SEL_REG:
555 devpriv->g0_g1_select_reg &= ~bit_mask;
556 devpriv->g0_g1_select_reg |= bit_values & bit_mask;
557 ni_writeb(dev, devpriv->g0_g1_select_reg, reg);
560 dev_err(dev->class_dev, "called with invalid register %d\n",
565 spin_unlock_irqrestore(&devpriv->soft_reg_copy_lock, flags);
569 /* DMA channel setup */
570 static inline unsigned ni_stc_dma_channel_select_bitfield(unsigned channel)
582 static inline void ni_set_ai_dma_channel(struct comedi_device *dev,
585 unsigned bits = ni_stc_dma_channel_select_bitfield(channel);
587 ni_set_bitfield(dev, NI_E_DMA_AI_AO_SEL_REG,
588 NI_E_DMA_AI_SEL_MASK, NI_E_DMA_AI_SEL(bits));
591 static inline void ni_set_ai_dma_no_channel(struct comedi_device *dev)
593 ni_set_bitfield(dev, NI_E_DMA_AI_AO_SEL_REG, NI_E_DMA_AI_SEL_MASK, 0);
596 static inline void ni_set_ao_dma_channel(struct comedi_device *dev,
599 unsigned bits = ni_stc_dma_channel_select_bitfield(channel);
601 ni_set_bitfield(dev, NI_E_DMA_AI_AO_SEL_REG,
602 NI_E_DMA_AO_SEL_MASK, NI_E_DMA_AO_SEL(bits));
605 static inline void ni_set_ao_dma_no_channel(struct comedi_device *dev)
607 ni_set_bitfield(dev, NI_E_DMA_AI_AO_SEL_REG, NI_E_DMA_AO_SEL_MASK, 0);
610 static inline void ni_set_gpct_dma_channel(struct comedi_device *dev,
614 unsigned bits = ni_stc_dma_channel_select_bitfield(channel);
616 ni_set_bitfield(dev, NI_E_DMA_G0_G1_SEL_REG,
617 NI_E_DMA_G0_G1_SEL_MASK(gpct_index),
618 NI_E_DMA_G0_G1_SEL(gpct_index, bits));
621 static inline void ni_set_gpct_dma_no_channel(struct comedi_device *dev,
624 ni_set_bitfield(dev, NI_E_DMA_G0_G1_SEL_REG,
625 NI_E_DMA_G0_G1_SEL_MASK(gpct_index), 0);
628 static inline void ni_set_cdo_dma_channel(struct comedi_device *dev,
629 unsigned mite_channel)
631 struct ni_private *devpriv = dev->private;
635 spin_lock_irqsave(&devpriv->soft_reg_copy_lock, flags);
636 devpriv->cdio_dma_select_reg &= ~NI_M_CDIO_DMA_SEL_CDO_MASK;
638 * XXX just guessing ni_stc_dma_channel_select_bitfield()
639 * returns the right bits, under the assumption the cdio dma
640 * selection works just like ai/ao/gpct.
641 * Definitely works for dma channels 0 and 1.
643 bits = ni_stc_dma_channel_select_bitfield(mite_channel);
644 devpriv->cdio_dma_select_reg |= NI_M_CDIO_DMA_SEL_CDO(bits);
645 ni_writeb(dev, devpriv->cdio_dma_select_reg, NI_M_CDIO_DMA_SEL_REG);
647 spin_unlock_irqrestore(&devpriv->soft_reg_copy_lock, flags);
650 static inline void ni_set_cdo_dma_no_channel(struct comedi_device *dev)
652 struct ni_private *devpriv = dev->private;
655 spin_lock_irqsave(&devpriv->soft_reg_copy_lock, flags);
656 devpriv->cdio_dma_select_reg &= ~NI_M_CDIO_DMA_SEL_CDO_MASK;
657 ni_writeb(dev, devpriv->cdio_dma_select_reg, NI_M_CDIO_DMA_SEL_REG);
659 spin_unlock_irqrestore(&devpriv->soft_reg_copy_lock, flags);
662 static int ni_request_ai_mite_channel(struct comedi_device *dev)
664 struct ni_private *devpriv = dev->private;
667 spin_lock_irqsave(&devpriv->mite_channel_lock, flags);
668 BUG_ON(devpriv->ai_mite_chan);
669 devpriv->ai_mite_chan =
670 mite_request_channel(devpriv->mite, devpriv->ai_mite_ring);
671 if (!devpriv->ai_mite_chan) {
672 spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
673 dev_err(dev->class_dev,
674 "failed to reserve mite dma channel for analog input\n");
677 devpriv->ai_mite_chan->dir = COMEDI_INPUT;
678 ni_set_ai_dma_channel(dev, devpriv->ai_mite_chan->channel);
679 spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
683 static int ni_request_ao_mite_channel(struct comedi_device *dev)
685 struct ni_private *devpriv = dev->private;
688 spin_lock_irqsave(&devpriv->mite_channel_lock, flags);
689 BUG_ON(devpriv->ao_mite_chan);
690 devpriv->ao_mite_chan =
691 mite_request_channel(devpriv->mite, devpriv->ao_mite_ring);
692 if (!devpriv->ao_mite_chan) {
693 spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
694 dev_err(dev->class_dev,
695 "failed to reserve mite dma channel for analog outut\n");
698 devpriv->ao_mite_chan->dir = COMEDI_OUTPUT;
699 ni_set_ao_dma_channel(dev, devpriv->ao_mite_chan->channel);
700 spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
704 static int ni_request_gpct_mite_channel(struct comedi_device *dev,
706 enum comedi_io_direction direction)
708 struct ni_private *devpriv = dev->private;
710 struct mite_channel *mite_chan;
712 spin_lock_irqsave(&devpriv->mite_channel_lock, flags);
713 BUG_ON(devpriv->counter_dev->counters[gpct_index].mite_chan);
715 mite_request_channel(devpriv->mite,
716 devpriv->gpct_mite_ring[gpct_index]);
718 spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
719 dev_err(dev->class_dev,
720 "failed to reserve mite dma channel for counter\n");
723 mite_chan->dir = direction;
724 ni_tio_set_mite_channel(&devpriv->counter_dev->counters[gpct_index],
726 ni_set_gpct_dma_channel(dev, gpct_index, mite_chan->channel);
727 spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
733 static int ni_request_cdo_mite_channel(struct comedi_device *dev)
736 struct ni_private *devpriv = dev->private;
739 spin_lock_irqsave(&devpriv->mite_channel_lock, flags);
740 BUG_ON(devpriv->cdo_mite_chan);
741 devpriv->cdo_mite_chan =
742 mite_request_channel(devpriv->mite, devpriv->cdo_mite_ring);
743 if (!devpriv->cdo_mite_chan) {
744 spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
745 dev_err(dev->class_dev,
746 "failed to reserve mite dma channel for correlated digital output\n");
749 devpriv->cdo_mite_chan->dir = COMEDI_OUTPUT;
750 ni_set_cdo_dma_channel(dev, devpriv->cdo_mite_chan->channel);
751 spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
756 static void ni_release_ai_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->ai_mite_chan) {
764 ni_set_ai_dma_no_channel(dev);
765 mite_release_channel(devpriv->ai_mite_chan);
766 devpriv->ai_mite_chan = NULL;
768 spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
772 static void ni_release_ao_mite_channel(struct comedi_device *dev)
775 struct ni_private *devpriv = dev->private;
778 spin_lock_irqsave(&devpriv->mite_channel_lock, flags);
779 if (devpriv->ao_mite_chan) {
780 ni_set_ao_dma_no_channel(dev);
781 mite_release_channel(devpriv->ao_mite_chan);
782 devpriv->ao_mite_chan = NULL;
784 spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
789 static void ni_release_gpct_mite_channel(struct comedi_device *dev,
792 struct ni_private *devpriv = dev->private;
795 spin_lock_irqsave(&devpriv->mite_channel_lock, flags);
796 if (devpriv->counter_dev->counters[gpct_index].mite_chan) {
797 struct mite_channel *mite_chan =
798 devpriv->counter_dev->counters[gpct_index].mite_chan;
800 ni_set_gpct_dma_no_channel(dev, gpct_index);
801 ni_tio_set_mite_channel(&devpriv->
802 counter_dev->counters[gpct_index],
804 mite_release_channel(mite_chan);
806 spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
810 static void ni_release_cdo_mite_channel(struct comedi_device *dev)
813 struct ni_private *devpriv = dev->private;
816 spin_lock_irqsave(&devpriv->mite_channel_lock, flags);
817 if (devpriv->cdo_mite_chan) {
818 ni_set_cdo_dma_no_channel(dev);
819 mite_release_channel(devpriv->cdo_mite_chan);
820 devpriv->cdo_mite_chan = NULL;
822 spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
827 static void ni_e_series_enable_second_irq(struct comedi_device *dev,
828 unsigned gpct_index, short enable)
830 struct ni_private *devpriv = dev->private;
834 if (devpriv->is_m_series || gpct_index > 1)
838 * e-series boards use the second irq signals to generate
839 * dma requests for their counters
841 if (gpct_index == 0) {
842 reg = NISTC_INTA2_ENA_REG;
844 val = NISTC_INTA_ENA_G0_GATE;
846 reg = NISTC_INTB2_ENA_REG;
848 val = NISTC_INTB_ENA_G1_GATE;
850 ni_stc_writew(dev, val, reg);
854 static void ni_clear_ai_fifo(struct comedi_device *dev)
856 struct ni_private *devpriv = dev->private;
857 static const int timeout = 10000;
860 if (devpriv->is_6143) {
861 /* Flush the 6143 data FIFO */
862 ni_writel(dev, 0x10, NI6143_AI_FIFO_CTRL_REG);
863 ni_writel(dev, 0x00, NI6143_AI_FIFO_CTRL_REG);
864 /* Wait for complete */
865 for (i = 0; i < timeout; i++) {
866 if (!(ni_readl(dev, NI6143_AI_FIFO_STATUS_REG) & 0x10))
871 dev_err(dev->class_dev, "FIFO flush timeout\n");
873 ni_stc_writew(dev, 1, NISTC_ADC_FIFO_CLR_REG);
874 if (devpriv->is_625x) {
875 ni_writeb(dev, 0, NI_M_STATIC_AI_CTRL_REG(0));
876 ni_writeb(dev, 1, NI_M_STATIC_AI_CTRL_REG(0));
878 /* the NI example code does 3 convert pulses for 625x boards,
879 but that appears to be wrong in practice. */
880 ni_stc_writew(dev, NISTC_AI_CMD1_CONVERT_PULSE,
882 ni_stc_writew(dev, NISTC_AI_CMD1_CONVERT_PULSE,
884 ni_stc_writew(dev, NISTC_AI_CMD1_CONVERT_PULSE,
891 static inline void ni_ao_win_outw(struct comedi_device *dev, uint16_t data,
894 struct ni_private *devpriv = dev->private;
897 spin_lock_irqsave(&devpriv->window_lock, flags);
898 ni_writew(dev, addr, NI611X_AO_WINDOW_ADDR_REG);
899 ni_writew(dev, data, NI611X_AO_WINDOW_DATA_REG);
900 spin_unlock_irqrestore(&devpriv->window_lock, flags);
903 static inline void ni_ao_win_outl(struct comedi_device *dev, uint32_t data,
906 struct ni_private *devpriv = dev->private;
909 spin_lock_irqsave(&devpriv->window_lock, flags);
910 ni_writew(dev, addr, NI611X_AO_WINDOW_ADDR_REG);
911 ni_writel(dev, data, NI611X_AO_WINDOW_DATA_REG);
912 spin_unlock_irqrestore(&devpriv->window_lock, flags);
915 static inline unsigned short ni_ao_win_inw(struct comedi_device *dev, int addr)
917 struct ni_private *devpriv = dev->private;
921 spin_lock_irqsave(&devpriv->window_lock, flags);
922 ni_writew(dev, addr, NI611X_AO_WINDOW_ADDR_REG);
923 data = ni_readw(dev, NI611X_AO_WINDOW_DATA_REG);
924 spin_unlock_irqrestore(&devpriv->window_lock, flags);
928 /* ni_set_bits( ) allows different parts of the ni_mio_common driver to
929 * share registers (such as Interrupt_A_Register) without interfering with
932 * NOTE: the switch/case statements are optimized out for a constant argument
933 * so this is actually quite fast--- If you must wrap another function around this
934 * make it inline to avoid a large speed penalty.
936 * value should only be 1 or 0.
938 static inline void ni_set_bits(struct comedi_device *dev, int reg,
939 unsigned bits, unsigned value)
947 ni_set_bitfield(dev, reg, bits, bit_values);
951 static void ni_sync_ai_dma(struct comedi_device *dev)
953 struct ni_private *devpriv = dev->private;
954 struct comedi_subdevice *s = dev->read_subdev;
957 spin_lock_irqsave(&devpriv->mite_channel_lock, flags);
958 if (devpriv->ai_mite_chan)
959 mite_sync_input_dma(devpriv->ai_mite_chan, s);
960 spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
963 static int ni_ai_drain_dma(struct comedi_device *dev)
965 struct ni_private *devpriv = dev->private;
967 static const int timeout = 10000;
971 spin_lock_irqsave(&devpriv->mite_channel_lock, flags);
972 if (devpriv->ai_mite_chan) {
973 for (i = 0; i < timeout; i++) {
974 if ((ni_stc_readw(dev, NISTC_AI_STATUS1_REG) &
975 NISTC_AI_STATUS1_FIFO_E)
976 && mite_bytes_in_transit(devpriv->ai_mite_chan) ==
982 dev_err(dev->class_dev, "timed out\n");
983 dev_err(dev->class_dev,
984 "mite_bytes_in_transit=%i, AI_Status1_Register=0x%x\n",
985 mite_bytes_in_transit(devpriv->ai_mite_chan),
986 ni_stc_readw(dev, NISTC_AI_STATUS1_REG));
990 spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
997 static void mite_handle_b_linkc(struct mite_struct *mite,
998 struct comedi_device *dev)
1000 struct ni_private *devpriv = dev->private;
1001 struct comedi_subdevice *s = dev->write_subdev;
1002 unsigned long flags;
1004 spin_lock_irqsave(&devpriv->mite_channel_lock, flags);
1005 if (devpriv->ao_mite_chan)
1006 mite_sync_output_dma(devpriv->ao_mite_chan, s);
1007 spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
1010 static int ni_ao_wait_for_dma_load(struct comedi_device *dev)
1012 static const int timeout = 10000;
1015 for (i = 0; i < timeout; i++) {
1016 unsigned short b_status;
1018 b_status = ni_stc_readw(dev, NISTC_AO_STATUS1_REG);
1019 if (b_status & NISTC_AO_STATUS1_FIFO_HF)
1021 /* if we poll too often, the pci bus activity seems
1022 to slow the dma transfer down */
1026 dev_err(dev->class_dev, "timed out waiting for dma load\n");
1035 static void ni_ao_fifo_load(struct comedi_device *dev,
1036 struct comedi_subdevice *s, int n)
1038 struct ni_private *devpriv = dev->private;
1043 for (i = 0; i < n; i++) {
1044 comedi_buf_read_samples(s, &d, 1);
1046 if (devpriv->is_6xxx) {
1047 packed_data = d & 0xffff;
1048 /* 6711 only has 16 bit wide ao fifo */
1049 if (!devpriv->is_6711) {
1050 comedi_buf_read_samples(s, &d, 1);
1052 packed_data |= (d << 16) & 0xffff0000;
1054 ni_writel(dev, packed_data, NI611X_AO_FIFO_DATA_REG);
1056 ni_writew(dev, d, NI_E_AO_FIFO_DATA_REG);
1062 * There's a small problem if the FIFO gets really low and we
1063 * don't have the data to fill it. Basically, if after we fill
1064 * the FIFO with all the data available, the FIFO is _still_
1065 * less than half full, we never clear the interrupt. If the
1066 * IRQ is in edge mode, we never get another interrupt, because
1067 * this one wasn't cleared. If in level mode, we get flooded
1068 * with interrupts that we can't fulfill, because nothing ever
1069 * gets put into the buffer.
1071 * This kind of situation is recoverable, but it is easier to
1072 * just pretend we had a FIFO underrun, since there is a good
1073 * chance it will happen anyway. This is _not_ the case for
1074 * RT code, as RT code might purposely be running close to the
1075 * metal. Needs to be fixed eventually.
1077 static int ni_ao_fifo_half_empty(struct comedi_device *dev,
1078 struct comedi_subdevice *s)
1080 const struct ni_board_struct *board = dev->board_ptr;
1081 unsigned int nbytes;
1082 unsigned int nsamples;
1084 nbytes = comedi_buf_read_n_available(s);
1086 s->async->events |= COMEDI_CB_OVERFLOW;
1090 nsamples = comedi_bytes_to_samples(s, nbytes);
1091 if (nsamples > board->ao_fifo_depth / 2)
1092 nsamples = board->ao_fifo_depth / 2;
1094 ni_ao_fifo_load(dev, s, nsamples);
1099 static int ni_ao_prep_fifo(struct comedi_device *dev,
1100 struct comedi_subdevice *s)
1102 const struct ni_board_struct *board = dev->board_ptr;
1103 struct ni_private *devpriv = dev->private;
1104 unsigned int nbytes;
1105 unsigned int nsamples;
1108 ni_stc_writew(dev, 1, NISTC_DAC_FIFO_CLR_REG);
1109 if (devpriv->is_6xxx)
1110 ni_ao_win_outl(dev, 0x6, NI611X_AO_FIFO_OFFSET_LOAD_REG);
1112 /* load some data */
1113 nbytes = comedi_buf_read_n_available(s);
1117 nsamples = comedi_bytes_to_samples(s, nbytes);
1118 if (nsamples > board->ao_fifo_depth)
1119 nsamples = board->ao_fifo_depth;
1121 ni_ao_fifo_load(dev, s, nsamples);
1126 static void ni_ai_fifo_read(struct comedi_device *dev,
1127 struct comedi_subdevice *s, int n)
1129 struct ni_private *devpriv = dev->private;
1130 struct comedi_async *async = s->async;
1132 unsigned short data;
1135 if (devpriv->is_611x) {
1136 for (i = 0; i < n / 2; i++) {
1137 dl = ni_readl(dev, NI611X_AI_FIFO_DATA_REG);
1138 /* This may get the hi/lo data in the wrong order */
1139 data = (dl >> 16) & 0xffff;
1140 comedi_buf_write_samples(s, &data, 1);
1142 comedi_buf_write_samples(s, &data, 1);
1144 /* Check if there's a single sample stuck in the FIFO */
1146 dl = ni_readl(dev, NI611X_AI_FIFO_DATA_REG);
1148 comedi_buf_write_samples(s, &data, 1);
1150 } else if (devpriv->is_6143) {
1151 /* This just reads the FIFO assuming the data is present, no checks on the FIFO status are performed */
1152 for (i = 0; i < n / 2; i++) {
1153 dl = ni_readl(dev, NI6143_AI_FIFO_DATA_REG);
1155 data = (dl >> 16) & 0xffff;
1156 comedi_buf_write_samples(s, &data, 1);
1158 comedi_buf_write_samples(s, &data, 1);
1161 /* Assume there is a single sample stuck in the FIFO */
1162 /* Get stranded sample into FIFO */
1163 ni_writel(dev, 0x01, NI6143_AI_FIFO_CTRL_REG);
1164 dl = ni_readl(dev, NI6143_AI_FIFO_DATA_REG);
1165 data = (dl >> 16) & 0xffff;
1166 comedi_buf_write_samples(s, &data, 1);
1169 if (n > ARRAY_SIZE(devpriv->ai_fifo_buffer)) {
1170 dev_err(dev->class_dev,
1171 "bug! ai_fifo_buffer too small\n");
1172 async->events |= COMEDI_CB_ERROR;
1175 for (i = 0; i < n; i++) {
1176 devpriv->ai_fifo_buffer[i] =
1177 ni_readw(dev, NI_E_AI_FIFO_DATA_REG);
1179 comedi_buf_write_samples(s, devpriv->ai_fifo_buffer, n);
1183 static void ni_handle_fifo_half_full(struct comedi_device *dev)
1185 const struct ni_board_struct *board = dev->board_ptr;
1186 struct comedi_subdevice *s = dev->read_subdev;
1189 n = board->ai_fifo_depth / 2;
1191 ni_ai_fifo_read(dev, s, n);
1198 static void ni_handle_fifo_dregs(struct comedi_device *dev)
1200 struct ni_private *devpriv = dev->private;
1201 struct comedi_subdevice *s = dev->read_subdev;
1203 unsigned short data;
1204 unsigned short fifo_empty;
1207 if (devpriv->is_611x) {
1208 while ((ni_stc_readw(dev, NISTC_AI_STATUS1_REG) &
1209 NISTC_AI_STATUS1_FIFO_E) == 0) {
1210 dl = ni_readl(dev, NI611X_AI_FIFO_DATA_REG);
1212 /* This may get the hi/lo data in the wrong order */
1214 comedi_buf_write_samples(s, &data, 1);
1216 comedi_buf_write_samples(s, &data, 1);
1218 } else if (devpriv->is_6143) {
1220 while (ni_readl(dev, NI6143_AI_FIFO_STATUS_REG) & 0x04) {
1221 dl = ni_readl(dev, NI6143_AI_FIFO_DATA_REG);
1223 /* This may get the hi/lo data in the wrong order */
1225 comedi_buf_write_samples(s, &data, 1);
1227 comedi_buf_write_samples(s, &data, 1);
1230 /* Check if stranded sample is present */
1231 if (ni_readl(dev, NI6143_AI_FIFO_STATUS_REG) & 0x01) {
1232 /* Get stranded sample into FIFO */
1233 ni_writel(dev, 0x01, NI6143_AI_FIFO_CTRL_REG);
1234 dl = ni_readl(dev, NI6143_AI_FIFO_DATA_REG);
1235 data = (dl >> 16) & 0xffff;
1236 comedi_buf_write_samples(s, &data, 1);
1240 fifo_empty = ni_stc_readw(dev, NISTC_AI_STATUS1_REG) &
1241 NISTC_AI_STATUS1_FIFO_E;
1242 while (fifo_empty == 0) {
1244 i < ARRAY_SIZE(devpriv->ai_fifo_buffer); i++) {
1245 fifo_empty = ni_stc_readw(dev,
1246 NISTC_AI_STATUS1_REG) &
1247 NISTC_AI_STATUS1_FIFO_E;
1250 devpriv->ai_fifo_buffer[i] =
1251 ni_readw(dev, NI_E_AI_FIFO_DATA_REG);
1253 comedi_buf_write_samples(s, devpriv->ai_fifo_buffer, i);
1258 static void get_last_sample_611x(struct comedi_device *dev)
1260 struct ni_private *devpriv = dev->private;
1261 struct comedi_subdevice *s = dev->read_subdev;
1262 unsigned short data;
1265 if (!devpriv->is_611x)
1268 /* Check if there's a single sample stuck in the FIFO */
1269 if (ni_readb(dev, NI_E_STATUS_REG) & 0x80) {
1270 dl = ni_readl(dev, NI611X_AI_FIFO_DATA_REG);
1272 comedi_buf_write_samples(s, &data, 1);
1276 static void get_last_sample_6143(struct comedi_device *dev)
1278 struct ni_private *devpriv = dev->private;
1279 struct comedi_subdevice *s = dev->read_subdev;
1280 unsigned short data;
1283 if (!devpriv->is_6143)
1286 /* Check if there's a single sample stuck in the FIFO */
1287 if (ni_readl(dev, NI6143_AI_FIFO_STATUS_REG) & 0x01) {
1288 /* Get stranded sample into FIFO */
1289 ni_writel(dev, 0x01, NI6143_AI_FIFO_CTRL_REG);
1290 dl = ni_readl(dev, NI6143_AI_FIFO_DATA_REG);
1292 /* This may get the hi/lo data in the wrong order */
1293 data = (dl >> 16) & 0xffff;
1294 comedi_buf_write_samples(s, &data, 1);
1298 static void shutdown_ai_command(struct comedi_device *dev)
1300 struct comedi_subdevice *s = dev->read_subdev;
1303 ni_ai_drain_dma(dev);
1305 ni_handle_fifo_dregs(dev);
1306 get_last_sample_611x(dev);
1307 get_last_sample_6143(dev);
1309 s->async->events |= COMEDI_CB_EOA;
1312 static void ni_handle_eos(struct comedi_device *dev, struct comedi_subdevice *s)
1314 struct ni_private *devpriv = dev->private;
1316 if (devpriv->aimode == AIMODE_SCAN) {
1318 static const int timeout = 10;
1321 for (i = 0; i < timeout; i++) {
1322 ni_sync_ai_dma(dev);
1323 if ((s->async->events & COMEDI_CB_EOS))
1328 ni_handle_fifo_dregs(dev);
1329 s->async->events |= COMEDI_CB_EOS;
1332 /* handle special case of single scan */
1333 if (devpriv->ai_cmd2 & NISTC_AI_CMD2_END_ON_EOS)
1334 shutdown_ai_command(dev);
1337 static void handle_gpct_interrupt(struct comedi_device *dev,
1338 unsigned short counter_index)
1341 struct ni_private *devpriv = dev->private;
1342 struct comedi_subdevice *s;
1344 s = &dev->subdevices[NI_GPCT_SUBDEV(counter_index)];
1346 ni_tio_handle_interrupt(&devpriv->counter_dev->counters[counter_index],
1348 comedi_handle_events(dev, s);
1352 static void ack_a_interrupt(struct comedi_device *dev, unsigned short a_status)
1354 unsigned short ack = 0;
1356 if (a_status & NISTC_AI_STATUS1_SC_TC)
1357 ack |= NISTC_INTA_ACK_AI_SC_TC;
1358 if (a_status & NISTC_AI_STATUS1_START1)
1359 ack |= NISTC_INTA_ACK_AI_START1;
1360 if (a_status & NISTC_AI_STATUS1_START)
1361 ack |= NISTC_INTA_ACK_AI_START;
1362 if (a_status & NISTC_AI_STATUS1_STOP)
1363 ack |= NISTC_INTA_ACK_AI_STOP;
1365 ni_stc_writew(dev, ack, NISTC_INTA_ACK_REG);
1368 static void handle_a_interrupt(struct comedi_device *dev, unsigned short status,
1369 unsigned ai_mite_status)
1371 struct comedi_subdevice *s = dev->read_subdev;
1372 struct comedi_cmd *cmd = &s->async->cmd;
1374 /* 67xx boards don't have ai subdevice, but their gpct0 might generate an a interrupt */
1375 if (s->type == COMEDI_SUBD_UNUSED)
1379 if (ai_mite_status & CHSR_LINKC)
1380 ni_sync_ai_dma(dev);
1382 if (ai_mite_status & ~(CHSR_INT | CHSR_LINKC | CHSR_DONE | CHSR_MRDY |
1383 CHSR_DRDY | CHSR_DRQ1 | CHSR_DRQ0 | CHSR_ERROR |
1384 CHSR_SABORT | CHSR_XFERR | CHSR_LxERR_mask)) {
1385 dev_err(dev->class_dev,
1386 "unknown mite interrupt (ai_mite_status=%08x)\n",
1388 s->async->events |= COMEDI_CB_ERROR;
1389 /* disable_irq(dev->irq); */
1393 /* test for all uncommon interrupt events at the same time */
1394 if (status & (NISTC_AI_STATUS1_ERR |
1395 NISTC_AI_STATUS1_SC_TC | NISTC_AI_STATUS1_START1)) {
1396 if (status == 0xffff) {
1397 dev_err(dev->class_dev, "Card removed?\n");
1398 /* we probably aren't even running a command now,
1399 * so it's a good idea to be careful. */
1400 if (comedi_is_subdevice_running(s)) {
1401 s->async->events |= COMEDI_CB_ERROR;
1402 comedi_handle_events(dev, s);
1406 if (status & NISTC_AI_STATUS1_ERR) {
1407 dev_err(dev->class_dev, "ai error a_status=%04x\n",
1410 shutdown_ai_command(dev);
1412 s->async->events |= COMEDI_CB_ERROR;
1413 if (status & NISTC_AI_STATUS1_OVER)
1414 s->async->events |= COMEDI_CB_OVERFLOW;
1416 comedi_handle_events(dev, s);
1419 if (status & NISTC_AI_STATUS1_SC_TC) {
1420 if (cmd->stop_src == TRIG_COUNT)
1421 shutdown_ai_command(dev);
1425 if (status & NISTC_AI_STATUS1_FIFO_HF) {
1427 static const int timeout = 10;
1428 /* pcmcia cards (at least 6036) seem to stop producing interrupts if we
1429 *fail to get the fifo less than half full, so loop to be sure.*/
1430 for (i = 0; i < timeout; ++i) {
1431 ni_handle_fifo_half_full(dev);
1432 if ((ni_stc_readw(dev, NISTC_AI_STATUS1_REG) &
1433 NISTC_AI_STATUS1_FIFO_HF) == 0)
1437 #endif /* !PCIDMA */
1439 if (status & NISTC_AI_STATUS1_STOP)
1440 ni_handle_eos(dev, s);
1442 comedi_handle_events(dev, s);
1445 static void ack_b_interrupt(struct comedi_device *dev, unsigned short b_status)
1447 unsigned short ack = 0;
1449 if (b_status & NISTC_AO_STATUS1_BC_TC)
1450 ack |= NISTC_INTB_ACK_AO_BC_TC;
1451 if (b_status & NISTC_AO_STATUS1_OVERRUN)
1452 ack |= NISTC_INTB_ACK_AO_ERR;
1453 if (b_status & NISTC_AO_STATUS1_START)
1454 ack |= NISTC_INTB_ACK_AO_START;
1455 if (b_status & NISTC_AO_STATUS1_START1)
1456 ack |= NISTC_INTB_ACK_AO_START1;
1457 if (b_status & NISTC_AO_STATUS1_UC_TC)
1458 ack |= NISTC_INTB_ACK_AO_UC_TC;
1459 if (b_status & NISTC_AO_STATUS1_UI2_TC)
1460 ack |= NISTC_INTB_ACK_AO_UI2_TC;
1461 if (b_status & NISTC_AO_STATUS1_UPDATE)
1462 ack |= NISTC_INTB_ACK_AO_UPDATE;
1464 ni_stc_writew(dev, ack, NISTC_INTB_ACK_REG);
1467 static void handle_b_interrupt(struct comedi_device *dev,
1468 unsigned short b_status, unsigned ao_mite_status)
1470 struct comedi_subdevice *s = dev->write_subdev;
1471 /* unsigned short ack=0; */
1474 /* Currently, mite.c requires us to handle LINKC */
1475 if (ao_mite_status & CHSR_LINKC) {
1476 struct ni_private *devpriv = dev->private;
1478 mite_handle_b_linkc(devpriv->mite, dev);
1481 if (ao_mite_status & ~(CHSR_INT | CHSR_LINKC | CHSR_DONE | CHSR_MRDY |
1482 CHSR_DRDY | CHSR_DRQ1 | CHSR_DRQ0 | CHSR_ERROR |
1483 CHSR_SABORT | CHSR_XFERR | CHSR_LxERR_mask)) {
1484 dev_err(dev->class_dev,
1485 "unknown mite interrupt (ao_mite_status=%08x)\n",
1487 s->async->events |= COMEDI_CB_ERROR;
1491 if (b_status == 0xffff)
1493 if (b_status & NISTC_AO_STATUS1_OVERRUN) {
1494 dev_err(dev->class_dev,
1495 "AO FIFO underrun status=0x%04x status2=0x%04x\n",
1496 b_status, ni_stc_readw(dev, NISTC_AO_STATUS2_REG));
1497 s->async->events |= COMEDI_CB_OVERFLOW;
1500 if (s->async->cmd.stop_src != TRIG_NONE &&
1501 b_status & NISTC_AO_STATUS1_BC_TC)
1502 s->async->events |= COMEDI_CB_EOA;
1505 if (b_status & NISTC_AO_STATUS1_FIFO_REQ) {
1508 ret = ni_ao_fifo_half_empty(dev, s);
1510 dev_err(dev->class_dev, "AO buffer underrun\n");
1511 ni_set_bits(dev, NISTC_INTB_ENA_REG,
1512 NISTC_INTB_ENA_AO_FIFO |
1513 NISTC_INTB_ENA_AO_ERR, 0);
1514 s->async->events |= COMEDI_CB_OVERFLOW;
1519 comedi_handle_events(dev, s);
1522 static void ni_ai_munge(struct comedi_device *dev, struct comedi_subdevice *s,
1523 void *data, unsigned int num_bytes,
1524 unsigned int chan_index)
1526 struct ni_private *devpriv = dev->private;
1527 struct comedi_async *async = s->async;
1528 struct comedi_cmd *cmd = &async->cmd;
1529 unsigned int nsamples = comedi_bytes_to_samples(s, num_bytes);
1530 unsigned short *array = data;
1531 unsigned int *larray = data;
1534 __le16 *barray = data;
1535 __le32 *blarray = data;
1538 for (i = 0; i < nsamples; i++) {
1540 if (s->subdev_flags & SDF_LSAMPL)
1541 larray[i] = le32_to_cpu(blarray[i]);
1543 array[i] = le16_to_cpu(barray[i]);
1545 if (s->subdev_flags & SDF_LSAMPL)
1546 larray[i] += devpriv->ai_offset[chan_index];
1548 array[i] += devpriv->ai_offset[chan_index];
1550 chan_index %= cmd->chanlist_len;
1556 static int ni_ai_setup_MITE_dma(struct comedi_device *dev)
1558 struct ni_private *devpriv = dev->private;
1559 struct comedi_subdevice *s = dev->read_subdev;
1561 unsigned long flags;
1563 retval = ni_request_ai_mite_channel(dev);
1567 /* write alloc the entire buffer */
1568 comedi_buf_write_alloc(s, s->async->prealloc_bufsz);
1570 spin_lock_irqsave(&devpriv->mite_channel_lock, flags);
1571 if (!devpriv->ai_mite_chan) {
1572 spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
1576 if (devpriv->is_611x || devpriv->is_6143)
1577 mite_prep_dma(devpriv->ai_mite_chan, 32, 16);
1578 else if (devpriv->is_628x)
1579 mite_prep_dma(devpriv->ai_mite_chan, 32, 32);
1581 mite_prep_dma(devpriv->ai_mite_chan, 16, 16);
1584 mite_dma_arm(devpriv->ai_mite_chan);
1585 spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
1590 static int ni_ao_setup_MITE_dma(struct comedi_device *dev)
1592 struct ni_private *devpriv = dev->private;
1593 struct comedi_subdevice *s = dev->write_subdev;
1595 unsigned long flags;
1597 retval = ni_request_ao_mite_channel(dev);
1601 /* read alloc the entire buffer */
1602 comedi_buf_read_alloc(s, s->async->prealloc_bufsz);
1604 spin_lock_irqsave(&devpriv->mite_channel_lock, flags);
1605 if (devpriv->ao_mite_chan) {
1606 if (devpriv->is_611x || devpriv->is_6713) {
1607 mite_prep_dma(devpriv->ao_mite_chan, 32, 32);
1609 /* doing 32 instead of 16 bit wide transfers from memory
1610 makes the mite do 32 bit pci transfers, doubling pci bandwidth. */
1611 mite_prep_dma(devpriv->ao_mite_chan, 16, 32);
1613 mite_dma_arm(devpriv->ao_mite_chan);
1617 spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
1625 used for both cancel ioctl and board initialization
1627 this is pretty harsh for a cancel, but it works...
1630 static int ni_ai_reset(struct comedi_device *dev, struct comedi_subdevice *s)
1632 struct ni_private *devpriv = dev->private;
1633 unsigned ai_personal;
1634 unsigned ai_out_ctrl;
1636 ni_release_ai_mite_channel(dev);
1637 /* ai configuration */
1638 ni_stc_writew(dev, NISTC_RESET_AI_CFG_START | NISTC_RESET_AI,
1641 ni_set_bits(dev, NISTC_INTA_ENA_REG, NISTC_INTA_ENA_AI_MASK, 0);
1643 ni_clear_ai_fifo(dev);
1645 if (!devpriv->is_6143)
1646 ni_writeb(dev, NI_E_MISC_CMD_EXT_ATRIG, NI_E_MISC_CMD_REG);
1648 ni_stc_writew(dev, NISTC_AI_CMD1_DISARM, NISTC_AI_CMD1_REG);
1649 ni_stc_writew(dev, NISTC_AI_MODE1_START_STOP |
1651 /*| NISTC_AI_MODE1_TRIGGER_ONCE */,
1652 NISTC_AI_MODE1_REG);
1653 ni_stc_writew(dev, 0, NISTC_AI_MODE2_REG);
1654 /* generate FIFO interrupts on non-empty */
1655 ni_stc_writew(dev, NISTC_AI_MODE3_FIFO_MODE_NE,
1656 NISTC_AI_MODE3_REG);
1658 ai_personal = NISTC_AI_PERSONAL_SHIFTIN_PW |
1659 NISTC_AI_PERSONAL_SOC_POLARITY |
1660 NISTC_AI_PERSONAL_LOCALMUX_CLK_PW;
1661 ai_out_ctrl = NISTC_AI_OUT_CTRL_SCAN_IN_PROG_SEL(3) |
1662 NISTC_AI_OUT_CTRL_EXTMUX_CLK_SEL(0) |
1663 NISTC_AI_OUT_CTRL_LOCALMUX_CLK_SEL(2) |
1664 NISTC_AI_OUT_CTRL_SC_TC_SEL(3);
1665 if (devpriv->is_611x) {
1666 ai_out_ctrl |= NISTC_AI_OUT_CTRL_CONVERT_HIGH;
1667 } else if (devpriv->is_6143) {
1668 ai_out_ctrl |= NISTC_AI_OUT_CTRL_CONVERT_LOW;
1670 ai_personal |= NISTC_AI_PERSONAL_CONVERT_PW;
1671 if (devpriv->is_622x)
1672 ai_out_ctrl |= NISTC_AI_OUT_CTRL_CONVERT_HIGH;
1674 ai_out_ctrl |= NISTC_AI_OUT_CTRL_CONVERT_LOW;
1676 ni_stc_writew(dev, ai_personal, NISTC_AI_PERSONAL_REG);
1677 ni_stc_writew(dev, ai_out_ctrl, NISTC_AI_OUT_CTRL_REG);
1679 /* the following registers should not be changed, because there
1680 * are no backup registers in devpriv. If you want to change
1681 * any of these, add a backup register and other appropriate code:
1682 * NISTC_AI_MODE1_REG
1683 * NISTC_AI_MODE3_REG
1684 * NISTC_AI_PERSONAL_REG
1685 * NISTC_AI_OUT_CTRL_REG
1688 /* clear interrupts */
1689 ni_stc_writew(dev, NISTC_INTA_ACK_AI_ALL, NISTC_INTA_ACK_REG);
1691 ni_stc_writew(dev, NISTC_RESET_AI_CFG_END, NISTC_RESET_REG);
1696 static int ni_ai_poll(struct comedi_device *dev, struct comedi_subdevice *s)
1698 unsigned long flags;
1701 /* lock to avoid race with interrupt handler */
1702 spin_lock_irqsave(&dev->spinlock, flags);
1704 ni_handle_fifo_dregs(dev);
1706 ni_sync_ai_dma(dev);
1708 count = comedi_buf_n_bytes_ready(s);
1709 spin_unlock_irqrestore(&dev->spinlock, flags);
1714 static void ni_prime_channelgain_list(struct comedi_device *dev)
1718 ni_stc_writew(dev, NISTC_AI_CMD1_CONVERT_PULSE, NISTC_AI_CMD1_REG);
1719 for (i = 0; i < NI_TIMEOUT; ++i) {
1720 if (!(ni_stc_readw(dev, NISTC_AI_STATUS1_REG) &
1721 NISTC_AI_STATUS1_FIFO_E)) {
1722 ni_stc_writew(dev, 1, NISTC_ADC_FIFO_CLR_REG);
1727 dev_err(dev->class_dev, "timeout loading channel/gain list\n");
1730 static void ni_m_series_load_channelgain_list(struct comedi_device *dev,
1731 unsigned int n_chan,
1734 const struct ni_board_struct *board = dev->board_ptr;
1735 struct ni_private *devpriv = dev->private;
1736 unsigned int chan, range, aref;
1738 unsigned int dither;
1739 unsigned range_code;
1741 ni_stc_writew(dev, 1, NISTC_CFG_MEM_CLR_REG);
1743 if ((list[0] & CR_ALT_SOURCE)) {
1744 unsigned bypass_bits;
1746 chan = CR_CHAN(list[0]);
1747 range = CR_RANGE(list[0]);
1748 range_code = ni_gainlkup[board->gainlkup][range];
1749 dither = (list[0] & CR_ALT_FILTER) != 0;
1750 bypass_bits = NI_M_CFG_BYPASS_FIFO |
1751 NI_M_CFG_BYPASS_AI_CHAN(chan) |
1752 NI_M_CFG_BYPASS_AI_GAIN(range_code) |
1753 devpriv->ai_calib_source;
1755 bypass_bits |= NI_M_CFG_BYPASS_AI_DITHER;
1756 /* don't use 2's complement encoding */
1757 bypass_bits |= NI_M_CFG_BYPASS_AI_POLARITY;
1758 ni_writel(dev, bypass_bits, NI_M_CFG_BYPASS_FIFO_REG);
1760 ni_writel(dev, 0, NI_M_CFG_BYPASS_FIFO_REG);
1762 for (i = 0; i < n_chan; i++) {
1763 unsigned config_bits = 0;
1765 chan = CR_CHAN(list[i]);
1766 aref = CR_AREF(list[i]);
1767 range = CR_RANGE(list[i]);
1768 dither = (list[i] & CR_ALT_FILTER) != 0;
1770 range_code = ni_gainlkup[board->gainlkup][range];
1771 devpriv->ai_offset[i] = 0;
1774 config_bits |= NI_M_AI_CFG_CHAN_TYPE_DIFF;
1777 config_bits |= NI_M_AI_CFG_CHAN_TYPE_COMMON;
1780 config_bits |= NI_M_AI_CFG_CHAN_TYPE_GROUND;
1785 config_bits |= NI_M_AI_CFG_CHAN_SEL(chan);
1786 config_bits |= NI_M_AI_CFG_BANK_SEL(chan);
1787 config_bits |= NI_M_AI_CFG_GAIN(range_code);
1788 if (i == n_chan - 1)
1789 config_bits |= NI_M_AI_CFG_LAST_CHAN;
1791 config_bits |= NI_M_AI_CFG_DITHER;
1792 /* don't use 2's complement encoding */
1793 config_bits |= NI_M_AI_CFG_POLARITY;
1794 ni_writew(dev, config_bits, NI_M_AI_CFG_FIFO_DATA_REG);
1796 ni_prime_channelgain_list(dev);
1800 * Notes on the 6110 and 6111:
1801 * These boards a slightly different than the rest of the series, since
1802 * they have multiple A/D converters.
1803 * From the driver side, the configuration memory is a
1805 * Configuration Memory Low:
1807 * bit 8: unipolar/bipolar (should be 0 for bipolar)
1808 * bits 0-3: gain. This is 4 bits instead of 3 for the other boards
1809 * 1001 gain=0.1 (+/- 50)
1818 * Configuration Memory High:
1819 * bits 12-14: Channel Type
1820 * 001 for differential
1821 * 000 for calibration
1822 * bit 11: coupling (this is not currently handled)
1826 * valid channels are 0-3
1828 static void ni_load_channelgain_list(struct comedi_device *dev,
1829 struct comedi_subdevice *s,
1830 unsigned int n_chan, unsigned int *list)
1832 const struct ni_board_struct *board = dev->board_ptr;
1833 struct ni_private *devpriv = dev->private;
1834 unsigned int offset = (s->maxdata + 1) >> 1;
1835 unsigned int chan, range, aref;
1837 unsigned int hi, lo;
1838 unsigned int dither;
1840 if (devpriv->is_m_series) {
1841 ni_m_series_load_channelgain_list(dev, n_chan, list);
1844 if (n_chan == 1 && !devpriv->is_611x && !devpriv->is_6143) {
1845 if (devpriv->changain_state
1846 && devpriv->changain_spec == list[0]) {
1850 devpriv->changain_state = 1;
1851 devpriv->changain_spec = list[0];
1853 devpriv->changain_state = 0;
1856 ni_stc_writew(dev, 1, NISTC_CFG_MEM_CLR_REG);
1858 /* Set up Calibration mode if required */
1859 if (devpriv->is_6143) {
1860 if ((list[0] & CR_ALT_SOURCE)
1861 && !devpriv->ai_calib_source_enabled) {
1862 /* Strobe Relay enable bit */
1863 ni_writew(dev, devpriv->ai_calib_source |
1864 NI6143_CALIB_CHAN_RELAY_ON,
1865 NI6143_CALIB_CHAN_REG);
1866 ni_writew(dev, devpriv->ai_calib_source,
1867 NI6143_CALIB_CHAN_REG);
1868 devpriv->ai_calib_source_enabled = 1;
1869 msleep_interruptible(100); /* Allow relays to change */
1870 } else if (!(list[0] & CR_ALT_SOURCE)
1871 && devpriv->ai_calib_source_enabled) {
1872 /* Strobe Relay disable bit */
1873 ni_writew(dev, devpriv->ai_calib_source |
1874 NI6143_CALIB_CHAN_RELAY_OFF,
1875 NI6143_CALIB_CHAN_REG);
1876 ni_writew(dev, devpriv->ai_calib_source,
1877 NI6143_CALIB_CHAN_REG);
1878 devpriv->ai_calib_source_enabled = 0;
1879 msleep_interruptible(100); /* Allow relays to change */
1883 for (i = 0; i < n_chan; i++) {
1884 if (!devpriv->is_6143 && (list[i] & CR_ALT_SOURCE))
1885 chan = devpriv->ai_calib_source;
1887 chan = CR_CHAN(list[i]);
1888 aref = CR_AREF(list[i]);
1889 range = CR_RANGE(list[i]);
1890 dither = (list[i] & CR_ALT_FILTER) != 0;
1892 /* fix the external/internal range differences */
1893 range = ni_gainlkup[board->gainlkup][range];
1894 if (devpriv->is_611x)
1895 devpriv->ai_offset[i] = offset;
1897 devpriv->ai_offset[i] = (range & 0x100) ? 0 : offset;
1900 if ((list[i] & CR_ALT_SOURCE)) {
1901 if (devpriv->is_611x)
1902 ni_writew(dev, CR_CHAN(list[i]) & 0x0003,
1903 NI611X_CALIB_CHAN_SEL_REG);
1905 if (devpriv->is_611x)
1907 else if (devpriv->is_6143)
1911 hi |= NI_E_AI_CFG_HI_TYPE_DIFF;
1914 hi |= NI_E_AI_CFG_HI_TYPE_COMMON;
1917 hi |= NI_E_AI_CFG_HI_TYPE_GROUND;
1923 hi |= NI_E_AI_CFG_HI_CHAN(chan);
1925 ni_writew(dev, hi, NI_E_AI_CFG_HI_REG);
1927 if (!devpriv->is_6143) {
1928 lo = NI_E_AI_CFG_LO_GAIN(range);
1930 if (i == n_chan - 1)
1931 lo |= NI_E_AI_CFG_LO_LAST_CHAN;
1933 lo |= NI_E_AI_CFG_LO_DITHER;
1935 ni_writew(dev, lo, NI_E_AI_CFG_LO_REG);
1939 /* prime the channel/gain list */
1940 if (!devpriv->is_611x && !devpriv->is_6143)
1941 ni_prime_channelgain_list(dev);
1944 static int ni_ai_insn_read(struct comedi_device *dev,
1945 struct comedi_subdevice *s,
1946 struct comedi_insn *insn,
1949 struct ni_private *devpriv = dev->private;
1950 unsigned int mask = (s->maxdata + 1) >> 1;
1956 ni_load_channelgain_list(dev, s, 1, &insn->chanspec);
1958 ni_clear_ai_fifo(dev);
1960 signbits = devpriv->ai_offset[0];
1961 if (devpriv->is_611x) {
1962 for (n = 0; n < num_adc_stages_611x; n++) {
1963 ni_stc_writew(dev, NISTC_AI_CMD1_CONVERT_PULSE,
1967 for (n = 0; n < insn->n; n++) {
1968 ni_stc_writew(dev, NISTC_AI_CMD1_CONVERT_PULSE,
1970 /* The 611x has screwy 32-bit FIFOs. */
1972 for (i = 0; i < NI_TIMEOUT; i++) {
1973 if (ni_readb(dev, NI_E_STATUS_REG) & 0x80) {
1975 NI611X_AI_FIFO_DATA_REG);
1980 if (!(ni_stc_readw(dev, NISTC_AI_STATUS1_REG) &
1981 NISTC_AI_STATUS1_FIFO_E)) {
1983 NI611X_AI_FIFO_DATA_REG);
1988 if (i == NI_TIMEOUT) {
1989 dev_err(dev->class_dev, "timeout\n");
1995 } else if (devpriv->is_6143) {
1996 for (n = 0; n < insn->n; n++) {
1997 ni_stc_writew(dev, NISTC_AI_CMD1_CONVERT_PULSE,
2000 /* The 6143 has 32-bit FIFOs. You need to strobe a bit to move a single 16bit stranded sample into the FIFO */
2002 for (i = 0; i < NI_TIMEOUT; i++) {
2003 if (ni_readl(dev, NI6143_AI_FIFO_STATUS_REG) &
2005 /* Get stranded sample into FIFO */
2006 ni_writel(dev, 0x01,
2007 NI6143_AI_FIFO_CTRL_REG);
2009 NI6143_AI_FIFO_DATA_REG);
2013 if (i == NI_TIMEOUT) {
2014 dev_err(dev->class_dev, "timeout\n");
2017 data[n] = (((dl >> 16) & 0xFFFF) + signbits) & 0xFFFF;
2020 for (n = 0; n < insn->n; n++) {
2021 ni_stc_writew(dev, NISTC_AI_CMD1_CONVERT_PULSE,
2023 for (i = 0; i < NI_TIMEOUT; i++) {
2024 if (!(ni_stc_readw(dev, NISTC_AI_STATUS1_REG) &
2025 NISTC_AI_STATUS1_FIFO_E))
2028 if (i == NI_TIMEOUT) {
2029 dev_err(dev->class_dev, "timeout\n");
2032 if (devpriv->is_m_series) {
2033 dl = ni_readl(dev, NI_M_AI_FIFO_DATA_REG);
2037 d = ni_readw(dev, NI_E_AI_FIFO_DATA_REG);
2038 d += signbits; /* subtle: needs to be short addition */
2046 static int ni_ns_to_timer(const struct comedi_device *dev, unsigned nanosec,
2049 struct ni_private *devpriv = dev->private;
2052 switch (flags & CMDF_ROUND_MASK) {
2053 case CMDF_ROUND_NEAREST:
2055 divider = DIV_ROUND_CLOSEST(nanosec, devpriv->clock_ns);
2057 case CMDF_ROUND_DOWN:
2058 divider = (nanosec) / devpriv->clock_ns;
2061 divider = DIV_ROUND_UP(nanosec, devpriv->clock_ns);
2067 static unsigned ni_timer_to_ns(const struct comedi_device *dev, int timer)
2069 struct ni_private *devpriv = dev->private;
2071 return devpriv->clock_ns * (timer + 1);
2074 static void ni_cmd_set_mite_transfer(struct mite_dma_descriptor_ring *ring,
2075 struct comedi_subdevice *sdev,
2076 const struct comedi_cmd *cmd,
2077 unsigned int max_count) {
2079 unsigned int nbytes = max_count;
2081 if (cmd->stop_arg > 0 && cmd->stop_arg < max_count)
2082 nbytes = cmd->stop_arg;
2083 nbytes *= comedi_bytes_per_scan(sdev);
2085 if (nbytes > sdev->async->prealloc_bufsz) {
2086 if (cmd->stop_arg > 0)
2087 dev_err(sdev->device->class_dev,
2088 "ni_cmd_set_mite_transfer: tried exact data transfer limits greater than buffer size\n");
2091 * we can only transfer up to the size of the buffer. In this
2092 * case, the user is expected to continue to write into the
2093 * comedi buffer (already implemented as a ring buffer).
2095 nbytes = sdev->async->prealloc_bufsz;
2098 mite_init_ring_descriptors(ring, sdev, nbytes);
2100 dev_err(sdev->device->class_dev,
2101 "ni_cmd_set_mite_transfer: exact data transfer limits not implemented yet without DMA\n");
2105 static unsigned ni_min_ai_scan_period_ns(struct comedi_device *dev,
2106 unsigned num_channels)
2108 const struct ni_board_struct *board = dev->board_ptr;
2109 struct ni_private *devpriv = dev->private;
2111 /* simultaneously-sampled inputs */
2112 if (devpriv->is_611x || devpriv->is_6143)
2113 return board->ai_speed;
2115 /* multiplexed inputs */
2116 return board->ai_speed * num_channels;
2119 static int ni_ai_cmdtest(struct comedi_device *dev, struct comedi_subdevice *s,
2120 struct comedi_cmd *cmd)
2122 const struct ni_board_struct *board = dev->board_ptr;
2123 struct ni_private *devpriv = dev->private;
2126 unsigned int sources;
2128 /* Step 1 : check if triggers are trivially valid */
2130 err |= comedi_check_trigger_src(&cmd->start_src,
2131 TRIG_NOW | TRIG_INT | TRIG_EXT);
2132 err |= comedi_check_trigger_src(&cmd->scan_begin_src,
2133 TRIG_TIMER | TRIG_EXT);
2135 sources = TRIG_TIMER | TRIG_EXT;
2136 if (devpriv->is_611x || devpriv->is_6143)
2137 sources |= TRIG_NOW;
2138 err |= comedi_check_trigger_src(&cmd->convert_src, sources);
2140 err |= comedi_check_trigger_src(&cmd->scan_end_src, TRIG_COUNT);
2141 err |= comedi_check_trigger_src(&cmd->stop_src, TRIG_COUNT | TRIG_NONE);
2146 /* Step 2a : make sure trigger sources are unique */
2148 err |= comedi_check_trigger_is_unique(cmd->start_src);
2149 err |= comedi_check_trigger_is_unique(cmd->scan_begin_src);
2150 err |= comedi_check_trigger_is_unique(cmd->convert_src);
2151 err |= comedi_check_trigger_is_unique(cmd->stop_src);
2153 /* Step 2b : and mutually compatible */
2158 /* Step 3: check if arguments are trivially valid */
2160 switch (cmd->start_src) {
2163 err |= comedi_check_trigger_arg_is(&cmd->start_arg, 0);
2166 tmp = CR_CHAN(cmd->start_arg);
2170 tmp |= (cmd->start_arg & (CR_INVERT | CR_EDGE));
2171 err |= comedi_check_trigger_arg_is(&cmd->start_arg, tmp);
2175 if (cmd->scan_begin_src == TRIG_TIMER) {
2176 err |= comedi_check_trigger_arg_min(&cmd->scan_begin_arg,
2177 ni_min_ai_scan_period_ns(dev, cmd->chanlist_len));
2178 err |= comedi_check_trigger_arg_max(&cmd->scan_begin_arg,
2181 } else if (cmd->scan_begin_src == TRIG_EXT) {
2182 /* external trigger */
2183 unsigned int tmp = CR_CHAN(cmd->scan_begin_arg);
2187 tmp |= (cmd->scan_begin_arg & (CR_INVERT | CR_EDGE));
2188 err |= comedi_check_trigger_arg_is(&cmd->scan_begin_arg, tmp);
2189 } else { /* TRIG_OTHER */
2190 err |= comedi_check_trigger_arg_is(&cmd->scan_begin_arg, 0);
2193 if (cmd->convert_src == TRIG_TIMER) {
2194 if (devpriv->is_611x || devpriv->is_6143) {
2195 err |= comedi_check_trigger_arg_is(&cmd->convert_arg,
2198 err |= comedi_check_trigger_arg_min(&cmd->convert_arg,
2200 err |= comedi_check_trigger_arg_max(&cmd->convert_arg,
2204 } else if (cmd->convert_src == TRIG_EXT) {
2205 /* external trigger */
2206 unsigned int tmp = CR_CHAN(cmd->convert_arg);
2210 tmp |= (cmd->convert_arg & (CR_ALT_FILTER | CR_INVERT));
2211 err |= comedi_check_trigger_arg_is(&cmd->convert_arg, tmp);
2212 } else if (cmd->convert_src == TRIG_NOW) {
2213 err |= comedi_check_trigger_arg_is(&cmd->convert_arg, 0);
2216 err |= comedi_check_trigger_arg_is(&cmd->scan_end_arg,
2219 if (cmd->stop_src == TRIG_COUNT) {
2220 unsigned int max_count = 0x01000000;
2222 if (devpriv->is_611x)
2223 max_count -= num_adc_stages_611x;
2224 err |= comedi_check_trigger_arg_max(&cmd->stop_arg, max_count);
2225 err |= comedi_check_trigger_arg_min(&cmd->stop_arg, 1);
2228 err |= comedi_check_trigger_arg_is(&cmd->stop_arg, 0);
2234 /* step 4: fix up any arguments */
2236 if (cmd->scan_begin_src == TRIG_TIMER) {
2237 tmp = cmd->scan_begin_arg;
2238 cmd->scan_begin_arg =
2239 ni_timer_to_ns(dev, ni_ns_to_timer(dev,
2240 cmd->scan_begin_arg,
2242 if (tmp != cmd->scan_begin_arg)
2245 if (cmd->convert_src == TRIG_TIMER) {
2246 if (!devpriv->is_611x && !devpriv->is_6143) {
2247 tmp = cmd->convert_arg;
2249 ni_timer_to_ns(dev, ni_ns_to_timer(dev,
2252 if (tmp != cmd->convert_arg)
2254 if (cmd->scan_begin_src == TRIG_TIMER &&
2255 cmd->scan_begin_arg <
2256 cmd->convert_arg * cmd->scan_end_arg) {
2257 cmd->scan_begin_arg =
2258 cmd->convert_arg * cmd->scan_end_arg;
2270 static int ni_ai_inttrig(struct comedi_device *dev,
2271 struct comedi_subdevice *s,
2272 unsigned int trig_num)
2274 struct ni_private *devpriv = dev->private;
2275 struct comedi_cmd *cmd = &s->async->cmd;
2277 if (trig_num != cmd->start_arg)
2280 ni_stc_writew(dev, NISTC_AI_CMD2_START1_PULSE | devpriv->ai_cmd2,
2282 s->async->inttrig = NULL;
2287 static int ni_ai_cmd(struct comedi_device *dev, struct comedi_subdevice *s)
2289 struct ni_private *devpriv = dev->private;
2290 const struct comedi_cmd *cmd = &s->async->cmd;
2292 int mode1 = 0; /* mode1 is needed for both stop and convert */
2294 int start_stop_select = 0;
2295 unsigned int stop_count;
2296 int interrupt_a_enable = 0;
2299 if (dev->irq == 0) {
2300 dev_err(dev->class_dev, "cannot run command without an irq\n");
2303 ni_clear_ai_fifo(dev);
2305 ni_load_channelgain_list(dev, s, cmd->chanlist_len, cmd->chanlist);
2307 /* start configuration */
2308 ni_stc_writew(dev, NISTC_RESET_AI_CFG_START, NISTC_RESET_REG);
2310 /* disable analog triggering for now, since it
2311 * interferes with the use of pfi0 */
2312 devpriv->an_trig_etc_reg &= ~NISTC_ATRIG_ETC_ENA;
2313 ni_stc_writew(dev, devpriv->an_trig_etc_reg, NISTC_ATRIG_ETC_REG);
2315 ai_trig = NISTC_AI_TRIG_START2_SEL(0) | NISTC_AI_TRIG_START1_SYNC;
2316 switch (cmd->start_src) {
2319 ai_trig |= NISTC_AI_TRIG_START1_EDGE |
2320 NISTC_AI_TRIG_START1_SEL(0);
2323 ai_trig |= NISTC_AI_TRIG_START1_SEL(CR_CHAN(cmd->start_arg) +
2326 if (cmd->start_arg & CR_INVERT)
2327 ai_trig |= NISTC_AI_TRIG_START1_POLARITY;
2328 if (cmd->start_arg & CR_EDGE)
2329 ai_trig |= NISTC_AI_TRIG_START1_EDGE;
2332 ni_stc_writew(dev, ai_trig, NISTC_AI_TRIG_SEL_REG);
2334 mode2 &= ~NISTC_AI_MODE2_PRE_TRIGGER;
2335 mode2 &= ~NISTC_AI_MODE2_SC_INIT_LOAD_SRC;
2336 mode2 &= ~NISTC_AI_MODE2_SC_RELOAD_MODE;
2337 ni_stc_writew(dev, mode2, NISTC_AI_MODE2_REG);
2339 if (cmd->chanlist_len == 1 || devpriv->is_611x || devpriv->is_6143) {
2341 start_stop_select |= NISTC_AI_STOP_POLARITY |
2342 NISTC_AI_STOP_SEL(31) |
2345 /* ai configuration memory */
2346 start_stop_select |= NISTC_AI_STOP_SEL(19);
2348 ni_stc_writew(dev, start_stop_select, NISTC_AI_START_STOP_REG);
2350 devpriv->ai_cmd2 = 0;
2351 switch (cmd->stop_src) {
2353 stop_count = cmd->stop_arg - 1;
2355 if (devpriv->is_611x) {
2356 /* have to take 3 stage adc pipeline into account */
2357 stop_count += num_adc_stages_611x;
2359 /* stage number of scans */
2360 ni_stc_writel(dev, stop_count, NISTC_AI_SC_LOADA_REG);
2362 mode1 |= NISTC_AI_MODE1_START_STOP |
2363 NISTC_AI_MODE1_RSVD |
2364 NISTC_AI_MODE1_TRIGGER_ONCE;
2365 ni_stc_writew(dev, mode1, NISTC_AI_MODE1_REG);
2366 /* load SC (Scan Count) */
2367 ni_stc_writew(dev, NISTC_AI_CMD1_SC_LOAD, NISTC_AI_CMD1_REG);
2369 if (stop_count == 0) {
2370 devpriv->ai_cmd2 |= NISTC_AI_CMD2_END_ON_EOS;
2371 interrupt_a_enable |= NISTC_INTA_ENA_AI_STOP;
2372 /* this is required to get the last sample for chanlist_len > 1, not sure why */
2373 if (cmd->chanlist_len > 1)
2374 start_stop_select |= NISTC_AI_STOP_POLARITY |
2379 /* stage number of scans */
2380 ni_stc_writel(dev, 0, NISTC_AI_SC_LOADA_REG);
2382 mode1 |= NISTC_AI_MODE1_START_STOP |
2383 NISTC_AI_MODE1_RSVD |
2384 NISTC_AI_MODE1_CONTINUOUS;
2385 ni_stc_writew(dev, mode1, NISTC_AI_MODE1_REG);
2387 /* load SC (Scan Count) */
2388 ni_stc_writew(dev, NISTC_AI_CMD1_SC_LOAD, NISTC_AI_CMD1_REG);
2392 switch (cmd->scan_begin_src) {
2395 * stop bits for non 611x boards
2396 * NISTC_AI_MODE3_SI_TRIG_DELAY=0
2397 * NISTC_AI_MODE2_PRE_TRIGGER=0
2398 * NISTC_AI_START_STOP_REG:
2399 * NISTC_AI_START_POLARITY=0 (?) rising edge
2400 * NISTC_AI_START_EDGE=1 edge triggered
2401 * NISTC_AI_START_SYNC=1 (?)
2402 * NISTC_AI_START_SEL=0 SI_TC
2403 * NISTC_AI_STOP_POLARITY=0 rising edge
2404 * NISTC_AI_STOP_EDGE=0 level
2405 * NISTC_AI_STOP_SYNC=1
2406 * NISTC_AI_STOP_SEL=19 external pin (configuration mem)
2408 start_stop_select |= NISTC_AI_START_EDGE | NISTC_AI_START_SYNC;
2409 ni_stc_writew(dev, start_stop_select, NISTC_AI_START_STOP_REG);
2411 mode2 &= ~NISTC_AI_MODE2_SI_INIT_LOAD_SRC; /* A */
2412 mode2 |= NISTC_AI_MODE2_SI_RELOAD_MODE(0);
2413 /* mode2 |= NISTC_AI_MODE2_SC_RELOAD_MODE; */
2414 ni_stc_writew(dev, mode2, NISTC_AI_MODE2_REG);
2417 timer = ni_ns_to_timer(dev, cmd->scan_begin_arg,
2418 CMDF_ROUND_NEAREST);
2419 ni_stc_writel(dev, timer, NISTC_AI_SI_LOADA_REG);
2420 ni_stc_writew(dev, NISTC_AI_CMD1_SI_LOAD, NISTC_AI_CMD1_REG);
2423 if (cmd->scan_begin_arg & CR_EDGE)
2424 start_stop_select |= NISTC_AI_START_EDGE;
2425 if (cmd->scan_begin_arg & CR_INVERT) /* falling edge */
2426 start_stop_select |= NISTC_AI_START_POLARITY;
2427 if (cmd->scan_begin_src != cmd->convert_src ||
2428 (cmd->scan_begin_arg & ~CR_EDGE) !=
2429 (cmd->convert_arg & ~CR_EDGE))
2430 start_stop_select |= NISTC_AI_START_SYNC;
2431 start_stop_select |=
2432 NISTC_AI_START_SEL(1 + CR_CHAN(cmd->scan_begin_arg));
2433 ni_stc_writew(dev, start_stop_select, NISTC_AI_START_STOP_REG);
2437 switch (cmd->convert_src) {
2440 if (cmd->convert_arg == 0 || cmd->convert_src == TRIG_NOW)
2443 timer = ni_ns_to_timer(dev, cmd->convert_arg,
2444 CMDF_ROUND_NEAREST);
2445 /* 0,0 does not work */
2446 ni_stc_writew(dev, 1, NISTC_AI_SI2_LOADA_REG);
2447 ni_stc_writew(dev, timer, NISTC_AI_SI2_LOADB_REG);
2449 mode2 &= ~NISTC_AI_MODE2_SI2_INIT_LOAD_SRC; /* A */
2450 mode2 |= NISTC_AI_MODE2_SI2_RELOAD_MODE; /* alternate */
2451 ni_stc_writew(dev, mode2, NISTC_AI_MODE2_REG);
2453 ni_stc_writew(dev, NISTC_AI_CMD1_SI2_LOAD, NISTC_AI_CMD1_REG);
2455 mode2 |= NISTC_AI_MODE2_SI2_INIT_LOAD_SRC; /* B */
2456 mode2 |= NISTC_AI_MODE2_SI2_RELOAD_MODE; /* alternate */
2457 ni_stc_writew(dev, mode2, NISTC_AI_MODE2_REG);
2460 mode1 |= NISTC_AI_MODE1_CONVERT_SRC(1 +
2461 CR_CHAN(cmd->convert_arg));
2462 if ((cmd->convert_arg & CR_INVERT) == 0)
2463 mode1 |= NISTC_AI_MODE1_CONVERT_POLARITY;
2464 ni_stc_writew(dev, mode1, NISTC_AI_MODE1_REG);
2466 mode2 |= NISTC_AI_MODE2_SC_GATE_ENA |
2467 NISTC_AI_MODE2_START_STOP_GATE_ENA;
2468 ni_stc_writew(dev, mode2, NISTC_AI_MODE2_REG);
2474 /* interrupt on FIFO, errors, SC_TC */
2475 interrupt_a_enable |= NISTC_INTA_ENA_AI_ERR |
2476 NISTC_INTA_ENA_AI_SC_TC;
2479 interrupt_a_enable |= NISTC_INTA_ENA_AI_FIFO;
2482 if ((cmd->flags & CMDF_WAKE_EOS) ||
2483 (devpriv->ai_cmd2 & NISTC_AI_CMD2_END_ON_EOS)) {
2484 /* wake on end-of-scan */
2485 devpriv->aimode = AIMODE_SCAN;
2487 devpriv->aimode = AIMODE_HALF_FULL;
2490 switch (devpriv->aimode) {
2491 case AIMODE_HALF_FULL:
2492 /*generate FIFO interrupts and DMA requests on half-full */
2494 ni_stc_writew(dev, NISTC_AI_MODE3_FIFO_MODE_HF_E,
2495 NISTC_AI_MODE3_REG);
2497 ni_stc_writew(dev, NISTC_AI_MODE3_FIFO_MODE_HF,
2498 NISTC_AI_MODE3_REG);
2502 /*generate FIFO interrupts on non-empty */
2503 ni_stc_writew(dev, NISTC_AI_MODE3_FIFO_MODE_NE,
2504 NISTC_AI_MODE3_REG);
2508 ni_stc_writew(dev, NISTC_AI_MODE3_FIFO_MODE_NE,
2509 NISTC_AI_MODE3_REG);
2511 ni_stc_writew(dev, NISTC_AI_MODE3_FIFO_MODE_HF,
2512 NISTC_AI_MODE3_REG);
2514 interrupt_a_enable |= NISTC_INTA_ENA_AI_STOP;
2520 /* clear interrupts */
2521 ni_stc_writew(dev, NISTC_INTA_ACK_AI_ALL, NISTC_INTA_ACK_REG);
2523 ni_set_bits(dev, NISTC_INTA_ENA_REG, interrupt_a_enable, 1);
2525 /* interrupt on nothing */
2526 ni_set_bits(dev, NISTC_INTA_ENA_REG, ~0, 0);
2528 /* XXX start polling if necessary */
2531 /* end configuration */
2532 ni_stc_writew(dev, NISTC_RESET_AI_CFG_END, NISTC_RESET_REG);
2534 switch (cmd->scan_begin_src) {
2536 ni_stc_writew(dev, NISTC_AI_CMD1_SI2_ARM |
2537 NISTC_AI_CMD1_SI_ARM |
2538 NISTC_AI_CMD1_DIV_ARM |
2539 NISTC_AI_CMD1_SC_ARM,
2543 ni_stc_writew(dev, NISTC_AI_CMD1_SI2_ARM |
2544 NISTC_AI_CMD1_SI_ARM | /* XXX ? */
2545 NISTC_AI_CMD1_DIV_ARM |
2546 NISTC_AI_CMD1_SC_ARM,
2553 int retval = ni_ai_setup_MITE_dma(dev);
2560 if (cmd->start_src == TRIG_NOW) {
2561 ni_stc_writew(dev, NISTC_AI_CMD2_START1_PULSE |
2564 s->async->inttrig = NULL;
2565 } else if (cmd->start_src == TRIG_EXT) {
2566 s->async->inttrig = NULL;
2567 } else { /* TRIG_INT */
2568 s->async->inttrig = ni_ai_inttrig;
2574 static int ni_ai_insn_config(struct comedi_device *dev,
2575 struct comedi_subdevice *s,
2576 struct comedi_insn *insn, unsigned int *data)
2578 struct ni_private *devpriv = dev->private;
2584 case INSN_CONFIG_ALT_SOURCE:
2585 if (devpriv->is_m_series) {
2586 if (data[1] & ~NI_M_CFG_BYPASS_AI_CAL_MASK)
2588 devpriv->ai_calib_source = data[1];
2589 } else if (devpriv->is_6143) {
2590 unsigned int calib_source;
2592 calib_source = data[1] & 0xf;
2594 devpriv->ai_calib_source = calib_source;
2595 ni_writew(dev, calib_source, NI6143_CALIB_CHAN_REG);
2597 unsigned int calib_source;
2598 unsigned int calib_source_adjust;
2600 calib_source = data[1] & 0xf;
2601 calib_source_adjust = (data[1] >> 4) & 0xff;
2603 if (calib_source >= 8)
2605 devpriv->ai_calib_source = calib_source;
2606 if (devpriv->is_611x) {
2607 ni_writeb(dev, calib_source_adjust,
2608 NI611X_CAL_GAIN_SEL_REG);
2619 static void ni_ao_munge(struct comedi_device *dev, struct comedi_subdevice *s,
2620 void *data, unsigned int num_bytes,
2621 unsigned int chan_index)
2623 struct comedi_cmd *cmd = &s->async->cmd;
2624 unsigned int nsamples = comedi_bytes_to_samples(s, num_bytes);
2625 unsigned short *array = data;
2628 __le16 buf, *barray = data;
2631 for (i = 0; i < nsamples; i++) {
2632 unsigned int range = CR_RANGE(cmd->chanlist[chan_index]);
2633 unsigned short val = array[i];
2636 * Munge data from unsigned to two's complement for
2639 if (comedi_range_is_bipolar(s, range))
2640 val = comedi_offset_munge(s, val);
2642 buf = cpu_to_le16(val);
2648 chan_index %= cmd->chanlist_len;
2652 static int ni_m_series_ao_config_chanlist(struct comedi_device *dev,
2653 struct comedi_subdevice *s,
2654 unsigned int chanspec[],
2655 unsigned int n_chans, int timed)
2657 struct ni_private *devpriv = dev->private;
2665 for (i = 0; i < s->n_chan; ++i) {
2666 devpriv->ao_conf[i] &= ~NI_M_AO_CFG_BANK_UPDATE_TIMED;
2667 ni_writeb(dev, devpriv->ao_conf[i],
2668 NI_M_AO_CFG_BANK_REG(i));
2669 ni_writeb(dev, 0xf, NI_M_AO_WAVEFORM_ORDER_REG(i));
2672 for (i = 0; i < n_chans; i++) {
2673 const struct comedi_krange *krange;
2675 chan = CR_CHAN(chanspec[i]);
2676 range = CR_RANGE(chanspec[i]);
2677 krange = s->range_table->range + range;
2680 switch (krange->max - krange->min) {
2682 conf |= NI_M_AO_CFG_BANK_REF_INT_10V;
2683 ni_writeb(dev, 0, NI_M_AO_REF_ATTENUATION_REG(chan));
2686 conf |= NI_M_AO_CFG_BANK_REF_INT_5V;
2687 ni_writeb(dev, 0, NI_M_AO_REF_ATTENUATION_REG(chan));
2690 conf |= NI_M_AO_CFG_BANK_REF_INT_10V;
2691 ni_writeb(dev, NI_M_AO_REF_ATTENUATION_X5,
2692 NI_M_AO_REF_ATTENUATION_REG(chan));
2695 conf |= NI_M_AO_CFG_BANK_REF_INT_5V;
2696 ni_writeb(dev, NI_M_AO_REF_ATTENUATION_X5,
2697 NI_M_AO_REF_ATTENUATION_REG(chan));
2700 dev_err(dev->class_dev,
2701 "bug! unhandled ao reference voltage\n");
2704 switch (krange->max + krange->min) {
2706 conf |= NI_M_AO_CFG_BANK_OFFSET_0V;
2709 conf |= NI_M_AO_CFG_BANK_OFFSET_5V;
2712 dev_err(dev->class_dev,
2713 "bug! unhandled ao offset voltage\n");
2717 conf |= NI_M_AO_CFG_BANK_UPDATE_TIMED;
2718 ni_writeb(dev, conf, NI_M_AO_CFG_BANK_REG(chan));
2719 devpriv->ao_conf[chan] = conf;
2720 ni_writeb(dev, i, NI_M_AO_WAVEFORM_ORDER_REG(chan));
2725 static int ni_old_ao_config_chanlist(struct comedi_device *dev,
2726 struct comedi_subdevice *s,
2727 unsigned int chanspec[],
2728 unsigned int n_chans)
2730 struct ni_private *devpriv = dev->private;
2737 for (i = 0; i < n_chans; i++) {
2738 chan = CR_CHAN(chanspec[i]);
2739 range = CR_RANGE(chanspec[i]);
2740 conf = NI_E_AO_DACSEL(chan);
2742 if (comedi_range_is_bipolar(s, range)) {
2743 conf |= NI_E_AO_CFG_BIP;
2744 invert = (s->maxdata + 1) >> 1;
2748 if (comedi_range_is_external(s, range))
2749 conf |= NI_E_AO_EXT_REF;
2751 /* not all boards can deglitch, but this shouldn't hurt */
2752 if (chanspec[i] & CR_DEGLITCH)
2753 conf |= NI_E_AO_DEGLITCH;
2755 /* analog reference */
2756 /* AREF_OTHER connects AO ground to AI ground, i think */
2757 if (CR_AREF(chanspec[i]) == AREF_OTHER)
2758 conf |= NI_E_AO_GROUND_REF;
2760 ni_writew(dev, conf, NI_E_AO_CFG_REG);
2761 devpriv->ao_conf[chan] = conf;
2766 static int ni_ao_config_chanlist(struct comedi_device *dev,
2767 struct comedi_subdevice *s,
2768 unsigned int chanspec[], unsigned int n_chans,
2771 struct ni_private *devpriv = dev->private;
2773 if (devpriv->is_m_series)
2774 return ni_m_series_ao_config_chanlist(dev, s, chanspec, n_chans,
2777 return ni_old_ao_config_chanlist(dev, s, chanspec, n_chans);
2780 static int ni_ao_insn_write(struct comedi_device *dev,
2781 struct comedi_subdevice *s,
2782 struct comedi_insn *insn,
2785 struct ni_private *devpriv = dev->private;
2786 unsigned int chan = CR_CHAN(insn->chanspec);
2787 unsigned int range = CR_RANGE(insn->chanspec);
2791 if (devpriv->is_6xxx) {
2792 ni_ao_win_outw(dev, 1 << chan, NI671X_AO_IMMEDIATE_REG);
2794 reg = NI671X_DAC_DIRECT_DATA_REG(chan);
2795 } else if (devpriv->is_m_series) {
2796 reg = NI_M_DAC_DIRECT_DATA_REG(chan);
2798 reg = NI_E_DAC_DIRECT_DATA_REG(chan);
2801 ni_ao_config_chanlist(dev, s, &insn->chanspec, 1, 0);
2803 for (i = 0; i < insn->n; i++) {
2804 unsigned int val = data[i];
2806 s->readback[chan] = val;
2808 if (devpriv->is_6xxx) {
2810 * 6xxx boards have bipolar outputs, munge the
2811 * unsigned comedi values to 2's complement
2813 val = comedi_offset_munge(s, val);
2815 ni_ao_win_outw(dev, val, reg);
2816 } else if (devpriv->is_m_series) {
2818 * M-series boards use offset binary values for
2819 * bipolar and uinpolar outputs
2821 ni_writew(dev, val, reg);
2824 * Non-M series boards need two's complement values
2825 * for bipolar ranges.
2827 if (comedi_range_is_bipolar(s, range))
2828 val = comedi_offset_munge(s, val);
2830 ni_writew(dev, val, reg);
2837 static int ni_ao_insn_config(struct comedi_device *dev,
2838 struct comedi_subdevice *s,
2839 struct comedi_insn *insn, unsigned int *data)
2841 const struct ni_board_struct *board = dev->board_ptr;
2842 struct ni_private *devpriv = dev->private;
2843 unsigned int nbytes;
2846 case INSN_CONFIG_GET_HARDWARE_BUFFER_SIZE:
2849 nbytes = comedi_samples_to_bytes(s,
2850 board->ao_fifo_depth);
2851 data[2] = 1 + nbytes;
2853 data[2] += devpriv->mite->fifo_size;
2869 static int ni_ao_inttrig(struct comedi_device *dev,
2870 struct comedi_subdevice *s,
2871 unsigned int trig_num)
2873 struct ni_private *devpriv = dev->private;
2874 struct comedi_cmd *cmd = &s->async->cmd;
2876 int interrupt_b_bits;
2878 static const int timeout = 1000;
2880 if (trig_num != cmd->start_arg)
2883 /* Null trig at beginning prevent ao start trigger from executing more than
2884 once per command (and doing things like trying to allocate the ao dma channel
2886 s->async->inttrig = NULL;
2888 ni_set_bits(dev, NISTC_INTB_ENA_REG,
2889 NISTC_INTB_ENA_AO_FIFO | NISTC_INTB_ENA_AO_ERR, 0);
2890 interrupt_b_bits = NISTC_INTB_ENA_AO_ERR;
2892 ni_stc_writew(dev, 1, NISTC_DAC_FIFO_CLR_REG);
2893 if (devpriv->is_6xxx)
2894 ni_ao_win_outl(dev, 0x6, NI611X_AO_FIFO_OFFSET_LOAD_REG);
2895 ret = ni_ao_setup_MITE_dma(dev);
2898 ret = ni_ao_wait_for_dma_load(dev);
2902 ret = ni_ao_prep_fifo(dev, s);
2906 interrupt_b_bits |= NISTC_INTB_ENA_AO_FIFO;
2909 ni_stc_writew(dev, devpriv->ao_mode3 | NISTC_AO_MODE3_NOT_AN_UPDATE,
2910 NISTC_AO_MODE3_REG);
2911 ni_stc_writew(dev, devpriv->ao_mode3, NISTC_AO_MODE3_REG);
2912 /* wait for DACs to be loaded */
2913 for (i = 0; i < timeout; i++) {
2915 if ((ni_stc_readw(dev, NISTC_STATUS2_REG) &
2916 NISTC_STATUS2_AO_TMRDACWRS_IN_PROGRESS) == 0)
2920 dev_err(dev->class_dev,
2921 "timed out waiting for AO_TMRDACWRs_In_Progress_St to clear\n");
2925 * stc manual says we are need to clear error interrupt after
2926 * AO_TMRDACWRs_In_Progress_St clears
2928 ni_stc_writew(dev, NISTC_INTB_ACK_AO_ERR, NISTC_INTB_ACK_REG);
2930 ni_set_bits(dev, NISTC_INTB_ENA_REG, interrupt_b_bits, 1);
2932 ni_stc_writew(dev, NISTC_AO_CMD1_UI_ARM |
2933 NISTC_AO_CMD1_UC_ARM |
2934 NISTC_AO_CMD1_BC_ARM |
2938 ni_stc_writew(dev, NISTC_AO_CMD2_START1_PULSE | devpriv->ao_cmd2,
2946 * Organized similar to NI-STC and MHDDK examples.
2947 * ni_ao_cmd is broken out into configuration sub-routines for clarity.
2950 static void ni_ao_cmd_personalize(struct comedi_device *dev,
2951 const struct comedi_cmd *cmd)
2953 const struct ni_board_struct *board = dev->board_ptr;
2956 ni_stc_writew(dev, NISTC_RESET_AO_CFG_START, NISTC_RESET_REG);
2959 /* fast CPU interface--only eseries */
2960 /* ((slow CPU interface) ? 0 : AO_Fast_CPU) | */
2961 NISTC_AO_PERSONAL_BC_SRC_SEL |
2962 0 /* (use_original_pulse ? 0 : NISTC_AO_PERSONAL_UPDATE_TIMEBASE) */ |
2964 * FIXME: start setting following bit when appropriate. Need to
2965 * determine whether board is E4 or E1.
2967 * if board is E4 or E1
2968 * Set bit "NISTC_AO_PERSONAL_UPDATE_PW" to 0
2972 NISTC_AO_PERSONAL_UPDATE_PW |
2973 /* FIXME: when should we set following bit to zero? */
2974 NISTC_AO_PERSONAL_TMRDACWR_PW |
2975 (board->ao_fifo_depth ?
2976 NISTC_AO_PERSONAL_FIFO_ENA : NISTC_AO_PERSONAL_DMA_PIO_CTRL)
2981 * add something like ".has_individual_dacs = 0" to ni_board_struct
2982 * since, as F Hess pointed out, not all in m series have singles. not
2983 * sure if e-series all have duals...
2987 * F Hess: windows driver does not set NISTC_AO_PERSONAL_NUM_DAC bit for
2988 * 6281, verified with bus analyzer.
2990 if (devpriv->is_m_series)
2991 bits |= NISTC_AO_PERSONAL_NUM_DAC;
2993 ni_stc_writew(dev, bits, NISTC_AO_PERSONAL_REG);
2995 ni_stc_writew(dev, NISTC_RESET_AO_CFG_END, NISTC_RESET_REG);
2998 static void ni_ao_cmd_set_trigger(struct comedi_device *dev,
2999 const struct comedi_cmd *cmd)
3001 struct ni_private *devpriv = dev->private;
3003 ni_stc_writew(dev, NISTC_RESET_AO_CFG_START, NISTC_RESET_REG);
3006 if (cmd->stop_src == TRIG_NONE) {
3007 devpriv->ao_mode1 |= NISTC_AO_MODE1_CONTINUOUS;
3008 devpriv->ao_mode1 &= ~NISTC_AO_MODE1_TRIGGER_ONCE;
3010 devpriv->ao_mode1 &= ~NISTC_AO_MODE1_CONTINUOUS;
3011 devpriv->ao_mode1 |= NISTC_AO_MODE1_TRIGGER_ONCE;
3013 ni_stc_writew(dev, devpriv->ao_mode1, NISTC_AO_MODE1_REG);
3016 unsigned int trigsel = devpriv->ao_trigger_select;
3018 switch (cmd->start_src) {
3021 trigsel &= ~(NISTC_AO_TRIG_START1_POLARITY |
3022 NISTC_AO_TRIG_START1_SEL_MASK);
3023 trigsel |= NISTC_AO_TRIG_START1_EDGE |
3024 NISTC_AO_TRIG_START1_SYNC;
3027 trigsel = NISTC_AO_TRIG_START1_SEL(
3028 CR_CHAN(cmd->start_arg) + 1);
3029 if (cmd->start_arg & CR_INVERT)
3031 * 0=active high, 1=active low.
3032 * see daq-stc 3-24 (p186)
3034 trigsel |= NISTC_AO_TRIG_START1_POLARITY;
3035 if (cmd->start_arg & CR_EDGE)
3036 /* 0=edge detection disabled, 1=enabled */
3037 trigsel |= NISTC_AO_TRIG_START1_EDGE;
3044 devpriv->ao_trigger_select = trigsel;
3045 ni_stc_writew(dev, devpriv->ao_trigger_select,
3046 NISTC_AO_TRIG_SEL_REG);
3048 /* AO_Delayed_START1 = 0, we do not support delayed start...yet */
3051 /* select DA_START1 as PFI6/AO_START1 when configured as an output */
3052 devpriv->ao_mode3 &= ~NISTC_AO_MODE3_TRIG_LEN;
3053 ni_stc_writew(dev, devpriv->ao_mode3, NISTC_AO_MODE3_REG);
3055 ni_stc_writew(dev, NISTC_RESET_AO_CFG_END, NISTC_RESET_REG);
3058 static void ni_ao_cmd_set_counters(struct comedi_device *dev,
3059 const struct comedi_cmd *cmd)
3061 struct ni_private *devpriv = dev->private;
3062 /* Not supporting 'waveform staging' or 'local buffer with pauses' */
3064 ni_stc_writew(dev, NISTC_RESET_AO_CFG_START, NISTC_RESET_REG);
3066 * This relies on ao_mode1/(Trigger_Once | Continuous) being set in
3067 * set_trigger above. It is unclear whether we really need to re-write
3068 * this register with these values. The mhddk examples for e-series
3069 * show writing this in both places, but the examples for m-series show
3070 * a single write in the set_counters function (here).
3072 ni_stc_writew(dev, devpriv->ao_mode1, NISTC_AO_MODE1_REG);
3074 /* sync (upload number of buffer iterations -1) */
3075 /* indicate that we want to use BC_Load_A_Register as the source */
3076 devpriv->ao_mode2 &= ~NISTC_AO_MODE2_BC_INIT_LOAD_SRC;
3077 ni_stc_writew(dev, devpriv->ao_mode2, NISTC_AO_MODE2_REG);
3080 * if the BC_TC interrupt is still issued in spite of UC, BC, UI
3081 * ignoring BC_TC, then we will need to find a way to ignore that
3082 * interrupt in continuous mode.
3084 ni_stc_writel(dev, 0, NISTC_AO_BC_LOADA_REG); /* iter once */
3086 /* sync (issue command to load number of buffer iterations -1) */
3087 ni_stc_writew(dev, NISTC_AO_CMD1_BC_LOAD, NISTC_AO_CMD1_REG);
3089 /* sync (upload number of updates in buffer) */
3090 /* indicate that we want to use UC_Load_A_Register as the source */
3091 devpriv->ao_mode2 &= ~NISTC_AO_MODE2_UC_INIT_LOAD_SRC;
3092 ni_stc_writew(dev, devpriv->ao_mode2, NISTC_AO_MODE2_REG);
3095 * if a user specifies '0', this automatically assumes the entire 24bit
3096 * address space is available for the (multiple iterations of single
3097 * buffer) MISB. Otherwise, stop_arg specifies the MISB length that
3098 * will be used, regardless of whether we are in continuous mode or not.
3099 * In continuous mode, the output will just iterate indefinitely over
3103 unsigned int stop_arg = cmd->stop_arg > 0 ?
3104 (cmd->stop_arg & 0xffffff) : 0xffffff;
3106 if (devpriv->is_m_series) {
3108 * this is how the NI example code does it for m-series
3109 * boards, verified correct with 6259
3111 ni_stc_writel(dev, stop_arg - 1, NISTC_AO_UC_LOADA_REG);
3113 /* sync (issue cmd to load number of updates in MISB) */
3114 ni_stc_writew(dev, NISTC_AO_CMD1_UC_LOAD,
3117 ni_stc_writel(dev, stop_arg, NISTC_AO_UC_LOADA_REG);
3119 /* sync (issue cmd to load number of updates in MISB) */
3120 ni_stc_writew(dev, NISTC_AO_CMD1_UC_LOAD,
3124 * sync (upload number of updates-1 in MISB)
3127 ni_stc_writel(dev, stop_arg - 1, NISTC_AO_UC_LOADA_REG);
3131 ni_stc_writew(dev, NISTC_RESET_AO_CFG_END, NISTC_RESET_REG);
3134 static void ni_ao_cmd_set_update(struct comedi_device *dev,
3135 const struct comedi_cmd *cmd)
3137 struct ni_private *devpriv = dev->private;
3139 ni_stc_writew(dev, NISTC_RESET_AO_CFG_START, NISTC_RESET_REG);
3142 * zero out these bit fields to be set below. Does an ao-reset do this
3145 devpriv->ao_mode1 &= ~(
3146 NISTC_AO_MODE1_UI_SRC_MASK |
3147 NISTC_AO_MODE1_UI_SRC_POLARITY |
3148 NISTC_AO_MODE1_UPDATE_SRC_MASK |
3149 NISTC_AO_MODE1_UPDATE_SRC_POLARITY
3152 switch (cmd->scan_begin_src) {
3154 devpriv->ao_cmd2 &= ~NISTC_AO_CMD2_BC_GATE_ENA;
3157 * NOTE: there are several other ways of configuring internal
3158 * updates, but we'll only support one for now: using
3159 * AO_IN_TIMEBASE, w/o waveform staging, w/o a delay between
3160 * START1 and first update, and also w/o local buffer mode w/
3165 * This is already done above:
3166 * devpriv->ao_mode1 &= ~(
3167 * // set UPDATE_Source to UI_TC:
3168 * NISTC_AO_MODE1_UPDATE_SRC_MASK |
3169 * // set UPDATE_Source_Polarity to rising (required?)
3170 * NISTC_AO_MODE1_UPDATE_SRC_POLARITY |
3171 * // set UI_Source to AO_IN_TIMEBASE1:
3172 * NISTC_AO_MODE1_UI_SRC_MASK |
3173 * // set UI_Source_Polarity to rising (required?)
3174 * NISTC_AO_MODE1_UI_SRC_POLARITY
3179 * TODO: use ao_ui_clock_source to allow all possible signals
3180 * to be routed to UI_Source_Select. See tSTC.h for
3181 * eseries/ni67xx and tMSeries.h for mseries.
3185 unsigned trigvar = ni_ns_to_timer(dev,
3186 cmd->scan_begin_arg,
3187 CMDF_ROUND_NEAREST);
3190 * Wait N TB3 ticks after the start trigger before
3191 * clocking(N must be >=2).
3193 /* following line: 2-1 per STC */
3194 ni_stc_writel(dev, 1, NISTC_AO_UI_LOADA_REG);
3195 ni_stc_writew(dev, NISTC_AO_CMD1_UI_LOAD,
3197 /* following line: N-1 per STC */
3198 ni_stc_writel(dev, trigvar - 1, NISTC_AO_UI_LOADA_REG);
3202 /* FIXME: assert scan_begin_arg != 0, ret failure otherwise */
3203 devpriv->ao_cmd2 |= NISTC_AO_CMD2_BC_GATE_ENA;
3204 devpriv->ao_mode1 |= NISTC_AO_MODE1_UPDATE_SRC(
3205 CR_CHAN(cmd->scan_begin_arg));
3206 if (cmd->scan_begin_arg & CR_INVERT)
3207 devpriv->ao_mode1 |= NISTC_AO_MODE1_UPDATE_SRC_POLARITY;
3214 ni_stc_writew(dev, devpriv->ao_cmd2, NISTC_AO_CMD2_REG);
3215 ni_stc_writew(dev, devpriv->ao_mode1, NISTC_AO_MODE1_REG);
3216 devpriv->ao_mode2 &= ~(NISTC_AO_MODE2_UI_RELOAD_MODE(3) |
3217 NISTC_AO_MODE2_UI_INIT_LOAD_SRC);
3218 ni_stc_writew(dev, devpriv->ao_mode2, NISTC_AO_MODE2_REG);
3220 /* Configure DAQ-STC for Timed update mode */
3221 devpriv->ao_cmd1 |= NISTC_AO_CMD1_DAC1_UPDATE_MODE |
3222 NISTC_AO_CMD1_DAC0_UPDATE_MODE;
3223 /* We are not using UPDATE2-->don't have to set DACx_Source_Select */
3224 ni_stc_writew(dev, devpriv->ao_cmd1, NISTC_AO_CMD1_REG);
3226 ni_stc_writew(dev, NISTC_RESET_AO_CFG_END, NISTC_RESET_REG);
3229 static void ni_ao_cmd_set_channels(struct comedi_device *dev,
3230 struct comedi_subdevice *s)
3232 struct ni_private *devpriv = dev->private;
3233 const struct comedi_cmd *cmd = &s->async->cmd;
3236 ni_stc_writew(dev, NISTC_RESET_AO_CFG_START, NISTC_RESET_REG);
3238 if (devpriv->is_6xxx) {
3242 for (i = 0; i < cmd->chanlist_len; ++i) {
3243 int chan = CR_CHAN(cmd->chanlist[i]);
3246 ni_ao_win_outw(dev, chan, NI611X_AO_WAVEFORM_GEN_REG);
3248 ni_ao_win_outw(dev, bits, NI611X_AO_TIMED_REG);
3251 ni_ao_config_chanlist(dev, s, cmd->chanlist, cmd->chanlist_len, 1);
3253 if (cmd->scan_end_arg > 1) {
3254 devpriv->ao_mode1 |= NISTC_AO_MODE1_MULTI_CHAN;
3255 bits = NISTC_AO_OUT_CTRL_CHANS(cmd->scan_end_arg - 1)
3256 | NISTC_AO_OUT_CTRL_UPDATE_SEL_HIGHZ;
3259 devpriv->ao_mode1 &= ~NISTC_AO_MODE1_MULTI_CHAN;
3260 bits = NISTC_AO_OUT_CTRL_UPDATE_SEL_HIGHZ;
3261 if (devpriv->is_m_series | devpriv->is_6xxx)
3262 bits |= NISTC_AO_OUT_CTRL_CHANS(0);
3264 bits |= NISTC_AO_OUT_CTRL_CHANS(
3265 CR_CHAN(cmd->chanlist[0]));
3268 ni_stc_writew(dev, devpriv->ao_mode1, NISTC_AO_MODE1_REG);
3269 ni_stc_writew(dev, bits, NISTC_AO_OUT_CTRL_REG);
3271 ni_stc_writew(dev, NISTC_RESET_AO_CFG_END, NISTC_RESET_REG);
3274 static void ni_ao_cmd_set_stop_conditions(struct comedi_device *dev,
3275 const struct comedi_cmd *cmd)
3277 struct ni_private *devpriv = dev->private;
3279 ni_stc_writew(dev, NISTC_RESET_AO_CFG_START, NISTC_RESET_REG);
3281 devpriv->ao_mode3 |= NISTC_AO_MODE3_STOP_ON_OVERRUN_ERR;
3282 ni_stc_writew(dev, devpriv->ao_mode3, NISTC_AO_MODE3_REG);
3285 * Since we are not supporting waveform staging, we ignore these errors:
3286 * NISTC_AO_MODE3_STOP_ON_BC_TC_ERR,
3287 * NISTC_AO_MODE3_STOP_ON_BC_TC_TRIG_ERR
3290 ni_stc_writew(dev, NISTC_RESET_AO_CFG_END, NISTC_RESET_REG);
3293 static void ni_ao_cmd_set_fifo_mode(struct comedi_device *dev)
3295 struct ni_private *devpriv = dev->private;
3297 ni_stc_writew(dev, NISTC_RESET_AO_CFG_START, NISTC_RESET_REG);
3299 devpriv->ao_mode2 &= ~NISTC_AO_MODE2_FIFO_MODE_MASK;
3301 devpriv->ao_mode2 |= NISTC_AO_MODE2_FIFO_MODE_HF_F;
3303 devpriv->ao_mode2 |= NISTC_AO_MODE2_FIFO_MODE_HF;
3305 /* NOTE: this is where use_onboard_memory=True would be implemented */
3306 devpriv->ao_mode2 &= ~NISTC_AO_MODE2_FIFO_REXMIT_ENA;
3307 ni_stc_writew(dev, devpriv->ao_mode2, NISTC_AO_MODE2_REG);
3309 /* enable sending of ao fifo requests (dma request) */
3310 ni_stc_writew(dev, NISTC_AO_START_AOFREQ_ENA, NISTC_AO_START_SEL_REG);
3312 ni_stc_writew(dev, NISTC_RESET_AO_CFG_END, NISTC_RESET_REG);
3314 /* we are not supporting boards with virtual fifos */
3317 static void ni_ao_cmd_set_interrupts(struct comedi_device *dev,
3318 struct comedi_subdevice *s)
3320 if (s->async->cmd.stop_src == TRIG_COUNT)
3321 ni_set_bits(dev, NISTC_INTB_ENA_REG,
3322 NISTC_INTB_ENA_AO_BC_TC, 1);
3324 s->async->inttrig = ni_ao_inttrig;
3327 static int ni_ao_cmd(struct comedi_device *dev, struct comedi_subdevice *s)
3329 struct ni_private *devpriv = dev->private;
3330 const struct comedi_cmd *cmd = &s->async->cmd;
3332 if (dev->irq == 0) {
3333 dev_err(dev->class_dev, "cannot run command without an irq");
3337 /* ni_ao_reset should have already been done */
3338 ni_ao_cmd_personalize(dev, cmd);
3339 /* clearing fifo and preload happens elsewhere */
3341 ni_ao_cmd_set_trigger(dev, cmd);
3342 ni_ao_cmd_set_counters(dev, cmd);
3343 ni_ao_cmd_set_update(dev, cmd);
3344 ni_ao_cmd_set_channels(dev, s);
3345 ni_ao_cmd_set_stop_conditions(dev, cmd);
3346 ni_ao_cmd_set_fifo_mode(dev);
3347 ni_cmd_set_mite_transfer(devpriv->ao_mite_ring, s, cmd, 0x00ffffff);
3348 ni_ao_cmd_set_interrupts(dev, s);
3351 * arm(ing) and star(ting) happen in ni_ao_inttrig, which _must_ be
3352 * called for ao commands since 1) TRIG_NOW is not supported and 2) DMA
3353 * must be setup and initially written to before arm/start happen.
3360 static int ni_ao_cmdtest(struct comedi_device *dev, struct comedi_subdevice *s,
3361 struct comedi_cmd *cmd)
3363 const struct ni_board_struct *board = dev->board_ptr;
3364 struct ni_private *devpriv = dev->private;
3368 /* Step 1 : check if triggers are trivially valid */
3370 err |= comedi_check_trigger_src(&cmd->start_src, TRIG_INT | TRIG_EXT);
3371 err |= comedi_check_trigger_src(&cmd->scan_begin_src,
3372 TRIG_TIMER | TRIG_EXT);
3373 err |= comedi_check_trigger_src(&cmd->convert_src, TRIG_NOW);
3374 err |= comedi_check_trigger_src(&cmd->scan_end_src, TRIG_COUNT);
3375 err |= comedi_check_trigger_src(&cmd->stop_src, TRIG_COUNT | TRIG_NONE);
3380 /* Step 2a : make sure trigger sources are unique */
3382 err |= comedi_check_trigger_is_unique(cmd->start_src);
3383 err |= comedi_check_trigger_is_unique(cmd->scan_begin_src);
3384 err |= comedi_check_trigger_is_unique(cmd->stop_src);
3386 /* Step 2b : and mutually compatible */
3391 /* Step 3: check if arguments are trivially valid */
3393 switch (cmd->start_src) {
3395 err |= comedi_check_trigger_arg_is(&cmd->start_arg, 0);
3398 tmp = CR_CHAN(cmd->start_arg);
3402 tmp |= (cmd->start_arg & (CR_INVERT | CR_EDGE));
3403 err |= comedi_check_trigger_arg_is(&cmd->start_arg, tmp);
3407 if (cmd->scan_begin_src == TRIG_TIMER) {
3408 err |= comedi_check_trigger_arg_min(&cmd->scan_begin_arg,
3410 err |= comedi_check_trigger_arg_max(&cmd->scan_begin_arg,
3415 err |= comedi_check_trigger_arg_is(&cmd->convert_arg, 0);
3416 err |= comedi_check_trigger_arg_is(&cmd->scan_end_arg,
3418 err |= comedi_check_trigger_arg_max(&cmd->stop_arg, 0x00ffffff);
3423 /* step 4: fix up any arguments */
3424 if (cmd->scan_begin_src == TRIG_TIMER) {
3425 tmp = cmd->scan_begin_arg;
3426 cmd->scan_begin_arg =
3427 ni_timer_to_ns(dev, ni_ns_to_timer(dev,
3428 cmd->scan_begin_arg,
3430 if (tmp != cmd->scan_begin_arg)
3439 static int ni_ao_reset(struct comedi_device *dev, struct comedi_subdevice *s)
3441 /* See 3.6.1.2 "Resetting", of DAQ-STC Technical Reference Manual */
3444 * In the following, the "--sync" comments are meant to denote
3445 * asynchronous boundaries for setting the registers as described in the
3446 * DAQ-STC mostly in the order also described in the DAQ-STC.
3449 struct ni_private *devpriv = dev->private;
3451 ni_release_ao_mite_channel(dev);
3453 /* --sync (reset AO) */
3454 if (devpriv->is_m_series)
3455 /* following example in mhddk for m-series */
3456 ni_stc_writew(dev, NISTC_RESET_AO, NISTC_RESET_REG);
3458 /*--sync (start config) */
3459 ni_stc_writew(dev, NISTC_RESET_AO_CFG_START, NISTC_RESET_REG);
3461 /*--sync (Disarm) */
3462 ni_stc_writew(dev, NISTC_AO_CMD1_DISARM, NISTC_AO_CMD1_REG);
3466 * (clear bunch of registers--mseries mhddk examples do not include
3469 devpriv->ao_cmd1 = 0;
3470 devpriv->ao_cmd2 = 0;
3471 devpriv->ao_mode1 = 0;
3472 devpriv->ao_mode2 = 0;
3473 if (devpriv->is_m_series)
3474 devpriv->ao_mode3 = NISTC_AO_MODE3_LAST_GATE_DISABLE;
3476 devpriv->ao_mode3 = 0;
3477 devpriv->ao_trigger_select = 0;
3479 ni_stc_writew(dev, 0, NISTC_AO_PERSONAL_REG);
3480 ni_stc_writew(dev, 0, NISTC_AO_CMD1_REG);
3481 ni_stc_writew(dev, 0, NISTC_AO_CMD2_REG);
3482 ni_stc_writew(dev, 0, NISTC_AO_MODE1_REG);
3483 ni_stc_writew(dev, 0, NISTC_AO_MODE2_REG);
3484 ni_stc_writew(dev, 0, NISTC_AO_OUT_CTRL_REG);
3485 ni_stc_writew(dev, devpriv->ao_mode3, NISTC_AO_MODE3_REG);
3486 ni_stc_writew(dev, 0, NISTC_AO_START_SEL_REG);
3487 ni_stc_writew(dev, 0, NISTC_AO_TRIG_SEL_REG);
3489 /*--sync (disable interrupts) */
3490 ni_set_bits(dev, NISTC_INTB_ENA_REG, ~0, 0);
3493 ni_stc_writew(dev, NISTC_AO_PERSONAL_BC_SRC_SEL, NISTC_AO_PERSONAL_REG);
3494 ni_stc_writew(dev, NISTC_INTB_ACK_AO_ALL, NISTC_INTB_ACK_REG);
3496 /*--not in DAQ-STC. which doc? */
3497 if (devpriv->is_6xxx) {
3498 ni_ao_win_outw(dev, (1u << s->n_chan) - 1u,
3499 NI671X_AO_IMMEDIATE_REG);
3500 ni_ao_win_outw(dev, NI611X_AO_MISC_CLEAR_WG,
3501 NI611X_AO_MISC_REG);
3503 ni_stc_writew(dev, NISTC_RESET_AO_CFG_END, NISTC_RESET_REG);
3511 static int ni_dio_insn_config(struct comedi_device *dev,
3512 struct comedi_subdevice *s,
3513 struct comedi_insn *insn,
3516 struct ni_private *devpriv = dev->private;
3519 ret = comedi_dio_insn_config(dev, s, insn, data, 0);
3523 devpriv->dio_control &= ~NISTC_DIO_CTRL_DIR_MASK;
3524 devpriv->dio_control |= NISTC_DIO_CTRL_DIR(s->io_bits);
3525 ni_stc_writew(dev, devpriv->dio_control, NISTC_DIO_CTRL_REG);
3530 static int ni_dio_insn_bits(struct comedi_device *dev,
3531 struct comedi_subdevice *s,
3532 struct comedi_insn *insn,
3535 struct ni_private *devpriv = dev->private;
3537 /* Make sure we're not using the serial part of the dio */
3538 if ((data[0] & (NISTC_DIO_SDIN | NISTC_DIO_SDOUT)) &&
3539 devpriv->serial_interval_ns)
3542 if (comedi_dio_update_state(s, data)) {
3543 devpriv->dio_output &= ~NISTC_DIO_OUT_PARALLEL_MASK;
3544 devpriv->dio_output |= NISTC_DIO_OUT_PARALLEL(s->state);
3545 ni_stc_writew(dev, devpriv->dio_output, NISTC_DIO_OUT_REG);
3548 data[1] = ni_stc_readw(dev, NISTC_DIO_IN_REG);
3553 static int ni_m_series_dio_insn_config(struct comedi_device *dev,
3554 struct comedi_subdevice *s,
3555 struct comedi_insn *insn,
3560 ret = comedi_dio_insn_config(dev, s, insn, data, 0);
3564 ni_writel(dev, s->io_bits, NI_M_DIO_DIR_REG);
3569 static int ni_m_series_dio_insn_bits(struct comedi_device *dev,
3570 struct comedi_subdevice *s,
3571 struct comedi_insn *insn,
3574 if (comedi_dio_update_state(s, data))
3575 ni_writel(dev, s->state, NI_M_DIO_REG);
3577 data[1] = ni_readl(dev, NI_M_DIO_REG);
3582 static int ni_cdio_check_chanlist(struct comedi_device *dev,
3583 struct comedi_subdevice *s,
3584 struct comedi_cmd *cmd)
3588 for (i = 0; i < cmd->chanlist_len; ++i) {
3589 unsigned int chan = CR_CHAN(cmd->chanlist[i]);
3598 static int ni_cdio_cmdtest(struct comedi_device *dev,
3599 struct comedi_subdevice *s, struct comedi_cmd *cmd)
3604 /* Step 1 : check if triggers are trivially valid */
3606 err |= comedi_check_trigger_src(&cmd->start_src, TRIG_INT);
3607 err |= comedi_check_trigger_src(&cmd->scan_begin_src, TRIG_EXT);
3608 err |= comedi_check_trigger_src(&cmd->convert_src, TRIG_NOW);
3609 err |= comedi_check_trigger_src(&cmd->scan_end_src, TRIG_COUNT);
3610 err |= comedi_check_trigger_src(&cmd->stop_src, TRIG_NONE);
3615 /* Step 2a : make sure trigger sources are unique */
3616 /* Step 2b : and mutually compatible */
3618 /* Step 3: check if arguments are trivially valid */
3620 err |= comedi_check_trigger_arg_is(&cmd->start_arg, 0);
3622 tmp = cmd->scan_begin_arg;
3623 tmp &= CR_PACK_FLAGS(NI_M_CDO_MODE_SAMPLE_SRC_MASK, 0, 0, CR_INVERT);
3624 if (tmp != cmd->scan_begin_arg)
3627 err |= comedi_check_trigger_arg_is(&cmd->convert_arg, 0);
3628 err |= comedi_check_trigger_arg_is(&cmd->scan_end_arg,
3630 err |= comedi_check_trigger_arg_max(&cmd->stop_arg,
3631 s->async->prealloc_bufsz /
3632 comedi_bytes_per_scan(s));
3637 /* Step 4: fix up any arguments */
3639 /* Step 5: check channel list if it exists */
3641 if (cmd->chanlist && cmd->chanlist_len > 0)
3642 err |= ni_cdio_check_chanlist(dev, s, cmd);
3650 static int ni_cdo_inttrig(struct comedi_device *dev,
3651 struct comedi_subdevice *s,
3652 unsigned int trig_num)
3654 struct comedi_cmd *cmd = &s->async->cmd;
3655 const unsigned timeout = 1000;
3659 struct ni_private *devpriv = dev->private;
3660 unsigned long flags;
3663 if (trig_num != cmd->start_arg)
3666 s->async->inttrig = NULL;
3668 /* read alloc the entire buffer */
3669 comedi_buf_read_alloc(s, s->async->prealloc_bufsz);
3672 spin_lock_irqsave(&devpriv->mite_channel_lock, flags);
3673 if (devpriv->cdo_mite_chan) {
3674 mite_prep_dma(devpriv->cdo_mite_chan, 32, 32);
3675 mite_dma_arm(devpriv->cdo_mite_chan);
3677 dev_err(dev->class_dev, "BUG: no cdo mite channel?\n");
3680 spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
3685 * XXX not sure what interrupt C group does
3686 * wait for dma to fill output fifo
3687 * ni_writeb(dev, NI_M_INTC_ENA, NI_M_INTC_ENA_REG);
3689 for (i = 0; i < timeout; ++i) {
3690 if (ni_readl(dev, NI_M_CDIO_STATUS_REG) &
3691 NI_M_CDIO_STATUS_CDO_FIFO_FULL)
3696 dev_err(dev->class_dev, "dma failed to fill cdo fifo!\n");
3700 ni_writel(dev, NI_M_CDO_CMD_ARM |
3701 NI_M_CDO_CMD_ERR_INT_ENA_SET |
3702 NI_M_CDO_CMD_F_E_INT_ENA_SET,
3707 static int ni_cdio_cmd(struct comedi_device *dev, struct comedi_subdevice *s)
3709 struct ni_private *devpriv = dev->private;
3710 const struct comedi_cmd *cmd = &s->async->cmd;
3711 unsigned cdo_mode_bits;
3714 ni_writel(dev, NI_M_CDO_CMD_RESET, NI_M_CDIO_CMD_REG);
3715 cdo_mode_bits = NI_M_CDO_MODE_FIFO_MODE |
3716 NI_M_CDO_MODE_HALT_ON_ERROR |
3717 NI_M_CDO_MODE_SAMPLE_SRC(CR_CHAN(cmd->scan_begin_arg));
3718 if (cmd->scan_begin_arg & CR_INVERT)
3719 cdo_mode_bits |= NI_M_CDO_MODE_POLARITY;
3720 ni_writel(dev, cdo_mode_bits, NI_M_CDO_MODE_REG);
3722 ni_writel(dev, s->state, NI_M_CDO_FIFO_DATA_REG);
3723 ni_writel(dev, NI_M_CDO_CMD_SW_UPDATE, NI_M_CDIO_CMD_REG);
3724 ni_writel(dev, s->io_bits, NI_M_CDO_MASK_ENA_REG);
3726 dev_err(dev->class_dev,
3727 "attempted to run digital output command with no lines configured as outputs\n");
3730 retval = ni_request_cdo_mite_channel(dev);
3734 ni_cmd_set_mite_transfer(devpriv->cdo_mite_ring, s, cmd,
3735 s->async->prealloc_bufsz /
3736 comedi_bytes_per_scan(s));
3738 s->async->inttrig = ni_cdo_inttrig;
3743 static int ni_cdio_cancel(struct comedi_device *dev, struct comedi_subdevice *s)
3745 ni_writel(dev, NI_M_CDO_CMD_DISARM |
3746 NI_M_CDO_CMD_ERR_INT_ENA_CLR |
3747 NI_M_CDO_CMD_F_E_INT_ENA_CLR |
3748 NI_M_CDO_CMD_F_REQ_INT_ENA_CLR,
3751 * XXX not sure what interrupt C group does
3752 * ni_writeb(dev, 0, NI_M_INTC_ENA_REG);
3754 ni_writel(dev, 0, NI_M_CDO_MASK_ENA_REG);
3755 ni_release_cdo_mite_channel(dev);
3759 static void handle_cdio_interrupt(struct comedi_device *dev)
3761 struct ni_private *devpriv = dev->private;
3762 unsigned cdio_status;
3763 struct comedi_subdevice *s = &dev->subdevices[NI_DIO_SUBDEV];
3765 unsigned long flags;
3768 if (!devpriv->is_m_series)
3771 spin_lock_irqsave(&devpriv->mite_channel_lock, flags);
3772 if (devpriv->cdo_mite_chan) {
3773 unsigned cdo_mite_status =
3774 mite_get_status(devpriv->cdo_mite_chan);
3775 if (cdo_mite_status & CHSR_LINKC) {
3777 devpriv->mite->mite_io_addr +
3778 MITE_CHOR(devpriv->cdo_mite_chan->channel));
3780 mite_sync_output_dma(devpriv->cdo_mite_chan, s);
3782 spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
3785 cdio_status = ni_readl(dev, NI_M_CDIO_STATUS_REG);
3786 if (cdio_status & NI_M_CDIO_STATUS_CDO_ERROR) {
3787 /* XXX just guessing this is needed and does something useful */
3788 ni_writel(dev, NI_M_CDO_CMD_ERR_INT_CONFIRM,
3790 s->async->events |= COMEDI_CB_OVERFLOW;
3792 if (cdio_status & NI_M_CDIO_STATUS_CDO_FIFO_EMPTY) {
3793 ni_writel(dev, NI_M_CDO_CMD_F_E_INT_ENA_CLR,
3795 /* s->async->events |= COMEDI_CB_EOA; */
3797 comedi_handle_events(dev, s);
3800 static int ni_serial_hw_readwrite8(struct comedi_device *dev,
3801 struct comedi_subdevice *s,
3802 unsigned char data_out,
3803 unsigned char *data_in)
3805 struct ni_private *devpriv = dev->private;
3806 unsigned int status1;
3807 int err = 0, count = 20;
3809 devpriv->dio_output &= ~NISTC_DIO_OUT_SERIAL_MASK;
3810 devpriv->dio_output |= NISTC_DIO_OUT_SERIAL(data_out);
3811 ni_stc_writew(dev, devpriv->dio_output, NISTC_DIO_OUT_REG);
3813 status1 = ni_stc_readw(dev, NISTC_STATUS1_REG);
3814 if (status1 & NISTC_STATUS1_SERIO_IN_PROG) {
3819 devpriv->dio_control |= NISTC_DIO_CTRL_HW_SER_START;
3820 ni_stc_writew(dev, devpriv->dio_control, NISTC_DIO_CTRL_REG);
3821 devpriv->dio_control &= ~NISTC_DIO_CTRL_HW_SER_START;
3823 /* Wait until STC says we're done, but don't loop infinitely. */
3824 while ((status1 = ni_stc_readw(dev, NISTC_STATUS1_REG)) &
3825 NISTC_STATUS1_SERIO_IN_PROG) {
3826 /* Delay one bit per loop */
3827 udelay((devpriv->serial_interval_ns + 999) / 1000);
3829 dev_err(dev->class_dev,
3830 "SPI serial I/O didn't finish in time!\n");
3837 * Delay for last bit. This delay is absolutely necessary, because
3838 * NISTC_STATUS1_SERIO_IN_PROG goes high one bit too early.
3840 udelay((devpriv->serial_interval_ns + 999) / 1000);
3843 *data_in = ni_stc_readw(dev, NISTC_DIO_SERIAL_IN_REG);
3846 ni_stc_writew(dev, devpriv->dio_control, NISTC_DIO_CTRL_REG);
3851 static int ni_serial_sw_readwrite8(struct comedi_device *dev,
3852 struct comedi_subdevice *s,
3853 unsigned char data_out,
3854 unsigned char *data_in)
3856 struct ni_private *devpriv = dev->private;
3857 unsigned char mask, input = 0;
3859 /* Wait for one bit before transfer */
3860 udelay((devpriv->serial_interval_ns + 999) / 1000);
3862 for (mask = 0x80; mask; mask >>= 1) {
3863 /* Output current bit; note that we cannot touch s->state
3864 because it is a per-subdevice field, and serial is
3865 a separate subdevice from DIO. */
3866 devpriv->dio_output &= ~NISTC_DIO_SDOUT;
3867 if (data_out & mask)
3868 devpriv->dio_output |= NISTC_DIO_SDOUT;
3869 ni_stc_writew(dev, devpriv->dio_output, NISTC_DIO_OUT_REG);
3871 /* Assert SDCLK (active low, inverted), wait for half of
3872 the delay, deassert SDCLK, and wait for the other half. */
3873 devpriv->dio_control |= NISTC_DIO_SDCLK;
3874 ni_stc_writew(dev, devpriv->dio_control, NISTC_DIO_CTRL_REG);
3876 udelay((devpriv->serial_interval_ns + 999) / 2000);
3878 devpriv->dio_control &= ~NISTC_DIO_SDCLK;
3879 ni_stc_writew(dev, devpriv->dio_control, NISTC_DIO_CTRL_REG);
3881 udelay((devpriv->serial_interval_ns + 999) / 2000);
3883 /* Input current bit */
3884 if (ni_stc_readw(dev, NISTC_DIO_IN_REG) & NISTC_DIO_SDIN)
3894 static int ni_serial_insn_config(struct comedi_device *dev,
3895 struct comedi_subdevice *s,
3896 struct comedi_insn *insn,
3899 struct ni_private *devpriv = dev->private;
3900 unsigned clk_fout = devpriv->clock_and_fout;
3902 unsigned char byte_out, byte_in = 0;
3908 case INSN_CONFIG_SERIAL_CLOCK:
3909 devpriv->serial_hw_mode = 1;
3910 devpriv->dio_control |= NISTC_DIO_CTRL_HW_SER_ENA;
3912 if (data[1] == SERIAL_DISABLED) {
3913 devpriv->serial_hw_mode = 0;
3914 devpriv->dio_control &= ~(NISTC_DIO_CTRL_HW_SER_ENA |
3916 data[1] = SERIAL_DISABLED;
3917 devpriv->serial_interval_ns = data[1];
3918 } else if (data[1] <= SERIAL_600NS) {
3919 /* Warning: this clock speed is too fast to reliably
3921 devpriv->dio_control &= ~NISTC_DIO_CTRL_HW_SER_TIMEBASE;
3922 clk_fout |= NISTC_CLK_FOUT_SLOW_TIMEBASE;
3923 clk_fout &= ~NISTC_CLK_FOUT_DIO_SER_OUT_DIV2;
3924 data[1] = SERIAL_600NS;
3925 devpriv->serial_interval_ns = data[1];
3926 } else if (data[1] <= SERIAL_1_2US) {
3927 devpriv->dio_control &= ~NISTC_DIO_CTRL_HW_SER_TIMEBASE;
3928 clk_fout |= NISTC_CLK_FOUT_SLOW_TIMEBASE |
3929 NISTC_CLK_FOUT_DIO_SER_OUT_DIV2;
3930 data[1] = SERIAL_1_2US;
3931 devpriv->serial_interval_ns = data[1];
3932 } else if (data[1] <= SERIAL_10US) {
3933 devpriv->dio_control |= NISTC_DIO_CTRL_HW_SER_TIMEBASE;
3934 clk_fout |= NISTC_CLK_FOUT_SLOW_TIMEBASE |
3935 NISTC_CLK_FOUT_DIO_SER_OUT_DIV2;
3936 /* Note: NISTC_CLK_FOUT_DIO_SER_OUT_DIV2 only affects
3937 600ns/1.2us. If you turn divide_by_2 off with the
3938 slow clock, you will still get 10us, except then
3939 all your delays are wrong. */
3940 data[1] = SERIAL_10US;
3941 devpriv->serial_interval_ns = data[1];
3943 devpriv->dio_control &= ~(NISTC_DIO_CTRL_HW_SER_ENA |
3945 devpriv->serial_hw_mode = 0;
3946 data[1] = (data[1] / 1000) * 1000;
3947 devpriv->serial_interval_ns = data[1];
3949 devpriv->clock_and_fout = clk_fout;
3951 ni_stc_writew(dev, devpriv->dio_control, NISTC_DIO_CTRL_REG);
3952 ni_stc_writew(dev, devpriv->clock_and_fout, NISTC_CLK_FOUT_REG);
3955 case INSN_CONFIG_BIDIRECTIONAL_DATA:
3957 if (devpriv->serial_interval_ns == 0)
3960 byte_out = data[1] & 0xFF;
3962 if (devpriv->serial_hw_mode) {
3963 err = ni_serial_hw_readwrite8(dev, s, byte_out,
3965 } else if (devpriv->serial_interval_ns > 0) {
3966 err = ni_serial_sw_readwrite8(dev, s, byte_out,
3969 dev_err(dev->class_dev, "serial disabled!\n");
3974 data[1] = byte_in & 0xFF;
3983 static void init_ao_67xx(struct comedi_device *dev, struct comedi_subdevice *s)
3987 for (i = 0; i < s->n_chan; i++) {
3988 ni_ao_win_outw(dev, NI_E_AO_DACSEL(i) | 0x0,
3989 NI67XX_AO_CFG2_REG);
3991 ni_ao_win_outw(dev, 0x0, NI67XX_AO_SP_UPDATES_REG);
3994 static const struct mio_regmap ni_gpct_to_stc_regmap[] = {
3995 [NITIO_G0_AUTO_INC] = { NISTC_G0_AUTOINC_REG, 2 },
3996 [NITIO_G1_AUTO_INC] = { NISTC_G1_AUTOINC_REG, 2 },
3997 [NITIO_G0_CMD] = { NISTC_G0_CMD_REG, 2 },
3998 [NITIO_G1_CMD] = { NISTC_G1_CMD_REG, 2 },
3999 [NITIO_G0_HW_SAVE] = { NISTC_G0_HW_SAVE_REG, 4 },
4000 [NITIO_G1_HW_SAVE] = { NISTC_G1_HW_SAVE_REG, 4 },
4001 [NITIO_G0_SW_SAVE] = { NISTC_G0_SAVE_REG, 4 },
4002 [NITIO_G1_SW_SAVE] = { NISTC_G1_SAVE_REG, 4 },
4003 [NITIO_G0_MODE] = { NISTC_G0_MODE_REG, 2 },
4004 [NITIO_G1_MODE] = { NISTC_G1_MODE_REG, 2 },
4005 [NITIO_G0_LOADA] = { NISTC_G0_LOADA_REG, 4 },
4006 [NITIO_G1_LOADA] = { NISTC_G1_LOADA_REG, 4 },
4007 [NITIO_G0_LOADB] = { NISTC_G0_LOADB_REG, 4 },
4008 [NITIO_G1_LOADB] = { NISTC_G1_LOADB_REG, 4 },
4009 [NITIO_G0_INPUT_SEL] = { NISTC_G0_INPUT_SEL_REG, 2 },
4010 [NITIO_G1_INPUT_SEL] = { NISTC_G1_INPUT_SEL_REG, 2 },
4011 [NITIO_G0_CNT_MODE] = { 0x1b0, 2 }, /* M-Series only */
4012 [NITIO_G1_CNT_MODE] = { 0x1b2, 2 }, /* M-Series only */
4013 [NITIO_G0_GATE2] = { 0x1b4, 2 }, /* M-Series only */
4014 [NITIO_G1_GATE2] = { 0x1b6, 2 }, /* M-Series only */
4015 [NITIO_G01_STATUS] = { NISTC_G01_STATUS_REG, 2 },
4016 [NITIO_G01_RESET] = { NISTC_RESET_REG, 2 },
4017 [NITIO_G01_STATUS1] = { NISTC_STATUS1_REG, 2 },
4018 [NITIO_G01_STATUS2] = { NISTC_STATUS2_REG, 2 },
4019 [NITIO_G0_DMA_CFG] = { 0x1b8, 2 }, /* M-Series only */
4020 [NITIO_G1_DMA_CFG] = { 0x1ba, 2 }, /* M-Series only */
4021 [NITIO_G0_DMA_STATUS] = { 0x1b8, 2 }, /* M-Series only */
4022 [NITIO_G1_DMA_STATUS] = { 0x1ba, 2 }, /* M-Series only */
4023 [NITIO_G0_ABZ] = { 0x1c0, 2 }, /* M-Series only */
4024 [NITIO_G1_ABZ] = { 0x1c2, 2 }, /* M-Series only */
4025 [NITIO_G0_INT_ACK] = { NISTC_INTA_ACK_REG, 2 },
4026 [NITIO_G1_INT_ACK] = { NISTC_INTB_ACK_REG, 2 },
4027 [NITIO_G0_STATUS] = { NISTC_AI_STATUS1_REG, 2 },
4028 [NITIO_G1_STATUS] = { NISTC_AO_STATUS1_REG, 2 },
4029 [NITIO_G0_INT_ENA] = { NISTC_INTA_ENA_REG, 2 },
4030 [NITIO_G1_INT_ENA] = { NISTC_INTB_ENA_REG, 2 },
4033 static unsigned int ni_gpct_to_stc_register(struct comedi_device *dev,
4034 enum ni_gpct_register reg)
4036 const struct mio_regmap *regmap;
4038 if (reg < ARRAY_SIZE(ni_gpct_to_stc_regmap)) {
4039 regmap = &ni_gpct_to_stc_regmap[reg];
4041 dev_warn(dev->class_dev, "%s: unhandled register=0x%x\n",
4046 return regmap->mio_reg;
4049 static void ni_gpct_write_register(struct ni_gpct *counter, unsigned bits,
4050 enum ni_gpct_register reg)
4052 struct comedi_device *dev = counter->counter_dev->dev;
4053 unsigned int stc_register = ni_gpct_to_stc_register(dev, reg);
4054 static const unsigned gpct_interrupt_a_enable_mask =
4055 NISTC_INTA_ENA_G0_GATE | NISTC_INTA_ENA_G0_TC;
4056 static const unsigned gpct_interrupt_b_enable_mask =
4057 NISTC_INTB_ENA_G1_GATE | NISTC_INTB_ENA_G1_TC;
4059 if (stc_register == 0)
4063 /* m-series only registers */
4064 case NITIO_G0_CNT_MODE:
4065 case NITIO_G1_CNT_MODE:
4066 case NITIO_G0_GATE2:
4067 case NITIO_G1_GATE2:
4068 case NITIO_G0_DMA_CFG:
4069 case NITIO_G1_DMA_CFG:
4072 ni_writew(dev, bits, stc_register);
4075 /* 32 bit registers */
4076 case NITIO_G0_LOADA:
4077 case NITIO_G1_LOADA:
4078 case NITIO_G0_LOADB:
4079 case NITIO_G1_LOADB:
4080 ni_stc_writel(dev, bits, stc_register);
4083 /* 16 bit registers */
4084 case NITIO_G0_INT_ENA:
4085 BUG_ON(bits & ~gpct_interrupt_a_enable_mask);
4086 ni_set_bitfield(dev, stc_register,
4087 gpct_interrupt_a_enable_mask, bits);
4089 case NITIO_G1_INT_ENA:
4090 BUG_ON(bits & ~gpct_interrupt_b_enable_mask);
4091 ni_set_bitfield(dev, stc_register,
4092 gpct_interrupt_b_enable_mask, bits);
4094 case NITIO_G01_RESET:
4095 BUG_ON(bits & ~(NISTC_RESET_G0 | NISTC_RESET_G1));
4098 ni_stc_writew(dev, bits, stc_register);
4102 static unsigned ni_gpct_read_register(struct ni_gpct *counter,
4103 enum ni_gpct_register reg)
4105 struct comedi_device *dev = counter->counter_dev->dev;
4106 unsigned int stc_register = ni_gpct_to_stc_register(dev, reg);
4108 if (stc_register == 0)
4112 /* m-series only registers */
4113 case NITIO_G0_DMA_STATUS:
4114 case NITIO_G1_DMA_STATUS:
4115 return ni_readw(dev, stc_register);
4117 /* 32 bit registers */
4118 case NITIO_G0_HW_SAVE:
4119 case NITIO_G1_HW_SAVE:
4120 case NITIO_G0_SW_SAVE:
4121 case NITIO_G1_SW_SAVE:
4122 return ni_stc_readl(dev, stc_register);
4124 /* 16 bit registers */
4126 return ni_stc_readw(dev, stc_register);
4130 static int ni_freq_out_insn_read(struct comedi_device *dev,
4131 struct comedi_subdevice *s,
4132 struct comedi_insn *insn,
4135 struct ni_private *devpriv = dev->private;
4136 unsigned int val = NISTC_CLK_FOUT_TO_DIVIDER(devpriv->clock_and_fout);
4139 for (i = 0; i < insn->n; i++)
4145 static int ni_freq_out_insn_write(struct comedi_device *dev,
4146 struct comedi_subdevice *s,
4147 struct comedi_insn *insn,
4150 struct ni_private *devpriv = dev->private;
4153 unsigned int val = data[insn->n - 1];
4155 devpriv->clock_and_fout &= ~NISTC_CLK_FOUT_ENA;
4156 ni_stc_writew(dev, devpriv->clock_and_fout, NISTC_CLK_FOUT_REG);
4157 devpriv->clock_and_fout &= ~NISTC_CLK_FOUT_DIVIDER_MASK;
4159 /* use the last data value to set the fout divider */
4160 devpriv->clock_and_fout |= NISTC_CLK_FOUT_DIVIDER(val);
4162 devpriv->clock_and_fout |= NISTC_CLK_FOUT_ENA;
4163 ni_stc_writew(dev, devpriv->clock_and_fout, NISTC_CLK_FOUT_REG);
4168 static int ni_freq_out_insn_config(struct comedi_device *dev,
4169 struct comedi_subdevice *s,
4170 struct comedi_insn *insn,
4173 struct ni_private *devpriv = dev->private;
4176 case INSN_CONFIG_SET_CLOCK_SRC:
4178 case NI_FREQ_OUT_TIMEBASE_1_DIV_2_CLOCK_SRC:
4179 devpriv->clock_and_fout &= ~NISTC_CLK_FOUT_TIMEBASE_SEL;
4181 case NI_FREQ_OUT_TIMEBASE_2_CLOCK_SRC:
4182 devpriv->clock_and_fout |= NISTC_CLK_FOUT_TIMEBASE_SEL;
4187 ni_stc_writew(dev, devpriv->clock_and_fout, NISTC_CLK_FOUT_REG);
4189 case INSN_CONFIG_GET_CLOCK_SRC:
4190 if (devpriv->clock_and_fout & NISTC_CLK_FOUT_TIMEBASE_SEL) {
4191 data[1] = NI_FREQ_OUT_TIMEBASE_2_CLOCK_SRC;
4192 data[2] = TIMEBASE_2_NS;
4194 data[1] = NI_FREQ_OUT_TIMEBASE_1_DIV_2_CLOCK_SRC;
4195 data[2] = TIMEBASE_1_NS * 2;
4204 static int ni_8255_callback(struct comedi_device *dev,
4205 int dir, int port, int data, unsigned long iobase)
4208 ni_writeb(dev, data, iobase + 2 * port);
4212 return ni_readb(dev, iobase + 2 * port);
4215 static int ni_get_pwm_config(struct comedi_device *dev, unsigned int *data)
4217 struct ni_private *devpriv = dev->private;
4219 data[1] = devpriv->pwm_up_count * devpriv->clock_ns;
4220 data[2] = devpriv->pwm_down_count * devpriv->clock_ns;
4224 static int ni_m_series_pwm_config(struct comedi_device *dev,
4225 struct comedi_subdevice *s,
4226 struct comedi_insn *insn,
4229 struct ni_private *devpriv = dev->private;
4230 unsigned up_count, down_count;
4233 case INSN_CONFIG_PWM_OUTPUT:
4235 case CMDF_ROUND_NEAREST:
4236 up_count = DIV_ROUND_CLOSEST(data[2],
4239 case CMDF_ROUND_DOWN:
4240 up_count = data[2] / devpriv->clock_ns;
4244 DIV_ROUND_UP(data[2], devpriv->clock_ns);
4250 case CMDF_ROUND_NEAREST:
4251 down_count = DIV_ROUND_CLOSEST(data[4],
4254 case CMDF_ROUND_DOWN:
4255 down_count = data[4] / devpriv->clock_ns;
4259 DIV_ROUND_UP(data[4], devpriv->clock_ns);
4264 if (up_count * devpriv->clock_ns != data[2] ||
4265 down_count * devpriv->clock_ns != data[4]) {
4266 data[2] = up_count * devpriv->clock_ns;
4267 data[4] = down_count * devpriv->clock_ns;
4270 ni_writel(dev, NI_M_CAL_PWM_HIGH_TIME(up_count) |
4271 NI_M_CAL_PWM_LOW_TIME(down_count),
4273 devpriv->pwm_up_count = up_count;
4274 devpriv->pwm_down_count = down_count;
4276 case INSN_CONFIG_GET_PWM_OUTPUT:
4277 return ni_get_pwm_config(dev, data);
4284 static int ni_6143_pwm_config(struct comedi_device *dev,
4285 struct comedi_subdevice *s,
4286 struct comedi_insn *insn,
4289 struct ni_private *devpriv = dev->private;
4290 unsigned up_count, down_count;
4293 case INSN_CONFIG_PWM_OUTPUT:
4295 case CMDF_ROUND_NEAREST:
4296 up_count = DIV_ROUND_CLOSEST(data[2],
4299 case CMDF_ROUND_DOWN:
4300 up_count = data[2] / devpriv->clock_ns;
4304 DIV_ROUND_UP(data[2], devpriv->clock_ns);
4310 case CMDF_ROUND_NEAREST:
4311 down_count = DIV_ROUND_CLOSEST(data[4],
4314 case CMDF_ROUND_DOWN:
4315 down_count = data[4] / devpriv->clock_ns;
4319 DIV_ROUND_UP(data[4], devpriv->clock_ns);
4324 if (up_count * devpriv->clock_ns != data[2] ||
4325 down_count * devpriv->clock_ns != data[4]) {
4326 data[2] = up_count * devpriv->clock_ns;
4327 data[4] = down_count * devpriv->clock_ns;
4330 ni_writel(dev, up_count, NI6143_CALIB_HI_TIME_REG);
4331 devpriv->pwm_up_count = up_count;
4332 ni_writel(dev, down_count, NI6143_CALIB_LO_TIME_REG);
4333 devpriv->pwm_down_count = down_count;
4335 case INSN_CONFIG_GET_PWM_OUTPUT:
4336 return ni_get_pwm_config(dev, data);
4343 static int pack_mb88341(int addr, int val, int *bitstring)
4347 Note that address bits are reversed. Thanks to
4348 Ingo Keen for noticing this.
4350 Note also that the 88341 expects address values from
4351 1-12, whereas we use channel numbers 0-11. The NI
4352 docs use 1-12, also, so be careful here.
4355 *bitstring = ((addr & 0x1) << 11) |
4356 ((addr & 0x2) << 9) |
4357 ((addr & 0x4) << 7) | ((addr & 0x8) << 5) | (val & 0xff);
4361 static int pack_dac8800(int addr, int val, int *bitstring)
4363 *bitstring = ((addr & 0x7) << 8) | (val & 0xff);
4367 static int pack_dac8043(int addr, int val, int *bitstring)
4369 *bitstring = val & 0xfff;
4373 static int pack_ad8522(int addr, int val, int *bitstring)
4375 *bitstring = (val & 0xfff) | (addr ? 0xc000 : 0xa000);
4379 static int pack_ad8804(int addr, int val, int *bitstring)
4381 *bitstring = ((addr & 0xf) << 8) | (val & 0xff);
4385 static int pack_ad8842(int addr, int val, int *bitstring)
4387 *bitstring = ((addr + 1) << 8) | (val & 0xff);
4391 struct caldac_struct {
4394 int (*packbits)(int, int, int *);
4397 static struct caldac_struct caldacs[] = {
4398 [mb88341] = {12, 8, pack_mb88341},
4399 [dac8800] = {8, 8, pack_dac8800},
4400 [dac8043] = {1, 12, pack_dac8043},
4401 [ad8522] = {2, 12, pack_ad8522},
4402 [ad8804] = {12, 8, pack_ad8804},
4403 [ad8842] = {8, 8, pack_ad8842},
4404 [ad8804_debug] = {16, 8, pack_ad8804},
4407 static void ni_write_caldac(struct comedi_device *dev, int addr, int val)
4409 const struct ni_board_struct *board = dev->board_ptr;
4410 struct ni_private *devpriv = dev->private;
4411 unsigned int loadbit = 0, bits = 0, bit, bitstring = 0;
4416 if (devpriv->caldacs[addr] == val)
4418 devpriv->caldacs[addr] = val;
4420 for (i = 0; i < 3; i++) {
4421 type = board->caldac[i];
4422 if (type == caldac_none)
4424 if (addr < caldacs[type].n_chans) {
4425 bits = caldacs[type].packbits(addr, val, &bitstring);
4426 loadbit = NI_E_SERIAL_CMD_DAC_LD(i);
4429 addr -= caldacs[type].n_chans;
4432 /* bits will be 0 if there is no caldac for the given addr */
4436 for (bit = 1 << (bits - 1); bit; bit >>= 1) {
4437 cmd = (bit & bitstring) ? NI_E_SERIAL_CMD_SDATA : 0;
4438 ni_writeb(dev, cmd, NI_E_SERIAL_CMD_REG);
4440 ni_writeb(dev, NI_E_SERIAL_CMD_SCLK | cmd, NI_E_SERIAL_CMD_REG);
4443 ni_writeb(dev, loadbit, NI_E_SERIAL_CMD_REG);
4445 ni_writeb(dev, 0, NI_E_SERIAL_CMD_REG);
4448 static int ni_calib_insn_write(struct comedi_device *dev,
4449 struct comedi_subdevice *s,
4450 struct comedi_insn *insn,
4453 ni_write_caldac(dev, CR_CHAN(insn->chanspec), data[0]);
4458 static int ni_calib_insn_read(struct comedi_device *dev,
4459 struct comedi_subdevice *s,
4460 struct comedi_insn *insn,
4463 struct ni_private *devpriv = dev->private;
4465 data[0] = devpriv->caldacs[CR_CHAN(insn->chanspec)];
4470 static void caldac_setup(struct comedi_device *dev, struct comedi_subdevice *s)
4472 const struct ni_board_struct *board = dev->board_ptr;
4473 struct ni_private *devpriv = dev->private;
4482 type = board->caldac[0];
4483 if (type == caldac_none)
4485 n_bits = caldacs[type].n_bits;
4486 for (i = 0; i < 3; i++) {
4487 type = board->caldac[i];
4488 if (type == caldac_none)
4490 if (caldacs[type].n_bits != n_bits)
4492 n_chans += caldacs[type].n_chans;
4495 s->n_chan = n_chans;
4498 unsigned int *maxdata_list;
4500 if (n_chans > MAX_N_CALDACS)
4501 dev_err(dev->class_dev,
4502 "BUG! MAX_N_CALDACS too small\n");
4503 s->maxdata_list = maxdata_list = devpriv->caldac_maxdata_list;
4505 for (i = 0; i < n_dacs; i++) {
4506 type = board->caldac[i];
4507 for (j = 0; j < caldacs[type].n_chans; j++) {
4508 maxdata_list[chan] =
4509 (1 << caldacs[type].n_bits) - 1;
4514 for (chan = 0; chan < s->n_chan; chan++)
4515 ni_write_caldac(dev, i, s->maxdata_list[i] / 2);
4517 type = board->caldac[0];
4518 s->maxdata = (1 << caldacs[type].n_bits) - 1;
4520 for (chan = 0; chan < s->n_chan; chan++)
4521 ni_write_caldac(dev, i, s->maxdata / 2);
4525 static int ni_read_eeprom(struct comedi_device *dev, int addr)
4527 unsigned int cmd = NI_E_SERIAL_CMD_EEPROM_CS;
4531 bitstring = 0x0300 | ((addr & 0x100) << 3) | (addr & 0xff);
4532 ni_writeb(dev, cmd, NI_E_SERIAL_CMD_REG);
4533 for (bit = 0x8000; bit; bit >>= 1) {
4534 if (bit & bitstring)
4535 cmd |= NI_E_SERIAL_CMD_SDATA;
4537 cmd &= ~NI_E_SERIAL_CMD_SDATA;
4539 ni_writeb(dev, cmd, NI_E_SERIAL_CMD_REG);
4540 ni_writeb(dev, NI_E_SERIAL_CMD_SCLK | cmd, NI_E_SERIAL_CMD_REG);
4542 cmd = NI_E_SERIAL_CMD_EEPROM_CS;
4544 for (bit = 0x80; bit; bit >>= 1) {
4545 ni_writeb(dev, cmd, NI_E_SERIAL_CMD_REG);
4546 ni_writeb(dev, NI_E_SERIAL_CMD_SCLK | cmd, NI_E_SERIAL_CMD_REG);
4547 if (ni_readb(dev, NI_E_STATUS_REG) & NI_E_STATUS_PROMOUT)
4550 ni_writeb(dev, 0, NI_E_SERIAL_CMD_REG);
4555 static int ni_eeprom_insn_read(struct comedi_device *dev,
4556 struct comedi_subdevice *s,
4557 struct comedi_insn *insn,
4560 data[0] = ni_read_eeprom(dev, CR_CHAN(insn->chanspec));
4565 static int ni_m_series_eeprom_insn_read(struct comedi_device *dev,
4566 struct comedi_subdevice *s,
4567 struct comedi_insn *insn,
4570 struct ni_private *devpriv = dev->private;
4572 data[0] = devpriv->eeprom_buffer[CR_CHAN(insn->chanspec)];
4577 static unsigned ni_old_get_pfi_routing(struct comedi_device *dev,
4580 /* pre-m-series boards have fixed signals on pfi pins */
4583 return NI_PFI_OUTPUT_AI_START1;
4585 return NI_PFI_OUTPUT_AI_START2;
4587 return NI_PFI_OUTPUT_AI_CONVERT;
4589 return NI_PFI_OUTPUT_G_SRC1;
4591 return NI_PFI_OUTPUT_G_GATE1;
4593 return NI_PFI_OUTPUT_AO_UPDATE_N;
4595 return NI_PFI_OUTPUT_AO_START1;
4597 return NI_PFI_OUTPUT_AI_START_PULSE;
4599 return NI_PFI_OUTPUT_G_SRC0;
4601 return NI_PFI_OUTPUT_G_GATE0;
4603 dev_err(dev->class_dev, "bug, unhandled case in switch.\n");
4609 static int ni_old_set_pfi_routing(struct comedi_device *dev,
4610 unsigned chan, unsigned source)
4612 /* pre-m-series boards have fixed signals on pfi pins */
4613 if (source != ni_old_get_pfi_routing(dev, chan))
4618 static unsigned ni_m_series_get_pfi_routing(struct comedi_device *dev,
4621 struct ni_private *devpriv = dev->private;
4622 const unsigned array_offset = chan / 3;
4624 return NI_M_PFI_OUT_SEL_TO_SRC(chan,
4625 devpriv->pfi_output_select_reg[array_offset]);
4628 static int ni_m_series_set_pfi_routing(struct comedi_device *dev,
4629 unsigned chan, unsigned source)
4631 struct ni_private *devpriv = dev->private;
4632 unsigned index = chan / 3;
4633 unsigned short val = devpriv->pfi_output_select_reg[index];
4635 if ((source & 0x1f) != source)
4638 val &= ~NI_M_PFI_OUT_SEL_MASK(chan);
4639 val |= NI_M_PFI_OUT_SEL(chan, source);
4640 ni_writew(dev, val, NI_M_PFI_OUT_SEL_REG(index));
4641 devpriv->pfi_output_select_reg[index] = val;
4646 static unsigned ni_get_pfi_routing(struct comedi_device *dev, unsigned chan)
4648 struct ni_private *devpriv = dev->private;
4650 return (devpriv->is_m_series)
4651 ? ni_m_series_get_pfi_routing(dev, chan)
4652 : ni_old_get_pfi_routing(dev, chan);
4655 static int ni_set_pfi_routing(struct comedi_device *dev, unsigned chan,
4658 struct ni_private *devpriv = dev->private;
4660 return (devpriv->is_m_series)
4661 ? ni_m_series_set_pfi_routing(dev, chan, source)
4662 : ni_old_set_pfi_routing(dev, chan, source);
4665 static int ni_config_filter(struct comedi_device *dev,
4666 unsigned pfi_channel,
4667 enum ni_pfi_filter_select filter)
4669 struct ni_private *devpriv = dev->private;
4672 if (!devpriv->is_m_series)
4675 bits = ni_readl(dev, NI_M_PFI_FILTER_REG);
4676 bits &= ~NI_M_PFI_FILTER_SEL_MASK(pfi_channel);
4677 bits |= NI_M_PFI_FILTER_SEL(pfi_channel, filter);
4678 ni_writel(dev, bits, NI_M_PFI_FILTER_REG);
4682 static int ni_pfi_insn_config(struct comedi_device *dev,
4683 struct comedi_subdevice *s,
4684 struct comedi_insn *insn,
4687 struct ni_private *devpriv = dev->private;
4693 chan = CR_CHAN(insn->chanspec);
4697 ni_set_bits(dev, NISTC_IO_BIDIR_PIN_REG, 1 << chan, 1);
4700 ni_set_bits(dev, NISTC_IO_BIDIR_PIN_REG, 1 << chan, 0);
4702 case INSN_CONFIG_DIO_QUERY:
4704 (devpriv->io_bidirection_pin_reg & (1 << chan)) ?
4705 COMEDI_OUTPUT : COMEDI_INPUT;
4707 case INSN_CONFIG_SET_ROUTING:
4708 return ni_set_pfi_routing(dev, chan, data[1]);
4709 case INSN_CONFIG_GET_ROUTING:
4710 data[1] = ni_get_pfi_routing(dev, chan);
4712 case INSN_CONFIG_FILTER:
4713 return ni_config_filter(dev, chan, data[1]);
4720 static int ni_pfi_insn_bits(struct comedi_device *dev,
4721 struct comedi_subdevice *s,
4722 struct comedi_insn *insn,
4725 struct ni_private *devpriv = dev->private;
4727 if (!devpriv->is_m_series)
4730 if (comedi_dio_update_state(s, data))
4731 ni_writew(dev, s->state, NI_M_PFI_DO_REG);
4733 data[1] = ni_readw(dev, NI_M_PFI_DI_REG);
4738 static int cs5529_wait_for_idle(struct comedi_device *dev)
4740 unsigned short status;
4741 const int timeout = HZ;
4744 for (i = 0; i < timeout; i++) {
4745 status = ni_ao_win_inw(dev, NI67XX_CAL_STATUS_REG);
4746 if ((status & NI67XX_CAL_STATUS_BUSY) == 0)
4748 set_current_state(TASK_INTERRUPTIBLE);
4749 if (schedule_timeout(1))
4753 dev_err(dev->class_dev, "timeout\n");
4759 static void cs5529_command(struct comedi_device *dev, unsigned short value)
4761 static const int timeout = 100;
4764 ni_ao_win_outw(dev, value, NI67XX_CAL_CMD_REG);
4765 /* give time for command to start being serially clocked into cs5529.
4766 * this insures that the NI67XX_CAL_STATUS_BUSY bit will get properly
4767 * set before we exit this function.
4769 for (i = 0; i < timeout; i++) {
4770 if (ni_ao_win_inw(dev, NI67XX_CAL_STATUS_REG) &
4771 NI67XX_CAL_STATUS_BUSY)
4776 dev_err(dev->class_dev,
4777 "possible problem - never saw adc go busy?\n");
4780 static int cs5529_do_conversion(struct comedi_device *dev,
4781 unsigned short *data)
4784 unsigned short status;
4786 cs5529_command(dev, CS5529_CMD_CB | CS5529_CMD_SINGLE_CONV);
4787 retval = cs5529_wait_for_idle(dev);
4789 dev_err(dev->class_dev,
4790 "timeout or signal in cs5529_do_conversion()\n");
4793 status = ni_ao_win_inw(dev, NI67XX_CAL_STATUS_REG);
4794 if (status & NI67XX_CAL_STATUS_OSC_DETECT) {
4795 dev_err(dev->class_dev,
4796 "cs5529 conversion error, status CSS_OSC_DETECT\n");
4799 if (status & NI67XX_CAL_STATUS_OVERRANGE) {
4800 dev_err(dev->class_dev,
4801 "cs5529 conversion error, overrange (ignoring)\n");
4804 *data = ni_ao_win_inw(dev, NI67XX_CAL_DATA_REG);
4805 /* cs5529 returns 16 bit signed data in bipolar mode */
4811 static int cs5529_ai_insn_read(struct comedi_device *dev,
4812 struct comedi_subdevice *s,
4813 struct comedi_insn *insn,
4817 unsigned short sample;
4818 unsigned int channel_select;
4819 const unsigned int INTERNAL_REF = 0x1000;
4821 /* Set calibration adc source. Docs lie, reference select bits 8 to 11
4822 * do nothing. bit 12 seems to chooses internal reference voltage, bit
4823 * 13 causes the adc input to go overrange (maybe reads external reference?) */
4824 if (insn->chanspec & CR_ALT_SOURCE)
4825 channel_select = INTERNAL_REF;
4827 channel_select = CR_CHAN(insn->chanspec);
4828 ni_ao_win_outw(dev, channel_select, NI67XX_AO_CAL_CHAN_SEL_REG);
4830 for (n = 0; n < insn->n; n++) {
4831 retval = cs5529_do_conversion(dev, &sample);
4839 static void cs5529_config_write(struct comedi_device *dev, unsigned int value,
4840 unsigned int reg_select_bits)
4842 ni_ao_win_outw(dev, (value >> 16) & 0xff, NI67XX_CAL_CFG_HI_REG);
4843 ni_ao_win_outw(dev, value & 0xffff, NI67XX_CAL_CFG_LO_REG);
4844 reg_select_bits &= CS5529_CMD_REG_MASK;
4845 cs5529_command(dev, CS5529_CMD_CB | reg_select_bits);
4846 if (cs5529_wait_for_idle(dev))
4847 dev_err(dev->class_dev,
4848 "timeout or signal in %s\n", __func__);
4851 static int init_cs5529(struct comedi_device *dev)
4853 unsigned int config_bits = CS5529_CFG_PORT_FLAG |
4854 CS5529_CFG_WORD_RATE_2180;
4857 /* do self-calibration */
4858 cs5529_config_write(dev, config_bits | CS5529_CFG_CALIB_BOTH_SELF,
4860 /* need to force a conversion for calibration to run */
4861 cs5529_do_conversion(dev, NULL);
4863 /* force gain calibration to 1 */
4864 cs5529_config_write(dev, 0x400000, CS5529_GAIN_REG);
4865 cs5529_config_write(dev, config_bits | CS5529_CFG_CALIB_OFFSET_SELF,
4867 if (cs5529_wait_for_idle(dev))
4868 dev_err(dev->class_dev,
4869 "timeout or signal in %s\n", __func__);
4875 * Find best multiplier/divider to try and get the PLL running at 80 MHz
4876 * given an arbitrary frequency input clock.
4878 static int ni_mseries_get_pll_parameters(unsigned reference_period_ns,
4879 unsigned *freq_divider,
4880 unsigned *freq_multiplier,
4881 unsigned *actual_period_ns)
4884 unsigned best_div = 1;
4886 unsigned best_mult = 1;
4887 static const unsigned pico_per_nano = 1000;
4889 const unsigned reference_picosec = reference_period_ns * pico_per_nano;
4890 /* m-series wants the phased-locked loop to output 80MHz, which is divided by 4 to
4891 * 20 MHz for most timing clocks */
4892 static const unsigned target_picosec = 12500;
4893 static const unsigned fudge_factor_80_to_20Mhz = 4;
4894 int best_period_picosec = 0;
4896 for (div = 1; div <= NI_M_PLL_MAX_DIVISOR; ++div) {
4897 for (mult = 1; mult <= NI_M_PLL_MAX_MULTIPLIER; ++mult) {
4898 unsigned new_period_ps =
4899 (reference_picosec * div) / mult;
4900 if (abs(new_period_ps - target_picosec) <
4901 abs(best_period_picosec - target_picosec)) {
4902 best_period_picosec = new_period_ps;
4908 if (best_period_picosec == 0)
4911 *freq_divider = best_div;
4912 *freq_multiplier = best_mult;
4913 *actual_period_ns = DIV_ROUND_CLOSEST(best_period_picosec *
4914 fudge_factor_80_to_20Mhz,
4919 static int ni_mseries_set_pll_master_clock(struct comedi_device *dev,
4920 unsigned source, unsigned period_ns)
4922 struct ni_private *devpriv = dev->private;
4923 static const unsigned min_period_ns = 50;
4924 static const unsigned max_period_ns = 1000;
4925 static const unsigned timeout = 1000;
4926 unsigned pll_control_bits;
4927 unsigned freq_divider;
4928 unsigned freq_multiplier;
4933 if (source == NI_MIO_PLL_PXI10_CLOCK)
4935 /* these limits are somewhat arbitrary, but NI advertises 1 to 20MHz range so we'll use that */
4936 if (period_ns < min_period_ns || period_ns > max_period_ns) {
4937 dev_err(dev->class_dev,
4938 "%s: you must specify an input clock frequency between %i and %i nanosec for the phased-lock loop\n",
4939 __func__, min_period_ns, max_period_ns);
4942 devpriv->rtsi_trig_direction_reg &= ~NISTC_RTSI_TRIG_USE_CLK;
4943 ni_stc_writew(dev, devpriv->rtsi_trig_direction_reg,
4944 NISTC_RTSI_TRIG_DIR_REG);
4945 pll_control_bits = NI_M_PLL_CTRL_ENA | NI_M_PLL_CTRL_VCO_MODE_75_150MHZ;
4946 devpriv->clock_and_fout2 |= NI_M_CLK_FOUT2_TIMEBASE1_PLL |
4947 NI_M_CLK_FOUT2_TIMEBASE3_PLL;
4948 devpriv->clock_and_fout2 &= ~NI_M_CLK_FOUT2_PLL_SRC_MASK;
4950 case NI_MIO_PLL_PXI_STAR_TRIGGER_CLOCK:
4951 devpriv->clock_and_fout2 |= NI_M_CLK_FOUT2_PLL_SRC_STAR;
4953 case NI_MIO_PLL_PXI10_CLOCK:
4954 /* pxi clock is 10MHz */
4955 devpriv->clock_and_fout2 |= NI_M_CLK_FOUT2_PLL_SRC_PXI10;
4958 for (rtsi = 0; rtsi <= NI_M_MAX_RTSI_CHAN; ++rtsi) {
4959 if (source == NI_MIO_PLL_RTSI_CLOCK(rtsi)) {
4960 devpriv->clock_and_fout2 |=
4961 NI_M_CLK_FOUT2_PLL_SRC_RTSI(rtsi);
4965 if (rtsi > NI_M_MAX_RTSI_CHAN)
4969 retval = ni_mseries_get_pll_parameters(period_ns,
4972 &devpriv->clock_ns);
4974 dev_err(dev->class_dev,
4975 "bug, failed to find pll parameters\n");
4979 ni_writew(dev, devpriv->clock_and_fout2, NI_M_CLK_FOUT2_REG);
4980 pll_control_bits |= NI_M_PLL_CTRL_DIVISOR(freq_divider) |
4981 NI_M_PLL_CTRL_MULTIPLIER(freq_multiplier);
4983 ni_writew(dev, pll_control_bits, NI_M_PLL_CTRL_REG);
4984 devpriv->clock_source = source;
4985 /* it seems to typically take a few hundred microseconds for PLL to lock */
4986 for (i = 0; i < timeout; ++i) {
4987 if (ni_readw(dev, NI_M_PLL_STATUS_REG) & NI_M_PLL_STATUS_LOCKED)
4992 dev_err(dev->class_dev,
4993 "%s: timed out waiting for PLL to lock to reference clock source %i with period %i ns\n",
4994 __func__, source, period_ns);
5000 static int ni_set_master_clock(struct comedi_device *dev,
5001 unsigned source, unsigned period_ns)
5003 struct ni_private *devpriv = dev->private;
5005 if (source == NI_MIO_INTERNAL_CLOCK) {
5006 devpriv->rtsi_trig_direction_reg &= ~NISTC_RTSI_TRIG_USE_CLK;
5007 ni_stc_writew(dev, devpriv->rtsi_trig_direction_reg,
5008 NISTC_RTSI_TRIG_DIR_REG);
5009 devpriv->clock_ns = TIMEBASE_1_NS;
5010 if (devpriv->is_m_series) {
5011 devpriv->clock_and_fout2 &=
5012 ~(NI_M_CLK_FOUT2_TIMEBASE1_PLL |
5013 NI_M_CLK_FOUT2_TIMEBASE3_PLL);
5014 ni_writew(dev, devpriv->clock_and_fout2,
5015 NI_M_CLK_FOUT2_REG);
5016 ni_writew(dev, 0, NI_M_PLL_CTRL_REG);
5018 devpriv->clock_source = source;
5020 if (devpriv->is_m_series) {
5021 return ni_mseries_set_pll_master_clock(dev, source,
5024 if (source == NI_MIO_RTSI_CLOCK) {
5025 devpriv->rtsi_trig_direction_reg |=
5026 NISTC_RTSI_TRIG_USE_CLK;
5028 devpriv->rtsi_trig_direction_reg,
5029 NISTC_RTSI_TRIG_DIR_REG);
5030 if (period_ns == 0) {
5031 dev_err(dev->class_dev,
5032 "we don't handle an unspecified clock period correctly yet, returning error\n");
5035 devpriv->clock_ns = period_ns;
5036 devpriv->clock_source = source;
5045 static int ni_valid_rtsi_output_source(struct comedi_device *dev,
5046 unsigned chan, unsigned source)
5048 struct ni_private *devpriv = dev->private;
5050 if (chan >= NISTC_RTSI_TRIG_NUM_CHAN(devpriv->is_m_series)) {
5051 if (chan == NISTC_RTSI_TRIG_OLD_CLK_CHAN) {
5052 if (source == NI_RTSI_OUTPUT_RTSI_OSC)
5055 dev_err(dev->class_dev,
5056 "%s: invalid source for channel=%i, channel %i is always the RTSI clock for pre-m-series boards\n",
5057 __func__, chan, NISTC_RTSI_TRIG_OLD_CLK_CHAN);
5063 case NI_RTSI_OUTPUT_ADR_START1:
5064 case NI_RTSI_OUTPUT_ADR_START2:
5065 case NI_RTSI_OUTPUT_SCLKG:
5066 case NI_RTSI_OUTPUT_DACUPDN:
5067 case NI_RTSI_OUTPUT_DA_START1:
5068 case NI_RTSI_OUTPUT_G_SRC0:
5069 case NI_RTSI_OUTPUT_G_GATE0:
5070 case NI_RTSI_OUTPUT_RGOUT0:
5071 case NI_RTSI_OUTPUT_RTSI_BRD_0:
5073 case NI_RTSI_OUTPUT_RTSI_OSC:
5074 return (devpriv->is_m_series) ? 1 : 0;
5080 static int ni_set_rtsi_routing(struct comedi_device *dev,
5081 unsigned chan, unsigned src)
5083 struct ni_private *devpriv = dev->private;
5085 if (ni_valid_rtsi_output_source(dev, chan, src) == 0)
5088 devpriv->rtsi_trig_a_output_reg &= ~NISTC_RTSI_TRIG_MASK(chan);
5089 devpriv->rtsi_trig_a_output_reg |= NISTC_RTSI_TRIG(chan, src);
5090 ni_stc_writew(dev, devpriv->rtsi_trig_a_output_reg,
5091 NISTC_RTSI_TRIGA_OUT_REG);
5092 } else if (chan < 8) {
5093 devpriv->rtsi_trig_b_output_reg &= ~NISTC_RTSI_TRIG_MASK(chan);
5094 devpriv->rtsi_trig_b_output_reg |= NISTC_RTSI_TRIG(chan, src);
5095 ni_stc_writew(dev, devpriv->rtsi_trig_b_output_reg,
5096 NISTC_RTSI_TRIGB_OUT_REG);
5101 static unsigned ni_get_rtsi_routing(struct comedi_device *dev, unsigned chan)
5103 struct ni_private *devpriv = dev->private;
5106 return NISTC_RTSI_TRIG_TO_SRC(chan,
5107 devpriv->rtsi_trig_a_output_reg);
5108 } else if (chan < NISTC_RTSI_TRIG_NUM_CHAN(devpriv->is_m_series)) {
5109 return NISTC_RTSI_TRIG_TO_SRC(chan,
5110 devpriv->rtsi_trig_b_output_reg);
5112 if (chan == NISTC_RTSI_TRIG_OLD_CLK_CHAN)
5113 return NI_RTSI_OUTPUT_RTSI_OSC;
5114 dev_err(dev->class_dev, "bug! should never get here?\n");
5119 static int ni_rtsi_insn_config(struct comedi_device *dev,
5120 struct comedi_subdevice *s,
5121 struct comedi_insn *insn,
5124 struct ni_private *devpriv = dev->private;
5125 unsigned int chan = CR_CHAN(insn->chanspec);
5126 unsigned int max_chan = NISTC_RTSI_TRIG_NUM_CHAN(devpriv->is_m_series);
5129 case INSN_CONFIG_DIO_OUTPUT:
5130 if (chan < max_chan) {
5131 devpriv->rtsi_trig_direction_reg |=
5132 NISTC_RTSI_TRIG_DIR(chan, devpriv->is_m_series);
5133 } else if (chan == NISTC_RTSI_TRIG_OLD_CLK_CHAN) {
5134 devpriv->rtsi_trig_direction_reg |=
5135 NISTC_RTSI_TRIG_DRV_CLK;
5137 ni_stc_writew(dev, devpriv->rtsi_trig_direction_reg,
5138 NISTC_RTSI_TRIG_DIR_REG);
5140 case INSN_CONFIG_DIO_INPUT:
5141 if (chan < max_chan) {
5142 devpriv->rtsi_trig_direction_reg &=
5143 ~NISTC_RTSI_TRIG_DIR(chan, devpriv->is_m_series);
5144 } else if (chan == NISTC_RTSI_TRIG_OLD_CLK_CHAN) {
5145 devpriv->rtsi_trig_direction_reg &=
5146 ~NISTC_RTSI_TRIG_DRV_CLK;
5148 ni_stc_writew(dev, devpriv->rtsi_trig_direction_reg,
5149 NISTC_RTSI_TRIG_DIR_REG);
5151 case INSN_CONFIG_DIO_QUERY:
5152 if (chan < max_chan) {
5154 (devpriv->rtsi_trig_direction_reg &
5155 NISTC_RTSI_TRIG_DIR(chan, devpriv->is_m_series))
5156 ? INSN_CONFIG_DIO_OUTPUT
5157 : INSN_CONFIG_DIO_INPUT;
5158 } else if (chan == NISTC_RTSI_TRIG_OLD_CLK_CHAN) {
5159 data[1] = (devpriv->rtsi_trig_direction_reg &
5160 NISTC_RTSI_TRIG_DRV_CLK)
5161 ? INSN_CONFIG_DIO_OUTPUT
5162 : INSN_CONFIG_DIO_INPUT;
5165 case INSN_CONFIG_SET_CLOCK_SRC:
5166 return ni_set_master_clock(dev, data[1], data[2]);
5167 case INSN_CONFIG_GET_CLOCK_SRC:
5168 data[1] = devpriv->clock_source;
5169 data[2] = devpriv->clock_ns;
5171 case INSN_CONFIG_SET_ROUTING:
5172 return ni_set_rtsi_routing(dev, chan, data[1]);
5173 case INSN_CONFIG_GET_ROUTING:
5174 data[1] = ni_get_rtsi_routing(dev, chan);
5182 static int ni_rtsi_insn_bits(struct comedi_device *dev,
5183 struct comedi_subdevice *s,
5184 struct comedi_insn *insn,
5192 static void ni_rtsi_init(struct comedi_device *dev)
5194 struct ni_private *devpriv = dev->private;
5196 /* Initialises the RTSI bus signal switch to a default state */
5199 * Use 10MHz instead of 20MHz for RTSI clock frequency. Appears
5200 * to have no effect, at least on pxi-6281, which always uses
5201 * 20MHz rtsi clock frequency
5203 devpriv->clock_and_fout2 = NI_M_CLK_FOUT2_RTSI_10MHZ;
5204 /* Set clock mode to internal */
5205 if (ni_set_master_clock(dev, NI_MIO_INTERNAL_CLOCK, 0) < 0)
5206 dev_err(dev->class_dev, "ni_set_master_clock failed, bug?\n");
5207 /* default internal lines routing to RTSI bus lines */
5208 devpriv->rtsi_trig_a_output_reg =
5209 NISTC_RTSI_TRIG(0, NI_RTSI_OUTPUT_ADR_START1) |
5210 NISTC_RTSI_TRIG(1, NI_RTSI_OUTPUT_ADR_START2) |
5211 NISTC_RTSI_TRIG(2, NI_RTSI_OUTPUT_SCLKG) |
5212 NISTC_RTSI_TRIG(3, NI_RTSI_OUTPUT_DACUPDN);
5213 ni_stc_writew(dev, devpriv->rtsi_trig_a_output_reg,
5214 NISTC_RTSI_TRIGA_OUT_REG);
5215 devpriv->rtsi_trig_b_output_reg =
5216 NISTC_RTSI_TRIG(4, NI_RTSI_OUTPUT_DA_START1) |
5217 NISTC_RTSI_TRIG(5, NI_RTSI_OUTPUT_G_SRC0) |
5218 NISTC_RTSI_TRIG(6, NI_RTSI_OUTPUT_G_GATE0);
5219 if (devpriv->is_m_series)
5220 devpriv->rtsi_trig_b_output_reg |=
5221 NISTC_RTSI_TRIG(7, NI_RTSI_OUTPUT_RTSI_OSC);
5222 ni_stc_writew(dev, devpriv->rtsi_trig_b_output_reg,
5223 NISTC_RTSI_TRIGB_OUT_REG);
5226 * Sets the source and direction of the 4 on board lines
5227 * ni_stc_writew(dev, 0, NISTC_RTSI_BOARD_REG);
5232 static int ni_gpct_cmd(struct comedi_device *dev, struct comedi_subdevice *s)
5234 struct ni_gpct *counter = s->private;
5237 retval = ni_request_gpct_mite_channel(dev, counter->counter_index,
5240 dev_err(dev->class_dev,
5241 "no dma channel available for use by counter\n");
5244 ni_tio_acknowledge(counter);
5245 ni_e_series_enable_second_irq(dev, counter->counter_index, 1);
5247 return ni_tio_cmd(dev, s);
5250 static int ni_gpct_cancel(struct comedi_device *dev, struct comedi_subdevice *s)
5252 struct ni_gpct *counter = s->private;
5255 retval = ni_tio_cancel(counter);
5256 ni_e_series_enable_second_irq(dev, counter->counter_index, 0);
5257 ni_release_gpct_mite_channel(dev, counter->counter_index);
5262 static irqreturn_t ni_E_interrupt(int irq, void *d)
5264 struct comedi_device *dev = d;
5265 unsigned short a_status;
5266 unsigned short b_status;
5267 unsigned int ai_mite_status = 0;
5268 unsigned int ao_mite_status = 0;
5269 unsigned long flags;
5271 struct ni_private *devpriv = dev->private;
5276 smp_mb(); /* make sure dev->attached is checked before handler does anything else. */
5278 /* lock to avoid race with comedi_poll */
5279 spin_lock_irqsave(&dev->spinlock, flags);
5280 a_status = ni_stc_readw(dev, NISTC_AI_STATUS1_REG);
5281 b_status = ni_stc_readw(dev, NISTC_AO_STATUS1_REG);
5283 if (devpriv->mite) {
5284 unsigned long flags_too;
5286 spin_lock_irqsave(&devpriv->mite_channel_lock, flags_too);
5287 if (devpriv->ai_mite_chan) {
5288 ai_mite_status = mite_get_status(devpriv->ai_mite_chan);
5289 if (ai_mite_status & CHSR_LINKC)
5291 devpriv->mite->mite_io_addr +
5293 ai_mite_chan->channel));
5295 if (devpriv->ao_mite_chan) {
5296 ao_mite_status = mite_get_status(devpriv->ao_mite_chan);
5297 if (ao_mite_status & CHSR_LINKC)
5299 devpriv->mite->mite_io_addr +
5301 ao_mite_chan->channel));
5303 spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags_too);
5306 ack_a_interrupt(dev, a_status);
5307 ack_b_interrupt(dev, b_status);
5308 if ((a_status & NISTC_AI_STATUS1_INTA) || (ai_mite_status & CHSR_INT))
5309 handle_a_interrupt(dev, a_status, ai_mite_status);
5310 if ((b_status & NISTC_AO_STATUS1_INTB) || (ao_mite_status & CHSR_INT))
5311 handle_b_interrupt(dev, b_status, ao_mite_status);
5312 handle_gpct_interrupt(dev, 0);
5313 handle_gpct_interrupt(dev, 1);
5314 handle_cdio_interrupt(dev);
5316 spin_unlock_irqrestore(&dev->spinlock, flags);
5320 static int ni_alloc_private(struct comedi_device *dev)
5322 struct ni_private *devpriv;
5324 devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
5328 spin_lock_init(&devpriv->window_lock);
5329 spin_lock_init(&devpriv->soft_reg_copy_lock);
5330 spin_lock_init(&devpriv->mite_channel_lock);
5335 static int ni_E_init(struct comedi_device *dev,
5336 unsigned interrupt_pin, unsigned irq_polarity)
5338 const struct ni_board_struct *board = dev->board_ptr;
5339 struct ni_private *devpriv = dev->private;
5340 struct comedi_subdevice *s;
5344 if (board->n_aochan > MAX_N_AO_CHAN) {
5345 dev_err(dev->class_dev, "bug! n_aochan > MAX_N_AO_CHAN\n");
5349 /* initialize clock dividers */
5350 devpriv->clock_and_fout = NISTC_CLK_FOUT_SLOW_DIV2 |
5351 NISTC_CLK_FOUT_SLOW_TIMEBASE |
5352 NISTC_CLK_FOUT_TO_BOARD_DIV2 |
5353 NISTC_CLK_FOUT_TO_BOARD;
5354 if (!devpriv->is_6xxx) {
5355 /* BEAM is this needed for PCI-6143 ?? */
5356 devpriv->clock_and_fout |= (NISTC_CLK_FOUT_AI_OUT_DIV2 |
5357 NISTC_CLK_FOUT_AO_OUT_DIV2);
5359 ni_stc_writew(dev, devpriv->clock_and_fout, NISTC_CLK_FOUT_REG);
5361 ret = comedi_alloc_subdevices(dev, NI_NUM_SUBDEVICES);
5365 /* Analog Input subdevice */
5366 s = &dev->subdevices[NI_AI_SUBDEV];
5367 if (board->n_adchan) {
5368 s->type = COMEDI_SUBD_AI;
5369 s->subdev_flags = SDF_READABLE | SDF_DIFF | SDF_DITHER;
5370 if (!devpriv->is_611x)
5371 s->subdev_flags |= SDF_GROUND | SDF_COMMON | SDF_OTHER;
5372 if (board->ai_maxdata > 0xffff)
5373 s->subdev_flags |= SDF_LSAMPL;
5374 if (devpriv->is_m_series)
5375 s->subdev_flags |= SDF_SOFT_CALIBRATED;
5376 s->n_chan = board->n_adchan;
5377 s->maxdata = board->ai_maxdata;
5378 s->range_table = ni_range_lkup[board->gainlkup];
5379 s->insn_read = ni_ai_insn_read;
5380 s->insn_config = ni_ai_insn_config;
5382 dev->read_subdev = s;
5383 s->subdev_flags |= SDF_CMD_READ;
5384 s->len_chanlist = 512;
5385 s->do_cmdtest = ni_ai_cmdtest;
5386 s->do_cmd = ni_ai_cmd;
5387 s->cancel = ni_ai_reset;
5388 s->poll = ni_ai_poll;
5389 s->munge = ni_ai_munge;
5392 s->async_dma_dir = DMA_FROM_DEVICE;
5395 /* reset the analog input configuration */
5396 ni_ai_reset(dev, s);
5398 s->type = COMEDI_SUBD_UNUSED;
5401 /* Analog Output subdevice */
5402 s = &dev->subdevices[NI_AO_SUBDEV];
5403 if (board->n_aochan) {
5404 s->type = COMEDI_SUBD_AO;
5405 s->subdev_flags = SDF_WRITABLE | SDF_DEGLITCH | SDF_GROUND;
5406 if (devpriv->is_m_series)
5407 s->subdev_flags |= SDF_SOFT_CALIBRATED;
5408 s->n_chan = board->n_aochan;
5409 s->maxdata = board->ao_maxdata;
5410 s->range_table = board->ao_range_table;
5411 s->insn_config = ni_ao_insn_config;
5412 s->insn_write = ni_ao_insn_write;
5414 ret = comedi_alloc_subdev_readback(s);
5419 * Along with the IRQ we need either a FIFO or DMA for
5420 * async command support.
5422 if (dev->irq && (board->ao_fifo_depth || devpriv->mite)) {
5423 dev->write_subdev = s;
5424 s->subdev_flags |= SDF_CMD_WRITE;
5425 s->len_chanlist = s->n_chan;
5426 s->do_cmdtest = ni_ao_cmdtest;
5427 s->do_cmd = ni_ao_cmd;
5428 s->cancel = ni_ao_reset;
5429 if (!devpriv->is_m_series)
5430 s->munge = ni_ao_munge;
5433 s->async_dma_dir = DMA_TO_DEVICE;
5436 if (devpriv->is_67xx)
5437 init_ao_67xx(dev, s);
5439 /* reset the analog output configuration */
5440 ni_ao_reset(dev, s);
5442 s->type = COMEDI_SUBD_UNUSED;
5445 /* Digital I/O subdevice */
5446 s = &dev->subdevices[NI_DIO_SUBDEV];
5447 s->type = COMEDI_SUBD_DIO;
5448 s->subdev_flags = SDF_WRITABLE | SDF_READABLE;
5449 s->n_chan = board->has_32dio_chan ? 32 : 8;
5451 s->range_table = &range_digital;
5452 if (devpriv->is_m_series) {
5453 s->subdev_flags |= SDF_LSAMPL;
5454 s->insn_bits = ni_m_series_dio_insn_bits;
5455 s->insn_config = ni_m_series_dio_insn_config;
5457 s->subdev_flags |= SDF_CMD_WRITE /* | SDF_CMD_READ */;
5458 s->len_chanlist = s->n_chan;
5459 s->do_cmdtest = ni_cdio_cmdtest;
5460 s->do_cmd = ni_cdio_cmd;
5461 s->cancel = ni_cdio_cancel;
5463 /* M-series boards use DMA */
5464 s->async_dma_dir = DMA_BIDIRECTIONAL;
5467 /* reset DIO and set all channels to inputs */
5468 ni_writel(dev, NI_M_CDO_CMD_RESET |
5471 ni_writel(dev, s->io_bits, NI_M_DIO_DIR_REG);
5473 s->insn_bits = ni_dio_insn_bits;
5474 s->insn_config = ni_dio_insn_config;
5476 /* set all channels to inputs */
5477 devpriv->dio_control = NISTC_DIO_CTRL_DIR(s->io_bits);
5478 ni_writew(dev, devpriv->dio_control, NISTC_DIO_CTRL_REG);
5482 s = &dev->subdevices[NI_8255_DIO_SUBDEV];
5483 if (board->has_8255) {
5484 ret = subdev_8255_init(dev, s, ni_8255_callback,
5489 s->type = COMEDI_SUBD_UNUSED;
5492 /* formerly general purpose counter/timer device, but no longer used */
5493 s = &dev->subdevices[NI_UNUSED_SUBDEV];
5494 s->type = COMEDI_SUBD_UNUSED;
5496 /* Calibration subdevice */
5497 s = &dev->subdevices[NI_CALIBRATION_SUBDEV];
5498 s->type = COMEDI_SUBD_CALIB;
5499 s->subdev_flags = SDF_INTERNAL;
5502 if (devpriv->is_m_series) {
5503 /* internal PWM output used for AI nonlinearity calibration */
5504 s->insn_config = ni_m_series_pwm_config;
5506 ni_writel(dev, 0x0, NI_M_CAL_PWM_REG);
5507 } else if (devpriv->is_6143) {
5508 /* internal PWM output used for AI nonlinearity calibration */
5509 s->insn_config = ni_6143_pwm_config;
5511 s->subdev_flags |= SDF_WRITABLE;
5512 s->insn_read = ni_calib_insn_read;
5513 s->insn_write = ni_calib_insn_write;
5515 /* setup the caldacs and find the real n_chan and maxdata */
5516 caldac_setup(dev, s);
5519 /* EEPROM subdevice */
5520 s = &dev->subdevices[NI_EEPROM_SUBDEV];
5521 s->type = COMEDI_SUBD_MEMORY;
5522 s->subdev_flags = SDF_READABLE | SDF_INTERNAL;
5524 if (devpriv->is_m_series) {
5525 s->n_chan = M_SERIES_EEPROM_SIZE;
5526 s->insn_read = ni_m_series_eeprom_insn_read;
5529 s->insn_read = ni_eeprom_insn_read;
5532 /* Digital I/O (PFI) subdevice */
5533 s = &dev->subdevices[NI_PFI_DIO_SUBDEV];
5534 s->type = COMEDI_SUBD_DIO;
5535 s->subdev_flags = SDF_READABLE | SDF_WRITABLE | SDF_INTERNAL;
5537 if (devpriv->is_m_series) {
5539 s->insn_bits = ni_pfi_insn_bits;
5541 ni_writew(dev, s->state, NI_M_PFI_DO_REG);
5542 for (i = 0; i < NUM_PFI_OUTPUT_SELECT_REGS; ++i) {
5543 ni_writew(dev, devpriv->pfi_output_select_reg[i],
5544 NI_M_PFI_OUT_SEL_REG(i));
5549 s->insn_config = ni_pfi_insn_config;
5551 ni_set_bits(dev, NISTC_IO_BIDIR_PIN_REG, ~0, 0);
5553 /* cs5529 calibration adc */
5554 s = &dev->subdevices[NI_CS5529_CALIBRATION_SUBDEV];
5555 if (devpriv->is_67xx) {
5556 s->type = COMEDI_SUBD_AI;
5557 s->subdev_flags = SDF_READABLE | SDF_DIFF | SDF_INTERNAL;
5558 /* one channel for each analog output channel */
5559 s->n_chan = board->n_aochan;
5560 s->maxdata = (1 << 16) - 1;
5561 s->range_table = &range_unknown; /* XXX */
5562 s->insn_read = cs5529_ai_insn_read;
5563 s->insn_config = NULL;
5566 s->type = COMEDI_SUBD_UNUSED;
5570 s = &dev->subdevices[NI_SERIAL_SUBDEV];
5571 s->type = COMEDI_SUBD_SERIAL;
5572 s->subdev_flags = SDF_READABLE | SDF_WRITABLE | SDF_INTERNAL;
5575 s->insn_config = ni_serial_insn_config;
5576 devpriv->serial_interval_ns = 0;
5577 devpriv->serial_hw_mode = 0;
5580 s = &dev->subdevices[NI_RTSI_SUBDEV];
5581 s->type = COMEDI_SUBD_DIO;
5582 s->subdev_flags = SDF_READABLE | SDF_WRITABLE | SDF_INTERNAL;
5585 s->insn_bits = ni_rtsi_insn_bits;
5586 s->insn_config = ni_rtsi_insn_config;
5589 /* allocate and initialize the gpct counter device */
5590 devpriv->counter_dev = ni_gpct_device_construct(dev,
5591 ni_gpct_write_register,
5592 ni_gpct_read_register,
5593 (devpriv->is_m_series)
5594 ? ni_gpct_variant_m_series
5595 : ni_gpct_variant_e_series,
5597 if (!devpriv->counter_dev)
5600 /* Counter (gpct) subdevices */
5601 for (i = 0; i < NUM_GPCT; ++i) {
5602 struct ni_gpct *gpct = &devpriv->counter_dev->counters[i];
5604 /* setup and initialize the counter */
5605 gpct->chip_index = 0;
5606 gpct->counter_index = i;
5607 ni_tio_init_counter(gpct);
5609 s = &dev->subdevices[NI_GPCT_SUBDEV(i)];
5610 s->type = COMEDI_SUBD_COUNTER;
5611 s->subdev_flags = SDF_READABLE | SDF_WRITABLE | SDF_LSAMPL;
5613 s->maxdata = (devpriv->is_m_series) ? 0xffffffff
5615 s->insn_read = ni_tio_insn_read;
5616 s->insn_write = ni_tio_insn_read;
5617 s->insn_config = ni_tio_insn_config;
5619 if (dev->irq && devpriv->mite) {
5620 s->subdev_flags |= SDF_CMD_READ /* | SDF_CMD_WRITE */;
5621 s->len_chanlist = 1;
5622 s->do_cmdtest = ni_tio_cmdtest;
5623 s->do_cmd = ni_gpct_cmd;
5624 s->cancel = ni_gpct_cancel;
5626 s->async_dma_dir = DMA_BIDIRECTIONAL;
5632 /* Frequency output subdevice */
5633 s = &dev->subdevices[NI_FREQ_OUT_SUBDEV];
5634 s->type = COMEDI_SUBD_COUNTER;
5635 s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
5638 s->insn_read = ni_freq_out_insn_read;
5639 s->insn_write = ni_freq_out_insn_write;
5640 s->insn_config = ni_freq_out_insn_config;
5644 (irq_polarity ? NISTC_INT_CTRL_INT_POL : 0) |
5645 (NISTC_INT_CTRL_3PIN_INT & 0) |
5646 NISTC_INT_CTRL_INTA_ENA |
5647 NISTC_INT_CTRL_INTB_ENA |
5648 NISTC_INT_CTRL_INTA_SEL(interrupt_pin) |
5649 NISTC_INT_CTRL_INTB_SEL(interrupt_pin),
5650 NISTC_INT_CTRL_REG);
5654 ni_writeb(dev, devpriv->ai_ao_select_reg, NI_E_DMA_AI_AO_SEL_REG);
5655 ni_writeb(dev, devpriv->g0_g1_select_reg, NI_E_DMA_G0_G1_SEL_REG);
5657 if (devpriv->is_6xxx) {
5658 ni_writeb(dev, 0, NI611X_MAGIC_REG);
5659 } else if (devpriv->is_m_series) {
5662 for (channel = 0; channel < board->n_aochan; ++channel) {
5664 NI_M_AO_WAVEFORM_ORDER_REG(channel));
5666 NI_M_AO_REF_ATTENUATION_REG(channel));
5668 ni_writeb(dev, 0x0, NI_M_AO_CALIB_REG);
5674 static void mio_common_detach(struct comedi_device *dev)
5676 struct ni_private *devpriv = dev->private;
5679 if (devpriv->counter_dev)
5680 ni_gpct_device_destroy(devpriv->counter_dev);