ni_atmio.c revision 5f74ea14c07fee91d3bdbaad88bff6264c6200e6
1/*
2    comedi/drivers/ni_atmio.c
3    Hardware driver for NI AT-MIO E series cards
4
5    COMEDI - Linux Control and Measurement Device Interface
6    Copyright (C) 1997-2001 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/*
23Driver: ni_atmio
24Description: National Instruments AT-MIO-E series
25Author: ds
26Devices: [National Instruments] AT-MIO-16E-1 (ni_atmio),
27  AT-MIO-16E-2, AT-MIO-16E-10, AT-MIO-16DE-10, AT-MIO-64E-3,
28  AT-MIO-16XE-50, AT-MIO-16XE-10, AT-AI-16XE-10
29Status: works
30Updated: Thu May  1 20:03:02 CDT 2003
31
32The driver has 2.6 kernel isapnp support, and
33will automatically probe for a supported board if the
34I/O base is left unspecified with comedi_config.
35However, many of
36the isapnp id numbers are unknown.  If your board is not
37recognized, please send the output of 'cat /proc/isapnp'
38(you may need to modprobe the isa-pnp module for
39/proc/isapnp to exist) so the
40id numbers for your board can be added to the driver.
41
42Otherwise, you can use the isapnptools package to configure
43your board.  Use isapnp to
44configure the I/O base and IRQ for the board, and then pass
45the same values as
46parameters in comedi_config.  A sample isapnp.conf file is included
47in the etc/ directory of Comedilib.
48
49Comedilib includes a utility to autocalibrate these boards.  The
50boards seem to boot into a state where the all calibration DACs
51are at one extreme of their range, thus the default calibration
52is terrible.  Calibration at boot is strongly encouraged.
53
54To use the extended digital I/O on some of the boards, enable the
558255 driver when configuring the Comedi source tree.
56
57External triggering is supported for some events.  The channel index
58(scan_begin_arg, etc.) maps to PFI0 - PFI9.
59
60Some of the more esoteric triggering possibilities of these boards
61are not supported.
62*/
63/*
64	The real guts of the driver is in ni_mio_common.c, which is included
65	both here and in ni_pcimio.c
66
67	Interrupt support added by Truxton Fulton <trux@truxton.com>
68
69	References for specifications:
70
71	   340747b.pdf  Register Level Programmer Manual (obsolete)
72	   340747c.pdf  Register Level Programmer Manual (new)
73	   DAQ-STC reference manual
74
75	Other possibly relevant info:
76
77	   320517c.pdf  User manual (obsolete)
78	   320517f.pdf  User manual (new)
79	   320889a.pdf  delete
80	   320906c.pdf  maximum signal ratings
81	   321066a.pdf  about 16x
82	   321791a.pdf  discontinuation of at-mio-16e-10 rev. c
83	   321808a.pdf  about at-mio-16e-10 rev P
84	   321837a.pdf  discontinuation of at-mio-16de-10 rev d
85	   321838a.pdf  about at-mio-16de-10 rev N
86
87	ISSUES:
88
89	need to deal with external reference for DAC, and other DAC
90	properties in board properties
91
92	deal with at-mio-16de-10 revision D to N changes, etc.
93
94*/
95
96#include "../comedidev.h"
97
98#include <linux/delay.h>
99#include <linux/isapnp.h>
100
101#include "ni_stc.h"
102#include "8255.h"
103
104#undef DEBUG
105
106#define ATMIO 1
107#undef PCIMIO
108
109/*
110 *  AT specific setup
111 */
112
113#define NI_SIZE 0x20
114
115#define MAX_N_CALDACS 32
116
117static const struct ni_board_struct ni_boards[] = {
118	{.device_id = 44,
119	.isapnp_id = 0x0000,/* XXX unknown */
120	.name = "at-mio-16e-1",
121	.n_adchan = 16,
122	.adbits = 12,
123	.ai_fifo_depth = 8192,
124	.alwaysdither = 0,
125	.gainlkup = ai_gain_16,
126	.ai_speed = 800,
127	.n_aochan = 2,
128	.aobits = 12,
129	.ao_fifo_depth = 2048,
130			.ao_range_table = &range_ni_E_ao_ext,
131	.ao_unipolar = 1,
132	.ao_speed = 1000,
133	.has_8255 = 0,
134			.num_p0_dio_channels = 8,
135	.caldac = {mb88341},
136		},
137	{.device_id = 25,
138	.isapnp_id = 0x1900,
139	.name = "at-mio-16e-2",
140	.n_adchan = 16,
141	.adbits = 12,
142	.ai_fifo_depth = 2048,
143	.alwaysdither = 0,
144	.gainlkup = ai_gain_16,
145	.ai_speed = 2000,
146	.n_aochan = 2,
147	.aobits = 12,
148	.ao_fifo_depth = 2048,
149			.ao_range_table = &range_ni_E_ao_ext,
150	.ao_unipolar = 1,
151	.ao_speed = 1000,
152	.has_8255 = 0,
153			.num_p0_dio_channels = 8,
154	.caldac = {mb88341},
155		},
156	{.device_id = 36,
157	.isapnp_id = 0x2400,
158	.name = "at-mio-16e-10",
159	.n_adchan = 16,
160	.adbits = 12,
161	.ai_fifo_depth = 512,
162	.alwaysdither = 0,
163	.gainlkup = ai_gain_16,
164	.ai_speed = 10000,
165	.n_aochan = 2,
166	.aobits = 12,
167	.ao_fifo_depth = 0,
168			.ao_range_table = &range_ni_E_ao_ext,
169	.ao_unipolar = 1,
170	.ao_speed = 10000,
171			.num_p0_dio_channels = 8,
172	.caldac = {ad8804_debug},
173	.has_8255 = 0,
174		},
175	{.device_id = 37,
176	.isapnp_id = 0x2500,
177	.name = "at-mio-16de-10",
178	.n_adchan = 16,
179	.adbits = 12,
180	.ai_fifo_depth = 512,
181	.alwaysdither = 0,
182	.gainlkup = ai_gain_16,
183	.ai_speed = 10000,
184	.n_aochan = 2,
185	.aobits = 12,
186	.ao_fifo_depth = 0,
187			.ao_range_table = &range_ni_E_ao_ext,
188	.ao_unipolar = 1,
189	.ao_speed = 10000,
190			.num_p0_dio_channels = 8,
191	.caldac = {ad8804_debug},
192	.has_8255 = 1,
193		},
194	{.device_id = 38,
195	.isapnp_id = 0x2600,
196	.name = "at-mio-64e-3",
197	.n_adchan = 64,
198	.adbits = 12,
199	.ai_fifo_depth = 2048,
200	.alwaysdither = 0,
201	.gainlkup = ai_gain_16,
202	.ai_speed = 2000,
203	.n_aochan = 2,
204	.aobits = 12,
205	.ao_fifo_depth = 2048,
206			.ao_range_table = &range_ni_E_ao_ext,
207	.ao_unipolar = 1,
208	.ao_speed = 1000,
209	.has_8255 = 0,
210			.num_p0_dio_channels = 8,
211	.caldac = {ad8804_debug},
212		},
213	{.device_id = 39,
214	.isapnp_id = 0x2700,
215	.name = "at-mio-16xe-50",
216	.n_adchan = 16,
217	.adbits = 16,
218	.ai_fifo_depth = 512,
219	.alwaysdither = 1,
220	.gainlkup = ai_gain_8,
221	.ai_speed = 50000,
222	.n_aochan = 2,
223	.aobits = 12,
224	.ao_fifo_depth = 0,
225			.ao_range_table = &range_bipolar10,
226	.ao_unipolar = 0,
227	.ao_speed = 50000,
228			.num_p0_dio_channels = 8,
229	.caldac = {dac8800, dac8043},
230	.has_8255 = 0,
231		},
232	{.device_id = 50,
233	.isapnp_id = 0x0000,/* XXX unknown */
234	.name = "at-mio-16xe-10",
235	.n_adchan = 16,
236	.adbits = 16,
237	.ai_fifo_depth = 512,
238	.alwaysdither = 1,
239	.gainlkup = ai_gain_14,
240	.ai_speed = 10000,
241	.n_aochan = 2,
242	.aobits = 16,
243	.ao_fifo_depth = 2048,
244			.ao_range_table = &range_ni_E_ao_ext,
245	.ao_unipolar = 1,
246	.ao_speed = 1000,
247			.num_p0_dio_channels = 8,
248	.caldac = {dac8800, dac8043, ad8522},
249	.has_8255 = 0,
250		},
251	{.device_id = 51,
252	.isapnp_id = 0x0000,/* XXX unknown */
253	.name = "at-ai-16xe-10",
254	.n_adchan = 16,
255	.adbits = 16,
256	.ai_fifo_depth = 512,
257	.alwaysdither = 1,	/* unknown */
258	.gainlkup = ai_gain_14,
259	.ai_speed = 10000,
260	.n_aochan = 0,
261	.aobits = 0,
262	.ao_fifo_depth = 0,
263	.ao_unipolar = 0,
264			.num_p0_dio_channels = 8,
265	.caldac = {dac8800, dac8043, ad8522},
266	.has_8255 = 0,
267		}
268};
269
270static const int ni_irqpin[] =
271	{ -1, -1, -1, 0, 1, 2, -1, 3, -1, -1, 4, 5, 6, -1, -1, 7 };
272
273#define interrupt_pin(a)	(ni_irqpin[(a)])
274
275#define IRQ_POLARITY 0
276
277#define NI_E_IRQ_FLAGS		0
278
279struct ni_private {
280	struct pnp_dev *isapnp_dev;
281	NI_PRIVATE_COMMON
282};
283#define devpriv ((struct ni_private *)dev->private)
284
285/* How we access registers */
286
287#define ni_writel(a, b)		(outl((a), (b)+dev->iobase))
288#define ni_readl(a)		(inl((a)+dev->iobase))
289#define ni_writew(a, b)		(outw((a), (b)+dev->iobase))
290#define ni_readw(a)		(inw((a)+dev->iobase))
291#define ni_writeb(a, b)		(outb((a), (b)+dev->iobase))
292#define ni_readb(a)		(inb((a)+dev->iobase))
293
294/* How we access windowed registers */
295
296/* We automatically take advantage of STC registers that can be
297 * read/written directly in the I/O space of the board.  The
298 * AT-MIO devices map the low 8 STC registers to iobase+addr*2. */
299
300static void ni_atmio_win_out(struct comedi_device *dev, uint16_t data, int addr)
301{
302	unsigned long flags;
303
304	spin_lock_irqsave(&devpriv->window_lock, flags);
305	if ((addr) < 8) {
306		ni_writew(data, addr * 2);
307	} else {
308		ni_writew(addr, Window_Address);
309		ni_writew(data, Window_Data);
310	}
311	spin_unlock_irqrestore(&devpriv->window_lock, flags);
312}
313
314static uint16_t ni_atmio_win_in(struct comedi_device *dev, int addr)
315{
316	unsigned long flags;
317	uint16_t ret;
318
319	spin_lock_irqsave(&devpriv->window_lock, flags);
320	if (addr < 8) {
321		ret = ni_readw(addr * 2);
322	} else {
323		ni_writew(addr, Window_Address);
324		ret = ni_readw(Window_Data);
325	}
326	spin_unlock_irqrestore(&devpriv->window_lock, flags);
327
328	return ret;
329}
330
331static struct pnp_device_id device_ids[] = {
332	{.id = "NIC1900", .driver_data = 0},
333	{.id = "NIC2400", .driver_data = 0},
334	{.id = "NIC2500", .driver_data = 0},
335	{.id = "NIC2600", .driver_data = 0},
336	{.id = "NIC2700", .driver_data = 0},
337	{.id = ""}
338};
339
340MODULE_DEVICE_TABLE(pnp, device_ids);
341
342static int ni_atmio_attach(struct comedi_device *dev, struct comedi_devconfig *it);
343static int ni_atmio_detach(struct comedi_device *dev);
344static struct comedi_driver driver_atmio = {
345	.driver_name = "ni_atmio",
346	.module = THIS_MODULE,
347	.attach = ni_atmio_attach,
348	.detach = ni_atmio_detach,
349};
350
351COMEDI_INITCLEANUP(driver_atmio);
352
353#include "ni_mio_common.c"
354
355static int ni_getboardtype(struct comedi_device *dev);
356
357/* clean up allocated resources */
358static int ni_atmio_detach(struct comedi_device *dev)
359{
360	mio_common_detach(dev);
361
362	if (dev->iobase)
363		release_region(dev->iobase, NI_SIZE);
364	if (dev->irq) {
365		free_irq(dev->irq, dev);
366	}
367	if (devpriv->isapnp_dev)
368		pnp_device_detach(devpriv->isapnp_dev);
369
370	return 0;
371}
372
373static int ni_isapnp_find_board(struct pnp_dev **dev)
374{
375	struct pnp_dev *isapnp_dev = NULL;
376	int i;
377
378	for (i = 0; i < n_ni_boards; i++) {
379		isapnp_dev = pnp_find_dev(NULL,
380			ISAPNP_VENDOR('N', 'I', 'C'),
381			ISAPNP_FUNCTION(ni_boards[i].isapnp_id), NULL);
382
383		if (isapnp_dev == NULL || isapnp_dev->card == NULL)
384			continue;
385
386		if (pnp_device_attach(isapnp_dev) < 0) {
387			printk("ni_atmio: %s found but already active, skipping.\n", ni_boards[i].name);
388			continue;
389		}
390		if (pnp_activate_dev(isapnp_dev) < 0) {
391			pnp_device_detach(isapnp_dev);
392			return -EAGAIN;
393		}
394		if (!pnp_port_valid(isapnp_dev, 0)
395			|| !pnp_irq_valid(isapnp_dev, 0)) {
396			pnp_device_detach(isapnp_dev);
397			printk("ni_atmio: pnp invalid port or irq, aborting\n");
398			return -ENOMEM;
399		}
400		break;
401	}
402	if (i == n_ni_boards)
403		return -ENODEV;
404	*dev = isapnp_dev;
405	return 0;
406}
407
408static int ni_atmio_attach(struct comedi_device *dev, struct comedi_devconfig *it)
409{
410	struct pnp_dev *isapnp_dev;
411	int ret;
412	unsigned long iobase;
413	int board;
414	unsigned int irq;
415
416	/* allocate private area */
417	ret = ni_alloc_private(dev);
418	if (ret < 0)
419		return ret;
420
421	devpriv->stc_writew = &ni_atmio_win_out;
422	devpriv->stc_readw = &ni_atmio_win_in;
423	devpriv->stc_writel = &win_out2;
424	devpriv->stc_readl = &win_in2;
425
426	iobase = it->options[0];
427	irq = it->options[1];
428	isapnp_dev = NULL;
429	if (iobase == 0) {
430		ret = ni_isapnp_find_board(&isapnp_dev);
431		if (ret < 0)
432			return ret;
433
434		iobase = pnp_port_start(isapnp_dev, 0);
435		irq = pnp_irq(isapnp_dev, 0);
436		devpriv->isapnp_dev = isapnp_dev;
437	}
438
439	/* reserve our I/O region */
440
441	printk("comedi%d: ni_atmio: 0x%04lx", dev->minor, iobase);
442	if (!request_region(iobase, NI_SIZE, "ni_atmio")) {
443		printk(" I/O port conflict\n");
444		return -EIO;
445	}
446
447	dev->iobase = iobase;
448
449#ifdef DEBUG
450	/* board existence sanity check */
451	{
452		int i;
453
454		printk(" board fingerprint:");
455		for (i = 0; i < 16; i += 2) {
456			printk(" %04x %02x", inw(dev->iobase + i),
457				inb(dev->iobase + i + 1));
458		}
459	}
460#endif
461
462	/* get board type */
463
464	board = ni_getboardtype(dev);
465	if (board < 0)
466		return -EIO;
467
468	dev->board_ptr = ni_boards + board;
469
470	printk(" %s", boardtype.name);
471	dev->board_name = boardtype.name;
472
473	/* irq stuff */
474
475	if (irq != 0) {
476		if (irq > 15 || ni_irqpin[irq] == -1) {
477			printk(" invalid irq %u\n", irq);
478			return -EINVAL;
479		}
480		printk(" ( irq = %u )", irq);
481		ret = request_irq(irq, ni_E_interrupt, NI_E_IRQ_FLAGS,
482				  "ni_atmio", dev);
483
484		if (ret < 0) {
485			printk(" irq not available\n");
486			return -EINVAL;
487		}
488		dev->irq = irq;
489	}
490
491	/* generic E series stuff in ni_mio_common.c */
492
493	ret = ni_E_init(dev, it);
494	if (ret < 0) {
495		return ret;
496	}
497
498	return 0;
499}
500
501static int ni_getboardtype(struct comedi_device *dev)
502{
503	int device_id = ni_read_eeprom(dev, 511);
504	int i;
505
506	for (i = 0; i < n_ni_boards; i++) {
507		if (ni_boards[i].device_id == device_id) {
508			return i;
509		}
510	}
511	if (device_id == 255) {
512		printk(" can't find board\n");
513	} else if (device_id == 0) {
514		printk(" EEPROM read error (?) or device not found\n");
515	} else {
516		printk(" unknown device ID %d -- contact author\n", device_id);
517	}
518	return -1;
519}
520