Deleted Added
full compact
cbus_dma.c (146214) cbus_dma.c (199220)
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

--- 19 unchanged lines hidden (view full) ---

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 */
34
35#include <sys/cdefs.h>
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

--- 19 unchanged lines hidden (view full) ---

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 */
34
35#include <sys/cdefs.h>
36__FBSDID("$FreeBSD: head/sys/pc98/cbus/cbus_dma.c 146214 2005-05-14 10:14:56Z nyan $");
36__FBSDID("$FreeBSD: head/sys/pc98/cbus/cbus_dma.c 199220 2009-11-12 11:27:07Z nyan $");
37
38/*
39 * code to manage AT bus
40 *
41 * 92/08/18 Frank P. MacLachlan (fpm@crash.cts.com):
42 * Fixed uninitialized variable problem and added code to deal
43 * with DMA page boundaries in isa_dmarangecheck(). Fixed word
44 * mode DMA count compution and reorganized DMA setup code in

--- 22 unchanged lines hidden (view full) ---

67static int isa_dmarangecheck(caddr_t va, u_int length, int chan);
68
69static caddr_t dma_bouncebuf[4];
70static u_int dma_bouncebufsize[4];
71static u_int8_t dma_bounced = 0;
72static u_int8_t dma_busy = 0; /* Used in isa_dmastart() */
73static u_int8_t dma_inuse = 0; /* User for acquire/release */
74static u_int8_t dma_auto_mode = 0;
37
38/*
39 * code to manage AT bus
40 *
41 * 92/08/18 Frank P. MacLachlan (fpm@crash.cts.com):
42 * Fixed uninitialized variable problem and added code to deal
43 * with DMA page boundaries in isa_dmarangecheck(). Fixed word
44 * mode DMA count compution and reorganized DMA setup code in

--- 22 unchanged lines hidden (view full) ---

67static int isa_dmarangecheck(caddr_t va, u_int length, int chan);
68
69static caddr_t dma_bouncebuf[4];
70static u_int dma_bouncebufsize[4];
71static u_int8_t dma_bounced = 0;
72static u_int8_t dma_busy = 0; /* Used in isa_dmastart() */
73static u_int8_t dma_inuse = 0; /* User for acquire/release */
74static u_int8_t dma_auto_mode = 0;
75static struct mtx isa_dma_lock;
76MTX_SYSINIT(isa_dma_lock, &isa_dma_lock, "isa DMA lock", MTX_DEF);
75
76#define VALID_DMA_MASK (3)
77
78/* high byte of address is stored in this port for i-th dma channel */
79static int dmapageport[4] = { 0x27, 0x21, 0x23, 0x25 };
80
81/*
82 * Setup a DMA channel's bounce buffer.

--- 5 unchanged lines hidden (view full) ---

88
89#ifdef DIAGNOSTIC
90 if (chan & ~VALID_DMA_MASK)
91 panic("isa_dma_init: channel out of range");
92 if (dma_bouncebuf[chan] != NULL)
93 panic("isa_dma_init: impossible request");
94#endif
95
77
78#define VALID_DMA_MASK (3)
79
80/* high byte of address is stored in this port for i-th dma channel */
81static int dmapageport[4] = { 0x27, 0x21, 0x23, 0x25 };
82
83/*
84 * Setup a DMA channel's bounce buffer.

--- 5 unchanged lines hidden (view full) ---

90
91#ifdef DIAGNOSTIC
92 if (chan & ~VALID_DMA_MASK)
93 panic("isa_dma_init: channel out of range");
94 if (dma_bouncebuf[chan] != NULL)
95 panic("isa_dma_init: impossible request");
96#endif
97
96 dma_bouncebufsize[chan] = bouncebufsize;
97
98 /* Try malloc() first. It works better if it works. */
99 buf = malloc(bouncebufsize, M_DEVBUF, flag);
100 if (buf != NULL) {
98
99 /* Try malloc() first. It works better if it works. */
100 buf = malloc(bouncebufsize, M_DEVBUF, flag);
101 if (buf != NULL) {
101 if (isa_dmarangecheck(buf, bouncebufsize, chan) == 0) {
102 dma_bouncebuf[chan] = buf;
103 return (0);
102 if (isa_dmarangecheck(buf, bouncebufsize, chan) != 0) {
103 free(buf, M_DEVBUF);
104 buf = NULL;
104 }
105 }
105 free(buf, M_DEVBUF);
106 }
106 }
107 buf = contigmalloc(bouncebufsize, M_DEVBUF, flag, 0ul, 0xfffffful,
107
108 if (buf == NULL) {
109 buf = contigmalloc(bouncebufsize, M_DEVBUF, flag, 0ul, 0xfffffful,
108 1ul, chan & 4 ? 0x20000ul : 0x10000ul);
110 1ul, chan & 4 ? 0x20000ul : 0x10000ul);
111 }
112
109 if (buf == NULL)
110 return (ENOMEM);
113 if (buf == NULL)
114 return (ENOMEM);
115
116 mtx_lock(&isa_dma_lock);
117
118 dma_bouncebufsize[chan] = bouncebufsize;
111 dma_bouncebuf[chan] = buf;
119 dma_bouncebuf[chan] = buf;
120
121 mtx_unlock(&isa_dma_lock);
122
112 return (0);
113}
114
115/*
116 * Register a DMA channel's usage. Usually called from a device driver
117 * in open() or during its initialization.
118 */
119int
120isa_dma_acquire(chan)
121 int chan;
122{
123#ifdef DIAGNOSTIC
124 if (chan & ~VALID_DMA_MASK)
125 panic("isa_dma_acquire: channel out of range");
126#endif
127
123 return (0);
124}
125
126/*
127 * Register a DMA channel's usage. Usually called from a device driver
128 * in open() or during its initialization.
129 */
130int
131isa_dma_acquire(chan)
132 int chan;
133{
134#ifdef DIAGNOSTIC
135 if (chan & ~VALID_DMA_MASK)
136 panic("isa_dma_acquire: channel out of range");
137#endif
138
139 mtx_lock(&isa_dma_lock);
128 if (dma_inuse & (1 << chan)) {
129 printf("isa_dma_acquire: channel %d already in use\n", chan);
140 if (dma_inuse & (1 << chan)) {
141 printf("isa_dma_acquire: channel %d already in use\n", chan);
142 mtx_unlock(&isa_dma_lock);
130 return (EBUSY);
131 }
132 dma_inuse |= (1 << chan);
133 dma_auto_mode &= ~(1 << chan);
143 return (EBUSY);
144 }
145 dma_inuse |= (1 << chan);
146 dma_auto_mode &= ~(1 << chan);
147 mtx_unlock(&isa_dma_lock);
134
135 return (0);
136}
137
138/*
139 * Unregister a DMA channel's usage. Usually called from a device driver
140 * during close() or during its shutdown.
141 */
142void
143isa_dma_release(chan)
144 int chan;
145{
146#ifdef DIAGNOSTIC
147 if (chan & ~VALID_DMA_MASK)
148 panic("isa_dma_release: channel out of range");
149
148
149 return (0);
150}
151
152/*
153 * Unregister a DMA channel's usage. Usually called from a device driver
154 * during close() or during its shutdown.
155 */
156void
157isa_dma_release(chan)
158 int chan;
159{
160#ifdef DIAGNOSTIC
161 if (chan & ~VALID_DMA_MASK)
162 panic("isa_dma_release: channel out of range");
163
164 mtx_lock(&isa_dma_lock);
150 if ((dma_inuse & (1 << chan)) == 0)
151 printf("isa_dma_release: channel %d not in use\n", chan);
165 if ((dma_inuse & (1 << chan)) == 0)
166 printf("isa_dma_release: channel %d not in use\n", chan);
167#else
168 mtx_lock(&isa_dma_lock);
152#endif
153
154 if (dma_busy & (1 << chan)) {
155 dma_busy &= ~(1 << chan);
156 /*
157 * XXX We should also do "dma_bounced &= (1 << chan);"
158 * because we are acting on behalf of isa_dmadone() which
159 * was not called to end the last DMA operation. This does
160 * not matter now, but it may in the future.
161 */
162 }
163
164 dma_inuse &= ~(1 << chan);
165 dma_auto_mode &= ~(1 << chan);
169#endif
170
171 if (dma_busy & (1 << chan)) {
172 dma_busy &= ~(1 << chan);
173 /*
174 * XXX We should also do "dma_bounced &= (1 << chan);"
175 * because we are acting on behalf of isa_dmadone() which
176 * was not called to end the last DMA operation. This does
177 * not matter now, but it may in the future.
178 */
179 }
180
181 dma_inuse &= ~(1 << chan);
182 dma_auto_mode &= ~(1 << chan);
183
184 mtx_unlock(&isa_dma_lock);
166}
167
168/*
169 * isa_dmastart(): program 8237 DMA controller channel, avoid page alignment
170 * problems by using a bounce buffer.
171 */
172void
173isa_dmastart(int flags, caddr_t addr, u_int nbytes, int chan)
174{
175 vm_paddr_t phys;
176 int waport;
177 caddr_t newaddr;
185}
186
187/*
188 * isa_dmastart(): program 8237 DMA controller channel, avoid page alignment
189 * problems by using a bounce buffer.
190 */
191void
192isa_dmastart(int flags, caddr_t addr, u_int nbytes, int chan)
193{
194 vm_paddr_t phys;
195 int waport;
196 caddr_t newaddr;
197 int dma_range_checked;
178
198
179 GIANT_REQUIRED;
199 /* translate to physical */
200 phys = pmap_extract(kernel_pmap, (vm_offset_t)addr);
201 dma_range_checked = isa_dmarangecheck(addr, nbytes, chan);
180
181#ifdef DIAGNOSTIC
182 if (chan & ~VALID_DMA_MASK)
183 panic("isa_dmastart: channel out of range");
184
185 if ((chan < 4 && nbytes > (1<<16))
186 || (chan >= 4 && (nbytes > (1<<17) || (u_int)addr & 1)))
187 panic("isa_dmastart: impossible request");
188
202
203#ifdef DIAGNOSTIC
204 if (chan & ~VALID_DMA_MASK)
205 panic("isa_dmastart: channel out of range");
206
207 if ((chan < 4 && nbytes > (1<<16))
208 || (chan >= 4 && (nbytes > (1<<17) || (u_int)addr & 1)))
209 panic("isa_dmastart: impossible request");
210
211 mtx_lock(&isa_dma_lock);
189 if ((dma_inuse & (1 << chan)) == 0)
190 printf("isa_dmastart: channel %d not acquired\n", chan);
212 if ((dma_inuse & (1 << chan)) == 0)
213 printf("isa_dmastart: channel %d not acquired\n", chan);
214#else
215 mtx_lock(&isa_dma_lock);
191#endif
192
193#if 0
194 /*
195 * XXX This should be checked, but drivers like ad1848 only call
196 * isa_dmastart() once because they use Auto DMA mode. If we
197 * leave this in, drivers that do this will print this continuously.
198 */
199 if (dma_busy & (1 << chan))
200 printf("isa_dmastart: channel %d busy\n", chan);
201#endif
202
203 dma_busy |= (1 << chan);
204
216#endif
217
218#if 0
219 /*
220 * XXX This should be checked, but drivers like ad1848 only call
221 * isa_dmastart() once because they use Auto DMA mode. If we
222 * leave this in, drivers that do this will print this continuously.
223 */
224 if (dma_busy & (1 << chan))
225 printf("isa_dmastart: channel %d busy\n", chan);
226#endif
227
228 dma_busy |= (1 << chan);
229
205 if (isa_dmarangecheck(addr, nbytes, chan)) {
230 if (dma_range_checked) {
206 if (dma_bouncebuf[chan] == NULL
207 || dma_bouncebufsize[chan] < nbytes)
208 panic("isa_dmastart: bad bounce buffer");
209 dma_bounced |= (1 << chan);
210 newaddr = dma_bouncebuf[chan];
211
212 /* copy bounce buffer on write */
213 if (!(flags & ISADMA_READ))
214 bcopy(addr, newaddr, nbytes);
215 addr = newaddr;
216 }
217
231 if (dma_bouncebuf[chan] == NULL
232 || dma_bouncebufsize[chan] < nbytes)
233 panic("isa_dmastart: bad bounce buffer");
234 dma_bounced |= (1 << chan);
235 newaddr = dma_bouncebuf[chan];
236
237 /* copy bounce buffer on write */
238 if (!(flags & ISADMA_READ))
239 bcopy(addr, newaddr, nbytes);
240 addr = newaddr;
241 }
242
218 /* translate to physical */
219 phys = pmap_extract(kernel_pmap, (vm_offset_t)addr);
220
221 if (flags & ISADMA_RAW) {
222 dma_auto_mode |= (1 << chan);
223 } else {
224 dma_auto_mode &= ~(1 << chan);
225 }
226
227 if (need_pre_dma_flush)
228 wbinvd(); /* wbinvd (WB cache flush) */

--- 21 unchanged lines hidden (view full) ---

250 outb(dmapageport[chan], phys>>16);
251
252 /* send count */
253 outb(waport + 2, --nbytes);
254 outb(waport + 2, nbytes>>8);
255
256 /* unmask channel */
257 outb(DMA1_SMSK, chan);
243 if (flags & ISADMA_RAW) {
244 dma_auto_mode |= (1 << chan);
245 } else {
246 dma_auto_mode &= ~(1 << chan);
247 }
248
249 if (need_pre_dma_flush)
250 wbinvd(); /* wbinvd (WB cache flush) */

--- 21 unchanged lines hidden (view full) ---

272 outb(dmapageport[chan], phys>>16);
273
274 /* send count */
275 outb(waport + 2, --nbytes);
276 outb(waport + 2, nbytes>>8);
277
278 /* unmask channel */
279 outb(DMA1_SMSK, chan);
280
281 mtx_unlock(&isa_dma_lock);
258}
259
260void
261isa_dmadone(int flags, caddr_t addr, int nbytes, int chan)
262{
263
264 if (flags & ISADMA_READ) {
265 /* cache flush only after reading 92/12/9 by A.Kojima */

--- 4 unchanged lines hidden (view full) ---

270#ifdef DIAGNOSTIC
271 if (chan & ~VALID_DMA_MASK)
272 panic("isa_dmadone: channel out of range");
273
274 if ((dma_inuse & (1 << chan)) == 0)
275 printf("isa_dmadone: channel %d not acquired\n", chan);
276#endif
277
282}
283
284void
285isa_dmadone(int flags, caddr_t addr, int nbytes, int chan)
286{
287
288 if (flags & ISADMA_READ) {
289 /* cache flush only after reading 92/12/9 by A.Kojima */

--- 4 unchanged lines hidden (view full) ---

294#ifdef DIAGNOSTIC
295 if (chan & ~VALID_DMA_MASK)
296 panic("isa_dmadone: channel out of range");
297
298 if ((dma_inuse & (1 << chan)) == 0)
299 printf("isa_dmadone: channel %d not acquired\n", chan);
300#endif
301
302 mtx_lock(&isa_dma_lock);
278 if (((dma_busy & (1 << chan)) == 0) &&
279 (dma_auto_mode & (1 << chan)) == 0 )
280 printf("isa_dmadone: channel %d not busy\n", chan);
281
282 if ((dma_auto_mode & (1 << chan)) == 0)
283 outb(DMA1_SMSK, (chan & 3) | 4);
284
285 if (dma_bounced & (1 << chan)) {
286 /* copy bounce buffer on read */
287 if (flags & ISADMA_READ)
288 bcopy(dma_bouncebuf[chan], addr, nbytes);
289
290 dma_bounced &= ~(1 << chan);
291 }
292 dma_busy &= ~(1 << chan);
303 if (((dma_busy & (1 << chan)) == 0) &&
304 (dma_auto_mode & (1 << chan)) == 0 )
305 printf("isa_dmadone: channel %d not busy\n", chan);
306
307 if ((dma_auto_mode & (1 << chan)) == 0)
308 outb(DMA1_SMSK, (chan & 3) | 4);
309
310 if (dma_bounced & (1 << chan)) {
311 /* copy bounce buffer on read */
312 if (flags & ISADMA_READ)
313 bcopy(dma_bouncebuf[chan], addr, nbytes);
314
315 dma_bounced &= ~(1 << chan);
316 }
317 dma_busy &= ~(1 << chan);
318 mtx_unlock(&isa_dma_lock);
293}
294
295/*
296 * Check for problems with the address range of a DMA transfer
297 * (non-contiguous physical pages, outside of bus address space,
298 * crossing DMA page boundaries).
299 * Return true if special handling needed.
300 */
301
302static int
303isa_dmarangecheck(caddr_t va, u_int length, int chan)
304{
305 vm_paddr_t phys, priorpage = 0;
306 vm_offset_t endva;
307 u_int dma_pgmsk = (chan & 4) ? ~(128*1024-1) : ~(64*1024-1);
308
319}
320
321/*
322 * Check for problems with the address range of a DMA transfer
323 * (non-contiguous physical pages, outside of bus address space,
324 * crossing DMA page boundaries).
325 * Return true if special handling needed.
326 */
327
328static int
329isa_dmarangecheck(caddr_t va, u_int length, int chan)
330{
331 vm_paddr_t phys, priorpage = 0;
332 vm_offset_t endva;
333 u_int dma_pgmsk = (chan & 4) ? ~(128*1024-1) : ~(64*1024-1);
334
309 GIANT_REQUIRED;
310
311 endva = (vm_offset_t)round_page((vm_offset_t)va + length);
312 for (; va < (caddr_t) endva ; va += PAGE_SIZE) {
313 phys = trunc_page(pmap_extract(kernel_pmap, (vm_offset_t)va));
314#ifdef EPSON_BOUNCEDMA
315#define ISARAM_END 0xf00000
316#else
317#define ISARAM_END RAM_END
318#endif

--- 40 unchanged lines hidden (view full) ---

359 * corresponding pair.
360 *
361 * In any other case, low1 and high1 can be considered to be correct.
362 *
363 * The function returns the number of bytes remaining in the transfer,
364 * or -1 if the channel requested is not active.
365 *
366 */
335 endva = (vm_offset_t)round_page((vm_offset_t)va + length);
336 for (; va < (caddr_t) endva ; va += PAGE_SIZE) {
337 phys = trunc_page(pmap_extract(kernel_pmap, (vm_offset_t)va));
338#ifdef EPSON_BOUNCEDMA
339#define ISARAM_END 0xf00000
340#else
341#define ISARAM_END RAM_END
342#endif

--- 40 unchanged lines hidden (view full) ---

383 * corresponding pair.
384 *
385 * In any other case, low1 and high1 can be considered to be correct.
386 *
387 * The function returns the number of bytes remaining in the transfer,
388 * or -1 if the channel requested is not active.
389 *
390 */
367int
368isa_dmastatus(int chan)
391static int
392isa_dmastatus_locked(int chan)
369{
370 u_long cnt = 0;
371 int ffport, waport;
372 u_long low1, high1, low2, high2;
373
393{
394 u_long cnt = 0;
395 int ffport, waport;
396 u_long low1, high1, low2, high2;
397
398 mtx_assert(&isa_dma_lock, MA_OWNED);
399
374 /* channel active? */
375 if ((dma_inuse & (1 << chan)) == 0) {
376 printf("isa_dmastatus: channel %d not active\n", chan);
377 return(-1);
378 }
379 /* channel busy? */
380
381 if (((dma_busy & (1 << chan)) == 0) &&

--- 24 unchanged lines hidden (view full) ---

406 cnt = (low2 + (high2 << 8) + 1) & 0xffff;
407 }
408
409 if (chan >= 4) /* high channels move words */
410 cnt *= 2;
411 return(cnt);
412}
413
400 /* channel active? */
401 if ((dma_inuse & (1 << chan)) == 0) {
402 printf("isa_dmastatus: channel %d not active\n", chan);
403 return(-1);
404 }
405 /* channel busy? */
406
407 if (((dma_busy & (1 << chan)) == 0) &&

--- 24 unchanged lines hidden (view full) ---

432 cnt = (low2 + (high2 << 8) + 1) & 0xffff;
433 }
434
435 if (chan >= 4) /* high channels move words */
436 cnt *= 2;
437 return(cnt);
438}
439
440int
441isa_dmastatus(int chan)
442{
443 int status;
444
445 mtx_lock(&isa_dma_lock);
446 status = isa_dmastatus_locked(chan);
447 mtx_unlock(&isa_dma_lock);
448
449 return (status);
450}
451
414/*
415 * Reached terminal count yet ?
416 */
417int
418isa_dmatc(int chan)
419{
420
421 return(inb(DMA1_STATUS) & (1 << chan));
422}
423
424/*
425 * Stop a DMA transfer currently in progress.
426 */
427int
428isa_dmastop(int chan)
429{
452/*
453 * Reached terminal count yet ?
454 */
455int
456isa_dmatc(int chan)
457{
458
459 return(inb(DMA1_STATUS) & (1 << chan));
460}
461
462/*
463 * Stop a DMA transfer currently in progress.
464 */
465int
466isa_dmastop(int chan)
467{
468 int status;
469
470 mtx_lock(&isa_dma_lock);
430 if ((dma_inuse & (1 << chan)) == 0)
431 printf("isa_dmastop: channel %d not acquired\n", chan);
432
433 if (((dma_busy & (1 << chan)) == 0) &&
434 ((dma_auto_mode & (1 << chan)) == 0)) {
435 printf("chan %d not busy\n", chan);
471 if ((dma_inuse & (1 << chan)) == 0)
472 printf("isa_dmastop: channel %d not acquired\n", chan);
473
474 if (((dma_busy & (1 << chan)) == 0) &&
475 ((dma_auto_mode & (1 << chan)) == 0)) {
476 printf("chan %d not busy\n", chan);
477 mtx_unlock(&isa_dma_lock);
436 return -2 ;
437 }
438
439 if ((chan & 4) == 0)
440 outb(DMA1_SMSK, (chan & 3) | 4 /* disable mask */);
441
478 return -2 ;
479 }
480
481 if ((chan & 4) == 0)
482 outb(DMA1_SMSK, (chan & 3) | 4 /* disable mask */);
483
442 return(isa_dmastatus(chan));
484 status = isa_dmastatus_locked(chan);
485
486 mtx_unlock(&isa_dma_lock);
487
488 return (status);
443}
444
445/*
446 * Attach to the ISA PnP descriptor for the AT DMA controller
447 */
448static struct isa_pnp_id atdma_ids[] = {
449 { 0x0002d041 /* PNP0200 */, "AT DMA controller" },
450 { 0 }

--- 38 unchanged lines hidden ---
489}
490
491/*
492 * Attach to the ISA PnP descriptor for the AT DMA controller
493 */
494static struct isa_pnp_id atdma_ids[] = {
495 { 0x0002d041 /* PNP0200 */, "AT DMA controller" },
496 { 0 }

--- 38 unchanged lines hidden ---