xref: /netbsd-src/sys/arch/playstation2/dev/sbus.c (revision bdc22b2e01993381dcefeff2bc9b56ca75a4235c)
1 /*	$NetBSD: sbus.c,v 1.17 2016/07/19 17:04:25 maya 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.17 2016/07/19 17:04:25 maya 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, sizeof (struct device),
89     sbus_match, sbus_attach, NULL, NULL);
90 
91 extern struct cfdriver sbus_cd;
92 STATIC int __sbus_attached;
93 
94 int
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
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_ia(sbus_search, self, "sbus", 0);
116 }
117 
118 int
119 sbus_search(device_t parent, cfdata_t cf,
120 	    const int *ldesc, void *aux)
121 {
122 	struct sbus_attach_args sa;
123 
124 	if (config_match(parent, cf, &sa))
125 		config_attach(parent, cf, &sa, sbus_print);
126 
127 	return (0);
128 }
129 
130 int
131 sbus_print(void *aux, const char *pnp)
132 {
133 
134 	return (pnp ? QUIET : UNCONF);
135 }
136 
137 void
138 sbus_init(int type)
139 {
140 	/* install model dependent hook */
141 #define SET_PCMCIA_INTR_OPS(x)	 					\
142 	sbus_pcmcia_intr_clear = sbus_type##x##_pcmcia_intr_clear;	\
143 	sbus_pcmcia_intr_enable = sbus_type##x##_pcmcia_intr_enable;	\
144 	sbus_pcmcia_intr_disable = sbus_type##x##_pcmcia_intr_disable;	\
145 	sbus_pcmcia_intr_reinstall = sbus_type##x##_pcmcia_intr_reinstall
146 
147 	switch (type) {
148 	default:
149 		panic("unknown pcmcia controller type = %d", type);
150 		break;
151 	case 0:
152 		/* FALLTHROUGH */
153 	case 1:
154 		/* FALLTHROUGH */
155 	case 2:
156 		SET_PCMCIA_INTR_OPS(2);
157 		break;
158 	case 3:
159 		SET_PCMCIA_INTR_OPS(3);
160 		break;
161 	}
162 #undef SET_PCMCIA_INTR_OPS
163 	/* disable interrupt */
164 	(*sbus_pcmcia_intr_disable)();
165 
166 	/* clear interrupt */
167 	(*sbus_pcmcia_intr_clear)();
168 	_reg_write_4(SBUS_SMFLG_REG, SMFLG_PCMCIA_INT);
169 	_reg_write_4(SBUS_SMFLG_REG, SMFLG_USB_INT);
170 
171 	/* connect to INTC */
172 	intc_intr_establish(I_CH1_SBUS, IPL_BIO, sbus_intr, 0);
173 }
174 
175 void *
176 sbus_intr_establish(enum sbus_irq irq, int (*ih_func)(void *), void *ih_arg)
177 {
178 	switch (irq) {
179 	default:
180 		panic("unknown IRQ");
181 		break;
182 	case SBUS_IRQ_PCMCIA:
183 		sbus_pcmcia_intr = ih_func;
184 		sbus_pcmcia_context = ih_arg;
185 		(*sbus_pcmcia_intr_enable)();
186 		break;
187 	case SBUS_IRQ_USB:
188 		sbus_usb_intr = ih_func;
189 		sbus_usb_context = ih_arg;
190 		break;
191 	}
192 
193 	return (void *)irq;
194 }
195 
196 void
197 sbus_intr_disestablish(void *handle)
198 {
199 	int irq = (int)handle;
200 
201 	switch (irq) {
202 	default:
203 		panic("unknown IRQ");
204 		break;
205 	case SBUS_IRQ_PCMCIA:
206 		sbus_pcmcia_intr = sbus_spurious_intr;
207 		(*sbus_pcmcia_intr_disable)();
208 		break;
209 	case SBUS_IRQ_USB:
210 		sbus_usb_intr = sbus_spurious_intr;
211 		break;
212 	}
213 }
214 
215 int
216 sbus_intr(void *arg)
217 {
218 	u_int32_t stat;
219 
220 	_playstation2_evcnt.sbus.ev_count++;
221 	stat = _reg_read_4(SBUS_SMFLG_REG);
222 
223 	if (stat & SMFLG_PCMCIA_INT) {
224 		(*sbus_pcmcia_intr_clear)();
225 		_reg_write_4(SBUS_SMFLG_REG, SMFLG_PCMCIA_INT);
226 		(*sbus_pcmcia_intr)(sbus_pcmcia_context);
227 	}
228 
229 	if (stat & SMFLG_USB_INT) {
230 		_reg_write_4(SBUS_SMFLG_REG, SMFLG_USB_INT);
231 		(*sbus_usb_intr)(sbus_usb_context);
232 	}
233 
234 	(*sbus_pcmcia_intr_reinstall)();
235 
236 	return (1);
237 }
238 
239 int
240 sbus_spurious_intr(void *arg)
241 {
242 
243 	printf("spurious interrupt.\n");
244 
245 	return (1);
246 }
247 
248 /* SCPH-18000 */
249 void
250 sbus_type2_pcmcia_intr_clear(void)
251 {
252 
253 	if (_reg_read_2(SBUS_PCMCIA_CSC1_REG16) & 0x080)
254 		_reg_write_2(SBUS_PCMCIA_CSC1_REG16, 0xffff);
255 }
256 
257 void
258 sbus_type2_pcmcia_intr_enable(void)
259 {
260 
261 	_reg_write_2(SBUS_PCMCIA_TIMR_REG16, 0);
262 }
263 
264 void
265 sbus_type2_pcmcia_intr_disable(void)
266 {
267 
268 	_reg_write_2(SBUS_PCMCIA_TIMR_REG16, 1);
269 }
270 
271 void
272 sbus_type2_pcmcia_intr_reinstall(void)
273 {
274 	u_int16_t r = _reg_read_2(SBUS_PCMCIA_TIMR_REG16);
275 
276 	_reg_write_2(SBUS_PCMCIA_TIMR_REG16, 1);
277 	_reg_write_2(SBUS_PCMCIA_TIMR_REG16, r);
278 }
279 
280 /* SCPH-30000/35000 */
281 void
282 sbus_type3_pcmcia_intr_clear(void)
283 {
284 	/* nothing */
285 }
286 
287 void
288 sbus_type3_pcmcia_intr_enable(void)
289 {
290 
291 	_reg_write_2(SBUS_PCMCIA3_TIMR_REG16, 0);
292 }
293 
294 void
295 sbus_type3_pcmcia_intr_disable(void)
296 {
297 
298 	_reg_write_2(SBUS_PCMCIA3_TIMR_REG16, 1);
299 }
300 
301 void
302 sbus_type3_pcmcia_intr_reinstall(void)
303 {
304 	u_int16_t r = _reg_read_2(SBUS_PCMCIA3_TIMR_REG16);
305 
306 	_reg_write_2(SBUS_PCMCIA3_TIMR_REG16, 1);
307 	_reg_write_2(SBUS_PCMCIA3_TIMR_REG16, r);
308 }
309