xref: /netbsd/sys/arch/sparc/dev/pckbc_js.c (revision c4a72b64)
1 /*	$NetBSD: pckbc_js.c,v 1.6 2002/10/03 16:22:46 uwe 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/param.h>
31 #include <sys/systm.h>
32 #include <sys/kernel.h>
33 #include <sys/device.h>
34 #include <sys/malloc.h>
35 
36 #include <machine/autoconf.h>
37 #include <machine/bus.h>
38 #include <machine/intr.h>
39 
40 #include <dev/ic/i8042reg.h>
41 #include <dev/ic/pckbcvar.h>
42 #include <dev/pckbc/pckbdvar.h>
43 
44 #include <dev/ebus/ebusreg.h>
45 #include <dev/ebus/ebusvar.h>
46 
47 
48 struct pckbc_js_softc {
49 	struct pckbc_softc jsc_pckbc;	/* real "pckbc" softc */
50 
51 	/* kbd and mouse share interrupt in both mr.coffee and krups */
52 	u_int32_t jsc_intr;
53 	int jsc_establised;
54 };
55 
56 
57 static int	pckbc_obio_match(struct device *, struct cfdata *, void *);
58 static void	pckbc_obio_attach(struct device *, struct device *, void *);
59 
60 static int	pckbc_ebus_match(struct device *, struct cfdata *, void *);
61 static void	pckbc_ebus_attach(struct device *, struct device *, void *);
62 
63 static void	pckbc_js_attach_common(	struct pckbc_js_softc *,
64 					bus_space_tag_t, bus_addr_t, int, int);
65 static void	pckbc_js_intr_establish(struct pckbc_softc *, pckbc_slot_t);
66 
67 /* Mr.Coffee */
68 CFATTACH_DECL(pckbc_obio, sizeof(struct pckbc_js_softc),
69     pckbc_obio_match, pckbc_obio_attach, NULL, NULL);
70 
71 /* ms-IIep */
72 CFATTACH_DECL(pckbc_ebus, sizeof(struct pckbc_js_softc),
73     pckbc_ebus_match, pckbc_ebus_attach, NULL, NULL);
74 
75 #define PCKBC_PROM_DEVICE_NAME "8042"
76 
77 static int
78 pckbc_obio_match(parent, cf, aux)
79 	struct device *parent;
80 	struct cfdata *cf;
81 	void *aux;
82 {
83 	union obio_attach_args *uoba = aux;
84 	struct sbus_attach_args *sa = &uoba->uoba_sbus;
85 
86 	return (strcmp(sa->sa_name, PCKBC_PROM_DEVICE_NAME) == 0);
87 }
88 
89 static int
90 pckbc_ebus_match(parent, cf, aux)
91 	struct device *parent;
92 	struct cfdata *cf;
93 	void *aux;
94 {
95 	struct ebus_attach_args *ea = aux;
96 
97 	return (strcmp(ea->ea_name, PCKBC_PROM_DEVICE_NAME) == 0);
98 }
99 
100 
101 static void
102 pckbc_obio_attach(parent, self, aux)
103 	struct device *parent, *self;
104 	void *aux;
105 {
106 	struct pckbc_js_softc *jsc = (struct pckbc_js_softc *)self;
107 	union obio_attach_args *uoba = aux;
108 	struct sbus_attach_args *sa = &uoba->uoba_sbus;
109 	bus_space_tag_t iot;
110 	bus_addr_t ioaddr;
111 	int intr, isconsole;
112 
113 	iot = sa->sa_bustag;
114 	ioaddr = BUS_ADDR(sa->sa_slot, sa->sa_offset);
115 	intr = sa->sa_nintr ? sa->sa_pri : /* level */ 13;
116 
117 	/*
118 	 * TODO:
119 	 * . on OFW machines stdin is keyboard node, not 8042 node
120 	 * . on OBP machines 8042 node is faked by boot's prompatch
121 	 *   and PROM's stdin points to zs keyboard (add prom patch?)
122 	 * . we probably want the stdout node to control whether
123 	 *   console goes to serial
124 	 */
125 	isconsole = 1;
126 
127 	pckbc_js_attach_common(jsc, iot, ioaddr, intr, isconsole);
128 }
129 
130 static void
131 pckbc_ebus_attach(parent, self, aux)
132 	struct device *parent, *self;
133 	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(jsc, iot, ioaddr, intr, isconsole)
164 	struct pckbc_js_softc *jsc;
165 	bus_space_tag_t iot;
166 	bus_addr_t ioaddr;
167 	int intr;
168 	int isconsole;
169 {
170 	struct pckbc_softc *sc = (struct pckbc_softc *)jsc;
171 	struct pckbc_internal *t;
172 
173 	jsc->jsc_pckbc.intr_establish = pckbc_js_intr_establish;
174 	jsc->jsc_intr = intr;
175 	jsc->jsc_establised = 0;
176 
177 	if (isconsole) {
178 		int status;
179 
180 		status = pckbc_cnattach(iot, ioaddr, KBCMDP, PCKBC_KBD_SLOT);
181 		if (status == 0)
182 			printf(": cnattach ok");
183 		else
184 			printf(": cnattach %d", status);
185 	}
186 
187 	if (pckbc_is_console(iot, ioaddr)) {
188 		t = &pckbc_consdata;
189 		pckbc_console_attached = 1;
190 	} else {
191 		bus_space_handle_t ioh_d, ioh_c;
192 
193 		if (bus_space_map(iot, ioaddr + KBDATAP, 1, 0, &ioh_d) != 0) {
194 			printf(": unable to map data register\n");
195 			return;
196 		}
197 
198 		if (bus_space_map(iot, ioaddr + KBCMDP,  1, 0, &ioh_c) != 0) {
199 			bus_space_unmap(iot, ioh_d, 1);
200 			printf(": unable to map cmd register\n");
201 			return;
202 		}
203 
204 		t = malloc(sizeof(struct pckbc_internal), M_DEVBUF, M_WAITOK);
205 		memset(t, 0, sizeof(struct pckbc_internal));
206 		t->t_iot = iot;
207 		t->t_ioh_d = ioh_d;
208 		t->t_ioh_c = ioh_c;
209 		t->t_addr = ioaddr;
210 		t->t_cmdbyte = KC8_CPU; /* initial command: enable ports */
211 		callout_init(&t->t_cleanup);
212 
213 		(void) pckbc_poll_data1(t, PCKBC_KBD_SLOT, 0); /* flush */
214 
215 		if (pckbc_send_cmd(iot, ioh_c, KBC_SELFTEST) == 0)
216 			printf(": unable to request self test");
217 		else {
218 			int response;
219 
220 			response = pckbc_poll_data1(t, PCKBC_KBD_SLOT, 0);
221 			if (response == 0x55)
222 				printf(": selftest ok");
223 			else
224 				printf(": selftest failed (0x%02x)", response);
225 		}
226 	}
227 
228 	/* crosslink */
229 	t->t_sc = sc;
230 	sc->id = t;
231 
232 	/* finish off the attach */
233 	printf("\n");
234 	pckbc_attach(sc);
235 }
236 
237 
238 /*
239  * Keyboard and mouse share the interrupt
240  * so don't install interrupt handler twice.
241  */
242 static void
243 pckbc_js_intr_establish(sc, slot)
244 	struct pckbc_softc *sc;
245 	pckbc_slot_t slot;
246 {
247 	struct pckbc_js_softc *jsc = (struct pckbc_js_softc *)sc;
248 	void *res;
249 
250 	if (jsc->jsc_establised) {
251 #ifdef DEBUG
252 		printf("%s: %s slot shares interrupt (already established)\n",
253 		       sc->sc_dv.dv_xname, pckbc_slot_names[slot]);
254 #endif
255 		return;
256 	}
257 
258 	res = bus_intr_establish(sc->id->t_iot, jsc->jsc_intr,
259 				 IPL_TTY, 0, pckbcintr, sc);
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 
268 /*
269  * MD hook for use without MI wscons.
270  * Called by pckbc_cnattach().
271  */
272 int
273 pckbc_machdep_cnattach(constag, slot)
274     pckbc_tag_t constag;
275     pckbc_slot_t slot;
276 {
277 
278 #if !defined(__HAVE_NWSCONS) && defined(MSIIEP)
279 	/* we do use wscons on krups! */
280 	return (pckbd_cnattach(constag, slot));
281 #else
282 	return (0);
283 #endif
284 }
285