macekbc.c revision 1.1 1 /* $NetBSD: macekbc.c,v 1.1 2007/04/10 23:44:10 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.1 2007/04/10 23:44:10 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/pckbport/pckbportvar.h>
53
54 #define MACEKBC_TX 0x00
55 #define MACEKBC_RX 0x08
56 #define MACEKBC_CTRL 0x10
57 #define MACEKBC_CTRL_TXCLKOFF (1 << 0)
58 #define MACEKBC_CTRL_TXON (1 << 1)
59 #define MACEKBC_CTRL_TXINTEN (1 << 2)
60 #define MACEKBC_CTRL_RXINTEN (1 << 3)
61 #define MACEKBC_CTRL_RXCLKON (1 << 4)
62 #define MACEKBC_CTRL_RESET (1 << 5)
63 #define MACEKBC_STAT 0x18
64 #define MACEKBC_STAT_TXEMPTY (1 << 3)
65 #define MACEKBC_STAT_RXFULL (1 << 4)
66
67 struct macekbc_softc {
68 struct device sc_dev;
69 struct macekbc_internal *sc_id;
70
71 bus_space_tag_t sc_iot;
72 bus_space_handle_t sc_ioh;
73 };
74
75 struct macekbc_internal {
76 struct macekbc_softc *t_sc;
77 pckbport_tag_t t_pt;
78
79 bus_space_tag_t t_iot;
80 bus_space_handle_t t_ioh[PCKBPORT_NSLOTS];
81
82 void *t_rxih;
83 };
84
85 static int macekbc_intr(void *);
86 static void macekbc_reset(struct macekbc_internal *, pckbport_slot_t);
87
88 static int macekbc_xt_translation(void *, pckbport_slot_t, int);
89 static int macekbc_send_devcmd(void *, pckbport_slot_t, u_char);
90 static int macekbc_poll_data1(void *, pckbport_slot_t);
91 static void macekbc_slot_enable(void *, pckbport_slot_t, int);
92 static void macekbc_intr_establish(void *, pckbport_slot_t);
93 static void macekbc_set_poll(void *, pckbport_slot_t, int);
94
95 static int macekbc_match(struct device *, struct cfdata *, void *);
96 static void macekbc_attach(struct device *, struct device *, void *);
97
98 CFATTACH_DECL(macekbc, sizeof(struct macekbc_softc),
99 macekbc_match, macekbc_attach, NULL, NULL);
100
101 static struct pckbport_accessops macekbc_ops = {
102 .t_xt_translation = macekbc_xt_translation,
103 .t_send_devcmd = macekbc_send_devcmd,
104 .t_poll_data1 = macekbc_poll_data1,
105 .t_slot_enable = macekbc_slot_enable,
106 .t_intr_establish = macekbc_intr_establish,
107 .t_set_poll = macekbc_set_poll,
108 };
109
110 static int
111 macekbc_match(struct device *parent, struct cfdata *match, void *aux)
112 {
113 return 1;
114 }
115
116 static void
117 macekbc_attach(struct device *parent, struct device *self, void *aux)
118 {
119 struct mace_attach_args *maa;
120 struct macekbc_softc *sc;
121 struct macekbc_internal *t;
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 t->t_iot = maa->maa_st;
131 if (bus_space_subregion(t->t_iot, maa->maa_sh, maa->maa_offset,
132 0, &t->t_ioh[PCKBPORT_KBD_SLOT]) != 0) {
133 aprint_error("%s: couldn't map kbd registers\n",
134 device_xname(self));
135 return;
136 }
137 if (bus_space_subregion(t->t_iot, maa->maa_sh, maa->maa_offset + 32,
138 0, &t->t_ioh[PCKBPORT_AUX_SLOT]) != 0) {
139 aprint_error("%s: couldn't map aux registers\n",
140 device_xname(self));
141 return;
142 }
143
144 if ((t->t_rxih = cpu_intr_establish(maa->maa_intr, maa->maa_intrmask,
145 macekbc_intr, t)) == NULL) {
146 printf("%s: couldn't establish interrupt\n",
147 device_xname(self));
148 return;
149 }
150 sc->sc_id = t;
151 t->t_sc = sc;
152
153 macekbc_reset(t, PCKBPORT_KBD_SLOT);
154 macekbc_reset(t, PCKBPORT_AUX_SLOT);
155
156 t->t_pt = pckbport_attach(t, &macekbc_ops);
157 pckbport_attach_slot(&sc->sc_dev, t->t_pt, PCKBPORT_KBD_SLOT);
158 pckbport_attach_slot(&sc->sc_dev, t->t_pt, PCKBPORT_AUX_SLOT);
159
160 return;
161 }
162
163 static int
164 macekbc_intr(void *opaque)
165 {
166 struct macekbc_internal *t;
167 bus_space_tag_t iot;
168 bus_space_handle_t ioh;
169 uint64_t stat, val;
170 pckbport_slot_t slot;
171 int rv;
172
173 t = opaque;
174 iot = t->t_iot;
175 rv = 0;
176
177 for (slot = 0; slot < PCKBPORT_NSLOTS; slot++) {
178 ioh = t->t_ioh[slot];
179 stat = bus_space_read_8(iot, ioh, MACEKBC_STAT);
180 if (stat & MACEKBC_STAT_RXFULL) {
181 val = bus_space_read_8(iot, ioh, MACEKBC_RX);
182 pckbportintr(t->t_pt, slot, val & 0xff);
183 rv = 1;
184 }
185 }
186
187 return rv;
188 }
189
190 static void
191 macekbc_reset(struct macekbc_internal *t, pckbport_slot_t slot)
192 {
193 bus_space_tag_t iot;
194 bus_space_handle_t ioh;
195 uint64_t val;
196
197 iot = t->t_iot;
198 ioh = t->t_ioh[slot];
199
200 val = bus_space_read_8(iot, ioh, MACEKBC_CTRL);
201 val |= MACEKBC_CTRL_TXCLKOFF | MACEKBC_CTRL_RESET;
202 bus_space_write_8(iot, ioh, MACEKBC_CTRL, val);
203
204 delay(10000);
205
206 val &= ~(MACEKBC_CTRL_TXCLKOFF | MACEKBC_CTRL_RESET);
207 val |= MACEKBC_CTRL_TXON | MACEKBC_CTRL_RXCLKON | MACEKBC_CTRL_RXINTEN;
208 bus_space_write_8(iot, ioh, MACEKBC_CTRL, val);
209
210 return;
211 }
212
213 static int
214 macekbc_wait(struct macekbc_internal *t, pckbport_slot_t slot,
215 uint64_t mask, bool set)
216 {
217 bus_space_tag_t iot;
218 bus_space_handle_t ioh;
219 uint64_t val, tmp;
220 int timeout;
221
222 iot = t->t_iot;
223 ioh = t->t_ioh[slot];
224 val = (set ? mask : 0);
225 timeout = 10000;
226
227 while (timeout-- > 0) {
228 tmp = bus_space_read_8(iot, ioh, MACEKBC_STAT);
229 if ((tmp & mask) == val)
230 return 0;
231 delay(10);
232 }
233
234 return 1;
235 }
236
237 static int
238 macekbc_xt_translation(void *opaque, pckbport_slot_t port, int on)
239 {
240
241 if (on)
242 return 0;
243
244 return 1;
245 }
246
247 static int
248 macekbc_send_devcmd(void *opaque, pckbport_slot_t slot, u_char byte)
249 {
250 struct macekbc_internal *t;
251 bus_space_tag_t iot;
252 bus_space_handle_t ioh;
253
254 t = opaque;
255 iot = t->t_iot;
256 ioh = t->t_ioh[slot];
257
258 if (macekbc_wait(t, slot, MACEKBC_STAT_TXEMPTY, 1))
259 return 0;
260
261 bus_space_write_8(iot, ioh, MACEKBC_TX, byte & 0xff);
262
263 return 1;
264 }
265
266 static int
267 macekbc_poll_data1(void *opaque, pckbport_slot_t slot)
268 {
269 struct macekbc_internal *t;
270 bus_space_tag_t iot;
271 bus_space_handle_t ioh;
272
273 t = opaque;
274 iot = t->t_iot;
275 ioh = t->t_ioh[slot];
276
277 if (macekbc_wait(t, slot, MACEKBC_STAT_RXFULL, 1)) /* rx full */
278 return -1;
279
280 return bus_space_read_8(iot, ioh, MACEKBC_RX) & 0xff;
281 }
282
283 static void
284 macekbc_slot_enable(void *opaque, pckbport_slot_t slot, int on)
285 {
286 struct macekbc_internal *t;
287 bus_space_tag_t iot;
288 bus_space_handle_t ioh;
289 uint64_t val;
290
291 t = opaque;
292 iot = t->t_iot;
293 ioh = t->t_ioh[slot];
294
295 val = bus_space_read_8(iot, ioh, MACEKBC_CTRL);
296 if (on)
297 val |= MACEKBC_CTRL_TXON | MACEKBC_CTRL_RXCLKON;
298 else
299 val &= ~(MACEKBC_CTRL_TXON | MACEKBC_CTRL_RXCLKON);
300 bus_space_write_8(iot, ioh, MACEKBC_CTRL, val);
301
302 return;
303 }
304
305 static void
306 macekbc_intr_establish(void *opaque, pckbport_slot_t slot)
307 {
308
309 /* XXX */
310 macekbc_set_poll(opaque, slot, 0);
311
312 return;
313 }
314
315 static void
316 macekbc_set_poll(void *opaque, pckbport_slot_t slot, int on)
317 {
318 struct macekbc_internal *t;
319 bus_space_tag_t iot;
320 bus_space_handle_t ioh;
321 uint64_t val;
322
323 t = opaque;
324 iot = t->t_iot;
325 ioh = t->t_ioh[slot];
326
327 val = bus_space_read_8(iot, ioh, MACEKBC_CTRL);
328 if (on)
329 val &= ~MACEKBC_CTRL_RXINTEN;
330 else
331 val |= MACEKBC_CTRL_RXINTEN;
332 bus_space_write_8(iot, ioh, MACEKBC_CTRL, val);
333
334 return;
335 }
336