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