1 /* $NetBSD: macekbc.c,v 1.3 2008/03/08 15:04:33 tnn Exp $ */
4 * Copyright (c) 2007 Jared D. McNeill <jmcneill@invisible.ca>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
30 * SGI MACE PS2 keyboard/mouse controller driver
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: macekbc.c,v 1.3 2008/03/08 15:04:33 tnn Exp $");
36 #include <sys/param.h>
37 #include <sys/device.h>
38 #include <sys/syslog.h>
39 #include <sys/malloc.h>
41 #include <machine/bus.h>
42 #include <machine/intr.h>
44 #include <sgimips/mace/macevar.h>
46 #include <dev/arcbios/arcbios.h>
47 #include <dev/arcbios/arcbiosvar.h>
48 #include <dev/pckbport/pckbportvar.h>
50 #define MACEKBC_TX 0x00
51 #define MACEKBC_RX 0x08
52 #define MACEKBC_CTRL 0x10
53 #define MACEKBC_CTRL_TXCLKOFF (1 << 0)
54 #define MACEKBC_CTRL_TXON (1 << 1)
55 #define MACEKBC_CTRL_TXINTEN (1 << 2)
56 #define MACEKBC_CTRL_RXINTEN (1 << 3)
57 #define MACEKBC_CTRL_RXCLKON (1 << 4)
58 #define MACEKBC_CTRL_RESET (1 << 5)
59 #define MACEKBC_STAT 0x18
60 #define MACEKBC_STAT_TXEMPTY (1 << 3)
61 #define MACEKBC_STAT_RXFULL (1 << 4)
63 struct macekbc_softc
{
65 struct macekbc_internal
*sc_id
;
67 bus_space_tag_t sc_iot
;
68 bus_space_handle_t sc_ioh
;
71 struct macekbc_internal
{
72 struct macekbc_softc
*t_sc
;
75 bus_space_tag_t t_iot
;
76 bus_space_handle_t t_ioh
[PCKBPORT_NSLOTS
];
77 int t_present
[PCKBPORT_NSLOTS
];
82 static int macekbc_intr(void *);
83 static void macekbc_reset(struct macekbc_internal
*, pckbport_slot_t
);
85 static int macekbc_xt_translation(void *, pckbport_slot_t
, int);
86 static int macekbc_send_devcmd(void *, pckbport_slot_t
, u_char
);
87 static int macekbc_poll_data1(void *, pckbport_slot_t
);
88 static void macekbc_slot_enable(void *, pckbport_slot_t
, int);
89 static void macekbc_intr_establish(void *, pckbport_slot_t
);
90 static void macekbc_set_poll(void *, pckbport_slot_t
, int);
92 static int macekbc_match(struct device
*, struct cfdata
*, void *);
93 static void macekbc_attach(struct device
*, struct device
*, void *);
95 CFATTACH_DECL(macekbc
, sizeof(struct macekbc_softc
),
96 macekbc_match
, macekbc_attach
, NULL
, NULL
);
98 static struct pckbport_accessops macekbc_ops
= {
99 .t_xt_translation
= macekbc_xt_translation
,
100 .t_send_devcmd
= macekbc_send_devcmd
,
101 .t_poll_data1
= macekbc_poll_data1
,
102 .t_slot_enable
= macekbc_slot_enable
,
103 .t_intr_establish
= macekbc_intr_establish
,
104 .t_set_poll
= macekbc_set_poll
,
108 macekbc_match(struct device
*parent
, struct cfdata
*match
, void *aux
)
115 macekbc_attach(struct device
*parent
, struct device
*self
, void *aux
)
117 struct mace_attach_args
*maa
;
118 struct macekbc_softc
*sc
;
119 struct macekbc_internal
*t
;
124 sc
= device_private(self
);
126 aprint_normal(": PS2 controller\n");
129 t
= malloc(sizeof(struct macekbc_internal
), M_DEVBUF
, M_NOWAIT
|M_ZERO
);
131 aprint_error("%s: not enough memory\n", device_xname(self
));
134 t
->t_iot
= maa
->maa_st
;
135 for (slot
= 0; slot
< PCKBPORT_NSLOTS
; slot
++)
136 t
->t_present
[slot
] = 0;
137 if (bus_space_subregion(t
->t_iot
, maa
->maa_sh
, maa
->maa_offset
,
138 0, &t
->t_ioh
[PCKBPORT_KBD_SLOT
]) != 0) {
139 aprint_error("%s: couldn't map kbd registers\n",
143 if (bus_space_subregion(t
->t_iot
, maa
->maa_sh
, maa
->maa_offset
+ 32,
144 0, &t
->t_ioh
[PCKBPORT_AUX_SLOT
]) != 0) {
145 aprint_error("%s: couldn't map aux registers\n",
150 if ((t
->t_rxih
= cpu_intr_establish(maa
->maa_intr
, maa
->maa_intrmask
,
151 macekbc_intr
, t
)) == NULL
) {
152 printf("%s: couldn't establish interrupt\n",
159 macekbc_reset(t
, PCKBPORT_KBD_SLOT
);
160 macekbc_reset(t
, PCKBPORT_AUX_SLOT
);
162 consdev
= ARCBIOS
->GetEnvironmentVariable("ConsoleIn");
163 if (consdev
!= NULL
&& strcmp(consdev
, "keyboard()") == 0)
164 pckbport_cnattach(t
, &macekbc_ops
, PCKBPORT_KBD_SLOT
);
166 t
->t_pt
= pckbport_attach(t
, &macekbc_ops
);
167 if (pckbport_attach_slot(&sc
->sc_dev
, t
->t_pt
, PCKBPORT_KBD_SLOT
))
168 t
->t_present
[PCKBPORT_KBD_SLOT
] = 1;
169 if (pckbport_attach_slot(&sc
->sc_dev
, t
->t_pt
, PCKBPORT_AUX_SLOT
))
170 t
->t_present
[PCKBPORT_AUX_SLOT
] = 1;
176 macekbc_intr(void *opaque
)
178 struct macekbc_internal
*t
;
180 bus_space_handle_t ioh
;
182 pckbport_slot_t slot
;
189 for (slot
= 0; slot
< PCKBPORT_NSLOTS
; slot
++) {
190 if (t
->t_present
[slot
] == 0)
193 ioh
= t
->t_ioh
[slot
];
194 stat
= bus_space_read_8(iot
, ioh
, MACEKBC_STAT
);
195 if (stat
& MACEKBC_STAT_RXFULL
) {
196 val
= bus_space_read_8(iot
, ioh
, MACEKBC_RX
);
197 pckbportintr(t
->t_pt
, slot
, val
& 0xff);
206 macekbc_reset(struct macekbc_internal
*t
, pckbport_slot_t slot
)
209 bus_space_handle_t ioh
;
213 ioh
= t
->t_ioh
[slot
];
215 val
= bus_space_read_8(iot
, ioh
, MACEKBC_CTRL
);
216 val
|= MACEKBC_CTRL_TXCLKOFF
| MACEKBC_CTRL_RESET
;
217 bus_space_write_8(iot
, ioh
, MACEKBC_CTRL
, val
);
221 val
&= ~(MACEKBC_CTRL_TXCLKOFF
| MACEKBC_CTRL_RESET
);
222 val
|= MACEKBC_CTRL_TXON
| MACEKBC_CTRL_RXCLKON
| MACEKBC_CTRL_RXINTEN
;
223 bus_space_write_8(iot
, ioh
, MACEKBC_CTRL
, val
);
229 macekbc_wait(struct macekbc_internal
*t
, pckbport_slot_t slot
,
230 uint64_t mask
, bool set
)
233 bus_space_handle_t ioh
;
238 ioh
= t
->t_ioh
[slot
];
239 val
= (set
? mask
: 0);
242 while (timeout
-- > 0) {
243 tmp
= bus_space_read_8(iot
, ioh
, MACEKBC_STAT
);
244 if ((tmp
& mask
) == val
)
253 macekbc_xt_translation(void *opaque
, pckbport_slot_t port
, int on
)
263 macekbc_send_devcmd(void *opaque
, pckbport_slot_t slot
, u_char byte
)
265 struct macekbc_internal
*t
;
267 bus_space_handle_t ioh
;
271 ioh
= t
->t_ioh
[slot
];
273 if (macekbc_wait(t
, slot
, MACEKBC_STAT_TXEMPTY
, 1))
276 bus_space_write_8(iot
, ioh
, MACEKBC_TX
, byte
& 0xff);
282 macekbc_poll_data1(void *opaque
, pckbport_slot_t slot
)
284 struct macekbc_internal
*t
;
286 bus_space_handle_t ioh
;
290 ioh
= t
->t_ioh
[slot
];
292 if (macekbc_wait(t
, slot
, MACEKBC_STAT_RXFULL
, 1)) /* rx full */
295 return bus_space_read_8(iot
, ioh
, MACEKBC_RX
) & 0xff;
299 macekbc_slot_enable(void *opaque
, pckbport_slot_t slot
, int on
)
301 struct macekbc_internal
*t
;
303 bus_space_handle_t ioh
;
308 ioh
= t
->t_ioh
[slot
];
310 val
= bus_space_read_8(iot
, ioh
, MACEKBC_CTRL
);
312 val
|= MACEKBC_CTRL_TXON
| MACEKBC_CTRL_RXCLKON
;
314 val
&= ~(MACEKBC_CTRL_TXON
| MACEKBC_CTRL_RXCLKON
);
315 bus_space_write_8(iot
, ioh
, MACEKBC_CTRL
, val
);
321 macekbc_intr_establish(void *opaque
, pckbport_slot_t slot
)
325 macekbc_set_poll(opaque
, slot
, 0);
331 macekbc_set_poll(void *opaque
, pckbport_slot_t slot
, int on
)
333 struct macekbc_internal
*t
;
335 bus_space_handle_t ioh
;
340 ioh
= t
->t_ioh
[slot
];
342 val
= bus_space_read_8(iot
, ioh
, MACEKBC_CTRL
);
344 val
&= ~MACEKBC_CTRL_RXINTEN
;
346 val
|= MACEKBC_CTRL_RXINTEN
;
347 bus_space_write_8(iot
, ioh
, MACEKBC_CTRL
, val
);