isadma.c revision 1.15 1 /* $NetBSD: isadma.c,v 1.15 1996/03/01 04:08:48 mycroft Exp $ */
2
3 #include <sys/param.h>
4 #include <sys/systm.h>
5 #include <sys/file.h>
6 #include <sys/buf.h>
7 #include <sys/syslog.h>
8 #include <sys/malloc.h>
9 #include <sys/uio.h>
10
11 #include <vm/vm.h>
12
13 #include <machine/pio.h>
14
15 #include <dev/isa/isareg.h>
16 #include <dev/isa/isadmavar.h>
17 #include <dev/isa/isadmareg.h>
18
19 /* region of physical memory known to be contiguous */
20 vm_offset_t isaphysmem;
21 static caddr_t dma_bounce[8]; /* XXX */
22 static char bounced[8]; /* XXX */
23 #define MAXDMASZ 512 /* XXX */
24 static u_int8_t dma_finished;
25
26 /* high byte of address is stored in this port for i-th dma channel */
27 static int dmapageport[8] = {
28 0x87, 0x83, 0x81, 0x82, 0x8f, 0x8b, 0x89, 0x8a
29 };
30
31 static u_int8_t dmamode[4] = {
32 DMA37MD_WRITE | DMA37MD_SINGLE,
33 DMA37MD_READ | DMA37MD_SINGLE,
34 DMA37MD_WRITE | DMA37MD_LOOP,
35 DMA37MD_READ | DMA37MD_LOOP
36 };
37
38 /*
39 * isa_dmacascade(): program 8237 DMA controller channel to accept
40 * external dma control by a board.
41 */
42 void
43 isa_dmacascade(chan)
44 int chan;
45 {
46
47 #ifdef ISADMA_DEBUG
48 if (chan < 0 || chan > 7)
49 panic("isa_dmacascade: impossible request");
50 #endif
51
52 /* set dma channel mode, and set dma channel mode */
53 if ((chan & 4) == 0) {
54 outb(DMA1_MODE, chan | DMA37MD_CASCADE);
55 outb(DMA1_SMSK, chan);
56 } else {
57 chan &= 3;
58
59 outb(DMA2_MODE, chan | DMA37MD_CASCADE);
60 outb(DMA2_SMSK, chan);
61 }
62 }
63
64 /*
65 * isa_dmastart(): program 8237 DMA controller channel, avoid page alignment
66 * problems by using a bounce buffer.
67 */
68 void
69 isa_dmastart(flags, addr, nbytes, chan)
70 int flags;
71 caddr_t addr;
72 vm_size_t nbytes;
73 int chan;
74 {
75 vm_offset_t phys;
76 int waport;
77 caddr_t newaddr;
78
79 #ifdef ISADMA_DEBUG
80 if (chan < 0 || chan > 7 ||
81 ((chan & 4) ? (nbytes >= (1<<17) || nbytes & 1 || (u_int)addr & 1) :
82 (nbytes >= (1<<16))))
83 panic("isa_dmastart: impossible request");
84 #endif
85
86 if (isa_dmarangecheck(addr, nbytes, chan)) {
87 if (dma_bounce[chan] == 0)
88 dma_bounce[chan] =
89 /*(caddr_t)malloc(MAXDMASZ, M_TEMP, M_WAITOK);*/
90 (caddr_t) isaphysmem + NBPG*chan;
91 bounced[chan] = 1;
92 newaddr = dma_bounce[chan];
93 *(int *) newaddr = 0; /* XXX */
94 /* copy bounce buffer on write */
95 if ((flags & DMAMODE_READ) == 0)
96 bcopy(addr, newaddr, nbytes);
97 addr = newaddr;
98 }
99
100 /* translate to physical */
101 phys = pmap_extract(pmap_kernel(), (vm_offset_t)addr);
102
103 dma_finished &= ~(1 << chan);
104
105 if ((chan & 4) == 0) {
106 /*
107 * Program one of DMA channels 0..3. These are
108 * byte mode channels.
109 */
110 /* set dma channel mode, and reset address ff */
111 if (flags & DMAMODE_READ)
112 outb(DMA1_MODE, chan | dmamode[flags]);
113 else
114 outb(DMA1_MODE, chan | dmamode[flags]);
115 outb(DMA1_FFC, 0);
116
117 /* send start address */
118 waport = DMA1_CHN(chan);
119 outb(dmapageport[chan], phys>>16);
120 outb(waport, phys);
121 outb(waport, phys>>8);
122
123 /* send count */
124 outb(waport + 1, --nbytes);
125 outb(waport + 1, nbytes>>8);
126
127 /* unmask channel */
128 outb(DMA1_SMSK, chan | DMA37SM_CLEAR);
129 } else {
130 chan &= 3;
131
132 /*
133 * Program one of DMA channels 4..7. These are
134 * word mode channels.
135 */
136 /* set dma channel mode, and reset address ff */
137 if (flags & DMAMODE_READ)
138 outb(DMA2_MODE, chan | dmamode[flags]);
139 else
140 outb(DMA2_MODE, chan | dmamode[flags]);
141 outb(DMA2_FFC, 0);
142
143 /* send start address */
144 waport = DMA2_CHN(chan);
145 outb(dmapageport[chan], phys>>16);
146 phys >>= 1;
147 outb(waport, phys);
148 outb(waport, phys>>8);
149
150 /* send count */
151 nbytes >>= 1;
152 outb(waport + 2, --nbytes);
153 outb(waport + 2, nbytes>>8);
154
155 /* unmask channel */
156 outb(DMA2_SMSK, chan | DMA37SM_CLEAR);
157 }
158 }
159
160 void
161 isa_dmaabort(chan)
162 int chan;
163 {
164
165 #ifdef ISADMA_DEBUG
166 if (chan < 0 || chan > 7)
167 panic("isa_dmaabort: impossible request");
168 #endif
169
170 bounced[chan] = 0;
171
172 /* mask channel */
173 if ((chan & 4) == 0)
174 outb(DMA1_SMSK, DMA37SM_SET | chan);
175 else
176 outb(DMA2_SMSK, DMA37SM_SET | (chan & 3));
177 }
178
179 int
180 isa_dmafinished(chan)
181 int chan;
182 {
183
184 #ifdef ISADMA_DEBUG
185 if (chan < 0 || chan > 7)
186 panic("isa_dmafinished: impossible request");
187 #endif
188
189 /* check that the terminal count was reached */
190 if ((chan & 4) == 0)
191 dma_finished |= inb(DMA1_SR) & 0x0f;
192 else
193 dma_finished |= (inb(DMA2_SR) & 0x0f) << 4;
194
195 return ((dma_finished & (1 << chan)) != 0);
196 }
197
198 void
199 isa_dmadone(flags, addr, nbytes, chan)
200 int flags;
201 caddr_t addr;
202 vm_size_t nbytes;
203 int chan;
204 {
205
206 #ifdef ISADMA_DEBUG
207 if (chan < 0 || chan > 7)
208 panic("isa_dmadone: impossible request");
209 #endif
210
211 if (!isa_dmafinished(chan))
212 printf("isa_dmadone: channel %d not finished\n", chan);
213
214 /* mask channel */
215 if ((chan & 4) == 0)
216 outb(DMA1_SMSK, DMA37SM_SET | chan);
217 else
218 outb(DMA2_SMSK, DMA37SM_SET | (chan & 3));
219
220 /* copy bounce buffer on read */
221 if (bounced[chan]) {
222 bcopy(dma_bounce[chan], addr, nbytes);
223 bounced[chan] = 0;
224 }
225 }
226
227 /*
228 * Check for problems with the address range of a DMA transfer
229 * (non-contiguous physical pages, outside of bus address space,
230 * crossing DMA page boundaries).
231 * Return true if special handling needed.
232 */
233 int
234 isa_dmarangecheck(va, length, chan)
235 vm_offset_t va;
236 u_long length;
237 int chan;
238 {
239 vm_offset_t phys, priorpage = 0, endva;
240 u_int dma_pgmsk = (chan & 4) ? ~(128*1024-1) : ~(64*1024-1);
241
242 endva = round_page(va + length);
243 for (; va < endva ; va += NBPG) {
244 phys = trunc_page(pmap_extract(pmap_kernel(), va));
245 if (phys == 0)
246 panic("isa_dmacheck: no physical page present");
247 if (phys >= (1<<24))
248 return 1;
249 if (priorpage) {
250 if (priorpage + NBPG != phys)
251 return 1;
252 /* check if crossing a DMA page boundary */
253 if ((priorpage ^ phys) & dma_pgmsk)
254 return 1;
255 }
256 priorpage = phys;
257 }
258 return 0;
259 }
260
261 /* head of queue waiting for physmem to become available */
262 struct buf isa_physmemq;
263
264 /* blocked waiting for resource to become free for exclusive use */
265 static isaphysmemflag;
266 /* if waited for and call requested when free (B_CALL) */
267 static void (*isaphysmemunblock)(); /* needs to be a list */
268
269 /*
270 * Allocate contiguous physical memory for transfer, returning
271 * a *virtual* address to region. May block waiting for resource.
272 * (assumed to be called at splbio())
273 */
274 caddr_t
275 isa_allocphysmem(caddr_t va, unsigned length, void (*func)()) {
276
277 isaphysmemunblock = func;
278 while (isaphysmemflag & B_BUSY) {
279 isaphysmemflag |= B_WANTED;
280 sleep((caddr_t)&isaphysmemflag, PRIBIO);
281 }
282 isaphysmemflag |= B_BUSY;
283
284 return((caddr_t)isaphysmem);
285 }
286
287 /*
288 * Free contiguous physical memory used for transfer.
289 * (assumed to be called at splbio())
290 */
291 void
292 isa_freephysmem(caddr_t va, unsigned length) {
293
294 isaphysmemflag &= ~B_BUSY;
295 if (isaphysmemflag & B_WANTED) {
296 isaphysmemflag &= B_WANTED;
297 wakeup((caddr_t)&isaphysmemflag);
298 if (isaphysmemunblock)
299 (*isaphysmemunblock)();
300 }
301 }
302