]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/staging/comedi/drivers/me_daq.c
staging: comedi: me_daq: tidy up counter registers
[karo-tx-linux.git] / drivers / staging / comedi / drivers / me_daq.c
1 /*
2  * comedi/drivers/me_daq.c
3  * Hardware driver for Meilhaus data acquisition cards:
4  *   ME-2000i, ME-2600i, ME-3000vm1
5  *
6  * Copyright (C) 2002 Michael Hillmann <hillmann@syscongroup.de>
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
19 /*
20  * Driver: me_daq
21  * Description: Meilhaus PCI data acquisition cards
22  * Devices: [Meilhaus] ME-2600i (me-2600i), ME-2000i (me-2000i)
23  * Author: Michael Hillmann <hillmann@syscongroup.de>
24  * Status: experimental
25  *
26  * Configuration options: not applicable, uses PCI auto config
27  *
28  * Supports:
29  *    Analog Input, Analog Output, Digital I/O
30  */
31
32 #include <linux/module.h>
33 #include <linux/interrupt.h>
34 #include <linux/sched.h>
35
36 #include "../comedi_pci.h"
37
38 #include "plx9052.h"
39
40 #define ME2600_FIRMWARE         "me2600_firmware.bin"
41
42 #define XILINX_DOWNLOAD_RESET   0x42    /* Xilinx registers */
43
44 /*
45  * PCI BAR2 Memory map (dev->mmio)
46  */
47 #define ME_CTRL1_REG                    0x00    /* R (ai start) | W */
48 #define   ME_CTRL1_INT_ENA              BIT(15)
49 #define   ME_CTRL1_COUNTER_B_IRQ        BIT(12)
50 #define   ME_CTRL1_COUNTER_A_IRQ        BIT(11)
51 #define   ME_CTRL1_CHANLIST_READY_IRQ   BIT(10)
52 #define   ME_CTRL1_EXT_IRQ              BIT(9)
53 #define   ME_CTRL1_ADFIFO_HALFFULL_IRQ  BIT(8)
54 #define   ME_CTRL1_SCAN_COUNT_ENA       BIT(5)
55 #define   ME_CTRL1_SIMULTANEOUS_ENA     BIT(4)
56 #define   ME_CTRL1_TRIGGER_FALLING_EDGE BIT(3)
57 #define   ME_CTRL1_CONTINUOUS_MODE      BIT(2)
58 #define   ME_CTRL1_ADC_MODE(x)          (((x) & 0x3) << 0)
59 #define   ME_CTRL1_ADC_MODE_DISABLE     ME_CTRL1_ADC_MODE(0)
60 #define   ME_CTRL1_ADC_MODE_SOFT_TRIG   ME_CTRL1_ADC_MODE(1)
61 #define   ME_CTRL1_ADC_MODE_SCAN_TRIG   ME_CTRL1_ADC_MODE(2)
62 #define   ME_CTRL1_ADC_MODE_EXT_TRIG    ME_CTRL1_ADC_MODE(3)
63 #define   ME_CTRL1_ADC_MODE_MASK        ME_CTRL1_ADC_MODE(3)
64 #define ME_CTRL2_REG                    0x02    /* R (dac update) | W */
65 #define   ME_CTRL2_ADFIFO_ENA           BIT(10)
66 #define   ME_CTRL2_CHANLIST_ENA         BIT(9)
67 #define   ME_CTRL2_PORT_B_ENA           BIT(7)
68 #define   ME_CTRL2_PORT_A_ENA           BIT(6)
69 #define   ME_CTRL2_COUNTER_B_ENA        BIT(4)
70 #define   ME_CTRL2_COUNTER_A_ENA        BIT(3)
71 #define   ME_CTRL2_DAC_ENA              BIT(1)
72 #define   ME_CTRL2_BUFFERED_DAC         BIT(0)
73 #define ME_STATUS_REG                   0x04    /* R | W (clears interrupts) */
74 #define   ME_STATUS_COUNTER_B_IRQ       BIT(12)
75 #define   ME_STATUS_COUNTER_A_IRQ       BIT(11)
76 #define   ME_STATUS_CHANLIST_READY_IRQ  BIT(10)
77 #define   ME_STATUS_EXT_IRQ             BIT(9)
78 #define   ME_STATUS_ADFIFO_HALFFULL_IRQ BIT(8)
79 #define   ME_STATUS_ADFIFO_FULL         BIT(4)
80 #define   ME_STATUS_ADFIFO_HALFFULL     BIT(3)
81 #define   ME_STATUS_ADFIFO_EMPTY        BIT(2)
82 #define   ME_STATUS_CHANLIST_FULL       BIT(1)
83 #define   ME_STATUS_FST_ACTIVE          BIT(0)
84 #define ME_DIO_PORT_A_REG               0x06    /* R | W */
85 #define ME_DIO_PORT_B_REG               0x08    /* R | W */
86 #define ME_TIMER_DATA_REG(x)            (0x0a + ((x) * 2))      /* - | W */
87 #define ME_AI_FIFO_REG                  0x10    /* R (fifo) | W (chanlist) */
88 #define   ME_AI_FIFO_CHANLIST_DIFF      BIT(7)
89 #define   ME_AI_FIFO_CHANLIST_UNIPOLAR  BIT(6)
90 #define   ME_AI_FIFO_CHANLIST_GAIN(x)   (((x) & 0x3) << 4)
91 #define   ME_AI_FIFO_CHANLIST_CHAN(x)   (((x) & 0xf) << 0)
92 #define ME_DAC_CTRL_REG                 0x12    /* R (updates) | W */
93 #define   ME_DAC_CTRL_BIPOLAR(x)        BIT(7 - ((x) & 0x3))
94 #define   ME_DAC_CTRL_GAIN(x)           BIT(11 - ((x) & 0x3))
95 #define   ME_DAC_CTRL_MASK(x)           (ME_DAC_CTRL_BIPOLAR(x) |       \
96                                          ME_DAC_CTRL_GAIN(x))
97 #define ME_AO_DATA_REG(x)               (0x14 + ((x) * 2))      /* - | W */
98 #define ME_COUNTER_ENDDATA_REG(x)       (0x1c + ((x) * 2))      /* - | W */
99 #define ME_COUNTER_STARTDATA_REG(x)     (0x20 + ((x) * 2))      /* - | W */
100 #define ME_COUNTER_VALUE_REG(x)         (0x20 + ((x) * 2))      /* R | - */
101
102 static const struct comedi_lrange me_ai_range = {
103         8, {
104                 BIP_RANGE(10),
105                 BIP_RANGE(5),
106                 BIP_RANGE(2.5),
107                 BIP_RANGE(1.25),
108                 UNI_RANGE(10),
109                 UNI_RANGE(5),
110                 UNI_RANGE(2.5),
111                 UNI_RANGE(1.25)
112         }
113 };
114
115 static const struct comedi_lrange me_ao_range = {
116         3, {
117                 BIP_RANGE(10),
118                 BIP_RANGE(5),
119                 UNI_RANGE(10)
120         }
121 };
122
123 enum me_boardid {
124         BOARD_ME2600,
125         BOARD_ME2000,
126 };
127
128 struct me_board {
129         const char *name;
130         int needs_firmware;
131         int has_ao;
132 };
133
134 static const struct me_board me_boards[] = {
135         [BOARD_ME2600] = {
136                 .name           = "me-2600i",
137                 .needs_firmware = 1,
138                 .has_ao         = 1,
139         },
140         [BOARD_ME2000] = {
141                 .name           = "me-2000i",
142         },
143 };
144
145 struct me_private_data {
146         void __iomem *plx_regbase;      /* PLX configuration base address */
147
148         unsigned short ctrl1;           /* Mirror of CONTROL_1 register */
149         unsigned short ctrl2;           /* Mirror of CONTROL_2 register */
150         unsigned short dac_ctrl;        /* Mirror of the DAC_CONTROL register */
151 };
152
153 static inline void sleep(unsigned sec)
154 {
155         schedule_timeout_interruptible(sec * HZ);
156 }
157
158 static int me_dio_insn_config(struct comedi_device *dev,
159                               struct comedi_subdevice *s,
160                               struct comedi_insn *insn,
161                               unsigned int *data)
162 {
163         struct me_private_data *devpriv = dev->private;
164         unsigned int chan = CR_CHAN(insn->chanspec);
165         unsigned int mask;
166         int ret;
167
168         if (chan < 16)
169                 mask = 0x0000ffff;
170         else
171                 mask = 0xffff0000;
172
173         ret = comedi_dio_insn_config(dev, s, insn, data, mask);
174         if (ret)
175                 return ret;
176
177         if (s->io_bits & 0x0000ffff)
178                 devpriv->ctrl2 |= ME_CTRL2_PORT_A_ENA;
179         else
180                 devpriv->ctrl2 &= ~ME_CTRL2_PORT_A_ENA;
181         if (s->io_bits & 0xffff0000)
182                 devpriv->ctrl2 |= ME_CTRL2_PORT_B_ENA;
183         else
184                 devpriv->ctrl2 &= ~ME_CTRL2_PORT_B_ENA;
185
186         writew(devpriv->ctrl2, dev->mmio + ME_CTRL2_REG);
187
188         return insn->n;
189 }
190
191 static int me_dio_insn_bits(struct comedi_device *dev,
192                             struct comedi_subdevice *s,
193                             struct comedi_insn *insn,
194                             unsigned int *data)
195 {
196         void __iomem *mmio_porta = dev->mmio + ME_DIO_PORT_A_REG;
197         void __iomem *mmio_portb = dev->mmio + ME_DIO_PORT_B_REG;
198         unsigned int mask;
199         unsigned int val;
200
201         mask = comedi_dio_update_state(s, data);
202         if (mask) {
203                 if (mask & 0x0000ffff)
204                         writew((s->state & 0xffff), mmio_porta);
205                 if (mask & 0xffff0000)
206                         writew(((s->state >> 16) & 0xffff), mmio_portb);
207         }
208
209         if (s->io_bits & 0x0000ffff)
210                 val = s->state & 0xffff;
211         else
212                 val = readw(mmio_porta);
213
214         if (s->io_bits & 0xffff0000)
215                 val |= (s->state & 0xffff0000);
216         else
217                 val |= (readw(mmio_portb) << 16);
218
219         data[1] = val;
220
221         return insn->n;
222 }
223
224 static int me_ai_eoc(struct comedi_device *dev,
225                      struct comedi_subdevice *s,
226                      struct comedi_insn *insn,
227                      unsigned long context)
228 {
229         unsigned int status;
230
231         status = readw(dev->mmio + ME_STATUS_REG);
232         if ((status & ME_STATUS_ADFIFO_EMPTY) == 0)
233                 return 0;
234         return -EBUSY;
235 }
236
237 static int me_ai_insn_read(struct comedi_device *dev,
238                            struct comedi_subdevice *s,
239                            struct comedi_insn *insn,
240                            unsigned int *data)
241 {
242         struct me_private_data *devpriv = dev->private;
243         unsigned int chan = CR_CHAN(insn->chanspec);
244         unsigned int range = CR_RANGE(insn->chanspec);
245         unsigned int aref = CR_AREF(insn->chanspec);
246         unsigned short val;
247         int ret;
248
249         /* stop any running conversion */
250         devpriv->ctrl1 &= ~ME_CTRL1_ADC_MODE_MASK;
251         writew(devpriv->ctrl1, dev->mmio + ME_CTRL1_REG);
252
253         /* clear chanlist and ad fifo */
254         devpriv->ctrl2 &= ~(ME_CTRL2_ADFIFO_ENA | ME_CTRL2_CHANLIST_ENA);
255         writew(devpriv->ctrl2, dev->mmio + ME_CTRL2_REG);
256
257         writew(0x00, dev->mmio + ME_STATUS_REG);        /* clear interrupts */
258
259         /* enable the chanlist and ADC fifo */
260         devpriv->ctrl2 |= (ME_CTRL2_ADFIFO_ENA | ME_CTRL2_CHANLIST_ENA);
261         writew(devpriv->ctrl2, dev->mmio + ME_CTRL2_REG);
262
263         /* write to channel list fifo */
264         val = ME_AI_FIFO_CHANLIST_CHAN(chan) | ME_AI_FIFO_CHANLIST_GAIN(range);
265         if (comedi_range_is_unipolar(s, range))
266                 val |= ME_AI_FIFO_CHANLIST_UNIPOLAR;
267         if (aref & AREF_DIFF)
268                 val |= ME_AI_FIFO_CHANLIST_DIFF;
269         writew(val, dev->mmio + ME_AI_FIFO_REG);
270
271         /* set ADC mode to software trigger */
272         devpriv->ctrl1 |= ME_CTRL1_ADC_MODE_SOFT_TRIG;
273         writew(devpriv->ctrl1, dev->mmio + ME_CTRL1_REG);
274
275         /* start ai conversion */
276         readw(dev->mmio + ME_CTRL1_REG);
277
278         /* wait for ADC fifo not empty flag */
279         ret = comedi_timeout(dev, s, insn, me_ai_eoc, 0);
280         if (ret)
281                 return ret;
282
283         /* get value from ADC fifo */
284         val = readw(dev->mmio + ME_AI_FIFO_REG);
285         val = (val ^ 0x800) & 0x0fff;
286         data[0] = val;
287
288         /* stop any running conversion */
289         devpriv->ctrl1 &= ~ME_CTRL1_ADC_MODE_MASK;
290         writew(devpriv->ctrl1, dev->mmio + ME_CTRL1_REG);
291
292         return 1;
293 }
294
295 static int me_ao_insn_write(struct comedi_device *dev,
296                             struct comedi_subdevice *s,
297                             struct comedi_insn *insn,
298                             unsigned int *data)
299 {
300         struct me_private_data *devpriv = dev->private;
301         unsigned int chan = CR_CHAN(insn->chanspec);
302         unsigned int range = CR_RANGE(insn->chanspec);
303         unsigned int val = s->readback[chan];
304         int i;
305
306         /* Enable all DAC */
307         devpriv->ctrl2 |= ME_CTRL2_DAC_ENA;
308         writew(devpriv->ctrl2, dev->mmio + ME_CTRL2_REG);
309
310         /* and set DAC to "buffered" mode */
311         devpriv->ctrl2 |= ME_CTRL2_BUFFERED_DAC;
312         writew(devpriv->ctrl2, dev->mmio + ME_CTRL2_REG);
313
314         /* Set dac-control register */
315         devpriv->dac_ctrl &= ~ME_DAC_CTRL_MASK(chan);
316         if (range == 0)
317                 devpriv->dac_ctrl |= ME_DAC_CTRL_GAIN(chan);
318         if (comedi_range_is_bipolar(s, range))
319                 devpriv->dac_ctrl |= ME_DAC_CTRL_BIPOLAR(chan);
320         writew(devpriv->dac_ctrl, dev->mmio + ME_DAC_CTRL_REG);
321
322         /* Update dac-control register */
323         readw(dev->mmio + ME_DAC_CTRL_REG);
324
325         /* Set data register */
326         for (i = 0; i < insn->n; i++) {
327                 val = data[i];
328
329                 writew(val, dev->mmio + ME_AO_DATA_REG(chan));
330         }
331         s->readback[chan] = val;
332
333         /* Update dac with data registers */
334         readw(dev->mmio + ME_CTRL2_REG);
335
336         return insn->n;
337 }
338
339 static int me2600_xilinx_download(struct comedi_device *dev,
340                                   const u8 *data, size_t size,
341                                   unsigned long context)
342 {
343         struct me_private_data *devpriv = dev->private;
344         unsigned int value;
345         unsigned int file_length;
346         unsigned int i;
347
348         /* disable irq's on PLX */
349         writel(0x00, devpriv->plx_regbase + PLX9052_INTCSR);
350
351         /* First, make a dummy read to reset xilinx */
352         value = readw(dev->mmio + XILINX_DOWNLOAD_RESET);
353
354         /* Wait until reset is over */
355         sleep(1);
356
357         /* Write a dummy value to Xilinx */
358         writeb(0x00, dev->mmio + 0x0);
359         sleep(1);
360
361         /*
362          * Format of the firmware
363          * Build longs from the byte-wise coded header
364          * Byte 1-3:   length of the array
365          * Byte 4-7:   version
366          * Byte 8-11:  date
367          * Byte 12-15: reserved
368          */
369         if (size < 16)
370                 return -EINVAL;
371
372         file_length = (((unsigned int)data[0] & 0xff) << 24) +
373             (((unsigned int)data[1] & 0xff) << 16) +
374             (((unsigned int)data[2] & 0xff) << 8) +
375             ((unsigned int)data[3] & 0xff);
376
377         /*
378          * Loop for writing firmware byte by byte to xilinx
379          * Firmware data start at offset 16
380          */
381         for (i = 0; i < file_length; i++)
382                 writeb((data[16 + i] & 0xff), dev->mmio + 0x0);
383
384         /* Write 5 dummy values to xilinx */
385         for (i = 0; i < 5; i++)
386                 writeb(0x00, dev->mmio + 0x0);
387
388         /* Test if there was an error during download -> INTB was thrown */
389         value = readl(devpriv->plx_regbase + PLX9052_INTCSR);
390         if (value & PLX9052_INTCSR_LI2STAT) {
391                 /* Disable interrupt */
392                 writel(0x00, devpriv->plx_regbase + PLX9052_INTCSR);
393                 dev_err(dev->class_dev, "Xilinx download failed\n");
394                 return -EIO;
395         }
396
397         /* Wait until the Xilinx is ready for real work */
398         sleep(1);
399
400         /* Enable PLX-Interrupts */
401         writel(PLX9052_INTCSR_LI1ENAB |
402                PLX9052_INTCSR_LI1POL |
403                PLX9052_INTCSR_PCIENAB,
404                devpriv->plx_regbase + PLX9052_INTCSR);
405
406         return 0;
407 }
408
409 static int me_reset(struct comedi_device *dev)
410 {
411         struct me_private_data *devpriv = dev->private;
412
413         /* Reset board */
414         writew(0x00, dev->mmio + ME_CTRL1_REG);
415         writew(0x00, dev->mmio + ME_CTRL2_REG);
416         writew(0x00, dev->mmio + ME_STATUS_REG);        /* clear interrupts */
417         writew(0x00, dev->mmio + ME_DAC_CTRL_REG);
418
419         /* Save values in the board context */
420         devpriv->dac_ctrl = 0;
421         devpriv->ctrl1 = 0;
422         devpriv->ctrl2 = 0;
423
424         return 0;
425 }
426
427 static int me_auto_attach(struct comedi_device *dev,
428                           unsigned long context)
429 {
430         struct pci_dev *pcidev = comedi_to_pci_dev(dev);
431         const struct me_board *board = NULL;
432         struct me_private_data *devpriv;
433         struct comedi_subdevice *s;
434         int ret;
435
436         if (context < ARRAY_SIZE(me_boards))
437                 board = &me_boards[context];
438         if (!board)
439                 return -ENODEV;
440         dev->board_ptr = board;
441         dev->board_name = board->name;
442
443         devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
444         if (!devpriv)
445                 return -ENOMEM;
446
447         ret = comedi_pci_enable(dev);
448         if (ret)
449                 return ret;
450
451         devpriv->plx_regbase = pci_ioremap_bar(pcidev, 0);
452         if (!devpriv->plx_regbase)
453                 return -ENOMEM;
454
455         dev->mmio = pci_ioremap_bar(pcidev, 2);
456         if (!dev->mmio)
457                 return -ENOMEM;
458
459         /* Download firmware and reset card */
460         if (board->needs_firmware) {
461                 ret = comedi_load_firmware(dev, &comedi_to_pci_dev(dev)->dev,
462                                            ME2600_FIRMWARE,
463                                            me2600_xilinx_download, 0);
464                 if (ret < 0)
465                         return ret;
466         }
467         me_reset(dev);
468
469         ret = comedi_alloc_subdevices(dev, 3);
470         if (ret)
471                 return ret;
472
473         s = &dev->subdevices[0];
474         s->type         = COMEDI_SUBD_AI;
475         s->subdev_flags = SDF_READABLE | SDF_COMMON;
476         s->n_chan       = 16;
477         s->maxdata      = 0x0fff;
478         s->len_chanlist = 16;
479         s->range_table  = &me_ai_range;
480         s->insn_read    = me_ai_insn_read;
481
482         s = &dev->subdevices[1];
483         if (board->has_ao) {
484                 s->type         = COMEDI_SUBD_AO;
485                 s->subdev_flags = SDF_WRITABLE | SDF_COMMON;
486                 s->n_chan       = 4;
487                 s->maxdata      = 0x0fff;
488                 s->len_chanlist = 4;
489                 s->range_table  = &me_ao_range;
490                 s->insn_write   = me_ao_insn_write;
491
492                 ret = comedi_alloc_subdev_readback(s);
493                 if (ret)
494                         return ret;
495         } else {
496                 s->type = COMEDI_SUBD_UNUSED;
497         }
498
499         s = &dev->subdevices[2];
500         s->type         = COMEDI_SUBD_DIO;
501         s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
502         s->n_chan       = 32;
503         s->maxdata      = 1;
504         s->len_chanlist = 32;
505         s->range_table  = &range_digital;
506         s->insn_bits    = me_dio_insn_bits;
507         s->insn_config  = me_dio_insn_config;
508
509         return 0;
510 }
511
512 static void me_detach(struct comedi_device *dev)
513 {
514         struct me_private_data *devpriv = dev->private;
515
516         if (devpriv) {
517                 if (dev->mmio)
518                         me_reset(dev);
519                 if (devpriv->plx_regbase)
520                         iounmap(devpriv->plx_regbase);
521         }
522         comedi_pci_detach(dev);
523 }
524
525 static struct comedi_driver me_daq_driver = {
526         .driver_name    = "me_daq",
527         .module         = THIS_MODULE,
528         .auto_attach    = me_auto_attach,
529         .detach         = me_detach,
530 };
531
532 static int me_daq_pci_probe(struct pci_dev *dev,
533                             const struct pci_device_id *id)
534 {
535         return comedi_pci_auto_config(dev, &me_daq_driver, id->driver_data);
536 }
537
538 static const struct pci_device_id me_daq_pci_table[] = {
539         { PCI_VDEVICE(MEILHAUS, 0x2600), BOARD_ME2600 },
540         { PCI_VDEVICE(MEILHAUS, 0x2000), BOARD_ME2000 },
541         { 0 }
542 };
543 MODULE_DEVICE_TABLE(pci, me_daq_pci_table);
544
545 static struct pci_driver me_daq_pci_driver = {
546         .name           = "me_daq",
547         .id_table       = me_daq_pci_table,
548         .probe          = me_daq_pci_probe,
549         .remove         = comedi_pci_auto_unconfig,
550 };
551 module_comedi_pci_driver(me_daq_driver, me_daq_pci_driver);
552
553 MODULE_AUTHOR("Comedi http://www.comedi.org");
554 MODULE_DESCRIPTION("Comedi low-level driver");
555 MODULE_LICENSE("GPL");
556 MODULE_FIRMWARE(ME2600_FIRMWARE);