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