isa.c revision 1.28.2.3 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.3 mycroft * $Id: isa.c,v 1.28.2.3 1993/09/30 20:21:17 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.3 mycroft printf(" XXX");
227 1.28.2.3 mycroft else
228 1.28.2.3 mycroft #endif
229 1.28.2.3 mycroft if (ia->ia_irq != IRQNONE)
230 1.28.2.1 mycroft printf(" irq %d", ffs(ia->ia_irq) - 1);
231 1.28.2.1 mycroft if (ia->ia_drq != DRQUNK)
232 1.28.2.1 mycroft printf(" drq %d", ia->ia_drq);
233 1.28.2.1 mycroft if (ia->ia_msize)
234 1.28.2.1 mycroft printf(" iomem 0x%x", ia->ia_maddr);
235 1.28.2.1 mycroft if (ia->ia_msize > 1)
236 1.28.2.1 mycroft printf("-0x%x", ia->ia_maddr + ia->ia_msize - 1);
237 1.28.2.1 mycroft /* XXXX need to print flags */
238 1.28.2.2 mycroft return QUIET; /* actually, our return value is irrelevant. */
239 1.1 cgd }
240 1.1 cgd
241 1.1 cgd
242 1.1 cgd #define IDTVEC(name) __CONCAT(X,name)
243 1.1 cgd /* default interrupt vector table entries */
244 1.1 cgd extern IDTVEC(intr0), IDTVEC(intr1), IDTVEC(intr2), IDTVEC(intr3),
245 1.1 cgd IDTVEC(intr4), IDTVEC(intr5), IDTVEC(intr6), IDTVEC(intr7),
246 1.1 cgd IDTVEC(intr8), IDTVEC(intr9), IDTVEC(intr10), IDTVEC(intr11),
247 1.1 cgd IDTVEC(intr12), IDTVEC(intr13), IDTVEC(intr14), IDTVEC(intr15);
248 1.1 cgd
249 1.1 cgd static *defvec[16] = {
250 1.1 cgd &IDTVEC(intr0), &IDTVEC(intr1), &IDTVEC(intr2), &IDTVEC(intr3),
251 1.1 cgd &IDTVEC(intr4), &IDTVEC(intr5), &IDTVEC(intr6), &IDTVEC(intr7),
252 1.1 cgd &IDTVEC(intr8), &IDTVEC(intr9), &IDTVEC(intr10), &IDTVEC(intr11),
253 1.1 cgd &IDTVEC(intr12), &IDTVEC(intr13), &IDTVEC(intr14), &IDTVEC(intr15) };
254 1.1 cgd
255 1.1 cgd /* out of range default interrupt vector gate entry */
256 1.1 cgd extern IDTVEC(intrdefault);
257 1.15 cgd
258 1.1 cgd /*
259 1.28.2.1 mycroft * Fill in default interrupt table, and mask all interrupts.
260 1.1 cgd */
261 1.21 andrew void
262 1.28.2.1 mycroft isa_defaultirq()
263 1.28.2.1 mycroft {
264 1.1 cgd int i;
265 1.1 cgd
266 1.1 cgd /* icu vectors */
267 1.28.2.1 mycroft for (i = ICU_OFFSET; i < ICU_OFFSET + ICU_LEN ; i++)
268 1.1 cgd setidt(i, defvec[i], SDT_SYS386IGT, SEL_KPL);
269 1.15 cgd
270 1.1 cgd /* out of range vectors */
271 1.28.2.1 mycroft for (; i < NIDT; i++)
272 1.1 cgd setidt(i, &IDTVEC(intrdefault), SDT_SYS386IGT, SEL_KPL);
273 1.1 cgd
274 1.1 cgd /* initialize 8259's */
275 1.1 cgd outb(IO_ICU1, 0x11); /* reset; program device, four bytes */
276 1.28.2.1 mycroft outb(IO_ICU1+1, ICU_OFFSET); /* starting at this vector index */
277 1.28.2.1 mycroft outb(IO_ICU1+1, IRQ_SLAVE);
278 1.15 cgd #ifdef AUTO_EOI_1
279 1.15 cgd outb(IO_ICU1+1, 2 | 1); /* auto EOI, 8086 mode */
280 1.15 cgd #else
281 1.1 cgd outb(IO_ICU1+1, 1); /* 8086 mode */
282 1.15 cgd #endif
283 1.1 cgd outb(IO_ICU1+1, 0xff); /* leave interrupts masked */
284 1.15 cgd outb(IO_ICU1, 0x0a); /* default to IRR on read */
285 1.21 andrew #ifdef REORDER_IRQ
286 1.15 cgd outb(IO_ICU1, 0xc0 | (3 - 1)); /* pri order 3-7, 0-2 (com2 first) */
287 1.21 andrew #endif
288 1.1 cgd
289 1.1 cgd outb(IO_ICU2, 0x11); /* reset; program device, four bytes */
290 1.28.2.1 mycroft outb(IO_ICU2+1, ICU_OFFSET+8); /* staring at this vector index */
291 1.28.2.1 mycroft outb(IO_ICU2+1, ffs(IRQ_SLAVE)-1);
292 1.15 cgd #ifdef AUTO_EOI_2
293 1.15 cgd outb(IO_ICU2+1, 2 | 1); /* auto EOI, 8086 mode */
294 1.15 cgd #else
295 1.28.2.1 mycroft outb(IO_ICU2+1, 1); /* 8086 mode */
296 1.15 cgd #endif
297 1.1 cgd outb(IO_ICU2+1, 0xff); /* leave interrupts masked */
298 1.15 cgd outb(IO_ICU2, 0x0a); /* default to IRR on read */
299 1.1 cgd }
300 1.1 cgd
301 1.28.2.1 mycroft
302 1.28.2.1 mycroft /* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX */
303 1.28.2.1 mycroft
304 1.28.2.1 mycroft int
305 1.28.2.1 mycroft config_isadev(isdp, mp)
306 1.28.2.1 mycroft struct isa_device *isdp;
307 1.28.2.1 mycroft u_int *mp;
308 1.28.2.1 mycroft {
309 1.28.2.1 mycroft if (isdp->id_irq) {
310 1.28.2.1 mycroft int intrno;
311 1.28.2.1 mycroft
312 1.28.2.1 mycroft intrno = ffs(isdp->id_irq)-1;
313 1.28.2.1 mycroft setidt(ICU_OFFSET+intrno, isdp->id_intr,
314 1.28.2.1 mycroft SDT_SYS386IGT, SEL_KPL);
315 1.28.2.1 mycroft if(mp)
316 1.28.2.1 mycroft INTRMASK(*mp,isdp->id_irq);
317 1.28.2.1 mycroft INTREN(isdp->id_irq);
318 1.28.2.1 mycroft }
319 1.28.2.1 mycroft }
320 1.28.2.1 mycroft
321 1.28.2.1 mycroft
322 1.1 cgd /* region of physical memory known to be contiguous */
323 1.1 cgd vm_offset_t isaphysmem;
324 1.28.2.1 mycroft static caddr_t bouncebuf[8]; /* XXX */
325 1.28.2.1 mycroft static caddr_t bounced[8]; /* XXX */
326 1.28.2.1 mycroft static vm_size_t bouncesize[8]; /* XXX */
327 1.28.2.1 mycroft #define MAXDMASZ 512 /* XXX */
328 1.1 cgd
329 1.1 cgd /* high byte of address is stored in this port for i-th dma channel */
330 1.1 cgd static short dmapageport[8] =
331 1.1 cgd { 0x87, 0x83, 0x81, 0x82, 0x8f, 0x8b, 0x89, 0x8a };
332 1.1 cgd
333 1.1 cgd /*
334 1.1 cgd * isa_dmacascade(): program 8237 DMA controller channel to accept
335 1.1 cgd * external dma control by a board.
336 1.1 cgd */
337 1.21 andrew void
338 1.28.2.1 mycroft at_dma_cascade(chan)
339 1.28.2.1 mycroft unsigned chan;
340 1.2 cgd {
341 1.28.2.1 mycroft #ifdef DEBUG
342 1.1 cgd if (chan > 7)
343 1.28.2.1 mycroft panic("at_dma_cascade: impossible request");
344 1.28.2.1 mycroft #endif
345 1.1 cgd
346 1.1 cgd /* set dma channel mode, and set dma channel mode */
347 1.2 cgd if ((chan & 4) == 0) {
348 1.2 cgd outb(DMA1_MODE, DMA37MD_CASCADE | chan);
349 1.2 cgd outb(DMA1_SMSK, chan);
350 1.2 cgd } else {
351 1.2 cgd outb(DMA2_MODE, DMA37MD_CASCADE | (chan & 3));
352 1.2 cgd outb(DMA2_SMSK, chan & 3);
353 1.2 cgd }
354 1.1 cgd }
355 1.1 cgd
356 1.1 cgd /*
357 1.28.2.1 mycroft * at_dma(): program 8237 DMA controller channel, avoid page alignment
358 1.1 cgd * problems by using a bounce buffer.
359 1.1 cgd */
360 1.21 andrew void
361 1.28.2.1 mycroft at_dma(flags, addr, nbytes, chan)
362 1.28.2.1 mycroft int read;
363 1.28.2.1 mycroft caddr_t addr;
364 1.28.2.1 mycroft vm_size_t nbytes;
365 1.28.2.1 mycroft unsigned chan;
366 1.28.2.1 mycroft {
367 1.28.2.1 mycroft vm_offset_t phys;
368 1.2 cgd int waport;
369 1.1 cgd caddr_t newaddr;
370 1.1 cgd
371 1.28.2.1 mycroft ppp if ( chan > 7
372 1.2 cgd || (chan < 4 && nbytes > (1<<16))
373 1.2 cgd || (chan >= 4 && (nbytes > (1<<17) || (u_int)addr & 1)))
374 1.1 cgd panic("isa_dmastart: impossible request");
375 1.1 cgd
376 1.28.2.1 mycroft if (at_dma_rangecheck(addr, nbytes, chan)) {
377 1.28.2.1 mycroft panic("bounce buffers don't work yet\n");
378 1.28.2.1 mycroft /* XXX totally braindead; NBPG is not enough */
379 1.28.2.1 mycroft if (bouncebuf[chan] == 0)
380 1.28.2.1 mycroft bouncebuf[chan] =
381 1.1 cgd /*(caddr_t)malloc(MAXDMASZ, M_TEMP, M_WAITOK);*/
382 1.1 cgd (caddr_t) isaphysmem + NBPG*chan;
383 1.28.2.1 mycroft bouncesize[chan] = nbytes;
384 1.28.2.1 mycroft newaddr = bouncebuf[chan];
385 1.1 cgd /* copy bounce buffer on write */
386 1.28.2.1 mycroft if (!read)
387 1.1 cgd bcopy(addr, newaddr, nbytes);
388 1.28.2.1 mycroft else
389 1.28.2.1 mycroft bounced[chan] = addr;
390 1.1 cgd addr = newaddr;
391 1.1 cgd }
392 1.1 cgd
393 1.1 cgd /* translate to physical */
394 1.1 cgd phys = pmap_extract(pmap_kernel(), (vm_offset_t)addr);
395 1.1 cgd
396 1.2 cgd if ((chan & 4) == 0) {
397 1.2 cgd /*
398 1.2 cgd * Program one of DMA channels 0..3. These are
399 1.2 cgd * byte mode channels.
400 1.2 cgd */
401 1.2 cgd /* set dma channel mode, and reset address ff */
402 1.28.2.1 mycroft if (read)
403 1.2 cgd outb(DMA1_MODE, DMA37MD_SINGLE|DMA37MD_WRITE|chan);
404 1.2 cgd else
405 1.2 cgd outb(DMA1_MODE, DMA37MD_SINGLE|DMA37MD_READ|chan);
406 1.2 cgd outb(DMA1_FFC, 0);
407 1.1 cgd
408 1.2 cgd /* send start address */
409 1.2 cgd waport = DMA1_CHN(chan);
410 1.1 cgd outb(waport, phys);
411 1.1 cgd outb(waport, phys>>8);
412 1.2 cgd outb(dmapageport[chan], phys>>16);
413 1.2 cgd
414 1.2 cgd /* send count */
415 1.2 cgd outb(waport + 1, --nbytes);
416 1.2 cgd outb(waport + 1, nbytes>>8);
417 1.2 cgd
418 1.2 cgd /* unmask channel */
419 1.2 cgd outb(DMA1_SMSK, chan);
420 1.1 cgd } else {
421 1.2 cgd /*
422 1.2 cgd * Program one of DMA channels 4..7. These are
423 1.2 cgd * word mode channels.
424 1.2 cgd */
425 1.2 cgd /* set dma channel mode, and reset address ff */
426 1.28.2.1 mycroft if (read)
427 1.2 cgd outb(DMA2_MODE, DMA37MD_SINGLE|DMA37MD_WRITE|(chan&3));
428 1.2 cgd else
429 1.2 cgd outb(DMA2_MODE, DMA37MD_SINGLE|DMA37MD_READ|(chan&3));
430 1.2 cgd outb(DMA2_FFC, 0);
431 1.2 cgd
432 1.2 cgd /* send start address */
433 1.2 cgd waport = DMA2_CHN(chan - 4);
434 1.1 cgd outb(waport, phys>>1);
435 1.1 cgd outb(waport, phys>>9);
436 1.2 cgd outb(dmapageport[chan], phys>>16);
437 1.1 cgd
438 1.2 cgd /* send count */
439 1.2 cgd nbytes >>= 1;
440 1.1 cgd outb(waport + 2, --nbytes);
441 1.1 cgd outb(waport + 2, nbytes>>8);
442 1.2 cgd
443 1.2 cgd /* unmask channel */
444 1.2 cgd outb(DMA2_SMSK, chan & 3);
445 1.1 cgd }
446 1.1 cgd }
447 1.1 cgd
448 1.21 andrew void
449 1.28.2.1 mycroft at_dma_terminate(int flags, caddr_t addr, int nbytes, int chan)
450 1.1 cgd {
451 1.1 cgd
452 1.1 cgd if (bounced[chan]) {
453 1.28.2.1 mycroft bcopy(bouncebuf[chan], bounced[chan], bouncesize[chan]);
454 1.1 cgd bounced[chan] = 0;
455 1.1 cgd }
456 1.1 cgd }
457 1.1 cgd
458 1.1 cgd /*
459 1.1 cgd * Check for problems with the address range of a DMA transfer
460 1.2 cgd * (non-contiguous physical pages, outside of bus address space,
461 1.2 cgd * crossing DMA page boundaries).
462 1.1 cgd * Return true if special handling needed.
463 1.1 cgd */
464 1.1 cgd
465 1.21 andrew int
466 1.28.2.1 mycroft at_dma_rangecheck(caddr_t va, unsigned length, unsigned chan) {
467 1.2 cgd vm_offset_t phys, priorpage = 0, endva;
468 1.2 cgd u_int dma_pgmsk = (chan & 4) ? ~(128*1024-1) : ~(64*1024-1);
469 1.1 cgd
470 1.1 cgd endva = (vm_offset_t)round_page(va + length);
471 1.1 cgd for (; va < (caddr_t) endva ; va += NBPG) {
472 1.1 cgd phys = trunc_page(pmap_extract(pmap_kernel(), (vm_offset_t)va));
473 1.1 cgd if (phys == 0)
474 1.1 cgd panic("isa_dmacheck: no physical page present");
475 1.28.2.1 mycroft if (phys > physmem)
476 1.1 cgd return (1);
477 1.2 cgd if (priorpage) {
478 1.2 cgd if (priorpage + NBPG != phys)
479 1.2 cgd return (1);
480 1.2 cgd /* check if crossing a DMA page boundary */
481 1.2 cgd if (((u_int)priorpage ^ (u_int)phys) & dma_pgmsk)
482 1.2 cgd return (1);
483 1.2 cgd }
484 1.1 cgd priorpage = phys;
485 1.1 cgd }
486 1.1 cgd return (0);
487 1.1 cgd }
488 1.1 cgd
489 1.1 cgd /* head of queue waiting for physmem to become available */
490 1.1 cgd struct buf isa_physmemq;
491 1.1 cgd
492 1.1 cgd /* blocked waiting for resource to become free for exclusive use */
493 1.1 cgd static isaphysmemflag;
494 1.1 cgd /* if waited for and call requested when free (B_CALL) */
495 1.28.2.1 mycroft static void (*isaphysmemunblock)(); /* XXX needs to be a list */
496 1.1 cgd
497 1.1 cgd /*
498 1.1 cgd * Allocate contiguous physical memory for transfer, returning
499 1.1 cgd * a *virtual* address to region. May block waiting for resource.
500 1.1 cgd * (assumed to be called at splbio())
501 1.1 cgd */
502 1.1 cgd caddr_t
503 1.1 cgd isa_allocphysmem(caddr_t va, unsigned length, void (*func)()) {
504 1.1 cgd
505 1.1 cgd isaphysmemunblock = func;
506 1.1 cgd while (isaphysmemflag & B_BUSY) {
507 1.1 cgd isaphysmemflag |= B_WANTED;
508 1.24 deraadt sleep((caddr_t)&isaphysmemflag, PRIBIO);
509 1.1 cgd }
510 1.1 cgd isaphysmemflag |= B_BUSY;
511 1.1 cgd
512 1.1 cgd return((caddr_t)isaphysmem);
513 1.1 cgd }
514 1.1 cgd
515 1.1 cgd /*
516 1.1 cgd * Free contiguous physical memory used for transfer.
517 1.1 cgd * (assumed to be called at splbio())
518 1.1 cgd */
519 1.1 cgd void
520 1.1 cgd isa_freephysmem(caddr_t va, unsigned length) {
521 1.1 cgd
522 1.1 cgd isaphysmemflag &= ~B_BUSY;
523 1.1 cgd if (isaphysmemflag & B_WANTED) {
524 1.1 cgd isaphysmemflag &= B_WANTED;
525 1.24 deraadt wakeup((caddr_t)&isaphysmemflag);
526 1.1 cgd if (isaphysmemunblock)
527 1.1 cgd (*isaphysmemunblock)();
528 1.1 cgd }
529 1.1 cgd }
530 1.1 cgd
531 1.1 cgd /*
532 1.1 cgd * Handle a NMI, possibly a machine check.
533 1.1 cgd * return true to panic system, false to ignore.
534 1.1 cgd */
535 1.21 andrew int
536 1.1 cgd isa_nmi(cd) {
537 1.1 cgd
538 1.1 cgd log(LOG_CRIT, "\nNMI port 61 %x, port 70 %x\n", inb(0x61), inb(0x70));
539 1.1 cgd return(0);
540 1.1 cgd }
541 1.1 cgd
542 1.1 cgd /*
543 1.1 cgd * Caught a stray interrupt, notify
544 1.1 cgd */
545 1.21 andrew void
546 1.28.2.1 mycroft isa_strayintr(d)
547 1.28.2.1 mycroft int d;
548 1.28.2.1 mycroft {
549 1.1 cgd
550 1.4 cgd /*
551 1.28.2.1 mycroft * Stray level 7 interrupts occur when someone raises an interrupt
552 1.28.2.1 mycroft * and then drops it before the CPU acknowledges it. This means
553 1.28.2.1 mycroft * either the device is screwed or something is cli'ing too long.
554 1.4 cgd */
555 1.15 cgd extern u_long intrcnt_stray;
556 1.4 cgd
557 1.15 cgd intrcnt_stray++;
558 1.15 cgd if (intrcnt_stray <= 5)
559 1.28.2.1 mycroft log(LOG_ERR, "stray interrupt %d\n", d);
560 1.15 cgd if (intrcnt_stray == 5)
561 1.28.2.1 mycroft log(LOG_CRIT,"too many stray interrupts; stopped logging\n");
562 1.1 cgd }
563 1.1 cgd
564 1.1 cgd static beeping;
565 1.21 andrew static void
566 1.21 andrew sysbeepstop(int f)
567 1.1 cgd {
568 1.16 mycroft int s = splhigh();
569 1.16 mycroft
570 1.1 cgd /* disable counter 2 */
571 1.16 mycroft disable_intr();
572 1.28 brezak outb(PITAUX_PORT, inb(PITAUX_PORT) & ~PIT_SPKR);
573 1.16 mycroft enable_intr();
574 1.1 cgd if (f)
575 1.24 deraadt timeout((timeout_t)sysbeepstop, (caddr_t)0, f);
576 1.1 cgd else
577 1.1 cgd beeping = 0;
578 1.16 mycroft
579 1.16 mycroft splx(s);
580 1.1 cgd }
581 1.1 cgd
582 1.21 andrew void
583 1.21 andrew sysbeep(int pitch, int period)
584 1.1 cgd {
585 1.16 mycroft int s = splhigh();
586 1.16 mycroft static int last_pitch, last_period;
587 1.1 cgd
588 1.16 mycroft if (beeping) {
589 1.24 deraadt untimeout((timeout_t)sysbeepstop, (caddr_t)(last_period/2));
590 1.24 deraadt untimeout((timeout_t)sysbeepstop, (caddr_t)0);
591 1.1 cgd }
592 1.16 mycroft if (!beeping || last_pitch != pitch) {
593 1.16 mycroft /*
594 1.16 mycroft * XXX - move timer stuff to clock.c.
595 1.16 mycroft */
596 1.16 mycroft disable_intr();
597 1.16 mycroft outb(TIMER_MODE, TIMER_SEL2|TIMER_16BIT|TIMER_SQWAVE);
598 1.19 mycroft outb(TIMER_CNTR2, TIMER_DIV(pitch)%256);
599 1.19 mycroft outb(TIMER_CNTR2, TIMER_DIV(pitch)/256);
600 1.28 brezak outb(PITAUX_PORT, inb(PITAUX_PORT) | PIT_SPKR); /* enable counter 2 */
601 1.16 mycroft enable_intr();
602 1.16 mycroft }
603 1.16 mycroft last_pitch = pitch;
604 1.16 mycroft beeping = last_period = period;
605 1.24 deraadt timeout((timeout_t)sysbeepstop, (caddr_t)(period/2), period);
606 1.16 mycroft
607 1.16 mycroft splx(s);
608 1.1 cgd }
609 1.1 cgd
610 1.1 cgd /*
611 1.1 cgd * Pass command to keyboard controller (8042)
612 1.1 cgd */
613 1.21 andrew unsigned
614 1.21 andrew kbc_8042cmd(int val)
615 1.21 andrew {
616 1.1 cgd while (inb(KBSTATP)&KBS_IBF);
617 1.1 cgd if (val) outb(KBCMDP, val);
618 1.1 cgd while (inb(KBSTATP)&KBS_IBF);
619 1.1 cgd return (inb(KBDATAP));
620 1.28 brezak }
621 1.28 brezak
622 1.28 brezak /*
623 1.28 brezak * find an ISA device in a given isa_devtab_* table, given
624 1.28 brezak * the table to search, the expected id_driver entry, and the unit number.
625 1.28 brezak *
626 1.28 brezak * this function is defined in isa_device.h, and this location is debatable;
627 1.28 brezak * i put it there because it's useless w/o, and directly operates on
628 1.28 brezak * the other stuff in that file.
629 1.28 brezak *
630 1.28 brezak */
631 1.28 brezak
632 1.28 brezak struct isa_device *find_isadev(table, driverp, unit)
633 1.28 brezak struct isa_device *table;
634 1.28 brezak struct isa_driver *driverp;
635 1.28 brezak int unit;
636 1.28 brezak {
637 1.28 brezak if (driverp == NULL) /* sanity check */
638 1.28 brezak return NULL;
639 1.28 brezak
640 1.28 brezak while ((table->id_driver != driverp) || (table->id_unit != unit)) {
641 1.28 brezak if (table->id_driver == 0)
642 1.28 brezak return NULL;
643 1.28 brezak
644 1.28 brezak table++;
645 1.28 brezak }
646 1.28 brezak
647 1.28 brezak return table;
648 1.15 cgd }
649 1.15 cgd
650 1.15 cgd /*
651 1.15 cgd * Return nonzero if a (masked) irq is pending for a given device.
652 1.15 cgd */
653 1.15 cgd int
654 1.15 cgd isa_irq_pending(dvp)
655 1.15 cgd struct isa_device *dvp;
656 1.15 cgd {
657 1.15 cgd unsigned id_irq;
658 1.15 cgd
659 1.15 cgd id_irq = (unsigned short) dvp->id_irq; /* XXX silly type in struct */
660 1.15 cgd if (id_irq & 0xff)
661 1.15 cgd return (inb(IO_ICU1) & id_irq);
662 1.15 cgd return (inb(IO_ICU2) & (id_irq >> 8));
663 1.1 cgd }
664