MAINTAINERS: Add phy-miphy28lp.c and phy-miphy365x.c to ARCH/STI architecture
[deliverable/linux.git] / drivers / staging / comedi / drivers / comedi_isadma.c
1 /*
2 * COMEDI ISA DMA support functions
3 * Copyright (c) 2014 H Hartley Sweeten <hsweeten@visionengravers.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 */
15
16 #include <linux/module.h>
17 #include <linux/slab.h>
18 #include <linux/delay.h>
19 #include <linux/dma-mapping.h>
20 #include <asm/dma.h>
21
22 #include "../comedidev.h"
23
24 #include "comedi_isadma.h"
25
26 /**
27 * comedi_isadma_program - program and enable an ISA DMA transfer
28 * @desc: the ISA DMA cookie to program and enable
29 */
30 void comedi_isadma_program(struct comedi_isadma_desc *desc)
31 {
32 unsigned long flags;
33
34 flags = claim_dma_lock();
35 clear_dma_ff(desc->chan);
36 set_dma_mode(desc->chan, desc->mode);
37 set_dma_addr(desc->chan, desc->hw_addr);
38 set_dma_count(desc->chan, desc->size);
39 enable_dma(desc->chan);
40 release_dma_lock(flags);
41 }
42 EXPORT_SYMBOL_GPL(comedi_isadma_program);
43
44 /**
45 * comedi_isadma_disable - disable the ISA DMA channel
46 * @dma_chan: the DMA channel to disable
47 *
48 * Returns the residue (remaining bytes) left in the DMA transfer.
49 */
50 unsigned int comedi_isadma_disable(unsigned int dma_chan)
51 {
52 unsigned long flags;
53 unsigned int residue;
54
55 flags = claim_dma_lock();
56 disable_dma(dma_chan);
57 residue = get_dma_residue(dma_chan);
58 release_dma_lock(flags);
59
60 return residue;
61 }
62 EXPORT_SYMBOL_GPL(comedi_isadma_disable);
63
64 /**
65 * comedi_isadma_disable_on_sample - disable the ISA DMA channel
66 * @dma_chan: the DMA channel to disable
67 * @size: the sample size (in bytes)
68 *
69 * Returns the residue (remaining bytes) left in the DMA transfer.
70 */
71 unsigned int comedi_isadma_disable_on_sample(unsigned int dma_chan,
72 unsigned int size)
73 {
74 int stalled = 0;
75 unsigned long flags;
76 unsigned int residue;
77 unsigned int new_residue;
78
79 residue = comedi_isadma_disable(dma_chan);
80 while (residue % size) {
81 /* residue is a partial sample, enable DMA to allow more data */
82 flags = claim_dma_lock();
83 enable_dma(dma_chan);
84 release_dma_lock(flags);
85
86 udelay(2);
87 new_residue = comedi_isadma_disable(dma_chan);
88
89 /* is DMA stalled? */
90 if (new_residue == residue) {
91 stalled++;
92 if (stalled > 10)
93 break;
94 }
95 residue = new_residue;
96 stalled = 0;
97 }
98 return residue;
99 }
100 EXPORT_SYMBOL_GPL(comedi_isadma_disable_on_sample);
101
102 /**
103 * comedi_isadma_poll - poll the current DMA transfer
104 * @dma: the ISA DMA to poll
105 *
106 * Returns the position (in bytes) of the current DMA transfer.
107 */
108 unsigned int comedi_isadma_poll(struct comedi_isadma *dma)
109 {
110 struct comedi_isadma_desc *desc = &dma->desc[dma->cur_dma];
111 unsigned long flags;
112 unsigned int result;
113 unsigned int result1;
114
115 flags = claim_dma_lock();
116 clear_dma_ff(desc->chan);
117 if (!isa_dma_bridge_buggy)
118 disable_dma(desc->chan);
119 result = get_dma_residue(desc->chan);
120 /*
121 * Read the counter again and choose higher value in order to
122 * avoid reading during counter lower byte roll over if the
123 * isa_dma_bridge_buggy is set.
124 */
125 result1 = get_dma_residue(desc->chan);
126 if (!isa_dma_bridge_buggy)
127 enable_dma(desc->chan);
128 release_dma_lock(flags);
129
130 if (result < result1)
131 result = result1;
132 if (result >= desc->size || result == 0)
133 return 0;
134 else
135 return desc->size - result;
136 }
137 EXPORT_SYMBOL_GPL(comedi_isadma_poll);
138
139 /**
140 * comedi_isadma_set_mode - set the ISA DMA transfer direction
141 * @desc: the ISA DMA cookie to set
142 * @dma_dir: the DMA direction
143 */
144 void comedi_isadma_set_mode(struct comedi_isadma_desc *desc, char dma_dir)
145 {
146 desc->mode = (dma_dir == COMEDI_ISADMA_READ) ? DMA_MODE_READ
147 : DMA_MODE_WRITE;
148 }
149 EXPORT_SYMBOL_GPL(comedi_isadma_set_mode);
150
151 /**
152 * comedi_isadma_alloc - allocate and initialize the ISA DMA
153 * @dev: comedi_device struct
154 * @n_desc: the number of cookies to allocate
155 * @dma_chan: DMA channel for the first cookie
156 * @dma_chan2: DMA channel for the second cookie
157 * @maxsize: the size of the buffer to allocate for each cookie
158 * @dma_dir: the DMA direction
159 *
160 * Returns the allocated and initialized ISA DMA or NULL if anything fails.
161 */
162 struct comedi_isadma *comedi_isadma_alloc(struct comedi_device *dev,
163 int n_desc, unsigned int dma_chan1,
164 unsigned int dma_chan2,
165 unsigned int maxsize, char dma_dir)
166 {
167 struct comedi_isadma *dma = NULL;
168 struct comedi_isadma_desc *desc;
169 unsigned int dma_chans[2];
170 int i;
171
172 if (n_desc < 1 || n_desc > 2)
173 goto no_dma;
174
175 dma = kzalloc(sizeof(*dma), GFP_KERNEL);
176 if (!dma)
177 goto no_dma;
178
179 desc = kcalloc(n_desc, sizeof(*desc), GFP_KERNEL);
180 if (!desc)
181 goto no_dma;
182 dma->desc = desc;
183 dma->n_desc = n_desc;
184
185 dma_chans[0] = dma_chan1;
186 if (dma_chan2 == 0 || dma_chan2 == dma_chan1)
187 dma_chans[1] = dma_chan1;
188 else
189 dma_chans[1] = dma_chan2;
190
191 if (request_dma(dma_chans[0], dev->board_name))
192 goto no_dma;
193 dma->chan = dma_chans[0];
194 if (dma_chans[1] != dma_chans[0]) {
195 if (request_dma(dma_chans[1], dev->board_name))
196 goto no_dma;
197 }
198 dma->chan2 = dma_chans[1];
199
200 for (i = 0; i < n_desc; i++) {
201 desc = &dma->desc[i];
202 desc->chan = dma_chans[i];
203 desc->maxsize = maxsize;
204 desc->virt_addr = dma_alloc_coherent(NULL, desc->maxsize,
205 &desc->hw_addr,
206 GFP_KERNEL);
207 if (!desc->virt_addr)
208 goto no_dma;
209 comedi_isadma_set_mode(desc, dma_dir);
210 }
211
212 return dma;
213
214 no_dma:
215 comedi_isadma_free(dma);
216 return NULL;
217 }
218 EXPORT_SYMBOL_GPL(comedi_isadma_alloc);
219
220 /**
221 * comedi_isadma_free - free the ISA DMA
222 * @dma: the ISA DMA to free
223 */
224 void comedi_isadma_free(struct comedi_isadma *dma)
225 {
226 struct comedi_isadma_desc *desc;
227 int i;
228
229 if (!dma)
230 return;
231
232 if (dma->desc) {
233 for (i = 0; i < dma->n_desc; i++) {
234 desc = &dma->desc[i];
235 if (desc->virt_addr)
236 dma_free_coherent(NULL, desc->maxsize,
237 desc->virt_addr, desc->hw_addr);
238 }
239 kfree(dma->desc);
240 }
241 if (dma->chan2 && dma->chan2 != dma->chan)
242 free_dma(dma->chan2);
243 if (dma->chan)
244 free_dma(dma->chan);
245 kfree(dma);
246 }
247 EXPORT_SYMBOL_GPL(comedi_isadma_free);
248
249 static int __init comedi_isadma_init(void)
250 {
251 return 0;
252 }
253 module_init(comedi_isadma_init);
254
255 static void __exit comedi_isadma_exit(void)
256 {
257 }
258 module_exit(comedi_isadma_exit);
259
260 MODULE_AUTHOR("H Hartley Sweeten <hsweeten@visionengravers.com>");
261 MODULE_DESCRIPTION("Comedi ISA DMA support");
262 MODULE_LICENSE("GPL");
This page took 0.036914 seconds and 5 git commands to generate.