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 > sizeof(devpriv->ai_fifo_buffer) /
1170 sizeof(devpriv->ai_fifo_buffer[0])) {
1171 dev_err(dev->class_dev,
1172 "bug! ai_fifo_buffer too small\n");
1173 async->events |= COMEDI_CB_ERROR;
1176 for (i = 0; i < n; i++) {
1177 devpriv->ai_fifo_buffer[i] =
1178 ni_readw(dev, NI_E_AI_FIFO_DATA_REG);
1180 comedi_buf_write_samples(s, devpriv->ai_fifo_buffer, n);
1184 static void ni_handle_fifo_half_full(struct comedi_device *dev)
1186 const struct ni_board_struct *board = dev->board_ptr;
1187 struct comedi_subdevice *s = dev->read_subdev;
1190 n = board->ai_fifo_depth / 2;
1192 ni_ai_fifo_read(dev, s, n);
1199 static void ni_handle_fifo_dregs(struct comedi_device *dev)
1201 struct ni_private *devpriv = dev->private;
1202 struct comedi_subdevice *s = dev->read_subdev;
1204 unsigned short data;
1205 unsigned short fifo_empty;
1208 if (devpriv->is_611x) {
1209 while ((ni_stc_readw(dev, NISTC_AI_STATUS1_REG) &
1210 NISTC_AI_STATUS1_FIFO_E) == 0) {
1211 dl = ni_readl(dev, NI611X_AI_FIFO_DATA_REG);
1213 /* This may get the hi/lo data in the wrong order */
1215 comedi_buf_write_samples(s, &data, 1);
1217 comedi_buf_write_samples(s, &data, 1);
1219 } else if (devpriv->is_6143) {
1221 while (ni_readl(dev, NI6143_AI_FIFO_STATUS_REG) & 0x04) {
1222 dl = ni_readl(dev, NI6143_AI_FIFO_DATA_REG);
1224 /* This may get the hi/lo data in the wrong order */
1226 comedi_buf_write_samples(s, &data, 1);
1228 comedi_buf_write_samples(s, &data, 1);
1231 /* Check if stranded sample is present */
1232 if (ni_readl(dev, NI6143_AI_FIFO_STATUS_REG) & 0x01) {
1233 /* Get stranded sample into FIFO */
1234 ni_writel(dev, 0x01, NI6143_AI_FIFO_CTRL_REG);
1235 dl = ni_readl(dev, NI6143_AI_FIFO_DATA_REG);
1236 data = (dl >> 16) & 0xffff;
1237 comedi_buf_write_samples(s, &data, 1);
1241 fifo_empty = ni_stc_readw(dev, NISTC_AI_STATUS1_REG) &
1242 NISTC_AI_STATUS1_FIFO_E;
1243 while (fifo_empty == 0) {
1246 sizeof(devpriv->ai_fifo_buffer) /
1247 sizeof(devpriv->ai_fifo_buffer[0]); i++) {
1248 fifo_empty = ni_stc_readw(dev,
1249 NISTC_AI_STATUS1_REG) &
1250 NISTC_AI_STATUS1_FIFO_E;
1253 devpriv->ai_fifo_buffer[i] =
1254 ni_readw(dev, NI_E_AI_FIFO_DATA_REG);
1256 comedi_buf_write_samples(s, devpriv->ai_fifo_buffer, i);
1261 static void get_last_sample_611x(struct comedi_device *dev)
1263 struct ni_private *devpriv = dev->private;
1264 struct comedi_subdevice *s = dev->read_subdev;
1265 unsigned short data;
1268 if (!devpriv->is_611x)
1271 /* Check if there's a single sample stuck in the FIFO */
1272 if (ni_readb(dev, NI_E_STATUS_REG) & 0x80) {
1273 dl = ni_readl(dev, NI611X_AI_FIFO_DATA_REG);
1275 comedi_buf_write_samples(s, &data, 1);
1279 static void get_last_sample_6143(struct comedi_device *dev)
1281 struct ni_private *devpriv = dev->private;
1282 struct comedi_subdevice *s = dev->read_subdev;
1283 unsigned short data;
1286 if (!devpriv->is_6143)
1289 /* Check if there's a single sample stuck in the FIFO */
1290 if (ni_readl(dev, NI6143_AI_FIFO_STATUS_REG) & 0x01) {
1291 /* Get stranded sample into FIFO */
1292 ni_writel(dev, 0x01, NI6143_AI_FIFO_CTRL_REG);
1293 dl = ni_readl(dev, NI6143_AI_FIFO_DATA_REG);
1295 /* This may get the hi/lo data in the wrong order */
1296 data = (dl >> 16) & 0xffff;
1297 comedi_buf_write_samples(s, &data, 1);
1301 static void shutdown_ai_command(struct comedi_device *dev)
1303 struct comedi_subdevice *s = dev->read_subdev;
1306 ni_ai_drain_dma(dev);
1308 ni_handle_fifo_dregs(dev);
1309 get_last_sample_611x(dev);
1310 get_last_sample_6143(dev);
1312 s->async->events |= COMEDI_CB_EOA;
1315 static void ni_handle_eos(struct comedi_device *dev, struct comedi_subdevice *s)
1317 struct ni_private *devpriv = dev->private;
1319 if (devpriv->aimode == AIMODE_SCAN) {
1321 static const int timeout = 10;
1324 for (i = 0; i < timeout; i++) {
1325 ni_sync_ai_dma(dev);
1326 if ((s->async->events & COMEDI_CB_EOS))
1331 ni_handle_fifo_dregs(dev);
1332 s->async->events |= COMEDI_CB_EOS;
1335 /* handle special case of single scan */
1336 if (devpriv->ai_cmd2 & NISTC_AI_CMD2_END_ON_EOS)
1337 shutdown_ai_command(dev);
1340 static void handle_gpct_interrupt(struct comedi_device *dev,
1341 unsigned short counter_index)
1344 struct ni_private *devpriv = dev->private;
1345 struct comedi_subdevice *s;
1347 s = &dev->subdevices[NI_GPCT_SUBDEV(counter_index)];
1349 ni_tio_handle_interrupt(&devpriv->counter_dev->counters[counter_index],
1351 comedi_handle_events(dev, s);
1355 static void ack_a_interrupt(struct comedi_device *dev, unsigned short a_status)
1357 unsigned short ack = 0;
1359 if (a_status & NISTC_AI_STATUS1_SC_TC)
1360 ack |= NISTC_INTA_ACK_AI_SC_TC;
1361 if (a_status & NISTC_AI_STATUS1_START1)
1362 ack |= NISTC_INTA_ACK_AI_START1;
1363 if (a_status & NISTC_AI_STATUS1_START)
1364 ack |= NISTC_INTA_ACK_AI_START;
1365 if (a_status & NISTC_AI_STATUS1_STOP)
1366 ack |= NISTC_INTA_ACK_AI_STOP;
1368 ni_stc_writew(dev, ack, NISTC_INTA_ACK_REG);
1371 static void handle_a_interrupt(struct comedi_device *dev, unsigned short status,
1372 unsigned ai_mite_status)
1374 struct comedi_subdevice *s = dev->read_subdev;
1375 struct comedi_cmd *cmd = &s->async->cmd;
1377 /* 67xx boards don't have ai subdevice, but their gpct0 might generate an a interrupt */
1378 if (s->type == COMEDI_SUBD_UNUSED)
1382 if (ai_mite_status & CHSR_LINKC)
1383 ni_sync_ai_dma(dev);
1385 if (ai_mite_status & ~(CHSR_INT | CHSR_LINKC | CHSR_DONE | CHSR_MRDY |
1386 CHSR_DRDY | CHSR_DRQ1 | CHSR_DRQ0 | CHSR_ERROR |
1387 CHSR_SABORT | CHSR_XFERR | CHSR_LxERR_mask)) {
1388 dev_err(dev->class_dev,
1389 "unknown mite interrupt (ai_mite_status=%08x)\n",
1391 s->async->events |= COMEDI_CB_ERROR;
1392 /* disable_irq(dev->irq); */
1396 /* test for all uncommon interrupt events at the same time */
1397 if (status & (NISTC_AI_STATUS1_ERR |
1398 NISTC_AI_STATUS1_SC_TC | NISTC_AI_STATUS1_START1)) {
1399 if (status == 0xffff) {
1400 dev_err(dev->class_dev, "Card removed?\n");
1401 /* we probably aren't even running a command now,
1402 * so it's a good idea to be careful. */
1403 if (comedi_is_subdevice_running(s)) {
1404 s->async->events |= COMEDI_CB_ERROR;
1405 comedi_handle_events(dev, s);
1409 if (status & NISTC_AI_STATUS1_ERR) {
1410 dev_err(dev->class_dev, "ai error a_status=%04x\n",
1413 shutdown_ai_command(dev);
1415 s->async->events |= COMEDI_CB_ERROR;
1416 if (status & NISTC_AI_STATUS1_OVER)
1417 s->async->events |= COMEDI_CB_OVERFLOW;
1419 comedi_handle_events(dev, s);
1422 if (status & NISTC_AI_STATUS1_SC_TC) {
1423 if (cmd->stop_src == TRIG_COUNT)
1424 shutdown_ai_command(dev);
1428 if (status & NISTC_AI_STATUS1_FIFO_HF) {
1430 static const int timeout = 10;
1431 /* pcmcia cards (at least 6036) seem to stop producing interrupts if we
1432 *fail to get the fifo less than half full, so loop to be sure.*/
1433 for (i = 0; i < timeout; ++i) {
1434 ni_handle_fifo_half_full(dev);
1435 if ((ni_stc_readw(dev, NISTC_AI_STATUS1_REG) &
1436 NISTC_AI_STATUS1_FIFO_HF) == 0)
1440 #endif /* !PCIDMA */
1442 if (status & NISTC_AI_STATUS1_STOP)
1443 ni_handle_eos(dev, s);
1445 comedi_handle_events(dev, s);
1448 static void ack_b_interrupt(struct comedi_device *dev, unsigned short b_status)
1450 unsigned short ack = 0;
1452 if (b_status & NISTC_AO_STATUS1_BC_TC)
1453 ack |= NISTC_INTB_ACK_AO_BC_TC;
1454 if (b_status & NISTC_AO_STATUS1_OVERRUN)
1455 ack |= NISTC_INTB_ACK_AO_ERR;
1456 if (b_status & NISTC_AO_STATUS1_START)
1457 ack |= NISTC_INTB_ACK_AO_START;
1458 if (b_status & NISTC_AO_STATUS1_START1)
1459 ack |= NISTC_INTB_ACK_AO_START1;
1460 if (b_status & NISTC_AO_STATUS1_UC_TC)
1461 ack |= NISTC_INTB_ACK_AO_UC_TC;
1462 if (b_status & NISTC_AO_STATUS1_UI2_TC)
1463 ack |= NISTC_INTB_ACK_AO_UI2_TC;
1464 if (b_status & NISTC_AO_STATUS1_UPDATE)
1465 ack |= NISTC_INTB_ACK_AO_UPDATE;
1467 ni_stc_writew(dev, ack, NISTC_INTB_ACK_REG);
1470 static void handle_b_interrupt(struct comedi_device *dev,
1471 unsigned short b_status, unsigned ao_mite_status)
1473 struct comedi_subdevice *s = dev->write_subdev;
1474 /* unsigned short ack=0; */
1477 /* Currently, mite.c requires us to handle LINKC */
1478 if (ao_mite_status & CHSR_LINKC) {
1479 struct ni_private *devpriv = dev->private;
1481 mite_handle_b_linkc(devpriv->mite, dev);
1484 if (ao_mite_status & ~(CHSR_INT | CHSR_LINKC | CHSR_DONE | CHSR_MRDY |
1485 CHSR_DRDY | CHSR_DRQ1 | CHSR_DRQ0 | CHSR_ERROR |
1486 CHSR_SABORT | CHSR_XFERR | CHSR_LxERR_mask)) {
1487 dev_err(dev->class_dev,
1488 "unknown mite interrupt (ao_mite_status=%08x)\n",
1490 s->async->events |= COMEDI_CB_ERROR;
1494 if (b_status == 0xffff)
1496 if (b_status & NISTC_AO_STATUS1_OVERRUN) {
1497 dev_err(dev->class_dev,
1498 "AO FIFO underrun status=0x%04x status2=0x%04x\n",
1499 b_status, ni_stc_readw(dev, NISTC_AO_STATUS2_REG));
1500 s->async->events |= COMEDI_CB_OVERFLOW;
1503 if (b_status & NISTC_AO_STATUS1_BC_TC)
1504 s->async->events |= COMEDI_CB_EOA;
1507 if (b_status & NISTC_AO_STATUS1_FIFO_REQ) {
1510 ret = ni_ao_fifo_half_empty(dev, s);
1512 dev_err(dev->class_dev, "AO buffer underrun\n");
1513 ni_set_bits(dev, NISTC_INTB_ENA_REG,
1514 NISTC_INTB_ENA_AO_FIFO |
1515 NISTC_INTB_ENA_AO_ERR, 0);
1516 s->async->events |= COMEDI_CB_OVERFLOW;
1521 comedi_handle_events(dev, s);
1524 static void ni_ai_munge(struct comedi_device *dev, struct comedi_subdevice *s,
1525 void *data, unsigned int num_bytes,
1526 unsigned int chan_index)
1528 struct ni_private *devpriv = dev->private;
1529 struct comedi_async *async = s->async;
1530 struct comedi_cmd *cmd = &async->cmd;
1531 unsigned int nsamples = comedi_bytes_to_samples(s, num_bytes);
1532 unsigned short *array = data;
1533 unsigned int *larray = data;
1536 __le16 *barray = data;
1537 __le32 *blarray = data;
1540 for (i = 0; i < nsamples; i++) {
1542 if (s->subdev_flags & SDF_LSAMPL)
1543 larray[i] = le32_to_cpu(blarray[i]);
1545 array[i] = le16_to_cpu(barray[i]);
1547 if (s->subdev_flags & SDF_LSAMPL)
1548 larray[i] += devpriv->ai_offset[chan_index];
1550 array[i] += devpriv->ai_offset[chan_index];
1552 chan_index %= cmd->chanlist_len;
1558 static int ni_ai_setup_MITE_dma(struct comedi_device *dev)
1560 struct ni_private *devpriv = dev->private;
1561 struct comedi_subdevice *s = dev->read_subdev;
1563 unsigned long flags;
1565 retval = ni_request_ai_mite_channel(dev);
1569 /* write alloc the entire buffer */
1570 comedi_buf_write_alloc(s, s->async->prealloc_bufsz);
1572 spin_lock_irqsave(&devpriv->mite_channel_lock, flags);
1573 if (!devpriv->ai_mite_chan) {
1574 spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
1578 if (devpriv->is_611x || devpriv->is_6143)
1579 mite_prep_dma(devpriv->ai_mite_chan, 32, 16);
1580 else if (devpriv->is_628x)
1581 mite_prep_dma(devpriv->ai_mite_chan, 32, 32);
1583 mite_prep_dma(devpriv->ai_mite_chan, 16, 16);
1586 mite_dma_arm(devpriv->ai_mite_chan);
1587 spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
1592 static int ni_ao_setup_MITE_dma(struct comedi_device *dev)
1594 struct ni_private *devpriv = dev->private;
1595 struct comedi_subdevice *s = dev->write_subdev;
1597 unsigned long flags;
1599 retval = ni_request_ao_mite_channel(dev);
1603 /* read alloc the entire buffer */
1604 comedi_buf_read_alloc(s, s->async->prealloc_bufsz);
1606 spin_lock_irqsave(&devpriv->mite_channel_lock, flags);
1607 if (devpriv->ao_mite_chan) {
1608 if (devpriv->is_611x || devpriv->is_6713) {
1609 mite_prep_dma(devpriv->ao_mite_chan, 32, 32);
1611 /* doing 32 instead of 16 bit wide transfers from memory
1612 makes the mite do 32 bit pci transfers, doubling pci bandwidth. */
1613 mite_prep_dma(devpriv->ao_mite_chan, 16, 32);
1615 mite_dma_arm(devpriv->ao_mite_chan);
1619 spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
1627 used for both cancel ioctl and board initialization
1629 this is pretty harsh for a cancel, but it works...
1632 static int ni_ai_reset(struct comedi_device *dev, struct comedi_subdevice *s)
1634 struct ni_private *devpriv = dev->private;
1635 unsigned ai_personal;
1636 unsigned ai_out_ctrl;
1638 ni_release_ai_mite_channel(dev);
1639 /* ai configuration */
1640 ni_stc_writew(dev, NISTC_RESET_AI_CFG_START | NISTC_RESET_AI,
1643 ni_set_bits(dev, NISTC_INTA_ENA_REG, NISTC_INTA_ENA_AI_MASK, 0);
1645 ni_clear_ai_fifo(dev);
1647 if (!devpriv->is_6143)
1648 ni_writeb(dev, NI_E_MISC_CMD_EXT_ATRIG, NI_E_MISC_CMD_REG);
1650 ni_stc_writew(dev, NISTC_AI_CMD1_DISARM, NISTC_AI_CMD1_REG);
1651 ni_stc_writew(dev, NISTC_AI_MODE1_START_STOP |
1653 /*| NISTC_AI_MODE1_TRIGGER_ONCE */,
1654 NISTC_AI_MODE1_REG);
1655 ni_stc_writew(dev, 0, NISTC_AI_MODE2_REG);
1656 /* generate FIFO interrupts on non-empty */
1657 ni_stc_writew(dev, NISTC_AI_MODE3_FIFO_MODE_NE,
1658 NISTC_AI_MODE3_REG);
1660 ai_personal = NISTC_AI_PERSONAL_SHIFTIN_PW |
1661 NISTC_AI_PERSONAL_SOC_POLARITY |
1662 NISTC_AI_PERSONAL_LOCALMUX_CLK_PW;
1663 ai_out_ctrl = NISTC_AI_OUT_CTRL_SCAN_IN_PROG_SEL(3) |
1664 NISTC_AI_OUT_CTRL_EXTMUX_CLK_SEL(0) |
1665 NISTC_AI_OUT_CTRL_LOCALMUX_CLK_SEL(2) |
1666 NISTC_AI_OUT_CTRL_SC_TC_SEL(3);
1667 if (devpriv->is_611x) {
1668 ai_out_ctrl |= NISTC_AI_OUT_CTRL_CONVERT_HIGH;
1669 } else if (devpriv->is_6143) {
1670 ai_out_ctrl |= NISTC_AI_OUT_CTRL_CONVERT_LOW;
1672 ai_personal |= NISTC_AI_PERSONAL_CONVERT_PW;
1673 if (devpriv->is_622x)
1674 ai_out_ctrl |= NISTC_AI_OUT_CTRL_CONVERT_HIGH;
1676 ai_out_ctrl |= NISTC_AI_OUT_CTRL_CONVERT_LOW;
1678 ni_stc_writew(dev, ai_personal, NISTC_AI_PERSONAL_REG);
1679 ni_stc_writew(dev, ai_out_ctrl, NISTC_AI_OUT_CTRL_REG);
1681 /* the following registers should not be changed, because there
1682 * are no backup registers in devpriv. If you want to change
1683 * any of these, add a backup register and other appropriate code:
1684 * NISTC_AI_MODE1_REG
1685 * NISTC_AI_MODE3_REG
1686 * NISTC_AI_PERSONAL_REG
1687 * NISTC_AI_OUT_CTRL_REG
1690 /* clear interrupts */
1691 ni_stc_writew(dev, NISTC_INTA_ACK_AI_ALL, NISTC_INTA_ACK_REG);
1693 ni_stc_writew(dev, NISTC_RESET_AI_CFG_END, NISTC_RESET_REG);
1698 static int ni_ai_poll(struct comedi_device *dev, struct comedi_subdevice *s)
1700 unsigned long flags;
1703 /* lock to avoid race with interrupt handler */
1704 spin_lock_irqsave(&dev->spinlock, flags);
1706 ni_handle_fifo_dregs(dev);
1708 ni_sync_ai_dma(dev);
1710 count = comedi_buf_n_bytes_ready(s);
1711 spin_unlock_irqrestore(&dev->spinlock, flags);
1716 static void ni_prime_channelgain_list(struct comedi_device *dev)
1720 ni_stc_writew(dev, NISTC_AI_CMD1_CONVERT_PULSE, NISTC_AI_CMD1_REG);
1721 for (i = 0; i < NI_TIMEOUT; ++i) {
1722 if (!(ni_stc_readw(dev, NISTC_AI_STATUS1_REG) &
1723 NISTC_AI_STATUS1_FIFO_E)) {
1724 ni_stc_writew(dev, 1, NISTC_ADC_FIFO_CLR_REG);
1729 dev_err(dev->class_dev, "timeout loading channel/gain list\n");
1732 static void ni_m_series_load_channelgain_list(struct comedi_device *dev,
1733 unsigned int n_chan,
1736 const struct ni_board_struct *board = dev->board_ptr;
1737 struct ni_private *devpriv = dev->private;
1738 unsigned int chan, range, aref;
1740 unsigned int dither;
1741 unsigned range_code;
1743 ni_stc_writew(dev, 1, NISTC_CFG_MEM_CLR_REG);
1745 if ((list[0] & CR_ALT_SOURCE)) {
1746 unsigned bypass_bits;
1748 chan = CR_CHAN(list[0]);
1749 range = CR_RANGE(list[0]);
1750 range_code = ni_gainlkup[board->gainlkup][range];
1751 dither = (list[0] & CR_ALT_FILTER) != 0;
1752 bypass_bits = NI_M_CFG_BYPASS_FIFO |
1753 NI_M_CFG_BYPASS_AI_CHAN(chan) |
1754 NI_M_CFG_BYPASS_AI_GAIN(range_code) |
1755 devpriv->ai_calib_source;
1757 bypass_bits |= NI_M_CFG_BYPASS_AI_DITHER;
1758 /* don't use 2's complement encoding */
1759 bypass_bits |= NI_M_CFG_BYPASS_AI_POLARITY;
1760 ni_writel(dev, bypass_bits, NI_M_CFG_BYPASS_FIFO_REG);
1762 ni_writel(dev, 0, NI_M_CFG_BYPASS_FIFO_REG);
1764 for (i = 0; i < n_chan; i++) {
1765 unsigned config_bits = 0;
1767 chan = CR_CHAN(list[i]);
1768 aref = CR_AREF(list[i]);
1769 range = CR_RANGE(list[i]);
1770 dither = (list[i] & CR_ALT_FILTER) != 0;
1772 range_code = ni_gainlkup[board->gainlkup][range];
1773 devpriv->ai_offset[i] = 0;
1776 config_bits |= NI_M_AI_CFG_CHAN_TYPE_DIFF;
1779 config_bits |= NI_M_AI_CFG_CHAN_TYPE_COMMON;
1782 config_bits |= NI_M_AI_CFG_CHAN_TYPE_GROUND;
1787 config_bits |= NI_M_AI_CFG_CHAN_SEL(chan);
1788 config_bits |= NI_M_AI_CFG_BANK_SEL(chan);
1789 config_bits |= NI_M_AI_CFG_GAIN(range_code);
1790 if (i == n_chan - 1)
1791 config_bits |= NI_M_AI_CFG_LAST_CHAN;
1793 config_bits |= NI_M_AI_CFG_DITHER;
1794 /* don't use 2's complement encoding */
1795 config_bits |= NI_M_AI_CFG_POLARITY;
1796 ni_writew(dev, config_bits, NI_M_AI_CFG_FIFO_DATA_REG);
1798 ni_prime_channelgain_list(dev);
1802 * Notes on the 6110 and 6111:
1803 * These boards a slightly different than the rest of the series, since
1804 * they have multiple A/D converters.
1805 * From the driver side, the configuration memory is a
1807 * Configuration Memory Low:
1809 * bit 8: unipolar/bipolar (should be 0 for bipolar)
1810 * bits 0-3: gain. This is 4 bits instead of 3 for the other boards
1811 * 1001 gain=0.1 (+/- 50)
1820 * Configuration Memory High:
1821 * bits 12-14: Channel Type
1822 * 001 for differential
1823 * 000 for calibration
1824 * bit 11: coupling (this is not currently handled)
1828 * valid channels are 0-3
1830 static void ni_load_channelgain_list(struct comedi_device *dev,
1831 struct comedi_subdevice *s,
1832 unsigned int n_chan, unsigned int *list)
1834 const struct ni_board_struct *board = dev->board_ptr;
1835 struct ni_private *devpriv = dev->private;
1836 unsigned int offset = (s->maxdata + 1) >> 1;
1837 unsigned int chan, range, aref;
1839 unsigned int hi, lo;
1840 unsigned int dither;
1842 if (devpriv->is_m_series) {
1843 ni_m_series_load_channelgain_list(dev, n_chan, list);
1846 if (n_chan == 1 && !devpriv->is_611x && !devpriv->is_6143) {
1847 if (devpriv->changain_state
1848 && devpriv->changain_spec == list[0]) {
1852 devpriv->changain_state = 1;
1853 devpriv->changain_spec = list[0];
1855 devpriv->changain_state = 0;
1858 ni_stc_writew(dev, 1, NISTC_CFG_MEM_CLR_REG);
1860 /* Set up Calibration mode if required */
1861 if (devpriv->is_6143) {
1862 if ((list[0] & CR_ALT_SOURCE)
1863 && !devpriv->ai_calib_source_enabled) {
1864 /* Strobe Relay enable bit */
1865 ni_writew(dev, devpriv->ai_calib_source |
1866 NI6143_CALIB_CHAN_RELAY_ON,
1867 NI6143_CALIB_CHAN_REG);
1868 ni_writew(dev, devpriv->ai_calib_source,
1869 NI6143_CALIB_CHAN_REG);
1870 devpriv->ai_calib_source_enabled = 1;
1871 msleep_interruptible(100); /* Allow relays to change */
1872 } else if (!(list[0] & CR_ALT_SOURCE)
1873 && devpriv->ai_calib_source_enabled) {
1874 /* Strobe Relay disable bit */
1875 ni_writew(dev, devpriv->ai_calib_source |
1876 NI6143_CALIB_CHAN_RELAY_OFF,
1877 NI6143_CALIB_CHAN_REG);
1878 ni_writew(dev, devpriv->ai_calib_source,
1879 NI6143_CALIB_CHAN_REG);
1880 devpriv->ai_calib_source_enabled = 0;
1881 msleep_interruptible(100); /* Allow relays to change */
1885 for (i = 0; i < n_chan; i++) {
1886 if (!devpriv->is_6143 && (list[i] & CR_ALT_SOURCE))
1887 chan = devpriv->ai_calib_source;
1889 chan = CR_CHAN(list[i]);
1890 aref = CR_AREF(list[i]);
1891 range = CR_RANGE(list[i]);
1892 dither = (list[i] & CR_ALT_FILTER) != 0;
1894 /* fix the external/internal range differences */
1895 range = ni_gainlkup[board->gainlkup][range];
1896 if (devpriv->is_611x)
1897 devpriv->ai_offset[i] = offset;
1899 devpriv->ai_offset[i] = (range & 0x100) ? 0 : offset;
1902 if ((list[i] & CR_ALT_SOURCE)) {
1903 if (devpriv->is_611x)
1904 ni_writew(dev, CR_CHAN(list[i]) & 0x0003,
1905 NI611X_CALIB_CHAN_SEL_REG);
1907 if (devpriv->is_611x)
1909 else if (devpriv->is_6143)
1913 hi |= NI_E_AI_CFG_HI_TYPE_DIFF;
1916 hi |= NI_E_AI_CFG_HI_TYPE_COMMON;
1919 hi |= NI_E_AI_CFG_HI_TYPE_GROUND;
1925 hi |= NI_E_AI_CFG_HI_CHAN(chan);
1927 ni_writew(dev, hi, NI_E_AI_CFG_HI_REG);
1929 if (!devpriv->is_6143) {
1930 lo = NI_E_AI_CFG_LO_GAIN(range);
1932 if (i == n_chan - 1)
1933 lo |= NI_E_AI_CFG_LO_LAST_CHAN;
1935 lo |= NI_E_AI_CFG_LO_DITHER;
1937 ni_writew(dev, lo, NI_E_AI_CFG_LO_REG);
1941 /* prime the channel/gain list */
1942 if (!devpriv->is_611x && !devpriv->is_6143)
1943 ni_prime_channelgain_list(dev);
1946 static int ni_ai_insn_read(struct comedi_device *dev,
1947 struct comedi_subdevice *s,
1948 struct comedi_insn *insn,
1951 struct ni_private *devpriv = dev->private;
1952 unsigned int mask = (s->maxdata + 1) >> 1;
1958 ni_load_channelgain_list(dev, s, 1, &insn->chanspec);
1960 ni_clear_ai_fifo(dev);
1962 signbits = devpriv->ai_offset[0];
1963 if (devpriv->is_611x) {
1964 for (n = 0; n < num_adc_stages_611x; n++) {
1965 ni_stc_writew(dev, NISTC_AI_CMD1_CONVERT_PULSE,
1969 for (n = 0; n < insn->n; n++) {
1970 ni_stc_writew(dev, NISTC_AI_CMD1_CONVERT_PULSE,
1972 /* The 611x has screwy 32-bit FIFOs. */
1974 for (i = 0; i < NI_TIMEOUT; i++) {
1975 if (ni_readb(dev, NI_E_STATUS_REG) & 0x80) {
1977 NI611X_AI_FIFO_DATA_REG);
1982 if (!(ni_stc_readw(dev, NISTC_AI_STATUS1_REG) &
1983 NISTC_AI_STATUS1_FIFO_E)) {
1985 NI611X_AI_FIFO_DATA_REG);
1990 if (i == NI_TIMEOUT) {
1991 dev_err(dev->class_dev, "timeout\n");
1997 } else if (devpriv->is_6143) {
1998 for (n = 0; n < insn->n; n++) {
1999 ni_stc_writew(dev, NISTC_AI_CMD1_CONVERT_PULSE,
2002 /* The 6143 has 32-bit FIFOs. You need to strobe a bit to move a single 16bit stranded sample into the FIFO */
2004 for (i = 0; i < NI_TIMEOUT; i++) {
2005 if (ni_readl(dev, NI6143_AI_FIFO_STATUS_REG) &
2007 /* Get stranded sample into FIFO */
2008 ni_writel(dev, 0x01,
2009 NI6143_AI_FIFO_CTRL_REG);
2011 NI6143_AI_FIFO_DATA_REG);
2015 if (i == NI_TIMEOUT) {
2016 dev_err(dev->class_dev, "timeout\n");
2019 data[n] = (((dl >> 16) & 0xFFFF) + signbits) & 0xFFFF;
2022 for (n = 0; n < insn->n; n++) {
2023 ni_stc_writew(dev, NISTC_AI_CMD1_CONVERT_PULSE,
2025 for (i = 0; i < NI_TIMEOUT; i++) {
2026 if (!(ni_stc_readw(dev, NISTC_AI_STATUS1_REG) &
2027 NISTC_AI_STATUS1_FIFO_E))
2030 if (i == NI_TIMEOUT) {
2031 dev_err(dev->class_dev, "timeout\n");
2034 if (devpriv->is_m_series) {
2035 dl = ni_readl(dev, NI_M_AI_FIFO_DATA_REG);
2039 d = ni_readw(dev, NI_E_AI_FIFO_DATA_REG);
2040 d += signbits; /* subtle: needs to be short addition */
2048 static int ni_ns_to_timer(const struct comedi_device *dev, unsigned nanosec,
2051 struct ni_private *devpriv = dev->private;
2054 switch (flags & CMDF_ROUND_MASK) {
2055 case CMDF_ROUND_NEAREST:
2057 divider = (nanosec + devpriv->clock_ns / 2) / devpriv->clock_ns;
2059 case CMDF_ROUND_DOWN:
2060 divider = (nanosec) / devpriv->clock_ns;
2063 divider = (nanosec + devpriv->clock_ns - 1) / devpriv->clock_ns;
2069 static unsigned ni_timer_to_ns(const struct comedi_device *dev, int timer)
2071 struct ni_private *devpriv = dev->private;
2073 return devpriv->clock_ns * (timer + 1);
2076 static unsigned ni_min_ai_scan_period_ns(struct comedi_device *dev,
2077 unsigned num_channels)
2079 const struct ni_board_struct *board = dev->board_ptr;
2080 struct ni_private *devpriv = dev->private;
2082 /* simultaneously-sampled inputs */
2083 if (devpriv->is_611x || devpriv->is_6143)
2084 return board->ai_speed;
2086 /* multiplexed inputs */
2087 return board->ai_speed * num_channels;
2090 static int ni_ai_cmdtest(struct comedi_device *dev, struct comedi_subdevice *s,
2091 struct comedi_cmd *cmd)
2093 const struct ni_board_struct *board = dev->board_ptr;
2094 struct ni_private *devpriv = dev->private;
2097 unsigned int sources;
2099 /* Step 1 : check if triggers are trivially valid */
2101 err |= comedi_check_trigger_src(&cmd->start_src,
2102 TRIG_NOW | TRIG_INT | TRIG_EXT);
2103 err |= comedi_check_trigger_src(&cmd->scan_begin_src,
2104 TRIG_TIMER | TRIG_EXT);
2106 sources = TRIG_TIMER | TRIG_EXT;
2107 if (devpriv->is_611x || devpriv->is_6143)
2108 sources |= TRIG_NOW;
2109 err |= comedi_check_trigger_src(&cmd->convert_src, sources);
2111 err |= comedi_check_trigger_src(&cmd->scan_end_src, TRIG_COUNT);
2112 err |= comedi_check_trigger_src(&cmd->stop_src, TRIG_COUNT | TRIG_NONE);
2117 /* Step 2a : make sure trigger sources are unique */
2119 err |= comedi_check_trigger_is_unique(cmd->start_src);
2120 err |= comedi_check_trigger_is_unique(cmd->scan_begin_src);
2121 err |= comedi_check_trigger_is_unique(cmd->convert_src);
2122 err |= comedi_check_trigger_is_unique(cmd->stop_src);
2124 /* Step 2b : and mutually compatible */
2129 /* Step 3: check if arguments are trivially valid */
2131 switch (cmd->start_src) {
2134 err |= comedi_check_trigger_arg_is(&cmd->start_arg, 0);
2137 tmp = CR_CHAN(cmd->start_arg);
2141 tmp |= (cmd->start_arg & (CR_INVERT | CR_EDGE));
2142 err |= comedi_check_trigger_arg_is(&cmd->start_arg, tmp);
2146 if (cmd->scan_begin_src == TRIG_TIMER) {
2147 err |= comedi_check_trigger_arg_min(&cmd->scan_begin_arg,
2148 ni_min_ai_scan_period_ns(dev, cmd->chanlist_len));
2149 err |= comedi_check_trigger_arg_max(&cmd->scan_begin_arg,
2152 } else if (cmd->scan_begin_src == TRIG_EXT) {
2153 /* external trigger */
2154 unsigned int tmp = CR_CHAN(cmd->scan_begin_arg);
2158 tmp |= (cmd->scan_begin_arg & (CR_INVERT | CR_EDGE));
2159 err |= comedi_check_trigger_arg_is(&cmd->scan_begin_arg, tmp);
2160 } else { /* TRIG_OTHER */
2161 err |= comedi_check_trigger_arg_is(&cmd->scan_begin_arg, 0);
2164 if (cmd->convert_src == TRIG_TIMER) {
2165 if (devpriv->is_611x || devpriv->is_6143) {
2166 err |= comedi_check_trigger_arg_is(&cmd->convert_arg,
2169 err |= comedi_check_trigger_arg_min(&cmd->convert_arg,
2171 err |= comedi_check_trigger_arg_max(&cmd->convert_arg,
2175 } else if (cmd->convert_src == TRIG_EXT) {
2176 /* external trigger */
2177 unsigned int tmp = CR_CHAN(cmd->convert_arg);
2181 tmp |= (cmd->convert_arg & (CR_ALT_FILTER | CR_INVERT));
2182 err |= comedi_check_trigger_arg_is(&cmd->convert_arg, tmp);
2183 } else if (cmd->convert_src == TRIG_NOW) {
2184 err |= comedi_check_trigger_arg_is(&cmd->convert_arg, 0);
2187 err |= comedi_check_trigger_arg_is(&cmd->scan_end_arg,
2190 if (cmd->stop_src == TRIG_COUNT) {
2191 unsigned int max_count = 0x01000000;
2193 if (devpriv->is_611x)
2194 max_count -= num_adc_stages_611x;
2195 err |= comedi_check_trigger_arg_max(&cmd->stop_arg, max_count);
2196 err |= comedi_check_trigger_arg_min(&cmd->stop_arg, 1);
2199 err |= comedi_check_trigger_arg_is(&cmd->stop_arg, 0);
2205 /* step 4: fix up any arguments */
2207 if (cmd->scan_begin_src == TRIG_TIMER) {
2208 tmp = cmd->scan_begin_arg;
2209 cmd->scan_begin_arg =
2210 ni_timer_to_ns(dev, ni_ns_to_timer(dev,
2211 cmd->scan_begin_arg,
2213 if (tmp != cmd->scan_begin_arg)
2216 if (cmd->convert_src == TRIG_TIMER) {
2217 if (!devpriv->is_611x && !devpriv->is_6143) {
2218 tmp = cmd->convert_arg;
2220 ni_timer_to_ns(dev, ni_ns_to_timer(dev,
2223 if (tmp != cmd->convert_arg)
2225 if (cmd->scan_begin_src == TRIG_TIMER &&
2226 cmd->scan_begin_arg <
2227 cmd->convert_arg * cmd->scan_end_arg) {
2228 cmd->scan_begin_arg =
2229 cmd->convert_arg * cmd->scan_end_arg;
2241 static int ni_ai_inttrig(struct comedi_device *dev,
2242 struct comedi_subdevice *s,
2243 unsigned int trig_num)
2245 struct ni_private *devpriv = dev->private;
2246 struct comedi_cmd *cmd = &s->async->cmd;
2248 if (trig_num != cmd->start_arg)
2251 ni_stc_writew(dev, NISTC_AI_CMD2_START1_PULSE | devpriv->ai_cmd2,
2253 s->async->inttrig = NULL;
2258 static int ni_ai_cmd(struct comedi_device *dev, struct comedi_subdevice *s)
2260 struct ni_private *devpriv = dev->private;
2261 const struct comedi_cmd *cmd = &s->async->cmd;
2263 int mode1 = 0; /* mode1 is needed for both stop and convert */
2265 int start_stop_select = 0;
2266 unsigned int stop_count;
2267 int interrupt_a_enable = 0;
2270 if (dev->irq == 0) {
2271 dev_err(dev->class_dev, "cannot run command without an irq\n");
2274 ni_clear_ai_fifo(dev);
2276 ni_load_channelgain_list(dev, s, cmd->chanlist_len, cmd->chanlist);
2278 /* start configuration */
2279 ni_stc_writew(dev, NISTC_RESET_AI_CFG_START, NISTC_RESET_REG);
2281 /* disable analog triggering for now, since it
2282 * interferes with the use of pfi0 */
2283 devpriv->an_trig_etc_reg &= ~NISTC_ATRIG_ETC_ENA;
2284 ni_stc_writew(dev, devpriv->an_trig_etc_reg, NISTC_ATRIG_ETC_REG);
2286 ai_trig = NISTC_AI_TRIG_START2_SEL(0) | NISTC_AI_TRIG_START1_SYNC;
2287 switch (cmd->start_src) {
2290 ai_trig |= NISTC_AI_TRIG_START1_EDGE |
2291 NISTC_AI_TRIG_START1_SEL(0);
2294 ai_trig |= NISTC_AI_TRIG_START1_SEL(CR_CHAN(cmd->start_arg) +
2297 if (cmd->start_arg & CR_INVERT)
2298 ai_trig |= NISTC_AI_TRIG_START1_POLARITY;
2299 if (cmd->start_arg & CR_EDGE)
2300 ai_trig |= NISTC_AI_TRIG_START1_EDGE;
2303 ni_stc_writew(dev, ai_trig, NISTC_AI_TRIG_SEL_REG);
2305 mode2 &= ~NISTC_AI_MODE2_PRE_TRIGGER;
2306 mode2 &= ~NISTC_AI_MODE2_SC_INIT_LOAD_SRC;
2307 mode2 &= ~NISTC_AI_MODE2_SC_RELOAD_MODE;
2308 ni_stc_writew(dev, mode2, NISTC_AI_MODE2_REG);
2310 if (cmd->chanlist_len == 1 || devpriv->is_611x || devpriv->is_6143) {
2312 start_stop_select |= NISTC_AI_STOP_POLARITY |
2313 NISTC_AI_STOP_SEL(31) |
2316 /* ai configuration memory */
2317 start_stop_select |= NISTC_AI_STOP_SEL(19);
2319 ni_stc_writew(dev, start_stop_select, NISTC_AI_START_STOP_REG);
2321 devpriv->ai_cmd2 = 0;
2322 switch (cmd->stop_src) {
2324 stop_count = cmd->stop_arg - 1;
2326 if (devpriv->is_611x) {
2327 /* have to take 3 stage adc pipeline into account */
2328 stop_count += num_adc_stages_611x;
2330 /* stage number of scans */
2331 ni_stc_writel(dev, stop_count, NISTC_AI_SC_LOADA_REG);
2333 mode1 |= NISTC_AI_MODE1_START_STOP |
2334 NISTC_AI_MODE1_RSVD |
2335 NISTC_AI_MODE1_TRIGGER_ONCE;
2336 ni_stc_writew(dev, mode1, NISTC_AI_MODE1_REG);
2337 /* load SC (Scan Count) */
2338 ni_stc_writew(dev, NISTC_AI_CMD1_SC_LOAD, NISTC_AI_CMD1_REG);
2340 if (stop_count == 0) {
2341 devpriv->ai_cmd2 |= NISTC_AI_CMD2_END_ON_EOS;
2342 interrupt_a_enable |= NISTC_INTA_ENA_AI_STOP;
2343 /* this is required to get the last sample for chanlist_len > 1, not sure why */
2344 if (cmd->chanlist_len > 1)
2345 start_stop_select |= NISTC_AI_STOP_POLARITY |
2350 /* stage number of scans */
2351 ni_stc_writel(dev, 0, NISTC_AI_SC_LOADA_REG);
2353 mode1 |= NISTC_AI_MODE1_START_STOP |
2354 NISTC_AI_MODE1_RSVD |
2355 NISTC_AI_MODE1_CONTINUOUS;
2356 ni_stc_writew(dev, mode1, NISTC_AI_MODE1_REG);
2358 /* load SC (Scan Count) */
2359 ni_stc_writew(dev, NISTC_AI_CMD1_SC_LOAD, NISTC_AI_CMD1_REG);
2363 switch (cmd->scan_begin_src) {
2366 * stop bits for non 611x boards
2367 * NISTC_AI_MODE3_SI_TRIG_DELAY=0
2368 * NISTC_AI_MODE2_PRE_TRIGGER=0
2369 * NISTC_AI_START_STOP_REG:
2370 * NISTC_AI_START_POLARITY=0 (?) rising edge
2371 * NISTC_AI_START_EDGE=1 edge triggered
2372 * NISTC_AI_START_SYNC=1 (?)
2373 * NISTC_AI_START_SEL=0 SI_TC
2374 * NISTC_AI_STOP_POLARITY=0 rising edge
2375 * NISTC_AI_STOP_EDGE=0 level
2376 * NISTC_AI_STOP_SYNC=1
2377 * NISTC_AI_STOP_SEL=19 external pin (configuration mem)
2379 start_stop_select |= NISTC_AI_START_EDGE | NISTC_AI_START_SYNC;
2380 ni_stc_writew(dev, start_stop_select, NISTC_AI_START_STOP_REG);
2382 mode2 &= ~NISTC_AI_MODE2_SI_INIT_LOAD_SRC; /* A */
2383 mode2 |= NISTC_AI_MODE2_SI_RELOAD_MODE(0);
2384 /* mode2 |= NISTC_AI_MODE2_SC_RELOAD_MODE; */
2385 ni_stc_writew(dev, mode2, NISTC_AI_MODE2_REG);
2388 timer = ni_ns_to_timer(dev, cmd->scan_begin_arg,
2389 CMDF_ROUND_NEAREST);
2390 ni_stc_writel(dev, timer, NISTC_AI_SI_LOADA_REG);
2391 ni_stc_writew(dev, NISTC_AI_CMD1_SI_LOAD, NISTC_AI_CMD1_REG);
2394 if (cmd->scan_begin_arg & CR_EDGE)
2395 start_stop_select |= NISTC_AI_START_EDGE;
2396 if (cmd->scan_begin_arg & CR_INVERT) /* falling edge */
2397 start_stop_select |= NISTC_AI_START_POLARITY;
2398 if (cmd->scan_begin_src != cmd->convert_src ||
2399 (cmd->scan_begin_arg & ~CR_EDGE) !=
2400 (cmd->convert_arg & ~CR_EDGE))
2401 start_stop_select |= NISTC_AI_START_SYNC;
2402 start_stop_select |=
2403 NISTC_AI_START_SEL(1 + CR_CHAN(cmd->scan_begin_arg));
2404 ni_stc_writew(dev, start_stop_select, NISTC_AI_START_STOP_REG);
2408 switch (cmd->convert_src) {
2411 if (cmd->convert_arg == 0 || cmd->convert_src == TRIG_NOW)
2414 timer = ni_ns_to_timer(dev, cmd->convert_arg,
2415 CMDF_ROUND_NEAREST);
2416 /* 0,0 does not work */
2417 ni_stc_writew(dev, 1, NISTC_AI_SI2_LOADA_REG);
2418 ni_stc_writew(dev, timer, NISTC_AI_SI2_LOADB_REG);
2420 mode2 &= ~NISTC_AI_MODE2_SI2_INIT_LOAD_SRC; /* A */
2421 mode2 |= NISTC_AI_MODE2_SI2_RELOAD_MODE; /* alternate */
2422 ni_stc_writew(dev, mode2, NISTC_AI_MODE2_REG);
2424 ni_stc_writew(dev, NISTC_AI_CMD1_SI2_LOAD, NISTC_AI_CMD1_REG);
2426 mode2 |= NISTC_AI_MODE2_SI2_INIT_LOAD_SRC; /* B */
2427 mode2 |= NISTC_AI_MODE2_SI2_RELOAD_MODE; /* alternate */
2428 ni_stc_writew(dev, mode2, NISTC_AI_MODE2_REG);
2431 mode1 |= NISTC_AI_MODE1_CONVERT_SRC(1 + cmd->convert_arg);
2432 if ((cmd->convert_arg & CR_INVERT) == 0)
2433 mode1 |= NISTC_AI_MODE1_CONVERT_POLARITY;
2434 ni_stc_writew(dev, mode1, NISTC_AI_MODE1_REG);
2436 mode2 |= NISTC_AI_MODE2_SC_GATE_ENA |
2437 NISTC_AI_MODE2_START_STOP_GATE_ENA;
2438 ni_stc_writew(dev, mode2, NISTC_AI_MODE2_REG);
2444 /* interrupt on FIFO, errors, SC_TC */
2445 interrupt_a_enable |= NISTC_INTA_ENA_AI_ERR |
2446 NISTC_INTA_ENA_AI_SC_TC;
2449 interrupt_a_enable |= NISTC_INTA_ENA_AI_FIFO;
2452 if ((cmd->flags & CMDF_WAKE_EOS) ||
2453 (devpriv->ai_cmd2 & NISTC_AI_CMD2_END_ON_EOS)) {
2454 /* wake on end-of-scan */
2455 devpriv->aimode = AIMODE_SCAN;
2457 devpriv->aimode = AIMODE_HALF_FULL;
2460 switch (devpriv->aimode) {
2461 case AIMODE_HALF_FULL:
2462 /*generate FIFO interrupts and DMA requests on half-full */
2464 ni_stc_writew(dev, NISTC_AI_MODE3_FIFO_MODE_HF_E,
2465 NISTC_AI_MODE3_REG);
2467 ni_stc_writew(dev, NISTC_AI_MODE3_FIFO_MODE_HF,
2468 NISTC_AI_MODE3_REG);
2472 /*generate FIFO interrupts on non-empty */
2473 ni_stc_writew(dev, NISTC_AI_MODE3_FIFO_MODE_NE,
2474 NISTC_AI_MODE3_REG);
2478 ni_stc_writew(dev, NISTC_AI_MODE3_FIFO_MODE_NE,
2479 NISTC_AI_MODE3_REG);
2481 ni_stc_writew(dev, NISTC_AI_MODE3_FIFO_MODE_HF,
2482 NISTC_AI_MODE3_REG);
2484 interrupt_a_enable |= NISTC_INTA_ENA_AI_STOP;
2490 /* clear interrupts */
2491 ni_stc_writew(dev, NISTC_INTA_ACK_AI_ALL, NISTC_INTA_ACK_REG);
2493 ni_set_bits(dev, NISTC_INTA_ENA_REG, interrupt_a_enable, 1);
2495 /* interrupt on nothing */
2496 ni_set_bits(dev, NISTC_INTA_ENA_REG, ~0, 0);
2498 /* XXX start polling if necessary */
2501 /* end configuration */
2502 ni_stc_writew(dev, NISTC_RESET_AI_CFG_END, NISTC_RESET_REG);
2504 switch (cmd->scan_begin_src) {
2506 ni_stc_writew(dev, NISTC_AI_CMD1_SI2_ARM |
2507 NISTC_AI_CMD1_SI_ARM |
2508 NISTC_AI_CMD1_DIV_ARM |
2509 NISTC_AI_CMD1_SC_ARM,
2513 ni_stc_writew(dev, NISTC_AI_CMD1_SI2_ARM |
2514 NISTC_AI_CMD1_SI_ARM | /* XXX ? */
2515 NISTC_AI_CMD1_DIV_ARM |
2516 NISTC_AI_CMD1_SC_ARM,
2523 int retval = ni_ai_setup_MITE_dma(dev);
2530 if (cmd->start_src == TRIG_NOW) {
2531 ni_stc_writew(dev, NISTC_AI_CMD2_START1_PULSE |
2534 s->async->inttrig = NULL;
2535 } else if (cmd->start_src == TRIG_EXT) {
2536 s->async->inttrig = NULL;
2537 } else { /* TRIG_INT */
2538 s->async->inttrig = ni_ai_inttrig;
2544 static int ni_ai_insn_config(struct comedi_device *dev,
2545 struct comedi_subdevice *s,
2546 struct comedi_insn *insn, unsigned int *data)
2548 struct ni_private *devpriv = dev->private;
2554 case INSN_CONFIG_ALT_SOURCE:
2555 if (devpriv->is_m_series) {
2556 if (data[1] & ~NI_M_CFG_BYPASS_AI_CAL_MASK)
2558 devpriv->ai_calib_source = data[1];
2559 } else if (devpriv->is_6143) {
2560 unsigned int calib_source;
2562 calib_source = data[1] & 0xf;
2564 devpriv->ai_calib_source = calib_source;
2565 ni_writew(dev, calib_source, NI6143_CALIB_CHAN_REG);
2567 unsigned int calib_source;
2568 unsigned int calib_source_adjust;
2570 calib_source = data[1] & 0xf;
2571 calib_source_adjust = (data[1] >> 4) & 0xff;
2573 if (calib_source >= 8)
2575 devpriv->ai_calib_source = calib_source;
2576 if (devpriv->is_611x) {
2577 ni_writeb(dev, calib_source_adjust,
2578 NI611X_CAL_GAIN_SEL_REG);
2589 static void ni_ao_munge(struct comedi_device *dev, struct comedi_subdevice *s,
2590 void *data, unsigned int num_bytes,
2591 unsigned int chan_index)
2593 struct comedi_cmd *cmd = &s->async->cmd;
2594 unsigned int nsamples = comedi_bytes_to_samples(s, num_bytes);
2595 unsigned short *array = data;
2598 __le16 buf, *barray = data;
2601 for (i = 0; i < nsamples; i++) {
2602 unsigned int range = CR_RANGE(cmd->chanlist[chan_index]);
2603 unsigned short val = array[i];
2606 * Munge data from unsigned to two's complement for
2609 if (comedi_range_is_bipolar(s, range))
2610 val = comedi_offset_munge(s, val);
2612 buf = cpu_to_le16(val);
2618 chan_index %= cmd->chanlist_len;
2622 static int ni_m_series_ao_config_chanlist(struct comedi_device *dev,
2623 struct comedi_subdevice *s,
2624 unsigned int chanspec[],
2625 unsigned int n_chans, int timed)
2627 struct ni_private *devpriv = dev->private;
2635 for (i = 0; i < s->n_chan; ++i) {
2636 devpriv->ao_conf[i] &= ~NI_M_AO_CFG_BANK_UPDATE_TIMED;
2637 ni_writeb(dev, devpriv->ao_conf[i],
2638 NI_M_AO_CFG_BANK_REG(i));
2639 ni_writeb(dev, 0xf, NI_M_AO_WAVEFORM_ORDER_REG(i));
2642 for (i = 0; i < n_chans; i++) {
2643 const struct comedi_krange *krange;
2645 chan = CR_CHAN(chanspec[i]);
2646 range = CR_RANGE(chanspec[i]);
2647 krange = s->range_table->range + range;
2650 switch (krange->max - krange->min) {
2652 conf |= NI_M_AO_CFG_BANK_REF_INT_10V;
2653 ni_writeb(dev, 0, NI_M_AO_REF_ATTENUATION_REG(chan));
2656 conf |= NI_M_AO_CFG_BANK_REF_INT_5V;
2657 ni_writeb(dev, 0, NI_M_AO_REF_ATTENUATION_REG(chan));
2660 conf |= NI_M_AO_CFG_BANK_REF_INT_10V;
2661 ni_writeb(dev, NI_M_AO_REF_ATTENUATION_X5,
2662 NI_M_AO_REF_ATTENUATION_REG(chan));
2665 conf |= NI_M_AO_CFG_BANK_REF_INT_5V;
2666 ni_writeb(dev, NI_M_AO_REF_ATTENUATION_X5,
2667 NI_M_AO_REF_ATTENUATION_REG(chan));
2670 dev_err(dev->class_dev,
2671 "bug! unhandled ao reference voltage\n");
2674 switch (krange->max + krange->min) {
2676 conf |= NI_M_AO_CFG_BANK_OFFSET_0V;
2679 conf |= NI_M_AO_CFG_BANK_OFFSET_5V;
2682 dev_err(dev->class_dev,
2683 "bug! unhandled ao offset voltage\n");
2687 conf |= NI_M_AO_CFG_BANK_UPDATE_TIMED;
2688 ni_writeb(dev, conf, NI_M_AO_CFG_BANK_REG(chan));
2689 devpriv->ao_conf[chan] = conf;
2690 ni_writeb(dev, i, NI_M_AO_WAVEFORM_ORDER_REG(chan));
2695 static int ni_old_ao_config_chanlist(struct comedi_device *dev,
2696 struct comedi_subdevice *s,
2697 unsigned int chanspec[],
2698 unsigned int n_chans)
2700 struct ni_private *devpriv = dev->private;
2707 for (i = 0; i < n_chans; i++) {
2708 chan = CR_CHAN(chanspec[i]);
2709 range = CR_RANGE(chanspec[i]);
2710 conf = NI_E_AO_DACSEL(chan);
2712 if (comedi_range_is_bipolar(s, range)) {
2713 conf |= NI_E_AO_CFG_BIP;
2714 invert = (s->maxdata + 1) >> 1;
2718 if (comedi_range_is_external(s, range))
2719 conf |= NI_E_AO_EXT_REF;
2721 /* not all boards can deglitch, but this shouldn't hurt */
2722 if (chanspec[i] & CR_DEGLITCH)
2723 conf |= NI_E_AO_DEGLITCH;
2725 /* analog reference */
2726 /* AREF_OTHER connects AO ground to AI ground, i think */
2727 if (CR_AREF(chanspec[i]) == AREF_OTHER)
2728 conf |= NI_E_AO_GROUND_REF;
2730 ni_writew(dev, conf, NI_E_AO_CFG_REG);
2731 devpriv->ao_conf[chan] = conf;
2736 static int ni_ao_config_chanlist(struct comedi_device *dev,
2737 struct comedi_subdevice *s,
2738 unsigned int chanspec[], unsigned int n_chans,
2741 struct ni_private *devpriv = dev->private;
2743 if (devpriv->is_m_series)
2744 return ni_m_series_ao_config_chanlist(dev, s, chanspec, n_chans,
2747 return ni_old_ao_config_chanlist(dev, s, chanspec, n_chans);
2750 static int ni_ao_insn_write(struct comedi_device *dev,
2751 struct comedi_subdevice *s,
2752 struct comedi_insn *insn,
2755 struct ni_private *devpriv = dev->private;
2756 unsigned int chan = CR_CHAN(insn->chanspec);
2757 unsigned int range = CR_RANGE(insn->chanspec);
2761 if (devpriv->is_6xxx) {
2762 ni_ao_win_outw(dev, 1 << chan, NI671X_AO_IMMEDIATE_REG);
2764 reg = NI671X_DAC_DIRECT_DATA_REG(chan);
2765 } else if (devpriv->is_m_series) {
2766 reg = NI_M_DAC_DIRECT_DATA_REG(chan);
2768 reg = NI_E_DAC_DIRECT_DATA_REG(chan);
2771 ni_ao_config_chanlist(dev, s, &insn->chanspec, 1, 0);
2773 for (i = 0; i < insn->n; i++) {
2774 unsigned int val = data[i];
2776 s->readback[chan] = val;
2778 if (devpriv->is_6xxx) {
2780 * 6xxx boards have bipolar outputs, munge the
2781 * unsigned comedi values to 2's complement
2783 val = comedi_offset_munge(s, val);
2785 ni_ao_win_outw(dev, val, reg);
2786 } else if (devpriv->is_m_series) {
2788 * M-series boards use offset binary values for
2789 * bipolar and uinpolar outputs
2791 ni_writew(dev, val, reg);
2794 * Non-M series boards need two's complement values
2795 * for bipolar ranges.
2797 if (comedi_range_is_bipolar(s, range))
2798 val = comedi_offset_munge(s, val);
2800 ni_writew(dev, val, reg);
2807 static int ni_ao_insn_config(struct comedi_device *dev,
2808 struct comedi_subdevice *s,
2809 struct comedi_insn *insn, unsigned int *data)
2811 const struct ni_board_struct *board = dev->board_ptr;
2812 struct ni_private *devpriv = dev->private;
2813 unsigned int nbytes;
2816 case INSN_CONFIG_GET_HARDWARE_BUFFER_SIZE:
2819 nbytes = comedi_samples_to_bytes(s,
2820 board->ao_fifo_depth);
2821 data[2] = 1 + nbytes;
2823 data[2] += devpriv->mite->fifo_size;
2839 static int ni_ao_inttrig(struct comedi_device *dev,
2840 struct comedi_subdevice *s,
2841 unsigned int trig_num)
2843 struct ni_private *devpriv = dev->private;
2844 struct comedi_cmd *cmd = &s->async->cmd;
2846 int interrupt_b_bits;
2848 static const int timeout = 1000;
2850 if (trig_num != cmd->start_arg)
2853 /* Null trig at beginning prevent ao start trigger from executing more than
2854 once per command (and doing things like trying to allocate the ao dma channel
2856 s->async->inttrig = NULL;
2858 ni_set_bits(dev, NISTC_INTB_ENA_REG,
2859 NISTC_INTB_ENA_AO_FIFO | NISTC_INTB_ENA_AO_ERR, 0);
2860 interrupt_b_bits = NISTC_INTB_ENA_AO_ERR;
2862 ni_stc_writew(dev, 1, NISTC_DAC_FIFO_CLR_REG);
2863 if (devpriv->is_6xxx)
2864 ni_ao_win_outl(dev, 0x6, NI611X_AO_FIFO_OFFSET_LOAD_REG);
2865 ret = ni_ao_setup_MITE_dma(dev);
2868 ret = ni_ao_wait_for_dma_load(dev);
2872 ret = ni_ao_prep_fifo(dev, s);
2876 interrupt_b_bits |= NISTC_INTB_ENA_AO_FIFO;
2879 ni_stc_writew(dev, devpriv->ao_mode3 | NISTC_AO_MODE3_NOT_AN_UPDATE,
2880 NISTC_AO_MODE3_REG);
2881 ni_stc_writew(dev, devpriv->ao_mode3, NISTC_AO_MODE3_REG);
2882 /* wait for DACs to be loaded */
2883 for (i = 0; i < timeout; i++) {
2885 if ((ni_stc_readw(dev, NISTC_STATUS2_REG) &
2886 NISTC_STATUS2_AO_TMRDACWRS_IN_PROGRESS) == 0)
2890 dev_err(dev->class_dev,
2891 "timed out waiting for AO_TMRDACWRs_In_Progress_St to clear\n");
2895 * stc manual says we are need to clear error interrupt after
2896 * AO_TMRDACWRs_In_Progress_St clears
2898 ni_stc_writew(dev, NISTC_INTB_ACK_AO_ERR, NISTC_INTB_ACK_REG);
2900 ni_set_bits(dev, NISTC_INTB_ENA_REG, interrupt_b_bits, 1);
2902 ni_stc_writew(dev, NISTC_AO_CMD1_UI_ARM |
2903 NISTC_AO_CMD1_UC_ARM |
2904 NISTC_AO_CMD1_BC_ARM |
2905 NISTC_AO_CMD1_DAC1_UPDATE_MODE |
2906 NISTC_AO_CMD1_DAC0_UPDATE_MODE |
2910 ni_stc_writew(dev, NISTC_AO_CMD2_START1_PULSE | devpriv->ao_cmd2,
2916 static int ni_ao_cmd(struct comedi_device *dev, struct comedi_subdevice *s)
2918 const struct ni_board_struct *board = dev->board_ptr;
2919 struct ni_private *devpriv = dev->private;
2920 const struct comedi_cmd *cmd = &s->async->cmd;
2926 if (dev->irq == 0) {
2927 dev_err(dev->class_dev, "cannot run command without an irq\n");
2931 ni_stc_writew(dev, NISTC_RESET_AO_CFG_START, NISTC_RESET_REG);
2933 ni_stc_writew(dev, NISTC_AO_CMD1_DISARM, NISTC_AO_CMD1_REG);
2935 if (devpriv->is_6xxx) {
2936 ni_ao_win_outw(dev, NI611X_AO_MISC_CLEAR_WG,
2937 NI611X_AO_MISC_REG);
2940 for (i = 0; i < cmd->chanlist_len; i++) {
2943 chan = CR_CHAN(cmd->chanlist[i]);
2945 ni_ao_win_outw(dev, chan, NI611X_AO_WAVEFORM_GEN_REG);
2947 ni_ao_win_outw(dev, bits, NI611X_AO_TIMED_REG);
2950 ni_ao_config_chanlist(dev, s, cmd->chanlist, cmd->chanlist_len, 1);
2952 if (cmd->stop_src == TRIG_NONE) {
2953 devpriv->ao_mode1 |= NISTC_AO_MODE1_CONTINUOUS;
2954 devpriv->ao_mode1 &= ~NISTC_AO_MODE1_TRIGGER_ONCE;
2956 devpriv->ao_mode1 &= ~NISTC_AO_MODE1_CONTINUOUS;
2957 devpriv->ao_mode1 |= NISTC_AO_MODE1_TRIGGER_ONCE;
2959 ni_stc_writew(dev, devpriv->ao_mode1, NISTC_AO_MODE1_REG);
2961 val = devpriv->ao_trigger_select;
2962 switch (cmd->start_src) {
2965 val &= ~(NISTC_AO_TRIG_START1_POLARITY |
2966 NISTC_AO_TRIG_START1_SEL_MASK);
2967 val |= NISTC_AO_TRIG_START1_EDGE |
2968 NISTC_AO_TRIG_START1_SYNC;
2971 val = NISTC_AO_TRIG_START1_SEL(CR_CHAN(cmd->start_arg) + 1);
2972 if (cmd->start_arg & CR_INVERT) {
2973 /* 0=active high, 1=active low. see daq-stc 3-24 (p186) */
2974 val |= NISTC_AO_TRIG_START1_POLARITY;
2976 if (cmd->start_arg & CR_EDGE) {
2977 /* 0=edge detection disabled, 1=enabled */
2978 val |= NISTC_AO_TRIG_START1_EDGE;
2980 ni_stc_writew(dev, devpriv->ao_trigger_select,
2981 NISTC_AO_TRIG_SEL_REG);
2987 devpriv->ao_trigger_select = val;
2988 ni_stc_writew(dev, devpriv->ao_trigger_select, NISTC_AO_TRIG_SEL_REG);
2990 devpriv->ao_mode3 &= ~NISTC_AO_MODE3_TRIG_LEN;
2991 ni_stc_writew(dev, devpriv->ao_mode3, NISTC_AO_MODE3_REG);
2993 ni_stc_writew(dev, devpriv->ao_mode1, NISTC_AO_MODE1_REG);
2994 devpriv->ao_mode2 &= ~NISTC_AO_MODE2_BC_INIT_LOAD_SRC;
2995 ni_stc_writew(dev, devpriv->ao_mode2, NISTC_AO_MODE2_REG);
2996 if (cmd->stop_src == TRIG_NONE)
2997 ni_stc_writel(dev, 0xffffff, NISTC_AO_BC_LOADA_REG);
2999 ni_stc_writel(dev, 0, NISTC_AO_BC_LOADA_REG);
3000 ni_stc_writew(dev, NISTC_AO_CMD1_BC_LOAD, NISTC_AO_CMD1_REG);
3001 devpriv->ao_mode2 &= ~NISTC_AO_MODE2_UC_INIT_LOAD_SRC;
3002 ni_stc_writew(dev, devpriv->ao_mode2, NISTC_AO_MODE2_REG);
3003 switch (cmd->stop_src) {
3005 if (devpriv->is_m_series) {
3006 /* this is how the NI example code does it for m-series boards, verified correct with 6259 */
3007 ni_stc_writel(dev, cmd->stop_arg - 1,
3008 NISTC_AO_UC_LOADA_REG);
3009 ni_stc_writew(dev, NISTC_AO_CMD1_UC_LOAD,
3012 ni_stc_writel(dev, cmd->stop_arg,
3013 NISTC_AO_UC_LOADA_REG);
3014 ni_stc_writew(dev, NISTC_AO_CMD1_UC_LOAD,
3016 ni_stc_writel(dev, cmd->stop_arg - 1,
3017 NISTC_AO_UC_LOADA_REG);
3021 ni_stc_writel(dev, 0xffffff, NISTC_AO_UC_LOADA_REG);
3022 ni_stc_writew(dev, NISTC_AO_CMD1_UC_LOAD, NISTC_AO_CMD1_REG);
3023 ni_stc_writel(dev, 0xffffff, NISTC_AO_UC_LOADA_REG);
3026 ni_stc_writel(dev, 0, NISTC_AO_UC_LOADA_REG);
3027 ni_stc_writew(dev, NISTC_AO_CMD1_UC_LOAD, NISTC_AO_CMD1_REG);
3028 ni_stc_writel(dev, cmd->stop_arg, NISTC_AO_UC_LOADA_REG);
3031 devpriv->ao_mode1 &= ~(NISTC_AO_MODE1_UPDATE_SRC_MASK |
3032 NISTC_AO_MODE1_UI_SRC_MASK |
3033 NISTC_AO_MODE1_UPDATE_SRC_POLARITY |
3034 NISTC_AO_MODE1_UI_SRC_POLARITY);
3035 switch (cmd->scan_begin_src) {
3037 devpriv->ao_cmd2 &= ~NISTC_AO_CMD2_BC_GATE_ENA;
3039 ni_ns_to_timer(dev, cmd->scan_begin_arg,
3040 CMDF_ROUND_NEAREST);
3041 ni_stc_writel(dev, 1, NISTC_AO_UI_LOADA_REG);
3042 ni_stc_writew(dev, NISTC_AO_CMD1_UI_LOAD, NISTC_AO_CMD1_REG);
3043 ni_stc_writel(dev, trigvar, NISTC_AO_UI_LOADA_REG);
3046 devpriv->ao_mode1 |=
3047 NISTC_AO_MODE1_UPDATE_SRC(cmd->scan_begin_arg);
3048 if (cmd->scan_begin_arg & CR_INVERT)
3049 devpriv->ao_mode1 |= NISTC_AO_MODE1_UPDATE_SRC_POLARITY;
3050 devpriv->ao_cmd2 |= NISTC_AO_CMD2_BC_GATE_ENA;
3056 ni_stc_writew(dev, devpriv->ao_cmd2, NISTC_AO_CMD2_REG);
3057 ni_stc_writew(dev, devpriv->ao_mode1, NISTC_AO_MODE1_REG);
3058 devpriv->ao_mode2 &= ~(NISTC_AO_MODE2_UI_RELOAD_MODE(3) |
3059 NISTC_AO_MODE2_UI_INIT_LOAD_SRC);
3060 ni_stc_writew(dev, devpriv->ao_mode2, NISTC_AO_MODE2_REG);
3062 if (cmd->scan_end_arg > 1) {
3063 devpriv->ao_mode1 |= NISTC_AO_MODE1_MULTI_CHAN;
3065 NISTC_AO_OUT_CTRL_CHANS(cmd->scan_end_arg - 1) |
3066 NISTC_AO_OUT_CTRL_UPDATE_SEL_HIGHZ,
3067 NISTC_AO_OUT_CTRL_REG);
3071 devpriv->ao_mode1 &= ~NISTC_AO_MODE1_MULTI_CHAN;
3072 bits = NISTC_AO_OUT_CTRL_UPDATE_SEL_HIGHZ;
3073 if (devpriv->is_m_series || devpriv->is_6xxx) {
3074 bits |= NISTC_AO_OUT_CTRL_CHANS(0);
3077 NISTC_AO_OUT_CTRL_CHANS(CR_CHAN(cmd->chanlist[0]));
3079 ni_stc_writew(dev, bits, NISTC_AO_OUT_CTRL_REG);
3081 ni_stc_writew(dev, devpriv->ao_mode1, NISTC_AO_MODE1_REG);
3083 ni_stc_writew(dev, NISTC_AO_CMD1_DAC1_UPDATE_MODE |
3084 NISTC_AO_CMD1_DAC0_UPDATE_MODE,
3087 devpriv->ao_mode3 |= NISTC_AO_MODE3_STOP_ON_OVERRUN_ERR;
3088 ni_stc_writew(dev, devpriv->ao_mode3, NISTC_AO_MODE3_REG);
3090 devpriv->ao_mode2 &= ~NISTC_AO_MODE2_FIFO_MODE_MASK;
3092 devpriv->ao_mode2 |= NISTC_AO_MODE2_FIFO_MODE_HF_F;
3094 devpriv->ao_mode2 |= NISTC_AO_MODE2_FIFO_MODE_HF;
3096 devpriv->ao_mode2 &= ~NISTC_AO_MODE2_FIFO_REXMIT_ENA;
3097 ni_stc_writew(dev, devpriv->ao_mode2, NISTC_AO_MODE2_REG);
3099 bits = NISTC_AO_PERSONAL_BC_SRC_SEL |
3100 NISTC_AO_PERSONAL_UPDATE_PW |
3101 NISTC_AO_PERSONAL_TMRDACWR_PW;
3102 if (board->ao_fifo_depth)
3103 bits |= NISTC_AO_PERSONAL_FIFO_ENA;
3105 bits |= NISTC_AO_PERSONAL_DMA_PIO_CTRL;
3108 * F Hess: windows driver does not set NISTC_AO_PERSONAL_NUM_DAC bit
3109 * for 6281, verified with bus analyzer.
3111 if (devpriv->is_m_series)
3112 bits |= NISTC_AO_PERSONAL_NUM_DAC;
3114 ni_stc_writew(dev, bits, NISTC_AO_PERSONAL_REG);
3115 /* enable sending of ao dma requests */
3116 ni_stc_writew(dev, NISTC_AO_START_AOFREQ_ENA, NISTC_AO_START_SEL_REG);
3118 ni_stc_writew(dev, NISTC_RESET_AO_CFG_END, NISTC_RESET_REG);
3120 if (cmd->stop_src == TRIG_COUNT) {
3121 ni_stc_writew(dev, NISTC_INTB_ACK_AO_BC_TC,
3122 NISTC_INTB_ACK_REG);
3123 ni_set_bits(dev, NISTC_INTB_ENA_REG,
3124 NISTC_INTB_ENA_AO_BC_TC, 1);
3127 s->async->inttrig = ni_ao_inttrig;
3132 static int ni_ao_cmdtest(struct comedi_device *dev, struct comedi_subdevice *s,
3133 struct comedi_cmd *cmd)
3135 const struct ni_board_struct *board = dev->board_ptr;
3136 struct ni_private *devpriv = dev->private;
3140 /* Step 1 : check if triggers are trivially valid */
3142 err |= comedi_check_trigger_src(&cmd->start_src, TRIG_INT | TRIG_EXT);
3143 err |= comedi_check_trigger_src(&cmd->scan_begin_src,
3144 TRIG_TIMER | TRIG_EXT);
3145 err |= comedi_check_trigger_src(&cmd->convert_src, TRIG_NOW);
3146 err |= comedi_check_trigger_src(&cmd->scan_end_src, TRIG_COUNT);
3147 err |= comedi_check_trigger_src(&cmd->stop_src, TRIG_COUNT | TRIG_NONE);
3152 /* Step 2a : make sure trigger sources are unique */
3154 err |= comedi_check_trigger_is_unique(cmd->start_src);
3155 err |= comedi_check_trigger_is_unique(cmd->scan_begin_src);
3156 err |= comedi_check_trigger_is_unique(cmd->stop_src);
3158 /* Step 2b : and mutually compatible */
3163 /* Step 3: check if arguments are trivially valid */
3165 switch (cmd->start_src) {
3167 err |= comedi_check_trigger_arg_is(&cmd->start_arg, 0);
3170 tmp = CR_CHAN(cmd->start_arg);
3174 tmp |= (cmd->start_arg & (CR_INVERT | CR_EDGE));
3175 err |= comedi_check_trigger_arg_is(&cmd->start_arg, tmp);
3179 if (cmd->scan_begin_src == TRIG_TIMER) {
3180 err |= comedi_check_trigger_arg_min(&cmd->scan_begin_arg,
3182 err |= comedi_check_trigger_arg_max(&cmd->scan_begin_arg,
3187 err |= comedi_check_trigger_arg_is(&cmd->convert_arg, 0);
3188 err |= comedi_check_trigger_arg_is(&cmd->scan_end_arg,
3191 if (cmd->stop_src == TRIG_COUNT)
3192 err |= comedi_check_trigger_arg_max(&cmd->stop_arg, 0x00ffffff);
3193 else /* TRIG_NONE */
3194 err |= comedi_check_trigger_arg_is(&cmd->stop_arg, 0);
3199 /* step 4: fix up any arguments */
3200 if (cmd->scan_begin_src == TRIG_TIMER) {
3201 tmp = cmd->scan_begin_arg;
3202 cmd->scan_begin_arg =
3203 ni_timer_to_ns(dev, ni_ns_to_timer(dev,
3204 cmd->scan_begin_arg,
3206 if (tmp != cmd->scan_begin_arg)
3215 static int ni_ao_reset(struct comedi_device *dev, struct comedi_subdevice *s)
3217 struct ni_private *devpriv = dev->private;
3219 ni_release_ao_mite_channel(dev);
3221 ni_stc_writew(dev, NISTC_RESET_AO_CFG_START, NISTC_RESET_REG);
3222 ni_stc_writew(dev, NISTC_AO_CMD1_DISARM, NISTC_AO_CMD1_REG);
3223 ni_set_bits(dev, NISTC_INTB_ENA_REG, ~0, 0);
3224 ni_stc_writew(dev, NISTC_AO_PERSONAL_BC_SRC_SEL, NISTC_AO_PERSONAL_REG);
3225 ni_stc_writew(dev, NISTC_INTB_ACK_AO_ALL, NISTC_INTB_ACK_REG);
3226 ni_stc_writew(dev, NISTC_AO_PERSONAL_BC_SRC_SEL |
3227 NISTC_AO_PERSONAL_UPDATE_PW |
3228 NISTC_AO_PERSONAL_TMRDACWR_PW,
3229 NISTC_AO_PERSONAL_REG);
3230 ni_stc_writew(dev, 0, NISTC_AO_OUT_CTRL_REG);
3231 ni_stc_writew(dev, 0, NISTC_AO_START_SEL_REG);
3232 devpriv->ao_cmd1 = 0;
3233 ni_stc_writew(dev, devpriv->ao_cmd1, NISTC_AO_CMD1_REG);
3234 devpriv->ao_cmd2 = 0;
3235 ni_stc_writew(dev, devpriv->ao_cmd2, NISTC_AO_CMD2_REG);
3236 devpriv->ao_mode1 = 0;
3237 ni_stc_writew(dev, devpriv->ao_mode1, NISTC_AO_MODE1_REG);
3238 devpriv->ao_mode2 = 0;
3239 ni_stc_writew(dev, devpriv->ao_mode2, NISTC_AO_MODE2_REG);
3240 if (devpriv->is_m_series)
3241 devpriv->ao_mode3 = NISTC_AO_MODE3_LAST_GATE_DISABLE;
3243 devpriv->ao_mode3 = 0;
3244 ni_stc_writew(dev, devpriv->ao_mode3, NISTC_AO_MODE3_REG);
3245 devpriv->ao_trigger_select = 0;
3246 ni_stc_writew(dev, devpriv->ao_trigger_select,
3247 NISTC_AO_TRIG_SEL_REG);
3248 if (devpriv->is_6xxx) {
3249 unsigned immediate_bits = 0;
3252 for (i = 0; i < s->n_chan; ++i)
3253 immediate_bits |= 1 << i;
3254 ni_ao_win_outw(dev, immediate_bits, NI671X_AO_IMMEDIATE_REG);
3255 ni_ao_win_outw(dev, NI611X_AO_MISC_CLEAR_WG,
3256 NI611X_AO_MISC_REG);
3258 ni_stc_writew(dev, NISTC_RESET_AO_CFG_END, NISTC_RESET_REG);
3265 static int ni_dio_insn_config(struct comedi_device *dev,
3266 struct comedi_subdevice *s,
3267 struct comedi_insn *insn,
3270 struct ni_private *devpriv = dev->private;
3273 ret = comedi_dio_insn_config(dev, s, insn, data, 0);
3277 devpriv->dio_control &= ~NISTC_DIO_CTRL_DIR_MASK;
3278 devpriv->dio_control |= NISTC_DIO_CTRL_DIR(s->io_bits);
3279 ni_stc_writew(dev, devpriv->dio_control, NISTC_DIO_CTRL_REG);
3284 static int ni_dio_insn_bits(struct comedi_device *dev,
3285 struct comedi_subdevice *s,
3286 struct comedi_insn *insn,
3289 struct ni_private *devpriv = dev->private;
3291 /* Make sure we're not using the serial part of the dio */
3292 if ((data[0] & (NISTC_DIO_SDIN | NISTC_DIO_SDOUT)) &&
3293 devpriv->serial_interval_ns)
3296 if (comedi_dio_update_state(s, data)) {
3297 devpriv->dio_output &= ~NISTC_DIO_OUT_PARALLEL_MASK;
3298 devpriv->dio_output |= NISTC_DIO_OUT_PARALLEL(s->state);
3299 ni_stc_writew(dev, devpriv->dio_output, NISTC_DIO_OUT_REG);
3302 data[1] = ni_stc_readw(dev, NISTC_DIO_IN_REG);
3307 static int ni_m_series_dio_insn_config(struct comedi_device *dev,
3308 struct comedi_subdevice *s,
3309 struct comedi_insn *insn,
3314 ret = comedi_dio_insn_config(dev, s, insn, data, 0);
3318 ni_writel(dev, s->io_bits, NI_M_DIO_DIR_REG);
3323 static int ni_m_series_dio_insn_bits(struct comedi_device *dev,
3324 struct comedi_subdevice *s,
3325 struct comedi_insn *insn,
3328 if (comedi_dio_update_state(s, data))
3329 ni_writel(dev, s->state, NI_M_DIO_REG);
3331 data[1] = ni_readl(dev, NI_M_DIO_REG);
3336 static int ni_cdio_check_chanlist(struct comedi_device *dev,
3337 struct comedi_subdevice *s,
3338 struct comedi_cmd *cmd)
3342 for (i = 0; i < cmd->chanlist_len; ++i) {
3343 unsigned int chan = CR_CHAN(cmd->chanlist[i]);
3352 static int ni_cdio_cmdtest(struct comedi_device *dev,
3353 struct comedi_subdevice *s, struct comedi_cmd *cmd)
3358 /* Step 1 : check if triggers are trivially valid */
3360 err |= comedi_check_trigger_src(&cmd->start_src, TRIG_INT);
3361 err |= comedi_check_trigger_src(&cmd->scan_begin_src, TRIG_EXT);
3362 err |= comedi_check_trigger_src(&cmd->convert_src, TRIG_NOW);
3363 err |= comedi_check_trigger_src(&cmd->scan_end_src, TRIG_COUNT);
3364 err |= comedi_check_trigger_src(&cmd->stop_src, TRIG_NONE);
3369 /* Step 2a : make sure trigger sources are unique */
3370 /* Step 2b : and mutually compatible */
3372 /* Step 3: check if arguments are trivially valid */
3374 err |= comedi_check_trigger_arg_is(&cmd->start_arg, 0);
3376 tmp = cmd->scan_begin_arg;
3377 tmp &= CR_PACK_FLAGS(NI_M_CDO_MODE_SAMPLE_SRC_MASK, 0, 0, CR_INVERT);
3378 if (tmp != cmd->scan_begin_arg)
3381 err |= comedi_check_trigger_arg_is(&cmd->convert_arg, 0);
3382 err |= comedi_check_trigger_arg_is(&cmd->scan_end_arg,
3384 err |= comedi_check_trigger_arg_is(&cmd->stop_arg, 0);
3389 /* Step 4: fix up any arguments */
3391 /* Step 5: check channel list if it exists */
3393 if (cmd->chanlist && cmd->chanlist_len > 0)
3394 err |= ni_cdio_check_chanlist(dev, s, cmd);
3402 static int ni_cdo_inttrig(struct comedi_device *dev,
3403 struct comedi_subdevice *s,
3404 unsigned int trig_num)
3406 struct comedi_cmd *cmd = &s->async->cmd;
3407 const unsigned timeout = 1000;
3411 struct ni_private *devpriv = dev->private;
3412 unsigned long flags;
3415 if (trig_num != cmd->start_arg)
3418 s->async->inttrig = NULL;
3420 /* read alloc the entire buffer */
3421 comedi_buf_read_alloc(s, s->async->prealloc_bufsz);
3424 spin_lock_irqsave(&devpriv->mite_channel_lock, flags);
3425 if (devpriv->cdo_mite_chan) {
3426 mite_prep_dma(devpriv->cdo_mite_chan, 32, 32);
3427 mite_dma_arm(devpriv->cdo_mite_chan);
3429 dev_err(dev->class_dev, "BUG: no cdo mite channel?\n");
3432 spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
3437 * XXX not sure what interrupt C group does
3438 * wait for dma to fill output fifo
3439 * ni_writeb(dev, NI_M_INTC_ENA, NI_M_INTC_ENA_REG);
3441 for (i = 0; i < timeout; ++i) {
3442 if (ni_readl(dev, NI_M_CDIO_STATUS_REG) &
3443 NI_M_CDIO_STATUS_CDO_FIFO_FULL)
3448 dev_err(dev->class_dev, "dma failed to fill cdo fifo!\n");
3452 ni_writel(dev, NI_M_CDO_CMD_ARM |
3453 NI_M_CDO_CMD_ERR_INT_ENA_SET |
3454 NI_M_CDO_CMD_F_E_INT_ENA_SET,
3459 static int ni_cdio_cmd(struct comedi_device *dev, struct comedi_subdevice *s)
3461 const struct comedi_cmd *cmd = &s->async->cmd;
3462 unsigned cdo_mode_bits;
3465 ni_writel(dev, NI_M_CDO_CMD_RESET, NI_M_CDIO_CMD_REG);
3466 cdo_mode_bits = NI_M_CDO_MODE_FIFO_MODE |
3467 NI_M_CDO_MODE_HALT_ON_ERROR |
3468 NI_M_CDO_MODE_SAMPLE_SRC(CR_CHAN(cmd->scan_begin_arg));
3469 if (cmd->scan_begin_arg & CR_INVERT)
3470 cdo_mode_bits |= NI_M_CDO_MODE_POLARITY;
3471 ni_writel(dev, cdo_mode_bits, NI_M_CDO_MODE_REG);
3473 ni_writel(dev, s->state, NI_M_CDO_FIFO_DATA_REG);
3474 ni_writel(dev, NI_M_CDO_CMD_SW_UPDATE, NI_M_CDIO_CMD_REG);
3475 ni_writel(dev, s->io_bits, NI_M_CDO_MASK_ENA_REG);
3477 dev_err(dev->class_dev,
3478 "attempted to run digital output command with no lines configured as outputs\n");
3481 retval = ni_request_cdo_mite_channel(dev);
3485 s->async->inttrig = ni_cdo_inttrig;
3490 static int ni_cdio_cancel(struct comedi_device *dev, struct comedi_subdevice *s)
3492 ni_writel(dev, NI_M_CDO_CMD_DISARM |
3493 NI_M_CDO_CMD_ERR_INT_ENA_CLR |
3494 NI_M_CDO_CMD_F_E_INT_ENA_CLR |
3495 NI_M_CDO_CMD_F_REQ_INT_ENA_CLR,
3498 * XXX not sure what interrupt C group does
3499 * ni_writeb(dev, 0, NI_M_INTC_ENA_REG);
3501 ni_writel(dev, 0, NI_M_CDO_MASK_ENA_REG);
3502 ni_release_cdo_mite_channel(dev);
3506 static void handle_cdio_interrupt(struct comedi_device *dev)
3508 struct ni_private *devpriv = dev->private;
3509 unsigned cdio_status;
3510 struct comedi_subdevice *s = &dev->subdevices[NI_DIO_SUBDEV];
3512 unsigned long flags;
3515 if (!devpriv->is_m_series)
3518 spin_lock_irqsave(&devpriv->mite_channel_lock, flags);
3519 if (devpriv->cdo_mite_chan) {
3520 unsigned cdo_mite_status =
3521 mite_get_status(devpriv->cdo_mite_chan);
3522 if (cdo_mite_status & CHSR_LINKC) {
3524 devpriv->mite->mite_io_addr +
3525 MITE_CHOR(devpriv->cdo_mite_chan->channel));
3527 mite_sync_output_dma(devpriv->cdo_mite_chan, s);
3529 spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
3532 cdio_status = ni_readl(dev, NI_M_CDIO_STATUS_REG);
3533 if (cdio_status & NI_M_CDIO_STATUS_CDO_ERROR) {
3534 /* XXX just guessing this is needed and does something useful */
3535 ni_writel(dev, NI_M_CDO_CMD_ERR_INT_CONFIRM,
3537 s->async->events |= COMEDI_CB_OVERFLOW;
3539 if (cdio_status & NI_M_CDIO_STATUS_CDO_FIFO_EMPTY) {
3540 ni_writel(dev, NI_M_CDO_CMD_F_E_INT_ENA_CLR,
3542 /* s->async->events |= COMEDI_CB_EOA; */
3544 comedi_handle_events(dev, s);
3547 static int ni_serial_hw_readwrite8(struct comedi_device *dev,
3548 struct comedi_subdevice *s,
3549 unsigned char data_out,
3550 unsigned char *data_in)
3552 struct ni_private *devpriv = dev->private;
3553 unsigned int status1;
3554 int err = 0, count = 20;
3556 devpriv->dio_output &= ~NISTC_DIO_OUT_SERIAL_MASK;
3557 devpriv->dio_output |= NISTC_DIO_OUT_SERIAL(data_out);
3558 ni_stc_writew(dev, devpriv->dio_output, NISTC_DIO_OUT_REG);
3560 status1 = ni_stc_readw(dev, NISTC_STATUS1_REG);
3561 if (status1 & NISTC_STATUS1_SERIO_IN_PROG) {
3566 devpriv->dio_control |= NISTC_DIO_CTRL_HW_SER_START;
3567 ni_stc_writew(dev, devpriv->dio_control, NISTC_DIO_CTRL_REG);
3568 devpriv->dio_control &= ~NISTC_DIO_CTRL_HW_SER_START;
3570 /* Wait until STC says we're done, but don't loop infinitely. */
3571 while ((status1 = ni_stc_readw(dev, NISTC_STATUS1_REG)) &
3572 NISTC_STATUS1_SERIO_IN_PROG) {
3573 /* Delay one bit per loop */
3574 udelay((devpriv->serial_interval_ns + 999) / 1000);
3576 dev_err(dev->class_dev,
3577 "SPI serial I/O didn't finish in time!\n");
3584 * Delay for last bit. This delay is absolutely necessary, because
3585 * NISTC_STATUS1_SERIO_IN_PROG goes high one bit too early.
3587 udelay((devpriv->serial_interval_ns + 999) / 1000);
3590 *data_in = ni_stc_readw(dev, NISTC_DIO_SERIAL_IN_REG);
3593 ni_stc_writew(dev, devpriv->dio_control, NISTC_DIO_CTRL_REG);
3598 static int ni_serial_sw_readwrite8(struct comedi_device *dev,
3599 struct comedi_subdevice *s,
3600 unsigned char data_out,
3601 unsigned char *data_in)
3603 struct ni_private *devpriv = dev->private;
3604 unsigned char mask, input = 0;
3606 /* Wait for one bit before transfer */
3607 udelay((devpriv->serial_interval_ns + 999) / 1000);
3609 for (mask = 0x80; mask; mask >>= 1) {
3610 /* Output current bit; note that we cannot touch s->state
3611 because it is a per-subdevice field, and serial is
3612 a separate subdevice from DIO. */
3613 devpriv->dio_output &= ~NISTC_DIO_SDOUT;
3614 if (data_out & mask)
3615 devpriv->dio_output |= NISTC_DIO_SDOUT;
3616 ni_stc_writew(dev, devpriv->dio_output, NISTC_DIO_OUT_REG);
3618 /* Assert SDCLK (active low, inverted), wait for half of
3619 the delay, deassert SDCLK, and wait for the other half. */
3620 devpriv->dio_control |= NISTC_DIO_SDCLK;
3621 ni_stc_writew(dev, devpriv->dio_control, NISTC_DIO_CTRL_REG);
3623 udelay((devpriv->serial_interval_ns + 999) / 2000);
3625 devpriv->dio_control &= ~NISTC_DIO_SDCLK;
3626 ni_stc_writew(dev, devpriv->dio_control, NISTC_DIO_CTRL_REG);
3628 udelay((devpriv->serial_interval_ns + 999) / 2000);
3630 /* Input current bit */
3631 if (ni_stc_readw(dev, NISTC_DIO_IN_REG) & NISTC_DIO_SDIN)
3641 static int ni_serial_insn_config(struct comedi_device *dev,
3642 struct comedi_subdevice *s,
3643 struct comedi_insn *insn,
3646 struct ni_private *devpriv = dev->private;
3647 unsigned clk_fout = devpriv->clock_and_fout;
3649 unsigned char byte_out, byte_in = 0;
3655 case INSN_CONFIG_SERIAL_CLOCK:
3656 devpriv->serial_hw_mode = 1;
3657 devpriv->dio_control |= NISTC_DIO_CTRL_HW_SER_ENA;
3659 if (data[1] == SERIAL_DISABLED) {
3660 devpriv->serial_hw_mode = 0;
3661 devpriv->dio_control &= ~(NISTC_DIO_CTRL_HW_SER_ENA |
3663 data[1] = SERIAL_DISABLED;
3664 devpriv->serial_interval_ns = data[1];
3665 } else if (data[1] <= SERIAL_600NS) {
3666 /* Warning: this clock speed is too fast to reliably
3668 devpriv->dio_control &= ~NISTC_DIO_CTRL_HW_SER_TIMEBASE;
3669 clk_fout |= NISTC_CLK_FOUT_SLOW_TIMEBASE;
3670 clk_fout &= ~NISTC_CLK_FOUT_DIO_SER_OUT_DIV2;
3671 data[1] = SERIAL_600NS;
3672 devpriv->serial_interval_ns = data[1];
3673 } else if (data[1] <= SERIAL_1_2US) {
3674 devpriv->dio_control &= ~NISTC_DIO_CTRL_HW_SER_TIMEBASE;
3675 clk_fout |= NISTC_CLK_FOUT_SLOW_TIMEBASE |
3676 NISTC_CLK_FOUT_DIO_SER_OUT_DIV2;
3677 data[1] = SERIAL_1_2US;
3678 devpriv->serial_interval_ns = data[1];
3679 } else if (data[1] <= SERIAL_10US) {
3680 devpriv->dio_control |= NISTC_DIO_CTRL_HW_SER_TIMEBASE;
3681 clk_fout |= NISTC_CLK_FOUT_SLOW_TIMEBASE |
3682 NISTC_CLK_FOUT_DIO_SER_OUT_DIV2;
3683 /* Note: NISTC_CLK_FOUT_DIO_SER_OUT_DIV2 only affects
3684 600ns/1.2us. If you turn divide_by_2 off with the
3685 slow clock, you will still get 10us, except then
3686 all your delays are wrong. */
3687 data[1] = SERIAL_10US;
3688 devpriv->serial_interval_ns = data[1];
3690 devpriv->dio_control &= ~(NISTC_DIO_CTRL_HW_SER_ENA |
3692 devpriv->serial_hw_mode = 0;
3693 data[1] = (data[1] / 1000) * 1000;
3694 devpriv->serial_interval_ns = data[1];
3696 devpriv->clock_and_fout = clk_fout;
3698 ni_stc_writew(dev, devpriv->dio_control, NISTC_DIO_CTRL_REG);
3699 ni_stc_writew(dev, devpriv->clock_and_fout, NISTC_CLK_FOUT_REG);
3702 case INSN_CONFIG_BIDIRECTIONAL_DATA:
3704 if (devpriv->serial_interval_ns == 0)
3707 byte_out = data[1] & 0xFF;
3709 if (devpriv->serial_hw_mode) {
3710 err = ni_serial_hw_readwrite8(dev, s, byte_out,
3712 } else if (devpriv->serial_interval_ns > 0) {
3713 err = ni_serial_sw_readwrite8(dev, s, byte_out,
3716 dev_err(dev->class_dev, "serial disabled!\n");
3721 data[1] = byte_in & 0xFF;
3730 static void init_ao_67xx(struct comedi_device *dev, struct comedi_subdevice *s)
3734 for (i = 0; i < s->n_chan; i++) {
3735 ni_ao_win_outw(dev, NI_E_AO_DACSEL(i) | 0x0,
3736 NI67XX_AO_CFG2_REG);
3738 ni_ao_win_outw(dev, 0x0, NI67XX_AO_SP_UPDATES_REG);
3741 static const struct mio_regmap ni_gpct_to_stc_regmap[] = {
3742 [NITIO_G0_AUTO_INC] = { NISTC_G0_AUTOINC_REG, 2 },
3743 [NITIO_G1_AUTO_INC] = { NISTC_G1_AUTOINC_REG, 2 },
3744 [NITIO_G0_CMD] = { NISTC_G0_CMD_REG, 2 },
3745 [NITIO_G1_CMD] = { NISTC_G1_CMD_REG, 2 },
3746 [NITIO_G0_HW_SAVE] = { NISTC_G0_HW_SAVE_REG, 4 },
3747 [NITIO_G1_HW_SAVE] = { NISTC_G1_HW_SAVE_REG, 4 },
3748 [NITIO_G0_SW_SAVE] = { NISTC_G0_SAVE_REG, 4 },
3749 [NITIO_G1_SW_SAVE] = { NISTC_G1_SAVE_REG, 4 },
3750 [NITIO_G0_MODE] = { NISTC_G0_MODE_REG, 2 },
3751 [NITIO_G1_MODE] = { NISTC_G1_MODE_REG, 2 },
3752 [NITIO_G0_LOADA] = { NISTC_G0_LOADA_REG, 4 },
3753 [NITIO_G1_LOADA] = { NISTC_G1_LOADA_REG, 4 },
3754 [NITIO_G0_LOADB] = { NISTC_G0_LOADB_REG, 4 },
3755 [NITIO_G1_LOADB] = { NISTC_G1_LOADB_REG, 4 },
3756 [NITIO_G0_INPUT_SEL] = { NISTC_G0_INPUT_SEL_REG, 2 },
3757 [NITIO_G1_INPUT_SEL] = { NISTC_G1_INPUT_SEL_REG, 2 },
3758 [NITIO_G0_CNT_MODE] = { 0x1b0, 2 }, /* M-Series only */
3759 [NITIO_G1_CNT_MODE] = { 0x1b2, 2 }, /* M-Series only */
3760 [NITIO_G0_GATE2] = { 0x1b4, 2 }, /* M-Series only */
3761 [NITIO_G1_GATE2] = { 0x1b6, 2 }, /* M-Series only */
3762 [NITIO_G01_STATUS] = { NISTC_G01_STATUS_REG, 2 },
3763 [NITIO_G01_RESET] = { NISTC_RESET_REG, 2 },
3764 [NITIO_G01_STATUS1] = { NISTC_STATUS1_REG, 2 },
3765 [NITIO_G01_STATUS2] = { NISTC_STATUS2_REG, 2 },
3766 [NITIO_G0_DMA_CFG] = { 0x1b8, 2 }, /* M-Series only */
3767 [NITIO_G1_DMA_CFG] = { 0x1ba, 2 }, /* M-Series only */
3768 [NITIO_G0_DMA_STATUS] = { 0x1b8, 2 }, /* M-Series only */
3769 [NITIO_G1_DMA_STATUS] = { 0x1ba, 2 }, /* M-Series only */
3770 [NITIO_G0_ABZ] = { 0x1c0, 2 }, /* M-Series only */
3771 [NITIO_G1_ABZ] = { 0x1c2, 2 }, /* M-Series only */
3772 [NITIO_G0_INT_ACK] = { NISTC_INTA_ACK_REG, 2 },
3773 [NITIO_G1_INT_ACK] = { NISTC_INTB_ACK_REG, 2 },
3774 [NITIO_G0_STATUS] = { NISTC_AI_STATUS1_REG, 2 },
3775 [NITIO_G1_STATUS] = { NISTC_AO_STATUS1_REG, 2 },
3776 [NITIO_G0_INT_ENA] = { NISTC_INTA_ENA_REG, 2 },
3777 [NITIO_G1_INT_ENA] = { NISTC_INTB_ENA_REG, 2 },
3780 static unsigned int ni_gpct_to_stc_register(struct comedi_device *dev,
3781 enum ni_gpct_register reg)
3783 const struct mio_regmap *regmap;
3785 if (reg < ARRAY_SIZE(ni_gpct_to_stc_regmap)) {
3786 regmap = &ni_gpct_to_stc_regmap[reg];
3788 dev_warn(dev->class_dev, "%s: unhandled register=0x%x\n",
3793 return regmap->mio_reg;
3796 static void ni_gpct_write_register(struct ni_gpct *counter, unsigned bits,
3797 enum ni_gpct_register reg)
3799 struct comedi_device *dev = counter->counter_dev->dev;
3800 unsigned int stc_register = ni_gpct_to_stc_register(dev, reg);
3801 static const unsigned gpct_interrupt_a_enable_mask =
3802 NISTC_INTA_ENA_G0_GATE | NISTC_INTA_ENA_G0_TC;
3803 static const unsigned gpct_interrupt_b_enable_mask =
3804 NISTC_INTB_ENA_G1_GATE | NISTC_INTB_ENA_G1_TC;
3806 if (stc_register == 0)
3810 /* m-series only registers */
3811 case NITIO_G0_CNT_MODE:
3812 case NITIO_G1_CNT_MODE:
3813 case NITIO_G0_GATE2:
3814 case NITIO_G1_GATE2:
3815 case NITIO_G0_DMA_CFG:
3816 case NITIO_G1_DMA_CFG:
3819 ni_writew(dev, bits, stc_register);
3822 /* 32 bit registers */
3823 case NITIO_G0_LOADA:
3824 case NITIO_G1_LOADA:
3825 case NITIO_G0_LOADB:
3826 case NITIO_G1_LOADB:
3827 ni_stc_writel(dev, bits, stc_register);
3830 /* 16 bit registers */
3831 case NITIO_G0_INT_ENA:
3832 BUG_ON(bits & ~gpct_interrupt_a_enable_mask);
3833 ni_set_bitfield(dev, stc_register,
3834 gpct_interrupt_a_enable_mask, bits);
3836 case NITIO_G1_INT_ENA:
3837 BUG_ON(bits & ~gpct_interrupt_b_enable_mask);
3838 ni_set_bitfield(dev, stc_register,
3839 gpct_interrupt_b_enable_mask, bits);
3841 case NITIO_G01_RESET:
3842 BUG_ON(bits & ~(NISTC_RESET_G0 | NISTC_RESET_G1));
3845 ni_stc_writew(dev, bits, stc_register);
3849 static unsigned ni_gpct_read_register(struct ni_gpct *counter,
3850 enum ni_gpct_register reg)
3852 struct comedi_device *dev = counter->counter_dev->dev;
3853 unsigned int stc_register = ni_gpct_to_stc_register(dev, reg);
3855 if (stc_register == 0)
3859 /* m-series only registers */
3860 case NITIO_G0_DMA_STATUS:
3861 case NITIO_G1_DMA_STATUS:
3862 return ni_readw(dev, stc_register);
3864 /* 32 bit registers */
3865 case NITIO_G0_HW_SAVE:
3866 case NITIO_G1_HW_SAVE:
3867 case NITIO_G0_SW_SAVE:
3868 case NITIO_G1_SW_SAVE:
3869 return ni_stc_readl(dev, stc_register);
3871 /* 16 bit registers */
3873 return ni_stc_readw(dev, stc_register);
3877 static int ni_freq_out_insn_read(struct comedi_device *dev,
3878 struct comedi_subdevice *s,
3879 struct comedi_insn *insn,
3882 struct ni_private *devpriv = dev->private;
3883 unsigned int val = NISTC_CLK_FOUT_TO_DIVIDER(devpriv->clock_and_fout);
3886 for (i = 0; i < insn->n; i++)
3892 static int ni_freq_out_insn_write(struct comedi_device *dev,
3893 struct comedi_subdevice *s,
3894 struct comedi_insn *insn,
3897 struct ni_private *devpriv = dev->private;
3900 unsigned int val = data[insn->n - 1];
3902 devpriv->clock_and_fout &= ~NISTC_CLK_FOUT_ENA;
3903 ni_stc_writew(dev, devpriv->clock_and_fout, NISTC_CLK_FOUT_REG);
3904 devpriv->clock_and_fout &= ~NISTC_CLK_FOUT_DIVIDER_MASK;
3906 /* use the last data value to set the fout divider */
3907 devpriv->clock_and_fout |= NISTC_CLK_FOUT_DIVIDER(val);
3909 devpriv->clock_and_fout |= NISTC_CLK_FOUT_ENA;
3910 ni_stc_writew(dev, devpriv->clock_and_fout, NISTC_CLK_FOUT_REG);
3915 static int ni_freq_out_insn_config(struct comedi_device *dev,
3916 struct comedi_subdevice *s,
3917 struct comedi_insn *insn,
3920 struct ni_private *devpriv = dev->private;
3923 case INSN_CONFIG_SET_CLOCK_SRC:
3925 case NI_FREQ_OUT_TIMEBASE_1_DIV_2_CLOCK_SRC:
3926 devpriv->clock_and_fout &= ~NISTC_CLK_FOUT_TIMEBASE_SEL;
3928 case NI_FREQ_OUT_TIMEBASE_2_CLOCK_SRC:
3929 devpriv->clock_and_fout |= NISTC_CLK_FOUT_TIMEBASE_SEL;
3934 ni_stc_writew(dev, devpriv->clock_and_fout, NISTC_CLK_FOUT_REG);
3936 case INSN_CONFIG_GET_CLOCK_SRC:
3937 if (devpriv->clock_and_fout & NISTC_CLK_FOUT_TIMEBASE_SEL) {
3938 data[1] = NI_FREQ_OUT_TIMEBASE_2_CLOCK_SRC;
3939 data[2] = TIMEBASE_2_NS;
3941 data[1] = NI_FREQ_OUT_TIMEBASE_1_DIV_2_CLOCK_SRC;
3942 data[2] = TIMEBASE_1_NS * 2;
3951 static int ni_8255_callback(struct comedi_device *dev,
3952 int dir, int port, int data, unsigned long iobase)
3955 ni_writeb(dev, data, iobase + 2 * port);
3959 return ni_readb(dev, iobase + 2 * port);
3962 static int ni_get_pwm_config(struct comedi_device *dev, unsigned int *data)
3964 struct ni_private *devpriv = dev->private;
3966 data[1] = devpriv->pwm_up_count * devpriv->clock_ns;
3967 data[2] = devpriv->pwm_down_count * devpriv->clock_ns;
3971 static int ni_m_series_pwm_config(struct comedi_device *dev,
3972 struct comedi_subdevice *s,
3973 struct comedi_insn *insn,
3976 struct ni_private *devpriv = dev->private;
3977 unsigned up_count, down_count;
3980 case INSN_CONFIG_PWM_OUTPUT:
3982 case CMDF_ROUND_NEAREST:
3985 devpriv->clock_ns / 2) / devpriv->clock_ns;
3987 case CMDF_ROUND_DOWN:
3988 up_count = data[2] / devpriv->clock_ns;
3992 (data[2] + devpriv->clock_ns -
3993 1) / devpriv->clock_ns;
3999 case CMDF_ROUND_NEAREST:
4002 devpriv->clock_ns / 2) / devpriv->clock_ns;
4004 case CMDF_ROUND_DOWN:
4005 down_count = data[4] / devpriv->clock_ns;
4009 (data[4] + devpriv->clock_ns -
4010 1) / devpriv->clock_ns;
4015 if (up_count * devpriv->clock_ns != data[2] ||
4016 down_count * devpriv->clock_ns != data[4]) {
4017 data[2] = up_count * devpriv->clock_ns;
4018 data[4] = down_count * devpriv->clock_ns;
4021 ni_writel(dev, NI_M_CAL_PWM_HIGH_TIME(up_count) |
4022 NI_M_CAL_PWM_LOW_TIME(down_count),
4024 devpriv->pwm_up_count = up_count;
4025 devpriv->pwm_down_count = down_count;
4027 case INSN_CONFIG_GET_PWM_OUTPUT:
4028 return ni_get_pwm_config(dev, data);
4035 static int ni_6143_pwm_config(struct comedi_device *dev,
4036 struct comedi_subdevice *s,
4037 struct comedi_insn *insn,
4040 struct ni_private *devpriv = dev->private;
4041 unsigned up_count, down_count;
4044 case INSN_CONFIG_PWM_OUTPUT:
4046 case CMDF_ROUND_NEAREST:
4049 devpriv->clock_ns / 2) / devpriv->clock_ns;
4051 case CMDF_ROUND_DOWN:
4052 up_count = data[2] / devpriv->clock_ns;
4056 (data[2] + devpriv->clock_ns -
4057 1) / devpriv->clock_ns;
4063 case CMDF_ROUND_NEAREST:
4066 devpriv->clock_ns / 2) / devpriv->clock_ns;
4068 case CMDF_ROUND_DOWN:
4069 down_count = data[4] / devpriv->clock_ns;
4073 (data[4] + devpriv->clock_ns -
4074 1) / devpriv->clock_ns;
4079 if (up_count * devpriv->clock_ns != data[2] ||
4080 down_count * devpriv->clock_ns != data[4]) {
4081 data[2] = up_count * devpriv->clock_ns;
4082 data[4] = down_count * devpriv->clock_ns;
4085 ni_writel(dev, up_count, NI6143_CALIB_HI_TIME_REG);
4086 devpriv->pwm_up_count = up_count;
4087 ni_writel(dev, down_count, NI6143_CALIB_LO_TIME_REG);
4088 devpriv->pwm_down_count = down_count;
4090 case INSN_CONFIG_GET_PWM_OUTPUT:
4091 return ni_get_pwm_config(dev, data);
4098 static int pack_mb88341(int addr, int val, int *bitstring)
4102 Note that address bits are reversed. Thanks to
4103 Ingo Keen for noticing this.
4105 Note also that the 88341 expects address values from
4106 1-12, whereas we use channel numbers 0-11. The NI
4107 docs use 1-12, also, so be careful here.
4110 *bitstring = ((addr & 0x1) << 11) |
4111 ((addr & 0x2) << 9) |
4112 ((addr & 0x4) << 7) | ((addr & 0x8) << 5) | (val & 0xff);
4116 static int pack_dac8800(int addr, int val, int *bitstring)
4118 *bitstring = ((addr & 0x7) << 8) | (val & 0xff);
4122 static int pack_dac8043(int addr, int val, int *bitstring)
4124 *bitstring = val & 0xfff;
4128 static int pack_ad8522(int addr, int val, int *bitstring)
4130 *bitstring = (val & 0xfff) | (addr ? 0xc000 : 0xa000);
4134 static int pack_ad8804(int addr, int val, int *bitstring)
4136 *bitstring = ((addr & 0xf) << 8) | (val & 0xff);
4140 static int pack_ad8842(int addr, int val, int *bitstring)
4142 *bitstring = ((addr + 1) << 8) | (val & 0xff);
4146 struct caldac_struct {
4149 int (*packbits)(int, int, int *);
4152 static struct caldac_struct caldacs[] = {
4153 [mb88341] = {12, 8, pack_mb88341},
4154 [dac8800] = {8, 8, pack_dac8800},
4155 [dac8043] = {1, 12, pack_dac8043},
4156 [ad8522] = {2, 12, pack_ad8522},
4157 [ad8804] = {12, 8, pack_ad8804},
4158 [ad8842] = {8, 8, pack_ad8842},
4159 [ad8804_debug] = {16, 8, pack_ad8804},
4162 static void ni_write_caldac(struct comedi_device *dev, int addr, int val)
4164 const struct ni_board_struct *board = dev->board_ptr;
4165 struct ni_private *devpriv = dev->private;
4166 unsigned int loadbit = 0, bits = 0, bit, bitstring = 0;
4171 if (devpriv->caldacs[addr] == val)
4173 devpriv->caldacs[addr] = val;
4175 for (i = 0; i < 3; i++) {
4176 type = board->caldac[i];
4177 if (type == caldac_none)
4179 if (addr < caldacs[type].n_chans) {
4180 bits = caldacs[type].packbits(addr, val, &bitstring);
4181 loadbit = NI_E_SERIAL_CMD_DAC_LD(i);
4184 addr -= caldacs[type].n_chans;
4187 /* bits will be 0 if there is no caldac for the given addr */
4191 for (bit = 1 << (bits - 1); bit; bit >>= 1) {
4192 cmd = (bit & bitstring) ? NI_E_SERIAL_CMD_SDATA : 0;
4193 ni_writeb(dev, cmd, NI_E_SERIAL_CMD_REG);
4195 ni_writeb(dev, NI_E_SERIAL_CMD_SCLK | cmd, NI_E_SERIAL_CMD_REG);
4198 ni_writeb(dev, loadbit, NI_E_SERIAL_CMD_REG);
4200 ni_writeb(dev, 0, NI_E_SERIAL_CMD_REG);
4203 static int ni_calib_insn_write(struct comedi_device *dev,
4204 struct comedi_subdevice *s,
4205 struct comedi_insn *insn,
4208 ni_write_caldac(dev, CR_CHAN(insn->chanspec), data[0]);
4213 static int ni_calib_insn_read(struct comedi_device *dev,
4214 struct comedi_subdevice *s,
4215 struct comedi_insn *insn,
4218 struct ni_private *devpriv = dev->private;
4220 data[0] = devpriv->caldacs[CR_CHAN(insn->chanspec)];
4225 static void caldac_setup(struct comedi_device *dev, struct comedi_subdevice *s)
4227 const struct ni_board_struct *board = dev->board_ptr;
4228 struct ni_private *devpriv = dev->private;
4237 type = board->caldac[0];
4238 if (type == caldac_none)
4240 n_bits = caldacs[type].n_bits;
4241 for (i = 0; i < 3; i++) {
4242 type = board->caldac[i];
4243 if (type == caldac_none)
4245 if (caldacs[type].n_bits != n_bits)
4247 n_chans += caldacs[type].n_chans;
4250 s->n_chan = n_chans;
4253 unsigned int *maxdata_list;
4255 if (n_chans > MAX_N_CALDACS)
4256 dev_err(dev->class_dev,
4257 "BUG! MAX_N_CALDACS too small\n");
4258 s->maxdata_list = maxdata_list = devpriv->caldac_maxdata_list;
4260 for (i = 0; i < n_dacs; i++) {
4261 type = board->caldac[i];
4262 for (j = 0; j < caldacs[type].n_chans; j++) {
4263 maxdata_list[chan] =
4264 (1 << caldacs[type].n_bits) - 1;
4269 for (chan = 0; chan < s->n_chan; chan++)
4270 ni_write_caldac(dev, i, s->maxdata_list[i] / 2);
4272 type = board->caldac[0];
4273 s->maxdata = (1 << caldacs[type].n_bits) - 1;
4275 for (chan = 0; chan < s->n_chan; chan++)
4276 ni_write_caldac(dev, i, s->maxdata / 2);
4280 static int ni_read_eeprom(struct comedi_device *dev, int addr)
4282 unsigned int cmd = NI_E_SERIAL_CMD_EEPROM_CS;
4286 bitstring = 0x0300 | ((addr & 0x100) << 3) | (addr & 0xff);
4287 ni_writeb(dev, cmd, NI_E_SERIAL_CMD_REG);
4288 for (bit = 0x8000; bit; bit >>= 1) {
4289 if (bit & bitstring)
4290 cmd |= NI_E_SERIAL_CMD_SDATA;
4292 cmd &= ~NI_E_SERIAL_CMD_SDATA;
4294 ni_writeb(dev, cmd, NI_E_SERIAL_CMD_REG);
4295 ni_writeb(dev, NI_E_SERIAL_CMD_SCLK | cmd, NI_E_SERIAL_CMD_REG);
4297 cmd = NI_E_SERIAL_CMD_EEPROM_CS;
4299 for (bit = 0x80; bit; bit >>= 1) {
4300 ni_writeb(dev, cmd, NI_E_SERIAL_CMD_REG);
4301 ni_writeb(dev, NI_E_SERIAL_CMD_SCLK | cmd, NI_E_SERIAL_CMD_REG);
4302 if (ni_readb(dev, NI_E_STATUS_REG) & NI_E_STATUS_PROMOUT)
4305 ni_writeb(dev, 0, NI_E_SERIAL_CMD_REG);
4310 static int ni_eeprom_insn_read(struct comedi_device *dev,
4311 struct comedi_subdevice *s,
4312 struct comedi_insn *insn,
4315 data[0] = ni_read_eeprom(dev, CR_CHAN(insn->chanspec));
4320 static int ni_m_series_eeprom_insn_read(struct comedi_device *dev,
4321 struct comedi_subdevice *s,
4322 struct comedi_insn *insn,
4325 struct ni_private *devpriv = dev->private;
4327 data[0] = devpriv->eeprom_buffer[CR_CHAN(insn->chanspec)];
4332 static unsigned ni_old_get_pfi_routing(struct comedi_device *dev,
4335 /* pre-m-series boards have fixed signals on pfi pins */
4338 return NI_PFI_OUTPUT_AI_START1;
4340 return NI_PFI_OUTPUT_AI_START2;
4342 return NI_PFI_OUTPUT_AI_CONVERT;
4344 return NI_PFI_OUTPUT_G_SRC1;
4346 return NI_PFI_OUTPUT_G_GATE1;
4348 return NI_PFI_OUTPUT_AO_UPDATE_N;
4350 return NI_PFI_OUTPUT_AO_START1;
4352 return NI_PFI_OUTPUT_AI_START_PULSE;
4354 return NI_PFI_OUTPUT_G_SRC0;
4356 return NI_PFI_OUTPUT_G_GATE0;
4358 dev_err(dev->class_dev, "bug, unhandled case in switch.\n");
4364 static int ni_old_set_pfi_routing(struct comedi_device *dev,
4365 unsigned chan, unsigned source)
4367 /* pre-m-series boards have fixed signals on pfi pins */
4368 if (source != ni_old_get_pfi_routing(dev, chan))
4373 static unsigned ni_m_series_get_pfi_routing(struct comedi_device *dev,
4376 struct ni_private *devpriv = dev->private;
4377 const unsigned array_offset = chan / 3;
4379 return NI_M_PFI_OUT_SEL_TO_SRC(chan,
4380 devpriv->pfi_output_select_reg[array_offset]);
4383 static int ni_m_series_set_pfi_routing(struct comedi_device *dev,
4384 unsigned chan, unsigned source)
4386 struct ni_private *devpriv = dev->private;
4387 unsigned index = chan / 3;
4388 unsigned short val = devpriv->pfi_output_select_reg[index];
4390 if ((source & 0x1f) != source)
4393 val &= ~NI_M_PFI_OUT_SEL_MASK(chan);
4394 val |= NI_M_PFI_OUT_SEL(chan, source);
4395 ni_writew(dev, val, NI_M_PFI_OUT_SEL_REG(index));
4396 devpriv->pfi_output_select_reg[index] = val;
4401 static unsigned ni_get_pfi_routing(struct comedi_device *dev, unsigned chan)
4403 struct ni_private *devpriv = dev->private;
4405 return (devpriv->is_m_series)
4406 ? ni_m_series_get_pfi_routing(dev, chan)
4407 : ni_old_get_pfi_routing(dev, chan);
4410 static int ni_set_pfi_routing(struct comedi_device *dev, unsigned chan,
4413 struct ni_private *devpriv = dev->private;
4415 return (devpriv->is_m_series)
4416 ? ni_m_series_set_pfi_routing(dev, chan, source)
4417 : ni_old_set_pfi_routing(dev, chan, source);
4420 static int ni_config_filter(struct comedi_device *dev,
4421 unsigned pfi_channel,
4422 enum ni_pfi_filter_select filter)
4424 struct ni_private *devpriv = dev->private;
4427 if (!devpriv->is_m_series)
4430 bits = ni_readl(dev, NI_M_PFI_FILTER_REG);
4431 bits &= ~NI_M_PFI_FILTER_SEL_MASK(pfi_channel);
4432 bits |= NI_M_PFI_FILTER_SEL(pfi_channel, filter);
4433 ni_writel(dev, bits, NI_M_PFI_FILTER_REG);
4437 static int ni_pfi_insn_config(struct comedi_device *dev,
4438 struct comedi_subdevice *s,
4439 struct comedi_insn *insn,
4442 struct ni_private *devpriv = dev->private;
4448 chan = CR_CHAN(insn->chanspec);
4452 ni_set_bits(dev, NISTC_IO_BIDIR_PIN_REG, 1 << chan, 1);
4455 ni_set_bits(dev, NISTC_IO_BIDIR_PIN_REG, 1 << chan, 0);
4457 case INSN_CONFIG_DIO_QUERY:
4459 (devpriv->io_bidirection_pin_reg & (1 << chan)) ?
4460 COMEDI_OUTPUT : COMEDI_INPUT;
4462 case INSN_CONFIG_SET_ROUTING:
4463 return ni_set_pfi_routing(dev, chan, data[1]);
4464 case INSN_CONFIG_GET_ROUTING:
4465 data[1] = ni_get_pfi_routing(dev, chan);
4467 case INSN_CONFIG_FILTER:
4468 return ni_config_filter(dev, chan, data[1]);
4475 static int ni_pfi_insn_bits(struct comedi_device *dev,
4476 struct comedi_subdevice *s,
4477 struct comedi_insn *insn,
4480 struct ni_private *devpriv = dev->private;
4482 if (!devpriv->is_m_series)
4485 if (comedi_dio_update_state(s, data))
4486 ni_writew(dev, s->state, NI_M_PFI_DO_REG);
4488 data[1] = ni_readw(dev, NI_M_PFI_DI_REG);
4493 static int cs5529_wait_for_idle(struct comedi_device *dev)
4495 unsigned short status;
4496 const int timeout = HZ;
4499 for (i = 0; i < timeout; i++) {
4500 status = ni_ao_win_inw(dev, NI67XX_CAL_STATUS_REG);
4501 if ((status & NI67XX_CAL_STATUS_BUSY) == 0)
4503 set_current_state(TASK_INTERRUPTIBLE);
4504 if (schedule_timeout(1))
4508 dev_err(dev->class_dev, "timeout\n");
4514 static void cs5529_command(struct comedi_device *dev, unsigned short value)
4516 static const int timeout = 100;
4519 ni_ao_win_outw(dev, value, NI67XX_CAL_CMD_REG);
4520 /* give time for command to start being serially clocked into cs5529.
4521 * this insures that the NI67XX_CAL_STATUS_BUSY bit will get properly
4522 * set before we exit this function.
4524 for (i = 0; i < timeout; i++) {
4525 if (ni_ao_win_inw(dev, NI67XX_CAL_STATUS_REG) &
4526 NI67XX_CAL_STATUS_BUSY)
4531 dev_err(dev->class_dev,
4532 "possible problem - never saw adc go busy?\n");
4535 static int cs5529_do_conversion(struct comedi_device *dev,
4536 unsigned short *data)
4539 unsigned short status;
4541 cs5529_command(dev, CS5529_CMD_CB | CS5529_CMD_SINGLE_CONV);
4542 retval = cs5529_wait_for_idle(dev);
4544 dev_err(dev->class_dev,
4545 "timeout or signal in cs5529_do_conversion()\n");
4548 status = ni_ao_win_inw(dev, NI67XX_CAL_STATUS_REG);
4549 if (status & NI67XX_CAL_STATUS_OSC_DETECT) {
4550 dev_err(dev->class_dev,
4551 "cs5529 conversion error, status CSS_OSC_DETECT\n");
4554 if (status & NI67XX_CAL_STATUS_OVERRANGE) {
4555 dev_err(dev->class_dev,
4556 "cs5529 conversion error, overrange (ignoring)\n");
4559 *data = ni_ao_win_inw(dev, NI67XX_CAL_DATA_REG);
4560 /* cs5529 returns 16 bit signed data in bipolar mode */
4566 static int cs5529_ai_insn_read(struct comedi_device *dev,
4567 struct comedi_subdevice *s,
4568 struct comedi_insn *insn,
4572 unsigned short sample;
4573 unsigned int channel_select;
4574 const unsigned int INTERNAL_REF = 0x1000;
4576 /* Set calibration adc source. Docs lie, reference select bits 8 to 11
4577 * do nothing. bit 12 seems to chooses internal reference voltage, bit
4578 * 13 causes the adc input to go overrange (maybe reads external reference?) */
4579 if (insn->chanspec & CR_ALT_SOURCE)
4580 channel_select = INTERNAL_REF;
4582 channel_select = CR_CHAN(insn->chanspec);
4583 ni_ao_win_outw(dev, channel_select, NI67XX_AO_CAL_CHAN_SEL_REG);
4585 for (n = 0; n < insn->n; n++) {
4586 retval = cs5529_do_conversion(dev, &sample);
4594 static void cs5529_config_write(struct comedi_device *dev, unsigned int value,
4595 unsigned int reg_select_bits)
4597 ni_ao_win_outw(dev, (value >> 16) & 0xff, NI67XX_CAL_CFG_HI_REG);
4598 ni_ao_win_outw(dev, value & 0xffff, NI67XX_CAL_CFG_LO_REG);
4599 reg_select_bits &= CS5529_CMD_REG_MASK;
4600 cs5529_command(dev, CS5529_CMD_CB | reg_select_bits);
4601 if (cs5529_wait_for_idle(dev))
4602 dev_err(dev->class_dev,
4603 "timeout or signal in %s\n", __func__);
4606 static int init_cs5529(struct comedi_device *dev)
4608 unsigned int config_bits = CS5529_CFG_PORT_FLAG |
4609 CS5529_CFG_WORD_RATE_2180;
4612 /* do self-calibration */
4613 cs5529_config_write(dev, config_bits | CS5529_CFG_CALIB_BOTH_SELF,
4615 /* need to force a conversion for calibration to run */
4616 cs5529_do_conversion(dev, NULL);
4618 /* force gain calibration to 1 */
4619 cs5529_config_write(dev, 0x400000, CS5529_GAIN_REG);
4620 cs5529_config_write(dev, config_bits | CS5529_CFG_CALIB_OFFSET_SELF,
4622 if (cs5529_wait_for_idle(dev))
4623 dev_err(dev->class_dev,
4624 "timeout or signal in %s\n", __func__);
4630 * Find best multiplier/divider to try and get the PLL running at 80 MHz
4631 * given an arbitrary frequency input clock.
4633 static int ni_mseries_get_pll_parameters(unsigned reference_period_ns,
4634 unsigned *freq_divider,
4635 unsigned *freq_multiplier,
4636 unsigned *actual_period_ns)
4639 unsigned best_div = 1;
4641 unsigned best_mult = 1;
4642 static const unsigned pico_per_nano = 1000;
4644 const unsigned reference_picosec = reference_period_ns * pico_per_nano;
4645 /* m-series wants the phased-locked loop to output 80MHz, which is divided by 4 to
4646 * 20 MHz for most timing clocks */
4647 static const unsigned target_picosec = 12500;
4648 static const unsigned fudge_factor_80_to_20Mhz = 4;
4649 int best_period_picosec = 0;
4651 for (div = 1; div <= NI_M_PLL_MAX_DIVISOR; ++div) {
4652 for (mult = 1; mult <= NI_M_PLL_MAX_MULTIPLIER; ++mult) {
4653 unsigned new_period_ps =
4654 (reference_picosec * div) / mult;
4655 if (abs(new_period_ps - target_picosec) <
4656 abs(best_period_picosec - target_picosec)) {
4657 best_period_picosec = new_period_ps;
4663 if (best_period_picosec == 0)
4666 *freq_divider = best_div;
4667 *freq_multiplier = best_mult;
4669 (best_period_picosec * fudge_factor_80_to_20Mhz +
4670 (pico_per_nano / 2)) / pico_per_nano;
4674 static int ni_mseries_set_pll_master_clock(struct comedi_device *dev,
4675 unsigned source, unsigned period_ns)
4677 struct ni_private *devpriv = dev->private;
4678 static const unsigned min_period_ns = 50;
4679 static const unsigned max_period_ns = 1000;
4680 static const unsigned timeout = 1000;
4681 unsigned pll_control_bits;
4682 unsigned freq_divider;
4683 unsigned freq_multiplier;
4688 if (source == NI_MIO_PLL_PXI10_CLOCK)
4690 /* these limits are somewhat arbitrary, but NI advertises 1 to 20MHz range so we'll use that */
4691 if (period_ns < min_period_ns || period_ns > max_period_ns) {
4692 dev_err(dev->class_dev,
4693 "%s: you must specify an input clock frequency between %i and %i nanosec for the phased-lock loop\n",
4694 __func__, min_period_ns, max_period_ns);
4697 devpriv->rtsi_trig_direction_reg &= ~NISTC_RTSI_TRIG_USE_CLK;
4698 ni_stc_writew(dev, devpriv->rtsi_trig_direction_reg,
4699 NISTC_RTSI_TRIG_DIR_REG);
4700 pll_control_bits = NI_M_PLL_CTRL_ENA | NI_M_PLL_CTRL_VCO_MODE_75_150MHZ;
4701 devpriv->clock_and_fout2 |= NI_M_CLK_FOUT2_TIMEBASE1_PLL |
4702 NI_M_CLK_FOUT2_TIMEBASE3_PLL;
4703 devpriv->clock_and_fout2 &= ~NI_M_CLK_FOUT2_PLL_SRC_MASK;
4705 case NI_MIO_PLL_PXI_STAR_TRIGGER_CLOCK:
4706 devpriv->clock_and_fout2 |= NI_M_CLK_FOUT2_PLL_SRC_STAR;
4708 case NI_MIO_PLL_PXI10_CLOCK:
4709 /* pxi clock is 10MHz */
4710 devpriv->clock_and_fout2 |= NI_M_CLK_FOUT2_PLL_SRC_PXI10;
4713 for (rtsi = 0; rtsi <= NI_M_MAX_RTSI_CHAN; ++rtsi) {
4714 if (source == NI_MIO_PLL_RTSI_CLOCK(rtsi)) {
4715 devpriv->clock_and_fout2 |=
4716 NI_M_CLK_FOUT2_PLL_SRC_RTSI(rtsi);
4720 if (rtsi > NI_M_MAX_RTSI_CHAN)
4724 retval = ni_mseries_get_pll_parameters(period_ns,
4727 &devpriv->clock_ns);
4729 dev_err(dev->class_dev,
4730 "bug, failed to find pll parameters\n");
4734 ni_writew(dev, devpriv->clock_and_fout2, NI_M_CLK_FOUT2_REG);
4735 pll_control_bits |= NI_M_PLL_CTRL_DIVISOR(freq_divider) |
4736 NI_M_PLL_CTRL_MULTIPLIER(freq_multiplier);
4738 ni_writew(dev, pll_control_bits, NI_M_PLL_CTRL_REG);
4739 devpriv->clock_source = source;
4740 /* it seems to typically take a few hundred microseconds for PLL to lock */
4741 for (i = 0; i < timeout; ++i) {
4742 if (ni_readw(dev, NI_M_PLL_STATUS_REG) & NI_M_PLL_STATUS_LOCKED)
4747 dev_err(dev->class_dev,
4748 "%s: timed out waiting for PLL to lock to reference clock source %i with period %i ns\n",
4749 __func__, source, period_ns);
4755 static int ni_set_master_clock(struct comedi_device *dev,
4756 unsigned source, unsigned period_ns)
4758 struct ni_private *devpriv = dev->private;
4760 if (source == NI_MIO_INTERNAL_CLOCK) {
4761 devpriv->rtsi_trig_direction_reg &= ~NISTC_RTSI_TRIG_USE_CLK;
4762 ni_stc_writew(dev, devpriv->rtsi_trig_direction_reg,
4763 NISTC_RTSI_TRIG_DIR_REG);
4764 devpriv->clock_ns = TIMEBASE_1_NS;
4765 if (devpriv->is_m_series) {
4766 devpriv->clock_and_fout2 &=
4767 ~(NI_M_CLK_FOUT2_TIMEBASE1_PLL |
4768 NI_M_CLK_FOUT2_TIMEBASE3_PLL);
4769 ni_writew(dev, devpriv->clock_and_fout2,
4770 NI_M_CLK_FOUT2_REG);
4771 ni_writew(dev, 0, NI_M_PLL_CTRL_REG);
4773 devpriv->clock_source = source;
4775 if (devpriv->is_m_series) {
4776 return ni_mseries_set_pll_master_clock(dev, source,
4779 if (source == NI_MIO_RTSI_CLOCK) {
4780 devpriv->rtsi_trig_direction_reg |=
4781 NISTC_RTSI_TRIG_USE_CLK;
4783 devpriv->rtsi_trig_direction_reg,
4784 NISTC_RTSI_TRIG_DIR_REG);
4785 if (period_ns == 0) {
4786 dev_err(dev->class_dev,
4787 "we don't handle an unspecified clock period correctly yet, returning error\n");
4790 devpriv->clock_ns = period_ns;
4791 devpriv->clock_source = source;
4800 static int ni_valid_rtsi_output_source(struct comedi_device *dev,
4801 unsigned chan, unsigned source)
4803 struct ni_private *devpriv = dev->private;
4805 if (chan >= NISTC_RTSI_TRIG_NUM_CHAN(devpriv->is_m_series)) {
4806 if (chan == NISTC_RTSI_TRIG_OLD_CLK_CHAN) {
4807 if (source == NI_RTSI_OUTPUT_RTSI_OSC)
4810 dev_err(dev->class_dev,
4811 "%s: invalid source for channel=%i, channel %i is always the RTSI clock for pre-m-series boards\n",
4812 __func__, chan, NISTC_RTSI_TRIG_OLD_CLK_CHAN);
4818 case NI_RTSI_OUTPUT_ADR_START1:
4819 case NI_RTSI_OUTPUT_ADR_START2:
4820 case NI_RTSI_OUTPUT_SCLKG:
4821 case NI_RTSI_OUTPUT_DACUPDN:
4822 case NI_RTSI_OUTPUT_DA_START1:
4823 case NI_RTSI_OUTPUT_G_SRC0:
4824 case NI_RTSI_OUTPUT_G_GATE0:
4825 case NI_RTSI_OUTPUT_RGOUT0:
4826 case NI_RTSI_OUTPUT_RTSI_BRD_0:
4828 case NI_RTSI_OUTPUT_RTSI_OSC:
4829 return (devpriv->is_m_series) ? 1 : 0;
4835 static int ni_set_rtsi_routing(struct comedi_device *dev,
4836 unsigned chan, unsigned src)
4838 struct ni_private *devpriv = dev->private;
4840 if (ni_valid_rtsi_output_source(dev, chan, src) == 0)
4843 devpriv->rtsi_trig_a_output_reg &= ~NISTC_RTSI_TRIG_MASK(chan);
4844 devpriv->rtsi_trig_a_output_reg |= NISTC_RTSI_TRIG(chan, src);
4845 ni_stc_writew(dev, devpriv->rtsi_trig_a_output_reg,
4846 NISTC_RTSI_TRIGA_OUT_REG);
4847 } else if (chan < 8) {
4848 devpriv->rtsi_trig_b_output_reg &= ~NISTC_RTSI_TRIG_MASK(chan);
4849 devpriv->rtsi_trig_b_output_reg |= NISTC_RTSI_TRIG(chan, src);
4850 ni_stc_writew(dev, devpriv->rtsi_trig_b_output_reg,
4851 NISTC_RTSI_TRIGB_OUT_REG);
4856 static unsigned ni_get_rtsi_routing(struct comedi_device *dev, unsigned chan)
4858 struct ni_private *devpriv = dev->private;
4861 return NISTC_RTSI_TRIG_TO_SRC(chan,
4862 devpriv->rtsi_trig_a_output_reg);
4863 } else if (chan < NISTC_RTSI_TRIG_NUM_CHAN(devpriv->is_m_series)) {
4864 return NISTC_RTSI_TRIG_TO_SRC(chan,
4865 devpriv->rtsi_trig_b_output_reg);
4867 if (chan == NISTC_RTSI_TRIG_OLD_CLK_CHAN)
4868 return NI_RTSI_OUTPUT_RTSI_OSC;
4869 dev_err(dev->class_dev, "bug! should never get here?\n");
4874 static int ni_rtsi_insn_config(struct comedi_device *dev,
4875 struct comedi_subdevice *s,
4876 struct comedi_insn *insn,
4879 struct ni_private *devpriv = dev->private;
4880 unsigned int chan = CR_CHAN(insn->chanspec);
4881 unsigned int max_chan = NISTC_RTSI_TRIG_NUM_CHAN(devpriv->is_m_series);
4884 case INSN_CONFIG_DIO_OUTPUT:
4885 if (chan < max_chan) {
4886 devpriv->rtsi_trig_direction_reg |=
4887 NISTC_RTSI_TRIG_DIR(chan, devpriv->is_m_series);
4888 } else if (chan == NISTC_RTSI_TRIG_OLD_CLK_CHAN) {
4889 devpriv->rtsi_trig_direction_reg |=
4890 NISTC_RTSI_TRIG_DRV_CLK;
4892 ni_stc_writew(dev, devpriv->rtsi_trig_direction_reg,
4893 NISTC_RTSI_TRIG_DIR_REG);
4895 case INSN_CONFIG_DIO_INPUT:
4896 if (chan < max_chan) {
4897 devpriv->rtsi_trig_direction_reg &=
4898 ~NISTC_RTSI_TRIG_DIR(chan, devpriv->is_m_series);
4899 } else if (chan == NISTC_RTSI_TRIG_OLD_CLK_CHAN) {
4900 devpriv->rtsi_trig_direction_reg &=
4901 ~NISTC_RTSI_TRIG_DRV_CLK;
4903 ni_stc_writew(dev, devpriv->rtsi_trig_direction_reg,
4904 NISTC_RTSI_TRIG_DIR_REG);
4906 case INSN_CONFIG_DIO_QUERY:
4907 if (chan < max_chan) {
4909 (devpriv->rtsi_trig_direction_reg &
4910 NISTC_RTSI_TRIG_DIR(chan, devpriv->is_m_series))
4911 ? INSN_CONFIG_DIO_OUTPUT
4912 : INSN_CONFIG_DIO_INPUT;
4913 } else if (chan == NISTC_RTSI_TRIG_OLD_CLK_CHAN) {
4914 data[1] = (devpriv->rtsi_trig_direction_reg &
4915 NISTC_RTSI_TRIG_DRV_CLK)
4916 ? INSN_CONFIG_DIO_OUTPUT
4917 : INSN_CONFIG_DIO_INPUT;
4920 case INSN_CONFIG_SET_CLOCK_SRC:
4921 return ni_set_master_clock(dev, data[1], data[2]);
4922 case INSN_CONFIG_GET_CLOCK_SRC:
4923 data[1] = devpriv->clock_source;
4924 data[2] = devpriv->clock_ns;
4926 case INSN_CONFIG_SET_ROUTING:
4927 return ni_set_rtsi_routing(dev, chan, data[1]);
4928 case INSN_CONFIG_GET_ROUTING:
4929 data[1] = ni_get_rtsi_routing(dev, chan);
4937 static int ni_rtsi_insn_bits(struct comedi_device *dev,
4938 struct comedi_subdevice *s,
4939 struct comedi_insn *insn,
4947 static void ni_rtsi_init(struct comedi_device *dev)
4949 struct ni_private *devpriv = dev->private;
4951 /* Initialises the RTSI bus signal switch to a default state */
4954 * Use 10MHz instead of 20MHz for RTSI clock frequency. Appears
4955 * to have no effect, at least on pxi-6281, which always uses
4956 * 20MHz rtsi clock frequency
4958 devpriv->clock_and_fout2 = NI_M_CLK_FOUT2_RTSI_10MHZ;
4959 /* Set clock mode to internal */
4960 if (ni_set_master_clock(dev, NI_MIO_INTERNAL_CLOCK, 0) < 0)
4961 dev_err(dev->class_dev, "ni_set_master_clock failed, bug?\n");
4962 /* default internal lines routing to RTSI bus lines */
4963 devpriv->rtsi_trig_a_output_reg =
4964 NISTC_RTSI_TRIG(0, NI_RTSI_OUTPUT_ADR_START1) |
4965 NISTC_RTSI_TRIG(1, NI_RTSI_OUTPUT_ADR_START2) |
4966 NISTC_RTSI_TRIG(2, NI_RTSI_OUTPUT_SCLKG) |
4967 NISTC_RTSI_TRIG(3, NI_RTSI_OUTPUT_DACUPDN);
4968 ni_stc_writew(dev, devpriv->rtsi_trig_a_output_reg,
4969 NISTC_RTSI_TRIGA_OUT_REG);
4970 devpriv->rtsi_trig_b_output_reg =
4971 NISTC_RTSI_TRIG(4, NI_RTSI_OUTPUT_DA_START1) |
4972 NISTC_RTSI_TRIG(5, NI_RTSI_OUTPUT_G_SRC0) |
4973 NISTC_RTSI_TRIG(6, NI_RTSI_OUTPUT_G_GATE0);
4974 if (devpriv->is_m_series)
4975 devpriv->rtsi_trig_b_output_reg |=
4976 NISTC_RTSI_TRIG(7, NI_RTSI_OUTPUT_RTSI_OSC);
4977 ni_stc_writew(dev, devpriv->rtsi_trig_b_output_reg,
4978 NISTC_RTSI_TRIGB_OUT_REG);
4981 * Sets the source and direction of the 4 on board lines
4982 * ni_stc_writew(dev, 0, NISTC_RTSI_BOARD_REG);
4987 static int ni_gpct_cmd(struct comedi_device *dev, struct comedi_subdevice *s)
4989 struct ni_gpct *counter = s->private;
4992 retval = ni_request_gpct_mite_channel(dev, counter->counter_index,
4995 dev_err(dev->class_dev,
4996 "no dma channel available for use by counter\n");
4999 ni_tio_acknowledge(counter);
5000 ni_e_series_enable_second_irq(dev, counter->counter_index, 1);
5002 return ni_tio_cmd(dev, s);
5005 static int ni_gpct_cancel(struct comedi_device *dev, struct comedi_subdevice *s)
5007 struct ni_gpct *counter = s->private;
5010 retval = ni_tio_cancel(counter);
5011 ni_e_series_enable_second_irq(dev, counter->counter_index, 0);
5012 ni_release_gpct_mite_channel(dev, counter->counter_index);
5017 static irqreturn_t ni_E_interrupt(int irq, void *d)
5019 struct comedi_device *dev = d;
5020 unsigned short a_status;
5021 unsigned short b_status;
5022 unsigned int ai_mite_status = 0;
5023 unsigned int ao_mite_status = 0;
5024 unsigned long flags;
5026 struct ni_private *devpriv = dev->private;
5027 struct mite_struct *mite = devpriv->mite;
5032 smp_mb(); /* make sure dev->attached is checked before handler does anything else. */
5034 /* lock to avoid race with comedi_poll */
5035 spin_lock_irqsave(&dev->spinlock, flags);
5036 a_status = ni_stc_readw(dev, NISTC_AI_STATUS1_REG);
5037 b_status = ni_stc_readw(dev, NISTC_AO_STATUS1_REG);
5040 struct ni_private *devpriv = dev->private;
5041 unsigned long flags_too;
5043 spin_lock_irqsave(&devpriv->mite_channel_lock, flags_too);
5044 if (devpriv->ai_mite_chan) {
5045 ai_mite_status = mite_get_status(devpriv->ai_mite_chan);
5046 if (ai_mite_status & CHSR_LINKC)
5048 devpriv->mite->mite_io_addr +
5050 ai_mite_chan->channel));
5052 if (devpriv->ao_mite_chan) {
5053 ao_mite_status = mite_get_status(devpriv->ao_mite_chan);
5054 if (ao_mite_status & CHSR_LINKC)
5056 mite->mite_io_addr +
5058 ao_mite_chan->channel));
5060 spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags_too);
5063 ack_a_interrupt(dev, a_status);
5064 ack_b_interrupt(dev, b_status);
5065 if ((a_status & NISTC_AI_STATUS1_INTA) || (ai_mite_status & CHSR_INT))
5066 handle_a_interrupt(dev, a_status, ai_mite_status);
5067 if ((b_status & NISTC_AO_STATUS1_INTB) || (ao_mite_status & CHSR_INT))
5068 handle_b_interrupt(dev, b_status, ao_mite_status);
5069 handle_gpct_interrupt(dev, 0);
5070 handle_gpct_interrupt(dev, 1);
5071 handle_cdio_interrupt(dev);
5073 spin_unlock_irqrestore(&dev->spinlock, flags);
5077 static int ni_alloc_private(struct comedi_device *dev)
5079 struct ni_private *devpriv;
5081 devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
5085 spin_lock_init(&devpriv->window_lock);
5086 spin_lock_init(&devpriv->soft_reg_copy_lock);
5087 spin_lock_init(&devpriv->mite_channel_lock);
5092 static int ni_E_init(struct comedi_device *dev,
5093 unsigned interrupt_pin, unsigned irq_polarity)
5095 const struct ni_board_struct *board = dev->board_ptr;
5096 struct ni_private *devpriv = dev->private;
5097 struct comedi_subdevice *s;
5101 if (board->n_aochan > MAX_N_AO_CHAN) {
5102 dev_err(dev->class_dev, "bug! n_aochan > MAX_N_AO_CHAN\n");
5106 /* initialize clock dividers */
5107 devpriv->clock_and_fout = NISTC_CLK_FOUT_SLOW_DIV2 |
5108 NISTC_CLK_FOUT_SLOW_TIMEBASE |
5109 NISTC_CLK_FOUT_TO_BOARD_DIV2 |
5110 NISTC_CLK_FOUT_TO_BOARD;
5111 if (!devpriv->is_6xxx) {
5112 /* BEAM is this needed for PCI-6143 ?? */
5113 devpriv->clock_and_fout |= (NISTC_CLK_FOUT_AI_OUT_DIV2 |
5114 NISTC_CLK_FOUT_AO_OUT_DIV2);
5116 ni_stc_writew(dev, devpriv->clock_and_fout, NISTC_CLK_FOUT_REG);
5118 ret = comedi_alloc_subdevices(dev, NI_NUM_SUBDEVICES);
5122 /* Analog Input subdevice */
5123 s = &dev->subdevices[NI_AI_SUBDEV];
5124 if (board->n_adchan) {
5125 s->type = COMEDI_SUBD_AI;
5126 s->subdev_flags = SDF_READABLE | SDF_DIFF | SDF_DITHER;
5127 if (!devpriv->is_611x)
5128 s->subdev_flags |= SDF_GROUND | SDF_COMMON | SDF_OTHER;
5129 if (board->ai_maxdata > 0xffff)
5130 s->subdev_flags |= SDF_LSAMPL;
5131 if (devpriv->is_m_series)
5132 s->subdev_flags |= SDF_SOFT_CALIBRATED;
5133 s->n_chan = board->n_adchan;
5134 s->maxdata = board->ai_maxdata;
5135 s->range_table = ni_range_lkup[board->gainlkup];
5136 s->insn_read = ni_ai_insn_read;
5137 s->insn_config = ni_ai_insn_config;
5139 dev->read_subdev = s;
5140 s->subdev_flags |= SDF_CMD_READ;
5141 s->len_chanlist = 512;
5142 s->do_cmdtest = ni_ai_cmdtest;
5143 s->do_cmd = ni_ai_cmd;
5144 s->cancel = ni_ai_reset;
5145 s->poll = ni_ai_poll;
5146 s->munge = ni_ai_munge;
5149 s->async_dma_dir = DMA_FROM_DEVICE;
5152 /* reset the analog input configuration */
5153 ni_ai_reset(dev, s);
5155 s->type = COMEDI_SUBD_UNUSED;
5158 /* Analog Output subdevice */
5159 s = &dev->subdevices[NI_AO_SUBDEV];
5160 if (board->n_aochan) {
5161 s->type = COMEDI_SUBD_AO;
5162 s->subdev_flags = SDF_WRITABLE | SDF_DEGLITCH | SDF_GROUND;
5163 if (devpriv->is_m_series)
5164 s->subdev_flags |= SDF_SOFT_CALIBRATED;
5165 s->n_chan = board->n_aochan;
5166 s->maxdata = board->ao_maxdata;
5167 s->range_table = board->ao_range_table;
5168 s->insn_config = ni_ao_insn_config;
5169 s->insn_write = ni_ao_insn_write;
5171 ret = comedi_alloc_subdev_readback(s);
5176 * Along with the IRQ we need either a FIFO or DMA for
5177 * async command support.
5179 if (dev->irq && (board->ao_fifo_depth || devpriv->mite)) {
5180 dev->write_subdev = s;
5181 s->subdev_flags |= SDF_CMD_WRITE;
5182 s->len_chanlist = s->n_chan;
5183 s->do_cmdtest = ni_ao_cmdtest;
5184 s->do_cmd = ni_ao_cmd;
5185 s->cancel = ni_ao_reset;
5186 if (!devpriv->is_m_series)
5187 s->munge = ni_ao_munge;
5190 s->async_dma_dir = DMA_TO_DEVICE;
5193 if (devpriv->is_67xx)
5194 init_ao_67xx(dev, s);
5196 /* reset the analog output configuration */
5197 ni_ao_reset(dev, s);
5199 s->type = COMEDI_SUBD_UNUSED;
5202 /* Digital I/O subdevice */
5203 s = &dev->subdevices[NI_DIO_SUBDEV];
5204 s->type = COMEDI_SUBD_DIO;
5205 s->subdev_flags = SDF_WRITABLE | SDF_READABLE;
5206 s->n_chan = board->has_32dio_chan ? 32 : 8;
5208 s->range_table = &range_digital;
5209 if (devpriv->is_m_series) {
5210 s->subdev_flags |= SDF_LSAMPL;
5211 s->insn_bits = ni_m_series_dio_insn_bits;
5212 s->insn_config = ni_m_series_dio_insn_config;
5214 s->subdev_flags |= SDF_CMD_WRITE /* | SDF_CMD_READ */;
5215 s->len_chanlist = s->n_chan;
5216 s->do_cmdtest = ni_cdio_cmdtest;
5217 s->do_cmd = ni_cdio_cmd;
5218 s->cancel = ni_cdio_cancel;
5220 /* M-series boards use DMA */
5221 s->async_dma_dir = DMA_BIDIRECTIONAL;
5224 /* reset DIO and set all channels to inputs */
5225 ni_writel(dev, NI_M_CDO_CMD_RESET |
5228 ni_writel(dev, s->io_bits, NI_M_DIO_DIR_REG);
5230 s->insn_bits = ni_dio_insn_bits;
5231 s->insn_config = ni_dio_insn_config;
5233 /* set all channels to inputs */
5234 devpriv->dio_control = NISTC_DIO_CTRL_DIR(s->io_bits);
5235 ni_writew(dev, devpriv->dio_control, NISTC_DIO_CTRL_REG);
5239 s = &dev->subdevices[NI_8255_DIO_SUBDEV];
5240 if (board->has_8255) {
5241 ret = subdev_8255_init(dev, s, ni_8255_callback,
5246 s->type = COMEDI_SUBD_UNUSED;
5249 /* formerly general purpose counter/timer device, but no longer used */
5250 s = &dev->subdevices[NI_UNUSED_SUBDEV];
5251 s->type = COMEDI_SUBD_UNUSED;
5253 /* Calibration subdevice */
5254 s = &dev->subdevices[NI_CALIBRATION_SUBDEV];
5255 s->type = COMEDI_SUBD_CALIB;
5256 s->subdev_flags = SDF_INTERNAL;
5259 if (devpriv->is_m_series) {
5260 /* internal PWM output used for AI nonlinearity calibration */
5261 s->insn_config = ni_m_series_pwm_config;
5263 ni_writel(dev, 0x0, NI_M_CAL_PWM_REG);
5264 } else if (devpriv->is_6143) {
5265 /* internal PWM output used for AI nonlinearity calibration */
5266 s->insn_config = ni_6143_pwm_config;
5268 s->subdev_flags |= SDF_WRITABLE;
5269 s->insn_read = ni_calib_insn_read;
5270 s->insn_write = ni_calib_insn_write;
5272 /* setup the caldacs and find the real n_chan and maxdata */
5273 caldac_setup(dev, s);
5276 /* EEPROM subdevice */
5277 s = &dev->subdevices[NI_EEPROM_SUBDEV];
5278 s->type = COMEDI_SUBD_MEMORY;
5279 s->subdev_flags = SDF_READABLE | SDF_INTERNAL;
5281 if (devpriv->is_m_series) {
5282 s->n_chan = M_SERIES_EEPROM_SIZE;
5283 s->insn_read = ni_m_series_eeprom_insn_read;
5286 s->insn_read = ni_eeprom_insn_read;
5289 /* Digital I/O (PFI) subdevice */
5290 s = &dev->subdevices[NI_PFI_DIO_SUBDEV];
5291 s->type = COMEDI_SUBD_DIO;
5292 s->subdev_flags = SDF_READABLE | SDF_WRITABLE | SDF_INTERNAL;
5294 if (devpriv->is_m_series) {
5296 s->insn_bits = ni_pfi_insn_bits;
5298 ni_writew(dev, s->state, NI_M_PFI_DO_REG);
5299 for (i = 0; i < NUM_PFI_OUTPUT_SELECT_REGS; ++i) {
5300 ni_writew(dev, devpriv->pfi_output_select_reg[i],
5301 NI_M_PFI_OUT_SEL_REG(i));
5306 s->insn_config = ni_pfi_insn_config;
5308 ni_set_bits(dev, NISTC_IO_BIDIR_PIN_REG, ~0, 0);
5310 /* cs5529 calibration adc */
5311 s = &dev->subdevices[NI_CS5529_CALIBRATION_SUBDEV];
5312 if (devpriv->is_67xx) {
5313 s->type = COMEDI_SUBD_AI;
5314 s->subdev_flags = SDF_READABLE | SDF_DIFF | SDF_INTERNAL;
5315 /* one channel for each analog output channel */
5316 s->n_chan = board->n_aochan;
5317 s->maxdata = (1 << 16) - 1;
5318 s->range_table = &range_unknown; /* XXX */
5319 s->insn_read = cs5529_ai_insn_read;
5320 s->insn_config = NULL;
5323 s->type = COMEDI_SUBD_UNUSED;
5327 s = &dev->subdevices[NI_SERIAL_SUBDEV];
5328 s->type = COMEDI_SUBD_SERIAL;
5329 s->subdev_flags = SDF_READABLE | SDF_WRITABLE | SDF_INTERNAL;
5332 s->insn_config = ni_serial_insn_config;
5333 devpriv->serial_interval_ns = 0;
5334 devpriv->serial_hw_mode = 0;
5337 s = &dev->subdevices[NI_RTSI_SUBDEV];
5338 s->type = COMEDI_SUBD_DIO;
5339 s->subdev_flags = SDF_READABLE | SDF_WRITABLE | SDF_INTERNAL;
5342 s->insn_bits = ni_rtsi_insn_bits;
5343 s->insn_config = ni_rtsi_insn_config;
5346 /* allocate and initialize the gpct counter device */
5347 devpriv->counter_dev = ni_gpct_device_construct(dev,
5348 ni_gpct_write_register,
5349 ni_gpct_read_register,
5350 (devpriv->is_m_series)
5351 ? ni_gpct_variant_m_series
5352 : ni_gpct_variant_e_series,
5354 if (!devpriv->counter_dev)
5357 /* Counter (gpct) subdevices */
5358 for (i = 0; i < NUM_GPCT; ++i) {
5359 struct ni_gpct *gpct = &devpriv->counter_dev->counters[i];
5361 /* setup and initialize the counter */
5362 gpct->chip_index = 0;
5363 gpct->counter_index = i;
5364 ni_tio_init_counter(gpct);
5366 s = &dev->subdevices[NI_GPCT_SUBDEV(i)];
5367 s->type = COMEDI_SUBD_COUNTER;
5368 s->subdev_flags = SDF_READABLE | SDF_WRITABLE | SDF_LSAMPL;
5370 s->maxdata = (devpriv->is_m_series) ? 0xffffffff
5372 s->insn_read = ni_tio_insn_read;
5373 s->insn_write = ni_tio_insn_read;
5374 s->insn_config = ni_tio_insn_config;
5376 if (dev->irq && devpriv->mite) {
5377 s->subdev_flags |= SDF_CMD_READ /* | SDF_CMD_WRITE */;
5378 s->len_chanlist = 1;
5379 s->do_cmdtest = ni_tio_cmdtest;
5380 s->do_cmd = ni_gpct_cmd;
5381 s->cancel = ni_gpct_cancel;
5383 s->async_dma_dir = DMA_BIDIRECTIONAL;
5389 /* Frequency output subdevice */
5390 s = &dev->subdevices[NI_FREQ_OUT_SUBDEV];
5391 s->type = COMEDI_SUBD_COUNTER;
5392 s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
5395 s->insn_read = ni_freq_out_insn_read;
5396 s->insn_write = ni_freq_out_insn_write;
5397 s->insn_config = ni_freq_out_insn_config;
5401 (irq_polarity ? NISTC_INT_CTRL_INT_POL : 0) |
5402 (NISTC_INT_CTRL_3PIN_INT & 0) |
5403 NISTC_INT_CTRL_INTA_ENA |
5404 NISTC_INT_CTRL_INTB_ENA |
5405 NISTC_INT_CTRL_INTA_SEL(interrupt_pin) |
5406 NISTC_INT_CTRL_INTB_SEL(interrupt_pin),
5407 NISTC_INT_CTRL_REG);
5411 ni_writeb(dev, devpriv->ai_ao_select_reg, NI_E_DMA_AI_AO_SEL_REG);
5412 ni_writeb(dev, devpriv->g0_g1_select_reg, NI_E_DMA_G0_G1_SEL_REG);
5414 if (devpriv->is_6xxx) {
5415 ni_writeb(dev, 0, NI611X_MAGIC_REG);
5416 } else if (devpriv->is_m_series) {
5419 for (channel = 0; channel < board->n_aochan; ++channel) {
5421 NI_M_AO_WAVEFORM_ORDER_REG(channel));
5423 NI_M_AO_REF_ATTENUATION_REG(channel));
5425 ni_writeb(dev, 0x0, NI_M_AO_CALIB_REG);
5431 static void mio_common_detach(struct comedi_device *dev)
5433 struct ni_private *devpriv = dev->private;
5436 if (devpriv->counter_dev)
5437 ni_gpct_device_destroy(devpriv->counter_dev);