xref: /netbsd-src/sys/arch/sparc/dev/pckbc_js.c (revision 23c8222edbfb0f0932d88a8351d3a0cf817dfb9e)
1 /*	$NetBSD: pckbc_js.c,v 1.12 2004/03/17 08:48:58 martin Exp $ */
2 
3 /*
4  * Copyright (c) 2002 Valeriy E. Ushakov
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. The name of the author may not be used to endorse or promote products
16  *    derived from this software without specific prior written permission
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #include <sys/cdefs.h>
31 __KERNEL_RCSID(0, "$NetBSD: pckbc_js.c,v 1.12 2004/03/17 08:48:58 martin Exp $");
32 
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/device.h>
37 #include <sys/malloc.h>
38 
39 #include <machine/autoconf.h>
40 #include <machine/bus.h>
41 #include <machine/intr.h>
42 
43 #include <dev/ic/i8042reg.h>
44 #include <dev/ic/pckbcvar.h>
45 #include <dev/pckbport/pckbportvar.h>
46 
47 #include <dev/ebus/ebusreg.h>
48 #include <dev/ebus/ebusvar.h>
49 
50 #ifdef __GENERIC_SOFT_INTERRUPTS_ALL_LEVELS
51 #define	IPL_JSCKBD	IPL_TTY
52 #else
53 #define	IPL_JSCKBD	IPL_SOFTSERIAL
54 #endif
55 
56 struct pckbc_js_softc {
57 	struct pckbc_softc jsc_pckbc;	/* real "pckbc" softc */
58 
59 	/* kbd and mouse share interrupt in both mr.coffee and krups */
60 	u_int32_t jsc_intr;
61 	int jsc_establised;
62 	void *jsc_int_cookie;
63 };
64 
65 
66 static int	pckbc_obio_match(struct device *, struct cfdata *, void *);
67 static void	pckbc_obio_attach(struct device *, struct device *, void *);
68 
69 static int	pckbc_ebus_match(struct device *, struct cfdata *, void *);
70 static void	pckbc_ebus_attach(struct device *, struct device *, void *);
71 
72 static void	pckbc_js_attach_common(	struct pckbc_js_softc *,
73 					bus_space_tag_t, bus_addr_t, int, int);
74 static void	pckbc_js_intr_establish(struct pckbc_softc *, pckbport_slot_t);
75 static int	jsc_pckbdintr(void *vsc);
76 
77 /* Mr.Coffee */
78 CFATTACH_DECL(pckbc_obio, sizeof(struct pckbc_js_softc),
79     pckbc_obio_match, pckbc_obio_attach, NULL, NULL);
80 
81 /* ms-IIep */
82 CFATTACH_DECL(pckbc_ebus, sizeof(struct pckbc_js_softc),
83     pckbc_ebus_match, pckbc_ebus_attach, NULL, NULL);
84 
85 #define PCKBC_PROM_DEVICE_NAME "8042"
86 
87 static int
88 pckbc_obio_match(parent, cf, aux)
89 	struct device *parent;
90 	struct cfdata *cf;
91 	void *aux;
92 {
93 	union obio_attach_args *uoba = aux;
94 	struct sbus_attach_args *sa = &uoba->uoba_sbus;
95 
96 	return (strcmp(sa->sa_name, PCKBC_PROM_DEVICE_NAME) == 0);
97 }
98 
99 static int
100 pckbc_ebus_match(parent, cf, aux)
101 	struct device *parent;
102 	struct cfdata *cf;
103 	void *aux;
104 {
105 	struct ebus_attach_args *ea = aux;
106 
107 	return (strcmp(ea->ea_name, PCKBC_PROM_DEVICE_NAME) == 0);
108 }
109 
110 
111 static void
112 pckbc_obio_attach(parent, self, aux)
113 	struct device *parent, *self;
114 	void *aux;
115 {
116 	struct pckbc_js_softc *jsc = (struct pckbc_js_softc *)self;
117 	union obio_attach_args *uoba = aux;
118 	struct sbus_attach_args *sa = &uoba->uoba_sbus;
119 	bus_space_tag_t iot;
120 	bus_addr_t ioaddr;
121 	int intr, isconsole;
122 
123 	iot = sa->sa_bustag;
124 	ioaddr = BUS_ADDR(sa->sa_slot, sa->sa_offset);
125 	intr = sa->sa_nintr ? sa->sa_pri : /* level */ 13;
126 
127 	/*
128 	 * TODO:
129 	 * . on OFW machines stdin is keyboard node, not 8042 node
130 	 * . on OBP machines 8042 node is faked by boot's prompatch
131 	 *   and PROM's stdin points to zs keyboard (add prom patch?)
132 	 * . we probably want the stdout node to control whether
133 	 *   console goes to serial
134 	 */
135 	isconsole = 1;
136 
137 	pckbc_js_attach_common(jsc, iot, ioaddr, intr, isconsole);
138 }
139 
140 static void
141 pckbc_ebus_attach(parent, self, aux)
142 	struct device *parent, *self;
143 	void *aux;
144 {
145 	struct pckbc_js_softc *jsc = (struct pckbc_js_softc *)self;
146 	struct ebus_attach_args *ea = aux;
147 	bus_space_tag_t iot;
148 	bus_addr_t ioaddr;
149 	int intr;
150 	int stdin_node,	node;
151 	int isconsole;
152 
153 	iot = ea->ea_bustag;
154 	ioaddr = EBUS_ADDR_FROM_REG(&ea->ea_reg[0]);
155 	intr = ea->ea_nintr ? ea->ea_intr[0] : /* line */ 0;
156 
157 	/* search children of "8042" node for stdin (keyboard) */
158 	stdin_node = prom_instance_to_package(prom_stdin());
159 	isconsole = 0;
160 
161 	for (node = prom_firstchild(ea->ea_node);
162 	     node != 0; node = prom_nextsibling(node))
163 		if (node == stdin_node) {
164 			isconsole = 1;
165 			break;
166 		}
167 
168 	pckbc_js_attach_common(jsc, iot, ioaddr, intr, isconsole);
169 }
170 
171 
172 static void
173 pckbc_js_attach_common(jsc, iot, ioaddr, intr, isconsole)
174 	struct pckbc_js_softc *jsc;
175 	bus_space_tag_t iot;
176 	bus_addr_t ioaddr;
177 	int intr;
178 	int isconsole;
179 {
180 	struct pckbc_softc *sc = (struct pckbc_softc *)jsc;
181 	struct pckbc_internal *t;
182 
183 	jsc->jsc_pckbc.intr_establish = pckbc_js_intr_establish;
184 	jsc->jsc_intr = intr;
185 	jsc->jsc_establised = 0;
186 
187 	if (isconsole) {
188 		int status;
189 
190 		status = pckbc_cnattach(iot, ioaddr, KBCMDP, PCKBC_KBD_SLOT);
191 		if (status == 0)
192 			printf(": cnattach ok");
193 		else
194 			printf(": cnattach %d", status);
195 	}
196 
197 	if (pckbc_is_console(iot, ioaddr)) {
198 		t = &pckbc_consdata;
199 		pckbc_console_attached = 1;
200 	} else {
201 		bus_space_handle_t ioh_d, ioh_c;
202 
203 		if (bus_space_map(iot, ioaddr + KBDATAP, 1, 0, &ioh_d) != 0) {
204 			printf(": unable to map data register\n");
205 			return;
206 		}
207 
208 		if (bus_space_map(iot, ioaddr + KBCMDP,  1, 0, &ioh_c) != 0) {
209 			bus_space_unmap(iot, ioh_d, 1);
210 			printf(": unable to map cmd register\n");
211 			return;
212 		}
213 
214 		t = malloc(sizeof(struct pckbc_internal), M_DEVBUF, M_WAITOK);
215 		memset(t, 0, sizeof(struct pckbc_internal));
216 		t->t_iot = iot;
217 		t->t_ioh_d = ioh_d;
218 		t->t_ioh_c = ioh_c;
219 		t->t_addr = ioaddr;
220 		t->t_cmdbyte = KC8_CPU; /* initial command: enable ports */
221 		callout_init(&t->t_cleanup);
222 
223 		(void) pckbc_poll_data1(t, PCKBC_KBD_SLOT); /* flush */
224 
225 		if (pckbc_send_cmd(iot, ioh_c, KBC_SELFTEST) == 0)
226 			printf(": unable to request self test");
227 		else {
228 			int response;
229 
230 			response = pckbc_poll_data1(t, PCKBC_KBD_SLOT);
231 			if (response == 0x55)
232 				printf(": selftest ok");
233 			else
234 				printf(": selftest failed (0x%02x)", response);
235 		}
236 	}
237 
238 	/* crosslink */
239 	t->t_sc = sc;
240 	sc->id = t;
241 
242 	/* finish off the attach */
243 	printf("\n");
244 	pckbc_attach(sc);
245 }
246 
247 
248 /*
249  * Keyboard and mouse share the interrupt
250  * so don't install interrupt handler twice.
251  */
252 static void
253 pckbc_js_intr_establish(sc, slot)
254 	struct pckbc_softc *sc;
255 	pckbport_slot_t slot;
256 {
257 	struct pckbc_js_softc *jsc = (struct pckbc_js_softc *)sc;
258 	void *res;
259 
260 	if (jsc->jsc_establised) {
261 #ifdef DEBUG
262 		printf("%s: %s slot shares interrupt (already established)\n",
263 		       sc->sc_dv.dv_xname, pckbc_slot_names[slot]);
264 #endif
265 		return;
266 	}
267 
268 	/*
269 	 * We can not choose the devic class interruptlevel freely,
270 	 * so we debounce via a softinterrupt.
271 	 */
272 	jsc->jsc_int_cookie = softintr_establish(IPL_JSCKBD,
273 	    pckbcintr_soft, &jsc->jsc_pckbc);
274 	if (jsc->jsc_int_cookie == NULL) {
275 		printf("%s: unable to establish %s soft interrupt\n",
276 		       sc->sc_dv.dv_xname, pckbc_slot_names[slot]);
277 		return;
278 	}
279 	res = bus_intr_establish(sc->id->t_iot, jsc->jsc_intr,
280 				 IPL_SERIAL, jsc_pckbdintr, jsc);
281 	if (res == NULL)
282 		printf("%s: unable to establish %s slot interrupt\n",
283 		       sc->sc_dv.dv_xname, pckbc_slot_names[slot]);
284 	else
285 		jsc->jsc_establised = 1;
286 }
287 
288 static int
289 jsc_pckbdintr(void *vsc)
290 {
291 	struct pckbc_js_softc *jsc = vsc;
292 
293 	softintr_schedule(jsc->jsc_int_cookie);
294 	pckbcintr_hard(&jsc->jsc_pckbc);
295 	/*
296 	 * This interrupt is not shared on javastations, avoid "stray"
297 	 * warnings. XXX - why do "stray interrupt" warnings happen if
298 	 * we don't claim the interrupt always?
299 	 */
300 	return 1;
301 }
302 
303 /*
304  * MD hook for use without MI wscons.
305  * Called by pckbc_cnattach().
306  */
307 int
308 pckbport_machdep_cnattach(constag, slot)
309     pckbport_tag_t constag;
310     pckbport_slot_t slot;
311 {
312 
313 	return (0);
314 }
315