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