1/* $NetBSD: i8259_common.c,v 1.6 2011/06/20 06:21:45 matt Exp $ */
2
3/*-
4 * Copyright (c) 2007 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Tim Rightnour
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32#include <sys/cdefs.h>
33__KERNEL_RCSID(0, "$NetBSD: i8259_common.c,v 1.6 2011/06/20 06:21:45 matt Exp $");
34
35#include <sys/param.h>
36#include <sys/kernel.h>
37#include <sys/intr.h>
38
39#include <uvm/uvm_extern.h>
40
41#include <machine/pio.h>
42
43#include <powerpc/pic/picvar.h>
44
45#include <dev/isa/isareg.h>
46#include <dev/isa/isavar.h>
47
48void
49i8259_initialize(void)
50{
51	isa_outb(IO_ICU1, 0x11);		/* program device, four bytes */
52	isa_outb(IO_ICU1+1, 0);			/* starting at this vector */
53	isa_outb(IO_ICU1+1, 1 << IRQ_SLAVE);	/* slave on line 2 */
54	isa_outb(IO_ICU1+1, 1);			/* 8086 mode */
55	isa_outb(IO_ICU1+1, 0xff);		/* leave interrupts masked */
56
57	isa_outb(IO_ICU2, 0x11);		/* program device, four bytes */
58	isa_outb(IO_ICU2+1, 8);			/* starting at this vector */
59	isa_outb(IO_ICU2+1, IRQ_SLAVE);
60	isa_outb(IO_ICU2+1, 1);			/* 8086 mode */
61	isa_outb(IO_ICU2+1, 0xff);		/* leave interrupts masked */
62}
63
64void
65i8259_enable_irq(struct pic_ops *pic, int irq, int type)
66{
67	struct i8259_ops *i8259 = (struct i8259_ops *)pic;
68
69	i8259->irqs |= 1 << irq;
70	if (i8259->irqs >= 0x100) /* IRQS >= 8 in use? */
71		i8259->irqs |= 1 << IRQ_SLAVE;
72
73	i8259->enable_mask = ~i8259->irqs;
74	isa_outb(IO_ICU1+1, i8259->enable_mask);
75	isa_outb(IO_ICU2+1, i8259->enable_mask >> 8);
76}
77
78void
79i8259_disable_irq(struct pic_ops *pic, int irq)
80{
81	struct i8259_ops *i8259 = (struct i8259_ops *)pic;
82	uint32_t mask = 1 << irq;
83
84	i8259->enable_mask |= mask;
85	isa_outb(IO_ICU1+1, i8259->enable_mask);
86	isa_outb(IO_ICU2+1, i8259->enable_mask >> 8);
87}
88
89void
90i8259_ack_irq(struct pic_ops *pic, int irq)
91{
92	if (irq < 8) {
93		isa_outb(IO_ICU1, 0xe0 | irq);
94	} else {
95		isa_outb(IO_ICU2, 0xe0 | (irq & 7));
96		isa_outb(IO_ICU1, 0xe0 | IRQ_SLAVE);
97	}
98}
99
100int
101i8259_get_irq(struct pic_ops *pic, int mode)
102{
103	int irq;
104
105	isa_outb(IO_ICU1, 0x0c);
106	irq = isa_inb(IO_ICU1) & 0x07;
107	if (irq == IRQ_SLAVE) {
108		isa_outb(IO_ICU2, 0x0c);
109		irq = (isa_inb(IO_ICU2) & 0x07) + 8;
110	}
111
112	if (irq == 0 && mode == PIC_GET_IRQ)
113		return 255;
114	if (irq == 7 && mode == PIC_GET_RECHECK)
115		return 255;
116
117	return irq;
118}
119