xref: /netbsd/sys/arch/sparc/dev/pckbc_js.c (revision bf9ec67e)
1 /*	$NetBSD: pckbc_js.c,v 1.2 2002/02/18 04:52:27 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 struct cfattach pckbc_obio_ca = {
69 	sizeof(struct pckbc_js_softc), pckbc_obio_match, pckbc_obio_attach
70 };
71 
72 /* ms-IIep */
73 struct cfattach pckbc_ebus_ca = {
74 	sizeof(struct pckbc_js_softc), pckbc_ebus_match, pckbc_ebus_attach
75 };
76 
77 
78 #define PCKBC_PROM_DEVICE_NAME "8042"
79 
80 static int
81 pckbc_obio_match(parent, cf, aux)
82 	struct device *parent;
83 	struct cfdata *cf;
84 	void *aux;
85 {
86 	union obio_attach_args *uoba = aux;
87 	struct sbus_attach_args *sa = &uoba->uoba_sbus;
88 
89 	return (strcmp(sa->sa_name, PCKBC_PROM_DEVICE_NAME) == 0);
90 }
91 
92 static int
93 pckbc_ebus_match(parent, cf, aux)
94 	struct device *parent;
95 	struct cfdata *cf;
96 	void *aux;
97 {
98 	struct ebus_attach_args *ea = aux;
99 
100 	return (strcmp(ea->ea_name, PCKBC_PROM_DEVICE_NAME) == 0);
101 }
102 
103 
104 static void
105 pckbc_obio_attach(parent, self, aux)
106 	struct device *parent, *self;
107 	void *aux;
108 {
109 	struct pckbc_js_softc *jsc = (struct pckbc_js_softc *)self;
110 	union obio_attach_args *uoba = aux;
111 	struct sbus_attach_args *sa = &uoba->uoba_sbus;
112 	bus_space_tag_t iot;
113 	bus_addr_t ioaddr;
114 	int intr, isconsole;
115 
116 	iot = sa->sa_bustag;
117 	ioaddr = BUS_ADDR(sa->sa_slot, sa->sa_offset);
118 	intr = sa->sa_nintr ? sa->sa_pri : /* level */ 13;
119 
120 	/*
121 	 * TODO:
122 	 * . on OFW machines stdin is keyboard node, not 8042 node
123 	 * . on OBP machines 8042 node is faked by boot's prompatch
124 	 *   and PROM's stdin points to zs keyboard (add prom patch?)
125 	 * . we probably want the stdout node to control whether
126 	 *   console goes to serial
127 	 */
128 	isconsole = 1;
129 
130 	pckbc_js_attach_common(jsc, iot, ioaddr, intr, isconsole);
131 }
132 
133 static void
134 pckbc_ebus_attach(parent, self, aux)
135 	struct device *parent, *self;
136 	void *aux;
137 {
138 	struct pckbc_js_softc *jsc = (struct pckbc_js_softc *)self;
139 	struct ebus_attach_args *ea = aux;
140 	bus_space_tag_t iot;
141 	bus_addr_t ioaddr;
142 	int intr, isconsole;
143 
144 	iot = ea->ea_bustag;
145 	ioaddr = EBUS_ADDR_FROM_REG(&ea->ea_reg[0]);
146 	intr = ea->ea_nintr ? ea->ea_intr[0] : /* line */ 0;
147 
148 	/* TODO: see comment in pckbc_obio_attach above */
149 	isconsole = 1;
150 
151 	pckbc_js_attach_common(jsc, iot, ioaddr, intr, isconsole);
152 }
153 
154 
155 static void
156 pckbc_js_attach_common(jsc, iot, ioaddr, intr, isconsole)
157 	struct pckbc_js_softc *jsc;
158 	bus_space_tag_t iot;
159 	bus_addr_t ioaddr;
160 	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 			printf(": cnattach ok");
176 		else
177 			printf(": 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 			printf(": 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 			printf(": 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);
205 
206 		(void) pckbc_poll_data1(t, PCKBC_KBD_SLOT, 0); /* flush */
207 
208 		if (pckbc_send_cmd(iot, ioh_c, KBC_SELFTEST) == 0)
209 			printf(": unable to request self test");
210 		else {
211 			int response;
212 
213 			response = pckbc_poll_data1(t, PCKBC_KBD_SLOT, 0);
214 			if (response == 0x55)
215 				printf(": selftest ok");
216 			else
217 				printf(": selftest failed (0x%02x)", response);
218 		}
219 	}
220 
221 	/* crosslink */
222 	t->t_sc = sc;
223 	sc->id = t;
224 
225 	/* finish off the attach */
226 	printf("\n");
227 	pckbc_attach(sc);
228 }
229 
230 
231 /*
232  * Keyboard and mouse share the interrupt
233  * so don't install interrupt handler twice.
234  */
235 static void
236 pckbc_js_intr_establish(sc, slot)
237 	struct pckbc_softc *sc;
238 	pckbc_slot_t slot;
239 {
240 	struct pckbc_js_softc *jsc = (struct pckbc_js_softc *)sc;
241 	void *res;
242 
243 	if (jsc->jsc_establised) {
244 #ifdef DEBUG
245 		printf("%s: %s slot shares interrupt (already established)\n",
246 		       sc->sc_dv.dv_xname, pckbc_slot_names[slot]);
247 #endif
248 		return;
249 	}
250 
251 	res = bus_intr_establish(sc->id->t_iot, jsc->jsc_intr,
252 				 IPL_TTY, 0, pckbcintr, sc);
253 	if (res == NULL)
254 		printf("%s: unable to establish %s slot interrupt\n",
255 		       sc->sc_dv.dv_xname, pckbc_slot_names[slot]);
256 	else
257 		jsc->jsc_establised = 1;
258 }
259 
260 
261 /*
262  * MD hook for use without MI wscons.
263  * Called by pckbc_cnattach().
264  */
265 int
266 pckbc_machdep_cnattach(constag, slot)
267     pckbc_tag_t constag;
268     pckbc_slot_t slot;
269 {
270 
271 #if !defined(__HAVE_NWSCONS) && defined(MSIIEP)
272 	/* we do use wscons on krups! */
273 	return (pckbd_cnattach(constag, slot));
274 #else
275 	return (0);
276 #endif
277 }
278