Expand PMF_FN_* macros.
[netbsd-mini2440.git] / sys / arch / arm / sa11x0 / sa1111_kbc.c
bloba98720e501ddad8e5b79c9fce904bea1519f92a1
1 /* $NetBSD: sa1111_kbc.c,v 1.10 2008/01/05 00:31:55 ad Exp $ */
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.
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.
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.
32 * Driver for keyboard controller in SA-1111 companion chip.
35 * Copyright (c) 1998
36 * Matthias Drochner. All rights reserved.
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.
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.
59 #include <sys/cdefs.h>
60 __KERNEL_RCSID(0, "$NetBSD: sa1111_kbc.c,v 1.10 2008/01/05 00:31:55 ad Exp $");
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>
75 #include <arm/sa11x0/sa1111_reg.h>
76 #include <arm/sa11x0/sa1111_var.h>
78 #include <dev/pckbport/pckbportvar.h> /* for prototypes */
80 #include "pckbd.h"
81 #include "rnd.h"
82 #include "locators.h"
84 struct sackbc_softc {
85 device_t dev;
87 bus_space_tag_t iot;
88 bus_space_handle_t ioh;
90 void *ih_rx; /* receive interrupt */
91 int intr; /* interrupt number */
92 int slot; /* KBD_SLOT or AUX_SLOT */
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 */
98 #if NRND > 0
99 rndsource_element_t rnd_source;
100 #endif
101 pckbport_tag_t pt;
104 static int sackbc_match(device_t, cfdata_t, void *);
105 static void sackbc_attach(device_t, device_t, void *);
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);
115 CFATTACH_DECL_NEW(sackbc, sizeof(struct sackbc_softc), sackbc_match,
116 sackbc_attach, NULL, NULL);
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
127 #define KBD_DELAY DELAY(8)
129 /*#define SACKBCDEBUG*/
131 #ifdef SACKBCDEBUG
132 #define DPRINTF(arg) printf arg
133 #else
134 #define DPRINTF(arg)
135 #endif
138 static int
139 sackbc_match(device_t parent, cfdata_t cf, void *aux)
141 struct sa1111_attach_args *aa = (struct sa1111_attach_args *)aux;
143 switch (aa->sa_addr) {
144 case SACC_KBD0:
145 case SACC_KBD1:
146 return 1;
148 return 0;
151 #if 0
152 static int
153 sackbc_txint(void *cookie)
155 struct sackbc_softc *sc = cookie;
157 bus_space_read_4(sc->iot, sc->ioh, SACCKBD_STAT);
159 return 0;
161 #endif
163 static int
164 sackbc_rxint(void *cookie)
166 struct sackbc_softc *sc = cookie;
167 int stat, code = -1;
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);
174 #if NRND > 0
175 rnd_add_uint32(&sc->rnd_source, (stat<<8)|data);
176 #endif
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;
186 return 0;
189 static void
190 sackbc_intr_establish(void *cookie, pckbport_slot_t slot)
192 struct sackbc_softc *sc = cookie;
194 if (!(sc->polling) && sc->ih_rx == NULL) {
195 sc->ih_rx = sacc_intr_establish(
196 (sacc_chipset_tag_t *) device_parent(sc->dev),
197 sc->intr+1, IST_EDGE_RAISE, IPL_TTY, sackbc_rxint, sc);
198 if (sc->ih_rx == NULL) {
199 aprint_normal_dev(sc->dev, "can't establish interrupt\n");
204 static void
205 sackbc_disable_intrhandler(struct sackbc_softc *sc)
207 if (sc->polling && sc->ih_rx) {
208 sacc_intr_disestablish(
209 (sacc_chipset_tag_t *) device_parent(sc->dev),
210 sc->ih_rx);
211 sc->ih_rx = NULL;
215 static void
216 sackbc_attach(device_t parent, device_t self, void *aux)
218 struct sackbc_softc *sc = device_private(self);
219 struct sacc_softc *psc = device_private(parent);
220 struct sa1111_attach_args *aa = (struct sa1111_attach_args *)aux;
221 device_t child;
222 uint32_t tmp, clock_bit;
223 int intr, slot;
225 switch (aa->sa_addr) {
226 case SACC_KBD0: clock_bit = (1<<6); intr = 21; break;
227 case SACC_KBD1: clock_bit = (1<<5); intr = 18; break;
228 default:
229 return;
232 if (aa->sa_size <= 0)
233 aa->sa_size = SACCKBD_SIZE;
234 if (aa->sa_intr == SACCCF_INTR_DEFAULT)
235 aa->sa_intr = intr;
237 sc->dev = self;
238 sc->iot = psc->sc_iot;
239 if (bus_space_subregion(psc->sc_iot, psc->sc_ioh,
240 aa->sa_addr, aa->sa_size, &sc->ioh)) {
241 aprint_normal(": can't map subregion\n");
242 return;
245 /* enable clock for PS/2 kbd or mouse */
246 tmp = bus_space_read_4(psc->sc_iot, psc->sc_ioh, SACCSC_SKPCR);
247 bus_space_write_4(psc->sc_iot, psc->sc_ioh, SACCSC_SKPCR,
248 tmp | clock_bit);
250 sc->ih_rx = NULL;
251 sc->intr = aa->sa_intr;
252 sc->polling = 0;
254 tmp = bus_space_read_4(sc->iot, sc->ioh, SACCKBD_CR);
255 bus_space_write_4(sc->iot, sc->ioh, SACCKBD_CR, tmp | KBDCR_ENA);
257 /* XXX: this is necessary to get keyboard working. but I don't know why */
258 bus_space_write_4(sc->iot, sc->ioh, SACCKBD_CLKDIV, 2);
260 tmp = bus_space_read_4(sc->iot, sc->ioh, SACCKBD_STAT);
261 if ((tmp & KBDSTAT_ENA) == 0) {
262 printf("??? can't enable KBD controller\n");
263 return;
266 printf("\n");
268 sc->pt = pckbport_attach(sc, &sackbc_ops);
271 * Although there is no such thing as SLOT for SA-1111 kbd
272 * controller, pckbd and pms drivers require it.
274 for (slot = PCKBPORT_KBD_SLOT; slot <= PCKBPORT_AUX_SLOT; ++slot) {
275 child = pckbport_attach_slot(self, sc->pt, slot);
277 if (child == NULL)
278 continue;
279 sc->slot = slot;
280 #if NRND > 0
281 rnd_attach_source(&sc->rnd_source, device_xname(child),
282 RND_TYPE_TTY, 0);
283 #endif
284 /* only one of KBD_SLOT or AUX_SLOT is used. */
285 break;
290 static inline int
291 sackbc_wait_output(struct sackbc_softc *sc)
293 u_int i, stat;
295 for (i = 100000; i; i--){
296 stat = bus_space_read_4(sc->iot, sc->ioh, SACCKBD_STAT);
297 delay(100);
298 if (stat & KBDSTAT_TXE)
299 return 1;
301 return 0;
304 static int
305 sackbc_poll_data1(void *cookie, pckbport_slot_t slot)
307 struct sackbc_softc *sc = cookie;
308 int i, s, stat, c = -1;
310 s = spltty();
312 if (sc->polling){
313 stat = sc->poll_stat;
314 c = sc->poll_data;
315 sc->poll_data = -1;
316 sc->poll_stat = -1;
317 if (stat >= 0 &&
318 (stat & (KBDSTAT_RXF|KBDSTAT_STP)) == KBDSTAT_RXF) {
319 splx(s);
320 return c;
324 /* if 1 port read takes 1us (?), this polls for 100ms */
325 for (i = 100000; i; i--) {
326 stat = bus_space_read_4(sc->iot, sc->ioh, SACCKBD_STAT);
327 if ((stat & (KBDSTAT_RXF|KBDSTAT_STP)) == KBDSTAT_RXF) {
328 KBD_DELAY;
329 c = bus_space_read_4(sc->iot, sc->ioh, SACCKBD_DATA);
330 break;
334 splx(s);
335 return c;
338 static int
339 sackbc_send_cmd(void *cookie, pckbport_slot_t slot, u_char val)
341 struct sackbc_softc *sc = cookie;
343 if (!sackbc_wait_output(sc))
344 return 0;
345 bus_space_write_1(sc->iot, sc->ioh, SACCKBD_DATA, val);
346 return 1;
351 * Glue functions for pckbd on sackbc.
352 * These functions emulate those in dev/ic/pckbc.c.
357 * switch scancode translation on / off
358 * return nonzero on success
360 static int
361 sackbc_xt_translation(void *self, pckbport_slot_t slot, int on)
363 /* KBD/Mouse controller doesn't have scancode translation */
364 return !on;
367 static void
368 sackbc_slot_enable(void *self, pckbport_slot_t slot, int on)
370 #if 0
371 struct sackbc_softc *sc = (struct sackbc_softc *) self;
372 int cmd;
374 cmd = on ? KBC_KBDENABLE : KBC_KBDDISABLE;
375 if (!sackbc_send_cmd(sc, cmd))
376 printf("sackbc_slot_enable(%d) failed\n", on);
377 #endif
381 static void
382 sackbc_set_poll(void *self, pckbport_slot_t slot, int on)
384 struct sackbc_softc *sc = (struct sackbc_softc *)self;
385 int s;
387 s = spltty();
389 if (sc->polling != on) {
391 sc->polling = on;
393 if (on) {
394 sc->poll_data = sc->poll_stat = -1;
395 sackbc_disable_intrhandler(sc);
396 } else {
398 * If disabling polling on a device that's
399 * been configured, make sure there are no
400 * bytes left in the FIFO, holding up the
401 * interrupt line. Otherwise we won't get any
402 * further interrupts.
404 sackbc_rxint(sc);
405 sackbc_intr_establish(sc, sc->slot);
408 splx(s);