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 (s->async->cmd.stop_src != TRIG_NONE &&
1504 b_status & NISTC_AO_STATUS1_BC_TC)
1505 s->async->events |= COMEDI_CB_EOA;
1508 if (b_status & NISTC_AO_STATUS1_FIFO_REQ) {
1511 ret = ni_ao_fifo_half_empty(dev, s);
1513 dev_err(dev->class_dev, "AO buffer underrun\n");
1514 ni_set_bits(dev, NISTC_INTB_ENA_REG,
1515 NISTC_INTB_ENA_AO_FIFO |
1516 NISTC_INTB_ENA_AO_ERR, 0);
1517 s->async->events |= COMEDI_CB_OVERFLOW;
1522 comedi_handle_events(dev, s);
1525 static void ni_ai_munge(struct comedi_device *dev, struct comedi_subdevice *s,
1526 void *data, unsigned int num_bytes,
1527 unsigned int chan_index)
1529 struct ni_private *devpriv = dev->private;
1530 struct comedi_async *async = s->async;
1531 struct comedi_cmd *cmd = &async->cmd;
1532 unsigned int nsamples = comedi_bytes_to_samples(s, num_bytes);
1533 unsigned short *array = data;
1534 unsigned int *larray = data;
1537 __le16 *barray = data;
1538 __le32 *blarray = data;
1541 for (i = 0; i < nsamples; i++) {
1543 if (s->subdev_flags & SDF_LSAMPL)
1544 larray[i] = le32_to_cpu(blarray[i]);
1546 array[i] = le16_to_cpu(barray[i]);
1548 if (s->subdev_flags & SDF_LSAMPL)
1549 larray[i] += devpriv->ai_offset[chan_index];
1551 array[i] += devpriv->ai_offset[chan_index];
1553 chan_index %= cmd->chanlist_len;
1559 static int ni_ai_setup_MITE_dma(struct comedi_device *dev)
1561 struct ni_private *devpriv = dev->private;
1562 struct comedi_subdevice *s = dev->read_subdev;
1564 unsigned long flags;
1566 retval = ni_request_ai_mite_channel(dev);
1570 /* write alloc the entire buffer */
1571 comedi_buf_write_alloc(s, s->async->prealloc_bufsz);
1573 spin_lock_irqsave(&devpriv->mite_channel_lock, flags);
1574 if (!devpriv->ai_mite_chan) {
1575 spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
1579 if (devpriv->is_611x || devpriv->is_6143)
1580 mite_prep_dma(devpriv->ai_mite_chan, 32, 16);
1581 else if (devpriv->is_628x)
1582 mite_prep_dma(devpriv->ai_mite_chan, 32, 32);
1584 mite_prep_dma(devpriv->ai_mite_chan, 16, 16);
1587 mite_dma_arm(devpriv->ai_mite_chan);
1588 spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
1593 static int ni_ao_setup_MITE_dma(struct comedi_device *dev)
1595 struct ni_private *devpriv = dev->private;
1596 struct comedi_subdevice *s = dev->write_subdev;
1598 unsigned long flags;
1600 retval = ni_request_ao_mite_channel(dev);
1604 /* read alloc the entire buffer */
1605 comedi_buf_read_alloc(s, s->async->prealloc_bufsz);
1607 spin_lock_irqsave(&devpriv->mite_channel_lock, flags);
1608 if (devpriv->ao_mite_chan) {
1609 if (devpriv->is_611x || devpriv->is_6713) {
1610 mite_prep_dma(devpriv->ao_mite_chan, 32, 32);
1612 /* doing 32 instead of 16 bit wide transfers from memory
1613 makes the mite do 32 bit pci transfers, doubling pci bandwidth. */
1614 mite_prep_dma(devpriv->ao_mite_chan, 16, 32);
1616 mite_dma_arm(devpriv->ao_mite_chan);
1620 spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
1628 used for both cancel ioctl and board initialization
1630 this is pretty harsh for a cancel, but it works...
1633 static int ni_ai_reset(struct comedi_device *dev, struct comedi_subdevice *s)
1635 struct ni_private *devpriv = dev->private;
1636 unsigned ai_personal;
1637 unsigned ai_out_ctrl;
1639 ni_release_ai_mite_channel(dev);
1640 /* ai configuration */
1641 ni_stc_writew(dev, NISTC_RESET_AI_CFG_START | NISTC_RESET_AI,
1644 ni_set_bits(dev, NISTC_INTA_ENA_REG, NISTC_INTA_ENA_AI_MASK, 0);
1646 ni_clear_ai_fifo(dev);
1648 if (!devpriv->is_6143)
1649 ni_writeb(dev, NI_E_MISC_CMD_EXT_ATRIG, NI_E_MISC_CMD_REG);
1651 ni_stc_writew(dev, NISTC_AI_CMD1_DISARM, NISTC_AI_CMD1_REG);
1652 ni_stc_writew(dev, NISTC_AI_MODE1_START_STOP |
1654 /*| NISTC_AI_MODE1_TRIGGER_ONCE */,
1655 NISTC_AI_MODE1_REG);
1656 ni_stc_writew(dev, 0, NISTC_AI_MODE2_REG);
1657 /* generate FIFO interrupts on non-empty */
1658 ni_stc_writew(dev, NISTC_AI_MODE3_FIFO_MODE_NE,
1659 NISTC_AI_MODE3_REG);
1661 ai_personal = NISTC_AI_PERSONAL_SHIFTIN_PW |
1662 NISTC_AI_PERSONAL_SOC_POLARITY |
1663 NISTC_AI_PERSONAL_LOCALMUX_CLK_PW;
1664 ai_out_ctrl = NISTC_AI_OUT_CTRL_SCAN_IN_PROG_SEL(3) |
1665 NISTC_AI_OUT_CTRL_EXTMUX_CLK_SEL(0) |
1666 NISTC_AI_OUT_CTRL_LOCALMUX_CLK_SEL(2) |
1667 NISTC_AI_OUT_CTRL_SC_TC_SEL(3);
1668 if (devpriv->is_611x) {
1669 ai_out_ctrl |= NISTC_AI_OUT_CTRL_CONVERT_HIGH;
1670 } else if (devpriv->is_6143) {
1671 ai_out_ctrl |= NISTC_AI_OUT_CTRL_CONVERT_LOW;
1673 ai_personal |= NISTC_AI_PERSONAL_CONVERT_PW;
1674 if (devpriv->is_622x)
1675 ai_out_ctrl |= NISTC_AI_OUT_CTRL_CONVERT_HIGH;
1677 ai_out_ctrl |= NISTC_AI_OUT_CTRL_CONVERT_LOW;
1679 ni_stc_writew(dev, ai_personal, NISTC_AI_PERSONAL_REG);
1680 ni_stc_writew(dev, ai_out_ctrl, NISTC_AI_OUT_CTRL_REG);
1682 /* the following registers should not be changed, because there
1683 * are no backup registers in devpriv. If you want to change
1684 * any of these, add a backup register and other appropriate code:
1685 * NISTC_AI_MODE1_REG
1686 * NISTC_AI_MODE3_REG
1687 * NISTC_AI_PERSONAL_REG
1688 * NISTC_AI_OUT_CTRL_REG
1691 /* clear interrupts */
1692 ni_stc_writew(dev, NISTC_INTA_ACK_AI_ALL, NISTC_INTA_ACK_REG);
1694 ni_stc_writew(dev, NISTC_RESET_AI_CFG_END, NISTC_RESET_REG);
1699 static int ni_ai_poll(struct comedi_device *dev, struct comedi_subdevice *s)
1701 unsigned long flags;
1704 /* lock to avoid race with interrupt handler */
1705 spin_lock_irqsave(&dev->spinlock, flags);
1707 ni_handle_fifo_dregs(dev);
1709 ni_sync_ai_dma(dev);
1711 count = comedi_buf_n_bytes_ready(s);
1712 spin_unlock_irqrestore(&dev->spinlock, flags);
1717 static void ni_prime_channelgain_list(struct comedi_device *dev)
1721 ni_stc_writew(dev, NISTC_AI_CMD1_CONVERT_PULSE, NISTC_AI_CMD1_REG);
1722 for (i = 0; i < NI_TIMEOUT; ++i) {
1723 if (!(ni_stc_readw(dev, NISTC_AI_STATUS1_REG) &
1724 NISTC_AI_STATUS1_FIFO_E)) {
1725 ni_stc_writew(dev, 1, NISTC_ADC_FIFO_CLR_REG);
1730 dev_err(dev->class_dev, "timeout loading channel/gain list\n");
1733 static void ni_m_series_load_channelgain_list(struct comedi_device *dev,
1734 unsigned int n_chan,
1737 const struct ni_board_struct *board = dev->board_ptr;
1738 struct ni_private *devpriv = dev->private;
1739 unsigned int chan, range, aref;
1741 unsigned int dither;
1742 unsigned range_code;
1744 ni_stc_writew(dev, 1, NISTC_CFG_MEM_CLR_REG);
1746 if ((list[0] & CR_ALT_SOURCE)) {
1747 unsigned bypass_bits;
1749 chan = CR_CHAN(list[0]);
1750 range = CR_RANGE(list[0]);
1751 range_code = ni_gainlkup[board->gainlkup][range];
1752 dither = (list[0] & CR_ALT_FILTER) != 0;
1753 bypass_bits = NI_M_CFG_BYPASS_FIFO |
1754 NI_M_CFG_BYPASS_AI_CHAN(chan) |
1755 NI_M_CFG_BYPASS_AI_GAIN(range_code) |
1756 devpriv->ai_calib_source;
1758 bypass_bits |= NI_M_CFG_BYPASS_AI_DITHER;
1759 /* don't use 2's complement encoding */
1760 bypass_bits |= NI_M_CFG_BYPASS_AI_POLARITY;
1761 ni_writel(dev, bypass_bits, NI_M_CFG_BYPASS_FIFO_REG);
1763 ni_writel(dev, 0, NI_M_CFG_BYPASS_FIFO_REG);
1765 for (i = 0; i < n_chan; i++) {
1766 unsigned config_bits = 0;
1768 chan = CR_CHAN(list[i]);
1769 aref = CR_AREF(list[i]);
1770 range = CR_RANGE(list[i]);
1771 dither = (list[i] & CR_ALT_FILTER) != 0;
1773 range_code = ni_gainlkup[board->gainlkup][range];
1774 devpriv->ai_offset[i] = 0;
1777 config_bits |= NI_M_AI_CFG_CHAN_TYPE_DIFF;
1780 config_bits |= NI_M_AI_CFG_CHAN_TYPE_COMMON;
1783 config_bits |= NI_M_AI_CFG_CHAN_TYPE_GROUND;
1788 config_bits |= NI_M_AI_CFG_CHAN_SEL(chan);
1789 config_bits |= NI_M_AI_CFG_BANK_SEL(chan);
1790 config_bits |= NI_M_AI_CFG_GAIN(range_code);
1791 if (i == n_chan - 1)
1792 config_bits |= NI_M_AI_CFG_LAST_CHAN;
1794 config_bits |= NI_M_AI_CFG_DITHER;
1795 /* don't use 2's complement encoding */
1796 config_bits |= NI_M_AI_CFG_POLARITY;
1797 ni_writew(dev, config_bits, NI_M_AI_CFG_FIFO_DATA_REG);
1799 ni_prime_channelgain_list(dev);
1803 * Notes on the 6110 and 6111:
1804 * These boards a slightly different than the rest of the series, since
1805 * they have multiple A/D converters.
1806 * From the driver side, the configuration memory is a
1808 * Configuration Memory Low:
1810 * bit 8: unipolar/bipolar (should be 0 for bipolar)
1811 * bits 0-3: gain. This is 4 bits instead of 3 for the other boards
1812 * 1001 gain=0.1 (+/- 50)
1821 * Configuration Memory High:
1822 * bits 12-14: Channel Type
1823 * 001 for differential
1824 * 000 for calibration
1825 * bit 11: coupling (this is not currently handled)
1829 * valid channels are 0-3
1831 static void ni_load_channelgain_list(struct comedi_device *dev,
1832 struct comedi_subdevice *s,
1833 unsigned int n_chan, unsigned int *list)
1835 const struct ni_board_struct *board = dev->board_ptr;
1836 struct ni_private *devpriv = dev->private;
1837 unsigned int offset = (s->maxdata + 1) >> 1;
1838 unsigned int chan, range, aref;
1840 unsigned int hi, lo;
1841 unsigned int dither;
1843 if (devpriv->is_m_series) {
1844 ni_m_series_load_channelgain_list(dev, n_chan, list);
1847 if (n_chan == 1 && !devpriv->is_611x && !devpriv->is_6143) {
1848 if (devpriv->changain_state
1849 && devpriv->changain_spec == list[0]) {
1853 devpriv->changain_state = 1;
1854 devpriv->changain_spec = list[0];
1856 devpriv->changain_state = 0;
1859 ni_stc_writew(dev, 1, NISTC_CFG_MEM_CLR_REG);
1861 /* Set up Calibration mode if required */
1862 if (devpriv->is_6143) {
1863 if ((list[0] & CR_ALT_SOURCE)
1864 && !devpriv->ai_calib_source_enabled) {
1865 /* Strobe Relay enable bit */
1866 ni_writew(dev, devpriv->ai_calib_source |
1867 NI6143_CALIB_CHAN_RELAY_ON,
1868 NI6143_CALIB_CHAN_REG);
1869 ni_writew(dev, devpriv->ai_calib_source,
1870 NI6143_CALIB_CHAN_REG);
1871 devpriv->ai_calib_source_enabled = 1;
1872 msleep_interruptible(100); /* Allow relays to change */
1873 } else if (!(list[0] & CR_ALT_SOURCE)
1874 && devpriv->ai_calib_source_enabled) {
1875 /* Strobe Relay disable bit */
1876 ni_writew(dev, devpriv->ai_calib_source |
1877 NI6143_CALIB_CHAN_RELAY_OFF,
1878 NI6143_CALIB_CHAN_REG);
1879 ni_writew(dev, devpriv->ai_calib_source,
1880 NI6143_CALIB_CHAN_REG);
1881 devpriv->ai_calib_source_enabled = 0;
1882 msleep_interruptible(100); /* Allow relays to change */
1886 for (i = 0; i < n_chan; i++) {
1887 if (!devpriv->is_6143 && (list[i] & CR_ALT_SOURCE))
1888 chan = devpriv->ai_calib_source;
1890 chan = CR_CHAN(list[i]);
1891 aref = CR_AREF(list[i]);
1892 range = CR_RANGE(list[i]);
1893 dither = (list[i] & CR_ALT_FILTER) != 0;
1895 /* fix the external/internal range differences */
1896 range = ni_gainlkup[board->gainlkup][range];
1897 if (devpriv->is_611x)
1898 devpriv->ai_offset[i] = offset;
1900 devpriv->ai_offset[i] = (range & 0x100) ? 0 : offset;
1903 if ((list[i] & CR_ALT_SOURCE)) {
1904 if (devpriv->is_611x)
1905 ni_writew(dev, CR_CHAN(list[i]) & 0x0003,
1906 NI611X_CALIB_CHAN_SEL_REG);
1908 if (devpriv->is_611x)
1910 else if (devpriv->is_6143)
1914 hi |= NI_E_AI_CFG_HI_TYPE_DIFF;
1917 hi |= NI_E_AI_CFG_HI_TYPE_COMMON;
1920 hi |= NI_E_AI_CFG_HI_TYPE_GROUND;
1926 hi |= NI_E_AI_CFG_HI_CHAN(chan);
1928 ni_writew(dev, hi, NI_E_AI_CFG_HI_REG);
1930 if (!devpriv->is_6143) {
1931 lo = NI_E_AI_CFG_LO_GAIN(range);
1933 if (i == n_chan - 1)
1934 lo |= NI_E_AI_CFG_LO_LAST_CHAN;
1936 lo |= NI_E_AI_CFG_LO_DITHER;
1938 ni_writew(dev, lo, NI_E_AI_CFG_LO_REG);
1942 /* prime the channel/gain list */
1943 if (!devpriv->is_611x && !devpriv->is_6143)
1944 ni_prime_channelgain_list(dev);
1947 static int ni_ai_insn_read(struct comedi_device *dev,
1948 struct comedi_subdevice *s,
1949 struct comedi_insn *insn,
1952 struct ni_private *devpriv = dev->private;
1953 unsigned int mask = (s->maxdata + 1) >> 1;
1959 ni_load_channelgain_list(dev, s, 1, &insn->chanspec);
1961 ni_clear_ai_fifo(dev);
1963 signbits = devpriv->ai_offset[0];
1964 if (devpriv->is_611x) {
1965 for (n = 0; n < num_adc_stages_611x; n++) {
1966 ni_stc_writew(dev, NISTC_AI_CMD1_CONVERT_PULSE,
1970 for (n = 0; n < insn->n; n++) {
1971 ni_stc_writew(dev, NISTC_AI_CMD1_CONVERT_PULSE,
1973 /* The 611x has screwy 32-bit FIFOs. */
1975 for (i = 0; i < NI_TIMEOUT; i++) {
1976 if (ni_readb(dev, NI_E_STATUS_REG) & 0x80) {
1978 NI611X_AI_FIFO_DATA_REG);
1983 if (!(ni_stc_readw(dev, NISTC_AI_STATUS1_REG) &
1984 NISTC_AI_STATUS1_FIFO_E)) {
1986 NI611X_AI_FIFO_DATA_REG);
1991 if (i == NI_TIMEOUT) {
1992 dev_err(dev->class_dev, "timeout\n");
1998 } else if (devpriv->is_6143) {
1999 for (n = 0; n < insn->n; n++) {
2000 ni_stc_writew(dev, NISTC_AI_CMD1_CONVERT_PULSE,
2003 /* The 6143 has 32-bit FIFOs. You need to strobe a bit to move a single 16bit stranded sample into the FIFO */
2005 for (i = 0; i < NI_TIMEOUT; i++) {
2006 if (ni_readl(dev, NI6143_AI_FIFO_STATUS_REG) &
2008 /* Get stranded sample into FIFO */
2009 ni_writel(dev, 0x01,
2010 NI6143_AI_FIFO_CTRL_REG);
2012 NI6143_AI_FIFO_DATA_REG);
2016 if (i == NI_TIMEOUT) {
2017 dev_err(dev->class_dev, "timeout\n");
2020 data[n] = (((dl >> 16) & 0xFFFF) + signbits) & 0xFFFF;
2023 for (n = 0; n < insn->n; n++) {
2024 ni_stc_writew(dev, NISTC_AI_CMD1_CONVERT_PULSE,
2026 for (i = 0; i < NI_TIMEOUT; i++) {
2027 if (!(ni_stc_readw(dev, NISTC_AI_STATUS1_REG) &
2028 NISTC_AI_STATUS1_FIFO_E))
2031 if (i == NI_TIMEOUT) {
2032 dev_err(dev->class_dev, "timeout\n");
2035 if (devpriv->is_m_series) {
2036 dl = ni_readl(dev, NI_M_AI_FIFO_DATA_REG);
2040 d = ni_readw(dev, NI_E_AI_FIFO_DATA_REG);
2041 d += signbits; /* subtle: needs to be short addition */
2049 static int ni_ns_to_timer(const struct comedi_device *dev, unsigned nanosec,
2052 struct ni_private *devpriv = dev->private;
2055 switch (flags & CMDF_ROUND_MASK) {
2056 case CMDF_ROUND_NEAREST:
2058 divider = (nanosec + devpriv->clock_ns / 2) / devpriv->clock_ns;
2060 case CMDF_ROUND_DOWN:
2061 divider = (nanosec) / devpriv->clock_ns;
2064 divider = (nanosec + devpriv->clock_ns - 1) / devpriv->clock_ns;
2070 static unsigned ni_timer_to_ns(const struct comedi_device *dev, int timer)
2072 struct ni_private *devpriv = dev->private;
2074 return devpriv->clock_ns * (timer + 1);
2077 static void ni_cmd_set_mite_transfer(struct mite_dma_descriptor_ring *ring,
2078 struct comedi_subdevice *sdev,
2079 const struct comedi_cmd *cmd,
2080 unsigned int max_count) {
2082 unsigned int nbytes = max_count;
2084 if (cmd->stop_arg > 0 && cmd->stop_arg < max_count)
2085 nbytes = cmd->stop_arg;
2086 nbytes *= comedi_bytes_per_scan(sdev);
2088 if (nbytes > sdev->async->prealloc_bufsz) {
2089 if (cmd->stop_arg > 0)
2090 dev_err(sdev->device->class_dev,
2091 "ni_cmd_set_mite_transfer: tried exact data transfer limits greater than buffer size\n");
2094 * we can only transfer up to the size of the buffer. In this
2095 * case, the user is expected to continue to write into the
2096 * comedi buffer (already implemented as a ring buffer).
2098 nbytes = sdev->async->prealloc_bufsz;
2101 mite_init_ring_descriptors(ring, sdev, nbytes);
2103 dev_err(sdev->device->class_dev,
2104 "ni_cmd_set_mite_transfer: exact data transfer limits not implemented yet without DMA\n");
2108 static unsigned ni_min_ai_scan_period_ns(struct comedi_device *dev,
2109 unsigned num_channels)
2111 const struct ni_board_struct *board = dev->board_ptr;
2112 struct ni_private *devpriv = dev->private;
2114 /* simultaneously-sampled inputs */
2115 if (devpriv->is_611x || devpriv->is_6143)
2116 return board->ai_speed;
2118 /* multiplexed inputs */
2119 return board->ai_speed * num_channels;
2122 static int ni_ai_cmdtest(struct comedi_device *dev, struct comedi_subdevice *s,
2123 struct comedi_cmd *cmd)
2125 const struct ni_board_struct *board = dev->board_ptr;
2126 struct ni_private *devpriv = dev->private;
2129 unsigned int sources;
2131 /* Step 1 : check if triggers are trivially valid */
2133 err |= comedi_check_trigger_src(&cmd->start_src,
2134 TRIG_NOW | TRIG_INT | TRIG_EXT);
2135 err |= comedi_check_trigger_src(&cmd->scan_begin_src,
2136 TRIG_TIMER | TRIG_EXT);
2138 sources = TRIG_TIMER | TRIG_EXT;
2139 if (devpriv->is_611x || devpriv->is_6143)
2140 sources |= TRIG_NOW;
2141 err |= comedi_check_trigger_src(&cmd->convert_src, sources);
2143 err |= comedi_check_trigger_src(&cmd->scan_end_src, TRIG_COUNT);
2144 err |= comedi_check_trigger_src(&cmd->stop_src, TRIG_COUNT | TRIG_NONE);
2149 /* Step 2a : make sure trigger sources are unique */
2151 err |= comedi_check_trigger_is_unique(cmd->start_src);
2152 err |= comedi_check_trigger_is_unique(cmd->scan_begin_src);
2153 err |= comedi_check_trigger_is_unique(cmd->convert_src);
2154 err |= comedi_check_trigger_is_unique(cmd->stop_src);
2156 /* Step 2b : and mutually compatible */
2161 /* Step 3: check if arguments are trivially valid */
2163 switch (cmd->start_src) {
2166 err |= comedi_check_trigger_arg_is(&cmd->start_arg, 0);
2169 tmp = CR_CHAN(cmd->start_arg);
2173 tmp |= (cmd->start_arg & (CR_INVERT | CR_EDGE));
2174 err |= comedi_check_trigger_arg_is(&cmd->start_arg, tmp);
2178 if (cmd->scan_begin_src == TRIG_TIMER) {
2179 err |= comedi_check_trigger_arg_min(&cmd->scan_begin_arg,
2180 ni_min_ai_scan_period_ns(dev, cmd->chanlist_len));
2181 err |= comedi_check_trigger_arg_max(&cmd->scan_begin_arg,
2184 } else if (cmd->scan_begin_src == TRIG_EXT) {
2185 /* external trigger */
2186 unsigned int tmp = CR_CHAN(cmd->scan_begin_arg);
2190 tmp |= (cmd->scan_begin_arg & (CR_INVERT | CR_EDGE));
2191 err |= comedi_check_trigger_arg_is(&cmd->scan_begin_arg, tmp);
2192 } else { /* TRIG_OTHER */
2193 err |= comedi_check_trigger_arg_is(&cmd->scan_begin_arg, 0);
2196 if (cmd->convert_src == TRIG_TIMER) {
2197 if (devpriv->is_611x || devpriv->is_6143) {
2198 err |= comedi_check_trigger_arg_is(&cmd->convert_arg,
2201 err |= comedi_check_trigger_arg_min(&cmd->convert_arg,
2203 err |= comedi_check_trigger_arg_max(&cmd->convert_arg,
2207 } else if (cmd->convert_src == TRIG_EXT) {
2208 /* external trigger */
2209 unsigned int tmp = CR_CHAN(cmd->convert_arg);
2213 tmp |= (cmd->convert_arg & (CR_ALT_FILTER | CR_INVERT));
2214 err |= comedi_check_trigger_arg_is(&cmd->convert_arg, tmp);
2215 } else if (cmd->convert_src == TRIG_NOW) {
2216 err |= comedi_check_trigger_arg_is(&cmd->convert_arg, 0);
2219 err |= comedi_check_trigger_arg_is(&cmd->scan_end_arg,
2222 if (cmd->stop_src == TRIG_COUNT) {
2223 unsigned int max_count = 0x01000000;
2225 if (devpriv->is_611x)
2226 max_count -= num_adc_stages_611x;
2227 err |= comedi_check_trigger_arg_max(&cmd->stop_arg, max_count);
2228 err |= comedi_check_trigger_arg_min(&cmd->stop_arg, 1);
2231 err |= comedi_check_trigger_arg_is(&cmd->stop_arg, 0);
2237 /* step 4: fix up any arguments */
2239 if (cmd->scan_begin_src == TRIG_TIMER) {
2240 tmp = cmd->scan_begin_arg;
2241 cmd->scan_begin_arg =
2242 ni_timer_to_ns(dev, ni_ns_to_timer(dev,
2243 cmd->scan_begin_arg,
2245 if (tmp != cmd->scan_begin_arg)
2248 if (cmd->convert_src == TRIG_TIMER) {
2249 if (!devpriv->is_611x && !devpriv->is_6143) {
2250 tmp = cmd->convert_arg;
2252 ni_timer_to_ns(dev, ni_ns_to_timer(dev,
2255 if (tmp != cmd->convert_arg)
2257 if (cmd->scan_begin_src == TRIG_TIMER &&
2258 cmd->scan_begin_arg <
2259 cmd->convert_arg * cmd->scan_end_arg) {
2260 cmd->scan_begin_arg =
2261 cmd->convert_arg * cmd->scan_end_arg;
2273 static int ni_ai_inttrig(struct comedi_device *dev,
2274 struct comedi_subdevice *s,
2275 unsigned int trig_num)
2277 struct ni_private *devpriv = dev->private;
2278 struct comedi_cmd *cmd = &s->async->cmd;
2280 if (trig_num != cmd->start_arg)
2283 ni_stc_writew(dev, NISTC_AI_CMD2_START1_PULSE | devpriv->ai_cmd2,
2285 s->async->inttrig = NULL;
2290 static int ni_ai_cmd(struct comedi_device *dev, struct comedi_subdevice *s)
2292 struct ni_private *devpriv = dev->private;
2293 const struct comedi_cmd *cmd = &s->async->cmd;
2295 int mode1 = 0; /* mode1 is needed for both stop and convert */
2297 int start_stop_select = 0;
2298 unsigned int stop_count;
2299 int interrupt_a_enable = 0;
2302 if (dev->irq == 0) {
2303 dev_err(dev->class_dev, "cannot run command without an irq\n");
2306 ni_clear_ai_fifo(dev);
2308 ni_load_channelgain_list(dev, s, cmd->chanlist_len, cmd->chanlist);
2310 /* start configuration */
2311 ni_stc_writew(dev, NISTC_RESET_AI_CFG_START, NISTC_RESET_REG);
2313 /* disable analog triggering for now, since it
2314 * interferes with the use of pfi0 */
2315 devpriv->an_trig_etc_reg &= ~NISTC_ATRIG_ETC_ENA;
2316 ni_stc_writew(dev, devpriv->an_trig_etc_reg, NISTC_ATRIG_ETC_REG);
2318 ai_trig = NISTC_AI_TRIG_START2_SEL(0) | NISTC_AI_TRIG_START1_SYNC;
2319 switch (cmd->start_src) {
2322 ai_trig |= NISTC_AI_TRIG_START1_EDGE |
2323 NISTC_AI_TRIG_START1_SEL(0);
2326 ai_trig |= NISTC_AI_TRIG_START1_SEL(CR_CHAN(cmd->start_arg) +
2329 if (cmd->start_arg & CR_INVERT)
2330 ai_trig |= NISTC_AI_TRIG_START1_POLARITY;
2331 if (cmd->start_arg & CR_EDGE)
2332 ai_trig |= NISTC_AI_TRIG_START1_EDGE;
2335 ni_stc_writew(dev, ai_trig, NISTC_AI_TRIG_SEL_REG);
2337 mode2 &= ~NISTC_AI_MODE2_PRE_TRIGGER;
2338 mode2 &= ~NISTC_AI_MODE2_SC_INIT_LOAD_SRC;
2339 mode2 &= ~NISTC_AI_MODE2_SC_RELOAD_MODE;
2340 ni_stc_writew(dev, mode2, NISTC_AI_MODE2_REG);
2342 if (cmd->chanlist_len == 1 || devpriv->is_611x || devpriv->is_6143) {
2344 start_stop_select |= NISTC_AI_STOP_POLARITY |
2345 NISTC_AI_STOP_SEL(31) |
2348 /* ai configuration memory */
2349 start_stop_select |= NISTC_AI_STOP_SEL(19);
2351 ni_stc_writew(dev, start_stop_select, NISTC_AI_START_STOP_REG);
2353 devpriv->ai_cmd2 = 0;
2354 switch (cmd->stop_src) {
2356 stop_count = cmd->stop_arg - 1;
2358 if (devpriv->is_611x) {
2359 /* have to take 3 stage adc pipeline into account */
2360 stop_count += num_adc_stages_611x;
2362 /* stage number of scans */
2363 ni_stc_writel(dev, stop_count, NISTC_AI_SC_LOADA_REG);
2365 mode1 |= NISTC_AI_MODE1_START_STOP |
2366 NISTC_AI_MODE1_RSVD |
2367 NISTC_AI_MODE1_TRIGGER_ONCE;
2368 ni_stc_writew(dev, mode1, NISTC_AI_MODE1_REG);
2369 /* load SC (Scan Count) */
2370 ni_stc_writew(dev, NISTC_AI_CMD1_SC_LOAD, NISTC_AI_CMD1_REG);
2372 if (stop_count == 0) {
2373 devpriv->ai_cmd2 |= NISTC_AI_CMD2_END_ON_EOS;
2374 interrupt_a_enable |= NISTC_INTA_ENA_AI_STOP;
2375 /* this is required to get the last sample for chanlist_len > 1, not sure why */
2376 if (cmd->chanlist_len > 1)
2377 start_stop_select |= NISTC_AI_STOP_POLARITY |
2382 /* stage number of scans */
2383 ni_stc_writel(dev, 0, NISTC_AI_SC_LOADA_REG);
2385 mode1 |= NISTC_AI_MODE1_START_STOP |
2386 NISTC_AI_MODE1_RSVD |
2387 NISTC_AI_MODE1_CONTINUOUS;
2388 ni_stc_writew(dev, mode1, NISTC_AI_MODE1_REG);
2390 /* load SC (Scan Count) */
2391 ni_stc_writew(dev, NISTC_AI_CMD1_SC_LOAD, NISTC_AI_CMD1_REG);
2395 switch (cmd->scan_begin_src) {
2398 * stop bits for non 611x boards
2399 * NISTC_AI_MODE3_SI_TRIG_DELAY=0
2400 * NISTC_AI_MODE2_PRE_TRIGGER=0
2401 * NISTC_AI_START_STOP_REG:
2402 * NISTC_AI_START_POLARITY=0 (?) rising edge
2403 * NISTC_AI_START_EDGE=1 edge triggered
2404 * NISTC_AI_START_SYNC=1 (?)
2405 * NISTC_AI_START_SEL=0 SI_TC
2406 * NISTC_AI_STOP_POLARITY=0 rising edge
2407 * NISTC_AI_STOP_EDGE=0 level
2408 * NISTC_AI_STOP_SYNC=1
2409 * NISTC_AI_STOP_SEL=19 external pin (configuration mem)
2411 start_stop_select |= NISTC_AI_START_EDGE | NISTC_AI_START_SYNC;
2412 ni_stc_writew(dev, start_stop_select, NISTC_AI_START_STOP_REG);
2414 mode2 &= ~NISTC_AI_MODE2_SI_INIT_LOAD_SRC; /* A */
2415 mode2 |= NISTC_AI_MODE2_SI_RELOAD_MODE(0);
2416 /* mode2 |= NISTC_AI_MODE2_SC_RELOAD_MODE; */
2417 ni_stc_writew(dev, mode2, NISTC_AI_MODE2_REG);
2420 timer = ni_ns_to_timer(dev, cmd->scan_begin_arg,
2421 CMDF_ROUND_NEAREST);
2422 ni_stc_writel(dev, timer, NISTC_AI_SI_LOADA_REG);
2423 ni_stc_writew(dev, NISTC_AI_CMD1_SI_LOAD, NISTC_AI_CMD1_REG);
2426 if (cmd->scan_begin_arg & CR_EDGE)
2427 start_stop_select |= NISTC_AI_START_EDGE;
2428 if (cmd->scan_begin_arg & CR_INVERT) /* falling edge */
2429 start_stop_select |= NISTC_AI_START_POLARITY;
2430 if (cmd->scan_begin_src != cmd->convert_src ||
2431 (cmd->scan_begin_arg & ~CR_EDGE) !=
2432 (cmd->convert_arg & ~CR_EDGE))
2433 start_stop_select |= NISTC_AI_START_SYNC;
2434 start_stop_select |=
2435 NISTC_AI_START_SEL(1 + CR_CHAN(cmd->scan_begin_arg));
2436 ni_stc_writew(dev, start_stop_select, NISTC_AI_START_STOP_REG);
2440 switch (cmd->convert_src) {
2443 if (cmd->convert_arg == 0 || cmd->convert_src == TRIG_NOW)
2446 timer = ni_ns_to_timer(dev, cmd->convert_arg,
2447 CMDF_ROUND_NEAREST);
2448 /* 0,0 does not work */
2449 ni_stc_writew(dev, 1, NISTC_AI_SI2_LOADA_REG);
2450 ni_stc_writew(dev, timer, NISTC_AI_SI2_LOADB_REG);
2452 mode2 &= ~NISTC_AI_MODE2_SI2_INIT_LOAD_SRC; /* A */
2453 mode2 |= NISTC_AI_MODE2_SI2_RELOAD_MODE; /* alternate */
2454 ni_stc_writew(dev, mode2, NISTC_AI_MODE2_REG);
2456 ni_stc_writew(dev, NISTC_AI_CMD1_SI2_LOAD, NISTC_AI_CMD1_REG);
2458 mode2 |= NISTC_AI_MODE2_SI2_INIT_LOAD_SRC; /* B */
2459 mode2 |= NISTC_AI_MODE2_SI2_RELOAD_MODE; /* alternate */
2460 ni_stc_writew(dev, mode2, NISTC_AI_MODE2_REG);
2463 mode1 |= NISTC_AI_MODE1_CONVERT_SRC(1 +
2464 CR_CHAN(cmd->convert_arg));
2465 if ((cmd->convert_arg & CR_INVERT) == 0)
2466 mode1 |= NISTC_AI_MODE1_CONVERT_POLARITY;
2467 ni_stc_writew(dev, mode1, NISTC_AI_MODE1_REG);
2469 mode2 |= NISTC_AI_MODE2_SC_GATE_ENA |
2470 NISTC_AI_MODE2_START_STOP_GATE_ENA;
2471 ni_stc_writew(dev, mode2, NISTC_AI_MODE2_REG);
2477 /* interrupt on FIFO, errors, SC_TC */
2478 interrupt_a_enable |= NISTC_INTA_ENA_AI_ERR |
2479 NISTC_INTA_ENA_AI_SC_TC;
2482 interrupt_a_enable |= NISTC_INTA_ENA_AI_FIFO;
2485 if ((cmd->flags & CMDF_WAKE_EOS) ||
2486 (devpriv->ai_cmd2 & NISTC_AI_CMD2_END_ON_EOS)) {
2487 /* wake on end-of-scan */
2488 devpriv->aimode = AIMODE_SCAN;
2490 devpriv->aimode = AIMODE_HALF_FULL;
2493 switch (devpriv->aimode) {
2494 case AIMODE_HALF_FULL:
2495 /*generate FIFO interrupts and DMA requests on half-full */
2497 ni_stc_writew(dev, NISTC_AI_MODE3_FIFO_MODE_HF_E,
2498 NISTC_AI_MODE3_REG);
2500 ni_stc_writew(dev, NISTC_AI_MODE3_FIFO_MODE_HF,
2501 NISTC_AI_MODE3_REG);
2505 /*generate FIFO interrupts on non-empty */
2506 ni_stc_writew(dev, NISTC_AI_MODE3_FIFO_MODE_NE,
2507 NISTC_AI_MODE3_REG);
2511 ni_stc_writew(dev, NISTC_AI_MODE3_FIFO_MODE_NE,
2512 NISTC_AI_MODE3_REG);
2514 ni_stc_writew(dev, NISTC_AI_MODE3_FIFO_MODE_HF,
2515 NISTC_AI_MODE3_REG);
2517 interrupt_a_enable |= NISTC_INTA_ENA_AI_STOP;
2523 /* clear interrupts */
2524 ni_stc_writew(dev, NISTC_INTA_ACK_AI_ALL, NISTC_INTA_ACK_REG);
2526 ni_set_bits(dev, NISTC_INTA_ENA_REG, interrupt_a_enable, 1);
2528 /* interrupt on nothing */
2529 ni_set_bits(dev, NISTC_INTA_ENA_REG, ~0, 0);
2531 /* XXX start polling if necessary */
2534 /* end configuration */
2535 ni_stc_writew(dev, NISTC_RESET_AI_CFG_END, NISTC_RESET_REG);
2537 switch (cmd->scan_begin_src) {
2539 ni_stc_writew(dev, NISTC_AI_CMD1_SI2_ARM |
2540 NISTC_AI_CMD1_SI_ARM |
2541 NISTC_AI_CMD1_DIV_ARM |
2542 NISTC_AI_CMD1_SC_ARM,
2546 ni_stc_writew(dev, NISTC_AI_CMD1_SI2_ARM |
2547 NISTC_AI_CMD1_SI_ARM | /* XXX ? */
2548 NISTC_AI_CMD1_DIV_ARM |
2549 NISTC_AI_CMD1_SC_ARM,
2556 int retval = ni_ai_setup_MITE_dma(dev);
2563 if (cmd->start_src == TRIG_NOW) {
2564 ni_stc_writew(dev, NISTC_AI_CMD2_START1_PULSE |
2567 s->async->inttrig = NULL;
2568 } else if (cmd->start_src == TRIG_EXT) {
2569 s->async->inttrig = NULL;
2570 } else { /* TRIG_INT */
2571 s->async->inttrig = ni_ai_inttrig;
2577 static int ni_ai_insn_config(struct comedi_device *dev,
2578 struct comedi_subdevice *s,
2579 struct comedi_insn *insn, unsigned int *data)
2581 struct ni_private *devpriv = dev->private;
2587 case INSN_CONFIG_ALT_SOURCE:
2588 if (devpriv->is_m_series) {
2589 if (data[1] & ~NI_M_CFG_BYPASS_AI_CAL_MASK)
2591 devpriv->ai_calib_source = data[1];
2592 } else if (devpriv->is_6143) {
2593 unsigned int calib_source;
2595 calib_source = data[1] & 0xf;
2597 devpriv->ai_calib_source = calib_source;
2598 ni_writew(dev, calib_source, NI6143_CALIB_CHAN_REG);
2600 unsigned int calib_source;
2601 unsigned int calib_source_adjust;
2603 calib_source = data[1] & 0xf;
2604 calib_source_adjust = (data[1] >> 4) & 0xff;
2606 if (calib_source >= 8)
2608 devpriv->ai_calib_source = calib_source;
2609 if (devpriv->is_611x) {
2610 ni_writeb(dev, calib_source_adjust,
2611 NI611X_CAL_GAIN_SEL_REG);
2622 static void ni_ao_munge(struct comedi_device *dev, struct comedi_subdevice *s,
2623 void *data, unsigned int num_bytes,
2624 unsigned int chan_index)
2626 struct comedi_cmd *cmd = &s->async->cmd;
2627 unsigned int nsamples = comedi_bytes_to_samples(s, num_bytes);
2628 unsigned short *array = data;
2631 __le16 buf, *barray = data;
2634 for (i = 0; i < nsamples; i++) {
2635 unsigned int range = CR_RANGE(cmd->chanlist[chan_index]);
2636 unsigned short val = array[i];
2639 * Munge data from unsigned to two's complement for
2642 if (comedi_range_is_bipolar(s, range))
2643 val = comedi_offset_munge(s, val);
2645 buf = cpu_to_le16(val);
2651 chan_index %= cmd->chanlist_len;
2655 static int ni_m_series_ao_config_chanlist(struct comedi_device *dev,
2656 struct comedi_subdevice *s,
2657 unsigned int chanspec[],
2658 unsigned int n_chans, int timed)
2660 struct ni_private *devpriv = dev->private;
2668 for (i = 0; i < s->n_chan; ++i) {
2669 devpriv->ao_conf[i] &= ~NI_M_AO_CFG_BANK_UPDATE_TIMED;
2670 ni_writeb(dev, devpriv->ao_conf[i],
2671 NI_M_AO_CFG_BANK_REG(i));
2672 ni_writeb(dev, 0xf, NI_M_AO_WAVEFORM_ORDER_REG(i));
2675 for (i = 0; i < n_chans; i++) {
2676 const struct comedi_krange *krange;
2678 chan = CR_CHAN(chanspec[i]);
2679 range = CR_RANGE(chanspec[i]);
2680 krange = s->range_table->range + range;
2683 switch (krange->max - krange->min) {
2685 conf |= NI_M_AO_CFG_BANK_REF_INT_10V;
2686 ni_writeb(dev, 0, NI_M_AO_REF_ATTENUATION_REG(chan));
2689 conf |= NI_M_AO_CFG_BANK_REF_INT_5V;
2690 ni_writeb(dev, 0, NI_M_AO_REF_ATTENUATION_REG(chan));
2693 conf |= NI_M_AO_CFG_BANK_REF_INT_10V;
2694 ni_writeb(dev, NI_M_AO_REF_ATTENUATION_X5,
2695 NI_M_AO_REF_ATTENUATION_REG(chan));
2698 conf |= NI_M_AO_CFG_BANK_REF_INT_5V;
2699 ni_writeb(dev, NI_M_AO_REF_ATTENUATION_X5,
2700 NI_M_AO_REF_ATTENUATION_REG(chan));
2703 dev_err(dev->class_dev,
2704 "bug! unhandled ao reference voltage\n");
2707 switch (krange->max + krange->min) {
2709 conf |= NI_M_AO_CFG_BANK_OFFSET_0V;
2712 conf |= NI_M_AO_CFG_BANK_OFFSET_5V;
2715 dev_err(dev->class_dev,
2716 "bug! unhandled ao offset voltage\n");
2720 conf |= NI_M_AO_CFG_BANK_UPDATE_TIMED;
2721 ni_writeb(dev, conf, NI_M_AO_CFG_BANK_REG(chan));
2722 devpriv->ao_conf[chan] = conf;
2723 ni_writeb(dev, i, NI_M_AO_WAVEFORM_ORDER_REG(chan));
2728 static int ni_old_ao_config_chanlist(struct comedi_device *dev,
2729 struct comedi_subdevice *s,
2730 unsigned int chanspec[],
2731 unsigned int n_chans)
2733 struct ni_private *devpriv = dev->private;
2740 for (i = 0; i < n_chans; i++) {
2741 chan = CR_CHAN(chanspec[i]);
2742 range = CR_RANGE(chanspec[i]);
2743 conf = NI_E_AO_DACSEL(chan);
2745 if (comedi_range_is_bipolar(s, range)) {
2746 conf |= NI_E_AO_CFG_BIP;
2747 invert = (s->maxdata + 1) >> 1;
2751 if (comedi_range_is_external(s, range))
2752 conf |= NI_E_AO_EXT_REF;
2754 /* not all boards can deglitch, but this shouldn't hurt */
2755 if (chanspec[i] & CR_DEGLITCH)
2756 conf |= NI_E_AO_DEGLITCH;
2758 /* analog reference */
2759 /* AREF_OTHER connects AO ground to AI ground, i think */
2760 if (CR_AREF(chanspec[i]) == AREF_OTHER)
2761 conf |= NI_E_AO_GROUND_REF;
2763 ni_writew(dev, conf, NI_E_AO_CFG_REG);
2764 devpriv->ao_conf[chan] = conf;
2769 static int ni_ao_config_chanlist(struct comedi_device *dev,
2770 struct comedi_subdevice *s,
2771 unsigned int chanspec[], unsigned int n_chans,
2774 struct ni_private *devpriv = dev->private;
2776 if (devpriv->is_m_series)
2777 return ni_m_series_ao_config_chanlist(dev, s, chanspec, n_chans,
2780 return ni_old_ao_config_chanlist(dev, s, chanspec, n_chans);
2783 static int ni_ao_insn_write(struct comedi_device *dev,
2784 struct comedi_subdevice *s,
2785 struct comedi_insn *insn,
2788 struct ni_private *devpriv = dev->private;
2789 unsigned int chan = CR_CHAN(insn->chanspec);
2790 unsigned int range = CR_RANGE(insn->chanspec);
2794 if (devpriv->is_6xxx) {
2795 ni_ao_win_outw(dev, 1 << chan, NI671X_AO_IMMEDIATE_REG);
2797 reg = NI671X_DAC_DIRECT_DATA_REG(chan);
2798 } else if (devpriv->is_m_series) {
2799 reg = NI_M_DAC_DIRECT_DATA_REG(chan);
2801 reg = NI_E_DAC_DIRECT_DATA_REG(chan);
2804 ni_ao_config_chanlist(dev, s, &insn->chanspec, 1, 0);
2806 for (i = 0; i < insn->n; i++) {
2807 unsigned int val = data[i];
2809 s->readback[chan] = val;
2811 if (devpriv->is_6xxx) {
2813 * 6xxx boards have bipolar outputs, munge the
2814 * unsigned comedi values to 2's complement
2816 val = comedi_offset_munge(s, val);
2818 ni_ao_win_outw(dev, val, reg);
2819 } else if (devpriv->is_m_series) {
2821 * M-series boards use offset binary values for
2822 * bipolar and uinpolar outputs
2824 ni_writew(dev, val, reg);
2827 * Non-M series boards need two's complement values
2828 * for bipolar ranges.
2830 if (comedi_range_is_bipolar(s, range))
2831 val = comedi_offset_munge(s, val);
2833 ni_writew(dev, val, reg);
2840 static int ni_ao_insn_config(struct comedi_device *dev,
2841 struct comedi_subdevice *s,
2842 struct comedi_insn *insn, unsigned int *data)
2844 const struct ni_board_struct *board = dev->board_ptr;
2845 struct ni_private *devpriv = dev->private;
2846 unsigned int nbytes;
2849 case INSN_CONFIG_GET_HARDWARE_BUFFER_SIZE:
2852 nbytes = comedi_samples_to_bytes(s,
2853 board->ao_fifo_depth);
2854 data[2] = 1 + nbytes;
2856 data[2] += devpriv->mite->fifo_size;
2872 static int ni_ao_inttrig(struct comedi_device *dev,
2873 struct comedi_subdevice *s,
2874 unsigned int trig_num)
2876 struct ni_private *devpriv = dev->private;
2877 struct comedi_cmd *cmd = &s->async->cmd;
2879 int interrupt_b_bits;
2881 static const int timeout = 1000;
2883 if (trig_num != cmd->start_arg)
2886 /* Null trig at beginning prevent ao start trigger from executing more than
2887 once per command (and doing things like trying to allocate the ao dma channel
2889 s->async->inttrig = NULL;
2891 ni_set_bits(dev, NISTC_INTB_ENA_REG,
2892 NISTC_INTB_ENA_AO_FIFO | NISTC_INTB_ENA_AO_ERR, 0);
2893 interrupt_b_bits = NISTC_INTB_ENA_AO_ERR;
2895 ni_stc_writew(dev, 1, NISTC_DAC_FIFO_CLR_REG);
2896 if (devpriv->is_6xxx)
2897 ni_ao_win_outl(dev, 0x6, NI611X_AO_FIFO_OFFSET_LOAD_REG);
2898 ret = ni_ao_setup_MITE_dma(dev);
2901 ret = ni_ao_wait_for_dma_load(dev);
2905 ret = ni_ao_prep_fifo(dev, s);
2909 interrupt_b_bits |= NISTC_INTB_ENA_AO_FIFO;
2912 ni_stc_writew(dev, devpriv->ao_mode3 | NISTC_AO_MODE3_NOT_AN_UPDATE,
2913 NISTC_AO_MODE3_REG);
2914 ni_stc_writew(dev, devpriv->ao_mode3, NISTC_AO_MODE3_REG);
2915 /* wait for DACs to be loaded */
2916 for (i = 0; i < timeout; i++) {
2918 if ((ni_stc_readw(dev, NISTC_STATUS2_REG) &
2919 NISTC_STATUS2_AO_TMRDACWRS_IN_PROGRESS) == 0)
2923 dev_err(dev->class_dev,
2924 "timed out waiting for AO_TMRDACWRs_In_Progress_St to clear\n");
2928 * stc manual says we are need to clear error interrupt after
2929 * AO_TMRDACWRs_In_Progress_St clears
2931 ni_stc_writew(dev, NISTC_INTB_ACK_AO_ERR, NISTC_INTB_ACK_REG);
2933 ni_set_bits(dev, NISTC_INTB_ENA_REG, interrupt_b_bits, 1);
2935 ni_stc_writew(dev, NISTC_AO_CMD1_UI_ARM |
2936 NISTC_AO_CMD1_UC_ARM |
2937 NISTC_AO_CMD1_BC_ARM |
2941 ni_stc_writew(dev, NISTC_AO_CMD2_START1_PULSE | devpriv->ao_cmd2,
2949 * Organized similar to NI-STC and MHDDK examples.
2950 * ni_ao_cmd is broken out into configuration sub-routines for clarity.
2953 static void ni_ao_cmd_personalize(struct comedi_device *dev,
2954 const struct comedi_cmd *cmd)
2956 const struct ni_board_struct *board = dev->board_ptr;
2959 ni_stc_writew(dev, NISTC_RESET_AO_CFG_START, NISTC_RESET_REG);
2962 /* fast CPU interface--only eseries */
2963 /* ((slow CPU interface) ? 0 : AO_Fast_CPU) | */
2964 NISTC_AO_PERSONAL_BC_SRC_SEL |
2965 0 /* (use_original_pulse ? 0 : NISTC_AO_PERSONAL_UPDATE_TIMEBASE) */ |
2967 * FIXME: start setting following bit when appropriate. Need to
2968 * determine whether board is E4 or E1.
2970 * if board is E4 or E1
2971 * Set bit "NISTC_AO_PERSONAL_UPDATE_PW" to 0
2975 NISTC_AO_PERSONAL_UPDATE_PW |
2976 /* FIXME: when should we set following bit to zero? */
2977 NISTC_AO_PERSONAL_TMRDACWR_PW |
2978 (board->ao_fifo_depth ?
2979 NISTC_AO_PERSONAL_FIFO_ENA : NISTC_AO_PERSONAL_DMA_PIO_CTRL)
2984 * add something like ".has_individual_dacs = 0" to ni_board_struct
2985 * since, as F Hess pointed out, not all in m series have singles. not
2986 * sure if e-series all have duals...
2990 * F Hess: windows driver does not set NISTC_AO_PERSONAL_NUM_DAC bit for
2991 * 6281, verified with bus analyzer.
2993 if (devpriv->is_m_series)
2994 bits |= NISTC_AO_PERSONAL_NUM_DAC;
2996 ni_stc_writew(dev, bits, NISTC_AO_PERSONAL_REG);
2998 ni_stc_writew(dev, NISTC_RESET_AO_CFG_END, NISTC_RESET_REG);
3001 static void ni_ao_cmd_set_trigger(struct comedi_device *dev,
3002 const struct comedi_cmd *cmd)
3004 struct ni_private *devpriv = dev->private;
3006 ni_stc_writew(dev, NISTC_RESET_AO_CFG_START, NISTC_RESET_REG);
3009 if (cmd->stop_src == TRIG_NONE) {
3010 devpriv->ao_mode1 |= NISTC_AO_MODE1_CONTINUOUS;
3011 devpriv->ao_mode1 &= ~NISTC_AO_MODE1_TRIGGER_ONCE;
3013 devpriv->ao_mode1 &= ~NISTC_AO_MODE1_CONTINUOUS;
3014 devpriv->ao_mode1 |= NISTC_AO_MODE1_TRIGGER_ONCE;
3016 ni_stc_writew(dev, devpriv->ao_mode1, NISTC_AO_MODE1_REG);
3019 unsigned int trigsel = devpriv->ao_trigger_select;
3021 switch (cmd->start_src) {
3024 trigsel &= ~(NISTC_AO_TRIG_START1_POLARITY |
3025 NISTC_AO_TRIG_START1_SEL_MASK);
3026 trigsel |= NISTC_AO_TRIG_START1_EDGE |
3027 NISTC_AO_TRIG_START1_SYNC;
3030 trigsel = NISTC_AO_TRIG_START1_SEL(
3031 CR_CHAN(cmd->start_arg) + 1);
3032 if (cmd->start_arg & CR_INVERT)
3034 * 0=active high, 1=active low.
3035 * see daq-stc 3-24 (p186)
3037 trigsel |= NISTC_AO_TRIG_START1_POLARITY;
3038 if (cmd->start_arg & CR_EDGE)
3039 /* 0=edge detection disabled, 1=enabled */
3040 trigsel |= NISTC_AO_TRIG_START1_EDGE;
3047 devpriv->ao_trigger_select = trigsel;
3048 ni_stc_writew(dev, devpriv->ao_trigger_select,
3049 NISTC_AO_TRIG_SEL_REG);
3051 /* AO_Delayed_START1 = 0, we do not support delayed start...yet */
3054 /* select DA_START1 as PFI6/AO_START1 when configured as an output */
3055 devpriv->ao_mode3 &= ~NISTC_AO_MODE3_TRIG_LEN;
3056 ni_stc_writew(dev, devpriv->ao_mode3, NISTC_AO_MODE3_REG);
3058 ni_stc_writew(dev, NISTC_RESET_AO_CFG_END, NISTC_RESET_REG);
3061 static void ni_ao_cmd_set_counters(struct comedi_device *dev,
3062 const struct comedi_cmd *cmd)
3064 struct ni_private *devpriv = dev->private;
3065 /* Not supporting 'waveform staging' or 'local buffer with pauses' */
3067 ni_stc_writew(dev, NISTC_RESET_AO_CFG_START, NISTC_RESET_REG);
3069 * This relies on ao_mode1/(Trigger_Once | Continuous) being set in
3070 * set_trigger above. It is unclear whether we really need to re-write
3071 * this register with these values. The mhddk examples for e-series
3072 * show writing this in both places, but the examples for m-series show
3073 * a single write in the set_counters function (here).
3075 ni_stc_writew(dev, devpriv->ao_mode1, NISTC_AO_MODE1_REG);
3077 /* sync (upload number of buffer iterations -1) */
3078 /* indicate that we want to use BC_Load_A_Register as the source */
3079 devpriv->ao_mode2 &= ~NISTC_AO_MODE2_BC_INIT_LOAD_SRC;
3080 ni_stc_writew(dev, devpriv->ao_mode2, NISTC_AO_MODE2_REG);
3083 * if the BC_TC interrupt is still issued in spite of UC, BC, UI
3084 * ignoring BC_TC, then we will need to find a way to ignore that
3085 * interrupt in continuous mode.
3087 ni_stc_writel(dev, 0, NISTC_AO_BC_LOADA_REG); /* iter once */
3089 /* sync (issue command to load number of buffer iterations -1) */
3090 ni_stc_writew(dev, NISTC_AO_CMD1_BC_LOAD, NISTC_AO_CMD1_REG);
3092 /* sync (upload number of updates in buffer) */
3093 /* indicate that we want to use UC_Load_A_Register as the source */
3094 devpriv->ao_mode2 &= ~NISTC_AO_MODE2_UC_INIT_LOAD_SRC;
3095 ni_stc_writew(dev, devpriv->ao_mode2, NISTC_AO_MODE2_REG);
3098 * if a user specifies '0', this automatically assumes the entire 24bit
3099 * address space is available for the (multiple iterations of single
3100 * buffer) MISB. Otherwise, stop_arg specifies the MISB length that
3101 * will be used, regardless of whether we are in continuous mode or not.
3102 * In continuous mode, the output will just iterate indefinitely over
3106 unsigned int stop_arg = cmd->stop_arg > 0 ?
3107 (cmd->stop_arg & 0xffffff) : 0xffffff;
3109 if (devpriv->is_m_series) {
3111 * this is how the NI example code does it for m-series
3112 * boards, verified correct with 6259
3114 ni_stc_writel(dev, stop_arg - 1, NISTC_AO_UC_LOADA_REG);
3116 /* sync (issue cmd to load number of updates in MISB) */
3117 ni_stc_writew(dev, NISTC_AO_CMD1_UC_LOAD,
3120 ni_stc_writel(dev, stop_arg, NISTC_AO_UC_LOADA_REG);
3122 /* sync (issue cmd to load number of updates in MISB) */
3123 ni_stc_writew(dev, NISTC_AO_CMD1_UC_LOAD,
3127 * sync (upload number of updates-1 in MISB)
3130 ni_stc_writel(dev, stop_arg - 1, NISTC_AO_UC_LOADA_REG);
3134 ni_stc_writew(dev, NISTC_RESET_AO_CFG_END, NISTC_RESET_REG);
3137 static void ni_ao_cmd_set_update(struct comedi_device *dev,
3138 const struct comedi_cmd *cmd)
3140 struct ni_private *devpriv = dev->private;
3142 ni_stc_writew(dev, NISTC_RESET_AO_CFG_START, NISTC_RESET_REG);
3145 * zero out these bit fields to be set below. Does an ao-reset do this
3148 devpriv->ao_mode1 &= ~(
3149 NISTC_AO_MODE1_UI_SRC_MASK |
3150 NISTC_AO_MODE1_UI_SRC_POLARITY |
3151 NISTC_AO_MODE1_UPDATE_SRC_MASK |
3152 NISTC_AO_MODE1_UPDATE_SRC_POLARITY
3155 switch (cmd->scan_begin_src) {
3157 devpriv->ao_cmd2 &= ~NISTC_AO_CMD2_BC_GATE_ENA;
3160 * NOTE: there are several other ways of configuring internal
3161 * updates, but we'll only support one for now: using
3162 * AO_IN_TIMEBASE, w/o waveform staging, w/o a delay between
3163 * START1 and first update, and also w/o local buffer mode w/
3168 * This is already done above:
3169 * devpriv->ao_mode1 &= ~(
3170 * // set UPDATE_Source to UI_TC:
3171 * NISTC_AO_MODE1_UPDATE_SRC_MASK |
3172 * // set UPDATE_Source_Polarity to rising (required?)
3173 * NISTC_AO_MODE1_UPDATE_SRC_POLARITY |
3174 * // set UI_Source to AO_IN_TIMEBASE1:
3175 * NISTC_AO_MODE1_UI_SRC_MASK |
3176 * // set UI_Source_Polarity to rising (required?)
3177 * NISTC_AO_MODE1_UI_SRC_POLARITY
3182 * TODO: use ao_ui_clock_source to allow all possible signals
3183 * to be routed to UI_Source_Select. See tSTC.h for
3184 * eseries/ni67xx and tMSeries.h for mseries.
3188 unsigned trigvar = ni_ns_to_timer(dev,
3189 cmd->scan_begin_arg,
3190 CMDF_ROUND_NEAREST);
3193 * Wait N TB3 ticks after the start trigger before
3194 * clocking(N must be >=2).
3196 /* following line: 2-1 per STC */
3197 ni_stc_writel(dev, 1, NISTC_AO_UI_LOADA_REG);
3198 ni_stc_writew(dev, NISTC_AO_CMD1_UI_LOAD,
3200 /* following line: N-1 per STC */
3201 ni_stc_writel(dev, trigvar - 1, NISTC_AO_UI_LOADA_REG);
3205 /* FIXME: assert scan_begin_arg != 0, ret failure otherwise */
3206 devpriv->ao_cmd2 |= NISTC_AO_CMD2_BC_GATE_ENA;
3207 devpriv->ao_mode1 |= NISTC_AO_MODE1_UPDATE_SRC(
3208 CR_CHAN(cmd->scan_begin_arg));
3209 if (cmd->scan_begin_arg & CR_INVERT)
3210 devpriv->ao_mode1 |= NISTC_AO_MODE1_UPDATE_SRC_POLARITY;
3217 ni_stc_writew(dev, devpriv->ao_cmd2, NISTC_AO_CMD2_REG);
3218 ni_stc_writew(dev, devpriv->ao_mode1, NISTC_AO_MODE1_REG);
3219 devpriv->ao_mode2 &= ~(NISTC_AO_MODE2_UI_RELOAD_MODE(3) |
3220 NISTC_AO_MODE2_UI_INIT_LOAD_SRC);
3221 ni_stc_writew(dev, devpriv->ao_mode2, NISTC_AO_MODE2_REG);
3223 /* Configure DAQ-STC for Timed update mode */
3224 devpriv->ao_cmd1 |= NISTC_AO_CMD1_DAC1_UPDATE_MODE |
3225 NISTC_AO_CMD1_DAC0_UPDATE_MODE;
3226 /* We are not using UPDATE2-->don't have to set DACx_Source_Select */
3227 ni_stc_writew(dev, devpriv->ao_cmd1, NISTC_AO_CMD1_REG);
3229 ni_stc_writew(dev, NISTC_RESET_AO_CFG_END, NISTC_RESET_REG);
3232 static void ni_ao_cmd_set_channels(struct comedi_device *dev,
3233 struct comedi_subdevice *s)
3235 struct ni_private *devpriv = dev->private;
3236 const struct comedi_cmd *cmd = &s->async->cmd;
3239 ni_stc_writew(dev, NISTC_RESET_AO_CFG_START, NISTC_RESET_REG);
3241 if (devpriv->is_6xxx) {
3245 for (i = 0; i < cmd->chanlist_len; ++i) {
3246 int chan = CR_CHAN(cmd->chanlist[i]);
3249 ni_ao_win_outw(dev, chan, NI611X_AO_WAVEFORM_GEN_REG);
3251 ni_ao_win_outw(dev, bits, NI611X_AO_TIMED_REG);
3254 ni_ao_config_chanlist(dev, s, cmd->chanlist, cmd->chanlist_len, 1);
3256 if (cmd->scan_end_arg > 1) {
3257 devpriv->ao_mode1 |= NISTC_AO_MODE1_MULTI_CHAN;
3258 bits = NISTC_AO_OUT_CTRL_CHANS(cmd->scan_end_arg - 1)
3259 | NISTC_AO_OUT_CTRL_UPDATE_SEL_HIGHZ;
3262 devpriv->ao_mode1 &= ~NISTC_AO_MODE1_MULTI_CHAN;
3263 bits = NISTC_AO_OUT_CTRL_UPDATE_SEL_HIGHZ;
3264 if (devpriv->is_m_series | devpriv->is_6xxx)
3265 bits |= NISTC_AO_OUT_CTRL_CHANS(0);
3267 bits |= NISTC_AO_OUT_CTRL_CHANS(
3268 CR_CHAN(cmd->chanlist[0]));
3271 ni_stc_writew(dev, devpriv->ao_mode1, NISTC_AO_MODE1_REG);
3272 ni_stc_writew(dev, bits, NISTC_AO_OUT_CTRL_REG);
3274 ni_stc_writew(dev, NISTC_RESET_AO_CFG_END, NISTC_RESET_REG);
3277 static void ni_ao_cmd_set_stop_conditions(struct comedi_device *dev,
3278 const struct comedi_cmd *cmd)
3280 struct ni_private *devpriv = dev->private;
3282 ni_stc_writew(dev, NISTC_RESET_AO_CFG_START, NISTC_RESET_REG);
3284 devpriv->ao_mode3 |= NISTC_AO_MODE3_STOP_ON_OVERRUN_ERR;
3285 ni_stc_writew(dev, devpriv->ao_mode3, NISTC_AO_MODE3_REG);
3288 * Since we are not supporting waveform staging, we ignore these errors:
3289 * NISTC_AO_MODE3_STOP_ON_BC_TC_ERR,
3290 * NISTC_AO_MODE3_STOP_ON_BC_TC_TRIG_ERR
3293 ni_stc_writew(dev, NISTC_RESET_AO_CFG_END, NISTC_RESET_REG);
3296 static void ni_ao_cmd_set_fifo_mode(struct comedi_device *dev)
3298 struct ni_private *devpriv = dev->private;
3300 ni_stc_writew(dev, NISTC_RESET_AO_CFG_START, NISTC_RESET_REG);
3302 devpriv->ao_mode2 &= ~NISTC_AO_MODE2_FIFO_MODE_MASK;
3304 devpriv->ao_mode2 |= NISTC_AO_MODE2_FIFO_MODE_HF_F;
3306 devpriv->ao_mode2 |= NISTC_AO_MODE2_FIFO_MODE_HF;
3308 /* NOTE: this is where use_onboard_memory=True would be implemented */
3309 devpriv->ao_mode2 &= ~NISTC_AO_MODE2_FIFO_REXMIT_ENA;
3310 ni_stc_writew(dev, devpriv->ao_mode2, NISTC_AO_MODE2_REG);
3312 /* enable sending of ao fifo requests (dma request) */
3313 ni_stc_writew(dev, NISTC_AO_START_AOFREQ_ENA, NISTC_AO_START_SEL_REG);
3315 ni_stc_writew(dev, NISTC_RESET_AO_CFG_END, NISTC_RESET_REG);
3317 /* we are not supporting boards with virtual fifos */
3320 static void ni_ao_cmd_set_interrupts(struct comedi_device *dev,
3321 struct comedi_subdevice *s)
3323 if (s->async->cmd.stop_src == TRIG_COUNT)
3324 ni_set_bits(dev, NISTC_INTB_ENA_REG,
3325 NISTC_INTB_ENA_AO_BC_TC, 1);
3327 s->async->inttrig = ni_ao_inttrig;
3330 static int ni_ao_cmd(struct comedi_device *dev, struct comedi_subdevice *s)
3332 struct ni_private *devpriv = dev->private;
3333 const struct comedi_cmd *cmd = &s->async->cmd;
3335 if (dev->irq == 0) {
3336 dev_err(dev->class_dev, "cannot run command without an irq");
3340 /* ni_ao_reset should have already been done */
3341 ni_ao_cmd_personalize(dev, cmd);
3342 /* clearing fifo and preload happens elsewhere */
3344 ni_ao_cmd_set_trigger(dev, cmd);
3345 ni_ao_cmd_set_counters(dev, cmd);
3346 ni_ao_cmd_set_update(dev, cmd);
3347 ni_ao_cmd_set_channels(dev, s);
3348 ni_ao_cmd_set_stop_conditions(dev, cmd);
3349 ni_ao_cmd_set_fifo_mode(dev);
3350 ni_cmd_set_mite_transfer(devpriv->ao_mite_ring, s, cmd, 0x00ffffff);
3351 ni_ao_cmd_set_interrupts(dev, s);
3354 * arm(ing) and star(ting) happen in ni_ao_inttrig, which _must_ be
3355 * called for ao commands since 1) TRIG_NOW is not supported and 2) DMA
3356 * must be setup and initially written to before arm/start happen.
3363 static int ni_ao_cmdtest(struct comedi_device *dev, struct comedi_subdevice *s,
3364 struct comedi_cmd *cmd)
3366 const struct ni_board_struct *board = dev->board_ptr;
3367 struct ni_private *devpriv = dev->private;
3371 /* Step 1 : check if triggers are trivially valid */
3373 err |= comedi_check_trigger_src(&cmd->start_src, TRIG_INT | TRIG_EXT);
3374 err |= comedi_check_trigger_src(&cmd->scan_begin_src,
3375 TRIG_TIMER | TRIG_EXT);
3376 err |= comedi_check_trigger_src(&cmd->convert_src, TRIG_NOW);
3377 err |= comedi_check_trigger_src(&cmd->scan_end_src, TRIG_COUNT);
3378 err |= comedi_check_trigger_src(&cmd->stop_src, TRIG_COUNT | TRIG_NONE);
3383 /* Step 2a : make sure trigger sources are unique */
3385 err |= comedi_check_trigger_is_unique(cmd->start_src);
3386 err |= comedi_check_trigger_is_unique(cmd->scan_begin_src);
3387 err |= comedi_check_trigger_is_unique(cmd->stop_src);
3389 /* Step 2b : and mutually compatible */
3394 /* Step 3: check if arguments are trivially valid */
3396 switch (cmd->start_src) {
3398 err |= comedi_check_trigger_arg_is(&cmd->start_arg, 0);
3401 tmp = CR_CHAN(cmd->start_arg);
3405 tmp |= (cmd->start_arg & (CR_INVERT | CR_EDGE));
3406 err |= comedi_check_trigger_arg_is(&cmd->start_arg, tmp);
3410 if (cmd->scan_begin_src == TRIG_TIMER) {
3411 err |= comedi_check_trigger_arg_min(&cmd->scan_begin_arg,
3413 err |= comedi_check_trigger_arg_max(&cmd->scan_begin_arg,
3418 err |= comedi_check_trigger_arg_is(&cmd->convert_arg, 0);
3419 err |= comedi_check_trigger_arg_is(&cmd->scan_end_arg,
3421 err |= comedi_check_trigger_arg_max(&cmd->stop_arg, 0x00ffffff);
3426 /* step 4: fix up any arguments */
3427 if (cmd->scan_begin_src == TRIG_TIMER) {
3428 tmp = cmd->scan_begin_arg;
3429 cmd->scan_begin_arg =
3430 ni_timer_to_ns(dev, ni_ns_to_timer(dev,
3431 cmd->scan_begin_arg,
3433 if (tmp != cmd->scan_begin_arg)
3442 static int ni_ao_reset(struct comedi_device *dev, struct comedi_subdevice *s)
3444 /* See 3.6.1.2 "Resetting", of DAQ-STC Technical Reference Manual */
3447 * In the following, the "--sync" comments are meant to denote
3448 * asynchronous boundaries for setting the registers as described in the
3449 * DAQ-STC mostly in the order also described in the DAQ-STC.
3452 struct ni_private *devpriv = dev->private;
3454 ni_release_ao_mite_channel(dev);
3456 /* --sync (reset AO) */
3457 if (devpriv->is_m_series)
3458 /* following example in mhddk for m-series */
3459 ni_stc_writew(dev, NISTC_RESET_AO, NISTC_RESET_REG);
3461 /*--sync (start config) */
3462 ni_stc_writew(dev, NISTC_RESET_AO_CFG_START, NISTC_RESET_REG);
3464 /*--sync (Disarm) */
3465 ni_stc_writew(dev, NISTC_AO_CMD1_DISARM, NISTC_AO_CMD1_REG);
3469 * (clear bunch of registers--mseries mhddk examples do not include
3472 devpriv->ao_cmd1 = 0;
3473 devpriv->ao_cmd2 = 0;
3474 devpriv->ao_mode1 = 0;
3475 devpriv->ao_mode2 = 0;
3476 if (devpriv->is_m_series)
3477 devpriv->ao_mode3 = NISTC_AO_MODE3_LAST_GATE_DISABLE;
3479 devpriv->ao_mode3 = 0;
3480 devpriv->ao_trigger_select = 0;
3482 ni_stc_writew(dev, 0, NISTC_AO_PERSONAL_REG);
3483 ni_stc_writew(dev, 0, NISTC_AO_CMD1_REG);
3484 ni_stc_writew(dev, 0, NISTC_AO_CMD2_REG);
3485 ni_stc_writew(dev, 0, NISTC_AO_MODE1_REG);
3486 ni_stc_writew(dev, 0, NISTC_AO_MODE2_REG);
3487 ni_stc_writew(dev, 0, NISTC_AO_OUT_CTRL_REG);
3488 ni_stc_writew(dev, devpriv->ao_mode3, NISTC_AO_MODE3_REG);
3489 ni_stc_writew(dev, 0, NISTC_AO_START_SEL_REG);
3490 ni_stc_writew(dev, 0, NISTC_AO_TRIG_SEL_REG);
3492 /*--sync (disable interrupts) */
3493 ni_set_bits(dev, NISTC_INTB_ENA_REG, ~0, 0);
3496 ni_stc_writew(dev, NISTC_AO_PERSONAL_BC_SRC_SEL, NISTC_AO_PERSONAL_REG);
3497 ni_stc_writew(dev, NISTC_INTB_ACK_AO_ALL, NISTC_INTB_ACK_REG);
3499 /*--not in DAQ-STC. which doc? */
3500 if (devpriv->is_6xxx) {
3501 ni_ao_win_outw(dev, (1u << s->n_chan) - 1u,
3502 NI671X_AO_IMMEDIATE_REG);
3503 ni_ao_win_outw(dev, NI611X_AO_MISC_CLEAR_WG,
3504 NI611X_AO_MISC_REG);
3506 ni_stc_writew(dev, NISTC_RESET_AO_CFG_END, NISTC_RESET_REG);
3514 static int ni_dio_insn_config(struct comedi_device *dev,
3515 struct comedi_subdevice *s,
3516 struct comedi_insn *insn,
3519 struct ni_private *devpriv = dev->private;
3522 ret = comedi_dio_insn_config(dev, s, insn, data, 0);
3526 devpriv->dio_control &= ~NISTC_DIO_CTRL_DIR_MASK;
3527 devpriv->dio_control |= NISTC_DIO_CTRL_DIR(s->io_bits);
3528 ni_stc_writew(dev, devpriv->dio_control, NISTC_DIO_CTRL_REG);
3533 static int ni_dio_insn_bits(struct comedi_device *dev,
3534 struct comedi_subdevice *s,
3535 struct comedi_insn *insn,
3538 struct ni_private *devpriv = dev->private;
3540 /* Make sure we're not using the serial part of the dio */
3541 if ((data[0] & (NISTC_DIO_SDIN | NISTC_DIO_SDOUT)) &&
3542 devpriv->serial_interval_ns)
3545 if (comedi_dio_update_state(s, data)) {
3546 devpriv->dio_output &= ~NISTC_DIO_OUT_PARALLEL_MASK;
3547 devpriv->dio_output |= NISTC_DIO_OUT_PARALLEL(s->state);
3548 ni_stc_writew(dev, devpriv->dio_output, NISTC_DIO_OUT_REG);
3551 data[1] = ni_stc_readw(dev, NISTC_DIO_IN_REG);
3556 static int ni_m_series_dio_insn_config(struct comedi_device *dev,
3557 struct comedi_subdevice *s,
3558 struct comedi_insn *insn,
3563 ret = comedi_dio_insn_config(dev, s, insn, data, 0);
3567 ni_writel(dev, s->io_bits, NI_M_DIO_DIR_REG);
3572 static int ni_m_series_dio_insn_bits(struct comedi_device *dev,
3573 struct comedi_subdevice *s,
3574 struct comedi_insn *insn,
3577 if (comedi_dio_update_state(s, data))
3578 ni_writel(dev, s->state, NI_M_DIO_REG);
3580 data[1] = ni_readl(dev, NI_M_DIO_REG);
3585 static int ni_cdio_check_chanlist(struct comedi_device *dev,
3586 struct comedi_subdevice *s,
3587 struct comedi_cmd *cmd)
3591 for (i = 0; i < cmd->chanlist_len; ++i) {
3592 unsigned int chan = CR_CHAN(cmd->chanlist[i]);
3601 static int ni_cdio_cmdtest(struct comedi_device *dev,
3602 struct comedi_subdevice *s, struct comedi_cmd *cmd)
3607 /* Step 1 : check if triggers are trivially valid */
3609 err |= comedi_check_trigger_src(&cmd->start_src, TRIG_INT);
3610 err |= comedi_check_trigger_src(&cmd->scan_begin_src, TRIG_EXT);
3611 err |= comedi_check_trigger_src(&cmd->convert_src, TRIG_NOW);
3612 err |= comedi_check_trigger_src(&cmd->scan_end_src, TRIG_COUNT);
3613 err |= comedi_check_trigger_src(&cmd->stop_src, TRIG_NONE);
3618 /* Step 2a : make sure trigger sources are unique */
3619 /* Step 2b : and mutually compatible */
3621 /* Step 3: check if arguments are trivially valid */
3623 err |= comedi_check_trigger_arg_is(&cmd->start_arg, 0);
3625 tmp = cmd->scan_begin_arg;
3626 tmp &= CR_PACK_FLAGS(NI_M_CDO_MODE_SAMPLE_SRC_MASK, 0, 0, CR_INVERT);
3627 if (tmp != cmd->scan_begin_arg)
3630 err |= comedi_check_trigger_arg_is(&cmd->convert_arg, 0);
3631 err |= comedi_check_trigger_arg_is(&cmd->scan_end_arg,
3633 err |= comedi_check_trigger_arg_max(&cmd->stop_arg,
3634 s->async->prealloc_bufsz /
3635 comedi_bytes_per_scan(s));
3640 /* Step 4: fix up any arguments */
3642 /* Step 5: check channel list if it exists */
3644 if (cmd->chanlist && cmd->chanlist_len > 0)
3645 err |= ni_cdio_check_chanlist(dev, s, cmd);
3653 static int ni_cdo_inttrig(struct comedi_device *dev,
3654 struct comedi_subdevice *s,
3655 unsigned int trig_num)
3657 struct comedi_cmd *cmd = &s->async->cmd;
3658 const unsigned timeout = 1000;
3662 struct ni_private *devpriv = dev->private;
3663 unsigned long flags;
3666 if (trig_num != cmd->start_arg)
3669 s->async->inttrig = NULL;
3671 /* read alloc the entire buffer */
3672 comedi_buf_read_alloc(s, s->async->prealloc_bufsz);
3675 spin_lock_irqsave(&devpriv->mite_channel_lock, flags);
3676 if (devpriv->cdo_mite_chan) {
3677 mite_prep_dma(devpriv->cdo_mite_chan, 32, 32);
3678 mite_dma_arm(devpriv->cdo_mite_chan);
3680 dev_err(dev->class_dev, "BUG: no cdo mite channel?\n");
3683 spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
3688 * XXX not sure what interrupt C group does
3689 * wait for dma to fill output fifo
3690 * ni_writeb(dev, NI_M_INTC_ENA, NI_M_INTC_ENA_REG);
3692 for (i = 0; i < timeout; ++i) {
3693 if (ni_readl(dev, NI_M_CDIO_STATUS_REG) &
3694 NI_M_CDIO_STATUS_CDO_FIFO_FULL)
3699 dev_err(dev->class_dev, "dma failed to fill cdo fifo!\n");
3703 ni_writel(dev, NI_M_CDO_CMD_ARM |
3704 NI_M_CDO_CMD_ERR_INT_ENA_SET |
3705 NI_M_CDO_CMD_F_E_INT_ENA_SET,
3710 static int ni_cdio_cmd(struct comedi_device *dev, struct comedi_subdevice *s)
3712 struct ni_private *devpriv = dev->private;
3713 const struct comedi_cmd *cmd = &s->async->cmd;
3714 unsigned cdo_mode_bits;
3717 ni_writel(dev, NI_M_CDO_CMD_RESET, NI_M_CDIO_CMD_REG);
3718 cdo_mode_bits = NI_M_CDO_MODE_FIFO_MODE |
3719 NI_M_CDO_MODE_HALT_ON_ERROR |
3720 NI_M_CDO_MODE_SAMPLE_SRC(CR_CHAN(cmd->scan_begin_arg));
3721 if (cmd->scan_begin_arg & CR_INVERT)
3722 cdo_mode_bits |= NI_M_CDO_MODE_POLARITY;
3723 ni_writel(dev, cdo_mode_bits, NI_M_CDO_MODE_REG);
3725 ni_writel(dev, s->state, NI_M_CDO_FIFO_DATA_REG);
3726 ni_writel(dev, NI_M_CDO_CMD_SW_UPDATE, NI_M_CDIO_CMD_REG);
3727 ni_writel(dev, s->io_bits, NI_M_CDO_MASK_ENA_REG);
3729 dev_err(dev->class_dev,
3730 "attempted to run digital output command with no lines configured as outputs\n");
3733 retval = ni_request_cdo_mite_channel(dev);
3737 ni_cmd_set_mite_transfer(devpriv->cdo_mite_ring, s, cmd,
3738 s->async->prealloc_bufsz /
3739 comedi_bytes_per_scan(s));
3741 s->async->inttrig = ni_cdo_inttrig;
3746 static int ni_cdio_cancel(struct comedi_device *dev, struct comedi_subdevice *s)
3748 ni_writel(dev, NI_M_CDO_CMD_DISARM |
3749 NI_M_CDO_CMD_ERR_INT_ENA_CLR |
3750 NI_M_CDO_CMD_F_E_INT_ENA_CLR |
3751 NI_M_CDO_CMD_F_REQ_INT_ENA_CLR,
3754 * XXX not sure what interrupt C group does
3755 * ni_writeb(dev, 0, NI_M_INTC_ENA_REG);
3757 ni_writel(dev, 0, NI_M_CDO_MASK_ENA_REG);
3758 ni_release_cdo_mite_channel(dev);
3762 static void handle_cdio_interrupt(struct comedi_device *dev)
3764 struct ni_private *devpriv = dev->private;
3765 unsigned cdio_status;
3766 struct comedi_subdevice *s = &dev->subdevices[NI_DIO_SUBDEV];
3768 unsigned long flags;
3771 if (!devpriv->is_m_series)
3774 spin_lock_irqsave(&devpriv->mite_channel_lock, flags);
3775 if (devpriv->cdo_mite_chan) {
3776 unsigned cdo_mite_status =
3777 mite_get_status(devpriv->cdo_mite_chan);
3778 if (cdo_mite_status & CHSR_LINKC) {
3780 devpriv->mite->mite_io_addr +
3781 MITE_CHOR(devpriv->cdo_mite_chan->channel));
3783 mite_sync_output_dma(devpriv->cdo_mite_chan, s);
3785 spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
3788 cdio_status = ni_readl(dev, NI_M_CDIO_STATUS_REG);
3789 if (cdio_status & NI_M_CDIO_STATUS_CDO_ERROR) {
3790 /* XXX just guessing this is needed and does something useful */
3791 ni_writel(dev, NI_M_CDO_CMD_ERR_INT_CONFIRM,
3793 s->async->events |= COMEDI_CB_OVERFLOW;
3795 if (cdio_status & NI_M_CDIO_STATUS_CDO_FIFO_EMPTY) {
3796 ni_writel(dev, NI_M_CDO_CMD_F_E_INT_ENA_CLR,
3798 /* s->async->events |= COMEDI_CB_EOA; */
3800 comedi_handle_events(dev, s);
3803 static int ni_serial_hw_readwrite8(struct comedi_device *dev,
3804 struct comedi_subdevice *s,
3805 unsigned char data_out,
3806 unsigned char *data_in)
3808 struct ni_private *devpriv = dev->private;
3809 unsigned int status1;
3810 int err = 0, count = 20;
3812 devpriv->dio_output &= ~NISTC_DIO_OUT_SERIAL_MASK;
3813 devpriv->dio_output |= NISTC_DIO_OUT_SERIAL(data_out);
3814 ni_stc_writew(dev, devpriv->dio_output, NISTC_DIO_OUT_REG);
3816 status1 = ni_stc_readw(dev, NISTC_STATUS1_REG);
3817 if (status1 & NISTC_STATUS1_SERIO_IN_PROG) {
3822 devpriv->dio_control |= NISTC_DIO_CTRL_HW_SER_START;
3823 ni_stc_writew(dev, devpriv->dio_control, NISTC_DIO_CTRL_REG);
3824 devpriv->dio_control &= ~NISTC_DIO_CTRL_HW_SER_START;
3826 /* Wait until STC says we're done, but don't loop infinitely. */
3827 while ((status1 = ni_stc_readw(dev, NISTC_STATUS1_REG)) &
3828 NISTC_STATUS1_SERIO_IN_PROG) {
3829 /* Delay one bit per loop */
3830 udelay((devpriv->serial_interval_ns + 999) / 1000);
3832 dev_err(dev->class_dev,
3833 "SPI serial I/O didn't finish in time!\n");
3840 * Delay for last bit. This delay is absolutely necessary, because
3841 * NISTC_STATUS1_SERIO_IN_PROG goes high one bit too early.
3843 udelay((devpriv->serial_interval_ns + 999) / 1000);
3846 *data_in = ni_stc_readw(dev, NISTC_DIO_SERIAL_IN_REG);
3849 ni_stc_writew(dev, devpriv->dio_control, NISTC_DIO_CTRL_REG);
3854 static int ni_serial_sw_readwrite8(struct comedi_device *dev,
3855 struct comedi_subdevice *s,
3856 unsigned char data_out,
3857 unsigned char *data_in)
3859 struct ni_private *devpriv = dev->private;
3860 unsigned char mask, input = 0;
3862 /* Wait for one bit before transfer */
3863 udelay((devpriv->serial_interval_ns + 999) / 1000);
3865 for (mask = 0x80; mask; mask >>= 1) {
3866 /* Output current bit; note that we cannot touch s->state
3867 because it is a per-subdevice field, and serial is
3868 a separate subdevice from DIO. */
3869 devpriv->dio_output &= ~NISTC_DIO_SDOUT;
3870 if (data_out & mask)
3871 devpriv->dio_output |= NISTC_DIO_SDOUT;
3872 ni_stc_writew(dev, devpriv->dio_output, NISTC_DIO_OUT_REG);
3874 /* Assert SDCLK (active low, inverted), wait for half of
3875 the delay, deassert SDCLK, and wait for the other half. */
3876 devpriv->dio_control |= NISTC_DIO_SDCLK;
3877 ni_stc_writew(dev, devpriv->dio_control, NISTC_DIO_CTRL_REG);
3879 udelay((devpriv->serial_interval_ns + 999) / 2000);
3881 devpriv->dio_control &= ~NISTC_DIO_SDCLK;
3882 ni_stc_writew(dev, devpriv->dio_control, NISTC_DIO_CTRL_REG);
3884 udelay((devpriv->serial_interval_ns + 999) / 2000);
3886 /* Input current bit */
3887 if (ni_stc_readw(dev, NISTC_DIO_IN_REG) & NISTC_DIO_SDIN)
3897 static int ni_serial_insn_config(struct comedi_device *dev,
3898 struct comedi_subdevice *s,
3899 struct comedi_insn *insn,
3902 struct ni_private *devpriv = dev->private;
3903 unsigned clk_fout = devpriv->clock_and_fout;
3905 unsigned char byte_out, byte_in = 0;
3911 case INSN_CONFIG_SERIAL_CLOCK:
3912 devpriv->serial_hw_mode = 1;
3913 devpriv->dio_control |= NISTC_DIO_CTRL_HW_SER_ENA;
3915 if (data[1] == SERIAL_DISABLED) {
3916 devpriv->serial_hw_mode = 0;
3917 devpriv->dio_control &= ~(NISTC_DIO_CTRL_HW_SER_ENA |
3919 data[1] = SERIAL_DISABLED;
3920 devpriv->serial_interval_ns = data[1];
3921 } else if (data[1] <= SERIAL_600NS) {
3922 /* Warning: this clock speed is too fast to reliably
3924 devpriv->dio_control &= ~NISTC_DIO_CTRL_HW_SER_TIMEBASE;
3925 clk_fout |= NISTC_CLK_FOUT_SLOW_TIMEBASE;
3926 clk_fout &= ~NISTC_CLK_FOUT_DIO_SER_OUT_DIV2;
3927 data[1] = SERIAL_600NS;
3928 devpriv->serial_interval_ns = data[1];
3929 } else if (data[1] <= SERIAL_1_2US) {
3930 devpriv->dio_control &= ~NISTC_DIO_CTRL_HW_SER_TIMEBASE;
3931 clk_fout |= NISTC_CLK_FOUT_SLOW_TIMEBASE |
3932 NISTC_CLK_FOUT_DIO_SER_OUT_DIV2;
3933 data[1] = SERIAL_1_2US;
3934 devpriv->serial_interval_ns = data[1];
3935 } else if (data[1] <= SERIAL_10US) {
3936 devpriv->dio_control |= NISTC_DIO_CTRL_HW_SER_TIMEBASE;
3937 clk_fout |= NISTC_CLK_FOUT_SLOW_TIMEBASE |
3938 NISTC_CLK_FOUT_DIO_SER_OUT_DIV2;
3939 /* Note: NISTC_CLK_FOUT_DIO_SER_OUT_DIV2 only affects
3940 600ns/1.2us. If you turn divide_by_2 off with the
3941 slow clock, you will still get 10us, except then
3942 all your delays are wrong. */
3943 data[1] = SERIAL_10US;
3944 devpriv->serial_interval_ns = data[1];
3946 devpriv->dio_control &= ~(NISTC_DIO_CTRL_HW_SER_ENA |
3948 devpriv->serial_hw_mode = 0;
3949 data[1] = (data[1] / 1000) * 1000;
3950 devpriv->serial_interval_ns = data[1];
3952 devpriv->clock_and_fout = clk_fout;
3954 ni_stc_writew(dev, devpriv->dio_control, NISTC_DIO_CTRL_REG);
3955 ni_stc_writew(dev, devpriv->clock_and_fout, NISTC_CLK_FOUT_REG);
3958 case INSN_CONFIG_BIDIRECTIONAL_DATA:
3960 if (devpriv->serial_interval_ns == 0)
3963 byte_out = data[1] & 0xFF;
3965 if (devpriv->serial_hw_mode) {
3966 err = ni_serial_hw_readwrite8(dev, s, byte_out,
3968 } else if (devpriv->serial_interval_ns > 0) {
3969 err = ni_serial_sw_readwrite8(dev, s, byte_out,
3972 dev_err(dev->class_dev, "serial disabled!\n");
3977 data[1] = byte_in & 0xFF;
3986 static void init_ao_67xx(struct comedi_device *dev, struct comedi_subdevice *s)
3990 for (i = 0; i < s->n_chan; i++) {
3991 ni_ao_win_outw(dev, NI_E_AO_DACSEL(i) | 0x0,
3992 NI67XX_AO_CFG2_REG);
3994 ni_ao_win_outw(dev, 0x0, NI67XX_AO_SP_UPDATES_REG);
3997 static const struct mio_regmap ni_gpct_to_stc_regmap[] = {
3998 [NITIO_G0_AUTO_INC] = { NISTC_G0_AUTOINC_REG, 2 },
3999 [NITIO_G1_AUTO_INC] = { NISTC_G1_AUTOINC_REG, 2 },
4000 [NITIO_G0_CMD] = { NISTC_G0_CMD_REG, 2 },
4001 [NITIO_G1_CMD] = { NISTC_G1_CMD_REG, 2 },
4002 [NITIO_G0_HW_SAVE] = { NISTC_G0_HW_SAVE_REG, 4 },
4003 [NITIO_G1_HW_SAVE] = { NISTC_G1_HW_SAVE_REG, 4 },
4004 [NITIO_G0_SW_SAVE] = { NISTC_G0_SAVE_REG, 4 },
4005 [NITIO_G1_SW_SAVE] = { NISTC_G1_SAVE_REG, 4 },
4006 [NITIO_G0_MODE] = { NISTC_G0_MODE_REG, 2 },
4007 [NITIO_G1_MODE] = { NISTC_G1_MODE_REG, 2 },
4008 [NITIO_G0_LOADA] = { NISTC_G0_LOADA_REG, 4 },
4009 [NITIO_G1_LOADA] = { NISTC_G1_LOADA_REG, 4 },
4010 [NITIO_G0_LOADB] = { NISTC_G0_LOADB_REG, 4 },
4011 [NITIO_G1_LOADB] = { NISTC_G1_LOADB_REG, 4 },
4012 [NITIO_G0_INPUT_SEL] = { NISTC_G0_INPUT_SEL_REG, 2 },
4013 [NITIO_G1_INPUT_SEL] = { NISTC_G1_INPUT_SEL_REG, 2 },
4014 [NITIO_G0_CNT_MODE] = { 0x1b0, 2 }, /* M-Series only */
4015 [NITIO_G1_CNT_MODE] = { 0x1b2, 2 }, /* M-Series only */
4016 [NITIO_G0_GATE2] = { 0x1b4, 2 }, /* M-Series only */
4017 [NITIO_G1_GATE2] = { 0x1b6, 2 }, /* M-Series only */
4018 [NITIO_G01_STATUS] = { NISTC_G01_STATUS_REG, 2 },
4019 [NITIO_G01_RESET] = { NISTC_RESET_REG, 2 },
4020 [NITIO_G01_STATUS1] = { NISTC_STATUS1_REG, 2 },
4021 [NITIO_G01_STATUS2] = { NISTC_STATUS2_REG, 2 },
4022 [NITIO_G0_DMA_CFG] = { 0x1b8, 2 }, /* M-Series only */
4023 [NITIO_G1_DMA_CFG] = { 0x1ba, 2 }, /* M-Series only */
4024 [NITIO_G0_DMA_STATUS] = { 0x1b8, 2 }, /* M-Series only */
4025 [NITIO_G1_DMA_STATUS] = { 0x1ba, 2 }, /* M-Series only */
4026 [NITIO_G0_ABZ] = { 0x1c0, 2 }, /* M-Series only */
4027 [NITIO_G1_ABZ] = { 0x1c2, 2 }, /* M-Series only */
4028 [NITIO_G0_INT_ACK] = { NISTC_INTA_ACK_REG, 2 },
4029 [NITIO_G1_INT_ACK] = { NISTC_INTB_ACK_REG, 2 },
4030 [NITIO_G0_STATUS] = { NISTC_AI_STATUS1_REG, 2 },
4031 [NITIO_G1_STATUS] = { NISTC_AO_STATUS1_REG, 2 },
4032 [NITIO_G0_INT_ENA] = { NISTC_INTA_ENA_REG, 2 },
4033 [NITIO_G1_INT_ENA] = { NISTC_INTB_ENA_REG, 2 },
4036 static unsigned int ni_gpct_to_stc_register(struct comedi_device *dev,
4037 enum ni_gpct_register reg)
4039 const struct mio_regmap *regmap;
4041 if (reg < ARRAY_SIZE(ni_gpct_to_stc_regmap)) {
4042 regmap = &ni_gpct_to_stc_regmap[reg];
4044 dev_warn(dev->class_dev, "%s: unhandled register=0x%x\n",
4049 return regmap->mio_reg;
4052 static void ni_gpct_write_register(struct ni_gpct *counter, unsigned bits,
4053 enum ni_gpct_register reg)
4055 struct comedi_device *dev = counter->counter_dev->dev;
4056 unsigned int stc_register = ni_gpct_to_stc_register(dev, reg);
4057 static const unsigned gpct_interrupt_a_enable_mask =
4058 NISTC_INTA_ENA_G0_GATE | NISTC_INTA_ENA_G0_TC;
4059 static const unsigned gpct_interrupt_b_enable_mask =
4060 NISTC_INTB_ENA_G1_GATE | NISTC_INTB_ENA_G1_TC;
4062 if (stc_register == 0)
4066 /* m-series only registers */
4067 case NITIO_G0_CNT_MODE:
4068 case NITIO_G1_CNT_MODE:
4069 case NITIO_G0_GATE2:
4070 case NITIO_G1_GATE2:
4071 case NITIO_G0_DMA_CFG:
4072 case NITIO_G1_DMA_CFG:
4075 ni_writew(dev, bits, stc_register);
4078 /* 32 bit registers */
4079 case NITIO_G0_LOADA:
4080 case NITIO_G1_LOADA:
4081 case NITIO_G0_LOADB:
4082 case NITIO_G1_LOADB:
4083 ni_stc_writel(dev, bits, stc_register);
4086 /* 16 bit registers */
4087 case NITIO_G0_INT_ENA:
4088 BUG_ON(bits & ~gpct_interrupt_a_enable_mask);
4089 ni_set_bitfield(dev, stc_register,
4090 gpct_interrupt_a_enable_mask, bits);
4092 case NITIO_G1_INT_ENA:
4093 BUG_ON(bits & ~gpct_interrupt_b_enable_mask);
4094 ni_set_bitfield(dev, stc_register,
4095 gpct_interrupt_b_enable_mask, bits);
4097 case NITIO_G01_RESET:
4098 BUG_ON(bits & ~(NISTC_RESET_G0 | NISTC_RESET_G1));
4101 ni_stc_writew(dev, bits, stc_register);
4105 static unsigned ni_gpct_read_register(struct ni_gpct *counter,
4106 enum ni_gpct_register reg)
4108 struct comedi_device *dev = counter->counter_dev->dev;
4109 unsigned int stc_register = ni_gpct_to_stc_register(dev, reg);
4111 if (stc_register == 0)
4115 /* m-series only registers */
4116 case NITIO_G0_DMA_STATUS:
4117 case NITIO_G1_DMA_STATUS:
4118 return ni_readw(dev, stc_register);
4120 /* 32 bit registers */
4121 case NITIO_G0_HW_SAVE:
4122 case NITIO_G1_HW_SAVE:
4123 case NITIO_G0_SW_SAVE:
4124 case NITIO_G1_SW_SAVE:
4125 return ni_stc_readl(dev, stc_register);
4127 /* 16 bit registers */
4129 return ni_stc_readw(dev, stc_register);
4133 static int ni_freq_out_insn_read(struct comedi_device *dev,
4134 struct comedi_subdevice *s,
4135 struct comedi_insn *insn,
4138 struct ni_private *devpriv = dev->private;
4139 unsigned int val = NISTC_CLK_FOUT_TO_DIVIDER(devpriv->clock_and_fout);
4142 for (i = 0; i < insn->n; i++)
4148 static int ni_freq_out_insn_write(struct comedi_device *dev,
4149 struct comedi_subdevice *s,
4150 struct comedi_insn *insn,
4153 struct ni_private *devpriv = dev->private;
4156 unsigned int val = data[insn->n - 1];
4158 devpriv->clock_and_fout &= ~NISTC_CLK_FOUT_ENA;
4159 ni_stc_writew(dev, devpriv->clock_and_fout, NISTC_CLK_FOUT_REG);
4160 devpriv->clock_and_fout &= ~NISTC_CLK_FOUT_DIVIDER_MASK;
4162 /* use the last data value to set the fout divider */
4163 devpriv->clock_and_fout |= NISTC_CLK_FOUT_DIVIDER(val);
4165 devpriv->clock_and_fout |= NISTC_CLK_FOUT_ENA;
4166 ni_stc_writew(dev, devpriv->clock_and_fout, NISTC_CLK_FOUT_REG);
4171 static int ni_freq_out_insn_config(struct comedi_device *dev,
4172 struct comedi_subdevice *s,
4173 struct comedi_insn *insn,
4176 struct ni_private *devpriv = dev->private;
4179 case INSN_CONFIG_SET_CLOCK_SRC:
4181 case NI_FREQ_OUT_TIMEBASE_1_DIV_2_CLOCK_SRC:
4182 devpriv->clock_and_fout &= ~NISTC_CLK_FOUT_TIMEBASE_SEL;
4184 case NI_FREQ_OUT_TIMEBASE_2_CLOCK_SRC:
4185 devpriv->clock_and_fout |= NISTC_CLK_FOUT_TIMEBASE_SEL;
4190 ni_stc_writew(dev, devpriv->clock_and_fout, NISTC_CLK_FOUT_REG);
4192 case INSN_CONFIG_GET_CLOCK_SRC:
4193 if (devpriv->clock_and_fout & NISTC_CLK_FOUT_TIMEBASE_SEL) {
4194 data[1] = NI_FREQ_OUT_TIMEBASE_2_CLOCK_SRC;
4195 data[2] = TIMEBASE_2_NS;
4197 data[1] = NI_FREQ_OUT_TIMEBASE_1_DIV_2_CLOCK_SRC;
4198 data[2] = TIMEBASE_1_NS * 2;
4207 static int ni_8255_callback(struct comedi_device *dev,
4208 int dir, int port, int data, unsigned long iobase)
4211 ni_writeb(dev, data, iobase + 2 * port);
4215 return ni_readb(dev, iobase + 2 * port);
4218 static int ni_get_pwm_config(struct comedi_device *dev, unsigned int *data)
4220 struct ni_private *devpriv = dev->private;
4222 data[1] = devpriv->pwm_up_count * devpriv->clock_ns;
4223 data[2] = devpriv->pwm_down_count * devpriv->clock_ns;
4227 static int ni_m_series_pwm_config(struct comedi_device *dev,
4228 struct comedi_subdevice *s,
4229 struct comedi_insn *insn,
4232 struct ni_private *devpriv = dev->private;
4233 unsigned up_count, down_count;
4236 case INSN_CONFIG_PWM_OUTPUT:
4238 case CMDF_ROUND_NEAREST:
4241 devpriv->clock_ns / 2) / devpriv->clock_ns;
4243 case CMDF_ROUND_DOWN:
4244 up_count = data[2] / devpriv->clock_ns;
4248 (data[2] + devpriv->clock_ns -
4249 1) / devpriv->clock_ns;
4255 case CMDF_ROUND_NEAREST:
4258 devpriv->clock_ns / 2) / devpriv->clock_ns;
4260 case CMDF_ROUND_DOWN:
4261 down_count = data[4] / devpriv->clock_ns;
4265 (data[4] + devpriv->clock_ns -
4266 1) / devpriv->clock_ns;
4271 if (up_count * devpriv->clock_ns != data[2] ||
4272 down_count * devpriv->clock_ns != data[4]) {
4273 data[2] = up_count * devpriv->clock_ns;
4274 data[4] = down_count * devpriv->clock_ns;
4277 ni_writel(dev, NI_M_CAL_PWM_HIGH_TIME(up_count) |
4278 NI_M_CAL_PWM_LOW_TIME(down_count),
4280 devpriv->pwm_up_count = up_count;
4281 devpriv->pwm_down_count = down_count;
4283 case INSN_CONFIG_GET_PWM_OUTPUT:
4284 return ni_get_pwm_config(dev, data);
4291 static int ni_6143_pwm_config(struct comedi_device *dev,
4292 struct comedi_subdevice *s,
4293 struct comedi_insn *insn,
4296 struct ni_private *devpriv = dev->private;
4297 unsigned up_count, down_count;
4300 case INSN_CONFIG_PWM_OUTPUT:
4302 case CMDF_ROUND_NEAREST:
4305 devpriv->clock_ns / 2) / devpriv->clock_ns;
4307 case CMDF_ROUND_DOWN:
4308 up_count = data[2] / devpriv->clock_ns;
4312 (data[2] + devpriv->clock_ns -
4313 1) / devpriv->clock_ns;
4319 case CMDF_ROUND_NEAREST:
4322 devpriv->clock_ns / 2) / devpriv->clock_ns;
4324 case CMDF_ROUND_DOWN:
4325 down_count = data[4] / devpriv->clock_ns;
4329 (data[4] + devpriv->clock_ns -
4330 1) / devpriv->clock_ns;
4335 if (up_count * devpriv->clock_ns != data[2] ||
4336 down_count * devpriv->clock_ns != data[4]) {
4337 data[2] = up_count * devpriv->clock_ns;
4338 data[4] = down_count * devpriv->clock_ns;
4341 ni_writel(dev, up_count, NI6143_CALIB_HI_TIME_REG);
4342 devpriv->pwm_up_count = up_count;
4343 ni_writel(dev, down_count, NI6143_CALIB_LO_TIME_REG);
4344 devpriv->pwm_down_count = down_count;
4346 case INSN_CONFIG_GET_PWM_OUTPUT:
4347 return ni_get_pwm_config(dev, data);
4354 static int pack_mb88341(int addr, int val, int *bitstring)
4358 Note that address bits are reversed. Thanks to
4359 Ingo Keen for noticing this.
4361 Note also that the 88341 expects address values from
4362 1-12, whereas we use channel numbers 0-11. The NI
4363 docs use 1-12, also, so be careful here.
4366 *bitstring = ((addr & 0x1) << 11) |
4367 ((addr & 0x2) << 9) |
4368 ((addr & 0x4) << 7) | ((addr & 0x8) << 5) | (val & 0xff);
4372 static int pack_dac8800(int addr, int val, int *bitstring)
4374 *bitstring = ((addr & 0x7) << 8) | (val & 0xff);
4378 static int pack_dac8043(int addr, int val, int *bitstring)
4380 *bitstring = val & 0xfff;
4384 static int pack_ad8522(int addr, int val, int *bitstring)
4386 *bitstring = (val & 0xfff) | (addr ? 0xc000 : 0xa000);
4390 static int pack_ad8804(int addr, int val, int *bitstring)
4392 *bitstring = ((addr & 0xf) << 8) | (val & 0xff);
4396 static int pack_ad8842(int addr, int val, int *bitstring)
4398 *bitstring = ((addr + 1) << 8) | (val & 0xff);
4402 struct caldac_struct {
4405 int (*packbits)(int, int, int *);
4408 static struct caldac_struct caldacs[] = {
4409 [mb88341] = {12, 8, pack_mb88341},
4410 [dac8800] = {8, 8, pack_dac8800},
4411 [dac8043] = {1, 12, pack_dac8043},
4412 [ad8522] = {2, 12, pack_ad8522},
4413 [ad8804] = {12, 8, pack_ad8804},
4414 [ad8842] = {8, 8, pack_ad8842},
4415 [ad8804_debug] = {16, 8, pack_ad8804},
4418 static void ni_write_caldac(struct comedi_device *dev, int addr, int val)
4420 const struct ni_board_struct *board = dev->board_ptr;
4421 struct ni_private *devpriv = dev->private;
4422 unsigned int loadbit = 0, bits = 0, bit, bitstring = 0;
4427 if (devpriv->caldacs[addr] == val)
4429 devpriv->caldacs[addr] = val;
4431 for (i = 0; i < 3; i++) {
4432 type = board->caldac[i];
4433 if (type == caldac_none)
4435 if (addr < caldacs[type].n_chans) {
4436 bits = caldacs[type].packbits(addr, val, &bitstring);
4437 loadbit = NI_E_SERIAL_CMD_DAC_LD(i);
4440 addr -= caldacs[type].n_chans;
4443 /* bits will be 0 if there is no caldac for the given addr */
4447 for (bit = 1 << (bits - 1); bit; bit >>= 1) {
4448 cmd = (bit & bitstring) ? NI_E_SERIAL_CMD_SDATA : 0;
4449 ni_writeb(dev, cmd, NI_E_SERIAL_CMD_REG);
4451 ni_writeb(dev, NI_E_SERIAL_CMD_SCLK | cmd, NI_E_SERIAL_CMD_REG);
4454 ni_writeb(dev, loadbit, NI_E_SERIAL_CMD_REG);
4456 ni_writeb(dev, 0, NI_E_SERIAL_CMD_REG);
4459 static int ni_calib_insn_write(struct comedi_device *dev,
4460 struct comedi_subdevice *s,
4461 struct comedi_insn *insn,
4464 ni_write_caldac(dev, CR_CHAN(insn->chanspec), data[0]);
4469 static int ni_calib_insn_read(struct comedi_device *dev,
4470 struct comedi_subdevice *s,
4471 struct comedi_insn *insn,
4474 struct ni_private *devpriv = dev->private;
4476 data[0] = devpriv->caldacs[CR_CHAN(insn->chanspec)];
4481 static void caldac_setup(struct comedi_device *dev, struct comedi_subdevice *s)
4483 const struct ni_board_struct *board = dev->board_ptr;
4484 struct ni_private *devpriv = dev->private;
4493 type = board->caldac[0];
4494 if (type == caldac_none)
4496 n_bits = caldacs[type].n_bits;
4497 for (i = 0; i < 3; i++) {
4498 type = board->caldac[i];
4499 if (type == caldac_none)
4501 if (caldacs[type].n_bits != n_bits)
4503 n_chans += caldacs[type].n_chans;
4506 s->n_chan = n_chans;
4509 unsigned int *maxdata_list;
4511 if (n_chans > MAX_N_CALDACS)
4512 dev_err(dev->class_dev,
4513 "BUG! MAX_N_CALDACS too small\n");
4514 s->maxdata_list = maxdata_list = devpriv->caldac_maxdata_list;
4516 for (i = 0; i < n_dacs; i++) {
4517 type = board->caldac[i];
4518 for (j = 0; j < caldacs[type].n_chans; j++) {
4519 maxdata_list[chan] =
4520 (1 << caldacs[type].n_bits) - 1;
4525 for (chan = 0; chan < s->n_chan; chan++)
4526 ni_write_caldac(dev, i, s->maxdata_list[i] / 2);
4528 type = board->caldac[0];
4529 s->maxdata = (1 << caldacs[type].n_bits) - 1;
4531 for (chan = 0; chan < s->n_chan; chan++)
4532 ni_write_caldac(dev, i, s->maxdata / 2);
4536 static int ni_read_eeprom(struct comedi_device *dev, int addr)
4538 unsigned int cmd = NI_E_SERIAL_CMD_EEPROM_CS;
4542 bitstring = 0x0300 | ((addr & 0x100) << 3) | (addr & 0xff);
4543 ni_writeb(dev, cmd, NI_E_SERIAL_CMD_REG);
4544 for (bit = 0x8000; bit; bit >>= 1) {
4545 if (bit & bitstring)
4546 cmd |= NI_E_SERIAL_CMD_SDATA;
4548 cmd &= ~NI_E_SERIAL_CMD_SDATA;
4550 ni_writeb(dev, cmd, NI_E_SERIAL_CMD_REG);
4551 ni_writeb(dev, NI_E_SERIAL_CMD_SCLK | cmd, NI_E_SERIAL_CMD_REG);
4553 cmd = NI_E_SERIAL_CMD_EEPROM_CS;
4555 for (bit = 0x80; bit; bit >>= 1) {
4556 ni_writeb(dev, cmd, NI_E_SERIAL_CMD_REG);
4557 ni_writeb(dev, NI_E_SERIAL_CMD_SCLK | cmd, NI_E_SERIAL_CMD_REG);
4558 if (ni_readb(dev, NI_E_STATUS_REG) & NI_E_STATUS_PROMOUT)
4561 ni_writeb(dev, 0, NI_E_SERIAL_CMD_REG);
4566 static int ni_eeprom_insn_read(struct comedi_device *dev,
4567 struct comedi_subdevice *s,
4568 struct comedi_insn *insn,
4571 data[0] = ni_read_eeprom(dev, CR_CHAN(insn->chanspec));
4576 static int ni_m_series_eeprom_insn_read(struct comedi_device *dev,
4577 struct comedi_subdevice *s,
4578 struct comedi_insn *insn,
4581 struct ni_private *devpriv = dev->private;
4583 data[0] = devpriv->eeprom_buffer[CR_CHAN(insn->chanspec)];
4588 static unsigned ni_old_get_pfi_routing(struct comedi_device *dev,
4591 /* pre-m-series boards have fixed signals on pfi pins */
4594 return NI_PFI_OUTPUT_AI_START1;
4596 return NI_PFI_OUTPUT_AI_START2;
4598 return NI_PFI_OUTPUT_AI_CONVERT;
4600 return NI_PFI_OUTPUT_G_SRC1;
4602 return NI_PFI_OUTPUT_G_GATE1;
4604 return NI_PFI_OUTPUT_AO_UPDATE_N;
4606 return NI_PFI_OUTPUT_AO_START1;
4608 return NI_PFI_OUTPUT_AI_START_PULSE;
4610 return NI_PFI_OUTPUT_G_SRC0;
4612 return NI_PFI_OUTPUT_G_GATE0;
4614 dev_err(dev->class_dev, "bug, unhandled case in switch.\n");
4620 static int ni_old_set_pfi_routing(struct comedi_device *dev,
4621 unsigned chan, unsigned source)
4623 /* pre-m-series boards have fixed signals on pfi pins */
4624 if (source != ni_old_get_pfi_routing(dev, chan))
4629 static unsigned ni_m_series_get_pfi_routing(struct comedi_device *dev,
4632 struct ni_private *devpriv = dev->private;
4633 const unsigned array_offset = chan / 3;
4635 return NI_M_PFI_OUT_SEL_TO_SRC(chan,
4636 devpriv->pfi_output_select_reg[array_offset]);
4639 static int ni_m_series_set_pfi_routing(struct comedi_device *dev,
4640 unsigned chan, unsigned source)
4642 struct ni_private *devpriv = dev->private;
4643 unsigned index = chan / 3;
4644 unsigned short val = devpriv->pfi_output_select_reg[index];
4646 if ((source & 0x1f) != source)
4649 val &= ~NI_M_PFI_OUT_SEL_MASK(chan);
4650 val |= NI_M_PFI_OUT_SEL(chan, source);
4651 ni_writew(dev, val, NI_M_PFI_OUT_SEL_REG(index));
4652 devpriv->pfi_output_select_reg[index] = val;
4657 static unsigned ni_get_pfi_routing(struct comedi_device *dev, unsigned chan)
4659 struct ni_private *devpriv = dev->private;
4661 return (devpriv->is_m_series)
4662 ? ni_m_series_get_pfi_routing(dev, chan)
4663 : ni_old_get_pfi_routing(dev, chan);
4666 static int ni_set_pfi_routing(struct comedi_device *dev, unsigned chan,
4669 struct ni_private *devpriv = dev->private;
4671 return (devpriv->is_m_series)
4672 ? ni_m_series_set_pfi_routing(dev, chan, source)
4673 : ni_old_set_pfi_routing(dev, chan, source);
4676 static int ni_config_filter(struct comedi_device *dev,
4677 unsigned pfi_channel,
4678 enum ni_pfi_filter_select filter)
4680 struct ni_private *devpriv = dev->private;
4683 if (!devpriv->is_m_series)
4686 bits = ni_readl(dev, NI_M_PFI_FILTER_REG);
4687 bits &= ~NI_M_PFI_FILTER_SEL_MASK(pfi_channel);
4688 bits |= NI_M_PFI_FILTER_SEL(pfi_channel, filter);
4689 ni_writel(dev, bits, NI_M_PFI_FILTER_REG);
4693 static int ni_pfi_insn_config(struct comedi_device *dev,
4694 struct comedi_subdevice *s,
4695 struct comedi_insn *insn,
4698 struct ni_private *devpriv = dev->private;
4704 chan = CR_CHAN(insn->chanspec);
4708 ni_set_bits(dev, NISTC_IO_BIDIR_PIN_REG, 1 << chan, 1);
4711 ni_set_bits(dev, NISTC_IO_BIDIR_PIN_REG, 1 << chan, 0);
4713 case INSN_CONFIG_DIO_QUERY:
4715 (devpriv->io_bidirection_pin_reg & (1 << chan)) ?
4716 COMEDI_OUTPUT : COMEDI_INPUT;
4718 case INSN_CONFIG_SET_ROUTING:
4719 return ni_set_pfi_routing(dev, chan, data[1]);
4720 case INSN_CONFIG_GET_ROUTING:
4721 data[1] = ni_get_pfi_routing(dev, chan);
4723 case INSN_CONFIG_FILTER:
4724 return ni_config_filter(dev, chan, data[1]);
4731 static int ni_pfi_insn_bits(struct comedi_device *dev,
4732 struct comedi_subdevice *s,
4733 struct comedi_insn *insn,
4736 struct ni_private *devpriv = dev->private;
4738 if (!devpriv->is_m_series)
4741 if (comedi_dio_update_state(s, data))
4742 ni_writew(dev, s->state, NI_M_PFI_DO_REG);
4744 data[1] = ni_readw(dev, NI_M_PFI_DI_REG);
4749 static int cs5529_wait_for_idle(struct comedi_device *dev)
4751 unsigned short status;
4752 const int timeout = HZ;
4755 for (i = 0; i < timeout; i++) {
4756 status = ni_ao_win_inw(dev, NI67XX_CAL_STATUS_REG);
4757 if ((status & NI67XX_CAL_STATUS_BUSY) == 0)
4759 set_current_state(TASK_INTERRUPTIBLE);
4760 if (schedule_timeout(1))
4764 dev_err(dev->class_dev, "timeout\n");
4770 static void cs5529_command(struct comedi_device *dev, unsigned short value)
4772 static const int timeout = 100;
4775 ni_ao_win_outw(dev, value, NI67XX_CAL_CMD_REG);
4776 /* give time for command to start being serially clocked into cs5529.
4777 * this insures that the NI67XX_CAL_STATUS_BUSY bit will get properly
4778 * set before we exit this function.
4780 for (i = 0; i < timeout; i++) {
4781 if (ni_ao_win_inw(dev, NI67XX_CAL_STATUS_REG) &
4782 NI67XX_CAL_STATUS_BUSY)
4787 dev_err(dev->class_dev,
4788 "possible problem - never saw adc go busy?\n");
4791 static int cs5529_do_conversion(struct comedi_device *dev,
4792 unsigned short *data)
4795 unsigned short status;
4797 cs5529_command(dev, CS5529_CMD_CB | CS5529_CMD_SINGLE_CONV);
4798 retval = cs5529_wait_for_idle(dev);
4800 dev_err(dev->class_dev,
4801 "timeout or signal in cs5529_do_conversion()\n");
4804 status = ni_ao_win_inw(dev, NI67XX_CAL_STATUS_REG);
4805 if (status & NI67XX_CAL_STATUS_OSC_DETECT) {
4806 dev_err(dev->class_dev,
4807 "cs5529 conversion error, status CSS_OSC_DETECT\n");
4810 if (status & NI67XX_CAL_STATUS_OVERRANGE) {
4811 dev_err(dev->class_dev,
4812 "cs5529 conversion error, overrange (ignoring)\n");
4815 *data = ni_ao_win_inw(dev, NI67XX_CAL_DATA_REG);
4816 /* cs5529 returns 16 bit signed data in bipolar mode */
4822 static int cs5529_ai_insn_read(struct comedi_device *dev,
4823 struct comedi_subdevice *s,
4824 struct comedi_insn *insn,
4828 unsigned short sample;
4829 unsigned int channel_select;
4830 const unsigned int INTERNAL_REF = 0x1000;
4832 /* Set calibration adc source. Docs lie, reference select bits 8 to 11
4833 * do nothing. bit 12 seems to chooses internal reference voltage, bit
4834 * 13 causes the adc input to go overrange (maybe reads external reference?) */
4835 if (insn->chanspec & CR_ALT_SOURCE)
4836 channel_select = INTERNAL_REF;
4838 channel_select = CR_CHAN(insn->chanspec);
4839 ni_ao_win_outw(dev, channel_select, NI67XX_AO_CAL_CHAN_SEL_REG);
4841 for (n = 0; n < insn->n; n++) {
4842 retval = cs5529_do_conversion(dev, &sample);
4850 static void cs5529_config_write(struct comedi_device *dev, unsigned int value,
4851 unsigned int reg_select_bits)
4853 ni_ao_win_outw(dev, (value >> 16) & 0xff, NI67XX_CAL_CFG_HI_REG);
4854 ni_ao_win_outw(dev, value & 0xffff, NI67XX_CAL_CFG_LO_REG);
4855 reg_select_bits &= CS5529_CMD_REG_MASK;
4856 cs5529_command(dev, CS5529_CMD_CB | reg_select_bits);
4857 if (cs5529_wait_for_idle(dev))
4858 dev_err(dev->class_dev,
4859 "timeout or signal in %s\n", __func__);
4862 static int init_cs5529(struct comedi_device *dev)
4864 unsigned int config_bits = CS5529_CFG_PORT_FLAG |
4865 CS5529_CFG_WORD_RATE_2180;
4868 /* do self-calibration */
4869 cs5529_config_write(dev, config_bits | CS5529_CFG_CALIB_BOTH_SELF,
4871 /* need to force a conversion for calibration to run */
4872 cs5529_do_conversion(dev, NULL);
4874 /* force gain calibration to 1 */
4875 cs5529_config_write(dev, 0x400000, CS5529_GAIN_REG);
4876 cs5529_config_write(dev, config_bits | CS5529_CFG_CALIB_OFFSET_SELF,
4878 if (cs5529_wait_for_idle(dev))
4879 dev_err(dev->class_dev,
4880 "timeout or signal in %s\n", __func__);
4886 * Find best multiplier/divider to try and get the PLL running at 80 MHz
4887 * given an arbitrary frequency input clock.
4889 static int ni_mseries_get_pll_parameters(unsigned reference_period_ns,
4890 unsigned *freq_divider,
4891 unsigned *freq_multiplier,
4892 unsigned *actual_period_ns)
4895 unsigned best_div = 1;
4897 unsigned best_mult = 1;
4898 static const unsigned pico_per_nano = 1000;
4900 const unsigned reference_picosec = reference_period_ns * pico_per_nano;
4901 /* m-series wants the phased-locked loop to output 80MHz, which is divided by 4 to
4902 * 20 MHz for most timing clocks */
4903 static const unsigned target_picosec = 12500;
4904 static const unsigned fudge_factor_80_to_20Mhz = 4;
4905 int best_period_picosec = 0;
4907 for (div = 1; div <= NI_M_PLL_MAX_DIVISOR; ++div) {
4908 for (mult = 1; mult <= NI_M_PLL_MAX_MULTIPLIER; ++mult) {
4909 unsigned new_period_ps =
4910 (reference_picosec * div) / mult;
4911 if (abs(new_period_ps - target_picosec) <
4912 abs(best_period_picosec - target_picosec)) {
4913 best_period_picosec = new_period_ps;
4919 if (best_period_picosec == 0)
4922 *freq_divider = best_div;
4923 *freq_multiplier = best_mult;
4925 (best_period_picosec * fudge_factor_80_to_20Mhz +
4926 (pico_per_nano / 2)) / pico_per_nano;
4930 static int ni_mseries_set_pll_master_clock(struct comedi_device *dev,
4931 unsigned source, unsigned period_ns)
4933 struct ni_private *devpriv = dev->private;
4934 static const unsigned min_period_ns = 50;
4935 static const unsigned max_period_ns = 1000;
4936 static const unsigned timeout = 1000;
4937 unsigned pll_control_bits;
4938 unsigned freq_divider;
4939 unsigned freq_multiplier;
4944 if (source == NI_MIO_PLL_PXI10_CLOCK)
4946 /* these limits are somewhat arbitrary, but NI advertises 1 to 20MHz range so we'll use that */
4947 if (period_ns < min_period_ns || period_ns > max_period_ns) {
4948 dev_err(dev->class_dev,
4949 "%s: you must specify an input clock frequency between %i and %i nanosec for the phased-lock loop\n",
4950 __func__, min_period_ns, max_period_ns);
4953 devpriv->rtsi_trig_direction_reg &= ~NISTC_RTSI_TRIG_USE_CLK;
4954 ni_stc_writew(dev, devpriv->rtsi_trig_direction_reg,
4955 NISTC_RTSI_TRIG_DIR_REG);
4956 pll_control_bits = NI_M_PLL_CTRL_ENA | NI_M_PLL_CTRL_VCO_MODE_75_150MHZ;
4957 devpriv->clock_and_fout2 |= NI_M_CLK_FOUT2_TIMEBASE1_PLL |
4958 NI_M_CLK_FOUT2_TIMEBASE3_PLL;
4959 devpriv->clock_and_fout2 &= ~NI_M_CLK_FOUT2_PLL_SRC_MASK;
4961 case NI_MIO_PLL_PXI_STAR_TRIGGER_CLOCK:
4962 devpriv->clock_and_fout2 |= NI_M_CLK_FOUT2_PLL_SRC_STAR;
4964 case NI_MIO_PLL_PXI10_CLOCK:
4965 /* pxi clock is 10MHz */
4966 devpriv->clock_and_fout2 |= NI_M_CLK_FOUT2_PLL_SRC_PXI10;
4969 for (rtsi = 0; rtsi <= NI_M_MAX_RTSI_CHAN; ++rtsi) {
4970 if (source == NI_MIO_PLL_RTSI_CLOCK(rtsi)) {
4971 devpriv->clock_and_fout2 |=
4972 NI_M_CLK_FOUT2_PLL_SRC_RTSI(rtsi);
4976 if (rtsi > NI_M_MAX_RTSI_CHAN)
4980 retval = ni_mseries_get_pll_parameters(period_ns,
4983 &devpriv->clock_ns);
4985 dev_err(dev->class_dev,
4986 "bug, failed to find pll parameters\n");
4990 ni_writew(dev, devpriv->clock_and_fout2, NI_M_CLK_FOUT2_REG);
4991 pll_control_bits |= NI_M_PLL_CTRL_DIVISOR(freq_divider) |
4992 NI_M_PLL_CTRL_MULTIPLIER(freq_multiplier);
4994 ni_writew(dev, pll_control_bits, NI_M_PLL_CTRL_REG);
4995 devpriv->clock_source = source;
4996 /* it seems to typically take a few hundred microseconds for PLL to lock */
4997 for (i = 0; i < timeout; ++i) {
4998 if (ni_readw(dev, NI_M_PLL_STATUS_REG) & NI_M_PLL_STATUS_LOCKED)
5003 dev_err(dev->class_dev,
5004 "%s: timed out waiting for PLL to lock to reference clock source %i with period %i ns\n",
5005 __func__, source, period_ns);
5011 static int ni_set_master_clock(struct comedi_device *dev,
5012 unsigned source, unsigned period_ns)
5014 struct ni_private *devpriv = dev->private;
5016 if (source == NI_MIO_INTERNAL_CLOCK) {
5017 devpriv->rtsi_trig_direction_reg &= ~NISTC_RTSI_TRIG_USE_CLK;
5018 ni_stc_writew(dev, devpriv->rtsi_trig_direction_reg,
5019 NISTC_RTSI_TRIG_DIR_REG);
5020 devpriv->clock_ns = TIMEBASE_1_NS;
5021 if (devpriv->is_m_series) {
5022 devpriv->clock_and_fout2 &=
5023 ~(NI_M_CLK_FOUT2_TIMEBASE1_PLL |
5024 NI_M_CLK_FOUT2_TIMEBASE3_PLL);
5025 ni_writew(dev, devpriv->clock_and_fout2,
5026 NI_M_CLK_FOUT2_REG);
5027 ni_writew(dev, 0, NI_M_PLL_CTRL_REG);
5029 devpriv->clock_source = source;
5031 if (devpriv->is_m_series) {
5032 return ni_mseries_set_pll_master_clock(dev, source,
5035 if (source == NI_MIO_RTSI_CLOCK) {
5036 devpriv->rtsi_trig_direction_reg |=
5037 NISTC_RTSI_TRIG_USE_CLK;
5039 devpriv->rtsi_trig_direction_reg,
5040 NISTC_RTSI_TRIG_DIR_REG);
5041 if (period_ns == 0) {
5042 dev_err(dev->class_dev,
5043 "we don't handle an unspecified clock period correctly yet, returning error\n");
5046 devpriv->clock_ns = period_ns;
5047 devpriv->clock_source = source;
5056 static int ni_valid_rtsi_output_source(struct comedi_device *dev,
5057 unsigned chan, unsigned source)
5059 struct ni_private *devpriv = dev->private;
5061 if (chan >= NISTC_RTSI_TRIG_NUM_CHAN(devpriv->is_m_series)) {
5062 if (chan == NISTC_RTSI_TRIG_OLD_CLK_CHAN) {
5063 if (source == NI_RTSI_OUTPUT_RTSI_OSC)
5066 dev_err(dev->class_dev,
5067 "%s: invalid source for channel=%i, channel %i is always the RTSI clock for pre-m-series boards\n",
5068 __func__, chan, NISTC_RTSI_TRIG_OLD_CLK_CHAN);
5074 case NI_RTSI_OUTPUT_ADR_START1:
5075 case NI_RTSI_OUTPUT_ADR_START2:
5076 case NI_RTSI_OUTPUT_SCLKG:
5077 case NI_RTSI_OUTPUT_DACUPDN:
5078 case NI_RTSI_OUTPUT_DA_START1:
5079 case NI_RTSI_OUTPUT_G_SRC0:
5080 case NI_RTSI_OUTPUT_G_GATE0:
5081 case NI_RTSI_OUTPUT_RGOUT0:
5082 case NI_RTSI_OUTPUT_RTSI_BRD_0:
5084 case NI_RTSI_OUTPUT_RTSI_OSC:
5085 return (devpriv->is_m_series) ? 1 : 0;
5091 static int ni_set_rtsi_routing(struct comedi_device *dev,
5092 unsigned chan, unsigned src)
5094 struct ni_private *devpriv = dev->private;
5096 if (ni_valid_rtsi_output_source(dev, chan, src) == 0)
5099 devpriv->rtsi_trig_a_output_reg &= ~NISTC_RTSI_TRIG_MASK(chan);
5100 devpriv->rtsi_trig_a_output_reg |= NISTC_RTSI_TRIG(chan, src);
5101 ni_stc_writew(dev, devpriv->rtsi_trig_a_output_reg,
5102 NISTC_RTSI_TRIGA_OUT_REG);
5103 } else if (chan < 8) {
5104 devpriv->rtsi_trig_b_output_reg &= ~NISTC_RTSI_TRIG_MASK(chan);
5105 devpriv->rtsi_trig_b_output_reg |= NISTC_RTSI_TRIG(chan, src);
5106 ni_stc_writew(dev, devpriv->rtsi_trig_b_output_reg,
5107 NISTC_RTSI_TRIGB_OUT_REG);
5112 static unsigned ni_get_rtsi_routing(struct comedi_device *dev, unsigned chan)
5114 struct ni_private *devpriv = dev->private;
5117 return NISTC_RTSI_TRIG_TO_SRC(chan,
5118 devpriv->rtsi_trig_a_output_reg);
5119 } else if (chan < NISTC_RTSI_TRIG_NUM_CHAN(devpriv->is_m_series)) {
5120 return NISTC_RTSI_TRIG_TO_SRC(chan,
5121 devpriv->rtsi_trig_b_output_reg);
5123 if (chan == NISTC_RTSI_TRIG_OLD_CLK_CHAN)
5124 return NI_RTSI_OUTPUT_RTSI_OSC;
5125 dev_err(dev->class_dev, "bug! should never get here?\n");
5130 static int ni_rtsi_insn_config(struct comedi_device *dev,
5131 struct comedi_subdevice *s,
5132 struct comedi_insn *insn,
5135 struct ni_private *devpriv = dev->private;
5136 unsigned int chan = CR_CHAN(insn->chanspec);
5137 unsigned int max_chan = NISTC_RTSI_TRIG_NUM_CHAN(devpriv->is_m_series);
5140 case INSN_CONFIG_DIO_OUTPUT:
5141 if (chan < max_chan) {
5142 devpriv->rtsi_trig_direction_reg |=
5143 NISTC_RTSI_TRIG_DIR(chan, devpriv->is_m_series);
5144 } else if (chan == NISTC_RTSI_TRIG_OLD_CLK_CHAN) {
5145 devpriv->rtsi_trig_direction_reg |=
5146 NISTC_RTSI_TRIG_DRV_CLK;
5148 ni_stc_writew(dev, devpriv->rtsi_trig_direction_reg,
5149 NISTC_RTSI_TRIG_DIR_REG);
5151 case INSN_CONFIG_DIO_INPUT:
5152 if (chan < max_chan) {
5153 devpriv->rtsi_trig_direction_reg &=
5154 ~NISTC_RTSI_TRIG_DIR(chan, devpriv->is_m_series);
5155 } else if (chan == NISTC_RTSI_TRIG_OLD_CLK_CHAN) {
5156 devpriv->rtsi_trig_direction_reg &=
5157 ~NISTC_RTSI_TRIG_DRV_CLK;
5159 ni_stc_writew(dev, devpriv->rtsi_trig_direction_reg,
5160 NISTC_RTSI_TRIG_DIR_REG);
5162 case INSN_CONFIG_DIO_QUERY:
5163 if (chan < max_chan) {
5165 (devpriv->rtsi_trig_direction_reg &
5166 NISTC_RTSI_TRIG_DIR(chan, devpriv->is_m_series))
5167 ? INSN_CONFIG_DIO_OUTPUT
5168 : INSN_CONFIG_DIO_INPUT;
5169 } else if (chan == NISTC_RTSI_TRIG_OLD_CLK_CHAN) {
5170 data[1] = (devpriv->rtsi_trig_direction_reg &
5171 NISTC_RTSI_TRIG_DRV_CLK)
5172 ? INSN_CONFIG_DIO_OUTPUT
5173 : INSN_CONFIG_DIO_INPUT;
5176 case INSN_CONFIG_SET_CLOCK_SRC:
5177 return ni_set_master_clock(dev, data[1], data[2]);
5178 case INSN_CONFIG_GET_CLOCK_SRC:
5179 data[1] = devpriv->clock_source;
5180 data[2] = devpriv->clock_ns;
5182 case INSN_CONFIG_SET_ROUTING:
5183 return ni_set_rtsi_routing(dev, chan, data[1]);
5184 case INSN_CONFIG_GET_ROUTING:
5185 data[1] = ni_get_rtsi_routing(dev, chan);
5193 static int ni_rtsi_insn_bits(struct comedi_device *dev,
5194 struct comedi_subdevice *s,
5195 struct comedi_insn *insn,
5203 static void ni_rtsi_init(struct comedi_device *dev)
5205 struct ni_private *devpriv = dev->private;
5207 /* Initialises the RTSI bus signal switch to a default state */
5210 * Use 10MHz instead of 20MHz for RTSI clock frequency. Appears
5211 * to have no effect, at least on pxi-6281, which always uses
5212 * 20MHz rtsi clock frequency
5214 devpriv->clock_and_fout2 = NI_M_CLK_FOUT2_RTSI_10MHZ;
5215 /* Set clock mode to internal */
5216 if (ni_set_master_clock(dev, NI_MIO_INTERNAL_CLOCK, 0) < 0)
5217 dev_err(dev->class_dev, "ni_set_master_clock failed, bug?\n");
5218 /* default internal lines routing to RTSI bus lines */
5219 devpriv->rtsi_trig_a_output_reg =
5220 NISTC_RTSI_TRIG(0, NI_RTSI_OUTPUT_ADR_START1) |
5221 NISTC_RTSI_TRIG(1, NI_RTSI_OUTPUT_ADR_START2) |
5222 NISTC_RTSI_TRIG(2, NI_RTSI_OUTPUT_SCLKG) |
5223 NISTC_RTSI_TRIG(3, NI_RTSI_OUTPUT_DACUPDN);
5224 ni_stc_writew(dev, devpriv->rtsi_trig_a_output_reg,
5225 NISTC_RTSI_TRIGA_OUT_REG);
5226 devpriv->rtsi_trig_b_output_reg =
5227 NISTC_RTSI_TRIG(4, NI_RTSI_OUTPUT_DA_START1) |
5228 NISTC_RTSI_TRIG(5, NI_RTSI_OUTPUT_G_SRC0) |
5229 NISTC_RTSI_TRIG(6, NI_RTSI_OUTPUT_G_GATE0);
5230 if (devpriv->is_m_series)
5231 devpriv->rtsi_trig_b_output_reg |=
5232 NISTC_RTSI_TRIG(7, NI_RTSI_OUTPUT_RTSI_OSC);
5233 ni_stc_writew(dev, devpriv->rtsi_trig_b_output_reg,
5234 NISTC_RTSI_TRIGB_OUT_REG);
5237 * Sets the source and direction of the 4 on board lines
5238 * ni_stc_writew(dev, 0, NISTC_RTSI_BOARD_REG);
5243 static int ni_gpct_cmd(struct comedi_device *dev, struct comedi_subdevice *s)
5245 struct ni_gpct *counter = s->private;
5248 retval = ni_request_gpct_mite_channel(dev, counter->counter_index,
5251 dev_err(dev->class_dev,
5252 "no dma channel available for use by counter\n");
5255 ni_tio_acknowledge(counter);
5256 ni_e_series_enable_second_irq(dev, counter->counter_index, 1);
5258 return ni_tio_cmd(dev, s);
5261 static int ni_gpct_cancel(struct comedi_device *dev, struct comedi_subdevice *s)
5263 struct ni_gpct *counter = s->private;
5266 retval = ni_tio_cancel(counter);
5267 ni_e_series_enable_second_irq(dev, counter->counter_index, 0);
5268 ni_release_gpct_mite_channel(dev, counter->counter_index);
5273 static irqreturn_t ni_E_interrupt(int irq, void *d)
5275 struct comedi_device *dev = d;
5276 unsigned short a_status;
5277 unsigned short b_status;
5278 unsigned int ai_mite_status = 0;
5279 unsigned int ao_mite_status = 0;
5280 unsigned long flags;
5282 struct ni_private *devpriv = dev->private;
5287 smp_mb(); /* make sure dev->attached is checked before handler does anything else. */
5289 /* lock to avoid race with comedi_poll */
5290 spin_lock_irqsave(&dev->spinlock, flags);
5291 a_status = ni_stc_readw(dev, NISTC_AI_STATUS1_REG);
5292 b_status = ni_stc_readw(dev, NISTC_AO_STATUS1_REG);
5294 if (devpriv->mite) {
5295 unsigned long flags_too;
5297 spin_lock_irqsave(&devpriv->mite_channel_lock, flags_too);
5298 if (devpriv->ai_mite_chan) {
5299 ai_mite_status = mite_get_status(devpriv->ai_mite_chan);
5300 if (ai_mite_status & CHSR_LINKC)
5302 devpriv->mite->mite_io_addr +
5304 ai_mite_chan->channel));
5306 if (devpriv->ao_mite_chan) {
5307 ao_mite_status = mite_get_status(devpriv->ao_mite_chan);
5308 if (ao_mite_status & CHSR_LINKC)
5310 devpriv->mite->mite_io_addr +
5312 ao_mite_chan->channel));
5314 spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags_too);
5317 ack_a_interrupt(dev, a_status);
5318 ack_b_interrupt(dev, b_status);
5319 if ((a_status & NISTC_AI_STATUS1_INTA) || (ai_mite_status & CHSR_INT))
5320 handle_a_interrupt(dev, a_status, ai_mite_status);
5321 if ((b_status & NISTC_AO_STATUS1_INTB) || (ao_mite_status & CHSR_INT))
5322 handle_b_interrupt(dev, b_status, ao_mite_status);
5323 handle_gpct_interrupt(dev, 0);
5324 handle_gpct_interrupt(dev, 1);
5325 handle_cdio_interrupt(dev);
5327 spin_unlock_irqrestore(&dev->spinlock, flags);
5331 static int ni_alloc_private(struct comedi_device *dev)
5333 struct ni_private *devpriv;
5335 devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
5339 spin_lock_init(&devpriv->window_lock);
5340 spin_lock_init(&devpriv->soft_reg_copy_lock);
5341 spin_lock_init(&devpriv->mite_channel_lock);
5346 static int ni_E_init(struct comedi_device *dev,
5347 unsigned interrupt_pin, unsigned irq_polarity)
5349 const struct ni_board_struct *board = dev->board_ptr;
5350 struct ni_private *devpriv = dev->private;
5351 struct comedi_subdevice *s;
5355 if (board->n_aochan > MAX_N_AO_CHAN) {
5356 dev_err(dev->class_dev, "bug! n_aochan > MAX_N_AO_CHAN\n");
5360 /* initialize clock dividers */
5361 devpriv->clock_and_fout = NISTC_CLK_FOUT_SLOW_DIV2 |
5362 NISTC_CLK_FOUT_SLOW_TIMEBASE |
5363 NISTC_CLK_FOUT_TO_BOARD_DIV2 |
5364 NISTC_CLK_FOUT_TO_BOARD;
5365 if (!devpriv->is_6xxx) {
5366 /* BEAM is this needed for PCI-6143 ?? */
5367 devpriv->clock_and_fout |= (NISTC_CLK_FOUT_AI_OUT_DIV2 |
5368 NISTC_CLK_FOUT_AO_OUT_DIV2);
5370 ni_stc_writew(dev, devpriv->clock_and_fout, NISTC_CLK_FOUT_REG);
5372 ret = comedi_alloc_subdevices(dev, NI_NUM_SUBDEVICES);
5376 /* Analog Input subdevice */
5377 s = &dev->subdevices[NI_AI_SUBDEV];
5378 if (board->n_adchan) {
5379 s->type = COMEDI_SUBD_AI;
5380 s->subdev_flags = SDF_READABLE | SDF_DIFF | SDF_DITHER;
5381 if (!devpriv->is_611x)
5382 s->subdev_flags |= SDF_GROUND | SDF_COMMON | SDF_OTHER;
5383 if (board->ai_maxdata > 0xffff)
5384 s->subdev_flags |= SDF_LSAMPL;
5385 if (devpriv->is_m_series)
5386 s->subdev_flags |= SDF_SOFT_CALIBRATED;
5387 s->n_chan = board->n_adchan;
5388 s->maxdata = board->ai_maxdata;
5389 s->range_table = ni_range_lkup[board->gainlkup];
5390 s->insn_read = ni_ai_insn_read;
5391 s->insn_config = ni_ai_insn_config;
5393 dev->read_subdev = s;
5394 s->subdev_flags |= SDF_CMD_READ;
5395 s->len_chanlist = 512;
5396 s->do_cmdtest = ni_ai_cmdtest;
5397 s->do_cmd = ni_ai_cmd;
5398 s->cancel = ni_ai_reset;
5399 s->poll = ni_ai_poll;
5400 s->munge = ni_ai_munge;
5403 s->async_dma_dir = DMA_FROM_DEVICE;
5406 /* reset the analog input configuration */
5407 ni_ai_reset(dev, s);
5409 s->type = COMEDI_SUBD_UNUSED;
5412 /* Analog Output subdevice */
5413 s = &dev->subdevices[NI_AO_SUBDEV];
5414 if (board->n_aochan) {
5415 s->type = COMEDI_SUBD_AO;
5416 s->subdev_flags = SDF_WRITABLE | SDF_DEGLITCH | SDF_GROUND;
5417 if (devpriv->is_m_series)
5418 s->subdev_flags |= SDF_SOFT_CALIBRATED;
5419 s->n_chan = board->n_aochan;
5420 s->maxdata = board->ao_maxdata;
5421 s->range_table = board->ao_range_table;
5422 s->insn_config = ni_ao_insn_config;
5423 s->insn_write = ni_ao_insn_write;
5425 ret = comedi_alloc_subdev_readback(s);
5430 * Along with the IRQ we need either a FIFO or DMA for
5431 * async command support.
5433 if (dev->irq && (board->ao_fifo_depth || devpriv->mite)) {
5434 dev->write_subdev = s;
5435 s->subdev_flags |= SDF_CMD_WRITE;
5436 s->len_chanlist = s->n_chan;
5437 s->do_cmdtest = ni_ao_cmdtest;
5438 s->do_cmd = ni_ao_cmd;
5439 s->cancel = ni_ao_reset;
5440 if (!devpriv->is_m_series)
5441 s->munge = ni_ao_munge;
5444 s->async_dma_dir = DMA_TO_DEVICE;
5447 if (devpriv->is_67xx)
5448 init_ao_67xx(dev, s);
5450 /* reset the analog output configuration */
5451 ni_ao_reset(dev, s);
5453 s->type = COMEDI_SUBD_UNUSED;
5456 /* Digital I/O subdevice */
5457 s = &dev->subdevices[NI_DIO_SUBDEV];
5458 s->type = COMEDI_SUBD_DIO;
5459 s->subdev_flags = SDF_WRITABLE | SDF_READABLE;
5460 s->n_chan = board->has_32dio_chan ? 32 : 8;
5462 s->range_table = &range_digital;
5463 if (devpriv->is_m_series) {
5464 s->subdev_flags |= SDF_LSAMPL;
5465 s->insn_bits = ni_m_series_dio_insn_bits;
5466 s->insn_config = ni_m_series_dio_insn_config;
5468 s->subdev_flags |= SDF_CMD_WRITE /* | SDF_CMD_READ */;
5469 s->len_chanlist = s->n_chan;
5470 s->do_cmdtest = ni_cdio_cmdtest;
5471 s->do_cmd = ni_cdio_cmd;
5472 s->cancel = ni_cdio_cancel;
5474 /* M-series boards use DMA */
5475 s->async_dma_dir = DMA_BIDIRECTIONAL;
5478 /* reset DIO and set all channels to inputs */
5479 ni_writel(dev, NI_M_CDO_CMD_RESET |
5482 ni_writel(dev, s->io_bits, NI_M_DIO_DIR_REG);
5484 s->insn_bits = ni_dio_insn_bits;
5485 s->insn_config = ni_dio_insn_config;
5487 /* set all channels to inputs */
5488 devpriv->dio_control = NISTC_DIO_CTRL_DIR(s->io_bits);
5489 ni_writew(dev, devpriv->dio_control, NISTC_DIO_CTRL_REG);
5493 s = &dev->subdevices[NI_8255_DIO_SUBDEV];
5494 if (board->has_8255) {
5495 ret = subdev_8255_init(dev, s, ni_8255_callback,
5500 s->type = COMEDI_SUBD_UNUSED;
5503 /* formerly general purpose counter/timer device, but no longer used */
5504 s = &dev->subdevices[NI_UNUSED_SUBDEV];
5505 s->type = COMEDI_SUBD_UNUSED;
5507 /* Calibration subdevice */
5508 s = &dev->subdevices[NI_CALIBRATION_SUBDEV];
5509 s->type = COMEDI_SUBD_CALIB;
5510 s->subdev_flags = SDF_INTERNAL;
5513 if (devpriv->is_m_series) {
5514 /* internal PWM output used for AI nonlinearity calibration */
5515 s->insn_config = ni_m_series_pwm_config;
5517 ni_writel(dev, 0x0, NI_M_CAL_PWM_REG);
5518 } else if (devpriv->is_6143) {
5519 /* internal PWM output used for AI nonlinearity calibration */
5520 s->insn_config = ni_6143_pwm_config;
5522 s->subdev_flags |= SDF_WRITABLE;
5523 s->insn_read = ni_calib_insn_read;
5524 s->insn_write = ni_calib_insn_write;
5526 /* setup the caldacs and find the real n_chan and maxdata */
5527 caldac_setup(dev, s);
5530 /* EEPROM subdevice */
5531 s = &dev->subdevices[NI_EEPROM_SUBDEV];
5532 s->type = COMEDI_SUBD_MEMORY;
5533 s->subdev_flags = SDF_READABLE | SDF_INTERNAL;
5535 if (devpriv->is_m_series) {
5536 s->n_chan = M_SERIES_EEPROM_SIZE;
5537 s->insn_read = ni_m_series_eeprom_insn_read;
5540 s->insn_read = ni_eeprom_insn_read;
5543 /* Digital I/O (PFI) subdevice */
5544 s = &dev->subdevices[NI_PFI_DIO_SUBDEV];
5545 s->type = COMEDI_SUBD_DIO;
5546 s->subdev_flags = SDF_READABLE | SDF_WRITABLE | SDF_INTERNAL;
5548 if (devpriv->is_m_series) {
5550 s->insn_bits = ni_pfi_insn_bits;
5552 ni_writew(dev, s->state, NI_M_PFI_DO_REG);
5553 for (i = 0; i < NUM_PFI_OUTPUT_SELECT_REGS; ++i) {
5554 ni_writew(dev, devpriv->pfi_output_select_reg[i],
5555 NI_M_PFI_OUT_SEL_REG(i));
5560 s->insn_config = ni_pfi_insn_config;
5562 ni_set_bits(dev, NISTC_IO_BIDIR_PIN_REG, ~0, 0);
5564 /* cs5529 calibration adc */
5565 s = &dev->subdevices[NI_CS5529_CALIBRATION_SUBDEV];
5566 if (devpriv->is_67xx) {
5567 s->type = COMEDI_SUBD_AI;
5568 s->subdev_flags = SDF_READABLE | SDF_DIFF | SDF_INTERNAL;
5569 /* one channel for each analog output channel */
5570 s->n_chan = board->n_aochan;
5571 s->maxdata = (1 << 16) - 1;
5572 s->range_table = &range_unknown; /* XXX */
5573 s->insn_read = cs5529_ai_insn_read;
5574 s->insn_config = NULL;
5577 s->type = COMEDI_SUBD_UNUSED;
5581 s = &dev->subdevices[NI_SERIAL_SUBDEV];
5582 s->type = COMEDI_SUBD_SERIAL;
5583 s->subdev_flags = SDF_READABLE | SDF_WRITABLE | SDF_INTERNAL;
5586 s->insn_config = ni_serial_insn_config;
5587 devpriv->serial_interval_ns = 0;
5588 devpriv->serial_hw_mode = 0;
5591 s = &dev->subdevices[NI_RTSI_SUBDEV];
5592 s->type = COMEDI_SUBD_DIO;
5593 s->subdev_flags = SDF_READABLE | SDF_WRITABLE | SDF_INTERNAL;
5596 s->insn_bits = ni_rtsi_insn_bits;
5597 s->insn_config = ni_rtsi_insn_config;
5600 /* allocate and initialize the gpct counter device */
5601 devpriv->counter_dev = ni_gpct_device_construct(dev,
5602 ni_gpct_write_register,
5603 ni_gpct_read_register,
5604 (devpriv->is_m_series)
5605 ? ni_gpct_variant_m_series
5606 : ni_gpct_variant_e_series,
5608 if (!devpriv->counter_dev)
5611 /* Counter (gpct) subdevices */
5612 for (i = 0; i < NUM_GPCT; ++i) {
5613 struct ni_gpct *gpct = &devpriv->counter_dev->counters[i];
5615 /* setup and initialize the counter */
5616 gpct->chip_index = 0;
5617 gpct->counter_index = i;
5618 ni_tio_init_counter(gpct);
5620 s = &dev->subdevices[NI_GPCT_SUBDEV(i)];
5621 s->type = COMEDI_SUBD_COUNTER;
5622 s->subdev_flags = SDF_READABLE | SDF_WRITABLE | SDF_LSAMPL;
5624 s->maxdata = (devpriv->is_m_series) ? 0xffffffff
5626 s->insn_read = ni_tio_insn_read;
5627 s->insn_write = ni_tio_insn_read;
5628 s->insn_config = ni_tio_insn_config;
5630 if (dev->irq && devpriv->mite) {
5631 s->subdev_flags |= SDF_CMD_READ /* | SDF_CMD_WRITE */;
5632 s->len_chanlist = 1;
5633 s->do_cmdtest = ni_tio_cmdtest;
5634 s->do_cmd = ni_gpct_cmd;
5635 s->cancel = ni_gpct_cancel;
5637 s->async_dma_dir = DMA_BIDIRECTIONAL;
5643 /* Frequency output subdevice */
5644 s = &dev->subdevices[NI_FREQ_OUT_SUBDEV];
5645 s->type = COMEDI_SUBD_COUNTER;
5646 s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
5649 s->insn_read = ni_freq_out_insn_read;
5650 s->insn_write = ni_freq_out_insn_write;
5651 s->insn_config = ni_freq_out_insn_config;
5655 (irq_polarity ? NISTC_INT_CTRL_INT_POL : 0) |
5656 (NISTC_INT_CTRL_3PIN_INT & 0) |
5657 NISTC_INT_CTRL_INTA_ENA |
5658 NISTC_INT_CTRL_INTB_ENA |
5659 NISTC_INT_CTRL_INTA_SEL(interrupt_pin) |
5660 NISTC_INT_CTRL_INTB_SEL(interrupt_pin),
5661 NISTC_INT_CTRL_REG);
5665 ni_writeb(dev, devpriv->ai_ao_select_reg, NI_E_DMA_AI_AO_SEL_REG);
5666 ni_writeb(dev, devpriv->g0_g1_select_reg, NI_E_DMA_G0_G1_SEL_REG);
5668 if (devpriv->is_6xxx) {
5669 ni_writeb(dev, 0, NI611X_MAGIC_REG);
5670 } else if (devpriv->is_m_series) {
5673 for (channel = 0; channel < board->n_aochan; ++channel) {
5675 NI_M_AO_WAVEFORM_ORDER_REG(channel));
5677 NI_M_AO_REF_ATTENUATION_REG(channel));
5679 ni_writeb(dev, 0x0, NI_M_AO_CALIB_REG);
5685 static void mio_common_detach(struct comedi_device *dev)
5687 struct ni_private *devpriv = dev->private;
5690 if (devpriv->counter_dev)
5691 ni_gpct_device_destroy(devpriv->counter_dev);