1 /* $NetBSD: sa1111_kbc.c,v 1.10 2008/01/05 00:31:55 ad Exp $ */
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
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
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.
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
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>
69 #include <sys/device.h>
70 #include <sys/malloc.h>
71 #include <sys/errno.h>
72 #include <sys/queue.h>
75 #include <arm/sa11x0/sa1111_reg.h>
76 #include <arm/sa11x0/sa1111_var.h>
78 #include <dev/pckbport/pckbportvar.h> /* for prototypes */
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 */
99 rndsource_element_t rnd_source
;
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
,
123 sackbc_intr_establish
,
127 #define KBD_DELAY DELAY(8)
129 /*#define SACKBCDEBUG*/
132 #define DPRINTF(arg) printf arg
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
) {
153 sackbc_txint(void *cookie
)
155 struct sackbc_softc
*sc
= cookie
;
157 bus_space_read_4(sc
->iot
, sc
->ioh
, SACCKBD_STAT
);
164 sackbc_rxint(void *cookie
)
166 struct sackbc_softc
*sc
= cookie
;
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
);
175 rnd_add_uint32(&sc
->rnd_source
, (stat
<<8)|data
);
179 sc
->poll_data
= code
;
180 sc
->poll_stat
= stat
;
182 pckbportintr(sc
->pt
, sc
->slot
, code
);
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");
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
),
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
;
222 uint32_t tmp
, clock_bit
;
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;
232 if (aa
->sa_size
<= 0)
233 aa
->sa_size
= SACCKBD_SIZE
;
234 if (aa
->sa_intr
== SACCCF_INTR_DEFAULT
)
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");
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
,
251 sc
->intr
= aa
->sa_intr
;
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");
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
);
281 rnd_attach_source(&sc
->rnd_source
, device_xname(child
),
284 /* only one of KBD_SLOT or AUX_SLOT is used. */
291 sackbc_wait_output(struct sackbc_softc
*sc
)
295 for (i
= 100000; i
; i
--){
296 stat
= bus_space_read_4(sc
->iot
, sc
->ioh
, SACCKBD_STAT
);
298 if (stat
& KBDSTAT_TXE
)
305 sackbc_poll_data1(void *cookie
, pckbport_slot_t slot
)
307 struct sackbc_softc
*sc
= cookie
;
308 int i
, s
, stat
, c
= -1;
313 stat
= sc
->poll_stat
;
318 (stat
& (KBDSTAT_RXF
|KBDSTAT_STP
)) == KBDSTAT_RXF
) {
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
) {
329 c
= bus_space_read_4(sc
->iot
, sc
->ioh
, SACCKBD_DATA
);
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
))
345 bus_space_write_1(sc
->iot
, sc
->ioh
, SACCKBD_DATA
, val
);
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
361 sackbc_xt_translation(void *self
, pckbport_slot_t slot
, int on
)
363 /* KBD/Mouse controller doesn't have scancode translation */
368 sackbc_slot_enable(void *self
, pckbport_slot_t slot
, int on
)
371 struct sackbc_softc
*sc
= (struct sackbc_softc
*) self
;
374 cmd
= on
? KBC_KBDENABLE
: KBC_KBDDISABLE
;
375 if (!sackbc_send_cmd(sc
, cmd
))
376 printf("sackbc_slot_enable(%d) failed\n", on
);
382 sackbc_set_poll(void *self
, pckbport_slot_t slot
, int on
)
384 struct sackbc_softc
*sc
= (struct sackbc_softc
*)self
;
389 if (sc
->polling
!= on
) {
394 sc
->poll_data
= sc
->poll_stat
= -1;
395 sackbc_disable_intrhandler(sc
);
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.
405 sackbc_intr_establish(sc
, sc
->slot
);