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