Commit | Line | Data |
---|---|---|
4fecf4a2 DS |
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 | /* | |
24 | Driver: ni_mio_cs | |
25 | Description: National Instruments DAQCard E series | |
26 | Author: ds | |
27 | Status: works | |
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 | |
31 | ||
32 | See 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> | |
4fecf4a2 DS |
47 | |
48 | #include "ni_stc.h" | |
49 | #include "8255.h" | |
50 | ||
4fecf4a2 DS |
51 | #include <pcmcia/cistpl.h> |
52 | #include <pcmcia/ds.h> | |
53 | ||
54 | #undef DEBUG | |
55 | ||
56 | #define ATMIO 1 | |
57 | #undef PCIMIO | |
58 | ||
59 | /* | |
60 | * AT specific setup | |
61 | */ | |
62 | ||
63 | #define NI_SIZE 0x20 | |
64 | ||
65 | #define MAX_N_CALDACS 32 | |
66 | ||
72a82280 | 67 | static const struct ni_board_struct ni_boards[] = { |
68c3dbff BP |
68 | {.device_id = 0x010d, |
69 | .name = "DAQCard-ai-16xe-50", | |
70 | .n_adchan = 16, | |
71 | .adbits = 16, | |
72 | .ai_fifo_depth = 1024, | |
73 | .alwaysdither = 0, | |
74 | .gainlkup = ai_gain_8, | |
75 | .ai_speed = 5000, | |
76 | .n_aochan = 0, | |
77 | .aobits = 0, | |
78 | .ao_fifo_depth = 0, | |
79 | .ao_unipolar = 0, | |
80 | .num_p0_dio_channels = 8, | |
81 | .has_8255 = 0, | |
82 | .caldac = {dac8800, dac8043}, | |
0a85b6f0 | 83 | }, |
68c3dbff BP |
84 | {.device_id = 0x010c, |
85 | .name = "DAQCard-ai-16e-4", | |
86 | .n_adchan = 16, | |
87 | .adbits = 12, | |
88 | .ai_fifo_depth = 1024, | |
89 | .alwaysdither = 0, | |
90 | .gainlkup = ai_gain_16, | |
91 | .ai_speed = 4000, | |
92 | .n_aochan = 0, | |
93 | .aobits = 0, | |
94 | .ao_fifo_depth = 0, | |
95 | .ao_unipolar = 0, | |
96 | .num_p0_dio_channels = 8, | |
97 | .has_8255 = 0, | |
98 | .caldac = {mb88341}, /* verified */ | |
0a85b6f0 | 99 | }, |
68c3dbff BP |
100 | {.device_id = 0x02c4, |
101 | .name = "DAQCard-6062E", | |
102 | .n_adchan = 16, | |
103 | .adbits = 12, | |
104 | .ai_fifo_depth = 8192, | |
105 | .alwaysdither = 0, | |
106 | .gainlkup = ai_gain_16, | |
107 | .ai_speed = 2000, | |
108 | .n_aochan = 2, | |
109 | .aobits = 12, | |
110 | .ao_fifo_depth = 2048, | |
111 | .ao_range_table = &range_bipolar10, | |
112 | .ao_unipolar = 0, | |
113 | .ao_speed = 1176, | |
114 | .num_p0_dio_channels = 8, | |
115 | .has_8255 = 0, | |
116 | .caldac = {ad8804_debug}, /* verified */ | |
0a85b6f0 | 117 | }, |
68c3dbff BP |
118 | {.device_id = 0x075e, |
119 | .name = "DAQCard-6024E", /* specs incorrect! */ | |
120 | .n_adchan = 16, | |
121 | .adbits = 12, | |
122 | .ai_fifo_depth = 1024, | |
123 | .alwaysdither = 0, | |
ebe86223 | 124 | .gainlkup = ai_gain_4, |
68c3dbff BP |
125 | .ai_speed = 5000, |
126 | .n_aochan = 2, | |
127 | .aobits = 12, | |
128 | .ao_fifo_depth = 0, | |
129 | .ao_range_table = &range_bipolar10, | |
130 | .ao_unipolar = 0, | |
131 | .ao_speed = 1000000, | |
132 | .num_p0_dio_channels = 8, | |
133 | .has_8255 = 0, | |
134 | .caldac = {ad8804_debug}, | |
0a85b6f0 | 135 | }, |
68c3dbff BP |
136 | {.device_id = 0x0245, |
137 | .name = "DAQCard-6036E", /* specs incorrect! */ | |
138 | .n_adchan = 16, | |
139 | .adbits = 16, | |
140 | .ai_fifo_depth = 1024, | |
141 | .alwaysdither = 1, | |
142 | .gainlkup = ai_gain_4, | |
143 | .ai_speed = 5000, | |
144 | .n_aochan = 2, | |
145 | .aobits = 16, | |
146 | .ao_fifo_depth = 0, | |
147 | .ao_range_table = &range_bipolar10, | |
148 | .ao_unipolar = 0, | |
149 | .ao_speed = 1000000, | |
150 | .num_p0_dio_channels = 8, | |
151 | .has_8255 = 0, | |
152 | .caldac = {ad8804_debug}, | |
0a85b6f0 | 153 | }, |
4fecf4a2 | 154 | #if 0 |
68c3dbff BP |
155 | {.device_id = 0x0000, /* unknown */ |
156 | .name = "DAQCard-6715", | |
157 | .n_adchan = 0, | |
158 | .n_aochan = 8, | |
159 | .aobits = 12, | |
160 | .ao_671x = 8192, | |
161 | .num_p0_dio_channels = 8, | |
162 | .caldac = {mb88341, mb88341}, | |
0a85b6f0 | 163 | }, |
4fecf4a2 DS |
164 | #endif |
165 | /* N.B. Update ni_mio_cs_ids[] when entries added above. */ | |
166 | }; | |
167 | ||
168 | #define interrupt_pin(a) 0 | |
169 | ||
170 | #define IRQ_POLARITY 1 | |
171 | ||
172 | #define NI_E_IRQ_FLAGS IRQF_SHARED | |
173 | ||
c7ef2298 BP |
174 | struct ni_private { |
175 | ||
4fecf4a2 DS |
176 | struct pcmcia_device *link; |
177 | ||
c7ef2298 BP |
178 | NI_PRIVATE_COMMON}; |
179 | ||
180 | #define devpriv ((struct ni_private *)dev->private) | |
4fecf4a2 DS |
181 | |
182 | /* How we access registers */ | |
183 | ||
f7cbd7aa | 184 | #define ni_writel(a, b) (outl((a), (b)+dev->iobase)) |
4fecf4a2 | 185 | #define ni_readl(a) (inl((a)+dev->iobase)) |
f7cbd7aa | 186 | #define ni_writew(a, b) (outw((a), (b)+dev->iobase)) |
4fecf4a2 | 187 | #define ni_readw(a) (inw((a)+dev->iobase)) |
f7cbd7aa | 188 | #define ni_writeb(a, b) (outb((a), (b)+dev->iobase)) |
4fecf4a2 DS |
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 | ||
da91b269 | 197 | static void mio_cs_win_out(struct comedi_device *dev, uint16_t data, int addr) |
4fecf4a2 DS |
198 | { |
199 | unsigned long flags; | |
200 | ||
5f74ea14 | 201 | spin_lock_irqsave(&devpriv->window_lock, flags); |
4fecf4a2 DS |
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 | } | |
5f74ea14 | 208 | spin_unlock_irqrestore(&devpriv->window_lock, flags); |
4fecf4a2 DS |
209 | } |
210 | ||
da91b269 | 211 | static uint16_t mio_cs_win_in(struct comedi_device *dev, int addr) |
4fecf4a2 DS |
212 | { |
213 | unsigned long flags; | |
214 | uint16_t ret; | |
215 | ||
5f74ea14 | 216 | spin_lock_irqsave(&devpriv->window_lock, flags); |
4fecf4a2 DS |
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 | } | |
5f74ea14 | 223 | spin_unlock_irqrestore(&devpriv->window_lock, flags); |
4fecf4a2 DS |
224 | |
225 | return ret; | |
226 | } | |
227 | ||
0a85b6f0 MT |
228 | static int mio_cs_attach(struct comedi_device *dev, |
229 | struct comedi_devconfig *it); | |
da91b269 | 230 | static int mio_cs_detach(struct comedi_device *dev); |
139dfbdf | 231 | static struct comedi_driver driver_ni_mio_cs = { |
68c3dbff BP |
232 | .driver_name = "ni_mio_cs", |
233 | .module = THIS_MODULE, | |
234 | .attach = mio_cs_attach, | |
235 | .detach = mio_cs_detach, | |
4fecf4a2 DS |
236 | }; |
237 | ||
238 | #include "ni_mio_common.c" | |
239 | ||
0a85b6f0 MT |
240 | static int ni_getboardtype(struct comedi_device *dev, |
241 | struct pcmcia_device *link); | |
4fecf4a2 DS |
242 | |
243 | /* clean up allocated resources */ | |
244 | /* called when driver is removed */ | |
da91b269 | 245 | static int mio_cs_detach(struct comedi_device *dev) |
4fecf4a2 DS |
246 | { |
247 | mio_common_detach(dev); | |
248 | ||
249 | /* PCMCIA layer frees the IO region */ | |
250 | ||
b055d0d3 | 251 | if (dev->irq) |
5f74ea14 | 252 | free_irq(dev->irq, dev); |
4fecf4a2 DS |
253 | |
254 | return 0; | |
255 | } | |
256 | ||
257 | static void mio_cs_config(struct pcmcia_device *link); | |
258 | static void cs_release(struct pcmcia_device *link); | |
259 | static void cs_detach(struct pcmcia_device *); | |
260 | ||
261 | static struct pcmcia_device *cur_dev = NULL; | |
0a85b6f0 | 262 | |
4fecf4a2 DS |
263 | static int cs_attach(struct pcmcia_device *link) |
264 | { | |
4fecf4a2 DS |
265 | cur_dev = link; |
266 | ||
267 | mio_cs_config(link); | |
268 | ||
269 | return 0; | |
270 | } | |
271 | ||
272 | static void cs_release(struct pcmcia_device *link) | |
273 | { | |
274 | pcmcia_disable_device(link); | |
275 | } | |
276 | ||
277 | static void cs_detach(struct pcmcia_device *link) | |
278 | { | |
279 | DPRINTK("cs_detach(link=%p)\n", link); | |
280 | ||
bfc2cc34 | 281 | cs_release(link); |
4fecf4a2 DS |
282 | } |
283 | ||
284 | static int mio_cs_suspend(struct pcmcia_device *link) | |
285 | { | |
286 | DPRINTK("pm suspend\n"); | |
287 | ||
288 | return 0; | |
289 | } | |
290 | ||
291 | static int mio_cs_resume(struct pcmcia_device *link) | |
292 | { | |
293 | DPRINTK("pm resume\n"); | |
294 | return 0; | |
295 | } | |
296 | ||
4fecf4a2 | 297 | |
00990e7c | 298 | static int mio_pcmcia_config_loop(struct pcmcia_device *p_dev, void *priv_data) |
55a19b39 DB |
299 | { |
300 | int base, ret; | |
4fecf4a2 | 301 | |
00990e7c DB |
302 | p_dev->resource[0]->flags &= ~IO_DATA_PATH_WIDTH; |
303 | p_dev->resource[0]->flags |= IO_DATA_PATH_WIDTH_16; | |
4fecf4a2 | 304 | |
55a19b39 | 305 | for (base = 0x000; base < 0x400; base += 0x20) { |
90abdc3b DB |
306 | p_dev->resource[0]->start = base; |
307 | ret = pcmcia_request_io(p_dev); | |
55a19b39 DB |
308 | if (!ret) |
309 | return 0; | |
4fecf4a2 | 310 | } |
55a19b39 DB |
311 | return -ENODEV; |
312 | } | |
4fecf4a2 | 313 | |
4fecf4a2 | 314 | |
55a19b39 DB |
315 | static void mio_cs_config(struct pcmcia_device *link) |
316 | { | |
317 | int ret; | |
4fecf4a2 | 318 | |
55a19b39 | 319 | DPRINTK("mio_cs_config(link=%p)\n", link); |
00990e7c | 320 | link->config_flags |= CONF_ENABLE_IRQ | CONF_AUTO_SET_IO; |
4fecf4a2 | 321 | |
55a19b39 DB |
322 | ret = pcmcia_loop_config(link, mio_pcmcia_config_loop, NULL); |
323 | if (ret) { | |
324 | dev_warn(&link->dev, "no configuration found\n"); | |
325 | return; | |
4fecf4a2 DS |
326 | } |
327 | ||
eb14120f DB |
328 | if (!link->irq) |
329 | dev_info(&link->dev, "no IRQ available\n"); | |
4fecf4a2 | 330 | |
1ac71e5a | 331 | ret = pcmcia_enable_device(link); |
4fecf4a2 DS |
332 | } |
333 | ||
da91b269 | 334 | static int mio_cs_attach(struct comedi_device *dev, struct comedi_devconfig *it) |
4fecf4a2 DS |
335 | { |
336 | struct pcmcia_device *link; | |
337 | unsigned int irq; | |
338 | int ret; | |
339 | ||
340 | DPRINTK("mio_cs_attach(dev=%p,it=%p)\n", dev, it); | |
341 | ||
342 | link = cur_dev; /* XXX hack */ | |
343 | if (!link) | |
344 | return -EIO; | |
345 | ||
346 | dev->driver = &driver_ni_mio_cs; | |
9a017a91 | 347 | dev->iobase = link->resource[0]->start; |
4fecf4a2 | 348 | |
eb14120f | 349 | irq = link->irq; |
4fecf4a2 DS |
350 | |
351 | printk("comedi%d: %s: DAQCard: io 0x%04lx, irq %u, ", | |
0a85b6f0 | 352 | dev->minor, dev->driver->driver_name, dev->iobase, irq); |
4fecf4a2 DS |
353 | |
354 | #if 0 | |
355 | { | |
356 | int i; | |
357 | ||
358 | printk(" board fingerprint:"); | |
359 | for (i = 0; i < 32; i += 2) { | |
360 | printk(" %04x %02x", inw(dev->iobase + i), | |
0a85b6f0 | 361 | inb(dev->iobase + i + 1)); |
4fecf4a2 DS |
362 | } |
363 | printk("\n"); | |
364 | printk(" board fingerprint (windowed):"); | |
b055d0d3 | 365 | for (i = 0; i < 10; i++) |
4fecf4a2 | 366 | printk(" 0x%04x", win_in(i)); |
4fecf4a2 DS |
367 | printk("\n"); |
368 | } | |
369 | #endif | |
370 | ||
371 | dev->board_ptr = ni_boards + ni_getboardtype(dev, link); | |
372 | ||
373 | printk(" %s", boardtype.name); | |
374 | dev->board_name = boardtype.name; | |
375 | ||
5f74ea14 | 376 | ret = request_irq(irq, ni_E_interrupt, NI_E_IRQ_FLAGS, |
0a85b6f0 | 377 | "ni_mio_cs", dev); |
c3744138 | 378 | if (ret < 0) { |
4fecf4a2 DS |
379 | printk(" irq not available\n"); |
380 | return -EINVAL; | |
381 | } | |
382 | dev->irq = irq; | |
383 | ||
384 | /* allocate private area */ | |
c3744138 BP |
385 | ret = ni_alloc_private(dev); |
386 | if (ret < 0) | |
4fecf4a2 | 387 | return ret; |
c3744138 | 388 | |
4fecf4a2 DS |
389 | devpriv->stc_writew = &mio_cs_win_out; |
390 | devpriv->stc_readw = &mio_cs_win_in; | |
391 | devpriv->stc_writel = &win_out2; | |
392 | devpriv->stc_readl = &win_in2; | |
393 | ||
c3744138 BP |
394 | ret = ni_E_init(dev, it); |
395 | ||
396 | if (ret < 0) | |
4fecf4a2 | 397 | return ret; |
4fecf4a2 DS |
398 | |
399 | return 0; | |
400 | } | |
401 | ||
0a85b6f0 MT |
402 | static int ni_getboardtype(struct comedi_device *dev, |
403 | struct pcmcia_device *link) | |
4fecf4a2 | 404 | { |
4fecf4a2 DS |
405 | int i; |
406 | ||
4fecf4a2 | 407 | for (i = 0; i < n_ni_boards; i++) { |
55a19b39 | 408 | if (ni_boards[i].device_id == link->card_id) |
4fecf4a2 | 409 | return i; |
4fecf4a2 DS |
410 | } |
411 | ||
55a19b39 | 412 | printk("unknown board 0x%04x -- pretend it is a ", link->card_id); |
4fecf4a2 DS |
413 | |
414 | return 0; | |
415 | } | |
416 | ||
417 | #ifdef MODULE | |
418 | ||
4fecf4a2 DS |
419 | static struct pcmcia_device_id ni_mio_cs_ids[] = { |
420 | PCMCIA_DEVICE_MANF_CARD(0x010b, 0x010d), /* DAQCard-ai-16xe-50 */ | |
421 | PCMCIA_DEVICE_MANF_CARD(0x010b, 0x010c), /* DAQCard-ai-16e-4 */ | |
422 | PCMCIA_DEVICE_MANF_CARD(0x010b, 0x02c4), /* DAQCard-6062E */ | |
423 | PCMCIA_DEVICE_MANF_CARD(0x010b, 0x075e), /* DAQCard-6024E */ | |
424 | PCMCIA_DEVICE_MANF_CARD(0x010b, 0x0245), /* DAQCard-6036E */ | |
425 | PCMCIA_DEVICE_NULL | |
426 | }; | |
427 | ||
428 | MODULE_DEVICE_TABLE(pcmcia, ni_mio_cs_ids); | |
6c7f8196 AK |
429 | MODULE_AUTHOR("David A. Schleef <ds@schleef.org>"); |
430 | MODULE_DESCRIPTION("Comedi driver for National Instruments DAQCard E series"); | |
431 | MODULE_LICENSE("GPL"); | |
4fecf4a2 DS |
432 | |
433 | struct pcmcia_driver ni_mio_cs_driver = { | |
434 | .probe = &cs_attach, | |
435 | .remove = &cs_detach, | |
436 | .suspend = &mio_cs_suspend, | |
437 | .resume = &mio_cs_resume, | |
438 | .id_table = ni_mio_cs_ids, | |
439 | .owner = THIS_MODULE, | |
440 | .drv = { | |
1685e633 | 441 | .name = "ni_mio_cs", |
4fecf4a2 DS |
442 | }, |
443 | }; | |
444 | ||
445 | int init_module(void) | |
446 | { | |
447 | pcmcia_register_driver(&ni_mio_cs_driver); | |
448 | comedi_driver_register(&driver_ni_mio_cs); | |
449 | return 0; | |
450 | } | |
451 | ||
452 | void cleanup_module(void) | |
453 | { | |
454 | pcmcia_unregister_driver(&ni_mio_cs_driver); | |
455 | #if 0 | |
456 | while (cur_dev != NULL) | |
457 | cs_detach(cur_dev->handle); | |
458 | #endif | |
459 | comedi_driver_unregister(&driver_ni_mio_cs); | |
460 | } | |
461 | #endif |