1/* $NetBSD: pic_prepivr.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: pic_prepivr.c,v 1.6 2011/06/20 06:21:45 matt Exp $");
34
35#include <sys/param.h>
36#include <sys/kmem.h>
37#include <sys/kernel.h>
38#include <sys/intr.h>
39
40#include <uvm/uvm_extern.h>
41
42#include <machine/pio.h>
43
44#include <powerpc/pic/picvar.h>
45
46#include <dev/isa/isareg.h>
47#include <dev/isa/isavar.h>
48
49static int  prepivr_get_irq(struct pic_ops *, int);
50static int  motivr_get_irq(struct pic_ops *, int);
51static void prepivr_establish_irq(struct pic_ops *, int, int, int);
52
53vaddr_t prep_intr_reg;		/* PReP interrupt vector register */
54uint32_t prep_intr_reg_off;	/* IVR offset within the mapped page */
55
56#define IO_ELCR1	0x4d0
57#define IO_ELCR2	0x4d1
58
59/*
60 * Prior to calling init_prepivr, the port is expected to have mapped the
61 * page containing the IVR with mapiodev to prep_intr_reg and set up the
62 * prep_intr_reg_off.
63 */
64
65struct pic_ops *
66setup_prepivr(int ivrtype)
67{
68	struct i8259_ops *prepivr;
69	struct pic_ops *pic;
70	uint32_t pivr;
71
72	prepivr = kmem_alloc(sizeof(*prepivr), KM_SLEEP);
73	KASSERT(prepivr != NULL);
74	pic = &prepivr->pic;
75
76	pivr = prep_intr_reg + prep_intr_reg_off;
77	pic->pic_numintrs = 16;
78	pic->pic_cookie = (void *)pivr;
79	pic->pic_enable_irq = i8259_enable_irq;
80	pic->pic_reenable_irq = i8259_enable_irq;
81	pic->pic_disable_irq = i8259_disable_irq;
82	if (ivrtype == PIC_IVR_MOT)
83		pic->pic_get_irq = motivr_get_irq;
84	else
85		pic->pic_get_irq = prepivr_get_irq;
86	pic->pic_ack_irq = i8259_ack_irq;
87	pic->pic_establish_irq = prepivr_establish_irq;
88	pic->pic_finish_setup = NULL;
89	strcpy(pic->pic_name, "prepivr");
90	pic_add(pic);
91	prepivr->pending_events = 0;
92	prepivr->enable_mask = 0xffffffff;
93	prepivr->irqs = 0;
94
95	/* initialize the ELCR */
96	isa_outb(IO_ELCR1, (0 >> 0) & 0xff);
97	isa_outb(IO_ELCR2, (0 >> 8) & 0xff);
98	i8259_initialize();
99
100	return pic;
101}
102
103static void
104prepivr_establish_irq(struct pic_ops *pic, int irq, int type, int maxlevel)
105{
106	u_int8_t elcr[2];
107	int icu, bit;
108
109	icu = irq / 8;
110	bit = irq % 8;
111	elcr[0] = isa_inb(IO_ELCR1);
112	elcr[1] = isa_inb(IO_ELCR2);
113
114	if (type == IST_LEVEL)
115		elcr[icu] |= 1 << bit;
116	else
117		elcr[icu] &= ~(1 << bit);
118
119	isa_outb(IO_ELCR1, elcr[0]);
120	isa_outb(IO_ELCR2, elcr[1]);
121}
122
123static int
124motivr_get_irq(struct pic_ops *pic, int mode)
125{
126	int irq;
127
128	irq = i8259_get_irq(pic, mode);
129	/* read the IVR to ack it */
130	(void)inb(pic->pic_cookie);
131
132	/* i8259_get_irq will return 255 by itself, so just pass it on */
133	return irq;
134}
135
136static int
137prepivr_get_irq(struct pic_ops *pic, int mode)
138{
139	int irq;
140
141	irq = inb(pic->pic_cookie);
142
143	if (irq == 0 && mode == PIC_GET_IRQ)
144		return 255;
145	if (irq == 7 && mode == PIC_GET_RECHECK)
146		return 255;
147
148	return irq;
149}
150