macekbc.c revision 1.6 1 /* $NetBSD: macekbc.c,v 1.6 2011/07/01 18:53:47 dyoung 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 *
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.
27 */
28
29 /*
30 * SGI MACE PS2 keyboard/mouse controller driver
31 */
32
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: macekbc.c,v 1.6 2011/07/01 18:53:47 dyoung Exp $");
35
36 #include <sys/param.h>
37 #include <sys/device.h>
38 #include <sys/syslog.h>
39 #include <sys/malloc.h>
40
41 #include <sys/bus.h>
42 #include <machine/intr.h>
43
44 #include <sgimips/mace/macevar.h>
45
46 #include <dev/arcbios/arcbios.h>
47 #include <dev/arcbios/arcbiosvar.h>
48 #include <dev/pckbport/pckbportvar.h>
49
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)
62
63 struct macekbc_softc {
64 struct device sc_dev;
65 struct macekbc_internal *sc_id;
66
67 bus_space_tag_t sc_iot;
68 bus_space_handle_t sc_ioh;
69 };
70
71 struct macekbc_internal {
72 struct macekbc_softc *t_sc;
73 pckbport_tag_t t_pt;
74
75 bus_space_tag_t t_iot;
76 bus_space_handle_t t_ioh[PCKBPORT_NSLOTS];
77 int t_present[PCKBPORT_NSLOTS];
78
79 void *t_rxih;
80 };
81
82 static int macekbc_intr(void *);
83 static void macekbc_reset(struct macekbc_internal *, pckbport_slot_t);
84
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);
91
92 static int macekbc_match(struct device *, struct cfdata *, void *);
93 static void macekbc_attach(struct device *, struct device *, void *);
94
95 CFATTACH_DECL(macekbc, sizeof(struct macekbc_softc),
96 macekbc_match, macekbc_attach, NULL, NULL);
97
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,
105 };
106
107 static int
108 macekbc_match(struct device *parent, struct cfdata *match, void *aux)
109 {
110
111 return 1;
112 }
113
114 static void
115 macekbc_attach(struct device *parent, struct device *self, void *aux)
116 {
117 struct mace_attach_args *maa;
118 struct macekbc_softc *sc;
119 struct macekbc_internal *t;
120 int slot;
121 const char *consdev;
122
123 maa = aux;
124 sc = device_private(self);
125
126 aprint_normal(": PS2 controller\n");
127 aprint_naive("\n");
128
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;
133 }
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;
142 }
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;
148 }
149
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;
155 }
156 sc->sc_id = t;
157 t->t_sc = sc;
158
159 macekbc_reset(t, PCKBPORT_KBD_SLOT);
160 macekbc_reset(t, PCKBPORT_AUX_SLOT);
161
162 consdev = arcbios_GetEnvironmentVariable("ConsoleIn");
163 if (consdev != NULL && strcmp(consdev, "keyboard()") == 0)
164 pckbport_cnattach(t, &macekbc_ops, PCKBPORT_KBD_SLOT);
165
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;
171
172 return;
173 }
174
175 static int
176 macekbc_intr(void *opaque)
177 {
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;
184
185 t = opaque;
186 iot = t->t_iot;
187 rv = 0;
188
189 for (slot = 0; slot < PCKBPORT_NSLOTS; slot++) {
190 if (t->t_present[slot] == 0)
191 continue;
192
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;
199 }
200 }
201
202 return rv;
203 }
204
205 static void
206 macekbc_reset(struct macekbc_internal *t, pckbport_slot_t slot)
207 {
208 bus_space_tag_t iot;
209 bus_space_handle_t ioh;
210 uint64_t val;
211
212 iot = t->t_iot;
213 ioh = t->t_ioh[slot];
214
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);
218
219 delay(10000);
220
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);
224
225 return;
226 }
227
228 static int
229 macekbc_wait(struct macekbc_internal *t, pckbport_slot_t slot,
230 uint64_t mask, bool set)
231 {
232 bus_space_tag_t iot;
233 bus_space_handle_t ioh;
234 uint64_t val, tmp;
235 int timeout;
236
237 iot = t->t_iot;
238 ioh = t->t_ioh[slot];
239 val = (set ? mask : 0);
240 timeout = 1000;
241
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);
247 }
248
249 return 1;
250 }
251
252 static int
253 macekbc_xt_translation(void *opaque, pckbport_slot_t port, int on)
254 {
255
256 if (on)
257 return 0;
258
259 return 1;
260 }
261
262 static int
263 macekbc_send_devcmd(void *opaque, pckbport_slot_t slot, u_char byte)
264 {
265 struct macekbc_internal *t;
266 bus_space_tag_t iot;
267 bus_space_handle_t ioh;
268
269 t = opaque;
270 iot = t->t_iot;
271 ioh = t->t_ioh[slot];
272
273 if (macekbc_wait(t, slot, MACEKBC_STAT_TXEMPTY, 1))
274 return 0;
275
276 bus_space_write_8(iot, ioh, MACEKBC_TX, byte & 0xff);
277
278 return 1;
279 }
280
281 static int
282 macekbc_poll_data1(void *opaque, pckbport_slot_t slot)
283 {
284 struct macekbc_internal *t;
285 bus_space_tag_t iot;
286 bus_space_handle_t ioh;
287
288 t = opaque;
289 iot = t->t_iot;
290 ioh = t->t_ioh[slot];
291
292 if (macekbc_wait(t, slot, MACEKBC_STAT_RXFULL, 1)) /* rx full */
293 return -1;
294
295 return bus_space_read_8(iot, ioh, MACEKBC_RX) & 0xff;
296 }
297
298 static void
299 macekbc_slot_enable(void *opaque, pckbport_slot_t slot, int on)
300 {
301 struct macekbc_internal *t;
302 bus_space_tag_t iot;
303 bus_space_handle_t ioh;
304 uint64_t val;
305
306 t = opaque;
307 iot = t->t_iot;
308 ioh = t->t_ioh[slot];
309
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);
316
317 return;
318 }
319
320 static void
321 macekbc_intr_establish(void *opaque, pckbport_slot_t slot)
322 {
323
324 /* XXX */
325 macekbc_set_poll(opaque, slot, 0);
326
327 return;
328 }
329
330 static void
331 macekbc_set_poll(void *opaque, pckbport_slot_t slot, int on)
332 {
333 struct macekbc_internal *t;
334 bus_space_tag_t iot;
335 bus_space_handle_t ioh;
336 uint64_t val;
337
338 t = opaque;
339 iot = t->t_iot;
340 ioh = t->t_ioh[slot];
341
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);
348
349 return;
350 }
351