]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - sys/ia64/isa/isa_dma.c
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / sys / ia64 / isa / isa_dma.c
1 /*-
2  * Copyright (c) 1991 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * William Jolitz.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 4. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  *      from: @(#)isa.c 7.2 (Berkeley) 5/13/91
33  *      from: isa_dma.c,v 1.3 1999/05/09 23:56:00 peter Exp $
34  * $FreeBSD$
35  */
36
37 /*
38  * code to manage AT bus
39  *
40  * 92/08/18  Frank P. MacLachlan (fpm@crash.cts.com):
41  * Fixed uninitialized variable problem and added code to deal
42  * with DMA page boundaries in isa_dmarangecheck().  Fixed word
43  * mode DMA count compution and reorganized DMA setup code in
44  * isa_dmastart()
45  */
46
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/malloc.h>
50 #include <sys/lock.h>
51 #include <sys/mutex.h>
52 #include <sys/bus.h>
53 #include <vm/vm.h>
54 #include <vm/vm_param.h>
55 #include <vm/pmap.h>
56 #include <isa/isareg.h>
57 #include <isa/isavar.h>
58 #include <isa/isa_dmareg.h>
59 #include <machine/bus.h>
60
61 static bus_dma_tag_t dma_tag[8];
62 static bus_dmamap_t dma_map[8];
63 static u_int8_t dma_busy = 0;           /* Used in isa_dmastart() */
64 static u_int8_t dma_inuse = 0;          /* User for acquire/release */
65 static u_int8_t dma_auto_mode = 0;
66 static u_int8_t dma_bounced = 0;
67
68 #define VALID_DMA_MASK (7)
69
70 /* high byte of address is stored in this port for i-th dma channel */
71 static int dmapageport[8] = { 0x87, 0x83, 0x81, 0x82, 0x8f, 0x8b, 0x89, 0x8a };
72
73 /*
74  * Setup a DMA channel's bounce buffer.
75  */
76 int
77 isa_dma_init(int chan, u_int bouncebufsize, int flag __unused)
78 {
79         static int initted = 0;
80         bus_addr_t boundary = chan >= 4 ? 0x20000 : 0x10000;
81
82         if (!initted) {
83                 /*
84                  * Reset the DMA hardware.
85                  */
86                 outb(DMA1_RESET, 0);
87                 outb(DMA2_RESET, 0);
88                 isa_dmacascade(4);
89             
90                 initted = 1;
91         }
92
93 #ifdef DIAGNOSTIC
94         if (chan & ~VALID_DMA_MASK)
95                 panic("isa_dma_init: channel out of range");
96
97         if (dma_tag[chan] || dma_map[chan])
98                 panic("isa_dma_init: impossible request"); 
99 #endif
100
101         if (bus_dma_tag_create(/*parent*/NULL,
102                                /*alignment*/2,
103                                /*boundary*/boundary,
104                                /*lowaddr*/BUS_SPACE_MAXADDR_24BIT,
105                                /*highaddr*/BUS_SPACE_MAXADDR,
106                                /*filter*/NULL, /*filterarg*/NULL,
107                                /*maxsize*/bouncebufsize,
108                                /*nsegments*/1, /*maxsegz*/0x3ffff,
109                                /*flags*/0,
110                                /*lockfunc*/busdma_lock_mutex,
111                                /*lockarg*/&Giant,
112                                &dma_tag[chan]) != 0) {
113                 panic("isa_dma_init: unable to create dma tag\n");
114         }
115         
116         if (bus_dmamap_create(dma_tag[chan], 0, &dma_map[chan])) {
117                 panic("isa_dma_init: unable to create dma map\n");
118         }
119
120         return (0);
121 }
122
123 /*
124  * Register a DMA channel's usage.  Usually called from a device driver
125  * in open() or during its initialization.
126  */
127 int
128 isa_dma_acquire(chan)
129         int chan;
130 {
131 #ifdef DIAGNOSTIC
132         if (chan & ~VALID_DMA_MASK)
133                 panic("isa_dma_acquire: channel out of range");
134 #endif
135
136         if (dma_inuse & (1 << chan)) {
137                 printf("isa_dma_acquire: channel %d already in use\n", chan);
138                 return (EBUSY);
139         }
140         dma_inuse |= (1 << chan);
141         dma_auto_mode &= ~(1 << chan);
142
143         return (0);
144 }
145
146 /*
147  * Unregister a DMA channel's usage.  Usually called from a device driver
148  * during close() or during its shutdown.
149  */
150 void
151 isa_dma_release(chan)
152         int chan;
153 {
154 #ifdef DIAGNOSTIC
155         if (chan & ~VALID_DMA_MASK)
156                 panic("isa_dma_release: channel out of range");
157
158         if ((dma_inuse & (1 << chan)) == 0)
159                 printf("isa_dma_release: channel %d not in use\n", chan);
160 #endif
161
162         if (dma_busy & (1 << chan)) {
163                 dma_busy &= ~(1 << chan);
164                 /* 
165                  * XXX We should also do "dma_bounced &= (1 << chan);"
166                  * because we are acting on behalf of isa_dmadone() which
167                  * was not called to end the last DMA operation.  This does
168                  * not matter now, but it may in the future.
169                  */
170         }
171
172         dma_inuse &= ~(1 << chan);
173         dma_auto_mode &= ~(1 << chan);
174 }
175
176 /*
177  * isa_dmacascade(): program 8237 DMA controller channel to accept
178  * external dma control by a board.
179  */
180 void
181 isa_dmacascade(chan)
182         int chan;
183 {
184 #ifdef DIAGNOSTIC
185         if (chan & ~VALID_DMA_MASK)
186                 panic("isa_dmacascade: channel out of range");
187 #endif
188
189         /* set dma channel mode, and set dma channel mode */
190         if ((chan & 4) == 0) {
191                 outb(DMA1_MODE, DMA37MD_CASCADE | chan);
192                 outb(DMA1_SMSK, chan);
193         } else {
194                 outb(DMA2_MODE, DMA37MD_CASCADE | (chan & 3));
195                 outb(DMA2_SMSK, chan & 3);
196         }
197 }
198
199 /*
200  * isa_dmastart(): program 8237 DMA controller channel.
201  */
202
203 struct isa_dmastart_arg {
204         caddr_t addr;
205         int     chan;
206         int     flags;
207 };
208
209 static void isa_dmastart_cb(void *arg, bus_dma_segment_t *segs, int nseg,
210                             int error)
211 {
212 #if 0
213         caddr_t addr = ((struct isa_dmastart_arg *) arg)->addr;
214 #endif
215         int chan = ((struct isa_dmastart_arg *) arg)->chan;
216         int flags = ((struct isa_dmastart_arg *) arg)->flags;
217         bus_addr_t phys = segs->ds_addr;
218         int nbytes = segs->ds_len;
219         int waport;
220
221         if (nseg != 1)
222                 panic("isa_dmastart: transfer mapping not contiguous");
223
224 #if 0
225         if ((chipset.sgmap == NULL) && 
226             (pmap_extract(kernel_pmap, (vm_offset_t)addr)
227                 > BUS_SPACE_MAXADDR_24BIT)) { 
228                 /* we bounced */
229                 dma_bounced |= (1 << chan);
230                 /* copy bounce buffer on write */
231                 if (!(flags & ISADMA_READ)) 
232                         bus_dmamap_sync(dma_tag[chan], dma_map[chan], 
233                                           BUS_DMASYNC_PREWRITE);
234         }
235 #endif
236         
237         if ((chan & 4) == 0) {
238                 /*
239                  * Program one of DMA channels 0..3.  These are
240                  * byte mode channels.
241                  */
242                 /* set dma channel mode, and reset address ff */
243
244                 /* If ISADMA_RAW flag is set, then use autoinitialise mode */
245                 if (flags & ISADMA_RAW) {
246                   if (flags & ISADMA_READ)
247                         outb(DMA1_MODE, DMA37MD_AUTO|DMA37MD_WRITE|chan);
248                   else
249                         outb(DMA1_MODE, DMA37MD_AUTO|DMA37MD_READ|chan);
250                 }
251                 else
252                 if (flags & ISADMA_READ)
253                         outb(DMA1_MODE, DMA37MD_SINGLE|DMA37MD_WRITE|chan);
254                 else
255                         outb(DMA1_MODE, DMA37MD_SINGLE|DMA37MD_READ|chan);
256                 outb(DMA1_FFC, 0);
257
258                 /* send start address */
259                 waport =  DMA1_CHN(chan);
260                 outb(waport, phys);
261                 outb(waport, phys>>8);
262                 outb(dmapageport[chan], phys>>16);
263
264                 /* send count */
265                 outb(waport + 1, --nbytes);
266                 outb(waport + 1, nbytes>>8);
267
268                 /* unmask channel */
269                 outb(DMA1_SMSK, chan);
270         } else {
271                 /*
272                  * Program one of DMA channels 4..7.  These are
273                  * word mode channels.
274                  */
275                 /* set dma channel mode, and reset address ff */
276
277                 /* If ISADMA_RAW flag is set, then use autoinitialise mode */
278                 if (flags & ISADMA_RAW) {
279                   if (flags & ISADMA_READ)
280                         outb(DMA2_MODE, DMA37MD_AUTO|DMA37MD_WRITE|(chan&3));
281                   else
282                         outb(DMA2_MODE, DMA37MD_AUTO|DMA37MD_READ|(chan&3));
283                 }
284                 else
285                 if (flags & ISADMA_READ)
286                         outb(DMA2_MODE, DMA37MD_SINGLE|DMA37MD_WRITE|(chan&3));
287                 else
288                         outb(DMA2_MODE, DMA37MD_SINGLE|DMA37MD_READ|(chan&3));
289                 outb(DMA2_FFC, 0);
290
291                 /* send start address */
292                 waport = DMA2_CHN(chan - 4);
293                 outb(waport, phys>>1);
294                 outb(waport, phys>>9);
295                 outb(dmapageport[chan], phys>>16);
296
297                 /* send count */
298                 nbytes >>= 1;
299                 outb(waport + 2, --nbytes);
300                 outb(waport + 2, nbytes>>8);
301
302                 /* unmask channel */
303                 outb(DMA2_SMSK, chan & 3);
304         }
305 }
306
307 void
308 isa_dmastart(int flags, caddr_t addr, u_int nbytes, int chan)
309 {
310         struct isa_dmastart_arg args;
311
312 #ifdef DIAGNOSTIC
313         if (chan & ~VALID_DMA_MASK)
314                 panic("isa_dmastart: channel out of range");
315
316         if ((chan < 4 && nbytes > (1<<16))
317             || (chan >= 4 && (nbytes > (1<<17) || (uintptr_t)addr & 1)))
318                 panic("isa_dmastart: impossible request");
319
320         if ((dma_inuse & (1 << chan)) == 0)
321                 printf("isa_dmastart: channel %d not acquired\n", chan);
322 #endif
323
324 #if 0
325         /*
326          * XXX This should be checked, but drivers like ad1848 only call
327          * isa_dmastart() once because they use Auto DMA mode.  If we
328          * leave this in, drivers that do this will print this continuously.
329          */
330         if (dma_busy & (1 << chan))
331                 printf("isa_dmastart: channel %d busy\n", chan);
332 #endif
333
334         if (!dma_tag[chan] || !dma_map[chan])
335                 panic("isa_dmastart: called without isa_dma_init");
336
337         dma_busy |= (1 << chan);
338
339         if (flags & ISADMA_RAW) {
340                 dma_auto_mode |= (1 << chan);
341         } else { 
342                 dma_auto_mode &= ~(1 << chan);
343         }
344
345         /*
346          * Freeze dma while updating registers.
347          */
348         outb(chan & 4 ? DMA2_SMSK : DMA1_SMSK, (chan & 3) | 4);
349
350         args.addr = addr;
351         args.chan = chan;
352         args.flags = flags;
353         bus_dmamap_load(dma_tag[chan], dma_map[chan], addr, nbytes,
354                         isa_dmastart_cb, &args, 0);
355 }
356
357 void
358 isa_dmadone(int flags, caddr_t addr, int nbytes, int chan)
359 {  
360 #ifdef DIAGNOSTIC
361         if (chan & ~VALID_DMA_MASK)
362                 panic("isa_dmadone: channel out of range");
363
364         if ((dma_inuse & (1 << chan)) == 0)
365                 printf("isa_dmadone: channel %d not acquired\n", chan);
366 #endif
367
368         if (((dma_busy & (1 << chan)) == 0) && 
369             (dma_auto_mode & (1 << chan)) == 0 )
370                 printf("isa_dmadone: channel %d not busy\n", chan);
371
372         if (dma_bounced & (1 << chan)) {
373                 /* copy bounce buffer on read */
374                 if (flags & ISADMA_READ) {
375                         bus_dmamap_sync(dma_tag[chan], dma_map[chan],
376                                           BUS_DMASYNC_POSTREAD);
377                 }
378                 dma_bounced &= ~(1 << chan);
379         }
380
381         if ((dma_auto_mode & (1 << chan)) == 0) {
382                 outb(chan & 4 ? DMA2_SMSK : DMA1_SMSK, (chan & 3) | 4);
383                 bus_dmamap_unload(dma_tag[chan], dma_map[chan]);
384         }
385
386         dma_busy &= ~(1 << chan);
387 }
388
389 /*
390  * Query the progress of a transfer on a DMA channel.
391  *
392  * To avoid having to interrupt a transfer in progress, we sample
393  * each of the high and low databytes twice, and apply the following
394  * logic to determine the correct count.
395  *
396  * Reads are performed with interrupts disabled, thus it is to be
397  * expected that the time between reads is very small.  At most
398  * one rollover in the low count byte can be expected within the
399  * four reads that are performed.
400  *
401  * There are three gaps in which a rollover can occur :
402  *
403  * - read low1
404  *              gap1
405  * - read high1
406  *              gap2
407  * - read low2
408  *              gap3
409  * - read high2
410  *
411  * If a rollover occurs in gap1 or gap2, the low2 value will be
412  * greater than the low1 value.  In this case, low2 and high2 are a
413  * corresponding pair. 
414  *
415  * In any other case, low1 and high1 can be considered to be correct.
416  *
417  * The function returns the number of bytes remaining in the transfer,
418  * or -1 if the channel requested is not active.
419  *
420  */
421 int
422 isa_dmastatus(int chan)
423 {
424         u_long  cnt = 0;
425         int     ffport, waport;
426         u_long  low1, high1, low2, high2;
427         int s;
428
429         /* channel active? */
430         if ((dma_inuse & (1 << chan)) == 0) {
431                 printf("isa_dmastatus: channel %d not active\n", chan);
432                 return(-1);
433         }
434         /* channel busy? */
435
436         if (((dma_busy & (1 << chan)) == 0) &&
437             (dma_auto_mode & (1 << chan)) == 0 ) {
438             printf("chan %d not busy\n", chan);
439             return -2 ;
440         }       
441         if (chan < 4) {                 /* low DMA controller */
442                 ffport = DMA1_FFC;
443                 waport = DMA1_CHN(chan) + 1;
444         } else {                        /* high DMA controller */
445                 ffport = DMA2_FFC;
446                 waport = DMA2_CHN(chan - 4) + 2;
447         }
448
449         s = splhigh();                  /* no interrupts Mr Jones! */
450         outb(ffport, 0);                /* clear register LSB flipflop */
451         low1 = inb(waport);
452         high1 = inb(waport);
453         outb(ffport, 0);                /* clear again */
454         low2 = inb(waport);
455         high2 = inb(waport);
456         splx(s);                        /* enable interrupts again */
457
458         /* 
459          * Now decide if a wrap has tried to skew our results.
460          * Note that after TC, the count will read 0xffff, while we want 
461          * to return zero, so we add and then mask to compensate.
462          */
463         if (low1 >= low2) {
464                 cnt = (low1 + (high1 << 8) + 1) & 0xffff;
465         } else {
466                 cnt = (low2 + (high2 << 8) + 1) & 0xffff;
467         }
468
469         if (chan >= 4)                  /* high channels move words */
470                 cnt *= 2;
471         return(cnt);
472 }
473
474 /*
475  * Reached terminal count yet ?
476  */
477 int
478 isa_dmatc(int chan)
479 {
480
481         if (chan < 4)
482                 return(inb(DMA1_STATUS) & (1 << chan));
483         else
484                 return(inb(DMA2_STATUS) & (1 << (chan & 3)));
485 }
486
487 /*
488  * Stop a DMA transfer currently in progress.
489  */
490 int
491 isa_dmastop(int chan) 
492 {
493         if ((dma_inuse & (1 << chan)) == 0)
494                 printf("isa_dmastop: channel %d not acquired\n", chan);  
495
496         if (((dma_busy & (1 << chan)) == 0) &&
497             ((dma_auto_mode & (1 << chan)) == 0)) {
498                 printf("chan %d not busy\n", chan);
499                 return -2 ;
500         }
501     
502         if ((chan & 4) == 0) {
503                 outb(DMA1_SMSK, (chan & 3) | 4 /* disable mask */);
504         } else {
505                 outb(DMA2_SMSK, (chan & 3) | 4 /* disable mask */);
506         }
507         return(isa_dmastatus(chan));
508 }