1 // SPDX-License-Identifier: GPL-2.0+
3 * COMEDI ISA DMA support functions
4 * Copyright (c) 2014 H Hartley Sweeten <hsweeten@visionengravers.com>
7 #include <linux/module.h>
8 #include <linux/slab.h>
9 #include <linux/delay.h>
10 #include <linux/dma-mapping.h>
11 #include <linux/isa-dma.h>
12 #include <linux/comedi/comedidev.h>
13 #include <linux/comedi/comedi_isadma.h>
16 * comedi_isadma_program - program and enable an ISA DMA transfer
17 * @desc: the ISA DMA cookie to program and enable
19 void comedi_isadma_program(struct comedi_isadma_desc *desc)
23 flags = claim_dma_lock();
24 clear_dma_ff(desc->chan);
25 set_dma_mode(desc->chan, desc->mode);
26 set_dma_addr(desc->chan, desc->hw_addr);
27 set_dma_count(desc->chan, desc->size);
28 enable_dma(desc->chan);
29 release_dma_lock(flags);
31 EXPORT_SYMBOL_GPL(comedi_isadma_program);
34 * comedi_isadma_disable - disable the ISA DMA channel
35 * @dma_chan: the DMA channel to disable
37 * Returns the residue (remaining bytes) left in the DMA transfer.
39 unsigned int comedi_isadma_disable(unsigned int dma_chan)
44 flags = claim_dma_lock();
45 disable_dma(dma_chan);
46 residue = get_dma_residue(dma_chan);
47 release_dma_lock(flags);
51 EXPORT_SYMBOL_GPL(comedi_isadma_disable);
54 * comedi_isadma_disable_on_sample - disable the ISA DMA channel
55 * @dma_chan: the DMA channel to disable
56 * @size: the sample size (in bytes)
58 * Returns the residue (remaining bytes) left in the DMA transfer.
60 unsigned int comedi_isadma_disable_on_sample(unsigned int dma_chan,
66 unsigned int new_residue;
68 residue = comedi_isadma_disable(dma_chan);
69 while (residue % size) {
70 /* residue is a partial sample, enable DMA to allow more data */
71 flags = claim_dma_lock();
73 release_dma_lock(flags);
76 new_residue = comedi_isadma_disable(dma_chan);
79 if (new_residue == residue) {
84 residue = new_residue;
90 EXPORT_SYMBOL_GPL(comedi_isadma_disable_on_sample);
93 * comedi_isadma_poll - poll the current DMA transfer
94 * @dma: the ISA DMA to poll
96 * Returns the position (in bytes) of the current DMA transfer.
98 unsigned int comedi_isadma_poll(struct comedi_isadma *dma)
100 struct comedi_isadma_desc *desc = &dma->desc[dma->cur_dma];
103 unsigned int result1;
105 flags = claim_dma_lock();
106 clear_dma_ff(desc->chan);
107 if (!isa_dma_bridge_buggy)
108 disable_dma(desc->chan);
109 result = get_dma_residue(desc->chan);
111 * Read the counter again and choose higher value in order to
112 * avoid reading during counter lower byte roll over if the
113 * isa_dma_bridge_buggy is set.
115 result1 = get_dma_residue(desc->chan);
116 if (!isa_dma_bridge_buggy)
117 enable_dma(desc->chan);
118 release_dma_lock(flags);
120 if (result < result1)
122 if (result >= desc->size || result == 0)
124 return desc->size - result;
126 EXPORT_SYMBOL_GPL(comedi_isadma_poll);
129 * comedi_isadma_set_mode - set the ISA DMA transfer direction
130 * @desc: the ISA DMA cookie to set
131 * @dma_dir: the DMA direction
133 void comedi_isadma_set_mode(struct comedi_isadma_desc *desc, char dma_dir)
135 desc->mode = (dma_dir == COMEDI_ISADMA_READ) ? DMA_MODE_READ
138 EXPORT_SYMBOL_GPL(comedi_isadma_set_mode);
141 * comedi_isadma_alloc - allocate and initialize the ISA DMA
142 * @dev: comedi_device struct
143 * @n_desc: the number of cookies to allocate
144 * @dma_chan1: DMA channel for the first cookie
145 * @dma_chan2: DMA channel for the second cookie
146 * @maxsize: the size of the buffer to allocate for each cookie
147 * @dma_dir: the DMA direction
149 * Returns the allocated and initialized ISA DMA or NULL if anything fails.
151 struct comedi_isadma *comedi_isadma_alloc(struct comedi_device *dev,
152 int n_desc, unsigned int dma_chan1,
153 unsigned int dma_chan2,
154 unsigned int maxsize, char dma_dir)
156 struct comedi_isadma *dma = NULL;
157 struct comedi_isadma_desc *desc;
158 unsigned int dma_chans[2];
161 if (n_desc < 1 || n_desc > 2)
164 dma = kzalloc(sizeof(*dma), GFP_KERNEL);
168 desc = kcalloc(n_desc, sizeof(*desc), GFP_KERNEL);
172 dma->n_desc = n_desc;
174 dma->dev = dev->hw_dev;
176 /* Fall back to using the "class" device. */
179 /* Need 24-bit mask for ISA DMA. */
180 if (dma_coerce_mask_and_coherent(dev->class_dev,
184 dma->dev = dev->class_dev;
187 dma_chans[0] = dma_chan1;
188 if (dma_chan2 == 0 || dma_chan2 == dma_chan1)
189 dma_chans[1] = dma_chan1;
191 dma_chans[1] = dma_chan2;
193 if (request_dma(dma_chans[0], dev->board_name))
195 dma->chan = dma_chans[0];
196 if (dma_chans[1] != dma_chans[0]) {
197 if (request_dma(dma_chans[1], dev->board_name))
200 dma->chan2 = dma_chans[1];
202 for (i = 0; i < n_desc; i++) {
203 desc = &dma->desc[i];
204 desc->chan = dma_chans[i];
205 desc->maxsize = maxsize;
206 desc->virt_addr = dma_alloc_coherent(dma->dev, desc->maxsize,
209 if (!desc->virt_addr)
211 comedi_isadma_set_mode(desc, dma_dir);
217 comedi_isadma_free(dma);
220 EXPORT_SYMBOL_GPL(comedi_isadma_alloc);
223 * comedi_isadma_free - free the ISA DMA
224 * @dma: the ISA DMA to free
226 void comedi_isadma_free(struct comedi_isadma *dma)
228 struct comedi_isadma_desc *desc;
235 for (i = 0; i < dma->n_desc; i++) {
236 desc = &dma->desc[i];
238 dma_free_coherent(dma->dev, desc->maxsize,
244 if (dma->chan2 && dma->chan2 != dma->chan)
245 free_dma(dma->chan2);
250 EXPORT_SYMBOL_GPL(comedi_isadma_free);
252 static int __init comedi_isadma_init(void)
256 module_init(comedi_isadma_init);
258 static void __exit comedi_isadma_exit(void)
261 module_exit(comedi_isadma_exit);
263 MODULE_AUTHOR("H Hartley Sweeten <hsweeten@visionengravers.com>");
264 MODULE_DESCRIPTION("Comedi ISA DMA support");
265 MODULE_LICENSE("GPL");