isa.c revision 1.28.2.4 1 1.1 cgd /*-
2 1.1 cgd * Copyright (c) 1991 The Regents of the University of California.
3 1.1 cgd * All rights reserved.
4 1.1 cgd *
5 1.1 cgd * This code is derived from software contributed to Berkeley by
6 1.1 cgd * William Jolitz.
7 1.1 cgd *
8 1.1 cgd * Redistribution and use in source and binary forms, with or without
9 1.1 cgd * modification, are permitted provided that the following conditions
10 1.1 cgd * are met:
11 1.1 cgd * 1. Redistributions of source code must retain the above copyright
12 1.1 cgd * notice, this list of conditions and the following disclaimer.
13 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 cgd * notice, this list of conditions and the following disclaimer in the
15 1.1 cgd * documentation and/or other materials provided with the distribution.
16 1.1 cgd * 3. All advertising materials mentioning features or use of this software
17 1.1 cgd * must display the following acknowledgement:
18 1.1 cgd * This product includes software developed by the University of
19 1.1 cgd * California, Berkeley and its contributors.
20 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
21 1.1 cgd * may be used to endorse or promote products derived from this software
22 1.1 cgd * without specific prior written permission.
23 1.1 cgd *
24 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 1.1 cgd * SUCH DAMAGE.
35 1.1 cgd *
36 1.13 cgd * from: @(#)isa.c 7.2 (Berkeley) 5/13/91
37 1.28.2.4 mycroft * $Id: isa.c,v 1.28.2.4 1993/10/06 12:09:12 mycroft Exp $
38 1.1 cgd */
39 1.1 cgd
40 1.1 cgd /*
41 1.1 cgd * code to manage AT bus
42 1.2 cgd *
43 1.2 cgd * 92/08/18 Frank P. MacLachlan (fpm (at) crash.cts.com):
44 1.2 cgd * Fixed uninitialized variable problem and added code to deal
45 1.2 cgd * with DMA page boundaries in isa_dmarangecheck(). Fixed word
46 1.2 cgd * mode DMA count compution and reorganized DMA setup code in
47 1.2 cgd * isa_dmastart()
48 1.1 cgd */
49 1.1 cgd
50 1.1 cgd #include "param.h"
51 1.1 cgd #include "systm.h"
52 1.1 cgd #include "conf.h"
53 1.1 cgd #include "file.h"
54 1.1 cgd #include "buf.h"
55 1.1 cgd #include "uio.h"
56 1.1 cgd #include "syslog.h"
57 1.1 cgd #include "malloc.h"
58 1.28.2.2 mycroft #include "machine/cpu.h"
59 1.21 andrew #include "machine/cpufunc.h"
60 1.28.2.1 mycroft #include "sys/device.h"
61 1.1 cgd #include "vm/vm.h"
62 1.1 cgd #include "i386/isa/isa.h"
63 1.28.2.1 mycroft #include "i386/isa/isavar.h"
64 1.1 cgd #include "i386/isa/icu.h"
65 1.1 cgd #include "i386/isa/ic/i8237.h"
66 1.1 cgd #include "i386/isa/ic/i8042.h"
67 1.16 mycroft #include "i386/isa/timerreg.h"
68 1.28 brezak #include "i386/isa/spkr_reg.h"
69 1.14 deraadt
70 1.2 cgd /*
71 1.2 cgd ** Register definitions for DMA controller 1 (channels 0..3):
72 1.2 cgd */
73 1.2 cgd #define DMA1_CHN(c) (IO_DMA1 + 1*(2*(c))) /* addr reg for channel c */
74 1.2 cgd #define DMA1_SMSK (IO_DMA1 + 1*10) /* single mask register */
75 1.2 cgd #define DMA1_MODE (IO_DMA1 + 1*11) /* mode register */
76 1.2 cgd #define DMA1_FFC (IO_DMA1 + 1*12) /* clear first/last FF */
77 1.2 cgd
78 1.2 cgd /*
79 1.2 cgd ** Register definitions for DMA controller 2 (channels 4..7):
80 1.2 cgd */
81 1.2 cgd #define DMA2_CHN(c) (IO_DMA1 + 2*(2*(c))) /* addr reg for channel c */
82 1.2 cgd #define DMA2_SMSK (IO_DMA2 + 2*10) /* single mask register */
83 1.2 cgd #define DMA2_MODE (IO_DMA2 + 2*11) /* mode register */
84 1.2 cgd #define DMA2_FFC (IO_DMA2 + 2*12) /* clear first/last FF */
85 1.2 cgd
86 1.28.2.1 mycroft isa_type isa_bustype; /* type of bus */
87 1.1 cgd
88 1.28.2.1 mycroft static int isaprobe __P((struct device *, struct cfdata *, void *));
89 1.28.2.2 mycroft static void isaattach __P((struct device *, struct device *, void *));
90 1.28.2.2 mycroft static int isasubmatch __P((struct device *, struct cfdata *, void *));
91 1.28.2.1 mycroft
92 1.28.2.1 mycroft struct cfdriver isacd =
93 1.28.2.2 mycroft { NULL, "isa", isaprobe, isaattach, DV_DULL, sizeof(struct isa_softc) };
94 1.28.2.1 mycroft
95 1.28.2.2 mycroft void isa_defaultirq __P((void));
96 1.28.2.2 mycroft static int isaprint __P((void *, char *));
97 1.28.2.2 mycroft
98 1.28.2.2 mycroft /*
99 1.28.2.2 mycroft * We think there might be an ISA bus here. Check it out.
100 1.28.2.2 mycroft */
101 1.28.2.2 mycroft static int
102 1.28.2.1 mycroft isaprobe(parent, cf, aux)
103 1.28.2.1 mycroft struct device *parent;
104 1.28.2.1 mycroft struct cfdata *cf;
105 1.28.2.1 mycroft void *aux;
106 1.28.2.1 mycroft {
107 1.28.2.1 mycroft
108 1.28.2.1 mycroft /* XXX should do a real probe */
109 1.28.2.1 mycroft isa_bustype = BUS_ISA;
110 1.28.2.1 mycroft return 1;
111 1.28.2.1 mycroft }
112 1.28.2.1 mycroft
113 1.28.2.2 mycroft /*
114 1.28.2.2 mycroft * Probe succeeded, someone called config_attach. Now we have to
115 1.28.2.2 mycroft * probe the ISA bus itself in our turn.
116 1.28.2.2 mycroft */
117 1.28.2.2 mycroft static void
118 1.28.2.2 mycroft isaattach(parent, self, aux)
119 1.28.2.2 mycroft struct device *parent, *self;
120 1.28.2.2 mycroft void *aux;
121 1.28.2.2 mycroft {
122 1.28.2.2 mycroft
123 1.28.2.2 mycroft /* should print ISA/EISA & bus clock frequency and anything else
124 1.28.2.2 mycroft we can figure out? */
125 1.28.2.2 mycroft printf("\n");
126 1.28.2.2 mycroft
127 1.28.2.2 mycroft isa_defaultirq();
128 1.28.2.2 mycroft
129 1.28.2.2 mycroft enable_intr();
130 1.28.2.2 mycroft splhigh();
131 1.28.2.2 mycroft intr_enable(IRQ_SLAVE);
132 1.28.2.2 mycroft
133 1.28.2.2 mycroft /* Iterate ``isasubmatch'' over all devices configured here. */
134 1.28.2.2 mycroft (void)config_search(isasubmatch, self, (void *)NULL);
135 1.28.2.2 mycroft
136 1.28.2.2 mycroft /* and the problem is... if netmask == 0, then the loopback
137 1.28.2.2 mycroft * code can do some really ugly things.
138 1.28.2.2 mycroft * workaround for this: if netmask == 0, set it to 0x8000, which
139 1.28.2.2 mycroft * is the value used by splsoftclock. this is nasty, but it
140 1.28.2.2 mycroft * should work until this interrupt system goes away. -- cgd
141 1.28.2.2 mycroft */
142 1.28.2.2 mycroft if (netmask == 0)
143 1.28.2.2 mycroft netmask = 0x8000; /* same as for softclock. XXXX */
144 1.28.2.2 mycroft
145 1.28.2.2 mycroft printf("biomask %x ttymask %x netmask %x impmask %x\n",
146 1.28.2.2 mycroft biomask, ttymask, netmask, impmask);
147 1.28.2.2 mycroft splnone();
148 1.28.2.2 mycroft }
149 1.28.2.2 mycroft
150 1.28.2.2 mycroft /*
151 1.28.2.2 mycroft * isaattach (above) iterates this function over all the sub-devices that
152 1.28.2.2 mycroft * were configured at the ``isa'' device. Our job is to provide defaults
153 1.28.2.2 mycroft * and do some internal/external representation conversion (some of which
154 1.28.2.2 mycroft * is scheduled to get cleaned up later), then call the device's probe
155 1.28.2.2 mycroft * function. If this says the device is there, we try to reserve ISA
156 1.28.2.2 mycroft * bus memory and ports for the device, and attach it.
157 1.28.2.2 mycroft *
158 1.28.2.2 mycroft * Note that we always return 0, but our ultimate caller (isaattach)
159 1.28.2.2 mycroft * does not care that we never `match' anything. (In fact, our return
160 1.28.2.2 mycroft * value is entirely irrelevant; this function is run entirely for its
161 1.28.2.2 mycroft * side effects.)
162 1.28.2.2 mycroft */
163 1.28.2.1 mycroft static int
164 1.28.2.2 mycroft isasubmatch(isa, cf, aux)
165 1.28.2.2 mycroft struct device *isa;
166 1.28.2.1 mycroft struct cfdata *cf;
167 1.28.2.1 mycroft void *aux;
168 1.28.2.1 mycroft {
169 1.28.2.2 mycroft struct isa_attach_args ia;
170 1.28.2.1 mycroft
171 1.28.2.1 mycroft #ifdef DIAGNOSTIC
172 1.28.2.2 mycroft if (cf->cf_driver->cd_match == NULL) {
173 1.28.2.2 mycroft /* we really ought to add printf formats to panic(). */
174 1.28.2.1 mycroft printf("isasubmatch: no match function for `%s' device\n",
175 1.28.2.1 mycroft cf->cf_driver->cd_name);
176 1.28.2.1 mycroft panic("isasubmatch: no match function\n");
177 1.28.2.1 mycroft }
178 1.28.2.1 mycroft #endif
179 1.28.2.1 mycroft
180 1.28.2.2 mycroft /* Init the info needed in the device probe routine. */
181 1.28.2.2 mycroft ia.ia_iobase = cf->cf_iobase;
182 1.28.2.2 mycroft ia.ia_iosize = cf->cf_iosize;
183 1.28.2.2 mycroft if (cf->cf_irq == -1)
184 1.28.2.2 mycroft ia.ia_irq = IRQUNK;
185 1.28.2.2 mycroft else
186 1.28.2.2 mycroft ia.ia_irq = 1 << cf->cf_irq;
187 1.28.2.2 mycroft ia.ia_drq = cf->cf_drq;
188 1.28.2.2 mycroft ia.ia_maddr = (caddr_t)cf->cf_maddr;
189 1.28.2.2 mycroft ia.ia_msize = cf->cf_msize;
190 1.28.2.1 mycroft
191 1.28.2.2 mycroft /* If driver says it's not there, believe it. */
192 1.28.2.2 mycroft if (!cf->cf_driver->cd_match(isa, cf, &ia))
193 1.28.2.1 mycroft return 0;
194 1.28.2.1 mycroft
195 1.28.2.2 mycroft /* Driver says it is there. Try to reserve ports and memory. */
196 1.28.2.2 mycroft if (isa_reserveports(ia.ia_iobase, ia.ia_iosize)) {
197 1.28.2.2 mycroft if (isa_reservemem(ia.ia_maddr, ia.ia_msize))
198 1.28.2.2 mycroft config_attach(isa, cf, &ia, isaprint); /* victory! */
199 1.28.2.2 mycroft else
200 1.28.2.2 mycroft isa_unreserveports(ia.ia_iobase, ia.ia_iosize);
201 1.28.2.1 mycroft }
202 1.28.2.1 mycroft
203 1.28.2.2 mycroft /* In any case, move on to next config entry. */
204 1.28.2.2 mycroft return 0;
205 1.28.2.1 mycroft }
206 1.28.2.1 mycroft
207 1.28.2.2 mycroft /*
208 1.28.2.2 mycroft * Called indirectly via config_attach (see above). Config has already
209 1.28.2.2 mycroft * printed, e.g., "wdc0 at isa0". We can ignore our ``isaname'' arg
210 1.28.2.2 mycroft * (it is always NULL, by definition) and just extend the line with
211 1.28.2.2 mycroft * the standard info (port(s), irq, drq, and iomem).
212 1.28.2.2 mycroft */
213 1.28.2.2 mycroft static int
214 1.28.2.1 mycroft isaprint(aux, isaname)
215 1.28.2.1 mycroft void *aux;
216 1.28.2.1 mycroft char *isaname;
217 1.28.2.1 mycroft {
218 1.28.2.1 mycroft struct isa_attach_args *ia = aux;
219 1.28.2.1 mycroft
220 1.28.2.1 mycroft if (ia->ia_iosize)
221 1.28.2.1 mycroft printf(" port 0x%x", ia->ia_iobase);
222 1.28.2.1 mycroft if (ia->ia_iosize > 1)
223 1.28.2.1 mycroft printf("-0x%x", ia->ia_iobase + ia->ia_iosize - 1);
224 1.28.2.3 mycroft #ifdef DIAGNOSTIC
225 1.28.2.3 mycroft if (ia->ia_irq == IRQUNK)
226 1.28.2.4 mycroft printf(" THIS IS A BUG ->");
227 1.28.2.3 mycroft #endif
228 1.28.2.3 mycroft if (ia->ia_irq != IRQNONE)
229 1.28.2.1 mycroft printf(" irq %d", ffs(ia->ia_irq) - 1);
230 1.28.2.1 mycroft if (ia->ia_drq != DRQUNK)
231 1.28.2.1 mycroft printf(" drq %d", ia->ia_drq);
232 1.28.2.1 mycroft if (ia->ia_msize)
233 1.28.2.1 mycroft printf(" iomem 0x%x", ia->ia_maddr);
234 1.28.2.1 mycroft if (ia->ia_msize > 1)
235 1.28.2.1 mycroft printf("-0x%x", ia->ia_maddr + ia->ia_msize - 1);
236 1.28.2.1 mycroft /* XXXX need to print flags */
237 1.28.2.2 mycroft return QUIET; /* actually, our return value is irrelevant. */
238 1.1 cgd }
239 1.1 cgd
240 1.1 cgd
241 1.1 cgd #define IDTVEC(name) __CONCAT(X,name)
242 1.1 cgd /* default interrupt vector table entries */
243 1.1 cgd extern IDTVEC(intr0), IDTVEC(intr1), IDTVEC(intr2), IDTVEC(intr3),
244 1.1 cgd IDTVEC(intr4), IDTVEC(intr5), IDTVEC(intr6), IDTVEC(intr7),
245 1.1 cgd IDTVEC(intr8), IDTVEC(intr9), IDTVEC(intr10), IDTVEC(intr11),
246 1.1 cgd IDTVEC(intr12), IDTVEC(intr13), IDTVEC(intr14), IDTVEC(intr15);
247 1.1 cgd
248 1.1 cgd static *defvec[16] = {
249 1.1 cgd &IDTVEC(intr0), &IDTVEC(intr1), &IDTVEC(intr2), &IDTVEC(intr3),
250 1.1 cgd &IDTVEC(intr4), &IDTVEC(intr5), &IDTVEC(intr6), &IDTVEC(intr7),
251 1.1 cgd &IDTVEC(intr8), &IDTVEC(intr9), &IDTVEC(intr10), &IDTVEC(intr11),
252 1.1 cgd &IDTVEC(intr12), &IDTVEC(intr13), &IDTVEC(intr14), &IDTVEC(intr15) };
253 1.1 cgd
254 1.1 cgd /* out of range default interrupt vector gate entry */
255 1.1 cgd extern IDTVEC(intrdefault);
256 1.15 cgd
257 1.1 cgd /*
258 1.28.2.1 mycroft * Fill in default interrupt table, and mask all interrupts.
259 1.1 cgd */
260 1.21 andrew void
261 1.28.2.1 mycroft isa_defaultirq()
262 1.28.2.1 mycroft {
263 1.1 cgd int i;
264 1.1 cgd
265 1.1 cgd /* icu vectors */
266 1.28.2.1 mycroft for (i = ICU_OFFSET; i < ICU_OFFSET + ICU_LEN ; i++)
267 1.1 cgd setidt(i, defvec[i], SDT_SYS386IGT, SEL_KPL);
268 1.15 cgd
269 1.1 cgd /* out of range vectors */
270 1.28.2.1 mycroft for (; i < NIDT; i++)
271 1.1 cgd setidt(i, &IDTVEC(intrdefault), SDT_SYS386IGT, SEL_KPL);
272 1.1 cgd
273 1.1 cgd /* initialize 8259's */
274 1.1 cgd outb(IO_ICU1, 0x11); /* reset; program device, four bytes */
275 1.28.2.1 mycroft outb(IO_ICU1+1, ICU_OFFSET); /* starting at this vector index */
276 1.28.2.1 mycroft outb(IO_ICU1+1, IRQ_SLAVE);
277 1.15 cgd #ifdef AUTO_EOI_1
278 1.15 cgd outb(IO_ICU1+1, 2 | 1); /* auto EOI, 8086 mode */
279 1.15 cgd #else
280 1.1 cgd outb(IO_ICU1+1, 1); /* 8086 mode */
281 1.15 cgd #endif
282 1.1 cgd outb(IO_ICU1+1, 0xff); /* leave interrupts masked */
283 1.15 cgd outb(IO_ICU1, 0x0a); /* default to IRR on read */
284 1.21 andrew #ifdef REORDER_IRQ
285 1.15 cgd outb(IO_ICU1, 0xc0 | (3 - 1)); /* pri order 3-7, 0-2 (com2 first) */
286 1.21 andrew #endif
287 1.1 cgd
288 1.1 cgd outb(IO_ICU2, 0x11); /* reset; program device, four bytes */
289 1.28.2.1 mycroft outb(IO_ICU2+1, ICU_OFFSET+8); /* staring at this vector index */
290 1.28.2.1 mycroft outb(IO_ICU2+1, ffs(IRQ_SLAVE)-1);
291 1.15 cgd #ifdef AUTO_EOI_2
292 1.15 cgd outb(IO_ICU2+1, 2 | 1); /* auto EOI, 8086 mode */
293 1.15 cgd #else
294 1.28.2.1 mycroft outb(IO_ICU2+1, 1); /* 8086 mode */
295 1.15 cgd #endif
296 1.1 cgd outb(IO_ICU2+1, 0xff); /* leave interrupts masked */
297 1.15 cgd outb(IO_ICU2, 0x0a); /* default to IRR on read */
298 1.1 cgd }
299 1.1 cgd
300 1.28.2.1 mycroft
301 1.28.2.1 mycroft /* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX */
302 1.28.2.1 mycroft
303 1.28.2.1 mycroft int
304 1.28.2.1 mycroft config_isadev(isdp, mp)
305 1.28.2.1 mycroft struct isa_device *isdp;
306 1.28.2.1 mycroft u_int *mp;
307 1.28.2.1 mycroft {
308 1.28.2.1 mycroft if (isdp->id_irq) {
309 1.28.2.1 mycroft int intrno;
310 1.28.2.1 mycroft
311 1.28.2.1 mycroft intrno = ffs(isdp->id_irq)-1;
312 1.28.2.1 mycroft setidt(ICU_OFFSET+intrno, isdp->id_intr,
313 1.28.2.1 mycroft SDT_SYS386IGT, SEL_KPL);
314 1.28.2.1 mycroft if(mp)
315 1.28.2.1 mycroft INTRMASK(*mp,isdp->id_irq);
316 1.28.2.1 mycroft INTREN(isdp->id_irq);
317 1.28.2.1 mycroft }
318 1.28.2.1 mycroft }
319 1.28.2.1 mycroft
320 1.28.2.1 mycroft
321 1.1 cgd /* region of physical memory known to be contiguous */
322 1.1 cgd vm_offset_t isaphysmem;
323 1.28.2.1 mycroft static caddr_t bouncebuf[8]; /* XXX */
324 1.28.2.1 mycroft static caddr_t bounced[8]; /* XXX */
325 1.28.2.1 mycroft static vm_size_t bouncesize[8]; /* XXX */
326 1.28.2.1 mycroft #define MAXDMASZ 512 /* XXX */
327 1.1 cgd
328 1.1 cgd /* high byte of address is stored in this port for i-th dma channel */
329 1.1 cgd static short dmapageport[8] =
330 1.1 cgd { 0x87, 0x83, 0x81, 0x82, 0x8f, 0x8b, 0x89, 0x8a };
331 1.1 cgd
332 1.1 cgd /*
333 1.1 cgd * isa_dmacascade(): program 8237 DMA controller channel to accept
334 1.1 cgd * external dma control by a board.
335 1.1 cgd */
336 1.21 andrew void
337 1.28.2.1 mycroft at_dma_cascade(chan)
338 1.28.2.1 mycroft unsigned chan;
339 1.2 cgd {
340 1.28.2.1 mycroft #ifdef DEBUG
341 1.1 cgd if (chan > 7)
342 1.28.2.1 mycroft panic("at_dma_cascade: impossible request");
343 1.28.2.1 mycroft #endif
344 1.1 cgd
345 1.1 cgd /* set dma channel mode, and set dma channel mode */
346 1.2 cgd if ((chan & 4) == 0) {
347 1.2 cgd outb(DMA1_MODE, DMA37MD_CASCADE | chan);
348 1.2 cgd outb(DMA1_SMSK, chan);
349 1.2 cgd } else {
350 1.2 cgd outb(DMA2_MODE, DMA37MD_CASCADE | (chan & 3));
351 1.2 cgd outb(DMA2_SMSK, chan & 3);
352 1.2 cgd }
353 1.1 cgd }
354 1.1 cgd
355 1.1 cgd /*
356 1.28.2.1 mycroft * at_dma(): program 8237 DMA controller channel, avoid page alignment
357 1.1 cgd * problems by using a bounce buffer.
358 1.1 cgd */
359 1.21 andrew void
360 1.28.2.1 mycroft at_dma(flags, addr, nbytes, chan)
361 1.28.2.1 mycroft int read;
362 1.28.2.1 mycroft caddr_t addr;
363 1.28.2.1 mycroft vm_size_t nbytes;
364 1.28.2.1 mycroft unsigned chan;
365 1.28.2.1 mycroft {
366 1.28.2.1 mycroft vm_offset_t phys;
367 1.2 cgd int waport;
368 1.1 cgd caddr_t newaddr;
369 1.1 cgd
370 1.28.2.1 mycroft ppp if ( chan > 7
371 1.2 cgd || (chan < 4 && nbytes > (1<<16))
372 1.2 cgd || (chan >= 4 && (nbytes > (1<<17) || (u_int)addr & 1)))
373 1.1 cgd panic("isa_dmastart: impossible request");
374 1.1 cgd
375 1.28.2.1 mycroft if (at_dma_rangecheck(addr, nbytes, chan)) {
376 1.28.2.1 mycroft panic("bounce buffers don't work yet\n");
377 1.28.2.1 mycroft /* XXX totally braindead; NBPG is not enough */
378 1.28.2.1 mycroft if (bouncebuf[chan] == 0)
379 1.28.2.1 mycroft bouncebuf[chan] =
380 1.1 cgd /*(caddr_t)malloc(MAXDMASZ, M_TEMP, M_WAITOK);*/
381 1.1 cgd (caddr_t) isaphysmem + NBPG*chan;
382 1.28.2.1 mycroft bouncesize[chan] = nbytes;
383 1.28.2.1 mycroft newaddr = bouncebuf[chan];
384 1.1 cgd /* copy bounce buffer on write */
385 1.28.2.1 mycroft if (!read)
386 1.1 cgd bcopy(addr, newaddr, nbytes);
387 1.28.2.1 mycroft else
388 1.28.2.1 mycroft bounced[chan] = addr;
389 1.1 cgd addr = newaddr;
390 1.1 cgd }
391 1.1 cgd
392 1.1 cgd /* translate to physical */
393 1.1 cgd phys = pmap_extract(pmap_kernel(), (vm_offset_t)addr);
394 1.1 cgd
395 1.2 cgd if ((chan & 4) == 0) {
396 1.2 cgd /*
397 1.2 cgd * Program one of DMA channels 0..3. These are
398 1.2 cgd * byte mode channels.
399 1.2 cgd */
400 1.2 cgd /* set dma channel mode, and reset address ff */
401 1.28.2.1 mycroft if (read)
402 1.2 cgd outb(DMA1_MODE, DMA37MD_SINGLE|DMA37MD_WRITE|chan);
403 1.2 cgd else
404 1.2 cgd outb(DMA1_MODE, DMA37MD_SINGLE|DMA37MD_READ|chan);
405 1.2 cgd outb(DMA1_FFC, 0);
406 1.1 cgd
407 1.2 cgd /* send start address */
408 1.2 cgd waport = DMA1_CHN(chan);
409 1.1 cgd outb(waport, phys);
410 1.1 cgd outb(waport, phys>>8);
411 1.2 cgd outb(dmapageport[chan], phys>>16);
412 1.2 cgd
413 1.2 cgd /* send count */
414 1.2 cgd outb(waport + 1, --nbytes);
415 1.2 cgd outb(waport + 1, nbytes>>8);
416 1.2 cgd
417 1.2 cgd /* unmask channel */
418 1.2 cgd outb(DMA1_SMSK, chan);
419 1.1 cgd } else {
420 1.2 cgd /*
421 1.2 cgd * Program one of DMA channels 4..7. These are
422 1.2 cgd * word mode channels.
423 1.2 cgd */
424 1.2 cgd /* set dma channel mode, and reset address ff */
425 1.28.2.1 mycroft if (read)
426 1.2 cgd outb(DMA2_MODE, DMA37MD_SINGLE|DMA37MD_WRITE|(chan&3));
427 1.2 cgd else
428 1.2 cgd outb(DMA2_MODE, DMA37MD_SINGLE|DMA37MD_READ|(chan&3));
429 1.2 cgd outb(DMA2_FFC, 0);
430 1.2 cgd
431 1.2 cgd /* send start address */
432 1.2 cgd waport = DMA2_CHN(chan - 4);
433 1.1 cgd outb(waport, phys>>1);
434 1.1 cgd outb(waport, phys>>9);
435 1.2 cgd outb(dmapageport[chan], phys>>16);
436 1.1 cgd
437 1.2 cgd /* send count */
438 1.2 cgd nbytes >>= 1;
439 1.1 cgd outb(waport + 2, --nbytes);
440 1.1 cgd outb(waport + 2, nbytes>>8);
441 1.2 cgd
442 1.2 cgd /* unmask channel */
443 1.2 cgd outb(DMA2_SMSK, chan & 3);
444 1.1 cgd }
445 1.1 cgd }
446 1.1 cgd
447 1.21 andrew void
448 1.28.2.1 mycroft at_dma_terminate(int flags, caddr_t addr, int nbytes, int chan)
449 1.1 cgd {
450 1.1 cgd
451 1.1 cgd if (bounced[chan]) {
452 1.28.2.1 mycroft bcopy(bouncebuf[chan], bounced[chan], bouncesize[chan]);
453 1.1 cgd bounced[chan] = 0;
454 1.1 cgd }
455 1.1 cgd }
456 1.1 cgd
457 1.1 cgd /*
458 1.1 cgd * Check for problems with the address range of a DMA transfer
459 1.2 cgd * (non-contiguous physical pages, outside of bus address space,
460 1.2 cgd * crossing DMA page boundaries).
461 1.1 cgd * Return true if special handling needed.
462 1.1 cgd */
463 1.1 cgd
464 1.21 andrew int
465 1.28.2.1 mycroft at_dma_rangecheck(caddr_t va, unsigned length, unsigned chan) {
466 1.2 cgd vm_offset_t phys, priorpage = 0, endva;
467 1.2 cgd u_int dma_pgmsk = (chan & 4) ? ~(128*1024-1) : ~(64*1024-1);
468 1.1 cgd
469 1.1 cgd endva = (vm_offset_t)round_page(va + length);
470 1.1 cgd for (; va < (caddr_t) endva ; va += NBPG) {
471 1.1 cgd phys = trunc_page(pmap_extract(pmap_kernel(), (vm_offset_t)va));
472 1.1 cgd if (phys == 0)
473 1.1 cgd panic("isa_dmacheck: no physical page present");
474 1.28.2.1 mycroft if (phys > physmem)
475 1.1 cgd return (1);
476 1.2 cgd if (priorpage) {
477 1.2 cgd if (priorpage + NBPG != phys)
478 1.2 cgd return (1);
479 1.2 cgd /* check if crossing a DMA page boundary */
480 1.2 cgd if (((u_int)priorpage ^ (u_int)phys) & dma_pgmsk)
481 1.2 cgd return (1);
482 1.2 cgd }
483 1.1 cgd priorpage = phys;
484 1.1 cgd }
485 1.1 cgd return (0);
486 1.1 cgd }
487 1.1 cgd
488 1.1 cgd /* head of queue waiting for physmem to become available */
489 1.1 cgd struct buf isa_physmemq;
490 1.1 cgd
491 1.1 cgd /* blocked waiting for resource to become free for exclusive use */
492 1.1 cgd static isaphysmemflag;
493 1.1 cgd /* if waited for and call requested when free (B_CALL) */
494 1.28.2.1 mycroft static void (*isaphysmemunblock)(); /* XXX needs to be a list */
495 1.1 cgd
496 1.1 cgd /*
497 1.1 cgd * Allocate contiguous physical memory for transfer, returning
498 1.1 cgd * a *virtual* address to region. May block waiting for resource.
499 1.1 cgd * (assumed to be called at splbio())
500 1.1 cgd */
501 1.1 cgd caddr_t
502 1.1 cgd isa_allocphysmem(caddr_t va, unsigned length, void (*func)()) {
503 1.1 cgd
504 1.1 cgd isaphysmemunblock = func;
505 1.1 cgd while (isaphysmemflag & B_BUSY) {
506 1.1 cgd isaphysmemflag |= B_WANTED;
507 1.24 deraadt sleep((caddr_t)&isaphysmemflag, PRIBIO);
508 1.1 cgd }
509 1.1 cgd isaphysmemflag |= B_BUSY;
510 1.1 cgd
511 1.1 cgd return((caddr_t)isaphysmem);
512 1.1 cgd }
513 1.1 cgd
514 1.1 cgd /*
515 1.1 cgd * Free contiguous physical memory used for transfer.
516 1.1 cgd * (assumed to be called at splbio())
517 1.1 cgd */
518 1.1 cgd void
519 1.1 cgd isa_freephysmem(caddr_t va, unsigned length) {
520 1.1 cgd
521 1.1 cgd isaphysmemflag &= ~B_BUSY;
522 1.1 cgd if (isaphysmemflag & B_WANTED) {
523 1.1 cgd isaphysmemflag &= B_WANTED;
524 1.24 deraadt wakeup((caddr_t)&isaphysmemflag);
525 1.1 cgd if (isaphysmemunblock)
526 1.1 cgd (*isaphysmemunblock)();
527 1.1 cgd }
528 1.1 cgd }
529 1.1 cgd
530 1.1 cgd /*
531 1.1 cgd * Handle a NMI, possibly a machine check.
532 1.1 cgd * return true to panic system, false to ignore.
533 1.1 cgd */
534 1.21 andrew int
535 1.1 cgd isa_nmi(cd) {
536 1.1 cgd
537 1.1 cgd log(LOG_CRIT, "\nNMI port 61 %x, port 70 %x\n", inb(0x61), inb(0x70));
538 1.1 cgd return(0);
539 1.1 cgd }
540 1.1 cgd
541 1.1 cgd /*
542 1.1 cgd * Caught a stray interrupt, notify
543 1.1 cgd */
544 1.21 andrew void
545 1.28.2.1 mycroft isa_strayintr(d)
546 1.28.2.1 mycroft int d;
547 1.28.2.1 mycroft {
548 1.1 cgd
549 1.4 cgd /*
550 1.28.2.1 mycroft * Stray level 7 interrupts occur when someone raises an interrupt
551 1.28.2.1 mycroft * and then drops it before the CPU acknowledges it. This means
552 1.28.2.1 mycroft * either the device is screwed or something is cli'ing too long.
553 1.4 cgd */
554 1.15 cgd extern u_long intrcnt_stray;
555 1.4 cgd
556 1.15 cgd intrcnt_stray++;
557 1.15 cgd if (intrcnt_stray <= 5)
558 1.28.2.1 mycroft log(LOG_ERR, "stray interrupt %d\n", d);
559 1.15 cgd if (intrcnt_stray == 5)
560 1.28.2.1 mycroft log(LOG_CRIT,"too many stray interrupts; stopped logging\n");
561 1.1 cgd }
562 1.1 cgd
563 1.1 cgd static beeping;
564 1.21 andrew static void
565 1.21 andrew sysbeepstop(int f)
566 1.1 cgd {
567 1.16 mycroft int s = splhigh();
568 1.16 mycroft
569 1.1 cgd /* disable counter 2 */
570 1.16 mycroft disable_intr();
571 1.28 brezak outb(PITAUX_PORT, inb(PITAUX_PORT) & ~PIT_SPKR);
572 1.16 mycroft enable_intr();
573 1.1 cgd if (f)
574 1.24 deraadt timeout((timeout_t)sysbeepstop, (caddr_t)0, f);
575 1.1 cgd else
576 1.1 cgd beeping = 0;
577 1.16 mycroft
578 1.16 mycroft splx(s);
579 1.1 cgd }
580 1.1 cgd
581 1.21 andrew void
582 1.21 andrew sysbeep(int pitch, int period)
583 1.1 cgd {
584 1.16 mycroft int s = splhigh();
585 1.16 mycroft static int last_pitch, last_period;
586 1.1 cgd
587 1.16 mycroft if (beeping) {
588 1.24 deraadt untimeout((timeout_t)sysbeepstop, (caddr_t)(last_period/2));
589 1.24 deraadt untimeout((timeout_t)sysbeepstop, (caddr_t)0);
590 1.1 cgd }
591 1.16 mycroft if (!beeping || last_pitch != pitch) {
592 1.16 mycroft /*
593 1.16 mycroft * XXX - move timer stuff to clock.c.
594 1.16 mycroft */
595 1.16 mycroft disable_intr();
596 1.16 mycroft outb(TIMER_MODE, TIMER_SEL2|TIMER_16BIT|TIMER_SQWAVE);
597 1.19 mycroft outb(TIMER_CNTR2, TIMER_DIV(pitch)%256);
598 1.19 mycroft outb(TIMER_CNTR2, TIMER_DIV(pitch)/256);
599 1.28 brezak outb(PITAUX_PORT, inb(PITAUX_PORT) | PIT_SPKR); /* enable counter 2 */
600 1.16 mycroft enable_intr();
601 1.16 mycroft }
602 1.16 mycroft last_pitch = pitch;
603 1.16 mycroft beeping = last_period = period;
604 1.24 deraadt timeout((timeout_t)sysbeepstop, (caddr_t)(period/2), period);
605 1.16 mycroft
606 1.16 mycroft splx(s);
607 1.1 cgd }
608 1.1 cgd
609 1.1 cgd /*
610 1.1 cgd * Pass command to keyboard controller (8042)
611 1.1 cgd */
612 1.21 andrew unsigned
613 1.21 andrew kbc_8042cmd(int val)
614 1.21 andrew {
615 1.1 cgd while (inb(KBSTATP)&KBS_IBF);
616 1.1 cgd if (val) outb(KBCMDP, val);
617 1.1 cgd while (inb(KBSTATP)&KBS_IBF);
618 1.1 cgd return (inb(KBDATAP));
619 1.28 brezak }
620 1.28 brezak
621 1.28 brezak /*
622 1.28 brezak * find an ISA device in a given isa_devtab_* table, given
623 1.28 brezak * the table to search, the expected id_driver entry, and the unit number.
624 1.28 brezak *
625 1.28 brezak * this function is defined in isa_device.h, and this location is debatable;
626 1.28 brezak * i put it there because it's useless w/o, and directly operates on
627 1.28 brezak * the other stuff in that file.
628 1.28 brezak *
629 1.28 brezak */
630 1.28 brezak
631 1.28 brezak struct isa_device *find_isadev(table, driverp, unit)
632 1.28 brezak struct isa_device *table;
633 1.28 brezak struct isa_driver *driverp;
634 1.28 brezak int unit;
635 1.28 brezak {
636 1.28 brezak if (driverp == NULL) /* sanity check */
637 1.28 brezak return NULL;
638 1.28 brezak
639 1.28 brezak while ((table->id_driver != driverp) || (table->id_unit != unit)) {
640 1.28 brezak if (table->id_driver == 0)
641 1.28 brezak return NULL;
642 1.28 brezak
643 1.28 brezak table++;
644 1.28 brezak }
645 1.28 brezak
646 1.28 brezak return table;
647 1.15 cgd }
648 1.15 cgd
649 1.15 cgd /*
650 1.15 cgd * Return nonzero if a (masked) irq is pending for a given device.
651 1.15 cgd */
652 1.15 cgd int
653 1.15 cgd isa_irq_pending(dvp)
654 1.15 cgd struct isa_device *dvp;
655 1.15 cgd {
656 1.15 cgd unsigned id_irq;
657 1.15 cgd
658 1.15 cgd id_irq = (unsigned short) dvp->id_irq; /* XXX silly type in struct */
659 1.15 cgd if (id_irq & 0xff)
660 1.15 cgd return (inb(IO_ICU1) & id_irq);
661 1.15 cgd return (inb(IO_ICU2) & (id_irq >> 8));
662 1.1 cgd }
663