Expand PMF_FN_* macros.
[netbsd-mini2440.git] / sys / arch / sgimips / mace / macekbc.c
blob8a9a3d4a1769b28d9f64e24e3fcc94a9a98b81b0
1 /* $NetBSD: macekbc.c,v 1.3 2008/03/08 15:04:33 tnn Exp $ */
3 /*-
4 * Copyright (c) 2007 Jared D. McNeill <jmcneill@invisible.ca>
5 * All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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 {
64 struct device sc_dev;
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;
73 pckbport_tag_t t_pt;
75 bus_space_tag_t t_iot;
76 bus_space_handle_t t_ioh[PCKBPORT_NSLOTS];
77 int t_present[PCKBPORT_NSLOTS];
79 void *t_rxih;
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,
107 static int
108 macekbc_match(struct device *parent, struct cfdata *match, void *aux)
111 return 1;
114 static void
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;
120 int slot;
121 const char *consdev;
123 maa = aux;
124 sc = device_private(self);
126 aprint_normal(": PS2 controller\n");
127 aprint_naive("\n");
129 t = malloc(sizeof(struct macekbc_internal), M_DEVBUF, M_NOWAIT|M_ZERO);
130 if (t == NULL) {
131 aprint_error("%s: not enough memory\n", device_xname(self));
132 return;
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",
140 device_xname(self));
141 return;
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",
146 device_xname(self));
147 return;
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",
153 device_xname(self));
154 return;
156 sc->sc_id = t;
157 t->t_sc = sc;
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;
172 return;
175 static int
176 macekbc_intr(void *opaque)
178 struct macekbc_internal *t;
179 bus_space_tag_t iot;
180 bus_space_handle_t ioh;
181 uint64_t stat, val;
182 pckbport_slot_t slot;
183 int rv;
185 t = opaque;
186 iot = t->t_iot;
187 rv = 0;
189 for (slot = 0; slot < PCKBPORT_NSLOTS; slot++) {
190 if (t->t_present[slot] == 0)
191 continue;
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);
198 rv = 1;
202 return rv;
205 static void
206 macekbc_reset(struct macekbc_internal *t, pckbport_slot_t slot)
208 bus_space_tag_t iot;
209 bus_space_handle_t ioh;
210 uint64_t val;
212 iot = t->t_iot;
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);
219 delay(10000);
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);
225 return;
228 static int
229 macekbc_wait(struct macekbc_internal *t, pckbport_slot_t slot,
230 uint64_t mask, bool set)
232 bus_space_tag_t iot;
233 bus_space_handle_t ioh;
234 uint64_t val, tmp;
235 int timeout;
237 iot = t->t_iot;
238 ioh = t->t_ioh[slot];
239 val = (set ? mask : 0);
240 timeout = 1000;
242 while (timeout-- > 0) {
243 tmp = bus_space_read_8(iot, ioh, MACEKBC_STAT);
244 if ((tmp & mask) == val)
245 return 0;
246 delay(10);
249 return 1;
252 static int
253 macekbc_xt_translation(void *opaque, pckbport_slot_t port, int on)
256 if (on)
257 return 0;
259 return 1;
262 static int
263 macekbc_send_devcmd(void *opaque, pckbport_slot_t slot, u_char byte)
265 struct macekbc_internal *t;
266 bus_space_tag_t iot;
267 bus_space_handle_t ioh;
269 t = opaque;
270 iot = t->t_iot;
271 ioh = t->t_ioh[slot];
273 if (macekbc_wait(t, slot, MACEKBC_STAT_TXEMPTY, 1))
274 return 0;
276 bus_space_write_8(iot, ioh, MACEKBC_TX, byte & 0xff);
278 return 1;
281 static int
282 macekbc_poll_data1(void *opaque, pckbport_slot_t slot)
284 struct macekbc_internal *t;
285 bus_space_tag_t iot;
286 bus_space_handle_t ioh;
288 t = opaque;
289 iot = t->t_iot;
290 ioh = t->t_ioh[slot];
292 if (macekbc_wait(t, slot, MACEKBC_STAT_RXFULL, 1)) /* rx full */
293 return -1;
295 return bus_space_read_8(iot, ioh, MACEKBC_RX) & 0xff;
298 static void
299 macekbc_slot_enable(void *opaque, pckbport_slot_t slot, int on)
301 struct macekbc_internal *t;
302 bus_space_tag_t iot;
303 bus_space_handle_t ioh;
304 uint64_t val;
306 t = opaque;
307 iot = t->t_iot;
308 ioh = t->t_ioh[slot];
310 val = bus_space_read_8(iot, ioh, MACEKBC_CTRL);
311 if (on)
312 val |= MACEKBC_CTRL_TXON | MACEKBC_CTRL_RXCLKON;
313 else
314 val &= ~(MACEKBC_CTRL_TXON | MACEKBC_CTRL_RXCLKON);
315 bus_space_write_8(iot, ioh, MACEKBC_CTRL, val);
317 return;
320 static void
321 macekbc_intr_establish(void *opaque, pckbport_slot_t slot)
324 /* XXX */
325 macekbc_set_poll(opaque, slot, 0);
327 return;
330 static void
331 macekbc_set_poll(void *opaque, pckbport_slot_t slot, int on)
333 struct macekbc_internal *t;
334 bus_space_tag_t iot;
335 bus_space_handle_t ioh;
336 uint64_t val;
338 t = opaque;
339 iot = t->t_iot;
340 ioh = t->t_ioh[slot];
342 val = bus_space_read_8(iot, ioh, MACEKBC_CTRL);
343 if (on)
344 val &= ~MACEKBC_CTRL_RXINTEN;
345 else
346 val |= MACEKBC_CTRL_RXINTEN;
347 bus_space_write_8(iot, ioh, MACEKBC_CTRL, val);
349 return;