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