intc.c revision 1.2 1 1.2 lukem /* $NetBSD: intc.c,v 1.2 2003/07/15 02:54:37 lukem Exp $ */
2 1.1 uch
3 1.1 uch /*-
4 1.1 uch * Copyright (c) 2001 The NetBSD Foundation, Inc.
5 1.1 uch * All rights reserved.
6 1.1 uch *
7 1.1 uch * This code is derived from software contributed to The NetBSD Foundation
8 1.1 uch * by UCHIYAMA Yasushi.
9 1.1 uch *
10 1.1 uch * Redistribution and use in source and binary forms, with or without
11 1.1 uch * modification, are permitted provided that the following conditions
12 1.1 uch * are met:
13 1.1 uch * 1. Redistributions of source code must retain the above copyright
14 1.1 uch * notice, this list of conditions and the following disclaimer.
15 1.1 uch * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 uch * notice, this list of conditions and the following disclaimer in the
17 1.1 uch * documentation and/or other materials provided with the distribution.
18 1.1 uch * 3. All advertising materials mentioning features or use of this software
19 1.1 uch * must display the following acknowledgement:
20 1.1 uch * This product includes software developed by the NetBSD
21 1.1 uch * Foundation, Inc. and its contributors.
22 1.1 uch * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1 uch * contributors may be used to endorse or promote products derived
24 1.1 uch * from this software without specific prior written permission.
25 1.1 uch *
26 1.1 uch * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1 uch * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1 uch * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1 uch * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.1 uch * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1 uch * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1 uch * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1 uch * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1 uch * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1 uch * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1 uch * POSSIBILITY OF SUCH DAMAGE.
37 1.1 uch */
38 1.2 lukem
39 1.2 lukem #include <sys/cdefs.h>
40 1.2 lukem __KERNEL_RCSID(0, "$NetBSD: intc.c,v 1.2 2003/07/15 02:54:37 lukem Exp $");
41 1.1 uch
42 1.1 uch #include "debug_playstation2.h"
43 1.1 uch
44 1.1 uch #include <sys/param.h>
45 1.1 uch #include <sys/systm.h>
46 1.1 uch
47 1.1 uch #include <playstation2/ee/eevar.h>
48 1.1 uch #include <playstation2/ee/intcvar.h>
49 1.1 uch #include <playstation2/ee/intcreg.h>
50 1.1 uch #include <playstation2/ee/gsvar.h> /* debug monitor */
51 1.1 uch
52 1.1 uch #include <playstation2/playstation2/interrupt.h>
53 1.1 uch
54 1.1 uch #ifdef DEBUG
55 1.1 uch #define LEGAL_CHANNEL(x) ((x) >= 0 && (x) <= 15)
56 1.1 uch #define STATIC
57 1.1 uch #else
58 1.1 uch #define STATIC static
59 1.1 uch #endif
60 1.1 uch
61 1.1 uch #define _INTC_NINTR 16
62 1.1 uch
63 1.1 uch u_int32_t __intc_enabled_channel;
64 1.1 uch
65 1.1 uch STATIC int __intc_initialized;
66 1.1 uch STATIC struct _ipl_dispatcher __intc_dispatcher[_INTC_NINTR];
67 1.1 uch STATIC struct _ipl_holder __intc_ipl_holder[_IPL_N];
68 1.1 uch
69 1.1 uch STATIC SLIST_HEAD(, _ipl_dispatcher) __intc_dispatcher_head =
70 1.1 uch SLIST_HEAD_INITIALIZER(__intc_dispatcher_head);
71 1.1 uch
72 1.1 uch void
73 1.1 uch intc_init()
74 1.1 uch {
75 1.1 uch int i;
76 1.1 uch
77 1.1 uch if (__intc_initialized++)
78 1.1 uch return;
79 1.1 uch
80 1.1 uch /* disable all channel */
81 1.1 uch for (i = 0; i < _INTC_NINTR; i++)
82 1.1 uch intc_intr_disable(i);
83 1.1 uch
84 1.1 uch /* clear interrupts */
85 1.1 uch _reg_write_4(I_STAT_REG, _reg_read_4(I_STAT_REG));
86 1.1 uch
87 1.1 uch for (i = 0; i < _IPL_N; i++)
88 1.1 uch __intc_ipl_holder[i].mask = 0xffffffff;
89 1.1 uch }
90 1.1 uch
91 1.1 uch int
92 1.1 uch intc_intr(u_int32_t mask)
93 1.1 uch {
94 1.1 uch struct _ipl_dispatcher *dispatcher;
95 1.1 uch u_int32_t r, dispatch, pending;
96 1.1 uch
97 1.1 uch r = _reg_read_4(I_STAT_REG);
98 1.1 uch dispatch = r & ~mask & __intc_enabled_channel;
99 1.1 uch pending = r & mask & __intc_enabled_channel;
100 1.1 uch
101 1.1 uch #if 0
102 1.1 uch __gsfb_print(1,
103 1.1 uch "INTC stat=%08x, mask=%08x, pend=%08x, disp=%08x enable=%08x\n",
104 1.1 uch r, mask, pending, dispatch, __intc_enabled_channel);
105 1.1 uch #endif
106 1.1 uch if (dispatch == 0)
107 1.1 uch return (pending == 0 ? 1 : 0);
108 1.1 uch
109 1.1 uch /* clear interrupt */
110 1.1 uch _reg_write_4(I_STAT_REG, dispatch);
111 1.1 uch
112 1.1 uch /* dispatch interrupt handler */
113 1.1 uch SLIST_FOREACH(dispatcher, &__intc_dispatcher_head, link) {
114 1.1 uch if (dispatcher->bit & dispatch) {
115 1.1 uch KDASSERT(dispatcher->func);
116 1.1 uch (*dispatcher->func)(dispatcher->arg);
117 1.1 uch dispatch &= ~dispatcher->bit;
118 1.1 uch }
119 1.1 uch }
120 1.1 uch
121 1.1 uch /* disable spurious interrupt source */
122 1.1 uch if (dispatch) {
123 1.1 uch int i, bit;
124 1.1 uch for (i = 0, bit = 1; i < _INTC_NINTR; i++, bit <<= 1) {
125 1.1 uch if (bit & dispatch) {
126 1.1 uch intc_intr_disable(i);
127 1.1 uch printf("%s: spurious interrupt %d disabled.\n",
128 1.1 uch __FUNCTION__, i);
129 1.1 uch }
130 1.1 uch }
131 1.1 uch }
132 1.1 uch
133 1.1 uch return (pending == 0 ? 1 : 0);
134 1.1 uch }
135 1.1 uch
136 1.1 uch void
137 1.1 uch intc_intr_enable(enum intc_channel ch)
138 1.1 uch {
139 1.1 uch u_int32_t mask;
140 1.1 uch
141 1.1 uch KDASSERT(LEGAL_CHANNEL(ch));
142 1.1 uch mask = 1 << ch;
143 1.1 uch _reg_write_4(I_MASK_REG, (_reg_read_4(I_MASK_REG) & mask) ^ mask);
144 1.1 uch }
145 1.1 uch
146 1.1 uch void
147 1.1 uch intc_intr_disable(enum intc_channel ch)
148 1.1 uch {
149 1.1 uch
150 1.1 uch KDASSERT(LEGAL_CHANNEL(ch));
151 1.1 uch _reg_write_4(I_MASK_REG, _reg_read_4(I_MASK_REG) & (1 << ch));
152 1.1 uch }
153 1.1 uch
154 1.1 uch void
155 1.1 uch intc_update_mask(u_int32_t mask)
156 1.1 uch {
157 1.1 uch u_int32_t cur_mask;
158 1.1 uch
159 1.1 uch cur_mask = _reg_read_4(I_MASK_REG);
160 1.1 uch
161 1.1 uch _reg_write_4(I_MASK_REG, ((cur_mask ^ ~mask) | (cur_mask & mask)) &
162 1.1 uch __intc_enabled_channel);
163 1.1 uch }
164 1.1 uch
165 1.1 uch void *
166 1.1 uch intc_intr_establish(enum intc_channel ch, int ipl, int (*func)(void *),
167 1.1 uch void *arg)
168 1.1 uch {
169 1.1 uch struct _ipl_dispatcher *dispatcher = &__intc_dispatcher[ch];
170 1.1 uch struct _ipl_dispatcher *d;
171 1.1 uch u_int32_t bit;
172 1.1 uch int i, s;
173 1.1 uch
174 1.1 uch KDASSERT(dispatcher->func == NULL);
175 1.1 uch
176 1.1 uch s = _intr_suspend();
177 1.1 uch dispatcher->func = func;
178 1.1 uch dispatcher->arg = arg;
179 1.1 uch dispatcher->ipl = ipl;
180 1.1 uch dispatcher->channel = ch;
181 1.1 uch dispatcher->bit = bit = (1 << ch);
182 1.1 uch
183 1.1 uch for (i = 0; i < _IPL_N; i++)
184 1.1 uch if (i < ipl)
185 1.1 uch __intc_ipl_holder[i].mask &= ~bit;
186 1.1 uch else
187 1.1 uch __intc_ipl_holder[i].mask |= bit;
188 1.1 uch
189 1.1 uch /* insert queue IPL order */
190 1.1 uch if (SLIST_EMPTY(&__intc_dispatcher_head)) {
191 1.1 uch SLIST_INSERT_HEAD(&__intc_dispatcher_head, dispatcher, link);
192 1.1 uch } else {
193 1.1 uch SLIST_FOREACH(d, &__intc_dispatcher_head, link) {
194 1.1 uch if (SLIST_NEXT(d, link) == 0 ||
195 1.1 uch SLIST_NEXT(d, link)->ipl < ipl) {
196 1.1 uch SLIST_INSERT_AFTER(d, dispatcher, link);
197 1.1 uch break;
198 1.1 uch }
199 1.1 uch }
200 1.1 uch }
201 1.1 uch
202 1.1 uch md_ipl_register(IPL_INTC, __intc_ipl_holder);
203 1.1 uch
204 1.1 uch intc_intr_enable(ch);
205 1.1 uch __intc_enabled_channel |= bit;
206 1.1 uch
207 1.1 uch _intr_resume(s);
208 1.1 uch
209 1.1 uch return ((void *)ch);
210 1.1 uch }
211 1.1 uch
212 1.1 uch void
213 1.1 uch intc_intr_disestablish(void *handle)
214 1.1 uch {
215 1.1 uch int ch = (int)(handle);
216 1.1 uch struct _ipl_dispatcher *dispatcher = &__intc_dispatcher[ch];
217 1.1 uch u_int32_t bit;
218 1.1 uch int i, s;
219 1.1 uch
220 1.1 uch s = _intr_suspend();
221 1.1 uch
222 1.1 uch intc_intr_disable(ch);
223 1.1 uch dispatcher->func = NULL;
224 1.1 uch
225 1.1 uch SLIST_REMOVE(&__intc_dispatcher_head, dispatcher,
226 1.1 uch _ipl_dispatcher, link);
227 1.1 uch
228 1.1 uch bit = dispatcher->bit;
229 1.1 uch for (i = 0; i < _IPL_N; i++)
230 1.1 uch __intc_ipl_holder[i].mask |= bit;
231 1.1 uch
232 1.1 uch md_ipl_register(IPL_INTC, __intc_ipl_holder);
233 1.1 uch __intc_enabled_channel &= ~bit;
234 1.1 uch
235 1.1 uch _intr_resume(s);
236 1.1 uch }
237 1.1 uch
238