ni_mio_cs.c revision 4dc6b15bce9c0635850a9f5c1a493e09d2e8fe61
1/*
2    comedi/drivers/ni_mio_cs.c
3    Hardware driver for NI PCMCIA MIO E series cards
4
5    COMEDI - Linux Control and Measurement Device Interface
6    Copyright (C) 1997-2000 David A. Schleef <ds@schleef.org>
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/*
24Driver: ni_mio_cs
25Description: National Instruments DAQCard E series
26Author: ds
27Status: works
28Devices: [National Instruments] DAQCard-AI-16XE-50 (ni_mio_cs),
29  DAQCard-AI-16E-4, DAQCard-6062E, DAQCard-6024E, DAQCard-6036E
30Updated: Thu Oct 23 19:43:17 CDT 2003
31
32See the notes in the ni_atmio.o driver.
33*/
34/*
35	The real guts of the driver is in ni_mio_common.c, which is
36	included by all the E series drivers.
37
38	References for specifications:
39
40	   341080a.pdf  DAQCard E Series Register Level Programmer Manual
41
42*/
43
44#include "../comedidev.h"
45
46#include <linux/delay.h>
47
48#include "ni_stc.h"
49#include "8255.h"
50
51#include <pcmcia/cs_types.h>
52#include <pcmcia/cs.h>
53#include <pcmcia/cistpl.h>
54#include <pcmcia/ds.h>
55
56#undef DEBUG
57
58#define ATMIO 1
59#undef PCIMIO
60
61/*
62 *  AT specific setup
63 */
64
65#define NI_SIZE 0x20
66
67#define MAX_N_CALDACS 32
68
69static const ni_board ni_boards[] = {
70      {device_id:0x010d,
71	      name:	"DAQCard-ai-16xe-50",
72	      n_adchan:16,
73	      adbits:	16,
74	      ai_fifo_depth:1024,
75	      alwaysdither:0,
76	      gainlkup:ai_gain_8,
77	      ai_speed:5000,
78	      n_aochan:0,
79	      aobits:	0,
80	      ao_fifo_depth:0,
81	      ao_unipolar:0,
82	      num_p0_dio_channels:8,
83	      has_8255:0,
84	      caldac:	{dac8800, dac8043},
85		},
86      {device_id:0x010c,
87	      name:	"DAQCard-ai-16e-4",
88	      n_adchan:16,
89	      adbits:	12,
90	      ai_fifo_depth:1024,
91	      alwaysdither:0,
92	      gainlkup:ai_gain_16,
93	      ai_speed:4000,
94	      n_aochan:0,
95	      aobits:	0,
96	      ao_fifo_depth:0,
97	      ao_unipolar:0,
98	      num_p0_dio_channels:8,
99	      has_8255:0,
100	      caldac:	{mb88341},	/* verified */
101		},
102      {device_id:0x02c4,
103	      name:	"DAQCard-6062E",
104	      n_adchan:16,
105	      adbits:	12,
106	      ai_fifo_depth:8192,
107	      alwaysdither:0,
108	      gainlkup:ai_gain_16,
109	      ai_speed:2000,
110	      n_aochan:2,
111	      aobits:	12,
112	      ao_fifo_depth:2048,
113	      ao_range_table:&range_bipolar10,
114	      ao_unipolar:0,
115	      ao_speed:1176,
116	      num_p0_dio_channels:8,
117	      has_8255:0,
118	      caldac:	{ad8804_debug},	/* verified */
119		},
120      {device_id:0x075e,
121	      name:	"DAQCard-6024E",	/* specs incorrect! */
122	      n_adchan:16,
123	      adbits:	12,
124	      ai_fifo_depth:1024,
125	      alwaysdither:0,
126	      gainlkup:ai_gain_16,
127	      ai_speed:5000,
128	      n_aochan:2,
129	      aobits:	12,
130	      ao_fifo_depth:0,
131	      ao_range_table:&range_bipolar10,
132	      ao_unipolar:0,
133	      ao_speed:1000000,
134	      num_p0_dio_channels:8,
135	      has_8255:0,
136	      caldac:	{ad8804_debug},
137		},
138      {device_id:0x0245,
139	      name:	"DAQCard-6036E",	/* specs incorrect! */
140	      n_adchan:16,
141	      adbits:	16,
142	      ai_fifo_depth:1024,
143	      alwaysdither:1,
144	      gainlkup:ai_gain_4,
145	      ai_speed:5000,
146	      n_aochan:2,
147	      aobits:	16,
148	      ao_fifo_depth:0,
149	      ao_range_table:&range_bipolar10,
150	      ao_unipolar:0,
151	      ao_speed:1000000,
152	      num_p0_dio_channels:8,
153	      has_8255:0,
154	      caldac:	{ad8804_debug},
155		},
156#if 0
157      {device_id:0x0000,	/* unknown */
158	      name:	"DAQCard-6715",
159	      n_adchan:0,
160	      n_aochan:8,
161	      aobits:	12,
162	      ao_671x:	8192,
163	      num_p0_dio_channels:8,
164	      caldac:	{mb88341, mb88341},
165		},
166#endif
167	/* N.B. Update ni_mio_cs_ids[] when entries added above. */
168};
169
170#define interrupt_pin(a)	0
171
172#define IRQ_POLARITY 1
173
174#define NI_E_IRQ_FLAGS		IRQF_SHARED
175
176typedef struct {
177	struct pcmcia_device *link;
178
179 NI_PRIVATE_COMMON} ni_private;
180#define devpriv ((ni_private *)dev->private)
181
182/* How we access registers */
183
184#define ni_writel(a,b)		(outl((a),(b)+dev->iobase))
185#define ni_readl(a)		(inl((a)+dev->iobase))
186#define ni_writew(a,b)		(outw((a),(b)+dev->iobase))
187#define ni_readw(a)		(inw((a)+dev->iobase))
188#define ni_writeb(a,b)		(outb((a),(b)+dev->iobase))
189#define ni_readb(a)		(inb((a)+dev->iobase))
190
191/* How we access windowed registers */
192
193/* We automatically take advantage of STC registers that can be
194 * read/written directly in the I/O space of the board.  The
195 * DAQCard devices map the low 8 STC registers to iobase+addr*2. */
196
197static void mio_cs_win_out(comedi_device * dev, uint16_t data, int addr)
198{
199	unsigned long flags;
200
201	comedi_spin_lock_irqsave(&devpriv->window_lock, flags);
202	if (addr < 8) {
203		ni_writew(data, addr * 2);
204	} else {
205		ni_writew(addr, Window_Address);
206		ni_writew(data, Window_Data);
207	}
208	comedi_spin_unlock_irqrestore(&devpriv->window_lock, flags);
209}
210
211static uint16_t mio_cs_win_in(comedi_device * dev, int addr)
212{
213	unsigned long flags;
214	uint16_t ret;
215
216	comedi_spin_lock_irqsave(&devpriv->window_lock, flags);
217	if (addr < 8) {
218		ret = ni_readw(addr * 2);
219	} else {
220		ni_writew(addr, Window_Address);
221		ret = ni_readw(Window_Data);
222	}
223	comedi_spin_unlock_irqrestore(&devpriv->window_lock, flags);
224
225	return ret;
226}
227
228static int mio_cs_attach(comedi_device * dev, comedi_devconfig * it);
229static int mio_cs_detach(comedi_device * dev);
230static comedi_driver driver_ni_mio_cs = {
231      driver_name:"ni_mio_cs",
232      module:THIS_MODULE,
233      attach:mio_cs_attach,
234      detach:mio_cs_detach,
235};
236
237#include "ni_mio_common.c"
238
239static int ni_getboardtype(comedi_device * dev, struct pcmcia_device *link);
240
241/* clean up allocated resources */
242/* called when driver is removed */
243static int mio_cs_detach(comedi_device * dev)
244{
245	mio_common_detach(dev);
246
247	/* PCMCIA layer frees the IO region */
248
249	if (dev->irq) {
250		comedi_free_irq(dev->irq, dev);
251	}
252
253	return 0;
254}
255
256static void mio_cs_config(struct pcmcia_device *link);
257static void cs_release(struct pcmcia_device *link);
258static void cs_detach(struct pcmcia_device *);
259
260static struct pcmcia_device *cur_dev = NULL;
261static const dev_info_t dev_info = "ni_mio_cs";
262static dev_node_t dev_node = {
263	"ni_mio_cs",
264	COMEDI_MAJOR, 0,
265	NULL
266};
267static int cs_attach(struct pcmcia_device *link)
268{
269	link->io.Attributes1 = IO_DATA_PATH_WIDTH_16;
270	link->io.NumPorts1 = 16;
271	link->irq.Attributes = IRQ_TYPE_EXCLUSIVE;
272	link->irq.IRQInfo1 = IRQ_LEVEL_ID;
273	link->conf.Attributes = CONF_ENABLE_IRQ;
274	link->conf.IntType = INT_MEMORY_AND_IO;
275
276	cur_dev = link;
277
278	mio_cs_config(link);
279
280	return 0;
281}
282
283static void cs_release(struct pcmcia_device *link)
284{
285	pcmcia_disable_device(link);
286}
287
288static void cs_detach(struct pcmcia_device *link)
289{
290	DPRINTK("cs_detach(link=%p)\n", link);
291
292	if (link->dev_node) {
293		cs_release(link);
294	}
295}
296
297static int mio_cs_suspend(struct pcmcia_device *link)
298{
299	DPRINTK("pm suspend\n");
300
301	return 0;
302}
303
304static int mio_cs_resume(struct pcmcia_device *link)
305{
306	DPRINTK("pm resume\n");
307	return 0;
308}
309
310static void mio_cs_config(struct pcmcia_device *link)
311{
312	tuple_t tuple;
313	u_short buf[128];
314	cisparse_t parse;
315	int manfid = 0, prodid = 0;
316	int ret;
317
318	DPRINTK("mio_cs_config(link=%p)\n", link);
319
320	tuple.TupleData = (cisdata_t *) buf;
321	tuple.TupleOffset = 0;
322	tuple.TupleDataMax = 255;
323	tuple.Attributes = 0;
324
325	tuple.DesiredTuple = CISTPL_CONFIG;
326	ret = pcmcia_get_first_tuple(link, &tuple);
327	ret = pcmcia_get_tuple_data(link, &tuple);
328	ret = pcmcia_parse_tuple(&tuple, &parse);
329	link->conf.ConfigBase = parse.config.base;
330	link->conf.Present = parse.config.rmask[0];
331
332#if 0
333	tuple.DesiredTuple = CISTPL_LONGLINK_MFC;
334	tuple.Attributes = TUPLE_RETURN_COMMON | TUPLE_RETURN_LINK;
335	info->multi(first_tuple(link, &tuple, &parse) == 0);
336#endif
337
338	tuple.DesiredTuple = CISTPL_MANFID;
339	tuple.Attributes = TUPLE_RETURN_COMMON;
340	if ((pcmcia_get_first_tuple(link, &tuple) == 0) &&
341		(pcmcia_get_tuple_data(link, &tuple) == 0)) {
342		manfid = le16_to_cpu(buf[0]);
343		prodid = le16_to_cpu(buf[1]);
344	}
345	//printk("manfid = 0x%04x, 0x%04x\n",manfid,prodid);
346
347	tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
348	tuple.Attributes = 0;
349	ret = pcmcia_get_first_tuple(link, &tuple);
350	ret = pcmcia_get_tuple_data(link, &tuple);
351	ret = pcmcia_parse_tuple(&tuple, &parse);
352
353#if 0
354	printk(" index: 0x%x\n", parse.cftable_entry.index);
355	printk(" flags: 0x%x\n", parse.cftable_entry.flags);
356	printk(" io flags: 0x%x\n", parse.cftable_entry.io.flags);
357	printk(" io nwin: 0x%x\n", parse.cftable_entry.io.nwin);
358	printk(" io base: 0x%x\n", parse.cftable_entry.io.win[0].base);
359	printk(" io len: 0x%x\n", parse.cftable_entry.io.win[0].len);
360	printk(" irq1: 0x%x\n", parse.cftable_entry.irq.IRQInfo1);
361	printk(" irq2: 0x%x\n", parse.cftable_entry.irq.IRQInfo2);
362	printk(" mem flags: 0x%x\n", parse.cftable_entry.mem.flags);
363	printk(" mem nwin: 0x%x\n", parse.cftable_entry.mem.nwin);
364	printk(" subtuples: 0x%x\n", parse.cftable_entry.subtuples);
365#endif
366
367#if 0
368	link->io.NumPorts1 = 0x20;
369	link->io.IOAddrLines = 5;
370	link->io.Attributes1 = IO_DATA_PATH_WIDTH_AUTO;
371#endif
372	link->io.NumPorts1 = parse.cftable_entry.io.win[0].len;
373	link->io.IOAddrLines =
374		parse.cftable_entry.io.flags & CISTPL_IO_LINES_MASK;
375	link->io.NumPorts2 = 0;
376
377	{
378		int base;
379		for (base = 0x000; base < 0x400; base += 0x20) {
380			link->io.BasePort1 = base;
381			ret = pcmcia_request_io(link, &link->io);
382			//printk("RequestIO 0x%02x\n",ret);
383			if (!ret)
384				break;
385		}
386	}
387
388	link->irq.IRQInfo1 = parse.cftable_entry.irq.IRQInfo1;
389	link->irq.IRQInfo2 = parse.cftable_entry.irq.IRQInfo2;
390	ret = pcmcia_request_irq(link, &link->irq);
391	if (ret) {
392		printk("pcmcia_request_irq() returned error: %i\n", ret);
393	}
394	//printk("RequestIRQ 0x%02x\n",ret);
395
396	link->conf.ConfigIndex = 1;
397
398	ret = pcmcia_request_configuration(link, &link->conf);
399	//printk("RequestConfiguration %d\n",ret);
400
401	link->dev_node = &dev_node;
402}
403
404static int mio_cs_attach(comedi_device * dev, comedi_devconfig * it)
405{
406	struct pcmcia_device *link;
407	unsigned int irq;
408	int ret;
409
410	DPRINTK("mio_cs_attach(dev=%p,it=%p)\n", dev, it);
411
412	link = cur_dev;		/* XXX hack */
413	if (!link)
414		return -EIO;
415
416	dev->driver = &driver_ni_mio_cs;
417	dev->iobase = link->io.BasePort1;
418
419	irq = link->irq.AssignedIRQ;
420
421	printk("comedi%d: %s: DAQCard: io 0x%04lx, irq %u, ",
422		dev->minor, dev->driver->driver_name, dev->iobase, irq);
423
424#if 0
425	{
426		int i;
427
428		printk(" board fingerprint:");
429		for (i = 0; i < 32; i += 2) {
430			printk(" %04x %02x", inw(dev->iobase + i),
431				inb(dev->iobase + i + 1));
432		}
433		printk("\n");
434		printk(" board fingerprint (windowed):");
435		for (i = 0; i < 10; i++) {
436			printk(" 0x%04x", win_in(i));
437		}
438		printk("\n");
439	}
440#endif
441
442	dev->board_ptr = ni_boards + ni_getboardtype(dev, link);
443
444	printk(" %s", boardtype.name);
445	dev->board_name = boardtype.name;
446
447	if ((ret = comedi_request_irq(irq, ni_E_interrupt, NI_E_IRQ_FLAGS,
448				"ni_mio_cs", dev)) < 0) {
449		printk(" irq not available\n");
450		return -EINVAL;
451	}
452	dev->irq = irq;
453
454	/* allocate private area */
455	if ((ret = ni_alloc_private(dev)) < 0)
456		return ret;
457	devpriv->stc_writew = &mio_cs_win_out;
458	devpriv->stc_readw = &mio_cs_win_in;
459	devpriv->stc_writel = &win_out2;
460	devpriv->stc_readl = &win_in2;
461
462	if ((ret = ni_E_init(dev, it)) < 0) {
463		return ret;
464	}
465
466	return 0;
467}
468
469static int get_prodid(comedi_device * dev, struct pcmcia_device *link)
470{
471	tuple_t tuple;
472	u_short buf[128];
473	int prodid = 0;
474
475	tuple.TupleData = (cisdata_t *) buf;
476	tuple.TupleOffset = 0;
477	tuple.TupleDataMax = 255;
478	tuple.DesiredTuple = CISTPL_MANFID;
479	tuple.Attributes = TUPLE_RETURN_COMMON;
480	if ((pcmcia_get_first_tuple(link, &tuple) == 0) &&
481		(pcmcia_get_tuple_data(link, &tuple) == 0)) {
482		prodid = le16_to_cpu(buf[1]);
483	}
484
485	return prodid;
486}
487
488static int ni_getboardtype(comedi_device * dev, struct pcmcia_device *link)
489{
490	int id;
491	int i;
492
493	id = get_prodid(dev, link);
494
495	for (i = 0; i < n_ni_boards; i++) {
496		if (ni_boards[i].device_id == id) {
497			return i;
498		}
499	}
500
501	printk("unknown board 0x%04x -- pretend it is a ", id);
502
503	return 0;
504}
505
506#ifdef MODULE
507
508MODULE_LICENSE("GPL");
509
510static struct pcmcia_device_id ni_mio_cs_ids[] = {
511	PCMCIA_DEVICE_MANF_CARD(0x010b, 0x010d),	/* DAQCard-ai-16xe-50 */
512	PCMCIA_DEVICE_MANF_CARD(0x010b, 0x010c),	/* DAQCard-ai-16e-4 */
513	PCMCIA_DEVICE_MANF_CARD(0x010b, 0x02c4),	/* DAQCard-6062E */
514	PCMCIA_DEVICE_MANF_CARD(0x010b, 0x075e),	/* DAQCard-6024E */
515	PCMCIA_DEVICE_MANF_CARD(0x010b, 0x0245),	/* DAQCard-6036E */
516	PCMCIA_DEVICE_NULL
517};
518
519MODULE_DEVICE_TABLE(pcmcia, ni_mio_cs_ids);
520
521struct pcmcia_driver ni_mio_cs_driver = {
522	.probe = &cs_attach,
523	.remove = &cs_detach,
524	.suspend = &mio_cs_suspend,
525	.resume = &mio_cs_resume,
526	.id_table = ni_mio_cs_ids,
527	.owner = THIS_MODULE,
528	.drv = {
529			.name = dev_info,
530		},
531};
532
533int init_module(void)
534{
535	pcmcia_register_driver(&ni_mio_cs_driver);
536	comedi_driver_register(&driver_ni_mio_cs);
537	return 0;
538}
539
540void cleanup_module(void)
541{
542	pcmcia_unregister_driver(&ni_mio_cs_driver);
543#if 0
544	while (cur_dev != NULL)
545		cs_detach(cur_dev->handle);
546#endif
547	comedi_driver_unregister(&driver_ni_mio_cs);
548}
549#endif
550