2 comedi/drivers/ni_mio_cs.c
3 Hardware driver for NI PCMCIA MIO E series cards
5 COMEDI - Linux Control and Measurement Device Interface
6 Copyright (C) 1997-2000 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.
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.
25 Description: National Instruments DAQCard E series
28 Devices: [National Instruments] DAQCard-AI-16XE-50 (ni_mio_cs),
29 DAQCard-AI-16E-4, DAQCard-6062E, DAQCard-6024E, DAQCard-6036E
30 Updated: Thu Oct 23 19:43:17 CDT 2003
32 See the notes in the ni_atmio.o driver.
35 The real guts of the driver is in ni_mio_common.c, which is
36 included by all the E series drivers.
38 References for specifications:
40 341080a.pdf DAQCard E Series Register Level Programmer Manual
44 #include "../comedidev.h"
46 #include <linux/delay.h>
51 #include <pcmcia/cs_types.h>
52 #include <pcmcia/cs.h>
53 #include <pcmcia/cistpl.h>
54 #include <pcmcia/ds.h>
67 #define MAX_N_CALDACS 32
69 static const struct ni_board_struct ni_boards
[] = {
71 name
: "DAQCard-ai-16xe-50",
82 num_p0_dio_channels
:8,
84 caldac
: {dac8800
, dac8043
},
87 name
: "DAQCard-ai-16e-4",
98 num_p0_dio_channels
:8,
100 caldac
: {mb88341
}, /* verified */
103 name
: "DAQCard-6062E",
113 ao_range_table
:&range_bipolar10
,
116 num_p0_dio_channels
:8,
118 caldac
: {ad8804_debug
}, /* verified */
121 name
: "DAQCard-6024E", /* specs incorrect! */
131 ao_range_table
:&range_bipolar10
,
134 num_p0_dio_channels
:8,
136 caldac
: {ad8804_debug
},
139 name
: "DAQCard-6036E", /* specs incorrect! */
149 ao_range_table
:&range_bipolar10
,
152 num_p0_dio_channels
:8,
154 caldac
: {ad8804_debug
},
157 {device_id
:0x0000, /* unknown */
158 name
: "DAQCard-6715",
163 num_p0_dio_channels
:8,
164 caldac
: {mb88341
, mb88341
},
167 /* N.B. Update ni_mio_cs_ids[] when entries added above. */
170 #define interrupt_pin(a) 0
172 #define IRQ_POLARITY 1
174 #define NI_E_IRQ_FLAGS IRQF_SHARED
178 struct pcmcia_device
*link
;
182 #define devpriv ((struct ni_private *)dev->private)
184 /* How we access registers */
186 #define ni_writel(a,b) (outl((a),(b)+dev->iobase))
187 #define ni_readl(a) (inl((a)+dev->iobase))
188 #define ni_writew(a,b) (outw((a),(b)+dev->iobase))
189 #define ni_readw(a) (inw((a)+dev->iobase))
190 #define ni_writeb(a,b) (outb((a),(b)+dev->iobase))
191 #define ni_readb(a) (inb((a)+dev->iobase))
193 /* How we access windowed registers */
195 /* We automatically take advantage of STC registers that can be
196 * read/written directly in the I/O space of the board. The
197 * DAQCard devices map the low 8 STC registers to iobase+addr*2. */
199 static void mio_cs_win_out(struct comedi_device
* dev
, uint16_t data
, int addr
)
203 comedi_spin_lock_irqsave(&devpriv
->window_lock
, flags
);
205 ni_writew(data
, addr
* 2);
207 ni_writew(addr
, Window_Address
);
208 ni_writew(data
, Window_Data
);
210 comedi_spin_unlock_irqrestore(&devpriv
->window_lock
, flags
);
213 static uint16_t mio_cs_win_in(struct comedi_device
* dev
, int addr
)
218 comedi_spin_lock_irqsave(&devpriv
->window_lock
, flags
);
220 ret
= ni_readw(addr
* 2);
222 ni_writew(addr
, Window_Address
);
223 ret
= ni_readw(Window_Data
);
225 comedi_spin_unlock_irqrestore(&devpriv
->window_lock
, flags
);
230 static int mio_cs_attach(struct comedi_device
* dev
, struct comedi_devconfig
* it
);
231 static int mio_cs_detach(struct comedi_device
* dev
);
232 static struct comedi_driver driver_ni_mio_cs
= {
233 driver_name
:"ni_mio_cs",
235 attach
:mio_cs_attach
,
236 detach
:mio_cs_detach
,
239 #include "ni_mio_common.c"
241 static int ni_getboardtype(struct comedi_device
* dev
, struct pcmcia_device
*link
);
243 /* clean up allocated resources */
244 /* called when driver is removed */
245 static int mio_cs_detach(struct comedi_device
* dev
)
247 mio_common_detach(dev
);
249 /* PCMCIA layer frees the IO region */
252 comedi_free_irq(dev
->irq
, dev
);
258 static void mio_cs_config(struct pcmcia_device
*link
);
259 static void cs_release(struct pcmcia_device
*link
);
260 static void cs_detach(struct pcmcia_device
*);
262 static struct pcmcia_device
*cur_dev
= NULL
;
263 static const dev_info_t dev_info
= "ni_mio_cs";
264 static dev_node_t dev_node
= {
269 static int cs_attach(struct pcmcia_device
*link
)
271 link
->io
.Attributes1
= IO_DATA_PATH_WIDTH_16
;
272 link
->io
.NumPorts1
= 16;
273 link
->irq
.Attributes
= IRQ_TYPE_EXCLUSIVE
;
274 link
->irq
.IRQInfo1
= IRQ_LEVEL_ID
;
275 link
->conf
.Attributes
= CONF_ENABLE_IRQ
;
276 link
->conf
.IntType
= INT_MEMORY_AND_IO
;
285 static void cs_release(struct pcmcia_device
*link
)
287 pcmcia_disable_device(link
);
290 static void cs_detach(struct pcmcia_device
*link
)
292 DPRINTK("cs_detach(link=%p)\n", link
);
294 if (link
->dev_node
) {
299 static int mio_cs_suspend(struct pcmcia_device
*link
)
301 DPRINTK("pm suspend\n");
306 static int mio_cs_resume(struct pcmcia_device
*link
)
308 DPRINTK("pm resume\n");
312 static void mio_cs_config(struct pcmcia_device
*link
)
317 int manfid
= 0, prodid
= 0;
320 DPRINTK("mio_cs_config(link=%p)\n", link
);
322 tuple
.TupleData
= (cisdata_t
*) buf
;
323 tuple
.TupleOffset
= 0;
324 tuple
.TupleDataMax
= 255;
325 tuple
.Attributes
= 0;
327 tuple
.DesiredTuple
= CISTPL_CONFIG
;
328 ret
= pcmcia_get_first_tuple(link
, &tuple
);
329 ret
= pcmcia_get_tuple_data(link
, &tuple
);
330 ret
= pcmcia_parse_tuple(&tuple
, &parse
);
331 link
->conf
.ConfigBase
= parse
.config
.base
;
332 link
->conf
.Present
= parse
.config
.rmask
[0];
335 tuple
.DesiredTuple
= CISTPL_LONGLINK_MFC
;
336 tuple
.Attributes
= TUPLE_RETURN_COMMON
| TUPLE_RETURN_LINK
;
337 info
->multi(first_tuple(link
, &tuple
, &parse
) == 0);
340 tuple
.DesiredTuple
= CISTPL_MANFID
;
341 tuple
.Attributes
= TUPLE_RETURN_COMMON
;
342 if ((pcmcia_get_first_tuple(link
, &tuple
) == 0) &&
343 (pcmcia_get_tuple_data(link
, &tuple
) == 0)) {
344 manfid
= le16_to_cpu(buf
[0]);
345 prodid
= le16_to_cpu(buf
[1]);
347 /* printk("manfid = 0x%04x, 0x%04x\n",manfid,prodid); */
349 tuple
.DesiredTuple
= CISTPL_CFTABLE_ENTRY
;
350 tuple
.Attributes
= 0;
351 ret
= pcmcia_get_first_tuple(link
, &tuple
);
352 ret
= pcmcia_get_tuple_data(link
, &tuple
);
353 ret
= pcmcia_parse_tuple(&tuple
, &parse
);
356 printk(" index: 0x%x\n", parse
.cftable_entry
.index
);
357 printk(" flags: 0x%x\n", parse
.cftable_entry
.flags
);
358 printk(" io flags: 0x%x\n", parse
.cftable_entry
.io
.flags
);
359 printk(" io nwin: 0x%x\n", parse
.cftable_entry
.io
.nwin
);
360 printk(" io base: 0x%x\n", parse
.cftable_entry
.io
.win
[0].base
);
361 printk(" io len: 0x%x\n", parse
.cftable_entry
.io
.win
[0].len
);
362 printk(" irq1: 0x%x\n", parse
.cftable_entry
.irq
.IRQInfo1
);
363 printk(" irq2: 0x%x\n", parse
.cftable_entry
.irq
.IRQInfo2
);
364 printk(" mem flags: 0x%x\n", parse
.cftable_entry
.mem
.flags
);
365 printk(" mem nwin: 0x%x\n", parse
.cftable_entry
.mem
.nwin
);
366 printk(" subtuples: 0x%x\n", parse
.cftable_entry
.subtuples
);
370 link
->io
.NumPorts1
= 0x20;
371 link
->io
.IOAddrLines
= 5;
372 link
->io
.Attributes1
= IO_DATA_PATH_WIDTH_AUTO
;
374 link
->io
.NumPorts1
= parse
.cftable_entry
.io
.win
[0].len
;
375 link
->io
.IOAddrLines
=
376 parse
.cftable_entry
.io
.flags
& CISTPL_IO_LINES_MASK
;
377 link
->io
.NumPorts2
= 0;
381 for (base
= 0x000; base
< 0x400; base
+= 0x20) {
382 link
->io
.BasePort1
= base
;
383 ret
= pcmcia_request_io(link
, &link
->io
);
384 /* printk("RequestIO 0x%02x\n",ret); */
390 link
->irq
.IRQInfo1
= parse
.cftable_entry
.irq
.IRQInfo1
;
391 link
->irq
.IRQInfo2
= parse
.cftable_entry
.irq
.IRQInfo2
;
392 ret
= pcmcia_request_irq(link
, &link
->irq
);
394 printk("pcmcia_request_irq() returned error: %i\n", ret
);
396 /* printk("RequestIRQ 0x%02x\n",ret); */
398 link
->conf
.ConfigIndex
= 1;
400 ret
= pcmcia_request_configuration(link
, &link
->conf
);
401 /* printk("RequestConfiguration %d\n",ret); */
403 link
->dev_node
= &dev_node
;
406 static int mio_cs_attach(struct comedi_device
* dev
, struct comedi_devconfig
* it
)
408 struct pcmcia_device
*link
;
412 DPRINTK("mio_cs_attach(dev=%p,it=%p)\n", dev
, it
);
414 link
= cur_dev
; /* XXX hack */
418 dev
->driver
= &driver_ni_mio_cs
;
419 dev
->iobase
= link
->io
.BasePort1
;
421 irq
= link
->irq
.AssignedIRQ
;
423 printk("comedi%d: %s: DAQCard: io 0x%04lx, irq %u, ",
424 dev
->minor
, dev
->driver
->driver_name
, dev
->iobase
, irq
);
430 printk(" board fingerprint:");
431 for (i
= 0; i
< 32; i
+= 2) {
432 printk(" %04x %02x", inw(dev
->iobase
+ i
),
433 inb(dev
->iobase
+ i
+ 1));
436 printk(" board fingerprint (windowed):");
437 for (i
= 0; i
< 10; i
++) {
438 printk(" 0x%04x", win_in(i
));
444 dev
->board_ptr
= ni_boards
+ ni_getboardtype(dev
, link
);
446 printk(" %s", boardtype
.name
);
447 dev
->board_name
= boardtype
.name
;
449 if ((ret
= comedi_request_irq(irq
, ni_E_interrupt
, NI_E_IRQ_FLAGS
,
450 "ni_mio_cs", dev
)) < 0) {
451 printk(" irq not available\n");
456 /* allocate private area */
457 if ((ret
= ni_alloc_private(dev
)) < 0)
459 devpriv
->stc_writew
= &mio_cs_win_out
;
460 devpriv
->stc_readw
= &mio_cs_win_in
;
461 devpriv
->stc_writel
= &win_out2
;
462 devpriv
->stc_readl
= &win_in2
;
464 if ((ret
= ni_E_init(dev
, it
)) < 0) {
471 static int get_prodid(struct comedi_device
* dev
, struct pcmcia_device
*link
)
477 tuple
.TupleData
= (cisdata_t
*) buf
;
478 tuple
.TupleOffset
= 0;
479 tuple
.TupleDataMax
= 255;
480 tuple
.DesiredTuple
= CISTPL_MANFID
;
481 tuple
.Attributes
= TUPLE_RETURN_COMMON
;
482 if ((pcmcia_get_first_tuple(link
, &tuple
) == 0) &&
483 (pcmcia_get_tuple_data(link
, &tuple
) == 0)) {
484 prodid
= le16_to_cpu(buf
[1]);
490 static int ni_getboardtype(struct comedi_device
* dev
, struct pcmcia_device
*link
)
495 id
= get_prodid(dev
, link
);
497 for (i
= 0; i
< n_ni_boards
; i
++) {
498 if (ni_boards
[i
].device_id
== id
) {
503 printk("unknown board 0x%04x -- pretend it is a ", id
);
510 MODULE_LICENSE("GPL");
512 static struct pcmcia_device_id ni_mio_cs_ids
[] = {
513 PCMCIA_DEVICE_MANF_CARD(0x010b, 0x010d), /* DAQCard-ai-16xe-50 */
514 PCMCIA_DEVICE_MANF_CARD(0x010b, 0x010c), /* DAQCard-ai-16e-4 */
515 PCMCIA_DEVICE_MANF_CARD(0x010b, 0x02c4), /* DAQCard-6062E */
516 PCMCIA_DEVICE_MANF_CARD(0x010b, 0x075e), /* DAQCard-6024E */
517 PCMCIA_DEVICE_MANF_CARD(0x010b, 0x0245), /* DAQCard-6036E */
521 MODULE_DEVICE_TABLE(pcmcia
, ni_mio_cs_ids
);
523 struct pcmcia_driver ni_mio_cs_driver
= {
525 .remove
= &cs_detach
,
526 .suspend
= &mio_cs_suspend
,
527 .resume
= &mio_cs_resume
,
528 .id_table
= ni_mio_cs_ids
,
529 .owner
= THIS_MODULE
,
535 int init_module(void)
537 pcmcia_register_driver(&ni_mio_cs_driver
);
538 comedi_driver_register(&driver_ni_mio_cs
);
542 void cleanup_module(void)
544 pcmcia_unregister_driver(&ni_mio_cs_driver
);
546 while (cur_dev
!= NULL
)
547 cs_detach(cur_dev
->handle
);
549 comedi_driver_unregister(&driver_ni_mio_cs
);