Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
rtd520.c
Go to the documentation of this file.
1 /*
2  comedi/drivers/rtd520.c
3  Comedi driver for Real Time Devices (RTD) PCI4520/DM7520
4 
5  COMEDI - Linux Control and Measurement Device Interface
6  Copyright (C) 2001 David A. Schleef <[email protected]>
7 
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.
12 
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.
17 
18  You should have received a copy of the GNU General Public License
19  along with this program; if not, write to the Free Software
20  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22 /*
23 Driver: rtd520
24 Description: Real Time Devices PCI4520/DM7520
25 Author: Dan Christian
26 Devices: [Real Time Devices] DM7520HR-1 (rtd520), DM7520HR-8,
27  PCI4520, PCI4520-8
28 Status: Works. Only tested on DM7520-8. Not SMP safe.
29 
30 Configuration options:
31  [0] - PCI bus of device (optional)
32  If bus / slot is not specified, the first available PCI
33  device will be used.
34  [1] - PCI slot of device (optional)
35 */
36 /*
37  Created by Dan Christian, NASA Ames Research Center.
38 
39  The PCI4520 is a PCI card. The DM7520 is a PC/104-plus card.
40  Both have:
41  8/16 12 bit ADC with FIFO and channel gain table
42  8 bits high speed digital out (for external MUX) (or 8 in or 8 out)
43  8 bits high speed digital in with FIFO and interrupt on change (or 8 IO)
44  2 12 bit DACs with FIFOs
45  2 bits output
46  2 bits input
47  bus mastering DMA
48  timers: ADC sample, pacer, burst, about, delay, DA1, DA2
49  sample counter
50  3 user timer/counters (8254)
51  external interrupt
52 
53  The DM7520 has slightly fewer features (fewer gain steps).
54 
55  These boards can support external multiplexors and multi-board
56  synchronization, but this driver doesn't support that.
57 
58  Board docs: http://www.rtdusa.com/PC104/DM/analog%20IO/dm7520.htm
59  Data sheet: http://www.rtdusa.com/pdf/dm7520.pdf
60  Example source: http://www.rtdusa.com/examples/dm/dm7520.zip
61  Call them and ask for the register level manual.
62  PCI chip: http://www.plxtech.com/products/io/pci9080
63 
64  Notes:
65  This board is memory mapped. There is some IO stuff, but it isn't needed.
66 
67  I use a pretty loose naming style within the driver (rtd_blah).
68  All externally visible names should be rtd520_blah.
69  I use camelCase for structures (and inside them).
70  I may also use upper CamelCase for function names (old habit).
71 
72  This board is somewhat related to the RTD PCI4400 board.
73 
74  I borrowed heavily from the ni_mio_common, ni_atmio16d, mite, and
75  das1800, since they have the best documented code. Driver
76  cb_pcidas64.c uses the same DMA controller.
77 
78  As far as I can tell, the About interrupt doesn't work if Sample is
79  also enabled. It turns out that About really isn't needed, since
80  we always count down samples read.
81 
82  There was some timer/counter code, but it didn't follow the right API.
83 
84 */
85 
86 /*
87  driver status:
88 
89  Analog-In supports instruction and command mode.
90 
91  With DMA, you can sample at 1.15Mhz with 70% idle on a 400Mhz K6-2
92  (single channel, 64K read buffer). I get random system lockups when
93  using DMA with ALI-15xx based systems. I haven't been able to test
94  any other chipsets. The lockups happen soon after the start of an
95  acquistion, not in the middle of a long run.
96 
97  Without DMA, you can do 620Khz sampling with 20% idle on a 400Mhz K6-2
98  (with a 256K read buffer).
99 
100  Digital-IO and Analog-Out only support instruction mode.
101 
102 */
103 
104 #include <linux/interrupt.h>
105 #include <linux/delay.h>
106 
107 #include "../comedidev.h"
108 
109 #include "comedi_fc.h"
110 
111 #define DRV_NAME "rtd520"
112 
113 /*======================================================================
114  Driver specific stuff (tunable)
115 ======================================================================*/
116 /* Enable this to test the new DMA support. You may get hard lock ups */
117 /*#define USE_DMA*/
118 
119 /* We really only need 2 buffers. More than that means being much
120  smarter about knowing which ones are full. */
121 #define DMA_CHAIN_COUNT 2 /* max DMA segments/buffers in a ring (min 2) */
122 
123 /* Target period for periodic transfers. This sets the user read latency. */
124 /* Note: There are certain rates where we give this up and transfer 1/2 FIFO */
125 /* If this is too low, efficiency is poor */
126 #define TRANS_TARGET_PERIOD 10000000 /* 10 ms (in nanoseconds) */
127 
128 /* Set a practical limit on how long a list to support (affects memory use) */
129 /* The board support a channel list up to the FIFO length (1K or 8K) */
130 #define RTD_MAX_CHANLIST 128 /* max channel list that we allow */
131 
132 /* tuning for ai/ao instruction done polling */
133 #ifdef FAST_SPIN
134 #define WAIT_QUIETLY /* as nothing, spin on done bit */
135 #define RTD_ADC_TIMEOUT 66000 /* 2 msec at 33mhz bus rate */
136 #define RTD_DAC_TIMEOUT 66000
137 #define RTD_DMA_TIMEOUT 33000 /* 1 msec */
138 #else
139 /* by delaying, power and electrical noise are reduced somewhat */
140 #define WAIT_QUIETLY udelay(1)
141 #define RTD_ADC_TIMEOUT 2000 /* in usec */
142 #define RTD_DAC_TIMEOUT 2000 /* in usec */
143 #define RTD_DMA_TIMEOUT 1000 /* in usec */
144 #endif
145 
146 /*======================================================================
147  Board specific stuff
148 ======================================================================*/
149 
150 /* registers */
151 #define PCI_VENDOR_ID_RTD 0x1435
152 /*
153  The board has three memory windows: las0, las1, and lcfg (the PCI chip)
154  Las1 has the data and can be burst DMAed 32bits at a time.
155 */
156 #define LCFG_PCIINDEX 0
157 /* PCI region 1 is a 256 byte IO space mapping. Use??? */
158 #define LAS0_PCIINDEX 2 /* PCI memory resources */
159 #define LAS1_PCIINDEX 3
160 #define LCFG_PCISIZE 0x100
161 #define LAS0_PCISIZE 0x200
162 #define LAS1_PCISIZE 0x10
163 
164 #define RTD_CLOCK_RATE 8000000 /* 8Mhz onboard clock */
165 #define RTD_CLOCK_BASE 125 /* clock period in ns */
166 
167 /* Note: these speed are slower than the spec, but fit the counter resolution*/
168 #define RTD_MAX_SPEED 1625 /* when sampling, in nanoseconds */
169 /* max speed if we don't have to wait for settling */
170 #define RTD_MAX_SPEED_1 875 /* if single channel, in nanoseconds */
171 
172 #define RTD_MIN_SPEED 2097151875 /* (24bit counter) in nanoseconds */
173 /* min speed when only 1 channel (no burst counter) */
174 #define RTD_MIN_SPEED_1 5000000 /* 200Hz, in nanoseconds */
175 
176 #include "rtd520.h"
177 #include "plx9080.h"
178 
179 /* Setup continuous ring of 1/2 FIFO transfers. See RTD manual p91 */
180 #define DMA_MODE_BITS (\
181  PLX_LOCAL_BUS_16_WIDE_BITS \
182  | PLX_DMA_EN_READYIN_BIT \
183  | PLX_DMA_LOCAL_BURST_EN_BIT \
184  | PLX_EN_CHAIN_BIT \
185  | PLX_DMA_INTR_PCI_BIT \
186  | PLX_LOCAL_ADDR_CONST_BIT \
187  | PLX_DEMAND_MODE_BIT)
188 
189 #define DMA_TRANSFER_BITS (\
190 /* descriptors in PCI memory*/ PLX_DESC_IN_PCI_BIT \
191 /* interrupt at end of block */ | PLX_INTR_TERM_COUNT \
192 /* from board to PCI */ | PLX_XFER_LOCAL_TO_PCI)
193 
194 /*======================================================================
195  Comedi specific stuff
196 ======================================================================*/
197 
198 /*
199  * The board has 3 input modes and the gains of 1,2,4,...32 (, 64, 128)
200  */
201 static const struct comedi_lrange rtd_ai_7520_range = {
202  18, {
203  /* +-5V input range gain steps */
204  BIP_RANGE(5.0),
205  BIP_RANGE(5.0 / 2),
206  BIP_RANGE(5.0 / 4),
207  BIP_RANGE(5.0 / 8),
208  BIP_RANGE(5.0 / 16),
209  BIP_RANGE(5.0 / 32),
210  /* +-10V input range gain steps */
211  BIP_RANGE(10.0),
212  BIP_RANGE(10.0 / 2),
213  BIP_RANGE(10.0 / 4),
214  BIP_RANGE(10.0 / 8),
215  BIP_RANGE(10.0 / 16),
216  BIP_RANGE(10.0 / 32),
217  /* +10V input range gain steps */
218  UNI_RANGE(10.0),
219  UNI_RANGE(10.0 / 2),
220  UNI_RANGE(10.0 / 4),
221  UNI_RANGE(10.0 / 8),
222  UNI_RANGE(10.0 / 16),
223  UNI_RANGE(10.0 / 32),
224  }
225 };
226 
227 /* PCI4520 has two more gains (6 more entries) */
228 static const struct comedi_lrange rtd_ai_4520_range = {
229  24, {
230  /* +-5V input range gain steps */
231  BIP_RANGE(5.0),
232  BIP_RANGE(5.0 / 2),
233  BIP_RANGE(5.0 / 4),
234  BIP_RANGE(5.0 / 8),
235  BIP_RANGE(5.0 / 16),
236  BIP_RANGE(5.0 / 32),
237  BIP_RANGE(5.0 / 64),
238  BIP_RANGE(5.0 / 128),
239  /* +-10V input range gain steps */
240  BIP_RANGE(10.0),
241  BIP_RANGE(10.0 / 2),
242  BIP_RANGE(10.0 / 4),
243  BIP_RANGE(10.0 / 8),
244  BIP_RANGE(10.0 / 16),
245  BIP_RANGE(10.0 / 32),
246  BIP_RANGE(10.0 / 64),
247  BIP_RANGE(10.0 / 128),
248  /* +10V input range gain steps */
249  UNI_RANGE(10.0),
250  UNI_RANGE(10.0 / 2),
251  UNI_RANGE(10.0 / 4),
252  UNI_RANGE(10.0 / 8),
253  UNI_RANGE(10.0 / 16),
254  UNI_RANGE(10.0 / 32),
255  UNI_RANGE(10.0 / 64),
256  UNI_RANGE(10.0 / 128),
257  }
258 };
259 
260 /* Table order matches range values */
261 static const struct comedi_lrange rtd_ao_range = {
262  4, {
263  UNI_RANGE(5),
264  UNI_RANGE(10),
265  BIP_RANGE(5),
266  BIP_RANGE(10),
267  }
268 };
269 
270 struct rtdBoard {
271  const char *name;
273  int aiChans;
274  int aiBits;
276  int range10Start; /* start of +-10V range */
277  int rangeUniStart; /* start of +10V range */
278 };
279 
280 static const struct rtdBoard rtd520Boards[] = {
281  {
282  .name = "DM7520",
283  .device_id = 0x7520,
284  .aiChans = 16,
285  .aiBits = 12,
286  .aiMaxGain = 32,
287  .range10Start = 6,
288  .rangeUniStart = 12,
289  }, {
290  .name = "PCI4520",
291  .device_id = 0x4520,
292  .aiChans = 16,
293  .aiBits = 12,
294  .aiMaxGain = 128,
295  .range10Start = 8,
296  .rangeUniStart = 16,
297  },
298 };
299 
300 /*
301  This structure is for data unique to this hardware driver.
302  This is also unique for each board in the system.
303 */
304 struct rtdPrivate {
305  /* memory mapped board structures */
306  void __iomem *las0;
307  void __iomem *las1;
308  void __iomem *lcfg;
309 
310  unsigned long intCount; /* interrupt count */
311  long aiCount; /* total transfer size (samples) */
312  int transCount; /* # to transfer data. 0->1/2FIFO */
313  int flags; /* flag event modes */
314 
315  /* channel list info */
316  /* chanBipolar tracks whether a channel is bipolar (and needs +2048) */
317  unsigned char chanBipolar[RTD_MAX_CHANLIST / 8]; /* bit array */
318 
319  /* read back data */
320  unsigned int aoValue[2]; /* Used for AO read back */
321 
322  /* timer gate (when enabled) */
323  u8 utcGate[4]; /* 1 extra allows simple range check */
324 
325  /* shadow registers affect other registers, but can't be read back */
326  /* The macros below update these on writes */
327  u16 intMask; /* interrupt mask */
328  u16 intClearMask; /* interrupt clear mask */
329  u8 utcCtrl[4]; /* crtl mode for 3 utc + read back */
330  u8 dioStatus; /* could be read back (dio0Ctrl) */
331 #ifdef USE_DMA
332  /*
333  * Always DMA 1/2 FIFO. Buffer (dmaBuff?) is (at least) twice that
334  * size. After transferring, interrupt processes 1/2 FIFO and
335  * passes to comedi
336  */
337  s16 dma0Offset; /* current processing offset (0, 1/2) */
338  uint16_t *dma0Buff[DMA_CHAIN_COUNT]; /* DMA buffers (for ADC) */
339  dma_addr_t dma0BuffPhysAddr[DMA_CHAIN_COUNT]; /* physical addresses */
340  struct plx_dma_desc *dma0Chain; /* DMA descriptor ring for dmaBuff */
341  dma_addr_t dma0ChainPhysAddr; /* physical addresses */
342  /* shadow registers */
343  u8 dma0Control;
344  u8 dma1Control;
345 #endif /* USE_DMA */
346  unsigned fifoLen;
347 };
348 
349 /* bit defines for "flags" */
350 #define SEND_EOS 0x01 /* send End Of Scan events */
351 #define DMA0_ACTIVE 0x02 /* DMA0 is active */
352 #define DMA1_ACTIVE 0x04 /* DMA1 is active */
353 
354 /* Macros for accessing channel list bit array */
355 #define CHAN_ARRAY_TEST(array, index) \
356  (((array)[(index)/8] >> ((index) & 0x7)) & 0x1)
357 #define CHAN_ARRAY_SET(array, index) \
358  (((array)[(index)/8] |= 1 << ((index) & 0x7)))
359 #define CHAN_ARRAY_CLEAR(array, index) \
360  (((array)[(index)/8] &= ~(1 << ((index) & 0x7))))
361 
362 /*
363  Given a desired period and the clock period (both in ns),
364  return the proper counter value (divider-1).
365  Sets the original period to be the true value.
366  Note: you have to check if the value is larger than the counter range!
367 */
368 static int rtd_ns_to_timer_base(unsigned int *nanosec, /* desired period (in ns) */
369  int round_mode, int base)
370 { /* clock period (in ns) */
371  int divider;
372 
373  switch (round_mode) {
374  case TRIG_ROUND_NEAREST:
375  default:
376  divider = (*nanosec + base / 2) / base;
377  break;
378  case TRIG_ROUND_DOWN:
379  divider = (*nanosec) / base;
380  break;
381  case TRIG_ROUND_UP:
382  divider = (*nanosec + base - 1) / base;
383  break;
384  }
385  if (divider < 2)
386  divider = 2; /* min is divide by 2 */
387 
388  /* Note: we don't check for max, because different timers
389  have different ranges */
390 
391  *nanosec = base * divider;
392  return divider - 1; /* countdown is divisor+1 */
393 }
394 
395 /*
396  Given a desired period (in ns),
397  return the proper counter value (divider-1) for the internal clock.
398  Sets the original period to be the true value.
399 */
400 static int rtd_ns_to_timer(unsigned int *ns, int round_mode)
401 {
402  return rtd_ns_to_timer_base(ns, round_mode, RTD_CLOCK_BASE);
403 }
404 
405 /*
406  Convert a single comedi channel-gain entry to a RTD520 table entry
407 */
408 static unsigned short rtdConvertChanGain(struct comedi_device *dev,
409  unsigned int comediChan, int chanIndex)
410 { /* index in channel list */
411  const struct rtdBoard *thisboard = comedi_board(dev);
412  struct rtdPrivate *devpriv = dev->private;
413  unsigned int chan, range, aref;
414  unsigned short r = 0;
415 
416  chan = CR_CHAN(comediChan);
417  range = CR_RANGE(comediChan);
418  aref = CR_AREF(comediChan);
419 
420  r |= chan & 0xf;
421 
422  /* Note: we also setup the channel list bipolar flag array */
423  if (range < thisboard->range10Start) { /* first batch are +-5 */
424  r |= 0x000; /* +-5 range */
425  r |= (range & 0x7) << 4; /* gain */
426  CHAN_ARRAY_SET(devpriv->chanBipolar, chanIndex);
427  } else if (range < thisboard->rangeUniStart) { /* second batch are +-10 */
428  r |= 0x100; /* +-10 range */
429  /* gain */
430  r |= ((range - thisboard->range10Start) & 0x7) << 4;
431  CHAN_ARRAY_SET(devpriv->chanBipolar, chanIndex);
432  } else { /* last batch is +10 */
433  r |= 0x200; /* +10 range */
434  /* gain */
435  r |= ((range - thisboard->rangeUniStart) & 0x7) << 4;
436  CHAN_ARRAY_CLEAR(devpriv->chanBipolar, chanIndex);
437  }
438 
439  switch (aref) {
440  case AREF_GROUND: /* on-board ground */
441  break;
442 
443  case AREF_COMMON:
444  r |= 0x80; /* ref external analog common */
445  break;
446 
447  case AREF_DIFF:
448  r |= 0x400; /* differential inputs */
449  break;
450 
451  case AREF_OTHER: /* ??? */
452  break;
453  }
454  /*printk ("chan=%d r=%d a=%d -> 0x%x\n",
455  chan, range, aref, r); */
456  return r;
457 }
458 
459 /*
460  Setup the channel-gain table from a comedi list
461 */
462 static void rtd_load_channelgain_list(struct comedi_device *dev,
463  unsigned int n_chan, unsigned int *list)
464 {
465  struct rtdPrivate *devpriv = dev->private;
466 
467  if (n_chan > 1) { /* setup channel gain table */
468  int ii;
469 
470  writel(0, devpriv->las0 + LAS0_CGT_CLEAR);
471  writel(1, devpriv->las0 + LAS0_CGT_ENABLE);
472  for (ii = 0; ii < n_chan; ii++) {
473  writel(rtdConvertChanGain(dev, list[ii], ii),
474  devpriv->las0 + LAS0_CGT_WRITE);
475  }
476  } else { /* just use the channel gain latch */
477  writel(0, devpriv->las0 + LAS0_CGT_ENABLE);
478  writel(rtdConvertChanGain(dev, list[0], 0),
479  devpriv->las0 + LAS0_CGL_WRITE);
480  }
481 }
482 
483 /* determine fifo size by doing adc conversions until the fifo half
484 empty status flag clears */
485 static int rtd520_probe_fifo_depth(struct comedi_device *dev)
486 {
487  struct rtdPrivate *devpriv = dev->private;
488  unsigned int chanspec = CR_PACK(0, 0, AREF_GROUND);
489  unsigned i;
490  static const unsigned limit = 0x2000;
491  unsigned fifo_size = 0;
492 
493  writel(0, devpriv->las0 + LAS0_ADC_FIFO_CLEAR);
494  rtd_load_channelgain_list(dev, 1, &chanspec);
495  /* ADC conversion trigger source: SOFTWARE */
496  writel(0, devpriv->las0 + LAS0_ADC_CONVERSION);
497  /* convert samples */
498  for (i = 0; i < limit; ++i) {
499  unsigned fifo_status;
500  /* trigger conversion */
501  writew(0, devpriv->las0 + LAS0_ADC);
502  udelay(1);
503  fifo_status = readl(devpriv->las0 + LAS0_ADC);
504  if ((fifo_status & FS_ADC_HEMPTY) == 0) {
505  fifo_size = 2 * i;
506  break;
507  }
508  }
509  if (i == limit) {
510  printk(KERN_INFO "\ncomedi: %s: failed to probe fifo size.\n",
511  DRV_NAME);
512  return -EIO;
513  }
514  writel(0, devpriv->las0 + LAS0_ADC_FIFO_CLEAR);
515  if (fifo_size != 0x400 && fifo_size != 0x2000) {
516  printk
517  (KERN_INFO "\ncomedi: %s: unexpected fifo size of %i, expected 1024 or 8192.\n",
518  DRV_NAME, fifo_size);
519  return -EIO;
520  }
521  return fifo_size;
522 }
523 
524 /*
525  "instructions" read/write data in "one-shot" or "software-triggered"
526  mode (simplest case).
527  This doesn't use interrupts.
528 
529  Note, we don't do any settling delays. Use a instruction list to
530  select, delay, then read.
531  */
532 static int rtd_ai_rinsn(struct comedi_device *dev,
533  struct comedi_subdevice *s, struct comedi_insn *insn,
534  unsigned int *data)
535 {
536  struct rtdPrivate *devpriv = dev->private;
537  int n, ii;
538  int stat;
539 
540  /* clear any old fifo data */
541  writel(0, devpriv->las0 + LAS0_ADC_FIFO_CLEAR);
542 
543  /* write channel to multiplexer and clear channel gain table */
544  rtd_load_channelgain_list(dev, 1, &insn->chanspec);
545 
546  /* ADC conversion trigger source: SOFTWARE */
547  writel(0, devpriv->las0 + LAS0_ADC_CONVERSION);
548 
549  /* convert n samples */
550  for (n = 0; n < insn->n; n++) {
551  s16 d;
552  /* trigger conversion */
553  writew(0, devpriv->las0 + LAS0_ADC);
554 
555  for (ii = 0; ii < RTD_ADC_TIMEOUT; ++ii) {
556  stat = readl(devpriv->las0 + LAS0_ADC);
557  if (stat & FS_ADC_NOT_EMPTY) /* 1 -> not empty */
558  break;
559  WAIT_QUIETLY;
560  }
561  if (ii >= RTD_ADC_TIMEOUT) {
562  DPRINTK
563  ("rtd520: Error: ADC never finished! FifoStatus=0x%x\n",
564  stat ^ 0x6666);
565  return -ETIMEDOUT;
566  }
567 
568  /* read data */
569  d = readw(devpriv->las1 + LAS1_ADC_FIFO);
570  /*printk ("rtd520: Got 0x%x after %d usec\n", d, ii+1); */
571  d = d >> 3; /* low 3 bits are marker lines */
572  if (CHAN_ARRAY_TEST(devpriv->chanBipolar, 0))
573  /* convert to comedi unsigned data */
574  data[n] = d + 2048;
575  else
576  data[n] = d;
577  }
578 
579  /* return the number of samples read/written */
580  return n;
581 }
582 
583 /*
584  Get what we know is there.... Fast!
585  This uses 1/2 the bus cycles of read_dregs (below).
586 
587  The manual claims that we can do a lword read, but it doesn't work here.
588 */
589 static int ai_read_n(struct comedi_device *dev, struct comedi_subdevice *s,
590  int count)
591 {
592  struct rtdPrivate *devpriv = dev->private;
593  int ii;
594 
595  for (ii = 0; ii < count; ii++) {
596  short sample;
597  s16 d;
598 
599  if (0 == devpriv->aiCount) { /* done */
600  d = readw(devpriv->las1 + LAS1_ADC_FIFO);
601  continue;
602  }
603 #if 0
604  if (!(readl(devpriv->las0 + LAS0_ADC) & FS_ADC_NOT_EMPTY)) {
605  DPRINTK("comedi: READ OOPS on %d of %d\n", ii + 1,
606  count);
607  break;
608  }
609 #endif
610  d = readw(devpriv->las1 + LAS1_ADC_FIFO);
611 
612  d = d >> 3; /* low 3 bits are marker lines */
613  if (CHAN_ARRAY_TEST(devpriv->chanBipolar, s->async->cur_chan)) {
614  /* convert to comedi unsigned data */
615  sample = d + 2048;
616  } else
617  sample = d;
618 
619  if (!comedi_buf_put(s->async, sample))
620  return -1;
621 
622  if (devpriv->aiCount > 0) /* < 0, means read forever */
623  devpriv->aiCount--;
624  }
625  return 0;
626 }
627 
628 /*
629  unknown amout of data is waiting in fifo.
630 */
631 static int ai_read_dregs(struct comedi_device *dev, struct comedi_subdevice *s)
632 {
633  struct rtdPrivate *devpriv = dev->private;
634 
635  while (readl(devpriv->las0 + LAS0_ADC) & FS_ADC_NOT_EMPTY) {
636  short sample;
637  s16 d = readw(devpriv->las1 + LAS1_ADC_FIFO);
638 
639  if (0 == devpriv->aiCount) { /* done */
640  continue; /* read rest */
641  }
642 
643  d = d >> 3; /* low 3 bits are marker lines */
644  if (CHAN_ARRAY_TEST(devpriv->chanBipolar, s->async->cur_chan)) {
645  /* convert to comedi unsigned data */
646  sample = d + 2048;
647  } else
648  sample = d;
649 
650  if (!comedi_buf_put(s->async, sample))
651  return -1;
652 
653  if (devpriv->aiCount > 0) /* < 0, means read forever */
654  devpriv->aiCount--;
655  }
656  return 0;
657 }
658 
659 #ifdef USE_DMA
660 /*
661  Terminate a DMA transfer and wait for everything to quiet down
662 */
663 void abort_dma(struct comedi_device *dev, unsigned int channel)
664 { /* DMA channel 0, 1 */
665  struct rtdPrivate *devpriv = dev->private;
666  unsigned long dma_cs_addr; /* the control/status register */
667  uint8_t status;
668  unsigned int ii;
669  /* unsigned long flags; */
670 
671  dma_cs_addr = (unsigned long)devpriv->lcfg
672  + ((channel == 0) ? LCFG_DMACSR0 : LCFG_DMACSR1);
673 
674  /* spinlock for plx dma control/status reg */
675  /* spin_lock_irqsave( &dev->spinlock, flags ); */
676 
677  /* abort dma transfer if necessary */
678  status = readb(dma_cs_addr);
679  if ((status & PLX_DMA_EN_BIT) == 0) { /* not enabled (Error?) */
680  DPRINTK("rtd520: AbortDma on non-active channel %d (0x%x)\n",
681  channel, status);
682  goto abortDmaExit;
683  }
684 
685  /* wait to make sure done bit is zero (needed?) */
686  for (ii = 0; (status & PLX_DMA_DONE_BIT) && ii < RTD_DMA_TIMEOUT; ii++) {
687  WAIT_QUIETLY;
688  status = readb(dma_cs_addr);
689  }
690  if (status & PLX_DMA_DONE_BIT) {
691  printk("rtd520: Timeout waiting for dma %i done clear\n",
692  channel);
693  goto abortDmaExit;
694  }
695 
696  /* disable channel (required) */
697  writeb(0, dma_cs_addr);
698  udelay(1); /* needed?? */
699  /* set abort bit for channel */
700  writeb(PLX_DMA_ABORT_BIT, dma_cs_addr);
701 
702  /* wait for dma done bit to be set */
703  status = readb(dma_cs_addr);
704  for (ii = 0;
705  (status & PLX_DMA_DONE_BIT) == 0 && ii < RTD_DMA_TIMEOUT; ii++) {
706  status = readb(dma_cs_addr);
707  WAIT_QUIETLY;
708  }
709  if ((status & PLX_DMA_DONE_BIT) == 0) {
710  printk("rtd520: Timeout waiting for dma %i done set\n",
711  channel);
712  }
713 
714 abortDmaExit:
715  /* spin_unlock_irqrestore( &dev->spinlock, flags ); */
716 }
717 
718 /*
719  Process what is in the DMA transfer buffer and pass to comedi
720  Note: this is not re-entrant
721 */
722 static int ai_process_dma(struct comedi_device *dev, struct comedi_subdevice *s)
723 {
724  struct rtdPrivate *devpriv = dev->private;
725  int ii, n;
726  s16 *dp;
727 
728  if (devpriv->aiCount == 0) /* transfer already complete */
729  return 0;
730 
731  dp = devpriv->dma0Buff[devpriv->dma0Offset];
732  for (ii = 0; ii < devpriv->fifoLen / 2;) { /* convert samples */
733  short sample;
734 
735  if (CHAN_ARRAY_TEST(devpriv->chanBipolar, s->async->cur_chan)) {
736  sample = (*dp >> 3) + 2048; /* convert to comedi unsigned data */
737  else
738  sample = *dp >> 3; /* low 3 bits are marker lines */
739 
740  *dp++ = sample; /* put processed value back */
741 
742  if (++s->async->cur_chan >= s->async->cmd.chanlist_len)
743  s->async->cur_chan = 0;
744 
745  ++ii; /* number ready to transfer */
746  if (devpriv->aiCount > 0) { /* < 0, means read forever */
747  if (--devpriv->aiCount == 0) { /* done */
748  /*DPRINTK ("rtd520: Final %d samples\n", ii); */
749  break;
750  }
751  }
752  }
753 
754  /* now pass the whole array to the comedi buffer */
755  dp = devpriv->dma0Buff[devpriv->dma0Offset];
756  n = comedi_buf_write_alloc(s->async, ii * sizeof(s16));
757  if (n < (ii * sizeof(s16))) { /* any residual is an error */
758  DPRINTK("rtd520:ai_process_dma buffer overflow %d samples!\n",
759  ii - (n / sizeof(s16)));
760  s->async->events |= COMEDI_CB_ERROR;
761  return -1;
762  }
763  comedi_buf_memcpy_to(s->async, 0, dp, n);
765 
766  /*
767  * always at least 1 scan -- 1/2 FIFO is larger than our max scan list
768  */
769  s->async->events |= COMEDI_CB_BLOCK | COMEDI_CB_EOS;
770 
771  if (++devpriv->dma0Offset >= DMA_CHAIN_COUNT) { /* next buffer */
772  devpriv->dma0Offset = 0;
773  }
774  return 0;
775 }
776 #endif /* USE_DMA */
777 
778 /*
779  Handle all rtd520 interrupts.
780  Runs atomically and is never re-entered.
781  This is a "slow handler"; other interrupts may be active.
782  The data conversion may someday happen in a "bottom half".
783 */
784 static irqreturn_t rtd_interrupt(int irq, /* interrupt number (ignored) */
785  void *d)
786 { /* our data *//* cpu context (ignored) */
787  struct comedi_device *dev = d;
788  struct comedi_subdevice *s = &dev->subdevices[0];
789  struct rtdPrivate *devpriv = dev->private;
790  u32 overrun;
791  u16 status;
792  u16 fifoStatus;
793 
794  if (!dev->attached)
795  return IRQ_NONE;
796 
797  devpriv->intCount++; /* DEBUG statistics */
798 
799  fifoStatus = readl(devpriv->las0 + LAS0_ADC);
800  /* check for FIFO full, this automatically halts the ADC! */
801  if (!(fifoStatus & FS_ADC_NOT_FULL)) { /* 0 -> full */
802  DPRINTK("rtd520: FIFO full! fifo_status=0x%x\n", (fifoStatus ^ 0x6666) & 0x7777); /* should be all 0s */
803  goto abortTransfer;
804  }
805 #ifdef USE_DMA
806  if (devpriv->flags & DMA0_ACTIVE) { /* Check DMA */
807  u32 istatus = readl(devpriv->lcfg + LCFG_ITCSR);
808 
809  if (istatus & ICS_DMA0_A) {
810  if (ai_process_dma(dev, s) < 0) {
811  DPRINTK
812  ("rtd520: comedi read buffer overflow (DMA) with %ld to go!\n",
813  devpriv->aiCount);
814  devpriv->dma0Control &= ~PLX_DMA_START_BIT;
815  devpriv->dma0Control |= PLX_CLEAR_DMA_INTR_BIT;
816  writeb(devpriv->dma0Control,
817  devpriv->lcfg + LCFG_DMACSR0);
818  goto abortTransfer;
819  }
820 
821  /*DPRINTK ("rtd520: DMA transfer: %ld to go, istatus %x\n",
822  devpriv->aiCount, istatus); */
823  devpriv->dma0Control &= ~PLX_DMA_START_BIT;
824  devpriv->dma0Control |= PLX_CLEAR_DMA_INTR_BIT;
825  writeb(devpriv->dma0Control,
826  devpriv->lcfg + LCFG_DMACSR0);
827  if (0 == devpriv->aiCount) { /* counted down */
828  DPRINTK("rtd520: Samples Done (DMA).\n");
829  goto transferDone;
830  }
831  comedi_event(dev, s);
832  } else {
833  /*DPRINTK ("rtd520: No DMA ready: istatus %x\n", istatus); */
834  }
835  }
836  /* Fall through and check for other interrupt sources */
837 #endif /* USE_DMA */
838 
839  status = readw(devpriv->las0 + LAS0_IT);
840  /* if interrupt was not caused by our board, or handled above */
841  if (0 == status)
842  return IRQ_HANDLED;
843 
844  if (status & IRQM_ADC_ABOUT_CNT) { /* sample count -> read FIFO */
845  /* since the priority interrupt controller may have queued a sample
846  counter interrupt, even though we have already finished,
847  we must handle the possibility that there is no data here */
848  if (!(fifoStatus & FS_ADC_HEMPTY)) { /* 0 -> 1/2 full */
849  /*DPRINTK("rtd520: Sample int, reading 1/2FIFO. fifo_status 0x%x\n",
850  (fifoStatus ^ 0x6666) & 0x7777); */
851  if (ai_read_n(dev, s, devpriv->fifoLen / 2) < 0) {
852  DPRINTK
853  ("rtd520: comedi read buffer overflow (1/2FIFO) with %ld to go!\n",
854  devpriv->aiCount);
855  goto abortTransfer;
856  }
857  if (0 == devpriv->aiCount) { /* counted down */
858  DPRINTK("rtd520: Samples Done (1/2). fifo_status was 0x%x\n", (fifoStatus ^ 0x6666) & 0x7777); /* should be all 0s */
859  goto transferDone;
860  }
861  comedi_event(dev, s);
862  } else if (devpriv->transCount > 0) { /* read often */
863  /*DPRINTK("rtd520: Sample int, reading %d fifo_status 0x%x\n",
864  devpriv->transCount, (fifoStatus ^ 0x6666) & 0x7777); */
865  if (fifoStatus & FS_ADC_NOT_EMPTY) { /* 1 -> not empty */
866  if (ai_read_n(dev, s, devpriv->transCount) < 0) {
867  DPRINTK
868  ("rtd520: comedi read buffer overflow (N) with %ld to go!\n",
869  devpriv->aiCount);
870  goto abortTransfer;
871  }
872  if (0 == devpriv->aiCount) { /* counted down */
873  DPRINTK
874  ("rtd520: Samples Done (N). fifo_status was 0x%x\n",
875  (fifoStatus ^ 0x6666) & 0x7777);
876  goto transferDone;
877  }
878  comedi_event(dev, s);
879  }
880  } else { /* wait for 1/2 FIFO (old) */
881  DPRINTK
882  ("rtd520: Sample int. Wait for 1/2. fifo_status 0x%x\n",
883  (fifoStatus ^ 0x6666) & 0x7777);
884  }
885  } else {
886  DPRINTK("rtd520: unknown interrupt source!\n");
887  }
888 
889  overrun = readl(devpriv->las0 + LAS0_OVERRUN) & 0xffff;
890  if (overrun) {
891  DPRINTK
892  ("rtd520: Interrupt overrun with %ld to go! over_status=0x%x\n",
893  devpriv->aiCount, overrun);
894  goto abortTransfer;
895  }
896 
897  /* clear the interrupt */
898  devpriv->intClearMask = status;
899  writew(devpriv->intClearMask, devpriv->las0 + LAS0_CLEAR);
900  readw(devpriv->las0 + LAS0_CLEAR);
901  return IRQ_HANDLED;
902 
903 abortTransfer:
904  writel(0, devpriv->las0 + LAS0_ADC_FIFO_CLEAR);
905  s->async->events |= COMEDI_CB_ERROR;
906  devpriv->aiCount = 0; /* stop and don't transfer any more */
907  /* fall into transferDone */
908 
909 transferDone:
910  /* pacer stop source: SOFTWARE */
911  writel(0, devpriv->las0 + LAS0_PACER_STOP);
912  writel(0, devpriv->las0 + LAS0_PACER); /* stop pacer */
913  writel(0, devpriv->las0 + LAS0_ADC_CONVERSION);
914  devpriv->intMask = 0;
915  writew(devpriv->intMask, devpriv->las0 + LAS0_IT);
916 #ifdef USE_DMA
917  if (devpriv->flags & DMA0_ACTIVE) {
918  writel(readl(devpriv->lcfg + LCFG_ITCSR) & ~ICS_DMA0_E,
919  devpriv->lcfg + LCFG_ITCSR);
920  abort_dma(dev, 0);
921  devpriv->flags &= ~DMA0_ACTIVE;
922  /* if Using DMA, then we should have read everything by now */
923  if (devpriv->aiCount > 0) {
924  DPRINTK("rtd520: Lost DMA data! %ld remain\n",
925  devpriv->aiCount);
926  }
927  }
928 #endif /* USE_DMA */
929 
930  if (devpriv->aiCount > 0) { /* there shouldn't be anything left */
931  fifoStatus = readl(devpriv->las0 + LAS0_ADC);
932  DPRINTK("rtd520: Finishing up. %ld remain, fifoStat=%x\n", devpriv->aiCount, (fifoStatus ^ 0x6666) & 0x7777); /* should read all 0s */
933  ai_read_dregs(dev, s); /* read anything left in FIFO */
934  }
935 
936  s->async->events |= COMEDI_CB_EOA; /* signal end to comedi */
937  comedi_event(dev, s);
938 
939  /* clear the interrupt */
940  status = readw(devpriv->las0 + LAS0_IT);
941  devpriv->intClearMask = status;
942  writew(devpriv->intClearMask, devpriv->las0 + LAS0_CLEAR);
943  readw(devpriv->las0 + LAS0_CLEAR);
944 
945  fifoStatus = readl(devpriv->las0 + LAS0_ADC);
946  overrun = readl(devpriv->las0 + LAS0_OVERRUN) & 0xffff;
947  DPRINTK
948  ("rtd520: Acquisition complete. %ld ints, intStat=%x, overStat=%x\n",
949  devpriv->intCount, status, overrun);
950 
951  return IRQ_HANDLED;
952 }
953 
954 #if 0
955 /*
956  return the number of samples available
957 */
958 static int rtd_ai_poll(struct comedi_device *dev, struct comedi_subdevice *s)
959 {
960  /* TODO: This needs to mask interrupts, read_dregs, and then re-enable */
961  /* Not sure what to do if DMA is active */
962  return s->async->buf_write_count - s->async->buf_read_count;
963 }
964 #endif
965 
966 /*
967  cmdtest tests a particular command to see if it is valid.
968  Using the cmdtest ioctl, a user can create a valid cmd
969  and then have it executed by the cmd ioctl (asynchronously).
970 
971  cmdtest returns 1,2,3,4 or 0, depending on which tests
972  the command passes.
973 */
974 
975 static int rtd_ai_cmdtest(struct comedi_device *dev,
976  struct comedi_subdevice *s, struct comedi_cmd *cmd)
977 {
978  int err = 0;
979  int tmp;
980 
981  /* Step 1 : check if triggers are trivially valid */
982 
983  err |= cfc_check_trigger_src(&cmd->start_src, TRIG_NOW);
984  err |= cfc_check_trigger_src(&cmd->scan_begin_src,
985  TRIG_TIMER | TRIG_EXT);
986  err |= cfc_check_trigger_src(&cmd->convert_src, TRIG_TIMER | TRIG_EXT);
987  err |= cfc_check_trigger_src(&cmd->scan_end_src, TRIG_COUNT);
988  err |= cfc_check_trigger_src(&cmd->stop_src, TRIG_COUNT | TRIG_NONE);
989 
990  if (err)
991  return 1;
992 
993  /* Step 2a : make sure trigger sources are unique */
994 
995  err |= cfc_check_trigger_is_unique(cmd->scan_begin_src);
996  err |= cfc_check_trigger_is_unique(cmd->convert_src);
997  err |= cfc_check_trigger_is_unique(cmd->stop_src);
998 
999  /* Step 2b : and mutually compatible */
1000 
1001  if (err)
1002  return 2;
1003 
1004  /* step 3: make sure arguments are trivially compatible */
1005 
1006  if (cmd->start_arg != 0) {
1007  cmd->start_arg = 0;
1008  err++;
1009  }
1010 
1011  if (cmd->scan_begin_src == TRIG_TIMER) {
1012  /* Note: these are time periods, not actual rates */
1013  if (1 == cmd->chanlist_len) { /* no scanning */
1014  if (cmd->scan_begin_arg < RTD_MAX_SPEED_1) {
1016  rtd_ns_to_timer(&cmd->scan_begin_arg,
1017  TRIG_ROUND_UP);
1018  err++;
1019  }
1020  if (cmd->scan_begin_arg > RTD_MIN_SPEED_1) {
1022  rtd_ns_to_timer(&cmd->scan_begin_arg,
1023  TRIG_ROUND_DOWN);
1024  err++;
1025  }
1026  } else {
1027  if (cmd->scan_begin_arg < RTD_MAX_SPEED) {
1029  rtd_ns_to_timer(&cmd->scan_begin_arg,
1030  TRIG_ROUND_UP);
1031  err++;
1032  }
1033  if (cmd->scan_begin_arg > RTD_MIN_SPEED) {
1035  rtd_ns_to_timer(&cmd->scan_begin_arg,
1036  TRIG_ROUND_DOWN);
1037  err++;
1038  }
1039  }
1040  } else {
1041  /* external trigger */
1042  /* should be level/edge, hi/lo specification here */
1043  /* should specify multiple external triggers */
1044  if (cmd->scan_begin_arg > 9) {
1045  cmd->scan_begin_arg = 9;
1046  err++;
1047  }
1048  }
1049  if (cmd->convert_src == TRIG_TIMER) {
1050  if (1 == cmd->chanlist_len) { /* no scanning */
1051  if (cmd->convert_arg < RTD_MAX_SPEED_1) {
1053  rtd_ns_to_timer(&cmd->convert_arg,
1054  TRIG_ROUND_UP);
1055  err++;
1056  }
1057  if (cmd->convert_arg > RTD_MIN_SPEED_1) {
1059  rtd_ns_to_timer(&cmd->convert_arg,
1060  TRIG_ROUND_DOWN);
1061  err++;
1062  }
1063  } else {
1064  if (cmd->convert_arg < RTD_MAX_SPEED) {
1065  cmd->convert_arg = RTD_MAX_SPEED;
1066  rtd_ns_to_timer(&cmd->convert_arg,
1067  TRIG_ROUND_UP);
1068  err++;
1069  }
1070  if (cmd->convert_arg > RTD_MIN_SPEED) {
1071  cmd->convert_arg = RTD_MIN_SPEED;
1072  rtd_ns_to_timer(&cmd->convert_arg,
1073  TRIG_ROUND_DOWN);
1074  err++;
1075  }
1076  }
1077  } else {
1078  /* external trigger */
1079  /* see above */
1080  if (cmd->convert_arg > 9) {
1081  cmd->convert_arg = 9;
1082  err++;
1083  }
1084  }
1085 
1086 #if 0
1087  if (cmd->scan_end_arg != cmd->chanlist_len) {
1088  cmd->scan_end_arg = cmd->chanlist_len;
1089  err++;
1090  }
1091 #endif
1092  if (cmd->stop_src == TRIG_COUNT) {
1093  /* TODO check for rounding error due to counter wrap */
1094 
1095  } else {
1096  /* TRIG_NONE */
1097  if (cmd->stop_arg != 0) {
1098  cmd->stop_arg = 0;
1099  err++;
1100  }
1101  }
1102 
1103  if (err)
1104  return 3;
1105 
1106 
1107  /* step 4: fix up any arguments */
1108 
1109  if (cmd->chanlist_len > RTD_MAX_CHANLIST) {
1111  err++;
1112  }
1113  if (cmd->scan_begin_src == TRIG_TIMER) {
1114  tmp = cmd->scan_begin_arg;
1115  rtd_ns_to_timer(&cmd->scan_begin_arg,
1116  cmd->flags & TRIG_ROUND_MASK);
1117  if (tmp != cmd->scan_begin_arg)
1118  err++;
1119 
1120  }
1121  if (cmd->convert_src == TRIG_TIMER) {
1122  tmp = cmd->convert_arg;
1123  rtd_ns_to_timer(&cmd->convert_arg,
1124  cmd->flags & TRIG_ROUND_MASK);
1125  if (tmp != cmd->convert_arg)
1126  err++;
1127 
1128  if (cmd->scan_begin_src == TRIG_TIMER
1129  && (cmd->scan_begin_arg
1130  < (cmd->convert_arg * cmd->scan_end_arg))) {
1131  cmd->scan_begin_arg =
1132  cmd->convert_arg * cmd->scan_end_arg;
1133  err++;
1134  }
1135  }
1136 
1137  if (err)
1138  return 4;
1139 
1140  return 0;
1141 }
1142 
1143 /*
1144  Execute a analog in command with many possible triggering options.
1145  The data get stored in the async structure of the subdevice.
1146  This is usually done by an interrupt handler.
1147  Userland gets to the data using read calls.
1148 */
1149 static int rtd_ai_cmd(struct comedi_device *dev, struct comedi_subdevice *s)
1150 {
1151  struct rtdPrivate *devpriv = dev->private;
1152  struct comedi_cmd *cmd = &s->async->cmd;
1153  int timer;
1154 
1155  /* stop anything currently running */
1156  /* pacer stop source: SOFTWARE */
1157  writel(0, devpriv->las0 + LAS0_PACER_STOP);
1158  writel(0, devpriv->las0 + LAS0_PACER); /* stop pacer */
1159  writel(0, devpriv->las0 + LAS0_ADC_CONVERSION);
1160  devpriv->intMask = 0;
1161  writew(devpriv->intMask, devpriv->las0 + LAS0_IT);
1162 #ifdef USE_DMA
1163  if (devpriv->flags & DMA0_ACTIVE) { /* cancel anything running */
1164  writel(readl(devpriv->lcfg + LCFG_ITCSR) & ~ICS_DMA0_E,
1165  devpriv->lcfg + LCFG_ITCSR);
1166  abort_dma(dev, 0);
1167  devpriv->flags &= ~DMA0_ACTIVE;
1168  if (readl(devpriv->lcfg + LCFG_ITCSR) & ICS_DMA0_A) {
1169  devpriv->dma0Control = PLX_CLEAR_DMA_INTR_BIT;
1170  writeb(devpriv->dma0Control,
1171  devpriv->lcfg + LCFG_DMACSR0);
1172  }
1173  }
1174  writel(0, devpriv->las0 + LAS0_DMA0_RESET);
1175 #endif /* USE_DMA */
1176  writel(0, devpriv->las0 + LAS0_ADC_FIFO_CLEAR);
1177  writel(0, devpriv->las0 + LAS0_OVERRUN);
1178  devpriv->intCount = 0;
1179 
1180  if (!dev->irq) { /* we need interrupts for this */
1181  DPRINTK("rtd520: ERROR! No interrupt available!\n");
1182  return -ENXIO;
1183  }
1184 
1185  /* start configuration */
1186  /* load channel list and reset CGT */
1187  rtd_load_channelgain_list(dev, cmd->chanlist_len, cmd->chanlist);
1188 
1189  /* setup the common case and override if needed */
1190  if (cmd->chanlist_len > 1) {
1191  /*DPRINTK ("rtd520: Multi channel setup\n"); */
1192  /* pacer start source: SOFTWARE */
1193  writel(0, devpriv->las0 + LAS0_PACER_START);
1194  /* burst trigger source: PACER */
1195  writel(1, devpriv->las0 + LAS0_BURST_START);
1196  /* ADC conversion trigger source: BURST */
1197  writel(2, devpriv->las0 + LAS0_ADC_CONVERSION);
1198  } else { /* single channel */
1199  /*DPRINTK ("rtd520: single channel setup\n"); */
1200  /* pacer start source: SOFTWARE */
1201  writel(0, devpriv->las0 + LAS0_PACER_START);
1202  /* ADC conversion trigger source: PACER */
1203  writel(1, devpriv->las0 + LAS0_ADC_CONVERSION);
1204  }
1205  writel((devpriv->fifoLen / 2 - 1) & 0xffff, devpriv->las0 + LAS0_ACNT);
1206 
1207  if (TRIG_TIMER == cmd->scan_begin_src) {
1208  /* scan_begin_arg is in nanoseconds */
1209  /* find out how many samples to wait before transferring */
1210  if (cmd->flags & TRIG_WAKE_EOS) {
1211  /* this may generate un-sustainable interrupt rates */
1212  /* the application is responsible for doing the right thing */
1213  devpriv->transCount = cmd->chanlist_len;
1214  devpriv->flags |= SEND_EOS;
1215  } else {
1216  /* arrange to transfer data periodically */
1217  devpriv->transCount
1218  =
1220  cmd->scan_begin_arg;
1221  if (devpriv->transCount < cmd->chanlist_len) {
1222  /* transfer after each scan (and avoid 0) */
1223  devpriv->transCount = cmd->chanlist_len;
1224  } else { /* make a multiple of scan length */
1225  devpriv->transCount =
1226  (devpriv->transCount +
1227  cmd->chanlist_len - 1)
1228  / cmd->chanlist_len;
1229  devpriv->transCount *= cmd->chanlist_len;
1230  }
1231  devpriv->flags |= SEND_EOS;
1232  }
1233  if (devpriv->transCount >= (devpriv->fifoLen / 2)) {
1234  /* out of counter range, use 1/2 fifo instead */
1235  devpriv->transCount = 0;
1236  devpriv->flags &= ~SEND_EOS;
1237  } else {
1238  /* interrupt for each transfer */
1239  writel((devpriv->transCount - 1) & 0xffff,
1240  devpriv->las0 + LAS0_ACNT);
1241  }
1242 
1243  DPRINTK
1244  ("rtd520: scanLen=%d transferCount=%d fifoLen=%d\n scanTime(ns)=%d flags=0x%x\n",
1245  cmd->chanlist_len, devpriv->transCount, devpriv->fifoLen,
1246  cmd->scan_begin_arg, devpriv->flags);
1247  } else { /* unknown timing, just use 1/2 FIFO */
1248  devpriv->transCount = 0;
1249  devpriv->flags &= ~SEND_EOS;
1250  }
1251  /* pacer clock source: INTERNAL 8MHz */
1252  writel(1, devpriv->las0 + LAS0_PACER_SELECT);
1253  /* just interrupt, don't stop */
1254  writel(1, devpriv->las0 + LAS0_ACNT_STOP_ENABLE);
1255 
1256  /* BUG??? these look like enumerated values, but they are bit fields */
1257 
1258  /* First, setup when to stop */
1259  switch (cmd->stop_src) {
1260  case TRIG_COUNT: /* stop after N scans */
1261  devpriv->aiCount = cmd->stop_arg * cmd->chanlist_len;
1262  if ((devpriv->transCount > 0)
1263  && (devpriv->transCount > devpriv->aiCount)) {
1264  devpriv->transCount = devpriv->aiCount;
1265  }
1266  break;
1267 
1268  case TRIG_NONE: /* stop when cancel is called */
1269  devpriv->aiCount = -1; /* read forever */
1270  break;
1271 
1272  default:
1273  DPRINTK("rtd520: Warning! ignoring stop_src mode %d\n",
1274  cmd->stop_src);
1275  }
1276 
1277  /* Scan timing */
1278  switch (cmd->scan_begin_src) {
1279  case TRIG_TIMER: /* periodic scanning */
1280  timer = rtd_ns_to_timer(&cmd->scan_begin_arg,
1282  /* set PACER clock */
1283  /*DPRINTK ("rtd520: loading %d into pacer\n", timer); */
1284  writel(timer & 0xffffff, devpriv->las0 + LAS0_PCLK);
1285 
1286  break;
1287 
1288  case TRIG_EXT:
1289  /* pacer start source: EXTERNAL */
1290  writel(1, devpriv->las0 + LAS0_PACER_START);
1291  break;
1292 
1293  default:
1294  DPRINTK("rtd520: Warning! ignoring scan_begin_src mode %d\n",
1295  cmd->scan_begin_src);
1296  }
1297 
1298  /* Sample timing within a scan */
1299  switch (cmd->convert_src) {
1300  case TRIG_TIMER: /* periodic */
1301  if (cmd->chanlist_len > 1) { /* only needed for multi-channel */
1302  timer = rtd_ns_to_timer(&cmd->convert_arg,
1304  /* setup BURST clock */
1305  /*DPRINTK ("rtd520: loading %d into burst\n", timer); */
1306  writel(timer & 0x3ff, devpriv->las0 + LAS0_BCLK);
1307  }
1308 
1309  break;
1310 
1311  case TRIG_EXT: /* external */
1312  /* burst trigger source: EXTERNAL */
1313  writel(2, devpriv->las0 + LAS0_BURST_START);
1314  break;
1315 
1316  default:
1317  DPRINTK("rtd520: Warning! ignoring convert_src mode %d\n",
1318  cmd->convert_src);
1319  }
1320  /* end configuration */
1321 
1322  /* This doesn't seem to work. There is no way to clear an interrupt
1323  that the priority controller has queued! */
1324  devpriv->intClearMask = ~0;
1325  writew(devpriv->intClearMask, devpriv->las0 + LAS0_CLEAR);
1326  readw(devpriv->las0 + LAS0_CLEAR);
1327 
1328  /* TODO: allow multiple interrupt sources */
1329  if (devpriv->transCount > 0) { /* transfer every N samples */
1330  devpriv->intMask = IRQM_ADC_ABOUT_CNT;
1331  writew(devpriv->intMask, devpriv->las0 + LAS0_IT);
1332  DPRINTK("rtd520: Transferring every %d\n", devpriv->transCount);
1333  } else { /* 1/2 FIFO transfers */
1334 #ifdef USE_DMA
1335  devpriv->flags |= DMA0_ACTIVE;
1336 
1337  /* point to first transfer in ring */
1338  devpriv->dma0Offset = 0;
1339  writel(DMA_MODE_BITS, devpriv->lcfg + LCFG_DMAMODE0);
1340  /* point to first block */
1341  writel(devpriv->dma0Chain[DMA_CHAIN_COUNT - 1].next,
1342  devpriv->lcfg + LCFG_DMADPR0);
1344  writel(readl(devpriv->lcfg + LCFG_ITCSR) | ICS_DMA0_E,
1345  devpriv->lcfg + LCFG_ITCSR);
1346  /* Must be 2 steps. See PLX app note about "Starting a DMA transfer" */
1347  devpriv->dma0Control = PLX_DMA_EN_BIT;
1348  writeb(devpriv->dma0Control,
1349  devpriv->lcfg + LCFG_DMACSR0);
1350  devpriv->dma0Control |= PLX_DMA_START_BIT;
1351  writeb(devpriv->dma0Control,
1352  devpriv->lcfg + LCFG_DMACSR0);
1353  DPRINTK("rtd520: Using DMA0 transfers. plxInt %x RtdInt %x\n",
1354  readl(devpriv->lcfg + LCFG_ITCSR), devpriv->intMask);
1355 #else /* USE_DMA */
1356  devpriv->intMask = IRQM_ADC_ABOUT_CNT;
1357  writew(devpriv->intMask, devpriv->las0 + LAS0_IT);
1358  DPRINTK("rtd520: Transferring every 1/2 FIFO\n");
1359 #endif /* USE_DMA */
1360  }
1361 
1362  /* BUG: start_src is ASSUMED to be TRIG_NOW */
1363  /* BUG? it seems like things are running before the "start" */
1364  readl(devpriv->las0 + LAS0_PACER); /* start pacer */
1365  return 0;
1366 }
1367 
1368 /*
1369  Stop a running data acquisition.
1370 */
1371 static int rtd_ai_cancel(struct comedi_device *dev, struct comedi_subdevice *s)
1372 {
1373  struct rtdPrivate *devpriv = dev->private;
1374  u32 overrun;
1375  u16 status;
1376 
1377  /* pacer stop source: SOFTWARE */
1378  writel(0, devpriv->las0 + LAS0_PACER_STOP);
1379  writel(0, devpriv->las0 + LAS0_PACER); /* stop pacer */
1380  writel(0, devpriv->las0 + LAS0_ADC_CONVERSION);
1381  devpriv->intMask = 0;
1382  writew(devpriv->intMask, devpriv->las0 + LAS0_IT);
1383  devpriv->aiCount = 0; /* stop and don't transfer any more */
1384 #ifdef USE_DMA
1385  if (devpriv->flags & DMA0_ACTIVE) {
1386  writel(readl(devpriv->lcfg + LCFG_ITCSR) & ~ICS_DMA0_E,
1387  devpriv->lcfg + LCFG_ITCSR);
1388  abort_dma(dev, 0);
1389  devpriv->flags &= ~DMA0_ACTIVE;
1390  }
1391 #endif /* USE_DMA */
1392  status = readw(devpriv->las0 + LAS0_IT);
1393  overrun = readl(devpriv->las0 + LAS0_OVERRUN) & 0xffff;
1394  DPRINTK
1395  ("rtd520: Acquisition canceled. %ld ints, intStat=%x, overStat=%x\n",
1396  devpriv->intCount, status, overrun);
1397  return 0;
1398 }
1399 
1400 /*
1401  Output one (or more) analog values to a single port as fast as possible.
1402 */
1403 static int rtd_ao_winsn(struct comedi_device *dev,
1404  struct comedi_subdevice *s, struct comedi_insn *insn,
1405  unsigned int *data)
1406 {
1407  struct rtdPrivate *devpriv = dev->private;
1408  int i;
1409  int chan = CR_CHAN(insn->chanspec);
1410  int range = CR_RANGE(insn->chanspec);
1411 
1412  /* Configure the output range (table index matches the range values) */
1413  writew(range & 7, devpriv->las0 +
1414  ((chan == 0) ? LAS0_DAC1_CTRL : LAS0_DAC2_CTRL));
1415 
1416  /* Writing a list of values to an AO channel is probably not
1417  * very useful, but that's how the interface is defined. */
1418  for (i = 0; i < insn->n; ++i) {
1419  int val = data[i] << 3;
1420  int stat = 0; /* initialize to avoid bogus warning */
1421  int ii;
1422 
1423  /* VERIFY: comedi range and offset conversions */
1424 
1425  if ((range > 1) /* bipolar */
1426  && (data[i] < 2048)) {
1427  /* offset and sign extend */
1428  val = (((int)data[i]) - 2048) << 3;
1429  } else { /* unipolor */
1430  val = data[i] << 3;
1431  }
1432 
1433  DPRINTK
1434  ("comedi: rtd520 DAC chan=%d range=%d writing %d as 0x%x\n",
1435  chan, range, data[i], val);
1436 
1437  /* a typical programming sequence */
1438  writew(val, devpriv->las1 +
1439  ((chan == 0) ? LAS1_DAC1_FIFO : LAS1_DAC2_FIFO));
1440  writew(0, devpriv->las0 + ((chan == 0) ? LAS0_DAC1 : LAS0_DAC2));
1441 
1442  devpriv->aoValue[chan] = data[i]; /* save for read back */
1443 
1444  for (ii = 0; ii < RTD_DAC_TIMEOUT; ++ii) {
1445  stat = readl(devpriv->las0 + LAS0_ADC);
1446  /* 1 -> not empty */
1447  if (stat & ((0 == chan) ? FS_DAC1_NOT_EMPTY :
1449  break;
1450  WAIT_QUIETLY;
1451  }
1452  if (ii >= RTD_DAC_TIMEOUT) {
1453  DPRINTK
1454  ("rtd520: Error: DAC never finished! FifoStatus=0x%x\n",
1455  stat ^ 0x6666);
1456  return -ETIMEDOUT;
1457  }
1458  }
1459 
1460  /* return the number of samples read/written */
1461  return i;
1462 }
1463 
1464 /* AO subdevices should have a read insn as well as a write insn.
1465  * Usually this means copying a value stored in devpriv. */
1466 static int rtd_ao_rinsn(struct comedi_device *dev,
1467  struct comedi_subdevice *s, struct comedi_insn *insn,
1468  unsigned int *data)
1469 {
1470  struct rtdPrivate *devpriv = dev->private;
1471  int i;
1472  int chan = CR_CHAN(insn->chanspec);
1473 
1474  for (i = 0; i < insn->n; i++)
1475  data[i] = devpriv->aoValue[chan];
1476 
1477 
1478  return i;
1479 }
1480 
1481 /*
1482  Write a masked set of bits and the read back the port.
1483  We track what the bits should be (i.e. we don't read the port first).
1484 
1485  DIO devices are slightly special. Although it is possible to
1486  * implement the insn_read/insn_write interface, it is much more
1487  * useful to applications if you implement the insn_bits interface.
1488  * This allows packed reading/writing of the DIO channels. The
1489  * comedi core can convert between insn_bits and insn_read/write
1490  */
1491 static int rtd_dio_insn_bits(struct comedi_device *dev,
1492  struct comedi_subdevice *s,
1493  struct comedi_insn *insn, unsigned int *data)
1494 {
1495  struct rtdPrivate *devpriv = dev->private;
1496 
1497  /* The insn data is a mask in data[0] and the new data
1498  * in data[1], each channel cooresponding to a bit. */
1499  if (data[0]) {
1500  s->state &= ~data[0];
1501  s->state |= data[0] & data[1];
1502 
1503  /* Write out the new digital output lines */
1504  writew(s->state & 0xff, devpriv->las0 + LAS0_DIO0);
1505  }
1506  /* on return, data[1] contains the value of the digital
1507  * input lines. */
1508  data[1] = readw(devpriv->las0 + LAS0_DIO0) & 0xff;
1509 
1510  /*DPRINTK("rtd520:port_0 wrote: 0x%x read: 0x%x\n", s->state, data[1]); */
1511 
1512  return insn->n;
1513 }
1514 
1515 /*
1516  Configure one bit on a IO port as Input or Output (hence the name :-).
1517 */
1518 static int rtd_dio_insn_config(struct comedi_device *dev,
1519  struct comedi_subdevice *s,
1520  struct comedi_insn *insn, unsigned int *data)
1521 {
1522  struct rtdPrivate *devpriv = dev->private;
1523  int chan = CR_CHAN(insn->chanspec);
1524 
1525  /* The input or output configuration of each digital line is
1526  * configured by a special insn_config instruction. chanspec
1527  * contains the channel to be changed, and data[0] contains the
1528  * value COMEDI_INPUT or COMEDI_OUTPUT. */
1529  switch (data[0]) {
1531  s->io_bits |= 1 << chan; /* 1 means Out */
1532  break;
1533  case INSN_CONFIG_DIO_INPUT:
1534  s->io_bits &= ~(1 << chan);
1535  break;
1536  case INSN_CONFIG_DIO_QUERY:
1537  data[1] =
1538  (s->io_bits & (1 << chan)) ? COMEDI_OUTPUT : COMEDI_INPUT;
1539  return insn->n;
1540  break;
1541  default:
1542  return -EINVAL;
1543  }
1544 
1545  DPRINTK("rtd520: port_0_direction=0x%x (1 means out)\n", s->io_bits);
1546  /* TODO support digital match interrupts and strobes */
1547  devpriv->dioStatus = 0x01; /* set direction */
1548  writew(devpriv->dioStatus, devpriv->las0 + LAS0_DIO_STATUS);
1549  writew(s->io_bits & 0xff, devpriv->las0 + LAS0_DIO0_CTRL);
1550  devpriv->dioStatus = 0x00; /* clear interrupts */
1551  writew(devpriv->dioStatus, devpriv->las0 + LAS0_DIO_STATUS);
1552 
1553  /* port1 can only be all input or all output */
1554 
1555  /* there are also 2 user input lines and 2 user output lines */
1556 
1557  return 1;
1558 }
1559 
1560 static struct pci_dev *rtd_find_pci(struct comedi_device *dev,
1561  struct comedi_devconfig *it)
1562 {
1563  const struct rtdBoard *thisboard;
1564  struct pci_dev *pcidev = NULL;
1565  int bus = it->options[0];
1566  int slot = it->options[1];
1567  int i;
1568 
1569  for_each_pci_dev(pcidev) {
1570  if (pcidev->vendor != PCI_VENDOR_ID_RTD)
1571  continue;
1572  if (bus || slot) {
1573  if (pcidev->bus->number != bus ||
1574  PCI_SLOT(pcidev->devfn) != slot)
1575  continue;
1576  }
1577  for (i = 0; i < ARRAY_SIZE(rtd520Boards); i++) {
1578  thisboard = &rtd520Boards[i];
1579  if (pcidev->device == thisboard->device_id) {
1580  dev->board_ptr = thisboard;
1581  return pcidev;
1582  }
1583  }
1584  }
1585  dev_warn(dev->class_dev,
1586  "no supported board found! (req. bus/slot: %d/%d)\n",
1587  bus, slot);
1588  return NULL;
1589 }
1590 
1591 static int rtd_attach(struct comedi_device *dev, struct comedi_devconfig *it)
1592 { /* board name and options flags */
1593  const struct rtdBoard *thisboard;
1594  struct rtdPrivate *devpriv;
1595  struct pci_dev *pcidev;
1596  struct comedi_subdevice *s;
1597  resource_size_t pci_base;
1598  int ret;
1599 #ifdef USE_DMA
1600  int index;
1601 #endif
1602 
1603  printk(KERN_INFO "comedi%d: rtd520 attaching.\n", dev->minor);
1604 
1605 #if defined(CONFIG_COMEDI_DEBUG) && defined(USE_DMA)
1606  /* You can set this a load time: modprobe comedi comedi_debug=1 */
1607  if (0 == comedi_debug) /* force DMA debug printks */
1608  comedi_debug = 1;
1609 #endif
1610 
1611  /*
1612  * Allocate the private structure area. alloc_private() is a
1613  * convenient macro defined in comedidev.h.
1614  */
1615  if (alloc_private(dev, sizeof(struct rtdPrivate)) < 0)
1616  return -ENOMEM;
1617  devpriv = dev->private;
1618 
1619  pcidev = rtd_find_pci(dev, it);
1620  if (!pcidev)
1621  return -EIO;
1622  comedi_set_hw_dev(dev, &pcidev->dev);
1623  thisboard = comedi_board(dev);
1624 
1625  dev->board_name = thisboard->name;
1626 
1627  ret = comedi_pci_enable(pcidev, DRV_NAME);
1628  if (ret < 0) {
1629  printk(KERN_INFO "Failed to enable PCI device and request regions.\n");
1630  return ret;
1631  }
1632  dev->iobase = 1; /* the "detach" needs this */
1633 
1634  /* Initialize the base addresses */
1635  pci_base = pci_resource_start(pcidev, LAS0_PCIINDEX);
1636  devpriv->las0 = ioremap_nocache(pci_base, LAS0_PCISIZE);
1637  pci_base = pci_resource_start(pcidev, LAS1_PCIINDEX);
1638  devpriv->las1 = ioremap_nocache(pci_base, LAS1_PCISIZE);
1639  pci_base = pci_resource_start(pcidev, LCFG_PCIINDEX);
1640  devpriv->lcfg = ioremap_nocache(pci_base, LCFG_PCISIZE);
1641  if (!devpriv->las0 || !devpriv->las1 || !devpriv->lcfg)
1642  return -ENOMEM;
1643 
1644  { /* The RTD driver does this */
1645  unsigned char pci_latency;
1646  u16 revision;
1647  /*uint32_t epld_version; */
1648 
1649  pci_read_config_word(pcidev, PCI_REVISION_ID,
1650  &revision);
1651  DPRINTK("%s: PCI revision %d.\n", dev->board_name, revision);
1652 
1653  pci_read_config_byte(pcidev,
1654  PCI_LATENCY_TIMER, &pci_latency);
1655  if (pci_latency < 32) {
1656  printk(KERN_INFO "%s: PCI latency changed from %d to %d\n",
1657  dev->board_name, pci_latency, 32);
1658  pci_write_config_byte(pcidev,
1659  PCI_LATENCY_TIMER, 32);
1660  } else {
1661  DPRINTK("rtd520: PCI latency = %d\n", pci_latency);
1662  }
1663 
1664  /*
1665  * Undocumented EPLD version (doesn't match RTD driver results)
1666  */
1667  /*DPRINTK ("rtd520: Reading epld from %p\n",
1668  devpriv->las0+0);
1669  epld_version = readl (devpriv->las0+0);
1670  if ((epld_version & 0xF0) >> 4 == 0x0F) {
1671  DPRINTK("rtd520: pre-v8 EPLD. (%x)\n", epld_version);
1672  } else {
1673  DPRINTK("rtd520: EPLD version %x.\n", epld_version >> 4);
1674  } */
1675  }
1676 
1677  /* Show board configuration */
1678  printk(KERN_INFO "%s:", dev->board_name);
1679 
1680  ret = comedi_alloc_subdevices(dev, 4);
1681  if (ret)
1682  return ret;
1683 
1684  s = &dev->subdevices[0];
1685  dev->read_subdev = s;
1686  /* analog input subdevice */
1687  s->type = COMEDI_SUBD_AI;
1688  s->subdev_flags =
1690  s->n_chan = thisboard->aiChans;
1691  s->maxdata = (1 << thisboard->aiBits) - 1;
1692  if (thisboard->aiMaxGain <= 32)
1693  s->range_table = &rtd_ai_7520_range;
1694  else
1695  s->range_table = &rtd_ai_4520_range;
1696 
1697  s->len_chanlist = RTD_MAX_CHANLIST; /* devpriv->fifoLen */
1698  s->insn_read = rtd_ai_rinsn;
1699  s->do_cmd = rtd_ai_cmd;
1700  s->do_cmdtest = rtd_ai_cmdtest;
1701  s->cancel = rtd_ai_cancel;
1702  /* s->poll = rtd_ai_poll; *//* not ready yet */
1703 
1704  s = &dev->subdevices[1];
1705  /* analog output subdevice */
1706  s->type = COMEDI_SUBD_AO;
1708  s->n_chan = 2;
1709  s->maxdata = (1 << thisboard->aiBits) - 1;
1710  s->range_table = &rtd_ao_range;
1711  s->insn_write = rtd_ao_winsn;
1712  s->insn_read = rtd_ao_rinsn;
1713 
1714  s = &dev->subdevices[2];
1715  /* digital i/o subdevice */
1716  s->type = COMEDI_SUBD_DIO;
1718  /* we only support port 0 right now. Ignoring port 1 and user IO */
1719  s->n_chan = 8;
1720  s->maxdata = 1;
1721  s->range_table = &range_digital;
1722  s->insn_bits = rtd_dio_insn_bits;
1723  s->insn_config = rtd_dio_insn_config;
1724 
1725  /* timer/counter subdevices (not currently supported) */
1726  s = &dev->subdevices[3];
1729  s->n_chan = 3;
1730  s->maxdata = 0xffff;
1731 
1732  /* initialize board, per RTD spec */
1733  /* also, initialize shadow registers */
1734  writel(0, devpriv->las0 + LAS0_BOARD_RESET);
1735  udelay(100); /* needed? */
1736  writel(0, devpriv->lcfg + LCFG_ITCSR);
1737  devpriv->intMask = 0;
1738  writew(devpriv->intMask, devpriv->las0 + LAS0_IT);
1739  devpriv->intClearMask = ~0;
1740  writew(devpriv->intClearMask, devpriv->las0 + LAS0_CLEAR);
1741  readw(devpriv->las0 + LAS0_CLEAR);
1742  writel(0, devpriv->las0 + LAS0_OVERRUN);
1743  writel(0, devpriv->las0 + LAS0_CGT_CLEAR);
1744  writel(0, devpriv->las0 + LAS0_ADC_FIFO_CLEAR);
1745  writel(0, devpriv->las0 + LAS0_DAC1_RESET);
1746  writel(0, devpriv->las0 + LAS0_DAC2_RESET);
1747  /* clear digital IO fifo */
1748  devpriv->dioStatus = 0;
1749  writew(devpriv->dioStatus, devpriv->las0 + LAS0_DIO_STATUS);
1750  devpriv->utcCtrl[0] = (0 << 6) | 0x30;
1751  devpriv->utcCtrl[1] = (1 << 6) | 0x30;
1752  devpriv->utcCtrl[2] = (2 << 6) | 0x30;
1753  devpriv->utcCtrl[3] = (3 << 6) | 0x00;
1754  writeb(devpriv->utcCtrl[0], devpriv->las0 + LAS0_UTC_CTRL);
1755  writeb(devpriv->utcCtrl[1], devpriv->las0 + LAS0_UTC_CTRL);
1756  writeb(devpriv->utcCtrl[2], devpriv->las0 + LAS0_UTC_CTRL);
1757  writeb(devpriv->utcCtrl[3], devpriv->las0 + LAS0_UTC_CTRL);
1758  /* TODO: set user out source ??? */
1759 
1760  /* check if our interrupt is available and get it */
1761  ret = request_irq(pcidev->irq, rtd_interrupt,
1762  IRQF_SHARED, DRV_NAME, dev);
1763 
1764  if (ret < 0) {
1765  printk("Could not get interrupt! (%u)\n",
1766  pcidev->irq);
1767  return ret;
1768  }
1769  dev->irq = pcidev->irq;
1770  printk(KERN_INFO "( irq=%u )", dev->irq);
1771 
1772  ret = rtd520_probe_fifo_depth(dev);
1773  if (ret < 0)
1774  return ret;
1775 
1776  devpriv->fifoLen = ret;
1777  printk("( fifoLen=%d )", devpriv->fifoLen);
1778 
1779 #ifdef USE_DMA
1780  if (dev->irq > 0) {
1781  printk("( DMA buff=%d )\n", DMA_CHAIN_COUNT);
1782  /*
1783  * The PLX9080 has 2 DMA controllers, but there could be
1784  * 4 sources: ADC, digital, DAC1, and DAC2. Since only the
1785  * ADC supports cmd mode right now, this isn't an issue (yet)
1786  */
1787  devpriv->dma0Offset = 0;
1788 
1789  for (index = 0; index < DMA_CHAIN_COUNT; index++) {
1790  devpriv->dma0Buff[index] =
1791  pci_alloc_consistent(pcidev,
1792  sizeof(u16) *
1793  devpriv->fifoLen / 2,
1794  &devpriv->
1795  dma0BuffPhysAddr[index]);
1796  if (devpriv->dma0Buff[index] == NULL) {
1797  ret = -ENOMEM;
1798  goto rtd_attach_die_error;
1799  }
1800  /*DPRINTK ("buff[%d] @ %p virtual, %x PCI\n",
1801  index,
1802  devpriv->dma0Buff[index],
1803  devpriv->dma0BuffPhysAddr[index]); */
1804  }
1805 
1806  /*
1807  * setup DMA descriptor ring (use cpu_to_le32 for byte
1808  * ordering?)
1809  */
1810  devpriv->dma0Chain =
1811  pci_alloc_consistent(pcidev,
1812  sizeof(struct plx_dma_desc) *
1813  DMA_CHAIN_COUNT,
1814  &devpriv->dma0ChainPhysAddr);
1815  for (index = 0; index < DMA_CHAIN_COUNT; index++) {
1816  devpriv->dma0Chain[index].pci_start_addr =
1817  devpriv->dma0BuffPhysAddr[index];
1818  devpriv->dma0Chain[index].local_start_addr =
1819  DMALADDR_ADC;
1820  devpriv->dma0Chain[index].transfer_size =
1821  sizeof(u16) * devpriv->fifoLen / 2;
1822  devpriv->dma0Chain[index].next =
1823  (devpriv->dma0ChainPhysAddr + ((index +
1824  1) %
1825  (DMA_CHAIN_COUNT))
1826  * sizeof(devpriv->dma0Chain[0]))
1828  /*DPRINTK ("ring[%d] @%lx PCI: %x, local: %x, N: 0x%x, next: %x\n",
1829  index,
1830  ((long)devpriv->dma0ChainPhysAddr
1831  + (index * sizeof(devpriv->dma0Chain[0]))),
1832  devpriv->dma0Chain[index].pci_start_addr,
1833  devpriv->dma0Chain[index].local_start_addr,
1834  devpriv->dma0Chain[index].transfer_size,
1835  devpriv->dma0Chain[index].next); */
1836  }
1837 
1838  if (devpriv->dma0Chain == NULL) {
1839  ret = -ENOMEM;
1840  goto rtd_attach_die_error;
1841  }
1842 
1843  writel(DMA_MODE_BITS, devpriv->lcfg + LCFG_DMAMODE0);
1844  /* set DMA trigger source */
1846  } else {
1847  printk(KERN_INFO "( no IRQ->no DMA )");
1848  }
1849 #endif /* USE_DMA */
1850 
1851  if (dev->irq)
1852  writel(ICS_PIE | ICS_PLIE, devpriv->lcfg + LCFG_ITCSR);
1853 
1854  printk("\ncomedi%d: rtd520 driver attached.\n", dev->minor);
1855 
1856  return 1;
1857 }
1858 
1859 static void rtd_detach(struct comedi_device *dev)
1860 {
1861  struct rtdPrivate *devpriv = dev->private;
1862  struct pci_dev *pcidev = comedi_to_pci_dev(dev);
1863 #ifdef USE_DMA
1864  int index;
1865 #endif
1866 
1867  if (devpriv) {
1868  /* Shut down any board ops by resetting it */
1869 #ifdef USE_DMA
1870  if (devpriv->lcfg) {
1871  devpriv->dma0Control = 0;
1872  devpriv->dma1Control = 0;
1873  writeb(devpriv->dma0Control,
1874  devpriv->lcfg + LCFG_DMACSR0);
1875  writeb(devpriv->dma1Control,
1876  devpriv->lcfg + LCFG_DMACSR1);
1877  writel(ICS_PIE | ICS_PLIE, devpriv->lcfg + LCFG_ITCSR);
1878  }
1879 #endif /* USE_DMA */
1880  if (devpriv->las0) {
1881  writel(0, devpriv->las0 + LAS0_BOARD_RESET);
1882  devpriv->intMask = 0;
1883  writew(devpriv->intMask, devpriv->las0 + LAS0_IT);
1884  devpriv->intClearMask = ~0;
1885  writew(devpriv->intClearMask,
1886  devpriv->las0 + LAS0_CLEAR);
1887  readw(devpriv->las0 + LAS0_CLEAR);
1888  }
1889 #ifdef USE_DMA
1890  /* release DMA */
1891  for (index = 0; index < DMA_CHAIN_COUNT; index++) {
1892  if (NULL != devpriv->dma0Buff[index]) {
1893  pci_free_consistent(pcidev,
1894  sizeof(u16) *
1895  devpriv->fifoLen / 2,
1896  devpriv->dma0Buff[index],
1897  devpriv->
1898  dma0BuffPhysAddr[index]);
1899  devpriv->dma0Buff[index] = NULL;
1900  }
1901  }
1902  if (NULL != devpriv->dma0Chain) {
1903  pci_free_consistent(pcidev,
1904  sizeof(struct plx_dma_desc) *
1905  DMA_CHAIN_COUNT, devpriv->dma0Chain,
1906  devpriv->dma0ChainPhysAddr);
1907  devpriv->dma0Chain = NULL;
1908  }
1909 #endif /* USE_DMA */
1910  if (dev->irq) {
1911  writel(readl(devpriv->lcfg + LCFG_ITCSR) &
1913  devpriv->lcfg + LCFG_ITCSR);
1914  free_irq(dev->irq, dev);
1915  }
1916  if (devpriv->las0)
1917  iounmap(devpriv->las0);
1918  if (devpriv->las1)
1919  iounmap(devpriv->las1);
1920  if (devpriv->lcfg)
1921  iounmap(devpriv->lcfg);
1922  }
1923  if (pcidev) {
1924  if (dev->iobase)
1925  comedi_pci_disable(pcidev);
1926  pci_dev_put(pcidev);
1927  }
1928 }
1929 
1930 static struct comedi_driver rtd520_driver = {
1931  .driver_name = "rtd520",
1932  .module = THIS_MODULE,
1933  .attach = rtd_attach,
1934  .detach = rtd_detach,
1935 };
1936 
1937 static int __devinit rtd520_pci_probe(struct pci_dev *dev,
1938  const struct pci_device_id *ent)
1939 {
1940  return comedi_pci_auto_config(dev, &rtd520_driver);
1941 }
1942 
1943 static void __devexit rtd520_pci_remove(struct pci_dev *dev)
1944 {
1946 }
1947 
1948 static DEFINE_PCI_DEVICE_TABLE(rtd520_pci_table) = {
1949  { PCI_DEVICE(PCI_VENDOR_ID_RTD, 0x7520) },
1950  { PCI_DEVICE(PCI_VENDOR_ID_RTD, 0x4520) },
1951  { 0 }
1952 };
1953 MODULE_DEVICE_TABLE(pci, rtd520_pci_table);
1954 
1955 static struct pci_driver rtd520_pci_driver = {
1956  .name = "rtd520",
1957  .id_table = rtd520_pci_table,
1958  .probe = rtd520_pci_probe,
1959  .remove = __devexit_p(rtd520_pci_remove),
1960 };
1961 module_comedi_pci_driver(rtd520_driver, rtd520_pci_driver);
1962 
1963 MODULE_AUTHOR("Comedi http://www.comedi.org");
1964 MODULE_DESCRIPTION("Comedi low-level driver");
1965 MODULE_LICENSE("GPL");