intc.c revision 1.1 1 1.1 uch /* $NetBSD: intc.c,v 1.1 2001/10/16 15:38:38 uch 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.1 uch
39 1.1 uch #include "debug_playstation2.h"
40 1.1 uch
41 1.1 uch #include <sys/param.h>
42 1.1 uch #include <sys/systm.h>
43 1.1 uch
44 1.1 uch #include <playstation2/ee/eevar.h>
45 1.1 uch #include <playstation2/ee/intcvar.h>
46 1.1 uch #include <playstation2/ee/intcreg.h>
47 1.1 uch #include <playstation2/ee/gsvar.h> /* debug monitor */
48 1.1 uch
49 1.1 uch #include <playstation2/playstation2/interrupt.h>
50 1.1 uch
51 1.1 uch #ifdef DEBUG
52 1.1 uch #define LEGAL_CHANNEL(x) ((x) >= 0 && (x) <= 15)
53 1.1 uch #define STATIC
54 1.1 uch #else
55 1.1 uch #define STATIC static
56 1.1 uch #endif
57 1.1 uch
58 1.1 uch #define _INTC_NINTR 16
59 1.1 uch
60 1.1 uch u_int32_t __intc_enabled_channel;
61 1.1 uch
62 1.1 uch STATIC int __intc_initialized;
63 1.1 uch STATIC struct _ipl_dispatcher __intc_dispatcher[_INTC_NINTR];
64 1.1 uch STATIC struct _ipl_holder __intc_ipl_holder[_IPL_N];
65 1.1 uch
66 1.1 uch STATIC SLIST_HEAD(, _ipl_dispatcher) __intc_dispatcher_head =
67 1.1 uch SLIST_HEAD_INITIALIZER(__intc_dispatcher_head);
68 1.1 uch
69 1.1 uch void
70 1.1 uch intc_init()
71 1.1 uch {
72 1.1 uch int i;
73 1.1 uch
74 1.1 uch if (__intc_initialized++)
75 1.1 uch return;
76 1.1 uch
77 1.1 uch /* disable all channel */
78 1.1 uch for (i = 0; i < _INTC_NINTR; i++)
79 1.1 uch intc_intr_disable(i);
80 1.1 uch
81 1.1 uch /* clear interrupts */
82 1.1 uch _reg_write_4(I_STAT_REG, _reg_read_4(I_STAT_REG));
83 1.1 uch
84 1.1 uch for (i = 0; i < _IPL_N; i++)
85 1.1 uch __intc_ipl_holder[i].mask = 0xffffffff;
86 1.1 uch }
87 1.1 uch
88 1.1 uch int
89 1.1 uch intc_intr(u_int32_t mask)
90 1.1 uch {
91 1.1 uch struct _ipl_dispatcher *dispatcher;
92 1.1 uch u_int32_t r, dispatch, pending;
93 1.1 uch
94 1.1 uch r = _reg_read_4(I_STAT_REG);
95 1.1 uch dispatch = r & ~mask & __intc_enabled_channel;
96 1.1 uch pending = r & mask & __intc_enabled_channel;
97 1.1 uch
98 1.1 uch #if 0
99 1.1 uch __gsfb_print(1,
100 1.1 uch "INTC stat=%08x, mask=%08x, pend=%08x, disp=%08x enable=%08x\n",
101 1.1 uch r, mask, pending, dispatch, __intc_enabled_channel);
102 1.1 uch #endif
103 1.1 uch if (dispatch == 0)
104 1.1 uch return (pending == 0 ? 1 : 0);
105 1.1 uch
106 1.1 uch /* clear interrupt */
107 1.1 uch _reg_write_4(I_STAT_REG, dispatch);
108 1.1 uch
109 1.1 uch /* dispatch interrupt handler */
110 1.1 uch SLIST_FOREACH(dispatcher, &__intc_dispatcher_head, link) {
111 1.1 uch if (dispatcher->bit & dispatch) {
112 1.1 uch KDASSERT(dispatcher->func);
113 1.1 uch (*dispatcher->func)(dispatcher->arg);
114 1.1 uch dispatch &= ~dispatcher->bit;
115 1.1 uch }
116 1.1 uch }
117 1.1 uch
118 1.1 uch /* disable spurious interrupt source */
119 1.1 uch if (dispatch) {
120 1.1 uch int i, bit;
121 1.1 uch for (i = 0, bit = 1; i < _INTC_NINTR; i++, bit <<= 1) {
122 1.1 uch if (bit & dispatch) {
123 1.1 uch intc_intr_disable(i);
124 1.1 uch printf("%s: spurious interrupt %d disabled.\n",
125 1.1 uch __FUNCTION__, i);
126 1.1 uch }
127 1.1 uch }
128 1.1 uch }
129 1.1 uch
130 1.1 uch return (pending == 0 ? 1 : 0);
131 1.1 uch }
132 1.1 uch
133 1.1 uch void
134 1.1 uch intc_intr_enable(enum intc_channel ch)
135 1.1 uch {
136 1.1 uch u_int32_t mask;
137 1.1 uch
138 1.1 uch KDASSERT(LEGAL_CHANNEL(ch));
139 1.1 uch mask = 1 << ch;
140 1.1 uch _reg_write_4(I_MASK_REG, (_reg_read_4(I_MASK_REG) & mask) ^ mask);
141 1.1 uch }
142 1.1 uch
143 1.1 uch void
144 1.1 uch intc_intr_disable(enum intc_channel ch)
145 1.1 uch {
146 1.1 uch
147 1.1 uch KDASSERT(LEGAL_CHANNEL(ch));
148 1.1 uch _reg_write_4(I_MASK_REG, _reg_read_4(I_MASK_REG) & (1 << ch));
149 1.1 uch }
150 1.1 uch
151 1.1 uch void
152 1.1 uch intc_update_mask(u_int32_t mask)
153 1.1 uch {
154 1.1 uch u_int32_t cur_mask;
155 1.1 uch
156 1.1 uch cur_mask = _reg_read_4(I_MASK_REG);
157 1.1 uch
158 1.1 uch _reg_write_4(I_MASK_REG, ((cur_mask ^ ~mask) | (cur_mask & mask)) &
159 1.1 uch __intc_enabled_channel);
160 1.1 uch }
161 1.1 uch
162 1.1 uch void *
163 1.1 uch intc_intr_establish(enum intc_channel ch, int ipl, int (*func)(void *),
164 1.1 uch void *arg)
165 1.1 uch {
166 1.1 uch struct _ipl_dispatcher *dispatcher = &__intc_dispatcher[ch];
167 1.1 uch struct _ipl_dispatcher *d;
168 1.1 uch u_int32_t bit;
169 1.1 uch int i, s;
170 1.1 uch
171 1.1 uch KDASSERT(dispatcher->func == NULL);
172 1.1 uch
173 1.1 uch s = _intr_suspend();
174 1.1 uch dispatcher->func = func;
175 1.1 uch dispatcher->arg = arg;
176 1.1 uch dispatcher->ipl = ipl;
177 1.1 uch dispatcher->channel = ch;
178 1.1 uch dispatcher->bit = bit = (1 << ch);
179 1.1 uch
180 1.1 uch for (i = 0; i < _IPL_N; i++)
181 1.1 uch if (i < ipl)
182 1.1 uch __intc_ipl_holder[i].mask &= ~bit;
183 1.1 uch else
184 1.1 uch __intc_ipl_holder[i].mask |= bit;
185 1.1 uch
186 1.1 uch /* insert queue IPL order */
187 1.1 uch if (SLIST_EMPTY(&__intc_dispatcher_head)) {
188 1.1 uch SLIST_INSERT_HEAD(&__intc_dispatcher_head, dispatcher, link);
189 1.1 uch } else {
190 1.1 uch SLIST_FOREACH(d, &__intc_dispatcher_head, link) {
191 1.1 uch if (SLIST_NEXT(d, link) == 0 ||
192 1.1 uch SLIST_NEXT(d, link)->ipl < ipl) {
193 1.1 uch SLIST_INSERT_AFTER(d, dispatcher, link);
194 1.1 uch break;
195 1.1 uch }
196 1.1 uch }
197 1.1 uch }
198 1.1 uch
199 1.1 uch md_ipl_register(IPL_INTC, __intc_ipl_holder);
200 1.1 uch
201 1.1 uch intc_intr_enable(ch);
202 1.1 uch __intc_enabled_channel |= bit;
203 1.1 uch
204 1.1 uch _intr_resume(s);
205 1.1 uch
206 1.1 uch return ((void *)ch);
207 1.1 uch }
208 1.1 uch
209 1.1 uch void
210 1.1 uch intc_intr_disestablish(void *handle)
211 1.1 uch {
212 1.1 uch int ch = (int)(handle);
213 1.1 uch struct _ipl_dispatcher *dispatcher = &__intc_dispatcher[ch];
214 1.1 uch u_int32_t bit;
215 1.1 uch int i, s;
216 1.1 uch
217 1.1 uch s = _intr_suspend();
218 1.1 uch
219 1.1 uch intc_intr_disable(ch);
220 1.1 uch dispatcher->func = NULL;
221 1.1 uch
222 1.1 uch SLIST_REMOVE(&__intc_dispatcher_head, dispatcher,
223 1.1 uch _ipl_dispatcher, link);
224 1.1 uch
225 1.1 uch bit = dispatcher->bit;
226 1.1 uch for (i = 0; i < _IPL_N; i++)
227 1.1 uch __intc_ipl_holder[i].mask |= bit;
228 1.1 uch
229 1.1 uch md_ipl_register(IPL_INTC, __intc_ipl_holder);
230 1.1 uch __intc_enabled_channel &= ~bit;
231 1.1 uch
232 1.1 uch _intr_resume(s);
233 1.1 uch }
234 1.1 uch
235