2 * Hardware driver for DAQ-STC based boards
4 * COMEDI - Linux Control and Measurement Device Interface
5 * Copyright (C) 1997-2001 David A. Schleef <ds@schleef.org>
6 * Copyright (C) 2002-2006 Frank Mori Hess <fmhess@users.sourceforge.net>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
20 * This file is meant to be included by another file, e.g.,
21 * ni_atmio.c or ni_pcimio.c.
23 * Interrupt support originally added by Truxton Fulton <trux@truxton.com>
25 * References (ftp://ftp.natinst.com/support/manuals):
26 * 340747b.pdf AT-MIO E series Register Level Programmer Manual
27 * 341079b.pdf PCI E Series RLPM
28 * 340934b.pdf DAQ-STC reference manual
30 * 67xx and 611x registers (ftp://ftp.ni.com/support/daq/mhddk/documentation/)
34 * Other possibly relevant info:
35 * 320517c.pdf User manual (obsolete)
36 * 320517f.pdf User manual (new)
38 * 320906c.pdf maximum signal ratings
39 * 321066a.pdf about 16x
40 * 321791a.pdf discontinuation of at-mio-16e-10 rev. c
41 * 321808a.pdf about at-mio-16e-10 rev P
42 * 321837a.pdf discontinuation of at-mio-16de-10 rev d
43 * 321838a.pdf about at-mio-16de-10 rev N
46 * - the interrupt routine needs to be cleaned up
48 * 2006-02-07: S-Series PCI-6143: Support has been added but is not
49 * fully tested as yet. Terry Barnaby, BEAM Ltd.
52 #include <linux/interrupt.h>
53 #include <linux/sched.h>
54 #include <linux/delay.h>
59 #define NI_TIMEOUT 1000
61 /* Note: this table must match the ai_gain_* definitions */
62 static const short ni_gainlkup[][16] = {
63 [ai_gain_16] = {0, 1, 2, 3, 4, 5, 6, 7,
64 0x100, 0x101, 0x102, 0x103, 0x104, 0x105, 0x106, 0x107},
65 [ai_gain_8] = {1, 2, 4, 7, 0x101, 0x102, 0x104, 0x107},
66 [ai_gain_14] = {1, 2, 3, 4, 5, 6, 7,
67 0x101, 0x102, 0x103, 0x104, 0x105, 0x106, 0x107},
68 [ai_gain_4] = {0, 1, 4, 7},
69 [ai_gain_611x] = {0x00a, 0x00b, 0x001, 0x002,
70 0x003, 0x004, 0x005, 0x006},
71 [ai_gain_622x] = {0, 1, 4, 5},
72 [ai_gain_628x] = {1, 2, 3, 4, 5, 6, 7},
73 [ai_gain_6143] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
76 static const struct comedi_lrange range_ni_E_ai = {
97 static const struct comedi_lrange range_ni_E_ai_limited = {
110 static const struct comedi_lrange range_ni_E_ai_limited14 = {
129 static const struct comedi_lrange range_ni_E_ai_bipolar4 = {
138 static const struct comedi_lrange range_ni_E_ai_611x = {
151 static const struct comedi_lrange range_ni_M_ai_622x = {
160 static const struct comedi_lrange range_ni_M_ai_628x = {
172 static const struct comedi_lrange range_ni_E_ao_ext = {
181 static const struct comedi_lrange *const ni_range_lkup[] = {
182 [ai_gain_16] = &range_ni_E_ai,
183 [ai_gain_8] = &range_ni_E_ai_limited,
184 [ai_gain_14] = &range_ni_E_ai_limited14,
185 [ai_gain_4] = &range_ni_E_ai_bipolar4,
186 [ai_gain_611x] = &range_ni_E_ai_611x,
187 [ai_gain_622x] = &range_ni_M_ai_622x,
188 [ai_gain_628x] = &range_ni_M_ai_628x,
189 [ai_gain_6143] = &range_bipolar5
194 AIMODE_HALF_FULL = 1,
199 enum ni_common_subdevices {
205 NI_CALIBRATION_SUBDEV,
208 NI_CS5529_CALIBRATION_SUBDEV,
217 #define NI_GPCT_SUBDEV(x) (NI_GPCT0_SUBDEV + (x))
219 enum timebase_nanoseconds {
221 TIMEBASE_2_NS = 10000
224 #define SERIAL_DISABLED 0
225 #define SERIAL_600NS 600
226 #define SERIAL_1_2US 1200
227 #define SERIAL_10US 10000
229 static const int num_adc_stages_611x = 3;
231 static void ni_writel(struct comedi_device *dev, unsigned int data, int reg)
234 writel(data, dev->mmio + reg);
236 outl(data, dev->iobase + reg);
239 static void ni_writew(struct comedi_device *dev, unsigned int data, int reg)
242 writew(data, dev->mmio + reg);
244 outw(data, dev->iobase + reg);
247 static void ni_writeb(struct comedi_device *dev, unsigned int data, int reg)
250 writeb(data, dev->mmio + reg);
252 outb(data, dev->iobase + reg);
255 static unsigned int ni_readl(struct comedi_device *dev, int reg)
258 return readl(dev->mmio + reg);
260 return inl(dev->iobase + reg);
263 static unsigned int ni_readw(struct comedi_device *dev, int reg)
266 return readw(dev->mmio + reg);
268 return inw(dev->iobase + reg);
271 static unsigned int ni_readb(struct comedi_device *dev, int reg)
274 return readb(dev->mmio + reg);
276 return inb(dev->iobase + reg);
280 * We automatically take advantage of STC registers that can be
281 * read/written directly in the I/O space of the board.
283 * The AT-MIO and DAQCard devices map the low 8 STC registers to
286 * Most PCIMIO devices also map the low 8 STC registers but the
287 * 611x devices map the read registers to iobase+(addr-1)*2.
288 * For now non-windowed STC access is disabled if a PCIMIO device
289 * is detected (devpriv->mite has been initialized).
291 * The M series devices do not used windowed registers for the
292 * STC registers. The functions below handle the mapping of the
293 * windowed STC registers to the m series register offsets.
297 unsigned int mio_reg;
301 static const struct mio_regmap m_series_stc_write_regmap[] = {
302 [NISTC_INTA_ACK_REG] = { 0x104, 2 },
303 [NISTC_INTB_ACK_REG] = { 0x106, 2 },
304 [NISTC_AI_CMD2_REG] = { 0x108, 2 },
305 [NISTC_AO_CMD2_REG] = { 0x10a, 2 },
306 [NISTC_G0_CMD_REG] = { 0x10c, 2 },
307 [NISTC_G1_CMD_REG] = { 0x10e, 2 },
308 [NISTC_AI_CMD1_REG] = { 0x110, 2 },
309 [NISTC_AO_CMD1_REG] = { 0x112, 2 },
311 * NISTC_DIO_OUT_REG maps to:
312 * { NI_M_DIO_REG, 4 } and { NI_M_SCXI_SER_DO_REG, 1 }
314 [NISTC_DIO_OUT_REG] = { 0, 0 }, /* DOES NOT MAP CLEANLY */
315 [NISTC_DIO_CTRL_REG] = { 0, 0 }, /* DOES NOT MAP CLEANLY */
316 [NISTC_AI_MODE1_REG] = { 0x118, 2 },
317 [NISTC_AI_MODE2_REG] = { 0x11a, 2 },
318 [NISTC_AI_SI_LOADA_REG] = { 0x11c, 4 },
319 [NISTC_AI_SI_LOADB_REG] = { 0x120, 4 },
320 [NISTC_AI_SC_LOADA_REG] = { 0x124, 4 },
321 [NISTC_AI_SC_LOADB_REG] = { 0x128, 4 },
322 [NISTC_AI_SI2_LOADA_REG] = { 0x12c, 4 },
323 [NISTC_AI_SI2_LOADB_REG] = { 0x130, 4 },
324 [NISTC_G0_MODE_REG] = { 0x134, 2 },
325 [NISTC_G1_MODE_REG] = { 0x136, 2 },
326 [NISTC_G0_LOADA_REG] = { 0x138, 4 },
327 [NISTC_G0_LOADB_REG] = { 0x13c, 4 },
328 [NISTC_G1_LOADA_REG] = { 0x140, 4 },
329 [NISTC_G1_LOADB_REG] = { 0x144, 4 },
330 [NISTC_G0_INPUT_SEL_REG] = { 0x148, 2 },
331 [NISTC_G1_INPUT_SEL_REG] = { 0x14a, 2 },
332 [NISTC_AO_MODE1_REG] = { 0x14c, 2 },
333 [NISTC_AO_MODE2_REG] = { 0x14e, 2 },
334 [NISTC_AO_UI_LOADA_REG] = { 0x150, 4 },
335 [NISTC_AO_UI_LOADB_REG] = { 0x154, 4 },
336 [NISTC_AO_BC_LOADA_REG] = { 0x158, 4 },
337 [NISTC_AO_BC_LOADB_REG] = { 0x15c, 4 },
338 [NISTC_AO_UC_LOADA_REG] = { 0x160, 4 },
339 [NISTC_AO_UC_LOADB_REG] = { 0x164, 4 },
340 [NISTC_CLK_FOUT_REG] = { 0x170, 2 },
341 [NISTC_IO_BIDIR_PIN_REG] = { 0x172, 2 },
342 [NISTC_RTSI_TRIG_DIR_REG] = { 0x174, 2 },
343 [NISTC_INT_CTRL_REG] = { 0x176, 2 },
344 [NISTC_AI_OUT_CTRL_REG] = { 0x178, 2 },
345 [NISTC_ATRIG_ETC_REG] = { 0x17a, 2 },
346 [NISTC_AI_START_STOP_REG] = { 0x17c, 2 },
347 [NISTC_AI_TRIG_SEL_REG] = { 0x17e, 2 },
348 [NISTC_AI_DIV_LOADA_REG] = { 0x180, 4 },
349 [NISTC_AO_START_SEL_REG] = { 0x184, 2 },
350 [NISTC_AO_TRIG_SEL_REG] = { 0x186, 2 },
351 [NISTC_G0_AUTOINC_REG] = { 0x188, 2 },
352 [NISTC_G1_AUTOINC_REG] = { 0x18a, 2 },
353 [NISTC_AO_MODE3_REG] = { 0x18c, 2 },
354 [NISTC_RESET_REG] = { 0x190, 2 },
355 [NISTC_INTA_ENA_REG] = { 0x192, 2 },
356 [NISTC_INTA2_ENA_REG] = { 0, 0 }, /* E-Series only */
357 [NISTC_INTB_ENA_REG] = { 0x196, 2 },
358 [NISTC_INTB2_ENA_REG] = { 0, 0 }, /* E-Series only */
359 [NISTC_AI_PERSONAL_REG] = { 0x19a, 2 },
360 [NISTC_AO_PERSONAL_REG] = { 0x19c, 2 },
361 [NISTC_RTSI_TRIGA_OUT_REG] = { 0x19e, 2 },
362 [NISTC_RTSI_TRIGB_OUT_REG] = { 0x1a0, 2 },
363 [NISTC_RTSI_BOARD_REG] = { 0, 0 }, /* Unknown */
364 [NISTC_CFG_MEM_CLR_REG] = { 0x1a4, 2 },
365 [NISTC_ADC_FIFO_CLR_REG] = { 0x1a6, 2 },
366 [NISTC_DAC_FIFO_CLR_REG] = { 0x1a8, 2 },
367 [NISTC_AO_OUT_CTRL_REG] = { 0x1ac, 2 },
368 [NISTC_AI_MODE3_REG] = { 0x1ae, 2 },
371 static void m_series_stc_write(struct comedi_device *dev,
372 unsigned int data, unsigned int reg)
374 const struct mio_regmap *regmap;
376 if (reg < ARRAY_SIZE(m_series_stc_write_regmap)) {
377 regmap = &m_series_stc_write_regmap[reg];
379 dev_warn(dev->class_dev, "%s: unhandled register=0x%x\n",
384 switch (regmap->size) {
386 ni_writel(dev, data, regmap->mio_reg);
389 ni_writew(dev, data, regmap->mio_reg);
392 dev_warn(dev->class_dev, "%s: unmapped register=0x%x\n",
398 static const struct mio_regmap m_series_stc_read_regmap[] = {
399 [NISTC_AI_STATUS1_REG] = { 0x104, 2 },
400 [NISTC_AO_STATUS1_REG] = { 0x106, 2 },
401 [NISTC_G01_STATUS_REG] = { 0x108, 2 },
402 [NISTC_AI_STATUS2_REG] = { 0, 0 }, /* Unknown */
403 [NISTC_AO_STATUS2_REG] = { 0x10c, 2 },
404 [NISTC_DIO_IN_REG] = { 0, 0 }, /* Unknown */
405 [NISTC_G0_HW_SAVE_REG] = { 0x110, 4 },
406 [NISTC_G1_HW_SAVE_REG] = { 0x114, 4 },
407 [NISTC_G0_SAVE_REG] = { 0x118, 4 },
408 [NISTC_G1_SAVE_REG] = { 0x11c, 4 },
409 [NISTC_AO_UI_SAVE_REG] = { 0x120, 4 },
410 [NISTC_AO_BC_SAVE_REG] = { 0x124, 4 },
411 [NISTC_AO_UC_SAVE_REG] = { 0x128, 4 },
412 [NISTC_STATUS1_REG] = { 0x136, 2 },
413 [NISTC_DIO_SERIAL_IN_REG] = { 0x009, 1 },
414 [NISTC_STATUS2_REG] = { 0x13a, 2 },
415 [NISTC_AI_SI_SAVE_REG] = { 0x180, 4 },
416 [NISTC_AI_SC_SAVE_REG] = { 0x184, 4 },
419 static unsigned int m_series_stc_read(struct comedi_device *dev,
422 const struct mio_regmap *regmap;
424 if (reg < ARRAY_SIZE(m_series_stc_read_regmap)) {
425 regmap = &m_series_stc_read_regmap[reg];
427 dev_warn(dev->class_dev, "%s: unhandled register=0x%x\n",
432 switch (regmap->size) {
434 return ni_readl(dev, regmap->mio_reg);
436 return ni_readw(dev, regmap->mio_reg);
438 return ni_readb(dev, regmap->mio_reg);
440 dev_warn(dev->class_dev, "%s: unmapped register=0x%x\n",
446 static void ni_stc_writew(struct comedi_device *dev,
447 unsigned int data, int reg)
449 struct ni_private *devpriv = dev->private;
452 if (devpriv->is_m_series) {
453 m_series_stc_write(dev, data, reg);
455 spin_lock_irqsave(&devpriv->window_lock, flags);
456 if (!devpriv->mite && reg < 8) {
457 ni_writew(dev, data, reg * 2);
459 ni_writew(dev, reg, NI_E_STC_WINDOW_ADDR_REG);
460 ni_writew(dev, data, NI_E_STC_WINDOW_DATA_REG);
462 spin_unlock_irqrestore(&devpriv->window_lock, flags);
466 static void ni_stc_writel(struct comedi_device *dev,
467 unsigned int data, int reg)
469 struct ni_private *devpriv = dev->private;
471 if (devpriv->is_m_series) {
472 m_series_stc_write(dev, data, reg);
474 ni_stc_writew(dev, data >> 16, reg);
475 ni_stc_writew(dev, data & 0xffff, reg + 1);
479 static unsigned int ni_stc_readw(struct comedi_device *dev, int reg)
481 struct ni_private *devpriv = dev->private;
485 if (devpriv->is_m_series) {
486 val = m_series_stc_read(dev, reg);
488 spin_lock_irqsave(&devpriv->window_lock, flags);
489 if (!devpriv->mite && reg < 8) {
490 val = ni_readw(dev, reg * 2);
492 ni_writew(dev, reg, NI_E_STC_WINDOW_ADDR_REG);
493 val = ni_readw(dev, NI_E_STC_WINDOW_DATA_REG);
495 spin_unlock_irqrestore(&devpriv->window_lock, flags);
500 static unsigned int ni_stc_readl(struct comedi_device *dev, int reg)
502 struct ni_private *devpriv = dev->private;
505 if (devpriv->is_m_series) {
506 val = m_series_stc_read(dev, reg);
508 val = ni_stc_readw(dev, reg) << 16;
509 val |= ni_stc_readw(dev, reg + 1);
514 static inline void ni_set_bitfield(struct comedi_device *dev, int reg,
515 unsigned int bit_mask,
516 unsigned int bit_values)
518 struct ni_private *devpriv = dev->private;
521 spin_lock_irqsave(&devpriv->soft_reg_copy_lock, flags);
523 case NISTC_INTA_ENA_REG:
524 devpriv->int_a_enable_reg &= ~bit_mask;
525 devpriv->int_a_enable_reg |= bit_values & bit_mask;
526 ni_stc_writew(dev, devpriv->int_a_enable_reg, reg);
528 case NISTC_INTB_ENA_REG:
529 devpriv->int_b_enable_reg &= ~bit_mask;
530 devpriv->int_b_enable_reg |= bit_values & bit_mask;
531 ni_stc_writew(dev, devpriv->int_b_enable_reg, reg);
533 case NISTC_IO_BIDIR_PIN_REG:
534 devpriv->io_bidirection_pin_reg &= ~bit_mask;
535 devpriv->io_bidirection_pin_reg |= bit_values & bit_mask;
536 ni_stc_writew(dev, devpriv->io_bidirection_pin_reg, reg);
538 case NI_E_DMA_AI_AO_SEL_REG:
539 devpriv->ai_ao_select_reg &= ~bit_mask;
540 devpriv->ai_ao_select_reg |= bit_values & bit_mask;
541 ni_writeb(dev, devpriv->ai_ao_select_reg, reg);
543 case NI_E_DMA_G0_G1_SEL_REG:
544 devpriv->g0_g1_select_reg &= ~bit_mask;
545 devpriv->g0_g1_select_reg |= bit_values & bit_mask;
546 ni_writeb(dev, devpriv->g0_g1_select_reg, reg);
548 case NI_M_CDIO_DMA_SEL_REG:
549 devpriv->cdio_dma_select_reg &= ~bit_mask;
550 devpriv->cdio_dma_select_reg |= bit_values & bit_mask;
551 ni_writeb(dev, devpriv->cdio_dma_select_reg, reg);
554 dev_err(dev->class_dev, "called with invalid register %d\n",
559 spin_unlock_irqrestore(&devpriv->soft_reg_copy_lock, flags);
564 /* selects the MITE channel to use for DMA */
565 #define NI_STC_DMA_CHAN_SEL(x) (((x) < 4) ? BIT(x) : \
567 ((x) == 5) ? 0x5 : 0x0)
569 /* DMA channel setup */
570 static int ni_request_ai_mite_channel(struct comedi_device *dev)
572 struct ni_private *devpriv = dev->private;
573 struct mite_channel *mite_chan;
577 spin_lock_irqsave(&devpriv->mite_channel_lock, flags);
578 mite_chan = mite_request_channel(devpriv->mite, devpriv->ai_mite_ring);
580 spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
581 dev_err(dev->class_dev,
582 "failed to reserve mite dma channel for analog input\n");
585 mite_chan->dir = COMEDI_INPUT;
586 devpriv->ai_mite_chan = mite_chan;
588 bits = NI_STC_DMA_CHAN_SEL(mite_chan->channel);
589 ni_set_bitfield(dev, NI_E_DMA_AI_AO_SEL_REG,
590 NI_E_DMA_AI_SEL_MASK, NI_E_DMA_AI_SEL(bits));
592 spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
596 static int ni_request_ao_mite_channel(struct comedi_device *dev)
598 struct ni_private *devpriv = dev->private;
599 struct mite_channel *mite_chan;
603 spin_lock_irqsave(&devpriv->mite_channel_lock, flags);
604 mite_chan = mite_request_channel(devpriv->mite, devpriv->ao_mite_ring);
606 spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
607 dev_err(dev->class_dev,
608 "failed to reserve mite dma channel for analog outut\n");
611 mite_chan->dir = COMEDI_OUTPUT;
612 devpriv->ao_mite_chan = mite_chan;
614 bits = NI_STC_DMA_CHAN_SEL(mite_chan->channel);
615 ni_set_bitfield(dev, NI_E_DMA_AI_AO_SEL_REG,
616 NI_E_DMA_AO_SEL_MASK, NI_E_DMA_AO_SEL(bits));
618 spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
622 static int ni_request_gpct_mite_channel(struct comedi_device *dev,
623 unsigned int gpct_index,
624 enum comedi_io_direction direction)
626 struct ni_private *devpriv = dev->private;
627 struct ni_gpct *counter = &devpriv->counter_dev->counters[gpct_index];
628 struct mite_channel *mite_chan;
632 spin_lock_irqsave(&devpriv->mite_channel_lock, flags);
633 mite_chan = mite_request_channel(devpriv->mite,
634 devpriv->gpct_mite_ring[gpct_index]);
636 spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
637 dev_err(dev->class_dev,
638 "failed to reserve mite dma channel for counter\n");
641 mite_chan->dir = direction;
642 ni_tio_set_mite_channel(counter, mite_chan);
644 bits = NI_STC_DMA_CHAN_SEL(mite_chan->channel);
645 ni_set_bitfield(dev, NI_E_DMA_G0_G1_SEL_REG,
646 NI_E_DMA_G0_G1_SEL_MASK(gpct_index),
647 NI_E_DMA_G0_G1_SEL(gpct_index, bits));
649 spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
653 static int ni_request_cdo_mite_channel(struct comedi_device *dev)
655 struct ni_private *devpriv = dev->private;
656 struct mite_channel *mite_chan;
660 spin_lock_irqsave(&devpriv->mite_channel_lock, flags);
661 mite_chan = mite_request_channel(devpriv->mite, devpriv->cdo_mite_ring);
663 spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
664 dev_err(dev->class_dev,
665 "failed to reserve mite dma channel for correlated digital output\n");
668 mite_chan->dir = COMEDI_OUTPUT;
669 devpriv->cdo_mite_chan = mite_chan;
672 * XXX just guessing NI_STC_DMA_CHAN_SEL()
673 * returns the right bits, under the assumption the cdio dma
674 * selection works just like ai/ao/gpct.
675 * Definitely works for dma channels 0 and 1.
677 bits = NI_STC_DMA_CHAN_SEL(mite_chan->channel);
678 ni_set_bitfield(dev, NI_M_CDIO_DMA_SEL_REG,
679 NI_M_CDIO_DMA_SEL_CDO_MASK,
680 NI_M_CDIO_DMA_SEL_CDO(bits));
682 spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
687 static void ni_release_ai_mite_channel(struct comedi_device *dev)
690 struct ni_private *devpriv = dev->private;
693 spin_lock_irqsave(&devpriv->mite_channel_lock, flags);
694 if (devpriv->ai_mite_chan) {
695 ni_set_bitfield(dev, NI_E_DMA_AI_AO_SEL_REG,
696 NI_E_DMA_AI_SEL_MASK, 0);
697 mite_release_channel(devpriv->ai_mite_chan);
698 devpriv->ai_mite_chan = NULL;
700 spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
704 static void ni_release_ao_mite_channel(struct comedi_device *dev)
707 struct ni_private *devpriv = dev->private;
710 spin_lock_irqsave(&devpriv->mite_channel_lock, flags);
711 if (devpriv->ao_mite_chan) {
712 ni_set_bitfield(dev, NI_E_DMA_AI_AO_SEL_REG,
713 NI_E_DMA_AO_SEL_MASK, 0);
714 mite_release_channel(devpriv->ao_mite_chan);
715 devpriv->ao_mite_chan = NULL;
717 spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
722 static void ni_release_gpct_mite_channel(struct comedi_device *dev,
723 unsigned int gpct_index)
725 struct ni_private *devpriv = dev->private;
728 spin_lock_irqsave(&devpriv->mite_channel_lock, flags);
729 if (devpriv->counter_dev->counters[gpct_index].mite_chan) {
730 struct mite_channel *mite_chan =
731 devpriv->counter_dev->counters[gpct_index].mite_chan;
733 ni_set_bitfield(dev, NI_E_DMA_G0_G1_SEL_REG,
734 NI_E_DMA_G0_G1_SEL_MASK(gpct_index), 0);
735 ni_tio_set_mite_channel(&devpriv->
736 counter_dev->counters[gpct_index],
738 mite_release_channel(mite_chan);
740 spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
743 static void ni_release_cdo_mite_channel(struct comedi_device *dev)
745 struct ni_private *devpriv = dev->private;
748 spin_lock_irqsave(&devpriv->mite_channel_lock, flags);
749 if (devpriv->cdo_mite_chan) {
750 ni_set_bitfield(dev, NI_M_CDIO_DMA_SEL_REG,
751 NI_M_CDIO_DMA_SEL_CDO_MASK, 0);
752 mite_release_channel(devpriv->cdo_mite_chan);
753 devpriv->cdo_mite_chan = NULL;
755 spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
758 static void ni_e_series_enable_second_irq(struct comedi_device *dev,
759 unsigned int gpct_index, short enable)
761 struct ni_private *devpriv = dev->private;
762 unsigned int val = 0;
765 if (devpriv->is_m_series || gpct_index > 1)
769 * e-series boards use the second irq signals to generate
770 * dma requests for their counters
772 if (gpct_index == 0) {
773 reg = NISTC_INTA2_ENA_REG;
775 val = NISTC_INTA_ENA_G0_GATE;
777 reg = NISTC_INTB2_ENA_REG;
779 val = NISTC_INTB_ENA_G1_GATE;
781 ni_stc_writew(dev, val, reg);
785 static void ni_clear_ai_fifo(struct comedi_device *dev)
787 struct ni_private *devpriv = dev->private;
788 static const int timeout = 10000;
791 if (devpriv->is_6143) {
792 /* Flush the 6143 data FIFO */
793 ni_writel(dev, 0x10, NI6143_AI_FIFO_CTRL_REG);
794 ni_writel(dev, 0x00, NI6143_AI_FIFO_CTRL_REG);
795 /* Wait for complete */
796 for (i = 0; i < timeout; i++) {
797 if (!(ni_readl(dev, NI6143_AI_FIFO_STATUS_REG) & 0x10))
802 dev_err(dev->class_dev, "FIFO flush timeout\n");
804 ni_stc_writew(dev, 1, NISTC_ADC_FIFO_CLR_REG);
805 if (devpriv->is_625x) {
806 ni_writeb(dev, 0, NI_M_STATIC_AI_CTRL_REG(0));
807 ni_writeb(dev, 1, NI_M_STATIC_AI_CTRL_REG(0));
810 * The NI example code does 3 convert pulses for 625x
811 * boards, But that appears to be wrong in practice.
813 ni_stc_writew(dev, NISTC_AI_CMD1_CONVERT_PULSE,
815 ni_stc_writew(dev, NISTC_AI_CMD1_CONVERT_PULSE,
817 ni_stc_writew(dev, NISTC_AI_CMD1_CONVERT_PULSE,
824 static inline void ni_ao_win_outw(struct comedi_device *dev,
825 unsigned int data, int addr)
827 struct ni_private *devpriv = dev->private;
830 spin_lock_irqsave(&devpriv->window_lock, flags);
831 ni_writew(dev, addr, NI611X_AO_WINDOW_ADDR_REG);
832 ni_writew(dev, data, NI611X_AO_WINDOW_DATA_REG);
833 spin_unlock_irqrestore(&devpriv->window_lock, flags);
836 static inline void ni_ao_win_outl(struct comedi_device *dev,
837 unsigned int data, int addr)
839 struct ni_private *devpriv = dev->private;
842 spin_lock_irqsave(&devpriv->window_lock, flags);
843 ni_writew(dev, addr, NI611X_AO_WINDOW_ADDR_REG);
844 ni_writel(dev, data, NI611X_AO_WINDOW_DATA_REG);
845 spin_unlock_irqrestore(&devpriv->window_lock, flags);
848 static inline unsigned short ni_ao_win_inw(struct comedi_device *dev, int addr)
850 struct ni_private *devpriv = dev->private;
854 spin_lock_irqsave(&devpriv->window_lock, flags);
855 ni_writew(dev, addr, NI611X_AO_WINDOW_ADDR_REG);
856 data = ni_readw(dev, NI611X_AO_WINDOW_DATA_REG);
857 spin_unlock_irqrestore(&devpriv->window_lock, flags);
862 * ni_set_bits( ) allows different parts of the ni_mio_common driver to
863 * share registers (such as Interrupt_A_Register) without interfering with
866 * NOTE: the switch/case statements are optimized out for a constant argument
867 * so this is actually quite fast--- If you must wrap another function around
868 * this make it inline to avoid a large speed penalty.
870 * value should only be 1 or 0.
872 static inline void ni_set_bits(struct comedi_device *dev, int reg,
873 unsigned int bits, unsigned int value)
875 unsigned int bit_values;
881 ni_set_bitfield(dev, reg, bits, bit_values);
885 static void ni_sync_ai_dma(struct comedi_device *dev)
887 struct ni_private *devpriv = dev->private;
888 struct comedi_subdevice *s = dev->read_subdev;
891 spin_lock_irqsave(&devpriv->mite_channel_lock, flags);
892 if (devpriv->ai_mite_chan)
893 mite_sync_dma(devpriv->ai_mite_chan, s);
894 spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
897 static int ni_ai_drain_dma(struct comedi_device *dev)
899 struct ni_private *devpriv = dev->private;
901 static const int timeout = 10000;
905 spin_lock_irqsave(&devpriv->mite_channel_lock, flags);
906 if (devpriv->ai_mite_chan) {
907 for (i = 0; i < timeout; i++) {
908 if ((ni_stc_readw(dev, NISTC_AI_STATUS1_REG) &
909 NISTC_AI_STATUS1_FIFO_E) &&
910 mite_bytes_in_transit(devpriv->ai_mite_chan) == 0)
915 dev_err(dev->class_dev, "timed out\n");
916 dev_err(dev->class_dev,
917 "mite_bytes_in_transit=%i, AI_Status1_Register=0x%x\n",
918 mite_bytes_in_transit(devpriv->ai_mite_chan),
919 ni_stc_readw(dev, NISTC_AI_STATUS1_REG));
923 spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
930 static int ni_ao_wait_for_dma_load(struct comedi_device *dev)
932 static const int timeout = 10000;
935 for (i = 0; i < timeout; i++) {
936 unsigned short b_status;
938 b_status = ni_stc_readw(dev, NISTC_AO_STATUS1_REG);
939 if (b_status & NISTC_AO_STATUS1_FIFO_HF)
942 * If we poll too often, the pci bus activity seems
943 * to slow the dma transfer down.
945 usleep_range(10, 100);
948 dev_err(dev->class_dev, "timed out waiting for dma load\n");
957 static void ni_ao_fifo_load(struct comedi_device *dev,
958 struct comedi_subdevice *s, int n)
960 struct ni_private *devpriv = dev->private;
963 unsigned int packed_data;
965 for (i = 0; i < n; i++) {
966 comedi_buf_read_samples(s, &d, 1);
968 if (devpriv->is_6xxx) {
969 packed_data = d & 0xffff;
970 /* 6711 only has 16 bit wide ao fifo */
971 if (!devpriv->is_6711) {
972 comedi_buf_read_samples(s, &d, 1);
974 packed_data |= (d << 16) & 0xffff0000;
976 ni_writel(dev, packed_data, NI611X_AO_FIFO_DATA_REG);
978 ni_writew(dev, d, NI_E_AO_FIFO_DATA_REG);
984 * There's a small problem if the FIFO gets really low and we
985 * don't have the data to fill it. Basically, if after we fill
986 * the FIFO with all the data available, the FIFO is _still_
987 * less than half full, we never clear the interrupt. If the
988 * IRQ is in edge mode, we never get another interrupt, because
989 * this one wasn't cleared. If in level mode, we get flooded
990 * with interrupts that we can't fulfill, because nothing ever
991 * gets put into the buffer.
993 * This kind of situation is recoverable, but it is easier to
994 * just pretend we had a FIFO underrun, since there is a good
995 * chance it will happen anyway. This is _not_ the case for
996 * RT code, as RT code might purposely be running close to the
997 * metal. Needs to be fixed eventually.
999 static int ni_ao_fifo_half_empty(struct comedi_device *dev,
1000 struct comedi_subdevice *s)
1002 const struct ni_board_struct *board = dev->board_ptr;
1003 unsigned int nbytes;
1004 unsigned int nsamples;
1006 nbytes = comedi_buf_read_n_available(s);
1008 s->async->events |= COMEDI_CB_OVERFLOW;
1012 nsamples = comedi_bytes_to_samples(s, nbytes);
1013 if (nsamples > board->ao_fifo_depth / 2)
1014 nsamples = board->ao_fifo_depth / 2;
1016 ni_ao_fifo_load(dev, s, nsamples);
1021 static int ni_ao_prep_fifo(struct comedi_device *dev,
1022 struct comedi_subdevice *s)
1024 const struct ni_board_struct *board = dev->board_ptr;
1025 struct ni_private *devpriv = dev->private;
1026 unsigned int nbytes;
1027 unsigned int nsamples;
1030 ni_stc_writew(dev, 1, NISTC_DAC_FIFO_CLR_REG);
1031 if (devpriv->is_6xxx)
1032 ni_ao_win_outl(dev, 0x6, NI611X_AO_FIFO_OFFSET_LOAD_REG);
1034 /* load some data */
1035 nbytes = comedi_buf_read_n_available(s);
1039 nsamples = comedi_bytes_to_samples(s, nbytes);
1040 if (nsamples > board->ao_fifo_depth)
1041 nsamples = board->ao_fifo_depth;
1043 ni_ao_fifo_load(dev, s, nsamples);
1048 static void ni_ai_fifo_read(struct comedi_device *dev,
1049 struct comedi_subdevice *s, int n)
1051 struct ni_private *devpriv = dev->private;
1052 struct comedi_async *async = s->async;
1054 unsigned short data;
1057 if (devpriv->is_611x) {
1058 for (i = 0; i < n / 2; i++) {
1059 dl = ni_readl(dev, NI611X_AI_FIFO_DATA_REG);
1060 /* This may get the hi/lo data in the wrong order */
1061 data = (dl >> 16) & 0xffff;
1062 comedi_buf_write_samples(s, &data, 1);
1064 comedi_buf_write_samples(s, &data, 1);
1066 /* Check if there's a single sample stuck in the FIFO */
1068 dl = ni_readl(dev, NI611X_AI_FIFO_DATA_REG);
1070 comedi_buf_write_samples(s, &data, 1);
1072 } else if (devpriv->is_6143) {
1074 * This just reads the FIFO assuming the data is present,
1075 * no checks on the FIFO status are performed.
1077 for (i = 0; i < n / 2; i++) {
1078 dl = ni_readl(dev, NI6143_AI_FIFO_DATA_REG);
1080 data = (dl >> 16) & 0xffff;
1081 comedi_buf_write_samples(s, &data, 1);
1083 comedi_buf_write_samples(s, &data, 1);
1086 /* Assume there is a single sample stuck in the FIFO */
1087 /* Get stranded sample into FIFO */
1088 ni_writel(dev, 0x01, NI6143_AI_FIFO_CTRL_REG);
1089 dl = ni_readl(dev, NI6143_AI_FIFO_DATA_REG);
1090 data = (dl >> 16) & 0xffff;
1091 comedi_buf_write_samples(s, &data, 1);
1094 if (n > ARRAY_SIZE(devpriv->ai_fifo_buffer)) {
1095 dev_err(dev->class_dev,
1096 "bug! ai_fifo_buffer too small\n");
1097 async->events |= COMEDI_CB_ERROR;
1100 for (i = 0; i < n; i++) {
1101 devpriv->ai_fifo_buffer[i] =
1102 ni_readw(dev, NI_E_AI_FIFO_DATA_REG);
1104 comedi_buf_write_samples(s, devpriv->ai_fifo_buffer, n);
1108 static void ni_handle_fifo_half_full(struct comedi_device *dev)
1110 const struct ni_board_struct *board = dev->board_ptr;
1111 struct comedi_subdevice *s = dev->read_subdev;
1114 n = board->ai_fifo_depth / 2;
1116 ni_ai_fifo_read(dev, s, n);
1120 /* Empties the AI fifo */
1121 static void ni_handle_fifo_dregs(struct comedi_device *dev)
1123 struct ni_private *devpriv = dev->private;
1124 struct comedi_subdevice *s = dev->read_subdev;
1126 unsigned short data;
1129 if (devpriv->is_611x) {
1130 while ((ni_stc_readw(dev, NISTC_AI_STATUS1_REG) &
1131 NISTC_AI_STATUS1_FIFO_E) == 0) {
1132 dl = ni_readl(dev, NI611X_AI_FIFO_DATA_REG);
1134 /* This may get the hi/lo data in the wrong order */
1136 comedi_buf_write_samples(s, &data, 1);
1138 comedi_buf_write_samples(s, &data, 1);
1140 } else if (devpriv->is_6143) {
1142 while (ni_readl(dev, NI6143_AI_FIFO_STATUS_REG) & 0x04) {
1143 dl = ni_readl(dev, NI6143_AI_FIFO_DATA_REG);
1145 /* This may get the hi/lo data in the wrong order */
1147 comedi_buf_write_samples(s, &data, 1);
1149 comedi_buf_write_samples(s, &data, 1);
1152 /* Check if stranded sample is present */
1153 if (ni_readl(dev, NI6143_AI_FIFO_STATUS_REG) & 0x01) {
1154 /* Get stranded sample into FIFO */
1155 ni_writel(dev, 0x01, NI6143_AI_FIFO_CTRL_REG);
1156 dl = ni_readl(dev, NI6143_AI_FIFO_DATA_REG);
1157 data = (dl >> 16) & 0xffff;
1158 comedi_buf_write_samples(s, &data, 1);
1162 unsigned short fe; /* fifo empty */
1164 fe = ni_stc_readw(dev, NISTC_AI_STATUS1_REG) &
1165 NISTC_AI_STATUS1_FIFO_E;
1168 i < ARRAY_SIZE(devpriv->ai_fifo_buffer); i++) {
1169 fe = ni_stc_readw(dev, NISTC_AI_STATUS1_REG) &
1170 NISTC_AI_STATUS1_FIFO_E;
1173 devpriv->ai_fifo_buffer[i] =
1174 ni_readw(dev, NI_E_AI_FIFO_DATA_REG);
1176 comedi_buf_write_samples(s, devpriv->ai_fifo_buffer, i);
1181 static void get_last_sample_611x(struct comedi_device *dev)
1183 struct ni_private *devpriv = dev->private;
1184 struct comedi_subdevice *s = dev->read_subdev;
1185 unsigned short data;
1188 if (!devpriv->is_611x)
1191 /* Check if there's a single sample stuck in the FIFO */
1192 if (ni_readb(dev, NI_E_STATUS_REG) & 0x80) {
1193 dl = ni_readl(dev, NI611X_AI_FIFO_DATA_REG);
1195 comedi_buf_write_samples(s, &data, 1);
1199 static void get_last_sample_6143(struct comedi_device *dev)
1201 struct ni_private *devpriv = dev->private;
1202 struct comedi_subdevice *s = dev->read_subdev;
1203 unsigned short data;
1206 if (!devpriv->is_6143)
1209 /* Check if there's a single sample stuck in the FIFO */
1210 if (ni_readl(dev, NI6143_AI_FIFO_STATUS_REG) & 0x01) {
1211 /* Get stranded sample into FIFO */
1212 ni_writel(dev, 0x01, NI6143_AI_FIFO_CTRL_REG);
1213 dl = ni_readl(dev, NI6143_AI_FIFO_DATA_REG);
1215 /* This may get the hi/lo data in the wrong order */
1216 data = (dl >> 16) & 0xffff;
1217 comedi_buf_write_samples(s, &data, 1);
1221 static void shutdown_ai_command(struct comedi_device *dev)
1223 struct comedi_subdevice *s = dev->read_subdev;
1226 ni_ai_drain_dma(dev);
1228 ni_handle_fifo_dregs(dev);
1229 get_last_sample_611x(dev);
1230 get_last_sample_6143(dev);
1232 s->async->events |= COMEDI_CB_EOA;
1235 static void ni_handle_eos(struct comedi_device *dev, struct comedi_subdevice *s)
1237 struct ni_private *devpriv = dev->private;
1239 if (devpriv->aimode == AIMODE_SCAN) {
1241 static const int timeout = 10;
1244 for (i = 0; i < timeout; i++) {
1245 ni_sync_ai_dma(dev);
1246 if ((s->async->events & COMEDI_CB_EOS))
1251 ni_handle_fifo_dregs(dev);
1252 s->async->events |= COMEDI_CB_EOS;
1255 /* handle special case of single scan */
1256 if (devpriv->ai_cmd2 & NISTC_AI_CMD2_END_ON_EOS)
1257 shutdown_ai_command(dev);
1260 static void handle_gpct_interrupt(struct comedi_device *dev,
1261 unsigned short counter_index)
1264 struct ni_private *devpriv = dev->private;
1265 struct comedi_subdevice *s;
1267 s = &dev->subdevices[NI_GPCT_SUBDEV(counter_index)];
1269 ni_tio_handle_interrupt(&devpriv->counter_dev->counters[counter_index],
1271 comedi_handle_events(dev, s);
1275 static void ack_a_interrupt(struct comedi_device *dev, unsigned short a_status)
1277 unsigned short ack = 0;
1279 if (a_status & NISTC_AI_STATUS1_SC_TC)
1280 ack |= NISTC_INTA_ACK_AI_SC_TC;
1281 if (a_status & NISTC_AI_STATUS1_START1)
1282 ack |= NISTC_INTA_ACK_AI_START1;
1283 if (a_status & NISTC_AI_STATUS1_START)
1284 ack |= NISTC_INTA_ACK_AI_START;
1285 if (a_status & NISTC_AI_STATUS1_STOP)
1286 ack |= NISTC_INTA_ACK_AI_STOP;
1287 if (a_status & NISTC_AI_STATUS1_OVER)
1288 ack |= NISTC_INTA_ACK_AI_ERR;
1290 ni_stc_writew(dev, ack, NISTC_INTA_ACK_REG);
1293 static void handle_a_interrupt(struct comedi_device *dev,
1294 struct comedi_subdevice *s,
1295 unsigned short status)
1297 struct comedi_cmd *cmd = &s->async->cmd;
1299 /* test for all uncommon interrupt events at the same time */
1300 if (status & (NISTC_AI_STATUS1_ERR |
1301 NISTC_AI_STATUS1_SC_TC | NISTC_AI_STATUS1_START1)) {
1302 if (status == 0xffff) {
1303 dev_err(dev->class_dev, "Card removed?\n");
1305 * We probably aren't even running a command now,
1306 * so it's a good idea to be careful.
1308 if (comedi_is_subdevice_running(s))
1309 s->async->events |= COMEDI_CB_ERROR;
1312 if (status & NISTC_AI_STATUS1_ERR) {
1313 dev_err(dev->class_dev, "ai error a_status=%04x\n",
1316 shutdown_ai_command(dev);
1318 s->async->events |= COMEDI_CB_ERROR;
1319 if (status & NISTC_AI_STATUS1_OVER)
1320 s->async->events |= COMEDI_CB_OVERFLOW;
1323 if (status & NISTC_AI_STATUS1_SC_TC) {
1324 if (cmd->stop_src == TRIG_COUNT)
1325 shutdown_ai_command(dev);
1329 if (status & NISTC_AI_STATUS1_FIFO_HF) {
1331 static const int timeout = 10;
1333 * PCMCIA cards (at least 6036) seem to stop producing
1334 * interrupts if we fail to get the fifo less than half
1335 * full, so loop to be sure.
1337 for (i = 0; i < timeout; ++i) {
1338 ni_handle_fifo_half_full(dev);
1339 if ((ni_stc_readw(dev, NISTC_AI_STATUS1_REG) &
1340 NISTC_AI_STATUS1_FIFO_HF) == 0)
1344 #endif /* !PCIDMA */
1346 if (status & NISTC_AI_STATUS1_STOP)
1347 ni_handle_eos(dev, s);
1350 static void ack_b_interrupt(struct comedi_device *dev, unsigned short b_status)
1352 unsigned short ack = 0;
1354 if (b_status & NISTC_AO_STATUS1_BC_TC)
1355 ack |= NISTC_INTB_ACK_AO_BC_TC;
1356 if (b_status & NISTC_AO_STATUS1_OVERRUN)
1357 ack |= NISTC_INTB_ACK_AO_ERR;
1358 if (b_status & NISTC_AO_STATUS1_START)
1359 ack |= NISTC_INTB_ACK_AO_START;
1360 if (b_status & NISTC_AO_STATUS1_START1)
1361 ack |= NISTC_INTB_ACK_AO_START1;
1362 if (b_status & NISTC_AO_STATUS1_UC_TC)
1363 ack |= NISTC_INTB_ACK_AO_UC_TC;
1364 if (b_status & NISTC_AO_STATUS1_UI2_TC)
1365 ack |= NISTC_INTB_ACK_AO_UI2_TC;
1366 if (b_status & NISTC_AO_STATUS1_UPDATE)
1367 ack |= NISTC_INTB_ACK_AO_UPDATE;
1369 ni_stc_writew(dev, ack, NISTC_INTB_ACK_REG);
1372 static void handle_b_interrupt(struct comedi_device *dev,
1373 struct comedi_subdevice *s,
1374 unsigned short b_status)
1376 if (b_status == 0xffff)
1378 if (b_status & NISTC_AO_STATUS1_OVERRUN) {
1379 dev_err(dev->class_dev,
1380 "AO FIFO underrun status=0x%04x status2=0x%04x\n",
1381 b_status, ni_stc_readw(dev, NISTC_AO_STATUS2_REG));
1382 s->async->events |= COMEDI_CB_OVERFLOW;
1385 if (s->async->cmd.stop_src != TRIG_NONE &&
1386 b_status & NISTC_AO_STATUS1_BC_TC)
1387 s->async->events |= COMEDI_CB_EOA;
1390 if (b_status & NISTC_AO_STATUS1_FIFO_REQ) {
1393 ret = ni_ao_fifo_half_empty(dev, s);
1395 dev_err(dev->class_dev, "AO buffer underrun\n");
1396 ni_set_bits(dev, NISTC_INTB_ENA_REG,
1397 NISTC_INTB_ENA_AO_FIFO |
1398 NISTC_INTB_ENA_AO_ERR, 0);
1399 s->async->events |= COMEDI_CB_OVERFLOW;
1405 static void ni_ai_munge(struct comedi_device *dev, struct comedi_subdevice *s,
1406 void *data, unsigned int num_bytes,
1407 unsigned int chan_index)
1409 struct ni_private *devpriv = dev->private;
1410 struct comedi_async *async = s->async;
1411 struct comedi_cmd *cmd = &async->cmd;
1412 unsigned int nsamples = comedi_bytes_to_samples(s, num_bytes);
1413 unsigned short *array = data;
1414 unsigned int *larray = data;
1417 __le16 *barray = data;
1418 __le32 *blarray = data;
1421 for (i = 0; i < nsamples; i++) {
1423 if (s->subdev_flags & SDF_LSAMPL)
1424 larray[i] = le32_to_cpu(blarray[i]);
1426 array[i] = le16_to_cpu(barray[i]);
1428 if (s->subdev_flags & SDF_LSAMPL)
1429 larray[i] += devpriv->ai_offset[chan_index];
1431 array[i] += devpriv->ai_offset[chan_index];
1433 chan_index %= cmd->chanlist_len;
1439 static int ni_ai_setup_MITE_dma(struct comedi_device *dev)
1441 struct ni_private *devpriv = dev->private;
1442 struct comedi_subdevice *s = dev->read_subdev;
1444 unsigned long flags;
1446 retval = ni_request_ai_mite_channel(dev);
1450 /* write alloc the entire buffer */
1451 comedi_buf_write_alloc(s, s->async->prealloc_bufsz);
1453 spin_lock_irqsave(&devpriv->mite_channel_lock, flags);
1454 if (!devpriv->ai_mite_chan) {
1455 spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
1459 if (devpriv->is_611x || devpriv->is_6143)
1460 mite_prep_dma(devpriv->ai_mite_chan, 32, 16);
1461 else if (devpriv->is_628x)
1462 mite_prep_dma(devpriv->ai_mite_chan, 32, 32);
1464 mite_prep_dma(devpriv->ai_mite_chan, 16, 16);
1467 mite_dma_arm(devpriv->ai_mite_chan);
1468 spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
1473 static int ni_ao_setup_MITE_dma(struct comedi_device *dev)
1475 struct ni_private *devpriv = dev->private;
1476 struct comedi_subdevice *s = dev->write_subdev;
1478 unsigned long flags;
1480 retval = ni_request_ao_mite_channel(dev);
1484 /* read alloc the entire buffer */
1485 comedi_buf_read_alloc(s, s->async->prealloc_bufsz);
1487 spin_lock_irqsave(&devpriv->mite_channel_lock, flags);
1488 if (devpriv->ao_mite_chan) {
1489 if (devpriv->is_611x || devpriv->is_6713) {
1490 mite_prep_dma(devpriv->ao_mite_chan, 32, 32);
1493 * Doing 32 instead of 16 bit wide transfers from
1494 * memory makes the mite do 32 bit pci transfers,
1495 * doubling pci bandwidth.
1497 mite_prep_dma(devpriv->ao_mite_chan, 16, 32);
1499 mite_dma_arm(devpriv->ao_mite_chan);
1503 spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
1511 * used for both cancel ioctl and board initialization
1513 * this is pretty harsh for a cancel, but it works...
1515 static int ni_ai_reset(struct comedi_device *dev, struct comedi_subdevice *s)
1517 struct ni_private *devpriv = dev->private;
1518 unsigned int ai_personal;
1519 unsigned int ai_out_ctrl;
1521 ni_release_ai_mite_channel(dev);
1522 /* ai configuration */
1523 ni_stc_writew(dev, NISTC_RESET_AI_CFG_START | NISTC_RESET_AI,
1526 ni_set_bits(dev, NISTC_INTA_ENA_REG, NISTC_INTA_ENA_AI_MASK, 0);
1528 ni_clear_ai_fifo(dev);
1530 if (!devpriv->is_6143)
1531 ni_writeb(dev, NI_E_MISC_CMD_EXT_ATRIG, NI_E_MISC_CMD_REG);
1533 ni_stc_writew(dev, NISTC_AI_CMD1_DISARM, NISTC_AI_CMD1_REG);
1534 ni_stc_writew(dev, NISTC_AI_MODE1_START_STOP |
1536 /*| NISTC_AI_MODE1_TRIGGER_ONCE */,
1537 NISTC_AI_MODE1_REG);
1538 ni_stc_writew(dev, 0, NISTC_AI_MODE2_REG);
1539 /* generate FIFO interrupts on non-empty */
1540 ni_stc_writew(dev, NISTC_AI_MODE3_FIFO_MODE_NE,
1541 NISTC_AI_MODE3_REG);
1543 ai_personal = NISTC_AI_PERSONAL_SHIFTIN_PW |
1544 NISTC_AI_PERSONAL_SOC_POLARITY |
1545 NISTC_AI_PERSONAL_LOCALMUX_CLK_PW;
1546 ai_out_ctrl = NISTC_AI_OUT_CTRL_SCAN_IN_PROG_SEL(3) |
1547 NISTC_AI_OUT_CTRL_EXTMUX_CLK_SEL(0) |
1548 NISTC_AI_OUT_CTRL_LOCALMUX_CLK_SEL(2) |
1549 NISTC_AI_OUT_CTRL_SC_TC_SEL(3);
1550 if (devpriv->is_611x) {
1551 ai_out_ctrl |= NISTC_AI_OUT_CTRL_CONVERT_HIGH;
1552 } else if (devpriv->is_6143) {
1553 ai_out_ctrl |= NISTC_AI_OUT_CTRL_CONVERT_LOW;
1555 ai_personal |= NISTC_AI_PERSONAL_CONVERT_PW;
1556 if (devpriv->is_622x)
1557 ai_out_ctrl |= NISTC_AI_OUT_CTRL_CONVERT_HIGH;
1559 ai_out_ctrl |= NISTC_AI_OUT_CTRL_CONVERT_LOW;
1561 ni_stc_writew(dev, ai_personal, NISTC_AI_PERSONAL_REG);
1562 ni_stc_writew(dev, ai_out_ctrl, NISTC_AI_OUT_CTRL_REG);
1564 /* the following registers should not be changed, because there
1565 * are no backup registers in devpriv. If you want to change
1566 * any of these, add a backup register and other appropriate code:
1567 * NISTC_AI_MODE1_REG
1568 * NISTC_AI_MODE3_REG
1569 * NISTC_AI_PERSONAL_REG
1570 * NISTC_AI_OUT_CTRL_REG
1573 /* clear interrupts */
1574 ni_stc_writew(dev, NISTC_INTA_ACK_AI_ALL, NISTC_INTA_ACK_REG);
1576 ni_stc_writew(dev, NISTC_RESET_AI_CFG_END, NISTC_RESET_REG);
1581 static int ni_ai_poll(struct comedi_device *dev, struct comedi_subdevice *s)
1583 unsigned long flags;
1586 /* lock to avoid race with interrupt handler */
1587 spin_lock_irqsave(&dev->spinlock, flags);
1589 ni_handle_fifo_dregs(dev);
1591 ni_sync_ai_dma(dev);
1593 count = comedi_buf_n_bytes_ready(s);
1594 spin_unlock_irqrestore(&dev->spinlock, flags);
1599 static void ni_prime_channelgain_list(struct comedi_device *dev)
1603 ni_stc_writew(dev, NISTC_AI_CMD1_CONVERT_PULSE, NISTC_AI_CMD1_REG);
1604 for (i = 0; i < NI_TIMEOUT; ++i) {
1605 if (!(ni_stc_readw(dev, NISTC_AI_STATUS1_REG) &
1606 NISTC_AI_STATUS1_FIFO_E)) {
1607 ni_stc_writew(dev, 1, NISTC_ADC_FIFO_CLR_REG);
1612 dev_err(dev->class_dev, "timeout loading channel/gain list\n");
1615 static void ni_m_series_load_channelgain_list(struct comedi_device *dev,
1616 unsigned int n_chan,
1619 const struct ni_board_struct *board = dev->board_ptr;
1620 struct ni_private *devpriv = dev->private;
1621 unsigned int chan, range, aref;
1623 unsigned int dither;
1624 unsigned int range_code;
1626 ni_stc_writew(dev, 1, NISTC_CFG_MEM_CLR_REG);
1628 if ((list[0] & CR_ALT_SOURCE)) {
1629 unsigned int bypass_bits;
1631 chan = CR_CHAN(list[0]);
1632 range = CR_RANGE(list[0]);
1633 range_code = ni_gainlkup[board->gainlkup][range];
1634 dither = (list[0] & CR_ALT_FILTER) != 0;
1635 bypass_bits = NI_M_CFG_BYPASS_FIFO |
1636 NI_M_CFG_BYPASS_AI_CHAN(chan) |
1637 NI_M_CFG_BYPASS_AI_GAIN(range_code) |
1638 devpriv->ai_calib_source;
1640 bypass_bits |= NI_M_CFG_BYPASS_AI_DITHER;
1641 /* don't use 2's complement encoding */
1642 bypass_bits |= NI_M_CFG_BYPASS_AI_POLARITY;
1643 ni_writel(dev, bypass_bits, NI_M_CFG_BYPASS_FIFO_REG);
1645 ni_writel(dev, 0, NI_M_CFG_BYPASS_FIFO_REG);
1647 for (i = 0; i < n_chan; i++) {
1648 unsigned int config_bits = 0;
1650 chan = CR_CHAN(list[i]);
1651 aref = CR_AREF(list[i]);
1652 range = CR_RANGE(list[i]);
1653 dither = (list[i] & CR_ALT_FILTER) != 0;
1655 range_code = ni_gainlkup[board->gainlkup][range];
1656 devpriv->ai_offset[i] = 0;
1659 config_bits |= NI_M_AI_CFG_CHAN_TYPE_DIFF;
1662 config_bits |= NI_M_AI_CFG_CHAN_TYPE_COMMON;
1665 config_bits |= NI_M_AI_CFG_CHAN_TYPE_GROUND;
1670 config_bits |= NI_M_AI_CFG_CHAN_SEL(chan);
1671 config_bits |= NI_M_AI_CFG_BANK_SEL(chan);
1672 config_bits |= NI_M_AI_CFG_GAIN(range_code);
1673 if (i == n_chan - 1)
1674 config_bits |= NI_M_AI_CFG_LAST_CHAN;
1676 config_bits |= NI_M_AI_CFG_DITHER;
1677 /* don't use 2's complement encoding */
1678 config_bits |= NI_M_AI_CFG_POLARITY;
1679 ni_writew(dev, config_bits, NI_M_AI_CFG_FIFO_DATA_REG);
1681 ni_prime_channelgain_list(dev);
1685 * Notes on the 6110 and 6111:
1686 * These boards a slightly different than the rest of the series, since
1687 * they have multiple A/D converters.
1688 * From the driver side, the configuration memory is a
1690 * Configuration Memory Low:
1692 * bit 8: unipolar/bipolar (should be 0 for bipolar)
1693 * bits 0-3: gain. This is 4 bits instead of 3 for the other boards
1694 * 1001 gain=0.1 (+/- 50)
1703 * Configuration Memory High:
1704 * bits 12-14: Channel Type
1705 * 001 for differential
1706 * 000 for calibration
1707 * bit 11: coupling (this is not currently handled)
1711 * valid channels are 0-3
1713 static void ni_load_channelgain_list(struct comedi_device *dev,
1714 struct comedi_subdevice *s,
1715 unsigned int n_chan, unsigned int *list)
1717 const struct ni_board_struct *board = dev->board_ptr;
1718 struct ni_private *devpriv = dev->private;
1719 unsigned int offset = (s->maxdata + 1) >> 1;
1720 unsigned int chan, range, aref;
1722 unsigned int hi, lo;
1723 unsigned int dither;
1725 if (devpriv->is_m_series) {
1726 ni_m_series_load_channelgain_list(dev, n_chan, list);
1729 if (n_chan == 1 && !devpriv->is_611x && !devpriv->is_6143) {
1730 if (devpriv->changain_state &&
1731 devpriv->changain_spec == list[0]) {
1735 devpriv->changain_state = 1;
1736 devpriv->changain_spec = list[0];
1738 devpriv->changain_state = 0;
1741 ni_stc_writew(dev, 1, NISTC_CFG_MEM_CLR_REG);
1743 /* Set up Calibration mode if required */
1744 if (devpriv->is_6143) {
1745 if ((list[0] & CR_ALT_SOURCE) &&
1746 !devpriv->ai_calib_source_enabled) {
1747 /* Strobe Relay enable bit */
1748 ni_writew(dev, devpriv->ai_calib_source |
1749 NI6143_CALIB_CHAN_RELAY_ON,
1750 NI6143_CALIB_CHAN_REG);
1751 ni_writew(dev, devpriv->ai_calib_source,
1752 NI6143_CALIB_CHAN_REG);
1753 devpriv->ai_calib_source_enabled = 1;
1754 /* Allow relays to change */
1755 msleep_interruptible(100);
1756 } else if (!(list[0] & CR_ALT_SOURCE) &&
1757 devpriv->ai_calib_source_enabled) {
1758 /* Strobe Relay disable bit */
1759 ni_writew(dev, devpriv->ai_calib_source |
1760 NI6143_CALIB_CHAN_RELAY_OFF,
1761 NI6143_CALIB_CHAN_REG);
1762 ni_writew(dev, devpriv->ai_calib_source,
1763 NI6143_CALIB_CHAN_REG);
1764 devpriv->ai_calib_source_enabled = 0;
1765 /* Allow relays to change */
1766 msleep_interruptible(100);
1770 for (i = 0; i < n_chan; i++) {
1771 if (!devpriv->is_6143 && (list[i] & CR_ALT_SOURCE))
1772 chan = devpriv->ai_calib_source;
1774 chan = CR_CHAN(list[i]);
1775 aref = CR_AREF(list[i]);
1776 range = CR_RANGE(list[i]);
1777 dither = (list[i] & CR_ALT_FILTER) != 0;
1779 /* fix the external/internal range differences */
1780 range = ni_gainlkup[board->gainlkup][range];
1781 if (devpriv->is_611x)
1782 devpriv->ai_offset[i] = offset;
1784 devpriv->ai_offset[i] = (range & 0x100) ? 0 : offset;
1787 if ((list[i] & CR_ALT_SOURCE)) {
1788 if (devpriv->is_611x)
1789 ni_writew(dev, CR_CHAN(list[i]) & 0x0003,
1790 NI611X_CALIB_CHAN_SEL_REG);
1792 if (devpriv->is_611x)
1794 else if (devpriv->is_6143)
1798 hi |= NI_E_AI_CFG_HI_TYPE_DIFF;
1801 hi |= NI_E_AI_CFG_HI_TYPE_COMMON;
1804 hi |= NI_E_AI_CFG_HI_TYPE_GROUND;
1810 hi |= NI_E_AI_CFG_HI_CHAN(chan);
1812 ni_writew(dev, hi, NI_E_AI_CFG_HI_REG);
1814 if (!devpriv->is_6143) {
1815 lo = NI_E_AI_CFG_LO_GAIN(range);
1817 if (i == n_chan - 1)
1818 lo |= NI_E_AI_CFG_LO_LAST_CHAN;
1820 lo |= NI_E_AI_CFG_LO_DITHER;
1822 ni_writew(dev, lo, NI_E_AI_CFG_LO_REG);
1826 /* prime the channel/gain list */
1827 if (!devpriv->is_611x && !devpriv->is_6143)
1828 ni_prime_channelgain_list(dev);
1831 static int ni_ai_insn_read(struct comedi_device *dev,
1832 struct comedi_subdevice *s,
1833 struct comedi_insn *insn,
1836 struct ni_private *devpriv = dev->private;
1837 unsigned int mask = s->maxdata;
1839 unsigned int signbits;
1842 ni_load_channelgain_list(dev, s, 1, &insn->chanspec);
1844 ni_clear_ai_fifo(dev);
1846 signbits = devpriv->ai_offset[0];
1847 if (devpriv->is_611x) {
1848 for (n = 0; n < num_adc_stages_611x; n++) {
1849 ni_stc_writew(dev, NISTC_AI_CMD1_CONVERT_PULSE,
1853 for (n = 0; n < insn->n; n++) {
1854 ni_stc_writew(dev, NISTC_AI_CMD1_CONVERT_PULSE,
1856 /* The 611x has screwy 32-bit FIFOs. */
1858 for (i = 0; i < NI_TIMEOUT; i++) {
1859 if (ni_readb(dev, NI_E_STATUS_REG) & 0x80) {
1861 NI611X_AI_FIFO_DATA_REG);
1866 if (!(ni_stc_readw(dev, NISTC_AI_STATUS1_REG) &
1867 NISTC_AI_STATUS1_FIFO_E)) {
1869 NI611X_AI_FIFO_DATA_REG);
1874 if (i == NI_TIMEOUT) {
1875 dev_err(dev->class_dev, "timeout\n");
1879 data[n] = d & 0xffff;
1881 } else if (devpriv->is_6143) {
1882 for (n = 0; n < insn->n; n++) {
1883 ni_stc_writew(dev, NISTC_AI_CMD1_CONVERT_PULSE,
1887 * The 6143 has 32-bit FIFOs. You need to strobe a
1888 * bit to move a single 16bit stranded sample into
1892 for (i = 0; i < NI_TIMEOUT; i++) {
1893 if (ni_readl(dev, NI6143_AI_FIFO_STATUS_REG) &
1895 /* Get stranded sample into FIFO */
1896 ni_writel(dev, 0x01,
1897 NI6143_AI_FIFO_CTRL_REG);
1899 NI6143_AI_FIFO_DATA_REG);
1903 if (i == NI_TIMEOUT) {
1904 dev_err(dev->class_dev, "timeout\n");
1907 data[n] = (((d >> 16) & 0xFFFF) + signbits) & 0xFFFF;
1910 for (n = 0; n < insn->n; n++) {
1911 ni_stc_writew(dev, NISTC_AI_CMD1_CONVERT_PULSE,
1913 for (i = 0; i < NI_TIMEOUT; i++) {
1914 if (!(ni_stc_readw(dev, NISTC_AI_STATUS1_REG) &
1915 NISTC_AI_STATUS1_FIFO_E))
1918 if (i == NI_TIMEOUT) {
1919 dev_err(dev->class_dev, "timeout\n");
1922 if (devpriv->is_m_series) {
1923 d = ni_readl(dev, NI_M_AI_FIFO_DATA_REG);
1927 d = ni_readw(dev, NI_E_AI_FIFO_DATA_REG);
1929 data[n] = d & 0xffff;
1936 static int ni_ns_to_timer(const struct comedi_device *dev,
1937 unsigned int nanosec, unsigned int flags)
1939 struct ni_private *devpriv = dev->private;
1942 switch (flags & CMDF_ROUND_MASK) {
1943 case CMDF_ROUND_NEAREST:
1945 divider = DIV_ROUND_CLOSEST(nanosec, devpriv->clock_ns);
1947 case CMDF_ROUND_DOWN:
1948 divider = (nanosec) / devpriv->clock_ns;
1951 divider = DIV_ROUND_UP(nanosec, devpriv->clock_ns);
1957 static unsigned int ni_timer_to_ns(const struct comedi_device *dev, int timer)
1959 struct ni_private *devpriv = dev->private;
1961 return devpriv->clock_ns * (timer + 1);
1964 static void ni_cmd_set_mite_transfer(struct mite_ring *ring,
1965 struct comedi_subdevice *sdev,
1966 const struct comedi_cmd *cmd,
1967 unsigned int max_count)
1970 unsigned int nbytes = max_count;
1972 if (cmd->stop_arg > 0 && cmd->stop_arg < max_count)
1973 nbytes = cmd->stop_arg;
1974 nbytes *= comedi_bytes_per_scan(sdev);
1976 if (nbytes > sdev->async->prealloc_bufsz) {
1977 if (cmd->stop_arg > 0)
1978 dev_err(sdev->device->class_dev,
1979 "ni_cmd_set_mite_transfer: tried exact data transfer limits greater than buffer size\n");
1982 * we can only transfer up to the size of the buffer. In this
1983 * case, the user is expected to continue to write into the
1984 * comedi buffer (already implemented as a ring buffer).
1986 nbytes = sdev->async->prealloc_bufsz;
1989 mite_init_ring_descriptors(ring, sdev, nbytes);
1991 dev_err(sdev->device->class_dev,
1992 "ni_cmd_set_mite_transfer: exact data transfer limits not implemented yet without DMA\n");
1996 static unsigned int ni_min_ai_scan_period_ns(struct comedi_device *dev,
1997 unsigned int num_channels)
1999 const struct ni_board_struct *board = dev->board_ptr;
2000 struct ni_private *devpriv = dev->private;
2002 /* simultaneously-sampled inputs */
2003 if (devpriv->is_611x || devpriv->is_6143)
2004 return board->ai_speed;
2006 /* multiplexed inputs */
2007 return board->ai_speed * num_channels;
2010 static int ni_ai_cmdtest(struct comedi_device *dev, struct comedi_subdevice *s,
2011 struct comedi_cmd *cmd)
2013 const struct ni_board_struct *board = dev->board_ptr;
2014 struct ni_private *devpriv = dev->private;
2017 unsigned int sources;
2019 /* Step 1 : check if triggers are trivially valid */
2021 err |= comedi_check_trigger_src(&cmd->start_src,
2022 TRIG_NOW | TRIG_INT | TRIG_EXT);
2023 err |= comedi_check_trigger_src(&cmd->scan_begin_src,
2024 TRIG_TIMER | TRIG_EXT);
2026 sources = TRIG_TIMER | TRIG_EXT;
2027 if (devpriv->is_611x || devpriv->is_6143)
2028 sources |= TRIG_NOW;
2029 err |= comedi_check_trigger_src(&cmd->convert_src, sources);
2031 err |= comedi_check_trigger_src(&cmd->scan_end_src, TRIG_COUNT);
2032 err |= comedi_check_trigger_src(&cmd->stop_src, TRIG_COUNT | TRIG_NONE);
2037 /* Step 2a : make sure trigger sources are unique */
2039 err |= comedi_check_trigger_is_unique(cmd->start_src);
2040 err |= comedi_check_trigger_is_unique(cmd->scan_begin_src);
2041 err |= comedi_check_trigger_is_unique(cmd->convert_src);
2042 err |= comedi_check_trigger_is_unique(cmd->stop_src);
2044 /* Step 2b : and mutually compatible */
2049 /* Step 3: check if arguments are trivially valid */
2051 switch (cmd->start_src) {
2054 err |= comedi_check_trigger_arg_is(&cmd->start_arg, 0);
2057 tmp = CR_CHAN(cmd->start_arg);
2061 tmp |= (cmd->start_arg & (CR_INVERT | CR_EDGE));
2062 err |= comedi_check_trigger_arg_is(&cmd->start_arg, tmp);
2066 if (cmd->scan_begin_src == TRIG_TIMER) {
2067 err |= comedi_check_trigger_arg_min(&cmd->scan_begin_arg,
2068 ni_min_ai_scan_period_ns(dev, cmd->chanlist_len));
2069 err |= comedi_check_trigger_arg_max(&cmd->scan_begin_arg,
2072 } else if (cmd->scan_begin_src == TRIG_EXT) {
2073 /* external trigger */
2074 unsigned int tmp = CR_CHAN(cmd->scan_begin_arg);
2078 tmp |= (cmd->scan_begin_arg & (CR_INVERT | CR_EDGE));
2079 err |= comedi_check_trigger_arg_is(&cmd->scan_begin_arg, tmp);
2080 } else { /* TRIG_OTHER */
2081 err |= comedi_check_trigger_arg_is(&cmd->scan_begin_arg, 0);
2084 if (cmd->convert_src == TRIG_TIMER) {
2085 if (devpriv->is_611x || devpriv->is_6143) {
2086 err |= comedi_check_trigger_arg_is(&cmd->convert_arg,
2089 err |= comedi_check_trigger_arg_min(&cmd->convert_arg,
2091 err |= comedi_check_trigger_arg_max(&cmd->convert_arg,
2095 } else if (cmd->convert_src == TRIG_EXT) {
2096 /* external trigger */
2097 unsigned int tmp = CR_CHAN(cmd->convert_arg);
2101 tmp |= (cmd->convert_arg & (CR_ALT_FILTER | CR_INVERT));
2102 err |= comedi_check_trigger_arg_is(&cmd->convert_arg, tmp);
2103 } else if (cmd->convert_src == TRIG_NOW) {
2104 err |= comedi_check_trigger_arg_is(&cmd->convert_arg, 0);
2107 err |= comedi_check_trigger_arg_is(&cmd->scan_end_arg,
2110 if (cmd->stop_src == TRIG_COUNT) {
2111 unsigned int max_count = 0x01000000;
2113 if (devpriv->is_611x)
2114 max_count -= num_adc_stages_611x;
2115 err |= comedi_check_trigger_arg_max(&cmd->stop_arg, max_count);
2116 err |= comedi_check_trigger_arg_min(&cmd->stop_arg, 1);
2119 err |= comedi_check_trigger_arg_is(&cmd->stop_arg, 0);
2125 /* step 4: fix up any arguments */
2127 if (cmd->scan_begin_src == TRIG_TIMER) {
2128 tmp = cmd->scan_begin_arg;
2129 cmd->scan_begin_arg =
2130 ni_timer_to_ns(dev, ni_ns_to_timer(dev,
2131 cmd->scan_begin_arg,
2133 if (tmp != cmd->scan_begin_arg)
2136 if (cmd->convert_src == TRIG_TIMER) {
2137 if (!devpriv->is_611x && !devpriv->is_6143) {
2138 tmp = cmd->convert_arg;
2140 ni_timer_to_ns(dev, ni_ns_to_timer(dev,
2143 if (tmp != cmd->convert_arg)
2145 if (cmd->scan_begin_src == TRIG_TIMER &&
2146 cmd->scan_begin_arg <
2147 cmd->convert_arg * cmd->scan_end_arg) {
2148 cmd->scan_begin_arg =
2149 cmd->convert_arg * cmd->scan_end_arg;
2161 static int ni_ai_inttrig(struct comedi_device *dev,
2162 struct comedi_subdevice *s,
2163 unsigned int trig_num)
2165 struct ni_private *devpriv = dev->private;
2166 struct comedi_cmd *cmd = &s->async->cmd;
2168 if (trig_num != cmd->start_arg)
2171 ni_stc_writew(dev, NISTC_AI_CMD2_START1_PULSE | devpriv->ai_cmd2,
2173 s->async->inttrig = NULL;
2178 static int ni_ai_cmd(struct comedi_device *dev, struct comedi_subdevice *s)
2180 struct ni_private *devpriv = dev->private;
2181 const struct comedi_cmd *cmd = &s->async->cmd;
2183 int mode1 = 0; /* mode1 is needed for both stop and convert */
2185 int start_stop_select = 0;
2186 unsigned int stop_count;
2187 int interrupt_a_enable = 0;
2188 unsigned int ai_trig;
2190 if (dev->irq == 0) {
2191 dev_err(dev->class_dev, "cannot run command without an irq\n");
2194 ni_clear_ai_fifo(dev);
2196 ni_load_channelgain_list(dev, s, cmd->chanlist_len, cmd->chanlist);
2198 /* start configuration */
2199 ni_stc_writew(dev, NISTC_RESET_AI_CFG_START, NISTC_RESET_REG);
2202 * Disable analog triggering for now, since it interferes
2203 * with the use of pfi0.
2205 devpriv->an_trig_etc_reg &= ~NISTC_ATRIG_ETC_ENA;
2206 ni_stc_writew(dev, devpriv->an_trig_etc_reg, NISTC_ATRIG_ETC_REG);
2208 ai_trig = NISTC_AI_TRIG_START2_SEL(0) | NISTC_AI_TRIG_START1_SYNC;
2209 switch (cmd->start_src) {
2212 ai_trig |= NISTC_AI_TRIG_START1_EDGE |
2213 NISTC_AI_TRIG_START1_SEL(0);
2216 ai_trig |= NISTC_AI_TRIG_START1_SEL(CR_CHAN(cmd->start_arg) +
2219 if (cmd->start_arg & CR_INVERT)
2220 ai_trig |= NISTC_AI_TRIG_START1_POLARITY;
2221 if (cmd->start_arg & CR_EDGE)
2222 ai_trig |= NISTC_AI_TRIG_START1_EDGE;
2225 ni_stc_writew(dev, ai_trig, NISTC_AI_TRIG_SEL_REG);
2227 mode2 &= ~NISTC_AI_MODE2_PRE_TRIGGER;
2228 mode2 &= ~NISTC_AI_MODE2_SC_INIT_LOAD_SRC;
2229 mode2 &= ~NISTC_AI_MODE2_SC_RELOAD_MODE;
2230 ni_stc_writew(dev, mode2, NISTC_AI_MODE2_REG);
2232 if (cmd->chanlist_len == 1 || devpriv->is_611x || devpriv->is_6143) {
2234 start_stop_select |= NISTC_AI_STOP_POLARITY |
2235 NISTC_AI_STOP_SEL(31) |
2238 /* ai configuration memory */
2239 start_stop_select |= NISTC_AI_STOP_SEL(19);
2241 ni_stc_writew(dev, start_stop_select, NISTC_AI_START_STOP_REG);
2243 devpriv->ai_cmd2 = 0;
2244 switch (cmd->stop_src) {
2246 stop_count = cmd->stop_arg - 1;
2248 if (devpriv->is_611x) {
2249 /* have to take 3 stage adc pipeline into account */
2250 stop_count += num_adc_stages_611x;
2252 /* stage number of scans */
2253 ni_stc_writel(dev, stop_count, NISTC_AI_SC_LOADA_REG);
2255 mode1 |= NISTC_AI_MODE1_START_STOP |
2256 NISTC_AI_MODE1_RSVD |
2257 NISTC_AI_MODE1_TRIGGER_ONCE;
2258 ni_stc_writew(dev, mode1, NISTC_AI_MODE1_REG);
2259 /* load SC (Scan Count) */
2260 ni_stc_writew(dev, NISTC_AI_CMD1_SC_LOAD, NISTC_AI_CMD1_REG);
2262 if (stop_count == 0) {
2263 devpriv->ai_cmd2 |= NISTC_AI_CMD2_END_ON_EOS;
2264 interrupt_a_enable |= NISTC_INTA_ENA_AI_STOP;
2266 * This is required to get the last sample for
2267 * chanlist_len > 1, not sure why.
2269 if (cmd->chanlist_len > 1)
2270 start_stop_select |= NISTC_AI_STOP_POLARITY |
2275 /* stage number of scans */
2276 ni_stc_writel(dev, 0, NISTC_AI_SC_LOADA_REG);
2278 mode1 |= NISTC_AI_MODE1_START_STOP |
2279 NISTC_AI_MODE1_RSVD |
2280 NISTC_AI_MODE1_CONTINUOUS;
2281 ni_stc_writew(dev, mode1, NISTC_AI_MODE1_REG);
2283 /* load SC (Scan Count) */
2284 ni_stc_writew(dev, NISTC_AI_CMD1_SC_LOAD, NISTC_AI_CMD1_REG);
2288 switch (cmd->scan_begin_src) {
2291 * stop bits for non 611x boards
2292 * NISTC_AI_MODE3_SI_TRIG_DELAY=0
2293 * NISTC_AI_MODE2_PRE_TRIGGER=0
2294 * NISTC_AI_START_STOP_REG:
2295 * NISTC_AI_START_POLARITY=0 (?) rising edge
2296 * NISTC_AI_START_EDGE=1 edge triggered
2297 * NISTC_AI_START_SYNC=1 (?)
2298 * NISTC_AI_START_SEL=0 SI_TC
2299 * NISTC_AI_STOP_POLARITY=0 rising edge
2300 * NISTC_AI_STOP_EDGE=0 level
2301 * NISTC_AI_STOP_SYNC=1
2302 * NISTC_AI_STOP_SEL=19 external pin (configuration mem)
2304 start_stop_select |= NISTC_AI_START_EDGE | NISTC_AI_START_SYNC;
2305 ni_stc_writew(dev, start_stop_select, NISTC_AI_START_STOP_REG);
2307 mode2 &= ~NISTC_AI_MODE2_SI_INIT_LOAD_SRC; /* A */
2308 mode2 |= NISTC_AI_MODE2_SI_RELOAD_MODE(0);
2309 /* mode2 |= NISTC_AI_MODE2_SC_RELOAD_MODE; */
2310 ni_stc_writew(dev, mode2, NISTC_AI_MODE2_REG);
2313 timer = ni_ns_to_timer(dev, cmd->scan_begin_arg,
2314 CMDF_ROUND_NEAREST);
2315 ni_stc_writel(dev, timer, NISTC_AI_SI_LOADA_REG);
2316 ni_stc_writew(dev, NISTC_AI_CMD1_SI_LOAD, NISTC_AI_CMD1_REG);
2319 if (cmd->scan_begin_arg & CR_EDGE)
2320 start_stop_select |= NISTC_AI_START_EDGE;
2321 if (cmd->scan_begin_arg & CR_INVERT) /* falling edge */
2322 start_stop_select |= NISTC_AI_START_POLARITY;
2323 if (cmd->scan_begin_src != cmd->convert_src ||
2324 (cmd->scan_begin_arg & ~CR_EDGE) !=
2325 (cmd->convert_arg & ~CR_EDGE))
2326 start_stop_select |= NISTC_AI_START_SYNC;
2327 start_stop_select |=
2328 NISTC_AI_START_SEL(1 + CR_CHAN(cmd->scan_begin_arg));
2329 ni_stc_writew(dev, start_stop_select, NISTC_AI_START_STOP_REG);
2333 switch (cmd->convert_src) {
2336 if (cmd->convert_arg == 0 || cmd->convert_src == TRIG_NOW)
2339 timer = ni_ns_to_timer(dev, cmd->convert_arg,
2340 CMDF_ROUND_NEAREST);
2341 /* 0,0 does not work */
2342 ni_stc_writew(dev, 1, NISTC_AI_SI2_LOADA_REG);
2343 ni_stc_writew(dev, timer, NISTC_AI_SI2_LOADB_REG);
2345 mode2 &= ~NISTC_AI_MODE2_SI2_INIT_LOAD_SRC; /* A */
2346 mode2 |= NISTC_AI_MODE2_SI2_RELOAD_MODE; /* alternate */
2347 ni_stc_writew(dev, mode2, NISTC_AI_MODE2_REG);
2349 ni_stc_writew(dev, NISTC_AI_CMD1_SI2_LOAD, NISTC_AI_CMD1_REG);
2351 mode2 |= NISTC_AI_MODE2_SI2_INIT_LOAD_SRC; /* B */
2352 mode2 |= NISTC_AI_MODE2_SI2_RELOAD_MODE; /* alternate */
2353 ni_stc_writew(dev, mode2, NISTC_AI_MODE2_REG);
2356 mode1 |= NISTC_AI_MODE1_CONVERT_SRC(1 +
2357 CR_CHAN(cmd->convert_arg));
2358 if ((cmd->convert_arg & CR_INVERT) == 0)
2359 mode1 |= NISTC_AI_MODE1_CONVERT_POLARITY;
2360 ni_stc_writew(dev, mode1, NISTC_AI_MODE1_REG);
2362 mode2 |= NISTC_AI_MODE2_SC_GATE_ENA |
2363 NISTC_AI_MODE2_START_STOP_GATE_ENA;
2364 ni_stc_writew(dev, mode2, NISTC_AI_MODE2_REG);
2370 /* interrupt on FIFO, errors, SC_TC */
2371 interrupt_a_enable |= NISTC_INTA_ENA_AI_ERR |
2372 NISTC_INTA_ENA_AI_SC_TC;
2375 interrupt_a_enable |= NISTC_INTA_ENA_AI_FIFO;
2378 if ((cmd->flags & CMDF_WAKE_EOS) ||
2379 (devpriv->ai_cmd2 & NISTC_AI_CMD2_END_ON_EOS)) {
2380 /* wake on end-of-scan */
2381 devpriv->aimode = AIMODE_SCAN;
2383 devpriv->aimode = AIMODE_HALF_FULL;
2386 switch (devpriv->aimode) {
2387 case AIMODE_HALF_FULL:
2388 /* FIFO interrupts and DMA requests on half-full */
2390 ni_stc_writew(dev, NISTC_AI_MODE3_FIFO_MODE_HF_E,
2391 NISTC_AI_MODE3_REG);
2393 ni_stc_writew(dev, NISTC_AI_MODE3_FIFO_MODE_HF,
2394 NISTC_AI_MODE3_REG);
2398 /*generate FIFO interrupts on non-empty */
2399 ni_stc_writew(dev, NISTC_AI_MODE3_FIFO_MODE_NE,
2400 NISTC_AI_MODE3_REG);
2404 ni_stc_writew(dev, NISTC_AI_MODE3_FIFO_MODE_NE,
2405 NISTC_AI_MODE3_REG);
2407 ni_stc_writew(dev, NISTC_AI_MODE3_FIFO_MODE_HF,
2408 NISTC_AI_MODE3_REG);
2410 interrupt_a_enable |= NISTC_INTA_ENA_AI_STOP;
2416 /* clear interrupts */
2417 ni_stc_writew(dev, NISTC_INTA_ACK_AI_ALL, NISTC_INTA_ACK_REG);
2419 ni_set_bits(dev, NISTC_INTA_ENA_REG, interrupt_a_enable, 1);
2421 /* interrupt on nothing */
2422 ni_set_bits(dev, NISTC_INTA_ENA_REG, ~0, 0);
2424 /* XXX start polling if necessary */
2427 /* end configuration */
2428 ni_stc_writew(dev, NISTC_RESET_AI_CFG_END, NISTC_RESET_REG);
2430 switch (cmd->scan_begin_src) {
2432 ni_stc_writew(dev, NISTC_AI_CMD1_SI2_ARM |
2433 NISTC_AI_CMD1_SI_ARM |
2434 NISTC_AI_CMD1_DIV_ARM |
2435 NISTC_AI_CMD1_SC_ARM,
2439 ni_stc_writew(dev, NISTC_AI_CMD1_SI2_ARM |
2440 NISTC_AI_CMD1_SI_ARM | /* XXX ? */
2441 NISTC_AI_CMD1_DIV_ARM |
2442 NISTC_AI_CMD1_SC_ARM,
2449 int retval = ni_ai_setup_MITE_dma(dev);
2456 if (cmd->start_src == TRIG_NOW) {
2457 ni_stc_writew(dev, NISTC_AI_CMD2_START1_PULSE |
2460 s->async->inttrig = NULL;
2461 } else if (cmd->start_src == TRIG_EXT) {
2462 s->async->inttrig = NULL;
2463 } else { /* TRIG_INT */
2464 s->async->inttrig = ni_ai_inttrig;
2470 static int ni_ai_insn_config(struct comedi_device *dev,
2471 struct comedi_subdevice *s,
2472 struct comedi_insn *insn, unsigned int *data)
2474 struct ni_private *devpriv = dev->private;
2480 case INSN_CONFIG_ALT_SOURCE:
2481 if (devpriv->is_m_series) {
2482 if (data[1] & ~NI_M_CFG_BYPASS_AI_CAL_MASK)
2484 devpriv->ai_calib_source = data[1];
2485 } else if (devpriv->is_6143) {
2486 unsigned int calib_source;
2488 calib_source = data[1] & 0xf;
2490 devpriv->ai_calib_source = calib_source;
2491 ni_writew(dev, calib_source, NI6143_CALIB_CHAN_REG);
2493 unsigned int calib_source;
2494 unsigned int calib_source_adjust;
2496 calib_source = data[1] & 0xf;
2497 calib_source_adjust = (data[1] >> 4) & 0xff;
2499 if (calib_source >= 8)
2501 devpriv->ai_calib_source = calib_source;
2502 if (devpriv->is_611x) {
2503 ni_writeb(dev, calib_source_adjust,
2504 NI611X_CAL_GAIN_SEL_REG);
2515 static void ni_ao_munge(struct comedi_device *dev, struct comedi_subdevice *s,
2516 void *data, unsigned int num_bytes,
2517 unsigned int chan_index)
2519 struct comedi_cmd *cmd = &s->async->cmd;
2520 unsigned int nsamples = comedi_bytes_to_samples(s, num_bytes);
2521 unsigned short *array = data;
2524 __le16 buf, *barray = data;
2527 for (i = 0; i < nsamples; i++) {
2528 unsigned int range = CR_RANGE(cmd->chanlist[chan_index]);
2529 unsigned short val = array[i];
2532 * Munge data from unsigned to two's complement for
2535 if (comedi_range_is_bipolar(s, range))
2536 val = comedi_offset_munge(s, val);
2538 buf = cpu_to_le16(val);
2544 chan_index %= cmd->chanlist_len;
2548 static int ni_m_series_ao_config_chanlist(struct comedi_device *dev,
2549 struct comedi_subdevice *s,
2550 unsigned int chanspec[],
2551 unsigned int n_chans, int timed)
2553 struct ni_private *devpriv = dev->private;
2561 for (i = 0; i < s->n_chan; ++i) {
2562 devpriv->ao_conf[i] &= ~NI_M_AO_CFG_BANK_UPDATE_TIMED;
2563 ni_writeb(dev, devpriv->ao_conf[i],
2564 NI_M_AO_CFG_BANK_REG(i));
2565 ni_writeb(dev, 0xf, NI_M_AO_WAVEFORM_ORDER_REG(i));
2568 for (i = 0; i < n_chans; i++) {
2569 const struct comedi_krange *krange;
2571 chan = CR_CHAN(chanspec[i]);
2572 range = CR_RANGE(chanspec[i]);
2573 krange = s->range_table->range + range;
2576 switch (krange->max - krange->min) {
2578 conf |= NI_M_AO_CFG_BANK_REF_INT_10V;
2579 ni_writeb(dev, 0, NI_M_AO_REF_ATTENUATION_REG(chan));
2582 conf |= NI_M_AO_CFG_BANK_REF_INT_5V;
2583 ni_writeb(dev, 0, NI_M_AO_REF_ATTENUATION_REG(chan));
2586 conf |= NI_M_AO_CFG_BANK_REF_INT_10V;
2587 ni_writeb(dev, NI_M_AO_REF_ATTENUATION_X5,
2588 NI_M_AO_REF_ATTENUATION_REG(chan));
2591 conf |= NI_M_AO_CFG_BANK_REF_INT_5V;
2592 ni_writeb(dev, NI_M_AO_REF_ATTENUATION_X5,
2593 NI_M_AO_REF_ATTENUATION_REG(chan));
2596 dev_err(dev->class_dev,
2597 "bug! unhandled ao reference voltage\n");
2600 switch (krange->max + krange->min) {
2602 conf |= NI_M_AO_CFG_BANK_OFFSET_0V;
2605 conf |= NI_M_AO_CFG_BANK_OFFSET_5V;
2608 dev_err(dev->class_dev,
2609 "bug! unhandled ao offset voltage\n");
2613 conf |= NI_M_AO_CFG_BANK_UPDATE_TIMED;
2614 ni_writeb(dev, conf, NI_M_AO_CFG_BANK_REG(chan));
2615 devpriv->ao_conf[chan] = conf;
2616 ni_writeb(dev, i, NI_M_AO_WAVEFORM_ORDER_REG(chan));
2621 static int ni_old_ao_config_chanlist(struct comedi_device *dev,
2622 struct comedi_subdevice *s,
2623 unsigned int chanspec[],
2624 unsigned int n_chans)
2626 struct ni_private *devpriv = dev->private;
2633 for (i = 0; i < n_chans; i++) {
2634 chan = CR_CHAN(chanspec[i]);
2635 range = CR_RANGE(chanspec[i]);
2636 conf = NI_E_AO_DACSEL(chan);
2638 if (comedi_range_is_bipolar(s, range)) {
2639 conf |= NI_E_AO_CFG_BIP;
2640 invert = (s->maxdata + 1) >> 1;
2644 if (comedi_range_is_external(s, range))
2645 conf |= NI_E_AO_EXT_REF;
2647 /* not all boards can deglitch, but this shouldn't hurt */
2648 if (chanspec[i] & CR_DEGLITCH)
2649 conf |= NI_E_AO_DEGLITCH;
2651 /* analog reference */
2652 /* AREF_OTHER connects AO ground to AI ground, i think */
2653 if (CR_AREF(chanspec[i]) == AREF_OTHER)
2654 conf |= NI_E_AO_GROUND_REF;
2656 ni_writew(dev, conf, NI_E_AO_CFG_REG);
2657 devpriv->ao_conf[chan] = conf;
2662 static int ni_ao_config_chanlist(struct comedi_device *dev,
2663 struct comedi_subdevice *s,
2664 unsigned int chanspec[], unsigned int n_chans,
2667 struct ni_private *devpriv = dev->private;
2669 if (devpriv->is_m_series)
2670 return ni_m_series_ao_config_chanlist(dev, s, chanspec, n_chans,
2673 return ni_old_ao_config_chanlist(dev, s, chanspec, n_chans);
2676 static int ni_ao_insn_write(struct comedi_device *dev,
2677 struct comedi_subdevice *s,
2678 struct comedi_insn *insn,
2681 struct ni_private *devpriv = dev->private;
2682 unsigned int chan = CR_CHAN(insn->chanspec);
2683 unsigned int range = CR_RANGE(insn->chanspec);
2687 if (devpriv->is_6xxx) {
2688 ni_ao_win_outw(dev, 1 << chan, NI671X_AO_IMMEDIATE_REG);
2690 reg = NI671X_DAC_DIRECT_DATA_REG(chan);
2691 } else if (devpriv->is_m_series) {
2692 reg = NI_M_DAC_DIRECT_DATA_REG(chan);
2694 reg = NI_E_DAC_DIRECT_DATA_REG(chan);
2697 ni_ao_config_chanlist(dev, s, &insn->chanspec, 1, 0);
2699 for (i = 0; i < insn->n; i++) {
2700 unsigned int val = data[i];
2702 s->readback[chan] = val;
2704 if (devpriv->is_6xxx) {
2706 * 6xxx boards have bipolar outputs, munge the
2707 * unsigned comedi values to 2's complement
2709 val = comedi_offset_munge(s, val);
2711 ni_ao_win_outw(dev, val, reg);
2712 } else if (devpriv->is_m_series) {
2714 * M-series boards use offset binary values for
2715 * bipolar and uinpolar outputs
2717 ni_writew(dev, val, reg);
2720 * Non-M series boards need two's complement values
2721 * for bipolar ranges.
2723 if (comedi_range_is_bipolar(s, range))
2724 val = comedi_offset_munge(s, val);
2726 ni_writew(dev, val, reg);
2734 * Arms the AO device in preparation for a trigger event.
2735 * This function also allocates and prepares a DMA channel (or FIFO if DMA is
2736 * not used). As a part of this preparation, this function preloads the DAC
2737 * registers with the first values of the output stream. This ensures that the
2738 * first clock cycle after the trigger can be used for output.
2740 * Note that this function _must_ happen after a user has written data to the
2741 * output buffers via either mmap or write(fileno,...).
2743 static int ni_ao_arm(struct comedi_device *dev,
2744 struct comedi_subdevice *s)
2746 struct ni_private *devpriv = dev->private;
2748 int interrupt_b_bits;
2750 static const int timeout = 1000;
2753 * Prevent ao from doing things like trying to allocate the ao dma
2754 * channel multiple times.
2756 if (!devpriv->ao_needs_arming) {
2757 dev_dbg(dev->class_dev, "%s: device does not need arming!\n",
2762 devpriv->ao_needs_arming = 0;
2764 ni_set_bits(dev, NISTC_INTB_ENA_REG,
2765 NISTC_INTB_ENA_AO_FIFO | NISTC_INTB_ENA_AO_ERR, 0);
2766 interrupt_b_bits = NISTC_INTB_ENA_AO_ERR;
2768 ni_stc_writew(dev, 1, NISTC_DAC_FIFO_CLR_REG);
2769 if (devpriv->is_6xxx)
2770 ni_ao_win_outl(dev, 0x6, NI611X_AO_FIFO_OFFSET_LOAD_REG);
2771 ret = ni_ao_setup_MITE_dma(dev);
2774 ret = ni_ao_wait_for_dma_load(dev);
2778 ret = ni_ao_prep_fifo(dev, s);
2782 interrupt_b_bits |= NISTC_INTB_ENA_AO_FIFO;
2785 ni_stc_writew(dev, devpriv->ao_mode3 | NISTC_AO_MODE3_NOT_AN_UPDATE,
2786 NISTC_AO_MODE3_REG);
2787 ni_stc_writew(dev, devpriv->ao_mode3, NISTC_AO_MODE3_REG);
2788 /* wait for DACs to be loaded */
2789 for (i = 0; i < timeout; i++) {
2791 if ((ni_stc_readw(dev, NISTC_STATUS2_REG) &
2792 NISTC_STATUS2_AO_TMRDACWRS_IN_PROGRESS) == 0)
2796 dev_err(dev->class_dev,
2797 "timed out waiting for AO_TMRDACWRs_In_Progress_St to clear\n");
2801 * stc manual says we are need to clear error interrupt after
2802 * AO_TMRDACWRs_In_Progress_St clears
2804 ni_stc_writew(dev, NISTC_INTB_ACK_AO_ERR, NISTC_INTB_ACK_REG);
2806 ni_set_bits(dev, NISTC_INTB_ENA_REG, interrupt_b_bits, 1);
2808 ni_stc_writew(dev, NISTC_AO_CMD1_UI_ARM |
2809 NISTC_AO_CMD1_UC_ARM |
2810 NISTC_AO_CMD1_BC_ARM |
2817 static int ni_ao_insn_config(struct comedi_device *dev,
2818 struct comedi_subdevice *s,
2819 struct comedi_insn *insn, unsigned int *data)
2821 const struct ni_board_struct *board = dev->board_ptr;
2822 struct ni_private *devpriv = dev->private;
2823 unsigned int nbytes;
2826 case INSN_CONFIG_GET_HARDWARE_BUFFER_SIZE:
2829 nbytes = comedi_samples_to_bytes(s,
2830 board->ao_fifo_depth);
2831 data[2] = 1 + nbytes;
2833 data[2] += devpriv->mite->fifo_size;
2842 case INSN_CONFIG_ARM:
2843 return ni_ao_arm(dev, s);
2851 static int ni_ao_inttrig(struct comedi_device *dev,
2852 struct comedi_subdevice *s,
2853 unsigned int trig_num)
2855 struct ni_private *devpriv = dev->private;
2856 struct comedi_cmd *cmd = &s->async->cmd;
2860 * Require trig_num == cmd->start_arg when cmd->start_src == TRIG_INT.
2861 * For backwards compatibility, also allow trig_num == 0 when
2862 * cmd->start_src != TRIG_INT (i.e. when cmd->start_src == TRIG_EXT);
2863 * in that case, the internal trigger is being used as a pre-trigger
2864 * before the external trigger.
2866 if (!(trig_num == cmd->start_arg ||
2867 (trig_num == 0 && cmd->start_src != TRIG_INT)))
2871 * Null trig at beginning prevent ao start trigger from executing more
2872 * than once per command.
2874 s->async->inttrig = NULL;
2876 if (devpriv->ao_needs_arming) {
2877 /* only arm this device if it still needs arming */
2878 ret = ni_ao_arm(dev, s);
2883 ni_stc_writew(dev, NISTC_AO_CMD2_START1_PULSE | devpriv->ao_cmd2,
2891 * Organized similar to NI-STC and MHDDK examples.
2892 * ni_ao_cmd is broken out into configuration sub-routines for clarity.
2895 static void ni_ao_cmd_personalize(struct comedi_device *dev,
2896 const struct comedi_cmd *cmd)
2898 const struct ni_board_struct *board = dev->board_ptr;
2901 ni_stc_writew(dev, NISTC_RESET_AO_CFG_START, NISTC_RESET_REG);
2904 /* fast CPU interface--only eseries */
2905 /* ((slow CPU interface) ? 0 : AO_Fast_CPU) | */
2906 NISTC_AO_PERSONAL_BC_SRC_SEL |
2907 0 /* (use_original_pulse ? 0 : NISTC_AO_PERSONAL_UPDATE_TIMEBASE) */ |
2909 * FIXME: start setting following bit when appropriate. Need to
2910 * determine whether board is E4 or E1.
2912 * if board is E4 or E1
2913 * Set bit "NISTC_AO_PERSONAL_UPDATE_PW" to 0
2917 NISTC_AO_PERSONAL_UPDATE_PW |
2918 /* FIXME: when should we set following bit to zero? */
2919 NISTC_AO_PERSONAL_TMRDACWR_PW |
2920 (board->ao_fifo_depth ?
2921 NISTC_AO_PERSONAL_FIFO_ENA : NISTC_AO_PERSONAL_DMA_PIO_CTRL)
2926 * add something like ".has_individual_dacs = 0" to ni_board_struct
2927 * since, as F Hess pointed out, not all in m series have singles. not
2928 * sure if e-series all have duals...
2932 * F Hess: windows driver does not set NISTC_AO_PERSONAL_NUM_DAC bit for
2933 * 6281, verified with bus analyzer.
2935 if (devpriv->is_m_series)
2936 bits |= NISTC_AO_PERSONAL_NUM_DAC;
2938 ni_stc_writew(dev, bits, NISTC_AO_PERSONAL_REG);
2940 ni_stc_writew(dev, NISTC_RESET_AO_CFG_END, NISTC_RESET_REG);
2943 static void ni_ao_cmd_set_trigger(struct comedi_device *dev,
2944 const struct comedi_cmd *cmd)
2946 struct ni_private *devpriv = dev->private;
2947 unsigned int trigsel;
2949 ni_stc_writew(dev, NISTC_RESET_AO_CFG_START, NISTC_RESET_REG);
2952 if (cmd->stop_src == TRIG_NONE) {
2953 devpriv->ao_mode1 |= NISTC_AO_MODE1_CONTINUOUS;
2954 devpriv->ao_mode1 &= ~NISTC_AO_MODE1_TRIGGER_ONCE;
2956 devpriv->ao_mode1 &= ~NISTC_AO_MODE1_CONTINUOUS;
2957 devpriv->ao_mode1 |= NISTC_AO_MODE1_TRIGGER_ONCE;
2959 ni_stc_writew(dev, devpriv->ao_mode1, NISTC_AO_MODE1_REG);
2961 if (cmd->start_src == TRIG_INT) {
2962 trigsel = NISTC_AO_TRIG_START1_EDGE |
2963 NISTC_AO_TRIG_START1_SYNC;
2964 } else { /* TRIG_EXT */
2965 trigsel = NISTC_AO_TRIG_START1_SEL(CR_CHAN(cmd->start_arg) + 1);
2966 /* 0=active high, 1=active low. see daq-stc 3-24 (p186) */
2967 if (cmd->start_arg & CR_INVERT)
2968 trigsel |= NISTC_AO_TRIG_START1_POLARITY;
2969 /* 0=edge detection disabled, 1=enabled */
2970 if (cmd->start_arg & CR_EDGE)
2971 trigsel |= NISTC_AO_TRIG_START1_EDGE;
2973 ni_stc_writew(dev, trigsel, NISTC_AO_TRIG_SEL_REG);
2975 /* AO_Delayed_START1 = 0, we do not support delayed start...yet */
2978 /* select DA_START1 as PFI6/AO_START1 when configured as an output */
2979 devpriv->ao_mode3 &= ~NISTC_AO_MODE3_TRIG_LEN;
2980 ni_stc_writew(dev, devpriv->ao_mode3, NISTC_AO_MODE3_REG);
2982 ni_stc_writew(dev, NISTC_RESET_AO_CFG_END, NISTC_RESET_REG);
2985 static void ni_ao_cmd_set_counters(struct comedi_device *dev,
2986 const struct comedi_cmd *cmd)
2988 struct ni_private *devpriv = dev->private;
2989 /* Not supporting 'waveform staging' or 'local buffer with pauses' */
2991 ni_stc_writew(dev, NISTC_RESET_AO_CFG_START, NISTC_RESET_REG);
2993 * This relies on ao_mode1/(Trigger_Once | Continuous) being set in
2994 * set_trigger above. It is unclear whether we really need to re-write
2995 * this register with these values. The mhddk examples for e-series
2996 * show writing this in both places, but the examples for m-series show
2997 * a single write in the set_counters function (here).
2999 ni_stc_writew(dev, devpriv->ao_mode1, NISTC_AO_MODE1_REG);
3001 /* sync (upload number of buffer iterations -1) */
3002 /* indicate that we want to use BC_Load_A_Register as the source */
3003 devpriv->ao_mode2 &= ~NISTC_AO_MODE2_BC_INIT_LOAD_SRC;
3004 ni_stc_writew(dev, devpriv->ao_mode2, NISTC_AO_MODE2_REG);
3007 * if the BC_TC interrupt is still issued in spite of UC, BC, UI
3008 * ignoring BC_TC, then we will need to find a way to ignore that
3009 * interrupt in continuous mode.
3011 ni_stc_writel(dev, 0, NISTC_AO_BC_LOADA_REG); /* iter once */
3013 /* sync (issue command to load number of buffer iterations -1) */
3014 ni_stc_writew(dev, NISTC_AO_CMD1_BC_LOAD, NISTC_AO_CMD1_REG);
3016 /* sync (upload number of updates in buffer) */
3017 /* indicate that we want to use UC_Load_A_Register as the source */
3018 devpriv->ao_mode2 &= ~NISTC_AO_MODE2_UC_INIT_LOAD_SRC;
3019 ni_stc_writew(dev, devpriv->ao_mode2, NISTC_AO_MODE2_REG);
3022 * if a user specifies '0', this automatically assumes the entire 24bit
3023 * address space is available for the (multiple iterations of single
3024 * buffer) MISB. Otherwise, stop_arg specifies the MISB length that
3025 * will be used, regardless of whether we are in continuous mode or not.
3026 * In continuous mode, the output will just iterate indefinitely over
3030 unsigned int stop_arg = cmd->stop_arg > 0 ?
3031 (cmd->stop_arg & 0xffffff) : 0xffffff;
3033 if (devpriv->is_m_series) {
3035 * this is how the NI example code does it for m-series
3036 * boards, verified correct with 6259
3038 ni_stc_writel(dev, stop_arg - 1, NISTC_AO_UC_LOADA_REG);
3040 /* sync (issue cmd to load number of updates in MISB) */
3041 ni_stc_writew(dev, NISTC_AO_CMD1_UC_LOAD,
3044 ni_stc_writel(dev, stop_arg, NISTC_AO_UC_LOADA_REG);
3046 /* sync (issue cmd to load number of updates in MISB) */
3047 ni_stc_writew(dev, NISTC_AO_CMD1_UC_LOAD,
3051 * sync (upload number of updates-1 in MISB)
3054 ni_stc_writel(dev, stop_arg - 1, NISTC_AO_UC_LOADA_REG);
3058 ni_stc_writew(dev, NISTC_RESET_AO_CFG_END, NISTC_RESET_REG);
3061 static void ni_ao_cmd_set_update(struct comedi_device *dev,
3062 const struct comedi_cmd *cmd)
3064 struct ni_private *devpriv = dev->private;
3066 ni_stc_writew(dev, NISTC_RESET_AO_CFG_START, NISTC_RESET_REG);
3069 * zero out these bit fields to be set below. Does an ao-reset do this
3072 devpriv->ao_mode1 &= ~(
3073 NISTC_AO_MODE1_UI_SRC_MASK |
3074 NISTC_AO_MODE1_UI_SRC_POLARITY |
3075 NISTC_AO_MODE1_UPDATE_SRC_MASK |
3076 NISTC_AO_MODE1_UPDATE_SRC_POLARITY
3079 if (cmd->scan_begin_src == TRIG_TIMER) {
3080 unsigned int trigvar;
3082 devpriv->ao_cmd2 &= ~NISTC_AO_CMD2_BC_GATE_ENA;
3085 * NOTE: there are several other ways of configuring internal
3086 * updates, but we'll only support one for now: using
3087 * AO_IN_TIMEBASE, w/o waveform staging, w/o a delay between
3088 * START1 and first update, and also w/o local buffer mode w/
3093 * This is already done above:
3094 * devpriv->ao_mode1 &= ~(
3095 * // set UPDATE_Source to UI_TC:
3096 * NISTC_AO_MODE1_UPDATE_SRC_MASK |
3097 * // set UPDATE_Source_Polarity to rising (required?)
3098 * NISTC_AO_MODE1_UPDATE_SRC_POLARITY |
3099 * // set UI_Source to AO_IN_TIMEBASE1:
3100 * NISTC_AO_MODE1_UI_SRC_MASK |
3101 * // set UI_Source_Polarity to rising (required?)
3102 * NISTC_AO_MODE1_UI_SRC_POLARITY
3107 * TODO: use ao_ui_clock_source to allow all possible signals
3108 * to be routed to UI_Source_Select. See tSTC.h for
3109 * eseries/ni67xx and tMSeries.h for mseries.
3112 trigvar = ni_ns_to_timer(dev, cmd->scan_begin_arg,
3113 CMDF_ROUND_NEAREST);
3116 * Wait N TB3 ticks after the start trigger before
3117 * clocking (N must be >=2).
3119 /* following line: 2-1 per STC */
3120 ni_stc_writel(dev, 1, NISTC_AO_UI_LOADA_REG);
3121 ni_stc_writew(dev, NISTC_AO_CMD1_UI_LOAD, NISTC_AO_CMD1_REG);
3122 ni_stc_writel(dev, trigvar, NISTC_AO_UI_LOADA_REG);
3123 } else { /* TRIG_EXT */
3124 /* FIXME: assert scan_begin_arg != 0, ret failure otherwise */
3125 devpriv->ao_cmd2 |= NISTC_AO_CMD2_BC_GATE_ENA;
3126 devpriv->ao_mode1 |= NISTC_AO_MODE1_UPDATE_SRC(
3127 CR_CHAN(cmd->scan_begin_arg));
3128 if (cmd->scan_begin_arg & CR_INVERT)
3129 devpriv->ao_mode1 |= NISTC_AO_MODE1_UPDATE_SRC_POLARITY;
3132 ni_stc_writew(dev, devpriv->ao_cmd2, NISTC_AO_CMD2_REG);
3133 ni_stc_writew(dev, devpriv->ao_mode1, NISTC_AO_MODE1_REG);
3134 devpriv->ao_mode2 &= ~(NISTC_AO_MODE2_UI_RELOAD_MODE(3) |
3135 NISTC_AO_MODE2_UI_INIT_LOAD_SRC);
3136 ni_stc_writew(dev, devpriv->ao_mode2, NISTC_AO_MODE2_REG);
3138 /* Configure DAQ-STC for Timed update mode */
3139 devpriv->ao_cmd1 |= NISTC_AO_CMD1_DAC1_UPDATE_MODE |
3140 NISTC_AO_CMD1_DAC0_UPDATE_MODE;
3141 /* We are not using UPDATE2-->don't have to set DACx_Source_Select */
3142 ni_stc_writew(dev, devpriv->ao_cmd1, NISTC_AO_CMD1_REG);
3144 ni_stc_writew(dev, NISTC_RESET_AO_CFG_END, NISTC_RESET_REG);
3147 static void ni_ao_cmd_set_channels(struct comedi_device *dev,
3148 struct comedi_subdevice *s)
3150 struct ni_private *devpriv = dev->private;
3151 const struct comedi_cmd *cmd = &s->async->cmd;
3152 unsigned int bits = 0;
3154 ni_stc_writew(dev, NISTC_RESET_AO_CFG_START, NISTC_RESET_REG);
3156 if (devpriv->is_6xxx) {
3160 for (i = 0; i < cmd->chanlist_len; ++i) {
3161 int chan = CR_CHAN(cmd->chanlist[i]);
3164 ni_ao_win_outw(dev, chan, NI611X_AO_WAVEFORM_GEN_REG);
3166 ni_ao_win_outw(dev, bits, NI611X_AO_TIMED_REG);
3169 ni_ao_config_chanlist(dev, s, cmd->chanlist, cmd->chanlist_len, 1);
3171 if (cmd->scan_end_arg > 1) {
3172 devpriv->ao_mode1 |= NISTC_AO_MODE1_MULTI_CHAN;
3173 bits = NISTC_AO_OUT_CTRL_CHANS(cmd->scan_end_arg - 1)
3174 | NISTC_AO_OUT_CTRL_UPDATE_SEL_HIGHZ;
3177 devpriv->ao_mode1 &= ~NISTC_AO_MODE1_MULTI_CHAN;
3178 bits = NISTC_AO_OUT_CTRL_UPDATE_SEL_HIGHZ;
3179 if (devpriv->is_m_series | devpriv->is_6xxx)
3180 bits |= NISTC_AO_OUT_CTRL_CHANS(0);
3182 bits |= NISTC_AO_OUT_CTRL_CHANS(
3183 CR_CHAN(cmd->chanlist[0]));
3186 ni_stc_writew(dev, devpriv->ao_mode1, NISTC_AO_MODE1_REG);
3187 ni_stc_writew(dev, bits, NISTC_AO_OUT_CTRL_REG);
3189 ni_stc_writew(dev, NISTC_RESET_AO_CFG_END, NISTC_RESET_REG);
3192 static void ni_ao_cmd_set_stop_conditions(struct comedi_device *dev,
3193 const struct comedi_cmd *cmd)
3195 struct ni_private *devpriv = dev->private;
3197 ni_stc_writew(dev, NISTC_RESET_AO_CFG_START, NISTC_RESET_REG);
3199 devpriv->ao_mode3 |= NISTC_AO_MODE3_STOP_ON_OVERRUN_ERR;
3200 ni_stc_writew(dev, devpriv->ao_mode3, NISTC_AO_MODE3_REG);
3203 * Since we are not supporting waveform staging, we ignore these errors:
3204 * NISTC_AO_MODE3_STOP_ON_BC_TC_ERR,
3205 * NISTC_AO_MODE3_STOP_ON_BC_TC_TRIG_ERR
3208 ni_stc_writew(dev, NISTC_RESET_AO_CFG_END, NISTC_RESET_REG);
3211 static void ni_ao_cmd_set_fifo_mode(struct comedi_device *dev)
3213 struct ni_private *devpriv = dev->private;
3215 ni_stc_writew(dev, NISTC_RESET_AO_CFG_START, NISTC_RESET_REG);
3217 devpriv->ao_mode2 &= ~NISTC_AO_MODE2_FIFO_MODE_MASK;
3219 devpriv->ao_mode2 |= NISTC_AO_MODE2_FIFO_MODE_HF_F;
3221 devpriv->ao_mode2 |= NISTC_AO_MODE2_FIFO_MODE_HF;
3223 /* NOTE: this is where use_onboard_memory=True would be implemented */
3224 devpriv->ao_mode2 &= ~NISTC_AO_MODE2_FIFO_REXMIT_ENA;
3225 ni_stc_writew(dev, devpriv->ao_mode2, NISTC_AO_MODE2_REG);
3227 /* enable sending of ao fifo requests (dma request) */
3228 ni_stc_writew(dev, NISTC_AO_START_AOFREQ_ENA, NISTC_AO_START_SEL_REG);
3230 ni_stc_writew(dev, NISTC_RESET_AO_CFG_END, NISTC_RESET_REG);
3232 /* we are not supporting boards with virtual fifos */
3235 static void ni_ao_cmd_set_interrupts(struct comedi_device *dev,
3236 struct comedi_subdevice *s)
3238 if (s->async->cmd.stop_src == TRIG_COUNT)
3239 ni_set_bits(dev, NISTC_INTB_ENA_REG,
3240 NISTC_INTB_ENA_AO_BC_TC, 1);
3242 s->async->inttrig = ni_ao_inttrig;
3245 static int ni_ao_cmd(struct comedi_device *dev, struct comedi_subdevice *s)
3247 struct ni_private *devpriv = dev->private;
3248 const struct comedi_cmd *cmd = &s->async->cmd;
3250 if (dev->irq == 0) {
3251 dev_err(dev->class_dev, "cannot run command without an irq");
3255 /* ni_ao_reset should have already been done */
3256 ni_ao_cmd_personalize(dev, cmd);
3257 /* clearing fifo and preload happens elsewhere */
3259 ni_ao_cmd_set_trigger(dev, cmd);
3260 ni_ao_cmd_set_counters(dev, cmd);
3261 ni_ao_cmd_set_update(dev, cmd);
3262 ni_ao_cmd_set_channels(dev, s);
3263 ni_ao_cmd_set_stop_conditions(dev, cmd);
3264 ni_ao_cmd_set_fifo_mode(dev);
3265 ni_cmd_set_mite_transfer(devpriv->ao_mite_ring, s, cmd, 0x00ffffff);
3266 ni_ao_cmd_set_interrupts(dev, s);
3269 * arm(ing) must happen later so that DMA can be setup and DACs
3270 * preloaded with the actual output buffer before starting.
3272 * start(ing) must happen _after_ arming is completed. Starting can be
3273 * done either via ni_ao_inttrig, or via an external trigger.
3275 * **Currently, ni_ao_inttrig will automatically attempt a call to
3276 * ni_ao_arm if the device still needs arming at that point. This
3277 * allows backwards compatibility.
3279 devpriv->ao_needs_arming = 1;
3285 static int ni_ao_cmdtest(struct comedi_device *dev, struct comedi_subdevice *s,
3286 struct comedi_cmd *cmd)
3288 const struct ni_board_struct *board = dev->board_ptr;
3289 struct ni_private *devpriv = dev->private;
3293 /* Step 1 : check if triggers are trivially valid */
3295 err |= comedi_check_trigger_src(&cmd->start_src, TRIG_INT | TRIG_EXT);
3296 err |= comedi_check_trigger_src(&cmd->scan_begin_src,
3297 TRIG_TIMER | TRIG_EXT);
3298 err |= comedi_check_trigger_src(&cmd->convert_src, TRIG_NOW);
3299 err |= comedi_check_trigger_src(&cmd->scan_end_src, TRIG_COUNT);
3300 err |= comedi_check_trigger_src(&cmd->stop_src, TRIG_COUNT | TRIG_NONE);
3305 /* Step 2a : make sure trigger sources are unique */
3307 err |= comedi_check_trigger_is_unique(cmd->start_src);
3308 err |= comedi_check_trigger_is_unique(cmd->scan_begin_src);
3309 err |= comedi_check_trigger_is_unique(cmd->stop_src);
3311 /* Step 2b : and mutually compatible */
3316 /* Step 3: check if arguments are trivially valid */
3318 switch (cmd->start_src) {
3320 err |= comedi_check_trigger_arg_is(&cmd->start_arg, 0);
3323 tmp = CR_CHAN(cmd->start_arg);
3327 tmp |= (cmd->start_arg & (CR_INVERT | CR_EDGE));
3328 err |= comedi_check_trigger_arg_is(&cmd->start_arg, tmp);
3332 if (cmd->scan_begin_src == TRIG_TIMER) {
3333 err |= comedi_check_trigger_arg_min(&cmd->scan_begin_arg,
3335 err |= comedi_check_trigger_arg_max(&cmd->scan_begin_arg,
3340 err |= comedi_check_trigger_arg_is(&cmd->convert_arg, 0);
3341 err |= comedi_check_trigger_arg_is(&cmd->scan_end_arg,
3343 err |= comedi_check_trigger_arg_max(&cmd->stop_arg, 0x00ffffff);
3348 /* step 4: fix up any arguments */
3349 if (cmd->scan_begin_src == TRIG_TIMER) {
3350 tmp = cmd->scan_begin_arg;
3351 cmd->scan_begin_arg =
3352 ni_timer_to_ns(dev, ni_ns_to_timer(dev,
3353 cmd->scan_begin_arg,
3355 if (tmp != cmd->scan_begin_arg)
3364 static int ni_ao_reset(struct comedi_device *dev, struct comedi_subdevice *s)
3366 /* See 3.6.1.2 "Resetting", of DAQ-STC Technical Reference Manual */
3369 * In the following, the "--sync" comments are meant to denote
3370 * asynchronous boundaries for setting the registers as described in the
3371 * DAQ-STC mostly in the order also described in the DAQ-STC.
3374 struct ni_private *devpriv = dev->private;
3376 ni_release_ao_mite_channel(dev);
3378 /* --sync (reset AO) */
3379 if (devpriv->is_m_series)
3380 /* following example in mhddk for m-series */
3381 ni_stc_writew(dev, NISTC_RESET_AO, NISTC_RESET_REG);
3383 /*--sync (start config) */
3384 ni_stc_writew(dev, NISTC_RESET_AO_CFG_START, NISTC_RESET_REG);
3386 /*--sync (Disarm) */
3387 ni_stc_writew(dev, NISTC_AO_CMD1_DISARM, NISTC_AO_CMD1_REG);
3391 * (clear bunch of registers--mseries mhddk examples do not include
3394 devpriv->ao_cmd1 = 0;
3395 devpriv->ao_cmd2 = 0;
3396 devpriv->ao_mode1 = 0;
3397 devpriv->ao_mode2 = 0;
3398 if (devpriv->is_m_series)
3399 devpriv->ao_mode3 = NISTC_AO_MODE3_LAST_GATE_DISABLE;
3401 devpriv->ao_mode3 = 0;
3403 ni_stc_writew(dev, 0, NISTC_AO_PERSONAL_REG);
3404 ni_stc_writew(dev, 0, NISTC_AO_CMD1_REG);
3405 ni_stc_writew(dev, 0, NISTC_AO_CMD2_REG);
3406 ni_stc_writew(dev, 0, NISTC_AO_MODE1_REG);
3407 ni_stc_writew(dev, 0, NISTC_AO_MODE2_REG);
3408 ni_stc_writew(dev, 0, NISTC_AO_OUT_CTRL_REG);
3409 ni_stc_writew(dev, devpriv->ao_mode3, NISTC_AO_MODE3_REG);
3410 ni_stc_writew(dev, 0, NISTC_AO_START_SEL_REG);
3411 ni_stc_writew(dev, 0, NISTC_AO_TRIG_SEL_REG);
3413 /*--sync (disable interrupts) */
3414 ni_set_bits(dev, NISTC_INTB_ENA_REG, ~0, 0);
3417 ni_stc_writew(dev, NISTC_AO_PERSONAL_BC_SRC_SEL, NISTC_AO_PERSONAL_REG);
3418 ni_stc_writew(dev, NISTC_INTB_ACK_AO_ALL, NISTC_INTB_ACK_REG);
3420 /*--not in DAQ-STC. which doc? */
3421 if (devpriv->is_6xxx) {
3422 ni_ao_win_outw(dev, (1u << s->n_chan) - 1u,
3423 NI671X_AO_IMMEDIATE_REG);
3424 ni_ao_win_outw(dev, NI611X_AO_MISC_CLEAR_WG,
3425 NI611X_AO_MISC_REG);
3427 ni_stc_writew(dev, NISTC_RESET_AO_CFG_END, NISTC_RESET_REG);
3435 static int ni_dio_insn_config(struct comedi_device *dev,
3436 struct comedi_subdevice *s,
3437 struct comedi_insn *insn,
3440 struct ni_private *devpriv = dev->private;
3443 ret = comedi_dio_insn_config(dev, s, insn, data, 0);
3447 devpriv->dio_control &= ~NISTC_DIO_CTRL_DIR_MASK;
3448 devpriv->dio_control |= NISTC_DIO_CTRL_DIR(s->io_bits);
3449 ni_stc_writew(dev, devpriv->dio_control, NISTC_DIO_CTRL_REG);
3454 static int ni_dio_insn_bits(struct comedi_device *dev,
3455 struct comedi_subdevice *s,
3456 struct comedi_insn *insn,
3459 struct ni_private *devpriv = dev->private;
3461 /* Make sure we're not using the serial part of the dio */
3462 if ((data[0] & (NISTC_DIO_SDIN | NISTC_DIO_SDOUT)) &&
3463 devpriv->serial_interval_ns)
3466 if (comedi_dio_update_state(s, data)) {
3467 devpriv->dio_output &= ~NISTC_DIO_OUT_PARALLEL_MASK;
3468 devpriv->dio_output |= NISTC_DIO_OUT_PARALLEL(s->state);
3469 ni_stc_writew(dev, devpriv->dio_output, NISTC_DIO_OUT_REG);
3472 data[1] = ni_stc_readw(dev, NISTC_DIO_IN_REG);
3478 static int ni_m_series_dio_insn_config(struct comedi_device *dev,
3479 struct comedi_subdevice *s,
3480 struct comedi_insn *insn,
3485 ret = comedi_dio_insn_config(dev, s, insn, data, 0);
3489 ni_writel(dev, s->io_bits, NI_M_DIO_DIR_REG);
3494 static int ni_m_series_dio_insn_bits(struct comedi_device *dev,
3495 struct comedi_subdevice *s,
3496 struct comedi_insn *insn,
3499 if (comedi_dio_update_state(s, data))
3500 ni_writel(dev, s->state, NI_M_DIO_REG);
3502 data[1] = ni_readl(dev, NI_M_DIO_REG);
3507 static int ni_cdio_check_chanlist(struct comedi_device *dev,
3508 struct comedi_subdevice *s,
3509 struct comedi_cmd *cmd)
3513 for (i = 0; i < cmd->chanlist_len; ++i) {
3514 unsigned int chan = CR_CHAN(cmd->chanlist[i]);
3523 static int ni_cdio_cmdtest(struct comedi_device *dev,
3524 struct comedi_subdevice *s, struct comedi_cmd *cmd)
3526 unsigned int bytes_per_scan;
3530 /* Step 1 : check if triggers are trivially valid */
3532 err |= comedi_check_trigger_src(&cmd->start_src, TRIG_INT);
3533 err |= comedi_check_trigger_src(&cmd->scan_begin_src, TRIG_EXT);
3534 err |= comedi_check_trigger_src(&cmd->convert_src, TRIG_NOW);
3535 err |= comedi_check_trigger_src(&cmd->scan_end_src, TRIG_COUNT);
3536 err |= comedi_check_trigger_src(&cmd->stop_src, TRIG_NONE);
3541 /* Step 2a : make sure trigger sources are unique */
3542 /* Step 2b : and mutually compatible */
3544 /* Step 3: check if arguments are trivially valid */
3546 err |= comedi_check_trigger_arg_is(&cmd->start_arg, 0);
3548 tmp = cmd->scan_begin_arg;
3549 tmp &= CR_PACK_FLAGS(NI_M_CDO_MODE_SAMPLE_SRC_MASK, 0, 0, CR_INVERT);
3550 if (tmp != cmd->scan_begin_arg)
3553 err |= comedi_check_trigger_arg_is(&cmd->convert_arg, 0);
3554 err |= comedi_check_trigger_arg_is(&cmd->scan_end_arg,
3556 bytes_per_scan = comedi_bytes_per_scan_cmd(s, cmd);
3557 if (bytes_per_scan) {
3558 err |= comedi_check_trigger_arg_max(&cmd->stop_arg,
3559 s->async->prealloc_bufsz /
3566 /* Step 4: fix up any arguments */
3568 /* Step 5: check channel list if it exists */
3570 if (cmd->chanlist && cmd->chanlist_len > 0)
3571 err |= ni_cdio_check_chanlist(dev, s, cmd);
3579 static int ni_cdo_inttrig(struct comedi_device *dev,
3580 struct comedi_subdevice *s,
3581 unsigned int trig_num)
3583 struct comedi_cmd *cmd = &s->async->cmd;
3584 const unsigned int timeout = 1000;
3587 struct ni_private *devpriv = dev->private;
3588 unsigned long flags;
3590 if (trig_num != cmd->start_arg)
3593 s->async->inttrig = NULL;
3595 /* read alloc the entire buffer */
3596 comedi_buf_read_alloc(s, s->async->prealloc_bufsz);
3598 spin_lock_irqsave(&devpriv->mite_channel_lock, flags);
3599 if (devpriv->cdo_mite_chan) {
3600 mite_prep_dma(devpriv->cdo_mite_chan, 32, 32);
3601 mite_dma_arm(devpriv->cdo_mite_chan);
3603 dev_err(dev->class_dev, "BUG: no cdo mite channel?\n");
3606 spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
3611 * XXX not sure what interrupt C group does
3612 * wait for dma to fill output fifo
3613 * ni_writeb(dev, NI_M_INTC_ENA, NI_M_INTC_ENA_REG);
3615 for (i = 0; i < timeout; ++i) {
3616 if (ni_readl(dev, NI_M_CDIO_STATUS_REG) &
3617 NI_M_CDIO_STATUS_CDO_FIFO_FULL)
3619 usleep_range(10, 100);
3622 dev_err(dev->class_dev, "dma failed to fill cdo fifo!\n");
3626 ni_writel(dev, NI_M_CDO_CMD_ARM |
3627 NI_M_CDO_CMD_ERR_INT_ENA_SET |
3628 NI_M_CDO_CMD_F_E_INT_ENA_SET,
3633 static int ni_cdio_cmd(struct comedi_device *dev, struct comedi_subdevice *s)
3635 struct ni_private *devpriv = dev->private;
3636 const struct comedi_cmd *cmd = &s->async->cmd;
3637 unsigned int cdo_mode_bits;
3640 ni_writel(dev, NI_M_CDO_CMD_RESET, NI_M_CDIO_CMD_REG);
3641 cdo_mode_bits = NI_M_CDO_MODE_FIFO_MODE |
3642 NI_M_CDO_MODE_HALT_ON_ERROR |
3643 NI_M_CDO_MODE_SAMPLE_SRC(CR_CHAN(cmd->scan_begin_arg));
3644 if (cmd->scan_begin_arg & CR_INVERT)
3645 cdo_mode_bits |= NI_M_CDO_MODE_POLARITY;
3646 ni_writel(dev, cdo_mode_bits, NI_M_CDO_MODE_REG);
3648 ni_writel(dev, s->state, NI_M_CDO_FIFO_DATA_REG);
3649 ni_writel(dev, NI_M_CDO_CMD_SW_UPDATE, NI_M_CDIO_CMD_REG);
3650 ni_writel(dev, s->io_bits, NI_M_CDO_MASK_ENA_REG);
3652 dev_err(dev->class_dev,
3653 "attempted to run digital output command with no lines configured as outputs\n");
3656 retval = ni_request_cdo_mite_channel(dev);
3660 ni_cmd_set_mite_transfer(devpriv->cdo_mite_ring, s, cmd,
3661 s->async->prealloc_bufsz /
3662 comedi_bytes_per_scan(s));
3664 s->async->inttrig = ni_cdo_inttrig;
3669 static int ni_cdio_cancel(struct comedi_device *dev, struct comedi_subdevice *s)
3671 ni_writel(dev, NI_M_CDO_CMD_DISARM |
3672 NI_M_CDO_CMD_ERR_INT_ENA_CLR |
3673 NI_M_CDO_CMD_F_E_INT_ENA_CLR |
3674 NI_M_CDO_CMD_F_REQ_INT_ENA_CLR,
3677 * XXX not sure what interrupt C group does
3678 * ni_writeb(dev, 0, NI_M_INTC_ENA_REG);
3680 ni_writel(dev, 0, NI_M_CDO_MASK_ENA_REG);
3681 ni_release_cdo_mite_channel(dev);
3685 static void handle_cdio_interrupt(struct comedi_device *dev)
3687 struct ni_private *devpriv = dev->private;
3688 unsigned int cdio_status;
3689 struct comedi_subdevice *s = &dev->subdevices[NI_DIO_SUBDEV];
3690 unsigned long flags;
3692 spin_lock_irqsave(&devpriv->mite_channel_lock, flags);
3693 if (devpriv->cdo_mite_chan)
3694 mite_ack_linkc(devpriv->cdo_mite_chan, s, true);
3695 spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
3697 cdio_status = ni_readl(dev, NI_M_CDIO_STATUS_REG);
3698 if (cdio_status & NI_M_CDIO_STATUS_CDO_ERROR) {
3699 /* XXX just guessing this is needed and does something useful */
3700 ni_writel(dev, NI_M_CDO_CMD_ERR_INT_CONFIRM,
3702 s->async->events |= COMEDI_CB_OVERFLOW;
3704 if (cdio_status & NI_M_CDIO_STATUS_CDO_FIFO_EMPTY) {
3705 ni_writel(dev, NI_M_CDO_CMD_F_E_INT_ENA_CLR,
3707 /* s->async->events |= COMEDI_CB_EOA; */
3709 comedi_handle_events(dev, s);
3713 static int ni_serial_hw_readwrite8(struct comedi_device *dev,
3714 struct comedi_subdevice *s,
3715 unsigned char data_out,
3716 unsigned char *data_in)
3718 struct ni_private *devpriv = dev->private;
3719 unsigned int status1;
3720 int err = 0, count = 20;
3722 devpriv->dio_output &= ~NISTC_DIO_OUT_SERIAL_MASK;
3723 devpriv->dio_output |= NISTC_DIO_OUT_SERIAL(data_out);
3724 ni_stc_writew(dev, devpriv->dio_output, NISTC_DIO_OUT_REG);
3726 status1 = ni_stc_readw(dev, NISTC_STATUS1_REG);
3727 if (status1 & NISTC_STATUS1_SERIO_IN_PROG) {
3732 devpriv->dio_control |= NISTC_DIO_CTRL_HW_SER_START;
3733 ni_stc_writew(dev, devpriv->dio_control, NISTC_DIO_CTRL_REG);
3734 devpriv->dio_control &= ~NISTC_DIO_CTRL_HW_SER_START;
3736 /* Wait until STC says we're done, but don't loop infinitely. */
3737 while ((status1 = ni_stc_readw(dev, NISTC_STATUS1_REG)) &
3738 NISTC_STATUS1_SERIO_IN_PROG) {
3739 /* Delay one bit per loop */
3740 udelay((devpriv->serial_interval_ns + 999) / 1000);
3742 dev_err(dev->class_dev,
3743 "SPI serial I/O didn't finish in time!\n");
3750 * Delay for last bit. This delay is absolutely necessary, because
3751 * NISTC_STATUS1_SERIO_IN_PROG goes high one bit too early.
3753 udelay((devpriv->serial_interval_ns + 999) / 1000);
3756 *data_in = ni_stc_readw(dev, NISTC_DIO_SERIAL_IN_REG);
3759 ni_stc_writew(dev, devpriv->dio_control, NISTC_DIO_CTRL_REG);
3764 static int ni_serial_sw_readwrite8(struct comedi_device *dev,
3765 struct comedi_subdevice *s,
3766 unsigned char data_out,
3767 unsigned char *data_in)
3769 struct ni_private *devpriv = dev->private;
3770 unsigned char mask, input = 0;
3772 /* Wait for one bit before transfer */
3773 udelay((devpriv->serial_interval_ns + 999) / 1000);
3775 for (mask = 0x80; mask; mask >>= 1) {
3777 * Output current bit; note that we cannot touch s->state
3778 * because it is a per-subdevice field, and serial is
3779 * a separate subdevice from DIO.
3781 devpriv->dio_output &= ~NISTC_DIO_SDOUT;
3782 if (data_out & mask)
3783 devpriv->dio_output |= NISTC_DIO_SDOUT;
3784 ni_stc_writew(dev, devpriv->dio_output, NISTC_DIO_OUT_REG);
3787 * Assert SDCLK (active low, inverted), wait for half of
3788 * the delay, deassert SDCLK, and wait for the other half.
3790 devpriv->dio_control |= NISTC_DIO_SDCLK;
3791 ni_stc_writew(dev, devpriv->dio_control, NISTC_DIO_CTRL_REG);
3793 udelay((devpriv->serial_interval_ns + 999) / 2000);
3795 devpriv->dio_control &= ~NISTC_DIO_SDCLK;
3796 ni_stc_writew(dev, devpriv->dio_control, NISTC_DIO_CTRL_REG);
3798 udelay((devpriv->serial_interval_ns + 999) / 2000);
3800 /* Input current bit */
3801 if (ni_stc_readw(dev, NISTC_DIO_IN_REG) & NISTC_DIO_SDIN)
3811 static int ni_serial_insn_config(struct comedi_device *dev,
3812 struct comedi_subdevice *s,
3813 struct comedi_insn *insn,
3816 struct ni_private *devpriv = dev->private;
3817 unsigned int clk_fout = devpriv->clock_and_fout;
3819 unsigned char byte_out, byte_in = 0;
3825 case INSN_CONFIG_SERIAL_CLOCK:
3826 devpriv->serial_hw_mode = 1;
3827 devpriv->dio_control |= NISTC_DIO_CTRL_HW_SER_ENA;
3829 if (data[1] == SERIAL_DISABLED) {
3830 devpriv->serial_hw_mode = 0;
3831 devpriv->dio_control &= ~(NISTC_DIO_CTRL_HW_SER_ENA |
3833 data[1] = SERIAL_DISABLED;
3834 devpriv->serial_interval_ns = data[1];
3835 } else if (data[1] <= SERIAL_600NS) {
3837 * Warning: this clock speed is too fast to reliably
3840 devpriv->dio_control &= ~NISTC_DIO_CTRL_HW_SER_TIMEBASE;
3841 clk_fout |= NISTC_CLK_FOUT_SLOW_TIMEBASE;
3842 clk_fout &= ~NISTC_CLK_FOUT_DIO_SER_OUT_DIV2;
3843 data[1] = SERIAL_600NS;
3844 devpriv->serial_interval_ns = data[1];
3845 } else if (data[1] <= SERIAL_1_2US) {
3846 devpriv->dio_control &= ~NISTC_DIO_CTRL_HW_SER_TIMEBASE;
3847 clk_fout |= NISTC_CLK_FOUT_SLOW_TIMEBASE |
3848 NISTC_CLK_FOUT_DIO_SER_OUT_DIV2;
3849 data[1] = SERIAL_1_2US;
3850 devpriv->serial_interval_ns = data[1];
3851 } else if (data[1] <= SERIAL_10US) {
3852 devpriv->dio_control |= NISTC_DIO_CTRL_HW_SER_TIMEBASE;
3853 clk_fout |= NISTC_CLK_FOUT_SLOW_TIMEBASE |
3854 NISTC_CLK_FOUT_DIO_SER_OUT_DIV2;
3856 * Note: NISTC_CLK_FOUT_DIO_SER_OUT_DIV2 only affects
3857 * 600ns/1.2us. If you turn divide_by_2 off with the
3858 * slow clock, you will still get 10us, except then
3859 * all your delays are wrong.
3861 data[1] = SERIAL_10US;
3862 devpriv->serial_interval_ns = data[1];
3864 devpriv->dio_control &= ~(NISTC_DIO_CTRL_HW_SER_ENA |
3866 devpriv->serial_hw_mode = 0;
3867 data[1] = (data[1] / 1000) * 1000;
3868 devpriv->serial_interval_ns = data[1];
3870 devpriv->clock_and_fout = clk_fout;
3872 ni_stc_writew(dev, devpriv->dio_control, NISTC_DIO_CTRL_REG);
3873 ni_stc_writew(dev, devpriv->clock_and_fout, NISTC_CLK_FOUT_REG);
3876 case INSN_CONFIG_BIDIRECTIONAL_DATA:
3878 if (devpriv->serial_interval_ns == 0)
3881 byte_out = data[1] & 0xFF;
3883 if (devpriv->serial_hw_mode) {
3884 err = ni_serial_hw_readwrite8(dev, s, byte_out,
3886 } else if (devpriv->serial_interval_ns > 0) {
3887 err = ni_serial_sw_readwrite8(dev, s, byte_out,
3890 dev_err(dev->class_dev, "serial disabled!\n");
3895 data[1] = byte_in & 0xFF;
3904 static void init_ao_67xx(struct comedi_device *dev, struct comedi_subdevice *s)
3908 for (i = 0; i < s->n_chan; i++) {
3909 ni_ao_win_outw(dev, NI_E_AO_DACSEL(i) | 0x0,
3910 NI67XX_AO_CFG2_REG);
3912 ni_ao_win_outw(dev, 0x0, NI67XX_AO_SP_UPDATES_REG);
3915 static const struct mio_regmap ni_gpct_to_stc_regmap[] = {
3916 [NITIO_G0_AUTO_INC] = { NISTC_G0_AUTOINC_REG, 2 },
3917 [NITIO_G1_AUTO_INC] = { NISTC_G1_AUTOINC_REG, 2 },
3918 [NITIO_G0_CMD] = { NISTC_G0_CMD_REG, 2 },
3919 [NITIO_G1_CMD] = { NISTC_G1_CMD_REG, 2 },
3920 [NITIO_G0_HW_SAVE] = { NISTC_G0_HW_SAVE_REG, 4 },
3921 [NITIO_G1_HW_SAVE] = { NISTC_G1_HW_SAVE_REG, 4 },
3922 [NITIO_G0_SW_SAVE] = { NISTC_G0_SAVE_REG, 4 },
3923 [NITIO_G1_SW_SAVE] = { NISTC_G1_SAVE_REG, 4 },
3924 [NITIO_G0_MODE] = { NISTC_G0_MODE_REG, 2 },
3925 [NITIO_G1_MODE] = { NISTC_G1_MODE_REG, 2 },
3926 [NITIO_G0_LOADA] = { NISTC_G0_LOADA_REG, 4 },
3927 [NITIO_G1_LOADA] = { NISTC_G1_LOADA_REG, 4 },
3928 [NITIO_G0_LOADB] = { NISTC_G0_LOADB_REG, 4 },
3929 [NITIO_G1_LOADB] = { NISTC_G1_LOADB_REG, 4 },
3930 [NITIO_G0_INPUT_SEL] = { NISTC_G0_INPUT_SEL_REG, 2 },
3931 [NITIO_G1_INPUT_SEL] = { NISTC_G1_INPUT_SEL_REG, 2 },
3932 [NITIO_G0_CNT_MODE] = { 0x1b0, 2 }, /* M-Series only */
3933 [NITIO_G1_CNT_MODE] = { 0x1b2, 2 }, /* M-Series only */
3934 [NITIO_G0_GATE2] = { 0x1b4, 2 }, /* M-Series only */
3935 [NITIO_G1_GATE2] = { 0x1b6, 2 }, /* M-Series only */
3936 [NITIO_G01_STATUS] = { NISTC_G01_STATUS_REG, 2 },
3937 [NITIO_G01_RESET] = { NISTC_RESET_REG, 2 },
3938 [NITIO_G01_STATUS1] = { NISTC_STATUS1_REG, 2 },
3939 [NITIO_G01_STATUS2] = { NISTC_STATUS2_REG, 2 },
3940 [NITIO_G0_DMA_CFG] = { 0x1b8, 2 }, /* M-Series only */
3941 [NITIO_G1_DMA_CFG] = { 0x1ba, 2 }, /* M-Series only */
3942 [NITIO_G0_DMA_STATUS] = { 0x1b8, 2 }, /* M-Series only */
3943 [NITIO_G1_DMA_STATUS] = { 0x1ba, 2 }, /* M-Series only */
3944 [NITIO_G0_ABZ] = { 0x1c0, 2 }, /* M-Series only */
3945 [NITIO_G1_ABZ] = { 0x1c2, 2 }, /* M-Series only */
3946 [NITIO_G0_INT_ACK] = { NISTC_INTA_ACK_REG, 2 },
3947 [NITIO_G1_INT_ACK] = { NISTC_INTB_ACK_REG, 2 },
3948 [NITIO_G0_STATUS] = { NISTC_AI_STATUS1_REG, 2 },
3949 [NITIO_G1_STATUS] = { NISTC_AO_STATUS1_REG, 2 },
3950 [NITIO_G0_INT_ENA] = { NISTC_INTA_ENA_REG, 2 },
3951 [NITIO_G1_INT_ENA] = { NISTC_INTB_ENA_REG, 2 },
3954 static unsigned int ni_gpct_to_stc_register(struct comedi_device *dev,
3955 enum ni_gpct_register reg)
3957 const struct mio_regmap *regmap;
3959 if (reg < ARRAY_SIZE(ni_gpct_to_stc_regmap)) {
3960 regmap = &ni_gpct_to_stc_regmap[reg];
3962 dev_warn(dev->class_dev, "%s: unhandled register=0x%x\n",
3967 return regmap->mio_reg;
3970 static void ni_gpct_write_register(struct ni_gpct *counter, unsigned int bits,
3971 enum ni_gpct_register reg)
3973 struct comedi_device *dev = counter->counter_dev->dev;
3974 unsigned int stc_register = ni_gpct_to_stc_register(dev, reg);
3976 if (stc_register == 0)
3980 /* m-series only registers */
3981 case NITIO_G0_CNT_MODE:
3982 case NITIO_G1_CNT_MODE:
3983 case NITIO_G0_GATE2:
3984 case NITIO_G1_GATE2:
3985 case NITIO_G0_DMA_CFG:
3986 case NITIO_G1_DMA_CFG:
3989 ni_writew(dev, bits, stc_register);
3992 /* 32 bit registers */
3993 case NITIO_G0_LOADA:
3994 case NITIO_G1_LOADA:
3995 case NITIO_G0_LOADB:
3996 case NITIO_G1_LOADB:
3997 ni_stc_writel(dev, bits, stc_register);
4000 /* 16 bit registers */
4001 case NITIO_G0_INT_ENA:
4002 ni_set_bitfield(dev, stc_register,
4003 NISTC_INTA_ENA_G0_GATE | NISTC_INTA_ENA_G0_TC,
4006 case NITIO_G1_INT_ENA:
4007 ni_set_bitfield(dev, stc_register,
4008 NISTC_INTB_ENA_G1_GATE | NISTC_INTB_ENA_G1_TC,
4012 ni_stc_writew(dev, bits, stc_register);
4016 static unsigned int ni_gpct_read_register(struct ni_gpct *counter,
4017 enum ni_gpct_register reg)
4019 struct comedi_device *dev = counter->counter_dev->dev;
4020 unsigned int stc_register = ni_gpct_to_stc_register(dev, reg);
4022 if (stc_register == 0)
4026 /* m-series only registers */
4027 case NITIO_G0_DMA_STATUS:
4028 case NITIO_G1_DMA_STATUS:
4029 return ni_readw(dev, stc_register);
4031 /* 32 bit registers */
4032 case NITIO_G0_HW_SAVE:
4033 case NITIO_G1_HW_SAVE:
4034 case NITIO_G0_SW_SAVE:
4035 case NITIO_G1_SW_SAVE:
4036 return ni_stc_readl(dev, stc_register);
4038 /* 16 bit registers */
4040 return ni_stc_readw(dev, stc_register);
4044 static int ni_freq_out_insn_read(struct comedi_device *dev,
4045 struct comedi_subdevice *s,
4046 struct comedi_insn *insn,
4049 struct ni_private *devpriv = dev->private;
4050 unsigned int val = NISTC_CLK_FOUT_TO_DIVIDER(devpriv->clock_and_fout);
4053 for (i = 0; i < insn->n; i++)
4059 static int ni_freq_out_insn_write(struct comedi_device *dev,
4060 struct comedi_subdevice *s,
4061 struct comedi_insn *insn,
4064 struct ni_private *devpriv = dev->private;
4067 unsigned int val = data[insn->n - 1];
4069 devpriv->clock_and_fout &= ~NISTC_CLK_FOUT_ENA;
4070 ni_stc_writew(dev, devpriv->clock_and_fout, NISTC_CLK_FOUT_REG);
4071 devpriv->clock_and_fout &= ~NISTC_CLK_FOUT_DIVIDER_MASK;
4073 /* use the last data value to set the fout divider */
4074 devpriv->clock_and_fout |= NISTC_CLK_FOUT_DIVIDER(val);
4076 devpriv->clock_and_fout |= NISTC_CLK_FOUT_ENA;
4077 ni_stc_writew(dev, devpriv->clock_and_fout, NISTC_CLK_FOUT_REG);
4082 static int ni_freq_out_insn_config(struct comedi_device *dev,
4083 struct comedi_subdevice *s,
4084 struct comedi_insn *insn,
4087 struct ni_private *devpriv = dev->private;
4090 case INSN_CONFIG_SET_CLOCK_SRC:
4092 case NI_FREQ_OUT_TIMEBASE_1_DIV_2_CLOCK_SRC:
4093 devpriv->clock_and_fout &= ~NISTC_CLK_FOUT_TIMEBASE_SEL;
4095 case NI_FREQ_OUT_TIMEBASE_2_CLOCK_SRC:
4096 devpriv->clock_and_fout |= NISTC_CLK_FOUT_TIMEBASE_SEL;
4101 ni_stc_writew(dev, devpriv->clock_and_fout, NISTC_CLK_FOUT_REG);
4103 case INSN_CONFIG_GET_CLOCK_SRC:
4104 if (devpriv->clock_and_fout & NISTC_CLK_FOUT_TIMEBASE_SEL) {
4105 data[1] = NI_FREQ_OUT_TIMEBASE_2_CLOCK_SRC;
4106 data[2] = TIMEBASE_2_NS;
4108 data[1] = NI_FREQ_OUT_TIMEBASE_1_DIV_2_CLOCK_SRC;
4109 data[2] = TIMEBASE_1_NS * 2;
4118 static int ni_8255_callback(struct comedi_device *dev,
4119 int dir, int port, int data, unsigned long iobase)
4122 ni_writeb(dev, data, iobase + 2 * port);
4126 return ni_readb(dev, iobase + 2 * port);
4129 static int ni_get_pwm_config(struct comedi_device *dev, unsigned int *data)
4131 struct ni_private *devpriv = dev->private;
4133 data[1] = devpriv->pwm_up_count * devpriv->clock_ns;
4134 data[2] = devpriv->pwm_down_count * devpriv->clock_ns;
4138 static int ni_m_series_pwm_config(struct comedi_device *dev,
4139 struct comedi_subdevice *s,
4140 struct comedi_insn *insn,
4143 struct ni_private *devpriv = dev->private;
4144 unsigned int up_count, down_count;
4147 case INSN_CONFIG_PWM_OUTPUT:
4149 case CMDF_ROUND_NEAREST:
4150 up_count = DIV_ROUND_CLOSEST(data[2],
4153 case CMDF_ROUND_DOWN:
4154 up_count = data[2] / devpriv->clock_ns;
4158 DIV_ROUND_UP(data[2], devpriv->clock_ns);
4164 case CMDF_ROUND_NEAREST:
4165 down_count = DIV_ROUND_CLOSEST(data[4],
4168 case CMDF_ROUND_DOWN:
4169 down_count = data[4] / devpriv->clock_ns;
4173 DIV_ROUND_UP(data[4], devpriv->clock_ns);
4178 if (up_count * devpriv->clock_ns != data[2] ||
4179 down_count * devpriv->clock_ns != data[4]) {
4180 data[2] = up_count * devpriv->clock_ns;
4181 data[4] = down_count * devpriv->clock_ns;
4184 ni_writel(dev, NI_M_CAL_PWM_HIGH_TIME(up_count) |
4185 NI_M_CAL_PWM_LOW_TIME(down_count),
4187 devpriv->pwm_up_count = up_count;
4188 devpriv->pwm_down_count = down_count;
4190 case INSN_CONFIG_GET_PWM_OUTPUT:
4191 return ni_get_pwm_config(dev, data);
4198 static int ni_6143_pwm_config(struct comedi_device *dev,
4199 struct comedi_subdevice *s,
4200 struct comedi_insn *insn,
4203 struct ni_private *devpriv = dev->private;
4204 unsigned int up_count, down_count;
4207 case INSN_CONFIG_PWM_OUTPUT:
4209 case CMDF_ROUND_NEAREST:
4210 up_count = DIV_ROUND_CLOSEST(data[2],
4213 case CMDF_ROUND_DOWN:
4214 up_count = data[2] / devpriv->clock_ns;
4218 DIV_ROUND_UP(data[2], devpriv->clock_ns);
4224 case CMDF_ROUND_NEAREST:
4225 down_count = DIV_ROUND_CLOSEST(data[4],
4228 case CMDF_ROUND_DOWN:
4229 down_count = data[4] / devpriv->clock_ns;
4233 DIV_ROUND_UP(data[4], devpriv->clock_ns);
4238 if (up_count * devpriv->clock_ns != data[2] ||
4239 down_count * devpriv->clock_ns != data[4]) {
4240 data[2] = up_count * devpriv->clock_ns;
4241 data[4] = down_count * devpriv->clock_ns;
4244 ni_writel(dev, up_count, NI6143_CALIB_HI_TIME_REG);
4245 devpriv->pwm_up_count = up_count;
4246 ni_writel(dev, down_count, NI6143_CALIB_LO_TIME_REG);
4247 devpriv->pwm_down_count = down_count;
4249 case INSN_CONFIG_GET_PWM_OUTPUT:
4250 return ni_get_pwm_config(dev, data);
4257 static int pack_mb88341(int addr, int val, int *bitstring)
4261 * Note that address bits are reversed. Thanks to
4262 * Ingo Keen for noticing this.
4264 * Note also that the 88341 expects address values from
4265 * 1-12, whereas we use channel numbers 0-11. The NI
4266 * docs use 1-12, also, so be careful here.
4269 *bitstring = ((addr & 0x1) << 11) |
4270 ((addr & 0x2) << 9) |
4271 ((addr & 0x4) << 7) | ((addr & 0x8) << 5) | (val & 0xff);
4275 static int pack_dac8800(int addr, int val, int *bitstring)
4277 *bitstring = ((addr & 0x7) << 8) | (val & 0xff);
4281 static int pack_dac8043(int addr, int val, int *bitstring)
4283 *bitstring = val & 0xfff;
4287 static int pack_ad8522(int addr, int val, int *bitstring)
4289 *bitstring = (val & 0xfff) | (addr ? 0xc000 : 0xa000);
4293 static int pack_ad8804(int addr, int val, int *bitstring)
4295 *bitstring = ((addr & 0xf) << 8) | (val & 0xff);
4299 static int pack_ad8842(int addr, int val, int *bitstring)
4301 *bitstring = ((addr + 1) << 8) | (val & 0xff);
4305 struct caldac_struct {
4308 int (*packbits)(int, int, int *);
4311 static struct caldac_struct caldacs[] = {
4312 [mb88341] = {12, 8, pack_mb88341},
4313 [dac8800] = {8, 8, pack_dac8800},
4314 [dac8043] = {1, 12, pack_dac8043},
4315 [ad8522] = {2, 12, pack_ad8522},
4316 [ad8804] = {12, 8, pack_ad8804},
4317 [ad8842] = {8, 8, pack_ad8842},
4318 [ad8804_debug] = {16, 8, pack_ad8804},
4321 static void ni_write_caldac(struct comedi_device *dev, int addr, int val)
4323 const struct ni_board_struct *board = dev->board_ptr;
4324 struct ni_private *devpriv = dev->private;
4325 unsigned int loadbit = 0, bits = 0, bit, bitstring = 0;
4330 if (devpriv->caldacs[addr] == val)
4332 devpriv->caldacs[addr] = val;
4334 for (i = 0; i < 3; i++) {
4335 type = board->caldac[i];
4336 if (type == caldac_none)
4338 if (addr < caldacs[type].n_chans) {
4339 bits = caldacs[type].packbits(addr, val, &bitstring);
4340 loadbit = NI_E_SERIAL_CMD_DAC_LD(i);
4343 addr -= caldacs[type].n_chans;
4346 /* bits will be 0 if there is no caldac for the given addr */
4350 for (bit = 1 << (bits - 1); bit; bit >>= 1) {
4351 cmd = (bit & bitstring) ? NI_E_SERIAL_CMD_SDATA : 0;
4352 ni_writeb(dev, cmd, NI_E_SERIAL_CMD_REG);
4354 ni_writeb(dev, NI_E_SERIAL_CMD_SCLK | cmd, NI_E_SERIAL_CMD_REG);
4357 ni_writeb(dev, loadbit, NI_E_SERIAL_CMD_REG);
4359 ni_writeb(dev, 0, NI_E_SERIAL_CMD_REG);
4362 static int ni_calib_insn_write(struct comedi_device *dev,
4363 struct comedi_subdevice *s,
4364 struct comedi_insn *insn,
4367 ni_write_caldac(dev, CR_CHAN(insn->chanspec), data[0]);
4372 static int ni_calib_insn_read(struct comedi_device *dev,
4373 struct comedi_subdevice *s,
4374 struct comedi_insn *insn,
4377 struct ni_private *devpriv = dev->private;
4379 data[0] = devpriv->caldacs[CR_CHAN(insn->chanspec)];
4384 static void caldac_setup(struct comedi_device *dev, struct comedi_subdevice *s)
4386 const struct ni_board_struct *board = dev->board_ptr;
4387 struct ni_private *devpriv = dev->private;
4396 type = board->caldac[0];
4397 if (type == caldac_none)
4399 n_bits = caldacs[type].n_bits;
4400 for (i = 0; i < 3; i++) {
4401 type = board->caldac[i];
4402 if (type == caldac_none)
4404 if (caldacs[type].n_bits != n_bits)
4406 n_chans += caldacs[type].n_chans;
4409 s->n_chan = n_chans;
4412 unsigned int *maxdata_list = devpriv->caldac_maxdata_list;
4414 if (n_chans > MAX_N_CALDACS)
4415 dev_err(dev->class_dev,
4416 "BUG! MAX_N_CALDACS too small\n");
4417 s->maxdata_list = maxdata_list;
4419 for (i = 0; i < n_dacs; i++) {
4420 type = board->caldac[i];
4421 for (j = 0; j < caldacs[type].n_chans; j++) {
4422 maxdata_list[chan] =
4423 (1 << caldacs[type].n_bits) - 1;
4428 for (chan = 0; chan < s->n_chan; chan++)
4429 ni_write_caldac(dev, i, s->maxdata_list[i] / 2);
4431 type = board->caldac[0];
4432 s->maxdata = (1 << caldacs[type].n_bits) - 1;
4434 for (chan = 0; chan < s->n_chan; chan++)
4435 ni_write_caldac(dev, i, s->maxdata / 2);
4439 static int ni_read_eeprom(struct comedi_device *dev, int addr)
4441 unsigned int cmd = NI_E_SERIAL_CMD_EEPROM_CS;
4445 bitstring = 0x0300 | ((addr & 0x100) << 3) | (addr & 0xff);
4446 ni_writeb(dev, cmd, NI_E_SERIAL_CMD_REG);
4447 for (bit = 0x8000; bit; bit >>= 1) {
4448 if (bit & bitstring)
4449 cmd |= NI_E_SERIAL_CMD_SDATA;
4451 cmd &= ~NI_E_SERIAL_CMD_SDATA;
4453 ni_writeb(dev, cmd, NI_E_SERIAL_CMD_REG);
4454 ni_writeb(dev, NI_E_SERIAL_CMD_SCLK | cmd, NI_E_SERIAL_CMD_REG);
4456 cmd = NI_E_SERIAL_CMD_EEPROM_CS;
4458 for (bit = 0x80; bit; bit >>= 1) {
4459 ni_writeb(dev, cmd, NI_E_SERIAL_CMD_REG);
4460 ni_writeb(dev, NI_E_SERIAL_CMD_SCLK | cmd, NI_E_SERIAL_CMD_REG);
4461 if (ni_readb(dev, NI_E_STATUS_REG) & NI_E_STATUS_PROMOUT)
4464 ni_writeb(dev, 0, NI_E_SERIAL_CMD_REG);
4469 static int ni_eeprom_insn_read(struct comedi_device *dev,
4470 struct comedi_subdevice *s,
4471 struct comedi_insn *insn,
4474 data[0] = ni_read_eeprom(dev, CR_CHAN(insn->chanspec));
4479 static int ni_m_series_eeprom_insn_read(struct comedi_device *dev,
4480 struct comedi_subdevice *s,
4481 struct comedi_insn *insn,
4484 struct ni_private *devpriv = dev->private;
4486 data[0] = devpriv->eeprom_buffer[CR_CHAN(insn->chanspec)];
4491 static unsigned int ni_old_get_pfi_routing(struct comedi_device *dev,
4494 /* pre-m-series boards have fixed signals on pfi pins */
4497 return NI_PFI_OUTPUT_AI_START1;
4499 return NI_PFI_OUTPUT_AI_START2;
4501 return NI_PFI_OUTPUT_AI_CONVERT;
4503 return NI_PFI_OUTPUT_G_SRC1;
4505 return NI_PFI_OUTPUT_G_GATE1;
4507 return NI_PFI_OUTPUT_AO_UPDATE_N;
4509 return NI_PFI_OUTPUT_AO_START1;
4511 return NI_PFI_OUTPUT_AI_START_PULSE;
4513 return NI_PFI_OUTPUT_G_SRC0;
4515 return NI_PFI_OUTPUT_G_GATE0;
4517 dev_err(dev->class_dev, "bug, unhandled case in switch.\n");
4523 static int ni_old_set_pfi_routing(struct comedi_device *dev,
4524 unsigned int chan, unsigned int source)
4526 /* pre-m-series boards have fixed signals on pfi pins */
4527 if (source != ni_old_get_pfi_routing(dev, chan))
4532 static unsigned int ni_m_series_get_pfi_routing(struct comedi_device *dev,
4535 struct ni_private *devpriv = dev->private;
4536 const unsigned int array_offset = chan / 3;
4538 return NI_M_PFI_OUT_SEL_TO_SRC(chan,
4539 devpriv->pfi_output_select_reg[array_offset]);
4542 static int ni_m_series_set_pfi_routing(struct comedi_device *dev,
4543 unsigned int chan, unsigned int source)
4545 struct ni_private *devpriv = dev->private;
4546 unsigned int index = chan / 3;
4547 unsigned short val = devpriv->pfi_output_select_reg[index];
4549 if ((source & 0x1f) != source)
4552 val &= ~NI_M_PFI_OUT_SEL_MASK(chan);
4553 val |= NI_M_PFI_OUT_SEL(chan, source);
4554 ni_writew(dev, val, NI_M_PFI_OUT_SEL_REG(index));
4555 devpriv->pfi_output_select_reg[index] = val;
4560 static unsigned int ni_get_pfi_routing(struct comedi_device *dev,
4563 struct ni_private *devpriv = dev->private;
4565 return (devpriv->is_m_series)
4566 ? ni_m_series_get_pfi_routing(dev, chan)
4567 : ni_old_get_pfi_routing(dev, chan);
4570 static int ni_set_pfi_routing(struct comedi_device *dev,
4571 unsigned int chan, unsigned int source)
4573 struct ni_private *devpriv = dev->private;
4575 return (devpriv->is_m_series)
4576 ? ni_m_series_set_pfi_routing(dev, chan, source)
4577 : ni_old_set_pfi_routing(dev, chan, source);
4580 static int ni_config_filter(struct comedi_device *dev,
4581 unsigned int pfi_channel,
4582 enum ni_pfi_filter_select filter)
4584 struct ni_private *devpriv = dev->private;
4587 if (!devpriv->is_m_series)
4590 bits = ni_readl(dev, NI_M_PFI_FILTER_REG);
4591 bits &= ~NI_M_PFI_FILTER_SEL_MASK(pfi_channel);
4592 bits |= NI_M_PFI_FILTER_SEL(pfi_channel, filter);
4593 ni_writel(dev, bits, NI_M_PFI_FILTER_REG);
4597 static int ni_pfi_insn_config(struct comedi_device *dev,
4598 struct comedi_subdevice *s,
4599 struct comedi_insn *insn,
4602 struct ni_private *devpriv = dev->private;
4608 chan = CR_CHAN(insn->chanspec);
4612 ni_set_bits(dev, NISTC_IO_BIDIR_PIN_REG, 1 << chan, 1);
4615 ni_set_bits(dev, NISTC_IO_BIDIR_PIN_REG, 1 << chan, 0);
4617 case INSN_CONFIG_DIO_QUERY:
4619 (devpriv->io_bidirection_pin_reg & (1 << chan)) ?
4620 COMEDI_OUTPUT : COMEDI_INPUT;
4622 case INSN_CONFIG_SET_ROUTING:
4623 return ni_set_pfi_routing(dev, chan, data[1]);
4624 case INSN_CONFIG_GET_ROUTING:
4625 data[1] = ni_get_pfi_routing(dev, chan);
4627 case INSN_CONFIG_FILTER:
4628 return ni_config_filter(dev, chan, data[1]);
4635 static int ni_pfi_insn_bits(struct comedi_device *dev,
4636 struct comedi_subdevice *s,
4637 struct comedi_insn *insn,
4640 struct ni_private *devpriv = dev->private;
4642 if (!devpriv->is_m_series)
4645 if (comedi_dio_update_state(s, data))
4646 ni_writew(dev, s->state, NI_M_PFI_DO_REG);
4648 data[1] = ni_readw(dev, NI_M_PFI_DI_REG);
4653 static int cs5529_wait_for_idle(struct comedi_device *dev)
4655 unsigned short status;
4656 const int timeout = HZ;
4659 for (i = 0; i < timeout; i++) {
4660 status = ni_ao_win_inw(dev, NI67XX_CAL_STATUS_REG);
4661 if ((status & NI67XX_CAL_STATUS_BUSY) == 0)
4663 set_current_state(TASK_INTERRUPTIBLE);
4664 if (schedule_timeout(1))
4668 dev_err(dev->class_dev, "timeout\n");
4674 static void cs5529_command(struct comedi_device *dev, unsigned short value)
4676 static const int timeout = 100;
4679 ni_ao_win_outw(dev, value, NI67XX_CAL_CMD_REG);
4680 /* give time for command to start being serially clocked into cs5529.
4681 * this insures that the NI67XX_CAL_STATUS_BUSY bit will get properly
4682 * set before we exit this function.
4684 for (i = 0; i < timeout; i++) {
4685 if (ni_ao_win_inw(dev, NI67XX_CAL_STATUS_REG) &
4686 NI67XX_CAL_STATUS_BUSY)
4691 dev_err(dev->class_dev,
4692 "possible problem - never saw adc go busy?\n");
4695 static int cs5529_do_conversion(struct comedi_device *dev,
4696 unsigned short *data)
4699 unsigned short status;
4701 cs5529_command(dev, CS5529_CMD_CB | CS5529_CMD_SINGLE_CONV);
4702 retval = cs5529_wait_for_idle(dev);
4704 dev_err(dev->class_dev,
4705 "timeout or signal in cs5529_do_conversion()\n");
4708 status = ni_ao_win_inw(dev, NI67XX_CAL_STATUS_REG);
4709 if (status & NI67XX_CAL_STATUS_OSC_DETECT) {
4710 dev_err(dev->class_dev,
4711 "cs5529 conversion error, status CSS_OSC_DETECT\n");
4714 if (status & NI67XX_CAL_STATUS_OVERRANGE) {
4715 dev_err(dev->class_dev,
4716 "cs5529 conversion error, overrange (ignoring)\n");
4719 *data = ni_ao_win_inw(dev, NI67XX_CAL_DATA_REG);
4720 /* cs5529 returns 16 bit signed data in bipolar mode */
4726 static int cs5529_ai_insn_read(struct comedi_device *dev,
4727 struct comedi_subdevice *s,
4728 struct comedi_insn *insn,
4732 unsigned short sample;
4733 unsigned int channel_select;
4734 const unsigned int INTERNAL_REF = 0x1000;
4737 * Set calibration adc source. Docs lie, reference select bits 8 to 11
4738 * do nothing. bit 12 seems to chooses internal reference voltage, bit
4739 * 13 causes the adc input to go overrange (maybe reads external
4742 if (insn->chanspec & CR_ALT_SOURCE)
4743 channel_select = INTERNAL_REF;
4745 channel_select = CR_CHAN(insn->chanspec);
4746 ni_ao_win_outw(dev, channel_select, NI67XX_AO_CAL_CHAN_SEL_REG);
4748 for (n = 0; n < insn->n; n++) {
4749 retval = cs5529_do_conversion(dev, &sample);
4757 static void cs5529_config_write(struct comedi_device *dev, unsigned int value,
4758 unsigned int reg_select_bits)
4760 ni_ao_win_outw(dev, (value >> 16) & 0xff, NI67XX_CAL_CFG_HI_REG);
4761 ni_ao_win_outw(dev, value & 0xffff, NI67XX_CAL_CFG_LO_REG);
4762 reg_select_bits &= CS5529_CMD_REG_MASK;
4763 cs5529_command(dev, CS5529_CMD_CB | reg_select_bits);
4764 if (cs5529_wait_for_idle(dev))
4765 dev_err(dev->class_dev,
4766 "timeout or signal in %s\n", __func__);
4769 static int init_cs5529(struct comedi_device *dev)
4771 unsigned int config_bits = CS5529_CFG_PORT_FLAG |
4772 CS5529_CFG_WORD_RATE_2180;
4775 /* do self-calibration */
4776 cs5529_config_write(dev, config_bits | CS5529_CFG_CALIB_BOTH_SELF,
4778 /* need to force a conversion for calibration to run */
4779 cs5529_do_conversion(dev, NULL);
4781 /* force gain calibration to 1 */
4782 cs5529_config_write(dev, 0x400000, CS5529_GAIN_REG);
4783 cs5529_config_write(dev, config_bits | CS5529_CFG_CALIB_OFFSET_SELF,
4785 if (cs5529_wait_for_idle(dev))
4786 dev_err(dev->class_dev,
4787 "timeout or signal in %s\n", __func__);
4793 * Find best multiplier/divider to try and get the PLL running at 80 MHz
4794 * given an arbitrary frequency input clock.
4796 static int ni_mseries_get_pll_parameters(unsigned int reference_period_ns,
4797 unsigned int *freq_divider,
4798 unsigned int *freq_multiplier,
4799 unsigned int *actual_period_ns)
4802 unsigned int best_div = 1;
4804 unsigned int best_mult = 1;
4805 static const unsigned int pico_per_nano = 1000;
4806 const unsigned int reference_picosec = reference_period_ns *
4809 * m-series wants the phased-locked loop to output 80MHz, which is
4810 * divided by 4 to 20 MHz for most timing clocks
4812 static const unsigned int target_picosec = 12500;
4813 int best_period_picosec = 0;
4815 for (div = 1; div <= NI_M_PLL_MAX_DIVISOR; ++div) {
4816 for (mult = 1; mult <= NI_M_PLL_MAX_MULTIPLIER; ++mult) {
4817 unsigned int new_period_ps =
4818 (reference_picosec * div) / mult;
4819 if (abs(new_period_ps - target_picosec) <
4820 abs(best_period_picosec - target_picosec)) {
4821 best_period_picosec = new_period_ps;
4827 if (best_period_picosec == 0)
4830 *freq_divider = best_div;
4831 *freq_multiplier = best_mult;
4832 /* return the actual period (* fudge factor for 80 to 20 MHz) */
4833 *actual_period_ns = DIV_ROUND_CLOSEST(best_period_picosec * 4,
4838 static int ni_mseries_set_pll_master_clock(struct comedi_device *dev,
4839 unsigned int source,
4840 unsigned int period_ns)
4842 struct ni_private *devpriv = dev->private;
4843 static const unsigned int min_period_ns = 50;
4844 static const unsigned int max_period_ns = 1000;
4845 static const unsigned int timeout = 1000;
4846 unsigned int pll_control_bits;
4847 unsigned int freq_divider;
4848 unsigned int freq_multiplier;
4853 if (source == NI_MIO_PLL_PXI10_CLOCK)
4856 * These limits are somewhat arbitrary, but NI advertises 1 to 20MHz
4857 * range so we'll use that.
4859 if (period_ns < min_period_ns || period_ns > max_period_ns) {
4860 dev_err(dev->class_dev,
4861 "%s: you must specify an input clock frequency between %i and %i nanosec for the phased-lock loop\n",
4862 __func__, min_period_ns, max_period_ns);
4865 devpriv->rtsi_trig_direction_reg &= ~NISTC_RTSI_TRIG_USE_CLK;
4866 ni_stc_writew(dev, devpriv->rtsi_trig_direction_reg,
4867 NISTC_RTSI_TRIG_DIR_REG);
4868 pll_control_bits = NI_M_PLL_CTRL_ENA | NI_M_PLL_CTRL_VCO_MODE_75_150MHZ;
4869 devpriv->clock_and_fout2 |= NI_M_CLK_FOUT2_TIMEBASE1_PLL |
4870 NI_M_CLK_FOUT2_TIMEBASE3_PLL;
4871 devpriv->clock_and_fout2 &= ~NI_M_CLK_FOUT2_PLL_SRC_MASK;
4873 case NI_MIO_PLL_PXI_STAR_TRIGGER_CLOCK:
4874 devpriv->clock_and_fout2 |= NI_M_CLK_FOUT2_PLL_SRC_STAR;
4876 case NI_MIO_PLL_PXI10_CLOCK:
4877 /* pxi clock is 10MHz */
4878 devpriv->clock_and_fout2 |= NI_M_CLK_FOUT2_PLL_SRC_PXI10;
4881 for (rtsi = 0; rtsi <= NI_M_MAX_RTSI_CHAN; ++rtsi) {
4882 if (source == NI_MIO_PLL_RTSI_CLOCK(rtsi)) {
4883 devpriv->clock_and_fout2 |=
4884 NI_M_CLK_FOUT2_PLL_SRC_RTSI(rtsi);
4888 if (rtsi > NI_M_MAX_RTSI_CHAN)
4892 retval = ni_mseries_get_pll_parameters(period_ns,
4895 &devpriv->clock_ns);
4897 dev_err(dev->class_dev,
4898 "bug, failed to find pll parameters\n");
4902 ni_writew(dev, devpriv->clock_and_fout2, NI_M_CLK_FOUT2_REG);
4903 pll_control_bits |= NI_M_PLL_CTRL_DIVISOR(freq_divider) |
4904 NI_M_PLL_CTRL_MULTIPLIER(freq_multiplier);
4906 ni_writew(dev, pll_control_bits, NI_M_PLL_CTRL_REG);
4907 devpriv->clock_source = source;
4908 /* it takes a few hundred microseconds for PLL to lock */
4909 for (i = 0; i < timeout; ++i) {
4910 if (ni_readw(dev, NI_M_PLL_STATUS_REG) & NI_M_PLL_STATUS_LOCKED)
4915 dev_err(dev->class_dev,
4916 "%s: timed out waiting for PLL to lock to reference clock source %i with period %i ns\n",
4917 __func__, source, period_ns);
4923 static int ni_set_master_clock(struct comedi_device *dev,
4924 unsigned int source, unsigned int period_ns)
4926 struct ni_private *devpriv = dev->private;
4928 if (source == NI_MIO_INTERNAL_CLOCK) {
4929 devpriv->rtsi_trig_direction_reg &= ~NISTC_RTSI_TRIG_USE_CLK;
4930 ni_stc_writew(dev, devpriv->rtsi_trig_direction_reg,
4931 NISTC_RTSI_TRIG_DIR_REG);
4932 devpriv->clock_ns = TIMEBASE_1_NS;
4933 if (devpriv->is_m_series) {
4934 devpriv->clock_and_fout2 &=
4935 ~(NI_M_CLK_FOUT2_TIMEBASE1_PLL |
4936 NI_M_CLK_FOUT2_TIMEBASE3_PLL);
4937 ni_writew(dev, devpriv->clock_and_fout2,
4938 NI_M_CLK_FOUT2_REG);
4939 ni_writew(dev, 0, NI_M_PLL_CTRL_REG);
4941 devpriv->clock_source = source;
4943 if (devpriv->is_m_series) {
4944 return ni_mseries_set_pll_master_clock(dev, source,
4947 if (source == NI_MIO_RTSI_CLOCK) {
4948 devpriv->rtsi_trig_direction_reg |=
4949 NISTC_RTSI_TRIG_USE_CLK;
4951 devpriv->rtsi_trig_direction_reg,
4952 NISTC_RTSI_TRIG_DIR_REG);
4953 if (period_ns == 0) {
4954 dev_err(dev->class_dev,
4955 "we don't handle an unspecified clock period correctly yet, returning error\n");
4958 devpriv->clock_ns = period_ns;
4959 devpriv->clock_source = source;
4968 static int ni_valid_rtsi_output_source(struct comedi_device *dev,
4969 unsigned int chan, unsigned int source)
4971 struct ni_private *devpriv = dev->private;
4973 if (chan >= NISTC_RTSI_TRIG_NUM_CHAN(devpriv->is_m_series)) {
4974 if (chan == NISTC_RTSI_TRIG_OLD_CLK_CHAN) {
4975 if (source == NI_RTSI_OUTPUT_RTSI_OSC)
4978 dev_err(dev->class_dev,
4979 "%s: invalid source for channel=%i, channel %i is always the RTSI clock for pre-m-series boards\n",
4980 __func__, chan, NISTC_RTSI_TRIG_OLD_CLK_CHAN);
4986 case NI_RTSI_OUTPUT_ADR_START1:
4987 case NI_RTSI_OUTPUT_ADR_START2:
4988 case NI_RTSI_OUTPUT_SCLKG:
4989 case NI_RTSI_OUTPUT_DACUPDN:
4990 case NI_RTSI_OUTPUT_DA_START1:
4991 case NI_RTSI_OUTPUT_G_SRC0:
4992 case NI_RTSI_OUTPUT_G_GATE0:
4993 case NI_RTSI_OUTPUT_RGOUT0:
4994 case NI_RTSI_OUTPUT_RTSI_BRD(0):
4995 case NI_RTSI_OUTPUT_RTSI_BRD(1):
4996 case NI_RTSI_OUTPUT_RTSI_BRD(2):
4997 case NI_RTSI_OUTPUT_RTSI_BRD(3):
4999 case NI_RTSI_OUTPUT_RTSI_OSC:
5000 return (devpriv->is_m_series) ? 1 : 0;
5006 static int ni_set_rtsi_routing(struct comedi_device *dev,
5007 unsigned int chan, unsigned int src)
5009 struct ni_private *devpriv = dev->private;
5011 if (ni_valid_rtsi_output_source(dev, chan, src) == 0)
5014 devpriv->rtsi_trig_a_output_reg &= ~NISTC_RTSI_TRIG_MASK(chan);
5015 devpriv->rtsi_trig_a_output_reg |= NISTC_RTSI_TRIG(chan, src);
5016 ni_stc_writew(dev, devpriv->rtsi_trig_a_output_reg,
5017 NISTC_RTSI_TRIGA_OUT_REG);
5018 } else if (chan < NISTC_RTSI_TRIG_NUM_CHAN(devpriv->is_m_series)) {
5019 devpriv->rtsi_trig_b_output_reg &= ~NISTC_RTSI_TRIG_MASK(chan);
5020 devpriv->rtsi_trig_b_output_reg |= NISTC_RTSI_TRIG(chan, src);
5021 ni_stc_writew(dev, devpriv->rtsi_trig_b_output_reg,
5022 NISTC_RTSI_TRIGB_OUT_REG);
5023 } else if (chan != NISTC_RTSI_TRIG_OLD_CLK_CHAN) {
5024 /* probably should never reach this, since the
5025 * ni_valid_rtsi_output_source above errors out if chan is too
5028 dev_err(dev->class_dev, "%s: unknown rtsi channel\n", __func__);
5034 static unsigned int ni_get_rtsi_routing(struct comedi_device *dev,
5037 struct ni_private *devpriv = dev->private;
5040 return NISTC_RTSI_TRIG_TO_SRC(chan,
5041 devpriv->rtsi_trig_a_output_reg);
5042 } else if (chan < NISTC_RTSI_TRIG_NUM_CHAN(devpriv->is_m_series)) {
5043 return NISTC_RTSI_TRIG_TO_SRC(chan,
5044 devpriv->rtsi_trig_b_output_reg);
5045 } else if (chan == NISTC_RTSI_TRIG_OLD_CLK_CHAN) {
5046 return NI_RTSI_OUTPUT_RTSI_OSC;
5049 dev_err(dev->class_dev, "%s: unknown rtsi channel\n", __func__);
5053 static int ni_rtsi_insn_config(struct comedi_device *dev,
5054 struct comedi_subdevice *s,
5055 struct comedi_insn *insn,
5058 struct ni_private *devpriv = dev->private;
5059 unsigned int chan = CR_CHAN(insn->chanspec);
5060 unsigned int max_chan = NISTC_RTSI_TRIG_NUM_CHAN(devpriv->is_m_series);
5063 case INSN_CONFIG_DIO_OUTPUT:
5064 if (chan < max_chan) {
5065 devpriv->rtsi_trig_direction_reg |=
5066 NISTC_RTSI_TRIG_DIR(chan, devpriv->is_m_series);
5067 } else if (chan == NISTC_RTSI_TRIG_OLD_CLK_CHAN) {
5068 devpriv->rtsi_trig_direction_reg |=
5069 NISTC_RTSI_TRIG_DRV_CLK;
5071 ni_stc_writew(dev, devpriv->rtsi_trig_direction_reg,
5072 NISTC_RTSI_TRIG_DIR_REG);
5074 case INSN_CONFIG_DIO_INPUT:
5075 if (chan < max_chan) {
5076 devpriv->rtsi_trig_direction_reg &=
5077 ~NISTC_RTSI_TRIG_DIR(chan, devpriv->is_m_series);
5078 } else if (chan == NISTC_RTSI_TRIG_OLD_CLK_CHAN) {
5079 devpriv->rtsi_trig_direction_reg &=
5080 ~NISTC_RTSI_TRIG_DRV_CLK;
5082 ni_stc_writew(dev, devpriv->rtsi_trig_direction_reg,
5083 NISTC_RTSI_TRIG_DIR_REG);
5085 case INSN_CONFIG_DIO_QUERY:
5086 if (chan < max_chan) {
5088 (devpriv->rtsi_trig_direction_reg &
5089 NISTC_RTSI_TRIG_DIR(chan, devpriv->is_m_series))
5090 ? INSN_CONFIG_DIO_OUTPUT
5091 : INSN_CONFIG_DIO_INPUT;
5092 } else if (chan == NISTC_RTSI_TRIG_OLD_CLK_CHAN) {
5093 data[1] = (devpriv->rtsi_trig_direction_reg &
5094 NISTC_RTSI_TRIG_DRV_CLK)
5095 ? INSN_CONFIG_DIO_OUTPUT
5096 : INSN_CONFIG_DIO_INPUT;
5099 case INSN_CONFIG_SET_CLOCK_SRC:
5100 return ni_set_master_clock(dev, data[1], data[2]);
5101 case INSN_CONFIG_GET_CLOCK_SRC:
5102 data[1] = devpriv->clock_source;
5103 data[2] = devpriv->clock_ns;
5105 case INSN_CONFIG_SET_ROUTING:
5106 return ni_set_rtsi_routing(dev, chan, data[1]);
5107 case INSN_CONFIG_GET_ROUTING:
5108 data[1] = ni_get_rtsi_routing(dev, chan);
5116 static int ni_rtsi_insn_bits(struct comedi_device *dev,
5117 struct comedi_subdevice *s,
5118 struct comedi_insn *insn,
5126 static void ni_rtsi_init(struct comedi_device *dev)
5128 struct ni_private *devpriv = dev->private;
5130 /* Initialises the RTSI bus signal switch to a default state */
5133 * Use 10MHz instead of 20MHz for RTSI clock frequency. Appears
5134 * to have no effect, at least on pxi-6281, which always uses
5135 * 20MHz rtsi clock frequency
5137 devpriv->clock_and_fout2 = NI_M_CLK_FOUT2_RTSI_10MHZ;
5138 /* Set clock mode to internal */
5139 if (ni_set_master_clock(dev, NI_MIO_INTERNAL_CLOCK, 0) < 0)
5140 dev_err(dev->class_dev, "ni_set_master_clock failed, bug?\n");
5141 /* default internal lines routing to RTSI bus lines */
5142 devpriv->rtsi_trig_a_output_reg =
5143 NISTC_RTSI_TRIG(0, NI_RTSI_OUTPUT_ADR_START1) |
5144 NISTC_RTSI_TRIG(1, NI_RTSI_OUTPUT_ADR_START2) |
5145 NISTC_RTSI_TRIG(2, NI_RTSI_OUTPUT_SCLKG) |
5146 NISTC_RTSI_TRIG(3, NI_RTSI_OUTPUT_DACUPDN);
5147 ni_stc_writew(dev, devpriv->rtsi_trig_a_output_reg,
5148 NISTC_RTSI_TRIGA_OUT_REG);
5149 devpriv->rtsi_trig_b_output_reg =
5150 NISTC_RTSI_TRIG(4, NI_RTSI_OUTPUT_DA_START1) |
5151 NISTC_RTSI_TRIG(5, NI_RTSI_OUTPUT_G_SRC0) |
5152 NISTC_RTSI_TRIG(6, NI_RTSI_OUTPUT_G_GATE0);
5153 if (devpriv->is_m_series)
5154 devpriv->rtsi_trig_b_output_reg |=
5155 NISTC_RTSI_TRIG(7, NI_RTSI_OUTPUT_RTSI_OSC);
5156 ni_stc_writew(dev, devpriv->rtsi_trig_b_output_reg,
5157 NISTC_RTSI_TRIGB_OUT_REG);
5160 * Sets the source and direction of the 4 on board lines
5161 * ni_stc_writew(dev, 0, NISTC_RTSI_BOARD_REG);
5166 static int ni_gpct_cmd(struct comedi_device *dev, struct comedi_subdevice *s)
5168 struct ni_gpct *counter = s->private;
5171 retval = ni_request_gpct_mite_channel(dev, counter->counter_index,
5174 dev_err(dev->class_dev,
5175 "no dma channel available for use by counter\n");
5178 ni_tio_acknowledge(counter);
5179 ni_e_series_enable_second_irq(dev, counter->counter_index, 1);
5181 return ni_tio_cmd(dev, s);
5184 static int ni_gpct_cancel(struct comedi_device *dev, struct comedi_subdevice *s)
5186 struct ni_gpct *counter = s->private;
5189 retval = ni_tio_cancel(counter);
5190 ni_e_series_enable_second_irq(dev, counter->counter_index, 0);
5191 ni_release_gpct_mite_channel(dev, counter->counter_index);
5196 static irqreturn_t ni_E_interrupt(int irq, void *d)
5198 struct comedi_device *dev = d;
5199 struct comedi_subdevice *s_ai = dev->read_subdev;
5200 struct comedi_subdevice *s_ao = dev->write_subdev;
5201 unsigned short a_status;
5202 unsigned short b_status;
5203 unsigned long flags;
5205 struct ni_private *devpriv = dev->private;
5210 smp_mb(); /* make sure dev->attached is checked */
5212 /* lock to avoid race with comedi_poll */
5213 spin_lock_irqsave(&dev->spinlock, flags);
5214 a_status = ni_stc_readw(dev, NISTC_AI_STATUS1_REG);
5215 b_status = ni_stc_readw(dev, NISTC_AO_STATUS1_REG);
5217 if (devpriv->mite) {
5218 unsigned long flags_too;
5220 spin_lock_irqsave(&devpriv->mite_channel_lock, flags_too);
5221 if (s_ai && devpriv->ai_mite_chan)
5222 mite_ack_linkc(devpriv->ai_mite_chan, s_ai, false);
5223 if (s_ao && devpriv->ao_mite_chan)
5224 mite_ack_linkc(devpriv->ao_mite_chan, s_ao, false);
5225 spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags_too);
5228 ack_a_interrupt(dev, a_status);
5229 ack_b_interrupt(dev, b_status);
5231 if (a_status & NISTC_AI_STATUS1_INTA)
5232 handle_a_interrupt(dev, s_ai, a_status);
5233 /* handle any interrupt or dma events */
5234 comedi_handle_events(dev, s_ai);
5237 if (b_status & NISTC_AO_STATUS1_INTB)
5238 handle_b_interrupt(dev, s_ao, b_status);
5239 /* handle any interrupt or dma events */
5240 comedi_handle_events(dev, s_ao);
5242 handle_gpct_interrupt(dev, 0);
5243 handle_gpct_interrupt(dev, 1);
5245 if (devpriv->is_m_series)
5246 handle_cdio_interrupt(dev);
5249 spin_unlock_irqrestore(&dev->spinlock, flags);
5253 static int ni_alloc_private(struct comedi_device *dev)
5255 struct ni_private *devpriv;
5257 devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
5261 spin_lock_init(&devpriv->window_lock);
5262 spin_lock_init(&devpriv->soft_reg_copy_lock);
5263 spin_lock_init(&devpriv->mite_channel_lock);
5268 static int ni_E_init(struct comedi_device *dev,
5269 unsigned int interrupt_pin, unsigned int irq_polarity)
5271 const struct ni_board_struct *board = dev->board_ptr;
5272 struct ni_private *devpriv = dev->private;
5273 struct comedi_subdevice *s;
5277 if (board->n_aochan > MAX_N_AO_CHAN) {
5278 dev_err(dev->class_dev, "bug! n_aochan > MAX_N_AO_CHAN\n");
5282 /* initialize clock dividers */
5283 devpriv->clock_and_fout = NISTC_CLK_FOUT_SLOW_DIV2 |
5284 NISTC_CLK_FOUT_SLOW_TIMEBASE |
5285 NISTC_CLK_FOUT_TO_BOARD_DIV2 |
5286 NISTC_CLK_FOUT_TO_BOARD;
5287 if (!devpriv->is_6xxx) {
5288 /* BEAM is this needed for PCI-6143 ?? */
5289 devpriv->clock_and_fout |= (NISTC_CLK_FOUT_AI_OUT_DIV2 |
5290 NISTC_CLK_FOUT_AO_OUT_DIV2);
5292 ni_stc_writew(dev, devpriv->clock_and_fout, NISTC_CLK_FOUT_REG);
5294 ret = comedi_alloc_subdevices(dev, NI_NUM_SUBDEVICES);
5298 /* Analog Input subdevice */
5299 s = &dev->subdevices[NI_AI_SUBDEV];
5300 if (board->n_adchan) {
5301 s->type = COMEDI_SUBD_AI;
5302 s->subdev_flags = SDF_READABLE | SDF_DIFF | SDF_DITHER;
5303 if (!devpriv->is_611x)
5304 s->subdev_flags |= SDF_GROUND | SDF_COMMON | SDF_OTHER;
5305 if (board->ai_maxdata > 0xffff)
5306 s->subdev_flags |= SDF_LSAMPL;
5307 if (devpriv->is_m_series)
5308 s->subdev_flags |= SDF_SOFT_CALIBRATED;
5309 s->n_chan = board->n_adchan;
5310 s->maxdata = board->ai_maxdata;
5311 s->range_table = ni_range_lkup[board->gainlkup];
5312 s->insn_read = ni_ai_insn_read;
5313 s->insn_config = ni_ai_insn_config;
5315 dev->read_subdev = s;
5316 s->subdev_flags |= SDF_CMD_READ;
5317 s->len_chanlist = 512;
5318 s->do_cmdtest = ni_ai_cmdtest;
5319 s->do_cmd = ni_ai_cmd;
5320 s->cancel = ni_ai_reset;
5321 s->poll = ni_ai_poll;
5322 s->munge = ni_ai_munge;
5325 s->async_dma_dir = DMA_FROM_DEVICE;
5328 /* reset the analog input configuration */
5329 ni_ai_reset(dev, s);
5331 s->type = COMEDI_SUBD_UNUSED;
5334 /* Analog Output subdevice */
5335 s = &dev->subdevices[NI_AO_SUBDEV];
5336 if (board->n_aochan) {
5337 s->type = COMEDI_SUBD_AO;
5338 s->subdev_flags = SDF_WRITABLE | SDF_DEGLITCH | SDF_GROUND;
5339 if (devpriv->is_m_series)
5340 s->subdev_flags |= SDF_SOFT_CALIBRATED;
5341 s->n_chan = board->n_aochan;
5342 s->maxdata = board->ao_maxdata;
5343 s->range_table = board->ao_range_table;
5344 s->insn_config = ni_ao_insn_config;
5345 s->insn_write = ni_ao_insn_write;
5347 ret = comedi_alloc_subdev_readback(s);
5352 * Along with the IRQ we need either a FIFO or DMA for
5353 * async command support.
5355 if (dev->irq && (board->ao_fifo_depth || devpriv->mite)) {
5356 dev->write_subdev = s;
5357 s->subdev_flags |= SDF_CMD_WRITE;
5358 s->len_chanlist = s->n_chan;
5359 s->do_cmdtest = ni_ao_cmdtest;
5360 s->do_cmd = ni_ao_cmd;
5361 s->cancel = ni_ao_reset;
5362 if (!devpriv->is_m_series)
5363 s->munge = ni_ao_munge;
5366 s->async_dma_dir = DMA_TO_DEVICE;
5369 if (devpriv->is_67xx)
5370 init_ao_67xx(dev, s);
5372 /* reset the analog output configuration */
5373 ni_ao_reset(dev, s);
5375 s->type = COMEDI_SUBD_UNUSED;
5378 /* Digital I/O subdevice */
5379 s = &dev->subdevices[NI_DIO_SUBDEV];
5380 s->type = COMEDI_SUBD_DIO;
5381 s->subdev_flags = SDF_WRITABLE | SDF_READABLE;
5382 s->n_chan = board->has_32dio_chan ? 32 : 8;
5384 s->range_table = &range_digital;
5385 if (devpriv->is_m_series) {
5387 s->subdev_flags |= SDF_LSAMPL;
5388 s->insn_bits = ni_m_series_dio_insn_bits;
5389 s->insn_config = ni_m_series_dio_insn_config;
5391 s->subdev_flags |= SDF_CMD_WRITE /* | SDF_CMD_READ */;
5392 s->len_chanlist = s->n_chan;
5393 s->do_cmdtest = ni_cdio_cmdtest;
5394 s->do_cmd = ni_cdio_cmd;
5395 s->cancel = ni_cdio_cancel;
5397 /* M-series boards use DMA */
5398 s->async_dma_dir = DMA_BIDIRECTIONAL;
5401 /* reset DIO and set all channels to inputs */
5402 ni_writel(dev, NI_M_CDO_CMD_RESET |
5405 ni_writel(dev, s->io_bits, NI_M_DIO_DIR_REG);
5408 s->insn_bits = ni_dio_insn_bits;
5409 s->insn_config = ni_dio_insn_config;
5411 /* set all channels to inputs */
5412 devpriv->dio_control = NISTC_DIO_CTRL_DIR(s->io_bits);
5413 ni_writew(dev, devpriv->dio_control, NISTC_DIO_CTRL_REG);
5417 s = &dev->subdevices[NI_8255_DIO_SUBDEV];
5418 if (board->has_8255) {
5419 ret = subdev_8255_init(dev, s, ni_8255_callback,
5424 s->type = COMEDI_SUBD_UNUSED;
5427 /* formerly general purpose counter/timer device, but no longer used */
5428 s = &dev->subdevices[NI_UNUSED_SUBDEV];
5429 s->type = COMEDI_SUBD_UNUSED;
5431 /* Calibration subdevice */
5432 s = &dev->subdevices[NI_CALIBRATION_SUBDEV];
5433 s->type = COMEDI_SUBD_CALIB;
5434 s->subdev_flags = SDF_INTERNAL;
5437 if (devpriv->is_m_series) {
5438 /* internal PWM output used for AI nonlinearity calibration */
5439 s->insn_config = ni_m_series_pwm_config;
5441 ni_writel(dev, 0x0, NI_M_CAL_PWM_REG);
5442 } else if (devpriv->is_6143) {
5443 /* internal PWM output used for AI nonlinearity calibration */
5444 s->insn_config = ni_6143_pwm_config;
5446 s->subdev_flags |= SDF_WRITABLE;
5447 s->insn_read = ni_calib_insn_read;
5448 s->insn_write = ni_calib_insn_write;
5450 /* setup the caldacs and find the real n_chan and maxdata */
5451 caldac_setup(dev, s);
5454 /* EEPROM subdevice */
5455 s = &dev->subdevices[NI_EEPROM_SUBDEV];
5456 s->type = COMEDI_SUBD_MEMORY;
5457 s->subdev_flags = SDF_READABLE | SDF_INTERNAL;
5459 if (devpriv->is_m_series) {
5460 s->n_chan = M_SERIES_EEPROM_SIZE;
5461 s->insn_read = ni_m_series_eeprom_insn_read;
5464 s->insn_read = ni_eeprom_insn_read;
5467 /* Digital I/O (PFI) subdevice */
5468 s = &dev->subdevices[NI_PFI_DIO_SUBDEV];
5469 s->type = COMEDI_SUBD_DIO;
5471 if (devpriv->is_m_series) {
5473 s->insn_bits = ni_pfi_insn_bits;
5474 s->subdev_flags = SDF_READABLE | SDF_WRITABLE | SDF_INTERNAL;
5476 ni_writew(dev, s->state, NI_M_PFI_DO_REG);
5477 for (i = 0; i < NUM_PFI_OUTPUT_SELECT_REGS; ++i) {
5478 ni_writew(dev, devpriv->pfi_output_select_reg[i],
5479 NI_M_PFI_OUT_SEL_REG(i));
5483 s->subdev_flags = SDF_INTERNAL;
5485 s->insn_config = ni_pfi_insn_config;
5487 ni_set_bits(dev, NISTC_IO_BIDIR_PIN_REG, ~0, 0);
5489 /* cs5529 calibration adc */
5490 s = &dev->subdevices[NI_CS5529_CALIBRATION_SUBDEV];
5491 if (devpriv->is_67xx) {
5492 s->type = COMEDI_SUBD_AI;
5493 s->subdev_flags = SDF_READABLE | SDF_DIFF | SDF_INTERNAL;
5494 /* one channel for each analog output channel */
5495 s->n_chan = board->n_aochan;
5496 s->maxdata = (1 << 16) - 1;
5497 s->range_table = &range_unknown; /* XXX */
5498 s->insn_read = cs5529_ai_insn_read;
5499 s->insn_config = NULL;
5502 s->type = COMEDI_SUBD_UNUSED;
5506 s = &dev->subdevices[NI_SERIAL_SUBDEV];
5507 s->type = COMEDI_SUBD_SERIAL;
5508 s->subdev_flags = SDF_READABLE | SDF_WRITABLE | SDF_INTERNAL;
5511 s->insn_config = ni_serial_insn_config;
5512 devpriv->serial_interval_ns = 0;
5513 devpriv->serial_hw_mode = 0;
5516 s = &dev->subdevices[NI_RTSI_SUBDEV];
5517 s->type = COMEDI_SUBD_DIO;
5518 s->subdev_flags = SDF_READABLE | SDF_WRITABLE | SDF_INTERNAL;
5521 s->insn_bits = ni_rtsi_insn_bits;
5522 s->insn_config = ni_rtsi_insn_config;
5525 /* allocate and initialize the gpct counter device */
5526 devpriv->counter_dev = ni_gpct_device_construct(dev,
5527 ni_gpct_write_register,
5528 ni_gpct_read_register,
5529 (devpriv->is_m_series)
5530 ? ni_gpct_variant_m_series
5531 : ni_gpct_variant_e_series,
5533 if (!devpriv->counter_dev)
5536 /* Counter (gpct) subdevices */
5537 for (i = 0; i < NUM_GPCT; ++i) {
5538 struct ni_gpct *gpct = &devpriv->counter_dev->counters[i];
5540 /* setup and initialize the counter */
5541 gpct->chip_index = 0;
5542 gpct->counter_index = i;
5543 ni_tio_init_counter(gpct);
5545 s = &dev->subdevices[NI_GPCT_SUBDEV(i)];
5546 s->type = COMEDI_SUBD_COUNTER;
5547 s->subdev_flags = SDF_READABLE | SDF_WRITABLE | SDF_LSAMPL;
5549 s->maxdata = (devpriv->is_m_series) ? 0xffffffff
5551 s->insn_read = ni_tio_insn_read;
5552 s->insn_write = ni_tio_insn_write;
5553 s->insn_config = ni_tio_insn_config;
5555 if (dev->irq && devpriv->mite) {
5556 s->subdev_flags |= SDF_CMD_READ /* | SDF_CMD_WRITE */;
5557 s->len_chanlist = 1;
5558 s->do_cmdtest = ni_tio_cmdtest;
5559 s->do_cmd = ni_gpct_cmd;
5560 s->cancel = ni_gpct_cancel;
5562 s->async_dma_dir = DMA_BIDIRECTIONAL;
5568 /* Frequency output subdevice */
5569 s = &dev->subdevices[NI_FREQ_OUT_SUBDEV];
5570 s->type = COMEDI_SUBD_COUNTER;
5571 s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
5574 s->insn_read = ni_freq_out_insn_read;
5575 s->insn_write = ni_freq_out_insn_write;
5576 s->insn_config = ni_freq_out_insn_config;
5580 (irq_polarity ? NISTC_INT_CTRL_INT_POL : 0) |
5581 (NISTC_INT_CTRL_3PIN_INT & 0) |
5582 NISTC_INT_CTRL_INTA_ENA |
5583 NISTC_INT_CTRL_INTB_ENA |
5584 NISTC_INT_CTRL_INTA_SEL(interrupt_pin) |
5585 NISTC_INT_CTRL_INTB_SEL(interrupt_pin),
5586 NISTC_INT_CTRL_REG);
5590 ni_writeb(dev, devpriv->ai_ao_select_reg, NI_E_DMA_AI_AO_SEL_REG);
5591 ni_writeb(dev, devpriv->g0_g1_select_reg, NI_E_DMA_G0_G1_SEL_REG);
5593 if (devpriv->is_6xxx) {
5594 ni_writeb(dev, 0, NI611X_MAGIC_REG);
5595 } else if (devpriv->is_m_series) {
5598 for (channel = 0; channel < board->n_aochan; ++channel) {
5600 NI_M_AO_WAVEFORM_ORDER_REG(channel));
5602 NI_M_AO_REF_ATTENUATION_REG(channel));
5604 ni_writeb(dev, 0x0, NI_M_AO_CALIB_REG);
5610 static void mio_common_detach(struct comedi_device *dev)
5612 struct ni_private *devpriv = dev->private;
5615 ni_gpct_device_destroy(devpriv->counter_dev);