xref: /netbsd-src/sys/arch/arm/sa11x0/sa1111_kbc.c (revision a7e090f70e491979434963c9a27df4020fe0a18b)
1 /*      $NetBSD: sa1111_kbc.c,v 1.12 2010/03/13 11:13:31 bsh Exp $ */
2 
3 /*
4  * Copyright (c) 2004  Ben Harris.
5  * Copyright (c) 2002, 2004  Genetec Corporation.  All rights reserved.
6  * Written by Hiroyuki Bessho for Genetec Corporation.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. The name of Genetec Corporation may not be used to endorse or
17  *    promote products derived from this software without specific prior
18  *    written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY GENETEC CORPORATION ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL GENETEC CORPORATION
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  *
32  * Driver for keyboard controller in SA-1111 companion chip.
33  */
34 /*
35  * Copyright (c) 1998
36  *	Matthias Drochner.  All rights reserved.
37  *
38  * Redistribution and use in source and binary forms, with or without
39  * modification, are permitted provided that the following conditions
40  * are met:
41  * 1. Redistributions of source code must retain the above copyright
42  *    notice, this list of conditions and the following disclaimer.
43  * 2. Redistributions in binary form must reproduce the above copyright
44  *    notice, this list of conditions and the following disclaimer in the
45  *    documentation and/or other materials provided with the distribution.
46  *
47  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
48  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
49  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
50  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
51  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
52  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
53  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
54  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
55  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
56  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
57  */
58 
59 #include <sys/cdefs.h>
60 __KERNEL_RCSID(0, "$NetBSD: sa1111_kbc.c,v 1.12 2010/03/13 11:13:31 bsh Exp $");
61 
62 #include <sys/param.h>
63 #include <sys/systm.h>
64 #include <sys/types.h>
65 #include <sys/callout.h>
66 #include <sys/kernel.h>
67 #include <sys/proc.h>
68 #include <sys/conf.h>
69 #include <sys/device.h>
70 #include <sys/malloc.h>
71 #include <sys/errno.h>
72 #include <sys/queue.h>
73 #include <sys/bus.h>
74 
75 #include <arm/sa11x0/sa1111_reg.h>
76 #include <arm/sa11x0/sa1111_var.h>
77 
78 #include <dev/pckbport/pckbportvar.h>		/* for prototypes */
79 
80 #include "pckbd.h"
81 #include "rnd.h"
82 #include "locators.h"
83 
84 struct sackbc_softc {
85 	device_t dev;
86 
87 	bus_space_tag_t    iot;
88 	bus_space_handle_t ioh;
89 
90 	void	*ih_rx;			/* receive interrupt */
91 	int	intr;			/* interrupt number */
92 	int	slot;		/* KBD_SLOT or AUX_SLOT */
93 
94 	int	polling;	/* don't process data in interrupt handler */
95 	int	poll_stat;	/* data read from inr handler if polling */
96 	int	poll_data;	/* status read from intr handler if polling */
97 
98 #if NRND > 0
99 	rndsource_element_t	rnd_source;
100 #endif
101 	pckbport_tag_t pt;
102 };
103 
104 static int	sackbc_match(device_t, cfdata_t, void *);
105 static void	sackbc_attach(device_t, device_t, void *);
106 
107 static int	sackbc_xt_translation(void *, pckbport_slot_t, int);
108 #define sackbc_send_devcmd	sackbc_send_cmd
109 static int	sackbc_send_devcmd(void *, pckbport_slot_t, u_char);
110 static int	sackbc_poll_data1(void *, pckbport_slot_t);
111 static void	sackbc_slot_enable(void *, pckbport_slot_t, int);
112 static void	sackbc_intr_establish(void *, pckbport_slot_t);
113 static void	sackbc_set_poll(void *, pckbport_slot_t, int);
114 
115 CFATTACH_DECL_NEW(sackbc, sizeof(struct sackbc_softc), sackbc_match,
116     sackbc_attach, NULL, NULL);
117 
118 static struct pckbport_accessops const sackbc_ops = {
119 	sackbc_xt_translation,
120 	sackbc_send_devcmd,
121 	sackbc_poll_data1,
122 	sackbc_slot_enable,
123 	sackbc_intr_establish,
124 	sackbc_set_poll
125 };
126 
127 #define	KBD_DELAY	DELAY(8)
128 
129 /*#define SACKBCDEBUG*/
130 
131 #ifdef SACKBCDEBUG
132 #define DPRINTF(arg)  printf arg
133 #else
134 #define DPRINTF(arg)
135 #endif
136 
137 
138 static int
139 sackbc_match(device_t parent, cfdata_t cf, void *aux)
140 {
141 	struct sa1111_attach_args *aa = (struct sa1111_attach_args *)aux;
142 
143 	switch (aa->sa_addr) {
144 	case SACC_KBD0:
145 	case SACC_KBD1:
146 		return 1;
147 	}
148 	return 0;
149 }
150 
151 #if 0
152 static int
153 sackbc_txint(void *cookie)
154 {
155 	struct sackbc_softc *sc = cookie;
156 
157 	bus_space_read_4(sc->iot, sc->ioh, SACCKBD_STAT);
158 
159 	return 0;
160 }
161 #endif
162 
163 static int
164 sackbc_rxint(void *cookie)
165 {
166 	struct sackbc_softc *sc = cookie;
167 	int stat, code = -1;
168 
169 	stat = bus_space_read_4(sc->iot, sc->ioh, SACCKBD_STAT);
170 	DPRINTF(("sackbc_rxint stat=%x\n", stat));
171 	if (stat & KBDSTAT_RXF) {
172 		code = bus_space_read_4(sc->iot, sc->ioh, SACCKBD_DATA);
173 
174 #if NRND > 0
175 		rnd_add_uint32(&sc->rnd_source, (stat<<8)|data);
176 #endif
177 
178 		if (sc->polling) {
179 			sc->poll_data = code;
180 			sc->poll_stat = stat;
181 		} else
182 			pckbportintr(sc->pt, sc->slot, code);
183 		return 1;
184 	}
185 
186 	return 0;
187 }
188 
189 static void
190 sackbc_intr_establish(void *cookie, pckbport_slot_t slot)
191 {
192 	struct sackbc_softc *sc = cookie;
193 
194 	if (!(sc->polling) && sc->ih_rx == NULL) {
195 		sc->ih_rx = sacc_intr_establish(
196 			(sacc_chipset_tag_t *)
197 			  device_private(device_parent(sc->dev)),
198 			sc->intr+1, IST_EDGE_RAISE, IPL_TTY, sackbc_rxint, sc);
199 		if (sc->ih_rx == NULL) {
200 			aprint_normal_dev(sc->dev, "can't establish interrupt\n");
201 		}
202 	}
203 }
204 
205 static void
206 sackbc_disable_intrhandler(struct sackbc_softc *sc)
207 {
208 	if (sc->polling && sc->ih_rx) {
209 		sacc_intr_disestablish(
210 			(sacc_chipset_tag_t *)
211 			  device_private(device_parent(sc->dev)),
212 			sc->ih_rx);
213 		sc->ih_rx = NULL;
214 	}
215 }
216 
217 static	void
218 sackbc_attach(device_t parent, device_t self, void *aux)
219 {
220 	struct sackbc_softc *sc = device_private(self);
221 	struct sacc_softc *psc = device_private(parent);
222 	struct sa1111_attach_args *aa = (struct sa1111_attach_args *)aux;
223 	device_t child;
224 	uint32_t tmp, clock_bit;
225 	int intr, slot;
226 
227 	switch (aa->sa_addr) {
228 	case SACC_KBD0: clock_bit = (1<<6); intr = 21; break;
229 	case SACC_KBD1: clock_bit = (1<<5); intr = 18; break;
230 	default:
231 		return;
232 	}
233 
234 	if (aa->sa_size <= 0)
235 		aa->sa_size = SACCKBD_SIZE;
236 	if (aa->sa_intr == SACCCF_INTR_DEFAULT)
237 		aa->sa_intr = intr;
238 
239 	sc->dev = self;
240 	sc->iot = psc->sc_iot;
241 	if (bus_space_subregion(psc->sc_iot, psc->sc_ioh,
242 	    aa->sa_addr, aa->sa_size, &sc->ioh)) {
243 		aprint_normal(": can't map subregion\n");
244 		return;
245 	}
246 
247 	/* enable clock for PS/2 kbd or mouse */
248 	tmp = bus_space_read_4(psc->sc_iot, psc->sc_ioh, SACCSC_SKPCR);
249 	bus_space_write_4(psc->sc_iot, psc->sc_ioh, SACCSC_SKPCR,
250 	    tmp | clock_bit);
251 
252 	sc->ih_rx = NULL;
253 	sc->intr = aa->sa_intr;
254 	sc->polling = 0;
255 
256 	tmp = bus_space_read_4(sc->iot, sc->ioh, SACCKBD_CR);
257 	bus_space_write_4(sc->iot, sc->ioh, SACCKBD_CR, tmp | KBDCR_ENA);
258 
259 	/* XXX: this is necessary to get keyboard working. but I don't know why */
260 	bus_space_write_4(sc->iot, sc->ioh, SACCKBD_CLKDIV, 2);
261 
262 	tmp = bus_space_read_4(sc->iot, sc->ioh, SACCKBD_STAT);
263 	if ((tmp & KBDSTAT_ENA) == 0) {
264 		printf("??? can't enable KBD controller\n");
265 		return;
266 	}
267 
268 	printf("\n");
269 
270 	sc->pt = pckbport_attach(sc, &sackbc_ops);
271 
272 	/*
273 	 * Although there is no such thing as SLOT for SA-1111 kbd
274 	 * controller, pckbd and pms drivers require it.
275 	 */
276 	for (slot = PCKBPORT_KBD_SLOT; slot <= PCKBPORT_AUX_SLOT; ++slot) {
277 		child = pckbport_attach_slot(self, sc->pt, slot);
278 
279 		if (child == NULL)
280 			continue;
281 		sc->slot = slot;
282 #if NRND > 0
283 		rnd_attach_source(&sc->rnd_source, device_xname(child),
284 		    RND_TYPE_TTY, 0);
285 #endif
286 		/* only one of KBD_SLOT or AUX_SLOT is used. */
287 		break;
288 	}
289 }
290 
291 
292 static inline int
293 sackbc_wait_output(struct sackbc_softc *sc)
294 {
295 	u_int i, stat;
296 
297 	for (i = 100000; i; i--){
298 		stat = bus_space_read_4(sc->iot, sc->ioh, SACCKBD_STAT);
299 		delay(100);
300 		if (stat & KBDSTAT_TXE)
301 			return 1;
302 	}
303 	return 0;
304 }
305 
306 static int
307 sackbc_poll_data1(void *cookie, pckbport_slot_t slot)
308 {
309 	struct sackbc_softc *sc = cookie;
310 	int i, s, stat, c = -1;
311 
312 	s = spltty();
313 
314 	if (sc->polling){
315 		stat	= sc->poll_stat;
316 		c	= sc->poll_data;
317 		sc->poll_data = -1;
318 		sc->poll_stat = -1;
319 		if (stat >= 0 &&
320 		    (stat & (KBDSTAT_RXF|KBDSTAT_STP)) == KBDSTAT_RXF) {
321 			splx(s);
322 			return c;
323 		}
324 	}
325 
326 	/* if 1 port read takes 1us (?), this polls for 100ms */
327 	for (i = 100000; i; i--) {
328 		stat = bus_space_read_4(sc->iot, sc->ioh, SACCKBD_STAT);
329 		if ((stat & (KBDSTAT_RXF|KBDSTAT_STP)) == KBDSTAT_RXF) {
330 			KBD_DELAY;
331 			c = bus_space_read_4(sc->iot, sc->ioh, SACCKBD_DATA);
332 			break;
333 		}
334 	}
335 
336 	splx(s);
337 	return c;
338 }
339 
340 static int
341 sackbc_send_cmd(void *cookie, pckbport_slot_t slot, u_char val)
342 {
343 	struct sackbc_softc *sc = cookie;
344 
345 	if (!sackbc_wait_output(sc))
346 		return 0;
347 	bus_space_write_1(sc->iot, sc->ioh, SACCKBD_DATA, val);
348 	return 1;
349 }
350 
351 
352 /*
353  * Glue functions for pckbd on sackbc.
354  * These functions emulate those in dev/ic/pckbc.c.
355  *
356  */
357 
358 /*
359  * switch scancode translation on / off
360  * return nonzero on success
361  */
362 static int
363 sackbc_xt_translation(void *self, pckbport_slot_t slot, int on)
364 {
365 	/* KBD/Mouse controller doesn't have scancode translation */
366 	return !on;
367 }
368 
369 static void
370 sackbc_slot_enable(void *self, pckbport_slot_t slot, int on)
371 {
372 #if 0
373 	struct sackbc_softc *sc = (struct sackbc_softc *) self;
374 	int cmd;
375 
376 	cmd = on ? KBC_KBDENABLE : KBC_KBDDISABLE;
377 	if (!sackbc_send_cmd(sc, cmd))
378 		printf("sackbc_slot_enable(%d) failed\n", on);
379 #endif
380 }
381 
382 
383 static void
384 sackbc_set_poll(void *self, pckbport_slot_t slot, int on)
385 {
386 	struct sackbc_softc *sc = (struct sackbc_softc *)self;
387 	int s;
388 
389 	s = spltty();
390 
391 	if (sc->polling != on) {
392 
393 		sc->polling = on;
394 
395 		if (on) {
396 			sc->poll_data = sc->poll_stat = -1;
397 			sackbc_disable_intrhandler(sc);
398 		} else {
399 			/*
400 			 * If disabling polling on a device that's
401 			 * been configured, make sure there are no
402 			 * bytes left in the FIFO, holding up the
403 			 * interrupt line.  Otherwise we won't get any
404 			 * further interrupts.
405 			 */
406 			sackbc_rxint(sc);
407 			sackbc_intr_establish(sc, sc->slot);
408 		}
409 	}
410 	splx(s);
411 }
412