1/*	$NetBSD: pckbc_js.c,v 1.17 2008/03/15 13:23:24 cube 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.17 2008/03/15 13:23:24 cube 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
50struct 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
60static int	pckbc_obio_match(device_t, cfdata_t, void *);
61static void	pckbc_obio_attach(device_t, device_t, void *);
62
63static int	pckbc_ebus_match(device_t, cfdata_t, void *);
64static void	pckbc_ebus_attach(device_t, device_t, void *);
65
66static void	pckbc_js_attach_common(struct pckbc_js_softc *,
67				       bus_space_tag_t, bus_addr_t, int, int);
68static void	pckbc_js_intr_establish(struct pckbc_softc *, pckbport_slot_t);
69static int	jsc_pckbdintr(void *);
70
71/* Mr.Coffee */
72CFATTACH_DECL_NEW(pckbc_obio, sizeof(struct pckbc_js_softc),
73    pckbc_obio_match, pckbc_obio_attach, NULL, NULL);
74
75/* ms-IIep */
76CFATTACH_DECL_NEW(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
81static int
82pckbc_obio_match(device_t parent, cfdata_t 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
90static int
91pckbc_ebus_match(device_t parent, cfdata_t 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
99static void
100pckbc_obio_attach(device_t parent, device_t self, void *aux)
101{
102	struct pckbc_js_softc *jsc = device_private(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	jsc->jsc_pckbc.sc_dv = self;
110	iot = sa->sa_bustag;
111	ioaddr = BUS_ADDR(sa->sa_slot, sa->sa_offset);
112	intr = sa->sa_nintr ? sa->sa_pri : /* level */ 13;
113
114	/*
115	 * TODO:
116	 * . on OFW machines stdin is keyboard node, not 8042 node
117	 * . on OBP machines 8042 node is faked by boot's prompatch
118	 *   and PROM's stdin points to zs keyboard (add prom patch?)
119	 * . we probably want the stdout node to control whether
120	 *   console goes to serial
121	 */
122	isconsole = 1;
123
124	pckbc_js_attach_common(jsc, iot, ioaddr, intr, isconsole);
125}
126
127static void
128pckbc_ebus_attach(device_t parent, device_t self, void *aux)
129{
130	struct pckbc_js_softc *jsc = device_private(self);
131	struct ebus_attach_args *ea = aux;
132	bus_space_tag_t iot;
133	bus_addr_t ioaddr;
134	int intr;
135	int stdin_node,	node;
136	int isconsole;
137
138	jsc->jsc_pckbc.sc_dv = self;
139	iot = ea->ea_bustag;
140	ioaddr = EBUS_ADDR_FROM_REG(&ea->ea_reg[0]);
141	intr = ea->ea_nintr ? ea->ea_intr[0] : /* line */ 0;
142
143	/* search children of "8042" node for stdin (keyboard) */
144	stdin_node = prom_instance_to_package(prom_stdin());
145	isconsole = 0;
146
147	for (node = prom_firstchild(ea->ea_node);
148	     node != 0; node = prom_nextsibling(node))
149		if (node == stdin_node) {
150			isconsole = 1;
151			break;
152		}
153
154	pckbc_js_attach_common(jsc, iot, ioaddr, intr, isconsole);
155}
156
157
158static void
159pckbc_js_attach_common(struct pckbc_js_softc *jsc,
160		       bus_space_tag_t iot, bus_addr_t ioaddr, int intr,
161		       int isconsole)
162{
163	struct pckbc_softc *sc = (struct pckbc_softc *)jsc;
164	struct pckbc_internal *t;
165
166	jsc->jsc_pckbc.intr_establish = pckbc_js_intr_establish;
167	jsc->jsc_intr = intr;
168	jsc->jsc_establised = 0;
169
170	if (isconsole) {
171		int status;
172
173		status = pckbc_cnattach(iot, ioaddr, KBCMDP, PCKBC_KBD_SLOT);
174		if (status == 0)
175			aprint_normal(": cnattach ok");
176		else
177			aprint_error(": cnattach %d", status);
178	}
179
180	if (pckbc_is_console(iot, ioaddr)) {
181		t = &pckbc_consdata;
182		pckbc_console_attached = 1;
183	} else {
184		bus_space_handle_t ioh_d, ioh_c;
185
186		if (bus_space_map(iot, ioaddr + KBDATAP, 1, 0, &ioh_d) != 0) {
187			aprint_error(": unable to map data register\n");
188			return;
189		}
190
191		if (bus_space_map(iot, ioaddr + KBCMDP,  1, 0, &ioh_c) != 0) {
192			bus_space_unmap(iot, ioh_d, 1);
193			aprint_error(": unable to map cmd register\n");
194			return;
195		}
196
197		t = malloc(sizeof(struct pckbc_internal), M_DEVBUF, M_WAITOK);
198		memset(t, 0, sizeof(struct pckbc_internal));
199		t->t_iot = iot;
200		t->t_ioh_d = ioh_d;
201		t->t_ioh_c = ioh_c;
202		t->t_addr = ioaddr;
203		t->t_cmdbyte = KC8_CPU; /* initial command: enable ports */
204		callout_init(&t->t_cleanup, 0);
205
206		(void) pckbc_poll_data1(t, PCKBC_KBD_SLOT); /* flush */
207
208		if (pckbc_send_cmd(iot, ioh_c, KBC_SELFTEST) == 0)
209			aprint_error(": unable to request self test");
210		else {
211			int response;
212
213			response = pckbc_poll_data1(t, PCKBC_KBD_SLOT);
214			if (response == 0x55)
215				aprint_normal(": selftest ok");
216			else
217				aprint_error(": selftest failed (0x%02x)",
218				    response);
219		}
220	}
221
222	/* crosslink */
223	t->t_sc = sc;
224	sc->id = t;
225
226	/* finish off the attach */
227	aprint_normal("\n");
228	pckbc_attach(sc);
229}
230
231
232/*
233 * Keyboard and mouse share the interrupt
234 * so don't install interrupt handler twice.
235 */
236static void
237pckbc_js_intr_establish(struct pckbc_softc *sc, pckbport_slot_t slot)
238{
239	struct pckbc_js_softc *jsc = (struct pckbc_js_softc *)sc;
240	void *res;
241
242	if (jsc->jsc_establised) {
243#ifdef DEBUG
244		aprint_verbose_dev(sc->sc_dv,
245		    "%s slot shares interrupt (already established)\n",
246		    pckbc_slot_names[slot]);
247#endif
248		return;
249	}
250
251	/*
252	 * We can not choose the devic class interruptlevel freely,
253	 * so we debounce via a softinterrupt.
254	 */
255	jsc->jsc_int_cookie = softint_establish(SOFTINT_SERIAL,
256	    pckbcintr_soft, &jsc->jsc_pckbc);
257	if (jsc->jsc_int_cookie == NULL) {
258		aprint_error_dev(sc->sc_dv,
259		    "unable to establish %s soft interrupt\n",
260		    pckbc_slot_names[slot]);
261		return;
262	}
263	res = bus_intr_establish(sc->id->t_iot, jsc->jsc_intr,
264				 IPL_SERIAL, jsc_pckbdintr, jsc);
265	if (res == NULL)
266		aprint_error_dev(sc->sc_dv,
267		    "unable to establish %s slot interrupt\n",
268		    pckbc_slot_names[slot]);
269	else
270		jsc->jsc_establised = 1;
271}
272
273static int
274jsc_pckbdintr(void *vsc)
275{
276	struct pckbc_js_softc *jsc = vsc;
277
278	softint_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 */
293int
294pckbport_machdep_cnattach(pckbport_tag_t constag, pckbport_slot_t slot)
295{
296
297	return (0);
298}
299