sbus.c revision 1.1.6.2 1 1.1.6.2 thorpej /* $NetBSD: sbus.c,v 1.1.6.2 2002/01/10 19:47:29 thorpej Exp $ */
2 1.1.6.2 thorpej
3 1.1.6.2 thorpej /*-
4 1.1.6.2 thorpej * Copyright (c) 2001 The NetBSD Foundation, Inc.
5 1.1.6.2 thorpej * All rights reserved.
6 1.1.6.2 thorpej *
7 1.1.6.2 thorpej * This code is derived from software contributed to The NetBSD Foundation
8 1.1.6.2 thorpej * by UCHIYAMA Yasushi.
9 1.1.6.2 thorpej *
10 1.1.6.2 thorpej * Redistribution and use in source and binary forms, with or without
11 1.1.6.2 thorpej * modification, are permitted provided that the following conditions
12 1.1.6.2 thorpej * are met:
13 1.1.6.2 thorpej * 1. Redistributions of source code must retain the above copyright
14 1.1.6.2 thorpej * notice, this list of conditions and the following disclaimer.
15 1.1.6.2 thorpej * 2. Redistributions in binary form must reproduce the above copyright
16 1.1.6.2 thorpej * notice, this list of conditions and the following disclaimer in the
17 1.1.6.2 thorpej * documentation and/or other materials provided with the distribution.
18 1.1.6.2 thorpej * 3. All advertising materials mentioning features or use of this software
19 1.1.6.2 thorpej * must display the following acknowledgement:
20 1.1.6.2 thorpej * This product includes software developed by the NetBSD
21 1.1.6.2 thorpej * Foundation, Inc. and its contributors.
22 1.1.6.2 thorpej * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1.6.2 thorpej * contributors may be used to endorse or promote products derived
24 1.1.6.2 thorpej * from this software without specific prior written permission.
25 1.1.6.2 thorpej *
26 1.1.6.2 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1.6.2 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1.6.2 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1.6.2 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.1.6.2 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1.6.2 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1.6.2 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1.6.2 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1.6.2 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1.6.2 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1.6.2 thorpej * POSSIBILITY OF SUCH DAMAGE.
37 1.1.6.2 thorpej */
38 1.1.6.2 thorpej
39 1.1.6.2 thorpej /*
40 1.1.6.2 thorpej * PlayStation 2 internal PCMCIA/USB interface unit.
41 1.1.6.2 thorpej */
42 1.1.6.2 thorpej
43 1.1.6.2 thorpej #include <sys/param.h>
44 1.1.6.2 thorpej #include <sys/systm.h>
45 1.1.6.2 thorpej
46 1.1.6.2 thorpej #include <machine/bootinfo.h>
47 1.1.6.2 thorpej #include <machine/autoconf.h>
48 1.1.6.2 thorpej
49 1.1.6.2 thorpej #include <playstation2/playstation2/interrupt.h>
50 1.1.6.2 thorpej
51 1.1.6.2 thorpej #include <playstation2/ee/eevar.h>
52 1.1.6.2 thorpej #include <playstation2/ee/intcvar.h>
53 1.1.6.2 thorpej #include <playstation2/dev/sbusvar.h>
54 1.1.6.2 thorpej #include <playstation2/dev/sbusreg.h>
55 1.1.6.2 thorpej
56 1.1.6.2 thorpej #ifdef DEBUG
57 1.1.6.2 thorpej #define STATIC
58 1.1.6.2 thorpej #else
59 1.1.6.2 thorpej #define STATIC static
60 1.1.6.2 thorpej #endif
61 1.1.6.2 thorpej
62 1.1.6.2 thorpej STATIC void sbus_type2_pcmcia_intr_clear(void);
63 1.1.6.2 thorpej STATIC void sbus_type2_pcmcia_intr_enable(void);
64 1.1.6.2 thorpej STATIC void sbus_type2_pcmcia_intr_disable(void);
65 1.1.6.2 thorpej STATIC void sbus_type2_pcmcia_intr_reinstall(void);
66 1.1.6.2 thorpej STATIC void sbus_type3_pcmcia_intr_clear(void);
67 1.1.6.2 thorpej STATIC void sbus_type3_pcmcia_intr_enable(void);
68 1.1.6.2 thorpej STATIC void sbus_type3_pcmcia_intr_disable(void);
69 1.1.6.2 thorpej STATIC void sbus_type3_pcmcia_intr_reinstall(void);
70 1.1.6.2 thorpej STATIC int sbus_spurious_intr(void *);
71 1.1.6.2 thorpej
72 1.1.6.2 thorpej STATIC void (*sbus_pcmcia_intr_clear)(void);
73 1.1.6.2 thorpej STATIC void (*sbus_pcmcia_intr_enable)(void);
74 1.1.6.2 thorpej STATIC void (*sbus_pcmcia_intr_disable)(void);
75 1.1.6.2 thorpej STATIC void (*sbus_pcmcia_intr_reinstall)(void);
76 1.1.6.2 thorpej
77 1.1.6.2 thorpej STATIC int (*sbus_pcmcia_intr)(void *) = sbus_spurious_intr;
78 1.1.6.2 thorpej STATIC void *sbus_pcmcia_context;
79 1.1.6.2 thorpej STATIC int (*sbus_usb_intr)(void *) = sbus_spurious_intr;
80 1.1.6.2 thorpej STATIC void *sbus_usb_context;
81 1.1.6.2 thorpej
82 1.1.6.2 thorpej STATIC void sbus_init(int);
83 1.1.6.2 thorpej STATIC int sbus_intr(void *);
84 1.1.6.2 thorpej
85 1.1.6.2 thorpej STATIC int sbus_match(struct device *, struct cfdata *, void *);
86 1.1.6.2 thorpej STATIC void sbus_attach(struct device *, struct device *, void *);
87 1.1.6.2 thorpej STATIC int sbus_search(struct device *, struct cfdata *, void *);
88 1.1.6.2 thorpej STATIC int sbus_print(void *, const char *);
89 1.1.6.2 thorpej
90 1.1.6.2 thorpej struct cfattach sbus_ca = {
91 1.1.6.2 thorpej sizeof (struct device), sbus_match, sbus_attach
92 1.1.6.2 thorpej };
93 1.1.6.2 thorpej
94 1.1.6.2 thorpej extern struct cfdriver sbus_cd;
95 1.1.6.2 thorpej STATIC int __sbus_attached;
96 1.1.6.2 thorpej
97 1.1.6.2 thorpej int
98 1.1.6.2 thorpej sbus_match(struct device *parent, struct cfdata *cf, void *aux)
99 1.1.6.2 thorpej {
100 1.1.6.2 thorpej struct mainbus_attach_args *ma = aux;
101 1.1.6.2 thorpej
102 1.1.6.2 thorpej if (strcmp(ma->ma_name, sbus_cd.cd_name) != 0)
103 1.1.6.2 thorpej return (0);
104 1.1.6.2 thorpej
105 1.1.6.2 thorpej return (!__sbus_attached);
106 1.1.6.2 thorpej }
107 1.1.6.2 thorpej
108 1.1.6.2 thorpej void
109 1.1.6.2 thorpej sbus_attach(struct device *parent, struct device *self, void *aux)
110 1.1.6.2 thorpej {
111 1.1.6.2 thorpej int type = BOOTINFO_REF(BOOTINFO_PCMCIA_TYPE);
112 1.1.6.2 thorpej
113 1.1.6.2 thorpej printf(": controller type %d\n", type);
114 1.1.6.2 thorpej
115 1.1.6.2 thorpej /* Initialize SBUS controller */
116 1.1.6.2 thorpej sbus_init(type);
117 1.1.6.2 thorpej
118 1.1.6.2 thorpej config_search(sbus_search, self, 0);
119 1.1.6.2 thorpej }
120 1.1.6.2 thorpej
121 1.1.6.2 thorpej int
122 1.1.6.2 thorpej sbus_search(struct device *parent, struct cfdata *cf, void *aux)
123 1.1.6.2 thorpej {
124 1.1.6.2 thorpej struct sbus_attach_args sa;
125 1.1.6.2 thorpej
126 1.1.6.2 thorpej if ((*cf->cf_attach->ca_match)(parent, cf, &sa))
127 1.1.6.2 thorpej config_attach(parent, cf, &sa, sbus_print);
128 1.1.6.2 thorpej
129 1.1.6.2 thorpej return (0);
130 1.1.6.2 thorpej }
131 1.1.6.2 thorpej
132 1.1.6.2 thorpej int
133 1.1.6.2 thorpej sbus_print(void *aux, const char *pnp)
134 1.1.6.2 thorpej {
135 1.1.6.2 thorpej
136 1.1.6.2 thorpej return (pnp ? QUIET : UNCONF);
137 1.1.6.2 thorpej }
138 1.1.6.2 thorpej
139 1.1.6.2 thorpej void
140 1.1.6.2 thorpej sbus_init(int type)
141 1.1.6.2 thorpej {
142 1.1.6.2 thorpej /* install model dependent hook */
143 1.1.6.2 thorpej #define SET_PCMCIA_INTR_OPS(x) \
144 1.1.6.2 thorpej sbus_pcmcia_intr_clear = sbus_type##x##_pcmcia_intr_clear; \
145 1.1.6.2 thorpej sbus_pcmcia_intr_enable = sbus_type##x##_pcmcia_intr_enable; \
146 1.1.6.2 thorpej sbus_pcmcia_intr_disable = sbus_type##x##_pcmcia_intr_disable; \
147 1.1.6.2 thorpej sbus_pcmcia_intr_reinstall = sbus_type##x##_pcmcia_intr_reinstall
148 1.1.6.2 thorpej
149 1.1.6.2 thorpej switch (type) {
150 1.1.6.2 thorpej default:
151 1.1.6.2 thorpej panic("unknown pcmcia controller type = %d", type);
152 1.1.6.2 thorpej break;
153 1.1.6.2 thorpej case 0:
154 1.1.6.2 thorpej /* FALLTHROUGH */
155 1.1.6.2 thorpej case 1:
156 1.1.6.2 thorpej /* FALLTHROUGH */
157 1.1.6.2 thorpej case 2:
158 1.1.6.2 thorpej SET_PCMCIA_INTR_OPS(2);
159 1.1.6.2 thorpej break;
160 1.1.6.2 thorpej case 3:
161 1.1.6.2 thorpej SET_PCMCIA_INTR_OPS(3);
162 1.1.6.2 thorpej break;
163 1.1.6.2 thorpej }
164 1.1.6.2 thorpej #undef SET_PCMCIA_INTR_OPS
165 1.1.6.2 thorpej /* disable interrupt */
166 1.1.6.2 thorpej (*sbus_pcmcia_intr_disable)();
167 1.1.6.2 thorpej
168 1.1.6.2 thorpej /* clear interrupt */
169 1.1.6.2 thorpej (*sbus_pcmcia_intr_clear)();
170 1.1.6.2 thorpej _reg_write_4(SBUS_SMFLG_REG, SMFLG_PCMCIA_INT);
171 1.1.6.2 thorpej _reg_write_4(SBUS_SMFLG_REG, SMFLG_USB_INT);
172 1.1.6.2 thorpej
173 1.1.6.2 thorpej /* connect to INTC */
174 1.1.6.2 thorpej intc_intr_establish(I_CH1_SBUS, IPL_BIO, sbus_intr, 0);
175 1.1.6.2 thorpej }
176 1.1.6.2 thorpej
177 1.1.6.2 thorpej void *
178 1.1.6.2 thorpej sbus_intr_establish(enum sbus_irq irq, int (*ih_func)(void *), void *ih_arg)
179 1.1.6.2 thorpej {
180 1.1.6.2 thorpej switch (irq) {
181 1.1.6.2 thorpej default:
182 1.1.6.2 thorpej panic("unknown IRQ\n");
183 1.1.6.2 thorpej break;
184 1.1.6.2 thorpej case SBUS_IRQ_PCMCIA:
185 1.1.6.2 thorpej sbus_pcmcia_intr = ih_func;
186 1.1.6.2 thorpej sbus_pcmcia_context = ih_arg;
187 1.1.6.2 thorpej (*sbus_pcmcia_intr_enable)();
188 1.1.6.2 thorpej break;
189 1.1.6.2 thorpej case SBUS_IRQ_USB:
190 1.1.6.2 thorpej sbus_usb_intr = ih_func;
191 1.1.6.2 thorpej sbus_usb_context = ih_arg;
192 1.1.6.2 thorpej break;
193 1.1.6.2 thorpej }
194 1.1.6.2 thorpej
195 1.1.6.2 thorpej return (void *)irq;
196 1.1.6.2 thorpej }
197 1.1.6.2 thorpej
198 1.1.6.2 thorpej void
199 1.1.6.2 thorpej sbus_intr_disestablish(void *handle)
200 1.1.6.2 thorpej {
201 1.1.6.2 thorpej int irq = (int)handle;
202 1.1.6.2 thorpej
203 1.1.6.2 thorpej switch (irq) {
204 1.1.6.2 thorpej default:
205 1.1.6.2 thorpej panic("unknown IRQ\n");
206 1.1.6.2 thorpej break;
207 1.1.6.2 thorpej case SBUS_IRQ_PCMCIA:
208 1.1.6.2 thorpej sbus_pcmcia_intr = sbus_spurious_intr;
209 1.1.6.2 thorpej (*sbus_pcmcia_intr_disable)();
210 1.1.6.2 thorpej break;
211 1.1.6.2 thorpej case SBUS_IRQ_USB:
212 1.1.6.2 thorpej sbus_usb_intr = sbus_spurious_intr;
213 1.1.6.2 thorpej break;
214 1.1.6.2 thorpej }
215 1.1.6.2 thorpej }
216 1.1.6.2 thorpej
217 1.1.6.2 thorpej int
218 1.1.6.2 thorpej sbus_intr(void *arg)
219 1.1.6.2 thorpej {
220 1.1.6.2 thorpej u_int32_t stat;
221 1.1.6.2 thorpej
222 1.1.6.2 thorpej _playstation2_evcnt.sbus.ev_count++;
223 1.1.6.2 thorpej stat = _reg_read_4(SBUS_SMFLG_REG);
224 1.1.6.2 thorpej
225 1.1.6.2 thorpej if (stat & SMFLG_PCMCIA_INT) {
226 1.1.6.2 thorpej (*sbus_pcmcia_intr_clear)();
227 1.1.6.2 thorpej _reg_write_4(SBUS_SMFLG_REG, SMFLG_PCMCIA_INT);
228 1.1.6.2 thorpej (*sbus_pcmcia_intr)(sbus_pcmcia_context);
229 1.1.6.2 thorpej }
230 1.1.6.2 thorpej
231 1.1.6.2 thorpej if (stat & SMFLG_USB_INT) {
232 1.1.6.2 thorpej _reg_write_4(SBUS_SMFLG_REG, SMFLG_USB_INT);
233 1.1.6.2 thorpej (*sbus_usb_intr)(sbus_usb_context);
234 1.1.6.2 thorpej }
235 1.1.6.2 thorpej
236 1.1.6.2 thorpej (*sbus_pcmcia_intr_reinstall)();
237 1.1.6.2 thorpej
238 1.1.6.2 thorpej return (1);
239 1.1.6.2 thorpej }
240 1.1.6.2 thorpej
241 1.1.6.2 thorpej int
242 1.1.6.2 thorpej sbus_spurious_intr(void *arg)
243 1.1.6.2 thorpej {
244 1.1.6.2 thorpej
245 1.1.6.2 thorpej printf("spurious interrupt.\n");
246 1.1.6.2 thorpej
247 1.1.6.2 thorpej return (1);
248 1.1.6.2 thorpej }
249 1.1.6.2 thorpej
250 1.1.6.2 thorpej /* SCPH-18000 */
251 1.1.6.2 thorpej void
252 1.1.6.2 thorpej sbus_type2_pcmcia_intr_clear()
253 1.1.6.2 thorpej {
254 1.1.6.2 thorpej
255 1.1.6.2 thorpej if (_reg_read_2(SBUS_PCMCIA_CSC1_REG16) & 0x080)
256 1.1.6.2 thorpej _reg_write_2(SBUS_PCMCIA_CSC1_REG16, 0xffff);
257 1.1.6.2 thorpej }
258 1.1.6.2 thorpej
259 1.1.6.2 thorpej void
260 1.1.6.2 thorpej sbus_type2_pcmcia_intr_enable()
261 1.1.6.2 thorpej {
262 1.1.6.2 thorpej
263 1.1.6.2 thorpej _reg_write_2(SBUS_PCMCIA_TIMR_REG16, 0);
264 1.1.6.2 thorpej }
265 1.1.6.2 thorpej
266 1.1.6.2 thorpej void
267 1.1.6.2 thorpej sbus_type2_pcmcia_intr_disable()
268 1.1.6.2 thorpej {
269 1.1.6.2 thorpej
270 1.1.6.2 thorpej _reg_write_2(SBUS_PCMCIA_TIMR_REG16, 1);
271 1.1.6.2 thorpej }
272 1.1.6.2 thorpej
273 1.1.6.2 thorpej void
274 1.1.6.2 thorpej sbus_type2_pcmcia_intr_reinstall()
275 1.1.6.2 thorpej {
276 1.1.6.2 thorpej u_int16_t r = _reg_read_2(SBUS_PCMCIA_TIMR_REG16);
277 1.1.6.2 thorpej
278 1.1.6.2 thorpej _reg_write_2(SBUS_PCMCIA_TIMR_REG16, 1);
279 1.1.6.2 thorpej _reg_write_2(SBUS_PCMCIA_TIMR_REG16, r);
280 1.1.6.2 thorpej }
281 1.1.6.2 thorpej
282 1.1.6.2 thorpej /* SCPH-30000/35000 */
283 1.1.6.2 thorpej void
284 1.1.6.2 thorpej sbus_type3_pcmcia_intr_clear()
285 1.1.6.2 thorpej {
286 1.1.6.2 thorpej /* nothing */
287 1.1.6.2 thorpej }
288 1.1.6.2 thorpej
289 1.1.6.2 thorpej void
290 1.1.6.2 thorpej sbus_type3_pcmcia_intr_enable()
291 1.1.6.2 thorpej {
292 1.1.6.2 thorpej
293 1.1.6.2 thorpej _reg_write_2(SBUS_PCMCIA3_TIMR_REG16, 0);
294 1.1.6.2 thorpej }
295 1.1.6.2 thorpej
296 1.1.6.2 thorpej void
297 1.1.6.2 thorpej sbus_type3_pcmcia_intr_disable()
298 1.1.6.2 thorpej {
299 1.1.6.2 thorpej
300 1.1.6.2 thorpej _reg_write_2(SBUS_PCMCIA3_TIMR_REG16, 1);
301 1.1.6.2 thorpej }
302 1.1.6.2 thorpej
303 1.1.6.2 thorpej void
304 1.1.6.2 thorpej sbus_type3_pcmcia_intr_reinstall()
305 1.1.6.2 thorpej {
306 1.1.6.2 thorpej u_int16_t r = _reg_read_2(SBUS_PCMCIA3_TIMR_REG16);
307 1.1.6.2 thorpej
308 1.1.6.2 thorpej _reg_write_2(SBUS_PCMCIA3_TIMR_REG16, 1);
309 1.1.6.2 thorpej _reg_write_2(SBUS_PCMCIA3_TIMR_REG16, r);
310 1.1.6.2 thorpej }
311