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