macekbc.c revision 1.2.8.2 1 1.2.8.2 mjf /* $NetBSD: macekbc.c,v 1.2.8.2 2007/07/11 20:01:45 mjf Exp $ */
2 1.2.8.2 mjf
3 1.2.8.2 mjf /*-
4 1.2.8.2 mjf * Copyright (c) 2007 Jared D. McNeill <jmcneill (at) invisible.ca>
5 1.2.8.2 mjf * All rights reserved.
6 1.2.8.2 mjf *
7 1.2.8.2 mjf * Redistribution and use in source and binary forms, with or without
8 1.2.8.2 mjf * modification, are permitted provided that the following conditions
9 1.2.8.2 mjf * are met:
10 1.2.8.2 mjf * 1. Redistributions of source code must retain the above copyright
11 1.2.8.2 mjf * notice, this list of conditions and the following disclaimer.
12 1.2.8.2 mjf * 2. Redistributions in binary form must reproduce the above copyright
13 1.2.8.2 mjf * notice, this list of conditions and the following disclaimer in the
14 1.2.8.2 mjf * documentation and/or other materials provided with the distribution.
15 1.2.8.2 mjf * 3. All advertising materials mentioning features or use of this software
16 1.2.8.2 mjf * must display the following acknowledgement:
17 1.2.8.2 mjf * This product includes software developed by Jared D. McNeill.
18 1.2.8.2 mjf * 4. Neither the name of The NetBSD Foundation nor the names of its
19 1.2.8.2 mjf * contributors may be used to endorse or promote products derived
20 1.2.8.2 mjf * from this software without specific prior written permission.
21 1.2.8.2 mjf *
22 1.2.8.2 mjf * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
23 1.2.8.2 mjf * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24 1.2.8.2 mjf * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25 1.2.8.2 mjf * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
26 1.2.8.2 mjf * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 1.2.8.2 mjf * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 1.2.8.2 mjf * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 1.2.8.2 mjf * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 1.2.8.2 mjf * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 1.2.8.2 mjf * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 1.2.8.2 mjf * POSSIBILITY OF SUCH DAMAGE.
33 1.2.8.2 mjf */
34 1.2.8.2 mjf
35 1.2.8.2 mjf /*
36 1.2.8.2 mjf * SGI MACE PS2 keyboard/mouse controller driver
37 1.2.8.2 mjf */
38 1.2.8.2 mjf
39 1.2.8.2 mjf #include <sys/cdefs.h>
40 1.2.8.2 mjf __KERNEL_RCSID(0, "$NetBSD: macekbc.c,v 1.2.8.2 2007/07/11 20:01:45 mjf Exp $");
41 1.2.8.2 mjf
42 1.2.8.2 mjf #include <sys/param.h>
43 1.2.8.2 mjf #include <sys/device.h>
44 1.2.8.2 mjf #include <sys/syslog.h>
45 1.2.8.2 mjf #include <sys/malloc.h>
46 1.2.8.2 mjf
47 1.2.8.2 mjf #include <machine/bus.h>
48 1.2.8.2 mjf #include <machine/intr.h>
49 1.2.8.2 mjf
50 1.2.8.2 mjf #include <sgimips/mace/macevar.h>
51 1.2.8.2 mjf
52 1.2.8.2 mjf #include <dev/arcbios/arcbios.h>
53 1.2.8.2 mjf #include <dev/arcbios/arcbiosvar.h>
54 1.2.8.2 mjf #include <dev/pckbport/pckbportvar.h>
55 1.2.8.2 mjf
56 1.2.8.2 mjf #define MACEKBC_TX 0x00
57 1.2.8.2 mjf #define MACEKBC_RX 0x08
58 1.2.8.2 mjf #define MACEKBC_CTRL 0x10
59 1.2.8.2 mjf #define MACEKBC_CTRL_TXCLKOFF (1 << 0)
60 1.2.8.2 mjf #define MACEKBC_CTRL_TXON (1 << 1)
61 1.2.8.2 mjf #define MACEKBC_CTRL_TXINTEN (1 << 2)
62 1.2.8.2 mjf #define MACEKBC_CTRL_RXINTEN (1 << 3)
63 1.2.8.2 mjf #define MACEKBC_CTRL_RXCLKON (1 << 4)
64 1.2.8.2 mjf #define MACEKBC_CTRL_RESET (1 << 5)
65 1.2.8.2 mjf #define MACEKBC_STAT 0x18
66 1.2.8.2 mjf #define MACEKBC_STAT_TXEMPTY (1 << 3)
67 1.2.8.2 mjf #define MACEKBC_STAT_RXFULL (1 << 4)
68 1.2.8.2 mjf
69 1.2.8.2 mjf struct macekbc_softc {
70 1.2.8.2 mjf struct device sc_dev;
71 1.2.8.2 mjf struct macekbc_internal *sc_id;
72 1.2.8.2 mjf
73 1.2.8.2 mjf bus_space_tag_t sc_iot;
74 1.2.8.2 mjf bus_space_handle_t sc_ioh;
75 1.2.8.2 mjf };
76 1.2.8.2 mjf
77 1.2.8.2 mjf struct macekbc_internal {
78 1.2.8.2 mjf struct macekbc_softc *t_sc;
79 1.2.8.2 mjf pckbport_tag_t t_pt;
80 1.2.8.2 mjf
81 1.2.8.2 mjf bus_space_tag_t t_iot;
82 1.2.8.2 mjf bus_space_handle_t t_ioh[PCKBPORT_NSLOTS];
83 1.2.8.2 mjf int t_present[PCKBPORT_NSLOTS];
84 1.2.8.2 mjf
85 1.2.8.2 mjf void *t_rxih;
86 1.2.8.2 mjf };
87 1.2.8.2 mjf
88 1.2.8.2 mjf static int macekbc_intr(void *);
89 1.2.8.2 mjf static void macekbc_reset(struct macekbc_internal *, pckbport_slot_t);
90 1.2.8.2 mjf
91 1.2.8.2 mjf static int macekbc_xt_translation(void *, pckbport_slot_t, int);
92 1.2.8.2 mjf static int macekbc_send_devcmd(void *, pckbport_slot_t, u_char);
93 1.2.8.2 mjf static int macekbc_poll_data1(void *, pckbport_slot_t);
94 1.2.8.2 mjf static void macekbc_slot_enable(void *, pckbport_slot_t, int);
95 1.2.8.2 mjf static void macekbc_intr_establish(void *, pckbport_slot_t);
96 1.2.8.2 mjf static void macekbc_set_poll(void *, pckbport_slot_t, int);
97 1.2.8.2 mjf
98 1.2.8.2 mjf static int macekbc_match(struct device *, struct cfdata *, void *);
99 1.2.8.2 mjf static void macekbc_attach(struct device *, struct device *, void *);
100 1.2.8.2 mjf
101 1.2.8.2 mjf CFATTACH_DECL(macekbc, sizeof(struct macekbc_softc),
102 1.2.8.2 mjf macekbc_match, macekbc_attach, NULL, NULL);
103 1.2.8.2 mjf
104 1.2.8.2 mjf static struct pckbport_accessops macekbc_ops = {
105 1.2.8.2 mjf .t_xt_translation = macekbc_xt_translation,
106 1.2.8.2 mjf .t_send_devcmd = macekbc_send_devcmd,
107 1.2.8.2 mjf .t_poll_data1 = macekbc_poll_data1,
108 1.2.8.2 mjf .t_slot_enable = macekbc_slot_enable,
109 1.2.8.2 mjf .t_intr_establish = macekbc_intr_establish,
110 1.2.8.2 mjf .t_set_poll = macekbc_set_poll,
111 1.2.8.2 mjf };
112 1.2.8.2 mjf
113 1.2.8.2 mjf static int
114 1.2.8.2 mjf macekbc_match(struct device *parent, struct cfdata *match, void *aux)
115 1.2.8.2 mjf {
116 1.2.8.2 mjf const char *consdev;
117 1.2.8.2 mjf
118 1.2.8.2 mjf /* XXX don't bother attaching if we're using a serial console */
119 1.2.8.2 mjf consdev = ARCBIOS->GetEnvironmentVariable("ConsoleIn");
120 1.2.8.2 mjf if (consdev == NULL || strcmp(consdev, "keyboard()") != 0)
121 1.2.8.2 mjf return 0;
122 1.2.8.2 mjf
123 1.2.8.2 mjf return 1;
124 1.2.8.2 mjf }
125 1.2.8.2 mjf
126 1.2.8.2 mjf static void
127 1.2.8.2 mjf macekbc_attach(struct device *parent, struct device *self, void *aux)
128 1.2.8.2 mjf {
129 1.2.8.2 mjf struct mace_attach_args *maa;
130 1.2.8.2 mjf struct macekbc_softc *sc;
131 1.2.8.2 mjf struct macekbc_internal *t;
132 1.2.8.2 mjf int slot;
133 1.2.8.2 mjf
134 1.2.8.2 mjf maa = aux;
135 1.2.8.2 mjf sc = device_private(self);
136 1.2.8.2 mjf
137 1.2.8.2 mjf aprint_normal(": PS2 controller\n");
138 1.2.8.2 mjf aprint_naive("\n");
139 1.2.8.2 mjf
140 1.2.8.2 mjf t = malloc(sizeof(struct macekbc_internal), M_DEVBUF, M_NOWAIT|M_ZERO);
141 1.2.8.2 mjf if (t == NULL) {
142 1.2.8.2 mjf aprint_error("%s: not enough memory\n", device_xname(self));
143 1.2.8.2 mjf return;
144 1.2.8.2 mjf }
145 1.2.8.2 mjf t->t_iot = maa->maa_st;
146 1.2.8.2 mjf for (slot = 0; slot < PCKBPORT_NSLOTS; slot++)
147 1.2.8.2 mjf t->t_present[slot] = 0;
148 1.2.8.2 mjf if (bus_space_subregion(t->t_iot, maa->maa_sh, maa->maa_offset,
149 1.2.8.2 mjf 0, &t->t_ioh[PCKBPORT_KBD_SLOT]) != 0) {
150 1.2.8.2 mjf aprint_error("%s: couldn't map kbd registers\n",
151 1.2.8.2 mjf device_xname(self));
152 1.2.8.2 mjf return;
153 1.2.8.2 mjf }
154 1.2.8.2 mjf if (bus_space_subregion(t->t_iot, maa->maa_sh, maa->maa_offset + 32,
155 1.2.8.2 mjf 0, &t->t_ioh[PCKBPORT_AUX_SLOT]) != 0) {
156 1.2.8.2 mjf aprint_error("%s: couldn't map aux registers\n",
157 1.2.8.2 mjf device_xname(self));
158 1.2.8.2 mjf return;
159 1.2.8.2 mjf }
160 1.2.8.2 mjf
161 1.2.8.2 mjf if ((t->t_rxih = cpu_intr_establish(maa->maa_intr, maa->maa_intrmask,
162 1.2.8.2 mjf macekbc_intr, t)) == NULL) {
163 1.2.8.2 mjf printf("%s: couldn't establish interrupt\n",
164 1.2.8.2 mjf device_xname(self));
165 1.2.8.2 mjf return;
166 1.2.8.2 mjf }
167 1.2.8.2 mjf sc->sc_id = t;
168 1.2.8.2 mjf t->t_sc = sc;
169 1.2.8.2 mjf
170 1.2.8.2 mjf macekbc_reset(t, PCKBPORT_KBD_SLOT);
171 1.2.8.2 mjf macekbc_reset(t, PCKBPORT_AUX_SLOT);
172 1.2.8.2 mjf
173 1.2.8.2 mjf t->t_pt = pckbport_attach(t, &macekbc_ops);
174 1.2.8.2 mjf if (pckbport_attach_slot(&sc->sc_dev, t->t_pt, PCKBPORT_KBD_SLOT))
175 1.2.8.2 mjf t->t_present[PCKBPORT_KBD_SLOT] = 1;
176 1.2.8.2 mjf if (pckbport_attach_slot(&sc->sc_dev, t->t_pt, PCKBPORT_AUX_SLOT))
177 1.2.8.2 mjf t->t_present[PCKBPORT_AUX_SLOT] = 1;
178 1.2.8.2 mjf
179 1.2.8.2 mjf return;
180 1.2.8.2 mjf }
181 1.2.8.2 mjf
182 1.2.8.2 mjf static int
183 1.2.8.2 mjf macekbc_intr(void *opaque)
184 1.2.8.2 mjf {
185 1.2.8.2 mjf struct macekbc_internal *t;
186 1.2.8.2 mjf bus_space_tag_t iot;
187 1.2.8.2 mjf bus_space_handle_t ioh;
188 1.2.8.2 mjf uint64_t stat, val;
189 1.2.8.2 mjf pckbport_slot_t slot;
190 1.2.8.2 mjf int rv;
191 1.2.8.2 mjf
192 1.2.8.2 mjf t = opaque;
193 1.2.8.2 mjf iot = t->t_iot;
194 1.2.8.2 mjf rv = 0;
195 1.2.8.2 mjf
196 1.2.8.2 mjf for (slot = 0; slot < PCKBPORT_NSLOTS; slot++) {
197 1.2.8.2 mjf if (t->t_present[slot] == 0)
198 1.2.8.2 mjf continue;
199 1.2.8.2 mjf
200 1.2.8.2 mjf ioh = t->t_ioh[slot];
201 1.2.8.2 mjf stat = bus_space_read_8(iot, ioh, MACEKBC_STAT);
202 1.2.8.2 mjf if (stat & MACEKBC_STAT_RXFULL) {
203 1.2.8.2 mjf val = bus_space_read_8(iot, ioh, MACEKBC_RX);
204 1.2.8.2 mjf pckbportintr(t->t_pt, slot, val & 0xff);
205 1.2.8.2 mjf rv = 1;
206 1.2.8.2 mjf }
207 1.2.8.2 mjf }
208 1.2.8.2 mjf
209 1.2.8.2 mjf return rv;
210 1.2.8.2 mjf }
211 1.2.8.2 mjf
212 1.2.8.2 mjf static void
213 1.2.8.2 mjf macekbc_reset(struct macekbc_internal *t, pckbport_slot_t slot)
214 1.2.8.2 mjf {
215 1.2.8.2 mjf bus_space_tag_t iot;
216 1.2.8.2 mjf bus_space_handle_t ioh;
217 1.2.8.2 mjf uint64_t val;
218 1.2.8.2 mjf
219 1.2.8.2 mjf iot = t->t_iot;
220 1.2.8.2 mjf ioh = t->t_ioh[slot];
221 1.2.8.2 mjf
222 1.2.8.2 mjf val = bus_space_read_8(iot, ioh, MACEKBC_CTRL);
223 1.2.8.2 mjf val |= MACEKBC_CTRL_TXCLKOFF | MACEKBC_CTRL_RESET;
224 1.2.8.2 mjf bus_space_write_8(iot, ioh, MACEKBC_CTRL, val);
225 1.2.8.2 mjf
226 1.2.8.2 mjf delay(10000);
227 1.2.8.2 mjf
228 1.2.8.2 mjf val &= ~(MACEKBC_CTRL_TXCLKOFF | MACEKBC_CTRL_RESET);
229 1.2.8.2 mjf val |= MACEKBC_CTRL_TXON | MACEKBC_CTRL_RXCLKON | MACEKBC_CTRL_RXINTEN;
230 1.2.8.2 mjf bus_space_write_8(iot, ioh, MACEKBC_CTRL, val);
231 1.2.8.2 mjf
232 1.2.8.2 mjf return;
233 1.2.8.2 mjf }
234 1.2.8.2 mjf
235 1.2.8.2 mjf static int
236 1.2.8.2 mjf macekbc_wait(struct macekbc_internal *t, pckbport_slot_t slot,
237 1.2.8.2 mjf uint64_t mask, bool set)
238 1.2.8.2 mjf {
239 1.2.8.2 mjf bus_space_tag_t iot;
240 1.2.8.2 mjf bus_space_handle_t ioh;
241 1.2.8.2 mjf uint64_t val, tmp;
242 1.2.8.2 mjf int timeout;
243 1.2.8.2 mjf
244 1.2.8.2 mjf iot = t->t_iot;
245 1.2.8.2 mjf ioh = t->t_ioh[slot];
246 1.2.8.2 mjf val = (set ? mask : 0);
247 1.2.8.2 mjf timeout = 1000;
248 1.2.8.2 mjf
249 1.2.8.2 mjf while (timeout-- > 0) {
250 1.2.8.2 mjf tmp = bus_space_read_8(iot, ioh, MACEKBC_STAT);
251 1.2.8.2 mjf if ((tmp & mask) == val)
252 1.2.8.2 mjf return 0;
253 1.2.8.2 mjf delay(10);
254 1.2.8.2 mjf }
255 1.2.8.2 mjf
256 1.2.8.2 mjf return 1;
257 1.2.8.2 mjf }
258 1.2.8.2 mjf
259 1.2.8.2 mjf static int
260 1.2.8.2 mjf macekbc_xt_translation(void *opaque, pckbport_slot_t port, int on)
261 1.2.8.2 mjf {
262 1.2.8.2 mjf
263 1.2.8.2 mjf if (on)
264 1.2.8.2 mjf return 0;
265 1.2.8.2 mjf
266 1.2.8.2 mjf return 1;
267 1.2.8.2 mjf }
268 1.2.8.2 mjf
269 1.2.8.2 mjf static int
270 1.2.8.2 mjf macekbc_send_devcmd(void *opaque, pckbport_slot_t slot, u_char byte)
271 1.2.8.2 mjf {
272 1.2.8.2 mjf struct macekbc_internal *t;
273 1.2.8.2 mjf bus_space_tag_t iot;
274 1.2.8.2 mjf bus_space_handle_t ioh;
275 1.2.8.2 mjf
276 1.2.8.2 mjf t = opaque;
277 1.2.8.2 mjf iot = t->t_iot;
278 1.2.8.2 mjf ioh = t->t_ioh[slot];
279 1.2.8.2 mjf
280 1.2.8.2 mjf if (macekbc_wait(t, slot, MACEKBC_STAT_TXEMPTY, 1))
281 1.2.8.2 mjf return 0;
282 1.2.8.2 mjf
283 1.2.8.2 mjf bus_space_write_8(iot, ioh, MACEKBC_TX, byte & 0xff);
284 1.2.8.2 mjf
285 1.2.8.2 mjf return 1;
286 1.2.8.2 mjf }
287 1.2.8.2 mjf
288 1.2.8.2 mjf static int
289 1.2.8.2 mjf macekbc_poll_data1(void *opaque, pckbport_slot_t slot)
290 1.2.8.2 mjf {
291 1.2.8.2 mjf struct macekbc_internal *t;
292 1.2.8.2 mjf bus_space_tag_t iot;
293 1.2.8.2 mjf bus_space_handle_t ioh;
294 1.2.8.2 mjf
295 1.2.8.2 mjf t = opaque;
296 1.2.8.2 mjf iot = t->t_iot;
297 1.2.8.2 mjf ioh = t->t_ioh[slot];
298 1.2.8.2 mjf
299 1.2.8.2 mjf if (macekbc_wait(t, slot, MACEKBC_STAT_RXFULL, 1)) /* rx full */
300 1.2.8.2 mjf return -1;
301 1.2.8.2 mjf
302 1.2.8.2 mjf return bus_space_read_8(iot, ioh, MACEKBC_RX) & 0xff;
303 1.2.8.2 mjf }
304 1.2.8.2 mjf
305 1.2.8.2 mjf static void
306 1.2.8.2 mjf macekbc_slot_enable(void *opaque, pckbport_slot_t slot, int on)
307 1.2.8.2 mjf {
308 1.2.8.2 mjf struct macekbc_internal *t;
309 1.2.8.2 mjf bus_space_tag_t iot;
310 1.2.8.2 mjf bus_space_handle_t ioh;
311 1.2.8.2 mjf uint64_t val;
312 1.2.8.2 mjf
313 1.2.8.2 mjf t = opaque;
314 1.2.8.2 mjf iot = t->t_iot;
315 1.2.8.2 mjf ioh = t->t_ioh[slot];
316 1.2.8.2 mjf
317 1.2.8.2 mjf val = bus_space_read_8(iot, ioh, MACEKBC_CTRL);
318 1.2.8.2 mjf if (on)
319 1.2.8.2 mjf val |= MACEKBC_CTRL_TXON | MACEKBC_CTRL_RXCLKON;
320 1.2.8.2 mjf else
321 1.2.8.2 mjf val &= ~(MACEKBC_CTRL_TXON | MACEKBC_CTRL_RXCLKON);
322 1.2.8.2 mjf bus_space_write_8(iot, ioh, MACEKBC_CTRL, val);
323 1.2.8.2 mjf
324 1.2.8.2 mjf return;
325 1.2.8.2 mjf }
326 1.2.8.2 mjf
327 1.2.8.2 mjf static void
328 1.2.8.2 mjf macekbc_intr_establish(void *opaque, pckbport_slot_t slot)
329 1.2.8.2 mjf {
330 1.2.8.2 mjf
331 1.2.8.2 mjf /* XXX */
332 1.2.8.2 mjf macekbc_set_poll(opaque, slot, 0);
333 1.2.8.2 mjf
334 1.2.8.2 mjf return;
335 1.2.8.2 mjf }
336 1.2.8.2 mjf
337 1.2.8.2 mjf static void
338 1.2.8.2 mjf macekbc_set_poll(void *opaque, pckbport_slot_t slot, int on)
339 1.2.8.2 mjf {
340 1.2.8.2 mjf struct macekbc_internal *t;
341 1.2.8.2 mjf bus_space_tag_t iot;
342 1.2.8.2 mjf bus_space_handle_t ioh;
343 1.2.8.2 mjf uint64_t val;
344 1.2.8.2 mjf
345 1.2.8.2 mjf t = opaque;
346 1.2.8.2 mjf iot = t->t_iot;
347 1.2.8.2 mjf ioh = t->t_ioh[slot];
348 1.2.8.2 mjf
349 1.2.8.2 mjf val = bus_space_read_8(iot, ioh, MACEKBC_CTRL);
350 1.2.8.2 mjf if (on)
351 1.2.8.2 mjf val &= ~MACEKBC_CTRL_RXINTEN;
352 1.2.8.2 mjf else
353 1.2.8.2 mjf val |= MACEKBC_CTRL_RXINTEN;
354 1.2.8.2 mjf bus_space_write_8(iot, ioh, MACEKBC_CTRL, val);
355 1.2.8.2 mjf
356 1.2.8.2 mjf return;
357 1.2.8.2 mjf }
358