macekbc.c revision 1.3 1 /* $NetBSD: macekbc.c,v 1.3 2008/03/08 15:04:33 tnn 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.3 2008/03/08 15:04:33 tnn 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
117 return 1;
118 }
119
120 static void
121 macekbc_attach(struct device *parent, struct device *self, void *aux)
122 {
123 struct mace_attach_args *maa;
124 struct macekbc_softc *sc;
125 struct macekbc_internal *t;
126 int slot;
127 const char *consdev;
128
129 maa = aux;
130 sc = device_private(self);
131
132 aprint_normal(": PS2 controller\n");
133 aprint_naive("\n");
134
135 t = malloc(sizeof(struct macekbc_internal), M_DEVBUF, M_NOWAIT|M_ZERO);
136 if (t == NULL) {
137 aprint_error("%s: not enough memory\n", device_xname(self));
138 return;
139 }
140 t->t_iot = maa->maa_st;
141 for (slot = 0; slot < PCKBPORT_NSLOTS; slot++)
142 t->t_present[slot] = 0;
143 if (bus_space_subregion(t->t_iot, maa->maa_sh, maa->maa_offset,
144 0, &t->t_ioh[PCKBPORT_KBD_SLOT]) != 0) {
145 aprint_error("%s: couldn't map kbd registers\n",
146 device_xname(self));
147 return;
148 }
149 if (bus_space_subregion(t->t_iot, maa->maa_sh, maa->maa_offset + 32,
150 0, &t->t_ioh[PCKBPORT_AUX_SLOT]) != 0) {
151 aprint_error("%s: couldn't map aux registers\n",
152 device_xname(self));
153 return;
154 }
155
156 if ((t->t_rxih = cpu_intr_establish(maa->maa_intr, maa->maa_intrmask,
157 macekbc_intr, t)) == NULL) {
158 printf("%s: couldn't establish interrupt\n",
159 device_xname(self));
160 return;
161 }
162 sc->sc_id = t;
163 t->t_sc = sc;
164
165 macekbc_reset(t, PCKBPORT_KBD_SLOT);
166 macekbc_reset(t, PCKBPORT_AUX_SLOT);
167
168 consdev = ARCBIOS->GetEnvironmentVariable("ConsoleIn");
169 if (consdev != NULL && strcmp(consdev, "keyboard()") == 0)
170 pckbport_cnattach(t, &macekbc_ops, PCKBPORT_KBD_SLOT);
171
172 t->t_pt = pckbport_attach(t, &macekbc_ops);
173 if (pckbport_attach_slot(&sc->sc_dev, t->t_pt, PCKBPORT_KBD_SLOT))
174 t->t_present[PCKBPORT_KBD_SLOT] = 1;
175 if (pckbport_attach_slot(&sc->sc_dev, t->t_pt, PCKBPORT_AUX_SLOT))
176 t->t_present[PCKBPORT_AUX_SLOT] = 1;
177
178 return;
179 }
180
181 static int
182 macekbc_intr(void *opaque)
183 {
184 struct macekbc_internal *t;
185 bus_space_tag_t iot;
186 bus_space_handle_t ioh;
187 uint64_t stat, val;
188 pckbport_slot_t slot;
189 int rv;
190
191 t = opaque;
192 iot = t->t_iot;
193 rv = 0;
194
195 for (slot = 0; slot < PCKBPORT_NSLOTS; slot++) {
196 if (t->t_present[slot] == 0)
197 continue;
198
199 ioh = t->t_ioh[slot];
200 stat = bus_space_read_8(iot, ioh, MACEKBC_STAT);
201 if (stat & MACEKBC_STAT_RXFULL) {
202 val = bus_space_read_8(iot, ioh, MACEKBC_RX);
203 pckbportintr(t->t_pt, slot, val & 0xff);
204 rv = 1;
205 }
206 }
207
208 return rv;
209 }
210
211 static void
212 macekbc_reset(struct macekbc_internal *t, pckbport_slot_t slot)
213 {
214 bus_space_tag_t iot;
215 bus_space_handle_t ioh;
216 uint64_t val;
217
218 iot = t->t_iot;
219 ioh = t->t_ioh[slot];
220
221 val = bus_space_read_8(iot, ioh, MACEKBC_CTRL);
222 val |= MACEKBC_CTRL_TXCLKOFF | MACEKBC_CTRL_RESET;
223 bus_space_write_8(iot, ioh, MACEKBC_CTRL, val);
224
225 delay(10000);
226
227 val &= ~(MACEKBC_CTRL_TXCLKOFF | MACEKBC_CTRL_RESET);
228 val |= MACEKBC_CTRL_TXON | MACEKBC_CTRL_RXCLKON | MACEKBC_CTRL_RXINTEN;
229 bus_space_write_8(iot, ioh, MACEKBC_CTRL, val);
230
231 return;
232 }
233
234 static int
235 macekbc_wait(struct macekbc_internal *t, pckbport_slot_t slot,
236 uint64_t mask, bool set)
237 {
238 bus_space_tag_t iot;
239 bus_space_handle_t ioh;
240 uint64_t val, tmp;
241 int timeout;
242
243 iot = t->t_iot;
244 ioh = t->t_ioh[slot];
245 val = (set ? mask : 0);
246 timeout = 1000;
247
248 while (timeout-- > 0) {
249 tmp = bus_space_read_8(iot, ioh, MACEKBC_STAT);
250 if ((tmp & mask) == val)
251 return 0;
252 delay(10);
253 }
254
255 return 1;
256 }
257
258 static int
259 macekbc_xt_translation(void *opaque, pckbport_slot_t port, int on)
260 {
261
262 if (on)
263 return 0;
264
265 return 1;
266 }
267
268 static int
269 macekbc_send_devcmd(void *opaque, pckbport_slot_t slot, u_char byte)
270 {
271 struct macekbc_internal *t;
272 bus_space_tag_t iot;
273 bus_space_handle_t ioh;
274
275 t = opaque;
276 iot = t->t_iot;
277 ioh = t->t_ioh[slot];
278
279 if (macekbc_wait(t, slot, MACEKBC_STAT_TXEMPTY, 1))
280 return 0;
281
282 bus_space_write_8(iot, ioh, MACEKBC_TX, byte & 0xff);
283
284 return 1;
285 }
286
287 static int
288 macekbc_poll_data1(void *opaque, pckbport_slot_t slot)
289 {
290 struct macekbc_internal *t;
291 bus_space_tag_t iot;
292 bus_space_handle_t ioh;
293
294 t = opaque;
295 iot = t->t_iot;
296 ioh = t->t_ioh[slot];
297
298 if (macekbc_wait(t, slot, MACEKBC_STAT_RXFULL, 1)) /* rx full */
299 return -1;
300
301 return bus_space_read_8(iot, ioh, MACEKBC_RX) & 0xff;
302 }
303
304 static void
305 macekbc_slot_enable(void *opaque, pckbport_slot_t slot, int on)
306 {
307 struct macekbc_internal *t;
308 bus_space_tag_t iot;
309 bus_space_handle_t ioh;
310 uint64_t val;
311
312 t = opaque;
313 iot = t->t_iot;
314 ioh = t->t_ioh[slot];
315
316 val = bus_space_read_8(iot, ioh, MACEKBC_CTRL);
317 if (on)
318 val |= MACEKBC_CTRL_TXON | MACEKBC_CTRL_RXCLKON;
319 else
320 val &= ~(MACEKBC_CTRL_TXON | MACEKBC_CTRL_RXCLKON);
321 bus_space_write_8(iot, ioh, MACEKBC_CTRL, val);
322
323 return;
324 }
325
326 static void
327 macekbc_intr_establish(void *opaque, pckbport_slot_t slot)
328 {
329
330 /* XXX */
331 macekbc_set_poll(opaque, slot, 0);
332
333 return;
334 }
335
336 static void
337 macekbc_set_poll(void *opaque, pckbport_slot_t slot, int on)
338 {
339 struct macekbc_internal *t;
340 bus_space_tag_t iot;
341 bus_space_handle_t ioh;
342 uint64_t val;
343
344 t = opaque;
345 iot = t->t_iot;
346 ioh = t->t_ioh[slot];
347
348 val = bus_space_read_8(iot, ioh, MACEKBC_CTRL);
349 if (on)
350 val &= ~MACEKBC_CTRL_RXINTEN;
351 else
352 val |= MACEKBC_CTRL_RXINTEN;
353 bus_space_write_8(iot, ioh, MACEKBC_CTRL, val);
354
355 return;
356 }
357