sa1111_kbc.c revision 1.14
1/*      $NetBSD: sa1111_kbc.c,v 1.14 2012/02/02 19:42:58 tls 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.14 2012/02/02 19:42:58 tls 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#include <sys/rnd.h>
75
76#include <arm/sa11x0/sa1111_reg.h>
77#include <arm/sa11x0/sa1111_var.h>
78
79#include <dev/pckbport/pckbportvar.h>		/* for prototypes */
80
81#include "pckbd.h"
82#include "locators.h"
83
84struct 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	krndsource_t	rnd_source;
99	pckbport_tag_t pt;
100};
101
102static int	sackbc_match(device_t, cfdata_t, void *);
103static void	sackbc_attach(device_t, device_t, void *);
104
105static int	sackbc_xt_translation(void *, pckbport_slot_t, int);
106#define sackbc_send_devcmd	sackbc_send_cmd
107static int	sackbc_send_devcmd(void *, pckbport_slot_t, u_char);
108static int	sackbc_poll_data1(void *, pckbport_slot_t);
109static void	sackbc_slot_enable(void *, pckbport_slot_t, int);
110static void	sackbc_intr_establish(void *, pckbport_slot_t);
111static void	sackbc_set_poll(void *, pckbport_slot_t, int);
112
113CFATTACH_DECL_NEW(sackbc, sizeof(struct sackbc_softc), sackbc_match,
114    sackbc_attach, NULL, NULL);
115
116static struct pckbport_accessops const sackbc_ops = {
117	sackbc_xt_translation,
118	sackbc_send_devcmd,
119	sackbc_poll_data1,
120	sackbc_slot_enable,
121	sackbc_intr_establish,
122	sackbc_set_poll
123};
124
125#define	KBD_DELAY	DELAY(8)
126
127/*#define SACKBCDEBUG*/
128
129#ifdef SACKBCDEBUG
130#define DPRINTF(arg)  printf arg
131#else
132#define DPRINTF(arg)
133#endif
134
135
136static int
137sackbc_match(device_t parent, cfdata_t cf, void *aux)
138{
139	struct sa1111_attach_args *aa = (struct sa1111_attach_args *)aux;
140
141	switch (aa->sa_addr) {
142	case SACC_KBD0:
143	case SACC_KBD1:
144		return 1;
145	}
146	return 0;
147}
148
149#if 0
150static int
151sackbc_txint(void *cookie)
152{
153	struct sackbc_softc *sc = cookie;
154
155	bus_space_read_4(sc->iot, sc->ioh, SACCKBD_STAT);
156
157	return 0;
158}
159#endif
160
161static int
162sackbc_rxint(void *cookie)
163{
164	struct sackbc_softc *sc = cookie;
165	int stat, code = -1;
166
167	stat = bus_space_read_4(sc->iot, sc->ioh, SACCKBD_STAT);
168	DPRINTF(("sackbc_rxint stat=%x\n", stat));
169	if (stat & KBDSTAT_RXF) {
170		code = bus_space_read_4(sc->iot, sc->ioh, SACCKBD_DATA);
171
172		rnd_add_uint32(&sc->rnd_source, (stat<<8)|data);
173
174		if (sc->polling) {
175			sc->poll_data = code;
176			sc->poll_stat = stat;
177		} else
178			pckbportintr(sc->pt, sc->slot, code);
179		return 1;
180	}
181
182	return 0;
183}
184
185static void
186sackbc_intr_establish(void *cookie, pckbport_slot_t slot)
187{
188	struct sackbc_softc *sc = cookie;
189
190	if (!(sc->polling) && sc->ih_rx == NULL) {
191		sc->ih_rx = sacc_intr_establish(
192			(sacc_chipset_tag_t *)
193			  device_private(device_parent(sc->dev)),
194			sc->intr+1, IST_EDGE_RAISE, IPL_TTY, sackbc_rxint, sc);
195		if (sc->ih_rx == NULL) {
196			aprint_normal_dev(sc->dev, "can't establish interrupt\n");
197		}
198	}
199}
200
201static void
202sackbc_disable_intrhandler(struct sackbc_softc *sc)
203{
204	if (sc->polling && sc->ih_rx) {
205		sacc_intr_disestablish(
206			(sacc_chipset_tag_t *)
207			  device_private(device_parent(sc->dev)),
208			sc->ih_rx);
209		sc->ih_rx = NULL;
210	}
211}
212
213static	void
214sackbc_attach(device_t parent, device_t self, void *aux)
215{
216	struct sackbc_softc *sc = device_private(self);
217	struct sacc_softc *psc = device_private(parent);
218	struct sa1111_attach_args *aa = (struct sa1111_attach_args *)aux;
219	device_t child;
220	uint32_t tmp, clock_bit;
221	int intr, slot;
222
223	switch (aa->sa_addr) {
224	case SACC_KBD0: clock_bit = (1<<6); intr = 21; break;
225	case SACC_KBD1: clock_bit = (1<<5); intr = 18; break;
226	default:
227		return;
228	}
229
230	if (aa->sa_size <= 0)
231		aa->sa_size = SACCKBD_SIZE;
232	if (aa->sa_intr == SACCCF_INTR_DEFAULT)
233		aa->sa_intr = intr;
234
235	sc->dev = self;
236	sc->iot = psc->sc_iot;
237	if (bus_space_subregion(psc->sc_iot, psc->sc_ioh,
238	    aa->sa_addr, aa->sa_size, &sc->ioh)) {
239		aprint_normal(": can't map subregion\n");
240		return;
241	}
242
243	/* enable clock for PS/2 kbd or mouse */
244	tmp = bus_space_read_4(psc->sc_iot, psc->sc_ioh, SACCSC_SKPCR);
245	bus_space_write_4(psc->sc_iot, psc->sc_ioh, SACCSC_SKPCR,
246	    tmp | clock_bit);
247
248	sc->ih_rx = NULL;
249	sc->intr = aa->sa_intr;
250	sc->polling = 0;
251
252	tmp = bus_space_read_4(sc->iot, sc->ioh, SACCKBD_CR);
253	bus_space_write_4(sc->iot, sc->ioh, SACCKBD_CR, tmp | KBDCR_ENA);
254
255	/* XXX: this is necessary to get keyboard working. but I don't know why */
256	bus_space_write_4(sc->iot, sc->ioh, SACCKBD_CLKDIV, 2);
257
258	tmp = bus_space_read_4(sc->iot, sc->ioh, SACCKBD_STAT);
259	if ((tmp & KBDSTAT_ENA) == 0) {
260		printf("??? can't enable KBD controller\n");
261		return;
262	}
263
264	printf("\n");
265
266	sc->pt = pckbport_attach(sc, &sackbc_ops);
267
268	/*
269	 * Although there is no such thing as SLOT for SA-1111 kbd
270	 * controller, pckbd and pms drivers require it.
271	 */
272	for (slot = PCKBPORT_KBD_SLOT; slot <= PCKBPORT_AUX_SLOT; ++slot) {
273		child = pckbport_attach_slot(self, sc->pt, slot);
274
275		if (child == NULL)
276			continue;
277		sc->slot = slot;
278		rnd_attach_source(&sc->rnd_source, device_xname(child),
279		    RND_TYPE_TTY, 0);
280		/* only one of KBD_SLOT or AUX_SLOT is used. */
281		break;
282	}
283}
284
285
286static inline int
287sackbc_wait_output(struct sackbc_softc *sc)
288{
289	u_int i, stat;
290
291	for (i = 100000; i; i--){
292		stat = bus_space_read_4(sc->iot, sc->ioh, SACCKBD_STAT);
293		delay(100);
294		if (stat & KBDSTAT_TXE)
295			return 1;
296	}
297	return 0;
298}
299
300static int
301sackbc_poll_data1(void *cookie, pckbport_slot_t slot)
302{
303	struct sackbc_softc *sc = cookie;
304	int i, s, stat, c = -1;
305
306	s = spltty();
307
308	if (sc->polling){
309		stat	= sc->poll_stat;
310		c	= sc->poll_data;
311		sc->poll_data = -1;
312		sc->poll_stat = -1;
313		if (stat >= 0 &&
314		    (stat & (KBDSTAT_RXF|KBDSTAT_STP)) == KBDSTAT_RXF) {
315			splx(s);
316			return c;
317		}
318	}
319
320	/* if 1 port read takes 1us (?), this polls for 100ms */
321	for (i = 100000; i; i--) {
322		stat = bus_space_read_4(sc->iot, sc->ioh, SACCKBD_STAT);
323		if ((stat & (KBDSTAT_RXF|KBDSTAT_STP)) == KBDSTAT_RXF) {
324			KBD_DELAY;
325			c = bus_space_read_4(sc->iot, sc->ioh, SACCKBD_DATA);
326			break;
327		}
328	}
329
330	splx(s);
331	return c;
332}
333
334static int
335sackbc_send_cmd(void *cookie, pckbport_slot_t slot, u_char val)
336{
337	struct sackbc_softc *sc = cookie;
338
339	if (!sackbc_wait_output(sc))
340		return 0;
341	bus_space_write_1(sc->iot, sc->ioh, SACCKBD_DATA, val);
342	return 1;
343}
344
345
346/*
347 * Glue functions for pckbd on sackbc.
348 * These functions emulate those in dev/ic/pckbc.c.
349 *
350 */
351
352/*
353 * switch scancode translation on / off
354 * return nonzero on success
355 */
356static int
357sackbc_xt_translation(void *self, pckbport_slot_t slot, int on)
358{
359	/* KBD/Mouse controller doesn't have scancode translation */
360	return !on;
361}
362
363static void
364sackbc_slot_enable(void *self, pckbport_slot_t slot, int on)
365{
366#if 0
367	struct sackbc_softc *sc = (struct sackbc_softc *) self;
368	int cmd;
369
370	cmd = on ? KBC_KBDENABLE : KBC_KBDDISABLE;
371	if (!sackbc_send_cmd(sc, cmd))
372		printf("sackbc_slot_enable(%d) failed\n", on);
373#endif
374}
375
376
377static void
378sackbc_set_poll(void *self, pckbport_slot_t slot, int on)
379{
380	struct sackbc_softc *sc = (struct sackbc_softc *)self;
381	int s;
382
383	s = spltty();
384
385	if (sc->polling != on) {
386
387		sc->polling = on;
388
389		if (on) {
390			sc->poll_data = sc->poll_stat = -1;
391			sackbc_disable_intrhandler(sc);
392		} else {
393			/*
394			 * If disabling polling on a device that's
395			 * been configured, make sure there are no
396			 * bytes left in the FIFO, holding up the
397			 * interrupt line.  Otherwise we won't get any
398			 * further interrupts.
399			 */
400			sackbc_rxint(sc);
401			sackbc_intr_establish(sc, sc->slot);
402		}
403	}
404	splx(s);
405}
406