isadma.c revision 1.1 1 1.1 mycroft /*-
2 1.1 mycroft * Copyright (c) 1993 Charles Hannum.
3 1.1 mycroft * Copyright (c) 1991 The Regents of the University of California.
4 1.1 mycroft * All rights reserved.
5 1.1 mycroft *
6 1.1 mycroft * This code is derived from software contributed to Berkeley by
7 1.1 mycroft * William Jolitz.
8 1.1 mycroft *
9 1.1 mycroft * Redistribution and use in source and binary forms, with or without
10 1.1 mycroft * modification, are permitted provided that the following conditions
11 1.1 mycroft * are met:
12 1.1 mycroft * 1. Redistributions of source code must retain the above copyright
13 1.1 mycroft * notice, this list of conditions and the following disclaimer.
14 1.1 mycroft * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 mycroft * notice, this list of conditions and the following disclaimer in the
16 1.1 mycroft * documentation and/or other materials provided with the distribution.
17 1.1 mycroft * 3. All advertising materials mentioning features or use of this software
18 1.1 mycroft * must display the following acknowledgement:
19 1.1 mycroft * This product includes software developed by the University of
20 1.1 mycroft * California, Berkeley and its contributors.
21 1.1 mycroft * 4. Neither the name of the University nor the names of its contributors
22 1.1 mycroft * may be used to endorse or promote products derived from this software
23 1.1 mycroft * without specific prior written permission.
24 1.1 mycroft *
25 1.1 mycroft * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 1.1 mycroft * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 1.1 mycroft * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 1.1 mycroft * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 1.1 mycroft * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 1.1 mycroft * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 1.1 mycroft * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 1.1 mycroft * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 1.1 mycroft * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 1.1 mycroft * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 1.1 mycroft * SUCH DAMAGE.
36 1.1 mycroft *
37 1.1 mycroft * from: @(#)isa.c 7.2 (Berkeley) 5/13/91
38 1.1 mycroft * $Id: isadma.c,v 1.1 1993/10/14 05:22:57 mycroft Exp $
39 1.1 mycroft */
40 1.1 mycroft
41 1.1 mycroft /*
42 1.1 mycroft * code to deal with ISA DMA
43 1.1 mycroft */
44 1.1 mycroft
45 1.1 mycroft #include "param.h"
46 1.1 mycroft #include "systm.h"
47 1.1 mycroft #include "conf.h"
48 1.1 mycroft #include "file.h"
49 1.1 mycroft #include "syslog.h"
50 1.1 mycroft #include "machine/cpu.h"
51 1.1 mycroft #include "machine/pio.h"
52 1.1 mycroft #include "sys/device.h"
53 1.1 mycroft #include "vm/vm.h"
54 1.1 mycroft #include "i386/isa/isa.h"
55 1.1 mycroft #include "i386/isa/isavar.h"
56 1.1 mycroft #include "i386/isa/ic/i8237.h"
57 1.1 mycroft
58 1.1 mycroft /*
59 1.1 mycroft ** Register definitions for DMA controller 1 (channels 0..3):
60 1.1 mycroft */
61 1.1 mycroft #define DMA1_CHN(c) (IO_DMA1 + 1*(2*(c))) /* addr reg for channel c */
62 1.1 mycroft #define DMA1_SR (IO_DMA1 + 1*8) /* status register */
63 1.1 mycroft #define DMA1_SMSK (IO_DMA1 + 1*10) /* single mask register */
64 1.1 mycroft #define DMA1_MODE (IO_DMA1 + 1*11) /* mode register */
65 1.1 mycroft #define DMA1_FFC (IO_DMA1 + 1*12) /* clear first/last FF */
66 1.1 mycroft
67 1.1 mycroft /*
68 1.1 mycroft ** Register definitions for DMA controller 2 (channels 4..7):
69 1.1 mycroft */
70 1.1 mycroft #define DMA2_CHN(c) (IO_DMA2 + 2*(2*(c))) /* addr reg for channel c */
71 1.1 mycroft #define DMA2_SR (IO_DMA2 + 2*8) /* status register */
72 1.1 mycroft #define DMA2_SMSK (IO_DMA2 + 2*10) /* single mask register */
73 1.1 mycroft #define DMA2_MODE (IO_DMA2 + 2*11) /* mode register */
74 1.1 mycroft #define DMA2_FFC (IO_DMA2 + 2*12) /* clear first/last FF */
75 1.1 mycroft
76 1.1 mycroft /* region of physical memory known to be contiguous */
77 1.1 mycroft caddr_t isaphysmem;
78 1.1 mycroft static caddr_t bouncebuf[8]; /* XXX */
79 1.1 mycroft static caddr_t bounced[8]; /* XXX */
80 1.1 mycroft static vm_size_t bouncesize[8]; /* XXX */
81 1.1 mycroft
82 1.1 mycroft /* high byte of address is stored in this port for i-th dma channel */
83 1.1 mycroft static u_short dmapageport[8] =
84 1.1 mycroft { 0x87, 0x83, 0x81, 0x82, 0x8f, 0x8b, 0x89, 0x8a };
85 1.1 mycroft
86 1.1 mycroft /*
87 1.1 mycroft * at_dma_cascade(): program 8237 DMA controller channel to accept
88 1.1 mycroft * external dma control by a board.
89 1.1 mycroft */
90 1.1 mycroft void
91 1.1 mycroft at_dma_cascade(chan)
92 1.1 mycroft unsigned chan;
93 1.1 mycroft {
94 1.1 mycroft #ifdef DIAGNOSTIC
95 1.1 mycroft if (chan > 7)
96 1.1 mycroft panic("at_dma_cascade: impossible request");
97 1.1 mycroft #endif
98 1.1 mycroft
99 1.1 mycroft /* set dma channel mode, and set dma channel mode */
100 1.1 mycroft if ((chan & 4) == 0) {
101 1.1 mycroft outb(DMA1_MODE, DMA37MD_CASCADE | chan);
102 1.1 mycroft outb(DMA1_SMSK, chan);
103 1.1 mycroft } else {
104 1.1 mycroft outb(DMA2_MODE, DMA37MD_CASCADE | (chan & 3));
105 1.1 mycroft outb(DMA2_SMSK, chan & 3);
106 1.1 mycroft }
107 1.1 mycroft }
108 1.1 mycroft
109 1.1 mycroft /*
110 1.1 mycroft * at_dma(): program 8237 DMA controller channel, avoid page alignment
111 1.1 mycroft * problems by using a bounce buffer.
112 1.1 mycroft */
113 1.1 mycroft void
114 1.1 mycroft at_dma(read, addr, nbytes, chan)
115 1.1 mycroft int read;
116 1.1 mycroft caddr_t addr;
117 1.1 mycroft vm_size_t nbytes;
118 1.1 mycroft unsigned chan;
119 1.1 mycroft {
120 1.1 mycroft vm_offset_t phys;
121 1.1 mycroft int waport;
122 1.1 mycroft caddr_t newaddr;
123 1.1 mycroft
124 1.1 mycroft if (chan > 7 ||
125 1.1 mycroft (chan < 4 && nbytes > (1<<16)) ||
126 1.1 mycroft (chan >= 4 && (nbytes > (1<<17) || (u_int)addr & 1)))
127 1.1 mycroft panic("at_dma: impossible request");
128 1.1 mycroft
129 1.1 mycroft if (at_dma_rangecheck((vm_offset_t)addr, nbytes, chan)) {
130 1.1 mycroft /* XXX totally braindead; NBPG is not enough */
131 1.1 mycroft if (bouncebuf[chan] == 0)
132 1.1 mycroft bouncebuf[chan] =
133 1.1 mycroft (caddr_t) isaphysmem + NBPG*chan;
134 1.1 mycroft bouncesize[chan] = nbytes;
135 1.1 mycroft newaddr = bouncebuf[chan];
136 1.1 mycroft /* copy bounce buffer on write */
137 1.1 mycroft if (!read) {
138 1.1 mycroft bcopy(addr, newaddr, nbytes);
139 1.1 mycroft bounced[chan] = 0;
140 1.1 mycroft } else
141 1.1 mycroft bounced[chan] = addr;
142 1.1 mycroft addr = newaddr;
143 1.1 mycroft }
144 1.1 mycroft
145 1.1 mycroft /* translate to physical */
146 1.1 mycroft phys = pmap_extract(pmap_kernel(), (vm_offset_t)addr);
147 1.1 mycroft
148 1.1 mycroft if ((chan & 4) == 0) {
149 1.1 mycroft /*
150 1.1 mycroft * Program one of DMA channels 0..3. These are
151 1.1 mycroft * byte mode channels.
152 1.1 mycroft */
153 1.1 mycroft /* set dma channel mode, and reset address ff */
154 1.1 mycroft if (read)
155 1.1 mycroft outb(DMA1_MODE, DMA37MD_SINGLE|DMA37MD_WRITE|chan);
156 1.1 mycroft else
157 1.1 mycroft outb(DMA1_MODE, DMA37MD_SINGLE|DMA37MD_READ|chan);
158 1.1 mycroft outb(DMA1_FFC, 0);
159 1.1 mycroft
160 1.1 mycroft /* send start address */
161 1.1 mycroft waport = DMA1_CHN(chan);
162 1.1 mycroft outb(waport, phys);
163 1.1 mycroft outb(waport, phys>>8);
164 1.1 mycroft outb(dmapageport[chan], phys>>16);
165 1.1 mycroft
166 1.1 mycroft /* send count */
167 1.1 mycroft outb(waport + 1, --nbytes);
168 1.1 mycroft outb(waport + 1, nbytes>>8);
169 1.1 mycroft
170 1.1 mycroft /* unmask channel */
171 1.1 mycroft outb(DMA1_SMSK, DMA37SM_CLEAR | chan);
172 1.1 mycroft } else {
173 1.1 mycroft /*
174 1.1 mycroft * Program one of DMA channels 4..7. These are
175 1.1 mycroft * word mode channels.
176 1.1 mycroft */
177 1.1 mycroft /* set dma channel mode, and reset address ff */
178 1.1 mycroft if (read)
179 1.1 mycroft outb(DMA2_MODE, DMA37MD_SINGLE|DMA37MD_WRITE|(chan&3));
180 1.1 mycroft else
181 1.1 mycroft outb(DMA2_MODE, DMA37MD_SINGLE|DMA37MD_READ|(chan&3));
182 1.1 mycroft outb(DMA2_FFC, 0);
183 1.1 mycroft
184 1.1 mycroft /* send start address */
185 1.1 mycroft waport = DMA2_CHN(chan & 3);
186 1.1 mycroft outb(waport, phys>>1);
187 1.1 mycroft outb(waport, phys>>9);
188 1.1 mycroft outb(dmapageport[chan], phys>>16);
189 1.1 mycroft
190 1.1 mycroft /* send count */
191 1.1 mycroft nbytes >>= 1;
192 1.1 mycroft outb(waport + 2, --nbytes);
193 1.1 mycroft outb(waport + 2, nbytes>>8);
194 1.1 mycroft
195 1.1 mycroft /* unmask channel */
196 1.1 mycroft outb(DMA2_SMSK, DMA37SM_CLEAR | (chan & 3));
197 1.1 mycroft }
198 1.1 mycroft }
199 1.1 mycroft
200 1.1 mycroft /*
201 1.1 mycroft * Abort a DMA request, clearing the bounce buffer, if any.
202 1.1 mycroft */
203 1.1 mycroft void
204 1.1 mycroft at_dma_abort(chan)
205 1.1 mycroft unsigned chan;
206 1.1 mycroft {
207 1.1 mycroft
208 1.1 mycroft #ifdef DIAGNOSTIC
209 1.1 mycroft if (chan > 7)
210 1.1 mycroft panic("at_dma_abort: impossible request");
211 1.1 mycroft #endif
212 1.1 mycroft
213 1.1 mycroft bounced[chan] = 0;
214 1.1 mycroft
215 1.1 mycroft /* mask channel */
216 1.1 mycroft if ((chan & 4) == 0)
217 1.1 mycroft outb(DMA1_SMSK, DMA37SM_SET | chan);
218 1.1 mycroft else
219 1.1 mycroft outb(DMA2_SMSK, DMA37SM_SET | (chan & 3));
220 1.1 mycroft }
221 1.1 mycroft
222 1.1 mycroft /*
223 1.1 mycroft * End a DMA request, copying data from the bounce buffer, if any,
224 1.1 mycroft * when reading.
225 1.1 mycroft */
226 1.1 mycroft void
227 1.1 mycroft at_dma_terminate(chan)
228 1.1 mycroft unsigned chan;
229 1.1 mycroft {
230 1.1 mycroft u_char tc;
231 1.1 mycroft
232 1.1 mycroft #ifdef DIAGNOSTIC
233 1.1 mycroft if (chan > 7)
234 1.1 mycroft panic("at_dma_terminate: impossible request");
235 1.1 mycroft #endif
236 1.1 mycroft
237 1.1 mycroft /* check that the terminal count was reached */
238 1.1 mycroft if ((chan & 4) == 0)
239 1.1 mycroft tc = inb(DMA1_SR) & (1 << chan);
240 1.1 mycroft else
241 1.1 mycroft tc = inb(DMA2_SR) & (1 << (chan & 3));
242 1.1 mycroft if (tc == 0)
243 1.1 mycroft /* XXX probably should panic or something */
244 1.1 mycroft log(LOG_ERR, "dma channel %d not finished\n", chan);
245 1.1 mycroft
246 1.1 mycroft /* copy bounce buffer on read */
247 1.1 mycroft if (bounced[chan]) {
248 1.1 mycroft bcopy(bouncebuf[chan], bounced[chan], bouncesize[chan]);
249 1.1 mycroft bounced[chan] = 0;
250 1.1 mycroft }
251 1.1 mycroft
252 1.1 mycroft /* mask channel */
253 1.1 mycroft if ((chan & 4) == 0)
254 1.1 mycroft outb(DMA1_SMSK, DMA37SM_SET | chan);
255 1.1 mycroft else
256 1.1 mycroft outb(DMA2_SMSK, DMA37SM_SET | (chan & 3));
257 1.1 mycroft }
258 1.1 mycroft
259 1.1 mycroft /*
260 1.1 mycroft * Check for problems with the address range of a DMA transfer
261 1.1 mycroft * (non-contiguous physical pages, outside of bus address space,
262 1.1 mycroft * crossing DMA page boundaries).
263 1.1 mycroft * Return true if special handling needed.
264 1.1 mycroft */
265 1.1 mycroft int
266 1.1 mycroft at_dma_rangecheck(va, length, chan)
267 1.1 mycroft vm_offset_t va;
268 1.1 mycroft unsigned length;
269 1.1 mycroft unsigned chan;
270 1.1 mycroft {
271 1.1 mycroft vm_offset_t phys, priorpage = 0, endva;
272 1.1 mycroft u_int dma_pgmsk = (chan&4) ? ~(128*1024-1) : ~(64*1024-1);
273 1.1 mycroft
274 1.1 mycroft endva = round_page(va + length);
275 1.1 mycroft for (; va < endva ; va += NBPG) {
276 1.1 mycroft phys = trunc_page(pmap_extract(pmap_kernel(), va));
277 1.1 mycroft if (phys == 0)
278 1.1 mycroft panic("isa_dmacheck: no physical page present");
279 1.1 mycroft if (phys >= (1<<24))
280 1.1 mycroft return 1;
281 1.1 mycroft if (priorpage) {
282 1.1 mycroft if (priorpage + NBPG != phys)
283 1.1 mycroft return 1;
284 1.1 mycroft /* check if crossing a DMA page boundary */
285 1.1 mycroft if ((priorpage ^ phys) & dma_pgmsk)
286 1.1 mycroft return 1;
287 1.1 mycroft }
288 1.1 mycroft priorpage = phys;
289 1.1 mycroft }
290 1.1 mycroft return 0;
291 1.1 mycroft }
292