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 "/*(DEBLOBBED)*/"
221 #define FW_PCI_6534_SCARAB_DI "/*(DEBLOBBED)*/"
222 #define FW_PCI_6534_SCARAB_DO "/*(DEBLOBBED)*/"
225 enum pci_6534_firmware_registers { /* 16 bit */
226 Firmware_Control_Register = 0x100,
227 Firmware_Status_Register = 0x104,
228 Firmware_Data_Register = 0x108,
229 Firmware_Mask_Register = 0x10c,
230 Firmware_Debug_Register = 0x110,
232 /* main fpga registers (32 bit)*/
233 enum pci_6534_fpga_registers {
234 FPGA_Control1_Register = 0x200,
235 FPGA_Control2_Register = 0x204,
236 FPGA_Irq_Mask_Register = 0x208,
237 FPGA_Status_Register = 0x20c,
238 FPGA_Signature_Register = 0x210,
239 FPGA_SCALS_Counter_Register = 0x280, /*write-clear */
240 FPGA_SCAMS_Counter_Register = 0x284, /*write-clear */
241 FPGA_SCBLS_Counter_Register = 0x288, /*write-clear */
242 FPGA_SCBMS_Counter_Register = 0x28c, /*write-clear */
243 FPGA_Temp_Control_Register = 0x2a0,
244 FPGA_DAR_Register = 0x2a8,
245 FPGA_ELC_Read_Register = 0x2b8,
246 FPGA_ELC_Write_Register = 0x2bc,
248 enum FPGA_Control_Bits {
249 FPGA_Enable_Bit = 0x8000,
252 #define TIMER_BASE 50 /* nanoseconds */
255 #define IntEn (CountExpired|Waited|PrimaryTC|SecondaryTC)
257 #define IntEn (TransferReady|CountExpired|Waited|PrimaryTC|SecondaryTC)
268 unsigned int uses_firmware:1;
271 static const struct nidio_board nidio_boards[] = {
272 [BOARD_PCIDIO_32HS] = {
273 .name = "pci-dio-32hs",
284 struct nidio96_private {
285 struct mite_struct *mite;
288 unsigned short OpModeBits;
289 struct mite_channel *di_mite_chan;
290 struct mite_dma_descriptor_ring *di_mite_ring;
291 spinlock_t mite_channel_lock;
294 static int ni_pcidio_request_di_mite_channel(struct comedi_device *dev)
296 struct nidio96_private *devpriv = dev->private;
299 spin_lock_irqsave(&devpriv->mite_channel_lock, flags);
300 BUG_ON(devpriv->di_mite_chan);
301 devpriv->di_mite_chan =
302 mite_request_channel_in_range(devpriv->mite,
303 devpriv->di_mite_ring, 1, 2);
304 if (!devpriv->di_mite_chan) {
305 spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
306 dev_err(dev->class_dev, "failed to reserve mite dma channel\n");
309 devpriv->di_mite_chan->dir = COMEDI_INPUT;
310 writeb(primary_DMAChannel_bits(devpriv->di_mite_chan->channel) |
311 secondary_DMAChannel_bits(devpriv->di_mite_chan->channel),
312 dev->mmio + DMA_Line_Control_Group1);
314 spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
318 static void ni_pcidio_release_di_mite_channel(struct comedi_device *dev)
320 struct nidio96_private *devpriv = dev->private;
323 spin_lock_irqsave(&devpriv->mite_channel_lock, flags);
324 if (devpriv->di_mite_chan) {
325 mite_dma_disarm(devpriv->di_mite_chan);
326 mite_dma_reset(devpriv->di_mite_chan);
327 mite_release_channel(devpriv->di_mite_chan);
328 devpriv->di_mite_chan = NULL;
329 writeb(primary_DMAChannel_bits(0) |
330 secondary_DMAChannel_bits(0),
331 dev->mmio + DMA_Line_Control_Group1);
334 spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
337 static int setup_mite_dma(struct comedi_device *dev, struct comedi_subdevice *s)
339 struct nidio96_private *devpriv = dev->private;
343 retval = ni_pcidio_request_di_mite_channel(dev);
347 /* write alloc the entire buffer */
348 comedi_buf_write_alloc(s, s->async->prealloc_bufsz);
350 spin_lock_irqsave(&devpriv->mite_channel_lock, flags);
351 if (devpriv->di_mite_chan) {
352 mite_prep_dma(devpriv->di_mite_chan, 32, 32);
353 mite_dma_arm(devpriv->di_mite_chan);
357 spin_unlock_irqrestore(&devpriv->mite_channel_lock, flags);
362 static int ni_pcidio_poll(struct comedi_device *dev, struct comedi_subdevice *s)
364 struct nidio96_private *devpriv = dev->private;
365 unsigned long irq_flags;
368 spin_lock_irqsave(&dev->spinlock, irq_flags);
369 spin_lock(&devpriv->mite_channel_lock);
370 if (devpriv->di_mite_chan)
371 mite_sync_input_dma(devpriv->di_mite_chan, s);
372 spin_unlock(&devpriv->mite_channel_lock);
373 count = comedi_buf_n_bytes_ready(s);
374 spin_unlock_irqrestore(&dev->spinlock, irq_flags);
378 static irqreturn_t nidio_interrupt(int irq, void *d)
380 struct comedi_device *dev = d;
381 struct nidio96_private *devpriv = dev->private;
382 struct comedi_subdevice *s = dev->read_subdev;
383 struct comedi_async *async = s->async;
384 struct mite_struct *mite = devpriv->mite;
385 unsigned int auxdata;
389 unsigned int m_status = 0;
391 /* interrupcions parasites */
392 if (!dev->attached) {
393 /* assume it's from another card */
397 /* Lock to avoid race with comedi_poll */
398 spin_lock(&dev->spinlock);
400 status = readb(dev->mmio + Interrupt_And_Window_Status);
401 flags = readb(dev->mmio + Group_1_Flags);
403 spin_lock(&devpriv->mite_channel_lock);
404 if (devpriv->di_mite_chan)
405 m_status = mite_get_status(devpriv->di_mite_chan);
407 if (m_status & CHSR_INT) {
408 if (m_status & CHSR_LINKC) {
411 MITE_CHOR(devpriv->di_mite_chan->channel));
412 mite_sync_input_dma(devpriv->di_mite_chan, s);
413 /* XXX need to byteswap */
415 if (m_status & ~(CHSR_INT | CHSR_LINKC | CHSR_DONE | CHSR_DRDY |
416 CHSR_DRQ1 | CHSR_MRDY)) {
417 dev_dbg(dev->class_dev,
418 "unknown mite interrupt, disabling IRQ\n");
419 async->events |= COMEDI_CB_ERROR;
420 disable_irq(dev->irq);
423 spin_unlock(&devpriv->mite_channel_lock);
425 while (status & DataLeft) {
428 dev_dbg(dev->class_dev, "too much work in interrupt\n");
430 dev->mmio + Master_DMA_And_Interrupt_Control);
436 if (flags & TransferReady) {
437 while (flags & TransferReady) {
440 dev_dbg(dev->class_dev,
441 "too much work in interrupt\n");
442 writeb(0x00, dev->mmio +
443 Master_DMA_And_Interrupt_Control
447 auxdata = readl(dev->mmio + Group_1_FIFO);
448 comedi_buf_write_samples(s, &auxdata, 1);
449 flags = readb(dev->mmio + Group_1_Flags);
453 if (flags & CountExpired) {
454 writeb(ClearExpired, dev->mmio + Group_1_Second_Clear);
455 async->events |= COMEDI_CB_EOA;
457 writeb(0x00, dev->mmio + OpMode);
459 } else if (flags & Waited) {
460 writeb(ClearWaited, dev->mmio + Group_1_First_Clear);
461 async->events |= COMEDI_CB_ERROR;
463 } else if (flags & PrimaryTC) {
464 writeb(ClearPrimaryTC,
465 dev->mmio + Group_1_First_Clear);
466 async->events |= COMEDI_CB_EOA;
467 } else if (flags & SecondaryTC) {
468 writeb(ClearSecondaryTC,
469 dev->mmio + Group_1_First_Clear);
470 async->events |= COMEDI_CB_EOA;
473 flags = readb(dev->mmio + Group_1_Flags);
474 status = readb(dev->mmio + Interrupt_And_Window_Status);
478 comedi_handle_events(dev, s);
481 writeb(0x03, dev->mmio + Master_DMA_And_Interrupt_Control);
484 spin_unlock(&dev->spinlock);
488 static int ni_pcidio_insn_config(struct comedi_device *dev,
489 struct comedi_subdevice *s,
490 struct comedi_insn *insn,
495 ret = comedi_dio_insn_config(dev, s, insn, data, 0);
499 writel(s->io_bits, dev->mmio + Port_Pin_Directions(0));
504 static int ni_pcidio_insn_bits(struct comedi_device *dev,
505 struct comedi_subdevice *s,
506 struct comedi_insn *insn,
509 if (comedi_dio_update_state(s, data))
510 writel(s->state, dev->mmio + Port_IO(0));
512 data[1] = readl(dev->mmio + Port_IO(0));
517 static int ni_pcidio_ns_to_timer(int *nanosec, unsigned int flags)
523 switch (flags & CMDF_ROUND_MASK) {
524 case CMDF_ROUND_NEAREST:
526 divider = (*nanosec + base / 2) / base;
528 case CMDF_ROUND_DOWN:
529 divider = (*nanosec) / base;
532 divider = (*nanosec + base - 1) / base;
536 *nanosec = base * divider;
540 static int ni_pcidio_cmdtest(struct comedi_device *dev,
541 struct comedi_subdevice *s, struct comedi_cmd *cmd)
546 /* Step 1 : check if triggers are trivially valid */
548 err |= comedi_check_trigger_src(&cmd->start_src, TRIG_NOW | TRIG_INT);
549 err |= comedi_check_trigger_src(&cmd->scan_begin_src,
550 TRIG_TIMER | TRIG_EXT);
551 err |= comedi_check_trigger_src(&cmd->convert_src, TRIG_NOW);
552 err |= comedi_check_trigger_src(&cmd->scan_end_src, TRIG_COUNT);
553 err |= comedi_check_trigger_src(&cmd->stop_src, TRIG_COUNT | TRIG_NONE);
558 /* Step 2a : make sure trigger sources are unique */
560 err |= comedi_check_trigger_is_unique(cmd->start_src);
561 err |= comedi_check_trigger_is_unique(cmd->scan_begin_src);
562 err |= comedi_check_trigger_is_unique(cmd->stop_src);
564 /* Step 2b : and mutually compatible */
569 /* Step 3: check if arguments are trivially valid */
571 err |= comedi_check_trigger_arg_is(&cmd->start_arg, 0);
573 #define MAX_SPEED (TIMER_BASE) /* in nanoseconds */
575 if (cmd->scan_begin_src == TRIG_TIMER) {
576 err |= comedi_check_trigger_arg_min(&cmd->scan_begin_arg,
578 /* no minimum speed */
581 /* should be level/edge, hi/lo specification here */
582 if ((cmd->scan_begin_arg & ~(CR_EDGE | CR_INVERT)) != 0) {
583 cmd->scan_begin_arg &= (CR_EDGE | CR_INVERT);
588 err |= comedi_check_trigger_arg_is(&cmd->convert_arg, 0);
589 err |= comedi_check_trigger_arg_is(&cmd->scan_end_arg,
592 if (cmd->stop_src == TRIG_COUNT)
593 err |= comedi_check_trigger_arg_min(&cmd->stop_arg, 1);
595 err |= comedi_check_trigger_arg_is(&cmd->stop_arg, 0);
600 /* step 4: fix up any arguments */
602 if (cmd->scan_begin_src == TRIG_TIMER) {
603 arg = cmd->scan_begin_arg;
604 ni_pcidio_ns_to_timer(&arg, cmd->flags);
605 err |= comedi_check_trigger_arg_is(&cmd->scan_begin_arg, arg);
614 static int ni_pcidio_inttrig(struct comedi_device *dev,
615 struct comedi_subdevice *s,
616 unsigned int trig_num)
618 struct nidio96_private *devpriv = dev->private;
619 struct comedi_cmd *cmd = &s->async->cmd;
621 if (trig_num != cmd->start_arg)
624 writeb(devpriv->OpModeBits, dev->mmio + OpMode);
625 s->async->inttrig = NULL;
630 static int ni_pcidio_cmd(struct comedi_device *dev, struct comedi_subdevice *s)
632 struct nidio96_private *devpriv = dev->private;
633 struct comedi_cmd *cmd = &s->async->cmd;
635 /* XXX configure ports for input */
636 writel(0x0000, dev->mmio + Port_Pin_Directions(0));
639 /* enable fifos A B C D */
640 writeb(0x0f, dev->mmio + Data_Path);
642 /* set transfer width a 32 bits */
643 writeb(TransferWidth(0) | TransferLength(0),
644 dev->mmio + Transfer_Size_Control);
646 writeb(0x03, dev->mmio + Data_Path);
647 writeb(TransferWidth(3) | TransferLength(0),
648 dev->mmio + Transfer_Size_Control);
651 /* protocol configuration */
652 if (cmd->scan_begin_src == TRIG_TIMER) {
653 /* page 4-5, "input with internal REQs" */
654 writeb(0, dev->mmio + OpMode);
655 writeb(0x00, dev->mmio + ClockReg);
656 writeb(1, dev->mmio + Sequence);
657 writeb(0x04, dev->mmio + ReqReg);
658 writeb(4, dev->mmio + BlockMode);
659 writeb(3, dev->mmio + LinePolarities);
660 writeb(0xc0, dev->mmio + AckSer);
661 writel(ni_pcidio_ns_to_timer(&cmd->scan_begin_arg,
663 dev->mmio + StartDelay);
664 writeb(1, dev->mmio + ReqDelay);
665 writeb(1, dev->mmio + ReqNotDelay);
666 writeb(1, dev->mmio + AckDelay);
667 writeb(0x0b, dev->mmio + AckNotDelay);
668 writeb(0x01, dev->mmio + Data1Delay);
669 /* manual, page 4-5: ClockSpeed comment is incorrectly listed
671 writew(0, dev->mmio + ClockSpeed);
672 writeb(0, dev->mmio + DAQOptions);
675 /* page 4-5, "input with external REQs" */
676 writeb(0, dev->mmio + OpMode);
677 writeb(0x00, dev->mmio + ClockReg);
678 writeb(0, dev->mmio + Sequence);
679 writeb(0x00, dev->mmio + ReqReg);
680 writeb(4, dev->mmio + BlockMode);
681 if (!(cmd->scan_begin_arg & CR_INVERT)) /* Leading Edge */
682 writeb(0, dev->mmio + LinePolarities);
683 else /* Trailing Edge */
684 writeb(2, dev->mmio + LinePolarities);
685 writeb(0x00, dev->mmio + AckSer);
686 writel(1, dev->mmio + StartDelay);
687 writeb(1, dev->mmio + ReqDelay);
688 writeb(1, dev->mmio + ReqNotDelay);
689 writeb(1, dev->mmio + AckDelay);
690 writeb(0x0C, dev->mmio + AckNotDelay);
691 writeb(0x10, dev->mmio + Data1Delay);
692 writew(0, dev->mmio + ClockSpeed);
693 writeb(0x60, dev->mmio + DAQOptions);
696 if (cmd->stop_src == TRIG_COUNT) {
697 writel(cmd->stop_arg,
698 dev->mmio + Transfer_Count);
704 writeb(ClearPrimaryTC | ClearSecondaryTC,
705 dev->mmio + Group_1_First_Clear);
708 int retval = setup_mite_dma(dev, s);
714 writeb(0x00, dev->mmio + DMA_Line_Control_Group1);
716 writeb(0x00, dev->mmio + DMA_Line_Control_Group2);
718 /* clear and enable interrupts */
719 writeb(0xff, dev->mmio + Group_1_First_Clear);
720 /* writeb(ClearExpired, dev->mmio+Group_1_Second_Clear); */
722 writeb(IntEn, dev->mmio + Interrupt_Control);
723 writeb(0x03, dev->mmio + Master_DMA_And_Interrupt_Control);
725 if (cmd->stop_src == TRIG_NONE) {
726 devpriv->OpModeBits = DataLatching(0) | RunMode(7);
727 } else { /* TRIG_TIMER */
728 devpriv->OpModeBits = Numbered | RunMode(7);
730 if (cmd->start_src == TRIG_NOW) {
732 writeb(devpriv->OpModeBits, dev->mmio + OpMode);
733 s->async->inttrig = NULL;
736 s->async->inttrig = ni_pcidio_inttrig;
742 static int ni_pcidio_cancel(struct comedi_device *dev,
743 struct comedi_subdevice *s)
745 writeb(0x00, dev->mmio + Master_DMA_And_Interrupt_Control);
746 ni_pcidio_release_di_mite_channel(dev);
751 static int ni_pcidio_change(struct comedi_device *dev,
752 struct comedi_subdevice *s)
754 struct nidio96_private *devpriv = dev->private;
757 ret = mite_buf_change(devpriv->di_mite_ring, s);
761 memset(s->async->prealloc_buf, 0xaa, s->async->prealloc_bufsz);
766 static int pci_6534_load_fpga(struct comedi_device *dev,
767 const u8 *data, size_t data_len,
768 unsigned long context)
770 static const int timeout = 1000;
771 int fpga_index = context;
775 writew(0x80 | fpga_index, dev->mmio + Firmware_Control_Register);
776 writew(0xc0 | fpga_index, dev->mmio + Firmware_Control_Register);
778 (readw(dev->mmio + Firmware_Status_Register) & 0x2) == 0 &&
783 dev_warn(dev->class_dev,
784 "ni_pcidio: failed to load fpga %i, waiting for status 0x2\n",
788 writew(0x80 | fpga_index, dev->mmio + Firmware_Control_Register);
790 readw(dev->mmio + Firmware_Status_Register) != 0x3 &&
795 dev_warn(dev->class_dev,
796 "ni_pcidio: failed to load fpga %i, waiting for status 0x3\n",
800 for (j = 0; j + 1 < data_len;) {
801 unsigned int value = data[j++];
803 value |= data[j++] << 8;
804 writew(value, dev->mmio + Firmware_Data_Register);
806 (readw(dev->mmio + Firmware_Status_Register) & 0x2) == 0
807 && i < timeout; ++i) {
811 dev_warn(dev->class_dev,
812 "ni_pcidio: failed to load word into fpga %i\n",
819 writew(0x0, dev->mmio + Firmware_Control_Register);
823 static int pci_6534_reset_fpga(struct comedi_device *dev, int fpga_index)
825 return pci_6534_load_fpga(dev, NULL, 0, fpga_index);
828 static int pci_6534_reset_fpgas(struct comedi_device *dev)
833 writew(0x0, dev->mmio + Firmware_Control_Register);
834 for (i = 0; i < 3; ++i) {
835 ret = pci_6534_reset_fpga(dev, i);
839 writew(0x0, dev->mmio + Firmware_Mask_Register);
843 static void pci_6534_init_main_fpga(struct comedi_device *dev)
845 writel(0, dev->mmio + FPGA_Control1_Register);
846 writel(0, dev->mmio + FPGA_Control2_Register);
847 writel(0, dev->mmio + FPGA_SCALS_Counter_Register);
848 writel(0, dev->mmio + FPGA_SCAMS_Counter_Register);
849 writel(0, dev->mmio + FPGA_SCBLS_Counter_Register);
850 writel(0, dev->mmio + FPGA_SCBMS_Counter_Register);
853 static int pci_6534_upload_firmware(struct comedi_device *dev)
855 struct nidio96_private *devpriv = dev->private;
856 static const char *const fw_file[3] = {
857 FW_PCI_6534_SCARAB_DI, /* loaded into scarab A for DI */
858 FW_PCI_6534_SCARAB_DO, /* loaded into scarab B for DO */
859 FW_PCI_6534_MAIN, /* loaded into main FPGA */
864 ret = pci_6534_reset_fpgas(dev);
867 /* load main FPGA first, then the two scarabs */
868 for (n = 2; n >= 0; n--) {
869 ret = comedi_load_firmware(dev, &devpriv->mite->pcidev->dev,
871 pci_6534_load_fpga, n);
872 if (ret == 0 && n == 2)
873 pci_6534_init_main_fpga(dev);
880 static void nidio_reset_board(struct comedi_device *dev)
882 writel(0, dev->mmio + Port_IO(0));
883 writel(0, dev->mmio + Port_Pin_Directions(0));
884 writel(0, dev->mmio + Port_Pin_Mask(0));
886 /* disable interrupts on board */
887 writeb(0, dev->mmio + Master_DMA_And_Interrupt_Control);
890 static int nidio_auto_attach(struct comedi_device *dev,
891 unsigned long context)
893 struct pci_dev *pcidev = comedi_to_pci_dev(dev);
894 const struct nidio_board *board = NULL;
895 struct nidio96_private *devpriv;
896 struct comedi_subdevice *s;
900 if (context < ARRAY_SIZE(nidio_boards))
901 board = &nidio_boards[context];
904 dev->board_ptr = board;
905 dev->board_name = board->name;
907 ret = comedi_pci_enable(dev);
911 devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
915 spin_lock_init(&devpriv->mite_channel_lock);
917 devpriv->mite = mite_alloc(pcidev);
921 ret = mite_setup(dev, devpriv->mite);
925 devpriv->di_mite_ring = mite_alloc_ring(devpriv->mite);
926 if (!devpriv->di_mite_ring)
929 if (board->uses_firmware) {
930 ret = pci_6534_upload_firmware(dev);
935 nidio_reset_board(dev);
937 ret = comedi_alloc_subdevices(dev, 1);
941 dev_info(dev->class_dev, "%s rev=%d\n", dev->board_name,
942 readb(dev->mmio + Chip_Version));
944 s = &dev->subdevices[0];
946 dev->read_subdev = s;
947 s->type = COMEDI_SUBD_DIO;
949 SDF_READABLE | SDF_WRITABLE | SDF_LSAMPL | SDF_PACKED |
952 s->range_table = &range_digital;
954 s->insn_config = &ni_pcidio_insn_config;
955 s->insn_bits = &ni_pcidio_insn_bits;
956 s->do_cmd = &ni_pcidio_cmd;
957 s->do_cmdtest = &ni_pcidio_cmdtest;
958 s->cancel = &ni_pcidio_cancel;
959 s->len_chanlist = 32; /* XXX */
960 s->buf_change = &ni_pcidio_change;
961 s->async_dma_dir = DMA_BIDIRECTIONAL;
962 s->poll = &ni_pcidio_poll;
966 ret = request_irq(irq, nidio_interrupt, IRQF_SHARED,
967 dev->board_name, dev);
975 static void nidio_detach(struct comedi_device *dev)
977 struct nidio96_private *devpriv = dev->private;
980 free_irq(dev->irq, dev);
982 if (devpriv->di_mite_ring) {
983 mite_free_ring(devpriv->di_mite_ring);
984 devpriv->di_mite_ring = NULL;
986 mite_detach(devpriv->mite);
990 comedi_pci_disable(dev);
993 static struct comedi_driver ni_pcidio_driver = {
994 .driver_name = "ni_pcidio",
995 .module = THIS_MODULE,
996 .auto_attach = nidio_auto_attach,
997 .detach = nidio_detach,
1000 static int ni_pcidio_pci_probe(struct pci_dev *dev,
1001 const struct pci_device_id *id)
1003 return comedi_pci_auto_config(dev, &ni_pcidio_driver, id->driver_data);
1006 static const struct pci_device_id ni_pcidio_pci_table[] = {
1007 { PCI_VDEVICE(NI, 0x1150), BOARD_PCIDIO_32HS },
1008 { PCI_VDEVICE(NI, 0x12b0), BOARD_PCI6534 },
1009 { PCI_VDEVICE(NI, 0x1320), BOARD_PXI6533 },
1012 MODULE_DEVICE_TABLE(pci, ni_pcidio_pci_table);
1014 static struct pci_driver ni_pcidio_pci_driver = {
1015 .name = "ni_pcidio",
1016 .id_table = ni_pcidio_pci_table,
1017 .probe = ni_pcidio_pci_probe,
1018 .remove = comedi_pci_auto_unconfig,
1020 module_comedi_pci_driver(ni_pcidio_driver, ni_pcidio_pci_driver);
1022 MODULE_AUTHOR("Comedi http://www.comedi.org");
1023 MODULE_DESCRIPTION("Comedi low-level driver");
1024 MODULE_LICENSE("GPL");