2 comedi/drivers/ni_pcidio.c
3 driver for National Instruments PCI-DIO-32HS
5 COMEDI - Linux Control and Measurement Device Interface
6 Copyright (C) 1999,2002 David A. Schleef <ds@schleef.org>
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 Description: National Instruments PCI-DIO32HS, PCI-6533
23 Devices: [National Instruments] PCI-DIO-32HS (ni_pcidio)
24 [National Instruments] PXI-6533, PCI-6533 (pxi-6533)
25 [National Instruments] PCI-6534 (pci-6534)
26 Updated: Mon, 09 Jan 2012 14:27:23 +0000
28 The DIO32HS board appears as one subdevice, with 32 channels.
29 Each channel is individually I/O configurable. The channel order
30 is 0=A0, 1=A1, 2=A2, ... 8=B0, 16=C0, 24=D0. The driver only
31 supports simple digital I/O; no handshaking is supported.
33 DMA mostly works for the PCI-DIO32HS, but only in timed input mode.
35 The PCI-DIO-32HS/PCI-6533 has a configurable external trigger. Setting
36 scan_begin_arg to 0 or CR_EDGE triggers on the leading edge. Setting
37 scan_begin_arg to CR_INVERT or (CR_EDGE | CR_INVERT) triggers on the
40 This driver could be easily modified to support AT-MIO32HS and
43 The PCI-6534 requires a firmware upload after power-up to work, the
44 firmware data and instructions for loading it with comedi_config
45 it are contained in the
46 comedi_nonfree_firmware tarball available from http://www.comedi.org
51 #include <linux/module.h>
52 #include <linux/delay.h>
53 #include <linux/interrupt.h>
54 #include <linux/sched.h>
56 #include "../comedi_pci.h"
60 /* defines for the PCI-DIO-32HS */
62 #define Window_Address 4 /* W */
63 #define Interrupt_And_Window_Status 4 /* R */
64 #define IntStatus1 (1<<0)
65 #define IntStatus2 (1<<1)
66 #define WindowAddressStatus_mask 0x7c
68 #define Master_DMA_And_Interrupt_Control 5 /* W */
69 #define InterruptLine(x) ((x)&3)
70 #define OpenInt (1<<2)
71 #define Group_Status 5 /* R */
72 #define DataLeft (1<<0)
74 #define StopTrig (1<<3)
76 #define Group_1_Flags 6 /* R */
77 #define Group_2_Flags 7 /* R */
78 #define TransferReady (1<<0)
79 #define CountExpired (1<<1)
81 #define PrimaryTC (1<<6)
82 #define SecondaryTC (1<<7)
83 /* #define SerialRose */
87 #define Group_1_First_Clear 6 /* W */
88 #define Group_2_First_Clear 7 /* W */
89 #define ClearWaited (1<<3)
90 #define ClearPrimaryTC (1<<4)
91 #define ClearSecondaryTC (1<<5)
92 #define DMAReset (1<<6)
93 #define FIFOReset (1<<7)
96 #define Group_1_FIFO 8 /* W */
97 #define Group_2_FIFO 12 /* W */
99 #define Transfer_Count 20
103 #define Chip_Version 27
104 #define Port_IO(x) (28+(x))
105 #define Port_Pin_Directions(x) (32+(x))
106 #define Port_Pin_Mask(x) (36+(x))
107 #define Port_Pin_Polarities(x) (40+(x))
109 #define Master_Clock_Routing 45
110 #define RTSIClocking(x) (((x)&3)<<4)
112 #define Group_1_Second_Clear 46 /* W */
113 #define Group_2_Second_Clear 47 /* W */
114 #define ClearExpired (1<<0)
116 #define Port_Pattern(x) (48+(x))
119 #define FIFOEnableA (1<<0)
120 #define FIFOEnableB (1<<1)
121 #define FIFOEnableC (1<<2)
122 #define FIFOEnableD (1<<3)
123 #define Funneling(x) (((x)&3)<<4)
124 #define GroupDirection (1<<7)
126 #define Protocol_Register_1 65
127 #define OpMode Protocol_Register_1
128 #define RunMode(x) ((x)&7)
129 #define Numbered (1<<3)
131 #define Protocol_Register_2 66
132 #define ClockReg Protocol_Register_2
133 #define ClockLine(x) (((x)&3)<<5)
134 #define InvertStopTrig (1<<7)
135 #define DataLatching(x) (((x)&3)<<5)
137 #define Protocol_Register_3 67
138 #define Sequence Protocol_Register_3
140 #define Protocol_Register_14 68 /* 16 bit */
141 #define ClockSpeed Protocol_Register_14
143 #define Protocol_Register_4 70
144 #define ReqReg Protocol_Register_4
145 #define ReqConditioning(x) (((x)&7)<<3)
147 #define Protocol_Register_5 71
148 #define BlockMode Protocol_Register_5
150 #define FIFO_Control 72
151 #define ReadyLevel(x) ((x)&7)
153 #define Protocol_Register_6 73
154 #define LinePolarities Protocol_Register_6
155 #define InvertAck (1<<0)
156 #define InvertReq (1<<1)
157 #define InvertClock (1<<2)
158 #define InvertSerial (1<<3)
159 #define OpenAck (1<<4)
160 #define OpenClock (1<<5)
162 #define Protocol_Register_7 74
163 #define AckSer Protocol_Register_7
164 #define AckLine(x) (((x)&3)<<2)
165 #define ExchangePins (1<<7)
167 #define Interrupt_Control 75
168 /* bits same as flags */
170 #define DMA_Line_Control_Group1 76
171 #define DMA_Line_Control_Group2 108
172 /* channel zero is none */
173 static inline unsigned primary_DMAChannel_bits(unsigned channel)
175 return channel & 0x3;
178 static inline unsigned secondary_DMAChannel_bits(unsigned channel)
180 return (channel << 2) & 0xc;
183 #define Transfer_Size_Control 77
184 #define TransferWidth(x) ((x)&3)
185 #define TransferLength(x) (((x)&3)<<3)
186 #define RequireRLevel (1<<5)
188 #define Protocol_Register_15 79
189 #define DAQOptions Protocol_Register_15
190 #define StartSource(x) ((x)&0x3)
191 #define InvertStart (1<<2)
192 #define StopSource(x) (((x)&0x3)<<3)
193 #define ReqStart (1<<6)
194 #define PreStart (1<<7)
196 #define Pattern_Detection 81
197 #define DetectionMethod (1<<0)
198 #define InvertMatch (1<<1)
199 #define IE_Pattern_Detection (1<<2)
201 #define Protocol_Register_9 82
202 #define ReqDelay Protocol_Register_9
204 #define Protocol_Register_10 83
205 #define ReqNotDelay Protocol_Register_10
207 #define Protocol_Register_11 84
208 #define AckDelay Protocol_Register_11
210 #define Protocol_Register_12 85
211 #define AckNotDelay Protocol_Register_12
213 #define Protocol_Register_13 86
214 #define Data1Delay Protocol_Register_13
216 #define Protocol_Register_8 88 /* 32 bit */
217 #define StartDelay Protocol_Register_8
219 /* Firmware files for PCI-6524 */
220 #define FW_PCI_6534_MAIN "ni6534a.bin"
221 #define FW_PCI_6534_SCARAB_DI "niscrb01.bin"
222 #define FW_PCI_6534_SCARAB_DO "niscrb02.bin"
223 MODULE_FIRMWARE(FW_PCI_6534_MAIN);
224 MODULE_FIRMWARE(FW_PCI_6534_SCARAB_DI);
225 MODULE_FIRMWARE(FW_PCI_6534_SCARAB_DO);
227 enum pci_6534_firmware_registers { /* 16 bit */
228 Firmware_Control_Register = 0x100,
229 Firmware_Status_Register = 0x104,
230 Firmware_Data_Register = 0x108,
231 Firmware_Mask_Register = 0x10c,
232 Firmware_Debug_Register = 0x110,
234 /* main fpga registers (32 bit)*/
235 enum pci_6534_fpga_registers {
236 FPGA_Control1_Register = 0x200,
237 FPGA_Control2_Register = 0x204,
238 FPGA_Irq_Mask_Register = 0x208,
239 FPGA_Status_Register = 0x20c,
240 FPGA_Signature_Register = 0x210,
241 FPGA_SCALS_Counter_Register = 0x280, /*write-clear */
242 FPGA_SCAMS_Counter_Register = 0x284, /*write-clear */
243 FPGA_SCBLS_Counter_Register = 0x288, /*write-clear */
244 FPGA_SCBMS_Counter_Register = 0x28c, /*write-clear */
245 FPGA_Temp_Control_Register = 0x2a0,
246 FPGA_DAR_Register = 0x2a8,
247 FPGA_ELC_Read_Register = 0x2b8,
248 FPGA_ELC_Write_Register = 0x2bc,
250 enum FPGA_Control_Bits {
251 FPGA_Enable_Bit = 0x8000,
254 #define TIMER_BASE 50 /* nanoseconds */
257 #define IntEn (CountExpired|Waited|PrimaryTC|SecondaryTC)
259 #define IntEn (TransferReady|CountExpired|Waited|PrimaryTC|SecondaryTC)
270 unsigned int uses_firmware:1;
273 static const struct nidio_board nidio_boards[] = {
274 [BOARD_PCIDIO_32HS] = {
275 .name = "pci-dio-32hs",
286 struct nidio96_private {
287 struct mite_struct *mite;
290 unsigned short OpModeBits;
291 struct mite_channel *di_mite_chan;
292 struct mite_dma_descriptor_ring *di_mite_ring;
293 spinlock_t mite_channel_lock;
296 static int ni_pcidio_request_di_mite_channel(struct comedi_device *dev)
298 struct nidio96_private *devpriv = dev->private;
301 spin_lock_irqsave(&devpriv->mite_channel_lock, flags);
302 BUG_ON(devpriv->di_mite_chan);
303 devpriv->di_mite_chan =
304 mite_request_channel_in_range(devpriv->mite,
305 devpriv->di_mite_ring, 1, 2);
306 if (!devpriv->di_mite_chan) {
307 spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
308 dev_err(dev->class_dev, "failed to reserve mite dma channel\n");
311 devpriv->di_mite_chan->dir = COMEDI_INPUT;
312 writeb(primary_DMAChannel_bits(devpriv->di_mite_chan->channel) |
313 secondary_DMAChannel_bits(devpriv->di_mite_chan->channel),
314 dev->mmio + DMA_Line_Control_Group1);
316 spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
320 static void ni_pcidio_release_di_mite_channel(struct comedi_device *dev)
322 struct nidio96_private *devpriv = dev->private;
325 spin_lock_irqsave(&devpriv->mite_channel_lock, flags);
326 if (devpriv->di_mite_chan) {
327 mite_dma_disarm(devpriv->di_mite_chan);
328 mite_dma_reset(devpriv->di_mite_chan);
329 mite_release_channel(devpriv->di_mite_chan);
330 devpriv->di_mite_chan = NULL;
331 writeb(primary_DMAChannel_bits(0) |
332 secondary_DMAChannel_bits(0),
333 dev->mmio + DMA_Line_Control_Group1);
336 spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
339 static int setup_mite_dma(struct comedi_device *dev, struct comedi_subdevice *s)
341 struct nidio96_private *devpriv = dev->private;
345 retval = ni_pcidio_request_di_mite_channel(dev);
349 /* write alloc the entire buffer */
350 comedi_buf_write_alloc(s, s->async->prealloc_bufsz);
352 spin_lock_irqsave(&devpriv->mite_channel_lock, flags);
353 if (devpriv->di_mite_chan) {
354 mite_prep_dma(devpriv->di_mite_chan, 32, 32);
355 mite_dma_arm(devpriv->di_mite_chan);
359 spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
364 static int ni_pcidio_poll(struct comedi_device *dev, struct comedi_subdevice *s)
366 struct nidio96_private *devpriv = dev->private;
367 unsigned long irq_flags;
370 spin_lock_irqsave(&dev->spinlock, irq_flags);
371 spin_lock(&devpriv->mite_channel_lock);
372 if (devpriv->di_mite_chan)
373 mite_sync_input_dma(devpriv->di_mite_chan, s);
374 spin_unlock(&devpriv->mite_channel_lock);
375 count = comedi_buf_n_bytes_ready(s);
376 spin_unlock_irqrestore(&dev->spinlock, irq_flags);
380 static irqreturn_t nidio_interrupt(int irq, void *d)
382 struct comedi_device *dev = d;
383 struct nidio96_private *devpriv = dev->private;
384 struct comedi_subdevice *s = dev->read_subdev;
385 struct comedi_async *async = s->async;
386 struct mite_struct *mite = devpriv->mite;
387 unsigned int auxdata;
391 unsigned int m_status = 0;
393 /* interrupcions parasites */
394 if (!dev->attached) {
395 /* assume it's from another card */
399 /* Lock to avoid race with comedi_poll */
400 spin_lock(&dev->spinlock);
402 status = readb(dev->mmio + Interrupt_And_Window_Status);
403 flags = readb(dev->mmio + Group_1_Flags);
405 spin_lock(&devpriv->mite_channel_lock);
406 if (devpriv->di_mite_chan)
407 m_status = mite_get_status(devpriv->di_mite_chan);
409 if (m_status & CHSR_INT) {
410 if (m_status & CHSR_LINKC) {
413 MITE_CHOR(devpriv->di_mite_chan->channel));
414 mite_sync_input_dma(devpriv->di_mite_chan, s);
415 /* XXX need to byteswap */
417 if (m_status & ~(CHSR_INT | CHSR_LINKC | CHSR_DONE | CHSR_DRDY |
418 CHSR_DRQ1 | CHSR_MRDY)) {
419 dev_dbg(dev->class_dev,
420 "unknown mite interrupt, disabling IRQ\n");
421 async->events |= COMEDI_CB_ERROR;
422 disable_irq(dev->irq);
425 spin_unlock(&devpriv->mite_channel_lock);
427 while (status & DataLeft) {
430 dev_dbg(dev->class_dev, "too much work in interrupt\n");
432 dev->mmio + Master_DMA_And_Interrupt_Control);
438 if (flags & TransferReady) {
439 while (flags & TransferReady) {
442 dev_dbg(dev->class_dev,
443 "too much work in interrupt\n");
444 writeb(0x00, dev->mmio +
445 Master_DMA_And_Interrupt_Control
449 auxdata = readl(dev->mmio + Group_1_FIFO);
450 comedi_buf_write_samples(s, &auxdata, 1);
451 flags = readb(dev->mmio + Group_1_Flags);
455 if (flags & CountExpired) {
456 writeb(ClearExpired, dev->mmio + Group_1_Second_Clear);
457 async->events |= COMEDI_CB_EOA;
459 writeb(0x00, dev->mmio + OpMode);
461 } else if (flags & Waited) {
462 writeb(ClearWaited, dev->mmio + Group_1_First_Clear);
463 async->events |= COMEDI_CB_ERROR;
465 } else if (flags & PrimaryTC) {
466 writeb(ClearPrimaryTC,
467 dev->mmio + Group_1_First_Clear);
468 async->events |= COMEDI_CB_EOA;
469 } else if (flags & SecondaryTC) {
470 writeb(ClearSecondaryTC,
471 dev->mmio + Group_1_First_Clear);
472 async->events |= COMEDI_CB_EOA;
475 flags = readb(dev->mmio + Group_1_Flags);
476 status = readb(dev->mmio + Interrupt_And_Window_Status);
480 comedi_handle_events(dev, s);
483 writeb(0x03, dev->mmio + Master_DMA_And_Interrupt_Control);
486 spin_unlock(&dev->spinlock);
490 static int ni_pcidio_insn_config(struct comedi_device *dev,
491 struct comedi_subdevice *s,
492 struct comedi_insn *insn,
497 ret = comedi_dio_insn_config(dev, s, insn, data, 0);
501 writel(s->io_bits, dev->mmio + Port_Pin_Directions(0));
506 static int ni_pcidio_insn_bits(struct comedi_device *dev,
507 struct comedi_subdevice *s,
508 struct comedi_insn *insn,
511 if (comedi_dio_update_state(s, data))
512 writel(s->state, dev->mmio + Port_IO(0));
514 data[1] = readl(dev->mmio + Port_IO(0));
519 static int ni_pcidio_ns_to_timer(int *nanosec, unsigned int flags)
525 switch (flags & CMDF_ROUND_MASK) {
526 case CMDF_ROUND_NEAREST:
528 divider = (*nanosec + base / 2) / base;
530 case CMDF_ROUND_DOWN:
531 divider = (*nanosec) / base;
534 divider = (*nanosec + base - 1) / base;
538 *nanosec = base * divider;
542 static int ni_pcidio_cmdtest(struct comedi_device *dev,
543 struct comedi_subdevice *s, struct comedi_cmd *cmd)
548 /* Step 1 : check if triggers are trivially valid */
550 err |= comedi_check_trigger_src(&cmd->start_src, TRIG_NOW | TRIG_INT);
551 err |= comedi_check_trigger_src(&cmd->scan_begin_src,
552 TRIG_TIMER | TRIG_EXT);
553 err |= comedi_check_trigger_src(&cmd->convert_src, TRIG_NOW);
554 err |= comedi_check_trigger_src(&cmd->scan_end_src, TRIG_COUNT);
555 err |= comedi_check_trigger_src(&cmd->stop_src, TRIG_COUNT | TRIG_NONE);
560 /* Step 2a : make sure trigger sources are unique */
562 err |= comedi_check_trigger_is_unique(cmd->start_src);
563 err |= comedi_check_trigger_is_unique(cmd->scan_begin_src);
564 err |= comedi_check_trigger_is_unique(cmd->stop_src);
566 /* Step 2b : and mutually compatible */
571 /* Step 3: check if arguments are trivially valid */
573 err |= comedi_check_trigger_arg_is(&cmd->start_arg, 0);
575 #define MAX_SPEED (TIMER_BASE) /* in nanoseconds */
577 if (cmd->scan_begin_src == TRIG_TIMER) {
578 err |= comedi_check_trigger_arg_min(&cmd->scan_begin_arg,
580 /* no minimum speed */
583 /* should be level/edge, hi/lo specification here */
584 if ((cmd->scan_begin_arg & ~(CR_EDGE | CR_INVERT)) != 0) {
585 cmd->scan_begin_arg &= (CR_EDGE | CR_INVERT);
590 err |= comedi_check_trigger_arg_is(&cmd->convert_arg, 0);
591 err |= comedi_check_trigger_arg_is(&cmd->scan_end_arg,
594 if (cmd->stop_src == TRIG_COUNT)
595 err |= comedi_check_trigger_arg_min(&cmd->stop_arg, 1);
597 err |= comedi_check_trigger_arg_is(&cmd->stop_arg, 0);
602 /* step 4: fix up any arguments */
604 if (cmd->scan_begin_src == TRIG_TIMER) {
605 arg = cmd->scan_begin_arg;
606 ni_pcidio_ns_to_timer(&arg, cmd->flags);
607 err |= comedi_check_trigger_arg_is(&cmd->scan_begin_arg, arg);
616 static int ni_pcidio_inttrig(struct comedi_device *dev,
617 struct comedi_subdevice *s,
618 unsigned int trig_num)
620 struct nidio96_private *devpriv = dev->private;
621 struct comedi_cmd *cmd = &s->async->cmd;
623 if (trig_num != cmd->start_arg)
626 writeb(devpriv->OpModeBits, dev->mmio + OpMode);
627 s->async->inttrig = NULL;
632 static int ni_pcidio_cmd(struct comedi_device *dev, struct comedi_subdevice *s)
634 struct nidio96_private *devpriv = dev->private;
635 struct comedi_cmd *cmd = &s->async->cmd;
637 /* XXX configure ports for input */
638 writel(0x0000, dev->mmio + Port_Pin_Directions(0));
641 /* enable fifos A B C D */
642 writeb(0x0f, dev->mmio + Data_Path);
644 /* set transfer width a 32 bits */
645 writeb(TransferWidth(0) | TransferLength(0),
646 dev->mmio + Transfer_Size_Control);
648 writeb(0x03, dev->mmio + Data_Path);
649 writeb(TransferWidth(3) | TransferLength(0),
650 dev->mmio + Transfer_Size_Control);
653 /* protocol configuration */
654 if (cmd->scan_begin_src == TRIG_TIMER) {
655 /* page 4-5, "input with internal REQs" */
656 writeb(0, dev->mmio + OpMode);
657 writeb(0x00, dev->mmio + ClockReg);
658 writeb(1, dev->mmio + Sequence);
659 writeb(0x04, dev->mmio + ReqReg);
660 writeb(4, dev->mmio + BlockMode);
661 writeb(3, dev->mmio + LinePolarities);
662 writeb(0xc0, dev->mmio + AckSer);
663 writel(ni_pcidio_ns_to_timer(&cmd->scan_begin_arg,
665 dev->mmio + StartDelay);
666 writeb(1, dev->mmio + ReqDelay);
667 writeb(1, dev->mmio + ReqNotDelay);
668 writeb(1, dev->mmio + AckDelay);
669 writeb(0x0b, dev->mmio + AckNotDelay);
670 writeb(0x01, dev->mmio + Data1Delay);
671 /* manual, page 4-5: ClockSpeed comment is incorrectly listed
673 writew(0, dev->mmio + ClockSpeed);
674 writeb(0, dev->mmio + DAQOptions);
677 /* page 4-5, "input with external REQs" */
678 writeb(0, dev->mmio + OpMode);
679 writeb(0x00, dev->mmio + ClockReg);
680 writeb(0, dev->mmio + Sequence);
681 writeb(0x00, dev->mmio + ReqReg);
682 writeb(4, dev->mmio + BlockMode);
683 if (!(cmd->scan_begin_arg & CR_INVERT)) /* Leading Edge */
684 writeb(0, dev->mmio + LinePolarities);
685 else /* Trailing Edge */
686 writeb(2, dev->mmio + LinePolarities);
687 writeb(0x00, dev->mmio + AckSer);
688 writel(1, dev->mmio + StartDelay);
689 writeb(1, dev->mmio + ReqDelay);
690 writeb(1, dev->mmio + ReqNotDelay);
691 writeb(1, dev->mmio + AckDelay);
692 writeb(0x0C, dev->mmio + AckNotDelay);
693 writeb(0x10, dev->mmio + Data1Delay);
694 writew(0, dev->mmio + ClockSpeed);
695 writeb(0x60, dev->mmio + DAQOptions);
698 if (cmd->stop_src == TRIG_COUNT) {
699 writel(cmd->stop_arg,
700 dev->mmio + Transfer_Count);
706 writeb(ClearPrimaryTC | ClearSecondaryTC,
707 dev->mmio + Group_1_First_Clear);
710 int retval = setup_mite_dma(dev, s);
716 writeb(0x00, dev->mmio + DMA_Line_Control_Group1);
718 writeb(0x00, dev->mmio + DMA_Line_Control_Group2);
720 /* clear and enable interrupts */
721 writeb(0xff, dev->mmio + Group_1_First_Clear);
722 /* writeb(ClearExpired, dev->mmio+Group_1_Second_Clear); */
724 writeb(IntEn, dev->mmio + Interrupt_Control);
725 writeb(0x03, dev->mmio + Master_DMA_And_Interrupt_Control);
727 if (cmd->stop_src == TRIG_NONE) {
728 devpriv->OpModeBits = DataLatching(0) | RunMode(7);
729 } else { /* TRIG_TIMER */
730 devpriv->OpModeBits = Numbered | RunMode(7);
732 if (cmd->start_src == TRIG_NOW) {
734 writeb(devpriv->OpModeBits, dev->mmio + OpMode);
735 s->async->inttrig = NULL;
738 s->async->inttrig = ni_pcidio_inttrig;
744 static int ni_pcidio_cancel(struct comedi_device *dev,
745 struct comedi_subdevice *s)
747 writeb(0x00, dev->mmio + Master_DMA_And_Interrupt_Control);
748 ni_pcidio_release_di_mite_channel(dev);
753 static int ni_pcidio_change(struct comedi_device *dev,
754 struct comedi_subdevice *s)
756 struct nidio96_private *devpriv = dev->private;
759 ret = mite_buf_change(devpriv->di_mite_ring, s);
763 memset(s->async->prealloc_buf, 0xaa, s->async->prealloc_bufsz);
768 static int pci_6534_load_fpga(struct comedi_device *dev,
769 const u8 *data, size_t data_len,
770 unsigned long context)
772 static const int timeout = 1000;
773 int fpga_index = context;
777 writew(0x80 | fpga_index, dev->mmio + Firmware_Control_Register);
778 writew(0xc0 | fpga_index, dev->mmio + Firmware_Control_Register);
780 (readw(dev->mmio + Firmware_Status_Register) & 0x2) == 0 &&
785 dev_warn(dev->class_dev,
786 "ni_pcidio: failed to load fpga %i, waiting for status 0x2\n",
790 writew(0x80 | fpga_index, dev->mmio + Firmware_Control_Register);
792 readw(dev->mmio + Firmware_Status_Register) != 0x3 &&
797 dev_warn(dev->class_dev,
798 "ni_pcidio: failed to load fpga %i, waiting for status 0x3\n",
802 for (j = 0; j + 1 < data_len;) {
803 unsigned int value = data[j++];
805 value |= data[j++] << 8;
806 writew(value, dev->mmio + Firmware_Data_Register);
808 (readw(dev->mmio + Firmware_Status_Register) & 0x2) == 0
809 && i < timeout; ++i) {
813 dev_warn(dev->class_dev,
814 "ni_pcidio: failed to load word into fpga %i\n",
821 writew(0x0, dev->mmio + Firmware_Control_Register);
825 static int pci_6534_reset_fpga(struct comedi_device *dev, int fpga_index)
827 return pci_6534_load_fpga(dev, NULL, 0, fpga_index);
830 static int pci_6534_reset_fpgas(struct comedi_device *dev)
835 writew(0x0, dev->mmio + Firmware_Control_Register);
836 for (i = 0; i < 3; ++i) {
837 ret = pci_6534_reset_fpga(dev, i);
841 writew(0x0, dev->mmio + Firmware_Mask_Register);
845 static void pci_6534_init_main_fpga(struct comedi_device *dev)
847 writel(0, dev->mmio + FPGA_Control1_Register);
848 writel(0, dev->mmio + FPGA_Control2_Register);
849 writel(0, dev->mmio + FPGA_SCALS_Counter_Register);
850 writel(0, dev->mmio + FPGA_SCAMS_Counter_Register);
851 writel(0, dev->mmio + FPGA_SCBLS_Counter_Register);
852 writel(0, dev->mmio + FPGA_SCBMS_Counter_Register);
855 static int pci_6534_upload_firmware(struct comedi_device *dev)
857 struct nidio96_private *devpriv = dev->private;
858 static const char *const fw_file[3] = {
859 FW_PCI_6534_SCARAB_DI, /* loaded into scarab A for DI */
860 FW_PCI_6534_SCARAB_DO, /* loaded into scarab B for DO */
861 FW_PCI_6534_MAIN, /* loaded into main FPGA */
866 ret = pci_6534_reset_fpgas(dev);
869 /* load main FPGA first, then the two scarabs */
870 for (n = 2; n >= 0; n--) {
871 ret = comedi_load_firmware(dev, &devpriv->mite->pcidev->dev,
873 pci_6534_load_fpga, n);
874 if (ret == 0 && n == 2)
875 pci_6534_init_main_fpga(dev);
882 static void nidio_reset_board(struct comedi_device *dev)
884 writel(0, dev->mmio + Port_IO(0));
885 writel(0, dev->mmio + Port_Pin_Directions(0));
886 writel(0, dev->mmio + Port_Pin_Mask(0));
888 /* disable interrupts on board */
889 writeb(0, dev->mmio + Master_DMA_And_Interrupt_Control);
892 static int nidio_auto_attach(struct comedi_device *dev,
893 unsigned long context)
895 struct pci_dev *pcidev = comedi_to_pci_dev(dev);
896 const struct nidio_board *board = NULL;
897 struct nidio96_private *devpriv;
898 struct comedi_subdevice *s;
902 if (context < ARRAY_SIZE(nidio_boards))
903 board = &nidio_boards[context];
906 dev->board_ptr = board;
907 dev->board_name = board->name;
909 ret = comedi_pci_enable(dev);
913 devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
917 spin_lock_init(&devpriv->mite_channel_lock);
919 devpriv->mite = mite_alloc(pcidev);
923 ret = mite_setup(dev, devpriv->mite);
927 devpriv->di_mite_ring = mite_alloc_ring(devpriv->mite);
928 if (!devpriv->di_mite_ring)
931 if (board->uses_firmware) {
932 ret = pci_6534_upload_firmware(dev);
937 nidio_reset_board(dev);
939 ret = comedi_alloc_subdevices(dev, 1);
943 dev_info(dev->class_dev, "%s rev=%d\n", dev->board_name,
944 readb(dev->mmio + Chip_Version));
946 s = &dev->subdevices[0];
948 dev->read_subdev = s;
949 s->type = COMEDI_SUBD_DIO;
951 SDF_READABLE | SDF_WRITABLE | SDF_LSAMPL | SDF_PACKED |
954 s->range_table = &range_digital;
956 s->insn_config = &ni_pcidio_insn_config;
957 s->insn_bits = &ni_pcidio_insn_bits;
958 s->do_cmd = &ni_pcidio_cmd;
959 s->do_cmdtest = &ni_pcidio_cmdtest;
960 s->cancel = &ni_pcidio_cancel;
961 s->len_chanlist = 32; /* XXX */
962 s->buf_change = &ni_pcidio_change;
963 s->async_dma_dir = DMA_BIDIRECTIONAL;
964 s->poll = &ni_pcidio_poll;
968 ret = request_irq(irq, nidio_interrupt, IRQF_SHARED,
969 dev->board_name, dev);
977 static void nidio_detach(struct comedi_device *dev)
979 struct nidio96_private *devpriv = dev->private;
982 free_irq(dev->irq, dev);
984 if (devpriv->di_mite_ring) {
985 mite_free_ring(devpriv->di_mite_ring);
986 devpriv->di_mite_ring = NULL;
988 mite_detach(devpriv->mite);
992 comedi_pci_disable(dev);
995 static struct comedi_driver ni_pcidio_driver = {
996 .driver_name = "ni_pcidio",
997 .module = THIS_MODULE,
998 .auto_attach = nidio_auto_attach,
999 .detach = nidio_detach,
1002 static int ni_pcidio_pci_probe(struct pci_dev *dev,
1003 const struct pci_device_id *id)
1005 return comedi_pci_auto_config(dev, &ni_pcidio_driver, id->driver_data);
1008 static const struct pci_device_id ni_pcidio_pci_table[] = {
1009 { PCI_VDEVICE(NI, 0x1150), BOARD_PCIDIO_32HS },
1010 { PCI_VDEVICE(NI, 0x12b0), BOARD_PCI6534 },
1011 { PCI_VDEVICE(NI, 0x1320), BOARD_PXI6533 },
1014 MODULE_DEVICE_TABLE(pci, ni_pcidio_pci_table);
1016 static struct pci_driver ni_pcidio_pci_driver = {
1017 .name = "ni_pcidio",
1018 .id_table = ni_pcidio_pci_table,
1019 .probe = ni_pcidio_pci_probe,
1020 .remove = comedi_pci_auto_unconfig,
1022 module_comedi_pci_driver(ni_pcidio_driver, ni_pcidio_pci_driver);
1024 MODULE_AUTHOR("Comedi http://www.comedi.org");
1025 MODULE_DESCRIPTION("Comedi low-level driver");
1026 MODULE_LICENSE("GPL");