isa_machdep.c revision 1.7 1 /* $NetBSD: isa_machdep.c,v 1.7 2007/12/03 15:33:18 ad Exp $ */
2
3 /*-
4 * Copyright (c) 1996-1998 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Mark Brinicombe, Charles M. Hannum and by Jason R. Thorpe of the
9 * Numerical Aerospace Simulation Facility, NASA Ames Research Center.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the NetBSD
22 * Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
38 */
39
40 /*-
41 * Copyright (c) 1991 The Regents of the University of California.
42 * All rights reserved.
43 *
44 * This code is derived from software contributed to Berkeley by
45 * William Jolitz.
46 *
47 * Redistribution and use in source and binary forms, with or without
48 * modification, are permitted provided that the following conditions
49 * are met:
50 * 1. Redistributions of source code must retain the above copyright
51 * notice, this list of conditions and the following disclaimer.
52 * 2. Redistributions in binary form must reproduce the above copyright
53 * notice, this list of conditions and the following disclaimer in the
54 * documentation and/or other materials provided with the distribution.
55 * 3. Neither the name of the University nor the names of its contributors
56 * may be used to endorse or promote products derived from this software
57 * without specific prior written permission.
58 *
59 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
60 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
61 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
62 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
63 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
64 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
65 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
66 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
67 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
68 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
69 * SUCH DAMAGE.
70 *
71 * @(#)isa.c 7.2 (Berkeley) 5/13/91
72 */
73
74 #include <sys/cdefs.h>
75 __KERNEL_RCSID(0, "$NetBSD: isa_machdep.c,v 1.7 2007/12/03 15:33:18 ad Exp $");
76
77 #include "opt_irqstats.h"
78
79 #include <sys/param.h>
80 #include <sys/systm.h>
81 #include <sys/kernel.h>
82 #include <sys/syslog.h>
83 #include <sys/device.h>
84 #include <sys/malloc.h>
85 #include <sys/proc.h>
86
87 #define _ARM32_BUS_DMA_PRIVATE
88 #include <machine/bus.h>
89
90 #include <machine/intr.h>
91 #include <machine/pio.h>
92 #include <machine/bootconfig.h>
93 #include <machine/isa_machdep.h>
94
95 #include <dev/isa/isareg.h>
96 #include <dev/isa/isavar.h>
97 #include <dev/isa/isadmareg.h>
98 #include <dev/isa/isadmavar.h>
99 #include <arm/footbridge/isa/icu.h>
100 #include <arm/footbridge/dc21285reg.h>
101 #include <arm/footbridge/dc21285mem.h>
102
103 #include <uvm/uvm_extern.h>
104
105 #include "isadma.h"
106
107 /* prototypes */
108 static void isa_icu_init __P((void));
109
110 struct arm32_isa_chipset isa_chipset_tag;
111
112 void isa_strayintr __P((int));
113 void intr_calculatemasks __P((void));
114 int fakeintr __P((void *));
115
116 int isa_irqdispatch __P((void *arg));
117
118 u_int imask[NIPL];
119 unsigned imen;
120
121 #define AUTO_EOI_1
122 #define AUTO_EOI_2
123
124 /*
125 * Fill in default interrupt table (in case of spuruious interrupt
126 * during configuration of kernel, setup interrupt control unit
127 */
128 static void
129 isa_icu_init(void)
130 {
131 /* initialize 8259's */
132 outb(IO_ICU1, 0x11); /* reset; program device, four bytes */
133 outb(IO_ICU1+1, ICU_OFFSET); /* starting at this vector index */
134 outb(IO_ICU1+1, 1 << IRQ_SLAVE); /* slave on line 2 */
135 #ifdef AUTO_EOI_1
136 outb(IO_ICU1+1, 2 | 1); /* auto EOI, 8086 mode */
137 #else
138 outb(IO_ICU1+1, 1); /* 8086 mode */
139 #endif
140 outb(IO_ICU1+1, 0xff); /* leave interrupts masked */
141 outb(IO_ICU1, 0x68); /* special mask mode (if available) */
142 outb(IO_ICU1, 0x0a); /* Read IRR by default. */
143 #ifdef REORDER_IRQ
144 outb(IO_ICU1, 0xc0 | (3 - 1)); /* pri order 3-7, 0-2 (com2 first) */
145 #endif
146
147 outb(IO_ICU2, 0x11); /* reset; program device, four bytes */
148 outb(IO_ICU2+1, ICU_OFFSET+8); /* staring at this vector index */
149 outb(IO_ICU2+1, IRQ_SLAVE);
150 #ifdef AUTO_EOI_2
151 outb(IO_ICU2+1, 2 | 1); /* auto EOI, 8086 mode */
152 #else
153 outb(IO_ICU2+1, 1); /* 8086 mode */
154 #endif
155 outb(IO_ICU2+1, 0xff); /* leave interrupts masked */
156 outb(IO_ICU2, 0x68); /* special mask mode (if available) */
157 outb(IO_ICU2, 0x0a); /* Read IRR by default. */
158 }
159
160 /*
161 * Caught a stray interrupt, notify
162 */
163 void
164 isa_strayintr(irq)
165 int irq;
166 {
167 static u_long strays;
168
169 /*
170 * Stray interrupts on irq 7 occur when an interrupt line is raised
171 * and then lowered before the CPU acknowledges it. This generally
172 * means either the device is screwed or something is cli'ing too
173 * long and it's timing out.
174 */
175 if (++strays <= 5)
176 log(LOG_ERR, "stray interrupt %d%s\n", irq,
177 strays >= 5 ? "; stopped logging" : "");
178 }
179
180 static struct intrq isa_intrq[ICU_LEN];
181
182 /*
183 * Recalculate the interrupt masks from scratch.
184 * We could code special registry and deregistry versions of this function that
185 * would be faster, but the code would be nastier, and we don't expect this to
186 * happen very much anyway.
187 */
188 void
189 intr_calculatemasks()
190 {
191 int irq, level;
192 struct intrq *iq;
193 struct intrhand *ih;
194
195 /* First, figure out which levels each IRQ uses. */
196 for (irq = 0; irq < ICU_LEN; irq++) {
197 int levels = 0;
198 iq = &isa_intrq[irq];
199 for (ih = TAILQ_FIRST(&iq->iq_list); ih != NULL;
200 ih = TAILQ_NEXT(ih, ih_list))
201 levels |= (1U << ih->ih_ipl);
202 iq->iq_levels = levels;
203 }
204
205 /* Then figure out which IRQs use each level. */
206 for (level = 0; level < NIPL; level++) {
207 int irqs = 0;
208 for (irq = 0; irq < ICU_LEN; irq++)
209 if (isa_intrq[irq].iq_levels & (1U << level))
210 irqs |= (1U << irq);
211 imask[level] = irqs;
212 }
213
214 imask[IPL_NONE] = 0;
215 imask[IPL_SOFTCLOCK] |= imask[IPL_NONE];
216 imask[IPL_SOFTBIO] |= imask[IPL_SOFTCLOCK];
217 imask[IPL_SOFTNET] |= imask[IPL_SOFTBIO];
218 imask[IPL_SOFTSERIAL] |= imask[IPL_SOFTNET];
219 imask[IPL_VM] |= imask[IPL_SOFTSERIAL];
220 imask[IPL_SCHED] |= imask[IPL_VM];
221 imask[IPL_HIGH] |= imask[IPL_SCHED];
222
223 /* And eventually calculate the complete masks. */
224 for (irq = 0; irq < ICU_LEN; irq++) {
225 int irqs = 1 << irq;
226 iq = &isa_intrq[irq];
227 for (ih = TAILQ_FIRST(&iq->iq_list); ih != NULL;
228 ih = TAILQ_NEXT(ih, ih_list))
229 irqs |= imask[ih->ih_ipl];
230 iq->iq_mask = irqs;
231 }
232
233 /* Lastly, determine which IRQs are actually in use. */
234 {
235 int irqs = 0;
236 for (irq = 0; irq < ICU_LEN; irq++)
237 if (!TAILQ_EMPTY(&isa_intrq[irq].iq_list))
238 irqs |= (1U << irq);
239 if (irqs >= 0x100) /* any IRQs >= 8 in use */
240 irqs |= 1 << IRQ_SLAVE;
241 imen = ~irqs;
242 SET_ICUS();
243 }
244 #if 0
245 printf("type\tmask\tlevel\thand\n");
246 for (irq = 0; irq < ICU_LEN; irq++) {
247 printf("%x\t%04x\t%x\t%p\n", intrtype[irq], intrmask[irq],
248 intrlevel[irq], intrhand[irq]);
249 }
250 for (level = 0; level < IPL_LEVELS; ++level)
251 printf("%d: %08x\n", level, imask[level]);
252 #endif
253 }
254
255 int
256 fakeintr(arg)
257 void *arg;
258 {
259
260 return 0;
261 }
262
263 #define LEGAL_IRQ(x) ((x) >= 0 && (x) < ICU_LEN && (x) != 2)
264
265 int
266 isa_intr_alloc(ic, mask, type, irq)
267 isa_chipset_tag_t ic;
268 int mask;
269 int type;
270 int *irq;
271 {
272 int i, tmp, bestirq, count;
273 struct intrq *iq;
274 struct intrhand *ih;
275
276 if (type == IST_NONE)
277 panic("intr_alloc: bogus type");
278
279 bestirq = -1;
280 count = -1;
281
282 /* some interrupts should never be dynamically allocated */
283 mask &= 0xdef8;
284
285 /*
286 * XXX some interrupts will be used later (6 for fdc, 12 for pms).
287 * the right answer is to do "breadth-first" searching of devices.
288 */
289 mask &= 0xefbf;
290
291 for (i = 0; i < ICU_LEN; i++) {
292 if (LEGAL_IRQ(i) == 0 || (mask & (1<<i)) == 0)
293 continue;
294
295 iq = &isa_intrq[i];
296 switch(iq->iq_ist) {
297 case IST_NONE:
298 /*
299 * if nothing's using the irq, just return it
300 */
301 *irq = i;
302 return (0);
303
304 case IST_EDGE:
305 case IST_LEVEL:
306 if (type != iq->iq_ist)
307 continue;
308 /*
309 * if the irq is shareable, count the number of other
310 * handlers, and if it's smaller than the last irq like
311 * this, remember it
312 *
313 * XXX We should probably also consider the
314 * interrupt level and stick IPL_TTY with other
315 * IPL_TTY, etc.
316 */
317 tmp = 0;
318 TAILQ_FOREACH(ih, &(iq->iq_list), ih_list)
319 tmp++;
320 if ((bestirq == -1) || (count > tmp)) {
321 bestirq = i;
322 count = tmp;
323 }
324 break;
325
326 case IST_PULSE:
327 /* this just isn't shareable */
328 continue;
329 }
330 }
331
332 if (bestirq == -1)
333 return (1);
334
335 *irq = bestirq;
336
337 return (0);
338 }
339
340 const struct evcnt *
341 isa_intr_evcnt(isa_chipset_tag_t ic, int irq)
342 {
343 return &isa_intrq[irq].iq_ev;
344 }
345
346 /*
347 * Set up an interrupt handler to start being called.
348 * XXX PRONE TO RACE CONDITIONS, UGLY, 'INTERESTING' INSERTION ALGORITHM.
349 */
350 void *
351 isa_intr_establish(ic, irq, type, level, ih_fun, ih_arg)
352 isa_chipset_tag_t ic;
353 int irq;
354 int type;
355 int level;
356 int (*ih_fun) __P((void *));
357 void *ih_arg;
358 {
359 struct intrq *iq;
360 struct intrhand *ih;
361 u_int oldirqstate;
362
363 #if 0
364 printf("isa_intr_establish(%d, %d, %d)\n", irq, type, level);
365 #endif
366 /* no point in sleeping unless someone can free memory. */
367 ih = malloc(sizeof *ih, M_DEVBUF, cold ? M_NOWAIT : M_WAITOK);
368 if (ih == NULL)
369 return (NULL);
370
371 if (!LEGAL_IRQ(irq) || type == IST_NONE)
372 panic("intr_establish: bogus irq or type");
373
374 iq = &isa_intrq[irq];
375
376 switch (iq->iq_ist) {
377 case IST_NONE:
378 iq->iq_ist = type;
379 #if 0
380 printf("Setting irq %d to type %d - ", irq, type);
381 #endif
382 if (irq < 8) {
383 outb(0x4d0, (inb(0x4d0) & ~(1 << irq))
384 | ((type == IST_LEVEL) ? (1 << irq) : 0));
385 /* printf("%02x\n", inb(0x4d0));*/
386 } else {
387 outb(0x4d1, (inb(0x4d1) & ~(1 << irq))
388 | ((type == IST_LEVEL) ? (1 << irq) : 0));
389 /* printf("%02x\n", inb(0x4d1));*/
390 }
391 break;
392 case IST_EDGE:
393 case IST_LEVEL:
394 if (iq->iq_ist == type)
395 break;
396 case IST_PULSE:
397 if (type != IST_NONE)
398 panic("intr_establish: can't share %s with %s",
399 isa_intr_typename(iq->iq_ist),
400 isa_intr_typename(type));
401 break;
402 }
403
404 ih->ih_func = ih_fun;
405 ih->ih_arg = ih_arg;
406 ih->ih_ipl = level;
407 ih->ih_irq = irq;
408
409 /* do not stop us */
410 oldirqstate = disable_interrupts(I32_bit);
411
412 TAILQ_INSERT_TAIL(&iq->iq_list, ih, ih_list);
413
414 intr_calculatemasks();
415 restore_interrupts(oldirqstate);
416
417 return (ih);
418 }
419
420 /*
421 * Deregister an interrupt handler.
422 */
423 void
424 isa_intr_disestablish(ic, arg)
425 isa_chipset_tag_t ic;
426 void *arg;
427 {
428 struct intrhand *ih = arg;
429 struct intrq *iq = &isa_intrq[ih->ih_irq];
430 int irq = ih->ih_irq;
431 u_int oldirqstate;
432
433 if (!LEGAL_IRQ(irq))
434 panic("intr_disestablish: bogus irq");
435
436 oldirqstate = disable_interrupts(I32_bit);
437
438 TAILQ_REMOVE(&iq->iq_list, ih, ih_list);
439
440 intr_calculatemasks();
441
442 restore_interrupts(oldirqstate);
443
444 free(ih, M_DEVBUF);
445
446 if (TAILQ_EMPTY(&(iq->iq_list)))
447 iq->iq_ist = IST_NONE;
448 }
449
450 /*
451 * isa_intr_init()
452 *
453 * Initialise the ISA ICU and attach an ISA interrupt handler to the
454 * ISA interrupt line on the footbridge.
455 */
456 void
457 isa_intr_init(void)
458 {
459 static void *isa_ih;
460 struct intrq *iq;
461 int i;
462
463 /*
464 * should get the parent here, but initialisation order being so
465 * strange I need to check if it's available
466 */
467 for (i = 0; i < ICU_LEN; i++) {
468 iq = &isa_intrq[i];
469 TAILQ_INIT(&iq->iq_list);
470
471 sprintf(iq->iq_name, "irq %d", i);
472 evcnt_attach_dynamic(&iq->iq_ev, EVCNT_TYPE_INTR,
473 NULL, "isa", iq->iq_name);
474 }
475
476 isa_icu_init();
477 intr_calculatemasks();
478 /* something to break the build in an informative way */
479 #ifndef ISA_FOOTBRIDGE_IRQ
480 #warning Before using isa with footbridge you must define ISA_FOOTBRIDGE_IRQ
481 #endif
482 isa_ih = footbridge_intr_claim(ISA_FOOTBRIDGE_IRQ, IPL_BIO, "isabus",
483 isa_irqdispatch, NULL);
484
485 }
486
487 /* Static array of ISA DMA segments. We only have one on CATS */
488 #if NISADMA > 0
489 struct arm32_dma_range machdep_isa_dma_ranges[1];
490 #endif
491
492 void
493 isa_footbridge_init(iobase, membase)
494 u_int iobase, membase;
495 {
496 #if NISADMA > 0
497 extern struct arm32_dma_range *footbridge_isa_dma_ranges;
498 extern int footbridge_isa_dma_nranges;
499
500 machdep_isa_dma_ranges[0].dr_sysbase = bootconfig.dram[0].address;
501 machdep_isa_dma_ranges[0].dr_busbase = bootconfig.dram[0].address;
502 machdep_isa_dma_ranges[0].dr_len = (16 * 1024 * 1024);
503
504 footbridge_isa_dma_ranges = machdep_isa_dma_ranges;
505 footbridge_isa_dma_nranges = 1;
506 #endif
507
508 isa_io_init(iobase, membase);
509 }
510
511 void
512 isa_attach_hook(parent, self, iba)
513 struct device *parent, *self;
514 struct isabus_attach_args *iba;
515 {
516 /*
517 * Since we can only have one ISA bus, we just use a single
518 * statically allocated ISA chipset structure. Pass it up
519 * now.
520 */
521 iba->iba_ic = &isa_chipset_tag;
522 #if NISADMA > 0
523 isa_dma_init();
524 #endif
525 }
526
527 int
528 isa_irqdispatch(arg)
529 void *arg;
530 {
531 struct clockframe *frame = arg;
532 int irq;
533 struct intrq *iq;
534 struct intrhand *ih;
535 u_int iack;
536 int res = 0;
537
538 iack = *((u_int *)(DC21285_PCI_IACK_VBASE));
539 iack &= 0xff;
540 if (iack < 0x20 || iack > 0x2f) {
541 printf("isa_irqdispatch: %x\n", iack);
542 return(0);
543 }
544
545 irq = iack & 0x0f;
546 iq = &isa_intrq[irq];
547 iq->iq_ev.ev_count++;
548 for (ih = TAILQ_FIRST(&iq->iq_list); res != 1 && ih != NULL;
549 ih = TAILQ_NEXT(ih, ih_list)) {
550 res = (*ih->ih_func)(ih->ih_arg ? ih->ih_arg : frame);
551 }
552 return res;
553 }
554
555
556 void
557 isa_fillw(val, addr, len)
558 u_int val;
559 void *addr;
560 size_t len;
561 {
562 if ((u_int)addr >= isa_mem_data_vaddr()
563 && (u_int)addr < isa_mem_data_vaddr() + 0x100000) {
564 bus_size_t offset = ((u_int)addr) & 0xfffff;
565 bus_space_set_region_2(&isa_mem_bs_tag,
566 (bus_space_handle_t)isa_mem_bs_tag.bs_cookie, offset,
567 val, len);
568 } else {
569 u_short *ptr = addr;
570
571 while (len > 0) {
572 *ptr++ = val;
573 --len;
574 }
575 }
576 }
577