xref: /netbsd-src/sys/arch/sparc/dev/pckbc_js.c (revision d974db0adae7638e51d0b459a9c2efece6ded7e4)
1 /*	$NetBSD: pckbc_js.c,v 1.15 2007/10/17 19:57:12 garbled 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.15 2007/10/17 19:57:12 garbled 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 	uint32_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 *);
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(struct device *parent, struct cfdata *cf, void *aux)
89 {
90 	union obio_attach_args *uoba = aux;
91 	struct sbus_attach_args *sa = &uoba->uoba_sbus;
92 
93 	return (strcmp(sa->sa_name, PCKBC_PROM_DEVICE_NAME) == 0);
94 }
95 
96 static int
97 pckbc_ebus_match(struct device *parent, struct cfdata *cf, void *aux)
98 {
99 	struct ebus_attach_args *ea = aux;
100 
101 	return (strcmp(ea->ea_name, PCKBC_PROM_DEVICE_NAME) == 0);
102 }
103 
104 
105 static void
106 pckbc_obio_attach(struct device *parent, struct device *self, void *aux)
107 {
108 	struct pckbc_js_softc *jsc = (struct pckbc_js_softc *)self;
109 	union obio_attach_args *uoba = aux;
110 	struct sbus_attach_args *sa = &uoba->uoba_sbus;
111 	bus_space_tag_t iot;
112 	bus_addr_t ioaddr;
113 	int intr, isconsole;
114 
115 	iot = sa->sa_bustag;
116 	ioaddr = BUS_ADDR(sa->sa_slot, sa->sa_offset);
117 	intr = sa->sa_nintr ? sa->sa_pri : /* level */ 13;
118 
119 	/*
120 	 * TODO:
121 	 * . on OFW machines stdin is keyboard node, not 8042 node
122 	 * . on OBP machines 8042 node is faked by boot's prompatch
123 	 *   and PROM's stdin points to zs keyboard (add prom patch?)
124 	 * . we probably want the stdout node to control whether
125 	 *   console goes to serial
126 	 */
127 	isconsole = 1;
128 
129 	pckbc_js_attach_common(jsc, iot, ioaddr, intr, isconsole);
130 }
131 
132 static void
133 pckbc_ebus_attach(struct device *parent, struct device *self, void *aux)
134 {
135 	struct pckbc_js_softc *jsc = (struct pckbc_js_softc *)self;
136 	struct ebus_attach_args *ea = aux;
137 	bus_space_tag_t iot;
138 	bus_addr_t ioaddr;
139 	int intr;
140 	int stdin_node,	node;
141 	int isconsole;
142 
143 	iot = ea->ea_bustag;
144 	ioaddr = EBUS_ADDR_FROM_REG(&ea->ea_reg[0]);
145 	intr = ea->ea_nintr ? ea->ea_intr[0] : /* line */ 0;
146 
147 	/* search children of "8042" node for stdin (keyboard) */
148 	stdin_node = prom_instance_to_package(prom_stdin());
149 	isconsole = 0;
150 
151 	for (node = prom_firstchild(ea->ea_node);
152 	     node != 0; node = prom_nextsibling(node))
153 		if (node == stdin_node) {
154 			isconsole = 1;
155 			break;
156 		}
157 
158 	pckbc_js_attach_common(jsc, iot, ioaddr, intr, isconsole);
159 }
160 
161 
162 static void
163 pckbc_js_attach_common(struct pckbc_js_softc *jsc,
164 		       bus_space_tag_t iot, bus_addr_t ioaddr, int intr,
165 		       int isconsole)
166 {
167 	struct pckbc_softc *sc = (struct pckbc_softc *)jsc;
168 	struct pckbc_internal *t;
169 
170 	jsc->jsc_pckbc.intr_establish = pckbc_js_intr_establish;
171 	jsc->jsc_intr = intr;
172 	jsc->jsc_establised = 0;
173 
174 	if (isconsole) {
175 		int status;
176 
177 		status = pckbc_cnattach(iot, ioaddr, KBCMDP, PCKBC_KBD_SLOT);
178 		if (status == 0)
179 			printf(": cnattach ok");
180 		else
181 			printf(": cnattach %d", status);
182 	}
183 
184 	if (pckbc_is_console(iot, ioaddr)) {
185 		t = &pckbc_consdata;
186 		pckbc_console_attached = 1;
187 	} else {
188 		bus_space_handle_t ioh_d, ioh_c;
189 
190 		if (bus_space_map(iot, ioaddr + KBDATAP, 1, 0, &ioh_d) != 0) {
191 			printf(": unable to map data register\n");
192 			return;
193 		}
194 
195 		if (bus_space_map(iot, ioaddr + KBCMDP,  1, 0, &ioh_c) != 0) {
196 			bus_space_unmap(iot, ioh_d, 1);
197 			printf(": unable to map cmd register\n");
198 			return;
199 		}
200 
201 		t = malloc(sizeof(struct pckbc_internal), M_DEVBUF, M_WAITOK);
202 		memset(t, 0, sizeof(struct pckbc_internal));
203 		t->t_iot = iot;
204 		t->t_ioh_d = ioh_d;
205 		t->t_ioh_c = ioh_c;
206 		t->t_addr = ioaddr;
207 		t->t_cmdbyte = KC8_CPU; /* initial command: enable ports */
208 		callout_init(&t->t_cleanup, 0);
209 
210 		(void) pckbc_poll_data1(t, PCKBC_KBD_SLOT); /* flush */
211 
212 		if (pckbc_send_cmd(iot, ioh_c, KBC_SELFTEST) == 0)
213 			printf(": unable to request self test");
214 		else {
215 			int response;
216 
217 			response = pckbc_poll_data1(t, PCKBC_KBD_SLOT);
218 			if (response == 0x55)
219 				printf(": selftest ok");
220 			else
221 				printf(": selftest failed (0x%02x)", response);
222 		}
223 	}
224 
225 	/* crosslink */
226 	t->t_sc = sc;
227 	sc->id = t;
228 
229 	/* finish off the attach */
230 	printf("\n");
231 	pckbc_attach(sc);
232 }
233 
234 
235 /*
236  * Keyboard and mouse share the interrupt
237  * so don't install interrupt handler twice.
238  */
239 static void
240 pckbc_js_intr_establish(struct pckbc_softc *sc, pckbport_slot_t slot)
241 {
242 	struct pckbc_js_softc *jsc = (struct pckbc_js_softc *)sc;
243 	void *res;
244 
245 	if (jsc->jsc_establised) {
246 #ifdef DEBUG
247 		printf("%s: %s slot shares interrupt (already established)\n",
248 		       sc->sc_dv.dv_xname, pckbc_slot_names[slot]);
249 #endif
250 		return;
251 	}
252 
253 	/*
254 	 * We can not choose the devic class interruptlevel freely,
255 	 * so we debounce via a softinterrupt.
256 	 */
257 	jsc->jsc_int_cookie = softintr_establish(IPL_JSCKBD,
258 	    pckbcintr_soft, &jsc->jsc_pckbc);
259 	if (jsc->jsc_int_cookie == NULL) {
260 		printf("%s: unable to establish %s soft interrupt\n",
261 		       sc->sc_dv.dv_xname, pckbc_slot_names[slot]);
262 		return;
263 	}
264 	res = bus_intr_establish(sc->id->t_iot, jsc->jsc_intr,
265 				 IPL_SERIAL, jsc_pckbdintr, jsc);
266 	if (res == NULL)
267 		printf("%s: unable to establish %s slot interrupt\n",
268 		       sc->sc_dv.dv_xname, pckbc_slot_names[slot]);
269 	else
270 		jsc->jsc_establised = 1;
271 }
272 
273 static int
274 jsc_pckbdintr(void *vsc)
275 {
276 	struct pckbc_js_softc *jsc = vsc;
277 
278 	softintr_schedule(jsc->jsc_int_cookie);
279 	pckbcintr_hard(&jsc->jsc_pckbc);
280 
281 	/*
282 	 * This interrupt is not shared on javastations, avoid "stray"
283 	 * warnings. XXX - why do "stray interrupt" warnings happen if
284 	 * we don't claim the interrupt always?
285 	 */
286 	return 1;
287 }
288 
289 /*
290  * MD hook for use without MI wscons.
291  * Called by pckbc_cnattach().
292  */
293 int
294 pckbport_machdep_cnattach(pckbport_tag_t constag, pckbport_slot_t slot)
295 {
296 
297 	return (0);
298 }
299