3 Hardware driver for NI Mite PCI interface chip
5 COMEDI - Linux Control and Measurement Device Interface
6 Copyright (C) 1999 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.
27 #include <linux/pci.h>
28 #include "../comedidev.h"
30 #define PCI_VENDOR_ID_NATINST 0x1093
32 /* #define DEBUG_MITE */
36 #define MDPRINTK(format, args...) printk(format , ## args)
38 #define MDPRINTK(format, args...)
41 #define MAX_MITE_DMA_CHANNELS 8
43 struct mite_dma_descriptor {
50 struct mite_dma_descriptor_ring {
51 struct device *hw_dev;
53 struct mite_dma_descriptor *descriptors;
54 dma_addr_t descriptors_dma_addr;
58 struct mite_struct *mite;
62 struct mite_dma_descriptor_ring *ring;
66 struct mite_struct *next;
69 struct pci_dev *pcidev;
70 resource_size_t mite_phys_addr;
72 resource_size_t daq_phys_addr;
75 struct mite_channel channels[MAX_MITE_DMA_CHANNELS];
76 short channel_allocated[MAX_MITE_DMA_CHANNELS];
82 static inline struct mite_dma_descriptor_ring *mite_alloc_ring(struct
85 struct mite_dma_descriptor_ring *ring =
86 kmalloc(sizeof(struct mite_dma_descriptor_ring), GFP_KERNEL);
89 ring->hw_dev = get_device(&mite->pcidev->dev);
90 if (ring->hw_dev == NULL) {
95 ring->descriptors = NULL;
96 ring->descriptors_dma_addr = 0;
100 static inline void mite_free_ring(struct mite_dma_descriptor_ring *ring)
103 if (ring->descriptors) {
104 dma_free_coherent(ring->hw_dev,
106 sizeof(struct mite_dma_descriptor),
107 ring->descriptors, ring->descriptors_dma_addr);
109 put_device(ring->hw_dev);
114 extern struct mite_struct *mite_devices;
116 static inline unsigned int mite_irq(struct mite_struct *mite)
118 return mite->pcidev->irq;
120 static inline unsigned int mite_device_id(struct mite_struct *mite)
122 return mite->pcidev->device;
125 void mite_init(void);
126 void mite_cleanup(void);
127 int mite_setup(struct mite_struct *mite);
128 int mite_setup2(struct mite_struct *mite, unsigned use_iodwbsr_1);
129 void mite_unsetup(struct mite_struct *mite);
130 void mite_list_devices(void);
131 struct mite_channel *mite_request_channel_in_range(struct mite_struct *mite,
132 struct mite_dma_descriptor_ring *ring, unsigned min_channel,
133 unsigned max_channel);
134 static inline struct mite_channel *mite_request_channel(struct mite_struct
135 *mite, struct mite_dma_descriptor_ring *ring)
137 return mite_request_channel_in_range(mite, ring, 0,
138 mite->num_channels - 1);
140 void mite_release_channel(struct mite_channel *mite_chan);
142 unsigned mite_dma_tcr(struct mite_channel *mite_chan);
143 void mite_dma_arm(struct mite_channel *mite_chan);
144 void mite_dma_disarm(struct mite_channel *mite_chan);
145 int mite_sync_input_dma(struct mite_channel *mite_chan, struct comedi_async * async);
146 int mite_sync_output_dma(struct mite_channel *mite_chan, struct comedi_async * async);
147 u32 mite_bytes_written_to_memory_lb(struct mite_channel *mite_chan);
148 u32 mite_bytes_written_to_memory_ub(struct mite_channel *mite_chan);
149 u32 mite_bytes_read_from_memory_lb(struct mite_channel *mite_chan);
150 u32 mite_bytes_read_from_memory_ub(struct mite_channel *mite_chan);
151 u32 mite_bytes_in_transit(struct mite_channel *mite_chan);
152 unsigned mite_get_status(struct mite_channel *mite_chan);
153 int mite_done(struct mite_channel *mite_chan);
156 unsigned long mite_ll_from_kvmem(struct mite_struct *mite, struct comedi_async * async,
158 void mite_setregs(struct mite_struct *mite, unsigned long ll_start, int chan,
162 void mite_prep_dma(struct mite_channel *mite_chan,
163 unsigned int num_device_bits, unsigned int num_memory_bits);
164 int mite_buf_change(struct mite_dma_descriptor_ring *ring,
165 struct comedi_async *async);
168 void mite_print_chsr(unsigned int chsr);
169 void mite_dump_regs(struct mite_channel *mite_chan);
172 static inline int CHAN_OFFSET(int channel)
174 return 0x500 + 0x100 * channel;
177 enum mite_registers {
178 /* The bits 0x90180700 in MITE_UNKNOWN_DMA_BURST_REG can be
179 written and read back. The bits 0x1f always read as 1.
180 The rest always read as zero. */
181 MITE_UNKNOWN_DMA_BURST_REG = 0x28,
182 MITE_IODWBSR = 0xc0, /* IO Device Window Base Size Register */
183 MITE_IODWBSR_1 = 0xc4, /* IO Device Window Base Size Register 1 */
184 MITE_IODWCR_1 = 0xf4,
185 MITE_PCI_CONFIG_OFFSET = 0x300,
186 MITE_CSIGR = 0x460 /* chip signature */
188 static inline int MITE_CHOR(int channel) /* channel operation */
190 return CHAN_OFFSET(channel) + 0x0;
192 static inline int MITE_CHCR(int channel) /* channel control */
194 return CHAN_OFFSET(channel) + 0x4;
196 static inline int MITE_TCR(int channel) /* transfer count */
198 return CHAN_OFFSET(channel) + 0x8;
200 static inline int MITE_MCR(int channel) /* memory configuration */
202 return CHAN_OFFSET(channel) + 0xc;
204 static inline int MITE_MAR(int channel) /* memory address */
206 return CHAN_OFFSET(channel) + 0x10;
208 static inline int MITE_DCR(int channel) /* device configuration */
210 return CHAN_OFFSET(channel) + 0x14;
212 static inline int MITE_DAR(int channel) /* device address */
214 return CHAN_OFFSET(channel) + 0x18;
216 static inline int MITE_LKCR(int channel) /* link configuration */
218 return CHAN_OFFSET(channel) + 0x1c;
220 static inline int MITE_LKAR(int channel) /* link address */
222 return CHAN_OFFSET(channel) + 0x20;
224 static inline int MITE_LLKAR(int channel) /* see mite section of tnt5002 manual */
226 return CHAN_OFFSET(channel) + 0x24;
228 static inline int MITE_BAR(int channel) /* base address */
230 return CHAN_OFFSET(channel) + 0x28;
232 static inline int MITE_BCR(int channel) /* base count */
234 return CHAN_OFFSET(channel) + 0x2c;
236 static inline int MITE_SAR(int channel) /* ? address */
238 return CHAN_OFFSET(channel) + 0x30;
240 static inline int MITE_WSCR(int channel) /* ? */
242 return CHAN_OFFSET(channel) + 0x34;
244 static inline int MITE_WSER(int channel) /* ? */
246 return CHAN_OFFSET(channel) + 0x38;
248 static inline int MITE_CHSR(int channel) /* channel status */
250 return CHAN_OFFSET(channel) + 0x3c;
252 static inline int MITE_FCR(int channel) /* fifo count */
254 return CHAN_OFFSET(channel) + 0x40;
257 enum MITE_IODWBSR_bits {
258 WENAB = 0x80, /* window enable */
261 static inline unsigned MITE_IODWBSR_1_WSIZE_bits(unsigned size)
267 return (order - 1) & 0x1f;
270 enum MITE_UNKNOWN_DMA_BURST_bits {
271 UNKNOWN_DMA_BURST_ENABLE_BITS = 0x600
274 static inline int mite_csigr_version(u32 csigr_bits)
276 return csigr_bits & 0xf;
278 static inline int mite_csigr_type(u32 csigr_bits)
279 { /* original mite = 0, minimite = 1 */
280 return (csigr_bits >> 4) & 0xf;
282 static inline int mite_csigr_mmode(u32 csigr_bits)
283 { /* mite mode, minimite = 1 */
284 return (csigr_bits >> 8) & 0x3;
286 static inline int mite_csigr_imode(u32 csigr_bits)
287 { /* cpu port interface mode, pci = 0x3 */
288 return (csigr_bits >> 12) & 0x3;
290 static inline int mite_csigr_dmac(u32 csigr_bits)
291 { /* number of dma channels */
292 return (csigr_bits >> 16) & 0xf;
294 static inline int mite_csigr_wpdep(u32 csigr_bits)
295 { /* write post fifo depth */
296 unsigned int wpdep_bits = (csigr_bits >> 20) & 0x7;
300 return 1 << (wpdep_bits - 1);
302 static inline int mite_csigr_wins(u32 csigr_bits)
304 return (csigr_bits >> 24) & 0x1f;
306 static inline int mite_csigr_iowins(u32 csigr_bits)
307 { /* number of io windows */
308 return (csigr_bits >> 29) & 0x7;
316 DCR_NORMAL = (1 << 29),
320 enum MITE_CHOR_bits {
321 CHOR_DMARESET = (1 << 31),
322 CHOR_SET_SEND_TC = (1 << 11),
323 CHOR_CLR_SEND_TC = (1 << 10),
324 CHOR_SET_LPAUSE = (1 << 9),
325 CHOR_CLR_LPAUSE = (1 << 8),
326 CHOR_CLRDONE = (1 << 7),
327 CHOR_CLRRB = (1 << 6),
328 CHOR_CLRLC = (1 << 5),
329 CHOR_FRESET = (1 << 4),
330 CHOR_ABORT = (1 << 3), /* stop without emptying fifo */
331 CHOR_STOP = (1 << 2), /* stop after emptying fifo */
332 CHOR_CONT = (1 << 1),
333 CHOR_START = (1 << 0),
334 CHOR_PON = (CHOR_CLR_SEND_TC | CHOR_CLR_LPAUSE),
337 enum MITE_CHCR_bits {
338 CHCR_SET_DMA_IE = (1 << 31),
339 CHCR_CLR_DMA_IE = (1 << 30),
340 CHCR_SET_LINKP_IE = (1 << 29),
341 CHCR_CLR_LINKP_IE = (1 << 28),
342 CHCR_SET_SAR_IE = (1 << 27),
343 CHCR_CLR_SAR_IE = (1 << 26),
344 CHCR_SET_DONE_IE = (1 << 25),
345 CHCR_CLR_DONE_IE = (1 << 24),
346 CHCR_SET_MRDY_IE = (1 << 23),
347 CHCR_CLR_MRDY_IE = (1 << 22),
348 CHCR_SET_DRDY_IE = (1 << 21),
349 CHCR_CLR_DRDY_IE = (1 << 20),
350 CHCR_SET_LC_IE = (1 << 19),
351 CHCR_CLR_LC_IE = (1 << 18),
352 CHCR_SET_CONT_RB_IE = (1 << 17),
353 CHCR_CLR_CONT_RB_IE = (1 << 16),
354 CHCR_FIFODIS = (1 << 15),
356 CHCR_BURSTEN = (1 << 14),
358 CHCR_BYTE_SWAP_DEVICE = (1 << 6),
359 CHCR_BYTE_SWAP_MEMORY = (1 << 4),
361 CHCR_DEV_TO_MEM = CHCR_DIR,
363 CHCR_NORMAL = (0 << 0),
364 CHCR_CONTINUE = (1 << 0),
365 CHCR_RINGBUFF = (2 << 0),
366 CHCR_LINKSHORT = (4 << 0),
367 CHCR_LINKLONG = (5 << 0),
369 (CHCR_CLR_DMA_IE | CHCR_CLR_LINKP_IE | CHCR_CLR_SAR_IE |
370 CHCR_CLR_DONE_IE | CHCR_CLR_MRDY_IE | CHCR_CLR_DRDY_IE |
371 CHCR_CLR_LC_IE | CHCR_CLR_CONT_RB_IE),
374 enum ConfigRegister_bits {
375 CR_REQS_MASK = 0x7 << 16,
376 CR_ASEQDONT = 0x0 << 10,
377 CR_ASEQUP = 0x1 << 10,
378 CR_ASEQDOWN = 0x2 << 10,
379 CR_ASEQ_MASK = 0x3 << 10,
380 CR_PSIZE8 = (1 << 8),
381 CR_PSIZE16 = (2 << 8),
382 CR_PSIZE32 = (3 << 8),
383 CR_PORTCPU = (0 << 6),
384 CR_PORTIO = (1 << 6),
385 CR_PORTVXI = (2 << 6),
386 CR_PORTMXI = (3 << 6),
387 CR_AMDEVICE = (1 << 0),
389 static inline int CR_REQS(int source)
391 return (source & 0x7) << 16;
393 static inline int CR_REQSDRQ(unsigned drq_line)
395 /* This also works on m-series when
396 using channels (drq_line) 4 or 5. */
397 return CR_REQS((drq_line & 0x3) | 0x4);
399 static inline int CR_RL(unsigned int retry_limit)
403 while (retry_limit) {
408 rt_printk("comedi: bug! retry_limit too large\n");
409 return (value & 0x7) << 21;
413 CHSR_INT = (1 << 31),
414 CHSR_LPAUSES = (1 << 29),
415 CHSR_SARS = (1 << 27),
416 CHSR_DONE = (1 << 25),
417 CHSR_MRDY = (1 << 23),
418 CHSR_DRDY = (1 << 21),
419 CHSR_LINKC = (1 << 19),
420 CHSR_CONTS_RB = (1 << 17),
421 CHSR_ERROR = (1 << 15),
422 CHSR_SABORT = (1 << 14),
423 CHSR_HABORT = (1 << 13),
424 CHSR_STOPS = (1 << 12),
425 CHSR_OPERR_mask = (3 << 10),
426 CHSR_OPERR_NOERROR = (0 << 10),
427 CHSR_OPERR_FIFOERROR = (1 << 10),
428 CHSR_OPERR_LINKERROR = (1 << 10), /* ??? */
429 CHSR_XFERR = (1 << 9),
431 CHSR_DRQ1 = (1 << 7),
432 CHSR_DRQ0 = (1 << 6),
433 CHSR_LxERR_mask = (3 << 4),
434 CHSR_LBERR = (1 << 4),
435 CHSR_LRERR = (2 << 4),
436 CHSR_LOERR = (3 << 4),
437 CHSR_MxERR_mask = (3 << 2),
438 CHSR_MBERR = (1 << 2),
439 CHSR_MRERR = (2 << 2),
440 CHSR_MOERR = (3 << 2),
441 CHSR_DxERR_mask = (3 << 0),
442 CHSR_DBERR = (1 << 0),
443 CHSR_DRERR = (2 << 0),
444 CHSR_DOERR = (3 << 0),
447 static inline void mite_dma_reset(struct mite_channel *mite_chan)
449 writel(CHOR_DMARESET | CHOR_FRESET,
450 mite_chan->mite->mite_io_addr + MITE_CHOR(mite_chan->channel));