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 */
34
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
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
45 * isa_dmastart()
46 */
47
48 #include "opt_pc98.h"
49
50 #include <sys/param.h>
51 #include <sys/systm.h>
52 #include <sys/bus.h>
53 #include <sys/kernel.h>
54 #include <sys/malloc.h>
55 #include <sys/lock.h>
56 #include <sys/proc.h>
57 #include <sys/mutex.h>
58 #include <sys/module.h>
59 #include <machine/md_var.h>
60 #include <vm/vm.h>
61 #include <vm/vm_param.h>
62 #include <vm/pmap.h>
63 #include <isa/isavar.h>
64 #include <pc98/cbus/cbus.h>
65 #include <pc98/cbus/cbus_dmareg.h>
66
67 static int isa_dmarangecheck(caddr_t va, u_int length, int chan);
68
69 static caddr_t dma_bouncebuf[4];
70 static u_int dma_bouncebufsize[4];
71 static u_int8_t dma_bounced = 0;
72 static u_int8_t dma_busy = 0; /* Used in isa_dmastart() */
73 static u_int8_t dma_inuse = 0; /* User for acquire/release */
74 static u_int8_t dma_auto_mode = 0;
75 static struct mtx isa_dma_lock;
76 MTX_SYSINIT(isa_dma_lock, &isa_dma_lock, "isa DMA lock", MTX_DEF);
77
78 #define VALID_DMA_MASK (3)
79
80 /* high byte of address is stored in this port for i-th dma channel */
81 static int dmapageport[4] = { 0x27, 0x21, 0x23, 0x25 };
82
83 /*
84 * Setup a DMA channel's bounce buffer.
85 */
86 int
87 isa_dma_init(int chan, u_int bouncebufsize, int flag)
88 {
89 void *buf;
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
98
99 /* Try malloc() first. It works better if it works. */
100 buf = malloc(bouncebufsize, M_DEVBUF, flag);
101 if (buf != NULL) {
102 if (isa_dmarangecheck(buf, bouncebufsize, chan) != 0) {
103 free(buf, M_DEVBUF);
104 buf = NULL;
105 }
106 }
107
108 if (buf == NULL) {
109 buf = contigmalloc(bouncebufsize, M_DEVBUF, flag, 0ul, 0xfffffful,
110 1ul, chan & 4 ? 0x20000ul : 0x10000ul);
111 }
112
113 if (buf == NULL)
114 return (ENOMEM);
115
116 mtx_lock(&isa_dma_lock);
117
118 dma_bouncebufsize[chan] = bouncebufsize;
119 dma_bouncebuf[chan] = buf;
120
121 mtx_unlock(&isa_dma_lock);
122
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 */
130 int
131 isa_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);
140 if (dma_inuse & (1 << chan)) {
141 printf("isa_dma_acquire: channel %d already in use\n", chan);
142 mtx_unlock(&isa_dma_lock);
143 return (EBUSY);
144 }
145 dma_inuse |= (1 << chan);
146 dma_auto_mode &= ~(1 << chan);
147 mtx_unlock(&isa_dma_lock);
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 */
156 void
157 isa_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);
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);
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);
185 }
186
187 /*
188 * isa_dmastart(): program 8237 DMA controller channel, avoid page alignment
189 * problems by using a bounce buffer.
190 */
191 void
192 isa_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;
198
199 /* translate to physical */
200 phys = pmap_extract(kernel_pmap, (vm_offset_t)addr);
201 dma_range_checked = isa_dmarangecheck(addr, nbytes, chan);
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);
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);
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
230 if (dma_range_checked) {
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
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) */
251
252 /* set dma channel mode, and reset address ff */
253
254 /* If ISADMA_RAW flag is set, then use autoinitialise mode */
255 if (flags & ISADMA_RAW) {
256 if (flags & ISADMA_READ)
257 outb(DMA1_MODE, DMA37MD_AUTO|DMA37MD_WRITE|chan);
258 else
259 outb(DMA1_MODE, DMA37MD_AUTO|DMA37MD_READ|chan);
260 } else {
261 if (flags & ISADMA_READ)
262 outb(DMA1_MODE, DMA37MD_SINGLE|DMA37MD_WRITE|chan);
263 else
264 outb(DMA1_MODE, DMA37MD_SINGLE|DMA37MD_READ|chan);
265 }
266 outb(DMA1_FFC, 0);
267
268 /* send start address */
269 waport = DMA1_CHN(chan);
270 outb(waport, phys);
271 outb(waport, phys>>8);
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);
282 }
283
284 void
285 isa_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 */
290 if (need_post_dma_flush)
291 invd();
292 }
293
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);
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);
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
328 static int
329 isa_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
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 0x0f00000
340 #else
341 #define ISARAM_END 0x1000000
342 #endif
343 if (phys == 0)
344 panic("isa_dmacheck: no physical page present");
345 if (phys >= ISARAM_END)
346 return (1);
347 if (priorpage) {
348 if (priorpage + PAGE_SIZE != phys)
349 return (1);
350 /* check if crossing a DMA page boundary */
351 if (((u_int)priorpage ^ (u_int)phys) & dma_pgmsk)
352 return (1);
353 }
354 priorpage = phys;
355 }
356 return (0);
357 }
358
359 /*
360 * Query the progress of a transfer on a DMA channel.
361 *
362 * To avoid having to interrupt a transfer in progress, we sample
363 * each of the high and low databytes twice, and apply the following
364 * logic to determine the correct count.
365 *
366 * Reads are performed with interrupts disabled, thus it is to be
367 * expected that the time between reads is very small. At most
368 * one rollover in the low count byte can be expected within the
369 * four reads that are performed.
370 *
371 * There are three gaps in which a rollover can occur :
372 *
373 * - read low1
374 * gap1
375 * - read high1
376 * gap2
377 * - read low2
378 * gap3
379 * - read high2
380 *
381 * If a rollover occurs in gap1 or gap2, the low2 value will be
382 * greater than the low1 value. In this case, low2 and high2 are a
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 */
391 static int
392 isa_dmastatus_locked(int chan)
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
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) &&
408 (dma_auto_mode & (1 << chan)) == 0 ) {
409 printf("chan %d not busy\n", chan);
410 return -2 ;
411 }
412 ffport = DMA1_FFC;
413 waport = DMA1_CHN(chan) + 2;
414
415 disable_intr(); /* no interrupts Mr Jones! */
416 outb(ffport, 0); /* clear register LSB flipflop */
417 low1 = inb(waport);
418 high1 = inb(waport);
419 outb(ffport, 0); /* clear again */
420 low2 = inb(waport);
421 high2 = inb(waport);
422 enable_intr(); /* enable interrupts again */
423
424 /*
425 * Now decide if a wrap has tried to skew our results.
426 * Note that after TC, the count will read 0xffff, while we want
427 * to return zero, so we add and then mask to compensate.
428 */
429 if (low1 >= low2) {
430 cnt = (low1 + (high1 << 8) + 1) & 0xffff;
431 } else {
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
440 int
441 isa_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
452 /*
453 * Reached terminal count yet ?
454 */
455 int
456 isa_dmatc(int chan)
457 {
458
459 return(inb(DMA1_STATUS) & (1 << chan));
460 }
461
462 /*
463 * Stop a DMA transfer currently in progress.
464 */
465 int
466 isa_dmastop(int chan)
467 {
468 int status;
469
470 mtx_lock(&isa_dma_lock);
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);
478 return -2 ;
479 }
480
481 if ((chan & 4) == 0)
482 outb(DMA1_SMSK, (chan & 3) | 4 /* disable mask */);
483
484 status = isa_dmastatus_locked(chan);
485
486 mtx_unlock(&isa_dma_lock);
487
488 return (status);
489 }
490
491 /*
492 * Attach to the ISA PnP descriptor for the AT DMA controller
493 */
494 static struct isa_pnp_id atdma_ids[] = {
495 { 0x0002d041 /* PNP0200 */, "AT DMA controller" },
496 { 0 }
497 };
498
499 static int
500 atdma_probe(device_t dev)
501 {
502 int result;
503
504 if ((result = ISA_PNP_PROBE(device_get_parent(dev), dev, atdma_ids)) <= 0)
505 device_quiet(dev);
506 return(result);
507 }
508
509 static int
510 atdma_attach(device_t dev)
511 {
512 return(0);
513 }
514
515 static device_method_t atdma_methods[] = {
516 /* Device interface */
517 DEVMETHOD(device_probe, atdma_probe),
518 DEVMETHOD(device_attach, atdma_attach),
519 DEVMETHOD(device_detach, bus_generic_detach),
520 DEVMETHOD(device_shutdown, bus_generic_shutdown),
521 DEVMETHOD(device_suspend, bus_generic_suspend),
522 DEVMETHOD(device_resume, bus_generic_resume),
523 { 0, 0 }
524 };
525
526 static driver_t atdma_driver = {
527 "atdma",
528 atdma_methods,
529 1, /* no softc */
530 };
531
532 static devclass_t atdma_devclass;
533
534 DRIVER_MODULE(atdma, isa, atdma_driver, atdma_devclass, 0, 0);
Cache object: 520cb6c7ed3b3a6f28bda6ff2b5d5b6f
|