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